1 // SPDX-License-Identifier: GPL-1.0+
2 /*
3  * Renesas USB driver
4  *
5  * Copyright (C) 2011 Renesas Solutions Corp.
6  * Copyright (C) 2019 Renesas Electronics Corporation
7  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
8  */
9 
10 #include <asm/io.h>
11 #include <clk.h>
12 #include <dm.h>
13 #include <errno.h>
14 #include <generic-phy.h>
15 #include <linux/err.h>
16 #include <linux/usb/ch9.h>
17 #include <linux/usb/gadget.h>
18 #include <usb.h>
19 
20 #include "common.h"
21 
22 /*
23  *		image of renesas_usbhs
24  *
25  * ex) gadget case
26 
27  * mod.c
28  * mod_gadget.c
29  * mod_host.c		pipe.c		fifo.c
30  *
31  *			+-------+	+-----------+
32  *			| pipe0 |------>| fifo pio  |
33  * +------------+	+-------+	+-----------+
34  * | mod_gadget |=====> | pipe1 |--+
35  * +------------+	+-------+  |	+-----------+
36  *			| pipe2 |  |  +-| fifo dma0 |
37  * +------------+	+-------+  |  |	+-----------+
38  * | mod_host   |	| pipe3 |<-|--+
39  * +------------+	+-------+  |	+-----------+
40  *			| ....  |  +--->| fifo dma1 |
41  *			| ....  |	+-----------+
42  */
43 
44 /*
45  *		common functions
46  */
usbhs_read(struct usbhs_priv * priv,u32 reg)47 u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
48 {
49 	return ioread16(priv->base + reg);
50 }
51 
usbhs_write(struct usbhs_priv * priv,u32 reg,u16 data)52 void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
53 {
54 	iowrite16(data, priv->base + reg);
55 }
56 
usbhs_bset(struct usbhs_priv * priv,u32 reg,u16 mask,u16 data)57 void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
58 {
59 	u16 val = usbhs_read(priv, reg);
60 
61 	val &= ~mask;
62 	val |= data & mask;
63 
64 	usbhs_write(priv, reg, val);
65 }
66 
67 /*
68  *		syscfg functions
69  */
usbhs_sys_clock_ctrl(struct usbhs_priv * priv,int enable)70 static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
71 {
72 	usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
73 }
74 
usbhs_sys_host_ctrl(struct usbhs_priv * priv,int enable)75 void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
76 {
77 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
78 	u16 val  = DCFM | DRPD | HSE | USBE;
79 
80 	/*
81 	 * if enable
82 	 *
83 	 * - select Host mode
84 	 * - D+ Line/D- Line Pull-down
85 	 */
86 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
87 }
88 
usbhs_sys_function_ctrl(struct usbhs_priv * priv,int enable)89 void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
90 {
91 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
92 	u16 val  = HSE | USBE;
93 
94 	/*
95 	 * if enable
96 	 *
97 	 * - select Function mode
98 	 * - D+ Line Pull-up is disabled
99 	 *      When D+ Line Pull-up is enabled,
100 	 *      calling usbhs_sys_function_pullup(,1)
101 	 */
102 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
103 }
104 
usbhs_sys_function_pullup(struct usbhs_priv * priv,int enable)105 void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable)
106 {
107 	usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0);
108 }
109 
usbhs_sys_set_test_mode(struct usbhs_priv * priv,u16 mode)110 void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
111 {
112 	usbhs_write(priv, TESTMODE, mode);
113 }
114 
115 /*
116  *		frame functions
117  */
usbhs_frame_get_num(struct usbhs_priv * priv)118 int usbhs_frame_get_num(struct usbhs_priv *priv)
119 {
120 	return usbhs_read(priv, FRMNUM) & FRNM_MASK;
121 }
122 
123 /*
124  *		usb request functions
125  */
usbhs_usbreq_get_val(struct usbhs_priv * priv,struct usb_ctrlrequest * req)126 void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
127 {
128 	u16 val;
129 
130 	val = usbhs_read(priv, USBREQ);
131 	req->bRequest		= (val >> 8) & 0xFF;
132 	req->bRequestType	= (val >> 0) & 0xFF;
133 
134 	req->wValue	= cpu_to_le16(usbhs_read(priv, USBVAL));
135 	req->wIndex	= cpu_to_le16(usbhs_read(priv, USBINDX));
136 	req->wLength	= cpu_to_le16(usbhs_read(priv, USBLENG));
137 }
138 
usbhs_usbreq_set_val(struct usbhs_priv * priv,struct usb_ctrlrequest * req)139 void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
140 {
141 	usbhs_write(priv, USBREQ,  (req->bRequest << 8) | req->bRequestType);
142 	usbhs_write(priv, USBVAL,  le16_to_cpu(req->wValue));
143 	usbhs_write(priv, USBINDX, le16_to_cpu(req->wIndex));
144 	usbhs_write(priv, USBLENG, le16_to_cpu(req->wLength));
145 
146 	usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
147 }
148 
149 /*
150  *		bus/vbus functions
151  */
usbhs_bus_send_sof_enable(struct usbhs_priv * priv)152 void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
153 {
154 	u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
155 
156 	if (status != USBRST) {
157 		struct device *dev = usbhs_priv_to_dev(priv);
158 		dev_err(dev, "usbhs should be reset\n");
159 	}
160 
161 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
162 }
163 
usbhs_bus_send_reset(struct usbhs_priv * priv)164 void usbhs_bus_send_reset(struct usbhs_priv *priv)
165 {
166 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
167 }
168 
usbhs_bus_get_speed(struct usbhs_priv * priv)169 int usbhs_bus_get_speed(struct usbhs_priv *priv)
170 {
171 	u16 dvstctr = usbhs_read(priv, DVSTCTR);
172 
173 	switch (RHST & dvstctr) {
174 	case RHST_LOW_SPEED:
175 		return USB_SPEED_LOW;
176 	case RHST_FULL_SPEED:
177 		return USB_SPEED_FULL;
178 	case RHST_HIGH_SPEED:
179 		return USB_SPEED_HIGH;
180 	}
181 
182 	return USB_SPEED_UNKNOWN;
183 }
184 
usbhsc_bus_init(struct usbhs_priv * priv)185 static void usbhsc_bus_init(struct usbhs_priv *priv)
186 {
187 	usbhs_write(priv, DVSTCTR, 0);
188 }
189 
190 /*
191  *		device configuration
192  */
usbhs_set_device_config(struct usbhs_priv * priv,int devnum,u16 upphub,u16 hubport,u16 speed)193 int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
194 			   u16 upphub, u16 hubport, u16 speed)
195 {
196 	struct device *dev = usbhs_priv_to_dev(priv);
197 	u16 usbspd = 0;
198 	u32 reg = DEVADD0 + (2 * devnum);
199 
200 	if (devnum > 10) {
201 		dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
202 		return -EIO;
203 	}
204 
205 	if (upphub > 0xA) {
206 		dev_err(dev, "unsupported hub number %d\n", upphub);
207 		return -EIO;
208 	}
209 
210 	switch (speed) {
211 	case USB_SPEED_LOW:
212 		usbspd = USBSPD_SPEED_LOW;
213 		break;
214 	case USB_SPEED_FULL:
215 		usbspd = USBSPD_SPEED_FULL;
216 		break;
217 	case USB_SPEED_HIGH:
218 		usbspd = USBSPD_SPEED_HIGH;
219 		break;
220 	default:
221 		dev_err(dev, "unsupported speed %d\n", speed);
222 		return -EIO;
223 	}
224 
225 	usbhs_write(priv, reg,	UPPHUB(upphub)	|
226 				HUBPORT(hubport)|
227 				USBSPD(usbspd));
228 
229 	return 0;
230 }
231 
232 /*
233  *		interrupt functions
234  */
usbhs_xxxsts_clear(struct usbhs_priv * priv,u16 sts_reg,u16 bit)235 void usbhs_xxxsts_clear(struct usbhs_priv *priv, u16 sts_reg, u16 bit)
236 {
237 	u16 pipe_mask = (u16)GENMASK(usbhs_get_dparam(priv, pipe_size), 0);
238 
239 	usbhs_write(priv, sts_reg, ~(1 << bit) & pipe_mask);
240 }
241 
242 /*
243  *		local functions
244  */
usbhsc_set_buswait(struct usbhs_priv * priv)245 static void usbhsc_set_buswait(struct usbhs_priv *priv)
246 {
247 	int wait = usbhs_get_dparam(priv, buswait_bwait);
248 
249 	/* set bus wait if platform have */
250 	if (wait)
251 		usbhs_bset(priv, BUSWAIT, 0x000F, wait);
252 }
253 
254 /*
255  *		platform default param
256  */
257 
258 /* commonly used on newer SH-Mobile and R-Car SoCs */
259 static struct renesas_usbhs_driver_pipe_config usbhsc_new_pipe[] = {
260 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
261 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true),
262 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true),
263 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
264 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true),
265 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true),
266 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
267 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
268 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
269 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x78, true),
270 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x88, true),
271 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x98, true),
272 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xa8, true),
273 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xb8, true),
274 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xc8, true),
275 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xd8, true),
276 };
277 
278 #define LPSTS			0x102
279 #define LPSTS_SUSPM		BIT(14)
280 
281 #define UGCTRL2			0x184
282 #define UGCTRL2_RESERVED_3	BIT(0)
283 #define UGCTRL2_USB0SEL_EHCI	0x10
284 #define UGCTRL2_USB0SEL_HSUSB	0x20
285 #define UGCTRL2_USB0SEL_OTG	0x30
286 #define UGCTRL2_USB0SEL_MASK	0x30
287 #define UGCTRL2_VBUSSEL		BIT(10)
288 
289 struct usbhs_priv_otg_data {
290 	void __iomem		*base;
291 	void __iomem		*phybase;
292 
293 	struct platform_device	usbhs_dev;
294 	struct usbhs_priv	usbhs_priv;
295 
296 	struct phy		phy;
297 };
298 
usbhs_rcar3_power_ctrl(struct usbhs_priv * priv,bool enable)299 static int usbhs_rcar3_power_ctrl(struct usbhs_priv *priv, bool enable)
300 {
301 	if (enable) {
302 		writel(UGCTRL2_USB0SEL_OTG | UGCTRL2_VBUSSEL | UGCTRL2_RESERVED_3,
303 		       priv->base + UGCTRL2);
304 
305 		usbhs_bset(priv, LPSTS, LPSTS_SUSPM, LPSTS_SUSPM);
306 		/* The controller on R-Car Gen3 needs to wait up to 90 usec */
307 		udelay(90);
308 
309 		usbhs_sys_clock_ctrl(priv, enable);
310 	} else {
311 		usbhs_sys_clock_ctrl(priv, enable);
312 
313 		usbhs_bset(priv, LPSTS, LPSTS_SUSPM, 0);
314 	}
315 
316 	return 0;
317 }
318 
usbhsc_hotplug(struct usbhs_priv * priv)319 void usbhsc_hotplug(struct usbhs_priv *priv)
320 {
321 	int ret;
322 
323 	ret = usbhs_mod_change(priv, USBHS_GADGET);
324 	if (ret < 0)
325 		return;
326 
327 	usbhs_rcar3_power_ctrl(priv, true);
328 
329 	/* bus init */
330 	usbhsc_set_buswait(priv);
331 	usbhsc_bus_init(priv);
332 
333 	/* module start */
334 	usbhs_mod_call(priv, start, priv);
335 }
336 
337 #define USB2_OBINTSTA		0x604
338 #define USB2_OBINT_SESSVLDCHG		BIT(12)
339 #define USB2_OBINT_IDDIGCHG		BIT(11)
340 
usbhs_udc_otg_gadget_handle_interrupts(struct udevice * dev)341 static int usbhs_udc_otg_gadget_handle_interrupts(struct udevice *dev)
342 {
343 	struct usbhs_priv_otg_data *priv = dev_get_priv(dev);
344 	const u32 status = readl(priv->phybase + USB2_OBINTSTA);
345 
346 	/* We don't have a good way to forward IRQ to PHY yet */
347 	if (status & (USB2_OBINT_SESSVLDCHG | USB2_OBINT_IDDIGCHG)) {
348 		writel(USB2_OBINT_SESSVLDCHG | USB2_OBINT_IDDIGCHG,
349 		       priv->phybase + USB2_OBINTSTA);
350 		generic_phy_set_mode(&priv->phy, PHY_MODE_USB_OTG, 0);
351 	}
352 
353 	usbhs_interrupt(0, &priv->usbhs_priv);
354 
355 	return 0;
356 }
357 
usbhs_probe(struct usbhs_priv * priv)358 static int usbhs_probe(struct usbhs_priv *priv)
359 {
360 	int ret;
361 
362 	priv->dparam.type = USBHS_TYPE_RCAR_GEN3;
363 	priv->dparam.pio_dma_border = 64;
364 	priv->dparam.pipe_configs = usbhsc_new_pipe;
365 	priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
366 
367 	/* call pipe and module init */
368 	ret = usbhs_pipe_probe(priv);
369 	if (ret < 0)
370 		return ret;
371 
372 	ret = usbhs_fifo_probe(priv);
373 	if (ret < 0)
374 		goto probe_end_pipe_exit;
375 
376 	ret = usbhs_mod_probe(priv);
377 	if (ret < 0)
378 		goto probe_end_fifo_exit;
379 
380 	usbhs_sys_clock_ctrl(priv, 0);
381 
382 	usbhs_rcar3_power_ctrl(priv, true);
383 	usbhs_mod_autonomy_mode(priv);
384 	usbhsc_hotplug(priv);
385 
386 	return ret;
387 
388 probe_end_fifo_exit:
389 	usbhs_fifo_remove(priv);
390 probe_end_pipe_exit:
391 	usbhs_pipe_remove(priv);
392 	return ret;
393 }
394 
usbhs_udc_otg_probe(struct udevice * dev)395 static int usbhs_udc_otg_probe(struct udevice *dev)
396 {
397 	struct usbhs_priv_otg_data *priv = dev_get_priv(dev);
398 	struct usb_gadget *gadget;
399 	struct clk_bulk clk_bulk;
400 	int ret = -EINVAL;
401 
402 	priv->base = dev_read_addr_ptr(dev);
403 	if (!priv->base)
404 		return -EINVAL;
405 
406 	ret = clk_get_bulk(dev, &clk_bulk);
407 	if (ret)
408 		return ret;
409 
410 	ret = clk_enable_bulk(&clk_bulk);
411 	if (ret)
412 		return ret;
413 
414 	clrsetbits_le32(priv->base + UGCTRL2, UGCTRL2_USB0SEL_MASK, UGCTRL2_USB0SEL_EHCI);
415 	clrsetbits_le16(priv->base + LPSTS, LPSTS_SUSPM, LPSTS_SUSPM);
416 
417 	ret = generic_setup_phy(dev, &priv->phy, 0, PHY_MODE_USB_OTG, 1);
418 	if (ret)
419 		goto err_clk;
420 
421 	priv->phybase = dev_read_addr_ptr(priv->phy.dev);
422 
423 	priv->usbhs_priv.pdev = &priv->usbhs_dev;
424 	priv->usbhs_priv.base = priv->base;
425 	priv->usbhs_dev.dev.driver_data = &priv->usbhs_priv;
426 	ret = usbhs_probe(&priv->usbhs_priv);
427 	if (ret < 0)
428 		goto err_phy;
429 
430 	gadget = usbhsg_get_gadget(&priv->usbhs_priv);
431 	gadget->is_dualspeed = 1;
432 	gadget->is_otg = 0;
433 	gadget->is_a_peripheral = 0;
434 	gadget->b_hnp_enable = 0;
435 	gadget->a_hnp_support = 0;
436 	gadget->a_alt_hnp_support = 0;
437 
438 	return usb_add_gadget_udc((struct device *)dev, gadget);
439 
440 err_phy:
441 	generic_shutdown_phy(&priv->phy);
442 err_clk:
443 	clk_disable_bulk(&clk_bulk);
444 	return ret;
445 }
446 
usbhs_udc_otg_remove(struct udevice * dev)447 static int usbhs_udc_otg_remove(struct udevice *dev)
448 {
449 	struct usbhs_priv_otg_data *priv = dev_get_priv(dev);
450 
451 	usbhs_rcar3_power_ctrl(&priv->usbhs_priv, false);
452 	usbhs_mod_remove(&priv->usbhs_priv);
453 	usbhs_fifo_remove(&priv->usbhs_priv);
454 	usbhs_pipe_remove(&priv->usbhs_priv);
455 
456 	generic_shutdown_phy(&priv->phy);
457 
458 	return dm_scan_fdt_dev(dev);
459 }
460 
461 static const struct udevice_id usbhs_udc_otg_ids[] = {
462 	{ .compatible = "renesas,rcar-gen3-usbhs" },
463 	{},
464 };
465 
466 static const struct usb_gadget_generic_ops usbhs_udc_otg_ops = {
467 	.handle_interrupts = usbhs_udc_otg_gadget_handle_interrupts,
468 };
469 
470 U_BOOT_DRIVER(usbhs_udc_otg) = {
471 	.name		= "usbhs-udc-otg",
472 	.id		= UCLASS_USB_GADGET_GENERIC,
473 	.ops		= &usbhs_udc_otg_ops,
474 	.of_match	= usbhs_udc_otg_ids,
475 	.probe		= usbhs_udc_otg_probe,
476 	.remove		= usbhs_udc_otg_remove,
477 	.priv_auto	= sizeof(struct usbhs_priv_otg_data),
478 };
479