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 };
62
63 static const int cdns_ti_rate_table[] = { /* in KHZ */
64 9600,
65 10000,
66 12000,
67 19200,
68 20000,
69 24000,
70 25000,
71 26000,
72 38400,
73 40000,
74 58000,
75 50000,
76 52000,
77 };
78
cdns_ti_readl(struct cdns_ti * data,u32 offset)79 static inline u32 cdns_ti_readl(struct cdns_ti *data, u32 offset)
80 {
81 return readl(data->usbss + offset);
82 }
83
cdns_ti_writel(struct cdns_ti * data,u32 offset,u32 value)84 static inline void cdns_ti_writel(struct cdns_ti *data, u32 offset, u32 value)
85 {
86 writel(value, data->usbss + offset);
87 }
88
89 static struct cdns3_platform_data cdns_ti_pdata = {
90 .quirks = CDNS3_DRD_SUSPEND_RESIDENCY_ENABLE, /* Errata i2409 */
91 };
92
93 static const struct of_dev_auxdata cdns_ti_auxdata[] = {
94 {
95 .compatible = "cdns,usb3",
96 .platform_data = &cdns_ti_pdata,
97 },
98 {},
99 };
100
cdns_ti_probe(struct platform_device * pdev)101 static int cdns_ti_probe(struct platform_device *pdev)
102 {
103 struct device *dev = &pdev->dev;
104 struct device_node *node = pdev->dev.of_node;
105 struct cdns_ti *data;
106 int error;
107 u32 reg;
108 int rate_code, i;
109 unsigned long rate;
110
111 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
112 if (!data)
113 return -ENOMEM;
114
115 platform_set_drvdata(pdev, data);
116
117 data->dev = dev;
118
119 data->usbss = devm_platform_ioremap_resource(pdev, 0);
120 if (IS_ERR(data->usbss)) {
121 dev_err(dev, "can't map IOMEM resource\n");
122 return PTR_ERR(data->usbss);
123 }
124
125 data->usb2_refclk = devm_clk_get(dev, "ref");
126 if (IS_ERR(data->usb2_refclk)) {
127 dev_err(dev, "can't get usb2_refclk\n");
128 return PTR_ERR(data->usb2_refclk);
129 }
130
131 data->lpm_clk = devm_clk_get(dev, "lpm");
132 if (IS_ERR(data->lpm_clk)) {
133 dev_err(dev, "can't get lpm_clk\n");
134 return PTR_ERR(data->lpm_clk);
135 }
136
137 rate = clk_get_rate(data->usb2_refclk);
138 rate /= 1000; /* To KHz */
139 for (i = 0; i < ARRAY_SIZE(cdns_ti_rate_table); i++) {
140 if (cdns_ti_rate_table[i] == rate)
141 break;
142 }
143
144 if (i == ARRAY_SIZE(cdns_ti_rate_table)) {
145 dev_err(dev, "unsupported usb2_refclk rate: %lu KHz\n", rate);
146 return -EINVAL;
147 }
148
149 rate_code = i;
150
151 pm_runtime_enable(dev);
152 error = pm_runtime_get_sync(dev);
153 if (error < 0) {
154 dev_err(dev, "pm_runtime_get_sync failed: %d\n", error);
155 goto err;
156 }
157
158 /* assert RESET */
159 reg = cdns_ti_readl(data, USBSS_W1);
160 reg &= ~USBSS_W1_PWRUP_RST;
161 cdns_ti_writel(data, USBSS_W1, reg);
162
163 /* set static config */
164 reg = cdns_ti_readl(data, USBSS_STATIC_CONFIG);
165 reg &= ~USBSS1_STATIC_PLL_REF_SEL_MASK;
166 reg |= rate_code << USBSS1_STATIC_PLL_REF_SEL_SHIFT;
167
168 reg &= ~USBSS1_STATIC_VBUS_SEL_MASK;
169 data->vbus_divider = device_property_read_bool(dev, "ti,vbus-divider");
170 if (data->vbus_divider)
171 reg |= 1 << USBSS1_STATIC_VBUS_SEL_SHIFT;
172
173 cdns_ti_writel(data, USBSS_STATIC_CONFIG, reg);
174 reg = cdns_ti_readl(data, USBSS_STATIC_CONFIG);
175
176 /* set USB2_ONLY mode if requested */
177 reg = cdns_ti_readl(data, USBSS_W1);
178 data->usb2_only = device_property_read_bool(dev, "ti,usb2-only");
179 if (data->usb2_only)
180 reg |= USBSS_W1_USB2_ONLY;
181
182 /* set default modestrap */
183 reg |= USBSS_W1_MODESTRAP_SEL;
184 reg &= ~USBSS_W1_MODESTRAP_MASK;
185 reg |= USBSS_MODESTRAP_MODE_NONE << USBSS_W1_MODESTRAP_SHIFT;
186 cdns_ti_writel(data, USBSS_W1, reg);
187
188 /* de-assert RESET */
189 reg |= USBSS_W1_PWRUP_RST;
190 cdns_ti_writel(data, USBSS_W1, reg);
191
192 error = of_platform_populate(node, NULL, cdns_ti_auxdata, dev);
193 if (error) {
194 dev_err(dev, "failed to create children: %d\n", error);
195 goto err;
196 }
197
198 return 0;
199
200 err:
201 pm_runtime_put_sync(data->dev);
202 pm_runtime_disable(data->dev);
203
204 return error;
205 }
206
cdns_ti_remove_core(struct device * dev,void * c)207 static int cdns_ti_remove_core(struct device *dev, void *c)
208 {
209 struct platform_device *pdev = to_platform_device(dev);
210
211 platform_device_unregister(pdev);
212
213 return 0;
214 }
215
cdns_ti_remove(struct platform_device * pdev)216 static void cdns_ti_remove(struct platform_device *pdev)
217 {
218 struct device *dev = &pdev->dev;
219
220 device_for_each_child(dev, NULL, cdns_ti_remove_core);
221 pm_runtime_put_sync(dev);
222 pm_runtime_disable(dev);
223
224 platform_set_drvdata(pdev, NULL);
225 }
226
227 static const struct of_device_id cdns_ti_of_match[] = {
228 { .compatible = "ti,j721e-usb", },
229 { .compatible = "ti,am64-usb", },
230 {},
231 };
232 MODULE_DEVICE_TABLE(of, cdns_ti_of_match);
233
234 static struct platform_driver cdns_ti_driver = {
235 .probe = cdns_ti_probe,
236 .remove_new = cdns_ti_remove,
237 .driver = {
238 .name = "cdns3-ti",
239 .of_match_table = cdns_ti_of_match,
240 },
241 };
242
243 module_platform_driver(cdns_ti_driver);
244
245 MODULE_ALIAS("platform:cdns3-ti");
246 MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
247 MODULE_LICENSE("GPL v2");
248 MODULE_DESCRIPTION("Cadence USB3 TI Glue Layer");
249