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