blob: 4c634eb56358a1231a6c9fcb1dc05c1b983bf99d [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 *
Yoshihiro Shimoda45304e82011-07-07 09:58:56 +09009 * Author : Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090010 *
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>
Eric Lescouet27729aa2010-04-24 23:21:52 +020036#include <linux/usb/hcd.h>
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090037#include <linux/platform_device.h>
Yoshihiro Shimodae2945312007-07-18 23:10:34 +090038#include <linux/io.h>
Paul Mundt27175682010-02-04 06:57:58 +000039#include <linux/mm.h>
Yoshihiro Shimodae2945312007-07-18 23:10:34 +090040#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Paul Mundt27175682010-02-04 06:57:58 +000042#include <asm/cacheflush.h>
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090043
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090044#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) {
Magnus Damm719a72b2009-07-17 14:59:55 +000098 clk_enable(r8a66597->clk);
Magnus Damm719a72b2009-07-17 14:59:55 +000099 do {
100 r8a66597_write(r8a66597, SCKE, SYSCFG0);
101 tmp = r8a66597_read(r8a66597, SYSCFG0);
102 if (i++ > 1000) {
103 printk(KERN_ERR "r8a66597: reg access fail.\n");
104 return -ENXIO;
105 }
106 } while ((tmp & SCKE) != SCKE);
107 r8a66597_write(r8a66597, 0x04, 0x02);
108 } else {
109 do {
110 r8a66597_write(r8a66597, USBE, SYSCFG0);
111 tmp = r8a66597_read(r8a66597, SYSCFG0);
112 if (i++ > 1000) {
113 printk(KERN_ERR "r8a66597: reg access fail.\n");
114 return -ENXIO;
115 }
116 } while ((tmp & USBE) != USBE);
117 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
118 r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata),
119 XTAL, SYSCFG0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900120
Magnus Damm719a72b2009-07-17 14:59:55 +0000121 i = 0;
122 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
123 do {
124 msleep(1);
125 tmp = r8a66597_read(r8a66597, SYSCFG0);
126 if (i++ > 500) {
127 printk(KERN_ERR "r8a66597: reg access fail.\n");
128 return -ENXIO;
129 }
130 } while ((tmp & SCKE) != SCKE);
131 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900132
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900133 return 0;
134}
135
136static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
137{
138 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
139 udelay(1);
Magnus Damm719a72b2009-07-17 14:59:55 +0000140
141 if (r8a66597->pdata->on_chip) {
Magnus Damm719a72b2009-07-17 14:59:55 +0000142 clk_disable(r8a66597->clk);
Magnus Damm719a72b2009-07-17 14:59:55 +0000143 } else {
144 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
145 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
146 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
147 }
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900148}
149
150static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
151{
152 u16 val;
153
154 val = port ? DRPD : DCFM | DRPD;
155 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
156 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
157
158 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
159 r8a66597_bclr(r8a66597, DTCHE, get_intenb_reg(port));
160 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
161}
162
163static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
164{
165 u16 val, tmp;
166
167 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
168 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
169
170 r8a66597_port_power(r8a66597, port, 0);
171
172 do {
173 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
174 udelay(640);
175 } while (tmp == EDGESTS);
176
177 val = port ? DRPD : DCFM | DRPD;
178 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
179 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
180}
181
182static int enable_controller(struct r8a66597 *r8a66597)
183{
184 int ret, port;
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +0900185 u16 vif = r8a66597->pdata->vif ? LDRV : 0;
186 u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
187 u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900188
189 ret = r8a66597_clock_enable(r8a66597);
190 if (ret < 0)
191 return ret;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900192
193 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900194 r8a66597_bset(r8a66597, USBE, SYSCFG0);
195
196 r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
197 r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
198 r8a66597_bset(r8a66597, BRDY0, BRDYENB);
199 r8a66597_bset(r8a66597, BEMP0, BEMPENB);
200
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900201 r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
202 r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
203 r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900204 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
205
206 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900207
Magnus Damm719a72b2009-07-17 14:59:55 +0000208 for (port = 0; port < r8a66597->max_root_hub; port++)
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900209 r8a66597_enable_port(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900210
211 return 0;
212}
213
214static void disable_controller(struct r8a66597 *r8a66597)
215{
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900216 int port;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900217
Magnus Damme5ff15b2010-01-27 07:41:19 +0000218 /* disable interrupts */
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900219 r8a66597_write(r8a66597, 0, INTENB0);
Magnus Damme5ff15b2010-01-27 07:41:19 +0000220 r8a66597_write(r8a66597, 0, INTENB1);
221 r8a66597_write(r8a66597, 0, BRDYENB);
222 r8a66597_write(r8a66597, 0, BEMPENB);
223 r8a66597_write(r8a66597, 0, NRDYENB);
224
225 /* clear status */
226 r8a66597_write(r8a66597, 0, BRDYSTS);
227 r8a66597_write(r8a66597, 0, NRDYSTS);
228 r8a66597_write(r8a66597, 0, BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900229
Magnus Damm719a72b2009-07-17 14:59:55 +0000230 for (port = 0; port < r8a66597->max_root_hub; port++)
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900231 r8a66597_disable_port(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900232
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900233 r8a66597_clock_disable(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900234}
235
236static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
237 struct usb_device *udev)
238{
239 struct r8a66597_device *dev;
240
241 if (udev->parent && udev->parent->devnum != 1)
242 udev = udev->parent;
243
244 dev = dev_get_drvdata(&udev->dev);
245 if (dev)
246 return dev->address;
247 else
248 return 0;
249}
250
251static int is_child_device(char *devpath)
252{
253 return (devpath[2] ? 1 : 0);
254}
255
256static int is_hub_limit(char *devpath)
257{
258 return ((strlen(devpath) >= 4) ? 1 : 0);
259}
260
Magnus Damm719a72b2009-07-17 14:59:55 +0000261static void get_port_number(struct r8a66597 *r8a66597,
262 char *devpath, u16 *root_port, u16 *hub_port)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900263{
264 if (root_port) {
265 *root_port = (devpath[0] & 0x0F) - 1;
Magnus Damm719a72b2009-07-17 14:59:55 +0000266 if (*root_port >= r8a66597->max_root_hub)
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700267 printk(KERN_ERR "r8a66597: Illegal root port number.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900268 }
269 if (hub_port)
270 *hub_port = devpath[2] & 0x0F;
271}
272
273static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
274{
275 u16 usbspd = 0;
276
277 switch (speed) {
278 case USB_SPEED_LOW:
279 usbspd = LSMODE;
280 break;
281 case USB_SPEED_FULL:
282 usbspd = FSMODE;
283 break;
284 case USB_SPEED_HIGH:
285 usbspd = HSMODE;
286 break;
287 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700288 printk(KERN_ERR "r8a66597: unknown speed\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900289 break;
290 }
291
292 return usbspd;
293}
294
295static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
296{
297 int idx;
298
299 idx = address / 32;
300 r8a66597->child_connect_map[idx] |= 1 << (address % 32);
301}
302
303static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
304{
305 int idx;
306
307 idx = address / 32;
308 r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
309}
310
311static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
312{
313 u16 pipenum = pipe->info.pipenum;
Ming Leife9b9032008-06-08 16:13:03 +0800314 const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
315 const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
316 const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900317
318 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
319 dma_ch = R8A66597_PIPE_NO_DMA;
320
321 pipe->fifoaddr = fifoaddr[dma_ch];
322 pipe->fifosel = fifosel[dma_ch];
323 pipe->fifoctr = fifoctr[dma_ch];
324
325 if (pipenum == 0)
326 pipe->pipectr = DCPCTR;
327 else
328 pipe->pipectr = get_pipectr_addr(pipenum);
329
330 if (check_bulk_or_isoc(pipenum)) {
331 pipe->pipetre = get_pipetre_addr(pipenum);
332 pipe->pipetrn = get_pipetrn_addr(pipenum);
333 } else {
334 pipe->pipetre = 0;
335 pipe->pipetrn = 0;
336 }
337}
338
339static struct r8a66597_device *
340get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
341{
342 if (usb_pipedevice(urb->pipe) == 0)
343 return &r8a66597->device0;
344
345 return dev_get_drvdata(&urb->dev->dev);
346}
347
348static int make_r8a66597_device(struct r8a66597 *r8a66597,
349 struct urb *urb, u8 addr)
350{
351 struct r8a66597_device *dev;
352 int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
353
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900354 dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900355 if (dev == NULL)
356 return -ENOMEM;
357
358 dev_set_drvdata(&urb->dev->dev, dev);
359 dev->udev = urb->dev;
360 dev->address = addr;
361 dev->usb_address = usb_address;
362 dev->state = USB_STATE_ADDRESS;
363 dev->ep_in_toggle = 0;
364 dev->ep_out_toggle = 0;
365 INIT_LIST_HEAD(&dev->device_list);
366 list_add_tail(&dev->device_list, &r8a66597->child_device);
367
Magnus Damm719a72b2009-07-17 14:59:55 +0000368 get_port_number(r8a66597, urb->dev->devpath,
369 &dev->root_port, &dev->hub_port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900370 if (!is_child_device(urb->dev->devpath))
371 r8a66597->root_hub[dev->root_port].dev = dev;
372
373 set_devadd_reg(r8a66597, dev->address,
374 get_r8a66597_usb_speed(urb->dev->speed),
375 get_parent_r8a66597_address(r8a66597, urb->dev),
376 dev->hub_port, dev->root_port);
377
378 return 0;
379}
380
381/* this function must be called with interrupt disabled */
382static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
383{
384 u8 addr; /* R8A66597's address */
385 struct r8a66597_device *dev;
386
387 if (is_hub_limit(urb->dev->devpath)) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700388 dev_err(&urb->dev->dev, "External hub limit reached.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900389 return 0;
390 }
391
392 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
393 if (dev && dev->state >= USB_STATE_ADDRESS)
394 return dev->address;
395
396 for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
397 if (r8a66597->address_map & (1 << addr))
398 continue;
399
Greg Kroah-Hartman167e4922012-05-01 21:33:46 -0700400 dev_dbg(&urb->dev->dev, "alloc_address: r8a66597_addr=%d\n", addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900401 r8a66597->address_map |= 1 << addr;
402
403 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
404 return 0;
405
406 return addr;
407 }
408
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700409 dev_err(&urb->dev->dev,
410 "cannot communicate with a USB device more than 10.(%x)\n",
411 r8a66597->address_map);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900412
413 return 0;
414}
415
416/* this function must be called with interrupt disabled */
417static void free_usb_address(struct r8a66597 *r8a66597,
Yoshihiro Shimodad8359332010-03-16 12:29:35 +0900418 struct r8a66597_device *dev, int reset)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900419{
420 int port;
421
422 if (!dev)
423 return;
424
Greg Kroah-Hartman167e4922012-05-01 21:33:46 -0700425 dev_dbg(&dev->udev->dev, "free_addr: addr=%d\n", dev->address);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900426
427 dev->state = USB_STATE_DEFAULT;
428 r8a66597->address_map &= ~(1 << dev->address);
429 dev->address = 0;
Yoshihiro Shimodad8359332010-03-16 12:29:35 +0900430 /*
431 * Only when resetting USB, it is necessary to erase drvdata. When
432 * a usb device with usb hub is disconnect, "dev->udev" is already
433 * freed on usb_desconnect(). So we cannot access the data.
434 */
435 if (reset)
436 dev_set_drvdata(&dev->udev->dev, NULL);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900437 list_del(&dev->device_list);
438 kfree(dev);
439
Magnus Damm719a72b2009-07-17 14:59:55 +0000440 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900441 if (r8a66597->root_hub[port].dev == dev) {
442 r8a66597->root_hub[port].dev = NULL;
443 break;
444 }
445 }
446}
447
448static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
449 u16 mask, u16 loop)
450{
451 u16 tmp;
452 int i = 0;
453
454 do {
455 tmp = r8a66597_read(r8a66597, reg);
456 if (i++ > 1000000) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700457 printk(KERN_ERR "r8a66597: register%lx, loop %x "
458 "is timeout\n", reg, loop);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900459 break;
460 }
461 ndelay(1);
462 } while ((tmp & mask) != loop);
463}
464
465/* this function must be called with interrupt disabled */
466static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
467{
468 u16 tmp;
469
470 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
471 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
472 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
473 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
474}
475
476/* this function must be called with interrupt disabled */
477static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
478{
479 u16 tmp;
480
481 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
482 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
483 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
484 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
485 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
486}
487
488/* this function must be called with interrupt disabled */
489static void clear_all_buffer(struct r8a66597 *r8a66597,
490 struct r8a66597_pipe *pipe)
491{
492 u16 tmp;
493
494 if (!pipe || pipe->info.pipenum == 0)
495 return;
496
497 pipe_stop(r8a66597, pipe);
498 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
499 tmp = r8a66597_read(r8a66597, pipe->pipectr);
500 tmp = r8a66597_read(r8a66597, pipe->pipectr);
501 tmp = r8a66597_read(r8a66597, pipe->pipectr);
502 r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
503}
504
505/* this function must be called with interrupt disabled */
506static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
507 struct r8a66597_pipe *pipe, int toggle)
508{
509 if (toggle)
510 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
511 else
512 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
513}
514
Magnus Damm719a72b2009-07-17 14:59:55 +0000515static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
516{
517 if (r8a66597->pdata->on_chip)
518 return MBW_32;
519 else
520 return MBW_16;
521}
522
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900523/* this function must be called with interrupt disabled */
524static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
525{
Magnus Damm719a72b2009-07-17 14:59:55 +0000526 unsigned short mbw = mbw_value(r8a66597);
527
528 r8a66597_mdfy(r8a66597, mbw | pipenum, mbw | CURPIPE, CFIFOSEL);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900529 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
530}
531
532/* this function must be called with interrupt disabled */
533static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
534 struct r8a66597_pipe *pipe)
535{
Magnus Damm719a72b2009-07-17 14:59:55 +0000536 unsigned short mbw = mbw_value(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900537
Magnus Damm719a72b2009-07-17 14:59:55 +0000538 cfifo_change(r8a66597, 0);
539 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D0FIFOSEL);
540 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D1FIFOSEL);
541
542 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum, mbw | CURPIPE,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900543 pipe->fifosel);
544 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
545}
546
547static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
548{
549 struct r8a66597_pipe *pipe = hep->hcpriv;
550
551 if (usb_pipeendpoint(urb->pipe) == 0)
552 return 0;
553 else
554 return pipe->info.pipenum;
555}
556
557static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
558{
559 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
560
561 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
562}
563
564static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
565 int urb_pipe)
566{
567 if (!dev)
568 return NULL;
569
570 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
571}
572
573/* this function must be called with interrupt disabled */
574static void pipe_toggle_set(struct r8a66597 *r8a66597,
575 struct r8a66597_pipe *pipe,
576 struct urb *urb, int set)
577{
578 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
579 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
580 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
581
582 if (!toggle)
583 return;
584
585 if (set)
586 *toggle |= 1 << endpoint;
587 else
588 *toggle &= ~(1 << endpoint);
589}
590
591/* this function must be called with interrupt disabled */
592static void pipe_toggle_save(struct r8a66597 *r8a66597,
593 struct r8a66597_pipe *pipe,
594 struct urb *urb)
595{
596 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
597 pipe_toggle_set(r8a66597, pipe, urb, 1);
598 else
599 pipe_toggle_set(r8a66597, pipe, urb, 0);
600}
601
602/* this function must be called with interrupt disabled */
603static void pipe_toggle_restore(struct r8a66597 *r8a66597,
604 struct r8a66597_pipe *pipe,
605 struct urb *urb)
606{
607 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
608 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
609 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
610
Yoshihiro Shimodaf6ace2c2007-05-30 20:42:41 +0900611 if (!toggle)
612 return;
613
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900614 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
615}
616
617/* this function must be called with interrupt disabled */
618static void pipe_buffer_setting(struct r8a66597 *r8a66597,
619 struct r8a66597_pipe_info *info)
620{
621 u16 val = 0;
622
623 if (info->pipenum == 0)
624 return;
625
626 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
627 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
628 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
629 if (!info->dir_in)
630 val |= R8A66597_DIR;
631 if (info->type == R8A66597_BULK && info->dir_in)
632 val |= R8A66597_DBLB | R8A66597_SHTNAK;
633 val |= info->type | info->epnum;
634 r8a66597_write(r8a66597, val, PIPECFG);
635
636 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
637 PIPEBUF);
638 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
639 PIPEMAXP);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900640 r8a66597_write(r8a66597, info->interval, PIPEPERI);
641}
642
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900643/* this function must be called with interrupt disabled */
644static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
645{
646 struct r8a66597_pipe_info *info;
647 struct urb *urb = td->urb;
648
649 if (td->pipenum > 0) {
650 info = &td->pipe->info;
651 cfifo_change(r8a66597, 0);
652 pipe_buffer_setting(r8a66597, info);
653
654 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
655 usb_pipeout(urb->pipe)) &&
656 !usb_pipecontrol(urb->pipe)) {
657 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
658 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
659 clear_all_buffer(r8a66597, td->pipe);
660 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
661 usb_pipeout(urb->pipe), 1);
662 }
663 pipe_toggle_restore(r8a66597, td->pipe, urb);
664 }
665}
666
667/* this function must be called with interrupt disabled */
668static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
669 struct usb_endpoint_descriptor *ep)
670{
671 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
672
673 memset(array, 0, sizeof(array));
Julia Lawall2e0fe702008-12-29 11:22:14 +0100674 switch (usb_endpoint_type(ep)) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900675 case USB_ENDPOINT_XFER_BULK:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100676 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900677 array[i++] = 4;
678 else {
679 array[i++] = 3;
680 array[i++] = 5;
681 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900682 break;
683 case USB_ENDPOINT_XFER_INT:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100684 if (usb_endpoint_dir_in(ep)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900685 array[i++] = 6;
686 array[i++] = 7;
687 array[i++] = 8;
688 } else
689 array[i++] = 9;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900690 break;
691 case USB_ENDPOINT_XFER_ISOC:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100692 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900693 array[i++] = 2;
694 else
695 array[i++] = 1;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900696 break;
697 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700698 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900699 return 0;
700 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900701
702 i = 1;
703 min = array[0];
704 while (array[i] != 0) {
705 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
706 min = array[i];
707 i++;
708 }
709
710 return min;
711}
712
713static u16 get_r8a66597_type(__u8 type)
714{
715 u16 r8a66597_type;
716
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900717 switch (type) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900718 case USB_ENDPOINT_XFER_BULK:
719 r8a66597_type = R8A66597_BULK;
720 break;
721 case USB_ENDPOINT_XFER_INT:
722 r8a66597_type = R8A66597_INT;
723 break;
724 case USB_ENDPOINT_XFER_ISOC:
725 r8a66597_type = R8A66597_ISO;
726 break;
727 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700728 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900729 r8a66597_type = 0x0000;
730 break;
731 }
732
733 return r8a66597_type;
734}
735
736static u16 get_bufnum(u16 pipenum)
737{
738 u16 bufnum = 0;
739
740 if (pipenum == 0)
741 bufnum = 0;
742 else if (check_bulk_or_isoc(pipenum))
743 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
744 else if (check_interrupt(pipenum))
745 bufnum = 4 + (pipenum - 6);
746 else
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700747 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900748
749 return bufnum;
750}
751
752static u16 get_buf_bsize(u16 pipenum)
753{
754 u16 buf_bsize = 0;
755
756 if (pipenum == 0)
757 buf_bsize = 3;
758 else if (check_bulk_or_isoc(pipenum))
759 buf_bsize = R8A66597_BUF_BSIZE - 1;
760 else if (check_interrupt(pipenum))
761 buf_bsize = 0;
762 else
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700763 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900764
765 return buf_bsize;
766}
767
768/* this function must be called with interrupt disabled */
769static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
770 struct r8a66597_device *dev,
771 struct r8a66597_pipe *pipe,
772 struct urb *urb)
773{
774 int i;
775 struct r8a66597_pipe_info *info = &pipe->info;
Magnus Damm719a72b2009-07-17 14:59:55 +0000776 unsigned short mbw = mbw_value(r8a66597);
777
778 /* pipe dma is only for external controlles */
779 if (r8a66597->pdata->on_chip)
780 return;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900781
782 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
783 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
784 if ((r8a66597->dma_map & (1 << i)) != 0)
785 continue;
786
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -0700787 dev_info(&dev->udev->dev,
788 "address %d, EndpointAddress 0x%02x use "
789 "DMA FIFO\n", usb_pipedevice(urb->pipe),
790 info->dir_in ?
791 USB_ENDPOINT_DIR_MASK + info->epnum
792 : info->epnum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900793
794 r8a66597->dma_map |= 1 << i;
795 dev->dma_map |= 1 << i;
796 set_pipe_reg_addr(pipe, i);
797
798 cfifo_change(r8a66597, 0);
Magnus Damm719a72b2009-07-17 14:59:55 +0000799 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum,
800 mbw | CURPIPE, pipe->fifosel);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900801
802 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
803 pipe->info.pipenum);
804 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
805 break;
806 }
807 }
808}
809
810/* this function must be called with interrupt disabled */
811static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
812 struct usb_host_endpoint *hep,
813 struct r8a66597_pipe_info *info)
814{
815 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
816 struct r8a66597_pipe *pipe = hep->hcpriv;
817
Greg Kroah-Hartman167e4922012-05-01 21:33:46 -0700818 dev_dbg(&dev->udev->dev, "enable_pipe:\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900819
820 pipe->info = *info;
821 set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
822 r8a66597->pipe_cnt[pipe->info.pipenum]++;
823 dev->pipe_cnt[pipe->info.pipenum]++;
824
825 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
826}
827
Paul Mundt27175682010-02-04 06:57:58 +0000828static void r8a66597_urb_done(struct r8a66597 *r8a66597, struct urb *urb,
829 int status)
830__releases(r8a66597->lock)
831__acquires(r8a66597->lock)
832{
833 if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
834 void *ptr;
835
836 for (ptr = urb->transfer_buffer;
837 ptr < urb->transfer_buffer + urb->transfer_buffer_length;
838 ptr += PAGE_SIZE)
839 flush_dcache_page(virt_to_page(ptr));
840 }
841
842 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
843 spin_unlock(&r8a66597->lock);
844 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb, status);
845 spin_lock(&r8a66597->lock);
846}
847
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900848/* this function must be called with interrupt disabled */
849static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
850{
851 struct r8a66597_td *td, *next;
852 struct urb *urb;
853 struct list_head *list = &r8a66597->pipe_queue[pipenum];
854
855 if (list_empty(list))
856 return;
857
858 list_for_each_entry_safe(td, next, list, queue) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900859 if (td->address != address)
860 continue;
861
862 urb = td->urb;
863 list_del(&td->queue);
864 kfree(td);
865
Paul Mundt27175682010-02-04 06:57:58 +0000866 if (urb)
867 r8a66597_urb_done(r8a66597, urb, -ENODEV);
Alan Sterne9df41c2007-08-08 11:48:02 -0400868
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900869 break;
870 }
871}
872
873/* this function must be called with interrupt disabled */
874static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
875 struct r8a66597_device *dev)
876{
877 int check_ep0 = 0;
878 u16 pipenum;
879
880 if (!dev)
881 return;
882
883 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
884 if (!dev->pipe_cnt[pipenum])
885 continue;
886
887 if (!check_ep0) {
888 check_ep0 = 1;
889 force_dequeue(r8a66597, 0, dev->address);
890 }
891
892 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
893 dev->pipe_cnt[pipenum] = 0;
894 force_dequeue(r8a66597, pipenum, dev->address);
895 }
896
Greg Kroah-Hartman167e4922012-05-01 21:33:46 -0700897 dev_dbg(&dev->udev->dev, "disable_pipe\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900898
899 r8a66597->dma_map &= ~(dev->dma_map);
900 dev->dma_map = 0;
901}
902
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900903static u16 get_interval(struct urb *urb, __u8 interval)
904{
905 u16 time = 1;
906 int i;
907
908 if (urb->dev->speed == USB_SPEED_HIGH) {
909 if (interval > IITV)
910 time = IITV;
911 else
912 time = interval ? interval - 1 : 0;
913 } else {
914 if (interval > 128) {
915 time = IITV;
916 } else {
917 /* calculate the nearest value for PIPEPERI */
918 for (i = 0; i < 7; i++) {
919 if ((1 << i) < interval &&
920 (1 << (i + 1) > interval))
921 time = 1 << i;
922 }
923 }
924 }
925
926 return time;
927}
928
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900929static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
930{
931 __u8 i;
932 unsigned long time = 1;
933
934 if (usb_pipeisoc(urb->pipe))
935 return 0;
936
937 if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
938 for (i = 0; i < (interval - 1); i++)
939 time *= 2;
940 time = time * 125 / 1000; /* uSOF -> msec */
941 } else {
942 time = interval;
943 }
944
945 return time;
946}
947
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900948/* this function must be called with interrupt disabled */
949static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
950 struct usb_host_endpoint *hep,
951 struct usb_endpoint_descriptor *ep)
952{
953 struct r8a66597_pipe_info info;
954
955 info.pipenum = get_empty_pipenum(r8a66597, ep);
956 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
Julia Lawall2e0fe702008-12-29 11:22:14 +0100957 info.epnum = usb_endpoint_num(ep);
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700958 info.maxpacket = usb_endpoint_maxp(ep);
Julia Lawall2e0fe702008-12-29 11:22:14 +0100959 info.type = get_r8a66597_type(usb_endpoint_type(ep));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900960 info.bufnum = get_bufnum(info.pipenum);
961 info.buf_bsize = get_buf_bsize(info.pipenum);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900962 if (info.type == R8A66597_BULK) {
963 info.interval = 0;
964 info.timer_interval = 0;
965 } else {
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900966 info.interval = get_interval(urb, ep->bInterval);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900967 info.timer_interval = get_timer_interval(urb, ep->bInterval);
968 }
Julia Lawall2e0fe702008-12-29 11:22:14 +0100969 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900970 info.dir_in = 1;
971 else
972 info.dir_in = 0;
973
974 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
975}
976
977static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
978{
979 struct r8a66597_device *dev;
980
981 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
982 dev->state = USB_STATE_CONFIGURED;
983}
984
985static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
986 u16 pipenum)
987{
988 if (pipenum == 0 && usb_pipeout(urb->pipe))
989 enable_irq_empty(r8a66597, pipenum);
990 else
991 enable_irq_ready(r8a66597, pipenum);
992
993 if (!usb_pipeisoc(urb->pipe))
994 enable_irq_nrdy(r8a66597, pipenum);
995}
996
997static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
998{
999 disable_irq_ready(r8a66597, pipenum);
1000 disable_irq_nrdy(r8a66597, pipenum);
1001}
1002
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001003static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
1004{
1005 mod_timer(&r8a66597->rh_timer,
1006 jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
1007}
1008
1009static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
1010 int connect)
1011{
1012 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1013
1014 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1015 rh->scount = R8A66597_MAX_SAMPLING;
1016 if (connect)
Alan Stern749da5f2010-03-04 17:05:08 -05001017 rh->port |= USB_PORT_STAT_CONNECTION;
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001018 else
Alan Stern749da5f2010-03-04 17:05:08 -05001019 rh->port &= ~USB_PORT_STAT_CONNECTION;
1020 rh->port |= USB_PORT_STAT_C_CONNECTION << 16;
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001021
1022 r8a66597_root_hub_start_polling(r8a66597);
1023}
1024
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001025/* this function must be called with interrupt disabled */
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001026static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
1027 u16 syssts)
Paul Mundt2c940db2010-02-04 06:58:28 +00001028__releases(r8a66597->lock)
1029__acquires(r8a66597->lock)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001030{
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001031 if (syssts == SE0) {
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001032 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001033 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
Yoshihiro Shimoda1e6159f2009-10-21 20:33:39 +09001034 } else {
1035 if (syssts == FS_JSTS)
1036 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
1037 else if (syssts == LS_JSTS)
1038 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
1039
1040 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
1041 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
1042
1043 if (r8a66597->bus_suspended)
1044 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001045 }
1046
Paul Mundt2c940db2010-02-04 06:58:28 +00001047 spin_unlock(&r8a66597->lock);
Yoshihiro Shimoda1e6159f2009-10-21 20:33:39 +09001048 usb_hcd_poll_rh_status(r8a66597_to_hcd(r8a66597));
Paul Mundt2c940db2010-02-04 06:58:28 +00001049 spin_lock(&r8a66597->lock);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001050}
1051
1052/* this function must be called with interrupt disabled */
1053static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
1054{
1055 u16 speed = get_rh_usb_speed(r8a66597, port);
1056 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1057
Alan Stern288ead42010-03-04 11:32:30 -05001058 rh->port &= ~(USB_PORT_STAT_HIGH_SPEED | USB_PORT_STAT_LOW_SPEED);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001059 if (speed == HSMODE)
Alan Stern288ead42010-03-04 11:32:30 -05001060 rh->port |= USB_PORT_STAT_HIGH_SPEED;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001061 else if (speed == LSMODE)
Alan Stern288ead42010-03-04 11:32:30 -05001062 rh->port |= USB_PORT_STAT_LOW_SPEED;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001063
Nobuhiro Iwamatsua5797a62010-06-07 16:55:56 +09001064 rh->port &= ~USB_PORT_STAT_RESET;
Alan Stern749da5f2010-03-04 17:05:08 -05001065 rh->port |= USB_PORT_STAT_ENABLE;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001066}
1067
1068/* this function must be called with interrupt disabled */
1069static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
1070{
1071 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
1072
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001073 disable_r8a66597_pipe_all(r8a66597, dev);
Yoshihiro Shimodad8359332010-03-16 12:29:35 +09001074 free_usb_address(r8a66597, dev, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001075
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001076 start_root_hub_sampling(r8a66597, port, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001077}
1078
1079/* this function must be called with interrupt disabled */
1080static void prepare_setup_packet(struct r8a66597 *r8a66597,
1081 struct r8a66597_td *td)
1082{
1083 int i;
Al Virofd05e722008-04-28 07:00:16 +01001084 __le16 *p = (__le16 *)td->urb->setup_packet;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001085 unsigned long setup_addr = USBREQ;
1086
1087 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1088 DCPMAXP);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001089 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001090
1091 for (i = 0; i < 4; i++) {
Al Virofd05e722008-04-28 07:00:16 +01001092 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001093 setup_addr += 2;
1094 }
1095 r8a66597_write(r8a66597, SUREQ, DCPCTR);
1096}
1097
1098/* this function must be called with interrupt disabled */
1099static void prepare_packet_read(struct r8a66597 *r8a66597,
1100 struct r8a66597_td *td)
1101{
1102 struct urb *urb = td->urb;
1103
1104 if (usb_pipecontrol(urb->pipe)) {
1105 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1106 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1107 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1108 if (urb->actual_length == 0) {
1109 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1110 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1111 }
1112 pipe_irq_disable(r8a66597, td->pipenum);
1113 pipe_start(r8a66597, td->pipe);
1114 pipe_irq_enable(r8a66597, urb, td->pipenum);
1115 } else {
1116 if (urb->actual_length == 0) {
1117 pipe_irq_disable(r8a66597, td->pipenum);
1118 pipe_setting(r8a66597, td);
1119 pipe_stop(r8a66597, td->pipe);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001120 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001121
1122 if (td->pipe->pipetre) {
1123 r8a66597_write(r8a66597, TRCLR,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001124 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001125 r8a66597_write(r8a66597,
Julia Lawalldfa5ec72008-03-04 15:25:11 -08001126 DIV_ROUND_UP
1127 (urb->transfer_buffer_length,
1128 td->maxpacket),
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001129 td->pipe->pipetrn);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001130 r8a66597_bset(r8a66597, TRENB,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001131 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001132 }
1133
1134 pipe_start(r8a66597, td->pipe);
1135 pipe_irq_enable(r8a66597, urb, td->pipenum);
1136 }
1137 }
1138}
1139
1140/* this function must be called with interrupt disabled */
1141static void prepare_packet_write(struct r8a66597 *r8a66597,
1142 struct r8a66597_td *td)
1143{
1144 u16 tmp;
1145 struct urb *urb = td->urb;
1146
1147 if (usb_pipecontrol(urb->pipe)) {
1148 pipe_stop(r8a66597, td->pipe);
1149 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1150 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1151 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1152 if (urb->actual_length == 0) {
1153 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1154 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1155 }
1156 } else {
1157 if (urb->actual_length == 0)
1158 pipe_setting(r8a66597, td);
1159 if (td->pipe->pipetre)
1160 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1161 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001162 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001163
1164 fifo_change_from_pipe(r8a66597, td->pipe);
1165 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1166 if (unlikely((tmp & FRDY) == 0))
1167 pipe_irq_enable(r8a66597, urb, td->pipenum);
1168 else
1169 packet_write(r8a66597, td->pipenum);
1170 pipe_start(r8a66597, td->pipe);
1171}
1172
1173/* this function must be called with interrupt disabled */
1174static void prepare_status_packet(struct r8a66597 *r8a66597,
1175 struct r8a66597_td *td)
1176{
1177 struct urb *urb = td->urb;
1178
1179 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001180 pipe_stop(r8a66597, td->pipe);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001181
1182 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1183 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1184 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1185 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001186 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +09001187 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001188 enable_irq_empty(r8a66597, 0);
1189 } else {
1190 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1191 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1192 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1193 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001194 enable_irq_ready(r8a66597, 0);
1195 }
1196 enable_irq_nrdy(r8a66597, 0);
1197 pipe_start(r8a66597, td->pipe);
1198}
1199
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001200static int is_set_address(unsigned char *setup_packet)
1201{
1202 if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1203 setup_packet[1] == USB_REQ_SET_ADDRESS)
1204 return 1;
1205 else
1206 return 0;
1207}
1208
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001209/* this function must be called with interrupt disabled */
1210static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1211{
1212 BUG_ON(!td);
1213
1214 switch (td->type) {
1215 case USB_PID_SETUP:
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001216 if (is_set_address(td->urb->setup_packet)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001217 td->set_address = 1;
1218 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1219 td->urb);
1220 if (td->urb->setup_packet[2] == 0)
1221 return -EPIPE;
1222 }
1223 prepare_setup_packet(r8a66597, td);
1224 break;
1225 case USB_PID_IN:
1226 prepare_packet_read(r8a66597, td);
1227 break;
1228 case USB_PID_OUT:
1229 prepare_packet_write(r8a66597, td);
1230 break;
1231 case USB_PID_ACK:
1232 prepare_status_packet(r8a66597, td);
1233 break;
1234 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001235 printk(KERN_ERR "r8a66597: invalid type.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001236 break;
1237 }
1238
1239 return 0;
1240}
1241
1242static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1243{
1244 if (usb_pipeisoc(urb->pipe)) {
1245 if (urb->number_of_packets == td->iso_cnt)
1246 return 1;
1247 }
1248
1249 /* control or bulk or interrupt */
1250 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1251 (td->short_packet) || (td->zero_packet))
1252 return 1;
1253
1254 return 0;
1255}
1256
1257/* this function must be called with interrupt disabled */
1258static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1259{
1260 unsigned long time;
1261
1262 BUG_ON(!td);
1263
1264 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1265 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1266 r8a66597->timeout_map |= 1 << td->pipenum;
1267 switch (usb_pipetype(td->urb->pipe)) {
1268 case PIPE_INTERRUPT:
1269 case PIPE_ISOCHRONOUS:
1270 time = 30;
1271 break;
1272 default:
1273 time = 300;
1274 break;
1275 }
1276
1277 mod_timer(&r8a66597->td_timer[td->pipenum],
1278 jiffies + msecs_to_jiffies(time));
1279 }
1280}
1281
1282/* this function must be called with interrupt disabled */
Alan Sterndfd1e532007-08-21 15:36:52 -04001283static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
Alan Stern888fda42007-08-24 15:41:18 -04001284 u16 pipenum, struct urb *urb, int status)
Alan Sterndfd1e532007-08-21 15:36:52 -04001285__releases(r8a66597->lock) __acquires(r8a66597->lock)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001286{
1287 int restart = 0;
1288 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1289
1290 r8a66597->timeout_map &= ~(1 << pipenum);
1291
1292 if (likely(td)) {
Alan Stern888fda42007-08-24 15:41:18 -04001293 if (td->set_address && (status != 0 || urb->unlinked))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001294 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1295
1296 pipe_toggle_save(r8a66597, td->pipe, urb);
1297 list_del(&td->queue);
1298 kfree(td);
1299 }
1300
1301 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1302 restart = 1;
1303
1304 if (likely(urb)) {
1305 if (usb_pipeisoc(urb->pipe))
1306 urb->start_frame = r8a66597_get_frame(hcd);
1307
Paul Mundt27175682010-02-04 06:57:58 +00001308 r8a66597_urb_done(r8a66597, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001309 }
1310
1311 if (restart) {
1312 td = r8a66597_get_td(r8a66597, pipenum);
1313 if (unlikely(!td))
1314 return;
1315
1316 start_transfer(r8a66597, td);
1317 set_td_timer(r8a66597, td);
1318 }
1319}
1320
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001321static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1322{
1323 u16 tmp;
1324 int rcv_len, bufsize, urb_len, size;
1325 u16 *buf;
1326 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1327 struct urb *urb;
1328 int finish = 0;
Alan Sterndfd1e532007-08-21 15:36:52 -04001329 int status = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001330
1331 if (unlikely(!td))
1332 return;
1333 urb = td->urb;
1334
1335 fifo_change_from_pipe(r8a66597, td->pipe);
1336 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1337 if (unlikely((tmp & FRDY) == 0)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001338 pipe_stop(r8a66597, td->pipe);
1339 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001340 printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001341 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001342 return;
1343 }
1344
1345 /* prepare parameters */
1346 rcv_len = tmp & DTLN;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001347 if (usb_pipeisoc(urb->pipe)) {
1348 buf = (u16 *)(urb->transfer_buffer +
1349 urb->iso_frame_desc[td->iso_cnt].offset);
1350 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1351 } else {
1352 buf = (void *)urb->transfer_buffer + urb->actual_length;
1353 urb_len = urb->transfer_buffer_length - urb->actual_length;
1354 }
Alan Sterndfd1e532007-08-21 15:36:52 -04001355 bufsize = min(urb_len, (int) td->maxpacket);
1356 if (rcv_len <= bufsize) {
1357 size = rcv_len;
1358 } else {
1359 size = bufsize;
1360 status = -EOVERFLOW;
1361 finish = 1;
1362 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001363
1364 /* update parameters */
1365 urb->actual_length += size;
1366 if (rcv_len == 0)
1367 td->zero_packet = 1;
Alan Sterndfd1e532007-08-21 15:36:52 -04001368 if (rcv_len < bufsize) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001369 td->short_packet = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001370 }
1371 if (usb_pipeisoc(urb->pipe)) {
1372 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
Alan Sterndfd1e532007-08-21 15:36:52 -04001373 urb->iso_frame_desc[td->iso_cnt].status = status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001374 td->iso_cnt++;
Alan Sterndfd1e532007-08-21 15:36:52 -04001375 finish = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001376 }
1377
1378 /* check transfer finish */
Alan Sternb0d9efb2007-08-21 15:39:21 -04001379 if (finish || check_transfer_finish(td, urb)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001380 pipe_stop(r8a66597, td->pipe);
1381 pipe_irq_disable(r8a66597, pipenum);
1382 finish = 1;
1383 }
1384
1385 /* read fifo */
1386 if (urb->transfer_buffer) {
1387 if (size == 0)
1388 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1389 else
1390 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1391 buf, size);
1392 }
1393
Alan Stern888fda42007-08-24 15:41:18 -04001394 if (finish && pipenum != 0)
1395 finish_request(r8a66597, td, pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001396}
1397
1398static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1399{
1400 u16 tmp;
1401 int bufsize, size;
1402 u16 *buf;
1403 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1404 struct urb *urb;
1405
1406 if (unlikely(!td))
1407 return;
1408 urb = td->urb;
1409
1410 fifo_change_from_pipe(r8a66597, td->pipe);
1411 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1412 if (unlikely((tmp & FRDY) == 0)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001413 pipe_stop(r8a66597, td->pipe);
1414 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001415 printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001416 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001417 return;
1418 }
1419
1420 /* prepare parameters */
1421 bufsize = td->maxpacket;
1422 if (usb_pipeisoc(urb->pipe)) {
1423 buf = (u16 *)(urb->transfer_buffer +
1424 urb->iso_frame_desc[td->iso_cnt].offset);
1425 size = min(bufsize,
1426 (int)urb->iso_frame_desc[td->iso_cnt].length);
1427 } else {
1428 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
Greg Kroah-Hartman16e2e5f2009-03-03 16:44:13 -08001429 size = min_t(u32, bufsize,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001430 urb->transfer_buffer_length - urb->actual_length);
1431 }
1432
1433 /* write fifo */
1434 if (pipenum > 0)
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001435 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001436 if (urb->transfer_buffer) {
Yoshihiro Shimodaf2e90392011-07-07 09:57:10 +09001437 r8a66597_write_fifo(r8a66597, td->pipe, buf, size);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001438 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1439 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1440 }
1441
1442 /* update parameters */
1443 urb->actual_length += size;
1444 if (usb_pipeisoc(urb->pipe)) {
1445 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1446 urb->iso_frame_desc[td->iso_cnt].status = 0;
1447 td->iso_cnt++;
1448 }
1449
1450 /* check transfer finish */
1451 if (check_transfer_finish(td, urb)) {
1452 disable_irq_ready(r8a66597, pipenum);
1453 enable_irq_empty(r8a66597, pipenum);
1454 if (!usb_pipeisoc(urb->pipe))
1455 enable_irq_nrdy(r8a66597, pipenum);
1456 } else
1457 pipe_irq_enable(r8a66597, urb, pipenum);
1458}
1459
1460
Alan Stern888fda42007-08-24 15:41:18 -04001461static void check_next_phase(struct r8a66597 *r8a66597, int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001462{
1463 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1464 struct urb *urb;
1465 u8 finish = 0;
1466
1467 if (unlikely(!td))
1468 return;
1469 urb = td->urb;
1470
1471 switch (td->type) {
1472 case USB_PID_IN:
1473 case USB_PID_OUT:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001474 if (check_transfer_finish(td, urb))
1475 td->type = USB_PID_ACK;
1476 break;
1477 case USB_PID_SETUP:
Alan Sterneb231052007-08-21 15:40:36 -04001478 if (urb->transfer_buffer_length == urb->actual_length)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001479 td->type = USB_PID_ACK;
Alan Sterneb231052007-08-21 15:40:36 -04001480 else if (usb_pipeout(urb->pipe))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001481 td->type = USB_PID_OUT;
1482 else
1483 td->type = USB_PID_IN;
1484 break;
1485 case USB_PID_ACK:
1486 finish = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001487 break;
1488 }
1489
Alan Stern888fda42007-08-24 15:41:18 -04001490 if (finish || status != 0 || urb->unlinked)
1491 finish_request(r8a66597, td, 0, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001492 else
1493 start_transfer(r8a66597, td);
1494}
1495
Alan Stern888fda42007-08-24 15:41:18 -04001496static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001497{
1498 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1499
Alan Stern888fda42007-08-24 15:41:18 -04001500 if (td) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001501 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1502
1503 if (pid == PID_NAK)
Alan Stern888fda42007-08-24 15:41:18 -04001504 return -ECONNRESET;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001505 else
Alan Stern888fda42007-08-24 15:41:18 -04001506 return -EPIPE;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001507 }
Alan Stern888fda42007-08-24 15:41:18 -04001508 return 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001509}
1510
1511static void irq_pipe_ready(struct r8a66597 *r8a66597)
1512{
1513 u16 check;
1514 u16 pipenum;
1515 u16 mask;
1516 struct r8a66597_td *td;
1517
1518 mask = r8a66597_read(r8a66597, BRDYSTS)
1519 & r8a66597_read(r8a66597, BRDYENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001520 r8a66597_write(r8a66597, ~mask, BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001521 if (mask & BRDY0) {
1522 td = r8a66597_get_td(r8a66597, 0);
1523 if (td && td->type == USB_PID_IN)
1524 packet_read(r8a66597, 0);
1525 else
1526 pipe_irq_disable(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001527 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001528 }
1529
1530 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1531 check = 1 << pipenum;
1532 if (mask & check) {
1533 td = r8a66597_get_td(r8a66597, pipenum);
1534 if (unlikely(!td))
1535 continue;
1536
1537 if (td->type == USB_PID_IN)
1538 packet_read(r8a66597, pipenum);
1539 else if (td->type == USB_PID_OUT)
1540 packet_write(r8a66597, pipenum);
1541 }
1542 }
1543}
1544
1545static void irq_pipe_empty(struct r8a66597 *r8a66597)
1546{
1547 u16 tmp;
1548 u16 check;
1549 u16 pipenum;
1550 u16 mask;
1551 struct r8a66597_td *td;
1552
1553 mask = r8a66597_read(r8a66597, BEMPSTS)
1554 & r8a66597_read(r8a66597, BEMPENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001555 r8a66597_write(r8a66597, ~mask, BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001556 if (mask & BEMP0) {
1557 cfifo_change(r8a66597, 0);
1558 td = r8a66597_get_td(r8a66597, 0);
1559 if (td && td->type != USB_PID_OUT)
1560 disable_irq_empty(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001561 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001562 }
1563
1564 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1565 check = 1 << pipenum;
1566 if (mask & check) {
1567 struct r8a66597_td *td;
1568 td = r8a66597_get_td(r8a66597, pipenum);
1569 if (unlikely(!td))
1570 continue;
1571
1572 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1573 if ((tmp & INBUFM) == 0) {
1574 disable_irq_empty(r8a66597, pipenum);
1575 pipe_irq_disable(r8a66597, pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001576 finish_request(r8a66597, td, pipenum, td->urb,
1577 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001578 }
1579 }
1580 }
1581}
1582
1583static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1584{
1585 u16 check;
1586 u16 pipenum;
1587 u16 mask;
Alan Stern888fda42007-08-24 15:41:18 -04001588 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001589
1590 mask = r8a66597_read(r8a66597, NRDYSTS)
1591 & r8a66597_read(r8a66597, NRDYENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001592 r8a66597_write(r8a66597, ~mask, NRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001593 if (mask & NRDY0) {
1594 cfifo_change(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001595 status = get_urb_error(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001596 pipe_irq_disable(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001597 check_next_phase(r8a66597, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001598 }
1599
1600 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1601 check = 1 << pipenum;
1602 if (mask & check) {
1603 struct r8a66597_td *td;
1604 td = r8a66597_get_td(r8a66597, pipenum);
1605 if (unlikely(!td))
1606 continue;
1607
Alan Stern888fda42007-08-24 15:41:18 -04001608 status = get_urb_error(r8a66597, pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001609 pipe_irq_disable(r8a66597, pipenum);
1610 pipe_stop(r8a66597, td->pipe);
Alan Stern888fda42007-08-24 15:41:18 -04001611 finish_request(r8a66597, td, pipenum, td->urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001612 }
1613 }
1614}
1615
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001616static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1617{
1618 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1619 u16 intsts0, intsts1, intsts2;
1620 u16 intenb0, intenb1, intenb2;
1621 u16 mask0, mask1, mask2;
Alan Stern888fda42007-08-24 15:41:18 -04001622 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001623
1624 spin_lock(&r8a66597->lock);
1625
1626 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1627 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1628 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1629 intenb0 = r8a66597_read(r8a66597, INTENB0);
1630 intenb1 = r8a66597_read(r8a66597, INTENB1);
1631 intenb2 = r8a66597_read(r8a66597, INTENB2);
1632
1633 mask2 = intsts2 & intenb2;
1634 mask1 = intsts1 & intenb1;
1635 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1636 if (mask2) {
1637 if (mask2 & ATTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001638 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001639 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1640
1641 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001642 start_root_hub_sampling(r8a66597, 1, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001643 }
1644 if (mask2 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001645 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001646 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1647 r8a66597_usb_disconnect(r8a66597, 1);
1648 }
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09001649 if (mask2 & BCHG) {
1650 r8a66597_write(r8a66597, ~BCHG, INTSTS2);
1651 r8a66597_bclr(r8a66597, BCHGE, INTENB2);
1652 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1653 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001654 }
1655
1656 if (mask1) {
1657 if (mask1 & ATTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001658 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001659 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1660
1661 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001662 start_root_hub_sampling(r8a66597, 0, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001663 }
1664 if (mask1 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001665 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001666 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1667 r8a66597_usb_disconnect(r8a66597, 0);
1668 }
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09001669 if (mask1 & BCHG) {
1670 r8a66597_write(r8a66597, ~BCHG, INTSTS1);
1671 r8a66597_bclr(r8a66597, BCHGE, INTENB1);
1672 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1673 }
1674
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001675 if (mask1 & SIGN) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001676 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001677 status = get_urb_error(r8a66597, 0);
1678 check_next_phase(r8a66597, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001679 }
1680 if (mask1 & SACK) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001681 r8a66597_write(r8a66597, ~SACK, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001682 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001683 }
1684 }
1685 if (mask0) {
1686 if (mask0 & BRDY)
1687 irq_pipe_ready(r8a66597);
1688 if (mask0 & BEMP)
1689 irq_pipe_empty(r8a66597);
1690 if (mask0 & NRDY)
1691 irq_pipe_nrdy(r8a66597);
1692 }
1693
1694 spin_unlock(&r8a66597->lock);
1695 return IRQ_HANDLED;
1696}
1697
1698/* this function must be called with interrupt disabled */
1699static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1700{
1701 u16 tmp;
1702 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1703
Alan Stern749da5f2010-03-04 17:05:08 -05001704 if (rh->port & USB_PORT_STAT_RESET) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001705 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1706
1707 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1708 if ((tmp & USBRST) == USBRST) {
1709 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1710 dvstctr_reg);
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001711 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001712 } else
1713 r8a66597_usb_connect(r8a66597, port);
1714 }
1715
Alan Stern749da5f2010-03-04 17:05:08 -05001716 if (!(rh->port & USB_PORT_STAT_CONNECTION)) {
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001717 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1718 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1719 }
1720
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001721 if (rh->scount > 0) {
1722 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1723 if (tmp == rh->old_syssts) {
1724 rh->scount--;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001725 if (rh->scount == 0)
1726 r8a66597_check_syssts(r8a66597, port, tmp);
1727 else
1728 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001729 } else {
1730 rh->scount = R8A66597_MAX_SAMPLING;
1731 rh->old_syssts = tmp;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001732 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001733 }
1734 }
1735}
1736
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001737static void r8a66597_interval_timer(unsigned long _r8a66597)
1738{
1739 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1740 unsigned long flags;
1741 u16 pipenum;
1742 struct r8a66597_td *td;
1743
1744 spin_lock_irqsave(&r8a66597->lock, flags);
1745
1746 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1747 if (!(r8a66597->interval_map & (1 << pipenum)))
1748 continue;
1749 if (timer_pending(&r8a66597->interval_timer[pipenum]))
1750 continue;
1751
1752 td = r8a66597_get_td(r8a66597, pipenum);
1753 if (td)
1754 start_transfer(r8a66597, td);
1755 }
1756
1757 spin_unlock_irqrestore(&r8a66597->lock, flags);
1758}
1759
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001760static void r8a66597_td_timer(unsigned long _r8a66597)
1761{
1762 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1763 unsigned long flags;
1764 u16 pipenum;
1765 struct r8a66597_td *td, *new_td = NULL;
1766 struct r8a66597_pipe *pipe;
1767
1768 spin_lock_irqsave(&r8a66597->lock, flags);
1769 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1770 if (!(r8a66597->timeout_map & (1 << pipenum)))
1771 continue;
1772 if (timer_pending(&r8a66597->td_timer[pipenum]))
1773 continue;
1774
1775 td = r8a66597_get_td(r8a66597, pipenum);
1776 if (!td) {
1777 r8a66597->timeout_map &= ~(1 << pipenum);
1778 continue;
1779 }
1780
1781 if (td->urb->actual_length) {
1782 set_td_timer(r8a66597, td);
1783 break;
1784 }
1785
1786 pipe = td->pipe;
1787 pipe_stop(r8a66597, pipe);
1788
1789 new_td = td;
1790 do {
1791 list_move_tail(&new_td->queue,
1792 &r8a66597->pipe_queue[pipenum]);
1793 new_td = r8a66597_get_td(r8a66597, pipenum);
1794 if (!new_td) {
1795 new_td = td;
1796 break;
1797 }
1798 } while (td != new_td && td->address == new_td->address);
1799
1800 start_transfer(r8a66597, new_td);
1801
1802 if (td == new_td)
1803 r8a66597->timeout_map &= ~(1 << pipenum);
1804 else
1805 set_td_timer(r8a66597, new_td);
1806 break;
1807 }
1808 spin_unlock_irqrestore(&r8a66597->lock, flags);
1809}
1810
1811static void r8a66597_timer(unsigned long _r8a66597)
1812{
1813 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1814 unsigned long flags;
Yoshihiro Shimoda58639642008-11-11 16:47:21 +09001815 int port;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001816
1817 spin_lock_irqsave(&r8a66597->lock, flags);
1818
Magnus Damm719a72b2009-07-17 14:59:55 +00001819 for (port = 0; port < r8a66597->max_root_hub; port++)
Yoshihiro Shimoda58639642008-11-11 16:47:21 +09001820 r8a66597_root_hub_control(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001821
1822 spin_unlock_irqrestore(&r8a66597->lock, flags);
1823}
1824
1825static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1826{
1827 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1828
1829 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1830 (urb->dev->state == USB_STATE_CONFIGURED))
1831 return 1;
1832 else
1833 return 0;
1834}
1835
1836static int r8a66597_start(struct usb_hcd *hcd)
1837{
1838 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001839
1840 hcd->state = HC_STATE_RUNNING;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001841 return enable_controller(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001842}
1843
1844static void r8a66597_stop(struct usb_hcd *hcd)
1845{
1846 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1847
1848 disable_controller(r8a66597);
1849}
1850
1851static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1852{
1853 unsigned int usb_address = usb_pipedevice(urb->pipe);
1854 u16 root_port, hub_port;
1855
1856 if (usb_address == 0) {
Magnus Damm719a72b2009-07-17 14:59:55 +00001857 get_port_number(r8a66597, urb->dev->devpath,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001858 &root_port, &hub_port);
1859 set_devadd_reg(r8a66597, 0,
1860 get_r8a66597_usb_speed(urb->dev->speed),
1861 get_parent_r8a66597_address(r8a66597, urb->dev),
1862 hub_port, root_port);
1863 }
1864}
1865
1866static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1867 struct urb *urb,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001868 struct usb_host_endpoint *hep)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001869{
1870 struct r8a66597_td *td;
1871 u16 pipenum;
1872
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001873 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001874 if (td == NULL)
1875 return NULL;
1876
1877 pipenum = r8a66597_get_pipenum(urb, hep);
1878 td->pipenum = pipenum;
1879 td->pipe = hep->hcpriv;
1880 td->urb = urb;
1881 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1882 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1883 !usb_pipein(urb->pipe));
1884 if (usb_pipecontrol(urb->pipe))
1885 td->type = USB_PID_SETUP;
1886 else if (usb_pipein(urb->pipe))
1887 td->type = USB_PID_IN;
1888 else
1889 td->type = USB_PID_OUT;
1890 INIT_LIST_HEAD(&td->queue);
1891
1892 return td;
1893}
1894
1895static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001896 struct urb *urb,
1897 gfp_t mem_flags)
1898{
Alan Sterne9df41c2007-08-08 11:48:02 -04001899 struct usb_host_endpoint *hep = urb->ep;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001900 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1901 struct r8a66597_td *td = NULL;
Alan Sterne9df41c2007-08-08 11:48:02 -04001902 int ret, request = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001903 unsigned long flags;
1904
1905 spin_lock_irqsave(&r8a66597->lock, flags);
1906 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1907 ret = -ENODEV;
Alan Sterne9df41c2007-08-08 11:48:02 -04001908 goto error_not_linked;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001909 }
1910
Alan Sterne9df41c2007-08-08 11:48:02 -04001911 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1912 if (ret)
1913 goto error_not_linked;
1914
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001915 if (!hep->hcpriv) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001916 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1917 GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001918 if (!hep->hcpriv) {
1919 ret = -ENOMEM;
1920 goto error;
1921 }
1922 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1923 if (usb_pipeendpoint(urb->pipe))
1924 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1925 }
1926
1927 if (unlikely(check_pipe_config(r8a66597, urb)))
1928 init_pipe_config(r8a66597, urb);
1929
1930 set_address_zero(r8a66597, urb);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001931 td = r8a66597_make_td(r8a66597, urb, hep);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001932 if (td == NULL) {
1933 ret = -ENOMEM;
1934 goto error;
1935 }
1936 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1937 request = 1;
1938 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001939 urb->hcpriv = td;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001940
1941 if (request) {
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001942 if (td->pipe->info.timer_interval) {
1943 r8a66597->interval_map |= 1 << td->pipenum;
1944 mod_timer(&r8a66597->interval_timer[td->pipenum],
1945 jiffies + msecs_to_jiffies(
1946 td->pipe->info.timer_interval));
1947 } else {
1948 ret = start_transfer(r8a66597, td);
1949 if (ret < 0) {
1950 list_del(&td->queue);
1951 kfree(td);
1952 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001953 }
1954 } else
1955 set_td_timer(r8a66597, td);
1956
1957error:
Alan Sterne9df41c2007-08-08 11:48:02 -04001958 if (ret)
1959 usb_hcd_unlink_urb_from_ep(hcd, urb);
1960error_not_linked:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001961 spin_unlock_irqrestore(&r8a66597->lock, flags);
1962 return ret;
1963}
1964
Alan Sterne9df41c2007-08-08 11:48:02 -04001965static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1966 int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001967{
1968 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1969 struct r8a66597_td *td;
1970 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001971 int rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001972
1973 spin_lock_irqsave(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001974 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1975 if (rc)
1976 goto done;
1977
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001978 if (urb->hcpriv) {
1979 td = urb->hcpriv;
1980 pipe_stop(r8a66597, td->pipe);
1981 pipe_irq_disable(r8a66597, td->pipenum);
1982 disable_irq_empty(r8a66597, td->pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001983 finish_request(r8a66597, td, td->pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001984 }
Alan Sterne9df41c2007-08-08 11:48:02 -04001985 done:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001986 spin_unlock_irqrestore(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001987 return rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001988}
1989
1990static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1991 struct usb_host_endpoint *hep)
1992{
1993 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1994 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1995 struct r8a66597_td *td;
1996 struct urb *urb = NULL;
1997 u16 pipenum;
1998 unsigned long flags;
1999
2000 if (pipe == NULL)
2001 return;
2002 pipenum = pipe->info.pipenum;
2003
2004 if (pipenum == 0) {
2005 kfree(hep->hcpriv);
2006 hep->hcpriv = NULL;
2007 return;
2008 }
2009
2010 spin_lock_irqsave(&r8a66597->lock, flags);
2011 pipe_stop(r8a66597, pipe);
2012 pipe_irq_disable(r8a66597, pipenum);
2013 disable_irq_empty(r8a66597, pipenum);
2014 td = r8a66597_get_td(r8a66597, pipenum);
2015 if (td)
2016 urb = td->urb;
Alan Stern888fda42007-08-24 15:41:18 -04002017 finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002018 kfree(hep->hcpriv);
2019 hep->hcpriv = NULL;
2020 spin_unlock_irqrestore(&r8a66597->lock, flags);
2021}
2022
2023static int r8a66597_get_frame(struct usb_hcd *hcd)
2024{
2025 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2026 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
2027}
2028
2029static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
2030{
2031 int chix;
2032
2033 if (udev->state == USB_STATE_CONFIGURED &&
2034 udev->parent && udev->parent->devnum > 1 &&
2035 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
2036 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
2037
2038 for (chix = 0; chix < udev->maxchild; chix++) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07002039 struct usb_device *childdev = udev->children[chix];
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002040
2041 if (childdev)
2042 collect_usb_address_map(childdev, map);
2043 }
2044}
2045
2046/* this function must be called with interrupt disabled */
2047static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
2048 int addr)
2049{
2050 struct r8a66597_device *dev;
2051 struct list_head *list = &r8a66597->child_device;
2052
2053 list_for_each_entry(dev, list, device_list) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002054 if (dev->usb_address != addr)
2055 continue;
2056
2057 return dev;
2058 }
2059
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002060 printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002061 return NULL;
2062}
2063
2064static void update_usb_address_map(struct r8a66597 *r8a66597,
2065 struct usb_device *root_hub,
2066 unsigned long *map)
2067{
2068 int i, j, addr;
2069 unsigned long diff;
2070 unsigned long flags;
2071
2072 for (i = 0; i < 4; i++) {
2073 diff = r8a66597->child_connect_map[i] ^ map[i];
2074 if (!diff)
2075 continue;
2076
2077 for (j = 0; j < 32; j++) {
2078 if (!(diff & (1 << j)))
2079 continue;
2080
2081 addr = i * 32 + j;
2082 if (map[i] & (1 << j))
2083 set_child_connect_map(r8a66597, addr);
2084 else {
2085 struct r8a66597_device *dev;
2086
2087 spin_lock_irqsave(&r8a66597->lock, flags);
2088 dev = get_r8a66597_device(r8a66597, addr);
2089 disable_r8a66597_pipe_all(r8a66597, dev);
Yoshihiro Shimodad8359332010-03-16 12:29:35 +09002090 free_usb_address(r8a66597, dev, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002091 put_child_connect_map(r8a66597, addr);
2092 spin_unlock_irqrestore(&r8a66597->lock, flags);
2093 }
2094 }
2095 }
2096}
2097
2098static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
2099 struct usb_hcd *hcd)
2100{
2101 struct usb_bus *bus;
2102 unsigned long now_map[4];
2103
2104 memset(now_map, 0, sizeof(now_map));
2105
2106 list_for_each_entry(bus, &usb_bus_list, bus_list) {
2107 if (!bus->root_hub)
2108 continue;
2109
2110 if (bus->busnum != hcd->self.busnum)
2111 continue;
2112
2113 collect_usb_address_map(bus->root_hub, now_map);
2114 update_usb_address_map(r8a66597, bus->root_hub, now_map);
2115 }
2116}
2117
2118static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2119{
2120 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2121 unsigned long flags;
2122 int i;
2123
2124 r8a66597_check_detect_child(r8a66597, hcd);
2125
2126 spin_lock_irqsave(&r8a66597->lock, flags);
2127
2128 *buf = 0; /* initialize (no change) */
2129
Magnus Damm719a72b2009-07-17 14:59:55 +00002130 for (i = 0; i < r8a66597->max_root_hub; i++) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002131 if (r8a66597->root_hub[i].port & 0xffff0000)
2132 *buf |= 1 << (i + 1);
2133 }
2134
2135 spin_unlock_irqrestore(&r8a66597->lock, flags);
2136
2137 return (*buf != 0);
2138}
2139
2140static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2141 struct usb_hub_descriptor *desc)
2142{
2143 desc->bDescriptorType = 0x29;
2144 desc->bHubContrCurrent = 0;
Magnus Damm719a72b2009-07-17 14:59:55 +00002145 desc->bNbrPorts = r8a66597->max_root_hub;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002146 desc->bDescLength = 9;
2147 desc->bPwrOn2PwrGood = 0;
2148 desc->wHubCharacteristics = cpu_to_le16(0x0011);
John Youndbe79bb2001-09-17 00:00:00 -07002149 desc->u.hs.DeviceRemovable[0] =
2150 ((1 << r8a66597->max_root_hub) - 1) << 1;
2151 desc->u.hs.DeviceRemovable[1] = ~0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002152}
2153
2154static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2155 u16 wIndex, char *buf, u16 wLength)
2156{
2157 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2158 int ret;
2159 int port = (wIndex & 0x00FF) - 1;
2160 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2161 unsigned long flags;
2162
2163 ret = 0;
2164
2165 spin_lock_irqsave(&r8a66597->lock, flags);
2166 switch (typeReq) {
2167 case ClearHubFeature:
2168 case SetHubFeature:
2169 switch (wValue) {
2170 case C_HUB_OVER_CURRENT:
2171 case C_HUB_LOCAL_POWER:
2172 break;
2173 default:
2174 goto error;
2175 }
2176 break;
2177 case ClearPortFeature:
Magnus Damm719a72b2009-07-17 14:59:55 +00002178 if (wIndex > r8a66597->max_root_hub)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002179 goto error;
2180 if (wLength != 0)
2181 goto error;
2182
2183 switch (wValue) {
2184 case USB_PORT_FEAT_ENABLE:
Alan Stern749da5f2010-03-04 17:05:08 -05002185 rh->port &= ~USB_PORT_STAT_POWER;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002186 break;
2187 case USB_PORT_FEAT_SUSPEND:
2188 break;
2189 case USB_PORT_FEAT_POWER:
2190 r8a66597_port_power(r8a66597, port, 0);
2191 break;
2192 case USB_PORT_FEAT_C_ENABLE:
2193 case USB_PORT_FEAT_C_SUSPEND:
2194 case USB_PORT_FEAT_C_CONNECTION:
2195 case USB_PORT_FEAT_C_OVER_CURRENT:
2196 case USB_PORT_FEAT_C_RESET:
2197 break;
2198 default:
2199 goto error;
2200 }
2201 rh->port &= ~(1 << wValue);
2202 break;
2203 case GetHubDescriptor:
2204 r8a66597_hub_descriptor(r8a66597,
2205 (struct usb_hub_descriptor *)buf);
2206 break;
2207 case GetHubStatus:
2208 *buf = 0x00;
2209 break;
2210 case GetPortStatus:
Magnus Damm719a72b2009-07-17 14:59:55 +00002211 if (wIndex > r8a66597->max_root_hub)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002212 goto error;
Al Virofd05e722008-04-28 07:00:16 +01002213 *(__le32 *)buf = cpu_to_le32(rh->port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002214 break;
2215 case SetPortFeature:
Magnus Damm719a72b2009-07-17 14:59:55 +00002216 if (wIndex > r8a66597->max_root_hub)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002217 goto error;
2218 if (wLength != 0)
2219 goto error;
2220
2221 switch (wValue) {
2222 case USB_PORT_FEAT_SUSPEND:
2223 break;
2224 case USB_PORT_FEAT_POWER:
2225 r8a66597_port_power(r8a66597, port, 1);
Alan Stern749da5f2010-03-04 17:05:08 -05002226 rh->port |= USB_PORT_STAT_POWER;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002227 break;
2228 case USB_PORT_FEAT_RESET: {
2229 struct r8a66597_device *dev = rh->dev;
2230
Alan Stern749da5f2010-03-04 17:05:08 -05002231 rh->port |= USB_PORT_STAT_RESET;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002232
2233 disable_r8a66597_pipe_all(r8a66597, dev);
Yoshihiro Shimodad8359332010-03-16 12:29:35 +09002234 free_usb_address(r8a66597, dev, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002235
2236 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2237 get_dvstctr_reg(port));
2238 mod_timer(&r8a66597->rh_timer,
2239 jiffies + msecs_to_jiffies(50));
2240 }
2241 break;
2242 default:
2243 goto error;
2244 }
2245 rh->port |= 1 << wValue;
2246 break;
2247 default:
2248error:
2249 ret = -EPIPE;
2250 break;
2251 }
2252
2253 spin_unlock_irqrestore(&r8a66597->lock, flags);
2254 return ret;
2255}
2256
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002257#if defined(CONFIG_PM)
2258static int r8a66597_bus_suspend(struct usb_hcd *hcd)
2259{
2260 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2261 int port;
2262
Greg Kroah-Hartman167e4922012-05-01 21:33:46 -07002263 dev_dbg(&r8a66597->device0.udev->dev, "%s\n", __func__);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002264
Magnus Damm719a72b2009-07-17 14:59:55 +00002265 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002266 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2267 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2268
Alan Stern749da5f2010-03-04 17:05:08 -05002269 if (!(rh->port & USB_PORT_STAT_ENABLE))
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002270 continue;
2271
Greg Kroah-Hartman167e4922012-05-01 21:33:46 -07002272 dev_dbg(&rh->dev->udev->dev, "suspend port = %d\n", port);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002273 r8a66597_bclr(r8a66597, UACT, dvstctr_reg); /* suspend */
Alan Stern749da5f2010-03-04 17:05:08 -05002274 rh->port |= USB_PORT_STAT_SUSPEND;
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002275
2276 if (rh->dev->udev->do_remote_wakeup) {
2277 msleep(3); /* waiting last SOF */
2278 r8a66597_bset(r8a66597, RWUPE, dvstctr_reg);
2279 r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
2280 r8a66597_bset(r8a66597, BCHGE, get_intenb_reg(port));
2281 }
2282 }
2283
2284 r8a66597->bus_suspended = 1;
2285
2286 return 0;
2287}
2288
2289static int r8a66597_bus_resume(struct usb_hcd *hcd)
2290{
2291 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2292 int port;
2293
Greg Kroah-Hartman167e4922012-05-01 21:33:46 -07002294 dev_dbg(&r8a66597->device0.udev->dev, "%s\n", __func__);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002295
Magnus Damm719a72b2009-07-17 14:59:55 +00002296 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002297 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2298 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2299
Alan Stern749da5f2010-03-04 17:05:08 -05002300 if (!(rh->port & USB_PORT_STAT_SUSPEND))
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002301 continue;
2302
Greg Kroah-Hartman167e4922012-05-01 21:33:46 -07002303 dev_dbg(&rh->dev->udev->dev, "resume port = %d\n", port);
Alan Stern749da5f2010-03-04 17:05:08 -05002304 rh->port &= ~USB_PORT_STAT_SUSPEND;
Kuninori Morimoto35da4132011-07-07 02:36:32 -07002305 rh->port |= USB_PORT_STAT_C_SUSPEND << 16;
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002306 r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
2307 msleep(50);
2308 r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
2309 }
2310
2311 return 0;
2312
2313}
2314#else
2315#define r8a66597_bus_suspend NULL
2316#define r8a66597_bus_resume NULL
2317#endif
2318
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002319static struct hc_driver r8a66597_hc_driver = {
2320 .description = hcd_name,
2321 .hcd_priv_size = sizeof(struct r8a66597),
2322 .irq = r8a66597_irq,
2323
2324 /*
2325 * generic hardware linkage
2326 */
2327 .flags = HCD_USB2,
2328
2329 .start = r8a66597_start,
2330 .stop = r8a66597_stop,
2331
2332 /*
2333 * managing i/o requests and associated device resources
2334 */
2335 .urb_enqueue = r8a66597_urb_enqueue,
2336 .urb_dequeue = r8a66597_urb_dequeue,
2337 .endpoint_disable = r8a66597_endpoint_disable,
2338
2339 /*
2340 * periodic schedule support
2341 */
2342 .get_frame_number = r8a66597_get_frame,
2343
2344 /*
2345 * root hub support
2346 */
2347 .hub_status_data = r8a66597_hub_status_data,
2348 .hub_control = r8a66597_hub_control,
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002349 .bus_suspend = r8a66597_bus_suspend,
2350 .bus_resume = r8a66597_bus_resume,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002351};
2352
2353#if defined(CONFIG_PM)
Magnus Dammae1cef62009-07-17 14:52:05 +00002354static int r8a66597_suspend(struct device *dev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002355{
Magnus Dammae1cef62009-07-17 14:52:05 +00002356 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002357 int port;
2358
Greg Kroah-Hartman167e4922012-05-01 21:33:46 -07002359 dev_dbg(dev, "%s\n", __func__);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002360
2361 disable_controller(r8a66597);
2362
Magnus Damm719a72b2009-07-17 14:59:55 +00002363 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002364 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2365
2366 rh->port = 0x00000000;
2367 }
2368
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002369 return 0;
2370}
2371
Magnus Dammae1cef62009-07-17 14:52:05 +00002372static int r8a66597_resume(struct device *dev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002373{
Magnus Dammae1cef62009-07-17 14:52:05 +00002374 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002375 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2376
Greg Kroah-Hartman167e4922012-05-01 21:33:46 -07002377 dev_dbg(dev, "%s\n", __func__);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002378
2379 enable_controller(r8a66597);
2380 usb_root_hub_lost_power(hcd->self.root_hub);
2381
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002382 return 0;
2383}
Magnus Dammae1cef62009-07-17 14:52:05 +00002384
Alexey Dobriyan47145212009-12-14 18:00:08 -08002385static const struct dev_pm_ops r8a66597_dev_pm_ops = {
Magnus Dammae1cef62009-07-17 14:52:05 +00002386 .suspend = r8a66597_suspend,
2387 .resume = r8a66597_resume,
Yoshihiro Shimoda3725f282009-07-29 09:24:41 +00002388 .poweroff = r8a66597_suspend,
2389 .restore = r8a66597_resume,
Magnus Dammae1cef62009-07-17 14:52:05 +00002390};
2391
2392#define R8A66597_DEV_PM_OPS (&r8a66597_dev_pm_ops)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002393#else /* if defined(CONFIG_PM) */
Magnus Dammae1cef62009-07-17 14:52:05 +00002394#define R8A66597_DEV_PM_OPS NULL
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002395#endif
2396
Paul Mundt0238a0d2010-09-16 17:16:31 +09002397static int __devexit r8a66597_remove(struct platform_device *pdev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002398{
2399 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2400 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2401
2402 del_timer_sync(&r8a66597->rh_timer);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002403 usb_remove_hcd(hcd);
Paul Mundt1c983472010-06-02 16:27:54 +09002404 iounmap(r8a66597->reg);
Magnus Damm719a72b2009-07-17 14:59:55 +00002405 if (r8a66597->pdata->on_chip)
2406 clk_put(r8a66597->clk);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002407 usb_put_hcd(hcd);
2408 return 0;
2409}
2410
Uwe Kleine-König8864bd82009-03-28 00:27:00 +01002411static int __devinit r8a66597_probe(struct platform_device *pdev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002412{
Magnus Damm765786e2008-10-31 20:22:38 +09002413 char clk_name[8];
Marc Zyngier27140212008-08-18 13:08:42 +02002414 struct resource *res = NULL, *ires;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002415 int irq = -1;
2416 void __iomem *reg = NULL;
2417 struct usb_hcd *hcd = NULL;
2418 struct r8a66597 *r8a66597;
2419 int ret = 0;
2420 int i;
Yoshihiro Shimoda0bf32b82008-06-27 19:09:55 +09002421 unsigned long irq_trigger;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002422
Tobias Klauser427914802012-02-28 12:57:22 +01002423 if (usb_disabled())
2424 return -ENODEV;
2425
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002426 if (pdev->dev.dma_mask) {
2427 ret = -EINVAL;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002428 dev_err(&pdev->dev, "dma not supported\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002429 goto clean_up;
2430 }
2431
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002432 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002433 if (!res) {
2434 ret = -ENODEV;
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002435 dev_err(&pdev->dev, "platform_get_resource error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002436 goto clean_up;
2437 }
2438
Marc Zyngier27140212008-08-18 13:08:42 +02002439 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2440 if (!ires) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002441 ret = -ENODEV;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002442 dev_err(&pdev->dev,
2443 "platform_get_resource IORESOURCE_IRQ error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002444 goto clean_up;
2445 }
2446
Marc Zyngier27140212008-08-18 13:08:42 +02002447 irq = ires->start;
2448 irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
2449
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002450 reg = ioremap(res->start, resource_size(res));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002451 if (reg == NULL) {
2452 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002453 dev_err(&pdev->dev, "ioremap error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002454 goto clean_up;
2455 }
2456
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +09002457 if (pdev->dev.platform_data == NULL) {
2458 dev_err(&pdev->dev, "no platform data\n");
2459 ret = -ENODEV;
2460 goto clean_up;
2461 }
2462
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002463 /* initialize hcd */
2464 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2465 if (!hcd) {
2466 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002467 dev_err(&pdev->dev, "Failed to create hcd\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002468 goto clean_up;
2469 }
2470 r8a66597 = hcd_to_r8a66597(hcd);
2471 memset(r8a66597, 0, sizeof(struct r8a66597));
2472 dev_set_drvdata(&pdev->dev, r8a66597);
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +09002473 r8a66597->pdata = pdev->dev.platform_data;
2474 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002475
Magnus Damm719a72b2009-07-17 14:59:55 +00002476 if (r8a66597->pdata->on_chip) {
Magnus Damm719a72b2009-07-17 14:59:55 +00002477 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
2478 r8a66597->clk = clk_get(&pdev->dev, clk_name);
2479 if (IS_ERR(r8a66597->clk)) {
2480 dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
2481 clk_name);
2482 ret = PTR_ERR(r8a66597->clk);
2483 goto clean_up2;
2484 }
Magnus Damm719a72b2009-07-17 14:59:55 +00002485 r8a66597->max_root_hub = 1;
2486 } else
2487 r8a66597->max_root_hub = 2;
Magnus Damm765786e2008-10-31 20:22:38 +09002488
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002489 spin_lock_init(&r8a66597->lock);
2490 init_timer(&r8a66597->rh_timer);
2491 r8a66597->rh_timer.function = r8a66597_timer;
2492 r8a66597->rh_timer.data = (unsigned long)r8a66597;
Paul Mundt1c983472010-06-02 16:27:54 +09002493 r8a66597->reg = reg;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002494
Magnus Damme5ff15b2010-01-27 07:41:19 +00002495 /* make sure no interrupts are pending */
2496 ret = r8a66597_clock_enable(r8a66597);
2497 if (ret < 0)
2498 goto clean_up3;
2499 disable_controller(r8a66597);
2500
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002501 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2502 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2503 init_timer(&r8a66597->td_timer[i]);
2504 r8a66597->td_timer[i].function = r8a66597_td_timer;
2505 r8a66597->td_timer[i].data = (unsigned long)r8a66597;
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09002506 setup_timer(&r8a66597->interval_timer[i],
2507 r8a66597_interval_timer,
2508 (unsigned long)r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002509 }
2510 INIT_LIST_HEAD(&r8a66597->child_device);
2511
2512 hcd->rsrc_start = res->start;
Yoshihiro Shimoda87abd0a2011-06-21 13:24:42 +09002513 hcd->has_tt = 1;
Marc Zyngier27140212008-08-18 13:08:42 +02002514
Yong Zhangb5dd18d2011-09-07 16:10:52 +08002515 ret = usb_add_hcd(hcd, irq, irq_trigger);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002516 if (ret != 0) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002517 dev_err(&pdev->dev, "Failed to add hcd\n");
Magnus Damm765786e2008-10-31 20:22:38 +09002518 goto clean_up3;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002519 }
2520
2521 return 0;
2522
Magnus Damm765786e2008-10-31 20:22:38 +09002523clean_up3:
Magnus Damm719a72b2009-07-17 14:59:55 +00002524 if (r8a66597->pdata->on_chip)
2525 clk_put(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +09002526clean_up2:
2527 usb_put_hcd(hcd);
2528
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002529clean_up:
2530 if (reg)
2531 iounmap(reg);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002532
2533 return ret;
2534}
2535
2536static struct platform_driver r8a66597_driver = {
2537 .probe = r8a66597_probe,
Paul Mundt0238a0d2010-09-16 17:16:31 +09002538 .remove = __devexit_p(r8a66597_remove),
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002539 .driver = {
2540 .name = (char *) hcd_name,
Kay Sieversf4fce612008-04-10 21:29:22 -07002541 .owner = THIS_MODULE,
Magnus Dammae1cef62009-07-17 14:52:05 +00002542 .pm = R8A66597_DEV_PM_OPS,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002543 },
2544};
2545
Tobias Klauser427914802012-02-28 12:57:22 +01002546module_platform_driver(r8a66597_driver);