xtensa: support DMA buffers in high memory
[cris-mirror.git] / drivers / phy / broadcom / phy-bcm-ns-usb3.c
bloba53ae128eadfb410f5a83c91098e41e45d458fec
1 /*
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
8 * from Broadcom's SDK:
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
46 enum bcm_ns_family {
47 BCM_NS_UNKNOWN,
48 BCM_NS_AX,
49 BCM_NS_BX,
52 struct bcm_ns_usb3 {
53 struct device *dev;
54 enum bcm_ns_family family;
55 void __iomem *dmp;
56 void __iomem *ccb_mii;
57 struct mdio_device *mdiodev;
58 struct phy *phy;
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,
72 {},
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,
77 u16 value)
79 return usb3->phy_write(usb3, reg, value);
82 static int bcm_ns_usb3_phy_init_ns_bx(struct bcm_ns_usb3 *usb3)
84 int err;
86 /* USB3 PLL Block */
87 err = bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PHY_BASE_ADDR_REG,
88 BCM_NS_USB3_PHY_PLL30_BLOCK);
89 if (err < 0)
90 return err;
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);
110 /* PIPE Block */
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);
120 /* TXPMD block */
121 bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PHY_BASE_ADDR_REG,
122 BCM_NS_USB3_PHY_TX_PMD_BLOCK);
124 /* Enabling SSC */
125 bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_TX_PMD_CONTROL1, 0x1003);
127 return 0;
130 static int bcm_ns_usb3_phy_init_ns_ax(struct bcm_ns_usb3 *usb3)
132 int err;
134 /* PLL30 block */
135 err = bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PHY_BASE_ADDR_REG,
136 BCM_NS_USB3_PHY_PLL30_BLOCK);
137 if (err < 0)
138 return err;
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);
146 /* Enable SSC */
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);
157 return 0;
160 static int bcm_ns_usb3_phy_init(struct phy *phy)
162 struct bcm_ns_usb3 *usb3 = phy_get_drvdata(phy);
163 int err;
165 /* Perform USB3 system soft reset */
166 writel(BCMA_RESET_CTL_RESET, usb3->dmp + BCMA_RESET_CTL);
168 switch (usb3->family) {
169 case BCM_NS_AX:
170 err = bcm_ns_usb3_phy_init_ns_ax(usb3);
171 break;
172 case BCM_NS_BX:
173 err = bcm_ns_usb3_phy_init_ns_bx(usb3);
174 break;
175 default:
176 WARN_ON(1);
177 err = -ENOTSUPP;
180 return err;
183 static const struct phy_ops ops = {
184 .init = bcm_ns_usb3_phy_init,
185 .owner = THIS_MODULE,
188 /**************************************************
189 * MDIO driver code
190 **************************************************/
192 static int bcm_ns_usb3_mdiodev_phy_write(struct bcm_ns_usb3 *usb3, u16 reg,
193 u16 value)
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;
207 struct resource res;
208 int err;
210 usb3 = devm_kzalloc(dev, sizeof(*usb3), GFP_KERNEL);
211 if (!usb3)
212 return -ENOMEM;
214 usb3->dev = dev;
215 usb3->mdiodev = mdiodev;
217 of_id = of_match_device(bcm_ns_usb3_id_table, dev);
218 if (!of_id)
219 return -EINVAL;
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);
225 if (err)
226 return err;
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 = {
250 .mdiodrv = {
251 .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;
267 u32 val;
269 do {
270 val = readl(addr);
271 if ((val & mask) == value)
272 return 0;
273 cpu_relax();
274 udelay(10);
275 } while (!time_after_eq(jiffies, deadline));
277 dev_err(usb3->dev, "Timeout waiting for register %p\n", addr);
279 return -EBUSY;
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,
285 0x0100, 0x0000,
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,
290 u16 value)
292 u32 tmp = 0;
293 int err;
295 err = bcm_ns_usb3_mii_mng_wait_idle(usb3);
296 if (err < 0) {
297 dev_err(usb3->dev, "Couldn't write 0x%08x value\n", value);
298 return err;
301 /* TODO: Use a proper MDIO bus layer */
302 tmp |= 0x58020000; /* Magic value for MDIO PHY write */
303 tmp |= reg << 18;
304 tmp |= value;
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);
319 if (!usb3)
320 return -ENOMEM;
322 usb3->dev = dev;
324 of_id = of_match_device(bcm_ns_usb3_id_table, dev);
325 if (!of_id)
326 return -EINVAL;
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);
346 /* Wait for MDIO? */
347 udelay(2);
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,
369 .driver = {
370 .name = "bcm_ns_usb3",
371 .of_match_table = bcm_ns_usb3_id_table,
375 static int __init bcm_ns_usb3_module_init(void)
377 int err;
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
383 * support for it.
386 err = mdio_driver_register(&bcm_ns_usb3_mdio_driver);
387 if (err)
388 return err;
390 err = platform_driver_register(&bcm_ns_usb3_driver);
391 if (err)
392 mdio_driver_unregister(&bcm_ns_usb3_mdio_driver);
394 return err;
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");