blob: 56976cc0352a9ae9e87a0a1cb681135410e0e581 [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>
29#include <linux/smp_lock.h>
30#include <linux/errno.h>
31#include <linux/init.h>
32#include <linux/timer.h>
33#include <linux/delay.h>
34#include <linux/list.h>
35#include <linux/interrupt.h>
36#include <linux/usb.h>
37#include <linux/platform_device.h>
Yoshihiro Shimodae2945312007-07-18 23:10:34 +090038#include <linux/io.h>
39#include <linux/irq.h>
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090040
41#include "../core/hcd.h"
42#include "r8a66597.h"
43
44MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
45MODULE_LICENSE("GPL");
46MODULE_AUTHOR("Yoshihiro Shimoda");
Kay Sieversf4fce612008-04-10 21:29:22 -070047MODULE_ALIAS("platform:r8a66597_hcd");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090048
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +090049#define DRIVER_VERSION "2009-05-26"
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090050
51static const char hcd_name[] = "r8a66597_hcd";
52
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090053static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
54static int r8a66597_get_frame(struct usb_hcd *hcd);
55
56/* this function must be called with interrupt disabled */
57static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
58 unsigned long reg)
59{
60 u16 tmp;
61
62 tmp = r8a66597_read(r8a66597, INTENB0);
63 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
64 r8a66597_bset(r8a66597, 1 << pipenum, reg);
65 r8a66597_write(r8a66597, tmp, INTENB0);
66}
67
68/* this function must be called with interrupt disabled */
69static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
70 unsigned long reg)
71{
72 u16 tmp;
73
74 tmp = r8a66597_read(r8a66597, INTENB0);
75 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
76 r8a66597_bclr(r8a66597, 1 << pipenum, reg);
77 r8a66597_write(r8a66597, tmp, INTENB0);
78}
79
80static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
81 u16 usbspd, u8 upphub, u8 hubport, int port)
82{
83 u16 val;
84 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
85
86 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
87 r8a66597_write(r8a66597, val, devadd_reg);
88}
89
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +090090static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090091{
92 u16 tmp;
93 int i = 0;
94
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +090095#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
Magnus Damm765786e2008-10-31 20:22:38 +090096#if defined(CONFIG_HAVE_CLK)
97 clk_enable(r8a66597->clk);
98#endif
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +090099 do {
100 r8a66597_write(r8a66597, SCKE, SYSCFG0);
101 tmp = r8a66597_read(r8a66597, SYSCFG0);
102 if (i++ > 1000) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700103 printk(KERN_ERR "r8a66597: register access fail.\n");
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900104 return -ENXIO;
105 }
106 } while ((tmp & SCKE) != SCKE);
107 r8a66597_write(r8a66597, 0x04, 0x02);
108#else
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900109 do {
110 r8a66597_write(r8a66597, USBE, SYSCFG0);
111 tmp = r8a66597_read(r8a66597, SYSCFG0);
112 if (i++ > 1000) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700113 printk(KERN_ERR "r8a66597: register access fail.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900114 return -ENXIO;
115 }
116 } while ((tmp & USBE) != USBE);
117 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +0900118 r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata), XTAL,
119 SYSCFG0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900120
121 i = 0;
122 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
123 do {
124 msleep(1);
125 tmp = r8a66597_read(r8a66597, SYSCFG0);
126 if (i++ > 500) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700127 printk(KERN_ERR "r8a66597: register access fail.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900128 return -ENXIO;
129 }
130 } while ((tmp & SCKE) != SCKE);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900131#endif /* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900132
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900133 return 0;
134}
135
136static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
137{
138 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
139 udelay(1);
Magnus Damm765786e2008-10-31 20:22:38 +0900140#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
141#if defined(CONFIG_HAVE_CLK)
142 clk_disable(r8a66597->clk);
143#endif
144#else
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900145 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
146 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
147 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
148#endif
149}
150
151static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
152{
153 u16 val;
154
155 val = port ? DRPD : DCFM | DRPD;
156 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
157 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
158
159 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
160 r8a66597_bclr(r8a66597, DTCHE, get_intenb_reg(port));
161 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
162}
163
164static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
165{
166 u16 val, tmp;
167
168 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
169 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
170
171 r8a66597_port_power(r8a66597, port, 0);
172
173 do {
174 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
175 udelay(640);
176 } while (tmp == EDGESTS);
177
178 val = port ? DRPD : DCFM | DRPD;
179 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
180 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
181}
182
183static int enable_controller(struct r8a66597 *r8a66597)
184{
185 int ret, port;
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +0900186 u16 vif = r8a66597->pdata->vif ? LDRV : 0;
187 u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
188 u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900189
190 ret = r8a66597_clock_enable(r8a66597);
191 if (ret < 0)
192 return ret;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900193
194 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900195 r8a66597_bset(r8a66597, USBE, SYSCFG0);
196
197 r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
198 r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
199 r8a66597_bset(r8a66597, BRDY0, BRDYENB);
200 r8a66597_bset(r8a66597, BEMP0, BEMPENB);
201
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900202 r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
203 r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
204 r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900205 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
206
207 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900208
209 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
210 r8a66597_enable_port(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900211
212 return 0;
213}
214
215static void disable_controller(struct r8a66597 *r8a66597)
216{
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900217 int port;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900218
219 r8a66597_write(r8a66597, 0, INTENB0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900220 r8a66597_write(r8a66597, 0, INTSTS0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900221
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900222 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
223 r8a66597_disable_port(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900224
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900225 r8a66597_clock_disable(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900226}
227
228static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
229 struct usb_device *udev)
230{
231 struct r8a66597_device *dev;
232
233 if (udev->parent && udev->parent->devnum != 1)
234 udev = udev->parent;
235
236 dev = dev_get_drvdata(&udev->dev);
237 if (dev)
238 return dev->address;
239 else
240 return 0;
241}
242
243static int is_child_device(char *devpath)
244{
245 return (devpath[2] ? 1 : 0);
246}
247
248static int is_hub_limit(char *devpath)
249{
250 return ((strlen(devpath) >= 4) ? 1 : 0);
251}
252
253static void get_port_number(char *devpath, u16 *root_port, u16 *hub_port)
254{
255 if (root_port) {
256 *root_port = (devpath[0] & 0x0F) - 1;
257 if (*root_port >= R8A66597_MAX_ROOT_HUB)
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700258 printk(KERN_ERR "r8a66597: Illegal root port number.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900259 }
260 if (hub_port)
261 *hub_port = devpath[2] & 0x0F;
262}
263
264static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
265{
266 u16 usbspd = 0;
267
268 switch (speed) {
269 case USB_SPEED_LOW:
270 usbspd = LSMODE;
271 break;
272 case USB_SPEED_FULL:
273 usbspd = FSMODE;
274 break;
275 case USB_SPEED_HIGH:
276 usbspd = HSMODE;
277 break;
278 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700279 printk(KERN_ERR "r8a66597: unknown speed\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900280 break;
281 }
282
283 return usbspd;
284}
285
286static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
287{
288 int idx;
289
290 idx = address / 32;
291 r8a66597->child_connect_map[idx] |= 1 << (address % 32);
292}
293
294static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
295{
296 int idx;
297
298 idx = address / 32;
299 r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
300}
301
302static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
303{
304 u16 pipenum = pipe->info.pipenum;
Ming Leife9b9032008-06-08 16:13:03 +0800305 const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
306 const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
307 const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900308
309 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
310 dma_ch = R8A66597_PIPE_NO_DMA;
311
312 pipe->fifoaddr = fifoaddr[dma_ch];
313 pipe->fifosel = fifosel[dma_ch];
314 pipe->fifoctr = fifoctr[dma_ch];
315
316 if (pipenum == 0)
317 pipe->pipectr = DCPCTR;
318 else
319 pipe->pipectr = get_pipectr_addr(pipenum);
320
321 if (check_bulk_or_isoc(pipenum)) {
322 pipe->pipetre = get_pipetre_addr(pipenum);
323 pipe->pipetrn = get_pipetrn_addr(pipenum);
324 } else {
325 pipe->pipetre = 0;
326 pipe->pipetrn = 0;
327 }
328}
329
330static struct r8a66597_device *
331get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
332{
333 if (usb_pipedevice(urb->pipe) == 0)
334 return &r8a66597->device0;
335
336 return dev_get_drvdata(&urb->dev->dev);
337}
338
339static int make_r8a66597_device(struct r8a66597 *r8a66597,
340 struct urb *urb, u8 addr)
341{
342 struct r8a66597_device *dev;
343 int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
344
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900345 dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900346 if (dev == NULL)
347 return -ENOMEM;
348
349 dev_set_drvdata(&urb->dev->dev, dev);
350 dev->udev = urb->dev;
351 dev->address = addr;
352 dev->usb_address = usb_address;
353 dev->state = USB_STATE_ADDRESS;
354 dev->ep_in_toggle = 0;
355 dev->ep_out_toggle = 0;
356 INIT_LIST_HEAD(&dev->device_list);
357 list_add_tail(&dev->device_list, &r8a66597->child_device);
358
359 get_port_number(urb->dev->devpath, &dev->root_port, &dev->hub_port);
360 if (!is_child_device(urb->dev->devpath))
361 r8a66597->root_hub[dev->root_port].dev = dev;
362
363 set_devadd_reg(r8a66597, dev->address,
364 get_r8a66597_usb_speed(urb->dev->speed),
365 get_parent_r8a66597_address(r8a66597, urb->dev),
366 dev->hub_port, dev->root_port);
367
368 return 0;
369}
370
371/* this function must be called with interrupt disabled */
372static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
373{
374 u8 addr; /* R8A66597's address */
375 struct r8a66597_device *dev;
376
377 if (is_hub_limit(urb->dev->devpath)) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700378 dev_err(&urb->dev->dev, "External hub limit reached.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900379 return 0;
380 }
381
382 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
383 if (dev && dev->state >= USB_STATE_ADDRESS)
384 return dev->address;
385
386 for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
387 if (r8a66597->address_map & (1 << addr))
388 continue;
389
390 dbg("alloc_address: r8a66597_addr=%d", addr);
391 r8a66597->address_map |= 1 << addr;
392
393 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
394 return 0;
395
396 return addr;
397 }
398
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700399 dev_err(&urb->dev->dev,
400 "cannot communicate with a USB device more than 10.(%x)\n",
401 r8a66597->address_map);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900402
403 return 0;
404}
405
406/* this function must be called with interrupt disabled */
407static void free_usb_address(struct r8a66597 *r8a66597,
408 struct r8a66597_device *dev)
409{
410 int port;
411
412 if (!dev)
413 return;
414
415 dbg("free_addr: addr=%d", dev->address);
416
417 dev->state = USB_STATE_DEFAULT;
418 r8a66597->address_map &= ~(1 << dev->address);
419 dev->address = 0;
420 dev_set_drvdata(&dev->udev->dev, NULL);
421 list_del(&dev->device_list);
422 kfree(dev);
423
424 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
425 if (r8a66597->root_hub[port].dev == dev) {
426 r8a66597->root_hub[port].dev = NULL;
427 break;
428 }
429 }
430}
431
432static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
433 u16 mask, u16 loop)
434{
435 u16 tmp;
436 int i = 0;
437
438 do {
439 tmp = r8a66597_read(r8a66597, reg);
440 if (i++ > 1000000) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700441 printk(KERN_ERR "r8a66597: register%lx, loop %x "
442 "is timeout\n", reg, loop);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900443 break;
444 }
445 ndelay(1);
446 } while ((tmp & mask) != loop);
447}
448
449/* this function must be called with interrupt disabled */
450static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
451{
452 u16 tmp;
453
454 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
455 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
456 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
457 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
458}
459
460/* this function must be called with interrupt disabled */
461static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
462{
463 u16 tmp;
464
465 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
466 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
467 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
468 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
469 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
470}
471
472/* this function must be called with interrupt disabled */
473static void clear_all_buffer(struct r8a66597 *r8a66597,
474 struct r8a66597_pipe *pipe)
475{
476 u16 tmp;
477
478 if (!pipe || pipe->info.pipenum == 0)
479 return;
480
481 pipe_stop(r8a66597, pipe);
482 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
483 tmp = r8a66597_read(r8a66597, pipe->pipectr);
484 tmp = r8a66597_read(r8a66597, pipe->pipectr);
485 tmp = r8a66597_read(r8a66597, pipe->pipectr);
486 r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
487}
488
489/* this function must be called with interrupt disabled */
490static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
491 struct r8a66597_pipe *pipe, int toggle)
492{
493 if (toggle)
494 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
495 else
496 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
497}
498
499/* this function must be called with interrupt disabled */
500static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
501{
502 r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
503 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
504}
505
506/* this function must be called with interrupt disabled */
507static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
508 struct r8a66597_pipe *pipe)
509{
510 cfifo_change(r8a66597, 0);
511 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D0FIFOSEL);
512 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D1FIFOSEL);
513
514 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum, MBW | CURPIPE,
515 pipe->fifosel);
516 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
517}
518
519static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
520{
521 struct r8a66597_pipe *pipe = hep->hcpriv;
522
523 if (usb_pipeendpoint(urb->pipe) == 0)
524 return 0;
525 else
526 return pipe->info.pipenum;
527}
528
529static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
530{
531 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
532
533 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
534}
535
536static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
537 int urb_pipe)
538{
539 if (!dev)
540 return NULL;
541
542 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
543}
544
545/* this function must be called with interrupt disabled */
546static void pipe_toggle_set(struct r8a66597 *r8a66597,
547 struct r8a66597_pipe *pipe,
548 struct urb *urb, int set)
549{
550 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
551 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
552 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
553
554 if (!toggle)
555 return;
556
557 if (set)
558 *toggle |= 1 << endpoint;
559 else
560 *toggle &= ~(1 << endpoint);
561}
562
563/* this function must be called with interrupt disabled */
564static void pipe_toggle_save(struct r8a66597 *r8a66597,
565 struct r8a66597_pipe *pipe,
566 struct urb *urb)
567{
568 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
569 pipe_toggle_set(r8a66597, pipe, urb, 1);
570 else
571 pipe_toggle_set(r8a66597, pipe, urb, 0);
572}
573
574/* this function must be called with interrupt disabled */
575static void pipe_toggle_restore(struct r8a66597 *r8a66597,
576 struct r8a66597_pipe *pipe,
577 struct urb *urb)
578{
579 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
580 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
581 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
582
Yoshihiro Shimodaf6ace2c2007-05-30 20:42:41 +0900583 if (!toggle)
584 return;
585
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900586 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
587}
588
589/* this function must be called with interrupt disabled */
590static void pipe_buffer_setting(struct r8a66597 *r8a66597,
591 struct r8a66597_pipe_info *info)
592{
593 u16 val = 0;
594
595 if (info->pipenum == 0)
596 return;
597
598 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
599 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
600 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
601 if (!info->dir_in)
602 val |= R8A66597_DIR;
603 if (info->type == R8A66597_BULK && info->dir_in)
604 val |= R8A66597_DBLB | R8A66597_SHTNAK;
605 val |= info->type | info->epnum;
606 r8a66597_write(r8a66597, val, PIPECFG);
607
608 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
609 PIPEBUF);
610 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
611 PIPEMAXP);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900612 r8a66597_write(r8a66597, info->interval, PIPEPERI);
613}
614
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900615/* this function must be called with interrupt disabled */
616static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
617{
618 struct r8a66597_pipe_info *info;
619 struct urb *urb = td->urb;
620
621 if (td->pipenum > 0) {
622 info = &td->pipe->info;
623 cfifo_change(r8a66597, 0);
624 pipe_buffer_setting(r8a66597, info);
625
626 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
627 usb_pipeout(urb->pipe)) &&
628 !usb_pipecontrol(urb->pipe)) {
629 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
630 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
631 clear_all_buffer(r8a66597, td->pipe);
632 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
633 usb_pipeout(urb->pipe), 1);
634 }
635 pipe_toggle_restore(r8a66597, td->pipe, urb);
636 }
637}
638
639/* this function must be called with interrupt disabled */
640static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
641 struct usb_endpoint_descriptor *ep)
642{
643 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
644
645 memset(array, 0, sizeof(array));
Julia Lawall2e0fe702008-12-29 11:22:14 +0100646 switch (usb_endpoint_type(ep)) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900647 case USB_ENDPOINT_XFER_BULK:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100648 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900649 array[i++] = 4;
650 else {
651 array[i++] = 3;
652 array[i++] = 5;
653 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900654 break;
655 case USB_ENDPOINT_XFER_INT:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100656 if (usb_endpoint_dir_in(ep)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900657 array[i++] = 6;
658 array[i++] = 7;
659 array[i++] = 8;
660 } else
661 array[i++] = 9;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900662 break;
663 case USB_ENDPOINT_XFER_ISOC:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100664 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900665 array[i++] = 2;
666 else
667 array[i++] = 1;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900668 break;
669 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700670 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900671 return 0;
672 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900673
674 i = 1;
675 min = array[0];
676 while (array[i] != 0) {
677 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
678 min = array[i];
679 i++;
680 }
681
682 return min;
683}
684
685static u16 get_r8a66597_type(__u8 type)
686{
687 u16 r8a66597_type;
688
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900689 switch (type) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900690 case USB_ENDPOINT_XFER_BULK:
691 r8a66597_type = R8A66597_BULK;
692 break;
693 case USB_ENDPOINT_XFER_INT:
694 r8a66597_type = R8A66597_INT;
695 break;
696 case USB_ENDPOINT_XFER_ISOC:
697 r8a66597_type = R8A66597_ISO;
698 break;
699 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700700 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900701 r8a66597_type = 0x0000;
702 break;
703 }
704
705 return r8a66597_type;
706}
707
708static u16 get_bufnum(u16 pipenum)
709{
710 u16 bufnum = 0;
711
712 if (pipenum == 0)
713 bufnum = 0;
714 else if (check_bulk_or_isoc(pipenum))
715 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
716 else if (check_interrupt(pipenum))
717 bufnum = 4 + (pipenum - 6);
718 else
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700719 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900720
721 return bufnum;
722}
723
724static u16 get_buf_bsize(u16 pipenum)
725{
726 u16 buf_bsize = 0;
727
728 if (pipenum == 0)
729 buf_bsize = 3;
730 else if (check_bulk_or_isoc(pipenum))
731 buf_bsize = R8A66597_BUF_BSIZE - 1;
732 else if (check_interrupt(pipenum))
733 buf_bsize = 0;
734 else
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700735 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900736
737 return buf_bsize;
738}
739
740/* this function must be called with interrupt disabled */
741static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
742 struct r8a66597_device *dev,
743 struct r8a66597_pipe *pipe,
744 struct urb *urb)
745{
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900746#if !defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900747 int i;
748 struct r8a66597_pipe_info *info = &pipe->info;
749
750 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
751 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
752 if ((r8a66597->dma_map & (1 << i)) != 0)
753 continue;
754
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -0700755 dev_info(&dev->udev->dev,
756 "address %d, EndpointAddress 0x%02x use "
757 "DMA FIFO\n", usb_pipedevice(urb->pipe),
758 info->dir_in ?
759 USB_ENDPOINT_DIR_MASK + info->epnum
760 : info->epnum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900761
762 r8a66597->dma_map |= 1 << i;
763 dev->dma_map |= 1 << i;
764 set_pipe_reg_addr(pipe, i);
765
766 cfifo_change(r8a66597, 0);
767 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum,
768 MBW | CURPIPE, pipe->fifosel);
769
770 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
771 pipe->info.pipenum);
772 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
773 break;
774 }
775 }
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900776#endif /* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900777}
778
779/* this function must be called with interrupt disabled */
780static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
781 struct usb_host_endpoint *hep,
782 struct r8a66597_pipe_info *info)
783{
784 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
785 struct r8a66597_pipe *pipe = hep->hcpriv;
786
787 dbg("enable_pipe:");
788
789 pipe->info = *info;
790 set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
791 r8a66597->pipe_cnt[pipe->info.pipenum]++;
792 dev->pipe_cnt[pipe->info.pipenum]++;
793
794 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
795}
796
797/* this function must be called with interrupt disabled */
798static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
799{
800 struct r8a66597_td *td, *next;
801 struct urb *urb;
802 struct list_head *list = &r8a66597->pipe_queue[pipenum];
803
804 if (list_empty(list))
805 return;
806
807 list_for_each_entry_safe(td, next, list, queue) {
808 if (!td)
809 continue;
810 if (td->address != address)
811 continue;
812
813 urb = td->urb;
814 list_del(&td->queue);
815 kfree(td);
816
817 if (urb) {
Alan Sterne9df41c2007-08-08 11:48:02 -0400818 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597),
819 urb);
820
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900821 spin_unlock(&r8a66597->lock);
Alan Stern4a000272007-08-24 15:42:24 -0400822 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb,
823 -ENODEV);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900824 spin_lock(&r8a66597->lock);
825 }
826 break;
827 }
828}
829
830/* this function must be called with interrupt disabled */
831static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
832 struct r8a66597_device *dev)
833{
834 int check_ep0 = 0;
835 u16 pipenum;
836
837 if (!dev)
838 return;
839
840 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
841 if (!dev->pipe_cnt[pipenum])
842 continue;
843
844 if (!check_ep0) {
845 check_ep0 = 1;
846 force_dequeue(r8a66597, 0, dev->address);
847 }
848
849 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
850 dev->pipe_cnt[pipenum] = 0;
851 force_dequeue(r8a66597, pipenum, dev->address);
852 }
853
854 dbg("disable_pipe");
855
856 r8a66597->dma_map &= ~(dev->dma_map);
857 dev->dma_map = 0;
858}
859
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900860static u16 get_interval(struct urb *urb, __u8 interval)
861{
862 u16 time = 1;
863 int i;
864
865 if (urb->dev->speed == USB_SPEED_HIGH) {
866 if (interval > IITV)
867 time = IITV;
868 else
869 time = interval ? interval - 1 : 0;
870 } else {
871 if (interval > 128) {
872 time = IITV;
873 } else {
874 /* calculate the nearest value for PIPEPERI */
875 for (i = 0; i < 7; i++) {
876 if ((1 << i) < interval &&
877 (1 << (i + 1) > interval))
878 time = 1 << i;
879 }
880 }
881 }
882
883 return time;
884}
885
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900886static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
887{
888 __u8 i;
889 unsigned long time = 1;
890
891 if (usb_pipeisoc(urb->pipe))
892 return 0;
893
894 if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
895 for (i = 0; i < (interval - 1); i++)
896 time *= 2;
897 time = time * 125 / 1000; /* uSOF -> msec */
898 } else {
899 time = interval;
900 }
901
902 return time;
903}
904
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900905/* this function must be called with interrupt disabled */
906static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
907 struct usb_host_endpoint *hep,
908 struct usb_endpoint_descriptor *ep)
909{
910 struct r8a66597_pipe_info info;
911
912 info.pipenum = get_empty_pipenum(r8a66597, ep);
913 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
Julia Lawall2e0fe702008-12-29 11:22:14 +0100914 info.epnum = usb_endpoint_num(ep);
Yoshihiro Shimoda05eac912007-10-03 18:53:28 +0900915 info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
Julia Lawall2e0fe702008-12-29 11:22:14 +0100916 info.type = get_r8a66597_type(usb_endpoint_type(ep));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900917 info.bufnum = get_bufnum(info.pipenum);
918 info.buf_bsize = get_buf_bsize(info.pipenum);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900919 if (info.type == R8A66597_BULK) {
920 info.interval = 0;
921 info.timer_interval = 0;
922 } else {
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900923 info.interval = get_interval(urb, ep->bInterval);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900924 info.timer_interval = get_timer_interval(urb, ep->bInterval);
925 }
Julia Lawall2e0fe702008-12-29 11:22:14 +0100926 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900927 info.dir_in = 1;
928 else
929 info.dir_in = 0;
930
931 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
932}
933
934static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
935{
936 struct r8a66597_device *dev;
937
938 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
939 dev->state = USB_STATE_CONFIGURED;
940}
941
942static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
943 u16 pipenum)
944{
945 if (pipenum == 0 && usb_pipeout(urb->pipe))
946 enable_irq_empty(r8a66597, pipenum);
947 else
948 enable_irq_ready(r8a66597, pipenum);
949
950 if (!usb_pipeisoc(urb->pipe))
951 enable_irq_nrdy(r8a66597, pipenum);
952}
953
954static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
955{
956 disable_irq_ready(r8a66597, pipenum);
957 disable_irq_nrdy(r8a66597, pipenum);
958}
959
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +0900960static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
961{
962 mod_timer(&r8a66597->rh_timer,
963 jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
964}
965
966static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
967 int connect)
968{
969 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
970
971 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
972 rh->scount = R8A66597_MAX_SAMPLING;
973 if (connect)
974 rh->port |= 1 << USB_PORT_FEAT_CONNECTION;
975 else
976 rh->port &= ~(1 << USB_PORT_FEAT_CONNECTION);
977 rh->port |= 1 << USB_PORT_FEAT_C_CONNECTION;
978
979 r8a66597_root_hub_start_polling(r8a66597);
980}
981
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900982/* this function must be called with interrupt disabled */
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +0900983static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
984 u16 syssts)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900985{
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +0900986 if (syssts == SE0) {
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +0900987 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +0900988 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
989 return;
990 }
991
992 if (syssts == FS_JSTS)
993 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
994 else if (syssts == LS_JSTS)
995 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
996
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900997 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900998 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +0900999
1000 if (r8a66597->bus_suspended)
1001 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001002}
1003
1004/* this function must be called with interrupt disabled */
1005static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
1006{
1007 u16 speed = get_rh_usb_speed(r8a66597, port);
1008 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1009
1010 if (speed == HSMODE)
1011 rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
1012 else if (speed == LSMODE)
1013 rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
1014
1015 rh->port &= ~(1 << USB_PORT_FEAT_RESET);
1016 rh->port |= 1 << USB_PORT_FEAT_ENABLE;
1017}
1018
1019/* this function must be called with interrupt disabled */
1020static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
1021{
1022 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
1023
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001024 disable_r8a66597_pipe_all(r8a66597, dev);
1025 free_usb_address(r8a66597, dev);
1026
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001027 start_root_hub_sampling(r8a66597, port, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001028}
1029
1030/* this function must be called with interrupt disabled */
1031static void prepare_setup_packet(struct r8a66597 *r8a66597,
1032 struct r8a66597_td *td)
1033{
1034 int i;
Al Virofd05e722008-04-28 07:00:16 +01001035 __le16 *p = (__le16 *)td->urb->setup_packet;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001036 unsigned long setup_addr = USBREQ;
1037
1038 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1039 DCPMAXP);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001040 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001041
1042 for (i = 0; i < 4; i++) {
Al Virofd05e722008-04-28 07:00:16 +01001043 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001044 setup_addr += 2;
1045 }
1046 r8a66597_write(r8a66597, SUREQ, DCPCTR);
1047}
1048
1049/* this function must be called with interrupt disabled */
1050static void prepare_packet_read(struct r8a66597 *r8a66597,
1051 struct r8a66597_td *td)
1052{
1053 struct urb *urb = td->urb;
1054
1055 if (usb_pipecontrol(urb->pipe)) {
1056 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1057 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1058 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1059 if (urb->actual_length == 0) {
1060 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1061 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1062 }
1063 pipe_irq_disable(r8a66597, td->pipenum);
1064 pipe_start(r8a66597, td->pipe);
1065 pipe_irq_enable(r8a66597, urb, td->pipenum);
1066 } else {
1067 if (urb->actual_length == 0) {
1068 pipe_irq_disable(r8a66597, td->pipenum);
1069 pipe_setting(r8a66597, td);
1070 pipe_stop(r8a66597, td->pipe);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001071 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001072
1073 if (td->pipe->pipetre) {
1074 r8a66597_write(r8a66597, TRCLR,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001075 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001076 r8a66597_write(r8a66597,
Julia Lawalldfa5ec72008-03-04 15:25:11 -08001077 DIV_ROUND_UP
1078 (urb->transfer_buffer_length,
1079 td->maxpacket),
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001080 td->pipe->pipetrn);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001081 r8a66597_bset(r8a66597, TRENB,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001082 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001083 }
1084
1085 pipe_start(r8a66597, td->pipe);
1086 pipe_irq_enable(r8a66597, urb, td->pipenum);
1087 }
1088 }
1089}
1090
1091/* this function must be called with interrupt disabled */
1092static void prepare_packet_write(struct r8a66597 *r8a66597,
1093 struct r8a66597_td *td)
1094{
1095 u16 tmp;
1096 struct urb *urb = td->urb;
1097
1098 if (usb_pipecontrol(urb->pipe)) {
1099 pipe_stop(r8a66597, td->pipe);
1100 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1101 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1102 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1103 if (urb->actual_length == 0) {
1104 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1105 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1106 }
1107 } else {
1108 if (urb->actual_length == 0)
1109 pipe_setting(r8a66597, td);
1110 if (td->pipe->pipetre)
1111 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1112 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001113 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001114
1115 fifo_change_from_pipe(r8a66597, td->pipe);
1116 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1117 if (unlikely((tmp & FRDY) == 0))
1118 pipe_irq_enable(r8a66597, urb, td->pipenum);
1119 else
1120 packet_write(r8a66597, td->pipenum);
1121 pipe_start(r8a66597, td->pipe);
1122}
1123
1124/* this function must be called with interrupt disabled */
1125static void prepare_status_packet(struct r8a66597 *r8a66597,
1126 struct r8a66597_td *td)
1127{
1128 struct urb *urb = td->urb;
1129
1130 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001131 pipe_stop(r8a66597, td->pipe);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001132
1133 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1134 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1135 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1136 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001137 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +09001138 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001139 enable_irq_empty(r8a66597, 0);
1140 } else {
1141 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1142 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1143 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1144 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001145 enable_irq_ready(r8a66597, 0);
1146 }
1147 enable_irq_nrdy(r8a66597, 0);
1148 pipe_start(r8a66597, td->pipe);
1149}
1150
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001151static int is_set_address(unsigned char *setup_packet)
1152{
1153 if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1154 setup_packet[1] == USB_REQ_SET_ADDRESS)
1155 return 1;
1156 else
1157 return 0;
1158}
1159
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001160/* this function must be called with interrupt disabled */
1161static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1162{
1163 BUG_ON(!td);
1164
1165 switch (td->type) {
1166 case USB_PID_SETUP:
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001167 if (is_set_address(td->urb->setup_packet)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001168 td->set_address = 1;
1169 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1170 td->urb);
1171 if (td->urb->setup_packet[2] == 0)
1172 return -EPIPE;
1173 }
1174 prepare_setup_packet(r8a66597, td);
1175 break;
1176 case USB_PID_IN:
1177 prepare_packet_read(r8a66597, td);
1178 break;
1179 case USB_PID_OUT:
1180 prepare_packet_write(r8a66597, td);
1181 break;
1182 case USB_PID_ACK:
1183 prepare_status_packet(r8a66597, td);
1184 break;
1185 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001186 printk(KERN_ERR "r8a66597: invalid type.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001187 break;
1188 }
1189
1190 return 0;
1191}
1192
1193static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1194{
1195 if (usb_pipeisoc(urb->pipe)) {
1196 if (urb->number_of_packets == td->iso_cnt)
1197 return 1;
1198 }
1199
1200 /* control or bulk or interrupt */
1201 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1202 (td->short_packet) || (td->zero_packet))
1203 return 1;
1204
1205 return 0;
1206}
1207
1208/* this function must be called with interrupt disabled */
1209static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1210{
1211 unsigned long time;
1212
1213 BUG_ON(!td);
1214
1215 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1216 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1217 r8a66597->timeout_map |= 1 << td->pipenum;
1218 switch (usb_pipetype(td->urb->pipe)) {
1219 case PIPE_INTERRUPT:
1220 case PIPE_ISOCHRONOUS:
1221 time = 30;
1222 break;
1223 default:
1224 time = 300;
1225 break;
1226 }
1227
1228 mod_timer(&r8a66597->td_timer[td->pipenum],
1229 jiffies + msecs_to_jiffies(time));
1230 }
1231}
1232
1233/* this function must be called with interrupt disabled */
Alan Sterndfd1e532007-08-21 15:36:52 -04001234static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
Alan Stern888fda42007-08-24 15:41:18 -04001235 u16 pipenum, struct urb *urb, int status)
Alan Sterndfd1e532007-08-21 15:36:52 -04001236__releases(r8a66597->lock) __acquires(r8a66597->lock)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001237{
1238 int restart = 0;
1239 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1240
1241 r8a66597->timeout_map &= ~(1 << pipenum);
1242
1243 if (likely(td)) {
Alan Stern888fda42007-08-24 15:41:18 -04001244 if (td->set_address && (status != 0 || urb->unlinked))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001245 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1246
1247 pipe_toggle_save(r8a66597, td->pipe, urb);
1248 list_del(&td->queue);
1249 kfree(td);
1250 }
1251
1252 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1253 restart = 1;
1254
1255 if (likely(urb)) {
1256 if (usb_pipeisoc(urb->pipe))
1257 urb->start_frame = r8a66597_get_frame(hcd);
1258
Alan Sterne9df41c2007-08-08 11:48:02 -04001259 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001260 spin_unlock(&r8a66597->lock);
Alan Stern4a000272007-08-24 15:42:24 -04001261 usb_hcd_giveback_urb(hcd, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001262 spin_lock(&r8a66597->lock);
1263 }
1264
1265 if (restart) {
1266 td = r8a66597_get_td(r8a66597, pipenum);
1267 if (unlikely(!td))
1268 return;
1269
1270 start_transfer(r8a66597, td);
1271 set_td_timer(r8a66597, td);
1272 }
1273}
1274
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001275static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1276{
1277 u16 tmp;
1278 int rcv_len, bufsize, urb_len, size;
1279 u16 *buf;
1280 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1281 struct urb *urb;
1282 int finish = 0;
Alan Sterndfd1e532007-08-21 15:36:52 -04001283 int status = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001284
1285 if (unlikely(!td))
1286 return;
1287 urb = td->urb;
1288
1289 fifo_change_from_pipe(r8a66597, td->pipe);
1290 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1291 if (unlikely((tmp & FRDY) == 0)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001292 pipe_stop(r8a66597, td->pipe);
1293 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001294 printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001295 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001296 return;
1297 }
1298
1299 /* prepare parameters */
1300 rcv_len = tmp & DTLN;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001301 if (usb_pipeisoc(urb->pipe)) {
1302 buf = (u16 *)(urb->transfer_buffer +
1303 urb->iso_frame_desc[td->iso_cnt].offset);
1304 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1305 } else {
1306 buf = (void *)urb->transfer_buffer + urb->actual_length;
1307 urb_len = urb->transfer_buffer_length - urb->actual_length;
1308 }
Alan Sterndfd1e532007-08-21 15:36:52 -04001309 bufsize = min(urb_len, (int) td->maxpacket);
1310 if (rcv_len <= bufsize) {
1311 size = rcv_len;
1312 } else {
1313 size = bufsize;
1314 status = -EOVERFLOW;
1315 finish = 1;
1316 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001317
1318 /* update parameters */
1319 urb->actual_length += size;
1320 if (rcv_len == 0)
1321 td->zero_packet = 1;
Alan Sterndfd1e532007-08-21 15:36:52 -04001322 if (rcv_len < bufsize) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001323 td->short_packet = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001324 }
1325 if (usb_pipeisoc(urb->pipe)) {
1326 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
Alan Sterndfd1e532007-08-21 15:36:52 -04001327 urb->iso_frame_desc[td->iso_cnt].status = status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001328 td->iso_cnt++;
Alan Sterndfd1e532007-08-21 15:36:52 -04001329 finish = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001330 }
1331
1332 /* check transfer finish */
Alan Sternb0d9efb2007-08-21 15:39:21 -04001333 if (finish || check_transfer_finish(td, urb)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001334 pipe_stop(r8a66597, td->pipe);
1335 pipe_irq_disable(r8a66597, pipenum);
1336 finish = 1;
1337 }
1338
1339 /* read fifo */
1340 if (urb->transfer_buffer) {
1341 if (size == 0)
1342 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1343 else
1344 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1345 buf, size);
1346 }
1347
Alan Stern888fda42007-08-24 15:41:18 -04001348 if (finish && pipenum != 0)
1349 finish_request(r8a66597, td, pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001350}
1351
1352static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1353{
1354 u16 tmp;
1355 int bufsize, size;
1356 u16 *buf;
1357 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1358 struct urb *urb;
1359
1360 if (unlikely(!td))
1361 return;
1362 urb = td->urb;
1363
1364 fifo_change_from_pipe(r8a66597, td->pipe);
1365 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1366 if (unlikely((tmp & FRDY) == 0)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001367 pipe_stop(r8a66597, td->pipe);
1368 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001369 printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001370 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001371 return;
1372 }
1373
1374 /* prepare parameters */
1375 bufsize = td->maxpacket;
1376 if (usb_pipeisoc(urb->pipe)) {
1377 buf = (u16 *)(urb->transfer_buffer +
1378 urb->iso_frame_desc[td->iso_cnt].offset);
1379 size = min(bufsize,
1380 (int)urb->iso_frame_desc[td->iso_cnt].length);
1381 } else {
1382 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
Greg Kroah-Hartman16e2e5f2009-03-03 16:44:13 -08001383 size = min_t(u32, bufsize,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001384 urb->transfer_buffer_length - urb->actual_length);
1385 }
1386
1387 /* write fifo */
1388 if (pipenum > 0)
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001389 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001390 if (urb->transfer_buffer) {
1391 r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
1392 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1393 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1394 }
1395
1396 /* update parameters */
1397 urb->actual_length += size;
1398 if (usb_pipeisoc(urb->pipe)) {
1399 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1400 urb->iso_frame_desc[td->iso_cnt].status = 0;
1401 td->iso_cnt++;
1402 }
1403
1404 /* check transfer finish */
1405 if (check_transfer_finish(td, urb)) {
1406 disable_irq_ready(r8a66597, pipenum);
1407 enable_irq_empty(r8a66597, pipenum);
1408 if (!usb_pipeisoc(urb->pipe))
1409 enable_irq_nrdy(r8a66597, pipenum);
1410 } else
1411 pipe_irq_enable(r8a66597, urb, pipenum);
1412}
1413
1414
Alan Stern888fda42007-08-24 15:41:18 -04001415static void check_next_phase(struct r8a66597 *r8a66597, int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001416{
1417 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1418 struct urb *urb;
1419 u8 finish = 0;
1420
1421 if (unlikely(!td))
1422 return;
1423 urb = td->urb;
1424
1425 switch (td->type) {
1426 case USB_PID_IN:
1427 case USB_PID_OUT:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001428 if (check_transfer_finish(td, urb))
1429 td->type = USB_PID_ACK;
1430 break;
1431 case USB_PID_SETUP:
Alan Sterneb231052007-08-21 15:40:36 -04001432 if (urb->transfer_buffer_length == urb->actual_length)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001433 td->type = USB_PID_ACK;
Alan Sterneb231052007-08-21 15:40:36 -04001434 else if (usb_pipeout(urb->pipe))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001435 td->type = USB_PID_OUT;
1436 else
1437 td->type = USB_PID_IN;
1438 break;
1439 case USB_PID_ACK:
1440 finish = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001441 break;
1442 }
1443
Alan Stern888fda42007-08-24 15:41:18 -04001444 if (finish || status != 0 || urb->unlinked)
1445 finish_request(r8a66597, td, 0, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001446 else
1447 start_transfer(r8a66597, td);
1448}
1449
Alan Stern888fda42007-08-24 15:41:18 -04001450static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001451{
1452 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1453
Alan Stern888fda42007-08-24 15:41:18 -04001454 if (td) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001455 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1456
1457 if (pid == PID_NAK)
Alan Stern888fda42007-08-24 15:41:18 -04001458 return -ECONNRESET;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001459 else
Alan Stern888fda42007-08-24 15:41:18 -04001460 return -EPIPE;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001461 }
Alan Stern888fda42007-08-24 15:41:18 -04001462 return 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001463}
1464
1465static void irq_pipe_ready(struct r8a66597 *r8a66597)
1466{
1467 u16 check;
1468 u16 pipenum;
1469 u16 mask;
1470 struct r8a66597_td *td;
1471
1472 mask = r8a66597_read(r8a66597, BRDYSTS)
1473 & r8a66597_read(r8a66597, BRDYENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001474 r8a66597_write(r8a66597, ~mask, BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001475 if (mask & BRDY0) {
1476 td = r8a66597_get_td(r8a66597, 0);
1477 if (td && td->type == USB_PID_IN)
1478 packet_read(r8a66597, 0);
1479 else
1480 pipe_irq_disable(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001481 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001482 }
1483
1484 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1485 check = 1 << pipenum;
1486 if (mask & check) {
1487 td = r8a66597_get_td(r8a66597, pipenum);
1488 if (unlikely(!td))
1489 continue;
1490
1491 if (td->type == USB_PID_IN)
1492 packet_read(r8a66597, pipenum);
1493 else if (td->type == USB_PID_OUT)
1494 packet_write(r8a66597, pipenum);
1495 }
1496 }
1497}
1498
1499static void irq_pipe_empty(struct r8a66597 *r8a66597)
1500{
1501 u16 tmp;
1502 u16 check;
1503 u16 pipenum;
1504 u16 mask;
1505 struct r8a66597_td *td;
1506
1507 mask = r8a66597_read(r8a66597, BEMPSTS)
1508 & r8a66597_read(r8a66597, BEMPENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001509 r8a66597_write(r8a66597, ~mask, BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001510 if (mask & BEMP0) {
1511 cfifo_change(r8a66597, 0);
1512 td = r8a66597_get_td(r8a66597, 0);
1513 if (td && td->type != USB_PID_OUT)
1514 disable_irq_empty(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001515 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001516 }
1517
1518 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1519 check = 1 << pipenum;
1520 if (mask & check) {
1521 struct r8a66597_td *td;
1522 td = r8a66597_get_td(r8a66597, pipenum);
1523 if (unlikely(!td))
1524 continue;
1525
1526 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1527 if ((tmp & INBUFM) == 0) {
1528 disable_irq_empty(r8a66597, pipenum);
1529 pipe_irq_disable(r8a66597, pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001530 finish_request(r8a66597, td, pipenum, td->urb,
1531 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001532 }
1533 }
1534 }
1535}
1536
1537static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1538{
1539 u16 check;
1540 u16 pipenum;
1541 u16 mask;
Alan Stern888fda42007-08-24 15:41:18 -04001542 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001543
1544 mask = r8a66597_read(r8a66597, NRDYSTS)
1545 & r8a66597_read(r8a66597, NRDYENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001546 r8a66597_write(r8a66597, ~mask, NRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001547 if (mask & NRDY0) {
1548 cfifo_change(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001549 status = get_urb_error(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001550 pipe_irq_disable(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001551 check_next_phase(r8a66597, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001552 }
1553
1554 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1555 check = 1 << pipenum;
1556 if (mask & check) {
1557 struct r8a66597_td *td;
1558 td = r8a66597_get_td(r8a66597, pipenum);
1559 if (unlikely(!td))
1560 continue;
1561
Alan Stern888fda42007-08-24 15:41:18 -04001562 status = get_urb_error(r8a66597, pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001563 pipe_irq_disable(r8a66597, pipenum);
1564 pipe_stop(r8a66597, td->pipe);
Alan Stern888fda42007-08-24 15:41:18 -04001565 finish_request(r8a66597, td, pipenum, td->urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001566 }
1567 }
1568}
1569
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001570static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1571{
1572 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1573 u16 intsts0, intsts1, intsts2;
1574 u16 intenb0, intenb1, intenb2;
1575 u16 mask0, mask1, mask2;
Alan Stern888fda42007-08-24 15:41:18 -04001576 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001577
1578 spin_lock(&r8a66597->lock);
1579
1580 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1581 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1582 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1583 intenb0 = r8a66597_read(r8a66597, INTENB0);
1584 intenb1 = r8a66597_read(r8a66597, INTENB1);
1585 intenb2 = r8a66597_read(r8a66597, INTENB2);
1586
1587 mask2 = intsts2 & intenb2;
1588 mask1 = intsts1 & intenb1;
1589 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1590 if (mask2) {
1591 if (mask2 & ATTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001592 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001593 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1594
1595 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001596 start_root_hub_sampling(r8a66597, 1, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001597 }
1598 if (mask2 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001599 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001600 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1601 r8a66597_usb_disconnect(r8a66597, 1);
1602 }
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09001603 if (mask2 & BCHG) {
1604 r8a66597_write(r8a66597, ~BCHG, INTSTS2);
1605 r8a66597_bclr(r8a66597, BCHGE, INTENB2);
1606 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1607 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001608 }
1609
1610 if (mask1) {
1611 if (mask1 & ATTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001612 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001613 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1614
1615 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001616 start_root_hub_sampling(r8a66597, 0, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001617 }
1618 if (mask1 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001619 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001620 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1621 r8a66597_usb_disconnect(r8a66597, 0);
1622 }
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09001623 if (mask1 & BCHG) {
1624 r8a66597_write(r8a66597, ~BCHG, INTSTS1);
1625 r8a66597_bclr(r8a66597, BCHGE, INTENB1);
1626 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1627 }
1628
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001629 if (mask1 & SIGN) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001630 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001631 status = get_urb_error(r8a66597, 0);
1632 check_next_phase(r8a66597, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001633 }
1634 if (mask1 & SACK) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001635 r8a66597_write(r8a66597, ~SACK, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001636 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001637 }
1638 }
1639 if (mask0) {
1640 if (mask0 & BRDY)
1641 irq_pipe_ready(r8a66597);
1642 if (mask0 & BEMP)
1643 irq_pipe_empty(r8a66597);
1644 if (mask0 & NRDY)
1645 irq_pipe_nrdy(r8a66597);
1646 }
1647
1648 spin_unlock(&r8a66597->lock);
1649 return IRQ_HANDLED;
1650}
1651
1652/* this function must be called with interrupt disabled */
1653static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1654{
1655 u16 tmp;
1656 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1657
1658 if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
1659 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1660
1661 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1662 if ((tmp & USBRST) == USBRST) {
1663 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1664 dvstctr_reg);
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001665 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001666 } else
1667 r8a66597_usb_connect(r8a66597, port);
1668 }
1669
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001670 if (!(rh->port & (1 << USB_PORT_FEAT_CONNECTION))) {
1671 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1672 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1673 }
1674
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001675 if (rh->scount > 0) {
1676 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1677 if (tmp == rh->old_syssts) {
1678 rh->scount--;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001679 if (rh->scount == 0)
1680 r8a66597_check_syssts(r8a66597, port, tmp);
1681 else
1682 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001683 } else {
1684 rh->scount = R8A66597_MAX_SAMPLING;
1685 rh->old_syssts = tmp;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001686 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001687 }
1688 }
1689}
1690
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001691static void r8a66597_interval_timer(unsigned long _r8a66597)
1692{
1693 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1694 unsigned long flags;
1695 u16 pipenum;
1696 struct r8a66597_td *td;
1697
1698 spin_lock_irqsave(&r8a66597->lock, flags);
1699
1700 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1701 if (!(r8a66597->interval_map & (1 << pipenum)))
1702 continue;
1703 if (timer_pending(&r8a66597->interval_timer[pipenum]))
1704 continue;
1705
1706 td = r8a66597_get_td(r8a66597, pipenum);
1707 if (td)
1708 start_transfer(r8a66597, td);
1709 }
1710
1711 spin_unlock_irqrestore(&r8a66597->lock, flags);
1712}
1713
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001714static void r8a66597_td_timer(unsigned long _r8a66597)
1715{
1716 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1717 unsigned long flags;
1718 u16 pipenum;
1719 struct r8a66597_td *td, *new_td = NULL;
1720 struct r8a66597_pipe *pipe;
1721
1722 spin_lock_irqsave(&r8a66597->lock, flags);
1723 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1724 if (!(r8a66597->timeout_map & (1 << pipenum)))
1725 continue;
1726 if (timer_pending(&r8a66597->td_timer[pipenum]))
1727 continue;
1728
1729 td = r8a66597_get_td(r8a66597, pipenum);
1730 if (!td) {
1731 r8a66597->timeout_map &= ~(1 << pipenum);
1732 continue;
1733 }
1734
1735 if (td->urb->actual_length) {
1736 set_td_timer(r8a66597, td);
1737 break;
1738 }
1739
1740 pipe = td->pipe;
1741 pipe_stop(r8a66597, pipe);
1742
1743 new_td = td;
1744 do {
1745 list_move_tail(&new_td->queue,
1746 &r8a66597->pipe_queue[pipenum]);
1747 new_td = r8a66597_get_td(r8a66597, pipenum);
1748 if (!new_td) {
1749 new_td = td;
1750 break;
1751 }
1752 } while (td != new_td && td->address == new_td->address);
1753
1754 start_transfer(r8a66597, new_td);
1755
1756 if (td == new_td)
1757 r8a66597->timeout_map &= ~(1 << pipenum);
1758 else
1759 set_td_timer(r8a66597, new_td);
1760 break;
1761 }
1762 spin_unlock_irqrestore(&r8a66597->lock, flags);
1763}
1764
1765static void r8a66597_timer(unsigned long _r8a66597)
1766{
1767 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1768 unsigned long flags;
Yoshihiro Shimoda58639642008-11-11 16:47:21 +09001769 int port;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001770
1771 spin_lock_irqsave(&r8a66597->lock, flags);
1772
Yoshihiro Shimoda58639642008-11-11 16:47:21 +09001773 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
1774 r8a66597_root_hub_control(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001775
1776 spin_unlock_irqrestore(&r8a66597->lock, flags);
1777}
1778
1779static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1780{
1781 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1782
1783 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1784 (urb->dev->state == USB_STATE_CONFIGURED))
1785 return 1;
1786 else
1787 return 0;
1788}
1789
1790static int r8a66597_start(struct usb_hcd *hcd)
1791{
1792 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001793
1794 hcd->state = HC_STATE_RUNNING;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001795 return enable_controller(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001796}
1797
1798static void r8a66597_stop(struct usb_hcd *hcd)
1799{
1800 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1801
1802 disable_controller(r8a66597);
1803}
1804
1805static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1806{
1807 unsigned int usb_address = usb_pipedevice(urb->pipe);
1808 u16 root_port, hub_port;
1809
1810 if (usb_address == 0) {
1811 get_port_number(urb->dev->devpath,
1812 &root_port, &hub_port);
1813 set_devadd_reg(r8a66597, 0,
1814 get_r8a66597_usb_speed(urb->dev->speed),
1815 get_parent_r8a66597_address(r8a66597, urb->dev),
1816 hub_port, root_port);
1817 }
1818}
1819
1820static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1821 struct urb *urb,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001822 struct usb_host_endpoint *hep)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001823{
1824 struct r8a66597_td *td;
1825 u16 pipenum;
1826
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001827 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001828 if (td == NULL)
1829 return NULL;
1830
1831 pipenum = r8a66597_get_pipenum(urb, hep);
1832 td->pipenum = pipenum;
1833 td->pipe = hep->hcpriv;
1834 td->urb = urb;
1835 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1836 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1837 !usb_pipein(urb->pipe));
1838 if (usb_pipecontrol(urb->pipe))
1839 td->type = USB_PID_SETUP;
1840 else if (usb_pipein(urb->pipe))
1841 td->type = USB_PID_IN;
1842 else
1843 td->type = USB_PID_OUT;
1844 INIT_LIST_HEAD(&td->queue);
1845
1846 return td;
1847}
1848
1849static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001850 struct urb *urb,
1851 gfp_t mem_flags)
1852{
Alan Sterne9df41c2007-08-08 11:48:02 -04001853 struct usb_host_endpoint *hep = urb->ep;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001854 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1855 struct r8a66597_td *td = NULL;
Alan Sterne9df41c2007-08-08 11:48:02 -04001856 int ret, request = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001857 unsigned long flags;
1858
1859 spin_lock_irqsave(&r8a66597->lock, flags);
1860 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1861 ret = -ENODEV;
Alan Sterne9df41c2007-08-08 11:48:02 -04001862 goto error_not_linked;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001863 }
1864
Alan Sterne9df41c2007-08-08 11:48:02 -04001865 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1866 if (ret)
1867 goto error_not_linked;
1868
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001869 if (!hep->hcpriv) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001870 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1871 GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001872 if (!hep->hcpriv) {
1873 ret = -ENOMEM;
1874 goto error;
1875 }
1876 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1877 if (usb_pipeendpoint(urb->pipe))
1878 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1879 }
1880
1881 if (unlikely(check_pipe_config(r8a66597, urb)))
1882 init_pipe_config(r8a66597, urb);
1883
1884 set_address_zero(r8a66597, urb);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001885 td = r8a66597_make_td(r8a66597, urb, hep);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001886 if (td == NULL) {
1887 ret = -ENOMEM;
1888 goto error;
1889 }
1890 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1891 request = 1;
1892 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001893 urb->hcpriv = td;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001894
1895 if (request) {
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001896 if (td->pipe->info.timer_interval) {
1897 r8a66597->interval_map |= 1 << td->pipenum;
1898 mod_timer(&r8a66597->interval_timer[td->pipenum],
1899 jiffies + msecs_to_jiffies(
1900 td->pipe->info.timer_interval));
1901 } else {
1902 ret = start_transfer(r8a66597, td);
1903 if (ret < 0) {
1904 list_del(&td->queue);
1905 kfree(td);
1906 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001907 }
1908 } else
1909 set_td_timer(r8a66597, td);
1910
1911error:
Alan Sterne9df41c2007-08-08 11:48:02 -04001912 if (ret)
1913 usb_hcd_unlink_urb_from_ep(hcd, urb);
1914error_not_linked:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001915 spin_unlock_irqrestore(&r8a66597->lock, flags);
1916 return ret;
1917}
1918
Alan Sterne9df41c2007-08-08 11:48:02 -04001919static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1920 int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001921{
1922 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1923 struct r8a66597_td *td;
1924 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001925 int rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001926
1927 spin_lock_irqsave(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001928 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1929 if (rc)
1930 goto done;
1931
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001932 if (urb->hcpriv) {
1933 td = urb->hcpriv;
1934 pipe_stop(r8a66597, td->pipe);
1935 pipe_irq_disable(r8a66597, td->pipenum);
1936 disable_irq_empty(r8a66597, td->pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001937 finish_request(r8a66597, td, td->pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001938 }
Alan Sterne9df41c2007-08-08 11:48:02 -04001939 done:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001940 spin_unlock_irqrestore(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001941 return rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001942}
1943
1944static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1945 struct usb_host_endpoint *hep)
1946{
1947 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1948 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1949 struct r8a66597_td *td;
1950 struct urb *urb = NULL;
1951 u16 pipenum;
1952 unsigned long flags;
1953
1954 if (pipe == NULL)
1955 return;
1956 pipenum = pipe->info.pipenum;
1957
1958 if (pipenum == 0) {
1959 kfree(hep->hcpriv);
1960 hep->hcpriv = NULL;
1961 return;
1962 }
1963
1964 spin_lock_irqsave(&r8a66597->lock, flags);
1965 pipe_stop(r8a66597, pipe);
1966 pipe_irq_disable(r8a66597, pipenum);
1967 disable_irq_empty(r8a66597, pipenum);
1968 td = r8a66597_get_td(r8a66597, pipenum);
1969 if (td)
1970 urb = td->urb;
Alan Stern888fda42007-08-24 15:41:18 -04001971 finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001972 kfree(hep->hcpriv);
1973 hep->hcpriv = NULL;
1974 spin_unlock_irqrestore(&r8a66597->lock, flags);
1975}
1976
1977static int r8a66597_get_frame(struct usb_hcd *hcd)
1978{
1979 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1980 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
1981}
1982
1983static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
1984{
1985 int chix;
1986
1987 if (udev->state == USB_STATE_CONFIGURED &&
1988 udev->parent && udev->parent->devnum > 1 &&
1989 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
1990 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
1991
1992 for (chix = 0; chix < udev->maxchild; chix++) {
1993 struct usb_device *childdev = udev->children[chix];
1994
1995 if (childdev)
1996 collect_usb_address_map(childdev, map);
1997 }
1998}
1999
2000/* this function must be called with interrupt disabled */
2001static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
2002 int addr)
2003{
2004 struct r8a66597_device *dev;
2005 struct list_head *list = &r8a66597->child_device;
2006
2007 list_for_each_entry(dev, list, device_list) {
2008 if (!dev)
2009 continue;
2010 if (dev->usb_address != addr)
2011 continue;
2012
2013 return dev;
2014 }
2015
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002016 printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002017 return NULL;
2018}
2019
2020static void update_usb_address_map(struct r8a66597 *r8a66597,
2021 struct usb_device *root_hub,
2022 unsigned long *map)
2023{
2024 int i, j, addr;
2025 unsigned long diff;
2026 unsigned long flags;
2027
2028 for (i = 0; i < 4; i++) {
2029 diff = r8a66597->child_connect_map[i] ^ map[i];
2030 if (!diff)
2031 continue;
2032
2033 for (j = 0; j < 32; j++) {
2034 if (!(diff & (1 << j)))
2035 continue;
2036
2037 addr = i * 32 + j;
2038 if (map[i] & (1 << j))
2039 set_child_connect_map(r8a66597, addr);
2040 else {
2041 struct r8a66597_device *dev;
2042
2043 spin_lock_irqsave(&r8a66597->lock, flags);
2044 dev = get_r8a66597_device(r8a66597, addr);
2045 disable_r8a66597_pipe_all(r8a66597, dev);
2046 free_usb_address(r8a66597, dev);
2047 put_child_connect_map(r8a66597, addr);
2048 spin_unlock_irqrestore(&r8a66597->lock, flags);
2049 }
2050 }
2051 }
2052}
2053
2054static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
2055 struct usb_hcd *hcd)
2056{
2057 struct usb_bus *bus;
2058 unsigned long now_map[4];
2059
2060 memset(now_map, 0, sizeof(now_map));
2061
2062 list_for_each_entry(bus, &usb_bus_list, bus_list) {
2063 if (!bus->root_hub)
2064 continue;
2065
2066 if (bus->busnum != hcd->self.busnum)
2067 continue;
2068
2069 collect_usb_address_map(bus->root_hub, now_map);
2070 update_usb_address_map(r8a66597, bus->root_hub, now_map);
2071 }
2072}
2073
2074static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2075{
2076 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2077 unsigned long flags;
2078 int i;
2079
2080 r8a66597_check_detect_child(r8a66597, hcd);
2081
2082 spin_lock_irqsave(&r8a66597->lock, flags);
2083
2084 *buf = 0; /* initialize (no change) */
2085
2086 for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++) {
2087 if (r8a66597->root_hub[i].port & 0xffff0000)
2088 *buf |= 1 << (i + 1);
2089 }
2090
2091 spin_unlock_irqrestore(&r8a66597->lock, flags);
2092
2093 return (*buf != 0);
2094}
2095
2096static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2097 struct usb_hub_descriptor *desc)
2098{
2099 desc->bDescriptorType = 0x29;
2100 desc->bHubContrCurrent = 0;
2101 desc->bNbrPorts = R8A66597_MAX_ROOT_HUB;
2102 desc->bDescLength = 9;
2103 desc->bPwrOn2PwrGood = 0;
2104 desc->wHubCharacteristics = cpu_to_le16(0x0011);
2105 desc->bitmap[0] = ((1 << R8A66597_MAX_ROOT_HUB) - 1) << 1;
2106 desc->bitmap[1] = ~0;
2107}
2108
2109static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2110 u16 wIndex, char *buf, u16 wLength)
2111{
2112 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2113 int ret;
2114 int port = (wIndex & 0x00FF) - 1;
2115 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2116 unsigned long flags;
2117
2118 ret = 0;
2119
2120 spin_lock_irqsave(&r8a66597->lock, flags);
2121 switch (typeReq) {
2122 case ClearHubFeature:
2123 case SetHubFeature:
2124 switch (wValue) {
2125 case C_HUB_OVER_CURRENT:
2126 case C_HUB_LOCAL_POWER:
2127 break;
2128 default:
2129 goto error;
2130 }
2131 break;
2132 case ClearPortFeature:
2133 if (wIndex > R8A66597_MAX_ROOT_HUB)
2134 goto error;
2135 if (wLength != 0)
2136 goto error;
2137
2138 switch (wValue) {
2139 case USB_PORT_FEAT_ENABLE:
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002140 rh->port &= ~(1 << USB_PORT_FEAT_POWER);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002141 break;
2142 case USB_PORT_FEAT_SUSPEND:
2143 break;
2144 case USB_PORT_FEAT_POWER:
2145 r8a66597_port_power(r8a66597, port, 0);
2146 break;
2147 case USB_PORT_FEAT_C_ENABLE:
2148 case USB_PORT_FEAT_C_SUSPEND:
2149 case USB_PORT_FEAT_C_CONNECTION:
2150 case USB_PORT_FEAT_C_OVER_CURRENT:
2151 case USB_PORT_FEAT_C_RESET:
2152 break;
2153 default:
2154 goto error;
2155 }
2156 rh->port &= ~(1 << wValue);
2157 break;
2158 case GetHubDescriptor:
2159 r8a66597_hub_descriptor(r8a66597,
2160 (struct usb_hub_descriptor *)buf);
2161 break;
2162 case GetHubStatus:
2163 *buf = 0x00;
2164 break;
2165 case GetPortStatus:
2166 if (wIndex > R8A66597_MAX_ROOT_HUB)
2167 goto error;
Al Virofd05e722008-04-28 07:00:16 +01002168 *(__le32 *)buf = cpu_to_le32(rh->port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002169 break;
2170 case SetPortFeature:
2171 if (wIndex > R8A66597_MAX_ROOT_HUB)
2172 goto error;
2173 if (wLength != 0)
2174 goto error;
2175
2176 switch (wValue) {
2177 case USB_PORT_FEAT_SUSPEND:
2178 break;
2179 case USB_PORT_FEAT_POWER:
2180 r8a66597_port_power(r8a66597, port, 1);
2181 rh->port |= (1 << USB_PORT_FEAT_POWER);
2182 break;
2183 case USB_PORT_FEAT_RESET: {
2184 struct r8a66597_device *dev = rh->dev;
2185
2186 rh->port |= (1 << USB_PORT_FEAT_RESET);
2187
2188 disable_r8a66597_pipe_all(r8a66597, dev);
2189 free_usb_address(r8a66597, dev);
2190
2191 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2192 get_dvstctr_reg(port));
2193 mod_timer(&r8a66597->rh_timer,
2194 jiffies + msecs_to_jiffies(50));
2195 }
2196 break;
2197 default:
2198 goto error;
2199 }
2200 rh->port |= 1 << wValue;
2201 break;
2202 default:
2203error:
2204 ret = -EPIPE;
2205 break;
2206 }
2207
2208 spin_unlock_irqrestore(&r8a66597->lock, flags);
2209 return ret;
2210}
2211
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002212#if defined(CONFIG_PM)
2213static int r8a66597_bus_suspend(struct usb_hcd *hcd)
2214{
2215 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2216 int port;
2217
2218 dbg("%s", __func__);
2219
2220 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
2221 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2222 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2223
2224 if (!(rh->port & (1 << USB_PORT_FEAT_ENABLE)))
2225 continue;
2226
2227 dbg("suspend port = %d", port);
2228 r8a66597_bclr(r8a66597, UACT, dvstctr_reg); /* suspend */
2229 rh->port |= 1 << USB_PORT_FEAT_SUSPEND;
2230
2231 if (rh->dev->udev->do_remote_wakeup) {
2232 msleep(3); /* waiting last SOF */
2233 r8a66597_bset(r8a66597, RWUPE, dvstctr_reg);
2234 r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
2235 r8a66597_bset(r8a66597, BCHGE, get_intenb_reg(port));
2236 }
2237 }
2238
2239 r8a66597->bus_suspended = 1;
2240
2241 return 0;
2242}
2243
2244static int r8a66597_bus_resume(struct usb_hcd *hcd)
2245{
2246 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2247 int port;
2248
2249 dbg("%s", __func__);
2250
2251 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
2252 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2253 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2254
2255 if (!(rh->port & (1 << USB_PORT_FEAT_SUSPEND)))
2256 continue;
2257
2258 dbg("resume port = %d", port);
2259 rh->port &= ~(1 << USB_PORT_FEAT_SUSPEND);
2260 rh->port |= 1 << USB_PORT_FEAT_C_SUSPEND;
2261 r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
2262 msleep(50);
2263 r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
2264 }
2265
2266 return 0;
2267
2268}
2269#else
2270#define r8a66597_bus_suspend NULL
2271#define r8a66597_bus_resume NULL
2272#endif
2273
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002274static struct hc_driver r8a66597_hc_driver = {
2275 .description = hcd_name,
2276 .hcd_priv_size = sizeof(struct r8a66597),
2277 .irq = r8a66597_irq,
2278
2279 /*
2280 * generic hardware linkage
2281 */
2282 .flags = HCD_USB2,
2283
2284 .start = r8a66597_start,
2285 .stop = r8a66597_stop,
2286
2287 /*
2288 * managing i/o requests and associated device resources
2289 */
2290 .urb_enqueue = r8a66597_urb_enqueue,
2291 .urb_dequeue = r8a66597_urb_dequeue,
2292 .endpoint_disable = r8a66597_endpoint_disable,
2293
2294 /*
2295 * periodic schedule support
2296 */
2297 .get_frame_number = r8a66597_get_frame,
2298
2299 /*
2300 * root hub support
2301 */
2302 .hub_status_data = r8a66597_hub_status_data,
2303 .hub_control = r8a66597_hub_control,
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002304 .bus_suspend = r8a66597_bus_suspend,
2305 .bus_resume = r8a66597_bus_resume,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002306};
2307
2308#if defined(CONFIG_PM)
2309static int r8a66597_suspend(struct platform_device *pdev, pm_message_t state)
2310{
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002311 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2312 int port;
2313
2314 dbg("%s", __func__);
2315
2316 disable_controller(r8a66597);
2317
2318 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
2319 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2320
2321 rh->port = 0x00000000;
2322 }
2323
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002324 return 0;
2325}
2326
2327static int r8a66597_resume(struct platform_device *pdev)
2328{
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002329 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2330 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2331
2332 dbg("%s", __func__);
2333
2334 enable_controller(r8a66597);
2335 usb_root_hub_lost_power(hcd->self.root_hub);
2336
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002337 return 0;
2338}
2339#else /* if defined(CONFIG_PM) */
2340#define r8a66597_suspend NULL
2341#define r8a66597_resume NULL
2342#endif
2343
2344static int __init_or_module r8a66597_remove(struct platform_device *pdev)
2345{
2346 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2347 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2348
2349 del_timer_sync(&r8a66597->rh_timer);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002350 usb_remove_hcd(hcd);
Yoshihiro Shimodaca0677a2007-10-05 15:53:12 +09002351 iounmap((void *)r8a66597->reg);
Magnus Damm765786e2008-10-31 20:22:38 +09002352#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2353 clk_put(r8a66597->clk);
2354#endif
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002355 usb_put_hcd(hcd);
2356 return 0;
2357}
2358
Uwe Kleine-König8864bd82009-03-28 00:27:00 +01002359static int __devinit r8a66597_probe(struct platform_device *pdev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002360{
Magnus Damm765786e2008-10-31 20:22:38 +09002361#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2362 char clk_name[8];
2363#endif
Marc Zyngier27140212008-08-18 13:08:42 +02002364 struct resource *res = NULL, *ires;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002365 int irq = -1;
2366 void __iomem *reg = NULL;
2367 struct usb_hcd *hcd = NULL;
2368 struct r8a66597 *r8a66597;
2369 int ret = 0;
2370 int i;
Yoshihiro Shimoda0bf32b82008-06-27 19:09:55 +09002371 unsigned long irq_trigger;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002372
2373 if (pdev->dev.dma_mask) {
2374 ret = -EINVAL;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002375 dev_err(&pdev->dev, "dma not supported\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002376 goto clean_up;
2377 }
2378
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002379 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002380 if (!res) {
2381 ret = -ENODEV;
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002382 dev_err(&pdev->dev, "platform_get_resource error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002383 goto clean_up;
2384 }
2385
Marc Zyngier27140212008-08-18 13:08:42 +02002386 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2387 if (!ires) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002388 ret = -ENODEV;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002389 dev_err(&pdev->dev,
2390 "platform_get_resource IORESOURCE_IRQ error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002391 goto clean_up;
2392 }
2393
Marc Zyngier27140212008-08-18 13:08:42 +02002394 irq = ires->start;
2395 irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
2396
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002397 reg = ioremap(res->start, resource_size(res));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002398 if (reg == NULL) {
2399 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002400 dev_err(&pdev->dev, "ioremap error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002401 goto clean_up;
2402 }
2403
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +09002404 if (pdev->dev.platform_data == NULL) {
2405 dev_err(&pdev->dev, "no platform data\n");
2406 ret = -ENODEV;
2407 goto clean_up;
2408 }
2409
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002410 /* initialize hcd */
2411 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2412 if (!hcd) {
2413 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002414 dev_err(&pdev->dev, "Failed to create hcd\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002415 goto clean_up;
2416 }
2417 r8a66597 = hcd_to_r8a66597(hcd);
2418 memset(r8a66597, 0, sizeof(struct r8a66597));
2419 dev_set_drvdata(&pdev->dev, r8a66597);
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +09002420 r8a66597->pdata = pdev->dev.platform_data;
2421 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002422
Magnus Damm765786e2008-10-31 20:22:38 +09002423#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2424 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
2425 r8a66597->clk = clk_get(&pdev->dev, clk_name);
2426 if (IS_ERR(r8a66597->clk)) {
2427 dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
2428 ret = PTR_ERR(r8a66597->clk);
2429 goto clean_up2;
2430 }
2431#endif
2432
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002433 spin_lock_init(&r8a66597->lock);
2434 init_timer(&r8a66597->rh_timer);
2435 r8a66597->rh_timer.function = r8a66597_timer;
2436 r8a66597->rh_timer.data = (unsigned long)r8a66597;
2437 r8a66597->reg = (unsigned long)reg;
2438
2439 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2440 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2441 init_timer(&r8a66597->td_timer[i]);
2442 r8a66597->td_timer[i].function = r8a66597_td_timer;
2443 r8a66597->td_timer[i].data = (unsigned long)r8a66597;
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09002444 setup_timer(&r8a66597->interval_timer[i],
2445 r8a66597_interval_timer,
2446 (unsigned long)r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002447 }
2448 INIT_LIST_HEAD(&r8a66597->child_device);
2449
2450 hcd->rsrc_start = res->start;
Marc Zyngier27140212008-08-18 13:08:42 +02002451
Yoshihiro Shimoda0bf32b82008-06-27 19:09:55 +09002452 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002453 if (ret != 0) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002454 dev_err(&pdev->dev, "Failed to add hcd\n");
Magnus Damm765786e2008-10-31 20:22:38 +09002455 goto clean_up3;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002456 }
2457
2458 return 0;
2459
Magnus Damm765786e2008-10-31 20:22:38 +09002460clean_up3:
2461#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2462 clk_put(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +09002463clean_up2:
Paul Mundtd6435102008-11-18 12:40:39 +09002464#endif
Magnus Damm765786e2008-10-31 20:22:38 +09002465 usb_put_hcd(hcd);
2466
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002467clean_up:
2468 if (reg)
2469 iounmap(reg);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002470
2471 return ret;
2472}
2473
2474static struct platform_driver r8a66597_driver = {
2475 .probe = r8a66597_probe,
2476 .remove = r8a66597_remove,
2477 .suspend = r8a66597_suspend,
2478 .resume = r8a66597_resume,
2479 .driver = {
2480 .name = (char *) hcd_name,
Kay Sieversf4fce612008-04-10 21:29:22 -07002481 .owner = THIS_MODULE,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002482 },
2483};
2484
2485static int __init r8a66597_init(void)
2486{
2487 if (usb_disabled())
2488 return -ENODEV;
2489
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -07002490 printk(KERN_INFO KBUILD_MODNAME ": driver %s, %s\n", hcd_name,
2491 DRIVER_VERSION);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002492 return platform_driver_register(&r8a66597_driver);
2493}
2494module_init(r8a66597_init);
2495
2496static void __exit r8a66597_cleanup(void)
2497{
2498 platform_driver_unregister(&r8a66597_driver);
2499}
2500module_exit(r8a66597_cleanup);
2501