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>
39 #include <linux/irq.h>
41 #include "../core/hcd.h"
44 MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
45 MODULE_LICENSE("GPL");
46 MODULE_AUTHOR("Yoshihiro Shimoda");
48 #define DRIVER_VERSION "29 May 2007"
50 static const char hcd_name[] = "r8a66597_hcd";
52 /* module parameters */
53 static unsigned short clock = XTAL12;
54 module_param(clock, ushort, 0644);
55 MODULE_PARM_DESC(clock, "input clock: 48MHz=32768, 24MHz=16384, 12MHz=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)");
62 static unsigned short endian;
63 module_param(endian, ushort, 0644);
64 MODULE_PARM_DESC(endian, "data endian: big=256, little=0 (default=0)");
66 static unsigned short irq_sense = INTL;
67 module_param(irq_sense, ushort, 0644);
68 MODULE_PARM_DESC(irq_sense, "IRQ sense: low level=32, falling edge=0 "
71 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
72 static int r8a66597_get_frame(struct usb_hcd *hcd);
74 /* this function must be called with interrupt disabled */
75 static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
80 tmp = r8a66597_read(r8a66597, INTENB0);
81 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
82 r8a66597_bset(r8a66597, 1 << pipenum, reg);
83 r8a66597_write(r8a66597, tmp, INTENB0);
86 /* this function must be called with interrupt disabled */
87 static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
92 tmp = r8a66597_read(r8a66597, INTENB0);
93 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
94 r8a66597_bclr(r8a66597, 1 << pipenum, reg);
95 r8a66597_write(r8a66597, tmp, INTENB0);
98 static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
99 u16 usbspd, u8 upphub, u8 hubport, int port)
102 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
104 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
105 r8a66597_write(r8a66597, val, devadd_reg);
108 static int enable_controller(struct r8a66597 *r8a66597)
114 r8a66597_write(r8a66597, USBE, SYSCFG0);
115 tmp = r8a66597_read(r8a66597, SYSCFG0);
117 err("register access fail.");
120 } while ((tmp & USBE) != USBE);
121 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
122 r8a66597_mdfy(r8a66597, clock, XTAL, SYSCFG0);
125 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
128 tmp = r8a66597_read(r8a66597, SYSCFG0);
130 err("register access fail.");
133 } while ((tmp & SCKE) != SCKE);
135 r8a66597_bset(r8a66597, DCFM | DRPD, SYSCFG0);
136 r8a66597_bset(r8a66597, DRPD, SYSCFG1);
138 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
139 r8a66597_bset(r8a66597, HSE, SYSCFG0);
140 r8a66597_bset(r8a66597, HSE, SYSCFG1);
141 r8a66597_bset(r8a66597, USBE, SYSCFG0);
143 r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
144 r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
145 r8a66597_bset(r8a66597, BRDY0, BRDYENB);
146 r8a66597_bset(r8a66597, BEMP0, BEMPENB);
148 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, DMA0CFG);
149 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, DMA1CFG);
151 r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
152 r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
153 r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
155 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
157 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
158 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
159 r8a66597_bset(r8a66597, ATTCHE, INTENB1);
160 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
161 r8a66597_bset(r8a66597, ATTCHE, INTENB2);
166 static void disable_controller(struct r8a66597 *r8a66597)
170 r8a66597_write(r8a66597, 0, INTENB0);
171 r8a66597_write(r8a66597, 0, INTENB1);
172 r8a66597_write(r8a66597, 0, INTENB2);
173 r8a66597_write(r8a66597, 0, INTSTS0);
174 r8a66597_write(r8a66597, 0, INTSTS1);
175 r8a66597_write(r8a66597, 0, INTSTS2);
177 r8a66597_port_power(r8a66597, 0, 0);
178 r8a66597_port_power(r8a66597, 1, 0);
181 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
183 } while (tmp == EDGESTS);
185 r8a66597_bclr(r8a66597, DCFM | DRPD, SYSCFG0);
186 r8a66597_bclr(r8a66597, DRPD, SYSCFG1);
187 r8a66597_bclr(r8a66597, HSE, SYSCFG0);
188 r8a66597_bclr(r8a66597, HSE, SYSCFG1);
190 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
192 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
193 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
194 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
197 static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
198 struct usb_device *udev)
200 struct r8a66597_device *dev;
202 if (udev->parent && udev->parent->devnum != 1)
205 dev = dev_get_drvdata(&udev->dev);
212 static int is_child_device(char *devpath)
214 return (devpath[2] ? 1 : 0);
217 static int is_hub_limit(char *devpath)
219 return ((strlen(devpath) >= 4) ? 1 : 0);
222 static void get_port_number(char *devpath, u16 *root_port, u16 *hub_port)
225 *root_port = (devpath[0] & 0x0F) - 1;
226 if (*root_port >= R8A66597_MAX_ROOT_HUB)
227 err("illegal root port number");
230 *hub_port = devpath[2] & 0x0F;
233 static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
248 err("unknown speed");
255 static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
260 r8a66597->child_connect_map[idx] |= 1 << (address % 32);
263 static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
268 r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
271 static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
273 u16 pipenum = pipe->info.pipenum;
274 unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
275 unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
276 unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
278 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
279 dma_ch = R8A66597_PIPE_NO_DMA;
281 pipe->fifoaddr = fifoaddr[dma_ch];
282 pipe->fifosel = fifosel[dma_ch];
283 pipe->fifoctr = fifoctr[dma_ch];
286 pipe->pipectr = DCPCTR;
288 pipe->pipectr = get_pipectr_addr(pipenum);
290 if (check_bulk_or_isoc(pipenum)) {
291 pipe->pipetre = get_pipetre_addr(pipenum);
292 pipe->pipetrn = get_pipetrn_addr(pipenum);
299 static struct r8a66597_device *
300 get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
302 if (usb_pipedevice(urb->pipe) == 0)
303 return &r8a66597->device0;
305 return dev_get_drvdata(&urb->dev->dev);
308 static int make_r8a66597_device(struct r8a66597 *r8a66597,
309 struct urb *urb, u8 addr)
311 struct r8a66597_device *dev;
312 int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
314 dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
318 dev_set_drvdata(&urb->dev->dev, dev);
319 dev->udev = urb->dev;
321 dev->usb_address = usb_address;
322 dev->state = USB_STATE_ADDRESS;
323 dev->ep_in_toggle = 0;
324 dev->ep_out_toggle = 0;
325 INIT_LIST_HEAD(&dev->device_list);
326 list_add_tail(&dev->device_list, &r8a66597->child_device);
328 get_port_number(urb->dev->devpath, &dev->root_port, &dev->hub_port);
329 if (!is_child_device(urb->dev->devpath))
330 r8a66597->root_hub[dev->root_port].dev = dev;
332 set_devadd_reg(r8a66597, dev->address,
333 get_r8a66597_usb_speed(urb->dev->speed),
334 get_parent_r8a66597_address(r8a66597, urb->dev),
335 dev->hub_port, dev->root_port);
340 /* this function must be called with interrupt disabled */
341 static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
343 u8 addr; /* R8A66597's address */
344 struct r8a66597_device *dev;
346 if (is_hub_limit(urb->dev->devpath)) {
347 err("Externel hub limit reached.");
351 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
352 if (dev && dev->state >= USB_STATE_ADDRESS)
355 for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
356 if (r8a66597->address_map & (1 << addr))
359 dbg("alloc_address: r8a66597_addr=%d", addr);
360 r8a66597->address_map |= 1 << addr;
362 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
368 err("cannot communicate with a USB device more than 10.(%x)",
369 r8a66597->address_map);
374 /* this function must be called with interrupt disabled */
375 static void free_usb_address(struct r8a66597 *r8a66597,
376 struct r8a66597_device *dev)
383 dbg("free_addr: addr=%d", dev->address);
385 dev->state = USB_STATE_DEFAULT;
386 r8a66597->address_map &= ~(1 << dev->address);
388 dev_set_drvdata(&dev->udev->dev, NULL);
389 list_del(&dev->device_list);
392 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
393 if (r8a66597->root_hub[port].dev == dev) {
394 r8a66597->root_hub[port].dev = NULL;
400 static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
407 tmp = r8a66597_read(r8a66597, reg);
409 err("register%lx, loop %x is timeout", reg, loop);
413 } while ((tmp & mask) != loop);
416 /* this function must be called with interrupt disabled */
417 static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
421 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
422 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
423 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
424 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
427 /* this function must be called with interrupt disabled */
428 static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
432 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
433 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
434 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
435 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
436 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
439 /* this function must be called with interrupt disabled */
440 static void clear_all_buffer(struct r8a66597 *r8a66597,
441 struct r8a66597_pipe *pipe)
445 if (!pipe || pipe->info.pipenum == 0)
448 pipe_stop(r8a66597, pipe);
449 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
450 tmp = r8a66597_read(r8a66597, pipe->pipectr);
451 tmp = r8a66597_read(r8a66597, pipe->pipectr);
452 tmp = r8a66597_read(r8a66597, pipe->pipectr);
453 r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
456 /* this function must be called with interrupt disabled */
457 static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
458 struct r8a66597_pipe *pipe, int toggle)
461 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
463 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
466 /* this function must be called with interrupt disabled */
467 static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
469 r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
470 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
473 /* this function must be called with interrupt disabled */
474 static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
475 struct r8a66597_pipe *pipe)
477 cfifo_change(r8a66597, 0);
478 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D0FIFOSEL);
479 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D1FIFOSEL);
481 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum, MBW | CURPIPE,
483 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
486 static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
488 struct r8a66597_pipe *pipe = hep->hcpriv;
490 if (usb_pipeendpoint(urb->pipe) == 0)
493 return pipe->info.pipenum;
496 static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
498 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
500 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
503 static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
509 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
512 /* this function must be called with interrupt disabled */
513 static void pipe_toggle_set(struct r8a66597 *r8a66597,
514 struct r8a66597_pipe *pipe,
515 struct urb *urb, int set)
517 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
518 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
519 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
525 *toggle |= 1 << endpoint;
527 *toggle &= ~(1 << endpoint);
530 /* this function must be called with interrupt disabled */
531 static void pipe_toggle_save(struct r8a66597 *r8a66597,
532 struct r8a66597_pipe *pipe,
535 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
536 pipe_toggle_set(r8a66597, pipe, urb, 1);
538 pipe_toggle_set(r8a66597, pipe, urb, 0);
541 /* this function must be called with interrupt disabled */
542 static void pipe_toggle_restore(struct r8a66597 *r8a66597,
543 struct r8a66597_pipe *pipe,
546 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
547 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
548 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
553 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
556 /* this function must be called with interrupt disabled */
557 static void pipe_buffer_setting(struct r8a66597 *r8a66597,
558 struct r8a66597_pipe_info *info)
562 if (info->pipenum == 0)
565 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
566 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
567 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
570 if (info->type == R8A66597_BULK && info->dir_in)
571 val |= R8A66597_DBLB | R8A66597_SHTNAK;
572 val |= info->type | info->epnum;
573 r8a66597_write(r8a66597, val, PIPECFG);
575 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
577 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
581 r8a66597_write(r8a66597, info->interval, PIPEPERI);
586 /* this function must be called with interrupt disabled */
587 static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
589 struct r8a66597_pipe_info *info;
590 struct urb *urb = td->urb;
592 if (td->pipenum > 0) {
593 info = &td->pipe->info;
594 cfifo_change(r8a66597, 0);
595 pipe_buffer_setting(r8a66597, info);
597 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
598 usb_pipeout(urb->pipe)) &&
599 !usb_pipecontrol(urb->pipe)) {
600 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
601 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
602 clear_all_buffer(r8a66597, td->pipe);
603 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
604 usb_pipeout(urb->pipe), 1);
606 pipe_toggle_restore(r8a66597, td->pipe, urb);
610 /* this function must be called with interrupt disabled */
611 static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
612 struct usb_endpoint_descriptor *ep)
614 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
616 memset(array, 0, sizeof(array));
617 switch (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
618 case USB_ENDPOINT_XFER_BULK:
619 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
626 case USB_ENDPOINT_XFER_INT:
627 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
634 case USB_ENDPOINT_XFER_ISOC:
635 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
647 while (array[i] != 0) {
648 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
656 static u16 get_r8a66597_type(__u8 type)
661 case USB_ENDPOINT_XFER_BULK:
662 r8a66597_type = R8A66597_BULK;
664 case USB_ENDPOINT_XFER_INT:
665 r8a66597_type = R8A66597_INT;
667 case USB_ENDPOINT_XFER_ISOC:
668 r8a66597_type = R8A66597_ISO;
672 r8a66597_type = 0x0000;
676 return r8a66597_type;
679 static u16 get_bufnum(u16 pipenum)
685 else if (check_bulk_or_isoc(pipenum))
686 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
687 else if (check_interrupt(pipenum))
688 bufnum = 4 + (pipenum - 6);
690 err("Illegal pipenum (%d)", pipenum);
695 static u16 get_buf_bsize(u16 pipenum)
701 else if (check_bulk_or_isoc(pipenum))
702 buf_bsize = R8A66597_BUF_BSIZE - 1;
703 else if (check_interrupt(pipenum))
706 err("Illegal pipenum (%d)", pipenum);
711 /* this function must be called with interrupt disabled */
712 static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
713 struct r8a66597_device *dev,
714 struct r8a66597_pipe *pipe,
718 struct r8a66597_pipe_info *info = &pipe->info;
720 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
721 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
722 if ((r8a66597->dma_map & (1 << i)) != 0)
725 info("address %d, EndpointAddress 0x%02x use DMA FIFO",
726 usb_pipedevice(urb->pipe),
727 info->dir_in ? USB_ENDPOINT_DIR_MASK + info->epnum
730 r8a66597->dma_map |= 1 << i;
731 dev->dma_map |= 1 << i;
732 set_pipe_reg_addr(pipe, i);
734 cfifo_change(r8a66597, 0);
735 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum,
736 MBW | CURPIPE, pipe->fifosel);
738 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
740 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
746 /* this function must be called with interrupt disabled */
747 static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
748 struct usb_host_endpoint *hep,
749 struct r8a66597_pipe_info *info)
751 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
752 struct r8a66597_pipe *pipe = hep->hcpriv;
757 set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
758 r8a66597->pipe_cnt[pipe->info.pipenum]++;
759 dev->pipe_cnt[pipe->info.pipenum]++;
761 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
764 /* this function must be called with interrupt disabled */
765 static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
767 struct r8a66597_td *td, *next;
769 struct list_head *list = &r8a66597->pipe_queue[pipenum];
771 if (list_empty(list))
774 list_for_each_entry_safe(td, next, list, queue) {
777 if (td->address != address)
781 list_del(&td->queue);
785 urb->status = -ENODEV;
787 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597),
790 spin_unlock(&r8a66597->lock);
791 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb);
792 spin_lock(&r8a66597->lock);
798 /* this function must be called with interrupt disabled */
799 static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
800 struct r8a66597_device *dev)
808 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
809 if (!dev->pipe_cnt[pipenum])
814 force_dequeue(r8a66597, 0, dev->address);
817 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
818 dev->pipe_cnt[pipenum] = 0;
819 force_dequeue(r8a66597, pipenum, dev->address);
824 r8a66597->dma_map &= ~(dev->dma_map);
828 /* this function must be called with interrupt disabled */
829 static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
830 struct usb_host_endpoint *hep,
831 struct usb_endpoint_descriptor *ep)
833 struct r8a66597_pipe_info info;
835 info.pipenum = get_empty_pipenum(r8a66597, ep);
836 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
837 info.epnum = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
838 info.maxpacket = ep->wMaxPacketSize;
839 info.type = get_r8a66597_type(ep->bmAttributes
840 & USB_ENDPOINT_XFERTYPE_MASK);
841 info.bufnum = get_bufnum(info.pipenum);
842 info.buf_bsize = get_buf_bsize(info.pipenum);
843 info.interval = ep->bInterval;
844 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
849 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
852 static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
854 struct r8a66597_device *dev;
856 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
857 dev->state = USB_STATE_CONFIGURED;
860 static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
863 if (pipenum == 0 && usb_pipeout(urb->pipe))
864 enable_irq_empty(r8a66597, pipenum);
866 enable_irq_ready(r8a66597, pipenum);
868 if (!usb_pipeisoc(urb->pipe))
869 enable_irq_nrdy(r8a66597, pipenum);
872 static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
874 disable_irq_ready(r8a66597, pipenum);
875 disable_irq_nrdy(r8a66597, pipenum);
878 /* this function must be called with interrupt disabled */
879 static void r8a66597_usb_preconnect(struct r8a66597 *r8a66597, int port)
881 r8a66597->root_hub[port].port |= (1 << USB_PORT_FEAT_CONNECTION)
882 | (1 << USB_PORT_FEAT_C_CONNECTION);
883 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
884 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
887 /* this function must be called with interrupt disabled */
888 static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
890 u16 speed = get_rh_usb_speed(r8a66597, port);
891 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
894 rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
895 else if (speed == LSMODE)
896 rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
898 rh->port &= ~(1 << USB_PORT_FEAT_RESET);
899 rh->port |= 1 << USB_PORT_FEAT_ENABLE;
902 /* this function must be called with interrupt disabled */
903 static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
905 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
907 r8a66597->root_hub[port].port &= ~(1 << USB_PORT_FEAT_CONNECTION);
908 r8a66597->root_hub[port].port |= (1 << USB_PORT_FEAT_C_CONNECTION);
910 disable_r8a66597_pipe_all(r8a66597, dev);
911 free_usb_address(r8a66597, dev);
913 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
916 /* this function must be called with interrupt disabled */
917 static void prepare_setup_packet(struct r8a66597 *r8a66597,
918 struct r8a66597_td *td)
921 u16 *p = (u16 *)td->urb->setup_packet;
922 unsigned long setup_addr = USBREQ;
924 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
926 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
928 for (i = 0; i < 4; i++) {
929 r8a66597_write(r8a66597, p[i], setup_addr);
932 r8a66597_write(r8a66597, SUREQ, DCPCTR);
935 /* this function must be called with interrupt disabled */
936 static void prepare_packet_read(struct r8a66597 *r8a66597,
937 struct r8a66597_td *td)
939 struct urb *urb = td->urb;
941 if (usb_pipecontrol(urb->pipe)) {
942 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
943 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
944 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
945 if (urb->actual_length == 0) {
946 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
947 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
949 pipe_irq_disable(r8a66597, td->pipenum);
950 pipe_start(r8a66597, td->pipe);
951 pipe_irq_enable(r8a66597, urb, td->pipenum);
953 if (urb->actual_length == 0) {
954 pipe_irq_disable(r8a66597, td->pipenum);
955 pipe_setting(r8a66597, td);
956 pipe_stop(r8a66597, td->pipe);
957 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
959 if (td->pipe->pipetre) {
960 r8a66597_write(r8a66597, TRCLR,
962 r8a66597_write(r8a66597,
963 (urb->transfer_buffer_length
967 r8a66597_bset(r8a66597, TRENB,
971 pipe_start(r8a66597, td->pipe);
972 pipe_irq_enable(r8a66597, urb, td->pipenum);
977 /* this function must be called with interrupt disabled */
978 static void prepare_packet_write(struct r8a66597 *r8a66597,
979 struct r8a66597_td *td)
982 struct urb *urb = td->urb;
984 if (usb_pipecontrol(urb->pipe)) {
985 pipe_stop(r8a66597, td->pipe);
986 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
987 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
988 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
989 if (urb->actual_length == 0) {
990 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
991 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
994 if (urb->actual_length == 0)
995 pipe_setting(r8a66597, td);
996 if (td->pipe->pipetre)
997 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
999 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1001 fifo_change_from_pipe(r8a66597, td->pipe);
1002 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1003 if (unlikely((tmp & FRDY) == 0))
1004 pipe_irq_enable(r8a66597, urb, td->pipenum);
1006 packet_write(r8a66597, td->pipenum);
1007 pipe_start(r8a66597, td->pipe);
1010 /* this function must be called with interrupt disabled */
1011 static void prepare_status_packet(struct r8a66597 *r8a66597,
1012 struct r8a66597_td *td)
1014 struct urb *urb = td->urb;
1016 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1017 pipe_stop(r8a66597, td->pipe);
1019 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1020 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1021 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1022 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1023 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
1024 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1025 r8a66597_write(r8a66597, BVAL, CFIFOCTR);
1026 enable_irq_empty(r8a66597, 0);
1028 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1029 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1030 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1031 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1032 enable_irq_ready(r8a66597, 0);
1034 enable_irq_nrdy(r8a66597, 0);
1035 pipe_start(r8a66597, td->pipe);
1038 /* this function must be called with interrupt disabled */
1039 static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1045 if (td->urb->setup_packet[1] == USB_REQ_SET_ADDRESS) {
1046 td->set_address = 1;
1047 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1049 if (td->urb->setup_packet[2] == 0)
1052 prepare_setup_packet(r8a66597, td);
1055 prepare_packet_read(r8a66597, td);
1058 prepare_packet_write(r8a66597, td);
1061 prepare_status_packet(r8a66597, td);
1064 err("invalid type.");
1071 static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1073 if (usb_pipeisoc(urb->pipe)) {
1074 if (urb->number_of_packets == td->iso_cnt)
1078 /* control or bulk or interrupt */
1079 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1080 (td->short_packet) || (td->zero_packet))
1086 /* this function must be called with interrupt disabled */
1087 static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1093 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1094 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1095 r8a66597->timeout_map |= 1 << td->pipenum;
1096 switch (usb_pipetype(td->urb->pipe)) {
1097 case PIPE_INTERRUPT:
1098 case PIPE_ISOCHRONOUS:
1106 mod_timer(&r8a66597->td_timer[td->pipenum],
1107 jiffies + msecs_to_jiffies(time));
1111 /* this function must be called with interrupt disabled */
1112 static void done(struct r8a66597 *r8a66597, struct r8a66597_td *td,
1113 u16 pipenum, struct urb *urb)
1116 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1118 r8a66597->timeout_map &= ~(1 << pipenum);
1121 if (td->set_address && urb->status != 0)
1122 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1124 pipe_toggle_save(r8a66597, td->pipe, urb);
1125 list_del(&td->queue);
1129 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1133 if (usb_pipeisoc(urb->pipe))
1134 urb->start_frame = r8a66597_get_frame(hcd);
1137 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
1139 spin_unlock(&r8a66597->lock);
1140 usb_hcd_giveback_urb(hcd, urb);
1141 spin_lock(&r8a66597->lock);
1145 td = r8a66597_get_td(r8a66597, pipenum);
1149 start_transfer(r8a66597, td);
1150 set_td_timer(r8a66597, td);
1154 /* this function must be called with interrupt disabled */
1155 static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
1156 u16 pipenum, struct urb *urb)
1157 __releases(r8a66597->lock) __acquires(r8a66597->lock)
1159 done(r8a66597, td, pipenum, urb);
1162 static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1165 int rcv_len, bufsize, urb_len, size;
1167 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1175 fifo_change_from_pipe(r8a66597, td->pipe);
1176 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1177 if (unlikely((tmp & FRDY) == 0)) {
1178 urb->status = -EPIPE;
1179 pipe_stop(r8a66597, td->pipe);
1180 pipe_irq_disable(r8a66597, pipenum);
1181 err("in fifo not ready (%d)", pipenum);
1182 finish_request(r8a66597, td, pipenum, td->urb);
1186 /* prepare parameters */
1187 rcv_len = tmp & DTLN;
1188 bufsize = td->maxpacket;
1189 if (usb_pipeisoc(urb->pipe)) {
1190 buf = (u16 *)(urb->transfer_buffer +
1191 urb->iso_frame_desc[td->iso_cnt].offset);
1192 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1194 buf = (void *)urb->transfer_buffer + urb->actual_length;
1195 urb_len = urb->transfer_buffer_length - urb->actual_length;
1197 if (rcv_len < bufsize)
1198 size = min(rcv_len, urb_len);
1200 size = min(bufsize, urb_len);
1202 /* update parameters */
1203 urb->actual_length += size;
1205 td->zero_packet = 1;
1206 if ((size % td->maxpacket) > 0) {
1207 td->short_packet = 1;
1208 if (urb->transfer_buffer_length != urb->actual_length &&
1209 urb->transfer_flags & URB_SHORT_NOT_OK)
1210 td->urb->status = -EREMOTEIO;
1212 if (usb_pipeisoc(urb->pipe)) {
1213 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1214 urb->iso_frame_desc[td->iso_cnt].status = 0;
1218 /* check transfer finish */
1219 if (check_transfer_finish(td, urb)) {
1220 pipe_stop(r8a66597, td->pipe);
1221 pipe_irq_disable(r8a66597, pipenum);
1226 if (urb->transfer_buffer) {
1228 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1230 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1234 if (finish && pipenum != 0) {
1235 if (td->urb->status == -EINPROGRESS)
1236 td->urb->status = 0;
1237 finish_request(r8a66597, td, pipenum, urb);
1241 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1246 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1253 fifo_change_from_pipe(r8a66597, td->pipe);
1254 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1255 if (unlikely((tmp & FRDY) == 0)) {
1256 urb->status = -EPIPE;
1257 pipe_stop(r8a66597, td->pipe);
1258 pipe_irq_disable(r8a66597, pipenum);
1259 err("out write fifo not ready. (%d)", pipenum);
1260 finish_request(r8a66597, td, pipenum, td->urb);
1264 /* prepare parameters */
1265 bufsize = td->maxpacket;
1266 if (usb_pipeisoc(urb->pipe)) {
1267 buf = (u16 *)(urb->transfer_buffer +
1268 urb->iso_frame_desc[td->iso_cnt].offset);
1270 (int)urb->iso_frame_desc[td->iso_cnt].length);
1272 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
1273 size = min((int)bufsize,
1274 urb->transfer_buffer_length - urb->actual_length);
1279 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
1280 if (urb->transfer_buffer) {
1281 r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
1282 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1283 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1286 /* update parameters */
1287 urb->actual_length += size;
1288 if (usb_pipeisoc(urb->pipe)) {
1289 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1290 urb->iso_frame_desc[td->iso_cnt].status = 0;
1294 /* check transfer finish */
1295 if (check_transfer_finish(td, urb)) {
1296 disable_irq_ready(r8a66597, pipenum);
1297 enable_irq_empty(r8a66597, pipenum);
1298 if (!usb_pipeisoc(urb->pipe))
1299 enable_irq_nrdy(r8a66597, pipenum);
1301 pipe_irq_enable(r8a66597, urb, pipenum);
1305 static void check_next_phase(struct r8a66597 *r8a66597)
1307 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1318 if (urb->status != -EINPROGRESS) {
1322 if (check_transfer_finish(td, urb))
1323 td->type = USB_PID_ACK;
1326 if (urb->status != -EINPROGRESS)
1328 else if (urb->transfer_buffer_length == urb->actual_length) {
1329 td->type = USB_PID_ACK;
1331 } else if (usb_pipeout(urb->pipe))
1332 td->type = USB_PID_OUT;
1334 td->type = USB_PID_IN;
1338 if (urb->status == -EINPROGRESS)
1344 finish_request(r8a66597, td, 0, urb);
1346 start_transfer(r8a66597, td);
1349 static void set_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
1351 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1353 if (td && td->urb) {
1354 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1357 td->urb->status = -ECONNRESET;
1359 td->urb->status = -EPIPE;
1363 static void irq_pipe_ready(struct r8a66597 *r8a66597)
1368 struct r8a66597_td *td;
1370 mask = r8a66597_read(r8a66597, BRDYSTS)
1371 & r8a66597_read(r8a66597, BRDYENB);
1372 r8a66597_write(r8a66597, ~mask, BRDYSTS);
1374 td = r8a66597_get_td(r8a66597, 0);
1375 if (td && td->type == USB_PID_IN)
1376 packet_read(r8a66597, 0);
1378 pipe_irq_disable(r8a66597, 0);
1379 check_next_phase(r8a66597);
1382 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1383 check = 1 << pipenum;
1385 td = r8a66597_get_td(r8a66597, pipenum);
1389 if (td->type == USB_PID_IN)
1390 packet_read(r8a66597, pipenum);
1391 else if (td->type == USB_PID_OUT)
1392 packet_write(r8a66597, pipenum);
1397 static void irq_pipe_empty(struct r8a66597 *r8a66597)
1403 struct r8a66597_td *td;
1405 mask = r8a66597_read(r8a66597, BEMPSTS)
1406 & r8a66597_read(r8a66597, BEMPENB);
1407 r8a66597_write(r8a66597, ~mask, BEMPSTS);
1409 cfifo_change(r8a66597, 0);
1410 td = r8a66597_get_td(r8a66597, 0);
1411 if (td && td->type != USB_PID_OUT)
1412 disable_irq_empty(r8a66597, 0);
1413 check_next_phase(r8a66597);
1416 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1417 check = 1 << pipenum;
1419 struct r8a66597_td *td;
1420 td = r8a66597_get_td(r8a66597, pipenum);
1424 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1425 if ((tmp & INBUFM) == 0) {
1426 disable_irq_empty(r8a66597, pipenum);
1427 pipe_irq_disable(r8a66597, pipenum);
1428 if (td->urb->status == -EINPROGRESS)
1429 td->urb->status = 0;
1430 finish_request(r8a66597, td, pipenum, td->urb);
1436 static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1442 mask = r8a66597_read(r8a66597, NRDYSTS)
1443 & r8a66597_read(r8a66597, NRDYENB);
1444 r8a66597_write(r8a66597, ~mask, NRDYSTS);
1446 cfifo_change(r8a66597, 0);
1447 set_urb_error(r8a66597, 0);
1448 pipe_irq_disable(r8a66597, 0);
1449 check_next_phase(r8a66597);
1452 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1453 check = 1 << pipenum;
1455 struct r8a66597_td *td;
1456 td = r8a66597_get_td(r8a66597, pipenum);
1460 set_urb_error(r8a66597, pipenum);
1461 pipe_irq_disable(r8a66597, pipenum);
1462 pipe_stop(r8a66597, td->pipe);
1463 finish_request(r8a66597, td, pipenum, td->urb);
1468 static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port)
1470 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1472 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1473 rh->scount = R8A66597_MAX_SAMPLING;
1474 mod_timer(&r8a66597->rh_timer, jiffies + msecs_to_jiffies(50));
1477 static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1479 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1480 u16 intsts0, intsts1, intsts2;
1481 u16 intenb0, intenb1, intenb2;
1482 u16 mask0, mask1, mask2;
1484 spin_lock(&r8a66597->lock);
1486 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1487 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1488 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1489 intenb0 = r8a66597_read(r8a66597, INTENB0);
1490 intenb1 = r8a66597_read(r8a66597, INTENB1);
1491 intenb2 = r8a66597_read(r8a66597, INTENB2);
1493 mask2 = intsts2 & intenb2;
1494 mask1 = intsts1 & intenb1;
1495 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1497 if (mask2 & ATTCH) {
1498 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
1499 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1501 /* start usb bus sampling */
1502 start_root_hub_sampling(r8a66597, 1);
1505 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
1506 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1507 r8a66597_usb_disconnect(r8a66597, 1);
1512 if (mask1 & ATTCH) {
1513 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
1514 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1516 /* start usb bus sampling */
1517 start_root_hub_sampling(r8a66597, 0);
1520 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
1521 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1522 r8a66597_usb_disconnect(r8a66597, 0);
1525 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
1526 set_urb_error(r8a66597, 0);
1527 check_next_phase(r8a66597);
1530 r8a66597_write(r8a66597, ~SACK, INTSTS1);
1531 check_next_phase(r8a66597);
1536 irq_pipe_ready(r8a66597);
1538 irq_pipe_empty(r8a66597);
1540 irq_pipe_nrdy(r8a66597);
1543 spin_unlock(&r8a66597->lock);
1547 /* this function must be called with interrupt disabled */
1548 static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1551 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1553 if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
1554 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1556 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1557 if ((tmp & USBRST) == USBRST) {
1558 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1560 mod_timer(&r8a66597->rh_timer,
1561 jiffies + msecs_to_jiffies(50));
1563 r8a66597_usb_connect(r8a66597, port);
1566 if (rh->scount > 0) {
1567 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1568 if (tmp == rh->old_syssts) {
1570 if (rh->scount == 0) {
1571 if (tmp == FS_JSTS) {
1572 r8a66597_bset(r8a66597, HSE,
1573 get_syscfg_reg(port));
1574 r8a66597_usb_preconnect(r8a66597, port);
1575 } else if (tmp == LS_JSTS) {
1576 r8a66597_bclr(r8a66597, HSE,
1577 get_syscfg_reg(port));
1578 r8a66597_usb_preconnect(r8a66597, port);
1579 } else if (tmp == SE0)
1580 r8a66597_bset(r8a66597, ATTCHE,
1581 get_intenb_reg(port));
1583 mod_timer(&r8a66597->rh_timer,
1584 jiffies + msecs_to_jiffies(50));
1587 rh->scount = R8A66597_MAX_SAMPLING;
1588 rh->old_syssts = tmp;
1589 mod_timer(&r8a66597->rh_timer,
1590 jiffies + msecs_to_jiffies(50));
1595 static void r8a66597_td_timer(unsigned long _r8a66597)
1597 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1598 unsigned long flags;
1600 struct r8a66597_td *td, *new_td = NULL;
1601 struct r8a66597_pipe *pipe;
1603 spin_lock_irqsave(&r8a66597->lock, flags);
1604 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1605 if (!(r8a66597->timeout_map & (1 << pipenum)))
1607 if (timer_pending(&r8a66597->td_timer[pipenum]))
1610 td = r8a66597_get_td(r8a66597, pipenum);
1612 r8a66597->timeout_map &= ~(1 << pipenum);
1616 if (td->urb->actual_length) {
1617 set_td_timer(r8a66597, td);
1622 pipe_stop(r8a66597, pipe);
1626 list_move_tail(&new_td->queue,
1627 &r8a66597->pipe_queue[pipenum]);
1628 new_td = r8a66597_get_td(r8a66597, pipenum);
1633 } while (td != new_td && td->address == new_td->address);
1635 start_transfer(r8a66597, new_td);
1638 r8a66597->timeout_map &= ~(1 << pipenum);
1640 set_td_timer(r8a66597, new_td);
1643 spin_unlock_irqrestore(&r8a66597->lock, flags);
1646 static void r8a66597_timer(unsigned long _r8a66597)
1648 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1649 unsigned long flags;
1651 spin_lock_irqsave(&r8a66597->lock, flags);
1653 r8a66597_root_hub_control(r8a66597, 0);
1654 r8a66597_root_hub_control(r8a66597, 1);
1656 spin_unlock_irqrestore(&r8a66597->lock, flags);
1659 static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1661 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1663 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1664 (urb->dev->state == USB_STATE_CONFIGURED))
1670 static int r8a66597_start(struct usb_hcd *hcd)
1672 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1674 hcd->state = HC_STATE_RUNNING;
1675 return enable_controller(r8a66597);
1678 static void r8a66597_stop(struct usb_hcd *hcd)
1680 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1682 disable_controller(r8a66597);
1685 static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1687 unsigned int usb_address = usb_pipedevice(urb->pipe);
1688 u16 root_port, hub_port;
1690 if (usb_address == 0) {
1691 get_port_number(urb->dev->devpath,
1692 &root_port, &hub_port);
1693 set_devadd_reg(r8a66597, 0,
1694 get_r8a66597_usb_speed(urb->dev->speed),
1695 get_parent_r8a66597_address(r8a66597, urb->dev),
1696 hub_port, root_port);
1700 static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1702 struct usb_host_endpoint *hep)
1704 struct r8a66597_td *td;
1707 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
1711 pipenum = r8a66597_get_pipenum(urb, hep);
1712 td->pipenum = pipenum;
1713 td->pipe = hep->hcpriv;
1715 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1716 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1717 !usb_pipein(urb->pipe));
1718 if (usb_pipecontrol(urb->pipe))
1719 td->type = USB_PID_SETUP;
1720 else if (usb_pipein(urb->pipe))
1721 td->type = USB_PID_IN;
1723 td->type = USB_PID_OUT;
1724 INIT_LIST_HEAD(&td->queue);
1729 static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
1733 struct usb_host_endpoint *hep = urb->ep;
1734 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1735 struct r8a66597_td *td = NULL;
1736 int ret, request = 0;
1737 unsigned long flags;
1739 spin_lock_irqsave(&r8a66597->lock, flags);
1740 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1742 goto error_not_linked;
1745 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1747 goto error_not_linked;
1750 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1756 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1757 if (usb_pipeendpoint(urb->pipe))
1758 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1761 if (unlikely(check_pipe_config(r8a66597, urb)))
1762 init_pipe_config(r8a66597, urb);
1764 set_address_zero(r8a66597, urb);
1765 td = r8a66597_make_td(r8a66597, urb, hep);
1770 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1772 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
1776 ret = start_transfer(r8a66597, td);
1778 list_del(&td->queue);
1782 set_td_timer(r8a66597, td);
1786 usb_hcd_unlink_urb_from_ep(hcd, urb);
1788 spin_unlock_irqrestore(&r8a66597->lock, flags);
1792 static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1795 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1796 struct r8a66597_td *td;
1797 unsigned long flags;
1800 spin_lock_irqsave(&r8a66597->lock, flags);
1801 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1807 pipe_stop(r8a66597, td->pipe);
1808 pipe_irq_disable(r8a66597, td->pipenum);
1809 disable_irq_empty(r8a66597, td->pipenum);
1810 done(r8a66597, td, td->pipenum, urb);
1813 spin_unlock_irqrestore(&r8a66597->lock, flags);
1817 static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1818 struct usb_host_endpoint *hep)
1820 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1821 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1822 struct r8a66597_td *td;
1823 struct urb *urb = NULL;
1825 unsigned long flags;
1829 pipenum = pipe->info.pipenum;
1837 spin_lock_irqsave(&r8a66597->lock, flags);
1838 pipe_stop(r8a66597, pipe);
1839 pipe_irq_disable(r8a66597, pipenum);
1840 disable_irq_empty(r8a66597, pipenum);
1841 td = r8a66597_get_td(r8a66597, pipenum);
1844 done(r8a66597, td, pipenum, urb);
1847 spin_unlock_irqrestore(&r8a66597->lock, flags);
1850 static int r8a66597_get_frame(struct usb_hcd *hcd)
1852 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1853 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
1856 static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
1860 if (udev->state == USB_STATE_CONFIGURED &&
1861 udev->parent && udev->parent->devnum > 1 &&
1862 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
1863 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
1865 for (chix = 0; chix < udev->maxchild; chix++) {
1866 struct usb_device *childdev = udev->children[chix];
1869 collect_usb_address_map(childdev, map);
1873 /* this function must be called with interrupt disabled */
1874 static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
1877 struct r8a66597_device *dev;
1878 struct list_head *list = &r8a66597->child_device;
1880 list_for_each_entry(dev, list, device_list) {
1883 if (dev->usb_address != addr)
1889 err("get_r8a66597_device fail.(%d)\n", addr);
1893 static void update_usb_address_map(struct r8a66597 *r8a66597,
1894 struct usb_device *root_hub,
1899 unsigned long flags;
1901 for (i = 0; i < 4; i++) {
1902 diff = r8a66597->child_connect_map[i] ^ map[i];
1906 for (j = 0; j < 32; j++) {
1907 if (!(diff & (1 << j)))
1911 if (map[i] & (1 << j))
1912 set_child_connect_map(r8a66597, addr);
1914 struct r8a66597_device *dev;
1916 spin_lock_irqsave(&r8a66597->lock, flags);
1917 dev = get_r8a66597_device(r8a66597, addr);
1918 disable_r8a66597_pipe_all(r8a66597, dev);
1919 free_usb_address(r8a66597, dev);
1920 put_child_connect_map(r8a66597, addr);
1921 spin_unlock_irqrestore(&r8a66597->lock, flags);
1927 static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
1928 struct usb_hcd *hcd)
1930 struct usb_bus *bus;
1931 unsigned long now_map[4];
1933 memset(now_map, 0, sizeof(now_map));
1935 list_for_each_entry(bus, &usb_bus_list, bus_list) {
1939 if (bus->busnum != hcd->self.busnum)
1942 collect_usb_address_map(bus->root_hub, now_map);
1943 update_usb_address_map(r8a66597, bus->root_hub, now_map);
1947 static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
1949 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1950 unsigned long flags;
1953 r8a66597_check_detect_child(r8a66597, hcd);
1955 spin_lock_irqsave(&r8a66597->lock, flags);
1957 *buf = 0; /* initialize (no change) */
1959 for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++) {
1960 if (r8a66597->root_hub[i].port & 0xffff0000)
1961 *buf |= 1 << (i + 1);
1964 spin_unlock_irqrestore(&r8a66597->lock, flags);
1969 static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
1970 struct usb_hub_descriptor *desc)
1972 desc->bDescriptorType = 0x29;
1973 desc->bHubContrCurrent = 0;
1974 desc->bNbrPorts = R8A66597_MAX_ROOT_HUB;
1975 desc->bDescLength = 9;
1976 desc->bPwrOn2PwrGood = 0;
1977 desc->wHubCharacteristics = cpu_to_le16(0x0011);
1978 desc->bitmap[0] = ((1 << R8A66597_MAX_ROOT_HUB) - 1) << 1;
1979 desc->bitmap[1] = ~0;
1982 static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
1983 u16 wIndex, char *buf, u16 wLength)
1985 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1987 int port = (wIndex & 0x00FF) - 1;
1988 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1989 unsigned long flags;
1993 spin_lock_irqsave(&r8a66597->lock, flags);
1995 case ClearHubFeature:
1998 case C_HUB_OVER_CURRENT:
1999 case C_HUB_LOCAL_POWER:
2005 case ClearPortFeature:
2006 if (wIndex > R8A66597_MAX_ROOT_HUB)
2012 case USB_PORT_FEAT_ENABLE:
2013 rh->port &= (1 << USB_PORT_FEAT_POWER);
2015 case USB_PORT_FEAT_SUSPEND:
2017 case USB_PORT_FEAT_POWER:
2018 r8a66597_port_power(r8a66597, port, 0);
2020 case USB_PORT_FEAT_C_ENABLE:
2021 case USB_PORT_FEAT_C_SUSPEND:
2022 case USB_PORT_FEAT_C_CONNECTION:
2023 case USB_PORT_FEAT_C_OVER_CURRENT:
2024 case USB_PORT_FEAT_C_RESET:
2029 rh->port &= ~(1 << wValue);
2031 case GetHubDescriptor:
2032 r8a66597_hub_descriptor(r8a66597,
2033 (struct usb_hub_descriptor *)buf);
2039 if (wIndex > R8A66597_MAX_ROOT_HUB)
2041 *(u32 *)buf = rh->port;
2043 case SetPortFeature:
2044 if (wIndex > R8A66597_MAX_ROOT_HUB)
2050 case USB_PORT_FEAT_SUSPEND:
2052 case USB_PORT_FEAT_POWER:
2053 r8a66597_port_power(r8a66597, port, 1);
2054 rh->port |= (1 << USB_PORT_FEAT_POWER);
2056 case USB_PORT_FEAT_RESET: {
2057 struct r8a66597_device *dev = rh->dev;
2059 rh->port |= (1 << USB_PORT_FEAT_RESET);
2061 disable_r8a66597_pipe_all(r8a66597, dev);
2062 free_usb_address(r8a66597, dev);
2064 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2065 get_dvstctr_reg(port));
2066 mod_timer(&r8a66597->rh_timer,
2067 jiffies + msecs_to_jiffies(50));
2073 rh->port |= 1 << wValue;
2081 spin_unlock_irqrestore(&r8a66597->lock, flags);
2085 static struct hc_driver r8a66597_hc_driver = {
2086 .description = hcd_name,
2087 .hcd_priv_size = sizeof(struct r8a66597),
2088 .irq = r8a66597_irq,
2091 * generic hardware linkage
2095 .start = r8a66597_start,
2096 .stop = r8a66597_stop,
2099 * managing i/o requests and associated device resources
2101 .urb_enqueue = r8a66597_urb_enqueue,
2102 .urb_dequeue = r8a66597_urb_dequeue,
2103 .endpoint_disable = r8a66597_endpoint_disable,
2106 * periodic schedule support
2108 .get_frame_number = r8a66597_get_frame,
2113 .hub_status_data = r8a66597_hub_status_data,
2114 .hub_control = r8a66597_hub_control,
2117 #if defined(CONFIG_PM)
2118 static int r8a66597_suspend(struct platform_device *pdev, pm_message_t state)
2120 pdev->dev.power.power_state = state;
2124 static int r8a66597_resume(struct platform_device *pdev)
2126 pdev->dev.power.power_state = PMSG_ON;
2129 #else /* if defined(CONFIG_PM) */
2130 #define r8a66597_suspend NULL
2131 #define r8a66597_resume NULL
2134 static int __init_or_module r8a66597_remove(struct platform_device *pdev)
2136 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2137 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2139 del_timer_sync(&r8a66597->rh_timer);
2140 iounmap((void *)r8a66597->reg);
2141 usb_remove_hcd(hcd);
2146 #define resource_len(r) (((r)->end - (r)->start) + 1)
2147 static int __init r8a66597_probe(struct platform_device *pdev)
2149 struct resource *res = NULL;
2151 void __iomem *reg = NULL;
2152 struct usb_hcd *hcd = NULL;
2153 struct r8a66597 *r8a66597;
2157 if (pdev->dev.dma_mask) {
2159 err("dma not support");
2163 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2167 err("platform_get_resource_byname error.");
2171 irq = platform_get_irq(pdev, 0);
2174 err("platform_get_irq error.");
2178 reg = ioremap(res->start, resource_len(res));
2181 err("ioremap error.");
2185 /* initialize hcd */
2186 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2189 err("Failed to create hcd");
2192 r8a66597 = hcd_to_r8a66597(hcd);
2193 memset(r8a66597, 0, sizeof(struct r8a66597));
2194 dev_set_drvdata(&pdev->dev, r8a66597);
2196 spin_lock_init(&r8a66597->lock);
2197 init_timer(&r8a66597->rh_timer);
2198 r8a66597->rh_timer.function = r8a66597_timer;
2199 r8a66597->rh_timer.data = (unsigned long)r8a66597;
2200 r8a66597->reg = (unsigned long)reg;
2202 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2203 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2204 init_timer(&r8a66597->td_timer[i]);
2205 r8a66597->td_timer[i].function = r8a66597_td_timer;
2206 r8a66597->td_timer[i].data = (unsigned long)r8a66597;
2208 INIT_LIST_HEAD(&r8a66597->child_device);
2210 hcd->rsrc_start = res->start;
2211 ret = usb_add_hcd(hcd, irq, 0);
2213 err("Failed to add hcd");
2226 static struct platform_driver r8a66597_driver = {
2227 .probe = r8a66597_probe,
2228 .remove = r8a66597_remove,
2229 .suspend = r8a66597_suspend,
2230 .resume = r8a66597_resume,
2232 .name = (char *) hcd_name,
2236 static int __init r8a66597_init(void)
2241 info("driver %s, %s", hcd_name, DRIVER_VERSION);
2242 return platform_driver_register(&r8a66597_driver);
2244 module_init(r8a66597_init);
2246 static void __exit r8a66597_cleanup(void)
2248 platform_driver_unregister(&r8a66597_driver);
2250 module_exit(r8a66597_cleanup);