phy-bcm-sr-usb.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2016-2018 Broadcom
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/io.h>
  7. #include <linux/iopoll.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/phy/phy.h>
  11. #include <linux/platform_device.h>
  12. enum bcm_usb_phy_version {
  13. BCM_SR_USB_COMBO_PHY,
  14. BCM_SR_USB_HS_PHY,
  15. };
  16. enum bcm_usb_phy_reg {
  17. PLL_CTRL,
  18. PHY_CTRL,
  19. PHY_PLL_CTRL,
  20. };
  21. /* USB PHY registers */
  22. static const u8 bcm_usb_combo_phy_ss[] = {
  23. [PLL_CTRL] = 0x18,
  24. [PHY_CTRL] = 0x14,
  25. };
  26. static const u8 bcm_usb_combo_phy_hs[] = {
  27. [PLL_CTRL] = 0x0c,
  28. [PHY_CTRL] = 0x10,
  29. };
  30. static const u8 bcm_usb_hs_phy[] = {
  31. [PLL_CTRL] = 0x8,
  32. [PHY_CTRL] = 0xc,
  33. };
  34. enum pll_ctrl_bits {
  35. PLL_RESETB,
  36. SSPLL_SUSPEND_EN,
  37. PLL_SEQ_START,
  38. PLL_LOCK,
  39. };
  40. static const u8 u3pll_ctrl[] = {
  41. [PLL_RESETB] = 0,
  42. [SSPLL_SUSPEND_EN] = 1,
  43. [PLL_SEQ_START] = 2,
  44. [PLL_LOCK] = 3,
  45. };
  46. #define HSPLL_PDIV_MASK 0xF
  47. #define HSPLL_PDIV_VAL 0x1
  48. static const u8 u2pll_ctrl[] = {
  49. [PLL_RESETB] = 5,
  50. [PLL_LOCK] = 6,
  51. };
  52. enum bcm_usb_phy_ctrl_bits {
  53. CORERDY,
  54. PHY_RESETB,
  55. PHY_PCTL,
  56. };
  57. #define PHY_PCTL_MASK 0xffff
  58. #define SSPHY_PCTL_VAL 0x0006
  59. static const u8 u3phy_ctrl[] = {
  60. [PHY_RESETB] = 1,
  61. [PHY_PCTL] = 2,
  62. };
  63. static const u8 u2phy_ctrl[] = {
  64. [CORERDY] = 0,
  65. [PHY_RESETB] = 5,
  66. [PHY_PCTL] = 6,
  67. };
  68. struct bcm_usb_phy_cfg {
  69. uint32_t type;
  70. uint32_t version;
  71. void __iomem *regs;
  72. struct phy *phy;
  73. const u8 *offset;
  74. };
  75. #define PLL_LOCK_RETRY_COUNT 1000
  76. enum bcm_usb_phy_type {
  77. USB_HS_PHY,
  78. USB_SS_PHY,
  79. };
  80. #define NUM_BCM_SR_USB_COMBO_PHYS 2
  81. static inline void bcm_usb_reg32_clrbits(void __iomem *addr, uint32_t clear)
  82. {
  83. writel(readl(addr) & ~clear, addr);
  84. }
  85. static inline void bcm_usb_reg32_setbits(void __iomem *addr, uint32_t set)
  86. {
  87. writel(readl(addr) | set, addr);
  88. }
  89. static int bcm_usb_pll_lock_check(void __iomem *addr, u32 bit)
  90. {
  91. u32 data;
  92. int ret;
  93. ret = readl_poll_timeout_atomic(addr, data, (data & bit), 1,
  94. PLL_LOCK_RETRY_COUNT);
  95. if (ret)
  96. pr_err("%s: FAIL\n", __func__);
  97. return ret;
  98. }
  99. static int bcm_usb_ss_phy_init(struct bcm_usb_phy_cfg *phy_cfg)
  100. {
  101. int ret = 0;
  102. void __iomem *regs = phy_cfg->regs;
  103. const u8 *offset;
  104. u32 rd_data;
  105. offset = phy_cfg->offset;
  106. /* Set pctl with mode and soft reset */
  107. rd_data = readl(regs + offset[PHY_CTRL]);
  108. rd_data &= ~(PHY_PCTL_MASK << u3phy_ctrl[PHY_PCTL]);
  109. rd_data |= (SSPHY_PCTL_VAL << u3phy_ctrl[PHY_PCTL]);
  110. writel(rd_data, regs + offset[PHY_CTRL]);
  111. bcm_usb_reg32_clrbits(regs + offset[PLL_CTRL],
  112. BIT(u3pll_ctrl[SSPLL_SUSPEND_EN]));
  113. bcm_usb_reg32_setbits(regs + offset[PLL_CTRL],
  114. BIT(u3pll_ctrl[PLL_SEQ_START]));
  115. bcm_usb_reg32_setbits(regs + offset[PLL_CTRL],
  116. BIT(u3pll_ctrl[PLL_RESETB]));
  117. /* Maximum timeout for PLL reset done */
  118. msleep(30);
  119. ret = bcm_usb_pll_lock_check(regs + offset[PLL_CTRL],
  120. BIT(u3pll_ctrl[PLL_LOCK]));
  121. return ret;
  122. }
  123. static int bcm_usb_hs_phy_init(struct bcm_usb_phy_cfg *phy_cfg)
  124. {
  125. int ret = 0;
  126. void __iomem *regs = phy_cfg->regs;
  127. const u8 *offset;
  128. offset = phy_cfg->offset;
  129. bcm_usb_reg32_clrbits(regs + offset[PLL_CTRL],
  130. BIT(u2pll_ctrl[PLL_RESETB]));
  131. bcm_usb_reg32_setbits(regs + offset[PLL_CTRL],
  132. BIT(u2pll_ctrl[PLL_RESETB]));
  133. ret = bcm_usb_pll_lock_check(regs + offset[PLL_CTRL],
  134. BIT(u2pll_ctrl[PLL_LOCK]));
  135. return ret;
  136. }
  137. static int bcm_usb_phy_reset(struct phy *phy)
  138. {
  139. struct bcm_usb_phy_cfg *phy_cfg = phy_get_drvdata(phy);
  140. void __iomem *regs = phy_cfg->regs;
  141. const u8 *offset;
  142. offset = phy_cfg->offset;
  143. if (phy_cfg->type == USB_HS_PHY) {
  144. bcm_usb_reg32_clrbits(regs + offset[PHY_CTRL],
  145. BIT(u2phy_ctrl[CORERDY]));
  146. bcm_usb_reg32_setbits(regs + offset[PHY_CTRL],
  147. BIT(u2phy_ctrl[CORERDY]));
  148. }
  149. return 0;
  150. }
  151. static int bcm_usb_phy_init(struct phy *phy)
  152. {
  153. struct bcm_usb_phy_cfg *phy_cfg = phy_get_drvdata(phy);
  154. int ret = -EINVAL;
  155. if (phy_cfg->type == USB_SS_PHY)
  156. ret = bcm_usb_ss_phy_init(phy_cfg);
  157. else if (phy_cfg->type == USB_HS_PHY)
  158. ret = bcm_usb_hs_phy_init(phy_cfg);
  159. return ret;
  160. }
  161. static const struct phy_ops sr_phy_ops = {
  162. .init = bcm_usb_phy_init,
  163. .reset = bcm_usb_phy_reset,
  164. .owner = THIS_MODULE,
  165. };
  166. static struct phy *bcm_usb_phy_xlate(struct device *dev,
  167. struct of_phandle_args *args)
  168. {
  169. struct bcm_usb_phy_cfg *phy_cfg;
  170. int phy_idx;
  171. phy_cfg = dev_get_drvdata(dev);
  172. if (!phy_cfg)
  173. return ERR_PTR(-EINVAL);
  174. if (phy_cfg->version == BCM_SR_USB_COMBO_PHY) {
  175. phy_idx = args->args[0];
  176. if (WARN_ON(phy_idx > 1))
  177. return ERR_PTR(-ENODEV);
  178. return phy_cfg[phy_idx].phy;
  179. } else
  180. return phy_cfg->phy;
  181. }
  182. static int bcm_usb_phy_create(struct device *dev, struct device_node *node,
  183. void __iomem *regs, uint32_t version)
  184. {
  185. struct bcm_usb_phy_cfg *phy_cfg;
  186. int idx;
  187. if (version == BCM_SR_USB_COMBO_PHY) {
  188. phy_cfg = devm_kzalloc(dev, NUM_BCM_SR_USB_COMBO_PHYS *
  189. sizeof(struct bcm_usb_phy_cfg),
  190. GFP_KERNEL);
  191. if (!phy_cfg)
  192. return -ENOMEM;
  193. for (idx = 0; idx < NUM_BCM_SR_USB_COMBO_PHYS; idx++) {
  194. phy_cfg[idx].regs = regs;
  195. phy_cfg[idx].version = version;
  196. if (idx == 0) {
  197. phy_cfg[idx].offset = bcm_usb_combo_phy_hs;
  198. phy_cfg[idx].type = USB_HS_PHY;
  199. } else {
  200. phy_cfg[idx].offset = bcm_usb_combo_phy_ss;
  201. phy_cfg[idx].type = USB_SS_PHY;
  202. }
  203. phy_cfg[idx].phy = devm_phy_create(dev, node,
  204. &sr_phy_ops);
  205. if (IS_ERR(phy_cfg[idx].phy))
  206. return PTR_ERR(phy_cfg[idx].phy);
  207. phy_set_drvdata(phy_cfg[idx].phy, &phy_cfg[idx]);
  208. }
  209. } else if (version == BCM_SR_USB_HS_PHY) {
  210. phy_cfg = devm_kzalloc(dev, sizeof(struct bcm_usb_phy_cfg),
  211. GFP_KERNEL);
  212. if (!phy_cfg)
  213. return -ENOMEM;
  214. phy_cfg->regs = regs;
  215. phy_cfg->version = version;
  216. phy_cfg->offset = bcm_usb_hs_phy;
  217. phy_cfg->type = USB_HS_PHY;
  218. phy_cfg->phy = devm_phy_create(dev, node, &sr_phy_ops);
  219. if (IS_ERR(phy_cfg->phy))
  220. return PTR_ERR(phy_cfg->phy);
  221. phy_set_drvdata(phy_cfg->phy, phy_cfg);
  222. } else
  223. return -ENODEV;
  224. dev_set_drvdata(dev, phy_cfg);
  225. return 0;
  226. }
  227. static const struct of_device_id bcm_usb_phy_of_match[] = {
  228. {
  229. .compatible = "brcm,sr-usb-combo-phy",
  230. .data = (void *)BCM_SR_USB_COMBO_PHY,
  231. },
  232. {
  233. .compatible = "brcm,sr-usb-hs-phy",
  234. .data = (void *)BCM_SR_USB_HS_PHY,
  235. },
  236. { /* sentinel */ },
  237. };
  238. MODULE_DEVICE_TABLE(of, bcm_usb_phy_of_match);
  239. static int bcm_usb_phy_probe(struct platform_device *pdev)
  240. {
  241. struct device *dev = &pdev->dev;
  242. struct device_node *dn = dev->of_node;
  243. const struct of_device_id *of_id;
  244. struct resource *res;
  245. void __iomem *regs;
  246. int ret;
  247. enum bcm_usb_phy_version version;
  248. struct phy_provider *phy_provider;
  249. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  250. regs = devm_ioremap_resource(dev, res);
  251. if (IS_ERR(regs))
  252. return PTR_ERR(regs);
  253. of_id = of_match_node(bcm_usb_phy_of_match, dn);
  254. if (of_id)
  255. version = (enum bcm_usb_phy_version)of_id->data;
  256. else
  257. return -ENODEV;
  258. ret = bcm_usb_phy_create(dev, dn, regs, version);
  259. if (ret)
  260. return ret;
  261. phy_provider = devm_of_phy_provider_register(dev, bcm_usb_phy_xlate);
  262. return PTR_ERR_OR_ZERO(phy_provider);
  263. }
  264. static struct platform_driver bcm_usb_phy_driver = {
  265. .driver = {
  266. .name = "phy-bcm-sr-usb",
  267. .of_match_table = bcm_usb_phy_of_match,
  268. },
  269. .probe = bcm_usb_phy_probe,
  270. };
  271. module_platform_driver(bcm_usb_phy_driver);
  272. MODULE_AUTHOR("Broadcom");
  273. MODULE_DESCRIPTION("Broadcom stingray USB Phy driver");
  274. MODULE_LICENSE("GPL v2");