1 // SPDX-License-Identifier: GPL-1.0+
2 /*
3  * Renesas USB driver
4  *
5  * Copyright (C) 2011 Renesas Solutions Corp.
6  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7  */
8 #include <linux/clk.h>
9 #include <linux/err.h>
10 #include <linux/gpio.h>
11 #include <linux/io.h>
12 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/of_gpio.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/reset.h>
17 #include <linux/slab.h>
18 #include <linux/sysfs.h>
19 #include "common.h"
20 #include "rcar2.h"
21 #include "rcar3.h"
22 #include "rza.h"
23 
24 /*
25  *		image of renesas_usbhs
26  *
27  * ex) gadget case
28 
29  * mod.c
30  * mod_gadget.c
31  * mod_host.c		pipe.c		fifo.c
32  *
33  *			+-------+	+-----------+
34  *			| pipe0 |------>| fifo pio  |
35  * +------------+	+-------+	+-----------+
36  * | mod_gadget |=====> | pipe1 |--+
37  * +------------+	+-------+  |	+-----------+
38  *			| pipe2 |  |  +-| fifo dma0 |
39  * +------------+	+-------+  |  |	+-----------+
40  * | mod_host   |	| pipe3 |<-|--+
41  * +------------+	+-------+  |	+-----------+
42  *			| ....  |  +--->| fifo dma1 |
43  *			| ....  |	+-----------+
44  */
45 
46 
47 #define USBHSF_RUNTIME_PWCTRL	(1 << 0)
48 
49 /* status */
50 #define usbhsc_flags_init(p)   do {(p)->flags = 0; } while (0)
51 #define usbhsc_flags_set(p, b) ((p)->flags |=  (b))
52 #define usbhsc_flags_clr(p, b) ((p)->flags &= ~(b))
53 #define usbhsc_flags_has(p, b) ((p)->flags &   (b))
54 
55 /*
56  * platform call back
57  *
58  * renesas usb support platform callback function.
59  * Below macro call it.
60  * if platform doesn't have callback, it return 0 (no error)
61  */
62 #define usbhs_platform_call(priv, func, args...)\
63 	(!(priv) ? -ENODEV :			\
64 	 !((priv)->pfunc.func) ? 0 :		\
65 	 (priv)->pfunc.func(args))
66 
67 /*
68  *		common functions
69  */
70 u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
71 {
72 	return ioread16(priv->base + reg);
73 }
74 
75 void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
76 {
77 	iowrite16(data, priv->base + reg);
78 }
79 
80 void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
81 {
82 	u16 val = usbhs_read(priv, reg);
83 
84 	val &= ~mask;
85 	val |= data & mask;
86 
87 	usbhs_write(priv, reg, val);
88 }
89 
90 struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
91 {
92 	return dev_get_drvdata(&pdev->dev);
93 }
94 
95 /*
96  *		syscfg functions
97  */
98 static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
99 {
100 	usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
101 }
102 
103 void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
104 {
105 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
106 	u16 val  = DCFM | DRPD | HSE | USBE;
107 	int has_otg = usbhs_get_dparam(priv, has_otg);
108 
109 	if (has_otg)
110 		usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN));
111 
112 	/*
113 	 * if enable
114 	 *
115 	 * - select Host mode
116 	 * - D+ Line/D- Line Pull-down
117 	 */
118 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
119 }
120 
121 void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
122 {
123 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
124 	u16 val  = HSE | USBE;
125 
126 	/*
127 	 * if enable
128 	 *
129 	 * - select Function mode
130 	 * - D+ Line Pull-up is disabled
131 	 *      When D+ Line Pull-up is enabled,
132 	 *      calling usbhs_sys_function_pullup(,1)
133 	 */
134 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
135 }
136 
137 void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable)
138 {
139 	usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0);
140 }
141 
142 void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
143 {
144 	usbhs_write(priv, TESTMODE, mode);
145 }
146 
147 /*
148  *		frame functions
149  */
150 int usbhs_frame_get_num(struct usbhs_priv *priv)
151 {
152 	return usbhs_read(priv, FRMNUM) & FRNM_MASK;
153 }
154 
155 /*
156  *		usb request functions
157  */
158 void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
159 {
160 	u16 val;
161 
162 	val = usbhs_read(priv, USBREQ);
163 	req->bRequest		= (val >> 8) & 0xFF;
164 	req->bRequestType	= (val >> 0) & 0xFF;
165 
166 	req->wValue	= usbhs_read(priv, USBVAL);
167 	req->wIndex	= usbhs_read(priv, USBINDX);
168 	req->wLength	= usbhs_read(priv, USBLENG);
169 }
170 
171 void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
172 {
173 	usbhs_write(priv, USBREQ,  (req->bRequest << 8) | req->bRequestType);
174 	usbhs_write(priv, USBVAL,  req->wValue);
175 	usbhs_write(priv, USBINDX, req->wIndex);
176 	usbhs_write(priv, USBLENG, req->wLength);
177 
178 	usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
179 }
180 
181 /*
182  *		bus/vbus functions
183  */
184 void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
185 {
186 	u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
187 
188 	if (status != USBRST) {
189 		struct device *dev = usbhs_priv_to_dev(priv);
190 		dev_err(dev, "usbhs should be reset\n");
191 	}
192 
193 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
194 }
195 
196 void usbhs_bus_send_reset(struct usbhs_priv *priv)
197 {
198 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
199 }
200 
201 int usbhs_bus_get_speed(struct usbhs_priv *priv)
202 {
203 	u16 dvstctr = usbhs_read(priv, DVSTCTR);
204 
205 	switch (RHST & dvstctr) {
206 	case RHST_LOW_SPEED:
207 		return USB_SPEED_LOW;
208 	case RHST_FULL_SPEED:
209 		return USB_SPEED_FULL;
210 	case RHST_HIGH_SPEED:
211 		return USB_SPEED_HIGH;
212 	}
213 
214 	return USB_SPEED_UNKNOWN;
215 }
216 
217 int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
218 {
219 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
220 
221 	return usbhs_platform_call(priv, set_vbus, pdev, enable);
222 }
223 
224 static void usbhsc_bus_init(struct usbhs_priv *priv)
225 {
226 	usbhs_write(priv, DVSTCTR, 0);
227 
228 	usbhs_vbus_ctrl(priv, 0);
229 }
230 
231 /*
232  *		device configuration
233  */
234 int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
235 			   u16 upphub, u16 hubport, u16 speed)
236 {
237 	struct device *dev = usbhs_priv_to_dev(priv);
238 	u16 usbspd = 0;
239 	u32 reg = DEVADD0 + (2 * devnum);
240 
241 	if (devnum > 10) {
242 		dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
243 		return -EIO;
244 	}
245 
246 	if (upphub > 0xA) {
247 		dev_err(dev, "unsupported hub number %d\n", upphub);
248 		return -EIO;
249 	}
250 
251 	switch (speed) {
252 	case USB_SPEED_LOW:
253 		usbspd = USBSPD_SPEED_LOW;
254 		break;
255 	case USB_SPEED_FULL:
256 		usbspd = USBSPD_SPEED_FULL;
257 		break;
258 	case USB_SPEED_HIGH:
259 		usbspd = USBSPD_SPEED_HIGH;
260 		break;
261 	default:
262 		dev_err(dev, "unsupported speed %d\n", speed);
263 		return -EIO;
264 	}
265 
266 	usbhs_write(priv, reg,	UPPHUB(upphub)	|
267 				HUBPORT(hubport)|
268 				USBSPD(usbspd));
269 
270 	return 0;
271 }
272 
273 /*
274  *		interrupt functions
275  */
276 void usbhs_xxxsts_clear(struct usbhs_priv *priv, u16 sts_reg, u16 bit)
277 {
278 	u16 pipe_mask = (u16)GENMASK(usbhs_get_dparam(priv, pipe_size), 0);
279 
280 	usbhs_write(priv, sts_reg, ~(1 << bit) & pipe_mask);
281 }
282 
283 /*
284  *		local functions
285  */
286 static void usbhsc_set_buswait(struct usbhs_priv *priv)
287 {
288 	int wait = usbhs_get_dparam(priv, buswait_bwait);
289 
290 	/* set bus wait if platform have */
291 	if (wait)
292 		usbhs_bset(priv, BUSWAIT, 0x000F, wait);
293 }
294 
295 static bool usbhsc_is_multi_clks(struct usbhs_priv *priv)
296 {
297 	if (priv->dparam.type == USBHS_TYPE_RCAR_GEN3 ||
298 	    priv->dparam.type == USBHS_TYPE_RCAR_GEN3_WITH_PLL)
299 		return true;
300 
301 	return false;
302 }
303 
304 static int usbhsc_clk_get(struct device *dev, struct usbhs_priv *priv)
305 {
306 	if (!usbhsc_is_multi_clks(priv))
307 		return 0;
308 
309 	/* The first clock should exist */
310 	priv->clks[0] = of_clk_get(dev->of_node, 0);
311 	if (IS_ERR(priv->clks[0]))
312 		return PTR_ERR(priv->clks[0]);
313 
314 	/*
315 	 * To backward compatibility with old DT, this driver checks the return
316 	 * value if it's -ENOENT or not.
317 	 */
318 	priv->clks[1] = of_clk_get(dev->of_node, 1);
319 	if (PTR_ERR(priv->clks[1]) == -ENOENT)
320 		priv->clks[1] = NULL;
321 	else if (IS_ERR(priv->clks[1]))
322 		return PTR_ERR(priv->clks[1]);
323 
324 	return 0;
325 }
326 
327 static void usbhsc_clk_put(struct usbhs_priv *priv)
328 {
329 	int i;
330 
331 	if (!usbhsc_is_multi_clks(priv))
332 		return;
333 
334 	for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
335 		clk_put(priv->clks[i]);
336 }
337 
338 static int usbhsc_clk_prepare_enable(struct usbhs_priv *priv)
339 {
340 	int i, ret;
341 
342 	if (!usbhsc_is_multi_clks(priv))
343 		return 0;
344 
345 	for (i = 0; i < ARRAY_SIZE(priv->clks); i++) {
346 		ret = clk_prepare_enable(priv->clks[i]);
347 		if (ret) {
348 			while (--i >= 0)
349 				clk_disable_unprepare(priv->clks[i]);
350 			return ret;
351 		}
352 	}
353 
354 	return ret;
355 }
356 
357 static void usbhsc_clk_disable_unprepare(struct usbhs_priv *priv)
358 {
359 	int i;
360 
361 	if (!usbhsc_is_multi_clks(priv))
362 		return;
363 
364 	for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
365 		clk_disable_unprepare(priv->clks[i]);
366 }
367 
368 /*
369  *		platform default param
370  */
371 
372 /* commonly used on old SH-Mobile SoCs */
373 static struct renesas_usbhs_driver_pipe_config usbhsc_default_pipe[] = {
374 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
375 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, false),
376 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x18, false),
377 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x28, true),
378 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x38, true),
379 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
380 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
381 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
382 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
383 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x07, false),
384 };
385 
386 /* commonly used on newer SH-Mobile and R-Car SoCs */
387 static struct renesas_usbhs_driver_pipe_config usbhsc_new_pipe[] = {
388 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
389 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true),
390 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true),
391 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
392 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true),
393 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true),
394 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
395 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
396 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
397 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x78, true),
398 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x88, true),
399 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x98, true),
400 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xa8, true),
401 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xb8, true),
402 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xc8, true),
403 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xd8, true),
404 };
405 
406 /*
407  *		power control
408  */
409 static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
410 {
411 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
412 	struct device *dev = usbhs_priv_to_dev(priv);
413 
414 	if (enable) {
415 		/* enable PM */
416 		pm_runtime_get_sync(dev);
417 
418 		/* enable clks */
419 		if (usbhsc_clk_prepare_enable(priv))
420 			return;
421 
422 		/* enable platform power */
423 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
424 
425 		/* USB on */
426 		usbhs_sys_clock_ctrl(priv, enable);
427 	} else {
428 		/* USB off */
429 		usbhs_sys_clock_ctrl(priv, enable);
430 
431 		/* disable platform power */
432 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
433 
434 		/* disable clks */
435 		usbhsc_clk_disable_unprepare(priv);
436 
437 		/* disable PM */
438 		pm_runtime_put_sync(dev);
439 	}
440 }
441 
442 /*
443  *		hotplug
444  */
445 static void usbhsc_hotplug(struct usbhs_priv *priv)
446 {
447 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
448 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
449 	int id;
450 	int enable;
451 	int cable;
452 	int ret;
453 
454 	/*
455 	 * get vbus status from platform
456 	 */
457 	enable = usbhs_platform_call(priv, get_vbus, pdev);
458 
459 	/*
460 	 * get id from platform
461 	 */
462 	id = usbhs_platform_call(priv, get_id, pdev);
463 
464 	if (enable && !mod) {
465 		if (priv->edev) {
466 			cable = extcon_get_state(priv->edev, EXTCON_USB_HOST);
467 			if ((cable > 0 && id != USBHS_HOST) ||
468 			    (!cable && id != USBHS_GADGET)) {
469 				dev_info(&pdev->dev,
470 					 "USB cable plugged in doesn't match the selected role!\n");
471 				return;
472 			}
473 		}
474 
475 		ret = usbhs_mod_change(priv, id);
476 		if (ret < 0)
477 			return;
478 
479 		dev_dbg(&pdev->dev, "%s enable\n", __func__);
480 
481 		/* power on */
482 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
483 			usbhsc_power_ctrl(priv, enable);
484 
485 		/* bus init */
486 		usbhsc_set_buswait(priv);
487 		usbhsc_bus_init(priv);
488 
489 		/* module start */
490 		usbhs_mod_call(priv, start, priv);
491 
492 	} else if (!enable && mod) {
493 		dev_dbg(&pdev->dev, "%s disable\n", __func__);
494 
495 		/* module stop */
496 		usbhs_mod_call(priv, stop, priv);
497 
498 		/* bus init */
499 		usbhsc_bus_init(priv);
500 
501 		/* power off */
502 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
503 			usbhsc_power_ctrl(priv, enable);
504 
505 		usbhs_mod_change(priv, -1);
506 
507 		/* reset phy for next connection */
508 		usbhs_platform_call(priv, phy_reset, pdev);
509 	}
510 }
511 
512 /*
513  *		notify hotplug
514  */
515 static void usbhsc_notify_hotplug(struct work_struct *work)
516 {
517 	struct usbhs_priv *priv = container_of(work,
518 					       struct usbhs_priv,
519 					       notify_hotplug_work.work);
520 	usbhsc_hotplug(priv);
521 }
522 
523 static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
524 {
525 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
526 	int delay = usbhs_get_dparam(priv, detection_delay);
527 
528 	/*
529 	 * This functions will be called in interrupt.
530 	 * To make sure safety context,
531 	 * use workqueue for usbhs_notify_hotplug
532 	 */
533 	schedule_delayed_work(&priv->notify_hotplug_work,
534 			      msecs_to_jiffies(delay));
535 	return 0;
536 }
537 
538 /*
539  *		platform functions
540  */
541 static const struct of_device_id usbhs_of_match[] = {
542 	{
543 		.compatible = "renesas,usbhs-r8a774c0",
544 		.data = (void *)USBHS_TYPE_RCAR_GEN3_WITH_PLL,
545 	},
546 	{
547 		.compatible = "renesas,usbhs-r8a7790",
548 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
549 	},
550 	{
551 		.compatible = "renesas,usbhs-r8a7791",
552 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
553 	},
554 	{
555 		.compatible = "renesas,usbhs-r8a7794",
556 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
557 	},
558 	{
559 		.compatible = "renesas,usbhs-r8a7795",
560 		.data = (void *)USBHS_TYPE_RCAR_GEN3,
561 	},
562 	{
563 		.compatible = "renesas,usbhs-r8a7796",
564 		.data = (void *)USBHS_TYPE_RCAR_GEN3,
565 	},
566 	{
567 		.compatible = "renesas,usbhs-r8a77990",
568 		.data = (void *)USBHS_TYPE_RCAR_GEN3_WITH_PLL,
569 	},
570 	{
571 		.compatible = "renesas,usbhs-r8a77995",
572 		.data = (void *)USBHS_TYPE_RCAR_GEN3_WITH_PLL,
573 	},
574 	{
575 		.compatible = "renesas,rcar-gen2-usbhs",
576 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
577 	},
578 	{
579 		.compatible = "renesas,rcar-gen3-usbhs",
580 		.data = (void *)USBHS_TYPE_RCAR_GEN3,
581 	},
582 	{
583 		.compatible = "renesas,rza1-usbhs",
584 		.data = (void *)USBHS_TYPE_RZA1,
585 	},
586 	{ },
587 };
588 MODULE_DEVICE_TABLE(of, usbhs_of_match);
589 
590 static struct renesas_usbhs_platform_info *usbhs_parse_dt(struct device *dev)
591 {
592 	struct renesas_usbhs_platform_info *info;
593 	struct renesas_usbhs_driver_param *dparam;
594 	u32 tmp;
595 	int gpio;
596 
597 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
598 	if (!info)
599 		return NULL;
600 
601 	dparam = &info->driver_param;
602 	dparam->type = (uintptr_t)of_device_get_match_data(dev);
603 	if (!of_property_read_u32(dev->of_node, "renesas,buswait", &tmp))
604 		dparam->buswait_bwait = tmp;
605 	gpio = of_get_named_gpio_flags(dev->of_node, "renesas,enable-gpio", 0,
606 				       NULL);
607 	if (gpio > 0)
608 		dparam->enable_gpio = gpio;
609 
610 	if (dparam->type == USBHS_TYPE_RCAR_GEN2 ||
611 	    dparam->type == USBHS_TYPE_RCAR_GEN3 ||
612 	    dparam->type == USBHS_TYPE_RCAR_GEN3_WITH_PLL) {
613 		dparam->has_usb_dmac = 1;
614 		dparam->pipe_configs = usbhsc_new_pipe;
615 		dparam->pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
616 	}
617 
618 	if (dparam->type == USBHS_TYPE_RZA1) {
619 		dparam->pipe_configs = usbhsc_new_pipe;
620 		dparam->pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
621 	}
622 
623 	return info;
624 }
625 
626 static int usbhs_probe(struct platform_device *pdev)
627 {
628 	struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
629 	struct renesas_usbhs_driver_callback *dfunc;
630 	struct usbhs_priv *priv;
631 	struct resource *res, *irq_res;
632 	int ret;
633 
634 	/* check device node */
635 	if (pdev->dev.of_node)
636 		info = pdev->dev.platform_data = usbhs_parse_dt(&pdev->dev);
637 
638 	/* check platform information */
639 	if (!info) {
640 		dev_err(&pdev->dev, "no platform information\n");
641 		return -EINVAL;
642 	}
643 
644 	/* platform data */
645 	irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
646 	if (!irq_res) {
647 		dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
648 		return -ENODEV;
649 	}
650 
651 	/* usb private data */
652 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
653 	if (!priv)
654 		return -ENOMEM;
655 
656 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
657 	priv->base = devm_ioremap_resource(&pdev->dev, res);
658 	if (IS_ERR(priv->base))
659 		return PTR_ERR(priv->base);
660 
661 	if (of_property_read_bool(pdev->dev.of_node, "extcon")) {
662 		priv->edev = extcon_get_edev_by_phandle(&pdev->dev, 0);
663 		if (IS_ERR(priv->edev))
664 			return PTR_ERR(priv->edev);
665 	}
666 
667 	priv->rsts = devm_reset_control_array_get_optional_shared(&pdev->dev);
668 	if (IS_ERR(priv->rsts))
669 		return PTR_ERR(priv->rsts);
670 
671 	/*
672 	 * care platform info
673 	 */
674 
675 	memcpy(&priv->dparam,
676 	       &info->driver_param,
677 	       sizeof(struct renesas_usbhs_driver_param));
678 
679 	switch (priv->dparam.type) {
680 	case USBHS_TYPE_RCAR_GEN2:
681 		priv->pfunc = usbhs_rcar2_ops;
682 		break;
683 	case USBHS_TYPE_RCAR_GEN3:
684 		priv->pfunc = usbhs_rcar3_ops;
685 		break;
686 	case USBHS_TYPE_RCAR_GEN3_WITH_PLL:
687 		priv->pfunc = usbhs_rcar3_with_pll_ops;
688 		break;
689 	case USBHS_TYPE_RZA1:
690 		priv->pfunc = usbhs_rza1_ops;
691 		break;
692 	default:
693 		if (!info->platform_callback.get_id) {
694 			dev_err(&pdev->dev, "no platform callbacks");
695 			return -EINVAL;
696 		}
697 		memcpy(&priv->pfunc,
698 		       &info->platform_callback,
699 		       sizeof(struct renesas_usbhs_platform_callback));
700 		break;
701 	}
702 
703 	/* set driver callback functions for platform */
704 	dfunc			= &info->driver_callback;
705 	dfunc->notify_hotplug	= usbhsc_drvcllbck_notify_hotplug;
706 
707 	/* set default param if platform doesn't have */
708 	if (!priv->dparam.pipe_configs) {
709 		priv->dparam.pipe_configs = usbhsc_default_pipe;
710 		priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe);
711 	}
712 	if (!priv->dparam.pio_dma_border)
713 		priv->dparam.pio_dma_border = 64; /* 64byte */
714 
715 	/* FIXME */
716 	/* runtime power control ? */
717 	if (priv->pfunc.get_vbus)
718 		usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
719 
720 	/*
721 	 * priv settings
722 	 */
723 	priv->irq	= irq_res->start;
724 	if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
725 		priv->irqflags = IRQF_SHARED;
726 	priv->pdev	= pdev;
727 	INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
728 	spin_lock_init(usbhs_priv_to_lock(priv));
729 
730 	/* call pipe and module init */
731 	ret = usbhs_pipe_probe(priv);
732 	if (ret < 0)
733 		return ret;
734 
735 	ret = usbhs_fifo_probe(priv);
736 	if (ret < 0)
737 		goto probe_end_pipe_exit;
738 
739 	ret = usbhs_mod_probe(priv);
740 	if (ret < 0)
741 		goto probe_end_fifo_exit;
742 
743 	/* dev_set_drvdata should be called after usbhs_mod_init */
744 	platform_set_drvdata(pdev, priv);
745 
746 	ret = reset_control_deassert(priv->rsts);
747 	if (ret)
748 		goto probe_fail_rst;
749 
750 	ret = usbhsc_clk_get(&pdev->dev, priv);
751 	if (ret)
752 		goto probe_fail_clks;
753 
754 	/*
755 	 * deviece reset here because
756 	 * USB device might be used in boot loader.
757 	 */
758 	usbhs_sys_clock_ctrl(priv, 0);
759 
760 	/* check GPIO determining if USB function should be enabled */
761 	if (priv->dparam.enable_gpio) {
762 		gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL);
763 		ret = !gpio_get_value(priv->dparam.enable_gpio);
764 		gpio_free(priv->dparam.enable_gpio);
765 		if (ret) {
766 			dev_warn(&pdev->dev,
767 				 "USB function not selected (GPIO %d)\n",
768 				 priv->dparam.enable_gpio);
769 			ret = -ENOTSUPP;
770 			goto probe_end_mod_exit;
771 		}
772 	}
773 
774 	/*
775 	 * platform call
776 	 *
777 	 * USB phy setup might depend on CPU/Board.
778 	 * If platform has its callback functions,
779 	 * call it here.
780 	 */
781 	ret = usbhs_platform_call(priv, hardware_init, pdev);
782 	if (ret < 0) {
783 		dev_err(&pdev->dev, "platform init failed.\n");
784 		goto probe_end_mod_exit;
785 	}
786 
787 	/* reset phy for connection */
788 	usbhs_platform_call(priv, phy_reset, pdev);
789 
790 	/* power control */
791 	pm_runtime_enable(&pdev->dev);
792 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
793 		usbhsc_power_ctrl(priv, 1);
794 		usbhs_mod_autonomy_mode(priv);
795 	}
796 
797 	/*
798 	 * manual call notify_hotplug for cold plug
799 	 */
800 	usbhsc_drvcllbck_notify_hotplug(pdev);
801 
802 	dev_info(&pdev->dev, "probed\n");
803 
804 	return ret;
805 
806 probe_end_mod_exit:
807 	usbhsc_clk_put(priv);
808 probe_fail_clks:
809 	reset_control_assert(priv->rsts);
810 probe_fail_rst:
811 	usbhs_mod_remove(priv);
812 probe_end_fifo_exit:
813 	usbhs_fifo_remove(priv);
814 probe_end_pipe_exit:
815 	usbhs_pipe_remove(priv);
816 
817 	dev_info(&pdev->dev, "probe failed (%d)\n", ret);
818 
819 	return ret;
820 }
821 
822 static int usbhs_remove(struct platform_device *pdev)
823 {
824 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
825 	struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
826 	struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
827 
828 	dev_dbg(&pdev->dev, "usb remove\n");
829 
830 	dfunc->notify_hotplug = NULL;
831 
832 	/* power off */
833 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
834 		usbhsc_power_ctrl(priv, 0);
835 
836 	pm_runtime_disable(&pdev->dev);
837 
838 	usbhs_platform_call(priv, hardware_exit, pdev);
839 	usbhsc_clk_put(priv);
840 	reset_control_assert(priv->rsts);
841 	usbhs_mod_remove(priv);
842 	usbhs_fifo_remove(priv);
843 	usbhs_pipe_remove(priv);
844 
845 	return 0;
846 }
847 
848 static __maybe_unused int usbhsc_suspend(struct device *dev)
849 {
850 	struct usbhs_priv *priv = dev_get_drvdata(dev);
851 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
852 
853 	if (mod) {
854 		usbhs_mod_call(priv, stop, priv);
855 		usbhs_mod_change(priv, -1);
856 	}
857 
858 	if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
859 		usbhsc_power_ctrl(priv, 0);
860 
861 	return 0;
862 }
863 
864 static __maybe_unused int usbhsc_resume(struct device *dev)
865 {
866 	struct usbhs_priv *priv = dev_get_drvdata(dev);
867 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
868 
869 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
870 		usbhsc_power_ctrl(priv, 1);
871 		usbhs_mod_autonomy_mode(priv);
872 	}
873 
874 	usbhs_platform_call(priv, phy_reset, pdev);
875 
876 	usbhsc_drvcllbck_notify_hotplug(pdev);
877 
878 	return 0;
879 }
880 
881 static SIMPLE_DEV_PM_OPS(usbhsc_pm_ops, usbhsc_suspend, usbhsc_resume);
882 
883 static struct platform_driver renesas_usbhs_driver = {
884 	.driver		= {
885 		.name	= "renesas_usbhs",
886 		.pm	= &usbhsc_pm_ops,
887 		.of_match_table = of_match_ptr(usbhs_of_match),
888 	},
889 	.probe		= usbhs_probe,
890 	.remove		= usbhs_remove,
891 };
892 
893 module_platform_driver(renesas_usbhs_driver);
894 
895 MODULE_LICENSE("GPL");
896 MODULE_DESCRIPTION("Renesas USB driver");
897 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
898