phy-bcm63xx-usbh.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * BCM6328 USBH PHY Controller Driver
  4. *
  5. * Copyright (C) 2020 Álvaro Fernández Rojas <[email protected]>
  6. * Copyright (C) 2015 Simon Arlott
  7. *
  8. * Derived from bcm963xx_4.12L.06B_consumer/kernel/linux/arch/mips/bcm963xx/setup.c:
  9. * Copyright (C) 2002 Broadcom Corporation
  10. *
  11. * Derived from OpenWrt patches:
  12. * Copyright (C) 2013 Jonas Gorski <[email protected]>
  13. * Copyright (C) 2013 Florian Fainelli <[email protected]>
  14. * Copyright (C) 2008 Maxime Bizon <[email protected]>
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/io.h>
  18. #include <linux/module.h>
  19. #include <linux/phy/phy.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/reset.h>
  22. /* USBH control register offsets */
  23. enum usbh_regs {
  24. USBH_BRT_CONTROL1 = 0,
  25. USBH_BRT_CONTROL2,
  26. USBH_BRT_STATUS1,
  27. USBH_BRT_STATUS2,
  28. USBH_UTMI_CONTROL1,
  29. #define USBH_UC1_DEV_MODE_SEL BIT(0)
  30. USBH_TEST_PORT_CONTROL,
  31. USBH_PLL_CONTROL1,
  32. #define USBH_PLLC_REFCLKSEL_SHIFT 0
  33. #define USBH_PLLC_REFCLKSEL_MASK (0x3 << USBH_PLLC_REFCLKSEL_SHIFT)
  34. #define USBH_PLLC_CLKSEL_SHIFT 2
  35. #define USBH_PLLC_CLKSEL_MASK (0x3 << USBH_PLLC_CLKSEL_MASK)
  36. #define USBH_PLLC_XTAL_PWRDWNB BIT(4)
  37. #define USBH_PLLC_PLL_PWRDWNB BIT(5)
  38. #define USBH_PLLC_PLL_CALEN BIT(6)
  39. #define USBH_PLLC_PHYPLL_BYP BIT(7)
  40. #define USBH_PLLC_PLL_RESET BIT(8)
  41. #define USBH_PLLC_PLL_IDDQ_PWRDN BIT(9)
  42. #define USBH_PLLC_PLL_PWRDN_DELAY BIT(10)
  43. #define USBH_6318_PLLC_PLL_SUSPEND_EN BIT(27)
  44. #define USBH_6318_PLLC_PHYPLL_BYP BIT(29)
  45. #define USBH_6318_PLLC_PLL_RESET BIT(30)
  46. #define USBH_6318_PLLC_PLL_IDDQ_PWRDN BIT(31)
  47. USBH_SWAP_CONTROL,
  48. #define USBH_SC_OHCI_DATA_SWAP BIT(0)
  49. #define USBH_SC_OHCI_ENDIAN_SWAP BIT(1)
  50. #define USBH_SC_OHCI_LOGICAL_ADDR_EN BIT(2)
  51. #define USBH_SC_EHCI_DATA_SWAP BIT(3)
  52. #define USBH_SC_EHCI_ENDIAN_SWAP BIT(4)
  53. #define USBH_SC_EHCI_LOGICAL_ADDR_EN BIT(5)
  54. #define USBH_SC_USB_DEVICE_SEL BIT(6)
  55. USBH_GENERIC_CONTROL,
  56. #define USBH_GC_PLL_SUSPEND_EN BIT(1)
  57. USBH_FRAME_ADJUST_VALUE,
  58. USBH_SETUP,
  59. #define USBH_S_IOC BIT(4)
  60. #define USBH_S_IPP BIT(5)
  61. USBH_MDIO,
  62. USBH_MDIO32,
  63. USBH_USB_SIM_CONTROL,
  64. #define USBH_USC_LADDR_SEL BIT(5)
  65. __USBH_ENUM_SIZE
  66. };
  67. struct bcm63xx_usbh_phy_variant {
  68. /* Registers */
  69. long regs[__USBH_ENUM_SIZE];
  70. /* PLLC bits to set/clear for power on */
  71. u32 power_pllc_clr;
  72. u32 power_pllc_set;
  73. /* Setup bits to set/clear for power on */
  74. u32 setup_clr;
  75. u32 setup_set;
  76. /* Swap Control bits to set */
  77. u32 swapctl_dev_set;
  78. /* Test Port Control value to set if non-zero */
  79. u32 tpc_val;
  80. /* USB Sim Control bits to set */
  81. u32 usc_set;
  82. /* UTMI Control 1 bits to set */
  83. u32 utmictl1_dev_set;
  84. };
  85. struct bcm63xx_usbh_phy {
  86. void __iomem *base;
  87. struct clk *usbh_clk;
  88. struct clk *usb_ref_clk;
  89. struct reset_control *reset;
  90. const struct bcm63xx_usbh_phy_variant *variant;
  91. bool device_mode;
  92. };
  93. static const struct bcm63xx_usbh_phy_variant usbh_bcm6318 = {
  94. .regs = {
  95. [USBH_BRT_CONTROL1] = -1,
  96. [USBH_BRT_CONTROL2] = -1,
  97. [USBH_BRT_STATUS1] = -1,
  98. [USBH_BRT_STATUS2] = -1,
  99. [USBH_UTMI_CONTROL1] = 0x2c,
  100. [USBH_TEST_PORT_CONTROL] = 0x1c,
  101. [USBH_PLL_CONTROL1] = 0x04,
  102. [USBH_SWAP_CONTROL] = 0x0c,
  103. [USBH_GENERIC_CONTROL] = -1,
  104. [USBH_FRAME_ADJUST_VALUE] = 0x08,
  105. [USBH_SETUP] = 0x00,
  106. [USBH_MDIO] = 0x14,
  107. [USBH_MDIO32] = 0x18,
  108. [USBH_USB_SIM_CONTROL] = 0x20,
  109. },
  110. .power_pllc_clr = USBH_6318_PLLC_PLL_IDDQ_PWRDN,
  111. .power_pllc_set = USBH_6318_PLLC_PLL_SUSPEND_EN,
  112. .setup_set = USBH_S_IOC,
  113. .swapctl_dev_set = USBH_SC_USB_DEVICE_SEL,
  114. .usc_set = USBH_USC_LADDR_SEL,
  115. .utmictl1_dev_set = USBH_UC1_DEV_MODE_SEL,
  116. };
  117. static const struct bcm63xx_usbh_phy_variant usbh_bcm6328 = {
  118. .regs = {
  119. [USBH_BRT_CONTROL1] = 0x00,
  120. [USBH_BRT_CONTROL2] = 0x04,
  121. [USBH_BRT_STATUS1] = 0x08,
  122. [USBH_BRT_STATUS2] = 0x0c,
  123. [USBH_UTMI_CONTROL1] = 0x10,
  124. [USBH_TEST_PORT_CONTROL] = 0x14,
  125. [USBH_PLL_CONTROL1] = 0x18,
  126. [USBH_SWAP_CONTROL] = 0x1c,
  127. [USBH_GENERIC_CONTROL] = 0x20,
  128. [USBH_FRAME_ADJUST_VALUE] = 0x24,
  129. [USBH_SETUP] = 0x28,
  130. [USBH_MDIO] = 0x2c,
  131. [USBH_MDIO32] = 0x30,
  132. [USBH_USB_SIM_CONTROL] = 0x34,
  133. },
  134. .setup_set = USBH_S_IOC,
  135. .swapctl_dev_set = USBH_SC_USB_DEVICE_SEL,
  136. .utmictl1_dev_set = USBH_UC1_DEV_MODE_SEL,
  137. };
  138. static const struct bcm63xx_usbh_phy_variant usbh_bcm6358 = {
  139. .regs = {
  140. [USBH_BRT_CONTROL1] = -1,
  141. [USBH_BRT_CONTROL2] = -1,
  142. [USBH_BRT_STATUS1] = -1,
  143. [USBH_BRT_STATUS2] = -1,
  144. [USBH_UTMI_CONTROL1] = -1,
  145. [USBH_TEST_PORT_CONTROL] = 0x24,
  146. [USBH_PLL_CONTROL1] = -1,
  147. [USBH_SWAP_CONTROL] = 0x00,
  148. [USBH_GENERIC_CONTROL] = -1,
  149. [USBH_FRAME_ADJUST_VALUE] = -1,
  150. [USBH_SETUP] = -1,
  151. [USBH_MDIO] = -1,
  152. [USBH_MDIO32] = -1,
  153. [USBH_USB_SIM_CONTROL] = -1,
  154. },
  155. /*
  156. * The magic value comes for the original vendor BSP
  157. * and is needed for USB to work. Datasheet does not
  158. * help, so the magic value is used as-is.
  159. */
  160. .tpc_val = 0x1c0020,
  161. };
  162. static const struct bcm63xx_usbh_phy_variant usbh_bcm6368 = {
  163. .regs = {
  164. [USBH_BRT_CONTROL1] = 0x00,
  165. [USBH_BRT_CONTROL2] = 0x04,
  166. [USBH_BRT_STATUS1] = 0x08,
  167. [USBH_BRT_STATUS2] = 0x0c,
  168. [USBH_UTMI_CONTROL1] = 0x10,
  169. [USBH_TEST_PORT_CONTROL] = 0x14,
  170. [USBH_PLL_CONTROL1] = 0x18,
  171. [USBH_SWAP_CONTROL] = 0x1c,
  172. [USBH_GENERIC_CONTROL] = -1,
  173. [USBH_FRAME_ADJUST_VALUE] = 0x24,
  174. [USBH_SETUP] = 0x28,
  175. [USBH_MDIO] = 0x2c,
  176. [USBH_MDIO32] = 0x30,
  177. [USBH_USB_SIM_CONTROL] = 0x34,
  178. },
  179. .power_pllc_clr = USBH_PLLC_PLL_IDDQ_PWRDN | USBH_PLLC_PLL_PWRDN_DELAY,
  180. .setup_set = USBH_S_IOC,
  181. .swapctl_dev_set = USBH_SC_USB_DEVICE_SEL,
  182. .utmictl1_dev_set = USBH_UC1_DEV_MODE_SEL,
  183. };
  184. static const struct bcm63xx_usbh_phy_variant usbh_bcm63268 = {
  185. .regs = {
  186. [USBH_BRT_CONTROL1] = 0x00,
  187. [USBH_BRT_CONTROL2] = 0x04,
  188. [USBH_BRT_STATUS1] = 0x08,
  189. [USBH_BRT_STATUS2] = 0x0c,
  190. [USBH_UTMI_CONTROL1] = 0x10,
  191. [USBH_TEST_PORT_CONTROL] = 0x14,
  192. [USBH_PLL_CONTROL1] = 0x18,
  193. [USBH_SWAP_CONTROL] = 0x1c,
  194. [USBH_GENERIC_CONTROL] = 0x20,
  195. [USBH_FRAME_ADJUST_VALUE] = 0x24,
  196. [USBH_SETUP] = 0x28,
  197. [USBH_MDIO] = 0x2c,
  198. [USBH_MDIO32] = 0x30,
  199. [USBH_USB_SIM_CONTROL] = 0x34,
  200. },
  201. .power_pllc_clr = USBH_PLLC_PLL_IDDQ_PWRDN | USBH_PLLC_PLL_PWRDN_DELAY,
  202. .setup_clr = USBH_S_IPP,
  203. .setup_set = USBH_S_IOC,
  204. .swapctl_dev_set = USBH_SC_USB_DEVICE_SEL,
  205. .utmictl1_dev_set = USBH_UC1_DEV_MODE_SEL,
  206. };
  207. static inline bool usbh_has_reg(struct bcm63xx_usbh_phy *usbh, int reg)
  208. {
  209. return (usbh->variant->regs[reg] >= 0);
  210. }
  211. static inline u32 usbh_readl(struct bcm63xx_usbh_phy *usbh, int reg)
  212. {
  213. return __raw_readl(usbh->base + usbh->variant->regs[reg]);
  214. }
  215. static inline void usbh_writel(struct bcm63xx_usbh_phy *usbh, int reg,
  216. u32 value)
  217. {
  218. __raw_writel(value, usbh->base + usbh->variant->regs[reg]);
  219. }
  220. static int bcm63xx_usbh_phy_init(struct phy *phy)
  221. {
  222. struct bcm63xx_usbh_phy *usbh = phy_get_drvdata(phy);
  223. int ret;
  224. ret = clk_prepare_enable(usbh->usbh_clk);
  225. if (ret) {
  226. dev_err(&phy->dev, "unable to enable usbh clock: %d\n", ret);
  227. return ret;
  228. }
  229. ret = clk_prepare_enable(usbh->usb_ref_clk);
  230. if (ret) {
  231. dev_err(&phy->dev, "unable to enable usb_ref clock: %d\n", ret);
  232. clk_disable_unprepare(usbh->usbh_clk);
  233. return ret;
  234. }
  235. ret = reset_control_reset(usbh->reset);
  236. if (ret) {
  237. dev_err(&phy->dev, "unable to reset device: %d\n", ret);
  238. clk_disable_unprepare(usbh->usb_ref_clk);
  239. clk_disable_unprepare(usbh->usbh_clk);
  240. return ret;
  241. }
  242. /* Configure to work in native CPU endian */
  243. if (usbh_has_reg(usbh, USBH_SWAP_CONTROL)) {
  244. u32 val = usbh_readl(usbh, USBH_SWAP_CONTROL);
  245. val |= USBH_SC_EHCI_DATA_SWAP;
  246. val &= ~USBH_SC_EHCI_ENDIAN_SWAP;
  247. val |= USBH_SC_OHCI_DATA_SWAP;
  248. val &= ~USBH_SC_OHCI_ENDIAN_SWAP;
  249. if (usbh->device_mode && usbh->variant->swapctl_dev_set)
  250. val |= usbh->variant->swapctl_dev_set;
  251. usbh_writel(usbh, USBH_SWAP_CONTROL, val);
  252. }
  253. if (usbh_has_reg(usbh, USBH_SETUP)) {
  254. u32 val = usbh_readl(usbh, USBH_SETUP);
  255. val |= usbh->variant->setup_set;
  256. val &= ~usbh->variant->setup_clr;
  257. usbh_writel(usbh, USBH_SETUP, val);
  258. }
  259. if (usbh_has_reg(usbh, USBH_USB_SIM_CONTROL)) {
  260. u32 val = usbh_readl(usbh, USBH_USB_SIM_CONTROL);
  261. val |= usbh->variant->usc_set;
  262. usbh_writel(usbh, USBH_USB_SIM_CONTROL, val);
  263. }
  264. if (usbh->variant->tpc_val &&
  265. usbh_has_reg(usbh, USBH_TEST_PORT_CONTROL))
  266. usbh_writel(usbh, USBH_TEST_PORT_CONTROL,
  267. usbh->variant->tpc_val);
  268. if (usbh->device_mode &&
  269. usbh_has_reg(usbh, USBH_UTMI_CONTROL1) &&
  270. usbh->variant->utmictl1_dev_set) {
  271. u32 val = usbh_readl(usbh, USBH_UTMI_CONTROL1);
  272. val |= usbh->variant->utmictl1_dev_set;
  273. usbh_writel(usbh, USBH_UTMI_CONTROL1, val);
  274. }
  275. return 0;
  276. }
  277. static int bcm63xx_usbh_phy_power_on(struct phy *phy)
  278. {
  279. struct bcm63xx_usbh_phy *usbh = phy_get_drvdata(phy);
  280. if (usbh_has_reg(usbh, USBH_PLL_CONTROL1)) {
  281. u32 val = usbh_readl(usbh, USBH_PLL_CONTROL1);
  282. val |= usbh->variant->power_pllc_set;
  283. val &= ~usbh->variant->power_pllc_clr;
  284. usbh_writel(usbh, USBH_PLL_CONTROL1, val);
  285. }
  286. return 0;
  287. }
  288. static int bcm63xx_usbh_phy_power_off(struct phy *phy)
  289. {
  290. struct bcm63xx_usbh_phy *usbh = phy_get_drvdata(phy);
  291. if (usbh_has_reg(usbh, USBH_PLL_CONTROL1)) {
  292. u32 val = usbh_readl(usbh, USBH_PLL_CONTROL1);
  293. val &= ~usbh->variant->power_pllc_set;
  294. val |= usbh->variant->power_pllc_clr;
  295. usbh_writel(usbh, USBH_PLL_CONTROL1, val);
  296. }
  297. return 0;
  298. }
  299. static int bcm63xx_usbh_phy_exit(struct phy *phy)
  300. {
  301. struct bcm63xx_usbh_phy *usbh = phy_get_drvdata(phy);
  302. clk_disable_unprepare(usbh->usbh_clk);
  303. clk_disable_unprepare(usbh->usb_ref_clk);
  304. return 0;
  305. }
  306. static const struct phy_ops bcm63xx_usbh_phy_ops = {
  307. .exit = bcm63xx_usbh_phy_exit,
  308. .init = bcm63xx_usbh_phy_init,
  309. .power_off = bcm63xx_usbh_phy_power_off,
  310. .power_on = bcm63xx_usbh_phy_power_on,
  311. .owner = THIS_MODULE,
  312. };
  313. static struct phy *bcm63xx_usbh_phy_xlate(struct device *dev,
  314. struct of_phandle_args *args)
  315. {
  316. struct bcm63xx_usbh_phy *usbh = dev_get_drvdata(dev);
  317. usbh->device_mode = !!args->args[0];
  318. return of_phy_simple_xlate(dev, args);
  319. }
  320. static int __init bcm63xx_usbh_phy_probe(struct platform_device *pdev)
  321. {
  322. struct device *dev = &pdev->dev;
  323. struct bcm63xx_usbh_phy *usbh;
  324. const struct bcm63xx_usbh_phy_variant *variant;
  325. struct phy *phy;
  326. struct phy_provider *phy_provider;
  327. usbh = devm_kzalloc(dev, sizeof(*usbh), GFP_KERNEL);
  328. if (!usbh)
  329. return -ENOMEM;
  330. variant = device_get_match_data(dev);
  331. if (!variant)
  332. return -EINVAL;
  333. usbh->variant = variant;
  334. usbh->base = devm_platform_ioremap_resource(pdev, 0);
  335. if (IS_ERR(usbh->base))
  336. return PTR_ERR(usbh->base);
  337. usbh->reset = devm_reset_control_get_exclusive(dev, NULL);
  338. if (IS_ERR(usbh->reset)) {
  339. if (PTR_ERR(usbh->reset) != -EPROBE_DEFER)
  340. dev_err(dev, "failed to get reset\n");
  341. return PTR_ERR(usbh->reset);
  342. }
  343. usbh->usbh_clk = devm_clk_get_optional(dev, "usbh");
  344. if (IS_ERR(usbh->usbh_clk))
  345. return PTR_ERR(usbh->usbh_clk);
  346. usbh->usb_ref_clk = devm_clk_get_optional(dev, "usb_ref");
  347. if (IS_ERR(usbh->usb_ref_clk))
  348. return PTR_ERR(usbh->usb_ref_clk);
  349. phy = devm_phy_create(dev, NULL, &bcm63xx_usbh_phy_ops);
  350. if (IS_ERR(phy)) {
  351. dev_err(dev, "failed to create PHY\n");
  352. return PTR_ERR(phy);
  353. }
  354. platform_set_drvdata(pdev, usbh);
  355. phy_set_drvdata(phy, usbh);
  356. phy_provider = devm_of_phy_provider_register(dev,
  357. bcm63xx_usbh_phy_xlate);
  358. if (IS_ERR(phy_provider)) {
  359. dev_err(dev, "failed to register PHY provider\n");
  360. return PTR_ERR(phy_provider);
  361. }
  362. dev_dbg(dev, "Registered BCM63xx USB PHY driver\n");
  363. return 0;
  364. }
  365. static const struct of_device_id bcm63xx_usbh_phy_ids[] __initconst = {
  366. { .compatible = "brcm,bcm6318-usbh-phy", .data = &usbh_bcm6318 },
  367. { .compatible = "brcm,bcm6328-usbh-phy", .data = &usbh_bcm6328 },
  368. { .compatible = "brcm,bcm6358-usbh-phy", .data = &usbh_bcm6358 },
  369. { .compatible = "brcm,bcm6362-usbh-phy", .data = &usbh_bcm6368 },
  370. { .compatible = "brcm,bcm6368-usbh-phy", .data = &usbh_bcm6368 },
  371. { .compatible = "brcm,bcm63268-usbh-phy", .data = &usbh_bcm63268 },
  372. { /* sentinel */ }
  373. };
  374. MODULE_DEVICE_TABLE(of, bcm63xx_usbh_phy_ids);
  375. static struct platform_driver bcm63xx_usbh_phy_driver __refdata = {
  376. .driver = {
  377. .name = "bcm63xx-usbh-phy",
  378. .of_match_table = bcm63xx_usbh_phy_ids,
  379. },
  380. .probe = bcm63xx_usbh_phy_probe,
  381. };
  382. module_platform_driver(bcm63xx_usbh_phy_driver);
  383. MODULE_DESCRIPTION("BCM63xx USBH PHY driver");
  384. MODULE_AUTHOR("Álvaro Fernández Rojas <[email protected]>");
  385. MODULE_AUTHOR("Simon Arlott");
  386. MODULE_LICENSE("GPL");