Merge master.kernel.org:/home/rmk/linux-2.6-arm
[linux-2.6-block.git] / drivers / usb / host / r8a66597-hcd.c
CommitLineData
5d304358
YS
1/*
2 * R8A66597 HCD (Host Controller Driver)
3 *
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
8 *
9 * Author : Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com>
10 *
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.
14 *
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.
19 *
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
23 *
24 */
25
26#include <linux/module.h>
27#include <linux/kernel.h>
28#include <linux/sched.h>
5d304358
YS
29#include <linux/errno.h>
30#include <linux/init.h>
31#include <linux/timer.h>
32#include <linux/delay.h>
33#include <linux/list.h>
34#include <linux/interrupt.h>
35#include <linux/usb.h>
36#include <linux/platform_device.h>
e294531d
YS
37#include <linux/io.h>
38#include <linux/irq.h>
5d304358
YS
39
40#include "../core/hcd.h"
41#include "r8a66597.h"
42
43MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
44MODULE_LICENSE("GPL");
45MODULE_AUTHOR("Yoshihiro Shimoda");
f4fce61d 46MODULE_ALIAS("platform:r8a66597_hcd");
5d304358 47
5effabbe 48#define DRIVER_VERSION "2009-05-26"
5d304358
YS
49
50static const char hcd_name[] = "r8a66597_hcd";
51
5d304358
YS
52static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
53static int r8a66597_get_frame(struct usb_hcd *hcd);
54
55/* this function must be called with interrupt disabled */
56static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
57 unsigned long reg)
58{
59 u16 tmp;
60
61 tmp = r8a66597_read(r8a66597, INTENB0);
62 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
63 r8a66597_bset(r8a66597, 1 << pipenum, reg);
64 r8a66597_write(r8a66597, tmp, INTENB0);
65}
66
67/* this function must be called with interrupt disabled */
68static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
69 unsigned long reg)
70{
71 u16 tmp;
72
73 tmp = r8a66597_read(r8a66597, INTENB0);
74 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
75 r8a66597_bclr(r8a66597, 1 << pipenum, reg);
76 r8a66597_write(r8a66597, tmp, INTENB0);
77}
78
79static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
80 u16 usbspd, u8 upphub, u8 hubport, int port)
81{
82 u16 val;
83 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
84
85 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
86 r8a66597_write(r8a66597, val, devadd_reg);
87}
88
9424ea29 89static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
5d304358
YS
90{
91 u16 tmp;
92 int i = 0;
93
719a72b7
MD
94 if (r8a66597->pdata->on_chip) {
95#ifdef CONFIG_HAVE_CLK
96 clk_enable(r8a66597->clk);
765786e0 97#endif
719a72b7
MD
98 do {
99 r8a66597_write(r8a66597, SCKE, SYSCFG0);
100 tmp = r8a66597_read(r8a66597, SYSCFG0);
101 if (i++ > 1000) {
102 printk(KERN_ERR "r8a66597: reg access fail.\n");
103 return -ENXIO;
104 }
105 } while ((tmp & SCKE) != SCKE);
106 r8a66597_write(r8a66597, 0x04, 0x02);
107 } else {
108 do {
109 r8a66597_write(r8a66597, USBE, SYSCFG0);
110 tmp = r8a66597_read(r8a66597, SYSCFG0);
111 if (i++ > 1000) {
112 printk(KERN_ERR "r8a66597: reg access fail.\n");
113 return -ENXIO;
114 }
115 } while ((tmp & USBE) != USBE);
116 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
117 r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata),
118 XTAL, SYSCFG0);
5d304358 119
719a72b7
MD
120 i = 0;
121 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
122 do {
123 msleep(1);
124 tmp = r8a66597_read(r8a66597, SYSCFG0);
125 if (i++ > 500) {
126 printk(KERN_ERR "r8a66597: reg access fail.\n");
127 return -ENXIO;
128 }
129 } while ((tmp & SCKE) != SCKE);
130 }
9424ea29
YS
131
132 return 0;
133}
134
135static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
136{
137 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
138 udelay(1);
719a72b7
MD
139
140 if (r8a66597->pdata->on_chip) {
141#ifdef CONFIG_HAVE_CLK
142 clk_disable(r8a66597->clk);
9424ea29 143#endif
719a72b7
MD
144 } else {
145 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
146 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
147 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
148 }
9424ea29
YS
149}
150
151static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
152{
153 u16 val;
154
155 val = port ? DRPD : DCFM | DRPD;
156 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
157 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
158
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));
162}
163
164static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
165{
166 u16 val, tmp;
167
168 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
169 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
5d304358 170
9424ea29
YS
171 r8a66597_port_power(r8a66597, port, 0);
172
173 do {
174 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
175 udelay(640);
176 } while (tmp == EDGESTS);
177
178 val = port ? DRPD : DCFM | DRPD;
179 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
180 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
181}
182
183static int enable_controller(struct r8a66597 *r8a66597)
184{
185 int ret, port;
5effabbe
YS
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;
9424ea29
YS
189
190 ret = r8a66597_clock_enable(r8a66597);
191 if (ret < 0)
192 return ret;
5d304358
YS
193
194 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
5d304358
YS
195 r8a66597_bset(r8a66597, USBE, SYSCFG0);
196
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);
201
5d304358
YS
202 r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
203 r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
204 r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
5d304358
YS
205 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
206
207 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
9424ea29 208
719a72b7 209 for (port = 0; port < r8a66597->max_root_hub; port++)
9424ea29 210 r8a66597_enable_port(r8a66597, port);
5d304358
YS
211
212 return 0;
213}
214
215static void disable_controller(struct r8a66597 *r8a66597)
216{
9424ea29 217 int port;
5d304358 218
e5ff15be 219 /* disable interrupts */
5d304358 220 r8a66597_write(r8a66597, 0, INTENB0);
e5ff15be
MD
221 r8a66597_write(r8a66597, 0, INTENB1);
222 r8a66597_write(r8a66597, 0, BRDYENB);
223 r8a66597_write(r8a66597, 0, BEMPENB);
224 r8a66597_write(r8a66597, 0, NRDYENB);
225
226 /* clear status */
227 r8a66597_write(r8a66597, 0, BRDYSTS);
228 r8a66597_write(r8a66597, 0, NRDYSTS);
229 r8a66597_write(r8a66597, 0, BEMPSTS);
5d304358 230
719a72b7 231 for (port = 0; port < r8a66597->max_root_hub; port++)
9424ea29 232 r8a66597_disable_port(r8a66597, port);
5d304358 233
9424ea29 234 r8a66597_clock_disable(r8a66597);
5d304358
YS
235}
236
237static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
238 struct usb_device *udev)
239{
240 struct r8a66597_device *dev;
241
242 if (udev->parent && udev->parent->devnum != 1)
243 udev = udev->parent;
244
245 dev = dev_get_drvdata(&udev->dev);
246 if (dev)
247 return dev->address;
248 else
249 return 0;
250}
251
252static int is_child_device(char *devpath)
253{
254 return (devpath[2] ? 1 : 0);
255}
256
257static int is_hub_limit(char *devpath)
258{
259 return ((strlen(devpath) >= 4) ? 1 : 0);
260}
261
719a72b7
MD
262static void get_port_number(struct r8a66597 *r8a66597,
263 char *devpath, u16 *root_port, u16 *hub_port)
5d304358
YS
264{
265 if (root_port) {
266 *root_port = (devpath[0] & 0x0F) - 1;
719a72b7 267 if (*root_port >= r8a66597->max_root_hub)
802f389a 268 printk(KERN_ERR "r8a66597: Illegal root port number.\n");
5d304358
YS
269 }
270 if (hub_port)
271 *hub_port = devpath[2] & 0x0F;
272}
273
274static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
275{
276 u16 usbspd = 0;
277
278 switch (speed) {
279 case USB_SPEED_LOW:
280 usbspd = LSMODE;
281 break;
282 case USB_SPEED_FULL:
283 usbspd = FSMODE;
284 break;
285 case USB_SPEED_HIGH:
286 usbspd = HSMODE;
287 break;
288 default:
802f389a 289 printk(KERN_ERR "r8a66597: unknown speed\n");
5d304358
YS
290 break;
291 }
292
293 return usbspd;
294}
295
296static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
297{
298 int idx;
299
300 idx = address / 32;
301 r8a66597->child_connect_map[idx] |= 1 << (address % 32);
302}
303
304static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
305{
306 int idx;
307
308 idx = address / 32;
309 r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
310}
311
312static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
313{
314 u16 pipenum = pipe->info.pipenum;
fe9b9034
ML
315 const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
316 const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
317 const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
5d304358
YS
318
319 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
320 dma_ch = R8A66597_PIPE_NO_DMA;
321
322 pipe->fifoaddr = fifoaddr[dma_ch];
323 pipe->fifosel = fifosel[dma_ch];
324 pipe->fifoctr = fifoctr[dma_ch];
325
326 if (pipenum == 0)
327 pipe->pipectr = DCPCTR;
328 else
329 pipe->pipectr = get_pipectr_addr(pipenum);
330
331 if (check_bulk_or_isoc(pipenum)) {
332 pipe->pipetre = get_pipetre_addr(pipenum);
333 pipe->pipetrn = get_pipetrn_addr(pipenum);
334 } else {
335 pipe->pipetre = 0;
336 pipe->pipetrn = 0;
337 }
338}
339
340static struct r8a66597_device *
341get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
342{
343 if (usb_pipedevice(urb->pipe) == 0)
344 return &r8a66597->device0;
345
346 return dev_get_drvdata(&urb->dev->dev);
347}
348
349static int make_r8a66597_device(struct r8a66597 *r8a66597,
350 struct urb *urb, u8 addr)
351{
352 struct r8a66597_device *dev;
353 int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
354
e294531d 355 dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
5d304358
YS
356 if (dev == NULL)
357 return -ENOMEM;
358
359 dev_set_drvdata(&urb->dev->dev, dev);
360 dev->udev = urb->dev;
361 dev->address = addr;
362 dev->usb_address = usb_address;
363 dev->state = USB_STATE_ADDRESS;
364 dev->ep_in_toggle = 0;
365 dev->ep_out_toggle = 0;
366 INIT_LIST_HEAD(&dev->device_list);
367 list_add_tail(&dev->device_list, &r8a66597->child_device);
368
719a72b7
MD
369 get_port_number(r8a66597, urb->dev->devpath,
370 &dev->root_port, &dev->hub_port);
5d304358
YS
371 if (!is_child_device(urb->dev->devpath))
372 r8a66597->root_hub[dev->root_port].dev = dev;
373
374 set_devadd_reg(r8a66597, dev->address,
375 get_r8a66597_usb_speed(urb->dev->speed),
376 get_parent_r8a66597_address(r8a66597, urb->dev),
377 dev->hub_port, dev->root_port);
378
379 return 0;
380}
381
382/* this function must be called with interrupt disabled */
383static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
384{
385 u8 addr; /* R8A66597's address */
386 struct r8a66597_device *dev;
387
388 if (is_hub_limit(urb->dev->devpath)) {
802f389a 389 dev_err(&urb->dev->dev, "External hub limit reached.\n");
5d304358
YS
390 return 0;
391 }
392
393 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
394 if (dev && dev->state >= USB_STATE_ADDRESS)
395 return dev->address;
396
397 for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
398 if (r8a66597->address_map & (1 << addr))
399 continue;
400
401 dbg("alloc_address: r8a66597_addr=%d", addr);
402 r8a66597->address_map |= 1 << addr;
403
404 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
405 return 0;
406
407 return addr;
408 }
409
802f389a
GKH
410 dev_err(&urb->dev->dev,
411 "cannot communicate with a USB device more than 10.(%x)\n",
412 r8a66597->address_map);
5d304358
YS
413
414 return 0;
415}
416
417/* this function must be called with interrupt disabled */
418static void free_usb_address(struct r8a66597 *r8a66597,
419 struct r8a66597_device *dev)
420{
421 int port;
422
423 if (!dev)
424 return;
425
426 dbg("free_addr: addr=%d", dev->address);
427
428 dev->state = USB_STATE_DEFAULT;
429 r8a66597->address_map &= ~(1 << dev->address);
430 dev->address = 0;
431 dev_set_drvdata(&dev->udev->dev, NULL);
432 list_del(&dev->device_list);
433 kfree(dev);
434
719a72b7 435 for (port = 0; port < r8a66597->max_root_hub; port++) {
5d304358
YS
436 if (r8a66597->root_hub[port].dev == dev) {
437 r8a66597->root_hub[port].dev = NULL;
438 break;
439 }
440 }
441}
442
443static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
444 u16 mask, u16 loop)
445{
446 u16 tmp;
447 int i = 0;
448
449 do {
450 tmp = r8a66597_read(r8a66597, reg);
451 if (i++ > 1000000) {
802f389a
GKH
452 printk(KERN_ERR "r8a66597: register%lx, loop %x "
453 "is timeout\n", reg, loop);
5d304358
YS
454 break;
455 }
456 ndelay(1);
457 } while ((tmp & mask) != loop);
458}
459
460/* this function must be called with interrupt disabled */
461static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
462{
463 u16 tmp;
464
465 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
466 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
467 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
468 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
469}
470
471/* this function must be called with interrupt disabled */
472static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
473{
474 u16 tmp;
475
476 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
477 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
478 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
479 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
480 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
481}
482
483/* this function must be called with interrupt disabled */
484static void clear_all_buffer(struct r8a66597 *r8a66597,
485 struct r8a66597_pipe *pipe)
486{
487 u16 tmp;
488
489 if (!pipe || pipe->info.pipenum == 0)
490 return;
491
492 pipe_stop(r8a66597, pipe);
493 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
494 tmp = r8a66597_read(r8a66597, pipe->pipectr);
495 tmp = r8a66597_read(r8a66597, pipe->pipectr);
496 tmp = r8a66597_read(r8a66597, pipe->pipectr);
497 r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
498}
499
500/* this function must be called with interrupt disabled */
501static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
502 struct r8a66597_pipe *pipe, int toggle)
503{
504 if (toggle)
505 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
506 else
507 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
508}
509
719a72b7
MD
510static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
511{
512 if (r8a66597->pdata->on_chip)
513 return MBW_32;
514 else
515 return MBW_16;
516}
517
5d304358
YS
518/* this function must be called with interrupt disabled */
519static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
520{
719a72b7
MD
521 unsigned short mbw = mbw_value(r8a66597);
522
523 r8a66597_mdfy(r8a66597, mbw | pipenum, mbw | CURPIPE, CFIFOSEL);
5d304358
YS
524 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
525}
526
527/* this function must be called with interrupt disabled */
528static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
529 struct r8a66597_pipe *pipe)
530{
719a72b7
MD
531 unsigned short mbw = mbw_value(r8a66597);
532
5d304358 533 cfifo_change(r8a66597, 0);
719a72b7
MD
534 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D0FIFOSEL);
535 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D1FIFOSEL);
5d304358 536
719a72b7 537 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum, mbw | CURPIPE,
5d304358
YS
538 pipe->fifosel);
539 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
540}
541
542static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
543{
544 struct r8a66597_pipe *pipe = hep->hcpriv;
545
546 if (usb_pipeendpoint(urb->pipe) == 0)
547 return 0;
548 else
549 return pipe->info.pipenum;
550}
551
552static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
553{
554 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
555
556 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
557}
558
559static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
560 int urb_pipe)
561{
562 if (!dev)
563 return NULL;
564
565 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
566}
567
568/* this function must be called with interrupt disabled */
569static void pipe_toggle_set(struct r8a66597 *r8a66597,
570 struct r8a66597_pipe *pipe,
571 struct urb *urb, int set)
572{
573 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
574 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
575 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
576
577 if (!toggle)
578 return;
579
580 if (set)
581 *toggle |= 1 << endpoint;
582 else
583 *toggle &= ~(1 << endpoint);
584}
585
586/* this function must be called with interrupt disabled */
587static void pipe_toggle_save(struct r8a66597 *r8a66597,
588 struct r8a66597_pipe *pipe,
589 struct urb *urb)
590{
591 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
592 pipe_toggle_set(r8a66597, pipe, urb, 1);
593 else
594 pipe_toggle_set(r8a66597, pipe, urb, 0);
595}
596
597/* this function must be called with interrupt disabled */
598static void pipe_toggle_restore(struct r8a66597 *r8a66597,
599 struct r8a66597_pipe *pipe,
600 struct urb *urb)
601{
602 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
603 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
604 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
605
f6ace2c9
YS
606 if (!toggle)
607 return;
608
5d304358
YS
609 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
610}
611
612/* this function must be called with interrupt disabled */
613static void pipe_buffer_setting(struct r8a66597 *r8a66597,
614 struct r8a66597_pipe_info *info)
615{
616 u16 val = 0;
617
618 if (info->pipenum == 0)
619 return;
620
621 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
622 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
623 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
624 if (!info->dir_in)
625 val |= R8A66597_DIR;
626 if (info->type == R8A66597_BULK && info->dir_in)
627 val |= R8A66597_DBLB | R8A66597_SHTNAK;
628 val |= info->type | info->epnum;
629 r8a66597_write(r8a66597, val, PIPECFG);
630
631 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
632 PIPEBUF);
633 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
634 PIPEMAXP);
5d304358
YS
635 r8a66597_write(r8a66597, info->interval, PIPEPERI);
636}
637
5d304358
YS
638/* this function must be called with interrupt disabled */
639static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
640{
641 struct r8a66597_pipe_info *info;
642 struct urb *urb = td->urb;
643
644 if (td->pipenum > 0) {
645 info = &td->pipe->info;
646 cfifo_change(r8a66597, 0);
647 pipe_buffer_setting(r8a66597, info);
648
649 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
650 usb_pipeout(urb->pipe)) &&
651 !usb_pipecontrol(urb->pipe)) {
652 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
653 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
654 clear_all_buffer(r8a66597, td->pipe);
655 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
656 usb_pipeout(urb->pipe), 1);
657 }
658 pipe_toggle_restore(r8a66597, td->pipe, urb);
659 }
660}
661
662/* this function must be called with interrupt disabled */
663static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
664 struct usb_endpoint_descriptor *ep)
665{
666 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
667
668 memset(array, 0, sizeof(array));
2e0fe709 669 switch (usb_endpoint_type(ep)) {
e294531d 670 case USB_ENDPOINT_XFER_BULK:
2e0fe709 671 if (usb_endpoint_dir_in(ep))
5d304358
YS
672 array[i++] = 4;
673 else {
674 array[i++] = 3;
675 array[i++] = 5;
676 }
e294531d
YS
677 break;
678 case USB_ENDPOINT_XFER_INT:
2e0fe709 679 if (usb_endpoint_dir_in(ep)) {
5d304358
YS
680 array[i++] = 6;
681 array[i++] = 7;
682 array[i++] = 8;
683 } else
684 array[i++] = 9;
e294531d
YS
685 break;
686 case USB_ENDPOINT_XFER_ISOC:
2e0fe709 687 if (usb_endpoint_dir_in(ep))
5d304358
YS
688 array[i++] = 2;
689 else
690 array[i++] = 1;
e294531d
YS
691 break;
692 default:
802f389a 693 printk(KERN_ERR "r8a66597: Illegal type\n");
e294531d
YS
694 return 0;
695 }
5d304358
YS
696
697 i = 1;
698 min = array[0];
699 while (array[i] != 0) {
700 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
701 min = array[i];
702 i++;
703 }
704
705 return min;
706}
707
708static u16 get_r8a66597_type(__u8 type)
709{
710 u16 r8a66597_type;
711
e294531d 712 switch (type) {
5d304358
YS
713 case USB_ENDPOINT_XFER_BULK:
714 r8a66597_type = R8A66597_BULK;
715 break;
716 case USB_ENDPOINT_XFER_INT:
717 r8a66597_type = R8A66597_INT;
718 break;
719 case USB_ENDPOINT_XFER_ISOC:
720 r8a66597_type = R8A66597_ISO;
721 break;
722 default:
802f389a 723 printk(KERN_ERR "r8a66597: Illegal type\n");
5d304358
YS
724 r8a66597_type = 0x0000;
725 break;
726 }
727
728 return r8a66597_type;
729}
730
731static u16 get_bufnum(u16 pipenum)
732{
733 u16 bufnum = 0;
734
735 if (pipenum == 0)
736 bufnum = 0;
737 else if (check_bulk_or_isoc(pipenum))
738 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
739 else if (check_interrupt(pipenum))
740 bufnum = 4 + (pipenum - 6);
741 else
802f389a 742 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
5d304358
YS
743
744 return bufnum;
745}
746
747static u16 get_buf_bsize(u16 pipenum)
748{
749 u16 buf_bsize = 0;
750
751 if (pipenum == 0)
752 buf_bsize = 3;
753 else if (check_bulk_or_isoc(pipenum))
754 buf_bsize = R8A66597_BUF_BSIZE - 1;
755 else if (check_interrupt(pipenum))
756 buf_bsize = 0;
757 else
802f389a 758 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
5d304358
YS
759
760 return buf_bsize;
761}
762
763/* this function must be called with interrupt disabled */
764static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
765 struct r8a66597_device *dev,
766 struct r8a66597_pipe *pipe,
767 struct urb *urb)
768{
769 int i;
770 struct r8a66597_pipe_info *info = &pipe->info;
719a72b7
MD
771 unsigned short mbw = mbw_value(r8a66597);
772
773 /* pipe dma is only for external controlles */
774 if (r8a66597->pdata->on_chip)
775 return;
5d304358
YS
776
777 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
778 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
779 if ((r8a66597->dma_map & (1 << i)) != 0)
780 continue;
781
5909f6ea
GKH
782 dev_info(&dev->udev->dev,
783 "address %d, EndpointAddress 0x%02x use "
784 "DMA FIFO\n", usb_pipedevice(urb->pipe),
785 info->dir_in ?
786 USB_ENDPOINT_DIR_MASK + info->epnum
787 : info->epnum);
5d304358
YS
788
789 r8a66597->dma_map |= 1 << i;
790 dev->dma_map |= 1 << i;
791 set_pipe_reg_addr(pipe, i);
792
793 cfifo_change(r8a66597, 0);
719a72b7
MD
794 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum,
795 mbw | CURPIPE, pipe->fifosel);
5d304358
YS
796
797 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
798 pipe->info.pipenum);
799 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
800 break;
801 }
802 }
803}
804
805/* this function must be called with interrupt disabled */
806static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
807 struct usb_host_endpoint *hep,
808 struct r8a66597_pipe_info *info)
809{
810 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
811 struct r8a66597_pipe *pipe = hep->hcpriv;
812
813 dbg("enable_pipe:");
814
815 pipe->info = *info;
816 set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
817 r8a66597->pipe_cnt[pipe->info.pipenum]++;
818 dev->pipe_cnt[pipe->info.pipenum]++;
819
820 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
821}
822
823/* this function must be called with interrupt disabled */
824static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
825{
826 struct r8a66597_td *td, *next;
827 struct urb *urb;
828 struct list_head *list = &r8a66597->pipe_queue[pipenum];
829
830 if (list_empty(list))
831 return;
832
833 list_for_each_entry_safe(td, next, list, queue) {
5d304358
YS
834 if (td->address != address)
835 continue;
836
837 urb = td->urb;
838 list_del(&td->queue);
839 kfree(td);
840
841 if (urb) {
e9df41c5
AS
842 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597),
843 urb);
844
5d304358 845 spin_unlock(&r8a66597->lock);
4a00027d
AS
846 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb,
847 -ENODEV);
5d304358
YS
848 spin_lock(&r8a66597->lock);
849 }
850 break;
851 }
852}
853
854/* this function must be called with interrupt disabled */
855static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
856 struct r8a66597_device *dev)
857{
858 int check_ep0 = 0;
859 u16 pipenum;
860
861 if (!dev)
862 return;
863
864 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
865 if (!dev->pipe_cnt[pipenum])
866 continue;
867
868 if (!check_ep0) {
869 check_ep0 = 1;
870 force_dequeue(r8a66597, 0, dev->address);
871 }
872
873 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
874 dev->pipe_cnt[pipenum] = 0;
875 force_dequeue(r8a66597, pipenum, dev->address);
876 }
877
878 dbg("disable_pipe");
879
880 r8a66597->dma_map &= ~(dev->dma_map);
881 dev->dma_map = 0;
882}
883
397f519a
YS
884static u16 get_interval(struct urb *urb, __u8 interval)
885{
886 u16 time = 1;
887 int i;
888
889 if (urb->dev->speed == USB_SPEED_HIGH) {
890 if (interval > IITV)
891 time = IITV;
892 else
893 time = interval ? interval - 1 : 0;
894 } else {
895 if (interval > 128) {
896 time = IITV;
897 } else {
898 /* calculate the nearest value for PIPEPERI */
899 for (i = 0; i < 7; i++) {
900 if ((1 << i) < interval &&
901 (1 << (i + 1) > interval))
902 time = 1 << i;
903 }
904 }
905 }
906
907 return time;
908}
909
6d879107
YS
910static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
911{
912 __u8 i;
913 unsigned long time = 1;
914
915 if (usb_pipeisoc(urb->pipe))
916 return 0;
917
918 if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
919 for (i = 0; i < (interval - 1); i++)
920 time *= 2;
921 time = time * 125 / 1000; /* uSOF -> msec */
922 } else {
923 time = interval;
924 }
925
926 return time;
927}
928
5d304358
YS
929/* this function must be called with interrupt disabled */
930static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
931 struct usb_host_endpoint *hep,
932 struct usb_endpoint_descriptor *ep)
933{
934 struct r8a66597_pipe_info info;
935
936 info.pipenum = get_empty_pipenum(r8a66597, ep);
937 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
2e0fe709 938 info.epnum = usb_endpoint_num(ep);
05eac910 939 info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
2e0fe709 940 info.type = get_r8a66597_type(usb_endpoint_type(ep));
5d304358
YS
941 info.bufnum = get_bufnum(info.pipenum);
942 info.buf_bsize = get_buf_bsize(info.pipenum);
6d879107
YS
943 if (info.type == R8A66597_BULK) {
944 info.interval = 0;
945 info.timer_interval = 0;
946 } else {
397f519a 947 info.interval = get_interval(urb, ep->bInterval);
6d879107
YS
948 info.timer_interval = get_timer_interval(urb, ep->bInterval);
949 }
2e0fe709 950 if (usb_endpoint_dir_in(ep))
5d304358
YS
951 info.dir_in = 1;
952 else
953 info.dir_in = 0;
954
955 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
956}
957
958static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
959{
960 struct r8a66597_device *dev;
961
962 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
963 dev->state = USB_STATE_CONFIGURED;
964}
965
966static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
967 u16 pipenum)
968{
969 if (pipenum == 0 && usb_pipeout(urb->pipe))
970 enable_irq_empty(r8a66597, pipenum);
971 else
972 enable_irq_ready(r8a66597, pipenum);
973
974 if (!usb_pipeisoc(urb->pipe))
975 enable_irq_nrdy(r8a66597, pipenum);
976}
977
978static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
979{
980 disable_irq_ready(r8a66597, pipenum);
981 disable_irq_nrdy(r8a66597, pipenum);
982}
983
54d0be9e
YS
984static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
985{
986 mod_timer(&r8a66597->rh_timer,
987 jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
988}
989
990static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
991 int connect)
992{
993 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
994
995 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
996 rh->scount = R8A66597_MAX_SAMPLING;
997 if (connect)
998 rh->port |= 1 << USB_PORT_FEAT_CONNECTION;
999 else
1000 rh->port &= ~(1 << USB_PORT_FEAT_CONNECTION);
1001 rh->port |= 1 << USB_PORT_FEAT_C_CONNECTION;
1002
1003 r8a66597_root_hub_start_polling(r8a66597);
1004}
1005
5d304358 1006/* this function must be called with interrupt disabled */
29fab0cd
YS
1007static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
1008 u16 syssts)
5d304358 1009{
29fab0cd 1010 if (syssts == SE0) {
54d0be9e 1011 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
29fab0cd 1012 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1e6159f8
YS
1013 } else {
1014 if (syssts == FS_JSTS)
1015 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
1016 else if (syssts == LS_JSTS)
1017 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
29fab0cd 1018
1e6159f8
YS
1019 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
1020 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
29fab0cd 1021
1e6159f8
YS
1022 if (r8a66597->bus_suspended)
1023 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1024 }
e1e609be 1025
1e6159f8 1026 usb_hcd_poll_rh_status(r8a66597_to_hcd(r8a66597));
5d304358
YS
1027}
1028
1029/* this function must be called with interrupt disabled */
1030static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
1031{
1032 u16 speed = get_rh_usb_speed(r8a66597, port);
1033 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1034
1e6159f8
YS
1035 rh->port &= ~((1 << USB_PORT_FEAT_HIGHSPEED) |
1036 (1 << USB_PORT_FEAT_LOWSPEED));
5d304358
YS
1037 if (speed == HSMODE)
1038 rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
1039 else if (speed == LSMODE)
1040 rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
1041
1042 rh->port &= ~(1 << USB_PORT_FEAT_RESET);
1043 rh->port |= 1 << USB_PORT_FEAT_ENABLE;
1044}
1045
1046/* this function must be called with interrupt disabled */
1047static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
1048{
1049 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
1050
5d304358
YS
1051 disable_r8a66597_pipe_all(r8a66597, dev);
1052 free_usb_address(r8a66597, dev);
1053
54d0be9e 1054 start_root_hub_sampling(r8a66597, port, 0);
5d304358
YS
1055}
1056
1057/* this function must be called with interrupt disabled */
1058static void prepare_setup_packet(struct r8a66597 *r8a66597,
1059 struct r8a66597_td *td)
1060{
1061 int i;
fd05e720 1062 __le16 *p = (__le16 *)td->urb->setup_packet;
5d304358
YS
1063 unsigned long setup_addr = USBREQ;
1064
1065 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1066 DCPMAXP);
e294531d 1067 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
5d304358
YS
1068
1069 for (i = 0; i < 4; i++) {
fd05e720 1070 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
5d304358
YS
1071 setup_addr += 2;
1072 }
1073 r8a66597_write(r8a66597, SUREQ, DCPCTR);
1074}
1075
1076/* this function must be called with interrupt disabled */
1077static void prepare_packet_read(struct r8a66597 *r8a66597,
1078 struct r8a66597_td *td)
1079{
1080 struct urb *urb = td->urb;
1081
1082 if (usb_pipecontrol(urb->pipe)) {
1083 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1084 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1085 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1086 if (urb->actual_length == 0) {
1087 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1088 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1089 }
1090 pipe_irq_disable(r8a66597, td->pipenum);
1091 pipe_start(r8a66597, td->pipe);
1092 pipe_irq_enable(r8a66597, urb, td->pipenum);
1093 } else {
1094 if (urb->actual_length == 0) {
1095 pipe_irq_disable(r8a66597, td->pipenum);
1096 pipe_setting(r8a66597, td);
1097 pipe_stop(r8a66597, td->pipe);
e294531d 1098 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
5d304358
YS
1099
1100 if (td->pipe->pipetre) {
1101 r8a66597_write(r8a66597, TRCLR,
e294531d 1102 td->pipe->pipetre);
5d304358 1103 r8a66597_write(r8a66597,
dfa5ec79
JL
1104 DIV_ROUND_UP
1105 (urb->transfer_buffer_length,
1106 td->maxpacket),
e294531d 1107 td->pipe->pipetrn);
5d304358 1108 r8a66597_bset(r8a66597, TRENB,
e294531d 1109 td->pipe->pipetre);
5d304358
YS
1110 }
1111
1112 pipe_start(r8a66597, td->pipe);
1113 pipe_irq_enable(r8a66597, urb, td->pipenum);
1114 }
1115 }
1116}
1117
1118/* this function must be called with interrupt disabled */
1119static void prepare_packet_write(struct r8a66597 *r8a66597,
1120 struct r8a66597_td *td)
1121{
1122 u16 tmp;
1123 struct urb *urb = td->urb;
1124
1125 if (usb_pipecontrol(urb->pipe)) {
1126 pipe_stop(r8a66597, td->pipe);
1127 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1128 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1129 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1130 if (urb->actual_length == 0) {
1131 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1132 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1133 }
1134 } else {
1135 if (urb->actual_length == 0)
1136 pipe_setting(r8a66597, td);
1137 if (td->pipe->pipetre)
1138 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1139 }
e294531d 1140 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
5d304358
YS
1141
1142 fifo_change_from_pipe(r8a66597, td->pipe);
1143 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1144 if (unlikely((tmp & FRDY) == 0))
1145 pipe_irq_enable(r8a66597, urb, td->pipenum);
1146 else
1147 packet_write(r8a66597, td->pipenum);
1148 pipe_start(r8a66597, td->pipe);
1149}
1150
1151/* this function must be called with interrupt disabled */
1152static void prepare_status_packet(struct r8a66597 *r8a66597,
1153 struct r8a66597_td *td)
1154{
1155 struct urb *urb = td->urb;
1156
1157 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
e294531d 1158 pipe_stop(r8a66597, td->pipe);
5d304358
YS
1159
1160 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1161 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1162 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1163 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
e294531d 1164 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
9424ea29 1165 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
5d304358
YS
1166 enable_irq_empty(r8a66597, 0);
1167 } else {
1168 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1169 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1170 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1171 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
5d304358
YS
1172 enable_irq_ready(r8a66597, 0);
1173 }
1174 enable_irq_nrdy(r8a66597, 0);
1175 pipe_start(r8a66597, td->pipe);
1176}
1177
e3a09051
YS
1178static int is_set_address(unsigned char *setup_packet)
1179{
1180 if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1181 setup_packet[1] == USB_REQ_SET_ADDRESS)
1182 return 1;
1183 else
1184 return 0;
1185}
1186
5d304358
YS
1187/* this function must be called with interrupt disabled */
1188static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1189{
1190 BUG_ON(!td);
1191
1192 switch (td->type) {
1193 case USB_PID_SETUP:
e3a09051 1194 if (is_set_address(td->urb->setup_packet)) {
5d304358
YS
1195 td->set_address = 1;
1196 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1197 td->urb);
1198 if (td->urb->setup_packet[2] == 0)
1199 return -EPIPE;
1200 }
1201 prepare_setup_packet(r8a66597, td);
1202 break;
1203 case USB_PID_IN:
1204 prepare_packet_read(r8a66597, td);
1205 break;
1206 case USB_PID_OUT:
1207 prepare_packet_write(r8a66597, td);
1208 break;
1209 case USB_PID_ACK:
1210 prepare_status_packet(r8a66597, td);
1211 break;
1212 default:
802f389a 1213 printk(KERN_ERR "r8a66597: invalid type.\n");
5d304358
YS
1214 break;
1215 }
1216
1217 return 0;
1218}
1219
1220static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1221{
1222 if (usb_pipeisoc(urb->pipe)) {
1223 if (urb->number_of_packets == td->iso_cnt)
1224 return 1;
1225 }
1226
1227 /* control or bulk or interrupt */
1228 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1229 (td->short_packet) || (td->zero_packet))
1230 return 1;
1231
1232 return 0;
1233}
1234
1235/* this function must be called with interrupt disabled */
1236static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1237{
1238 unsigned long time;
1239
1240 BUG_ON(!td);
1241
1242 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1243 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1244 r8a66597->timeout_map |= 1 << td->pipenum;
1245 switch (usb_pipetype(td->urb->pipe)) {
1246 case PIPE_INTERRUPT:
1247 case PIPE_ISOCHRONOUS:
1248 time = 30;
1249 break;
1250 default:
1251 time = 300;
1252 break;
1253 }
1254
1255 mod_timer(&r8a66597->td_timer[td->pipenum],
1256 jiffies + msecs_to_jiffies(time));
1257 }
1258}
1259
1260/* this function must be called with interrupt disabled */
dfd1e537 1261static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
888fda47 1262 u16 pipenum, struct urb *urb, int status)
dfd1e537 1263__releases(r8a66597->lock) __acquires(r8a66597->lock)
5d304358
YS
1264{
1265 int restart = 0;
1266 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1267
1268 r8a66597->timeout_map &= ~(1 << pipenum);
1269
1270 if (likely(td)) {
888fda47 1271 if (td->set_address && (status != 0 || urb->unlinked))
5d304358
YS
1272 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1273
1274 pipe_toggle_save(r8a66597, td->pipe, urb);
1275 list_del(&td->queue);
1276 kfree(td);
1277 }
1278
1279 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1280 restart = 1;
1281
1282 if (likely(urb)) {
1283 if (usb_pipeisoc(urb->pipe))
1284 urb->start_frame = r8a66597_get_frame(hcd);
1285
e9df41c5 1286 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
5d304358 1287 spin_unlock(&r8a66597->lock);
4a00027d 1288 usb_hcd_giveback_urb(hcd, urb, status);
5d304358
YS
1289 spin_lock(&r8a66597->lock);
1290 }
1291
1292 if (restart) {
1293 td = r8a66597_get_td(r8a66597, pipenum);
1294 if (unlikely(!td))
1295 return;
1296
1297 start_transfer(r8a66597, td);
1298 set_td_timer(r8a66597, td);
1299 }
1300}
1301
5d304358
YS
1302static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1303{
1304 u16 tmp;
1305 int rcv_len, bufsize, urb_len, size;
1306 u16 *buf;
1307 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1308 struct urb *urb;
1309 int finish = 0;
dfd1e537 1310 int status = 0;
5d304358
YS
1311
1312 if (unlikely(!td))
1313 return;
1314 urb = td->urb;
1315
1316 fifo_change_from_pipe(r8a66597, td->pipe);
1317 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1318 if (unlikely((tmp & FRDY) == 0)) {
5d304358
YS
1319 pipe_stop(r8a66597, td->pipe);
1320 pipe_irq_disable(r8a66597, pipenum);
802f389a 1321 printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
888fda47 1322 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
5d304358
YS
1323 return;
1324 }
1325
1326 /* prepare parameters */
1327 rcv_len = tmp & DTLN;
5d304358
YS
1328 if (usb_pipeisoc(urb->pipe)) {
1329 buf = (u16 *)(urb->transfer_buffer +
1330 urb->iso_frame_desc[td->iso_cnt].offset);
1331 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1332 } else {
1333 buf = (void *)urb->transfer_buffer + urb->actual_length;
1334 urb_len = urb->transfer_buffer_length - urb->actual_length;
1335 }
dfd1e537
AS
1336 bufsize = min(urb_len, (int) td->maxpacket);
1337 if (rcv_len <= bufsize) {
1338 size = rcv_len;
1339 } else {
1340 size = bufsize;
1341 status = -EOVERFLOW;
1342 finish = 1;
1343 }
5d304358
YS
1344
1345 /* update parameters */
1346 urb->actual_length += size;
1347 if (rcv_len == 0)
1348 td->zero_packet = 1;
dfd1e537 1349 if (rcv_len < bufsize) {
5d304358 1350 td->short_packet = 1;
5d304358
YS
1351 }
1352 if (usb_pipeisoc(urb->pipe)) {
1353 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
dfd1e537 1354 urb->iso_frame_desc[td->iso_cnt].status = status;
5d304358 1355 td->iso_cnt++;
dfd1e537 1356 finish = 0;
5d304358
YS
1357 }
1358
1359 /* check transfer finish */
b0d9efba 1360 if (finish || check_transfer_finish(td, urb)) {
5d304358
YS
1361 pipe_stop(r8a66597, td->pipe);
1362 pipe_irq_disable(r8a66597, pipenum);
1363 finish = 1;
1364 }
1365
1366 /* read fifo */
1367 if (urb->transfer_buffer) {
1368 if (size == 0)
1369 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1370 else
1371 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1372 buf, size);
1373 }
1374
888fda47
AS
1375 if (finish && pipenum != 0)
1376 finish_request(r8a66597, td, pipenum, urb, status);
5d304358
YS
1377}
1378
1379static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1380{
1381 u16 tmp;
1382 int bufsize, size;
1383 u16 *buf;
1384 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1385 struct urb *urb;
1386
1387 if (unlikely(!td))
1388 return;
1389 urb = td->urb;
1390
1391 fifo_change_from_pipe(r8a66597, td->pipe);
1392 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1393 if (unlikely((tmp & FRDY) == 0)) {
5d304358
YS
1394 pipe_stop(r8a66597, td->pipe);
1395 pipe_irq_disable(r8a66597, pipenum);
802f389a 1396 printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
888fda47 1397 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
5d304358
YS
1398 return;
1399 }
1400
1401 /* prepare parameters */
1402 bufsize = td->maxpacket;
1403 if (usb_pipeisoc(urb->pipe)) {
1404 buf = (u16 *)(urb->transfer_buffer +
1405 urb->iso_frame_desc[td->iso_cnt].offset);
1406 size = min(bufsize,
1407 (int)urb->iso_frame_desc[td->iso_cnt].length);
1408 } else {
1409 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
16e2e5f6 1410 size = min_t(u32, bufsize,
5d304358
YS
1411 urb->transfer_buffer_length - urb->actual_length);
1412 }
1413
1414 /* write fifo */
1415 if (pipenum > 0)
e294531d 1416 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
5d304358
YS
1417 if (urb->transfer_buffer) {
1418 r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
1419 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1420 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1421 }
1422
1423 /* update parameters */
1424 urb->actual_length += size;
1425 if (usb_pipeisoc(urb->pipe)) {
1426 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1427 urb->iso_frame_desc[td->iso_cnt].status = 0;
1428 td->iso_cnt++;
1429 }
1430
1431 /* check transfer finish */
1432 if (check_transfer_finish(td, urb)) {
1433 disable_irq_ready(r8a66597, pipenum);
1434 enable_irq_empty(r8a66597, pipenum);
1435 if (!usb_pipeisoc(urb->pipe))
1436 enable_irq_nrdy(r8a66597, pipenum);
1437 } else
1438 pipe_irq_enable(r8a66597, urb, pipenum);
1439}
1440
1441
888fda47 1442static void check_next_phase(struct r8a66597 *r8a66597, int status)
5d304358
YS
1443{
1444 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1445 struct urb *urb;
1446 u8 finish = 0;
1447
1448 if (unlikely(!td))
1449 return;
1450 urb = td->urb;
1451
1452 switch (td->type) {
1453 case USB_PID_IN:
1454 case USB_PID_OUT:
5d304358
YS
1455 if (check_transfer_finish(td, urb))
1456 td->type = USB_PID_ACK;
1457 break;
1458 case USB_PID_SETUP:
eb231054 1459 if (urb->transfer_buffer_length == urb->actual_length)
5d304358 1460 td->type = USB_PID_ACK;
eb231054 1461 else if (usb_pipeout(urb->pipe))
5d304358
YS
1462 td->type = USB_PID_OUT;
1463 else
1464 td->type = USB_PID_IN;
1465 break;
1466 case USB_PID_ACK:
1467 finish = 1;
5d304358
YS
1468 break;
1469 }
1470
888fda47
AS
1471 if (finish || status != 0 || urb->unlinked)
1472 finish_request(r8a66597, td, 0, urb, status);
5d304358
YS
1473 else
1474 start_transfer(r8a66597, td);
1475}
1476
888fda47 1477static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
5d304358
YS
1478{
1479 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1480
888fda47 1481 if (td) {
5d304358
YS
1482 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1483
1484 if (pid == PID_NAK)
888fda47 1485 return -ECONNRESET;
5d304358 1486 else
888fda47 1487 return -EPIPE;
5d304358 1488 }
888fda47 1489 return 0;
5d304358
YS
1490}
1491
1492static void irq_pipe_ready(struct r8a66597 *r8a66597)
1493{
1494 u16 check;
1495 u16 pipenum;
1496 u16 mask;
1497 struct r8a66597_td *td;
1498
1499 mask = r8a66597_read(r8a66597, BRDYSTS)
1500 & r8a66597_read(r8a66597, BRDYENB);
e294531d 1501 r8a66597_write(r8a66597, ~mask, BRDYSTS);
5d304358
YS
1502 if (mask & BRDY0) {
1503 td = r8a66597_get_td(r8a66597, 0);
1504 if (td && td->type == USB_PID_IN)
1505 packet_read(r8a66597, 0);
1506 else
1507 pipe_irq_disable(r8a66597, 0);
888fda47 1508 check_next_phase(r8a66597, 0);
5d304358
YS
1509 }
1510
1511 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1512 check = 1 << pipenum;
1513 if (mask & check) {
1514 td = r8a66597_get_td(r8a66597, pipenum);
1515 if (unlikely(!td))
1516 continue;
1517
1518 if (td->type == USB_PID_IN)
1519 packet_read(r8a66597, pipenum);
1520 else if (td->type == USB_PID_OUT)
1521 packet_write(r8a66597, pipenum);
1522 }
1523 }
1524}
1525
1526static void irq_pipe_empty(struct r8a66597 *r8a66597)
1527{
1528 u16 tmp;
1529 u16 check;
1530 u16 pipenum;
1531 u16 mask;
1532 struct r8a66597_td *td;
1533
1534 mask = r8a66597_read(r8a66597, BEMPSTS)
1535 & r8a66597_read(r8a66597, BEMPENB);
e294531d 1536 r8a66597_write(r8a66597, ~mask, BEMPSTS);
5d304358
YS
1537 if (mask & BEMP0) {
1538 cfifo_change(r8a66597, 0);
1539 td = r8a66597_get_td(r8a66597, 0);
1540 if (td && td->type != USB_PID_OUT)
1541 disable_irq_empty(r8a66597, 0);
888fda47 1542 check_next_phase(r8a66597, 0);
5d304358
YS
1543 }
1544
1545 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1546 check = 1 << pipenum;
1547 if (mask & check) {
1548 struct r8a66597_td *td;
1549 td = r8a66597_get_td(r8a66597, pipenum);
1550 if (unlikely(!td))
1551 continue;
1552
1553 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1554 if ((tmp & INBUFM) == 0) {
1555 disable_irq_empty(r8a66597, pipenum);
1556 pipe_irq_disable(r8a66597, pipenum);
888fda47
AS
1557 finish_request(r8a66597, td, pipenum, td->urb,
1558 0);
5d304358
YS
1559 }
1560 }
1561 }
1562}
1563
1564static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1565{
1566 u16 check;
1567 u16 pipenum;
1568 u16 mask;
888fda47 1569 int status;
5d304358
YS
1570
1571 mask = r8a66597_read(r8a66597, NRDYSTS)
1572 & r8a66597_read(r8a66597, NRDYENB);
e294531d 1573 r8a66597_write(r8a66597, ~mask, NRDYSTS);
5d304358
YS
1574 if (mask & NRDY0) {
1575 cfifo_change(r8a66597, 0);
888fda47 1576 status = get_urb_error(r8a66597, 0);
5d304358 1577 pipe_irq_disable(r8a66597, 0);
888fda47 1578 check_next_phase(r8a66597, status);
5d304358
YS
1579 }
1580
1581 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1582 check = 1 << pipenum;
1583 if (mask & check) {
1584 struct r8a66597_td *td;
1585 td = r8a66597_get_td(r8a66597, pipenum);
1586 if (unlikely(!td))
1587 continue;
1588
888fda47 1589 status = get_urb_error(r8a66597, pipenum);
5d304358
YS
1590 pipe_irq_disable(r8a66597, pipenum);
1591 pipe_stop(r8a66597, td->pipe);
888fda47 1592 finish_request(r8a66597, td, pipenum, td->urb, status);
5d304358
YS
1593 }
1594 }
1595}
1596
5d304358
YS
1597static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1598{
1599 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1600 u16 intsts0, intsts1, intsts2;
1601 u16 intenb0, intenb1, intenb2;
1602 u16 mask0, mask1, mask2;
888fda47 1603 int status;
5d304358
YS
1604
1605 spin_lock(&r8a66597->lock);
1606
1607 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1608 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1609 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1610 intenb0 = r8a66597_read(r8a66597, INTENB0);
1611 intenb1 = r8a66597_read(r8a66597, INTENB1);
1612 intenb2 = r8a66597_read(r8a66597, INTENB2);
1613
1614 mask2 = intsts2 & intenb2;
1615 mask1 = intsts1 & intenb1;
1616 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1617 if (mask2) {
1618 if (mask2 & ATTCH) {
e294531d 1619 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
5d304358
YS
1620 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1621
1622 /* start usb bus sampling */
54d0be9e 1623 start_root_hub_sampling(r8a66597, 1, 1);
5d304358
YS
1624 }
1625 if (mask2 & DTCH) {
e294531d 1626 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
5d304358
YS
1627 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1628 r8a66597_usb_disconnect(r8a66597, 1);
1629 }
e1e609be
YS
1630 if (mask2 & BCHG) {
1631 r8a66597_write(r8a66597, ~BCHG, INTSTS2);
1632 r8a66597_bclr(r8a66597, BCHGE, INTENB2);
1633 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1634 }
5d304358
YS
1635 }
1636
1637 if (mask1) {
1638 if (mask1 & ATTCH) {
e294531d 1639 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
5d304358
YS
1640 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1641
1642 /* start usb bus sampling */
54d0be9e 1643 start_root_hub_sampling(r8a66597, 0, 1);
5d304358
YS
1644 }
1645 if (mask1 & DTCH) {
e294531d 1646 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
5d304358
YS
1647 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1648 r8a66597_usb_disconnect(r8a66597, 0);
1649 }
e1e609be
YS
1650 if (mask1 & BCHG) {
1651 r8a66597_write(r8a66597, ~BCHG, INTSTS1);
1652 r8a66597_bclr(r8a66597, BCHGE, INTENB1);
1653 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1654 }
1655
5d304358 1656 if (mask1 & SIGN) {
e294531d 1657 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
888fda47
AS
1658 status = get_urb_error(r8a66597, 0);
1659 check_next_phase(r8a66597, status);
5d304358
YS
1660 }
1661 if (mask1 & SACK) {
e294531d 1662 r8a66597_write(r8a66597, ~SACK, INTSTS1);
888fda47 1663 check_next_phase(r8a66597, 0);
5d304358
YS
1664 }
1665 }
1666 if (mask0) {
1667 if (mask0 & BRDY)
1668 irq_pipe_ready(r8a66597);
1669 if (mask0 & BEMP)
1670 irq_pipe_empty(r8a66597);
1671 if (mask0 & NRDY)
1672 irq_pipe_nrdy(r8a66597);
1673 }
1674
1675 spin_unlock(&r8a66597->lock);
1676 return IRQ_HANDLED;
1677}
1678
1679/* this function must be called with interrupt disabled */
1680static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1681{
1682 u16 tmp;
1683 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1684
1685 if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
1686 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1687
1688 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1689 if ((tmp & USBRST) == USBRST) {
1690 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1691 dvstctr_reg);
29fab0cd 1692 r8a66597_root_hub_start_polling(r8a66597);
5d304358
YS
1693 } else
1694 r8a66597_usb_connect(r8a66597, port);
1695 }
1696
29fab0cd
YS
1697 if (!(rh->port & (1 << USB_PORT_FEAT_CONNECTION))) {
1698 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1699 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1700 }
1701
5d304358
YS
1702 if (rh->scount > 0) {
1703 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1704 if (tmp == rh->old_syssts) {
1705 rh->scount--;
29fab0cd
YS
1706 if (rh->scount == 0)
1707 r8a66597_check_syssts(r8a66597, port, tmp);
1708 else
1709 r8a66597_root_hub_start_polling(r8a66597);
5d304358
YS
1710 } else {
1711 rh->scount = R8A66597_MAX_SAMPLING;
1712 rh->old_syssts = tmp;
29fab0cd 1713 r8a66597_root_hub_start_polling(r8a66597);
5d304358
YS
1714 }
1715 }
1716}
1717
6d879107
YS
1718static void r8a66597_interval_timer(unsigned long _r8a66597)
1719{
1720 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1721 unsigned long flags;
1722 u16 pipenum;
1723 struct r8a66597_td *td;
1724
1725 spin_lock_irqsave(&r8a66597->lock, flags);
1726
1727 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1728 if (!(r8a66597->interval_map & (1 << pipenum)))
1729 continue;
1730 if (timer_pending(&r8a66597->interval_timer[pipenum]))
1731 continue;
1732
1733 td = r8a66597_get_td(r8a66597, pipenum);
1734 if (td)
1735 start_transfer(r8a66597, td);
1736 }
1737
1738 spin_unlock_irqrestore(&r8a66597->lock, flags);
1739}
1740
5d304358
YS
1741static void r8a66597_td_timer(unsigned long _r8a66597)
1742{
1743 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1744 unsigned long flags;
1745 u16 pipenum;
1746 struct r8a66597_td *td, *new_td = NULL;
1747 struct r8a66597_pipe *pipe;
1748
1749 spin_lock_irqsave(&r8a66597->lock, flags);
1750 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1751 if (!(r8a66597->timeout_map & (1 << pipenum)))
1752 continue;
1753 if (timer_pending(&r8a66597->td_timer[pipenum]))
1754 continue;
1755
1756 td = r8a66597_get_td(r8a66597, pipenum);
1757 if (!td) {
1758 r8a66597->timeout_map &= ~(1 << pipenum);
1759 continue;
1760 }
1761
1762 if (td->urb->actual_length) {
1763 set_td_timer(r8a66597, td);
1764 break;
1765 }
1766
1767 pipe = td->pipe;
1768 pipe_stop(r8a66597, pipe);
1769
1770 new_td = td;
1771 do {
1772 list_move_tail(&new_td->queue,
1773 &r8a66597->pipe_queue[pipenum]);
1774 new_td = r8a66597_get_td(r8a66597, pipenum);
1775 if (!new_td) {
1776 new_td = td;
1777 break;
1778 }
1779 } while (td != new_td && td->address == new_td->address);
1780
1781 start_transfer(r8a66597, new_td);
1782
1783 if (td == new_td)
1784 r8a66597->timeout_map &= ~(1 << pipenum);
1785 else
1786 set_td_timer(r8a66597, new_td);
1787 break;
1788 }
1789 spin_unlock_irqrestore(&r8a66597->lock, flags);
1790}
1791
1792static void r8a66597_timer(unsigned long _r8a66597)
1793{
1794 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1795 unsigned long flags;
58639646 1796 int port;
5d304358
YS
1797
1798 spin_lock_irqsave(&r8a66597->lock, flags);
1799
719a72b7 1800 for (port = 0; port < r8a66597->max_root_hub; port++)
58639646 1801 r8a66597_root_hub_control(r8a66597, port);
5d304358
YS
1802
1803 spin_unlock_irqrestore(&r8a66597->lock, flags);
1804}
1805
1806static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1807{
1808 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1809
1810 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1811 (urb->dev->state == USB_STATE_CONFIGURED))
1812 return 1;
1813 else
1814 return 0;
1815}
1816
1817static int r8a66597_start(struct usb_hcd *hcd)
1818{
1819 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
5d304358
YS
1820
1821 hcd->state = HC_STATE_RUNNING;
e294531d 1822 return enable_controller(r8a66597);
5d304358
YS
1823}
1824
1825static void r8a66597_stop(struct usb_hcd *hcd)
1826{
1827 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1828
1829 disable_controller(r8a66597);
1830}
1831
1832static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1833{
1834 unsigned int usb_address = usb_pipedevice(urb->pipe);
1835 u16 root_port, hub_port;
1836
1837 if (usb_address == 0) {
719a72b7 1838 get_port_number(r8a66597, urb->dev->devpath,
5d304358
YS
1839 &root_port, &hub_port);
1840 set_devadd_reg(r8a66597, 0,
1841 get_r8a66597_usb_speed(urb->dev->speed),
1842 get_parent_r8a66597_address(r8a66597, urb->dev),
1843 hub_port, root_port);
1844 }
1845}
1846
1847static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1848 struct urb *urb,
e294531d 1849 struct usb_host_endpoint *hep)
5d304358
YS
1850{
1851 struct r8a66597_td *td;
1852 u16 pipenum;
1853
e294531d 1854 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
5d304358
YS
1855 if (td == NULL)
1856 return NULL;
1857
1858 pipenum = r8a66597_get_pipenum(urb, hep);
1859 td->pipenum = pipenum;
1860 td->pipe = hep->hcpriv;
1861 td->urb = urb;
1862 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1863 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1864 !usb_pipein(urb->pipe));
1865 if (usb_pipecontrol(urb->pipe))
1866 td->type = USB_PID_SETUP;
1867 else if (usb_pipein(urb->pipe))
1868 td->type = USB_PID_IN;
1869 else
1870 td->type = USB_PID_OUT;
1871 INIT_LIST_HEAD(&td->queue);
1872
1873 return td;
1874}
1875
1876static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
5d304358
YS
1877 struct urb *urb,
1878 gfp_t mem_flags)
1879{
e9df41c5 1880 struct usb_host_endpoint *hep = urb->ep;
5d304358
YS
1881 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1882 struct r8a66597_td *td = NULL;
e9df41c5 1883 int ret, request = 0;
5d304358
YS
1884 unsigned long flags;
1885
1886 spin_lock_irqsave(&r8a66597->lock, flags);
1887 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1888 ret = -ENODEV;
e9df41c5 1889 goto error_not_linked;
5d304358
YS
1890 }
1891
e9df41c5
AS
1892 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1893 if (ret)
1894 goto error_not_linked;
1895
5d304358 1896 if (!hep->hcpriv) {
e294531d
YS
1897 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1898 GFP_ATOMIC);
5d304358
YS
1899 if (!hep->hcpriv) {
1900 ret = -ENOMEM;
1901 goto error;
1902 }
1903 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1904 if (usb_pipeendpoint(urb->pipe))
1905 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1906 }
1907
1908 if (unlikely(check_pipe_config(r8a66597, urb)))
1909 init_pipe_config(r8a66597, urb);
1910
1911 set_address_zero(r8a66597, urb);
e294531d 1912 td = r8a66597_make_td(r8a66597, urb, hep);
5d304358
YS
1913 if (td == NULL) {
1914 ret = -ENOMEM;
1915 goto error;
1916 }
1917 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1918 request = 1;
1919 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
5d304358 1920 urb->hcpriv = td;
5d304358
YS
1921
1922 if (request) {
6d879107
YS
1923 if (td->pipe->info.timer_interval) {
1924 r8a66597->interval_map |= 1 << td->pipenum;
1925 mod_timer(&r8a66597->interval_timer[td->pipenum],
1926 jiffies + msecs_to_jiffies(
1927 td->pipe->info.timer_interval));
1928 } else {
1929 ret = start_transfer(r8a66597, td);
1930 if (ret < 0) {
1931 list_del(&td->queue);
1932 kfree(td);
1933 }
5d304358
YS
1934 }
1935 } else
1936 set_td_timer(r8a66597, td);
1937
1938error:
e9df41c5
AS
1939 if (ret)
1940 usb_hcd_unlink_urb_from_ep(hcd, urb);
1941error_not_linked:
5d304358
YS
1942 spin_unlock_irqrestore(&r8a66597->lock, flags);
1943 return ret;
1944}
1945
e9df41c5
AS
1946static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1947 int status)
5d304358
YS
1948{
1949 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1950 struct r8a66597_td *td;
1951 unsigned long flags;
e9df41c5 1952 int rc;
5d304358
YS
1953
1954 spin_lock_irqsave(&r8a66597->lock, flags);
e9df41c5
AS
1955 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1956 if (rc)
1957 goto done;
1958
5d304358
YS
1959 if (urb->hcpriv) {
1960 td = urb->hcpriv;
1961 pipe_stop(r8a66597, td->pipe);
1962 pipe_irq_disable(r8a66597, td->pipenum);
1963 disable_irq_empty(r8a66597, td->pipenum);
888fda47 1964 finish_request(r8a66597, td, td->pipenum, urb, status);
5d304358 1965 }
e9df41c5 1966 done:
5d304358 1967 spin_unlock_irqrestore(&r8a66597->lock, flags);
e9df41c5 1968 return rc;
5d304358
YS
1969}
1970
1971static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1972 struct usb_host_endpoint *hep)
1973{
1974 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1975 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1976 struct r8a66597_td *td;
1977 struct urb *urb = NULL;
1978 u16 pipenum;
1979 unsigned long flags;
1980
1981 if (pipe == NULL)
1982 return;
1983 pipenum = pipe->info.pipenum;
1984
1985 if (pipenum == 0) {
1986 kfree(hep->hcpriv);
1987 hep->hcpriv = NULL;
1988 return;
1989 }
1990
1991 spin_lock_irqsave(&r8a66597->lock, flags);
1992 pipe_stop(r8a66597, pipe);
1993 pipe_irq_disable(r8a66597, pipenum);
1994 disable_irq_empty(r8a66597, pipenum);
1995 td = r8a66597_get_td(r8a66597, pipenum);
1996 if (td)
1997 urb = td->urb;
888fda47 1998 finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
5d304358
YS
1999 kfree(hep->hcpriv);
2000 hep->hcpriv = NULL;
2001 spin_unlock_irqrestore(&r8a66597->lock, flags);
2002}
2003
2004static int r8a66597_get_frame(struct usb_hcd *hcd)
2005{
2006 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2007 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
2008}
2009
2010static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
2011{
2012 int chix;
2013
2014 if (udev->state == USB_STATE_CONFIGURED &&
2015 udev->parent && udev->parent->devnum > 1 &&
2016 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
2017 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
2018
2019 for (chix = 0; chix < udev->maxchild; chix++) {
2020 struct usb_device *childdev = udev->children[chix];
2021
2022 if (childdev)
2023 collect_usb_address_map(childdev, map);
2024 }
2025}
2026
2027/* this function must be called with interrupt disabled */
2028static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
2029 int addr)
2030{
2031 struct r8a66597_device *dev;
2032 struct list_head *list = &r8a66597->child_device;
2033
2034 list_for_each_entry(dev, list, device_list) {
5d304358
YS
2035 if (dev->usb_address != addr)
2036 continue;
2037
2038 return dev;
2039 }
2040
802f389a 2041 printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
5d304358
YS
2042 return NULL;
2043}
2044
2045static void update_usb_address_map(struct r8a66597 *r8a66597,
2046 struct usb_device *root_hub,
2047 unsigned long *map)
2048{
2049 int i, j, addr;
2050 unsigned long diff;
2051 unsigned long flags;
2052
2053 for (i = 0; i < 4; i++) {
2054 diff = r8a66597->child_connect_map[i] ^ map[i];
2055 if (!diff)
2056 continue;
2057
2058 for (j = 0; j < 32; j++) {
2059 if (!(diff & (1 << j)))
2060 continue;
2061
2062 addr = i * 32 + j;
2063 if (map[i] & (1 << j))
2064 set_child_connect_map(r8a66597, addr);
2065 else {
2066 struct r8a66597_device *dev;
2067
2068 spin_lock_irqsave(&r8a66597->lock, flags);
2069 dev = get_r8a66597_device(r8a66597, addr);
2070 disable_r8a66597_pipe_all(r8a66597, dev);
2071 free_usb_address(r8a66597, dev);
2072 put_child_connect_map(r8a66597, addr);
2073 spin_unlock_irqrestore(&r8a66597->lock, flags);
2074 }
2075 }
2076 }
2077}
2078
2079static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
2080 struct usb_hcd *hcd)
2081{
2082 struct usb_bus *bus;
2083 unsigned long now_map[4];
2084
2085 memset(now_map, 0, sizeof(now_map));
2086
2087 list_for_each_entry(bus, &usb_bus_list, bus_list) {
2088 if (!bus->root_hub)
2089 continue;
2090
2091 if (bus->busnum != hcd->self.busnum)
2092 continue;
2093
2094 collect_usb_address_map(bus->root_hub, now_map);
2095 update_usb_address_map(r8a66597, bus->root_hub, now_map);
2096 }
2097}
2098
2099static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2100{
2101 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2102 unsigned long flags;
2103 int i;
2104
2105 r8a66597_check_detect_child(r8a66597, hcd);
2106
2107 spin_lock_irqsave(&r8a66597->lock, flags);
2108
2109 *buf = 0; /* initialize (no change) */
2110
719a72b7 2111 for (i = 0; i < r8a66597->max_root_hub; i++) {
5d304358
YS
2112 if (r8a66597->root_hub[i].port & 0xffff0000)
2113 *buf |= 1 << (i + 1);
2114 }
2115
2116 spin_unlock_irqrestore(&r8a66597->lock, flags);
2117
2118 return (*buf != 0);
2119}
2120
2121static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2122 struct usb_hub_descriptor *desc)
2123{
2124 desc->bDescriptorType = 0x29;
2125 desc->bHubContrCurrent = 0;
719a72b7 2126 desc->bNbrPorts = r8a66597->max_root_hub;
5d304358
YS
2127 desc->bDescLength = 9;
2128 desc->bPwrOn2PwrGood = 0;
2129 desc->wHubCharacteristics = cpu_to_le16(0x0011);
719a72b7 2130 desc->bitmap[0] = ((1 << r8a66597->max_root_hub) - 1) << 1;
5d304358
YS
2131 desc->bitmap[1] = ~0;
2132}
2133
2134static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2135 u16 wIndex, char *buf, u16 wLength)
2136{
2137 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2138 int ret;
2139 int port = (wIndex & 0x00FF) - 1;
2140 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2141 unsigned long flags;
2142
2143 ret = 0;
2144
2145 spin_lock_irqsave(&r8a66597->lock, flags);
2146 switch (typeReq) {
2147 case ClearHubFeature:
2148 case SetHubFeature:
2149 switch (wValue) {
2150 case C_HUB_OVER_CURRENT:
2151 case C_HUB_LOCAL_POWER:
2152 break;
2153 default:
2154 goto error;
2155 }
2156 break;
2157 case ClearPortFeature:
719a72b7 2158 if (wIndex > r8a66597->max_root_hub)
5d304358
YS
2159 goto error;
2160 if (wLength != 0)
2161 goto error;
2162
2163 switch (wValue) {
2164 case USB_PORT_FEAT_ENABLE:
e1e609be 2165 rh->port &= ~(1 << USB_PORT_FEAT_POWER);
5d304358
YS
2166 break;
2167 case USB_PORT_FEAT_SUSPEND:
2168 break;
2169 case USB_PORT_FEAT_POWER:
2170 r8a66597_port_power(r8a66597, port, 0);
2171 break;
2172 case USB_PORT_FEAT_C_ENABLE:
2173 case USB_PORT_FEAT_C_SUSPEND:
2174 case USB_PORT_FEAT_C_CONNECTION:
2175 case USB_PORT_FEAT_C_OVER_CURRENT:
2176 case USB_PORT_FEAT_C_RESET:
2177 break;
2178 default:
2179 goto error;
2180 }
2181 rh->port &= ~(1 << wValue);
2182 break;
2183 case GetHubDescriptor:
2184 r8a66597_hub_descriptor(r8a66597,
2185 (struct usb_hub_descriptor *)buf);
2186 break;
2187 case GetHubStatus:
2188 *buf = 0x00;
2189 break;
2190 case GetPortStatus:
719a72b7 2191 if (wIndex > r8a66597->max_root_hub)
5d304358 2192 goto error;
fd05e720 2193 *(__le32 *)buf = cpu_to_le32(rh->port);
5d304358
YS
2194 break;
2195 case SetPortFeature:
719a72b7 2196 if (wIndex > r8a66597->max_root_hub)
5d304358
YS
2197 goto error;
2198 if (wLength != 0)
2199 goto error;
2200
2201 switch (wValue) {
2202 case USB_PORT_FEAT_SUSPEND:
2203 break;
2204 case USB_PORT_FEAT_POWER:
2205 r8a66597_port_power(r8a66597, port, 1);
2206 rh->port |= (1 << USB_PORT_FEAT_POWER);
2207 break;
2208 case USB_PORT_FEAT_RESET: {
2209 struct r8a66597_device *dev = rh->dev;
2210
2211 rh->port |= (1 << USB_PORT_FEAT_RESET);
2212
2213 disable_r8a66597_pipe_all(r8a66597, dev);
2214 free_usb_address(r8a66597, dev);
2215
2216 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2217 get_dvstctr_reg(port));
2218 mod_timer(&r8a66597->rh_timer,
2219 jiffies + msecs_to_jiffies(50));
2220 }
2221 break;
2222 default:
2223 goto error;
2224 }
2225 rh->port |= 1 << wValue;
2226 break;
2227 default:
2228error:
2229 ret = -EPIPE;
2230 break;
2231 }
2232
2233 spin_unlock_irqrestore(&r8a66597->lock, flags);
2234 return ret;
2235}
2236
e1e609be
YS
2237#if defined(CONFIG_PM)
2238static int r8a66597_bus_suspend(struct usb_hcd *hcd)
2239{
2240 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2241 int port;
2242
2243 dbg("%s", __func__);
2244
719a72b7 2245 for (port = 0; port < r8a66597->max_root_hub; port++) {
e1e609be
YS
2246 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2247 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2248
2249 if (!(rh->port & (1 << USB_PORT_FEAT_ENABLE)))
2250 continue;
2251
2252 dbg("suspend port = %d", port);
2253 r8a66597_bclr(r8a66597, UACT, dvstctr_reg); /* suspend */
2254 rh->port |= 1 << USB_PORT_FEAT_SUSPEND;
2255
2256 if (rh->dev->udev->do_remote_wakeup) {
2257 msleep(3); /* waiting last SOF */
2258 r8a66597_bset(r8a66597, RWUPE, dvstctr_reg);
2259 r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
2260 r8a66597_bset(r8a66597, BCHGE, get_intenb_reg(port));
2261 }
2262 }
2263
2264 r8a66597->bus_suspended = 1;
2265
2266 return 0;
2267}
2268
2269static int r8a66597_bus_resume(struct usb_hcd *hcd)
2270{
2271 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2272 int port;
2273
2274 dbg("%s", __func__);
2275
719a72b7 2276 for (port = 0; port < r8a66597->max_root_hub; port++) {
e1e609be
YS
2277 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2278 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2279
2280 if (!(rh->port & (1 << USB_PORT_FEAT_SUSPEND)))
2281 continue;
2282
2283 dbg("resume port = %d", port);
2284 rh->port &= ~(1 << USB_PORT_FEAT_SUSPEND);
2285 rh->port |= 1 << USB_PORT_FEAT_C_SUSPEND;
2286 r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
2287 msleep(50);
2288 r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
2289 }
2290
2291 return 0;
2292
2293}
2294#else
2295#define r8a66597_bus_suspend NULL
2296#define r8a66597_bus_resume NULL
2297#endif
2298
5d304358
YS
2299static struct hc_driver r8a66597_hc_driver = {
2300 .description = hcd_name,
2301 .hcd_priv_size = sizeof(struct r8a66597),
2302 .irq = r8a66597_irq,
2303
2304 /*
2305 * generic hardware linkage
2306 */
2307 .flags = HCD_USB2,
2308
2309 .start = r8a66597_start,
2310 .stop = r8a66597_stop,
2311
2312 /*
2313 * managing i/o requests and associated device resources
2314 */
2315 .urb_enqueue = r8a66597_urb_enqueue,
2316 .urb_dequeue = r8a66597_urb_dequeue,
2317 .endpoint_disable = r8a66597_endpoint_disable,
2318
2319 /*
2320 * periodic schedule support
2321 */
2322 .get_frame_number = r8a66597_get_frame,
2323
2324 /*
2325 * root hub support
2326 */
2327 .hub_status_data = r8a66597_hub_status_data,
2328 .hub_control = r8a66597_hub_control,
e1e609be
YS
2329 .bus_suspend = r8a66597_bus_suspend,
2330 .bus_resume = r8a66597_bus_resume,
5d304358
YS
2331};
2332
2333#if defined(CONFIG_PM)
ae1cef6e 2334static int r8a66597_suspend(struct device *dev)
5d304358 2335{
ae1cef6e 2336 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
e1e609be
YS
2337 int port;
2338
2339 dbg("%s", __func__);
2340
2341 disable_controller(r8a66597);
2342
719a72b7 2343 for (port = 0; port < r8a66597->max_root_hub; port++) {
e1e609be
YS
2344 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2345
2346 rh->port = 0x00000000;
2347 }
2348
5d304358
YS
2349 return 0;
2350}
2351
ae1cef6e 2352static int r8a66597_resume(struct device *dev)
5d304358 2353{
ae1cef6e 2354 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
e1e609be
YS
2355 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2356
2357 dbg("%s", __func__);
2358
2359 enable_controller(r8a66597);
2360 usb_root_hub_lost_power(hcd->self.root_hub);
2361
5d304358
YS
2362 return 0;
2363}
ae1cef6e 2364
47145210 2365static const struct dev_pm_ops r8a66597_dev_pm_ops = {
ae1cef6e
MD
2366 .suspend = r8a66597_suspend,
2367 .resume = r8a66597_resume,
3725f28b
YS
2368 .poweroff = r8a66597_suspend,
2369 .restore = r8a66597_resume,
ae1cef6e
MD
2370};
2371
2372#define R8A66597_DEV_PM_OPS (&r8a66597_dev_pm_ops)
5d304358 2373#else /* if defined(CONFIG_PM) */
ae1cef6e 2374#define R8A66597_DEV_PM_OPS NULL
5d304358
YS
2375#endif
2376
2377static int __init_or_module r8a66597_remove(struct platform_device *pdev)
2378{
2379 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2380 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2381
2382 del_timer_sync(&r8a66597->rh_timer);
5d304358 2383 usb_remove_hcd(hcd);
ca0677a2 2384 iounmap((void *)r8a66597->reg);
719a72b7
MD
2385#ifdef CONFIG_HAVE_CLK
2386 if (r8a66597->pdata->on_chip)
2387 clk_put(r8a66597->clk);
765786e0 2388#endif
5d304358
YS
2389 usb_put_hcd(hcd);
2390 return 0;
2391}
2392
8864bd86 2393static int __devinit r8a66597_probe(struct platform_device *pdev)
5d304358 2394{
719a72b7 2395#ifdef CONFIG_HAVE_CLK
765786e0
MD
2396 char clk_name[8];
2397#endif
27140219 2398 struct resource *res = NULL, *ires;
5d304358
YS
2399 int irq = -1;
2400 void __iomem *reg = NULL;
2401 struct usb_hcd *hcd = NULL;
2402 struct r8a66597 *r8a66597;
2403 int ret = 0;
2404 int i;
0bf32b80 2405 unsigned long irq_trigger;
5d304358
YS
2406
2407 if (pdev->dev.dma_mask) {
2408 ret = -EINVAL;
802f389a 2409 dev_err(&pdev->dev, "dma not supported\n");
5d304358
YS
2410 goto clean_up;
2411 }
2412
0a2e5b9b 2413 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5d304358
YS
2414 if (!res) {
2415 ret = -ENODEV;
0a2e5b9b 2416 dev_err(&pdev->dev, "platform_get_resource error.\n");
5d304358
YS
2417 goto clean_up;
2418 }
2419
27140219
MZ
2420 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2421 if (!ires) {
5d304358 2422 ret = -ENODEV;
802f389a
GKH
2423 dev_err(&pdev->dev,
2424 "platform_get_resource IORESOURCE_IRQ error.\n");
5d304358
YS
2425 goto clean_up;
2426 }
2427
27140219
MZ
2428 irq = ires->start;
2429 irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
2430
0a2e5b9b 2431 reg = ioremap(res->start, resource_size(res));
5d304358
YS
2432 if (reg == NULL) {
2433 ret = -ENOMEM;
802f389a 2434 dev_err(&pdev->dev, "ioremap error.\n");
5d304358
YS
2435 goto clean_up;
2436 }
2437
5effabbe
YS
2438 if (pdev->dev.platform_data == NULL) {
2439 dev_err(&pdev->dev, "no platform data\n");
2440 ret = -ENODEV;
2441 goto clean_up;
2442 }
2443
5d304358
YS
2444 /* initialize hcd */
2445 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2446 if (!hcd) {
2447 ret = -ENOMEM;
802f389a 2448 dev_err(&pdev->dev, "Failed to create hcd\n");
5d304358
YS
2449 goto clean_up;
2450 }
2451 r8a66597 = hcd_to_r8a66597(hcd);
2452 memset(r8a66597, 0, sizeof(struct r8a66597));
2453 dev_set_drvdata(&pdev->dev, r8a66597);
5effabbe
YS
2454 r8a66597->pdata = pdev->dev.platform_data;
2455 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
5d304358 2456
719a72b7
MD
2457 if (r8a66597->pdata->on_chip) {
2458#ifdef CONFIG_HAVE_CLK
2459 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
2460 r8a66597->clk = clk_get(&pdev->dev, clk_name);
2461 if (IS_ERR(r8a66597->clk)) {
2462 dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
2463 clk_name);
2464 ret = PTR_ERR(r8a66597->clk);
2465 goto clean_up2;
2466 }
765786e0 2467#endif
719a72b7
MD
2468 r8a66597->max_root_hub = 1;
2469 } else
2470 r8a66597->max_root_hub = 2;
765786e0 2471
5d304358
YS
2472 spin_lock_init(&r8a66597->lock);
2473 init_timer(&r8a66597->rh_timer);
2474 r8a66597->rh_timer.function = r8a66597_timer;
2475 r8a66597->rh_timer.data = (unsigned long)r8a66597;
2476 r8a66597->reg = (unsigned long)reg;
2477
e5ff15be
MD
2478 /* make sure no interrupts are pending */
2479 ret = r8a66597_clock_enable(r8a66597);
2480 if (ret < 0)
2481 goto clean_up3;
2482 disable_controller(r8a66597);
2483
5d304358
YS
2484 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2485 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2486 init_timer(&r8a66597->td_timer[i]);
2487 r8a66597->td_timer[i].function = r8a66597_td_timer;
2488 r8a66597->td_timer[i].data = (unsigned long)r8a66597;
6d879107
YS
2489 setup_timer(&r8a66597->interval_timer[i],
2490 r8a66597_interval_timer,
2491 (unsigned long)r8a66597);
5d304358
YS
2492 }
2493 INIT_LIST_HEAD(&r8a66597->child_device);
2494
2495 hcd->rsrc_start = res->start;
27140219 2496
0bf32b80 2497 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger);
5d304358 2498 if (ret != 0) {
802f389a 2499 dev_err(&pdev->dev, "Failed to add hcd\n");
765786e0 2500 goto clean_up3;
5d304358
YS
2501 }
2502
2503 return 0;
2504
765786e0 2505clean_up3:
719a72b7
MD
2506#ifdef CONFIG_HAVE_CLK
2507 if (r8a66597->pdata->on_chip)
2508 clk_put(r8a66597->clk);
765786e0 2509clean_up2:
d6435102 2510#endif
765786e0
MD
2511 usb_put_hcd(hcd);
2512
5d304358
YS
2513clean_up:
2514 if (reg)
2515 iounmap(reg);
5d304358
YS
2516
2517 return ret;
2518}
2519
2520static struct platform_driver r8a66597_driver = {
2521 .probe = r8a66597_probe,
2522 .remove = r8a66597_remove,
5d304358
YS
2523 .driver = {
2524 .name = (char *) hcd_name,
f4fce61d 2525 .owner = THIS_MODULE,
ae1cef6e 2526 .pm = R8A66597_DEV_PM_OPS,
5d304358
YS
2527 },
2528};
2529
2530static int __init r8a66597_init(void)
2531{
2532 if (usb_disabled())
2533 return -ENODEV;
2534
5909f6ea
GKH
2535 printk(KERN_INFO KBUILD_MODNAME ": driver %s, %s\n", hcd_name,
2536 DRIVER_VERSION);
5d304358
YS
2537 return platform_driver_register(&r8a66597_driver);
2538}
2539module_init(r8a66597_init);
2540
2541static void __exit r8a66597_cleanup(void)
2542{
2543 platform_driver_unregister(&r8a66597_driver);
2544}
2545module_exit(r8a66597_cleanup);
2546