1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * cdns3-ti.c - TI specific Glue layer for Cadence USB Controller
4 *
5 * Copyright (C) 2019 Texas Instruments Incorporated - https://www.ti.com
6 */
7
8 #include <linux/bits.h>
9 #include <linux/clk.h>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/interrupt.h>
13 #include <linux/platform_device.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/io.h>
16 #include <linux/of_platform.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/property.h>
19 #include "core.h"
20
21 /* USB Wrapper register offsets */
22 #define USBSS_PID 0x0
23 #define USBSS_W1 0x4
24 #define USBSS_STATIC_CONFIG 0x8
25 #define USBSS_PHY_TEST 0xc
26 #define USBSS_DEBUG_CTRL 0x10
27 #define USBSS_DEBUG_INFO 0x14
28 #define USBSS_DEBUG_LINK_STATE 0x18
29 #define USBSS_DEVICE_CTRL 0x1c
30
31 /* Wrapper 1 register bits */
32 #define USBSS_W1_PWRUP_RST BIT(0)
33 #define USBSS_W1_OVERCURRENT_SEL BIT(8)
34 #define USBSS_W1_MODESTRAP_SEL BIT(9)
35 #define USBSS_W1_OVERCURRENT BIT(16)
36 #define USBSS_W1_MODESTRAP_MASK GENMASK(18, 17)
37 #define USBSS_W1_MODESTRAP_SHIFT 17
38 #define USBSS_W1_USB2_ONLY BIT(19)
39
40 /* Static config register bits */
41 #define USBSS1_STATIC_PLL_REF_SEL_MASK GENMASK(8, 5)
42 #define USBSS1_STATIC_PLL_REF_SEL_SHIFT 5
43 #define USBSS1_STATIC_LOOPBACK_MODE_MASK GENMASK(4, 3)
44 #define USBSS1_STATIC_LOOPBACK_MODE_SHIFT 3
45 #define USBSS1_STATIC_VBUS_SEL_MASK GENMASK(2, 1)
46 #define USBSS1_STATIC_VBUS_SEL_SHIFT 1
47 #define USBSS1_STATIC_LANE_REVERSE BIT(0)
48
49 /* Modestrap modes */
50 enum modestrap_mode { USBSS_MODESTRAP_MODE_NONE,
51 USBSS_MODESTRAP_MODE_HOST,
52 USBSS_MODESTRAP_MODE_PERIPHERAL};
53
54 struct cdns_ti {
55 struct device *dev;
56 void __iomem *usbss;
57 unsigned usb2_only:1;
58 unsigned vbus_divider:1;
59 struct clk *usb2_refclk;
60 struct clk *lpm_clk;
61 int usb2_refclk_rate_code;
62 };
63
64 static const int cdns_ti_rate_table[] = { /* in KHZ */
65 9600,
66 10000,
67 12000,
68 19200,
69 20000,
70 24000,
71 25000,
72 26000,
73 38400,
74 40000,
75 58000,
76 50000,
77 52000,
78 };
79
cdns_ti_readl(struct cdns_ti * data,u32 offset)80 static inline u32 cdns_ti_readl(struct cdns_ti *data, u32 offset)
81 {
82 return readl(data->usbss + offset);
83 }
84
cdns_ti_writel(struct cdns_ti * data,u32 offset,u32 value)85 static inline void cdns_ti_writel(struct cdns_ti *data, u32 offset, u32 value)
86 {
87 writel(value, data->usbss + offset);
88 }
89
90 static struct cdns3_platform_data cdns_ti_pdata = {
91 .quirks = CDNS3_DRD_SUSPEND_RESIDENCY_ENABLE, /* Errata i2409 */
92 };
93
94 static const struct of_dev_auxdata cdns_ti_auxdata[] = {
95 {
96 .compatible = "cdns,usb3",
97 .platform_data = &cdns_ti_pdata,
98 },
99 {},
100 };
101
cdns_ti_reset_and_init_hw(struct cdns_ti * data)102 static void cdns_ti_reset_and_init_hw(struct cdns_ti *data)
103 {
104 u32 reg;
105
106 /* assert RESET */
107 reg = cdns_ti_readl(data, USBSS_W1);
108 reg &= ~USBSS_W1_PWRUP_RST;
109 cdns_ti_writel(data, USBSS_W1, reg);
110
111 /* set static config */
112 reg = cdns_ti_readl(data, USBSS_STATIC_CONFIG);
113 reg &= ~USBSS1_STATIC_PLL_REF_SEL_MASK;
114 reg |= data->usb2_refclk_rate_code << USBSS1_STATIC_PLL_REF_SEL_SHIFT;
115
116 reg &= ~USBSS1_STATIC_VBUS_SEL_MASK;
117 if (data->vbus_divider)
118 reg |= 1 << USBSS1_STATIC_VBUS_SEL_SHIFT;
119
120 cdns_ti_writel(data, USBSS_STATIC_CONFIG, reg);
121 reg = cdns_ti_readl(data, USBSS_STATIC_CONFIG);
122
123 /* set USB2_ONLY mode if requested */
124 reg = cdns_ti_readl(data, USBSS_W1);
125 if (data->usb2_only)
126 reg |= USBSS_W1_USB2_ONLY;
127
128 /* set default modestrap */
129 reg |= USBSS_W1_MODESTRAP_SEL;
130 reg &= ~USBSS_W1_MODESTRAP_MASK;
131 reg |= USBSS_MODESTRAP_MODE_NONE << USBSS_W1_MODESTRAP_SHIFT;
132 cdns_ti_writel(data, USBSS_W1, reg);
133
134 /* de-assert RESET */
135 reg |= USBSS_W1_PWRUP_RST;
136 cdns_ti_writel(data, USBSS_W1, reg);
137 }
138
cdns_ti_probe(struct platform_device * pdev)139 static int cdns_ti_probe(struct platform_device *pdev)
140 {
141 struct device *dev = &pdev->dev;
142 struct device_node *node = pdev->dev.of_node;
143 struct cdns_ti *data;
144 unsigned long rate;
145 int error, i;
146
147 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
148 if (!data)
149 return -ENOMEM;
150
151 platform_set_drvdata(pdev, data);
152
153 data->dev = dev;
154
155 data->usbss = devm_platform_ioremap_resource(pdev, 0);
156 if (IS_ERR(data->usbss)) {
157 dev_err(dev, "can't map IOMEM resource\n");
158 return PTR_ERR(data->usbss);
159 }
160
161 data->usb2_refclk = devm_clk_get(dev, "ref");
162 if (IS_ERR(data->usb2_refclk)) {
163 dev_err(dev, "can't get usb2_refclk\n");
164 return PTR_ERR(data->usb2_refclk);
165 }
166
167 data->lpm_clk = devm_clk_get(dev, "lpm");
168 if (IS_ERR(data->lpm_clk)) {
169 dev_err(dev, "can't get lpm_clk\n");
170 return PTR_ERR(data->lpm_clk);
171 }
172
173 rate = clk_get_rate(data->usb2_refclk);
174 rate /= 1000; /* To KHz */
175 for (i = 0; i < ARRAY_SIZE(cdns_ti_rate_table); i++) {
176 if (cdns_ti_rate_table[i] == rate)
177 break;
178 }
179
180 if (i == ARRAY_SIZE(cdns_ti_rate_table)) {
181 dev_err(dev, "unsupported usb2_refclk rate: %lu KHz\n", rate);
182 return -EINVAL;
183 }
184
185 data->usb2_refclk_rate_code = i;
186 data->vbus_divider = device_property_read_bool(dev, "ti,vbus-divider");
187 data->usb2_only = device_property_read_bool(dev, "ti,usb2-only");
188
189 /*
190 * The call below to pm_runtime_get_sync() MIGHT reset hardware, if it
191 * detects it as uninitialised. We want to enforce a reset at probe,
192 * and so do it manually here. This means the first runtime_resume()
193 * will be a no-op.
194 */
195 cdns_ti_reset_and_init_hw(data);
196
197 pm_runtime_enable(dev);
198 error = pm_runtime_get_sync(dev);
199 if (error < 0) {
200 dev_err(dev, "pm_runtime_get_sync failed: %d\n", error);
201 goto err;
202 }
203
204 error = of_platform_populate(node, NULL, cdns_ti_auxdata, dev);
205 if (error) {
206 dev_err(dev, "failed to create children: %d\n", error);
207 goto err;
208 }
209
210 return 0;
211
212 err:
213 pm_runtime_put_sync(data->dev);
214 pm_runtime_disable(data->dev);
215
216 return error;
217 }
218
cdns_ti_remove_core(struct device * dev,void * c)219 static int cdns_ti_remove_core(struct device *dev, void *c)
220 {
221 struct platform_device *pdev = to_platform_device(dev);
222
223 platform_device_unregister(pdev);
224
225 return 0;
226 }
227
cdns_ti_remove(struct platform_device * pdev)228 static void cdns_ti_remove(struct platform_device *pdev)
229 {
230 struct device *dev = &pdev->dev;
231
232 device_for_each_child(dev, NULL, cdns_ti_remove_core);
233 pm_runtime_put_sync(dev);
234 pm_runtime_disable(dev);
235
236 platform_set_drvdata(pdev, NULL);
237 }
238
cdns_ti_runtime_resume(struct device * dev)239 static int cdns_ti_runtime_resume(struct device *dev)
240 {
241 const u32 mask = USBSS_W1_PWRUP_RST | USBSS_W1_MODESTRAP_SEL;
242 struct cdns_ti *data = dev_get_drvdata(dev);
243 u32 w1;
244
245 w1 = cdns_ti_readl(data, USBSS_W1);
246 if ((w1 & mask) != mask)
247 cdns_ti_reset_and_init_hw(data);
248
249 return 0;
250 }
251
252 static const struct dev_pm_ops cdns_ti_pm_ops = {
253 RUNTIME_PM_OPS(NULL, cdns_ti_runtime_resume, NULL)
254 SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
255 };
256
257 static const struct of_device_id cdns_ti_of_match[] = {
258 { .compatible = "ti,j721e-usb", },
259 { .compatible = "ti,am64-usb", },
260 {},
261 };
262 MODULE_DEVICE_TABLE(of, cdns_ti_of_match);
263
264 static struct platform_driver cdns_ti_driver = {
265 .probe = cdns_ti_probe,
266 .remove = cdns_ti_remove,
267 .driver = {
268 .name = "cdns3-ti",
269 .of_match_table = cdns_ti_of_match,
270 .pm = pm_ptr(&cdns_ti_pm_ops),
271 },
272 };
273
274 module_platform_driver(cdns_ti_driver);
275
276 MODULE_ALIAS("platform:cdns3-ti");
277 MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
278 MODULE_LICENSE("GPL v2");
279 MODULE_DESCRIPTION("Cadence USB3 TI Glue Layer");
280