xref: /openbmc/linux/drivers/usb/renesas_usbhs/common.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
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.
60966648dSYoshihiro Shimoda  * Copyright (C) 2019 Renesas Electronics Corporation
7f1407d5cSKuninori Morimoto  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
8f1407d5cSKuninori Morimoto  */
93df0e240SYoshihiro Shimoda #include <linux/clk.h>
10148e1134SThierry Reding #include <linux/err.h>
113b31ec18SLinus Walleij #include <linux/gpio/consumer.h>
12f1407d5cSKuninori Morimoto #include <linux/io.h>
13f1407d5cSKuninori Morimoto #include <linux/module.h>
14*484468fbSRob Herring #include <linux/of.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 
46f1407d5cSKuninori Morimoto /*
47f1407d5cSKuninori Morimoto  * platform call back
48f1407d5cSKuninori Morimoto  *
49f1407d5cSKuninori Morimoto  * renesas usb support platform callback function.
50f1407d5cSKuninori Morimoto  * Below macro call it.
51f1407d5cSKuninori Morimoto  * if platform doesn't have callback, it return 0 (no error)
52f1407d5cSKuninori Morimoto  */
53f1407d5cSKuninori Morimoto #define usbhs_platform_call(priv, func, args...)\
54f1407d5cSKuninori Morimoto 	(!(priv) ? -ENODEV :			\
55426d3ff2SYoshihiro Shimoda 	 !((priv)->pfunc->func) ? 0 :		\
56426d3ff2SYoshihiro Shimoda 	 (priv)->pfunc->func(args))
57f1407d5cSKuninori Morimoto 
58f1407d5cSKuninori Morimoto /*
59f1407d5cSKuninori Morimoto  *		common functions
60f1407d5cSKuninori Morimoto  */
usbhs_read(struct usbhs_priv * priv,u32 reg)61f1407d5cSKuninori Morimoto u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
62f1407d5cSKuninori Morimoto {
63f1407d5cSKuninori Morimoto 	return ioread16(priv->base + reg);
64f1407d5cSKuninori Morimoto }
65f1407d5cSKuninori Morimoto 
usbhs_write(struct usbhs_priv * priv,u32 reg,u16 data)66f1407d5cSKuninori Morimoto void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
67f1407d5cSKuninori Morimoto {
68f1407d5cSKuninori Morimoto 	iowrite16(data, priv->base + reg);
69f1407d5cSKuninori Morimoto }
70f1407d5cSKuninori Morimoto 
usbhs_bset(struct usbhs_priv * priv,u32 reg,u16 mask,u16 data)71f1407d5cSKuninori Morimoto void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
72f1407d5cSKuninori Morimoto {
73f1407d5cSKuninori Morimoto 	u16 val = usbhs_read(priv, reg);
74f1407d5cSKuninori Morimoto 
75f1407d5cSKuninori Morimoto 	val &= ~mask;
76f1407d5cSKuninori Morimoto 	val |= data & mask;
77f1407d5cSKuninori Morimoto 
78f1407d5cSKuninori Morimoto 	usbhs_write(priv, reg, val);
79f1407d5cSKuninori Morimoto }
80f1407d5cSKuninori Morimoto 
usbhs_pdev_to_priv(struct platform_device * pdev)81206dcc2cSKuninori Morimoto struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
82206dcc2cSKuninori Morimoto {
83206dcc2cSKuninori Morimoto 	return dev_get_drvdata(&pdev->dev);
84206dcc2cSKuninori Morimoto }
85206dcc2cSKuninori Morimoto 
usbhs_get_id_as_gadget(struct platform_device * pdev)86be0a42a7SYoshihiro Shimoda int usbhs_get_id_as_gadget(struct platform_device *pdev)
87be0a42a7SYoshihiro Shimoda {
88be0a42a7SYoshihiro Shimoda 	return USBHS_GADGET;
89be0a42a7SYoshihiro Shimoda }
90be0a42a7SYoshihiro Shimoda 
91f1407d5cSKuninori Morimoto /*
92f1407d5cSKuninori Morimoto  *		syscfg functions
93f1407d5cSKuninori Morimoto  */
usbhs_sys_clock_ctrl(struct usbhs_priv * priv,int enable)9476190152SKuninori Morimoto static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
95f1407d5cSKuninori Morimoto {
96f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
97f1407d5cSKuninori Morimoto }
98f1407d5cSKuninori Morimoto 
usbhs_sys_host_ctrl(struct usbhs_priv * priv,int enable)99f1407d5cSKuninori Morimoto void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
100f1407d5cSKuninori Morimoto {
1013244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
1023244a7b4SKuninori Morimoto 	u16 val  = DCFM | DRPD | HSE | USBE;
103f1407d5cSKuninori Morimoto 
104f1407d5cSKuninori Morimoto 	/*
105f1407d5cSKuninori Morimoto 	 * if enable
106f1407d5cSKuninori Morimoto 	 *
107f1407d5cSKuninori Morimoto 	 * - select Host mode
108f1407d5cSKuninori Morimoto 	 * - D+ Line/D- Line Pull-down
109f1407d5cSKuninori Morimoto 	 */
110f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
111f1407d5cSKuninori Morimoto }
112f1407d5cSKuninori Morimoto 
usbhs_sys_function_ctrl(struct usbhs_priv * priv,int enable)113f1407d5cSKuninori Morimoto void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
114f1407d5cSKuninori Morimoto {
1153244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
11604a5def3STakeshi Kihara 	u16 val  = HSE | USBE;
117f1407d5cSKuninori Morimoto 
1182195e3afSChris Brandt 	/* CNEN bit is required for function operation */
1192195e3afSChris Brandt 	if (usbhs_get_dparam(priv, has_cnen)) {
1202195e3afSChris Brandt 		mask |= CNEN;
1212195e3afSChris Brandt 		val  |= CNEN;
1222195e3afSChris Brandt 	}
1232195e3afSChris Brandt 
124f1407d5cSKuninori Morimoto 	/*
125f1407d5cSKuninori Morimoto 	 * if enable
126f1407d5cSKuninori Morimoto 	 *
127f1407d5cSKuninori Morimoto 	 * - select Function mode
12804a5def3STakeshi Kihara 	 * - D+ Line Pull-up is disabled
12904a5def3STakeshi Kihara 	 *      When D+ Line Pull-up is enabled,
13004a5def3STakeshi Kihara 	 *      calling usbhs_sys_function_pullup(,1)
131f1407d5cSKuninori Morimoto 	 */
132f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
133f1407d5cSKuninori Morimoto }
134f1407d5cSKuninori Morimoto 
usbhs_sys_function_pullup(struct usbhs_priv * priv,int enable)1354cd2f599Skuninori.morimoto.gx@renesas.com void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable)
1364cd2f599Skuninori.morimoto.gx@renesas.com {
1374cd2f599Skuninori.morimoto.gx@renesas.com 	usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0);
1384cd2f599Skuninori.morimoto.gx@renesas.com }
1394cd2f599Skuninori.morimoto.gx@renesas.com 
usbhs_sys_set_test_mode(struct usbhs_priv * priv,u16 mode)140dfbb7f4fSKuninori Morimoto void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
141dfbb7f4fSKuninori Morimoto {
142dfbb7f4fSKuninori Morimoto 	usbhs_write(priv, TESTMODE, mode);
143dfbb7f4fSKuninori Morimoto }
144dfbb7f4fSKuninori Morimoto 
145f1407d5cSKuninori Morimoto /*
146f1407d5cSKuninori Morimoto  *		frame functions
147f1407d5cSKuninori Morimoto  */
usbhs_frame_get_num(struct usbhs_priv * priv)148f1407d5cSKuninori Morimoto int usbhs_frame_get_num(struct usbhs_priv *priv)
149f1407d5cSKuninori Morimoto {
150f1407d5cSKuninori Morimoto 	return usbhs_read(priv, FRMNUM) & FRNM_MASK;
151f1407d5cSKuninori Morimoto }
152f1407d5cSKuninori Morimoto 
153f1407d5cSKuninori Morimoto /*
154ef8bedb9SKuninori Morimoto  *		usb request functions
155ef8bedb9SKuninori Morimoto  */
usbhs_usbreq_get_val(struct usbhs_priv * priv,struct usb_ctrlrequest * req)156ef8bedb9SKuninori Morimoto void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
157ef8bedb9SKuninori Morimoto {
158ef8bedb9SKuninori Morimoto 	u16 val;
159ef8bedb9SKuninori Morimoto 
160ef8bedb9SKuninori Morimoto 	val = usbhs_read(priv, USBREQ);
161ef8bedb9SKuninori Morimoto 	req->bRequest		= (val >> 8) & 0xFF;
162ef8bedb9SKuninori Morimoto 	req->bRequestType	= (val >> 0) & 0xFF;
163ef8bedb9SKuninori Morimoto 
16446f62f88SBen Dooks (Codethink) 	req->wValue	= cpu_to_le16(usbhs_read(priv, USBVAL));
16546f62f88SBen Dooks (Codethink) 	req->wIndex	= cpu_to_le16(usbhs_read(priv, USBINDX));
16646f62f88SBen Dooks (Codethink) 	req->wLength	= cpu_to_le16(usbhs_read(priv, USBLENG));
167ef8bedb9SKuninori Morimoto }
168ef8bedb9SKuninori Morimoto 
usbhs_usbreq_set_val(struct usbhs_priv * priv,struct usb_ctrlrequest * req)169ef8bedb9SKuninori Morimoto void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
170ef8bedb9SKuninori Morimoto {
171ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBREQ,  (req->bRequest << 8) | req->bRequestType);
17246f62f88SBen Dooks (Codethink) 	usbhs_write(priv, USBVAL,  le16_to_cpu(req->wValue));
17346f62f88SBen Dooks (Codethink) 	usbhs_write(priv, USBINDX, le16_to_cpu(req->wIndex));
17446f62f88SBen Dooks (Codethink) 	usbhs_write(priv, USBLENG, le16_to_cpu(req->wLength));
175ef8bedb9SKuninori Morimoto 
176ef8bedb9SKuninori Morimoto 	usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
177ef8bedb9SKuninori Morimoto }
178ef8bedb9SKuninori Morimoto 
179ef8bedb9SKuninori Morimoto /*
180258485d9SKuninori Morimoto  *		bus/vbus functions
181258485d9SKuninori Morimoto  */
usbhs_bus_send_sof_enable(struct usbhs_priv * priv)182258485d9SKuninori Morimoto void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
183258485d9SKuninori Morimoto {
184a9be4a45SKuninori Morimoto 	u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
185a9be4a45SKuninori Morimoto 
186a9be4a45SKuninori Morimoto 	if (status != USBRST) {
187a9be4a45SKuninori Morimoto 		struct device *dev = usbhs_priv_to_dev(priv);
188a9be4a45SKuninori Morimoto 		dev_err(dev, "usbhs should be reset\n");
189a9be4a45SKuninori Morimoto 	}
190a9be4a45SKuninori Morimoto 
191258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
192258485d9SKuninori Morimoto }
193258485d9SKuninori Morimoto 
usbhs_bus_send_reset(struct usbhs_priv * priv)194258485d9SKuninori Morimoto void usbhs_bus_send_reset(struct usbhs_priv *priv)
195258485d9SKuninori Morimoto {
196258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
197258485d9SKuninori Morimoto }
198258485d9SKuninori Morimoto 
usbhs_bus_get_speed(struct usbhs_priv * priv)19975587f52SKuninori Morimoto int usbhs_bus_get_speed(struct usbhs_priv *priv)
20075587f52SKuninori Morimoto {
20175587f52SKuninori Morimoto 	u16 dvstctr = usbhs_read(priv, DVSTCTR);
20275587f52SKuninori Morimoto 
20375587f52SKuninori Morimoto 	switch (RHST & dvstctr) {
20475587f52SKuninori Morimoto 	case RHST_LOW_SPEED:
20575587f52SKuninori Morimoto 		return USB_SPEED_LOW;
20675587f52SKuninori Morimoto 	case RHST_FULL_SPEED:
20775587f52SKuninori Morimoto 		return USB_SPEED_FULL;
20875587f52SKuninori Morimoto 	case RHST_HIGH_SPEED:
20975587f52SKuninori Morimoto 		return USB_SPEED_HIGH;
21075587f52SKuninori Morimoto 	}
21175587f52SKuninori Morimoto 
21275587f52SKuninori Morimoto 	return USB_SPEED_UNKNOWN;
21375587f52SKuninori Morimoto }
21475587f52SKuninori Morimoto 
usbhs_vbus_ctrl(struct usbhs_priv * priv,int enable)215258485d9SKuninori Morimoto int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
216258485d9SKuninori Morimoto {
217258485d9SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
218258485d9SKuninori Morimoto 
219258485d9SKuninori Morimoto 	return usbhs_platform_call(priv, set_vbus, pdev, enable);
220258485d9SKuninori Morimoto }
221258485d9SKuninori Morimoto 
usbhsc_bus_init(struct usbhs_priv * priv)222258485d9SKuninori Morimoto static void usbhsc_bus_init(struct usbhs_priv *priv)
223258485d9SKuninori Morimoto {
224258485d9SKuninori Morimoto 	usbhs_write(priv, DVSTCTR, 0);
225258485d9SKuninori Morimoto 
226258485d9SKuninori Morimoto 	usbhs_vbus_ctrl(priv, 0);
227258485d9SKuninori Morimoto }
228258485d9SKuninori Morimoto 
229258485d9SKuninori Morimoto /*
230eb05191fSKuninori Morimoto  *		device configuration
231eb05191fSKuninori Morimoto  */
usbhs_set_device_config(struct usbhs_priv * priv,int devnum,u16 upphub,u16 hubport,u16 speed)2323dd49268SKuninori Morimoto int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
233eb05191fSKuninori Morimoto 			   u16 upphub, u16 hubport, u16 speed)
234eb05191fSKuninori Morimoto {
235eb05191fSKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
236eb05191fSKuninori Morimoto 	u16 usbspd = 0;
237eb05191fSKuninori Morimoto 	u32 reg = DEVADD0 + (2 * devnum);
238eb05191fSKuninori Morimoto 
239eb05191fSKuninori Morimoto 	if (devnum > 10) {
240eb05191fSKuninori Morimoto 		dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
241eb05191fSKuninori Morimoto 		return -EIO;
242eb05191fSKuninori Morimoto 	}
243eb05191fSKuninori Morimoto 
244eb05191fSKuninori Morimoto 	if (upphub > 0xA) {
245eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported hub number %d\n", upphub);
246eb05191fSKuninori Morimoto 		return -EIO;
247eb05191fSKuninori Morimoto 	}
248eb05191fSKuninori Morimoto 
249eb05191fSKuninori Morimoto 	switch (speed) {
250eb05191fSKuninori Morimoto 	case USB_SPEED_LOW:
251eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_LOW;
252eb05191fSKuninori Morimoto 		break;
253eb05191fSKuninori Morimoto 	case USB_SPEED_FULL:
254eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_FULL;
255eb05191fSKuninori Morimoto 		break;
256eb05191fSKuninori Morimoto 	case USB_SPEED_HIGH:
257eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_HIGH;
258eb05191fSKuninori Morimoto 		break;
259eb05191fSKuninori Morimoto 	default:
260eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported speed %d\n", speed);
261eb05191fSKuninori Morimoto 		return -EIO;
262eb05191fSKuninori Morimoto 	}
263eb05191fSKuninori Morimoto 
264eb05191fSKuninori Morimoto 	usbhs_write(priv, reg,	UPPHUB(upphub)	|
265eb05191fSKuninori Morimoto 				HUBPORT(hubport)|
266eb05191fSKuninori Morimoto 				USBSPD(usbspd));
267eb05191fSKuninori Morimoto 
268eb05191fSKuninori Morimoto 	return 0;
269eb05191fSKuninori Morimoto }
270eb05191fSKuninori Morimoto 
271eb05191fSKuninori Morimoto /*
272ab330cf3SYoshihiro Shimoda  *		interrupt functions
273ab330cf3SYoshihiro Shimoda  */
usbhs_xxxsts_clear(struct usbhs_priv * priv,u16 sts_reg,u16 bit)274ab330cf3SYoshihiro Shimoda void usbhs_xxxsts_clear(struct usbhs_priv *priv, u16 sts_reg, u16 bit)
275ab330cf3SYoshihiro Shimoda {
276ab330cf3SYoshihiro Shimoda 	u16 pipe_mask = (u16)GENMASK(usbhs_get_dparam(priv, pipe_size), 0);
277ab330cf3SYoshihiro Shimoda 
278ab330cf3SYoshihiro Shimoda 	usbhs_write(priv, sts_reg, ~(1 << bit) & pipe_mask);
279ab330cf3SYoshihiro Shimoda }
280ab330cf3SYoshihiro Shimoda 
281ab330cf3SYoshihiro Shimoda /*
282f1407d5cSKuninori Morimoto  *		local functions
283f1407d5cSKuninori Morimoto  */
usbhsc_set_buswait(struct usbhs_priv * priv)28411935de5SKuninori Morimoto static void usbhsc_set_buswait(struct usbhs_priv *priv)
285f1407d5cSKuninori Morimoto {
286f1407d5cSKuninori Morimoto 	int wait = usbhs_get_dparam(priv, buswait_bwait);
287f1407d5cSKuninori Morimoto 
288f1407d5cSKuninori Morimoto 	/* set bus wait if platform have */
289f1407d5cSKuninori Morimoto 	if (wait)
290f1407d5cSKuninori Morimoto 		usbhs_bset(priv, BUSWAIT, 0x000F, wait);
291f1407d5cSKuninori Morimoto }
292f1407d5cSKuninori Morimoto 
usbhsc_is_multi_clks(struct usbhs_priv * priv)2933df0e240SYoshihiro Shimoda static bool usbhsc_is_multi_clks(struct usbhs_priv *priv)
2943df0e240SYoshihiro Shimoda {
295df9f2c27SYoshihiro Shimoda 	return priv->dparam.multi_clks;
2963df0e240SYoshihiro Shimoda }
2973df0e240SYoshihiro Shimoda 
usbhsc_clk_get(struct device * dev,struct usbhs_priv * priv)2983df0e240SYoshihiro Shimoda static int usbhsc_clk_get(struct device *dev, struct usbhs_priv *priv)
2993df0e240SYoshihiro Shimoda {
3003df0e240SYoshihiro Shimoda 	if (!usbhsc_is_multi_clks(priv))
3013df0e240SYoshihiro Shimoda 		return 0;
3023df0e240SYoshihiro Shimoda 
3033df0e240SYoshihiro Shimoda 	/* The first clock should exist */
30431e795c6SYoshihiro Shimoda 	priv->clks[0] = of_clk_get(dev_of_node(dev), 0);
3053df0e240SYoshihiro Shimoda 	if (IS_ERR(priv->clks[0]))
3063df0e240SYoshihiro Shimoda 		return PTR_ERR(priv->clks[0]);
3073df0e240SYoshihiro Shimoda 
3083df0e240SYoshihiro Shimoda 	/*
3093df0e240SYoshihiro Shimoda 	 * To backward compatibility with old DT, this driver checks the return
3103df0e240SYoshihiro Shimoda 	 * value if it's -ENOENT or not.
3113df0e240SYoshihiro Shimoda 	 */
31231e795c6SYoshihiro Shimoda 	priv->clks[1] = of_clk_get(dev_of_node(dev), 1);
3133df0e240SYoshihiro Shimoda 	if (PTR_ERR(priv->clks[1]) == -ENOENT)
3143df0e240SYoshihiro Shimoda 		priv->clks[1] = NULL;
3153df0e240SYoshihiro Shimoda 	else if (IS_ERR(priv->clks[1]))
3163df0e240SYoshihiro Shimoda 		return PTR_ERR(priv->clks[1]);
3173df0e240SYoshihiro Shimoda 
3183df0e240SYoshihiro Shimoda 	return 0;
3193df0e240SYoshihiro Shimoda }
3203df0e240SYoshihiro Shimoda 
usbhsc_clk_put(struct usbhs_priv * priv)3213df0e240SYoshihiro Shimoda static void usbhsc_clk_put(struct usbhs_priv *priv)
3223df0e240SYoshihiro Shimoda {
3233df0e240SYoshihiro Shimoda 	int i;
3243df0e240SYoshihiro Shimoda 
3253df0e240SYoshihiro Shimoda 	if (!usbhsc_is_multi_clks(priv))
3263df0e240SYoshihiro Shimoda 		return;
3273df0e240SYoshihiro Shimoda 
3283df0e240SYoshihiro Shimoda 	for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
3293df0e240SYoshihiro Shimoda 		clk_put(priv->clks[i]);
3303df0e240SYoshihiro Shimoda }
3313df0e240SYoshihiro Shimoda 
usbhsc_clk_prepare_enable(struct usbhs_priv * priv)3323df0e240SYoshihiro Shimoda static int usbhsc_clk_prepare_enable(struct usbhs_priv *priv)
3333df0e240SYoshihiro Shimoda {
3343df0e240SYoshihiro Shimoda 	int i, ret;
3353df0e240SYoshihiro Shimoda 
3363df0e240SYoshihiro Shimoda 	if (!usbhsc_is_multi_clks(priv))
3373df0e240SYoshihiro Shimoda 		return 0;
3383df0e240SYoshihiro Shimoda 
3393df0e240SYoshihiro Shimoda 	for (i = 0; i < ARRAY_SIZE(priv->clks); i++) {
3403df0e240SYoshihiro Shimoda 		ret = clk_prepare_enable(priv->clks[i]);
3413df0e240SYoshihiro Shimoda 		if (ret) {
3423df0e240SYoshihiro Shimoda 			while (--i >= 0)
3433df0e240SYoshihiro Shimoda 				clk_disable_unprepare(priv->clks[i]);
3443df0e240SYoshihiro Shimoda 			return ret;
3453df0e240SYoshihiro Shimoda 		}
3463df0e240SYoshihiro Shimoda 	}
3473df0e240SYoshihiro Shimoda 
3483df0e240SYoshihiro Shimoda 	return ret;
3493df0e240SYoshihiro Shimoda }
3503df0e240SYoshihiro Shimoda 
usbhsc_clk_disable_unprepare(struct usbhs_priv * priv)3513df0e240SYoshihiro Shimoda static void usbhsc_clk_disable_unprepare(struct usbhs_priv *priv)
3523df0e240SYoshihiro Shimoda {
3533df0e240SYoshihiro Shimoda 	int i;
3543df0e240SYoshihiro Shimoda 
3553df0e240SYoshihiro Shimoda 	if (!usbhsc_is_multi_clks(priv))
3563df0e240SYoshihiro Shimoda 		return;
3573df0e240SYoshihiro Shimoda 
3583df0e240SYoshihiro Shimoda 	for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
3593df0e240SYoshihiro Shimoda 		clk_disable_unprepare(priv->clks[i]);
3603df0e240SYoshihiro Shimoda }
3613df0e240SYoshihiro Shimoda 
362f1407d5cSKuninori Morimoto /*
363f1407d5cSKuninori Morimoto  *		platform default param
364f1407d5cSKuninori Morimoto  */
3658ecef00fSUlrich Hecht 
3668ecef00fSUlrich Hecht /* commonly used on old SH-Mobile SoCs */
36751f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_default_pipe[] = {
36851f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
36951f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, false),
37051f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x18, false),
37151f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x28, true),
37251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x38, true),
37351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
37451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
37551f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
37651f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
37751f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x07, false),
378f1407d5cSKuninori Morimoto };
379f1407d5cSKuninori Morimoto 
3808ecef00fSUlrich Hecht /* commonly used on newer SH-Mobile and R-Car SoCs */
38151f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_new_pipe[] = {
38251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
38351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true),
38451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true),
38551f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
38651f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true),
38751f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true),
38851f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
38951f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
39051f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
39151f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x78, true),
39251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x88, true),
39351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x98, true),
39451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xa8, true),
39551f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xb8, true),
39651f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xc8, true),
39751f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xd8, true),
3988ecef00fSUlrich Hecht };
3998ecef00fSUlrich Hecht 
400f1407d5cSKuninori Morimoto /*
4016e267da8SKuninori Morimoto  *		power control
4026e267da8SKuninori Morimoto  */
usbhsc_power_ctrl(struct usbhs_priv * priv,int enable)4036e267da8SKuninori Morimoto static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
4046e267da8SKuninori Morimoto {
405f1ee56a0SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
4066e267da8SKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
4076e267da8SKuninori Morimoto 
4086e267da8SKuninori Morimoto 	if (enable) {
4096e267da8SKuninori Morimoto 		/* enable PM */
4106e267da8SKuninori Morimoto 		pm_runtime_get_sync(dev);
4116e267da8SKuninori Morimoto 
4123df0e240SYoshihiro Shimoda 		/* enable clks */
4133df0e240SYoshihiro Shimoda 		if (usbhsc_clk_prepare_enable(priv))
4143df0e240SYoshihiro Shimoda 			return;
4153df0e240SYoshihiro Shimoda 
416f1ee56a0SKuninori Morimoto 		/* enable platform power */
417f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
418f1ee56a0SKuninori Morimoto 
4196e267da8SKuninori Morimoto 		/* USB on */
4206e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
4216e267da8SKuninori Morimoto 	} else {
4226e267da8SKuninori Morimoto 		/* USB off */
4236e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
4246e267da8SKuninori Morimoto 
425f1ee56a0SKuninori Morimoto 		/* disable platform power */
426f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
427f1ee56a0SKuninori Morimoto 
4283df0e240SYoshihiro Shimoda 		/* disable clks */
4293df0e240SYoshihiro Shimoda 		usbhsc_clk_disable_unprepare(priv);
4303df0e240SYoshihiro Shimoda 
4316e267da8SKuninori Morimoto 		/* disable PM */
4326e267da8SKuninori Morimoto 		pm_runtime_put_sync(dev);
4336e267da8SKuninori Morimoto 	}
4346e267da8SKuninori Morimoto }
4356e267da8SKuninori Morimoto 
4366e267da8SKuninori Morimoto /*
437ca8a282aSKuninori Morimoto  *		hotplug
438f1407d5cSKuninori Morimoto  */
usbhsc_hotplug(struct usbhs_priv * priv)439ca8a282aSKuninori Morimoto static void usbhsc_hotplug(struct usbhs_priv *priv)
440f1407d5cSKuninori Morimoto {
441f1407d5cSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
442f1407d5cSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
443f1407d5cSKuninori Morimoto 	int id;
444f1407d5cSKuninori Morimoto 	int enable;
445d7b3968dSSergei Shtylyov 	int cable;
446f1407d5cSKuninori Morimoto 	int ret;
447f1407d5cSKuninori Morimoto 
448f1407d5cSKuninori Morimoto 	/*
449f1407d5cSKuninori Morimoto 	 * get vbus status from platform
450f1407d5cSKuninori Morimoto 	 */
451ccc3264cSYoshihiro Shimoda 	enable = usbhs_mod_info_call(priv, get_vbus, pdev);
452f1407d5cSKuninori Morimoto 
453f1407d5cSKuninori Morimoto 	/*
454f1407d5cSKuninori Morimoto 	 * get id from platform
455f1407d5cSKuninori Morimoto 	 */
456f1407d5cSKuninori Morimoto 	id = usbhs_platform_call(priv, get_id, pdev);
457f1407d5cSKuninori Morimoto 
458f1407d5cSKuninori Morimoto 	if (enable && !mod) {
459d7b3968dSSergei Shtylyov 		if (priv->edev) {
460ea07b8cfSChanwoo Choi 			cable = extcon_get_state(priv->edev, EXTCON_USB_HOST);
461d7b3968dSSergei Shtylyov 			if ((cable > 0 && id != USBHS_HOST) ||
462d7b3968dSSergei Shtylyov 			    (!cable && id != USBHS_GADGET)) {
463d7b3968dSSergei Shtylyov 				dev_info(&pdev->dev,
464d7b3968dSSergei Shtylyov 					 "USB cable plugged in doesn't match the selected role!\n");
465d7b3968dSSergei Shtylyov 				return;
466d7b3968dSSergei Shtylyov 			}
467d7b3968dSSergei Shtylyov 		}
468d7b3968dSSergei Shtylyov 
469f1407d5cSKuninori Morimoto 		ret = usbhs_mod_change(priv, id);
470f1407d5cSKuninori Morimoto 		if (ret < 0)
471f1407d5cSKuninori Morimoto 			return;
472f1407d5cSKuninori Morimoto 
473f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s enable\n", __func__);
474f1407d5cSKuninori Morimoto 
4756e267da8SKuninori Morimoto 		/* power on */
47697a79684SChris Brandt 		if (usbhs_get_dparam(priv, runtime_pwctrl))
4776e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
478f1407d5cSKuninori Morimoto 
479258485d9SKuninori Morimoto 		/* bus init */
480258485d9SKuninori Morimoto 		usbhsc_set_buswait(priv);
481258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
482258485d9SKuninori Morimoto 
483f1407d5cSKuninori Morimoto 		/* module start */
484f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, start, priv);
485f1407d5cSKuninori Morimoto 
486f1407d5cSKuninori Morimoto 	} else if (!enable && mod) {
487f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s disable\n", __func__);
488f1407d5cSKuninori Morimoto 
489f1407d5cSKuninori Morimoto 		/* module stop */
490f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
491f1407d5cSKuninori Morimoto 
492258485d9SKuninori Morimoto 		/* bus init */
493258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
494258485d9SKuninori Morimoto 
4956e267da8SKuninori Morimoto 		/* power off */
49697a79684SChris Brandt 		if (usbhs_get_dparam(priv, runtime_pwctrl))
4976e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
498f1407d5cSKuninori Morimoto 
499f1407d5cSKuninori Morimoto 		usbhs_mod_change(priv, -1);
500f1407d5cSKuninori Morimoto 
501f1407d5cSKuninori Morimoto 		/* reset phy for next connection */
502f1407d5cSKuninori Morimoto 		usbhs_platform_call(priv, phy_reset, pdev);
503f1407d5cSKuninori Morimoto 	}
504f1407d5cSKuninori Morimoto }
505f1407d5cSKuninori Morimoto 
506ca8a282aSKuninori Morimoto /*
507ca8a282aSKuninori Morimoto  *		notify hotplug
508ca8a282aSKuninori Morimoto  */
usbhsc_notify_hotplug(struct work_struct * work)509ca8a282aSKuninori Morimoto static void usbhsc_notify_hotplug(struct work_struct *work)
510ca8a282aSKuninori Morimoto {
511ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = container_of(work,
512ca8a282aSKuninori Morimoto 					       struct usbhs_priv,
513ca8a282aSKuninori Morimoto 					       notify_hotplug_work.work);
514ca8a282aSKuninori Morimoto 	usbhsc_hotplug(priv);
515ca8a282aSKuninori Morimoto }
516ca8a282aSKuninori Morimoto 
usbhsc_schedule_notify_hotplug(struct platform_device * pdev)5170966648dSYoshihiro Shimoda int usbhsc_schedule_notify_hotplug(struct platform_device *pdev)
518f1407d5cSKuninori Morimoto {
519206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
520bc57381eSKuninori Morimoto 	int delay = usbhs_get_dparam(priv, detection_delay);
521f1407d5cSKuninori Morimoto 
522f1407d5cSKuninori Morimoto 	/*
523f1407d5cSKuninori Morimoto 	 * This functions will be called in interrupt.
524f1407d5cSKuninori Morimoto 	 * To make sure safety context,
525f1407d5cSKuninori Morimoto 	 * use workqueue for usbhs_notify_hotplug
526f1407d5cSKuninori Morimoto 	 */
527a49a88f1SKuninori Morimoto 	schedule_delayed_work(&priv->notify_hotplug_work,
528a49a88f1SKuninori Morimoto 			      msecs_to_jiffies(delay));
529f1407d5cSKuninori Morimoto 	return 0;
530f1407d5cSKuninori Morimoto }
531f1407d5cSKuninori Morimoto 
532f1407d5cSKuninori Morimoto /*
533f1407d5cSKuninori Morimoto  *		platform functions
534f1407d5cSKuninori Morimoto  */
535b854100eSYoshihiro Shimoda static const struct of_device_id usbhs_of_match[] = {
536b854100eSYoshihiro Shimoda 	{
5371d6e81a2SFabrizio Castro 		.compatible = "renesas,usbhs-r8a774c0",
53876eff170SYoshihiro Shimoda 		.data = &usbhs_rcar_gen3_with_pll_plat_info,
5391d6e81a2SFabrizio Castro 	},
5401d6e81a2SFabrizio Castro 	{
541b854100eSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7790",
54276eff170SYoshihiro Shimoda 		.data = &usbhs_rcar_gen2_plat_info,
543b854100eSYoshihiro Shimoda 	},
544b854100eSYoshihiro Shimoda 	{
545b854100eSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7791",
54676eff170SYoshihiro Shimoda 		.data = &usbhs_rcar_gen2_plat_info,
547b854100eSYoshihiro Shimoda 	},
548af6e613bSYoshihiro Shimoda 	{
549af6e613bSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7794",
55076eff170SYoshihiro Shimoda 		.data = &usbhs_rcar_gen2_plat_info,
551af6e613bSYoshihiro Shimoda 	},
552f5f6afa8SYoshihiro Shimoda 	{
553f5f6afa8SYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7795",
55476eff170SYoshihiro Shimoda 		.data = &usbhs_rcar_gen3_plat_info,
555f5f6afa8SYoshihiro Shimoda 	},
5562f1a993aSSimon Horman 	{
557aae819e1SYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7796",
55876eff170SYoshihiro Shimoda 		.data = &usbhs_rcar_gen3_plat_info,
559aae819e1SYoshihiro Shimoda 	},
560aae819e1SYoshihiro Shimoda 	{
5614d2a863fSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a77990",
56276eff170SYoshihiro Shimoda 		.data = &usbhs_rcar_gen3_with_pll_plat_info,
5634d2a863fSYoshihiro Shimoda 	},
5644d2a863fSYoshihiro Shimoda 	{
5650f38672cSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a77995",
56676eff170SYoshihiro Shimoda 		.data = &usbhs_rcar_gen3_with_pll_plat_info,
5670f38672cSYoshihiro Shimoda 	},
5680f38672cSYoshihiro Shimoda 	{
5692f1a993aSSimon Horman 		.compatible = "renesas,rcar-gen2-usbhs",
57076eff170SYoshihiro Shimoda 		.data = &usbhs_rcar_gen2_plat_info,
5712f1a993aSSimon Horman 	},
5722f1a993aSSimon Horman 	{
5732f1a993aSSimon Horman 		.compatible = "renesas,rcar-gen3-usbhs",
57476eff170SYoshihiro Shimoda 		.data = &usbhs_rcar_gen3_plat_info,
5752f1a993aSSimon Horman 	},
576aec2927bSChris Brandt 	{
577aec2927bSChris Brandt 		.compatible = "renesas,rza1-usbhs",
57876eff170SYoshihiro Shimoda 		.data = &usbhs_rza1_plat_info,
579aec2927bSChris Brandt 	},
580b69dce63SChris Brandt 	{
581b69dce63SChris Brandt 		.compatible = "renesas,rza2-usbhs",
58276eff170SYoshihiro Shimoda 		.data = &usbhs_rza2_plat_info,
583b69dce63SChris Brandt 	},
584b854100eSYoshihiro Shimoda 	{ },
585b854100eSYoshihiro Shimoda };
586b854100eSYoshihiro Shimoda MODULE_DEVICE_TABLE(of, usbhs_of_match);
587b854100eSYoshihiro Shimoda 
usbhs_probe(struct platform_device * pdev)588b7a8d17dSKuninori Morimoto static int usbhs_probe(struct platform_device *pdev)
589f1407d5cSKuninori Morimoto {
59076eff170SYoshihiro Shimoda 	const struct renesas_usbhs_platform_info *info;
591f1407d5cSKuninori Morimoto 	struct usbhs_priv *priv;
592f08acaf0SYoshihiro Shimoda 	struct device *dev = &pdev->dev;
5933b31ec18SLinus Walleij 	struct gpio_desc *gpiod;
5943b31ec18SLinus Walleij 	int ret;
595b3103d0bSYoshihiro Shimoda 	u32 tmp;
59622ae6415SLad Prabhakar 	int irq;
597f1407d5cSKuninori Morimoto 
598b854100eSYoshihiro Shimoda 	/* check device node */
599f08acaf0SYoshihiro Shimoda 	if (dev_of_node(dev))
60076eff170SYoshihiro Shimoda 		info = of_device_get_match_data(dev);
60176eff170SYoshihiro Shimoda 	else
60276eff170SYoshihiro Shimoda 		info = renesas_usbhs_get_info(pdev);
603b854100eSYoshihiro Shimoda 
604f1407d5cSKuninori Morimoto 	/* check platform information */
6058ecef00fSUlrich Hecht 	if (!info) {
606f08acaf0SYoshihiro Shimoda 		dev_err(dev, "no platform information\n");
607f1407d5cSKuninori Morimoto 		return -EINVAL;
608f1407d5cSKuninori Morimoto 	}
609f1407d5cSKuninori Morimoto 
610f1407d5cSKuninori Morimoto 	/* platform data */
61122ae6415SLad Prabhakar 	irq = platform_get_irq(pdev, 0);
61222ae6415SLad Prabhakar 	if (irq < 0)
61322ae6415SLad Prabhakar 		return irq;
614f1407d5cSKuninori Morimoto 
615f1407d5cSKuninori Morimoto 	/* usb private data */
616f08acaf0SYoshihiro Shimoda 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
617b1cb07cdSPeter Chen 	if (!priv)
618f1407d5cSKuninori Morimoto 		return -ENOMEM;
619f1407d5cSKuninori Morimoto 
620d706a95bSYueHaibing 	priv->base = devm_platform_ioremap_resource(pdev, 0);
621148e1134SThierry Reding 	if (IS_ERR(priv->base))
622148e1134SThierry Reding 		return PTR_ERR(priv->base);
623f1407d5cSKuninori Morimoto 
624f08acaf0SYoshihiro Shimoda 	if (of_property_read_bool(dev_of_node(dev), "extcon")) {
625f08acaf0SYoshihiro Shimoda 		priv->edev = extcon_get_edev_by_phandle(dev, 0);
626d7b3968dSSergei Shtylyov 		if (IS_ERR(priv->edev))
627d7b3968dSSergei Shtylyov 			return PTR_ERR(priv->edev);
628d7b3968dSSergei Shtylyov 	}
629d7b3968dSSergei Shtylyov 
630f08acaf0SYoshihiro Shimoda 	priv->rsts = devm_reset_control_array_get_optional_shared(dev);
631f181dbb4SYoshihiro Shimoda 	if (IS_ERR(priv->rsts))
632f181dbb4SYoshihiro Shimoda 		return PTR_ERR(priv->rsts);
633f181dbb4SYoshihiro Shimoda 
634f1407d5cSKuninori Morimoto 	/*
635f1407d5cSKuninori Morimoto 	 * care platform info
636f1407d5cSKuninori Morimoto 	 */
6378ecef00fSUlrich Hecht 
638743344a9SYoshihiro Shimoda 	priv->dparam = info->driver_param;
639f1407d5cSKuninori Morimoto 
6408ecef00fSUlrich Hecht 	if (!info->platform_callback.get_id) {
641f08acaf0SYoshihiro Shimoda 		dev_err(dev, "no platform callbacks\n");
6428ecef00fSUlrich Hecht 		return -EINVAL;
6438ecef00fSUlrich Hecht 	}
644426d3ff2SYoshihiro Shimoda 	priv->pfunc = &info->platform_callback;
6458ecef00fSUlrich Hecht 
646f1407d5cSKuninori Morimoto 	/* set default param if platform doesn't have */
64798e86506SYoshihiro Shimoda 	if (usbhs_get_dparam(priv, has_new_pipe_configs)) {
64898e86506SYoshihiro Shimoda 		priv->dparam.pipe_configs = usbhsc_new_pipe;
64998e86506SYoshihiro Shimoda 		priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
65098e86506SYoshihiro Shimoda 	} else if (!priv->dparam.pipe_configs) {
65151f141a9SYoshihiro Shimoda 		priv->dparam.pipe_configs = usbhsc_default_pipe;
65251f141a9SYoshihiro Shimoda 		priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe);
653f1407d5cSKuninori Morimoto 	}
65448298206SKuninori Morimoto 	if (!priv->dparam.pio_dma_border)
65548298206SKuninori Morimoto 		priv->dparam.pio_dma_border = 64; /* 64byte */
656b3103d0bSYoshihiro Shimoda 	if (!of_property_read_u32(dev_of_node(dev), "renesas,buswait", &tmp))
657b3103d0bSYoshihiro Shimoda 		priv->dparam.buswait_bwait = tmp;
6583b31ec18SLinus Walleij 	gpiod = devm_gpiod_get_optional(dev, "renesas,enable", GPIOD_IN);
6593b31ec18SLinus Walleij 	if (IS_ERR(gpiod))
6603b31ec18SLinus Walleij 		return PTR_ERR(gpiod);
661f1407d5cSKuninori Morimoto 
662b002ff6eSKuninori Morimoto 	/* FIXME */
663b002ff6eSKuninori Morimoto 	/* runtime power control ? */
664426d3ff2SYoshihiro Shimoda 	if (priv->pfunc->get_vbus)
66597a79684SChris Brandt 		usbhs_get_dparam(priv, runtime_pwctrl) = 1;
666b002ff6eSKuninori Morimoto 
667f1407d5cSKuninori Morimoto 	/*
668f1407d5cSKuninori Morimoto 	 * priv settings
669f1407d5cSKuninori Morimoto 	 */
67022ae6415SLad Prabhakar 	priv->irq = irq;
671f1407d5cSKuninori Morimoto 	priv->pdev	= pdev;
672bc57381eSKuninori Morimoto 	INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
673f1407d5cSKuninori Morimoto 	spin_lock_init(usbhs_priv_to_lock(priv));
674f1407d5cSKuninori Morimoto 
675f1407d5cSKuninori Morimoto 	/* call pipe and module init */
676f1407d5cSKuninori Morimoto 	ret = usbhs_pipe_probe(priv);
677f1407d5cSKuninori Morimoto 	if (ret < 0)
67858efc77cSKuninori Morimoto 		return ret;
679f1407d5cSKuninori Morimoto 
680d3af90a5SKuninori Morimoto 	ret = usbhs_fifo_probe(priv);
681f1407d5cSKuninori Morimoto 	if (ret < 0)
68297f93227SKuninori Morimoto 		goto probe_end_pipe_exit;
683f1407d5cSKuninori Morimoto 
684d3af90a5SKuninori Morimoto 	ret = usbhs_mod_probe(priv);
685d3af90a5SKuninori Morimoto 	if (ret < 0)
686d3af90a5SKuninori Morimoto 		goto probe_end_fifo_exit;
687d3af90a5SKuninori Morimoto 
688f1407d5cSKuninori Morimoto 	/* dev_set_drvdata should be called after usbhs_mod_init */
689c9a0552eSLibo Chen 	platform_set_drvdata(pdev, priv);
690f1407d5cSKuninori Morimoto 
691f181dbb4SYoshihiro Shimoda 	ret = reset_control_deassert(priv->rsts);
692f181dbb4SYoshihiro Shimoda 	if (ret)
693f181dbb4SYoshihiro Shimoda 		goto probe_fail_rst;
694f181dbb4SYoshihiro Shimoda 
695f08acaf0SYoshihiro Shimoda 	ret = usbhsc_clk_get(dev, priv);
6963df0e240SYoshihiro Shimoda 	if (ret)
6973df0e240SYoshihiro Shimoda 		goto probe_fail_clks;
6983df0e240SYoshihiro Shimoda 
699f1407d5cSKuninori Morimoto 	/*
700f1407d5cSKuninori Morimoto 	 * deviece reset here because
701f1407d5cSKuninori Morimoto 	 * USB device might be used in boot loader.
702f1407d5cSKuninori Morimoto 	 */
703f1407d5cSKuninori Morimoto 	usbhs_sys_clock_ctrl(priv, 0);
704f1407d5cSKuninori Morimoto 
7058ecef00fSUlrich Hecht 	/* check GPIO determining if USB function should be enabled */
7063b31ec18SLinus Walleij 	if (gpiod) {
7073b31ec18SLinus Walleij 		ret = !gpiod_get_value(gpiod);
7088ecef00fSUlrich Hecht 		if (ret) {
7093b31ec18SLinus Walleij 			dev_warn(dev, "USB function not selected (GPIO)\n");
7108ecef00fSUlrich Hecht 			ret = -ENOTSUPP;
7118ecef00fSUlrich Hecht 			goto probe_end_mod_exit;
7128ecef00fSUlrich Hecht 		}
7138ecef00fSUlrich Hecht 	}
7148ecef00fSUlrich Hecht 
715f1407d5cSKuninori Morimoto 	/*
716f1407d5cSKuninori Morimoto 	 * platform call
717f1407d5cSKuninori Morimoto 	 *
718f1407d5cSKuninori Morimoto 	 * USB phy setup might depend on CPU/Board.
719f1407d5cSKuninori Morimoto 	 * If platform has its callback functions,
720f1407d5cSKuninori Morimoto 	 * call it here.
721f1407d5cSKuninori Morimoto 	 */
722f1407d5cSKuninori Morimoto 	ret = usbhs_platform_call(priv, hardware_init, pdev);
723f1407d5cSKuninori Morimoto 	if (ret < 0) {
724f08acaf0SYoshihiro Shimoda 		dev_err(dev, "platform init failed.\n");
72597f93227SKuninori Morimoto 		goto probe_end_mod_exit;
726f1407d5cSKuninori Morimoto 	}
727f1407d5cSKuninori Morimoto 
728f1407d5cSKuninori Morimoto 	/* reset phy for connection */
729f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
730f1407d5cSKuninori Morimoto 
731b002ff6eSKuninori Morimoto 	/* power control */
732f08acaf0SYoshihiro Shimoda 	pm_runtime_enable(dev);
73397a79684SChris Brandt 	if (!usbhs_get_dparam(priv, runtime_pwctrl)) {
734b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
735b002ff6eSKuninori Morimoto 		usbhs_mod_autonomy_mode(priv);
736ccc3264cSYoshihiro Shimoda 	} else {
737ccc3264cSYoshihiro Shimoda 		usbhs_mod_non_autonomy_mode(priv);
738b002ff6eSKuninori Morimoto 	}
739b002ff6eSKuninori Morimoto 
740f1407d5cSKuninori Morimoto 	/*
741f1407d5cSKuninori Morimoto 	 * manual call notify_hotplug for cold plug
742f1407d5cSKuninori Morimoto 	 */
7430966648dSYoshihiro Shimoda 	usbhsc_schedule_notify_hotplug(pdev);
744f1407d5cSKuninori Morimoto 
745f08acaf0SYoshihiro Shimoda 	dev_info(dev, "probed\n");
746f1407d5cSKuninori Morimoto 
747f1407d5cSKuninori Morimoto 	return ret;
748f1407d5cSKuninori Morimoto 
749f1407d5cSKuninori Morimoto probe_end_mod_exit:
7503df0e240SYoshihiro Shimoda 	usbhsc_clk_put(priv);
7513df0e240SYoshihiro Shimoda probe_fail_clks:
752f181dbb4SYoshihiro Shimoda 	reset_control_assert(priv->rsts);
753f181dbb4SYoshihiro Shimoda probe_fail_rst:
754f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
755d3af90a5SKuninori Morimoto probe_end_fifo_exit:
756d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
75797f93227SKuninori Morimoto probe_end_pipe_exit:
75897f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
759f1407d5cSKuninori Morimoto 
760f08acaf0SYoshihiro Shimoda 	dev_info(dev, "probe failed (%d)\n", ret);
761f1407d5cSKuninori Morimoto 
762f1407d5cSKuninori Morimoto 	return ret;
763f1407d5cSKuninori Morimoto }
764f1407d5cSKuninori Morimoto 
usbhs_remove(struct platform_device * pdev)765456a91ceSUwe Kleine-König static void usbhs_remove(struct platform_device *pdev)
766f1407d5cSKuninori Morimoto {
767206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
768f1407d5cSKuninori Morimoto 
769f1407d5cSKuninori Morimoto 	dev_dbg(&pdev->dev, "usb remove\n");
770f1407d5cSKuninori Morimoto 
771b002ff6eSKuninori Morimoto 	/* power off */
77297a79684SChris Brandt 	if (!usbhs_get_dparam(priv, runtime_pwctrl))
773b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
774f1407d5cSKuninori Morimoto 
775b002ff6eSKuninori Morimoto 	pm_runtime_disable(&pdev->dev);
776f1407d5cSKuninori Morimoto 
777f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, hardware_exit, pdev);
7783df0e240SYoshihiro Shimoda 	usbhsc_clk_put(priv);
779f181dbb4SYoshihiro Shimoda 	reset_control_assert(priv->rsts);
780f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
781d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
78297f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
783f1407d5cSKuninori Morimoto }
784f1407d5cSKuninori Morimoto 
usbhsc_suspend(struct device * dev)785eaf3074eSArnd Bergmann static __maybe_unused int usbhsc_suspend(struct device *dev)
786ca8a282aSKuninori Morimoto {
787ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
788ca8a282aSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
789ca8a282aSKuninori Morimoto 
790ca8a282aSKuninori Morimoto 	if (mod) {
791ca8a282aSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
792ca8a282aSKuninori Morimoto 		usbhs_mod_change(priv, -1);
793ca8a282aSKuninori Morimoto 	}
794ca8a282aSKuninori Morimoto 
79597a79684SChris Brandt 	if (mod || !usbhs_get_dparam(priv, runtime_pwctrl))
796ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
797ca8a282aSKuninori Morimoto 
798ca8a282aSKuninori Morimoto 	return 0;
799ca8a282aSKuninori Morimoto }
800ca8a282aSKuninori Morimoto 
usbhsc_resume(struct device * dev)801eaf3074eSArnd Bergmann static __maybe_unused int usbhsc_resume(struct device *dev)
802ca8a282aSKuninori Morimoto {
803ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
804ca8a282aSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
805ca8a282aSKuninori Morimoto 
80697a79684SChris Brandt 	if (!usbhs_get_dparam(priv, runtime_pwctrl)) {
807ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
80859a0879aSYoshihiro Shimoda 		usbhs_mod_autonomy_mode(priv);
80959a0879aSYoshihiro Shimoda 	}
810ca8a282aSKuninori Morimoto 
8115b50d3b5SKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
8125b50d3b5SKuninori Morimoto 
8130966648dSYoshihiro Shimoda 	usbhsc_schedule_notify_hotplug(pdev);
814ca8a282aSKuninori Morimoto 
815ca8a282aSKuninori Morimoto 	return 0;
816ca8a282aSKuninori Morimoto }
817ca8a282aSKuninori Morimoto 
818d54d334eSYoshihiro Shimoda static SIMPLE_DEV_PM_OPS(usbhsc_pm_ops, usbhsc_suspend, usbhsc_resume);
819ca8a282aSKuninori Morimoto 
820f1407d5cSKuninori Morimoto static struct platform_driver renesas_usbhs_driver = {
821f1407d5cSKuninori Morimoto 	.driver		= {
822f1407d5cSKuninori Morimoto 		.name	= "renesas_usbhs",
823ca8a282aSKuninori Morimoto 		.pm	= &usbhsc_pm_ops,
82494d1957fSKrzysztof Kozlowski 		.of_match_table = usbhs_of_match,
825f1407d5cSKuninori Morimoto 	},
826f1407d5cSKuninori Morimoto 	.probe		= usbhs_probe,
827456a91ceSUwe Kleine-König 	.remove_new	= usbhs_remove,
828f1407d5cSKuninori Morimoto };
829f1407d5cSKuninori Morimoto 
830cc27c96cSAxel Lin module_platform_driver(renesas_usbhs_driver);
831f1407d5cSKuninori Morimoto 
832f1407d5cSKuninori Morimoto MODULE_LICENSE("GPL");
833f1407d5cSKuninori Morimoto MODULE_DESCRIPTION("Renesas USB driver");
834f1407d5cSKuninori Morimoto MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
835