xref: /openbmc/linux/drivers/usb/renesas_usbhs/common.c (revision 97a7968448cb0ef5c15e3d395746b108b1a55556)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-1.0+
2f1407d5cSKuninori Morimoto /*
3f1407d5cSKuninori Morimoto  * Renesas USB driver
4f1407d5cSKuninori Morimoto  *
5f1407d5cSKuninori Morimoto  * Copyright (C) 2011 Renesas Solutions Corp.
6f1407d5cSKuninori Morimoto  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7f1407d5cSKuninori Morimoto  */
83df0e240SYoshihiro Shimoda #include <linux/clk.h>
9148e1134SThierry Reding #include <linux/err.h>
108ecef00fSUlrich Hecht #include <linux/gpio.h>
11f1407d5cSKuninori Morimoto #include <linux/io.h>
12f1407d5cSKuninori Morimoto #include <linux/module.h>
13b854100eSYoshihiro Shimoda #include <linux/of_device.h>
14b854100eSYoshihiro Shimoda #include <linux/of_gpio.h>
15f1407d5cSKuninori Morimoto #include <linux/pm_runtime.h>
16f181dbb4SYoshihiro Shimoda #include <linux/reset.h>
17f1407d5cSKuninori Morimoto #include <linux/slab.h>
18f1407d5cSKuninori Morimoto #include <linux/sysfs.h>
19cc502bb7SPaul Bolle #include "common.h"
208ecef00fSUlrich Hecht #include "rcar2.h"
21de18757eSYoshihiro Shimoda #include "rcar3.h"
22aec2927bSChris Brandt #include "rza.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 
46f1407d5cSKuninori Morimoto /*
47f1407d5cSKuninori Morimoto  * platform call back
48f1407d5cSKuninori Morimoto  *
49f1407d5cSKuninori Morimoto  * renesas usb support platform callback function.
50f1407d5cSKuninori Morimoto  * Below macro call it.
51f1407d5cSKuninori Morimoto  * if platform doesn't have callback, it return 0 (no error)
52f1407d5cSKuninori Morimoto  */
53f1407d5cSKuninori Morimoto #define usbhs_platform_call(priv, func, args...)\
54f1407d5cSKuninori Morimoto 	(!(priv) ? -ENODEV :			\
5548298206SKuninori Morimoto 	 !((priv)->pfunc.func) ? 0 :		\
5648298206SKuninori Morimoto 	 (priv)->pfunc.func(args))
57f1407d5cSKuninori Morimoto 
58f1407d5cSKuninori Morimoto /*
59f1407d5cSKuninori Morimoto  *		common functions
60f1407d5cSKuninori Morimoto  */
61f1407d5cSKuninori Morimoto u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
62f1407d5cSKuninori Morimoto {
63f1407d5cSKuninori Morimoto 	return ioread16(priv->base + reg);
64f1407d5cSKuninori Morimoto }
65f1407d5cSKuninori Morimoto 
66f1407d5cSKuninori Morimoto void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
67f1407d5cSKuninori Morimoto {
68f1407d5cSKuninori Morimoto 	iowrite16(data, priv->base + reg);
69f1407d5cSKuninori Morimoto }
70f1407d5cSKuninori Morimoto 
71f1407d5cSKuninori Morimoto void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
72f1407d5cSKuninori Morimoto {
73f1407d5cSKuninori Morimoto 	u16 val = usbhs_read(priv, reg);
74f1407d5cSKuninori Morimoto 
75f1407d5cSKuninori Morimoto 	val &= ~mask;
76f1407d5cSKuninori Morimoto 	val |= data & mask;
77f1407d5cSKuninori Morimoto 
78f1407d5cSKuninori Morimoto 	usbhs_write(priv, reg, val);
79f1407d5cSKuninori Morimoto }
80f1407d5cSKuninori Morimoto 
81206dcc2cSKuninori Morimoto struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
82206dcc2cSKuninori Morimoto {
83206dcc2cSKuninori Morimoto 	return dev_get_drvdata(&pdev->dev);
84206dcc2cSKuninori Morimoto }
85206dcc2cSKuninori Morimoto 
86f1407d5cSKuninori Morimoto /*
87f1407d5cSKuninori Morimoto  *		syscfg functions
88f1407d5cSKuninori Morimoto  */
8976190152SKuninori Morimoto static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
90f1407d5cSKuninori Morimoto {
91f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
92f1407d5cSKuninori Morimoto }
93f1407d5cSKuninori Morimoto 
94f1407d5cSKuninori Morimoto void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
95f1407d5cSKuninori Morimoto {
963244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
973244a7b4SKuninori Morimoto 	u16 val  = DCFM | DRPD | HSE | USBE;
98f427eb64SKuninori Morimoto 	int has_otg = usbhs_get_dparam(priv, has_otg);
99f427eb64SKuninori Morimoto 
100f427eb64SKuninori Morimoto 	if (has_otg)
101f427eb64SKuninori Morimoto 		usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN));
102f1407d5cSKuninori Morimoto 
103f1407d5cSKuninori Morimoto 	/*
104f1407d5cSKuninori Morimoto 	 * if enable
105f1407d5cSKuninori Morimoto 	 *
106f1407d5cSKuninori Morimoto 	 * - select Host mode
107f1407d5cSKuninori Morimoto 	 * - D+ Line/D- Line Pull-down
108f1407d5cSKuninori Morimoto 	 */
109f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
110f1407d5cSKuninori Morimoto }
111f1407d5cSKuninori Morimoto 
112f1407d5cSKuninori Morimoto void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
113f1407d5cSKuninori Morimoto {
1143244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
11504a5def3STakeshi Kihara 	u16 val  = HSE | USBE;
116f1407d5cSKuninori Morimoto 
117f1407d5cSKuninori Morimoto 	/*
118f1407d5cSKuninori Morimoto 	 * if enable
119f1407d5cSKuninori Morimoto 	 *
120f1407d5cSKuninori Morimoto 	 * - select Function mode
12104a5def3STakeshi Kihara 	 * - D+ Line Pull-up is disabled
12204a5def3STakeshi Kihara 	 *      When D+ Line Pull-up is enabled,
12304a5def3STakeshi Kihara 	 *      calling usbhs_sys_function_pullup(,1)
124f1407d5cSKuninori Morimoto 	 */
125f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
126f1407d5cSKuninori Morimoto }
127f1407d5cSKuninori Morimoto 
1284cd2f599Skuninori.morimoto.gx@renesas.com void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable)
1294cd2f599Skuninori.morimoto.gx@renesas.com {
1304cd2f599Skuninori.morimoto.gx@renesas.com 	usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0);
1314cd2f599Skuninori.morimoto.gx@renesas.com }
1324cd2f599Skuninori.morimoto.gx@renesas.com 
133dfbb7f4fSKuninori Morimoto void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
134dfbb7f4fSKuninori Morimoto {
135dfbb7f4fSKuninori Morimoto 	usbhs_write(priv, TESTMODE, mode);
136dfbb7f4fSKuninori Morimoto }
137dfbb7f4fSKuninori Morimoto 
138f1407d5cSKuninori Morimoto /*
139f1407d5cSKuninori Morimoto  *		frame functions
140f1407d5cSKuninori Morimoto  */
141f1407d5cSKuninori Morimoto int usbhs_frame_get_num(struct usbhs_priv *priv)
142f1407d5cSKuninori Morimoto {
143f1407d5cSKuninori Morimoto 	return usbhs_read(priv, FRMNUM) & FRNM_MASK;
144f1407d5cSKuninori Morimoto }
145f1407d5cSKuninori Morimoto 
146f1407d5cSKuninori Morimoto /*
147ef8bedb9SKuninori Morimoto  *		usb request functions
148ef8bedb9SKuninori Morimoto  */
149ef8bedb9SKuninori Morimoto void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
150ef8bedb9SKuninori Morimoto {
151ef8bedb9SKuninori Morimoto 	u16 val;
152ef8bedb9SKuninori Morimoto 
153ef8bedb9SKuninori Morimoto 	val = usbhs_read(priv, USBREQ);
154ef8bedb9SKuninori Morimoto 	req->bRequest		= (val >> 8) & 0xFF;
155ef8bedb9SKuninori Morimoto 	req->bRequestType	= (val >> 0) & 0xFF;
156ef8bedb9SKuninori Morimoto 
157ef8bedb9SKuninori Morimoto 	req->wValue	= usbhs_read(priv, USBVAL);
158ef8bedb9SKuninori Morimoto 	req->wIndex	= usbhs_read(priv, USBINDX);
159ef8bedb9SKuninori Morimoto 	req->wLength	= usbhs_read(priv, USBLENG);
160ef8bedb9SKuninori Morimoto }
161ef8bedb9SKuninori Morimoto 
162ef8bedb9SKuninori Morimoto void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
163ef8bedb9SKuninori Morimoto {
164ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBREQ,  (req->bRequest << 8) | req->bRequestType);
165ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBVAL,  req->wValue);
166ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBINDX, req->wIndex);
167ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBLENG, req->wLength);
168ef8bedb9SKuninori Morimoto 
169ef8bedb9SKuninori Morimoto 	usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
170ef8bedb9SKuninori Morimoto }
171ef8bedb9SKuninori Morimoto 
172ef8bedb9SKuninori Morimoto /*
173258485d9SKuninori Morimoto  *		bus/vbus functions
174258485d9SKuninori Morimoto  */
175258485d9SKuninori Morimoto void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
176258485d9SKuninori Morimoto {
177a9be4a45SKuninori Morimoto 	u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
178a9be4a45SKuninori Morimoto 
179a9be4a45SKuninori Morimoto 	if (status != USBRST) {
180a9be4a45SKuninori Morimoto 		struct device *dev = usbhs_priv_to_dev(priv);
181a9be4a45SKuninori Morimoto 		dev_err(dev, "usbhs should be reset\n");
182a9be4a45SKuninori Morimoto 	}
183a9be4a45SKuninori Morimoto 
184258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
185258485d9SKuninori Morimoto }
186258485d9SKuninori Morimoto 
187258485d9SKuninori Morimoto void usbhs_bus_send_reset(struct usbhs_priv *priv)
188258485d9SKuninori Morimoto {
189258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
190258485d9SKuninori Morimoto }
191258485d9SKuninori Morimoto 
19275587f52SKuninori Morimoto int usbhs_bus_get_speed(struct usbhs_priv *priv)
19375587f52SKuninori Morimoto {
19475587f52SKuninori Morimoto 	u16 dvstctr = usbhs_read(priv, DVSTCTR);
19575587f52SKuninori Morimoto 
19675587f52SKuninori Morimoto 	switch (RHST & dvstctr) {
19775587f52SKuninori Morimoto 	case RHST_LOW_SPEED:
19875587f52SKuninori Morimoto 		return USB_SPEED_LOW;
19975587f52SKuninori Morimoto 	case RHST_FULL_SPEED:
20075587f52SKuninori Morimoto 		return USB_SPEED_FULL;
20175587f52SKuninori Morimoto 	case RHST_HIGH_SPEED:
20275587f52SKuninori Morimoto 		return USB_SPEED_HIGH;
20375587f52SKuninori Morimoto 	}
20475587f52SKuninori Morimoto 
20575587f52SKuninori Morimoto 	return USB_SPEED_UNKNOWN;
20675587f52SKuninori Morimoto }
20775587f52SKuninori Morimoto 
208258485d9SKuninori Morimoto int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
209258485d9SKuninori Morimoto {
210258485d9SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
211258485d9SKuninori Morimoto 
212258485d9SKuninori Morimoto 	return usbhs_platform_call(priv, set_vbus, pdev, enable);
213258485d9SKuninori Morimoto }
214258485d9SKuninori Morimoto 
215258485d9SKuninori Morimoto static void usbhsc_bus_init(struct usbhs_priv *priv)
216258485d9SKuninori Morimoto {
217258485d9SKuninori Morimoto 	usbhs_write(priv, DVSTCTR, 0);
218258485d9SKuninori Morimoto 
219258485d9SKuninori Morimoto 	usbhs_vbus_ctrl(priv, 0);
220258485d9SKuninori Morimoto }
221258485d9SKuninori Morimoto 
222258485d9SKuninori Morimoto /*
223eb05191fSKuninori Morimoto  *		device configuration
224eb05191fSKuninori Morimoto  */
2253dd49268SKuninori Morimoto int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
226eb05191fSKuninori Morimoto 			   u16 upphub, u16 hubport, u16 speed)
227eb05191fSKuninori Morimoto {
228eb05191fSKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
229eb05191fSKuninori Morimoto 	u16 usbspd = 0;
230eb05191fSKuninori Morimoto 	u32 reg = DEVADD0 + (2 * devnum);
231eb05191fSKuninori Morimoto 
232eb05191fSKuninori Morimoto 	if (devnum > 10) {
233eb05191fSKuninori Morimoto 		dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
234eb05191fSKuninori Morimoto 		return -EIO;
235eb05191fSKuninori Morimoto 	}
236eb05191fSKuninori Morimoto 
237eb05191fSKuninori Morimoto 	if (upphub > 0xA) {
238eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported hub number %d\n", upphub);
239eb05191fSKuninori Morimoto 		return -EIO;
240eb05191fSKuninori Morimoto 	}
241eb05191fSKuninori Morimoto 
242eb05191fSKuninori Morimoto 	switch (speed) {
243eb05191fSKuninori Morimoto 	case USB_SPEED_LOW:
244eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_LOW;
245eb05191fSKuninori Morimoto 		break;
246eb05191fSKuninori Morimoto 	case USB_SPEED_FULL:
247eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_FULL;
248eb05191fSKuninori Morimoto 		break;
249eb05191fSKuninori Morimoto 	case USB_SPEED_HIGH:
250eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_HIGH;
251eb05191fSKuninori Morimoto 		break;
252eb05191fSKuninori Morimoto 	default:
253eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported speed %d\n", speed);
254eb05191fSKuninori Morimoto 		return -EIO;
255eb05191fSKuninori Morimoto 	}
256eb05191fSKuninori Morimoto 
257eb05191fSKuninori Morimoto 	usbhs_write(priv, reg,	UPPHUB(upphub)	|
258eb05191fSKuninori Morimoto 				HUBPORT(hubport)|
259eb05191fSKuninori Morimoto 				USBSPD(usbspd));
260eb05191fSKuninori Morimoto 
261eb05191fSKuninori Morimoto 	return 0;
262eb05191fSKuninori Morimoto }
263eb05191fSKuninori Morimoto 
264eb05191fSKuninori Morimoto /*
265ab330cf3SYoshihiro Shimoda  *		interrupt functions
266ab330cf3SYoshihiro Shimoda  */
267ab330cf3SYoshihiro Shimoda void usbhs_xxxsts_clear(struct usbhs_priv *priv, u16 sts_reg, u16 bit)
268ab330cf3SYoshihiro Shimoda {
269ab330cf3SYoshihiro Shimoda 	u16 pipe_mask = (u16)GENMASK(usbhs_get_dparam(priv, pipe_size), 0);
270ab330cf3SYoshihiro Shimoda 
271ab330cf3SYoshihiro Shimoda 	usbhs_write(priv, sts_reg, ~(1 << bit) & pipe_mask);
272ab330cf3SYoshihiro Shimoda }
273ab330cf3SYoshihiro Shimoda 
274ab330cf3SYoshihiro Shimoda /*
275f1407d5cSKuninori Morimoto  *		local functions
276f1407d5cSKuninori Morimoto  */
27711935de5SKuninori Morimoto static void usbhsc_set_buswait(struct usbhs_priv *priv)
278f1407d5cSKuninori Morimoto {
279f1407d5cSKuninori Morimoto 	int wait = usbhs_get_dparam(priv, buswait_bwait);
280f1407d5cSKuninori Morimoto 
281f1407d5cSKuninori Morimoto 	/* set bus wait if platform have */
282f1407d5cSKuninori Morimoto 	if (wait)
283f1407d5cSKuninori Morimoto 		usbhs_bset(priv, BUSWAIT, 0x000F, wait);
284f1407d5cSKuninori Morimoto }
285f1407d5cSKuninori Morimoto 
2863df0e240SYoshihiro Shimoda static bool usbhsc_is_multi_clks(struct usbhs_priv *priv)
2873df0e240SYoshihiro Shimoda {
2883df0e240SYoshihiro Shimoda 	if (priv->dparam.type == USBHS_TYPE_RCAR_GEN3 ||
2893df0e240SYoshihiro Shimoda 	    priv->dparam.type == USBHS_TYPE_RCAR_GEN3_WITH_PLL)
2903df0e240SYoshihiro Shimoda 		return true;
2913df0e240SYoshihiro Shimoda 
2923df0e240SYoshihiro Shimoda 	return false;
2933df0e240SYoshihiro Shimoda }
2943df0e240SYoshihiro Shimoda 
2953df0e240SYoshihiro Shimoda static int usbhsc_clk_get(struct device *dev, struct usbhs_priv *priv)
2963df0e240SYoshihiro Shimoda {
2973df0e240SYoshihiro Shimoda 	if (!usbhsc_is_multi_clks(priv))
2983df0e240SYoshihiro Shimoda 		return 0;
2993df0e240SYoshihiro Shimoda 
3003df0e240SYoshihiro Shimoda 	/* The first clock should exist */
3013df0e240SYoshihiro Shimoda 	priv->clks[0] = of_clk_get(dev->of_node, 0);
3023df0e240SYoshihiro Shimoda 	if (IS_ERR(priv->clks[0]))
3033df0e240SYoshihiro Shimoda 		return PTR_ERR(priv->clks[0]);
3043df0e240SYoshihiro Shimoda 
3053df0e240SYoshihiro Shimoda 	/*
3063df0e240SYoshihiro Shimoda 	 * To backward compatibility with old DT, this driver checks the return
3073df0e240SYoshihiro Shimoda 	 * value if it's -ENOENT or not.
3083df0e240SYoshihiro Shimoda 	 */
3093df0e240SYoshihiro Shimoda 	priv->clks[1] = of_clk_get(dev->of_node, 1);
3103df0e240SYoshihiro Shimoda 	if (PTR_ERR(priv->clks[1]) == -ENOENT)
3113df0e240SYoshihiro Shimoda 		priv->clks[1] = NULL;
3123df0e240SYoshihiro Shimoda 	else if (IS_ERR(priv->clks[1]))
3133df0e240SYoshihiro Shimoda 		return PTR_ERR(priv->clks[1]);
3143df0e240SYoshihiro Shimoda 
3153df0e240SYoshihiro Shimoda 	return 0;
3163df0e240SYoshihiro Shimoda }
3173df0e240SYoshihiro Shimoda 
3183df0e240SYoshihiro Shimoda static void usbhsc_clk_put(struct usbhs_priv *priv)
3193df0e240SYoshihiro Shimoda {
3203df0e240SYoshihiro Shimoda 	int i;
3213df0e240SYoshihiro Shimoda 
3223df0e240SYoshihiro Shimoda 	if (!usbhsc_is_multi_clks(priv))
3233df0e240SYoshihiro Shimoda 		return;
3243df0e240SYoshihiro Shimoda 
3253df0e240SYoshihiro Shimoda 	for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
3263df0e240SYoshihiro Shimoda 		clk_put(priv->clks[i]);
3273df0e240SYoshihiro Shimoda }
3283df0e240SYoshihiro Shimoda 
3293df0e240SYoshihiro Shimoda static int usbhsc_clk_prepare_enable(struct usbhs_priv *priv)
3303df0e240SYoshihiro Shimoda {
3313df0e240SYoshihiro Shimoda 	int i, ret;
3323df0e240SYoshihiro Shimoda 
3333df0e240SYoshihiro Shimoda 	if (!usbhsc_is_multi_clks(priv))
3343df0e240SYoshihiro Shimoda 		return 0;
3353df0e240SYoshihiro Shimoda 
3363df0e240SYoshihiro Shimoda 	for (i = 0; i < ARRAY_SIZE(priv->clks); i++) {
3373df0e240SYoshihiro Shimoda 		ret = clk_prepare_enable(priv->clks[i]);
3383df0e240SYoshihiro Shimoda 		if (ret) {
3393df0e240SYoshihiro Shimoda 			while (--i >= 0)
3403df0e240SYoshihiro Shimoda 				clk_disable_unprepare(priv->clks[i]);
3413df0e240SYoshihiro Shimoda 			return ret;
3423df0e240SYoshihiro Shimoda 		}
3433df0e240SYoshihiro Shimoda 	}
3443df0e240SYoshihiro Shimoda 
3453df0e240SYoshihiro Shimoda 	return ret;
3463df0e240SYoshihiro Shimoda }
3473df0e240SYoshihiro Shimoda 
3483df0e240SYoshihiro Shimoda static void usbhsc_clk_disable_unprepare(struct usbhs_priv *priv)
3493df0e240SYoshihiro Shimoda {
3503df0e240SYoshihiro Shimoda 	int i;
3513df0e240SYoshihiro Shimoda 
3523df0e240SYoshihiro Shimoda 	if (!usbhsc_is_multi_clks(priv))
3533df0e240SYoshihiro Shimoda 		return;
3543df0e240SYoshihiro Shimoda 
3553df0e240SYoshihiro Shimoda 	for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
3563df0e240SYoshihiro Shimoda 		clk_disable_unprepare(priv->clks[i]);
3573df0e240SYoshihiro Shimoda }
3583df0e240SYoshihiro Shimoda 
359f1407d5cSKuninori Morimoto /*
360f1407d5cSKuninori Morimoto  *		platform default param
361f1407d5cSKuninori Morimoto  */
3628ecef00fSUlrich Hecht 
3638ecef00fSUlrich Hecht /* commonly used on old SH-Mobile SoCs */
36451f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_default_pipe[] = {
36551f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
36651f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, false),
36751f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x18, false),
36851f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x28, true),
36951f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x38, true),
37051f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
37151f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
37251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
37351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
37451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x07, false),
375f1407d5cSKuninori Morimoto };
376f1407d5cSKuninori Morimoto 
3778ecef00fSUlrich Hecht /* commonly used on newer SH-Mobile and R-Car SoCs */
37851f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_new_pipe[] = {
37951f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
38051f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true),
38151f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true),
38251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
38351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true),
38451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true),
38551f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
38651f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
38751f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
38851f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x78, true),
38951f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x88, true),
39051f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x98, true),
39151f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xa8, true),
39251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xb8, true),
39351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xc8, true),
39451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xd8, true),
3958ecef00fSUlrich Hecht };
3968ecef00fSUlrich Hecht 
397f1407d5cSKuninori Morimoto /*
3986e267da8SKuninori Morimoto  *		power control
3996e267da8SKuninori Morimoto  */
4006e267da8SKuninori Morimoto static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
4016e267da8SKuninori Morimoto {
402f1ee56a0SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
4036e267da8SKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
4046e267da8SKuninori Morimoto 
4056e267da8SKuninori Morimoto 	if (enable) {
4066e267da8SKuninori Morimoto 		/* enable PM */
4076e267da8SKuninori Morimoto 		pm_runtime_get_sync(dev);
4086e267da8SKuninori Morimoto 
4093df0e240SYoshihiro Shimoda 		/* enable clks */
4103df0e240SYoshihiro Shimoda 		if (usbhsc_clk_prepare_enable(priv))
4113df0e240SYoshihiro Shimoda 			return;
4123df0e240SYoshihiro Shimoda 
413f1ee56a0SKuninori Morimoto 		/* enable platform power */
414f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
415f1ee56a0SKuninori Morimoto 
4166e267da8SKuninori Morimoto 		/* USB on */
4176e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
4186e267da8SKuninori Morimoto 	} else {
4196e267da8SKuninori Morimoto 		/* USB off */
4206e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
4216e267da8SKuninori Morimoto 
422f1ee56a0SKuninori Morimoto 		/* disable platform power */
423f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
424f1ee56a0SKuninori Morimoto 
4253df0e240SYoshihiro Shimoda 		/* disable clks */
4263df0e240SYoshihiro Shimoda 		usbhsc_clk_disable_unprepare(priv);
4273df0e240SYoshihiro Shimoda 
4286e267da8SKuninori Morimoto 		/* disable PM */
4296e267da8SKuninori Morimoto 		pm_runtime_put_sync(dev);
4306e267da8SKuninori Morimoto 	}
4316e267da8SKuninori Morimoto }
4326e267da8SKuninori Morimoto 
4336e267da8SKuninori Morimoto /*
434ca8a282aSKuninori Morimoto  *		hotplug
435f1407d5cSKuninori Morimoto  */
436ca8a282aSKuninori Morimoto static void usbhsc_hotplug(struct usbhs_priv *priv)
437f1407d5cSKuninori Morimoto {
438f1407d5cSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
439f1407d5cSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
440f1407d5cSKuninori Morimoto 	int id;
441f1407d5cSKuninori Morimoto 	int enable;
442d7b3968dSSergei Shtylyov 	int cable;
443f1407d5cSKuninori Morimoto 	int ret;
444f1407d5cSKuninori Morimoto 
445f1407d5cSKuninori Morimoto 	/*
446f1407d5cSKuninori Morimoto 	 * get vbus status from platform
447f1407d5cSKuninori Morimoto 	 */
448f1407d5cSKuninori Morimoto 	enable = usbhs_platform_call(priv, get_vbus, pdev);
449f1407d5cSKuninori Morimoto 
450f1407d5cSKuninori Morimoto 	/*
451f1407d5cSKuninori Morimoto 	 * get id from platform
452f1407d5cSKuninori Morimoto 	 */
453f1407d5cSKuninori Morimoto 	id = usbhs_platform_call(priv, get_id, pdev);
454f1407d5cSKuninori Morimoto 
455f1407d5cSKuninori Morimoto 	if (enable && !mod) {
456d7b3968dSSergei Shtylyov 		if (priv->edev) {
457ea07b8cfSChanwoo Choi 			cable = extcon_get_state(priv->edev, EXTCON_USB_HOST);
458d7b3968dSSergei Shtylyov 			if ((cable > 0 && id != USBHS_HOST) ||
459d7b3968dSSergei Shtylyov 			    (!cable && id != USBHS_GADGET)) {
460d7b3968dSSergei Shtylyov 				dev_info(&pdev->dev,
461d7b3968dSSergei Shtylyov 					 "USB cable plugged in doesn't match the selected role!\n");
462d7b3968dSSergei Shtylyov 				return;
463d7b3968dSSergei Shtylyov 			}
464d7b3968dSSergei Shtylyov 		}
465d7b3968dSSergei Shtylyov 
466f1407d5cSKuninori Morimoto 		ret = usbhs_mod_change(priv, id);
467f1407d5cSKuninori Morimoto 		if (ret < 0)
468f1407d5cSKuninori Morimoto 			return;
469f1407d5cSKuninori Morimoto 
470f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s enable\n", __func__);
471f1407d5cSKuninori Morimoto 
4726e267da8SKuninori Morimoto 		/* power on */
473*97a79684SChris Brandt 		if (usbhs_get_dparam(priv, runtime_pwctrl))
4746e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
475f1407d5cSKuninori Morimoto 
476258485d9SKuninori Morimoto 		/* bus init */
477258485d9SKuninori Morimoto 		usbhsc_set_buswait(priv);
478258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
479258485d9SKuninori Morimoto 
480f1407d5cSKuninori Morimoto 		/* module start */
481f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, start, priv);
482f1407d5cSKuninori Morimoto 
483f1407d5cSKuninori Morimoto 	} else if (!enable && mod) {
484f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s disable\n", __func__);
485f1407d5cSKuninori Morimoto 
486f1407d5cSKuninori Morimoto 		/* module stop */
487f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
488f1407d5cSKuninori Morimoto 
489258485d9SKuninori Morimoto 		/* bus init */
490258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
491258485d9SKuninori Morimoto 
4926e267da8SKuninori Morimoto 		/* power off */
493*97a79684SChris Brandt 		if (usbhs_get_dparam(priv, runtime_pwctrl))
4946e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
495f1407d5cSKuninori Morimoto 
496f1407d5cSKuninori Morimoto 		usbhs_mod_change(priv, -1);
497f1407d5cSKuninori Morimoto 
498f1407d5cSKuninori Morimoto 		/* reset phy for next connection */
499f1407d5cSKuninori Morimoto 		usbhs_platform_call(priv, phy_reset, pdev);
500f1407d5cSKuninori Morimoto 	}
501f1407d5cSKuninori Morimoto }
502f1407d5cSKuninori Morimoto 
503ca8a282aSKuninori Morimoto /*
504ca8a282aSKuninori Morimoto  *		notify hotplug
505ca8a282aSKuninori Morimoto  */
506ca8a282aSKuninori Morimoto static void usbhsc_notify_hotplug(struct work_struct *work)
507ca8a282aSKuninori Morimoto {
508ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = container_of(work,
509ca8a282aSKuninori Morimoto 					       struct usbhs_priv,
510ca8a282aSKuninori Morimoto 					       notify_hotplug_work.work);
511ca8a282aSKuninori Morimoto 	usbhsc_hotplug(priv);
512ca8a282aSKuninori Morimoto }
513ca8a282aSKuninori Morimoto 
514b4fcea2aSKuninori Morimoto static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
515f1407d5cSKuninori Morimoto {
516206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
517bc57381eSKuninori Morimoto 	int delay = usbhs_get_dparam(priv, detection_delay);
518f1407d5cSKuninori Morimoto 
519f1407d5cSKuninori Morimoto 	/*
520f1407d5cSKuninori Morimoto 	 * This functions will be called in interrupt.
521f1407d5cSKuninori Morimoto 	 * To make sure safety context,
522f1407d5cSKuninori Morimoto 	 * use workqueue for usbhs_notify_hotplug
523f1407d5cSKuninori Morimoto 	 */
524a49a88f1SKuninori Morimoto 	schedule_delayed_work(&priv->notify_hotplug_work,
525a49a88f1SKuninori Morimoto 			      msecs_to_jiffies(delay));
526f1407d5cSKuninori Morimoto 	return 0;
527f1407d5cSKuninori Morimoto }
528f1407d5cSKuninori Morimoto 
529be21a02aSYoshihiro Shimoda static const struct usbhs_of_data rcar_gen2_data = {
530be21a02aSYoshihiro Shimoda 	.platform_callback = &usbhs_rcar2_ops,
531be21a02aSYoshihiro Shimoda 	.param = {
532be21a02aSYoshihiro Shimoda 		.type = USBHS_TYPE_RCAR_GEN2,
533be21a02aSYoshihiro Shimoda 		.has_usb_dmac = 1,
534be21a02aSYoshihiro Shimoda 		.pipe_configs = usbhsc_new_pipe,
535be21a02aSYoshihiro Shimoda 		.pipe_size = ARRAY_SIZE(usbhsc_new_pipe),
536be21a02aSYoshihiro Shimoda 	}
537be21a02aSYoshihiro Shimoda };
538be21a02aSYoshihiro Shimoda 
539be21a02aSYoshihiro Shimoda static const struct usbhs_of_data rcar_gen3_data = {
540be21a02aSYoshihiro Shimoda 	.platform_callback = &usbhs_rcar3_ops,
541be21a02aSYoshihiro Shimoda 	.param = {
542be21a02aSYoshihiro Shimoda 		.type = USBHS_TYPE_RCAR_GEN3,
543be21a02aSYoshihiro Shimoda 		.has_usb_dmac = 1,
544be21a02aSYoshihiro Shimoda 		.pipe_configs = usbhsc_new_pipe,
545be21a02aSYoshihiro Shimoda 		.pipe_size = ARRAY_SIZE(usbhsc_new_pipe),
546be21a02aSYoshihiro Shimoda 	}
547be21a02aSYoshihiro Shimoda };
548be21a02aSYoshihiro Shimoda 
549be21a02aSYoshihiro Shimoda static const struct usbhs_of_data rcar_gen3_with_pll_data = {
550be21a02aSYoshihiro Shimoda 	.platform_callback = &usbhs_rcar3_with_pll_ops,
551be21a02aSYoshihiro Shimoda 	.param = {
552be21a02aSYoshihiro Shimoda 		.type = USBHS_TYPE_RCAR_GEN3_WITH_PLL,
553be21a02aSYoshihiro Shimoda 		.has_usb_dmac = 1,
554be21a02aSYoshihiro Shimoda 		.pipe_configs = usbhsc_new_pipe,
555be21a02aSYoshihiro Shimoda 		.pipe_size = ARRAY_SIZE(usbhsc_new_pipe),
556be21a02aSYoshihiro Shimoda 	}
557be21a02aSYoshihiro Shimoda };
558be21a02aSYoshihiro Shimoda 
559be21a02aSYoshihiro Shimoda static const struct usbhs_of_data rza1_data = {
560be21a02aSYoshihiro Shimoda 	.platform_callback = &usbhs_rza1_ops,
561be21a02aSYoshihiro Shimoda 	.param = {
562be21a02aSYoshihiro Shimoda 		.type = USBHS_TYPE_RZA1,
563be21a02aSYoshihiro Shimoda 		.pipe_configs = usbhsc_new_pipe,
564be21a02aSYoshihiro Shimoda 		.pipe_size = ARRAY_SIZE(usbhsc_new_pipe),
565be21a02aSYoshihiro Shimoda 	}
566be21a02aSYoshihiro Shimoda };
567be21a02aSYoshihiro Shimoda 
568f1407d5cSKuninori Morimoto /*
569f1407d5cSKuninori Morimoto  *		platform functions
570f1407d5cSKuninori Morimoto  */
571b854100eSYoshihiro Shimoda static const struct of_device_id usbhs_of_match[] = {
572b854100eSYoshihiro Shimoda 	{
5731d6e81a2SFabrizio Castro 		.compatible = "renesas,usbhs-r8a774c0",
574be21a02aSYoshihiro Shimoda 		.data = &rcar_gen3_with_pll_data,
5751d6e81a2SFabrizio Castro 	},
5761d6e81a2SFabrizio Castro 	{
577b854100eSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7790",
578be21a02aSYoshihiro Shimoda 		.data = &rcar_gen2_data,
579b854100eSYoshihiro Shimoda 	},
580b854100eSYoshihiro Shimoda 	{
581b854100eSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7791",
582be21a02aSYoshihiro Shimoda 		.data = &rcar_gen2_data,
583b854100eSYoshihiro Shimoda 	},
584af6e613bSYoshihiro Shimoda 	{
585af6e613bSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7794",
586be21a02aSYoshihiro Shimoda 		.data = &rcar_gen2_data,
587af6e613bSYoshihiro Shimoda 	},
588f5f6afa8SYoshihiro Shimoda 	{
589f5f6afa8SYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7795",
590be21a02aSYoshihiro Shimoda 		.data = &rcar_gen3_data,
591f5f6afa8SYoshihiro Shimoda 	},
5922f1a993aSSimon Horman 	{
593aae819e1SYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7796",
594be21a02aSYoshihiro Shimoda 		.data = &rcar_gen3_data,
595aae819e1SYoshihiro Shimoda 	},
596aae819e1SYoshihiro Shimoda 	{
5974d2a863fSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a77990",
598be21a02aSYoshihiro Shimoda 		.data = &rcar_gen3_with_pll_data,
5994d2a863fSYoshihiro Shimoda 	},
6004d2a863fSYoshihiro Shimoda 	{
6010f38672cSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a77995",
602be21a02aSYoshihiro Shimoda 		.data = &rcar_gen3_with_pll_data,
6030f38672cSYoshihiro Shimoda 	},
6040f38672cSYoshihiro Shimoda 	{
6052f1a993aSSimon Horman 		.compatible = "renesas,rcar-gen2-usbhs",
606be21a02aSYoshihiro Shimoda 		.data = &rcar_gen2_data,
6072f1a993aSSimon Horman 	},
6082f1a993aSSimon Horman 	{
6092f1a993aSSimon Horman 		.compatible = "renesas,rcar-gen3-usbhs",
610be21a02aSYoshihiro Shimoda 		.data = &rcar_gen3_data,
6112f1a993aSSimon Horman 	},
612aec2927bSChris Brandt 	{
613aec2927bSChris Brandt 		.compatible = "renesas,rza1-usbhs",
614be21a02aSYoshihiro Shimoda 		.data = &rza1_data,
615aec2927bSChris Brandt 	},
616b854100eSYoshihiro Shimoda 	{ },
617b854100eSYoshihiro Shimoda };
618b854100eSYoshihiro Shimoda MODULE_DEVICE_TABLE(of, usbhs_of_match);
619b854100eSYoshihiro Shimoda 
620b854100eSYoshihiro Shimoda static struct renesas_usbhs_platform_info *usbhs_parse_dt(struct device *dev)
621b854100eSYoshihiro Shimoda {
622b854100eSYoshihiro Shimoda 	struct renesas_usbhs_platform_info *info;
623b854100eSYoshihiro Shimoda 	struct renesas_usbhs_driver_param *dparam;
624be21a02aSYoshihiro Shimoda 	const struct usbhs_of_data *data;
625b854100eSYoshihiro Shimoda 	u32 tmp;
626b854100eSYoshihiro Shimoda 	int gpio;
627b854100eSYoshihiro Shimoda 
628b854100eSYoshihiro Shimoda 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
629b854100eSYoshihiro Shimoda 	if (!info)
630b854100eSYoshihiro Shimoda 		return NULL;
631b854100eSYoshihiro Shimoda 
632be21a02aSYoshihiro Shimoda 	data = of_device_get_match_data(dev);
633be21a02aSYoshihiro Shimoda 	if (!data)
634be21a02aSYoshihiro Shimoda 		return NULL;
635be21a02aSYoshihiro Shimoda 
636b854100eSYoshihiro Shimoda 	dparam = &info->driver_param;
637be21a02aSYoshihiro Shimoda 	memcpy(dparam, &data->param, sizeof(data->param));
638be21a02aSYoshihiro Shimoda 	memcpy(&info->platform_callback, data->platform_callback,
639be21a02aSYoshihiro Shimoda 	       sizeof(*data->platform_callback));
640be21a02aSYoshihiro Shimoda 
641b854100eSYoshihiro Shimoda 	if (!of_property_read_u32(dev->of_node, "renesas,buswait", &tmp))
642b854100eSYoshihiro Shimoda 		dparam->buswait_bwait = tmp;
643b854100eSYoshihiro Shimoda 	gpio = of_get_named_gpio_flags(dev->of_node, "renesas,enable-gpio", 0,
644b854100eSYoshihiro Shimoda 				       NULL);
645b854100eSYoshihiro Shimoda 	if (gpio > 0)
646b854100eSYoshihiro Shimoda 		dparam->enable_gpio = gpio;
647b854100eSYoshihiro Shimoda 
648b854100eSYoshihiro Shimoda 	return info;
649b854100eSYoshihiro Shimoda }
650b854100eSYoshihiro Shimoda 
651b7a8d17dSKuninori Morimoto static int usbhs_probe(struct platform_device *pdev)
652f1407d5cSKuninori Morimoto {
653f0f14a7aSKuninori Morimoto 	struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
654f1407d5cSKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc;
655f1407d5cSKuninori Morimoto 	struct usbhs_priv *priv;
65653069af3SShimoda, Yoshihiro 	struct resource *res, *irq_res;
657f1407d5cSKuninori Morimoto 	int ret;
658f1407d5cSKuninori Morimoto 
659b854100eSYoshihiro Shimoda 	/* check device node */
660b854100eSYoshihiro Shimoda 	if (pdev->dev.of_node)
661b854100eSYoshihiro Shimoda 		info = pdev->dev.platform_data = usbhs_parse_dt(&pdev->dev);
662b854100eSYoshihiro Shimoda 
663f1407d5cSKuninori Morimoto 	/* check platform information */
6648ecef00fSUlrich Hecht 	if (!info) {
665f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "no platform information\n");
666f1407d5cSKuninori Morimoto 		return -EINVAL;
667f1407d5cSKuninori Morimoto 	}
668f1407d5cSKuninori Morimoto 
669f1407d5cSKuninori Morimoto 	/* platform data */
67053069af3SShimoda, Yoshihiro 	irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
671687f16b8SVarka Bhadram 	if (!irq_res) {
672f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
673f1407d5cSKuninori Morimoto 		return -ENODEV;
674f1407d5cSKuninori Morimoto 	}
675f1407d5cSKuninori Morimoto 
676f1407d5cSKuninori Morimoto 	/* usb private data */
67758efc77cSKuninori Morimoto 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
678b1cb07cdSPeter Chen 	if (!priv)
679f1407d5cSKuninori Morimoto 		return -ENOMEM;
680f1407d5cSKuninori Morimoto 
681687f16b8SVarka Bhadram 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
682148e1134SThierry Reding 	priv->base = devm_ioremap_resource(&pdev->dev, res);
683148e1134SThierry Reding 	if (IS_ERR(priv->base))
684148e1134SThierry Reding 		return PTR_ERR(priv->base);
685f1407d5cSKuninori Morimoto 
686d7b3968dSSergei Shtylyov 	if (of_property_read_bool(pdev->dev.of_node, "extcon")) {
687d7b3968dSSergei Shtylyov 		priv->edev = extcon_get_edev_by_phandle(&pdev->dev, 0);
688d7b3968dSSergei Shtylyov 		if (IS_ERR(priv->edev))
689d7b3968dSSergei Shtylyov 			return PTR_ERR(priv->edev);
690d7b3968dSSergei Shtylyov 	}
691d7b3968dSSergei Shtylyov 
692f181dbb4SYoshihiro Shimoda 	priv->rsts = devm_reset_control_array_get_optional_shared(&pdev->dev);
693f181dbb4SYoshihiro Shimoda 	if (IS_ERR(priv->rsts))
694f181dbb4SYoshihiro Shimoda 		return PTR_ERR(priv->rsts);
695f181dbb4SYoshihiro Shimoda 
696f1407d5cSKuninori Morimoto 	/*
697f1407d5cSKuninori Morimoto 	 * care platform info
698f1407d5cSKuninori Morimoto 	 */
6998ecef00fSUlrich Hecht 
70048298206SKuninori Morimoto 	memcpy(&priv->dparam,
70148298206SKuninori Morimoto 	       &info->driver_param,
70248298206SKuninori Morimoto 	       sizeof(struct renesas_usbhs_driver_param));
703f1407d5cSKuninori Morimoto 
7048ecef00fSUlrich Hecht 	if (!info->platform_callback.get_id) {
7058ecef00fSUlrich Hecht 		dev_err(&pdev->dev, "no platform callbacks");
7068ecef00fSUlrich Hecht 		return -EINVAL;
7078ecef00fSUlrich Hecht 	}
7088ecef00fSUlrich Hecht 	memcpy(&priv->pfunc,
7098ecef00fSUlrich Hecht 	       &info->platform_callback,
7108ecef00fSUlrich Hecht 	       sizeof(struct renesas_usbhs_platform_callback));
7118ecef00fSUlrich Hecht 
712f1407d5cSKuninori Morimoto 	/* set driver callback functions for platform */
713f1407d5cSKuninori Morimoto 	dfunc			= &info->driver_callback;
714f1407d5cSKuninori Morimoto 	dfunc->notify_hotplug	= usbhsc_drvcllbck_notify_hotplug;
715f1407d5cSKuninori Morimoto 
716f1407d5cSKuninori Morimoto 	/* set default param if platform doesn't have */
71751f141a9SYoshihiro Shimoda 	if (!priv->dparam.pipe_configs) {
71851f141a9SYoshihiro Shimoda 		priv->dparam.pipe_configs = usbhsc_default_pipe;
71951f141a9SYoshihiro Shimoda 		priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe);
720f1407d5cSKuninori Morimoto 	}
72148298206SKuninori Morimoto 	if (!priv->dparam.pio_dma_border)
72248298206SKuninori Morimoto 		priv->dparam.pio_dma_border = 64; /* 64byte */
723f1407d5cSKuninori Morimoto 
724b002ff6eSKuninori Morimoto 	/* FIXME */
725b002ff6eSKuninori Morimoto 	/* runtime power control ? */
72648298206SKuninori Morimoto 	if (priv->pfunc.get_vbus)
727*97a79684SChris Brandt 		usbhs_get_dparam(priv, runtime_pwctrl) = 1;
728b002ff6eSKuninori Morimoto 
729f1407d5cSKuninori Morimoto 	/*
730f1407d5cSKuninori Morimoto 	 * priv settings
731f1407d5cSKuninori Morimoto 	 */
73253069af3SShimoda, Yoshihiro 	priv->irq	= irq_res->start;
73353069af3SShimoda, Yoshihiro 	if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
73453069af3SShimoda, Yoshihiro 		priv->irqflags = IRQF_SHARED;
735f1407d5cSKuninori Morimoto 	priv->pdev	= pdev;
736bc57381eSKuninori Morimoto 	INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
737f1407d5cSKuninori Morimoto 	spin_lock_init(usbhs_priv_to_lock(priv));
738f1407d5cSKuninori Morimoto 
739f1407d5cSKuninori Morimoto 	/* call pipe and module init */
740f1407d5cSKuninori Morimoto 	ret = usbhs_pipe_probe(priv);
741f1407d5cSKuninori Morimoto 	if (ret < 0)
74258efc77cSKuninori Morimoto 		return ret;
743f1407d5cSKuninori Morimoto 
744d3af90a5SKuninori Morimoto 	ret = usbhs_fifo_probe(priv);
745f1407d5cSKuninori Morimoto 	if (ret < 0)
74697f93227SKuninori Morimoto 		goto probe_end_pipe_exit;
747f1407d5cSKuninori Morimoto 
748d3af90a5SKuninori Morimoto 	ret = usbhs_mod_probe(priv);
749d3af90a5SKuninori Morimoto 	if (ret < 0)
750d3af90a5SKuninori Morimoto 		goto probe_end_fifo_exit;
751d3af90a5SKuninori Morimoto 
752f1407d5cSKuninori Morimoto 	/* dev_set_drvdata should be called after usbhs_mod_init */
753c9a0552eSLibo Chen 	platform_set_drvdata(pdev, priv);
754f1407d5cSKuninori Morimoto 
755f181dbb4SYoshihiro Shimoda 	ret = reset_control_deassert(priv->rsts);
756f181dbb4SYoshihiro Shimoda 	if (ret)
757f181dbb4SYoshihiro Shimoda 		goto probe_fail_rst;
758f181dbb4SYoshihiro Shimoda 
7593df0e240SYoshihiro Shimoda 	ret = usbhsc_clk_get(&pdev->dev, priv);
7603df0e240SYoshihiro Shimoda 	if (ret)
7613df0e240SYoshihiro Shimoda 		goto probe_fail_clks;
7623df0e240SYoshihiro Shimoda 
763f1407d5cSKuninori Morimoto 	/*
764f1407d5cSKuninori Morimoto 	 * deviece reset here because
765f1407d5cSKuninori Morimoto 	 * USB device might be used in boot loader.
766f1407d5cSKuninori Morimoto 	 */
767f1407d5cSKuninori Morimoto 	usbhs_sys_clock_ctrl(priv, 0);
768f1407d5cSKuninori Morimoto 
7698ecef00fSUlrich Hecht 	/* check GPIO determining if USB function should be enabled */
7708ecef00fSUlrich Hecht 	if (priv->dparam.enable_gpio) {
7718ecef00fSUlrich Hecht 		gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL);
7728ecef00fSUlrich Hecht 		ret = !gpio_get_value(priv->dparam.enable_gpio);
7738ecef00fSUlrich Hecht 		gpio_free(priv->dparam.enable_gpio);
7748ecef00fSUlrich Hecht 		if (ret) {
7758ecef00fSUlrich Hecht 			dev_warn(&pdev->dev,
7768ecef00fSUlrich Hecht 				 "USB function not selected (GPIO %d)\n",
7778ecef00fSUlrich Hecht 				 priv->dparam.enable_gpio);
7788ecef00fSUlrich Hecht 			ret = -ENOTSUPP;
7798ecef00fSUlrich Hecht 			goto probe_end_mod_exit;
7808ecef00fSUlrich Hecht 		}
7818ecef00fSUlrich Hecht 	}
7828ecef00fSUlrich Hecht 
783f1407d5cSKuninori Morimoto 	/*
784f1407d5cSKuninori Morimoto 	 * platform call
785f1407d5cSKuninori Morimoto 	 *
786f1407d5cSKuninori Morimoto 	 * USB phy setup might depend on CPU/Board.
787f1407d5cSKuninori Morimoto 	 * If platform has its callback functions,
788f1407d5cSKuninori Morimoto 	 * call it here.
789f1407d5cSKuninori Morimoto 	 */
790f1407d5cSKuninori Morimoto 	ret = usbhs_platform_call(priv, hardware_init, pdev);
791f1407d5cSKuninori Morimoto 	if (ret < 0) {
79248ac1e57SSergei Shtylyov 		dev_err(&pdev->dev, "platform init failed.\n");
79397f93227SKuninori Morimoto 		goto probe_end_mod_exit;
794f1407d5cSKuninori Morimoto 	}
795f1407d5cSKuninori Morimoto 
796f1407d5cSKuninori Morimoto 	/* reset phy for connection */
797f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
798f1407d5cSKuninori Morimoto 
799b002ff6eSKuninori Morimoto 	/* power control */
800b002ff6eSKuninori Morimoto 	pm_runtime_enable(&pdev->dev);
801*97a79684SChris Brandt 	if (!usbhs_get_dparam(priv, runtime_pwctrl)) {
802b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
803b002ff6eSKuninori Morimoto 		usbhs_mod_autonomy_mode(priv);
804b002ff6eSKuninori Morimoto 	}
805b002ff6eSKuninori Morimoto 
806f1407d5cSKuninori Morimoto 	/*
807f1407d5cSKuninori Morimoto 	 * manual call notify_hotplug for cold plug
808f1407d5cSKuninori Morimoto 	 */
809368504c0SSergei Shtylyov 	usbhsc_drvcllbck_notify_hotplug(pdev);
810f1407d5cSKuninori Morimoto 
811f1407d5cSKuninori Morimoto 	dev_info(&pdev->dev, "probed\n");
812f1407d5cSKuninori Morimoto 
813f1407d5cSKuninori Morimoto 	return ret;
814f1407d5cSKuninori Morimoto 
815f1407d5cSKuninori Morimoto probe_end_mod_exit:
8163df0e240SYoshihiro Shimoda 	usbhsc_clk_put(priv);
8173df0e240SYoshihiro Shimoda probe_fail_clks:
818f181dbb4SYoshihiro Shimoda 	reset_control_assert(priv->rsts);
819f181dbb4SYoshihiro Shimoda probe_fail_rst:
820f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
821d3af90a5SKuninori Morimoto probe_end_fifo_exit:
822d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
82397f93227SKuninori Morimoto probe_end_pipe_exit:
82497f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
825f1407d5cSKuninori Morimoto 
8260d799503SYoshihiro Shimoda 	dev_info(&pdev->dev, "probe failed (%d)\n", ret);
827f1407d5cSKuninori Morimoto 
828f1407d5cSKuninori Morimoto 	return ret;
829f1407d5cSKuninori Morimoto }
830f1407d5cSKuninori Morimoto 
831fb4e98abSBill Pemberton static int usbhs_remove(struct platform_device *pdev)
832f1407d5cSKuninori Morimoto {
833206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
834f0f14a7aSKuninori Morimoto 	struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
835af32fe51SKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
836f1407d5cSKuninori Morimoto 
837f1407d5cSKuninori Morimoto 	dev_dbg(&pdev->dev, "usb remove\n");
838f1407d5cSKuninori Morimoto 
839af32fe51SKuninori Morimoto 	dfunc->notify_hotplug = NULL;
840af32fe51SKuninori Morimoto 
841b002ff6eSKuninori Morimoto 	/* power off */
842*97a79684SChris Brandt 	if (!usbhs_get_dparam(priv, runtime_pwctrl))
843b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
844f1407d5cSKuninori Morimoto 
845b002ff6eSKuninori Morimoto 	pm_runtime_disable(&pdev->dev);
846f1407d5cSKuninori Morimoto 
847f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, hardware_exit, pdev);
8483df0e240SYoshihiro Shimoda 	usbhsc_clk_put(priv);
849f181dbb4SYoshihiro Shimoda 	reset_control_assert(priv->rsts);
850f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
851d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
85297f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
853f1407d5cSKuninori Morimoto 
854f1407d5cSKuninori Morimoto 	return 0;
855f1407d5cSKuninori Morimoto }
856f1407d5cSKuninori Morimoto 
857eaf3074eSArnd Bergmann static __maybe_unused int usbhsc_suspend(struct device *dev)
858ca8a282aSKuninori Morimoto {
859ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
860ca8a282aSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
861ca8a282aSKuninori Morimoto 
862ca8a282aSKuninori Morimoto 	if (mod) {
863ca8a282aSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
864ca8a282aSKuninori Morimoto 		usbhs_mod_change(priv, -1);
865ca8a282aSKuninori Morimoto 	}
866ca8a282aSKuninori Morimoto 
867*97a79684SChris Brandt 	if (mod || !usbhs_get_dparam(priv, runtime_pwctrl))
868ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
869ca8a282aSKuninori Morimoto 
870ca8a282aSKuninori Morimoto 	return 0;
871ca8a282aSKuninori Morimoto }
872ca8a282aSKuninori Morimoto 
873eaf3074eSArnd Bergmann static __maybe_unused int usbhsc_resume(struct device *dev)
874ca8a282aSKuninori Morimoto {
875ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
876ca8a282aSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
877ca8a282aSKuninori Morimoto 
878*97a79684SChris Brandt 	if (!usbhs_get_dparam(priv, runtime_pwctrl)) {
879ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
88059a0879aSYoshihiro Shimoda 		usbhs_mod_autonomy_mode(priv);
88159a0879aSYoshihiro Shimoda 	}
882ca8a282aSKuninori Morimoto 
8835b50d3b5SKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
8845b50d3b5SKuninori Morimoto 
8855b50d3b5SKuninori Morimoto 	usbhsc_drvcllbck_notify_hotplug(pdev);
886ca8a282aSKuninori Morimoto 
887ca8a282aSKuninori Morimoto 	return 0;
888ca8a282aSKuninori Morimoto }
889ca8a282aSKuninori Morimoto 
890d54d334eSYoshihiro Shimoda static SIMPLE_DEV_PM_OPS(usbhsc_pm_ops, usbhsc_suspend, usbhsc_resume);
891ca8a282aSKuninori Morimoto 
892f1407d5cSKuninori Morimoto static struct platform_driver renesas_usbhs_driver = {
893f1407d5cSKuninori Morimoto 	.driver		= {
894f1407d5cSKuninori Morimoto 		.name	= "renesas_usbhs",
895ca8a282aSKuninori Morimoto 		.pm	= &usbhsc_pm_ops,
896b854100eSYoshihiro Shimoda 		.of_match_table = of_match_ptr(usbhs_of_match),
897f1407d5cSKuninori Morimoto 	},
898f1407d5cSKuninori Morimoto 	.probe		= usbhs_probe,
8997690417dSBill Pemberton 	.remove		= usbhs_remove,
900f1407d5cSKuninori Morimoto };
901f1407d5cSKuninori Morimoto 
902cc27c96cSAxel Lin module_platform_driver(renesas_usbhs_driver);
903f1407d5cSKuninori Morimoto 
904f1407d5cSKuninori Morimoto MODULE_LICENSE("GPL");
905f1407d5cSKuninori Morimoto MODULE_DESCRIPTION("Renesas USB driver");
906f1407d5cSKuninori Morimoto MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
907