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