xref: /openbmc/u-boot/drivers/usb/host/r8a66597-hcd.c (revision 702e6014)
1 /*
2  * R8A66597 HCD (Host Controller Driver) for u-boot
3  *
4  * Copyright (C) 2008  Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  */
20 
21 #include <common.h>
22 #include <usb.h>
23 #include <asm/io.h>
24 
25 #include "r8a66597.h"
26 
27 #ifdef R8A66597_DEBUG
28 #define R8A66597_DPRINT		printf
29 #else
30 #define R8A66597_DPRINT(...)
31 #endif
32 
33 static const char hcd_name[] = "r8a66597_hcd";
34 static unsigned short clock = CONFIG_R8A66597_XTAL;
35 static unsigned short vif = CONFIG_R8A66597_LDRV;
36 static unsigned short endian = CONFIG_R8A66597_ENDIAN;
37 static struct r8a66597 gr8a66597;
38 
39 static void get_hub_data(struct usb_device *dev, u16 *hub_devnum, u16 *hubport)
40 {
41 	int i;
42 
43 	*hub_devnum = 0;
44 	*hubport = 0;
45 
46 	/* check a device connected to root_hub */
47 	if ((dev->parent && dev->parent->devnum == 1) ||
48 	    (dev->devnum == 1))
49 		return;
50 
51 	for (i = 0; i < USB_MAXCHILDREN; i++) {
52 		if (dev->parent->children[i] == dev) {
53 			*hub_devnum = (u8)dev->parent->devnum;
54 			*hubport = i;
55 			return;
56 		}
57 	}
58 
59 	printf("get_hub_data error.\n");
60 }
61 
62 static void set_devadd(struct r8a66597 *r8a66597, u8 r8a66597_address,
63 			struct usb_device *dev, int port)
64 {
65 	u16 val, usbspd, upphub, hubport;
66 	unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
67 
68 	get_hub_data(dev, &upphub, &hubport);
69 	usbspd = r8a66597->speed;
70 	val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
71 	r8a66597_write(r8a66597, val, devadd_reg);
72 }
73 
74 static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
75 {
76 	u16 tmp;
77 	int i = 0;
78 
79 #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
80 	do {
81 		r8a66597_write(r8a66597, SCKE, SYSCFG0);
82 		tmp = r8a66597_read(r8a66597, SYSCFG0);
83 		if (i++ > 1000) {
84 			printf("register access fail.\n");
85 			return -1;
86 		}
87 	} while ((tmp & SCKE) != SCKE);
88 	r8a66597_write(r8a66597, 0x04, 0x02);
89 #else
90 	do {
91 		r8a66597_write(r8a66597, USBE, SYSCFG0);
92 		tmp = r8a66597_read(r8a66597, SYSCFG0);
93 		if (i++ > 1000) {
94 			printf("register access fail.\n");
95 			return -1;
96 		}
97 	} while ((tmp & USBE) != USBE);
98 	r8a66597_bclr(r8a66597, USBE, SYSCFG0);
99 	r8a66597_mdfy(r8a66597, clock, XTAL, SYSCFG0);
100 
101 	i = 0;
102 	r8a66597_bset(r8a66597, XCKE, SYSCFG0);
103 	do {
104 		udelay(1000);
105 		tmp = r8a66597_read(r8a66597, SYSCFG0);
106 		if (i++ > 500) {
107 			printf("register access fail.\n");
108 			return -1;
109 		}
110 	} while ((tmp & SCKE) != SCKE);
111 #endif	/* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */
112 
113 	return 0;
114 }
115 
116 static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
117 {
118 	r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
119 	udelay(1);
120 #if !defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
121 	r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
122 	r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
123 	r8a66597_bclr(r8a66597, USBE, SYSCFG0);
124 #endif
125 }
126 
127 static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
128 {
129 	u16 val;
130 
131 	val = port ? DRPD : DCFM | DRPD;
132 	r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
133 	r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
134 
135 	r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
136 }
137 
138 static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
139 {
140 	u16 val, tmp;
141 
142 	r8a66597_write(r8a66597, 0, get_intenb_reg(port));
143 	r8a66597_write(r8a66597, 0, get_intsts_reg(port));
144 
145 	r8a66597_port_power(r8a66597, port, 0);
146 
147 	do {
148 		tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
149 		udelay(640);
150 	} while (tmp == EDGESTS);
151 
152 	val = port ? DRPD : DCFM | DRPD;
153 	r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
154 	r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
155 }
156 
157 static int enable_controller(struct r8a66597 *r8a66597)
158 {
159 	int ret, port;
160 
161 	ret = r8a66597_clock_enable(r8a66597);
162 	if (ret < 0)
163 		return ret;
164 
165 	r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
166 	r8a66597_bset(r8a66597, USBE, SYSCFG0);
167 
168 	r8a66597_bset(r8a66597, INTL, SOFCFG);
169 	r8a66597_write(r8a66597, 0, INTENB0);
170 	r8a66597_write(r8a66597, 0, INTENB1);
171 	r8a66597_write(r8a66597, 0, INTENB2);
172 
173 	r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
174 	r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
175 	r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
176 	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 
184 static 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 
221 static 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 
236 static 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 
275 static 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;
283 	u16 devsel = setup->request == USB_REQ_SET_ADDRESS ? 0 : dev->devnum;
284 
285 	r8a66597_write(r8a66597, make_devsel(devsel) |
286 				 (8 << dev->maxpacketsize), DCPMAXP);
287 	r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
288 
289 	for (i = 0; i < 4; i++) {
290 		r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
291 		setup_addr += 2;
292 	}
293 	r8a66597_write(r8a66597, ~0x0001, BRDYSTS);
294 	r8a66597_write(r8a66597, SUREQ, DCPCTR);
295 
296 	while (1) {
297 		intsts1 = r8a66597_read(r8a66597, INTSTS1);
298 		if (intsts1 & SACK)
299 			break;
300 		if (intsts1 & SIGN) {
301 			printf("setup packet send error\n");
302 			return -1;
303 		}
304 		if (timeout-- < 0) {
305 			printf("setup packet timeout\n");
306 			return -1;
307 		}
308 		udelay(500);
309 	}
310 
311 	return 0;
312 }
313 
314 static int send_bulk_packet(struct r8a66597 *r8a66597, struct usb_device *dev,
315 			    unsigned long pipe, void *buffer, int transfer_len)
316 {
317 	u16 tmp, bufsize;
318 	u16 *buf;
319 	size_t size;
320 
321 	R8A66597_DPRINT("%s\n", __func__);
322 
323 	r8a66597_mdfy(r8a66597, MBW | BULK_OUT_PIPENUM,
324 			MBW | CURPIPE, CFIFOSEL);
325 	r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, BULK_OUT_PIPENUM);
326 	tmp = r8a66597_read(r8a66597, CFIFOCTR);
327 	if ((tmp & FRDY) == 0) {
328 		printf("%s FRDY is not set (%x)\n", __func__, tmp);
329 		return -1;
330 	}
331 
332 	/* prepare parameters */
333 	bufsize = dev->epmaxpacketout[usb_pipeendpoint(pipe)];
334 	buf = (u16 *)(buffer + dev->act_len);
335 	size = min((int)bufsize, transfer_len - dev->act_len);
336 
337 	/* write fifo */
338 	r8a66597_write(r8a66597, ~(1 << BULK_OUT_PIPENUM), BEMPSTS);
339 	if (buffer) {
340 		r8a66597_write_fifo(r8a66597, CFIFO, buf, size);
341 		r8a66597_write(r8a66597, BVAL, CFIFOCTR);
342 	}
343 
344 	/* update parameters */
345 	dev->act_len += size;
346 
347 	r8a66597_mdfy(r8a66597, PID_BUF, PID,
348 			get_pipectr_addr(BULK_OUT_PIPENUM));
349 
350 	while (!(r8a66597_read(r8a66597, BEMPSTS) & (1 << BULK_OUT_PIPENUM)))
351 		if (ctrlc())
352 			return -1;
353 	r8a66597_write(r8a66597, ~(1 << BULK_OUT_PIPENUM), BEMPSTS);
354 
355 	if (dev->act_len >= transfer_len)
356 		r8a66597_mdfy(r8a66597, PID_NAK, PID,
357 				get_pipectr_addr(BULK_OUT_PIPENUM));
358 
359 	return 0;
360 }
361 
362 static int receive_bulk_packet(struct r8a66597 *r8a66597,
363 			       struct usb_device *dev,
364 			       unsigned long pipe,
365 			       void *buffer, int transfer_len)
366 {
367 	u16 tmp;
368 	u16 *buf;
369 	const u16 pipenum = BULK_IN_PIPENUM;
370 	int rcv_len;
371 	int maxpacket = dev->epmaxpacketin[usb_pipeendpoint(pipe)];
372 
373 	R8A66597_DPRINT("%s\n", __func__);
374 
375 	/* prepare */
376 	if (dev->act_len == 0) {
377 		r8a66597_mdfy(r8a66597, PID_NAK, PID,
378 				get_pipectr_addr(pipenum));
379 		r8a66597_write(r8a66597, ~(1 << pipenum), BRDYSTS);
380 
381 		r8a66597_write(r8a66597, TRCLR, get_pipetre_addr(pipenum));
382 		r8a66597_write(r8a66597,
383 				(transfer_len + maxpacket - 1) / maxpacket,
384 				get_pipetrn_addr(pipenum));
385 		r8a66597_bset(r8a66597, TRENB, get_pipetre_addr(pipenum));
386 
387 		r8a66597_mdfy(r8a66597, PID_BUF, PID,
388 				get_pipectr_addr(pipenum));
389 	}
390 
391 	r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
392 	r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
393 
394 	while (!(r8a66597_read(r8a66597, BRDYSTS) & (1 << pipenum)))
395 		if (ctrlc())
396 			return -1;
397 	r8a66597_write(r8a66597, ~(1 << pipenum), BRDYSTS);
398 
399 	tmp = r8a66597_read(r8a66597, CFIFOCTR);
400 	if ((tmp & FRDY) == 0) {
401 		printf("%s FRDY is not set. (%x)\n", __func__, tmp);
402 		return -1;
403 	}
404 
405 	buf = (u16 *)(buffer + dev->act_len);
406 	rcv_len = tmp & DTLN;
407 	dev->act_len += rcv_len;
408 
409 	if (buffer) {
410 		if (rcv_len == 0)
411 			r8a66597_write(r8a66597, BCLR, CFIFOCTR);
412 		else
413 			r8a66597_read_fifo(r8a66597, CFIFO, buf, rcv_len);
414 	}
415 
416 	return 0;
417 }
418 
419 static int receive_control_packet(struct r8a66597 *r8a66597,
420 				  struct usb_device *dev,
421 				  void *buffer, int transfer_len)
422 {
423 	u16 tmp;
424 	int rcv_len;
425 
426 	/* FIXME: limit transfer size : 64byte or less */
427 
428 	r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
429 	r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
430 	r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
431 	r8a66597_bset(r8a66597, SQSET, DCPCTR);
432 	r8a66597_write(r8a66597, BCLR, CFIFOCTR);
433 	r8a66597_mdfy(r8a66597, PID_BUF, PID, DCPCTR);
434 
435 	while (!(r8a66597_read(r8a66597, BRDYSTS) & 0x0001))
436 		if (ctrlc())
437 			return -1;
438 	r8a66597_write(r8a66597, ~0x0001, BRDYSTS);
439 
440 	r8a66597_mdfy(r8a66597, MBW, MBW | CURPIPE, CFIFOSEL);
441 	r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
442 
443 	tmp = r8a66597_read(r8a66597, CFIFOCTR);
444 	if ((tmp & FRDY) == 0) {
445 		printf("%s FRDY is not set. (%x)\n", __func__, tmp);
446 		return -1;
447 	}
448 
449 	rcv_len = tmp & DTLN;
450 	dev->act_len += rcv_len;
451 
452 	r8a66597_mdfy(r8a66597, PID_NAK, PID, DCPCTR);
453 
454 	if (buffer) {
455 		if (rcv_len == 0)
456 			r8a66597_write(r8a66597, BCLR, DCPCTR);
457 		else
458 			r8a66597_read_fifo(r8a66597, CFIFO, buffer, rcv_len);
459 	}
460 
461 	return 0;
462 }
463 
464 static int send_status_packet(struct r8a66597 *r8a66597,
465 			       unsigned long pipe)
466 {
467 	r8a66597_bset(r8a66597, SQSET, DCPCTR);
468 	r8a66597_mdfy(r8a66597, PID_NAK, PID, DCPCTR);
469 
470 	if (usb_pipein(pipe)) {
471 		r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
472 		r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
473 		r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
474 		r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
475 		r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
476 	} else {
477 		r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
478 		r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
479 		r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
480 		r8a66597_write(r8a66597, BCLR, CFIFOCTR);
481 	}
482 	r8a66597_mdfy(r8a66597, PID_BUF, PID, DCPCTR);
483 
484 	while (!(r8a66597_read(r8a66597, BEMPSTS) & 0x0001))
485 		if (ctrlc())
486 			return -1;
487 
488 	return 0;
489 }
490 
491 static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port)
492 {
493 	int count = R8A66597_MAX_SAMPLING;
494 	unsigned short syssts, old_syssts;
495 
496 	R8A66597_DPRINT("%s\n", __func__);
497 
498 	old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port) & LNST);
499 	while (count > 0) {
500 		mdelay(R8A66597_RH_POLL_TIME);
501 
502 		syssts = r8a66597_read(r8a66597, get_syssts_reg(port) & LNST);
503 		if (syssts == old_syssts) {
504 			count--;
505 		} else {
506 			count = R8A66597_MAX_SAMPLING;
507 			old_syssts = syssts;
508 		}
509 	}
510 }
511 
512 static void r8a66597_bus_reset(struct r8a66597 *r8a66597, int port)
513 {
514 	mdelay(10);
515 	r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT, get_dvstctr_reg(port));
516 	mdelay(50);
517 	r8a66597_mdfy(r8a66597, UACT, USBRST | UACT, get_dvstctr_reg(port));
518 	mdelay(50);
519 }
520 
521 static int check_usb_device_connecting(struct r8a66597 *r8a66597)
522 {
523 	int timeout = 10000;	/* 100usec * 10000 = 1sec */
524 	int i;
525 
526 	for (i = 0; i < 5; i++) {
527 		/* check a usb cable connect */
528 		while (!(r8a66597_read(r8a66597, INTSTS1) & ATTCH)) {
529 			if (timeout-- < 0) {
530 				printf("%s timeout.\n", __func__);
531 				return -1;
532 			}
533 			udelay(100);
534 		}
535 
536 		/* check a data line */
537 		r8a66597_check_syssts(r8a66597, 0);
538 
539 		r8a66597_bus_reset(r8a66597, 0);
540 		r8a66597->speed = get_rh_usb_speed(r8a66597, 0);
541 
542 		if (!(r8a66597_read(r8a66597, INTSTS1) & DTCH)) {
543 			r8a66597->port_change = USB_PORT_STAT_C_CONNECTION;
544 			r8a66597->port_status = USB_PORT_STAT_CONNECTION |
545 						USB_PORT_STAT_ENABLE;
546 			return 0;	/* success */
547 		}
548 
549 		R8A66597_DPRINT("USB device has detached. retry = %d\n", i);
550 		r8a66597_write(r8a66597, ~DTCH, INTSTS1);
551 	}
552 
553 	return -1;	/* fail */
554 }
555 
556 /* based on usb_ohci.c */
557 #define min_t(type, x, y) \
558 		({ type __x = (x); type __y = (y); __x < __y ? __x : __y; })
559 /*-------------------------------------------------------------------------*
560  * Virtual Root Hub
561  *-------------------------------------------------------------------------*/
562 
563 /* Device descriptor */
564 static __u8 root_hub_dev_des[] =
565 {
566 	0x12,	    /*	__u8  bLength; */
567 	0x01,	    /*	__u8  bDescriptorType; Device */
568 	0x10,	    /*	__u16 bcdUSB; v1.1 */
569 	0x01,
570 	0x09,	    /*	__u8  bDeviceClass; HUB_CLASSCODE */
571 	0x00,	    /*	__u8  bDeviceSubClass; */
572 	0x00,	    /*	__u8  bDeviceProtocol; */
573 	0x08,	    /*	__u8  bMaxPacketSize0; 8 Bytes */
574 	0x00,	    /*	__u16 idVendor; */
575 	0x00,
576 	0x00,	    /*	__u16 idProduct; */
577 	0x00,
578 	0x00,	    /*	__u16 bcdDevice; */
579 	0x00,
580 	0x00,	    /*	__u8  iManufacturer; */
581 	0x01,	    /*	__u8  iProduct; */
582 	0x00,	    /*	__u8  iSerialNumber; */
583 	0x01	    /*	__u8  bNumConfigurations; */
584 };
585 
586 /* Configuration descriptor */
587 static __u8 root_hub_config_des[] =
588 {
589 	0x09,	    /*	__u8  bLength; */
590 	0x02,	    /*	__u8  bDescriptorType; Configuration */
591 	0x19,	    /*	__u16 wTotalLength; */
592 	0x00,
593 	0x01,	    /*	__u8  bNumInterfaces; */
594 	0x01,	    /*	__u8  bConfigurationValue; */
595 	0x00,	    /*	__u8  iConfiguration; */
596 	0x40,	    /*	__u8  bmAttributes; */
597 
598 	0x00,	    /*	__u8  MaxPower; */
599 
600 	/* interface */
601 	0x09,	    /*	__u8  if_bLength; */
602 	0x04,	    /*	__u8  if_bDescriptorType; Interface */
603 	0x00,	    /*	__u8  if_bInterfaceNumber; */
604 	0x00,	    /*	__u8  if_bAlternateSetting; */
605 	0x01,	    /*	__u8  if_bNumEndpoints; */
606 	0x09,	    /*	__u8  if_bInterfaceClass; HUB_CLASSCODE */
607 	0x00,	    /*	__u8  if_bInterfaceSubClass; */
608 	0x00,	    /*	__u8  if_bInterfaceProtocol; */
609 	0x00,	    /*	__u8  if_iInterface; */
610 
611 	/* endpoint */
612 	0x07,	    /*	__u8  ep_bLength; */
613 	0x05,	    /*	__u8  ep_bDescriptorType; Endpoint */
614 	0x81,	    /*	__u8  ep_bEndpointAddress; IN Endpoint 1 */
615 	0x03,	    /*	__u8  ep_bmAttributes; Interrupt */
616 	0x02,	    /*	__u16 ep_wMaxPacketSize; ((MAX_ROOT_PORTS + 1) / 8 */
617 	0x00,
618 	0xff	    /*	__u8  ep_bInterval; 255 ms */
619 };
620 
621 static unsigned char root_hub_str_index0[] =
622 {
623 	0x04,			/*  __u8  bLength; */
624 	0x03,			/*  __u8  bDescriptorType; String-descriptor */
625 	0x09,			/*  __u8  lang ID */
626 	0x04,			/*  __u8  lang ID */
627 };
628 
629 static unsigned char root_hub_str_index1[] =
630 {
631 	34,			/*  __u8  bLength; */
632 	0x03,			/*  __u8  bDescriptorType; String-descriptor */
633 	'R',			/*  __u8  Unicode */
634 	0,				/*  __u8  Unicode */
635 	'8',			/*  __u8  Unicode */
636 	0,				/*  __u8  Unicode */
637 	'A',			/*  __u8  Unicode */
638 	0,				/*  __u8  Unicode */
639 	'6',			/*  __u8  Unicode */
640 	0,				/*  __u8  Unicode */
641 	'6',			/*  __u8  Unicode */
642 	0,				/*  __u8  Unicode */
643 	'5',			/*  __u8  Unicode */
644 	0,				/*  __u8  Unicode */
645 	'9',			/*  __u8  Unicode */
646 	0,				/*  __u8  Unicode */
647 	'7',			/*  __u8  Unicode */
648 	0,				/*  __u8  Unicode */
649 	' ',			/*  __u8  Unicode */
650 	0,				/*  __u8  Unicode */
651 	'R',			/*  __u8  Unicode */
652 	0,				/*  __u8  Unicode */
653 	'o',			/*  __u8  Unicode */
654 	0,				/*  __u8  Unicode */
655 	'o',			/*  __u8  Unicode */
656 	0,				/*  __u8  Unicode */
657 	't',			/*  __u8  Unicode */
658 	0,				/*  __u8  Unicode */
659 	'H',			/*  __u8  Unicode */
660 	0,				/*  __u8  Unicode */
661 	'u',			/*  __u8  Unicode */
662 	0,				/*  __u8  Unicode */
663 	'b',			/*  __u8  Unicode */
664 	0,				/*  __u8  Unicode */
665 };
666 
667 static int r8a66597_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
668 			void *buffer, int transfer_len, struct devrequest *cmd)
669 {
670 	struct r8a66597 *r8a66597 = &gr8a66597;
671 	int leni = transfer_len;
672 	int len = 0;
673 	int stat = 0;
674 	__u16 bmRType_bReq;
675 	__u16 wValue;
676 	__u16 wIndex;
677 	__u16 wLength;
678 	unsigned char data[32];
679 
680 	R8A66597_DPRINT("%s\n", __func__);
681 
682 	if (usb_pipeint(pipe)) {
683 		printf("Root-Hub submit IRQ: NOT implemented");
684 		return 0;
685 	}
686 
687 	bmRType_bReq  = cmd->requesttype | (cmd->request << 8);
688 	wValue	      = cpu_to_le16 (cmd->value);
689 	wIndex	      = cpu_to_le16 (cmd->index);
690 	wLength	      = cpu_to_le16 (cmd->length);
691 
692 	switch (bmRType_bReq) {
693 	case RH_GET_STATUS:
694 		*(__u16 *)buffer = cpu_to_le16(1);
695 		len = 2;
696 		break;
697 	case RH_GET_STATUS | RH_INTERFACE:
698 		*(__u16 *)buffer = cpu_to_le16(0);
699 		len = 2;
700 		break;
701 	case RH_GET_STATUS | RH_ENDPOINT:
702 		*(__u16 *)buffer = cpu_to_le16(0);
703 		len = 2;
704 		break;
705 	case RH_GET_STATUS | RH_CLASS:
706 		*(__u32 *)buffer = cpu_to_le32(0);
707 		len = 4;
708 		break;
709 	case RH_GET_STATUS | RH_OTHER | RH_CLASS:
710 		*(__u32 *)buffer = cpu_to_le32(r8a66597->port_status |
711 						(r8a66597->port_change << 16));
712 		len = 4;
713 		break;
714 	case RH_CLEAR_FEATURE | RH_ENDPOINT:
715 	case RH_CLEAR_FEATURE | RH_CLASS:
716 		break;
717 
718 	case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
719 		switch (wValue) {
720 		case RH_C_PORT_CONNECTION:
721 			r8a66597->port_change &= ~USB_PORT_STAT_C_CONNECTION;
722 			break;
723 		}
724 		break;
725 
726 	case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
727 		switch (wValue) {
728 		case (RH_PORT_SUSPEND):
729 			break;
730 		case (RH_PORT_RESET):
731 			r8a66597_bus_reset(r8a66597, 0);
732 			break;
733 		case (RH_PORT_POWER):
734 			break;
735 		case (RH_PORT_ENABLE):
736 			break;
737 		}
738 		break;
739 	case RH_SET_ADDRESS:
740 		gr8a66597.rh_devnum = wValue;
741 		break;
742 	case RH_GET_DESCRIPTOR:
743 		switch ((wValue & 0xff00) >> 8) {
744 		case (0x01): /* device descriptor */
745 			len = min_t(unsigned int,
746 				  leni,
747 				  min_t(unsigned int,
748 				      sizeof(root_hub_dev_des),
749 				      wLength));
750 			memcpy(buffer, root_hub_dev_des, len);
751 			break;
752 		case (0x02): /* configuration descriptor */
753 			len = min_t(unsigned int,
754 				  leni,
755 				  min_t(unsigned int,
756 				      sizeof(root_hub_config_des),
757 				      wLength));
758 			memcpy(buffer, root_hub_config_des, len);
759 			break;
760 		case (0x03): /* string descriptors */
761 			if (wValue == 0x0300) {
762 				len = min_t(unsigned int,
763 					  leni,
764 					  min_t(unsigned int,
765 					      sizeof(root_hub_str_index0),
766 					      wLength));
767 				memcpy(buffer, root_hub_str_index0, len);
768 			}
769 			if (wValue == 0x0301) {
770 				len = min_t(unsigned int,
771 					  leni,
772 					  min_t(unsigned int,
773 					      sizeof(root_hub_str_index1),
774 					      wLength));
775 				memcpy(buffer, root_hub_str_index1, len);
776 			}
777 			break;
778 		default:
779 			stat = USB_ST_STALLED;
780 		}
781 		break;
782 
783 	case RH_GET_DESCRIPTOR | RH_CLASS:
784 	{
785 		__u32 temp = 0x00000001;
786 
787 		data[0] = 9;		/* min length; */
788 		data[1] = 0x29;
789 		data[2] = temp & RH_A_NDP;
790 		data[3] = 0;
791 		if (temp & RH_A_PSM)
792 			data[3] |= 0x1;
793 		if (temp & RH_A_NOCP)
794 			data[3] |= 0x10;
795 		else if (temp & RH_A_OCPM)
796 			data[3] |= 0x8;
797 
798 		/* corresponds to data[4-7] */
799 		data[5] = (temp & RH_A_POTPGT) >> 24;
800 		data[7] = temp & RH_B_DR;
801 		if (data[2] < 7) {
802 			data[8] = 0xff;
803 		} else {
804 			data[0] += 2;
805 			data[8] = (temp & RH_B_DR) >> 8;
806 			data[10] = data[9] = 0xff;
807 		}
808 
809 		len = min_t(unsigned int, leni,
810 			    min_t(unsigned int, data[0], wLength));
811 		memcpy(buffer, data, len);
812 		break;
813 	}
814 
815 	case RH_GET_CONFIGURATION:
816 		*(__u8 *) buffer = 0x01;
817 		len = 1;
818 		break;
819 	case RH_SET_CONFIGURATION:
820 		break;
821 	default:
822 		R8A66597_DPRINT("unsupported root hub command");
823 		stat = USB_ST_STALLED;
824 	}
825 
826 	mdelay(1);
827 
828 	len = min_t(int, len, leni);
829 
830 	dev->act_len = len;
831 	dev->status = stat;
832 
833 	return stat;
834 }
835 
836 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
837 		    int transfer_len)
838 {
839 	struct r8a66597 *r8a66597 = &gr8a66597;
840 	int ret = 0;
841 
842 	R8A66597_DPRINT("%s\n", __func__);
843 	R8A66597_DPRINT("pipe = %08x, buffer = %p, len = %d, devnum = %d\n",
844 			pipe, buffer, transfer_len, dev->devnum);
845 
846 	set_devadd(r8a66597, dev->devnum, dev, 0);
847 
848 	pipe_buffer_setting(r8a66597, dev, pipe);
849 
850 	dev->act_len = 0;
851 	while (dev->act_len < transfer_len && ret == 0) {
852 		if (ctrlc())
853 			return -1;
854 
855 		if (usb_pipein(pipe))
856 			ret = receive_bulk_packet(r8a66597, dev, pipe, buffer,
857 							transfer_len);
858 		else
859 			ret = send_bulk_packet(r8a66597, dev, pipe, buffer,
860 							transfer_len);
861 	}
862 
863 	if (ret == 0)
864 		dev->status = 0;
865 
866 	return ret;
867 }
868 
869 int submit_control_msg(struct usb_device *dev, unsigned long pipe,
870 		       void *buffer, int transfer_len, struct devrequest *setup)
871 {
872 	struct r8a66597 *r8a66597 = &gr8a66597;
873 	u16 r8a66597_address = setup->request == USB_REQ_SET_ADDRESS ?
874 					0 : dev->devnum;
875 
876 	R8A66597_DPRINT("%s\n", __func__);
877 	if (usb_pipedevice(pipe) == r8a66597->rh_devnum)
878 		return r8a66597_submit_rh_msg(dev, pipe, buffer, transfer_len,
879 						setup);
880 
881 	R8A66597_DPRINT("%s: setup\n", __func__);
882 	set_devadd(r8a66597, r8a66597_address, dev, 0);
883 
884 	if (send_setup_packet(r8a66597, dev, setup) < 0) {
885 		printf("setup packet send error\n");
886 		return -1;
887 	}
888 
889 	dev->act_len = 0;
890 	if (usb_pipein(pipe))
891 		if (receive_control_packet(r8a66597, dev, buffer,
892 						transfer_len) < 0)
893 			return -1;
894 
895 	if (send_status_packet(r8a66597, pipe) < 0)
896 		return -1;
897 
898 	dev->status = 0;
899 
900 	return 0;
901 }
902 
903 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
904 			int transfer_len, int interval)
905 {
906 	/* no implement */
907 	R8A66597_DPRINT("%s\n", __func__);
908 	return 0;
909 }
910 
911 int usb_lowlevel_init(void)
912 {
913 	struct r8a66597 *r8a66597 = &gr8a66597;
914 
915 	R8A66597_DPRINT("%s\n", __func__);
916 
917 	memset(r8a66597, 0, sizeof(r8a66597));
918 	r8a66597->reg = CONFIG_R8A66597_BASE_ADDR;
919 
920 	disable_controller(r8a66597);
921 	mdelay(100);
922 
923 	enable_controller(r8a66597);
924 	r8a66597_port_power(r8a66597, 0 , 1);
925 
926 	/* check usb device */
927 	check_usb_device_connecting(r8a66597);
928 
929 	mdelay(50);
930 
931 	return 0;
932 }
933 
934 int usb_lowlevel_stop(void)
935 {
936 	disable_controller(&gr8a66597);
937 
938 	return 0;
939 }
940