treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / phy / broadcom / phy-bcm-ns-usb3.c
blob14f45bc35cc5e86805d94a210f6061bfc285783d
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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
9 * from Broadcom's SDK:
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
43 enum bcm_ns_family {
44 BCM_NS_UNKNOWN,
45 BCM_NS_AX,
46 BCM_NS_BX,
49 struct bcm_ns_usb3 {
50 struct device *dev;
51 enum bcm_ns_family family;
52 void __iomem *dmp;
53 void __iomem *ccb_mii;
54 struct mdio_device *mdiodev;
55 struct phy *phy;
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,
69 {},
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,
74 u16 value)
76 return usb3->phy_write(usb3, reg, value);
79 static int bcm_ns_usb3_phy_init_ns_bx(struct bcm_ns_usb3 *usb3)
81 int err;
83 /* USB3 PLL Block */
84 err = bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PHY_BASE_ADDR_REG,
85 BCM_NS_USB3_PHY_PLL30_BLOCK);
86 if (err < 0)
87 return err;
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);
107 /* PIPE Block */
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);
117 /* TXPMD block */
118 bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PHY_BASE_ADDR_REG,
119 BCM_NS_USB3_PHY_TX_PMD_BLOCK);
121 /* Enabling SSC */
122 bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_TX_PMD_CONTROL1, 0x1003);
124 return 0;
127 static int bcm_ns_usb3_phy_init_ns_ax(struct bcm_ns_usb3 *usb3)
129 int err;
131 /* PLL30 block */
132 err = bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PHY_BASE_ADDR_REG,
133 BCM_NS_USB3_PHY_PLL30_BLOCK);
134 if (err < 0)
135 return err;
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);
143 /* Enable SSC */
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);
154 return 0;
157 static int bcm_ns_usb3_phy_init(struct phy *phy)
159 struct bcm_ns_usb3 *usb3 = phy_get_drvdata(phy);
160 int err;
162 /* Perform USB3 system soft reset */
163 writel(BCMA_RESET_CTL_RESET, usb3->dmp + BCMA_RESET_CTL);
165 switch (usb3->family) {
166 case BCM_NS_AX:
167 err = bcm_ns_usb3_phy_init_ns_ax(usb3);
168 break;
169 case BCM_NS_BX:
170 err = bcm_ns_usb3_phy_init_ns_bx(usb3);
171 break;
172 default:
173 WARN_ON(1);
174 err = -ENOTSUPP;
177 return err;
180 static const struct phy_ops ops = {
181 .init = bcm_ns_usb3_phy_init,
182 .owner = THIS_MODULE,
185 /**************************************************
186 * MDIO driver code
187 **************************************************/
189 static int bcm_ns_usb3_mdiodev_phy_write(struct bcm_ns_usb3 *usb3, u16 reg,
190 u16 value)
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;
204 struct resource res;
205 int err;
207 usb3 = devm_kzalloc(dev, sizeof(*usb3), GFP_KERNEL);
208 if (!usb3)
209 return -ENOMEM;
211 usb3->dev = dev;
212 usb3->mdiodev = mdiodev;
214 of_id = of_match_device(bcm_ns_usb3_id_table, dev);
215 if (!of_id)
216 return -EINVAL;
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);
222 if (err)
223 return err;
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 = {
247 .mdiodrv = {
248 .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;
264 u32 val;
266 do {
267 val = readl(addr);
268 if ((val & mask) == value)
269 return 0;
270 cpu_relax();
271 udelay(10);
272 } while (!time_after_eq(jiffies, deadline));
274 dev_err(usb3->dev, "Timeout waiting for register %p\n", addr);
276 return -EBUSY;
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,
282 0x0100, 0x0000,
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,
287 u16 value)
289 u32 tmp = 0;
290 int err;
292 err = bcm_ns_usb3_mii_mng_wait_idle(usb3);
293 if (err < 0) {
294 dev_err(usb3->dev, "Couldn't write 0x%08x value\n", value);
295 return err;
298 /* TODO: Use a proper MDIO bus layer */
299 tmp |= 0x58020000; /* Magic value for MDIO PHY write */
300 tmp |= reg << 18;
301 tmp |= value;
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);
316 if (!usb3)
317 return -ENOMEM;
319 usb3->dev = dev;
321 of_id = of_match_device(bcm_ns_usb3_id_table, dev);
322 if (!of_id)
323 return -EINVAL;
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);
343 /* Wait for MDIO? */
344 udelay(2);
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,
366 .driver = {
367 .name = "bcm_ns_usb3",
368 .of_match_table = bcm_ns_usb3_id_table,
372 static int __init bcm_ns_usb3_module_init(void)
374 int err;
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
380 * support for it.
383 err = mdio_driver_register(&bcm_ns_usb3_mdio_driver);
384 if (err)
385 return err;
387 err = platform_driver_register(&bcm_ns_usb3_driver);
388 if (err)
389 mdio_driver_unregister(&bcm_ns_usb3_mdio_driver);
391 return err;
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");