xref: /openbmc/linux/drivers/usb/renesas_usbhs/common.c (revision f181dbb4824130e84f46e5be5b49cf6456f96683)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-1.0+
2f1407d5cSKuninori Morimoto /*
3f1407d5cSKuninori Morimoto  * Renesas USB driver
4f1407d5cSKuninori Morimoto  *
5f1407d5cSKuninori Morimoto  * Copyright (C) 2011 Renesas Solutions Corp.
6f1407d5cSKuninori Morimoto  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7f1407d5cSKuninori Morimoto  */
8148e1134SThierry Reding #include <linux/err.h>
98ecef00fSUlrich Hecht #include <linux/gpio.h>
10f1407d5cSKuninori Morimoto #include <linux/io.h>
11f1407d5cSKuninori Morimoto #include <linux/module.h>
12b854100eSYoshihiro Shimoda #include <linux/of_device.h>
13b854100eSYoshihiro Shimoda #include <linux/of_gpio.h>
14f1407d5cSKuninori Morimoto #include <linux/pm_runtime.h>
15*f181dbb4SYoshihiro Shimoda #include <linux/reset.h>
16f1407d5cSKuninori Morimoto #include <linux/slab.h>
17f1407d5cSKuninori Morimoto #include <linux/sysfs.h>
18cc502bb7SPaul Bolle #include "common.h"
198ecef00fSUlrich Hecht #include "rcar2.h"
20de18757eSYoshihiro Shimoda #include "rcar3.h"
21aec2927bSChris Brandt #include "rza.h"
22f1407d5cSKuninori Morimoto 
23233f519dSKuninori Morimoto /*
24233f519dSKuninori Morimoto  *		image of renesas_usbhs
25233f519dSKuninori Morimoto  *
26233f519dSKuninori Morimoto  * ex) gadget case
27233f519dSKuninori Morimoto 
28233f519dSKuninori Morimoto  * mod.c
29233f519dSKuninori Morimoto  * mod_gadget.c
30233f519dSKuninori Morimoto  * mod_host.c		pipe.c		fifo.c
31233f519dSKuninori Morimoto  *
32233f519dSKuninori Morimoto  *			+-------+	+-----------+
33233f519dSKuninori Morimoto  *			| pipe0 |------>| fifo pio  |
34233f519dSKuninori Morimoto  * +------------+	+-------+	+-----------+
35233f519dSKuninori Morimoto  * | mod_gadget |=====> | pipe1 |--+
36233f519dSKuninori Morimoto  * +------------+	+-------+  |	+-----------+
37233f519dSKuninori Morimoto  *			| pipe2 |  |  +-| fifo dma0 |
38233f519dSKuninori Morimoto  * +------------+	+-------+  |  |	+-----------+
39233f519dSKuninori Morimoto  * | mod_host   |	| pipe3 |<-|--+
40233f519dSKuninori Morimoto  * +------------+	+-------+  |	+-----------+
41233f519dSKuninori Morimoto  *			| ....  |  +--->| fifo dma1 |
42233f519dSKuninori Morimoto  *			| ....  |	+-----------+
43233f519dSKuninori Morimoto  */
44233f519dSKuninori Morimoto 
45233f519dSKuninori Morimoto 
46b002ff6eSKuninori Morimoto #define USBHSF_RUNTIME_PWCTRL	(1 << 0)
47b002ff6eSKuninori Morimoto 
48b002ff6eSKuninori Morimoto /* status */
49b002ff6eSKuninori Morimoto #define usbhsc_flags_init(p)   do {(p)->flags = 0; } while (0)
50b002ff6eSKuninori Morimoto #define usbhsc_flags_set(p, b) ((p)->flags |=  (b))
51b002ff6eSKuninori Morimoto #define usbhsc_flags_clr(p, b) ((p)->flags &= ~(b))
52b002ff6eSKuninori Morimoto #define usbhsc_flags_has(p, b) ((p)->flags &   (b))
53b002ff6eSKuninori Morimoto 
54f1407d5cSKuninori Morimoto /*
55f1407d5cSKuninori Morimoto  * platform call back
56f1407d5cSKuninori Morimoto  *
57f1407d5cSKuninori Morimoto  * renesas usb support platform callback function.
58f1407d5cSKuninori Morimoto  * Below macro call it.
59f1407d5cSKuninori Morimoto  * if platform doesn't have callback, it return 0 (no error)
60f1407d5cSKuninori Morimoto  */
61f1407d5cSKuninori Morimoto #define usbhs_platform_call(priv, func, args...)\
62f1407d5cSKuninori Morimoto 	(!(priv) ? -ENODEV :			\
6348298206SKuninori Morimoto 	 !((priv)->pfunc.func) ? 0 :		\
6448298206SKuninori Morimoto 	 (priv)->pfunc.func(args))
65f1407d5cSKuninori Morimoto 
66f1407d5cSKuninori Morimoto /*
67f1407d5cSKuninori Morimoto  *		common functions
68f1407d5cSKuninori Morimoto  */
69f1407d5cSKuninori Morimoto u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
70f1407d5cSKuninori Morimoto {
71f1407d5cSKuninori Morimoto 	return ioread16(priv->base + reg);
72f1407d5cSKuninori Morimoto }
73f1407d5cSKuninori Morimoto 
74f1407d5cSKuninori Morimoto void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
75f1407d5cSKuninori Morimoto {
76f1407d5cSKuninori Morimoto 	iowrite16(data, priv->base + reg);
77f1407d5cSKuninori Morimoto }
78f1407d5cSKuninori Morimoto 
79f1407d5cSKuninori Morimoto void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
80f1407d5cSKuninori Morimoto {
81f1407d5cSKuninori Morimoto 	u16 val = usbhs_read(priv, reg);
82f1407d5cSKuninori Morimoto 
83f1407d5cSKuninori Morimoto 	val &= ~mask;
84f1407d5cSKuninori Morimoto 	val |= data & mask;
85f1407d5cSKuninori Morimoto 
86f1407d5cSKuninori Morimoto 	usbhs_write(priv, reg, val);
87f1407d5cSKuninori Morimoto }
88f1407d5cSKuninori Morimoto 
89206dcc2cSKuninori Morimoto struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
90206dcc2cSKuninori Morimoto {
91206dcc2cSKuninori Morimoto 	return dev_get_drvdata(&pdev->dev);
92206dcc2cSKuninori Morimoto }
93206dcc2cSKuninori Morimoto 
94f1407d5cSKuninori Morimoto /*
95f1407d5cSKuninori Morimoto  *		syscfg functions
96f1407d5cSKuninori Morimoto  */
9776190152SKuninori Morimoto static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
98f1407d5cSKuninori Morimoto {
99f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
100f1407d5cSKuninori Morimoto }
101f1407d5cSKuninori Morimoto 
102f1407d5cSKuninori Morimoto void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
103f1407d5cSKuninori Morimoto {
1043244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
1053244a7b4SKuninori Morimoto 	u16 val  = DCFM | DRPD | HSE | USBE;
106f427eb64SKuninori Morimoto 	int has_otg = usbhs_get_dparam(priv, has_otg);
107f427eb64SKuninori Morimoto 
108f427eb64SKuninori Morimoto 	if (has_otg)
109f427eb64SKuninori Morimoto 		usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN));
110f1407d5cSKuninori Morimoto 
111f1407d5cSKuninori Morimoto 	/*
112f1407d5cSKuninori Morimoto 	 * if enable
113f1407d5cSKuninori Morimoto 	 *
114f1407d5cSKuninori Morimoto 	 * - select Host mode
115f1407d5cSKuninori Morimoto 	 * - D+ Line/D- Line Pull-down
116f1407d5cSKuninori Morimoto 	 */
117f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
118f1407d5cSKuninori Morimoto }
119f1407d5cSKuninori Morimoto 
120f1407d5cSKuninori Morimoto void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
121f1407d5cSKuninori Morimoto {
1223244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
12304a5def3STakeshi Kihara 	u16 val  = HSE | USBE;
124f1407d5cSKuninori Morimoto 
125f1407d5cSKuninori Morimoto 	/*
126f1407d5cSKuninori Morimoto 	 * if enable
127f1407d5cSKuninori Morimoto 	 *
128f1407d5cSKuninori Morimoto 	 * - select Function mode
12904a5def3STakeshi Kihara 	 * - D+ Line Pull-up is disabled
13004a5def3STakeshi Kihara 	 *      When D+ Line Pull-up is enabled,
13104a5def3STakeshi Kihara 	 *      calling usbhs_sys_function_pullup(,1)
132f1407d5cSKuninori Morimoto 	 */
133f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
134f1407d5cSKuninori Morimoto }
135f1407d5cSKuninori Morimoto 
1364cd2f599Skuninori.morimoto.gx@renesas.com void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable)
1374cd2f599Skuninori.morimoto.gx@renesas.com {
1384cd2f599Skuninori.morimoto.gx@renesas.com 	usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0);
1394cd2f599Skuninori.morimoto.gx@renesas.com }
1404cd2f599Skuninori.morimoto.gx@renesas.com 
141dfbb7f4fSKuninori Morimoto void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
142dfbb7f4fSKuninori Morimoto {
143dfbb7f4fSKuninori Morimoto 	usbhs_write(priv, TESTMODE, mode);
144dfbb7f4fSKuninori Morimoto }
145dfbb7f4fSKuninori Morimoto 
146f1407d5cSKuninori Morimoto /*
147f1407d5cSKuninori Morimoto  *		frame functions
148f1407d5cSKuninori Morimoto  */
149f1407d5cSKuninori Morimoto int usbhs_frame_get_num(struct usbhs_priv *priv)
150f1407d5cSKuninori Morimoto {
151f1407d5cSKuninori Morimoto 	return usbhs_read(priv, FRMNUM) & FRNM_MASK;
152f1407d5cSKuninori Morimoto }
153f1407d5cSKuninori Morimoto 
154f1407d5cSKuninori Morimoto /*
155ef8bedb9SKuninori Morimoto  *		usb request functions
156ef8bedb9SKuninori Morimoto  */
157ef8bedb9SKuninori Morimoto void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
158ef8bedb9SKuninori Morimoto {
159ef8bedb9SKuninori Morimoto 	u16 val;
160ef8bedb9SKuninori Morimoto 
161ef8bedb9SKuninori Morimoto 	val = usbhs_read(priv, USBREQ);
162ef8bedb9SKuninori Morimoto 	req->bRequest		= (val >> 8) & 0xFF;
163ef8bedb9SKuninori Morimoto 	req->bRequestType	= (val >> 0) & 0xFF;
164ef8bedb9SKuninori Morimoto 
165ef8bedb9SKuninori Morimoto 	req->wValue	= usbhs_read(priv, USBVAL);
166ef8bedb9SKuninori Morimoto 	req->wIndex	= usbhs_read(priv, USBINDX);
167ef8bedb9SKuninori Morimoto 	req->wLength	= usbhs_read(priv, USBLENG);
168ef8bedb9SKuninori Morimoto }
169ef8bedb9SKuninori Morimoto 
170ef8bedb9SKuninori Morimoto void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
171ef8bedb9SKuninori Morimoto {
172ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBREQ,  (req->bRequest << 8) | req->bRequestType);
173ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBVAL,  req->wValue);
174ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBINDX, req->wIndex);
175ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBLENG, req->wLength);
176ef8bedb9SKuninori Morimoto 
177ef8bedb9SKuninori Morimoto 	usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
178ef8bedb9SKuninori Morimoto }
179ef8bedb9SKuninori Morimoto 
180ef8bedb9SKuninori Morimoto /*
181258485d9SKuninori Morimoto  *		bus/vbus functions
182258485d9SKuninori Morimoto  */
183258485d9SKuninori Morimoto void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
184258485d9SKuninori Morimoto {
185a9be4a45SKuninori Morimoto 	u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
186a9be4a45SKuninori Morimoto 
187a9be4a45SKuninori Morimoto 	if (status != USBRST) {
188a9be4a45SKuninori Morimoto 		struct device *dev = usbhs_priv_to_dev(priv);
189a9be4a45SKuninori Morimoto 		dev_err(dev, "usbhs should be reset\n");
190a9be4a45SKuninori Morimoto 	}
191a9be4a45SKuninori Morimoto 
192258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
193258485d9SKuninori Morimoto }
194258485d9SKuninori Morimoto 
195258485d9SKuninori Morimoto void usbhs_bus_send_reset(struct usbhs_priv *priv)
196258485d9SKuninori Morimoto {
197258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
198258485d9SKuninori Morimoto }
199258485d9SKuninori Morimoto 
20075587f52SKuninori Morimoto int usbhs_bus_get_speed(struct usbhs_priv *priv)
20175587f52SKuninori Morimoto {
20275587f52SKuninori Morimoto 	u16 dvstctr = usbhs_read(priv, DVSTCTR);
20375587f52SKuninori Morimoto 
20475587f52SKuninori Morimoto 	switch (RHST & dvstctr) {
20575587f52SKuninori Morimoto 	case RHST_LOW_SPEED:
20675587f52SKuninori Morimoto 		return USB_SPEED_LOW;
20775587f52SKuninori Morimoto 	case RHST_FULL_SPEED:
20875587f52SKuninori Morimoto 		return USB_SPEED_FULL;
20975587f52SKuninori Morimoto 	case RHST_HIGH_SPEED:
21075587f52SKuninori Morimoto 		return USB_SPEED_HIGH;
21175587f52SKuninori Morimoto 	}
21275587f52SKuninori Morimoto 
21375587f52SKuninori Morimoto 	return USB_SPEED_UNKNOWN;
21475587f52SKuninori Morimoto }
21575587f52SKuninori Morimoto 
216258485d9SKuninori Morimoto int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
217258485d9SKuninori Morimoto {
218258485d9SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
219258485d9SKuninori Morimoto 
220258485d9SKuninori Morimoto 	return usbhs_platform_call(priv, set_vbus, pdev, enable);
221258485d9SKuninori Morimoto }
222258485d9SKuninori Morimoto 
223258485d9SKuninori Morimoto static void usbhsc_bus_init(struct usbhs_priv *priv)
224258485d9SKuninori Morimoto {
225258485d9SKuninori Morimoto 	usbhs_write(priv, DVSTCTR, 0);
226258485d9SKuninori Morimoto 
227258485d9SKuninori Morimoto 	usbhs_vbus_ctrl(priv, 0);
228258485d9SKuninori Morimoto }
229258485d9SKuninori Morimoto 
230258485d9SKuninori Morimoto /*
231eb05191fSKuninori Morimoto  *		device configuration
232eb05191fSKuninori Morimoto  */
2333dd49268SKuninori Morimoto int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
234eb05191fSKuninori Morimoto 			   u16 upphub, u16 hubport, u16 speed)
235eb05191fSKuninori Morimoto {
236eb05191fSKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
237eb05191fSKuninori Morimoto 	u16 usbspd = 0;
238eb05191fSKuninori Morimoto 	u32 reg = DEVADD0 + (2 * devnum);
239eb05191fSKuninori Morimoto 
240eb05191fSKuninori Morimoto 	if (devnum > 10) {
241eb05191fSKuninori Morimoto 		dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
242eb05191fSKuninori Morimoto 		return -EIO;
243eb05191fSKuninori Morimoto 	}
244eb05191fSKuninori Morimoto 
245eb05191fSKuninori Morimoto 	if (upphub > 0xA) {
246eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported hub number %d\n", upphub);
247eb05191fSKuninori Morimoto 		return -EIO;
248eb05191fSKuninori Morimoto 	}
249eb05191fSKuninori Morimoto 
250eb05191fSKuninori Morimoto 	switch (speed) {
251eb05191fSKuninori Morimoto 	case USB_SPEED_LOW:
252eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_LOW;
253eb05191fSKuninori Morimoto 		break;
254eb05191fSKuninori Morimoto 	case USB_SPEED_FULL:
255eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_FULL;
256eb05191fSKuninori Morimoto 		break;
257eb05191fSKuninori Morimoto 	case USB_SPEED_HIGH:
258eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_HIGH;
259eb05191fSKuninori Morimoto 		break;
260eb05191fSKuninori Morimoto 	default:
261eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported speed %d\n", speed);
262eb05191fSKuninori Morimoto 		return -EIO;
263eb05191fSKuninori Morimoto 	}
264eb05191fSKuninori Morimoto 
265eb05191fSKuninori Morimoto 	usbhs_write(priv, reg,	UPPHUB(upphub)	|
266eb05191fSKuninori Morimoto 				HUBPORT(hubport)|
267eb05191fSKuninori Morimoto 				USBSPD(usbspd));
268eb05191fSKuninori Morimoto 
269eb05191fSKuninori Morimoto 	return 0;
270eb05191fSKuninori Morimoto }
271eb05191fSKuninori Morimoto 
272eb05191fSKuninori Morimoto /*
273ab330cf3SYoshihiro Shimoda  *		interrupt functions
274ab330cf3SYoshihiro Shimoda  */
275ab330cf3SYoshihiro Shimoda void usbhs_xxxsts_clear(struct usbhs_priv *priv, u16 sts_reg, u16 bit)
276ab330cf3SYoshihiro Shimoda {
277ab330cf3SYoshihiro Shimoda 	u16 pipe_mask = (u16)GENMASK(usbhs_get_dparam(priv, pipe_size), 0);
278ab330cf3SYoshihiro Shimoda 
279ab330cf3SYoshihiro Shimoda 	usbhs_write(priv, sts_reg, ~(1 << bit) & pipe_mask);
280ab330cf3SYoshihiro Shimoda }
281ab330cf3SYoshihiro Shimoda 
282ab330cf3SYoshihiro Shimoda /*
283f1407d5cSKuninori Morimoto  *		local functions
284f1407d5cSKuninori Morimoto  */
28511935de5SKuninori Morimoto static void usbhsc_set_buswait(struct usbhs_priv *priv)
286f1407d5cSKuninori Morimoto {
287f1407d5cSKuninori Morimoto 	int wait = usbhs_get_dparam(priv, buswait_bwait);
288f1407d5cSKuninori Morimoto 
289f1407d5cSKuninori Morimoto 	/* set bus wait if platform have */
290f1407d5cSKuninori Morimoto 	if (wait)
291f1407d5cSKuninori Morimoto 		usbhs_bset(priv, BUSWAIT, 0x000F, wait);
292f1407d5cSKuninori Morimoto }
293f1407d5cSKuninori Morimoto 
294f1407d5cSKuninori Morimoto /*
295f1407d5cSKuninori Morimoto  *		platform default param
296f1407d5cSKuninori Morimoto  */
2978ecef00fSUlrich Hecht 
2988ecef00fSUlrich Hecht /* commonly used on old SH-Mobile SoCs */
29951f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_default_pipe[] = {
30051f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
30151f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, false),
30251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x18, false),
30351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x28, true),
30451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x38, true),
30551f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
30651f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
30751f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
30851f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
30951f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x07, false),
310f1407d5cSKuninori Morimoto };
311f1407d5cSKuninori Morimoto 
3128ecef00fSUlrich Hecht /* commonly used on newer SH-Mobile and R-Car SoCs */
31351f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_new_pipe[] = {
31451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
31551f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true),
31651f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true),
31751f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
31851f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true),
31951f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true),
32051f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
32151f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
32251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
32351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x78, true),
32451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x88, true),
32551f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x98, true),
32651f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xa8, true),
32751f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xb8, true),
32851f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xc8, true),
32951f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xd8, true),
3308ecef00fSUlrich Hecht };
3318ecef00fSUlrich Hecht 
332f1407d5cSKuninori Morimoto /*
3336e267da8SKuninori Morimoto  *		power control
3346e267da8SKuninori Morimoto  */
3356e267da8SKuninori Morimoto static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
3366e267da8SKuninori Morimoto {
337f1ee56a0SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
3386e267da8SKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
3396e267da8SKuninori Morimoto 
3406e267da8SKuninori Morimoto 	if (enable) {
3416e267da8SKuninori Morimoto 		/* enable PM */
3426e267da8SKuninori Morimoto 		pm_runtime_get_sync(dev);
3436e267da8SKuninori Morimoto 
344f1ee56a0SKuninori Morimoto 		/* enable platform power */
345f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
346f1ee56a0SKuninori Morimoto 
3476e267da8SKuninori Morimoto 		/* USB on */
3486e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
3496e267da8SKuninori Morimoto 	} else {
3506e267da8SKuninori Morimoto 		/* USB off */
3516e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
3526e267da8SKuninori Morimoto 
353f1ee56a0SKuninori Morimoto 		/* disable platform power */
354f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
355f1ee56a0SKuninori Morimoto 
3566e267da8SKuninori Morimoto 		/* disable PM */
3576e267da8SKuninori Morimoto 		pm_runtime_put_sync(dev);
3586e267da8SKuninori Morimoto 	}
3596e267da8SKuninori Morimoto }
3606e267da8SKuninori Morimoto 
3616e267da8SKuninori Morimoto /*
362ca8a282aSKuninori Morimoto  *		hotplug
363f1407d5cSKuninori Morimoto  */
364ca8a282aSKuninori Morimoto static void usbhsc_hotplug(struct usbhs_priv *priv)
365f1407d5cSKuninori Morimoto {
366f1407d5cSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
367f1407d5cSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
368f1407d5cSKuninori Morimoto 	int id;
369f1407d5cSKuninori Morimoto 	int enable;
370d7b3968dSSergei Shtylyov 	int cable;
371f1407d5cSKuninori Morimoto 	int ret;
372f1407d5cSKuninori Morimoto 
373f1407d5cSKuninori Morimoto 	/*
374f1407d5cSKuninori Morimoto 	 * get vbus status from platform
375f1407d5cSKuninori Morimoto 	 */
376f1407d5cSKuninori Morimoto 	enable = usbhs_platform_call(priv, get_vbus, pdev);
377f1407d5cSKuninori Morimoto 
378f1407d5cSKuninori Morimoto 	/*
379f1407d5cSKuninori Morimoto 	 * get id from platform
380f1407d5cSKuninori Morimoto 	 */
381f1407d5cSKuninori Morimoto 	id = usbhs_platform_call(priv, get_id, pdev);
382f1407d5cSKuninori Morimoto 
383f1407d5cSKuninori Morimoto 	if (enable && !mod) {
384d7b3968dSSergei Shtylyov 		if (priv->edev) {
385ea07b8cfSChanwoo Choi 			cable = extcon_get_state(priv->edev, EXTCON_USB_HOST);
386d7b3968dSSergei Shtylyov 			if ((cable > 0 && id != USBHS_HOST) ||
387d7b3968dSSergei Shtylyov 			    (!cable && id != USBHS_GADGET)) {
388d7b3968dSSergei Shtylyov 				dev_info(&pdev->dev,
389d7b3968dSSergei Shtylyov 					 "USB cable plugged in doesn't match the selected role!\n");
390d7b3968dSSergei Shtylyov 				return;
391d7b3968dSSergei Shtylyov 			}
392d7b3968dSSergei Shtylyov 		}
393d7b3968dSSergei Shtylyov 
394f1407d5cSKuninori Morimoto 		ret = usbhs_mod_change(priv, id);
395f1407d5cSKuninori Morimoto 		if (ret < 0)
396f1407d5cSKuninori Morimoto 			return;
397f1407d5cSKuninori Morimoto 
398f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s enable\n", __func__);
399f1407d5cSKuninori Morimoto 
4006e267da8SKuninori Morimoto 		/* power on */
401b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
4026e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
403f1407d5cSKuninori Morimoto 
404258485d9SKuninori Morimoto 		/* bus init */
405258485d9SKuninori Morimoto 		usbhsc_set_buswait(priv);
406258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
407258485d9SKuninori Morimoto 
408f1407d5cSKuninori Morimoto 		/* module start */
409f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, start, priv);
410f1407d5cSKuninori Morimoto 
411f1407d5cSKuninori Morimoto 	} else if (!enable && mod) {
412f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s disable\n", __func__);
413f1407d5cSKuninori Morimoto 
414f1407d5cSKuninori Morimoto 		/* module stop */
415f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
416f1407d5cSKuninori Morimoto 
417258485d9SKuninori Morimoto 		/* bus init */
418258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
419258485d9SKuninori Morimoto 
4206e267da8SKuninori Morimoto 		/* power off */
421b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
4226e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
423f1407d5cSKuninori Morimoto 
424f1407d5cSKuninori Morimoto 		usbhs_mod_change(priv, -1);
425f1407d5cSKuninori Morimoto 
426f1407d5cSKuninori Morimoto 		/* reset phy for next connection */
427f1407d5cSKuninori Morimoto 		usbhs_platform_call(priv, phy_reset, pdev);
428f1407d5cSKuninori Morimoto 	}
429f1407d5cSKuninori Morimoto }
430f1407d5cSKuninori Morimoto 
431ca8a282aSKuninori Morimoto /*
432ca8a282aSKuninori Morimoto  *		notify hotplug
433ca8a282aSKuninori Morimoto  */
434ca8a282aSKuninori Morimoto static void usbhsc_notify_hotplug(struct work_struct *work)
435ca8a282aSKuninori Morimoto {
436ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = container_of(work,
437ca8a282aSKuninori Morimoto 					       struct usbhs_priv,
438ca8a282aSKuninori Morimoto 					       notify_hotplug_work.work);
439ca8a282aSKuninori Morimoto 	usbhsc_hotplug(priv);
440ca8a282aSKuninori Morimoto }
441ca8a282aSKuninori Morimoto 
442b4fcea2aSKuninori Morimoto static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
443f1407d5cSKuninori Morimoto {
444206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
445bc57381eSKuninori Morimoto 	int delay = usbhs_get_dparam(priv, detection_delay);
446f1407d5cSKuninori Morimoto 
447f1407d5cSKuninori Morimoto 	/*
448f1407d5cSKuninori Morimoto 	 * This functions will be called in interrupt.
449f1407d5cSKuninori Morimoto 	 * To make sure safety context,
450f1407d5cSKuninori Morimoto 	 * use workqueue for usbhs_notify_hotplug
451f1407d5cSKuninori Morimoto 	 */
452a49a88f1SKuninori Morimoto 	schedule_delayed_work(&priv->notify_hotplug_work,
453a49a88f1SKuninori Morimoto 			      msecs_to_jiffies(delay));
454f1407d5cSKuninori Morimoto 	return 0;
455f1407d5cSKuninori Morimoto }
456f1407d5cSKuninori Morimoto 
457f1407d5cSKuninori Morimoto /*
458f1407d5cSKuninori Morimoto  *		platform functions
459f1407d5cSKuninori Morimoto  */
460b854100eSYoshihiro Shimoda static const struct of_device_id usbhs_of_match[] = {
461b854100eSYoshihiro Shimoda 	{
462b854100eSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7790",
463e0213bc5SYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
464b854100eSYoshihiro Shimoda 	},
465b854100eSYoshihiro Shimoda 	{
466b854100eSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7791",
467e0213bc5SYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
468b854100eSYoshihiro Shimoda 	},
469af6e613bSYoshihiro Shimoda 	{
470af6e613bSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7794",
471af6e613bSYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
472af6e613bSYoshihiro Shimoda 	},
473f5f6afa8SYoshihiro Shimoda 	{
474f5f6afa8SYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7795",
475de18757eSYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN3,
476f5f6afa8SYoshihiro Shimoda 	},
4772f1a993aSSimon Horman 	{
478aae819e1SYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7796",
479aae819e1SYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN3,
480aae819e1SYoshihiro Shimoda 	},
481aae819e1SYoshihiro Shimoda 	{
4820f38672cSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a77995",
4830f38672cSYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN3_WITH_PLL,
4840f38672cSYoshihiro Shimoda 	},
4850f38672cSYoshihiro Shimoda 	{
4862f1a993aSSimon Horman 		.compatible = "renesas,rcar-gen2-usbhs",
4872f1a993aSSimon Horman 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
4882f1a993aSSimon Horman 	},
4892f1a993aSSimon Horman 	{
4902f1a993aSSimon Horman 		.compatible = "renesas,rcar-gen3-usbhs",
491de18757eSYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN3,
4922f1a993aSSimon Horman 	},
493aec2927bSChris Brandt 	{
494aec2927bSChris Brandt 		.compatible = "renesas,rza1-usbhs",
495aec2927bSChris Brandt 		.data = (void *)USBHS_TYPE_RZA1,
496aec2927bSChris Brandt 	},
497b854100eSYoshihiro Shimoda 	{ },
498b854100eSYoshihiro Shimoda };
499b854100eSYoshihiro Shimoda MODULE_DEVICE_TABLE(of, usbhs_of_match);
500b854100eSYoshihiro Shimoda 
501b854100eSYoshihiro Shimoda static struct renesas_usbhs_platform_info *usbhs_parse_dt(struct device *dev)
502b854100eSYoshihiro Shimoda {
503b854100eSYoshihiro Shimoda 	struct renesas_usbhs_platform_info *info;
504b854100eSYoshihiro Shimoda 	struct renesas_usbhs_driver_param *dparam;
505b854100eSYoshihiro Shimoda 	u32 tmp;
506b854100eSYoshihiro Shimoda 	int gpio;
507b854100eSYoshihiro Shimoda 
508b854100eSYoshihiro Shimoda 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
509b854100eSYoshihiro Shimoda 	if (!info)
510b854100eSYoshihiro Shimoda 		return NULL;
511b854100eSYoshihiro Shimoda 
512b854100eSYoshihiro Shimoda 	dparam = &info->driver_param;
513b61e47b4SGeert Uytterhoeven 	dparam->type = (uintptr_t)of_device_get_match_data(dev);
514b854100eSYoshihiro Shimoda 	if (!of_property_read_u32(dev->of_node, "renesas,buswait", &tmp))
515b854100eSYoshihiro Shimoda 		dparam->buswait_bwait = tmp;
516b854100eSYoshihiro Shimoda 	gpio = of_get_named_gpio_flags(dev->of_node, "renesas,enable-gpio", 0,
517b854100eSYoshihiro Shimoda 				       NULL);
518b854100eSYoshihiro Shimoda 	if (gpio > 0)
519b854100eSYoshihiro Shimoda 		dparam->enable_gpio = gpio;
520b854100eSYoshihiro Shimoda 
521772ce812SYoshihiro Shimoda 	if (dparam->type == USBHS_TYPE_RCAR_GEN2 ||
5220f38672cSYoshihiro Shimoda 	    dparam->type == USBHS_TYPE_RCAR_GEN3 ||
5230f38672cSYoshihiro Shimoda 	    dparam->type == USBHS_TYPE_RCAR_GEN3_WITH_PLL) {
524ab330cf3SYoshihiro Shimoda 		dparam->has_usb_dmac = 1;
525a877b8e5SYoshihiro Shimoda 		dparam->pipe_configs = usbhsc_new_pipe;
526a877b8e5SYoshihiro Shimoda 		dparam->pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
527a877b8e5SYoshihiro Shimoda 	}
528ab330cf3SYoshihiro Shimoda 
529aec2927bSChris Brandt 	if (dparam->type == USBHS_TYPE_RZA1) {
530aec2927bSChris Brandt 		dparam->pipe_configs = usbhsc_new_pipe;
531aec2927bSChris Brandt 		dparam->pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
532aec2927bSChris Brandt 	}
533aec2927bSChris Brandt 
534b854100eSYoshihiro Shimoda 	return info;
535b854100eSYoshihiro Shimoda }
536b854100eSYoshihiro Shimoda 
537b7a8d17dSKuninori Morimoto static int usbhs_probe(struct platform_device *pdev)
538f1407d5cSKuninori Morimoto {
539f0f14a7aSKuninori Morimoto 	struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
540f1407d5cSKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc;
541f1407d5cSKuninori Morimoto 	struct usbhs_priv *priv;
54253069af3SShimoda, Yoshihiro 	struct resource *res, *irq_res;
543f1407d5cSKuninori Morimoto 	int ret;
544f1407d5cSKuninori Morimoto 
545b854100eSYoshihiro Shimoda 	/* check device node */
546b854100eSYoshihiro Shimoda 	if (pdev->dev.of_node)
547b854100eSYoshihiro Shimoda 		info = pdev->dev.platform_data = usbhs_parse_dt(&pdev->dev);
548b854100eSYoshihiro Shimoda 
549f1407d5cSKuninori Morimoto 	/* check platform information */
5508ecef00fSUlrich Hecht 	if (!info) {
551f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "no platform information\n");
552f1407d5cSKuninori Morimoto 		return -EINVAL;
553f1407d5cSKuninori Morimoto 	}
554f1407d5cSKuninori Morimoto 
555f1407d5cSKuninori Morimoto 	/* platform data */
55653069af3SShimoda, Yoshihiro 	irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
557687f16b8SVarka Bhadram 	if (!irq_res) {
558f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
559f1407d5cSKuninori Morimoto 		return -ENODEV;
560f1407d5cSKuninori Morimoto 	}
561f1407d5cSKuninori Morimoto 
562f1407d5cSKuninori Morimoto 	/* usb private data */
56358efc77cSKuninori Morimoto 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
564b1cb07cdSPeter Chen 	if (!priv)
565f1407d5cSKuninori Morimoto 		return -ENOMEM;
566f1407d5cSKuninori Morimoto 
567687f16b8SVarka Bhadram 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
568148e1134SThierry Reding 	priv->base = devm_ioremap_resource(&pdev->dev, res);
569148e1134SThierry Reding 	if (IS_ERR(priv->base))
570148e1134SThierry Reding 		return PTR_ERR(priv->base);
571f1407d5cSKuninori Morimoto 
572d7b3968dSSergei Shtylyov 	if (of_property_read_bool(pdev->dev.of_node, "extcon")) {
573d7b3968dSSergei Shtylyov 		priv->edev = extcon_get_edev_by_phandle(&pdev->dev, 0);
574d7b3968dSSergei Shtylyov 		if (IS_ERR(priv->edev))
575d7b3968dSSergei Shtylyov 			return PTR_ERR(priv->edev);
576d7b3968dSSergei Shtylyov 	}
577d7b3968dSSergei Shtylyov 
578*f181dbb4SYoshihiro Shimoda 	priv->rsts = devm_reset_control_array_get_optional_shared(&pdev->dev);
579*f181dbb4SYoshihiro Shimoda 	if (IS_ERR(priv->rsts))
580*f181dbb4SYoshihiro Shimoda 		return PTR_ERR(priv->rsts);
581*f181dbb4SYoshihiro Shimoda 
582f1407d5cSKuninori Morimoto 	/*
583f1407d5cSKuninori Morimoto 	 * care platform info
584f1407d5cSKuninori Morimoto 	 */
5858ecef00fSUlrich Hecht 
58648298206SKuninori Morimoto 	memcpy(&priv->dparam,
58748298206SKuninori Morimoto 	       &info->driver_param,
58848298206SKuninori Morimoto 	       sizeof(struct renesas_usbhs_driver_param));
589f1407d5cSKuninori Morimoto 
5908ecef00fSUlrich Hecht 	switch (priv->dparam.type) {
591e0213bc5SYoshihiro Shimoda 	case USBHS_TYPE_RCAR_GEN2:
5928ecef00fSUlrich Hecht 		priv->pfunc = usbhs_rcar2_ops;
5938ecef00fSUlrich Hecht 		break;
594de18757eSYoshihiro Shimoda 	case USBHS_TYPE_RCAR_GEN3:
595de18757eSYoshihiro Shimoda 		priv->pfunc = usbhs_rcar3_ops;
596de18757eSYoshihiro Shimoda 		break;
5970f38672cSYoshihiro Shimoda 	case USBHS_TYPE_RCAR_GEN3_WITH_PLL:
5980f38672cSYoshihiro Shimoda 		priv->pfunc = usbhs_rcar3_with_pll_ops;
5998ada211dSYoshihiro Shimoda 		if (!IS_ERR_OR_NULL(priv->edev)) {
6008ada211dSYoshihiro Shimoda 			priv->nb.notifier_call = priv->pfunc.notifier;
6018ada211dSYoshihiro Shimoda 			ret = devm_extcon_register_notifier(&pdev->dev,
6028ada211dSYoshihiro Shimoda 							    priv->edev,
6038ada211dSYoshihiro Shimoda 							    EXTCON_USB_HOST,
6048ada211dSYoshihiro Shimoda 							    &priv->nb);
6058ada211dSYoshihiro Shimoda 			if (ret < 0)
6068ada211dSYoshihiro Shimoda 				dev_err(&pdev->dev, "no notifier registered\n");
6078ada211dSYoshihiro Shimoda 		}
6080f38672cSYoshihiro Shimoda 		break;
609aec2927bSChris Brandt 	case USBHS_TYPE_RZA1:
610aec2927bSChris Brandt 		priv->pfunc = usbhs_rza1_ops;
611aec2927bSChris Brandt 		break;
6128ecef00fSUlrich Hecht 	default:
6138ecef00fSUlrich Hecht 		if (!info->platform_callback.get_id) {
6148ecef00fSUlrich Hecht 			dev_err(&pdev->dev, "no platform callbacks");
6158ecef00fSUlrich Hecht 			return -EINVAL;
6168ecef00fSUlrich Hecht 		}
6178ecef00fSUlrich Hecht 		memcpy(&priv->pfunc,
6188ecef00fSUlrich Hecht 		       &info->platform_callback,
6198ecef00fSUlrich Hecht 		       sizeof(struct renesas_usbhs_platform_callback));
6208ecef00fSUlrich Hecht 		break;
6218ecef00fSUlrich Hecht 	}
6228ecef00fSUlrich Hecht 
623f1407d5cSKuninori Morimoto 	/* set driver callback functions for platform */
624f1407d5cSKuninori Morimoto 	dfunc			= &info->driver_callback;
625f1407d5cSKuninori Morimoto 	dfunc->notify_hotplug	= usbhsc_drvcllbck_notify_hotplug;
626f1407d5cSKuninori Morimoto 
627f1407d5cSKuninori Morimoto 	/* set default param if platform doesn't have */
62851f141a9SYoshihiro Shimoda 	if (!priv->dparam.pipe_configs) {
62951f141a9SYoshihiro Shimoda 		priv->dparam.pipe_configs = usbhsc_default_pipe;
63051f141a9SYoshihiro Shimoda 		priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe);
631f1407d5cSKuninori Morimoto 	}
63248298206SKuninori Morimoto 	if (!priv->dparam.pio_dma_border)
63348298206SKuninori Morimoto 		priv->dparam.pio_dma_border = 64; /* 64byte */
634f1407d5cSKuninori Morimoto 
635b002ff6eSKuninori Morimoto 	/* FIXME */
636b002ff6eSKuninori Morimoto 	/* runtime power control ? */
63748298206SKuninori Morimoto 	if (priv->pfunc.get_vbus)
638b002ff6eSKuninori Morimoto 		usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
639b002ff6eSKuninori Morimoto 
640f1407d5cSKuninori Morimoto 	/*
641f1407d5cSKuninori Morimoto 	 * priv settings
642f1407d5cSKuninori Morimoto 	 */
64353069af3SShimoda, Yoshihiro 	priv->irq	= irq_res->start;
64453069af3SShimoda, Yoshihiro 	if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
64553069af3SShimoda, Yoshihiro 		priv->irqflags = IRQF_SHARED;
646f1407d5cSKuninori Morimoto 	priv->pdev	= pdev;
647bc57381eSKuninori Morimoto 	INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
648f1407d5cSKuninori Morimoto 	spin_lock_init(usbhs_priv_to_lock(priv));
649f1407d5cSKuninori Morimoto 
650f1407d5cSKuninori Morimoto 	/* call pipe and module init */
651f1407d5cSKuninori Morimoto 	ret = usbhs_pipe_probe(priv);
652f1407d5cSKuninori Morimoto 	if (ret < 0)
65358efc77cSKuninori Morimoto 		return ret;
654f1407d5cSKuninori Morimoto 
655d3af90a5SKuninori Morimoto 	ret = usbhs_fifo_probe(priv);
656f1407d5cSKuninori Morimoto 	if (ret < 0)
65797f93227SKuninori Morimoto 		goto probe_end_pipe_exit;
658f1407d5cSKuninori Morimoto 
659d3af90a5SKuninori Morimoto 	ret = usbhs_mod_probe(priv);
660d3af90a5SKuninori Morimoto 	if (ret < 0)
661d3af90a5SKuninori Morimoto 		goto probe_end_fifo_exit;
662d3af90a5SKuninori Morimoto 
663f1407d5cSKuninori Morimoto 	/* dev_set_drvdata should be called after usbhs_mod_init */
664c9a0552eSLibo Chen 	platform_set_drvdata(pdev, priv);
665f1407d5cSKuninori Morimoto 
666*f181dbb4SYoshihiro Shimoda 	ret = reset_control_deassert(priv->rsts);
667*f181dbb4SYoshihiro Shimoda 	if (ret)
668*f181dbb4SYoshihiro Shimoda 		goto probe_fail_rst;
669*f181dbb4SYoshihiro Shimoda 
670f1407d5cSKuninori Morimoto 	/*
671f1407d5cSKuninori Morimoto 	 * deviece reset here because
672f1407d5cSKuninori Morimoto 	 * USB device might be used in boot loader.
673f1407d5cSKuninori Morimoto 	 */
674f1407d5cSKuninori Morimoto 	usbhs_sys_clock_ctrl(priv, 0);
675f1407d5cSKuninori Morimoto 
6768ecef00fSUlrich Hecht 	/* check GPIO determining if USB function should be enabled */
6778ecef00fSUlrich Hecht 	if (priv->dparam.enable_gpio) {
6788ecef00fSUlrich Hecht 		gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL);
6798ecef00fSUlrich Hecht 		ret = !gpio_get_value(priv->dparam.enable_gpio);
6808ecef00fSUlrich Hecht 		gpio_free(priv->dparam.enable_gpio);
6818ecef00fSUlrich Hecht 		if (ret) {
6828ecef00fSUlrich Hecht 			dev_warn(&pdev->dev,
6838ecef00fSUlrich Hecht 				 "USB function not selected (GPIO %d)\n",
6848ecef00fSUlrich Hecht 				 priv->dparam.enable_gpio);
6858ecef00fSUlrich Hecht 			ret = -ENOTSUPP;
6868ecef00fSUlrich Hecht 			goto probe_end_mod_exit;
6878ecef00fSUlrich Hecht 		}
6888ecef00fSUlrich Hecht 	}
6898ecef00fSUlrich Hecht 
690f1407d5cSKuninori Morimoto 	/*
691f1407d5cSKuninori Morimoto 	 * platform call
692f1407d5cSKuninori Morimoto 	 *
693f1407d5cSKuninori Morimoto 	 * USB phy setup might depend on CPU/Board.
694f1407d5cSKuninori Morimoto 	 * If platform has its callback functions,
695f1407d5cSKuninori Morimoto 	 * call it here.
696f1407d5cSKuninori Morimoto 	 */
697f1407d5cSKuninori Morimoto 	ret = usbhs_platform_call(priv, hardware_init, pdev);
698f1407d5cSKuninori Morimoto 	if (ret < 0) {
69948ac1e57SSergei Shtylyov 		dev_err(&pdev->dev, "platform init failed.\n");
70097f93227SKuninori Morimoto 		goto probe_end_mod_exit;
701f1407d5cSKuninori Morimoto 	}
702f1407d5cSKuninori Morimoto 
703f1407d5cSKuninori Morimoto 	/* reset phy for connection */
704f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
705f1407d5cSKuninori Morimoto 
706b002ff6eSKuninori Morimoto 	/* power control */
707b002ff6eSKuninori Morimoto 	pm_runtime_enable(&pdev->dev);
708b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
709b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
710b002ff6eSKuninori Morimoto 		usbhs_mod_autonomy_mode(priv);
711b002ff6eSKuninori Morimoto 	}
712b002ff6eSKuninori Morimoto 
713f1407d5cSKuninori Morimoto 	/*
714f1407d5cSKuninori Morimoto 	 * manual call notify_hotplug for cold plug
715f1407d5cSKuninori Morimoto 	 */
716368504c0SSergei Shtylyov 	usbhsc_drvcllbck_notify_hotplug(pdev);
717f1407d5cSKuninori Morimoto 
718f1407d5cSKuninori Morimoto 	dev_info(&pdev->dev, "probed\n");
719f1407d5cSKuninori Morimoto 
720f1407d5cSKuninori Morimoto 	return ret;
721f1407d5cSKuninori Morimoto 
722f1407d5cSKuninori Morimoto probe_end_mod_exit:
723*f181dbb4SYoshihiro Shimoda 	reset_control_assert(priv->rsts);
724*f181dbb4SYoshihiro Shimoda probe_fail_rst:
725f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
726d3af90a5SKuninori Morimoto probe_end_fifo_exit:
727d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
72897f93227SKuninori Morimoto probe_end_pipe_exit:
72997f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
730f1407d5cSKuninori Morimoto 
7310d799503SYoshihiro Shimoda 	dev_info(&pdev->dev, "probe failed (%d)\n", ret);
732f1407d5cSKuninori Morimoto 
733f1407d5cSKuninori Morimoto 	return ret;
734f1407d5cSKuninori Morimoto }
735f1407d5cSKuninori Morimoto 
736fb4e98abSBill Pemberton static int usbhs_remove(struct platform_device *pdev)
737f1407d5cSKuninori Morimoto {
738206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
739f0f14a7aSKuninori Morimoto 	struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
740af32fe51SKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
741f1407d5cSKuninori Morimoto 
742f1407d5cSKuninori Morimoto 	dev_dbg(&pdev->dev, "usb remove\n");
743f1407d5cSKuninori Morimoto 
744af32fe51SKuninori Morimoto 	dfunc->notify_hotplug = NULL;
745af32fe51SKuninori Morimoto 
746b002ff6eSKuninori Morimoto 	/* power off */
747b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
748b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
749f1407d5cSKuninori Morimoto 
750b002ff6eSKuninori Morimoto 	pm_runtime_disable(&pdev->dev);
751f1407d5cSKuninori Morimoto 
752f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, hardware_exit, pdev);
753*f181dbb4SYoshihiro Shimoda 	reset_control_assert(priv->rsts);
754f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
755d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
75697f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
757f1407d5cSKuninori Morimoto 
758f1407d5cSKuninori Morimoto 	return 0;
759f1407d5cSKuninori Morimoto }
760f1407d5cSKuninori Morimoto 
761ca8a282aSKuninori Morimoto static int usbhsc_suspend(struct device *dev)
762ca8a282aSKuninori Morimoto {
763ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
764ca8a282aSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
765ca8a282aSKuninori Morimoto 
766ca8a282aSKuninori Morimoto 	if (mod) {
767ca8a282aSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
768ca8a282aSKuninori Morimoto 		usbhs_mod_change(priv, -1);
769ca8a282aSKuninori Morimoto 	}
770ca8a282aSKuninori Morimoto 
771ca8a282aSKuninori Morimoto 	if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
772ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
773ca8a282aSKuninori Morimoto 
774ca8a282aSKuninori Morimoto 	return 0;
775ca8a282aSKuninori Morimoto }
776ca8a282aSKuninori Morimoto 
777ca8a282aSKuninori Morimoto static int usbhsc_resume(struct device *dev)
778ca8a282aSKuninori Morimoto {
779ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
780ca8a282aSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
781ca8a282aSKuninori Morimoto 
78259a0879aSYoshihiro Shimoda 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
783ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
78459a0879aSYoshihiro Shimoda 		usbhs_mod_autonomy_mode(priv);
78559a0879aSYoshihiro Shimoda 	}
786ca8a282aSKuninori Morimoto 
7875b50d3b5SKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
7885b50d3b5SKuninori Morimoto 
7895b50d3b5SKuninori Morimoto 	usbhsc_drvcllbck_notify_hotplug(pdev);
790ca8a282aSKuninori Morimoto 
791ca8a282aSKuninori Morimoto 	return 0;
792ca8a282aSKuninori Morimoto }
793ca8a282aSKuninori Morimoto 
794ca8a282aSKuninori Morimoto static int usbhsc_runtime_nop(struct device *dev)
795ca8a282aSKuninori Morimoto {
796ca8a282aSKuninori Morimoto 	/* Runtime PM callback shared between ->runtime_suspend()
797ca8a282aSKuninori Morimoto 	 * and ->runtime_resume(). Simply returns success.
798ca8a282aSKuninori Morimoto 	 *
799ca8a282aSKuninori Morimoto 	 * This driver re-initializes all registers after
800ca8a282aSKuninori Morimoto 	 * pm_runtime_get_sync() anyway so there is no need
801ca8a282aSKuninori Morimoto 	 * to save and restore registers here.
802ca8a282aSKuninori Morimoto 	 */
803ca8a282aSKuninori Morimoto 	return 0;
804ca8a282aSKuninori Morimoto }
805ca8a282aSKuninori Morimoto 
806ca8a282aSKuninori Morimoto static const struct dev_pm_ops usbhsc_pm_ops = {
807ca8a282aSKuninori Morimoto 	.suspend		= usbhsc_suspend,
808ca8a282aSKuninori Morimoto 	.resume			= usbhsc_resume,
809ca8a282aSKuninori Morimoto 	.runtime_suspend	= usbhsc_runtime_nop,
810ca8a282aSKuninori Morimoto 	.runtime_resume		= usbhsc_runtime_nop,
811ca8a282aSKuninori Morimoto };
812ca8a282aSKuninori Morimoto 
813f1407d5cSKuninori Morimoto static struct platform_driver renesas_usbhs_driver = {
814f1407d5cSKuninori Morimoto 	.driver		= {
815f1407d5cSKuninori Morimoto 		.name	= "renesas_usbhs",
816ca8a282aSKuninori Morimoto 		.pm	= &usbhsc_pm_ops,
817b854100eSYoshihiro Shimoda 		.of_match_table = of_match_ptr(usbhs_of_match),
818f1407d5cSKuninori Morimoto 	},
819f1407d5cSKuninori Morimoto 	.probe		= usbhs_probe,
8207690417dSBill Pemberton 	.remove		= usbhs_remove,
821f1407d5cSKuninori Morimoto };
822f1407d5cSKuninori Morimoto 
823cc27c96cSAxel Lin module_platform_driver(renesas_usbhs_driver);
824f1407d5cSKuninori Morimoto 
825f1407d5cSKuninori Morimoto MODULE_LICENSE("GPL");
826f1407d5cSKuninori Morimoto MODULE_DESCRIPTION("Renesas USB driver");
827f1407d5cSKuninori Morimoto MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
828