blob: f71a73a93d0ccbd3500595e1ae0e7767eb895f1a [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,
Yoshihiro Shimodad8359332010-03-16 12:29:35 +0900421 struct r8a66597_device *dev, int reset)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900422{
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;
Yoshihiro Shimodad8359332010-03-16 12:29:35 +0900433 /*
434 * Only when resetting USB, it is necessary to erase drvdata. When
435 * a usb device with usb hub is disconnect, "dev->udev" is already
436 * freed on usb_desconnect(). So we cannot access the data.
437 */
438 if (reset)
439 dev_set_drvdata(&dev->udev->dev, NULL);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900440 list_del(&dev->device_list);
441 kfree(dev);
442
Magnus Damm719a72b2009-07-17 14:59:55 +0000443 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900444 if (r8a66597->root_hub[port].dev == dev) {
445 r8a66597->root_hub[port].dev = NULL;
446 break;
447 }
448 }
449}
450
451static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
452 u16 mask, u16 loop)
453{
454 u16 tmp;
455 int i = 0;
456
457 do {
458 tmp = r8a66597_read(r8a66597, reg);
459 if (i++ > 1000000) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700460 printk(KERN_ERR "r8a66597: register%lx, loop %x "
461 "is timeout\n", reg, loop);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900462 break;
463 }
464 ndelay(1);
465 } while ((tmp & mask) != loop);
466}
467
468/* this function must be called with interrupt disabled */
469static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
470{
471 u16 tmp;
472
473 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
474 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
475 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
476 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
477}
478
479/* this function must be called with interrupt disabled */
480static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
481{
482 u16 tmp;
483
484 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
485 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
486 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
487 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
488 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
489}
490
491/* this function must be called with interrupt disabled */
492static void clear_all_buffer(struct r8a66597 *r8a66597,
493 struct r8a66597_pipe *pipe)
494{
495 u16 tmp;
496
497 if (!pipe || pipe->info.pipenum == 0)
498 return;
499
500 pipe_stop(r8a66597, pipe);
501 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
502 tmp = r8a66597_read(r8a66597, pipe->pipectr);
503 tmp = r8a66597_read(r8a66597, pipe->pipectr);
504 tmp = r8a66597_read(r8a66597, pipe->pipectr);
505 r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
506}
507
508/* this function must be called with interrupt disabled */
509static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
510 struct r8a66597_pipe *pipe, int toggle)
511{
512 if (toggle)
513 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
514 else
515 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
516}
517
Magnus Damm719a72b2009-07-17 14:59:55 +0000518static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
519{
520 if (r8a66597->pdata->on_chip)
521 return MBW_32;
522 else
523 return MBW_16;
524}
525
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900526/* this function must be called with interrupt disabled */
527static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
528{
Magnus Damm719a72b2009-07-17 14:59:55 +0000529 unsigned short mbw = mbw_value(r8a66597);
530
531 r8a66597_mdfy(r8a66597, mbw | pipenum, mbw | CURPIPE, CFIFOSEL);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900532 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
533}
534
535/* this function must be called with interrupt disabled */
536static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
537 struct r8a66597_pipe *pipe)
538{
Magnus Damm719a72b2009-07-17 14:59:55 +0000539 unsigned short mbw = mbw_value(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900540
Magnus Damm719a72b2009-07-17 14:59:55 +0000541 cfifo_change(r8a66597, 0);
542 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D0FIFOSEL);
543 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D1FIFOSEL);
544
545 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum, mbw | CURPIPE,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900546 pipe->fifosel);
547 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
548}
549
550static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
551{
552 struct r8a66597_pipe *pipe = hep->hcpriv;
553
554 if (usb_pipeendpoint(urb->pipe) == 0)
555 return 0;
556 else
557 return pipe->info.pipenum;
558}
559
560static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
561{
562 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
563
564 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
565}
566
567static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
568 int urb_pipe)
569{
570 if (!dev)
571 return NULL;
572
573 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
574}
575
576/* this function must be called with interrupt disabled */
577static void pipe_toggle_set(struct r8a66597 *r8a66597,
578 struct r8a66597_pipe *pipe,
579 struct urb *urb, int set)
580{
581 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
582 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
583 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
584
585 if (!toggle)
586 return;
587
588 if (set)
589 *toggle |= 1 << endpoint;
590 else
591 *toggle &= ~(1 << endpoint);
592}
593
594/* this function must be called with interrupt disabled */
595static void pipe_toggle_save(struct r8a66597 *r8a66597,
596 struct r8a66597_pipe *pipe,
597 struct urb *urb)
598{
599 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
600 pipe_toggle_set(r8a66597, pipe, urb, 1);
601 else
602 pipe_toggle_set(r8a66597, pipe, urb, 0);
603}
604
605/* this function must be called with interrupt disabled */
606static void pipe_toggle_restore(struct r8a66597 *r8a66597,
607 struct r8a66597_pipe *pipe,
608 struct urb *urb)
609{
610 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
611 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
612 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
613
Yoshihiro Shimodaf6ace2c2007-05-30 20:42:41 +0900614 if (!toggle)
615 return;
616
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900617 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
618}
619
620/* this function must be called with interrupt disabled */
621static void pipe_buffer_setting(struct r8a66597 *r8a66597,
622 struct r8a66597_pipe_info *info)
623{
624 u16 val = 0;
625
626 if (info->pipenum == 0)
627 return;
628
629 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
630 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
631 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
632 if (!info->dir_in)
633 val |= R8A66597_DIR;
634 if (info->type == R8A66597_BULK && info->dir_in)
635 val |= R8A66597_DBLB | R8A66597_SHTNAK;
636 val |= info->type | info->epnum;
637 r8a66597_write(r8a66597, val, PIPECFG);
638
639 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
640 PIPEBUF);
641 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
642 PIPEMAXP);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900643 r8a66597_write(r8a66597, info->interval, PIPEPERI);
644}
645
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900646/* this function must be called with interrupt disabled */
647static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
648{
649 struct r8a66597_pipe_info *info;
650 struct urb *urb = td->urb;
651
652 if (td->pipenum > 0) {
653 info = &td->pipe->info;
654 cfifo_change(r8a66597, 0);
655 pipe_buffer_setting(r8a66597, info);
656
657 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
658 usb_pipeout(urb->pipe)) &&
659 !usb_pipecontrol(urb->pipe)) {
660 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
661 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
662 clear_all_buffer(r8a66597, td->pipe);
663 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
664 usb_pipeout(urb->pipe), 1);
665 }
666 pipe_toggle_restore(r8a66597, td->pipe, urb);
667 }
668}
669
670/* this function must be called with interrupt disabled */
671static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
672 struct usb_endpoint_descriptor *ep)
673{
674 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
675
676 memset(array, 0, sizeof(array));
Julia Lawall2e0fe702008-12-29 11:22:14 +0100677 switch (usb_endpoint_type(ep)) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900678 case USB_ENDPOINT_XFER_BULK:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100679 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900680 array[i++] = 4;
681 else {
682 array[i++] = 3;
683 array[i++] = 5;
684 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900685 break;
686 case USB_ENDPOINT_XFER_INT:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100687 if (usb_endpoint_dir_in(ep)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900688 array[i++] = 6;
689 array[i++] = 7;
690 array[i++] = 8;
691 } else
692 array[i++] = 9;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900693 break;
694 case USB_ENDPOINT_XFER_ISOC:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100695 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900696 array[i++] = 2;
697 else
698 array[i++] = 1;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900699 break;
700 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700701 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900702 return 0;
703 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900704
705 i = 1;
706 min = array[0];
707 while (array[i] != 0) {
708 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
709 min = array[i];
710 i++;
711 }
712
713 return min;
714}
715
716static u16 get_r8a66597_type(__u8 type)
717{
718 u16 r8a66597_type;
719
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900720 switch (type) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900721 case USB_ENDPOINT_XFER_BULK:
722 r8a66597_type = R8A66597_BULK;
723 break;
724 case USB_ENDPOINT_XFER_INT:
725 r8a66597_type = R8A66597_INT;
726 break;
727 case USB_ENDPOINT_XFER_ISOC:
728 r8a66597_type = R8A66597_ISO;
729 break;
730 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700731 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900732 r8a66597_type = 0x0000;
733 break;
734 }
735
736 return r8a66597_type;
737}
738
739static u16 get_bufnum(u16 pipenum)
740{
741 u16 bufnum = 0;
742
743 if (pipenum == 0)
744 bufnum = 0;
745 else if (check_bulk_or_isoc(pipenum))
746 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
747 else if (check_interrupt(pipenum))
748 bufnum = 4 + (pipenum - 6);
749 else
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700750 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900751
752 return bufnum;
753}
754
755static u16 get_buf_bsize(u16 pipenum)
756{
757 u16 buf_bsize = 0;
758
759 if (pipenum == 0)
760 buf_bsize = 3;
761 else if (check_bulk_or_isoc(pipenum))
762 buf_bsize = R8A66597_BUF_BSIZE - 1;
763 else if (check_interrupt(pipenum))
764 buf_bsize = 0;
765 else
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700766 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900767
768 return buf_bsize;
769}
770
771/* this function must be called with interrupt disabled */
772static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
773 struct r8a66597_device *dev,
774 struct r8a66597_pipe *pipe,
775 struct urb *urb)
776{
777 int i;
778 struct r8a66597_pipe_info *info = &pipe->info;
Magnus Damm719a72b2009-07-17 14:59:55 +0000779 unsigned short mbw = mbw_value(r8a66597);
780
781 /* pipe dma is only for external controlles */
782 if (r8a66597->pdata->on_chip)
783 return;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900784
785 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
786 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
787 if ((r8a66597->dma_map & (1 << i)) != 0)
788 continue;
789
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -0700790 dev_info(&dev->udev->dev,
791 "address %d, EndpointAddress 0x%02x use "
792 "DMA FIFO\n", usb_pipedevice(urb->pipe),
793 info->dir_in ?
794 USB_ENDPOINT_DIR_MASK + info->epnum
795 : info->epnum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900796
797 r8a66597->dma_map |= 1 << i;
798 dev->dma_map |= 1 << i;
799 set_pipe_reg_addr(pipe, i);
800
801 cfifo_change(r8a66597, 0);
Magnus Damm719a72b2009-07-17 14:59:55 +0000802 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum,
803 mbw | CURPIPE, pipe->fifosel);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900804
805 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
806 pipe->info.pipenum);
807 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
808 break;
809 }
810 }
811}
812
813/* this function must be called with interrupt disabled */
814static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
815 struct usb_host_endpoint *hep,
816 struct r8a66597_pipe_info *info)
817{
818 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
819 struct r8a66597_pipe *pipe = hep->hcpriv;
820
821 dbg("enable_pipe:");
822
823 pipe->info = *info;
824 set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
825 r8a66597->pipe_cnt[pipe->info.pipenum]++;
826 dev->pipe_cnt[pipe->info.pipenum]++;
827
828 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
829}
830
Paul Mundt27175682010-02-04 06:57:58 +0000831static void r8a66597_urb_done(struct r8a66597 *r8a66597, struct urb *urb,
832 int status)
833__releases(r8a66597->lock)
834__acquires(r8a66597->lock)
835{
836 if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
837 void *ptr;
838
839 for (ptr = urb->transfer_buffer;
840 ptr < urb->transfer_buffer + urb->transfer_buffer_length;
841 ptr += PAGE_SIZE)
842 flush_dcache_page(virt_to_page(ptr));
843 }
844
845 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
846 spin_unlock(&r8a66597->lock);
847 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb, status);
848 spin_lock(&r8a66597->lock);
849}
850
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900851/* this function must be called with interrupt disabled */
852static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
853{
854 struct r8a66597_td *td, *next;
855 struct urb *urb;
856 struct list_head *list = &r8a66597->pipe_queue[pipenum];
857
858 if (list_empty(list))
859 return;
860
861 list_for_each_entry_safe(td, next, list, queue) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900862 if (td->address != address)
863 continue;
864
865 urb = td->urb;
866 list_del(&td->queue);
867 kfree(td);
868
Paul Mundt27175682010-02-04 06:57:58 +0000869 if (urb)
870 r8a66597_urb_done(r8a66597, urb, -ENODEV);
Alan Sterne9df41c2007-08-08 11:48:02 -0400871
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900872 break;
873 }
874}
875
876/* this function must be called with interrupt disabled */
877static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
878 struct r8a66597_device *dev)
879{
880 int check_ep0 = 0;
881 u16 pipenum;
882
883 if (!dev)
884 return;
885
886 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
887 if (!dev->pipe_cnt[pipenum])
888 continue;
889
890 if (!check_ep0) {
891 check_ep0 = 1;
892 force_dequeue(r8a66597, 0, dev->address);
893 }
894
895 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
896 dev->pipe_cnt[pipenum] = 0;
897 force_dequeue(r8a66597, pipenum, dev->address);
898 }
899
900 dbg("disable_pipe");
901
902 r8a66597->dma_map &= ~(dev->dma_map);
903 dev->dma_map = 0;
904}
905
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900906static u16 get_interval(struct urb *urb, __u8 interval)
907{
908 u16 time = 1;
909 int i;
910
911 if (urb->dev->speed == USB_SPEED_HIGH) {
912 if (interval > IITV)
913 time = IITV;
914 else
915 time = interval ? interval - 1 : 0;
916 } else {
917 if (interval > 128) {
918 time = IITV;
919 } else {
920 /* calculate the nearest value for PIPEPERI */
921 for (i = 0; i < 7; i++) {
922 if ((1 << i) < interval &&
923 (1 << (i + 1) > interval))
924 time = 1 << i;
925 }
926 }
927 }
928
929 return time;
930}
931
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900932static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
933{
934 __u8 i;
935 unsigned long time = 1;
936
937 if (usb_pipeisoc(urb->pipe))
938 return 0;
939
940 if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
941 for (i = 0; i < (interval - 1); i++)
942 time *= 2;
943 time = time * 125 / 1000; /* uSOF -> msec */
944 } else {
945 time = interval;
946 }
947
948 return time;
949}
950
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900951/* this function must be called with interrupt disabled */
952static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
953 struct usb_host_endpoint *hep,
954 struct usb_endpoint_descriptor *ep)
955{
956 struct r8a66597_pipe_info info;
957
958 info.pipenum = get_empty_pipenum(r8a66597, ep);
959 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
Julia Lawall2e0fe702008-12-29 11:22:14 +0100960 info.epnum = usb_endpoint_num(ep);
Yoshihiro Shimoda05eac912007-10-03 18:53:28 +0900961 info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
Julia Lawall2e0fe702008-12-29 11:22:14 +0100962 info.type = get_r8a66597_type(usb_endpoint_type(ep));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900963 info.bufnum = get_bufnum(info.pipenum);
964 info.buf_bsize = get_buf_bsize(info.pipenum);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900965 if (info.type == R8A66597_BULK) {
966 info.interval = 0;
967 info.timer_interval = 0;
968 } else {
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900969 info.interval = get_interval(urb, ep->bInterval);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900970 info.timer_interval = get_timer_interval(urb, ep->bInterval);
971 }
Julia Lawall2e0fe702008-12-29 11:22:14 +0100972 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900973 info.dir_in = 1;
974 else
975 info.dir_in = 0;
976
977 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
978}
979
980static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
981{
982 struct r8a66597_device *dev;
983
984 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
985 dev->state = USB_STATE_CONFIGURED;
986}
987
988static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
989 u16 pipenum)
990{
991 if (pipenum == 0 && usb_pipeout(urb->pipe))
992 enable_irq_empty(r8a66597, pipenum);
993 else
994 enable_irq_ready(r8a66597, pipenum);
995
996 if (!usb_pipeisoc(urb->pipe))
997 enable_irq_nrdy(r8a66597, pipenum);
998}
999
1000static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
1001{
1002 disable_irq_ready(r8a66597, pipenum);
1003 disable_irq_nrdy(r8a66597, pipenum);
1004}
1005
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001006static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
1007{
1008 mod_timer(&r8a66597->rh_timer,
1009 jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
1010}
1011
1012static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
1013 int connect)
1014{
1015 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1016
1017 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1018 rh->scount = R8A66597_MAX_SAMPLING;
1019 if (connect)
1020 rh->port |= 1 << USB_PORT_FEAT_CONNECTION;
1021 else
1022 rh->port &= ~(1 << USB_PORT_FEAT_CONNECTION);
1023 rh->port |= 1 << USB_PORT_FEAT_C_CONNECTION;
1024
1025 r8a66597_root_hub_start_polling(r8a66597);
1026}
1027
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001028/* this function must be called with interrupt disabled */
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001029static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
1030 u16 syssts)
Paul Mundt2c940db2010-02-04 06:58:28 +00001031__releases(r8a66597->lock)
1032__acquires(r8a66597->lock)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001033{
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001034 if (syssts == SE0) {
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001035 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001036 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
Yoshihiro Shimoda1e6159f2009-10-21 20:33:39 +09001037 } else {
1038 if (syssts == FS_JSTS)
1039 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
1040 else if (syssts == LS_JSTS)
1041 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
1042
1043 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
1044 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
1045
1046 if (r8a66597->bus_suspended)
1047 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001048 }
1049
Paul Mundt2c940db2010-02-04 06:58:28 +00001050 spin_unlock(&r8a66597->lock);
Yoshihiro Shimoda1e6159f2009-10-21 20:33:39 +09001051 usb_hcd_poll_rh_status(r8a66597_to_hcd(r8a66597));
Paul Mundt2c940db2010-02-04 06:58:28 +00001052 spin_lock(&r8a66597->lock);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001053}
1054
1055/* this function must be called with interrupt disabled */
1056static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
1057{
1058 u16 speed = get_rh_usb_speed(r8a66597, port);
1059 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1060
Yoshihiro Shimoda1e6159f2009-10-21 20:33:39 +09001061 rh->port &= ~((1 << USB_PORT_FEAT_HIGHSPEED) |
1062 (1 << USB_PORT_FEAT_LOWSPEED));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001063 if (speed == HSMODE)
1064 rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
1065 else if (speed == LSMODE)
1066 rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
1067
1068 rh->port &= ~(1 << USB_PORT_FEAT_RESET);
1069 rh->port |= 1 << USB_PORT_FEAT_ENABLE;
1070}
1071
1072/* this function must be called with interrupt disabled */
1073static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
1074{
1075 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
1076
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001077 disable_r8a66597_pipe_all(r8a66597, dev);
Yoshihiro Shimodad8359332010-03-16 12:29:35 +09001078 free_usb_address(r8a66597, dev, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001079
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001080 start_root_hub_sampling(r8a66597, port, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001081}
1082
1083/* this function must be called with interrupt disabled */
1084static void prepare_setup_packet(struct r8a66597 *r8a66597,
1085 struct r8a66597_td *td)
1086{
1087 int i;
Al Virofd05e722008-04-28 07:00:16 +01001088 __le16 *p = (__le16 *)td->urb->setup_packet;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001089 unsigned long setup_addr = USBREQ;
1090
1091 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1092 DCPMAXP);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001093 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001094
1095 for (i = 0; i < 4; i++) {
Al Virofd05e722008-04-28 07:00:16 +01001096 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001097 setup_addr += 2;
1098 }
1099 r8a66597_write(r8a66597, SUREQ, DCPCTR);
1100}
1101
1102/* this function must be called with interrupt disabled */
1103static void prepare_packet_read(struct r8a66597 *r8a66597,
1104 struct r8a66597_td *td)
1105{
1106 struct urb *urb = td->urb;
1107
1108 if (usb_pipecontrol(urb->pipe)) {
1109 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1110 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1111 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1112 if (urb->actual_length == 0) {
1113 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1114 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1115 }
1116 pipe_irq_disable(r8a66597, td->pipenum);
1117 pipe_start(r8a66597, td->pipe);
1118 pipe_irq_enable(r8a66597, urb, td->pipenum);
1119 } else {
1120 if (urb->actual_length == 0) {
1121 pipe_irq_disable(r8a66597, td->pipenum);
1122 pipe_setting(r8a66597, td);
1123 pipe_stop(r8a66597, td->pipe);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001124 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001125
1126 if (td->pipe->pipetre) {
1127 r8a66597_write(r8a66597, TRCLR,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001128 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001129 r8a66597_write(r8a66597,
Julia Lawalldfa5ec72008-03-04 15:25:11 -08001130 DIV_ROUND_UP
1131 (urb->transfer_buffer_length,
1132 td->maxpacket),
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001133 td->pipe->pipetrn);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001134 r8a66597_bset(r8a66597, TRENB,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001135 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001136 }
1137
1138 pipe_start(r8a66597, td->pipe);
1139 pipe_irq_enable(r8a66597, urb, td->pipenum);
1140 }
1141 }
1142}
1143
1144/* this function must be called with interrupt disabled */
1145static void prepare_packet_write(struct r8a66597 *r8a66597,
1146 struct r8a66597_td *td)
1147{
1148 u16 tmp;
1149 struct urb *urb = td->urb;
1150
1151 if (usb_pipecontrol(urb->pipe)) {
1152 pipe_stop(r8a66597, td->pipe);
1153 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1154 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1155 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1156 if (urb->actual_length == 0) {
1157 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1158 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1159 }
1160 } else {
1161 if (urb->actual_length == 0)
1162 pipe_setting(r8a66597, td);
1163 if (td->pipe->pipetre)
1164 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1165 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001166 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001167
1168 fifo_change_from_pipe(r8a66597, td->pipe);
1169 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1170 if (unlikely((tmp & FRDY) == 0))
1171 pipe_irq_enable(r8a66597, urb, td->pipenum);
1172 else
1173 packet_write(r8a66597, td->pipenum);
1174 pipe_start(r8a66597, td->pipe);
1175}
1176
1177/* this function must be called with interrupt disabled */
1178static void prepare_status_packet(struct r8a66597 *r8a66597,
1179 struct r8a66597_td *td)
1180{
1181 struct urb *urb = td->urb;
1182
1183 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001184 pipe_stop(r8a66597, td->pipe);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001185
1186 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1187 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1188 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1189 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001190 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +09001191 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001192 enable_irq_empty(r8a66597, 0);
1193 } else {
1194 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1195 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1196 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1197 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001198 enable_irq_ready(r8a66597, 0);
1199 }
1200 enable_irq_nrdy(r8a66597, 0);
1201 pipe_start(r8a66597, td->pipe);
1202}
1203
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001204static int is_set_address(unsigned char *setup_packet)
1205{
1206 if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1207 setup_packet[1] == USB_REQ_SET_ADDRESS)
1208 return 1;
1209 else
1210 return 0;
1211}
1212
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001213/* this function must be called with interrupt disabled */
1214static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1215{
1216 BUG_ON(!td);
1217
1218 switch (td->type) {
1219 case USB_PID_SETUP:
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001220 if (is_set_address(td->urb->setup_packet)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001221 td->set_address = 1;
1222 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1223 td->urb);
1224 if (td->urb->setup_packet[2] == 0)
1225 return -EPIPE;
1226 }
1227 prepare_setup_packet(r8a66597, td);
1228 break;
1229 case USB_PID_IN:
1230 prepare_packet_read(r8a66597, td);
1231 break;
1232 case USB_PID_OUT:
1233 prepare_packet_write(r8a66597, td);
1234 break;
1235 case USB_PID_ACK:
1236 prepare_status_packet(r8a66597, td);
1237 break;
1238 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001239 printk(KERN_ERR "r8a66597: invalid type.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001240 break;
1241 }
1242
1243 return 0;
1244}
1245
1246static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1247{
1248 if (usb_pipeisoc(urb->pipe)) {
1249 if (urb->number_of_packets == td->iso_cnt)
1250 return 1;
1251 }
1252
1253 /* control or bulk or interrupt */
1254 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1255 (td->short_packet) || (td->zero_packet))
1256 return 1;
1257
1258 return 0;
1259}
1260
1261/* this function must be called with interrupt disabled */
1262static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1263{
1264 unsigned long time;
1265
1266 BUG_ON(!td);
1267
1268 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1269 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1270 r8a66597->timeout_map |= 1 << td->pipenum;
1271 switch (usb_pipetype(td->urb->pipe)) {
1272 case PIPE_INTERRUPT:
1273 case PIPE_ISOCHRONOUS:
1274 time = 30;
1275 break;
1276 default:
1277 time = 300;
1278 break;
1279 }
1280
1281 mod_timer(&r8a66597->td_timer[td->pipenum],
1282 jiffies + msecs_to_jiffies(time));
1283 }
1284}
1285
1286/* this function must be called with interrupt disabled */
Alan Sterndfd1e532007-08-21 15:36:52 -04001287static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
Alan Stern888fda42007-08-24 15:41:18 -04001288 u16 pipenum, struct urb *urb, int status)
Alan Sterndfd1e532007-08-21 15:36:52 -04001289__releases(r8a66597->lock) __acquires(r8a66597->lock)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001290{
1291 int restart = 0;
1292 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1293
1294 r8a66597->timeout_map &= ~(1 << pipenum);
1295
1296 if (likely(td)) {
Alan Stern888fda42007-08-24 15:41:18 -04001297 if (td->set_address && (status != 0 || urb->unlinked))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001298 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1299
1300 pipe_toggle_save(r8a66597, td->pipe, urb);
1301 list_del(&td->queue);
1302 kfree(td);
1303 }
1304
1305 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1306 restart = 1;
1307
1308 if (likely(urb)) {
1309 if (usb_pipeisoc(urb->pipe))
1310 urb->start_frame = r8a66597_get_frame(hcd);
1311
Paul Mundt27175682010-02-04 06:57:58 +00001312 r8a66597_urb_done(r8a66597, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001313 }
1314
1315 if (restart) {
1316 td = r8a66597_get_td(r8a66597, pipenum);
1317 if (unlikely(!td))
1318 return;
1319
1320 start_transfer(r8a66597, td);
1321 set_td_timer(r8a66597, td);
1322 }
1323}
1324
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001325static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1326{
1327 u16 tmp;
1328 int rcv_len, bufsize, urb_len, size;
1329 u16 *buf;
1330 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1331 struct urb *urb;
1332 int finish = 0;
Alan Sterndfd1e532007-08-21 15:36:52 -04001333 int status = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001334
1335 if (unlikely(!td))
1336 return;
1337 urb = td->urb;
1338
1339 fifo_change_from_pipe(r8a66597, td->pipe);
1340 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1341 if (unlikely((tmp & FRDY) == 0)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001342 pipe_stop(r8a66597, td->pipe);
1343 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001344 printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001345 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001346 return;
1347 }
1348
1349 /* prepare parameters */
1350 rcv_len = tmp & DTLN;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001351 if (usb_pipeisoc(urb->pipe)) {
1352 buf = (u16 *)(urb->transfer_buffer +
1353 urb->iso_frame_desc[td->iso_cnt].offset);
1354 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1355 } else {
1356 buf = (void *)urb->transfer_buffer + urb->actual_length;
1357 urb_len = urb->transfer_buffer_length - urb->actual_length;
1358 }
Alan Sterndfd1e532007-08-21 15:36:52 -04001359 bufsize = min(urb_len, (int) td->maxpacket);
1360 if (rcv_len <= bufsize) {
1361 size = rcv_len;
1362 } else {
1363 size = bufsize;
1364 status = -EOVERFLOW;
1365 finish = 1;
1366 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001367
1368 /* update parameters */
1369 urb->actual_length += size;
1370 if (rcv_len == 0)
1371 td->zero_packet = 1;
Alan Sterndfd1e532007-08-21 15:36:52 -04001372 if (rcv_len < bufsize) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001373 td->short_packet = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001374 }
1375 if (usb_pipeisoc(urb->pipe)) {
1376 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
Alan Sterndfd1e532007-08-21 15:36:52 -04001377 urb->iso_frame_desc[td->iso_cnt].status = status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001378 td->iso_cnt++;
Alan Sterndfd1e532007-08-21 15:36:52 -04001379 finish = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001380 }
1381
1382 /* check transfer finish */
Alan Sternb0d9efb2007-08-21 15:39:21 -04001383 if (finish || check_transfer_finish(td, urb)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001384 pipe_stop(r8a66597, td->pipe);
1385 pipe_irq_disable(r8a66597, pipenum);
1386 finish = 1;
1387 }
1388
1389 /* read fifo */
1390 if (urb->transfer_buffer) {
1391 if (size == 0)
1392 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1393 else
1394 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1395 buf, size);
1396 }
1397
Alan Stern888fda42007-08-24 15:41:18 -04001398 if (finish && pipenum != 0)
1399 finish_request(r8a66597, td, pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001400}
1401
1402static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1403{
1404 u16 tmp;
1405 int bufsize, size;
1406 u16 *buf;
1407 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1408 struct urb *urb;
1409
1410 if (unlikely(!td))
1411 return;
1412 urb = td->urb;
1413
1414 fifo_change_from_pipe(r8a66597, td->pipe);
1415 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1416 if (unlikely((tmp & FRDY) == 0)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001417 pipe_stop(r8a66597, td->pipe);
1418 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001419 printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001420 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001421 return;
1422 }
1423
1424 /* prepare parameters */
1425 bufsize = td->maxpacket;
1426 if (usb_pipeisoc(urb->pipe)) {
1427 buf = (u16 *)(urb->transfer_buffer +
1428 urb->iso_frame_desc[td->iso_cnt].offset);
1429 size = min(bufsize,
1430 (int)urb->iso_frame_desc[td->iso_cnt].length);
1431 } else {
1432 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
Greg Kroah-Hartman16e2e5f2009-03-03 16:44:13 -08001433 size = min_t(u32, bufsize,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001434 urb->transfer_buffer_length - urb->actual_length);
1435 }
1436
1437 /* write fifo */
1438 if (pipenum > 0)
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001439 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001440 if (urb->transfer_buffer) {
1441 r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
1442 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1443 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1444 }
1445
1446 /* update parameters */
1447 urb->actual_length += size;
1448 if (usb_pipeisoc(urb->pipe)) {
1449 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1450 urb->iso_frame_desc[td->iso_cnt].status = 0;
1451 td->iso_cnt++;
1452 }
1453
1454 /* check transfer finish */
1455 if (check_transfer_finish(td, urb)) {
1456 disable_irq_ready(r8a66597, pipenum);
1457 enable_irq_empty(r8a66597, pipenum);
1458 if (!usb_pipeisoc(urb->pipe))
1459 enable_irq_nrdy(r8a66597, pipenum);
1460 } else
1461 pipe_irq_enable(r8a66597, urb, pipenum);
1462}
1463
1464
Alan Stern888fda42007-08-24 15:41:18 -04001465static void check_next_phase(struct r8a66597 *r8a66597, int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001466{
1467 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1468 struct urb *urb;
1469 u8 finish = 0;
1470
1471 if (unlikely(!td))
1472 return;
1473 urb = td->urb;
1474
1475 switch (td->type) {
1476 case USB_PID_IN:
1477 case USB_PID_OUT:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001478 if (check_transfer_finish(td, urb))
1479 td->type = USB_PID_ACK;
1480 break;
1481 case USB_PID_SETUP:
Alan Sterneb231052007-08-21 15:40:36 -04001482 if (urb->transfer_buffer_length == urb->actual_length)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001483 td->type = USB_PID_ACK;
Alan Sterneb231052007-08-21 15:40:36 -04001484 else if (usb_pipeout(urb->pipe))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001485 td->type = USB_PID_OUT;
1486 else
1487 td->type = USB_PID_IN;
1488 break;
1489 case USB_PID_ACK:
1490 finish = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001491 break;
1492 }
1493
Alan Stern888fda42007-08-24 15:41:18 -04001494 if (finish || status != 0 || urb->unlinked)
1495 finish_request(r8a66597, td, 0, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001496 else
1497 start_transfer(r8a66597, td);
1498}
1499
Alan Stern888fda42007-08-24 15:41:18 -04001500static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001501{
1502 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1503
Alan Stern888fda42007-08-24 15:41:18 -04001504 if (td) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001505 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1506
1507 if (pid == PID_NAK)
Alan Stern888fda42007-08-24 15:41:18 -04001508 return -ECONNRESET;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001509 else
Alan Stern888fda42007-08-24 15:41:18 -04001510 return -EPIPE;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001511 }
Alan Stern888fda42007-08-24 15:41:18 -04001512 return 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001513}
1514
1515static void irq_pipe_ready(struct r8a66597 *r8a66597)
1516{
1517 u16 check;
1518 u16 pipenum;
1519 u16 mask;
1520 struct r8a66597_td *td;
1521
1522 mask = r8a66597_read(r8a66597, BRDYSTS)
1523 & r8a66597_read(r8a66597, BRDYENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001524 r8a66597_write(r8a66597, ~mask, BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001525 if (mask & BRDY0) {
1526 td = r8a66597_get_td(r8a66597, 0);
1527 if (td && td->type == USB_PID_IN)
1528 packet_read(r8a66597, 0);
1529 else
1530 pipe_irq_disable(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001531 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001532 }
1533
1534 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1535 check = 1 << pipenum;
1536 if (mask & check) {
1537 td = r8a66597_get_td(r8a66597, pipenum);
1538 if (unlikely(!td))
1539 continue;
1540
1541 if (td->type == USB_PID_IN)
1542 packet_read(r8a66597, pipenum);
1543 else if (td->type == USB_PID_OUT)
1544 packet_write(r8a66597, pipenum);
1545 }
1546 }
1547}
1548
1549static void irq_pipe_empty(struct r8a66597 *r8a66597)
1550{
1551 u16 tmp;
1552 u16 check;
1553 u16 pipenum;
1554 u16 mask;
1555 struct r8a66597_td *td;
1556
1557 mask = r8a66597_read(r8a66597, BEMPSTS)
1558 & r8a66597_read(r8a66597, BEMPENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001559 r8a66597_write(r8a66597, ~mask, BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001560 if (mask & BEMP0) {
1561 cfifo_change(r8a66597, 0);
1562 td = r8a66597_get_td(r8a66597, 0);
1563 if (td && td->type != USB_PID_OUT)
1564 disable_irq_empty(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001565 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001566 }
1567
1568 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1569 check = 1 << pipenum;
1570 if (mask & check) {
1571 struct r8a66597_td *td;
1572 td = r8a66597_get_td(r8a66597, pipenum);
1573 if (unlikely(!td))
1574 continue;
1575
1576 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1577 if ((tmp & INBUFM) == 0) {
1578 disable_irq_empty(r8a66597, pipenum);
1579 pipe_irq_disable(r8a66597, pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001580 finish_request(r8a66597, td, pipenum, td->urb,
1581 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001582 }
1583 }
1584 }
1585}
1586
1587static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1588{
1589 u16 check;
1590 u16 pipenum;
1591 u16 mask;
Alan Stern888fda42007-08-24 15:41:18 -04001592 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001593
1594 mask = r8a66597_read(r8a66597, NRDYSTS)
1595 & r8a66597_read(r8a66597, NRDYENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001596 r8a66597_write(r8a66597, ~mask, NRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001597 if (mask & NRDY0) {
1598 cfifo_change(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001599 status = get_urb_error(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001600 pipe_irq_disable(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001601 check_next_phase(r8a66597, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001602 }
1603
1604 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1605 check = 1 << pipenum;
1606 if (mask & check) {
1607 struct r8a66597_td *td;
1608 td = r8a66597_get_td(r8a66597, pipenum);
1609 if (unlikely(!td))
1610 continue;
1611
Alan Stern888fda42007-08-24 15:41:18 -04001612 status = get_urb_error(r8a66597, pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001613 pipe_irq_disable(r8a66597, pipenum);
1614 pipe_stop(r8a66597, td->pipe);
Alan Stern888fda42007-08-24 15:41:18 -04001615 finish_request(r8a66597, td, pipenum, td->urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001616 }
1617 }
1618}
1619
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001620static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1621{
1622 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1623 u16 intsts0, intsts1, intsts2;
1624 u16 intenb0, intenb1, intenb2;
1625 u16 mask0, mask1, mask2;
Alan Stern888fda42007-08-24 15:41:18 -04001626 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001627
1628 spin_lock(&r8a66597->lock);
1629
1630 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1631 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1632 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1633 intenb0 = r8a66597_read(r8a66597, INTENB0);
1634 intenb1 = r8a66597_read(r8a66597, INTENB1);
1635 intenb2 = r8a66597_read(r8a66597, INTENB2);
1636
1637 mask2 = intsts2 & intenb2;
1638 mask1 = intsts1 & intenb1;
1639 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1640 if (mask2) {
1641 if (mask2 & ATTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001642 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001643 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1644
1645 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001646 start_root_hub_sampling(r8a66597, 1, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001647 }
1648 if (mask2 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001649 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001650 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1651 r8a66597_usb_disconnect(r8a66597, 1);
1652 }
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09001653 if (mask2 & BCHG) {
1654 r8a66597_write(r8a66597, ~BCHG, INTSTS2);
1655 r8a66597_bclr(r8a66597, BCHGE, INTENB2);
1656 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1657 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001658 }
1659
1660 if (mask1) {
1661 if (mask1 & ATTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001662 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001663 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1664
1665 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001666 start_root_hub_sampling(r8a66597, 0, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001667 }
1668 if (mask1 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001669 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001670 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1671 r8a66597_usb_disconnect(r8a66597, 0);
1672 }
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09001673 if (mask1 & BCHG) {
1674 r8a66597_write(r8a66597, ~BCHG, INTSTS1);
1675 r8a66597_bclr(r8a66597, BCHGE, INTENB1);
1676 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1677 }
1678
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001679 if (mask1 & SIGN) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001680 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001681 status = get_urb_error(r8a66597, 0);
1682 check_next_phase(r8a66597, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001683 }
1684 if (mask1 & SACK) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001685 r8a66597_write(r8a66597, ~SACK, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001686 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001687 }
1688 }
1689 if (mask0) {
1690 if (mask0 & BRDY)
1691 irq_pipe_ready(r8a66597);
1692 if (mask0 & BEMP)
1693 irq_pipe_empty(r8a66597);
1694 if (mask0 & NRDY)
1695 irq_pipe_nrdy(r8a66597);
1696 }
1697
1698 spin_unlock(&r8a66597->lock);
1699 return IRQ_HANDLED;
1700}
1701
1702/* this function must be called with interrupt disabled */
1703static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1704{
1705 u16 tmp;
1706 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1707
1708 if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
1709 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1710
1711 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1712 if ((tmp & USBRST) == USBRST) {
1713 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1714 dvstctr_reg);
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001715 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001716 } else
1717 r8a66597_usb_connect(r8a66597, port);
1718 }
1719
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001720 if (!(rh->port & (1 << USB_PORT_FEAT_CONNECTION))) {
1721 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1722 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1723 }
1724
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001725 if (rh->scount > 0) {
1726 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1727 if (tmp == rh->old_syssts) {
1728 rh->scount--;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001729 if (rh->scount == 0)
1730 r8a66597_check_syssts(r8a66597, port, tmp);
1731 else
1732 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001733 } else {
1734 rh->scount = R8A66597_MAX_SAMPLING;
1735 rh->old_syssts = tmp;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001736 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001737 }
1738 }
1739}
1740
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001741static void r8a66597_interval_timer(unsigned long _r8a66597)
1742{
1743 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1744 unsigned long flags;
1745 u16 pipenum;
1746 struct r8a66597_td *td;
1747
1748 spin_lock_irqsave(&r8a66597->lock, flags);
1749
1750 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1751 if (!(r8a66597->interval_map & (1 << pipenum)))
1752 continue;
1753 if (timer_pending(&r8a66597->interval_timer[pipenum]))
1754 continue;
1755
1756 td = r8a66597_get_td(r8a66597, pipenum);
1757 if (td)
1758 start_transfer(r8a66597, td);
1759 }
1760
1761 spin_unlock_irqrestore(&r8a66597->lock, flags);
1762}
1763
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001764static void r8a66597_td_timer(unsigned long _r8a66597)
1765{
1766 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1767 unsigned long flags;
1768 u16 pipenum;
1769 struct r8a66597_td *td, *new_td = NULL;
1770 struct r8a66597_pipe *pipe;
1771
1772 spin_lock_irqsave(&r8a66597->lock, flags);
1773 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1774 if (!(r8a66597->timeout_map & (1 << pipenum)))
1775 continue;
1776 if (timer_pending(&r8a66597->td_timer[pipenum]))
1777 continue;
1778
1779 td = r8a66597_get_td(r8a66597, pipenum);
1780 if (!td) {
1781 r8a66597->timeout_map &= ~(1 << pipenum);
1782 continue;
1783 }
1784
1785 if (td->urb->actual_length) {
1786 set_td_timer(r8a66597, td);
1787 break;
1788 }
1789
1790 pipe = td->pipe;
1791 pipe_stop(r8a66597, pipe);
1792
1793 new_td = td;
1794 do {
1795 list_move_tail(&new_td->queue,
1796 &r8a66597->pipe_queue[pipenum]);
1797 new_td = r8a66597_get_td(r8a66597, pipenum);
1798 if (!new_td) {
1799 new_td = td;
1800 break;
1801 }
1802 } while (td != new_td && td->address == new_td->address);
1803
1804 start_transfer(r8a66597, new_td);
1805
1806 if (td == new_td)
1807 r8a66597->timeout_map &= ~(1 << pipenum);
1808 else
1809 set_td_timer(r8a66597, new_td);
1810 break;
1811 }
1812 spin_unlock_irqrestore(&r8a66597->lock, flags);
1813}
1814
1815static void r8a66597_timer(unsigned long _r8a66597)
1816{
1817 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1818 unsigned long flags;
Yoshihiro Shimoda58639642008-11-11 16:47:21 +09001819 int port;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001820
1821 spin_lock_irqsave(&r8a66597->lock, flags);
1822
Magnus Damm719a72b2009-07-17 14:59:55 +00001823 for (port = 0; port < r8a66597->max_root_hub; port++)
Yoshihiro Shimoda58639642008-11-11 16:47:21 +09001824 r8a66597_root_hub_control(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001825
1826 spin_unlock_irqrestore(&r8a66597->lock, flags);
1827}
1828
1829static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1830{
1831 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1832
1833 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1834 (urb->dev->state == USB_STATE_CONFIGURED))
1835 return 1;
1836 else
1837 return 0;
1838}
1839
1840static int r8a66597_start(struct usb_hcd *hcd)
1841{
1842 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001843
1844 hcd->state = HC_STATE_RUNNING;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001845 return enable_controller(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001846}
1847
1848static void r8a66597_stop(struct usb_hcd *hcd)
1849{
1850 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1851
1852 disable_controller(r8a66597);
1853}
1854
1855static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1856{
1857 unsigned int usb_address = usb_pipedevice(urb->pipe);
1858 u16 root_port, hub_port;
1859
1860 if (usb_address == 0) {
Magnus Damm719a72b2009-07-17 14:59:55 +00001861 get_port_number(r8a66597, urb->dev->devpath,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001862 &root_port, &hub_port);
1863 set_devadd_reg(r8a66597, 0,
1864 get_r8a66597_usb_speed(urb->dev->speed),
1865 get_parent_r8a66597_address(r8a66597, urb->dev),
1866 hub_port, root_port);
1867 }
1868}
1869
1870static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1871 struct urb *urb,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001872 struct usb_host_endpoint *hep)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001873{
1874 struct r8a66597_td *td;
1875 u16 pipenum;
1876
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001877 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001878 if (td == NULL)
1879 return NULL;
1880
1881 pipenum = r8a66597_get_pipenum(urb, hep);
1882 td->pipenum = pipenum;
1883 td->pipe = hep->hcpriv;
1884 td->urb = urb;
1885 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1886 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1887 !usb_pipein(urb->pipe));
1888 if (usb_pipecontrol(urb->pipe))
1889 td->type = USB_PID_SETUP;
1890 else if (usb_pipein(urb->pipe))
1891 td->type = USB_PID_IN;
1892 else
1893 td->type = USB_PID_OUT;
1894 INIT_LIST_HEAD(&td->queue);
1895
1896 return td;
1897}
1898
1899static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001900 struct urb *urb,
1901 gfp_t mem_flags)
1902{
Alan Sterne9df41c2007-08-08 11:48:02 -04001903 struct usb_host_endpoint *hep = urb->ep;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001904 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1905 struct r8a66597_td *td = NULL;
Alan Sterne9df41c2007-08-08 11:48:02 -04001906 int ret, request = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001907 unsigned long flags;
1908
1909 spin_lock_irqsave(&r8a66597->lock, flags);
1910 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1911 ret = -ENODEV;
Alan Sterne9df41c2007-08-08 11:48:02 -04001912 goto error_not_linked;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001913 }
1914
Alan Sterne9df41c2007-08-08 11:48:02 -04001915 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1916 if (ret)
1917 goto error_not_linked;
1918
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001919 if (!hep->hcpriv) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001920 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1921 GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001922 if (!hep->hcpriv) {
1923 ret = -ENOMEM;
1924 goto error;
1925 }
1926 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1927 if (usb_pipeendpoint(urb->pipe))
1928 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1929 }
1930
1931 if (unlikely(check_pipe_config(r8a66597, urb)))
1932 init_pipe_config(r8a66597, urb);
1933
1934 set_address_zero(r8a66597, urb);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001935 td = r8a66597_make_td(r8a66597, urb, hep);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001936 if (td == NULL) {
1937 ret = -ENOMEM;
1938 goto error;
1939 }
1940 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1941 request = 1;
1942 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001943 urb->hcpriv = td;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001944
1945 if (request) {
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001946 if (td->pipe->info.timer_interval) {
1947 r8a66597->interval_map |= 1 << td->pipenum;
1948 mod_timer(&r8a66597->interval_timer[td->pipenum],
1949 jiffies + msecs_to_jiffies(
1950 td->pipe->info.timer_interval));
1951 } else {
1952 ret = start_transfer(r8a66597, td);
1953 if (ret < 0) {
1954 list_del(&td->queue);
1955 kfree(td);
1956 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001957 }
1958 } else
1959 set_td_timer(r8a66597, td);
1960
1961error:
Alan Sterne9df41c2007-08-08 11:48:02 -04001962 if (ret)
1963 usb_hcd_unlink_urb_from_ep(hcd, urb);
1964error_not_linked:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001965 spin_unlock_irqrestore(&r8a66597->lock, flags);
1966 return ret;
1967}
1968
Alan Sterne9df41c2007-08-08 11:48:02 -04001969static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1970 int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001971{
1972 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1973 struct r8a66597_td *td;
1974 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001975 int rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001976
1977 spin_lock_irqsave(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001978 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1979 if (rc)
1980 goto done;
1981
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001982 if (urb->hcpriv) {
1983 td = urb->hcpriv;
1984 pipe_stop(r8a66597, td->pipe);
1985 pipe_irq_disable(r8a66597, td->pipenum);
1986 disable_irq_empty(r8a66597, td->pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001987 finish_request(r8a66597, td, td->pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001988 }
Alan Sterne9df41c2007-08-08 11:48:02 -04001989 done:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001990 spin_unlock_irqrestore(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001991 return rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001992}
1993
1994static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1995 struct usb_host_endpoint *hep)
1996{
1997 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1998 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1999 struct r8a66597_td *td;
2000 struct urb *urb = NULL;
2001 u16 pipenum;
2002 unsigned long flags;
2003
2004 if (pipe == NULL)
2005 return;
2006 pipenum = pipe->info.pipenum;
2007
2008 if (pipenum == 0) {
2009 kfree(hep->hcpriv);
2010 hep->hcpriv = NULL;
2011 return;
2012 }
2013
2014 spin_lock_irqsave(&r8a66597->lock, flags);
2015 pipe_stop(r8a66597, pipe);
2016 pipe_irq_disable(r8a66597, pipenum);
2017 disable_irq_empty(r8a66597, pipenum);
2018 td = r8a66597_get_td(r8a66597, pipenum);
2019 if (td)
2020 urb = td->urb;
Alan Stern888fda42007-08-24 15:41:18 -04002021 finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002022 kfree(hep->hcpriv);
2023 hep->hcpriv = NULL;
2024 spin_unlock_irqrestore(&r8a66597->lock, flags);
2025}
2026
2027static int r8a66597_get_frame(struct usb_hcd *hcd)
2028{
2029 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2030 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
2031}
2032
2033static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
2034{
2035 int chix;
2036
2037 if (udev->state == USB_STATE_CONFIGURED &&
2038 udev->parent && udev->parent->devnum > 1 &&
2039 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
2040 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
2041
2042 for (chix = 0; chix < udev->maxchild; chix++) {
2043 struct usb_device *childdev = udev->children[chix];
2044
2045 if (childdev)
2046 collect_usb_address_map(childdev, map);
2047 }
2048}
2049
2050/* this function must be called with interrupt disabled */
2051static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
2052 int addr)
2053{
2054 struct r8a66597_device *dev;
2055 struct list_head *list = &r8a66597->child_device;
2056
2057 list_for_each_entry(dev, list, device_list) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002058 if (dev->usb_address != addr)
2059 continue;
2060
2061 return dev;
2062 }
2063
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002064 printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002065 return NULL;
2066}
2067
2068static void update_usb_address_map(struct r8a66597 *r8a66597,
2069 struct usb_device *root_hub,
2070 unsigned long *map)
2071{
2072 int i, j, addr;
2073 unsigned long diff;
2074 unsigned long flags;
2075
2076 for (i = 0; i < 4; i++) {
2077 diff = r8a66597->child_connect_map[i] ^ map[i];
2078 if (!diff)
2079 continue;
2080
2081 for (j = 0; j < 32; j++) {
2082 if (!(diff & (1 << j)))
2083 continue;
2084
2085 addr = i * 32 + j;
2086 if (map[i] & (1 << j))
2087 set_child_connect_map(r8a66597, addr);
2088 else {
2089 struct r8a66597_device *dev;
2090
2091 spin_lock_irqsave(&r8a66597->lock, flags);
2092 dev = get_r8a66597_device(r8a66597, addr);
2093 disable_r8a66597_pipe_all(r8a66597, dev);
Yoshihiro Shimodad8359332010-03-16 12:29:35 +09002094 free_usb_address(r8a66597, dev, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002095 put_child_connect_map(r8a66597, addr);
2096 spin_unlock_irqrestore(&r8a66597->lock, flags);
2097 }
2098 }
2099 }
2100}
2101
2102static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
2103 struct usb_hcd *hcd)
2104{
2105 struct usb_bus *bus;
2106 unsigned long now_map[4];
2107
2108 memset(now_map, 0, sizeof(now_map));
2109
2110 list_for_each_entry(bus, &usb_bus_list, bus_list) {
2111 if (!bus->root_hub)
2112 continue;
2113
2114 if (bus->busnum != hcd->self.busnum)
2115 continue;
2116
2117 collect_usb_address_map(bus->root_hub, now_map);
2118 update_usb_address_map(r8a66597, bus->root_hub, now_map);
2119 }
2120}
2121
2122static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2123{
2124 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2125 unsigned long flags;
2126 int i;
2127
2128 r8a66597_check_detect_child(r8a66597, hcd);
2129
2130 spin_lock_irqsave(&r8a66597->lock, flags);
2131
2132 *buf = 0; /* initialize (no change) */
2133
Magnus Damm719a72b2009-07-17 14:59:55 +00002134 for (i = 0; i < r8a66597->max_root_hub; i++) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002135 if (r8a66597->root_hub[i].port & 0xffff0000)
2136 *buf |= 1 << (i + 1);
2137 }
2138
2139 spin_unlock_irqrestore(&r8a66597->lock, flags);
2140
2141 return (*buf != 0);
2142}
2143
2144static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2145 struct usb_hub_descriptor *desc)
2146{
2147 desc->bDescriptorType = 0x29;
2148 desc->bHubContrCurrent = 0;
Magnus Damm719a72b2009-07-17 14:59:55 +00002149 desc->bNbrPorts = r8a66597->max_root_hub;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002150 desc->bDescLength = 9;
2151 desc->bPwrOn2PwrGood = 0;
2152 desc->wHubCharacteristics = cpu_to_le16(0x0011);
Magnus Damm719a72b2009-07-17 14:59:55 +00002153 desc->bitmap[0] = ((1 << r8a66597->max_root_hub) - 1) << 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002154 desc->bitmap[1] = ~0;
2155}
2156
2157static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2158 u16 wIndex, char *buf, u16 wLength)
2159{
2160 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2161 int ret;
2162 int port = (wIndex & 0x00FF) - 1;
2163 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2164 unsigned long flags;
2165
2166 ret = 0;
2167
2168 spin_lock_irqsave(&r8a66597->lock, flags);
2169 switch (typeReq) {
2170 case ClearHubFeature:
2171 case SetHubFeature:
2172 switch (wValue) {
2173 case C_HUB_OVER_CURRENT:
2174 case C_HUB_LOCAL_POWER:
2175 break;
2176 default:
2177 goto error;
2178 }
2179 break;
2180 case ClearPortFeature:
Magnus Damm719a72b2009-07-17 14:59:55 +00002181 if (wIndex > r8a66597->max_root_hub)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002182 goto error;
2183 if (wLength != 0)
2184 goto error;
2185
2186 switch (wValue) {
2187 case USB_PORT_FEAT_ENABLE:
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002188 rh->port &= ~(1 << USB_PORT_FEAT_POWER);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002189 break;
2190 case USB_PORT_FEAT_SUSPEND:
2191 break;
2192 case USB_PORT_FEAT_POWER:
2193 r8a66597_port_power(r8a66597, port, 0);
2194 break;
2195 case USB_PORT_FEAT_C_ENABLE:
2196 case USB_PORT_FEAT_C_SUSPEND:
2197 case USB_PORT_FEAT_C_CONNECTION:
2198 case USB_PORT_FEAT_C_OVER_CURRENT:
2199 case USB_PORT_FEAT_C_RESET:
2200 break;
2201 default:
2202 goto error;
2203 }
2204 rh->port &= ~(1 << wValue);
2205 break;
2206 case GetHubDescriptor:
2207 r8a66597_hub_descriptor(r8a66597,
2208 (struct usb_hub_descriptor *)buf);
2209 break;
2210 case GetHubStatus:
2211 *buf = 0x00;
2212 break;
2213 case GetPortStatus:
Magnus Damm719a72b2009-07-17 14:59:55 +00002214 if (wIndex > r8a66597->max_root_hub)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002215 goto error;
Al Virofd05e722008-04-28 07:00:16 +01002216 *(__le32 *)buf = cpu_to_le32(rh->port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002217 break;
2218 case SetPortFeature:
Magnus Damm719a72b2009-07-17 14:59:55 +00002219 if (wIndex > r8a66597->max_root_hub)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002220 goto error;
2221 if (wLength != 0)
2222 goto error;
2223
2224 switch (wValue) {
2225 case USB_PORT_FEAT_SUSPEND:
2226 break;
2227 case USB_PORT_FEAT_POWER:
2228 r8a66597_port_power(r8a66597, port, 1);
2229 rh->port |= (1 << USB_PORT_FEAT_POWER);
2230 break;
2231 case USB_PORT_FEAT_RESET: {
2232 struct r8a66597_device *dev = rh->dev;
2233
2234 rh->port |= (1 << USB_PORT_FEAT_RESET);
2235
2236 disable_r8a66597_pipe_all(r8a66597, dev);
Yoshihiro Shimodad8359332010-03-16 12:29:35 +09002237 free_usb_address(r8a66597, dev, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002238
2239 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2240 get_dvstctr_reg(port));
2241 mod_timer(&r8a66597->rh_timer,
2242 jiffies + msecs_to_jiffies(50));
2243 }
2244 break;
2245 default:
2246 goto error;
2247 }
2248 rh->port |= 1 << wValue;
2249 break;
2250 default:
2251error:
2252 ret = -EPIPE;
2253 break;
2254 }
2255
2256 spin_unlock_irqrestore(&r8a66597->lock, flags);
2257 return ret;
2258}
2259
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002260#if defined(CONFIG_PM)
2261static int r8a66597_bus_suspend(struct usb_hcd *hcd)
2262{
2263 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2264 int port;
2265
2266 dbg("%s", __func__);
2267
Magnus Damm719a72b2009-07-17 14:59:55 +00002268 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002269 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2270 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2271
2272 if (!(rh->port & (1 << USB_PORT_FEAT_ENABLE)))
2273 continue;
2274
2275 dbg("suspend port = %d", port);
2276 r8a66597_bclr(r8a66597, UACT, dvstctr_reg); /* suspend */
2277 rh->port |= 1 << USB_PORT_FEAT_SUSPEND;
2278
2279 if (rh->dev->udev->do_remote_wakeup) {
2280 msleep(3); /* waiting last SOF */
2281 r8a66597_bset(r8a66597, RWUPE, dvstctr_reg);
2282 r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
2283 r8a66597_bset(r8a66597, BCHGE, get_intenb_reg(port));
2284 }
2285 }
2286
2287 r8a66597->bus_suspended = 1;
2288
2289 return 0;
2290}
2291
2292static int r8a66597_bus_resume(struct usb_hcd *hcd)
2293{
2294 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2295 int port;
2296
2297 dbg("%s", __func__);
2298
Magnus Damm719a72b2009-07-17 14:59:55 +00002299 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002300 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2301 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2302
2303 if (!(rh->port & (1 << USB_PORT_FEAT_SUSPEND)))
2304 continue;
2305
2306 dbg("resume port = %d", port);
2307 rh->port &= ~(1 << USB_PORT_FEAT_SUSPEND);
2308 rh->port |= 1 << USB_PORT_FEAT_C_SUSPEND;
2309 r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
2310 msleep(50);
2311 r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
2312 }
2313
2314 return 0;
2315
2316}
2317#else
2318#define r8a66597_bus_suspend NULL
2319#define r8a66597_bus_resume NULL
2320#endif
2321
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002322static struct hc_driver r8a66597_hc_driver = {
2323 .description = hcd_name,
2324 .hcd_priv_size = sizeof(struct r8a66597),
2325 .irq = r8a66597_irq,
2326
2327 /*
2328 * generic hardware linkage
2329 */
2330 .flags = HCD_USB2,
2331
2332 .start = r8a66597_start,
2333 .stop = r8a66597_stop,
2334
2335 /*
2336 * managing i/o requests and associated device resources
2337 */
2338 .urb_enqueue = r8a66597_urb_enqueue,
2339 .urb_dequeue = r8a66597_urb_dequeue,
2340 .endpoint_disable = r8a66597_endpoint_disable,
2341
2342 /*
2343 * periodic schedule support
2344 */
2345 .get_frame_number = r8a66597_get_frame,
2346
2347 /*
2348 * root hub support
2349 */
2350 .hub_status_data = r8a66597_hub_status_data,
2351 .hub_control = r8a66597_hub_control,
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002352 .bus_suspend = r8a66597_bus_suspend,
2353 .bus_resume = r8a66597_bus_resume,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002354};
2355
2356#if defined(CONFIG_PM)
Magnus Dammae1cef62009-07-17 14:52:05 +00002357static int r8a66597_suspend(struct device *dev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002358{
Magnus Dammae1cef62009-07-17 14:52:05 +00002359 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002360 int port;
2361
2362 dbg("%s", __func__);
2363
2364 disable_controller(r8a66597);
2365
Magnus Damm719a72b2009-07-17 14:59:55 +00002366 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002367 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2368
2369 rh->port = 0x00000000;
2370 }
2371
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002372 return 0;
2373}
2374
Magnus Dammae1cef62009-07-17 14:52:05 +00002375static int r8a66597_resume(struct device *dev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002376{
Magnus Dammae1cef62009-07-17 14:52:05 +00002377 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002378 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2379
2380 dbg("%s", __func__);
2381
2382 enable_controller(r8a66597);
2383 usb_root_hub_lost_power(hcd->self.root_hub);
2384
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002385 return 0;
2386}
Magnus Dammae1cef62009-07-17 14:52:05 +00002387
Alexey Dobriyan47145212009-12-14 18:00:08 -08002388static const struct dev_pm_ops r8a66597_dev_pm_ops = {
Magnus Dammae1cef62009-07-17 14:52:05 +00002389 .suspend = r8a66597_suspend,
2390 .resume = r8a66597_resume,
Yoshihiro Shimoda3725f282009-07-29 09:24:41 +00002391 .poweroff = r8a66597_suspend,
2392 .restore = r8a66597_resume,
Magnus Dammae1cef62009-07-17 14:52:05 +00002393};
2394
2395#define R8A66597_DEV_PM_OPS (&r8a66597_dev_pm_ops)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002396#else /* if defined(CONFIG_PM) */
Magnus Dammae1cef62009-07-17 14:52:05 +00002397#define R8A66597_DEV_PM_OPS NULL
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002398#endif
2399
2400static int __init_or_module r8a66597_remove(struct platform_device *pdev)
2401{
2402 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2403 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2404
2405 del_timer_sync(&r8a66597->rh_timer);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002406 usb_remove_hcd(hcd);
Yoshihiro Shimodaca0677a2007-10-05 15:53:12 +09002407 iounmap((void *)r8a66597->reg);
Magnus Damm719a72b2009-07-17 14:59:55 +00002408#ifdef CONFIG_HAVE_CLK
2409 if (r8a66597->pdata->on_chip)
2410 clk_put(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +09002411#endif
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002412 usb_put_hcd(hcd);
2413 return 0;
2414}
2415
Uwe Kleine-König8864bd82009-03-28 00:27:00 +01002416static int __devinit r8a66597_probe(struct platform_device *pdev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002417{
Magnus Damm719a72b2009-07-17 14:59:55 +00002418#ifdef CONFIG_HAVE_CLK
Magnus Damm765786e2008-10-31 20:22:38 +09002419 char clk_name[8];
2420#endif
Marc Zyngier27140212008-08-18 13:08:42 +02002421 struct resource *res = NULL, *ires;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002422 int irq = -1;
2423 void __iomem *reg = NULL;
2424 struct usb_hcd *hcd = NULL;
2425 struct r8a66597 *r8a66597;
2426 int ret = 0;
2427 int i;
Yoshihiro Shimoda0bf32b82008-06-27 19:09:55 +09002428 unsigned long irq_trigger;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002429
2430 if (pdev->dev.dma_mask) {
2431 ret = -EINVAL;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002432 dev_err(&pdev->dev, "dma not supported\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002433 goto clean_up;
2434 }
2435
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002436 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002437 if (!res) {
2438 ret = -ENODEV;
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002439 dev_err(&pdev->dev, "platform_get_resource error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002440 goto clean_up;
2441 }
2442
Marc Zyngier27140212008-08-18 13:08:42 +02002443 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2444 if (!ires) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002445 ret = -ENODEV;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002446 dev_err(&pdev->dev,
2447 "platform_get_resource IORESOURCE_IRQ error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002448 goto clean_up;
2449 }
2450
Marc Zyngier27140212008-08-18 13:08:42 +02002451 irq = ires->start;
2452 irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
2453
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002454 reg = ioremap(res->start, resource_size(res));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002455 if (reg == NULL) {
2456 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002457 dev_err(&pdev->dev, "ioremap error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002458 goto clean_up;
2459 }
2460
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +09002461 if (pdev->dev.platform_data == NULL) {
2462 dev_err(&pdev->dev, "no platform data\n");
2463 ret = -ENODEV;
2464 goto clean_up;
2465 }
2466
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002467 /* initialize hcd */
2468 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2469 if (!hcd) {
2470 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002471 dev_err(&pdev->dev, "Failed to create hcd\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002472 goto clean_up;
2473 }
2474 r8a66597 = hcd_to_r8a66597(hcd);
2475 memset(r8a66597, 0, sizeof(struct r8a66597));
2476 dev_set_drvdata(&pdev->dev, r8a66597);
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +09002477 r8a66597->pdata = pdev->dev.platform_data;
2478 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002479
Magnus Damm719a72b2009-07-17 14:59:55 +00002480 if (r8a66597->pdata->on_chip) {
2481#ifdef CONFIG_HAVE_CLK
2482 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
2483 r8a66597->clk = clk_get(&pdev->dev, clk_name);
2484 if (IS_ERR(r8a66597->clk)) {
2485 dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
2486 clk_name);
2487 ret = PTR_ERR(r8a66597->clk);
2488 goto clean_up2;
2489 }
Magnus Damm765786e2008-10-31 20:22:38 +09002490#endif
Magnus Damm719a72b2009-07-17 14:59:55 +00002491 r8a66597->max_root_hub = 1;
2492 } else
2493 r8a66597->max_root_hub = 2;
Magnus Damm765786e2008-10-31 20:22:38 +09002494
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002495 spin_lock_init(&r8a66597->lock);
2496 init_timer(&r8a66597->rh_timer);
2497 r8a66597->rh_timer.function = r8a66597_timer;
2498 r8a66597->rh_timer.data = (unsigned long)r8a66597;
2499 r8a66597->reg = (unsigned long)reg;
2500
Magnus Damme5ff15b2010-01-27 07:41:19 +00002501 /* make sure no interrupts are pending */
2502 ret = r8a66597_clock_enable(r8a66597);
2503 if (ret < 0)
2504 goto clean_up3;
2505 disable_controller(r8a66597);
2506
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002507 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2508 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2509 init_timer(&r8a66597->td_timer[i]);
2510 r8a66597->td_timer[i].function = r8a66597_td_timer;
2511 r8a66597->td_timer[i].data = (unsigned long)r8a66597;
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09002512 setup_timer(&r8a66597->interval_timer[i],
2513 r8a66597_interval_timer,
2514 (unsigned long)r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002515 }
2516 INIT_LIST_HEAD(&r8a66597->child_device);
2517
2518 hcd->rsrc_start = res->start;
Marc Zyngier27140212008-08-18 13:08:42 +02002519
Yoshihiro Shimoda0bf32b82008-06-27 19:09:55 +09002520 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002521 if (ret != 0) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002522 dev_err(&pdev->dev, "Failed to add hcd\n");
Magnus Damm765786e2008-10-31 20:22:38 +09002523 goto clean_up3;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002524 }
2525
2526 return 0;
2527
Magnus Damm765786e2008-10-31 20:22:38 +09002528clean_up3:
Magnus Damm719a72b2009-07-17 14:59:55 +00002529#ifdef CONFIG_HAVE_CLK
2530 if (r8a66597->pdata->on_chip)
2531 clk_put(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +09002532clean_up2:
Paul Mundtd6435102008-11-18 12:40:39 +09002533#endif
Magnus Damm765786e2008-10-31 20:22:38 +09002534 usb_put_hcd(hcd);
2535
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002536clean_up:
2537 if (reg)
2538 iounmap(reg);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002539
2540 return ret;
2541}
2542
2543static struct platform_driver r8a66597_driver = {
2544 .probe = r8a66597_probe,
2545 .remove = r8a66597_remove,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002546 .driver = {
2547 .name = (char *) hcd_name,
Kay Sieversf4fce612008-04-10 21:29:22 -07002548 .owner = THIS_MODULE,
Magnus Dammae1cef62009-07-17 14:52:05 +00002549 .pm = R8A66597_DEV_PM_OPS,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002550 },
2551};
2552
2553static int __init r8a66597_init(void)
2554{
2555 if (usb_disabled())
2556 return -ENODEV;
2557
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -07002558 printk(KERN_INFO KBUILD_MODNAME ": driver %s, %s\n", hcd_name,
2559 DRIVER_VERSION);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002560 return platform_driver_register(&r8a66597_driver);
2561}
2562module_init(r8a66597_init);
2563
2564static void __exit r8a66597_cleanup(void)
2565{
2566 platform_driver_unregister(&r8a66597_driver);
2567}
2568module_exit(r8a66597_cleanup);
2569