blob: 8b60ab2c6d022e0dbd8da6cbacf36b49a255a1ca [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Yoshihiro Shimodafd0f2f32008-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 Shimodafd0f2f32008-07-09 21:07:38 +09006 */
7
8#include <common.h>
Simon Glass24b852a2015-11-08 23:47:45 -07009#include <console.h>
Marek Vasut7f3858f2019-08-11 12:34:38 +020010#include <dm.h>
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +090011#include <usb.h>
12#include <asm/io.h>
Chris Brandt243fd642017-11-29 14:49:21 -050013#include <linux/iopoll.h>
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +090014
15#include "r8a66597.h"
16
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +090017#ifdef R8A66597_DEBUG
18#define R8A66597_DPRINT printf
19#else
20#define R8A66597_DPRINT(...)
21#endif
22
Marek Vasut7f3858f2019-08-11 12:34:38 +020023static inline struct usb_device *usb_dev_get_parent(struct usb_device *udev)
24{
25 struct udevice *parent = udev->dev->parent;
26
27 /*
28 * When called from usb-uclass.c: usb_scan_device() udev->dev points
29 * to the parent udevice, not the actual udevice belonging to the
30 * udev as the device is not instantiated yet.
31 *
32 * If dev is an usb-bus, then we are called from usb_scan_device() for
33 * an usb-device plugged directly into the root port, return NULL.
34 */
35 if (device_get_uclass_id(udev->dev) == UCLASS_USB)
36 return NULL;
37
38 /*
39 * If these 2 are not the same we are being called from
40 * usb_scan_device() and udev itself is the parent.
41 */
42 if (dev_get_parent_priv(udev->dev) != udev)
43 return udev;
44
45 /* We are being called normally, use the parent pointer */
46 if (device_get_uclass_id(parent) == UCLASS_USB_HUB)
47 return dev_get_parent_priv(parent);
48
49 return NULL;
50}
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +090051
Yoshihiro Shimoda60ece6d2008-10-29 20:05:18 +090052static void get_hub_data(struct usb_device *dev, u16 *hub_devnum, u16 *hubport)
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +090053{
Marek Vasut7f3858f2019-08-11 12:34:38 +020054 struct usb_device *parent = usb_dev_get_parent(dev);
Yoshihiro Shimoda60ece6d2008-10-29 20:05:18 +090055
56 *hub_devnum = 0;
57 *hubport = 0;
58
59 /* check a device connected to root_hub */
Marek Vasut7f3858f2019-08-11 12:34:38 +020060 if ((parent && parent->devnum == 1) ||
Marek Vasut8b548302019-08-11 13:44:10 +020061 dev->devnum == 1)
Yoshihiro Shimoda60ece6d2008-10-29 20:05:18 +090062 return;
63
Marek Vasut7f3858f2019-08-11 12:34:38 +020064 *hub_devnum = (u8)parent->devnum;
65 *hubport = parent->portnr - 1;
Yoshihiro Shimoda60ece6d2008-10-29 20:05:18 +090066}
67
68static void set_devadd(struct r8a66597 *r8a66597, u8 r8a66597_address,
Marek Vasut8b548302019-08-11 13:44:10 +020069 struct usb_device *dev, int port)
Yoshihiro Shimoda60ece6d2008-10-29 20:05:18 +090070{
71 u16 val, usbspd, upphub, hubport;
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +090072 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
73
Yoshihiro Shimoda60ece6d2008-10-29 20:05:18 +090074 get_hub_data(dev, &upphub, &hubport);
75 usbspd = r8a66597->speed;
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +090076 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
77 r8a66597_write(r8a66597, val, devadd_reg);
78}
79
80static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
81{
82 u16 tmp;
83 int i = 0;
84
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +090085 do {
86 r8a66597_write(r8a66597, USBE, SYSCFG0);
87 tmp = r8a66597_read(r8a66597, SYSCFG0);
88 if (i++ > 1000) {
89 printf("register access fail.\n");
90 return -1;
91 }
92 } while ((tmp & USBE) != USBE);
93 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
Chris Brandt11f46782017-11-27 14:04:10 -050094 /*
95 * RZ/A Only:
96 * Bits XTAL(UCKSEL) and UPLLE in SYSCFG0 for USB0 controls both USB0
97 * and USB1, so we must always set the USB0 register
98 */
99#if (CONFIG_R8A66597_XTAL == 1)
Marek Vasut1eb381a2019-08-11 12:34:38 +0200100 r8a66597_bset(r8a66597, XTAL, SYSCFG0);
Chris Brandt11f46782017-11-27 14:04:10 -0500101#endif
102 mdelay(1);
Marek Vasut1eb381a2019-08-11 12:34:38 +0200103 r8a66597_bset(r8a66597, UPLLE, SYSCFG0);
Chris Brandt11f46782017-11-27 14:04:10 -0500104 mdelay(1);
105 r8a66597_bset(r8a66597, SUSPM, SUSPMODE0);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900106
107 return 0;
108}
109
110static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
111{
Chris Brandt11f46782017-11-27 14:04:10 -0500112 r8a66597_bclr(r8a66597, SUSPM, SUSPMODE0);
113
Marek Vasut1eb381a2019-08-11 12:34:38 +0200114 r8a66597_bclr(r8a66597, UPLLE, SYSCFG0);
Chris Brandt11f46782017-11-27 14:04:10 -0500115 mdelay(1);
116 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
117 mdelay(1);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900118}
119
120static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
121{
122 u16 val;
123
124 val = port ? DRPD : DCFM | DRPD;
125 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
126 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900127}
128
129static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
130{
131 u16 val, tmp;
132
133 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
134 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
135
136 r8a66597_port_power(r8a66597, port, 0);
137
138 do {
139 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
140 udelay(640);
141 } while (tmp == EDGESTS);
142
143 val = port ? DRPD : DCFM | DRPD;
144 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
145 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
146}
147
148static int enable_controller(struct r8a66597 *r8a66597)
149{
150 int ret, port;
151
152 ret = r8a66597_clock_enable(r8a66597);
153 if (ret < 0)
154 return ret;
155
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900156 r8a66597_bset(r8a66597, USBE, SYSCFG0);
157
158 r8a66597_bset(r8a66597, INTL, SOFCFG);
159 r8a66597_write(r8a66597, 0, INTENB0);
yasuhisa umano8ecdce72014-04-18 11:33:08 +0900160 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
161 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900162
Marek Vasut81644e02019-08-11 13:23:11 +0200163 r8a66597_bclr(r8a66597, BIGEND, CFIFOSEL);
164 r8a66597_bclr(r8a66597, BIGEND, D0FIFOSEL);
165 r8a66597_bclr(r8a66597, BIGEND, D1FIFOSEL);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900166 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
167
168 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
169 r8a66597_enable_port(r8a66597, port);
170
171 return 0;
172}
173
174static void disable_controller(struct r8a66597 *r8a66597)
175{
176 int i;
177
178 if (!(r8a66597_read(r8a66597, SYSCFG0) & USBE))
179 return;
180
181 r8a66597_write(r8a66597, 0, INTENB0);
182 r8a66597_write(r8a66597, 0, INTSTS0);
183
184 r8a66597_write(r8a66597, 0, D0FIFOSEL);
185 r8a66597_write(r8a66597, 0, D1FIFOSEL);
186 r8a66597_write(r8a66597, 0, DCPCFG);
187 r8a66597_write(r8a66597, 0x40, DCPMAXP);
188 r8a66597_write(r8a66597, 0, DCPCTR);
189
190 for (i = 0; i <= 10; i++)
191 r8a66597_write(r8a66597, 0, get_devadd_addr(i));
192 for (i = 1; i <= 5; i++) {
193 r8a66597_write(r8a66597, 0, get_pipetre_addr(i));
194 r8a66597_write(r8a66597, 0, get_pipetrn_addr(i));
195 }
196 for (i = 1; i < R8A66597_MAX_NUM_PIPE; i++) {
197 r8a66597_write(r8a66597, 0, get_pipectr_addr(i));
198 r8a66597_write(r8a66597, i, PIPESEL);
199 r8a66597_write(r8a66597, 0, PIPECFG);
200 r8a66597_write(r8a66597, 0, PIPEBUF);
201 r8a66597_write(r8a66597, 0, PIPEMAXP);
202 r8a66597_write(r8a66597, 0, PIPEPERI);
203 }
204
205 for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++)
206 r8a66597_disable_port(r8a66597, i);
207
208 r8a66597_clock_disable(r8a66597);
209}
210
211static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
212 u16 mask, u16 loop)
213{
214 u16 tmp;
215 int i = 0;
216
217 do {
218 tmp = r8a66597_read(r8a66597, reg);
219 if (i++ > 1000000) {
220 printf("register%lx, loop %x is timeout\n", reg, loop);
221 break;
222 }
223 } while ((tmp & mask) != loop);
224}
225
226static void pipe_buffer_setting(struct r8a66597 *r8a66597,
227 struct usb_device *dev, unsigned long pipe)
228{
229 u16 val = 0;
230 u16 pipenum, bufnum, maxpacket;
231
232 if (usb_pipein(pipe)) {
233 pipenum = BULK_IN_PIPENUM;
234 bufnum = BULK_IN_BUFNUM;
235 maxpacket = dev->epmaxpacketin[usb_pipeendpoint(pipe)];
236 } else {
237 pipenum = BULK_OUT_PIPENUM;
238 bufnum = BULK_OUT_BUFNUM;
239 maxpacket = dev->epmaxpacketout[usb_pipeendpoint(pipe)];
240 }
241
242 if (r8a66597->pipe_config & (1 << pipenum))
243 return;
244 r8a66597->pipe_config |= (1 << pipenum);
245
246 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(pipenum));
247 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(pipenum));
248 r8a66597_write(r8a66597, pipenum, PIPESEL);
249
250 /* FIXME: This driver support bulk transfer only. */
251 if (!usb_pipein(pipe))
252 val |= R8A66597_DIR;
253 else
254 val |= R8A66597_SHTNAK;
255 val |= R8A66597_BULK | R8A66597_DBLB | usb_pipeendpoint(pipe);
256 r8a66597_write(r8a66597, val, PIPECFG);
257
258 r8a66597_write(r8a66597, (8 << 10) | bufnum, PIPEBUF);
259 r8a66597_write(r8a66597, make_devsel(usb_pipedevice(pipe)) |
260 maxpacket, PIPEMAXP);
261 r8a66597_write(r8a66597, 0, PIPEPERI);
262 r8a66597_write(r8a66597, SQCLR, get_pipectr_addr(pipenum));
263}
264
265static int send_setup_packet(struct r8a66597 *r8a66597, struct usb_device *dev,
266 struct devrequest *setup)
267{
268 int i;
269 unsigned short *p = (unsigned short *)setup;
270 unsigned long setup_addr = USBREQ;
271 u16 intsts1;
272 int timeout = 3000;
Chris Brandt11f46782017-11-27 14:04:10 -0500273 u16 dcpctr;
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900274 u16 devsel = setup->request == USB_REQ_SET_ADDRESS ? 0 : dev->devnum;
275
276 r8a66597_write(r8a66597, make_devsel(devsel) |
277 (8 << dev->maxpacketsize), DCPMAXP);
278 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
279
Chris Brandt11f46782017-11-27 14:04:10 -0500280 dcpctr = r8a66597_read(r8a66597, DCPCTR);
281 if ((dcpctr & PID) == PID_BUF) {
Chris Brandt243fd642017-11-29 14:49:21 -0500282 if (readw_poll_timeout(r8a66597->reg + DCPCTR, dcpctr,
283 dcpctr & BSTS, 1000) < 0) {
284 printf("DCPCTR BSTS timeout!\n");
285 return -ETIMEDOUT;
Chris Brandt11f46782017-11-27 14:04:10 -0500286 }
287 }
Chris Brandt11f46782017-11-27 14:04:10 -0500288
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900289 for (i = 0; i < 4; i++) {
290 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
291 setup_addr += 2;
292 }
293 r8a66597_write(r8a66597, ~0x0001, BRDYSTS);
294 r8a66597_write(r8a66597, SUREQ, DCPCTR);
295
296 while (1) {
297 intsts1 = r8a66597_read(r8a66597, INTSTS1);
298 if (intsts1 & SACK)
299 break;
300 if (intsts1 & SIGN) {
301 printf("setup packet send error\n");
302 return -1;
303 }
304 if (timeout-- < 0) {
305 printf("setup packet timeout\n");
306 return -1;
307 }
308 udelay(500);
309 }
310
311 return 0;
312}
313
314static int send_bulk_packet(struct r8a66597 *r8a66597, struct usb_device *dev,
315 unsigned long pipe, void *buffer, int transfer_len)
316{
317 u16 tmp, bufsize;
318 u16 *buf;
319 size_t size;
320
321 R8A66597_DPRINT("%s\n", __func__);
322
323 r8a66597_mdfy(r8a66597, MBW | BULK_OUT_PIPENUM,
Marek Vasut8b548302019-08-11 13:44:10 +0200324 MBW | CURPIPE, CFIFOSEL);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900325 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, BULK_OUT_PIPENUM);
326 tmp = r8a66597_read(r8a66597, CFIFOCTR);
327 if ((tmp & FRDY) == 0) {
328 printf("%s FRDY is not set (%x)\n", __func__, tmp);
329 return -1;
330 }
331
332 /* prepare parameters */
333 bufsize = dev->epmaxpacketout[usb_pipeendpoint(pipe)];
334 buf = (u16 *)(buffer + dev->act_len);
335 size = min((int)bufsize, transfer_len - dev->act_len);
336
337 /* write fifo */
338 r8a66597_write(r8a66597, ~(1 << BULK_OUT_PIPENUM), BEMPSTS);
339 if (buffer) {
340 r8a66597_write_fifo(r8a66597, CFIFO, buf, size);
341 r8a66597_write(r8a66597, BVAL, CFIFOCTR);
342 }
343
344 /* update parameters */
345 dev->act_len += size;
346
347 r8a66597_mdfy(r8a66597, PID_BUF, PID,
Marek Vasut8b548302019-08-11 13:44:10 +0200348 get_pipectr_addr(BULK_OUT_PIPENUM));
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900349
350 while (!(r8a66597_read(r8a66597, BEMPSTS) & (1 << BULK_OUT_PIPENUM)))
351 if (ctrlc())
352 return -1;
353 r8a66597_write(r8a66597, ~(1 << BULK_OUT_PIPENUM), BEMPSTS);
354
355 if (dev->act_len >= transfer_len)
356 r8a66597_mdfy(r8a66597, PID_NAK, PID,
Marek Vasut8b548302019-08-11 13:44:10 +0200357 get_pipectr_addr(BULK_OUT_PIPENUM));
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900358
359 return 0;
360}
361
362static int receive_bulk_packet(struct r8a66597 *r8a66597,
363 struct usb_device *dev,
364 unsigned long pipe,
365 void *buffer, int transfer_len)
366{
367 u16 tmp;
368 u16 *buf;
369 const u16 pipenum = BULK_IN_PIPENUM;
370 int rcv_len;
371 int maxpacket = dev->epmaxpacketin[usb_pipeendpoint(pipe)];
372
373 R8A66597_DPRINT("%s\n", __func__);
374
375 /* prepare */
376 if (dev->act_len == 0) {
377 r8a66597_mdfy(r8a66597, PID_NAK, PID,
Marek Vasut8b548302019-08-11 13:44:10 +0200378 get_pipectr_addr(pipenum));
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900379 r8a66597_write(r8a66597, ~(1 << pipenum), BRDYSTS);
380
381 r8a66597_write(r8a66597, TRCLR, get_pipetre_addr(pipenum));
382 r8a66597_write(r8a66597,
Marek Vasut8b548302019-08-11 13:44:10 +0200383 (transfer_len + maxpacket - 1) / maxpacket,
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900384 get_pipetrn_addr(pipenum));
385 r8a66597_bset(r8a66597, TRENB, get_pipetre_addr(pipenum));
386
387 r8a66597_mdfy(r8a66597, PID_BUF, PID,
Marek Vasut8b548302019-08-11 13:44:10 +0200388 get_pipectr_addr(pipenum));
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900389 }
390
391 r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
392 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
393
394 while (!(r8a66597_read(r8a66597, BRDYSTS) & (1 << pipenum)))
395 if (ctrlc())
396 return -1;
397 r8a66597_write(r8a66597, ~(1 << pipenum), BRDYSTS);
398
399 tmp = r8a66597_read(r8a66597, CFIFOCTR);
400 if ((tmp & FRDY) == 0) {
401 printf("%s FRDY is not set. (%x)\n", __func__, tmp);
402 return -1;
403 }
404
405 buf = (u16 *)(buffer + dev->act_len);
406 rcv_len = tmp & DTLN;
407 dev->act_len += rcv_len;
408
409 if (buffer) {
410 if (rcv_len == 0)
411 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
412 else
413 r8a66597_read_fifo(r8a66597, CFIFO, buf, rcv_len);
414 }
415
416 return 0;
417}
418
419static int receive_control_packet(struct r8a66597 *r8a66597,
420 struct usb_device *dev,
421 void *buffer, int transfer_len)
422{
423 u16 tmp;
424 int rcv_len;
425
426 /* FIXME: limit transfer size : 64byte or less */
427
428 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
429 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
430 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
431 r8a66597_bset(r8a66597, SQSET, DCPCTR);
432 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
433 r8a66597_mdfy(r8a66597, PID_BUF, PID, DCPCTR);
434
435 while (!(r8a66597_read(r8a66597, BRDYSTS) & 0x0001))
436 if (ctrlc())
437 return -1;
438 r8a66597_write(r8a66597, ~0x0001, BRDYSTS);
439
440 r8a66597_mdfy(r8a66597, MBW, MBW | CURPIPE, CFIFOSEL);
441 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
442
443 tmp = r8a66597_read(r8a66597, CFIFOCTR);
444 if ((tmp & FRDY) == 0) {
445 printf("%s FRDY is not set. (%x)\n", __func__, tmp);
446 return -1;
447 }
448
449 rcv_len = tmp & DTLN;
450 dev->act_len += rcv_len;
451
452 r8a66597_mdfy(r8a66597, PID_NAK, PID, DCPCTR);
453
454 if (buffer) {
455 if (rcv_len == 0)
456 r8a66597_write(r8a66597, BCLR, DCPCTR);
457 else
458 r8a66597_read_fifo(r8a66597, CFIFO, buffer, rcv_len);
459 }
460
461 return 0;
462}
463
464static int send_status_packet(struct r8a66597 *r8a66597,
Marek Vasut8b548302019-08-11 13:44:10 +0200465 unsigned long pipe)
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900466{
467 r8a66597_bset(r8a66597, SQSET, DCPCTR);
468 r8a66597_mdfy(r8a66597, PID_NAK, PID, DCPCTR);
469
470 if (usb_pipein(pipe)) {
471 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
472 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
473 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
474 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
475 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
476 } else {
477 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
478 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
479 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
480 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
481 }
482 r8a66597_mdfy(r8a66597, PID_BUF, PID, DCPCTR);
483
484 while (!(r8a66597_read(r8a66597, BEMPSTS) & 0x0001))
485 if (ctrlc())
486 return -1;
487
488 return 0;
489}
490
491static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port)
492{
493 int count = R8A66597_MAX_SAMPLING;
494 unsigned short syssts, old_syssts;
495
496 R8A66597_DPRINT("%s\n", __func__);
497
498 old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port) & LNST);
499 while (count > 0) {
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000500 mdelay(R8A66597_RH_POLL_TIME);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900501
502 syssts = r8a66597_read(r8a66597, get_syssts_reg(port) & LNST);
503 if (syssts == old_syssts) {
504 count--;
505 } else {
506 count = R8A66597_MAX_SAMPLING;
507 old_syssts = syssts;
508 }
509 }
510}
511
512static void r8a66597_bus_reset(struct r8a66597 *r8a66597, int port)
513{
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000514 mdelay(10);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900515 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT, get_dvstctr_reg(port));
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000516 mdelay(50);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900517 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT, get_dvstctr_reg(port));
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000518 mdelay(50);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900519}
520
521static int check_usb_device_connecting(struct r8a66597 *r8a66597)
522{
523 int timeout = 10000; /* 100usec * 10000 = 1sec */
524 int i;
525
526 for (i = 0; i < 5; i++) {
527 /* check a usb cable connect */
528 while (!(r8a66597_read(r8a66597, INTSTS1) & ATTCH)) {
529 if (timeout-- < 0) {
530 printf("%s timeout.\n", __func__);
531 return -1;
532 }
533 udelay(100);
534 }
535
536 /* check a data line */
537 r8a66597_check_syssts(r8a66597, 0);
538
539 r8a66597_bus_reset(r8a66597, 0);
540 r8a66597->speed = get_rh_usb_speed(r8a66597, 0);
541
542 if (!(r8a66597_read(r8a66597, INTSTS1) & DTCH)) {
543 r8a66597->port_change = USB_PORT_STAT_C_CONNECTION;
544 r8a66597->port_status = USB_PORT_STAT_CONNECTION |
545 USB_PORT_STAT_ENABLE;
546 return 0; /* success */
547 }
548
549 R8A66597_DPRINT("USB device has detached. retry = %d\n", i);
550 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
551 }
552
553 return -1; /* fail */
554}
555
Marek Vasut8b548302019-08-11 13:44:10 +0200556/* Virtual Root Hub */
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900557
Stephen Warreneb838e72014-02-13 21:15:18 -0700558#include <usbroothubdes.h>
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900559
Marek Vasut7f3858f2019-08-11 12:34:38 +0200560static int r8a66597_submit_rh_msg(struct udevice *udev, struct usb_device *dev,
561 unsigned long pipe, void *buffer,
562 int transfer_len, struct devrequest *cmd)
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900563{
Marek Vasut7f3858f2019-08-11 12:34:38 +0200564 struct r8a66597 *r8a66597 = dev_get_priv(udev);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900565 int leni = transfer_len;
566 int len = 0;
567 int stat = 0;
568 __u16 bmRType_bReq;
569 __u16 wValue;
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900570 __u16 wLength;
571 unsigned char data[32];
572
573 R8A66597_DPRINT("%s\n", __func__);
574
Remy Bohmer9dbc3662008-10-10 10:23:22 +0200575 if (usb_pipeint(pipe)) {
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900576 printf("Root-Hub submit IRQ: NOT implemented");
577 return 0;
578 }
579
580 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
581 wValue = cpu_to_le16 (cmd->value);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900582 wLength = cpu_to_le16 (cmd->length);
583
584 switch (bmRType_bReq) {
585 case RH_GET_STATUS:
586 *(__u16 *)buffer = cpu_to_le16(1);
587 len = 2;
588 break;
589 case RH_GET_STATUS | RH_INTERFACE:
590 *(__u16 *)buffer = cpu_to_le16(0);
591 len = 2;
592 break;
593 case RH_GET_STATUS | RH_ENDPOINT:
594 *(__u16 *)buffer = cpu_to_le16(0);
595 len = 2;
596 break;
597 case RH_GET_STATUS | RH_CLASS:
598 *(__u32 *)buffer = cpu_to_le32(0);
599 len = 4;
600 break;
601 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
602 *(__u32 *)buffer = cpu_to_le32(r8a66597->port_status |
603 (r8a66597->port_change << 16));
604 len = 4;
605 break;
606 case RH_CLEAR_FEATURE | RH_ENDPOINT:
607 case RH_CLEAR_FEATURE | RH_CLASS:
608 break;
609
610 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
611 switch (wValue) {
612 case RH_C_PORT_CONNECTION:
613 r8a66597->port_change &= ~USB_PORT_STAT_C_CONNECTION;
614 break;
615 }
616 break;
617
618 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
619 switch (wValue) {
620 case (RH_PORT_SUSPEND):
621 break;
622 case (RH_PORT_RESET):
623 r8a66597_bus_reset(r8a66597, 0);
624 break;
625 case (RH_PORT_POWER):
626 break;
627 case (RH_PORT_ENABLE):
628 break;
629 }
630 break;
631 case RH_SET_ADDRESS:
Marek Vasut7f3858f2019-08-11 12:34:38 +0200632 r8a66597->rh_devnum = wValue;
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900633 break;
634 case RH_GET_DESCRIPTOR:
635 switch ((wValue & 0xff00) >> 8) {
636 case (0x01): /* device descriptor */
637 len = min_t(unsigned int,
Marek Vasut8b548302019-08-11 13:44:10 +0200638 leni,
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900639 min_t(unsigned int,
Marek Vasut8b548302019-08-11 13:44:10 +0200640 sizeof(root_hub_dev_des),
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900641 wLength));
642 memcpy(buffer, root_hub_dev_des, len);
643 break;
644 case (0x02): /* configuration descriptor */
645 len = min_t(unsigned int,
Marek Vasut8b548302019-08-11 13:44:10 +0200646 leni,
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900647 min_t(unsigned int,
Marek Vasut8b548302019-08-11 13:44:10 +0200648 sizeof(root_hub_config_des),
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900649 wLength));
650 memcpy(buffer, root_hub_config_des, len);
651 break;
652 case (0x03): /* string descriptors */
653 if (wValue == 0x0300) {
654 len = min_t(unsigned int,
Marek Vasut8b548302019-08-11 13:44:10 +0200655 leni,
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900656 min_t(unsigned int,
Marek Vasut8b548302019-08-11 13:44:10 +0200657 sizeof(root_hub_str_index0),
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900658 wLength));
659 memcpy(buffer, root_hub_str_index0, len);
660 }
661 if (wValue == 0x0301) {
662 len = min_t(unsigned int,
Marek Vasut8b548302019-08-11 13:44:10 +0200663 leni,
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900664 min_t(unsigned int,
Marek Vasut8b548302019-08-11 13:44:10 +0200665 sizeof(root_hub_str_index1),
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900666 wLength));
667 memcpy(buffer, root_hub_str_index1, len);
668 }
669 break;
670 default:
671 stat = USB_ST_STALLED;
672 }
673 break;
674
675 case RH_GET_DESCRIPTOR | RH_CLASS:
676 {
677 __u32 temp = 0x00000001;
678
679 data[0] = 9; /* min length; */
680 data[1] = 0x29;
681 data[2] = temp & RH_A_NDP;
682 data[3] = 0;
683 if (temp & RH_A_PSM)
684 data[3] |= 0x1;
685 if (temp & RH_A_NOCP)
686 data[3] |= 0x10;
687 else if (temp & RH_A_OCPM)
688 data[3] |= 0x8;
689
690 /* corresponds to data[4-7] */
691 data[5] = (temp & RH_A_POTPGT) >> 24;
692 data[7] = temp & RH_B_DR;
693 if (data[2] < 7) {
694 data[8] = 0xff;
695 } else {
696 data[0] += 2;
697 data[8] = (temp & RH_B_DR) >> 8;
Marek Vasut8b548302019-08-11 13:44:10 +0200698 data[9] = 0xff;
699 data[10] = 0xff;
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900700 }
701
702 len = min_t(unsigned int, leni,
703 min_t(unsigned int, data[0], wLength));
704 memcpy(buffer, data, len);
705 break;
706 }
707
708 case RH_GET_CONFIGURATION:
Marek Vasut8b548302019-08-11 13:44:10 +0200709 *(__u8 *)buffer = 0x01;
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900710 len = 1;
711 break;
712 case RH_SET_CONFIGURATION:
713 break;
714 default:
Nobuhiro Iwamatsue58c41e2008-09-18 20:13:08 +0900715 R8A66597_DPRINT("unsupported root hub command");
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900716 stat = USB_ST_STALLED;
717 }
718
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000719 mdelay(1);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900720
721 len = min_t(int, len, leni);
722
723 dev->act_len = len;
724 dev->status = stat;
725
726 return stat;
727}
728
Marek Vasut8b548302019-08-11 13:44:10 +0200729static int r8a66597_submit_control_msg(struct udevice *udev,
730 struct usb_device *dev,
731 unsigned long pipe, void *buffer,
732 int length, struct devrequest *setup)
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900733{
Marek Vasut7f3858f2019-08-11 12:34:38 +0200734 struct r8a66597 *r8a66597 = dev_get_priv(udev);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900735 u16 r8a66597_address = setup->request == USB_REQ_SET_ADDRESS ?
736 0 : dev->devnum;
737
Marek Vasut7f3858f2019-08-11 12:34:38 +0200738 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
739 udev->name, dev, dev->dev->name, dev->portnr);
740
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900741 R8A66597_DPRINT("%s\n", __func__);
742 if (usb_pipedevice(pipe) == r8a66597->rh_devnum)
Marek Vasut7f3858f2019-08-11 12:34:38 +0200743 return r8a66597_submit_rh_msg(udev, dev, pipe, buffer,
744 length, setup);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900745
746 R8A66597_DPRINT("%s: setup\n", __func__);
Yoshihiro Shimoda60ece6d2008-10-29 20:05:18 +0900747 set_devadd(r8a66597, r8a66597_address, dev, 0);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900748
749 if (send_setup_packet(r8a66597, dev, setup) < 0) {
750 printf("setup packet send error\n");
751 return -1;
752 }
753
Yoshihiro Shimoda60ece6d2008-10-29 20:05:18 +0900754 dev->act_len = 0;
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900755 if (usb_pipein(pipe))
756 if (receive_control_packet(r8a66597, dev, buffer,
Marek Vasut8b548302019-08-11 13:44:10 +0200757 length) < 0)
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900758 return -1;
759
760 if (send_status_packet(r8a66597, pipe) < 0)
761 return -1;
762
763 dev->status = 0;
764
765 return 0;
766}
767
Marek Vasut8b548302019-08-11 13:44:10 +0200768static int r8a66597_submit_bulk_msg(struct udevice *udev,
769 struct usb_device *dev, unsigned long pipe,
770 void *buffer, int length)
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900771{
Marek Vasut7f3858f2019-08-11 12:34:38 +0200772 struct r8a66597 *r8a66597 = dev_get_priv(udev);
773 int ret = 0;
774
775 debug("%s: dev='%s', udev=%p\n", __func__, udev->name, dev);
776
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900777 R8A66597_DPRINT("%s\n", __func__);
Marek Vasut7f3858f2019-08-11 12:34:38 +0200778 R8A66597_DPRINT("pipe = %08x, buffer = %p, len = %d, devnum = %d\n",
779 pipe, buffer, length, dev->devnum);
780
781 set_devadd(r8a66597, dev->devnum, dev, 0);
782
783 pipe_buffer_setting(r8a66597, dev, pipe);
784
785 dev->act_len = 0;
786 while (dev->act_len < length && ret == 0) {
787 if (ctrlc())
788 return -1;
789
790 if (usb_pipein(pipe))
791 ret = receive_bulk_packet(r8a66597, dev, pipe, buffer,
Marek Vasut8b548302019-08-11 13:44:10 +0200792 length);
Marek Vasut7f3858f2019-08-11 12:34:38 +0200793 else
794 ret = send_bulk_packet(r8a66597, dev, pipe, buffer,
Marek Vasut8b548302019-08-11 13:44:10 +0200795 length);
Marek Vasut7f3858f2019-08-11 12:34:38 +0200796 }
797
798 if (ret == 0)
799 dev->status = 0;
800
801 return ret;
802}
803
804static int r8a66597_usb_ofdata_to_platdata(struct udevice *dev)
805{
806 struct r8a66597 *priv = dev_get_priv(dev);
807 fdt_addr_t addr;
808
809 addr = dev_read_addr(dev);
810 if (addr == FDT_ADDR_T_NONE)
811 return -EINVAL;
812 priv->reg = addr;
813
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900814 return 0;
815}
816
Marek Vasut7f3858f2019-08-11 12:34:38 +0200817static int r8a66597_usb_probe(struct udevice *dev)
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900818{
Marek Vasut7f3858f2019-08-11 12:34:38 +0200819 struct r8a66597 *priv = dev_get_priv(dev);
820 struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900821
Marek Vasut7f3858f2019-08-11 12:34:38 +0200822 bus_priv->desc_before_addr = true;
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900823
Marek Vasut7f3858f2019-08-11 12:34:38 +0200824 disable_controller(priv);
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000825 mdelay(100);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900826
Marek Vasut7f3858f2019-08-11 12:34:38 +0200827 enable_controller(priv);
Marek Vasut8b548302019-08-11 13:44:10 +0200828 r8a66597_port_power(priv, 0, 1);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900829
830 /* check usb device */
Marek Vasut7f3858f2019-08-11 12:34:38 +0200831 check_usb_device_connecting(priv);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900832
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000833 mdelay(50);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900834
835 return 0;
836}
837
Marek Vasut7f3858f2019-08-11 12:34:38 +0200838static int r8a66597_usb_remove(struct udevice *dev)
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900839{
Marek Vasut7f3858f2019-08-11 12:34:38 +0200840 struct r8a66597 *priv = dev_get_priv(dev);
841
842 disable_controller(priv);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900843
844 return 0;
845}
Marek Vasut7f3858f2019-08-11 12:34:38 +0200846
847struct dm_usb_ops r8a66597_usb_ops = {
848 .control = r8a66597_submit_control_msg,
849 .bulk = r8a66597_submit_bulk_msg,
850};
851
852static const struct udevice_id r8a66597_usb_ids[] = {
853 { .compatible = "renesas,rza1-usbhs" },
854 { }
855};
856
857U_BOOT_DRIVER(usb_r8a66597) = {
858 .name = "r8a66597_usb",
859 .id = UCLASS_USB,
860 .of_match = r8a66597_usb_ids,
861 .ofdata_to_platdata = r8a66597_usb_ofdata_to_platdata,
862 .probe = r8a66597_usb_probe,
863 .remove = r8a66597_usb_remove,
864 .ops = &r8a66597_usb_ops,
865 .priv_auto_alloc_size = sizeof(struct r8a66597),
866 .flags = DM_FLAG_ALLOC_PRIV_DMA,
867};