xref: /openbmc/linux/drivers/usb/renesas_usbhs/common.c (revision 58efc77c86af5cb81d7d7ea0c3c3e675e7d121dc)
1f1407d5cSKuninori Morimoto /*
2f1407d5cSKuninori Morimoto  * Renesas USB driver
3f1407d5cSKuninori Morimoto  *
4f1407d5cSKuninori Morimoto  * Copyright (C) 2011 Renesas Solutions Corp.
5f1407d5cSKuninori Morimoto  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6f1407d5cSKuninori Morimoto  *
7f1407d5cSKuninori Morimoto  * This program is distributed in the hope that it will be useful,
8f1407d5cSKuninori Morimoto  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9f1407d5cSKuninori Morimoto  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10f1407d5cSKuninori Morimoto  * GNU General Public License for more details.
11f1407d5cSKuninori Morimoto  *
12f1407d5cSKuninori Morimoto  * You should have received a copy of the GNU General Public License
13f1407d5cSKuninori Morimoto  * along with this program; if not, write to the Free Software
14f1407d5cSKuninori Morimoto  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15f1407d5cSKuninori Morimoto  *
16f1407d5cSKuninori Morimoto  */
17f1407d5cSKuninori Morimoto #include <linux/io.h>
18f1407d5cSKuninori Morimoto #include <linux/module.h>
19f1407d5cSKuninori Morimoto #include <linux/pm_runtime.h>
20f1407d5cSKuninori Morimoto #include <linux/slab.h>
21f1407d5cSKuninori Morimoto #include <linux/sysfs.h>
22cc502bb7SPaul Bolle #include "common.h"
23f1407d5cSKuninori Morimoto 
24233f519dSKuninori Morimoto /*
25233f519dSKuninori Morimoto  *		image of renesas_usbhs
26233f519dSKuninori Morimoto  *
27233f519dSKuninori Morimoto  * ex) gadget case
28233f519dSKuninori Morimoto 
29233f519dSKuninori Morimoto  * mod.c
30233f519dSKuninori Morimoto  * mod_gadget.c
31233f519dSKuninori Morimoto  * mod_host.c		pipe.c		fifo.c
32233f519dSKuninori Morimoto  *
33233f519dSKuninori Morimoto  *			+-------+	+-----------+
34233f519dSKuninori Morimoto  *			| pipe0 |------>| fifo pio  |
35233f519dSKuninori Morimoto  * +------------+	+-------+	+-----------+
36233f519dSKuninori Morimoto  * | mod_gadget |=====> | pipe1 |--+
37233f519dSKuninori Morimoto  * +------------+	+-------+  |	+-----------+
38233f519dSKuninori Morimoto  *			| pipe2 |  |  +-| fifo dma0 |
39233f519dSKuninori Morimoto  * +------------+	+-------+  |  |	+-----------+
40233f519dSKuninori Morimoto  * | mod_host   |	| pipe3 |<-|--+
41233f519dSKuninori Morimoto  * +------------+	+-------+  |	+-----------+
42233f519dSKuninori Morimoto  *			| ....  |  +--->| fifo dma1 |
43233f519dSKuninori Morimoto  *			| ....  |	+-----------+
44233f519dSKuninori Morimoto  */
45233f519dSKuninori Morimoto 
46233f519dSKuninori Morimoto 
47b002ff6eSKuninori Morimoto #define USBHSF_RUNTIME_PWCTRL	(1 << 0)
48b002ff6eSKuninori Morimoto 
49b002ff6eSKuninori Morimoto /* status */
50b002ff6eSKuninori Morimoto #define usbhsc_flags_init(p)   do {(p)->flags = 0; } while (0)
51b002ff6eSKuninori Morimoto #define usbhsc_flags_set(p, b) ((p)->flags |=  (b))
52b002ff6eSKuninori Morimoto #define usbhsc_flags_clr(p, b) ((p)->flags &= ~(b))
53b002ff6eSKuninori Morimoto #define usbhsc_flags_has(p, b) ((p)->flags &   (b))
54b002ff6eSKuninori Morimoto 
55f1407d5cSKuninori Morimoto /*
56f1407d5cSKuninori Morimoto  * platform call back
57f1407d5cSKuninori Morimoto  *
58f1407d5cSKuninori Morimoto  * renesas usb support platform callback function.
59f1407d5cSKuninori Morimoto  * Below macro call it.
60f1407d5cSKuninori Morimoto  * if platform doesn't have callback, it return 0 (no error)
61f1407d5cSKuninori Morimoto  */
62f1407d5cSKuninori Morimoto #define usbhs_platform_call(priv, func, args...)\
63f1407d5cSKuninori Morimoto 	(!(priv) ? -ENODEV :			\
6448298206SKuninori Morimoto 	 !((priv)->pfunc.func) ? 0 :		\
6548298206SKuninori Morimoto 	 (priv)->pfunc.func(args))
66f1407d5cSKuninori Morimoto 
67f1407d5cSKuninori Morimoto /*
68f1407d5cSKuninori Morimoto  *		common functions
69f1407d5cSKuninori Morimoto  */
70f1407d5cSKuninori Morimoto u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
71f1407d5cSKuninori Morimoto {
72f1407d5cSKuninori Morimoto 	return ioread16(priv->base + reg);
73f1407d5cSKuninori Morimoto }
74f1407d5cSKuninori Morimoto 
75f1407d5cSKuninori Morimoto void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
76f1407d5cSKuninori Morimoto {
77f1407d5cSKuninori Morimoto 	iowrite16(data, priv->base + reg);
78f1407d5cSKuninori Morimoto }
79f1407d5cSKuninori Morimoto 
80f1407d5cSKuninori Morimoto void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
81f1407d5cSKuninori Morimoto {
82f1407d5cSKuninori Morimoto 	u16 val = usbhs_read(priv, reg);
83f1407d5cSKuninori Morimoto 
84f1407d5cSKuninori Morimoto 	val &= ~mask;
85f1407d5cSKuninori Morimoto 	val |= data & mask;
86f1407d5cSKuninori Morimoto 
87f1407d5cSKuninori Morimoto 	usbhs_write(priv, reg, val);
88f1407d5cSKuninori Morimoto }
89f1407d5cSKuninori Morimoto 
90206dcc2cSKuninori Morimoto struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
91206dcc2cSKuninori Morimoto {
92206dcc2cSKuninori Morimoto 	return dev_get_drvdata(&pdev->dev);
93206dcc2cSKuninori Morimoto }
94206dcc2cSKuninori Morimoto 
95f1407d5cSKuninori Morimoto /*
96f1407d5cSKuninori Morimoto  *		syscfg functions
97f1407d5cSKuninori Morimoto  */
9876190152SKuninori Morimoto static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
99f1407d5cSKuninori Morimoto {
100f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
101f1407d5cSKuninori Morimoto }
102f1407d5cSKuninori Morimoto 
103f1407d5cSKuninori Morimoto void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
104f1407d5cSKuninori Morimoto {
1053244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
1063244a7b4SKuninori Morimoto 	u16 val  = DCFM | DRPD | HSE | USBE;
107f427eb64SKuninori Morimoto 	int has_otg = usbhs_get_dparam(priv, has_otg);
108f427eb64SKuninori Morimoto 
109f427eb64SKuninori Morimoto 	if (has_otg)
110f427eb64SKuninori Morimoto 		usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN));
111f1407d5cSKuninori Morimoto 
112f1407d5cSKuninori Morimoto 	/*
113f1407d5cSKuninori Morimoto 	 * if enable
114f1407d5cSKuninori Morimoto 	 *
115f1407d5cSKuninori Morimoto 	 * - select Host mode
116f1407d5cSKuninori Morimoto 	 * - D+ Line/D- Line Pull-down
117f1407d5cSKuninori Morimoto 	 */
118f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
119f1407d5cSKuninori Morimoto }
120f1407d5cSKuninori Morimoto 
121f1407d5cSKuninori Morimoto void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
122f1407d5cSKuninori Morimoto {
1233244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
1243244a7b4SKuninori Morimoto 	u16 val  = DPRPU | HSE | USBE;
125f1407d5cSKuninori Morimoto 
126f1407d5cSKuninori Morimoto 	/*
127f1407d5cSKuninori Morimoto 	 * if enable
128f1407d5cSKuninori Morimoto 	 *
129f1407d5cSKuninori Morimoto 	 * - select Function mode
130f1407d5cSKuninori Morimoto 	 * - D+ Line Pull-up
131f1407d5cSKuninori Morimoto 	 */
132f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
133f1407d5cSKuninori Morimoto }
134f1407d5cSKuninori Morimoto 
135dfbb7f4fSKuninori Morimoto void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
136dfbb7f4fSKuninori Morimoto {
137dfbb7f4fSKuninori Morimoto 	usbhs_write(priv, TESTMODE, mode);
138dfbb7f4fSKuninori Morimoto }
139dfbb7f4fSKuninori Morimoto 
140f1407d5cSKuninori Morimoto /*
141f1407d5cSKuninori Morimoto  *		frame functions
142f1407d5cSKuninori Morimoto  */
143f1407d5cSKuninori Morimoto int usbhs_frame_get_num(struct usbhs_priv *priv)
144f1407d5cSKuninori Morimoto {
145f1407d5cSKuninori Morimoto 	return usbhs_read(priv, FRMNUM) & FRNM_MASK;
146f1407d5cSKuninori Morimoto }
147f1407d5cSKuninori Morimoto 
148f1407d5cSKuninori Morimoto /*
149ef8bedb9SKuninori Morimoto  *		usb request functions
150ef8bedb9SKuninori Morimoto  */
151ef8bedb9SKuninori Morimoto void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
152ef8bedb9SKuninori Morimoto {
153ef8bedb9SKuninori Morimoto 	u16 val;
154ef8bedb9SKuninori Morimoto 
155ef8bedb9SKuninori Morimoto 	val = usbhs_read(priv, USBREQ);
156ef8bedb9SKuninori Morimoto 	req->bRequest		= (val >> 8) & 0xFF;
157ef8bedb9SKuninori Morimoto 	req->bRequestType	= (val >> 0) & 0xFF;
158ef8bedb9SKuninori Morimoto 
159ef8bedb9SKuninori Morimoto 	req->wValue	= usbhs_read(priv, USBVAL);
160ef8bedb9SKuninori Morimoto 	req->wIndex	= usbhs_read(priv, USBINDX);
161ef8bedb9SKuninori Morimoto 	req->wLength	= usbhs_read(priv, USBLENG);
162ef8bedb9SKuninori Morimoto }
163ef8bedb9SKuninori Morimoto 
164ef8bedb9SKuninori Morimoto void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
165ef8bedb9SKuninori Morimoto {
166ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBREQ,  (req->bRequest << 8) | req->bRequestType);
167ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBVAL,  req->wValue);
168ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBINDX, req->wIndex);
169ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBLENG, req->wLength);
170ef8bedb9SKuninori Morimoto 
171ef8bedb9SKuninori Morimoto 	usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
172ef8bedb9SKuninori Morimoto }
173ef8bedb9SKuninori Morimoto 
174ef8bedb9SKuninori Morimoto /*
175258485d9SKuninori Morimoto  *		bus/vbus functions
176258485d9SKuninori Morimoto  */
177258485d9SKuninori Morimoto void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
178258485d9SKuninori Morimoto {
179a9be4a45SKuninori Morimoto 	u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
180a9be4a45SKuninori Morimoto 
181a9be4a45SKuninori Morimoto 	if (status != USBRST) {
182a9be4a45SKuninori Morimoto 		struct device *dev = usbhs_priv_to_dev(priv);
183a9be4a45SKuninori Morimoto 		dev_err(dev, "usbhs should be reset\n");
184a9be4a45SKuninori Morimoto 	}
185a9be4a45SKuninori Morimoto 
186258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
187258485d9SKuninori Morimoto }
188258485d9SKuninori Morimoto 
189258485d9SKuninori Morimoto void usbhs_bus_send_reset(struct usbhs_priv *priv)
190258485d9SKuninori Morimoto {
191258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
192258485d9SKuninori Morimoto }
193258485d9SKuninori Morimoto 
19475587f52SKuninori Morimoto int usbhs_bus_get_speed(struct usbhs_priv *priv)
19575587f52SKuninori Morimoto {
19675587f52SKuninori Morimoto 	u16 dvstctr = usbhs_read(priv, DVSTCTR);
19775587f52SKuninori Morimoto 
19875587f52SKuninori Morimoto 	switch (RHST & dvstctr) {
19975587f52SKuninori Morimoto 	case RHST_LOW_SPEED:
20075587f52SKuninori Morimoto 		return USB_SPEED_LOW;
20175587f52SKuninori Morimoto 	case RHST_FULL_SPEED:
20275587f52SKuninori Morimoto 		return USB_SPEED_FULL;
20375587f52SKuninori Morimoto 	case RHST_HIGH_SPEED:
20475587f52SKuninori Morimoto 		return USB_SPEED_HIGH;
20575587f52SKuninori Morimoto 	}
20675587f52SKuninori Morimoto 
20775587f52SKuninori Morimoto 	return USB_SPEED_UNKNOWN;
20875587f52SKuninori Morimoto }
20975587f52SKuninori Morimoto 
210258485d9SKuninori Morimoto int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
211258485d9SKuninori Morimoto {
212258485d9SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
213258485d9SKuninori Morimoto 
214258485d9SKuninori Morimoto 	return usbhs_platform_call(priv, set_vbus, pdev, enable);
215258485d9SKuninori Morimoto }
216258485d9SKuninori Morimoto 
217258485d9SKuninori Morimoto static void usbhsc_bus_init(struct usbhs_priv *priv)
218258485d9SKuninori Morimoto {
219258485d9SKuninori Morimoto 	usbhs_write(priv, DVSTCTR, 0);
220258485d9SKuninori Morimoto 
221258485d9SKuninori Morimoto 	usbhs_vbus_ctrl(priv, 0);
222258485d9SKuninori Morimoto }
223258485d9SKuninori Morimoto 
224258485d9SKuninori Morimoto /*
225eb05191fSKuninori Morimoto  *		device configuration
226eb05191fSKuninori Morimoto  */
2273dd49268SKuninori Morimoto int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
228eb05191fSKuninori Morimoto 			   u16 upphub, u16 hubport, u16 speed)
229eb05191fSKuninori Morimoto {
230eb05191fSKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
231eb05191fSKuninori Morimoto 	u16 usbspd = 0;
232eb05191fSKuninori Morimoto 	u32 reg = DEVADD0 + (2 * devnum);
233eb05191fSKuninori Morimoto 
234eb05191fSKuninori Morimoto 	if (devnum > 10) {
235eb05191fSKuninori Morimoto 		dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
236eb05191fSKuninori Morimoto 		return -EIO;
237eb05191fSKuninori Morimoto 	}
238eb05191fSKuninori Morimoto 
239eb05191fSKuninori Morimoto 	if (upphub > 0xA) {
240eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported hub number %d\n", upphub);
241eb05191fSKuninori Morimoto 		return -EIO;
242eb05191fSKuninori Morimoto 	}
243eb05191fSKuninori Morimoto 
244eb05191fSKuninori Morimoto 	switch (speed) {
245eb05191fSKuninori Morimoto 	case USB_SPEED_LOW:
246eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_LOW;
247eb05191fSKuninori Morimoto 		break;
248eb05191fSKuninori Morimoto 	case USB_SPEED_FULL:
249eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_FULL;
250eb05191fSKuninori Morimoto 		break;
251eb05191fSKuninori Morimoto 	case USB_SPEED_HIGH:
252eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_HIGH;
253eb05191fSKuninori Morimoto 		break;
254eb05191fSKuninori Morimoto 	default:
255eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported speed %d\n", speed);
256eb05191fSKuninori Morimoto 		return -EIO;
257eb05191fSKuninori Morimoto 	}
258eb05191fSKuninori Morimoto 
259eb05191fSKuninori Morimoto 	usbhs_write(priv, reg,	UPPHUB(upphub)	|
260eb05191fSKuninori Morimoto 				HUBPORT(hubport)|
261eb05191fSKuninori Morimoto 				USBSPD(usbspd));
262eb05191fSKuninori Morimoto 
263eb05191fSKuninori Morimoto 	return 0;
264eb05191fSKuninori Morimoto }
265eb05191fSKuninori Morimoto 
266eb05191fSKuninori Morimoto /*
267f1407d5cSKuninori Morimoto  *		local functions
268f1407d5cSKuninori Morimoto  */
26911935de5SKuninori Morimoto static void usbhsc_set_buswait(struct usbhs_priv *priv)
270f1407d5cSKuninori Morimoto {
271f1407d5cSKuninori Morimoto 	int wait = usbhs_get_dparam(priv, buswait_bwait);
272f1407d5cSKuninori Morimoto 
273f1407d5cSKuninori Morimoto 	/* set bus wait if platform have */
274f1407d5cSKuninori Morimoto 	if (wait)
275f1407d5cSKuninori Morimoto 		usbhs_bset(priv, BUSWAIT, 0x000F, wait);
276f1407d5cSKuninori Morimoto }
277f1407d5cSKuninori Morimoto 
278f1407d5cSKuninori Morimoto /*
279f1407d5cSKuninori Morimoto  *		platform default param
280f1407d5cSKuninori Morimoto  */
281f1407d5cSKuninori Morimoto static u32 usbhsc_default_pipe_type[] = {
282f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_CONTROL,
283f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_ISOC,
284f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_ISOC,
285f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_BULK,
286f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_BULK,
287f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_BULK,
288f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_INT,
289f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_INT,
290f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_INT,
291f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_INT,
292f1407d5cSKuninori Morimoto };
293f1407d5cSKuninori Morimoto 
294f1407d5cSKuninori Morimoto /*
2956e267da8SKuninori Morimoto  *		power control
2966e267da8SKuninori Morimoto  */
2976e267da8SKuninori Morimoto static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
2986e267da8SKuninori Morimoto {
299f1ee56a0SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
3006e267da8SKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
3016e267da8SKuninori Morimoto 
3026e267da8SKuninori Morimoto 	if (enable) {
3036e267da8SKuninori Morimoto 		/* enable PM */
3046e267da8SKuninori Morimoto 		pm_runtime_get_sync(dev);
3056e267da8SKuninori Morimoto 
306f1ee56a0SKuninori Morimoto 		/* enable platform power */
307f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
308f1ee56a0SKuninori Morimoto 
3096e267da8SKuninori Morimoto 		/* USB on */
3106e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
3116e267da8SKuninori Morimoto 	} else {
3126e267da8SKuninori Morimoto 		/* USB off */
3136e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
3146e267da8SKuninori Morimoto 
315f1ee56a0SKuninori Morimoto 		/* disable platform power */
316f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
317f1ee56a0SKuninori Morimoto 
3186e267da8SKuninori Morimoto 		/* disable PM */
3196e267da8SKuninori Morimoto 		pm_runtime_put_sync(dev);
3206e267da8SKuninori Morimoto 	}
3216e267da8SKuninori Morimoto }
3226e267da8SKuninori Morimoto 
3236e267da8SKuninori Morimoto /*
324ca8a282aSKuninori Morimoto  *		hotplug
325f1407d5cSKuninori Morimoto  */
326ca8a282aSKuninori Morimoto static void usbhsc_hotplug(struct usbhs_priv *priv)
327f1407d5cSKuninori Morimoto {
328f1407d5cSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
329f1407d5cSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
330f1407d5cSKuninori Morimoto 	int id;
331f1407d5cSKuninori Morimoto 	int enable;
332f1407d5cSKuninori Morimoto 	int ret;
333f1407d5cSKuninori Morimoto 
334f1407d5cSKuninori Morimoto 	/*
335f1407d5cSKuninori Morimoto 	 * get vbus status from platform
336f1407d5cSKuninori Morimoto 	 */
337f1407d5cSKuninori Morimoto 	enable = usbhs_platform_call(priv, get_vbus, pdev);
338f1407d5cSKuninori Morimoto 
339f1407d5cSKuninori Morimoto 	/*
340f1407d5cSKuninori Morimoto 	 * get id from platform
341f1407d5cSKuninori Morimoto 	 */
342f1407d5cSKuninori Morimoto 	id = usbhs_platform_call(priv, get_id, pdev);
343f1407d5cSKuninori Morimoto 
344f1407d5cSKuninori Morimoto 	if (enable && !mod) {
345f1407d5cSKuninori Morimoto 		ret = usbhs_mod_change(priv, id);
346f1407d5cSKuninori Morimoto 		if (ret < 0)
347f1407d5cSKuninori Morimoto 			return;
348f1407d5cSKuninori Morimoto 
349f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s enable\n", __func__);
350f1407d5cSKuninori Morimoto 
3516e267da8SKuninori Morimoto 		/* power on */
352b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
3536e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
354f1407d5cSKuninori Morimoto 
355258485d9SKuninori Morimoto 		/* bus init */
356258485d9SKuninori Morimoto 		usbhsc_set_buswait(priv);
357258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
358258485d9SKuninori Morimoto 
359f1407d5cSKuninori Morimoto 		/* module start */
360f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, start, priv);
361f1407d5cSKuninori Morimoto 
362f1407d5cSKuninori Morimoto 	} else if (!enable && mod) {
363f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s disable\n", __func__);
364f1407d5cSKuninori Morimoto 
365f1407d5cSKuninori Morimoto 		/* module stop */
366f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
367f1407d5cSKuninori Morimoto 
368258485d9SKuninori Morimoto 		/* bus init */
369258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
370258485d9SKuninori Morimoto 
3716e267da8SKuninori Morimoto 		/* power off */
372b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
3736e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
374f1407d5cSKuninori Morimoto 
375f1407d5cSKuninori Morimoto 		usbhs_mod_change(priv, -1);
376f1407d5cSKuninori Morimoto 
377f1407d5cSKuninori Morimoto 		/* reset phy for next connection */
378f1407d5cSKuninori Morimoto 		usbhs_platform_call(priv, phy_reset, pdev);
379f1407d5cSKuninori Morimoto 	}
380f1407d5cSKuninori Morimoto }
381f1407d5cSKuninori Morimoto 
382ca8a282aSKuninori Morimoto /*
383ca8a282aSKuninori Morimoto  *		notify hotplug
384ca8a282aSKuninori Morimoto  */
385ca8a282aSKuninori Morimoto static void usbhsc_notify_hotplug(struct work_struct *work)
386ca8a282aSKuninori Morimoto {
387ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = container_of(work,
388ca8a282aSKuninori Morimoto 					       struct usbhs_priv,
389ca8a282aSKuninori Morimoto 					       notify_hotplug_work.work);
390ca8a282aSKuninori Morimoto 	usbhsc_hotplug(priv);
391ca8a282aSKuninori Morimoto }
392ca8a282aSKuninori Morimoto 
393b4fcea2aSKuninori Morimoto static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
394f1407d5cSKuninori Morimoto {
395206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
396bc57381eSKuninori Morimoto 	int delay = usbhs_get_dparam(priv, detection_delay);
397f1407d5cSKuninori Morimoto 
398f1407d5cSKuninori Morimoto 	/*
399f1407d5cSKuninori Morimoto 	 * This functions will be called in interrupt.
400f1407d5cSKuninori Morimoto 	 * To make sure safety context,
401f1407d5cSKuninori Morimoto 	 * use workqueue for usbhs_notify_hotplug
402f1407d5cSKuninori Morimoto 	 */
403a49a88f1SKuninori Morimoto 	schedule_delayed_work(&priv->notify_hotplug_work,
404a49a88f1SKuninori Morimoto 			      msecs_to_jiffies(delay));
405f1407d5cSKuninori Morimoto 	return 0;
406f1407d5cSKuninori Morimoto }
407f1407d5cSKuninori Morimoto 
408f1407d5cSKuninori Morimoto /*
409f1407d5cSKuninori Morimoto  *		platform functions
410f1407d5cSKuninori Morimoto  */
411b7a8d17dSKuninori Morimoto static int usbhs_probe(struct platform_device *pdev)
412f1407d5cSKuninori Morimoto {
413f1407d5cSKuninori Morimoto 	struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
414f1407d5cSKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc;
415f1407d5cSKuninori Morimoto 	struct usbhs_priv *priv;
41653069af3SShimoda, Yoshihiro 	struct resource *res, *irq_res;
417f1407d5cSKuninori Morimoto 	int ret;
418f1407d5cSKuninori Morimoto 
419f1407d5cSKuninori Morimoto 	/* check platform information */
420f1407d5cSKuninori Morimoto 	if (!info ||
421b002ff6eSKuninori Morimoto 	    !info->platform_callback.get_id) {
422f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "no platform information\n");
423f1407d5cSKuninori Morimoto 		return -EINVAL;
424f1407d5cSKuninori Morimoto 	}
425f1407d5cSKuninori Morimoto 
426f1407d5cSKuninori Morimoto 	/* platform data */
427f1407d5cSKuninori Morimoto 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
42853069af3SShimoda, Yoshihiro 	irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
42953069af3SShimoda, Yoshihiro 	if (!res || !irq_res) {
430f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
431f1407d5cSKuninori Morimoto 		return -ENODEV;
432f1407d5cSKuninori Morimoto 	}
433f1407d5cSKuninori Morimoto 
434f1407d5cSKuninori Morimoto 	/* usb private data */
435*58efc77cSKuninori Morimoto 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
436f1407d5cSKuninori Morimoto 	if (!priv) {
437f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "Could not allocate priv\n");
438f1407d5cSKuninori Morimoto 		return -ENOMEM;
439f1407d5cSKuninori Morimoto 	}
440f1407d5cSKuninori Morimoto 
441*58efc77cSKuninori Morimoto 	priv->base = devm_request_and_ioremap(&pdev->dev, res);
442f1407d5cSKuninori Morimoto 	if (!priv->base) {
443f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "ioremap error.\n");
444*58efc77cSKuninori Morimoto 		return -ENOMEM;
445f1407d5cSKuninori Morimoto 	}
446f1407d5cSKuninori Morimoto 
447f1407d5cSKuninori Morimoto 	/*
448f1407d5cSKuninori Morimoto 	 * care platform info
449f1407d5cSKuninori Morimoto 	 */
45048298206SKuninori Morimoto 	memcpy(&priv->pfunc,
45148298206SKuninori Morimoto 	       &info->platform_callback,
45248298206SKuninori Morimoto 	       sizeof(struct renesas_usbhs_platform_callback));
45348298206SKuninori Morimoto 	memcpy(&priv->dparam,
45448298206SKuninori Morimoto 	       &info->driver_param,
45548298206SKuninori Morimoto 	       sizeof(struct renesas_usbhs_driver_param));
456f1407d5cSKuninori Morimoto 
457f1407d5cSKuninori Morimoto 	/* set driver callback functions for platform */
458f1407d5cSKuninori Morimoto 	dfunc			= &info->driver_callback;
459f1407d5cSKuninori Morimoto 	dfunc->notify_hotplug	= usbhsc_drvcllbck_notify_hotplug;
460f1407d5cSKuninori Morimoto 
461f1407d5cSKuninori Morimoto 	/* set default param if platform doesn't have */
46248298206SKuninori Morimoto 	if (!priv->dparam.pipe_type) {
46348298206SKuninori Morimoto 		priv->dparam.pipe_type = usbhsc_default_pipe_type;
46448298206SKuninori Morimoto 		priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe_type);
465f1407d5cSKuninori Morimoto 	}
46648298206SKuninori Morimoto 	if (!priv->dparam.pio_dma_border)
46748298206SKuninori Morimoto 		priv->dparam.pio_dma_border = 64; /* 64byte */
468f1407d5cSKuninori Morimoto 
469b002ff6eSKuninori Morimoto 	/* FIXME */
470b002ff6eSKuninori Morimoto 	/* runtime power control ? */
47148298206SKuninori Morimoto 	if (priv->pfunc.get_vbus)
472b002ff6eSKuninori Morimoto 		usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
473b002ff6eSKuninori Morimoto 
474f1407d5cSKuninori Morimoto 	/*
475f1407d5cSKuninori Morimoto 	 * priv settings
476f1407d5cSKuninori Morimoto 	 */
47753069af3SShimoda, Yoshihiro 	priv->irq	= irq_res->start;
47853069af3SShimoda, Yoshihiro 	if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
47953069af3SShimoda, Yoshihiro 		priv->irqflags = IRQF_SHARED;
480f1407d5cSKuninori Morimoto 	priv->pdev	= pdev;
481bc57381eSKuninori Morimoto 	INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
482f1407d5cSKuninori Morimoto 	spin_lock_init(usbhs_priv_to_lock(priv));
483f1407d5cSKuninori Morimoto 
484f1407d5cSKuninori Morimoto 	/* call pipe and module init */
485f1407d5cSKuninori Morimoto 	ret = usbhs_pipe_probe(priv);
486f1407d5cSKuninori Morimoto 	if (ret < 0)
487*58efc77cSKuninori Morimoto 		return ret;
488f1407d5cSKuninori Morimoto 
489d3af90a5SKuninori Morimoto 	ret = usbhs_fifo_probe(priv);
490f1407d5cSKuninori Morimoto 	if (ret < 0)
49197f93227SKuninori Morimoto 		goto probe_end_pipe_exit;
492f1407d5cSKuninori Morimoto 
493d3af90a5SKuninori Morimoto 	ret = usbhs_mod_probe(priv);
494d3af90a5SKuninori Morimoto 	if (ret < 0)
495d3af90a5SKuninori Morimoto 		goto probe_end_fifo_exit;
496d3af90a5SKuninori Morimoto 
497f1407d5cSKuninori Morimoto 	/* dev_set_drvdata should be called after usbhs_mod_init */
498f1407d5cSKuninori Morimoto 	dev_set_drvdata(&pdev->dev, priv);
499f1407d5cSKuninori Morimoto 
500f1407d5cSKuninori Morimoto 	/*
501f1407d5cSKuninori Morimoto 	 * deviece reset here because
502f1407d5cSKuninori Morimoto 	 * USB device might be used in boot loader.
503f1407d5cSKuninori Morimoto 	 */
504f1407d5cSKuninori Morimoto 	usbhs_sys_clock_ctrl(priv, 0);
505f1407d5cSKuninori Morimoto 
506f1407d5cSKuninori Morimoto 	/*
507f1407d5cSKuninori Morimoto 	 * platform call
508f1407d5cSKuninori Morimoto 	 *
509f1407d5cSKuninori Morimoto 	 * USB phy setup might depend on CPU/Board.
510f1407d5cSKuninori Morimoto 	 * If platform has its callback functions,
511f1407d5cSKuninori Morimoto 	 * call it here.
512f1407d5cSKuninori Morimoto 	 */
513f1407d5cSKuninori Morimoto 	ret = usbhs_platform_call(priv, hardware_init, pdev);
514f1407d5cSKuninori Morimoto 	if (ret < 0) {
515f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "platform prove failed.\n");
51697f93227SKuninori Morimoto 		goto probe_end_mod_exit;
517f1407d5cSKuninori Morimoto 	}
518f1407d5cSKuninori Morimoto 
519f1407d5cSKuninori Morimoto 	/* reset phy for connection */
520f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
521f1407d5cSKuninori Morimoto 
522b002ff6eSKuninori Morimoto 	/* power control */
523b002ff6eSKuninori Morimoto 	pm_runtime_enable(&pdev->dev);
524b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
525b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
526b002ff6eSKuninori Morimoto 		usbhs_mod_autonomy_mode(priv);
527b002ff6eSKuninori Morimoto 	}
528b002ff6eSKuninori Morimoto 
529f1407d5cSKuninori Morimoto 	/*
530f1407d5cSKuninori Morimoto 	 * manual call notify_hotplug for cold plug
531f1407d5cSKuninori Morimoto 	 */
532f1407d5cSKuninori Morimoto 	ret = usbhsc_drvcllbck_notify_hotplug(pdev);
533f1407d5cSKuninori Morimoto 	if (ret < 0)
534f1407d5cSKuninori Morimoto 		goto probe_end_call_remove;
535f1407d5cSKuninori Morimoto 
536f1407d5cSKuninori Morimoto 	dev_info(&pdev->dev, "probed\n");
537f1407d5cSKuninori Morimoto 
538f1407d5cSKuninori Morimoto 	return ret;
539f1407d5cSKuninori Morimoto 
540f1407d5cSKuninori Morimoto probe_end_call_remove:
541f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, hardware_exit, pdev);
542f1407d5cSKuninori Morimoto probe_end_mod_exit:
543f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
544d3af90a5SKuninori Morimoto probe_end_fifo_exit:
545d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
54697f93227SKuninori Morimoto probe_end_pipe_exit:
54797f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
548f1407d5cSKuninori Morimoto 
549f1407d5cSKuninori Morimoto 	dev_info(&pdev->dev, "probe failed\n");
550f1407d5cSKuninori Morimoto 
551f1407d5cSKuninori Morimoto 	return ret;
552f1407d5cSKuninori Morimoto }
553f1407d5cSKuninori Morimoto 
554f1407d5cSKuninori Morimoto static int __devexit usbhs_remove(struct platform_device *pdev)
555f1407d5cSKuninori Morimoto {
556206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
557af32fe51SKuninori Morimoto 	struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
558af32fe51SKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
559f1407d5cSKuninori Morimoto 
560f1407d5cSKuninori Morimoto 	dev_dbg(&pdev->dev, "usb remove\n");
561f1407d5cSKuninori Morimoto 
562af32fe51SKuninori Morimoto 	dfunc->notify_hotplug = NULL;
563af32fe51SKuninori Morimoto 
564b002ff6eSKuninori Morimoto 	/* power off */
565b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
566b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
567f1407d5cSKuninori Morimoto 
568b002ff6eSKuninori Morimoto 	pm_runtime_disable(&pdev->dev);
569f1407d5cSKuninori Morimoto 
570f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, hardware_exit, pdev);
571f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
572d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
57397f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
574f1407d5cSKuninori Morimoto 
575f1407d5cSKuninori Morimoto 	return 0;
576f1407d5cSKuninori Morimoto }
577f1407d5cSKuninori Morimoto 
578ca8a282aSKuninori Morimoto static int usbhsc_suspend(struct device *dev)
579ca8a282aSKuninori Morimoto {
580ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
581ca8a282aSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
582ca8a282aSKuninori Morimoto 
583ca8a282aSKuninori Morimoto 	if (mod) {
584ca8a282aSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
585ca8a282aSKuninori Morimoto 		usbhs_mod_change(priv, -1);
586ca8a282aSKuninori Morimoto 	}
587ca8a282aSKuninori Morimoto 
588ca8a282aSKuninori Morimoto 	if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
589ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
590ca8a282aSKuninori Morimoto 
591ca8a282aSKuninori Morimoto 	return 0;
592ca8a282aSKuninori Morimoto }
593ca8a282aSKuninori Morimoto 
594ca8a282aSKuninori Morimoto static int usbhsc_resume(struct device *dev)
595ca8a282aSKuninori Morimoto {
596ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
597ca8a282aSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
598ca8a282aSKuninori Morimoto 
599ca8a282aSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
600ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
601ca8a282aSKuninori Morimoto 
6025b50d3b5SKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
6035b50d3b5SKuninori Morimoto 
6045b50d3b5SKuninori Morimoto 	usbhsc_drvcllbck_notify_hotplug(pdev);
605ca8a282aSKuninori Morimoto 
606ca8a282aSKuninori Morimoto 	return 0;
607ca8a282aSKuninori Morimoto }
608ca8a282aSKuninori Morimoto 
609ca8a282aSKuninori Morimoto static int usbhsc_runtime_nop(struct device *dev)
610ca8a282aSKuninori Morimoto {
611ca8a282aSKuninori Morimoto 	/* Runtime PM callback shared between ->runtime_suspend()
612ca8a282aSKuninori Morimoto 	 * and ->runtime_resume(). Simply returns success.
613ca8a282aSKuninori Morimoto 	 *
614ca8a282aSKuninori Morimoto 	 * This driver re-initializes all registers after
615ca8a282aSKuninori Morimoto 	 * pm_runtime_get_sync() anyway so there is no need
616ca8a282aSKuninori Morimoto 	 * to save and restore registers here.
617ca8a282aSKuninori Morimoto 	 */
618ca8a282aSKuninori Morimoto 	return 0;
619ca8a282aSKuninori Morimoto }
620ca8a282aSKuninori Morimoto 
621ca8a282aSKuninori Morimoto static const struct dev_pm_ops usbhsc_pm_ops = {
622ca8a282aSKuninori Morimoto 	.suspend		= usbhsc_suspend,
623ca8a282aSKuninori Morimoto 	.resume			= usbhsc_resume,
624ca8a282aSKuninori Morimoto 	.runtime_suspend	= usbhsc_runtime_nop,
625ca8a282aSKuninori Morimoto 	.runtime_resume		= usbhsc_runtime_nop,
626ca8a282aSKuninori Morimoto };
627ca8a282aSKuninori Morimoto 
628f1407d5cSKuninori Morimoto static struct platform_driver renesas_usbhs_driver = {
629f1407d5cSKuninori Morimoto 	.driver		= {
630f1407d5cSKuninori Morimoto 		.name	= "renesas_usbhs",
631ca8a282aSKuninori Morimoto 		.pm	= &usbhsc_pm_ops,
632f1407d5cSKuninori Morimoto 	},
633f1407d5cSKuninori Morimoto 	.probe		= usbhs_probe,
634f1407d5cSKuninori Morimoto 	.remove		= __devexit_p(usbhs_remove),
635f1407d5cSKuninori Morimoto };
636f1407d5cSKuninori Morimoto 
637cc27c96cSAxel Lin module_platform_driver(renesas_usbhs_driver);
638f1407d5cSKuninori Morimoto 
639f1407d5cSKuninori Morimoto MODULE_LICENSE("GPL");
640f1407d5cSKuninori Morimoto MODULE_DESCRIPTION("Renesas USB driver");
641f1407d5cSKuninori Morimoto MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
642