phy-bcm-ns-usb3.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Broadcom Northstar USB 3.0 PHY Driver
  3. *
  4. * Copyright (C) 2016 Rafał Miłecki <rafal@milecki.pl>
  5. *
  6. * All magic values used for initialization (and related comments) were obtained
  7. * from Broadcom's SDK:
  8. * Copyright (c) Broadcom Corp, 2012
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/bcma/bcma.h>
  15. #include <linux/delay.h>
  16. #include <linux/err.h>
  17. #include <linux/module.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/phy/phy.h>
  21. #include <linux/slab.h>
  22. #define BCM_NS_USB3_MII_MNG_TIMEOUT_US 1000 /* usecs */
  23. enum bcm_ns_family {
  24. BCM_NS_UNKNOWN,
  25. BCM_NS_AX,
  26. BCM_NS_BX,
  27. };
  28. struct bcm_ns_usb3 {
  29. struct device *dev;
  30. enum bcm_ns_family family;
  31. void __iomem *dmp;
  32. void __iomem *ccb_mii;
  33. struct phy *phy;
  34. };
  35. static const struct of_device_id bcm_ns_usb3_id_table[] = {
  36. {
  37. .compatible = "brcm,ns-ax-usb3-phy",
  38. .data = (int *)BCM_NS_AX,
  39. },
  40. {
  41. .compatible = "brcm,ns-bx-usb3-phy",
  42. .data = (int *)BCM_NS_BX,
  43. },
  44. {},
  45. };
  46. MODULE_DEVICE_TABLE(of, bcm_ns_usb3_id_table);
  47. static int bcm_ns_usb3_wait_reg(struct bcm_ns_usb3 *usb3, void __iomem *addr,
  48. u32 mask, u32 value, unsigned long timeout)
  49. {
  50. unsigned long deadline = jiffies + timeout;
  51. u32 val;
  52. do {
  53. val = readl(addr);
  54. if ((val & mask) == value)
  55. return 0;
  56. cpu_relax();
  57. udelay(10);
  58. } while (!time_after_eq(jiffies, deadline));
  59. dev_err(usb3->dev, "Timeout waiting for register %p\n", addr);
  60. return -EBUSY;
  61. }
  62. static inline int bcm_ns_usb3_mii_mng_wait_idle(struct bcm_ns_usb3 *usb3)
  63. {
  64. return bcm_ns_usb3_wait_reg(usb3, usb3->ccb_mii + BCMA_CCB_MII_MNG_CTL,
  65. 0x0100, 0x0000,
  66. usecs_to_jiffies(BCM_NS_USB3_MII_MNG_TIMEOUT_US));
  67. }
  68. static int bcm_ns_usb3_mii_mng_write32(struct bcm_ns_usb3 *usb3, u32 value)
  69. {
  70. int err;
  71. err = bcm_ns_usb3_mii_mng_wait_idle(usb3);
  72. if (err < 0) {
  73. dev_err(usb3->dev, "Couldn't write 0x%08x value\n", value);
  74. return err;
  75. }
  76. writel(value, usb3->ccb_mii + BCMA_CCB_MII_MNG_CMD_DATA);
  77. return 0;
  78. }
  79. static int bcm_ns_usb3_phy_init_ns_bx(struct bcm_ns_usb3 *usb3)
  80. {
  81. int err;
  82. /* Enable MDIO. Setting MDCDIV as 26 */
  83. writel(0x0000009a, usb3->ccb_mii + BCMA_CCB_MII_MNG_CTL);
  84. /* Wait for MDIO? */
  85. udelay(2);
  86. /* USB3 PLL Block */
  87. err = bcm_ns_usb3_mii_mng_write32(usb3, 0x587e8000);
  88. if (err < 0)
  89. return err;
  90. /* Assert Ana_Pllseq start */
  91. bcm_ns_usb3_mii_mng_write32(usb3, 0x58061000);
  92. /* Assert CML Divider ratio to 26 */
  93. bcm_ns_usb3_mii_mng_write32(usb3, 0x582a6400);
  94. /* Asserting PLL Reset */
  95. bcm_ns_usb3_mii_mng_write32(usb3, 0x582ec000);
  96. /* Deaaserting PLL Reset */
  97. bcm_ns_usb3_mii_mng_write32(usb3, 0x582e8000);
  98. /* Waiting MII Mgt interface idle */
  99. bcm_ns_usb3_mii_mng_wait_idle(usb3);
  100. /* Deasserting USB3 system reset */
  101. writel(0, usb3->dmp + BCMA_RESET_CTL);
  102. /* PLL frequency monitor enable */
  103. bcm_ns_usb3_mii_mng_write32(usb3, 0x58069000);
  104. /* PIPE Block */
  105. bcm_ns_usb3_mii_mng_write32(usb3, 0x587e8060);
  106. /* CMPMAX & CMPMINTH setting */
  107. bcm_ns_usb3_mii_mng_write32(usb3, 0x580af30d);
  108. /* DEGLITCH MIN & MAX setting */
  109. bcm_ns_usb3_mii_mng_write32(usb3, 0x580e6302);
  110. /* TXPMD block */
  111. bcm_ns_usb3_mii_mng_write32(usb3, 0x587e8040);
  112. /* Enabling SSC */
  113. bcm_ns_usb3_mii_mng_write32(usb3, 0x58061003);
  114. /* Waiting MII Mgt interface idle */
  115. bcm_ns_usb3_mii_mng_wait_idle(usb3);
  116. return 0;
  117. }
  118. static int bcm_ns_usb3_phy_init_ns_ax(struct bcm_ns_usb3 *usb3)
  119. {
  120. int err;
  121. /* Enable MDIO. Setting MDCDIV as 26 */
  122. writel(0x0000009a, usb3->ccb_mii + BCMA_CCB_MII_MNG_CTL);
  123. /* Wait for MDIO? */
  124. udelay(2);
  125. /* PLL30 block */
  126. err = bcm_ns_usb3_mii_mng_write32(usb3, 0x587e8000);
  127. if (err < 0)
  128. return err;
  129. bcm_ns_usb3_mii_mng_write32(usb3, 0x582a6400);
  130. bcm_ns_usb3_mii_mng_write32(usb3, 0x587e80e0);
  131. bcm_ns_usb3_mii_mng_write32(usb3, 0x580a009c);
  132. /* Enable SSC */
  133. bcm_ns_usb3_mii_mng_write32(usb3, 0x587e8040);
  134. bcm_ns_usb3_mii_mng_write32(usb3, 0x580a21d3);
  135. bcm_ns_usb3_mii_mng_write32(usb3, 0x58061003);
  136. /* Waiting MII Mgt interface idle */
  137. bcm_ns_usb3_mii_mng_wait_idle(usb3);
  138. /* Deasserting USB3 system reset */
  139. writel(0, usb3->dmp + BCMA_RESET_CTL);
  140. return 0;
  141. }
  142. static int bcm_ns_usb3_phy_init(struct phy *phy)
  143. {
  144. struct bcm_ns_usb3 *usb3 = phy_get_drvdata(phy);
  145. int err;
  146. /* Perform USB3 system soft reset */
  147. writel(BCMA_RESET_CTL_RESET, usb3->dmp + BCMA_RESET_CTL);
  148. switch (usb3->family) {
  149. case BCM_NS_AX:
  150. err = bcm_ns_usb3_phy_init_ns_ax(usb3);
  151. break;
  152. case BCM_NS_BX:
  153. err = bcm_ns_usb3_phy_init_ns_bx(usb3);
  154. break;
  155. default:
  156. WARN_ON(1);
  157. err = -ENOTSUPP;
  158. }
  159. return err;
  160. }
  161. static const struct phy_ops ops = {
  162. .init = bcm_ns_usb3_phy_init,
  163. .owner = THIS_MODULE,
  164. };
  165. static int bcm_ns_usb3_probe(struct platform_device *pdev)
  166. {
  167. struct device *dev = &pdev->dev;
  168. const struct of_device_id *of_id;
  169. struct bcm_ns_usb3 *usb3;
  170. struct resource *res;
  171. struct phy_provider *phy_provider;
  172. usb3 = devm_kzalloc(dev, sizeof(*usb3), GFP_KERNEL);
  173. if (!usb3)
  174. return -ENOMEM;
  175. usb3->dev = dev;
  176. of_id = of_match_device(bcm_ns_usb3_id_table, dev);
  177. if (!of_id)
  178. return -EINVAL;
  179. usb3->family = (enum bcm_ns_family)of_id->data;
  180. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dmp");
  181. usb3->dmp = devm_ioremap_resource(dev, res);
  182. if (IS_ERR(usb3->dmp)) {
  183. dev_err(dev, "Failed to map DMP regs\n");
  184. return PTR_ERR(usb3->dmp);
  185. }
  186. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ccb-mii");
  187. usb3->ccb_mii = devm_ioremap_resource(dev, res);
  188. if (IS_ERR(usb3->ccb_mii)) {
  189. dev_err(dev, "Failed to map ChipCommon B MII regs\n");
  190. return PTR_ERR(usb3->ccb_mii);
  191. }
  192. usb3->phy = devm_phy_create(dev, NULL, &ops);
  193. if (IS_ERR(usb3->phy)) {
  194. dev_err(dev, "Failed to create PHY\n");
  195. return PTR_ERR(usb3->phy);
  196. }
  197. phy_set_drvdata(usb3->phy, usb3);
  198. platform_set_drvdata(pdev, usb3);
  199. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  200. if (!IS_ERR(phy_provider))
  201. dev_info(dev, "Registered Broadcom Northstar USB 3.0 PHY driver\n");
  202. return PTR_ERR_OR_ZERO(phy_provider);
  203. }
  204. static struct platform_driver bcm_ns_usb3_driver = {
  205. .probe = bcm_ns_usb3_probe,
  206. .driver = {
  207. .name = "bcm_ns_usb3",
  208. .of_match_table = bcm_ns_usb3_id_table,
  209. },
  210. };
  211. module_platform_driver(bcm_ns_usb3_driver);
  212. MODULE_LICENSE("GPL v2");