blob: d478ffad59b4db18eb1b5c0eb0654e4882b94efe [file] [log] [blame]
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001/*
2 * R8A66597 HCD (Host Controller Driver)
3 *
4 * Copyright (C) 2006-2007 Renesas Solutions Corp.
5 * Portions Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
6 * Portions Copyright (C) 2004-2005 David Brownell
7 * Portions Copyright (C) 1999 Roman Weissgaerber
8 *
9 * Author : Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 *
24 */
25
26#include <linux/module.h>
27#include <linux/kernel.h>
28#include <linux/sched.h>
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090029#include <linux/errno.h>
30#include <linux/init.h>
31#include <linux/timer.h>
32#include <linux/delay.h>
33#include <linux/list.h>
34#include <linux/interrupt.h>
35#include <linux/usb.h>
36#include <linux/platform_device.h>
Yoshihiro Shimodae2945312007-07-18 23:10:34 +090037#include <linux/io.h>
Paul Mundt27175682010-02-04 06:57:58 +000038#include <linux/mm.h>
Yoshihiro Shimodae2945312007-07-18 23:10:34 +090039#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Paul Mundt27175682010-02-04 06:57:58 +000041#include <asm/cacheflush.h>
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090042
43#include "../core/hcd.h"
44#include "r8a66597.h"
45
46MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
47MODULE_LICENSE("GPL");
48MODULE_AUTHOR("Yoshihiro Shimoda");
Kay Sieversf4fce612008-04-10 21:29:22 -070049MODULE_ALIAS("platform:r8a66597_hcd");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090050
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +090051#define DRIVER_VERSION "2009-05-26"
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090052
53static const char hcd_name[] = "r8a66597_hcd";
54
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090055static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
56static int r8a66597_get_frame(struct usb_hcd *hcd);
57
58/* this function must be called with interrupt disabled */
59static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
60 unsigned long reg)
61{
62 u16 tmp;
63
64 tmp = r8a66597_read(r8a66597, INTENB0);
65 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
66 r8a66597_bset(r8a66597, 1 << pipenum, reg);
67 r8a66597_write(r8a66597, tmp, INTENB0);
68}
69
70/* this function must be called with interrupt disabled */
71static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
72 unsigned long reg)
73{
74 u16 tmp;
75
76 tmp = r8a66597_read(r8a66597, INTENB0);
77 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
78 r8a66597_bclr(r8a66597, 1 << pipenum, reg);
79 r8a66597_write(r8a66597, tmp, INTENB0);
80}
81
82static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
83 u16 usbspd, u8 upphub, u8 hubport, int port)
84{
85 u16 val;
86 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
87
88 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
89 r8a66597_write(r8a66597, val, devadd_reg);
90}
91
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +090092static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090093{
94 u16 tmp;
95 int i = 0;
96
Magnus Damm719a72b2009-07-17 14:59:55 +000097 if (r8a66597->pdata->on_chip) {
98#ifdef CONFIG_HAVE_CLK
99 clk_enable(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +0900100#endif
Magnus Damm719a72b2009-07-17 14:59:55 +0000101 do {
102 r8a66597_write(r8a66597, SCKE, SYSCFG0);
103 tmp = r8a66597_read(r8a66597, SYSCFG0);
104 if (i++ > 1000) {
105 printk(KERN_ERR "r8a66597: reg access fail.\n");
106 return -ENXIO;
107 }
108 } while ((tmp & SCKE) != SCKE);
109 r8a66597_write(r8a66597, 0x04, 0x02);
110 } else {
111 do {
112 r8a66597_write(r8a66597, USBE, SYSCFG0);
113 tmp = r8a66597_read(r8a66597, SYSCFG0);
114 if (i++ > 1000) {
115 printk(KERN_ERR "r8a66597: reg access fail.\n");
116 return -ENXIO;
117 }
118 } while ((tmp & USBE) != USBE);
119 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
120 r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata),
121 XTAL, SYSCFG0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900122
Magnus Damm719a72b2009-07-17 14:59:55 +0000123 i = 0;
124 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
125 do {
126 msleep(1);
127 tmp = r8a66597_read(r8a66597, SYSCFG0);
128 if (i++ > 500) {
129 printk(KERN_ERR "r8a66597: reg access fail.\n");
130 return -ENXIO;
131 }
132 } while ((tmp & SCKE) != SCKE);
133 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900134
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900135 return 0;
136}
137
138static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
139{
140 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
141 udelay(1);
Magnus Damm719a72b2009-07-17 14:59:55 +0000142
143 if (r8a66597->pdata->on_chip) {
144#ifdef CONFIG_HAVE_CLK
145 clk_disable(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +0900146#endif
Magnus Damm719a72b2009-07-17 14:59:55 +0000147 } else {
148 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
149 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
150 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
151 }
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900152}
153
154static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
155{
156 u16 val;
157
158 val = port ? DRPD : DCFM | DRPD;
159 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
160 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
161
162 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
163 r8a66597_bclr(r8a66597, DTCHE, get_intenb_reg(port));
164 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
165}
166
167static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
168{
169 u16 val, tmp;
170
171 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
172 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
173
174 r8a66597_port_power(r8a66597, port, 0);
175
176 do {
177 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
178 udelay(640);
179 } while (tmp == EDGESTS);
180
181 val = port ? DRPD : DCFM | DRPD;
182 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
183 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
184}
185
186static int enable_controller(struct r8a66597 *r8a66597)
187{
188 int ret, port;
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +0900189 u16 vif = r8a66597->pdata->vif ? LDRV : 0;
190 u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
191 u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900192
193 ret = r8a66597_clock_enable(r8a66597);
194 if (ret < 0)
195 return ret;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900196
197 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900198 r8a66597_bset(r8a66597, USBE, SYSCFG0);
199
200 r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
201 r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
202 r8a66597_bset(r8a66597, BRDY0, BRDYENB);
203 r8a66597_bset(r8a66597, BEMP0, BEMPENB);
204
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900205 r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
206 r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
207 r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900208 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
209
210 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900211
Magnus Damm719a72b2009-07-17 14:59:55 +0000212 for (port = 0; port < r8a66597->max_root_hub; port++)
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900213 r8a66597_enable_port(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900214
215 return 0;
216}
217
218static void disable_controller(struct r8a66597 *r8a66597)
219{
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900220 int port;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900221
Magnus Damme5ff15b2010-01-27 07:41:19 +0000222 /* disable interrupts */
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900223 r8a66597_write(r8a66597, 0, INTENB0);
Magnus Damme5ff15b2010-01-27 07:41:19 +0000224 r8a66597_write(r8a66597, 0, INTENB1);
225 r8a66597_write(r8a66597, 0, BRDYENB);
226 r8a66597_write(r8a66597, 0, BEMPENB);
227 r8a66597_write(r8a66597, 0, NRDYENB);
228
229 /* clear status */
230 r8a66597_write(r8a66597, 0, BRDYSTS);
231 r8a66597_write(r8a66597, 0, NRDYSTS);
232 r8a66597_write(r8a66597, 0, BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900233
Magnus Damm719a72b2009-07-17 14:59:55 +0000234 for (port = 0; port < r8a66597->max_root_hub; port++)
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900235 r8a66597_disable_port(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900236
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900237 r8a66597_clock_disable(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900238}
239
240static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
241 struct usb_device *udev)
242{
243 struct r8a66597_device *dev;
244
245 if (udev->parent && udev->parent->devnum != 1)
246 udev = udev->parent;
247
248 dev = dev_get_drvdata(&udev->dev);
249 if (dev)
250 return dev->address;
251 else
252 return 0;
253}
254
255static int is_child_device(char *devpath)
256{
257 return (devpath[2] ? 1 : 0);
258}
259
260static int is_hub_limit(char *devpath)
261{
262 return ((strlen(devpath) >= 4) ? 1 : 0);
263}
264
Magnus Damm719a72b2009-07-17 14:59:55 +0000265static void get_port_number(struct r8a66597 *r8a66597,
266 char *devpath, u16 *root_port, u16 *hub_port)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900267{
268 if (root_port) {
269 *root_port = (devpath[0] & 0x0F) - 1;
Magnus Damm719a72b2009-07-17 14:59:55 +0000270 if (*root_port >= r8a66597->max_root_hub)
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700271 printk(KERN_ERR "r8a66597: Illegal root port number.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900272 }
273 if (hub_port)
274 *hub_port = devpath[2] & 0x0F;
275}
276
277static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
278{
279 u16 usbspd = 0;
280
281 switch (speed) {
282 case USB_SPEED_LOW:
283 usbspd = LSMODE;
284 break;
285 case USB_SPEED_FULL:
286 usbspd = FSMODE;
287 break;
288 case USB_SPEED_HIGH:
289 usbspd = HSMODE;
290 break;
291 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700292 printk(KERN_ERR "r8a66597: unknown speed\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900293 break;
294 }
295
296 return usbspd;
297}
298
299static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
300{
301 int idx;
302
303 idx = address / 32;
304 r8a66597->child_connect_map[idx] |= 1 << (address % 32);
305}
306
307static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
308{
309 int idx;
310
311 idx = address / 32;
312 r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
313}
314
315static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
316{
317 u16 pipenum = pipe->info.pipenum;
Ming Leife9b9032008-06-08 16:13:03 +0800318 const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
319 const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
320 const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900321
322 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
323 dma_ch = R8A66597_PIPE_NO_DMA;
324
325 pipe->fifoaddr = fifoaddr[dma_ch];
326 pipe->fifosel = fifosel[dma_ch];
327 pipe->fifoctr = fifoctr[dma_ch];
328
329 if (pipenum == 0)
330 pipe->pipectr = DCPCTR;
331 else
332 pipe->pipectr = get_pipectr_addr(pipenum);
333
334 if (check_bulk_or_isoc(pipenum)) {
335 pipe->pipetre = get_pipetre_addr(pipenum);
336 pipe->pipetrn = get_pipetrn_addr(pipenum);
337 } else {
338 pipe->pipetre = 0;
339 pipe->pipetrn = 0;
340 }
341}
342
343static struct r8a66597_device *
344get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
345{
346 if (usb_pipedevice(urb->pipe) == 0)
347 return &r8a66597->device0;
348
349 return dev_get_drvdata(&urb->dev->dev);
350}
351
352static int make_r8a66597_device(struct r8a66597 *r8a66597,
353 struct urb *urb, u8 addr)
354{
355 struct r8a66597_device *dev;
356 int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
357
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900358 dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900359 if (dev == NULL)
360 return -ENOMEM;
361
362 dev_set_drvdata(&urb->dev->dev, dev);
363 dev->udev = urb->dev;
364 dev->address = addr;
365 dev->usb_address = usb_address;
366 dev->state = USB_STATE_ADDRESS;
367 dev->ep_in_toggle = 0;
368 dev->ep_out_toggle = 0;
369 INIT_LIST_HEAD(&dev->device_list);
370 list_add_tail(&dev->device_list, &r8a66597->child_device);
371
Magnus Damm719a72b2009-07-17 14:59:55 +0000372 get_port_number(r8a66597, urb->dev->devpath,
373 &dev->root_port, &dev->hub_port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900374 if (!is_child_device(urb->dev->devpath))
375 r8a66597->root_hub[dev->root_port].dev = dev;
376
377 set_devadd_reg(r8a66597, dev->address,
378 get_r8a66597_usb_speed(urb->dev->speed),
379 get_parent_r8a66597_address(r8a66597, urb->dev),
380 dev->hub_port, dev->root_port);
381
382 return 0;
383}
384
385/* this function must be called with interrupt disabled */
386static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
387{
388 u8 addr; /* R8A66597's address */
389 struct r8a66597_device *dev;
390
391 if (is_hub_limit(urb->dev->devpath)) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700392 dev_err(&urb->dev->dev, "External hub limit reached.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900393 return 0;
394 }
395
396 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
397 if (dev && dev->state >= USB_STATE_ADDRESS)
398 return dev->address;
399
400 for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
401 if (r8a66597->address_map & (1 << addr))
402 continue;
403
404 dbg("alloc_address: r8a66597_addr=%d", addr);
405 r8a66597->address_map |= 1 << addr;
406
407 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
408 return 0;
409
410 return addr;
411 }
412
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700413 dev_err(&urb->dev->dev,
414 "cannot communicate with a USB device more than 10.(%x)\n",
415 r8a66597->address_map);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900416
417 return 0;
418}
419
420/* this function must be called with interrupt disabled */
421static void free_usb_address(struct r8a66597 *r8a66597,
Yoshihiro Shimodad8359332010-03-16 12:29:35 +0900422 struct r8a66597_device *dev, int reset)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900423{
424 int port;
425
426 if (!dev)
427 return;
428
429 dbg("free_addr: addr=%d", dev->address);
430
431 dev->state = USB_STATE_DEFAULT;
432 r8a66597->address_map &= ~(1 << dev->address);
433 dev->address = 0;
Yoshihiro Shimodad8359332010-03-16 12:29:35 +0900434 /*
435 * Only when resetting USB, it is necessary to erase drvdata. When
436 * a usb device with usb hub is disconnect, "dev->udev" is already
437 * freed on usb_desconnect(). So we cannot access the data.
438 */
439 if (reset)
440 dev_set_drvdata(&dev->udev->dev, NULL);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900441 list_del(&dev->device_list);
442 kfree(dev);
443
Magnus Damm719a72b2009-07-17 14:59:55 +0000444 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900445 if (r8a66597->root_hub[port].dev == dev) {
446 r8a66597->root_hub[port].dev = NULL;
447 break;
448 }
449 }
450}
451
452static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
453 u16 mask, u16 loop)
454{
455 u16 tmp;
456 int i = 0;
457
458 do {
459 tmp = r8a66597_read(r8a66597, reg);
460 if (i++ > 1000000) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700461 printk(KERN_ERR "r8a66597: register%lx, loop %x "
462 "is timeout\n", reg, loop);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900463 break;
464 }
465 ndelay(1);
466 } while ((tmp & mask) != loop);
467}
468
469/* this function must be called with interrupt disabled */
470static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
471{
472 u16 tmp;
473
474 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
475 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
476 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
477 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
478}
479
480/* this function must be called with interrupt disabled */
481static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
482{
483 u16 tmp;
484
485 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
486 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
487 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
488 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
489 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
490}
491
492/* this function must be called with interrupt disabled */
493static void clear_all_buffer(struct r8a66597 *r8a66597,
494 struct r8a66597_pipe *pipe)
495{
496 u16 tmp;
497
498 if (!pipe || pipe->info.pipenum == 0)
499 return;
500
501 pipe_stop(r8a66597, pipe);
502 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
503 tmp = r8a66597_read(r8a66597, pipe->pipectr);
504 tmp = r8a66597_read(r8a66597, pipe->pipectr);
505 tmp = r8a66597_read(r8a66597, pipe->pipectr);
506 r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
507}
508
509/* this function must be called with interrupt disabled */
510static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
511 struct r8a66597_pipe *pipe, int toggle)
512{
513 if (toggle)
514 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
515 else
516 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
517}
518
Magnus Damm719a72b2009-07-17 14:59:55 +0000519static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
520{
521 if (r8a66597->pdata->on_chip)
522 return MBW_32;
523 else
524 return MBW_16;
525}
526
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900527/* this function must be called with interrupt disabled */
528static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
529{
Magnus Damm719a72b2009-07-17 14:59:55 +0000530 unsigned short mbw = mbw_value(r8a66597);
531
532 r8a66597_mdfy(r8a66597, mbw | pipenum, mbw | CURPIPE, CFIFOSEL);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900533 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
534}
535
536/* this function must be called with interrupt disabled */
537static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
538 struct r8a66597_pipe *pipe)
539{
Magnus Damm719a72b2009-07-17 14:59:55 +0000540 unsigned short mbw = mbw_value(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900541
Magnus Damm719a72b2009-07-17 14:59:55 +0000542 cfifo_change(r8a66597, 0);
543 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D0FIFOSEL);
544 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D1FIFOSEL);
545
546 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum, mbw | CURPIPE,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900547 pipe->fifosel);
548 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
549}
550
551static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
552{
553 struct r8a66597_pipe *pipe = hep->hcpriv;
554
555 if (usb_pipeendpoint(urb->pipe) == 0)
556 return 0;
557 else
558 return pipe->info.pipenum;
559}
560
561static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
562{
563 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
564
565 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
566}
567
568static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
569 int urb_pipe)
570{
571 if (!dev)
572 return NULL;
573
574 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
575}
576
577/* this function must be called with interrupt disabled */
578static void pipe_toggle_set(struct r8a66597 *r8a66597,
579 struct r8a66597_pipe *pipe,
580 struct urb *urb, int set)
581{
582 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
583 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
584 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
585
586 if (!toggle)
587 return;
588
589 if (set)
590 *toggle |= 1 << endpoint;
591 else
592 *toggle &= ~(1 << endpoint);
593}
594
595/* this function must be called with interrupt disabled */
596static void pipe_toggle_save(struct r8a66597 *r8a66597,
597 struct r8a66597_pipe *pipe,
598 struct urb *urb)
599{
600 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
601 pipe_toggle_set(r8a66597, pipe, urb, 1);
602 else
603 pipe_toggle_set(r8a66597, pipe, urb, 0);
604}
605
606/* this function must be called with interrupt disabled */
607static void pipe_toggle_restore(struct r8a66597 *r8a66597,
608 struct r8a66597_pipe *pipe,
609 struct urb *urb)
610{
611 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
612 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
613 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
614
Yoshihiro Shimodaf6ace2c2007-05-30 20:42:41 +0900615 if (!toggle)
616 return;
617
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900618 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
619}
620
621/* this function must be called with interrupt disabled */
622static void pipe_buffer_setting(struct r8a66597 *r8a66597,
623 struct r8a66597_pipe_info *info)
624{
625 u16 val = 0;
626
627 if (info->pipenum == 0)
628 return;
629
630 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
631 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
632 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
633 if (!info->dir_in)
634 val |= R8A66597_DIR;
635 if (info->type == R8A66597_BULK && info->dir_in)
636 val |= R8A66597_DBLB | R8A66597_SHTNAK;
637 val |= info->type | info->epnum;
638 r8a66597_write(r8a66597, val, PIPECFG);
639
640 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
641 PIPEBUF);
642 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
643 PIPEMAXP);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900644 r8a66597_write(r8a66597, info->interval, PIPEPERI);
645}
646
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900647/* this function must be called with interrupt disabled */
648static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
649{
650 struct r8a66597_pipe_info *info;
651 struct urb *urb = td->urb;
652
653 if (td->pipenum > 0) {
654 info = &td->pipe->info;
655 cfifo_change(r8a66597, 0);
656 pipe_buffer_setting(r8a66597, info);
657
658 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
659 usb_pipeout(urb->pipe)) &&
660 !usb_pipecontrol(urb->pipe)) {
661 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
662 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
663 clear_all_buffer(r8a66597, td->pipe);
664 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
665 usb_pipeout(urb->pipe), 1);
666 }
667 pipe_toggle_restore(r8a66597, td->pipe, urb);
668 }
669}
670
671/* this function must be called with interrupt disabled */
672static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
673 struct usb_endpoint_descriptor *ep)
674{
675 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
676
677 memset(array, 0, sizeof(array));
Julia Lawall2e0fe702008-12-29 11:22:14 +0100678 switch (usb_endpoint_type(ep)) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900679 case USB_ENDPOINT_XFER_BULK:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100680 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900681 array[i++] = 4;
682 else {
683 array[i++] = 3;
684 array[i++] = 5;
685 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900686 break;
687 case USB_ENDPOINT_XFER_INT:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100688 if (usb_endpoint_dir_in(ep)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900689 array[i++] = 6;
690 array[i++] = 7;
691 array[i++] = 8;
692 } else
693 array[i++] = 9;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900694 break;
695 case USB_ENDPOINT_XFER_ISOC:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100696 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900697 array[i++] = 2;
698 else
699 array[i++] = 1;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900700 break;
701 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700702 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900703 return 0;
704 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900705
706 i = 1;
707 min = array[0];
708 while (array[i] != 0) {
709 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
710 min = array[i];
711 i++;
712 }
713
714 return min;
715}
716
717static u16 get_r8a66597_type(__u8 type)
718{
719 u16 r8a66597_type;
720
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900721 switch (type) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900722 case USB_ENDPOINT_XFER_BULK:
723 r8a66597_type = R8A66597_BULK;
724 break;
725 case USB_ENDPOINT_XFER_INT:
726 r8a66597_type = R8A66597_INT;
727 break;
728 case USB_ENDPOINT_XFER_ISOC:
729 r8a66597_type = R8A66597_ISO;
730 break;
731 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700732 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900733 r8a66597_type = 0x0000;
734 break;
735 }
736
737 return r8a66597_type;
738}
739
740static u16 get_bufnum(u16 pipenum)
741{
742 u16 bufnum = 0;
743
744 if (pipenum == 0)
745 bufnum = 0;
746 else if (check_bulk_or_isoc(pipenum))
747 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
748 else if (check_interrupt(pipenum))
749 bufnum = 4 + (pipenum - 6);
750 else
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700751 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900752
753 return bufnum;
754}
755
756static u16 get_buf_bsize(u16 pipenum)
757{
758 u16 buf_bsize = 0;
759
760 if (pipenum == 0)
761 buf_bsize = 3;
762 else if (check_bulk_or_isoc(pipenum))
763 buf_bsize = R8A66597_BUF_BSIZE - 1;
764 else if (check_interrupt(pipenum))
765 buf_bsize = 0;
766 else
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700767 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900768
769 return buf_bsize;
770}
771
772/* this function must be called with interrupt disabled */
773static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
774 struct r8a66597_device *dev,
775 struct r8a66597_pipe *pipe,
776 struct urb *urb)
777{
778 int i;
779 struct r8a66597_pipe_info *info = &pipe->info;
Magnus Damm719a72b2009-07-17 14:59:55 +0000780 unsigned short mbw = mbw_value(r8a66597);
781
782 /* pipe dma is only for external controlles */
783 if (r8a66597->pdata->on_chip)
784 return;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900785
786 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
787 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
788 if ((r8a66597->dma_map & (1 << i)) != 0)
789 continue;
790
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -0700791 dev_info(&dev->udev->dev,
792 "address %d, EndpointAddress 0x%02x use "
793 "DMA FIFO\n", usb_pipedevice(urb->pipe),
794 info->dir_in ?
795 USB_ENDPOINT_DIR_MASK + info->epnum
796 : info->epnum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900797
798 r8a66597->dma_map |= 1 << i;
799 dev->dma_map |= 1 << i;
800 set_pipe_reg_addr(pipe, i);
801
802 cfifo_change(r8a66597, 0);
Magnus Damm719a72b2009-07-17 14:59:55 +0000803 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum,
804 mbw | CURPIPE, pipe->fifosel);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900805
806 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
807 pipe->info.pipenum);
808 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
809 break;
810 }
811 }
812}
813
814/* this function must be called with interrupt disabled */
815static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
816 struct usb_host_endpoint *hep,
817 struct r8a66597_pipe_info *info)
818{
819 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
820 struct r8a66597_pipe *pipe = hep->hcpriv;
821
822 dbg("enable_pipe:");
823
824 pipe->info = *info;
825 set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
826 r8a66597->pipe_cnt[pipe->info.pipenum]++;
827 dev->pipe_cnt[pipe->info.pipenum]++;
828
829 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
830}
831
Paul Mundt27175682010-02-04 06:57:58 +0000832static void r8a66597_urb_done(struct r8a66597 *r8a66597, struct urb *urb,
833 int status)
834__releases(r8a66597->lock)
835__acquires(r8a66597->lock)
836{
837 if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
838 void *ptr;
839
840 for (ptr = urb->transfer_buffer;
841 ptr < urb->transfer_buffer + urb->transfer_buffer_length;
842 ptr += PAGE_SIZE)
843 flush_dcache_page(virt_to_page(ptr));
844 }
845
846 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
847 spin_unlock(&r8a66597->lock);
848 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb, status);
849 spin_lock(&r8a66597->lock);
850}
851
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900852/* this function must be called with interrupt disabled */
853static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
854{
855 struct r8a66597_td *td, *next;
856 struct urb *urb;
857 struct list_head *list = &r8a66597->pipe_queue[pipenum];
858
859 if (list_empty(list))
860 return;
861
862 list_for_each_entry_safe(td, next, list, queue) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900863 if (td->address != address)
864 continue;
865
866 urb = td->urb;
867 list_del(&td->queue);
868 kfree(td);
869
Paul Mundt27175682010-02-04 06:57:58 +0000870 if (urb)
871 r8a66597_urb_done(r8a66597, urb, -ENODEV);
Alan Sterne9df41c2007-08-08 11:48:02 -0400872
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900873 break;
874 }
875}
876
877/* this function must be called with interrupt disabled */
878static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
879 struct r8a66597_device *dev)
880{
881 int check_ep0 = 0;
882 u16 pipenum;
883
884 if (!dev)
885 return;
886
887 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
888 if (!dev->pipe_cnt[pipenum])
889 continue;
890
891 if (!check_ep0) {
892 check_ep0 = 1;
893 force_dequeue(r8a66597, 0, dev->address);
894 }
895
896 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
897 dev->pipe_cnt[pipenum] = 0;
898 force_dequeue(r8a66597, pipenum, dev->address);
899 }
900
901 dbg("disable_pipe");
902
903 r8a66597->dma_map &= ~(dev->dma_map);
904 dev->dma_map = 0;
905}
906
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900907static u16 get_interval(struct urb *urb, __u8 interval)
908{
909 u16 time = 1;
910 int i;
911
912 if (urb->dev->speed == USB_SPEED_HIGH) {
913 if (interval > IITV)
914 time = IITV;
915 else
916 time = interval ? interval - 1 : 0;
917 } else {
918 if (interval > 128) {
919 time = IITV;
920 } else {
921 /* calculate the nearest value for PIPEPERI */
922 for (i = 0; i < 7; i++) {
923 if ((1 << i) < interval &&
924 (1 << (i + 1) > interval))
925 time = 1 << i;
926 }
927 }
928 }
929
930 return time;
931}
932
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900933static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
934{
935 __u8 i;
936 unsigned long time = 1;
937
938 if (usb_pipeisoc(urb->pipe))
939 return 0;
940
941 if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
942 for (i = 0; i < (interval - 1); i++)
943 time *= 2;
944 time = time * 125 / 1000; /* uSOF -> msec */
945 } else {
946 time = interval;
947 }
948
949 return time;
950}
951
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900952/* this function must be called with interrupt disabled */
953static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
954 struct usb_host_endpoint *hep,
955 struct usb_endpoint_descriptor *ep)
956{
957 struct r8a66597_pipe_info info;
958
959 info.pipenum = get_empty_pipenum(r8a66597, ep);
960 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
Julia Lawall2e0fe702008-12-29 11:22:14 +0100961 info.epnum = usb_endpoint_num(ep);
Yoshihiro Shimoda05eac912007-10-03 18:53:28 +0900962 info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
Julia Lawall2e0fe702008-12-29 11:22:14 +0100963 info.type = get_r8a66597_type(usb_endpoint_type(ep));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900964 info.bufnum = get_bufnum(info.pipenum);
965 info.buf_bsize = get_buf_bsize(info.pipenum);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900966 if (info.type == R8A66597_BULK) {
967 info.interval = 0;
968 info.timer_interval = 0;
969 } else {
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900970 info.interval = get_interval(urb, ep->bInterval);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900971 info.timer_interval = get_timer_interval(urb, ep->bInterval);
972 }
Julia Lawall2e0fe702008-12-29 11:22:14 +0100973 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900974 info.dir_in = 1;
975 else
976 info.dir_in = 0;
977
978 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
979}
980
981static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
982{
983 struct r8a66597_device *dev;
984
985 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
986 dev->state = USB_STATE_CONFIGURED;
987}
988
989static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
990 u16 pipenum)
991{
992 if (pipenum == 0 && usb_pipeout(urb->pipe))
993 enable_irq_empty(r8a66597, pipenum);
994 else
995 enable_irq_ready(r8a66597, pipenum);
996
997 if (!usb_pipeisoc(urb->pipe))
998 enable_irq_nrdy(r8a66597, pipenum);
999}
1000
1001static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
1002{
1003 disable_irq_ready(r8a66597, pipenum);
1004 disable_irq_nrdy(r8a66597, pipenum);
1005}
1006
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001007static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
1008{
1009 mod_timer(&r8a66597->rh_timer,
1010 jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
1011}
1012
1013static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
1014 int connect)
1015{
1016 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1017
1018 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1019 rh->scount = R8A66597_MAX_SAMPLING;
1020 if (connect)
1021 rh->port |= 1 << USB_PORT_FEAT_CONNECTION;
1022 else
1023 rh->port &= ~(1 << USB_PORT_FEAT_CONNECTION);
1024 rh->port |= 1 << USB_PORT_FEAT_C_CONNECTION;
1025
1026 r8a66597_root_hub_start_polling(r8a66597);
1027}
1028
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001029/* this function must be called with interrupt disabled */
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001030static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
1031 u16 syssts)
Paul Mundt2c940db2010-02-04 06:58:28 +00001032__releases(r8a66597->lock)
1033__acquires(r8a66597->lock)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001034{
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001035 if (syssts == SE0) {
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001036 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001037 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
Yoshihiro Shimoda1e6159f2009-10-21 20:33:39 +09001038 } else {
1039 if (syssts == FS_JSTS)
1040 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
1041 else if (syssts == LS_JSTS)
1042 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
1043
1044 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
1045 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
1046
1047 if (r8a66597->bus_suspended)
1048 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001049 }
1050
Paul Mundt2c940db2010-02-04 06:58:28 +00001051 spin_unlock(&r8a66597->lock);
Yoshihiro Shimoda1e6159f2009-10-21 20:33:39 +09001052 usb_hcd_poll_rh_status(r8a66597_to_hcd(r8a66597));
Paul Mundt2c940db2010-02-04 06:58:28 +00001053 spin_lock(&r8a66597->lock);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001054}
1055
1056/* this function must be called with interrupt disabled */
1057static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
1058{
1059 u16 speed = get_rh_usb_speed(r8a66597, port);
1060 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1061
Yoshihiro Shimoda1e6159f2009-10-21 20:33:39 +09001062 rh->port &= ~((1 << USB_PORT_FEAT_HIGHSPEED) |
1063 (1 << USB_PORT_FEAT_LOWSPEED));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001064 if (speed == HSMODE)
1065 rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
1066 else if (speed == LSMODE)
1067 rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
1068
1069 rh->port &= ~(1 << USB_PORT_FEAT_RESET);
1070 rh->port |= 1 << USB_PORT_FEAT_ENABLE;
1071}
1072
1073/* this function must be called with interrupt disabled */
1074static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
1075{
1076 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
1077
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001078 disable_r8a66597_pipe_all(r8a66597, dev);
Yoshihiro Shimodad8359332010-03-16 12:29:35 +09001079 free_usb_address(r8a66597, dev, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001080
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001081 start_root_hub_sampling(r8a66597, port, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001082}
1083
1084/* this function must be called with interrupt disabled */
1085static void prepare_setup_packet(struct r8a66597 *r8a66597,
1086 struct r8a66597_td *td)
1087{
1088 int i;
Al Virofd05e722008-04-28 07:00:16 +01001089 __le16 *p = (__le16 *)td->urb->setup_packet;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001090 unsigned long setup_addr = USBREQ;
1091
1092 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1093 DCPMAXP);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001094 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001095
1096 for (i = 0; i < 4; i++) {
Al Virofd05e722008-04-28 07:00:16 +01001097 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001098 setup_addr += 2;
1099 }
1100 r8a66597_write(r8a66597, SUREQ, DCPCTR);
1101}
1102
1103/* this function must be called with interrupt disabled */
1104static void prepare_packet_read(struct r8a66597 *r8a66597,
1105 struct r8a66597_td *td)
1106{
1107 struct urb *urb = td->urb;
1108
1109 if (usb_pipecontrol(urb->pipe)) {
1110 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1111 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1112 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1113 if (urb->actual_length == 0) {
1114 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1115 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1116 }
1117 pipe_irq_disable(r8a66597, td->pipenum);
1118 pipe_start(r8a66597, td->pipe);
1119 pipe_irq_enable(r8a66597, urb, td->pipenum);
1120 } else {
1121 if (urb->actual_length == 0) {
1122 pipe_irq_disable(r8a66597, td->pipenum);
1123 pipe_setting(r8a66597, td);
1124 pipe_stop(r8a66597, td->pipe);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001125 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001126
1127 if (td->pipe->pipetre) {
1128 r8a66597_write(r8a66597, TRCLR,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001129 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001130 r8a66597_write(r8a66597,
Julia Lawalldfa5ec72008-03-04 15:25:11 -08001131 DIV_ROUND_UP
1132 (urb->transfer_buffer_length,
1133 td->maxpacket),
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001134 td->pipe->pipetrn);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001135 r8a66597_bset(r8a66597, TRENB,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001136 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001137 }
1138
1139 pipe_start(r8a66597, td->pipe);
1140 pipe_irq_enable(r8a66597, urb, td->pipenum);
1141 }
1142 }
1143}
1144
1145/* this function must be called with interrupt disabled */
1146static void prepare_packet_write(struct r8a66597 *r8a66597,
1147 struct r8a66597_td *td)
1148{
1149 u16 tmp;
1150 struct urb *urb = td->urb;
1151
1152 if (usb_pipecontrol(urb->pipe)) {
1153 pipe_stop(r8a66597, td->pipe);
1154 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1155 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1156 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1157 if (urb->actual_length == 0) {
1158 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1159 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1160 }
1161 } else {
1162 if (urb->actual_length == 0)
1163 pipe_setting(r8a66597, td);
1164 if (td->pipe->pipetre)
1165 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1166 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001167 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001168
1169 fifo_change_from_pipe(r8a66597, td->pipe);
1170 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1171 if (unlikely((tmp & FRDY) == 0))
1172 pipe_irq_enable(r8a66597, urb, td->pipenum);
1173 else
1174 packet_write(r8a66597, td->pipenum);
1175 pipe_start(r8a66597, td->pipe);
1176}
1177
1178/* this function must be called with interrupt disabled */
1179static void prepare_status_packet(struct r8a66597 *r8a66597,
1180 struct r8a66597_td *td)
1181{
1182 struct urb *urb = td->urb;
1183
1184 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001185 pipe_stop(r8a66597, td->pipe);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001186
1187 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1188 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1189 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1190 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001191 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +09001192 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001193 enable_irq_empty(r8a66597, 0);
1194 } else {
1195 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1196 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1197 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1198 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001199 enable_irq_ready(r8a66597, 0);
1200 }
1201 enable_irq_nrdy(r8a66597, 0);
1202 pipe_start(r8a66597, td->pipe);
1203}
1204
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001205static int is_set_address(unsigned char *setup_packet)
1206{
1207 if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1208 setup_packet[1] == USB_REQ_SET_ADDRESS)
1209 return 1;
1210 else
1211 return 0;
1212}
1213
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001214/* this function must be called with interrupt disabled */
1215static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1216{
1217 BUG_ON(!td);
1218
1219 switch (td->type) {
1220 case USB_PID_SETUP:
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001221 if (is_set_address(td->urb->setup_packet)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001222 td->set_address = 1;
1223 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1224 td->urb);
1225 if (td->urb->setup_packet[2] == 0)
1226 return -EPIPE;
1227 }
1228 prepare_setup_packet(r8a66597, td);
1229 break;
1230 case USB_PID_IN:
1231 prepare_packet_read(r8a66597, td);
1232 break;
1233 case USB_PID_OUT:
1234 prepare_packet_write(r8a66597, td);
1235 break;
1236 case USB_PID_ACK:
1237 prepare_status_packet(r8a66597, td);
1238 break;
1239 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001240 printk(KERN_ERR "r8a66597: invalid type.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001241 break;
1242 }
1243
1244 return 0;
1245}
1246
1247static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1248{
1249 if (usb_pipeisoc(urb->pipe)) {
1250 if (urb->number_of_packets == td->iso_cnt)
1251 return 1;
1252 }
1253
1254 /* control or bulk or interrupt */
1255 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1256 (td->short_packet) || (td->zero_packet))
1257 return 1;
1258
1259 return 0;
1260}
1261
1262/* this function must be called with interrupt disabled */
1263static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1264{
1265 unsigned long time;
1266
1267 BUG_ON(!td);
1268
1269 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1270 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1271 r8a66597->timeout_map |= 1 << td->pipenum;
1272 switch (usb_pipetype(td->urb->pipe)) {
1273 case PIPE_INTERRUPT:
1274 case PIPE_ISOCHRONOUS:
1275 time = 30;
1276 break;
1277 default:
1278 time = 300;
1279 break;
1280 }
1281
1282 mod_timer(&r8a66597->td_timer[td->pipenum],
1283 jiffies + msecs_to_jiffies(time));
1284 }
1285}
1286
1287/* this function must be called with interrupt disabled */
Alan Sterndfd1e532007-08-21 15:36:52 -04001288static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
Alan Stern888fda42007-08-24 15:41:18 -04001289 u16 pipenum, struct urb *urb, int status)
Alan Sterndfd1e532007-08-21 15:36:52 -04001290__releases(r8a66597->lock) __acquires(r8a66597->lock)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001291{
1292 int restart = 0;
1293 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1294
1295 r8a66597->timeout_map &= ~(1 << pipenum);
1296
1297 if (likely(td)) {
Alan Stern888fda42007-08-24 15:41:18 -04001298 if (td->set_address && (status != 0 || urb->unlinked))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001299 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1300
1301 pipe_toggle_save(r8a66597, td->pipe, urb);
1302 list_del(&td->queue);
1303 kfree(td);
1304 }
1305
1306 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1307 restart = 1;
1308
1309 if (likely(urb)) {
1310 if (usb_pipeisoc(urb->pipe))
1311 urb->start_frame = r8a66597_get_frame(hcd);
1312
Paul Mundt27175682010-02-04 06:57:58 +00001313 r8a66597_urb_done(r8a66597, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001314 }
1315
1316 if (restart) {
1317 td = r8a66597_get_td(r8a66597, pipenum);
1318 if (unlikely(!td))
1319 return;
1320
1321 start_transfer(r8a66597, td);
1322 set_td_timer(r8a66597, td);
1323 }
1324}
1325
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001326static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1327{
1328 u16 tmp;
1329 int rcv_len, bufsize, urb_len, size;
1330 u16 *buf;
1331 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1332 struct urb *urb;
1333 int finish = 0;
Alan Sterndfd1e532007-08-21 15:36:52 -04001334 int status = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001335
1336 if (unlikely(!td))
1337 return;
1338 urb = td->urb;
1339
1340 fifo_change_from_pipe(r8a66597, td->pipe);
1341 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1342 if (unlikely((tmp & FRDY) == 0)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001343 pipe_stop(r8a66597, td->pipe);
1344 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001345 printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001346 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001347 return;
1348 }
1349
1350 /* prepare parameters */
1351 rcv_len = tmp & DTLN;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001352 if (usb_pipeisoc(urb->pipe)) {
1353 buf = (u16 *)(urb->transfer_buffer +
1354 urb->iso_frame_desc[td->iso_cnt].offset);
1355 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1356 } else {
1357 buf = (void *)urb->transfer_buffer + urb->actual_length;
1358 urb_len = urb->transfer_buffer_length - urb->actual_length;
1359 }
Alan Sterndfd1e532007-08-21 15:36:52 -04001360 bufsize = min(urb_len, (int) td->maxpacket);
1361 if (rcv_len <= bufsize) {
1362 size = rcv_len;
1363 } else {
1364 size = bufsize;
1365 status = -EOVERFLOW;
1366 finish = 1;
1367 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001368
1369 /* update parameters */
1370 urb->actual_length += size;
1371 if (rcv_len == 0)
1372 td->zero_packet = 1;
Alan Sterndfd1e532007-08-21 15:36:52 -04001373 if (rcv_len < bufsize) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001374 td->short_packet = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001375 }
1376 if (usb_pipeisoc(urb->pipe)) {
1377 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
Alan Sterndfd1e532007-08-21 15:36:52 -04001378 urb->iso_frame_desc[td->iso_cnt].status = status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001379 td->iso_cnt++;
Alan Sterndfd1e532007-08-21 15:36:52 -04001380 finish = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001381 }
1382
1383 /* check transfer finish */
Alan Sternb0d9efb2007-08-21 15:39:21 -04001384 if (finish || check_transfer_finish(td, urb)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001385 pipe_stop(r8a66597, td->pipe);
1386 pipe_irq_disable(r8a66597, pipenum);
1387 finish = 1;
1388 }
1389
1390 /* read fifo */
1391 if (urb->transfer_buffer) {
1392 if (size == 0)
1393 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1394 else
1395 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1396 buf, size);
1397 }
1398
Alan Stern888fda42007-08-24 15:41:18 -04001399 if (finish && pipenum != 0)
1400 finish_request(r8a66597, td, pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001401}
1402
1403static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1404{
1405 u16 tmp;
1406 int bufsize, size;
1407 u16 *buf;
1408 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1409 struct urb *urb;
1410
1411 if (unlikely(!td))
1412 return;
1413 urb = td->urb;
1414
1415 fifo_change_from_pipe(r8a66597, td->pipe);
1416 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1417 if (unlikely((tmp & FRDY) == 0)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001418 pipe_stop(r8a66597, td->pipe);
1419 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001420 printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001421 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001422 return;
1423 }
1424
1425 /* prepare parameters */
1426 bufsize = td->maxpacket;
1427 if (usb_pipeisoc(urb->pipe)) {
1428 buf = (u16 *)(urb->transfer_buffer +
1429 urb->iso_frame_desc[td->iso_cnt].offset);
1430 size = min(bufsize,
1431 (int)urb->iso_frame_desc[td->iso_cnt].length);
1432 } else {
1433 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
Greg Kroah-Hartman16e2e5f2009-03-03 16:44:13 -08001434 size = min_t(u32, bufsize,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001435 urb->transfer_buffer_length - urb->actual_length);
1436 }
1437
1438 /* write fifo */
1439 if (pipenum > 0)
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001440 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001441 if (urb->transfer_buffer) {
1442 r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
1443 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1444 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1445 }
1446
1447 /* update parameters */
1448 urb->actual_length += size;
1449 if (usb_pipeisoc(urb->pipe)) {
1450 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1451 urb->iso_frame_desc[td->iso_cnt].status = 0;
1452 td->iso_cnt++;
1453 }
1454
1455 /* check transfer finish */
1456 if (check_transfer_finish(td, urb)) {
1457 disable_irq_ready(r8a66597, pipenum);
1458 enable_irq_empty(r8a66597, pipenum);
1459 if (!usb_pipeisoc(urb->pipe))
1460 enable_irq_nrdy(r8a66597, pipenum);
1461 } else
1462 pipe_irq_enable(r8a66597, urb, pipenum);
1463}
1464
1465
Alan Stern888fda42007-08-24 15:41:18 -04001466static void check_next_phase(struct r8a66597 *r8a66597, int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001467{
1468 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1469 struct urb *urb;
1470 u8 finish = 0;
1471
1472 if (unlikely(!td))
1473 return;
1474 urb = td->urb;
1475
1476 switch (td->type) {
1477 case USB_PID_IN:
1478 case USB_PID_OUT:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001479 if (check_transfer_finish(td, urb))
1480 td->type = USB_PID_ACK;
1481 break;
1482 case USB_PID_SETUP:
Alan Sterneb231052007-08-21 15:40:36 -04001483 if (urb->transfer_buffer_length == urb->actual_length)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001484 td->type = USB_PID_ACK;
Alan Sterneb231052007-08-21 15:40:36 -04001485 else if (usb_pipeout(urb->pipe))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001486 td->type = USB_PID_OUT;
1487 else
1488 td->type = USB_PID_IN;
1489 break;
1490 case USB_PID_ACK:
1491 finish = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001492 break;
1493 }
1494
Alan Stern888fda42007-08-24 15:41:18 -04001495 if (finish || status != 0 || urb->unlinked)
1496 finish_request(r8a66597, td, 0, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001497 else
1498 start_transfer(r8a66597, td);
1499}
1500
Alan Stern888fda42007-08-24 15:41:18 -04001501static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001502{
1503 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1504
Alan Stern888fda42007-08-24 15:41:18 -04001505 if (td) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001506 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1507
1508 if (pid == PID_NAK)
Alan Stern888fda42007-08-24 15:41:18 -04001509 return -ECONNRESET;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001510 else
Alan Stern888fda42007-08-24 15:41:18 -04001511 return -EPIPE;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001512 }
Alan Stern888fda42007-08-24 15:41:18 -04001513 return 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001514}
1515
1516static void irq_pipe_ready(struct r8a66597 *r8a66597)
1517{
1518 u16 check;
1519 u16 pipenum;
1520 u16 mask;
1521 struct r8a66597_td *td;
1522
1523 mask = r8a66597_read(r8a66597, BRDYSTS)
1524 & r8a66597_read(r8a66597, BRDYENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001525 r8a66597_write(r8a66597, ~mask, BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001526 if (mask & BRDY0) {
1527 td = r8a66597_get_td(r8a66597, 0);
1528 if (td && td->type == USB_PID_IN)
1529 packet_read(r8a66597, 0);
1530 else
1531 pipe_irq_disable(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001532 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001533 }
1534
1535 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1536 check = 1 << pipenum;
1537 if (mask & check) {
1538 td = r8a66597_get_td(r8a66597, pipenum);
1539 if (unlikely(!td))
1540 continue;
1541
1542 if (td->type == USB_PID_IN)
1543 packet_read(r8a66597, pipenum);
1544 else if (td->type == USB_PID_OUT)
1545 packet_write(r8a66597, pipenum);
1546 }
1547 }
1548}
1549
1550static void irq_pipe_empty(struct r8a66597 *r8a66597)
1551{
1552 u16 tmp;
1553 u16 check;
1554 u16 pipenum;
1555 u16 mask;
1556 struct r8a66597_td *td;
1557
1558 mask = r8a66597_read(r8a66597, BEMPSTS)
1559 & r8a66597_read(r8a66597, BEMPENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001560 r8a66597_write(r8a66597, ~mask, BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001561 if (mask & BEMP0) {
1562 cfifo_change(r8a66597, 0);
1563 td = r8a66597_get_td(r8a66597, 0);
1564 if (td && td->type != USB_PID_OUT)
1565 disable_irq_empty(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001566 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001567 }
1568
1569 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1570 check = 1 << pipenum;
1571 if (mask & check) {
1572 struct r8a66597_td *td;
1573 td = r8a66597_get_td(r8a66597, pipenum);
1574 if (unlikely(!td))
1575 continue;
1576
1577 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1578 if ((tmp & INBUFM) == 0) {
1579 disable_irq_empty(r8a66597, pipenum);
1580 pipe_irq_disable(r8a66597, pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001581 finish_request(r8a66597, td, pipenum, td->urb,
1582 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001583 }
1584 }
1585 }
1586}
1587
1588static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1589{
1590 u16 check;
1591 u16 pipenum;
1592 u16 mask;
Alan Stern888fda42007-08-24 15:41:18 -04001593 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001594
1595 mask = r8a66597_read(r8a66597, NRDYSTS)
1596 & r8a66597_read(r8a66597, NRDYENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001597 r8a66597_write(r8a66597, ~mask, NRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001598 if (mask & NRDY0) {
1599 cfifo_change(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001600 status = get_urb_error(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001601 pipe_irq_disable(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001602 check_next_phase(r8a66597, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001603 }
1604
1605 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1606 check = 1 << pipenum;
1607 if (mask & check) {
1608 struct r8a66597_td *td;
1609 td = r8a66597_get_td(r8a66597, pipenum);
1610 if (unlikely(!td))
1611 continue;
1612
Alan Stern888fda42007-08-24 15:41:18 -04001613 status = get_urb_error(r8a66597, pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001614 pipe_irq_disable(r8a66597, pipenum);
1615 pipe_stop(r8a66597, td->pipe);
Alan Stern888fda42007-08-24 15:41:18 -04001616 finish_request(r8a66597, td, pipenum, td->urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001617 }
1618 }
1619}
1620
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001621static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1622{
1623 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1624 u16 intsts0, intsts1, intsts2;
1625 u16 intenb0, intenb1, intenb2;
1626 u16 mask0, mask1, mask2;
Alan Stern888fda42007-08-24 15:41:18 -04001627 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001628
1629 spin_lock(&r8a66597->lock);
1630
1631 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1632 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1633 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1634 intenb0 = r8a66597_read(r8a66597, INTENB0);
1635 intenb1 = r8a66597_read(r8a66597, INTENB1);
1636 intenb2 = r8a66597_read(r8a66597, INTENB2);
1637
1638 mask2 = intsts2 & intenb2;
1639 mask1 = intsts1 & intenb1;
1640 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1641 if (mask2) {
1642 if (mask2 & ATTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001643 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001644 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1645
1646 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001647 start_root_hub_sampling(r8a66597, 1, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001648 }
1649 if (mask2 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001650 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001651 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1652 r8a66597_usb_disconnect(r8a66597, 1);
1653 }
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09001654 if (mask2 & BCHG) {
1655 r8a66597_write(r8a66597, ~BCHG, INTSTS2);
1656 r8a66597_bclr(r8a66597, BCHGE, INTENB2);
1657 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1658 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001659 }
1660
1661 if (mask1) {
1662 if (mask1 & ATTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001663 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001664 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1665
1666 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001667 start_root_hub_sampling(r8a66597, 0, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001668 }
1669 if (mask1 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001670 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001671 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1672 r8a66597_usb_disconnect(r8a66597, 0);
1673 }
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09001674 if (mask1 & BCHG) {
1675 r8a66597_write(r8a66597, ~BCHG, INTSTS1);
1676 r8a66597_bclr(r8a66597, BCHGE, INTENB1);
1677 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1678 }
1679
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001680 if (mask1 & SIGN) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001681 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001682 status = get_urb_error(r8a66597, 0);
1683 check_next_phase(r8a66597, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001684 }
1685 if (mask1 & SACK) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001686 r8a66597_write(r8a66597, ~SACK, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001687 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001688 }
1689 }
1690 if (mask0) {
1691 if (mask0 & BRDY)
1692 irq_pipe_ready(r8a66597);
1693 if (mask0 & BEMP)
1694 irq_pipe_empty(r8a66597);
1695 if (mask0 & NRDY)
1696 irq_pipe_nrdy(r8a66597);
1697 }
1698
1699 spin_unlock(&r8a66597->lock);
1700 return IRQ_HANDLED;
1701}
1702
1703/* this function must be called with interrupt disabled */
1704static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1705{
1706 u16 tmp;
1707 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1708
1709 if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
1710 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1711
1712 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1713 if ((tmp & USBRST) == USBRST) {
1714 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1715 dvstctr_reg);
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001716 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001717 } else
1718 r8a66597_usb_connect(r8a66597, port);
1719 }
1720
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001721 if (!(rh->port & (1 << USB_PORT_FEAT_CONNECTION))) {
1722 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1723 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1724 }
1725
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001726 if (rh->scount > 0) {
1727 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1728 if (tmp == rh->old_syssts) {
1729 rh->scount--;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001730 if (rh->scount == 0)
1731 r8a66597_check_syssts(r8a66597, port, tmp);
1732 else
1733 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001734 } else {
1735 rh->scount = R8A66597_MAX_SAMPLING;
1736 rh->old_syssts = tmp;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001737 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001738 }
1739 }
1740}
1741
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001742static void r8a66597_interval_timer(unsigned long _r8a66597)
1743{
1744 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1745 unsigned long flags;
1746 u16 pipenum;
1747 struct r8a66597_td *td;
1748
1749 spin_lock_irqsave(&r8a66597->lock, flags);
1750
1751 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1752 if (!(r8a66597->interval_map & (1 << pipenum)))
1753 continue;
1754 if (timer_pending(&r8a66597->interval_timer[pipenum]))
1755 continue;
1756
1757 td = r8a66597_get_td(r8a66597, pipenum);
1758 if (td)
1759 start_transfer(r8a66597, td);
1760 }
1761
1762 spin_unlock_irqrestore(&r8a66597->lock, flags);
1763}
1764
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001765static void r8a66597_td_timer(unsigned long _r8a66597)
1766{
1767 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1768 unsigned long flags;
1769 u16 pipenum;
1770 struct r8a66597_td *td, *new_td = NULL;
1771 struct r8a66597_pipe *pipe;
1772
1773 spin_lock_irqsave(&r8a66597->lock, flags);
1774 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1775 if (!(r8a66597->timeout_map & (1 << pipenum)))
1776 continue;
1777 if (timer_pending(&r8a66597->td_timer[pipenum]))
1778 continue;
1779
1780 td = r8a66597_get_td(r8a66597, pipenum);
1781 if (!td) {
1782 r8a66597->timeout_map &= ~(1 << pipenum);
1783 continue;
1784 }
1785
1786 if (td->urb->actual_length) {
1787 set_td_timer(r8a66597, td);
1788 break;
1789 }
1790
1791 pipe = td->pipe;
1792 pipe_stop(r8a66597, pipe);
1793
1794 new_td = td;
1795 do {
1796 list_move_tail(&new_td->queue,
1797 &r8a66597->pipe_queue[pipenum]);
1798 new_td = r8a66597_get_td(r8a66597, pipenum);
1799 if (!new_td) {
1800 new_td = td;
1801 break;
1802 }
1803 } while (td != new_td && td->address == new_td->address);
1804
1805 start_transfer(r8a66597, new_td);
1806
1807 if (td == new_td)
1808 r8a66597->timeout_map &= ~(1 << pipenum);
1809 else
1810 set_td_timer(r8a66597, new_td);
1811 break;
1812 }
1813 spin_unlock_irqrestore(&r8a66597->lock, flags);
1814}
1815
1816static void r8a66597_timer(unsigned long _r8a66597)
1817{
1818 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1819 unsigned long flags;
Yoshihiro Shimoda58639642008-11-11 16:47:21 +09001820 int port;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001821
1822 spin_lock_irqsave(&r8a66597->lock, flags);
1823
Magnus Damm719a72b2009-07-17 14:59:55 +00001824 for (port = 0; port < r8a66597->max_root_hub; port++)
Yoshihiro Shimoda58639642008-11-11 16:47:21 +09001825 r8a66597_root_hub_control(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001826
1827 spin_unlock_irqrestore(&r8a66597->lock, flags);
1828}
1829
1830static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1831{
1832 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1833
1834 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1835 (urb->dev->state == USB_STATE_CONFIGURED))
1836 return 1;
1837 else
1838 return 0;
1839}
1840
1841static int r8a66597_start(struct usb_hcd *hcd)
1842{
1843 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001844
1845 hcd->state = HC_STATE_RUNNING;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001846 return enable_controller(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001847}
1848
1849static void r8a66597_stop(struct usb_hcd *hcd)
1850{
1851 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1852
1853 disable_controller(r8a66597);
1854}
1855
1856static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1857{
1858 unsigned int usb_address = usb_pipedevice(urb->pipe);
1859 u16 root_port, hub_port;
1860
1861 if (usb_address == 0) {
Magnus Damm719a72b2009-07-17 14:59:55 +00001862 get_port_number(r8a66597, urb->dev->devpath,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001863 &root_port, &hub_port);
1864 set_devadd_reg(r8a66597, 0,
1865 get_r8a66597_usb_speed(urb->dev->speed),
1866 get_parent_r8a66597_address(r8a66597, urb->dev),
1867 hub_port, root_port);
1868 }
1869}
1870
1871static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1872 struct urb *urb,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001873 struct usb_host_endpoint *hep)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001874{
1875 struct r8a66597_td *td;
1876 u16 pipenum;
1877
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001878 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001879 if (td == NULL)
1880 return NULL;
1881
1882 pipenum = r8a66597_get_pipenum(urb, hep);
1883 td->pipenum = pipenum;
1884 td->pipe = hep->hcpriv;
1885 td->urb = urb;
1886 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1887 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1888 !usb_pipein(urb->pipe));
1889 if (usb_pipecontrol(urb->pipe))
1890 td->type = USB_PID_SETUP;
1891 else if (usb_pipein(urb->pipe))
1892 td->type = USB_PID_IN;
1893 else
1894 td->type = USB_PID_OUT;
1895 INIT_LIST_HEAD(&td->queue);
1896
1897 return td;
1898}
1899
1900static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001901 struct urb *urb,
1902 gfp_t mem_flags)
1903{
Alan Sterne9df41c2007-08-08 11:48:02 -04001904 struct usb_host_endpoint *hep = urb->ep;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001905 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1906 struct r8a66597_td *td = NULL;
Alan Sterne9df41c2007-08-08 11:48:02 -04001907 int ret, request = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001908 unsigned long flags;
1909
1910 spin_lock_irqsave(&r8a66597->lock, flags);
1911 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1912 ret = -ENODEV;
Alan Sterne9df41c2007-08-08 11:48:02 -04001913 goto error_not_linked;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001914 }
1915
Alan Sterne9df41c2007-08-08 11:48:02 -04001916 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1917 if (ret)
1918 goto error_not_linked;
1919
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001920 if (!hep->hcpriv) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001921 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1922 GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001923 if (!hep->hcpriv) {
1924 ret = -ENOMEM;
1925 goto error;
1926 }
1927 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1928 if (usb_pipeendpoint(urb->pipe))
1929 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1930 }
1931
1932 if (unlikely(check_pipe_config(r8a66597, urb)))
1933 init_pipe_config(r8a66597, urb);
1934
1935 set_address_zero(r8a66597, urb);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001936 td = r8a66597_make_td(r8a66597, urb, hep);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001937 if (td == NULL) {
1938 ret = -ENOMEM;
1939 goto error;
1940 }
1941 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1942 request = 1;
1943 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001944 urb->hcpriv = td;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001945
1946 if (request) {
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001947 if (td->pipe->info.timer_interval) {
1948 r8a66597->interval_map |= 1 << td->pipenum;
1949 mod_timer(&r8a66597->interval_timer[td->pipenum],
1950 jiffies + msecs_to_jiffies(
1951 td->pipe->info.timer_interval));
1952 } else {
1953 ret = start_transfer(r8a66597, td);
1954 if (ret < 0) {
1955 list_del(&td->queue);
1956 kfree(td);
1957 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001958 }
1959 } else
1960 set_td_timer(r8a66597, td);
1961
1962error:
Alan Sterne9df41c2007-08-08 11:48:02 -04001963 if (ret)
1964 usb_hcd_unlink_urb_from_ep(hcd, urb);
1965error_not_linked:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001966 spin_unlock_irqrestore(&r8a66597->lock, flags);
1967 return ret;
1968}
1969
Alan Sterne9df41c2007-08-08 11:48:02 -04001970static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1971 int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001972{
1973 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1974 struct r8a66597_td *td;
1975 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001976 int rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001977
1978 spin_lock_irqsave(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001979 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1980 if (rc)
1981 goto done;
1982
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001983 if (urb->hcpriv) {
1984 td = urb->hcpriv;
1985 pipe_stop(r8a66597, td->pipe);
1986 pipe_irq_disable(r8a66597, td->pipenum);
1987 disable_irq_empty(r8a66597, td->pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001988 finish_request(r8a66597, td, td->pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001989 }
Alan Sterne9df41c2007-08-08 11:48:02 -04001990 done:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001991 spin_unlock_irqrestore(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001992 return rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001993}
1994
1995static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1996 struct usb_host_endpoint *hep)
1997{
1998 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1999 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
2000 struct r8a66597_td *td;
2001 struct urb *urb = NULL;
2002 u16 pipenum;
2003 unsigned long flags;
2004
2005 if (pipe == NULL)
2006 return;
2007 pipenum = pipe->info.pipenum;
2008
2009 if (pipenum == 0) {
2010 kfree(hep->hcpriv);
2011 hep->hcpriv = NULL;
2012 return;
2013 }
2014
2015 spin_lock_irqsave(&r8a66597->lock, flags);
2016 pipe_stop(r8a66597, pipe);
2017 pipe_irq_disable(r8a66597, pipenum);
2018 disable_irq_empty(r8a66597, pipenum);
2019 td = r8a66597_get_td(r8a66597, pipenum);
2020 if (td)
2021 urb = td->urb;
Alan Stern888fda42007-08-24 15:41:18 -04002022 finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002023 kfree(hep->hcpriv);
2024 hep->hcpriv = NULL;
2025 spin_unlock_irqrestore(&r8a66597->lock, flags);
2026}
2027
2028static int r8a66597_get_frame(struct usb_hcd *hcd)
2029{
2030 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2031 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
2032}
2033
2034static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
2035{
2036 int chix;
2037
2038 if (udev->state == USB_STATE_CONFIGURED &&
2039 udev->parent && udev->parent->devnum > 1 &&
2040 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
2041 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
2042
2043 for (chix = 0; chix < udev->maxchild; chix++) {
2044 struct usb_device *childdev = udev->children[chix];
2045
2046 if (childdev)
2047 collect_usb_address_map(childdev, map);
2048 }
2049}
2050
2051/* this function must be called with interrupt disabled */
2052static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
2053 int addr)
2054{
2055 struct r8a66597_device *dev;
2056 struct list_head *list = &r8a66597->child_device;
2057
2058 list_for_each_entry(dev, list, device_list) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002059 if (dev->usb_address != addr)
2060 continue;
2061
2062 return dev;
2063 }
2064
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002065 printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002066 return NULL;
2067}
2068
2069static void update_usb_address_map(struct r8a66597 *r8a66597,
2070 struct usb_device *root_hub,
2071 unsigned long *map)
2072{
2073 int i, j, addr;
2074 unsigned long diff;
2075 unsigned long flags;
2076
2077 for (i = 0; i < 4; i++) {
2078 diff = r8a66597->child_connect_map[i] ^ map[i];
2079 if (!diff)
2080 continue;
2081
2082 for (j = 0; j < 32; j++) {
2083 if (!(diff & (1 << j)))
2084 continue;
2085
2086 addr = i * 32 + j;
2087 if (map[i] & (1 << j))
2088 set_child_connect_map(r8a66597, addr);
2089 else {
2090 struct r8a66597_device *dev;
2091
2092 spin_lock_irqsave(&r8a66597->lock, flags);
2093 dev = get_r8a66597_device(r8a66597, addr);
2094 disable_r8a66597_pipe_all(r8a66597, dev);
Yoshihiro Shimodad8359332010-03-16 12:29:35 +09002095 free_usb_address(r8a66597, dev, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002096 put_child_connect_map(r8a66597, addr);
2097 spin_unlock_irqrestore(&r8a66597->lock, flags);
2098 }
2099 }
2100 }
2101}
2102
2103static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
2104 struct usb_hcd *hcd)
2105{
2106 struct usb_bus *bus;
2107 unsigned long now_map[4];
2108
2109 memset(now_map, 0, sizeof(now_map));
2110
2111 list_for_each_entry(bus, &usb_bus_list, bus_list) {
2112 if (!bus->root_hub)
2113 continue;
2114
2115 if (bus->busnum != hcd->self.busnum)
2116 continue;
2117
2118 collect_usb_address_map(bus->root_hub, now_map);
2119 update_usb_address_map(r8a66597, bus->root_hub, now_map);
2120 }
2121}
2122
2123static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2124{
2125 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2126 unsigned long flags;
2127 int i;
2128
2129 r8a66597_check_detect_child(r8a66597, hcd);
2130
2131 spin_lock_irqsave(&r8a66597->lock, flags);
2132
2133 *buf = 0; /* initialize (no change) */
2134
Magnus Damm719a72b2009-07-17 14:59:55 +00002135 for (i = 0; i < r8a66597->max_root_hub; i++) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002136 if (r8a66597->root_hub[i].port & 0xffff0000)
2137 *buf |= 1 << (i + 1);
2138 }
2139
2140 spin_unlock_irqrestore(&r8a66597->lock, flags);
2141
2142 return (*buf != 0);
2143}
2144
2145static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2146 struct usb_hub_descriptor *desc)
2147{
2148 desc->bDescriptorType = 0x29;
2149 desc->bHubContrCurrent = 0;
Magnus Damm719a72b2009-07-17 14:59:55 +00002150 desc->bNbrPorts = r8a66597->max_root_hub;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002151 desc->bDescLength = 9;
2152 desc->bPwrOn2PwrGood = 0;
2153 desc->wHubCharacteristics = cpu_to_le16(0x0011);
Magnus Damm719a72b2009-07-17 14:59:55 +00002154 desc->bitmap[0] = ((1 << r8a66597->max_root_hub) - 1) << 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002155 desc->bitmap[1] = ~0;
2156}
2157
2158static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2159 u16 wIndex, char *buf, u16 wLength)
2160{
2161 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2162 int ret;
2163 int port = (wIndex & 0x00FF) - 1;
2164 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2165 unsigned long flags;
2166
2167 ret = 0;
2168
2169 spin_lock_irqsave(&r8a66597->lock, flags);
2170 switch (typeReq) {
2171 case ClearHubFeature:
2172 case SetHubFeature:
2173 switch (wValue) {
2174 case C_HUB_OVER_CURRENT:
2175 case C_HUB_LOCAL_POWER:
2176 break;
2177 default:
2178 goto error;
2179 }
2180 break;
2181 case ClearPortFeature:
Magnus Damm719a72b2009-07-17 14:59:55 +00002182 if (wIndex > r8a66597->max_root_hub)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002183 goto error;
2184 if (wLength != 0)
2185 goto error;
2186
2187 switch (wValue) {
2188 case USB_PORT_FEAT_ENABLE:
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002189 rh->port &= ~(1 << USB_PORT_FEAT_POWER);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002190 break;
2191 case USB_PORT_FEAT_SUSPEND:
2192 break;
2193 case USB_PORT_FEAT_POWER:
2194 r8a66597_port_power(r8a66597, port, 0);
2195 break;
2196 case USB_PORT_FEAT_C_ENABLE:
2197 case USB_PORT_FEAT_C_SUSPEND:
2198 case USB_PORT_FEAT_C_CONNECTION:
2199 case USB_PORT_FEAT_C_OVER_CURRENT:
2200 case USB_PORT_FEAT_C_RESET:
2201 break;
2202 default:
2203 goto error;
2204 }
2205 rh->port &= ~(1 << wValue);
2206 break;
2207 case GetHubDescriptor:
2208 r8a66597_hub_descriptor(r8a66597,
2209 (struct usb_hub_descriptor *)buf);
2210 break;
2211 case GetHubStatus:
2212 *buf = 0x00;
2213 break;
2214 case GetPortStatus:
Magnus Damm719a72b2009-07-17 14:59:55 +00002215 if (wIndex > r8a66597->max_root_hub)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002216 goto error;
Al Virofd05e722008-04-28 07:00:16 +01002217 *(__le32 *)buf = cpu_to_le32(rh->port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002218 break;
2219 case SetPortFeature:
Magnus Damm719a72b2009-07-17 14:59:55 +00002220 if (wIndex > r8a66597->max_root_hub)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002221 goto error;
2222 if (wLength != 0)
2223 goto error;
2224
2225 switch (wValue) {
2226 case USB_PORT_FEAT_SUSPEND:
2227 break;
2228 case USB_PORT_FEAT_POWER:
2229 r8a66597_port_power(r8a66597, port, 1);
2230 rh->port |= (1 << USB_PORT_FEAT_POWER);
2231 break;
2232 case USB_PORT_FEAT_RESET: {
2233 struct r8a66597_device *dev = rh->dev;
2234
2235 rh->port |= (1 << USB_PORT_FEAT_RESET);
2236
2237 disable_r8a66597_pipe_all(r8a66597, dev);
Yoshihiro Shimodad8359332010-03-16 12:29:35 +09002238 free_usb_address(r8a66597, dev, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002239
2240 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2241 get_dvstctr_reg(port));
2242 mod_timer(&r8a66597->rh_timer,
2243 jiffies + msecs_to_jiffies(50));
2244 }
2245 break;
2246 default:
2247 goto error;
2248 }
2249 rh->port |= 1 << wValue;
2250 break;
2251 default:
2252error:
2253 ret = -EPIPE;
2254 break;
2255 }
2256
2257 spin_unlock_irqrestore(&r8a66597->lock, flags);
2258 return ret;
2259}
2260
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002261#if defined(CONFIG_PM)
2262static int r8a66597_bus_suspend(struct usb_hcd *hcd)
2263{
2264 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2265 int port;
2266
2267 dbg("%s", __func__);
2268
Magnus Damm719a72b2009-07-17 14:59:55 +00002269 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002270 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2271 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2272
2273 if (!(rh->port & (1 << USB_PORT_FEAT_ENABLE)))
2274 continue;
2275
2276 dbg("suspend port = %d", port);
2277 r8a66597_bclr(r8a66597, UACT, dvstctr_reg); /* suspend */
2278 rh->port |= 1 << USB_PORT_FEAT_SUSPEND;
2279
2280 if (rh->dev->udev->do_remote_wakeup) {
2281 msleep(3); /* waiting last SOF */
2282 r8a66597_bset(r8a66597, RWUPE, dvstctr_reg);
2283 r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
2284 r8a66597_bset(r8a66597, BCHGE, get_intenb_reg(port));
2285 }
2286 }
2287
2288 r8a66597->bus_suspended = 1;
2289
2290 return 0;
2291}
2292
2293static int r8a66597_bus_resume(struct usb_hcd *hcd)
2294{
2295 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2296 int port;
2297
2298 dbg("%s", __func__);
2299
Magnus Damm719a72b2009-07-17 14:59:55 +00002300 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002301 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2302 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2303
2304 if (!(rh->port & (1 << USB_PORT_FEAT_SUSPEND)))
2305 continue;
2306
2307 dbg("resume port = %d", port);
2308 rh->port &= ~(1 << USB_PORT_FEAT_SUSPEND);
2309 rh->port |= 1 << USB_PORT_FEAT_C_SUSPEND;
2310 r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
2311 msleep(50);
2312 r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
2313 }
2314
2315 return 0;
2316
2317}
2318#else
2319#define r8a66597_bus_suspend NULL
2320#define r8a66597_bus_resume NULL
2321#endif
2322
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002323static struct hc_driver r8a66597_hc_driver = {
2324 .description = hcd_name,
2325 .hcd_priv_size = sizeof(struct r8a66597),
2326 .irq = r8a66597_irq,
2327
2328 /*
2329 * generic hardware linkage
2330 */
2331 .flags = HCD_USB2,
2332
2333 .start = r8a66597_start,
2334 .stop = r8a66597_stop,
2335
2336 /*
2337 * managing i/o requests and associated device resources
2338 */
2339 .urb_enqueue = r8a66597_urb_enqueue,
2340 .urb_dequeue = r8a66597_urb_dequeue,
2341 .endpoint_disable = r8a66597_endpoint_disable,
2342
2343 /*
2344 * periodic schedule support
2345 */
2346 .get_frame_number = r8a66597_get_frame,
2347
2348 /*
2349 * root hub support
2350 */
2351 .hub_status_data = r8a66597_hub_status_data,
2352 .hub_control = r8a66597_hub_control,
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002353 .bus_suspend = r8a66597_bus_suspend,
2354 .bus_resume = r8a66597_bus_resume,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002355};
2356
2357#if defined(CONFIG_PM)
Magnus Dammae1cef62009-07-17 14:52:05 +00002358static int r8a66597_suspend(struct device *dev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002359{
Magnus Dammae1cef62009-07-17 14:52:05 +00002360 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002361 int port;
2362
2363 dbg("%s", __func__);
2364
2365 disable_controller(r8a66597);
2366
Magnus Damm719a72b2009-07-17 14:59:55 +00002367 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002368 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2369
2370 rh->port = 0x00000000;
2371 }
2372
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002373 return 0;
2374}
2375
Magnus Dammae1cef62009-07-17 14:52:05 +00002376static int r8a66597_resume(struct device *dev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002377{
Magnus Dammae1cef62009-07-17 14:52:05 +00002378 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002379 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2380
2381 dbg("%s", __func__);
2382
2383 enable_controller(r8a66597);
2384 usb_root_hub_lost_power(hcd->self.root_hub);
2385
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002386 return 0;
2387}
Magnus Dammae1cef62009-07-17 14:52:05 +00002388
Alexey Dobriyan47145212009-12-14 18:00:08 -08002389static const struct dev_pm_ops r8a66597_dev_pm_ops = {
Magnus Dammae1cef62009-07-17 14:52:05 +00002390 .suspend = r8a66597_suspend,
2391 .resume = r8a66597_resume,
Yoshihiro Shimoda3725f282009-07-29 09:24:41 +00002392 .poweroff = r8a66597_suspend,
2393 .restore = r8a66597_resume,
Magnus Dammae1cef62009-07-17 14:52:05 +00002394};
2395
2396#define R8A66597_DEV_PM_OPS (&r8a66597_dev_pm_ops)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002397#else /* if defined(CONFIG_PM) */
Magnus Dammae1cef62009-07-17 14:52:05 +00002398#define R8A66597_DEV_PM_OPS NULL
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002399#endif
2400
2401static int __init_or_module r8a66597_remove(struct platform_device *pdev)
2402{
2403 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2404 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2405
2406 del_timer_sync(&r8a66597->rh_timer);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002407 usb_remove_hcd(hcd);
Yoshihiro Shimodaca0677a2007-10-05 15:53:12 +09002408 iounmap((void *)r8a66597->reg);
Magnus Damm719a72b2009-07-17 14:59:55 +00002409#ifdef CONFIG_HAVE_CLK
2410 if (r8a66597->pdata->on_chip)
2411 clk_put(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +09002412#endif
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002413 usb_put_hcd(hcd);
2414 return 0;
2415}
2416
Uwe Kleine-König8864bd82009-03-28 00:27:00 +01002417static int __devinit r8a66597_probe(struct platform_device *pdev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002418{
Magnus Damm719a72b2009-07-17 14:59:55 +00002419#ifdef CONFIG_HAVE_CLK
Magnus Damm765786e2008-10-31 20:22:38 +09002420 char clk_name[8];
2421#endif
Marc Zyngier27140212008-08-18 13:08:42 +02002422 struct resource *res = NULL, *ires;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002423 int irq = -1;
2424 void __iomem *reg = NULL;
2425 struct usb_hcd *hcd = NULL;
2426 struct r8a66597 *r8a66597;
2427 int ret = 0;
2428 int i;
Yoshihiro Shimoda0bf32b82008-06-27 19:09:55 +09002429 unsigned long irq_trigger;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002430
2431 if (pdev->dev.dma_mask) {
2432 ret = -EINVAL;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002433 dev_err(&pdev->dev, "dma not supported\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002434 goto clean_up;
2435 }
2436
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002437 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002438 if (!res) {
2439 ret = -ENODEV;
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002440 dev_err(&pdev->dev, "platform_get_resource error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002441 goto clean_up;
2442 }
2443
Marc Zyngier27140212008-08-18 13:08:42 +02002444 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2445 if (!ires) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002446 ret = -ENODEV;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002447 dev_err(&pdev->dev,
2448 "platform_get_resource IORESOURCE_IRQ error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002449 goto clean_up;
2450 }
2451
Marc Zyngier27140212008-08-18 13:08:42 +02002452 irq = ires->start;
2453 irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
2454
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002455 reg = ioremap(res->start, resource_size(res));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002456 if (reg == NULL) {
2457 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002458 dev_err(&pdev->dev, "ioremap error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002459 goto clean_up;
2460 }
2461
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +09002462 if (pdev->dev.platform_data == NULL) {
2463 dev_err(&pdev->dev, "no platform data\n");
2464 ret = -ENODEV;
2465 goto clean_up;
2466 }
2467
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002468 /* initialize hcd */
2469 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2470 if (!hcd) {
2471 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002472 dev_err(&pdev->dev, "Failed to create hcd\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002473 goto clean_up;
2474 }
2475 r8a66597 = hcd_to_r8a66597(hcd);
2476 memset(r8a66597, 0, sizeof(struct r8a66597));
2477 dev_set_drvdata(&pdev->dev, r8a66597);
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +09002478 r8a66597->pdata = pdev->dev.platform_data;
2479 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002480
Magnus Damm719a72b2009-07-17 14:59:55 +00002481 if (r8a66597->pdata->on_chip) {
2482#ifdef CONFIG_HAVE_CLK
2483 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
2484 r8a66597->clk = clk_get(&pdev->dev, clk_name);
2485 if (IS_ERR(r8a66597->clk)) {
2486 dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
2487 clk_name);
2488 ret = PTR_ERR(r8a66597->clk);
2489 goto clean_up2;
2490 }
Magnus Damm765786e2008-10-31 20:22:38 +09002491#endif
Magnus Damm719a72b2009-07-17 14:59:55 +00002492 r8a66597->max_root_hub = 1;
2493 } else
2494 r8a66597->max_root_hub = 2;
Magnus Damm765786e2008-10-31 20:22:38 +09002495
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002496 spin_lock_init(&r8a66597->lock);
2497 init_timer(&r8a66597->rh_timer);
2498 r8a66597->rh_timer.function = r8a66597_timer;
2499 r8a66597->rh_timer.data = (unsigned long)r8a66597;
2500 r8a66597->reg = (unsigned long)reg;
2501
Magnus Damme5ff15b2010-01-27 07:41:19 +00002502 /* make sure no interrupts are pending */
2503 ret = r8a66597_clock_enable(r8a66597);
2504 if (ret < 0)
2505 goto clean_up3;
2506 disable_controller(r8a66597);
2507
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002508 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2509 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2510 init_timer(&r8a66597->td_timer[i]);
2511 r8a66597->td_timer[i].function = r8a66597_td_timer;
2512 r8a66597->td_timer[i].data = (unsigned long)r8a66597;
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09002513 setup_timer(&r8a66597->interval_timer[i],
2514 r8a66597_interval_timer,
2515 (unsigned long)r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002516 }
2517 INIT_LIST_HEAD(&r8a66597->child_device);
2518
2519 hcd->rsrc_start = res->start;
Marc Zyngier27140212008-08-18 13:08:42 +02002520
Yoshihiro Shimoda0bf32b82008-06-27 19:09:55 +09002521 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002522 if (ret != 0) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002523 dev_err(&pdev->dev, "Failed to add hcd\n");
Magnus Damm765786e2008-10-31 20:22:38 +09002524 goto clean_up3;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002525 }
2526
2527 return 0;
2528
Magnus Damm765786e2008-10-31 20:22:38 +09002529clean_up3:
Magnus Damm719a72b2009-07-17 14:59:55 +00002530#ifdef CONFIG_HAVE_CLK
2531 if (r8a66597->pdata->on_chip)
2532 clk_put(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +09002533clean_up2:
Paul Mundtd6435102008-11-18 12:40:39 +09002534#endif
Magnus Damm765786e2008-10-31 20:22:38 +09002535 usb_put_hcd(hcd);
2536
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002537clean_up:
2538 if (reg)
2539 iounmap(reg);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002540
2541 return ret;
2542}
2543
2544static struct platform_driver r8a66597_driver = {
2545 .probe = r8a66597_probe,
2546 .remove = r8a66597_remove,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002547 .driver = {
2548 .name = (char *) hcd_name,
Kay Sieversf4fce612008-04-10 21:29:22 -07002549 .owner = THIS_MODULE,
Magnus Dammae1cef62009-07-17 14:52:05 +00002550 .pm = R8A66597_DEV_PM_OPS,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002551 },
2552};
2553
2554static int __init r8a66597_init(void)
2555{
2556 if (usb_disabled())
2557 return -ENODEV;
2558
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -07002559 printk(KERN_INFO KBUILD_MODNAME ": driver %s, %s\n", hcd_name,
2560 DRIVER_VERSION);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002561 return platform_driver_register(&r8a66597_driver);
2562}
2563module_init(r8a66597_init);
2564
2565static void __exit r8a66597_cleanup(void)
2566{
2567 platform_driver_unregister(&r8a66597_driver);
2568}
2569module_exit(r8a66597_cleanup);
2570