blob: 0ceec123ddfd09928c86cc55fca0ef522937afae [file] [log] [blame]
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001/*
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>
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090029#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>
Yoshihiro Shimodae2945312007-07-18 23:10:34 +090037#include <linux/io.h>
38#include <linux/irq.h>
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090039
40#include "../core/hcd.h"
41#include "r8a66597.h"
42
43MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
44MODULE_LICENSE("GPL");
45MODULE_AUTHOR("Yoshihiro Shimoda");
Kay Sieversf4fce612008-04-10 21:29:22 -070046MODULE_ALIAS("platform:r8a66597_hcd");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090047
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +090048#define DRIVER_VERSION "2009-05-26"
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090049
50static const char hcd_name[] = "r8a66597_hcd";
51
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090052static 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
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +090089static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090090{
91 u16 tmp;
92 int i = 0;
93
Magnus Damm719a72b2009-07-17 14:59:55 +000094 if (r8a66597->pdata->on_chip) {
95#ifdef CONFIG_HAVE_CLK
96 clk_enable(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +090097#endif
Magnus Damm719a72b2009-07-17 14:59:55 +000098 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);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900119
Magnus Damm719a72b2009-07-17 14:59:55 +0000120 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 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900131
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900132 return 0;
133}
134
135static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
136{
137 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
138 udelay(1);
Magnus Damm719a72b2009-07-17 14:59:55 +0000139
140 if (r8a66597->pdata->on_chip) {
141#ifdef CONFIG_HAVE_CLK
142 clk_disable(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +0900143#endif
Magnus Damm719a72b2009-07-17 14:59:55 +0000144 } else {
145 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
146 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
147 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
148 }
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900149}
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));
170
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;
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +0900186 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;
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900189
190 ret = r8a66597_clock_enable(r8a66597);
191 if (ret < 0)
192 return ret;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900193
194 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900195 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
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900202 r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
203 r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
204 r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900205 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
206
207 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900208
Magnus Damm719a72b2009-07-17 14:59:55 +0000209 for (port = 0; port < r8a66597->max_root_hub; port++)
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900210 r8a66597_enable_port(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900211
212 return 0;
213}
214
215static void disable_controller(struct r8a66597 *r8a66597)
216{
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900217 int port;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900218
Magnus Damme5ff15b2010-01-27 07:41:19 +0000219 /* disable interrupts */
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900220 r8a66597_write(r8a66597, 0, INTENB0);
Magnus Damme5ff15b2010-01-27 07:41:19 +0000221 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);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900230
Magnus Damm719a72b2009-07-17 14:59:55 +0000231 for (port = 0; port < r8a66597->max_root_hub; port++)
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900232 r8a66597_disable_port(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900233
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900234 r8a66597_clock_disable(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900235}
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
Magnus Damm719a72b2009-07-17 14:59:55 +0000262static void get_port_number(struct r8a66597 *r8a66597,
263 char *devpath, u16 *root_port, u16 *hub_port)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900264{
265 if (root_port) {
266 *root_port = (devpath[0] & 0x0F) - 1;
Magnus Damm719a72b2009-07-17 14:59:55 +0000267 if (*root_port >= r8a66597->max_root_hub)
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700268 printk(KERN_ERR "r8a66597: Illegal root port number.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900269 }
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:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700289 printk(KERN_ERR "r8a66597: unknown speed\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900290 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;
Ming Leife9b9032008-06-08 16:13:03 +0800315 const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
316 const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
317 const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900318
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
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900355 dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900356 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
Magnus Damm719a72b2009-07-17 14:59:55 +0000369 get_port_number(r8a66597, urb->dev->devpath,
370 &dev->root_port, &dev->hub_port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900371 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)) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700389 dev_err(&urb->dev->dev, "External hub limit reached.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900390 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
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700410 dev_err(&urb->dev->dev,
411 "cannot communicate with a USB device more than 10.(%x)\n",
412 r8a66597->address_map);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900413
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
Magnus Damm719a72b2009-07-17 14:59:55 +0000435 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900436 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) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700452 printk(KERN_ERR "r8a66597: register%lx, loop %x "
453 "is timeout\n", reg, loop);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900454 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
Magnus Damm719a72b2009-07-17 14:59:55 +0000510static 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
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900518/* this function must be called with interrupt disabled */
519static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
520{
Magnus Damm719a72b2009-07-17 14:59:55 +0000521 unsigned short mbw = mbw_value(r8a66597);
522
523 r8a66597_mdfy(r8a66597, mbw | pipenum, mbw | CURPIPE, CFIFOSEL);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900524 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{
Magnus Damm719a72b2009-07-17 14:59:55 +0000531 unsigned short mbw = mbw_value(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900532
Magnus Damm719a72b2009-07-17 14:59:55 +0000533 cfifo_change(r8a66597, 0);
534 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D0FIFOSEL);
535 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D1FIFOSEL);
536
537 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum, mbw | CURPIPE,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900538 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
Yoshihiro Shimodaf6ace2c2007-05-30 20:42:41 +0900606 if (!toggle)
607 return;
608
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900609 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);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900635 r8a66597_write(r8a66597, info->interval, PIPEPERI);
636}
637
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900638/* 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));
Julia Lawall2e0fe702008-12-29 11:22:14 +0100669 switch (usb_endpoint_type(ep)) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900670 case USB_ENDPOINT_XFER_BULK:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100671 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900672 array[i++] = 4;
673 else {
674 array[i++] = 3;
675 array[i++] = 5;
676 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900677 break;
678 case USB_ENDPOINT_XFER_INT:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100679 if (usb_endpoint_dir_in(ep)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900680 array[i++] = 6;
681 array[i++] = 7;
682 array[i++] = 8;
683 } else
684 array[i++] = 9;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900685 break;
686 case USB_ENDPOINT_XFER_ISOC:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100687 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900688 array[i++] = 2;
689 else
690 array[i++] = 1;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900691 break;
692 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700693 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900694 return 0;
695 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900696
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
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900712 switch (type) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900713 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:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700723 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900724 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
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700742 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900743
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
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700758 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900759
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;
Magnus Damm719a72b2009-07-17 14:59:55 +0000771 unsigned short mbw = mbw_value(r8a66597);
772
773 /* pipe dma is only for external controlles */
774 if (r8a66597->pdata->on_chip)
775 return;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900776
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
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -0700782 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);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900788
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);
Magnus Damm719a72b2009-07-17 14:59:55 +0000794 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum,
795 mbw | CURPIPE, pipe->fifosel);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900796
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) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900834 if (td->address != address)
835 continue;
836
837 urb = td->urb;
838 list_del(&td->queue);
839 kfree(td);
840
841 if (urb) {
Alan Sterne9df41c2007-08-08 11:48:02 -0400842 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597),
843 urb);
844
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900845 spin_unlock(&r8a66597->lock);
Alan Stern4a000272007-08-24 15:42:24 -0400846 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb,
847 -ENODEV);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900848 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
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900884static 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
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900910static 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
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900929/* 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);
Julia Lawall2e0fe702008-12-29 11:22:14 +0100938 info.epnum = usb_endpoint_num(ep);
Yoshihiro Shimoda05eac912007-10-03 18:53:28 +0900939 info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
Julia Lawall2e0fe702008-12-29 11:22:14 +0100940 info.type = get_r8a66597_type(usb_endpoint_type(ep));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900941 info.bufnum = get_bufnum(info.pipenum);
942 info.buf_bsize = get_buf_bsize(info.pipenum);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900943 if (info.type == R8A66597_BULK) {
944 info.interval = 0;
945 info.timer_interval = 0;
946 } else {
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900947 info.interval = get_interval(urb, ep->bInterval);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900948 info.timer_interval = get_timer_interval(urb, ep->bInterval);
949 }
Julia Lawall2e0fe702008-12-29 11:22:14 +0100950 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900951 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
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +0900984static 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
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001006/* this function must be called with interrupt disabled */
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001007static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
1008 u16 syssts)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001009{
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001010 if (syssts == SE0) {
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001011 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001012 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
Yoshihiro Shimoda1e6159f2009-10-21 20:33:39 +09001013 } 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));
1018
1019 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
1020 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
1021
1022 if (r8a66597->bus_suspended)
1023 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001024 }
1025
Yoshihiro Shimoda1e6159f2009-10-21 20:33:39 +09001026 usb_hcd_poll_rh_status(r8a66597_to_hcd(r8a66597));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001027}
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
Yoshihiro Shimoda1e6159f2009-10-21 20:33:39 +09001035 rh->port &= ~((1 << USB_PORT_FEAT_HIGHSPEED) |
1036 (1 << USB_PORT_FEAT_LOWSPEED));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001037 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
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001051 disable_r8a66597_pipe_all(r8a66597, dev);
1052 free_usb_address(r8a66597, dev);
1053
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001054 start_root_hub_sampling(r8a66597, port, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001055}
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;
Al Virofd05e722008-04-28 07:00:16 +01001062 __le16 *p = (__le16 *)td->urb->setup_packet;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001063 unsigned long setup_addr = USBREQ;
1064
1065 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1066 DCPMAXP);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001067 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001068
1069 for (i = 0; i < 4; i++) {
Al Virofd05e722008-04-28 07:00:16 +01001070 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001071 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);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001098 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001099
1100 if (td->pipe->pipetre) {
1101 r8a66597_write(r8a66597, TRCLR,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001102 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001103 r8a66597_write(r8a66597,
Julia Lawalldfa5ec72008-03-04 15:25:11 -08001104 DIV_ROUND_UP
1105 (urb->transfer_buffer_length,
1106 td->maxpacket),
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001107 td->pipe->pipetrn);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001108 r8a66597_bset(r8a66597, TRENB,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001109 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001110 }
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 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001140 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001141
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);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001158 pipe_stop(r8a66597, td->pipe);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001159
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);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001164 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +09001165 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001166 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);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001172 enable_irq_ready(r8a66597, 0);
1173 }
1174 enable_irq_nrdy(r8a66597, 0);
1175 pipe_start(r8a66597, td->pipe);
1176}
1177
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001178static 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
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001187/* 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:
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001194 if (is_set_address(td->urb->setup_packet)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001195 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:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001213 printk(KERN_ERR "r8a66597: invalid type.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001214 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 */
Alan Sterndfd1e532007-08-21 15:36:52 -04001261static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
Alan Stern888fda42007-08-24 15:41:18 -04001262 u16 pipenum, struct urb *urb, int status)
Alan Sterndfd1e532007-08-21 15:36:52 -04001263__releases(r8a66597->lock) __acquires(r8a66597->lock)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001264{
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)) {
Alan Stern888fda42007-08-24 15:41:18 -04001271 if (td->set_address && (status != 0 || urb->unlinked))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001272 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
Alan Sterne9df41c2007-08-08 11:48:02 -04001286 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001287 spin_unlock(&r8a66597->lock);
Alan Stern4a000272007-08-24 15:42:24 -04001288 usb_hcd_giveback_urb(hcd, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001289 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
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001302static 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;
Alan Sterndfd1e532007-08-21 15:36:52 -04001310 int status = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001311
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)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001319 pipe_stop(r8a66597, td->pipe);
1320 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001321 printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001322 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001323 return;
1324 }
1325
1326 /* prepare parameters */
1327 rcv_len = tmp & DTLN;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001328 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 }
Alan Sterndfd1e532007-08-21 15:36:52 -04001336 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 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001344
1345 /* update parameters */
1346 urb->actual_length += size;
1347 if (rcv_len == 0)
1348 td->zero_packet = 1;
Alan Sterndfd1e532007-08-21 15:36:52 -04001349 if (rcv_len < bufsize) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001350 td->short_packet = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001351 }
1352 if (usb_pipeisoc(urb->pipe)) {
1353 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
Alan Sterndfd1e532007-08-21 15:36:52 -04001354 urb->iso_frame_desc[td->iso_cnt].status = status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001355 td->iso_cnt++;
Alan Sterndfd1e532007-08-21 15:36:52 -04001356 finish = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001357 }
1358
1359 /* check transfer finish */
Alan Sternb0d9efb2007-08-21 15:39:21 -04001360 if (finish || check_transfer_finish(td, urb)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001361 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
Alan Stern888fda42007-08-24 15:41:18 -04001375 if (finish && pipenum != 0)
1376 finish_request(r8a66597, td, pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001377}
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)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001394 pipe_stop(r8a66597, td->pipe);
1395 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001396 printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001397 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001398 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);
Greg Kroah-Hartman16e2e5f2009-03-03 16:44:13 -08001410 size = min_t(u32, bufsize,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001411 urb->transfer_buffer_length - urb->actual_length);
1412 }
1413
1414 /* write fifo */
1415 if (pipenum > 0)
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001416 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001417 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
Alan Stern888fda42007-08-24 15:41:18 -04001442static void check_next_phase(struct r8a66597 *r8a66597, int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001443{
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:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001455 if (check_transfer_finish(td, urb))
1456 td->type = USB_PID_ACK;
1457 break;
1458 case USB_PID_SETUP:
Alan Sterneb231052007-08-21 15:40:36 -04001459 if (urb->transfer_buffer_length == urb->actual_length)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001460 td->type = USB_PID_ACK;
Alan Sterneb231052007-08-21 15:40:36 -04001461 else if (usb_pipeout(urb->pipe))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001462 td->type = USB_PID_OUT;
1463 else
1464 td->type = USB_PID_IN;
1465 break;
1466 case USB_PID_ACK:
1467 finish = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001468 break;
1469 }
1470
Alan Stern888fda42007-08-24 15:41:18 -04001471 if (finish || status != 0 || urb->unlinked)
1472 finish_request(r8a66597, td, 0, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001473 else
1474 start_transfer(r8a66597, td);
1475}
1476
Alan Stern888fda42007-08-24 15:41:18 -04001477static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001478{
1479 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1480
Alan Stern888fda42007-08-24 15:41:18 -04001481 if (td) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001482 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1483
1484 if (pid == PID_NAK)
Alan Stern888fda42007-08-24 15:41:18 -04001485 return -ECONNRESET;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001486 else
Alan Stern888fda42007-08-24 15:41:18 -04001487 return -EPIPE;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001488 }
Alan Stern888fda42007-08-24 15:41:18 -04001489 return 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001490}
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);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001501 r8a66597_write(r8a66597, ~mask, BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001502 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);
Alan Stern888fda42007-08-24 15:41:18 -04001508 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001509 }
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);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001536 r8a66597_write(r8a66597, ~mask, BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001537 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);
Alan Stern888fda42007-08-24 15:41:18 -04001542 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001543 }
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);
Alan Stern888fda42007-08-24 15:41:18 -04001557 finish_request(r8a66597, td, pipenum, td->urb,
1558 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001559 }
1560 }
1561 }
1562}
1563
1564static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1565{
1566 u16 check;
1567 u16 pipenum;
1568 u16 mask;
Alan Stern888fda42007-08-24 15:41:18 -04001569 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001570
1571 mask = r8a66597_read(r8a66597, NRDYSTS)
1572 & r8a66597_read(r8a66597, NRDYENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001573 r8a66597_write(r8a66597, ~mask, NRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001574 if (mask & NRDY0) {
1575 cfifo_change(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001576 status = get_urb_error(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001577 pipe_irq_disable(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001578 check_next_phase(r8a66597, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001579 }
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
Alan Stern888fda42007-08-24 15:41:18 -04001589 status = get_urb_error(r8a66597, pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001590 pipe_irq_disable(r8a66597, pipenum);
1591 pipe_stop(r8a66597, td->pipe);
Alan Stern888fda42007-08-24 15:41:18 -04001592 finish_request(r8a66597, td, pipenum, td->urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001593 }
1594 }
1595}
1596
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001597static 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;
Alan Stern888fda42007-08-24 15:41:18 -04001603 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001604
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) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001619 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001620 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1621
1622 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001623 start_root_hub_sampling(r8a66597, 1, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001624 }
1625 if (mask2 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001626 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001627 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1628 r8a66597_usb_disconnect(r8a66597, 1);
1629 }
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09001630 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 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001635 }
1636
1637 if (mask1) {
1638 if (mask1 & ATTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001639 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001640 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1641
1642 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001643 start_root_hub_sampling(r8a66597, 0, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001644 }
1645 if (mask1 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001646 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001647 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1648 r8a66597_usb_disconnect(r8a66597, 0);
1649 }
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09001650 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
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001656 if (mask1 & SIGN) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001657 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001658 status = get_urb_error(r8a66597, 0);
1659 check_next_phase(r8a66597, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001660 }
1661 if (mask1 & SACK) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001662 r8a66597_write(r8a66597, ~SACK, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001663 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001664 }
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);
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001692 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001693 } else
1694 r8a66597_usb_connect(r8a66597, port);
1695 }
1696
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001697 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
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001702 if (rh->scount > 0) {
1703 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1704 if (tmp == rh->old_syssts) {
1705 rh->scount--;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001706 if (rh->scount == 0)
1707 r8a66597_check_syssts(r8a66597, port, tmp);
1708 else
1709 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001710 } else {
1711 rh->scount = R8A66597_MAX_SAMPLING;
1712 rh->old_syssts = tmp;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001713 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001714 }
1715 }
1716}
1717
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001718static 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
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001741static 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;
Yoshihiro Shimoda58639642008-11-11 16:47:21 +09001796 int port;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001797
1798 spin_lock_irqsave(&r8a66597->lock, flags);
1799
Magnus Damm719a72b2009-07-17 14:59:55 +00001800 for (port = 0; port < r8a66597->max_root_hub; port++)
Yoshihiro Shimoda58639642008-11-11 16:47:21 +09001801 r8a66597_root_hub_control(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001802
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);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001820
1821 hcd->state = HC_STATE_RUNNING;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001822 return enable_controller(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001823}
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) {
Magnus Damm719a72b2009-07-17 14:59:55 +00001838 get_port_number(r8a66597, urb->dev->devpath,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001839 &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,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001849 struct usb_host_endpoint *hep)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001850{
1851 struct r8a66597_td *td;
1852 u16 pipenum;
1853
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001854 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001855 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,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001877 struct urb *urb,
1878 gfp_t mem_flags)
1879{
Alan Sterne9df41c2007-08-08 11:48:02 -04001880 struct usb_host_endpoint *hep = urb->ep;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001881 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1882 struct r8a66597_td *td = NULL;
Alan Sterne9df41c2007-08-08 11:48:02 -04001883 int ret, request = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001884 unsigned long flags;
1885
1886 spin_lock_irqsave(&r8a66597->lock, flags);
1887 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1888 ret = -ENODEV;
Alan Sterne9df41c2007-08-08 11:48:02 -04001889 goto error_not_linked;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001890 }
1891
Alan Sterne9df41c2007-08-08 11:48:02 -04001892 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1893 if (ret)
1894 goto error_not_linked;
1895
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001896 if (!hep->hcpriv) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001897 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1898 GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001899 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);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001912 td = r8a66597_make_td(r8a66597, urb, hep);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001913 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]);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001920 urb->hcpriv = td;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001921
1922 if (request) {
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001923 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 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001934 }
1935 } else
1936 set_td_timer(r8a66597, td);
1937
1938error:
Alan Sterne9df41c2007-08-08 11:48:02 -04001939 if (ret)
1940 usb_hcd_unlink_urb_from_ep(hcd, urb);
1941error_not_linked:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001942 spin_unlock_irqrestore(&r8a66597->lock, flags);
1943 return ret;
1944}
1945
Alan Sterne9df41c2007-08-08 11:48:02 -04001946static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1947 int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001948{
1949 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1950 struct r8a66597_td *td;
1951 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001952 int rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001953
1954 spin_lock_irqsave(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001955 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1956 if (rc)
1957 goto done;
1958
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001959 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);
Alan Stern888fda42007-08-24 15:41:18 -04001964 finish_request(r8a66597, td, td->pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001965 }
Alan Sterne9df41c2007-08-08 11:48:02 -04001966 done:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001967 spin_unlock_irqrestore(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001968 return rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001969}
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;
Alan Stern888fda42007-08-24 15:41:18 -04001998 finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001999 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) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002035 if (dev->usb_address != addr)
2036 continue;
2037
2038 return dev;
2039 }
2040
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002041 printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002042 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
Magnus Damm719a72b2009-07-17 14:59:55 +00002111 for (i = 0; i < r8a66597->max_root_hub; i++) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002112 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;
Magnus Damm719a72b2009-07-17 14:59:55 +00002126 desc->bNbrPorts = r8a66597->max_root_hub;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002127 desc->bDescLength = 9;
2128 desc->bPwrOn2PwrGood = 0;
2129 desc->wHubCharacteristics = cpu_to_le16(0x0011);
Magnus Damm719a72b2009-07-17 14:59:55 +00002130 desc->bitmap[0] = ((1 << r8a66597->max_root_hub) - 1) << 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002131 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:
Magnus Damm719a72b2009-07-17 14:59:55 +00002158 if (wIndex > r8a66597->max_root_hub)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002159 goto error;
2160 if (wLength != 0)
2161 goto error;
2162
2163 switch (wValue) {
2164 case USB_PORT_FEAT_ENABLE:
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002165 rh->port &= ~(1 << USB_PORT_FEAT_POWER);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002166 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:
Magnus Damm719a72b2009-07-17 14:59:55 +00002191 if (wIndex > r8a66597->max_root_hub)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002192 goto error;
Al Virofd05e722008-04-28 07:00:16 +01002193 *(__le32 *)buf = cpu_to_le32(rh->port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002194 break;
2195 case SetPortFeature:
Magnus Damm719a72b2009-07-17 14:59:55 +00002196 if (wIndex > r8a66597->max_root_hub)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002197 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
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002237#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
Magnus Damm719a72b2009-07-17 14:59:55 +00002245 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002246 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
Magnus Damm719a72b2009-07-17 14:59:55 +00002276 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002277 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
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002299static 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,
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002329 .bus_suspend = r8a66597_bus_suspend,
2330 .bus_resume = r8a66597_bus_resume,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002331};
2332
2333#if defined(CONFIG_PM)
Magnus Dammae1cef62009-07-17 14:52:05 +00002334static int r8a66597_suspend(struct device *dev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002335{
Magnus Dammae1cef62009-07-17 14:52:05 +00002336 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002337 int port;
2338
2339 dbg("%s", __func__);
2340
2341 disable_controller(r8a66597);
2342
Magnus Damm719a72b2009-07-17 14:59:55 +00002343 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002344 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2345
2346 rh->port = 0x00000000;
2347 }
2348
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002349 return 0;
2350}
2351
Magnus Dammae1cef62009-07-17 14:52:05 +00002352static int r8a66597_resume(struct device *dev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002353{
Magnus Dammae1cef62009-07-17 14:52:05 +00002354 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002355 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
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002362 return 0;
2363}
Magnus Dammae1cef62009-07-17 14:52:05 +00002364
Alexey Dobriyan47145212009-12-14 18:00:08 -08002365static const struct dev_pm_ops r8a66597_dev_pm_ops = {
Magnus Dammae1cef62009-07-17 14:52:05 +00002366 .suspend = r8a66597_suspend,
2367 .resume = r8a66597_resume,
Yoshihiro Shimoda3725f282009-07-29 09:24:41 +00002368 .poweroff = r8a66597_suspend,
2369 .restore = r8a66597_resume,
Magnus Dammae1cef62009-07-17 14:52:05 +00002370};
2371
2372#define R8A66597_DEV_PM_OPS (&r8a66597_dev_pm_ops)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002373#else /* if defined(CONFIG_PM) */
Magnus Dammae1cef62009-07-17 14:52:05 +00002374#define R8A66597_DEV_PM_OPS NULL
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002375#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);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002383 usb_remove_hcd(hcd);
Yoshihiro Shimodaca0677a2007-10-05 15:53:12 +09002384 iounmap((void *)r8a66597->reg);
Magnus Damm719a72b2009-07-17 14:59:55 +00002385#ifdef CONFIG_HAVE_CLK
2386 if (r8a66597->pdata->on_chip)
2387 clk_put(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +09002388#endif
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002389 usb_put_hcd(hcd);
2390 return 0;
2391}
2392
Uwe Kleine-König8864bd82009-03-28 00:27:00 +01002393static int __devinit r8a66597_probe(struct platform_device *pdev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002394{
Magnus Damm719a72b2009-07-17 14:59:55 +00002395#ifdef CONFIG_HAVE_CLK
Magnus Damm765786e2008-10-31 20:22:38 +09002396 char clk_name[8];
2397#endif
Marc Zyngier27140212008-08-18 13:08:42 +02002398 struct resource *res = NULL, *ires;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002399 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;
Yoshihiro Shimoda0bf32b82008-06-27 19:09:55 +09002405 unsigned long irq_trigger;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002406
2407 if (pdev->dev.dma_mask) {
2408 ret = -EINVAL;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002409 dev_err(&pdev->dev, "dma not supported\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002410 goto clean_up;
2411 }
2412
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002413 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002414 if (!res) {
2415 ret = -ENODEV;
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002416 dev_err(&pdev->dev, "platform_get_resource error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002417 goto clean_up;
2418 }
2419
Marc Zyngier27140212008-08-18 13:08:42 +02002420 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2421 if (!ires) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002422 ret = -ENODEV;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002423 dev_err(&pdev->dev,
2424 "platform_get_resource IORESOURCE_IRQ error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002425 goto clean_up;
2426 }
2427
Marc Zyngier27140212008-08-18 13:08:42 +02002428 irq = ires->start;
2429 irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
2430
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002431 reg = ioremap(res->start, resource_size(res));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002432 if (reg == NULL) {
2433 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002434 dev_err(&pdev->dev, "ioremap error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002435 goto clean_up;
2436 }
2437
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +09002438 if (pdev->dev.platform_data == NULL) {
2439 dev_err(&pdev->dev, "no platform data\n");
2440 ret = -ENODEV;
2441 goto clean_up;
2442 }
2443
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002444 /* initialize hcd */
2445 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2446 if (!hcd) {
2447 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002448 dev_err(&pdev->dev, "Failed to create hcd\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002449 goto clean_up;
2450 }
2451 r8a66597 = hcd_to_r8a66597(hcd);
2452 memset(r8a66597, 0, sizeof(struct r8a66597));
2453 dev_set_drvdata(&pdev->dev, r8a66597);
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +09002454 r8a66597->pdata = pdev->dev.platform_data;
2455 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002456
Magnus Damm719a72b2009-07-17 14:59:55 +00002457 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 }
Magnus Damm765786e2008-10-31 20:22:38 +09002467#endif
Magnus Damm719a72b2009-07-17 14:59:55 +00002468 r8a66597->max_root_hub = 1;
2469 } else
2470 r8a66597->max_root_hub = 2;
Magnus Damm765786e2008-10-31 20:22:38 +09002471
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002472 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
Magnus Damme5ff15b2010-01-27 07:41:19 +00002478 /* 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
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002484 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;
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09002489 setup_timer(&r8a66597->interval_timer[i],
2490 r8a66597_interval_timer,
2491 (unsigned long)r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002492 }
2493 INIT_LIST_HEAD(&r8a66597->child_device);
2494
2495 hcd->rsrc_start = res->start;
Marc Zyngier27140212008-08-18 13:08:42 +02002496
Yoshihiro Shimoda0bf32b82008-06-27 19:09:55 +09002497 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002498 if (ret != 0) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002499 dev_err(&pdev->dev, "Failed to add hcd\n");
Magnus Damm765786e2008-10-31 20:22:38 +09002500 goto clean_up3;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002501 }
2502
2503 return 0;
2504
Magnus Damm765786e2008-10-31 20:22:38 +09002505clean_up3:
Magnus Damm719a72b2009-07-17 14:59:55 +00002506#ifdef CONFIG_HAVE_CLK
2507 if (r8a66597->pdata->on_chip)
2508 clk_put(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +09002509clean_up2:
Paul Mundtd6435102008-11-18 12:40:39 +09002510#endif
Magnus Damm765786e2008-10-31 20:22:38 +09002511 usb_put_hcd(hcd);
2512
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002513clean_up:
2514 if (reg)
2515 iounmap(reg);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002516
2517 return ret;
2518}
2519
2520static struct platform_driver r8a66597_driver = {
2521 .probe = r8a66597_probe,
2522 .remove = r8a66597_remove,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002523 .driver = {
2524 .name = (char *) hcd_name,
Kay Sieversf4fce612008-04-10 21:29:22 -07002525 .owner = THIS_MODULE,
Magnus Dammae1cef62009-07-17 14:52:05 +00002526 .pm = R8A66597_DEV_PM_OPS,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002527 },
2528};
2529
2530static int __init r8a66597_init(void)
2531{
2532 if (usb_disabled())
2533 return -ENODEV;
2534
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -07002535 printk(KERN_INFO KBUILD_MODNAME ": driver %s, %s\n", hcd_name,
2536 DRIVER_VERSION);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002537 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