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 static unsigned short clock = XTAL12;
55 module_param(clock, ushort, 0644);
56 MODULE_PARM_DESC(clock, "input clock: 48MHz=32768, 24MHz=16384, 12MHz=0 "
59 static unsigned short vif = LDRV;
60 module_param(vif, ushort, 0644);
61 MODULE_PARM_DESC(vif, "input VIF: 3.3V=32768, 1.5V=0(default=32768)");
63 static unsigned short endian;
64 module_param(endian, ushort, 0644);
65 MODULE_PARM_DESC(endian, "data endian: big=256, little=0 (default=0)");
67 static unsigned short irq_sense = INTL;
68 module_param(irq_sense, ushort, 0644);
69 MODULE_PARM_DESC(irq_sense, "IRQ sense: low level=32, falling edge=0 "
72 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
73 static int r8a66597_get_frame(struct usb_hcd *hcd);
75 /* this function must be called with interrupt disabled */
76 static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
81 tmp = r8a66597_read(r8a66597, INTENB0);
82 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
83 r8a66597_bset(r8a66597, 1 << pipenum, reg);
84 r8a66597_write(r8a66597, tmp, INTENB0);
87 /* this function must be called with interrupt disabled */
88 static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
93 tmp = r8a66597_read(r8a66597, INTENB0);
94 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
95 r8a66597_bclr(r8a66597, 1 << pipenum, reg);
96 r8a66597_write(r8a66597, tmp, INTENB0);
99 static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
100 u16 usbspd, u8 upphub, u8 hubport, int port)
103 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
105 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
106 r8a66597_write(r8a66597, val, devadd_reg);
109 static int enable_controller(struct r8a66597 *r8a66597)
115 r8a66597_write(r8a66597, USBE, SYSCFG0);
116 tmp = r8a66597_read(r8a66597, SYSCFG0);
118 err("register access fail.");
121 } while ((tmp & USBE) != USBE);
122 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
123 r8a66597_mdfy(r8a66597, clock, XTAL, SYSCFG0);
126 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
129 tmp = r8a66597_read(r8a66597, SYSCFG0);
131 err("register access fail.");
134 } while ((tmp & SCKE) != SCKE);
136 r8a66597_bset(r8a66597, DCFM | DRPD, SYSCFG0);
137 r8a66597_bset(r8a66597, DRPD, SYSCFG1);
139 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
140 r8a66597_bset(r8a66597, HSE, SYSCFG0);
141 r8a66597_bset(r8a66597, HSE, SYSCFG1);
142 r8a66597_bset(r8a66597, USBE, SYSCFG0);
144 r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
145 r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
146 r8a66597_bset(r8a66597, BRDY0, BRDYENB);
147 r8a66597_bset(r8a66597, BEMP0, BEMPENB);
149 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, DMA0CFG);
150 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, DMA1CFG);
152 r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
153 r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
154 r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
156 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
158 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
159 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
160 r8a66597_bset(r8a66597, ATTCHE, INTENB1);
161 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
162 r8a66597_bset(r8a66597, ATTCHE, INTENB2);
167 static void disable_controller(struct r8a66597 *r8a66597)
171 r8a66597_write(r8a66597, 0, INTENB0);
172 r8a66597_write(r8a66597, 0, INTENB1);
173 r8a66597_write(r8a66597, 0, INTENB2);
174 r8a66597_write(r8a66597, 0, INTSTS0);
175 r8a66597_write(r8a66597, 0, INTSTS1);
176 r8a66597_write(r8a66597, 0, INTSTS2);
178 r8a66597_port_power(r8a66597, 0, 0);
179 r8a66597_port_power(r8a66597, 1, 0);
182 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
184 } while (tmp == EDGESTS);
186 r8a66597_bclr(r8a66597, DCFM | DRPD, SYSCFG0);
187 r8a66597_bclr(r8a66597, DRPD, SYSCFG1);
188 r8a66597_bclr(r8a66597, HSE, SYSCFG0);
189 r8a66597_bclr(r8a66597, HSE, SYSCFG1);
191 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
193 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
194 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
195 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
198 static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
199 struct usb_device *udev)
201 struct r8a66597_device *dev;
203 if (udev->parent && udev->parent->devnum != 1)
206 dev = dev_get_drvdata(&udev->dev);
213 static int is_child_device(char *devpath)
215 return (devpath[2] ? 1 : 0);
218 static int is_hub_limit(char *devpath)
220 return ((strlen(devpath) >= 4) ? 1 : 0);
223 static void get_port_number(char *devpath, u16 *root_port, u16 *hub_port)
226 *root_port = (devpath[0] & 0x0F) - 1;
227 if (*root_port >= R8A66597_MAX_ROOT_HUB)
228 err("illegal root port number");
231 *hub_port = devpath[2] & 0x0F;
234 static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
249 err("unknown speed");
256 static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
261 r8a66597->child_connect_map[idx] |= 1 << (address % 32);
264 static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
269 r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
272 static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
274 u16 pipenum = pipe->info.pipenum;
275 unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
276 unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
277 unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
279 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
280 dma_ch = R8A66597_PIPE_NO_DMA;
282 pipe->fifoaddr = fifoaddr[dma_ch];
283 pipe->fifosel = fifosel[dma_ch];
284 pipe->fifoctr = fifoctr[dma_ch];
287 pipe->pipectr = DCPCTR;
289 pipe->pipectr = get_pipectr_addr(pipenum);
291 if (check_bulk_or_isoc(pipenum)) {
292 pipe->pipetre = get_pipetre_addr(pipenum);
293 pipe->pipetrn = get_pipetrn_addr(pipenum);
300 static struct r8a66597_device *
301 get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
303 if (usb_pipedevice(urb->pipe) == 0)
304 return &r8a66597->device0;
306 return dev_get_drvdata(&urb->dev->dev);
309 static int make_r8a66597_device(struct r8a66597 *r8a66597,
310 struct urb *urb, u8 addr)
312 struct r8a66597_device *dev;
313 int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
315 dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
319 dev_set_drvdata(&urb->dev->dev, dev);
320 dev->udev = urb->dev;
322 dev->usb_address = usb_address;
323 dev->state = USB_STATE_ADDRESS;
324 dev->ep_in_toggle = 0;
325 dev->ep_out_toggle = 0;
326 INIT_LIST_HEAD(&dev->device_list);
327 list_add_tail(&dev->device_list, &r8a66597->child_device);
329 get_port_number(urb->dev->devpath, &dev->root_port, &dev->hub_port);
330 if (!is_child_device(urb->dev->devpath))
331 r8a66597->root_hub[dev->root_port].dev = dev;
333 set_devadd_reg(r8a66597, dev->address,
334 get_r8a66597_usb_speed(urb->dev->speed),
335 get_parent_r8a66597_address(r8a66597, urb->dev),
336 dev->hub_port, dev->root_port);
341 /* this function must be called with interrupt disabled */
342 static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
344 u8 addr; /* R8A66597's address */
345 struct r8a66597_device *dev;
347 if (is_hub_limit(urb->dev->devpath)) {
348 err("Externel hub limit reached.");
352 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
353 if (dev && dev->state >= USB_STATE_ADDRESS)
356 for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
357 if (r8a66597->address_map & (1 << addr))
360 dbg("alloc_address: r8a66597_addr=%d", addr);
361 r8a66597->address_map |= 1 << addr;
363 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
369 err("cannot communicate with a USB device more than 10.(%x)",
370 r8a66597->address_map);
375 /* this function must be called with interrupt disabled */
376 static void free_usb_address(struct r8a66597 *r8a66597,
377 struct r8a66597_device *dev)
384 dbg("free_addr: addr=%d", dev->address);
386 dev->state = USB_STATE_DEFAULT;
387 r8a66597->address_map &= ~(1 << dev->address);
389 dev_set_drvdata(&dev->udev->dev, NULL);
390 list_del(&dev->device_list);
393 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
394 if (r8a66597->root_hub[port].dev == dev) {
395 r8a66597->root_hub[port].dev = NULL;
401 static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
408 tmp = r8a66597_read(r8a66597, reg);
410 err("register%lx, loop %x is timeout", reg, loop);
414 } while ((tmp & mask) != loop);
417 /* this function must be called with interrupt disabled */
418 static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
422 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
423 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
424 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
425 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
428 /* this function must be called with interrupt disabled */
429 static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
433 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
434 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
435 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
436 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
437 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
440 /* this function must be called with interrupt disabled */
441 static void clear_all_buffer(struct r8a66597 *r8a66597,
442 struct r8a66597_pipe *pipe)
446 if (!pipe || pipe->info.pipenum == 0)
449 pipe_stop(r8a66597, pipe);
450 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
451 tmp = r8a66597_read(r8a66597, pipe->pipectr);
452 tmp = r8a66597_read(r8a66597, pipe->pipectr);
453 tmp = r8a66597_read(r8a66597, pipe->pipectr);
454 r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
457 /* this function must be called with interrupt disabled */
458 static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
459 struct r8a66597_pipe *pipe, int toggle)
462 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
464 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
467 /* this function must be called with interrupt disabled */
468 static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
470 r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
471 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
474 /* this function must be called with interrupt disabled */
475 static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
476 struct r8a66597_pipe *pipe)
478 cfifo_change(r8a66597, 0);
479 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D0FIFOSEL);
480 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D1FIFOSEL);
482 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum, MBW | CURPIPE,
484 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
487 static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
489 struct r8a66597_pipe *pipe = hep->hcpriv;
491 if (usb_pipeendpoint(urb->pipe) == 0)
494 return pipe->info.pipenum;
497 static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
499 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
501 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
504 static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
510 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
513 /* this function must be called with interrupt disabled */
514 static void pipe_toggle_set(struct r8a66597 *r8a66597,
515 struct r8a66597_pipe *pipe,
516 struct urb *urb, int set)
518 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
519 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
520 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
526 *toggle |= 1 << endpoint;
528 *toggle &= ~(1 << endpoint);
531 /* this function must be called with interrupt disabled */
532 static void pipe_toggle_save(struct r8a66597 *r8a66597,
533 struct r8a66597_pipe *pipe,
536 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
537 pipe_toggle_set(r8a66597, pipe, urb, 1);
539 pipe_toggle_set(r8a66597, pipe, urb, 0);
542 /* this function must be called with interrupt disabled */
543 static void pipe_toggle_restore(struct r8a66597 *r8a66597,
544 struct r8a66597_pipe *pipe,
547 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
548 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
549 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
554 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
557 /* this function must be called with interrupt disabled */
558 static void pipe_buffer_setting(struct r8a66597 *r8a66597,
559 struct r8a66597_pipe_info *info)
563 if (info->pipenum == 0)
566 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
567 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
568 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
571 if (info->type == R8A66597_BULK && info->dir_in)
572 val |= R8A66597_DBLB | R8A66597_SHTNAK;
573 val |= info->type | info->epnum;
574 r8a66597_write(r8a66597, val, PIPECFG);
576 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
578 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
580 r8a66597_write(r8a66597, info->interval, PIPEPERI);
583 /* this function must be called with interrupt disabled */
584 static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
586 struct r8a66597_pipe_info *info;
587 struct urb *urb = td->urb;
589 if (td->pipenum > 0) {
590 info = &td->pipe->info;
591 cfifo_change(r8a66597, 0);
592 pipe_buffer_setting(r8a66597, info);
594 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
595 usb_pipeout(urb->pipe)) &&
596 !usb_pipecontrol(urb->pipe)) {
597 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
598 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
599 clear_all_buffer(r8a66597, td->pipe);
600 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
601 usb_pipeout(urb->pipe), 1);
603 pipe_toggle_restore(r8a66597, td->pipe, urb);
607 /* this function must be called with interrupt disabled */
608 static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
609 struct usb_endpoint_descriptor *ep)
611 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
613 memset(array, 0, sizeof(array));
614 switch (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
615 case USB_ENDPOINT_XFER_BULK:
616 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
623 case USB_ENDPOINT_XFER_INT:
624 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
631 case USB_ENDPOINT_XFER_ISOC:
632 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
644 while (array[i] != 0) {
645 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
653 static u16 get_r8a66597_type(__u8 type)
658 case USB_ENDPOINT_XFER_BULK:
659 r8a66597_type = R8A66597_BULK;
661 case USB_ENDPOINT_XFER_INT:
662 r8a66597_type = R8A66597_INT;
664 case USB_ENDPOINT_XFER_ISOC:
665 r8a66597_type = R8A66597_ISO;
669 r8a66597_type = 0x0000;
673 return r8a66597_type;
676 static u16 get_bufnum(u16 pipenum)
682 else if (check_bulk_or_isoc(pipenum))
683 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
684 else if (check_interrupt(pipenum))
685 bufnum = 4 + (pipenum - 6);
687 err("Illegal pipenum (%d)", pipenum);
692 static u16 get_buf_bsize(u16 pipenum)
698 else if (check_bulk_or_isoc(pipenum))
699 buf_bsize = R8A66597_BUF_BSIZE - 1;
700 else if (check_interrupt(pipenum))
703 err("Illegal pipenum (%d)", pipenum);
708 /* this function must be called with interrupt disabled */
709 static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
710 struct r8a66597_device *dev,
711 struct r8a66597_pipe *pipe,
715 struct r8a66597_pipe_info *info = &pipe->info;
717 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
718 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
719 if ((r8a66597->dma_map & (1 << i)) != 0)
722 info("address %d, EndpointAddress 0x%02x use DMA FIFO",
723 usb_pipedevice(urb->pipe),
724 info->dir_in ? USB_ENDPOINT_DIR_MASK + info->epnum
727 r8a66597->dma_map |= 1 << i;
728 dev->dma_map |= 1 << i;
729 set_pipe_reg_addr(pipe, i);
731 cfifo_change(r8a66597, 0);
732 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum,
733 MBW | CURPIPE, pipe->fifosel);
735 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
737 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
743 /* this function must be called with interrupt disabled */
744 static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
745 struct usb_host_endpoint *hep,
746 struct r8a66597_pipe_info *info)
748 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
749 struct r8a66597_pipe *pipe = hep->hcpriv;
754 set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
755 r8a66597->pipe_cnt[pipe->info.pipenum]++;
756 dev->pipe_cnt[pipe->info.pipenum]++;
758 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
761 /* this function must be called with interrupt disabled */
762 static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
764 struct r8a66597_td *td, *next;
766 struct list_head *list = &r8a66597->pipe_queue[pipenum];
768 if (list_empty(list))
771 list_for_each_entry_safe(td, next, list, queue) {
774 if (td->address != address)
778 list_del(&td->queue);
782 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597),
785 spin_unlock(&r8a66597->lock);
786 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb,
788 spin_lock(&r8a66597->lock);
794 /* this function must be called with interrupt disabled */
795 static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
796 struct r8a66597_device *dev)
804 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
805 if (!dev->pipe_cnt[pipenum])
810 force_dequeue(r8a66597, 0, dev->address);
813 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
814 dev->pipe_cnt[pipenum] = 0;
815 force_dequeue(r8a66597, pipenum, dev->address);
820 r8a66597->dma_map &= ~(dev->dma_map);
824 static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
827 unsigned long time = 1;
829 if (usb_pipeisoc(urb->pipe))
832 if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
833 for (i = 0; i < (interval - 1); i++)
835 time = time * 125 / 1000; /* uSOF -> msec */
843 /* this function must be called with interrupt disabled */
844 static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
845 struct usb_host_endpoint *hep,
846 struct usb_endpoint_descriptor *ep)
848 struct r8a66597_pipe_info info;
850 info.pipenum = get_empty_pipenum(r8a66597, ep);
851 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
852 info.epnum = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
853 info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
854 info.type = get_r8a66597_type(ep->bmAttributes
855 & USB_ENDPOINT_XFERTYPE_MASK);
856 info.bufnum = get_bufnum(info.pipenum);
857 info.buf_bsize = get_buf_bsize(info.pipenum);
858 if (info.type == R8A66597_BULK) {
860 info.timer_interval = 0;
862 if (ep->bInterval > IITV)
863 info.interval = IITV;
865 info.interval = ep->bInterval ? ep->bInterval - 1 : 0;
866 info.timer_interval = get_timer_interval(urb, ep->bInterval);
868 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
873 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
876 static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
878 struct r8a66597_device *dev;
880 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
881 dev->state = USB_STATE_CONFIGURED;
884 static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
887 if (pipenum == 0 && usb_pipeout(urb->pipe))
888 enable_irq_empty(r8a66597, pipenum);
890 enable_irq_ready(r8a66597, pipenum);
892 if (!usb_pipeisoc(urb->pipe))
893 enable_irq_nrdy(r8a66597, pipenum);
896 static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
898 disable_irq_ready(r8a66597, pipenum);
899 disable_irq_nrdy(r8a66597, pipenum);
902 /* this function must be called with interrupt disabled */
903 static void r8a66597_usb_preconnect(struct r8a66597 *r8a66597, int port)
905 r8a66597->root_hub[port].port |= (1 << USB_PORT_FEAT_CONNECTION)
906 | (1 << USB_PORT_FEAT_C_CONNECTION);
907 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
908 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
911 /* this function must be called with interrupt disabled */
912 static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
914 u16 speed = get_rh_usb_speed(r8a66597, port);
915 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
918 rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
919 else if (speed == LSMODE)
920 rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
922 rh->port &= ~(1 << USB_PORT_FEAT_RESET);
923 rh->port |= 1 << USB_PORT_FEAT_ENABLE;
926 /* this function must be called with interrupt disabled */
927 static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
929 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
931 r8a66597->root_hub[port].port &= ~(1 << USB_PORT_FEAT_CONNECTION);
932 r8a66597->root_hub[port].port |= (1 << USB_PORT_FEAT_C_CONNECTION);
934 disable_r8a66597_pipe_all(r8a66597, dev);
935 free_usb_address(r8a66597, dev);
937 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
940 /* this function must be called with interrupt disabled */
941 static void prepare_setup_packet(struct r8a66597 *r8a66597,
942 struct r8a66597_td *td)
945 u16 *p = (u16 *)td->urb->setup_packet;
946 unsigned long setup_addr = USBREQ;
948 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
950 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
952 for (i = 0; i < 4; i++) {
953 r8a66597_write(r8a66597, cpu_to_le16(p[i]), setup_addr);
956 r8a66597_write(r8a66597, SUREQ, DCPCTR);
959 /* this function must be called with interrupt disabled */
960 static void prepare_packet_read(struct r8a66597 *r8a66597,
961 struct r8a66597_td *td)
963 struct urb *urb = td->urb;
965 if (usb_pipecontrol(urb->pipe)) {
966 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
967 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
968 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
969 if (urb->actual_length == 0) {
970 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
971 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
973 pipe_irq_disable(r8a66597, td->pipenum);
974 pipe_start(r8a66597, td->pipe);
975 pipe_irq_enable(r8a66597, urb, td->pipenum);
977 if (urb->actual_length == 0) {
978 pipe_irq_disable(r8a66597, td->pipenum);
979 pipe_setting(r8a66597, td);
980 pipe_stop(r8a66597, td->pipe);
981 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
983 if (td->pipe->pipetre) {
984 r8a66597_write(r8a66597, TRCLR,
986 r8a66597_write(r8a66597,
988 (urb->transfer_buffer_length,
991 r8a66597_bset(r8a66597, TRENB,
995 pipe_start(r8a66597, td->pipe);
996 pipe_irq_enable(r8a66597, urb, td->pipenum);
1001 /* this function must be called with interrupt disabled */
1002 static void prepare_packet_write(struct r8a66597 *r8a66597,
1003 struct r8a66597_td *td)
1006 struct urb *urb = td->urb;
1008 if (usb_pipecontrol(urb->pipe)) {
1009 pipe_stop(r8a66597, td->pipe);
1010 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1011 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1012 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1013 if (urb->actual_length == 0) {
1014 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1015 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1018 if (urb->actual_length == 0)
1019 pipe_setting(r8a66597, td);
1020 if (td->pipe->pipetre)
1021 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1023 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1025 fifo_change_from_pipe(r8a66597, td->pipe);
1026 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1027 if (unlikely((tmp & FRDY) == 0))
1028 pipe_irq_enable(r8a66597, urb, td->pipenum);
1030 packet_write(r8a66597, td->pipenum);
1031 pipe_start(r8a66597, td->pipe);
1034 /* this function must be called with interrupt disabled */
1035 static void prepare_status_packet(struct r8a66597 *r8a66597,
1036 struct r8a66597_td *td)
1038 struct urb *urb = td->urb;
1040 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1041 pipe_stop(r8a66597, td->pipe);
1043 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1044 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1045 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1046 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1047 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
1048 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1049 r8a66597_write(r8a66597, BVAL, CFIFOCTR);
1050 enable_irq_empty(r8a66597, 0);
1052 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1053 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1054 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1055 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1056 enable_irq_ready(r8a66597, 0);
1058 enable_irq_nrdy(r8a66597, 0);
1059 pipe_start(r8a66597, td->pipe);
1062 static int is_set_address(unsigned char *setup_packet)
1064 if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1065 setup_packet[1] == USB_REQ_SET_ADDRESS)
1071 /* this function must be called with interrupt disabled */
1072 static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1078 if (is_set_address(td->urb->setup_packet)) {
1079 td->set_address = 1;
1080 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1082 if (td->urb->setup_packet[2] == 0)
1085 prepare_setup_packet(r8a66597, td);
1088 prepare_packet_read(r8a66597, td);
1091 prepare_packet_write(r8a66597, td);
1094 prepare_status_packet(r8a66597, td);
1097 err("invalid type.");
1104 static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1106 if (usb_pipeisoc(urb->pipe)) {
1107 if (urb->number_of_packets == td->iso_cnt)
1111 /* control or bulk or interrupt */
1112 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1113 (td->short_packet) || (td->zero_packet))
1119 /* this function must be called with interrupt disabled */
1120 static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1126 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1127 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1128 r8a66597->timeout_map |= 1 << td->pipenum;
1129 switch (usb_pipetype(td->urb->pipe)) {
1130 case PIPE_INTERRUPT:
1131 case PIPE_ISOCHRONOUS:
1139 mod_timer(&r8a66597->td_timer[td->pipenum],
1140 jiffies + msecs_to_jiffies(time));
1144 /* this function must be called with interrupt disabled */
1145 static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
1146 u16 pipenum, struct urb *urb, int status)
1147 __releases(r8a66597->lock) __acquires(r8a66597->lock)
1150 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1152 r8a66597->timeout_map &= ~(1 << pipenum);
1155 if (td->set_address && (status != 0 || urb->unlinked))
1156 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1158 pipe_toggle_save(r8a66597, td->pipe, urb);
1159 list_del(&td->queue);
1163 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1167 if (usb_pipeisoc(urb->pipe))
1168 urb->start_frame = r8a66597_get_frame(hcd);
1170 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
1171 spin_unlock(&r8a66597->lock);
1172 usb_hcd_giveback_urb(hcd, urb, status);
1173 spin_lock(&r8a66597->lock);
1177 td = r8a66597_get_td(r8a66597, pipenum);
1181 start_transfer(r8a66597, td);
1182 set_td_timer(r8a66597, td);
1186 static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1189 int rcv_len, bufsize, urb_len, size;
1191 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1200 fifo_change_from_pipe(r8a66597, td->pipe);
1201 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1202 if (unlikely((tmp & FRDY) == 0)) {
1203 pipe_stop(r8a66597, td->pipe);
1204 pipe_irq_disable(r8a66597, pipenum);
1205 err("in fifo not ready (%d)", pipenum);
1206 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
1210 /* prepare parameters */
1211 rcv_len = tmp & DTLN;
1212 if (usb_pipeisoc(urb->pipe)) {
1213 buf = (u16 *)(urb->transfer_buffer +
1214 urb->iso_frame_desc[td->iso_cnt].offset);
1215 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1217 buf = (void *)urb->transfer_buffer + urb->actual_length;
1218 urb_len = urb->transfer_buffer_length - urb->actual_length;
1220 bufsize = min(urb_len, (int) td->maxpacket);
1221 if (rcv_len <= bufsize) {
1225 status = -EOVERFLOW;
1229 /* update parameters */
1230 urb->actual_length += size;
1232 td->zero_packet = 1;
1233 if (rcv_len < bufsize) {
1234 td->short_packet = 1;
1236 if (usb_pipeisoc(urb->pipe)) {
1237 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1238 urb->iso_frame_desc[td->iso_cnt].status = status;
1243 /* check transfer finish */
1244 if (finish || check_transfer_finish(td, urb)) {
1245 pipe_stop(r8a66597, td->pipe);
1246 pipe_irq_disable(r8a66597, pipenum);
1251 if (urb->transfer_buffer) {
1253 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1255 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1259 if (finish && pipenum != 0)
1260 finish_request(r8a66597, td, pipenum, urb, status);
1263 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1268 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1275 fifo_change_from_pipe(r8a66597, td->pipe);
1276 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1277 if (unlikely((tmp & FRDY) == 0)) {
1278 pipe_stop(r8a66597, td->pipe);
1279 pipe_irq_disable(r8a66597, pipenum);
1280 err("out write fifo not ready. (%d)", pipenum);
1281 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
1285 /* prepare parameters */
1286 bufsize = td->maxpacket;
1287 if (usb_pipeisoc(urb->pipe)) {
1288 buf = (u16 *)(urb->transfer_buffer +
1289 urb->iso_frame_desc[td->iso_cnt].offset);
1291 (int)urb->iso_frame_desc[td->iso_cnt].length);
1293 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
1294 size = min((int)bufsize,
1295 urb->transfer_buffer_length - urb->actual_length);
1300 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
1301 if (urb->transfer_buffer) {
1302 r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
1303 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1304 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1307 /* update parameters */
1308 urb->actual_length += size;
1309 if (usb_pipeisoc(urb->pipe)) {
1310 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1311 urb->iso_frame_desc[td->iso_cnt].status = 0;
1315 /* check transfer finish */
1316 if (check_transfer_finish(td, urb)) {
1317 disable_irq_ready(r8a66597, pipenum);
1318 enable_irq_empty(r8a66597, pipenum);
1319 if (!usb_pipeisoc(urb->pipe))
1320 enable_irq_nrdy(r8a66597, pipenum);
1322 pipe_irq_enable(r8a66597, urb, pipenum);
1326 static void check_next_phase(struct r8a66597 *r8a66597, int status)
1328 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1339 if (check_transfer_finish(td, urb))
1340 td->type = USB_PID_ACK;
1343 if (urb->transfer_buffer_length == urb->actual_length)
1344 td->type = USB_PID_ACK;
1345 else if (usb_pipeout(urb->pipe))
1346 td->type = USB_PID_OUT;
1348 td->type = USB_PID_IN;
1355 if (finish || status != 0 || urb->unlinked)
1356 finish_request(r8a66597, td, 0, urb, status);
1358 start_transfer(r8a66597, td);
1361 static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
1363 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1366 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1376 static void irq_pipe_ready(struct r8a66597 *r8a66597)
1381 struct r8a66597_td *td;
1383 mask = r8a66597_read(r8a66597, BRDYSTS)
1384 & r8a66597_read(r8a66597, BRDYENB);
1385 r8a66597_write(r8a66597, ~mask, BRDYSTS);
1387 td = r8a66597_get_td(r8a66597, 0);
1388 if (td && td->type == USB_PID_IN)
1389 packet_read(r8a66597, 0);
1391 pipe_irq_disable(r8a66597, 0);
1392 check_next_phase(r8a66597, 0);
1395 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1396 check = 1 << pipenum;
1398 td = r8a66597_get_td(r8a66597, pipenum);
1402 if (td->type == USB_PID_IN)
1403 packet_read(r8a66597, pipenum);
1404 else if (td->type == USB_PID_OUT)
1405 packet_write(r8a66597, pipenum);
1410 static void irq_pipe_empty(struct r8a66597 *r8a66597)
1416 struct r8a66597_td *td;
1418 mask = r8a66597_read(r8a66597, BEMPSTS)
1419 & r8a66597_read(r8a66597, BEMPENB);
1420 r8a66597_write(r8a66597, ~mask, BEMPSTS);
1422 cfifo_change(r8a66597, 0);
1423 td = r8a66597_get_td(r8a66597, 0);
1424 if (td && td->type != USB_PID_OUT)
1425 disable_irq_empty(r8a66597, 0);
1426 check_next_phase(r8a66597, 0);
1429 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1430 check = 1 << pipenum;
1432 struct r8a66597_td *td;
1433 td = r8a66597_get_td(r8a66597, pipenum);
1437 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1438 if ((tmp & INBUFM) == 0) {
1439 disable_irq_empty(r8a66597, pipenum);
1440 pipe_irq_disable(r8a66597, pipenum);
1441 finish_request(r8a66597, td, pipenum, td->urb,
1448 static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1455 mask = r8a66597_read(r8a66597, NRDYSTS)
1456 & r8a66597_read(r8a66597, NRDYENB);
1457 r8a66597_write(r8a66597, ~mask, NRDYSTS);
1459 cfifo_change(r8a66597, 0);
1460 status = get_urb_error(r8a66597, 0);
1461 pipe_irq_disable(r8a66597, 0);
1462 check_next_phase(r8a66597, status);
1465 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1466 check = 1 << pipenum;
1468 struct r8a66597_td *td;
1469 td = r8a66597_get_td(r8a66597, pipenum);
1473 status = get_urb_error(r8a66597, pipenum);
1474 pipe_irq_disable(r8a66597, pipenum);
1475 pipe_stop(r8a66597, td->pipe);
1476 finish_request(r8a66597, td, pipenum, td->urb, status);
1481 static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port)
1483 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1485 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1486 rh->scount = R8A66597_MAX_SAMPLING;
1487 mod_timer(&r8a66597->rh_timer, jiffies + msecs_to_jiffies(50));
1490 static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1492 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1493 u16 intsts0, intsts1, intsts2;
1494 u16 intenb0, intenb1, intenb2;
1495 u16 mask0, mask1, mask2;
1498 spin_lock(&r8a66597->lock);
1500 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1501 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1502 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1503 intenb0 = r8a66597_read(r8a66597, INTENB0);
1504 intenb1 = r8a66597_read(r8a66597, INTENB1);
1505 intenb2 = r8a66597_read(r8a66597, INTENB2);
1507 mask2 = intsts2 & intenb2;
1508 mask1 = intsts1 & intenb1;
1509 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1511 if (mask2 & ATTCH) {
1512 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
1513 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1515 /* start usb bus sampling */
1516 start_root_hub_sampling(r8a66597, 1);
1519 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
1520 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1521 r8a66597_usb_disconnect(r8a66597, 1);
1526 if (mask1 & ATTCH) {
1527 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
1528 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1530 /* start usb bus sampling */
1531 start_root_hub_sampling(r8a66597, 0);
1534 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
1535 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1536 r8a66597_usb_disconnect(r8a66597, 0);
1539 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
1540 status = get_urb_error(r8a66597, 0);
1541 check_next_phase(r8a66597, status);
1544 r8a66597_write(r8a66597, ~SACK, INTSTS1);
1545 check_next_phase(r8a66597, 0);
1550 irq_pipe_ready(r8a66597);
1552 irq_pipe_empty(r8a66597);
1554 irq_pipe_nrdy(r8a66597);
1557 spin_unlock(&r8a66597->lock);
1561 /* this function must be called with interrupt disabled */
1562 static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1565 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1567 if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
1568 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1570 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1571 if ((tmp & USBRST) == USBRST) {
1572 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1574 mod_timer(&r8a66597->rh_timer,
1575 jiffies + msecs_to_jiffies(50));
1577 r8a66597_usb_connect(r8a66597, port);
1580 if (rh->scount > 0) {
1581 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1582 if (tmp == rh->old_syssts) {
1584 if (rh->scount == 0) {
1585 if (tmp == FS_JSTS) {
1586 r8a66597_bset(r8a66597, HSE,
1587 get_syscfg_reg(port));
1588 r8a66597_usb_preconnect(r8a66597, port);
1589 } else if (tmp == LS_JSTS) {
1590 r8a66597_bclr(r8a66597, HSE,
1591 get_syscfg_reg(port));
1592 r8a66597_usb_preconnect(r8a66597, port);
1593 } else if (tmp == SE0)
1594 r8a66597_bset(r8a66597, ATTCHE,
1595 get_intenb_reg(port));
1597 mod_timer(&r8a66597->rh_timer,
1598 jiffies + msecs_to_jiffies(50));
1601 rh->scount = R8A66597_MAX_SAMPLING;
1602 rh->old_syssts = tmp;
1603 mod_timer(&r8a66597->rh_timer,
1604 jiffies + msecs_to_jiffies(50));
1609 static void r8a66597_interval_timer(unsigned long _r8a66597)
1611 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1612 unsigned long flags;
1614 struct r8a66597_td *td;
1616 spin_lock_irqsave(&r8a66597->lock, flags);
1618 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1619 if (!(r8a66597->interval_map & (1 << pipenum)))
1621 if (timer_pending(&r8a66597->interval_timer[pipenum]))
1624 td = r8a66597_get_td(r8a66597, pipenum);
1626 start_transfer(r8a66597, td);
1629 spin_unlock_irqrestore(&r8a66597->lock, flags);
1632 static void r8a66597_td_timer(unsigned long _r8a66597)
1634 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1635 unsigned long flags;
1637 struct r8a66597_td *td, *new_td = NULL;
1638 struct r8a66597_pipe *pipe;
1640 spin_lock_irqsave(&r8a66597->lock, flags);
1641 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1642 if (!(r8a66597->timeout_map & (1 << pipenum)))
1644 if (timer_pending(&r8a66597->td_timer[pipenum]))
1647 td = r8a66597_get_td(r8a66597, pipenum);
1649 r8a66597->timeout_map &= ~(1 << pipenum);
1653 if (td->urb->actual_length) {
1654 set_td_timer(r8a66597, td);
1659 pipe_stop(r8a66597, pipe);
1663 list_move_tail(&new_td->queue,
1664 &r8a66597->pipe_queue[pipenum]);
1665 new_td = r8a66597_get_td(r8a66597, pipenum);
1670 } while (td != new_td && td->address == new_td->address);
1672 start_transfer(r8a66597, new_td);
1675 r8a66597->timeout_map &= ~(1 << pipenum);
1677 set_td_timer(r8a66597, new_td);
1680 spin_unlock_irqrestore(&r8a66597->lock, flags);
1683 static void r8a66597_timer(unsigned long _r8a66597)
1685 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1686 unsigned long flags;
1688 spin_lock_irqsave(&r8a66597->lock, flags);
1690 r8a66597_root_hub_control(r8a66597, 0);
1691 r8a66597_root_hub_control(r8a66597, 1);
1693 spin_unlock_irqrestore(&r8a66597->lock, flags);
1696 static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1698 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1700 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1701 (urb->dev->state == USB_STATE_CONFIGURED))
1707 static int r8a66597_start(struct usb_hcd *hcd)
1709 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1711 hcd->state = HC_STATE_RUNNING;
1712 return enable_controller(r8a66597);
1715 static void r8a66597_stop(struct usb_hcd *hcd)
1717 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1719 disable_controller(r8a66597);
1722 static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1724 unsigned int usb_address = usb_pipedevice(urb->pipe);
1725 u16 root_port, hub_port;
1727 if (usb_address == 0) {
1728 get_port_number(urb->dev->devpath,
1729 &root_port, &hub_port);
1730 set_devadd_reg(r8a66597, 0,
1731 get_r8a66597_usb_speed(urb->dev->speed),
1732 get_parent_r8a66597_address(r8a66597, urb->dev),
1733 hub_port, root_port);
1737 static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1739 struct usb_host_endpoint *hep)
1741 struct r8a66597_td *td;
1744 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
1748 pipenum = r8a66597_get_pipenum(urb, hep);
1749 td->pipenum = pipenum;
1750 td->pipe = hep->hcpriv;
1752 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1753 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1754 !usb_pipein(urb->pipe));
1755 if (usb_pipecontrol(urb->pipe))
1756 td->type = USB_PID_SETUP;
1757 else if (usb_pipein(urb->pipe))
1758 td->type = USB_PID_IN;
1760 td->type = USB_PID_OUT;
1761 INIT_LIST_HEAD(&td->queue);
1766 static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
1770 struct usb_host_endpoint *hep = urb->ep;
1771 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1772 struct r8a66597_td *td = NULL;
1773 int ret, request = 0;
1774 unsigned long flags;
1776 spin_lock_irqsave(&r8a66597->lock, flags);
1777 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1779 goto error_not_linked;
1782 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1784 goto error_not_linked;
1787 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1793 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1794 if (usb_pipeendpoint(urb->pipe))
1795 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1798 if (unlikely(check_pipe_config(r8a66597, urb)))
1799 init_pipe_config(r8a66597, urb);
1801 set_address_zero(r8a66597, urb);
1802 td = r8a66597_make_td(r8a66597, urb, hep);
1807 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1809 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
1813 if (td->pipe->info.timer_interval) {
1814 r8a66597->interval_map |= 1 << td->pipenum;
1815 mod_timer(&r8a66597->interval_timer[td->pipenum],
1816 jiffies + msecs_to_jiffies(
1817 td->pipe->info.timer_interval));
1819 ret = start_transfer(r8a66597, td);
1821 list_del(&td->queue);
1826 set_td_timer(r8a66597, td);
1830 usb_hcd_unlink_urb_from_ep(hcd, urb);
1832 spin_unlock_irqrestore(&r8a66597->lock, flags);
1836 static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1839 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1840 struct r8a66597_td *td;
1841 unsigned long flags;
1844 spin_lock_irqsave(&r8a66597->lock, flags);
1845 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1851 pipe_stop(r8a66597, td->pipe);
1852 pipe_irq_disable(r8a66597, td->pipenum);
1853 disable_irq_empty(r8a66597, td->pipenum);
1854 finish_request(r8a66597, td, td->pipenum, urb, status);
1857 spin_unlock_irqrestore(&r8a66597->lock, flags);
1861 static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1862 struct usb_host_endpoint *hep)
1864 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1865 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1866 struct r8a66597_td *td;
1867 struct urb *urb = NULL;
1869 unsigned long flags;
1873 pipenum = pipe->info.pipenum;
1881 spin_lock_irqsave(&r8a66597->lock, flags);
1882 pipe_stop(r8a66597, pipe);
1883 pipe_irq_disable(r8a66597, pipenum);
1884 disable_irq_empty(r8a66597, pipenum);
1885 td = r8a66597_get_td(r8a66597, pipenum);
1888 finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
1891 spin_unlock_irqrestore(&r8a66597->lock, flags);
1894 static int r8a66597_get_frame(struct usb_hcd *hcd)
1896 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1897 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
1900 static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
1904 if (udev->state == USB_STATE_CONFIGURED &&
1905 udev->parent && udev->parent->devnum > 1 &&
1906 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
1907 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
1909 for (chix = 0; chix < udev->maxchild; chix++) {
1910 struct usb_device *childdev = udev->children[chix];
1913 collect_usb_address_map(childdev, map);
1917 /* this function must be called with interrupt disabled */
1918 static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
1921 struct r8a66597_device *dev;
1922 struct list_head *list = &r8a66597->child_device;
1924 list_for_each_entry(dev, list, device_list) {
1927 if (dev->usb_address != addr)
1933 err("get_r8a66597_device fail.(%d)\n", addr);
1937 static void update_usb_address_map(struct r8a66597 *r8a66597,
1938 struct usb_device *root_hub,
1943 unsigned long flags;
1945 for (i = 0; i < 4; i++) {
1946 diff = r8a66597->child_connect_map[i] ^ map[i];
1950 for (j = 0; j < 32; j++) {
1951 if (!(diff & (1 << j)))
1955 if (map[i] & (1 << j))
1956 set_child_connect_map(r8a66597, addr);
1958 struct r8a66597_device *dev;
1960 spin_lock_irqsave(&r8a66597->lock, flags);
1961 dev = get_r8a66597_device(r8a66597, addr);
1962 disable_r8a66597_pipe_all(r8a66597, dev);
1963 free_usb_address(r8a66597, dev);
1964 put_child_connect_map(r8a66597, addr);
1965 spin_unlock_irqrestore(&r8a66597->lock, flags);
1971 static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
1972 struct usb_hcd *hcd)
1974 struct usb_bus *bus;
1975 unsigned long now_map[4];
1977 memset(now_map, 0, sizeof(now_map));
1979 list_for_each_entry(bus, &usb_bus_list, bus_list) {
1983 if (bus->busnum != hcd->self.busnum)
1986 collect_usb_address_map(bus->root_hub, now_map);
1987 update_usb_address_map(r8a66597, bus->root_hub, now_map);
1991 static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
1993 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1994 unsigned long flags;
1997 r8a66597_check_detect_child(r8a66597, hcd);
1999 spin_lock_irqsave(&r8a66597->lock, flags);
2001 *buf = 0; /* initialize (no change) */
2003 for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++) {
2004 if (r8a66597->root_hub[i].port & 0xffff0000)
2005 *buf |= 1 << (i + 1);
2008 spin_unlock_irqrestore(&r8a66597->lock, flags);
2013 static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2014 struct usb_hub_descriptor *desc)
2016 desc->bDescriptorType = 0x29;
2017 desc->bHubContrCurrent = 0;
2018 desc->bNbrPorts = R8A66597_MAX_ROOT_HUB;
2019 desc->bDescLength = 9;
2020 desc->bPwrOn2PwrGood = 0;
2021 desc->wHubCharacteristics = cpu_to_le16(0x0011);
2022 desc->bitmap[0] = ((1 << R8A66597_MAX_ROOT_HUB) - 1) << 1;
2023 desc->bitmap[1] = ~0;
2026 static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2027 u16 wIndex, char *buf, u16 wLength)
2029 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2031 int port = (wIndex & 0x00FF) - 1;
2032 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2033 unsigned long flags;
2037 spin_lock_irqsave(&r8a66597->lock, flags);
2039 case ClearHubFeature:
2042 case C_HUB_OVER_CURRENT:
2043 case C_HUB_LOCAL_POWER:
2049 case ClearPortFeature:
2050 if (wIndex > R8A66597_MAX_ROOT_HUB)
2056 case USB_PORT_FEAT_ENABLE:
2057 rh->port &= (1 << USB_PORT_FEAT_POWER);
2059 case USB_PORT_FEAT_SUSPEND:
2061 case USB_PORT_FEAT_POWER:
2062 r8a66597_port_power(r8a66597, port, 0);
2064 case USB_PORT_FEAT_C_ENABLE:
2065 case USB_PORT_FEAT_C_SUSPEND:
2066 case USB_PORT_FEAT_C_CONNECTION:
2067 case USB_PORT_FEAT_C_OVER_CURRENT:
2068 case USB_PORT_FEAT_C_RESET:
2073 rh->port &= ~(1 << wValue);
2075 case GetHubDescriptor:
2076 r8a66597_hub_descriptor(r8a66597,
2077 (struct usb_hub_descriptor *)buf);
2083 if (wIndex > R8A66597_MAX_ROOT_HUB)
2085 *(u32 *)buf = cpu_to_le32(rh->port);
2087 case SetPortFeature:
2088 if (wIndex > R8A66597_MAX_ROOT_HUB)
2094 case USB_PORT_FEAT_SUSPEND:
2096 case USB_PORT_FEAT_POWER:
2097 r8a66597_port_power(r8a66597, port, 1);
2098 rh->port |= (1 << USB_PORT_FEAT_POWER);
2100 case USB_PORT_FEAT_RESET: {
2101 struct r8a66597_device *dev = rh->dev;
2103 rh->port |= (1 << USB_PORT_FEAT_RESET);
2105 disable_r8a66597_pipe_all(r8a66597, dev);
2106 free_usb_address(r8a66597, dev);
2108 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2109 get_dvstctr_reg(port));
2110 mod_timer(&r8a66597->rh_timer,
2111 jiffies + msecs_to_jiffies(50));
2117 rh->port |= 1 << wValue;
2125 spin_unlock_irqrestore(&r8a66597->lock, flags);
2129 static struct hc_driver r8a66597_hc_driver = {
2130 .description = hcd_name,
2131 .hcd_priv_size = sizeof(struct r8a66597),
2132 .irq = r8a66597_irq,
2135 * generic hardware linkage
2139 .start = r8a66597_start,
2140 .stop = r8a66597_stop,
2143 * managing i/o requests and associated device resources
2145 .urb_enqueue = r8a66597_urb_enqueue,
2146 .urb_dequeue = r8a66597_urb_dequeue,
2147 .endpoint_disable = r8a66597_endpoint_disable,
2150 * periodic schedule support
2152 .get_frame_number = r8a66597_get_frame,
2157 .hub_status_data = r8a66597_hub_status_data,
2158 .hub_control = r8a66597_hub_control,
2161 #if defined(CONFIG_PM)
2162 static int r8a66597_suspend(struct platform_device *pdev, pm_message_t state)
2167 static int r8a66597_resume(struct platform_device *pdev)
2171 #else /* if defined(CONFIG_PM) */
2172 #define r8a66597_suspend NULL
2173 #define r8a66597_resume NULL
2176 static int __init_or_module r8a66597_remove(struct platform_device *pdev)
2178 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2179 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2181 del_timer_sync(&r8a66597->rh_timer);
2182 usb_remove_hcd(hcd);
2183 iounmap((void *)r8a66597->reg);
2188 #define resource_len(r) (((r)->end - (r)->start) + 1)
2189 static int __init r8a66597_probe(struct platform_device *pdev)
2191 struct resource *res = NULL;
2193 void __iomem *reg = NULL;
2194 struct usb_hcd *hcd = NULL;
2195 struct r8a66597 *r8a66597;
2199 if (pdev->dev.dma_mask) {
2201 err("dma not support");
2205 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2209 err("platform_get_resource_byname error.");
2213 irq = platform_get_irq(pdev, 0);
2216 err("platform_get_irq error.");
2220 reg = ioremap(res->start, resource_len(res));
2223 err("ioremap error.");
2227 /* initialize hcd */
2228 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2231 err("Failed to create hcd");
2234 r8a66597 = hcd_to_r8a66597(hcd);
2235 memset(r8a66597, 0, sizeof(struct r8a66597));
2236 dev_set_drvdata(&pdev->dev, r8a66597);
2238 spin_lock_init(&r8a66597->lock);
2239 init_timer(&r8a66597->rh_timer);
2240 r8a66597->rh_timer.function = r8a66597_timer;
2241 r8a66597->rh_timer.data = (unsigned long)r8a66597;
2242 r8a66597->reg = (unsigned long)reg;
2244 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2245 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2246 init_timer(&r8a66597->td_timer[i]);
2247 r8a66597->td_timer[i].function = r8a66597_td_timer;
2248 r8a66597->td_timer[i].data = (unsigned long)r8a66597;
2249 setup_timer(&r8a66597->interval_timer[i],
2250 r8a66597_interval_timer,
2251 (unsigned long)r8a66597);
2253 INIT_LIST_HEAD(&r8a66597->child_device);
2255 hcd->rsrc_start = res->start;
2256 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED);
2258 err("Failed to add hcd");
2271 static struct platform_driver r8a66597_driver = {
2272 .probe = r8a66597_probe,
2273 .remove = r8a66597_remove,
2274 .suspend = r8a66597_suspend,
2275 .resume = r8a66597_resume,
2277 .name = (char *) hcd_name,
2278 .owner = THIS_MODULE,
2282 static int __init r8a66597_init(void)
2287 info("driver %s, %s", hcd_name, DRIVER_VERSION);
2288 return platform_driver_register(&r8a66597_driver);
2290 module_init(r8a66597_init);
2292 static void __exit r8a66597_cleanup(void)
2294 platform_driver_unregister(&r8a66597_driver);
2296 module_exit(r8a66597_cleanup);