phy-bcm-ns-usb3.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Broadcom Northstar USB 3.0 PHY Driver
  4. *
  5. * Copyright (C) 2016 Rafał Miłecki <[email protected]>
  6. * Copyright (C) 2016 Broadcom
  7. *
  8. * All magic values used for initialization (and related comments) were obtained
  9. * from Broadcom's SDK:
  10. * Copyright (c) Broadcom Corp, 2012
  11. */
  12. #include <linux/bcma/bcma.h>
  13. #include <linux/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/iopoll.h>
  16. #include <linux/mdio.h>
  17. #include <linux/module.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/phy/phy.h>
  22. #include <linux/slab.h>
  23. #define BCM_NS_USB3_PHY_BASE_ADDR_REG 0x1f
  24. #define BCM_NS_USB3_PHY_PLL30_BLOCK 0x8000
  25. #define BCM_NS_USB3_PHY_TX_PMD_BLOCK 0x8040
  26. #define BCM_NS_USB3_PHY_PIPE_BLOCK 0x8060
  27. /* Registers of PLL30 block */
  28. #define BCM_NS_USB3_PLL_CONTROL 0x01
  29. #define BCM_NS_USB3_PLLA_CONTROL0 0x0a
  30. #define BCM_NS_USB3_PLLA_CONTROL1 0x0b
  31. /* Registers of TX PMD block */
  32. #define BCM_NS_USB3_TX_PMD_CONTROL1 0x01
  33. /* Registers of PIPE block */
  34. #define BCM_NS_USB3_LFPS_CMP 0x02
  35. #define BCM_NS_USB3_LFPS_DEGLITCH 0x03
  36. enum bcm_ns_family {
  37. BCM_NS_UNKNOWN,
  38. BCM_NS_AX,
  39. BCM_NS_BX,
  40. };
  41. struct bcm_ns_usb3 {
  42. struct device *dev;
  43. enum bcm_ns_family family;
  44. void __iomem *dmp;
  45. struct mdio_device *mdiodev;
  46. struct phy *phy;
  47. };
  48. static const struct of_device_id bcm_ns_usb3_id_table[] = {
  49. {
  50. .compatible = "brcm,ns-ax-usb3-phy",
  51. .data = (int *)BCM_NS_AX,
  52. },
  53. {
  54. .compatible = "brcm,ns-bx-usb3-phy",
  55. .data = (int *)BCM_NS_BX,
  56. },
  57. {},
  58. };
  59. static int bcm_ns_usb3_mdio_phy_write(struct bcm_ns_usb3 *usb3, u16 reg,
  60. u16 value);
  61. static int bcm_ns_usb3_phy_init_ns_bx(struct bcm_ns_usb3 *usb3)
  62. {
  63. int err;
  64. /* USB3 PLL Block */
  65. err = bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PHY_BASE_ADDR_REG,
  66. BCM_NS_USB3_PHY_PLL30_BLOCK);
  67. if (err < 0)
  68. return err;
  69. /* Assert Ana_Pllseq start */
  70. bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PLL_CONTROL, 0x1000);
  71. /* Assert CML Divider ratio to 26 */
  72. bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PLLA_CONTROL0, 0x6400);
  73. /* Asserting PLL Reset */
  74. bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PLLA_CONTROL1, 0xc000);
  75. /* Deaaserting PLL Reset */
  76. bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PLLA_CONTROL1, 0x8000);
  77. /* Deasserting USB3 system reset */
  78. writel(0, usb3->dmp + BCMA_RESET_CTL);
  79. /* PLL frequency monitor enable */
  80. bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PLL_CONTROL, 0x9000);
  81. /* PIPE Block */
  82. bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PHY_BASE_ADDR_REG,
  83. BCM_NS_USB3_PHY_PIPE_BLOCK);
  84. /* CMPMAX & CMPMINTH setting */
  85. bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_LFPS_CMP, 0xf30d);
  86. /* DEGLITCH MIN & MAX setting */
  87. bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_LFPS_DEGLITCH, 0x6302);
  88. /* TXPMD block */
  89. bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PHY_BASE_ADDR_REG,
  90. BCM_NS_USB3_PHY_TX_PMD_BLOCK);
  91. /* Enabling SSC */
  92. bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_TX_PMD_CONTROL1, 0x1003);
  93. return 0;
  94. }
  95. static int bcm_ns_usb3_phy_init_ns_ax(struct bcm_ns_usb3 *usb3)
  96. {
  97. int err;
  98. /* PLL30 block */
  99. err = bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PHY_BASE_ADDR_REG,
  100. BCM_NS_USB3_PHY_PLL30_BLOCK);
  101. if (err < 0)
  102. return err;
  103. bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PLLA_CONTROL0, 0x6400);
  104. bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PHY_BASE_ADDR_REG, 0x80e0);
  105. bcm_ns_usb3_mdio_phy_write(usb3, 0x02, 0x009c);
  106. /* Enable SSC */
  107. bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PHY_BASE_ADDR_REG,
  108. BCM_NS_USB3_PHY_TX_PMD_BLOCK);
  109. bcm_ns_usb3_mdio_phy_write(usb3, 0x02, 0x21d3);
  110. bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_TX_PMD_CONTROL1, 0x1003);
  111. /* Deasserting USB3 system reset */
  112. writel(0, usb3->dmp + BCMA_RESET_CTL);
  113. return 0;
  114. }
  115. static int bcm_ns_usb3_phy_init(struct phy *phy)
  116. {
  117. struct bcm_ns_usb3 *usb3 = phy_get_drvdata(phy);
  118. int err;
  119. /* Perform USB3 system soft reset */
  120. writel(BCMA_RESET_CTL_RESET, usb3->dmp + BCMA_RESET_CTL);
  121. switch (usb3->family) {
  122. case BCM_NS_AX:
  123. err = bcm_ns_usb3_phy_init_ns_ax(usb3);
  124. break;
  125. case BCM_NS_BX:
  126. err = bcm_ns_usb3_phy_init_ns_bx(usb3);
  127. break;
  128. default:
  129. WARN_ON(1);
  130. err = -ENOTSUPP;
  131. }
  132. return err;
  133. }
  134. static const struct phy_ops ops = {
  135. .init = bcm_ns_usb3_phy_init,
  136. .owner = THIS_MODULE,
  137. };
  138. /**************************************************
  139. * MDIO driver code
  140. **************************************************/
  141. static int bcm_ns_usb3_mdio_phy_write(struct bcm_ns_usb3 *usb3, u16 reg,
  142. u16 value)
  143. {
  144. struct mdio_device *mdiodev = usb3->mdiodev;
  145. return mdiodev_write(mdiodev, reg, value);
  146. }
  147. static int bcm_ns_usb3_mdio_probe(struct mdio_device *mdiodev)
  148. {
  149. struct device *dev = &mdiodev->dev;
  150. const struct of_device_id *of_id;
  151. struct phy_provider *phy_provider;
  152. struct device_node *syscon_np;
  153. struct bcm_ns_usb3 *usb3;
  154. struct resource res;
  155. int err;
  156. usb3 = devm_kzalloc(dev, sizeof(*usb3), GFP_KERNEL);
  157. if (!usb3)
  158. return -ENOMEM;
  159. usb3->dev = dev;
  160. usb3->mdiodev = mdiodev;
  161. of_id = of_match_device(bcm_ns_usb3_id_table, dev);
  162. if (!of_id)
  163. return -EINVAL;
  164. usb3->family = (enum bcm_ns_family)of_id->data;
  165. syscon_np = of_parse_phandle(dev->of_node, "usb3-dmp-syscon", 0);
  166. err = of_address_to_resource(syscon_np, 0, &res);
  167. of_node_put(syscon_np);
  168. if (err)
  169. return err;
  170. usb3->dmp = devm_ioremap_resource(dev, &res);
  171. if (IS_ERR(usb3->dmp))
  172. return PTR_ERR(usb3->dmp);
  173. usb3->phy = devm_phy_create(dev, NULL, &ops);
  174. if (IS_ERR(usb3->phy)) {
  175. dev_err(dev, "Failed to create PHY\n");
  176. return PTR_ERR(usb3->phy);
  177. }
  178. phy_set_drvdata(usb3->phy, usb3);
  179. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  180. return PTR_ERR_OR_ZERO(phy_provider);
  181. }
  182. static struct mdio_driver bcm_ns_usb3_mdio_driver = {
  183. .mdiodrv = {
  184. .driver = {
  185. .name = "bcm_ns_mdio_usb3",
  186. .of_match_table = bcm_ns_usb3_id_table,
  187. },
  188. },
  189. .probe = bcm_ns_usb3_mdio_probe,
  190. };
  191. mdio_module_driver(bcm_ns_usb3_mdio_driver);
  192. MODULE_LICENSE("GPL v2");
  193. MODULE_DEVICE_TABLE(of, bcm_ns_usb3_id_table);