xref: /openbmc/linux/drivers/usb/renesas_usbhs/common.c (revision 943126417891372d56aa3fe46295cbf53db31370)
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-r8a7790",
544 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
545 	},
546 	{
547 		.compatible = "renesas,usbhs-r8a7791",
548 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
549 	},
550 	{
551 		.compatible = "renesas,usbhs-r8a7794",
552 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
553 	},
554 	{
555 		.compatible = "renesas,usbhs-r8a7795",
556 		.data = (void *)USBHS_TYPE_RCAR_GEN3,
557 	},
558 	{
559 		.compatible = "renesas,usbhs-r8a7796",
560 		.data = (void *)USBHS_TYPE_RCAR_GEN3,
561 	},
562 	{
563 		.compatible = "renesas,usbhs-r8a77990",
564 		.data = (void *)USBHS_TYPE_RCAR_GEN3_WITH_PLL,
565 	},
566 	{
567 		.compatible = "renesas,usbhs-r8a77995",
568 		.data = (void *)USBHS_TYPE_RCAR_GEN3_WITH_PLL,
569 	},
570 	{
571 		.compatible = "renesas,rcar-gen2-usbhs",
572 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
573 	},
574 	{
575 		.compatible = "renesas,rcar-gen3-usbhs",
576 		.data = (void *)USBHS_TYPE_RCAR_GEN3,
577 	},
578 	{
579 		.compatible = "renesas,rza1-usbhs",
580 		.data = (void *)USBHS_TYPE_RZA1,
581 	},
582 	{ },
583 };
584 MODULE_DEVICE_TABLE(of, usbhs_of_match);
585 
586 static struct renesas_usbhs_platform_info *usbhs_parse_dt(struct device *dev)
587 {
588 	struct renesas_usbhs_platform_info *info;
589 	struct renesas_usbhs_driver_param *dparam;
590 	u32 tmp;
591 	int gpio;
592 
593 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
594 	if (!info)
595 		return NULL;
596 
597 	dparam = &info->driver_param;
598 	dparam->type = (uintptr_t)of_device_get_match_data(dev);
599 	if (!of_property_read_u32(dev->of_node, "renesas,buswait", &tmp))
600 		dparam->buswait_bwait = tmp;
601 	gpio = of_get_named_gpio_flags(dev->of_node, "renesas,enable-gpio", 0,
602 				       NULL);
603 	if (gpio > 0)
604 		dparam->enable_gpio = gpio;
605 
606 	if (dparam->type == USBHS_TYPE_RCAR_GEN2 ||
607 	    dparam->type == USBHS_TYPE_RCAR_GEN3 ||
608 	    dparam->type == USBHS_TYPE_RCAR_GEN3_WITH_PLL) {
609 		dparam->has_usb_dmac = 1;
610 		dparam->pipe_configs = usbhsc_new_pipe;
611 		dparam->pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
612 	}
613 
614 	if (dparam->type == USBHS_TYPE_RZA1) {
615 		dparam->pipe_configs = usbhsc_new_pipe;
616 		dparam->pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
617 	}
618 
619 	return info;
620 }
621 
622 static int usbhs_probe(struct platform_device *pdev)
623 {
624 	struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
625 	struct renesas_usbhs_driver_callback *dfunc;
626 	struct usbhs_priv *priv;
627 	struct resource *res, *irq_res;
628 	int ret;
629 
630 	/* check device node */
631 	if (pdev->dev.of_node)
632 		info = pdev->dev.platform_data = usbhs_parse_dt(&pdev->dev);
633 
634 	/* check platform information */
635 	if (!info) {
636 		dev_err(&pdev->dev, "no platform information\n");
637 		return -EINVAL;
638 	}
639 
640 	/* platform data */
641 	irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
642 	if (!irq_res) {
643 		dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
644 		return -ENODEV;
645 	}
646 
647 	/* usb private data */
648 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
649 	if (!priv)
650 		return -ENOMEM;
651 
652 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
653 	priv->base = devm_ioremap_resource(&pdev->dev, res);
654 	if (IS_ERR(priv->base))
655 		return PTR_ERR(priv->base);
656 
657 	if (of_property_read_bool(pdev->dev.of_node, "extcon")) {
658 		priv->edev = extcon_get_edev_by_phandle(&pdev->dev, 0);
659 		if (IS_ERR(priv->edev))
660 			return PTR_ERR(priv->edev);
661 	}
662 
663 	priv->rsts = devm_reset_control_array_get_optional_shared(&pdev->dev);
664 	if (IS_ERR(priv->rsts))
665 		return PTR_ERR(priv->rsts);
666 
667 	/*
668 	 * care platform info
669 	 */
670 
671 	memcpy(&priv->dparam,
672 	       &info->driver_param,
673 	       sizeof(struct renesas_usbhs_driver_param));
674 
675 	switch (priv->dparam.type) {
676 	case USBHS_TYPE_RCAR_GEN2:
677 		priv->pfunc = usbhs_rcar2_ops;
678 		break;
679 	case USBHS_TYPE_RCAR_GEN3:
680 		priv->pfunc = usbhs_rcar3_ops;
681 		break;
682 	case USBHS_TYPE_RCAR_GEN3_WITH_PLL:
683 		priv->pfunc = usbhs_rcar3_with_pll_ops;
684 		break;
685 	case USBHS_TYPE_RZA1:
686 		priv->pfunc = usbhs_rza1_ops;
687 		break;
688 	default:
689 		if (!info->platform_callback.get_id) {
690 			dev_err(&pdev->dev, "no platform callbacks");
691 			return -EINVAL;
692 		}
693 		memcpy(&priv->pfunc,
694 		       &info->platform_callback,
695 		       sizeof(struct renesas_usbhs_platform_callback));
696 		break;
697 	}
698 
699 	/* set driver callback functions for platform */
700 	dfunc			= &info->driver_callback;
701 	dfunc->notify_hotplug	= usbhsc_drvcllbck_notify_hotplug;
702 
703 	/* set default param if platform doesn't have */
704 	if (!priv->dparam.pipe_configs) {
705 		priv->dparam.pipe_configs = usbhsc_default_pipe;
706 		priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe);
707 	}
708 	if (!priv->dparam.pio_dma_border)
709 		priv->dparam.pio_dma_border = 64; /* 64byte */
710 
711 	/* FIXME */
712 	/* runtime power control ? */
713 	if (priv->pfunc.get_vbus)
714 		usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
715 
716 	/*
717 	 * priv settings
718 	 */
719 	priv->irq	= irq_res->start;
720 	if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
721 		priv->irqflags = IRQF_SHARED;
722 	priv->pdev	= pdev;
723 	INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
724 	spin_lock_init(usbhs_priv_to_lock(priv));
725 
726 	/* call pipe and module init */
727 	ret = usbhs_pipe_probe(priv);
728 	if (ret < 0)
729 		return ret;
730 
731 	ret = usbhs_fifo_probe(priv);
732 	if (ret < 0)
733 		goto probe_end_pipe_exit;
734 
735 	ret = usbhs_mod_probe(priv);
736 	if (ret < 0)
737 		goto probe_end_fifo_exit;
738 
739 	/* dev_set_drvdata should be called after usbhs_mod_init */
740 	platform_set_drvdata(pdev, priv);
741 
742 	ret = reset_control_deassert(priv->rsts);
743 	if (ret)
744 		goto probe_fail_rst;
745 
746 	ret = usbhsc_clk_get(&pdev->dev, priv);
747 	if (ret)
748 		goto probe_fail_clks;
749 
750 	/*
751 	 * deviece reset here because
752 	 * USB device might be used in boot loader.
753 	 */
754 	usbhs_sys_clock_ctrl(priv, 0);
755 
756 	/* check GPIO determining if USB function should be enabled */
757 	if (priv->dparam.enable_gpio) {
758 		gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL);
759 		ret = !gpio_get_value(priv->dparam.enable_gpio);
760 		gpio_free(priv->dparam.enable_gpio);
761 		if (ret) {
762 			dev_warn(&pdev->dev,
763 				 "USB function not selected (GPIO %d)\n",
764 				 priv->dparam.enable_gpio);
765 			ret = -ENOTSUPP;
766 			goto probe_end_mod_exit;
767 		}
768 	}
769 
770 	/*
771 	 * platform call
772 	 *
773 	 * USB phy setup might depend on CPU/Board.
774 	 * If platform has its callback functions,
775 	 * call it here.
776 	 */
777 	ret = usbhs_platform_call(priv, hardware_init, pdev);
778 	if (ret < 0) {
779 		dev_err(&pdev->dev, "platform init failed.\n");
780 		goto probe_end_mod_exit;
781 	}
782 
783 	/* reset phy for connection */
784 	usbhs_platform_call(priv, phy_reset, pdev);
785 
786 	/* power control */
787 	pm_runtime_enable(&pdev->dev);
788 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
789 		usbhsc_power_ctrl(priv, 1);
790 		usbhs_mod_autonomy_mode(priv);
791 	}
792 
793 	/*
794 	 * manual call notify_hotplug for cold plug
795 	 */
796 	usbhsc_drvcllbck_notify_hotplug(pdev);
797 
798 	dev_info(&pdev->dev, "probed\n");
799 
800 	return ret;
801 
802 probe_end_mod_exit:
803 	usbhsc_clk_put(priv);
804 probe_fail_clks:
805 	reset_control_assert(priv->rsts);
806 probe_fail_rst:
807 	usbhs_mod_remove(priv);
808 probe_end_fifo_exit:
809 	usbhs_fifo_remove(priv);
810 probe_end_pipe_exit:
811 	usbhs_pipe_remove(priv);
812 
813 	dev_info(&pdev->dev, "probe failed (%d)\n", ret);
814 
815 	return ret;
816 }
817 
818 static int usbhs_remove(struct platform_device *pdev)
819 {
820 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
821 	struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
822 	struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
823 
824 	dev_dbg(&pdev->dev, "usb remove\n");
825 
826 	dfunc->notify_hotplug = NULL;
827 
828 	/* power off */
829 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
830 		usbhsc_power_ctrl(priv, 0);
831 
832 	pm_runtime_disable(&pdev->dev);
833 
834 	usbhs_platform_call(priv, hardware_exit, pdev);
835 	usbhsc_clk_put(priv);
836 	reset_control_assert(priv->rsts);
837 	usbhs_mod_remove(priv);
838 	usbhs_fifo_remove(priv);
839 	usbhs_pipe_remove(priv);
840 
841 	return 0;
842 }
843 
844 static int usbhsc_suspend(struct device *dev)
845 {
846 	struct usbhs_priv *priv = dev_get_drvdata(dev);
847 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
848 
849 	if (mod) {
850 		usbhs_mod_call(priv, stop, priv);
851 		usbhs_mod_change(priv, -1);
852 	}
853 
854 	if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
855 		usbhsc_power_ctrl(priv, 0);
856 
857 	return 0;
858 }
859 
860 static int usbhsc_resume(struct device *dev)
861 {
862 	struct usbhs_priv *priv = dev_get_drvdata(dev);
863 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
864 
865 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
866 		usbhsc_power_ctrl(priv, 1);
867 		usbhs_mod_autonomy_mode(priv);
868 	}
869 
870 	usbhs_platform_call(priv, phy_reset, pdev);
871 
872 	usbhsc_drvcllbck_notify_hotplug(pdev);
873 
874 	return 0;
875 }
876 
877 static int usbhsc_runtime_nop(struct device *dev)
878 {
879 	/* Runtime PM callback shared between ->runtime_suspend()
880 	 * and ->runtime_resume(). Simply returns success.
881 	 *
882 	 * This driver re-initializes all registers after
883 	 * pm_runtime_get_sync() anyway so there is no need
884 	 * to save and restore registers here.
885 	 */
886 	return 0;
887 }
888 
889 static const struct dev_pm_ops usbhsc_pm_ops = {
890 	.suspend		= usbhsc_suspend,
891 	.resume			= usbhsc_resume,
892 	.runtime_suspend	= usbhsc_runtime_nop,
893 	.runtime_resume		= usbhsc_runtime_nop,
894 };
895 
896 static struct platform_driver renesas_usbhs_driver = {
897 	.driver		= {
898 		.name	= "renesas_usbhs",
899 		.pm	= &usbhsc_pm_ops,
900 		.of_match_table = of_match_ptr(usbhs_of_match),
901 	},
902 	.probe		= usbhs_probe,
903 	.remove		= usbhs_remove,
904 };
905 
906 module_platform_driver(renesas_usbhs_driver);
907 
908 MODULE_LICENSE("GPL");
909 MODULE_DESCRIPTION("Renesas USB driver");
910 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
911