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/module.h>
20 #include <linux/of_platform.h>
21 #include <linux/platform_device.h>
22 #include <linux/phy/phy.h>
23 #include <linux/slab.h>
25 #define BCM_NS_USB3_MII_MNG_TIMEOUT_US 1000 /* usecs */
27 #define BCM_NS_USB3_PHY_BASE_ADDR_REG 0x1f
28 #define BCM_NS_USB3_PHY_PLL30_BLOCK 0x8000
29 #define BCM_NS_USB3_PHY_TX_PMD_BLOCK 0x8040
30 #define BCM_NS_USB3_PHY_PIPE_BLOCK 0x8060
32 /* Registers of PLL30 block */
33 #define BCM_NS_USB3_PLL_CONTROL 0x01
34 #define BCM_NS_USB3_PLLA_CONTROL0 0x0a
35 #define BCM_NS_USB3_PLLA_CONTROL1 0x0b
37 /* Registers of TX PMD block */
38 #define BCM_NS_USB3_TX_PMD_CONTROL1 0x01
40 /* Registers of PIPE block */
41 #define BCM_NS_USB3_LFPS_CMP 0x02
42 #define BCM_NS_USB3_LFPS_DEGLITCH 0x03
52 enum bcm_ns_family family
;
54 void __iomem
*ccb_mii
;
58 static const struct of_device_id bcm_ns_usb3_id_table
[] = {
60 .compatible
= "brcm,ns-ax-usb3-phy",
61 .data
= (int *)BCM_NS_AX
,
64 .compatible
= "brcm,ns-bx-usb3-phy",
65 .data
= (int *)BCM_NS_BX
,
69 MODULE_DEVICE_TABLE(of
, bcm_ns_usb3_id_table
);
71 static int bcm_ns_usb3_wait_reg(struct bcm_ns_usb3
*usb3
, void __iomem
*addr
,
72 u32 mask
, u32 value
, unsigned long timeout
)
74 unsigned long deadline
= jiffies
+ timeout
;
79 if ((val
& mask
) == value
)
83 } while (!time_after_eq(jiffies
, deadline
));
85 dev_err(usb3
->dev
, "Timeout waiting for register %p\n", addr
);
90 static inline int bcm_ns_usb3_mii_mng_wait_idle(struct bcm_ns_usb3
*usb3
)
92 return bcm_ns_usb3_wait_reg(usb3
, usb3
->ccb_mii
+ BCMA_CCB_MII_MNG_CTL
,
94 usecs_to_jiffies(BCM_NS_USB3_MII_MNG_TIMEOUT_US
));
97 static int bcm_ns_usb3_mdio_phy_write(struct bcm_ns_usb3
*usb3
, u16 reg
,
103 err
= bcm_ns_usb3_mii_mng_wait_idle(usb3
);
105 dev_err(usb3
->dev
, "Couldn't write 0x%08x value\n", value
);
109 /* TODO: Use a proper MDIO bus layer */
110 tmp
|= 0x58020000; /* Magic value for MDIO PHY write */
113 writel(tmp
, usb3
->ccb_mii
+ BCMA_CCB_MII_MNG_CMD_DATA
);
118 static int bcm_ns_usb3_phy_init_ns_bx(struct bcm_ns_usb3
*usb3
)
122 /* Enable MDIO. Setting MDCDIV as 26 */
123 writel(0x0000009a, usb3
->ccb_mii
+ BCMA_CCB_MII_MNG_CTL
);
129 err
= bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PHY_BASE_ADDR_REG
,
130 BCM_NS_USB3_PHY_PLL30_BLOCK
);
134 /* Assert Ana_Pllseq start */
135 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PLL_CONTROL
, 0x1000);
137 /* Assert CML Divider ratio to 26 */
138 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PLLA_CONTROL0
, 0x6400);
140 /* Asserting PLL Reset */
141 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PLLA_CONTROL1
, 0xc000);
143 /* Deaaserting PLL Reset */
144 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PLLA_CONTROL1
, 0x8000);
146 /* Waiting MII Mgt interface idle */
147 bcm_ns_usb3_mii_mng_wait_idle(usb3
);
149 /* Deasserting USB3 system reset */
150 writel(0, usb3
->dmp
+ BCMA_RESET_CTL
);
152 /* PLL frequency monitor enable */
153 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PLL_CONTROL
, 0x9000);
156 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PHY_BASE_ADDR_REG
,
157 BCM_NS_USB3_PHY_PIPE_BLOCK
);
159 /* CMPMAX & CMPMINTH setting */
160 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_LFPS_CMP
, 0xf30d);
162 /* DEGLITCH MIN & MAX setting */
163 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_LFPS_DEGLITCH
, 0x6302);
166 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PHY_BASE_ADDR_REG
,
167 BCM_NS_USB3_PHY_TX_PMD_BLOCK
);
170 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_TX_PMD_CONTROL1
, 0x1003);
172 /* Waiting MII Mgt interface idle */
173 bcm_ns_usb3_mii_mng_wait_idle(usb3
);
178 static int bcm_ns_usb3_phy_init_ns_ax(struct bcm_ns_usb3
*usb3
)
182 /* Enable MDIO. Setting MDCDIV as 26 */
183 writel(0x0000009a, usb3
->ccb_mii
+ BCMA_CCB_MII_MNG_CTL
);
189 err
= bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PHY_BASE_ADDR_REG
,
190 BCM_NS_USB3_PHY_PLL30_BLOCK
);
194 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PLLA_CONTROL0
, 0x6400);
196 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PHY_BASE_ADDR_REG
, 0x80e0);
198 bcm_ns_usb3_mdio_phy_write(usb3
, 0x02, 0x009c);
201 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_PHY_BASE_ADDR_REG
,
202 BCM_NS_USB3_PHY_TX_PMD_BLOCK
);
204 bcm_ns_usb3_mdio_phy_write(usb3
, 0x02, 0x21d3);
206 bcm_ns_usb3_mdio_phy_write(usb3
, BCM_NS_USB3_TX_PMD_CONTROL1
, 0x1003);
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
);
217 static int bcm_ns_usb3_phy_init(struct phy
*phy
)
219 struct bcm_ns_usb3
*usb3
= phy_get_drvdata(phy
);
222 /* Perform USB3 system soft reset */
223 writel(BCMA_RESET_CTL_RESET
, usb3
->dmp
+ BCMA_RESET_CTL
);
225 switch (usb3
->family
) {
227 err
= bcm_ns_usb3_phy_init_ns_ax(usb3
);
230 err
= bcm_ns_usb3_phy_init_ns_bx(usb3
);
240 static const struct phy_ops ops
= {
241 .init
= bcm_ns_usb3_phy_init
,
242 .owner
= THIS_MODULE
,
245 static int bcm_ns_usb3_probe(struct platform_device
*pdev
)
247 struct device
*dev
= &pdev
->dev
;
248 const struct of_device_id
*of_id
;
249 struct bcm_ns_usb3
*usb3
;
250 struct resource
*res
;
251 struct phy_provider
*phy_provider
;
253 usb3
= devm_kzalloc(dev
, sizeof(*usb3
), GFP_KERNEL
);
259 of_id
= of_match_device(bcm_ns_usb3_id_table
, dev
);
262 usb3
->family
= (enum bcm_ns_family
)of_id
->data
;
264 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dmp");
265 usb3
->dmp
= devm_ioremap_resource(dev
, res
);
266 if (IS_ERR(usb3
->dmp
)) {
267 dev_err(dev
, "Failed to map DMP regs\n");
268 return PTR_ERR(usb3
->dmp
);
271 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "ccb-mii");
272 usb3
->ccb_mii
= devm_ioremap_resource(dev
, res
);
273 if (IS_ERR(usb3
->ccb_mii
)) {
274 dev_err(dev
, "Failed to map ChipCommon B MII regs\n");
275 return PTR_ERR(usb3
->ccb_mii
);
278 usb3
->phy
= devm_phy_create(dev
, NULL
, &ops
);
279 if (IS_ERR(usb3
->phy
)) {
280 dev_err(dev
, "Failed to create PHY\n");
281 return PTR_ERR(usb3
->phy
);
284 phy_set_drvdata(usb3
->phy
, usb3
);
285 platform_set_drvdata(pdev
, usb3
);
287 phy_provider
= devm_of_phy_provider_register(dev
, of_phy_simple_xlate
);
288 if (!IS_ERR(phy_provider
))
289 dev_info(dev
, "Registered Broadcom Northstar USB 3.0 PHY driver\n");
291 return PTR_ERR_OR_ZERO(phy_provider
);
294 static struct platform_driver bcm_ns_usb3_driver
= {
295 .probe
= bcm_ns_usb3_probe
,
297 .name
= "bcm_ns_usb3",
298 .of_match_table
= bcm_ns_usb3_id_table
,
301 module_platform_driver(bcm_ns_usb3_driver
);
303 MODULE_LICENSE("GPL v2");