blob: 8cca09f6d8850877d392d140e85cd80af41fa65e [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>
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090010#include <usb.h>
11#include <asm/io.h>
Chris Brandtc85f3cc2017-11-29 14:49:21 -050012#include <linux/iopoll.h>
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090013
14#include "r8a66597.h"
15
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090016#ifdef R8A66597_DEBUG
17#define R8A66597_DPRINT printf
18#else
19#define R8A66597_DPRINT(...)
20#endif
21
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090022static struct r8a66597 gr8a66597;
23
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090024static void get_hub_data(struct usb_device *dev, u16 *hub_devnum, u16 *hubport)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090025{
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090026 int i;
27
28 *hub_devnum = 0;
29 *hubport = 0;
30
31 /* check a device connected to root_hub */
32 if ((dev->parent && dev->parent->devnum == 1) ||
33 (dev->devnum == 1))
34 return;
35
36 for (i = 0; i < USB_MAXCHILDREN; i++) {
37 if (dev->parent->children[i] == dev) {
38 *hub_devnum = (u8)dev->parent->devnum;
39 *hubport = i;
40 return;
41 }
42 }
43
44 printf("get_hub_data error.\n");
45}
46
47static void set_devadd(struct r8a66597 *r8a66597, u8 r8a66597_address,
48 struct usb_device *dev, int port)
49{
50 u16 val, usbspd, upphub, hubport;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090051 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
52
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +090053 get_hub_data(dev, &upphub, &hubport);
54 usbspd = r8a66597->speed;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090055 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
56 r8a66597_write(r8a66597, val, devadd_reg);
57}
58
59static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
60{
61 u16 tmp;
62 int i = 0;
63
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090064 do {
65 r8a66597_write(r8a66597, USBE, SYSCFG0);
66 tmp = r8a66597_read(r8a66597, SYSCFG0);
67 if (i++ > 1000) {
68 printf("register access fail.\n");
69 return -1;
70 }
71 } while ((tmp & USBE) != USBE);
72 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
Chris Brandtfaecead2017-11-27 14:04:10 -050073#if !defined(CONFIG_RZA_USB)
Nobuhiro Iwamatsu0083cd72012-03-20 20:23:29 +000074 r8a66597_mdfy(r8a66597, CONFIG_R8A66597_XTAL, XTAL, SYSCFG0);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +090075
76 i = 0;
77 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
78 do {
79 udelay(1000);
80 tmp = r8a66597_read(r8a66597, SYSCFG0);
81 if (i++ > 500) {
82 printf("register access fail.\n");
83 return -1;
84 }
85 } while ((tmp & SCKE) != SCKE);
Chris Brandtfaecead2017-11-27 14:04:10 -050086#else
87 /*
88 * RZ/A Only:
89 * Bits XTAL(UCKSEL) and UPLLE in SYSCFG0 for USB0 controls both USB0
90 * and USB1, so we must always set the USB0 register
91 */
92#if (CONFIG_R8A66597_XTAL == 1)
93 setbits(le16, R8A66597_BASE0, XTAL);
94#endif
95 mdelay(1);
96 setbits(le16, R8A66597_BASE0, UPLLE);
97 mdelay(1);
98 r8a66597_bset(r8a66597, SUSPM, SUSPMODE0);
99#endif /* CONFIG_RZA_USB */
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900100
101 return 0;
102}
103
104static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
105{
Chris Brandtfaecead2017-11-27 14:04:10 -0500106#if !defined(CONFIG_RZA_USB)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900107 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
108 udelay(1);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900109 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
110 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
111 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
Chris Brandtfaecead2017-11-27 14:04:10 -0500112#else
113 r8a66597_bclr(r8a66597, SUSPM, SUSPMODE0);
114
115 clrbits(le16, R8A66597_BASE0, UPLLE);
116 mdelay(1);
117 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
118 mdelay(1);
119
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900120#endif
121}
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));
130
Chris Brandtfaecead2017-11-27 14:04:10 -0500131#if !defined(CONFIG_RZA_USB)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900132 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
Chris Brandtfaecead2017-11-27 14:04:10 -0500133#endif
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900134}
135
136static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
137{
138 u16 val, tmp;
139
140 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
141 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
142
143 r8a66597_port_power(r8a66597, port, 0);
144
145 do {
146 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
147 udelay(640);
148 } while (tmp == EDGESTS);
149
150 val = port ? DRPD : DCFM | DRPD;
151 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
152 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
153}
154
155static int enable_controller(struct r8a66597 *r8a66597)
156{
157 int ret, port;
158
159 ret = r8a66597_clock_enable(r8a66597);
160 if (ret < 0)
161 return ret;
162
Chris Brandtfaecead2017-11-27 14:04:10 -0500163#if !defined(CONFIG_RZA_USB)
Nobuhiro Iwamatsu0083cd72012-03-20 20:23:29 +0000164 r8a66597_bset(r8a66597, CONFIG_R8A66597_LDRV & LDRV, PINCFG);
Chris Brandtfaecead2017-11-27 14:04:10 -0500165#endif
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900166 r8a66597_bset(r8a66597, USBE, SYSCFG0);
167
168 r8a66597_bset(r8a66597, INTL, SOFCFG);
169 r8a66597_write(r8a66597, 0, INTENB0);
yasuhisa umanoea33c6a2014-04-18 11:33:08 +0900170 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
171 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900172
Nobuhiro Iwamatsu0083cd72012-03-20 20:23:29 +0000173 r8a66597_bset(r8a66597, CONFIG_R8A66597_ENDIAN & BIGEND, CFIFOSEL);
174 r8a66597_bset(r8a66597, CONFIG_R8A66597_ENDIAN & BIGEND, D0FIFOSEL);
175 r8a66597_bset(r8a66597, CONFIG_R8A66597_ENDIAN & BIGEND, D1FIFOSEL);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900176 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
177
178 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
179 r8a66597_enable_port(r8a66597, port);
180
181 return 0;
182}
183
184static void disable_controller(struct r8a66597 *r8a66597)
185{
186 int i;
187
188 if (!(r8a66597_read(r8a66597, SYSCFG0) & USBE))
189 return;
190
191 r8a66597_write(r8a66597, 0, INTENB0);
192 r8a66597_write(r8a66597, 0, INTSTS0);
193
194 r8a66597_write(r8a66597, 0, D0FIFOSEL);
195 r8a66597_write(r8a66597, 0, D1FIFOSEL);
196 r8a66597_write(r8a66597, 0, DCPCFG);
197 r8a66597_write(r8a66597, 0x40, DCPMAXP);
198 r8a66597_write(r8a66597, 0, DCPCTR);
199
200 for (i = 0; i <= 10; i++)
201 r8a66597_write(r8a66597, 0, get_devadd_addr(i));
202 for (i = 1; i <= 5; i++) {
203 r8a66597_write(r8a66597, 0, get_pipetre_addr(i));
204 r8a66597_write(r8a66597, 0, get_pipetrn_addr(i));
205 }
206 for (i = 1; i < R8A66597_MAX_NUM_PIPE; i++) {
207 r8a66597_write(r8a66597, 0, get_pipectr_addr(i));
208 r8a66597_write(r8a66597, i, PIPESEL);
209 r8a66597_write(r8a66597, 0, PIPECFG);
210 r8a66597_write(r8a66597, 0, PIPEBUF);
211 r8a66597_write(r8a66597, 0, PIPEMAXP);
212 r8a66597_write(r8a66597, 0, PIPEPERI);
213 }
214
215 for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++)
216 r8a66597_disable_port(r8a66597, i);
217
218 r8a66597_clock_disable(r8a66597);
219}
220
221static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
222 u16 mask, u16 loop)
223{
224 u16 tmp;
225 int i = 0;
226
227 do {
228 tmp = r8a66597_read(r8a66597, reg);
229 if (i++ > 1000000) {
230 printf("register%lx, loop %x is timeout\n", reg, loop);
231 break;
232 }
233 } while ((tmp & mask) != loop);
234}
235
236static void pipe_buffer_setting(struct r8a66597 *r8a66597,
237 struct usb_device *dev, unsigned long pipe)
238{
239 u16 val = 0;
240 u16 pipenum, bufnum, maxpacket;
241
242 if (usb_pipein(pipe)) {
243 pipenum = BULK_IN_PIPENUM;
244 bufnum = BULK_IN_BUFNUM;
245 maxpacket = dev->epmaxpacketin[usb_pipeendpoint(pipe)];
246 } else {
247 pipenum = BULK_OUT_PIPENUM;
248 bufnum = BULK_OUT_BUFNUM;
249 maxpacket = dev->epmaxpacketout[usb_pipeendpoint(pipe)];
250 }
251
252 if (r8a66597->pipe_config & (1 << pipenum))
253 return;
254 r8a66597->pipe_config |= (1 << pipenum);
255
256 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(pipenum));
257 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(pipenum));
258 r8a66597_write(r8a66597, pipenum, PIPESEL);
259
260 /* FIXME: This driver support bulk transfer only. */
261 if (!usb_pipein(pipe))
262 val |= R8A66597_DIR;
263 else
264 val |= R8A66597_SHTNAK;
265 val |= R8A66597_BULK | R8A66597_DBLB | usb_pipeendpoint(pipe);
266 r8a66597_write(r8a66597, val, PIPECFG);
267
268 r8a66597_write(r8a66597, (8 << 10) | bufnum, PIPEBUF);
269 r8a66597_write(r8a66597, make_devsel(usb_pipedevice(pipe)) |
270 maxpacket, PIPEMAXP);
271 r8a66597_write(r8a66597, 0, PIPEPERI);
272 r8a66597_write(r8a66597, SQCLR, get_pipectr_addr(pipenum));
273}
274
275static int send_setup_packet(struct r8a66597 *r8a66597, struct usb_device *dev,
276 struct devrequest *setup)
277{
278 int i;
279 unsigned short *p = (unsigned short *)setup;
280 unsigned long setup_addr = USBREQ;
281 u16 intsts1;
282 int timeout = 3000;
Chris Brandtfaecead2017-11-27 14:04:10 -0500283#if defined(CONFIG_RZA_USB)
284 u16 dcpctr;
Chris Brandtfaecead2017-11-27 14:04:10 -0500285#endif
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900286 u16 devsel = setup->request == USB_REQ_SET_ADDRESS ? 0 : dev->devnum;
287
288 r8a66597_write(r8a66597, make_devsel(devsel) |
289 (8 << dev->maxpacketsize), DCPMAXP);
290 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
291
Chris Brandtfaecead2017-11-27 14:04:10 -0500292#if defined(CONFIG_RZA_USB)
293 dcpctr = r8a66597_read(r8a66597, DCPCTR);
294 if ((dcpctr & PID) == PID_BUF) {
Chris Brandtc85f3cc2017-11-29 14:49:21 -0500295 if (readw_poll_timeout(r8a66597->reg + DCPCTR, dcpctr,
296 dcpctr & BSTS, 1000) < 0) {
297 printf("DCPCTR BSTS timeout!\n");
298 return -ETIMEDOUT;
Chris Brandtfaecead2017-11-27 14:04:10 -0500299 }
300 }
301#endif
302
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900303 for (i = 0; i < 4; i++) {
304 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
305 setup_addr += 2;
306 }
307 r8a66597_write(r8a66597, ~0x0001, BRDYSTS);
308 r8a66597_write(r8a66597, SUREQ, DCPCTR);
309
310 while (1) {
311 intsts1 = r8a66597_read(r8a66597, INTSTS1);
312 if (intsts1 & SACK)
313 break;
314 if (intsts1 & SIGN) {
315 printf("setup packet send error\n");
316 return -1;
317 }
318 if (timeout-- < 0) {
319 printf("setup packet timeout\n");
320 return -1;
321 }
322 udelay(500);
323 }
324
325 return 0;
326}
327
328static int send_bulk_packet(struct r8a66597 *r8a66597, struct usb_device *dev,
329 unsigned long pipe, void *buffer, int transfer_len)
330{
331 u16 tmp, bufsize;
332 u16 *buf;
333 size_t size;
334
335 R8A66597_DPRINT("%s\n", __func__);
336
337 r8a66597_mdfy(r8a66597, MBW | BULK_OUT_PIPENUM,
338 MBW | CURPIPE, CFIFOSEL);
339 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, BULK_OUT_PIPENUM);
340 tmp = r8a66597_read(r8a66597, CFIFOCTR);
341 if ((tmp & FRDY) == 0) {
342 printf("%s FRDY is not set (%x)\n", __func__, tmp);
343 return -1;
344 }
345
346 /* prepare parameters */
347 bufsize = dev->epmaxpacketout[usb_pipeendpoint(pipe)];
348 buf = (u16 *)(buffer + dev->act_len);
349 size = min((int)bufsize, transfer_len - dev->act_len);
350
351 /* write fifo */
352 r8a66597_write(r8a66597, ~(1 << BULK_OUT_PIPENUM), BEMPSTS);
353 if (buffer) {
354 r8a66597_write_fifo(r8a66597, CFIFO, buf, size);
355 r8a66597_write(r8a66597, BVAL, CFIFOCTR);
356 }
357
358 /* update parameters */
359 dev->act_len += size;
360
361 r8a66597_mdfy(r8a66597, PID_BUF, PID,
362 get_pipectr_addr(BULK_OUT_PIPENUM));
363
364 while (!(r8a66597_read(r8a66597, BEMPSTS) & (1 << BULK_OUT_PIPENUM)))
365 if (ctrlc())
366 return -1;
367 r8a66597_write(r8a66597, ~(1 << BULK_OUT_PIPENUM), BEMPSTS);
368
369 if (dev->act_len >= transfer_len)
370 r8a66597_mdfy(r8a66597, PID_NAK, PID,
371 get_pipectr_addr(BULK_OUT_PIPENUM));
372
373 return 0;
374}
375
376static int receive_bulk_packet(struct r8a66597 *r8a66597,
377 struct usb_device *dev,
378 unsigned long pipe,
379 void *buffer, int transfer_len)
380{
381 u16 tmp;
382 u16 *buf;
383 const u16 pipenum = BULK_IN_PIPENUM;
384 int rcv_len;
385 int maxpacket = dev->epmaxpacketin[usb_pipeendpoint(pipe)];
386
387 R8A66597_DPRINT("%s\n", __func__);
388
389 /* prepare */
390 if (dev->act_len == 0) {
391 r8a66597_mdfy(r8a66597, PID_NAK, PID,
392 get_pipectr_addr(pipenum));
393 r8a66597_write(r8a66597, ~(1 << pipenum), BRDYSTS);
394
395 r8a66597_write(r8a66597, TRCLR, get_pipetre_addr(pipenum));
396 r8a66597_write(r8a66597,
397 (transfer_len + maxpacket - 1) / maxpacket,
398 get_pipetrn_addr(pipenum));
399 r8a66597_bset(r8a66597, TRENB, get_pipetre_addr(pipenum));
400
401 r8a66597_mdfy(r8a66597, PID_BUF, PID,
402 get_pipectr_addr(pipenum));
403 }
404
405 r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
406 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
407
408 while (!(r8a66597_read(r8a66597, BRDYSTS) & (1 << pipenum)))
409 if (ctrlc())
410 return -1;
411 r8a66597_write(r8a66597, ~(1 << pipenum), BRDYSTS);
412
413 tmp = r8a66597_read(r8a66597, CFIFOCTR);
414 if ((tmp & FRDY) == 0) {
415 printf("%s FRDY is not set. (%x)\n", __func__, tmp);
416 return -1;
417 }
418
419 buf = (u16 *)(buffer + dev->act_len);
420 rcv_len = tmp & DTLN;
421 dev->act_len += rcv_len;
422
423 if (buffer) {
424 if (rcv_len == 0)
425 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
426 else
427 r8a66597_read_fifo(r8a66597, CFIFO, buf, rcv_len);
428 }
429
430 return 0;
431}
432
433static int receive_control_packet(struct r8a66597 *r8a66597,
434 struct usb_device *dev,
435 void *buffer, int transfer_len)
436{
437 u16 tmp;
438 int rcv_len;
439
440 /* FIXME: limit transfer size : 64byte or less */
441
442 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
443 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
444 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
445 r8a66597_bset(r8a66597, SQSET, DCPCTR);
446 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
447 r8a66597_mdfy(r8a66597, PID_BUF, PID, DCPCTR);
448
449 while (!(r8a66597_read(r8a66597, BRDYSTS) & 0x0001))
450 if (ctrlc())
451 return -1;
452 r8a66597_write(r8a66597, ~0x0001, BRDYSTS);
453
454 r8a66597_mdfy(r8a66597, MBW, MBW | CURPIPE, CFIFOSEL);
455 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
456
457 tmp = r8a66597_read(r8a66597, CFIFOCTR);
458 if ((tmp & FRDY) == 0) {
459 printf("%s FRDY is not set. (%x)\n", __func__, tmp);
460 return -1;
461 }
462
463 rcv_len = tmp & DTLN;
464 dev->act_len += rcv_len;
465
466 r8a66597_mdfy(r8a66597, PID_NAK, PID, DCPCTR);
467
468 if (buffer) {
469 if (rcv_len == 0)
470 r8a66597_write(r8a66597, BCLR, DCPCTR);
471 else
472 r8a66597_read_fifo(r8a66597, CFIFO, buffer, rcv_len);
473 }
474
475 return 0;
476}
477
478static int send_status_packet(struct r8a66597 *r8a66597,
479 unsigned long pipe)
480{
481 r8a66597_bset(r8a66597, SQSET, DCPCTR);
482 r8a66597_mdfy(r8a66597, PID_NAK, PID, DCPCTR);
483
484 if (usb_pipein(pipe)) {
485 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
486 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
487 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
488 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
489 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
490 } else {
491 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
492 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
493 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
494 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
495 }
496 r8a66597_mdfy(r8a66597, PID_BUF, PID, DCPCTR);
497
498 while (!(r8a66597_read(r8a66597, BEMPSTS) & 0x0001))
499 if (ctrlc())
500 return -1;
501
502 return 0;
503}
504
505static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port)
506{
507 int count = R8A66597_MAX_SAMPLING;
508 unsigned short syssts, old_syssts;
509
510 R8A66597_DPRINT("%s\n", __func__);
511
512 old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port) & LNST);
513 while (count > 0) {
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000514 mdelay(R8A66597_RH_POLL_TIME);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900515
516 syssts = r8a66597_read(r8a66597, get_syssts_reg(port) & LNST);
517 if (syssts == old_syssts) {
518 count--;
519 } else {
520 count = R8A66597_MAX_SAMPLING;
521 old_syssts = syssts;
522 }
523 }
524}
525
526static void r8a66597_bus_reset(struct r8a66597 *r8a66597, int port)
527{
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000528 mdelay(10);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900529 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT, get_dvstctr_reg(port));
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000530 mdelay(50);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900531 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT, get_dvstctr_reg(port));
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000532 mdelay(50);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900533}
534
535static int check_usb_device_connecting(struct r8a66597 *r8a66597)
536{
537 int timeout = 10000; /* 100usec * 10000 = 1sec */
538 int i;
539
540 for (i = 0; i < 5; i++) {
541 /* check a usb cable connect */
542 while (!(r8a66597_read(r8a66597, INTSTS1) & ATTCH)) {
543 if (timeout-- < 0) {
544 printf("%s timeout.\n", __func__);
545 return -1;
546 }
547 udelay(100);
548 }
549
550 /* check a data line */
551 r8a66597_check_syssts(r8a66597, 0);
552
553 r8a66597_bus_reset(r8a66597, 0);
554 r8a66597->speed = get_rh_usb_speed(r8a66597, 0);
555
556 if (!(r8a66597_read(r8a66597, INTSTS1) & DTCH)) {
557 r8a66597->port_change = USB_PORT_STAT_C_CONNECTION;
558 r8a66597->port_status = USB_PORT_STAT_CONNECTION |
559 USB_PORT_STAT_ENABLE;
560 return 0; /* success */
561 }
562
563 R8A66597_DPRINT("USB device has detached. retry = %d\n", i);
564 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
565 }
566
567 return -1; /* fail */
568}
569
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900570/*-------------------------------------------------------------------------*
571 * Virtual Root Hub
572 *-------------------------------------------------------------------------*/
573
Stephen Warren39c89682014-02-13 21:15:18 -0700574#include <usbroothubdes.h>
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900575
576static int r8a66597_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
577 void *buffer, int transfer_len, struct devrequest *cmd)
578{
579 struct r8a66597 *r8a66597 = &gr8a66597;
580 int leni = transfer_len;
581 int len = 0;
582 int stat = 0;
583 __u16 bmRType_bReq;
584 __u16 wValue;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900585 __u16 wLength;
586 unsigned char data[32];
587
588 R8A66597_DPRINT("%s\n", __func__);
589
Remy Bohmerd8c55ab2008-10-10 10:23:22 +0200590 if (usb_pipeint(pipe)) {
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900591 printf("Root-Hub submit IRQ: NOT implemented");
592 return 0;
593 }
594
595 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
596 wValue = cpu_to_le16 (cmd->value);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900597 wLength = cpu_to_le16 (cmd->length);
598
599 switch (bmRType_bReq) {
600 case RH_GET_STATUS:
601 *(__u16 *)buffer = cpu_to_le16(1);
602 len = 2;
603 break;
604 case RH_GET_STATUS | RH_INTERFACE:
605 *(__u16 *)buffer = cpu_to_le16(0);
606 len = 2;
607 break;
608 case RH_GET_STATUS | RH_ENDPOINT:
609 *(__u16 *)buffer = cpu_to_le16(0);
610 len = 2;
611 break;
612 case RH_GET_STATUS | RH_CLASS:
613 *(__u32 *)buffer = cpu_to_le32(0);
614 len = 4;
615 break;
616 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
617 *(__u32 *)buffer = cpu_to_le32(r8a66597->port_status |
618 (r8a66597->port_change << 16));
619 len = 4;
620 break;
621 case RH_CLEAR_FEATURE | RH_ENDPOINT:
622 case RH_CLEAR_FEATURE | RH_CLASS:
623 break;
624
625 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
626 switch (wValue) {
627 case RH_C_PORT_CONNECTION:
628 r8a66597->port_change &= ~USB_PORT_STAT_C_CONNECTION;
629 break;
630 }
631 break;
632
633 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
634 switch (wValue) {
635 case (RH_PORT_SUSPEND):
636 break;
637 case (RH_PORT_RESET):
638 r8a66597_bus_reset(r8a66597, 0);
639 break;
640 case (RH_PORT_POWER):
641 break;
642 case (RH_PORT_ENABLE):
643 break;
644 }
645 break;
646 case RH_SET_ADDRESS:
647 gr8a66597.rh_devnum = wValue;
648 break;
649 case RH_GET_DESCRIPTOR:
650 switch ((wValue & 0xff00) >> 8) {
651 case (0x01): /* device descriptor */
652 len = min_t(unsigned int,
653 leni,
654 min_t(unsigned int,
655 sizeof(root_hub_dev_des),
656 wLength));
657 memcpy(buffer, root_hub_dev_des, len);
658 break;
659 case (0x02): /* configuration descriptor */
660 len = min_t(unsigned int,
661 leni,
662 min_t(unsigned int,
663 sizeof(root_hub_config_des),
664 wLength));
665 memcpy(buffer, root_hub_config_des, len);
666 break;
667 case (0x03): /* string descriptors */
668 if (wValue == 0x0300) {
669 len = min_t(unsigned int,
670 leni,
671 min_t(unsigned int,
672 sizeof(root_hub_str_index0),
673 wLength));
674 memcpy(buffer, root_hub_str_index0, len);
675 }
676 if (wValue == 0x0301) {
677 len = min_t(unsigned int,
678 leni,
679 min_t(unsigned int,
680 sizeof(root_hub_str_index1),
681 wLength));
682 memcpy(buffer, root_hub_str_index1, len);
683 }
684 break;
685 default:
686 stat = USB_ST_STALLED;
687 }
688 break;
689
690 case RH_GET_DESCRIPTOR | RH_CLASS:
691 {
692 __u32 temp = 0x00000001;
693
694 data[0] = 9; /* min length; */
695 data[1] = 0x29;
696 data[2] = temp & RH_A_NDP;
697 data[3] = 0;
698 if (temp & RH_A_PSM)
699 data[3] |= 0x1;
700 if (temp & RH_A_NOCP)
701 data[3] |= 0x10;
702 else if (temp & RH_A_OCPM)
703 data[3] |= 0x8;
704
705 /* corresponds to data[4-7] */
706 data[5] = (temp & RH_A_POTPGT) >> 24;
707 data[7] = temp & RH_B_DR;
708 if (data[2] < 7) {
709 data[8] = 0xff;
710 } else {
711 data[0] += 2;
712 data[8] = (temp & RH_B_DR) >> 8;
713 data[10] = data[9] = 0xff;
714 }
715
716 len = min_t(unsigned int, leni,
717 min_t(unsigned int, data[0], wLength));
718 memcpy(buffer, data, len);
719 break;
720 }
721
722 case RH_GET_CONFIGURATION:
723 *(__u8 *) buffer = 0x01;
724 len = 1;
725 break;
726 case RH_SET_CONFIGURATION:
727 break;
728 default:
Nobuhiro Iwamatsu9d58a012008-09-18 20:13:08 +0900729 R8A66597_DPRINT("unsupported root hub command");
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900730 stat = USB_ST_STALLED;
731 }
732
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000733 mdelay(1);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900734
735 len = min_t(int, len, leni);
736
737 dev->act_len = len;
738 dev->status = stat;
739
740 return stat;
741}
742
743int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
744 int transfer_len)
745{
746 struct r8a66597 *r8a66597 = &gr8a66597;
747 int ret = 0;
748
749 R8A66597_DPRINT("%s\n", __func__);
750 R8A66597_DPRINT("pipe = %08x, buffer = %p, len = %d, devnum = %d\n",
751 pipe, buffer, transfer_len, dev->devnum);
752
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +0900753 set_devadd(r8a66597, dev->devnum, dev, 0);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900754
755 pipe_buffer_setting(r8a66597, dev, pipe);
756
757 dev->act_len = 0;
758 while (dev->act_len < transfer_len && ret == 0) {
759 if (ctrlc())
760 return -1;
761
762 if (usb_pipein(pipe))
763 ret = receive_bulk_packet(r8a66597, dev, pipe, buffer,
764 transfer_len);
765 else
766 ret = send_bulk_packet(r8a66597, dev, pipe, buffer,
767 transfer_len);
768 }
769
770 if (ret == 0)
771 dev->status = 0;
772
773 return ret;
774}
775
776int submit_control_msg(struct usb_device *dev, unsigned long pipe,
777 void *buffer, int transfer_len, struct devrequest *setup)
778{
779 struct r8a66597 *r8a66597 = &gr8a66597;
780 u16 r8a66597_address = setup->request == USB_REQ_SET_ADDRESS ?
781 0 : dev->devnum;
782
783 R8A66597_DPRINT("%s\n", __func__);
784 if (usb_pipedevice(pipe) == r8a66597->rh_devnum)
785 return r8a66597_submit_rh_msg(dev, pipe, buffer, transfer_len,
786 setup);
787
788 R8A66597_DPRINT("%s: setup\n", __func__);
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +0900789 set_devadd(r8a66597, r8a66597_address, dev, 0);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900790
791 if (send_setup_packet(r8a66597, dev, setup) < 0) {
792 printf("setup packet send error\n");
793 return -1;
794 }
795
Yoshihiro Shimoda8e6cc002008-10-29 20:05:18 +0900796 dev->act_len = 0;
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900797 if (usb_pipein(pipe))
798 if (receive_control_packet(r8a66597, dev, buffer,
799 transfer_len) < 0)
800 return -1;
801
802 if (send_status_packet(r8a66597, pipe) < 0)
803 return -1;
804
805 dev->status = 0;
806
807 return 0;
808}
809
810int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
811 int transfer_len, int interval)
812{
813 /* no implement */
814 R8A66597_DPRINT("%s\n", __func__);
815 return 0;
816}
817
Troy Kisky8f9c49d2013-10-10 15:27:56 -0700818int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900819{
820 struct r8a66597 *r8a66597 = &gr8a66597;
821
822 R8A66597_DPRINT("%s\n", __func__);
823
Yasuhisa Umano8af893e2014-04-18 11:33:15 +0900824 memset(r8a66597, 0, sizeof(*r8a66597));
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900825 r8a66597->reg = CONFIG_R8A66597_BASE_ADDR;
826
827 disable_controller(r8a66597);
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000828 mdelay(100);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900829
830 enable_controller(r8a66597);
831 r8a66597_port_power(r8a66597, 0 , 1);
832
833 /* check usb device */
834 check_usb_device_connecting(r8a66597);
835
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000836 mdelay(50);
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900837
838 return 0;
839}
840
Lucas Stacha3231282012-09-26 00:14:34 +0200841int usb_lowlevel_stop(int index)
Yoshihiro Shimodaf66d9982008-07-09 21:07:38 +0900842{
843 disable_controller(&gr8a66597);
844
845 return 0;
846}