xref: /openbmc/linux/drivers/usb/renesas_usbhs/common.c (revision f08acaf0096380235e2657089d8a5e09f6d4213f)
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 :			\
5648298206SKuninori Morimoto 	 !((priv)->pfunc.func) ? 0 :		\
5748298206SKuninori Morimoto 	 (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 
87f1407d5cSKuninori Morimoto /*
88f1407d5cSKuninori Morimoto  *		syscfg functions
89f1407d5cSKuninori Morimoto  */
9076190152SKuninori Morimoto static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
91f1407d5cSKuninori Morimoto {
92f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
93f1407d5cSKuninori Morimoto }
94f1407d5cSKuninori Morimoto 
95f1407d5cSKuninori Morimoto void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
96f1407d5cSKuninori Morimoto {
973244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
983244a7b4SKuninori Morimoto 	u16 val  = DCFM | DRPD | HSE | USBE;
99f1407d5cSKuninori Morimoto 
100f1407d5cSKuninori Morimoto 	/*
101f1407d5cSKuninori Morimoto 	 * if enable
102f1407d5cSKuninori Morimoto 	 *
103f1407d5cSKuninori Morimoto 	 * - select Host mode
104f1407d5cSKuninori Morimoto 	 * - D+ Line/D- Line Pull-down
105f1407d5cSKuninori Morimoto 	 */
106f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
107f1407d5cSKuninori Morimoto }
108f1407d5cSKuninori Morimoto 
109f1407d5cSKuninori Morimoto void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
110f1407d5cSKuninori Morimoto {
1113244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
11204a5def3STakeshi Kihara 	u16 val  = HSE | USBE;
113f1407d5cSKuninori Morimoto 
1142195e3afSChris Brandt 	/* CNEN bit is required for function operation */
1152195e3afSChris Brandt 	if (usbhs_get_dparam(priv, has_cnen)) {
1162195e3afSChris Brandt 		mask |= CNEN;
1172195e3afSChris Brandt 		val  |= CNEN;
1182195e3afSChris Brandt 	}
1192195e3afSChris Brandt 
120f1407d5cSKuninori Morimoto 	/*
121f1407d5cSKuninori Morimoto 	 * if enable
122f1407d5cSKuninori Morimoto 	 *
123f1407d5cSKuninori Morimoto 	 * - select Function mode
12404a5def3STakeshi Kihara 	 * - D+ Line Pull-up is disabled
12504a5def3STakeshi Kihara 	 *      When D+ Line Pull-up is enabled,
12604a5def3STakeshi Kihara 	 *      calling usbhs_sys_function_pullup(,1)
127f1407d5cSKuninori Morimoto 	 */
128f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
129f1407d5cSKuninori Morimoto }
130f1407d5cSKuninori Morimoto 
1314cd2f599Skuninori.morimoto.gx@renesas.com void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable)
1324cd2f599Skuninori.morimoto.gx@renesas.com {
1334cd2f599Skuninori.morimoto.gx@renesas.com 	usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0);
1344cd2f599Skuninori.morimoto.gx@renesas.com }
1354cd2f599Skuninori.morimoto.gx@renesas.com 
136dfbb7f4fSKuninori Morimoto void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
137dfbb7f4fSKuninori Morimoto {
138dfbb7f4fSKuninori Morimoto 	usbhs_write(priv, TESTMODE, mode);
139dfbb7f4fSKuninori Morimoto }
140dfbb7f4fSKuninori Morimoto 
141f1407d5cSKuninori Morimoto /*
142f1407d5cSKuninori Morimoto  *		frame functions
143f1407d5cSKuninori Morimoto  */
144f1407d5cSKuninori Morimoto int usbhs_frame_get_num(struct usbhs_priv *priv)
145f1407d5cSKuninori Morimoto {
146f1407d5cSKuninori Morimoto 	return usbhs_read(priv, FRMNUM) & FRNM_MASK;
147f1407d5cSKuninori Morimoto }
148f1407d5cSKuninori Morimoto 
149f1407d5cSKuninori Morimoto /*
150ef8bedb9SKuninori Morimoto  *		usb request functions
151ef8bedb9SKuninori Morimoto  */
152ef8bedb9SKuninori Morimoto void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
153ef8bedb9SKuninori Morimoto {
154ef8bedb9SKuninori Morimoto 	u16 val;
155ef8bedb9SKuninori Morimoto 
156ef8bedb9SKuninori Morimoto 	val = usbhs_read(priv, USBREQ);
157ef8bedb9SKuninori Morimoto 	req->bRequest		= (val >> 8) & 0xFF;
158ef8bedb9SKuninori Morimoto 	req->bRequestType	= (val >> 0) & 0xFF;
159ef8bedb9SKuninori Morimoto 
160ef8bedb9SKuninori Morimoto 	req->wValue	= usbhs_read(priv, USBVAL);
161ef8bedb9SKuninori Morimoto 	req->wIndex	= usbhs_read(priv, USBINDX);
162ef8bedb9SKuninori Morimoto 	req->wLength	= usbhs_read(priv, USBLENG);
163ef8bedb9SKuninori Morimoto }
164ef8bedb9SKuninori Morimoto 
165ef8bedb9SKuninori Morimoto void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
166ef8bedb9SKuninori Morimoto {
167ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBREQ,  (req->bRequest << 8) | req->bRequestType);
168ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBVAL,  req->wValue);
169ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBINDX, req->wIndex);
170ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBLENG, req->wLength);
171ef8bedb9SKuninori Morimoto 
172ef8bedb9SKuninori Morimoto 	usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
173ef8bedb9SKuninori Morimoto }
174ef8bedb9SKuninori Morimoto 
175ef8bedb9SKuninori Morimoto /*
176258485d9SKuninori Morimoto  *		bus/vbus functions
177258485d9SKuninori Morimoto  */
178258485d9SKuninori Morimoto void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
179258485d9SKuninori Morimoto {
180a9be4a45SKuninori Morimoto 	u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
181a9be4a45SKuninori Morimoto 
182a9be4a45SKuninori Morimoto 	if (status != USBRST) {
183a9be4a45SKuninori Morimoto 		struct device *dev = usbhs_priv_to_dev(priv);
184a9be4a45SKuninori Morimoto 		dev_err(dev, "usbhs should be reset\n");
185a9be4a45SKuninori Morimoto 	}
186a9be4a45SKuninori Morimoto 
187258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
188258485d9SKuninori Morimoto }
189258485d9SKuninori Morimoto 
190258485d9SKuninori Morimoto void usbhs_bus_send_reset(struct usbhs_priv *priv)
191258485d9SKuninori Morimoto {
192258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
193258485d9SKuninori Morimoto }
194258485d9SKuninori Morimoto 
19575587f52SKuninori Morimoto int usbhs_bus_get_speed(struct usbhs_priv *priv)
19675587f52SKuninori Morimoto {
19775587f52SKuninori Morimoto 	u16 dvstctr = usbhs_read(priv, DVSTCTR);
19875587f52SKuninori Morimoto 
19975587f52SKuninori Morimoto 	switch (RHST & dvstctr) {
20075587f52SKuninori Morimoto 	case RHST_LOW_SPEED:
20175587f52SKuninori Morimoto 		return USB_SPEED_LOW;
20275587f52SKuninori Morimoto 	case RHST_FULL_SPEED:
20375587f52SKuninori Morimoto 		return USB_SPEED_FULL;
20475587f52SKuninori Morimoto 	case RHST_HIGH_SPEED:
20575587f52SKuninori Morimoto 		return USB_SPEED_HIGH;
20675587f52SKuninori Morimoto 	}
20775587f52SKuninori Morimoto 
20875587f52SKuninori Morimoto 	return USB_SPEED_UNKNOWN;
20975587f52SKuninori Morimoto }
21075587f52SKuninori Morimoto 
211258485d9SKuninori Morimoto int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
212258485d9SKuninori Morimoto {
213258485d9SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
214258485d9SKuninori Morimoto 
215258485d9SKuninori Morimoto 	return usbhs_platform_call(priv, set_vbus, pdev, enable);
216258485d9SKuninori Morimoto }
217258485d9SKuninori Morimoto 
218258485d9SKuninori Morimoto static void usbhsc_bus_init(struct usbhs_priv *priv)
219258485d9SKuninori Morimoto {
220258485d9SKuninori Morimoto 	usbhs_write(priv, DVSTCTR, 0);
221258485d9SKuninori Morimoto 
222258485d9SKuninori Morimoto 	usbhs_vbus_ctrl(priv, 0);
223258485d9SKuninori Morimoto }
224258485d9SKuninori Morimoto 
225258485d9SKuninori Morimoto /*
226eb05191fSKuninori Morimoto  *		device configuration
227eb05191fSKuninori Morimoto  */
2283dd49268SKuninori Morimoto int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
229eb05191fSKuninori Morimoto 			   u16 upphub, u16 hubport, u16 speed)
230eb05191fSKuninori Morimoto {
231eb05191fSKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
232eb05191fSKuninori Morimoto 	u16 usbspd = 0;
233eb05191fSKuninori Morimoto 	u32 reg = DEVADD0 + (2 * devnum);
234eb05191fSKuninori Morimoto 
235eb05191fSKuninori Morimoto 	if (devnum > 10) {
236eb05191fSKuninori Morimoto 		dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
237eb05191fSKuninori Morimoto 		return -EIO;
238eb05191fSKuninori Morimoto 	}
239eb05191fSKuninori Morimoto 
240eb05191fSKuninori Morimoto 	if (upphub > 0xA) {
241eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported hub number %d\n", upphub);
242eb05191fSKuninori Morimoto 		return -EIO;
243eb05191fSKuninori Morimoto 	}
244eb05191fSKuninori Morimoto 
245eb05191fSKuninori Morimoto 	switch (speed) {
246eb05191fSKuninori Morimoto 	case USB_SPEED_LOW:
247eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_LOW;
248eb05191fSKuninori Morimoto 		break;
249eb05191fSKuninori Morimoto 	case USB_SPEED_FULL:
250eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_FULL;
251eb05191fSKuninori Morimoto 		break;
252eb05191fSKuninori Morimoto 	case USB_SPEED_HIGH:
253eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_HIGH;
254eb05191fSKuninori Morimoto 		break;
255eb05191fSKuninori Morimoto 	default:
256eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported speed %d\n", speed);
257eb05191fSKuninori Morimoto 		return -EIO;
258eb05191fSKuninori Morimoto 	}
259eb05191fSKuninori Morimoto 
260eb05191fSKuninori Morimoto 	usbhs_write(priv, reg,	UPPHUB(upphub)	|
261eb05191fSKuninori Morimoto 				HUBPORT(hubport)|
262eb05191fSKuninori Morimoto 				USBSPD(usbspd));
263eb05191fSKuninori Morimoto 
264eb05191fSKuninori Morimoto 	return 0;
265eb05191fSKuninori Morimoto }
266eb05191fSKuninori Morimoto 
267eb05191fSKuninori Morimoto /*
268ab330cf3SYoshihiro Shimoda  *		interrupt functions
269ab330cf3SYoshihiro Shimoda  */
270ab330cf3SYoshihiro Shimoda void usbhs_xxxsts_clear(struct usbhs_priv *priv, u16 sts_reg, u16 bit)
271ab330cf3SYoshihiro Shimoda {
272ab330cf3SYoshihiro Shimoda 	u16 pipe_mask = (u16)GENMASK(usbhs_get_dparam(priv, pipe_size), 0);
273ab330cf3SYoshihiro Shimoda 
274ab330cf3SYoshihiro Shimoda 	usbhs_write(priv, sts_reg, ~(1 << bit) & pipe_mask);
275ab330cf3SYoshihiro Shimoda }
276ab330cf3SYoshihiro Shimoda 
277ab330cf3SYoshihiro Shimoda /*
278f1407d5cSKuninori Morimoto  *		local functions
279f1407d5cSKuninori Morimoto  */
28011935de5SKuninori Morimoto static void usbhsc_set_buswait(struct usbhs_priv *priv)
281f1407d5cSKuninori Morimoto {
282f1407d5cSKuninori Morimoto 	int wait = usbhs_get_dparam(priv, buswait_bwait);
283f1407d5cSKuninori Morimoto 
284f1407d5cSKuninori Morimoto 	/* set bus wait if platform have */
285f1407d5cSKuninori Morimoto 	if (wait)
286f1407d5cSKuninori Morimoto 		usbhs_bset(priv, BUSWAIT, 0x000F, wait);
287f1407d5cSKuninori Morimoto }
288f1407d5cSKuninori Morimoto 
2893df0e240SYoshihiro Shimoda static bool usbhsc_is_multi_clks(struct usbhs_priv *priv)
2903df0e240SYoshihiro Shimoda {
291df9f2c27SYoshihiro Shimoda 	return priv->dparam.multi_clks;
2923df0e240SYoshihiro Shimoda }
2933df0e240SYoshihiro Shimoda 
2943df0e240SYoshihiro Shimoda static int usbhsc_clk_get(struct device *dev, struct usbhs_priv *priv)
2953df0e240SYoshihiro Shimoda {
2963df0e240SYoshihiro Shimoda 	if (!usbhsc_is_multi_clks(priv))
2973df0e240SYoshihiro Shimoda 		return 0;
2983df0e240SYoshihiro Shimoda 
2993df0e240SYoshihiro Shimoda 	/* The first clock should exist */
30031e795c6SYoshihiro Shimoda 	priv->clks[0] = of_clk_get(dev_of_node(dev), 0);
3013df0e240SYoshihiro Shimoda 	if (IS_ERR(priv->clks[0]))
3023df0e240SYoshihiro Shimoda 		return PTR_ERR(priv->clks[0]);
3033df0e240SYoshihiro Shimoda 
3043df0e240SYoshihiro Shimoda 	/*
3053df0e240SYoshihiro Shimoda 	 * To backward compatibility with old DT, this driver checks the return
3063df0e240SYoshihiro Shimoda 	 * value if it's -ENOENT or not.
3073df0e240SYoshihiro Shimoda 	 */
30831e795c6SYoshihiro Shimoda 	priv->clks[1] = of_clk_get(dev_of_node(dev), 1);
3093df0e240SYoshihiro Shimoda 	if (PTR_ERR(priv->clks[1]) == -ENOENT)
3103df0e240SYoshihiro Shimoda 		priv->clks[1] = NULL;
3113df0e240SYoshihiro Shimoda 	else if (IS_ERR(priv->clks[1]))
3123df0e240SYoshihiro Shimoda 		return PTR_ERR(priv->clks[1]);
3133df0e240SYoshihiro Shimoda 
3143df0e240SYoshihiro Shimoda 	return 0;
3153df0e240SYoshihiro Shimoda }
3163df0e240SYoshihiro Shimoda 
3173df0e240SYoshihiro Shimoda static void usbhsc_clk_put(struct usbhs_priv *priv)
3183df0e240SYoshihiro Shimoda {
3193df0e240SYoshihiro Shimoda 	int i;
3203df0e240SYoshihiro Shimoda 
3213df0e240SYoshihiro Shimoda 	if (!usbhsc_is_multi_clks(priv))
3223df0e240SYoshihiro Shimoda 		return;
3233df0e240SYoshihiro Shimoda 
3243df0e240SYoshihiro Shimoda 	for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
3253df0e240SYoshihiro Shimoda 		clk_put(priv->clks[i]);
3263df0e240SYoshihiro Shimoda }
3273df0e240SYoshihiro Shimoda 
3283df0e240SYoshihiro Shimoda static int usbhsc_clk_prepare_enable(struct usbhs_priv *priv)
3293df0e240SYoshihiro Shimoda {
3303df0e240SYoshihiro Shimoda 	int i, ret;
3313df0e240SYoshihiro Shimoda 
3323df0e240SYoshihiro Shimoda 	if (!usbhsc_is_multi_clks(priv))
3333df0e240SYoshihiro Shimoda 		return 0;
3343df0e240SYoshihiro Shimoda 
3353df0e240SYoshihiro Shimoda 	for (i = 0; i < ARRAY_SIZE(priv->clks); i++) {
3363df0e240SYoshihiro Shimoda 		ret = clk_prepare_enable(priv->clks[i]);
3373df0e240SYoshihiro Shimoda 		if (ret) {
3383df0e240SYoshihiro Shimoda 			while (--i >= 0)
3393df0e240SYoshihiro Shimoda 				clk_disable_unprepare(priv->clks[i]);
3403df0e240SYoshihiro Shimoda 			return ret;
3413df0e240SYoshihiro Shimoda 		}
3423df0e240SYoshihiro Shimoda 	}
3433df0e240SYoshihiro Shimoda 
3443df0e240SYoshihiro Shimoda 	return ret;
3453df0e240SYoshihiro Shimoda }
3463df0e240SYoshihiro Shimoda 
3473df0e240SYoshihiro Shimoda static void usbhsc_clk_disable_unprepare(struct usbhs_priv *priv)
3483df0e240SYoshihiro Shimoda {
3493df0e240SYoshihiro Shimoda 	int i;
3503df0e240SYoshihiro Shimoda 
3513df0e240SYoshihiro Shimoda 	if (!usbhsc_is_multi_clks(priv))
3523df0e240SYoshihiro Shimoda 		return;
3533df0e240SYoshihiro Shimoda 
3543df0e240SYoshihiro Shimoda 	for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
3553df0e240SYoshihiro Shimoda 		clk_disable_unprepare(priv->clks[i]);
3563df0e240SYoshihiro Shimoda }
3573df0e240SYoshihiro Shimoda 
358f1407d5cSKuninori Morimoto /*
359f1407d5cSKuninori Morimoto  *		platform default param
360f1407d5cSKuninori Morimoto  */
3618ecef00fSUlrich Hecht 
3628ecef00fSUlrich Hecht /* commonly used on old SH-Mobile SoCs */
36351f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_default_pipe[] = {
36451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
36551f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, false),
36651f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x18, false),
36751f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x28, true),
36851f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x38, true),
36951f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
37051f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
37151f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
37251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
37351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x07, false),
374f1407d5cSKuninori Morimoto };
375f1407d5cSKuninori Morimoto 
3768ecef00fSUlrich Hecht /* commonly used on newer SH-Mobile and R-Car SoCs */
37751f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_new_pipe[] = {
37851f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
37951f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true),
38051f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true),
38151f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
38251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true),
38351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true),
38451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
38551f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
38651f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
38751f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x78, true),
38851f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x88, true),
38951f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x98, true),
39051f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xa8, true),
39151f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xb8, true),
39251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xc8, true),
39351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xd8, true),
3948ecef00fSUlrich Hecht };
3958ecef00fSUlrich Hecht 
396f1407d5cSKuninori Morimoto /*
3976e267da8SKuninori Morimoto  *		power control
3986e267da8SKuninori Morimoto  */
3996e267da8SKuninori Morimoto static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
4006e267da8SKuninori Morimoto {
401f1ee56a0SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
4026e267da8SKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
4036e267da8SKuninori Morimoto 
4046e267da8SKuninori Morimoto 	if (enable) {
4056e267da8SKuninori Morimoto 		/* enable PM */
4066e267da8SKuninori Morimoto 		pm_runtime_get_sync(dev);
4076e267da8SKuninori Morimoto 
4083df0e240SYoshihiro Shimoda 		/* enable clks */
4093df0e240SYoshihiro Shimoda 		if (usbhsc_clk_prepare_enable(priv))
4103df0e240SYoshihiro Shimoda 			return;
4113df0e240SYoshihiro Shimoda 
412f1ee56a0SKuninori Morimoto 		/* enable platform power */
413f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
414f1ee56a0SKuninori Morimoto 
4156e267da8SKuninori Morimoto 		/* USB on */
4166e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
4176e267da8SKuninori Morimoto 	} else {
4186e267da8SKuninori Morimoto 		/* USB off */
4196e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
4206e267da8SKuninori Morimoto 
421f1ee56a0SKuninori Morimoto 		/* disable platform power */
422f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
423f1ee56a0SKuninori Morimoto 
4243df0e240SYoshihiro Shimoda 		/* disable clks */
4253df0e240SYoshihiro Shimoda 		usbhsc_clk_disable_unprepare(priv);
4263df0e240SYoshihiro Shimoda 
4276e267da8SKuninori Morimoto 		/* disable PM */
4286e267da8SKuninori Morimoto 		pm_runtime_put_sync(dev);
4296e267da8SKuninori Morimoto 	}
4306e267da8SKuninori Morimoto }
4316e267da8SKuninori Morimoto 
4326e267da8SKuninori Morimoto /*
433ca8a282aSKuninori Morimoto  *		hotplug
434f1407d5cSKuninori Morimoto  */
435ca8a282aSKuninori Morimoto static void usbhsc_hotplug(struct usbhs_priv *priv)
436f1407d5cSKuninori Morimoto {
437f1407d5cSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
438f1407d5cSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
439f1407d5cSKuninori Morimoto 	int id;
440f1407d5cSKuninori Morimoto 	int enable;
441d7b3968dSSergei Shtylyov 	int cable;
442f1407d5cSKuninori Morimoto 	int ret;
443f1407d5cSKuninori Morimoto 
444f1407d5cSKuninori Morimoto 	/*
445f1407d5cSKuninori Morimoto 	 * get vbus status from platform
446f1407d5cSKuninori Morimoto 	 */
447ccc3264cSYoshihiro Shimoda 	enable = usbhs_mod_info_call(priv, get_vbus, pdev);
448f1407d5cSKuninori Morimoto 
449f1407d5cSKuninori Morimoto 	/*
450f1407d5cSKuninori Morimoto 	 * get id from platform
451f1407d5cSKuninori Morimoto 	 */
452f1407d5cSKuninori Morimoto 	id = usbhs_platform_call(priv, get_id, pdev);
453f1407d5cSKuninori Morimoto 
454f1407d5cSKuninori Morimoto 	if (enable && !mod) {
455d7b3968dSSergei Shtylyov 		if (priv->edev) {
456ea07b8cfSChanwoo Choi 			cable = extcon_get_state(priv->edev, EXTCON_USB_HOST);
457d7b3968dSSergei Shtylyov 			if ((cable > 0 && id != USBHS_HOST) ||
458d7b3968dSSergei Shtylyov 			    (!cable && id != USBHS_GADGET)) {
459d7b3968dSSergei Shtylyov 				dev_info(&pdev->dev,
460d7b3968dSSergei Shtylyov 					 "USB cable plugged in doesn't match the selected role!\n");
461d7b3968dSSergei Shtylyov 				return;
462d7b3968dSSergei Shtylyov 			}
463d7b3968dSSergei Shtylyov 		}
464d7b3968dSSergei Shtylyov 
465f1407d5cSKuninori Morimoto 		ret = usbhs_mod_change(priv, id);
466f1407d5cSKuninori Morimoto 		if (ret < 0)
467f1407d5cSKuninori Morimoto 			return;
468f1407d5cSKuninori Morimoto 
469f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s enable\n", __func__);
470f1407d5cSKuninori Morimoto 
4716e267da8SKuninori Morimoto 		/* power on */
47297a79684SChris Brandt 		if (usbhs_get_dparam(priv, runtime_pwctrl))
4736e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
474f1407d5cSKuninori Morimoto 
475258485d9SKuninori Morimoto 		/* bus init */
476258485d9SKuninori Morimoto 		usbhsc_set_buswait(priv);
477258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
478258485d9SKuninori Morimoto 
479f1407d5cSKuninori Morimoto 		/* module start */
480f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, start, priv);
481f1407d5cSKuninori Morimoto 
482f1407d5cSKuninori Morimoto 	} else if (!enable && mod) {
483f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s disable\n", __func__);
484f1407d5cSKuninori Morimoto 
485f1407d5cSKuninori Morimoto 		/* module stop */
486f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
487f1407d5cSKuninori Morimoto 
488258485d9SKuninori Morimoto 		/* bus init */
489258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
490258485d9SKuninori Morimoto 
4916e267da8SKuninori Morimoto 		/* power off */
49297a79684SChris Brandt 		if (usbhs_get_dparam(priv, runtime_pwctrl))
4936e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
494f1407d5cSKuninori Morimoto 
495f1407d5cSKuninori Morimoto 		usbhs_mod_change(priv, -1);
496f1407d5cSKuninori Morimoto 
497f1407d5cSKuninori Morimoto 		/* reset phy for next connection */
498f1407d5cSKuninori Morimoto 		usbhs_platform_call(priv, phy_reset, pdev);
499f1407d5cSKuninori Morimoto 	}
500f1407d5cSKuninori Morimoto }
501f1407d5cSKuninori Morimoto 
502ca8a282aSKuninori Morimoto /*
503ca8a282aSKuninori Morimoto  *		notify hotplug
504ca8a282aSKuninori Morimoto  */
505ca8a282aSKuninori Morimoto static void usbhsc_notify_hotplug(struct work_struct *work)
506ca8a282aSKuninori Morimoto {
507ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = container_of(work,
508ca8a282aSKuninori Morimoto 					       struct usbhs_priv,
509ca8a282aSKuninori Morimoto 					       notify_hotplug_work.work);
510ca8a282aSKuninori Morimoto 	usbhsc_hotplug(priv);
511ca8a282aSKuninori Morimoto }
512ca8a282aSKuninori Morimoto 
5130966648dSYoshihiro Shimoda int usbhsc_schedule_notify_hotplug(struct platform_device *pdev)
514f1407d5cSKuninori Morimoto {
515206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
516bc57381eSKuninori Morimoto 	int delay = usbhs_get_dparam(priv, detection_delay);
517f1407d5cSKuninori Morimoto 
518f1407d5cSKuninori Morimoto 	/*
519f1407d5cSKuninori Morimoto 	 * This functions will be called in interrupt.
520f1407d5cSKuninori Morimoto 	 * To make sure safety context,
521f1407d5cSKuninori Morimoto 	 * use workqueue for usbhs_notify_hotplug
522f1407d5cSKuninori Morimoto 	 */
523a49a88f1SKuninori Morimoto 	schedule_delayed_work(&priv->notify_hotplug_work,
524a49a88f1SKuninori Morimoto 			      msecs_to_jiffies(delay));
525f1407d5cSKuninori Morimoto 	return 0;
526f1407d5cSKuninori Morimoto }
527f1407d5cSKuninori Morimoto 
528be21a02aSYoshihiro Shimoda static const struct usbhs_of_data rcar_gen2_data = {
529be21a02aSYoshihiro Shimoda 	.platform_callback = &usbhs_rcar2_ops,
530be21a02aSYoshihiro Shimoda 	.param = {
531be21a02aSYoshihiro Shimoda 		.has_usb_dmac = 1,
53298e86506SYoshihiro Shimoda 		.has_new_pipe_configs = 1,
533be21a02aSYoshihiro Shimoda 	}
534be21a02aSYoshihiro Shimoda };
535be21a02aSYoshihiro Shimoda 
536be21a02aSYoshihiro Shimoda static const struct usbhs_of_data rcar_gen3_data = {
537be21a02aSYoshihiro Shimoda 	.platform_callback = &usbhs_rcar3_ops,
538be21a02aSYoshihiro Shimoda 	.param = {
539be21a02aSYoshihiro Shimoda 		.has_usb_dmac = 1,
540df9f2c27SYoshihiro Shimoda 		.multi_clks = 1,
54198e86506SYoshihiro Shimoda 		.has_new_pipe_configs = 1,
542be21a02aSYoshihiro Shimoda 	}
543be21a02aSYoshihiro Shimoda };
544be21a02aSYoshihiro Shimoda 
545be21a02aSYoshihiro Shimoda static const struct usbhs_of_data rcar_gen3_with_pll_data = {
546be21a02aSYoshihiro Shimoda 	.platform_callback = &usbhs_rcar3_with_pll_ops,
547be21a02aSYoshihiro Shimoda 	.param = {
548be21a02aSYoshihiro Shimoda 		.has_usb_dmac = 1,
549df9f2c27SYoshihiro Shimoda 		.multi_clks = 1,
55098e86506SYoshihiro Shimoda 		.has_new_pipe_configs = 1,
551be21a02aSYoshihiro Shimoda 	}
552be21a02aSYoshihiro Shimoda };
553be21a02aSYoshihiro Shimoda 
554be21a02aSYoshihiro Shimoda static const struct usbhs_of_data rza1_data = {
555be21a02aSYoshihiro Shimoda 	.platform_callback = &usbhs_rza1_ops,
556be21a02aSYoshihiro Shimoda 	.param = {
55798e86506SYoshihiro Shimoda 		.has_new_pipe_configs = 1,
558be21a02aSYoshihiro Shimoda 	}
559be21a02aSYoshihiro Shimoda };
560be21a02aSYoshihiro Shimoda 
561b69dce63SChris Brandt static const struct usbhs_of_data rza2_data = {
562b69dce63SChris Brandt 	.platform_callback = &usbhs_rza2_ops,
563b69dce63SChris Brandt 	.param = {
564b69dce63SChris Brandt 		.has_cnen = 1,
565b69dce63SChris Brandt 		.cfifo_byte_addr = 1,
56698e86506SYoshihiro Shimoda 		.has_new_pipe_configs = 1,
567b69dce63SChris Brandt 	}
568b69dce63SChris Brandt };
569b69dce63SChris Brandt 
570f1407d5cSKuninori Morimoto /*
571f1407d5cSKuninori Morimoto  *		platform functions
572f1407d5cSKuninori Morimoto  */
573b854100eSYoshihiro Shimoda static const struct of_device_id usbhs_of_match[] = {
574b854100eSYoshihiro Shimoda 	{
5751d6e81a2SFabrizio Castro 		.compatible = "renesas,usbhs-r8a774c0",
576be21a02aSYoshihiro Shimoda 		.data = &rcar_gen3_with_pll_data,
5771d6e81a2SFabrizio Castro 	},
5781d6e81a2SFabrizio Castro 	{
579b854100eSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7790",
580be21a02aSYoshihiro Shimoda 		.data = &rcar_gen2_data,
581b854100eSYoshihiro Shimoda 	},
582b854100eSYoshihiro Shimoda 	{
583b854100eSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7791",
584be21a02aSYoshihiro Shimoda 		.data = &rcar_gen2_data,
585b854100eSYoshihiro Shimoda 	},
586af6e613bSYoshihiro Shimoda 	{
587af6e613bSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7794",
588be21a02aSYoshihiro Shimoda 		.data = &rcar_gen2_data,
589af6e613bSYoshihiro Shimoda 	},
590f5f6afa8SYoshihiro Shimoda 	{
591f5f6afa8SYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7795",
592be21a02aSYoshihiro Shimoda 		.data = &rcar_gen3_data,
593f5f6afa8SYoshihiro Shimoda 	},
5942f1a993aSSimon Horman 	{
595aae819e1SYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7796",
596be21a02aSYoshihiro Shimoda 		.data = &rcar_gen3_data,
597aae819e1SYoshihiro Shimoda 	},
598aae819e1SYoshihiro Shimoda 	{
5994d2a863fSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a77990",
600be21a02aSYoshihiro Shimoda 		.data = &rcar_gen3_with_pll_data,
6014d2a863fSYoshihiro Shimoda 	},
6024d2a863fSYoshihiro Shimoda 	{
6030f38672cSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a77995",
604be21a02aSYoshihiro Shimoda 		.data = &rcar_gen3_with_pll_data,
6050f38672cSYoshihiro Shimoda 	},
6060f38672cSYoshihiro Shimoda 	{
6072f1a993aSSimon Horman 		.compatible = "renesas,rcar-gen2-usbhs",
608be21a02aSYoshihiro Shimoda 		.data = &rcar_gen2_data,
6092f1a993aSSimon Horman 	},
6102f1a993aSSimon Horman 	{
6112f1a993aSSimon Horman 		.compatible = "renesas,rcar-gen3-usbhs",
612be21a02aSYoshihiro Shimoda 		.data = &rcar_gen3_data,
6132f1a993aSSimon Horman 	},
614aec2927bSChris Brandt 	{
615aec2927bSChris Brandt 		.compatible = "renesas,rza1-usbhs",
616be21a02aSYoshihiro Shimoda 		.data = &rza1_data,
617aec2927bSChris Brandt 	},
618b69dce63SChris Brandt 	{
619b69dce63SChris Brandt 		.compatible = "renesas,rza2-usbhs",
620b69dce63SChris Brandt 		.data = &rza2_data,
621b69dce63SChris Brandt 	},
622b854100eSYoshihiro Shimoda 	{ },
623b854100eSYoshihiro Shimoda };
624b854100eSYoshihiro Shimoda MODULE_DEVICE_TABLE(of, usbhs_of_match);
625b854100eSYoshihiro Shimoda 
626b854100eSYoshihiro Shimoda static struct renesas_usbhs_platform_info *usbhs_parse_dt(struct device *dev)
627b854100eSYoshihiro Shimoda {
628b854100eSYoshihiro Shimoda 	struct renesas_usbhs_platform_info *info;
629b854100eSYoshihiro Shimoda 	struct renesas_usbhs_driver_param *dparam;
630be21a02aSYoshihiro Shimoda 	const struct usbhs_of_data *data;
631b854100eSYoshihiro Shimoda 	u32 tmp;
632b854100eSYoshihiro Shimoda 	int gpio;
633b854100eSYoshihiro Shimoda 
634b854100eSYoshihiro Shimoda 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
635b854100eSYoshihiro Shimoda 	if (!info)
636b854100eSYoshihiro Shimoda 		return NULL;
637b854100eSYoshihiro Shimoda 
638be21a02aSYoshihiro Shimoda 	data = of_device_get_match_data(dev);
639be21a02aSYoshihiro Shimoda 	if (!data)
640be21a02aSYoshihiro Shimoda 		return NULL;
641be21a02aSYoshihiro Shimoda 
642b854100eSYoshihiro Shimoda 	dparam = &info->driver_param;
643743344a9SYoshihiro Shimoda 	*dparam = data->param;
644743344a9SYoshihiro Shimoda 	info->platform_callback = *data->platform_callback;
645be21a02aSYoshihiro Shimoda 
64631e795c6SYoshihiro Shimoda 	if (!of_property_read_u32(dev_of_node(dev), "renesas,buswait", &tmp))
647b854100eSYoshihiro Shimoda 		dparam->buswait_bwait = tmp;
64831e795c6SYoshihiro Shimoda 	gpio = of_get_named_gpio_flags(dev_of_node(dev), "renesas,enable-gpio",
64931e795c6SYoshihiro Shimoda 				       0, NULL);
650b854100eSYoshihiro Shimoda 	if (gpio > 0)
651b854100eSYoshihiro Shimoda 		dparam->enable_gpio = gpio;
652b854100eSYoshihiro Shimoda 
653b854100eSYoshihiro Shimoda 	return info;
654b854100eSYoshihiro Shimoda }
655b854100eSYoshihiro Shimoda 
656b7a8d17dSKuninori Morimoto static int usbhs_probe(struct platform_device *pdev)
657f1407d5cSKuninori Morimoto {
658f0f14a7aSKuninori Morimoto 	struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
659f1407d5cSKuninori Morimoto 	struct usbhs_priv *priv;
66053069af3SShimoda, Yoshihiro 	struct resource *res, *irq_res;
661*f08acaf0SYoshihiro Shimoda 	struct device *dev = &pdev->dev;
662f1407d5cSKuninori Morimoto 	int ret;
663f1407d5cSKuninori Morimoto 
664b854100eSYoshihiro Shimoda 	/* check device node */
665*f08acaf0SYoshihiro Shimoda 	if (dev_of_node(dev))
666b854100eSYoshihiro Shimoda 		info = pdev->dev.platform_data = usbhs_parse_dt(&pdev->dev);
667b854100eSYoshihiro Shimoda 
668f1407d5cSKuninori Morimoto 	/* check platform information */
6698ecef00fSUlrich Hecht 	if (!info) {
670*f08acaf0SYoshihiro Shimoda 		dev_err(dev, "no platform information\n");
671f1407d5cSKuninori Morimoto 		return -EINVAL;
672f1407d5cSKuninori Morimoto 	}
673f1407d5cSKuninori Morimoto 
674f1407d5cSKuninori Morimoto 	/* platform data */
67553069af3SShimoda, Yoshihiro 	irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
676687f16b8SVarka Bhadram 	if (!irq_res) {
677*f08acaf0SYoshihiro Shimoda 		dev_err(dev, "Not enough Renesas USB platform resources.\n");
678f1407d5cSKuninori Morimoto 		return -ENODEV;
679f1407d5cSKuninori Morimoto 	}
680f1407d5cSKuninori Morimoto 
681f1407d5cSKuninori Morimoto 	/* usb private data */
682*f08acaf0SYoshihiro Shimoda 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
683b1cb07cdSPeter Chen 	if (!priv)
684f1407d5cSKuninori Morimoto 		return -ENOMEM;
685f1407d5cSKuninori Morimoto 
686687f16b8SVarka Bhadram 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
687148e1134SThierry Reding 	priv->base = devm_ioremap_resource(&pdev->dev, res);
688148e1134SThierry Reding 	if (IS_ERR(priv->base))
689148e1134SThierry Reding 		return PTR_ERR(priv->base);
690f1407d5cSKuninori Morimoto 
691*f08acaf0SYoshihiro Shimoda 	if (of_property_read_bool(dev_of_node(dev), "extcon")) {
692*f08acaf0SYoshihiro Shimoda 		priv->edev = extcon_get_edev_by_phandle(dev, 0);
693d7b3968dSSergei Shtylyov 		if (IS_ERR(priv->edev))
694d7b3968dSSergei Shtylyov 			return PTR_ERR(priv->edev);
695d7b3968dSSergei Shtylyov 	}
696d7b3968dSSergei Shtylyov 
697*f08acaf0SYoshihiro Shimoda 	priv->rsts = devm_reset_control_array_get_optional_shared(dev);
698f181dbb4SYoshihiro Shimoda 	if (IS_ERR(priv->rsts))
699f181dbb4SYoshihiro Shimoda 		return PTR_ERR(priv->rsts);
700f181dbb4SYoshihiro Shimoda 
701f1407d5cSKuninori Morimoto 	/*
702f1407d5cSKuninori Morimoto 	 * care platform info
703f1407d5cSKuninori Morimoto 	 */
7048ecef00fSUlrich Hecht 
705743344a9SYoshihiro Shimoda 	priv->dparam = info->driver_param;
706f1407d5cSKuninori Morimoto 
7078ecef00fSUlrich Hecht 	if (!info->platform_callback.get_id) {
708*f08acaf0SYoshihiro Shimoda 		dev_err(dev, "no platform callbacks\n");
7098ecef00fSUlrich Hecht 		return -EINVAL;
7108ecef00fSUlrich Hecht 	}
711743344a9SYoshihiro Shimoda 	priv->pfunc = info->platform_callback;
7128ecef00fSUlrich Hecht 
713f1407d5cSKuninori Morimoto 	/* set default param if platform doesn't have */
71498e86506SYoshihiro Shimoda 	if (usbhs_get_dparam(priv, has_new_pipe_configs)) {
71598e86506SYoshihiro Shimoda 		priv->dparam.pipe_configs = usbhsc_new_pipe;
71698e86506SYoshihiro Shimoda 		priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
71798e86506SYoshihiro Shimoda 	} else if (!priv->dparam.pipe_configs) {
71851f141a9SYoshihiro Shimoda 		priv->dparam.pipe_configs = usbhsc_default_pipe;
71951f141a9SYoshihiro Shimoda 		priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe);
720f1407d5cSKuninori Morimoto 	}
72148298206SKuninori Morimoto 	if (!priv->dparam.pio_dma_border)
72248298206SKuninori Morimoto 		priv->dparam.pio_dma_border = 64; /* 64byte */
723f1407d5cSKuninori Morimoto 
724b002ff6eSKuninori Morimoto 	/* FIXME */
725b002ff6eSKuninori Morimoto 	/* runtime power control ? */
72648298206SKuninori Morimoto 	if (priv->pfunc.get_vbus)
72797a79684SChris Brandt 		usbhs_get_dparam(priv, runtime_pwctrl) = 1;
728b002ff6eSKuninori Morimoto 
729f1407d5cSKuninori Morimoto 	/*
730f1407d5cSKuninori Morimoto 	 * priv settings
731f1407d5cSKuninori Morimoto 	 */
73253069af3SShimoda, Yoshihiro 	priv->irq	= irq_res->start;
73353069af3SShimoda, Yoshihiro 	if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
73453069af3SShimoda, Yoshihiro 		priv->irqflags = IRQF_SHARED;
735f1407d5cSKuninori Morimoto 	priv->pdev	= pdev;
736bc57381eSKuninori Morimoto 	INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
737f1407d5cSKuninori Morimoto 	spin_lock_init(usbhs_priv_to_lock(priv));
738f1407d5cSKuninori Morimoto 
739f1407d5cSKuninori Morimoto 	/* call pipe and module init */
740f1407d5cSKuninori Morimoto 	ret = usbhs_pipe_probe(priv);
741f1407d5cSKuninori Morimoto 	if (ret < 0)
74258efc77cSKuninori Morimoto 		return ret;
743f1407d5cSKuninori Morimoto 
744d3af90a5SKuninori Morimoto 	ret = usbhs_fifo_probe(priv);
745f1407d5cSKuninori Morimoto 	if (ret < 0)
74697f93227SKuninori Morimoto 		goto probe_end_pipe_exit;
747f1407d5cSKuninori Morimoto 
748d3af90a5SKuninori Morimoto 	ret = usbhs_mod_probe(priv);
749d3af90a5SKuninori Morimoto 	if (ret < 0)
750d3af90a5SKuninori Morimoto 		goto probe_end_fifo_exit;
751d3af90a5SKuninori Morimoto 
752f1407d5cSKuninori Morimoto 	/* dev_set_drvdata should be called after usbhs_mod_init */
753c9a0552eSLibo Chen 	platform_set_drvdata(pdev, priv);
754f1407d5cSKuninori Morimoto 
755f181dbb4SYoshihiro Shimoda 	ret = reset_control_deassert(priv->rsts);
756f181dbb4SYoshihiro Shimoda 	if (ret)
757f181dbb4SYoshihiro Shimoda 		goto probe_fail_rst;
758f181dbb4SYoshihiro Shimoda 
759*f08acaf0SYoshihiro Shimoda 	ret = usbhsc_clk_get(dev, priv);
7603df0e240SYoshihiro Shimoda 	if (ret)
7613df0e240SYoshihiro Shimoda 		goto probe_fail_clks;
7623df0e240SYoshihiro Shimoda 
763f1407d5cSKuninori Morimoto 	/*
764f1407d5cSKuninori Morimoto 	 * deviece reset here because
765f1407d5cSKuninori Morimoto 	 * USB device might be used in boot loader.
766f1407d5cSKuninori Morimoto 	 */
767f1407d5cSKuninori Morimoto 	usbhs_sys_clock_ctrl(priv, 0);
768f1407d5cSKuninori Morimoto 
7698ecef00fSUlrich Hecht 	/* check GPIO determining if USB function should be enabled */
7708ecef00fSUlrich Hecht 	if (priv->dparam.enable_gpio) {
7718ecef00fSUlrich Hecht 		gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL);
7728ecef00fSUlrich Hecht 		ret = !gpio_get_value(priv->dparam.enable_gpio);
7738ecef00fSUlrich Hecht 		gpio_free(priv->dparam.enable_gpio);
7748ecef00fSUlrich Hecht 		if (ret) {
775*f08acaf0SYoshihiro Shimoda 			dev_warn(dev, "USB function not selected (GPIO %d)\n",
7768ecef00fSUlrich Hecht 				 priv->dparam.enable_gpio);
7778ecef00fSUlrich Hecht 			ret = -ENOTSUPP;
7788ecef00fSUlrich Hecht 			goto probe_end_mod_exit;
7798ecef00fSUlrich Hecht 		}
7808ecef00fSUlrich Hecht 	}
7818ecef00fSUlrich Hecht 
782f1407d5cSKuninori Morimoto 	/*
783f1407d5cSKuninori Morimoto 	 * platform call
784f1407d5cSKuninori Morimoto 	 *
785f1407d5cSKuninori Morimoto 	 * USB phy setup might depend on CPU/Board.
786f1407d5cSKuninori Morimoto 	 * If platform has its callback functions,
787f1407d5cSKuninori Morimoto 	 * call it here.
788f1407d5cSKuninori Morimoto 	 */
789f1407d5cSKuninori Morimoto 	ret = usbhs_platform_call(priv, hardware_init, pdev);
790f1407d5cSKuninori Morimoto 	if (ret < 0) {
791*f08acaf0SYoshihiro Shimoda 		dev_err(dev, "platform init failed.\n");
79297f93227SKuninori Morimoto 		goto probe_end_mod_exit;
793f1407d5cSKuninori Morimoto 	}
794f1407d5cSKuninori Morimoto 
795f1407d5cSKuninori Morimoto 	/* reset phy for connection */
796f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
797f1407d5cSKuninori Morimoto 
798b002ff6eSKuninori Morimoto 	/* power control */
799*f08acaf0SYoshihiro Shimoda 	pm_runtime_enable(dev);
80097a79684SChris Brandt 	if (!usbhs_get_dparam(priv, runtime_pwctrl)) {
801b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
802b002ff6eSKuninori Morimoto 		usbhs_mod_autonomy_mode(priv);
803ccc3264cSYoshihiro Shimoda 	} else {
804ccc3264cSYoshihiro Shimoda 		usbhs_mod_non_autonomy_mode(priv);
805b002ff6eSKuninori Morimoto 	}
806b002ff6eSKuninori Morimoto 
807f1407d5cSKuninori Morimoto 	/*
808f1407d5cSKuninori Morimoto 	 * manual call notify_hotplug for cold plug
809f1407d5cSKuninori Morimoto 	 */
8100966648dSYoshihiro Shimoda 	usbhsc_schedule_notify_hotplug(pdev);
811f1407d5cSKuninori Morimoto 
812*f08acaf0SYoshihiro Shimoda 	dev_info(dev, "probed\n");
813f1407d5cSKuninori Morimoto 
814f1407d5cSKuninori Morimoto 	return ret;
815f1407d5cSKuninori Morimoto 
816f1407d5cSKuninori Morimoto probe_end_mod_exit:
8173df0e240SYoshihiro Shimoda 	usbhsc_clk_put(priv);
8183df0e240SYoshihiro Shimoda probe_fail_clks:
819f181dbb4SYoshihiro Shimoda 	reset_control_assert(priv->rsts);
820f181dbb4SYoshihiro Shimoda probe_fail_rst:
821f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
822d3af90a5SKuninori Morimoto probe_end_fifo_exit:
823d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
82497f93227SKuninori Morimoto probe_end_pipe_exit:
82597f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
826f1407d5cSKuninori Morimoto 
827*f08acaf0SYoshihiro Shimoda 	dev_info(dev, "probe failed (%d)\n", ret);
828f1407d5cSKuninori Morimoto 
829f1407d5cSKuninori Morimoto 	return ret;
830f1407d5cSKuninori Morimoto }
831f1407d5cSKuninori Morimoto 
832fb4e98abSBill Pemberton static int usbhs_remove(struct platform_device *pdev)
833f1407d5cSKuninori Morimoto {
834206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
835f1407d5cSKuninori Morimoto 
836f1407d5cSKuninori Morimoto 	dev_dbg(&pdev->dev, "usb remove\n");
837f1407d5cSKuninori Morimoto 
838b002ff6eSKuninori Morimoto 	/* power off */
83997a79684SChris Brandt 	if (!usbhs_get_dparam(priv, runtime_pwctrl))
840b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
841f1407d5cSKuninori Morimoto 
842b002ff6eSKuninori Morimoto 	pm_runtime_disable(&pdev->dev);
843f1407d5cSKuninori Morimoto 
844f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, hardware_exit, pdev);
8453df0e240SYoshihiro Shimoda 	usbhsc_clk_put(priv);
846f181dbb4SYoshihiro Shimoda 	reset_control_assert(priv->rsts);
847f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
848d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
84997f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
850f1407d5cSKuninori Morimoto 
851f1407d5cSKuninori Morimoto 	return 0;
852f1407d5cSKuninori Morimoto }
853f1407d5cSKuninori Morimoto 
854eaf3074eSArnd Bergmann static __maybe_unused int usbhsc_suspend(struct device *dev)
855ca8a282aSKuninori Morimoto {
856ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
857ca8a282aSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
858ca8a282aSKuninori Morimoto 
859ca8a282aSKuninori Morimoto 	if (mod) {
860ca8a282aSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
861ca8a282aSKuninori Morimoto 		usbhs_mod_change(priv, -1);
862ca8a282aSKuninori Morimoto 	}
863ca8a282aSKuninori Morimoto 
86497a79684SChris Brandt 	if (mod || !usbhs_get_dparam(priv, runtime_pwctrl))
865ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
866ca8a282aSKuninori Morimoto 
867ca8a282aSKuninori Morimoto 	return 0;
868ca8a282aSKuninori Morimoto }
869ca8a282aSKuninori Morimoto 
870eaf3074eSArnd Bergmann static __maybe_unused int usbhsc_resume(struct device *dev)
871ca8a282aSKuninori Morimoto {
872ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
873ca8a282aSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
874ca8a282aSKuninori Morimoto 
87597a79684SChris Brandt 	if (!usbhs_get_dparam(priv, runtime_pwctrl)) {
876ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
87759a0879aSYoshihiro Shimoda 		usbhs_mod_autonomy_mode(priv);
87859a0879aSYoshihiro Shimoda 	}
879ca8a282aSKuninori Morimoto 
8805b50d3b5SKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
8815b50d3b5SKuninori Morimoto 
8820966648dSYoshihiro Shimoda 	usbhsc_schedule_notify_hotplug(pdev);
883ca8a282aSKuninori Morimoto 
884ca8a282aSKuninori Morimoto 	return 0;
885ca8a282aSKuninori Morimoto }
886ca8a282aSKuninori Morimoto 
887d54d334eSYoshihiro Shimoda static SIMPLE_DEV_PM_OPS(usbhsc_pm_ops, usbhsc_suspend, usbhsc_resume);
888ca8a282aSKuninori Morimoto 
889f1407d5cSKuninori Morimoto static struct platform_driver renesas_usbhs_driver = {
890f1407d5cSKuninori Morimoto 	.driver		= {
891f1407d5cSKuninori Morimoto 		.name	= "renesas_usbhs",
892ca8a282aSKuninori Morimoto 		.pm	= &usbhsc_pm_ops,
893b854100eSYoshihiro Shimoda 		.of_match_table = of_match_ptr(usbhs_of_match),
894f1407d5cSKuninori Morimoto 	},
895f1407d5cSKuninori Morimoto 	.probe		= usbhs_probe,
8967690417dSBill Pemberton 	.remove		= usbhs_remove,
897f1407d5cSKuninori Morimoto };
898f1407d5cSKuninori Morimoto 
899cc27c96cSAxel Lin module_platform_driver(renesas_usbhs_driver);
900f1407d5cSKuninori Morimoto 
901f1407d5cSKuninori Morimoto MODULE_LICENSE("GPL");
902f1407d5cSKuninori Morimoto MODULE_DESCRIPTION("Renesas USB driver");
903f1407d5cSKuninori Morimoto MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
904