blob: 3ccbc16da379f1ff33dd43ea47e87c2565a3b2d2 [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>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +090012#include <usb.h>
13#include <asm/io.h>
Simon Glass336d4612020-02-03 07:36:16 -070014#include <dm/device_compat.h>
Simon Glassc05ed002020-05-10 11:40:11 -060015#include <linux/delay.h>
Chris Brandt243fd642017-11-29 14:49:21 -050016#include <linux/iopoll.h>
Rui Miguel Silvab2d2b782022-06-29 11:06:14 +010017#include <linux/usb/usb_urb_compat.h>
Marek Vasut769a9cd2019-08-22 17:53:04 +020018#include <power/regulator.h>
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +090019
20#include "r8a66597.h"
21
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +090022#ifdef R8A66597_DEBUG
23#define R8A66597_DPRINT printf
24#else
25#define R8A66597_DPRINT(...)
26#endif
27
Yoshihiro Shimoda60ece6d2008-10-29 20:05:18 +090028static void get_hub_data(struct usb_device *dev, u16 *hub_devnum, u16 *hubport)
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +090029{
Marek Vasut7f3858f2019-08-11 12:34:38 +020030 struct usb_device *parent = usb_dev_get_parent(dev);
Yoshihiro Shimoda60ece6d2008-10-29 20:05:18 +090031
32 *hub_devnum = 0;
33 *hubport = 0;
34
35 /* check a device connected to root_hub */
Marek Vasut7f3858f2019-08-11 12:34:38 +020036 if ((parent && parent->devnum == 1) ||
Marek Vasut8b548302019-08-11 13:44:10 +020037 dev->devnum == 1)
Yoshihiro Shimoda60ece6d2008-10-29 20:05:18 +090038 return;
39
Marek Vasut7f3858f2019-08-11 12:34:38 +020040 *hub_devnum = (u8)parent->devnum;
41 *hubport = parent->portnr - 1;
Yoshihiro Shimoda60ece6d2008-10-29 20:05:18 +090042}
43
44static void set_devadd(struct r8a66597 *r8a66597, u8 r8a66597_address,
Marek Vasut8b548302019-08-11 13:44:10 +020045 struct usb_device *dev, int port)
Yoshihiro Shimoda60ece6d2008-10-29 20:05:18 +090046{
47 u16 val, usbspd, upphub, hubport;
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +090048 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
49
Yoshihiro Shimoda60ece6d2008-10-29 20:05:18 +090050 get_hub_data(dev, &upphub, &hubport);
51 usbspd = r8a66597->speed;
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +090052 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
53 r8a66597_write(r8a66597, val, devadd_reg);
54}
55
56static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
57{
58 u16 tmp;
59 int i = 0;
60
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +090061 do {
62 r8a66597_write(r8a66597, USBE, SYSCFG0);
63 tmp = r8a66597_read(r8a66597, SYSCFG0);
64 if (i++ > 1000) {
65 printf("register access fail.\n");
66 return -1;
67 }
68 } while ((tmp & USBE) != USBE);
69 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
Chris Brandt11f46782017-11-27 14:04:10 -050070 /*
71 * RZ/A Only:
72 * Bits XTAL(UCKSEL) and UPLLE in SYSCFG0 for USB0 controls both USB0
73 * and USB1, so we must always set the USB0 register
74 */
75#if (CONFIG_R8A66597_XTAL == 1)
Marek Vasut1eb381a2019-08-11 12:34:38 +020076 r8a66597_bset(r8a66597, XTAL, SYSCFG0);
Chris Brandt11f46782017-11-27 14:04:10 -050077#endif
78 mdelay(1);
Marek Vasut1eb381a2019-08-11 12:34:38 +020079 r8a66597_bset(r8a66597, UPLLE, SYSCFG0);
Chris Brandt11f46782017-11-27 14:04:10 -050080 mdelay(1);
81 r8a66597_bset(r8a66597, SUSPM, SUSPMODE0);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +090082
83 return 0;
84}
85
86static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
87{
Chris Brandt11f46782017-11-27 14:04:10 -050088 r8a66597_bclr(r8a66597, SUSPM, SUSPMODE0);
89
Marek Vasut1eb381a2019-08-11 12:34:38 +020090 r8a66597_bclr(r8a66597, UPLLE, SYSCFG0);
Chris Brandt11f46782017-11-27 14:04:10 -050091 mdelay(1);
92 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
93 mdelay(1);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +090094}
95
96static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
97{
98 u16 val;
99
100 val = port ? DRPD : DCFM | DRPD;
101 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
102 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900103}
104
105static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
106{
107 u16 val, tmp;
108
109 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
110 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
111
112 r8a66597_port_power(r8a66597, port, 0);
113
114 do {
115 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
116 udelay(640);
117 } while (tmp == EDGESTS);
118
119 val = port ? DRPD : DCFM | DRPD;
120 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
121 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
122}
123
124static int enable_controller(struct r8a66597 *r8a66597)
125{
126 int ret, port;
127
128 ret = r8a66597_clock_enable(r8a66597);
129 if (ret < 0)
130 return ret;
131
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900132 r8a66597_bset(r8a66597, USBE, SYSCFG0);
133
134 r8a66597_bset(r8a66597, INTL, SOFCFG);
135 r8a66597_write(r8a66597, 0, INTENB0);
yasuhisa umano8ecdce72014-04-18 11:33:08 +0900136 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
137 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900138
Marek Vasut81644e02019-08-11 13:23:11 +0200139 r8a66597_bclr(r8a66597, BIGEND, CFIFOSEL);
140 r8a66597_bclr(r8a66597, BIGEND, D0FIFOSEL);
141 r8a66597_bclr(r8a66597, BIGEND, D1FIFOSEL);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900142 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
143
144 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
145 r8a66597_enable_port(r8a66597, port);
146
147 return 0;
148}
149
150static void disable_controller(struct r8a66597 *r8a66597)
151{
152 int i;
153
154 if (!(r8a66597_read(r8a66597, SYSCFG0) & USBE))
155 return;
156
157 r8a66597_write(r8a66597, 0, INTENB0);
158 r8a66597_write(r8a66597, 0, INTSTS0);
159
160 r8a66597_write(r8a66597, 0, D0FIFOSEL);
161 r8a66597_write(r8a66597, 0, D1FIFOSEL);
162 r8a66597_write(r8a66597, 0, DCPCFG);
163 r8a66597_write(r8a66597, 0x40, DCPMAXP);
164 r8a66597_write(r8a66597, 0, DCPCTR);
165
166 for (i = 0; i <= 10; i++)
167 r8a66597_write(r8a66597, 0, get_devadd_addr(i));
168 for (i = 1; i <= 5; i++) {
169 r8a66597_write(r8a66597, 0, get_pipetre_addr(i));
170 r8a66597_write(r8a66597, 0, get_pipetrn_addr(i));
171 }
172 for (i = 1; i < R8A66597_MAX_NUM_PIPE; i++) {
173 r8a66597_write(r8a66597, 0, get_pipectr_addr(i));
174 r8a66597_write(r8a66597, i, PIPESEL);
175 r8a66597_write(r8a66597, 0, PIPECFG);
176 r8a66597_write(r8a66597, 0, PIPEBUF);
177 r8a66597_write(r8a66597, 0, PIPEMAXP);
178 r8a66597_write(r8a66597, 0, PIPEPERI);
179 }
180
181 for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++)
182 r8a66597_disable_port(r8a66597, i);
183
184 r8a66597_clock_disable(r8a66597);
185}
186
187static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
188 u16 mask, u16 loop)
189{
190 u16 tmp;
191 int i = 0;
192
193 do {
194 tmp = r8a66597_read(r8a66597, reg);
195 if (i++ > 1000000) {
196 printf("register%lx, loop %x is timeout\n", reg, loop);
197 break;
198 }
199 } while ((tmp & mask) != loop);
200}
201
202static void pipe_buffer_setting(struct r8a66597 *r8a66597,
203 struct usb_device *dev, unsigned long pipe)
204{
205 u16 val = 0;
206 u16 pipenum, bufnum, maxpacket;
207
208 if (usb_pipein(pipe)) {
209 pipenum = BULK_IN_PIPENUM;
210 bufnum = BULK_IN_BUFNUM;
211 maxpacket = dev->epmaxpacketin[usb_pipeendpoint(pipe)];
212 } else {
213 pipenum = BULK_OUT_PIPENUM;
214 bufnum = BULK_OUT_BUFNUM;
215 maxpacket = dev->epmaxpacketout[usb_pipeendpoint(pipe)];
216 }
217
218 if (r8a66597->pipe_config & (1 << pipenum))
219 return;
220 r8a66597->pipe_config |= (1 << pipenum);
221
222 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(pipenum));
223 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(pipenum));
224 r8a66597_write(r8a66597, pipenum, PIPESEL);
225
226 /* FIXME: This driver support bulk transfer only. */
227 if (!usb_pipein(pipe))
228 val |= R8A66597_DIR;
229 else
230 val |= R8A66597_SHTNAK;
231 val |= R8A66597_BULK | R8A66597_DBLB | usb_pipeendpoint(pipe);
232 r8a66597_write(r8a66597, val, PIPECFG);
233
234 r8a66597_write(r8a66597, (8 << 10) | bufnum, PIPEBUF);
235 r8a66597_write(r8a66597, make_devsel(usb_pipedevice(pipe)) |
236 maxpacket, PIPEMAXP);
237 r8a66597_write(r8a66597, 0, PIPEPERI);
238 r8a66597_write(r8a66597, SQCLR, get_pipectr_addr(pipenum));
239}
240
241static int send_setup_packet(struct r8a66597 *r8a66597, struct usb_device *dev,
242 struct devrequest *setup)
243{
244 int i;
245 unsigned short *p = (unsigned short *)setup;
246 unsigned long setup_addr = USBREQ;
247 u16 intsts1;
248 int timeout = 3000;
Chris Brandt11f46782017-11-27 14:04:10 -0500249 u16 dcpctr;
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900250 u16 devsel = setup->request == USB_REQ_SET_ADDRESS ? 0 : dev->devnum;
251
252 r8a66597_write(r8a66597, make_devsel(devsel) |
253 (8 << dev->maxpacketsize), DCPMAXP);
254 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
255
Chris Brandt11f46782017-11-27 14:04:10 -0500256 dcpctr = r8a66597_read(r8a66597, DCPCTR);
257 if ((dcpctr & PID) == PID_BUF) {
Chris Brandt243fd642017-11-29 14:49:21 -0500258 if (readw_poll_timeout(r8a66597->reg + DCPCTR, dcpctr,
259 dcpctr & BSTS, 1000) < 0) {
260 printf("DCPCTR BSTS timeout!\n");
261 return -ETIMEDOUT;
Chris Brandt11f46782017-11-27 14:04:10 -0500262 }
263 }
Chris Brandt11f46782017-11-27 14:04:10 -0500264
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900265 for (i = 0; i < 4; i++) {
266 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
267 setup_addr += 2;
268 }
269 r8a66597_write(r8a66597, ~0x0001, BRDYSTS);
270 r8a66597_write(r8a66597, SUREQ, DCPCTR);
271
272 while (1) {
273 intsts1 = r8a66597_read(r8a66597, INTSTS1);
274 if (intsts1 & SACK)
275 break;
276 if (intsts1 & SIGN) {
277 printf("setup packet send error\n");
278 return -1;
279 }
280 if (timeout-- < 0) {
281 printf("setup packet timeout\n");
282 return -1;
283 }
284 udelay(500);
285 }
286
287 return 0;
288}
289
290static int send_bulk_packet(struct r8a66597 *r8a66597, struct usb_device *dev,
291 unsigned long pipe, void *buffer, int transfer_len)
292{
293 u16 tmp, bufsize;
294 u16 *buf;
295 size_t size;
296
297 R8A66597_DPRINT("%s\n", __func__);
298
299 r8a66597_mdfy(r8a66597, MBW | BULK_OUT_PIPENUM,
Marek Vasut8b548302019-08-11 13:44:10 +0200300 MBW | CURPIPE, CFIFOSEL);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900301 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, BULK_OUT_PIPENUM);
302 tmp = r8a66597_read(r8a66597, CFIFOCTR);
303 if ((tmp & FRDY) == 0) {
304 printf("%s FRDY is not set (%x)\n", __func__, tmp);
305 return -1;
306 }
307
308 /* prepare parameters */
309 bufsize = dev->epmaxpacketout[usb_pipeendpoint(pipe)];
310 buf = (u16 *)(buffer + dev->act_len);
311 size = min((int)bufsize, transfer_len - dev->act_len);
312
313 /* write fifo */
314 r8a66597_write(r8a66597, ~(1 << BULK_OUT_PIPENUM), BEMPSTS);
315 if (buffer) {
316 r8a66597_write_fifo(r8a66597, CFIFO, buf, size);
317 r8a66597_write(r8a66597, BVAL, CFIFOCTR);
318 }
319
320 /* update parameters */
321 dev->act_len += size;
322
323 r8a66597_mdfy(r8a66597, PID_BUF, PID,
Marek Vasut8b548302019-08-11 13:44:10 +0200324 get_pipectr_addr(BULK_OUT_PIPENUM));
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900325
326 while (!(r8a66597_read(r8a66597, BEMPSTS) & (1 << BULK_OUT_PIPENUM)))
327 if (ctrlc())
328 return -1;
329 r8a66597_write(r8a66597, ~(1 << BULK_OUT_PIPENUM), BEMPSTS);
330
331 if (dev->act_len >= transfer_len)
332 r8a66597_mdfy(r8a66597, PID_NAK, PID,
Marek Vasut8b548302019-08-11 13:44:10 +0200333 get_pipectr_addr(BULK_OUT_PIPENUM));
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900334
335 return 0;
336}
337
338static int receive_bulk_packet(struct r8a66597 *r8a66597,
339 struct usb_device *dev,
340 unsigned long pipe,
341 void *buffer, int transfer_len)
342{
343 u16 tmp;
344 u16 *buf;
345 const u16 pipenum = BULK_IN_PIPENUM;
346 int rcv_len;
347 int maxpacket = dev->epmaxpacketin[usb_pipeendpoint(pipe)];
348
349 R8A66597_DPRINT("%s\n", __func__);
350
351 /* prepare */
352 if (dev->act_len == 0) {
353 r8a66597_mdfy(r8a66597, PID_NAK, PID,
Marek Vasut8b548302019-08-11 13:44:10 +0200354 get_pipectr_addr(pipenum));
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900355 r8a66597_write(r8a66597, ~(1 << pipenum), BRDYSTS);
356
357 r8a66597_write(r8a66597, TRCLR, get_pipetre_addr(pipenum));
358 r8a66597_write(r8a66597,
Marek Vasut8b548302019-08-11 13:44:10 +0200359 (transfer_len + maxpacket - 1) / maxpacket,
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900360 get_pipetrn_addr(pipenum));
361 r8a66597_bset(r8a66597, TRENB, get_pipetre_addr(pipenum));
362
363 r8a66597_mdfy(r8a66597, PID_BUF, PID,
Marek Vasut8b548302019-08-11 13:44:10 +0200364 get_pipectr_addr(pipenum));
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900365 }
366
367 r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
368 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
369
370 while (!(r8a66597_read(r8a66597, BRDYSTS) & (1 << pipenum)))
371 if (ctrlc())
372 return -1;
373 r8a66597_write(r8a66597, ~(1 << pipenum), BRDYSTS);
374
375 tmp = r8a66597_read(r8a66597, CFIFOCTR);
376 if ((tmp & FRDY) == 0) {
377 printf("%s FRDY is not set. (%x)\n", __func__, tmp);
378 return -1;
379 }
380
381 buf = (u16 *)(buffer + dev->act_len);
382 rcv_len = tmp & DTLN;
383 dev->act_len += rcv_len;
384
385 if (buffer) {
386 if (rcv_len == 0)
387 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
388 else
389 r8a66597_read_fifo(r8a66597, CFIFO, buf, rcv_len);
390 }
391
392 return 0;
393}
394
395static int receive_control_packet(struct r8a66597 *r8a66597,
396 struct usb_device *dev,
397 void *buffer, int transfer_len)
398{
399 u16 tmp;
400 int rcv_len;
401
402 /* FIXME: limit transfer size : 64byte or less */
403
404 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
405 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
406 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
407 r8a66597_bset(r8a66597, SQSET, DCPCTR);
408 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
409 r8a66597_mdfy(r8a66597, PID_BUF, PID, DCPCTR);
410
411 while (!(r8a66597_read(r8a66597, BRDYSTS) & 0x0001))
412 if (ctrlc())
413 return -1;
414 r8a66597_write(r8a66597, ~0x0001, BRDYSTS);
415
416 r8a66597_mdfy(r8a66597, MBW, MBW | CURPIPE, CFIFOSEL);
417 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
418
419 tmp = r8a66597_read(r8a66597, CFIFOCTR);
420 if ((tmp & FRDY) == 0) {
421 printf("%s FRDY is not set. (%x)\n", __func__, tmp);
422 return -1;
423 }
424
425 rcv_len = tmp & DTLN;
426 dev->act_len += rcv_len;
427
428 r8a66597_mdfy(r8a66597, PID_NAK, PID, DCPCTR);
429
430 if (buffer) {
431 if (rcv_len == 0)
432 r8a66597_write(r8a66597, BCLR, DCPCTR);
433 else
434 r8a66597_read_fifo(r8a66597, CFIFO, buffer, rcv_len);
435 }
436
437 return 0;
438}
439
440static int send_status_packet(struct r8a66597 *r8a66597,
Marek Vasut8b548302019-08-11 13:44:10 +0200441 unsigned long pipe)
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900442{
443 r8a66597_bset(r8a66597, SQSET, DCPCTR);
444 r8a66597_mdfy(r8a66597, PID_NAK, PID, DCPCTR);
445
446 if (usb_pipein(pipe)) {
447 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
448 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
449 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
450 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
451 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
452 } else {
453 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
454 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
455 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
456 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
457 }
458 r8a66597_mdfy(r8a66597, PID_BUF, PID, DCPCTR);
459
460 while (!(r8a66597_read(r8a66597, BEMPSTS) & 0x0001))
461 if (ctrlc())
462 return -1;
463
464 return 0;
465}
466
467static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port)
468{
469 int count = R8A66597_MAX_SAMPLING;
470 unsigned short syssts, old_syssts;
471
472 R8A66597_DPRINT("%s\n", __func__);
473
474 old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port) & LNST);
475 while (count > 0) {
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000476 mdelay(R8A66597_RH_POLL_TIME);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900477
478 syssts = r8a66597_read(r8a66597, get_syssts_reg(port) & LNST);
479 if (syssts == old_syssts) {
480 count--;
481 } else {
482 count = R8A66597_MAX_SAMPLING;
483 old_syssts = syssts;
484 }
485 }
486}
487
488static void r8a66597_bus_reset(struct r8a66597 *r8a66597, int port)
489{
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000490 mdelay(10);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900491 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT, get_dvstctr_reg(port));
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000492 mdelay(50);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900493 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT, get_dvstctr_reg(port));
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000494 mdelay(50);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900495}
496
497static int check_usb_device_connecting(struct r8a66597 *r8a66597)
498{
499 int timeout = 10000; /* 100usec * 10000 = 1sec */
500 int i;
501
502 for (i = 0; i < 5; i++) {
503 /* check a usb cable connect */
504 while (!(r8a66597_read(r8a66597, INTSTS1) & ATTCH)) {
505 if (timeout-- < 0) {
506 printf("%s timeout.\n", __func__);
507 return -1;
508 }
509 udelay(100);
510 }
511
512 /* check a data line */
513 r8a66597_check_syssts(r8a66597, 0);
514
515 r8a66597_bus_reset(r8a66597, 0);
516 r8a66597->speed = get_rh_usb_speed(r8a66597, 0);
517
518 if (!(r8a66597_read(r8a66597, INTSTS1) & DTCH)) {
519 r8a66597->port_change = USB_PORT_STAT_C_CONNECTION;
520 r8a66597->port_status = USB_PORT_STAT_CONNECTION |
521 USB_PORT_STAT_ENABLE;
522 return 0; /* success */
523 }
524
525 R8A66597_DPRINT("USB device has detached. retry = %d\n", i);
526 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
527 }
528
529 return -1; /* fail */
530}
531
Marek Vasut8b548302019-08-11 13:44:10 +0200532/* Virtual Root Hub */
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900533
Stephen Warreneb838e72014-02-13 21:15:18 -0700534#include <usbroothubdes.h>
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900535
Marek Vasut7f3858f2019-08-11 12:34:38 +0200536static int r8a66597_submit_rh_msg(struct udevice *udev, struct usb_device *dev,
537 unsigned long pipe, void *buffer,
538 int transfer_len, struct devrequest *cmd)
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900539{
Marek Vasut7f3858f2019-08-11 12:34:38 +0200540 struct r8a66597 *r8a66597 = dev_get_priv(udev);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900541 int leni = transfer_len;
542 int len = 0;
543 int stat = 0;
544 __u16 bmRType_bReq;
545 __u16 wValue;
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900546 __u16 wLength;
547 unsigned char data[32];
548
549 R8A66597_DPRINT("%s\n", __func__);
550
Remy Bohmer9dbc3662008-10-10 10:23:22 +0200551 if (usb_pipeint(pipe)) {
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900552 printf("Root-Hub submit IRQ: NOT implemented");
553 return 0;
554 }
555
556 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
557 wValue = cpu_to_le16 (cmd->value);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900558 wLength = cpu_to_le16 (cmd->length);
559
560 switch (bmRType_bReq) {
561 case RH_GET_STATUS:
562 *(__u16 *)buffer = cpu_to_le16(1);
563 len = 2;
564 break;
565 case RH_GET_STATUS | RH_INTERFACE:
566 *(__u16 *)buffer = cpu_to_le16(0);
567 len = 2;
568 break;
569 case RH_GET_STATUS | RH_ENDPOINT:
570 *(__u16 *)buffer = cpu_to_le16(0);
571 len = 2;
572 break;
573 case RH_GET_STATUS | RH_CLASS:
574 *(__u32 *)buffer = cpu_to_le32(0);
575 len = 4;
576 break;
577 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
578 *(__u32 *)buffer = cpu_to_le32(r8a66597->port_status |
579 (r8a66597->port_change << 16));
580 len = 4;
581 break;
582 case RH_CLEAR_FEATURE | RH_ENDPOINT:
583 case RH_CLEAR_FEATURE | RH_CLASS:
584 break;
585
586 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
587 switch (wValue) {
588 case RH_C_PORT_CONNECTION:
589 r8a66597->port_change &= ~USB_PORT_STAT_C_CONNECTION;
590 break;
591 }
592 break;
593
594 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
595 switch (wValue) {
596 case (RH_PORT_SUSPEND):
597 break;
598 case (RH_PORT_RESET):
599 r8a66597_bus_reset(r8a66597, 0);
600 break;
601 case (RH_PORT_POWER):
602 break;
603 case (RH_PORT_ENABLE):
604 break;
605 }
606 break;
607 case RH_SET_ADDRESS:
Marek Vasut7f3858f2019-08-11 12:34:38 +0200608 r8a66597->rh_devnum = wValue;
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900609 break;
610 case RH_GET_DESCRIPTOR:
611 switch ((wValue & 0xff00) >> 8) {
612 case (0x01): /* device descriptor */
613 len = min_t(unsigned int,
Marek Vasut8b548302019-08-11 13:44:10 +0200614 leni,
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900615 min_t(unsigned int,
Marek Vasut8b548302019-08-11 13:44:10 +0200616 sizeof(root_hub_dev_des),
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900617 wLength));
618 memcpy(buffer, root_hub_dev_des, len);
619 break;
620 case (0x02): /* configuration descriptor */
621 len = min_t(unsigned int,
Marek Vasut8b548302019-08-11 13:44:10 +0200622 leni,
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900623 min_t(unsigned int,
Marek Vasut8b548302019-08-11 13:44:10 +0200624 sizeof(root_hub_config_des),
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900625 wLength));
626 memcpy(buffer, root_hub_config_des, len);
627 break;
628 case (0x03): /* string descriptors */
629 if (wValue == 0x0300) {
630 len = min_t(unsigned int,
Marek Vasut8b548302019-08-11 13:44:10 +0200631 leni,
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900632 min_t(unsigned int,
Marek Vasut8b548302019-08-11 13:44:10 +0200633 sizeof(root_hub_str_index0),
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900634 wLength));
635 memcpy(buffer, root_hub_str_index0, len);
636 }
637 if (wValue == 0x0301) {
638 len = min_t(unsigned int,
Marek Vasut8b548302019-08-11 13:44:10 +0200639 leni,
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900640 min_t(unsigned int,
Marek Vasut8b548302019-08-11 13:44:10 +0200641 sizeof(root_hub_str_index1),
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900642 wLength));
643 memcpy(buffer, root_hub_str_index1, len);
644 }
645 break;
646 default:
647 stat = USB_ST_STALLED;
648 }
649 break;
650
651 case RH_GET_DESCRIPTOR | RH_CLASS:
652 {
653 __u32 temp = 0x00000001;
654
655 data[0] = 9; /* min length; */
656 data[1] = 0x29;
657 data[2] = temp & RH_A_NDP;
658 data[3] = 0;
659 if (temp & RH_A_PSM)
660 data[3] |= 0x1;
661 if (temp & RH_A_NOCP)
662 data[3] |= 0x10;
663 else if (temp & RH_A_OCPM)
664 data[3] |= 0x8;
665
666 /* corresponds to data[4-7] */
667 data[5] = (temp & RH_A_POTPGT) >> 24;
668 data[7] = temp & RH_B_DR;
669 if (data[2] < 7) {
670 data[8] = 0xff;
671 } else {
672 data[0] += 2;
673 data[8] = (temp & RH_B_DR) >> 8;
Marek Vasut8b548302019-08-11 13:44:10 +0200674 data[9] = 0xff;
675 data[10] = 0xff;
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900676 }
677
678 len = min_t(unsigned int, leni,
679 min_t(unsigned int, data[0], wLength));
680 memcpy(buffer, data, len);
681 break;
682 }
683
684 case RH_GET_CONFIGURATION:
Marek Vasut8b548302019-08-11 13:44:10 +0200685 *(__u8 *)buffer = 0x01;
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900686 len = 1;
687 break;
688 case RH_SET_CONFIGURATION:
689 break;
690 default:
Nobuhiro Iwamatsue58c41e2008-09-18 20:13:08 +0900691 R8A66597_DPRINT("unsupported root hub command");
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900692 stat = USB_ST_STALLED;
693 }
694
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000695 mdelay(1);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900696
697 len = min_t(int, len, leni);
698
699 dev->act_len = len;
700 dev->status = stat;
701
702 return stat;
703}
704
Marek Vasut8b548302019-08-11 13:44:10 +0200705static int r8a66597_submit_control_msg(struct udevice *udev,
706 struct usb_device *dev,
707 unsigned long pipe, void *buffer,
708 int length, struct devrequest *setup)
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900709{
Marek Vasut7f3858f2019-08-11 12:34:38 +0200710 struct r8a66597 *r8a66597 = dev_get_priv(udev);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900711 u16 r8a66597_address = setup->request == USB_REQ_SET_ADDRESS ?
712 0 : dev->devnum;
713
Marek Vasut7f3858f2019-08-11 12:34:38 +0200714 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
715 udev->name, dev, dev->dev->name, dev->portnr);
716
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900717 R8A66597_DPRINT("%s\n", __func__);
718 if (usb_pipedevice(pipe) == r8a66597->rh_devnum)
Marek Vasut7f3858f2019-08-11 12:34:38 +0200719 return r8a66597_submit_rh_msg(udev, dev, pipe, buffer,
720 length, setup);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900721
722 R8A66597_DPRINT("%s: setup\n", __func__);
Yoshihiro Shimoda60ece6d2008-10-29 20:05:18 +0900723 set_devadd(r8a66597, r8a66597_address, dev, 0);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900724
725 if (send_setup_packet(r8a66597, dev, setup) < 0) {
726 printf("setup packet send error\n");
727 return -1;
728 }
729
Yoshihiro Shimoda60ece6d2008-10-29 20:05:18 +0900730 dev->act_len = 0;
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900731 if (usb_pipein(pipe))
732 if (receive_control_packet(r8a66597, dev, buffer,
Marek Vasut8b548302019-08-11 13:44:10 +0200733 length) < 0)
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900734 return -1;
735
736 if (send_status_packet(r8a66597, pipe) < 0)
737 return -1;
738
739 dev->status = 0;
740
741 return 0;
742}
743
Marek Vasut8b548302019-08-11 13:44:10 +0200744static int r8a66597_submit_bulk_msg(struct udevice *udev,
745 struct usb_device *dev, unsigned long pipe,
746 void *buffer, int length)
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900747{
Marek Vasut7f3858f2019-08-11 12:34:38 +0200748 struct r8a66597 *r8a66597 = dev_get_priv(udev);
749 int ret = 0;
750
751 debug("%s: dev='%s', udev=%p\n", __func__, udev->name, dev);
752
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900753 R8A66597_DPRINT("%s\n", __func__);
Marek Vasut7f3858f2019-08-11 12:34:38 +0200754 R8A66597_DPRINT("pipe = %08x, buffer = %p, len = %d, devnum = %d\n",
755 pipe, buffer, length, dev->devnum);
756
757 set_devadd(r8a66597, dev->devnum, dev, 0);
758
759 pipe_buffer_setting(r8a66597, dev, pipe);
760
761 dev->act_len = 0;
762 while (dev->act_len < length && ret == 0) {
763 if (ctrlc())
764 return -1;
765
766 if (usb_pipein(pipe))
767 ret = receive_bulk_packet(r8a66597, dev, pipe, buffer,
Marek Vasut8b548302019-08-11 13:44:10 +0200768 length);
Marek Vasut7f3858f2019-08-11 12:34:38 +0200769 else
770 ret = send_bulk_packet(r8a66597, dev, pipe, buffer,
Marek Vasut8b548302019-08-11 13:44:10 +0200771 length);
Marek Vasut7f3858f2019-08-11 12:34:38 +0200772 }
773
774 if (ret == 0)
775 dev->status = 0;
776
777 return ret;
778}
779
Simon Glassd1998a92020-12-03 16:55:21 -0700780static int r8a66597_usb_of_to_plat(struct udevice *dev)
Marek Vasut7f3858f2019-08-11 12:34:38 +0200781{
782 struct r8a66597 *priv = dev_get_priv(dev);
783 fdt_addr_t addr;
784
785 addr = dev_read_addr(dev);
786 if (addr == FDT_ADDR_T_NONE)
787 return -EINVAL;
788 priv->reg = addr;
789
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900790 return 0;
791}
792
Marek Vasut7f3858f2019-08-11 12:34:38 +0200793static int r8a66597_usb_probe(struct udevice *dev)
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900794{
Marek Vasut7f3858f2019-08-11 12:34:38 +0200795 struct r8a66597 *priv = dev_get_priv(dev);
796 struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
Marek Vasut769a9cd2019-08-22 17:53:04 +0200797 int ret;
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900798
Marek Vasut7f3858f2019-08-11 12:34:38 +0200799 bus_priv->desc_before_addr = true;
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900800
Marek Vasut769a9cd2019-08-22 17:53:04 +0200801 if (CONFIG_IS_ENABLED(DM_REGULATOR)) {
802 ret = device_get_supply_regulator(dev, "vbus-supply",
803 &priv->vbus_supply);
804 if (ret) {
805 dev_err(dev,
806 "can't get VBUS supply\n");
807 return ret;
808 }
809
810 ret = regulator_set_enable(priv->vbus_supply, true);
811 if (ret) {
812 dev_err(dev,
813 "can't enable VBUS supply\n");
814 return ret;
815 }
816 }
817
Marek Vasut7f3858f2019-08-11 12:34:38 +0200818 disable_controller(priv);
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000819 mdelay(100);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900820
Marek Vasut7f3858f2019-08-11 12:34:38 +0200821 enable_controller(priv);
Marek Vasut8b548302019-08-11 13:44:10 +0200822 r8a66597_port_power(priv, 0, 1);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900823
824 /* check usb device */
Marek Vasut7f3858f2019-08-11 12:34:38 +0200825 check_usb_device_connecting(priv);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900826
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000827 mdelay(50);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900828
829 return 0;
830}
831
Marek Vasut7f3858f2019-08-11 12:34:38 +0200832static int r8a66597_usb_remove(struct udevice *dev)
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900833{
Marek Vasut7f3858f2019-08-11 12:34:38 +0200834 struct r8a66597 *priv = dev_get_priv(dev);
Marek Vasut769a9cd2019-08-22 17:53:04 +0200835 int ret;
Marek Vasut7f3858f2019-08-11 12:34:38 +0200836
837 disable_controller(priv);
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900838
Marek Vasut769a9cd2019-08-22 17:53:04 +0200839 if (CONFIG_IS_ENABLED(DM_REGULATOR)) {
840 ret = regulator_set_enable(priv->vbus_supply, false);
841 if (ret) {
842 dev_err(dev,
843 "can't disable VBUS supply\n");
844 return ret;
845 }
846 }
847
Yoshihiro Shimodafd0f2f32008-07-09 21:07:38 +0900848 return 0;
849}
Marek Vasut7f3858f2019-08-11 12:34:38 +0200850
851struct dm_usb_ops r8a66597_usb_ops = {
852 .control = r8a66597_submit_control_msg,
853 .bulk = r8a66597_submit_bulk_msg,
854};
855
856static const struct udevice_id r8a66597_usb_ids[] = {
857 { .compatible = "renesas,rza1-usbhs" },
858 { }
859};
860
861U_BOOT_DRIVER(usb_r8a66597) = {
862 .name = "r8a66597_usb",
863 .id = UCLASS_USB,
864 .of_match = r8a66597_usb_ids,
Simon Glassd1998a92020-12-03 16:55:21 -0700865 .of_to_plat = r8a66597_usb_of_to_plat,
Marek Vasut7f3858f2019-08-11 12:34:38 +0200866 .probe = r8a66597_usb_probe,
867 .remove = r8a66597_usb_remove,
868 .ops = &r8a66597_usb_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700869 .priv_auto = sizeof(struct r8a66597),
Marek Vasut7f3858f2019-08-11 12:34:38 +0200870 .flags = DM_FLAG_ALLOC_PRIV_DMA,
871};