xref: /linux/drivers/phy/broadcom/phy-bcm-ns-usb3.c (revision 63af040b0b4067d75e59df9fa093f3f5dbc5899e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Broadcom Northstar USB 3.0 PHY Driver
4  *
5  * Copyright (C) 2016 Rafał Miłecki <rafal@milecki.pl>
6  * Copyright (C) 2016 Broadcom
7  *
8  * All magic values used for initialization (and related comments) were obtained
9  * from Broadcom's SDK:
10  * Copyright (c) Broadcom Corp, 2012
11  */
12 
13 #include <linux/bcma/bcma.h>
14 #include <linux/delay.h>
15 #include <linux/err.h>
16 #include <linux/iopoll.h>
17 #include <linux/mdio.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/of_address.h>
21 #include <linux/platform_device.h>
22 #include <linux/phy/phy.h>
23 #include <linux/property.h>
24 #include <linux/slab.h>
25 
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
30 
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
35 
36 /* Registers of TX PMD block */
37 #define BCM_NS_USB3_TX_PMD_CONTROL1	0x01
38 
39 /* Registers of PIPE block */
40 #define BCM_NS_USB3_LFPS_CMP		0x02
41 #define BCM_NS_USB3_LFPS_DEGLITCH	0x03
42 
43 enum bcm_ns_family {
44 	BCM_NS_UNKNOWN,
45 	BCM_NS_AX,
46 	BCM_NS_BX,
47 };
48 
49 struct bcm_ns_usb3 {
50 	struct device *dev;
51 	enum bcm_ns_family family;
52 	void __iomem *dmp;
53 	struct mdio_device *mdiodev;
54 	struct phy *phy;
55 };
56 
57 static const struct of_device_id bcm_ns_usb3_id_table[] = {
58 	{
59 		.compatible = "brcm,ns-ax-usb3-phy",
60 		.data = (int *)BCM_NS_AX,
61 	},
62 	{
63 		.compatible = "brcm,ns-bx-usb3-phy",
64 		.data = (int *)BCM_NS_BX,
65 	},
66 	{},
67 };
68 MODULE_DEVICE_TABLE(of, bcm_ns_usb3_id_table);
69 
70 static int bcm_ns_usb3_mdio_phy_write(struct bcm_ns_usb3 *usb3, u16 reg,
71 				      u16 value);
72 
73 static int bcm_ns_usb3_phy_init_ns_bx(struct bcm_ns_usb3 *usb3)
74 {
75 	int err;
76 
77 	/* USB3 PLL Block */
78 	err = bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PHY_BASE_ADDR_REG,
79 					 BCM_NS_USB3_PHY_PLL30_BLOCK);
80 	if (err < 0)
81 		return err;
82 
83 	/* Assert Ana_Pllseq start */
84 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PLL_CONTROL, 0x1000);
85 
86 	/* Assert CML Divider ratio to 26 */
87 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PLLA_CONTROL0, 0x6400);
88 
89 	/* Asserting PLL Reset */
90 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PLLA_CONTROL1, 0xc000);
91 
92 	/* Deaaserting PLL Reset */
93 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PLLA_CONTROL1, 0x8000);
94 
95 	/* Deasserting USB3 system reset */
96 	writel(0, usb3->dmp + BCMA_RESET_CTL);
97 
98 	/* PLL frequency monitor enable */
99 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PLL_CONTROL, 0x9000);
100 
101 	/* PIPE Block */
102 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PHY_BASE_ADDR_REG,
103 				   BCM_NS_USB3_PHY_PIPE_BLOCK);
104 
105 	/* CMPMAX & CMPMINTH setting */
106 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_LFPS_CMP, 0xf30d);
107 
108 	/* DEGLITCH MIN & MAX setting */
109 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_LFPS_DEGLITCH, 0x6302);
110 
111 	/* TXPMD block */
112 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PHY_BASE_ADDR_REG,
113 				   BCM_NS_USB3_PHY_TX_PMD_BLOCK);
114 
115 	/* Enabling SSC */
116 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_TX_PMD_CONTROL1, 0x1003);
117 
118 	return 0;
119 }
120 
121 static int bcm_ns_usb3_phy_init_ns_ax(struct bcm_ns_usb3 *usb3)
122 {
123 	int err;
124 
125 	/* PLL30 block */
126 	err = bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PHY_BASE_ADDR_REG,
127 					 BCM_NS_USB3_PHY_PLL30_BLOCK);
128 	if (err < 0)
129 		return err;
130 
131 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PLLA_CONTROL0, 0x6400);
132 
133 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PHY_BASE_ADDR_REG, 0x80e0);
134 
135 	bcm_ns_usb3_mdio_phy_write(usb3, 0x02, 0x009c);
136 
137 	/* Enable SSC */
138 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_PHY_BASE_ADDR_REG,
139 				   BCM_NS_USB3_PHY_TX_PMD_BLOCK);
140 
141 	bcm_ns_usb3_mdio_phy_write(usb3, 0x02, 0x21d3);
142 
143 	bcm_ns_usb3_mdio_phy_write(usb3, BCM_NS_USB3_TX_PMD_CONTROL1, 0x1003);
144 
145 	/* Deasserting USB3 system reset */
146 	writel(0, usb3->dmp + BCMA_RESET_CTL);
147 
148 	return 0;
149 }
150 
151 static int bcm_ns_usb3_phy_init(struct phy *phy)
152 {
153 	struct bcm_ns_usb3 *usb3 = phy_get_drvdata(phy);
154 	int err;
155 
156 	/* Perform USB3 system soft reset */
157 	writel(BCMA_RESET_CTL_RESET, usb3->dmp + BCMA_RESET_CTL);
158 
159 	switch (usb3->family) {
160 	case BCM_NS_AX:
161 		err = bcm_ns_usb3_phy_init_ns_ax(usb3);
162 		break;
163 	case BCM_NS_BX:
164 		err = bcm_ns_usb3_phy_init_ns_bx(usb3);
165 		break;
166 	default:
167 		WARN_ON(1);
168 		err = -ENOTSUPP;
169 	}
170 
171 	return err;
172 }
173 
174 static const struct phy_ops ops = {
175 	.init		= bcm_ns_usb3_phy_init,
176 	.owner		= THIS_MODULE,
177 };
178 
179 /**************************************************
180  * MDIO driver code
181  **************************************************/
182 
183 static int bcm_ns_usb3_mdio_phy_write(struct bcm_ns_usb3 *usb3, u16 reg,
184 				      u16 value)
185 {
186 	struct mdio_device *mdiodev = usb3->mdiodev;
187 
188 	return mdiodev_write(mdiodev, reg, value);
189 }
190 
191 static int bcm_ns_usb3_mdio_probe(struct mdio_device *mdiodev)
192 {
193 	struct device *dev = &mdiodev->dev;
194 	struct phy_provider *phy_provider;
195 	struct device_node *syscon_np;
196 	struct bcm_ns_usb3 *usb3;
197 	struct resource res;
198 	int err;
199 
200 	usb3 = devm_kzalloc(dev, sizeof(*usb3), GFP_KERNEL);
201 	if (!usb3)
202 		return -ENOMEM;
203 
204 	usb3->dev = dev;
205 	usb3->mdiodev = mdiodev;
206 
207 	usb3->family = (unsigned long)device_get_match_data(dev);
208 
209 	syscon_np = of_parse_phandle(dev->of_node, "usb3-dmp-syscon", 0);
210 	err = of_address_to_resource(syscon_np, 0, &res);
211 	of_node_put(syscon_np);
212 	if (err)
213 		return err;
214 
215 	usb3->dmp = devm_ioremap_resource(dev, &res);
216 	if (IS_ERR(usb3->dmp))
217 		return PTR_ERR(usb3->dmp);
218 
219 	usb3->phy = devm_phy_create(dev, NULL, &ops);
220 	if (IS_ERR(usb3->phy)) {
221 		dev_err(dev, "Failed to create PHY\n");
222 		return PTR_ERR(usb3->phy);
223 	}
224 
225 	phy_set_drvdata(usb3->phy, usb3);
226 
227 	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
228 
229 	return PTR_ERR_OR_ZERO(phy_provider);
230 }
231 
232 static struct mdio_driver bcm_ns_usb3_mdio_driver = {
233 	.mdiodrv = {
234 		.driver = {
235 			.name = "bcm_ns_mdio_usb3",
236 			.of_match_table = bcm_ns_usb3_id_table,
237 		},
238 	},
239 	.probe = bcm_ns_usb3_mdio_probe,
240 };
241 
242 mdio_module_driver(bcm_ns_usb3_mdio_driver);
243 
244 MODULE_DESCRIPTION("Broadcom Northstar USB 3.0 PHY Driver");
245 MODULE_LICENSE("GPL v2");
246