xref: /openbmc/linux/drivers/usb/renesas_usbhs/common.c (revision 4d2a863fe9b952d7147bd169d54062e71f343415)
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  */
83df0e240SYoshihiro Shimoda #include <linux/clk.h>
9148e1134SThierry Reding #include <linux/err.h>
108ecef00fSUlrich Hecht #include <linux/gpio.h>
11f1407d5cSKuninori Morimoto #include <linux/io.h>
12f1407d5cSKuninori Morimoto #include <linux/module.h>
13b854100eSYoshihiro Shimoda #include <linux/of_device.h>
14b854100eSYoshihiro Shimoda #include <linux/of_gpio.h>
15f1407d5cSKuninori Morimoto #include <linux/pm_runtime.h>
16f181dbb4SYoshihiro Shimoda #include <linux/reset.h>
17f1407d5cSKuninori Morimoto #include <linux/slab.h>
18f1407d5cSKuninori Morimoto #include <linux/sysfs.h>
19cc502bb7SPaul Bolle #include "common.h"
208ecef00fSUlrich Hecht #include "rcar2.h"
21de18757eSYoshihiro Shimoda #include "rcar3.h"
22aec2927bSChris Brandt #include "rza.h"
23f1407d5cSKuninori Morimoto 
24233f519dSKuninori Morimoto /*
25233f519dSKuninori Morimoto  *		image of renesas_usbhs
26233f519dSKuninori Morimoto  *
27233f519dSKuninori Morimoto  * ex) gadget case
28233f519dSKuninori Morimoto 
29233f519dSKuninori Morimoto  * mod.c
30233f519dSKuninori Morimoto  * mod_gadget.c
31233f519dSKuninori Morimoto  * mod_host.c		pipe.c		fifo.c
32233f519dSKuninori Morimoto  *
33233f519dSKuninori Morimoto  *			+-------+	+-----------+
34233f519dSKuninori Morimoto  *			| pipe0 |------>| fifo pio  |
35233f519dSKuninori Morimoto  * +------------+	+-------+	+-----------+
36233f519dSKuninori Morimoto  * | mod_gadget |=====> | pipe1 |--+
37233f519dSKuninori Morimoto  * +------------+	+-------+  |	+-----------+
38233f519dSKuninori Morimoto  *			| pipe2 |  |  +-| fifo dma0 |
39233f519dSKuninori Morimoto  * +------------+	+-------+  |  |	+-----------+
40233f519dSKuninori Morimoto  * | mod_host   |	| pipe3 |<-|--+
41233f519dSKuninori Morimoto  * +------------+	+-------+  |	+-----------+
42233f519dSKuninori Morimoto  *			| ....  |  +--->| fifo dma1 |
43233f519dSKuninori Morimoto  *			| ....  |	+-----------+
44233f519dSKuninori Morimoto  */
45233f519dSKuninori Morimoto 
46233f519dSKuninori Morimoto 
47b002ff6eSKuninori Morimoto #define USBHSF_RUNTIME_PWCTRL	(1 << 0)
48b002ff6eSKuninori Morimoto 
49b002ff6eSKuninori Morimoto /* status */
50b002ff6eSKuninori Morimoto #define usbhsc_flags_init(p)   do {(p)->flags = 0; } while (0)
51b002ff6eSKuninori Morimoto #define usbhsc_flags_set(p, b) ((p)->flags |=  (b))
52b002ff6eSKuninori Morimoto #define usbhsc_flags_clr(p, b) ((p)->flags &= ~(b))
53b002ff6eSKuninori Morimoto #define usbhsc_flags_has(p, b) ((p)->flags &   (b))
54b002ff6eSKuninori Morimoto 
55f1407d5cSKuninori Morimoto /*
56f1407d5cSKuninori Morimoto  * platform call back
57f1407d5cSKuninori Morimoto  *
58f1407d5cSKuninori Morimoto  * renesas usb support platform callback function.
59f1407d5cSKuninori Morimoto  * Below macro call it.
60f1407d5cSKuninori Morimoto  * if platform doesn't have callback, it return 0 (no error)
61f1407d5cSKuninori Morimoto  */
62f1407d5cSKuninori Morimoto #define usbhs_platform_call(priv, func, args...)\
63f1407d5cSKuninori Morimoto 	(!(priv) ? -ENODEV :			\
6448298206SKuninori Morimoto 	 !((priv)->pfunc.func) ? 0 :		\
6548298206SKuninori Morimoto 	 (priv)->pfunc.func(args))
66f1407d5cSKuninori Morimoto 
67f1407d5cSKuninori Morimoto /*
68f1407d5cSKuninori Morimoto  *		common functions
69f1407d5cSKuninori Morimoto  */
70f1407d5cSKuninori Morimoto u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
71f1407d5cSKuninori Morimoto {
72f1407d5cSKuninori Morimoto 	return ioread16(priv->base + reg);
73f1407d5cSKuninori Morimoto }
74f1407d5cSKuninori Morimoto 
75f1407d5cSKuninori Morimoto void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
76f1407d5cSKuninori Morimoto {
77f1407d5cSKuninori Morimoto 	iowrite16(data, priv->base + reg);
78f1407d5cSKuninori Morimoto }
79f1407d5cSKuninori Morimoto 
80f1407d5cSKuninori Morimoto void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
81f1407d5cSKuninori Morimoto {
82f1407d5cSKuninori Morimoto 	u16 val = usbhs_read(priv, reg);
83f1407d5cSKuninori Morimoto 
84f1407d5cSKuninori Morimoto 	val &= ~mask;
85f1407d5cSKuninori Morimoto 	val |= data & mask;
86f1407d5cSKuninori Morimoto 
87f1407d5cSKuninori Morimoto 	usbhs_write(priv, reg, val);
88f1407d5cSKuninori Morimoto }
89f1407d5cSKuninori Morimoto 
90206dcc2cSKuninori Morimoto struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
91206dcc2cSKuninori Morimoto {
92206dcc2cSKuninori Morimoto 	return dev_get_drvdata(&pdev->dev);
93206dcc2cSKuninori Morimoto }
94206dcc2cSKuninori Morimoto 
95f1407d5cSKuninori Morimoto /*
96f1407d5cSKuninori Morimoto  *		syscfg functions
97f1407d5cSKuninori Morimoto  */
9876190152SKuninori Morimoto static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
99f1407d5cSKuninori Morimoto {
100f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
101f1407d5cSKuninori Morimoto }
102f1407d5cSKuninori Morimoto 
103f1407d5cSKuninori Morimoto void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
104f1407d5cSKuninori Morimoto {
1053244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
1063244a7b4SKuninori Morimoto 	u16 val  = DCFM | DRPD | HSE | USBE;
107f427eb64SKuninori Morimoto 	int has_otg = usbhs_get_dparam(priv, has_otg);
108f427eb64SKuninori Morimoto 
109f427eb64SKuninori Morimoto 	if (has_otg)
110f427eb64SKuninori Morimoto 		usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN));
111f1407d5cSKuninori Morimoto 
112f1407d5cSKuninori Morimoto 	/*
113f1407d5cSKuninori Morimoto 	 * if enable
114f1407d5cSKuninori Morimoto 	 *
115f1407d5cSKuninori Morimoto 	 * - select Host mode
116f1407d5cSKuninori Morimoto 	 * - D+ Line/D- Line Pull-down
117f1407d5cSKuninori Morimoto 	 */
118f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
119f1407d5cSKuninori Morimoto }
120f1407d5cSKuninori Morimoto 
121f1407d5cSKuninori Morimoto void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
122f1407d5cSKuninori Morimoto {
1233244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
12404a5def3STakeshi Kihara 	u16 val  = HSE | USBE;
125f1407d5cSKuninori Morimoto 
126f1407d5cSKuninori Morimoto 	/*
127f1407d5cSKuninori Morimoto 	 * if enable
128f1407d5cSKuninori Morimoto 	 *
129f1407d5cSKuninori Morimoto 	 * - select Function mode
13004a5def3STakeshi Kihara 	 * - D+ Line Pull-up is disabled
13104a5def3STakeshi Kihara 	 *      When D+ Line Pull-up is enabled,
13204a5def3STakeshi Kihara 	 *      calling usbhs_sys_function_pullup(,1)
133f1407d5cSKuninori Morimoto 	 */
134f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
135f1407d5cSKuninori Morimoto }
136f1407d5cSKuninori Morimoto 
1374cd2f599Skuninori.morimoto.gx@renesas.com void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable)
1384cd2f599Skuninori.morimoto.gx@renesas.com {
1394cd2f599Skuninori.morimoto.gx@renesas.com 	usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0);
1404cd2f599Skuninori.morimoto.gx@renesas.com }
1414cd2f599Skuninori.morimoto.gx@renesas.com 
142dfbb7f4fSKuninori Morimoto void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
143dfbb7f4fSKuninori Morimoto {
144dfbb7f4fSKuninori Morimoto 	usbhs_write(priv, TESTMODE, mode);
145dfbb7f4fSKuninori Morimoto }
146dfbb7f4fSKuninori Morimoto 
147f1407d5cSKuninori Morimoto /*
148f1407d5cSKuninori Morimoto  *		frame functions
149f1407d5cSKuninori Morimoto  */
150f1407d5cSKuninori Morimoto int usbhs_frame_get_num(struct usbhs_priv *priv)
151f1407d5cSKuninori Morimoto {
152f1407d5cSKuninori Morimoto 	return usbhs_read(priv, FRMNUM) & FRNM_MASK;
153f1407d5cSKuninori Morimoto }
154f1407d5cSKuninori Morimoto 
155f1407d5cSKuninori Morimoto /*
156ef8bedb9SKuninori Morimoto  *		usb request functions
157ef8bedb9SKuninori Morimoto  */
158ef8bedb9SKuninori Morimoto void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
159ef8bedb9SKuninori Morimoto {
160ef8bedb9SKuninori Morimoto 	u16 val;
161ef8bedb9SKuninori Morimoto 
162ef8bedb9SKuninori Morimoto 	val = usbhs_read(priv, USBREQ);
163ef8bedb9SKuninori Morimoto 	req->bRequest		= (val >> 8) & 0xFF;
164ef8bedb9SKuninori Morimoto 	req->bRequestType	= (val >> 0) & 0xFF;
165ef8bedb9SKuninori Morimoto 
166ef8bedb9SKuninori Morimoto 	req->wValue	= usbhs_read(priv, USBVAL);
167ef8bedb9SKuninori Morimoto 	req->wIndex	= usbhs_read(priv, USBINDX);
168ef8bedb9SKuninori Morimoto 	req->wLength	= usbhs_read(priv, USBLENG);
169ef8bedb9SKuninori Morimoto }
170ef8bedb9SKuninori Morimoto 
171ef8bedb9SKuninori Morimoto void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
172ef8bedb9SKuninori Morimoto {
173ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBREQ,  (req->bRequest << 8) | req->bRequestType);
174ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBVAL,  req->wValue);
175ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBINDX, req->wIndex);
176ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBLENG, req->wLength);
177ef8bedb9SKuninori Morimoto 
178ef8bedb9SKuninori Morimoto 	usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
179ef8bedb9SKuninori Morimoto }
180ef8bedb9SKuninori Morimoto 
181ef8bedb9SKuninori Morimoto /*
182258485d9SKuninori Morimoto  *		bus/vbus functions
183258485d9SKuninori Morimoto  */
184258485d9SKuninori Morimoto void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
185258485d9SKuninori Morimoto {
186a9be4a45SKuninori Morimoto 	u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
187a9be4a45SKuninori Morimoto 
188a9be4a45SKuninori Morimoto 	if (status != USBRST) {
189a9be4a45SKuninori Morimoto 		struct device *dev = usbhs_priv_to_dev(priv);
190a9be4a45SKuninori Morimoto 		dev_err(dev, "usbhs should be reset\n");
191a9be4a45SKuninori Morimoto 	}
192a9be4a45SKuninori Morimoto 
193258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
194258485d9SKuninori Morimoto }
195258485d9SKuninori Morimoto 
196258485d9SKuninori Morimoto void usbhs_bus_send_reset(struct usbhs_priv *priv)
197258485d9SKuninori Morimoto {
198258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
199258485d9SKuninori Morimoto }
200258485d9SKuninori Morimoto 
20175587f52SKuninori Morimoto int usbhs_bus_get_speed(struct usbhs_priv *priv)
20275587f52SKuninori Morimoto {
20375587f52SKuninori Morimoto 	u16 dvstctr = usbhs_read(priv, DVSTCTR);
20475587f52SKuninori Morimoto 
20575587f52SKuninori Morimoto 	switch (RHST & dvstctr) {
20675587f52SKuninori Morimoto 	case RHST_LOW_SPEED:
20775587f52SKuninori Morimoto 		return USB_SPEED_LOW;
20875587f52SKuninori Morimoto 	case RHST_FULL_SPEED:
20975587f52SKuninori Morimoto 		return USB_SPEED_FULL;
21075587f52SKuninori Morimoto 	case RHST_HIGH_SPEED:
21175587f52SKuninori Morimoto 		return USB_SPEED_HIGH;
21275587f52SKuninori Morimoto 	}
21375587f52SKuninori Morimoto 
21475587f52SKuninori Morimoto 	return USB_SPEED_UNKNOWN;
21575587f52SKuninori Morimoto }
21675587f52SKuninori Morimoto 
217258485d9SKuninori Morimoto int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
218258485d9SKuninori Morimoto {
219258485d9SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
220258485d9SKuninori Morimoto 
221258485d9SKuninori Morimoto 	return usbhs_platform_call(priv, set_vbus, pdev, enable);
222258485d9SKuninori Morimoto }
223258485d9SKuninori Morimoto 
224258485d9SKuninori Morimoto static void usbhsc_bus_init(struct usbhs_priv *priv)
225258485d9SKuninori Morimoto {
226258485d9SKuninori Morimoto 	usbhs_write(priv, DVSTCTR, 0);
227258485d9SKuninori Morimoto 
228258485d9SKuninori Morimoto 	usbhs_vbus_ctrl(priv, 0);
229258485d9SKuninori Morimoto }
230258485d9SKuninori Morimoto 
231258485d9SKuninori Morimoto /*
232eb05191fSKuninori Morimoto  *		device configuration
233eb05191fSKuninori Morimoto  */
2343dd49268SKuninori Morimoto int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
235eb05191fSKuninori Morimoto 			   u16 upphub, u16 hubport, u16 speed)
236eb05191fSKuninori Morimoto {
237eb05191fSKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
238eb05191fSKuninori Morimoto 	u16 usbspd = 0;
239eb05191fSKuninori Morimoto 	u32 reg = DEVADD0 + (2 * devnum);
240eb05191fSKuninori Morimoto 
241eb05191fSKuninori Morimoto 	if (devnum > 10) {
242eb05191fSKuninori Morimoto 		dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
243eb05191fSKuninori Morimoto 		return -EIO;
244eb05191fSKuninori Morimoto 	}
245eb05191fSKuninori Morimoto 
246eb05191fSKuninori Morimoto 	if (upphub > 0xA) {
247eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported hub number %d\n", upphub);
248eb05191fSKuninori Morimoto 		return -EIO;
249eb05191fSKuninori Morimoto 	}
250eb05191fSKuninori Morimoto 
251eb05191fSKuninori Morimoto 	switch (speed) {
252eb05191fSKuninori Morimoto 	case USB_SPEED_LOW:
253eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_LOW;
254eb05191fSKuninori Morimoto 		break;
255eb05191fSKuninori Morimoto 	case USB_SPEED_FULL:
256eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_FULL;
257eb05191fSKuninori Morimoto 		break;
258eb05191fSKuninori Morimoto 	case USB_SPEED_HIGH:
259eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_HIGH;
260eb05191fSKuninori Morimoto 		break;
261eb05191fSKuninori Morimoto 	default:
262eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported speed %d\n", speed);
263eb05191fSKuninori Morimoto 		return -EIO;
264eb05191fSKuninori Morimoto 	}
265eb05191fSKuninori Morimoto 
266eb05191fSKuninori Morimoto 	usbhs_write(priv, reg,	UPPHUB(upphub)	|
267eb05191fSKuninori Morimoto 				HUBPORT(hubport)|
268eb05191fSKuninori Morimoto 				USBSPD(usbspd));
269eb05191fSKuninori Morimoto 
270eb05191fSKuninori Morimoto 	return 0;
271eb05191fSKuninori Morimoto }
272eb05191fSKuninori Morimoto 
273eb05191fSKuninori Morimoto /*
274ab330cf3SYoshihiro Shimoda  *		interrupt functions
275ab330cf3SYoshihiro Shimoda  */
276ab330cf3SYoshihiro Shimoda void usbhs_xxxsts_clear(struct usbhs_priv *priv, u16 sts_reg, u16 bit)
277ab330cf3SYoshihiro Shimoda {
278ab330cf3SYoshihiro Shimoda 	u16 pipe_mask = (u16)GENMASK(usbhs_get_dparam(priv, pipe_size), 0);
279ab330cf3SYoshihiro Shimoda 
280ab330cf3SYoshihiro Shimoda 	usbhs_write(priv, sts_reg, ~(1 << bit) & pipe_mask);
281ab330cf3SYoshihiro Shimoda }
282ab330cf3SYoshihiro Shimoda 
283ab330cf3SYoshihiro Shimoda /*
284f1407d5cSKuninori Morimoto  *		local functions
285f1407d5cSKuninori Morimoto  */
28611935de5SKuninori Morimoto static void usbhsc_set_buswait(struct usbhs_priv *priv)
287f1407d5cSKuninori Morimoto {
288f1407d5cSKuninori Morimoto 	int wait = usbhs_get_dparam(priv, buswait_bwait);
289f1407d5cSKuninori Morimoto 
290f1407d5cSKuninori Morimoto 	/* set bus wait if platform have */
291f1407d5cSKuninori Morimoto 	if (wait)
292f1407d5cSKuninori Morimoto 		usbhs_bset(priv, BUSWAIT, 0x000F, wait);
293f1407d5cSKuninori Morimoto }
294f1407d5cSKuninori Morimoto 
2953df0e240SYoshihiro Shimoda static bool usbhsc_is_multi_clks(struct usbhs_priv *priv)
2963df0e240SYoshihiro Shimoda {
2973df0e240SYoshihiro Shimoda 	if (priv->dparam.type == USBHS_TYPE_RCAR_GEN3 ||
2983df0e240SYoshihiro Shimoda 	    priv->dparam.type == USBHS_TYPE_RCAR_GEN3_WITH_PLL)
2993df0e240SYoshihiro Shimoda 		return true;
3003df0e240SYoshihiro Shimoda 
3013df0e240SYoshihiro Shimoda 	return false;
3023df0e240SYoshihiro Shimoda }
3033df0e240SYoshihiro Shimoda 
3043df0e240SYoshihiro Shimoda static int usbhsc_clk_get(struct device *dev, struct usbhs_priv *priv)
3053df0e240SYoshihiro Shimoda {
3063df0e240SYoshihiro Shimoda 	if (!usbhsc_is_multi_clks(priv))
3073df0e240SYoshihiro Shimoda 		return 0;
3083df0e240SYoshihiro Shimoda 
3093df0e240SYoshihiro Shimoda 	/* The first clock should exist */
3103df0e240SYoshihiro Shimoda 	priv->clks[0] = of_clk_get(dev->of_node, 0);
3113df0e240SYoshihiro Shimoda 	if (IS_ERR(priv->clks[0]))
3123df0e240SYoshihiro Shimoda 		return PTR_ERR(priv->clks[0]);
3133df0e240SYoshihiro Shimoda 
3143df0e240SYoshihiro Shimoda 	/*
3153df0e240SYoshihiro Shimoda 	 * To backward compatibility with old DT, this driver checks the return
3163df0e240SYoshihiro Shimoda 	 * value if it's -ENOENT or not.
3173df0e240SYoshihiro Shimoda 	 */
3183df0e240SYoshihiro Shimoda 	priv->clks[1] = of_clk_get(dev->of_node, 1);
3193df0e240SYoshihiro Shimoda 	if (PTR_ERR(priv->clks[1]) == -ENOENT)
3203df0e240SYoshihiro Shimoda 		priv->clks[1] = NULL;
3213df0e240SYoshihiro Shimoda 	else if (IS_ERR(priv->clks[1]))
3223df0e240SYoshihiro Shimoda 		return PTR_ERR(priv->clks[1]);
3233df0e240SYoshihiro Shimoda 
3243df0e240SYoshihiro Shimoda 	return 0;
3253df0e240SYoshihiro Shimoda }
3263df0e240SYoshihiro Shimoda 
3273df0e240SYoshihiro Shimoda static void usbhsc_clk_put(struct usbhs_priv *priv)
3283df0e240SYoshihiro Shimoda {
3293df0e240SYoshihiro Shimoda 	int i;
3303df0e240SYoshihiro Shimoda 
3313df0e240SYoshihiro Shimoda 	if (!usbhsc_is_multi_clks(priv))
3323df0e240SYoshihiro Shimoda 		return;
3333df0e240SYoshihiro Shimoda 
3343df0e240SYoshihiro Shimoda 	for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
3353df0e240SYoshihiro Shimoda 		clk_put(priv->clks[i]);
3363df0e240SYoshihiro Shimoda }
3373df0e240SYoshihiro Shimoda 
3383df0e240SYoshihiro Shimoda static int usbhsc_clk_prepare_enable(struct usbhs_priv *priv)
3393df0e240SYoshihiro Shimoda {
3403df0e240SYoshihiro Shimoda 	int i, ret;
3413df0e240SYoshihiro Shimoda 
3423df0e240SYoshihiro Shimoda 	if (!usbhsc_is_multi_clks(priv))
3433df0e240SYoshihiro Shimoda 		return 0;
3443df0e240SYoshihiro Shimoda 
3453df0e240SYoshihiro Shimoda 	for (i = 0; i < ARRAY_SIZE(priv->clks); i++) {
3463df0e240SYoshihiro Shimoda 		ret = clk_prepare_enable(priv->clks[i]);
3473df0e240SYoshihiro Shimoda 		if (ret) {
3483df0e240SYoshihiro Shimoda 			while (--i >= 0)
3493df0e240SYoshihiro Shimoda 				clk_disable_unprepare(priv->clks[i]);
3503df0e240SYoshihiro Shimoda 			return ret;
3513df0e240SYoshihiro Shimoda 		}
3523df0e240SYoshihiro Shimoda 	}
3533df0e240SYoshihiro Shimoda 
3543df0e240SYoshihiro Shimoda 	return ret;
3553df0e240SYoshihiro Shimoda }
3563df0e240SYoshihiro Shimoda 
3573df0e240SYoshihiro Shimoda static void usbhsc_clk_disable_unprepare(struct usbhs_priv *priv)
3583df0e240SYoshihiro Shimoda {
3593df0e240SYoshihiro Shimoda 	int i;
3603df0e240SYoshihiro Shimoda 
3613df0e240SYoshihiro Shimoda 	if (!usbhsc_is_multi_clks(priv))
3623df0e240SYoshihiro Shimoda 		return;
3633df0e240SYoshihiro Shimoda 
3643df0e240SYoshihiro Shimoda 	for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
3653df0e240SYoshihiro Shimoda 		clk_disable_unprepare(priv->clks[i]);
3663df0e240SYoshihiro Shimoda }
3673df0e240SYoshihiro Shimoda 
368f1407d5cSKuninori Morimoto /*
369f1407d5cSKuninori Morimoto  *		platform default param
370f1407d5cSKuninori Morimoto  */
3718ecef00fSUlrich Hecht 
3728ecef00fSUlrich Hecht /* commonly used on old SH-Mobile SoCs */
37351f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_default_pipe[] = {
37451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
37551f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, false),
37651f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x18, false),
37751f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x28, true),
37851f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x38, true),
37951f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
38051f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
38151f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
38251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
38351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x07, false),
384f1407d5cSKuninori Morimoto };
385f1407d5cSKuninori Morimoto 
3868ecef00fSUlrich Hecht /* commonly used on newer SH-Mobile and R-Car SoCs */
38751f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_new_pipe[] = {
38851f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
38951f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true),
39051f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true),
39151f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
39251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true),
39351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true),
39451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
39551f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
39651f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
39751f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x78, true),
39851f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x88, true),
39951f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x98, true),
40051f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xa8, true),
40151f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xb8, true),
40251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xc8, true),
40351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xd8, true),
4048ecef00fSUlrich Hecht };
4058ecef00fSUlrich Hecht 
406f1407d5cSKuninori Morimoto /*
4076e267da8SKuninori Morimoto  *		power control
4086e267da8SKuninori Morimoto  */
4096e267da8SKuninori Morimoto static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
4106e267da8SKuninori Morimoto {
411f1ee56a0SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
4126e267da8SKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
4136e267da8SKuninori Morimoto 
4146e267da8SKuninori Morimoto 	if (enable) {
4156e267da8SKuninori Morimoto 		/* enable PM */
4166e267da8SKuninori Morimoto 		pm_runtime_get_sync(dev);
4176e267da8SKuninori Morimoto 
4183df0e240SYoshihiro Shimoda 		/* enable clks */
4193df0e240SYoshihiro Shimoda 		if (usbhsc_clk_prepare_enable(priv))
4203df0e240SYoshihiro Shimoda 			return;
4213df0e240SYoshihiro Shimoda 
422f1ee56a0SKuninori Morimoto 		/* enable platform power */
423f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
424f1ee56a0SKuninori Morimoto 
4256e267da8SKuninori Morimoto 		/* USB on */
4266e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
4276e267da8SKuninori Morimoto 	} else {
4286e267da8SKuninori Morimoto 		/* USB off */
4296e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
4306e267da8SKuninori Morimoto 
431f1ee56a0SKuninori Morimoto 		/* disable platform power */
432f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
433f1ee56a0SKuninori Morimoto 
4343df0e240SYoshihiro Shimoda 		/* disable clks */
4353df0e240SYoshihiro Shimoda 		usbhsc_clk_disable_unprepare(priv);
4363df0e240SYoshihiro Shimoda 
4376e267da8SKuninori Morimoto 		/* disable PM */
4386e267da8SKuninori Morimoto 		pm_runtime_put_sync(dev);
4396e267da8SKuninori Morimoto 	}
4406e267da8SKuninori Morimoto }
4416e267da8SKuninori Morimoto 
4426e267da8SKuninori Morimoto /*
443ca8a282aSKuninori Morimoto  *		hotplug
444f1407d5cSKuninori Morimoto  */
445ca8a282aSKuninori Morimoto static void usbhsc_hotplug(struct usbhs_priv *priv)
446f1407d5cSKuninori Morimoto {
447f1407d5cSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
448f1407d5cSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
449f1407d5cSKuninori Morimoto 	int id;
450f1407d5cSKuninori Morimoto 	int enable;
451d7b3968dSSergei Shtylyov 	int cable;
452f1407d5cSKuninori Morimoto 	int ret;
453f1407d5cSKuninori Morimoto 
454f1407d5cSKuninori Morimoto 	/*
455f1407d5cSKuninori Morimoto 	 * get vbus status from platform
456f1407d5cSKuninori Morimoto 	 */
457f1407d5cSKuninori Morimoto 	enable = usbhs_platform_call(priv, get_vbus, pdev);
458f1407d5cSKuninori Morimoto 
459f1407d5cSKuninori Morimoto 	/*
460f1407d5cSKuninori Morimoto 	 * get id from platform
461f1407d5cSKuninori Morimoto 	 */
462f1407d5cSKuninori Morimoto 	id = usbhs_platform_call(priv, get_id, pdev);
463f1407d5cSKuninori Morimoto 
464f1407d5cSKuninori Morimoto 	if (enable && !mod) {
465d7b3968dSSergei Shtylyov 		if (priv->edev) {
466ea07b8cfSChanwoo Choi 			cable = extcon_get_state(priv->edev, EXTCON_USB_HOST);
467d7b3968dSSergei Shtylyov 			if ((cable > 0 && id != USBHS_HOST) ||
468d7b3968dSSergei Shtylyov 			    (!cable && id != USBHS_GADGET)) {
469d7b3968dSSergei Shtylyov 				dev_info(&pdev->dev,
470d7b3968dSSergei Shtylyov 					 "USB cable plugged in doesn't match the selected role!\n");
471d7b3968dSSergei Shtylyov 				return;
472d7b3968dSSergei Shtylyov 			}
473d7b3968dSSergei Shtylyov 		}
474d7b3968dSSergei Shtylyov 
475f1407d5cSKuninori Morimoto 		ret = usbhs_mod_change(priv, id);
476f1407d5cSKuninori Morimoto 		if (ret < 0)
477f1407d5cSKuninori Morimoto 			return;
478f1407d5cSKuninori Morimoto 
479f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s enable\n", __func__);
480f1407d5cSKuninori Morimoto 
4816e267da8SKuninori Morimoto 		/* power on */
482b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
4836e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
484f1407d5cSKuninori Morimoto 
485258485d9SKuninori Morimoto 		/* bus init */
486258485d9SKuninori Morimoto 		usbhsc_set_buswait(priv);
487258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
488258485d9SKuninori Morimoto 
489f1407d5cSKuninori Morimoto 		/* module start */
490f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, start, priv);
491f1407d5cSKuninori Morimoto 
492f1407d5cSKuninori Morimoto 	} else if (!enable && mod) {
493f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s disable\n", __func__);
494f1407d5cSKuninori Morimoto 
495f1407d5cSKuninori Morimoto 		/* module stop */
496f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
497f1407d5cSKuninori Morimoto 
498258485d9SKuninori Morimoto 		/* bus init */
499258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
500258485d9SKuninori Morimoto 
5016e267da8SKuninori Morimoto 		/* power off */
502b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
5036e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
504f1407d5cSKuninori Morimoto 
505f1407d5cSKuninori Morimoto 		usbhs_mod_change(priv, -1);
506f1407d5cSKuninori Morimoto 
507f1407d5cSKuninori Morimoto 		/* reset phy for next connection */
508f1407d5cSKuninori Morimoto 		usbhs_platform_call(priv, phy_reset, pdev);
509f1407d5cSKuninori Morimoto 	}
510f1407d5cSKuninori Morimoto }
511f1407d5cSKuninori Morimoto 
512ca8a282aSKuninori Morimoto /*
513ca8a282aSKuninori Morimoto  *		notify hotplug
514ca8a282aSKuninori Morimoto  */
515ca8a282aSKuninori Morimoto static void usbhsc_notify_hotplug(struct work_struct *work)
516ca8a282aSKuninori Morimoto {
517ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = container_of(work,
518ca8a282aSKuninori Morimoto 					       struct usbhs_priv,
519ca8a282aSKuninori Morimoto 					       notify_hotplug_work.work);
520ca8a282aSKuninori Morimoto 	usbhsc_hotplug(priv);
521ca8a282aSKuninori Morimoto }
522ca8a282aSKuninori Morimoto 
523b4fcea2aSKuninori Morimoto static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
524f1407d5cSKuninori Morimoto {
525206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
526bc57381eSKuninori Morimoto 	int delay = usbhs_get_dparam(priv, detection_delay);
527f1407d5cSKuninori Morimoto 
528f1407d5cSKuninori Morimoto 	/*
529f1407d5cSKuninori Morimoto 	 * This functions will be called in interrupt.
530f1407d5cSKuninori Morimoto 	 * To make sure safety context,
531f1407d5cSKuninori Morimoto 	 * use workqueue for usbhs_notify_hotplug
532f1407d5cSKuninori Morimoto 	 */
533a49a88f1SKuninori Morimoto 	schedule_delayed_work(&priv->notify_hotplug_work,
534a49a88f1SKuninori Morimoto 			      msecs_to_jiffies(delay));
535f1407d5cSKuninori Morimoto 	return 0;
536f1407d5cSKuninori Morimoto }
537f1407d5cSKuninori Morimoto 
538f1407d5cSKuninori Morimoto /*
539f1407d5cSKuninori Morimoto  *		platform functions
540f1407d5cSKuninori Morimoto  */
541b854100eSYoshihiro Shimoda static const struct of_device_id usbhs_of_match[] = {
542b854100eSYoshihiro Shimoda 	{
543b854100eSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7790",
544e0213bc5SYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
545b854100eSYoshihiro Shimoda 	},
546b854100eSYoshihiro Shimoda 	{
547b854100eSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7791",
548e0213bc5SYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
549b854100eSYoshihiro Shimoda 	},
550af6e613bSYoshihiro Shimoda 	{
551af6e613bSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7794",
552af6e613bSYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
553af6e613bSYoshihiro Shimoda 	},
554f5f6afa8SYoshihiro Shimoda 	{
555f5f6afa8SYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7795",
556de18757eSYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN3,
557f5f6afa8SYoshihiro Shimoda 	},
5582f1a993aSSimon Horman 	{
559aae819e1SYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7796",
560aae819e1SYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN3,
561aae819e1SYoshihiro Shimoda 	},
562aae819e1SYoshihiro Shimoda 	{
563*4d2a863fSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a77990",
564*4d2a863fSYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN3_WITH_PLL,
565*4d2a863fSYoshihiro Shimoda 	},
566*4d2a863fSYoshihiro Shimoda 	{
5670f38672cSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a77995",
5680f38672cSYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN3_WITH_PLL,
5690f38672cSYoshihiro Shimoda 	},
5700f38672cSYoshihiro Shimoda 	{
5712f1a993aSSimon Horman 		.compatible = "renesas,rcar-gen2-usbhs",
5722f1a993aSSimon Horman 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
5732f1a993aSSimon Horman 	},
5742f1a993aSSimon Horman 	{
5752f1a993aSSimon Horman 		.compatible = "renesas,rcar-gen3-usbhs",
576de18757eSYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN3,
5772f1a993aSSimon Horman 	},
578aec2927bSChris Brandt 	{
579aec2927bSChris Brandt 		.compatible = "renesas,rza1-usbhs",
580aec2927bSChris Brandt 		.data = (void *)USBHS_TYPE_RZA1,
581aec2927bSChris Brandt 	},
582b854100eSYoshihiro Shimoda 	{ },
583b854100eSYoshihiro Shimoda };
584b854100eSYoshihiro Shimoda MODULE_DEVICE_TABLE(of, usbhs_of_match);
585b854100eSYoshihiro Shimoda 
586b854100eSYoshihiro Shimoda static struct renesas_usbhs_platform_info *usbhs_parse_dt(struct device *dev)
587b854100eSYoshihiro Shimoda {
588b854100eSYoshihiro Shimoda 	struct renesas_usbhs_platform_info *info;
589b854100eSYoshihiro Shimoda 	struct renesas_usbhs_driver_param *dparam;
590b854100eSYoshihiro Shimoda 	u32 tmp;
591b854100eSYoshihiro Shimoda 	int gpio;
592b854100eSYoshihiro Shimoda 
593b854100eSYoshihiro Shimoda 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
594b854100eSYoshihiro Shimoda 	if (!info)
595b854100eSYoshihiro Shimoda 		return NULL;
596b854100eSYoshihiro Shimoda 
597b854100eSYoshihiro Shimoda 	dparam = &info->driver_param;
598b61e47b4SGeert Uytterhoeven 	dparam->type = (uintptr_t)of_device_get_match_data(dev);
599b854100eSYoshihiro Shimoda 	if (!of_property_read_u32(dev->of_node, "renesas,buswait", &tmp))
600b854100eSYoshihiro Shimoda 		dparam->buswait_bwait = tmp;
601b854100eSYoshihiro Shimoda 	gpio = of_get_named_gpio_flags(dev->of_node, "renesas,enable-gpio", 0,
602b854100eSYoshihiro Shimoda 				       NULL);
603b854100eSYoshihiro Shimoda 	if (gpio > 0)
604b854100eSYoshihiro Shimoda 		dparam->enable_gpio = gpio;
605b854100eSYoshihiro Shimoda 
606772ce812SYoshihiro Shimoda 	if (dparam->type == USBHS_TYPE_RCAR_GEN2 ||
6070f38672cSYoshihiro Shimoda 	    dparam->type == USBHS_TYPE_RCAR_GEN3 ||
6080f38672cSYoshihiro Shimoda 	    dparam->type == USBHS_TYPE_RCAR_GEN3_WITH_PLL) {
609ab330cf3SYoshihiro Shimoda 		dparam->has_usb_dmac = 1;
610a877b8e5SYoshihiro Shimoda 		dparam->pipe_configs = usbhsc_new_pipe;
611a877b8e5SYoshihiro Shimoda 		dparam->pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
612a877b8e5SYoshihiro Shimoda 	}
613ab330cf3SYoshihiro Shimoda 
614aec2927bSChris Brandt 	if (dparam->type == USBHS_TYPE_RZA1) {
615aec2927bSChris Brandt 		dparam->pipe_configs = usbhsc_new_pipe;
616aec2927bSChris Brandt 		dparam->pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
617aec2927bSChris Brandt 	}
618aec2927bSChris Brandt 
619b854100eSYoshihiro Shimoda 	return info;
620b854100eSYoshihiro Shimoda }
621b854100eSYoshihiro Shimoda 
622b7a8d17dSKuninori Morimoto static int usbhs_probe(struct platform_device *pdev)
623f1407d5cSKuninori Morimoto {
624f0f14a7aSKuninori Morimoto 	struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
625f1407d5cSKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc;
626f1407d5cSKuninori Morimoto 	struct usbhs_priv *priv;
62753069af3SShimoda, Yoshihiro 	struct resource *res, *irq_res;
628f1407d5cSKuninori Morimoto 	int ret;
629f1407d5cSKuninori Morimoto 
630b854100eSYoshihiro Shimoda 	/* check device node */
631b854100eSYoshihiro Shimoda 	if (pdev->dev.of_node)
632b854100eSYoshihiro Shimoda 		info = pdev->dev.platform_data = usbhs_parse_dt(&pdev->dev);
633b854100eSYoshihiro Shimoda 
634f1407d5cSKuninori Morimoto 	/* check platform information */
6358ecef00fSUlrich Hecht 	if (!info) {
636f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "no platform information\n");
637f1407d5cSKuninori Morimoto 		return -EINVAL;
638f1407d5cSKuninori Morimoto 	}
639f1407d5cSKuninori Morimoto 
640f1407d5cSKuninori Morimoto 	/* platform data */
64153069af3SShimoda, Yoshihiro 	irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
642687f16b8SVarka Bhadram 	if (!irq_res) {
643f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
644f1407d5cSKuninori Morimoto 		return -ENODEV;
645f1407d5cSKuninori Morimoto 	}
646f1407d5cSKuninori Morimoto 
647f1407d5cSKuninori Morimoto 	/* usb private data */
64858efc77cSKuninori Morimoto 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
649b1cb07cdSPeter Chen 	if (!priv)
650f1407d5cSKuninori Morimoto 		return -ENOMEM;
651f1407d5cSKuninori Morimoto 
652687f16b8SVarka Bhadram 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
653148e1134SThierry Reding 	priv->base = devm_ioremap_resource(&pdev->dev, res);
654148e1134SThierry Reding 	if (IS_ERR(priv->base))
655148e1134SThierry Reding 		return PTR_ERR(priv->base);
656f1407d5cSKuninori Morimoto 
657d7b3968dSSergei Shtylyov 	if (of_property_read_bool(pdev->dev.of_node, "extcon")) {
658d7b3968dSSergei Shtylyov 		priv->edev = extcon_get_edev_by_phandle(&pdev->dev, 0);
659d7b3968dSSergei Shtylyov 		if (IS_ERR(priv->edev))
660d7b3968dSSergei Shtylyov 			return PTR_ERR(priv->edev);
661d7b3968dSSergei Shtylyov 	}
662d7b3968dSSergei Shtylyov 
663f181dbb4SYoshihiro Shimoda 	priv->rsts = devm_reset_control_array_get_optional_shared(&pdev->dev);
664f181dbb4SYoshihiro Shimoda 	if (IS_ERR(priv->rsts))
665f181dbb4SYoshihiro Shimoda 		return PTR_ERR(priv->rsts);
666f181dbb4SYoshihiro Shimoda 
667f1407d5cSKuninori Morimoto 	/*
668f1407d5cSKuninori Morimoto 	 * care platform info
669f1407d5cSKuninori Morimoto 	 */
6708ecef00fSUlrich Hecht 
67148298206SKuninori Morimoto 	memcpy(&priv->dparam,
67248298206SKuninori Morimoto 	       &info->driver_param,
67348298206SKuninori Morimoto 	       sizeof(struct renesas_usbhs_driver_param));
674f1407d5cSKuninori Morimoto 
6758ecef00fSUlrich Hecht 	switch (priv->dparam.type) {
676e0213bc5SYoshihiro Shimoda 	case USBHS_TYPE_RCAR_GEN2:
6778ecef00fSUlrich Hecht 		priv->pfunc = usbhs_rcar2_ops;
6788ecef00fSUlrich Hecht 		break;
679de18757eSYoshihiro Shimoda 	case USBHS_TYPE_RCAR_GEN3:
680de18757eSYoshihiro Shimoda 		priv->pfunc = usbhs_rcar3_ops;
681de18757eSYoshihiro Shimoda 		break;
6820f38672cSYoshihiro Shimoda 	case USBHS_TYPE_RCAR_GEN3_WITH_PLL:
6830f38672cSYoshihiro Shimoda 		priv->pfunc = usbhs_rcar3_with_pll_ops;
6840f38672cSYoshihiro Shimoda 		break;
685aec2927bSChris Brandt 	case USBHS_TYPE_RZA1:
686aec2927bSChris Brandt 		priv->pfunc = usbhs_rza1_ops;
687aec2927bSChris Brandt 		break;
6888ecef00fSUlrich Hecht 	default:
6898ecef00fSUlrich Hecht 		if (!info->platform_callback.get_id) {
6908ecef00fSUlrich Hecht 			dev_err(&pdev->dev, "no platform callbacks");
6918ecef00fSUlrich Hecht 			return -EINVAL;
6928ecef00fSUlrich Hecht 		}
6938ecef00fSUlrich Hecht 		memcpy(&priv->pfunc,
6948ecef00fSUlrich Hecht 		       &info->platform_callback,
6958ecef00fSUlrich Hecht 		       sizeof(struct renesas_usbhs_platform_callback));
6968ecef00fSUlrich Hecht 		break;
6978ecef00fSUlrich Hecht 	}
6988ecef00fSUlrich Hecht 
699f1407d5cSKuninori Morimoto 	/* set driver callback functions for platform */
700f1407d5cSKuninori Morimoto 	dfunc			= &info->driver_callback;
701f1407d5cSKuninori Morimoto 	dfunc->notify_hotplug	= usbhsc_drvcllbck_notify_hotplug;
702f1407d5cSKuninori Morimoto 
703f1407d5cSKuninori Morimoto 	/* set default param if platform doesn't have */
70451f141a9SYoshihiro Shimoda 	if (!priv->dparam.pipe_configs) {
70551f141a9SYoshihiro Shimoda 		priv->dparam.pipe_configs = usbhsc_default_pipe;
70651f141a9SYoshihiro Shimoda 		priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe);
707f1407d5cSKuninori Morimoto 	}
70848298206SKuninori Morimoto 	if (!priv->dparam.pio_dma_border)
70948298206SKuninori Morimoto 		priv->dparam.pio_dma_border = 64; /* 64byte */
710f1407d5cSKuninori Morimoto 
711b002ff6eSKuninori Morimoto 	/* FIXME */
712b002ff6eSKuninori Morimoto 	/* runtime power control ? */
71348298206SKuninori Morimoto 	if (priv->pfunc.get_vbus)
714b002ff6eSKuninori Morimoto 		usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
715b002ff6eSKuninori Morimoto 
716f1407d5cSKuninori Morimoto 	/*
717f1407d5cSKuninori Morimoto 	 * priv settings
718f1407d5cSKuninori Morimoto 	 */
71953069af3SShimoda, Yoshihiro 	priv->irq	= irq_res->start;
72053069af3SShimoda, Yoshihiro 	if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
72153069af3SShimoda, Yoshihiro 		priv->irqflags = IRQF_SHARED;
722f1407d5cSKuninori Morimoto 	priv->pdev	= pdev;
723bc57381eSKuninori Morimoto 	INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
724f1407d5cSKuninori Morimoto 	spin_lock_init(usbhs_priv_to_lock(priv));
725f1407d5cSKuninori Morimoto 
726f1407d5cSKuninori Morimoto 	/* call pipe and module init */
727f1407d5cSKuninori Morimoto 	ret = usbhs_pipe_probe(priv);
728f1407d5cSKuninori Morimoto 	if (ret < 0)
72958efc77cSKuninori Morimoto 		return ret;
730f1407d5cSKuninori Morimoto 
731d3af90a5SKuninori Morimoto 	ret = usbhs_fifo_probe(priv);
732f1407d5cSKuninori Morimoto 	if (ret < 0)
73397f93227SKuninori Morimoto 		goto probe_end_pipe_exit;
734f1407d5cSKuninori Morimoto 
735d3af90a5SKuninori Morimoto 	ret = usbhs_mod_probe(priv);
736d3af90a5SKuninori Morimoto 	if (ret < 0)
737d3af90a5SKuninori Morimoto 		goto probe_end_fifo_exit;
738d3af90a5SKuninori Morimoto 
739f1407d5cSKuninori Morimoto 	/* dev_set_drvdata should be called after usbhs_mod_init */
740c9a0552eSLibo Chen 	platform_set_drvdata(pdev, priv);
741f1407d5cSKuninori Morimoto 
742f181dbb4SYoshihiro Shimoda 	ret = reset_control_deassert(priv->rsts);
743f181dbb4SYoshihiro Shimoda 	if (ret)
744f181dbb4SYoshihiro Shimoda 		goto probe_fail_rst;
745f181dbb4SYoshihiro Shimoda 
7463df0e240SYoshihiro Shimoda 	ret = usbhsc_clk_get(&pdev->dev, priv);
7473df0e240SYoshihiro Shimoda 	if (ret)
7483df0e240SYoshihiro Shimoda 		goto probe_fail_clks;
7493df0e240SYoshihiro Shimoda 
750f1407d5cSKuninori Morimoto 	/*
751f1407d5cSKuninori Morimoto 	 * deviece reset here because
752f1407d5cSKuninori Morimoto 	 * USB device might be used in boot loader.
753f1407d5cSKuninori Morimoto 	 */
754f1407d5cSKuninori Morimoto 	usbhs_sys_clock_ctrl(priv, 0);
755f1407d5cSKuninori Morimoto 
7568ecef00fSUlrich Hecht 	/* check GPIO determining if USB function should be enabled */
7578ecef00fSUlrich Hecht 	if (priv->dparam.enable_gpio) {
7588ecef00fSUlrich Hecht 		gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL);
7598ecef00fSUlrich Hecht 		ret = !gpio_get_value(priv->dparam.enable_gpio);
7608ecef00fSUlrich Hecht 		gpio_free(priv->dparam.enable_gpio);
7618ecef00fSUlrich Hecht 		if (ret) {
7628ecef00fSUlrich Hecht 			dev_warn(&pdev->dev,
7638ecef00fSUlrich Hecht 				 "USB function not selected (GPIO %d)\n",
7648ecef00fSUlrich Hecht 				 priv->dparam.enable_gpio);
7658ecef00fSUlrich Hecht 			ret = -ENOTSUPP;
7668ecef00fSUlrich Hecht 			goto probe_end_mod_exit;
7678ecef00fSUlrich Hecht 		}
7688ecef00fSUlrich Hecht 	}
7698ecef00fSUlrich Hecht 
770f1407d5cSKuninori Morimoto 	/*
771f1407d5cSKuninori Morimoto 	 * platform call
772f1407d5cSKuninori Morimoto 	 *
773f1407d5cSKuninori Morimoto 	 * USB phy setup might depend on CPU/Board.
774f1407d5cSKuninori Morimoto 	 * If platform has its callback functions,
775f1407d5cSKuninori Morimoto 	 * call it here.
776f1407d5cSKuninori Morimoto 	 */
777f1407d5cSKuninori Morimoto 	ret = usbhs_platform_call(priv, hardware_init, pdev);
778f1407d5cSKuninori Morimoto 	if (ret < 0) {
77948ac1e57SSergei Shtylyov 		dev_err(&pdev->dev, "platform init failed.\n");
78097f93227SKuninori Morimoto 		goto probe_end_mod_exit;
781f1407d5cSKuninori Morimoto 	}
782f1407d5cSKuninori Morimoto 
783f1407d5cSKuninori Morimoto 	/* reset phy for connection */
784f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
785f1407d5cSKuninori Morimoto 
786b002ff6eSKuninori Morimoto 	/* power control */
787b002ff6eSKuninori Morimoto 	pm_runtime_enable(&pdev->dev);
788b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
789b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
790b002ff6eSKuninori Morimoto 		usbhs_mod_autonomy_mode(priv);
791b002ff6eSKuninori Morimoto 	}
792b002ff6eSKuninori Morimoto 
793f1407d5cSKuninori Morimoto 	/*
794f1407d5cSKuninori Morimoto 	 * manual call notify_hotplug for cold plug
795f1407d5cSKuninori Morimoto 	 */
796368504c0SSergei Shtylyov 	usbhsc_drvcllbck_notify_hotplug(pdev);
797f1407d5cSKuninori Morimoto 
798f1407d5cSKuninori Morimoto 	dev_info(&pdev->dev, "probed\n");
799f1407d5cSKuninori Morimoto 
800f1407d5cSKuninori Morimoto 	return ret;
801f1407d5cSKuninori Morimoto 
802f1407d5cSKuninori Morimoto probe_end_mod_exit:
8033df0e240SYoshihiro Shimoda 	usbhsc_clk_put(priv);
8043df0e240SYoshihiro Shimoda probe_fail_clks:
805f181dbb4SYoshihiro Shimoda 	reset_control_assert(priv->rsts);
806f181dbb4SYoshihiro Shimoda probe_fail_rst:
807f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
808d3af90a5SKuninori Morimoto probe_end_fifo_exit:
809d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
81097f93227SKuninori Morimoto probe_end_pipe_exit:
81197f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
812f1407d5cSKuninori Morimoto 
8130d799503SYoshihiro Shimoda 	dev_info(&pdev->dev, "probe failed (%d)\n", ret);
814f1407d5cSKuninori Morimoto 
815f1407d5cSKuninori Morimoto 	return ret;
816f1407d5cSKuninori Morimoto }
817f1407d5cSKuninori Morimoto 
818fb4e98abSBill Pemberton static int usbhs_remove(struct platform_device *pdev)
819f1407d5cSKuninori Morimoto {
820206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
821f0f14a7aSKuninori Morimoto 	struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
822af32fe51SKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
823f1407d5cSKuninori Morimoto 
824f1407d5cSKuninori Morimoto 	dev_dbg(&pdev->dev, "usb remove\n");
825f1407d5cSKuninori Morimoto 
826af32fe51SKuninori Morimoto 	dfunc->notify_hotplug = NULL;
827af32fe51SKuninori Morimoto 
828b002ff6eSKuninori Morimoto 	/* power off */
829b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
830b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
831f1407d5cSKuninori Morimoto 
832b002ff6eSKuninori Morimoto 	pm_runtime_disable(&pdev->dev);
833f1407d5cSKuninori Morimoto 
834f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, hardware_exit, pdev);
8353df0e240SYoshihiro Shimoda 	usbhsc_clk_put(priv);
836f181dbb4SYoshihiro Shimoda 	reset_control_assert(priv->rsts);
837f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
838d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
83997f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
840f1407d5cSKuninori Morimoto 
841f1407d5cSKuninori Morimoto 	return 0;
842f1407d5cSKuninori Morimoto }
843f1407d5cSKuninori Morimoto 
844ca8a282aSKuninori Morimoto static int usbhsc_suspend(struct device *dev)
845ca8a282aSKuninori Morimoto {
846ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
847ca8a282aSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
848ca8a282aSKuninori Morimoto 
849ca8a282aSKuninori Morimoto 	if (mod) {
850ca8a282aSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
851ca8a282aSKuninori Morimoto 		usbhs_mod_change(priv, -1);
852ca8a282aSKuninori Morimoto 	}
853ca8a282aSKuninori Morimoto 
854ca8a282aSKuninori Morimoto 	if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
855ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
856ca8a282aSKuninori Morimoto 
857ca8a282aSKuninori Morimoto 	return 0;
858ca8a282aSKuninori Morimoto }
859ca8a282aSKuninori Morimoto 
860ca8a282aSKuninori Morimoto static int usbhsc_resume(struct device *dev)
861ca8a282aSKuninori Morimoto {
862ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
863ca8a282aSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
864ca8a282aSKuninori Morimoto 
86559a0879aSYoshihiro Shimoda 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
866ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
86759a0879aSYoshihiro Shimoda 		usbhs_mod_autonomy_mode(priv);
86859a0879aSYoshihiro Shimoda 	}
869ca8a282aSKuninori Morimoto 
8705b50d3b5SKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
8715b50d3b5SKuninori Morimoto 
8725b50d3b5SKuninori Morimoto 	usbhsc_drvcllbck_notify_hotplug(pdev);
873ca8a282aSKuninori Morimoto 
874ca8a282aSKuninori Morimoto 	return 0;
875ca8a282aSKuninori Morimoto }
876ca8a282aSKuninori Morimoto 
877ca8a282aSKuninori Morimoto static int usbhsc_runtime_nop(struct device *dev)
878ca8a282aSKuninori Morimoto {
879ca8a282aSKuninori Morimoto 	/* Runtime PM callback shared between ->runtime_suspend()
880ca8a282aSKuninori Morimoto 	 * and ->runtime_resume(). Simply returns success.
881ca8a282aSKuninori Morimoto 	 *
882ca8a282aSKuninori Morimoto 	 * This driver re-initializes all registers after
883ca8a282aSKuninori Morimoto 	 * pm_runtime_get_sync() anyway so there is no need
884ca8a282aSKuninori Morimoto 	 * to save and restore registers here.
885ca8a282aSKuninori Morimoto 	 */
886ca8a282aSKuninori Morimoto 	return 0;
887ca8a282aSKuninori Morimoto }
888ca8a282aSKuninori Morimoto 
889ca8a282aSKuninori Morimoto static const struct dev_pm_ops usbhsc_pm_ops = {
890ca8a282aSKuninori Morimoto 	.suspend		= usbhsc_suspend,
891ca8a282aSKuninori Morimoto 	.resume			= usbhsc_resume,
892ca8a282aSKuninori Morimoto 	.runtime_suspend	= usbhsc_runtime_nop,
893ca8a282aSKuninori Morimoto 	.runtime_resume		= usbhsc_runtime_nop,
894ca8a282aSKuninori Morimoto };
895ca8a282aSKuninori Morimoto 
896f1407d5cSKuninori Morimoto static struct platform_driver renesas_usbhs_driver = {
897f1407d5cSKuninori Morimoto 	.driver		= {
898f1407d5cSKuninori Morimoto 		.name	= "renesas_usbhs",
899ca8a282aSKuninori Morimoto 		.pm	= &usbhsc_pm_ops,
900b854100eSYoshihiro Shimoda 		.of_match_table = of_match_ptr(usbhs_of_match),
901f1407d5cSKuninori Morimoto 	},
902f1407d5cSKuninori Morimoto 	.probe		= usbhs_probe,
9037690417dSBill Pemberton 	.remove		= usbhs_remove,
904f1407d5cSKuninori Morimoto };
905f1407d5cSKuninori Morimoto 
906cc27c96cSAxel Lin module_platform_driver(renesas_usbhs_driver);
907f1407d5cSKuninori Morimoto 
908f1407d5cSKuninori Morimoto MODULE_LICENSE("GPL");
909f1407d5cSKuninori Morimoto MODULE_DESCRIPTION("Renesas USB driver");
910f1407d5cSKuninori Morimoto MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
911