xref: /linux/drivers/usb/cdns3/cdns3-ti.c (revision be239684b18e1cdcafcf8c7face4a2f562c745ad)
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 
20 /* USB Wrapper register offsets */
21 #define USBSS_PID		0x0
22 #define	USBSS_W1		0x4
23 #define USBSS_STATIC_CONFIG	0x8
24 #define USBSS_PHY_TEST		0xc
25 #define	USBSS_DEBUG_CTRL	0x10
26 #define	USBSS_DEBUG_INFO	0x14
27 #define	USBSS_DEBUG_LINK_STATE	0x18
28 #define	USBSS_DEVICE_CTRL	0x1c
29 
30 /* Wrapper 1 register bits */
31 #define USBSS_W1_PWRUP_RST		BIT(0)
32 #define USBSS_W1_OVERCURRENT_SEL	BIT(8)
33 #define USBSS_W1_MODESTRAP_SEL		BIT(9)
34 #define USBSS_W1_OVERCURRENT		BIT(16)
35 #define USBSS_W1_MODESTRAP_MASK		GENMASK(18, 17)
36 #define USBSS_W1_MODESTRAP_SHIFT	17
37 #define USBSS_W1_USB2_ONLY		BIT(19)
38 
39 /* Static config register bits */
40 #define USBSS1_STATIC_PLL_REF_SEL_MASK	GENMASK(8, 5)
41 #define USBSS1_STATIC_PLL_REF_SEL_SHIFT	5
42 #define USBSS1_STATIC_LOOPBACK_MODE_MASK	GENMASK(4, 3)
43 #define USBSS1_STATIC_LOOPBACK_MODE_SHIFT	3
44 #define USBSS1_STATIC_VBUS_SEL_MASK	GENMASK(2, 1)
45 #define USBSS1_STATIC_VBUS_SEL_SHIFT	1
46 #define USBSS1_STATIC_LANE_REVERSE	BIT(0)
47 
48 /* Modestrap modes */
49 enum modestrap_mode { USBSS_MODESTRAP_MODE_NONE,
50 		      USBSS_MODESTRAP_MODE_HOST,
51 		      USBSS_MODESTRAP_MODE_PERIPHERAL};
52 
53 struct cdns_ti {
54 	struct device *dev;
55 	void __iomem *usbss;
56 	unsigned usb2_only:1;
57 	unsigned vbus_divider:1;
58 	struct clk *usb2_refclk;
59 	struct clk *lpm_clk;
60 };
61 
62 static const int cdns_ti_rate_table[] = {	/* in KHZ */
63 	9600,
64 	10000,
65 	12000,
66 	19200,
67 	20000,
68 	24000,
69 	25000,
70 	26000,
71 	38400,
72 	40000,
73 	58000,
74 	50000,
75 	52000,
76 };
77 
78 static inline u32 cdns_ti_readl(struct cdns_ti *data, u32 offset)
79 {
80 	return readl(data->usbss + offset);
81 }
82 
83 static inline void cdns_ti_writel(struct cdns_ti *data, u32 offset, u32 value)
84 {
85 	writel(value, data->usbss + offset);
86 }
87 
88 static int cdns_ti_probe(struct platform_device *pdev)
89 {
90 	struct device *dev = &pdev->dev;
91 	struct device_node *node = pdev->dev.of_node;
92 	struct cdns_ti *data;
93 	int error;
94 	u32 reg;
95 	int rate_code, i;
96 	unsigned long rate;
97 
98 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
99 	if (!data)
100 		return -ENOMEM;
101 
102 	platform_set_drvdata(pdev, data);
103 
104 	data->dev = dev;
105 
106 	data->usbss = devm_platform_ioremap_resource(pdev, 0);
107 	if (IS_ERR(data->usbss)) {
108 		dev_err(dev, "can't map IOMEM resource\n");
109 		return PTR_ERR(data->usbss);
110 	}
111 
112 	data->usb2_refclk = devm_clk_get(dev, "ref");
113 	if (IS_ERR(data->usb2_refclk)) {
114 		dev_err(dev, "can't get usb2_refclk\n");
115 		return PTR_ERR(data->usb2_refclk);
116 	}
117 
118 	data->lpm_clk = devm_clk_get(dev, "lpm");
119 	if (IS_ERR(data->lpm_clk)) {
120 		dev_err(dev, "can't get lpm_clk\n");
121 		return PTR_ERR(data->lpm_clk);
122 	}
123 
124 	rate = clk_get_rate(data->usb2_refclk);
125 	rate /= 1000;	/* To KHz */
126 	for (i = 0; i < ARRAY_SIZE(cdns_ti_rate_table); i++) {
127 		if (cdns_ti_rate_table[i] == rate)
128 			break;
129 	}
130 
131 	if (i == ARRAY_SIZE(cdns_ti_rate_table)) {
132 		dev_err(dev, "unsupported usb2_refclk rate: %lu KHz\n", rate);
133 		return -EINVAL;
134 	}
135 
136 	rate_code = i;
137 
138 	pm_runtime_enable(dev);
139 	error = pm_runtime_get_sync(dev);
140 	if (error < 0) {
141 		dev_err(dev, "pm_runtime_get_sync failed: %d\n", error);
142 		goto err;
143 	}
144 
145 	/* assert RESET */
146 	reg = cdns_ti_readl(data, USBSS_W1);
147 	reg &= ~USBSS_W1_PWRUP_RST;
148 	cdns_ti_writel(data, USBSS_W1, reg);
149 
150 	/* set static config */
151 	reg = cdns_ti_readl(data, USBSS_STATIC_CONFIG);
152 	reg &= ~USBSS1_STATIC_PLL_REF_SEL_MASK;
153 	reg |= rate_code << USBSS1_STATIC_PLL_REF_SEL_SHIFT;
154 
155 	reg &= ~USBSS1_STATIC_VBUS_SEL_MASK;
156 	data->vbus_divider = device_property_read_bool(dev, "ti,vbus-divider");
157 	if (data->vbus_divider)
158 		reg |= 1 << USBSS1_STATIC_VBUS_SEL_SHIFT;
159 
160 	cdns_ti_writel(data, USBSS_STATIC_CONFIG, reg);
161 	reg = cdns_ti_readl(data, USBSS_STATIC_CONFIG);
162 
163 	/* set USB2_ONLY mode if requested */
164 	reg = cdns_ti_readl(data, USBSS_W1);
165 	data->usb2_only = device_property_read_bool(dev, "ti,usb2-only");
166 	if (data->usb2_only)
167 		reg |= USBSS_W1_USB2_ONLY;
168 
169 	/* set default modestrap */
170 	reg |= USBSS_W1_MODESTRAP_SEL;
171 	reg &= ~USBSS_W1_MODESTRAP_MASK;
172 	reg |= USBSS_MODESTRAP_MODE_NONE << USBSS_W1_MODESTRAP_SHIFT;
173 	cdns_ti_writel(data, USBSS_W1, reg);
174 
175 	/* de-assert RESET */
176 	reg |= USBSS_W1_PWRUP_RST;
177 	cdns_ti_writel(data, USBSS_W1, reg);
178 
179 	error = of_platform_populate(node, NULL, NULL, dev);
180 	if (error) {
181 		dev_err(dev, "failed to create children: %d\n", error);
182 		goto err;
183 	}
184 
185 	return 0;
186 
187 err:
188 	pm_runtime_put_sync(data->dev);
189 	pm_runtime_disable(data->dev);
190 
191 	return error;
192 }
193 
194 static int cdns_ti_remove_core(struct device *dev, void *c)
195 {
196 	struct platform_device *pdev = to_platform_device(dev);
197 
198 	platform_device_unregister(pdev);
199 
200 	return 0;
201 }
202 
203 static void cdns_ti_remove(struct platform_device *pdev)
204 {
205 	struct device *dev = &pdev->dev;
206 
207 	device_for_each_child(dev, NULL, cdns_ti_remove_core);
208 	pm_runtime_put_sync(dev);
209 	pm_runtime_disable(dev);
210 
211 	platform_set_drvdata(pdev, NULL);
212 }
213 
214 static const struct of_device_id cdns_ti_of_match[] = {
215 	{ .compatible = "ti,j721e-usb", },
216 	{ .compatible = "ti,am64-usb", },
217 	{},
218 };
219 MODULE_DEVICE_TABLE(of, cdns_ti_of_match);
220 
221 static struct platform_driver cdns_ti_driver = {
222 	.probe		= cdns_ti_probe,
223 	.remove_new	= cdns_ti_remove,
224 	.driver		= {
225 		.name	= "cdns3-ti",
226 		.of_match_table	= cdns_ti_of_match,
227 	},
228 };
229 
230 module_platform_driver(cdns_ti_driver);
231 
232 MODULE_ALIAS("platform:cdns3-ti");
233 MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
234 MODULE_LICENSE("GPL v2");
235 MODULE_DESCRIPTION("Cadence USB3 TI Glue Layer");
236