blob: f1fc93f3d40376dde8808b4733ae022642259db7 [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>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090012#include <usb.h>
13#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <dm/device_compat.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
Chris Brandtc85f3cc2017-11-29 14:49:21 -050016#include <linux/iopoll.h>
Marek Vasutc1214092019-08-22 17:53:04 +020017#include <power/regulator.h>
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090018
19#include "r8a66597.h"
20
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090021#ifdef R8A66597_DEBUG
22#define R8A66597_DPRINT printf
23#else
24#define R8A66597_DPRINT(...)
25#endif
26
Marek Vasut5e890c52019-08-11 12:34:38 +020027static 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}
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090055
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090056static void get_hub_data(struct usb_device *dev, u16 *hub_devnum, u16 *hubport)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090057{
Marek Vasut5e890c52019-08-11 12:34:38 +020058 struct usb_device *parent = usb_dev_get_parent(dev);
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090059
60 *hub_devnum = 0;
61 *hubport = 0;
62
63 /* check a device connected to root_hub */
Marek Vasut5e890c52019-08-11 12:34:38 +020064 if ((parent && parent->devnum == 1) ||
Marek Vasutb3935f12019-08-11 13:44:10 +020065 dev->devnum == 1)
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090066 return;
67
Marek Vasut5e890c52019-08-11 12:34:38 +020068 *hub_devnum = (u8)parent->devnum;
69 *hubport = parent->portnr - 1;
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090070}
71
72static void set_devadd(struct r8a66597 *r8a66597, u8 r8a66597_address,
Marek Vasutb3935f12019-08-11 13:44:10 +020073 struct usb_device *dev, int port)
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090074{
75 u16 val, usbspd, upphub, hubport;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090076 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
77
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090078 get_hub_data(dev, &upphub, &hubport);
79 usbspd = r8a66597->speed;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090080 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
81 r8a66597_write(r8a66597, val, devadd_reg);
82}
83
84static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
85{
86 u16 tmp;
87 int i = 0;
88
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090089 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);
Chris Brandtfaecead2017-11-27 14:04:10 -050098 /*
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)
Marek Vasut2c11a3c2019-08-11 12:34:38 +0200104 r8a66597_bset(r8a66597, XTAL, SYSCFG0);
Chris Brandtfaecead2017-11-27 14:04:10 -0500105#endif
106 mdelay(1);
Marek Vasut2c11a3c2019-08-11 12:34:38 +0200107 r8a66597_bset(r8a66597, UPLLE, SYSCFG0);
Chris Brandtfaecead2017-11-27 14:04:10 -0500108 mdelay(1);
109 r8a66597_bset(r8a66597, SUSPM, SUSPMODE0);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900110
111 return 0;
112}
113
114static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
115{
Chris Brandtfaecead2017-11-27 14:04:10 -0500116 r8a66597_bclr(r8a66597, SUSPM, SUSPMODE0);
117
Marek Vasut2c11a3c2019-08-11 12:34:38 +0200118 r8a66597_bclr(r8a66597, UPLLE, SYSCFG0);
Chris Brandtfaecead2017-11-27 14:04:10 -0500119 mdelay(1);
120 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
121 mdelay(1);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900122}
123
124static 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));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900131}
132
133static 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
152static 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
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900160 r8a66597_bset(r8a66597, USBE, SYSCFG0);
161
162 r8a66597_bset(r8a66597, INTL, SOFCFG);
163 r8a66597_write(r8a66597, 0, INTENB0);
yasuhisa umanoea33c6a2014-04-18 11:33:08 +0900164 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
165 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900166
Marek Vasutcf8a6272019-08-11 13:23:11 +0200167 r8a66597_bclr(r8a66597, BIGEND, CFIFOSEL);
168 r8a66597_bclr(r8a66597, BIGEND, D0FIFOSEL);
169 r8a66597_bclr(r8a66597, BIGEND, D1FIFOSEL);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900170 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
178static 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
215static 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
230static 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
269static 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;
Chris Brandtfaecead2017-11-27 14:04:10 -0500277 u16 dcpctr;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900278 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
Chris Brandtfaecead2017-11-27 14:04:10 -0500284 dcpctr = r8a66597_read(r8a66597, DCPCTR);
285 if ((dcpctr & PID) == PID_BUF) {
Chris Brandtc85f3cc2017-11-29 14:49:21 -0500286 if (readw_poll_timeout(r8a66597->reg + DCPCTR, dcpctr,
287 dcpctr & BSTS, 1000) < 0) {
288 printf("DCPCTR BSTS timeout!\n");
289 return -ETIMEDOUT;
Chris Brandtfaecead2017-11-27 14:04:10 -0500290 }
291 }
Chris Brandtfaecead2017-11-27 14:04:10 -0500292
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900293 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
318static 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,
Marek Vasutb3935f12019-08-11 13:44:10 +0200328 MBW | CURPIPE, CFIFOSEL);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900329 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,
Marek Vasutb3935f12019-08-11 13:44:10 +0200352 get_pipectr_addr(BULK_OUT_PIPENUM));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900353
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,
Marek Vasutb3935f12019-08-11 13:44:10 +0200361 get_pipectr_addr(BULK_OUT_PIPENUM));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900362
363 return 0;
364}
365
366static 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,
Marek Vasutb3935f12019-08-11 13:44:10 +0200382 get_pipectr_addr(pipenum));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900383 r8a66597_write(r8a66597, ~(1 << pipenum), BRDYSTS);
384
385 r8a66597_write(r8a66597, TRCLR, get_pipetre_addr(pipenum));
386 r8a66597_write(r8a66597,
Marek Vasutb3935f12019-08-11 13:44:10 +0200387 (transfer_len + maxpacket - 1) / maxpacket,
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900388 get_pipetrn_addr(pipenum));
389 r8a66597_bset(r8a66597, TRENB, get_pipetre_addr(pipenum));
390
391 r8a66597_mdfy(r8a66597, PID_BUF, PID,
Marek Vasutb3935f12019-08-11 13:44:10 +0200392 get_pipectr_addr(pipenum));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900393 }
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
423static 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
468static int send_status_packet(struct r8a66597 *r8a66597,
Marek Vasutb3935f12019-08-11 13:44:10 +0200469 unsigned long pipe)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900470{
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
495static 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) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000504 mdelay(R8A66597_RH_POLL_TIME);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900505
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
516static void r8a66597_bus_reset(struct r8a66597 *r8a66597, int port)
517{
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000518 mdelay(10);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900519 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT, get_dvstctr_reg(port));
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000520 mdelay(50);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900521 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT, get_dvstctr_reg(port));
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000522 mdelay(50);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900523}
524
525static 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
Marek Vasutb3935f12019-08-11 13:44:10 +0200560/* Virtual Root Hub */
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900561
Stephen Warren39c89682014-02-13 21:15:18 -0700562#include <usbroothubdes.h>
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900563
Marek Vasut5e890c52019-08-11 12:34:38 +0200564static 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)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900567{
Marek Vasut5e890c52019-08-11 12:34:38 +0200568 struct r8a66597 *r8a66597 = dev_get_priv(udev);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900569 int leni = transfer_len;
570 int len = 0;
571 int stat = 0;
572 __u16 bmRType_bReq;
573 __u16 wValue;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900574 __u16 wLength;
575 unsigned char data[32];
576
577 R8A66597_DPRINT("%s\n", __func__);
578
Remy Bohmerd8c55ab2008-10-10 10:23:22 +0200579 if (usb_pipeint(pipe)) {
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900580 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);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900586 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:
Marek Vasut5e890c52019-08-11 12:34:38 +0200636 r8a66597->rh_devnum = wValue;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900637 break;
638 case RH_GET_DESCRIPTOR:
639 switch ((wValue & 0xff00) >> 8) {
640 case (0x01): /* device descriptor */
641 len = min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200642 leni,
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900643 min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200644 sizeof(root_hub_dev_des),
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900645 wLength));
646 memcpy(buffer, root_hub_dev_des, len);
647 break;
648 case (0x02): /* configuration descriptor */
649 len = min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200650 leni,
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900651 min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200652 sizeof(root_hub_config_des),
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900653 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,
Marek Vasutb3935f12019-08-11 13:44:10 +0200659 leni,
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900660 min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200661 sizeof(root_hub_str_index0),
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900662 wLength));
663 memcpy(buffer, root_hub_str_index0, len);
664 }
665 if (wValue == 0x0301) {
666 len = min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200667 leni,
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900668 min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200669 sizeof(root_hub_str_index1),
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900670 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;
Marek Vasutb3935f12019-08-11 13:44:10 +0200702 data[9] = 0xff;
703 data[10] = 0xff;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900704 }
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:
Marek Vasutb3935f12019-08-11 13:44:10 +0200713 *(__u8 *)buffer = 0x01;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900714 len = 1;
715 break;
716 case RH_SET_CONFIGURATION:
717 break;
718 default:
Nobuhiro Iwamatsu9d58a012008-09-18 20:13:08 +0900719 R8A66597_DPRINT("unsupported root hub command");
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900720 stat = USB_ST_STALLED;
721 }
722
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000723 mdelay(1);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900724
725 len = min_t(int, len, leni);
726
727 dev->act_len = len;
728 dev->status = stat;
729
730 return stat;
731}
732
Marek Vasutb3935f12019-08-11 13:44:10 +0200733static 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)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900737{
Marek Vasut5e890c52019-08-11 12:34:38 +0200738 struct r8a66597 *r8a66597 = dev_get_priv(udev);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900739 u16 r8a66597_address = setup->request == USB_REQ_SET_ADDRESS ?
740 0 : dev->devnum;
741
Marek Vasut5e890c52019-08-11 12:34:38 +0200742 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
743 udev->name, dev, dev->dev->name, dev->portnr);
744
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900745 R8A66597_DPRINT("%s\n", __func__);
746 if (usb_pipedevice(pipe) == r8a66597->rh_devnum)
Marek Vasut5e890c52019-08-11 12:34:38 +0200747 return r8a66597_submit_rh_msg(udev, dev, pipe, buffer,
748 length, setup);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900749
750 R8A66597_DPRINT("%s: setup\n", __func__);
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +0900751 set_devadd(r8a66597, r8a66597_address, dev, 0);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900752
753 if (send_setup_packet(r8a66597, dev, setup) < 0) {
754 printf("setup packet send error\n");
755 return -1;
756 }
757
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +0900758 dev->act_len = 0;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900759 if (usb_pipein(pipe))
760 if (receive_control_packet(r8a66597, dev, buffer,
Marek Vasutb3935f12019-08-11 13:44:10 +0200761 length) < 0)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900762 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
Marek Vasutb3935f12019-08-11 13:44:10 +0200772static int r8a66597_submit_bulk_msg(struct udevice *udev,
773 struct usb_device *dev, unsigned long pipe,
774 void *buffer, int length)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900775{
Marek Vasut5e890c52019-08-11 12:34:38 +0200776 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
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900781 R8A66597_DPRINT("%s\n", __func__);
Marek Vasut5e890c52019-08-11 12:34:38 +0200782 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,
Marek Vasutb3935f12019-08-11 13:44:10 +0200796 length);
Marek Vasut5e890c52019-08-11 12:34:38 +0200797 else
798 ret = send_bulk_packet(r8a66597, dev, pipe, buffer,
Marek Vasutb3935f12019-08-11 13:44:10 +0200799 length);
Marek Vasut5e890c52019-08-11 12:34:38 +0200800 }
801
802 if (ret == 0)
803 dev->status = 0;
804
805 return ret;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900806}
807
Simon Glassaad29ae2020-12-03 16:55:21 -0700808static int r8a66597_usb_of_to_plat(struct udevice *dev)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900809{
Marek Vasut5e890c52019-08-11 12:34:38 +0200810 struct r8a66597 *priv = dev_get_priv(dev);
811 fdt_addr_t addr;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900812
Marek Vasut5e890c52019-08-11 12:34:38 +0200813 addr = dev_read_addr(dev);
814 if (addr == FDT_ADDR_T_NONE)
815 return -EINVAL;
816 priv->reg = addr;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900817
Marek Vasut5e890c52019-08-11 12:34:38 +0200818 return 0;
819}
820
821static 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);
Marek Vasutc1214092019-08-22 17:53:04 +0200825 int ret;
Marek Vasut5e890c52019-08-11 12:34:38 +0200826
827 bus_priv->desc_before_addr = true;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900828
Marek Vasutc1214092019-08-22 17:53:04 +0200829 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
Marek Vasut5e890c52019-08-11 12:34:38 +0200846 disable_controller(priv);
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000847 mdelay(100);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900848
Marek Vasut5e890c52019-08-11 12:34:38 +0200849 enable_controller(priv);
Marek Vasutb3935f12019-08-11 13:44:10 +0200850 r8a66597_port_power(priv, 0, 1);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900851
852 /* check usb device */
Marek Vasut5e890c52019-08-11 12:34:38 +0200853 check_usb_device_connecting(priv);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900854
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000855 mdelay(50);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900856
857 return 0;
858}
859
Marek Vasut5e890c52019-08-11 12:34:38 +0200860static int r8a66597_usb_remove(struct udevice *dev)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900861{
Marek Vasut5e890c52019-08-11 12:34:38 +0200862 struct r8a66597 *priv = dev_get_priv(dev);
Marek Vasutc1214092019-08-22 17:53:04 +0200863 int ret;
Marek Vasut5e890c52019-08-11 12:34:38 +0200864
865 disable_controller(priv);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900866
Marek Vasutc1214092019-08-22 17:53:04 +0200867 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
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900876 return 0;
877}
Marek Vasut5e890c52019-08-11 12:34:38 +0200878
879struct dm_usb_ops r8a66597_usb_ops = {
880 .control = r8a66597_submit_control_msg,
881 .bulk = r8a66597_submit_bulk_msg,
882};
883
884static const struct udevice_id r8a66597_usb_ids[] = {
885 { .compatible = "renesas,rza1-usbhs" },
886 { }
887};
888
889U_BOOT_DRIVER(usb_r8a66597) = {
890 .name = "r8a66597_usb",
891 .id = UCLASS_USB,
892 .of_match = r8a66597_usb_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700893 .of_to_plat = r8a66597_usb_of_to_plat,
Marek Vasut5e890c52019-08-11 12:34:38 +0200894 .probe = r8a66597_usb_probe,
895 .remove = r8a66597_usb_remove,
896 .ops = &r8a66597_usb_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700897 .priv_auto = sizeof(struct r8a66597),
Marek Vasut5e890c52019-08-11 12:34:38 +0200898 .flags = DM_FLAG_ALLOC_PRIV_DMA,
899};