blob: 713f4cf0b0dd18d74d11d9a790bf108bd95fe5fe [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 Shimoda6d879102008-04-10 21:05:47 +090049#define DRIVER_VERSION "10 Apr 2008"
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090050
51static const char hcd_name[] = "r8a66597_hcd";
52
53/* module parameters */
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +090054#if !defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090055static unsigned short clock = XTAL12;
56module_param(clock, ushort, 0644);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +090057MODULE_PARM_DESC(clock, "input clock: 48MHz=32768, 24MHz=16384, 12MHz=0 "
58 "(default=0)");
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +090059#endif
Yoshihiro Shimodae2945312007-07-18 23:10:34 +090060
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090061static unsigned short vif = LDRV;
62module_param(vif, ushort, 0644);
63MODULE_PARM_DESC(vif, "input VIF: 3.3V=32768, 1.5V=0(default=32768)");
Yoshihiro Shimodae2945312007-07-18 23:10:34 +090064
65static unsigned short endian;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090066module_param(endian, ushort, 0644);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +090067MODULE_PARM_DESC(endian, "data endian: big=256, little=0 (default=0)");
68
Marc Zyngier27140212008-08-18 13:08:42 +020069static unsigned short irq_sense = 0xff;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090070module_param(irq_sense, ushort, 0644);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +090071MODULE_PARM_DESC(irq_sense, "IRQ sense: low level=32, falling edge=0 "
72 "(default=32)");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090073
74static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
75static int r8a66597_get_frame(struct usb_hcd *hcd);
76
77/* this function must be called with interrupt disabled */
78static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
79 unsigned long reg)
80{
81 u16 tmp;
82
83 tmp = r8a66597_read(r8a66597, INTENB0);
84 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
85 r8a66597_bset(r8a66597, 1 << pipenum, reg);
86 r8a66597_write(r8a66597, tmp, INTENB0);
87}
88
89/* this function must be called with interrupt disabled */
90static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
91 unsigned long reg)
92{
93 u16 tmp;
94
95 tmp = r8a66597_read(r8a66597, INTENB0);
96 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
97 r8a66597_bclr(r8a66597, 1 << pipenum, reg);
98 r8a66597_write(r8a66597, tmp, INTENB0);
99}
100
101static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
102 u16 usbspd, u8 upphub, u8 hubport, int port)
103{
104 u16 val;
105 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
106
107 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
108 r8a66597_write(r8a66597, val, devadd_reg);
109}
110
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900111static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900112{
113 u16 tmp;
114 int i = 0;
115
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900116#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
Magnus Damm765786e2008-10-31 20:22:38 +0900117#if defined(CONFIG_HAVE_CLK)
118 clk_enable(r8a66597->clk);
119#endif
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900120 do {
121 r8a66597_write(r8a66597, SCKE, SYSCFG0);
122 tmp = r8a66597_read(r8a66597, SYSCFG0);
123 if (i++ > 1000) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700124 printk(KERN_ERR "r8a66597: register access fail.\n");
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900125 return -ENXIO;
126 }
127 } while ((tmp & SCKE) != SCKE);
128 r8a66597_write(r8a66597, 0x04, 0x02);
129#else
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900130 do {
131 r8a66597_write(r8a66597, USBE, SYSCFG0);
132 tmp = r8a66597_read(r8a66597, SYSCFG0);
133 if (i++ > 1000) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700134 printk(KERN_ERR "r8a66597: register access fail.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900135 return -ENXIO;
136 }
137 } while ((tmp & USBE) != USBE);
138 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
139 r8a66597_mdfy(r8a66597, clock, XTAL, SYSCFG0);
140
141 i = 0;
142 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
143 do {
144 msleep(1);
145 tmp = r8a66597_read(r8a66597, SYSCFG0);
146 if (i++ > 500) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700147 printk(KERN_ERR "r8a66597: register access fail.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900148 return -ENXIO;
149 }
150 } while ((tmp & SCKE) != SCKE);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900151#endif /* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900152
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900153 return 0;
154}
155
156static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
157{
158 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
159 udelay(1);
Magnus Damm765786e2008-10-31 20:22:38 +0900160#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
161#if defined(CONFIG_HAVE_CLK)
162 clk_disable(r8a66597->clk);
163#endif
164#else
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900165 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
166 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
167 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
168#endif
169}
170
171static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
172{
173 u16 val;
174
175 val = port ? DRPD : DCFM | DRPD;
176 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
177 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
178
179 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
180 r8a66597_bclr(r8a66597, DTCHE, get_intenb_reg(port));
181 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
182}
183
184static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
185{
186 u16 val, tmp;
187
188 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
189 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
190
191 r8a66597_port_power(r8a66597, port, 0);
192
193 do {
194 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
195 udelay(640);
196 } while (tmp == EDGESTS);
197
198 val = port ? DRPD : DCFM | DRPD;
199 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
200 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
201}
202
203static int enable_controller(struct r8a66597 *r8a66597)
204{
205 int ret, port;
206
207 ret = r8a66597_clock_enable(r8a66597);
208 if (ret < 0)
209 return ret;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900210
211 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900212 r8a66597_bset(r8a66597, USBE, SYSCFG0);
213
214 r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
215 r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
216 r8a66597_bset(r8a66597, BRDY0, BRDYENB);
217 r8a66597_bset(r8a66597, BEMP0, BEMPENB);
218
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900219 r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
220 r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
221 r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900222 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
223
224 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900225
226 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
227 r8a66597_enable_port(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900228
229 return 0;
230}
231
232static void disable_controller(struct r8a66597 *r8a66597)
233{
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900234 int port;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900235
236 r8a66597_write(r8a66597, 0, INTENB0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900237 r8a66597_write(r8a66597, 0, INTSTS0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900238
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900239 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
240 r8a66597_disable_port(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900241
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900242 r8a66597_clock_disable(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900243}
244
245static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
246 struct usb_device *udev)
247{
248 struct r8a66597_device *dev;
249
250 if (udev->parent && udev->parent->devnum != 1)
251 udev = udev->parent;
252
253 dev = dev_get_drvdata(&udev->dev);
254 if (dev)
255 return dev->address;
256 else
257 return 0;
258}
259
260static int is_child_device(char *devpath)
261{
262 return (devpath[2] ? 1 : 0);
263}
264
265static int is_hub_limit(char *devpath)
266{
267 return ((strlen(devpath) >= 4) ? 1 : 0);
268}
269
270static void get_port_number(char *devpath, u16 *root_port, u16 *hub_port)
271{
272 if (root_port) {
273 *root_port = (devpath[0] & 0x0F) - 1;
274 if (*root_port >= R8A66597_MAX_ROOT_HUB)
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700275 printk(KERN_ERR "r8a66597: Illegal root port number.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900276 }
277 if (hub_port)
278 *hub_port = devpath[2] & 0x0F;
279}
280
281static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
282{
283 u16 usbspd = 0;
284
285 switch (speed) {
286 case USB_SPEED_LOW:
287 usbspd = LSMODE;
288 break;
289 case USB_SPEED_FULL:
290 usbspd = FSMODE;
291 break;
292 case USB_SPEED_HIGH:
293 usbspd = HSMODE;
294 break;
295 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700296 printk(KERN_ERR "r8a66597: unknown speed\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900297 break;
298 }
299
300 return usbspd;
301}
302
303static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
304{
305 int idx;
306
307 idx = address / 32;
308 r8a66597->child_connect_map[idx] |= 1 << (address % 32);
309}
310
311static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
312{
313 int idx;
314
315 idx = address / 32;
316 r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
317}
318
319static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
320{
321 u16 pipenum = pipe->info.pipenum;
Ming Leife9b9032008-06-08 16:13:03 +0800322 const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
323 const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
324 const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900325
326 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
327 dma_ch = R8A66597_PIPE_NO_DMA;
328
329 pipe->fifoaddr = fifoaddr[dma_ch];
330 pipe->fifosel = fifosel[dma_ch];
331 pipe->fifoctr = fifoctr[dma_ch];
332
333 if (pipenum == 0)
334 pipe->pipectr = DCPCTR;
335 else
336 pipe->pipectr = get_pipectr_addr(pipenum);
337
338 if (check_bulk_or_isoc(pipenum)) {
339 pipe->pipetre = get_pipetre_addr(pipenum);
340 pipe->pipetrn = get_pipetrn_addr(pipenum);
341 } else {
342 pipe->pipetre = 0;
343 pipe->pipetrn = 0;
344 }
345}
346
347static struct r8a66597_device *
348get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
349{
350 if (usb_pipedevice(urb->pipe) == 0)
351 return &r8a66597->device0;
352
353 return dev_get_drvdata(&urb->dev->dev);
354}
355
356static int make_r8a66597_device(struct r8a66597 *r8a66597,
357 struct urb *urb, u8 addr)
358{
359 struct r8a66597_device *dev;
360 int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
361
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900362 dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900363 if (dev == NULL)
364 return -ENOMEM;
365
366 dev_set_drvdata(&urb->dev->dev, dev);
367 dev->udev = urb->dev;
368 dev->address = addr;
369 dev->usb_address = usb_address;
370 dev->state = USB_STATE_ADDRESS;
371 dev->ep_in_toggle = 0;
372 dev->ep_out_toggle = 0;
373 INIT_LIST_HEAD(&dev->device_list);
374 list_add_tail(&dev->device_list, &r8a66597->child_device);
375
376 get_port_number(urb->dev->devpath, &dev->root_port, &dev->hub_port);
377 if (!is_child_device(urb->dev->devpath))
378 r8a66597->root_hub[dev->root_port].dev = dev;
379
380 set_devadd_reg(r8a66597, dev->address,
381 get_r8a66597_usb_speed(urb->dev->speed),
382 get_parent_r8a66597_address(r8a66597, urb->dev),
383 dev->hub_port, dev->root_port);
384
385 return 0;
386}
387
388/* this function must be called with interrupt disabled */
389static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
390{
391 u8 addr; /* R8A66597's address */
392 struct r8a66597_device *dev;
393
394 if (is_hub_limit(urb->dev->devpath)) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700395 dev_err(&urb->dev->dev, "External hub limit reached.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900396 return 0;
397 }
398
399 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
400 if (dev && dev->state >= USB_STATE_ADDRESS)
401 return dev->address;
402
403 for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
404 if (r8a66597->address_map & (1 << addr))
405 continue;
406
407 dbg("alloc_address: r8a66597_addr=%d", addr);
408 r8a66597->address_map |= 1 << addr;
409
410 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
411 return 0;
412
413 return addr;
414 }
415
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700416 dev_err(&urb->dev->dev,
417 "cannot communicate with a USB device more than 10.(%x)\n",
418 r8a66597->address_map);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900419
420 return 0;
421}
422
423/* this function must be called with interrupt disabled */
424static void free_usb_address(struct r8a66597 *r8a66597,
425 struct r8a66597_device *dev)
426{
427 int port;
428
429 if (!dev)
430 return;
431
432 dbg("free_addr: addr=%d", dev->address);
433
434 dev->state = USB_STATE_DEFAULT;
435 r8a66597->address_map &= ~(1 << dev->address);
436 dev->address = 0;
437 dev_set_drvdata(&dev->udev->dev, NULL);
438 list_del(&dev->device_list);
439 kfree(dev);
440
441 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
442 if (r8a66597->root_hub[port].dev == dev) {
443 r8a66597->root_hub[port].dev = NULL;
444 break;
445 }
446 }
447}
448
449static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
450 u16 mask, u16 loop)
451{
452 u16 tmp;
453 int i = 0;
454
455 do {
456 tmp = r8a66597_read(r8a66597, reg);
457 if (i++ > 1000000) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700458 printk(KERN_ERR "r8a66597: register%lx, loop %x "
459 "is timeout\n", reg, loop);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900460 break;
461 }
462 ndelay(1);
463 } while ((tmp & mask) != loop);
464}
465
466/* this function must be called with interrupt disabled */
467static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
468{
469 u16 tmp;
470
471 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
472 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
473 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
474 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
475}
476
477/* this function must be called with interrupt disabled */
478static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
479{
480 u16 tmp;
481
482 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
483 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
484 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
485 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
486 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
487}
488
489/* this function must be called with interrupt disabled */
490static void clear_all_buffer(struct r8a66597 *r8a66597,
491 struct r8a66597_pipe *pipe)
492{
493 u16 tmp;
494
495 if (!pipe || pipe->info.pipenum == 0)
496 return;
497
498 pipe_stop(r8a66597, pipe);
499 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
500 tmp = r8a66597_read(r8a66597, pipe->pipectr);
501 tmp = r8a66597_read(r8a66597, pipe->pipectr);
502 tmp = r8a66597_read(r8a66597, pipe->pipectr);
503 r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
504}
505
506/* this function must be called with interrupt disabled */
507static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
508 struct r8a66597_pipe *pipe, int toggle)
509{
510 if (toggle)
511 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
512 else
513 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
514}
515
516/* this function must be called with interrupt disabled */
517static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
518{
519 r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
520 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
521}
522
523/* this function must be called with interrupt disabled */
524static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
525 struct r8a66597_pipe *pipe)
526{
527 cfifo_change(r8a66597, 0);
528 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D0FIFOSEL);
529 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D1FIFOSEL);
530
531 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum, MBW | CURPIPE,
532 pipe->fifosel);
533 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
534}
535
536static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
537{
538 struct r8a66597_pipe *pipe = hep->hcpriv;
539
540 if (usb_pipeendpoint(urb->pipe) == 0)
541 return 0;
542 else
543 return pipe->info.pipenum;
544}
545
546static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
547{
548 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
549
550 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
551}
552
553static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
554 int urb_pipe)
555{
556 if (!dev)
557 return NULL;
558
559 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
560}
561
562/* this function must be called with interrupt disabled */
563static void pipe_toggle_set(struct r8a66597 *r8a66597,
564 struct r8a66597_pipe *pipe,
565 struct urb *urb, int set)
566{
567 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
568 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
569 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
570
571 if (!toggle)
572 return;
573
574 if (set)
575 *toggle |= 1 << endpoint;
576 else
577 *toggle &= ~(1 << endpoint);
578}
579
580/* this function must be called with interrupt disabled */
581static void pipe_toggle_save(struct r8a66597 *r8a66597,
582 struct r8a66597_pipe *pipe,
583 struct urb *urb)
584{
585 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
586 pipe_toggle_set(r8a66597, pipe, urb, 1);
587 else
588 pipe_toggle_set(r8a66597, pipe, urb, 0);
589}
590
591/* this function must be called with interrupt disabled */
592static void pipe_toggle_restore(struct r8a66597 *r8a66597,
593 struct r8a66597_pipe *pipe,
594 struct urb *urb)
595{
596 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
597 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
598 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
599
Yoshihiro Shimodaf6ace2c2007-05-30 20:42:41 +0900600 if (!toggle)
601 return;
602
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900603 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
604}
605
606/* this function must be called with interrupt disabled */
607static void pipe_buffer_setting(struct r8a66597 *r8a66597,
608 struct r8a66597_pipe_info *info)
609{
610 u16 val = 0;
611
612 if (info->pipenum == 0)
613 return;
614
615 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
616 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
617 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
618 if (!info->dir_in)
619 val |= R8A66597_DIR;
620 if (info->type == R8A66597_BULK && info->dir_in)
621 val |= R8A66597_DBLB | R8A66597_SHTNAK;
622 val |= info->type | info->epnum;
623 r8a66597_write(r8a66597, val, PIPECFG);
624
625 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
626 PIPEBUF);
627 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
628 PIPEMAXP);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900629 r8a66597_write(r8a66597, info->interval, PIPEPERI);
630}
631
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900632/* this function must be called with interrupt disabled */
633static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
634{
635 struct r8a66597_pipe_info *info;
636 struct urb *urb = td->urb;
637
638 if (td->pipenum > 0) {
639 info = &td->pipe->info;
640 cfifo_change(r8a66597, 0);
641 pipe_buffer_setting(r8a66597, info);
642
643 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
644 usb_pipeout(urb->pipe)) &&
645 !usb_pipecontrol(urb->pipe)) {
646 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
647 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
648 clear_all_buffer(r8a66597, td->pipe);
649 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
650 usb_pipeout(urb->pipe), 1);
651 }
652 pipe_toggle_restore(r8a66597, td->pipe, urb);
653 }
654}
655
656/* this function must be called with interrupt disabled */
657static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
658 struct usb_endpoint_descriptor *ep)
659{
660 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
661
662 memset(array, 0, sizeof(array));
Julia Lawall2e0fe702008-12-29 11:22:14 +0100663 switch (usb_endpoint_type(ep)) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900664 case USB_ENDPOINT_XFER_BULK:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100665 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900666 array[i++] = 4;
667 else {
668 array[i++] = 3;
669 array[i++] = 5;
670 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900671 break;
672 case USB_ENDPOINT_XFER_INT:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100673 if (usb_endpoint_dir_in(ep)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900674 array[i++] = 6;
675 array[i++] = 7;
676 array[i++] = 8;
677 } else
678 array[i++] = 9;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900679 break;
680 case USB_ENDPOINT_XFER_ISOC:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100681 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900682 array[i++] = 2;
683 else
684 array[i++] = 1;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900685 break;
686 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700687 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900688 return 0;
689 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900690
691 i = 1;
692 min = array[0];
693 while (array[i] != 0) {
694 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
695 min = array[i];
696 i++;
697 }
698
699 return min;
700}
701
702static u16 get_r8a66597_type(__u8 type)
703{
704 u16 r8a66597_type;
705
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900706 switch (type) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900707 case USB_ENDPOINT_XFER_BULK:
708 r8a66597_type = R8A66597_BULK;
709 break;
710 case USB_ENDPOINT_XFER_INT:
711 r8a66597_type = R8A66597_INT;
712 break;
713 case USB_ENDPOINT_XFER_ISOC:
714 r8a66597_type = R8A66597_ISO;
715 break;
716 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700717 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900718 r8a66597_type = 0x0000;
719 break;
720 }
721
722 return r8a66597_type;
723}
724
725static u16 get_bufnum(u16 pipenum)
726{
727 u16 bufnum = 0;
728
729 if (pipenum == 0)
730 bufnum = 0;
731 else if (check_bulk_or_isoc(pipenum))
732 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
733 else if (check_interrupt(pipenum))
734 bufnum = 4 + (pipenum - 6);
735 else
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700736 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900737
738 return bufnum;
739}
740
741static u16 get_buf_bsize(u16 pipenum)
742{
743 u16 buf_bsize = 0;
744
745 if (pipenum == 0)
746 buf_bsize = 3;
747 else if (check_bulk_or_isoc(pipenum))
748 buf_bsize = R8A66597_BUF_BSIZE - 1;
749 else if (check_interrupt(pipenum))
750 buf_bsize = 0;
751 else
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700752 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900753
754 return buf_bsize;
755}
756
757/* this function must be called with interrupt disabled */
758static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
759 struct r8a66597_device *dev,
760 struct r8a66597_pipe *pipe,
761 struct urb *urb)
762{
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900763#if !defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900764 int i;
765 struct r8a66597_pipe_info *info = &pipe->info;
766
767 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
768 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
769 if ((r8a66597->dma_map & (1 << i)) != 0)
770 continue;
771
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -0700772 dev_info(&dev->udev->dev,
773 "address %d, EndpointAddress 0x%02x use "
774 "DMA FIFO\n", usb_pipedevice(urb->pipe),
775 info->dir_in ?
776 USB_ENDPOINT_DIR_MASK + info->epnum
777 : info->epnum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900778
779 r8a66597->dma_map |= 1 << i;
780 dev->dma_map |= 1 << i;
781 set_pipe_reg_addr(pipe, i);
782
783 cfifo_change(r8a66597, 0);
784 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum,
785 MBW | CURPIPE, pipe->fifosel);
786
787 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
788 pipe->info.pipenum);
789 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
790 break;
791 }
792 }
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900793#endif /* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900794}
795
796/* this function must be called with interrupt disabled */
797static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
798 struct usb_host_endpoint *hep,
799 struct r8a66597_pipe_info *info)
800{
801 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
802 struct r8a66597_pipe *pipe = hep->hcpriv;
803
804 dbg("enable_pipe:");
805
806 pipe->info = *info;
807 set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
808 r8a66597->pipe_cnt[pipe->info.pipenum]++;
809 dev->pipe_cnt[pipe->info.pipenum]++;
810
811 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
812}
813
814/* this function must be called with interrupt disabled */
815static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
816{
817 struct r8a66597_td *td, *next;
818 struct urb *urb;
819 struct list_head *list = &r8a66597->pipe_queue[pipenum];
820
821 if (list_empty(list))
822 return;
823
824 list_for_each_entry_safe(td, next, list, queue) {
825 if (!td)
826 continue;
827 if (td->address != address)
828 continue;
829
830 urb = td->urb;
831 list_del(&td->queue);
832 kfree(td);
833
834 if (urb) {
Alan Sterne9df41c2007-08-08 11:48:02 -0400835 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597),
836 urb);
837
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900838 spin_unlock(&r8a66597->lock);
Alan Stern4a000272007-08-24 15:42:24 -0400839 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb,
840 -ENODEV);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900841 spin_lock(&r8a66597->lock);
842 }
843 break;
844 }
845}
846
847/* this function must be called with interrupt disabled */
848static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
849 struct r8a66597_device *dev)
850{
851 int check_ep0 = 0;
852 u16 pipenum;
853
854 if (!dev)
855 return;
856
857 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
858 if (!dev->pipe_cnt[pipenum])
859 continue;
860
861 if (!check_ep0) {
862 check_ep0 = 1;
863 force_dequeue(r8a66597, 0, dev->address);
864 }
865
866 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
867 dev->pipe_cnt[pipenum] = 0;
868 force_dequeue(r8a66597, pipenum, dev->address);
869 }
870
871 dbg("disable_pipe");
872
873 r8a66597->dma_map &= ~(dev->dma_map);
874 dev->dma_map = 0;
875}
876
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900877static u16 get_interval(struct urb *urb, __u8 interval)
878{
879 u16 time = 1;
880 int i;
881
882 if (urb->dev->speed == USB_SPEED_HIGH) {
883 if (interval > IITV)
884 time = IITV;
885 else
886 time = interval ? interval - 1 : 0;
887 } else {
888 if (interval > 128) {
889 time = IITV;
890 } else {
891 /* calculate the nearest value for PIPEPERI */
892 for (i = 0; i < 7; i++) {
893 if ((1 << i) < interval &&
894 (1 << (i + 1) > interval))
895 time = 1 << i;
896 }
897 }
898 }
899
900 return time;
901}
902
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900903static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
904{
905 __u8 i;
906 unsigned long time = 1;
907
908 if (usb_pipeisoc(urb->pipe))
909 return 0;
910
911 if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
912 for (i = 0; i < (interval - 1); i++)
913 time *= 2;
914 time = time * 125 / 1000; /* uSOF -> msec */
915 } else {
916 time = interval;
917 }
918
919 return time;
920}
921
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900922/* this function must be called with interrupt disabled */
923static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
924 struct usb_host_endpoint *hep,
925 struct usb_endpoint_descriptor *ep)
926{
927 struct r8a66597_pipe_info info;
928
929 info.pipenum = get_empty_pipenum(r8a66597, ep);
930 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
Julia Lawall2e0fe702008-12-29 11:22:14 +0100931 info.epnum = usb_endpoint_num(ep);
Yoshihiro Shimoda05eac912007-10-03 18:53:28 +0900932 info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
Julia Lawall2e0fe702008-12-29 11:22:14 +0100933 info.type = get_r8a66597_type(usb_endpoint_type(ep));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900934 info.bufnum = get_bufnum(info.pipenum);
935 info.buf_bsize = get_buf_bsize(info.pipenum);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900936 if (info.type == R8A66597_BULK) {
937 info.interval = 0;
938 info.timer_interval = 0;
939 } else {
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900940 info.interval = get_interval(urb, ep->bInterval);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900941 info.timer_interval = get_timer_interval(urb, ep->bInterval);
942 }
Julia Lawall2e0fe702008-12-29 11:22:14 +0100943 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900944 info.dir_in = 1;
945 else
946 info.dir_in = 0;
947
948 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
949}
950
951static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
952{
953 struct r8a66597_device *dev;
954
955 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
956 dev->state = USB_STATE_CONFIGURED;
957}
958
959static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
960 u16 pipenum)
961{
962 if (pipenum == 0 && usb_pipeout(urb->pipe))
963 enable_irq_empty(r8a66597, pipenum);
964 else
965 enable_irq_ready(r8a66597, pipenum);
966
967 if (!usb_pipeisoc(urb->pipe))
968 enable_irq_nrdy(r8a66597, pipenum);
969}
970
971static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
972{
973 disable_irq_ready(r8a66597, pipenum);
974 disable_irq_nrdy(r8a66597, pipenum);
975}
976
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +0900977static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
978{
979 mod_timer(&r8a66597->rh_timer,
980 jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
981}
982
983static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
984 int connect)
985{
986 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
987
988 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
989 rh->scount = R8A66597_MAX_SAMPLING;
990 if (connect)
991 rh->port |= 1 << USB_PORT_FEAT_CONNECTION;
992 else
993 rh->port &= ~(1 << USB_PORT_FEAT_CONNECTION);
994 rh->port |= 1 << USB_PORT_FEAT_C_CONNECTION;
995
996 r8a66597_root_hub_start_polling(r8a66597);
997}
998
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900999/* this function must be called with interrupt disabled */
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001000static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
1001 u16 syssts)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001002{
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001003 if (syssts == SE0) {
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001004 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001005 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1006 return;
1007 }
1008
1009 if (syssts == FS_JSTS)
1010 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
1011 else if (syssts == LS_JSTS)
1012 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
1013
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001014 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001015 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
1016}
1017
1018/* this function must be called with interrupt disabled */
1019static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
1020{
1021 u16 speed = get_rh_usb_speed(r8a66597, port);
1022 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1023
1024 if (speed == HSMODE)
1025 rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
1026 else if (speed == LSMODE)
1027 rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
1028
1029 rh->port &= ~(1 << USB_PORT_FEAT_RESET);
1030 rh->port |= 1 << USB_PORT_FEAT_ENABLE;
1031}
1032
1033/* this function must be called with interrupt disabled */
1034static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
1035{
1036 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
1037
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001038 disable_r8a66597_pipe_all(r8a66597, dev);
1039 free_usb_address(r8a66597, dev);
1040
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001041 start_root_hub_sampling(r8a66597, port, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001042}
1043
1044/* this function must be called with interrupt disabled */
1045static void prepare_setup_packet(struct r8a66597 *r8a66597,
1046 struct r8a66597_td *td)
1047{
1048 int i;
Al Virofd05e722008-04-28 07:00:16 +01001049 __le16 *p = (__le16 *)td->urb->setup_packet;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001050 unsigned long setup_addr = USBREQ;
1051
1052 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1053 DCPMAXP);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001054 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001055
1056 for (i = 0; i < 4; i++) {
Al Virofd05e722008-04-28 07:00:16 +01001057 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001058 setup_addr += 2;
1059 }
1060 r8a66597_write(r8a66597, SUREQ, DCPCTR);
1061}
1062
1063/* this function must be called with interrupt disabled */
1064static void prepare_packet_read(struct r8a66597 *r8a66597,
1065 struct r8a66597_td *td)
1066{
1067 struct urb *urb = td->urb;
1068
1069 if (usb_pipecontrol(urb->pipe)) {
1070 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1071 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1072 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1073 if (urb->actual_length == 0) {
1074 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1075 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1076 }
1077 pipe_irq_disable(r8a66597, td->pipenum);
1078 pipe_start(r8a66597, td->pipe);
1079 pipe_irq_enable(r8a66597, urb, td->pipenum);
1080 } else {
1081 if (urb->actual_length == 0) {
1082 pipe_irq_disable(r8a66597, td->pipenum);
1083 pipe_setting(r8a66597, td);
1084 pipe_stop(r8a66597, td->pipe);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001085 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001086
1087 if (td->pipe->pipetre) {
1088 r8a66597_write(r8a66597, TRCLR,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001089 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001090 r8a66597_write(r8a66597,
Julia Lawalldfa5ec72008-03-04 15:25:11 -08001091 DIV_ROUND_UP
1092 (urb->transfer_buffer_length,
1093 td->maxpacket),
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001094 td->pipe->pipetrn);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001095 r8a66597_bset(r8a66597, TRENB,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001096 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001097 }
1098
1099 pipe_start(r8a66597, td->pipe);
1100 pipe_irq_enable(r8a66597, urb, td->pipenum);
1101 }
1102 }
1103}
1104
1105/* this function must be called with interrupt disabled */
1106static void prepare_packet_write(struct r8a66597 *r8a66597,
1107 struct r8a66597_td *td)
1108{
1109 u16 tmp;
1110 struct urb *urb = td->urb;
1111
1112 if (usb_pipecontrol(urb->pipe)) {
1113 pipe_stop(r8a66597, td->pipe);
1114 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1115 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1116 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1117 if (urb->actual_length == 0) {
1118 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1119 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1120 }
1121 } else {
1122 if (urb->actual_length == 0)
1123 pipe_setting(r8a66597, td);
1124 if (td->pipe->pipetre)
1125 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1126 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001127 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001128
1129 fifo_change_from_pipe(r8a66597, td->pipe);
1130 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1131 if (unlikely((tmp & FRDY) == 0))
1132 pipe_irq_enable(r8a66597, urb, td->pipenum);
1133 else
1134 packet_write(r8a66597, td->pipenum);
1135 pipe_start(r8a66597, td->pipe);
1136}
1137
1138/* this function must be called with interrupt disabled */
1139static void prepare_status_packet(struct r8a66597 *r8a66597,
1140 struct r8a66597_td *td)
1141{
1142 struct urb *urb = td->urb;
1143
1144 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001145 pipe_stop(r8a66597, td->pipe);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001146
1147 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1148 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1149 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1150 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001151 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +09001152 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001153 enable_irq_empty(r8a66597, 0);
1154 } else {
1155 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1156 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1157 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1158 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001159 enable_irq_ready(r8a66597, 0);
1160 }
1161 enable_irq_nrdy(r8a66597, 0);
1162 pipe_start(r8a66597, td->pipe);
1163}
1164
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001165static int is_set_address(unsigned char *setup_packet)
1166{
1167 if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1168 setup_packet[1] == USB_REQ_SET_ADDRESS)
1169 return 1;
1170 else
1171 return 0;
1172}
1173
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001174/* this function must be called with interrupt disabled */
1175static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1176{
1177 BUG_ON(!td);
1178
1179 switch (td->type) {
1180 case USB_PID_SETUP:
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001181 if (is_set_address(td->urb->setup_packet)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001182 td->set_address = 1;
1183 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1184 td->urb);
1185 if (td->urb->setup_packet[2] == 0)
1186 return -EPIPE;
1187 }
1188 prepare_setup_packet(r8a66597, td);
1189 break;
1190 case USB_PID_IN:
1191 prepare_packet_read(r8a66597, td);
1192 break;
1193 case USB_PID_OUT:
1194 prepare_packet_write(r8a66597, td);
1195 break;
1196 case USB_PID_ACK:
1197 prepare_status_packet(r8a66597, td);
1198 break;
1199 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001200 printk(KERN_ERR "r8a66597: invalid type.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001201 break;
1202 }
1203
1204 return 0;
1205}
1206
1207static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1208{
1209 if (usb_pipeisoc(urb->pipe)) {
1210 if (urb->number_of_packets == td->iso_cnt)
1211 return 1;
1212 }
1213
1214 /* control or bulk or interrupt */
1215 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1216 (td->short_packet) || (td->zero_packet))
1217 return 1;
1218
1219 return 0;
1220}
1221
1222/* this function must be called with interrupt disabled */
1223static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1224{
1225 unsigned long time;
1226
1227 BUG_ON(!td);
1228
1229 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1230 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1231 r8a66597->timeout_map |= 1 << td->pipenum;
1232 switch (usb_pipetype(td->urb->pipe)) {
1233 case PIPE_INTERRUPT:
1234 case PIPE_ISOCHRONOUS:
1235 time = 30;
1236 break;
1237 default:
1238 time = 300;
1239 break;
1240 }
1241
1242 mod_timer(&r8a66597->td_timer[td->pipenum],
1243 jiffies + msecs_to_jiffies(time));
1244 }
1245}
1246
1247/* this function must be called with interrupt disabled */
Alan Sterndfd1e532007-08-21 15:36:52 -04001248static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
Alan Stern888fda42007-08-24 15:41:18 -04001249 u16 pipenum, struct urb *urb, int status)
Alan Sterndfd1e532007-08-21 15:36:52 -04001250__releases(r8a66597->lock) __acquires(r8a66597->lock)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001251{
1252 int restart = 0;
1253 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1254
1255 r8a66597->timeout_map &= ~(1 << pipenum);
1256
1257 if (likely(td)) {
Alan Stern888fda42007-08-24 15:41:18 -04001258 if (td->set_address && (status != 0 || urb->unlinked))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001259 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1260
1261 pipe_toggle_save(r8a66597, td->pipe, urb);
1262 list_del(&td->queue);
1263 kfree(td);
1264 }
1265
1266 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1267 restart = 1;
1268
1269 if (likely(urb)) {
1270 if (usb_pipeisoc(urb->pipe))
1271 urb->start_frame = r8a66597_get_frame(hcd);
1272
Alan Sterne9df41c2007-08-08 11:48:02 -04001273 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001274 spin_unlock(&r8a66597->lock);
Alan Stern4a000272007-08-24 15:42:24 -04001275 usb_hcd_giveback_urb(hcd, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001276 spin_lock(&r8a66597->lock);
1277 }
1278
1279 if (restart) {
1280 td = r8a66597_get_td(r8a66597, pipenum);
1281 if (unlikely(!td))
1282 return;
1283
1284 start_transfer(r8a66597, td);
1285 set_td_timer(r8a66597, td);
1286 }
1287}
1288
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001289static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1290{
1291 u16 tmp;
1292 int rcv_len, bufsize, urb_len, size;
1293 u16 *buf;
1294 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1295 struct urb *urb;
1296 int finish = 0;
Alan Sterndfd1e532007-08-21 15:36:52 -04001297 int status = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001298
1299 if (unlikely(!td))
1300 return;
1301 urb = td->urb;
1302
1303 fifo_change_from_pipe(r8a66597, td->pipe);
1304 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1305 if (unlikely((tmp & FRDY) == 0)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001306 pipe_stop(r8a66597, td->pipe);
1307 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001308 printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001309 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001310 return;
1311 }
1312
1313 /* prepare parameters */
1314 rcv_len = tmp & DTLN;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001315 if (usb_pipeisoc(urb->pipe)) {
1316 buf = (u16 *)(urb->transfer_buffer +
1317 urb->iso_frame_desc[td->iso_cnt].offset);
1318 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1319 } else {
1320 buf = (void *)urb->transfer_buffer + urb->actual_length;
1321 urb_len = urb->transfer_buffer_length - urb->actual_length;
1322 }
Alan Sterndfd1e532007-08-21 15:36:52 -04001323 bufsize = min(urb_len, (int) td->maxpacket);
1324 if (rcv_len <= bufsize) {
1325 size = rcv_len;
1326 } else {
1327 size = bufsize;
1328 status = -EOVERFLOW;
1329 finish = 1;
1330 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001331
1332 /* update parameters */
1333 urb->actual_length += size;
1334 if (rcv_len == 0)
1335 td->zero_packet = 1;
Alan Sterndfd1e532007-08-21 15:36:52 -04001336 if (rcv_len < bufsize) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001337 td->short_packet = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001338 }
1339 if (usb_pipeisoc(urb->pipe)) {
1340 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
Alan Sterndfd1e532007-08-21 15:36:52 -04001341 urb->iso_frame_desc[td->iso_cnt].status = status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001342 td->iso_cnt++;
Alan Sterndfd1e532007-08-21 15:36:52 -04001343 finish = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001344 }
1345
1346 /* check transfer finish */
Alan Sternb0d9efb2007-08-21 15:39:21 -04001347 if (finish || check_transfer_finish(td, urb)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001348 pipe_stop(r8a66597, td->pipe);
1349 pipe_irq_disable(r8a66597, pipenum);
1350 finish = 1;
1351 }
1352
1353 /* read fifo */
1354 if (urb->transfer_buffer) {
1355 if (size == 0)
1356 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1357 else
1358 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1359 buf, size);
1360 }
1361
Alan Stern888fda42007-08-24 15:41:18 -04001362 if (finish && pipenum != 0)
1363 finish_request(r8a66597, td, pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001364}
1365
1366static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1367{
1368 u16 tmp;
1369 int bufsize, size;
1370 u16 *buf;
1371 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1372 struct urb *urb;
1373
1374 if (unlikely(!td))
1375 return;
1376 urb = td->urb;
1377
1378 fifo_change_from_pipe(r8a66597, td->pipe);
1379 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1380 if (unlikely((tmp & FRDY) == 0)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001381 pipe_stop(r8a66597, td->pipe);
1382 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001383 printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001384 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001385 return;
1386 }
1387
1388 /* prepare parameters */
1389 bufsize = td->maxpacket;
1390 if (usb_pipeisoc(urb->pipe)) {
1391 buf = (u16 *)(urb->transfer_buffer +
1392 urb->iso_frame_desc[td->iso_cnt].offset);
1393 size = min(bufsize,
1394 (int)urb->iso_frame_desc[td->iso_cnt].length);
1395 } else {
1396 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
Greg Kroah-Hartman16e2e5f2009-03-03 16:44:13 -08001397 size = min_t(u32, bufsize,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001398 urb->transfer_buffer_length - urb->actual_length);
1399 }
1400
1401 /* write fifo */
1402 if (pipenum > 0)
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001403 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001404 if (urb->transfer_buffer) {
1405 r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
1406 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1407 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1408 }
1409
1410 /* update parameters */
1411 urb->actual_length += size;
1412 if (usb_pipeisoc(urb->pipe)) {
1413 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1414 urb->iso_frame_desc[td->iso_cnt].status = 0;
1415 td->iso_cnt++;
1416 }
1417
1418 /* check transfer finish */
1419 if (check_transfer_finish(td, urb)) {
1420 disable_irq_ready(r8a66597, pipenum);
1421 enable_irq_empty(r8a66597, pipenum);
1422 if (!usb_pipeisoc(urb->pipe))
1423 enable_irq_nrdy(r8a66597, pipenum);
1424 } else
1425 pipe_irq_enable(r8a66597, urb, pipenum);
1426}
1427
1428
Alan Stern888fda42007-08-24 15:41:18 -04001429static void check_next_phase(struct r8a66597 *r8a66597, int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001430{
1431 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1432 struct urb *urb;
1433 u8 finish = 0;
1434
1435 if (unlikely(!td))
1436 return;
1437 urb = td->urb;
1438
1439 switch (td->type) {
1440 case USB_PID_IN:
1441 case USB_PID_OUT:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001442 if (check_transfer_finish(td, urb))
1443 td->type = USB_PID_ACK;
1444 break;
1445 case USB_PID_SETUP:
Alan Sterneb231052007-08-21 15:40:36 -04001446 if (urb->transfer_buffer_length == urb->actual_length)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001447 td->type = USB_PID_ACK;
Alan Sterneb231052007-08-21 15:40:36 -04001448 else if (usb_pipeout(urb->pipe))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001449 td->type = USB_PID_OUT;
1450 else
1451 td->type = USB_PID_IN;
1452 break;
1453 case USB_PID_ACK:
1454 finish = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001455 break;
1456 }
1457
Alan Stern888fda42007-08-24 15:41:18 -04001458 if (finish || status != 0 || urb->unlinked)
1459 finish_request(r8a66597, td, 0, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001460 else
1461 start_transfer(r8a66597, td);
1462}
1463
Alan Stern888fda42007-08-24 15:41:18 -04001464static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001465{
1466 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1467
Alan Stern888fda42007-08-24 15:41:18 -04001468 if (td) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001469 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1470
1471 if (pid == PID_NAK)
Alan Stern888fda42007-08-24 15:41:18 -04001472 return -ECONNRESET;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001473 else
Alan Stern888fda42007-08-24 15:41:18 -04001474 return -EPIPE;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001475 }
Alan Stern888fda42007-08-24 15:41:18 -04001476 return 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001477}
1478
1479static void irq_pipe_ready(struct r8a66597 *r8a66597)
1480{
1481 u16 check;
1482 u16 pipenum;
1483 u16 mask;
1484 struct r8a66597_td *td;
1485
1486 mask = r8a66597_read(r8a66597, BRDYSTS)
1487 & r8a66597_read(r8a66597, BRDYENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001488 r8a66597_write(r8a66597, ~mask, BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001489 if (mask & BRDY0) {
1490 td = r8a66597_get_td(r8a66597, 0);
1491 if (td && td->type == USB_PID_IN)
1492 packet_read(r8a66597, 0);
1493 else
1494 pipe_irq_disable(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001495 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001496 }
1497
1498 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1499 check = 1 << pipenum;
1500 if (mask & check) {
1501 td = r8a66597_get_td(r8a66597, pipenum);
1502 if (unlikely(!td))
1503 continue;
1504
1505 if (td->type == USB_PID_IN)
1506 packet_read(r8a66597, pipenum);
1507 else if (td->type == USB_PID_OUT)
1508 packet_write(r8a66597, pipenum);
1509 }
1510 }
1511}
1512
1513static void irq_pipe_empty(struct r8a66597 *r8a66597)
1514{
1515 u16 tmp;
1516 u16 check;
1517 u16 pipenum;
1518 u16 mask;
1519 struct r8a66597_td *td;
1520
1521 mask = r8a66597_read(r8a66597, BEMPSTS)
1522 & r8a66597_read(r8a66597, BEMPENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001523 r8a66597_write(r8a66597, ~mask, BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001524 if (mask & BEMP0) {
1525 cfifo_change(r8a66597, 0);
1526 td = r8a66597_get_td(r8a66597, 0);
1527 if (td && td->type != USB_PID_OUT)
1528 disable_irq_empty(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001529 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001530 }
1531
1532 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1533 check = 1 << pipenum;
1534 if (mask & check) {
1535 struct r8a66597_td *td;
1536 td = r8a66597_get_td(r8a66597, pipenum);
1537 if (unlikely(!td))
1538 continue;
1539
1540 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1541 if ((tmp & INBUFM) == 0) {
1542 disable_irq_empty(r8a66597, pipenum);
1543 pipe_irq_disable(r8a66597, pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001544 finish_request(r8a66597, td, pipenum, td->urb,
1545 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001546 }
1547 }
1548 }
1549}
1550
1551static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1552{
1553 u16 check;
1554 u16 pipenum;
1555 u16 mask;
Alan Stern888fda42007-08-24 15:41:18 -04001556 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001557
1558 mask = r8a66597_read(r8a66597, NRDYSTS)
1559 & r8a66597_read(r8a66597, NRDYENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001560 r8a66597_write(r8a66597, ~mask, NRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001561 if (mask & NRDY0) {
1562 cfifo_change(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001563 status = get_urb_error(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001564 pipe_irq_disable(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001565 check_next_phase(r8a66597, status);
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
Alan Stern888fda42007-08-24 15:41:18 -04001576 status = get_urb_error(r8a66597, pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001577 pipe_irq_disable(r8a66597, pipenum);
1578 pipe_stop(r8a66597, td->pipe);
Alan Stern888fda42007-08-24 15:41:18 -04001579 finish_request(r8a66597, td, pipenum, td->urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001580 }
1581 }
1582}
1583
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001584static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1585{
1586 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1587 u16 intsts0, intsts1, intsts2;
1588 u16 intenb0, intenb1, intenb2;
1589 u16 mask0, mask1, mask2;
Alan Stern888fda42007-08-24 15:41:18 -04001590 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001591
1592 spin_lock(&r8a66597->lock);
1593
1594 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1595 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1596 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1597 intenb0 = r8a66597_read(r8a66597, INTENB0);
1598 intenb1 = r8a66597_read(r8a66597, INTENB1);
1599 intenb2 = r8a66597_read(r8a66597, INTENB2);
1600
1601 mask2 = intsts2 & intenb2;
1602 mask1 = intsts1 & intenb1;
1603 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1604 if (mask2) {
1605 if (mask2 & ATTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001606 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001607 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1608
1609 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001610 start_root_hub_sampling(r8a66597, 1, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001611 }
1612 if (mask2 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001613 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001614 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1615 r8a66597_usb_disconnect(r8a66597, 1);
1616 }
1617 }
1618
1619 if (mask1) {
1620 if (mask1 & ATTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001621 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001622 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1623
1624 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001625 start_root_hub_sampling(r8a66597, 0, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001626 }
1627 if (mask1 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001628 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001629 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1630 r8a66597_usb_disconnect(r8a66597, 0);
1631 }
1632 if (mask1 & SIGN) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001633 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001634 status = get_urb_error(r8a66597, 0);
1635 check_next_phase(r8a66597, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001636 }
1637 if (mask1 & SACK) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001638 r8a66597_write(r8a66597, ~SACK, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001639 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001640 }
1641 }
1642 if (mask0) {
1643 if (mask0 & BRDY)
1644 irq_pipe_ready(r8a66597);
1645 if (mask0 & BEMP)
1646 irq_pipe_empty(r8a66597);
1647 if (mask0 & NRDY)
1648 irq_pipe_nrdy(r8a66597);
1649 }
1650
1651 spin_unlock(&r8a66597->lock);
1652 return IRQ_HANDLED;
1653}
1654
1655/* this function must be called with interrupt disabled */
1656static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1657{
1658 u16 tmp;
1659 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1660
1661 if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
1662 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1663
1664 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1665 if ((tmp & USBRST) == USBRST) {
1666 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1667 dvstctr_reg);
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001668 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001669 } else
1670 r8a66597_usb_connect(r8a66597, port);
1671 }
1672
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001673 if (!(rh->port & (1 << USB_PORT_FEAT_CONNECTION))) {
1674 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1675 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1676 }
1677
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001678 if (rh->scount > 0) {
1679 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1680 if (tmp == rh->old_syssts) {
1681 rh->scount--;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001682 if (rh->scount == 0)
1683 r8a66597_check_syssts(r8a66597, port, tmp);
1684 else
1685 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001686 } else {
1687 rh->scount = R8A66597_MAX_SAMPLING;
1688 rh->old_syssts = tmp;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001689 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001690 }
1691 }
1692}
1693
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001694static void r8a66597_interval_timer(unsigned long _r8a66597)
1695{
1696 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1697 unsigned long flags;
1698 u16 pipenum;
1699 struct r8a66597_td *td;
1700
1701 spin_lock_irqsave(&r8a66597->lock, flags);
1702
1703 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1704 if (!(r8a66597->interval_map & (1 << pipenum)))
1705 continue;
1706 if (timer_pending(&r8a66597->interval_timer[pipenum]))
1707 continue;
1708
1709 td = r8a66597_get_td(r8a66597, pipenum);
1710 if (td)
1711 start_transfer(r8a66597, td);
1712 }
1713
1714 spin_unlock_irqrestore(&r8a66597->lock, flags);
1715}
1716
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001717static void r8a66597_td_timer(unsigned long _r8a66597)
1718{
1719 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1720 unsigned long flags;
1721 u16 pipenum;
1722 struct r8a66597_td *td, *new_td = NULL;
1723 struct r8a66597_pipe *pipe;
1724
1725 spin_lock_irqsave(&r8a66597->lock, flags);
1726 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1727 if (!(r8a66597->timeout_map & (1 << pipenum)))
1728 continue;
1729 if (timer_pending(&r8a66597->td_timer[pipenum]))
1730 continue;
1731
1732 td = r8a66597_get_td(r8a66597, pipenum);
1733 if (!td) {
1734 r8a66597->timeout_map &= ~(1 << pipenum);
1735 continue;
1736 }
1737
1738 if (td->urb->actual_length) {
1739 set_td_timer(r8a66597, td);
1740 break;
1741 }
1742
1743 pipe = td->pipe;
1744 pipe_stop(r8a66597, pipe);
1745
1746 new_td = td;
1747 do {
1748 list_move_tail(&new_td->queue,
1749 &r8a66597->pipe_queue[pipenum]);
1750 new_td = r8a66597_get_td(r8a66597, pipenum);
1751 if (!new_td) {
1752 new_td = td;
1753 break;
1754 }
1755 } while (td != new_td && td->address == new_td->address);
1756
1757 start_transfer(r8a66597, new_td);
1758
1759 if (td == new_td)
1760 r8a66597->timeout_map &= ~(1 << pipenum);
1761 else
1762 set_td_timer(r8a66597, new_td);
1763 break;
1764 }
1765 spin_unlock_irqrestore(&r8a66597->lock, flags);
1766}
1767
1768static void r8a66597_timer(unsigned long _r8a66597)
1769{
1770 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1771 unsigned long flags;
Yoshihiro Shimoda58639642008-11-11 16:47:21 +09001772 int port;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001773
1774 spin_lock_irqsave(&r8a66597->lock, flags);
1775
Yoshihiro Shimoda58639642008-11-11 16:47:21 +09001776 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
1777 r8a66597_root_hub_control(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001778
1779 spin_unlock_irqrestore(&r8a66597->lock, flags);
1780}
1781
1782static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1783{
1784 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1785
1786 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1787 (urb->dev->state == USB_STATE_CONFIGURED))
1788 return 1;
1789 else
1790 return 0;
1791}
1792
1793static int r8a66597_start(struct usb_hcd *hcd)
1794{
1795 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001796
1797 hcd->state = HC_STATE_RUNNING;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001798 return enable_controller(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001799}
1800
1801static void r8a66597_stop(struct usb_hcd *hcd)
1802{
1803 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1804
1805 disable_controller(r8a66597);
1806}
1807
1808static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1809{
1810 unsigned int usb_address = usb_pipedevice(urb->pipe);
1811 u16 root_port, hub_port;
1812
1813 if (usb_address == 0) {
1814 get_port_number(urb->dev->devpath,
1815 &root_port, &hub_port);
1816 set_devadd_reg(r8a66597, 0,
1817 get_r8a66597_usb_speed(urb->dev->speed),
1818 get_parent_r8a66597_address(r8a66597, urb->dev),
1819 hub_port, root_port);
1820 }
1821}
1822
1823static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1824 struct urb *urb,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001825 struct usb_host_endpoint *hep)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001826{
1827 struct r8a66597_td *td;
1828 u16 pipenum;
1829
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001830 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001831 if (td == NULL)
1832 return NULL;
1833
1834 pipenum = r8a66597_get_pipenum(urb, hep);
1835 td->pipenum = pipenum;
1836 td->pipe = hep->hcpriv;
1837 td->urb = urb;
1838 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1839 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1840 !usb_pipein(urb->pipe));
1841 if (usb_pipecontrol(urb->pipe))
1842 td->type = USB_PID_SETUP;
1843 else if (usb_pipein(urb->pipe))
1844 td->type = USB_PID_IN;
1845 else
1846 td->type = USB_PID_OUT;
1847 INIT_LIST_HEAD(&td->queue);
1848
1849 return td;
1850}
1851
1852static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001853 struct urb *urb,
1854 gfp_t mem_flags)
1855{
Alan Sterne9df41c2007-08-08 11:48:02 -04001856 struct usb_host_endpoint *hep = urb->ep;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001857 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1858 struct r8a66597_td *td = NULL;
Alan Sterne9df41c2007-08-08 11:48:02 -04001859 int ret, request = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001860 unsigned long flags;
1861
1862 spin_lock_irqsave(&r8a66597->lock, flags);
1863 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1864 ret = -ENODEV;
Alan Sterne9df41c2007-08-08 11:48:02 -04001865 goto error_not_linked;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001866 }
1867
Alan Sterne9df41c2007-08-08 11:48:02 -04001868 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1869 if (ret)
1870 goto error_not_linked;
1871
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001872 if (!hep->hcpriv) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001873 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1874 GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001875 if (!hep->hcpriv) {
1876 ret = -ENOMEM;
1877 goto error;
1878 }
1879 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1880 if (usb_pipeendpoint(urb->pipe))
1881 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1882 }
1883
1884 if (unlikely(check_pipe_config(r8a66597, urb)))
1885 init_pipe_config(r8a66597, urb);
1886
1887 set_address_zero(r8a66597, urb);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001888 td = r8a66597_make_td(r8a66597, urb, hep);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001889 if (td == NULL) {
1890 ret = -ENOMEM;
1891 goto error;
1892 }
1893 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1894 request = 1;
1895 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001896 urb->hcpriv = td;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001897
1898 if (request) {
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001899 if (td->pipe->info.timer_interval) {
1900 r8a66597->interval_map |= 1 << td->pipenum;
1901 mod_timer(&r8a66597->interval_timer[td->pipenum],
1902 jiffies + msecs_to_jiffies(
1903 td->pipe->info.timer_interval));
1904 } else {
1905 ret = start_transfer(r8a66597, td);
1906 if (ret < 0) {
1907 list_del(&td->queue);
1908 kfree(td);
1909 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001910 }
1911 } else
1912 set_td_timer(r8a66597, td);
1913
1914error:
Alan Sterne9df41c2007-08-08 11:48:02 -04001915 if (ret)
1916 usb_hcd_unlink_urb_from_ep(hcd, urb);
1917error_not_linked:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001918 spin_unlock_irqrestore(&r8a66597->lock, flags);
1919 return ret;
1920}
1921
Alan Sterne9df41c2007-08-08 11:48:02 -04001922static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1923 int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001924{
1925 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1926 struct r8a66597_td *td;
1927 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001928 int rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001929
1930 spin_lock_irqsave(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001931 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1932 if (rc)
1933 goto done;
1934
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001935 if (urb->hcpriv) {
1936 td = urb->hcpriv;
1937 pipe_stop(r8a66597, td->pipe);
1938 pipe_irq_disable(r8a66597, td->pipenum);
1939 disable_irq_empty(r8a66597, td->pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001940 finish_request(r8a66597, td, td->pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001941 }
Alan Sterne9df41c2007-08-08 11:48:02 -04001942 done:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001943 spin_unlock_irqrestore(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001944 return rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001945}
1946
1947static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1948 struct usb_host_endpoint *hep)
1949{
1950 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1951 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1952 struct r8a66597_td *td;
1953 struct urb *urb = NULL;
1954 u16 pipenum;
1955 unsigned long flags;
1956
1957 if (pipe == NULL)
1958 return;
1959 pipenum = pipe->info.pipenum;
1960
1961 if (pipenum == 0) {
1962 kfree(hep->hcpriv);
1963 hep->hcpriv = NULL;
1964 return;
1965 }
1966
1967 spin_lock_irqsave(&r8a66597->lock, flags);
1968 pipe_stop(r8a66597, pipe);
1969 pipe_irq_disable(r8a66597, pipenum);
1970 disable_irq_empty(r8a66597, pipenum);
1971 td = r8a66597_get_td(r8a66597, pipenum);
1972 if (td)
1973 urb = td->urb;
Alan Stern888fda42007-08-24 15:41:18 -04001974 finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001975 kfree(hep->hcpriv);
1976 hep->hcpriv = NULL;
1977 spin_unlock_irqrestore(&r8a66597->lock, flags);
1978}
1979
1980static int r8a66597_get_frame(struct usb_hcd *hcd)
1981{
1982 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1983 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
1984}
1985
1986static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
1987{
1988 int chix;
1989
1990 if (udev->state == USB_STATE_CONFIGURED &&
1991 udev->parent && udev->parent->devnum > 1 &&
1992 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
1993 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
1994
1995 for (chix = 0; chix < udev->maxchild; chix++) {
1996 struct usb_device *childdev = udev->children[chix];
1997
1998 if (childdev)
1999 collect_usb_address_map(childdev, map);
2000 }
2001}
2002
2003/* this function must be called with interrupt disabled */
2004static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
2005 int addr)
2006{
2007 struct r8a66597_device *dev;
2008 struct list_head *list = &r8a66597->child_device;
2009
2010 list_for_each_entry(dev, list, device_list) {
2011 if (!dev)
2012 continue;
2013 if (dev->usb_address != addr)
2014 continue;
2015
2016 return dev;
2017 }
2018
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002019 printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002020 return NULL;
2021}
2022
2023static void update_usb_address_map(struct r8a66597 *r8a66597,
2024 struct usb_device *root_hub,
2025 unsigned long *map)
2026{
2027 int i, j, addr;
2028 unsigned long diff;
2029 unsigned long flags;
2030
2031 for (i = 0; i < 4; i++) {
2032 diff = r8a66597->child_connect_map[i] ^ map[i];
2033 if (!diff)
2034 continue;
2035
2036 for (j = 0; j < 32; j++) {
2037 if (!(diff & (1 << j)))
2038 continue;
2039
2040 addr = i * 32 + j;
2041 if (map[i] & (1 << j))
2042 set_child_connect_map(r8a66597, addr);
2043 else {
2044 struct r8a66597_device *dev;
2045
2046 spin_lock_irqsave(&r8a66597->lock, flags);
2047 dev = get_r8a66597_device(r8a66597, addr);
2048 disable_r8a66597_pipe_all(r8a66597, dev);
2049 free_usb_address(r8a66597, dev);
2050 put_child_connect_map(r8a66597, addr);
2051 spin_unlock_irqrestore(&r8a66597->lock, flags);
2052 }
2053 }
2054 }
2055}
2056
2057static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
2058 struct usb_hcd *hcd)
2059{
2060 struct usb_bus *bus;
2061 unsigned long now_map[4];
2062
2063 memset(now_map, 0, sizeof(now_map));
2064
2065 list_for_each_entry(bus, &usb_bus_list, bus_list) {
2066 if (!bus->root_hub)
2067 continue;
2068
2069 if (bus->busnum != hcd->self.busnum)
2070 continue;
2071
2072 collect_usb_address_map(bus->root_hub, now_map);
2073 update_usb_address_map(r8a66597, bus->root_hub, now_map);
2074 }
2075}
2076
2077static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2078{
2079 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2080 unsigned long flags;
2081 int i;
2082
2083 r8a66597_check_detect_child(r8a66597, hcd);
2084
2085 spin_lock_irqsave(&r8a66597->lock, flags);
2086
2087 *buf = 0; /* initialize (no change) */
2088
2089 for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++) {
2090 if (r8a66597->root_hub[i].port & 0xffff0000)
2091 *buf |= 1 << (i + 1);
2092 }
2093
2094 spin_unlock_irqrestore(&r8a66597->lock, flags);
2095
2096 return (*buf != 0);
2097}
2098
2099static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2100 struct usb_hub_descriptor *desc)
2101{
2102 desc->bDescriptorType = 0x29;
2103 desc->bHubContrCurrent = 0;
2104 desc->bNbrPorts = R8A66597_MAX_ROOT_HUB;
2105 desc->bDescLength = 9;
2106 desc->bPwrOn2PwrGood = 0;
2107 desc->wHubCharacteristics = cpu_to_le16(0x0011);
2108 desc->bitmap[0] = ((1 << R8A66597_MAX_ROOT_HUB) - 1) << 1;
2109 desc->bitmap[1] = ~0;
2110}
2111
2112static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2113 u16 wIndex, char *buf, u16 wLength)
2114{
2115 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2116 int ret;
2117 int port = (wIndex & 0x00FF) - 1;
2118 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2119 unsigned long flags;
2120
2121 ret = 0;
2122
2123 spin_lock_irqsave(&r8a66597->lock, flags);
2124 switch (typeReq) {
2125 case ClearHubFeature:
2126 case SetHubFeature:
2127 switch (wValue) {
2128 case C_HUB_OVER_CURRENT:
2129 case C_HUB_LOCAL_POWER:
2130 break;
2131 default:
2132 goto error;
2133 }
2134 break;
2135 case ClearPortFeature:
2136 if (wIndex > R8A66597_MAX_ROOT_HUB)
2137 goto error;
2138 if (wLength != 0)
2139 goto error;
2140
2141 switch (wValue) {
2142 case USB_PORT_FEAT_ENABLE:
2143 rh->port &= (1 << USB_PORT_FEAT_POWER);
2144 break;
2145 case USB_PORT_FEAT_SUSPEND:
2146 break;
2147 case USB_PORT_FEAT_POWER:
2148 r8a66597_port_power(r8a66597, port, 0);
2149 break;
2150 case USB_PORT_FEAT_C_ENABLE:
2151 case USB_PORT_FEAT_C_SUSPEND:
2152 case USB_PORT_FEAT_C_CONNECTION:
2153 case USB_PORT_FEAT_C_OVER_CURRENT:
2154 case USB_PORT_FEAT_C_RESET:
2155 break;
2156 default:
2157 goto error;
2158 }
2159 rh->port &= ~(1 << wValue);
2160 break;
2161 case GetHubDescriptor:
2162 r8a66597_hub_descriptor(r8a66597,
2163 (struct usb_hub_descriptor *)buf);
2164 break;
2165 case GetHubStatus:
2166 *buf = 0x00;
2167 break;
2168 case GetPortStatus:
2169 if (wIndex > R8A66597_MAX_ROOT_HUB)
2170 goto error;
Al Virofd05e722008-04-28 07:00:16 +01002171 *(__le32 *)buf = cpu_to_le32(rh->port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002172 break;
2173 case SetPortFeature:
2174 if (wIndex > R8A66597_MAX_ROOT_HUB)
2175 goto error;
2176 if (wLength != 0)
2177 goto error;
2178
2179 switch (wValue) {
2180 case USB_PORT_FEAT_SUSPEND:
2181 break;
2182 case USB_PORT_FEAT_POWER:
2183 r8a66597_port_power(r8a66597, port, 1);
2184 rh->port |= (1 << USB_PORT_FEAT_POWER);
2185 break;
2186 case USB_PORT_FEAT_RESET: {
2187 struct r8a66597_device *dev = rh->dev;
2188
2189 rh->port |= (1 << USB_PORT_FEAT_RESET);
2190
2191 disable_r8a66597_pipe_all(r8a66597, dev);
2192 free_usb_address(r8a66597, dev);
2193
2194 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2195 get_dvstctr_reg(port));
2196 mod_timer(&r8a66597->rh_timer,
2197 jiffies + msecs_to_jiffies(50));
2198 }
2199 break;
2200 default:
2201 goto error;
2202 }
2203 rh->port |= 1 << wValue;
2204 break;
2205 default:
2206error:
2207 ret = -EPIPE;
2208 break;
2209 }
2210
2211 spin_unlock_irqrestore(&r8a66597->lock, flags);
2212 return ret;
2213}
2214
2215static struct hc_driver r8a66597_hc_driver = {
2216 .description = hcd_name,
2217 .hcd_priv_size = sizeof(struct r8a66597),
2218 .irq = r8a66597_irq,
2219
2220 /*
2221 * generic hardware linkage
2222 */
2223 .flags = HCD_USB2,
2224
2225 .start = r8a66597_start,
2226 .stop = r8a66597_stop,
2227
2228 /*
2229 * managing i/o requests and associated device resources
2230 */
2231 .urb_enqueue = r8a66597_urb_enqueue,
2232 .urb_dequeue = r8a66597_urb_dequeue,
2233 .endpoint_disable = r8a66597_endpoint_disable,
2234
2235 /*
2236 * periodic schedule support
2237 */
2238 .get_frame_number = r8a66597_get_frame,
2239
2240 /*
2241 * root hub support
2242 */
2243 .hub_status_data = r8a66597_hub_status_data,
2244 .hub_control = r8a66597_hub_control,
2245};
2246
2247#if defined(CONFIG_PM)
2248static int r8a66597_suspend(struct platform_device *pdev, pm_message_t state)
2249{
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002250 return 0;
2251}
2252
2253static int r8a66597_resume(struct platform_device *pdev)
2254{
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002255 return 0;
2256}
2257#else /* if defined(CONFIG_PM) */
2258#define r8a66597_suspend NULL
2259#define r8a66597_resume NULL
2260#endif
2261
2262static int __init_or_module r8a66597_remove(struct platform_device *pdev)
2263{
2264 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2265 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2266
2267 del_timer_sync(&r8a66597->rh_timer);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002268 usb_remove_hcd(hcd);
Yoshihiro Shimodaca0677a2007-10-05 15:53:12 +09002269 iounmap((void *)r8a66597->reg);
Magnus Damm765786e2008-10-31 20:22:38 +09002270#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2271 clk_put(r8a66597->clk);
2272#endif
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002273 usb_put_hcd(hcd);
2274 return 0;
2275}
2276
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002277static int __init r8a66597_probe(struct platform_device *pdev)
2278{
Magnus Damm765786e2008-10-31 20:22:38 +09002279#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2280 char clk_name[8];
2281#endif
Marc Zyngier27140212008-08-18 13:08:42 +02002282 struct resource *res = NULL, *ires;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002283 int irq = -1;
2284 void __iomem *reg = NULL;
2285 struct usb_hcd *hcd = NULL;
2286 struct r8a66597 *r8a66597;
2287 int ret = 0;
2288 int i;
Yoshihiro Shimoda0bf32b82008-06-27 19:09:55 +09002289 unsigned long irq_trigger;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002290
2291 if (pdev->dev.dma_mask) {
2292 ret = -EINVAL;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002293 dev_err(&pdev->dev, "dma not supported\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002294 goto clean_up;
2295 }
2296
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002297 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002298 if (!res) {
2299 ret = -ENODEV;
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002300 dev_err(&pdev->dev, "platform_get_resource error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002301 goto clean_up;
2302 }
2303
Marc Zyngier27140212008-08-18 13:08:42 +02002304 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2305 if (!ires) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002306 ret = -ENODEV;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002307 dev_err(&pdev->dev,
2308 "platform_get_resource IORESOURCE_IRQ error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002309 goto clean_up;
2310 }
2311
Marc Zyngier27140212008-08-18 13:08:42 +02002312 irq = ires->start;
2313 irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
2314
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002315 reg = ioremap(res->start, resource_size(res));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002316 if (reg == NULL) {
2317 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002318 dev_err(&pdev->dev, "ioremap error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002319 goto clean_up;
2320 }
2321
2322 /* initialize hcd */
2323 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2324 if (!hcd) {
2325 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002326 dev_err(&pdev->dev, "Failed to create hcd\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002327 goto clean_up;
2328 }
2329 r8a66597 = hcd_to_r8a66597(hcd);
2330 memset(r8a66597, 0, sizeof(struct r8a66597));
2331 dev_set_drvdata(&pdev->dev, r8a66597);
2332
Magnus Damm765786e2008-10-31 20:22:38 +09002333#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2334 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
2335 r8a66597->clk = clk_get(&pdev->dev, clk_name);
2336 if (IS_ERR(r8a66597->clk)) {
2337 dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
2338 ret = PTR_ERR(r8a66597->clk);
2339 goto clean_up2;
2340 }
2341#endif
2342
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002343 spin_lock_init(&r8a66597->lock);
2344 init_timer(&r8a66597->rh_timer);
2345 r8a66597->rh_timer.function = r8a66597_timer;
2346 r8a66597->rh_timer.data = (unsigned long)r8a66597;
2347 r8a66597->reg = (unsigned long)reg;
2348
2349 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2350 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2351 init_timer(&r8a66597->td_timer[i]);
2352 r8a66597->td_timer[i].function = r8a66597_td_timer;
2353 r8a66597->td_timer[i].data = (unsigned long)r8a66597;
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09002354 setup_timer(&r8a66597->interval_timer[i],
2355 r8a66597_interval_timer,
2356 (unsigned long)r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002357 }
2358 INIT_LIST_HEAD(&r8a66597->child_device);
2359
2360 hcd->rsrc_start = res->start;
Marc Zyngier27140212008-08-18 13:08:42 +02002361
2362 /* irq_sense setting on cmdline takes precedence over resource
2363 * settings, so the introduction of irqflags in IRQ resourse
2364 * won't disturb existing setups */
2365 switch (irq_sense) {
2366 case INTL:
2367 irq_trigger = IRQF_TRIGGER_LOW;
2368 break;
2369 case 0:
2370 irq_trigger = IRQF_TRIGGER_FALLING;
2371 break;
2372 case 0xff:
2373 if (irq_trigger)
2374 irq_sense = (irq_trigger & IRQF_TRIGGER_LOW) ?
2375 INTL : 0;
2376 else {
2377 irq_sense = INTL;
2378 irq_trigger = IRQF_TRIGGER_LOW;
2379 }
2380 break;
2381 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002382 dev_err(&pdev->dev, "Unknown irq_sense value.\n");
Marc Zyngier27140212008-08-18 13:08:42 +02002383 }
2384
Yoshihiro Shimoda0bf32b82008-06-27 19:09:55 +09002385 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002386 if (ret != 0) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002387 dev_err(&pdev->dev, "Failed to add hcd\n");
Magnus Damm765786e2008-10-31 20:22:38 +09002388 goto clean_up3;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002389 }
2390
2391 return 0;
2392
Magnus Damm765786e2008-10-31 20:22:38 +09002393clean_up3:
2394#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2395 clk_put(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +09002396clean_up2:
Paul Mundtd6435102008-11-18 12:40:39 +09002397#endif
Magnus Damm765786e2008-10-31 20:22:38 +09002398 usb_put_hcd(hcd);
2399
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002400clean_up:
2401 if (reg)
2402 iounmap(reg);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002403
2404 return ret;
2405}
2406
2407static struct platform_driver r8a66597_driver = {
2408 .probe = r8a66597_probe,
2409 .remove = r8a66597_remove,
2410 .suspend = r8a66597_suspend,
2411 .resume = r8a66597_resume,
2412 .driver = {
2413 .name = (char *) hcd_name,
Kay Sieversf4fce612008-04-10 21:29:22 -07002414 .owner = THIS_MODULE,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002415 },
2416};
2417
2418static int __init r8a66597_init(void)
2419{
2420 if (usb_disabled())
2421 return -ENODEV;
2422
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -07002423 printk(KERN_INFO KBUILD_MODNAME ": driver %s, %s\n", hcd_name,
2424 DRIVER_VERSION);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002425 return platform_driver_register(&r8a66597_driver);
2426}
2427module_init(r8a66597_init);
2428
2429static void __exit r8a66597_cleanup(void)
2430{
2431 platform_driver_unregister(&r8a66597_driver);
2432}
2433module_exit(r8a66597_cleanup);
2434