xref: /openbmc/linux/drivers/usb/renesas_usbhs/common.c (revision 51f141a97a1406bb0b59d490e837a39ccb7c3999)
1f1407d5cSKuninori Morimoto /*
2f1407d5cSKuninori Morimoto  * Renesas USB driver
3f1407d5cSKuninori Morimoto  *
4f1407d5cSKuninori Morimoto  * Copyright (C) 2011 Renesas Solutions Corp.
5f1407d5cSKuninori Morimoto  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6f1407d5cSKuninori Morimoto  *
7f1407d5cSKuninori Morimoto  * This program is distributed in the hope that it will be useful,
8f1407d5cSKuninori Morimoto  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9f1407d5cSKuninori Morimoto  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10f1407d5cSKuninori Morimoto  * GNU General Public License for more details.
11f1407d5cSKuninori Morimoto  *
12f1407d5cSKuninori Morimoto  * You should have received a copy of the GNU General Public License
13f1407d5cSKuninori Morimoto  * along with this program; if not, write to the Free Software
14f1407d5cSKuninori Morimoto  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15f1407d5cSKuninori Morimoto  *
16f1407d5cSKuninori Morimoto  */
17148e1134SThierry Reding #include <linux/err.h>
188ecef00fSUlrich Hecht #include <linux/gpio.h>
19f1407d5cSKuninori Morimoto #include <linux/io.h>
20f1407d5cSKuninori Morimoto #include <linux/module.h>
21b854100eSYoshihiro Shimoda #include <linux/of_device.h>
22b854100eSYoshihiro Shimoda #include <linux/of_gpio.h>
23f1407d5cSKuninori Morimoto #include <linux/pm_runtime.h>
24f1407d5cSKuninori Morimoto #include <linux/slab.h>
25f1407d5cSKuninori Morimoto #include <linux/sysfs.h>
26cc502bb7SPaul Bolle #include "common.h"
278ecef00fSUlrich Hecht #include "rcar2.h"
28f1407d5cSKuninori Morimoto 
29233f519dSKuninori Morimoto /*
30233f519dSKuninori Morimoto  *		image of renesas_usbhs
31233f519dSKuninori Morimoto  *
32233f519dSKuninori Morimoto  * ex) gadget case
33233f519dSKuninori Morimoto 
34233f519dSKuninori Morimoto  * mod.c
35233f519dSKuninori Morimoto  * mod_gadget.c
36233f519dSKuninori Morimoto  * mod_host.c		pipe.c		fifo.c
37233f519dSKuninori Morimoto  *
38233f519dSKuninori Morimoto  *			+-------+	+-----------+
39233f519dSKuninori Morimoto  *			| pipe0 |------>| fifo pio  |
40233f519dSKuninori Morimoto  * +------------+	+-------+	+-----------+
41233f519dSKuninori Morimoto  * | mod_gadget |=====> | pipe1 |--+
42233f519dSKuninori Morimoto  * +------------+	+-------+  |	+-----------+
43233f519dSKuninori Morimoto  *			| pipe2 |  |  +-| fifo dma0 |
44233f519dSKuninori Morimoto  * +------------+	+-------+  |  |	+-----------+
45233f519dSKuninori Morimoto  * | mod_host   |	| pipe3 |<-|--+
46233f519dSKuninori Morimoto  * +------------+	+-------+  |	+-----------+
47233f519dSKuninori Morimoto  *			| ....  |  +--->| fifo dma1 |
48233f519dSKuninori Morimoto  *			| ....  |	+-----------+
49233f519dSKuninori Morimoto  */
50233f519dSKuninori Morimoto 
51233f519dSKuninori Morimoto 
52b002ff6eSKuninori Morimoto #define USBHSF_RUNTIME_PWCTRL	(1 << 0)
53b002ff6eSKuninori Morimoto 
54b002ff6eSKuninori Morimoto /* status */
55b002ff6eSKuninori Morimoto #define usbhsc_flags_init(p)   do {(p)->flags = 0; } while (0)
56b002ff6eSKuninori Morimoto #define usbhsc_flags_set(p, b) ((p)->flags |=  (b))
57b002ff6eSKuninori Morimoto #define usbhsc_flags_clr(p, b) ((p)->flags &= ~(b))
58b002ff6eSKuninori Morimoto #define usbhsc_flags_has(p, b) ((p)->flags &   (b))
59b002ff6eSKuninori Morimoto 
60f1407d5cSKuninori Morimoto /*
61f1407d5cSKuninori Morimoto  * platform call back
62f1407d5cSKuninori Morimoto  *
63f1407d5cSKuninori Morimoto  * renesas usb support platform callback function.
64f1407d5cSKuninori Morimoto  * Below macro call it.
65f1407d5cSKuninori Morimoto  * if platform doesn't have callback, it return 0 (no error)
66f1407d5cSKuninori Morimoto  */
67f1407d5cSKuninori Morimoto #define usbhs_platform_call(priv, func, args...)\
68f1407d5cSKuninori Morimoto 	(!(priv) ? -ENODEV :			\
6948298206SKuninori Morimoto 	 !((priv)->pfunc.func) ? 0 :		\
7048298206SKuninori Morimoto 	 (priv)->pfunc.func(args))
71f1407d5cSKuninori Morimoto 
72f1407d5cSKuninori Morimoto /*
73f1407d5cSKuninori Morimoto  *		common functions
74f1407d5cSKuninori Morimoto  */
75f1407d5cSKuninori Morimoto u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
76f1407d5cSKuninori Morimoto {
77f1407d5cSKuninori Morimoto 	return ioread16(priv->base + reg);
78f1407d5cSKuninori Morimoto }
79f1407d5cSKuninori Morimoto 
80f1407d5cSKuninori Morimoto void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
81f1407d5cSKuninori Morimoto {
82f1407d5cSKuninori Morimoto 	iowrite16(data, priv->base + reg);
83f1407d5cSKuninori Morimoto }
84f1407d5cSKuninori Morimoto 
85f1407d5cSKuninori Morimoto void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
86f1407d5cSKuninori Morimoto {
87f1407d5cSKuninori Morimoto 	u16 val = usbhs_read(priv, reg);
88f1407d5cSKuninori Morimoto 
89f1407d5cSKuninori Morimoto 	val &= ~mask;
90f1407d5cSKuninori Morimoto 	val |= data & mask;
91f1407d5cSKuninori Morimoto 
92f1407d5cSKuninori Morimoto 	usbhs_write(priv, reg, val);
93f1407d5cSKuninori Morimoto }
94f1407d5cSKuninori Morimoto 
95206dcc2cSKuninori Morimoto struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
96206dcc2cSKuninori Morimoto {
97206dcc2cSKuninori Morimoto 	return dev_get_drvdata(&pdev->dev);
98206dcc2cSKuninori Morimoto }
99206dcc2cSKuninori Morimoto 
100f1407d5cSKuninori Morimoto /*
101f1407d5cSKuninori Morimoto  *		syscfg functions
102f1407d5cSKuninori Morimoto  */
10376190152SKuninori Morimoto static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
104f1407d5cSKuninori Morimoto {
105f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
106f1407d5cSKuninori Morimoto }
107f1407d5cSKuninori Morimoto 
108f1407d5cSKuninori Morimoto void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
109f1407d5cSKuninori Morimoto {
1103244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
1113244a7b4SKuninori Morimoto 	u16 val  = DCFM | DRPD | HSE | USBE;
112f427eb64SKuninori Morimoto 	int has_otg = usbhs_get_dparam(priv, has_otg);
113f427eb64SKuninori Morimoto 
114f427eb64SKuninori Morimoto 	if (has_otg)
115f427eb64SKuninori Morimoto 		usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN));
116f1407d5cSKuninori Morimoto 
117f1407d5cSKuninori Morimoto 	/*
118f1407d5cSKuninori Morimoto 	 * if enable
119f1407d5cSKuninori Morimoto 	 *
120f1407d5cSKuninori Morimoto 	 * - select Host mode
121f1407d5cSKuninori Morimoto 	 * - D+ Line/D- Line Pull-down
122f1407d5cSKuninori Morimoto 	 */
123f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
124f1407d5cSKuninori Morimoto }
125f1407d5cSKuninori Morimoto 
126f1407d5cSKuninori Morimoto void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
127f1407d5cSKuninori Morimoto {
1283244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
12904a5def3STakeshi Kihara 	u16 val  = HSE | USBE;
130f1407d5cSKuninori Morimoto 
131f1407d5cSKuninori Morimoto 	/*
132f1407d5cSKuninori Morimoto 	 * if enable
133f1407d5cSKuninori Morimoto 	 *
134f1407d5cSKuninori Morimoto 	 * - select Function mode
13504a5def3STakeshi Kihara 	 * - D+ Line Pull-up is disabled
13604a5def3STakeshi Kihara 	 *      When D+ Line Pull-up is enabled,
13704a5def3STakeshi Kihara 	 *      calling usbhs_sys_function_pullup(,1)
138f1407d5cSKuninori Morimoto 	 */
139f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
140f1407d5cSKuninori Morimoto }
141f1407d5cSKuninori Morimoto 
1424cd2f599Skuninori.morimoto.gx@renesas.com void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable)
1434cd2f599Skuninori.morimoto.gx@renesas.com {
1444cd2f599Skuninori.morimoto.gx@renesas.com 	usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0);
1454cd2f599Skuninori.morimoto.gx@renesas.com }
1464cd2f599Skuninori.morimoto.gx@renesas.com 
147dfbb7f4fSKuninori Morimoto void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
148dfbb7f4fSKuninori Morimoto {
149dfbb7f4fSKuninori Morimoto 	usbhs_write(priv, TESTMODE, mode);
150dfbb7f4fSKuninori Morimoto }
151dfbb7f4fSKuninori Morimoto 
152f1407d5cSKuninori Morimoto /*
153f1407d5cSKuninori Morimoto  *		frame functions
154f1407d5cSKuninori Morimoto  */
155f1407d5cSKuninori Morimoto int usbhs_frame_get_num(struct usbhs_priv *priv)
156f1407d5cSKuninori Morimoto {
157f1407d5cSKuninori Morimoto 	return usbhs_read(priv, FRMNUM) & FRNM_MASK;
158f1407d5cSKuninori Morimoto }
159f1407d5cSKuninori Morimoto 
160f1407d5cSKuninori Morimoto /*
161ef8bedb9SKuninori Morimoto  *		usb request functions
162ef8bedb9SKuninori Morimoto  */
163ef8bedb9SKuninori Morimoto void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
164ef8bedb9SKuninori Morimoto {
165ef8bedb9SKuninori Morimoto 	u16 val;
166ef8bedb9SKuninori Morimoto 
167ef8bedb9SKuninori Morimoto 	val = usbhs_read(priv, USBREQ);
168ef8bedb9SKuninori Morimoto 	req->bRequest		= (val >> 8) & 0xFF;
169ef8bedb9SKuninori Morimoto 	req->bRequestType	= (val >> 0) & 0xFF;
170ef8bedb9SKuninori Morimoto 
171ef8bedb9SKuninori Morimoto 	req->wValue	= usbhs_read(priv, USBVAL);
172ef8bedb9SKuninori Morimoto 	req->wIndex	= usbhs_read(priv, USBINDX);
173ef8bedb9SKuninori Morimoto 	req->wLength	= usbhs_read(priv, USBLENG);
174ef8bedb9SKuninori Morimoto }
175ef8bedb9SKuninori Morimoto 
176ef8bedb9SKuninori Morimoto void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
177ef8bedb9SKuninori Morimoto {
178ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBREQ,  (req->bRequest << 8) | req->bRequestType);
179ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBVAL,  req->wValue);
180ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBINDX, req->wIndex);
181ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBLENG, req->wLength);
182ef8bedb9SKuninori Morimoto 
183ef8bedb9SKuninori Morimoto 	usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
184ef8bedb9SKuninori Morimoto }
185ef8bedb9SKuninori Morimoto 
186ef8bedb9SKuninori Morimoto /*
187258485d9SKuninori Morimoto  *		bus/vbus functions
188258485d9SKuninori Morimoto  */
189258485d9SKuninori Morimoto void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
190258485d9SKuninori Morimoto {
191a9be4a45SKuninori Morimoto 	u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
192a9be4a45SKuninori Morimoto 
193a9be4a45SKuninori Morimoto 	if (status != USBRST) {
194a9be4a45SKuninori Morimoto 		struct device *dev = usbhs_priv_to_dev(priv);
195a9be4a45SKuninori Morimoto 		dev_err(dev, "usbhs should be reset\n");
196a9be4a45SKuninori Morimoto 	}
197a9be4a45SKuninori Morimoto 
198258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
199258485d9SKuninori Morimoto }
200258485d9SKuninori Morimoto 
201258485d9SKuninori Morimoto void usbhs_bus_send_reset(struct usbhs_priv *priv)
202258485d9SKuninori Morimoto {
203258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
204258485d9SKuninori Morimoto }
205258485d9SKuninori Morimoto 
20675587f52SKuninori Morimoto int usbhs_bus_get_speed(struct usbhs_priv *priv)
20775587f52SKuninori Morimoto {
20875587f52SKuninori Morimoto 	u16 dvstctr = usbhs_read(priv, DVSTCTR);
20975587f52SKuninori Morimoto 
21075587f52SKuninori Morimoto 	switch (RHST & dvstctr) {
21175587f52SKuninori Morimoto 	case RHST_LOW_SPEED:
21275587f52SKuninori Morimoto 		return USB_SPEED_LOW;
21375587f52SKuninori Morimoto 	case RHST_FULL_SPEED:
21475587f52SKuninori Morimoto 		return USB_SPEED_FULL;
21575587f52SKuninori Morimoto 	case RHST_HIGH_SPEED:
21675587f52SKuninori Morimoto 		return USB_SPEED_HIGH;
21775587f52SKuninori Morimoto 	}
21875587f52SKuninori Morimoto 
21975587f52SKuninori Morimoto 	return USB_SPEED_UNKNOWN;
22075587f52SKuninori Morimoto }
22175587f52SKuninori Morimoto 
222258485d9SKuninori Morimoto int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
223258485d9SKuninori Morimoto {
224258485d9SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
225258485d9SKuninori Morimoto 
226258485d9SKuninori Morimoto 	return usbhs_platform_call(priv, set_vbus, pdev, enable);
227258485d9SKuninori Morimoto }
228258485d9SKuninori Morimoto 
229258485d9SKuninori Morimoto static void usbhsc_bus_init(struct usbhs_priv *priv)
230258485d9SKuninori Morimoto {
231258485d9SKuninori Morimoto 	usbhs_write(priv, DVSTCTR, 0);
232258485d9SKuninori Morimoto 
233258485d9SKuninori Morimoto 	usbhs_vbus_ctrl(priv, 0);
234258485d9SKuninori Morimoto }
235258485d9SKuninori Morimoto 
236258485d9SKuninori Morimoto /*
237eb05191fSKuninori Morimoto  *		device configuration
238eb05191fSKuninori Morimoto  */
2393dd49268SKuninori Morimoto int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
240eb05191fSKuninori Morimoto 			   u16 upphub, u16 hubport, u16 speed)
241eb05191fSKuninori Morimoto {
242eb05191fSKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
243eb05191fSKuninori Morimoto 	u16 usbspd = 0;
244eb05191fSKuninori Morimoto 	u32 reg = DEVADD0 + (2 * devnum);
245eb05191fSKuninori Morimoto 
246eb05191fSKuninori Morimoto 	if (devnum > 10) {
247eb05191fSKuninori Morimoto 		dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
248eb05191fSKuninori Morimoto 		return -EIO;
249eb05191fSKuninori Morimoto 	}
250eb05191fSKuninori Morimoto 
251eb05191fSKuninori Morimoto 	if (upphub > 0xA) {
252eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported hub number %d\n", upphub);
253eb05191fSKuninori Morimoto 		return -EIO;
254eb05191fSKuninori Morimoto 	}
255eb05191fSKuninori Morimoto 
256eb05191fSKuninori Morimoto 	switch (speed) {
257eb05191fSKuninori Morimoto 	case USB_SPEED_LOW:
258eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_LOW;
259eb05191fSKuninori Morimoto 		break;
260eb05191fSKuninori Morimoto 	case USB_SPEED_FULL:
261eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_FULL;
262eb05191fSKuninori Morimoto 		break;
263eb05191fSKuninori Morimoto 	case USB_SPEED_HIGH:
264eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_HIGH;
265eb05191fSKuninori Morimoto 		break;
266eb05191fSKuninori Morimoto 	default:
267eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported speed %d\n", speed);
268eb05191fSKuninori Morimoto 		return -EIO;
269eb05191fSKuninori Morimoto 	}
270eb05191fSKuninori Morimoto 
271eb05191fSKuninori Morimoto 	usbhs_write(priv, reg,	UPPHUB(upphub)	|
272eb05191fSKuninori Morimoto 				HUBPORT(hubport)|
273eb05191fSKuninori Morimoto 				USBSPD(usbspd));
274eb05191fSKuninori Morimoto 
275eb05191fSKuninori Morimoto 	return 0;
276eb05191fSKuninori Morimoto }
277eb05191fSKuninori Morimoto 
278eb05191fSKuninori Morimoto /*
279ab330cf3SYoshihiro Shimoda  *		interrupt functions
280ab330cf3SYoshihiro Shimoda  */
281ab330cf3SYoshihiro Shimoda void usbhs_xxxsts_clear(struct usbhs_priv *priv, u16 sts_reg, u16 bit)
282ab330cf3SYoshihiro Shimoda {
283ab330cf3SYoshihiro Shimoda 	u16 pipe_mask = (u16)GENMASK(usbhs_get_dparam(priv, pipe_size), 0);
284ab330cf3SYoshihiro Shimoda 
285ab330cf3SYoshihiro Shimoda 	usbhs_write(priv, sts_reg, ~(1 << bit) & pipe_mask);
286ab330cf3SYoshihiro Shimoda }
287ab330cf3SYoshihiro Shimoda 
288ab330cf3SYoshihiro Shimoda /*
289f1407d5cSKuninori Morimoto  *		local functions
290f1407d5cSKuninori Morimoto  */
29111935de5SKuninori Morimoto static void usbhsc_set_buswait(struct usbhs_priv *priv)
292f1407d5cSKuninori Morimoto {
293f1407d5cSKuninori Morimoto 	int wait = usbhs_get_dparam(priv, buswait_bwait);
294f1407d5cSKuninori Morimoto 
295f1407d5cSKuninori Morimoto 	/* set bus wait if platform have */
296f1407d5cSKuninori Morimoto 	if (wait)
297f1407d5cSKuninori Morimoto 		usbhs_bset(priv, BUSWAIT, 0x000F, wait);
298f1407d5cSKuninori Morimoto }
299f1407d5cSKuninori Morimoto 
300f1407d5cSKuninori Morimoto /*
301f1407d5cSKuninori Morimoto  *		platform default param
302f1407d5cSKuninori Morimoto  */
3038ecef00fSUlrich Hecht 
3048ecef00fSUlrich Hecht /* commonly used on old SH-Mobile SoCs */
305*51f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_default_pipe[] = {
306*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
307*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, false),
308*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x18, false),
309*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x28, true),
310*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x38, true),
311*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
312*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
313*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
314*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
315*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x07, false),
316f1407d5cSKuninori Morimoto };
317f1407d5cSKuninori Morimoto 
3188ecef00fSUlrich Hecht /* commonly used on newer SH-Mobile and R-Car SoCs */
319*51f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_new_pipe[] = {
320*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
321*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true),
322*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true),
323*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
324*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true),
325*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true),
326*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
327*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
328*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
329*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x78, true),
330*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x88, true),
331*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x98, true),
332*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xa8, true),
333*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xb8, true),
334*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xc8, true),
335*51f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xd8, true),
3368ecef00fSUlrich Hecht };
3378ecef00fSUlrich Hecht 
338f1407d5cSKuninori Morimoto /*
3396e267da8SKuninori Morimoto  *		power control
3406e267da8SKuninori Morimoto  */
3416e267da8SKuninori Morimoto static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
3426e267da8SKuninori Morimoto {
343f1ee56a0SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
3446e267da8SKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
3456e267da8SKuninori Morimoto 
3466e267da8SKuninori Morimoto 	if (enable) {
3476e267da8SKuninori Morimoto 		/* enable PM */
3486e267da8SKuninori Morimoto 		pm_runtime_get_sync(dev);
3496e267da8SKuninori Morimoto 
350f1ee56a0SKuninori Morimoto 		/* enable platform power */
351f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
352f1ee56a0SKuninori Morimoto 
3536e267da8SKuninori Morimoto 		/* USB on */
3546e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
3556e267da8SKuninori Morimoto 	} else {
3566e267da8SKuninori Morimoto 		/* USB off */
3576e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
3586e267da8SKuninori Morimoto 
359f1ee56a0SKuninori Morimoto 		/* disable platform power */
360f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
361f1ee56a0SKuninori Morimoto 
3626e267da8SKuninori Morimoto 		/* disable PM */
3636e267da8SKuninori Morimoto 		pm_runtime_put_sync(dev);
3646e267da8SKuninori Morimoto 	}
3656e267da8SKuninori Morimoto }
3666e267da8SKuninori Morimoto 
3676e267da8SKuninori Morimoto /*
368ca8a282aSKuninori Morimoto  *		hotplug
369f1407d5cSKuninori Morimoto  */
370ca8a282aSKuninori Morimoto static void usbhsc_hotplug(struct usbhs_priv *priv)
371f1407d5cSKuninori Morimoto {
372f1407d5cSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
373f1407d5cSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
374f1407d5cSKuninori Morimoto 	int id;
375f1407d5cSKuninori Morimoto 	int enable;
376d7b3968dSSergei Shtylyov 	int cable;
377f1407d5cSKuninori Morimoto 	int ret;
378f1407d5cSKuninori Morimoto 
379f1407d5cSKuninori Morimoto 	/*
380f1407d5cSKuninori Morimoto 	 * get vbus status from platform
381f1407d5cSKuninori Morimoto 	 */
382f1407d5cSKuninori Morimoto 	enable = usbhs_platform_call(priv, get_vbus, pdev);
383f1407d5cSKuninori Morimoto 
384f1407d5cSKuninori Morimoto 	/*
385f1407d5cSKuninori Morimoto 	 * get id from platform
386f1407d5cSKuninori Morimoto 	 */
387f1407d5cSKuninori Morimoto 	id = usbhs_platform_call(priv, get_id, pdev);
388f1407d5cSKuninori Morimoto 
389f1407d5cSKuninori Morimoto 	if (enable && !mod) {
390d7b3968dSSergei Shtylyov 		if (priv->edev) {
39150297b79SChanwoo Choi 			cable = extcon_get_cable_state_(priv->edev, EXTCON_USB_HOST);
392d7b3968dSSergei Shtylyov 			if ((cable > 0 && id != USBHS_HOST) ||
393d7b3968dSSergei Shtylyov 			    (!cable && id != USBHS_GADGET)) {
394d7b3968dSSergei Shtylyov 				dev_info(&pdev->dev,
395d7b3968dSSergei Shtylyov 					 "USB cable plugged in doesn't match the selected role!\n");
396d7b3968dSSergei Shtylyov 				return;
397d7b3968dSSergei Shtylyov 			}
398d7b3968dSSergei Shtylyov 		}
399d7b3968dSSergei Shtylyov 
400f1407d5cSKuninori Morimoto 		ret = usbhs_mod_change(priv, id);
401f1407d5cSKuninori Morimoto 		if (ret < 0)
402f1407d5cSKuninori Morimoto 			return;
403f1407d5cSKuninori Morimoto 
404f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s enable\n", __func__);
405f1407d5cSKuninori Morimoto 
4066e267da8SKuninori Morimoto 		/* power on */
407b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
4086e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
409f1407d5cSKuninori Morimoto 
410258485d9SKuninori Morimoto 		/* bus init */
411258485d9SKuninori Morimoto 		usbhsc_set_buswait(priv);
412258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
413258485d9SKuninori Morimoto 
414f1407d5cSKuninori Morimoto 		/* module start */
415f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, start, priv);
416f1407d5cSKuninori Morimoto 
417f1407d5cSKuninori Morimoto 	} else if (!enable && mod) {
418f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s disable\n", __func__);
419f1407d5cSKuninori Morimoto 
420f1407d5cSKuninori Morimoto 		/* module stop */
421f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
422f1407d5cSKuninori Morimoto 
423258485d9SKuninori Morimoto 		/* bus init */
424258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
425258485d9SKuninori Morimoto 
4266e267da8SKuninori Morimoto 		/* power off */
427b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
4286e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
429f1407d5cSKuninori Morimoto 
430f1407d5cSKuninori Morimoto 		usbhs_mod_change(priv, -1);
431f1407d5cSKuninori Morimoto 
432f1407d5cSKuninori Morimoto 		/* reset phy for next connection */
433f1407d5cSKuninori Morimoto 		usbhs_platform_call(priv, phy_reset, pdev);
434f1407d5cSKuninori Morimoto 	}
435f1407d5cSKuninori Morimoto }
436f1407d5cSKuninori Morimoto 
437ca8a282aSKuninori Morimoto /*
438ca8a282aSKuninori Morimoto  *		notify hotplug
439ca8a282aSKuninori Morimoto  */
440ca8a282aSKuninori Morimoto static void usbhsc_notify_hotplug(struct work_struct *work)
441ca8a282aSKuninori Morimoto {
442ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = container_of(work,
443ca8a282aSKuninori Morimoto 					       struct usbhs_priv,
444ca8a282aSKuninori Morimoto 					       notify_hotplug_work.work);
445ca8a282aSKuninori Morimoto 	usbhsc_hotplug(priv);
446ca8a282aSKuninori Morimoto }
447ca8a282aSKuninori Morimoto 
448b4fcea2aSKuninori Morimoto static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
449f1407d5cSKuninori Morimoto {
450206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
451bc57381eSKuninori Morimoto 	int delay = usbhs_get_dparam(priv, detection_delay);
452f1407d5cSKuninori Morimoto 
453f1407d5cSKuninori Morimoto 	/*
454f1407d5cSKuninori Morimoto 	 * This functions will be called in interrupt.
455f1407d5cSKuninori Morimoto 	 * To make sure safety context,
456f1407d5cSKuninori Morimoto 	 * use workqueue for usbhs_notify_hotplug
457f1407d5cSKuninori Morimoto 	 */
458a49a88f1SKuninori Morimoto 	schedule_delayed_work(&priv->notify_hotplug_work,
459a49a88f1SKuninori Morimoto 			      msecs_to_jiffies(delay));
460f1407d5cSKuninori Morimoto 	return 0;
461f1407d5cSKuninori Morimoto }
462f1407d5cSKuninori Morimoto 
463f1407d5cSKuninori Morimoto /*
464f1407d5cSKuninori Morimoto  *		platform functions
465f1407d5cSKuninori Morimoto  */
466b854100eSYoshihiro Shimoda static const struct of_device_id usbhs_of_match[] = {
467b854100eSYoshihiro Shimoda 	{
468b854100eSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7790",
469e0213bc5SYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
470b854100eSYoshihiro Shimoda 	},
471b854100eSYoshihiro Shimoda 	{
472b854100eSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7791",
473e0213bc5SYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
474b854100eSYoshihiro Shimoda 	},
475af6e613bSYoshihiro Shimoda 	{
476af6e613bSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7794",
477af6e613bSYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
478af6e613bSYoshihiro Shimoda 	},
479f5f6afa8SYoshihiro Shimoda 	{
480f5f6afa8SYoshihiro Shimoda 		/* Gen3 is compatible with Gen2 */
481f5f6afa8SYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7795",
482f5f6afa8SYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
483f5f6afa8SYoshihiro Shimoda 	},
484b854100eSYoshihiro Shimoda 	{ },
485b854100eSYoshihiro Shimoda };
486b854100eSYoshihiro Shimoda MODULE_DEVICE_TABLE(of, usbhs_of_match);
487b854100eSYoshihiro Shimoda 
488b854100eSYoshihiro Shimoda static struct renesas_usbhs_platform_info *usbhs_parse_dt(struct device *dev)
489b854100eSYoshihiro Shimoda {
490b854100eSYoshihiro Shimoda 	struct renesas_usbhs_platform_info *info;
491b854100eSYoshihiro Shimoda 	struct renesas_usbhs_driver_param *dparam;
492b854100eSYoshihiro Shimoda 	const struct of_device_id *of_id = of_match_device(usbhs_of_match, dev);
493b854100eSYoshihiro Shimoda 	u32 tmp;
494b854100eSYoshihiro Shimoda 	int gpio;
495b854100eSYoshihiro Shimoda 
496b854100eSYoshihiro Shimoda 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
497b854100eSYoshihiro Shimoda 	if (!info)
498b854100eSYoshihiro Shimoda 		return NULL;
499b854100eSYoshihiro Shimoda 
500b854100eSYoshihiro Shimoda 	dparam = &info->driver_param;
5019ae7ce00SYoshihiro Shimoda 	dparam->type = of_id ? (uintptr_t)of_id->data : 0;
502b854100eSYoshihiro Shimoda 	if (!of_property_read_u32(dev->of_node, "renesas,buswait", &tmp))
503b854100eSYoshihiro Shimoda 		dparam->buswait_bwait = tmp;
504b854100eSYoshihiro Shimoda 	gpio = of_get_named_gpio_flags(dev->of_node, "renesas,enable-gpio", 0,
505b854100eSYoshihiro Shimoda 				       NULL);
506b854100eSYoshihiro Shimoda 	if (gpio > 0)
507b854100eSYoshihiro Shimoda 		dparam->enable_gpio = gpio;
508b854100eSYoshihiro Shimoda 
509e0213bc5SYoshihiro Shimoda 	if (dparam->type == USBHS_TYPE_RCAR_GEN2)
510ab330cf3SYoshihiro Shimoda 		dparam->has_usb_dmac = 1;
511ab330cf3SYoshihiro Shimoda 
512b854100eSYoshihiro Shimoda 	return info;
513b854100eSYoshihiro Shimoda }
514b854100eSYoshihiro Shimoda 
515b7a8d17dSKuninori Morimoto static int usbhs_probe(struct platform_device *pdev)
516f1407d5cSKuninori Morimoto {
517ace0a5f9SJingoo Han 	struct renesas_usbhs_platform_info *info = dev_get_platdata(&pdev->dev);
518f1407d5cSKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc;
519f1407d5cSKuninori Morimoto 	struct usbhs_priv *priv;
52053069af3SShimoda, Yoshihiro 	struct resource *res, *irq_res;
521f1407d5cSKuninori Morimoto 	int ret;
522f1407d5cSKuninori Morimoto 
523b854100eSYoshihiro Shimoda 	/* check device node */
524b854100eSYoshihiro Shimoda 	if (pdev->dev.of_node)
525b854100eSYoshihiro Shimoda 		info = pdev->dev.platform_data = usbhs_parse_dt(&pdev->dev);
526b854100eSYoshihiro Shimoda 
527f1407d5cSKuninori Morimoto 	/* check platform information */
5288ecef00fSUlrich Hecht 	if (!info) {
529f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "no platform information\n");
530f1407d5cSKuninori Morimoto 		return -EINVAL;
531f1407d5cSKuninori Morimoto 	}
532f1407d5cSKuninori Morimoto 
533f1407d5cSKuninori Morimoto 	/* platform data */
53453069af3SShimoda, Yoshihiro 	irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
535687f16b8SVarka Bhadram 	if (!irq_res) {
536f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
537f1407d5cSKuninori Morimoto 		return -ENODEV;
538f1407d5cSKuninori Morimoto 	}
539f1407d5cSKuninori Morimoto 
540f1407d5cSKuninori Morimoto 	/* usb private data */
54158efc77cSKuninori Morimoto 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
542b1cb07cdSPeter Chen 	if (!priv)
543f1407d5cSKuninori Morimoto 		return -ENOMEM;
544f1407d5cSKuninori Morimoto 
545687f16b8SVarka Bhadram 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
546148e1134SThierry Reding 	priv->base = devm_ioremap_resource(&pdev->dev, res);
547148e1134SThierry Reding 	if (IS_ERR(priv->base))
548148e1134SThierry Reding 		return PTR_ERR(priv->base);
549f1407d5cSKuninori Morimoto 
550d7b3968dSSergei Shtylyov 	if (of_property_read_bool(pdev->dev.of_node, "extcon")) {
551d7b3968dSSergei Shtylyov 		priv->edev = extcon_get_edev_by_phandle(&pdev->dev, 0);
552d7b3968dSSergei Shtylyov 		if (IS_ERR(priv->edev))
553d7b3968dSSergei Shtylyov 			return PTR_ERR(priv->edev);
554d7b3968dSSergei Shtylyov 	}
555d7b3968dSSergei Shtylyov 
556f1407d5cSKuninori Morimoto 	/*
557f1407d5cSKuninori Morimoto 	 * care platform info
558f1407d5cSKuninori Morimoto 	 */
5598ecef00fSUlrich Hecht 
56048298206SKuninori Morimoto 	memcpy(&priv->dparam,
56148298206SKuninori Morimoto 	       &info->driver_param,
56248298206SKuninori Morimoto 	       sizeof(struct renesas_usbhs_driver_param));
563f1407d5cSKuninori Morimoto 
5648ecef00fSUlrich Hecht 	switch (priv->dparam.type) {
565e0213bc5SYoshihiro Shimoda 	case USBHS_TYPE_RCAR_GEN2:
5668ecef00fSUlrich Hecht 		priv->pfunc = usbhs_rcar2_ops;
567*51f141a9SYoshihiro Shimoda 		if (!priv->dparam.pipe_configs) {
568*51f141a9SYoshihiro Shimoda 			priv->dparam.pipe_configs = usbhsc_new_pipe;
569*51f141a9SYoshihiro Shimoda 			priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
5708ecef00fSUlrich Hecht 		}
5718ecef00fSUlrich Hecht 		break;
5728ecef00fSUlrich Hecht 	default:
5738ecef00fSUlrich Hecht 		if (!info->platform_callback.get_id) {
5748ecef00fSUlrich Hecht 			dev_err(&pdev->dev, "no platform callbacks");
5758ecef00fSUlrich Hecht 			return -EINVAL;
5768ecef00fSUlrich Hecht 		}
5778ecef00fSUlrich Hecht 		memcpy(&priv->pfunc,
5788ecef00fSUlrich Hecht 		       &info->platform_callback,
5798ecef00fSUlrich Hecht 		       sizeof(struct renesas_usbhs_platform_callback));
5808ecef00fSUlrich Hecht 		break;
5818ecef00fSUlrich Hecht 	}
5828ecef00fSUlrich Hecht 
583f1407d5cSKuninori Morimoto 	/* set driver callback functions for platform */
584f1407d5cSKuninori Morimoto 	dfunc			= &info->driver_callback;
585f1407d5cSKuninori Morimoto 	dfunc->notify_hotplug	= usbhsc_drvcllbck_notify_hotplug;
586f1407d5cSKuninori Morimoto 
587f1407d5cSKuninori Morimoto 	/* set default param if platform doesn't have */
588*51f141a9SYoshihiro Shimoda 	if (!priv->dparam.pipe_configs) {
589*51f141a9SYoshihiro Shimoda 		priv->dparam.pipe_configs = usbhsc_default_pipe;
590*51f141a9SYoshihiro Shimoda 		priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe);
591f1407d5cSKuninori Morimoto 	}
59248298206SKuninori Morimoto 	if (!priv->dparam.pio_dma_border)
59348298206SKuninori Morimoto 		priv->dparam.pio_dma_border = 64; /* 64byte */
594f1407d5cSKuninori Morimoto 
595b002ff6eSKuninori Morimoto 	/* FIXME */
596b002ff6eSKuninori Morimoto 	/* runtime power control ? */
59748298206SKuninori Morimoto 	if (priv->pfunc.get_vbus)
598b002ff6eSKuninori Morimoto 		usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
599b002ff6eSKuninori Morimoto 
600f1407d5cSKuninori Morimoto 	/*
601f1407d5cSKuninori Morimoto 	 * priv settings
602f1407d5cSKuninori Morimoto 	 */
60353069af3SShimoda, Yoshihiro 	priv->irq	= irq_res->start;
60453069af3SShimoda, Yoshihiro 	if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
60553069af3SShimoda, Yoshihiro 		priv->irqflags = IRQF_SHARED;
606f1407d5cSKuninori Morimoto 	priv->pdev	= pdev;
607bc57381eSKuninori Morimoto 	INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
608f1407d5cSKuninori Morimoto 	spin_lock_init(usbhs_priv_to_lock(priv));
609f1407d5cSKuninori Morimoto 
610f1407d5cSKuninori Morimoto 	/* call pipe and module init */
611f1407d5cSKuninori Morimoto 	ret = usbhs_pipe_probe(priv);
612f1407d5cSKuninori Morimoto 	if (ret < 0)
61358efc77cSKuninori Morimoto 		return ret;
614f1407d5cSKuninori Morimoto 
615d3af90a5SKuninori Morimoto 	ret = usbhs_fifo_probe(priv);
616f1407d5cSKuninori Morimoto 	if (ret < 0)
61797f93227SKuninori Morimoto 		goto probe_end_pipe_exit;
618f1407d5cSKuninori Morimoto 
619d3af90a5SKuninori Morimoto 	ret = usbhs_mod_probe(priv);
620d3af90a5SKuninori Morimoto 	if (ret < 0)
621d3af90a5SKuninori Morimoto 		goto probe_end_fifo_exit;
622d3af90a5SKuninori Morimoto 
623f1407d5cSKuninori Morimoto 	/* dev_set_drvdata should be called after usbhs_mod_init */
624c9a0552eSLibo Chen 	platform_set_drvdata(pdev, priv);
625f1407d5cSKuninori Morimoto 
626f1407d5cSKuninori Morimoto 	/*
627f1407d5cSKuninori Morimoto 	 * deviece reset here because
628f1407d5cSKuninori Morimoto 	 * USB device might be used in boot loader.
629f1407d5cSKuninori Morimoto 	 */
630f1407d5cSKuninori Morimoto 	usbhs_sys_clock_ctrl(priv, 0);
631f1407d5cSKuninori Morimoto 
6328ecef00fSUlrich Hecht 	/* check GPIO determining if USB function should be enabled */
6338ecef00fSUlrich Hecht 	if (priv->dparam.enable_gpio) {
6348ecef00fSUlrich Hecht 		gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL);
6358ecef00fSUlrich Hecht 		ret = !gpio_get_value(priv->dparam.enable_gpio);
6368ecef00fSUlrich Hecht 		gpio_free(priv->dparam.enable_gpio);
6378ecef00fSUlrich Hecht 		if (ret) {
6388ecef00fSUlrich Hecht 			dev_warn(&pdev->dev,
6398ecef00fSUlrich Hecht 				 "USB function not selected (GPIO %d)\n",
6408ecef00fSUlrich Hecht 				 priv->dparam.enable_gpio);
6418ecef00fSUlrich Hecht 			ret = -ENOTSUPP;
6428ecef00fSUlrich Hecht 			goto probe_end_mod_exit;
6438ecef00fSUlrich Hecht 		}
6448ecef00fSUlrich Hecht 	}
6458ecef00fSUlrich Hecht 
646f1407d5cSKuninori Morimoto 	/*
647f1407d5cSKuninori Morimoto 	 * platform call
648f1407d5cSKuninori Morimoto 	 *
649f1407d5cSKuninori Morimoto 	 * USB phy setup might depend on CPU/Board.
650f1407d5cSKuninori Morimoto 	 * If platform has its callback functions,
651f1407d5cSKuninori Morimoto 	 * call it here.
652f1407d5cSKuninori Morimoto 	 */
653f1407d5cSKuninori Morimoto 	ret = usbhs_platform_call(priv, hardware_init, pdev);
654f1407d5cSKuninori Morimoto 	if (ret < 0) {
65548ac1e57SSergei Shtylyov 		dev_err(&pdev->dev, "platform init failed.\n");
65697f93227SKuninori Morimoto 		goto probe_end_mod_exit;
657f1407d5cSKuninori Morimoto 	}
658f1407d5cSKuninori Morimoto 
659f1407d5cSKuninori Morimoto 	/* reset phy for connection */
660f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
661f1407d5cSKuninori Morimoto 
662b002ff6eSKuninori Morimoto 	/* power control */
663b002ff6eSKuninori Morimoto 	pm_runtime_enable(&pdev->dev);
664b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
665b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
666b002ff6eSKuninori Morimoto 		usbhs_mod_autonomy_mode(priv);
667b002ff6eSKuninori Morimoto 	}
668b002ff6eSKuninori Morimoto 
669f1407d5cSKuninori Morimoto 	/*
670f1407d5cSKuninori Morimoto 	 * manual call notify_hotplug for cold plug
671f1407d5cSKuninori Morimoto 	 */
672368504c0SSergei Shtylyov 	usbhsc_drvcllbck_notify_hotplug(pdev);
673f1407d5cSKuninori Morimoto 
674f1407d5cSKuninori Morimoto 	dev_info(&pdev->dev, "probed\n");
675f1407d5cSKuninori Morimoto 
676f1407d5cSKuninori Morimoto 	return ret;
677f1407d5cSKuninori Morimoto 
678f1407d5cSKuninori Morimoto probe_end_mod_exit:
679f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
680d3af90a5SKuninori Morimoto probe_end_fifo_exit:
681d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
68297f93227SKuninori Morimoto probe_end_pipe_exit:
68397f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
684f1407d5cSKuninori Morimoto 
685f1407d5cSKuninori Morimoto 	dev_info(&pdev->dev, "probe failed\n");
686f1407d5cSKuninori Morimoto 
687f1407d5cSKuninori Morimoto 	return ret;
688f1407d5cSKuninori Morimoto }
689f1407d5cSKuninori Morimoto 
690fb4e98abSBill Pemberton static int usbhs_remove(struct platform_device *pdev)
691f1407d5cSKuninori Morimoto {
692206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
693ace0a5f9SJingoo Han 	struct renesas_usbhs_platform_info *info = dev_get_platdata(&pdev->dev);
694af32fe51SKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
695f1407d5cSKuninori Morimoto 
696f1407d5cSKuninori Morimoto 	dev_dbg(&pdev->dev, "usb remove\n");
697f1407d5cSKuninori Morimoto 
698af32fe51SKuninori Morimoto 	dfunc->notify_hotplug = NULL;
699af32fe51SKuninori Morimoto 
700b002ff6eSKuninori Morimoto 	/* power off */
701b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
702b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
703f1407d5cSKuninori Morimoto 
704b002ff6eSKuninori Morimoto 	pm_runtime_disable(&pdev->dev);
705f1407d5cSKuninori Morimoto 
706f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, hardware_exit, pdev);
707f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
708d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
70997f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
710f1407d5cSKuninori Morimoto 
711f1407d5cSKuninori Morimoto 	return 0;
712f1407d5cSKuninori Morimoto }
713f1407d5cSKuninori Morimoto 
714ca8a282aSKuninori Morimoto static int usbhsc_suspend(struct device *dev)
715ca8a282aSKuninori Morimoto {
716ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
717ca8a282aSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
718ca8a282aSKuninori Morimoto 
719ca8a282aSKuninori Morimoto 	if (mod) {
720ca8a282aSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
721ca8a282aSKuninori Morimoto 		usbhs_mod_change(priv, -1);
722ca8a282aSKuninori Morimoto 	}
723ca8a282aSKuninori Morimoto 
724ca8a282aSKuninori Morimoto 	if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
725ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
726ca8a282aSKuninori Morimoto 
727ca8a282aSKuninori Morimoto 	return 0;
728ca8a282aSKuninori Morimoto }
729ca8a282aSKuninori Morimoto 
730ca8a282aSKuninori Morimoto static int usbhsc_resume(struct device *dev)
731ca8a282aSKuninori Morimoto {
732ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
733ca8a282aSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
734ca8a282aSKuninori Morimoto 
735ca8a282aSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
736ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
737ca8a282aSKuninori Morimoto 
7385b50d3b5SKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
7395b50d3b5SKuninori Morimoto 
7405b50d3b5SKuninori Morimoto 	usbhsc_drvcllbck_notify_hotplug(pdev);
741ca8a282aSKuninori Morimoto 
742ca8a282aSKuninori Morimoto 	return 0;
743ca8a282aSKuninori Morimoto }
744ca8a282aSKuninori Morimoto 
745ca8a282aSKuninori Morimoto static int usbhsc_runtime_nop(struct device *dev)
746ca8a282aSKuninori Morimoto {
747ca8a282aSKuninori Morimoto 	/* Runtime PM callback shared between ->runtime_suspend()
748ca8a282aSKuninori Morimoto 	 * and ->runtime_resume(). Simply returns success.
749ca8a282aSKuninori Morimoto 	 *
750ca8a282aSKuninori Morimoto 	 * This driver re-initializes all registers after
751ca8a282aSKuninori Morimoto 	 * pm_runtime_get_sync() anyway so there is no need
752ca8a282aSKuninori Morimoto 	 * to save and restore registers here.
753ca8a282aSKuninori Morimoto 	 */
754ca8a282aSKuninori Morimoto 	return 0;
755ca8a282aSKuninori Morimoto }
756ca8a282aSKuninori Morimoto 
757ca8a282aSKuninori Morimoto static const struct dev_pm_ops usbhsc_pm_ops = {
758ca8a282aSKuninori Morimoto 	.suspend		= usbhsc_suspend,
759ca8a282aSKuninori Morimoto 	.resume			= usbhsc_resume,
760ca8a282aSKuninori Morimoto 	.runtime_suspend	= usbhsc_runtime_nop,
761ca8a282aSKuninori Morimoto 	.runtime_resume		= usbhsc_runtime_nop,
762ca8a282aSKuninori Morimoto };
763ca8a282aSKuninori Morimoto 
764f1407d5cSKuninori Morimoto static struct platform_driver renesas_usbhs_driver = {
765f1407d5cSKuninori Morimoto 	.driver		= {
766f1407d5cSKuninori Morimoto 		.name	= "renesas_usbhs",
767ca8a282aSKuninori Morimoto 		.pm	= &usbhsc_pm_ops,
768b854100eSYoshihiro Shimoda 		.of_match_table = of_match_ptr(usbhs_of_match),
769f1407d5cSKuninori Morimoto 	},
770f1407d5cSKuninori Morimoto 	.probe		= usbhs_probe,
7717690417dSBill Pemberton 	.remove		= usbhs_remove,
772f1407d5cSKuninori Morimoto };
773f1407d5cSKuninori Morimoto 
774cc27c96cSAxel Lin module_platform_driver(renesas_usbhs_driver);
775f1407d5cSKuninori Morimoto 
776f1407d5cSKuninori Morimoto MODULE_LICENSE("GPL");
777f1407d5cSKuninori Morimoto MODULE_DESCRIPTION("Renesas USB driver");
778f1407d5cSKuninori Morimoto MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
779