blob: d790839fda53c57c9174ff7439ee8dce7e839c3a [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>
Chris Brandtc85f3cc2017-11-29 14:49:21 -050015#include <linux/iopoll.h>
Marek Vasutc1214092019-08-22 17:53:04 +020016#include <power/regulator.h>
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090017
18#include "r8a66597.h"
19
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090020#ifdef R8A66597_DEBUG
21#define R8A66597_DPRINT printf
22#else
23#define R8A66597_DPRINT(...)
24#endif
25
Marek Vasut5e890c52019-08-11 12:34:38 +020026static inline struct usb_device *usb_dev_get_parent(struct usb_device *udev)
27{
28 struct udevice *parent = udev->dev->parent;
29
30 /*
31 * When called from usb-uclass.c: usb_scan_device() udev->dev points
32 * to the parent udevice, not the actual udevice belonging to the
33 * udev as the device is not instantiated yet.
34 *
35 * If dev is an usb-bus, then we are called from usb_scan_device() for
36 * an usb-device plugged directly into the root port, return NULL.
37 */
38 if (device_get_uclass_id(udev->dev) == UCLASS_USB)
39 return NULL;
40
41 /*
42 * If these 2 are not the same we are being called from
43 * usb_scan_device() and udev itself is the parent.
44 */
45 if (dev_get_parent_priv(udev->dev) != udev)
46 return udev;
47
48 /* We are being called normally, use the parent pointer */
49 if (device_get_uclass_id(parent) == UCLASS_USB_HUB)
50 return dev_get_parent_priv(parent);
51
52 return NULL;
53}
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090054
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090055static void get_hub_data(struct usb_device *dev, u16 *hub_devnum, u16 *hubport)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090056{
Marek Vasut5e890c52019-08-11 12:34:38 +020057 struct usb_device *parent = usb_dev_get_parent(dev);
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090058
59 *hub_devnum = 0;
60 *hubport = 0;
61
62 /* check a device connected to root_hub */
Marek Vasut5e890c52019-08-11 12:34:38 +020063 if ((parent && parent->devnum == 1) ||
Marek Vasutb3935f12019-08-11 13:44:10 +020064 dev->devnum == 1)
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090065 return;
66
Marek Vasut5e890c52019-08-11 12:34:38 +020067 *hub_devnum = (u8)parent->devnum;
68 *hubport = parent->portnr - 1;
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090069}
70
71static void set_devadd(struct r8a66597 *r8a66597, u8 r8a66597_address,
Marek Vasutb3935f12019-08-11 13:44:10 +020072 struct usb_device *dev, int port)
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090073{
74 u16 val, usbspd, upphub, hubport;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090075 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
76
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090077 get_hub_data(dev, &upphub, &hubport);
78 usbspd = r8a66597->speed;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090079 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
80 r8a66597_write(r8a66597, val, devadd_reg);
81}
82
83static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
84{
85 u16 tmp;
86 int i = 0;
87
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090088 do {
89 r8a66597_write(r8a66597, USBE, SYSCFG0);
90 tmp = r8a66597_read(r8a66597, SYSCFG0);
91 if (i++ > 1000) {
92 printf("register access fail.\n");
93 return -1;
94 }
95 } while ((tmp & USBE) != USBE);
96 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
Chris Brandtfaecead2017-11-27 14:04:10 -050097 /*
98 * RZ/A Only:
99 * Bits XTAL(UCKSEL) and UPLLE in SYSCFG0 for USB0 controls both USB0
100 * and USB1, so we must always set the USB0 register
101 */
102#if (CONFIG_R8A66597_XTAL == 1)
Marek Vasut2c11a3c2019-08-11 12:34:38 +0200103 r8a66597_bset(r8a66597, XTAL, SYSCFG0);
Chris Brandtfaecead2017-11-27 14:04:10 -0500104#endif
105 mdelay(1);
Marek Vasut2c11a3c2019-08-11 12:34:38 +0200106 r8a66597_bset(r8a66597, UPLLE, SYSCFG0);
Chris Brandtfaecead2017-11-27 14:04:10 -0500107 mdelay(1);
108 r8a66597_bset(r8a66597, SUSPM, SUSPMODE0);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900109
110 return 0;
111}
112
113static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
114{
Chris Brandtfaecead2017-11-27 14:04:10 -0500115 r8a66597_bclr(r8a66597, SUSPM, SUSPMODE0);
116
Marek Vasut2c11a3c2019-08-11 12:34:38 +0200117 r8a66597_bclr(r8a66597, UPLLE, SYSCFG0);
Chris Brandtfaecead2017-11-27 14:04:10 -0500118 mdelay(1);
119 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
120 mdelay(1);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900121}
122
123static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
124{
125 u16 val;
126
127 val = port ? DRPD : DCFM | DRPD;
128 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
129 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900130}
131
132static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
133{
134 u16 val, tmp;
135
136 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
137 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
138
139 r8a66597_port_power(r8a66597, port, 0);
140
141 do {
142 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
143 udelay(640);
144 } while (tmp == EDGESTS);
145
146 val = port ? DRPD : DCFM | DRPD;
147 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
148 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
149}
150
151static int enable_controller(struct r8a66597 *r8a66597)
152{
153 int ret, port;
154
155 ret = r8a66597_clock_enable(r8a66597);
156 if (ret < 0)
157 return ret;
158
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900159 r8a66597_bset(r8a66597, USBE, SYSCFG0);
160
161 r8a66597_bset(r8a66597, INTL, SOFCFG);
162 r8a66597_write(r8a66597, 0, INTENB0);
yasuhisa umanoea33c6a2014-04-18 11:33:08 +0900163 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
164 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900165
Marek Vasutcf8a6272019-08-11 13:23:11 +0200166 r8a66597_bclr(r8a66597, BIGEND, CFIFOSEL);
167 r8a66597_bclr(r8a66597, BIGEND, D0FIFOSEL);
168 r8a66597_bclr(r8a66597, BIGEND, D1FIFOSEL);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900169 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
170
171 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
172 r8a66597_enable_port(r8a66597, port);
173
174 return 0;
175}
176
177static void disable_controller(struct r8a66597 *r8a66597)
178{
179 int i;
180
181 if (!(r8a66597_read(r8a66597, SYSCFG0) & USBE))
182 return;
183
184 r8a66597_write(r8a66597, 0, INTENB0);
185 r8a66597_write(r8a66597, 0, INTSTS0);
186
187 r8a66597_write(r8a66597, 0, D0FIFOSEL);
188 r8a66597_write(r8a66597, 0, D1FIFOSEL);
189 r8a66597_write(r8a66597, 0, DCPCFG);
190 r8a66597_write(r8a66597, 0x40, DCPMAXP);
191 r8a66597_write(r8a66597, 0, DCPCTR);
192
193 for (i = 0; i <= 10; i++)
194 r8a66597_write(r8a66597, 0, get_devadd_addr(i));
195 for (i = 1; i <= 5; i++) {
196 r8a66597_write(r8a66597, 0, get_pipetre_addr(i));
197 r8a66597_write(r8a66597, 0, get_pipetrn_addr(i));
198 }
199 for (i = 1; i < R8A66597_MAX_NUM_PIPE; i++) {
200 r8a66597_write(r8a66597, 0, get_pipectr_addr(i));
201 r8a66597_write(r8a66597, i, PIPESEL);
202 r8a66597_write(r8a66597, 0, PIPECFG);
203 r8a66597_write(r8a66597, 0, PIPEBUF);
204 r8a66597_write(r8a66597, 0, PIPEMAXP);
205 r8a66597_write(r8a66597, 0, PIPEPERI);
206 }
207
208 for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++)
209 r8a66597_disable_port(r8a66597, i);
210
211 r8a66597_clock_disable(r8a66597);
212}
213
214static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
215 u16 mask, u16 loop)
216{
217 u16 tmp;
218 int i = 0;
219
220 do {
221 tmp = r8a66597_read(r8a66597, reg);
222 if (i++ > 1000000) {
223 printf("register%lx, loop %x is timeout\n", reg, loop);
224 break;
225 }
226 } while ((tmp & mask) != loop);
227}
228
229static void pipe_buffer_setting(struct r8a66597 *r8a66597,
230 struct usb_device *dev, unsigned long pipe)
231{
232 u16 val = 0;
233 u16 pipenum, bufnum, maxpacket;
234
235 if (usb_pipein(pipe)) {
236 pipenum = BULK_IN_PIPENUM;
237 bufnum = BULK_IN_BUFNUM;
238 maxpacket = dev->epmaxpacketin[usb_pipeendpoint(pipe)];
239 } else {
240 pipenum = BULK_OUT_PIPENUM;
241 bufnum = BULK_OUT_BUFNUM;
242 maxpacket = dev->epmaxpacketout[usb_pipeendpoint(pipe)];
243 }
244
245 if (r8a66597->pipe_config & (1 << pipenum))
246 return;
247 r8a66597->pipe_config |= (1 << pipenum);
248
249 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(pipenum));
250 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(pipenum));
251 r8a66597_write(r8a66597, pipenum, PIPESEL);
252
253 /* FIXME: This driver support bulk transfer only. */
254 if (!usb_pipein(pipe))
255 val |= R8A66597_DIR;
256 else
257 val |= R8A66597_SHTNAK;
258 val |= R8A66597_BULK | R8A66597_DBLB | usb_pipeendpoint(pipe);
259 r8a66597_write(r8a66597, val, PIPECFG);
260
261 r8a66597_write(r8a66597, (8 << 10) | bufnum, PIPEBUF);
262 r8a66597_write(r8a66597, make_devsel(usb_pipedevice(pipe)) |
263 maxpacket, PIPEMAXP);
264 r8a66597_write(r8a66597, 0, PIPEPERI);
265 r8a66597_write(r8a66597, SQCLR, get_pipectr_addr(pipenum));
266}
267
268static int send_setup_packet(struct r8a66597 *r8a66597, struct usb_device *dev,
269 struct devrequest *setup)
270{
271 int i;
272 unsigned short *p = (unsigned short *)setup;
273 unsigned long setup_addr = USBREQ;
274 u16 intsts1;
275 int timeout = 3000;
Chris Brandtfaecead2017-11-27 14:04:10 -0500276 u16 dcpctr;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900277 u16 devsel = setup->request == USB_REQ_SET_ADDRESS ? 0 : dev->devnum;
278
279 r8a66597_write(r8a66597, make_devsel(devsel) |
280 (8 << dev->maxpacketsize), DCPMAXP);
281 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
282
Chris Brandtfaecead2017-11-27 14:04:10 -0500283 dcpctr = r8a66597_read(r8a66597, DCPCTR);
284 if ((dcpctr & PID) == PID_BUF) {
Chris Brandtc85f3cc2017-11-29 14:49:21 -0500285 if (readw_poll_timeout(r8a66597->reg + DCPCTR, dcpctr,
286 dcpctr & BSTS, 1000) < 0) {
287 printf("DCPCTR BSTS timeout!\n");
288 return -ETIMEDOUT;
Chris Brandtfaecead2017-11-27 14:04:10 -0500289 }
290 }
Chris Brandtfaecead2017-11-27 14:04:10 -0500291
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900292 for (i = 0; i < 4; i++) {
293 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
294 setup_addr += 2;
295 }
296 r8a66597_write(r8a66597, ~0x0001, BRDYSTS);
297 r8a66597_write(r8a66597, SUREQ, DCPCTR);
298
299 while (1) {
300 intsts1 = r8a66597_read(r8a66597, INTSTS1);
301 if (intsts1 & SACK)
302 break;
303 if (intsts1 & SIGN) {
304 printf("setup packet send error\n");
305 return -1;
306 }
307 if (timeout-- < 0) {
308 printf("setup packet timeout\n");
309 return -1;
310 }
311 udelay(500);
312 }
313
314 return 0;
315}
316
317static int send_bulk_packet(struct r8a66597 *r8a66597, struct usb_device *dev,
318 unsigned long pipe, void *buffer, int transfer_len)
319{
320 u16 tmp, bufsize;
321 u16 *buf;
322 size_t size;
323
324 R8A66597_DPRINT("%s\n", __func__);
325
326 r8a66597_mdfy(r8a66597, MBW | BULK_OUT_PIPENUM,
Marek Vasutb3935f12019-08-11 13:44:10 +0200327 MBW | CURPIPE, CFIFOSEL);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900328 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, BULK_OUT_PIPENUM);
329 tmp = r8a66597_read(r8a66597, CFIFOCTR);
330 if ((tmp & FRDY) == 0) {
331 printf("%s FRDY is not set (%x)\n", __func__, tmp);
332 return -1;
333 }
334
335 /* prepare parameters */
336 bufsize = dev->epmaxpacketout[usb_pipeendpoint(pipe)];
337 buf = (u16 *)(buffer + dev->act_len);
338 size = min((int)bufsize, transfer_len - dev->act_len);
339
340 /* write fifo */
341 r8a66597_write(r8a66597, ~(1 << BULK_OUT_PIPENUM), BEMPSTS);
342 if (buffer) {
343 r8a66597_write_fifo(r8a66597, CFIFO, buf, size);
344 r8a66597_write(r8a66597, BVAL, CFIFOCTR);
345 }
346
347 /* update parameters */
348 dev->act_len += size;
349
350 r8a66597_mdfy(r8a66597, PID_BUF, PID,
Marek Vasutb3935f12019-08-11 13:44:10 +0200351 get_pipectr_addr(BULK_OUT_PIPENUM));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900352
353 while (!(r8a66597_read(r8a66597, BEMPSTS) & (1 << BULK_OUT_PIPENUM)))
354 if (ctrlc())
355 return -1;
356 r8a66597_write(r8a66597, ~(1 << BULK_OUT_PIPENUM), BEMPSTS);
357
358 if (dev->act_len >= transfer_len)
359 r8a66597_mdfy(r8a66597, PID_NAK, PID,
Marek Vasutb3935f12019-08-11 13:44:10 +0200360 get_pipectr_addr(BULK_OUT_PIPENUM));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900361
362 return 0;
363}
364
365static int receive_bulk_packet(struct r8a66597 *r8a66597,
366 struct usb_device *dev,
367 unsigned long pipe,
368 void *buffer, int transfer_len)
369{
370 u16 tmp;
371 u16 *buf;
372 const u16 pipenum = BULK_IN_PIPENUM;
373 int rcv_len;
374 int maxpacket = dev->epmaxpacketin[usb_pipeendpoint(pipe)];
375
376 R8A66597_DPRINT("%s\n", __func__);
377
378 /* prepare */
379 if (dev->act_len == 0) {
380 r8a66597_mdfy(r8a66597, PID_NAK, PID,
Marek Vasutb3935f12019-08-11 13:44:10 +0200381 get_pipectr_addr(pipenum));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900382 r8a66597_write(r8a66597, ~(1 << pipenum), BRDYSTS);
383
384 r8a66597_write(r8a66597, TRCLR, get_pipetre_addr(pipenum));
385 r8a66597_write(r8a66597,
Marek Vasutb3935f12019-08-11 13:44:10 +0200386 (transfer_len + maxpacket - 1) / maxpacket,
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900387 get_pipetrn_addr(pipenum));
388 r8a66597_bset(r8a66597, TRENB, get_pipetre_addr(pipenum));
389
390 r8a66597_mdfy(r8a66597, PID_BUF, PID,
Marek Vasutb3935f12019-08-11 13:44:10 +0200391 get_pipectr_addr(pipenum));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900392 }
393
394 r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
395 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
396
397 while (!(r8a66597_read(r8a66597, BRDYSTS) & (1 << pipenum)))
398 if (ctrlc())
399 return -1;
400 r8a66597_write(r8a66597, ~(1 << pipenum), BRDYSTS);
401
402 tmp = r8a66597_read(r8a66597, CFIFOCTR);
403 if ((tmp & FRDY) == 0) {
404 printf("%s FRDY is not set. (%x)\n", __func__, tmp);
405 return -1;
406 }
407
408 buf = (u16 *)(buffer + dev->act_len);
409 rcv_len = tmp & DTLN;
410 dev->act_len += rcv_len;
411
412 if (buffer) {
413 if (rcv_len == 0)
414 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
415 else
416 r8a66597_read_fifo(r8a66597, CFIFO, buf, rcv_len);
417 }
418
419 return 0;
420}
421
422static int receive_control_packet(struct r8a66597 *r8a66597,
423 struct usb_device *dev,
424 void *buffer, int transfer_len)
425{
426 u16 tmp;
427 int rcv_len;
428
429 /* FIXME: limit transfer size : 64byte or less */
430
431 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
432 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
433 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
434 r8a66597_bset(r8a66597, SQSET, DCPCTR);
435 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
436 r8a66597_mdfy(r8a66597, PID_BUF, PID, DCPCTR);
437
438 while (!(r8a66597_read(r8a66597, BRDYSTS) & 0x0001))
439 if (ctrlc())
440 return -1;
441 r8a66597_write(r8a66597, ~0x0001, BRDYSTS);
442
443 r8a66597_mdfy(r8a66597, MBW, MBW | CURPIPE, CFIFOSEL);
444 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
445
446 tmp = r8a66597_read(r8a66597, CFIFOCTR);
447 if ((tmp & FRDY) == 0) {
448 printf("%s FRDY is not set. (%x)\n", __func__, tmp);
449 return -1;
450 }
451
452 rcv_len = tmp & DTLN;
453 dev->act_len += rcv_len;
454
455 r8a66597_mdfy(r8a66597, PID_NAK, PID, DCPCTR);
456
457 if (buffer) {
458 if (rcv_len == 0)
459 r8a66597_write(r8a66597, BCLR, DCPCTR);
460 else
461 r8a66597_read_fifo(r8a66597, CFIFO, buffer, rcv_len);
462 }
463
464 return 0;
465}
466
467static int send_status_packet(struct r8a66597 *r8a66597,
Marek Vasutb3935f12019-08-11 13:44:10 +0200468 unsigned long pipe)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900469{
470 r8a66597_bset(r8a66597, SQSET, DCPCTR);
471 r8a66597_mdfy(r8a66597, PID_NAK, PID, DCPCTR);
472
473 if (usb_pipein(pipe)) {
474 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
475 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
476 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
477 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
478 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
479 } else {
480 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
481 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
482 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
483 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
484 }
485 r8a66597_mdfy(r8a66597, PID_BUF, PID, DCPCTR);
486
487 while (!(r8a66597_read(r8a66597, BEMPSTS) & 0x0001))
488 if (ctrlc())
489 return -1;
490
491 return 0;
492}
493
494static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port)
495{
496 int count = R8A66597_MAX_SAMPLING;
497 unsigned short syssts, old_syssts;
498
499 R8A66597_DPRINT("%s\n", __func__);
500
501 old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port) & LNST);
502 while (count > 0) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000503 mdelay(R8A66597_RH_POLL_TIME);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900504
505 syssts = r8a66597_read(r8a66597, get_syssts_reg(port) & LNST);
506 if (syssts == old_syssts) {
507 count--;
508 } else {
509 count = R8A66597_MAX_SAMPLING;
510 old_syssts = syssts;
511 }
512 }
513}
514
515static void r8a66597_bus_reset(struct r8a66597 *r8a66597, int port)
516{
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000517 mdelay(10);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900518 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT, get_dvstctr_reg(port));
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000519 mdelay(50);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900520 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT, get_dvstctr_reg(port));
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000521 mdelay(50);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900522}
523
524static int check_usb_device_connecting(struct r8a66597 *r8a66597)
525{
526 int timeout = 10000; /* 100usec * 10000 = 1sec */
527 int i;
528
529 for (i = 0; i < 5; i++) {
530 /* check a usb cable connect */
531 while (!(r8a66597_read(r8a66597, INTSTS1) & ATTCH)) {
532 if (timeout-- < 0) {
533 printf("%s timeout.\n", __func__);
534 return -1;
535 }
536 udelay(100);
537 }
538
539 /* check a data line */
540 r8a66597_check_syssts(r8a66597, 0);
541
542 r8a66597_bus_reset(r8a66597, 0);
543 r8a66597->speed = get_rh_usb_speed(r8a66597, 0);
544
545 if (!(r8a66597_read(r8a66597, INTSTS1) & DTCH)) {
546 r8a66597->port_change = USB_PORT_STAT_C_CONNECTION;
547 r8a66597->port_status = USB_PORT_STAT_CONNECTION |
548 USB_PORT_STAT_ENABLE;
549 return 0; /* success */
550 }
551
552 R8A66597_DPRINT("USB device has detached. retry = %d\n", i);
553 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
554 }
555
556 return -1; /* fail */
557}
558
Marek Vasutb3935f12019-08-11 13:44:10 +0200559/* Virtual Root Hub */
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900560
Stephen Warren39c89682014-02-13 21:15:18 -0700561#include <usbroothubdes.h>
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900562
Marek Vasut5e890c52019-08-11 12:34:38 +0200563static int r8a66597_submit_rh_msg(struct udevice *udev, struct usb_device *dev,
564 unsigned long pipe, void *buffer,
565 int transfer_len, struct devrequest *cmd)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900566{
Marek Vasut5e890c52019-08-11 12:34:38 +0200567 struct r8a66597 *r8a66597 = dev_get_priv(udev);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900568 int leni = transfer_len;
569 int len = 0;
570 int stat = 0;
571 __u16 bmRType_bReq;
572 __u16 wValue;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900573 __u16 wLength;
574 unsigned char data[32];
575
576 R8A66597_DPRINT("%s\n", __func__);
577
Remy Bohmerd8c55ab2008-10-10 10:23:22 +0200578 if (usb_pipeint(pipe)) {
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900579 printf("Root-Hub submit IRQ: NOT implemented");
580 return 0;
581 }
582
583 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
584 wValue = cpu_to_le16 (cmd->value);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900585 wLength = cpu_to_le16 (cmd->length);
586
587 switch (bmRType_bReq) {
588 case RH_GET_STATUS:
589 *(__u16 *)buffer = cpu_to_le16(1);
590 len = 2;
591 break;
592 case RH_GET_STATUS | RH_INTERFACE:
593 *(__u16 *)buffer = cpu_to_le16(0);
594 len = 2;
595 break;
596 case RH_GET_STATUS | RH_ENDPOINT:
597 *(__u16 *)buffer = cpu_to_le16(0);
598 len = 2;
599 break;
600 case RH_GET_STATUS | RH_CLASS:
601 *(__u32 *)buffer = cpu_to_le32(0);
602 len = 4;
603 break;
604 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
605 *(__u32 *)buffer = cpu_to_le32(r8a66597->port_status |
606 (r8a66597->port_change << 16));
607 len = 4;
608 break;
609 case RH_CLEAR_FEATURE | RH_ENDPOINT:
610 case RH_CLEAR_FEATURE | RH_CLASS:
611 break;
612
613 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
614 switch (wValue) {
615 case RH_C_PORT_CONNECTION:
616 r8a66597->port_change &= ~USB_PORT_STAT_C_CONNECTION;
617 break;
618 }
619 break;
620
621 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
622 switch (wValue) {
623 case (RH_PORT_SUSPEND):
624 break;
625 case (RH_PORT_RESET):
626 r8a66597_bus_reset(r8a66597, 0);
627 break;
628 case (RH_PORT_POWER):
629 break;
630 case (RH_PORT_ENABLE):
631 break;
632 }
633 break;
634 case RH_SET_ADDRESS:
Marek Vasut5e890c52019-08-11 12:34:38 +0200635 r8a66597->rh_devnum = wValue;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900636 break;
637 case RH_GET_DESCRIPTOR:
638 switch ((wValue & 0xff00) >> 8) {
639 case (0x01): /* device descriptor */
640 len = min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200641 leni,
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900642 min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200643 sizeof(root_hub_dev_des),
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900644 wLength));
645 memcpy(buffer, root_hub_dev_des, len);
646 break;
647 case (0x02): /* configuration descriptor */
648 len = min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200649 leni,
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900650 min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200651 sizeof(root_hub_config_des),
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900652 wLength));
653 memcpy(buffer, root_hub_config_des, len);
654 break;
655 case (0x03): /* string descriptors */
656 if (wValue == 0x0300) {
657 len = min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200658 leni,
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900659 min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200660 sizeof(root_hub_str_index0),
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900661 wLength));
662 memcpy(buffer, root_hub_str_index0, len);
663 }
664 if (wValue == 0x0301) {
665 len = min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200666 leni,
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900667 min_t(unsigned int,
Marek Vasutb3935f12019-08-11 13:44:10 +0200668 sizeof(root_hub_str_index1),
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900669 wLength));
670 memcpy(buffer, root_hub_str_index1, len);
671 }
672 break;
673 default:
674 stat = USB_ST_STALLED;
675 }
676 break;
677
678 case RH_GET_DESCRIPTOR | RH_CLASS:
679 {
680 __u32 temp = 0x00000001;
681
682 data[0] = 9; /* min length; */
683 data[1] = 0x29;
684 data[2] = temp & RH_A_NDP;
685 data[3] = 0;
686 if (temp & RH_A_PSM)
687 data[3] |= 0x1;
688 if (temp & RH_A_NOCP)
689 data[3] |= 0x10;
690 else if (temp & RH_A_OCPM)
691 data[3] |= 0x8;
692
693 /* corresponds to data[4-7] */
694 data[5] = (temp & RH_A_POTPGT) >> 24;
695 data[7] = temp & RH_B_DR;
696 if (data[2] < 7) {
697 data[8] = 0xff;
698 } else {
699 data[0] += 2;
700 data[8] = (temp & RH_B_DR) >> 8;
Marek Vasutb3935f12019-08-11 13:44:10 +0200701 data[9] = 0xff;
702 data[10] = 0xff;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900703 }
704
705 len = min_t(unsigned int, leni,
706 min_t(unsigned int, data[0], wLength));
707 memcpy(buffer, data, len);
708 break;
709 }
710
711 case RH_GET_CONFIGURATION:
Marek Vasutb3935f12019-08-11 13:44:10 +0200712 *(__u8 *)buffer = 0x01;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900713 len = 1;
714 break;
715 case RH_SET_CONFIGURATION:
716 break;
717 default:
Nobuhiro Iwamatsu9d58a012008-09-18 20:13:08 +0900718 R8A66597_DPRINT("unsupported root hub command");
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900719 stat = USB_ST_STALLED;
720 }
721
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000722 mdelay(1);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900723
724 len = min_t(int, len, leni);
725
726 dev->act_len = len;
727 dev->status = stat;
728
729 return stat;
730}
731
Marek Vasutb3935f12019-08-11 13:44:10 +0200732static int r8a66597_submit_control_msg(struct udevice *udev,
733 struct usb_device *dev,
734 unsigned long pipe, void *buffer,
735 int length, struct devrequest *setup)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900736{
Marek Vasut5e890c52019-08-11 12:34:38 +0200737 struct r8a66597 *r8a66597 = dev_get_priv(udev);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900738 u16 r8a66597_address = setup->request == USB_REQ_SET_ADDRESS ?
739 0 : dev->devnum;
740
Marek Vasut5e890c52019-08-11 12:34:38 +0200741 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
742 udev->name, dev, dev->dev->name, dev->portnr);
743
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900744 R8A66597_DPRINT("%s\n", __func__);
745 if (usb_pipedevice(pipe) == r8a66597->rh_devnum)
Marek Vasut5e890c52019-08-11 12:34:38 +0200746 return r8a66597_submit_rh_msg(udev, dev, pipe, buffer,
747 length, setup);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900748
749 R8A66597_DPRINT("%s: setup\n", __func__);
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +0900750 set_devadd(r8a66597, r8a66597_address, dev, 0);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900751
752 if (send_setup_packet(r8a66597, dev, setup) < 0) {
753 printf("setup packet send error\n");
754 return -1;
755 }
756
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +0900757 dev->act_len = 0;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900758 if (usb_pipein(pipe))
759 if (receive_control_packet(r8a66597, dev, buffer,
Marek Vasutb3935f12019-08-11 13:44:10 +0200760 length) < 0)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900761 return -1;
762
763 if (send_status_packet(r8a66597, pipe) < 0)
764 return -1;
765
766 dev->status = 0;
767
768 return 0;
769}
770
Marek Vasutb3935f12019-08-11 13:44:10 +0200771static int r8a66597_submit_bulk_msg(struct udevice *udev,
772 struct usb_device *dev, unsigned long pipe,
773 void *buffer, int length)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900774{
Marek Vasut5e890c52019-08-11 12:34:38 +0200775 struct r8a66597 *r8a66597 = dev_get_priv(udev);
776 int ret = 0;
777
778 debug("%s: dev='%s', udev=%p\n", __func__, udev->name, dev);
779
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900780 R8A66597_DPRINT("%s\n", __func__);
Marek Vasut5e890c52019-08-11 12:34:38 +0200781 R8A66597_DPRINT("pipe = %08x, buffer = %p, len = %d, devnum = %d\n",
782 pipe, buffer, length, dev->devnum);
783
784 set_devadd(r8a66597, dev->devnum, dev, 0);
785
786 pipe_buffer_setting(r8a66597, dev, pipe);
787
788 dev->act_len = 0;
789 while (dev->act_len < length && ret == 0) {
790 if (ctrlc())
791 return -1;
792
793 if (usb_pipein(pipe))
794 ret = receive_bulk_packet(r8a66597, dev, pipe, buffer,
Marek Vasutb3935f12019-08-11 13:44:10 +0200795 length);
Marek Vasut5e890c52019-08-11 12:34:38 +0200796 else
797 ret = send_bulk_packet(r8a66597, dev, pipe, buffer,
Marek Vasutb3935f12019-08-11 13:44:10 +0200798 length);
Marek Vasut5e890c52019-08-11 12:34:38 +0200799 }
800
801 if (ret == 0)
802 dev->status = 0;
803
804 return ret;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900805}
806
Marek Vasut5e890c52019-08-11 12:34:38 +0200807static int r8a66597_usb_ofdata_to_platdata(struct udevice *dev)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900808{
Marek Vasut5e890c52019-08-11 12:34:38 +0200809 struct r8a66597 *priv = dev_get_priv(dev);
810 fdt_addr_t addr;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900811
Marek Vasut5e890c52019-08-11 12:34:38 +0200812 addr = dev_read_addr(dev);
813 if (addr == FDT_ADDR_T_NONE)
814 return -EINVAL;
815 priv->reg = addr;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900816
Marek Vasut5e890c52019-08-11 12:34:38 +0200817 return 0;
818}
819
820static int r8a66597_usb_probe(struct udevice *dev)
821{
822 struct r8a66597 *priv = dev_get_priv(dev);
823 struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
Marek Vasutc1214092019-08-22 17:53:04 +0200824 int ret;
Marek Vasut5e890c52019-08-11 12:34:38 +0200825
826 bus_priv->desc_before_addr = true;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900827
Marek Vasutc1214092019-08-22 17:53:04 +0200828 if (CONFIG_IS_ENABLED(DM_REGULATOR)) {
829 ret = device_get_supply_regulator(dev, "vbus-supply",
830 &priv->vbus_supply);
831 if (ret) {
832 dev_err(dev,
833 "can't get VBUS supply\n");
834 return ret;
835 }
836
837 ret = regulator_set_enable(priv->vbus_supply, true);
838 if (ret) {
839 dev_err(dev,
840 "can't enable VBUS supply\n");
841 return ret;
842 }
843 }
844
Marek Vasut5e890c52019-08-11 12:34:38 +0200845 disable_controller(priv);
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000846 mdelay(100);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900847
Marek Vasut5e890c52019-08-11 12:34:38 +0200848 enable_controller(priv);
Marek Vasutb3935f12019-08-11 13:44:10 +0200849 r8a66597_port_power(priv, 0, 1);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900850
851 /* check usb device */
Marek Vasut5e890c52019-08-11 12:34:38 +0200852 check_usb_device_connecting(priv);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900853
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000854 mdelay(50);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900855
856 return 0;
857}
858
Marek Vasut5e890c52019-08-11 12:34:38 +0200859static int r8a66597_usb_remove(struct udevice *dev)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900860{
Marek Vasut5e890c52019-08-11 12:34:38 +0200861 struct r8a66597 *priv = dev_get_priv(dev);
Marek Vasutc1214092019-08-22 17:53:04 +0200862 int ret;
Marek Vasut5e890c52019-08-11 12:34:38 +0200863
864 disable_controller(priv);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900865
Marek Vasutc1214092019-08-22 17:53:04 +0200866 if (CONFIG_IS_ENABLED(DM_REGULATOR)) {
867 ret = regulator_set_enable(priv->vbus_supply, false);
868 if (ret) {
869 dev_err(dev,
870 "can't disable VBUS supply\n");
871 return ret;
872 }
873 }
874
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900875 return 0;
876}
Marek Vasut5e890c52019-08-11 12:34:38 +0200877
878struct dm_usb_ops r8a66597_usb_ops = {
879 .control = r8a66597_submit_control_msg,
880 .bulk = r8a66597_submit_bulk_msg,
881};
882
883static const struct udevice_id r8a66597_usb_ids[] = {
884 { .compatible = "renesas,rza1-usbhs" },
885 { }
886};
887
888U_BOOT_DRIVER(usb_r8a66597) = {
889 .name = "r8a66597_usb",
890 .id = UCLASS_USB,
891 .of_match = r8a66597_usb_ids,
892 .ofdata_to_platdata = r8a66597_usb_ofdata_to_platdata,
893 .probe = r8a66597_usb_probe,
894 .remove = r8a66597_usb_remove,
895 .ops = &r8a66597_usb_ops,
896 .priv_auto_alloc_size = sizeof(struct r8a66597),
897 .flags = DM_FLAG_ALLOC_PRIV_DMA,
898};