xref: /openbmc/linux/drivers/usb/renesas_usbhs/common.c (revision 46f62f887b2cb008b91d5bb39a14178fd5c56fbc)
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>
118ecef00fSUlrich Hecht #include <linux/gpio.h>
12f1407d5cSKuninori Morimoto #include <linux/io.h>
13f1407d5cSKuninori Morimoto #include <linux/module.h>
14b854100eSYoshihiro Shimoda #include <linux/of_device.h>
15b854100eSYoshihiro Shimoda #include <linux/of_gpio.h>
16f1407d5cSKuninori Morimoto #include <linux/pm_runtime.h>
17f181dbb4SYoshihiro Shimoda #include <linux/reset.h>
18f1407d5cSKuninori Morimoto #include <linux/slab.h>
19f1407d5cSKuninori Morimoto #include <linux/sysfs.h>
20cc502bb7SPaul Bolle #include "common.h"
218ecef00fSUlrich Hecht #include "rcar2.h"
22de18757eSYoshihiro Shimoda #include "rcar3.h"
23aec2927bSChris Brandt #include "rza.h"
24f1407d5cSKuninori Morimoto 
25233f519dSKuninori Morimoto /*
26233f519dSKuninori Morimoto  *		image of renesas_usbhs
27233f519dSKuninori Morimoto  *
28233f519dSKuninori Morimoto  * ex) gadget case
29233f519dSKuninori Morimoto 
30233f519dSKuninori Morimoto  * mod.c
31233f519dSKuninori Morimoto  * mod_gadget.c
32233f519dSKuninori Morimoto  * mod_host.c		pipe.c		fifo.c
33233f519dSKuninori Morimoto  *
34233f519dSKuninori Morimoto  *			+-------+	+-----------+
35233f519dSKuninori Morimoto  *			| pipe0 |------>| fifo pio  |
36233f519dSKuninori Morimoto  * +------------+	+-------+	+-----------+
37233f519dSKuninori Morimoto  * | mod_gadget |=====> | pipe1 |--+
38233f519dSKuninori Morimoto  * +------------+	+-------+  |	+-----------+
39233f519dSKuninori Morimoto  *			| pipe2 |  |  +-| fifo dma0 |
40233f519dSKuninori Morimoto  * +------------+	+-------+  |  |	+-----------+
41233f519dSKuninori Morimoto  * | mod_host   |	| pipe3 |<-|--+
42233f519dSKuninori Morimoto  * +------------+	+-------+  |	+-----------+
43233f519dSKuninori Morimoto  *			| ....  |  +--->| fifo dma1 |
44233f519dSKuninori Morimoto  *			| ....  |	+-----------+
45233f519dSKuninori Morimoto  */
46233f519dSKuninori Morimoto 
47f1407d5cSKuninori Morimoto /*
48f1407d5cSKuninori Morimoto  * platform call back
49f1407d5cSKuninori Morimoto  *
50f1407d5cSKuninori Morimoto  * renesas usb support platform callback function.
51f1407d5cSKuninori Morimoto  * Below macro call it.
52f1407d5cSKuninori Morimoto  * if platform doesn't have callback, it return 0 (no error)
53f1407d5cSKuninori Morimoto  */
54f1407d5cSKuninori Morimoto #define usbhs_platform_call(priv, func, args...)\
55f1407d5cSKuninori Morimoto 	(!(priv) ? -ENODEV :			\
56426d3ff2SYoshihiro Shimoda 	 !((priv)->pfunc->func) ? 0 :		\
57426d3ff2SYoshihiro Shimoda 	 (priv)->pfunc->func(args))
58f1407d5cSKuninori Morimoto 
59f1407d5cSKuninori Morimoto /*
60f1407d5cSKuninori Morimoto  *		common functions
61f1407d5cSKuninori Morimoto  */
62f1407d5cSKuninori Morimoto u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
63f1407d5cSKuninori Morimoto {
64f1407d5cSKuninori Morimoto 	return ioread16(priv->base + reg);
65f1407d5cSKuninori Morimoto }
66f1407d5cSKuninori Morimoto 
67f1407d5cSKuninori Morimoto void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
68f1407d5cSKuninori Morimoto {
69f1407d5cSKuninori Morimoto 	iowrite16(data, priv->base + reg);
70f1407d5cSKuninori Morimoto }
71f1407d5cSKuninori Morimoto 
72f1407d5cSKuninori Morimoto void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
73f1407d5cSKuninori Morimoto {
74f1407d5cSKuninori Morimoto 	u16 val = usbhs_read(priv, reg);
75f1407d5cSKuninori Morimoto 
76f1407d5cSKuninori Morimoto 	val &= ~mask;
77f1407d5cSKuninori Morimoto 	val |= data & mask;
78f1407d5cSKuninori Morimoto 
79f1407d5cSKuninori Morimoto 	usbhs_write(priv, reg, val);
80f1407d5cSKuninori Morimoto }
81f1407d5cSKuninori Morimoto 
82206dcc2cSKuninori Morimoto struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
83206dcc2cSKuninori Morimoto {
84206dcc2cSKuninori Morimoto 	return dev_get_drvdata(&pdev->dev);
85206dcc2cSKuninori Morimoto }
86206dcc2cSKuninori Morimoto 
87be0a42a7SYoshihiro Shimoda int usbhs_get_id_as_gadget(struct platform_device *pdev)
88be0a42a7SYoshihiro Shimoda {
89be0a42a7SYoshihiro Shimoda 	return USBHS_GADGET;
90be0a42a7SYoshihiro Shimoda }
91be0a42a7SYoshihiro Shimoda 
92f1407d5cSKuninori Morimoto /*
93f1407d5cSKuninori Morimoto  *		syscfg functions
94f1407d5cSKuninori Morimoto  */
9576190152SKuninori Morimoto static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
96f1407d5cSKuninori Morimoto {
97f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
98f1407d5cSKuninori Morimoto }
99f1407d5cSKuninori Morimoto 
100f1407d5cSKuninori Morimoto void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
101f1407d5cSKuninori Morimoto {
1023244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
1033244a7b4SKuninori Morimoto 	u16 val  = DCFM | DRPD | HSE | USBE;
104f1407d5cSKuninori Morimoto 
105f1407d5cSKuninori Morimoto 	/*
106f1407d5cSKuninori Morimoto 	 * if enable
107f1407d5cSKuninori Morimoto 	 *
108f1407d5cSKuninori Morimoto 	 * - select Host mode
109f1407d5cSKuninori Morimoto 	 * - D+ Line/D- Line Pull-down
110f1407d5cSKuninori Morimoto 	 */
111f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
112f1407d5cSKuninori Morimoto }
113f1407d5cSKuninori Morimoto 
114f1407d5cSKuninori Morimoto void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
115f1407d5cSKuninori Morimoto {
1163244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
11704a5def3STakeshi Kihara 	u16 val  = HSE | USBE;
118f1407d5cSKuninori Morimoto 
1192195e3afSChris Brandt 	/* CNEN bit is required for function operation */
1202195e3afSChris Brandt 	if (usbhs_get_dparam(priv, has_cnen)) {
1212195e3afSChris Brandt 		mask |= CNEN;
1222195e3afSChris Brandt 		val  |= CNEN;
1232195e3afSChris Brandt 	}
1242195e3afSChris Brandt 
125f1407d5cSKuninori Morimoto 	/*
126f1407d5cSKuninori Morimoto 	 * if enable
127f1407d5cSKuninori Morimoto 	 *
128f1407d5cSKuninori Morimoto 	 * - select Function mode
12904a5def3STakeshi Kihara 	 * - D+ Line Pull-up is disabled
13004a5def3STakeshi Kihara 	 *      When D+ Line Pull-up is enabled,
13104a5def3STakeshi Kihara 	 *      calling usbhs_sys_function_pullup(,1)
132f1407d5cSKuninori Morimoto 	 */
133f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
134f1407d5cSKuninori Morimoto }
135f1407d5cSKuninori Morimoto 
1364cd2f599Skuninori.morimoto.gx@renesas.com void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable)
1374cd2f599Skuninori.morimoto.gx@renesas.com {
1384cd2f599Skuninori.morimoto.gx@renesas.com 	usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0);
1394cd2f599Skuninori.morimoto.gx@renesas.com }
1404cd2f599Skuninori.morimoto.gx@renesas.com 
141dfbb7f4fSKuninori Morimoto void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
142dfbb7f4fSKuninori Morimoto {
143dfbb7f4fSKuninori Morimoto 	usbhs_write(priv, TESTMODE, mode);
144dfbb7f4fSKuninori Morimoto }
145dfbb7f4fSKuninori Morimoto 
146f1407d5cSKuninori Morimoto /*
147f1407d5cSKuninori Morimoto  *		frame functions
148f1407d5cSKuninori Morimoto  */
149f1407d5cSKuninori Morimoto int usbhs_frame_get_num(struct usbhs_priv *priv)
150f1407d5cSKuninori Morimoto {
151f1407d5cSKuninori Morimoto 	return usbhs_read(priv, FRMNUM) & FRNM_MASK;
152f1407d5cSKuninori Morimoto }
153f1407d5cSKuninori Morimoto 
154f1407d5cSKuninori Morimoto /*
155ef8bedb9SKuninori Morimoto  *		usb request functions
156ef8bedb9SKuninori Morimoto  */
157ef8bedb9SKuninori Morimoto void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
158ef8bedb9SKuninori Morimoto {
159ef8bedb9SKuninori Morimoto 	u16 val;
160ef8bedb9SKuninori Morimoto 
161ef8bedb9SKuninori Morimoto 	val = usbhs_read(priv, USBREQ);
162ef8bedb9SKuninori Morimoto 	req->bRequest		= (val >> 8) & 0xFF;
163ef8bedb9SKuninori Morimoto 	req->bRequestType	= (val >> 0) & 0xFF;
164ef8bedb9SKuninori Morimoto 
165*46f62f88SBen Dooks (Codethink) 	req->wValue	= cpu_to_le16(usbhs_read(priv, USBVAL));
166*46f62f88SBen Dooks (Codethink) 	req->wIndex	= cpu_to_le16(usbhs_read(priv, USBINDX));
167*46f62f88SBen Dooks (Codethink) 	req->wLength	= cpu_to_le16(usbhs_read(priv, USBLENG));
168ef8bedb9SKuninori Morimoto }
169ef8bedb9SKuninori Morimoto 
170ef8bedb9SKuninori Morimoto void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
171ef8bedb9SKuninori Morimoto {
172ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBREQ,  (req->bRequest << 8) | req->bRequestType);
173*46f62f88SBen Dooks (Codethink) 	usbhs_write(priv, USBVAL,  le16_to_cpu(req->wValue));
174*46f62f88SBen Dooks (Codethink) 	usbhs_write(priv, USBINDX, le16_to_cpu(req->wIndex));
175*46f62f88SBen Dooks (Codethink) 	usbhs_write(priv, USBLENG, le16_to_cpu(req->wLength));
176ef8bedb9SKuninori Morimoto 
177ef8bedb9SKuninori Morimoto 	usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
178ef8bedb9SKuninori Morimoto }
179ef8bedb9SKuninori Morimoto 
180ef8bedb9SKuninori Morimoto /*
181258485d9SKuninori Morimoto  *		bus/vbus functions
182258485d9SKuninori Morimoto  */
183258485d9SKuninori Morimoto void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
184258485d9SKuninori Morimoto {
185a9be4a45SKuninori Morimoto 	u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
186a9be4a45SKuninori Morimoto 
187a9be4a45SKuninori Morimoto 	if (status != USBRST) {
188a9be4a45SKuninori Morimoto 		struct device *dev = usbhs_priv_to_dev(priv);
189a9be4a45SKuninori Morimoto 		dev_err(dev, "usbhs should be reset\n");
190a9be4a45SKuninori Morimoto 	}
191a9be4a45SKuninori Morimoto 
192258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
193258485d9SKuninori Morimoto }
194258485d9SKuninori Morimoto 
195258485d9SKuninori Morimoto void usbhs_bus_send_reset(struct usbhs_priv *priv)
196258485d9SKuninori Morimoto {
197258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
198258485d9SKuninori Morimoto }
199258485d9SKuninori Morimoto 
20075587f52SKuninori Morimoto int usbhs_bus_get_speed(struct usbhs_priv *priv)
20175587f52SKuninori Morimoto {
20275587f52SKuninori Morimoto 	u16 dvstctr = usbhs_read(priv, DVSTCTR);
20375587f52SKuninori Morimoto 
20475587f52SKuninori Morimoto 	switch (RHST & dvstctr) {
20575587f52SKuninori Morimoto 	case RHST_LOW_SPEED:
20675587f52SKuninori Morimoto 		return USB_SPEED_LOW;
20775587f52SKuninori Morimoto 	case RHST_FULL_SPEED:
20875587f52SKuninori Morimoto 		return USB_SPEED_FULL;
20975587f52SKuninori Morimoto 	case RHST_HIGH_SPEED:
21075587f52SKuninori Morimoto 		return USB_SPEED_HIGH;
21175587f52SKuninori Morimoto 	}
21275587f52SKuninori Morimoto 
21375587f52SKuninori Morimoto 	return USB_SPEED_UNKNOWN;
21475587f52SKuninori Morimoto }
21575587f52SKuninori Morimoto 
216258485d9SKuninori Morimoto int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
217258485d9SKuninori Morimoto {
218258485d9SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
219258485d9SKuninori Morimoto 
220258485d9SKuninori Morimoto 	return usbhs_platform_call(priv, set_vbus, pdev, enable);
221258485d9SKuninori Morimoto }
222258485d9SKuninori Morimoto 
223258485d9SKuninori Morimoto static void usbhsc_bus_init(struct usbhs_priv *priv)
224258485d9SKuninori Morimoto {
225258485d9SKuninori Morimoto 	usbhs_write(priv, DVSTCTR, 0);
226258485d9SKuninori Morimoto 
227258485d9SKuninori Morimoto 	usbhs_vbus_ctrl(priv, 0);
228258485d9SKuninori Morimoto }
229258485d9SKuninori Morimoto 
230258485d9SKuninori Morimoto /*
231eb05191fSKuninori Morimoto  *		device configuration
232eb05191fSKuninori Morimoto  */
2333dd49268SKuninori Morimoto int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
234eb05191fSKuninori Morimoto 			   u16 upphub, u16 hubport, u16 speed)
235eb05191fSKuninori Morimoto {
236eb05191fSKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
237eb05191fSKuninori Morimoto 	u16 usbspd = 0;
238eb05191fSKuninori Morimoto 	u32 reg = DEVADD0 + (2 * devnum);
239eb05191fSKuninori Morimoto 
240eb05191fSKuninori Morimoto 	if (devnum > 10) {
241eb05191fSKuninori Morimoto 		dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
242eb05191fSKuninori Morimoto 		return -EIO;
243eb05191fSKuninori Morimoto 	}
244eb05191fSKuninori Morimoto 
245eb05191fSKuninori Morimoto 	if (upphub > 0xA) {
246eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported hub number %d\n", upphub);
247eb05191fSKuninori Morimoto 		return -EIO;
248eb05191fSKuninori Morimoto 	}
249eb05191fSKuninori Morimoto 
250eb05191fSKuninori Morimoto 	switch (speed) {
251eb05191fSKuninori Morimoto 	case USB_SPEED_LOW:
252eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_LOW;
253eb05191fSKuninori Morimoto 		break;
254eb05191fSKuninori Morimoto 	case USB_SPEED_FULL:
255eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_FULL;
256eb05191fSKuninori Morimoto 		break;
257eb05191fSKuninori Morimoto 	case USB_SPEED_HIGH:
258eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_HIGH;
259eb05191fSKuninori Morimoto 		break;
260eb05191fSKuninori Morimoto 	default:
261eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported speed %d\n", speed);
262eb05191fSKuninori Morimoto 		return -EIO;
263eb05191fSKuninori Morimoto 	}
264eb05191fSKuninori Morimoto 
265eb05191fSKuninori Morimoto 	usbhs_write(priv, reg,	UPPHUB(upphub)	|
266eb05191fSKuninori Morimoto 				HUBPORT(hubport)|
267eb05191fSKuninori Morimoto 				USBSPD(usbspd));
268eb05191fSKuninori Morimoto 
269eb05191fSKuninori Morimoto 	return 0;
270eb05191fSKuninori Morimoto }
271eb05191fSKuninori Morimoto 
272eb05191fSKuninori Morimoto /*
273ab330cf3SYoshihiro Shimoda  *		interrupt functions
274ab330cf3SYoshihiro Shimoda  */
275ab330cf3SYoshihiro Shimoda void usbhs_xxxsts_clear(struct usbhs_priv *priv, u16 sts_reg, u16 bit)
276ab330cf3SYoshihiro Shimoda {
277ab330cf3SYoshihiro Shimoda 	u16 pipe_mask = (u16)GENMASK(usbhs_get_dparam(priv, pipe_size), 0);
278ab330cf3SYoshihiro Shimoda 
279ab330cf3SYoshihiro Shimoda 	usbhs_write(priv, sts_reg, ~(1 << bit) & pipe_mask);
280ab330cf3SYoshihiro Shimoda }
281ab330cf3SYoshihiro Shimoda 
282ab330cf3SYoshihiro Shimoda /*
283f1407d5cSKuninori Morimoto  *		local functions
284f1407d5cSKuninori Morimoto  */
28511935de5SKuninori Morimoto static void usbhsc_set_buswait(struct usbhs_priv *priv)
286f1407d5cSKuninori Morimoto {
287f1407d5cSKuninori Morimoto 	int wait = usbhs_get_dparam(priv, buswait_bwait);
288f1407d5cSKuninori Morimoto 
289f1407d5cSKuninori Morimoto 	/* set bus wait if platform have */
290f1407d5cSKuninori Morimoto 	if (wait)
291f1407d5cSKuninori Morimoto 		usbhs_bset(priv, BUSWAIT, 0x000F, wait);
292f1407d5cSKuninori Morimoto }
293f1407d5cSKuninori Morimoto 
2943df0e240SYoshihiro Shimoda static bool usbhsc_is_multi_clks(struct usbhs_priv *priv)
2953df0e240SYoshihiro Shimoda {
296df9f2c27SYoshihiro Shimoda 	return priv->dparam.multi_clks;
2973df0e240SYoshihiro Shimoda }
2983df0e240SYoshihiro Shimoda 
2993df0e240SYoshihiro Shimoda static int usbhsc_clk_get(struct device *dev, struct usbhs_priv *priv)
3003df0e240SYoshihiro Shimoda {
3013df0e240SYoshihiro Shimoda 	if (!usbhsc_is_multi_clks(priv))
3023df0e240SYoshihiro Shimoda 		return 0;
3033df0e240SYoshihiro Shimoda 
3043df0e240SYoshihiro Shimoda 	/* The first clock should exist */
30531e795c6SYoshihiro Shimoda 	priv->clks[0] = of_clk_get(dev_of_node(dev), 0);
3063df0e240SYoshihiro Shimoda 	if (IS_ERR(priv->clks[0]))
3073df0e240SYoshihiro Shimoda 		return PTR_ERR(priv->clks[0]);
3083df0e240SYoshihiro Shimoda 
3093df0e240SYoshihiro Shimoda 	/*
3103df0e240SYoshihiro Shimoda 	 * To backward compatibility with old DT, this driver checks the return
3113df0e240SYoshihiro Shimoda 	 * value if it's -ENOENT or not.
3123df0e240SYoshihiro Shimoda 	 */
31331e795c6SYoshihiro Shimoda 	priv->clks[1] = of_clk_get(dev_of_node(dev), 1);
3143df0e240SYoshihiro Shimoda 	if (PTR_ERR(priv->clks[1]) == -ENOENT)
3153df0e240SYoshihiro Shimoda 		priv->clks[1] = NULL;
3163df0e240SYoshihiro Shimoda 	else if (IS_ERR(priv->clks[1]))
3173df0e240SYoshihiro Shimoda 		return PTR_ERR(priv->clks[1]);
3183df0e240SYoshihiro Shimoda 
3193df0e240SYoshihiro Shimoda 	return 0;
3203df0e240SYoshihiro Shimoda }
3213df0e240SYoshihiro Shimoda 
3223df0e240SYoshihiro Shimoda static void usbhsc_clk_put(struct usbhs_priv *priv)
3233df0e240SYoshihiro Shimoda {
3243df0e240SYoshihiro Shimoda 	int i;
3253df0e240SYoshihiro Shimoda 
3263df0e240SYoshihiro Shimoda 	if (!usbhsc_is_multi_clks(priv))
3273df0e240SYoshihiro Shimoda 		return;
3283df0e240SYoshihiro Shimoda 
3293df0e240SYoshihiro Shimoda 	for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
3303df0e240SYoshihiro Shimoda 		clk_put(priv->clks[i]);
3313df0e240SYoshihiro Shimoda }
3323df0e240SYoshihiro Shimoda 
3333df0e240SYoshihiro Shimoda static int usbhsc_clk_prepare_enable(struct usbhs_priv *priv)
3343df0e240SYoshihiro Shimoda {
3353df0e240SYoshihiro Shimoda 	int i, ret;
3363df0e240SYoshihiro Shimoda 
3373df0e240SYoshihiro Shimoda 	if (!usbhsc_is_multi_clks(priv))
3383df0e240SYoshihiro Shimoda 		return 0;
3393df0e240SYoshihiro Shimoda 
3403df0e240SYoshihiro Shimoda 	for (i = 0; i < ARRAY_SIZE(priv->clks); i++) {
3413df0e240SYoshihiro Shimoda 		ret = clk_prepare_enable(priv->clks[i]);
3423df0e240SYoshihiro Shimoda 		if (ret) {
3433df0e240SYoshihiro Shimoda 			while (--i >= 0)
3443df0e240SYoshihiro Shimoda 				clk_disable_unprepare(priv->clks[i]);
3453df0e240SYoshihiro Shimoda 			return ret;
3463df0e240SYoshihiro Shimoda 		}
3473df0e240SYoshihiro Shimoda 	}
3483df0e240SYoshihiro Shimoda 
3493df0e240SYoshihiro Shimoda 	return ret;
3503df0e240SYoshihiro Shimoda }
3513df0e240SYoshihiro Shimoda 
3523df0e240SYoshihiro Shimoda static void usbhsc_clk_disable_unprepare(struct usbhs_priv *priv)
3533df0e240SYoshihiro Shimoda {
3543df0e240SYoshihiro Shimoda 	int i;
3553df0e240SYoshihiro Shimoda 
3563df0e240SYoshihiro Shimoda 	if (!usbhsc_is_multi_clks(priv))
3573df0e240SYoshihiro Shimoda 		return;
3583df0e240SYoshihiro Shimoda 
3593df0e240SYoshihiro Shimoda 	for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
3603df0e240SYoshihiro Shimoda 		clk_disable_unprepare(priv->clks[i]);
3613df0e240SYoshihiro Shimoda }
3623df0e240SYoshihiro Shimoda 
363f1407d5cSKuninori Morimoto /*
364f1407d5cSKuninori Morimoto  *		platform default param
365f1407d5cSKuninori Morimoto  */
3668ecef00fSUlrich Hecht 
3678ecef00fSUlrich Hecht /* commonly used on old SH-Mobile SoCs */
36851f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_default_pipe[] = {
36951f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
37051f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, false),
37151f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x18, false),
37251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x28, true),
37351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x38, true),
37451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
37551f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
37651f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
37751f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
37851f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x07, false),
379f1407d5cSKuninori Morimoto };
380f1407d5cSKuninori Morimoto 
3818ecef00fSUlrich Hecht /* commonly used on newer SH-Mobile and R-Car SoCs */
38251f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_new_pipe[] = {
38351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
38451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true),
38551f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true),
38651f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
38751f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true),
38851f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true),
38951f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
39051f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
39151f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
39251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x78, true),
39351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x88, true),
39451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x98, true),
39551f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xa8, true),
39651f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xb8, true),
39751f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xc8, true),
39851f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xd8, true),
3998ecef00fSUlrich Hecht };
4008ecef00fSUlrich Hecht 
401f1407d5cSKuninori Morimoto /*
4026e267da8SKuninori Morimoto  *		power control
4036e267da8SKuninori Morimoto  */
4046e267da8SKuninori Morimoto static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
4056e267da8SKuninori Morimoto {
406f1ee56a0SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
4076e267da8SKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
4086e267da8SKuninori Morimoto 
4096e267da8SKuninori Morimoto 	if (enable) {
4106e267da8SKuninori Morimoto 		/* enable PM */
4116e267da8SKuninori Morimoto 		pm_runtime_get_sync(dev);
4126e267da8SKuninori Morimoto 
4133df0e240SYoshihiro Shimoda 		/* enable clks */
4143df0e240SYoshihiro Shimoda 		if (usbhsc_clk_prepare_enable(priv))
4153df0e240SYoshihiro Shimoda 			return;
4163df0e240SYoshihiro Shimoda 
417f1ee56a0SKuninori Morimoto 		/* enable platform power */
418f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
419f1ee56a0SKuninori Morimoto 
4206e267da8SKuninori Morimoto 		/* USB on */
4216e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
4226e267da8SKuninori Morimoto 	} else {
4236e267da8SKuninori Morimoto 		/* USB off */
4246e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
4256e267da8SKuninori Morimoto 
426f1ee56a0SKuninori Morimoto 		/* disable platform power */
427f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
428f1ee56a0SKuninori Morimoto 
4293df0e240SYoshihiro Shimoda 		/* disable clks */
4303df0e240SYoshihiro Shimoda 		usbhsc_clk_disable_unprepare(priv);
4313df0e240SYoshihiro Shimoda 
4326e267da8SKuninori Morimoto 		/* disable PM */
4336e267da8SKuninori Morimoto 		pm_runtime_put_sync(dev);
4346e267da8SKuninori Morimoto 	}
4356e267da8SKuninori Morimoto }
4366e267da8SKuninori Morimoto 
4376e267da8SKuninori Morimoto /*
438ca8a282aSKuninori Morimoto  *		hotplug
439f1407d5cSKuninori Morimoto  */
440ca8a282aSKuninori Morimoto static void usbhsc_hotplug(struct usbhs_priv *priv)
441f1407d5cSKuninori Morimoto {
442f1407d5cSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
443f1407d5cSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
444f1407d5cSKuninori Morimoto 	int id;
445f1407d5cSKuninori Morimoto 	int enable;
446d7b3968dSSergei Shtylyov 	int cable;
447f1407d5cSKuninori Morimoto 	int ret;
448f1407d5cSKuninori Morimoto 
449f1407d5cSKuninori Morimoto 	/*
450f1407d5cSKuninori Morimoto 	 * get vbus status from platform
451f1407d5cSKuninori Morimoto 	 */
452ccc3264cSYoshihiro Shimoda 	enable = usbhs_mod_info_call(priv, get_vbus, pdev);
453f1407d5cSKuninori Morimoto 
454f1407d5cSKuninori Morimoto 	/*
455f1407d5cSKuninori Morimoto 	 * get id from platform
456f1407d5cSKuninori Morimoto 	 */
457f1407d5cSKuninori Morimoto 	id = usbhs_platform_call(priv, get_id, pdev);
458f1407d5cSKuninori Morimoto 
459f1407d5cSKuninori Morimoto 	if (enable && !mod) {
460d7b3968dSSergei Shtylyov 		if (priv->edev) {
461ea07b8cfSChanwoo Choi 			cable = extcon_get_state(priv->edev, EXTCON_USB_HOST);
462d7b3968dSSergei Shtylyov 			if ((cable > 0 && id != USBHS_HOST) ||
463d7b3968dSSergei Shtylyov 			    (!cable && id != USBHS_GADGET)) {
464d7b3968dSSergei Shtylyov 				dev_info(&pdev->dev,
465d7b3968dSSergei Shtylyov 					 "USB cable plugged in doesn't match the selected role!\n");
466d7b3968dSSergei Shtylyov 				return;
467d7b3968dSSergei Shtylyov 			}
468d7b3968dSSergei Shtylyov 		}
469d7b3968dSSergei Shtylyov 
470f1407d5cSKuninori Morimoto 		ret = usbhs_mod_change(priv, id);
471f1407d5cSKuninori Morimoto 		if (ret < 0)
472f1407d5cSKuninori Morimoto 			return;
473f1407d5cSKuninori Morimoto 
474f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s enable\n", __func__);
475f1407d5cSKuninori Morimoto 
4766e267da8SKuninori Morimoto 		/* power on */
47797a79684SChris Brandt 		if (usbhs_get_dparam(priv, runtime_pwctrl))
4786e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
479f1407d5cSKuninori Morimoto 
480258485d9SKuninori Morimoto 		/* bus init */
481258485d9SKuninori Morimoto 		usbhsc_set_buswait(priv);
482258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
483258485d9SKuninori Morimoto 
484f1407d5cSKuninori Morimoto 		/* module start */
485f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, start, priv);
486f1407d5cSKuninori Morimoto 
487f1407d5cSKuninori Morimoto 	} else if (!enable && mod) {
488f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s disable\n", __func__);
489f1407d5cSKuninori Morimoto 
490f1407d5cSKuninori Morimoto 		/* module stop */
491f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
492f1407d5cSKuninori Morimoto 
493258485d9SKuninori Morimoto 		/* bus init */
494258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
495258485d9SKuninori Morimoto 
4966e267da8SKuninori Morimoto 		/* power off */
49797a79684SChris Brandt 		if (usbhs_get_dparam(priv, runtime_pwctrl))
4986e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
499f1407d5cSKuninori Morimoto 
500f1407d5cSKuninori Morimoto 		usbhs_mod_change(priv, -1);
501f1407d5cSKuninori Morimoto 
502f1407d5cSKuninori Morimoto 		/* reset phy for next connection */
503f1407d5cSKuninori Morimoto 		usbhs_platform_call(priv, phy_reset, pdev);
504f1407d5cSKuninori Morimoto 	}
505f1407d5cSKuninori Morimoto }
506f1407d5cSKuninori Morimoto 
507ca8a282aSKuninori Morimoto /*
508ca8a282aSKuninori Morimoto  *		notify hotplug
509ca8a282aSKuninori Morimoto  */
510ca8a282aSKuninori Morimoto static void usbhsc_notify_hotplug(struct work_struct *work)
511ca8a282aSKuninori Morimoto {
512ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = container_of(work,
513ca8a282aSKuninori Morimoto 					       struct usbhs_priv,
514ca8a282aSKuninori Morimoto 					       notify_hotplug_work.work);
515ca8a282aSKuninori Morimoto 	usbhsc_hotplug(priv);
516ca8a282aSKuninori Morimoto }
517ca8a282aSKuninori Morimoto 
5180966648dSYoshihiro Shimoda int usbhsc_schedule_notify_hotplug(struct platform_device *pdev)
519f1407d5cSKuninori Morimoto {
520206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
521bc57381eSKuninori Morimoto 	int delay = usbhs_get_dparam(priv, detection_delay);
522f1407d5cSKuninori Morimoto 
523f1407d5cSKuninori Morimoto 	/*
524f1407d5cSKuninori Morimoto 	 * This functions will be called in interrupt.
525f1407d5cSKuninori Morimoto 	 * To make sure safety context,
526f1407d5cSKuninori Morimoto 	 * use workqueue for usbhs_notify_hotplug
527f1407d5cSKuninori Morimoto 	 */
528a49a88f1SKuninori Morimoto 	schedule_delayed_work(&priv->notify_hotplug_work,
529a49a88f1SKuninori Morimoto 			      msecs_to_jiffies(delay));
530f1407d5cSKuninori Morimoto 	return 0;
531f1407d5cSKuninori Morimoto }
532f1407d5cSKuninori Morimoto 
533f1407d5cSKuninori Morimoto /*
534f1407d5cSKuninori Morimoto  *		platform functions
535f1407d5cSKuninori Morimoto  */
536b854100eSYoshihiro Shimoda static const struct of_device_id usbhs_of_match[] = {
537b854100eSYoshihiro Shimoda 	{
5381d6e81a2SFabrizio Castro 		.compatible = "renesas,usbhs-r8a774c0",
53976eff170SYoshihiro Shimoda 		.data = &usbhs_rcar_gen3_with_pll_plat_info,
5401d6e81a2SFabrizio Castro 	},
5411d6e81a2SFabrizio Castro 	{
542b854100eSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7790",
54376eff170SYoshihiro Shimoda 		.data = &usbhs_rcar_gen2_plat_info,
544b854100eSYoshihiro Shimoda 	},
545b854100eSYoshihiro Shimoda 	{
546b854100eSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7791",
54776eff170SYoshihiro Shimoda 		.data = &usbhs_rcar_gen2_plat_info,
548b854100eSYoshihiro Shimoda 	},
549af6e613bSYoshihiro Shimoda 	{
550af6e613bSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7794",
55176eff170SYoshihiro Shimoda 		.data = &usbhs_rcar_gen2_plat_info,
552af6e613bSYoshihiro Shimoda 	},
553f5f6afa8SYoshihiro Shimoda 	{
554f5f6afa8SYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7795",
55576eff170SYoshihiro Shimoda 		.data = &usbhs_rcar_gen3_plat_info,
556f5f6afa8SYoshihiro Shimoda 	},
5572f1a993aSSimon Horman 	{
558aae819e1SYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7796",
55976eff170SYoshihiro Shimoda 		.data = &usbhs_rcar_gen3_plat_info,
560aae819e1SYoshihiro Shimoda 	},
561aae819e1SYoshihiro Shimoda 	{
5624d2a863fSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a77990",
56376eff170SYoshihiro Shimoda 		.data = &usbhs_rcar_gen3_with_pll_plat_info,
5644d2a863fSYoshihiro Shimoda 	},
5654d2a863fSYoshihiro Shimoda 	{
5660f38672cSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a77995",
56776eff170SYoshihiro Shimoda 		.data = &usbhs_rcar_gen3_with_pll_plat_info,
5680f38672cSYoshihiro Shimoda 	},
5690f38672cSYoshihiro Shimoda 	{
5702f1a993aSSimon Horman 		.compatible = "renesas,rcar-gen2-usbhs",
57176eff170SYoshihiro Shimoda 		.data = &usbhs_rcar_gen2_plat_info,
5722f1a993aSSimon Horman 	},
5732f1a993aSSimon Horman 	{
5742f1a993aSSimon Horman 		.compatible = "renesas,rcar-gen3-usbhs",
57576eff170SYoshihiro Shimoda 		.data = &usbhs_rcar_gen3_plat_info,
5762f1a993aSSimon Horman 	},
577aec2927bSChris Brandt 	{
578aec2927bSChris Brandt 		.compatible = "renesas,rza1-usbhs",
57976eff170SYoshihiro Shimoda 		.data = &usbhs_rza1_plat_info,
580aec2927bSChris Brandt 	},
581b69dce63SChris Brandt 	{
582b69dce63SChris Brandt 		.compatible = "renesas,rza2-usbhs",
58376eff170SYoshihiro Shimoda 		.data = &usbhs_rza2_plat_info,
584b69dce63SChris Brandt 	},
585b854100eSYoshihiro Shimoda 	{ },
586b854100eSYoshihiro Shimoda };
587b854100eSYoshihiro Shimoda MODULE_DEVICE_TABLE(of, usbhs_of_match);
588b854100eSYoshihiro Shimoda 
589b7a8d17dSKuninori Morimoto static int usbhs_probe(struct platform_device *pdev)
590f1407d5cSKuninori Morimoto {
59176eff170SYoshihiro Shimoda 	const struct renesas_usbhs_platform_info *info;
592f1407d5cSKuninori Morimoto 	struct usbhs_priv *priv;
593d706a95bSYueHaibing 	struct resource *irq_res;
594f08acaf0SYoshihiro Shimoda 	struct device *dev = &pdev->dev;
595b3103d0bSYoshihiro Shimoda 	int ret, gpio;
596b3103d0bSYoshihiro Shimoda 	u32 tmp;
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 */
61153069af3SShimoda, Yoshihiro 	irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
612687f16b8SVarka Bhadram 	if (!irq_res) {
613f08acaf0SYoshihiro Shimoda 		dev_err(dev, "Not enough Renesas USB platform resources.\n");
614f1407d5cSKuninori Morimoto 		return -ENODEV;
615f1407d5cSKuninori Morimoto 	}
616f1407d5cSKuninori Morimoto 
617f1407d5cSKuninori Morimoto 	/* usb private data */
618f08acaf0SYoshihiro Shimoda 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
619b1cb07cdSPeter Chen 	if (!priv)
620f1407d5cSKuninori Morimoto 		return -ENOMEM;
621f1407d5cSKuninori Morimoto 
622d706a95bSYueHaibing 	priv->base = devm_platform_ioremap_resource(pdev, 0);
623148e1134SThierry Reding 	if (IS_ERR(priv->base))
624148e1134SThierry Reding 		return PTR_ERR(priv->base);
625f1407d5cSKuninori Morimoto 
626f08acaf0SYoshihiro Shimoda 	if (of_property_read_bool(dev_of_node(dev), "extcon")) {
627f08acaf0SYoshihiro Shimoda 		priv->edev = extcon_get_edev_by_phandle(dev, 0);
628d7b3968dSSergei Shtylyov 		if (IS_ERR(priv->edev))
629d7b3968dSSergei Shtylyov 			return PTR_ERR(priv->edev);
630d7b3968dSSergei Shtylyov 	}
631d7b3968dSSergei Shtylyov 
632f08acaf0SYoshihiro Shimoda 	priv->rsts = devm_reset_control_array_get_optional_shared(dev);
633f181dbb4SYoshihiro Shimoda 	if (IS_ERR(priv->rsts))
634f181dbb4SYoshihiro Shimoda 		return PTR_ERR(priv->rsts);
635f181dbb4SYoshihiro Shimoda 
636f1407d5cSKuninori Morimoto 	/*
637f1407d5cSKuninori Morimoto 	 * care platform info
638f1407d5cSKuninori Morimoto 	 */
6398ecef00fSUlrich Hecht 
640743344a9SYoshihiro Shimoda 	priv->dparam = info->driver_param;
641f1407d5cSKuninori Morimoto 
6428ecef00fSUlrich Hecht 	if (!info->platform_callback.get_id) {
643f08acaf0SYoshihiro Shimoda 		dev_err(dev, "no platform callbacks\n");
6448ecef00fSUlrich Hecht 		return -EINVAL;
6458ecef00fSUlrich Hecht 	}
646426d3ff2SYoshihiro Shimoda 	priv->pfunc = &info->platform_callback;
6478ecef00fSUlrich Hecht 
648f1407d5cSKuninori Morimoto 	/* set default param if platform doesn't have */
64998e86506SYoshihiro Shimoda 	if (usbhs_get_dparam(priv, has_new_pipe_configs)) {
65098e86506SYoshihiro Shimoda 		priv->dparam.pipe_configs = usbhsc_new_pipe;
65198e86506SYoshihiro Shimoda 		priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
65298e86506SYoshihiro Shimoda 	} else if (!priv->dparam.pipe_configs) {
65351f141a9SYoshihiro Shimoda 		priv->dparam.pipe_configs = usbhsc_default_pipe;
65451f141a9SYoshihiro Shimoda 		priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe);
655f1407d5cSKuninori Morimoto 	}
65648298206SKuninori Morimoto 	if (!priv->dparam.pio_dma_border)
65748298206SKuninori Morimoto 		priv->dparam.pio_dma_border = 64; /* 64byte */
658b3103d0bSYoshihiro Shimoda 	if (!of_property_read_u32(dev_of_node(dev), "renesas,buswait", &tmp))
659b3103d0bSYoshihiro Shimoda 		priv->dparam.buswait_bwait = tmp;
660b3103d0bSYoshihiro Shimoda 	gpio = of_get_named_gpio_flags(dev_of_node(dev), "renesas,enable-gpio",
661b3103d0bSYoshihiro Shimoda 				       0, NULL);
662b3103d0bSYoshihiro Shimoda 	if (gpio > 0)
663b3103d0bSYoshihiro Shimoda 		priv->dparam.enable_gpio = gpio;
664f1407d5cSKuninori Morimoto 
665b002ff6eSKuninori Morimoto 	/* FIXME */
666b002ff6eSKuninori Morimoto 	/* runtime power control ? */
667426d3ff2SYoshihiro Shimoda 	if (priv->pfunc->get_vbus)
66897a79684SChris Brandt 		usbhs_get_dparam(priv, runtime_pwctrl) = 1;
669b002ff6eSKuninori Morimoto 
670f1407d5cSKuninori Morimoto 	/*
671f1407d5cSKuninori Morimoto 	 * priv settings
672f1407d5cSKuninori Morimoto 	 */
67353069af3SShimoda, Yoshihiro 	priv->irq	= irq_res->start;
67453069af3SShimoda, Yoshihiro 	if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
67553069af3SShimoda, Yoshihiro 		priv->irqflags = IRQF_SHARED;
676f1407d5cSKuninori Morimoto 	priv->pdev	= pdev;
677bc57381eSKuninori Morimoto 	INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
678f1407d5cSKuninori Morimoto 	spin_lock_init(usbhs_priv_to_lock(priv));
679f1407d5cSKuninori Morimoto 
680f1407d5cSKuninori Morimoto 	/* call pipe and module init */
681f1407d5cSKuninori Morimoto 	ret = usbhs_pipe_probe(priv);
682f1407d5cSKuninori Morimoto 	if (ret < 0)
68358efc77cSKuninori Morimoto 		return ret;
684f1407d5cSKuninori Morimoto 
685d3af90a5SKuninori Morimoto 	ret = usbhs_fifo_probe(priv);
686f1407d5cSKuninori Morimoto 	if (ret < 0)
68797f93227SKuninori Morimoto 		goto probe_end_pipe_exit;
688f1407d5cSKuninori Morimoto 
689d3af90a5SKuninori Morimoto 	ret = usbhs_mod_probe(priv);
690d3af90a5SKuninori Morimoto 	if (ret < 0)
691d3af90a5SKuninori Morimoto 		goto probe_end_fifo_exit;
692d3af90a5SKuninori Morimoto 
693f1407d5cSKuninori Morimoto 	/* dev_set_drvdata should be called after usbhs_mod_init */
694c9a0552eSLibo Chen 	platform_set_drvdata(pdev, priv);
695f1407d5cSKuninori Morimoto 
696f181dbb4SYoshihiro Shimoda 	ret = reset_control_deassert(priv->rsts);
697f181dbb4SYoshihiro Shimoda 	if (ret)
698f181dbb4SYoshihiro Shimoda 		goto probe_fail_rst;
699f181dbb4SYoshihiro Shimoda 
700f08acaf0SYoshihiro Shimoda 	ret = usbhsc_clk_get(dev, priv);
7013df0e240SYoshihiro Shimoda 	if (ret)
7023df0e240SYoshihiro Shimoda 		goto probe_fail_clks;
7033df0e240SYoshihiro Shimoda 
704f1407d5cSKuninori Morimoto 	/*
705f1407d5cSKuninori Morimoto 	 * deviece reset here because
706f1407d5cSKuninori Morimoto 	 * USB device might be used in boot loader.
707f1407d5cSKuninori Morimoto 	 */
708f1407d5cSKuninori Morimoto 	usbhs_sys_clock_ctrl(priv, 0);
709f1407d5cSKuninori Morimoto 
7108ecef00fSUlrich Hecht 	/* check GPIO determining if USB function should be enabled */
7118ecef00fSUlrich Hecht 	if (priv->dparam.enable_gpio) {
7128ecef00fSUlrich Hecht 		gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL);
7138ecef00fSUlrich Hecht 		ret = !gpio_get_value(priv->dparam.enable_gpio);
7148ecef00fSUlrich Hecht 		gpio_free(priv->dparam.enable_gpio);
7158ecef00fSUlrich Hecht 		if (ret) {
716f08acaf0SYoshihiro Shimoda 			dev_warn(dev, "USB function not selected (GPIO %d)\n",
7178ecef00fSUlrich Hecht 				 priv->dparam.enable_gpio);
7188ecef00fSUlrich Hecht 			ret = -ENOTSUPP;
7198ecef00fSUlrich Hecht 			goto probe_end_mod_exit;
7208ecef00fSUlrich Hecht 		}
7218ecef00fSUlrich Hecht 	}
7228ecef00fSUlrich Hecht 
723f1407d5cSKuninori Morimoto 	/*
724f1407d5cSKuninori Morimoto 	 * platform call
725f1407d5cSKuninori Morimoto 	 *
726f1407d5cSKuninori Morimoto 	 * USB phy setup might depend on CPU/Board.
727f1407d5cSKuninori Morimoto 	 * If platform has its callback functions,
728f1407d5cSKuninori Morimoto 	 * call it here.
729f1407d5cSKuninori Morimoto 	 */
730f1407d5cSKuninori Morimoto 	ret = usbhs_platform_call(priv, hardware_init, pdev);
731f1407d5cSKuninori Morimoto 	if (ret < 0) {
732f08acaf0SYoshihiro Shimoda 		dev_err(dev, "platform init failed.\n");
73397f93227SKuninori Morimoto 		goto probe_end_mod_exit;
734f1407d5cSKuninori Morimoto 	}
735f1407d5cSKuninori Morimoto 
736f1407d5cSKuninori Morimoto 	/* reset phy for connection */
737f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
738f1407d5cSKuninori Morimoto 
739b002ff6eSKuninori Morimoto 	/* power control */
740f08acaf0SYoshihiro Shimoda 	pm_runtime_enable(dev);
74197a79684SChris Brandt 	if (!usbhs_get_dparam(priv, runtime_pwctrl)) {
742b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
743b002ff6eSKuninori Morimoto 		usbhs_mod_autonomy_mode(priv);
744ccc3264cSYoshihiro Shimoda 	} else {
745ccc3264cSYoshihiro Shimoda 		usbhs_mod_non_autonomy_mode(priv);
746b002ff6eSKuninori Morimoto 	}
747b002ff6eSKuninori Morimoto 
748f1407d5cSKuninori Morimoto 	/*
749f1407d5cSKuninori Morimoto 	 * manual call notify_hotplug for cold plug
750f1407d5cSKuninori Morimoto 	 */
7510966648dSYoshihiro Shimoda 	usbhsc_schedule_notify_hotplug(pdev);
752f1407d5cSKuninori Morimoto 
753f08acaf0SYoshihiro Shimoda 	dev_info(dev, "probed\n");
754f1407d5cSKuninori Morimoto 
755f1407d5cSKuninori Morimoto 	return ret;
756f1407d5cSKuninori Morimoto 
757f1407d5cSKuninori Morimoto probe_end_mod_exit:
7583df0e240SYoshihiro Shimoda 	usbhsc_clk_put(priv);
7593df0e240SYoshihiro Shimoda probe_fail_clks:
760f181dbb4SYoshihiro Shimoda 	reset_control_assert(priv->rsts);
761f181dbb4SYoshihiro Shimoda probe_fail_rst:
762f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
763d3af90a5SKuninori Morimoto probe_end_fifo_exit:
764d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
76597f93227SKuninori Morimoto probe_end_pipe_exit:
76697f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
767f1407d5cSKuninori Morimoto 
768f08acaf0SYoshihiro Shimoda 	dev_info(dev, "probe failed (%d)\n", ret);
769f1407d5cSKuninori Morimoto 
770f1407d5cSKuninori Morimoto 	return ret;
771f1407d5cSKuninori Morimoto }
772f1407d5cSKuninori Morimoto 
773fb4e98abSBill Pemberton static int usbhs_remove(struct platform_device *pdev)
774f1407d5cSKuninori Morimoto {
775206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
776f1407d5cSKuninori Morimoto 
777f1407d5cSKuninori Morimoto 	dev_dbg(&pdev->dev, "usb remove\n");
778f1407d5cSKuninori Morimoto 
779b002ff6eSKuninori Morimoto 	/* power off */
78097a79684SChris Brandt 	if (!usbhs_get_dparam(priv, runtime_pwctrl))
781b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
782f1407d5cSKuninori Morimoto 
783b002ff6eSKuninori Morimoto 	pm_runtime_disable(&pdev->dev);
784f1407d5cSKuninori Morimoto 
785f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, hardware_exit, pdev);
7863df0e240SYoshihiro Shimoda 	usbhsc_clk_put(priv);
787f181dbb4SYoshihiro Shimoda 	reset_control_assert(priv->rsts);
788f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
789d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
79097f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
791f1407d5cSKuninori Morimoto 
792f1407d5cSKuninori Morimoto 	return 0;
793f1407d5cSKuninori Morimoto }
794f1407d5cSKuninori Morimoto 
795eaf3074eSArnd Bergmann static __maybe_unused int usbhsc_suspend(struct device *dev)
796ca8a282aSKuninori Morimoto {
797ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
798ca8a282aSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
799ca8a282aSKuninori Morimoto 
800ca8a282aSKuninori Morimoto 	if (mod) {
801ca8a282aSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
802ca8a282aSKuninori Morimoto 		usbhs_mod_change(priv, -1);
803ca8a282aSKuninori Morimoto 	}
804ca8a282aSKuninori Morimoto 
80597a79684SChris Brandt 	if (mod || !usbhs_get_dparam(priv, runtime_pwctrl))
806ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
807ca8a282aSKuninori Morimoto 
808ca8a282aSKuninori Morimoto 	return 0;
809ca8a282aSKuninori Morimoto }
810ca8a282aSKuninori Morimoto 
811eaf3074eSArnd Bergmann static __maybe_unused int usbhsc_resume(struct device *dev)
812ca8a282aSKuninori Morimoto {
813ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
814ca8a282aSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
815ca8a282aSKuninori Morimoto 
81697a79684SChris Brandt 	if (!usbhs_get_dparam(priv, runtime_pwctrl)) {
817ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
81859a0879aSYoshihiro Shimoda 		usbhs_mod_autonomy_mode(priv);
81959a0879aSYoshihiro Shimoda 	}
820ca8a282aSKuninori Morimoto 
8215b50d3b5SKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
8225b50d3b5SKuninori Morimoto 
8230966648dSYoshihiro Shimoda 	usbhsc_schedule_notify_hotplug(pdev);
824ca8a282aSKuninori Morimoto 
825ca8a282aSKuninori Morimoto 	return 0;
826ca8a282aSKuninori Morimoto }
827ca8a282aSKuninori Morimoto 
828d54d334eSYoshihiro Shimoda static SIMPLE_DEV_PM_OPS(usbhsc_pm_ops, usbhsc_suspend, usbhsc_resume);
829ca8a282aSKuninori Morimoto 
830f1407d5cSKuninori Morimoto static struct platform_driver renesas_usbhs_driver = {
831f1407d5cSKuninori Morimoto 	.driver		= {
832f1407d5cSKuninori Morimoto 		.name	= "renesas_usbhs",
833ca8a282aSKuninori Morimoto 		.pm	= &usbhsc_pm_ops,
834b854100eSYoshihiro Shimoda 		.of_match_table = of_match_ptr(usbhs_of_match),
835f1407d5cSKuninori Morimoto 	},
836f1407d5cSKuninori Morimoto 	.probe		= usbhs_probe,
8377690417dSBill Pemberton 	.remove		= usbhs_remove,
838f1407d5cSKuninori Morimoto };
839f1407d5cSKuninori Morimoto 
840cc27c96cSAxel Lin module_platform_driver(renesas_usbhs_driver);
841f1407d5cSKuninori Morimoto 
842f1407d5cSKuninori Morimoto MODULE_LICENSE("GPL");
843f1407d5cSKuninori Morimoto MODULE_DESCRIPTION("Renesas USB driver");
844f1407d5cSKuninori Morimoto MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
845