071-phy-bcm-ns-usb3-new-driver-for-USB-3.0-PHY-on-Norths.patch 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. From b8d7478ecfec51b3430f677da44e662d5ff12444 Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
  3. Date: Fri, 12 Aug 2016 00:28:03 +0200
  4. Subject: [PATCH] phy: bcm-ns-usb3: new driver for USB 3.0 PHY on Northstar
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. Northstar is a family of SoCs used in home routers. They have USB 2.0
  9. and 3.0 controllers with PHYs that need to be properly initialized.
  10. This driver provides PHY init support in a generic way and can be bound
  11. with XHCI controller driver.
  12. There aren't any public datasheets from Broadcom so we can't have nice
  13. defines for all used bits. It means we just follow Broadcom's
  14. initialization procedure using their magic values. We were quite lucky
  15. actually that Broadcom put some comments in their SDK reference code
  16. explaining what given writes are responsible for.
  17. Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
  18. Signed-off-by: Kishon Vijay Abraham I <kishon@ti.com>
  19. ---
  20. .../devicetree/bindings/phy/bcm-ns-usb3-phy.txt | 23 ++
  21. drivers/phy/Kconfig | 9 +
  22. drivers/phy/Makefile | 1 +
  23. drivers/phy/phy-bcm-ns-usb3.c | 274 +++++++++++++++++++++
  24. 4 files changed, 307 insertions(+)
  25. create mode 100644 Documentation/devicetree/bindings/phy/bcm-ns-usb3-phy.txt
  26. create mode 100644 drivers/phy/phy-bcm-ns-usb3.c
  27. --- /dev/null
  28. +++ b/Documentation/devicetree/bindings/phy/bcm-ns-usb3-phy.txt
  29. @@ -0,0 +1,23 @@
  30. +Driver for Broadcom Northstar USB 3.0 PHY
  31. +
  32. +Required properties:
  33. +
  34. +- compatible: one of: "brcm,ns-ax-usb3-phy", "brcm,ns-bx-usb3-phy".
  35. +- reg: register mappings for DMP (Device Management Plugin) and ChipCommon B
  36. + MMI.
  37. +- reg-names: "dmp" and "ccb-mii"
  38. +
  39. +Initialization of USB 3.0 PHY depends on Northstar version. There are currently
  40. +three known series: Ax, Bx and Cx.
  41. +Known A0: BCM4707 rev 0
  42. +Known B0: BCM4707 rev 4, BCM53573 rev 2
  43. +Known B1: BCM4707 rev 6
  44. +Known C0: BCM47094 rev 0
  45. +
  46. +Example:
  47. + usb3-phy {
  48. + compatible = "brcm,ns-ax-usb3-phy";
  49. + reg = <0x18105000 0x1000>, <0x18003000 0x1000>;
  50. + reg-names = "dmp", "ccb-mii";
  51. + #phy-cells = <0>;
  52. + };
  53. --- a/drivers/phy/Kconfig
  54. +++ b/drivers/phy/Kconfig
  55. @@ -24,6 +24,15 @@ config PHY_BCM_NS_USB2
  56. Enable this to support Broadcom USB 2.0 PHY connected to the USB
  57. controller on Northstar family.
  58. +config PHY_BCM_NS_USB3
  59. + tristate "Broadcom Northstar USB 3.0 PHY Driver"
  60. + depends on ARCH_BCM_IPROC || COMPILE_TEST
  61. + depends on HAS_IOMEM && OF
  62. + select GENERIC_PHY
  63. + help
  64. + Enable this to support Broadcom USB 3.0 PHY connected to the USB
  65. + controller on Northstar family.
  66. +
  67. config PHY_BERLIN_USB
  68. tristate "Marvell Berlin USB PHY Driver"
  69. depends on ARCH_BERLIN && RESET_CONTROLLER && HAS_IOMEM && OF
  70. --- a/drivers/phy/Makefile
  71. +++ b/drivers/phy/Makefile
  72. @@ -4,6 +4,7 @@
  73. obj-$(CONFIG_GENERIC_PHY) += phy-core.o
  74. obj-$(CONFIG_PHY_BCM_NS_USB2) += phy-bcm-ns-usb2.o
  75. +obj-$(CONFIG_PHY_BCM_NS_USB3) += phy-bcm-ns-usb3.o
  76. obj-$(CONFIG_PHY_BERLIN_USB) += phy-berlin-usb.o
  77. obj-$(CONFIG_PHY_BERLIN_SATA) += phy-berlin-sata.o
  78. obj-$(CONFIG_PHY_DM816X_USB) += phy-dm816x-usb.o
  79. --- /dev/null
  80. +++ b/drivers/phy/phy-bcm-ns-usb3.c
  81. @@ -0,0 +1,274 @@
  82. +/*
  83. + * Broadcom Northstar USB 3.0 PHY Driver
  84. + *
  85. + * Copyright (C) 2016 Rafał Miłecki <rafal@milecki.pl>
  86. + *
  87. + * All magic values used for initialization (and related comments) were obtained
  88. + * from Broadcom's SDK:
  89. + * Copyright (c) Broadcom Corp, 2012
  90. + *
  91. + * This program is free software; you can redistribute it and/or modify
  92. + * it under the terms of the GNU General Public License version 2 as
  93. + * published by the Free Software Foundation.
  94. + */
  95. +
  96. +#include <linux/bcma/bcma.h>
  97. +#include <linux/delay.h>
  98. +#include <linux/err.h>
  99. +#include <linux/module.h>
  100. +#include <linux/of_platform.h>
  101. +#include <linux/platform_device.h>
  102. +#include <linux/phy/phy.h>
  103. +#include <linux/slab.h>
  104. +
  105. +#define BCM_NS_USB3_MII_MNG_TIMEOUT_US 1000 /* usecs */
  106. +
  107. +enum bcm_ns_family {
  108. + BCM_NS_UNKNOWN,
  109. + BCM_NS_AX,
  110. + BCM_NS_BX,
  111. +};
  112. +
  113. +struct bcm_ns_usb3 {
  114. + struct device *dev;
  115. + enum bcm_ns_family family;
  116. + void __iomem *dmp;
  117. + void __iomem *ccb_mii;
  118. + struct phy *phy;
  119. +};
  120. +
  121. +static const struct of_device_id bcm_ns_usb3_id_table[] = {
  122. + {
  123. + .compatible = "brcm,ns-ax-usb3-phy",
  124. + .data = (int *)BCM_NS_AX,
  125. + },
  126. + {
  127. + .compatible = "brcm,ns-bx-usb3-phy",
  128. + .data = (int *)BCM_NS_BX,
  129. + },
  130. + {},
  131. +};
  132. +MODULE_DEVICE_TABLE(of, bcm_ns_usb3_id_table);
  133. +
  134. +static int bcm_ns_usb3_wait_reg(struct bcm_ns_usb3 *usb3, void __iomem *addr,
  135. + u32 mask, u32 value, unsigned long timeout)
  136. +{
  137. + unsigned long deadline = jiffies + timeout;
  138. + u32 val;
  139. +
  140. + do {
  141. + val = readl(addr);
  142. + if ((val & mask) == value)
  143. + return 0;
  144. + cpu_relax();
  145. + udelay(10);
  146. + } while (!time_after_eq(jiffies, deadline));
  147. +
  148. + dev_err(usb3->dev, "Timeout waiting for register %p\n", addr);
  149. +
  150. + return -EBUSY;
  151. +}
  152. +
  153. +static inline int bcm_ns_usb3_mii_mng_wait_idle(struct bcm_ns_usb3 *usb3)
  154. +{
  155. + return bcm_ns_usb3_wait_reg(usb3, usb3->ccb_mii + BCMA_CCB_MII_MNG_CTL,
  156. + 0x0100, 0x0000,
  157. + usecs_to_jiffies(BCM_NS_USB3_MII_MNG_TIMEOUT_US));
  158. +}
  159. +
  160. +static int bcm_ns_usb3_mii_mng_write32(struct bcm_ns_usb3 *usb3, u32 value)
  161. +{
  162. + int err;
  163. +
  164. + err = bcm_ns_usb3_mii_mng_wait_idle(usb3);
  165. + if (err < 0) {
  166. + dev_err(usb3->dev, "Couldn't write 0x%08x value\n", value);
  167. + return err;
  168. + }
  169. +
  170. + writel(value, usb3->ccb_mii + BCMA_CCB_MII_MNG_CMD_DATA);
  171. +
  172. + return 0;
  173. +}
  174. +
  175. +static int bcm_ns_usb3_phy_init_ns_bx(struct bcm_ns_usb3 *usb3)
  176. +{
  177. + int err;
  178. +
  179. + /* Enable MDIO. Setting MDCDIV as 26 */
  180. + writel(0x0000009a, usb3->ccb_mii + BCMA_CCB_MII_MNG_CTL);
  181. +
  182. + /* Wait for MDIO? */
  183. + udelay(2);
  184. +
  185. + /* USB3 PLL Block */
  186. + err = bcm_ns_usb3_mii_mng_write32(usb3, 0x587e8000);
  187. + if (err < 0)
  188. + return err;
  189. +
  190. + /* Assert Ana_Pllseq start */
  191. + bcm_ns_usb3_mii_mng_write32(usb3, 0x58061000);
  192. +
  193. + /* Assert CML Divider ratio to 26 */
  194. + bcm_ns_usb3_mii_mng_write32(usb3, 0x582a6400);
  195. +
  196. + /* Asserting PLL Reset */
  197. + bcm_ns_usb3_mii_mng_write32(usb3, 0x582ec000);
  198. +
  199. + /* Deaaserting PLL Reset */
  200. + bcm_ns_usb3_mii_mng_write32(usb3, 0x582e8000);
  201. +
  202. + /* Waiting MII Mgt interface idle */
  203. + bcm_ns_usb3_mii_mng_wait_idle(usb3);
  204. +
  205. + /* Deasserting USB3 system reset */
  206. + writel(0, usb3->dmp + BCMA_RESET_CTL);
  207. +
  208. + /* PLL frequency monitor enable */
  209. + bcm_ns_usb3_mii_mng_write32(usb3, 0x58069000);
  210. +
  211. + /* PIPE Block */
  212. + bcm_ns_usb3_mii_mng_write32(usb3, 0x587e8060);
  213. +
  214. + /* CMPMAX & CMPMINTH setting */
  215. + bcm_ns_usb3_mii_mng_write32(usb3, 0x580af30d);
  216. +
  217. + /* DEGLITCH MIN & MAX setting */
  218. + bcm_ns_usb3_mii_mng_write32(usb3, 0x580e6302);
  219. +
  220. + /* TXPMD block */
  221. + bcm_ns_usb3_mii_mng_write32(usb3, 0x587e8040);
  222. +
  223. + /* Enabling SSC */
  224. + bcm_ns_usb3_mii_mng_write32(usb3, 0x58061003);
  225. +
  226. + /* Waiting MII Mgt interface idle */
  227. + bcm_ns_usb3_mii_mng_wait_idle(usb3);
  228. +
  229. + return 0;
  230. +}
  231. +
  232. +static int bcm_ns_usb3_phy_init_ns_ax(struct bcm_ns_usb3 *usb3)
  233. +{
  234. + int err;
  235. +
  236. + /* Enable MDIO. Setting MDCDIV as 26 */
  237. + writel(0x0000009a, usb3->ccb_mii + BCMA_CCB_MII_MNG_CTL);
  238. +
  239. + /* Wait for MDIO? */
  240. + udelay(2);
  241. +
  242. + /* PLL30 block */
  243. + err = bcm_ns_usb3_mii_mng_write32(usb3, 0x587e8000);
  244. + if (err < 0)
  245. + return err;
  246. +
  247. + bcm_ns_usb3_mii_mng_write32(usb3, 0x582a6400);
  248. +
  249. + bcm_ns_usb3_mii_mng_write32(usb3, 0x587e80e0);
  250. +
  251. + bcm_ns_usb3_mii_mng_write32(usb3, 0x580a009c);
  252. +
  253. + /* Enable SSC */
  254. + bcm_ns_usb3_mii_mng_write32(usb3, 0x587e8040);
  255. +
  256. + bcm_ns_usb3_mii_mng_write32(usb3, 0x580a21d3);
  257. +
  258. + bcm_ns_usb3_mii_mng_write32(usb3, 0x58061003);
  259. +
  260. + /* Waiting MII Mgt interface idle */
  261. + bcm_ns_usb3_mii_mng_wait_idle(usb3);
  262. +
  263. + /* Deasserting USB3 system reset */
  264. + writel(0, usb3->dmp + BCMA_RESET_CTL);
  265. +
  266. + return 0;
  267. +}
  268. +
  269. +static int bcm_ns_usb3_phy_init(struct phy *phy)
  270. +{
  271. + struct bcm_ns_usb3 *usb3 = phy_get_drvdata(phy);
  272. + int err;
  273. +
  274. + /* Perform USB3 system soft reset */
  275. + writel(BCMA_RESET_CTL_RESET, usb3->dmp + BCMA_RESET_CTL);
  276. +
  277. + switch (usb3->family) {
  278. + case BCM_NS_AX:
  279. + err = bcm_ns_usb3_phy_init_ns_ax(usb3);
  280. + break;
  281. + case BCM_NS_BX:
  282. + err = bcm_ns_usb3_phy_init_ns_bx(usb3);
  283. + break;
  284. + default:
  285. + WARN_ON(1);
  286. + err = -ENOTSUPP;
  287. + }
  288. +
  289. + return err;
  290. +}
  291. +
  292. +static const struct phy_ops ops = {
  293. + .init = bcm_ns_usb3_phy_init,
  294. + .owner = THIS_MODULE,
  295. +};
  296. +
  297. +static int bcm_ns_usb3_probe(struct platform_device *pdev)
  298. +{
  299. + struct device *dev = &pdev->dev;
  300. + const struct of_device_id *of_id;
  301. + struct bcm_ns_usb3 *usb3;
  302. + struct resource *res;
  303. + struct phy_provider *phy_provider;
  304. +
  305. + usb3 = devm_kzalloc(dev, sizeof(*usb3), GFP_KERNEL);
  306. + if (!usb3)
  307. + return -ENOMEM;
  308. +
  309. + usb3->dev = dev;
  310. +
  311. + of_id = of_match_device(bcm_ns_usb3_id_table, dev);
  312. + if (!of_id)
  313. + return -EINVAL;
  314. + usb3->family = (enum bcm_ns_family)of_id->data;
  315. +
  316. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dmp");
  317. + usb3->dmp = devm_ioremap_resource(dev, res);
  318. + if (IS_ERR(usb3->dmp)) {
  319. + dev_err(dev, "Failed to map DMP regs\n");
  320. + return PTR_ERR(usb3->dmp);
  321. + }
  322. +
  323. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ccb-mii");
  324. + usb3->ccb_mii = devm_ioremap_resource(dev, res);
  325. + if (IS_ERR(usb3->ccb_mii)) {
  326. + dev_err(dev, "Failed to map ChipCommon B MII regs\n");
  327. + return PTR_ERR(usb3->ccb_mii);
  328. + }
  329. +
  330. + usb3->phy = devm_phy_create(dev, NULL, &ops);
  331. + if (IS_ERR(usb3->phy)) {
  332. + dev_err(dev, "Failed to create PHY\n");
  333. + return PTR_ERR(usb3->phy);
  334. + }
  335. +
  336. + phy_set_drvdata(usb3->phy, usb3);
  337. + platform_set_drvdata(pdev, usb3);
  338. +
  339. + phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  340. + if (!IS_ERR(phy_provider))
  341. + dev_info(dev, "Registered Broadcom Northstar USB 3.0 PHY driver\n");
  342. +
  343. + return PTR_ERR_OR_ZERO(phy_provider);
  344. +}
  345. +
  346. +static struct platform_driver bcm_ns_usb3_driver = {
  347. + .probe = bcm_ns_usb3_probe,
  348. + .driver = {
  349. + .name = "bcm_ns_usb3",
  350. + .of_match_table = bcm_ns_usb3_id_table,
  351. + },
  352. +};
  353. +module_platform_driver(bcm_ns_usb3_driver);
  354. +
  355. +MODULE_LICENSE("GPL v2");