xref: /openbmc/linux/drivers/usb/renesas_usbhs/common.c (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
1*5fd54aceSGreg 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  *
8f1407d5cSKuninori Morimoto  * This program is distributed in the hope that it will be useful,
9f1407d5cSKuninori Morimoto  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10f1407d5cSKuninori Morimoto  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11f1407d5cSKuninori Morimoto  * GNU General Public License for more details.
12f1407d5cSKuninori Morimoto  *
13f1407d5cSKuninori Morimoto  * You should have received a copy of the GNU General Public License
14f1407d5cSKuninori Morimoto  * along with this program; if not, write to the Free Software
15f1407d5cSKuninori Morimoto  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16f1407d5cSKuninori Morimoto  *
17f1407d5cSKuninori Morimoto  */
18148e1134SThierry Reding #include <linux/err.h>
198ecef00fSUlrich Hecht #include <linux/gpio.h>
20f1407d5cSKuninori Morimoto #include <linux/io.h>
21f1407d5cSKuninori Morimoto #include <linux/module.h>
22b854100eSYoshihiro Shimoda #include <linux/of_device.h>
23b854100eSYoshihiro Shimoda #include <linux/of_gpio.h>
24f1407d5cSKuninori Morimoto #include <linux/pm_runtime.h>
25f1407d5cSKuninori Morimoto #include <linux/slab.h>
26f1407d5cSKuninori Morimoto #include <linux/sysfs.h>
27cc502bb7SPaul Bolle #include "common.h"
288ecef00fSUlrich Hecht #include "rcar2.h"
29de18757eSYoshihiro Shimoda #include "rcar3.h"
30f1407d5cSKuninori Morimoto 
31233f519dSKuninori Morimoto /*
32233f519dSKuninori Morimoto  *		image of renesas_usbhs
33233f519dSKuninori Morimoto  *
34233f519dSKuninori Morimoto  * ex) gadget case
35233f519dSKuninori Morimoto 
36233f519dSKuninori Morimoto  * mod.c
37233f519dSKuninori Morimoto  * mod_gadget.c
38233f519dSKuninori Morimoto  * mod_host.c		pipe.c		fifo.c
39233f519dSKuninori Morimoto  *
40233f519dSKuninori Morimoto  *			+-------+	+-----------+
41233f519dSKuninori Morimoto  *			| pipe0 |------>| fifo pio  |
42233f519dSKuninori Morimoto  * +------------+	+-------+	+-----------+
43233f519dSKuninori Morimoto  * | mod_gadget |=====> | pipe1 |--+
44233f519dSKuninori Morimoto  * +------------+	+-------+  |	+-----------+
45233f519dSKuninori Morimoto  *			| pipe2 |  |  +-| fifo dma0 |
46233f519dSKuninori Morimoto  * +------------+	+-------+  |  |	+-----------+
47233f519dSKuninori Morimoto  * | mod_host   |	| pipe3 |<-|--+
48233f519dSKuninori Morimoto  * +------------+	+-------+  |	+-----------+
49233f519dSKuninori Morimoto  *			| ....  |  +--->| fifo dma1 |
50233f519dSKuninori Morimoto  *			| ....  |	+-----------+
51233f519dSKuninori Morimoto  */
52233f519dSKuninori Morimoto 
53233f519dSKuninori Morimoto 
54b002ff6eSKuninori Morimoto #define USBHSF_RUNTIME_PWCTRL	(1 << 0)
55b002ff6eSKuninori Morimoto 
56b002ff6eSKuninori Morimoto /* status */
57b002ff6eSKuninori Morimoto #define usbhsc_flags_init(p)   do {(p)->flags = 0; } while (0)
58b002ff6eSKuninori Morimoto #define usbhsc_flags_set(p, b) ((p)->flags |=  (b))
59b002ff6eSKuninori Morimoto #define usbhsc_flags_clr(p, b) ((p)->flags &= ~(b))
60b002ff6eSKuninori Morimoto #define usbhsc_flags_has(p, b) ((p)->flags &   (b))
61b002ff6eSKuninori Morimoto 
62f1407d5cSKuninori Morimoto /*
63f1407d5cSKuninori Morimoto  * platform call back
64f1407d5cSKuninori Morimoto  *
65f1407d5cSKuninori Morimoto  * renesas usb support platform callback function.
66f1407d5cSKuninori Morimoto  * Below macro call it.
67f1407d5cSKuninori Morimoto  * if platform doesn't have callback, it return 0 (no error)
68f1407d5cSKuninori Morimoto  */
69f1407d5cSKuninori Morimoto #define usbhs_platform_call(priv, func, args...)\
70f1407d5cSKuninori Morimoto 	(!(priv) ? -ENODEV :			\
7148298206SKuninori Morimoto 	 !((priv)->pfunc.func) ? 0 :		\
7248298206SKuninori Morimoto 	 (priv)->pfunc.func(args))
73f1407d5cSKuninori Morimoto 
74f1407d5cSKuninori Morimoto /*
75f1407d5cSKuninori Morimoto  *		common functions
76f1407d5cSKuninori Morimoto  */
77f1407d5cSKuninori Morimoto u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
78f1407d5cSKuninori Morimoto {
79f1407d5cSKuninori Morimoto 	return ioread16(priv->base + reg);
80f1407d5cSKuninori Morimoto }
81f1407d5cSKuninori Morimoto 
82f1407d5cSKuninori Morimoto void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
83f1407d5cSKuninori Morimoto {
84f1407d5cSKuninori Morimoto 	iowrite16(data, priv->base + reg);
85f1407d5cSKuninori Morimoto }
86f1407d5cSKuninori Morimoto 
87f1407d5cSKuninori Morimoto void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
88f1407d5cSKuninori Morimoto {
89f1407d5cSKuninori Morimoto 	u16 val = usbhs_read(priv, reg);
90f1407d5cSKuninori Morimoto 
91f1407d5cSKuninori Morimoto 	val &= ~mask;
92f1407d5cSKuninori Morimoto 	val |= data & mask;
93f1407d5cSKuninori Morimoto 
94f1407d5cSKuninori Morimoto 	usbhs_write(priv, reg, val);
95f1407d5cSKuninori Morimoto }
96f1407d5cSKuninori Morimoto 
97206dcc2cSKuninori Morimoto struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
98206dcc2cSKuninori Morimoto {
99206dcc2cSKuninori Morimoto 	return dev_get_drvdata(&pdev->dev);
100206dcc2cSKuninori Morimoto }
101206dcc2cSKuninori Morimoto 
102f1407d5cSKuninori Morimoto /*
103f1407d5cSKuninori Morimoto  *		syscfg functions
104f1407d5cSKuninori Morimoto  */
10576190152SKuninori Morimoto static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
106f1407d5cSKuninori Morimoto {
107f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
108f1407d5cSKuninori Morimoto }
109f1407d5cSKuninori Morimoto 
110f1407d5cSKuninori Morimoto void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
111f1407d5cSKuninori Morimoto {
1123244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
1133244a7b4SKuninori Morimoto 	u16 val  = DCFM | DRPD | HSE | USBE;
114f427eb64SKuninori Morimoto 	int has_otg = usbhs_get_dparam(priv, has_otg);
115f427eb64SKuninori Morimoto 
116f427eb64SKuninori Morimoto 	if (has_otg)
117f427eb64SKuninori Morimoto 		usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN));
118f1407d5cSKuninori Morimoto 
119f1407d5cSKuninori Morimoto 	/*
120f1407d5cSKuninori Morimoto 	 * if enable
121f1407d5cSKuninori Morimoto 	 *
122f1407d5cSKuninori Morimoto 	 * - select Host mode
123f1407d5cSKuninori Morimoto 	 * - D+ Line/D- Line Pull-down
124f1407d5cSKuninori Morimoto 	 */
125f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
126f1407d5cSKuninori Morimoto }
127f1407d5cSKuninori Morimoto 
128f1407d5cSKuninori Morimoto void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
129f1407d5cSKuninori Morimoto {
1303244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
13104a5def3STakeshi Kihara 	u16 val  = HSE | USBE;
132f1407d5cSKuninori Morimoto 
133f1407d5cSKuninori Morimoto 	/*
134f1407d5cSKuninori Morimoto 	 * if enable
135f1407d5cSKuninori Morimoto 	 *
136f1407d5cSKuninori Morimoto 	 * - select Function mode
13704a5def3STakeshi Kihara 	 * - D+ Line Pull-up is disabled
13804a5def3STakeshi Kihara 	 *      When D+ Line Pull-up is enabled,
13904a5def3STakeshi Kihara 	 *      calling usbhs_sys_function_pullup(,1)
140f1407d5cSKuninori Morimoto 	 */
141f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
142f1407d5cSKuninori Morimoto }
143f1407d5cSKuninori Morimoto 
1444cd2f599Skuninori.morimoto.gx@renesas.com void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable)
1454cd2f599Skuninori.morimoto.gx@renesas.com {
1464cd2f599Skuninori.morimoto.gx@renesas.com 	usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0);
1474cd2f599Skuninori.morimoto.gx@renesas.com }
1484cd2f599Skuninori.morimoto.gx@renesas.com 
149dfbb7f4fSKuninori Morimoto void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
150dfbb7f4fSKuninori Morimoto {
151dfbb7f4fSKuninori Morimoto 	usbhs_write(priv, TESTMODE, mode);
152dfbb7f4fSKuninori Morimoto }
153dfbb7f4fSKuninori Morimoto 
154f1407d5cSKuninori Morimoto /*
155f1407d5cSKuninori Morimoto  *		frame functions
156f1407d5cSKuninori Morimoto  */
157f1407d5cSKuninori Morimoto int usbhs_frame_get_num(struct usbhs_priv *priv)
158f1407d5cSKuninori Morimoto {
159f1407d5cSKuninori Morimoto 	return usbhs_read(priv, FRMNUM) & FRNM_MASK;
160f1407d5cSKuninori Morimoto }
161f1407d5cSKuninori Morimoto 
162f1407d5cSKuninori Morimoto /*
163ef8bedb9SKuninori Morimoto  *		usb request functions
164ef8bedb9SKuninori Morimoto  */
165ef8bedb9SKuninori Morimoto void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
166ef8bedb9SKuninori Morimoto {
167ef8bedb9SKuninori Morimoto 	u16 val;
168ef8bedb9SKuninori Morimoto 
169ef8bedb9SKuninori Morimoto 	val = usbhs_read(priv, USBREQ);
170ef8bedb9SKuninori Morimoto 	req->bRequest		= (val >> 8) & 0xFF;
171ef8bedb9SKuninori Morimoto 	req->bRequestType	= (val >> 0) & 0xFF;
172ef8bedb9SKuninori Morimoto 
173ef8bedb9SKuninori Morimoto 	req->wValue	= usbhs_read(priv, USBVAL);
174ef8bedb9SKuninori Morimoto 	req->wIndex	= usbhs_read(priv, USBINDX);
175ef8bedb9SKuninori Morimoto 	req->wLength	= usbhs_read(priv, USBLENG);
176ef8bedb9SKuninori Morimoto }
177ef8bedb9SKuninori Morimoto 
178ef8bedb9SKuninori Morimoto void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
179ef8bedb9SKuninori Morimoto {
180ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBREQ,  (req->bRequest << 8) | req->bRequestType);
181ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBVAL,  req->wValue);
182ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBINDX, req->wIndex);
183ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBLENG, req->wLength);
184ef8bedb9SKuninori Morimoto 
185ef8bedb9SKuninori Morimoto 	usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
186ef8bedb9SKuninori Morimoto }
187ef8bedb9SKuninori Morimoto 
188ef8bedb9SKuninori Morimoto /*
189258485d9SKuninori Morimoto  *		bus/vbus functions
190258485d9SKuninori Morimoto  */
191258485d9SKuninori Morimoto void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
192258485d9SKuninori Morimoto {
193a9be4a45SKuninori Morimoto 	u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
194a9be4a45SKuninori Morimoto 
195a9be4a45SKuninori Morimoto 	if (status != USBRST) {
196a9be4a45SKuninori Morimoto 		struct device *dev = usbhs_priv_to_dev(priv);
197a9be4a45SKuninori Morimoto 		dev_err(dev, "usbhs should be reset\n");
198a9be4a45SKuninori Morimoto 	}
199a9be4a45SKuninori Morimoto 
200258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
201258485d9SKuninori Morimoto }
202258485d9SKuninori Morimoto 
203258485d9SKuninori Morimoto void usbhs_bus_send_reset(struct usbhs_priv *priv)
204258485d9SKuninori Morimoto {
205258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
206258485d9SKuninori Morimoto }
207258485d9SKuninori Morimoto 
20875587f52SKuninori Morimoto int usbhs_bus_get_speed(struct usbhs_priv *priv)
20975587f52SKuninori Morimoto {
21075587f52SKuninori Morimoto 	u16 dvstctr = usbhs_read(priv, DVSTCTR);
21175587f52SKuninori Morimoto 
21275587f52SKuninori Morimoto 	switch (RHST & dvstctr) {
21375587f52SKuninori Morimoto 	case RHST_LOW_SPEED:
21475587f52SKuninori Morimoto 		return USB_SPEED_LOW;
21575587f52SKuninori Morimoto 	case RHST_FULL_SPEED:
21675587f52SKuninori Morimoto 		return USB_SPEED_FULL;
21775587f52SKuninori Morimoto 	case RHST_HIGH_SPEED:
21875587f52SKuninori Morimoto 		return USB_SPEED_HIGH;
21975587f52SKuninori Morimoto 	}
22075587f52SKuninori Morimoto 
22175587f52SKuninori Morimoto 	return USB_SPEED_UNKNOWN;
22275587f52SKuninori Morimoto }
22375587f52SKuninori Morimoto 
224258485d9SKuninori Morimoto int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
225258485d9SKuninori Morimoto {
226258485d9SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
227258485d9SKuninori Morimoto 
228258485d9SKuninori Morimoto 	return usbhs_platform_call(priv, set_vbus, pdev, enable);
229258485d9SKuninori Morimoto }
230258485d9SKuninori Morimoto 
231258485d9SKuninori Morimoto static void usbhsc_bus_init(struct usbhs_priv *priv)
232258485d9SKuninori Morimoto {
233258485d9SKuninori Morimoto 	usbhs_write(priv, DVSTCTR, 0);
234258485d9SKuninori Morimoto 
235258485d9SKuninori Morimoto 	usbhs_vbus_ctrl(priv, 0);
236258485d9SKuninori Morimoto }
237258485d9SKuninori Morimoto 
238258485d9SKuninori Morimoto /*
239eb05191fSKuninori Morimoto  *		device configuration
240eb05191fSKuninori Morimoto  */
2413dd49268SKuninori Morimoto int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
242eb05191fSKuninori Morimoto 			   u16 upphub, u16 hubport, u16 speed)
243eb05191fSKuninori Morimoto {
244eb05191fSKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
245eb05191fSKuninori Morimoto 	u16 usbspd = 0;
246eb05191fSKuninori Morimoto 	u32 reg = DEVADD0 + (2 * devnum);
247eb05191fSKuninori Morimoto 
248eb05191fSKuninori Morimoto 	if (devnum > 10) {
249eb05191fSKuninori Morimoto 		dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
250eb05191fSKuninori Morimoto 		return -EIO;
251eb05191fSKuninori Morimoto 	}
252eb05191fSKuninori Morimoto 
253eb05191fSKuninori Morimoto 	if (upphub > 0xA) {
254eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported hub number %d\n", upphub);
255eb05191fSKuninori Morimoto 		return -EIO;
256eb05191fSKuninori Morimoto 	}
257eb05191fSKuninori Morimoto 
258eb05191fSKuninori Morimoto 	switch (speed) {
259eb05191fSKuninori Morimoto 	case USB_SPEED_LOW:
260eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_LOW;
261eb05191fSKuninori Morimoto 		break;
262eb05191fSKuninori Morimoto 	case USB_SPEED_FULL:
263eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_FULL;
264eb05191fSKuninori Morimoto 		break;
265eb05191fSKuninori Morimoto 	case USB_SPEED_HIGH:
266eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_HIGH;
267eb05191fSKuninori Morimoto 		break;
268eb05191fSKuninori Morimoto 	default:
269eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported speed %d\n", speed);
270eb05191fSKuninori Morimoto 		return -EIO;
271eb05191fSKuninori Morimoto 	}
272eb05191fSKuninori Morimoto 
273eb05191fSKuninori Morimoto 	usbhs_write(priv, reg,	UPPHUB(upphub)	|
274eb05191fSKuninori Morimoto 				HUBPORT(hubport)|
275eb05191fSKuninori Morimoto 				USBSPD(usbspd));
276eb05191fSKuninori Morimoto 
277eb05191fSKuninori Morimoto 	return 0;
278eb05191fSKuninori Morimoto }
279eb05191fSKuninori Morimoto 
280eb05191fSKuninori Morimoto /*
281ab330cf3SYoshihiro Shimoda  *		interrupt functions
282ab330cf3SYoshihiro Shimoda  */
283ab330cf3SYoshihiro Shimoda void usbhs_xxxsts_clear(struct usbhs_priv *priv, u16 sts_reg, u16 bit)
284ab330cf3SYoshihiro Shimoda {
285ab330cf3SYoshihiro Shimoda 	u16 pipe_mask = (u16)GENMASK(usbhs_get_dparam(priv, pipe_size), 0);
286ab330cf3SYoshihiro Shimoda 
287ab330cf3SYoshihiro Shimoda 	usbhs_write(priv, sts_reg, ~(1 << bit) & pipe_mask);
288ab330cf3SYoshihiro Shimoda }
289ab330cf3SYoshihiro Shimoda 
290ab330cf3SYoshihiro Shimoda /*
291f1407d5cSKuninori Morimoto  *		local functions
292f1407d5cSKuninori Morimoto  */
29311935de5SKuninori Morimoto static void usbhsc_set_buswait(struct usbhs_priv *priv)
294f1407d5cSKuninori Morimoto {
295f1407d5cSKuninori Morimoto 	int wait = usbhs_get_dparam(priv, buswait_bwait);
296f1407d5cSKuninori Morimoto 
297f1407d5cSKuninori Morimoto 	/* set bus wait if platform have */
298f1407d5cSKuninori Morimoto 	if (wait)
299f1407d5cSKuninori Morimoto 		usbhs_bset(priv, BUSWAIT, 0x000F, wait);
300f1407d5cSKuninori Morimoto }
301f1407d5cSKuninori Morimoto 
302f1407d5cSKuninori Morimoto /*
303f1407d5cSKuninori Morimoto  *		platform default param
304f1407d5cSKuninori Morimoto  */
3058ecef00fSUlrich Hecht 
3068ecef00fSUlrich Hecht /* commonly used on old SH-Mobile SoCs */
30751f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_default_pipe[] = {
30851f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
30951f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, false),
31051f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x18, false),
31151f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x28, true),
31251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x38, true),
31351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
31451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
31551f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
31651f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
31751f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x07, false),
318f1407d5cSKuninori Morimoto };
319f1407d5cSKuninori Morimoto 
3208ecef00fSUlrich Hecht /* commonly used on newer SH-Mobile and R-Car SoCs */
32151f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_new_pipe[] = {
32251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
32351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true),
32451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true),
32551f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
32651f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true),
32751f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true),
32851f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
32951f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
33051f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
33151f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x78, true),
33251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x88, true),
33351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x98, true),
33451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xa8, true),
33551f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xb8, true),
33651f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xc8, true),
33751f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xd8, true),
3388ecef00fSUlrich Hecht };
3398ecef00fSUlrich Hecht 
340f1407d5cSKuninori Morimoto /*
3416e267da8SKuninori Morimoto  *		power control
3426e267da8SKuninori Morimoto  */
3436e267da8SKuninori Morimoto static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
3446e267da8SKuninori Morimoto {
345f1ee56a0SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
3466e267da8SKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
3476e267da8SKuninori Morimoto 
3486e267da8SKuninori Morimoto 	if (enable) {
3496e267da8SKuninori Morimoto 		/* enable PM */
3506e267da8SKuninori Morimoto 		pm_runtime_get_sync(dev);
3516e267da8SKuninori Morimoto 
352f1ee56a0SKuninori Morimoto 		/* enable platform power */
353f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
354f1ee56a0SKuninori Morimoto 
3556e267da8SKuninori Morimoto 		/* USB on */
3566e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
3576e267da8SKuninori Morimoto 	} else {
3586e267da8SKuninori Morimoto 		/* USB off */
3596e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
3606e267da8SKuninori Morimoto 
361f1ee56a0SKuninori Morimoto 		/* disable platform power */
362f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
363f1ee56a0SKuninori Morimoto 
3646e267da8SKuninori Morimoto 		/* disable PM */
3656e267da8SKuninori Morimoto 		pm_runtime_put_sync(dev);
3666e267da8SKuninori Morimoto 	}
3676e267da8SKuninori Morimoto }
3686e267da8SKuninori Morimoto 
3696e267da8SKuninori Morimoto /*
370ca8a282aSKuninori Morimoto  *		hotplug
371f1407d5cSKuninori Morimoto  */
372ca8a282aSKuninori Morimoto static void usbhsc_hotplug(struct usbhs_priv *priv)
373f1407d5cSKuninori Morimoto {
374f1407d5cSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
375f1407d5cSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
376f1407d5cSKuninori Morimoto 	int id;
377f1407d5cSKuninori Morimoto 	int enable;
378d7b3968dSSergei Shtylyov 	int cable;
379f1407d5cSKuninori Morimoto 	int ret;
380f1407d5cSKuninori Morimoto 
381f1407d5cSKuninori Morimoto 	/*
382f1407d5cSKuninori Morimoto 	 * get vbus status from platform
383f1407d5cSKuninori Morimoto 	 */
384f1407d5cSKuninori Morimoto 	enable = usbhs_platform_call(priv, get_vbus, pdev);
385f1407d5cSKuninori Morimoto 
386f1407d5cSKuninori Morimoto 	/*
387f1407d5cSKuninori Morimoto 	 * get id from platform
388f1407d5cSKuninori Morimoto 	 */
389f1407d5cSKuninori Morimoto 	id = usbhs_platform_call(priv, get_id, pdev);
390f1407d5cSKuninori Morimoto 
391f1407d5cSKuninori Morimoto 	if (enable && !mod) {
392d7b3968dSSergei Shtylyov 		if (priv->edev) {
393ea07b8cfSChanwoo Choi 			cable = extcon_get_state(priv->edev, EXTCON_USB_HOST);
394d7b3968dSSergei Shtylyov 			if ((cable > 0 && id != USBHS_HOST) ||
395d7b3968dSSergei Shtylyov 			    (!cable && id != USBHS_GADGET)) {
396d7b3968dSSergei Shtylyov 				dev_info(&pdev->dev,
397d7b3968dSSergei Shtylyov 					 "USB cable plugged in doesn't match the selected role!\n");
398d7b3968dSSergei Shtylyov 				return;
399d7b3968dSSergei Shtylyov 			}
400d7b3968dSSergei Shtylyov 		}
401d7b3968dSSergei Shtylyov 
402f1407d5cSKuninori Morimoto 		ret = usbhs_mod_change(priv, id);
403f1407d5cSKuninori Morimoto 		if (ret < 0)
404f1407d5cSKuninori Morimoto 			return;
405f1407d5cSKuninori Morimoto 
406f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s enable\n", __func__);
407f1407d5cSKuninori Morimoto 
4086e267da8SKuninori Morimoto 		/* power on */
409b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
4106e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
411f1407d5cSKuninori Morimoto 
412258485d9SKuninori Morimoto 		/* bus init */
413258485d9SKuninori Morimoto 		usbhsc_set_buswait(priv);
414258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
415258485d9SKuninori Morimoto 
416f1407d5cSKuninori Morimoto 		/* module start */
417f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, start, priv);
418f1407d5cSKuninori Morimoto 
419f1407d5cSKuninori Morimoto 	} else if (!enable && mod) {
420f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s disable\n", __func__);
421f1407d5cSKuninori Morimoto 
422f1407d5cSKuninori Morimoto 		/* module stop */
423f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
424f1407d5cSKuninori Morimoto 
425258485d9SKuninori Morimoto 		/* bus init */
426258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
427258485d9SKuninori Morimoto 
4286e267da8SKuninori Morimoto 		/* power off */
429b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
4306e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
431f1407d5cSKuninori Morimoto 
432f1407d5cSKuninori Morimoto 		usbhs_mod_change(priv, -1);
433f1407d5cSKuninori Morimoto 
434f1407d5cSKuninori Morimoto 		/* reset phy for next connection */
435f1407d5cSKuninori Morimoto 		usbhs_platform_call(priv, phy_reset, pdev);
436f1407d5cSKuninori Morimoto 	}
437f1407d5cSKuninori Morimoto }
438f1407d5cSKuninori Morimoto 
439ca8a282aSKuninori Morimoto /*
440ca8a282aSKuninori Morimoto  *		notify hotplug
441ca8a282aSKuninori Morimoto  */
442ca8a282aSKuninori Morimoto static void usbhsc_notify_hotplug(struct work_struct *work)
443ca8a282aSKuninori Morimoto {
444ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = container_of(work,
445ca8a282aSKuninori Morimoto 					       struct usbhs_priv,
446ca8a282aSKuninori Morimoto 					       notify_hotplug_work.work);
447ca8a282aSKuninori Morimoto 	usbhsc_hotplug(priv);
448ca8a282aSKuninori Morimoto }
449ca8a282aSKuninori Morimoto 
450b4fcea2aSKuninori Morimoto static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
451f1407d5cSKuninori Morimoto {
452206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
453bc57381eSKuninori Morimoto 	int delay = usbhs_get_dparam(priv, detection_delay);
454f1407d5cSKuninori Morimoto 
455f1407d5cSKuninori Morimoto 	/*
456f1407d5cSKuninori Morimoto 	 * This functions will be called in interrupt.
457f1407d5cSKuninori Morimoto 	 * To make sure safety context,
458f1407d5cSKuninori Morimoto 	 * use workqueue for usbhs_notify_hotplug
459f1407d5cSKuninori Morimoto 	 */
460a49a88f1SKuninori Morimoto 	schedule_delayed_work(&priv->notify_hotplug_work,
461a49a88f1SKuninori Morimoto 			      msecs_to_jiffies(delay));
462f1407d5cSKuninori Morimoto 	return 0;
463f1407d5cSKuninori Morimoto }
464f1407d5cSKuninori Morimoto 
465f1407d5cSKuninori Morimoto /*
466f1407d5cSKuninori Morimoto  *		platform functions
467f1407d5cSKuninori Morimoto  */
468b854100eSYoshihiro Shimoda static const struct of_device_id usbhs_of_match[] = {
469b854100eSYoshihiro Shimoda 	{
470b854100eSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7790",
471e0213bc5SYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
472b854100eSYoshihiro Shimoda 	},
473b854100eSYoshihiro Shimoda 	{
474b854100eSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7791",
475e0213bc5SYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
476b854100eSYoshihiro Shimoda 	},
477af6e613bSYoshihiro Shimoda 	{
478af6e613bSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7794",
479af6e613bSYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
480af6e613bSYoshihiro Shimoda 	},
481f5f6afa8SYoshihiro Shimoda 	{
482f5f6afa8SYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7795",
483de18757eSYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN3,
484f5f6afa8SYoshihiro Shimoda 	},
4852f1a993aSSimon Horman 	{
486aae819e1SYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7796",
487aae819e1SYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN3,
488aae819e1SYoshihiro Shimoda 	},
489aae819e1SYoshihiro Shimoda 	{
4900f38672cSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a77995",
4910f38672cSYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN3_WITH_PLL,
4920f38672cSYoshihiro Shimoda 	},
4930f38672cSYoshihiro Shimoda 	{
4942f1a993aSSimon Horman 		.compatible = "renesas,rcar-gen2-usbhs",
4952f1a993aSSimon Horman 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
4962f1a993aSSimon Horman 	},
4972f1a993aSSimon Horman 	{
4982f1a993aSSimon Horman 		.compatible = "renesas,rcar-gen3-usbhs",
499de18757eSYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN3,
5002f1a993aSSimon Horman 	},
501b854100eSYoshihiro Shimoda 	{ },
502b854100eSYoshihiro Shimoda };
503b854100eSYoshihiro Shimoda MODULE_DEVICE_TABLE(of, usbhs_of_match);
504b854100eSYoshihiro Shimoda 
505b854100eSYoshihiro Shimoda static struct renesas_usbhs_platform_info *usbhs_parse_dt(struct device *dev)
506b854100eSYoshihiro Shimoda {
507b854100eSYoshihiro Shimoda 	struct renesas_usbhs_platform_info *info;
508b854100eSYoshihiro Shimoda 	struct renesas_usbhs_driver_param *dparam;
509b854100eSYoshihiro Shimoda 	u32 tmp;
510b854100eSYoshihiro Shimoda 	int gpio;
511b854100eSYoshihiro Shimoda 
512b854100eSYoshihiro Shimoda 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
513b854100eSYoshihiro Shimoda 	if (!info)
514b854100eSYoshihiro Shimoda 		return NULL;
515b854100eSYoshihiro Shimoda 
516b854100eSYoshihiro Shimoda 	dparam = &info->driver_param;
517b61e47b4SGeert Uytterhoeven 	dparam->type = (uintptr_t)of_device_get_match_data(dev);
518b854100eSYoshihiro Shimoda 	if (!of_property_read_u32(dev->of_node, "renesas,buswait", &tmp))
519b854100eSYoshihiro Shimoda 		dparam->buswait_bwait = tmp;
520b854100eSYoshihiro Shimoda 	gpio = of_get_named_gpio_flags(dev->of_node, "renesas,enable-gpio", 0,
521b854100eSYoshihiro Shimoda 				       NULL);
522b854100eSYoshihiro Shimoda 	if (gpio > 0)
523b854100eSYoshihiro Shimoda 		dparam->enable_gpio = gpio;
524b854100eSYoshihiro Shimoda 
525772ce812SYoshihiro Shimoda 	if (dparam->type == USBHS_TYPE_RCAR_GEN2 ||
5260f38672cSYoshihiro Shimoda 	    dparam->type == USBHS_TYPE_RCAR_GEN3 ||
5270f38672cSYoshihiro Shimoda 	    dparam->type == USBHS_TYPE_RCAR_GEN3_WITH_PLL) {
528ab330cf3SYoshihiro Shimoda 		dparam->has_usb_dmac = 1;
529a877b8e5SYoshihiro Shimoda 		dparam->pipe_configs = usbhsc_new_pipe;
530a877b8e5SYoshihiro Shimoda 		dparam->pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
531a877b8e5SYoshihiro Shimoda 	}
532ab330cf3SYoshihiro Shimoda 
533b854100eSYoshihiro Shimoda 	return info;
534b854100eSYoshihiro Shimoda }
535b854100eSYoshihiro Shimoda 
536b7a8d17dSKuninori Morimoto static int usbhs_probe(struct platform_device *pdev)
537f1407d5cSKuninori Morimoto {
538ace0a5f9SJingoo Han 	struct renesas_usbhs_platform_info *info = dev_get_platdata(&pdev->dev);
539f1407d5cSKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc;
540f1407d5cSKuninori Morimoto 	struct usbhs_priv *priv;
54153069af3SShimoda, Yoshihiro 	struct resource *res, *irq_res;
542f1407d5cSKuninori Morimoto 	int ret;
543f1407d5cSKuninori Morimoto 
544b854100eSYoshihiro Shimoda 	/* check device node */
545b854100eSYoshihiro Shimoda 	if (pdev->dev.of_node)
546b854100eSYoshihiro Shimoda 		info = pdev->dev.platform_data = usbhs_parse_dt(&pdev->dev);
547b854100eSYoshihiro Shimoda 
548f1407d5cSKuninori Morimoto 	/* check platform information */
5498ecef00fSUlrich Hecht 	if (!info) {
550f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "no platform information\n");
551f1407d5cSKuninori Morimoto 		return -EINVAL;
552f1407d5cSKuninori Morimoto 	}
553f1407d5cSKuninori Morimoto 
554f1407d5cSKuninori Morimoto 	/* platform data */
55553069af3SShimoda, Yoshihiro 	irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
556687f16b8SVarka Bhadram 	if (!irq_res) {
557f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
558f1407d5cSKuninori Morimoto 		return -ENODEV;
559f1407d5cSKuninori Morimoto 	}
560f1407d5cSKuninori Morimoto 
561f1407d5cSKuninori Morimoto 	/* usb private data */
56258efc77cSKuninori Morimoto 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
563b1cb07cdSPeter Chen 	if (!priv)
564f1407d5cSKuninori Morimoto 		return -ENOMEM;
565f1407d5cSKuninori Morimoto 
566687f16b8SVarka Bhadram 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
567148e1134SThierry Reding 	priv->base = devm_ioremap_resource(&pdev->dev, res);
568148e1134SThierry Reding 	if (IS_ERR(priv->base))
569148e1134SThierry Reding 		return PTR_ERR(priv->base);
570f1407d5cSKuninori Morimoto 
571d7b3968dSSergei Shtylyov 	if (of_property_read_bool(pdev->dev.of_node, "extcon")) {
572d7b3968dSSergei Shtylyov 		priv->edev = extcon_get_edev_by_phandle(&pdev->dev, 0);
573d7b3968dSSergei Shtylyov 		if (IS_ERR(priv->edev))
574d7b3968dSSergei Shtylyov 			return PTR_ERR(priv->edev);
575d7b3968dSSergei Shtylyov 	}
576d7b3968dSSergei Shtylyov 
577f1407d5cSKuninori Morimoto 	/*
578f1407d5cSKuninori Morimoto 	 * care platform info
579f1407d5cSKuninori Morimoto 	 */
5808ecef00fSUlrich Hecht 
58148298206SKuninori Morimoto 	memcpy(&priv->dparam,
58248298206SKuninori Morimoto 	       &info->driver_param,
58348298206SKuninori Morimoto 	       sizeof(struct renesas_usbhs_driver_param));
584f1407d5cSKuninori Morimoto 
5858ecef00fSUlrich Hecht 	switch (priv->dparam.type) {
586e0213bc5SYoshihiro Shimoda 	case USBHS_TYPE_RCAR_GEN2:
5878ecef00fSUlrich Hecht 		priv->pfunc = usbhs_rcar2_ops;
5888ecef00fSUlrich Hecht 		break;
589de18757eSYoshihiro Shimoda 	case USBHS_TYPE_RCAR_GEN3:
590de18757eSYoshihiro Shimoda 		priv->pfunc = usbhs_rcar3_ops;
591de18757eSYoshihiro Shimoda 		break;
5920f38672cSYoshihiro Shimoda 	case USBHS_TYPE_RCAR_GEN3_WITH_PLL:
5930f38672cSYoshihiro Shimoda 		priv->pfunc = usbhs_rcar3_with_pll_ops;
5940f38672cSYoshihiro Shimoda 		break;
5958ecef00fSUlrich Hecht 	default:
5968ecef00fSUlrich Hecht 		if (!info->platform_callback.get_id) {
5978ecef00fSUlrich Hecht 			dev_err(&pdev->dev, "no platform callbacks");
5988ecef00fSUlrich Hecht 			return -EINVAL;
5998ecef00fSUlrich Hecht 		}
6008ecef00fSUlrich Hecht 		memcpy(&priv->pfunc,
6018ecef00fSUlrich Hecht 		       &info->platform_callback,
6028ecef00fSUlrich Hecht 		       sizeof(struct renesas_usbhs_platform_callback));
6038ecef00fSUlrich Hecht 		break;
6048ecef00fSUlrich Hecht 	}
6058ecef00fSUlrich Hecht 
606f1407d5cSKuninori Morimoto 	/* set driver callback functions for platform */
607f1407d5cSKuninori Morimoto 	dfunc			= &info->driver_callback;
608f1407d5cSKuninori Morimoto 	dfunc->notify_hotplug	= usbhsc_drvcllbck_notify_hotplug;
609f1407d5cSKuninori Morimoto 
610f1407d5cSKuninori Morimoto 	/* set default param if platform doesn't have */
61151f141a9SYoshihiro Shimoda 	if (!priv->dparam.pipe_configs) {
61251f141a9SYoshihiro Shimoda 		priv->dparam.pipe_configs = usbhsc_default_pipe;
61351f141a9SYoshihiro Shimoda 		priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe);
614f1407d5cSKuninori Morimoto 	}
61548298206SKuninori Morimoto 	if (!priv->dparam.pio_dma_border)
61648298206SKuninori Morimoto 		priv->dparam.pio_dma_border = 64; /* 64byte */
617f1407d5cSKuninori Morimoto 
618b002ff6eSKuninori Morimoto 	/* FIXME */
619b002ff6eSKuninori Morimoto 	/* runtime power control ? */
62048298206SKuninori Morimoto 	if (priv->pfunc.get_vbus)
621b002ff6eSKuninori Morimoto 		usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
622b002ff6eSKuninori Morimoto 
623f1407d5cSKuninori Morimoto 	/*
624f1407d5cSKuninori Morimoto 	 * priv settings
625f1407d5cSKuninori Morimoto 	 */
62653069af3SShimoda, Yoshihiro 	priv->irq	= irq_res->start;
62753069af3SShimoda, Yoshihiro 	if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
62853069af3SShimoda, Yoshihiro 		priv->irqflags = IRQF_SHARED;
629f1407d5cSKuninori Morimoto 	priv->pdev	= pdev;
630bc57381eSKuninori Morimoto 	INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
631f1407d5cSKuninori Morimoto 	spin_lock_init(usbhs_priv_to_lock(priv));
632f1407d5cSKuninori Morimoto 
633f1407d5cSKuninori Morimoto 	/* call pipe and module init */
634f1407d5cSKuninori Morimoto 	ret = usbhs_pipe_probe(priv);
635f1407d5cSKuninori Morimoto 	if (ret < 0)
63658efc77cSKuninori Morimoto 		return ret;
637f1407d5cSKuninori Morimoto 
638d3af90a5SKuninori Morimoto 	ret = usbhs_fifo_probe(priv);
639f1407d5cSKuninori Morimoto 	if (ret < 0)
64097f93227SKuninori Morimoto 		goto probe_end_pipe_exit;
641f1407d5cSKuninori Morimoto 
642d3af90a5SKuninori Morimoto 	ret = usbhs_mod_probe(priv);
643d3af90a5SKuninori Morimoto 	if (ret < 0)
644d3af90a5SKuninori Morimoto 		goto probe_end_fifo_exit;
645d3af90a5SKuninori Morimoto 
646f1407d5cSKuninori Morimoto 	/* dev_set_drvdata should be called after usbhs_mod_init */
647c9a0552eSLibo Chen 	platform_set_drvdata(pdev, priv);
648f1407d5cSKuninori Morimoto 
649f1407d5cSKuninori Morimoto 	/*
650f1407d5cSKuninori Morimoto 	 * deviece reset here because
651f1407d5cSKuninori Morimoto 	 * USB device might be used in boot loader.
652f1407d5cSKuninori Morimoto 	 */
653f1407d5cSKuninori Morimoto 	usbhs_sys_clock_ctrl(priv, 0);
654f1407d5cSKuninori Morimoto 
6558ecef00fSUlrich Hecht 	/* check GPIO determining if USB function should be enabled */
6568ecef00fSUlrich Hecht 	if (priv->dparam.enable_gpio) {
6578ecef00fSUlrich Hecht 		gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL);
6588ecef00fSUlrich Hecht 		ret = !gpio_get_value(priv->dparam.enable_gpio);
6598ecef00fSUlrich Hecht 		gpio_free(priv->dparam.enable_gpio);
6608ecef00fSUlrich Hecht 		if (ret) {
6618ecef00fSUlrich Hecht 			dev_warn(&pdev->dev,
6628ecef00fSUlrich Hecht 				 "USB function not selected (GPIO %d)\n",
6638ecef00fSUlrich Hecht 				 priv->dparam.enable_gpio);
6648ecef00fSUlrich Hecht 			ret = -ENOTSUPP;
6658ecef00fSUlrich Hecht 			goto probe_end_mod_exit;
6668ecef00fSUlrich Hecht 		}
6678ecef00fSUlrich Hecht 	}
6688ecef00fSUlrich Hecht 
669f1407d5cSKuninori Morimoto 	/*
670f1407d5cSKuninori Morimoto 	 * platform call
671f1407d5cSKuninori Morimoto 	 *
672f1407d5cSKuninori Morimoto 	 * USB phy setup might depend on CPU/Board.
673f1407d5cSKuninori Morimoto 	 * If platform has its callback functions,
674f1407d5cSKuninori Morimoto 	 * call it here.
675f1407d5cSKuninori Morimoto 	 */
676f1407d5cSKuninori Morimoto 	ret = usbhs_platform_call(priv, hardware_init, pdev);
677f1407d5cSKuninori Morimoto 	if (ret < 0) {
67848ac1e57SSergei Shtylyov 		dev_err(&pdev->dev, "platform init failed.\n");
67997f93227SKuninori Morimoto 		goto probe_end_mod_exit;
680f1407d5cSKuninori Morimoto 	}
681f1407d5cSKuninori Morimoto 
682f1407d5cSKuninori Morimoto 	/* reset phy for connection */
683f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
684f1407d5cSKuninori Morimoto 
685b002ff6eSKuninori Morimoto 	/* power control */
686b002ff6eSKuninori Morimoto 	pm_runtime_enable(&pdev->dev);
687b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
688b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
689b002ff6eSKuninori Morimoto 		usbhs_mod_autonomy_mode(priv);
690b002ff6eSKuninori Morimoto 	}
691b002ff6eSKuninori Morimoto 
692f1407d5cSKuninori Morimoto 	/*
693f1407d5cSKuninori Morimoto 	 * manual call notify_hotplug for cold plug
694f1407d5cSKuninori Morimoto 	 */
695368504c0SSergei Shtylyov 	usbhsc_drvcllbck_notify_hotplug(pdev);
696f1407d5cSKuninori Morimoto 
697f1407d5cSKuninori Morimoto 	dev_info(&pdev->dev, "probed\n");
698f1407d5cSKuninori Morimoto 
699f1407d5cSKuninori Morimoto 	return ret;
700f1407d5cSKuninori Morimoto 
701f1407d5cSKuninori Morimoto probe_end_mod_exit:
702f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
703d3af90a5SKuninori Morimoto probe_end_fifo_exit:
704d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
70597f93227SKuninori Morimoto probe_end_pipe_exit:
70697f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
707f1407d5cSKuninori Morimoto 
7080d799503SYoshihiro Shimoda 	dev_info(&pdev->dev, "probe failed (%d)\n", ret);
709f1407d5cSKuninori Morimoto 
710f1407d5cSKuninori Morimoto 	return ret;
711f1407d5cSKuninori Morimoto }
712f1407d5cSKuninori Morimoto 
713fb4e98abSBill Pemberton static int usbhs_remove(struct platform_device *pdev)
714f1407d5cSKuninori Morimoto {
715206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
716ace0a5f9SJingoo Han 	struct renesas_usbhs_platform_info *info = dev_get_platdata(&pdev->dev);
717af32fe51SKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
718f1407d5cSKuninori Morimoto 
719f1407d5cSKuninori Morimoto 	dev_dbg(&pdev->dev, "usb remove\n");
720f1407d5cSKuninori Morimoto 
721af32fe51SKuninori Morimoto 	dfunc->notify_hotplug = NULL;
722af32fe51SKuninori Morimoto 
723b002ff6eSKuninori Morimoto 	/* power off */
724b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
725b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
726f1407d5cSKuninori Morimoto 
727b002ff6eSKuninori Morimoto 	pm_runtime_disable(&pdev->dev);
728f1407d5cSKuninori Morimoto 
729f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, hardware_exit, pdev);
730f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
731d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
73297f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
733f1407d5cSKuninori Morimoto 
734f1407d5cSKuninori Morimoto 	return 0;
735f1407d5cSKuninori Morimoto }
736f1407d5cSKuninori Morimoto 
737ca8a282aSKuninori Morimoto static int usbhsc_suspend(struct device *dev)
738ca8a282aSKuninori Morimoto {
739ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
740ca8a282aSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
741ca8a282aSKuninori Morimoto 
742ca8a282aSKuninori Morimoto 	if (mod) {
743ca8a282aSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
744ca8a282aSKuninori Morimoto 		usbhs_mod_change(priv, -1);
745ca8a282aSKuninori Morimoto 	}
746ca8a282aSKuninori Morimoto 
747ca8a282aSKuninori Morimoto 	if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
748ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
749ca8a282aSKuninori Morimoto 
750ca8a282aSKuninori Morimoto 	return 0;
751ca8a282aSKuninori Morimoto }
752ca8a282aSKuninori Morimoto 
753ca8a282aSKuninori Morimoto static int usbhsc_resume(struct device *dev)
754ca8a282aSKuninori Morimoto {
755ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
756ca8a282aSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
757ca8a282aSKuninori Morimoto 
75859a0879aSYoshihiro Shimoda 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
759ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
76059a0879aSYoshihiro Shimoda 		usbhs_mod_autonomy_mode(priv);
76159a0879aSYoshihiro Shimoda 	}
762ca8a282aSKuninori Morimoto 
7635b50d3b5SKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
7645b50d3b5SKuninori Morimoto 
7655b50d3b5SKuninori Morimoto 	usbhsc_drvcllbck_notify_hotplug(pdev);
766ca8a282aSKuninori Morimoto 
767ca8a282aSKuninori Morimoto 	return 0;
768ca8a282aSKuninori Morimoto }
769ca8a282aSKuninori Morimoto 
770ca8a282aSKuninori Morimoto static int usbhsc_runtime_nop(struct device *dev)
771ca8a282aSKuninori Morimoto {
772ca8a282aSKuninori Morimoto 	/* Runtime PM callback shared between ->runtime_suspend()
773ca8a282aSKuninori Morimoto 	 * and ->runtime_resume(). Simply returns success.
774ca8a282aSKuninori Morimoto 	 *
775ca8a282aSKuninori Morimoto 	 * This driver re-initializes all registers after
776ca8a282aSKuninori Morimoto 	 * pm_runtime_get_sync() anyway so there is no need
777ca8a282aSKuninori Morimoto 	 * to save and restore registers here.
778ca8a282aSKuninori Morimoto 	 */
779ca8a282aSKuninori Morimoto 	return 0;
780ca8a282aSKuninori Morimoto }
781ca8a282aSKuninori Morimoto 
782ca8a282aSKuninori Morimoto static const struct dev_pm_ops usbhsc_pm_ops = {
783ca8a282aSKuninori Morimoto 	.suspend		= usbhsc_suspend,
784ca8a282aSKuninori Morimoto 	.resume			= usbhsc_resume,
785ca8a282aSKuninori Morimoto 	.runtime_suspend	= usbhsc_runtime_nop,
786ca8a282aSKuninori Morimoto 	.runtime_resume		= usbhsc_runtime_nop,
787ca8a282aSKuninori Morimoto };
788ca8a282aSKuninori Morimoto 
789f1407d5cSKuninori Morimoto static struct platform_driver renesas_usbhs_driver = {
790f1407d5cSKuninori Morimoto 	.driver		= {
791f1407d5cSKuninori Morimoto 		.name	= "renesas_usbhs",
792ca8a282aSKuninori Morimoto 		.pm	= &usbhsc_pm_ops,
793b854100eSYoshihiro Shimoda 		.of_match_table = of_match_ptr(usbhs_of_match),
794f1407d5cSKuninori Morimoto 	},
795f1407d5cSKuninori Morimoto 	.probe		= usbhs_probe,
7967690417dSBill Pemberton 	.remove		= usbhs_remove,
797f1407d5cSKuninori Morimoto };
798f1407d5cSKuninori Morimoto 
799cc27c96cSAxel Lin module_platform_driver(renesas_usbhs_driver);
800f1407d5cSKuninori Morimoto 
801f1407d5cSKuninori Morimoto MODULE_LICENSE("GPL");
802f1407d5cSKuninori Morimoto MODULE_DESCRIPTION("Renesas USB driver");
803f1407d5cSKuninori Morimoto MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
804