1 // SPDX-License-Identifier: GPL-2.0-only
3 * Broadcom Northstar USB 3.0 PHY Driver
5 * Copyright (C) 2016 Rafał Miłecki <rafal@milecki.pl>
6 * Copyright (C) 2016 Broadcom
8 * All magic values used for initialization (and related comments) were obtained
10 * Copyright (c) Broadcom Corp, 2012
13 #include <linux/bcma/bcma.h>
14 #include <linux/delay.h>
15 #include <linux/err.h>
16 #include <linux/mdio.h>
17 #include <linux/module.h>
18 #include <linux/of_address.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_device.h>
21 #include <linux/phy/phy.h>
22 #include <linux/slab.h>
24 #define BCM_NS_USB3_MII_MNG_TIMEOUT_US 1000 /* usecs */
26 #define BCM_NS_USB3_PHY_BASE_ADDR_REG 0x1f
27 #define BCM_NS_USB3_PHY_PLL30_BLOCK 0x8000
28 #define BCM_NS_USB3_PHY_TX_PMD_BLOCK 0x8040
29 #define BCM_NS_USB3_PHY_PIPE_BLOCK 0x8060
31 /* Registers of PLL30 block */
32 #define BCM_NS_USB3_PLL_CONTROL 0x01
33 #define BCM_NS_USB3_PLLA_CONTROL0 0x0a
34 #define BCM_NS_USB3_PLLA_CONTROL1 0x0b
36 /* Registers of TX PMD block */
37 #define BCM_NS_USB3_TX_PMD_CONTROL1 0x01
39 /* Registers of PIPE block */
40 #define BCM_NS_USB3_LFPS_CMP 0x02
41 #define BCM_NS_USB3_LFPS_DEGLITCH 0x03
51 enum bcm_ns_family family
;
53 void __iomem
*ccb_mii
;
54 struct mdio_device
*mdiodev
;
57 int (*phy_write
)(struct bcm_ns_usb3
*usb3
, u16 reg
, u16 value
);
60 static const struct of_device_id bcm_ns_usb3_id_table
[] = {
62 .compatible
= "brcm,ns-ax-usb3-phy",
63 .data
= (int *)BCM_NS_AX
,
66 .compatible
= "brcm,ns-bx-usb3-phy",
67 .data
= (int *)BCM_NS_BX
,
71 MODULE_DEVICE_TABLE(of
, bcm_ns_usb3_id_table
);
73 static int bcm_ns_usb3_mdio_phy_write(struct bcm_ns_usb3
*usb3
, u16 reg
,
76 return usb3
->phy_write(usb3
, reg
, value
);
79 static int bcm_ns_usb3_phy_init_ns_bx(struct bcm_ns_usb3
*usb3
)
84 err
= bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PHY_BASE_ADDR_REG
,
85 BCM_NS_USB3_PHY_PLL30_BLOCK
);
89 /* Assert Ana_Pllseq start */
90 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PLL_CONTROL
, 0x1000);
92 /* Assert CML Divider ratio to 26 */
93 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PLLA_CONTROL0
, 0x6400);
95 /* Asserting PLL Reset */
96 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PLLA_CONTROL1
, 0xc000);
98 /* Deaaserting PLL Reset */
99 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PLLA_CONTROL1
, 0x8000);
101 /* Deasserting USB3 system reset */
102 writel(0, usb3
->dmp
+ BCMA_RESET_CTL
);
104 /* PLL frequency monitor enable */
105 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PLL_CONTROL
, 0x9000);
108 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PHY_BASE_ADDR_REG
,
109 BCM_NS_USB3_PHY_PIPE_BLOCK
);
111 /* CMPMAX & CMPMINTH setting */
112 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_LFPS_CMP
, 0xf30d);
114 /* DEGLITCH MIN & MAX setting */
115 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_LFPS_DEGLITCH
, 0x6302);
118 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PHY_BASE_ADDR_REG
,
119 BCM_NS_USB3_PHY_TX_PMD_BLOCK
);
122 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_TX_PMD_CONTROL1
, 0x1003);
127 static int bcm_ns_usb3_phy_init_ns_ax(struct bcm_ns_usb3
*usb3
)
132 err
= bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PHY_BASE_ADDR_REG
,
133 BCM_NS_USB3_PHY_PLL30_BLOCK
);
137 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PLLA_CONTROL0
, 0x6400);
139 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PHY_BASE_ADDR_REG
, 0x80e0);
141 bcm_ns_usb3_mdio_phy_write(usb3
, 0x02, 0x009c);
144 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PHY_BASE_ADDR_REG
,
145 BCM_NS_USB3_PHY_TX_PMD_BLOCK
);
147 bcm_ns_usb3_mdio_phy_write(usb3
, 0x02, 0x21d3);
149 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_TX_PMD_CONTROL1
, 0x1003);
151 /* Deasserting USB3 system reset */
152 writel(0, usb3
->dmp
+ BCMA_RESET_CTL
);
157 static int bcm_ns_usb3_phy_init(struct phy
*phy
)
159 struct bcm_ns_usb3
*usb3
= phy_get_drvdata(phy
);
162 /* Perform USB3 system soft reset */
163 writel(BCMA_RESET_CTL_RESET
, usb3
->dmp
+ BCMA_RESET_CTL
);
165 switch (usb3
->family
) {
167 err
= bcm_ns_usb3_phy_init_ns_ax(usb3
);
170 err
= bcm_ns_usb3_phy_init_ns_bx(usb3
);
180 static const struct phy_ops ops
= {
181 .init
= bcm_ns_usb3_phy_init
,
182 .owner
= THIS_MODULE
,
185 /**************************************************
187 **************************************************/
189 static int bcm_ns_usb3_mdiodev_phy_write(struct bcm_ns_usb3
*usb3
, u16 reg
,
192 struct mdio_device
*mdiodev
= usb3
->mdiodev
;
194 return mdiobus_write(mdiodev
->bus
, mdiodev
->addr
, reg
, value
);
197 static int bcm_ns_usb3_mdio_probe(struct mdio_device
*mdiodev
)
199 struct device
*dev
= &mdiodev
->dev
;
200 const struct of_device_id
*of_id
;
201 struct phy_provider
*phy_provider
;
202 struct device_node
*syscon_np
;
203 struct bcm_ns_usb3
*usb3
;
207 usb3
= devm_kzalloc(dev
, sizeof(*usb3
), GFP_KERNEL
);
212 usb3
->mdiodev
= mdiodev
;
214 of_id
= of_match_device(bcm_ns_usb3_id_table
, dev
);
217 usb3
->family
= (enum bcm_ns_family
)of_id
->data
;
219 syscon_np
= of_parse_phandle(dev
->of_node
, "usb3-dmp-syscon", 0);
220 err
= of_address_to_resource(syscon_np
, 0, &res
);
221 of_node_put(syscon_np
);
225 usb3
->dmp
= devm_ioremap_resource(dev
, &res
);
226 if (IS_ERR(usb3
->dmp
)) {
227 dev_err(dev
, "Failed to map DMP regs\n");
228 return PTR_ERR(usb3
->dmp
);
231 usb3
->phy_write
= bcm_ns_usb3_mdiodev_phy_write
;
233 usb3
->phy
= devm_phy_create(dev
, NULL
, &ops
);
234 if (IS_ERR(usb3
->phy
)) {
235 dev_err(dev
, "Failed to create PHY\n");
236 return PTR_ERR(usb3
->phy
);
239 phy_set_drvdata(usb3
->phy
, usb3
);
241 phy_provider
= devm_of_phy_provider_register(dev
, of_phy_simple_xlate
);
243 return PTR_ERR_OR_ZERO(phy_provider
);
246 static struct mdio_driver bcm_ns_usb3_mdio_driver
= {
249 .name
= "bcm_ns_mdio_usb3",
250 .of_match_table
= bcm_ns_usb3_id_table
,
253 .probe
= bcm_ns_usb3_mdio_probe
,
256 /**************************************************
257 * Platform driver code
258 **************************************************/
260 static int bcm_ns_usb3_wait_reg(struct bcm_ns_usb3
*usb3
, void __iomem
*addr
,
261 u32 mask
, u32 value
, unsigned long timeout
)
263 unsigned long deadline
= jiffies
+ timeout
;
268 if ((val
& mask
) == value
)
272 } while (!time_after_eq(jiffies
, deadline
));
274 dev_err(usb3
->dev
, "Timeout waiting for register %p\n", addr
);
279 static inline int bcm_ns_usb3_mii_mng_wait_idle(struct bcm_ns_usb3
*usb3
)
281 return bcm_ns_usb3_wait_reg(usb3
, usb3
->ccb_mii
+ BCMA_CCB_MII_MNG_CTL
,
283 usecs_to_jiffies(BCM_NS_USB3_MII_MNG_TIMEOUT_US
));
286 static int bcm_ns_usb3_platform_phy_write(struct bcm_ns_usb3
*usb3
, u16 reg
,
292 err
= bcm_ns_usb3_mii_mng_wait_idle(usb3
);
294 dev_err(usb3
->dev
, "Couldn't write 0x%08x value\n", value
);
298 /* TODO: Use a proper MDIO bus layer */
299 tmp
|= 0x58020000; /* Magic value for MDIO PHY write */
302 writel(tmp
, usb3
->ccb_mii
+ BCMA_CCB_MII_MNG_CMD_DATA
);
304 return bcm_ns_usb3_mii_mng_wait_idle(usb3
);
307 static int bcm_ns_usb3_probe(struct platform_device
*pdev
)
309 struct device
*dev
= &pdev
->dev
;
310 const struct of_device_id
*of_id
;
311 struct bcm_ns_usb3
*usb3
;
312 struct resource
*res
;
313 struct phy_provider
*phy_provider
;
315 usb3
= devm_kzalloc(dev
, sizeof(*usb3
), GFP_KERNEL
);
321 of_id
= of_match_device(bcm_ns_usb3_id_table
, dev
);
324 usb3
->family
= (enum bcm_ns_family
)of_id
->data
;
326 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dmp");
327 usb3
->dmp
= devm_ioremap_resource(dev
, res
);
328 if (IS_ERR(usb3
->dmp
)) {
329 dev_err(dev
, "Failed to map DMP regs\n");
330 return PTR_ERR(usb3
->dmp
);
333 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "ccb-mii");
334 usb3
->ccb_mii
= devm_ioremap_resource(dev
, res
);
335 if (IS_ERR(usb3
->ccb_mii
)) {
336 dev_err(dev
, "Failed to map ChipCommon B MII regs\n");
337 return PTR_ERR(usb3
->ccb_mii
);
340 /* Enable MDIO. Setting MDCDIV as 26 */
341 writel(0x0000009a, usb3
->ccb_mii
+ BCMA_CCB_MII_MNG_CTL
);
346 usb3
->phy_write
= bcm_ns_usb3_platform_phy_write
;
348 usb3
->phy
= devm_phy_create(dev
, NULL
, &ops
);
349 if (IS_ERR(usb3
->phy
)) {
350 dev_err(dev
, "Failed to create PHY\n");
351 return PTR_ERR(usb3
->phy
);
354 phy_set_drvdata(usb3
->phy
, usb3
);
355 platform_set_drvdata(pdev
, usb3
);
357 phy_provider
= devm_of_phy_provider_register(dev
, of_phy_simple_xlate
);
358 if (!IS_ERR(phy_provider
))
359 dev_info(dev
, "Registered Broadcom Northstar USB 3.0 PHY driver\n");
361 return PTR_ERR_OR_ZERO(phy_provider
);
364 static struct platform_driver bcm_ns_usb3_driver
= {
365 .probe
= bcm_ns_usb3_probe
,
367 .name
= "bcm_ns_usb3",
368 .of_match_table
= bcm_ns_usb3_id_table
,
372 static int __init
bcm_ns_usb3_module_init(void)
377 * For backward compatibility we register as MDIO and platform driver.
378 * After getting MDIO binding commonly used (e.g. switching all DT files
379 * to use it) we should deprecate the old binding and eventually drop
383 err
= mdio_driver_register(&bcm_ns_usb3_mdio_driver
);
387 err
= platform_driver_register(&bcm_ns_usb3_driver
);
389 mdio_driver_unregister(&bcm_ns_usb3_mdio_driver
);
393 module_init(bcm_ns_usb3_module_init
);
395 static void __exit
bcm_ns_usb3_module_exit(void)
397 platform_driver_unregister(&bcm_ns_usb3_driver
);
398 mdio_driver_unregister(&bcm_ns_usb3_mdio_driver
);
400 module_exit(bcm_ns_usb3_module_exit
)
402 MODULE_LICENSE("GPL v2");