2 * R8A66597 HCD (Host Controller Driver)
4 * Copyright (C) 2006-2007 Renesas Solutions Corp.
5 * Portions Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
6 * Portions Copyright (C) 2004-2005 David Brownell
7 * Portions Copyright (C) 1999 Roman Weissgaerber
9 * Author : Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/smp_lock.h>
30 #include <linux/errno.h>
31 #include <linux/init.h>
32 #include <linux/timer.h>
33 #include <linux/delay.h>
34 #include <linux/list.h>
35 #include <linux/interrupt.h>
36 #include <linux/usb.h>
37 #include <linux/platform_device.h>
41 #include <asm/system.h>
43 #include "../core/hcd.h"
46 MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
47 MODULE_LICENSE("GPL");
48 MODULE_AUTHOR("Yoshihiro Shimoda");
50 #define DRIVER_VERSION "9 May 2007"
52 static const char hcd_name[] = "r8a66597_hcd";
54 /* module parameters */
55 static unsigned short clock = XTAL12;
56 module_param(clock, ushort, 0644);
57 MODULE_PARM_DESC(clock, "input clock: 48MHz=32768, 24MHz=16384, 12MHz=0(default=0)");
58 static unsigned short vif = LDRV;
59 module_param(vif, ushort, 0644);
60 MODULE_PARM_DESC(vif, "input VIF: 3.3V=32768, 1.5V=0(default=32768)");
61 static unsigned short endian = 0;
62 module_param(endian, ushort, 0644);
63 MODULE_PARM_DESC(endian, "data endian: big=256, little=0(default=0)");
64 static unsigned short irq_sense = INTL;
65 module_param(irq_sense, ushort, 0644);
66 MODULE_PARM_DESC(irq_sense, "IRQ sense: low level=32, falling edge=0(default=32)");
68 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
69 static int r8a66597_get_frame(struct usb_hcd *hcd);
71 /* this function must be called with interrupt disabled */
72 static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
77 tmp = r8a66597_read(r8a66597, INTENB0);
78 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
79 r8a66597_bset(r8a66597, 1 << pipenum, reg);
80 r8a66597_write(r8a66597, tmp, INTENB0);
83 /* this function must be called with interrupt disabled */
84 static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
89 tmp = r8a66597_read(r8a66597, INTENB0);
90 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
91 r8a66597_bclr(r8a66597, 1 << pipenum, reg);
92 r8a66597_write(r8a66597, tmp, INTENB0);
95 static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
96 u16 usbspd, u8 upphub, u8 hubport, int port)
99 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
101 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
102 r8a66597_write(r8a66597, val, devadd_reg);
105 static int enable_controller(struct r8a66597 *r8a66597)
111 r8a66597_write(r8a66597, USBE, SYSCFG0);
112 tmp = r8a66597_read(r8a66597, SYSCFG0);
114 err("register access fail.");
117 } while ((tmp & USBE) != USBE);
118 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
119 r8a66597_mdfy(r8a66597, clock, XTAL, SYSCFG0);
122 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
125 tmp = r8a66597_read(r8a66597, SYSCFG0);
127 err("register access fail.");
130 } while ((tmp & SCKE) != SCKE);
132 r8a66597_bset(r8a66597, DCFM | DRPD, SYSCFG0);
133 r8a66597_bset(r8a66597, DRPD, SYSCFG1);
135 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
136 r8a66597_bset(r8a66597, HSE, SYSCFG0);
137 r8a66597_bset(r8a66597, HSE, SYSCFG1);
138 r8a66597_bset(r8a66597, USBE, SYSCFG0);
140 r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
141 r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
142 r8a66597_bset(r8a66597, BRDY0, BRDYENB);
143 r8a66597_bset(r8a66597, BEMP0, BEMPENB);
145 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, DMA0CFG);
146 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, DMA1CFG);
148 r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
149 r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
150 r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
152 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
154 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
155 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
156 r8a66597_bset(r8a66597, ATTCHE, INTENB1);
157 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
158 r8a66597_bset(r8a66597, ATTCHE, INTENB2);
163 static void disable_controller(struct r8a66597 *r8a66597)
167 r8a66597_write(r8a66597, 0, INTENB0);
168 r8a66597_write(r8a66597, 0, INTENB1);
169 r8a66597_write(r8a66597, 0, INTENB2);
170 r8a66597_write(r8a66597, 0, INTSTS0);
171 r8a66597_write(r8a66597, 0, INTSTS1);
172 r8a66597_write(r8a66597, 0, INTSTS2);
174 r8a66597_port_power(r8a66597, 0, 0);
175 r8a66597_port_power(r8a66597, 1, 0);
178 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
180 } while (tmp == EDGESTS);
182 r8a66597_bclr(r8a66597, DCFM | DRPD, SYSCFG0);
183 r8a66597_bclr(r8a66597, DRPD, SYSCFG1);
184 r8a66597_bclr(r8a66597, HSE, SYSCFG0);
185 r8a66597_bclr(r8a66597, HSE, SYSCFG1);
187 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
189 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
190 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
191 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
194 static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
195 struct usb_device *udev)
197 struct r8a66597_device *dev;
199 if (udev->parent && udev->parent->devnum != 1)
202 dev = dev_get_drvdata(&udev->dev);
209 static int is_child_device(char *devpath)
211 return (devpath[2] ? 1 : 0);
214 static int is_hub_limit(char *devpath)
216 return ((strlen(devpath) >= 4) ? 1 : 0);
219 static void get_port_number(char *devpath, u16 *root_port, u16 *hub_port)
222 *root_port = (devpath[0] & 0x0F) - 1;
223 if (*root_port >= R8A66597_MAX_ROOT_HUB)
224 err("illegal root port number");
227 *hub_port = devpath[2] & 0x0F;
230 static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
245 err("unknown speed");
252 static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
257 r8a66597->child_connect_map[idx] |= 1 << (address % 32);
260 static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
265 r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
268 static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
270 u16 pipenum = pipe->info.pipenum;
271 unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
272 unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
273 unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
275 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
276 dma_ch = R8A66597_PIPE_NO_DMA;
278 pipe->fifoaddr = fifoaddr[dma_ch];
279 pipe->fifosel = fifosel[dma_ch];
280 pipe->fifoctr = fifoctr[dma_ch];
283 pipe->pipectr = DCPCTR;
285 pipe->pipectr = get_pipectr_addr(pipenum);
287 if (check_bulk_or_isoc(pipenum)) {
288 pipe->pipetre = get_pipetre_addr(pipenum);
289 pipe->pipetrn = get_pipetrn_addr(pipenum);
296 static struct r8a66597_device *
297 get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
299 if (usb_pipedevice(urb->pipe) == 0)
300 return &r8a66597->device0;
302 return dev_get_drvdata(&urb->dev->dev);
305 static int make_r8a66597_device(struct r8a66597 *r8a66597,
306 struct urb *urb, u8 addr)
308 struct r8a66597_device *dev;
309 int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
311 dev = kzalloc(sizeof(struct r8a66597_device), GFP_KERNEL);
315 dev_set_drvdata(&urb->dev->dev, dev);
316 dev->udev = urb->dev;
318 dev->usb_address = usb_address;
319 dev->state = USB_STATE_ADDRESS;
320 dev->ep_in_toggle = 0;
321 dev->ep_out_toggle = 0;
322 INIT_LIST_HEAD(&dev->device_list);
323 list_add_tail(&dev->device_list, &r8a66597->child_device);
325 get_port_number(urb->dev->devpath, &dev->root_port, &dev->hub_port);
326 if (!is_child_device(urb->dev->devpath))
327 r8a66597->root_hub[dev->root_port].dev = dev;
329 set_devadd_reg(r8a66597, dev->address,
330 get_r8a66597_usb_speed(urb->dev->speed),
331 get_parent_r8a66597_address(r8a66597, urb->dev),
332 dev->hub_port, dev->root_port);
337 /* this function must be called with interrupt disabled */
338 static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
340 u8 addr; /* R8A66597's address */
341 struct r8a66597_device *dev;
343 if (is_hub_limit(urb->dev->devpath)) {
344 err("Externel hub limit reached.");
348 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
349 if (dev && dev->state >= USB_STATE_ADDRESS)
352 for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
353 if (r8a66597->address_map & (1 << addr))
356 dbg("alloc_address: r8a66597_addr=%d", addr);
357 r8a66597->address_map |= 1 << addr;
359 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
365 err("cannot communicate with a USB device more than 10.(%x)",
366 r8a66597->address_map);
371 /* this function must be called with interrupt disabled */
372 static void free_usb_address(struct r8a66597 *r8a66597,
373 struct r8a66597_device *dev)
380 dbg("free_addr: addr=%d", dev->address);
382 dev->state = USB_STATE_DEFAULT;
383 r8a66597->address_map &= ~(1 << dev->address);
385 dev_set_drvdata(&dev->udev->dev, NULL);
386 list_del(&dev->device_list);
389 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
390 if (r8a66597->root_hub[port].dev == dev) {
391 r8a66597->root_hub[port].dev = NULL;
397 static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
404 tmp = r8a66597_read(r8a66597, reg);
406 err("register%lx, loop %x is timeout", reg, loop);
410 } while ((tmp & mask) != loop);
413 /* this function must be called with interrupt disabled */
414 static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
418 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
419 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
420 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
421 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
424 /* this function must be called with interrupt disabled */
425 static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
429 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
430 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
431 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
432 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
433 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
436 /* this function must be called with interrupt disabled */
437 static void clear_all_buffer(struct r8a66597 *r8a66597,
438 struct r8a66597_pipe *pipe)
442 if (!pipe || pipe->info.pipenum == 0)
445 pipe_stop(r8a66597, pipe);
446 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
447 tmp = r8a66597_read(r8a66597, pipe->pipectr);
448 tmp = r8a66597_read(r8a66597, pipe->pipectr);
449 tmp = r8a66597_read(r8a66597, pipe->pipectr);
450 r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
453 /* this function must be called with interrupt disabled */
454 static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
455 struct r8a66597_pipe *pipe, int toggle)
458 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
460 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
463 /* this function must be called with interrupt disabled */
464 static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
466 r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
467 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
470 /* this function must be called with interrupt disabled */
471 static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
472 struct r8a66597_pipe *pipe)
474 cfifo_change(r8a66597, 0);
475 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D0FIFOSEL);
476 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D1FIFOSEL);
478 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum, MBW | CURPIPE,
480 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
483 static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
485 struct r8a66597_pipe *pipe = hep->hcpriv;
487 if (usb_pipeendpoint(urb->pipe) == 0)
490 return pipe->info.pipenum;
493 static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
495 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
497 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
500 static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
506 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
509 /* this function must be called with interrupt disabled */
510 static void pipe_toggle_set(struct r8a66597 *r8a66597,
511 struct r8a66597_pipe *pipe,
512 struct urb *urb, int set)
514 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
515 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
516 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
522 *toggle |= 1 << endpoint;
524 *toggle &= ~(1 << endpoint);
527 /* this function must be called with interrupt disabled */
528 static void pipe_toggle_save(struct r8a66597 *r8a66597,
529 struct r8a66597_pipe *pipe,
532 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
533 pipe_toggle_set(r8a66597, pipe, urb, 1);
535 pipe_toggle_set(r8a66597, pipe, urb, 0);
538 /* this function must be called with interrupt disabled */
539 static void pipe_toggle_restore(struct r8a66597 *r8a66597,
540 struct r8a66597_pipe *pipe,
543 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
544 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
545 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
547 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
550 /* this function must be called with interrupt disabled */
551 static void pipe_buffer_setting(struct r8a66597 *r8a66597,
552 struct r8a66597_pipe_info *info)
556 if (info->pipenum == 0)
559 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
560 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
561 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
564 if (info->type == R8A66597_BULK && info->dir_in)
565 val |= R8A66597_DBLB | R8A66597_SHTNAK;
566 val |= info->type | info->epnum;
567 r8a66597_write(r8a66597, val, PIPECFG);
569 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
571 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
575 r8a66597_write(r8a66597, info->interval, PIPEPERI);
580 /* this function must be called with interrupt disabled */
581 static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
583 struct r8a66597_pipe_info *info;
584 struct urb *urb = td->urb;
586 if (td->pipenum > 0) {
587 info = &td->pipe->info;
588 cfifo_change(r8a66597, 0);
589 pipe_buffer_setting(r8a66597, info);
591 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
592 usb_pipeout(urb->pipe)) &&
593 !usb_pipecontrol(urb->pipe)) {
594 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
595 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
596 clear_all_buffer(r8a66597, td->pipe);
597 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
598 usb_pipeout(urb->pipe), 1);
600 pipe_toggle_restore(r8a66597, td->pipe, urb);
604 /* this function must be called with interrupt disabled */
605 static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
606 struct usb_endpoint_descriptor *ep)
608 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
610 memset(array, 0, sizeof(array));
611 switch(ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
612 case USB_ENDPOINT_XFER_BULK:
613 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
620 case USB_ENDPOINT_XFER_INT:
621 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
628 case USB_ENDPOINT_XFER_ISOC:
629 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
641 while (array[i] != 0) {
642 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
650 static u16 get_r8a66597_type(__u8 type)
655 case USB_ENDPOINT_XFER_BULK:
656 r8a66597_type = R8A66597_BULK;
658 case USB_ENDPOINT_XFER_INT:
659 r8a66597_type = R8A66597_INT;
661 case USB_ENDPOINT_XFER_ISOC:
662 r8a66597_type = R8A66597_ISO;
666 r8a66597_type = 0x0000;
670 return r8a66597_type;
673 static u16 get_bufnum(u16 pipenum)
679 else if (check_bulk_or_isoc(pipenum))
680 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
681 else if (check_interrupt(pipenum))
682 bufnum = 4 + (pipenum - 6);
684 err("Illegal pipenum (%d)", pipenum);
689 static u16 get_buf_bsize(u16 pipenum)
695 else if (check_bulk_or_isoc(pipenum))
696 buf_bsize = R8A66597_BUF_BSIZE - 1;
697 else if (check_interrupt(pipenum))
700 err("Illegal pipenum (%d)", pipenum);
705 /* this function must be called with interrupt disabled */
706 static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
707 struct r8a66597_device *dev,
708 struct r8a66597_pipe *pipe,
712 struct r8a66597_pipe_info *info = &pipe->info;
714 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
715 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
716 if ((r8a66597->dma_map & (1 << i)) != 0)
719 info("address %d, EndpointAddress 0x%02x use DMA FIFO",
720 usb_pipedevice(urb->pipe),
721 info->dir_in ? USB_ENDPOINT_DIR_MASK + info->epnum
724 r8a66597->dma_map |= 1 << i;
725 dev->dma_map |= 1 << i;
726 set_pipe_reg_addr(pipe, i);
728 cfifo_change(r8a66597, 0);
729 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum,
730 MBW | CURPIPE, pipe->fifosel);
732 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
734 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
740 /* this function must be called with interrupt disabled */
741 static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
742 struct usb_host_endpoint *hep,
743 struct r8a66597_pipe_info *info)
745 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
746 struct r8a66597_pipe *pipe = hep->hcpriv;
751 set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
752 r8a66597->pipe_cnt[pipe->info.pipenum]++;
753 dev->pipe_cnt[pipe->info.pipenum]++;
755 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
758 /* this function must be called with interrupt disabled */
759 static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
761 struct r8a66597_td *td, *next;
763 struct list_head *list = &r8a66597->pipe_queue[pipenum];
765 if (list_empty(list))
768 list_for_each_entry_safe(td, next, list, queue) {
771 if (td->address != address)
775 list_del(&td->queue);
779 urb->status = -ENODEV;
781 spin_unlock(&r8a66597->lock);
782 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb);
783 spin_lock(&r8a66597->lock);
789 /* this function must be called with interrupt disabled */
790 static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
791 struct r8a66597_device *dev)
799 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
800 if (!dev->pipe_cnt[pipenum])
805 force_dequeue(r8a66597, 0, dev->address);
808 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
809 dev->pipe_cnt[pipenum] = 0;
810 force_dequeue(r8a66597, pipenum, dev->address);
815 r8a66597->dma_map &= ~(dev->dma_map);
819 /* this function must be called with interrupt disabled */
820 static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
821 struct usb_host_endpoint *hep,
822 struct usb_endpoint_descriptor *ep)
824 struct r8a66597_pipe_info info;
826 info.pipenum = get_empty_pipenum(r8a66597, ep);
827 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
828 info.epnum = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
829 info.maxpacket = ep->wMaxPacketSize;
830 info.type = get_r8a66597_type(ep->bmAttributes
831 & USB_ENDPOINT_XFERTYPE_MASK);
832 info.bufnum = get_bufnum(info.pipenum);
833 info.buf_bsize = get_buf_bsize(info.pipenum);
834 info.interval = ep->bInterval;
835 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
840 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
843 static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
845 struct r8a66597_device *dev;
847 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
848 dev->state = USB_STATE_CONFIGURED;
851 static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
854 if (pipenum == 0 && usb_pipeout(urb->pipe))
855 enable_irq_empty(r8a66597, pipenum);
857 enable_irq_ready(r8a66597, pipenum);
859 if (!usb_pipeisoc(urb->pipe))
860 enable_irq_nrdy(r8a66597, pipenum);
863 static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
865 disable_irq_ready(r8a66597, pipenum);
866 disable_irq_nrdy(r8a66597, pipenum);
869 /* this function must be called with interrupt disabled */
870 static void r8a66597_usb_preconnect(struct r8a66597 *r8a66597, int port)
872 r8a66597->root_hub[port].port |= (1 << USB_PORT_FEAT_CONNECTION)
873 | (1 << USB_PORT_FEAT_C_CONNECTION);
874 r8a66597_write(r8a66597, (u16)~DTCH, get_intsts_reg(port));
875 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
878 /* this function must be called with interrupt disabled */
879 static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
881 u16 speed = get_rh_usb_speed(r8a66597, port);
882 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
885 rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
886 else if (speed == LSMODE)
887 rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
889 rh->port &= ~(1 << USB_PORT_FEAT_RESET);
890 rh->port |= 1 << USB_PORT_FEAT_ENABLE;
893 /* this function must be called with interrupt disabled */
894 static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
896 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
898 r8a66597->root_hub[port].port &= ~(1 << USB_PORT_FEAT_CONNECTION);
899 r8a66597->root_hub[port].port |= (1 << USB_PORT_FEAT_C_CONNECTION);
901 disable_r8a66597_pipe_all(r8a66597, dev);
902 free_usb_address(r8a66597, dev);
904 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
907 /* this function must be called with interrupt disabled */
908 static void prepare_setup_packet(struct r8a66597 *r8a66597,
909 struct r8a66597_td *td)
912 u16 *p = (u16 *)td->urb->setup_packet;
913 unsigned long setup_addr = USBREQ;
915 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
917 r8a66597_write(r8a66597, (u16)~(SIGN | SACK), INTSTS1);
919 for (i = 0; i < 4; i++) {
920 r8a66597_write(r8a66597, p[i], setup_addr);
923 r8a66597_write(r8a66597, SUREQ, DCPCTR);
926 /* this function must be called with interrupt disabled */
927 static void prepare_packet_read(struct r8a66597 *r8a66597,
928 struct r8a66597_td *td)
930 struct urb *urb = td->urb;
932 if (usb_pipecontrol(urb->pipe)) {
933 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
934 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
935 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
936 if (urb->actual_length == 0) {
937 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
938 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
940 pipe_irq_disable(r8a66597, td->pipenum);
941 pipe_start(r8a66597, td->pipe);
942 pipe_irq_enable(r8a66597, urb, td->pipenum);
944 if (urb->actual_length == 0) {
945 pipe_irq_disable(r8a66597, td->pipenum);
946 pipe_setting(r8a66597, td);
947 pipe_stop(r8a66597, td->pipe);
948 r8a66597_write(r8a66597, (u16)~(1 << td->pipenum),
951 if (td->pipe->pipetre) {
952 r8a66597_write(r8a66597, TRCLR,
954 r8a66597_write(r8a66597,
955 (urb->transfer_buffer_length
959 r8a66597_bset(r8a66597, TRENB,
963 pipe_start(r8a66597, td->pipe);
964 pipe_irq_enable(r8a66597, urb, td->pipenum);
969 /* this function must be called with interrupt disabled */
970 static void prepare_packet_write(struct r8a66597 *r8a66597,
971 struct r8a66597_td *td)
974 struct urb *urb = td->urb;
976 if (usb_pipecontrol(urb->pipe)) {
977 pipe_stop(r8a66597, td->pipe);
978 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
979 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
980 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
981 if (urb->actual_length == 0) {
982 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
983 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
986 if (urb->actual_length == 0)
987 pipe_setting(r8a66597, td);
988 if (td->pipe->pipetre)
989 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
991 r8a66597_write(r8a66597, (u16)~(1 << td->pipenum), BRDYSTS);
993 fifo_change_from_pipe(r8a66597, td->pipe);
994 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
995 if (unlikely((tmp & FRDY) == 0))
996 pipe_irq_enable(r8a66597, urb, td->pipenum);
998 packet_write(r8a66597, td->pipenum);
999 pipe_start(r8a66597, td->pipe);
1002 /* this function must be called with interrupt disabled */
1003 static void prepare_status_packet(struct r8a66597 *r8a66597,
1004 struct r8a66597_td *td)
1006 struct urb *urb = td->urb;
1008 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1010 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1011 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1012 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1013 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1014 r8a66597_write(r8a66597, BVAL | BCLR, CFIFOCTR);
1015 r8a66597_write(r8a66597, (u16)~BEMP0, BEMPSTS);
1016 enable_irq_empty(r8a66597, 0);
1018 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1019 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1020 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1021 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1022 r8a66597_write(r8a66597, (u16)~BRDY0, BRDYSTS);
1023 r8a66597_write(r8a66597, (u16)~BEMP0, BEMPSTS);
1024 enable_irq_ready(r8a66597, 0);
1026 enable_irq_nrdy(r8a66597, 0);
1027 pipe_start(r8a66597, td->pipe);
1030 /* this function must be called with interrupt disabled */
1031 static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1037 if (td->urb->setup_packet[1] == USB_REQ_SET_ADDRESS) {
1038 td->set_address = 1;
1039 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1041 if (td->urb->setup_packet[2] == 0)
1044 prepare_setup_packet(r8a66597, td);
1047 prepare_packet_read(r8a66597, td);
1050 prepare_packet_write(r8a66597, td);
1053 prepare_status_packet(r8a66597, td);
1056 err("invalid type.");
1063 static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1065 if (usb_pipeisoc(urb->pipe)) {
1066 if (urb->number_of_packets == td->iso_cnt)
1070 /* control or bulk or interrupt */
1071 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1072 (td->short_packet) || (td->zero_packet))
1078 /* this function must be called with interrupt disabled */
1079 static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1085 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1086 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1087 r8a66597->timeout_map |= 1 << td->pipenum;
1088 switch (usb_pipetype(td->urb->pipe)) {
1089 case PIPE_INTERRUPT:
1090 case PIPE_ISOCHRONOUS:
1098 mod_timer(&r8a66597->td_timer[td->pipenum],
1099 jiffies + msecs_to_jiffies(time));
1103 /* this function must be called with interrupt disabled */
1104 static void done(struct r8a66597 *r8a66597, struct r8a66597_td *td,
1105 u16 pipenum, struct urb *urb)
1108 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1110 r8a66597->timeout_map &= ~(1 << pipenum);
1113 if (td->set_address && urb->status != 0)
1114 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1116 pipe_toggle_save(r8a66597, td->pipe, urb);
1117 list_del(&td->queue);
1121 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1125 if (usb_pipeisoc(urb->pipe))
1126 urb->start_frame = r8a66597_get_frame(hcd);
1129 spin_unlock(&r8a66597->lock);
1130 usb_hcd_giveback_urb(hcd, urb);
1131 spin_lock(&r8a66597->lock);
1135 td = r8a66597_get_td(r8a66597, pipenum);
1139 start_transfer(r8a66597, td);
1140 set_td_timer(r8a66597, td);
1144 /* this function must be called with interrupt disabled */
1145 static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
1146 u16 pipenum, struct urb *urb)
1147 __releases(r8a66597->lock) __acquires(r8a66597->lock)
1149 done(r8a66597, td, pipenum, urb);
1152 static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1155 int rcv_len, bufsize, urb_len, size;
1157 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1165 fifo_change_from_pipe(r8a66597, td->pipe);
1166 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1167 if (unlikely((tmp & FRDY) == 0)) {
1168 urb->status = -EPIPE;
1169 pipe_stop(r8a66597, td->pipe);
1170 pipe_irq_disable(r8a66597, pipenum);
1171 err("in fifo not ready (%d)", pipenum);
1172 finish_request(r8a66597, td, pipenum, td->urb);
1176 /* prepare parameters */
1177 rcv_len = tmp & DTLN;
1178 bufsize = td->maxpacket;
1179 if (usb_pipeisoc(urb->pipe)) {
1180 buf = (u16 *)(urb->transfer_buffer +
1181 urb->iso_frame_desc[td->iso_cnt].offset);
1182 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1184 buf = (void *)urb->transfer_buffer + urb->actual_length;
1185 urb_len = urb->transfer_buffer_length - urb->actual_length;
1187 if (rcv_len < bufsize)
1188 size = min(rcv_len, urb_len);
1190 size = min(bufsize, urb_len);
1192 /* update parameters */
1193 urb->actual_length += size;
1195 td->zero_packet = 1;
1196 if ((size % td->maxpacket) > 0) {
1197 td->short_packet = 1;
1198 if (urb->transfer_buffer_length != urb->actual_length &&
1199 urb->transfer_flags & URB_SHORT_NOT_OK)
1200 td->urb->status = -EREMOTEIO;
1202 if (usb_pipeisoc(urb->pipe)) {
1203 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1204 urb->iso_frame_desc[td->iso_cnt].status = 0;
1208 /* check transfer finish */
1209 if (check_transfer_finish(td, urb)) {
1210 pipe_stop(r8a66597, td->pipe);
1211 pipe_irq_disable(r8a66597, pipenum);
1216 if (urb->transfer_buffer) {
1218 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1220 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1224 if (finish && pipenum != 0) {
1225 if (td->urb->status == -EINPROGRESS)
1226 td->urb->status = 0;
1227 finish_request(r8a66597, td, pipenum, urb);
1231 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1236 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1243 fifo_change_from_pipe(r8a66597, td->pipe);
1244 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1245 if (unlikely((tmp & FRDY) == 0)) {
1246 urb->status = -EPIPE;
1247 pipe_stop(r8a66597, td->pipe);
1248 pipe_irq_disable(r8a66597, pipenum);
1249 err("out write fifo not ready. (%d)", pipenum);
1250 finish_request(r8a66597, td, pipenum, td->urb);
1254 /* prepare parameters */
1255 bufsize = td->maxpacket;
1256 if (usb_pipeisoc(urb->pipe)) {
1257 buf = (u16 *)(urb->transfer_buffer +
1258 urb->iso_frame_desc[td->iso_cnt].offset);
1260 (int)urb->iso_frame_desc[td->iso_cnt].length);
1262 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
1263 size = min((int)bufsize,
1264 urb->transfer_buffer_length - urb->actual_length);
1269 r8a66597_write(r8a66597, (u16)~(1 << pipenum), BEMPSTS);
1270 if (urb->transfer_buffer) {
1271 r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
1272 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1273 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1276 /* update parameters */
1277 urb->actual_length += size;
1278 if (usb_pipeisoc(urb->pipe)) {
1279 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1280 urb->iso_frame_desc[td->iso_cnt].status = 0;
1284 /* check transfer finish */
1285 if (check_transfer_finish(td, urb)) {
1286 disable_irq_ready(r8a66597, pipenum);
1287 enable_irq_empty(r8a66597, pipenum);
1288 if (!usb_pipeisoc(urb->pipe))
1289 enable_irq_nrdy(r8a66597, pipenum);
1291 pipe_irq_enable(r8a66597, urb, pipenum);
1295 static void check_next_phase(struct r8a66597 *r8a66597)
1297 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1308 if (urb->status != -EINPROGRESS) {
1312 if (check_transfer_finish(td, urb))
1313 td->type = USB_PID_ACK;
1316 if (urb->status != -EINPROGRESS)
1318 else if (urb->transfer_buffer_length == urb->actual_length) {
1319 td->type = USB_PID_ACK;
1321 } else if (usb_pipeout(urb->pipe))
1322 td->type = USB_PID_OUT;
1324 td->type = USB_PID_IN;
1328 if (urb->status == -EINPROGRESS)
1334 finish_request(r8a66597, td, 0, urb);
1336 start_transfer(r8a66597, td);
1339 static void set_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
1341 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1343 if (td && td->urb) {
1344 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1347 td->urb->status = -ECONNRESET;
1349 td->urb->status = -EPIPE;
1353 static void irq_pipe_ready(struct r8a66597 *r8a66597)
1358 struct r8a66597_td *td;
1360 mask = r8a66597_read(r8a66597, BRDYSTS)
1361 & r8a66597_read(r8a66597, BRDYENB);
1362 r8a66597_write(r8a66597, (u16)~mask, BRDYSTS);
1364 td = r8a66597_get_td(r8a66597, 0);
1365 if (td && td->type == USB_PID_IN)
1366 packet_read(r8a66597, 0);
1368 pipe_irq_disable(r8a66597, 0);
1369 check_next_phase(r8a66597);
1372 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1373 check = 1 << pipenum;
1375 td = r8a66597_get_td(r8a66597, pipenum);
1379 if (td->type == USB_PID_IN)
1380 packet_read(r8a66597, pipenum);
1381 else if (td->type == USB_PID_OUT)
1382 packet_write(r8a66597, pipenum);
1387 static void irq_pipe_empty(struct r8a66597 *r8a66597)
1393 struct r8a66597_td *td;
1395 mask = r8a66597_read(r8a66597, BEMPSTS)
1396 & r8a66597_read(r8a66597, BEMPENB);
1397 r8a66597_write(r8a66597, (u16)~mask, BEMPSTS);
1399 cfifo_change(r8a66597, 0);
1400 td = r8a66597_get_td(r8a66597, 0);
1401 if (td && td->type != USB_PID_OUT)
1402 disable_irq_empty(r8a66597, 0);
1403 check_next_phase(r8a66597);
1406 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1407 check = 1 << pipenum;
1409 struct r8a66597_td *td;
1410 td = r8a66597_get_td(r8a66597, pipenum);
1414 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1415 if ((tmp & INBUFM) == 0) {
1416 disable_irq_empty(r8a66597, pipenum);
1417 pipe_irq_disable(r8a66597, pipenum);
1418 if (td->urb->status == -EINPROGRESS)
1419 td->urb->status = 0;
1420 finish_request(r8a66597, td, pipenum, td->urb);
1426 static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1432 mask = r8a66597_read(r8a66597, NRDYSTS)
1433 & r8a66597_read(r8a66597, NRDYENB);
1434 r8a66597_write(r8a66597, (u16)~mask, NRDYSTS);
1436 cfifo_change(r8a66597, 0);
1437 set_urb_error(r8a66597, 0);
1438 pipe_irq_disable(r8a66597, 0);
1439 check_next_phase(r8a66597);
1442 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1443 check = 1 << pipenum;
1445 struct r8a66597_td *td;
1446 td = r8a66597_get_td(r8a66597, pipenum);
1450 set_urb_error(r8a66597, pipenum);
1451 pipe_irq_disable(r8a66597, pipenum);
1452 pipe_stop(r8a66597, td->pipe);
1453 finish_request(r8a66597, td, pipenum, td->urb);
1458 static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port)
1460 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1462 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1463 rh->scount = R8A66597_MAX_SAMPLING;
1464 mod_timer(&r8a66597->rh_timer, jiffies + msecs_to_jiffies(50));
1467 static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1469 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1470 u16 intsts0, intsts1, intsts2;
1471 u16 intenb0, intenb1, intenb2;
1472 u16 mask0, mask1, mask2;
1474 spin_lock(&r8a66597->lock);
1476 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1477 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1478 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1479 intenb0 = r8a66597_read(r8a66597, INTENB0);
1480 intenb1 = r8a66597_read(r8a66597, INTENB1);
1481 intenb2 = r8a66597_read(r8a66597, INTENB2);
1483 mask2 = intsts2 & intenb2;
1484 mask1 = intsts1 & intenb1;
1485 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1487 if (mask2 & ATTCH) {
1488 r8a66597_write(r8a66597, (u16)~ATTCH, INTSTS2);
1489 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1491 /* start usb bus sampling */
1492 start_root_hub_sampling(r8a66597, 1);
1495 r8a66597_write(r8a66597, (u16)~DTCH, INTSTS2);
1496 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1497 r8a66597_usb_disconnect(r8a66597, 1);
1502 if (mask1 & ATTCH) {
1503 r8a66597_write(r8a66597, (u16)~ATTCH, INTSTS1);
1504 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1506 /* start usb bus sampling */
1507 start_root_hub_sampling(r8a66597, 0);
1510 r8a66597_write(r8a66597, (u16)~DTCH, INTSTS1);
1511 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1512 r8a66597_usb_disconnect(r8a66597, 0);
1515 r8a66597_write(r8a66597, (u16)~SIGN, INTSTS1);
1516 set_urb_error(r8a66597, 0);
1517 check_next_phase(r8a66597);
1520 r8a66597_write(r8a66597, (u16)~SACK, INTSTS1);
1521 check_next_phase(r8a66597);
1526 irq_pipe_ready(r8a66597);
1528 irq_pipe_empty(r8a66597);
1530 irq_pipe_nrdy(r8a66597);
1533 spin_unlock(&r8a66597->lock);
1537 /* this function must be called with interrupt disabled */
1538 static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1541 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1543 if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
1544 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1546 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1547 if ((tmp & USBRST) == USBRST) {
1548 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1550 mod_timer(&r8a66597->rh_timer,
1551 jiffies + msecs_to_jiffies(50));
1553 r8a66597_usb_connect(r8a66597, port);
1556 if (rh->scount > 0) {
1557 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1558 if (tmp == rh->old_syssts) {
1560 if (rh->scount == 0) {
1561 if (tmp == FS_JSTS) {
1562 r8a66597_bset(r8a66597, HSE,
1563 get_syscfg_reg(port));
1564 r8a66597_usb_preconnect(r8a66597, port);
1565 } else if (tmp == LS_JSTS) {
1566 r8a66597_bclr(r8a66597, HSE,
1567 get_syscfg_reg(port));
1568 r8a66597_usb_preconnect(r8a66597, port);
1569 } else if (tmp == SE0)
1570 r8a66597_bset(r8a66597, ATTCHE,
1571 get_intenb_reg(port));
1573 mod_timer(&r8a66597->rh_timer,
1574 jiffies + msecs_to_jiffies(50));
1577 rh->scount = R8A66597_MAX_SAMPLING;
1578 rh->old_syssts = tmp;
1579 mod_timer(&r8a66597->rh_timer,
1580 jiffies + msecs_to_jiffies(50));
1585 static void r8a66597_td_timer(unsigned long _r8a66597)
1587 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1588 unsigned long flags;
1590 struct r8a66597_td *td, *new_td = NULL;
1591 struct r8a66597_pipe *pipe;
1593 spin_lock_irqsave(&r8a66597->lock, flags);
1594 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1595 if (!(r8a66597->timeout_map & (1 << pipenum)))
1597 if (timer_pending(&r8a66597->td_timer[pipenum]))
1600 td = r8a66597_get_td(r8a66597, pipenum);
1602 r8a66597->timeout_map &= ~(1 << pipenum);
1606 if (td->urb->actual_length) {
1607 set_td_timer(r8a66597, td);
1612 pipe_stop(r8a66597, pipe);
1616 list_move_tail(&new_td->queue,
1617 &r8a66597->pipe_queue[pipenum]);
1618 new_td = r8a66597_get_td(r8a66597, pipenum);
1623 } while (td != new_td && td->address == new_td->address);
1625 start_transfer(r8a66597, new_td);
1628 r8a66597->timeout_map &= ~(1 << pipenum);
1630 set_td_timer(r8a66597, new_td);
1633 spin_unlock_irqrestore(&r8a66597->lock, flags);
1636 static void r8a66597_timer(unsigned long _r8a66597)
1638 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1639 unsigned long flags;
1641 spin_lock_irqsave(&r8a66597->lock, flags);
1643 r8a66597_root_hub_control(r8a66597, 0);
1644 r8a66597_root_hub_control(r8a66597, 1);
1646 spin_unlock_irqrestore(&r8a66597->lock, flags);
1649 static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1651 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1653 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1654 (urb->dev->state == USB_STATE_CONFIGURED))
1660 static int r8a66597_start(struct usb_hcd *hcd)
1662 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1665 hcd->state = HC_STATE_RUNNING;
1666 if ((ret = enable_controller(r8a66597)) < 0)
1672 static void r8a66597_stop(struct usb_hcd *hcd)
1674 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1676 disable_controller(r8a66597);
1679 static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1681 unsigned int usb_address = usb_pipedevice(urb->pipe);
1682 u16 root_port, hub_port;
1684 if (usb_address == 0) {
1685 get_port_number(urb->dev->devpath,
1686 &root_port, &hub_port);
1687 set_devadd_reg(r8a66597, 0,
1688 get_r8a66597_usb_speed(urb->dev->speed),
1689 get_parent_r8a66597_address(r8a66597, urb->dev),
1690 hub_port, root_port);
1694 static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1696 struct usb_host_endpoint *hep,
1699 struct r8a66597_td *td;
1702 td = kzalloc(sizeof(struct r8a66597_td), mem_flags);
1706 pipenum = r8a66597_get_pipenum(urb, hep);
1707 td->pipenum = pipenum;
1708 td->pipe = hep->hcpriv;
1710 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1711 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1712 !usb_pipein(urb->pipe));
1713 if (usb_pipecontrol(urb->pipe))
1714 td->type = USB_PID_SETUP;
1715 else if (usb_pipein(urb->pipe))
1716 td->type = USB_PID_IN;
1718 td->type = USB_PID_OUT;
1719 INIT_LIST_HEAD(&td->queue);
1724 static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
1725 struct usb_host_endpoint *hep,
1729 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1730 struct r8a66597_td *td = NULL;
1731 int ret = 0, request = 0;
1732 unsigned long flags;
1734 spin_lock_irqsave(&r8a66597->lock, flags);
1735 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1741 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe), mem_flags);
1746 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1747 if (usb_pipeendpoint(urb->pipe))
1748 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1751 if (unlikely(check_pipe_config(r8a66597, urb)))
1752 init_pipe_config(r8a66597, urb);
1754 set_address_zero(r8a66597, urb);
1755 td = r8a66597_make_td(r8a66597, urb, hep, mem_flags);
1760 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1762 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
1764 spin_lock(&urb->lock);
1765 if (urb->status != -EINPROGRESS) {
1766 spin_unlock(&urb->lock);
1771 spin_unlock(&urb->lock);
1774 ret = start_transfer(r8a66597, td);
1776 list_del(&td->queue);
1780 set_td_timer(r8a66597, td);
1783 spin_unlock_irqrestore(&r8a66597->lock, flags);
1787 static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
1789 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1790 struct r8a66597_td *td;
1791 unsigned long flags;
1793 spin_lock_irqsave(&r8a66597->lock, flags);
1796 pipe_stop(r8a66597, td->pipe);
1797 pipe_irq_disable(r8a66597, td->pipenum);
1798 disable_irq_empty(r8a66597, td->pipenum);
1799 done(r8a66597, td, td->pipenum, urb);
1801 spin_unlock_irqrestore(&r8a66597->lock, flags);
1805 static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1806 struct usb_host_endpoint *hep)
1808 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1809 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1810 struct r8a66597_td *td;
1811 struct urb *urb = NULL;
1813 unsigned long flags;
1817 pipenum = pipe->info.pipenum;
1825 spin_lock_irqsave(&r8a66597->lock, flags);
1826 pipe_stop(r8a66597, pipe);
1827 pipe_irq_disable(r8a66597, pipenum);
1828 disable_irq_empty(r8a66597, pipenum);
1829 td = r8a66597_get_td(r8a66597, pipenum);
1832 done(r8a66597, td, pipenum, urb);
1835 spin_unlock_irqrestore(&r8a66597->lock, flags);
1838 static int r8a66597_get_frame(struct usb_hcd *hcd)
1840 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1841 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
1844 static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
1848 if (udev->state == USB_STATE_CONFIGURED &&
1849 udev->parent && udev->parent->devnum > 1 &&
1850 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
1851 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
1853 for (chix = 0; chix < udev->maxchild; chix++) {
1854 struct usb_device *childdev = udev->children[chix];
1857 collect_usb_address_map(childdev, map);
1861 /* this function must be called with interrupt disabled */
1862 static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
1865 struct r8a66597_device *dev;
1866 struct list_head *list = &r8a66597->child_device;
1868 list_for_each_entry(dev, list, device_list) {
1871 if (dev->usb_address != addr)
1877 err("get_r8a66597_device fail.(%d)\n", addr);
1881 static void update_usb_address_map(struct r8a66597 *r8a66597,
1882 struct usb_device *root_hub,
1887 unsigned long flags;
1889 for (i = 0; i < 4; i++) {
1890 diff = r8a66597->child_connect_map[i] ^ map[i];
1894 for (j = 0; j < 32; j++) {
1895 if (!(diff & (1 << j)))
1899 if (map[i] & (1 << j))
1900 set_child_connect_map(r8a66597, addr);
1902 struct r8a66597_device *dev;
1904 spin_lock_irqsave(&r8a66597->lock, flags);
1905 dev = get_r8a66597_device(r8a66597, addr);
1906 disable_r8a66597_pipe_all(r8a66597, dev);
1907 free_usb_address(r8a66597, dev);
1908 put_child_connect_map(r8a66597, addr);
1909 spin_unlock_irqrestore(&r8a66597->lock, flags);
1915 static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
1916 struct usb_hcd *hcd)
1918 struct usb_bus *bus;
1919 unsigned long now_map[4];
1921 memset(now_map, 0, sizeof(now_map));
1923 list_for_each_entry(bus, &usb_bus_list, bus_list) {
1927 if (bus->busnum != hcd->self.busnum)
1930 collect_usb_address_map(bus->root_hub, now_map);
1931 update_usb_address_map(r8a66597, bus->root_hub, now_map);
1935 static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
1937 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1938 unsigned long flags;
1941 r8a66597_check_detect_child(r8a66597, hcd);
1943 spin_lock_irqsave(&r8a66597->lock, flags);
1945 *buf = 0; /* initialize (no change) */
1947 for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++) {
1948 if (r8a66597->root_hub[i].port & 0xffff0000)
1949 *buf |= 1 << (i + 1);
1952 spin_unlock_irqrestore(&r8a66597->lock, flags);
1957 static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
1958 struct usb_hub_descriptor *desc)
1960 desc->bDescriptorType = 0x29;
1961 desc->bHubContrCurrent = 0;
1962 desc->bNbrPorts = R8A66597_MAX_ROOT_HUB;
1963 desc->bDescLength = 9;
1964 desc->bPwrOn2PwrGood = 0;
1965 desc->wHubCharacteristics = cpu_to_le16(0x0011);
1966 desc->bitmap[0] = ((1 << R8A66597_MAX_ROOT_HUB) - 1) << 1;
1967 desc->bitmap[1] = ~0;
1970 static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
1971 u16 wIndex, char *buf, u16 wLength)
1973 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1975 int port = (wIndex & 0x00FF) - 1;
1976 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1977 unsigned long flags;
1981 spin_lock_irqsave(&r8a66597->lock, flags);
1983 case ClearHubFeature:
1986 case C_HUB_OVER_CURRENT:
1987 case C_HUB_LOCAL_POWER:
1993 case ClearPortFeature:
1994 if (wIndex > R8A66597_MAX_ROOT_HUB)
2000 case USB_PORT_FEAT_ENABLE:
2001 rh->port &= (1 << USB_PORT_FEAT_POWER);
2003 case USB_PORT_FEAT_SUSPEND:
2005 case USB_PORT_FEAT_POWER:
2006 r8a66597_port_power(r8a66597, port, 0);
2008 case USB_PORT_FEAT_C_ENABLE:
2009 case USB_PORT_FEAT_C_SUSPEND:
2010 case USB_PORT_FEAT_C_CONNECTION:
2011 case USB_PORT_FEAT_C_OVER_CURRENT:
2012 case USB_PORT_FEAT_C_RESET:
2017 rh->port &= ~(1 << wValue);
2019 case GetHubDescriptor:
2020 r8a66597_hub_descriptor(r8a66597,
2021 (struct usb_hub_descriptor *)buf);
2027 if (wIndex > R8A66597_MAX_ROOT_HUB)
2029 *(u32 *)buf = rh->port;
2031 case SetPortFeature:
2032 if (wIndex > R8A66597_MAX_ROOT_HUB)
2038 case USB_PORT_FEAT_SUSPEND:
2040 case USB_PORT_FEAT_POWER:
2041 r8a66597_port_power(r8a66597, port, 1);
2042 rh->port |= (1 << USB_PORT_FEAT_POWER);
2044 case USB_PORT_FEAT_RESET: {
2045 struct r8a66597_device *dev = rh->dev;
2047 rh->port |= (1 << USB_PORT_FEAT_RESET);
2049 disable_r8a66597_pipe_all(r8a66597, dev);
2050 free_usb_address(r8a66597, dev);
2052 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2053 get_dvstctr_reg(port));
2054 mod_timer(&r8a66597->rh_timer,
2055 jiffies + msecs_to_jiffies(50));
2061 rh->port |= 1 << wValue;
2069 spin_unlock_irqrestore(&r8a66597->lock, flags);
2073 static struct hc_driver r8a66597_hc_driver = {
2074 .description = hcd_name,
2075 .hcd_priv_size = sizeof(struct r8a66597),
2076 .irq = r8a66597_irq,
2079 * generic hardware linkage
2083 .start = r8a66597_start,
2084 .stop = r8a66597_stop,
2087 * managing i/o requests and associated device resources
2089 .urb_enqueue = r8a66597_urb_enqueue,
2090 .urb_dequeue = r8a66597_urb_dequeue,
2091 .endpoint_disable = r8a66597_endpoint_disable,
2094 * periodic schedule support
2096 .get_frame_number = r8a66597_get_frame,
2101 .hub_status_data = r8a66597_hub_status_data,
2102 .hub_control = r8a66597_hub_control,
2105 #if defined(CONFIG_PM)
2106 static int r8a66597_suspend(struct platform_device *pdev, pm_message_t state)
2108 pdev->dev.power.power_state = state;
2112 static int r8a66597_resume(struct platform_device *pdev)
2114 pdev->dev.power.power_state = PMSG_ON;
2117 #else /* if defined(CONFIG_PM) */
2118 #define r8a66597_suspend NULL
2119 #define r8a66597_resume NULL
2122 static int __init_or_module r8a66597_remove(struct platform_device *pdev)
2124 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2125 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2127 del_timer_sync(&r8a66597->rh_timer);
2128 iounmap((void *)r8a66597->reg);
2129 usb_remove_hcd(hcd);
2134 #define resource_len(r) (((r)->end - (r)->start) + 1)
2135 static int __init r8a66597_probe(struct platform_device *pdev)
2137 struct resource *res = NULL;
2139 void __iomem *reg = NULL;
2140 struct usb_hcd *hcd = NULL;
2141 struct r8a66597 *r8a66597;
2145 if (pdev->dev.dma_mask) {
2147 err("dma not support");
2151 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2155 err("platform_get_resource_byname error.");
2159 irq = platform_get_irq(pdev, 0);
2162 err("platform_get_irq error.");
2166 reg = ioremap(res->start, resource_len(res));
2169 err("ioremap error.");
2173 /* initialize hcd */
2174 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2177 err("Failed to create hcd");
2180 r8a66597 = hcd_to_r8a66597(hcd);
2181 memset(r8a66597, 0, sizeof(struct r8a66597));
2182 dev_set_drvdata(&pdev->dev, r8a66597);
2184 spin_lock_init(&r8a66597->lock);
2185 init_timer(&r8a66597->rh_timer);
2186 r8a66597->rh_timer.function = r8a66597_timer;
2187 r8a66597->rh_timer.data = (unsigned long)r8a66597;
2188 r8a66597->reg = (unsigned long)reg;
2190 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2191 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2192 init_timer(&r8a66597->td_timer[i]);
2193 r8a66597->td_timer[i].function = r8a66597_td_timer;
2194 r8a66597->td_timer[i].data = (unsigned long)r8a66597;
2196 INIT_LIST_HEAD(&r8a66597->child_device);
2198 hcd->rsrc_start = res->start;
2199 ret = usb_add_hcd(hcd, irq, 0);
2201 err("Failed to add hcd");
2211 release_mem_region(res->start, 1);
2216 static struct platform_driver r8a66597_driver = {
2217 .probe = r8a66597_probe,
2218 .remove = r8a66597_remove,
2219 .suspend = r8a66597_suspend,
2220 .resume = r8a66597_resume,
2222 .name = (char *) hcd_name,
2226 static int __init r8a66597_init(void)
2231 info("driver %s, %s", hcd_name, DRIVER_VERSION);
2232 return platform_driver_register(&r8a66597_driver);
2234 module_init(r8a66597_init);
2236 static void __exit r8a66597_cleanup(void)
2238 platform_driver_unregister(&r8a66597_driver);
2240 module_exit(r8a66597_cleanup);