xref: /openbmc/linux/drivers/usb/renesas_usbhs/common.c (revision 258485d9904703c4cb3a2b3ee38fe2a0cbf01f48)
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 :			\
64f1407d5cSKuninori Morimoto 	 !((priv)->pfunc->func) ? 0 :		\
65f1407d5cSKuninori 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  */
98f1407d5cSKuninori Morimoto 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_hispeed_ctrl(struct usbhs_priv *priv, int enable)
104f1407d5cSKuninori Morimoto {
105f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, HSE, enable ? HSE : 0);
106f1407d5cSKuninori Morimoto }
107f1407d5cSKuninori Morimoto 
108f1407d5cSKuninori Morimoto void usbhs_sys_usb_ctrl(struct usbhs_priv *priv, int enable)
109f1407d5cSKuninori Morimoto {
110f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, USBE, enable ? USBE : 0);
111f1407d5cSKuninori Morimoto }
112f1407d5cSKuninori Morimoto 
113f1407d5cSKuninori Morimoto void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
114f1407d5cSKuninori Morimoto {
115f1407d5cSKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU;
116f1407d5cSKuninori Morimoto 	u16 val  = DCFM | DRPD;
117f1407d5cSKuninori Morimoto 
118f1407d5cSKuninori Morimoto 	/*
119f1407d5cSKuninori Morimoto 	 * if enable
120f1407d5cSKuninori Morimoto 	 *
121f1407d5cSKuninori Morimoto 	 * - select Host mode
122f1407d5cSKuninori Morimoto 	 * - D+ Line/D- Line Pull-down
123f1407d5cSKuninori Morimoto 	 */
124f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
125f1407d5cSKuninori Morimoto }
126f1407d5cSKuninori Morimoto 
127f1407d5cSKuninori Morimoto void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
128f1407d5cSKuninori Morimoto {
129f1407d5cSKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU;
130f1407d5cSKuninori Morimoto 	u16 val  = DPRPU;
131f1407d5cSKuninori Morimoto 
132f1407d5cSKuninori Morimoto 	/*
133f1407d5cSKuninori Morimoto 	 * if enable
134f1407d5cSKuninori Morimoto 	 *
135f1407d5cSKuninori Morimoto 	 * - select Function mode
136f1407d5cSKuninori Morimoto 	 * - D+ Line Pull-up
137f1407d5cSKuninori Morimoto 	 */
138f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
139f1407d5cSKuninori Morimoto }
140f1407d5cSKuninori 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 /*
150*258485d9SKuninori Morimoto  *		bus/vbus functions
151*258485d9SKuninori Morimoto  */
152*258485d9SKuninori Morimoto void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
153*258485d9SKuninori Morimoto {
154*258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
155*258485d9SKuninori Morimoto }
156*258485d9SKuninori Morimoto 
157*258485d9SKuninori Morimoto void usbhs_bus_send_reset(struct usbhs_priv *priv)
158*258485d9SKuninori Morimoto {
159*258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
160*258485d9SKuninori Morimoto }
161*258485d9SKuninori Morimoto 
162*258485d9SKuninori Morimoto int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
163*258485d9SKuninori Morimoto {
164*258485d9SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
165*258485d9SKuninori Morimoto 
166*258485d9SKuninori Morimoto 	return usbhs_platform_call(priv, set_vbus, pdev, enable);
167*258485d9SKuninori Morimoto }
168*258485d9SKuninori Morimoto 
169*258485d9SKuninori Morimoto static void usbhsc_bus_init(struct usbhs_priv *priv)
170*258485d9SKuninori Morimoto {
171*258485d9SKuninori Morimoto 	usbhs_write(priv, DVSTCTR, 0);
172*258485d9SKuninori Morimoto 
173*258485d9SKuninori Morimoto 	usbhs_vbus_ctrl(priv, 0);
174*258485d9SKuninori Morimoto }
175*258485d9SKuninori Morimoto 
176*258485d9SKuninori Morimoto /*
177f1407d5cSKuninori Morimoto  *		local functions
178f1407d5cSKuninori Morimoto  */
17911935de5SKuninori Morimoto static void usbhsc_set_buswait(struct usbhs_priv *priv)
180f1407d5cSKuninori Morimoto {
181f1407d5cSKuninori Morimoto 	int wait = usbhs_get_dparam(priv, buswait_bwait);
182f1407d5cSKuninori Morimoto 
183f1407d5cSKuninori Morimoto 	/* set bus wait if platform have */
184f1407d5cSKuninori Morimoto 	if (wait)
185f1407d5cSKuninori Morimoto 		usbhs_bset(priv, BUSWAIT, 0x000F, wait);
186f1407d5cSKuninori Morimoto }
187f1407d5cSKuninori Morimoto 
188f1407d5cSKuninori Morimoto /*
189f1407d5cSKuninori Morimoto  *		platform default param
190f1407d5cSKuninori Morimoto  */
191f1407d5cSKuninori Morimoto static u32 usbhsc_default_pipe_type[] = {
192f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_CONTROL,
193f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_ISOC,
194f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_ISOC,
195f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_BULK,
196f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_BULK,
197f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_BULK,
198f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_INT,
199f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_INT,
200f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_INT,
201f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_INT,
202f1407d5cSKuninori Morimoto };
203f1407d5cSKuninori Morimoto 
204f1407d5cSKuninori Morimoto /*
2056e267da8SKuninori Morimoto  *		power control
2066e267da8SKuninori Morimoto  */
2076e267da8SKuninori Morimoto static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
2086e267da8SKuninori Morimoto {
2096e267da8SKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
2106e267da8SKuninori Morimoto 
2116e267da8SKuninori Morimoto 	if (enable) {
2126e267da8SKuninori Morimoto 		/* enable PM */
2136e267da8SKuninori Morimoto 		pm_runtime_get_sync(dev);
2146e267da8SKuninori Morimoto 
2156e267da8SKuninori Morimoto 		/* USB on */
2166e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
2176e267da8SKuninori Morimoto 	} else {
2186e267da8SKuninori Morimoto 		/* USB off */
2196e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
2206e267da8SKuninori Morimoto 
2216e267da8SKuninori Morimoto 		/* disable PM */
2226e267da8SKuninori Morimoto 		pm_runtime_put_sync(dev);
2236e267da8SKuninori Morimoto 	}
2246e267da8SKuninori Morimoto }
2256e267da8SKuninori Morimoto 
2266e267da8SKuninori Morimoto /*
227ca8a282aSKuninori Morimoto  *		hotplug
228f1407d5cSKuninori Morimoto  */
229ca8a282aSKuninori Morimoto static void usbhsc_hotplug(struct usbhs_priv *priv)
230f1407d5cSKuninori Morimoto {
231f1407d5cSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
232f1407d5cSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
233f1407d5cSKuninori Morimoto 	int id;
234f1407d5cSKuninori Morimoto 	int enable;
235f1407d5cSKuninori Morimoto 	int ret;
236f1407d5cSKuninori Morimoto 
237f1407d5cSKuninori Morimoto 	/*
238f1407d5cSKuninori Morimoto 	 * get vbus status from platform
239f1407d5cSKuninori Morimoto 	 */
240f1407d5cSKuninori Morimoto 	enable = usbhs_platform_call(priv, get_vbus, pdev);
241f1407d5cSKuninori Morimoto 
242f1407d5cSKuninori Morimoto 	/*
243f1407d5cSKuninori Morimoto 	 * get id from platform
244f1407d5cSKuninori Morimoto 	 */
245f1407d5cSKuninori Morimoto 	id = usbhs_platform_call(priv, get_id, pdev);
246f1407d5cSKuninori Morimoto 
247f1407d5cSKuninori Morimoto 	if (enable && !mod) {
248f1407d5cSKuninori Morimoto 		ret = usbhs_mod_change(priv, id);
249f1407d5cSKuninori Morimoto 		if (ret < 0)
250f1407d5cSKuninori Morimoto 			return;
251f1407d5cSKuninori Morimoto 
252f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s enable\n", __func__);
253f1407d5cSKuninori Morimoto 
2546e267da8SKuninori Morimoto 		/* power on */
255b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
2566e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
257f1407d5cSKuninori Morimoto 
258*258485d9SKuninori Morimoto 		/* bus init */
259*258485d9SKuninori Morimoto 		usbhsc_set_buswait(priv);
260*258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
261*258485d9SKuninori Morimoto 
262f1407d5cSKuninori Morimoto 		/* module start */
263f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, start, priv);
264f1407d5cSKuninori Morimoto 
265f1407d5cSKuninori Morimoto 	} else if (!enable && mod) {
266f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s disable\n", __func__);
267f1407d5cSKuninori Morimoto 
268f1407d5cSKuninori Morimoto 		/* module stop */
269f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
270f1407d5cSKuninori Morimoto 
271*258485d9SKuninori Morimoto 		/* bus init */
272*258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
273*258485d9SKuninori Morimoto 
2746e267da8SKuninori Morimoto 		/* power off */
275b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
2766e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
277f1407d5cSKuninori Morimoto 
278f1407d5cSKuninori Morimoto 		usbhs_mod_change(priv, -1);
279f1407d5cSKuninori Morimoto 
280f1407d5cSKuninori Morimoto 		/* reset phy for next connection */
281f1407d5cSKuninori Morimoto 		usbhs_platform_call(priv, phy_reset, pdev);
282f1407d5cSKuninori Morimoto 	}
283f1407d5cSKuninori Morimoto }
284f1407d5cSKuninori Morimoto 
285ca8a282aSKuninori Morimoto /*
286ca8a282aSKuninori Morimoto  *		notify hotplug
287ca8a282aSKuninori Morimoto  */
288ca8a282aSKuninori Morimoto static void usbhsc_notify_hotplug(struct work_struct *work)
289ca8a282aSKuninori Morimoto {
290ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = container_of(work,
291ca8a282aSKuninori Morimoto 					       struct usbhs_priv,
292ca8a282aSKuninori Morimoto 					       notify_hotplug_work.work);
293ca8a282aSKuninori Morimoto 	usbhsc_hotplug(priv);
294ca8a282aSKuninori Morimoto }
295ca8a282aSKuninori Morimoto 
296bc57381eSKuninori Morimoto int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
297f1407d5cSKuninori Morimoto {
298206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
299bc57381eSKuninori Morimoto 	int delay = usbhs_get_dparam(priv, detection_delay);
300f1407d5cSKuninori Morimoto 
301f1407d5cSKuninori Morimoto 	/*
302f1407d5cSKuninori Morimoto 	 * This functions will be called in interrupt.
303f1407d5cSKuninori Morimoto 	 * To make sure safety context,
304f1407d5cSKuninori Morimoto 	 * use workqueue for usbhs_notify_hotplug
305f1407d5cSKuninori Morimoto 	 */
306bc57381eSKuninori Morimoto 	schedule_delayed_work(&priv->notify_hotplug_work, delay);
307f1407d5cSKuninori Morimoto 	return 0;
308f1407d5cSKuninori Morimoto }
309f1407d5cSKuninori Morimoto 
310f1407d5cSKuninori Morimoto /*
311f1407d5cSKuninori Morimoto  *		platform functions
312f1407d5cSKuninori Morimoto  */
313f1407d5cSKuninori Morimoto static int __devinit usbhs_probe(struct platform_device *pdev)
314f1407d5cSKuninori Morimoto {
315f1407d5cSKuninori Morimoto 	struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
316f1407d5cSKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc;
317f1407d5cSKuninori Morimoto 	struct usbhs_priv *priv;
318f1407d5cSKuninori Morimoto 	struct resource *res;
319f1407d5cSKuninori Morimoto 	unsigned int irq;
320f1407d5cSKuninori Morimoto 	int ret;
321f1407d5cSKuninori Morimoto 
322f1407d5cSKuninori Morimoto 	/* check platform information */
323f1407d5cSKuninori Morimoto 	if (!info ||
324b002ff6eSKuninori Morimoto 	    !info->platform_callback.get_id) {
325f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "no platform information\n");
326f1407d5cSKuninori Morimoto 		return -EINVAL;
327f1407d5cSKuninori Morimoto 	}
328f1407d5cSKuninori Morimoto 
329f1407d5cSKuninori Morimoto 	/* platform data */
330f1407d5cSKuninori Morimoto 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
331f1407d5cSKuninori Morimoto 	irq = platform_get_irq(pdev, 0);
332f1407d5cSKuninori Morimoto 	if (!res || (int)irq <= 0) {
333f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
334f1407d5cSKuninori Morimoto 		return -ENODEV;
335f1407d5cSKuninori Morimoto 	}
336f1407d5cSKuninori Morimoto 
337f1407d5cSKuninori Morimoto 	/* usb private data */
338f1407d5cSKuninori Morimoto 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
339f1407d5cSKuninori Morimoto 	if (!priv) {
340f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "Could not allocate priv\n");
341f1407d5cSKuninori Morimoto 		return -ENOMEM;
342f1407d5cSKuninori Morimoto 	}
343f1407d5cSKuninori Morimoto 
344f1407d5cSKuninori Morimoto 	priv->base = ioremap_nocache(res->start, resource_size(res));
345f1407d5cSKuninori Morimoto 	if (!priv->base) {
346f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "ioremap error.\n");
347f1407d5cSKuninori Morimoto 		ret = -ENOMEM;
348f1407d5cSKuninori Morimoto 		goto probe_end_kfree;
349f1407d5cSKuninori Morimoto 	}
350f1407d5cSKuninori Morimoto 
351f1407d5cSKuninori Morimoto 	/*
352f1407d5cSKuninori Morimoto 	 * care platform info
353f1407d5cSKuninori Morimoto 	 */
354f1407d5cSKuninori Morimoto 	priv->pfunc	= &info->platform_callback;
355f1407d5cSKuninori Morimoto 	priv->dparam	= &info->driver_param;
356f1407d5cSKuninori Morimoto 
357f1407d5cSKuninori Morimoto 	/* set driver callback functions for platform */
358f1407d5cSKuninori Morimoto 	dfunc			= &info->driver_callback;
359f1407d5cSKuninori Morimoto 	dfunc->notify_hotplug	= usbhsc_drvcllbck_notify_hotplug;
360f1407d5cSKuninori Morimoto 
361f1407d5cSKuninori Morimoto 	/* set default param if platform doesn't have */
362f1407d5cSKuninori Morimoto 	if (!priv->dparam->pipe_type) {
363f1407d5cSKuninori Morimoto 		priv->dparam->pipe_type = usbhsc_default_pipe_type;
364f1407d5cSKuninori Morimoto 		priv->dparam->pipe_size = ARRAY_SIZE(usbhsc_default_pipe_type);
365f1407d5cSKuninori Morimoto 	}
366e73a9891SKuninori Morimoto 	if (!priv->dparam->pio_dma_border)
367e73a9891SKuninori Morimoto 		priv->dparam->pio_dma_border = 64; /* 64byte */
368f1407d5cSKuninori Morimoto 
369b002ff6eSKuninori Morimoto 	/* FIXME */
370b002ff6eSKuninori Morimoto 	/* runtime power control ? */
371b002ff6eSKuninori Morimoto 	if (priv->pfunc->get_vbus)
372b002ff6eSKuninori Morimoto 		usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
373b002ff6eSKuninori Morimoto 
374f1407d5cSKuninori Morimoto 	/*
375f1407d5cSKuninori Morimoto 	 * priv settings
376f1407d5cSKuninori Morimoto 	 */
377f1407d5cSKuninori Morimoto 	priv->irq	= irq;
378f1407d5cSKuninori Morimoto 	priv->pdev	= pdev;
379bc57381eSKuninori Morimoto 	INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
380f1407d5cSKuninori Morimoto 	spin_lock_init(usbhs_priv_to_lock(priv));
381f1407d5cSKuninori Morimoto 
382f1407d5cSKuninori Morimoto 	/* call pipe and module init */
383f1407d5cSKuninori Morimoto 	ret = usbhs_pipe_probe(priv);
384f1407d5cSKuninori Morimoto 	if (ret < 0)
38597f93227SKuninori Morimoto 		goto probe_end_iounmap;
386f1407d5cSKuninori Morimoto 
387d3af90a5SKuninori Morimoto 	ret = usbhs_fifo_probe(priv);
388f1407d5cSKuninori Morimoto 	if (ret < 0)
38997f93227SKuninori Morimoto 		goto probe_end_pipe_exit;
390f1407d5cSKuninori Morimoto 
391d3af90a5SKuninori Morimoto 	ret = usbhs_mod_probe(priv);
392d3af90a5SKuninori Morimoto 	if (ret < 0)
393d3af90a5SKuninori Morimoto 		goto probe_end_fifo_exit;
394d3af90a5SKuninori Morimoto 
395f1407d5cSKuninori Morimoto 	/* dev_set_drvdata should be called after usbhs_mod_init */
396f1407d5cSKuninori Morimoto 	dev_set_drvdata(&pdev->dev, priv);
397f1407d5cSKuninori Morimoto 
398f1407d5cSKuninori Morimoto 	/*
399f1407d5cSKuninori Morimoto 	 * deviece reset here because
400f1407d5cSKuninori Morimoto 	 * USB device might be used in boot loader.
401f1407d5cSKuninori Morimoto 	 */
402f1407d5cSKuninori Morimoto 	usbhs_sys_clock_ctrl(priv, 0);
403f1407d5cSKuninori Morimoto 
404f1407d5cSKuninori Morimoto 	/*
405f1407d5cSKuninori Morimoto 	 * platform call
406f1407d5cSKuninori Morimoto 	 *
407f1407d5cSKuninori Morimoto 	 * USB phy setup might depend on CPU/Board.
408f1407d5cSKuninori Morimoto 	 * If platform has its callback functions,
409f1407d5cSKuninori Morimoto 	 * call it here.
410f1407d5cSKuninori Morimoto 	 */
411f1407d5cSKuninori Morimoto 	ret = usbhs_platform_call(priv, hardware_init, pdev);
412f1407d5cSKuninori Morimoto 	if (ret < 0) {
413f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "platform prove failed.\n");
41497f93227SKuninori Morimoto 		goto probe_end_mod_exit;
415f1407d5cSKuninori Morimoto 	}
416f1407d5cSKuninori Morimoto 
417f1407d5cSKuninori Morimoto 	/* reset phy for connection */
418f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
419f1407d5cSKuninori Morimoto 
420b002ff6eSKuninori Morimoto 	/* power control */
421b002ff6eSKuninori Morimoto 	pm_runtime_enable(&pdev->dev);
422b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
423b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
424b002ff6eSKuninori Morimoto 		usbhs_mod_autonomy_mode(priv);
425b002ff6eSKuninori Morimoto 	}
426b002ff6eSKuninori Morimoto 
427f1407d5cSKuninori Morimoto 	/*
428f1407d5cSKuninori Morimoto 	 * manual call notify_hotplug for cold plug
429f1407d5cSKuninori Morimoto 	 */
430f1407d5cSKuninori Morimoto 	ret = usbhsc_drvcllbck_notify_hotplug(pdev);
431f1407d5cSKuninori Morimoto 	if (ret < 0)
432f1407d5cSKuninori Morimoto 		goto probe_end_call_remove;
433f1407d5cSKuninori Morimoto 
434f1407d5cSKuninori Morimoto 	dev_info(&pdev->dev, "probed\n");
435f1407d5cSKuninori Morimoto 
436f1407d5cSKuninori Morimoto 	return ret;
437f1407d5cSKuninori Morimoto 
438f1407d5cSKuninori Morimoto probe_end_call_remove:
439f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, hardware_exit, pdev);
440f1407d5cSKuninori Morimoto probe_end_mod_exit:
441f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
442d3af90a5SKuninori Morimoto probe_end_fifo_exit:
443d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
44497f93227SKuninori Morimoto probe_end_pipe_exit:
44597f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
446f1407d5cSKuninori Morimoto probe_end_iounmap:
447f1407d5cSKuninori Morimoto 	iounmap(priv->base);
448f1407d5cSKuninori Morimoto probe_end_kfree:
449f1407d5cSKuninori Morimoto 	kfree(priv);
450f1407d5cSKuninori Morimoto 
451f1407d5cSKuninori Morimoto 	dev_info(&pdev->dev, "probe failed\n");
452f1407d5cSKuninori Morimoto 
453f1407d5cSKuninori Morimoto 	return ret;
454f1407d5cSKuninori Morimoto }
455f1407d5cSKuninori Morimoto 
456f1407d5cSKuninori Morimoto static int __devexit usbhs_remove(struct platform_device *pdev)
457f1407d5cSKuninori Morimoto {
458206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
459af32fe51SKuninori Morimoto 	struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
460af32fe51SKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
461f1407d5cSKuninori Morimoto 
462f1407d5cSKuninori Morimoto 	dev_dbg(&pdev->dev, "usb remove\n");
463f1407d5cSKuninori Morimoto 
464af32fe51SKuninori Morimoto 	dfunc->notify_hotplug = NULL;
465af32fe51SKuninori Morimoto 
466b002ff6eSKuninori Morimoto 	/* power off */
467b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
468b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
469f1407d5cSKuninori Morimoto 
470b002ff6eSKuninori Morimoto 	pm_runtime_disable(&pdev->dev);
471f1407d5cSKuninori Morimoto 
472f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, hardware_exit, pdev);
473f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
474d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
47597f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
476f1407d5cSKuninori Morimoto 	iounmap(priv->base);
477f1407d5cSKuninori Morimoto 	kfree(priv);
478f1407d5cSKuninori Morimoto 
479f1407d5cSKuninori Morimoto 	return 0;
480f1407d5cSKuninori Morimoto }
481f1407d5cSKuninori Morimoto 
482ca8a282aSKuninori Morimoto static int usbhsc_suspend(struct device *dev)
483ca8a282aSKuninori Morimoto {
484ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
485ca8a282aSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
486ca8a282aSKuninori Morimoto 
487ca8a282aSKuninori Morimoto 	if (mod) {
488ca8a282aSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
489ca8a282aSKuninori Morimoto 		usbhs_mod_change(priv, -1);
490ca8a282aSKuninori Morimoto 	}
491ca8a282aSKuninori Morimoto 
492ca8a282aSKuninori Morimoto 	if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
493ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
494ca8a282aSKuninori Morimoto 
495ca8a282aSKuninori Morimoto 	return 0;
496ca8a282aSKuninori Morimoto }
497ca8a282aSKuninori Morimoto 
498ca8a282aSKuninori Morimoto static int usbhsc_resume(struct device *dev)
499ca8a282aSKuninori Morimoto {
500ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
501ca8a282aSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
502ca8a282aSKuninori Morimoto 
503ca8a282aSKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
504ca8a282aSKuninori Morimoto 
505ca8a282aSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
506ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
507ca8a282aSKuninori Morimoto 
508ca8a282aSKuninori Morimoto 	usbhsc_hotplug(priv);
509ca8a282aSKuninori Morimoto 
510ca8a282aSKuninori Morimoto 	return 0;
511ca8a282aSKuninori Morimoto }
512ca8a282aSKuninori Morimoto 
513ca8a282aSKuninori Morimoto static int usbhsc_runtime_nop(struct device *dev)
514ca8a282aSKuninori Morimoto {
515ca8a282aSKuninori Morimoto 	/* Runtime PM callback shared between ->runtime_suspend()
516ca8a282aSKuninori Morimoto 	 * and ->runtime_resume(). Simply returns success.
517ca8a282aSKuninori Morimoto 	 *
518ca8a282aSKuninori Morimoto 	 * This driver re-initializes all registers after
519ca8a282aSKuninori Morimoto 	 * pm_runtime_get_sync() anyway so there is no need
520ca8a282aSKuninori Morimoto 	 * to save and restore registers here.
521ca8a282aSKuninori Morimoto 	 */
522ca8a282aSKuninori Morimoto 	return 0;
523ca8a282aSKuninori Morimoto }
524ca8a282aSKuninori Morimoto 
525ca8a282aSKuninori Morimoto static const struct dev_pm_ops usbhsc_pm_ops = {
526ca8a282aSKuninori Morimoto 	.suspend		= usbhsc_suspend,
527ca8a282aSKuninori Morimoto 	.resume			= usbhsc_resume,
528ca8a282aSKuninori Morimoto 	.runtime_suspend	= usbhsc_runtime_nop,
529ca8a282aSKuninori Morimoto 	.runtime_resume		= usbhsc_runtime_nop,
530ca8a282aSKuninori Morimoto };
531ca8a282aSKuninori Morimoto 
532f1407d5cSKuninori Morimoto static struct platform_driver renesas_usbhs_driver = {
533f1407d5cSKuninori Morimoto 	.driver		= {
534f1407d5cSKuninori Morimoto 		.name	= "renesas_usbhs",
535ca8a282aSKuninori Morimoto 		.pm	= &usbhsc_pm_ops,
536f1407d5cSKuninori Morimoto 	},
537f1407d5cSKuninori Morimoto 	.probe		= usbhs_probe,
538f1407d5cSKuninori Morimoto 	.remove		= __devexit_p(usbhs_remove),
539f1407d5cSKuninori Morimoto };
540f1407d5cSKuninori Morimoto 
541f1407d5cSKuninori Morimoto static int __init usbhs_init(void)
542f1407d5cSKuninori Morimoto {
543f1407d5cSKuninori Morimoto 	return platform_driver_register(&renesas_usbhs_driver);
544f1407d5cSKuninori Morimoto }
545f1407d5cSKuninori Morimoto 
546f1407d5cSKuninori Morimoto static void __exit usbhs_exit(void)
547f1407d5cSKuninori Morimoto {
548f1407d5cSKuninori Morimoto 	platform_driver_unregister(&renesas_usbhs_driver);
549f1407d5cSKuninori Morimoto }
550f1407d5cSKuninori Morimoto 
551f1407d5cSKuninori Morimoto module_init(usbhs_init);
552f1407d5cSKuninori Morimoto module_exit(usbhs_exit);
553f1407d5cSKuninori Morimoto 
554f1407d5cSKuninori Morimoto MODULE_LICENSE("GPL");
555f1407d5cSKuninori Morimoto MODULE_DESCRIPTION("Renesas USB driver");
556f1407d5cSKuninori Morimoto MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
557