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 "2009-05-26"
51 static const char hcd_name[] = "r8a66597_hcd";
53 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
54 static int r8a66597_get_frame(struct usb_hcd *hcd);
56 /* this function must be called with interrupt disabled */
57 static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
62 tmp = r8a66597_read(r8a66597, INTENB0);
63 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
64 r8a66597_bset(r8a66597, 1 << pipenum, reg);
65 r8a66597_write(r8a66597, tmp, INTENB0);
68 /* this function must be called with interrupt disabled */
69 static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
74 tmp = r8a66597_read(r8a66597, INTENB0);
75 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
76 r8a66597_bclr(r8a66597, 1 << pipenum, reg);
77 r8a66597_write(r8a66597, tmp, INTENB0);
80 static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
81 u16 usbspd, u8 upphub, u8 hubport, int port)
84 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
86 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
87 r8a66597_write(r8a66597, val, devadd_reg);
90 static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
95 #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
96 #if defined(CONFIG_HAVE_CLK)
97 clk_enable(r8a66597->clk);
100 r8a66597_write(r8a66597, SCKE, SYSCFG0);
101 tmp = r8a66597_read(r8a66597, SYSCFG0);
103 printk(KERN_ERR "r8a66597: register access fail.\n");
106 } while ((tmp & SCKE) != SCKE);
107 r8a66597_write(r8a66597, 0x04, 0x02);
110 r8a66597_write(r8a66597, USBE, SYSCFG0);
111 tmp = r8a66597_read(r8a66597, SYSCFG0);
113 printk(KERN_ERR "r8a66597: register access fail.\n");
116 } while ((tmp & USBE) != USBE);
117 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
118 r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata), XTAL,
122 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
125 tmp = r8a66597_read(r8a66597, SYSCFG0);
127 printk(KERN_ERR "r8a66597: register access fail.\n");
130 } while ((tmp & SCKE) != SCKE);
131 #endif /* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */
136 static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
138 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
140 #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
141 #if defined(CONFIG_HAVE_CLK)
142 clk_disable(r8a66597->clk);
145 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
146 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
147 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
151 static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
155 val = port ? DRPD : DCFM | DRPD;
156 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
157 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
159 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
160 r8a66597_bclr(r8a66597, DTCHE, get_intenb_reg(port));
161 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
164 static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
168 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
169 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
171 r8a66597_port_power(r8a66597, port, 0);
174 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
176 } while (tmp == EDGESTS);
178 val = port ? DRPD : DCFM | DRPD;
179 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
180 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
183 static int enable_controller(struct r8a66597 *r8a66597)
186 u16 vif = r8a66597->pdata->vif ? LDRV : 0;
187 u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
188 u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
190 ret = r8a66597_clock_enable(r8a66597);
194 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
195 r8a66597_bset(r8a66597, USBE, SYSCFG0);
197 r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
198 r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
199 r8a66597_bset(r8a66597, BRDY0, BRDYENB);
200 r8a66597_bset(r8a66597, BEMP0, BEMPENB);
202 r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
203 r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
204 r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
205 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
207 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
209 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
210 r8a66597_enable_port(r8a66597, port);
215 static void disable_controller(struct r8a66597 *r8a66597)
219 r8a66597_write(r8a66597, 0, INTENB0);
220 r8a66597_write(r8a66597, 0, INTSTS0);
222 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
223 r8a66597_disable_port(r8a66597, port);
225 r8a66597_clock_disable(r8a66597);
228 static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
229 struct usb_device *udev)
231 struct r8a66597_device *dev;
233 if (udev->parent && udev->parent->devnum != 1)
236 dev = dev_get_drvdata(&udev->dev);
243 static int is_child_device(char *devpath)
245 return (devpath[2] ? 1 : 0);
248 static int is_hub_limit(char *devpath)
250 return ((strlen(devpath) >= 4) ? 1 : 0);
253 static void get_port_number(char *devpath, u16 *root_port, u16 *hub_port)
256 *root_port = (devpath[0] & 0x0F) - 1;
257 if (*root_port >= R8A66597_MAX_ROOT_HUB)
258 printk(KERN_ERR "r8a66597: Illegal root port number.\n");
261 *hub_port = devpath[2] & 0x0F;
264 static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
279 printk(KERN_ERR "r8a66597: unknown speed\n");
286 static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
291 r8a66597->child_connect_map[idx] |= 1 << (address % 32);
294 static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
299 r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
302 static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
304 u16 pipenum = pipe->info.pipenum;
305 const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
306 const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
307 const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
309 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
310 dma_ch = R8A66597_PIPE_NO_DMA;
312 pipe->fifoaddr = fifoaddr[dma_ch];
313 pipe->fifosel = fifosel[dma_ch];
314 pipe->fifoctr = fifoctr[dma_ch];
317 pipe->pipectr = DCPCTR;
319 pipe->pipectr = get_pipectr_addr(pipenum);
321 if (check_bulk_or_isoc(pipenum)) {
322 pipe->pipetre = get_pipetre_addr(pipenum);
323 pipe->pipetrn = get_pipetrn_addr(pipenum);
330 static struct r8a66597_device *
331 get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
333 if (usb_pipedevice(urb->pipe) == 0)
334 return &r8a66597->device0;
336 return dev_get_drvdata(&urb->dev->dev);
339 static int make_r8a66597_device(struct r8a66597 *r8a66597,
340 struct urb *urb, u8 addr)
342 struct r8a66597_device *dev;
343 int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
345 dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
349 dev_set_drvdata(&urb->dev->dev, dev);
350 dev->udev = urb->dev;
352 dev->usb_address = usb_address;
353 dev->state = USB_STATE_ADDRESS;
354 dev->ep_in_toggle = 0;
355 dev->ep_out_toggle = 0;
356 INIT_LIST_HEAD(&dev->device_list);
357 list_add_tail(&dev->device_list, &r8a66597->child_device);
359 get_port_number(urb->dev->devpath, &dev->root_port, &dev->hub_port);
360 if (!is_child_device(urb->dev->devpath))
361 r8a66597->root_hub[dev->root_port].dev = dev;
363 set_devadd_reg(r8a66597, dev->address,
364 get_r8a66597_usb_speed(urb->dev->speed),
365 get_parent_r8a66597_address(r8a66597, urb->dev),
366 dev->hub_port, dev->root_port);
371 /* this function must be called with interrupt disabled */
372 static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
374 u8 addr; /* R8A66597's address */
375 struct r8a66597_device *dev;
377 if (is_hub_limit(urb->dev->devpath)) {
378 dev_err(&urb->dev->dev, "External hub limit reached.\n");
382 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
383 if (dev && dev->state >= USB_STATE_ADDRESS)
386 for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
387 if (r8a66597->address_map & (1 << addr))
390 dbg("alloc_address: r8a66597_addr=%d", addr);
391 r8a66597->address_map |= 1 << addr;
393 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
399 dev_err(&urb->dev->dev,
400 "cannot communicate with a USB device more than 10.(%x)\n",
401 r8a66597->address_map);
406 /* this function must be called with interrupt disabled */
407 static void free_usb_address(struct r8a66597 *r8a66597,
408 struct r8a66597_device *dev)
415 dbg("free_addr: addr=%d", dev->address);
417 dev->state = USB_STATE_DEFAULT;
418 r8a66597->address_map &= ~(1 << dev->address);
420 dev_set_drvdata(&dev->udev->dev, NULL);
421 list_del(&dev->device_list);
424 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
425 if (r8a66597->root_hub[port].dev == dev) {
426 r8a66597->root_hub[port].dev = NULL;
432 static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
439 tmp = r8a66597_read(r8a66597, reg);
441 printk(KERN_ERR "r8a66597: register%lx, loop %x "
442 "is timeout\n", reg, loop);
446 } while ((tmp & mask) != loop);
449 /* this function must be called with interrupt disabled */
450 static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
454 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
455 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
456 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
457 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
460 /* this function must be called with interrupt disabled */
461 static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
465 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
466 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
467 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
468 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
469 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
472 /* this function must be called with interrupt disabled */
473 static void clear_all_buffer(struct r8a66597 *r8a66597,
474 struct r8a66597_pipe *pipe)
478 if (!pipe || pipe->info.pipenum == 0)
481 pipe_stop(r8a66597, pipe);
482 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
483 tmp = r8a66597_read(r8a66597, pipe->pipectr);
484 tmp = r8a66597_read(r8a66597, pipe->pipectr);
485 tmp = r8a66597_read(r8a66597, pipe->pipectr);
486 r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
489 /* this function must be called with interrupt disabled */
490 static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
491 struct r8a66597_pipe *pipe, int toggle)
494 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
496 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
499 /* this function must be called with interrupt disabled */
500 static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
502 r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
503 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
506 /* this function must be called with interrupt disabled */
507 static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
508 struct r8a66597_pipe *pipe)
510 cfifo_change(r8a66597, 0);
511 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D0FIFOSEL);
512 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D1FIFOSEL);
514 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum, MBW | CURPIPE,
516 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
519 static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
521 struct r8a66597_pipe *pipe = hep->hcpriv;
523 if (usb_pipeendpoint(urb->pipe) == 0)
526 return pipe->info.pipenum;
529 static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
531 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
533 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
536 static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
542 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
545 /* this function must be called with interrupt disabled */
546 static void pipe_toggle_set(struct r8a66597 *r8a66597,
547 struct r8a66597_pipe *pipe,
548 struct urb *urb, int set)
550 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
551 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
552 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
558 *toggle |= 1 << endpoint;
560 *toggle &= ~(1 << endpoint);
563 /* this function must be called with interrupt disabled */
564 static void pipe_toggle_save(struct r8a66597 *r8a66597,
565 struct r8a66597_pipe *pipe,
568 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
569 pipe_toggle_set(r8a66597, pipe, urb, 1);
571 pipe_toggle_set(r8a66597, pipe, urb, 0);
574 /* this function must be called with interrupt disabled */
575 static void pipe_toggle_restore(struct r8a66597 *r8a66597,
576 struct r8a66597_pipe *pipe,
579 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
580 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
581 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
586 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
589 /* this function must be called with interrupt disabled */
590 static void pipe_buffer_setting(struct r8a66597 *r8a66597,
591 struct r8a66597_pipe_info *info)
595 if (info->pipenum == 0)
598 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
599 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
600 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
603 if (info->type == R8A66597_BULK && info->dir_in)
604 val |= R8A66597_DBLB | R8A66597_SHTNAK;
605 val |= info->type | info->epnum;
606 r8a66597_write(r8a66597, val, PIPECFG);
608 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
610 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
612 r8a66597_write(r8a66597, info->interval, PIPEPERI);
615 /* this function must be called with interrupt disabled */
616 static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
618 struct r8a66597_pipe_info *info;
619 struct urb *urb = td->urb;
621 if (td->pipenum > 0) {
622 info = &td->pipe->info;
623 cfifo_change(r8a66597, 0);
624 pipe_buffer_setting(r8a66597, info);
626 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
627 usb_pipeout(urb->pipe)) &&
628 !usb_pipecontrol(urb->pipe)) {
629 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
630 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
631 clear_all_buffer(r8a66597, td->pipe);
632 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
633 usb_pipeout(urb->pipe), 1);
635 pipe_toggle_restore(r8a66597, td->pipe, urb);
639 /* this function must be called with interrupt disabled */
640 static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
641 struct usb_endpoint_descriptor *ep)
643 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
645 memset(array, 0, sizeof(array));
646 switch (usb_endpoint_type(ep)) {
647 case USB_ENDPOINT_XFER_BULK:
648 if (usb_endpoint_dir_in(ep))
655 case USB_ENDPOINT_XFER_INT:
656 if (usb_endpoint_dir_in(ep)) {
663 case USB_ENDPOINT_XFER_ISOC:
664 if (usb_endpoint_dir_in(ep))
670 printk(KERN_ERR "r8a66597: Illegal type\n");
676 while (array[i] != 0) {
677 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
685 static u16 get_r8a66597_type(__u8 type)
690 case USB_ENDPOINT_XFER_BULK:
691 r8a66597_type = R8A66597_BULK;
693 case USB_ENDPOINT_XFER_INT:
694 r8a66597_type = R8A66597_INT;
696 case USB_ENDPOINT_XFER_ISOC:
697 r8a66597_type = R8A66597_ISO;
700 printk(KERN_ERR "r8a66597: Illegal type\n");
701 r8a66597_type = 0x0000;
705 return r8a66597_type;
708 static u16 get_bufnum(u16 pipenum)
714 else if (check_bulk_or_isoc(pipenum))
715 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
716 else if (check_interrupt(pipenum))
717 bufnum = 4 + (pipenum - 6);
719 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
724 static u16 get_buf_bsize(u16 pipenum)
730 else if (check_bulk_or_isoc(pipenum))
731 buf_bsize = R8A66597_BUF_BSIZE - 1;
732 else if (check_interrupt(pipenum))
735 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
740 /* this function must be called with interrupt disabled */
741 static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
742 struct r8a66597_device *dev,
743 struct r8a66597_pipe *pipe,
746 #if !defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
748 struct r8a66597_pipe_info *info = &pipe->info;
750 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
751 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
752 if ((r8a66597->dma_map & (1 << i)) != 0)
755 dev_info(&dev->udev->dev,
756 "address %d, EndpointAddress 0x%02x use "
757 "DMA FIFO\n", usb_pipedevice(urb->pipe),
759 USB_ENDPOINT_DIR_MASK + info->epnum
762 r8a66597->dma_map |= 1 << i;
763 dev->dma_map |= 1 << i;
764 set_pipe_reg_addr(pipe, i);
766 cfifo_change(r8a66597, 0);
767 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum,
768 MBW | CURPIPE, pipe->fifosel);
770 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
772 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
776 #endif /* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */
779 /* this function must be called with interrupt disabled */
780 static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
781 struct usb_host_endpoint *hep,
782 struct r8a66597_pipe_info *info)
784 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
785 struct r8a66597_pipe *pipe = hep->hcpriv;
790 set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
791 r8a66597->pipe_cnt[pipe->info.pipenum]++;
792 dev->pipe_cnt[pipe->info.pipenum]++;
794 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
797 /* this function must be called with interrupt disabled */
798 static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
800 struct r8a66597_td *td, *next;
802 struct list_head *list = &r8a66597->pipe_queue[pipenum];
804 if (list_empty(list))
807 list_for_each_entry_safe(td, next, list, queue) {
810 if (td->address != address)
814 list_del(&td->queue);
818 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597),
821 spin_unlock(&r8a66597->lock);
822 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb,
824 spin_lock(&r8a66597->lock);
830 /* this function must be called with interrupt disabled */
831 static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
832 struct r8a66597_device *dev)
840 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
841 if (!dev->pipe_cnt[pipenum])
846 force_dequeue(r8a66597, 0, dev->address);
849 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
850 dev->pipe_cnt[pipenum] = 0;
851 force_dequeue(r8a66597, pipenum, dev->address);
856 r8a66597->dma_map &= ~(dev->dma_map);
860 static u16 get_interval(struct urb *urb, __u8 interval)
865 if (urb->dev->speed == USB_SPEED_HIGH) {
869 time = interval ? interval - 1 : 0;
871 if (interval > 128) {
874 /* calculate the nearest value for PIPEPERI */
875 for (i = 0; i < 7; i++) {
876 if ((1 << i) < interval &&
877 (1 << (i + 1) > interval))
886 static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
889 unsigned long time = 1;
891 if (usb_pipeisoc(urb->pipe))
894 if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
895 for (i = 0; i < (interval - 1); i++)
897 time = time * 125 / 1000; /* uSOF -> msec */
905 /* this function must be called with interrupt disabled */
906 static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
907 struct usb_host_endpoint *hep,
908 struct usb_endpoint_descriptor *ep)
910 struct r8a66597_pipe_info info;
912 info.pipenum = get_empty_pipenum(r8a66597, ep);
913 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
914 info.epnum = usb_endpoint_num(ep);
915 info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
916 info.type = get_r8a66597_type(usb_endpoint_type(ep));
917 info.bufnum = get_bufnum(info.pipenum);
918 info.buf_bsize = get_buf_bsize(info.pipenum);
919 if (info.type == R8A66597_BULK) {
921 info.timer_interval = 0;
923 info.interval = get_interval(urb, ep->bInterval);
924 info.timer_interval = get_timer_interval(urb, ep->bInterval);
926 if (usb_endpoint_dir_in(ep))
931 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
934 static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
936 struct r8a66597_device *dev;
938 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
939 dev->state = USB_STATE_CONFIGURED;
942 static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
945 if (pipenum == 0 && usb_pipeout(urb->pipe))
946 enable_irq_empty(r8a66597, pipenum);
948 enable_irq_ready(r8a66597, pipenum);
950 if (!usb_pipeisoc(urb->pipe))
951 enable_irq_nrdy(r8a66597, pipenum);
954 static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
956 disable_irq_ready(r8a66597, pipenum);
957 disable_irq_nrdy(r8a66597, pipenum);
960 static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
962 mod_timer(&r8a66597->rh_timer,
963 jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
966 static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
969 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
971 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
972 rh->scount = R8A66597_MAX_SAMPLING;
974 rh->port |= 1 << USB_PORT_FEAT_CONNECTION;
976 rh->port &= ~(1 << USB_PORT_FEAT_CONNECTION);
977 rh->port |= 1 << USB_PORT_FEAT_C_CONNECTION;
979 r8a66597_root_hub_start_polling(r8a66597);
982 /* this function must be called with interrupt disabled */
983 static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
987 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
988 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
992 if (syssts == FS_JSTS)
993 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
994 else if (syssts == LS_JSTS)
995 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
997 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
998 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
1000 if (r8a66597->bus_suspended)
1001 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1004 /* this function must be called with interrupt disabled */
1005 static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
1007 u16 speed = get_rh_usb_speed(r8a66597, port);
1008 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1010 if (speed == HSMODE)
1011 rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
1012 else if (speed == LSMODE)
1013 rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
1015 rh->port &= ~(1 << USB_PORT_FEAT_RESET);
1016 rh->port |= 1 << USB_PORT_FEAT_ENABLE;
1019 /* this function must be called with interrupt disabled */
1020 static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
1022 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
1024 disable_r8a66597_pipe_all(r8a66597, dev);
1025 free_usb_address(r8a66597, dev);
1027 start_root_hub_sampling(r8a66597, port, 0);
1030 /* this function must be called with interrupt disabled */
1031 static void prepare_setup_packet(struct r8a66597 *r8a66597,
1032 struct r8a66597_td *td)
1035 __le16 *p = (__le16 *)td->urb->setup_packet;
1036 unsigned long setup_addr = USBREQ;
1038 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1040 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
1042 for (i = 0; i < 4; i++) {
1043 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
1046 r8a66597_write(r8a66597, SUREQ, DCPCTR);
1049 /* this function must be called with interrupt disabled */
1050 static void prepare_packet_read(struct r8a66597 *r8a66597,
1051 struct r8a66597_td *td)
1053 struct urb *urb = td->urb;
1055 if (usb_pipecontrol(urb->pipe)) {
1056 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1057 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1058 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1059 if (urb->actual_length == 0) {
1060 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1061 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1063 pipe_irq_disable(r8a66597, td->pipenum);
1064 pipe_start(r8a66597, td->pipe);
1065 pipe_irq_enable(r8a66597, urb, td->pipenum);
1067 if (urb->actual_length == 0) {
1068 pipe_irq_disable(r8a66597, td->pipenum);
1069 pipe_setting(r8a66597, td);
1070 pipe_stop(r8a66597, td->pipe);
1071 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1073 if (td->pipe->pipetre) {
1074 r8a66597_write(r8a66597, TRCLR,
1076 r8a66597_write(r8a66597,
1078 (urb->transfer_buffer_length,
1081 r8a66597_bset(r8a66597, TRENB,
1085 pipe_start(r8a66597, td->pipe);
1086 pipe_irq_enable(r8a66597, urb, td->pipenum);
1091 /* this function must be called with interrupt disabled */
1092 static void prepare_packet_write(struct r8a66597 *r8a66597,
1093 struct r8a66597_td *td)
1096 struct urb *urb = td->urb;
1098 if (usb_pipecontrol(urb->pipe)) {
1099 pipe_stop(r8a66597, td->pipe);
1100 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1101 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1102 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1103 if (urb->actual_length == 0) {
1104 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1105 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1108 if (urb->actual_length == 0)
1109 pipe_setting(r8a66597, td);
1110 if (td->pipe->pipetre)
1111 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1113 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1115 fifo_change_from_pipe(r8a66597, td->pipe);
1116 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1117 if (unlikely((tmp & FRDY) == 0))
1118 pipe_irq_enable(r8a66597, urb, td->pipenum);
1120 packet_write(r8a66597, td->pipenum);
1121 pipe_start(r8a66597, td->pipe);
1124 /* this function must be called with interrupt disabled */
1125 static void prepare_status_packet(struct r8a66597 *r8a66597,
1126 struct r8a66597_td *td)
1128 struct urb *urb = td->urb;
1130 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1131 pipe_stop(r8a66597, td->pipe);
1133 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1134 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1135 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1136 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1137 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
1138 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
1139 enable_irq_empty(r8a66597, 0);
1141 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1142 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1143 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1144 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1145 enable_irq_ready(r8a66597, 0);
1147 enable_irq_nrdy(r8a66597, 0);
1148 pipe_start(r8a66597, td->pipe);
1151 static int is_set_address(unsigned char *setup_packet)
1153 if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1154 setup_packet[1] == USB_REQ_SET_ADDRESS)
1160 /* this function must be called with interrupt disabled */
1161 static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1167 if (is_set_address(td->urb->setup_packet)) {
1168 td->set_address = 1;
1169 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1171 if (td->urb->setup_packet[2] == 0)
1174 prepare_setup_packet(r8a66597, td);
1177 prepare_packet_read(r8a66597, td);
1180 prepare_packet_write(r8a66597, td);
1183 prepare_status_packet(r8a66597, td);
1186 printk(KERN_ERR "r8a66597: invalid type.\n");
1193 static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1195 if (usb_pipeisoc(urb->pipe)) {
1196 if (urb->number_of_packets == td->iso_cnt)
1200 /* control or bulk or interrupt */
1201 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1202 (td->short_packet) || (td->zero_packet))
1208 /* this function must be called with interrupt disabled */
1209 static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1215 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1216 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1217 r8a66597->timeout_map |= 1 << td->pipenum;
1218 switch (usb_pipetype(td->urb->pipe)) {
1219 case PIPE_INTERRUPT:
1220 case PIPE_ISOCHRONOUS:
1228 mod_timer(&r8a66597->td_timer[td->pipenum],
1229 jiffies + msecs_to_jiffies(time));
1233 /* this function must be called with interrupt disabled */
1234 static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
1235 u16 pipenum, struct urb *urb, int status)
1236 __releases(r8a66597->lock) __acquires(r8a66597->lock)
1239 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1241 r8a66597->timeout_map &= ~(1 << pipenum);
1244 if (td->set_address && (status != 0 || urb->unlinked))
1245 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1247 pipe_toggle_save(r8a66597, td->pipe, urb);
1248 list_del(&td->queue);
1252 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1256 if (usb_pipeisoc(urb->pipe))
1257 urb->start_frame = r8a66597_get_frame(hcd);
1259 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
1260 spin_unlock(&r8a66597->lock);
1261 usb_hcd_giveback_urb(hcd, urb, status);
1262 spin_lock(&r8a66597->lock);
1266 td = r8a66597_get_td(r8a66597, pipenum);
1270 start_transfer(r8a66597, td);
1271 set_td_timer(r8a66597, td);
1275 static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1278 int rcv_len, bufsize, urb_len, size;
1280 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1289 fifo_change_from_pipe(r8a66597, td->pipe);
1290 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1291 if (unlikely((tmp & FRDY) == 0)) {
1292 pipe_stop(r8a66597, td->pipe);
1293 pipe_irq_disable(r8a66597, pipenum);
1294 printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
1295 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
1299 /* prepare parameters */
1300 rcv_len = tmp & DTLN;
1301 if (usb_pipeisoc(urb->pipe)) {
1302 buf = (u16 *)(urb->transfer_buffer +
1303 urb->iso_frame_desc[td->iso_cnt].offset);
1304 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1306 buf = (void *)urb->transfer_buffer + urb->actual_length;
1307 urb_len = urb->transfer_buffer_length - urb->actual_length;
1309 bufsize = min(urb_len, (int) td->maxpacket);
1310 if (rcv_len <= bufsize) {
1314 status = -EOVERFLOW;
1318 /* update parameters */
1319 urb->actual_length += size;
1321 td->zero_packet = 1;
1322 if (rcv_len < bufsize) {
1323 td->short_packet = 1;
1325 if (usb_pipeisoc(urb->pipe)) {
1326 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1327 urb->iso_frame_desc[td->iso_cnt].status = status;
1332 /* check transfer finish */
1333 if (finish || check_transfer_finish(td, urb)) {
1334 pipe_stop(r8a66597, td->pipe);
1335 pipe_irq_disable(r8a66597, pipenum);
1340 if (urb->transfer_buffer) {
1342 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1344 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1348 if (finish && pipenum != 0)
1349 finish_request(r8a66597, td, pipenum, urb, status);
1352 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1357 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1364 fifo_change_from_pipe(r8a66597, td->pipe);
1365 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1366 if (unlikely((tmp & FRDY) == 0)) {
1367 pipe_stop(r8a66597, td->pipe);
1368 pipe_irq_disable(r8a66597, pipenum);
1369 printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
1370 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
1374 /* prepare parameters */
1375 bufsize = td->maxpacket;
1376 if (usb_pipeisoc(urb->pipe)) {
1377 buf = (u16 *)(urb->transfer_buffer +
1378 urb->iso_frame_desc[td->iso_cnt].offset);
1380 (int)urb->iso_frame_desc[td->iso_cnt].length);
1382 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
1383 size = min_t(u32, bufsize,
1384 urb->transfer_buffer_length - urb->actual_length);
1389 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
1390 if (urb->transfer_buffer) {
1391 r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
1392 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1393 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1396 /* update parameters */
1397 urb->actual_length += size;
1398 if (usb_pipeisoc(urb->pipe)) {
1399 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1400 urb->iso_frame_desc[td->iso_cnt].status = 0;
1404 /* check transfer finish */
1405 if (check_transfer_finish(td, urb)) {
1406 disable_irq_ready(r8a66597, pipenum);
1407 enable_irq_empty(r8a66597, pipenum);
1408 if (!usb_pipeisoc(urb->pipe))
1409 enable_irq_nrdy(r8a66597, pipenum);
1411 pipe_irq_enable(r8a66597, urb, pipenum);
1415 static void check_next_phase(struct r8a66597 *r8a66597, int status)
1417 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1428 if (check_transfer_finish(td, urb))
1429 td->type = USB_PID_ACK;
1432 if (urb->transfer_buffer_length == urb->actual_length)
1433 td->type = USB_PID_ACK;
1434 else if (usb_pipeout(urb->pipe))
1435 td->type = USB_PID_OUT;
1437 td->type = USB_PID_IN;
1444 if (finish || status != 0 || urb->unlinked)
1445 finish_request(r8a66597, td, 0, urb, status);
1447 start_transfer(r8a66597, td);
1450 static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
1452 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1455 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1465 static void irq_pipe_ready(struct r8a66597 *r8a66597)
1470 struct r8a66597_td *td;
1472 mask = r8a66597_read(r8a66597, BRDYSTS)
1473 & r8a66597_read(r8a66597, BRDYENB);
1474 r8a66597_write(r8a66597, ~mask, BRDYSTS);
1476 td = r8a66597_get_td(r8a66597, 0);
1477 if (td && td->type == USB_PID_IN)
1478 packet_read(r8a66597, 0);
1480 pipe_irq_disable(r8a66597, 0);
1481 check_next_phase(r8a66597, 0);
1484 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1485 check = 1 << pipenum;
1487 td = r8a66597_get_td(r8a66597, pipenum);
1491 if (td->type == USB_PID_IN)
1492 packet_read(r8a66597, pipenum);
1493 else if (td->type == USB_PID_OUT)
1494 packet_write(r8a66597, pipenum);
1499 static void irq_pipe_empty(struct r8a66597 *r8a66597)
1505 struct r8a66597_td *td;
1507 mask = r8a66597_read(r8a66597, BEMPSTS)
1508 & r8a66597_read(r8a66597, BEMPENB);
1509 r8a66597_write(r8a66597, ~mask, BEMPSTS);
1511 cfifo_change(r8a66597, 0);
1512 td = r8a66597_get_td(r8a66597, 0);
1513 if (td && td->type != USB_PID_OUT)
1514 disable_irq_empty(r8a66597, 0);
1515 check_next_phase(r8a66597, 0);
1518 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1519 check = 1 << pipenum;
1521 struct r8a66597_td *td;
1522 td = r8a66597_get_td(r8a66597, pipenum);
1526 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1527 if ((tmp & INBUFM) == 0) {
1528 disable_irq_empty(r8a66597, pipenum);
1529 pipe_irq_disable(r8a66597, pipenum);
1530 finish_request(r8a66597, td, pipenum, td->urb,
1537 static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1544 mask = r8a66597_read(r8a66597, NRDYSTS)
1545 & r8a66597_read(r8a66597, NRDYENB);
1546 r8a66597_write(r8a66597, ~mask, NRDYSTS);
1548 cfifo_change(r8a66597, 0);
1549 status = get_urb_error(r8a66597, 0);
1550 pipe_irq_disable(r8a66597, 0);
1551 check_next_phase(r8a66597, status);
1554 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1555 check = 1 << pipenum;
1557 struct r8a66597_td *td;
1558 td = r8a66597_get_td(r8a66597, pipenum);
1562 status = get_urb_error(r8a66597, pipenum);
1563 pipe_irq_disable(r8a66597, pipenum);
1564 pipe_stop(r8a66597, td->pipe);
1565 finish_request(r8a66597, td, pipenum, td->urb, status);
1570 static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1572 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1573 u16 intsts0, intsts1, intsts2;
1574 u16 intenb0, intenb1, intenb2;
1575 u16 mask0, mask1, mask2;
1578 spin_lock(&r8a66597->lock);
1580 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1581 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1582 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1583 intenb0 = r8a66597_read(r8a66597, INTENB0);
1584 intenb1 = r8a66597_read(r8a66597, INTENB1);
1585 intenb2 = r8a66597_read(r8a66597, INTENB2);
1587 mask2 = intsts2 & intenb2;
1588 mask1 = intsts1 & intenb1;
1589 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1591 if (mask2 & ATTCH) {
1592 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
1593 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1595 /* start usb bus sampling */
1596 start_root_hub_sampling(r8a66597, 1, 1);
1599 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
1600 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1601 r8a66597_usb_disconnect(r8a66597, 1);
1604 r8a66597_write(r8a66597, ~BCHG, INTSTS2);
1605 r8a66597_bclr(r8a66597, BCHGE, INTENB2);
1606 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1611 if (mask1 & ATTCH) {
1612 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
1613 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1615 /* start usb bus sampling */
1616 start_root_hub_sampling(r8a66597, 0, 1);
1619 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
1620 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1621 r8a66597_usb_disconnect(r8a66597, 0);
1624 r8a66597_write(r8a66597, ~BCHG, INTSTS1);
1625 r8a66597_bclr(r8a66597, BCHGE, INTENB1);
1626 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1630 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
1631 status = get_urb_error(r8a66597, 0);
1632 check_next_phase(r8a66597, status);
1635 r8a66597_write(r8a66597, ~SACK, INTSTS1);
1636 check_next_phase(r8a66597, 0);
1641 irq_pipe_ready(r8a66597);
1643 irq_pipe_empty(r8a66597);
1645 irq_pipe_nrdy(r8a66597);
1648 spin_unlock(&r8a66597->lock);
1652 /* this function must be called with interrupt disabled */
1653 static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1656 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1658 if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
1659 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1661 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1662 if ((tmp & USBRST) == USBRST) {
1663 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1665 r8a66597_root_hub_start_polling(r8a66597);
1667 r8a66597_usb_connect(r8a66597, port);
1670 if (!(rh->port & (1 << USB_PORT_FEAT_CONNECTION))) {
1671 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1672 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1675 if (rh->scount > 0) {
1676 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1677 if (tmp == rh->old_syssts) {
1679 if (rh->scount == 0)
1680 r8a66597_check_syssts(r8a66597, port, tmp);
1682 r8a66597_root_hub_start_polling(r8a66597);
1684 rh->scount = R8A66597_MAX_SAMPLING;
1685 rh->old_syssts = tmp;
1686 r8a66597_root_hub_start_polling(r8a66597);
1691 static void r8a66597_interval_timer(unsigned long _r8a66597)
1693 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1694 unsigned long flags;
1696 struct r8a66597_td *td;
1698 spin_lock_irqsave(&r8a66597->lock, flags);
1700 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1701 if (!(r8a66597->interval_map & (1 << pipenum)))
1703 if (timer_pending(&r8a66597->interval_timer[pipenum]))
1706 td = r8a66597_get_td(r8a66597, pipenum);
1708 start_transfer(r8a66597, td);
1711 spin_unlock_irqrestore(&r8a66597->lock, flags);
1714 static void r8a66597_td_timer(unsigned long _r8a66597)
1716 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1717 unsigned long flags;
1719 struct r8a66597_td *td, *new_td = NULL;
1720 struct r8a66597_pipe *pipe;
1722 spin_lock_irqsave(&r8a66597->lock, flags);
1723 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1724 if (!(r8a66597->timeout_map & (1 << pipenum)))
1726 if (timer_pending(&r8a66597->td_timer[pipenum]))
1729 td = r8a66597_get_td(r8a66597, pipenum);
1731 r8a66597->timeout_map &= ~(1 << pipenum);
1735 if (td->urb->actual_length) {
1736 set_td_timer(r8a66597, td);
1741 pipe_stop(r8a66597, pipe);
1745 list_move_tail(&new_td->queue,
1746 &r8a66597->pipe_queue[pipenum]);
1747 new_td = r8a66597_get_td(r8a66597, pipenum);
1752 } while (td != new_td && td->address == new_td->address);
1754 start_transfer(r8a66597, new_td);
1757 r8a66597->timeout_map &= ~(1 << pipenum);
1759 set_td_timer(r8a66597, new_td);
1762 spin_unlock_irqrestore(&r8a66597->lock, flags);
1765 static void r8a66597_timer(unsigned long _r8a66597)
1767 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1768 unsigned long flags;
1771 spin_lock_irqsave(&r8a66597->lock, flags);
1773 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
1774 r8a66597_root_hub_control(r8a66597, port);
1776 spin_unlock_irqrestore(&r8a66597->lock, flags);
1779 static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1781 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1783 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1784 (urb->dev->state == USB_STATE_CONFIGURED))
1790 static int r8a66597_start(struct usb_hcd *hcd)
1792 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1794 hcd->state = HC_STATE_RUNNING;
1795 return enable_controller(r8a66597);
1798 static void r8a66597_stop(struct usb_hcd *hcd)
1800 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1802 disable_controller(r8a66597);
1805 static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1807 unsigned int usb_address = usb_pipedevice(urb->pipe);
1808 u16 root_port, hub_port;
1810 if (usb_address == 0) {
1811 get_port_number(urb->dev->devpath,
1812 &root_port, &hub_port);
1813 set_devadd_reg(r8a66597, 0,
1814 get_r8a66597_usb_speed(urb->dev->speed),
1815 get_parent_r8a66597_address(r8a66597, urb->dev),
1816 hub_port, root_port);
1820 static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1822 struct usb_host_endpoint *hep)
1824 struct r8a66597_td *td;
1827 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
1831 pipenum = r8a66597_get_pipenum(urb, hep);
1832 td->pipenum = pipenum;
1833 td->pipe = hep->hcpriv;
1835 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1836 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1837 !usb_pipein(urb->pipe));
1838 if (usb_pipecontrol(urb->pipe))
1839 td->type = USB_PID_SETUP;
1840 else if (usb_pipein(urb->pipe))
1841 td->type = USB_PID_IN;
1843 td->type = USB_PID_OUT;
1844 INIT_LIST_HEAD(&td->queue);
1849 static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
1853 struct usb_host_endpoint *hep = urb->ep;
1854 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1855 struct r8a66597_td *td = NULL;
1856 int ret, request = 0;
1857 unsigned long flags;
1859 spin_lock_irqsave(&r8a66597->lock, flags);
1860 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1862 goto error_not_linked;
1865 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1867 goto error_not_linked;
1870 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1876 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1877 if (usb_pipeendpoint(urb->pipe))
1878 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1881 if (unlikely(check_pipe_config(r8a66597, urb)))
1882 init_pipe_config(r8a66597, urb);
1884 set_address_zero(r8a66597, urb);
1885 td = r8a66597_make_td(r8a66597, urb, hep);
1890 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1892 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
1896 if (td->pipe->info.timer_interval) {
1897 r8a66597->interval_map |= 1 << td->pipenum;
1898 mod_timer(&r8a66597->interval_timer[td->pipenum],
1899 jiffies + msecs_to_jiffies(
1900 td->pipe->info.timer_interval));
1902 ret = start_transfer(r8a66597, td);
1904 list_del(&td->queue);
1909 set_td_timer(r8a66597, td);
1913 usb_hcd_unlink_urb_from_ep(hcd, urb);
1915 spin_unlock_irqrestore(&r8a66597->lock, flags);
1919 static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1922 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1923 struct r8a66597_td *td;
1924 unsigned long flags;
1927 spin_lock_irqsave(&r8a66597->lock, flags);
1928 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1934 pipe_stop(r8a66597, td->pipe);
1935 pipe_irq_disable(r8a66597, td->pipenum);
1936 disable_irq_empty(r8a66597, td->pipenum);
1937 finish_request(r8a66597, td, td->pipenum, urb, status);
1940 spin_unlock_irqrestore(&r8a66597->lock, flags);
1944 static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1945 struct usb_host_endpoint *hep)
1947 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1948 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1949 struct r8a66597_td *td;
1950 struct urb *urb = NULL;
1952 unsigned long flags;
1956 pipenum = pipe->info.pipenum;
1964 spin_lock_irqsave(&r8a66597->lock, flags);
1965 pipe_stop(r8a66597, pipe);
1966 pipe_irq_disable(r8a66597, pipenum);
1967 disable_irq_empty(r8a66597, pipenum);
1968 td = r8a66597_get_td(r8a66597, pipenum);
1971 finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
1974 spin_unlock_irqrestore(&r8a66597->lock, flags);
1977 static int r8a66597_get_frame(struct usb_hcd *hcd)
1979 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1980 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
1983 static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
1987 if (udev->state == USB_STATE_CONFIGURED &&
1988 udev->parent && udev->parent->devnum > 1 &&
1989 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
1990 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
1992 for (chix = 0; chix < udev->maxchild; chix++) {
1993 struct usb_device *childdev = udev->children[chix];
1996 collect_usb_address_map(childdev, map);
2000 /* this function must be called with interrupt disabled */
2001 static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
2004 struct r8a66597_device *dev;
2005 struct list_head *list = &r8a66597->child_device;
2007 list_for_each_entry(dev, list, device_list) {
2010 if (dev->usb_address != addr)
2016 printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
2020 static void update_usb_address_map(struct r8a66597 *r8a66597,
2021 struct usb_device *root_hub,
2026 unsigned long flags;
2028 for (i = 0; i < 4; i++) {
2029 diff = r8a66597->child_connect_map[i] ^ map[i];
2033 for (j = 0; j < 32; j++) {
2034 if (!(diff & (1 << j)))
2038 if (map[i] & (1 << j))
2039 set_child_connect_map(r8a66597, addr);
2041 struct r8a66597_device *dev;
2043 spin_lock_irqsave(&r8a66597->lock, flags);
2044 dev = get_r8a66597_device(r8a66597, addr);
2045 disable_r8a66597_pipe_all(r8a66597, dev);
2046 free_usb_address(r8a66597, dev);
2047 put_child_connect_map(r8a66597, addr);
2048 spin_unlock_irqrestore(&r8a66597->lock, flags);
2054 static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
2055 struct usb_hcd *hcd)
2057 struct usb_bus *bus;
2058 unsigned long now_map[4];
2060 memset(now_map, 0, sizeof(now_map));
2062 list_for_each_entry(bus, &usb_bus_list, bus_list) {
2066 if (bus->busnum != hcd->self.busnum)
2069 collect_usb_address_map(bus->root_hub, now_map);
2070 update_usb_address_map(r8a66597, bus->root_hub, now_map);
2074 static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2076 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2077 unsigned long flags;
2080 r8a66597_check_detect_child(r8a66597, hcd);
2082 spin_lock_irqsave(&r8a66597->lock, flags);
2084 *buf = 0; /* initialize (no change) */
2086 for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++) {
2087 if (r8a66597->root_hub[i].port & 0xffff0000)
2088 *buf |= 1 << (i + 1);
2091 spin_unlock_irqrestore(&r8a66597->lock, flags);
2096 static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2097 struct usb_hub_descriptor *desc)
2099 desc->bDescriptorType = 0x29;
2100 desc->bHubContrCurrent = 0;
2101 desc->bNbrPorts = R8A66597_MAX_ROOT_HUB;
2102 desc->bDescLength = 9;
2103 desc->bPwrOn2PwrGood = 0;
2104 desc->wHubCharacteristics = cpu_to_le16(0x0011);
2105 desc->bitmap[0] = ((1 << R8A66597_MAX_ROOT_HUB) - 1) << 1;
2106 desc->bitmap[1] = ~0;
2109 static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2110 u16 wIndex, char *buf, u16 wLength)
2112 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2114 int port = (wIndex & 0x00FF) - 1;
2115 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2116 unsigned long flags;
2120 spin_lock_irqsave(&r8a66597->lock, flags);
2122 case ClearHubFeature:
2125 case C_HUB_OVER_CURRENT:
2126 case C_HUB_LOCAL_POWER:
2132 case ClearPortFeature:
2133 if (wIndex > R8A66597_MAX_ROOT_HUB)
2139 case USB_PORT_FEAT_ENABLE:
2140 rh->port &= ~(1 << USB_PORT_FEAT_POWER);
2142 case USB_PORT_FEAT_SUSPEND:
2144 case USB_PORT_FEAT_POWER:
2145 r8a66597_port_power(r8a66597, port, 0);
2147 case USB_PORT_FEAT_C_ENABLE:
2148 case USB_PORT_FEAT_C_SUSPEND:
2149 case USB_PORT_FEAT_C_CONNECTION:
2150 case USB_PORT_FEAT_C_OVER_CURRENT:
2151 case USB_PORT_FEAT_C_RESET:
2156 rh->port &= ~(1 << wValue);
2158 case GetHubDescriptor:
2159 r8a66597_hub_descriptor(r8a66597,
2160 (struct usb_hub_descriptor *)buf);
2166 if (wIndex > R8A66597_MAX_ROOT_HUB)
2168 *(__le32 *)buf = cpu_to_le32(rh->port);
2170 case SetPortFeature:
2171 if (wIndex > R8A66597_MAX_ROOT_HUB)
2177 case USB_PORT_FEAT_SUSPEND:
2179 case USB_PORT_FEAT_POWER:
2180 r8a66597_port_power(r8a66597, port, 1);
2181 rh->port |= (1 << USB_PORT_FEAT_POWER);
2183 case USB_PORT_FEAT_RESET: {
2184 struct r8a66597_device *dev = rh->dev;
2186 rh->port |= (1 << USB_PORT_FEAT_RESET);
2188 disable_r8a66597_pipe_all(r8a66597, dev);
2189 free_usb_address(r8a66597, dev);
2191 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2192 get_dvstctr_reg(port));
2193 mod_timer(&r8a66597->rh_timer,
2194 jiffies + msecs_to_jiffies(50));
2200 rh->port |= 1 << wValue;
2208 spin_unlock_irqrestore(&r8a66597->lock, flags);
2212 #if defined(CONFIG_PM)
2213 static int r8a66597_bus_suspend(struct usb_hcd *hcd)
2215 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2218 dbg("%s", __func__);
2220 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
2221 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2222 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2224 if (!(rh->port & (1 << USB_PORT_FEAT_ENABLE)))
2227 dbg("suspend port = %d", port);
2228 r8a66597_bclr(r8a66597, UACT, dvstctr_reg); /* suspend */
2229 rh->port |= 1 << USB_PORT_FEAT_SUSPEND;
2231 if (rh->dev->udev->do_remote_wakeup) {
2232 msleep(3); /* waiting last SOF */
2233 r8a66597_bset(r8a66597, RWUPE, dvstctr_reg);
2234 r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
2235 r8a66597_bset(r8a66597, BCHGE, get_intenb_reg(port));
2239 r8a66597->bus_suspended = 1;
2244 static int r8a66597_bus_resume(struct usb_hcd *hcd)
2246 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2249 dbg("%s", __func__);
2251 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
2252 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2253 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2255 if (!(rh->port & (1 << USB_PORT_FEAT_SUSPEND)))
2258 dbg("resume port = %d", port);
2259 rh->port &= ~(1 << USB_PORT_FEAT_SUSPEND);
2260 rh->port |= 1 << USB_PORT_FEAT_C_SUSPEND;
2261 r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
2263 r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
2270 #define r8a66597_bus_suspend NULL
2271 #define r8a66597_bus_resume NULL
2274 static struct hc_driver r8a66597_hc_driver = {
2275 .description = hcd_name,
2276 .hcd_priv_size = sizeof(struct r8a66597),
2277 .irq = r8a66597_irq,
2280 * generic hardware linkage
2284 .start = r8a66597_start,
2285 .stop = r8a66597_stop,
2288 * managing i/o requests and associated device resources
2290 .urb_enqueue = r8a66597_urb_enqueue,
2291 .urb_dequeue = r8a66597_urb_dequeue,
2292 .endpoint_disable = r8a66597_endpoint_disable,
2295 * periodic schedule support
2297 .get_frame_number = r8a66597_get_frame,
2302 .hub_status_data = r8a66597_hub_status_data,
2303 .hub_control = r8a66597_hub_control,
2304 .bus_suspend = r8a66597_bus_suspend,
2305 .bus_resume = r8a66597_bus_resume,
2308 #if defined(CONFIG_PM)
2309 static int r8a66597_suspend(struct platform_device *pdev, pm_message_t state)
2311 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2314 dbg("%s", __func__);
2316 disable_controller(r8a66597);
2318 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
2319 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2321 rh->port = 0x00000000;
2327 static int r8a66597_resume(struct platform_device *pdev)
2329 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2330 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2332 dbg("%s", __func__);
2334 enable_controller(r8a66597);
2335 usb_root_hub_lost_power(hcd->self.root_hub);
2339 #else /* if defined(CONFIG_PM) */
2340 #define r8a66597_suspend NULL
2341 #define r8a66597_resume NULL
2344 static int __init_or_module r8a66597_remove(struct platform_device *pdev)
2346 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2347 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2349 del_timer_sync(&r8a66597->rh_timer);
2350 usb_remove_hcd(hcd);
2351 iounmap((void *)r8a66597->reg);
2352 #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2353 clk_put(r8a66597->clk);
2359 static int __devinit r8a66597_probe(struct platform_device *pdev)
2361 #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2364 struct resource *res = NULL, *ires;
2366 void __iomem *reg = NULL;
2367 struct usb_hcd *hcd = NULL;
2368 struct r8a66597 *r8a66597;
2371 unsigned long irq_trigger;
2373 if (pdev->dev.dma_mask) {
2375 dev_err(&pdev->dev, "dma not supported\n");
2379 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2382 dev_err(&pdev->dev, "platform_get_resource error.\n");
2386 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2390 "platform_get_resource IORESOURCE_IRQ error.\n");
2395 irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
2397 reg = ioremap(res->start, resource_size(res));
2400 dev_err(&pdev->dev, "ioremap error.\n");
2404 if (pdev->dev.platform_data == NULL) {
2405 dev_err(&pdev->dev, "no platform data\n");
2410 /* initialize hcd */
2411 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2414 dev_err(&pdev->dev, "Failed to create hcd\n");
2417 r8a66597 = hcd_to_r8a66597(hcd);
2418 memset(r8a66597, 0, sizeof(struct r8a66597));
2419 dev_set_drvdata(&pdev->dev, r8a66597);
2420 r8a66597->pdata = pdev->dev.platform_data;
2421 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
2423 #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2424 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
2425 r8a66597->clk = clk_get(&pdev->dev, clk_name);
2426 if (IS_ERR(r8a66597->clk)) {
2427 dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
2428 ret = PTR_ERR(r8a66597->clk);
2433 spin_lock_init(&r8a66597->lock);
2434 init_timer(&r8a66597->rh_timer);
2435 r8a66597->rh_timer.function = r8a66597_timer;
2436 r8a66597->rh_timer.data = (unsigned long)r8a66597;
2437 r8a66597->reg = (unsigned long)reg;
2439 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2440 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2441 init_timer(&r8a66597->td_timer[i]);
2442 r8a66597->td_timer[i].function = r8a66597_td_timer;
2443 r8a66597->td_timer[i].data = (unsigned long)r8a66597;
2444 setup_timer(&r8a66597->interval_timer[i],
2445 r8a66597_interval_timer,
2446 (unsigned long)r8a66597);
2448 INIT_LIST_HEAD(&r8a66597->child_device);
2450 hcd->rsrc_start = res->start;
2452 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger);
2454 dev_err(&pdev->dev, "Failed to add hcd\n");
2461 #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2462 clk_put(r8a66597->clk);
2474 static struct platform_driver r8a66597_driver = {
2475 .probe = r8a66597_probe,
2476 .remove = r8a66597_remove,
2477 .suspend = r8a66597_suspend,
2478 .resume = r8a66597_resume,
2480 .name = (char *) hcd_name,
2481 .owner = THIS_MODULE,
2485 static int __init r8a66597_init(void)
2490 printk(KERN_INFO KBUILD_MODNAME ": driver %s, %s\n", hcd_name,
2492 return platform_driver_register(&r8a66597_driver);
2494 module_init(r8a66597_init);
2496 static void __exit r8a66597_cleanup(void)
2498 platform_driver_unregister(&r8a66597_driver);
2500 module_exit(r8a66597_cleanup);