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");
47 MODULE_ALIAS("platform:r8a66597_hcd");
49 #define DRIVER_VERSION "10 Apr 2008"
51 static const char hcd_name[] = "r8a66597_hcd";
53 /* module parameters */
54 #if !defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
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 "
61 static unsigned short vif = LDRV;
62 module_param(vif, ushort, 0644);
63 MODULE_PARM_DESC(vif, "input VIF: 3.3V=32768, 1.5V=0(default=32768)");
65 static unsigned short endian;
66 module_param(endian, ushort, 0644);
67 MODULE_PARM_DESC(endian, "data endian: big=256, little=0 (default=0)");
69 static unsigned short irq_sense = INTL;
70 module_param(irq_sense, ushort, 0644);
71 MODULE_PARM_DESC(irq_sense, "IRQ sense: low level=32, falling edge=0 "
74 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
75 static int r8a66597_get_frame(struct usb_hcd *hcd);
77 /* this function must be called with interrupt disabled */
78 static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
83 tmp = r8a66597_read(r8a66597, INTENB0);
84 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
85 r8a66597_bset(r8a66597, 1 << pipenum, reg);
86 r8a66597_write(r8a66597, tmp, INTENB0);
89 /* this function must be called with interrupt disabled */
90 static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
95 tmp = r8a66597_read(r8a66597, INTENB0);
96 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
97 r8a66597_bclr(r8a66597, 1 << pipenum, reg);
98 r8a66597_write(r8a66597, tmp, INTENB0);
101 static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
102 u16 usbspd, u8 upphub, u8 hubport, int port)
105 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
107 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
108 r8a66597_write(r8a66597, val, devadd_reg);
111 static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
116 #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
118 r8a66597_write(r8a66597, SCKE, SYSCFG0);
119 tmp = r8a66597_read(r8a66597, SYSCFG0);
121 err("register access fail.");
124 } while ((tmp & SCKE) != SCKE);
125 r8a66597_write(r8a66597, 0x04, 0x02);
128 r8a66597_write(r8a66597, USBE, SYSCFG0);
129 tmp = r8a66597_read(r8a66597, SYSCFG0);
131 err("register access fail.");
134 } while ((tmp & USBE) != USBE);
135 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
136 r8a66597_mdfy(r8a66597, clock, XTAL, SYSCFG0);
139 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
142 tmp = r8a66597_read(r8a66597, SYSCFG0);
144 err("register access fail.");
147 } while ((tmp & SCKE) != SCKE);
148 #endif /* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */
153 static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
155 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
157 #if !defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
158 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
159 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
160 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
164 static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
168 val = port ? DRPD : DCFM | DRPD;
169 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
170 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
172 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
173 r8a66597_bclr(r8a66597, DTCHE, get_intenb_reg(port));
174 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
177 static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
181 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
182 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
184 r8a66597_port_power(r8a66597, port, 0);
187 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
189 } while (tmp == EDGESTS);
191 val = port ? DRPD : DCFM | DRPD;
192 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
193 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
196 static int enable_controller(struct r8a66597 *r8a66597)
200 ret = r8a66597_clock_enable(r8a66597);
204 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
205 r8a66597_bset(r8a66597, USBE, SYSCFG0);
207 r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
208 r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
209 r8a66597_bset(r8a66597, BRDY0, BRDYENB);
210 r8a66597_bset(r8a66597, BEMP0, BEMPENB);
212 r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
213 r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
214 r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
215 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
217 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
219 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
220 r8a66597_enable_port(r8a66597, port);
225 static void disable_controller(struct r8a66597 *r8a66597)
229 r8a66597_write(r8a66597, 0, INTENB0);
230 r8a66597_write(r8a66597, 0, INTSTS0);
232 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
233 r8a66597_disable_port(r8a66597, port);
235 r8a66597_clock_disable(r8a66597);
238 static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
239 struct usb_device *udev)
241 struct r8a66597_device *dev;
243 if (udev->parent && udev->parent->devnum != 1)
246 dev = dev_get_drvdata(&udev->dev);
253 static int is_child_device(char *devpath)
255 return (devpath[2] ? 1 : 0);
258 static int is_hub_limit(char *devpath)
260 return ((strlen(devpath) >= 4) ? 1 : 0);
263 static void get_port_number(char *devpath, u16 *root_port, u16 *hub_port)
266 *root_port = (devpath[0] & 0x0F) - 1;
267 if (*root_port >= R8A66597_MAX_ROOT_HUB)
268 err("illegal root port number");
271 *hub_port = devpath[2] & 0x0F;
274 static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
289 err("unknown speed");
296 static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
301 r8a66597->child_connect_map[idx] |= 1 << (address % 32);
304 static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
309 r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
312 static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
314 u16 pipenum = pipe->info.pipenum;
315 unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
316 unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
317 unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
319 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
320 dma_ch = R8A66597_PIPE_NO_DMA;
322 pipe->fifoaddr = fifoaddr[dma_ch];
323 pipe->fifosel = fifosel[dma_ch];
324 pipe->fifoctr = fifoctr[dma_ch];
327 pipe->pipectr = DCPCTR;
329 pipe->pipectr = get_pipectr_addr(pipenum);
331 if (check_bulk_or_isoc(pipenum)) {
332 pipe->pipetre = get_pipetre_addr(pipenum);
333 pipe->pipetrn = get_pipetrn_addr(pipenum);
340 static struct r8a66597_device *
341 get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
343 if (usb_pipedevice(urb->pipe) == 0)
344 return &r8a66597->device0;
346 return dev_get_drvdata(&urb->dev->dev);
349 static int make_r8a66597_device(struct r8a66597 *r8a66597,
350 struct urb *urb, u8 addr)
352 struct r8a66597_device *dev;
353 int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
355 dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
359 dev_set_drvdata(&urb->dev->dev, dev);
360 dev->udev = urb->dev;
362 dev->usb_address = usb_address;
363 dev->state = USB_STATE_ADDRESS;
364 dev->ep_in_toggle = 0;
365 dev->ep_out_toggle = 0;
366 INIT_LIST_HEAD(&dev->device_list);
367 list_add_tail(&dev->device_list, &r8a66597->child_device);
369 get_port_number(urb->dev->devpath, &dev->root_port, &dev->hub_port);
370 if (!is_child_device(urb->dev->devpath))
371 r8a66597->root_hub[dev->root_port].dev = dev;
373 set_devadd_reg(r8a66597, dev->address,
374 get_r8a66597_usb_speed(urb->dev->speed),
375 get_parent_r8a66597_address(r8a66597, urb->dev),
376 dev->hub_port, dev->root_port);
381 /* this function must be called with interrupt disabled */
382 static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
384 u8 addr; /* R8A66597's address */
385 struct r8a66597_device *dev;
387 if (is_hub_limit(urb->dev->devpath)) {
388 err("Externel hub limit reached.");
392 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
393 if (dev && dev->state >= USB_STATE_ADDRESS)
396 for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
397 if (r8a66597->address_map & (1 << addr))
400 dbg("alloc_address: r8a66597_addr=%d", addr);
401 r8a66597->address_map |= 1 << addr;
403 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
409 err("cannot communicate with a USB device more than 10.(%x)",
410 r8a66597->address_map);
415 /* this function must be called with interrupt disabled */
416 static void free_usb_address(struct r8a66597 *r8a66597,
417 struct r8a66597_device *dev)
424 dbg("free_addr: addr=%d", dev->address);
426 dev->state = USB_STATE_DEFAULT;
427 r8a66597->address_map &= ~(1 << dev->address);
429 dev_set_drvdata(&dev->udev->dev, NULL);
430 list_del(&dev->device_list);
433 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
434 if (r8a66597->root_hub[port].dev == dev) {
435 r8a66597->root_hub[port].dev = NULL;
441 static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
448 tmp = r8a66597_read(r8a66597, reg);
450 err("register%lx, loop %x is timeout", reg, loop);
454 } while ((tmp & mask) != loop);
457 /* this function must be called with interrupt disabled */
458 static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
462 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
463 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
464 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
465 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
468 /* this function must be called with interrupt disabled */
469 static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
473 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
474 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
475 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
476 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
477 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
480 /* this function must be called with interrupt disabled */
481 static void clear_all_buffer(struct r8a66597 *r8a66597,
482 struct r8a66597_pipe *pipe)
486 if (!pipe || pipe->info.pipenum == 0)
489 pipe_stop(r8a66597, pipe);
490 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
491 tmp = r8a66597_read(r8a66597, pipe->pipectr);
492 tmp = r8a66597_read(r8a66597, pipe->pipectr);
493 tmp = r8a66597_read(r8a66597, pipe->pipectr);
494 r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
497 /* this function must be called with interrupt disabled */
498 static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
499 struct r8a66597_pipe *pipe, int toggle)
502 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
504 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
507 /* this function must be called with interrupt disabled */
508 static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
510 r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
511 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
514 /* this function must be called with interrupt disabled */
515 static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
516 struct r8a66597_pipe *pipe)
518 cfifo_change(r8a66597, 0);
519 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D0FIFOSEL);
520 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D1FIFOSEL);
522 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum, MBW | CURPIPE,
524 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
527 static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
529 struct r8a66597_pipe *pipe = hep->hcpriv;
531 if (usb_pipeendpoint(urb->pipe) == 0)
534 return pipe->info.pipenum;
537 static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
539 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
541 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
544 static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
550 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
553 /* this function must be called with interrupt disabled */
554 static void pipe_toggle_set(struct r8a66597 *r8a66597,
555 struct r8a66597_pipe *pipe,
556 struct urb *urb, int set)
558 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
559 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
560 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
566 *toggle |= 1 << endpoint;
568 *toggle &= ~(1 << endpoint);
571 /* this function must be called with interrupt disabled */
572 static void pipe_toggle_save(struct r8a66597 *r8a66597,
573 struct r8a66597_pipe *pipe,
576 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
577 pipe_toggle_set(r8a66597, pipe, urb, 1);
579 pipe_toggle_set(r8a66597, pipe, urb, 0);
582 /* this function must be called with interrupt disabled */
583 static void pipe_toggle_restore(struct r8a66597 *r8a66597,
584 struct r8a66597_pipe *pipe,
587 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
588 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
589 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
594 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
597 /* this function must be called with interrupt disabled */
598 static void pipe_buffer_setting(struct r8a66597 *r8a66597,
599 struct r8a66597_pipe_info *info)
603 if (info->pipenum == 0)
606 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
607 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
608 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
611 if (info->type == R8A66597_BULK && info->dir_in)
612 val |= R8A66597_DBLB | R8A66597_SHTNAK;
613 val |= info->type | info->epnum;
614 r8a66597_write(r8a66597, val, PIPECFG);
616 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
618 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
620 r8a66597_write(r8a66597, info->interval, PIPEPERI);
623 /* this function must be called with interrupt disabled */
624 static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
626 struct r8a66597_pipe_info *info;
627 struct urb *urb = td->urb;
629 if (td->pipenum > 0) {
630 info = &td->pipe->info;
631 cfifo_change(r8a66597, 0);
632 pipe_buffer_setting(r8a66597, info);
634 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
635 usb_pipeout(urb->pipe)) &&
636 !usb_pipecontrol(urb->pipe)) {
637 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
638 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
639 clear_all_buffer(r8a66597, td->pipe);
640 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
641 usb_pipeout(urb->pipe), 1);
643 pipe_toggle_restore(r8a66597, td->pipe, urb);
647 /* this function must be called with interrupt disabled */
648 static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
649 struct usb_endpoint_descriptor *ep)
651 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
653 memset(array, 0, sizeof(array));
654 switch (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
655 case USB_ENDPOINT_XFER_BULK:
656 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
663 case USB_ENDPOINT_XFER_INT:
664 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
671 case USB_ENDPOINT_XFER_ISOC:
672 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
684 while (array[i] != 0) {
685 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
693 static u16 get_r8a66597_type(__u8 type)
698 case USB_ENDPOINT_XFER_BULK:
699 r8a66597_type = R8A66597_BULK;
701 case USB_ENDPOINT_XFER_INT:
702 r8a66597_type = R8A66597_INT;
704 case USB_ENDPOINT_XFER_ISOC:
705 r8a66597_type = R8A66597_ISO;
709 r8a66597_type = 0x0000;
713 return r8a66597_type;
716 static u16 get_bufnum(u16 pipenum)
722 else if (check_bulk_or_isoc(pipenum))
723 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
724 else if (check_interrupt(pipenum))
725 bufnum = 4 + (pipenum - 6);
727 err("Illegal pipenum (%d)", pipenum);
732 static u16 get_buf_bsize(u16 pipenum)
738 else if (check_bulk_or_isoc(pipenum))
739 buf_bsize = R8A66597_BUF_BSIZE - 1;
740 else if (check_interrupt(pipenum))
743 err("Illegal pipenum (%d)", pipenum);
748 /* this function must be called with interrupt disabled */
749 static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
750 struct r8a66597_device *dev,
751 struct r8a66597_pipe *pipe,
754 #if !defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
756 struct r8a66597_pipe_info *info = &pipe->info;
758 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
759 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
760 if ((r8a66597->dma_map & (1 << i)) != 0)
763 info("address %d, EndpointAddress 0x%02x use DMA FIFO",
764 usb_pipedevice(urb->pipe),
765 info->dir_in ? USB_ENDPOINT_DIR_MASK + info->epnum
768 r8a66597->dma_map |= 1 << i;
769 dev->dma_map |= 1 << i;
770 set_pipe_reg_addr(pipe, i);
772 cfifo_change(r8a66597, 0);
773 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum,
774 MBW | CURPIPE, pipe->fifosel);
776 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
778 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
782 #endif /* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */
785 /* this function must be called with interrupt disabled */
786 static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
787 struct usb_host_endpoint *hep,
788 struct r8a66597_pipe_info *info)
790 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
791 struct r8a66597_pipe *pipe = hep->hcpriv;
796 set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
797 r8a66597->pipe_cnt[pipe->info.pipenum]++;
798 dev->pipe_cnt[pipe->info.pipenum]++;
800 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
803 /* this function must be called with interrupt disabled */
804 static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
806 struct r8a66597_td *td, *next;
808 struct list_head *list = &r8a66597->pipe_queue[pipenum];
810 if (list_empty(list))
813 list_for_each_entry_safe(td, next, list, queue) {
816 if (td->address != address)
820 list_del(&td->queue);
824 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597),
827 spin_unlock(&r8a66597->lock);
828 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb,
830 spin_lock(&r8a66597->lock);
836 /* this function must be called with interrupt disabled */
837 static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
838 struct r8a66597_device *dev)
846 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
847 if (!dev->pipe_cnt[pipenum])
852 force_dequeue(r8a66597, 0, dev->address);
855 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
856 dev->pipe_cnt[pipenum] = 0;
857 force_dequeue(r8a66597, pipenum, dev->address);
862 r8a66597->dma_map &= ~(dev->dma_map);
866 static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
869 unsigned long time = 1;
871 if (usb_pipeisoc(urb->pipe))
874 if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
875 for (i = 0; i < (interval - 1); i++)
877 time = time * 125 / 1000; /* uSOF -> msec */
885 /* this function must be called with interrupt disabled */
886 static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
887 struct usb_host_endpoint *hep,
888 struct usb_endpoint_descriptor *ep)
890 struct r8a66597_pipe_info info;
892 info.pipenum = get_empty_pipenum(r8a66597, ep);
893 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
894 info.epnum = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
895 info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
896 info.type = get_r8a66597_type(ep->bmAttributes
897 & USB_ENDPOINT_XFERTYPE_MASK);
898 info.bufnum = get_bufnum(info.pipenum);
899 info.buf_bsize = get_buf_bsize(info.pipenum);
900 if (info.type == R8A66597_BULK) {
902 info.timer_interval = 0;
904 if (ep->bInterval > IITV)
905 info.interval = IITV;
907 info.interval = ep->bInterval ? ep->bInterval - 1 : 0;
908 info.timer_interval = get_timer_interval(urb, ep->bInterval);
910 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
915 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
918 static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
920 struct r8a66597_device *dev;
922 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
923 dev->state = USB_STATE_CONFIGURED;
926 static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
929 if (pipenum == 0 && usb_pipeout(urb->pipe))
930 enable_irq_empty(r8a66597, pipenum);
932 enable_irq_ready(r8a66597, pipenum);
934 if (!usb_pipeisoc(urb->pipe))
935 enable_irq_nrdy(r8a66597, pipenum);
938 static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
940 disable_irq_ready(r8a66597, pipenum);
941 disable_irq_nrdy(r8a66597, pipenum);
944 /* this function must be called with interrupt disabled */
945 static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
949 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
953 if (syssts == FS_JSTS)
954 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
955 else if (syssts == LS_JSTS)
956 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
958 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
959 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
962 /* this function must be called with interrupt disabled */
963 static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
965 u16 speed = get_rh_usb_speed(r8a66597, port);
966 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
969 rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
970 else if (speed == LSMODE)
971 rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
973 rh->port &= ~(1 << USB_PORT_FEAT_RESET);
974 rh->port |= 1 << USB_PORT_FEAT_ENABLE;
977 /* this function must be called with interrupt disabled */
978 static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
980 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
982 r8a66597->root_hub[port].port &= ~(1 << USB_PORT_FEAT_CONNECTION);
983 r8a66597->root_hub[port].port |= (1 << USB_PORT_FEAT_C_CONNECTION);
985 disable_r8a66597_pipe_all(r8a66597, dev);
986 free_usb_address(r8a66597, dev);
988 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
991 /* this function must be called with interrupt disabled */
992 static void prepare_setup_packet(struct r8a66597 *r8a66597,
993 struct r8a66597_td *td)
996 u16 *p = (u16 *)td->urb->setup_packet;
997 unsigned long setup_addr = USBREQ;
999 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1001 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
1003 for (i = 0; i < 4; i++) {
1004 r8a66597_write(r8a66597, cpu_to_le16(p[i]), setup_addr);
1007 r8a66597_write(r8a66597, SUREQ, DCPCTR);
1010 /* this function must be called with interrupt disabled */
1011 static void prepare_packet_read(struct r8a66597 *r8a66597,
1012 struct r8a66597_td *td)
1014 struct urb *urb = td->urb;
1016 if (usb_pipecontrol(urb->pipe)) {
1017 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1018 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1019 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1020 if (urb->actual_length == 0) {
1021 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1022 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1024 pipe_irq_disable(r8a66597, td->pipenum);
1025 pipe_start(r8a66597, td->pipe);
1026 pipe_irq_enable(r8a66597, urb, td->pipenum);
1028 if (urb->actual_length == 0) {
1029 pipe_irq_disable(r8a66597, td->pipenum);
1030 pipe_setting(r8a66597, td);
1031 pipe_stop(r8a66597, td->pipe);
1032 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1034 if (td->pipe->pipetre) {
1035 r8a66597_write(r8a66597, TRCLR,
1037 r8a66597_write(r8a66597,
1039 (urb->transfer_buffer_length,
1042 r8a66597_bset(r8a66597, TRENB,
1046 pipe_start(r8a66597, td->pipe);
1047 pipe_irq_enable(r8a66597, urb, td->pipenum);
1052 /* this function must be called with interrupt disabled */
1053 static void prepare_packet_write(struct r8a66597 *r8a66597,
1054 struct r8a66597_td *td)
1057 struct urb *urb = td->urb;
1059 if (usb_pipecontrol(urb->pipe)) {
1060 pipe_stop(r8a66597, td->pipe);
1061 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1062 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1063 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1064 if (urb->actual_length == 0) {
1065 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1066 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1069 if (urb->actual_length == 0)
1070 pipe_setting(r8a66597, td);
1071 if (td->pipe->pipetre)
1072 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1074 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1076 fifo_change_from_pipe(r8a66597, td->pipe);
1077 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1078 if (unlikely((tmp & FRDY) == 0))
1079 pipe_irq_enable(r8a66597, urb, td->pipenum);
1081 packet_write(r8a66597, td->pipenum);
1082 pipe_start(r8a66597, td->pipe);
1085 /* this function must be called with interrupt disabled */
1086 static void prepare_status_packet(struct r8a66597 *r8a66597,
1087 struct r8a66597_td *td)
1089 struct urb *urb = td->urb;
1091 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1092 pipe_stop(r8a66597, td->pipe);
1094 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1095 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1096 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1097 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1098 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
1099 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
1100 enable_irq_empty(r8a66597, 0);
1102 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1103 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1104 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1105 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1106 enable_irq_ready(r8a66597, 0);
1108 enable_irq_nrdy(r8a66597, 0);
1109 pipe_start(r8a66597, td->pipe);
1112 static int is_set_address(unsigned char *setup_packet)
1114 if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1115 setup_packet[1] == USB_REQ_SET_ADDRESS)
1121 /* this function must be called with interrupt disabled */
1122 static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1128 if (is_set_address(td->urb->setup_packet)) {
1129 td->set_address = 1;
1130 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1132 if (td->urb->setup_packet[2] == 0)
1135 prepare_setup_packet(r8a66597, td);
1138 prepare_packet_read(r8a66597, td);
1141 prepare_packet_write(r8a66597, td);
1144 prepare_status_packet(r8a66597, td);
1147 err("invalid type.");
1154 static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1156 if (usb_pipeisoc(urb->pipe)) {
1157 if (urb->number_of_packets == td->iso_cnt)
1161 /* control or bulk or interrupt */
1162 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1163 (td->short_packet) || (td->zero_packet))
1169 /* this function must be called with interrupt disabled */
1170 static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1176 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1177 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1178 r8a66597->timeout_map |= 1 << td->pipenum;
1179 switch (usb_pipetype(td->urb->pipe)) {
1180 case PIPE_INTERRUPT:
1181 case PIPE_ISOCHRONOUS:
1189 mod_timer(&r8a66597->td_timer[td->pipenum],
1190 jiffies + msecs_to_jiffies(time));
1194 /* this function must be called with interrupt disabled */
1195 static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
1196 u16 pipenum, struct urb *urb, int status)
1197 __releases(r8a66597->lock) __acquires(r8a66597->lock)
1200 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1202 r8a66597->timeout_map &= ~(1 << pipenum);
1205 if (td->set_address && (status != 0 || urb->unlinked))
1206 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1208 pipe_toggle_save(r8a66597, td->pipe, urb);
1209 list_del(&td->queue);
1213 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1217 if (usb_pipeisoc(urb->pipe))
1218 urb->start_frame = r8a66597_get_frame(hcd);
1220 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
1221 spin_unlock(&r8a66597->lock);
1222 usb_hcd_giveback_urb(hcd, urb, status);
1223 spin_lock(&r8a66597->lock);
1227 td = r8a66597_get_td(r8a66597, pipenum);
1231 start_transfer(r8a66597, td);
1232 set_td_timer(r8a66597, td);
1236 static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1239 int rcv_len, bufsize, urb_len, size;
1241 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1250 fifo_change_from_pipe(r8a66597, td->pipe);
1251 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1252 if (unlikely((tmp & FRDY) == 0)) {
1253 pipe_stop(r8a66597, td->pipe);
1254 pipe_irq_disable(r8a66597, pipenum);
1255 err("in fifo not ready (%d)", pipenum);
1256 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
1260 /* prepare parameters */
1261 rcv_len = tmp & DTLN;
1262 if (usb_pipeisoc(urb->pipe)) {
1263 buf = (u16 *)(urb->transfer_buffer +
1264 urb->iso_frame_desc[td->iso_cnt].offset);
1265 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1267 buf = (void *)urb->transfer_buffer + urb->actual_length;
1268 urb_len = urb->transfer_buffer_length - urb->actual_length;
1270 bufsize = min(urb_len, (int) td->maxpacket);
1271 if (rcv_len <= bufsize) {
1275 status = -EOVERFLOW;
1279 /* update parameters */
1280 urb->actual_length += size;
1282 td->zero_packet = 1;
1283 if (rcv_len < bufsize) {
1284 td->short_packet = 1;
1286 if (usb_pipeisoc(urb->pipe)) {
1287 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1288 urb->iso_frame_desc[td->iso_cnt].status = status;
1293 /* check transfer finish */
1294 if (finish || check_transfer_finish(td, urb)) {
1295 pipe_stop(r8a66597, td->pipe);
1296 pipe_irq_disable(r8a66597, pipenum);
1301 if (urb->transfer_buffer) {
1303 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1305 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1309 if (finish && pipenum != 0)
1310 finish_request(r8a66597, td, pipenum, urb, status);
1313 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1318 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1325 fifo_change_from_pipe(r8a66597, td->pipe);
1326 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1327 if (unlikely((tmp & FRDY) == 0)) {
1328 pipe_stop(r8a66597, td->pipe);
1329 pipe_irq_disable(r8a66597, pipenum);
1330 err("out write fifo not ready. (%d)", pipenum);
1331 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
1335 /* prepare parameters */
1336 bufsize = td->maxpacket;
1337 if (usb_pipeisoc(urb->pipe)) {
1338 buf = (u16 *)(urb->transfer_buffer +
1339 urb->iso_frame_desc[td->iso_cnt].offset);
1341 (int)urb->iso_frame_desc[td->iso_cnt].length);
1343 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
1344 size = min((int)bufsize,
1345 urb->transfer_buffer_length - urb->actual_length);
1350 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
1351 if (urb->transfer_buffer) {
1352 r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
1353 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1354 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1357 /* update parameters */
1358 urb->actual_length += size;
1359 if (usb_pipeisoc(urb->pipe)) {
1360 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1361 urb->iso_frame_desc[td->iso_cnt].status = 0;
1365 /* check transfer finish */
1366 if (check_transfer_finish(td, urb)) {
1367 disable_irq_ready(r8a66597, pipenum);
1368 enable_irq_empty(r8a66597, pipenum);
1369 if (!usb_pipeisoc(urb->pipe))
1370 enable_irq_nrdy(r8a66597, pipenum);
1372 pipe_irq_enable(r8a66597, urb, pipenum);
1376 static void check_next_phase(struct r8a66597 *r8a66597, int status)
1378 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1389 if (check_transfer_finish(td, urb))
1390 td->type = USB_PID_ACK;
1393 if (urb->transfer_buffer_length == urb->actual_length)
1394 td->type = USB_PID_ACK;
1395 else if (usb_pipeout(urb->pipe))
1396 td->type = USB_PID_OUT;
1398 td->type = USB_PID_IN;
1405 if (finish || status != 0 || urb->unlinked)
1406 finish_request(r8a66597, td, 0, urb, status);
1408 start_transfer(r8a66597, td);
1411 static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
1413 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1416 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1426 static void irq_pipe_ready(struct r8a66597 *r8a66597)
1431 struct r8a66597_td *td;
1433 mask = r8a66597_read(r8a66597, BRDYSTS)
1434 & r8a66597_read(r8a66597, BRDYENB);
1435 r8a66597_write(r8a66597, ~mask, BRDYSTS);
1437 td = r8a66597_get_td(r8a66597, 0);
1438 if (td && td->type == USB_PID_IN)
1439 packet_read(r8a66597, 0);
1441 pipe_irq_disable(r8a66597, 0);
1442 check_next_phase(r8a66597, 0);
1445 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1446 check = 1 << pipenum;
1448 td = r8a66597_get_td(r8a66597, pipenum);
1452 if (td->type == USB_PID_IN)
1453 packet_read(r8a66597, pipenum);
1454 else if (td->type == USB_PID_OUT)
1455 packet_write(r8a66597, pipenum);
1460 static void irq_pipe_empty(struct r8a66597 *r8a66597)
1466 struct r8a66597_td *td;
1468 mask = r8a66597_read(r8a66597, BEMPSTS)
1469 & r8a66597_read(r8a66597, BEMPENB);
1470 r8a66597_write(r8a66597, ~mask, BEMPSTS);
1472 cfifo_change(r8a66597, 0);
1473 td = r8a66597_get_td(r8a66597, 0);
1474 if (td && td->type != USB_PID_OUT)
1475 disable_irq_empty(r8a66597, 0);
1476 check_next_phase(r8a66597, 0);
1479 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1480 check = 1 << pipenum;
1482 struct r8a66597_td *td;
1483 td = r8a66597_get_td(r8a66597, pipenum);
1487 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1488 if ((tmp & INBUFM) == 0) {
1489 disable_irq_empty(r8a66597, pipenum);
1490 pipe_irq_disable(r8a66597, pipenum);
1491 finish_request(r8a66597, td, pipenum, td->urb,
1498 static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1505 mask = r8a66597_read(r8a66597, NRDYSTS)
1506 & r8a66597_read(r8a66597, NRDYENB);
1507 r8a66597_write(r8a66597, ~mask, NRDYSTS);
1509 cfifo_change(r8a66597, 0);
1510 status = get_urb_error(r8a66597, 0);
1511 pipe_irq_disable(r8a66597, 0);
1512 check_next_phase(r8a66597, status);
1515 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1516 check = 1 << pipenum;
1518 struct r8a66597_td *td;
1519 td = r8a66597_get_td(r8a66597, pipenum);
1523 status = get_urb_error(r8a66597, pipenum);
1524 pipe_irq_disable(r8a66597, pipenum);
1525 pipe_stop(r8a66597, td->pipe);
1526 finish_request(r8a66597, td, pipenum, td->urb, status);
1531 static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
1533 mod_timer(&r8a66597->rh_timer,
1534 jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
1537 static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port)
1539 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1541 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1542 rh->scount = R8A66597_MAX_SAMPLING;
1543 r8a66597->root_hub[port].port |= (1 << USB_PORT_FEAT_CONNECTION)
1544 | (1 << USB_PORT_FEAT_C_CONNECTION);
1545 r8a66597_root_hub_start_polling(r8a66597);
1548 static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1550 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1551 u16 intsts0, intsts1, intsts2;
1552 u16 intenb0, intenb1, intenb2;
1553 u16 mask0, mask1, mask2;
1556 spin_lock(&r8a66597->lock);
1558 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1559 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1560 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1561 intenb0 = r8a66597_read(r8a66597, INTENB0);
1562 intenb1 = r8a66597_read(r8a66597, INTENB1);
1563 intenb2 = r8a66597_read(r8a66597, INTENB2);
1565 mask2 = intsts2 & intenb2;
1566 mask1 = intsts1 & intenb1;
1567 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1569 if (mask2 & ATTCH) {
1570 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
1571 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1573 /* start usb bus sampling */
1574 start_root_hub_sampling(r8a66597, 1);
1577 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
1578 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1579 r8a66597_usb_disconnect(r8a66597, 1);
1584 if (mask1 & ATTCH) {
1585 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
1586 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1588 /* start usb bus sampling */
1589 start_root_hub_sampling(r8a66597, 0);
1592 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
1593 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1594 r8a66597_usb_disconnect(r8a66597, 0);
1597 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
1598 status = get_urb_error(r8a66597, 0);
1599 check_next_phase(r8a66597, status);
1602 r8a66597_write(r8a66597, ~SACK, INTSTS1);
1603 check_next_phase(r8a66597, 0);
1608 irq_pipe_ready(r8a66597);
1610 irq_pipe_empty(r8a66597);
1612 irq_pipe_nrdy(r8a66597);
1615 spin_unlock(&r8a66597->lock);
1619 /* this function must be called with interrupt disabled */
1620 static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1623 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1625 if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
1626 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1628 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1629 if ((tmp & USBRST) == USBRST) {
1630 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1632 r8a66597_root_hub_start_polling(r8a66597);
1634 r8a66597_usb_connect(r8a66597, port);
1637 if (!(rh->port & (1 << USB_PORT_FEAT_CONNECTION))) {
1638 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1639 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1642 if (rh->scount > 0) {
1643 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1644 if (tmp == rh->old_syssts) {
1646 if (rh->scount == 0)
1647 r8a66597_check_syssts(r8a66597, port, tmp);
1649 r8a66597_root_hub_start_polling(r8a66597);
1651 rh->scount = R8A66597_MAX_SAMPLING;
1652 rh->old_syssts = tmp;
1653 r8a66597_root_hub_start_polling(r8a66597);
1658 static void r8a66597_interval_timer(unsigned long _r8a66597)
1660 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1661 unsigned long flags;
1663 struct r8a66597_td *td;
1665 spin_lock_irqsave(&r8a66597->lock, flags);
1667 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1668 if (!(r8a66597->interval_map & (1 << pipenum)))
1670 if (timer_pending(&r8a66597->interval_timer[pipenum]))
1673 td = r8a66597_get_td(r8a66597, pipenum);
1675 start_transfer(r8a66597, td);
1678 spin_unlock_irqrestore(&r8a66597->lock, flags);
1681 static void r8a66597_td_timer(unsigned long _r8a66597)
1683 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1684 unsigned long flags;
1686 struct r8a66597_td *td, *new_td = NULL;
1687 struct r8a66597_pipe *pipe;
1689 spin_lock_irqsave(&r8a66597->lock, flags);
1690 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1691 if (!(r8a66597->timeout_map & (1 << pipenum)))
1693 if (timer_pending(&r8a66597->td_timer[pipenum]))
1696 td = r8a66597_get_td(r8a66597, pipenum);
1698 r8a66597->timeout_map &= ~(1 << pipenum);
1702 if (td->urb->actual_length) {
1703 set_td_timer(r8a66597, td);
1708 pipe_stop(r8a66597, pipe);
1712 list_move_tail(&new_td->queue,
1713 &r8a66597->pipe_queue[pipenum]);
1714 new_td = r8a66597_get_td(r8a66597, pipenum);
1719 } while (td != new_td && td->address == new_td->address);
1721 start_transfer(r8a66597, new_td);
1724 r8a66597->timeout_map &= ~(1 << pipenum);
1726 set_td_timer(r8a66597, new_td);
1729 spin_unlock_irqrestore(&r8a66597->lock, flags);
1732 static void r8a66597_timer(unsigned long _r8a66597)
1734 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1735 unsigned long flags;
1737 spin_lock_irqsave(&r8a66597->lock, flags);
1739 r8a66597_root_hub_control(r8a66597, 0);
1740 r8a66597_root_hub_control(r8a66597, 1);
1742 spin_unlock_irqrestore(&r8a66597->lock, flags);
1745 static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1747 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1749 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1750 (urb->dev->state == USB_STATE_CONFIGURED))
1756 static int r8a66597_start(struct usb_hcd *hcd)
1758 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1760 hcd->state = HC_STATE_RUNNING;
1761 return enable_controller(r8a66597);
1764 static void r8a66597_stop(struct usb_hcd *hcd)
1766 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1768 disable_controller(r8a66597);
1771 static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1773 unsigned int usb_address = usb_pipedevice(urb->pipe);
1774 u16 root_port, hub_port;
1776 if (usb_address == 0) {
1777 get_port_number(urb->dev->devpath,
1778 &root_port, &hub_port);
1779 set_devadd_reg(r8a66597, 0,
1780 get_r8a66597_usb_speed(urb->dev->speed),
1781 get_parent_r8a66597_address(r8a66597, urb->dev),
1782 hub_port, root_port);
1786 static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1788 struct usb_host_endpoint *hep)
1790 struct r8a66597_td *td;
1793 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
1797 pipenum = r8a66597_get_pipenum(urb, hep);
1798 td->pipenum = pipenum;
1799 td->pipe = hep->hcpriv;
1801 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1802 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1803 !usb_pipein(urb->pipe));
1804 if (usb_pipecontrol(urb->pipe))
1805 td->type = USB_PID_SETUP;
1806 else if (usb_pipein(urb->pipe))
1807 td->type = USB_PID_IN;
1809 td->type = USB_PID_OUT;
1810 INIT_LIST_HEAD(&td->queue);
1815 static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
1819 struct usb_host_endpoint *hep = urb->ep;
1820 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1821 struct r8a66597_td *td = NULL;
1822 int ret, request = 0;
1823 unsigned long flags;
1825 spin_lock_irqsave(&r8a66597->lock, flags);
1826 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1828 goto error_not_linked;
1831 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1833 goto error_not_linked;
1836 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1842 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1843 if (usb_pipeendpoint(urb->pipe))
1844 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1847 if (unlikely(check_pipe_config(r8a66597, urb)))
1848 init_pipe_config(r8a66597, urb);
1850 set_address_zero(r8a66597, urb);
1851 td = r8a66597_make_td(r8a66597, urb, hep);
1856 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1858 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
1862 if (td->pipe->info.timer_interval) {
1863 r8a66597->interval_map |= 1 << td->pipenum;
1864 mod_timer(&r8a66597->interval_timer[td->pipenum],
1865 jiffies + msecs_to_jiffies(
1866 td->pipe->info.timer_interval));
1868 ret = start_transfer(r8a66597, td);
1870 list_del(&td->queue);
1875 set_td_timer(r8a66597, td);
1879 usb_hcd_unlink_urb_from_ep(hcd, urb);
1881 spin_unlock_irqrestore(&r8a66597->lock, flags);
1885 static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1888 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1889 struct r8a66597_td *td;
1890 unsigned long flags;
1893 spin_lock_irqsave(&r8a66597->lock, flags);
1894 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1900 pipe_stop(r8a66597, td->pipe);
1901 pipe_irq_disable(r8a66597, td->pipenum);
1902 disable_irq_empty(r8a66597, td->pipenum);
1903 finish_request(r8a66597, td, td->pipenum, urb, status);
1906 spin_unlock_irqrestore(&r8a66597->lock, flags);
1910 static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1911 struct usb_host_endpoint *hep)
1913 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1914 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1915 struct r8a66597_td *td;
1916 struct urb *urb = NULL;
1918 unsigned long flags;
1922 pipenum = pipe->info.pipenum;
1930 spin_lock_irqsave(&r8a66597->lock, flags);
1931 pipe_stop(r8a66597, pipe);
1932 pipe_irq_disable(r8a66597, pipenum);
1933 disable_irq_empty(r8a66597, pipenum);
1934 td = r8a66597_get_td(r8a66597, pipenum);
1937 finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
1940 spin_unlock_irqrestore(&r8a66597->lock, flags);
1943 static int r8a66597_get_frame(struct usb_hcd *hcd)
1945 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1946 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
1949 static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
1953 if (udev->state == USB_STATE_CONFIGURED &&
1954 udev->parent && udev->parent->devnum > 1 &&
1955 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
1956 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
1958 for (chix = 0; chix < udev->maxchild; chix++) {
1959 struct usb_device *childdev = udev->children[chix];
1962 collect_usb_address_map(childdev, map);
1966 /* this function must be called with interrupt disabled */
1967 static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
1970 struct r8a66597_device *dev;
1971 struct list_head *list = &r8a66597->child_device;
1973 list_for_each_entry(dev, list, device_list) {
1976 if (dev->usb_address != addr)
1982 err("get_r8a66597_device fail.(%d)\n", addr);
1986 static void update_usb_address_map(struct r8a66597 *r8a66597,
1987 struct usb_device *root_hub,
1992 unsigned long flags;
1994 for (i = 0; i < 4; i++) {
1995 diff = r8a66597->child_connect_map[i] ^ map[i];
1999 for (j = 0; j < 32; j++) {
2000 if (!(diff & (1 << j)))
2004 if (map[i] & (1 << j))
2005 set_child_connect_map(r8a66597, addr);
2007 struct r8a66597_device *dev;
2009 spin_lock_irqsave(&r8a66597->lock, flags);
2010 dev = get_r8a66597_device(r8a66597, addr);
2011 disable_r8a66597_pipe_all(r8a66597, dev);
2012 free_usb_address(r8a66597, dev);
2013 put_child_connect_map(r8a66597, addr);
2014 spin_unlock_irqrestore(&r8a66597->lock, flags);
2020 static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
2021 struct usb_hcd *hcd)
2023 struct usb_bus *bus;
2024 unsigned long now_map[4];
2026 memset(now_map, 0, sizeof(now_map));
2028 list_for_each_entry(bus, &usb_bus_list, bus_list) {
2032 if (bus->busnum != hcd->self.busnum)
2035 collect_usb_address_map(bus->root_hub, now_map);
2036 update_usb_address_map(r8a66597, bus->root_hub, now_map);
2040 static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2042 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2043 unsigned long flags;
2046 r8a66597_check_detect_child(r8a66597, hcd);
2048 spin_lock_irqsave(&r8a66597->lock, flags);
2050 *buf = 0; /* initialize (no change) */
2052 for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++) {
2053 if (r8a66597->root_hub[i].port & 0xffff0000)
2054 *buf |= 1 << (i + 1);
2057 spin_unlock_irqrestore(&r8a66597->lock, flags);
2062 static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2063 struct usb_hub_descriptor *desc)
2065 desc->bDescriptorType = 0x29;
2066 desc->bHubContrCurrent = 0;
2067 desc->bNbrPorts = R8A66597_MAX_ROOT_HUB;
2068 desc->bDescLength = 9;
2069 desc->bPwrOn2PwrGood = 0;
2070 desc->wHubCharacteristics = cpu_to_le16(0x0011);
2071 desc->bitmap[0] = ((1 << R8A66597_MAX_ROOT_HUB) - 1) << 1;
2072 desc->bitmap[1] = ~0;
2075 static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2076 u16 wIndex, char *buf, u16 wLength)
2078 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2080 int port = (wIndex & 0x00FF) - 1;
2081 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2082 unsigned long flags;
2086 spin_lock_irqsave(&r8a66597->lock, flags);
2088 case ClearHubFeature:
2091 case C_HUB_OVER_CURRENT:
2092 case C_HUB_LOCAL_POWER:
2098 case ClearPortFeature:
2099 if (wIndex > R8A66597_MAX_ROOT_HUB)
2105 case USB_PORT_FEAT_ENABLE:
2106 rh->port &= (1 << USB_PORT_FEAT_POWER);
2108 case USB_PORT_FEAT_SUSPEND:
2110 case USB_PORT_FEAT_POWER:
2111 r8a66597_port_power(r8a66597, port, 0);
2113 case USB_PORT_FEAT_C_ENABLE:
2114 case USB_PORT_FEAT_C_SUSPEND:
2115 case USB_PORT_FEAT_C_CONNECTION:
2116 case USB_PORT_FEAT_C_OVER_CURRENT:
2117 case USB_PORT_FEAT_C_RESET:
2122 rh->port &= ~(1 << wValue);
2124 case GetHubDescriptor:
2125 r8a66597_hub_descriptor(r8a66597,
2126 (struct usb_hub_descriptor *)buf);
2132 if (wIndex > R8A66597_MAX_ROOT_HUB)
2134 *(u32 *)buf = cpu_to_le32(rh->port);
2136 case SetPortFeature:
2137 if (wIndex > R8A66597_MAX_ROOT_HUB)
2143 case USB_PORT_FEAT_SUSPEND:
2145 case USB_PORT_FEAT_POWER:
2146 r8a66597_port_power(r8a66597, port, 1);
2147 rh->port |= (1 << USB_PORT_FEAT_POWER);
2149 case USB_PORT_FEAT_RESET: {
2150 struct r8a66597_device *dev = rh->dev;
2152 rh->port |= (1 << USB_PORT_FEAT_RESET);
2154 disable_r8a66597_pipe_all(r8a66597, dev);
2155 free_usb_address(r8a66597, dev);
2157 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2158 get_dvstctr_reg(port));
2159 mod_timer(&r8a66597->rh_timer,
2160 jiffies + msecs_to_jiffies(50));
2166 rh->port |= 1 << wValue;
2174 spin_unlock_irqrestore(&r8a66597->lock, flags);
2178 static struct hc_driver r8a66597_hc_driver = {
2179 .description = hcd_name,
2180 .hcd_priv_size = sizeof(struct r8a66597),
2181 .irq = r8a66597_irq,
2184 * generic hardware linkage
2188 .start = r8a66597_start,
2189 .stop = r8a66597_stop,
2192 * managing i/o requests and associated device resources
2194 .urb_enqueue = r8a66597_urb_enqueue,
2195 .urb_dequeue = r8a66597_urb_dequeue,
2196 .endpoint_disable = r8a66597_endpoint_disable,
2199 * periodic schedule support
2201 .get_frame_number = r8a66597_get_frame,
2206 .hub_status_data = r8a66597_hub_status_data,
2207 .hub_control = r8a66597_hub_control,
2210 #if defined(CONFIG_PM)
2211 static int r8a66597_suspend(struct platform_device *pdev, pm_message_t state)
2216 static int r8a66597_resume(struct platform_device *pdev)
2220 #else /* if defined(CONFIG_PM) */
2221 #define r8a66597_suspend NULL
2222 #define r8a66597_resume NULL
2225 static int __init_or_module r8a66597_remove(struct platform_device *pdev)
2227 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2228 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2230 del_timer_sync(&r8a66597->rh_timer);
2231 usb_remove_hcd(hcd);
2232 iounmap((void *)r8a66597->reg);
2237 #define resource_len(r) (((r)->end - (r)->start) + 1)
2238 static int __init r8a66597_probe(struct platform_device *pdev)
2240 struct resource *res = NULL;
2242 void __iomem *reg = NULL;
2243 struct usb_hcd *hcd = NULL;
2244 struct r8a66597 *r8a66597;
2248 if (pdev->dev.dma_mask) {
2250 err("dma not support");
2254 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2258 err("platform_get_resource_byname error.");
2262 irq = platform_get_irq(pdev, 0);
2265 err("platform_get_irq error.");
2269 reg = ioremap(res->start, resource_len(res));
2272 err("ioremap error.");
2276 /* initialize hcd */
2277 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2280 err("Failed to create hcd");
2283 r8a66597 = hcd_to_r8a66597(hcd);
2284 memset(r8a66597, 0, sizeof(struct r8a66597));
2285 dev_set_drvdata(&pdev->dev, r8a66597);
2287 spin_lock_init(&r8a66597->lock);
2288 init_timer(&r8a66597->rh_timer);
2289 r8a66597->rh_timer.function = r8a66597_timer;
2290 r8a66597->rh_timer.data = (unsigned long)r8a66597;
2291 r8a66597->reg = (unsigned long)reg;
2293 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2294 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2295 init_timer(&r8a66597->td_timer[i]);
2296 r8a66597->td_timer[i].function = r8a66597_td_timer;
2297 r8a66597->td_timer[i].data = (unsigned long)r8a66597;
2298 setup_timer(&r8a66597->interval_timer[i],
2299 r8a66597_interval_timer,
2300 (unsigned long)r8a66597);
2302 INIT_LIST_HEAD(&r8a66597->child_device);
2304 hcd->rsrc_start = res->start;
2305 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED);
2307 err("Failed to add hcd");
2320 static struct platform_driver r8a66597_driver = {
2321 .probe = r8a66597_probe,
2322 .remove = r8a66597_remove,
2323 .suspend = r8a66597_suspend,
2324 .resume = r8a66597_resume,
2326 .name = (char *) hcd_name,
2327 .owner = THIS_MODULE,
2331 static int __init r8a66597_init(void)
2336 info("driver %s, %s", hcd_name, DRIVER_VERSION);
2337 return platform_driver_register(&r8a66597_driver);
2339 module_init(r8a66597_init);
2341 static void __exit r8a66597_cleanup(void)
2343 platform_driver_unregister(&r8a66597_driver);
2345 module_exit(r8a66597_cleanup);