blob: 8fc9d211db516a89dc69084679f49011160160c6 [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>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <dm/device_compat.h>
Chris Brandtc85f3cc2017-11-29 14:49:21 -050014#include <linux/iopoll.h>
Marek Vasutc1214092019-08-22 17:53:04 +020015#include <power/regulator.h>
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090016
17#include "r8a66597.h"
18
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090019#ifdef R8A66597_DEBUG
20#define R8A66597_DPRINT printf
21#else
22#define R8A66597_DPRINT(...)
23#endif
24
Marek Vasut5e890c52019-08-11 12:34:38 +020025static inline struct usb_device *usb_dev_get_parent(struct usb_device *udev)
26{
27 struct udevice *parent = udev->dev->parent;
28
29 /*
30 * When called from usb-uclass.c: usb_scan_device() udev->dev points
31 * to the parent udevice, not the actual udevice belonging to the
32 * udev as the device is not instantiated yet.
33 *
34 * If dev is an usb-bus, then we are called from usb_scan_device() for
35 * an usb-device plugged directly into the root port, return NULL.
36 */
37 if (device_get_uclass_id(udev->dev) == UCLASS_USB)
38 return NULL;
39
40 /*
41 * If these 2 are not the same we are being called from
42 * usb_scan_device() and udev itself is the parent.
43 */
44 if (dev_get_parent_priv(udev->dev) != udev)
45 return udev;
46
47 /* We are being called normally, use the parent pointer */
48 if (device_get_uclass_id(parent) == UCLASS_USB_HUB)
49 return dev_get_parent_priv(parent);
50
51 return NULL;
52}
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090053
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090054static void get_hub_data(struct usb_device *dev, u16 *hub_devnum, u16 *hubport)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090055{
Marek Vasut5e890c52019-08-11 12:34:38 +020056 struct usb_device *parent = usb_dev_get_parent(dev);
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090057
58 *hub_devnum = 0;
59 *hubport = 0;
60
61 /* check a device connected to root_hub */
Marek Vasut5e890c52019-08-11 12:34:38 +020062 if ((parent && parent->devnum == 1) ||
Marek Vasutb3935f12019-08-11 13:44:10 +020063 dev->devnum == 1)
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090064 return;
65
Marek Vasut5e890c52019-08-11 12:34:38 +020066 *hub_devnum = (u8)parent->devnum;
67 *hubport = parent->portnr - 1;
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090068}
69
70static void set_devadd(struct r8a66597 *r8a66597, u8 r8a66597_address,
Marek Vasutb3935f12019-08-11 13:44:10 +020071 struct usb_device *dev, int port)
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090072{
73 u16 val, usbspd, upphub, hubport;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090074 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
75
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090076 get_hub_data(dev, &upphub, &hubport);
77 usbspd = r8a66597->speed;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090078 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
79 r8a66597_write(r8a66597, val, devadd_reg);
80}
81
82static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
83{
84 u16 tmp;
85 int i = 0;
86
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090087 do {
88 r8a66597_write(r8a66597, USBE, SYSCFG0);
89 tmp = r8a66597_read(r8a66597, SYSCFG0);
90 if (i++ > 1000) {
91 printf("register access fail.\n");
92 return -1;
93 }
94 } while ((tmp & USBE) != USBE);
95 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
Chris Brandtfaecead2017-11-27 14:04:10 -050096 /*
97 * RZ/A Only:
98 * Bits XTAL(UCKSEL) and UPLLE in SYSCFG0 for USB0 controls both USB0
99 * and USB1, so we must always set the USB0 register
100 */
101#if (CONFIG_R8A66597_XTAL == 1)
Marek Vasut2c11a3c2019-08-11 12:34:38 +0200102 r8a66597_bset(r8a66597, XTAL, SYSCFG0);
Chris Brandtfaecead2017-11-27 14:04:10 -0500103#endif
104 mdelay(1);
Marek Vasut2c11a3c2019-08-11 12:34:38 +0200105 r8a66597_bset(r8a66597, UPLLE, SYSCFG0);
Chris Brandtfaecead2017-11-27 14:04:10 -0500106 mdelay(1);
107 r8a66597_bset(r8a66597, SUSPM, SUSPMODE0);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900108
109 return 0;
110}
111
112static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
113{
Chris Brandtfaecead2017-11-27 14:04:10 -0500114 r8a66597_bclr(r8a66597, SUSPM, SUSPMODE0);
115
Marek Vasut2c11a3c2019-08-11 12:34:38 +0200116 r8a66597_bclr(r8a66597, UPLLE, SYSCFG0);
Chris Brandtfaecead2017-11-27 14:04:10 -0500117 mdelay(1);
118 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
119 mdelay(1);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900120}
121
122static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
123{
124 u16 val;
125
126 val = port ? DRPD : DCFM | DRPD;
127 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
128 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900129}
130
131static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
132{
133 u16 val, tmp;
134
135 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
136 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
137
138 r8a66597_port_power(r8a66597, port, 0);
139
140 do {
141 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
142 udelay(640);
143 } while (tmp == EDGESTS);
144
145 val = port ? DRPD : DCFM | DRPD;
146 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
147 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
148}
149
150static int enable_controller(struct r8a66597 *r8a66597)
151{
152 int ret, port;
153
154 ret = r8a66597_clock_enable(r8a66597);
155 if (ret < 0)
156 return ret;
157
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900158 r8a66597_bset(r8a66597, USBE, SYSCFG0);
159
160 r8a66597_bset(r8a66597, INTL, SOFCFG);
161 r8a66597_write(r8a66597, 0, INTENB0);
yasuhisa umanoea33c6a2014-04-18 11:33:08 +0900162 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
163 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900164
Marek Vasutcf8a6272019-08-11 13:23:11 +0200165 r8a66597_bclr(r8a66597, BIGEND, CFIFOSEL);
166 r8a66597_bclr(r8a66597, BIGEND, D0FIFOSEL);
167 r8a66597_bclr(r8a66597, BIGEND, D1FIFOSEL);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900168 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
169
170 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
171 r8a66597_enable_port(r8a66597, port);
172
173 return 0;
174}
175
176static void disable_controller(struct r8a66597 *r8a66597)
177{
178 int i;
179
180 if (!(r8a66597_read(r8a66597, SYSCFG0) & USBE))
181 return;
182
183 r8a66597_write(r8a66597, 0, INTENB0);
184 r8a66597_write(r8a66597, 0, INTSTS0);
185
186 r8a66597_write(r8a66597, 0, D0FIFOSEL);
187 r8a66597_write(r8a66597, 0, D1FIFOSEL);
188 r8a66597_write(r8a66597, 0, DCPCFG);
189 r8a66597_write(r8a66597, 0x40, DCPMAXP);
190 r8a66597_write(r8a66597, 0, DCPCTR);
191
192 for (i = 0; i <= 10; i++)
193 r8a66597_write(r8a66597, 0, get_devadd_addr(i));
194 for (i = 1; i <= 5; i++) {
195 r8a66597_write(r8a66597, 0, get_pipetre_addr(i));
196 r8a66597_write(r8a66597, 0, get_pipetrn_addr(i));
197 }
198 for (i = 1; i < R8A66597_MAX_NUM_PIPE; i++) {
199 r8a66597_write(r8a66597, 0, get_pipectr_addr(i));
200 r8a66597_write(r8a66597, i, PIPESEL);
201 r8a66597_write(r8a66597, 0, PIPECFG);
202 r8a66597_write(r8a66597, 0, PIPEBUF);
203 r8a66597_write(r8a66597, 0, PIPEMAXP);
204 r8a66597_write(r8a66597, 0, PIPEPERI);
205 }
206
207 for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++)
208 r8a66597_disable_port(r8a66597, i);
209
210 r8a66597_clock_disable(r8a66597);
211}
212
213static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
214 u16 mask, u16 loop)
215{
216 u16 tmp;
217 int i = 0;
218
219 do {
220 tmp = r8a66597_read(r8a66597, reg);
221 if (i++ > 1000000) {
222 printf("register%lx, loop %x is timeout\n", reg, loop);
223 break;
224 }
225 } while ((tmp & mask) != loop);
226}
227
228static void pipe_buffer_setting(struct r8a66597 *r8a66597,
229 struct usb_device *dev, unsigned long pipe)
230{
231 u16 val = 0;
232 u16 pipenum, bufnum, maxpacket;
233
234 if (usb_pipein(pipe)) {
235 pipenum = BULK_IN_PIPENUM;
236 bufnum = BULK_IN_BUFNUM;
237 maxpacket = dev->epmaxpacketin[usb_pipeendpoint(pipe)];
238 } else {
239 pipenum = BULK_OUT_PIPENUM;
240 bufnum = BULK_OUT_BUFNUM;
241 maxpacket = dev->epmaxpacketout[usb_pipeendpoint(pipe)];
242 }
243
244 if (r8a66597->pipe_config & (1 << pipenum))
245 return;
246 r8a66597->pipe_config |= (1 << pipenum);
247
248 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(pipenum));
249 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(pipenum));
250 r8a66597_write(r8a66597, pipenum, PIPESEL);
251
252 /* FIXME: This driver support bulk transfer only. */
253 if (!usb_pipein(pipe))
254 val |= R8A66597_DIR;
255 else
256 val |= R8A66597_SHTNAK;
257 val |= R8A66597_BULK | R8A66597_DBLB | usb_pipeendpoint(pipe);
258 r8a66597_write(r8a66597, val, PIPECFG);
259
260 r8a66597_write(r8a66597, (8 << 10) | bufnum, PIPEBUF);
261 r8a66597_write(r8a66597, make_devsel(usb_pipedevice(pipe)) |
262 maxpacket, PIPEMAXP);
263 r8a66597_write(r8a66597, 0, PIPEPERI);
264 r8a66597_write(r8a66597, SQCLR, get_pipectr_addr(pipenum));
265}
266
267static int send_setup_packet(struct r8a66597 *r8a66597, struct usb_device *dev,
268 struct devrequest *setup)
269{
270 int i;
271 unsigned short *p = (unsigned short *)setup;
272 unsigned long setup_addr = USBREQ;
273 u16 intsts1;
274 int timeout = 3000;
Chris Brandtfaecead2017-11-27 14:04:10 -0500275 u16 dcpctr;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900276 u16 devsel = setup->request == USB_REQ_SET_ADDRESS ? 0 : dev->devnum;
277
278 r8a66597_write(r8a66597, make_devsel(devsel) |
279 (8 << dev->maxpacketsize), DCPMAXP);
280 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
281
Chris Brandtfaecead2017-11-27 14:04:10 -0500282 dcpctr = r8a66597_read(r8a66597, DCPCTR);
283 if ((dcpctr & PID) == PID_BUF) {
Chris Brandtc85f3cc2017-11-29 14:49:21 -0500284 if (readw_poll_timeout(r8a66597->reg + DCPCTR, dcpctr,
285 dcpctr & BSTS, 1000) < 0) {
286 printf("DCPCTR BSTS timeout!\n");
287 return -ETIMEDOUT;
Chris Brandtfaecead2017-11-27 14:04:10 -0500288 }
289 }
Chris Brandtfaecead2017-11-27 14:04:10 -0500290
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900291 for (i = 0; i < 4; i++) {
292 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
293 setup_addr += 2;
294 }
295 r8a66597_write(r8a66597, ~0x0001, BRDYSTS);
296 r8a66597_write(r8a66597, SUREQ, DCPCTR);
297
298 while (1) {
299 intsts1 = r8a66597_read(r8a66597, INTSTS1);
300 if (intsts1 & SACK)
301 break;
302 if (intsts1 & SIGN) {
303 printf("setup packet send error\n");
304 return -1;
305 }
306 if (timeout-- < 0) {
307 printf("setup packet timeout\n");
308 return -1;
309 }
310 udelay(500);
311 }
312
313 return 0;
314}
315
316static int send_bulk_packet(struct r8a66597 *r8a66597, struct usb_device *dev,
317 unsigned long pipe, void *buffer, int transfer_len)
318{
319 u16 tmp, bufsize;
320 u16 *buf;
321 size_t size;
322
323 R8A66597_DPRINT("%s\n", __func__);
324
325 r8a66597_mdfy(r8a66597, MBW | BULK_OUT_PIPENUM,
Marek Vasutb3935f12019-08-11 13:44:10 +0200326 MBW | CURPIPE, CFIFOSEL);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900327 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, BULK_OUT_PIPENUM);
328 tmp = r8a66597_read(r8a66597, CFIFOCTR);
329 if ((tmp & FRDY) == 0) {
330 printf("%s FRDY is not set (%x)\n", __func__, tmp);
331 return -1;
332 }
333
334 /* prepare parameters */
335 bufsize = dev->epmaxpacketout[usb_pipeendpoint(pipe)];
336 buf = (u16 *)(buffer + dev->act_len);
337 size = min((int)bufsize, transfer_len - dev->act_len);
338
339 /* write fifo */
340 r8a66597_write(r8a66597, ~(1 << BULK_OUT_PIPENUM), BEMPSTS);
341 if (buffer) {
342 r8a66597_write_fifo(r8a66597, CFIFO, buf, size);
343 r8a66597_write(r8a66597, BVAL, CFIFOCTR);
344 }
345
346 /* update parameters */
347 dev->act_len += size;
348
349 r8a66597_mdfy(r8a66597, PID_BUF, PID,
Marek Vasutb3935f12019-08-11 13:44:10 +0200350 get_pipectr_addr(BULK_OUT_PIPENUM));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900351
352 while (!(r8a66597_read(r8a66597, BEMPSTS) & (1 << BULK_OUT_PIPENUM)))
353 if (ctrlc())
354 return -1;
355 r8a66597_write(r8a66597, ~(1 << BULK_OUT_PIPENUM), BEMPSTS);
356
357 if (dev->act_len >= transfer_len)
358 r8a66597_mdfy(r8a66597, PID_NAK, PID,
Marek Vasutb3935f12019-08-11 13:44:10 +0200359 get_pipectr_addr(BULK_OUT_PIPENUM));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900360
361 return 0;
362}
363
364static int receive_bulk_packet(struct r8a66597 *r8a66597,
365 struct usb_device *dev,
366 unsigned long pipe,
367 void *buffer, int transfer_len)
368{
369 u16 tmp;
370 u16 *buf;
371 const u16 pipenum = BULK_IN_PIPENUM;
372 int rcv_len;
373 int maxpacket = dev->epmaxpacketin[usb_pipeendpoint(pipe)];
374
375 R8A66597_DPRINT("%s\n", __func__);
376
377 /* prepare */
378 if (dev->act_len == 0) {
379 r8a66597_mdfy(r8a66597, PID_NAK, PID,
Marek Vasutb3935f12019-08-11 13:44:10 +0200380 get_pipectr_addr(pipenum));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900381 r8a66597_write(r8a66597, ~(1 << pipenum), BRDYSTS);
382
383 r8a66597_write(r8a66597, TRCLR, get_pipetre_addr(pipenum));
384 r8a66597_write(r8a66597,
Marek Vasutb3935f12019-08-11 13:44:10 +0200385 (transfer_len + maxpacket - 1) / maxpacket,
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900386 get_pipetrn_addr(pipenum));
387 r8a66597_bset(r8a66597, TRENB, get_pipetre_addr(pipenum));
388
389 r8a66597_mdfy(r8a66597, PID_BUF, PID,
Marek Vasutb3935f12019-08-11 13:44:10 +0200390 get_pipectr_addr(pipenum));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900391 }
392
393 r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
394 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
395
396 while (!(r8a66597_read(r8a66597, BRDYSTS) & (1 << pipenum)))
397 if (ctrlc())
398 return -1;
399 r8a66597_write(r8a66597, ~(1 << pipenum), BRDYSTS);
400
401 tmp = r8a66597_read(r8a66597, CFIFOCTR);
402 if ((tmp & FRDY) == 0) {
403 printf("%s FRDY is not set. (%x)\n", __func__, tmp);
404 return -1;
405 }
406
407 buf = (u16 *)(buffer + dev->act_len);
408 rcv_len = tmp & DTLN;
409 dev->act_len += rcv_len;
410
411 if (buffer) {
412 if (rcv_len == 0)
413 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
414 else
415 r8a66597_read_fifo(r8a66597, CFIFO, buf, rcv_len);
416 }
417
418 return 0;
419}
420
421static int receive_control_packet(struct r8a66597 *r8a66597,
422 struct usb_device *dev,
423 void *buffer, int transfer_len)
424{
425 u16 tmp;
426 int rcv_len;
427
428 /* FIXME: limit transfer size : 64byte or less */
429
430 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
431 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
432 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
433 r8a66597_bset(r8a66597, SQSET, DCPCTR);
434 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
435 r8a66597_mdfy(r8a66597, PID_BUF, PID, DCPCTR);
436
437 while (!(r8a66597_read(r8a66597, BRDYSTS) & 0x0001))
438 if (ctrlc())
439 return -1;
440 r8a66597_write(r8a66597, ~0x0001, BRDYSTS);
441
442 r8a66597_mdfy(r8a66597, MBW, MBW | CURPIPE, CFIFOSEL);
443 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
444
445 tmp = r8a66597_read(r8a66597, CFIFOCTR);
446 if ((tmp & FRDY) == 0) {
447 printf("%s FRDY is not set. (%x)\n", __func__, tmp);
448 return -1;
449 }
450
451 rcv_len = tmp & DTLN;
452 dev->act_len += rcv_len;
453
454 r8a66597_mdfy(r8a66597, PID_NAK, PID, DCPCTR);
455
456 if (buffer) {
457 if (rcv_len == 0)
458 r8a66597_write(r8a66597, BCLR, DCPCTR);
459 else
460 r8a66597_read_fifo(r8a66597, CFIFO, buffer, rcv_len);
461 }
462
463 return 0;
464}
465
466static int send_status_packet(struct r8a66597 *r8a66597,
Marek Vasutb3935f12019-08-11 13:44:10 +0200467 unsigned long pipe)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900468{
469 r8a66597_bset(r8a66597, SQSET, DCPCTR);
470 r8a66597_mdfy(r8a66597, PID_NAK, PID, DCPCTR);
471
472 if (usb_pipein(pipe)) {
473 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
474 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
475 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
476 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
477 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
478 } else {
479 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
480 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
481 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
482 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
483 }
484 r8a66597_mdfy(r8a66597, PID_BUF, PID, DCPCTR);
485
486 while (!(r8a66597_read(r8a66597, BEMPSTS) & 0x0001))
487 if (ctrlc())
488 return -1;
489
490 return 0;
491}
492
493static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port)
494{
495 int count = R8A66597_MAX_SAMPLING;
496 unsigned short syssts, old_syssts;
497
498 R8A66597_DPRINT("%s\n", __func__);
499
500 old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port) & LNST);
501 while (count > 0) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000502 mdelay(R8A66597_RH_POLL_TIME);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900503
504 syssts = r8a66597_read(r8a66597, get_syssts_reg(port) & LNST);
505 if (syssts == old_syssts) {
506 count--;
507 } else {
508 count = R8A66597_MAX_SAMPLING;
509 old_syssts = syssts;
510 }
511 }
512}
513
514static void r8a66597_bus_reset(struct r8a66597 *r8a66597, int port)
515{
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000516 mdelay(10);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900517 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT, get_dvstctr_reg(port));
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000518 mdelay(50);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900519 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT, get_dvstctr_reg(port));
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000520 mdelay(50);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900521}
522
523static int check_usb_device_connecting(struct r8a66597 *r8a66597)
524{
525 int timeout = 10000; /* 100usec * 10000 = 1sec */
526 int i;
527
528 for (i = 0; i < 5; i++) {
529 /* check a usb cable connect */
530 while (!(r8a66597_read(r8a66597, INTSTS1) & ATTCH)) {
531 if (timeout-- < 0) {
532 printf("%s timeout.\n", __func__);
533 return -1;
534 }
535 udelay(100);
536 }
537
538 /* check a data line */
539 r8a66597_check_syssts(r8a66597, 0);
540
541 r8a66597_bus_reset(r8a66597, 0);
542 r8a66597->speed = get_rh_usb_speed(r8a66597, 0);
543
544 if (!(r8a66597_read(r8a66597, INTSTS1) & DTCH)) {
545 r8a66597->port_change = USB_PORT_STAT_C_CONNECTION;
546 r8a66597->port_status = USB_PORT_STAT_CONNECTION |
547 USB_PORT_STAT_ENABLE;
548 return 0; /* success */
549 }
550
551 R8A66597_DPRINT("USB device has detached. retry = %d\n", i);
552 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
553 }
554
555 return -1; /* fail */
556}
557
Marek Vasutb3935f12019-08-11 13:44:10 +0200558/* Virtual Root Hub */
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900559
Stephen Warren39c89682014-02-13 21:15:18 -0700560#include <usbroothubdes.h>
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900561
Marek Vasut5e890c52019-08-11 12:34:38 +0200562static int r8a66597_submit_rh_msg(struct udevice *udev, struct usb_device *dev,
563 unsigned long pipe, void *buffer,
564 int transfer_len, struct devrequest *cmd)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900565{
Marek Vasut5e890c52019-08-11 12:34:38 +0200566 struct r8a66597 *r8a66597 = dev_get_priv(udev);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900567 int leni = transfer_len;
568 int len = 0;
569 int stat = 0;
570 __u16 bmRType_bReq;
571 __u16 wValue;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900572 __u16 wLength;
573 unsigned char data[32];
574
575 R8A66597_DPRINT("%s\n", __func__);
576
Remy Bohmerd8c55ab2008-10-10 10:23:22 +0200577 if (usb_pipeint(pipe)) {
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900578 printf("Root-Hub submit IRQ: NOT implemented");
579 return 0;
580 }
581
582 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
583 wValue = cpu_to_le16 (cmd->value);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900584 wLength = cpu_to_le16 (cmd->length);
585
586 switch (bmRType_bReq) {
587 case RH_GET_STATUS:
588 *(__u16 *)buffer = cpu_to_le16(1);
589 len = 2;
590 break;
591 case RH_GET_STATUS | RH_INTERFACE:
592 *(__u16 *)buffer = cpu_to_le16(0);
593 len = 2;
594 break;
595 case RH_GET_STATUS | RH_ENDPOINT:
596 *(__u16 *)buffer = cpu_to_le16(0);
597 len = 2;
598 break;
599 case RH_GET_STATUS | RH_CLASS:
600 *(__u32 *)buffer = cpu_to_le32(0);
601 len = 4;
602 break;
603 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
604 *(__u32 *)buffer = cpu_to_le32(r8a66597->port_status |
605 (r8a66597->port_change << 16));
606 len = 4;
607 break;
608 case RH_CLEAR_FEATURE | RH_ENDPOINT:
609 case RH_CLEAR_FEATURE | RH_CLASS:
610 break;
611
612 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
613 switch (wValue) {
614 case RH_C_PORT_CONNECTION:
615 r8a66597->port_change &= ~USB_PORT_STAT_C_CONNECTION;
616 break;
617 }
618 break;
619
620 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
621 switch (wValue) {
622 case (RH_PORT_SUSPEND):
623 break;
624 case (RH_PORT_RESET):
625 r8a66597_bus_reset(r8a66597, 0);
626 break;
627 case (RH_PORT_POWER):
628 break;
629 case (RH_PORT_ENABLE):
630 break;
631 }
632 break;
633 case RH_SET_ADDRESS:
Marek Vasut5e890c52019-08-11 12:34:38 +0200634 r8a66597->rh_devnum = wValue;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900635 break;
636 case RH_GET_DESCRIPTOR:
637 switch ((wValue & 0xff00) >> 8) {
638 case (0x01): /* device descriptor */
639 len = min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200640 leni,
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900641 min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200642 sizeof(root_hub_dev_des),
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900643 wLength));
644 memcpy(buffer, root_hub_dev_des, len);
645 break;
646 case (0x02): /* configuration descriptor */
647 len = min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200648 leni,
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900649 min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200650 sizeof(root_hub_config_des),
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900651 wLength));
652 memcpy(buffer, root_hub_config_des, len);
653 break;
654 case (0x03): /* string descriptors */
655 if (wValue == 0x0300) {
656 len = min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200657 leni,
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900658 min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200659 sizeof(root_hub_str_index0),
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900660 wLength));
661 memcpy(buffer, root_hub_str_index0, len);
662 }
663 if (wValue == 0x0301) {
664 len = min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200665 leni,
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900666 min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200667 sizeof(root_hub_str_index1),
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900668 wLength));
669 memcpy(buffer, root_hub_str_index1, len);
670 }
671 break;
672 default:
673 stat = USB_ST_STALLED;
674 }
675 break;
676
677 case RH_GET_DESCRIPTOR | RH_CLASS:
678 {
679 __u32 temp = 0x00000001;
680
681 data[0] = 9; /* min length; */
682 data[1] = 0x29;
683 data[2] = temp & RH_A_NDP;
684 data[3] = 0;
685 if (temp & RH_A_PSM)
686 data[3] |= 0x1;
687 if (temp & RH_A_NOCP)
688 data[3] |= 0x10;
689 else if (temp & RH_A_OCPM)
690 data[3] |= 0x8;
691
692 /* corresponds to data[4-7] */
693 data[5] = (temp & RH_A_POTPGT) >> 24;
694 data[7] = temp & RH_B_DR;
695 if (data[2] < 7) {
696 data[8] = 0xff;
697 } else {
698 data[0] += 2;
699 data[8] = (temp & RH_B_DR) >> 8;
Marek Vasutb3935f12019-08-11 13:44:10 +0200700 data[9] = 0xff;
701 data[10] = 0xff;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900702 }
703
704 len = min_t(unsigned int, leni,
705 min_t(unsigned int, data[0], wLength));
706 memcpy(buffer, data, len);
707 break;
708 }
709
710 case RH_GET_CONFIGURATION:
Marek Vasutb3935f12019-08-11 13:44:10 +0200711 *(__u8 *)buffer = 0x01;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900712 len = 1;
713 break;
714 case RH_SET_CONFIGURATION:
715 break;
716 default:
Nobuhiro Iwamatsu9d58a012008-09-18 20:13:08 +0900717 R8A66597_DPRINT("unsupported root hub command");
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900718 stat = USB_ST_STALLED;
719 }
720
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000721 mdelay(1);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900722
723 len = min_t(int, len, leni);
724
725 dev->act_len = len;
726 dev->status = stat;
727
728 return stat;
729}
730
Marek Vasutb3935f12019-08-11 13:44:10 +0200731static int r8a66597_submit_control_msg(struct udevice *udev,
732 struct usb_device *dev,
733 unsigned long pipe, void *buffer,
734 int length, struct devrequest *setup)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900735{
Marek Vasut5e890c52019-08-11 12:34:38 +0200736 struct r8a66597 *r8a66597 = dev_get_priv(udev);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900737 u16 r8a66597_address = setup->request == USB_REQ_SET_ADDRESS ?
738 0 : dev->devnum;
739
Marek Vasut5e890c52019-08-11 12:34:38 +0200740 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
741 udev->name, dev, dev->dev->name, dev->portnr);
742
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900743 R8A66597_DPRINT("%s\n", __func__);
744 if (usb_pipedevice(pipe) == r8a66597->rh_devnum)
Marek Vasut5e890c52019-08-11 12:34:38 +0200745 return r8a66597_submit_rh_msg(udev, dev, pipe, buffer,
746 length, setup);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900747
748 R8A66597_DPRINT("%s: setup\n", __func__);
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +0900749 set_devadd(r8a66597, r8a66597_address, dev, 0);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900750
751 if (send_setup_packet(r8a66597, dev, setup) < 0) {
752 printf("setup packet send error\n");
753 return -1;
754 }
755
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +0900756 dev->act_len = 0;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900757 if (usb_pipein(pipe))
758 if (receive_control_packet(r8a66597, dev, buffer,
Marek Vasutb3935f12019-08-11 13:44:10 +0200759 length) < 0)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900760 return -1;
761
762 if (send_status_packet(r8a66597, pipe) < 0)
763 return -1;
764
765 dev->status = 0;
766
767 return 0;
768}
769
Marek Vasutb3935f12019-08-11 13:44:10 +0200770static int r8a66597_submit_bulk_msg(struct udevice *udev,
771 struct usb_device *dev, unsigned long pipe,
772 void *buffer, int length)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900773{
Marek Vasut5e890c52019-08-11 12:34:38 +0200774 struct r8a66597 *r8a66597 = dev_get_priv(udev);
775 int ret = 0;
776
777 debug("%s: dev='%s', udev=%p\n", __func__, udev->name, dev);
778
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900779 R8A66597_DPRINT("%s\n", __func__);
Marek Vasut5e890c52019-08-11 12:34:38 +0200780 R8A66597_DPRINT("pipe = %08x, buffer = %p, len = %d, devnum = %d\n",
781 pipe, buffer, length, dev->devnum);
782
783 set_devadd(r8a66597, dev->devnum, dev, 0);
784
785 pipe_buffer_setting(r8a66597, dev, pipe);
786
787 dev->act_len = 0;
788 while (dev->act_len < length && ret == 0) {
789 if (ctrlc())
790 return -1;
791
792 if (usb_pipein(pipe))
793 ret = receive_bulk_packet(r8a66597, dev, pipe, buffer,
Marek Vasutb3935f12019-08-11 13:44:10 +0200794 length);
Marek Vasut5e890c52019-08-11 12:34:38 +0200795 else
796 ret = send_bulk_packet(r8a66597, dev, pipe, buffer,
Marek Vasutb3935f12019-08-11 13:44:10 +0200797 length);
Marek Vasut5e890c52019-08-11 12:34:38 +0200798 }
799
800 if (ret == 0)
801 dev->status = 0;
802
803 return ret;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900804}
805
Marek Vasut5e890c52019-08-11 12:34:38 +0200806static int r8a66597_usb_ofdata_to_platdata(struct udevice *dev)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900807{
Marek Vasut5e890c52019-08-11 12:34:38 +0200808 struct r8a66597 *priv = dev_get_priv(dev);
809 fdt_addr_t addr;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900810
Marek Vasut5e890c52019-08-11 12:34:38 +0200811 addr = dev_read_addr(dev);
812 if (addr == FDT_ADDR_T_NONE)
813 return -EINVAL;
814 priv->reg = addr;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900815
Marek Vasut5e890c52019-08-11 12:34:38 +0200816 return 0;
817}
818
819static int r8a66597_usb_probe(struct udevice *dev)
820{
821 struct r8a66597 *priv = dev_get_priv(dev);
822 struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
Marek Vasutc1214092019-08-22 17:53:04 +0200823 int ret;
Marek Vasut5e890c52019-08-11 12:34:38 +0200824
825 bus_priv->desc_before_addr = true;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900826
Marek Vasutc1214092019-08-22 17:53:04 +0200827 if (CONFIG_IS_ENABLED(DM_REGULATOR)) {
828 ret = device_get_supply_regulator(dev, "vbus-supply",
829 &priv->vbus_supply);
830 if (ret) {
831 dev_err(dev,
832 "can't get VBUS supply\n");
833 return ret;
834 }
835
836 ret = regulator_set_enable(priv->vbus_supply, true);
837 if (ret) {
838 dev_err(dev,
839 "can't enable VBUS supply\n");
840 return ret;
841 }
842 }
843
Marek Vasut5e890c52019-08-11 12:34:38 +0200844 disable_controller(priv);
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000845 mdelay(100);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900846
Marek Vasut5e890c52019-08-11 12:34:38 +0200847 enable_controller(priv);
Marek Vasutb3935f12019-08-11 13:44:10 +0200848 r8a66597_port_power(priv, 0, 1);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900849
850 /* check usb device */
Marek Vasut5e890c52019-08-11 12:34:38 +0200851 check_usb_device_connecting(priv);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900852
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000853 mdelay(50);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900854
855 return 0;
856}
857
Marek Vasut5e890c52019-08-11 12:34:38 +0200858static int r8a66597_usb_remove(struct udevice *dev)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900859{
Marek Vasut5e890c52019-08-11 12:34:38 +0200860 struct r8a66597 *priv = dev_get_priv(dev);
Marek Vasutc1214092019-08-22 17:53:04 +0200861 int ret;
Marek Vasut5e890c52019-08-11 12:34:38 +0200862
863 disable_controller(priv);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900864
Marek Vasutc1214092019-08-22 17:53:04 +0200865 if (CONFIG_IS_ENABLED(DM_REGULATOR)) {
866 ret = regulator_set_enable(priv->vbus_supply, false);
867 if (ret) {
868 dev_err(dev,
869 "can't disable VBUS supply\n");
870 return ret;
871 }
872 }
873
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900874 return 0;
875}
Marek Vasut5e890c52019-08-11 12:34:38 +0200876
877struct dm_usb_ops r8a66597_usb_ops = {
878 .control = r8a66597_submit_control_msg,
879 .bulk = r8a66597_submit_bulk_msg,
880};
881
882static const struct udevice_id r8a66597_usb_ids[] = {
883 { .compatible = "renesas,rza1-usbhs" },
884 { }
885};
886
887U_BOOT_DRIVER(usb_r8a66597) = {
888 .name = "r8a66597_usb",
889 .id = UCLASS_USB,
890 .of_match = r8a66597_usb_ids,
891 .ofdata_to_platdata = r8a66597_usb_ofdata_to_platdata,
892 .probe = r8a66597_usb_probe,
893 .remove = r8a66597_usb_remove,
894 .ops = &r8a66597_usb_ops,
895 .priv_auto_alloc_size = sizeof(struct r8a66597),
896 .flags = DM_FLAG_ALLOC_PRIV_DMA,
897};