phy-bcm63xx-usbh.c 12 KB

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