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
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 Northstar is a family of SoCs used in home routers. They have USB 2.0
10 and 3.0 controllers with PHYs that need to be properly initialized.
11 This driver provides PHY init support in a generic way and can be bound
12 with XHCI controller driver.
14 There aren't any public datasheets from Broadcom so we can't have nice
15 defines for all used bits. It means we just follow Broadcom's
16 initialization procedure using their magic values. We were quite lucky
17 actually that Broadcom put some comments in their SDK reference code
18 explaining what given writes are responsible for.
20 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
21 Signed-off-by: Kishon Vijay Abraham I <kishon@ti.com>
23 .../devicetree/bindings/phy/bcm-ns-usb3-phy.txt | 23 ++
24 drivers/phy/Kconfig | 9 +
25 drivers/phy/Makefile | 1 +
26 drivers/phy/phy-bcm-ns-usb3.c | 274 +++++++++++++++++++++
27 4 files changed, 307 insertions(+)
28 create mode 100644 Documentation/devicetree/bindings/phy/bcm-ns-usb3-phy.txt
29 create mode 100644 drivers/phy/phy-bcm-ns-usb3.c
32 +++ b/Documentation/devicetree/bindings/phy/bcm-ns-usb3-phy.txt
34 +Driver for Broadcom Northstar USB 3.0 PHY
38 +- compatible: one of: "brcm,ns-ax-usb3-phy", "brcm,ns-bx-usb3-phy".
39 +- reg: register mappings for DMP (Device Management Plugin) and ChipCommon B
41 +- reg-names: "dmp" and "ccb-mii"
43 +Initialization of USB 3.0 PHY depends on Northstar version. There are currently
44 +three known series: Ax, Bx and Cx.
45 +Known A0: BCM4707 rev 0
46 +Known B0: BCM4707 rev 4, BCM53573 rev 2
47 +Known B1: BCM4707 rev 6
48 +Known C0: BCM47094 rev 0
52 + compatible = "brcm,ns-ax-usb3-phy";
53 + reg = <0x18105000 0x1000>, <0x18003000 0x1000>;
54 + reg-names = "dmp", "ccb-mii";
57 --- a/drivers/phy/Kconfig
58 +++ b/drivers/phy/Kconfig
59 @@ -24,6 +24,15 @@ config PHY_BCM_NS_USB2
60 Enable this to support Broadcom USB 2.0 PHY connected to the USB
61 controller on Northstar family.
63 +config PHY_BCM_NS_USB3
64 + tristate "Broadcom Northstar USB 3.0 PHY Driver"
65 + depends on ARCH_BCM_IPROC || COMPILE_TEST
66 + depends on HAS_IOMEM && OF
69 + Enable this to support Broadcom USB 3.0 PHY connected to the USB
70 + controller on Northstar family.
73 tristate "Marvell Berlin USB PHY Driver"
74 depends on ARCH_BERLIN && RESET_CONTROLLER && HAS_IOMEM && OF
75 --- a/drivers/phy/Makefile
76 +++ b/drivers/phy/Makefile
79 obj-$(CONFIG_GENERIC_PHY) += phy-core.o
80 obj-$(CONFIG_PHY_BCM_NS_USB2) += phy-bcm-ns-usb2.o
81 +obj-$(CONFIG_PHY_BCM_NS_USB3) += phy-bcm-ns-usb3.o
82 obj-$(CONFIG_PHY_BERLIN_USB) += phy-berlin-usb.o
83 obj-$(CONFIG_PHY_BERLIN_SATA) += phy-berlin-sata.o
84 obj-$(CONFIG_PHY_DM816X_USB) += phy-dm816x-usb.o
86 +++ b/drivers/phy/phy-bcm-ns-usb3.c
89 + * Broadcom Northstar USB 3.0 PHY Driver
91 + * Copyright (C) 2016 Rafał Miłecki <rafal@milecki.pl>
93 + * All magic values used for initialization (and related comments) were obtained
94 + * from Broadcom's SDK:
95 + * Copyright (c) Broadcom Corp, 2012
97 + * This program is free software; you can redistribute it and/or modify
98 + * it under the terms of the GNU General Public License version 2 as
99 + * published by the Free Software Foundation.
102 +#include <linux/bcma/bcma.h>
103 +#include <linux/delay.h>
104 +#include <linux/err.h>
105 +#include <linux/module.h>
106 +#include <linux/of_platform.h>
107 +#include <linux/platform_device.h>
108 +#include <linux/phy/phy.h>
109 +#include <linux/slab.h>
111 +#define BCM_NS_USB3_MII_MNG_TIMEOUT_US 1000 /* usecs */
113 +enum bcm_ns_family {
119 +struct bcm_ns_usb3 {
120 + struct device *dev;
121 + enum bcm_ns_family family;
123 + void __iomem *ccb_mii;
127 +static const struct of_device_id bcm_ns_usb3_id_table[] = {
129 + .compatible = "brcm,ns-ax-usb3-phy",
130 + .data = (int *)BCM_NS_AX,
133 + .compatible = "brcm,ns-bx-usb3-phy",
134 + .data = (int *)BCM_NS_BX,
138 +MODULE_DEVICE_TABLE(of, bcm_ns_usb3_id_table);
140 +static int bcm_ns_usb3_wait_reg(struct bcm_ns_usb3 *usb3, void __iomem *addr,
141 + u32 mask, u32 value, unsigned long timeout)
143 + unsigned long deadline = jiffies + timeout;
148 + if ((val & mask) == value)
152 + } while (!time_after_eq(jiffies, deadline));
154 + dev_err(usb3->dev, "Timeout waiting for register %p\n", addr);
159 +static inline int bcm_ns_usb3_mii_mng_wait_idle(struct bcm_ns_usb3 *usb3)
161 + return bcm_ns_usb3_wait_reg(usb3, usb3->ccb_mii + BCMA_CCB_MII_MNG_CTL,
163 + usecs_to_jiffies(BCM_NS_USB3_MII_MNG_TIMEOUT_US));
166 +static int bcm_ns_usb3_mii_mng_write32(struct bcm_ns_usb3 *usb3, u32 value)
170 + err = bcm_ns_usb3_mii_mng_wait_idle(usb3);
172 + dev_err(usb3->dev, "Couldn't write 0x%08x value\n", value);
176 + writel(value, usb3->ccb_mii + BCMA_CCB_MII_MNG_CMD_DATA);
181 +static int bcm_ns_usb3_phy_init_ns_bx(struct bcm_ns_usb3 *usb3)
185 + /* Enable MDIO. Setting MDCDIV as 26 */
186 + writel(0x0000009a, usb3->ccb_mii + BCMA_CCB_MII_MNG_CTL);
188 + /* Wait for MDIO? */
191 + /* USB3 PLL Block */
192 + err = bcm_ns_usb3_mii_mng_write32(usb3, 0x587e8000);
196 + /* Assert Ana_Pllseq start */
197 + bcm_ns_usb3_mii_mng_write32(usb3, 0x58061000);
199 + /* Assert CML Divider ratio to 26 */
200 + bcm_ns_usb3_mii_mng_write32(usb3, 0x582a6400);
202 + /* Asserting PLL Reset */
203 + bcm_ns_usb3_mii_mng_write32(usb3, 0x582ec000);
205 + /* Deaaserting PLL Reset */
206 + bcm_ns_usb3_mii_mng_write32(usb3, 0x582e8000);
208 + /* Waiting MII Mgt interface idle */
209 + bcm_ns_usb3_mii_mng_wait_idle(usb3);
211 + /* Deasserting USB3 system reset */
212 + writel(0, usb3->dmp + BCMA_RESET_CTL);
214 + /* PLL frequency monitor enable */
215 + bcm_ns_usb3_mii_mng_write32(usb3, 0x58069000);
218 + bcm_ns_usb3_mii_mng_write32(usb3, 0x587e8060);
220 + /* CMPMAX & CMPMINTH setting */
221 + bcm_ns_usb3_mii_mng_write32(usb3, 0x580af30d);
223 + /* DEGLITCH MIN & MAX setting */
224 + bcm_ns_usb3_mii_mng_write32(usb3, 0x580e6302);
227 + bcm_ns_usb3_mii_mng_write32(usb3, 0x587e8040);
230 + bcm_ns_usb3_mii_mng_write32(usb3, 0x58061003);
232 + /* Waiting MII Mgt interface idle */
233 + bcm_ns_usb3_mii_mng_wait_idle(usb3);
238 +static int bcm_ns_usb3_phy_init_ns_ax(struct bcm_ns_usb3 *usb3)
242 + /* Enable MDIO. Setting MDCDIV as 26 */
243 + writel(0x0000009a, usb3->ccb_mii + BCMA_CCB_MII_MNG_CTL);
245 + /* Wait for MDIO? */
249 + err = bcm_ns_usb3_mii_mng_write32(usb3, 0x587e8000);
253 + bcm_ns_usb3_mii_mng_write32(usb3, 0x582a6400);
255 + bcm_ns_usb3_mii_mng_write32(usb3, 0x587e80e0);
257 + bcm_ns_usb3_mii_mng_write32(usb3, 0x580a009c);
260 + bcm_ns_usb3_mii_mng_write32(usb3, 0x587e8040);
262 + bcm_ns_usb3_mii_mng_write32(usb3, 0x580a21d3);
264 + bcm_ns_usb3_mii_mng_write32(usb3, 0x58061003);
266 + /* Waiting MII Mgt interface idle */
267 + bcm_ns_usb3_mii_mng_wait_idle(usb3);
269 + /* Deasserting USB3 system reset */
270 + writel(0, usb3->dmp + BCMA_RESET_CTL);
275 +static int bcm_ns_usb3_phy_init(struct phy *phy)
277 + struct bcm_ns_usb3 *usb3 = phy_get_drvdata(phy);
280 + /* Perform USB3 system soft reset */
281 + writel(BCMA_RESET_CTL_RESET, usb3->dmp + BCMA_RESET_CTL);
283 + switch (usb3->family) {
285 + err = bcm_ns_usb3_phy_init_ns_ax(usb3);
288 + err = bcm_ns_usb3_phy_init_ns_bx(usb3);
298 +static const struct phy_ops ops = {
299 + .init = bcm_ns_usb3_phy_init,
300 + .owner = THIS_MODULE,
303 +static int bcm_ns_usb3_probe(struct platform_device *pdev)
305 + struct device *dev = &pdev->dev;
306 + const struct of_device_id *of_id;
307 + struct bcm_ns_usb3 *usb3;
308 + struct resource *res;
309 + struct phy_provider *phy_provider;
311 + usb3 = devm_kzalloc(dev, sizeof(*usb3), GFP_KERNEL);
317 + of_id = of_match_device(bcm_ns_usb3_id_table, dev);
320 + usb3->family = (enum bcm_ns_family)of_id->data;
322 + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dmp");
323 + usb3->dmp = devm_ioremap_resource(dev, res);
324 + if (IS_ERR(usb3->dmp)) {
325 + dev_err(dev, "Failed to map DMP regs\n");
326 + return PTR_ERR(usb3->dmp);
329 + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ccb-mii");
330 + usb3->ccb_mii = devm_ioremap_resource(dev, res);
331 + if (IS_ERR(usb3->ccb_mii)) {
332 + dev_err(dev, "Failed to map ChipCommon B MII regs\n");
333 + return PTR_ERR(usb3->ccb_mii);
336 + usb3->phy = devm_phy_create(dev, NULL, &ops);
337 + if (IS_ERR(usb3->phy)) {
338 + dev_err(dev, "Failed to create PHY\n");
339 + return PTR_ERR(usb3->phy);
342 + phy_set_drvdata(usb3->phy, usb3);
343 + platform_set_drvdata(pdev, usb3);
345 + phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
346 + if (!IS_ERR(phy_provider))
347 + dev_info(dev, "Registered Broadcom Northstar USB 3.0 PHY driver\n");
349 + return PTR_ERR_OR_ZERO(phy_provider);
352 +static struct platform_driver bcm_ns_usb3_driver = {
353 + .probe = bcm_ns_usb3_probe,
355 + .name = "bcm_ns_usb3",
356 + .of_match_table = bcm_ns_usb3_id_table,
359 +module_platform_driver(bcm_ns_usb3_driver);
361 +MODULE_LICENSE("GPL v2");