xref: /openbmc/linux/drivers/usb/renesas_usbhs/common.c (revision cc27c96c2bee93068bfc60ea6b09611d88cef429)
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  */
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;
117f427eb64SKuninori Morimoto 	int has_otg = usbhs_get_dparam(priv, has_otg);
118f427eb64SKuninori Morimoto 
119f427eb64SKuninori Morimoto 	if (has_otg)
120f427eb64SKuninori Morimoto 		usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN));
121f1407d5cSKuninori Morimoto 
122f1407d5cSKuninori Morimoto 	/*
123f1407d5cSKuninori Morimoto 	 * if enable
124f1407d5cSKuninori Morimoto 	 *
125f1407d5cSKuninori Morimoto 	 * - select Host mode
126f1407d5cSKuninori Morimoto 	 * - D+ Line/D- Line Pull-down
127f1407d5cSKuninori Morimoto 	 */
128f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
129f1407d5cSKuninori Morimoto }
130f1407d5cSKuninori Morimoto 
131f1407d5cSKuninori Morimoto void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
132f1407d5cSKuninori Morimoto {
133f1407d5cSKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU;
134f1407d5cSKuninori Morimoto 	u16 val  = DPRPU;
135f1407d5cSKuninori Morimoto 
136f1407d5cSKuninori Morimoto 	/*
137f1407d5cSKuninori Morimoto 	 * if enable
138f1407d5cSKuninori Morimoto 	 *
139f1407d5cSKuninori Morimoto 	 * - select Function mode
140f1407d5cSKuninori Morimoto 	 * - D+ Line Pull-up
141f1407d5cSKuninori Morimoto 	 */
142f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
143f1407d5cSKuninori Morimoto }
144f1407d5cSKuninori Morimoto 
145f1407d5cSKuninori Morimoto /*
146f1407d5cSKuninori Morimoto  *		frame functions
147f1407d5cSKuninori Morimoto  */
148f1407d5cSKuninori Morimoto int usbhs_frame_get_num(struct usbhs_priv *priv)
149f1407d5cSKuninori Morimoto {
150f1407d5cSKuninori Morimoto 	return usbhs_read(priv, FRMNUM) & FRNM_MASK;
151f1407d5cSKuninori Morimoto }
152f1407d5cSKuninori Morimoto 
153f1407d5cSKuninori Morimoto /*
154ef8bedb9SKuninori Morimoto  *		usb request functions
155ef8bedb9SKuninori Morimoto  */
156ef8bedb9SKuninori Morimoto void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
157ef8bedb9SKuninori Morimoto {
158ef8bedb9SKuninori Morimoto 	u16 val;
159ef8bedb9SKuninori Morimoto 
160ef8bedb9SKuninori Morimoto 	val = usbhs_read(priv, USBREQ);
161ef8bedb9SKuninori Morimoto 	req->bRequest		= (val >> 8) & 0xFF;
162ef8bedb9SKuninori Morimoto 	req->bRequestType	= (val >> 0) & 0xFF;
163ef8bedb9SKuninori Morimoto 
164ef8bedb9SKuninori Morimoto 	req->wValue	= usbhs_read(priv, USBVAL);
165ef8bedb9SKuninori Morimoto 	req->wIndex	= usbhs_read(priv, USBINDX);
166ef8bedb9SKuninori Morimoto 	req->wLength	= usbhs_read(priv, USBLENG);
167ef8bedb9SKuninori Morimoto }
168ef8bedb9SKuninori Morimoto 
169ef8bedb9SKuninori Morimoto void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
170ef8bedb9SKuninori Morimoto {
171ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBREQ,  (req->bRequest << 8) | req->bRequestType);
172ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBVAL,  req->wValue);
173ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBINDX, req->wIndex);
174ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBLENG, req->wLength);
175ef8bedb9SKuninori Morimoto 
176ef8bedb9SKuninori Morimoto 	usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
177ef8bedb9SKuninori Morimoto }
178ef8bedb9SKuninori Morimoto 
179ef8bedb9SKuninori Morimoto /*
180258485d9SKuninori Morimoto  *		bus/vbus functions
181258485d9SKuninori Morimoto  */
182258485d9SKuninori Morimoto void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
183258485d9SKuninori Morimoto {
184a9be4a45SKuninori Morimoto 	u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
185a9be4a45SKuninori Morimoto 
186a9be4a45SKuninori Morimoto 	if (status != USBRST) {
187a9be4a45SKuninori Morimoto 		struct device *dev = usbhs_priv_to_dev(priv);
188a9be4a45SKuninori Morimoto 		dev_err(dev, "usbhs should be reset\n");
189a9be4a45SKuninori Morimoto 	}
190a9be4a45SKuninori Morimoto 
191258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
192258485d9SKuninori Morimoto }
193258485d9SKuninori Morimoto 
194258485d9SKuninori Morimoto void usbhs_bus_send_reset(struct usbhs_priv *priv)
195258485d9SKuninori Morimoto {
196258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
197258485d9SKuninori Morimoto }
198258485d9SKuninori Morimoto 
19975587f52SKuninori Morimoto int usbhs_bus_get_speed(struct usbhs_priv *priv)
20075587f52SKuninori Morimoto {
20175587f52SKuninori Morimoto 	u16 dvstctr = usbhs_read(priv, DVSTCTR);
20275587f52SKuninori Morimoto 
20375587f52SKuninori Morimoto 	switch (RHST & dvstctr) {
20475587f52SKuninori Morimoto 	case RHST_LOW_SPEED:
20575587f52SKuninori Morimoto 		return USB_SPEED_LOW;
20675587f52SKuninori Morimoto 	case RHST_FULL_SPEED:
20775587f52SKuninori Morimoto 		return USB_SPEED_FULL;
20875587f52SKuninori Morimoto 	case RHST_HIGH_SPEED:
20975587f52SKuninori Morimoto 		return USB_SPEED_HIGH;
21075587f52SKuninori Morimoto 	}
21175587f52SKuninori Morimoto 
21275587f52SKuninori Morimoto 	return USB_SPEED_UNKNOWN;
21375587f52SKuninori Morimoto }
21475587f52SKuninori Morimoto 
215258485d9SKuninori Morimoto int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
216258485d9SKuninori Morimoto {
217258485d9SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
218258485d9SKuninori Morimoto 
219258485d9SKuninori Morimoto 	return usbhs_platform_call(priv, set_vbus, pdev, enable);
220258485d9SKuninori Morimoto }
221258485d9SKuninori Morimoto 
222258485d9SKuninori Morimoto static void usbhsc_bus_init(struct usbhs_priv *priv)
223258485d9SKuninori Morimoto {
224258485d9SKuninori Morimoto 	usbhs_write(priv, DVSTCTR, 0);
225258485d9SKuninori Morimoto 
226258485d9SKuninori Morimoto 	usbhs_vbus_ctrl(priv, 0);
227258485d9SKuninori Morimoto }
228258485d9SKuninori Morimoto 
229258485d9SKuninori Morimoto /*
230eb05191fSKuninori Morimoto  *		device configuration
231eb05191fSKuninori Morimoto  */
232eb05191fSKuninori Morimoto int usbhs_set_device_speed(struct usbhs_priv *priv, int devnum,
233eb05191fSKuninori Morimoto 			   u16 upphub, u16 hubport, u16 speed)
234eb05191fSKuninori Morimoto {
235eb05191fSKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
236eb05191fSKuninori Morimoto 	u16 usbspd = 0;
237eb05191fSKuninori Morimoto 	u32 reg = DEVADD0 + (2 * devnum);
238eb05191fSKuninori Morimoto 
239eb05191fSKuninori Morimoto 	if (devnum > 10) {
240eb05191fSKuninori Morimoto 		dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
241eb05191fSKuninori Morimoto 		return -EIO;
242eb05191fSKuninori Morimoto 	}
243eb05191fSKuninori Morimoto 
244eb05191fSKuninori Morimoto 	if (upphub > 0xA) {
245eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported hub number %d\n", upphub);
246eb05191fSKuninori Morimoto 		return -EIO;
247eb05191fSKuninori Morimoto 	}
248eb05191fSKuninori Morimoto 
249eb05191fSKuninori Morimoto 	switch (speed) {
250eb05191fSKuninori Morimoto 	case USB_SPEED_LOW:
251eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_LOW;
252eb05191fSKuninori Morimoto 		break;
253eb05191fSKuninori Morimoto 	case USB_SPEED_FULL:
254eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_FULL;
255eb05191fSKuninori Morimoto 		break;
256eb05191fSKuninori Morimoto 	case USB_SPEED_HIGH:
257eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_HIGH;
258eb05191fSKuninori Morimoto 		break;
259eb05191fSKuninori Morimoto 	default:
260eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported speed %d\n", speed);
261eb05191fSKuninori Morimoto 		return -EIO;
262eb05191fSKuninori Morimoto 	}
263eb05191fSKuninori Morimoto 
264eb05191fSKuninori Morimoto 	usbhs_write(priv, reg,	UPPHUB(upphub)	|
265eb05191fSKuninori Morimoto 				HUBPORT(hubport)|
266eb05191fSKuninori Morimoto 				USBSPD(usbspd));
267eb05191fSKuninori Morimoto 
268eb05191fSKuninori Morimoto 	return 0;
269eb05191fSKuninori Morimoto }
270eb05191fSKuninori Morimoto 
271eb05191fSKuninori Morimoto /*
272f1407d5cSKuninori Morimoto  *		local functions
273f1407d5cSKuninori Morimoto  */
27411935de5SKuninori Morimoto static void usbhsc_set_buswait(struct usbhs_priv *priv)
275f1407d5cSKuninori Morimoto {
276f1407d5cSKuninori Morimoto 	int wait = usbhs_get_dparam(priv, buswait_bwait);
277f1407d5cSKuninori Morimoto 
278f1407d5cSKuninori Morimoto 	/* set bus wait if platform have */
279f1407d5cSKuninori Morimoto 	if (wait)
280f1407d5cSKuninori Morimoto 		usbhs_bset(priv, BUSWAIT, 0x000F, wait);
281f1407d5cSKuninori Morimoto }
282f1407d5cSKuninori Morimoto 
283f1407d5cSKuninori Morimoto /*
284f1407d5cSKuninori Morimoto  *		platform default param
285f1407d5cSKuninori Morimoto  */
286f1407d5cSKuninori Morimoto static u32 usbhsc_default_pipe_type[] = {
287f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_CONTROL,
288f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_ISOC,
289f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_ISOC,
290f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_BULK,
291f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_BULK,
292f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_BULK,
293f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_INT,
294f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_INT,
295f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_INT,
296f1407d5cSKuninori Morimoto 		USB_ENDPOINT_XFER_INT,
297f1407d5cSKuninori Morimoto };
298f1407d5cSKuninori Morimoto 
299f1407d5cSKuninori Morimoto /*
3006e267da8SKuninori Morimoto  *		power control
3016e267da8SKuninori Morimoto  */
3026e267da8SKuninori Morimoto static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
3036e267da8SKuninori Morimoto {
3046e267da8SKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
3056e267da8SKuninori Morimoto 
3066e267da8SKuninori Morimoto 	if (enable) {
3076e267da8SKuninori Morimoto 		/* enable PM */
3086e267da8SKuninori Morimoto 		pm_runtime_get_sync(dev);
3096e267da8SKuninori Morimoto 
3106e267da8SKuninori Morimoto 		/* USB on */
3116e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
3126e267da8SKuninori Morimoto 	} else {
3136e267da8SKuninori Morimoto 		/* USB off */
3146e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
3156e267da8SKuninori Morimoto 
3166e267da8SKuninori Morimoto 		/* disable PM */
3176e267da8SKuninori Morimoto 		pm_runtime_put_sync(dev);
3186e267da8SKuninori Morimoto 	}
3196e267da8SKuninori Morimoto }
3206e267da8SKuninori Morimoto 
3216e267da8SKuninori Morimoto /*
322ca8a282aSKuninori Morimoto  *		hotplug
323f1407d5cSKuninori Morimoto  */
324ca8a282aSKuninori Morimoto static void usbhsc_hotplug(struct usbhs_priv *priv)
325f1407d5cSKuninori Morimoto {
326f1407d5cSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
327f1407d5cSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
328f1407d5cSKuninori Morimoto 	int id;
329f1407d5cSKuninori Morimoto 	int enable;
330f1407d5cSKuninori Morimoto 	int ret;
331f1407d5cSKuninori Morimoto 
332f1407d5cSKuninori Morimoto 	/*
333f1407d5cSKuninori Morimoto 	 * get vbus status from platform
334f1407d5cSKuninori Morimoto 	 */
335f1407d5cSKuninori Morimoto 	enable = usbhs_platform_call(priv, get_vbus, pdev);
336f1407d5cSKuninori Morimoto 
337f1407d5cSKuninori Morimoto 	/*
338f1407d5cSKuninori Morimoto 	 * get id from platform
339f1407d5cSKuninori Morimoto 	 */
340f1407d5cSKuninori Morimoto 	id = usbhs_platform_call(priv, get_id, pdev);
341f1407d5cSKuninori Morimoto 
342f1407d5cSKuninori Morimoto 	if (enable && !mod) {
343f1407d5cSKuninori Morimoto 		ret = usbhs_mod_change(priv, id);
344f1407d5cSKuninori Morimoto 		if (ret < 0)
345f1407d5cSKuninori Morimoto 			return;
346f1407d5cSKuninori Morimoto 
347f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s enable\n", __func__);
348f1407d5cSKuninori Morimoto 
3496e267da8SKuninori Morimoto 		/* power on */
350b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
3516e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
352f1407d5cSKuninori Morimoto 
353258485d9SKuninori Morimoto 		/* bus init */
354258485d9SKuninori Morimoto 		usbhsc_set_buswait(priv);
355258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
356258485d9SKuninori Morimoto 
357f1407d5cSKuninori Morimoto 		/* module start */
358f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, start, priv);
359f1407d5cSKuninori Morimoto 
360f1407d5cSKuninori Morimoto 	} else if (!enable && mod) {
361f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s disable\n", __func__);
362f1407d5cSKuninori Morimoto 
363f1407d5cSKuninori Morimoto 		/* module stop */
364f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
365f1407d5cSKuninori Morimoto 
366258485d9SKuninori Morimoto 		/* bus init */
367258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
368258485d9SKuninori Morimoto 
3696e267da8SKuninori Morimoto 		/* power off */
370b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
3716e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
372f1407d5cSKuninori Morimoto 
373f1407d5cSKuninori Morimoto 		usbhs_mod_change(priv, -1);
374f1407d5cSKuninori Morimoto 
375f1407d5cSKuninori Morimoto 		/* reset phy for next connection */
376f1407d5cSKuninori Morimoto 		usbhs_platform_call(priv, phy_reset, pdev);
377f1407d5cSKuninori Morimoto 	}
378f1407d5cSKuninori Morimoto }
379f1407d5cSKuninori Morimoto 
380ca8a282aSKuninori Morimoto /*
381ca8a282aSKuninori Morimoto  *		notify hotplug
382ca8a282aSKuninori Morimoto  */
383ca8a282aSKuninori Morimoto static void usbhsc_notify_hotplug(struct work_struct *work)
384ca8a282aSKuninori Morimoto {
385ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = container_of(work,
386ca8a282aSKuninori Morimoto 					       struct usbhs_priv,
387ca8a282aSKuninori Morimoto 					       notify_hotplug_work.work);
388ca8a282aSKuninori Morimoto 	usbhsc_hotplug(priv);
389ca8a282aSKuninori Morimoto }
390ca8a282aSKuninori Morimoto 
391bc57381eSKuninori Morimoto int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
392f1407d5cSKuninori Morimoto {
393206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
394bc57381eSKuninori Morimoto 	int delay = usbhs_get_dparam(priv, detection_delay);
395f1407d5cSKuninori Morimoto 
396f1407d5cSKuninori Morimoto 	/*
397f1407d5cSKuninori Morimoto 	 * This functions will be called in interrupt.
398f1407d5cSKuninori Morimoto 	 * To make sure safety context,
399f1407d5cSKuninori Morimoto 	 * use workqueue for usbhs_notify_hotplug
400f1407d5cSKuninori Morimoto 	 */
401bc57381eSKuninori Morimoto 	schedule_delayed_work(&priv->notify_hotplug_work, delay);
402f1407d5cSKuninori Morimoto 	return 0;
403f1407d5cSKuninori Morimoto }
404f1407d5cSKuninori Morimoto 
405f1407d5cSKuninori Morimoto /*
406f1407d5cSKuninori Morimoto  *		platform functions
407f1407d5cSKuninori Morimoto  */
408b7a8d17dSKuninori Morimoto static int usbhs_probe(struct platform_device *pdev)
409f1407d5cSKuninori Morimoto {
410f1407d5cSKuninori Morimoto 	struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
411f1407d5cSKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc;
412f1407d5cSKuninori Morimoto 	struct usbhs_priv *priv;
413f1407d5cSKuninori Morimoto 	struct resource *res;
414f1407d5cSKuninori Morimoto 	unsigned int irq;
415f1407d5cSKuninori Morimoto 	int ret;
416f1407d5cSKuninori Morimoto 
417f1407d5cSKuninori Morimoto 	/* check platform information */
418f1407d5cSKuninori Morimoto 	if (!info ||
419b002ff6eSKuninori Morimoto 	    !info->platform_callback.get_id) {
420f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "no platform information\n");
421f1407d5cSKuninori Morimoto 		return -EINVAL;
422f1407d5cSKuninori Morimoto 	}
423f1407d5cSKuninori Morimoto 
424f1407d5cSKuninori Morimoto 	/* platform data */
425f1407d5cSKuninori Morimoto 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
426f1407d5cSKuninori Morimoto 	irq = platform_get_irq(pdev, 0);
427f1407d5cSKuninori Morimoto 	if (!res || (int)irq <= 0) {
428f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
429f1407d5cSKuninori Morimoto 		return -ENODEV;
430f1407d5cSKuninori Morimoto 	}
431f1407d5cSKuninori Morimoto 
432f1407d5cSKuninori Morimoto 	/* usb private data */
433f1407d5cSKuninori Morimoto 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
434f1407d5cSKuninori Morimoto 	if (!priv) {
435f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "Could not allocate priv\n");
436f1407d5cSKuninori Morimoto 		return -ENOMEM;
437f1407d5cSKuninori Morimoto 	}
438f1407d5cSKuninori Morimoto 
439f1407d5cSKuninori Morimoto 	priv->base = ioremap_nocache(res->start, resource_size(res));
440f1407d5cSKuninori Morimoto 	if (!priv->base) {
441f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "ioremap error.\n");
442f1407d5cSKuninori Morimoto 		ret = -ENOMEM;
443f1407d5cSKuninori Morimoto 		goto probe_end_kfree;
444f1407d5cSKuninori Morimoto 	}
445f1407d5cSKuninori Morimoto 
446f1407d5cSKuninori Morimoto 	/*
447f1407d5cSKuninori Morimoto 	 * care platform info
448f1407d5cSKuninori Morimoto 	 */
44948298206SKuninori Morimoto 	memcpy(&priv->pfunc,
45048298206SKuninori Morimoto 	       &info->platform_callback,
45148298206SKuninori Morimoto 	       sizeof(struct renesas_usbhs_platform_callback));
45248298206SKuninori Morimoto 	memcpy(&priv->dparam,
45348298206SKuninori Morimoto 	       &info->driver_param,
45448298206SKuninori Morimoto 	       sizeof(struct renesas_usbhs_driver_param));
455f1407d5cSKuninori Morimoto 
456f1407d5cSKuninori Morimoto 	/* set driver callback functions for platform */
457f1407d5cSKuninori Morimoto 	dfunc			= &info->driver_callback;
458f1407d5cSKuninori Morimoto 	dfunc->notify_hotplug	= usbhsc_drvcllbck_notify_hotplug;
459f1407d5cSKuninori Morimoto 
460f1407d5cSKuninori Morimoto 	/* set default param if platform doesn't have */
46148298206SKuninori Morimoto 	if (!priv->dparam.pipe_type) {
46248298206SKuninori Morimoto 		priv->dparam.pipe_type = usbhsc_default_pipe_type;
46348298206SKuninori Morimoto 		priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe_type);
464f1407d5cSKuninori Morimoto 	}
46548298206SKuninori Morimoto 	if (!priv->dparam.pio_dma_border)
46648298206SKuninori Morimoto 		priv->dparam.pio_dma_border = 64; /* 64byte */
467f1407d5cSKuninori Morimoto 
468b002ff6eSKuninori Morimoto 	/* FIXME */
469b002ff6eSKuninori Morimoto 	/* runtime power control ? */
47048298206SKuninori Morimoto 	if (priv->pfunc.get_vbus)
471b002ff6eSKuninori Morimoto 		usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
472b002ff6eSKuninori Morimoto 
473f1407d5cSKuninori Morimoto 	/*
474f1407d5cSKuninori Morimoto 	 * priv settings
475f1407d5cSKuninori Morimoto 	 */
476f1407d5cSKuninori Morimoto 	priv->irq	= irq;
477f1407d5cSKuninori Morimoto 	priv->pdev	= pdev;
478bc57381eSKuninori Morimoto 	INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
479f1407d5cSKuninori Morimoto 	spin_lock_init(usbhs_priv_to_lock(priv));
480f1407d5cSKuninori Morimoto 
481f1407d5cSKuninori Morimoto 	/* call pipe and module init */
482f1407d5cSKuninori Morimoto 	ret = usbhs_pipe_probe(priv);
483f1407d5cSKuninori Morimoto 	if (ret < 0)
48497f93227SKuninori Morimoto 		goto probe_end_iounmap;
485f1407d5cSKuninori Morimoto 
486d3af90a5SKuninori Morimoto 	ret = usbhs_fifo_probe(priv);
487f1407d5cSKuninori Morimoto 	if (ret < 0)
48897f93227SKuninori Morimoto 		goto probe_end_pipe_exit;
489f1407d5cSKuninori Morimoto 
490d3af90a5SKuninori Morimoto 	ret = usbhs_mod_probe(priv);
491d3af90a5SKuninori Morimoto 	if (ret < 0)
492d3af90a5SKuninori Morimoto 		goto probe_end_fifo_exit;
493d3af90a5SKuninori Morimoto 
494f1407d5cSKuninori Morimoto 	/* dev_set_drvdata should be called after usbhs_mod_init */
495f1407d5cSKuninori Morimoto 	dev_set_drvdata(&pdev->dev, priv);
496f1407d5cSKuninori Morimoto 
497f1407d5cSKuninori Morimoto 	/*
498f1407d5cSKuninori Morimoto 	 * deviece reset here because
499f1407d5cSKuninori Morimoto 	 * USB device might be used in boot loader.
500f1407d5cSKuninori Morimoto 	 */
501f1407d5cSKuninori Morimoto 	usbhs_sys_clock_ctrl(priv, 0);
502f1407d5cSKuninori Morimoto 
503f1407d5cSKuninori Morimoto 	/*
504f1407d5cSKuninori Morimoto 	 * platform call
505f1407d5cSKuninori Morimoto 	 *
506f1407d5cSKuninori Morimoto 	 * USB phy setup might depend on CPU/Board.
507f1407d5cSKuninori Morimoto 	 * If platform has its callback functions,
508f1407d5cSKuninori Morimoto 	 * call it here.
509f1407d5cSKuninori Morimoto 	 */
510f1407d5cSKuninori Morimoto 	ret = usbhs_platform_call(priv, hardware_init, pdev);
511f1407d5cSKuninori Morimoto 	if (ret < 0) {
512f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "platform prove failed.\n");
51397f93227SKuninori Morimoto 		goto probe_end_mod_exit;
514f1407d5cSKuninori Morimoto 	}
515f1407d5cSKuninori Morimoto 
516f1407d5cSKuninori Morimoto 	/* reset phy for connection */
517f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
518f1407d5cSKuninori Morimoto 
519b002ff6eSKuninori Morimoto 	/* power control */
520b002ff6eSKuninori Morimoto 	pm_runtime_enable(&pdev->dev);
521b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
522b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
523b002ff6eSKuninori Morimoto 		usbhs_mod_autonomy_mode(priv);
524b002ff6eSKuninori Morimoto 	}
525b002ff6eSKuninori Morimoto 
526f1407d5cSKuninori Morimoto 	/*
527f1407d5cSKuninori Morimoto 	 * manual call notify_hotplug for cold plug
528f1407d5cSKuninori Morimoto 	 */
529f1407d5cSKuninori Morimoto 	ret = usbhsc_drvcllbck_notify_hotplug(pdev);
530f1407d5cSKuninori Morimoto 	if (ret < 0)
531f1407d5cSKuninori Morimoto 		goto probe_end_call_remove;
532f1407d5cSKuninori Morimoto 
533f1407d5cSKuninori Morimoto 	dev_info(&pdev->dev, "probed\n");
534f1407d5cSKuninori Morimoto 
535f1407d5cSKuninori Morimoto 	return ret;
536f1407d5cSKuninori Morimoto 
537f1407d5cSKuninori Morimoto probe_end_call_remove:
538f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, hardware_exit, pdev);
539f1407d5cSKuninori Morimoto probe_end_mod_exit:
540f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
541d3af90a5SKuninori Morimoto probe_end_fifo_exit:
542d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
54397f93227SKuninori Morimoto probe_end_pipe_exit:
54497f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
545f1407d5cSKuninori Morimoto probe_end_iounmap:
546f1407d5cSKuninori Morimoto 	iounmap(priv->base);
547f1407d5cSKuninori Morimoto probe_end_kfree:
548f1407d5cSKuninori Morimoto 	kfree(priv);
549f1407d5cSKuninori Morimoto 
550f1407d5cSKuninori Morimoto 	dev_info(&pdev->dev, "probe failed\n");
551f1407d5cSKuninori Morimoto 
552f1407d5cSKuninori Morimoto 	return ret;
553f1407d5cSKuninori Morimoto }
554f1407d5cSKuninori Morimoto 
555f1407d5cSKuninori Morimoto static int __devexit usbhs_remove(struct platform_device *pdev)
556f1407d5cSKuninori Morimoto {
557206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
558af32fe51SKuninori Morimoto 	struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
559af32fe51SKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
560f1407d5cSKuninori Morimoto 
561f1407d5cSKuninori Morimoto 	dev_dbg(&pdev->dev, "usb remove\n");
562f1407d5cSKuninori Morimoto 
563af32fe51SKuninori Morimoto 	dfunc->notify_hotplug = NULL;
564af32fe51SKuninori Morimoto 
565b002ff6eSKuninori Morimoto 	/* power off */
566b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
567b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
568f1407d5cSKuninori Morimoto 
569b002ff6eSKuninori Morimoto 	pm_runtime_disable(&pdev->dev);
570f1407d5cSKuninori Morimoto 
571f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, hardware_exit, pdev);
572f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
573d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
57497f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
575f1407d5cSKuninori Morimoto 	iounmap(priv->base);
576f1407d5cSKuninori Morimoto 	kfree(priv);
577f1407d5cSKuninori Morimoto 
578f1407d5cSKuninori Morimoto 	return 0;
579f1407d5cSKuninori Morimoto }
580f1407d5cSKuninori Morimoto 
581ca8a282aSKuninori Morimoto static int usbhsc_suspend(struct device *dev)
582ca8a282aSKuninori Morimoto {
583ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
584ca8a282aSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
585ca8a282aSKuninori Morimoto 
586ca8a282aSKuninori Morimoto 	if (mod) {
587ca8a282aSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
588ca8a282aSKuninori Morimoto 		usbhs_mod_change(priv, -1);
589ca8a282aSKuninori Morimoto 	}
590ca8a282aSKuninori Morimoto 
591ca8a282aSKuninori Morimoto 	if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
592ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
593ca8a282aSKuninori Morimoto 
594ca8a282aSKuninori Morimoto 	return 0;
595ca8a282aSKuninori Morimoto }
596ca8a282aSKuninori Morimoto 
597ca8a282aSKuninori Morimoto static int usbhsc_resume(struct device *dev)
598ca8a282aSKuninori Morimoto {
599ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
600ca8a282aSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
601ca8a282aSKuninori Morimoto 
602ca8a282aSKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
603ca8a282aSKuninori Morimoto 
604ca8a282aSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
605ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
606ca8a282aSKuninori Morimoto 
607ca8a282aSKuninori Morimoto 	usbhsc_hotplug(priv);
608ca8a282aSKuninori Morimoto 
609ca8a282aSKuninori Morimoto 	return 0;
610ca8a282aSKuninori Morimoto }
611ca8a282aSKuninori Morimoto 
612ca8a282aSKuninori Morimoto static int usbhsc_runtime_nop(struct device *dev)
613ca8a282aSKuninori Morimoto {
614ca8a282aSKuninori Morimoto 	/* Runtime PM callback shared between ->runtime_suspend()
615ca8a282aSKuninori Morimoto 	 * and ->runtime_resume(). Simply returns success.
616ca8a282aSKuninori Morimoto 	 *
617ca8a282aSKuninori Morimoto 	 * This driver re-initializes all registers after
618ca8a282aSKuninori Morimoto 	 * pm_runtime_get_sync() anyway so there is no need
619ca8a282aSKuninori Morimoto 	 * to save and restore registers here.
620ca8a282aSKuninori Morimoto 	 */
621ca8a282aSKuninori Morimoto 	return 0;
622ca8a282aSKuninori Morimoto }
623ca8a282aSKuninori Morimoto 
624ca8a282aSKuninori Morimoto static const struct dev_pm_ops usbhsc_pm_ops = {
625ca8a282aSKuninori Morimoto 	.suspend		= usbhsc_suspend,
626ca8a282aSKuninori Morimoto 	.resume			= usbhsc_resume,
627ca8a282aSKuninori Morimoto 	.runtime_suspend	= usbhsc_runtime_nop,
628ca8a282aSKuninori Morimoto 	.runtime_resume		= usbhsc_runtime_nop,
629ca8a282aSKuninori Morimoto };
630ca8a282aSKuninori Morimoto 
631f1407d5cSKuninori Morimoto static struct platform_driver renesas_usbhs_driver = {
632f1407d5cSKuninori Morimoto 	.driver		= {
633f1407d5cSKuninori Morimoto 		.name	= "renesas_usbhs",
634ca8a282aSKuninori Morimoto 		.pm	= &usbhsc_pm_ops,
635f1407d5cSKuninori Morimoto 	},
636f1407d5cSKuninori Morimoto 	.probe		= usbhs_probe,
637f1407d5cSKuninori Morimoto 	.remove		= __devexit_p(usbhs_remove),
638f1407d5cSKuninori Morimoto };
639f1407d5cSKuninori Morimoto 
640*cc27c96cSAxel Lin module_platform_driver(renesas_usbhs_driver);
641f1407d5cSKuninori Morimoto 
642f1407d5cSKuninori Morimoto MODULE_LICENSE("GPL");
643f1407d5cSKuninori Morimoto MODULE_DESCRIPTION("Renesas USB driver");
644f1407d5cSKuninori Morimoto MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
645