7620ac2da7951795af9abd21410bdc56c43860d8
[platform/kernel/u-boot.git] / drivers / usb / host / r8a66597-hcd.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * R8A66597 HCD (Host Controller Driver) for u-boot
4  *
5  * Copyright (C) 2008  Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com>
6  */
7
8 #include <common.h>
9 #include <console.h>
10 #include <dm.h>
11 #include <log.h>
12 #include <usb.h>
13 #include <asm/io.h>
14 #include <dm/device_compat.h>
15 #include <linux/delay.h>
16 #include <linux/iopoll.h>
17 #include <power/regulator.h>
18
19 #include "r8a66597.h"
20
21 #ifdef R8A66597_DEBUG
22 #define R8A66597_DPRINT         printf
23 #else
24 #define R8A66597_DPRINT(...)
25 #endif
26
27 static inline struct usb_device *usb_dev_get_parent(struct usb_device *udev)
28 {
29         struct udevice *parent = udev->dev->parent;
30
31         /*
32          * When called from usb-uclass.c: usb_scan_device() udev->dev points
33          * to the parent udevice, not the actual udevice belonging to the
34          * udev as the device is not instantiated yet.
35          *
36          * If dev is an usb-bus, then we are called from usb_scan_device() for
37          * an usb-device plugged directly into the root port, return NULL.
38          */
39         if (device_get_uclass_id(udev->dev) == UCLASS_USB)
40                 return NULL;
41
42         /*
43          * If these 2 are not the same we are being called from
44          * usb_scan_device() and udev itself is the parent.
45          */
46         if (dev_get_parent_priv(udev->dev) != udev)
47                 return udev;
48
49         /* We are being called normally, use the parent pointer */
50         if (device_get_uclass_id(parent) == UCLASS_USB_HUB)
51                 return dev_get_parent_priv(parent);
52
53         return NULL;
54 }
55
56 static void get_hub_data(struct usb_device *dev, u16 *hub_devnum, u16 *hubport)
57 {
58         struct usb_device *parent = usb_dev_get_parent(dev);
59
60         *hub_devnum = 0;
61         *hubport = 0;
62
63         /* check a device connected to root_hub */
64         if ((parent && parent->devnum == 1) ||
65             dev->devnum == 1)
66                 return;
67
68         *hub_devnum = (u8)parent->devnum;
69         *hubport = parent->portnr - 1;
70 }
71
72 static void set_devadd(struct r8a66597 *r8a66597, u8 r8a66597_address,
73                        struct usb_device *dev, int port)
74 {
75         u16 val, usbspd, upphub, hubport;
76         unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
77
78         get_hub_data(dev, &upphub, &hubport);
79         usbspd = r8a66597->speed;
80         val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
81         r8a66597_write(r8a66597, val, devadd_reg);
82 }
83
84 static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
85 {
86         u16 tmp;
87         int i = 0;
88
89         do {
90                 r8a66597_write(r8a66597, USBE, SYSCFG0);
91                 tmp = r8a66597_read(r8a66597, SYSCFG0);
92                 if (i++ > 1000) {
93                         printf("register access fail.\n");
94                         return -1;
95                 }
96         } while ((tmp & USBE) != USBE);
97         r8a66597_bclr(r8a66597, USBE, SYSCFG0);
98         /*
99          * RZ/A Only:
100          * Bits XTAL(UCKSEL) and UPLLE in SYSCFG0 for USB0 controls both USB0
101          * and USB1, so we must always set the USB0 register
102          */
103 #if (CONFIG_R8A66597_XTAL == 1)
104         r8a66597_bset(r8a66597, XTAL, SYSCFG0);
105 #endif
106         mdelay(1);
107         r8a66597_bset(r8a66597, UPLLE, SYSCFG0);
108         mdelay(1);
109         r8a66597_bset(r8a66597, SUSPM, SUSPMODE0);
110
111         return 0;
112 }
113
114 static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
115 {
116         r8a66597_bclr(r8a66597, SUSPM, SUSPMODE0);
117
118         r8a66597_bclr(r8a66597, UPLLE, SYSCFG0);
119         mdelay(1);
120         r8a66597_bclr(r8a66597, USBE, SYSCFG0);
121         mdelay(1);
122 }
123
124 static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
125 {
126         u16 val;
127
128         val = port ? DRPD : DCFM | DRPD;
129         r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
130         r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
131 }
132
133 static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
134 {
135         u16 val, tmp;
136
137         r8a66597_write(r8a66597, 0, get_intenb_reg(port));
138         r8a66597_write(r8a66597, 0, get_intsts_reg(port));
139
140         r8a66597_port_power(r8a66597, port, 0);
141
142         do {
143                 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
144                 udelay(640);
145         } while (tmp == EDGESTS);
146
147         val = port ? DRPD : DCFM | DRPD;
148         r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
149         r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
150 }
151
152 static int enable_controller(struct r8a66597 *r8a66597)
153 {
154         int ret, port;
155
156         ret = r8a66597_clock_enable(r8a66597);
157         if (ret < 0)
158                 return ret;
159
160         r8a66597_bset(r8a66597, USBE, SYSCFG0);
161
162         r8a66597_bset(r8a66597, INTL, SOFCFG);
163         r8a66597_write(r8a66597, 0, INTENB0);
164         for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
165                 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
166
167         r8a66597_bclr(r8a66597, BIGEND, CFIFOSEL);
168         r8a66597_bclr(r8a66597, BIGEND, D0FIFOSEL);
169         r8a66597_bclr(r8a66597, BIGEND, D1FIFOSEL);
170         r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
171
172         for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
173                 r8a66597_enable_port(r8a66597, port);
174
175         return 0;
176 }
177
178 static void disable_controller(struct r8a66597 *r8a66597)
179 {
180         int i;
181
182         if (!(r8a66597_read(r8a66597, SYSCFG0) & USBE))
183                 return;
184
185         r8a66597_write(r8a66597, 0, INTENB0);
186         r8a66597_write(r8a66597, 0, INTSTS0);
187
188         r8a66597_write(r8a66597, 0, D0FIFOSEL);
189         r8a66597_write(r8a66597, 0, D1FIFOSEL);
190         r8a66597_write(r8a66597, 0, DCPCFG);
191         r8a66597_write(r8a66597, 0x40, DCPMAXP);
192         r8a66597_write(r8a66597, 0, DCPCTR);
193
194         for (i = 0; i <= 10; i++)
195                 r8a66597_write(r8a66597, 0, get_devadd_addr(i));
196         for (i = 1; i <= 5; i++) {
197                 r8a66597_write(r8a66597, 0, get_pipetre_addr(i));
198                 r8a66597_write(r8a66597, 0, get_pipetrn_addr(i));
199         }
200         for (i = 1; i < R8A66597_MAX_NUM_PIPE; i++) {
201                 r8a66597_write(r8a66597, 0, get_pipectr_addr(i));
202                 r8a66597_write(r8a66597, i, PIPESEL);
203                 r8a66597_write(r8a66597, 0, PIPECFG);
204                 r8a66597_write(r8a66597, 0, PIPEBUF);
205                 r8a66597_write(r8a66597, 0, PIPEMAXP);
206                 r8a66597_write(r8a66597, 0, PIPEPERI);
207         }
208
209         for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++)
210                 r8a66597_disable_port(r8a66597, i);
211
212         r8a66597_clock_disable(r8a66597);
213 }
214
215 static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
216                               u16 mask, u16 loop)
217 {
218         u16 tmp;
219         int i = 0;
220
221         do {
222                 tmp = r8a66597_read(r8a66597, reg);
223                 if (i++ > 1000000) {
224                         printf("register%lx, loop %x is timeout\n", reg, loop);
225                         break;
226                 }
227         } while ((tmp & mask) != loop);
228 }
229
230 static void pipe_buffer_setting(struct r8a66597 *r8a66597,
231                                 struct usb_device *dev, unsigned long pipe)
232 {
233         u16 val = 0;
234         u16 pipenum, bufnum, maxpacket;
235
236         if (usb_pipein(pipe)) {
237                 pipenum = BULK_IN_PIPENUM;
238                 bufnum = BULK_IN_BUFNUM;
239                 maxpacket = dev->epmaxpacketin[usb_pipeendpoint(pipe)];
240         } else {
241                 pipenum = BULK_OUT_PIPENUM;
242                 bufnum = BULK_OUT_BUFNUM;
243                 maxpacket = dev->epmaxpacketout[usb_pipeendpoint(pipe)];
244         }
245
246         if (r8a66597->pipe_config & (1 << pipenum))
247                 return;
248         r8a66597->pipe_config |= (1 << pipenum);
249
250         r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(pipenum));
251         r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(pipenum));
252         r8a66597_write(r8a66597, pipenum, PIPESEL);
253
254         /* FIXME: This driver support bulk transfer only. */
255         if (!usb_pipein(pipe))
256                 val |= R8A66597_DIR;
257         else
258                 val |= R8A66597_SHTNAK;
259         val |= R8A66597_BULK | R8A66597_DBLB | usb_pipeendpoint(pipe);
260         r8a66597_write(r8a66597, val, PIPECFG);
261
262         r8a66597_write(r8a66597, (8 << 10) | bufnum, PIPEBUF);
263         r8a66597_write(r8a66597, make_devsel(usb_pipedevice(pipe)) |
264                                  maxpacket, PIPEMAXP);
265         r8a66597_write(r8a66597, 0, PIPEPERI);
266         r8a66597_write(r8a66597, SQCLR, get_pipectr_addr(pipenum));
267 }
268
269 static int send_setup_packet(struct r8a66597 *r8a66597, struct usb_device *dev,
270                              struct devrequest *setup)
271 {
272         int i;
273         unsigned short *p = (unsigned short *)setup;
274         unsigned long setup_addr = USBREQ;
275         u16 intsts1;
276         int timeout = 3000;
277         u16 dcpctr;
278         u16 devsel = setup->request == USB_REQ_SET_ADDRESS ? 0 : dev->devnum;
279
280         r8a66597_write(r8a66597, make_devsel(devsel) |
281                                  (8 << dev->maxpacketsize), DCPMAXP);
282         r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
283
284         dcpctr = r8a66597_read(r8a66597, DCPCTR);
285         if ((dcpctr & PID) == PID_BUF) {
286                 if (readw_poll_timeout(r8a66597->reg + DCPCTR, dcpctr,
287                                        dcpctr & BSTS, 1000) < 0) {
288                         printf("DCPCTR BSTS timeout!\n");
289                         return -ETIMEDOUT;
290                 }
291         }
292
293         for (i = 0; i < 4; i++) {
294                 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
295                 setup_addr += 2;
296         }
297         r8a66597_write(r8a66597, ~0x0001, BRDYSTS);
298         r8a66597_write(r8a66597, SUREQ, DCPCTR);
299
300         while (1) {
301                 intsts1 = r8a66597_read(r8a66597, INTSTS1);
302                 if (intsts1 & SACK)
303                         break;
304                 if (intsts1 & SIGN) {
305                         printf("setup packet send error\n");
306                         return -1;
307                 }
308                 if (timeout-- < 0) {
309                         printf("setup packet timeout\n");
310                         return -1;
311                 }
312                 udelay(500);
313         }
314
315         return 0;
316 }
317
318 static int send_bulk_packet(struct r8a66597 *r8a66597, struct usb_device *dev,
319                             unsigned long pipe, void *buffer, int transfer_len)
320 {
321         u16 tmp, bufsize;
322         u16 *buf;
323         size_t size;
324
325         R8A66597_DPRINT("%s\n", __func__);
326
327         r8a66597_mdfy(r8a66597, MBW | BULK_OUT_PIPENUM,
328                       MBW | CURPIPE, CFIFOSEL);
329         r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, BULK_OUT_PIPENUM);
330         tmp = r8a66597_read(r8a66597, CFIFOCTR);
331         if ((tmp & FRDY) == 0) {
332                 printf("%s FRDY is not set (%x)\n", __func__, tmp);
333                 return -1;
334         }
335
336         /* prepare parameters */
337         bufsize = dev->epmaxpacketout[usb_pipeendpoint(pipe)];
338         buf = (u16 *)(buffer + dev->act_len);
339         size = min((int)bufsize, transfer_len - dev->act_len);
340
341         /* write fifo */
342         r8a66597_write(r8a66597, ~(1 << BULK_OUT_PIPENUM), BEMPSTS);
343         if (buffer) {
344                 r8a66597_write_fifo(r8a66597, CFIFO, buf, size);
345                 r8a66597_write(r8a66597, BVAL, CFIFOCTR);
346         }
347
348         /* update parameters */
349         dev->act_len += size;
350
351         r8a66597_mdfy(r8a66597, PID_BUF, PID,
352                       get_pipectr_addr(BULK_OUT_PIPENUM));
353
354         while (!(r8a66597_read(r8a66597, BEMPSTS) & (1 << BULK_OUT_PIPENUM)))
355                 if (ctrlc())
356                         return -1;
357         r8a66597_write(r8a66597, ~(1 << BULK_OUT_PIPENUM), BEMPSTS);
358
359         if (dev->act_len >= transfer_len)
360                 r8a66597_mdfy(r8a66597, PID_NAK, PID,
361                               get_pipectr_addr(BULK_OUT_PIPENUM));
362
363         return 0;
364 }
365
366 static int receive_bulk_packet(struct r8a66597 *r8a66597,
367                                struct usb_device *dev,
368                                unsigned long pipe,
369                                void *buffer, int transfer_len)
370 {
371         u16 tmp;
372         u16 *buf;
373         const u16 pipenum = BULK_IN_PIPENUM;
374         int rcv_len;
375         int maxpacket = dev->epmaxpacketin[usb_pipeendpoint(pipe)];
376
377         R8A66597_DPRINT("%s\n", __func__);
378
379         /* prepare */
380         if (dev->act_len == 0) {
381                 r8a66597_mdfy(r8a66597, PID_NAK, PID,
382                               get_pipectr_addr(pipenum));
383                 r8a66597_write(r8a66597, ~(1 << pipenum), BRDYSTS);
384
385                 r8a66597_write(r8a66597, TRCLR, get_pipetre_addr(pipenum));
386                 r8a66597_write(r8a66597,
387                                (transfer_len + maxpacket - 1) / maxpacket,
388                                 get_pipetrn_addr(pipenum));
389                 r8a66597_bset(r8a66597, TRENB, get_pipetre_addr(pipenum));
390
391                 r8a66597_mdfy(r8a66597, PID_BUF, PID,
392                               get_pipectr_addr(pipenum));
393         }
394
395         r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
396         r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
397
398         while (!(r8a66597_read(r8a66597, BRDYSTS) & (1 << pipenum)))
399                 if (ctrlc())
400                         return -1;
401         r8a66597_write(r8a66597, ~(1 << pipenum), BRDYSTS);
402
403         tmp = r8a66597_read(r8a66597, CFIFOCTR);
404         if ((tmp & FRDY) == 0) {
405                 printf("%s FRDY is not set. (%x)\n", __func__, tmp);
406                 return -1;
407         }
408
409         buf = (u16 *)(buffer + dev->act_len);
410         rcv_len = tmp & DTLN;
411         dev->act_len += rcv_len;
412
413         if (buffer) {
414                 if (rcv_len == 0)
415                         r8a66597_write(r8a66597, BCLR, CFIFOCTR);
416                 else
417                         r8a66597_read_fifo(r8a66597, CFIFO, buf, rcv_len);
418         }
419
420         return 0;
421 }
422
423 static int receive_control_packet(struct r8a66597 *r8a66597,
424                                   struct usb_device *dev,
425                                   void *buffer, int transfer_len)
426 {
427         u16 tmp;
428         int rcv_len;
429
430         /* FIXME: limit transfer size : 64byte or less */
431
432         r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
433         r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
434         r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
435         r8a66597_bset(r8a66597, SQSET, DCPCTR);
436         r8a66597_write(r8a66597, BCLR, CFIFOCTR);
437         r8a66597_mdfy(r8a66597, PID_BUF, PID, DCPCTR);
438
439         while (!(r8a66597_read(r8a66597, BRDYSTS) & 0x0001))
440                 if (ctrlc())
441                         return -1;
442         r8a66597_write(r8a66597, ~0x0001, BRDYSTS);
443
444         r8a66597_mdfy(r8a66597, MBW, MBW | CURPIPE, CFIFOSEL);
445         r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
446
447         tmp = r8a66597_read(r8a66597, CFIFOCTR);
448         if ((tmp & FRDY) == 0) {
449                 printf("%s FRDY is not set. (%x)\n", __func__, tmp);
450                 return -1;
451         }
452
453         rcv_len = tmp & DTLN;
454         dev->act_len += rcv_len;
455
456         r8a66597_mdfy(r8a66597, PID_NAK, PID, DCPCTR);
457
458         if (buffer) {
459                 if (rcv_len == 0)
460                         r8a66597_write(r8a66597, BCLR, DCPCTR);
461                 else
462                         r8a66597_read_fifo(r8a66597, CFIFO, buffer, rcv_len);
463         }
464
465         return 0;
466 }
467
468 static int send_status_packet(struct r8a66597 *r8a66597,
469                               unsigned long pipe)
470 {
471         r8a66597_bset(r8a66597, SQSET, DCPCTR);
472         r8a66597_mdfy(r8a66597, PID_NAK, PID, DCPCTR);
473
474         if (usb_pipein(pipe)) {
475                 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
476                 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
477                 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
478                 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
479                 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
480         } else {
481                 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
482                 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
483                 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
484                 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
485         }
486         r8a66597_mdfy(r8a66597, PID_BUF, PID, DCPCTR);
487
488         while (!(r8a66597_read(r8a66597, BEMPSTS) & 0x0001))
489                 if (ctrlc())
490                         return -1;
491
492         return 0;
493 }
494
495 static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port)
496 {
497         int count = R8A66597_MAX_SAMPLING;
498         unsigned short syssts, old_syssts;
499
500         R8A66597_DPRINT("%s\n", __func__);
501
502         old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port) & LNST);
503         while (count > 0) {
504                 mdelay(R8A66597_RH_POLL_TIME);
505
506                 syssts = r8a66597_read(r8a66597, get_syssts_reg(port) & LNST);
507                 if (syssts == old_syssts) {
508                         count--;
509                 } else {
510                         count = R8A66597_MAX_SAMPLING;
511                         old_syssts = syssts;
512                 }
513         }
514 }
515
516 static void r8a66597_bus_reset(struct r8a66597 *r8a66597, int port)
517 {
518         mdelay(10);
519         r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT, get_dvstctr_reg(port));
520         mdelay(50);
521         r8a66597_mdfy(r8a66597, UACT, USBRST | UACT, get_dvstctr_reg(port));
522         mdelay(50);
523 }
524
525 static int check_usb_device_connecting(struct r8a66597 *r8a66597)
526 {
527         int timeout = 10000;    /* 100usec * 10000 = 1sec */
528         int i;
529
530         for (i = 0; i < 5; i++) {
531                 /* check a usb cable connect */
532                 while (!(r8a66597_read(r8a66597, INTSTS1) & ATTCH)) {
533                         if (timeout-- < 0) {
534                                 printf("%s timeout.\n", __func__);
535                                 return -1;
536                         }
537                         udelay(100);
538                 }
539
540                 /* check a data line */
541                 r8a66597_check_syssts(r8a66597, 0);
542
543                 r8a66597_bus_reset(r8a66597, 0);
544                 r8a66597->speed = get_rh_usb_speed(r8a66597, 0);
545
546                 if (!(r8a66597_read(r8a66597, INTSTS1) & DTCH)) {
547                         r8a66597->port_change = USB_PORT_STAT_C_CONNECTION;
548                         r8a66597->port_status = USB_PORT_STAT_CONNECTION |
549                                                 USB_PORT_STAT_ENABLE;
550                         return 0;       /* success */
551                 }
552
553                 R8A66597_DPRINT("USB device has detached. retry = %d\n", i);
554                 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
555         }
556
557         return -1;      /* fail */
558 }
559
560 /* Virtual Root Hub */
561
562 #include <usbroothubdes.h>
563
564 static int r8a66597_submit_rh_msg(struct udevice *udev, struct usb_device *dev,
565                                   unsigned long pipe, void *buffer,
566                                   int transfer_len, struct devrequest *cmd)
567 {
568         struct r8a66597 *r8a66597 = dev_get_priv(udev);
569         int leni = transfer_len;
570         int len = 0;
571         int stat = 0;
572         __u16 bmRType_bReq;
573         __u16 wValue;
574         __u16 wLength;
575         unsigned char data[32];
576
577         R8A66597_DPRINT("%s\n", __func__);
578
579         if (usb_pipeint(pipe)) {
580                 printf("Root-Hub submit IRQ: NOT implemented");
581                 return 0;
582         }
583
584         bmRType_bReq  = cmd->requesttype | (cmd->request << 8);
585         wValue        = cpu_to_le16 (cmd->value);
586         wLength       = cpu_to_le16 (cmd->length);
587
588         switch (bmRType_bReq) {
589         case RH_GET_STATUS:
590                 *(__u16 *)buffer = cpu_to_le16(1);
591                 len = 2;
592                 break;
593         case RH_GET_STATUS | RH_INTERFACE:
594                 *(__u16 *)buffer = cpu_to_le16(0);
595                 len = 2;
596                 break;
597         case RH_GET_STATUS | RH_ENDPOINT:
598                 *(__u16 *)buffer = cpu_to_le16(0);
599                 len = 2;
600                 break;
601         case RH_GET_STATUS | RH_CLASS:
602                 *(__u32 *)buffer = cpu_to_le32(0);
603                 len = 4;
604                 break;
605         case RH_GET_STATUS | RH_OTHER | RH_CLASS:
606                 *(__u32 *)buffer = cpu_to_le32(r8a66597->port_status |
607                                                 (r8a66597->port_change << 16));
608                 len = 4;
609                 break;
610         case RH_CLEAR_FEATURE | RH_ENDPOINT:
611         case RH_CLEAR_FEATURE | RH_CLASS:
612                 break;
613
614         case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
615                 switch (wValue) {
616                 case RH_C_PORT_CONNECTION:
617                         r8a66597->port_change &= ~USB_PORT_STAT_C_CONNECTION;
618                         break;
619                 }
620                 break;
621
622         case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
623                 switch (wValue) {
624                 case (RH_PORT_SUSPEND):
625                         break;
626                 case (RH_PORT_RESET):
627                         r8a66597_bus_reset(r8a66597, 0);
628                         break;
629                 case (RH_PORT_POWER):
630                         break;
631                 case (RH_PORT_ENABLE):
632                         break;
633                 }
634                 break;
635         case RH_SET_ADDRESS:
636                 r8a66597->rh_devnum = wValue;
637                 break;
638         case RH_GET_DESCRIPTOR:
639                 switch ((wValue & 0xff00) >> 8) {
640                 case (0x01): /* device descriptor */
641                         len = min_t(unsigned int,
642                                     leni,
643                                   min_t(unsigned int,
644                                         sizeof(root_hub_dev_des),
645                                       wLength));
646                         memcpy(buffer, root_hub_dev_des, len);
647                         break;
648                 case (0x02): /* configuration descriptor */
649                         len = min_t(unsigned int,
650                                     leni,
651                                   min_t(unsigned int,
652                                         sizeof(root_hub_config_des),
653                                       wLength));
654                         memcpy(buffer, root_hub_config_des, len);
655                         break;
656                 case (0x03): /* string descriptors */
657                         if (wValue == 0x0300) {
658                                 len = min_t(unsigned int,
659                                             leni,
660                                           min_t(unsigned int,
661                                                 sizeof(root_hub_str_index0),
662                                               wLength));
663                                 memcpy(buffer, root_hub_str_index0, len);
664                         }
665                         if (wValue == 0x0301) {
666                                 len = min_t(unsigned int,
667                                             leni,
668                                           min_t(unsigned int,
669                                                 sizeof(root_hub_str_index1),
670                                               wLength));
671                                 memcpy(buffer, root_hub_str_index1, len);
672                         }
673                         break;
674                 default:
675                         stat = USB_ST_STALLED;
676                 }
677                 break;
678
679         case RH_GET_DESCRIPTOR | RH_CLASS:
680         {
681                 __u32 temp = 0x00000001;
682
683                 data[0] = 9;            /* min length; */
684                 data[1] = 0x29;
685                 data[2] = temp & RH_A_NDP;
686                 data[3] = 0;
687                 if (temp & RH_A_PSM)
688                         data[3] |= 0x1;
689                 if (temp & RH_A_NOCP)
690                         data[3] |= 0x10;
691                 else if (temp & RH_A_OCPM)
692                         data[3] |= 0x8;
693
694                 /* corresponds to data[4-7] */
695                 data[5] = (temp & RH_A_POTPGT) >> 24;
696                 data[7] = temp & RH_B_DR;
697                 if (data[2] < 7) {
698                         data[8] = 0xff;
699                 } else {
700                         data[0] += 2;
701                         data[8] = (temp & RH_B_DR) >> 8;
702                         data[9] = 0xff;
703                         data[10] = 0xff;
704                 }
705
706                 len = min_t(unsigned int, leni,
707                             min_t(unsigned int, data[0], wLength));
708                 memcpy(buffer, data, len);
709                 break;
710         }
711
712         case RH_GET_CONFIGURATION:
713                 *(__u8 *)buffer = 0x01;
714                 len = 1;
715                 break;
716         case RH_SET_CONFIGURATION:
717                 break;
718         default:
719                 R8A66597_DPRINT("unsupported root hub command");
720                 stat = USB_ST_STALLED;
721         }
722
723         mdelay(1);
724
725         len = min_t(int, len, leni);
726
727         dev->act_len = len;
728         dev->status = stat;
729
730         return stat;
731 }
732
733 static int r8a66597_submit_control_msg(struct udevice *udev,
734                                        struct usb_device *dev,
735                                        unsigned long pipe, void *buffer,
736                                        int length, struct devrequest *setup)
737 {
738         struct r8a66597 *r8a66597 = dev_get_priv(udev);
739         u16 r8a66597_address = setup->request == USB_REQ_SET_ADDRESS ?
740                                         0 : dev->devnum;
741
742         debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
743               udev->name, dev, dev->dev->name, dev->portnr);
744
745         R8A66597_DPRINT("%s\n", __func__);
746         if (usb_pipedevice(pipe) == r8a66597->rh_devnum)
747                 return r8a66597_submit_rh_msg(udev, dev, pipe, buffer,
748                                               length, setup);
749
750         R8A66597_DPRINT("%s: setup\n", __func__);
751         set_devadd(r8a66597, r8a66597_address, dev, 0);
752
753         if (send_setup_packet(r8a66597, dev, setup) < 0) {
754                 printf("setup packet send error\n");
755                 return -1;
756         }
757
758         dev->act_len = 0;
759         if (usb_pipein(pipe))
760                 if (receive_control_packet(r8a66597, dev, buffer,
761                                            length) < 0)
762                         return -1;
763
764         if (send_status_packet(r8a66597, pipe) < 0)
765                 return -1;
766
767         dev->status = 0;
768
769         return 0;
770 }
771
772 static int r8a66597_submit_bulk_msg(struct udevice *udev,
773                                     struct usb_device *dev, unsigned long pipe,
774                                     void *buffer, int length)
775 {
776         struct r8a66597 *r8a66597 = dev_get_priv(udev);
777         int ret = 0;
778
779         debug("%s: dev='%s', udev=%p\n", __func__, udev->name, dev);
780
781         R8A66597_DPRINT("%s\n", __func__);
782         R8A66597_DPRINT("pipe = %08x, buffer = %p, len = %d, devnum = %d\n",
783                         pipe, buffer, length, dev->devnum);
784
785         set_devadd(r8a66597, dev->devnum, dev, 0);
786
787         pipe_buffer_setting(r8a66597, dev, pipe);
788
789         dev->act_len = 0;
790         while (dev->act_len < length && ret == 0) {
791                 if (ctrlc())
792                         return -1;
793
794                 if (usb_pipein(pipe))
795                         ret = receive_bulk_packet(r8a66597, dev, pipe, buffer,
796                                                   length);
797                 else
798                         ret = send_bulk_packet(r8a66597, dev, pipe, buffer,
799                                                length);
800         }
801
802         if (ret == 0)
803                 dev->status = 0;
804
805         return ret;
806 }
807
808 static int r8a66597_usb_ofdata_to_platdata(struct udevice *dev)
809 {
810         struct r8a66597 *priv = dev_get_priv(dev);
811         fdt_addr_t addr;
812
813         addr = dev_read_addr(dev);
814         if (addr == FDT_ADDR_T_NONE)
815                 return -EINVAL;
816         priv->reg = addr;
817
818         return 0;
819 }
820
821 static int r8a66597_usb_probe(struct udevice *dev)
822 {
823         struct r8a66597 *priv = dev_get_priv(dev);
824         struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
825         int ret;
826
827         bus_priv->desc_before_addr = true;
828
829         if (CONFIG_IS_ENABLED(DM_REGULATOR)) {
830                 ret = device_get_supply_regulator(dev, "vbus-supply",
831                                                   &priv->vbus_supply);
832                 if (ret) {
833                         dev_err(dev,
834                                 "can't get VBUS supply\n");
835                         return ret;
836                 }
837
838                 ret = regulator_set_enable(priv->vbus_supply, true);
839                 if (ret) {
840                         dev_err(dev,
841                                 "can't enable VBUS supply\n");
842                         return ret;
843                 }
844         }
845
846         disable_controller(priv);
847         mdelay(100);
848
849         enable_controller(priv);
850         r8a66597_port_power(priv, 0, 1);
851
852         /* check usb device */
853         check_usb_device_connecting(priv);
854
855         mdelay(50);
856
857         return 0;
858 }
859
860 static int r8a66597_usb_remove(struct udevice *dev)
861 {
862         struct r8a66597 *priv = dev_get_priv(dev);
863         int ret;
864
865         disable_controller(priv);
866
867         if (CONFIG_IS_ENABLED(DM_REGULATOR)) {
868                 ret = regulator_set_enable(priv->vbus_supply, false);
869                 if (ret) {
870                         dev_err(dev,
871                                 "can't disable VBUS supply\n");
872                         return ret;
873                 }
874         }
875
876         return 0;
877 }
878
879 struct dm_usb_ops r8a66597_usb_ops = {
880         .control = r8a66597_submit_control_msg,
881         .bulk = r8a66597_submit_bulk_msg,
882 };
883
884 static const struct udevice_id r8a66597_usb_ids[] = {
885         { .compatible = "renesas,rza1-usbhs" },
886         { }
887 };
888
889 U_BOOT_DRIVER(usb_r8a66597) = {
890         .name   = "r8a66597_usb",
891         .id     = UCLASS_USB,
892         .of_match = r8a66597_usb_ids,
893         .ofdata_to_platdata = r8a66597_usb_ofdata_to_platdata,
894         .probe  = r8a66597_usb_probe,
895         .remove = r8a66597_usb_remove,
896         .ops    = &r8a66597_usb_ops,
897         .priv_auto      = sizeof(struct r8a66597),
898         .flags  = DM_FLAG_ALLOC_PRIV_DMA,
899 };