xref: /openbmc/linux/drivers/usb/renesas_usbhs/common.c (revision fb4e98ab63433c4d3a1588ea91c73f1cd7ebaa00)
1f1407d5cSKuninori Morimoto /*
2f1407d5cSKuninori Morimoto  * Renesas USB driver
3f1407d5cSKuninori Morimoto  *
4f1407d5cSKuninori Morimoto  * Copyright (C) 2011 Renesas Solutions Corp.
5f1407d5cSKuninori Morimoto  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6f1407d5cSKuninori Morimoto  *
7f1407d5cSKuninori Morimoto  * This program is distributed in the hope that it will be useful,
8f1407d5cSKuninori Morimoto  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9f1407d5cSKuninori Morimoto  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10f1407d5cSKuninori Morimoto  * GNU General Public License for more details.
11f1407d5cSKuninori Morimoto  *
12f1407d5cSKuninori Morimoto  * You should have received a copy of the GNU General Public License
13f1407d5cSKuninori Morimoto  * along with this program; if not, write to the Free Software
14f1407d5cSKuninori Morimoto  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15f1407d5cSKuninori Morimoto  *
16f1407d5cSKuninori Morimoto  */
17f1407d5cSKuninori Morimoto #include <linux/io.h>
18f1407d5cSKuninori Morimoto #include <linux/module.h>
19f1407d5cSKuninori Morimoto #include <linux/pm_runtime.h>
20f1407d5cSKuninori Morimoto #include <linux/slab.h>
21f1407d5cSKuninori Morimoto #include <linux/sysfs.h>
22cc502bb7SPaul Bolle #include "common.h"
23f1407d5cSKuninori Morimoto 
24233f519dSKuninori Morimoto /*
25233f519dSKuninori Morimoto  *		image of renesas_usbhs
26233f519dSKuninori Morimoto  *
27233f519dSKuninori Morimoto  * ex) gadget case
28233f519dSKuninori Morimoto 
29233f519dSKuninori Morimoto  * mod.c
30233f519dSKuninori Morimoto  * mod_gadget.c
31233f519dSKuninori Morimoto  * mod_host.c		pipe.c		fifo.c
32233f519dSKuninori Morimoto  *
33233f519dSKuninori Morimoto  *			+-------+	+-----------+
34233f519dSKuninori Morimoto  *			| pipe0 |------>| fifo pio  |
35233f519dSKuninori Morimoto  * +------------+	+-------+	+-----------+
36233f519dSKuninori Morimoto  * | mod_gadget |=====> | pipe1 |--+
37233f519dSKuninori Morimoto  * +------------+	+-------+  |	+-----------+
38233f519dSKuninori Morimoto  *			| pipe2 |  |  +-| fifo dma0 |
39233f519dSKuninori Morimoto  * +------------+	+-------+  |  |	+-----------+
40233f519dSKuninori Morimoto  * | mod_host   |	| pipe3 |<-|--+
41233f519dSKuninori Morimoto  * +------------+	+-------+  |	+-----------+
42233f519dSKuninori Morimoto  *			| ....  |  +--->| fifo dma1 |
43233f519dSKuninori Morimoto  *			| ....  |	+-----------+
44233f519dSKuninori Morimoto  */
45233f519dSKuninori Morimoto 
46233f519dSKuninori Morimoto 
47b002ff6eSKuninori Morimoto #define USBHSF_RUNTIME_PWCTRL	(1 << 0)
48b002ff6eSKuninori Morimoto 
49b002ff6eSKuninori Morimoto /* status */
50b002ff6eSKuninori Morimoto #define usbhsc_flags_init(p)   do {(p)->flags = 0; } while (0)
51b002ff6eSKuninori Morimoto #define usbhsc_flags_set(p, b) ((p)->flags |=  (b))
52b002ff6eSKuninori Morimoto #define usbhsc_flags_clr(p, b) ((p)->flags &= ~(b))
53b002ff6eSKuninori Morimoto #define usbhsc_flags_has(p, b) ((p)->flags &   (b))
54b002ff6eSKuninori Morimoto 
55f1407d5cSKuninori Morimoto /*
56f1407d5cSKuninori Morimoto  * platform call back
57f1407d5cSKuninori Morimoto  *
58f1407d5cSKuninori Morimoto  * renesas usb support platform callback function.
59f1407d5cSKuninori Morimoto  * Below macro call it.
60f1407d5cSKuninori Morimoto  * if platform doesn't have callback, it return 0 (no error)
61f1407d5cSKuninori Morimoto  */
62f1407d5cSKuninori Morimoto #define usbhs_platform_call(priv, func, args...)\
63f1407d5cSKuninori Morimoto 	(!(priv) ? -ENODEV :			\
6448298206SKuninori Morimoto 	 !((priv)->pfunc.func) ? 0 :		\
6548298206SKuninori Morimoto 	 (priv)->pfunc.func(args))
66f1407d5cSKuninori Morimoto 
67f1407d5cSKuninori Morimoto /*
68f1407d5cSKuninori Morimoto  *		common functions
69f1407d5cSKuninori Morimoto  */
70f1407d5cSKuninori Morimoto u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
71f1407d5cSKuninori Morimoto {
72f1407d5cSKuninori Morimoto 	return ioread16(priv->base + reg);
73f1407d5cSKuninori Morimoto }
74f1407d5cSKuninori Morimoto 
75f1407d5cSKuninori Morimoto void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
76f1407d5cSKuninori Morimoto {
77f1407d5cSKuninori Morimoto 	iowrite16(data, priv->base + reg);
78f1407d5cSKuninori Morimoto }
79f1407d5cSKuninori Morimoto 
80f1407d5cSKuninori Morimoto void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
81f1407d5cSKuninori Morimoto {
82f1407d5cSKuninori Morimoto 	u16 val = usbhs_read(priv, reg);
83f1407d5cSKuninori Morimoto 
84f1407d5cSKuninori Morimoto 	val &= ~mask;
85f1407d5cSKuninori Morimoto 	val |= data & mask;
86f1407d5cSKuninori Morimoto 
87f1407d5cSKuninori Morimoto 	usbhs_write(priv, reg, val);
88f1407d5cSKuninori Morimoto }
89f1407d5cSKuninori Morimoto 
90206dcc2cSKuninori Morimoto struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
91206dcc2cSKuninori Morimoto {
92206dcc2cSKuninori Morimoto 	return dev_get_drvdata(&pdev->dev);
93206dcc2cSKuninori Morimoto }
94206dcc2cSKuninori Morimoto 
95f1407d5cSKuninori Morimoto /*
96f1407d5cSKuninori Morimoto  *		syscfg functions
97f1407d5cSKuninori Morimoto  */
9876190152SKuninori Morimoto static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
99f1407d5cSKuninori Morimoto {
100f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
101f1407d5cSKuninori Morimoto }
102f1407d5cSKuninori Morimoto 
103f1407d5cSKuninori Morimoto void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
104f1407d5cSKuninori Morimoto {
1053244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
1063244a7b4SKuninori Morimoto 	u16 val  = DCFM | DRPD | HSE | USBE;
107f427eb64SKuninori Morimoto 	int has_otg = usbhs_get_dparam(priv, has_otg);
108f427eb64SKuninori Morimoto 
109f427eb64SKuninori Morimoto 	if (has_otg)
110f427eb64SKuninori Morimoto 		usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN));
111f1407d5cSKuninori Morimoto 
112f1407d5cSKuninori Morimoto 	/*
113f1407d5cSKuninori Morimoto 	 * if enable
114f1407d5cSKuninori Morimoto 	 *
115f1407d5cSKuninori Morimoto 	 * - select Host mode
116f1407d5cSKuninori Morimoto 	 * - D+ Line/D- Line Pull-down
117f1407d5cSKuninori Morimoto 	 */
118f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
119f1407d5cSKuninori Morimoto }
120f1407d5cSKuninori Morimoto 
121f1407d5cSKuninori Morimoto void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
122f1407d5cSKuninori Morimoto {
1233244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
1243244a7b4SKuninori Morimoto 	u16 val  = DPRPU | HSE | USBE;
125f1407d5cSKuninori Morimoto 
126f1407d5cSKuninori Morimoto 	/*
127f1407d5cSKuninori Morimoto 	 * if enable
128f1407d5cSKuninori Morimoto 	 *
129f1407d5cSKuninori Morimoto 	 * - select Function mode
130f1407d5cSKuninori Morimoto 	 * - D+ Line Pull-up
131f1407d5cSKuninori Morimoto 	 */
132f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
133f1407d5cSKuninori Morimoto }
134f1407d5cSKuninori Morimoto 
1354cd2f599Skuninori.morimoto.gx@renesas.com void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable)
1364cd2f599Skuninori.morimoto.gx@renesas.com {
1374cd2f599Skuninori.morimoto.gx@renesas.com 	usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0);
1384cd2f599Skuninori.morimoto.gx@renesas.com }
1394cd2f599Skuninori.morimoto.gx@renesas.com 
140dfbb7f4fSKuninori Morimoto void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
141dfbb7f4fSKuninori Morimoto {
142dfbb7f4fSKuninori Morimoto 	usbhs_write(priv, TESTMODE, mode);
143dfbb7f4fSKuninori Morimoto }
144dfbb7f4fSKuninori 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  */
2323dd49268SKuninori Morimoto int usbhs_set_device_config(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 {
304f1ee56a0SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
3056e267da8SKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
3066e267da8SKuninori Morimoto 
3076e267da8SKuninori Morimoto 	if (enable) {
3086e267da8SKuninori Morimoto 		/* enable PM */
3096e267da8SKuninori Morimoto 		pm_runtime_get_sync(dev);
3106e267da8SKuninori Morimoto 
311f1ee56a0SKuninori Morimoto 		/* enable platform power */
312f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
313f1ee56a0SKuninori Morimoto 
3146e267da8SKuninori Morimoto 		/* USB on */
3156e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
3166e267da8SKuninori Morimoto 	} else {
3176e267da8SKuninori Morimoto 		/* USB off */
3186e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
3196e267da8SKuninori Morimoto 
320f1ee56a0SKuninori Morimoto 		/* disable platform power */
321f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
322f1ee56a0SKuninori Morimoto 
3236e267da8SKuninori Morimoto 		/* disable PM */
3246e267da8SKuninori Morimoto 		pm_runtime_put_sync(dev);
3256e267da8SKuninori Morimoto 	}
3266e267da8SKuninori Morimoto }
3276e267da8SKuninori Morimoto 
3286e267da8SKuninori Morimoto /*
329ca8a282aSKuninori Morimoto  *		hotplug
330f1407d5cSKuninori Morimoto  */
331ca8a282aSKuninori Morimoto static void usbhsc_hotplug(struct usbhs_priv *priv)
332f1407d5cSKuninori Morimoto {
333f1407d5cSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
334f1407d5cSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
335f1407d5cSKuninori Morimoto 	int id;
336f1407d5cSKuninori Morimoto 	int enable;
337f1407d5cSKuninori Morimoto 	int ret;
338f1407d5cSKuninori Morimoto 
339f1407d5cSKuninori Morimoto 	/*
340f1407d5cSKuninori Morimoto 	 * get vbus status from platform
341f1407d5cSKuninori Morimoto 	 */
342f1407d5cSKuninori Morimoto 	enable = usbhs_platform_call(priv, get_vbus, pdev);
343f1407d5cSKuninori Morimoto 
344f1407d5cSKuninori Morimoto 	/*
345f1407d5cSKuninori Morimoto 	 * get id from platform
346f1407d5cSKuninori Morimoto 	 */
347f1407d5cSKuninori Morimoto 	id = usbhs_platform_call(priv, get_id, pdev);
348f1407d5cSKuninori Morimoto 
349f1407d5cSKuninori Morimoto 	if (enable && !mod) {
350f1407d5cSKuninori Morimoto 		ret = usbhs_mod_change(priv, id);
351f1407d5cSKuninori Morimoto 		if (ret < 0)
352f1407d5cSKuninori Morimoto 			return;
353f1407d5cSKuninori Morimoto 
354f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s enable\n", __func__);
355f1407d5cSKuninori Morimoto 
3566e267da8SKuninori Morimoto 		/* power on */
357b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
3586e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
359f1407d5cSKuninori Morimoto 
360258485d9SKuninori Morimoto 		/* bus init */
361258485d9SKuninori Morimoto 		usbhsc_set_buswait(priv);
362258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
363258485d9SKuninori Morimoto 
364f1407d5cSKuninori Morimoto 		/* module start */
365f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, start, priv);
366f1407d5cSKuninori Morimoto 
367f1407d5cSKuninori Morimoto 	} else if (!enable && mod) {
368f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s disable\n", __func__);
369f1407d5cSKuninori Morimoto 
370f1407d5cSKuninori Morimoto 		/* module stop */
371f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
372f1407d5cSKuninori Morimoto 
373258485d9SKuninori Morimoto 		/* bus init */
374258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
375258485d9SKuninori Morimoto 
3766e267da8SKuninori Morimoto 		/* power off */
377b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
3786e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
379f1407d5cSKuninori Morimoto 
380f1407d5cSKuninori Morimoto 		usbhs_mod_change(priv, -1);
381f1407d5cSKuninori Morimoto 
382f1407d5cSKuninori Morimoto 		/* reset phy for next connection */
383f1407d5cSKuninori Morimoto 		usbhs_platform_call(priv, phy_reset, pdev);
384f1407d5cSKuninori Morimoto 	}
385f1407d5cSKuninori Morimoto }
386f1407d5cSKuninori Morimoto 
387ca8a282aSKuninori Morimoto /*
388ca8a282aSKuninori Morimoto  *		notify hotplug
389ca8a282aSKuninori Morimoto  */
390ca8a282aSKuninori Morimoto static void usbhsc_notify_hotplug(struct work_struct *work)
391ca8a282aSKuninori Morimoto {
392ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = container_of(work,
393ca8a282aSKuninori Morimoto 					       struct usbhs_priv,
394ca8a282aSKuninori Morimoto 					       notify_hotplug_work.work);
395ca8a282aSKuninori Morimoto 	usbhsc_hotplug(priv);
396ca8a282aSKuninori Morimoto }
397ca8a282aSKuninori Morimoto 
398b4fcea2aSKuninori Morimoto static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
399f1407d5cSKuninori Morimoto {
400206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
401bc57381eSKuninori Morimoto 	int delay = usbhs_get_dparam(priv, detection_delay);
402f1407d5cSKuninori Morimoto 
403f1407d5cSKuninori Morimoto 	/*
404f1407d5cSKuninori Morimoto 	 * This functions will be called in interrupt.
405f1407d5cSKuninori Morimoto 	 * To make sure safety context,
406f1407d5cSKuninori Morimoto 	 * use workqueue for usbhs_notify_hotplug
407f1407d5cSKuninori Morimoto 	 */
408a49a88f1SKuninori Morimoto 	schedule_delayed_work(&priv->notify_hotplug_work,
409a49a88f1SKuninori Morimoto 			      msecs_to_jiffies(delay));
410f1407d5cSKuninori Morimoto 	return 0;
411f1407d5cSKuninori Morimoto }
412f1407d5cSKuninori Morimoto 
413f1407d5cSKuninori Morimoto /*
414f1407d5cSKuninori Morimoto  *		platform functions
415f1407d5cSKuninori Morimoto  */
416b7a8d17dSKuninori Morimoto static int usbhs_probe(struct platform_device *pdev)
417f1407d5cSKuninori Morimoto {
418f1407d5cSKuninori Morimoto 	struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
419f1407d5cSKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc;
420f1407d5cSKuninori Morimoto 	struct usbhs_priv *priv;
42153069af3SShimoda, Yoshihiro 	struct resource *res, *irq_res;
422f1407d5cSKuninori Morimoto 	int ret;
423f1407d5cSKuninori Morimoto 
424f1407d5cSKuninori Morimoto 	/* check platform information */
425f1407d5cSKuninori Morimoto 	if (!info ||
426b002ff6eSKuninori Morimoto 	    !info->platform_callback.get_id) {
427f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "no platform information\n");
428f1407d5cSKuninori Morimoto 		return -EINVAL;
429f1407d5cSKuninori Morimoto 	}
430f1407d5cSKuninori Morimoto 
431f1407d5cSKuninori Morimoto 	/* platform data */
432f1407d5cSKuninori Morimoto 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
43353069af3SShimoda, Yoshihiro 	irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
43453069af3SShimoda, Yoshihiro 	if (!res || !irq_res) {
435f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
436f1407d5cSKuninori Morimoto 		return -ENODEV;
437f1407d5cSKuninori Morimoto 	}
438f1407d5cSKuninori Morimoto 
439f1407d5cSKuninori Morimoto 	/* usb private data */
44058efc77cSKuninori Morimoto 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
441f1407d5cSKuninori Morimoto 	if (!priv) {
442f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "Could not allocate priv\n");
443f1407d5cSKuninori Morimoto 		return -ENOMEM;
444f1407d5cSKuninori Morimoto 	}
445f1407d5cSKuninori Morimoto 
44658efc77cSKuninori Morimoto 	priv->base = devm_request_and_ioremap(&pdev->dev, res);
447f1407d5cSKuninori Morimoto 	if (!priv->base) {
448f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "ioremap error.\n");
44958efc77cSKuninori Morimoto 		return -ENOMEM;
450f1407d5cSKuninori Morimoto 	}
451f1407d5cSKuninori Morimoto 
452f1407d5cSKuninori Morimoto 	/*
453f1407d5cSKuninori Morimoto 	 * care platform info
454f1407d5cSKuninori Morimoto 	 */
45548298206SKuninori Morimoto 	memcpy(&priv->pfunc,
45648298206SKuninori Morimoto 	       &info->platform_callback,
45748298206SKuninori Morimoto 	       sizeof(struct renesas_usbhs_platform_callback));
45848298206SKuninori Morimoto 	memcpy(&priv->dparam,
45948298206SKuninori Morimoto 	       &info->driver_param,
46048298206SKuninori Morimoto 	       sizeof(struct renesas_usbhs_driver_param));
461f1407d5cSKuninori Morimoto 
462f1407d5cSKuninori Morimoto 	/* set driver callback functions for platform */
463f1407d5cSKuninori Morimoto 	dfunc			= &info->driver_callback;
464f1407d5cSKuninori Morimoto 	dfunc->notify_hotplug	= usbhsc_drvcllbck_notify_hotplug;
465f1407d5cSKuninori Morimoto 
466f1407d5cSKuninori Morimoto 	/* set default param if platform doesn't have */
46748298206SKuninori Morimoto 	if (!priv->dparam.pipe_type) {
46848298206SKuninori Morimoto 		priv->dparam.pipe_type = usbhsc_default_pipe_type;
46948298206SKuninori Morimoto 		priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe_type);
470f1407d5cSKuninori Morimoto 	}
47148298206SKuninori Morimoto 	if (!priv->dparam.pio_dma_border)
47248298206SKuninori Morimoto 		priv->dparam.pio_dma_border = 64; /* 64byte */
473f1407d5cSKuninori Morimoto 
474b002ff6eSKuninori Morimoto 	/* FIXME */
475b002ff6eSKuninori Morimoto 	/* runtime power control ? */
47648298206SKuninori Morimoto 	if (priv->pfunc.get_vbus)
477b002ff6eSKuninori Morimoto 		usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
478b002ff6eSKuninori Morimoto 
479f1407d5cSKuninori Morimoto 	/*
480f1407d5cSKuninori Morimoto 	 * priv settings
481f1407d5cSKuninori Morimoto 	 */
48253069af3SShimoda, Yoshihiro 	priv->irq	= irq_res->start;
48353069af3SShimoda, Yoshihiro 	if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
48453069af3SShimoda, Yoshihiro 		priv->irqflags = IRQF_SHARED;
485f1407d5cSKuninori Morimoto 	priv->pdev	= pdev;
486bc57381eSKuninori Morimoto 	INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
487f1407d5cSKuninori Morimoto 	spin_lock_init(usbhs_priv_to_lock(priv));
488f1407d5cSKuninori Morimoto 
489f1407d5cSKuninori Morimoto 	/* call pipe and module init */
490f1407d5cSKuninori Morimoto 	ret = usbhs_pipe_probe(priv);
491f1407d5cSKuninori Morimoto 	if (ret < 0)
49258efc77cSKuninori Morimoto 		return ret;
493f1407d5cSKuninori Morimoto 
494d3af90a5SKuninori Morimoto 	ret = usbhs_fifo_probe(priv);
495f1407d5cSKuninori Morimoto 	if (ret < 0)
49697f93227SKuninori Morimoto 		goto probe_end_pipe_exit;
497f1407d5cSKuninori Morimoto 
498d3af90a5SKuninori Morimoto 	ret = usbhs_mod_probe(priv);
499d3af90a5SKuninori Morimoto 	if (ret < 0)
500d3af90a5SKuninori Morimoto 		goto probe_end_fifo_exit;
501d3af90a5SKuninori Morimoto 
502f1407d5cSKuninori Morimoto 	/* dev_set_drvdata should be called after usbhs_mod_init */
503f1407d5cSKuninori Morimoto 	dev_set_drvdata(&pdev->dev, priv);
504f1407d5cSKuninori Morimoto 
505f1407d5cSKuninori Morimoto 	/*
506f1407d5cSKuninori Morimoto 	 * deviece reset here because
507f1407d5cSKuninori Morimoto 	 * USB device might be used in boot loader.
508f1407d5cSKuninori Morimoto 	 */
509f1407d5cSKuninori Morimoto 	usbhs_sys_clock_ctrl(priv, 0);
510f1407d5cSKuninori Morimoto 
511f1407d5cSKuninori Morimoto 	/*
512f1407d5cSKuninori Morimoto 	 * platform call
513f1407d5cSKuninori Morimoto 	 *
514f1407d5cSKuninori Morimoto 	 * USB phy setup might depend on CPU/Board.
515f1407d5cSKuninori Morimoto 	 * If platform has its callback functions,
516f1407d5cSKuninori Morimoto 	 * call it here.
517f1407d5cSKuninori Morimoto 	 */
518f1407d5cSKuninori Morimoto 	ret = usbhs_platform_call(priv, hardware_init, pdev);
519f1407d5cSKuninori Morimoto 	if (ret < 0) {
520f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "platform prove failed.\n");
52197f93227SKuninori Morimoto 		goto probe_end_mod_exit;
522f1407d5cSKuninori Morimoto 	}
523f1407d5cSKuninori Morimoto 
524f1407d5cSKuninori Morimoto 	/* reset phy for connection */
525f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
526f1407d5cSKuninori Morimoto 
527b002ff6eSKuninori Morimoto 	/* power control */
528b002ff6eSKuninori Morimoto 	pm_runtime_enable(&pdev->dev);
529b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
530b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
531b002ff6eSKuninori Morimoto 		usbhs_mod_autonomy_mode(priv);
532b002ff6eSKuninori Morimoto 	}
533b002ff6eSKuninori Morimoto 
534f1407d5cSKuninori Morimoto 	/*
535f1407d5cSKuninori Morimoto 	 * manual call notify_hotplug for cold plug
536f1407d5cSKuninori Morimoto 	 */
537f1407d5cSKuninori Morimoto 	ret = usbhsc_drvcllbck_notify_hotplug(pdev);
538f1407d5cSKuninori Morimoto 	if (ret < 0)
539f1407d5cSKuninori Morimoto 		goto probe_end_call_remove;
540f1407d5cSKuninori Morimoto 
541f1407d5cSKuninori Morimoto 	dev_info(&pdev->dev, "probed\n");
542f1407d5cSKuninori Morimoto 
543f1407d5cSKuninori Morimoto 	return ret;
544f1407d5cSKuninori Morimoto 
545f1407d5cSKuninori Morimoto probe_end_call_remove:
546f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, hardware_exit, pdev);
547f1407d5cSKuninori Morimoto probe_end_mod_exit:
548f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
549d3af90a5SKuninori Morimoto probe_end_fifo_exit:
550d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
55197f93227SKuninori Morimoto probe_end_pipe_exit:
55297f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
553f1407d5cSKuninori Morimoto 
554f1407d5cSKuninori Morimoto 	dev_info(&pdev->dev, "probe failed\n");
555f1407d5cSKuninori Morimoto 
556f1407d5cSKuninori Morimoto 	return ret;
557f1407d5cSKuninori Morimoto }
558f1407d5cSKuninori Morimoto 
559*fb4e98abSBill Pemberton static int usbhs_remove(struct platform_device *pdev)
560f1407d5cSKuninori Morimoto {
561206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
562af32fe51SKuninori Morimoto 	struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
563af32fe51SKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
564f1407d5cSKuninori Morimoto 
565f1407d5cSKuninori Morimoto 	dev_dbg(&pdev->dev, "usb remove\n");
566f1407d5cSKuninori Morimoto 
567af32fe51SKuninori Morimoto 	dfunc->notify_hotplug = NULL;
568af32fe51SKuninori Morimoto 
569b002ff6eSKuninori Morimoto 	/* power off */
570b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
571b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
572f1407d5cSKuninori Morimoto 
573b002ff6eSKuninori Morimoto 	pm_runtime_disable(&pdev->dev);
574f1407d5cSKuninori Morimoto 
575f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, hardware_exit, pdev);
576f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
577d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
57897f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
579f1407d5cSKuninori Morimoto 
580f1407d5cSKuninori Morimoto 	return 0;
581f1407d5cSKuninori Morimoto }
582f1407d5cSKuninori Morimoto 
583ca8a282aSKuninori Morimoto static int usbhsc_suspend(struct device *dev)
584ca8a282aSKuninori Morimoto {
585ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
586ca8a282aSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
587ca8a282aSKuninori Morimoto 
588ca8a282aSKuninori Morimoto 	if (mod) {
589ca8a282aSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
590ca8a282aSKuninori Morimoto 		usbhs_mod_change(priv, -1);
591ca8a282aSKuninori Morimoto 	}
592ca8a282aSKuninori Morimoto 
593ca8a282aSKuninori Morimoto 	if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
594ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
595ca8a282aSKuninori Morimoto 
596ca8a282aSKuninori Morimoto 	return 0;
597ca8a282aSKuninori Morimoto }
598ca8a282aSKuninori Morimoto 
599ca8a282aSKuninori Morimoto static int usbhsc_resume(struct device *dev)
600ca8a282aSKuninori Morimoto {
601ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
602ca8a282aSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
603ca8a282aSKuninori Morimoto 
604ca8a282aSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
605ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
606ca8a282aSKuninori Morimoto 
6075b50d3b5SKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
6085b50d3b5SKuninori Morimoto 
6095b50d3b5SKuninori Morimoto 	usbhsc_drvcllbck_notify_hotplug(pdev);
610ca8a282aSKuninori Morimoto 
611ca8a282aSKuninori Morimoto 	return 0;
612ca8a282aSKuninori Morimoto }
613ca8a282aSKuninori Morimoto 
614ca8a282aSKuninori Morimoto static int usbhsc_runtime_nop(struct device *dev)
615ca8a282aSKuninori Morimoto {
616ca8a282aSKuninori Morimoto 	/* Runtime PM callback shared between ->runtime_suspend()
617ca8a282aSKuninori Morimoto 	 * and ->runtime_resume(). Simply returns success.
618ca8a282aSKuninori Morimoto 	 *
619ca8a282aSKuninori Morimoto 	 * This driver re-initializes all registers after
620ca8a282aSKuninori Morimoto 	 * pm_runtime_get_sync() anyway so there is no need
621ca8a282aSKuninori Morimoto 	 * to save and restore registers here.
622ca8a282aSKuninori Morimoto 	 */
623ca8a282aSKuninori Morimoto 	return 0;
624ca8a282aSKuninori Morimoto }
625ca8a282aSKuninori Morimoto 
626ca8a282aSKuninori Morimoto static const struct dev_pm_ops usbhsc_pm_ops = {
627ca8a282aSKuninori Morimoto 	.suspend		= usbhsc_suspend,
628ca8a282aSKuninori Morimoto 	.resume			= usbhsc_resume,
629ca8a282aSKuninori Morimoto 	.runtime_suspend	= usbhsc_runtime_nop,
630ca8a282aSKuninori Morimoto 	.runtime_resume		= usbhsc_runtime_nop,
631ca8a282aSKuninori Morimoto };
632ca8a282aSKuninori Morimoto 
633f1407d5cSKuninori Morimoto static struct platform_driver renesas_usbhs_driver = {
634f1407d5cSKuninori Morimoto 	.driver		= {
635f1407d5cSKuninori Morimoto 		.name	= "renesas_usbhs",
636ca8a282aSKuninori Morimoto 		.pm	= &usbhsc_pm_ops,
637f1407d5cSKuninori Morimoto 	},
638f1407d5cSKuninori Morimoto 	.probe		= usbhs_probe,
6397690417dSBill Pemberton 	.remove		= usbhs_remove,
640f1407d5cSKuninori Morimoto };
641f1407d5cSKuninori Morimoto 
642cc27c96cSAxel Lin module_platform_driver(renesas_usbhs_driver);
643f1407d5cSKuninori Morimoto 
644f1407d5cSKuninori Morimoto MODULE_LICENSE("GPL");
645f1407d5cSKuninori Morimoto MODULE_DESCRIPTION("Renesas USB driver");
646f1407d5cSKuninori Morimoto MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
647