2 * Broadcom Northstar USB 3.0 PHY Driver
4 * Copyright (C) 2016 Rafał Miłecki <rafal@milecki.pl>
5 * Copyright (C) 2016 Broadcom
7 * All magic values used for initialization (and related comments) were obtained
9 * Copyright (c) Broadcom Corp, 2012
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <linux/bcma/bcma.h>
17 #include <linux/delay.h>
18 #include <linux/err.h>
19 #include <linux/mdio.h>
20 #include <linux/module.h>
21 #include <linux/of_address.h>
22 #include <linux/of_platform.h>
23 #include <linux/platform_device.h>
24 #include <linux/phy/phy.h>
25 #include <linux/slab.h>
27 #define BCM_NS_USB3_MII_MNG_TIMEOUT_US 1000 /* usecs */
29 #define BCM_NS_USB3_PHY_BASE_ADDR_REG 0x1f
30 #define BCM_NS_USB3_PHY_PLL30_BLOCK 0x8000
31 #define BCM_NS_USB3_PHY_TX_PMD_BLOCK 0x8040
32 #define BCM_NS_USB3_PHY_PIPE_BLOCK 0x8060
34 /* Registers of PLL30 block */
35 #define BCM_NS_USB3_PLL_CONTROL 0x01
36 #define BCM_NS_USB3_PLLA_CONTROL0 0x0a
37 #define BCM_NS_USB3_PLLA_CONTROL1 0x0b
39 /* Registers of TX PMD block */
40 #define BCM_NS_USB3_TX_PMD_CONTROL1 0x01
42 /* Registers of PIPE block */
43 #define BCM_NS_USB3_LFPS_CMP 0x02
44 #define BCM_NS_USB3_LFPS_DEGLITCH 0x03
54 enum bcm_ns_family family
;
56 void __iomem
*ccb_mii
;
57 struct mdio_device
*mdiodev
;
60 int (*phy_write
)(struct bcm_ns_usb3
*usb3
, u16 reg
, u16 value
);
63 static const struct of_device_id bcm_ns_usb3_id_table
[] = {
65 .compatible
= "brcm,ns-ax-usb3-phy",
66 .data
= (int *)BCM_NS_AX
,
69 .compatible
= "brcm,ns-bx-usb3-phy",
70 .data
= (int *)BCM_NS_BX
,
74 MODULE_DEVICE_TABLE(of
, bcm_ns_usb3_id_table
);
76 static int bcm_ns_usb3_mdio_phy_write(struct bcm_ns_usb3
*usb3
, u16 reg
,
79 return usb3
->phy_write(usb3
, reg
, value
);
82 static int bcm_ns_usb3_phy_init_ns_bx(struct bcm_ns_usb3
*usb3
)
87 err
= bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PHY_BASE_ADDR_REG
,
88 BCM_NS_USB3_PHY_PLL30_BLOCK
);
92 /* Assert Ana_Pllseq start */
93 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PLL_CONTROL
, 0x1000);
95 /* Assert CML Divider ratio to 26 */
96 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PLLA_CONTROL0
, 0x6400);
98 /* Asserting PLL Reset */
99 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PLLA_CONTROL1
, 0xc000);
101 /* Deaaserting PLL Reset */
102 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PLLA_CONTROL1
, 0x8000);
104 /* Deasserting USB3 system reset */
105 writel(0, usb3
->dmp
+ BCMA_RESET_CTL
);
107 /* PLL frequency monitor enable */
108 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PLL_CONTROL
, 0x9000);
111 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PHY_BASE_ADDR_REG
,
112 BCM_NS_USB3_PHY_PIPE_BLOCK
);
114 /* CMPMAX & CMPMINTH setting */
115 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_LFPS_CMP
, 0xf30d);
117 /* DEGLITCH MIN & MAX setting */
118 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_LFPS_DEGLITCH
, 0x6302);
121 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PHY_BASE_ADDR_REG
,
122 BCM_NS_USB3_PHY_TX_PMD_BLOCK
);
125 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_TX_PMD_CONTROL1
, 0x1003);
130 static int bcm_ns_usb3_phy_init_ns_ax(struct bcm_ns_usb3
*usb3
)
135 err
= bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PHY_BASE_ADDR_REG
,
136 BCM_NS_USB3_PHY_PLL30_BLOCK
);
140 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PLLA_CONTROL0
, 0x6400);
142 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PHY_BASE_ADDR_REG
, 0x80e0);
144 bcm_ns_usb3_mdio_phy_write(usb3
, 0x02, 0x009c);
147 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PHY_BASE_ADDR_REG
,
148 BCM_NS_USB3_PHY_TX_PMD_BLOCK
);
150 bcm_ns_usb3_mdio_phy_write(usb3
, 0x02, 0x21d3);
152 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_TX_PMD_CONTROL1
, 0x1003);
154 /* Deasserting USB3 system reset */
155 writel(0, usb3
->dmp
+ BCMA_RESET_CTL
);
160 static int bcm_ns_usb3_phy_init(struct phy
*phy
)
162 struct bcm_ns_usb3
*usb3
= phy_get_drvdata(phy
);
165 /* Perform USB3 system soft reset */
166 writel(BCMA_RESET_CTL_RESET
, usb3
->dmp
+ BCMA_RESET_CTL
);
168 switch (usb3
->family
) {
170 err
= bcm_ns_usb3_phy_init_ns_ax(usb3
);
173 err
= bcm_ns_usb3_phy_init_ns_bx(usb3
);
183 static const struct phy_ops ops
= {
184 .init
= bcm_ns_usb3_phy_init
,
185 .owner
= THIS_MODULE
,
188 /**************************************************
190 **************************************************/
192 static int bcm_ns_usb3_mdiodev_phy_write(struct bcm_ns_usb3
*usb3
, u16 reg
,
195 struct mdio_device
*mdiodev
= usb3
->mdiodev
;
197 return mdiobus_write(mdiodev
->bus
, mdiodev
->addr
, reg
, value
);
200 static int bcm_ns_usb3_mdio_probe(struct mdio_device
*mdiodev
)
202 struct device
*dev
= &mdiodev
->dev
;
203 const struct of_device_id
*of_id
;
204 struct phy_provider
*phy_provider
;
205 struct device_node
*syscon_np
;
206 struct bcm_ns_usb3
*usb3
;
210 usb3
= devm_kzalloc(dev
, sizeof(*usb3
), GFP_KERNEL
);
215 usb3
->mdiodev
= mdiodev
;
217 of_id
= of_match_device(bcm_ns_usb3_id_table
, dev
);
220 usb3
->family
= (enum bcm_ns_family
)of_id
->data
;
222 syscon_np
= of_parse_phandle(dev
->of_node
, "usb3-dmp-syscon", 0);
223 err
= of_address_to_resource(syscon_np
, 0, &res
);
224 of_node_put(syscon_np
);
228 usb3
->dmp
= devm_ioremap_resource(dev
, &res
);
229 if (IS_ERR(usb3
->dmp
)) {
230 dev_err(dev
, "Failed to map DMP regs\n");
231 return PTR_ERR(usb3
->dmp
);
234 usb3
->phy_write
= bcm_ns_usb3_mdiodev_phy_write
;
236 usb3
->phy
= devm_phy_create(dev
, NULL
, &ops
);
237 if (IS_ERR(usb3
->phy
)) {
238 dev_err(dev
, "Failed to create PHY\n");
239 return PTR_ERR(usb3
->phy
);
242 phy_set_drvdata(usb3
->phy
, usb3
);
244 phy_provider
= devm_of_phy_provider_register(dev
, of_phy_simple_xlate
);
246 return PTR_ERR_OR_ZERO(phy_provider
);
249 static struct mdio_driver bcm_ns_usb3_mdio_driver
= {
252 .name
= "bcm_ns_mdio_usb3",
253 .of_match_table
= bcm_ns_usb3_id_table
,
256 .probe
= bcm_ns_usb3_mdio_probe
,
259 /**************************************************
260 * Platform driver code
261 **************************************************/
263 static int bcm_ns_usb3_wait_reg(struct bcm_ns_usb3
*usb3
, void __iomem
*addr
,
264 u32 mask
, u32 value
, unsigned long timeout
)
266 unsigned long deadline
= jiffies
+ timeout
;
271 if ((val
& mask
) == value
)
275 } while (!time_after_eq(jiffies
, deadline
));
277 dev_err(usb3
->dev
, "Timeout waiting for register %p\n", addr
);
282 static inline int bcm_ns_usb3_mii_mng_wait_idle(struct bcm_ns_usb3
*usb3
)
284 return bcm_ns_usb3_wait_reg(usb3
, usb3
->ccb_mii
+ BCMA_CCB_MII_MNG_CTL
,
286 usecs_to_jiffies(BCM_NS_USB3_MII_MNG_TIMEOUT_US
));
289 static int bcm_ns_usb3_platform_phy_write(struct bcm_ns_usb3
*usb3
, u16 reg
,
295 err
= bcm_ns_usb3_mii_mng_wait_idle(usb3
);
297 dev_err(usb3
->dev
, "Couldn't write 0x%08x value\n", value
);
301 /* TODO: Use a proper MDIO bus layer */
302 tmp
|= 0x58020000; /* Magic value for MDIO PHY write */
305 writel(tmp
, usb3
->ccb_mii
+ BCMA_CCB_MII_MNG_CMD_DATA
);
307 return bcm_ns_usb3_mii_mng_wait_idle(usb3
);
310 static int bcm_ns_usb3_probe(struct platform_device
*pdev
)
312 struct device
*dev
= &pdev
->dev
;
313 const struct of_device_id
*of_id
;
314 struct bcm_ns_usb3
*usb3
;
315 struct resource
*res
;
316 struct phy_provider
*phy_provider
;
318 usb3
= devm_kzalloc(dev
, sizeof(*usb3
), GFP_KERNEL
);
324 of_id
= of_match_device(bcm_ns_usb3_id_table
, dev
);
327 usb3
->family
= (enum bcm_ns_family
)of_id
->data
;
329 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dmp");
330 usb3
->dmp
= devm_ioremap_resource(dev
, res
);
331 if (IS_ERR(usb3
->dmp
)) {
332 dev_err(dev
, "Failed to map DMP regs\n");
333 return PTR_ERR(usb3
->dmp
);
336 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "ccb-mii");
337 usb3
->ccb_mii
= devm_ioremap_resource(dev
, res
);
338 if (IS_ERR(usb3
->ccb_mii
)) {
339 dev_err(dev
, "Failed to map ChipCommon B MII regs\n");
340 return PTR_ERR(usb3
->ccb_mii
);
343 /* Enable MDIO. Setting MDCDIV as 26 */
344 writel(0x0000009a, usb3
->ccb_mii
+ BCMA_CCB_MII_MNG_CTL
);
349 usb3
->phy_write
= bcm_ns_usb3_platform_phy_write
;
351 usb3
->phy
= devm_phy_create(dev
, NULL
, &ops
);
352 if (IS_ERR(usb3
->phy
)) {
353 dev_err(dev
, "Failed to create PHY\n");
354 return PTR_ERR(usb3
->phy
);
357 phy_set_drvdata(usb3
->phy
, usb3
);
358 platform_set_drvdata(pdev
, usb3
);
360 phy_provider
= devm_of_phy_provider_register(dev
, of_phy_simple_xlate
);
361 if (!IS_ERR(phy_provider
))
362 dev_info(dev
, "Registered Broadcom Northstar USB 3.0 PHY driver\n");
364 return PTR_ERR_OR_ZERO(phy_provider
);
367 static struct platform_driver bcm_ns_usb3_driver
= {
368 .probe
= bcm_ns_usb3_probe
,
370 .name
= "bcm_ns_usb3",
371 .of_match_table
= bcm_ns_usb3_id_table
,
375 static int __init
bcm_ns_usb3_module_init(void)
380 * For backward compatibility we register as MDIO and platform driver.
381 * After getting MDIO binding commonly used (e.g. switching all DT files
382 * to use it) we should deprecate the old binding and eventually drop
386 err
= mdio_driver_register(&bcm_ns_usb3_mdio_driver
);
390 err
= platform_driver_register(&bcm_ns_usb3_driver
);
392 mdio_driver_unregister(&bcm_ns_usb3_mdio_driver
);
396 module_init(bcm_ns_usb3_module_init
);
398 static void __exit
bcm_ns_usb3_module_exit(void)
400 platform_driver_unregister(&bcm_ns_usb3_driver
);
401 mdio_driver_unregister(&bcm_ns_usb3_mdio_driver
);
403 module_exit(bcm_ns_usb3_module_exit
)
405 MODULE_LICENSE("GPL v2");