xref: /openbmc/linux/drivers/usb/renesas_usbhs/common.c (revision 76190152fb62650ea6530c166d9adbaa08cdb5d0)
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>
22f1407d5cSKuninori Morimoto #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  */
98*76190152SKuninori 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 
135f1407d5cSKuninori Morimoto /*
136f1407d5cSKuninori Morimoto  *		frame functions
137f1407d5cSKuninori Morimoto  */
138f1407d5cSKuninori Morimoto int usbhs_frame_get_num(struct usbhs_priv *priv)
139f1407d5cSKuninori Morimoto {
140f1407d5cSKuninori Morimoto 	return usbhs_read(priv, FRMNUM) & FRNM_MASK;
141f1407d5cSKuninori Morimoto }
142f1407d5cSKuninori Morimoto 
143f1407d5cSKuninori Morimoto /*
144ef8bedb9SKuninori Morimoto  *		usb request functions
145ef8bedb9SKuninori Morimoto  */
146ef8bedb9SKuninori Morimoto void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
147ef8bedb9SKuninori Morimoto {
148ef8bedb9SKuninori Morimoto 	u16 val;
149ef8bedb9SKuninori Morimoto 
150ef8bedb9SKuninori Morimoto 	val = usbhs_read(priv, USBREQ);
151ef8bedb9SKuninori Morimoto 	req->bRequest		= (val >> 8) & 0xFF;
152ef8bedb9SKuninori Morimoto 	req->bRequestType	= (val >> 0) & 0xFF;
153ef8bedb9SKuninori Morimoto 
154ef8bedb9SKuninori Morimoto 	req->wValue	= usbhs_read(priv, USBVAL);
155ef8bedb9SKuninori Morimoto 	req->wIndex	= usbhs_read(priv, USBINDX);
156ef8bedb9SKuninori Morimoto 	req->wLength	= usbhs_read(priv, USBLENG);
157ef8bedb9SKuninori Morimoto }
158ef8bedb9SKuninori Morimoto 
159ef8bedb9SKuninori Morimoto void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
160ef8bedb9SKuninori Morimoto {
161ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBREQ,  (req->bRequest << 8) | req->bRequestType);
162ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBVAL,  req->wValue);
163ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBINDX, req->wIndex);
164ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBLENG, req->wLength);
165ef8bedb9SKuninori Morimoto 
166ef8bedb9SKuninori Morimoto 	usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
167ef8bedb9SKuninori Morimoto }
168ef8bedb9SKuninori Morimoto 
169ef8bedb9SKuninori Morimoto /*
170258485d9SKuninori Morimoto  *		bus/vbus functions
171258485d9SKuninori Morimoto  */
172258485d9SKuninori Morimoto void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
173258485d9SKuninori Morimoto {
174a9be4a45SKuninori Morimoto 	u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
175a9be4a45SKuninori Morimoto 
176a9be4a45SKuninori Morimoto 	if (status != USBRST) {
177a9be4a45SKuninori Morimoto 		struct device *dev = usbhs_priv_to_dev(priv);
178a9be4a45SKuninori Morimoto 		dev_err(dev, "usbhs should be reset\n");
179a9be4a45SKuninori Morimoto 	}
180a9be4a45SKuninori Morimoto 
181258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
182258485d9SKuninori Morimoto }
183258485d9SKuninori Morimoto 
184258485d9SKuninori Morimoto void usbhs_bus_send_reset(struct usbhs_priv *priv)
185258485d9SKuninori Morimoto {
186258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
187258485d9SKuninori Morimoto }
188258485d9SKuninori Morimoto 
18975587f52SKuninori Morimoto int usbhs_bus_get_speed(struct usbhs_priv *priv)
19075587f52SKuninori Morimoto {
19175587f52SKuninori Morimoto 	u16 dvstctr = usbhs_read(priv, DVSTCTR);
19275587f52SKuninori Morimoto 
19375587f52SKuninori Morimoto 	switch (RHST & dvstctr) {
19475587f52SKuninori Morimoto 	case RHST_LOW_SPEED:
19575587f52SKuninori Morimoto 		return USB_SPEED_LOW;
19675587f52SKuninori Morimoto 	case RHST_FULL_SPEED:
19775587f52SKuninori Morimoto 		return USB_SPEED_FULL;
19875587f52SKuninori Morimoto 	case RHST_HIGH_SPEED:
19975587f52SKuninori Morimoto 		return USB_SPEED_HIGH;
20075587f52SKuninori Morimoto 	}
20175587f52SKuninori Morimoto 
20275587f52SKuninori Morimoto 	return USB_SPEED_UNKNOWN;
20375587f52SKuninori Morimoto }
20475587f52SKuninori Morimoto 
205258485d9SKuninori Morimoto int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
206258485d9SKuninori Morimoto {
207258485d9SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
208258485d9SKuninori Morimoto 
209258485d9SKuninori Morimoto 	return usbhs_platform_call(priv, set_vbus, pdev, enable);
210258485d9SKuninori Morimoto }
211258485d9SKuninori Morimoto 
212258485d9SKuninori Morimoto static void usbhsc_bus_init(struct usbhs_priv *priv)
213258485d9SKuninori Morimoto {
214258485d9SKuninori Morimoto 	usbhs_write(priv, DVSTCTR, 0);
215258485d9SKuninori Morimoto 
216258485d9SKuninori Morimoto 	usbhs_vbus_ctrl(priv, 0);
217258485d9SKuninori Morimoto }
218258485d9SKuninori Morimoto 
219258485d9SKuninori Morimoto /*
220eb05191fSKuninori Morimoto  *		device configuration
221eb05191fSKuninori Morimoto  */
222eb05191fSKuninori Morimoto int usbhs_set_device_speed(struct usbhs_priv *priv, int devnum,
223eb05191fSKuninori Morimoto 			   u16 upphub, u16 hubport, u16 speed)
224eb05191fSKuninori Morimoto {
225eb05191fSKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
226eb05191fSKuninori Morimoto 	u16 usbspd = 0;
227eb05191fSKuninori Morimoto 	u32 reg = DEVADD0 + (2 * devnum);
228eb05191fSKuninori Morimoto 
229eb05191fSKuninori Morimoto 	if (devnum > 10) {
230eb05191fSKuninori Morimoto 		dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
231eb05191fSKuninori Morimoto 		return -EIO;
232eb05191fSKuninori Morimoto 	}
233eb05191fSKuninori Morimoto 
234eb05191fSKuninori Morimoto 	if (upphub > 0xA) {
235eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported hub number %d\n", upphub);
236eb05191fSKuninori Morimoto 		return -EIO;
237eb05191fSKuninori Morimoto 	}
238eb05191fSKuninori Morimoto 
239eb05191fSKuninori Morimoto 	switch (speed) {
240eb05191fSKuninori Morimoto 	case USB_SPEED_LOW:
241eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_LOW;
242eb05191fSKuninori Morimoto 		break;
243eb05191fSKuninori Morimoto 	case USB_SPEED_FULL:
244eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_FULL;
245eb05191fSKuninori Morimoto 		break;
246eb05191fSKuninori Morimoto 	case USB_SPEED_HIGH:
247eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_HIGH;
248eb05191fSKuninori Morimoto 		break;
249eb05191fSKuninori Morimoto 	default:
250eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported speed %d\n", speed);
251eb05191fSKuninori Morimoto 		return -EIO;
252eb05191fSKuninori Morimoto 	}
253eb05191fSKuninori Morimoto 
254eb05191fSKuninori Morimoto 	usbhs_write(priv, reg,	UPPHUB(upphub)	|
255eb05191fSKuninori Morimoto 				HUBPORT(hubport)|
256eb05191fSKuninori Morimoto 				USBSPD(usbspd));
257eb05191fSKuninori Morimoto 
258eb05191fSKuninori Morimoto 	return 0;
259eb05191fSKuninori Morimoto }
260eb05191fSKuninori Morimoto 
261eb05191fSKuninori Morimoto /*
262f1407d5cSKuninori Morimoto  *		local functions
263f1407d5cSKuninori Morimoto  */
26411935de5SKuninori Morimoto static void usbhsc_set_buswait(struct usbhs_priv *priv)
265f1407d5cSKuninori Morimoto {
266f1407d5cSKuninori Morimoto 	int wait = usbhs_get_dparam(priv, buswait_bwait);
267f1407d5cSKuninori Morimoto 
268f1407d5cSKuninori Morimoto 	/* set bus wait if platform have */
269f1407d5cSKuninori Morimoto 	if (wait)
270f1407d5cSKuninori Morimoto 		usbhs_bset(priv, BUSWAIT, 0x000F, wait);
271f1407d5cSKuninori Morimoto }
272f1407d5cSKuninori Morimoto 
273f1407d5cSKuninori Morimoto /*
274f1407d5cSKuninori Morimoto  *		platform default param
275f1407d5cSKuninori Morimoto  */
276f1407d5cSKuninori Morimoto static u32 usbhsc_default_pipe_type[] = {
277f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_CONTROL,
278f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_ISOC,
279f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_ISOC,
280f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_BULK,
281f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_BULK,
282f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_BULK,
283f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_INT,
284f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_INT,
285f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_INT,
286f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_INT,
287f1407d5cSKuninori Morimoto };
288f1407d5cSKuninori Morimoto 
289f1407d5cSKuninori Morimoto /*
2906e267da8SKuninori Morimoto  *		power control
2916e267da8SKuninori Morimoto  */
2926e267da8SKuninori Morimoto static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
2936e267da8SKuninori Morimoto {
2946e267da8SKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
2956e267da8SKuninori Morimoto 
2966e267da8SKuninori Morimoto 	if (enable) {
2976e267da8SKuninori Morimoto 		/* enable PM */
2986e267da8SKuninori Morimoto 		pm_runtime_get_sync(dev);
2996e267da8SKuninori Morimoto 
3006e267da8SKuninori Morimoto 		/* USB on */
3016e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
3026e267da8SKuninori Morimoto 	} else {
3036e267da8SKuninori Morimoto 		/* USB off */
3046e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
3056e267da8SKuninori Morimoto 
3066e267da8SKuninori Morimoto 		/* disable PM */
3076e267da8SKuninori Morimoto 		pm_runtime_put_sync(dev);
3086e267da8SKuninori Morimoto 	}
3096e267da8SKuninori Morimoto }
3106e267da8SKuninori Morimoto 
3116e267da8SKuninori Morimoto /*
312ca8a282aSKuninori Morimoto  *		hotplug
313f1407d5cSKuninori Morimoto  */
314ca8a282aSKuninori Morimoto static void usbhsc_hotplug(struct usbhs_priv *priv)
315f1407d5cSKuninori Morimoto {
316f1407d5cSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
317f1407d5cSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
318f1407d5cSKuninori Morimoto 	int id;
319f1407d5cSKuninori Morimoto 	int enable;
320f1407d5cSKuninori Morimoto 	int ret;
321f1407d5cSKuninori Morimoto 
322f1407d5cSKuninori Morimoto 	/*
323f1407d5cSKuninori Morimoto 	 * get vbus status from platform
324f1407d5cSKuninori Morimoto 	 */
325f1407d5cSKuninori Morimoto 	enable = usbhs_platform_call(priv, get_vbus, pdev);
326f1407d5cSKuninori Morimoto 
327f1407d5cSKuninori Morimoto 	/*
328f1407d5cSKuninori Morimoto 	 * get id from platform
329f1407d5cSKuninori Morimoto 	 */
330f1407d5cSKuninori Morimoto 	id = usbhs_platform_call(priv, get_id, pdev);
331f1407d5cSKuninori Morimoto 
332f1407d5cSKuninori Morimoto 	if (enable && !mod) {
333f1407d5cSKuninori Morimoto 		ret = usbhs_mod_change(priv, id);
334f1407d5cSKuninori Morimoto 		if (ret < 0)
335f1407d5cSKuninori Morimoto 			return;
336f1407d5cSKuninori Morimoto 
337f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s enable\n", __func__);
338f1407d5cSKuninori Morimoto 
3396e267da8SKuninori Morimoto 		/* power on */
340b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
3416e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
342f1407d5cSKuninori Morimoto 
343258485d9SKuninori Morimoto 		/* bus init */
344258485d9SKuninori Morimoto 		usbhsc_set_buswait(priv);
345258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
346258485d9SKuninori Morimoto 
347f1407d5cSKuninori Morimoto 		/* module start */
348f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, start, priv);
349f1407d5cSKuninori Morimoto 
350f1407d5cSKuninori Morimoto 	} else if (!enable && mod) {
351f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s disable\n", __func__);
352f1407d5cSKuninori Morimoto 
353f1407d5cSKuninori Morimoto 		/* module stop */
354f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
355f1407d5cSKuninori Morimoto 
356258485d9SKuninori Morimoto 		/* bus init */
357258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
358258485d9SKuninori Morimoto 
3596e267da8SKuninori Morimoto 		/* power off */
360b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
3616e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
362f1407d5cSKuninori Morimoto 
363f1407d5cSKuninori Morimoto 		usbhs_mod_change(priv, -1);
364f1407d5cSKuninori Morimoto 
365f1407d5cSKuninori Morimoto 		/* reset phy for next connection */
366f1407d5cSKuninori Morimoto 		usbhs_platform_call(priv, phy_reset, pdev);
367f1407d5cSKuninori Morimoto 	}
368f1407d5cSKuninori Morimoto }
369f1407d5cSKuninori Morimoto 
370ca8a282aSKuninori Morimoto /*
371ca8a282aSKuninori Morimoto  *		notify hotplug
372ca8a282aSKuninori Morimoto  */
373ca8a282aSKuninori Morimoto static void usbhsc_notify_hotplug(struct work_struct *work)
374ca8a282aSKuninori Morimoto {
375ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = container_of(work,
376ca8a282aSKuninori Morimoto 					       struct usbhs_priv,
377ca8a282aSKuninori Morimoto 					       notify_hotplug_work.work);
378ca8a282aSKuninori Morimoto 	usbhsc_hotplug(priv);
379ca8a282aSKuninori Morimoto }
380ca8a282aSKuninori Morimoto 
381bc57381eSKuninori Morimoto int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
382f1407d5cSKuninori Morimoto {
383206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
384bc57381eSKuninori Morimoto 	int delay = usbhs_get_dparam(priv, detection_delay);
385f1407d5cSKuninori Morimoto 
386f1407d5cSKuninori Morimoto 	/*
387f1407d5cSKuninori Morimoto 	 * This functions will be called in interrupt.
388f1407d5cSKuninori Morimoto 	 * To make sure safety context,
389f1407d5cSKuninori Morimoto 	 * use workqueue for usbhs_notify_hotplug
390f1407d5cSKuninori Morimoto 	 */
391bc57381eSKuninori Morimoto 	schedule_delayed_work(&priv->notify_hotplug_work, delay);
392f1407d5cSKuninori Morimoto 	return 0;
393f1407d5cSKuninori Morimoto }
394f1407d5cSKuninori Morimoto 
395f1407d5cSKuninori Morimoto /*
396f1407d5cSKuninori Morimoto  *		platform functions
397f1407d5cSKuninori Morimoto  */
398b7a8d17dSKuninori Morimoto static int usbhs_probe(struct platform_device *pdev)
399f1407d5cSKuninori Morimoto {
400f1407d5cSKuninori Morimoto 	struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
401f1407d5cSKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc;
402f1407d5cSKuninori Morimoto 	struct usbhs_priv *priv;
403f1407d5cSKuninori Morimoto 	struct resource *res;
404f1407d5cSKuninori Morimoto 	unsigned int irq;
405f1407d5cSKuninori Morimoto 	int ret;
406f1407d5cSKuninori Morimoto 
407f1407d5cSKuninori Morimoto 	/* check platform information */
408f1407d5cSKuninori Morimoto 	if (!info ||
409b002ff6eSKuninori Morimoto 	    !info->platform_callback.get_id) {
410f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "no platform information\n");
411f1407d5cSKuninori Morimoto 		return -EINVAL;
412f1407d5cSKuninori Morimoto 	}
413f1407d5cSKuninori Morimoto 
414f1407d5cSKuninori Morimoto 	/* platform data */
415f1407d5cSKuninori Morimoto 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
416f1407d5cSKuninori Morimoto 	irq = platform_get_irq(pdev, 0);
417f1407d5cSKuninori Morimoto 	if (!res || (int)irq <= 0) {
418f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
419f1407d5cSKuninori Morimoto 		return -ENODEV;
420f1407d5cSKuninori Morimoto 	}
421f1407d5cSKuninori Morimoto 
422f1407d5cSKuninori Morimoto 	/* usb private data */
423f1407d5cSKuninori Morimoto 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
424f1407d5cSKuninori Morimoto 	if (!priv) {
425f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "Could not allocate priv\n");
426f1407d5cSKuninori Morimoto 		return -ENOMEM;
427f1407d5cSKuninori Morimoto 	}
428f1407d5cSKuninori Morimoto 
429f1407d5cSKuninori Morimoto 	priv->base = ioremap_nocache(res->start, resource_size(res));
430f1407d5cSKuninori Morimoto 	if (!priv->base) {
431f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "ioremap error.\n");
432f1407d5cSKuninori Morimoto 		ret = -ENOMEM;
433f1407d5cSKuninori Morimoto 		goto probe_end_kfree;
434f1407d5cSKuninori Morimoto 	}
435f1407d5cSKuninori Morimoto 
436f1407d5cSKuninori Morimoto 	/*
437f1407d5cSKuninori Morimoto 	 * care platform info
438f1407d5cSKuninori Morimoto 	 */
43948298206SKuninori Morimoto 	memcpy(&priv->pfunc,
44048298206SKuninori Morimoto 	       &info->platform_callback,
44148298206SKuninori Morimoto 	       sizeof(struct renesas_usbhs_platform_callback));
44248298206SKuninori Morimoto 	memcpy(&priv->dparam,
44348298206SKuninori Morimoto 	       &info->driver_param,
44448298206SKuninori Morimoto 	       sizeof(struct renesas_usbhs_driver_param));
445f1407d5cSKuninori Morimoto 
446f1407d5cSKuninori Morimoto 	/* set driver callback functions for platform */
447f1407d5cSKuninori Morimoto 	dfunc			= &info->driver_callback;
448f1407d5cSKuninori Morimoto 	dfunc->notify_hotplug	= usbhsc_drvcllbck_notify_hotplug;
449f1407d5cSKuninori Morimoto 
450f1407d5cSKuninori Morimoto 	/* set default param if platform doesn't have */
45148298206SKuninori Morimoto 	if (!priv->dparam.pipe_type) {
45248298206SKuninori Morimoto 		priv->dparam.pipe_type = usbhsc_default_pipe_type;
45348298206SKuninori Morimoto 		priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe_type);
454f1407d5cSKuninori Morimoto 	}
45548298206SKuninori Morimoto 	if (!priv->dparam.pio_dma_border)
45648298206SKuninori Morimoto 		priv->dparam.pio_dma_border = 64; /* 64byte */
457f1407d5cSKuninori Morimoto 
458b002ff6eSKuninori Morimoto 	/* FIXME */
459b002ff6eSKuninori Morimoto 	/* runtime power control ? */
46048298206SKuninori Morimoto 	if (priv->pfunc.get_vbus)
461b002ff6eSKuninori Morimoto 		usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
462b002ff6eSKuninori Morimoto 
463f1407d5cSKuninori Morimoto 	/*
464f1407d5cSKuninori Morimoto 	 * priv settings
465f1407d5cSKuninori Morimoto 	 */
466f1407d5cSKuninori Morimoto 	priv->irq	= irq;
467f1407d5cSKuninori Morimoto 	priv->pdev	= pdev;
468bc57381eSKuninori Morimoto 	INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
469f1407d5cSKuninori Morimoto 	spin_lock_init(usbhs_priv_to_lock(priv));
470f1407d5cSKuninori Morimoto 
471f1407d5cSKuninori Morimoto 	/* call pipe and module init */
472f1407d5cSKuninori Morimoto 	ret = usbhs_pipe_probe(priv);
473f1407d5cSKuninori Morimoto 	if (ret < 0)
47497f93227SKuninori Morimoto 		goto probe_end_iounmap;
475f1407d5cSKuninori Morimoto 
476d3af90a5SKuninori Morimoto 	ret = usbhs_fifo_probe(priv);
477f1407d5cSKuninori Morimoto 	if (ret < 0)
47897f93227SKuninori Morimoto 		goto probe_end_pipe_exit;
479f1407d5cSKuninori Morimoto 
480d3af90a5SKuninori Morimoto 	ret = usbhs_mod_probe(priv);
481d3af90a5SKuninori Morimoto 	if (ret < 0)
482d3af90a5SKuninori Morimoto 		goto probe_end_fifo_exit;
483d3af90a5SKuninori Morimoto 
484f1407d5cSKuninori Morimoto 	/* dev_set_drvdata should be called after usbhs_mod_init */
485f1407d5cSKuninori Morimoto 	dev_set_drvdata(&pdev->dev, priv);
486f1407d5cSKuninori Morimoto 
487f1407d5cSKuninori Morimoto 	/*
488f1407d5cSKuninori Morimoto 	 * deviece reset here because
489f1407d5cSKuninori Morimoto 	 * USB device might be used in boot loader.
490f1407d5cSKuninori Morimoto 	 */
491f1407d5cSKuninori Morimoto 	usbhs_sys_clock_ctrl(priv, 0);
492f1407d5cSKuninori Morimoto 
493f1407d5cSKuninori Morimoto 	/*
494f1407d5cSKuninori Morimoto 	 * platform call
495f1407d5cSKuninori Morimoto 	 *
496f1407d5cSKuninori Morimoto 	 * USB phy setup might depend on CPU/Board.
497f1407d5cSKuninori Morimoto 	 * If platform has its callback functions,
498f1407d5cSKuninori Morimoto 	 * call it here.
499f1407d5cSKuninori Morimoto 	 */
500f1407d5cSKuninori Morimoto 	ret = usbhs_platform_call(priv, hardware_init, pdev);
501f1407d5cSKuninori Morimoto 	if (ret < 0) {
502f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "platform prove failed.\n");
50397f93227SKuninori Morimoto 		goto probe_end_mod_exit;
504f1407d5cSKuninori Morimoto 	}
505f1407d5cSKuninori Morimoto 
506f1407d5cSKuninori Morimoto 	/* reset phy for connection */
507f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
508f1407d5cSKuninori Morimoto 
509b002ff6eSKuninori Morimoto 	/* power control */
510b002ff6eSKuninori Morimoto 	pm_runtime_enable(&pdev->dev);
511b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
512b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
513b002ff6eSKuninori Morimoto 		usbhs_mod_autonomy_mode(priv);
514b002ff6eSKuninori Morimoto 	}
515b002ff6eSKuninori Morimoto 
516f1407d5cSKuninori Morimoto 	/*
517f1407d5cSKuninori Morimoto 	 * manual call notify_hotplug for cold plug
518f1407d5cSKuninori Morimoto 	 */
519f1407d5cSKuninori Morimoto 	ret = usbhsc_drvcllbck_notify_hotplug(pdev);
520f1407d5cSKuninori Morimoto 	if (ret < 0)
521f1407d5cSKuninori Morimoto 		goto probe_end_call_remove;
522f1407d5cSKuninori Morimoto 
523f1407d5cSKuninori Morimoto 	dev_info(&pdev->dev, "probed\n");
524f1407d5cSKuninori Morimoto 
525f1407d5cSKuninori Morimoto 	return ret;
526f1407d5cSKuninori Morimoto 
527f1407d5cSKuninori Morimoto probe_end_call_remove:
528f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, hardware_exit, pdev);
529f1407d5cSKuninori Morimoto probe_end_mod_exit:
530f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
531d3af90a5SKuninori Morimoto probe_end_fifo_exit:
532d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
53397f93227SKuninori Morimoto probe_end_pipe_exit:
53497f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
535f1407d5cSKuninori Morimoto probe_end_iounmap:
536f1407d5cSKuninori Morimoto 	iounmap(priv->base);
537f1407d5cSKuninori Morimoto probe_end_kfree:
538f1407d5cSKuninori Morimoto 	kfree(priv);
539f1407d5cSKuninori Morimoto 
540f1407d5cSKuninori Morimoto 	dev_info(&pdev->dev, "probe failed\n");
541f1407d5cSKuninori Morimoto 
542f1407d5cSKuninori Morimoto 	return ret;
543f1407d5cSKuninori Morimoto }
544f1407d5cSKuninori Morimoto 
545f1407d5cSKuninori Morimoto static int __devexit usbhs_remove(struct platform_device *pdev)
546f1407d5cSKuninori Morimoto {
547206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
548af32fe51SKuninori Morimoto 	struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
549af32fe51SKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
550f1407d5cSKuninori Morimoto 
551f1407d5cSKuninori Morimoto 	dev_dbg(&pdev->dev, "usb remove\n");
552f1407d5cSKuninori Morimoto 
553af32fe51SKuninori Morimoto 	dfunc->notify_hotplug = NULL;
554af32fe51SKuninori Morimoto 
555b002ff6eSKuninori Morimoto 	/* power off */
556b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
557b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
558f1407d5cSKuninori Morimoto 
559b002ff6eSKuninori Morimoto 	pm_runtime_disable(&pdev->dev);
560f1407d5cSKuninori Morimoto 
561f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, hardware_exit, pdev);
562f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
563d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
56497f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
565f1407d5cSKuninori Morimoto 	iounmap(priv->base);
566f1407d5cSKuninori Morimoto 	kfree(priv);
567f1407d5cSKuninori Morimoto 
568f1407d5cSKuninori Morimoto 	return 0;
569f1407d5cSKuninori Morimoto }
570f1407d5cSKuninori Morimoto 
571ca8a282aSKuninori Morimoto static int usbhsc_suspend(struct device *dev)
572ca8a282aSKuninori Morimoto {
573ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
574ca8a282aSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
575ca8a282aSKuninori Morimoto 
576ca8a282aSKuninori Morimoto 	if (mod) {
577ca8a282aSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
578ca8a282aSKuninori Morimoto 		usbhs_mod_change(priv, -1);
579ca8a282aSKuninori Morimoto 	}
580ca8a282aSKuninori Morimoto 
581ca8a282aSKuninori Morimoto 	if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
582ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
583ca8a282aSKuninori Morimoto 
584ca8a282aSKuninori Morimoto 	return 0;
585ca8a282aSKuninori Morimoto }
586ca8a282aSKuninori Morimoto 
587ca8a282aSKuninori Morimoto static int usbhsc_resume(struct device *dev)
588ca8a282aSKuninori Morimoto {
589ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
590ca8a282aSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
591ca8a282aSKuninori Morimoto 
592ca8a282aSKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
593ca8a282aSKuninori Morimoto 
594ca8a282aSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
595ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
596ca8a282aSKuninori Morimoto 
597ca8a282aSKuninori Morimoto 	usbhsc_hotplug(priv);
598ca8a282aSKuninori Morimoto 
599ca8a282aSKuninori Morimoto 	return 0;
600ca8a282aSKuninori Morimoto }
601ca8a282aSKuninori Morimoto 
602ca8a282aSKuninori Morimoto static int usbhsc_runtime_nop(struct device *dev)
603ca8a282aSKuninori Morimoto {
604ca8a282aSKuninori Morimoto 	/* Runtime PM callback shared between ->runtime_suspend()
605ca8a282aSKuninori Morimoto 	 * and ->runtime_resume(). Simply returns success.
606ca8a282aSKuninori Morimoto 	 *
607ca8a282aSKuninori Morimoto 	 * This driver re-initializes all registers after
608ca8a282aSKuninori Morimoto 	 * pm_runtime_get_sync() anyway so there is no need
609ca8a282aSKuninori Morimoto 	 * to save and restore registers here.
610ca8a282aSKuninori Morimoto 	 */
611ca8a282aSKuninori Morimoto 	return 0;
612ca8a282aSKuninori Morimoto }
613ca8a282aSKuninori Morimoto 
614ca8a282aSKuninori Morimoto static const struct dev_pm_ops usbhsc_pm_ops = {
615ca8a282aSKuninori Morimoto 	.suspend		= usbhsc_suspend,
616ca8a282aSKuninori Morimoto 	.resume			= usbhsc_resume,
617ca8a282aSKuninori Morimoto 	.runtime_suspend	= usbhsc_runtime_nop,
618ca8a282aSKuninori Morimoto 	.runtime_resume		= usbhsc_runtime_nop,
619ca8a282aSKuninori Morimoto };
620ca8a282aSKuninori Morimoto 
621f1407d5cSKuninori Morimoto static struct platform_driver renesas_usbhs_driver = {
622f1407d5cSKuninori Morimoto 	.driver		= {
623f1407d5cSKuninori Morimoto 		.name	= "renesas_usbhs",
624ca8a282aSKuninori Morimoto 		.pm	= &usbhsc_pm_ops,
625f1407d5cSKuninori Morimoto 	},
626f1407d5cSKuninori Morimoto 	.probe		= usbhs_probe,
627f1407d5cSKuninori Morimoto 	.remove		= __devexit_p(usbhs_remove),
628f1407d5cSKuninori Morimoto };
629f1407d5cSKuninori Morimoto 
630f1407d5cSKuninori Morimoto static int __init usbhs_init(void)
631f1407d5cSKuninori Morimoto {
632f1407d5cSKuninori Morimoto 	return platform_driver_register(&renesas_usbhs_driver);
633f1407d5cSKuninori Morimoto }
634f1407d5cSKuninori Morimoto 
635f1407d5cSKuninori Morimoto static void __exit usbhs_exit(void)
636f1407d5cSKuninori Morimoto {
637f1407d5cSKuninori Morimoto 	platform_driver_unregister(&renesas_usbhs_driver);
638f1407d5cSKuninori Morimoto }
639f1407d5cSKuninori Morimoto 
640f1407d5cSKuninori Morimoto module_init(usbhs_init);
641f1407d5cSKuninori Morimoto module_exit(usbhs_exit);
642f1407d5cSKuninori Morimoto 
643f1407d5cSKuninori Morimoto MODULE_LICENSE("GPL");
644f1407d5cSKuninori Morimoto MODULE_DESCRIPTION("Renesas USB driver");
645f1407d5cSKuninori Morimoto MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
646