blob: 50a3e2d6a6ccfb5d411325d99bed299be4e066a9 [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>
Paul Mundt27175682010-02-04 06:57:58 +000038#include <linux/mm.h>
Yoshihiro Shimodae2945312007-07-18 23:10:34 +090039#include <linux/irq.h>
Paul Mundt27175682010-02-04 06:57:58 +000040#include <asm/cacheflush.h>
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090041
42#include "../core/hcd.h"
43#include "r8a66597.h"
44
45MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
46MODULE_LICENSE("GPL");
47MODULE_AUTHOR("Yoshihiro Shimoda");
Kay Sieversf4fce612008-04-10 21:29:22 -070048MODULE_ALIAS("platform:r8a66597_hcd");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090049
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +090050#define DRIVER_VERSION "2009-05-26"
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090051
52static const char hcd_name[] = "r8a66597_hcd";
53
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090054static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
55static int r8a66597_get_frame(struct usb_hcd *hcd);
56
57/* this function must be called with interrupt disabled */
58static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
59 unsigned long reg)
60{
61 u16 tmp;
62
63 tmp = r8a66597_read(r8a66597, INTENB0);
64 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
65 r8a66597_bset(r8a66597, 1 << pipenum, reg);
66 r8a66597_write(r8a66597, tmp, INTENB0);
67}
68
69/* this function must be called with interrupt disabled */
70static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
71 unsigned long reg)
72{
73 u16 tmp;
74
75 tmp = r8a66597_read(r8a66597, INTENB0);
76 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
77 r8a66597_bclr(r8a66597, 1 << pipenum, reg);
78 r8a66597_write(r8a66597, tmp, INTENB0);
79}
80
81static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
82 u16 usbspd, u8 upphub, u8 hubport, int port)
83{
84 u16 val;
85 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
86
87 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
88 r8a66597_write(r8a66597, val, devadd_reg);
89}
90
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +090091static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090092{
93 u16 tmp;
94 int i = 0;
95
Magnus Damm719a72b2009-07-17 14:59:55 +000096 if (r8a66597->pdata->on_chip) {
97#ifdef CONFIG_HAVE_CLK
98 clk_enable(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +090099#endif
Magnus Damm719a72b2009-07-17 14:59:55 +0000100 do {
101 r8a66597_write(r8a66597, SCKE, SYSCFG0);
102 tmp = r8a66597_read(r8a66597, SYSCFG0);
103 if (i++ > 1000) {
104 printk(KERN_ERR "r8a66597: reg access fail.\n");
105 return -ENXIO;
106 }
107 } while ((tmp & SCKE) != SCKE);
108 r8a66597_write(r8a66597, 0x04, 0x02);
109 } else {
110 do {
111 r8a66597_write(r8a66597, USBE, SYSCFG0);
112 tmp = r8a66597_read(r8a66597, SYSCFG0);
113 if (i++ > 1000) {
114 printk(KERN_ERR "r8a66597: reg access fail.\n");
115 return -ENXIO;
116 }
117 } while ((tmp & USBE) != USBE);
118 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
119 r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata),
120 XTAL, SYSCFG0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900121
Magnus Damm719a72b2009-07-17 14:59:55 +0000122 i = 0;
123 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
124 do {
125 msleep(1);
126 tmp = r8a66597_read(r8a66597, SYSCFG0);
127 if (i++ > 500) {
128 printk(KERN_ERR "r8a66597: reg access fail.\n");
129 return -ENXIO;
130 }
131 } while ((tmp & SCKE) != SCKE);
132 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900133
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900134 return 0;
135}
136
137static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
138{
139 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
140 udelay(1);
Magnus Damm719a72b2009-07-17 14:59:55 +0000141
142 if (r8a66597->pdata->on_chip) {
143#ifdef CONFIG_HAVE_CLK
144 clk_disable(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +0900145#endif
Magnus Damm719a72b2009-07-17 14:59:55 +0000146 } else {
147 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
148 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
149 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
150 }
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900151}
152
153static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
154{
155 u16 val;
156
157 val = port ? DRPD : DCFM | DRPD;
158 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
159 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
160
161 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
162 r8a66597_bclr(r8a66597, DTCHE, get_intenb_reg(port));
163 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
164}
165
166static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
167{
168 u16 val, tmp;
169
170 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
171 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
172
173 r8a66597_port_power(r8a66597, port, 0);
174
175 do {
176 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
177 udelay(640);
178 } while (tmp == EDGESTS);
179
180 val = port ? DRPD : DCFM | DRPD;
181 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
182 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
183}
184
185static int enable_controller(struct r8a66597 *r8a66597)
186{
187 int ret, port;
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +0900188 u16 vif = r8a66597->pdata->vif ? LDRV : 0;
189 u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
190 u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900191
192 ret = r8a66597_clock_enable(r8a66597);
193 if (ret < 0)
194 return ret;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900195
196 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900197 r8a66597_bset(r8a66597, USBE, SYSCFG0);
198
199 r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
200 r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
201 r8a66597_bset(r8a66597, BRDY0, BRDYENB);
202 r8a66597_bset(r8a66597, BEMP0, BEMPENB);
203
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900204 r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
205 r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
206 r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900207 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
208
209 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900210
Magnus Damm719a72b2009-07-17 14:59:55 +0000211 for (port = 0; port < r8a66597->max_root_hub; port++)
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900212 r8a66597_enable_port(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900213
214 return 0;
215}
216
217static void disable_controller(struct r8a66597 *r8a66597)
218{
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900219 int port;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900220
Magnus Damme5ff15b2010-01-27 07:41:19 +0000221 /* disable interrupts */
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900222 r8a66597_write(r8a66597, 0, INTENB0);
Magnus Damme5ff15b2010-01-27 07:41:19 +0000223 r8a66597_write(r8a66597, 0, INTENB1);
224 r8a66597_write(r8a66597, 0, BRDYENB);
225 r8a66597_write(r8a66597, 0, BEMPENB);
226 r8a66597_write(r8a66597, 0, NRDYENB);
227
228 /* clear status */
229 r8a66597_write(r8a66597, 0, BRDYSTS);
230 r8a66597_write(r8a66597, 0, NRDYSTS);
231 r8a66597_write(r8a66597, 0, BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900232
Magnus Damm719a72b2009-07-17 14:59:55 +0000233 for (port = 0; port < r8a66597->max_root_hub; port++)
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900234 r8a66597_disable_port(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900235
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900236 r8a66597_clock_disable(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900237}
238
239static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
240 struct usb_device *udev)
241{
242 struct r8a66597_device *dev;
243
244 if (udev->parent && udev->parent->devnum != 1)
245 udev = udev->parent;
246
247 dev = dev_get_drvdata(&udev->dev);
248 if (dev)
249 return dev->address;
250 else
251 return 0;
252}
253
254static int is_child_device(char *devpath)
255{
256 return (devpath[2] ? 1 : 0);
257}
258
259static int is_hub_limit(char *devpath)
260{
261 return ((strlen(devpath) >= 4) ? 1 : 0);
262}
263
Magnus Damm719a72b2009-07-17 14:59:55 +0000264static void get_port_number(struct r8a66597 *r8a66597,
265 char *devpath, u16 *root_port, u16 *hub_port)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900266{
267 if (root_port) {
268 *root_port = (devpath[0] & 0x0F) - 1;
Magnus Damm719a72b2009-07-17 14:59:55 +0000269 if (*root_port >= r8a66597->max_root_hub)
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700270 printk(KERN_ERR "r8a66597: Illegal root port number.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900271 }
272 if (hub_port)
273 *hub_port = devpath[2] & 0x0F;
274}
275
276static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
277{
278 u16 usbspd = 0;
279
280 switch (speed) {
281 case USB_SPEED_LOW:
282 usbspd = LSMODE;
283 break;
284 case USB_SPEED_FULL:
285 usbspd = FSMODE;
286 break;
287 case USB_SPEED_HIGH:
288 usbspd = HSMODE;
289 break;
290 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700291 printk(KERN_ERR "r8a66597: unknown speed\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900292 break;
293 }
294
295 return usbspd;
296}
297
298static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
299{
300 int idx;
301
302 idx = address / 32;
303 r8a66597->child_connect_map[idx] |= 1 << (address % 32);
304}
305
306static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
307{
308 int idx;
309
310 idx = address / 32;
311 r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
312}
313
314static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
315{
316 u16 pipenum = pipe->info.pipenum;
Ming Leife9b9032008-06-08 16:13:03 +0800317 const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
318 const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
319 const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900320
321 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
322 dma_ch = R8A66597_PIPE_NO_DMA;
323
324 pipe->fifoaddr = fifoaddr[dma_ch];
325 pipe->fifosel = fifosel[dma_ch];
326 pipe->fifoctr = fifoctr[dma_ch];
327
328 if (pipenum == 0)
329 pipe->pipectr = DCPCTR;
330 else
331 pipe->pipectr = get_pipectr_addr(pipenum);
332
333 if (check_bulk_or_isoc(pipenum)) {
334 pipe->pipetre = get_pipetre_addr(pipenum);
335 pipe->pipetrn = get_pipetrn_addr(pipenum);
336 } else {
337 pipe->pipetre = 0;
338 pipe->pipetrn = 0;
339 }
340}
341
342static struct r8a66597_device *
343get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
344{
345 if (usb_pipedevice(urb->pipe) == 0)
346 return &r8a66597->device0;
347
348 return dev_get_drvdata(&urb->dev->dev);
349}
350
351static int make_r8a66597_device(struct r8a66597 *r8a66597,
352 struct urb *urb, u8 addr)
353{
354 struct r8a66597_device *dev;
355 int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
356
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900357 dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900358 if (dev == NULL)
359 return -ENOMEM;
360
361 dev_set_drvdata(&urb->dev->dev, dev);
362 dev->udev = urb->dev;
363 dev->address = addr;
364 dev->usb_address = usb_address;
365 dev->state = USB_STATE_ADDRESS;
366 dev->ep_in_toggle = 0;
367 dev->ep_out_toggle = 0;
368 INIT_LIST_HEAD(&dev->device_list);
369 list_add_tail(&dev->device_list, &r8a66597->child_device);
370
Magnus Damm719a72b2009-07-17 14:59:55 +0000371 get_port_number(r8a66597, urb->dev->devpath,
372 &dev->root_port, &dev->hub_port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900373 if (!is_child_device(urb->dev->devpath))
374 r8a66597->root_hub[dev->root_port].dev = dev;
375
376 set_devadd_reg(r8a66597, dev->address,
377 get_r8a66597_usb_speed(urb->dev->speed),
378 get_parent_r8a66597_address(r8a66597, urb->dev),
379 dev->hub_port, dev->root_port);
380
381 return 0;
382}
383
384/* this function must be called with interrupt disabled */
385static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
386{
387 u8 addr; /* R8A66597's address */
388 struct r8a66597_device *dev;
389
390 if (is_hub_limit(urb->dev->devpath)) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700391 dev_err(&urb->dev->dev, "External hub limit reached.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900392 return 0;
393 }
394
395 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
396 if (dev && dev->state >= USB_STATE_ADDRESS)
397 return dev->address;
398
399 for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
400 if (r8a66597->address_map & (1 << addr))
401 continue;
402
403 dbg("alloc_address: r8a66597_addr=%d", addr);
404 r8a66597->address_map |= 1 << addr;
405
406 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
407 return 0;
408
409 return addr;
410 }
411
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700412 dev_err(&urb->dev->dev,
413 "cannot communicate with a USB device more than 10.(%x)\n",
414 r8a66597->address_map);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900415
416 return 0;
417}
418
419/* this function must be called with interrupt disabled */
420static void free_usb_address(struct r8a66597 *r8a66597,
421 struct r8a66597_device *dev)
422{
423 int port;
424
425 if (!dev)
426 return;
427
428 dbg("free_addr: addr=%d", dev->address);
429
430 dev->state = USB_STATE_DEFAULT;
431 r8a66597->address_map &= ~(1 << dev->address);
432 dev->address = 0;
433 dev_set_drvdata(&dev->udev->dev, NULL);
434 list_del(&dev->device_list);
435 kfree(dev);
436
Magnus Damm719a72b2009-07-17 14:59:55 +0000437 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900438 if (r8a66597->root_hub[port].dev == dev) {
439 r8a66597->root_hub[port].dev = NULL;
440 break;
441 }
442 }
443}
444
445static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
446 u16 mask, u16 loop)
447{
448 u16 tmp;
449 int i = 0;
450
451 do {
452 tmp = r8a66597_read(r8a66597, reg);
453 if (i++ > 1000000) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700454 printk(KERN_ERR "r8a66597: register%lx, loop %x "
455 "is timeout\n", reg, loop);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900456 break;
457 }
458 ndelay(1);
459 } while ((tmp & mask) != loop);
460}
461
462/* this function must be called with interrupt disabled */
463static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
464{
465 u16 tmp;
466
467 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
468 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
469 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
470 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
471}
472
473/* this function must be called with interrupt disabled */
474static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
475{
476 u16 tmp;
477
478 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
479 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
480 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
481 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
482 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
483}
484
485/* this function must be called with interrupt disabled */
486static void clear_all_buffer(struct r8a66597 *r8a66597,
487 struct r8a66597_pipe *pipe)
488{
489 u16 tmp;
490
491 if (!pipe || pipe->info.pipenum == 0)
492 return;
493
494 pipe_stop(r8a66597, pipe);
495 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
496 tmp = r8a66597_read(r8a66597, pipe->pipectr);
497 tmp = r8a66597_read(r8a66597, pipe->pipectr);
498 tmp = r8a66597_read(r8a66597, pipe->pipectr);
499 r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
500}
501
502/* this function must be called with interrupt disabled */
503static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
504 struct r8a66597_pipe *pipe, int toggle)
505{
506 if (toggle)
507 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
508 else
509 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
510}
511
Magnus Damm719a72b2009-07-17 14:59:55 +0000512static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
513{
514 if (r8a66597->pdata->on_chip)
515 return MBW_32;
516 else
517 return MBW_16;
518}
519
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900520/* this function must be called with interrupt disabled */
521static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
522{
Magnus Damm719a72b2009-07-17 14:59:55 +0000523 unsigned short mbw = mbw_value(r8a66597);
524
525 r8a66597_mdfy(r8a66597, mbw | pipenum, mbw | CURPIPE, CFIFOSEL);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900526 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
527}
528
529/* this function must be called with interrupt disabled */
530static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
531 struct r8a66597_pipe *pipe)
532{
Magnus Damm719a72b2009-07-17 14:59:55 +0000533 unsigned short mbw = mbw_value(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900534
Magnus Damm719a72b2009-07-17 14:59:55 +0000535 cfifo_change(r8a66597, 0);
536 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D0FIFOSEL);
537 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D1FIFOSEL);
538
539 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum, mbw | CURPIPE,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900540 pipe->fifosel);
541 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
542}
543
544static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
545{
546 struct r8a66597_pipe *pipe = hep->hcpriv;
547
548 if (usb_pipeendpoint(urb->pipe) == 0)
549 return 0;
550 else
551 return pipe->info.pipenum;
552}
553
554static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
555{
556 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
557
558 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
559}
560
561static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
562 int urb_pipe)
563{
564 if (!dev)
565 return NULL;
566
567 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
568}
569
570/* this function must be called with interrupt disabled */
571static void pipe_toggle_set(struct r8a66597 *r8a66597,
572 struct r8a66597_pipe *pipe,
573 struct urb *urb, int set)
574{
575 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
576 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
577 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
578
579 if (!toggle)
580 return;
581
582 if (set)
583 *toggle |= 1 << endpoint;
584 else
585 *toggle &= ~(1 << endpoint);
586}
587
588/* this function must be called with interrupt disabled */
589static void pipe_toggle_save(struct r8a66597 *r8a66597,
590 struct r8a66597_pipe *pipe,
591 struct urb *urb)
592{
593 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
594 pipe_toggle_set(r8a66597, pipe, urb, 1);
595 else
596 pipe_toggle_set(r8a66597, pipe, urb, 0);
597}
598
599/* this function must be called with interrupt disabled */
600static void pipe_toggle_restore(struct r8a66597 *r8a66597,
601 struct r8a66597_pipe *pipe,
602 struct urb *urb)
603{
604 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
605 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
606 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
607
Yoshihiro Shimodaf6ace2c2007-05-30 20:42:41 +0900608 if (!toggle)
609 return;
610
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900611 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
612}
613
614/* this function must be called with interrupt disabled */
615static void pipe_buffer_setting(struct r8a66597 *r8a66597,
616 struct r8a66597_pipe_info *info)
617{
618 u16 val = 0;
619
620 if (info->pipenum == 0)
621 return;
622
623 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
624 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
625 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
626 if (!info->dir_in)
627 val |= R8A66597_DIR;
628 if (info->type == R8A66597_BULK && info->dir_in)
629 val |= R8A66597_DBLB | R8A66597_SHTNAK;
630 val |= info->type | info->epnum;
631 r8a66597_write(r8a66597, val, PIPECFG);
632
633 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
634 PIPEBUF);
635 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
636 PIPEMAXP);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900637 r8a66597_write(r8a66597, info->interval, PIPEPERI);
638}
639
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900640/* this function must be called with interrupt disabled */
641static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
642{
643 struct r8a66597_pipe_info *info;
644 struct urb *urb = td->urb;
645
646 if (td->pipenum > 0) {
647 info = &td->pipe->info;
648 cfifo_change(r8a66597, 0);
649 pipe_buffer_setting(r8a66597, info);
650
651 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
652 usb_pipeout(urb->pipe)) &&
653 !usb_pipecontrol(urb->pipe)) {
654 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
655 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
656 clear_all_buffer(r8a66597, td->pipe);
657 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
658 usb_pipeout(urb->pipe), 1);
659 }
660 pipe_toggle_restore(r8a66597, td->pipe, urb);
661 }
662}
663
664/* this function must be called with interrupt disabled */
665static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
666 struct usb_endpoint_descriptor *ep)
667{
668 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
669
670 memset(array, 0, sizeof(array));
Julia Lawall2e0fe702008-12-29 11:22:14 +0100671 switch (usb_endpoint_type(ep)) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900672 case USB_ENDPOINT_XFER_BULK:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100673 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900674 array[i++] = 4;
675 else {
676 array[i++] = 3;
677 array[i++] = 5;
678 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900679 break;
680 case USB_ENDPOINT_XFER_INT:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100681 if (usb_endpoint_dir_in(ep)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900682 array[i++] = 6;
683 array[i++] = 7;
684 array[i++] = 8;
685 } else
686 array[i++] = 9;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900687 break;
688 case USB_ENDPOINT_XFER_ISOC:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100689 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900690 array[i++] = 2;
691 else
692 array[i++] = 1;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900693 break;
694 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700695 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900696 return 0;
697 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900698
699 i = 1;
700 min = array[0];
701 while (array[i] != 0) {
702 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
703 min = array[i];
704 i++;
705 }
706
707 return min;
708}
709
710static u16 get_r8a66597_type(__u8 type)
711{
712 u16 r8a66597_type;
713
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900714 switch (type) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900715 case USB_ENDPOINT_XFER_BULK:
716 r8a66597_type = R8A66597_BULK;
717 break;
718 case USB_ENDPOINT_XFER_INT:
719 r8a66597_type = R8A66597_INT;
720 break;
721 case USB_ENDPOINT_XFER_ISOC:
722 r8a66597_type = R8A66597_ISO;
723 break;
724 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700725 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900726 r8a66597_type = 0x0000;
727 break;
728 }
729
730 return r8a66597_type;
731}
732
733static u16 get_bufnum(u16 pipenum)
734{
735 u16 bufnum = 0;
736
737 if (pipenum == 0)
738 bufnum = 0;
739 else if (check_bulk_or_isoc(pipenum))
740 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
741 else if (check_interrupt(pipenum))
742 bufnum = 4 + (pipenum - 6);
743 else
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700744 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900745
746 return bufnum;
747}
748
749static u16 get_buf_bsize(u16 pipenum)
750{
751 u16 buf_bsize = 0;
752
753 if (pipenum == 0)
754 buf_bsize = 3;
755 else if (check_bulk_or_isoc(pipenum))
756 buf_bsize = R8A66597_BUF_BSIZE - 1;
757 else if (check_interrupt(pipenum))
758 buf_bsize = 0;
759 else
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700760 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900761
762 return buf_bsize;
763}
764
765/* this function must be called with interrupt disabled */
766static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
767 struct r8a66597_device *dev,
768 struct r8a66597_pipe *pipe,
769 struct urb *urb)
770{
771 int i;
772 struct r8a66597_pipe_info *info = &pipe->info;
Magnus Damm719a72b2009-07-17 14:59:55 +0000773 unsigned short mbw = mbw_value(r8a66597);
774
775 /* pipe dma is only for external controlles */
776 if (r8a66597->pdata->on_chip)
777 return;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900778
779 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
780 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
781 if ((r8a66597->dma_map & (1 << i)) != 0)
782 continue;
783
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -0700784 dev_info(&dev->udev->dev,
785 "address %d, EndpointAddress 0x%02x use "
786 "DMA FIFO\n", usb_pipedevice(urb->pipe),
787 info->dir_in ?
788 USB_ENDPOINT_DIR_MASK + info->epnum
789 : info->epnum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900790
791 r8a66597->dma_map |= 1 << i;
792 dev->dma_map |= 1 << i;
793 set_pipe_reg_addr(pipe, i);
794
795 cfifo_change(r8a66597, 0);
Magnus Damm719a72b2009-07-17 14:59:55 +0000796 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum,
797 mbw | CURPIPE, pipe->fifosel);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900798
799 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
800 pipe->info.pipenum);
801 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
802 break;
803 }
804 }
805}
806
807/* this function must be called with interrupt disabled */
808static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
809 struct usb_host_endpoint *hep,
810 struct r8a66597_pipe_info *info)
811{
812 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
813 struct r8a66597_pipe *pipe = hep->hcpriv;
814
815 dbg("enable_pipe:");
816
817 pipe->info = *info;
818 set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
819 r8a66597->pipe_cnt[pipe->info.pipenum]++;
820 dev->pipe_cnt[pipe->info.pipenum]++;
821
822 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
823}
824
Paul Mundt27175682010-02-04 06:57:58 +0000825static void r8a66597_urb_done(struct r8a66597 *r8a66597, struct urb *urb,
826 int status)
827__releases(r8a66597->lock)
828__acquires(r8a66597->lock)
829{
830 if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
831 void *ptr;
832
833 for (ptr = urb->transfer_buffer;
834 ptr < urb->transfer_buffer + urb->transfer_buffer_length;
835 ptr += PAGE_SIZE)
836 flush_dcache_page(virt_to_page(ptr));
837 }
838
839 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
840 spin_unlock(&r8a66597->lock);
841 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb, status);
842 spin_lock(&r8a66597->lock);
843}
844
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900845/* this function must be called with interrupt disabled */
846static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
847{
848 struct r8a66597_td *td, *next;
849 struct urb *urb;
850 struct list_head *list = &r8a66597->pipe_queue[pipenum];
851
852 if (list_empty(list))
853 return;
854
855 list_for_each_entry_safe(td, next, list, queue) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900856 if (td->address != address)
857 continue;
858
859 urb = td->urb;
860 list_del(&td->queue);
861 kfree(td);
862
Paul Mundt27175682010-02-04 06:57:58 +0000863 if (urb)
864 r8a66597_urb_done(r8a66597, urb, -ENODEV);
Alan Sterne9df41c2007-08-08 11:48:02 -0400865
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900866 break;
867 }
868}
869
870/* this function must be called with interrupt disabled */
871static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
872 struct r8a66597_device *dev)
873{
874 int check_ep0 = 0;
875 u16 pipenum;
876
877 if (!dev)
878 return;
879
880 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
881 if (!dev->pipe_cnt[pipenum])
882 continue;
883
884 if (!check_ep0) {
885 check_ep0 = 1;
886 force_dequeue(r8a66597, 0, dev->address);
887 }
888
889 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
890 dev->pipe_cnt[pipenum] = 0;
891 force_dequeue(r8a66597, pipenum, dev->address);
892 }
893
894 dbg("disable_pipe");
895
896 r8a66597->dma_map &= ~(dev->dma_map);
897 dev->dma_map = 0;
898}
899
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900900static u16 get_interval(struct urb *urb, __u8 interval)
901{
902 u16 time = 1;
903 int i;
904
905 if (urb->dev->speed == USB_SPEED_HIGH) {
906 if (interval > IITV)
907 time = IITV;
908 else
909 time = interval ? interval - 1 : 0;
910 } else {
911 if (interval > 128) {
912 time = IITV;
913 } else {
914 /* calculate the nearest value for PIPEPERI */
915 for (i = 0; i < 7; i++) {
916 if ((1 << i) < interval &&
917 (1 << (i + 1) > interval))
918 time = 1 << i;
919 }
920 }
921 }
922
923 return time;
924}
925
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900926static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
927{
928 __u8 i;
929 unsigned long time = 1;
930
931 if (usb_pipeisoc(urb->pipe))
932 return 0;
933
934 if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
935 for (i = 0; i < (interval - 1); i++)
936 time *= 2;
937 time = time * 125 / 1000; /* uSOF -> msec */
938 } else {
939 time = interval;
940 }
941
942 return time;
943}
944
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900945/* this function must be called with interrupt disabled */
946static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
947 struct usb_host_endpoint *hep,
948 struct usb_endpoint_descriptor *ep)
949{
950 struct r8a66597_pipe_info info;
951
952 info.pipenum = get_empty_pipenum(r8a66597, ep);
953 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
Julia Lawall2e0fe702008-12-29 11:22:14 +0100954 info.epnum = usb_endpoint_num(ep);
Yoshihiro Shimoda05eac912007-10-03 18:53:28 +0900955 info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
Julia Lawall2e0fe702008-12-29 11:22:14 +0100956 info.type = get_r8a66597_type(usb_endpoint_type(ep));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900957 info.bufnum = get_bufnum(info.pipenum);
958 info.buf_bsize = get_buf_bsize(info.pipenum);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900959 if (info.type == R8A66597_BULK) {
960 info.interval = 0;
961 info.timer_interval = 0;
962 } else {
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900963 info.interval = get_interval(urb, ep->bInterval);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900964 info.timer_interval = get_timer_interval(urb, ep->bInterval);
965 }
Julia Lawall2e0fe702008-12-29 11:22:14 +0100966 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900967 info.dir_in = 1;
968 else
969 info.dir_in = 0;
970
971 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
972}
973
974static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
975{
976 struct r8a66597_device *dev;
977
978 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
979 dev->state = USB_STATE_CONFIGURED;
980}
981
982static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
983 u16 pipenum)
984{
985 if (pipenum == 0 && usb_pipeout(urb->pipe))
986 enable_irq_empty(r8a66597, pipenum);
987 else
988 enable_irq_ready(r8a66597, pipenum);
989
990 if (!usb_pipeisoc(urb->pipe))
991 enable_irq_nrdy(r8a66597, pipenum);
992}
993
994static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
995{
996 disable_irq_ready(r8a66597, pipenum);
997 disable_irq_nrdy(r8a66597, pipenum);
998}
999
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001000static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
1001{
1002 mod_timer(&r8a66597->rh_timer,
1003 jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
1004}
1005
1006static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
1007 int connect)
1008{
1009 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1010
1011 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1012 rh->scount = R8A66597_MAX_SAMPLING;
1013 if (connect)
1014 rh->port |= 1 << USB_PORT_FEAT_CONNECTION;
1015 else
1016 rh->port &= ~(1 << USB_PORT_FEAT_CONNECTION);
1017 rh->port |= 1 << USB_PORT_FEAT_C_CONNECTION;
1018
1019 r8a66597_root_hub_start_polling(r8a66597);
1020}
1021
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001022/* this function must be called with interrupt disabled */
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001023static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
1024 u16 syssts)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001025{
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001026 if (syssts == SE0) {
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001027 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001028 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
Yoshihiro Shimoda1e6159f2009-10-21 20:33:39 +09001029 } else {
1030 if (syssts == FS_JSTS)
1031 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
1032 else if (syssts == LS_JSTS)
1033 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
1034
1035 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
1036 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
1037
1038 if (r8a66597->bus_suspended)
1039 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001040 }
1041
Yoshihiro Shimoda1e6159f2009-10-21 20:33:39 +09001042 usb_hcd_poll_rh_status(r8a66597_to_hcd(r8a66597));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001043}
1044
1045/* this function must be called with interrupt disabled */
1046static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
1047{
1048 u16 speed = get_rh_usb_speed(r8a66597, port);
1049 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1050
Yoshihiro Shimoda1e6159f2009-10-21 20:33:39 +09001051 rh->port &= ~((1 << USB_PORT_FEAT_HIGHSPEED) |
1052 (1 << USB_PORT_FEAT_LOWSPEED));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001053 if (speed == HSMODE)
1054 rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
1055 else if (speed == LSMODE)
1056 rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
1057
1058 rh->port &= ~(1 << USB_PORT_FEAT_RESET);
1059 rh->port |= 1 << USB_PORT_FEAT_ENABLE;
1060}
1061
1062/* this function must be called with interrupt disabled */
1063static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
1064{
1065 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
1066
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001067 disable_r8a66597_pipe_all(r8a66597, dev);
1068 free_usb_address(r8a66597, dev);
1069
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001070 start_root_hub_sampling(r8a66597, port, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001071}
1072
1073/* this function must be called with interrupt disabled */
1074static void prepare_setup_packet(struct r8a66597 *r8a66597,
1075 struct r8a66597_td *td)
1076{
1077 int i;
Al Virofd05e722008-04-28 07:00:16 +01001078 __le16 *p = (__le16 *)td->urb->setup_packet;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001079 unsigned long setup_addr = USBREQ;
1080
1081 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1082 DCPMAXP);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001083 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001084
1085 for (i = 0; i < 4; i++) {
Al Virofd05e722008-04-28 07:00:16 +01001086 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001087 setup_addr += 2;
1088 }
1089 r8a66597_write(r8a66597, SUREQ, DCPCTR);
1090}
1091
1092/* this function must be called with interrupt disabled */
1093static void prepare_packet_read(struct r8a66597 *r8a66597,
1094 struct r8a66597_td *td)
1095{
1096 struct urb *urb = td->urb;
1097
1098 if (usb_pipecontrol(urb->pipe)) {
1099 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1100 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1101 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1102 if (urb->actual_length == 0) {
1103 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1104 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1105 }
1106 pipe_irq_disable(r8a66597, td->pipenum);
1107 pipe_start(r8a66597, td->pipe);
1108 pipe_irq_enable(r8a66597, urb, td->pipenum);
1109 } else {
1110 if (urb->actual_length == 0) {
1111 pipe_irq_disable(r8a66597, td->pipenum);
1112 pipe_setting(r8a66597, td);
1113 pipe_stop(r8a66597, td->pipe);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001114 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001115
1116 if (td->pipe->pipetre) {
1117 r8a66597_write(r8a66597, TRCLR,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001118 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001119 r8a66597_write(r8a66597,
Julia Lawalldfa5ec72008-03-04 15:25:11 -08001120 DIV_ROUND_UP
1121 (urb->transfer_buffer_length,
1122 td->maxpacket),
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001123 td->pipe->pipetrn);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001124 r8a66597_bset(r8a66597, TRENB,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001125 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001126 }
1127
1128 pipe_start(r8a66597, td->pipe);
1129 pipe_irq_enable(r8a66597, urb, td->pipenum);
1130 }
1131 }
1132}
1133
1134/* this function must be called with interrupt disabled */
1135static void prepare_packet_write(struct r8a66597 *r8a66597,
1136 struct r8a66597_td *td)
1137{
1138 u16 tmp;
1139 struct urb *urb = td->urb;
1140
1141 if (usb_pipecontrol(urb->pipe)) {
1142 pipe_stop(r8a66597, td->pipe);
1143 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1144 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1145 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1146 if (urb->actual_length == 0) {
1147 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1148 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1149 }
1150 } else {
1151 if (urb->actual_length == 0)
1152 pipe_setting(r8a66597, td);
1153 if (td->pipe->pipetre)
1154 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1155 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001156 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001157
1158 fifo_change_from_pipe(r8a66597, td->pipe);
1159 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1160 if (unlikely((tmp & FRDY) == 0))
1161 pipe_irq_enable(r8a66597, urb, td->pipenum);
1162 else
1163 packet_write(r8a66597, td->pipenum);
1164 pipe_start(r8a66597, td->pipe);
1165}
1166
1167/* this function must be called with interrupt disabled */
1168static void prepare_status_packet(struct r8a66597 *r8a66597,
1169 struct r8a66597_td *td)
1170{
1171 struct urb *urb = td->urb;
1172
1173 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001174 pipe_stop(r8a66597, td->pipe);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001175
1176 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1177 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1178 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1179 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001180 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +09001181 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001182 enable_irq_empty(r8a66597, 0);
1183 } else {
1184 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1185 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1186 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1187 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001188 enable_irq_ready(r8a66597, 0);
1189 }
1190 enable_irq_nrdy(r8a66597, 0);
1191 pipe_start(r8a66597, td->pipe);
1192}
1193
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001194static int is_set_address(unsigned char *setup_packet)
1195{
1196 if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1197 setup_packet[1] == USB_REQ_SET_ADDRESS)
1198 return 1;
1199 else
1200 return 0;
1201}
1202
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001203/* this function must be called with interrupt disabled */
1204static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1205{
1206 BUG_ON(!td);
1207
1208 switch (td->type) {
1209 case USB_PID_SETUP:
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001210 if (is_set_address(td->urb->setup_packet)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001211 td->set_address = 1;
1212 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1213 td->urb);
1214 if (td->urb->setup_packet[2] == 0)
1215 return -EPIPE;
1216 }
1217 prepare_setup_packet(r8a66597, td);
1218 break;
1219 case USB_PID_IN:
1220 prepare_packet_read(r8a66597, td);
1221 break;
1222 case USB_PID_OUT:
1223 prepare_packet_write(r8a66597, td);
1224 break;
1225 case USB_PID_ACK:
1226 prepare_status_packet(r8a66597, td);
1227 break;
1228 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001229 printk(KERN_ERR "r8a66597: invalid type.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001230 break;
1231 }
1232
1233 return 0;
1234}
1235
1236static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1237{
1238 if (usb_pipeisoc(urb->pipe)) {
1239 if (urb->number_of_packets == td->iso_cnt)
1240 return 1;
1241 }
1242
1243 /* control or bulk or interrupt */
1244 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1245 (td->short_packet) || (td->zero_packet))
1246 return 1;
1247
1248 return 0;
1249}
1250
1251/* this function must be called with interrupt disabled */
1252static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1253{
1254 unsigned long time;
1255
1256 BUG_ON(!td);
1257
1258 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1259 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1260 r8a66597->timeout_map |= 1 << td->pipenum;
1261 switch (usb_pipetype(td->urb->pipe)) {
1262 case PIPE_INTERRUPT:
1263 case PIPE_ISOCHRONOUS:
1264 time = 30;
1265 break;
1266 default:
1267 time = 300;
1268 break;
1269 }
1270
1271 mod_timer(&r8a66597->td_timer[td->pipenum],
1272 jiffies + msecs_to_jiffies(time));
1273 }
1274}
1275
1276/* this function must be called with interrupt disabled */
Alan Sterndfd1e532007-08-21 15:36:52 -04001277static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
Alan Stern888fda42007-08-24 15:41:18 -04001278 u16 pipenum, struct urb *urb, int status)
Alan Sterndfd1e532007-08-21 15:36:52 -04001279__releases(r8a66597->lock) __acquires(r8a66597->lock)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001280{
1281 int restart = 0;
1282 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1283
1284 r8a66597->timeout_map &= ~(1 << pipenum);
1285
1286 if (likely(td)) {
Alan Stern888fda42007-08-24 15:41:18 -04001287 if (td->set_address && (status != 0 || urb->unlinked))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001288 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1289
1290 pipe_toggle_save(r8a66597, td->pipe, urb);
1291 list_del(&td->queue);
1292 kfree(td);
1293 }
1294
1295 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1296 restart = 1;
1297
1298 if (likely(urb)) {
1299 if (usb_pipeisoc(urb->pipe))
1300 urb->start_frame = r8a66597_get_frame(hcd);
1301
Paul Mundt27175682010-02-04 06:57:58 +00001302 r8a66597_urb_done(r8a66597, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001303 }
1304
1305 if (restart) {
1306 td = r8a66597_get_td(r8a66597, pipenum);
1307 if (unlikely(!td))
1308 return;
1309
1310 start_transfer(r8a66597, td);
1311 set_td_timer(r8a66597, td);
1312 }
1313}
1314
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001315static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1316{
1317 u16 tmp;
1318 int rcv_len, bufsize, urb_len, size;
1319 u16 *buf;
1320 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1321 struct urb *urb;
1322 int finish = 0;
Alan Sterndfd1e532007-08-21 15:36:52 -04001323 int status = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001324
1325 if (unlikely(!td))
1326 return;
1327 urb = td->urb;
1328
1329 fifo_change_from_pipe(r8a66597, td->pipe);
1330 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1331 if (unlikely((tmp & FRDY) == 0)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001332 pipe_stop(r8a66597, td->pipe);
1333 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001334 printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001335 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001336 return;
1337 }
1338
1339 /* prepare parameters */
1340 rcv_len = tmp & DTLN;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001341 if (usb_pipeisoc(urb->pipe)) {
1342 buf = (u16 *)(urb->transfer_buffer +
1343 urb->iso_frame_desc[td->iso_cnt].offset);
1344 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1345 } else {
1346 buf = (void *)urb->transfer_buffer + urb->actual_length;
1347 urb_len = urb->transfer_buffer_length - urb->actual_length;
1348 }
Alan Sterndfd1e532007-08-21 15:36:52 -04001349 bufsize = min(urb_len, (int) td->maxpacket);
1350 if (rcv_len <= bufsize) {
1351 size = rcv_len;
1352 } else {
1353 size = bufsize;
1354 status = -EOVERFLOW;
1355 finish = 1;
1356 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001357
1358 /* update parameters */
1359 urb->actual_length += size;
1360 if (rcv_len == 0)
1361 td->zero_packet = 1;
Alan Sterndfd1e532007-08-21 15:36:52 -04001362 if (rcv_len < bufsize) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001363 td->short_packet = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001364 }
1365 if (usb_pipeisoc(urb->pipe)) {
1366 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
Alan Sterndfd1e532007-08-21 15:36:52 -04001367 urb->iso_frame_desc[td->iso_cnt].status = status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001368 td->iso_cnt++;
Alan Sterndfd1e532007-08-21 15:36:52 -04001369 finish = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001370 }
1371
1372 /* check transfer finish */
Alan Sternb0d9efb2007-08-21 15:39:21 -04001373 if (finish || check_transfer_finish(td, urb)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001374 pipe_stop(r8a66597, td->pipe);
1375 pipe_irq_disable(r8a66597, pipenum);
1376 finish = 1;
1377 }
1378
1379 /* read fifo */
1380 if (urb->transfer_buffer) {
1381 if (size == 0)
1382 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1383 else
1384 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1385 buf, size);
1386 }
1387
Alan Stern888fda42007-08-24 15:41:18 -04001388 if (finish && pipenum != 0)
1389 finish_request(r8a66597, td, pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001390}
1391
1392static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1393{
1394 u16 tmp;
1395 int bufsize, size;
1396 u16 *buf;
1397 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1398 struct urb *urb;
1399
1400 if (unlikely(!td))
1401 return;
1402 urb = td->urb;
1403
1404 fifo_change_from_pipe(r8a66597, td->pipe);
1405 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1406 if (unlikely((tmp & FRDY) == 0)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001407 pipe_stop(r8a66597, td->pipe);
1408 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001409 printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001410 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001411 return;
1412 }
1413
1414 /* prepare parameters */
1415 bufsize = td->maxpacket;
1416 if (usb_pipeisoc(urb->pipe)) {
1417 buf = (u16 *)(urb->transfer_buffer +
1418 urb->iso_frame_desc[td->iso_cnt].offset);
1419 size = min(bufsize,
1420 (int)urb->iso_frame_desc[td->iso_cnt].length);
1421 } else {
1422 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
Greg Kroah-Hartman16e2e5f2009-03-03 16:44:13 -08001423 size = min_t(u32, bufsize,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001424 urb->transfer_buffer_length - urb->actual_length);
1425 }
1426
1427 /* write fifo */
1428 if (pipenum > 0)
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001429 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001430 if (urb->transfer_buffer) {
1431 r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
1432 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1433 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1434 }
1435
1436 /* update parameters */
1437 urb->actual_length += size;
1438 if (usb_pipeisoc(urb->pipe)) {
1439 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1440 urb->iso_frame_desc[td->iso_cnt].status = 0;
1441 td->iso_cnt++;
1442 }
1443
1444 /* check transfer finish */
1445 if (check_transfer_finish(td, urb)) {
1446 disable_irq_ready(r8a66597, pipenum);
1447 enable_irq_empty(r8a66597, pipenum);
1448 if (!usb_pipeisoc(urb->pipe))
1449 enable_irq_nrdy(r8a66597, pipenum);
1450 } else
1451 pipe_irq_enable(r8a66597, urb, pipenum);
1452}
1453
1454
Alan Stern888fda42007-08-24 15:41:18 -04001455static void check_next_phase(struct r8a66597 *r8a66597, int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001456{
1457 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1458 struct urb *urb;
1459 u8 finish = 0;
1460
1461 if (unlikely(!td))
1462 return;
1463 urb = td->urb;
1464
1465 switch (td->type) {
1466 case USB_PID_IN:
1467 case USB_PID_OUT:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001468 if (check_transfer_finish(td, urb))
1469 td->type = USB_PID_ACK;
1470 break;
1471 case USB_PID_SETUP:
Alan Sterneb231052007-08-21 15:40:36 -04001472 if (urb->transfer_buffer_length == urb->actual_length)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001473 td->type = USB_PID_ACK;
Alan Sterneb231052007-08-21 15:40:36 -04001474 else if (usb_pipeout(urb->pipe))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001475 td->type = USB_PID_OUT;
1476 else
1477 td->type = USB_PID_IN;
1478 break;
1479 case USB_PID_ACK:
1480 finish = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001481 break;
1482 }
1483
Alan Stern888fda42007-08-24 15:41:18 -04001484 if (finish || status != 0 || urb->unlinked)
1485 finish_request(r8a66597, td, 0, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001486 else
1487 start_transfer(r8a66597, td);
1488}
1489
Alan Stern888fda42007-08-24 15:41:18 -04001490static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001491{
1492 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1493
Alan Stern888fda42007-08-24 15:41:18 -04001494 if (td) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001495 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1496
1497 if (pid == PID_NAK)
Alan Stern888fda42007-08-24 15:41:18 -04001498 return -ECONNRESET;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001499 else
Alan Stern888fda42007-08-24 15:41:18 -04001500 return -EPIPE;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001501 }
Alan Stern888fda42007-08-24 15:41:18 -04001502 return 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001503}
1504
1505static void irq_pipe_ready(struct r8a66597 *r8a66597)
1506{
1507 u16 check;
1508 u16 pipenum;
1509 u16 mask;
1510 struct r8a66597_td *td;
1511
1512 mask = r8a66597_read(r8a66597, BRDYSTS)
1513 & r8a66597_read(r8a66597, BRDYENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001514 r8a66597_write(r8a66597, ~mask, BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001515 if (mask & BRDY0) {
1516 td = r8a66597_get_td(r8a66597, 0);
1517 if (td && td->type == USB_PID_IN)
1518 packet_read(r8a66597, 0);
1519 else
1520 pipe_irq_disable(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001521 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001522 }
1523
1524 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1525 check = 1 << pipenum;
1526 if (mask & check) {
1527 td = r8a66597_get_td(r8a66597, pipenum);
1528 if (unlikely(!td))
1529 continue;
1530
1531 if (td->type == USB_PID_IN)
1532 packet_read(r8a66597, pipenum);
1533 else if (td->type == USB_PID_OUT)
1534 packet_write(r8a66597, pipenum);
1535 }
1536 }
1537}
1538
1539static void irq_pipe_empty(struct r8a66597 *r8a66597)
1540{
1541 u16 tmp;
1542 u16 check;
1543 u16 pipenum;
1544 u16 mask;
1545 struct r8a66597_td *td;
1546
1547 mask = r8a66597_read(r8a66597, BEMPSTS)
1548 & r8a66597_read(r8a66597, BEMPENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001549 r8a66597_write(r8a66597, ~mask, BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001550 if (mask & BEMP0) {
1551 cfifo_change(r8a66597, 0);
1552 td = r8a66597_get_td(r8a66597, 0);
1553 if (td && td->type != USB_PID_OUT)
1554 disable_irq_empty(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001555 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001556 }
1557
1558 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1559 check = 1 << pipenum;
1560 if (mask & check) {
1561 struct r8a66597_td *td;
1562 td = r8a66597_get_td(r8a66597, pipenum);
1563 if (unlikely(!td))
1564 continue;
1565
1566 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1567 if ((tmp & INBUFM) == 0) {
1568 disable_irq_empty(r8a66597, pipenum);
1569 pipe_irq_disable(r8a66597, pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001570 finish_request(r8a66597, td, pipenum, td->urb,
1571 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001572 }
1573 }
1574 }
1575}
1576
1577static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1578{
1579 u16 check;
1580 u16 pipenum;
1581 u16 mask;
Alan Stern888fda42007-08-24 15:41:18 -04001582 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001583
1584 mask = r8a66597_read(r8a66597, NRDYSTS)
1585 & r8a66597_read(r8a66597, NRDYENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001586 r8a66597_write(r8a66597, ~mask, NRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001587 if (mask & NRDY0) {
1588 cfifo_change(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001589 status = get_urb_error(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001590 pipe_irq_disable(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001591 check_next_phase(r8a66597, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001592 }
1593
1594 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1595 check = 1 << pipenum;
1596 if (mask & check) {
1597 struct r8a66597_td *td;
1598 td = r8a66597_get_td(r8a66597, pipenum);
1599 if (unlikely(!td))
1600 continue;
1601
Alan Stern888fda42007-08-24 15:41:18 -04001602 status = get_urb_error(r8a66597, pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001603 pipe_irq_disable(r8a66597, pipenum);
1604 pipe_stop(r8a66597, td->pipe);
Alan Stern888fda42007-08-24 15:41:18 -04001605 finish_request(r8a66597, td, pipenum, td->urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001606 }
1607 }
1608}
1609
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001610static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1611{
1612 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1613 u16 intsts0, intsts1, intsts2;
1614 u16 intenb0, intenb1, intenb2;
1615 u16 mask0, mask1, mask2;
Alan Stern888fda42007-08-24 15:41:18 -04001616 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001617
1618 spin_lock(&r8a66597->lock);
1619
1620 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1621 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1622 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1623 intenb0 = r8a66597_read(r8a66597, INTENB0);
1624 intenb1 = r8a66597_read(r8a66597, INTENB1);
1625 intenb2 = r8a66597_read(r8a66597, INTENB2);
1626
1627 mask2 = intsts2 & intenb2;
1628 mask1 = intsts1 & intenb1;
1629 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1630 if (mask2) {
1631 if (mask2 & ATTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001632 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001633 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1634
1635 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001636 start_root_hub_sampling(r8a66597, 1, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001637 }
1638 if (mask2 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001639 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001640 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1641 r8a66597_usb_disconnect(r8a66597, 1);
1642 }
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09001643 if (mask2 & BCHG) {
1644 r8a66597_write(r8a66597, ~BCHG, INTSTS2);
1645 r8a66597_bclr(r8a66597, BCHGE, INTENB2);
1646 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1647 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001648 }
1649
1650 if (mask1) {
1651 if (mask1 & ATTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001652 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001653 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1654
1655 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001656 start_root_hub_sampling(r8a66597, 0, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001657 }
1658 if (mask1 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001659 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001660 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1661 r8a66597_usb_disconnect(r8a66597, 0);
1662 }
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09001663 if (mask1 & BCHG) {
1664 r8a66597_write(r8a66597, ~BCHG, INTSTS1);
1665 r8a66597_bclr(r8a66597, BCHGE, INTENB1);
1666 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1667 }
1668
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001669 if (mask1 & SIGN) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001670 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001671 status = get_urb_error(r8a66597, 0);
1672 check_next_phase(r8a66597, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001673 }
1674 if (mask1 & SACK) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001675 r8a66597_write(r8a66597, ~SACK, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001676 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001677 }
1678 }
1679 if (mask0) {
1680 if (mask0 & BRDY)
1681 irq_pipe_ready(r8a66597);
1682 if (mask0 & BEMP)
1683 irq_pipe_empty(r8a66597);
1684 if (mask0 & NRDY)
1685 irq_pipe_nrdy(r8a66597);
1686 }
1687
1688 spin_unlock(&r8a66597->lock);
1689 return IRQ_HANDLED;
1690}
1691
1692/* this function must be called with interrupt disabled */
1693static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1694{
1695 u16 tmp;
1696 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1697
1698 if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
1699 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1700
1701 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1702 if ((tmp & USBRST) == USBRST) {
1703 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1704 dvstctr_reg);
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001705 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001706 } else
1707 r8a66597_usb_connect(r8a66597, port);
1708 }
1709
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001710 if (!(rh->port & (1 << USB_PORT_FEAT_CONNECTION))) {
1711 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1712 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1713 }
1714
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001715 if (rh->scount > 0) {
1716 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1717 if (tmp == rh->old_syssts) {
1718 rh->scount--;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001719 if (rh->scount == 0)
1720 r8a66597_check_syssts(r8a66597, port, tmp);
1721 else
1722 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001723 } else {
1724 rh->scount = R8A66597_MAX_SAMPLING;
1725 rh->old_syssts = tmp;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001726 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001727 }
1728 }
1729}
1730
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001731static void r8a66597_interval_timer(unsigned long _r8a66597)
1732{
1733 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1734 unsigned long flags;
1735 u16 pipenum;
1736 struct r8a66597_td *td;
1737
1738 spin_lock_irqsave(&r8a66597->lock, flags);
1739
1740 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1741 if (!(r8a66597->interval_map & (1 << pipenum)))
1742 continue;
1743 if (timer_pending(&r8a66597->interval_timer[pipenum]))
1744 continue;
1745
1746 td = r8a66597_get_td(r8a66597, pipenum);
1747 if (td)
1748 start_transfer(r8a66597, td);
1749 }
1750
1751 spin_unlock_irqrestore(&r8a66597->lock, flags);
1752}
1753
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001754static void r8a66597_td_timer(unsigned long _r8a66597)
1755{
1756 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1757 unsigned long flags;
1758 u16 pipenum;
1759 struct r8a66597_td *td, *new_td = NULL;
1760 struct r8a66597_pipe *pipe;
1761
1762 spin_lock_irqsave(&r8a66597->lock, flags);
1763 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1764 if (!(r8a66597->timeout_map & (1 << pipenum)))
1765 continue;
1766 if (timer_pending(&r8a66597->td_timer[pipenum]))
1767 continue;
1768
1769 td = r8a66597_get_td(r8a66597, pipenum);
1770 if (!td) {
1771 r8a66597->timeout_map &= ~(1 << pipenum);
1772 continue;
1773 }
1774
1775 if (td->urb->actual_length) {
1776 set_td_timer(r8a66597, td);
1777 break;
1778 }
1779
1780 pipe = td->pipe;
1781 pipe_stop(r8a66597, pipe);
1782
1783 new_td = td;
1784 do {
1785 list_move_tail(&new_td->queue,
1786 &r8a66597->pipe_queue[pipenum]);
1787 new_td = r8a66597_get_td(r8a66597, pipenum);
1788 if (!new_td) {
1789 new_td = td;
1790 break;
1791 }
1792 } while (td != new_td && td->address == new_td->address);
1793
1794 start_transfer(r8a66597, new_td);
1795
1796 if (td == new_td)
1797 r8a66597->timeout_map &= ~(1 << pipenum);
1798 else
1799 set_td_timer(r8a66597, new_td);
1800 break;
1801 }
1802 spin_unlock_irqrestore(&r8a66597->lock, flags);
1803}
1804
1805static void r8a66597_timer(unsigned long _r8a66597)
1806{
1807 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1808 unsigned long flags;
Yoshihiro Shimoda58639642008-11-11 16:47:21 +09001809 int port;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001810
1811 spin_lock_irqsave(&r8a66597->lock, flags);
1812
Magnus Damm719a72b2009-07-17 14:59:55 +00001813 for (port = 0; port < r8a66597->max_root_hub; port++)
Yoshihiro Shimoda58639642008-11-11 16:47:21 +09001814 r8a66597_root_hub_control(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001815
1816 spin_unlock_irqrestore(&r8a66597->lock, flags);
1817}
1818
1819static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1820{
1821 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1822
1823 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1824 (urb->dev->state == USB_STATE_CONFIGURED))
1825 return 1;
1826 else
1827 return 0;
1828}
1829
1830static int r8a66597_start(struct usb_hcd *hcd)
1831{
1832 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001833
1834 hcd->state = HC_STATE_RUNNING;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001835 return enable_controller(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001836}
1837
1838static void r8a66597_stop(struct usb_hcd *hcd)
1839{
1840 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1841
1842 disable_controller(r8a66597);
1843}
1844
1845static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1846{
1847 unsigned int usb_address = usb_pipedevice(urb->pipe);
1848 u16 root_port, hub_port;
1849
1850 if (usb_address == 0) {
Magnus Damm719a72b2009-07-17 14:59:55 +00001851 get_port_number(r8a66597, urb->dev->devpath,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001852 &root_port, &hub_port);
1853 set_devadd_reg(r8a66597, 0,
1854 get_r8a66597_usb_speed(urb->dev->speed),
1855 get_parent_r8a66597_address(r8a66597, urb->dev),
1856 hub_port, root_port);
1857 }
1858}
1859
1860static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1861 struct urb *urb,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001862 struct usb_host_endpoint *hep)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001863{
1864 struct r8a66597_td *td;
1865 u16 pipenum;
1866
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001867 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001868 if (td == NULL)
1869 return NULL;
1870
1871 pipenum = r8a66597_get_pipenum(urb, hep);
1872 td->pipenum = pipenum;
1873 td->pipe = hep->hcpriv;
1874 td->urb = urb;
1875 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1876 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1877 !usb_pipein(urb->pipe));
1878 if (usb_pipecontrol(urb->pipe))
1879 td->type = USB_PID_SETUP;
1880 else if (usb_pipein(urb->pipe))
1881 td->type = USB_PID_IN;
1882 else
1883 td->type = USB_PID_OUT;
1884 INIT_LIST_HEAD(&td->queue);
1885
1886 return td;
1887}
1888
1889static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001890 struct urb *urb,
1891 gfp_t mem_flags)
1892{
Alan Sterne9df41c2007-08-08 11:48:02 -04001893 struct usb_host_endpoint *hep = urb->ep;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001894 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1895 struct r8a66597_td *td = NULL;
Alan Sterne9df41c2007-08-08 11:48:02 -04001896 int ret, request = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001897 unsigned long flags;
1898
1899 spin_lock_irqsave(&r8a66597->lock, flags);
1900 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1901 ret = -ENODEV;
Alan Sterne9df41c2007-08-08 11:48:02 -04001902 goto error_not_linked;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001903 }
1904
Alan Sterne9df41c2007-08-08 11:48:02 -04001905 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1906 if (ret)
1907 goto error_not_linked;
1908
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001909 if (!hep->hcpriv) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001910 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1911 GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001912 if (!hep->hcpriv) {
1913 ret = -ENOMEM;
1914 goto error;
1915 }
1916 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1917 if (usb_pipeendpoint(urb->pipe))
1918 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1919 }
1920
1921 if (unlikely(check_pipe_config(r8a66597, urb)))
1922 init_pipe_config(r8a66597, urb);
1923
1924 set_address_zero(r8a66597, urb);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001925 td = r8a66597_make_td(r8a66597, urb, hep);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001926 if (td == NULL) {
1927 ret = -ENOMEM;
1928 goto error;
1929 }
1930 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1931 request = 1;
1932 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001933 urb->hcpriv = td;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001934
1935 if (request) {
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001936 if (td->pipe->info.timer_interval) {
1937 r8a66597->interval_map |= 1 << td->pipenum;
1938 mod_timer(&r8a66597->interval_timer[td->pipenum],
1939 jiffies + msecs_to_jiffies(
1940 td->pipe->info.timer_interval));
1941 } else {
1942 ret = start_transfer(r8a66597, td);
1943 if (ret < 0) {
1944 list_del(&td->queue);
1945 kfree(td);
1946 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001947 }
1948 } else
1949 set_td_timer(r8a66597, td);
1950
1951error:
Alan Sterne9df41c2007-08-08 11:48:02 -04001952 if (ret)
1953 usb_hcd_unlink_urb_from_ep(hcd, urb);
1954error_not_linked:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001955 spin_unlock_irqrestore(&r8a66597->lock, flags);
1956 return ret;
1957}
1958
Alan Sterne9df41c2007-08-08 11:48:02 -04001959static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1960 int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001961{
1962 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1963 struct r8a66597_td *td;
1964 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001965 int rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001966
1967 spin_lock_irqsave(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001968 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1969 if (rc)
1970 goto done;
1971
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001972 if (urb->hcpriv) {
1973 td = urb->hcpriv;
1974 pipe_stop(r8a66597, td->pipe);
1975 pipe_irq_disable(r8a66597, td->pipenum);
1976 disable_irq_empty(r8a66597, td->pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001977 finish_request(r8a66597, td, td->pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001978 }
Alan Sterne9df41c2007-08-08 11:48:02 -04001979 done:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001980 spin_unlock_irqrestore(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001981 return rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001982}
1983
1984static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1985 struct usb_host_endpoint *hep)
1986{
1987 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1988 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1989 struct r8a66597_td *td;
1990 struct urb *urb = NULL;
1991 u16 pipenum;
1992 unsigned long flags;
1993
1994 if (pipe == NULL)
1995 return;
1996 pipenum = pipe->info.pipenum;
1997
1998 if (pipenum == 0) {
1999 kfree(hep->hcpriv);
2000 hep->hcpriv = NULL;
2001 return;
2002 }
2003
2004 spin_lock_irqsave(&r8a66597->lock, flags);
2005 pipe_stop(r8a66597, pipe);
2006 pipe_irq_disable(r8a66597, pipenum);
2007 disable_irq_empty(r8a66597, pipenum);
2008 td = r8a66597_get_td(r8a66597, pipenum);
2009 if (td)
2010 urb = td->urb;
Alan Stern888fda42007-08-24 15:41:18 -04002011 finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002012 kfree(hep->hcpriv);
2013 hep->hcpriv = NULL;
2014 spin_unlock_irqrestore(&r8a66597->lock, flags);
2015}
2016
2017static int r8a66597_get_frame(struct usb_hcd *hcd)
2018{
2019 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2020 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
2021}
2022
2023static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
2024{
2025 int chix;
2026
2027 if (udev->state == USB_STATE_CONFIGURED &&
2028 udev->parent && udev->parent->devnum > 1 &&
2029 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
2030 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
2031
2032 for (chix = 0; chix < udev->maxchild; chix++) {
2033 struct usb_device *childdev = udev->children[chix];
2034
2035 if (childdev)
2036 collect_usb_address_map(childdev, map);
2037 }
2038}
2039
2040/* this function must be called with interrupt disabled */
2041static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
2042 int addr)
2043{
2044 struct r8a66597_device *dev;
2045 struct list_head *list = &r8a66597->child_device;
2046
2047 list_for_each_entry(dev, list, device_list) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002048 if (dev->usb_address != addr)
2049 continue;
2050
2051 return dev;
2052 }
2053
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002054 printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002055 return NULL;
2056}
2057
2058static void update_usb_address_map(struct r8a66597 *r8a66597,
2059 struct usb_device *root_hub,
2060 unsigned long *map)
2061{
2062 int i, j, addr;
2063 unsigned long diff;
2064 unsigned long flags;
2065
2066 for (i = 0; i < 4; i++) {
2067 diff = r8a66597->child_connect_map[i] ^ map[i];
2068 if (!diff)
2069 continue;
2070
2071 for (j = 0; j < 32; j++) {
2072 if (!(diff & (1 << j)))
2073 continue;
2074
2075 addr = i * 32 + j;
2076 if (map[i] & (1 << j))
2077 set_child_connect_map(r8a66597, addr);
2078 else {
2079 struct r8a66597_device *dev;
2080
2081 spin_lock_irqsave(&r8a66597->lock, flags);
2082 dev = get_r8a66597_device(r8a66597, addr);
2083 disable_r8a66597_pipe_all(r8a66597, dev);
2084 free_usb_address(r8a66597, dev);
2085 put_child_connect_map(r8a66597, addr);
2086 spin_unlock_irqrestore(&r8a66597->lock, flags);
2087 }
2088 }
2089 }
2090}
2091
2092static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
2093 struct usb_hcd *hcd)
2094{
2095 struct usb_bus *bus;
2096 unsigned long now_map[4];
2097
2098 memset(now_map, 0, sizeof(now_map));
2099
2100 list_for_each_entry(bus, &usb_bus_list, bus_list) {
2101 if (!bus->root_hub)
2102 continue;
2103
2104 if (bus->busnum != hcd->self.busnum)
2105 continue;
2106
2107 collect_usb_address_map(bus->root_hub, now_map);
2108 update_usb_address_map(r8a66597, bus->root_hub, now_map);
2109 }
2110}
2111
2112static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2113{
2114 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2115 unsigned long flags;
2116 int i;
2117
2118 r8a66597_check_detect_child(r8a66597, hcd);
2119
2120 spin_lock_irqsave(&r8a66597->lock, flags);
2121
2122 *buf = 0; /* initialize (no change) */
2123
Magnus Damm719a72b2009-07-17 14:59:55 +00002124 for (i = 0; i < r8a66597->max_root_hub; i++) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002125 if (r8a66597->root_hub[i].port & 0xffff0000)
2126 *buf |= 1 << (i + 1);
2127 }
2128
2129 spin_unlock_irqrestore(&r8a66597->lock, flags);
2130
2131 return (*buf != 0);
2132}
2133
2134static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2135 struct usb_hub_descriptor *desc)
2136{
2137 desc->bDescriptorType = 0x29;
2138 desc->bHubContrCurrent = 0;
Magnus Damm719a72b2009-07-17 14:59:55 +00002139 desc->bNbrPorts = r8a66597->max_root_hub;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002140 desc->bDescLength = 9;
2141 desc->bPwrOn2PwrGood = 0;
2142 desc->wHubCharacteristics = cpu_to_le16(0x0011);
Magnus Damm719a72b2009-07-17 14:59:55 +00002143 desc->bitmap[0] = ((1 << r8a66597->max_root_hub) - 1) << 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002144 desc->bitmap[1] = ~0;
2145}
2146
2147static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2148 u16 wIndex, char *buf, u16 wLength)
2149{
2150 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2151 int ret;
2152 int port = (wIndex & 0x00FF) - 1;
2153 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2154 unsigned long flags;
2155
2156 ret = 0;
2157
2158 spin_lock_irqsave(&r8a66597->lock, flags);
2159 switch (typeReq) {
2160 case ClearHubFeature:
2161 case SetHubFeature:
2162 switch (wValue) {
2163 case C_HUB_OVER_CURRENT:
2164 case C_HUB_LOCAL_POWER:
2165 break;
2166 default:
2167 goto error;
2168 }
2169 break;
2170 case ClearPortFeature:
Magnus Damm719a72b2009-07-17 14:59:55 +00002171 if (wIndex > r8a66597->max_root_hub)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002172 goto error;
2173 if (wLength != 0)
2174 goto error;
2175
2176 switch (wValue) {
2177 case USB_PORT_FEAT_ENABLE:
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002178 rh->port &= ~(1 << USB_PORT_FEAT_POWER);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002179 break;
2180 case USB_PORT_FEAT_SUSPEND:
2181 break;
2182 case USB_PORT_FEAT_POWER:
2183 r8a66597_port_power(r8a66597, port, 0);
2184 break;
2185 case USB_PORT_FEAT_C_ENABLE:
2186 case USB_PORT_FEAT_C_SUSPEND:
2187 case USB_PORT_FEAT_C_CONNECTION:
2188 case USB_PORT_FEAT_C_OVER_CURRENT:
2189 case USB_PORT_FEAT_C_RESET:
2190 break;
2191 default:
2192 goto error;
2193 }
2194 rh->port &= ~(1 << wValue);
2195 break;
2196 case GetHubDescriptor:
2197 r8a66597_hub_descriptor(r8a66597,
2198 (struct usb_hub_descriptor *)buf);
2199 break;
2200 case GetHubStatus:
2201 *buf = 0x00;
2202 break;
2203 case GetPortStatus:
Magnus Damm719a72b2009-07-17 14:59:55 +00002204 if (wIndex > r8a66597->max_root_hub)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002205 goto error;
Al Virofd05e722008-04-28 07:00:16 +01002206 *(__le32 *)buf = cpu_to_le32(rh->port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002207 break;
2208 case SetPortFeature:
Magnus Damm719a72b2009-07-17 14:59:55 +00002209 if (wIndex > r8a66597->max_root_hub)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002210 goto error;
2211 if (wLength != 0)
2212 goto error;
2213
2214 switch (wValue) {
2215 case USB_PORT_FEAT_SUSPEND:
2216 break;
2217 case USB_PORT_FEAT_POWER:
2218 r8a66597_port_power(r8a66597, port, 1);
2219 rh->port |= (1 << USB_PORT_FEAT_POWER);
2220 break;
2221 case USB_PORT_FEAT_RESET: {
2222 struct r8a66597_device *dev = rh->dev;
2223
2224 rh->port |= (1 << USB_PORT_FEAT_RESET);
2225
2226 disable_r8a66597_pipe_all(r8a66597, dev);
2227 free_usb_address(r8a66597, dev);
2228
2229 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2230 get_dvstctr_reg(port));
2231 mod_timer(&r8a66597->rh_timer,
2232 jiffies + msecs_to_jiffies(50));
2233 }
2234 break;
2235 default:
2236 goto error;
2237 }
2238 rh->port |= 1 << wValue;
2239 break;
2240 default:
2241error:
2242 ret = -EPIPE;
2243 break;
2244 }
2245
2246 spin_unlock_irqrestore(&r8a66597->lock, flags);
2247 return ret;
2248}
2249
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002250#if defined(CONFIG_PM)
2251static int r8a66597_bus_suspend(struct usb_hcd *hcd)
2252{
2253 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2254 int port;
2255
2256 dbg("%s", __func__);
2257
Magnus Damm719a72b2009-07-17 14:59:55 +00002258 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002259 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2260 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2261
2262 if (!(rh->port & (1 << USB_PORT_FEAT_ENABLE)))
2263 continue;
2264
2265 dbg("suspend port = %d", port);
2266 r8a66597_bclr(r8a66597, UACT, dvstctr_reg); /* suspend */
2267 rh->port |= 1 << USB_PORT_FEAT_SUSPEND;
2268
2269 if (rh->dev->udev->do_remote_wakeup) {
2270 msleep(3); /* waiting last SOF */
2271 r8a66597_bset(r8a66597, RWUPE, dvstctr_reg);
2272 r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
2273 r8a66597_bset(r8a66597, BCHGE, get_intenb_reg(port));
2274 }
2275 }
2276
2277 r8a66597->bus_suspended = 1;
2278
2279 return 0;
2280}
2281
2282static int r8a66597_bus_resume(struct usb_hcd *hcd)
2283{
2284 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2285 int port;
2286
2287 dbg("%s", __func__);
2288
Magnus Damm719a72b2009-07-17 14:59:55 +00002289 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002290 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2291 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2292
2293 if (!(rh->port & (1 << USB_PORT_FEAT_SUSPEND)))
2294 continue;
2295
2296 dbg("resume port = %d", port);
2297 rh->port &= ~(1 << USB_PORT_FEAT_SUSPEND);
2298 rh->port |= 1 << USB_PORT_FEAT_C_SUSPEND;
2299 r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
2300 msleep(50);
2301 r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
2302 }
2303
2304 return 0;
2305
2306}
2307#else
2308#define r8a66597_bus_suspend NULL
2309#define r8a66597_bus_resume NULL
2310#endif
2311
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002312static struct hc_driver r8a66597_hc_driver = {
2313 .description = hcd_name,
2314 .hcd_priv_size = sizeof(struct r8a66597),
2315 .irq = r8a66597_irq,
2316
2317 /*
2318 * generic hardware linkage
2319 */
2320 .flags = HCD_USB2,
2321
2322 .start = r8a66597_start,
2323 .stop = r8a66597_stop,
2324
2325 /*
2326 * managing i/o requests and associated device resources
2327 */
2328 .urb_enqueue = r8a66597_urb_enqueue,
2329 .urb_dequeue = r8a66597_urb_dequeue,
2330 .endpoint_disable = r8a66597_endpoint_disable,
2331
2332 /*
2333 * periodic schedule support
2334 */
2335 .get_frame_number = r8a66597_get_frame,
2336
2337 /*
2338 * root hub support
2339 */
2340 .hub_status_data = r8a66597_hub_status_data,
2341 .hub_control = r8a66597_hub_control,
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002342 .bus_suspend = r8a66597_bus_suspend,
2343 .bus_resume = r8a66597_bus_resume,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002344};
2345
2346#if defined(CONFIG_PM)
Magnus Dammae1cef62009-07-17 14:52:05 +00002347static int r8a66597_suspend(struct device *dev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002348{
Magnus Dammae1cef62009-07-17 14:52:05 +00002349 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002350 int port;
2351
2352 dbg("%s", __func__);
2353
2354 disable_controller(r8a66597);
2355
Magnus Damm719a72b2009-07-17 14:59:55 +00002356 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002357 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2358
2359 rh->port = 0x00000000;
2360 }
2361
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002362 return 0;
2363}
2364
Magnus Dammae1cef62009-07-17 14:52:05 +00002365static int r8a66597_resume(struct device *dev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002366{
Magnus Dammae1cef62009-07-17 14:52:05 +00002367 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002368 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2369
2370 dbg("%s", __func__);
2371
2372 enable_controller(r8a66597);
2373 usb_root_hub_lost_power(hcd->self.root_hub);
2374
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002375 return 0;
2376}
Magnus Dammae1cef62009-07-17 14:52:05 +00002377
Alexey Dobriyan47145212009-12-14 18:00:08 -08002378static const struct dev_pm_ops r8a66597_dev_pm_ops = {
Magnus Dammae1cef62009-07-17 14:52:05 +00002379 .suspend = r8a66597_suspend,
2380 .resume = r8a66597_resume,
Yoshihiro Shimoda3725f282009-07-29 09:24:41 +00002381 .poweroff = r8a66597_suspend,
2382 .restore = r8a66597_resume,
Magnus Dammae1cef62009-07-17 14:52:05 +00002383};
2384
2385#define R8A66597_DEV_PM_OPS (&r8a66597_dev_pm_ops)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002386#else /* if defined(CONFIG_PM) */
Magnus Dammae1cef62009-07-17 14:52:05 +00002387#define R8A66597_DEV_PM_OPS NULL
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002388#endif
2389
2390static int __init_or_module r8a66597_remove(struct platform_device *pdev)
2391{
2392 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2393 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2394
2395 del_timer_sync(&r8a66597->rh_timer);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002396 usb_remove_hcd(hcd);
Yoshihiro Shimodaca0677a2007-10-05 15:53:12 +09002397 iounmap((void *)r8a66597->reg);
Magnus Damm719a72b2009-07-17 14:59:55 +00002398#ifdef CONFIG_HAVE_CLK
2399 if (r8a66597->pdata->on_chip)
2400 clk_put(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +09002401#endif
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002402 usb_put_hcd(hcd);
2403 return 0;
2404}
2405
Uwe Kleine-König8864bd82009-03-28 00:27:00 +01002406static int __devinit r8a66597_probe(struct platform_device *pdev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002407{
Magnus Damm719a72b2009-07-17 14:59:55 +00002408#ifdef CONFIG_HAVE_CLK
Magnus Damm765786e2008-10-31 20:22:38 +09002409 char clk_name[8];
2410#endif
Marc Zyngier27140212008-08-18 13:08:42 +02002411 struct resource *res = NULL, *ires;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002412 int irq = -1;
2413 void __iomem *reg = NULL;
2414 struct usb_hcd *hcd = NULL;
2415 struct r8a66597 *r8a66597;
2416 int ret = 0;
2417 int i;
Yoshihiro Shimoda0bf32b82008-06-27 19:09:55 +09002418 unsigned long irq_trigger;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002419
2420 if (pdev->dev.dma_mask) {
2421 ret = -EINVAL;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002422 dev_err(&pdev->dev, "dma not supported\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002423 goto clean_up;
2424 }
2425
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002426 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002427 if (!res) {
2428 ret = -ENODEV;
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002429 dev_err(&pdev->dev, "platform_get_resource error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002430 goto clean_up;
2431 }
2432
Marc Zyngier27140212008-08-18 13:08:42 +02002433 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2434 if (!ires) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002435 ret = -ENODEV;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002436 dev_err(&pdev->dev,
2437 "platform_get_resource IORESOURCE_IRQ error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002438 goto clean_up;
2439 }
2440
Marc Zyngier27140212008-08-18 13:08:42 +02002441 irq = ires->start;
2442 irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
2443
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002444 reg = ioremap(res->start, resource_size(res));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002445 if (reg == NULL) {
2446 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002447 dev_err(&pdev->dev, "ioremap error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002448 goto clean_up;
2449 }
2450
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +09002451 if (pdev->dev.platform_data == NULL) {
2452 dev_err(&pdev->dev, "no platform data\n");
2453 ret = -ENODEV;
2454 goto clean_up;
2455 }
2456
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002457 /* initialize hcd */
2458 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2459 if (!hcd) {
2460 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002461 dev_err(&pdev->dev, "Failed to create hcd\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002462 goto clean_up;
2463 }
2464 r8a66597 = hcd_to_r8a66597(hcd);
2465 memset(r8a66597, 0, sizeof(struct r8a66597));
2466 dev_set_drvdata(&pdev->dev, r8a66597);
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +09002467 r8a66597->pdata = pdev->dev.platform_data;
2468 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002469
Magnus Damm719a72b2009-07-17 14:59:55 +00002470 if (r8a66597->pdata->on_chip) {
2471#ifdef CONFIG_HAVE_CLK
2472 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
2473 r8a66597->clk = clk_get(&pdev->dev, clk_name);
2474 if (IS_ERR(r8a66597->clk)) {
2475 dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
2476 clk_name);
2477 ret = PTR_ERR(r8a66597->clk);
2478 goto clean_up2;
2479 }
Magnus Damm765786e2008-10-31 20:22:38 +09002480#endif
Magnus Damm719a72b2009-07-17 14:59:55 +00002481 r8a66597->max_root_hub = 1;
2482 } else
2483 r8a66597->max_root_hub = 2;
Magnus Damm765786e2008-10-31 20:22:38 +09002484
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002485 spin_lock_init(&r8a66597->lock);
2486 init_timer(&r8a66597->rh_timer);
2487 r8a66597->rh_timer.function = r8a66597_timer;
2488 r8a66597->rh_timer.data = (unsigned long)r8a66597;
2489 r8a66597->reg = (unsigned long)reg;
2490
Magnus Damme5ff15b2010-01-27 07:41:19 +00002491 /* make sure no interrupts are pending */
2492 ret = r8a66597_clock_enable(r8a66597);
2493 if (ret < 0)
2494 goto clean_up3;
2495 disable_controller(r8a66597);
2496
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002497 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2498 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2499 init_timer(&r8a66597->td_timer[i]);
2500 r8a66597->td_timer[i].function = r8a66597_td_timer;
2501 r8a66597->td_timer[i].data = (unsigned long)r8a66597;
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09002502 setup_timer(&r8a66597->interval_timer[i],
2503 r8a66597_interval_timer,
2504 (unsigned long)r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002505 }
2506 INIT_LIST_HEAD(&r8a66597->child_device);
2507
2508 hcd->rsrc_start = res->start;
Marc Zyngier27140212008-08-18 13:08:42 +02002509
Yoshihiro Shimoda0bf32b82008-06-27 19:09:55 +09002510 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002511 if (ret != 0) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002512 dev_err(&pdev->dev, "Failed to add hcd\n");
Magnus Damm765786e2008-10-31 20:22:38 +09002513 goto clean_up3;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002514 }
2515
2516 return 0;
2517
Magnus Damm765786e2008-10-31 20:22:38 +09002518clean_up3:
Magnus Damm719a72b2009-07-17 14:59:55 +00002519#ifdef CONFIG_HAVE_CLK
2520 if (r8a66597->pdata->on_chip)
2521 clk_put(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +09002522clean_up2:
Paul Mundtd6435102008-11-18 12:40:39 +09002523#endif
Magnus Damm765786e2008-10-31 20:22:38 +09002524 usb_put_hcd(hcd);
2525
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002526clean_up:
2527 if (reg)
2528 iounmap(reg);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002529
2530 return ret;
2531}
2532
2533static struct platform_driver r8a66597_driver = {
2534 .probe = r8a66597_probe,
2535 .remove = r8a66597_remove,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002536 .driver = {
2537 .name = (char *) hcd_name,
Kay Sieversf4fce612008-04-10 21:29:22 -07002538 .owner = THIS_MODULE,
Magnus Dammae1cef62009-07-17 14:52:05 +00002539 .pm = R8A66597_DEV_PM_OPS,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002540 },
2541};
2542
2543static int __init r8a66597_init(void)
2544{
2545 if (usb_disabled())
2546 return -ENODEV;
2547
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -07002548 printk(KERN_INFO KBUILD_MODNAME ": driver %s, %s\n", hcd_name,
2549 DRIVER_VERSION);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002550 return platform_driver_register(&r8a66597_driver);
2551}
2552module_init(r8a66597_init);
2553
2554static void __exit r8a66597_cleanup(void)
2555{
2556 platform_driver_unregister(&r8a66597_driver);
2557}
2558module_exit(r8a66597_cleanup);
2559