1 // SPDX-License-Identifier: GPL-2.0
3 * R8A66597 HCD (Host Controller Driver)
5 * Copyright (C) 2006-2007 Renesas Solutions Corp.
6 * Portions Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
7 * Portions Copyright (C) 2004-2005 David Brownell
8 * Portions Copyright (C) 1999 Roman Weissgaerber
10 * Author : Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/errno.h>
17 #include <linux/timer.h>
18 #include <linux/delay.h>
19 #include <linux/list.h>
20 #include <linux/interrupt.h>
21 #include <linux/usb.h>
22 #include <linux/usb/hcd.h>
23 #include <linux/platform_device.h>
26 #include <linux/irq.h>
27 #include <linux/slab.h>
28 #include <asm/cacheflush.h>
32 MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Yoshihiro Shimoda");
35 MODULE_ALIAS("platform:r8a66597_hcd");
37 #define DRIVER_VERSION "2009-05-26"
39 static const char hcd_name[] = "r8a66597_hcd";
41 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
42 static int r8a66597_get_frame(struct usb_hcd *hcd);
44 /* this function must be called with interrupt disabled */
45 static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
50 tmp = r8a66597_read(r8a66597, INTENB0);
51 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
52 r8a66597_bset(r8a66597, 1 << pipenum, reg);
53 r8a66597_write(r8a66597, tmp, INTENB0);
56 /* this function must be called with interrupt disabled */
57 static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
62 tmp = r8a66597_read(r8a66597, INTENB0);
63 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
64 r8a66597_bclr(r8a66597, 1 << pipenum, reg);
65 r8a66597_write(r8a66597, tmp, INTENB0);
68 static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
69 u16 usbspd, u8 upphub, u8 hubport, int port)
72 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
74 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
75 r8a66597_write(r8a66597, val, devadd_reg);
78 static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
83 if (r8a66597->pdata->on_chip) {
84 clk_prepare_enable(r8a66597->clk);
86 r8a66597_write(r8a66597, SCKE, SYSCFG0);
87 tmp = r8a66597_read(r8a66597, SYSCFG0);
89 printk(KERN_ERR "r8a66597: reg access fail.\n");
92 } while ((tmp & SCKE) != SCKE);
93 r8a66597_write(r8a66597, 0x04, 0x02);
96 r8a66597_write(r8a66597, USBE, SYSCFG0);
97 tmp = r8a66597_read(r8a66597, SYSCFG0);
99 printk(KERN_ERR "r8a66597: reg access fail.\n");
102 } while ((tmp & USBE) != USBE);
103 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
104 r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata),
108 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
111 tmp = r8a66597_read(r8a66597, SYSCFG0);
113 printk(KERN_ERR "r8a66597: reg access fail.\n");
116 } while ((tmp & SCKE) != SCKE);
122 static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
124 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
127 if (r8a66597->pdata->on_chip) {
128 clk_disable_unprepare(r8a66597->clk);
130 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
131 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
132 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
136 static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
140 val = port ? DRPD : DCFM | DRPD;
141 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
142 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
144 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
145 r8a66597_bclr(r8a66597, DTCHE, get_intenb_reg(port));
146 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
149 static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
153 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
154 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
156 r8a66597_port_power(r8a66597, port, 0);
159 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
161 } while (tmp == EDGESTS);
163 val = port ? DRPD : DCFM | DRPD;
164 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
165 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
168 static int enable_controller(struct r8a66597 *r8a66597)
171 u16 vif = r8a66597->pdata->vif ? LDRV : 0;
172 u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
173 u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
175 ret = r8a66597_clock_enable(r8a66597);
179 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
180 r8a66597_bset(r8a66597, USBE, SYSCFG0);
182 r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
183 r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
184 r8a66597_bset(r8a66597, BRDY0, BRDYENB);
185 r8a66597_bset(r8a66597, BEMP0, BEMPENB);
187 r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
188 r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
189 r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
190 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
192 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
194 for (port = 0; port < r8a66597->max_root_hub; port++)
195 r8a66597_enable_port(r8a66597, port);
200 static void disable_controller(struct r8a66597 *r8a66597)
204 /* disable interrupts */
205 r8a66597_write(r8a66597, 0, INTENB0);
206 r8a66597_write(r8a66597, 0, INTENB1);
207 r8a66597_write(r8a66597, 0, BRDYENB);
208 r8a66597_write(r8a66597, 0, BEMPENB);
209 r8a66597_write(r8a66597, 0, NRDYENB);
212 r8a66597_write(r8a66597, 0, BRDYSTS);
213 r8a66597_write(r8a66597, 0, NRDYSTS);
214 r8a66597_write(r8a66597, 0, BEMPSTS);
216 for (port = 0; port < r8a66597->max_root_hub; port++)
217 r8a66597_disable_port(r8a66597, port);
219 r8a66597_clock_disable(r8a66597);
222 static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
223 struct usb_device *udev)
225 struct r8a66597_device *dev;
227 if (udev->parent && udev->parent->devnum != 1)
230 dev = dev_get_drvdata(&udev->dev);
237 static int is_child_device(char *devpath)
239 return (devpath[2] ? 1 : 0);
242 static int is_hub_limit(char *devpath)
244 return ((strlen(devpath) >= 4) ? 1 : 0);
247 static void get_port_number(struct r8a66597 *r8a66597,
248 char *devpath, u16 *root_port, u16 *hub_port)
251 *root_port = (devpath[0] & 0x0F) - 1;
252 if (*root_port >= r8a66597->max_root_hub)
253 printk(KERN_ERR "r8a66597: Illegal root port number.\n");
256 *hub_port = devpath[2] & 0x0F;
259 static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
274 printk(KERN_ERR "r8a66597: unknown speed\n");
281 static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
286 r8a66597->child_connect_map[idx] |= 1 << (address % 32);
289 static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
294 r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
297 static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
299 u16 pipenum = pipe->info.pipenum;
300 const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
301 const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
302 const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
304 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
305 dma_ch = R8A66597_PIPE_NO_DMA;
307 pipe->fifoaddr = fifoaddr[dma_ch];
308 pipe->fifosel = fifosel[dma_ch];
309 pipe->fifoctr = fifoctr[dma_ch];
312 pipe->pipectr = DCPCTR;
314 pipe->pipectr = get_pipectr_addr(pipenum);
316 if (check_bulk_or_isoc(pipenum)) {
317 pipe->pipetre = get_pipetre_addr(pipenum);
318 pipe->pipetrn = get_pipetrn_addr(pipenum);
325 static struct r8a66597_device *
326 get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
328 if (usb_pipedevice(urb->pipe) == 0)
329 return &r8a66597->device0;
331 return dev_get_drvdata(&urb->dev->dev);
334 static int make_r8a66597_device(struct r8a66597 *r8a66597,
335 struct urb *urb, u8 addr)
337 struct r8a66597_device *dev;
338 int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
340 dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
344 dev_set_drvdata(&urb->dev->dev, dev);
345 dev->udev = urb->dev;
347 dev->usb_address = usb_address;
348 dev->state = USB_STATE_ADDRESS;
349 dev->ep_in_toggle = 0;
350 dev->ep_out_toggle = 0;
351 INIT_LIST_HEAD(&dev->device_list);
352 list_add_tail(&dev->device_list, &r8a66597->child_device);
354 get_port_number(r8a66597, urb->dev->devpath,
355 &dev->root_port, &dev->hub_port);
356 if (!is_child_device(urb->dev->devpath))
357 r8a66597->root_hub[dev->root_port].dev = dev;
359 set_devadd_reg(r8a66597, dev->address,
360 get_r8a66597_usb_speed(urb->dev->speed),
361 get_parent_r8a66597_address(r8a66597, urb->dev),
362 dev->hub_port, dev->root_port);
367 /* this function must be called with interrupt disabled */
368 static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
370 u8 addr; /* R8A66597's address */
371 struct r8a66597_device *dev;
373 if (is_hub_limit(urb->dev->devpath)) {
374 dev_err(&urb->dev->dev, "External hub limit reached.\n");
378 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
379 if (dev && dev->state >= USB_STATE_ADDRESS)
382 for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
383 if (r8a66597->address_map & (1 << addr))
386 dev_dbg(&urb->dev->dev, "alloc_address: r8a66597_addr=%d\n", addr);
387 r8a66597->address_map |= 1 << addr;
389 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
395 dev_err(&urb->dev->dev,
396 "cannot communicate with a USB device more than 10.(%x)\n",
397 r8a66597->address_map);
402 /* this function must be called with interrupt disabled */
403 static void free_usb_address(struct r8a66597 *r8a66597,
404 struct r8a66597_device *dev, int reset)
411 dev_dbg(&dev->udev->dev, "free_addr: addr=%d\n", dev->address);
413 dev->state = USB_STATE_DEFAULT;
414 r8a66597->address_map &= ~(1 << dev->address);
417 * Only when resetting USB, it is necessary to erase drvdata. When
418 * a usb device with usb hub is disconnect, "dev->udev" is already
419 * freed on usb_desconnect(). So we cannot access the data.
422 dev_set_drvdata(&dev->udev->dev, NULL);
423 list_del(&dev->device_list);
426 for (port = 0; port < r8a66597->max_root_hub; port++) {
427 if (r8a66597->root_hub[port].dev == dev) {
428 r8a66597->root_hub[port].dev = NULL;
434 static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
441 tmp = r8a66597_read(r8a66597, reg);
443 printk(KERN_ERR "r8a66597: register%lx, loop %x "
444 "is timeout\n", reg, loop);
448 } while ((tmp & mask) != loop);
451 /* this function must be called with interrupt disabled */
452 static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
456 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
457 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
458 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
459 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
462 /* this function must be called with interrupt disabled */
463 static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
467 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
468 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
469 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
470 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
471 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
474 /* this function must be called with interrupt disabled */
475 static void clear_all_buffer(struct r8a66597 *r8a66597,
476 struct r8a66597_pipe *pipe)
478 if (!pipe || pipe->info.pipenum == 0)
481 pipe_stop(r8a66597, pipe);
482 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
483 r8a66597_read(r8a66597, pipe->pipectr);
484 r8a66597_read(r8a66597, pipe->pipectr);
485 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 static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
501 if (r8a66597->pdata->on_chip)
507 /* this function must be called with interrupt disabled */
508 static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
510 unsigned short mbw = mbw_value(r8a66597);
512 r8a66597_mdfy(r8a66597, mbw | pipenum, mbw | CURPIPE, CFIFOSEL);
513 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
516 /* this function must be called with interrupt disabled */
517 static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
518 struct r8a66597_pipe *pipe)
520 unsigned short mbw = mbw_value(r8a66597);
522 cfifo_change(r8a66597, 0);
523 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D0FIFOSEL);
524 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D1FIFOSEL);
526 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum, mbw | CURPIPE,
528 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
531 static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
533 struct r8a66597_pipe *pipe = hep->hcpriv;
535 if (usb_pipeendpoint(urb->pipe) == 0)
538 return pipe->info.pipenum;
541 static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
543 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
545 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
548 static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
554 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
557 /* this function must be called with interrupt disabled */
558 static void pipe_toggle_set(struct r8a66597 *r8a66597,
559 struct r8a66597_pipe *pipe,
560 struct urb *urb, int set)
562 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
563 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
564 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
570 *toggle |= 1 << endpoint;
572 *toggle &= ~(1 << endpoint);
575 /* this function must be called with interrupt disabled */
576 static void pipe_toggle_save(struct r8a66597 *r8a66597,
577 struct r8a66597_pipe *pipe,
580 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
581 pipe_toggle_set(r8a66597, pipe, urb, 1);
583 pipe_toggle_set(r8a66597, pipe, urb, 0);
586 /* this function must be called with interrupt disabled */
587 static void pipe_toggle_restore(struct r8a66597 *r8a66597,
588 struct r8a66597_pipe *pipe,
591 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
592 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
593 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
598 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
601 /* this function must be called with interrupt disabled */
602 static void pipe_buffer_setting(struct r8a66597 *r8a66597,
603 struct r8a66597_pipe_info *info)
607 if (info->pipenum == 0)
610 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
611 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
612 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
615 if (info->type == R8A66597_BULK && info->dir_in)
616 val |= R8A66597_DBLB | R8A66597_SHTNAK;
617 val |= info->type | info->epnum;
618 r8a66597_write(r8a66597, val, PIPECFG);
620 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
622 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
624 r8a66597_write(r8a66597, info->interval, PIPEPERI);
627 /* this function must be called with interrupt disabled */
628 static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
630 struct r8a66597_pipe_info *info;
631 struct urb *urb = td->urb;
633 if (td->pipenum > 0) {
634 info = &td->pipe->info;
635 cfifo_change(r8a66597, 0);
636 pipe_buffer_setting(r8a66597, info);
638 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
639 usb_pipeout(urb->pipe)) &&
640 !usb_pipecontrol(urb->pipe)) {
641 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
642 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
643 clear_all_buffer(r8a66597, td->pipe);
644 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
645 usb_pipeout(urb->pipe), 1);
647 pipe_toggle_restore(r8a66597, td->pipe, urb);
651 /* this function must be called with interrupt disabled */
652 static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
653 struct usb_endpoint_descriptor *ep)
655 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
657 memset(array, 0, sizeof(array));
658 switch (usb_endpoint_type(ep)) {
659 case USB_ENDPOINT_XFER_BULK:
660 if (usb_endpoint_dir_in(ep))
667 case USB_ENDPOINT_XFER_INT:
668 if (usb_endpoint_dir_in(ep)) {
675 case USB_ENDPOINT_XFER_ISOC:
676 if (usb_endpoint_dir_in(ep))
682 printk(KERN_ERR "r8a66597: Illegal type\n");
688 while (array[i] != 0) {
689 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
697 static u16 get_r8a66597_type(__u8 type)
702 case USB_ENDPOINT_XFER_BULK:
703 r8a66597_type = R8A66597_BULK;
705 case USB_ENDPOINT_XFER_INT:
706 r8a66597_type = R8A66597_INT;
708 case USB_ENDPOINT_XFER_ISOC:
709 r8a66597_type = R8A66597_ISO;
712 printk(KERN_ERR "r8a66597: Illegal type\n");
713 r8a66597_type = 0x0000;
717 return r8a66597_type;
720 static u16 get_bufnum(u16 pipenum)
726 else if (check_bulk_or_isoc(pipenum))
727 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
728 else if (check_interrupt(pipenum))
729 bufnum = 4 + (pipenum - 6);
731 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
736 static u16 get_buf_bsize(u16 pipenum)
742 else if (check_bulk_or_isoc(pipenum))
743 buf_bsize = R8A66597_BUF_BSIZE - 1;
744 else if (check_interrupt(pipenum))
747 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
752 /* this function must be called with interrupt disabled */
753 static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
754 struct r8a66597_device *dev,
755 struct r8a66597_pipe *pipe,
759 struct r8a66597_pipe_info *info = &pipe->info;
760 unsigned short mbw = mbw_value(r8a66597);
762 /* pipe dma is only for external controlles */
763 if (r8a66597->pdata->on_chip)
766 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
767 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
768 if ((r8a66597->dma_map & (1 << i)) != 0)
771 dev_info(&dev->udev->dev,
772 "address %d, EndpointAddress 0x%02x use "
773 "DMA FIFO\n", usb_pipedevice(urb->pipe),
775 USB_ENDPOINT_DIR_MASK + info->epnum
778 r8a66597->dma_map |= 1 << i;
779 dev->dma_map |= 1 << i;
780 set_pipe_reg_addr(pipe, i);
782 cfifo_change(r8a66597, 0);
783 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum,
784 mbw | CURPIPE, pipe->fifosel);
786 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
788 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
794 /* this function must be called with interrupt disabled */
795 static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
796 struct usb_host_endpoint *hep,
797 struct r8a66597_pipe_info *info)
799 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
800 struct r8a66597_pipe *pipe = hep->hcpriv;
802 dev_dbg(&dev->udev->dev, "enable_pipe:\n");
805 set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
806 r8a66597->pipe_cnt[pipe->info.pipenum]++;
807 dev->pipe_cnt[pipe->info.pipenum]++;
809 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
812 static void r8a66597_urb_done(struct r8a66597 *r8a66597, struct urb *urb,
814 __releases(r8a66597->lock)
815 __acquires(r8a66597->lock)
817 if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
820 for (ptr = urb->transfer_buffer;
821 ptr < urb->transfer_buffer + urb->transfer_buffer_length;
823 flush_dcache_page(virt_to_page(ptr));
826 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
827 spin_unlock(&r8a66597->lock);
828 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb, status);
829 spin_lock(&r8a66597->lock);
832 /* this function must be called with interrupt disabled */
833 static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
835 struct r8a66597_td *td, *next;
837 struct list_head *list = &r8a66597->pipe_queue[pipenum];
839 if (list_empty(list))
842 list_for_each_entry_safe(td, next, list, queue) {
843 if (td->address != address)
847 list_del(&td->queue);
851 r8a66597_urb_done(r8a66597, urb, -ENODEV);
857 /* this function must be called with interrupt disabled */
858 static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
859 struct r8a66597_device *dev)
867 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
868 if (!dev->pipe_cnt[pipenum])
873 force_dequeue(r8a66597, 0, dev->address);
876 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
877 dev->pipe_cnt[pipenum] = 0;
878 force_dequeue(r8a66597, pipenum, dev->address);
881 dev_dbg(&dev->udev->dev, "disable_pipe\n");
883 r8a66597->dma_map &= ~(dev->dma_map);
887 static u16 get_interval(struct urb *urb, __u8 interval)
892 if (urb->dev->speed == USB_SPEED_HIGH) {
896 time = interval ? interval - 1 : 0;
898 if (interval > 128) {
901 /* calculate the nearest value for PIPEPERI */
902 for (i = 0; i < 7; i++) {
903 if ((1 << i) < interval &&
904 (1 << (i + 1) > interval))
913 static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
916 unsigned long time = 1;
918 if (usb_pipeisoc(urb->pipe))
921 if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
922 for (i = 0; i < (interval - 1); i++)
924 time = time * 125 / 1000; /* uSOF -> msec */
932 /* this function must be called with interrupt disabled */
933 static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
934 struct usb_host_endpoint *hep,
935 struct usb_endpoint_descriptor *ep)
937 struct r8a66597_pipe_info info;
939 info.pipenum = get_empty_pipenum(r8a66597, ep);
940 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
941 info.epnum = usb_endpoint_num(ep);
942 info.maxpacket = usb_endpoint_maxp(ep);
943 info.type = get_r8a66597_type(usb_endpoint_type(ep));
944 info.bufnum = get_bufnum(info.pipenum);
945 info.buf_bsize = get_buf_bsize(info.pipenum);
946 if (info.type == R8A66597_BULK) {
948 info.timer_interval = 0;
950 info.interval = get_interval(urb, ep->bInterval);
951 info.timer_interval = get_timer_interval(urb, ep->bInterval);
953 if (usb_endpoint_dir_in(ep))
958 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
961 static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
963 struct r8a66597_device *dev;
965 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
966 dev->state = USB_STATE_CONFIGURED;
969 static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
972 if (pipenum == 0 && usb_pipeout(urb->pipe))
973 enable_irq_empty(r8a66597, pipenum);
975 enable_irq_ready(r8a66597, pipenum);
977 if (!usb_pipeisoc(urb->pipe))
978 enable_irq_nrdy(r8a66597, pipenum);
981 static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
983 disable_irq_ready(r8a66597, pipenum);
984 disable_irq_nrdy(r8a66597, pipenum);
987 static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
989 mod_timer(&r8a66597->rh_timer,
990 jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
993 static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
996 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
998 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
999 rh->scount = R8A66597_MAX_SAMPLING;
1001 rh->port |= USB_PORT_STAT_CONNECTION;
1003 rh->port &= ~USB_PORT_STAT_CONNECTION;
1004 rh->port |= USB_PORT_STAT_C_CONNECTION << 16;
1006 r8a66597_root_hub_start_polling(r8a66597);
1009 /* this function must be called with interrupt disabled */
1010 static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
1012 __releases(r8a66597->lock)
1013 __acquires(r8a66597->lock)
1015 if (syssts == SE0) {
1016 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1017 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1019 if (syssts == FS_JSTS)
1020 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
1021 else if (syssts == LS_JSTS)
1022 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
1024 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
1025 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
1027 if (r8a66597->bus_suspended)
1028 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1031 spin_unlock(&r8a66597->lock);
1032 usb_hcd_poll_rh_status(r8a66597_to_hcd(r8a66597));
1033 spin_lock(&r8a66597->lock);
1036 /* this function must be called with interrupt disabled */
1037 static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
1039 u16 speed = get_rh_usb_speed(r8a66597, port);
1040 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1042 rh->port &= ~(USB_PORT_STAT_HIGH_SPEED | USB_PORT_STAT_LOW_SPEED);
1043 if (speed == HSMODE)
1044 rh->port |= USB_PORT_STAT_HIGH_SPEED;
1045 else if (speed == LSMODE)
1046 rh->port |= USB_PORT_STAT_LOW_SPEED;
1048 rh->port &= ~USB_PORT_STAT_RESET;
1049 rh->port |= USB_PORT_STAT_ENABLE;
1052 /* this function must be called with interrupt disabled */
1053 static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
1055 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
1057 disable_r8a66597_pipe_all(r8a66597, dev);
1058 free_usb_address(r8a66597, dev, 0);
1060 start_root_hub_sampling(r8a66597, port, 0);
1063 /* this function must be called with interrupt disabled */
1064 static void prepare_setup_packet(struct r8a66597 *r8a66597,
1065 struct r8a66597_td *td)
1068 __le16 *p = (__le16 *)td->urb->setup_packet;
1069 unsigned long setup_addr = USBREQ;
1071 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1073 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
1075 for (i = 0; i < 4; i++) {
1076 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
1079 r8a66597_write(r8a66597, SUREQ, DCPCTR);
1082 /* this function must be called with interrupt disabled */
1083 static void prepare_packet_read(struct r8a66597 *r8a66597,
1084 struct r8a66597_td *td)
1086 struct urb *urb = td->urb;
1088 if (usb_pipecontrol(urb->pipe)) {
1089 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1090 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1091 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1092 if (urb->actual_length == 0) {
1093 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1094 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1096 pipe_irq_disable(r8a66597, td->pipenum);
1097 pipe_start(r8a66597, td->pipe);
1098 pipe_irq_enable(r8a66597, urb, td->pipenum);
1100 if (urb->actual_length == 0) {
1101 pipe_irq_disable(r8a66597, td->pipenum);
1102 pipe_setting(r8a66597, td);
1103 pipe_stop(r8a66597, td->pipe);
1104 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1106 if (td->pipe->pipetre) {
1107 r8a66597_write(r8a66597, TRCLR,
1109 r8a66597_write(r8a66597,
1111 (urb->transfer_buffer_length,
1114 r8a66597_bset(r8a66597, TRENB,
1118 pipe_start(r8a66597, td->pipe);
1119 pipe_irq_enable(r8a66597, urb, td->pipenum);
1124 /* this function must be called with interrupt disabled */
1125 static void prepare_packet_write(struct r8a66597 *r8a66597,
1126 struct r8a66597_td *td)
1129 struct urb *urb = td->urb;
1131 if (usb_pipecontrol(urb->pipe)) {
1132 pipe_stop(r8a66597, td->pipe);
1133 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1134 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1135 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1136 if (urb->actual_length == 0) {
1137 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1138 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1141 if (urb->actual_length == 0)
1142 pipe_setting(r8a66597, td);
1143 if (td->pipe->pipetre)
1144 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1146 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1148 fifo_change_from_pipe(r8a66597, td->pipe);
1149 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1150 if (unlikely((tmp & FRDY) == 0))
1151 pipe_irq_enable(r8a66597, urb, td->pipenum);
1153 packet_write(r8a66597, td->pipenum);
1154 pipe_start(r8a66597, td->pipe);
1157 /* this function must be called with interrupt disabled */
1158 static void prepare_status_packet(struct r8a66597 *r8a66597,
1159 struct r8a66597_td *td)
1161 struct urb *urb = td->urb;
1163 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1164 pipe_stop(r8a66597, td->pipe);
1166 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1167 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1168 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1169 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1170 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
1171 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
1172 enable_irq_empty(r8a66597, 0);
1174 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1175 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1176 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1177 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1178 enable_irq_ready(r8a66597, 0);
1180 enable_irq_nrdy(r8a66597, 0);
1181 pipe_start(r8a66597, td->pipe);
1184 static int is_set_address(unsigned char *setup_packet)
1186 if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1187 setup_packet[1] == USB_REQ_SET_ADDRESS)
1193 /* this function must be called with interrupt disabled */
1194 static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1200 if (is_set_address(td->urb->setup_packet)) {
1201 td->set_address = 1;
1202 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1204 if (td->urb->setup_packet[2] == 0)
1207 prepare_setup_packet(r8a66597, td);
1210 prepare_packet_read(r8a66597, td);
1213 prepare_packet_write(r8a66597, td);
1216 prepare_status_packet(r8a66597, td);
1219 printk(KERN_ERR "r8a66597: invalid type.\n");
1226 static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1228 if (usb_pipeisoc(urb->pipe)) {
1229 if (urb->number_of_packets == td->iso_cnt)
1233 /* control or bulk or interrupt */
1234 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1235 (td->short_packet) || (td->zero_packet))
1241 /* this function must be called with interrupt disabled */
1242 static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1248 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1249 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1250 r8a66597->timeout_map |= 1 << td->pipenum;
1251 switch (usb_pipetype(td->urb->pipe)) {
1252 case PIPE_INTERRUPT:
1253 case PIPE_ISOCHRONOUS:
1261 mod_timer(&r8a66597->timers[td->pipenum].td,
1262 jiffies + msecs_to_jiffies(time));
1266 /* this function must be called with interrupt disabled */
1267 static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
1268 u16 pipenum, struct urb *urb, int status)
1269 __releases(r8a66597->lock) __acquires(r8a66597->lock)
1272 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1274 r8a66597->timeout_map &= ~(1 << pipenum);
1277 if (td->set_address && (status != 0 || urb->unlinked))
1278 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1280 pipe_toggle_save(r8a66597, td->pipe, urb);
1281 list_del(&td->queue);
1285 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1289 if (usb_pipeisoc(urb->pipe))
1290 urb->start_frame = r8a66597_get_frame(hcd);
1292 r8a66597_urb_done(r8a66597, urb, status);
1296 td = r8a66597_get_td(r8a66597, pipenum);
1300 start_transfer(r8a66597, td);
1301 set_td_timer(r8a66597, td);
1305 static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1308 int rcv_len, bufsize, urb_len, size;
1310 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1319 fifo_change_from_pipe(r8a66597, td->pipe);
1320 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1321 if (unlikely((tmp & FRDY) == 0)) {
1322 pipe_stop(r8a66597, td->pipe);
1323 pipe_irq_disable(r8a66597, pipenum);
1324 printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
1325 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
1329 /* prepare parameters */
1330 rcv_len = tmp & DTLN;
1331 if (usb_pipeisoc(urb->pipe)) {
1332 buf = (u16 *)(urb->transfer_buffer +
1333 urb->iso_frame_desc[td->iso_cnt].offset);
1334 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1336 buf = (void *)urb->transfer_buffer + urb->actual_length;
1337 urb_len = urb->transfer_buffer_length - urb->actual_length;
1339 bufsize = min(urb_len, (int) td->maxpacket);
1340 if (rcv_len <= bufsize) {
1344 status = -EOVERFLOW;
1348 /* update parameters */
1349 urb->actual_length += size;
1351 td->zero_packet = 1;
1352 if (rcv_len < bufsize) {
1353 td->short_packet = 1;
1355 if (usb_pipeisoc(urb->pipe)) {
1356 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1357 urb->iso_frame_desc[td->iso_cnt].status = status;
1362 /* check transfer finish */
1363 if (finish || check_transfer_finish(td, urb)) {
1364 pipe_stop(r8a66597, td->pipe);
1365 pipe_irq_disable(r8a66597, pipenum);
1370 if (urb->transfer_buffer) {
1372 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1374 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1378 if (finish && pipenum != 0)
1379 finish_request(r8a66597, td, pipenum, urb, status);
1382 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1387 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1394 fifo_change_from_pipe(r8a66597, td->pipe);
1395 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1396 if (unlikely((tmp & FRDY) == 0)) {
1397 pipe_stop(r8a66597, td->pipe);
1398 pipe_irq_disable(r8a66597, pipenum);
1399 printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
1400 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
1404 /* prepare parameters */
1405 bufsize = td->maxpacket;
1406 if (usb_pipeisoc(urb->pipe)) {
1407 buf = (u16 *)(urb->transfer_buffer +
1408 urb->iso_frame_desc[td->iso_cnt].offset);
1410 (int)urb->iso_frame_desc[td->iso_cnt].length);
1412 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
1413 size = min_t(u32, bufsize,
1414 urb->transfer_buffer_length - urb->actual_length);
1419 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
1420 if (urb->transfer_buffer) {
1421 r8a66597_write_fifo(r8a66597, td->pipe, buf, size);
1422 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1423 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1426 /* update parameters */
1427 urb->actual_length += size;
1428 if (usb_pipeisoc(urb->pipe)) {
1429 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1430 urb->iso_frame_desc[td->iso_cnt].status = 0;
1434 /* check transfer finish */
1435 if (check_transfer_finish(td, urb)) {
1436 disable_irq_ready(r8a66597, pipenum);
1437 enable_irq_empty(r8a66597, pipenum);
1438 if (!usb_pipeisoc(urb->pipe))
1439 enable_irq_nrdy(r8a66597, pipenum);
1441 pipe_irq_enable(r8a66597, urb, pipenum);
1445 static void check_next_phase(struct r8a66597 *r8a66597, int status)
1447 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1458 if (check_transfer_finish(td, urb))
1459 td->type = USB_PID_ACK;
1462 if (urb->transfer_buffer_length == urb->actual_length)
1463 td->type = USB_PID_ACK;
1464 else if (usb_pipeout(urb->pipe))
1465 td->type = USB_PID_OUT;
1467 td->type = USB_PID_IN;
1474 if (finish || status != 0 || urb->unlinked)
1475 finish_request(r8a66597, td, 0, urb, status);
1477 start_transfer(r8a66597, td);
1480 static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
1482 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1485 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1495 static void irq_pipe_ready(struct r8a66597 *r8a66597)
1500 struct r8a66597_td *td;
1502 mask = r8a66597_read(r8a66597, BRDYSTS)
1503 & r8a66597_read(r8a66597, BRDYENB);
1504 r8a66597_write(r8a66597, ~mask, BRDYSTS);
1506 td = r8a66597_get_td(r8a66597, 0);
1507 if (td && td->type == USB_PID_IN)
1508 packet_read(r8a66597, 0);
1510 pipe_irq_disable(r8a66597, 0);
1511 check_next_phase(r8a66597, 0);
1514 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1515 check = 1 << pipenum;
1517 td = r8a66597_get_td(r8a66597, pipenum);
1521 if (td->type == USB_PID_IN)
1522 packet_read(r8a66597, pipenum);
1523 else if (td->type == USB_PID_OUT)
1524 packet_write(r8a66597, pipenum);
1529 static void irq_pipe_empty(struct r8a66597 *r8a66597)
1535 struct r8a66597_td *td;
1537 mask = r8a66597_read(r8a66597, BEMPSTS)
1538 & r8a66597_read(r8a66597, BEMPENB);
1539 r8a66597_write(r8a66597, ~mask, BEMPSTS);
1541 cfifo_change(r8a66597, 0);
1542 td = r8a66597_get_td(r8a66597, 0);
1543 if (td && td->type != USB_PID_OUT)
1544 disable_irq_empty(r8a66597, 0);
1545 check_next_phase(r8a66597, 0);
1548 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1549 check = 1 << pipenum;
1551 struct r8a66597_td *td;
1552 td = r8a66597_get_td(r8a66597, pipenum);
1556 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1557 if ((tmp & INBUFM) == 0) {
1558 disable_irq_empty(r8a66597, pipenum);
1559 pipe_irq_disable(r8a66597, pipenum);
1560 finish_request(r8a66597, td, pipenum, td->urb,
1567 static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1574 mask = r8a66597_read(r8a66597, NRDYSTS)
1575 & r8a66597_read(r8a66597, NRDYENB);
1576 r8a66597_write(r8a66597, ~mask, NRDYSTS);
1578 cfifo_change(r8a66597, 0);
1579 status = get_urb_error(r8a66597, 0);
1580 pipe_irq_disable(r8a66597, 0);
1581 check_next_phase(r8a66597, status);
1584 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1585 check = 1 << pipenum;
1587 struct r8a66597_td *td;
1588 td = r8a66597_get_td(r8a66597, pipenum);
1592 status = get_urb_error(r8a66597, pipenum);
1593 pipe_irq_disable(r8a66597, pipenum);
1594 pipe_stop(r8a66597, td->pipe);
1595 finish_request(r8a66597, td, pipenum, td->urb, status);
1600 static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1602 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1603 u16 intsts0, intsts1, intsts2;
1604 u16 intenb0, intenb1, intenb2;
1605 u16 mask0, mask1, mask2;
1608 spin_lock(&r8a66597->lock);
1610 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1611 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1612 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1613 intenb0 = r8a66597_read(r8a66597, INTENB0);
1614 intenb1 = r8a66597_read(r8a66597, INTENB1);
1615 intenb2 = r8a66597_read(r8a66597, INTENB2);
1617 mask2 = intsts2 & intenb2;
1618 mask1 = intsts1 & intenb1;
1619 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1621 if (mask2 & ATTCH) {
1622 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
1623 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1625 /* start usb bus sampling */
1626 start_root_hub_sampling(r8a66597, 1, 1);
1629 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
1630 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1631 r8a66597_usb_disconnect(r8a66597, 1);
1634 r8a66597_write(r8a66597, ~BCHG, INTSTS2);
1635 r8a66597_bclr(r8a66597, BCHGE, INTENB2);
1636 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1641 if (mask1 & ATTCH) {
1642 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
1643 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1645 /* start usb bus sampling */
1646 start_root_hub_sampling(r8a66597, 0, 1);
1649 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
1650 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1651 r8a66597_usb_disconnect(r8a66597, 0);
1654 r8a66597_write(r8a66597, ~BCHG, INTSTS1);
1655 r8a66597_bclr(r8a66597, BCHGE, INTENB1);
1656 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1660 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
1661 status = get_urb_error(r8a66597, 0);
1662 check_next_phase(r8a66597, status);
1665 r8a66597_write(r8a66597, ~SACK, INTSTS1);
1666 check_next_phase(r8a66597, 0);
1671 irq_pipe_ready(r8a66597);
1673 irq_pipe_empty(r8a66597);
1675 irq_pipe_nrdy(r8a66597);
1678 spin_unlock(&r8a66597->lock);
1682 /* this function must be called with interrupt disabled */
1683 static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1686 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1688 if (rh->port & USB_PORT_STAT_RESET) {
1689 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1691 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1692 if ((tmp & USBRST) == USBRST) {
1693 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1695 r8a66597_root_hub_start_polling(r8a66597);
1697 r8a66597_usb_connect(r8a66597, port);
1700 if (!(rh->port & USB_PORT_STAT_CONNECTION)) {
1701 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1702 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1705 if (rh->scount > 0) {
1706 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1707 if (tmp == rh->old_syssts) {
1709 if (rh->scount == 0)
1710 r8a66597_check_syssts(r8a66597, port, tmp);
1712 r8a66597_root_hub_start_polling(r8a66597);
1714 rh->scount = R8A66597_MAX_SAMPLING;
1715 rh->old_syssts = tmp;
1716 r8a66597_root_hub_start_polling(r8a66597);
1721 static void r8a66597_interval_timer(struct timer_list *t)
1723 struct r8a66597_timers *timers = from_timer(timers, t, interval);
1724 struct r8a66597 *r8a66597 = timers->r8a66597;
1725 unsigned long flags;
1727 struct r8a66597_td *td;
1729 spin_lock_irqsave(&r8a66597->lock, flags);
1731 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1732 if (!(r8a66597->interval_map & (1 << pipenum)))
1734 if (timer_pending(&r8a66597->timers[pipenum].interval))
1737 td = r8a66597_get_td(r8a66597, pipenum);
1739 start_transfer(r8a66597, td);
1742 spin_unlock_irqrestore(&r8a66597->lock, flags);
1745 static void r8a66597_td_timer(struct timer_list *t)
1747 struct r8a66597_timers *timers = from_timer(timers, t, td);
1748 struct r8a66597 *r8a66597 = timers->r8a66597;
1749 unsigned long flags;
1751 struct r8a66597_td *td, *new_td = NULL;
1752 struct r8a66597_pipe *pipe;
1754 spin_lock_irqsave(&r8a66597->lock, flags);
1755 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1756 if (!(r8a66597->timeout_map & (1 << pipenum)))
1758 if (timer_pending(&r8a66597->timers[pipenum].td))
1761 td = r8a66597_get_td(r8a66597, pipenum);
1763 r8a66597->timeout_map &= ~(1 << pipenum);
1767 if (td->urb->actual_length) {
1768 set_td_timer(r8a66597, td);
1773 pipe_stop(r8a66597, pipe);
1775 /* Select a different address or endpoint */
1778 list_move_tail(&new_td->queue,
1779 &r8a66597->pipe_queue[pipenum]);
1780 new_td = r8a66597_get_td(r8a66597, pipenum);
1785 } while (td != new_td && td->address == new_td->address &&
1786 td->pipe->info.epnum == new_td->pipe->info.epnum);
1788 start_transfer(r8a66597, new_td);
1791 r8a66597->timeout_map &= ~(1 << pipenum);
1793 set_td_timer(r8a66597, new_td);
1796 spin_unlock_irqrestore(&r8a66597->lock, flags);
1799 static void r8a66597_timer(struct timer_list *t)
1801 struct r8a66597 *r8a66597 = from_timer(r8a66597, t, rh_timer);
1802 unsigned long flags;
1805 spin_lock_irqsave(&r8a66597->lock, flags);
1807 for (port = 0; port < r8a66597->max_root_hub; port++)
1808 r8a66597_root_hub_control(r8a66597, port);
1810 spin_unlock_irqrestore(&r8a66597->lock, flags);
1813 static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1815 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1817 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1818 (urb->dev->state == USB_STATE_CONFIGURED))
1824 static int r8a66597_start(struct usb_hcd *hcd)
1826 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1828 hcd->state = HC_STATE_RUNNING;
1829 return enable_controller(r8a66597);
1832 static void r8a66597_stop(struct usb_hcd *hcd)
1834 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1836 disable_controller(r8a66597);
1839 static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1841 unsigned int usb_address = usb_pipedevice(urb->pipe);
1842 u16 root_port, hub_port;
1844 if (usb_address == 0) {
1845 get_port_number(r8a66597, urb->dev->devpath,
1846 &root_port, &hub_port);
1847 set_devadd_reg(r8a66597, 0,
1848 get_r8a66597_usb_speed(urb->dev->speed),
1849 get_parent_r8a66597_address(r8a66597, urb->dev),
1850 hub_port, root_port);
1854 static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1856 struct usb_host_endpoint *hep)
1858 struct r8a66597_td *td;
1861 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
1865 pipenum = r8a66597_get_pipenum(urb, hep);
1866 td->pipenum = pipenum;
1867 td->pipe = hep->hcpriv;
1869 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1870 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1871 !usb_pipein(urb->pipe));
1872 if (usb_pipecontrol(urb->pipe))
1873 td->type = USB_PID_SETUP;
1874 else if (usb_pipein(urb->pipe))
1875 td->type = USB_PID_IN;
1877 td->type = USB_PID_OUT;
1878 INIT_LIST_HEAD(&td->queue);
1883 static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
1887 struct usb_host_endpoint *hep = urb->ep;
1888 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1889 struct r8a66597_td *td = NULL;
1890 int ret, request = 0;
1891 unsigned long flags;
1893 spin_lock_irqsave(&r8a66597->lock, flags);
1894 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1896 goto error_not_linked;
1899 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1901 goto error_not_linked;
1904 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1910 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1911 if (usb_pipeendpoint(urb->pipe))
1912 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1915 if (unlikely(check_pipe_config(r8a66597, urb)))
1916 init_pipe_config(r8a66597, urb);
1918 set_address_zero(r8a66597, urb);
1919 td = r8a66597_make_td(r8a66597, urb, hep);
1924 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1926 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
1930 if (td->pipe->info.timer_interval) {
1931 r8a66597->interval_map |= 1 << td->pipenum;
1932 mod_timer(&r8a66597->timers[td->pipenum].interval,
1933 jiffies + msecs_to_jiffies(
1934 td->pipe->info.timer_interval));
1936 ret = start_transfer(r8a66597, td);
1938 list_del(&td->queue);
1943 set_td_timer(r8a66597, td);
1947 usb_hcd_unlink_urb_from_ep(hcd, urb);
1949 spin_unlock_irqrestore(&r8a66597->lock, flags);
1953 static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1956 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1957 struct r8a66597_td *td;
1958 unsigned long flags;
1961 spin_lock_irqsave(&r8a66597->lock, flags);
1962 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1968 pipe_stop(r8a66597, td->pipe);
1969 pipe_irq_disable(r8a66597, td->pipenum);
1970 disable_irq_empty(r8a66597, td->pipenum);
1971 finish_request(r8a66597, td, td->pipenum, urb, status);
1974 spin_unlock_irqrestore(&r8a66597->lock, flags);
1978 static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1979 struct usb_host_endpoint *hep)
1980 __acquires(r8a66597->lock)
1981 __releases(r8a66597->lock)
1983 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1984 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1985 struct r8a66597_td *td;
1986 struct urb *urb = NULL;
1988 unsigned long flags;
1992 pipenum = pipe->info.pipenum;
1994 spin_lock_irqsave(&r8a66597->lock, flags);
1998 spin_unlock_irqrestore(&r8a66597->lock, flags);
2002 pipe_stop(r8a66597, pipe);
2003 pipe_irq_disable(r8a66597, pipenum);
2004 disable_irq_empty(r8a66597, pipenum);
2005 td = r8a66597_get_td(r8a66597, pipenum);
2008 finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
2011 spin_unlock_irqrestore(&r8a66597->lock, flags);
2014 static int r8a66597_get_frame(struct usb_hcd *hcd)
2016 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2017 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
2020 static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
2023 struct usb_device *childdev;
2025 if (udev->state == USB_STATE_CONFIGURED &&
2026 udev->parent && udev->parent->devnum > 1 &&
2027 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
2028 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
2030 usb_hub_for_each_child(udev, chix, childdev)
2031 collect_usb_address_map(childdev, map);
2034 /* this function must be called with interrupt disabled */
2035 static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
2038 struct r8a66597_device *dev;
2039 struct list_head *list = &r8a66597->child_device;
2041 list_for_each_entry(dev, list, device_list) {
2042 if (dev->usb_address != addr)
2048 printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
2052 static void update_usb_address_map(struct r8a66597 *r8a66597,
2053 struct usb_device *root_hub,
2058 unsigned long flags;
2060 for (i = 0; i < 4; i++) {
2061 diff = r8a66597->child_connect_map[i] ^ map[i];
2065 for (j = 0; j < 32; j++) {
2066 if (!(diff & (1 << j)))
2070 if (map[i] & (1 << j))
2071 set_child_connect_map(r8a66597, addr);
2073 struct r8a66597_device *dev;
2075 spin_lock_irqsave(&r8a66597->lock, flags);
2076 dev = get_r8a66597_device(r8a66597, addr);
2077 disable_r8a66597_pipe_all(r8a66597, dev);
2078 free_usb_address(r8a66597, dev, 0);
2079 put_child_connect_map(r8a66597, addr);
2080 spin_unlock_irqrestore(&r8a66597->lock, flags);
2086 static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
2087 struct usb_hcd *hcd)
2089 struct usb_bus *bus;
2090 unsigned long now_map[4];
2092 memset(now_map, 0, sizeof(now_map));
2094 mutex_lock(&usb_bus_idr_lock);
2095 bus = idr_find(&usb_bus_idr, hcd->self.busnum);
2096 if (bus && bus->root_hub) {
2097 collect_usb_address_map(bus->root_hub, now_map);
2098 update_usb_address_map(r8a66597, bus->root_hub, now_map);
2100 mutex_unlock(&usb_bus_idr_lock);
2103 static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2105 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2106 unsigned long flags;
2109 r8a66597_check_detect_child(r8a66597, hcd);
2111 spin_lock_irqsave(&r8a66597->lock, flags);
2113 *buf = 0; /* initialize (no change) */
2115 for (i = 0; i < r8a66597->max_root_hub; i++) {
2116 if (r8a66597->root_hub[i].port & 0xffff0000)
2117 *buf |= 1 << (i + 1);
2120 spin_unlock_irqrestore(&r8a66597->lock, flags);
2125 static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2126 struct usb_hub_descriptor *desc)
2128 desc->bDescriptorType = USB_DT_HUB;
2129 desc->bHubContrCurrent = 0;
2130 desc->bNbrPorts = r8a66597->max_root_hub;
2131 desc->bDescLength = 9;
2132 desc->bPwrOn2PwrGood = 0;
2133 desc->wHubCharacteristics =
2134 cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_NO_OCPM);
2135 desc->u.hs.DeviceRemovable[0] =
2136 ((1 << r8a66597->max_root_hub) - 1) << 1;
2137 desc->u.hs.DeviceRemovable[1] = ~0;
2140 static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2141 u16 wIndex, char *buf, u16 wLength)
2143 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2145 int port = (wIndex & 0x00FF) - 1;
2146 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2147 unsigned long flags;
2151 spin_lock_irqsave(&r8a66597->lock, flags);
2153 case ClearHubFeature:
2156 case C_HUB_OVER_CURRENT:
2157 case C_HUB_LOCAL_POWER:
2163 case ClearPortFeature:
2164 if (wIndex > r8a66597->max_root_hub)
2170 case USB_PORT_FEAT_ENABLE:
2171 rh->port &= ~USB_PORT_STAT_POWER;
2173 case USB_PORT_FEAT_SUSPEND:
2175 case USB_PORT_FEAT_POWER:
2176 r8a66597_port_power(r8a66597, port, 0);
2178 case USB_PORT_FEAT_C_ENABLE:
2179 case USB_PORT_FEAT_C_SUSPEND:
2180 case USB_PORT_FEAT_C_CONNECTION:
2181 case USB_PORT_FEAT_C_OVER_CURRENT:
2182 case USB_PORT_FEAT_C_RESET:
2187 rh->port &= ~(1 << wValue);
2189 case GetHubDescriptor:
2190 r8a66597_hub_descriptor(r8a66597,
2191 (struct usb_hub_descriptor *)buf);
2197 if (wIndex > r8a66597->max_root_hub)
2199 *(__le32 *)buf = cpu_to_le32(rh->port);
2201 case SetPortFeature:
2202 if (wIndex > r8a66597->max_root_hub)
2208 case USB_PORT_FEAT_SUSPEND:
2210 case USB_PORT_FEAT_POWER:
2211 r8a66597_port_power(r8a66597, port, 1);
2212 rh->port |= USB_PORT_STAT_POWER;
2214 case USB_PORT_FEAT_RESET: {
2215 struct r8a66597_device *dev = rh->dev;
2217 rh->port |= USB_PORT_STAT_RESET;
2219 disable_r8a66597_pipe_all(r8a66597, dev);
2220 free_usb_address(r8a66597, dev, 1);
2222 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2223 get_dvstctr_reg(port));
2224 mod_timer(&r8a66597->rh_timer,
2225 jiffies + msecs_to_jiffies(50));
2231 rh->port |= 1 << wValue;
2239 spin_unlock_irqrestore(&r8a66597->lock, flags);
2243 #if defined(CONFIG_PM)
2244 static int r8a66597_bus_suspend(struct usb_hcd *hcd)
2246 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2249 dev_dbg(&r8a66597->device0.udev->dev, "%s\n", __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 & USB_PORT_STAT_ENABLE))
2258 dev_dbg(&rh->dev->udev->dev, "suspend port = %d\n", port);
2259 r8a66597_bclr(r8a66597, UACT, dvstctr_reg); /* suspend */
2260 rh->port |= USB_PORT_STAT_SUSPEND;
2262 if (rh->dev->udev->do_remote_wakeup) {
2263 msleep(3); /* waiting last SOF */
2264 r8a66597_bset(r8a66597, RWUPE, dvstctr_reg);
2265 r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
2266 r8a66597_bset(r8a66597, BCHGE, get_intenb_reg(port));
2270 r8a66597->bus_suspended = 1;
2275 static int r8a66597_bus_resume(struct usb_hcd *hcd)
2277 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2280 dev_dbg(&r8a66597->device0.udev->dev, "%s\n", __func__);
2282 for (port = 0; port < r8a66597->max_root_hub; port++) {
2283 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2284 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2286 if (!(rh->port & USB_PORT_STAT_SUSPEND))
2289 dev_dbg(&rh->dev->udev->dev, "resume port = %d\n", port);
2290 rh->port &= ~USB_PORT_STAT_SUSPEND;
2291 rh->port |= USB_PORT_STAT_C_SUSPEND << 16;
2292 r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
2293 msleep(USB_RESUME_TIMEOUT);
2294 r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
2301 #define r8a66597_bus_suspend NULL
2302 #define r8a66597_bus_resume NULL
2305 static const struct hc_driver r8a66597_hc_driver = {
2306 .description = hcd_name,
2307 .hcd_priv_size = sizeof(struct r8a66597),
2308 .irq = r8a66597_irq,
2311 * generic hardware linkage
2315 .start = r8a66597_start,
2316 .stop = r8a66597_stop,
2319 * managing i/o requests and associated device resources
2321 .urb_enqueue = r8a66597_urb_enqueue,
2322 .urb_dequeue = r8a66597_urb_dequeue,
2323 .endpoint_disable = r8a66597_endpoint_disable,
2326 * periodic schedule support
2328 .get_frame_number = r8a66597_get_frame,
2333 .hub_status_data = r8a66597_hub_status_data,
2334 .hub_control = r8a66597_hub_control,
2335 .bus_suspend = r8a66597_bus_suspend,
2336 .bus_resume = r8a66597_bus_resume,
2339 #if defined(CONFIG_PM)
2340 static int r8a66597_suspend(struct device *dev)
2342 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
2345 dev_dbg(dev, "%s\n", __func__);
2347 disable_controller(r8a66597);
2349 for (port = 0; port < r8a66597->max_root_hub; port++) {
2350 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2352 rh->port = 0x00000000;
2358 static int r8a66597_resume(struct device *dev)
2360 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
2361 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2363 dev_dbg(dev, "%s\n", __func__);
2365 enable_controller(r8a66597);
2366 usb_root_hub_lost_power(hcd->self.root_hub);
2371 static const struct dev_pm_ops r8a66597_dev_pm_ops = {
2372 .suspend = r8a66597_suspend,
2373 .resume = r8a66597_resume,
2374 .poweroff = r8a66597_suspend,
2375 .restore = r8a66597_resume,
2378 #define R8A66597_DEV_PM_OPS (&r8a66597_dev_pm_ops)
2379 #else /* if defined(CONFIG_PM) */
2380 #define R8A66597_DEV_PM_OPS NULL
2383 static int r8a66597_remove(struct platform_device *pdev)
2385 struct r8a66597 *r8a66597 = platform_get_drvdata(pdev);
2386 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2388 del_timer_sync(&r8a66597->rh_timer);
2389 usb_remove_hcd(hcd);
2390 iounmap(r8a66597->reg);
2391 if (r8a66597->pdata->on_chip)
2392 clk_put(r8a66597->clk);
2397 static int r8a66597_probe(struct platform_device *pdev)
2400 struct resource *res = NULL, *ires;
2402 void __iomem *reg = NULL;
2403 struct usb_hcd *hcd = NULL;
2404 struct r8a66597 *r8a66597;
2407 unsigned long irq_trigger;
2412 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2415 dev_err(&pdev->dev, "platform_get_resource error.\n");
2419 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2423 "platform_get_resource IORESOURCE_IRQ error.\n");
2428 irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
2430 reg = ioremap(res->start, resource_size(res));
2433 dev_err(&pdev->dev, "ioremap error.\n");
2437 if (pdev->dev.platform_data == NULL) {
2438 dev_err(&pdev->dev, "no platform data\n");
2443 /* initialize hcd */
2444 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2447 dev_err(&pdev->dev, "Failed to create hcd\n");
2450 r8a66597 = hcd_to_r8a66597(hcd);
2451 memset(r8a66597, 0, sizeof(struct r8a66597));
2452 platform_set_drvdata(pdev, r8a66597);
2453 r8a66597->pdata = dev_get_platdata(&pdev->dev);
2454 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
2456 if (r8a66597->pdata->on_chip) {
2457 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
2458 r8a66597->clk = clk_get(&pdev->dev, clk_name);
2459 if (IS_ERR(r8a66597->clk)) {
2460 dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
2462 ret = PTR_ERR(r8a66597->clk);
2465 r8a66597->max_root_hub = 1;
2467 r8a66597->max_root_hub = 2;
2469 spin_lock_init(&r8a66597->lock);
2470 timer_setup(&r8a66597->rh_timer, r8a66597_timer, 0);
2471 r8a66597->reg = reg;
2473 /* make sure no interrupts are pending */
2474 ret = r8a66597_clock_enable(r8a66597);
2477 disable_controller(r8a66597);
2479 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2480 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2481 r8a66597->timers[i].r8a66597 = r8a66597;
2482 timer_setup(&r8a66597->timers[i].td, r8a66597_td_timer, 0);
2483 timer_setup(&r8a66597->timers[i].interval,
2484 r8a66597_interval_timer, 0);
2486 INIT_LIST_HEAD(&r8a66597->child_device);
2488 hcd->rsrc_start = res->start;
2491 ret = usb_add_hcd(hcd, irq, irq_trigger);
2493 dev_err(&pdev->dev, "Failed to add hcd\n");
2496 device_wakeup_enable(hcd->self.controller);
2501 if (r8a66597->pdata->on_chip)
2502 clk_put(r8a66597->clk);
2513 static struct platform_driver r8a66597_driver = {
2514 .probe = r8a66597_probe,
2515 .remove = r8a66597_remove,
2518 .pm = R8A66597_DEV_PM_OPS,
2522 module_platform_driver(r8a66597_driver);