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