blob: a37696d83faf2626dc217f54395c3bbe276af242 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +09002/*
3 * R8A66597 HCD (Host Controller Driver) for u-boot
4 *
5 * Copyright (C) 2008 Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com>
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +09006 */
7
8#include <common.h>
Simon Glassa73bda42015-11-08 23:47:45 -07009#include <console.h>
Marek Vasut5e890c52019-08-11 12:34:38 +020010#include <dm.h>
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090011#include <usb.h>
12#include <asm/io.h>
Chris Brandtc85f3cc2017-11-29 14:49:21 -050013#include <linux/iopoll.h>
Marek Vasutc1214092019-08-22 17:53:04 +020014#include <power/regulator.h>
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090015
16#include "r8a66597.h"
17
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090018#ifdef R8A66597_DEBUG
19#define R8A66597_DPRINT printf
20#else
21#define R8A66597_DPRINT(...)
22#endif
23
Marek Vasut5e890c52019-08-11 12:34:38 +020024static inline struct usb_device *usb_dev_get_parent(struct usb_device *udev)
25{
26 struct udevice *parent = udev->dev->parent;
27
28 /*
29 * When called from usb-uclass.c: usb_scan_device() udev->dev points
30 * to the parent udevice, not the actual udevice belonging to the
31 * udev as the device is not instantiated yet.
32 *
33 * If dev is an usb-bus, then we are called from usb_scan_device() for
34 * an usb-device plugged directly into the root port, return NULL.
35 */
36 if (device_get_uclass_id(udev->dev) == UCLASS_USB)
37 return NULL;
38
39 /*
40 * If these 2 are not the same we are being called from
41 * usb_scan_device() and udev itself is the parent.
42 */
43 if (dev_get_parent_priv(udev->dev) != udev)
44 return udev;
45
46 /* We are being called normally, use the parent pointer */
47 if (device_get_uclass_id(parent) == UCLASS_USB_HUB)
48 return dev_get_parent_priv(parent);
49
50 return NULL;
51}
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090052
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090053static void get_hub_data(struct usb_device *dev, u16 *hub_devnum, u16 *hubport)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090054{
Marek Vasut5e890c52019-08-11 12:34:38 +020055 struct usb_device *parent = usb_dev_get_parent(dev);
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090056
57 *hub_devnum = 0;
58 *hubport = 0;
59
60 /* check a device connected to root_hub */
Marek Vasut5e890c52019-08-11 12:34:38 +020061 if ((parent && parent->devnum == 1) ||
Marek Vasutb3935f12019-08-11 13:44:10 +020062 dev->devnum == 1)
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090063 return;
64
Marek Vasut5e890c52019-08-11 12:34:38 +020065 *hub_devnum = (u8)parent->devnum;
66 *hubport = parent->portnr - 1;
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090067}
68
69static void set_devadd(struct r8a66597 *r8a66597, u8 r8a66597_address,
Marek Vasutb3935f12019-08-11 13:44:10 +020070 struct usb_device *dev, int port)
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090071{
72 u16 val, usbspd, upphub, hubport;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090073 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
74
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090075 get_hub_data(dev, &upphub, &hubport);
76 usbspd = r8a66597->speed;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090077 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
78 r8a66597_write(r8a66597, val, devadd_reg);
79}
80
81static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
82{
83 u16 tmp;
84 int i = 0;
85
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090086 do {
87 r8a66597_write(r8a66597, USBE, SYSCFG0);
88 tmp = r8a66597_read(r8a66597, SYSCFG0);
89 if (i++ > 1000) {
90 printf("register access fail.\n");
91 return -1;
92 }
93 } while ((tmp & USBE) != USBE);
94 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
Chris Brandtfaecead2017-11-27 14:04:10 -050095 /*
96 * RZ/A Only:
97 * Bits XTAL(UCKSEL) and UPLLE in SYSCFG0 for USB0 controls both USB0
98 * and USB1, so we must always set the USB0 register
99 */
100#if (CONFIG_R8A66597_XTAL == 1)
Marek Vasut2c11a3c2019-08-11 12:34:38 +0200101 r8a66597_bset(r8a66597, XTAL, SYSCFG0);
Chris Brandtfaecead2017-11-27 14:04:10 -0500102#endif
103 mdelay(1);
Marek Vasut2c11a3c2019-08-11 12:34:38 +0200104 r8a66597_bset(r8a66597, UPLLE, SYSCFG0);
Chris Brandtfaecead2017-11-27 14:04:10 -0500105 mdelay(1);
106 r8a66597_bset(r8a66597, SUSPM, SUSPMODE0);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900107
108 return 0;
109}
110
111static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
112{
Chris Brandtfaecead2017-11-27 14:04:10 -0500113 r8a66597_bclr(r8a66597, SUSPM, SUSPMODE0);
114
Marek Vasut2c11a3c2019-08-11 12:34:38 +0200115 r8a66597_bclr(r8a66597, UPLLE, SYSCFG0);
Chris Brandtfaecead2017-11-27 14:04:10 -0500116 mdelay(1);
117 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
118 mdelay(1);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900119}
120
121static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
122{
123 u16 val;
124
125 val = port ? DRPD : DCFM | DRPD;
126 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
127 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900128}
129
130static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
131{
132 u16 val, tmp;
133
134 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
135 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
136
137 r8a66597_port_power(r8a66597, port, 0);
138
139 do {
140 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
141 udelay(640);
142 } while (tmp == EDGESTS);
143
144 val = port ? DRPD : DCFM | DRPD;
145 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
146 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
147}
148
149static int enable_controller(struct r8a66597 *r8a66597)
150{
151 int ret, port;
152
153 ret = r8a66597_clock_enable(r8a66597);
154 if (ret < 0)
155 return ret;
156
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900157 r8a66597_bset(r8a66597, USBE, SYSCFG0);
158
159 r8a66597_bset(r8a66597, INTL, SOFCFG);
160 r8a66597_write(r8a66597, 0, INTENB0);
yasuhisa umanoea33c6a2014-04-18 11:33:08 +0900161 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
162 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900163
Marek Vasutcf8a6272019-08-11 13:23:11 +0200164 r8a66597_bclr(r8a66597, BIGEND, CFIFOSEL);
165 r8a66597_bclr(r8a66597, BIGEND, D0FIFOSEL);
166 r8a66597_bclr(r8a66597, BIGEND, D1FIFOSEL);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900167 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
168
169 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
170 r8a66597_enable_port(r8a66597, port);
171
172 return 0;
173}
174
175static void disable_controller(struct r8a66597 *r8a66597)
176{
177 int i;
178
179 if (!(r8a66597_read(r8a66597, SYSCFG0) & USBE))
180 return;
181
182 r8a66597_write(r8a66597, 0, INTENB0);
183 r8a66597_write(r8a66597, 0, INTSTS0);
184
185 r8a66597_write(r8a66597, 0, D0FIFOSEL);
186 r8a66597_write(r8a66597, 0, D1FIFOSEL);
187 r8a66597_write(r8a66597, 0, DCPCFG);
188 r8a66597_write(r8a66597, 0x40, DCPMAXP);
189 r8a66597_write(r8a66597, 0, DCPCTR);
190
191 for (i = 0; i <= 10; i++)
192 r8a66597_write(r8a66597, 0, get_devadd_addr(i));
193 for (i = 1; i <= 5; i++) {
194 r8a66597_write(r8a66597, 0, get_pipetre_addr(i));
195 r8a66597_write(r8a66597, 0, get_pipetrn_addr(i));
196 }
197 for (i = 1; i < R8A66597_MAX_NUM_PIPE; i++) {
198 r8a66597_write(r8a66597, 0, get_pipectr_addr(i));
199 r8a66597_write(r8a66597, i, PIPESEL);
200 r8a66597_write(r8a66597, 0, PIPECFG);
201 r8a66597_write(r8a66597, 0, PIPEBUF);
202 r8a66597_write(r8a66597, 0, PIPEMAXP);
203 r8a66597_write(r8a66597, 0, PIPEPERI);
204 }
205
206 for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++)
207 r8a66597_disable_port(r8a66597, i);
208
209 r8a66597_clock_disable(r8a66597);
210}
211
212static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
213 u16 mask, u16 loop)
214{
215 u16 tmp;
216 int i = 0;
217
218 do {
219 tmp = r8a66597_read(r8a66597, reg);
220 if (i++ > 1000000) {
221 printf("register%lx, loop %x is timeout\n", reg, loop);
222 break;
223 }
224 } while ((tmp & mask) != loop);
225}
226
227static void pipe_buffer_setting(struct r8a66597 *r8a66597,
228 struct usb_device *dev, unsigned long pipe)
229{
230 u16 val = 0;
231 u16 pipenum, bufnum, maxpacket;
232
233 if (usb_pipein(pipe)) {
234 pipenum = BULK_IN_PIPENUM;
235 bufnum = BULK_IN_BUFNUM;
236 maxpacket = dev->epmaxpacketin[usb_pipeendpoint(pipe)];
237 } else {
238 pipenum = BULK_OUT_PIPENUM;
239 bufnum = BULK_OUT_BUFNUM;
240 maxpacket = dev->epmaxpacketout[usb_pipeendpoint(pipe)];
241 }
242
243 if (r8a66597->pipe_config & (1 << pipenum))
244 return;
245 r8a66597->pipe_config |= (1 << pipenum);
246
247 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(pipenum));
248 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(pipenum));
249 r8a66597_write(r8a66597, pipenum, PIPESEL);
250
251 /* FIXME: This driver support bulk transfer only. */
252 if (!usb_pipein(pipe))
253 val |= R8A66597_DIR;
254 else
255 val |= R8A66597_SHTNAK;
256 val |= R8A66597_BULK | R8A66597_DBLB | usb_pipeendpoint(pipe);
257 r8a66597_write(r8a66597, val, PIPECFG);
258
259 r8a66597_write(r8a66597, (8 << 10) | bufnum, PIPEBUF);
260 r8a66597_write(r8a66597, make_devsel(usb_pipedevice(pipe)) |
261 maxpacket, PIPEMAXP);
262 r8a66597_write(r8a66597, 0, PIPEPERI);
263 r8a66597_write(r8a66597, SQCLR, get_pipectr_addr(pipenum));
264}
265
266static int send_setup_packet(struct r8a66597 *r8a66597, struct usb_device *dev,
267 struct devrequest *setup)
268{
269 int i;
270 unsigned short *p = (unsigned short *)setup;
271 unsigned long setup_addr = USBREQ;
272 u16 intsts1;
273 int timeout = 3000;
Chris Brandtfaecead2017-11-27 14:04:10 -0500274 u16 dcpctr;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900275 u16 devsel = setup->request == USB_REQ_SET_ADDRESS ? 0 : dev->devnum;
276
277 r8a66597_write(r8a66597, make_devsel(devsel) |
278 (8 << dev->maxpacketsize), DCPMAXP);
279 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
280
Chris Brandtfaecead2017-11-27 14:04:10 -0500281 dcpctr = r8a66597_read(r8a66597, DCPCTR);
282 if ((dcpctr & PID) == PID_BUF) {
Chris Brandtc85f3cc2017-11-29 14:49:21 -0500283 if (readw_poll_timeout(r8a66597->reg + DCPCTR, dcpctr,
284 dcpctr & BSTS, 1000) < 0) {
285 printf("DCPCTR BSTS timeout!\n");
286 return -ETIMEDOUT;
Chris Brandtfaecead2017-11-27 14:04:10 -0500287 }
288 }
Chris Brandtfaecead2017-11-27 14:04:10 -0500289
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900290 for (i = 0; i < 4; i++) {
291 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
292 setup_addr += 2;
293 }
294 r8a66597_write(r8a66597, ~0x0001, BRDYSTS);
295 r8a66597_write(r8a66597, SUREQ, DCPCTR);
296
297 while (1) {
298 intsts1 = r8a66597_read(r8a66597, INTSTS1);
299 if (intsts1 & SACK)
300 break;
301 if (intsts1 & SIGN) {
302 printf("setup packet send error\n");
303 return -1;
304 }
305 if (timeout-- < 0) {
306 printf("setup packet timeout\n");
307 return -1;
308 }
309 udelay(500);
310 }
311
312 return 0;
313}
314
315static int send_bulk_packet(struct r8a66597 *r8a66597, struct usb_device *dev,
316 unsigned long pipe, void *buffer, int transfer_len)
317{
318 u16 tmp, bufsize;
319 u16 *buf;
320 size_t size;
321
322 R8A66597_DPRINT("%s\n", __func__);
323
324 r8a66597_mdfy(r8a66597, MBW | BULK_OUT_PIPENUM,
Marek Vasutb3935f12019-08-11 13:44:10 +0200325 MBW | CURPIPE, CFIFOSEL);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900326 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, BULK_OUT_PIPENUM);
327 tmp = r8a66597_read(r8a66597, CFIFOCTR);
328 if ((tmp & FRDY) == 0) {
329 printf("%s FRDY is not set (%x)\n", __func__, tmp);
330 return -1;
331 }
332
333 /* prepare parameters */
334 bufsize = dev->epmaxpacketout[usb_pipeendpoint(pipe)];
335 buf = (u16 *)(buffer + dev->act_len);
336 size = min((int)bufsize, transfer_len - dev->act_len);
337
338 /* write fifo */
339 r8a66597_write(r8a66597, ~(1 << BULK_OUT_PIPENUM), BEMPSTS);
340 if (buffer) {
341 r8a66597_write_fifo(r8a66597, CFIFO, buf, size);
342 r8a66597_write(r8a66597, BVAL, CFIFOCTR);
343 }
344
345 /* update parameters */
346 dev->act_len += size;
347
348 r8a66597_mdfy(r8a66597, PID_BUF, PID,
Marek Vasutb3935f12019-08-11 13:44:10 +0200349 get_pipectr_addr(BULK_OUT_PIPENUM));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900350
351 while (!(r8a66597_read(r8a66597, BEMPSTS) & (1 << BULK_OUT_PIPENUM)))
352 if (ctrlc())
353 return -1;
354 r8a66597_write(r8a66597, ~(1 << BULK_OUT_PIPENUM), BEMPSTS);
355
356 if (dev->act_len >= transfer_len)
357 r8a66597_mdfy(r8a66597, PID_NAK, PID,
Marek Vasutb3935f12019-08-11 13:44:10 +0200358 get_pipectr_addr(BULK_OUT_PIPENUM));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900359
360 return 0;
361}
362
363static int receive_bulk_packet(struct r8a66597 *r8a66597,
364 struct usb_device *dev,
365 unsigned long pipe,
366 void *buffer, int transfer_len)
367{
368 u16 tmp;
369 u16 *buf;
370 const u16 pipenum = BULK_IN_PIPENUM;
371 int rcv_len;
372 int maxpacket = dev->epmaxpacketin[usb_pipeendpoint(pipe)];
373
374 R8A66597_DPRINT("%s\n", __func__);
375
376 /* prepare */
377 if (dev->act_len == 0) {
378 r8a66597_mdfy(r8a66597, PID_NAK, PID,
Marek Vasutb3935f12019-08-11 13:44:10 +0200379 get_pipectr_addr(pipenum));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900380 r8a66597_write(r8a66597, ~(1 << pipenum), BRDYSTS);
381
382 r8a66597_write(r8a66597, TRCLR, get_pipetre_addr(pipenum));
383 r8a66597_write(r8a66597,
Marek Vasutb3935f12019-08-11 13:44:10 +0200384 (transfer_len + maxpacket - 1) / maxpacket,
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900385 get_pipetrn_addr(pipenum));
386 r8a66597_bset(r8a66597, TRENB, get_pipetre_addr(pipenum));
387
388 r8a66597_mdfy(r8a66597, PID_BUF, PID,
Marek Vasutb3935f12019-08-11 13:44:10 +0200389 get_pipectr_addr(pipenum));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900390 }
391
392 r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
393 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
394
395 while (!(r8a66597_read(r8a66597, BRDYSTS) & (1 << pipenum)))
396 if (ctrlc())
397 return -1;
398 r8a66597_write(r8a66597, ~(1 << pipenum), BRDYSTS);
399
400 tmp = r8a66597_read(r8a66597, CFIFOCTR);
401 if ((tmp & FRDY) == 0) {
402 printf("%s FRDY is not set. (%x)\n", __func__, tmp);
403 return -1;
404 }
405
406 buf = (u16 *)(buffer + dev->act_len);
407 rcv_len = tmp & DTLN;
408 dev->act_len += rcv_len;
409
410 if (buffer) {
411 if (rcv_len == 0)
412 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
413 else
414 r8a66597_read_fifo(r8a66597, CFIFO, buf, rcv_len);
415 }
416
417 return 0;
418}
419
420static int receive_control_packet(struct r8a66597 *r8a66597,
421 struct usb_device *dev,
422 void *buffer, int transfer_len)
423{
424 u16 tmp;
425 int rcv_len;
426
427 /* FIXME: limit transfer size : 64byte or less */
428
429 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
430 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
431 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
432 r8a66597_bset(r8a66597, SQSET, DCPCTR);
433 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
434 r8a66597_mdfy(r8a66597, PID_BUF, PID, DCPCTR);
435
436 while (!(r8a66597_read(r8a66597, BRDYSTS) & 0x0001))
437 if (ctrlc())
438 return -1;
439 r8a66597_write(r8a66597, ~0x0001, BRDYSTS);
440
441 r8a66597_mdfy(r8a66597, MBW, MBW | CURPIPE, CFIFOSEL);
442 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
443
444 tmp = r8a66597_read(r8a66597, CFIFOCTR);
445 if ((tmp & FRDY) == 0) {
446 printf("%s FRDY is not set. (%x)\n", __func__, tmp);
447 return -1;
448 }
449
450 rcv_len = tmp & DTLN;
451 dev->act_len += rcv_len;
452
453 r8a66597_mdfy(r8a66597, PID_NAK, PID, DCPCTR);
454
455 if (buffer) {
456 if (rcv_len == 0)
457 r8a66597_write(r8a66597, BCLR, DCPCTR);
458 else
459 r8a66597_read_fifo(r8a66597, CFIFO, buffer, rcv_len);
460 }
461
462 return 0;
463}
464
465static int send_status_packet(struct r8a66597 *r8a66597,
Marek Vasutb3935f12019-08-11 13:44:10 +0200466 unsigned long pipe)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900467{
468 r8a66597_bset(r8a66597, SQSET, DCPCTR);
469 r8a66597_mdfy(r8a66597, PID_NAK, PID, DCPCTR);
470
471 if (usb_pipein(pipe)) {
472 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
473 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
474 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
475 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
476 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
477 } else {
478 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
479 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
480 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
481 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
482 }
483 r8a66597_mdfy(r8a66597, PID_BUF, PID, DCPCTR);
484
485 while (!(r8a66597_read(r8a66597, BEMPSTS) & 0x0001))
486 if (ctrlc())
487 return -1;
488
489 return 0;
490}
491
492static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port)
493{
494 int count = R8A66597_MAX_SAMPLING;
495 unsigned short syssts, old_syssts;
496
497 R8A66597_DPRINT("%s\n", __func__);
498
499 old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port) & LNST);
500 while (count > 0) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000501 mdelay(R8A66597_RH_POLL_TIME);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900502
503 syssts = r8a66597_read(r8a66597, get_syssts_reg(port) & LNST);
504 if (syssts == old_syssts) {
505 count--;
506 } else {
507 count = R8A66597_MAX_SAMPLING;
508 old_syssts = syssts;
509 }
510 }
511}
512
513static void r8a66597_bus_reset(struct r8a66597 *r8a66597, int port)
514{
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000515 mdelay(10);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900516 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT, get_dvstctr_reg(port));
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000517 mdelay(50);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900518 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT, get_dvstctr_reg(port));
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000519 mdelay(50);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900520}
521
522static int check_usb_device_connecting(struct r8a66597 *r8a66597)
523{
524 int timeout = 10000; /* 100usec * 10000 = 1sec */
525 int i;
526
527 for (i = 0; i < 5; i++) {
528 /* check a usb cable connect */
529 while (!(r8a66597_read(r8a66597, INTSTS1) & ATTCH)) {
530 if (timeout-- < 0) {
531 printf("%s timeout.\n", __func__);
532 return -1;
533 }
534 udelay(100);
535 }
536
537 /* check a data line */
538 r8a66597_check_syssts(r8a66597, 0);
539
540 r8a66597_bus_reset(r8a66597, 0);
541 r8a66597->speed = get_rh_usb_speed(r8a66597, 0);
542
543 if (!(r8a66597_read(r8a66597, INTSTS1) & DTCH)) {
544 r8a66597->port_change = USB_PORT_STAT_C_CONNECTION;
545 r8a66597->port_status = USB_PORT_STAT_CONNECTION |
546 USB_PORT_STAT_ENABLE;
547 return 0; /* success */
548 }
549
550 R8A66597_DPRINT("USB device has detached. retry = %d\n", i);
551 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
552 }
553
554 return -1; /* fail */
555}
556
Marek Vasutb3935f12019-08-11 13:44:10 +0200557/* Virtual Root Hub */
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900558
Stephen Warren39c89682014-02-13 21:15:18 -0700559#include <usbroothubdes.h>
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900560
Marek Vasut5e890c52019-08-11 12:34:38 +0200561static int r8a66597_submit_rh_msg(struct udevice *udev, struct usb_device *dev,
562 unsigned long pipe, void *buffer,
563 int transfer_len, struct devrequest *cmd)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900564{
Marek Vasut5e890c52019-08-11 12:34:38 +0200565 struct r8a66597 *r8a66597 = dev_get_priv(udev);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900566 int leni = transfer_len;
567 int len = 0;
568 int stat = 0;
569 __u16 bmRType_bReq;
570 __u16 wValue;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900571 __u16 wLength;
572 unsigned char data[32];
573
574 R8A66597_DPRINT("%s\n", __func__);
575
Remy Bohmerd8c55ab2008-10-10 10:23:22 +0200576 if (usb_pipeint(pipe)) {
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900577 printf("Root-Hub submit IRQ: NOT implemented");
578 return 0;
579 }
580
581 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
582 wValue = cpu_to_le16 (cmd->value);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900583 wLength = cpu_to_le16 (cmd->length);
584
585 switch (bmRType_bReq) {
586 case RH_GET_STATUS:
587 *(__u16 *)buffer = cpu_to_le16(1);
588 len = 2;
589 break;
590 case RH_GET_STATUS | RH_INTERFACE:
591 *(__u16 *)buffer = cpu_to_le16(0);
592 len = 2;
593 break;
594 case RH_GET_STATUS | RH_ENDPOINT:
595 *(__u16 *)buffer = cpu_to_le16(0);
596 len = 2;
597 break;
598 case RH_GET_STATUS | RH_CLASS:
599 *(__u32 *)buffer = cpu_to_le32(0);
600 len = 4;
601 break;
602 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
603 *(__u32 *)buffer = cpu_to_le32(r8a66597->port_status |
604 (r8a66597->port_change << 16));
605 len = 4;
606 break;
607 case RH_CLEAR_FEATURE | RH_ENDPOINT:
608 case RH_CLEAR_FEATURE | RH_CLASS:
609 break;
610
611 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
612 switch (wValue) {
613 case RH_C_PORT_CONNECTION:
614 r8a66597->port_change &= ~USB_PORT_STAT_C_CONNECTION;
615 break;
616 }
617 break;
618
619 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
620 switch (wValue) {
621 case (RH_PORT_SUSPEND):
622 break;
623 case (RH_PORT_RESET):
624 r8a66597_bus_reset(r8a66597, 0);
625 break;
626 case (RH_PORT_POWER):
627 break;
628 case (RH_PORT_ENABLE):
629 break;
630 }
631 break;
632 case RH_SET_ADDRESS:
Marek Vasut5e890c52019-08-11 12:34:38 +0200633 r8a66597->rh_devnum = wValue;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900634 break;
635 case RH_GET_DESCRIPTOR:
636 switch ((wValue & 0xff00) >> 8) {
637 case (0x01): /* device descriptor */
638 len = min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200639 leni,
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900640 min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200641 sizeof(root_hub_dev_des),
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900642 wLength));
643 memcpy(buffer, root_hub_dev_des, len);
644 break;
645 case (0x02): /* configuration descriptor */
646 len = min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200647 leni,
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900648 min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200649 sizeof(root_hub_config_des),
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900650 wLength));
651 memcpy(buffer, root_hub_config_des, len);
652 break;
653 case (0x03): /* string descriptors */
654 if (wValue == 0x0300) {
655 len = min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200656 leni,
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900657 min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200658 sizeof(root_hub_str_index0),
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900659 wLength));
660 memcpy(buffer, root_hub_str_index0, len);
661 }
662 if (wValue == 0x0301) {
663 len = min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200664 leni,
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900665 min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200666 sizeof(root_hub_str_index1),
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900667 wLength));
668 memcpy(buffer, root_hub_str_index1, len);
669 }
670 break;
671 default:
672 stat = USB_ST_STALLED;
673 }
674 break;
675
676 case RH_GET_DESCRIPTOR | RH_CLASS:
677 {
678 __u32 temp = 0x00000001;
679
680 data[0] = 9; /* min length; */
681 data[1] = 0x29;
682 data[2] = temp & RH_A_NDP;
683 data[3] = 0;
684 if (temp & RH_A_PSM)
685 data[3] |= 0x1;
686 if (temp & RH_A_NOCP)
687 data[3] |= 0x10;
688 else if (temp & RH_A_OCPM)
689 data[3] |= 0x8;
690
691 /* corresponds to data[4-7] */
692 data[5] = (temp & RH_A_POTPGT) >> 24;
693 data[7] = temp & RH_B_DR;
694 if (data[2] < 7) {
695 data[8] = 0xff;
696 } else {
697 data[0] += 2;
698 data[8] = (temp & RH_B_DR) >> 8;
Marek Vasutb3935f12019-08-11 13:44:10 +0200699 data[9] = 0xff;
700 data[10] = 0xff;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900701 }
702
703 len = min_t(unsigned int, leni,
704 min_t(unsigned int, data[0], wLength));
705 memcpy(buffer, data, len);
706 break;
707 }
708
709 case RH_GET_CONFIGURATION:
Marek Vasutb3935f12019-08-11 13:44:10 +0200710 *(__u8 *)buffer = 0x01;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900711 len = 1;
712 break;
713 case RH_SET_CONFIGURATION:
714 break;
715 default:
Nobuhiro Iwamatsu9d58a012008-09-18 20:13:08 +0900716 R8A66597_DPRINT("unsupported root hub command");
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900717 stat = USB_ST_STALLED;
718 }
719
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000720 mdelay(1);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900721
722 len = min_t(int, len, leni);
723
724 dev->act_len = len;
725 dev->status = stat;
726
727 return stat;
728}
729
Marek Vasutb3935f12019-08-11 13:44:10 +0200730static int r8a66597_submit_control_msg(struct udevice *udev,
731 struct usb_device *dev,
732 unsigned long pipe, void *buffer,
733 int length, struct devrequest *setup)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900734{
Marek Vasut5e890c52019-08-11 12:34:38 +0200735 struct r8a66597 *r8a66597 = dev_get_priv(udev);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900736 u16 r8a66597_address = setup->request == USB_REQ_SET_ADDRESS ?
737 0 : dev->devnum;
738
Marek Vasut5e890c52019-08-11 12:34:38 +0200739 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
740 udev->name, dev, dev->dev->name, dev->portnr);
741
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900742 R8A66597_DPRINT("%s\n", __func__);
743 if (usb_pipedevice(pipe) == r8a66597->rh_devnum)
Marek Vasut5e890c52019-08-11 12:34:38 +0200744 return r8a66597_submit_rh_msg(udev, dev, pipe, buffer,
745 length, setup);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900746
747 R8A66597_DPRINT("%s: setup\n", __func__);
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +0900748 set_devadd(r8a66597, r8a66597_address, dev, 0);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900749
750 if (send_setup_packet(r8a66597, dev, setup) < 0) {
751 printf("setup packet send error\n");
752 return -1;
753 }
754
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +0900755 dev->act_len = 0;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900756 if (usb_pipein(pipe))
757 if (receive_control_packet(r8a66597, dev, buffer,
Marek Vasutb3935f12019-08-11 13:44:10 +0200758 length) < 0)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900759 return -1;
760
761 if (send_status_packet(r8a66597, pipe) < 0)
762 return -1;
763
764 dev->status = 0;
765
766 return 0;
767}
768
Marek Vasutb3935f12019-08-11 13:44:10 +0200769static int r8a66597_submit_bulk_msg(struct udevice *udev,
770 struct usb_device *dev, unsigned long pipe,
771 void *buffer, int length)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900772{
Marek Vasut5e890c52019-08-11 12:34:38 +0200773 struct r8a66597 *r8a66597 = dev_get_priv(udev);
774 int ret = 0;
775
776 debug("%s: dev='%s', udev=%p\n", __func__, udev->name, dev);
777
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900778 R8A66597_DPRINT("%s\n", __func__);
Marek Vasut5e890c52019-08-11 12:34:38 +0200779 R8A66597_DPRINT("pipe = %08x, buffer = %p, len = %d, devnum = %d\n",
780 pipe, buffer, length, dev->devnum);
781
782 set_devadd(r8a66597, dev->devnum, dev, 0);
783
784 pipe_buffer_setting(r8a66597, dev, pipe);
785
786 dev->act_len = 0;
787 while (dev->act_len < length && ret == 0) {
788 if (ctrlc())
789 return -1;
790
791 if (usb_pipein(pipe))
792 ret = receive_bulk_packet(r8a66597, dev, pipe, buffer,
Marek Vasutb3935f12019-08-11 13:44:10 +0200793 length);
Marek Vasut5e890c52019-08-11 12:34:38 +0200794 else
795 ret = send_bulk_packet(r8a66597, dev, pipe, buffer,
Marek Vasutb3935f12019-08-11 13:44:10 +0200796 length);
Marek Vasut5e890c52019-08-11 12:34:38 +0200797 }
798
799 if (ret == 0)
800 dev->status = 0;
801
802 return ret;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900803}
804
Marek Vasut5e890c52019-08-11 12:34:38 +0200805static int r8a66597_usb_ofdata_to_platdata(struct udevice *dev)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900806{
Marek Vasut5e890c52019-08-11 12:34:38 +0200807 struct r8a66597 *priv = dev_get_priv(dev);
808 fdt_addr_t addr;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900809
Marek Vasut5e890c52019-08-11 12:34:38 +0200810 addr = dev_read_addr(dev);
811 if (addr == FDT_ADDR_T_NONE)
812 return -EINVAL;
813 priv->reg = addr;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900814
Marek Vasut5e890c52019-08-11 12:34:38 +0200815 return 0;
816}
817
818static int r8a66597_usb_probe(struct udevice *dev)
819{
820 struct r8a66597 *priv = dev_get_priv(dev);
821 struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
Marek Vasutc1214092019-08-22 17:53:04 +0200822 int ret;
Marek Vasut5e890c52019-08-11 12:34:38 +0200823
824 bus_priv->desc_before_addr = true;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900825
Marek Vasutc1214092019-08-22 17:53:04 +0200826 if (CONFIG_IS_ENABLED(DM_REGULATOR)) {
827 ret = device_get_supply_regulator(dev, "vbus-supply",
828 &priv->vbus_supply);
829 if (ret) {
830 dev_err(dev,
831 "can't get VBUS supply\n");
832 return ret;
833 }
834
835 ret = regulator_set_enable(priv->vbus_supply, true);
836 if (ret) {
837 dev_err(dev,
838 "can't enable VBUS supply\n");
839 return ret;
840 }
841 }
842
Marek Vasut5e890c52019-08-11 12:34:38 +0200843 disable_controller(priv);
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000844 mdelay(100);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900845
Marek Vasut5e890c52019-08-11 12:34:38 +0200846 enable_controller(priv);
Marek Vasutb3935f12019-08-11 13:44:10 +0200847 r8a66597_port_power(priv, 0, 1);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900848
849 /* check usb device */
Marek Vasut5e890c52019-08-11 12:34:38 +0200850 check_usb_device_connecting(priv);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900851
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000852 mdelay(50);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900853
854 return 0;
855}
856
Marek Vasut5e890c52019-08-11 12:34:38 +0200857static int r8a66597_usb_remove(struct udevice *dev)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900858{
Marek Vasut5e890c52019-08-11 12:34:38 +0200859 struct r8a66597 *priv = dev_get_priv(dev);
Marek Vasutc1214092019-08-22 17:53:04 +0200860 int ret;
Marek Vasut5e890c52019-08-11 12:34:38 +0200861
862 disable_controller(priv);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900863
Marek Vasutc1214092019-08-22 17:53:04 +0200864 if (CONFIG_IS_ENABLED(DM_REGULATOR)) {
865 ret = regulator_set_enable(priv->vbus_supply, false);
866 if (ret) {
867 dev_err(dev,
868 "can't disable VBUS supply\n");
869 return ret;
870 }
871 }
872
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900873 return 0;
874}
Marek Vasut5e890c52019-08-11 12:34:38 +0200875
876struct dm_usb_ops r8a66597_usb_ops = {
877 .control = r8a66597_submit_control_msg,
878 .bulk = r8a66597_submit_bulk_msg,
879};
880
881static const struct udevice_id r8a66597_usb_ids[] = {
882 { .compatible = "renesas,rza1-usbhs" },
883 { }
884};
885
886U_BOOT_DRIVER(usb_r8a66597) = {
887 .name = "r8a66597_usb",
888 .id = UCLASS_USB,
889 .of_match = r8a66597_usb_ids,
890 .ofdata_to_platdata = r8a66597_usb_ofdata_to_platdata,
891 .probe = r8a66597_usb_probe,
892 .remove = r8a66597_usb_remove,
893 .ops = &r8a66597_usb_ops,
894 .priv_auto_alloc_size = sizeof(struct r8a66597),
895 .flags = DM_FLAG_ALLOC_PRIV_DMA,
896};