1 /*
2  * Broadcom Northstar USB 3.0 PHY Driver
3  *
4  * Copyright (C) 2016 Rafał Miłecki <rafal@milecki.pl>
5  * Copyright (C) 2016 Broadcom
6  *
7  * All magic values used for initialization (and related comments) were obtained
8  * from Broadcom's SDK:
9  * Copyright (c) Broadcom Corp, 2012
10  *
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.
14  */
15 
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>
26 
27 #define BCM_NS_USB3_MII_MNG_TIMEOUT_US	1000	/* usecs */
28 
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
33 
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
38 
39 /* Registers of TX PMD block */
40 #define BCM_NS_USB3_TX_PMD_CONTROL1	0x01
41 
42 /* Registers of PIPE block */
43 #define BCM_NS_USB3_LFPS_CMP		0x02
44 #define BCM_NS_USB3_LFPS_DEGLITCH	0x03
45 
46 enum bcm_ns_family {
47 	BCM_NS_UNKNOWN,
48 	BCM_NS_AX,
49 	BCM_NS_BX,
50 };
51 
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;
59 
60 	int (*phy_write)(struct bcm_ns_usb3 *usb3, u16 reg, u16 value);
61 };
62 
63 static const struct of_device_id bcm_ns_usb3_id_table[] = {
64 	{
65 		.compatible = "brcm,ns-ax-usb3-phy",
66 		.data = (int *)BCM_NS_AX,
67 	},
68 	{
69 		.compatible = "brcm,ns-bx-usb3-phy",
70 		.data = (int *)BCM_NS_BX,
71 	},
72 	{},
73 };
74 MODULE_DEVICE_TABLE(of, bcm_ns_usb3_id_table);
75 
76 static int bcm_ns_usb3_mdio_phy_write(struct bcm_ns_usb3 *usb3, u16 reg,
77 				      u16 value)
78 {
79 	return usb3->phy_write(usb3, reg, value);
80 }
81 
82 static int bcm_ns_usb3_phy_init_ns_bx(struct bcm_ns_usb3 *usb3)
83 {
84 	int err;
85 
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;
91 
92 	/* Assert Ana_Pllseq start */
93 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PLL_CONTROL, 0x1000);
94 
95 	/* Assert CML Divider ratio to 26 */
96 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PLLA_CONTROL0, 0x6400);
97 
98 	/* Asserting PLL Reset */
99 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PLLA_CONTROL1, 0xc000);
100 
101 	/* Deaaserting PLL Reset */
102 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PLLA_CONTROL1, 0x8000);
103 
104 	/* Deasserting USB3 system reset */
105 	writel(0, usb3->dmp + BCMA_RESET_CTL);
106 
107 	/* PLL frequency monitor enable */
108 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PLL_CONTROL, 0x9000);
109 
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);
113 
114 	/* CMPMAX & CMPMINTH setting */
115 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_LFPS_CMP, 0xf30d);
116 
117 	/* DEGLITCH MIN & MAX setting */
118 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_LFPS_DEGLITCH, 0x6302);
119 
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);
123 
124 	/* Enabling SSC */
125 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_TX_PMD_CONTROL1, 0x1003);
126 
127 	return 0;
128 }
129 
130 static int bcm_ns_usb3_phy_init_ns_ax(struct bcm_ns_usb3 *usb3)
131 {
132 	int err;
133 
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;
139 
140 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PLLA_CONTROL0, 0x6400);
141 
142 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PHY_BASE_ADDR_REG, 0x80e0);
143 
144 	bcm_ns_usb3_mdio_phy_write(usb3, 0x02, 0x009c);
145 
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);
149 
150 	bcm_ns_usb3_mdio_phy_write(usb3, 0x02, 0x21d3);
151 
152 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_TX_PMD_CONTROL1, 0x1003);
153 
154 	/* Deasserting USB3 system reset */
155 	writel(0, usb3->dmp + BCMA_RESET_CTL);
156 
157 	return 0;
158 }
159 
160 static int bcm_ns_usb3_phy_init(struct phy *phy)
161 {
162 	struct bcm_ns_usb3 *usb3 = phy_get_drvdata(phy);
163 	int err;
164 
165 	/* Perform USB3 system soft reset */
166 	writel(BCMA_RESET_CTL_RESET, usb3->dmp + BCMA_RESET_CTL);
167 
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;
178 	}
179 
180 	return err;
181 }
182 
183 static const struct phy_ops ops = {
184 	.init		= bcm_ns_usb3_phy_init,
185 	.owner		= THIS_MODULE,
186 };
187 
188 /**************************************************
189  * MDIO driver code
190  **************************************************/
191 
192 static int bcm_ns_usb3_mdiodev_phy_write(struct bcm_ns_usb3 *usb3, u16 reg,
193 					 u16 value)
194 {
195 	struct mdio_device *mdiodev = usb3->mdiodev;
196 
197 	return mdiobus_write(mdiodev->bus, mdiodev->addr, reg, value);
198 }
199 
200 static int bcm_ns_usb3_mdio_probe(struct mdio_device *mdiodev)
201 {
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;
209 
210 	usb3 = devm_kzalloc(dev, sizeof(*usb3), GFP_KERNEL);
211 	if (!usb3)
212 		return -ENOMEM;
213 
214 	usb3->dev = dev;
215 	usb3->mdiodev = mdiodev;
216 
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;
221 
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;
227 
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);
232 	}
233 
234 	usb3->phy_write = bcm_ns_usb3_mdiodev_phy_write;
235 
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);
240 	}
241 
242 	phy_set_drvdata(usb3->phy, usb3);
243 
244 	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
245 
246 	return PTR_ERR_OR_ZERO(phy_provider);
247 }
248 
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,
254 		},
255 	},
256 	.probe = bcm_ns_usb3_mdio_probe,
257 };
258 
259 /**************************************************
260  * Platform driver code
261  **************************************************/
262 
263 static int bcm_ns_usb3_wait_reg(struct bcm_ns_usb3 *usb3, void __iomem *addr,
264 				u32 mask, u32 value, unsigned long timeout)
265 {
266 	unsigned long deadline = jiffies + timeout;
267 	u32 val;
268 
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));
276 
277 	dev_err(usb3->dev, "Timeout waiting for register %p\n", addr);
278 
279 	return -EBUSY;
280 }
281 
282 static inline int bcm_ns_usb3_mii_mng_wait_idle(struct bcm_ns_usb3 *usb3)
283 {
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));
287 }
288 
289 static int bcm_ns_usb3_platform_phy_write(struct bcm_ns_usb3 *usb3, u16 reg,
290 					  u16 value)
291 {
292 	u32 tmp = 0;
293 	int err;
294 
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;
299 	}
300 
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);
306 
307 	return bcm_ns_usb3_mii_mng_wait_idle(usb3);
308 }
309 
310 static int bcm_ns_usb3_probe(struct platform_device *pdev)
311 {
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;
317 
318 	usb3 = devm_kzalloc(dev, sizeof(*usb3), GFP_KERNEL);
319 	if (!usb3)
320 		return -ENOMEM;
321 
322 	usb3->dev = dev;
323 
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;
328 
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);
334 	}
335 
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);
341 	}
342 
343 	/* Enable MDIO. Setting MDCDIV as 26  */
344 	writel(0x0000009a, usb3->ccb_mii + BCMA_CCB_MII_MNG_CTL);
345 
346 	/* Wait for MDIO? */
347 	udelay(2);
348 
349 	usb3->phy_write = bcm_ns_usb3_platform_phy_write;
350 
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);
355 	}
356 
357 	phy_set_drvdata(usb3->phy, usb3);
358 	platform_set_drvdata(pdev, usb3);
359 
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");
363 
364 	return PTR_ERR_OR_ZERO(phy_provider);
365 }
366 
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,
372 	},
373 };
374 
375 static int __init bcm_ns_usb3_module_init(void)
376 {
377 	int err;
378 
379 	/*
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.
384 	 */
385 
386 	err = mdio_driver_register(&bcm_ns_usb3_mdio_driver);
387 	if (err)
388 		return err;
389 
390 	err = platform_driver_register(&bcm_ns_usb3_driver);
391 	if (err)
392 		mdio_driver_unregister(&bcm_ns_usb3_mdio_driver);
393 
394 	return err;
395 }
396 module_init(bcm_ns_usb3_module_init);
397 
398 static void __exit bcm_ns_usb3_module_exit(void)
399 {
400 	platform_driver_unregister(&bcm_ns_usb3_driver);
401 	mdio_driver_unregister(&bcm_ns_usb3_mdio_driver);
402 }
403 module_exit(bcm_ns_usb3_module_exit)
404 
405 MODULE_LICENSE("GPL v2");
406