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/errno.h>
30 #include <linux/init.h>
31 #include <linux/timer.h>
32 #include <linux/delay.h>
33 #include <linux/list.h>
34 #include <linux/interrupt.h>
35 #include <linux/usb.h>
36 #include <linux/platform_device.h>
39 #include <linux/irq.h>
40 #include <asm/cacheflush.h>
42 #include "../core/hcd.h"
45 MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
46 MODULE_LICENSE("GPL");
47 MODULE_AUTHOR("Yoshihiro Shimoda");
48 MODULE_ALIAS("platform:r8a66597_hcd");
50 #define DRIVER_VERSION "2009-05-26"
52 static const char hcd_name[] = "r8a66597_hcd";
54 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
55 static int r8a66597_get_frame(struct usb_hcd *hcd);
57 /* this function must be called with interrupt disabled */
58 static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
63 tmp = r8a66597_read(r8a66597, INTENB0);
64 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
65 r8a66597_bset(r8a66597, 1 << pipenum, reg);
66 r8a66597_write(r8a66597, tmp, INTENB0);
69 /* this function must be called with interrupt disabled */
70 static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
75 tmp = r8a66597_read(r8a66597, INTENB0);
76 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
77 r8a66597_bclr(r8a66597, 1 << pipenum, reg);
78 r8a66597_write(r8a66597, tmp, INTENB0);
81 static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
82 u16 usbspd, u8 upphub, u8 hubport, int port)
85 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
87 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
88 r8a66597_write(r8a66597, val, devadd_reg);
91 static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
96 if (r8a66597->pdata->on_chip) {
97 #ifdef CONFIG_HAVE_CLK
98 clk_enable(r8a66597->clk);
101 r8a66597_write(r8a66597, SCKE, SYSCFG0);
102 tmp = r8a66597_read(r8a66597, SYSCFG0);
104 printk(KERN_ERR "r8a66597: reg access fail.\n");
107 } while ((tmp & SCKE) != SCKE);
108 r8a66597_write(r8a66597, 0x04, 0x02);
111 r8a66597_write(r8a66597, USBE, SYSCFG0);
112 tmp = r8a66597_read(r8a66597, SYSCFG0);
114 printk(KERN_ERR "r8a66597: reg access fail.\n");
117 } while ((tmp & USBE) != USBE);
118 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
119 r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata),
123 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
126 tmp = r8a66597_read(r8a66597, SYSCFG0);
128 printk(KERN_ERR "r8a66597: reg access fail.\n");
131 } while ((tmp & SCKE) != SCKE);
137 static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
139 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
142 if (r8a66597->pdata->on_chip) {
143 #ifdef CONFIG_HAVE_CLK
144 clk_disable(r8a66597->clk);
147 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
148 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
149 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
153 static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
157 val = port ? DRPD : DCFM | DRPD;
158 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
159 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
161 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
162 r8a66597_bclr(r8a66597, DTCHE, get_intenb_reg(port));
163 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
166 static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
170 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
171 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
173 r8a66597_port_power(r8a66597, port, 0);
176 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
178 } while (tmp == EDGESTS);
180 val = port ? DRPD : DCFM | DRPD;
181 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
182 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
185 static int enable_controller(struct r8a66597 *r8a66597)
188 u16 vif = r8a66597->pdata->vif ? LDRV : 0;
189 u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
190 u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
192 ret = r8a66597_clock_enable(r8a66597);
196 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
197 r8a66597_bset(r8a66597, USBE, SYSCFG0);
199 r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
200 r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
201 r8a66597_bset(r8a66597, BRDY0, BRDYENB);
202 r8a66597_bset(r8a66597, BEMP0, BEMPENB);
204 r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
205 r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
206 r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
207 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
209 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
211 for (port = 0; port < r8a66597->max_root_hub; port++)
212 r8a66597_enable_port(r8a66597, port);
217 static void disable_controller(struct r8a66597 *r8a66597)
221 /* disable interrupts */
222 r8a66597_write(r8a66597, 0, INTENB0);
223 r8a66597_write(r8a66597, 0, INTENB1);
224 r8a66597_write(r8a66597, 0, BRDYENB);
225 r8a66597_write(r8a66597, 0, BEMPENB);
226 r8a66597_write(r8a66597, 0, NRDYENB);
229 r8a66597_write(r8a66597, 0, BRDYSTS);
230 r8a66597_write(r8a66597, 0, NRDYSTS);
231 r8a66597_write(r8a66597, 0, BEMPSTS);
233 for (port = 0; port < r8a66597->max_root_hub; port++)
234 r8a66597_disable_port(r8a66597, port);
236 r8a66597_clock_disable(r8a66597);
239 static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
240 struct usb_device *udev)
242 struct r8a66597_device *dev;
244 if (udev->parent && udev->parent->devnum != 1)
247 dev = dev_get_drvdata(&udev->dev);
254 static int is_child_device(char *devpath)
256 return (devpath[2] ? 1 : 0);
259 static int is_hub_limit(char *devpath)
261 return ((strlen(devpath) >= 4) ? 1 : 0);
264 static void get_port_number(struct r8a66597 *r8a66597,
265 char *devpath, u16 *root_port, u16 *hub_port)
268 *root_port = (devpath[0] & 0x0F) - 1;
269 if (*root_port >= r8a66597->max_root_hub)
270 printk(KERN_ERR "r8a66597: Illegal root port number.\n");
273 *hub_port = devpath[2] & 0x0F;
276 static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
291 printk(KERN_ERR "r8a66597: unknown speed\n");
298 static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
303 r8a66597->child_connect_map[idx] |= 1 << (address % 32);
306 static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
311 r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
314 static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
316 u16 pipenum = pipe->info.pipenum;
317 const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
318 const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
319 const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
321 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
322 dma_ch = R8A66597_PIPE_NO_DMA;
324 pipe->fifoaddr = fifoaddr[dma_ch];
325 pipe->fifosel = fifosel[dma_ch];
326 pipe->fifoctr = fifoctr[dma_ch];
329 pipe->pipectr = DCPCTR;
331 pipe->pipectr = get_pipectr_addr(pipenum);
333 if (check_bulk_or_isoc(pipenum)) {
334 pipe->pipetre = get_pipetre_addr(pipenum);
335 pipe->pipetrn = get_pipetrn_addr(pipenum);
342 static struct r8a66597_device *
343 get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
345 if (usb_pipedevice(urb->pipe) == 0)
346 return &r8a66597->device0;
348 return dev_get_drvdata(&urb->dev->dev);
351 static int make_r8a66597_device(struct r8a66597 *r8a66597,
352 struct urb *urb, u8 addr)
354 struct r8a66597_device *dev;
355 int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
357 dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
361 dev_set_drvdata(&urb->dev->dev, dev);
362 dev->udev = urb->dev;
364 dev->usb_address = usb_address;
365 dev->state = USB_STATE_ADDRESS;
366 dev->ep_in_toggle = 0;
367 dev->ep_out_toggle = 0;
368 INIT_LIST_HEAD(&dev->device_list);
369 list_add_tail(&dev->device_list, &r8a66597->child_device);
371 get_port_number(r8a66597, urb->dev->devpath,
372 &dev->root_port, &dev->hub_port);
373 if (!is_child_device(urb->dev->devpath))
374 r8a66597->root_hub[dev->root_port].dev = dev;
376 set_devadd_reg(r8a66597, dev->address,
377 get_r8a66597_usb_speed(urb->dev->speed),
378 get_parent_r8a66597_address(r8a66597, urb->dev),
379 dev->hub_port, dev->root_port);
384 /* this function must be called with interrupt disabled */
385 static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
387 u8 addr; /* R8A66597's address */
388 struct r8a66597_device *dev;
390 if (is_hub_limit(urb->dev->devpath)) {
391 dev_err(&urb->dev->dev, "External hub limit reached.\n");
395 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
396 if (dev && dev->state >= USB_STATE_ADDRESS)
399 for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
400 if (r8a66597->address_map & (1 << addr))
403 dbg("alloc_address: r8a66597_addr=%d", addr);
404 r8a66597->address_map |= 1 << addr;
406 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
412 dev_err(&urb->dev->dev,
413 "cannot communicate with a USB device more than 10.(%x)\n",
414 r8a66597->address_map);
419 /* this function must be called with interrupt disabled */
420 static void free_usb_address(struct r8a66597 *r8a66597,
421 struct r8a66597_device *dev)
428 dbg("free_addr: addr=%d", dev->address);
430 dev->state = USB_STATE_DEFAULT;
431 r8a66597->address_map &= ~(1 << dev->address);
433 dev_set_drvdata(&dev->udev->dev, NULL);
434 list_del(&dev->device_list);
437 for (port = 0; port < r8a66597->max_root_hub; port++) {
438 if (r8a66597->root_hub[port].dev == dev) {
439 r8a66597->root_hub[port].dev = NULL;
445 static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
452 tmp = r8a66597_read(r8a66597, reg);
454 printk(KERN_ERR "r8a66597: register%lx, loop %x "
455 "is timeout\n", reg, loop);
459 } while ((tmp & mask) != loop);
462 /* this function must be called with interrupt disabled */
463 static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
467 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
468 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
469 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
470 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
473 /* this function must be called with interrupt disabled */
474 static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
478 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
479 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
480 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
481 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
482 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
485 /* this function must be called with interrupt disabled */
486 static void clear_all_buffer(struct r8a66597 *r8a66597,
487 struct r8a66597_pipe *pipe)
491 if (!pipe || pipe->info.pipenum == 0)
494 pipe_stop(r8a66597, pipe);
495 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
496 tmp = r8a66597_read(r8a66597, pipe->pipectr);
497 tmp = r8a66597_read(r8a66597, pipe->pipectr);
498 tmp = r8a66597_read(r8a66597, pipe->pipectr);
499 r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
502 /* this function must be called with interrupt disabled */
503 static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
504 struct r8a66597_pipe *pipe, int toggle)
507 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
509 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
512 static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
514 if (r8a66597->pdata->on_chip)
520 /* this function must be called with interrupt disabled */
521 static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
523 unsigned short mbw = mbw_value(r8a66597);
525 r8a66597_mdfy(r8a66597, mbw | pipenum, mbw | CURPIPE, CFIFOSEL);
526 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
529 /* this function must be called with interrupt disabled */
530 static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
531 struct r8a66597_pipe *pipe)
533 unsigned short mbw = mbw_value(r8a66597);
535 cfifo_change(r8a66597, 0);
536 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D0FIFOSEL);
537 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D1FIFOSEL);
539 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum, mbw | CURPIPE,
541 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
544 static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
546 struct r8a66597_pipe *pipe = hep->hcpriv;
548 if (usb_pipeendpoint(urb->pipe) == 0)
551 return pipe->info.pipenum;
554 static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
556 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
558 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
561 static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
567 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
570 /* this function must be called with interrupt disabled */
571 static void pipe_toggle_set(struct r8a66597 *r8a66597,
572 struct r8a66597_pipe *pipe,
573 struct urb *urb, int set)
575 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
576 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
577 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
583 *toggle |= 1 << endpoint;
585 *toggle &= ~(1 << endpoint);
588 /* this function must be called with interrupt disabled */
589 static void pipe_toggle_save(struct r8a66597 *r8a66597,
590 struct r8a66597_pipe *pipe,
593 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
594 pipe_toggle_set(r8a66597, pipe, urb, 1);
596 pipe_toggle_set(r8a66597, pipe, urb, 0);
599 /* this function must be called with interrupt disabled */
600 static void pipe_toggle_restore(struct r8a66597 *r8a66597,
601 struct r8a66597_pipe *pipe,
604 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
605 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
606 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
611 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
614 /* this function must be called with interrupt disabled */
615 static void pipe_buffer_setting(struct r8a66597 *r8a66597,
616 struct r8a66597_pipe_info *info)
620 if (info->pipenum == 0)
623 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
624 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
625 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
628 if (info->type == R8A66597_BULK && info->dir_in)
629 val |= R8A66597_DBLB | R8A66597_SHTNAK;
630 val |= info->type | info->epnum;
631 r8a66597_write(r8a66597, val, PIPECFG);
633 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
635 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
637 r8a66597_write(r8a66597, info->interval, PIPEPERI);
640 /* this function must be called with interrupt disabled */
641 static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
643 struct r8a66597_pipe_info *info;
644 struct urb *urb = td->urb;
646 if (td->pipenum > 0) {
647 info = &td->pipe->info;
648 cfifo_change(r8a66597, 0);
649 pipe_buffer_setting(r8a66597, info);
651 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
652 usb_pipeout(urb->pipe)) &&
653 !usb_pipecontrol(urb->pipe)) {
654 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
655 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
656 clear_all_buffer(r8a66597, td->pipe);
657 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
658 usb_pipeout(urb->pipe), 1);
660 pipe_toggle_restore(r8a66597, td->pipe, urb);
664 /* this function must be called with interrupt disabled */
665 static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
666 struct usb_endpoint_descriptor *ep)
668 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
670 memset(array, 0, sizeof(array));
671 switch (usb_endpoint_type(ep)) {
672 case USB_ENDPOINT_XFER_BULK:
673 if (usb_endpoint_dir_in(ep))
680 case USB_ENDPOINT_XFER_INT:
681 if (usb_endpoint_dir_in(ep)) {
688 case USB_ENDPOINT_XFER_ISOC:
689 if (usb_endpoint_dir_in(ep))
695 printk(KERN_ERR "r8a66597: Illegal type\n");
701 while (array[i] != 0) {
702 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
710 static u16 get_r8a66597_type(__u8 type)
715 case USB_ENDPOINT_XFER_BULK:
716 r8a66597_type = R8A66597_BULK;
718 case USB_ENDPOINT_XFER_INT:
719 r8a66597_type = R8A66597_INT;
721 case USB_ENDPOINT_XFER_ISOC:
722 r8a66597_type = R8A66597_ISO;
725 printk(KERN_ERR "r8a66597: Illegal type\n");
726 r8a66597_type = 0x0000;
730 return r8a66597_type;
733 static u16 get_bufnum(u16 pipenum)
739 else if (check_bulk_or_isoc(pipenum))
740 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
741 else if (check_interrupt(pipenum))
742 bufnum = 4 + (pipenum - 6);
744 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
749 static u16 get_buf_bsize(u16 pipenum)
755 else if (check_bulk_or_isoc(pipenum))
756 buf_bsize = R8A66597_BUF_BSIZE - 1;
757 else if (check_interrupt(pipenum))
760 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
765 /* this function must be called with interrupt disabled */
766 static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
767 struct r8a66597_device *dev,
768 struct r8a66597_pipe *pipe,
772 struct r8a66597_pipe_info *info = &pipe->info;
773 unsigned short mbw = mbw_value(r8a66597);
775 /* pipe dma is only for external controlles */
776 if (r8a66597->pdata->on_chip)
779 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
780 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
781 if ((r8a66597->dma_map & (1 << i)) != 0)
784 dev_info(&dev->udev->dev,
785 "address %d, EndpointAddress 0x%02x use "
786 "DMA FIFO\n", usb_pipedevice(urb->pipe),
788 USB_ENDPOINT_DIR_MASK + info->epnum
791 r8a66597->dma_map |= 1 << i;
792 dev->dma_map |= 1 << i;
793 set_pipe_reg_addr(pipe, i);
795 cfifo_change(r8a66597, 0);
796 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum,
797 mbw | CURPIPE, pipe->fifosel);
799 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
801 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
807 /* this function must be called with interrupt disabled */
808 static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
809 struct usb_host_endpoint *hep,
810 struct r8a66597_pipe_info *info)
812 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
813 struct r8a66597_pipe *pipe = hep->hcpriv;
818 set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
819 r8a66597->pipe_cnt[pipe->info.pipenum]++;
820 dev->pipe_cnt[pipe->info.pipenum]++;
822 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
825 static void r8a66597_urb_done(struct r8a66597 *r8a66597, struct urb *urb,
827 __releases(r8a66597->lock)
828 __acquires(r8a66597->lock)
830 if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
833 for (ptr = urb->transfer_buffer;
834 ptr < urb->transfer_buffer + urb->transfer_buffer_length;
836 flush_dcache_page(virt_to_page(ptr));
839 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
840 spin_unlock(&r8a66597->lock);
841 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb, status);
842 spin_lock(&r8a66597->lock);
845 /* this function must be called with interrupt disabled */
846 static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
848 struct r8a66597_td *td, *next;
850 struct list_head *list = &r8a66597->pipe_queue[pipenum];
852 if (list_empty(list))
855 list_for_each_entry_safe(td, next, list, queue) {
856 if (td->address != address)
860 list_del(&td->queue);
864 r8a66597_urb_done(r8a66597, urb, -ENODEV);
870 /* this function must be called with interrupt disabled */
871 static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
872 struct r8a66597_device *dev)
880 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
881 if (!dev->pipe_cnt[pipenum])
886 force_dequeue(r8a66597, 0, dev->address);
889 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
890 dev->pipe_cnt[pipenum] = 0;
891 force_dequeue(r8a66597, pipenum, dev->address);
896 r8a66597->dma_map &= ~(dev->dma_map);
900 static u16 get_interval(struct urb *urb, __u8 interval)
905 if (urb->dev->speed == USB_SPEED_HIGH) {
909 time = interval ? interval - 1 : 0;
911 if (interval > 128) {
914 /* calculate the nearest value for PIPEPERI */
915 for (i = 0; i < 7; i++) {
916 if ((1 << i) < interval &&
917 (1 << (i + 1) > interval))
926 static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
929 unsigned long time = 1;
931 if (usb_pipeisoc(urb->pipe))
934 if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
935 for (i = 0; i < (interval - 1); i++)
937 time = time * 125 / 1000; /* uSOF -> msec */
945 /* this function must be called with interrupt disabled */
946 static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
947 struct usb_host_endpoint *hep,
948 struct usb_endpoint_descriptor *ep)
950 struct r8a66597_pipe_info info;
952 info.pipenum = get_empty_pipenum(r8a66597, ep);
953 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
954 info.epnum = usb_endpoint_num(ep);
955 info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
956 info.type = get_r8a66597_type(usb_endpoint_type(ep));
957 info.bufnum = get_bufnum(info.pipenum);
958 info.buf_bsize = get_buf_bsize(info.pipenum);
959 if (info.type == R8A66597_BULK) {
961 info.timer_interval = 0;
963 info.interval = get_interval(urb, ep->bInterval);
964 info.timer_interval = get_timer_interval(urb, ep->bInterval);
966 if (usb_endpoint_dir_in(ep))
971 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
974 static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
976 struct r8a66597_device *dev;
978 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
979 dev->state = USB_STATE_CONFIGURED;
982 static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
985 if (pipenum == 0 && usb_pipeout(urb->pipe))
986 enable_irq_empty(r8a66597, pipenum);
988 enable_irq_ready(r8a66597, pipenum);
990 if (!usb_pipeisoc(urb->pipe))
991 enable_irq_nrdy(r8a66597, pipenum);
994 static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
996 disable_irq_ready(r8a66597, pipenum);
997 disable_irq_nrdy(r8a66597, pipenum);
1000 static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
1002 mod_timer(&r8a66597->rh_timer,
1003 jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
1006 static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
1009 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1011 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1012 rh->scount = R8A66597_MAX_SAMPLING;
1014 rh->port |= 1 << USB_PORT_FEAT_CONNECTION;
1016 rh->port &= ~(1 << USB_PORT_FEAT_CONNECTION);
1017 rh->port |= 1 << USB_PORT_FEAT_C_CONNECTION;
1019 r8a66597_root_hub_start_polling(r8a66597);
1022 /* this function must be called with interrupt disabled */
1023 static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
1025 __releases(r8a66597->lock)
1026 __acquires(r8a66597->lock)
1028 if (syssts == SE0) {
1029 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1030 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1032 if (syssts == FS_JSTS)
1033 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
1034 else if (syssts == LS_JSTS)
1035 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
1037 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
1038 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
1040 if (r8a66597->bus_suspended)
1041 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1044 spin_unlock(&r8a66597->lock);
1045 usb_hcd_poll_rh_status(r8a66597_to_hcd(r8a66597));
1046 spin_lock(&r8a66597->lock);
1049 /* this function must be called with interrupt disabled */
1050 static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
1052 u16 speed = get_rh_usb_speed(r8a66597, port);
1053 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1055 rh->port &= ~((1 << USB_PORT_FEAT_HIGHSPEED) |
1056 (1 << USB_PORT_FEAT_LOWSPEED));
1057 if (speed == HSMODE)
1058 rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
1059 else if (speed == LSMODE)
1060 rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
1062 rh->port &= ~(1 << USB_PORT_FEAT_RESET);
1063 rh->port |= 1 << USB_PORT_FEAT_ENABLE;
1066 /* this function must be called with interrupt disabled */
1067 static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
1069 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
1071 disable_r8a66597_pipe_all(r8a66597, dev);
1072 free_usb_address(r8a66597, dev);
1074 start_root_hub_sampling(r8a66597, port, 0);
1077 /* this function must be called with interrupt disabled */
1078 static void prepare_setup_packet(struct r8a66597 *r8a66597,
1079 struct r8a66597_td *td)
1082 __le16 *p = (__le16 *)td->urb->setup_packet;
1083 unsigned long setup_addr = USBREQ;
1085 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1087 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
1089 for (i = 0; i < 4; i++) {
1090 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
1093 r8a66597_write(r8a66597, SUREQ, DCPCTR);
1096 /* this function must be called with interrupt disabled */
1097 static void prepare_packet_read(struct r8a66597 *r8a66597,
1098 struct r8a66597_td *td)
1100 struct urb *urb = td->urb;
1102 if (usb_pipecontrol(urb->pipe)) {
1103 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1104 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1105 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1106 if (urb->actual_length == 0) {
1107 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1108 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1110 pipe_irq_disable(r8a66597, td->pipenum);
1111 pipe_start(r8a66597, td->pipe);
1112 pipe_irq_enable(r8a66597, urb, td->pipenum);
1114 if (urb->actual_length == 0) {
1115 pipe_irq_disable(r8a66597, td->pipenum);
1116 pipe_setting(r8a66597, td);
1117 pipe_stop(r8a66597, td->pipe);
1118 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1120 if (td->pipe->pipetre) {
1121 r8a66597_write(r8a66597, TRCLR,
1123 r8a66597_write(r8a66597,
1125 (urb->transfer_buffer_length,
1128 r8a66597_bset(r8a66597, TRENB,
1132 pipe_start(r8a66597, td->pipe);
1133 pipe_irq_enable(r8a66597, urb, td->pipenum);
1138 /* this function must be called with interrupt disabled */
1139 static void prepare_packet_write(struct r8a66597 *r8a66597,
1140 struct r8a66597_td *td)
1143 struct urb *urb = td->urb;
1145 if (usb_pipecontrol(urb->pipe)) {
1146 pipe_stop(r8a66597, td->pipe);
1147 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1148 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1149 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1150 if (urb->actual_length == 0) {
1151 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1152 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1155 if (urb->actual_length == 0)
1156 pipe_setting(r8a66597, td);
1157 if (td->pipe->pipetre)
1158 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1160 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1162 fifo_change_from_pipe(r8a66597, td->pipe);
1163 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1164 if (unlikely((tmp & FRDY) == 0))
1165 pipe_irq_enable(r8a66597, urb, td->pipenum);
1167 packet_write(r8a66597, td->pipenum);
1168 pipe_start(r8a66597, td->pipe);
1171 /* this function must be called with interrupt disabled */
1172 static void prepare_status_packet(struct r8a66597 *r8a66597,
1173 struct r8a66597_td *td)
1175 struct urb *urb = td->urb;
1177 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1178 pipe_stop(r8a66597, td->pipe);
1180 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1181 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1182 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1183 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1184 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
1185 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
1186 enable_irq_empty(r8a66597, 0);
1188 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1189 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1190 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1191 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1192 enable_irq_ready(r8a66597, 0);
1194 enable_irq_nrdy(r8a66597, 0);
1195 pipe_start(r8a66597, td->pipe);
1198 static int is_set_address(unsigned char *setup_packet)
1200 if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1201 setup_packet[1] == USB_REQ_SET_ADDRESS)
1207 /* this function must be called with interrupt disabled */
1208 static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1214 if (is_set_address(td->urb->setup_packet)) {
1215 td->set_address = 1;
1216 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1218 if (td->urb->setup_packet[2] == 0)
1221 prepare_setup_packet(r8a66597, td);
1224 prepare_packet_read(r8a66597, td);
1227 prepare_packet_write(r8a66597, td);
1230 prepare_status_packet(r8a66597, td);
1233 printk(KERN_ERR "r8a66597: invalid type.\n");
1240 static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1242 if (usb_pipeisoc(urb->pipe)) {
1243 if (urb->number_of_packets == td->iso_cnt)
1247 /* control or bulk or interrupt */
1248 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1249 (td->short_packet) || (td->zero_packet))
1255 /* this function must be called with interrupt disabled */
1256 static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1262 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1263 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1264 r8a66597->timeout_map |= 1 << td->pipenum;
1265 switch (usb_pipetype(td->urb->pipe)) {
1266 case PIPE_INTERRUPT:
1267 case PIPE_ISOCHRONOUS:
1275 mod_timer(&r8a66597->td_timer[td->pipenum],
1276 jiffies + msecs_to_jiffies(time));
1280 /* this function must be called with interrupt disabled */
1281 static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
1282 u16 pipenum, struct urb *urb, int status)
1283 __releases(r8a66597->lock) __acquires(r8a66597->lock)
1286 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1288 r8a66597->timeout_map &= ~(1 << pipenum);
1291 if (td->set_address && (status != 0 || urb->unlinked))
1292 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1294 pipe_toggle_save(r8a66597, td->pipe, urb);
1295 list_del(&td->queue);
1299 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1303 if (usb_pipeisoc(urb->pipe))
1304 urb->start_frame = r8a66597_get_frame(hcd);
1306 r8a66597_urb_done(r8a66597, urb, status);
1310 td = r8a66597_get_td(r8a66597, pipenum);
1314 start_transfer(r8a66597, td);
1315 set_td_timer(r8a66597, td);
1319 static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1322 int rcv_len, bufsize, urb_len, size;
1324 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1333 fifo_change_from_pipe(r8a66597, td->pipe);
1334 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1335 if (unlikely((tmp & FRDY) == 0)) {
1336 pipe_stop(r8a66597, td->pipe);
1337 pipe_irq_disable(r8a66597, pipenum);
1338 printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
1339 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
1343 /* prepare parameters */
1344 rcv_len = tmp & DTLN;
1345 if (usb_pipeisoc(urb->pipe)) {
1346 buf = (u16 *)(urb->transfer_buffer +
1347 urb->iso_frame_desc[td->iso_cnt].offset);
1348 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1350 buf = (void *)urb->transfer_buffer + urb->actual_length;
1351 urb_len = urb->transfer_buffer_length - urb->actual_length;
1353 bufsize = min(urb_len, (int) td->maxpacket);
1354 if (rcv_len <= bufsize) {
1358 status = -EOVERFLOW;
1362 /* update parameters */
1363 urb->actual_length += size;
1365 td->zero_packet = 1;
1366 if (rcv_len < bufsize) {
1367 td->short_packet = 1;
1369 if (usb_pipeisoc(urb->pipe)) {
1370 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1371 urb->iso_frame_desc[td->iso_cnt].status = status;
1376 /* check transfer finish */
1377 if (finish || check_transfer_finish(td, urb)) {
1378 pipe_stop(r8a66597, td->pipe);
1379 pipe_irq_disable(r8a66597, pipenum);
1384 if (urb->transfer_buffer) {
1386 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1388 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1392 if (finish && pipenum != 0)
1393 finish_request(r8a66597, td, pipenum, urb, status);
1396 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1401 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1408 fifo_change_from_pipe(r8a66597, td->pipe);
1409 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1410 if (unlikely((tmp & FRDY) == 0)) {
1411 pipe_stop(r8a66597, td->pipe);
1412 pipe_irq_disable(r8a66597, pipenum);
1413 printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
1414 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
1418 /* prepare parameters */
1419 bufsize = td->maxpacket;
1420 if (usb_pipeisoc(urb->pipe)) {
1421 buf = (u16 *)(urb->transfer_buffer +
1422 urb->iso_frame_desc[td->iso_cnt].offset);
1424 (int)urb->iso_frame_desc[td->iso_cnt].length);
1426 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
1427 size = min_t(u32, bufsize,
1428 urb->transfer_buffer_length - urb->actual_length);
1433 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
1434 if (urb->transfer_buffer) {
1435 r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
1436 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1437 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1440 /* update parameters */
1441 urb->actual_length += size;
1442 if (usb_pipeisoc(urb->pipe)) {
1443 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1444 urb->iso_frame_desc[td->iso_cnt].status = 0;
1448 /* check transfer finish */
1449 if (check_transfer_finish(td, urb)) {
1450 disable_irq_ready(r8a66597, pipenum);
1451 enable_irq_empty(r8a66597, pipenum);
1452 if (!usb_pipeisoc(urb->pipe))
1453 enable_irq_nrdy(r8a66597, pipenum);
1455 pipe_irq_enable(r8a66597, urb, pipenum);
1459 static void check_next_phase(struct r8a66597 *r8a66597, int status)
1461 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1472 if (check_transfer_finish(td, urb))
1473 td->type = USB_PID_ACK;
1476 if (urb->transfer_buffer_length == urb->actual_length)
1477 td->type = USB_PID_ACK;
1478 else if (usb_pipeout(urb->pipe))
1479 td->type = USB_PID_OUT;
1481 td->type = USB_PID_IN;
1488 if (finish || status != 0 || urb->unlinked)
1489 finish_request(r8a66597, td, 0, urb, status);
1491 start_transfer(r8a66597, td);
1494 static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
1496 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1499 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1509 static void irq_pipe_ready(struct r8a66597 *r8a66597)
1514 struct r8a66597_td *td;
1516 mask = r8a66597_read(r8a66597, BRDYSTS)
1517 & r8a66597_read(r8a66597, BRDYENB);
1518 r8a66597_write(r8a66597, ~mask, BRDYSTS);
1520 td = r8a66597_get_td(r8a66597, 0);
1521 if (td && td->type == USB_PID_IN)
1522 packet_read(r8a66597, 0);
1524 pipe_irq_disable(r8a66597, 0);
1525 check_next_phase(r8a66597, 0);
1528 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1529 check = 1 << pipenum;
1531 td = r8a66597_get_td(r8a66597, pipenum);
1535 if (td->type == USB_PID_IN)
1536 packet_read(r8a66597, pipenum);
1537 else if (td->type == USB_PID_OUT)
1538 packet_write(r8a66597, pipenum);
1543 static void irq_pipe_empty(struct r8a66597 *r8a66597)
1549 struct r8a66597_td *td;
1551 mask = r8a66597_read(r8a66597, BEMPSTS)
1552 & r8a66597_read(r8a66597, BEMPENB);
1553 r8a66597_write(r8a66597, ~mask, BEMPSTS);
1555 cfifo_change(r8a66597, 0);
1556 td = r8a66597_get_td(r8a66597, 0);
1557 if (td && td->type != USB_PID_OUT)
1558 disable_irq_empty(r8a66597, 0);
1559 check_next_phase(r8a66597, 0);
1562 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1563 check = 1 << pipenum;
1565 struct r8a66597_td *td;
1566 td = r8a66597_get_td(r8a66597, pipenum);
1570 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1571 if ((tmp & INBUFM) == 0) {
1572 disable_irq_empty(r8a66597, pipenum);
1573 pipe_irq_disable(r8a66597, pipenum);
1574 finish_request(r8a66597, td, pipenum, td->urb,
1581 static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1588 mask = r8a66597_read(r8a66597, NRDYSTS)
1589 & r8a66597_read(r8a66597, NRDYENB);
1590 r8a66597_write(r8a66597, ~mask, NRDYSTS);
1592 cfifo_change(r8a66597, 0);
1593 status = get_urb_error(r8a66597, 0);
1594 pipe_irq_disable(r8a66597, 0);
1595 check_next_phase(r8a66597, status);
1598 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1599 check = 1 << pipenum;
1601 struct r8a66597_td *td;
1602 td = r8a66597_get_td(r8a66597, pipenum);
1606 status = get_urb_error(r8a66597, pipenum);
1607 pipe_irq_disable(r8a66597, pipenum);
1608 pipe_stop(r8a66597, td->pipe);
1609 finish_request(r8a66597, td, pipenum, td->urb, status);
1614 static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1616 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1617 u16 intsts0, intsts1, intsts2;
1618 u16 intenb0, intenb1, intenb2;
1619 u16 mask0, mask1, mask2;
1622 spin_lock(&r8a66597->lock);
1624 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1625 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1626 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1627 intenb0 = r8a66597_read(r8a66597, INTENB0);
1628 intenb1 = r8a66597_read(r8a66597, INTENB1);
1629 intenb2 = r8a66597_read(r8a66597, INTENB2);
1631 mask2 = intsts2 & intenb2;
1632 mask1 = intsts1 & intenb1;
1633 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1635 if (mask2 & ATTCH) {
1636 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
1637 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1639 /* start usb bus sampling */
1640 start_root_hub_sampling(r8a66597, 1, 1);
1643 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
1644 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1645 r8a66597_usb_disconnect(r8a66597, 1);
1648 r8a66597_write(r8a66597, ~BCHG, INTSTS2);
1649 r8a66597_bclr(r8a66597, BCHGE, INTENB2);
1650 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1655 if (mask1 & ATTCH) {
1656 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
1657 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1659 /* start usb bus sampling */
1660 start_root_hub_sampling(r8a66597, 0, 1);
1663 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
1664 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1665 r8a66597_usb_disconnect(r8a66597, 0);
1668 r8a66597_write(r8a66597, ~BCHG, INTSTS1);
1669 r8a66597_bclr(r8a66597, BCHGE, INTENB1);
1670 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1674 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
1675 status = get_urb_error(r8a66597, 0);
1676 check_next_phase(r8a66597, status);
1679 r8a66597_write(r8a66597, ~SACK, INTSTS1);
1680 check_next_phase(r8a66597, 0);
1685 irq_pipe_ready(r8a66597);
1687 irq_pipe_empty(r8a66597);
1689 irq_pipe_nrdy(r8a66597);
1692 spin_unlock(&r8a66597->lock);
1696 /* this function must be called with interrupt disabled */
1697 static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1700 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1702 if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
1703 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1705 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1706 if ((tmp & USBRST) == USBRST) {
1707 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1709 r8a66597_root_hub_start_polling(r8a66597);
1711 r8a66597_usb_connect(r8a66597, port);
1714 if (!(rh->port & (1 << USB_PORT_FEAT_CONNECTION))) {
1715 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1716 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1719 if (rh->scount > 0) {
1720 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1721 if (tmp == rh->old_syssts) {
1723 if (rh->scount == 0)
1724 r8a66597_check_syssts(r8a66597, port, tmp);
1726 r8a66597_root_hub_start_polling(r8a66597);
1728 rh->scount = R8A66597_MAX_SAMPLING;
1729 rh->old_syssts = tmp;
1730 r8a66597_root_hub_start_polling(r8a66597);
1735 static void r8a66597_interval_timer(unsigned long _r8a66597)
1737 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1738 unsigned long flags;
1740 struct r8a66597_td *td;
1742 spin_lock_irqsave(&r8a66597->lock, flags);
1744 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1745 if (!(r8a66597->interval_map & (1 << pipenum)))
1747 if (timer_pending(&r8a66597->interval_timer[pipenum]))
1750 td = r8a66597_get_td(r8a66597, pipenum);
1752 start_transfer(r8a66597, td);
1755 spin_unlock_irqrestore(&r8a66597->lock, flags);
1758 static void r8a66597_td_timer(unsigned long _r8a66597)
1760 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1761 unsigned long flags;
1763 struct r8a66597_td *td, *new_td = NULL;
1764 struct r8a66597_pipe *pipe;
1766 spin_lock_irqsave(&r8a66597->lock, flags);
1767 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1768 if (!(r8a66597->timeout_map & (1 << pipenum)))
1770 if (timer_pending(&r8a66597->td_timer[pipenum]))
1773 td = r8a66597_get_td(r8a66597, pipenum);
1775 r8a66597->timeout_map &= ~(1 << pipenum);
1779 if (td->urb->actual_length) {
1780 set_td_timer(r8a66597, td);
1785 pipe_stop(r8a66597, pipe);
1789 list_move_tail(&new_td->queue,
1790 &r8a66597->pipe_queue[pipenum]);
1791 new_td = r8a66597_get_td(r8a66597, pipenum);
1796 } while (td != new_td && td->address == new_td->address);
1798 start_transfer(r8a66597, new_td);
1801 r8a66597->timeout_map &= ~(1 << pipenum);
1803 set_td_timer(r8a66597, new_td);
1806 spin_unlock_irqrestore(&r8a66597->lock, flags);
1809 static void r8a66597_timer(unsigned long _r8a66597)
1811 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1812 unsigned long flags;
1815 spin_lock_irqsave(&r8a66597->lock, flags);
1817 for (port = 0; port < r8a66597->max_root_hub; port++)
1818 r8a66597_root_hub_control(r8a66597, port);
1820 spin_unlock_irqrestore(&r8a66597->lock, flags);
1823 static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1825 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1827 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1828 (urb->dev->state == USB_STATE_CONFIGURED))
1834 static int r8a66597_start(struct usb_hcd *hcd)
1836 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1838 hcd->state = HC_STATE_RUNNING;
1839 return enable_controller(r8a66597);
1842 static void r8a66597_stop(struct usb_hcd *hcd)
1844 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1846 disable_controller(r8a66597);
1849 static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1851 unsigned int usb_address = usb_pipedevice(urb->pipe);
1852 u16 root_port, hub_port;
1854 if (usb_address == 0) {
1855 get_port_number(r8a66597, urb->dev->devpath,
1856 &root_port, &hub_port);
1857 set_devadd_reg(r8a66597, 0,
1858 get_r8a66597_usb_speed(urb->dev->speed),
1859 get_parent_r8a66597_address(r8a66597, urb->dev),
1860 hub_port, root_port);
1864 static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1866 struct usb_host_endpoint *hep)
1868 struct r8a66597_td *td;
1871 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
1875 pipenum = r8a66597_get_pipenum(urb, hep);
1876 td->pipenum = pipenum;
1877 td->pipe = hep->hcpriv;
1879 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1880 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1881 !usb_pipein(urb->pipe));
1882 if (usb_pipecontrol(urb->pipe))
1883 td->type = USB_PID_SETUP;
1884 else if (usb_pipein(urb->pipe))
1885 td->type = USB_PID_IN;
1887 td->type = USB_PID_OUT;
1888 INIT_LIST_HEAD(&td->queue);
1893 static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
1897 struct usb_host_endpoint *hep = urb->ep;
1898 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1899 struct r8a66597_td *td = NULL;
1900 int ret, request = 0;
1901 unsigned long flags;
1903 spin_lock_irqsave(&r8a66597->lock, flags);
1904 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1906 goto error_not_linked;
1909 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1911 goto error_not_linked;
1914 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1920 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1921 if (usb_pipeendpoint(urb->pipe))
1922 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1925 if (unlikely(check_pipe_config(r8a66597, urb)))
1926 init_pipe_config(r8a66597, urb);
1928 set_address_zero(r8a66597, urb);
1929 td = r8a66597_make_td(r8a66597, urb, hep);
1934 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1936 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
1940 if (td->pipe->info.timer_interval) {
1941 r8a66597->interval_map |= 1 << td->pipenum;
1942 mod_timer(&r8a66597->interval_timer[td->pipenum],
1943 jiffies + msecs_to_jiffies(
1944 td->pipe->info.timer_interval));
1946 ret = start_transfer(r8a66597, td);
1948 list_del(&td->queue);
1953 set_td_timer(r8a66597, td);
1957 usb_hcd_unlink_urb_from_ep(hcd, urb);
1959 spin_unlock_irqrestore(&r8a66597->lock, flags);
1963 static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1966 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1967 struct r8a66597_td *td;
1968 unsigned long flags;
1971 spin_lock_irqsave(&r8a66597->lock, flags);
1972 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1978 pipe_stop(r8a66597, td->pipe);
1979 pipe_irq_disable(r8a66597, td->pipenum);
1980 disable_irq_empty(r8a66597, td->pipenum);
1981 finish_request(r8a66597, td, td->pipenum, urb, status);
1984 spin_unlock_irqrestore(&r8a66597->lock, flags);
1988 static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1989 struct usb_host_endpoint *hep)
1991 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1992 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1993 struct r8a66597_td *td;
1994 struct urb *urb = NULL;
1996 unsigned long flags;
2000 pipenum = pipe->info.pipenum;
2008 spin_lock_irqsave(&r8a66597->lock, flags);
2009 pipe_stop(r8a66597, pipe);
2010 pipe_irq_disable(r8a66597, pipenum);
2011 disable_irq_empty(r8a66597, pipenum);
2012 td = r8a66597_get_td(r8a66597, pipenum);
2015 finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
2018 spin_unlock_irqrestore(&r8a66597->lock, flags);
2021 static int r8a66597_get_frame(struct usb_hcd *hcd)
2023 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2024 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
2027 static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
2031 if (udev->state == USB_STATE_CONFIGURED &&
2032 udev->parent && udev->parent->devnum > 1 &&
2033 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
2034 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
2036 for (chix = 0; chix < udev->maxchild; chix++) {
2037 struct usb_device *childdev = udev->children[chix];
2040 collect_usb_address_map(childdev, map);
2044 /* this function must be called with interrupt disabled */
2045 static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
2048 struct r8a66597_device *dev;
2049 struct list_head *list = &r8a66597->child_device;
2051 list_for_each_entry(dev, list, device_list) {
2052 if (dev->usb_address != addr)
2058 printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
2062 static void update_usb_address_map(struct r8a66597 *r8a66597,
2063 struct usb_device *root_hub,
2068 unsigned long flags;
2070 for (i = 0; i < 4; i++) {
2071 diff = r8a66597->child_connect_map[i] ^ map[i];
2075 for (j = 0; j < 32; j++) {
2076 if (!(diff & (1 << j)))
2080 if (map[i] & (1 << j))
2081 set_child_connect_map(r8a66597, addr);
2083 struct r8a66597_device *dev;
2085 spin_lock_irqsave(&r8a66597->lock, flags);
2086 dev = get_r8a66597_device(r8a66597, addr);
2087 disable_r8a66597_pipe_all(r8a66597, dev);
2088 free_usb_address(r8a66597, dev);
2089 put_child_connect_map(r8a66597, addr);
2090 spin_unlock_irqrestore(&r8a66597->lock, flags);
2096 static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
2097 struct usb_hcd *hcd)
2099 struct usb_bus *bus;
2100 unsigned long now_map[4];
2102 memset(now_map, 0, sizeof(now_map));
2104 list_for_each_entry(bus, &usb_bus_list, bus_list) {
2108 if (bus->busnum != hcd->self.busnum)
2111 collect_usb_address_map(bus->root_hub, now_map);
2112 update_usb_address_map(r8a66597, bus->root_hub, now_map);
2116 static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2118 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2119 unsigned long flags;
2122 r8a66597_check_detect_child(r8a66597, hcd);
2124 spin_lock_irqsave(&r8a66597->lock, flags);
2126 *buf = 0; /* initialize (no change) */
2128 for (i = 0; i < r8a66597->max_root_hub; i++) {
2129 if (r8a66597->root_hub[i].port & 0xffff0000)
2130 *buf |= 1 << (i + 1);
2133 spin_unlock_irqrestore(&r8a66597->lock, flags);
2138 static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2139 struct usb_hub_descriptor *desc)
2141 desc->bDescriptorType = 0x29;
2142 desc->bHubContrCurrent = 0;
2143 desc->bNbrPorts = r8a66597->max_root_hub;
2144 desc->bDescLength = 9;
2145 desc->bPwrOn2PwrGood = 0;
2146 desc->wHubCharacteristics = cpu_to_le16(0x0011);
2147 desc->bitmap[0] = ((1 << r8a66597->max_root_hub) - 1) << 1;
2148 desc->bitmap[1] = ~0;
2151 static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2152 u16 wIndex, char *buf, u16 wLength)
2154 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2156 int port = (wIndex & 0x00FF) - 1;
2157 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2158 unsigned long flags;
2162 spin_lock_irqsave(&r8a66597->lock, flags);
2164 case ClearHubFeature:
2167 case C_HUB_OVER_CURRENT:
2168 case C_HUB_LOCAL_POWER:
2174 case ClearPortFeature:
2175 if (wIndex > r8a66597->max_root_hub)
2181 case USB_PORT_FEAT_ENABLE:
2182 rh->port &= ~(1 << USB_PORT_FEAT_POWER);
2184 case USB_PORT_FEAT_SUSPEND:
2186 case USB_PORT_FEAT_POWER:
2187 r8a66597_port_power(r8a66597, port, 0);
2189 case USB_PORT_FEAT_C_ENABLE:
2190 case USB_PORT_FEAT_C_SUSPEND:
2191 case USB_PORT_FEAT_C_CONNECTION:
2192 case USB_PORT_FEAT_C_OVER_CURRENT:
2193 case USB_PORT_FEAT_C_RESET:
2198 rh->port &= ~(1 << wValue);
2200 case GetHubDescriptor:
2201 r8a66597_hub_descriptor(r8a66597,
2202 (struct usb_hub_descriptor *)buf);
2208 if (wIndex > r8a66597->max_root_hub)
2210 *(__le32 *)buf = cpu_to_le32(rh->port);
2212 case SetPortFeature:
2213 if (wIndex > r8a66597->max_root_hub)
2219 case USB_PORT_FEAT_SUSPEND:
2221 case USB_PORT_FEAT_POWER:
2222 r8a66597_port_power(r8a66597, port, 1);
2223 rh->port |= (1 << USB_PORT_FEAT_POWER);
2225 case USB_PORT_FEAT_RESET: {
2226 struct r8a66597_device *dev = rh->dev;
2228 rh->port |= (1 << USB_PORT_FEAT_RESET);
2230 disable_r8a66597_pipe_all(r8a66597, dev);
2231 free_usb_address(r8a66597, dev);
2233 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2234 get_dvstctr_reg(port));
2235 mod_timer(&r8a66597->rh_timer,
2236 jiffies + msecs_to_jiffies(50));
2242 rh->port |= 1 << wValue;
2250 spin_unlock_irqrestore(&r8a66597->lock, flags);
2254 #if defined(CONFIG_PM)
2255 static int r8a66597_bus_suspend(struct usb_hcd *hcd)
2257 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2260 dbg("%s", __func__);
2262 for (port = 0; port < r8a66597->max_root_hub; port++) {
2263 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2264 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2266 if (!(rh->port & (1 << USB_PORT_FEAT_ENABLE)))
2269 dbg("suspend port = %d", port);
2270 r8a66597_bclr(r8a66597, UACT, dvstctr_reg); /* suspend */
2271 rh->port |= 1 << USB_PORT_FEAT_SUSPEND;
2273 if (rh->dev->udev->do_remote_wakeup) {
2274 msleep(3); /* waiting last SOF */
2275 r8a66597_bset(r8a66597, RWUPE, dvstctr_reg);
2276 r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
2277 r8a66597_bset(r8a66597, BCHGE, get_intenb_reg(port));
2281 r8a66597->bus_suspended = 1;
2286 static int r8a66597_bus_resume(struct usb_hcd *hcd)
2288 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2291 dbg("%s", __func__);
2293 for (port = 0; port < r8a66597->max_root_hub; port++) {
2294 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2295 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2297 if (!(rh->port & (1 << USB_PORT_FEAT_SUSPEND)))
2300 dbg("resume port = %d", port);
2301 rh->port &= ~(1 << USB_PORT_FEAT_SUSPEND);
2302 rh->port |= 1 << USB_PORT_FEAT_C_SUSPEND;
2303 r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
2305 r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
2312 #define r8a66597_bus_suspend NULL
2313 #define r8a66597_bus_resume NULL
2316 static struct hc_driver r8a66597_hc_driver = {
2317 .description = hcd_name,
2318 .hcd_priv_size = sizeof(struct r8a66597),
2319 .irq = r8a66597_irq,
2322 * generic hardware linkage
2326 .start = r8a66597_start,
2327 .stop = r8a66597_stop,
2330 * managing i/o requests and associated device resources
2332 .urb_enqueue = r8a66597_urb_enqueue,
2333 .urb_dequeue = r8a66597_urb_dequeue,
2334 .endpoint_disable = r8a66597_endpoint_disable,
2337 * periodic schedule support
2339 .get_frame_number = r8a66597_get_frame,
2344 .hub_status_data = r8a66597_hub_status_data,
2345 .hub_control = r8a66597_hub_control,
2346 .bus_suspend = r8a66597_bus_suspend,
2347 .bus_resume = r8a66597_bus_resume,
2350 #if defined(CONFIG_PM)
2351 static int r8a66597_suspend(struct device *dev)
2353 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
2356 dbg("%s", __func__);
2358 disable_controller(r8a66597);
2360 for (port = 0; port < r8a66597->max_root_hub; port++) {
2361 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2363 rh->port = 0x00000000;
2369 static int r8a66597_resume(struct device *dev)
2371 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
2372 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2374 dbg("%s", __func__);
2376 enable_controller(r8a66597);
2377 usb_root_hub_lost_power(hcd->self.root_hub);
2382 static const struct dev_pm_ops r8a66597_dev_pm_ops = {
2383 .suspend = r8a66597_suspend,
2384 .resume = r8a66597_resume,
2385 .poweroff = r8a66597_suspend,
2386 .restore = r8a66597_resume,
2389 #define R8A66597_DEV_PM_OPS (&r8a66597_dev_pm_ops)
2390 #else /* if defined(CONFIG_PM) */
2391 #define R8A66597_DEV_PM_OPS NULL
2394 static int __init_or_module r8a66597_remove(struct platform_device *pdev)
2396 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2397 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2399 del_timer_sync(&r8a66597->rh_timer);
2400 usb_remove_hcd(hcd);
2401 iounmap((void *)r8a66597->reg);
2402 #ifdef CONFIG_HAVE_CLK
2403 if (r8a66597->pdata->on_chip)
2404 clk_put(r8a66597->clk);
2410 static int __devinit r8a66597_probe(struct platform_device *pdev)
2412 #ifdef CONFIG_HAVE_CLK
2415 struct resource *res = NULL, *ires;
2417 void __iomem *reg = NULL;
2418 struct usb_hcd *hcd = NULL;
2419 struct r8a66597 *r8a66597;
2422 unsigned long irq_trigger;
2424 if (pdev->dev.dma_mask) {
2426 dev_err(&pdev->dev, "dma not supported\n");
2430 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2433 dev_err(&pdev->dev, "platform_get_resource error.\n");
2437 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2441 "platform_get_resource IORESOURCE_IRQ error.\n");
2446 irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
2448 reg = ioremap(res->start, resource_size(res));
2451 dev_err(&pdev->dev, "ioremap error.\n");
2455 if (pdev->dev.platform_data == NULL) {
2456 dev_err(&pdev->dev, "no platform data\n");
2461 /* initialize hcd */
2462 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2465 dev_err(&pdev->dev, "Failed to create hcd\n");
2468 r8a66597 = hcd_to_r8a66597(hcd);
2469 memset(r8a66597, 0, sizeof(struct r8a66597));
2470 dev_set_drvdata(&pdev->dev, r8a66597);
2471 r8a66597->pdata = pdev->dev.platform_data;
2472 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
2474 if (r8a66597->pdata->on_chip) {
2475 #ifdef CONFIG_HAVE_CLK
2476 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
2477 r8a66597->clk = clk_get(&pdev->dev, clk_name);
2478 if (IS_ERR(r8a66597->clk)) {
2479 dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
2481 ret = PTR_ERR(r8a66597->clk);
2485 r8a66597->max_root_hub = 1;
2487 r8a66597->max_root_hub = 2;
2489 spin_lock_init(&r8a66597->lock);
2490 init_timer(&r8a66597->rh_timer);
2491 r8a66597->rh_timer.function = r8a66597_timer;
2492 r8a66597->rh_timer.data = (unsigned long)r8a66597;
2493 r8a66597->reg = (unsigned long)reg;
2495 /* make sure no interrupts are pending */
2496 ret = r8a66597_clock_enable(r8a66597);
2499 disable_controller(r8a66597);
2501 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2502 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2503 init_timer(&r8a66597->td_timer[i]);
2504 r8a66597->td_timer[i].function = r8a66597_td_timer;
2505 r8a66597->td_timer[i].data = (unsigned long)r8a66597;
2506 setup_timer(&r8a66597->interval_timer[i],
2507 r8a66597_interval_timer,
2508 (unsigned long)r8a66597);
2510 INIT_LIST_HEAD(&r8a66597->child_device);
2512 hcd->rsrc_start = res->start;
2514 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger);
2516 dev_err(&pdev->dev, "Failed to add hcd\n");
2523 #ifdef CONFIG_HAVE_CLK
2524 if (r8a66597->pdata->on_chip)
2525 clk_put(r8a66597->clk);
2537 static struct platform_driver r8a66597_driver = {
2538 .probe = r8a66597_probe,
2539 .remove = r8a66597_remove,
2541 .name = (char *) hcd_name,
2542 .owner = THIS_MODULE,
2543 .pm = R8A66597_DEV_PM_OPS,
2547 static int __init r8a66597_init(void)
2552 printk(KERN_INFO KBUILD_MODNAME ": driver %s, %s\n", hcd_name,
2554 return platform_driver_register(&r8a66597_driver);
2556 module_init(r8a66597_init);
2558 static void __exit r8a66597_cleanup(void)
2560 platform_driver_unregister(&r8a66597_driver);
2562 module_exit(r8a66597_cleanup);