xref: /openbmc/linux/drivers/usb/renesas_usbhs/common.c (revision 772ce81264b179c0e61340998e3b29e900b2fa6d)
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  */
17148e1134SThierry Reding #include <linux/err.h>
188ecef00fSUlrich Hecht #include <linux/gpio.h>
19f1407d5cSKuninori Morimoto #include <linux/io.h>
20f1407d5cSKuninori Morimoto #include <linux/module.h>
21b854100eSYoshihiro Shimoda #include <linux/of_device.h>
22b854100eSYoshihiro Shimoda #include <linux/of_gpio.h>
23f1407d5cSKuninori Morimoto #include <linux/pm_runtime.h>
24f1407d5cSKuninori Morimoto #include <linux/slab.h>
25f1407d5cSKuninori Morimoto #include <linux/sysfs.h>
26cc502bb7SPaul Bolle #include "common.h"
278ecef00fSUlrich Hecht #include "rcar2.h"
28de18757eSYoshihiro Shimoda #include "rcar3.h"
29f1407d5cSKuninori Morimoto 
30233f519dSKuninori Morimoto /*
31233f519dSKuninori Morimoto  *		image of renesas_usbhs
32233f519dSKuninori Morimoto  *
33233f519dSKuninori Morimoto  * ex) gadget case
34233f519dSKuninori Morimoto 
35233f519dSKuninori Morimoto  * mod.c
36233f519dSKuninori Morimoto  * mod_gadget.c
37233f519dSKuninori Morimoto  * mod_host.c		pipe.c		fifo.c
38233f519dSKuninori Morimoto  *
39233f519dSKuninori Morimoto  *			+-------+	+-----------+
40233f519dSKuninori Morimoto  *			| pipe0 |------>| fifo pio  |
41233f519dSKuninori Morimoto  * +------------+	+-------+	+-----------+
42233f519dSKuninori Morimoto  * | mod_gadget |=====> | pipe1 |--+
43233f519dSKuninori Morimoto  * +------------+	+-------+  |	+-----------+
44233f519dSKuninori Morimoto  *			| pipe2 |  |  +-| fifo dma0 |
45233f519dSKuninori Morimoto  * +------------+	+-------+  |  |	+-----------+
46233f519dSKuninori Morimoto  * | mod_host   |	| pipe3 |<-|--+
47233f519dSKuninori Morimoto  * +------------+	+-------+  |	+-----------+
48233f519dSKuninori Morimoto  *			| ....  |  +--->| fifo dma1 |
49233f519dSKuninori Morimoto  *			| ....  |	+-----------+
50233f519dSKuninori Morimoto  */
51233f519dSKuninori Morimoto 
52233f519dSKuninori Morimoto 
53b002ff6eSKuninori Morimoto #define USBHSF_RUNTIME_PWCTRL	(1 << 0)
54b002ff6eSKuninori Morimoto 
55b002ff6eSKuninori Morimoto /* status */
56b002ff6eSKuninori Morimoto #define usbhsc_flags_init(p)   do {(p)->flags = 0; } while (0)
57b002ff6eSKuninori Morimoto #define usbhsc_flags_set(p, b) ((p)->flags |=  (b))
58b002ff6eSKuninori Morimoto #define usbhsc_flags_clr(p, b) ((p)->flags &= ~(b))
59b002ff6eSKuninori Morimoto #define usbhsc_flags_has(p, b) ((p)->flags &   (b))
60b002ff6eSKuninori Morimoto 
61f1407d5cSKuninori Morimoto /*
62f1407d5cSKuninori Morimoto  * platform call back
63f1407d5cSKuninori Morimoto  *
64f1407d5cSKuninori Morimoto  * renesas usb support platform callback function.
65f1407d5cSKuninori Morimoto  * Below macro call it.
66f1407d5cSKuninori Morimoto  * if platform doesn't have callback, it return 0 (no error)
67f1407d5cSKuninori Morimoto  */
68f1407d5cSKuninori Morimoto #define usbhs_platform_call(priv, func, args...)\
69f1407d5cSKuninori Morimoto 	(!(priv) ? -ENODEV :			\
7048298206SKuninori Morimoto 	 !((priv)->pfunc.func) ? 0 :		\
7148298206SKuninori Morimoto 	 (priv)->pfunc.func(args))
72f1407d5cSKuninori Morimoto 
73f1407d5cSKuninori Morimoto /*
74f1407d5cSKuninori Morimoto  *		common functions
75f1407d5cSKuninori Morimoto  */
76f1407d5cSKuninori Morimoto u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
77f1407d5cSKuninori Morimoto {
78f1407d5cSKuninori Morimoto 	return ioread16(priv->base + reg);
79f1407d5cSKuninori Morimoto }
80f1407d5cSKuninori Morimoto 
81f1407d5cSKuninori Morimoto void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
82f1407d5cSKuninori Morimoto {
83f1407d5cSKuninori Morimoto 	iowrite16(data, priv->base + reg);
84f1407d5cSKuninori Morimoto }
85f1407d5cSKuninori Morimoto 
86f1407d5cSKuninori Morimoto void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
87f1407d5cSKuninori Morimoto {
88f1407d5cSKuninori Morimoto 	u16 val = usbhs_read(priv, reg);
89f1407d5cSKuninori Morimoto 
90f1407d5cSKuninori Morimoto 	val &= ~mask;
91f1407d5cSKuninori Morimoto 	val |= data & mask;
92f1407d5cSKuninori Morimoto 
93f1407d5cSKuninori Morimoto 	usbhs_write(priv, reg, val);
94f1407d5cSKuninori Morimoto }
95f1407d5cSKuninori Morimoto 
96206dcc2cSKuninori Morimoto struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
97206dcc2cSKuninori Morimoto {
98206dcc2cSKuninori Morimoto 	return dev_get_drvdata(&pdev->dev);
99206dcc2cSKuninori Morimoto }
100206dcc2cSKuninori Morimoto 
101f1407d5cSKuninori Morimoto /*
102f1407d5cSKuninori Morimoto  *		syscfg functions
103f1407d5cSKuninori Morimoto  */
10476190152SKuninori Morimoto static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
105f1407d5cSKuninori Morimoto {
106f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
107f1407d5cSKuninori Morimoto }
108f1407d5cSKuninori Morimoto 
109f1407d5cSKuninori Morimoto void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
110f1407d5cSKuninori Morimoto {
1113244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
1123244a7b4SKuninori Morimoto 	u16 val  = DCFM | DRPD | HSE | USBE;
113f427eb64SKuninori Morimoto 	int has_otg = usbhs_get_dparam(priv, has_otg);
114f427eb64SKuninori Morimoto 
115f427eb64SKuninori Morimoto 	if (has_otg)
116f427eb64SKuninori Morimoto 		usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN));
117f1407d5cSKuninori Morimoto 
118f1407d5cSKuninori Morimoto 	/*
119f1407d5cSKuninori Morimoto 	 * if enable
120f1407d5cSKuninori Morimoto 	 *
121f1407d5cSKuninori Morimoto 	 * - select Host mode
122f1407d5cSKuninori Morimoto 	 * - D+ Line/D- Line Pull-down
123f1407d5cSKuninori Morimoto 	 */
124f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
125f1407d5cSKuninori Morimoto }
126f1407d5cSKuninori Morimoto 
127f1407d5cSKuninori Morimoto void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
128f1407d5cSKuninori Morimoto {
1293244a7b4SKuninori Morimoto 	u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
13004a5def3STakeshi Kihara 	u16 val  = HSE | USBE;
131f1407d5cSKuninori Morimoto 
132f1407d5cSKuninori Morimoto 	/*
133f1407d5cSKuninori Morimoto 	 * if enable
134f1407d5cSKuninori Morimoto 	 *
135f1407d5cSKuninori Morimoto 	 * - select Function mode
13604a5def3STakeshi Kihara 	 * - D+ Line Pull-up is disabled
13704a5def3STakeshi Kihara 	 *      When D+ Line Pull-up is enabled,
13804a5def3STakeshi Kihara 	 *      calling usbhs_sys_function_pullup(,1)
139f1407d5cSKuninori Morimoto 	 */
140f1407d5cSKuninori Morimoto 	usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
141f1407d5cSKuninori Morimoto }
142f1407d5cSKuninori Morimoto 
1434cd2f599Skuninori.morimoto.gx@renesas.com void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable)
1444cd2f599Skuninori.morimoto.gx@renesas.com {
1454cd2f599Skuninori.morimoto.gx@renesas.com 	usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0);
1464cd2f599Skuninori.morimoto.gx@renesas.com }
1474cd2f599Skuninori.morimoto.gx@renesas.com 
148dfbb7f4fSKuninori Morimoto void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
149dfbb7f4fSKuninori Morimoto {
150dfbb7f4fSKuninori Morimoto 	usbhs_write(priv, TESTMODE, mode);
151dfbb7f4fSKuninori Morimoto }
152dfbb7f4fSKuninori Morimoto 
153f1407d5cSKuninori Morimoto /*
154f1407d5cSKuninori Morimoto  *		frame functions
155f1407d5cSKuninori Morimoto  */
156f1407d5cSKuninori Morimoto int usbhs_frame_get_num(struct usbhs_priv *priv)
157f1407d5cSKuninori Morimoto {
158f1407d5cSKuninori Morimoto 	return usbhs_read(priv, FRMNUM) & FRNM_MASK;
159f1407d5cSKuninori Morimoto }
160f1407d5cSKuninori Morimoto 
161f1407d5cSKuninori Morimoto /*
162ef8bedb9SKuninori Morimoto  *		usb request functions
163ef8bedb9SKuninori Morimoto  */
164ef8bedb9SKuninori Morimoto void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
165ef8bedb9SKuninori Morimoto {
166ef8bedb9SKuninori Morimoto 	u16 val;
167ef8bedb9SKuninori Morimoto 
168ef8bedb9SKuninori Morimoto 	val = usbhs_read(priv, USBREQ);
169ef8bedb9SKuninori Morimoto 	req->bRequest		= (val >> 8) & 0xFF;
170ef8bedb9SKuninori Morimoto 	req->bRequestType	= (val >> 0) & 0xFF;
171ef8bedb9SKuninori Morimoto 
172ef8bedb9SKuninori Morimoto 	req->wValue	= usbhs_read(priv, USBVAL);
173ef8bedb9SKuninori Morimoto 	req->wIndex	= usbhs_read(priv, USBINDX);
174ef8bedb9SKuninori Morimoto 	req->wLength	= usbhs_read(priv, USBLENG);
175ef8bedb9SKuninori Morimoto }
176ef8bedb9SKuninori Morimoto 
177ef8bedb9SKuninori Morimoto void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
178ef8bedb9SKuninori Morimoto {
179ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBREQ,  (req->bRequest << 8) | req->bRequestType);
180ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBVAL,  req->wValue);
181ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBINDX, req->wIndex);
182ef8bedb9SKuninori Morimoto 	usbhs_write(priv, USBLENG, req->wLength);
183ef8bedb9SKuninori Morimoto 
184ef8bedb9SKuninori Morimoto 	usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
185ef8bedb9SKuninori Morimoto }
186ef8bedb9SKuninori Morimoto 
187ef8bedb9SKuninori Morimoto /*
188258485d9SKuninori Morimoto  *		bus/vbus functions
189258485d9SKuninori Morimoto  */
190258485d9SKuninori Morimoto void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
191258485d9SKuninori Morimoto {
192a9be4a45SKuninori Morimoto 	u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
193a9be4a45SKuninori Morimoto 
194a9be4a45SKuninori Morimoto 	if (status != USBRST) {
195a9be4a45SKuninori Morimoto 		struct device *dev = usbhs_priv_to_dev(priv);
196a9be4a45SKuninori Morimoto 		dev_err(dev, "usbhs should be reset\n");
197a9be4a45SKuninori Morimoto 	}
198a9be4a45SKuninori Morimoto 
199258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
200258485d9SKuninori Morimoto }
201258485d9SKuninori Morimoto 
202258485d9SKuninori Morimoto void usbhs_bus_send_reset(struct usbhs_priv *priv)
203258485d9SKuninori Morimoto {
204258485d9SKuninori Morimoto 	usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
205258485d9SKuninori Morimoto }
206258485d9SKuninori Morimoto 
20775587f52SKuninori Morimoto int usbhs_bus_get_speed(struct usbhs_priv *priv)
20875587f52SKuninori Morimoto {
20975587f52SKuninori Morimoto 	u16 dvstctr = usbhs_read(priv, DVSTCTR);
21075587f52SKuninori Morimoto 
21175587f52SKuninori Morimoto 	switch (RHST & dvstctr) {
21275587f52SKuninori Morimoto 	case RHST_LOW_SPEED:
21375587f52SKuninori Morimoto 		return USB_SPEED_LOW;
21475587f52SKuninori Morimoto 	case RHST_FULL_SPEED:
21575587f52SKuninori Morimoto 		return USB_SPEED_FULL;
21675587f52SKuninori Morimoto 	case RHST_HIGH_SPEED:
21775587f52SKuninori Morimoto 		return USB_SPEED_HIGH;
21875587f52SKuninori Morimoto 	}
21975587f52SKuninori Morimoto 
22075587f52SKuninori Morimoto 	return USB_SPEED_UNKNOWN;
22175587f52SKuninori Morimoto }
22275587f52SKuninori Morimoto 
223258485d9SKuninori Morimoto int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
224258485d9SKuninori Morimoto {
225258485d9SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
226258485d9SKuninori Morimoto 
227258485d9SKuninori Morimoto 	return usbhs_platform_call(priv, set_vbus, pdev, enable);
228258485d9SKuninori Morimoto }
229258485d9SKuninori Morimoto 
230258485d9SKuninori Morimoto static void usbhsc_bus_init(struct usbhs_priv *priv)
231258485d9SKuninori Morimoto {
232258485d9SKuninori Morimoto 	usbhs_write(priv, DVSTCTR, 0);
233258485d9SKuninori Morimoto 
234258485d9SKuninori Morimoto 	usbhs_vbus_ctrl(priv, 0);
235258485d9SKuninori Morimoto }
236258485d9SKuninori Morimoto 
237258485d9SKuninori Morimoto /*
238eb05191fSKuninori Morimoto  *		device configuration
239eb05191fSKuninori Morimoto  */
2403dd49268SKuninori Morimoto int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
241eb05191fSKuninori Morimoto 			   u16 upphub, u16 hubport, u16 speed)
242eb05191fSKuninori Morimoto {
243eb05191fSKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
244eb05191fSKuninori Morimoto 	u16 usbspd = 0;
245eb05191fSKuninori Morimoto 	u32 reg = DEVADD0 + (2 * devnum);
246eb05191fSKuninori Morimoto 
247eb05191fSKuninori Morimoto 	if (devnum > 10) {
248eb05191fSKuninori Morimoto 		dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
249eb05191fSKuninori Morimoto 		return -EIO;
250eb05191fSKuninori Morimoto 	}
251eb05191fSKuninori Morimoto 
252eb05191fSKuninori Morimoto 	if (upphub > 0xA) {
253eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported hub number %d\n", upphub);
254eb05191fSKuninori Morimoto 		return -EIO;
255eb05191fSKuninori Morimoto 	}
256eb05191fSKuninori Morimoto 
257eb05191fSKuninori Morimoto 	switch (speed) {
258eb05191fSKuninori Morimoto 	case USB_SPEED_LOW:
259eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_LOW;
260eb05191fSKuninori Morimoto 		break;
261eb05191fSKuninori Morimoto 	case USB_SPEED_FULL:
262eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_FULL;
263eb05191fSKuninori Morimoto 		break;
264eb05191fSKuninori Morimoto 	case USB_SPEED_HIGH:
265eb05191fSKuninori Morimoto 		usbspd = USBSPD_SPEED_HIGH;
266eb05191fSKuninori Morimoto 		break;
267eb05191fSKuninori Morimoto 	default:
268eb05191fSKuninori Morimoto 		dev_err(dev, "unsupported speed %d\n", speed);
269eb05191fSKuninori Morimoto 		return -EIO;
270eb05191fSKuninori Morimoto 	}
271eb05191fSKuninori Morimoto 
272eb05191fSKuninori Morimoto 	usbhs_write(priv, reg,	UPPHUB(upphub)	|
273eb05191fSKuninori Morimoto 				HUBPORT(hubport)|
274eb05191fSKuninori Morimoto 				USBSPD(usbspd));
275eb05191fSKuninori Morimoto 
276eb05191fSKuninori Morimoto 	return 0;
277eb05191fSKuninori Morimoto }
278eb05191fSKuninori Morimoto 
279eb05191fSKuninori Morimoto /*
280ab330cf3SYoshihiro Shimoda  *		interrupt functions
281ab330cf3SYoshihiro Shimoda  */
282ab330cf3SYoshihiro Shimoda void usbhs_xxxsts_clear(struct usbhs_priv *priv, u16 sts_reg, u16 bit)
283ab330cf3SYoshihiro Shimoda {
284ab330cf3SYoshihiro Shimoda 	u16 pipe_mask = (u16)GENMASK(usbhs_get_dparam(priv, pipe_size), 0);
285ab330cf3SYoshihiro Shimoda 
286ab330cf3SYoshihiro Shimoda 	usbhs_write(priv, sts_reg, ~(1 << bit) & pipe_mask);
287ab330cf3SYoshihiro Shimoda }
288ab330cf3SYoshihiro Shimoda 
289ab330cf3SYoshihiro Shimoda /*
290f1407d5cSKuninori Morimoto  *		local functions
291f1407d5cSKuninori Morimoto  */
29211935de5SKuninori Morimoto static void usbhsc_set_buswait(struct usbhs_priv *priv)
293f1407d5cSKuninori Morimoto {
294f1407d5cSKuninori Morimoto 	int wait = usbhs_get_dparam(priv, buswait_bwait);
295f1407d5cSKuninori Morimoto 
296f1407d5cSKuninori Morimoto 	/* set bus wait if platform have */
297f1407d5cSKuninori Morimoto 	if (wait)
298f1407d5cSKuninori Morimoto 		usbhs_bset(priv, BUSWAIT, 0x000F, wait);
299f1407d5cSKuninori Morimoto }
300f1407d5cSKuninori Morimoto 
301f1407d5cSKuninori Morimoto /*
302f1407d5cSKuninori Morimoto  *		platform default param
303f1407d5cSKuninori Morimoto  */
3048ecef00fSUlrich Hecht 
3058ecef00fSUlrich Hecht /* commonly used on old SH-Mobile SoCs */
30651f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_default_pipe[] = {
30751f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
30851f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, false),
30951f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x18, false),
31051f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x28, true),
31151f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x38, true),
31251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
31351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
31451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
31551f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
31651f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x07, false),
317f1407d5cSKuninori Morimoto };
318f1407d5cSKuninori Morimoto 
3198ecef00fSUlrich Hecht /* commonly used on newer SH-Mobile and R-Car SoCs */
32051f141a9SYoshihiro Shimoda static struct renesas_usbhs_driver_pipe_config usbhsc_new_pipe[] = {
32151f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
32251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true),
32351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true),
32451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
32551f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true),
32651f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true),
32751f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
32851f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
32951f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
33051f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x78, true),
33151f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x88, true),
33251f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x98, true),
33351f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xa8, true),
33451f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xb8, true),
33551f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xc8, true),
33651f141a9SYoshihiro Shimoda 	RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xd8, true),
3378ecef00fSUlrich Hecht };
3388ecef00fSUlrich Hecht 
339f1407d5cSKuninori Morimoto /*
3406e267da8SKuninori Morimoto  *		power control
3416e267da8SKuninori Morimoto  */
3426e267da8SKuninori Morimoto static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
3436e267da8SKuninori Morimoto {
344f1ee56a0SKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
3456e267da8SKuninori Morimoto 	struct device *dev = usbhs_priv_to_dev(priv);
3466e267da8SKuninori Morimoto 
3476e267da8SKuninori Morimoto 	if (enable) {
3486e267da8SKuninori Morimoto 		/* enable PM */
3496e267da8SKuninori Morimoto 		pm_runtime_get_sync(dev);
3506e267da8SKuninori Morimoto 
351f1ee56a0SKuninori Morimoto 		/* enable platform power */
352f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
353f1ee56a0SKuninori Morimoto 
3546e267da8SKuninori Morimoto 		/* USB on */
3556e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
3566e267da8SKuninori Morimoto 	} else {
3576e267da8SKuninori Morimoto 		/* USB off */
3586e267da8SKuninori Morimoto 		usbhs_sys_clock_ctrl(priv, enable);
3596e267da8SKuninori Morimoto 
360f1ee56a0SKuninori Morimoto 		/* disable platform power */
361f1ee56a0SKuninori Morimoto 		usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
362f1ee56a0SKuninori Morimoto 
3636e267da8SKuninori Morimoto 		/* disable PM */
3646e267da8SKuninori Morimoto 		pm_runtime_put_sync(dev);
3656e267da8SKuninori Morimoto 	}
3666e267da8SKuninori Morimoto }
3676e267da8SKuninori Morimoto 
3686e267da8SKuninori Morimoto /*
369ca8a282aSKuninori Morimoto  *		hotplug
370f1407d5cSKuninori Morimoto  */
371ca8a282aSKuninori Morimoto static void usbhsc_hotplug(struct usbhs_priv *priv)
372f1407d5cSKuninori Morimoto {
373f1407d5cSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
374f1407d5cSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
375f1407d5cSKuninori Morimoto 	int id;
376f1407d5cSKuninori Morimoto 	int enable;
377d7b3968dSSergei Shtylyov 	int cable;
378f1407d5cSKuninori Morimoto 	int ret;
379f1407d5cSKuninori Morimoto 
380f1407d5cSKuninori Morimoto 	/*
381f1407d5cSKuninori Morimoto 	 * get vbus status from platform
382f1407d5cSKuninori Morimoto 	 */
383f1407d5cSKuninori Morimoto 	enable = usbhs_platform_call(priv, get_vbus, pdev);
384f1407d5cSKuninori Morimoto 
385f1407d5cSKuninori Morimoto 	/*
386f1407d5cSKuninori Morimoto 	 * get id from platform
387f1407d5cSKuninori Morimoto 	 */
388f1407d5cSKuninori Morimoto 	id = usbhs_platform_call(priv, get_id, pdev);
389f1407d5cSKuninori Morimoto 
390f1407d5cSKuninori Morimoto 	if (enable && !mod) {
391d7b3968dSSergei Shtylyov 		if (priv->edev) {
39250297b79SChanwoo Choi 			cable = extcon_get_cable_state_(priv->edev, EXTCON_USB_HOST);
393d7b3968dSSergei Shtylyov 			if ((cable > 0 && id != USBHS_HOST) ||
394d7b3968dSSergei Shtylyov 			    (!cable && id != USBHS_GADGET)) {
395d7b3968dSSergei Shtylyov 				dev_info(&pdev->dev,
396d7b3968dSSergei Shtylyov 					 "USB cable plugged in doesn't match the selected role!\n");
397d7b3968dSSergei Shtylyov 				return;
398d7b3968dSSergei Shtylyov 			}
399d7b3968dSSergei Shtylyov 		}
400d7b3968dSSergei Shtylyov 
401f1407d5cSKuninori Morimoto 		ret = usbhs_mod_change(priv, id);
402f1407d5cSKuninori Morimoto 		if (ret < 0)
403f1407d5cSKuninori Morimoto 			return;
404f1407d5cSKuninori Morimoto 
405f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s enable\n", __func__);
406f1407d5cSKuninori Morimoto 
4076e267da8SKuninori Morimoto 		/* power on */
408b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
4096e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
410f1407d5cSKuninori Morimoto 
411258485d9SKuninori Morimoto 		/* bus init */
412258485d9SKuninori Morimoto 		usbhsc_set_buswait(priv);
413258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
414258485d9SKuninori Morimoto 
415f1407d5cSKuninori Morimoto 		/* module start */
416f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, start, priv);
417f1407d5cSKuninori Morimoto 
418f1407d5cSKuninori Morimoto 	} else if (!enable && mod) {
419f1407d5cSKuninori Morimoto 		dev_dbg(&pdev->dev, "%s disable\n", __func__);
420f1407d5cSKuninori Morimoto 
421f1407d5cSKuninori Morimoto 		/* module stop */
422f1407d5cSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
423f1407d5cSKuninori Morimoto 
424258485d9SKuninori Morimoto 		/* bus init */
425258485d9SKuninori Morimoto 		usbhsc_bus_init(priv);
426258485d9SKuninori Morimoto 
4276e267da8SKuninori Morimoto 		/* power off */
428b002ff6eSKuninori Morimoto 		if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
4296e267da8SKuninori Morimoto 			usbhsc_power_ctrl(priv, enable);
430f1407d5cSKuninori Morimoto 
431f1407d5cSKuninori Morimoto 		usbhs_mod_change(priv, -1);
432f1407d5cSKuninori Morimoto 
433f1407d5cSKuninori Morimoto 		/* reset phy for next connection */
434f1407d5cSKuninori Morimoto 		usbhs_platform_call(priv, phy_reset, pdev);
435f1407d5cSKuninori Morimoto 	}
436f1407d5cSKuninori Morimoto }
437f1407d5cSKuninori Morimoto 
438ca8a282aSKuninori Morimoto /*
439ca8a282aSKuninori Morimoto  *		notify hotplug
440ca8a282aSKuninori Morimoto  */
441ca8a282aSKuninori Morimoto static void usbhsc_notify_hotplug(struct work_struct *work)
442ca8a282aSKuninori Morimoto {
443ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = container_of(work,
444ca8a282aSKuninori Morimoto 					       struct usbhs_priv,
445ca8a282aSKuninori Morimoto 					       notify_hotplug_work.work);
446ca8a282aSKuninori Morimoto 	usbhsc_hotplug(priv);
447ca8a282aSKuninori Morimoto }
448ca8a282aSKuninori Morimoto 
449b4fcea2aSKuninori Morimoto static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
450f1407d5cSKuninori Morimoto {
451206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
452bc57381eSKuninori Morimoto 	int delay = usbhs_get_dparam(priv, detection_delay);
453f1407d5cSKuninori Morimoto 
454f1407d5cSKuninori Morimoto 	/*
455f1407d5cSKuninori Morimoto 	 * This functions will be called in interrupt.
456f1407d5cSKuninori Morimoto 	 * To make sure safety context,
457f1407d5cSKuninori Morimoto 	 * use workqueue for usbhs_notify_hotplug
458f1407d5cSKuninori Morimoto 	 */
459a49a88f1SKuninori Morimoto 	schedule_delayed_work(&priv->notify_hotplug_work,
460a49a88f1SKuninori Morimoto 			      msecs_to_jiffies(delay));
461f1407d5cSKuninori Morimoto 	return 0;
462f1407d5cSKuninori Morimoto }
463f1407d5cSKuninori Morimoto 
464f1407d5cSKuninori Morimoto /*
465f1407d5cSKuninori Morimoto  *		platform functions
466f1407d5cSKuninori Morimoto  */
467b854100eSYoshihiro Shimoda static const struct of_device_id usbhs_of_match[] = {
468b854100eSYoshihiro Shimoda 	{
469b854100eSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7790",
470e0213bc5SYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
471b854100eSYoshihiro Shimoda 	},
472b854100eSYoshihiro Shimoda 	{
473b854100eSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7791",
474e0213bc5SYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
475b854100eSYoshihiro Shimoda 	},
476af6e613bSYoshihiro Shimoda 	{
477af6e613bSYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7794",
478af6e613bSYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
479af6e613bSYoshihiro Shimoda 	},
480f5f6afa8SYoshihiro Shimoda 	{
481f5f6afa8SYoshihiro Shimoda 		.compatible = "renesas,usbhs-r8a7795",
482de18757eSYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN3,
483f5f6afa8SYoshihiro Shimoda 	},
4842f1a993aSSimon Horman 	{
4852f1a993aSSimon Horman 		.compatible = "renesas,rcar-gen2-usbhs",
4862f1a993aSSimon Horman 		.data = (void *)USBHS_TYPE_RCAR_GEN2,
4872f1a993aSSimon Horman 	},
4882f1a993aSSimon Horman 	{
4892f1a993aSSimon Horman 		.compatible = "renesas,rcar-gen3-usbhs",
490de18757eSYoshihiro Shimoda 		.data = (void *)USBHS_TYPE_RCAR_GEN3,
4912f1a993aSSimon Horman 	},
492b854100eSYoshihiro Shimoda 	{ },
493b854100eSYoshihiro Shimoda };
494b854100eSYoshihiro Shimoda MODULE_DEVICE_TABLE(of, usbhs_of_match);
495b854100eSYoshihiro Shimoda 
496b854100eSYoshihiro Shimoda static struct renesas_usbhs_platform_info *usbhs_parse_dt(struct device *dev)
497b854100eSYoshihiro Shimoda {
498b854100eSYoshihiro Shimoda 	struct renesas_usbhs_platform_info *info;
499b854100eSYoshihiro Shimoda 	struct renesas_usbhs_driver_param *dparam;
500b854100eSYoshihiro Shimoda 	const struct of_device_id *of_id = of_match_device(usbhs_of_match, dev);
501b854100eSYoshihiro Shimoda 	u32 tmp;
502b854100eSYoshihiro Shimoda 	int gpio;
503b854100eSYoshihiro Shimoda 
504b854100eSYoshihiro Shimoda 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
505b854100eSYoshihiro Shimoda 	if (!info)
506b854100eSYoshihiro Shimoda 		return NULL;
507b854100eSYoshihiro Shimoda 
508b854100eSYoshihiro Shimoda 	dparam = &info->driver_param;
5099ae7ce00SYoshihiro Shimoda 	dparam->type = of_id ? (uintptr_t)of_id->data : 0;
510b854100eSYoshihiro Shimoda 	if (!of_property_read_u32(dev->of_node, "renesas,buswait", &tmp))
511b854100eSYoshihiro Shimoda 		dparam->buswait_bwait = tmp;
512b854100eSYoshihiro Shimoda 	gpio = of_get_named_gpio_flags(dev->of_node, "renesas,enable-gpio", 0,
513b854100eSYoshihiro Shimoda 				       NULL);
514b854100eSYoshihiro Shimoda 	if (gpio > 0)
515b854100eSYoshihiro Shimoda 		dparam->enable_gpio = gpio;
516b854100eSYoshihiro Shimoda 
517*772ce812SYoshihiro Shimoda 	if (dparam->type == USBHS_TYPE_RCAR_GEN2 ||
518*772ce812SYoshihiro Shimoda 	    dparam->type == USBHS_TYPE_RCAR_GEN3)
519ab330cf3SYoshihiro Shimoda 		dparam->has_usb_dmac = 1;
520ab330cf3SYoshihiro Shimoda 
521b854100eSYoshihiro Shimoda 	return info;
522b854100eSYoshihiro Shimoda }
523b854100eSYoshihiro Shimoda 
524b7a8d17dSKuninori Morimoto static int usbhs_probe(struct platform_device *pdev)
525f1407d5cSKuninori Morimoto {
526ace0a5f9SJingoo Han 	struct renesas_usbhs_platform_info *info = dev_get_platdata(&pdev->dev);
527f1407d5cSKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc;
528f1407d5cSKuninori Morimoto 	struct usbhs_priv *priv;
52953069af3SShimoda, Yoshihiro 	struct resource *res, *irq_res;
530f1407d5cSKuninori Morimoto 	int ret;
531f1407d5cSKuninori Morimoto 
532b854100eSYoshihiro Shimoda 	/* check device node */
533b854100eSYoshihiro Shimoda 	if (pdev->dev.of_node)
534b854100eSYoshihiro Shimoda 		info = pdev->dev.platform_data = usbhs_parse_dt(&pdev->dev);
535b854100eSYoshihiro Shimoda 
536f1407d5cSKuninori Morimoto 	/* check platform information */
5378ecef00fSUlrich Hecht 	if (!info) {
538f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "no platform information\n");
539f1407d5cSKuninori Morimoto 		return -EINVAL;
540f1407d5cSKuninori Morimoto 	}
541f1407d5cSKuninori Morimoto 
542f1407d5cSKuninori Morimoto 	/* platform data */
54353069af3SShimoda, Yoshihiro 	irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
544687f16b8SVarka Bhadram 	if (!irq_res) {
545f1407d5cSKuninori Morimoto 		dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
546f1407d5cSKuninori Morimoto 		return -ENODEV;
547f1407d5cSKuninori Morimoto 	}
548f1407d5cSKuninori Morimoto 
549f1407d5cSKuninori Morimoto 	/* usb private data */
55058efc77cSKuninori Morimoto 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
551b1cb07cdSPeter Chen 	if (!priv)
552f1407d5cSKuninori Morimoto 		return -ENOMEM;
553f1407d5cSKuninori Morimoto 
554687f16b8SVarka Bhadram 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
555148e1134SThierry Reding 	priv->base = devm_ioremap_resource(&pdev->dev, res);
556148e1134SThierry Reding 	if (IS_ERR(priv->base))
557148e1134SThierry Reding 		return PTR_ERR(priv->base);
558f1407d5cSKuninori Morimoto 
559d7b3968dSSergei Shtylyov 	if (of_property_read_bool(pdev->dev.of_node, "extcon")) {
560d7b3968dSSergei Shtylyov 		priv->edev = extcon_get_edev_by_phandle(&pdev->dev, 0);
561d7b3968dSSergei Shtylyov 		if (IS_ERR(priv->edev))
562d7b3968dSSergei Shtylyov 			return PTR_ERR(priv->edev);
563d7b3968dSSergei Shtylyov 	}
564d7b3968dSSergei Shtylyov 
565f1407d5cSKuninori Morimoto 	/*
566f1407d5cSKuninori Morimoto 	 * care platform info
567f1407d5cSKuninori Morimoto 	 */
5688ecef00fSUlrich Hecht 
56948298206SKuninori Morimoto 	memcpy(&priv->dparam,
57048298206SKuninori Morimoto 	       &info->driver_param,
57148298206SKuninori Morimoto 	       sizeof(struct renesas_usbhs_driver_param));
572f1407d5cSKuninori Morimoto 
5738ecef00fSUlrich Hecht 	switch (priv->dparam.type) {
574e0213bc5SYoshihiro Shimoda 	case USBHS_TYPE_RCAR_GEN2:
5758ecef00fSUlrich Hecht 		priv->pfunc = usbhs_rcar2_ops;
57651f141a9SYoshihiro Shimoda 		if (!priv->dparam.pipe_configs) {
57751f141a9SYoshihiro Shimoda 			priv->dparam.pipe_configs = usbhsc_new_pipe;
57851f141a9SYoshihiro Shimoda 			priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
5798ecef00fSUlrich Hecht 		}
5808ecef00fSUlrich Hecht 		break;
581de18757eSYoshihiro Shimoda 	case USBHS_TYPE_RCAR_GEN3:
582de18757eSYoshihiro Shimoda 		priv->pfunc = usbhs_rcar3_ops;
583de18757eSYoshihiro Shimoda 		if (!priv->dparam.pipe_configs) {
584de18757eSYoshihiro Shimoda 			priv->dparam.pipe_configs = usbhsc_new_pipe;
585de18757eSYoshihiro Shimoda 			priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
586de18757eSYoshihiro Shimoda 		}
587de18757eSYoshihiro Shimoda 		break;
5888ecef00fSUlrich Hecht 	default:
5898ecef00fSUlrich Hecht 		if (!info->platform_callback.get_id) {
5908ecef00fSUlrich Hecht 			dev_err(&pdev->dev, "no platform callbacks");
5918ecef00fSUlrich Hecht 			return -EINVAL;
5928ecef00fSUlrich Hecht 		}
5938ecef00fSUlrich Hecht 		memcpy(&priv->pfunc,
5948ecef00fSUlrich Hecht 		       &info->platform_callback,
5958ecef00fSUlrich Hecht 		       sizeof(struct renesas_usbhs_platform_callback));
5968ecef00fSUlrich Hecht 		break;
5978ecef00fSUlrich Hecht 	}
5988ecef00fSUlrich Hecht 
599f1407d5cSKuninori Morimoto 	/* set driver callback functions for platform */
600f1407d5cSKuninori Morimoto 	dfunc			= &info->driver_callback;
601f1407d5cSKuninori Morimoto 	dfunc->notify_hotplug	= usbhsc_drvcllbck_notify_hotplug;
602f1407d5cSKuninori Morimoto 
603f1407d5cSKuninori Morimoto 	/* set default param if platform doesn't have */
60451f141a9SYoshihiro Shimoda 	if (!priv->dparam.pipe_configs) {
60551f141a9SYoshihiro Shimoda 		priv->dparam.pipe_configs = usbhsc_default_pipe;
60651f141a9SYoshihiro Shimoda 		priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe);
607f1407d5cSKuninori Morimoto 	}
60848298206SKuninori Morimoto 	if (!priv->dparam.pio_dma_border)
60948298206SKuninori Morimoto 		priv->dparam.pio_dma_border = 64; /* 64byte */
610f1407d5cSKuninori Morimoto 
611b002ff6eSKuninori Morimoto 	/* FIXME */
612b002ff6eSKuninori Morimoto 	/* runtime power control ? */
61348298206SKuninori Morimoto 	if (priv->pfunc.get_vbus)
614b002ff6eSKuninori Morimoto 		usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
615b002ff6eSKuninori Morimoto 
616f1407d5cSKuninori Morimoto 	/*
617f1407d5cSKuninori Morimoto 	 * priv settings
618f1407d5cSKuninori Morimoto 	 */
61953069af3SShimoda, Yoshihiro 	priv->irq	= irq_res->start;
62053069af3SShimoda, Yoshihiro 	if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
62153069af3SShimoda, Yoshihiro 		priv->irqflags = IRQF_SHARED;
622f1407d5cSKuninori Morimoto 	priv->pdev	= pdev;
623bc57381eSKuninori Morimoto 	INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
624f1407d5cSKuninori Morimoto 	spin_lock_init(usbhs_priv_to_lock(priv));
625f1407d5cSKuninori Morimoto 
626f1407d5cSKuninori Morimoto 	/* call pipe and module init */
627f1407d5cSKuninori Morimoto 	ret = usbhs_pipe_probe(priv);
628f1407d5cSKuninori Morimoto 	if (ret < 0)
62958efc77cSKuninori Morimoto 		return ret;
630f1407d5cSKuninori Morimoto 
631d3af90a5SKuninori Morimoto 	ret = usbhs_fifo_probe(priv);
632f1407d5cSKuninori Morimoto 	if (ret < 0)
63397f93227SKuninori Morimoto 		goto probe_end_pipe_exit;
634f1407d5cSKuninori Morimoto 
635d3af90a5SKuninori Morimoto 	ret = usbhs_mod_probe(priv);
636d3af90a5SKuninori Morimoto 	if (ret < 0)
637d3af90a5SKuninori Morimoto 		goto probe_end_fifo_exit;
638d3af90a5SKuninori Morimoto 
639f1407d5cSKuninori Morimoto 	/* dev_set_drvdata should be called after usbhs_mod_init */
640c9a0552eSLibo Chen 	platform_set_drvdata(pdev, priv);
641f1407d5cSKuninori Morimoto 
642f1407d5cSKuninori Morimoto 	/*
643f1407d5cSKuninori Morimoto 	 * deviece reset here because
644f1407d5cSKuninori Morimoto 	 * USB device might be used in boot loader.
645f1407d5cSKuninori Morimoto 	 */
646f1407d5cSKuninori Morimoto 	usbhs_sys_clock_ctrl(priv, 0);
647f1407d5cSKuninori Morimoto 
6488ecef00fSUlrich Hecht 	/* check GPIO determining if USB function should be enabled */
6498ecef00fSUlrich Hecht 	if (priv->dparam.enable_gpio) {
6508ecef00fSUlrich Hecht 		gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL);
6518ecef00fSUlrich Hecht 		ret = !gpio_get_value(priv->dparam.enable_gpio);
6528ecef00fSUlrich Hecht 		gpio_free(priv->dparam.enable_gpio);
6538ecef00fSUlrich Hecht 		if (ret) {
6548ecef00fSUlrich Hecht 			dev_warn(&pdev->dev,
6558ecef00fSUlrich Hecht 				 "USB function not selected (GPIO %d)\n",
6568ecef00fSUlrich Hecht 				 priv->dparam.enable_gpio);
6578ecef00fSUlrich Hecht 			ret = -ENOTSUPP;
6588ecef00fSUlrich Hecht 			goto probe_end_mod_exit;
6598ecef00fSUlrich Hecht 		}
6608ecef00fSUlrich Hecht 	}
6618ecef00fSUlrich Hecht 
662f1407d5cSKuninori Morimoto 	/*
663f1407d5cSKuninori Morimoto 	 * platform call
664f1407d5cSKuninori Morimoto 	 *
665f1407d5cSKuninori Morimoto 	 * USB phy setup might depend on CPU/Board.
666f1407d5cSKuninori Morimoto 	 * If platform has its callback functions,
667f1407d5cSKuninori Morimoto 	 * call it here.
668f1407d5cSKuninori Morimoto 	 */
669f1407d5cSKuninori Morimoto 	ret = usbhs_platform_call(priv, hardware_init, pdev);
670f1407d5cSKuninori Morimoto 	if (ret < 0) {
67148ac1e57SSergei Shtylyov 		dev_err(&pdev->dev, "platform init failed.\n");
67297f93227SKuninori Morimoto 		goto probe_end_mod_exit;
673f1407d5cSKuninori Morimoto 	}
674f1407d5cSKuninori Morimoto 
675f1407d5cSKuninori Morimoto 	/* reset phy for connection */
676f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
677f1407d5cSKuninori Morimoto 
678b002ff6eSKuninori Morimoto 	/* power control */
679b002ff6eSKuninori Morimoto 	pm_runtime_enable(&pdev->dev);
680b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
681b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
682b002ff6eSKuninori Morimoto 		usbhs_mod_autonomy_mode(priv);
683b002ff6eSKuninori Morimoto 	}
684b002ff6eSKuninori Morimoto 
685f1407d5cSKuninori Morimoto 	/*
686f1407d5cSKuninori Morimoto 	 * manual call notify_hotplug for cold plug
687f1407d5cSKuninori Morimoto 	 */
688368504c0SSergei Shtylyov 	usbhsc_drvcllbck_notify_hotplug(pdev);
689f1407d5cSKuninori Morimoto 
690f1407d5cSKuninori Morimoto 	dev_info(&pdev->dev, "probed\n");
691f1407d5cSKuninori Morimoto 
692f1407d5cSKuninori Morimoto 	return ret;
693f1407d5cSKuninori Morimoto 
694f1407d5cSKuninori Morimoto probe_end_mod_exit:
695f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
696d3af90a5SKuninori Morimoto probe_end_fifo_exit:
697d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
69897f93227SKuninori Morimoto probe_end_pipe_exit:
69997f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
700f1407d5cSKuninori Morimoto 
7010d799503SYoshihiro Shimoda 	dev_info(&pdev->dev, "probe failed (%d)\n", ret);
702f1407d5cSKuninori Morimoto 
703f1407d5cSKuninori Morimoto 	return ret;
704f1407d5cSKuninori Morimoto }
705f1407d5cSKuninori Morimoto 
706fb4e98abSBill Pemberton static int usbhs_remove(struct platform_device *pdev)
707f1407d5cSKuninori Morimoto {
708206dcc2cSKuninori Morimoto 	struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
709ace0a5f9SJingoo Han 	struct renesas_usbhs_platform_info *info = dev_get_platdata(&pdev->dev);
710af32fe51SKuninori Morimoto 	struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
711f1407d5cSKuninori Morimoto 
712f1407d5cSKuninori Morimoto 	dev_dbg(&pdev->dev, "usb remove\n");
713f1407d5cSKuninori Morimoto 
714af32fe51SKuninori Morimoto 	dfunc->notify_hotplug = NULL;
715af32fe51SKuninori Morimoto 
716b002ff6eSKuninori Morimoto 	/* power off */
717b002ff6eSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
718b002ff6eSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
719f1407d5cSKuninori Morimoto 
720b002ff6eSKuninori Morimoto 	pm_runtime_disable(&pdev->dev);
721f1407d5cSKuninori Morimoto 
722f1407d5cSKuninori Morimoto 	usbhs_platform_call(priv, hardware_exit, pdev);
723f1407d5cSKuninori Morimoto 	usbhs_mod_remove(priv);
724d3af90a5SKuninori Morimoto 	usbhs_fifo_remove(priv);
72597f93227SKuninori Morimoto 	usbhs_pipe_remove(priv);
726f1407d5cSKuninori Morimoto 
727f1407d5cSKuninori Morimoto 	return 0;
728f1407d5cSKuninori Morimoto }
729f1407d5cSKuninori Morimoto 
730ca8a282aSKuninori Morimoto static int usbhsc_suspend(struct device *dev)
731ca8a282aSKuninori Morimoto {
732ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
733ca8a282aSKuninori Morimoto 	struct usbhs_mod *mod = usbhs_mod_get_current(priv);
734ca8a282aSKuninori Morimoto 
735ca8a282aSKuninori Morimoto 	if (mod) {
736ca8a282aSKuninori Morimoto 		usbhs_mod_call(priv, stop, priv);
737ca8a282aSKuninori Morimoto 		usbhs_mod_change(priv, -1);
738ca8a282aSKuninori Morimoto 	}
739ca8a282aSKuninori Morimoto 
740ca8a282aSKuninori Morimoto 	if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
741ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 0);
742ca8a282aSKuninori Morimoto 
743ca8a282aSKuninori Morimoto 	return 0;
744ca8a282aSKuninori Morimoto }
745ca8a282aSKuninori Morimoto 
746ca8a282aSKuninori Morimoto static int usbhsc_resume(struct device *dev)
747ca8a282aSKuninori Morimoto {
748ca8a282aSKuninori Morimoto 	struct usbhs_priv *priv = dev_get_drvdata(dev);
749ca8a282aSKuninori Morimoto 	struct platform_device *pdev = usbhs_priv_to_pdev(priv);
750ca8a282aSKuninori Morimoto 
751ca8a282aSKuninori Morimoto 	if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
752ca8a282aSKuninori Morimoto 		usbhsc_power_ctrl(priv, 1);
753ca8a282aSKuninori Morimoto 
7545b50d3b5SKuninori Morimoto 	usbhs_platform_call(priv, phy_reset, pdev);
7555b50d3b5SKuninori Morimoto 
7565b50d3b5SKuninori Morimoto 	usbhsc_drvcllbck_notify_hotplug(pdev);
757ca8a282aSKuninori Morimoto 
758ca8a282aSKuninori Morimoto 	return 0;
759ca8a282aSKuninori Morimoto }
760ca8a282aSKuninori Morimoto 
761ca8a282aSKuninori Morimoto static int usbhsc_runtime_nop(struct device *dev)
762ca8a282aSKuninori Morimoto {
763ca8a282aSKuninori Morimoto 	/* Runtime PM callback shared between ->runtime_suspend()
764ca8a282aSKuninori Morimoto 	 * and ->runtime_resume(). Simply returns success.
765ca8a282aSKuninori Morimoto 	 *
766ca8a282aSKuninori Morimoto 	 * This driver re-initializes all registers after
767ca8a282aSKuninori Morimoto 	 * pm_runtime_get_sync() anyway so there is no need
768ca8a282aSKuninori Morimoto 	 * to save and restore registers here.
769ca8a282aSKuninori Morimoto 	 */
770ca8a282aSKuninori Morimoto 	return 0;
771ca8a282aSKuninori Morimoto }
772ca8a282aSKuninori Morimoto 
773ca8a282aSKuninori Morimoto static const struct dev_pm_ops usbhsc_pm_ops = {
774ca8a282aSKuninori Morimoto 	.suspend		= usbhsc_suspend,
775ca8a282aSKuninori Morimoto 	.resume			= usbhsc_resume,
776ca8a282aSKuninori Morimoto 	.runtime_suspend	= usbhsc_runtime_nop,
777ca8a282aSKuninori Morimoto 	.runtime_resume		= usbhsc_runtime_nop,
778ca8a282aSKuninori Morimoto };
779ca8a282aSKuninori Morimoto 
780f1407d5cSKuninori Morimoto static struct platform_driver renesas_usbhs_driver = {
781f1407d5cSKuninori Morimoto 	.driver		= {
782f1407d5cSKuninori Morimoto 		.name	= "renesas_usbhs",
783ca8a282aSKuninori Morimoto 		.pm	= &usbhsc_pm_ops,
784b854100eSYoshihiro Shimoda 		.of_match_table = of_match_ptr(usbhs_of_match),
785f1407d5cSKuninori Morimoto 	},
786f1407d5cSKuninori Morimoto 	.probe		= usbhs_probe,
7877690417dSBill Pemberton 	.remove		= usbhs_remove,
788f1407d5cSKuninori Morimoto };
789f1407d5cSKuninori Morimoto 
790cc27c96cSAxel Lin module_platform_driver(renesas_usbhs_driver);
791f1407d5cSKuninori Morimoto 
792f1407d5cSKuninori Morimoto MODULE_LICENSE("GPL");
793f1407d5cSKuninori Morimoto MODULE_DESCRIPTION("Renesas USB driver");
794f1407d5cSKuninori Morimoto MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
795