xref: /openbmc/linux/drivers/usb/renesas_usbhs/common.c (revision 75587f52c7b0d6c319515138a495a619b552a670)
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 /*
150258485d9SKuninori Morimoto  *		bus/vbus functions
151258485d9SKuninori Morimoto  */
152258485d9SKuninori Morimoto void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
153258485d9SKuninori Morimoto {
154258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
155258485d9SKuninori Morimoto }
156258485d9SKuninori Morimoto 
157258485d9SKuninori Morimoto void usbhs_bus_send_reset(struct usbhs_priv *priv)
158258485d9SKuninori Morimoto {
159258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
160258485d9SKuninori Morimoto }
161258485d9SKuninori Morimoto 
162*75587f52SKuninori Morimoto int usbhs_bus_get_speed(struct usbhs_priv *priv)
163*75587f52SKuninori Morimoto {
164*75587f52SKuninori Morimoto 	u16 dvstctr = usbhs_read(priv, DVSTCTR);
165*75587f52SKuninori Morimoto 
166*75587f52SKuninori Morimoto 	switch (RHST & dvstctr) {
167*75587f52SKuninori Morimoto 	case RHST_LOW_SPEED:
168*75587f52SKuninori Morimoto 		return USB_SPEED_LOW;
169*75587f52SKuninori Morimoto 	case RHST_FULL_SPEED:
170*75587f52SKuninori Morimoto 		return USB_SPEED_FULL;
171*75587f52SKuninori Morimoto 	case RHST_HIGH_SPEED:
172*75587f52SKuninori Morimoto 		return USB_SPEED_HIGH;
173*75587f52SKuninori Morimoto 	}
174*75587f52SKuninori Morimoto 
175*75587f52SKuninori Morimoto 	return USB_SPEED_UNKNOWN;
176*75587f52SKuninori Morimoto }
177*75587f52SKuninori Morimoto 
178258485d9SKuninori Morimoto int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
179258485d9SKuninori Morimoto {
180258485d9SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
181258485d9SKuninori Morimoto 
182258485d9SKuninori Morimoto 	return usbhs_platform_call(priv, set_vbus, pdev, enable);
183258485d9SKuninori Morimoto }
184258485d9SKuninori Morimoto 
185258485d9SKuninori Morimoto static void usbhsc_bus_init(struct usbhs_priv *priv)
186258485d9SKuninori Morimoto {
187258485d9SKuninori Morimoto 	usbhs_write(priv, DVSTCTR, 0);
188258485d9SKuninori Morimoto 
189258485d9SKuninori Morimoto 	usbhs_vbus_ctrl(priv, 0);
190258485d9SKuninori Morimoto }
191258485d9SKuninori Morimoto 
192258485d9SKuninori Morimoto /*
193f1407d5cSKuninori Morimoto  *		local functions
194f1407d5cSKuninori Morimoto  */
19511935de5SKuninori Morimoto static void usbhsc_set_buswait(struct usbhs_priv *priv)
196f1407d5cSKuninori Morimoto {
197f1407d5cSKuninori Morimoto 	int wait = usbhs_get_dparam(priv, buswait_bwait);
198f1407d5cSKuninori Morimoto 
199f1407d5cSKuninori Morimoto 	/* set bus wait if platform have */
200f1407d5cSKuninori Morimoto 	if (wait)
201f1407d5cSKuninori Morimoto 		usbhs_bset(priv, BUSWAIT, 0x000F, wait);
202f1407d5cSKuninori Morimoto }
203f1407d5cSKuninori Morimoto 
204f1407d5cSKuninori Morimoto /*
205f1407d5cSKuninori Morimoto  *		platform default param
206f1407d5cSKuninori Morimoto  */
207f1407d5cSKuninori Morimoto static u32 usbhsc_default_pipe_type[] = {
208f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_CONTROL,
209f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_ISOC,
210f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_ISOC,
211f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_BULK,
212f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_BULK,
213f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_BULK,
214f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_INT,
215f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_INT,
216f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_INT,
217f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_INT,
218f1407d5cSKuninori Morimoto };
219f1407d5cSKuninori Morimoto 
220f1407d5cSKuninori Morimoto /*
2216e267da8SKuninori Morimoto  *		power control
2226e267da8SKuninori Morimoto  */
2236e267da8SKuninori Morimoto static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
2246e267da8SKuninori Morimoto {
2256e267da8SKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
2266e267da8SKuninori Morimoto 
2276e267da8SKuninori Morimoto 	if (enable) {
2286e267da8SKuninori Morimoto 		/* enable PM */
2296e267da8SKuninori Morimoto 		pm_runtime_get_sync(dev);
2306e267da8SKuninori Morimoto 
2316e267da8SKuninori Morimoto 		/* USB on */
2326e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
2336e267da8SKuninori Morimoto 	} else {
2346e267da8SKuninori Morimoto 		/* USB off */
2356e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
2366e267da8SKuninori Morimoto 
2376e267da8SKuninori Morimoto 		/* disable PM */
2386e267da8SKuninori Morimoto 		pm_runtime_put_sync(dev);
2396e267da8SKuninori Morimoto 	}
2406e267da8SKuninori Morimoto }
2416e267da8SKuninori Morimoto 
2426e267da8SKuninori Morimoto /*
243ca8a282aSKuninori Morimoto  *		hotplug
244f1407d5cSKuninori Morimoto  */
245ca8a282aSKuninori Morimoto static void usbhsc_hotplug(struct usbhs_priv *priv)
246f1407d5cSKuninori Morimoto {
247f1407d5cSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
248f1407d5cSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
249f1407d5cSKuninori Morimoto 	int id;
250f1407d5cSKuninori Morimoto 	int enable;
251f1407d5cSKuninori Morimoto 	int ret;
252f1407d5cSKuninori Morimoto 
253f1407d5cSKuninori Morimoto 	/*
254f1407d5cSKuninori Morimoto 	 * get vbus status from platform
255f1407d5cSKuninori Morimoto 	 */
256f1407d5cSKuninori Morimoto 	enable = usbhs_platform_call(priv, get_vbus, pdev);
257f1407d5cSKuninori Morimoto 
258f1407d5cSKuninori Morimoto 	/*
259f1407d5cSKuninori Morimoto 	 * get id from platform
260f1407d5cSKuninori Morimoto 	 */
261f1407d5cSKuninori Morimoto 	id = usbhs_platform_call(priv, get_id, pdev);
262f1407d5cSKuninori Morimoto 
263f1407d5cSKuninori Morimoto 	if (enable && !mod) {
264f1407d5cSKuninori Morimoto 		ret = usbhs_mod_change(priv, id);
265f1407d5cSKuninori Morimoto 		if (ret < 0)
266f1407d5cSKuninori Morimoto 			return;
267f1407d5cSKuninori Morimoto 
268f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s enable\n", __func__);
269f1407d5cSKuninori Morimoto 
2706e267da8SKuninori Morimoto 		/* power on */
271b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
2726e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
273f1407d5cSKuninori Morimoto 
274258485d9SKuninori Morimoto 		/* bus init */
275258485d9SKuninori Morimoto 		usbhsc_set_buswait(priv);
276258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
277258485d9SKuninori Morimoto 
278f1407d5cSKuninori Morimoto 		/* module start */
279f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, start, priv);
280f1407d5cSKuninori Morimoto 
281f1407d5cSKuninori Morimoto 	} else if (!enable && mod) {
282f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s disable\n", __func__);
283f1407d5cSKuninori Morimoto 
284f1407d5cSKuninori Morimoto 		/* module stop */
285f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
286f1407d5cSKuninori Morimoto 
287258485d9SKuninori Morimoto 		/* bus init */
288258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
289258485d9SKuninori Morimoto 
2906e267da8SKuninori Morimoto 		/* power off */
291b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
2926e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
293f1407d5cSKuninori Morimoto 
294f1407d5cSKuninori Morimoto 		usbhs_mod_change(priv, -1);
295f1407d5cSKuninori Morimoto 
296f1407d5cSKuninori Morimoto 		/* reset phy for next connection */
297f1407d5cSKuninori Morimoto 		usbhs_platform_call(priv, phy_reset, pdev);
298f1407d5cSKuninori Morimoto 	}
299f1407d5cSKuninori Morimoto }
300f1407d5cSKuninori Morimoto 
301ca8a282aSKuninori Morimoto /*
302ca8a282aSKuninori Morimoto  *		notify hotplug
303ca8a282aSKuninori Morimoto  */
304ca8a282aSKuninori Morimoto static void usbhsc_notify_hotplug(struct work_struct *work)
305ca8a282aSKuninori Morimoto {
306ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = container_of(work,
307ca8a282aSKuninori Morimoto 					       struct usbhs_priv,
308ca8a282aSKuninori Morimoto 					       notify_hotplug_work.work);
309ca8a282aSKuninori Morimoto 	usbhsc_hotplug(priv);
310ca8a282aSKuninori Morimoto }
311ca8a282aSKuninori Morimoto 
312bc57381eSKuninori Morimoto int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
313f1407d5cSKuninori Morimoto {
314206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
315bc57381eSKuninori Morimoto 	int delay = usbhs_get_dparam(priv, detection_delay);
316f1407d5cSKuninori Morimoto 
317f1407d5cSKuninori Morimoto 	/*
318f1407d5cSKuninori Morimoto 	 * This functions will be called in interrupt.
319f1407d5cSKuninori Morimoto 	 * To make sure safety context,
320f1407d5cSKuninori Morimoto 	 * use workqueue for usbhs_notify_hotplug
321f1407d5cSKuninori Morimoto 	 */
322bc57381eSKuninori Morimoto 	schedule_delayed_work(&priv->notify_hotplug_work, delay);
323f1407d5cSKuninori Morimoto 	return 0;
324f1407d5cSKuninori Morimoto }
325f1407d5cSKuninori Morimoto 
326f1407d5cSKuninori Morimoto /*
327f1407d5cSKuninori Morimoto  *		platform functions
328f1407d5cSKuninori Morimoto  */
329f1407d5cSKuninori Morimoto static int __devinit usbhs_probe(struct platform_device *pdev)
330f1407d5cSKuninori Morimoto {
331f1407d5cSKuninori Morimoto 	struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
332f1407d5cSKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc;
333f1407d5cSKuninori Morimoto 	struct usbhs_priv *priv;
334f1407d5cSKuninori Morimoto 	struct resource *res;
335f1407d5cSKuninori Morimoto 	unsigned int irq;
336f1407d5cSKuninori Morimoto 	int ret;
337f1407d5cSKuninori Morimoto 
338f1407d5cSKuninori Morimoto 	/* check platform information */
339f1407d5cSKuninori Morimoto 	if (!info ||
340b002ff6eSKuninori Morimoto 	    !info->platform_callback.get_id) {
341f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "no platform information\n");
342f1407d5cSKuninori Morimoto 		return -EINVAL;
343f1407d5cSKuninori Morimoto 	}
344f1407d5cSKuninori Morimoto 
345f1407d5cSKuninori Morimoto 	/* platform data */
346f1407d5cSKuninori Morimoto 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
347f1407d5cSKuninori Morimoto 	irq = platform_get_irq(pdev, 0);
348f1407d5cSKuninori Morimoto 	if (!res || (int)irq <= 0) {
349f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
350f1407d5cSKuninori Morimoto 		return -ENODEV;
351f1407d5cSKuninori Morimoto 	}
352f1407d5cSKuninori Morimoto 
353f1407d5cSKuninori Morimoto 	/* usb private data */
354f1407d5cSKuninori Morimoto 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
355f1407d5cSKuninori Morimoto 	if (!priv) {
356f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "Could not allocate priv\n");
357f1407d5cSKuninori Morimoto 		return -ENOMEM;
358f1407d5cSKuninori Morimoto 	}
359f1407d5cSKuninori Morimoto 
360f1407d5cSKuninori Morimoto 	priv->base = ioremap_nocache(res->start, resource_size(res));
361f1407d5cSKuninori Morimoto 	if (!priv->base) {
362f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "ioremap error.\n");
363f1407d5cSKuninori Morimoto 		ret = -ENOMEM;
364f1407d5cSKuninori Morimoto 		goto probe_end_kfree;
365f1407d5cSKuninori Morimoto 	}
366f1407d5cSKuninori Morimoto 
367f1407d5cSKuninori Morimoto 	/*
368f1407d5cSKuninori Morimoto 	 * care platform info
369f1407d5cSKuninori Morimoto 	 */
370f1407d5cSKuninori Morimoto 	priv->pfunc	= &info->platform_callback;
371f1407d5cSKuninori Morimoto 	priv->dparam	= &info->driver_param;
372f1407d5cSKuninori Morimoto 
373f1407d5cSKuninori Morimoto 	/* set driver callback functions for platform */
374f1407d5cSKuninori Morimoto 	dfunc			= &info->driver_callback;
375f1407d5cSKuninori Morimoto 	dfunc->notify_hotplug	= usbhsc_drvcllbck_notify_hotplug;
376f1407d5cSKuninori Morimoto 
377f1407d5cSKuninori Morimoto 	/* set default param if platform doesn't have */
378f1407d5cSKuninori Morimoto 	if (!priv->dparam->pipe_type) {
379f1407d5cSKuninori Morimoto 		priv->dparam->pipe_type = usbhsc_default_pipe_type;
380f1407d5cSKuninori Morimoto 		priv->dparam->pipe_size = ARRAY_SIZE(usbhsc_default_pipe_type);
381f1407d5cSKuninori Morimoto 	}
382e73a9891SKuninori Morimoto 	if (!priv->dparam->pio_dma_border)
383e73a9891SKuninori Morimoto 		priv->dparam->pio_dma_border = 64; /* 64byte */
384f1407d5cSKuninori Morimoto 
385b002ff6eSKuninori Morimoto 	/* FIXME */
386b002ff6eSKuninori Morimoto 	/* runtime power control ? */
387b002ff6eSKuninori Morimoto 	if (priv->pfunc->get_vbus)
388b002ff6eSKuninori Morimoto 		usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
389b002ff6eSKuninori Morimoto 
390f1407d5cSKuninori Morimoto 	/*
391f1407d5cSKuninori Morimoto 	 * priv settings
392f1407d5cSKuninori Morimoto 	 */
393f1407d5cSKuninori Morimoto 	priv->irq	= irq;
394f1407d5cSKuninori Morimoto 	priv->pdev	= pdev;
395bc57381eSKuninori Morimoto 	INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
396f1407d5cSKuninori Morimoto 	spin_lock_init(usbhs_priv_to_lock(priv));
397f1407d5cSKuninori Morimoto 
398f1407d5cSKuninori Morimoto 	/* call pipe and module init */
399f1407d5cSKuninori Morimoto 	ret = usbhs_pipe_probe(priv);
400f1407d5cSKuninori Morimoto 	if (ret < 0)
40197f93227SKuninori Morimoto 		goto probe_end_iounmap;
402f1407d5cSKuninori Morimoto 
403d3af90a5SKuninori Morimoto 	ret = usbhs_fifo_probe(priv);
404f1407d5cSKuninori Morimoto 	if (ret < 0)
40597f93227SKuninori Morimoto 		goto probe_end_pipe_exit;
406f1407d5cSKuninori Morimoto 
407d3af90a5SKuninori Morimoto 	ret = usbhs_mod_probe(priv);
408d3af90a5SKuninori Morimoto 	if (ret < 0)
409d3af90a5SKuninori Morimoto 		goto probe_end_fifo_exit;
410d3af90a5SKuninori Morimoto 
411f1407d5cSKuninori Morimoto 	/* dev_set_drvdata should be called after usbhs_mod_init */
412f1407d5cSKuninori Morimoto 	dev_set_drvdata(&pdev->dev, priv);
413f1407d5cSKuninori Morimoto 
414f1407d5cSKuninori Morimoto 	/*
415f1407d5cSKuninori Morimoto 	 * deviece reset here because
416f1407d5cSKuninori Morimoto 	 * USB device might be used in boot loader.
417f1407d5cSKuninori Morimoto 	 */
418f1407d5cSKuninori Morimoto 	usbhs_sys_clock_ctrl(priv, 0);
419f1407d5cSKuninori Morimoto 
420f1407d5cSKuninori Morimoto 	/*
421f1407d5cSKuninori Morimoto 	 * platform call
422f1407d5cSKuninori Morimoto 	 *
423f1407d5cSKuninori Morimoto 	 * USB phy setup might depend on CPU/Board.
424f1407d5cSKuninori Morimoto 	 * If platform has its callback functions,
425f1407d5cSKuninori Morimoto 	 * call it here.
426f1407d5cSKuninori Morimoto 	 */
427f1407d5cSKuninori Morimoto 	ret = usbhs_platform_call(priv, hardware_init, pdev);
428f1407d5cSKuninori Morimoto 	if (ret < 0) {
429f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "platform prove failed.\n");
43097f93227SKuninori Morimoto 		goto probe_end_mod_exit;
431f1407d5cSKuninori Morimoto 	}
432f1407d5cSKuninori Morimoto 
433f1407d5cSKuninori Morimoto 	/* reset phy for connection */
434f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
435f1407d5cSKuninori Morimoto 
436b002ff6eSKuninori Morimoto 	/* power control */
437b002ff6eSKuninori Morimoto 	pm_runtime_enable(&pdev->dev);
438b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
439b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
440b002ff6eSKuninori Morimoto 		usbhs_mod_autonomy_mode(priv);
441b002ff6eSKuninori Morimoto 	}
442b002ff6eSKuninori Morimoto 
443f1407d5cSKuninori Morimoto 	/*
444f1407d5cSKuninori Morimoto 	 * manual call notify_hotplug for cold plug
445f1407d5cSKuninori Morimoto 	 */
446f1407d5cSKuninori Morimoto 	ret = usbhsc_drvcllbck_notify_hotplug(pdev);
447f1407d5cSKuninori Morimoto 	if (ret < 0)
448f1407d5cSKuninori Morimoto 		goto probe_end_call_remove;
449f1407d5cSKuninori Morimoto 
450f1407d5cSKuninori Morimoto 	dev_info(&pdev->dev, "probed\n");
451f1407d5cSKuninori Morimoto 
452f1407d5cSKuninori Morimoto 	return ret;
453f1407d5cSKuninori Morimoto 
454f1407d5cSKuninori Morimoto probe_end_call_remove:
455f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, hardware_exit, pdev);
456f1407d5cSKuninori Morimoto probe_end_mod_exit:
457f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
458d3af90a5SKuninori Morimoto probe_end_fifo_exit:
459d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
46097f93227SKuninori Morimoto probe_end_pipe_exit:
46197f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
462f1407d5cSKuninori Morimoto probe_end_iounmap:
463f1407d5cSKuninori Morimoto 	iounmap(priv->base);
464f1407d5cSKuninori Morimoto probe_end_kfree:
465f1407d5cSKuninori Morimoto 	kfree(priv);
466f1407d5cSKuninori Morimoto 
467f1407d5cSKuninori Morimoto 	dev_info(&pdev->dev, "probe failed\n");
468f1407d5cSKuninori Morimoto 
469f1407d5cSKuninori Morimoto 	return ret;
470f1407d5cSKuninori Morimoto }
471f1407d5cSKuninori Morimoto 
472f1407d5cSKuninori Morimoto static int __devexit usbhs_remove(struct platform_device *pdev)
473f1407d5cSKuninori Morimoto {
474206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
475af32fe51SKuninori Morimoto 	struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
476af32fe51SKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
477f1407d5cSKuninori Morimoto 
478f1407d5cSKuninori Morimoto 	dev_dbg(&pdev->dev, "usb remove\n");
479f1407d5cSKuninori Morimoto 
480af32fe51SKuninori Morimoto 	dfunc->notify_hotplug = NULL;
481af32fe51SKuninori Morimoto 
482b002ff6eSKuninori Morimoto 	/* power off */
483b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
484b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
485f1407d5cSKuninori Morimoto 
486b002ff6eSKuninori Morimoto 	pm_runtime_disable(&pdev->dev);
487f1407d5cSKuninori Morimoto 
488f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, hardware_exit, pdev);
489f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
490d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
49197f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
492f1407d5cSKuninori Morimoto 	iounmap(priv->base);
493f1407d5cSKuninori Morimoto 	kfree(priv);
494f1407d5cSKuninori Morimoto 
495f1407d5cSKuninori Morimoto 	return 0;
496f1407d5cSKuninori Morimoto }
497f1407d5cSKuninori Morimoto 
498ca8a282aSKuninori Morimoto static int usbhsc_suspend(struct device *dev)
499ca8a282aSKuninori Morimoto {
500ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
501ca8a282aSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
502ca8a282aSKuninori Morimoto 
503ca8a282aSKuninori Morimoto 	if (mod) {
504ca8a282aSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
505ca8a282aSKuninori Morimoto 		usbhs_mod_change(priv, -1);
506ca8a282aSKuninori Morimoto 	}
507ca8a282aSKuninori Morimoto 
508ca8a282aSKuninori Morimoto 	if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
509ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
510ca8a282aSKuninori Morimoto 
511ca8a282aSKuninori Morimoto 	return 0;
512ca8a282aSKuninori Morimoto }
513ca8a282aSKuninori Morimoto 
514ca8a282aSKuninori Morimoto static int usbhsc_resume(struct device *dev)
515ca8a282aSKuninori Morimoto {
516ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
517ca8a282aSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
518ca8a282aSKuninori Morimoto 
519ca8a282aSKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
520ca8a282aSKuninori Morimoto 
521ca8a282aSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
522ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
523ca8a282aSKuninori Morimoto 
524ca8a282aSKuninori Morimoto 	usbhsc_hotplug(priv);
525ca8a282aSKuninori Morimoto 
526ca8a282aSKuninori Morimoto 	return 0;
527ca8a282aSKuninori Morimoto }
528ca8a282aSKuninori Morimoto 
529ca8a282aSKuninori Morimoto static int usbhsc_runtime_nop(struct device *dev)
530ca8a282aSKuninori Morimoto {
531ca8a282aSKuninori Morimoto 	/* Runtime PM callback shared between ->runtime_suspend()
532ca8a282aSKuninori Morimoto 	 * and ->runtime_resume(). Simply returns success.
533ca8a282aSKuninori Morimoto 	 *
534ca8a282aSKuninori Morimoto 	 * This driver re-initializes all registers after
535ca8a282aSKuninori Morimoto 	 * pm_runtime_get_sync() anyway so there is no need
536ca8a282aSKuninori Morimoto 	 * to save and restore registers here.
537ca8a282aSKuninori Morimoto 	 */
538ca8a282aSKuninori Morimoto 	return 0;
539ca8a282aSKuninori Morimoto }
540ca8a282aSKuninori Morimoto 
541ca8a282aSKuninori Morimoto static const struct dev_pm_ops usbhsc_pm_ops = {
542ca8a282aSKuninori Morimoto 	.suspend		= usbhsc_suspend,
543ca8a282aSKuninori Morimoto 	.resume			= usbhsc_resume,
544ca8a282aSKuninori Morimoto 	.runtime_suspend	= usbhsc_runtime_nop,
545ca8a282aSKuninori Morimoto 	.runtime_resume		= usbhsc_runtime_nop,
546ca8a282aSKuninori Morimoto };
547ca8a282aSKuninori Morimoto 
548f1407d5cSKuninori Morimoto static struct platform_driver renesas_usbhs_driver = {
549f1407d5cSKuninori Morimoto 	.driver		= {
550f1407d5cSKuninori Morimoto 		.name	= "renesas_usbhs",
551ca8a282aSKuninori Morimoto 		.pm	= &usbhsc_pm_ops,
552f1407d5cSKuninori Morimoto 	},
553f1407d5cSKuninori Morimoto 	.probe		= usbhs_probe,
554f1407d5cSKuninori Morimoto 	.remove		= __devexit_p(usbhs_remove),
555f1407d5cSKuninori Morimoto };
556f1407d5cSKuninori Morimoto 
557f1407d5cSKuninori Morimoto static int __init usbhs_init(void)
558f1407d5cSKuninori Morimoto {
559f1407d5cSKuninori Morimoto 	return platform_driver_register(&renesas_usbhs_driver);
560f1407d5cSKuninori Morimoto }
561f1407d5cSKuninori Morimoto 
562f1407d5cSKuninori Morimoto static void __exit usbhs_exit(void)
563f1407d5cSKuninori Morimoto {
564f1407d5cSKuninori Morimoto 	platform_driver_unregister(&renesas_usbhs_driver);
565f1407d5cSKuninori Morimoto }
566f1407d5cSKuninori Morimoto 
567f1407d5cSKuninori Morimoto module_init(usbhs_init);
568f1407d5cSKuninori Morimoto module_exit(usbhs_exit);
569f1407d5cSKuninori Morimoto 
570f1407d5cSKuninori Morimoto MODULE_LICENSE("GPL");
571f1407d5cSKuninori Morimoto MODULE_DESCRIPTION("Renesas USB driver");
572f1407d5cSKuninori Morimoto MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
573