phy-bcm-ns-usb3.c 5.9 KB

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