blob: 7bf78be1fd32503571df68999197008c85b64879 [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>
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090030#include <linux/timer.h>
31#include <linux/delay.h>
32#include <linux/list.h>
33#include <linux/interrupt.h>
34#include <linux/usb.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020035#include <linux/usb/hcd.h>
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090036#include <linux/platform_device.h>
Yoshihiro Shimodae2945312007-07-18 23:10:34 +090037#include <linux/io.h>
Paul Mundt27175682010-02-04 06:57:58 +000038#include <linux/mm.h>
Yoshihiro Shimodae2945312007-07-18 23:10:34 +090039#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Paul Mundt27175682010-02-04 06:57:58 +000041#include <asm/cacheflush.h>
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090042
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090043#include "r8a66597.h"
44
45MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
46MODULE_LICENSE("GPL");
47MODULE_AUTHOR("Yoshihiro Shimoda");
Kay Sieversf4fce612008-04-10 21:29:22 -070048MODULE_ALIAS("platform:r8a66597_hcd");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090049
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +090050#define DRIVER_VERSION "2009-05-26"
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090051
52static const char hcd_name[] = "r8a66597_hcd";
53
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090054static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
55static int r8a66597_get_frame(struct usb_hcd *hcd);
56
57/* this function must be called with interrupt disabled */
58static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
59 unsigned long reg)
60{
61 u16 tmp;
62
63 tmp = r8a66597_read(r8a66597, INTENB0);
64 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
65 r8a66597_bset(r8a66597, 1 << pipenum, reg);
66 r8a66597_write(r8a66597, tmp, INTENB0);
67}
68
69/* this function must be called with interrupt disabled */
70static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
71 unsigned long reg)
72{
73 u16 tmp;
74
75 tmp = r8a66597_read(r8a66597, INTENB0);
76 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
77 r8a66597_bclr(r8a66597, 1 << pipenum, reg);
78 r8a66597_write(r8a66597, tmp, INTENB0);
79}
80
81static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
82 u16 usbspd, u8 upphub, u8 hubport, int port)
83{
84 u16 val;
85 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
86
87 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
88 r8a66597_write(r8a66597, val, devadd_reg);
89}
90
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +090091static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090092{
93 u16 tmp;
94 int i = 0;
95
Magnus Damm719a72b2009-07-17 14:59:55 +000096 if (r8a66597->pdata->on_chip) {
Laurent Pinchart9b547a82013-10-28 23:49:27 +010097 clk_prepare_enable(r8a66597->clk);
Magnus Damm719a72b2009-07-17 14:59:55 +000098 do {
99 r8a66597_write(r8a66597, SCKE, SYSCFG0);
100 tmp = r8a66597_read(r8a66597, SYSCFG0);
101 if (i++ > 1000) {
102 printk(KERN_ERR "r8a66597: reg access fail.\n");
103 return -ENXIO;
104 }
105 } while ((tmp & SCKE) != SCKE);
106 r8a66597_write(r8a66597, 0x04, 0x02);
107 } else {
108 do {
109 r8a66597_write(r8a66597, USBE, SYSCFG0);
110 tmp = r8a66597_read(r8a66597, SYSCFG0);
111 if (i++ > 1000) {
112 printk(KERN_ERR "r8a66597: reg access fail.\n");
113 return -ENXIO;
114 }
115 } while ((tmp & USBE) != USBE);
116 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
117 r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata),
118 XTAL, SYSCFG0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900119
Magnus Damm719a72b2009-07-17 14:59:55 +0000120 i = 0;
121 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
122 do {
123 msleep(1);
124 tmp = r8a66597_read(r8a66597, SYSCFG0);
125 if (i++ > 500) {
126 printk(KERN_ERR "r8a66597: reg access fail.\n");
127 return -ENXIO;
128 }
129 } while ((tmp & SCKE) != SCKE);
130 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900131
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900132 return 0;
133}
134
135static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
136{
137 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
138 udelay(1);
Magnus Damm719a72b2009-07-17 14:59:55 +0000139
140 if (r8a66597->pdata->on_chip) {
Laurent Pinchart9b547a82013-10-28 23:49:27 +0100141 clk_disable_unprepare(r8a66597->clk);
Magnus Damm719a72b2009-07-17 14:59:55 +0000142 } else {
143 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
144 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
145 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
146 }
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900147}
148
149static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
150{
151 u16 val;
152
153 val = port ? DRPD : DCFM | DRPD;
154 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
155 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
156
157 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
158 r8a66597_bclr(r8a66597, DTCHE, get_intenb_reg(port));
159 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
160}
161
162static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
163{
164 u16 val, tmp;
165
166 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
167 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
168
169 r8a66597_port_power(r8a66597, port, 0);
170
171 do {
172 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
173 udelay(640);
174 } while (tmp == EDGESTS);
175
176 val = port ? DRPD : DCFM | DRPD;
177 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
178 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
179}
180
181static int enable_controller(struct r8a66597 *r8a66597)
182{
183 int ret, port;
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +0900184 u16 vif = r8a66597->pdata->vif ? LDRV : 0;
185 u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
186 u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900187
188 ret = r8a66597_clock_enable(r8a66597);
189 if (ret < 0)
190 return ret;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900191
192 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900193 r8a66597_bset(r8a66597, USBE, SYSCFG0);
194
195 r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
196 r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
197 r8a66597_bset(r8a66597, BRDY0, BRDYENB);
198 r8a66597_bset(r8a66597, BEMP0, BEMPENB);
199
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900200 r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
201 r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
202 r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900203 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
204
205 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900206
Magnus Damm719a72b2009-07-17 14:59:55 +0000207 for (port = 0; port < r8a66597->max_root_hub; port++)
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900208 r8a66597_enable_port(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900209
210 return 0;
211}
212
213static void disable_controller(struct r8a66597 *r8a66597)
214{
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900215 int port;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900216
Magnus Damme5ff15b2010-01-27 07:41:19 +0000217 /* disable interrupts */
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900218 r8a66597_write(r8a66597, 0, INTENB0);
Magnus Damme5ff15b2010-01-27 07:41:19 +0000219 r8a66597_write(r8a66597, 0, INTENB1);
220 r8a66597_write(r8a66597, 0, BRDYENB);
221 r8a66597_write(r8a66597, 0, BEMPENB);
222 r8a66597_write(r8a66597, 0, NRDYENB);
223
224 /* clear status */
225 r8a66597_write(r8a66597, 0, BRDYSTS);
226 r8a66597_write(r8a66597, 0, NRDYSTS);
227 r8a66597_write(r8a66597, 0, BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900228
Magnus Damm719a72b2009-07-17 14:59:55 +0000229 for (port = 0; port < r8a66597->max_root_hub; port++)
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900230 r8a66597_disable_port(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900231
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900232 r8a66597_clock_disable(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900233}
234
235static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
236 struct usb_device *udev)
237{
238 struct r8a66597_device *dev;
239
240 if (udev->parent && udev->parent->devnum != 1)
241 udev = udev->parent;
242
243 dev = dev_get_drvdata(&udev->dev);
244 if (dev)
245 return dev->address;
246 else
247 return 0;
248}
249
250static int is_child_device(char *devpath)
251{
252 return (devpath[2] ? 1 : 0);
253}
254
255static int is_hub_limit(char *devpath)
256{
257 return ((strlen(devpath) >= 4) ? 1 : 0);
258}
259
Magnus Damm719a72b2009-07-17 14:59:55 +0000260static void get_port_number(struct r8a66597 *r8a66597,
261 char *devpath, u16 *root_port, u16 *hub_port)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900262{
263 if (root_port) {
264 *root_port = (devpath[0] & 0x0F) - 1;
Magnus Damm719a72b2009-07-17 14:59:55 +0000265 if (*root_port >= r8a66597->max_root_hub)
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700266 printk(KERN_ERR "r8a66597: Illegal root port number.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900267 }
268 if (hub_port)
269 *hub_port = devpath[2] & 0x0F;
270}
271
272static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
273{
274 u16 usbspd = 0;
275
276 switch (speed) {
277 case USB_SPEED_LOW:
278 usbspd = LSMODE;
279 break;
280 case USB_SPEED_FULL:
281 usbspd = FSMODE;
282 break;
283 case USB_SPEED_HIGH:
284 usbspd = HSMODE;
285 break;
286 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700287 printk(KERN_ERR "r8a66597: unknown speed\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900288 break;
289 }
290
291 return usbspd;
292}
293
294static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
295{
296 int idx;
297
298 idx = address / 32;
299 r8a66597->child_connect_map[idx] |= 1 << (address % 32);
300}
301
302static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
303{
304 int idx;
305
306 idx = address / 32;
307 r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
308}
309
310static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
311{
312 u16 pipenum = pipe->info.pipenum;
Ming Leife9b9032008-06-08 16:13:03 +0800313 const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
314 const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
315 const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900316
317 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
318 dma_ch = R8A66597_PIPE_NO_DMA;
319
320 pipe->fifoaddr = fifoaddr[dma_ch];
321 pipe->fifosel = fifosel[dma_ch];
322 pipe->fifoctr = fifoctr[dma_ch];
323
324 if (pipenum == 0)
325 pipe->pipectr = DCPCTR;
326 else
327 pipe->pipectr = get_pipectr_addr(pipenum);
328
329 if (check_bulk_or_isoc(pipenum)) {
330 pipe->pipetre = get_pipetre_addr(pipenum);
331 pipe->pipetrn = get_pipetrn_addr(pipenum);
332 } else {
333 pipe->pipetre = 0;
334 pipe->pipetrn = 0;
335 }
336}
337
338static struct r8a66597_device *
339get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
340{
341 if (usb_pipedevice(urb->pipe) == 0)
342 return &r8a66597->device0;
343
344 return dev_get_drvdata(&urb->dev->dev);
345}
346
347static int make_r8a66597_device(struct r8a66597 *r8a66597,
348 struct urb *urb, u8 addr)
349{
350 struct r8a66597_device *dev;
351 int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
352
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900353 dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900354 if (dev == NULL)
355 return -ENOMEM;
356
357 dev_set_drvdata(&urb->dev->dev, dev);
358 dev->udev = urb->dev;
359 dev->address = addr;
360 dev->usb_address = usb_address;
361 dev->state = USB_STATE_ADDRESS;
362 dev->ep_in_toggle = 0;
363 dev->ep_out_toggle = 0;
364 INIT_LIST_HEAD(&dev->device_list);
365 list_add_tail(&dev->device_list, &r8a66597->child_device);
366
Magnus Damm719a72b2009-07-17 14:59:55 +0000367 get_port_number(r8a66597, urb->dev->devpath,
368 &dev->root_port, &dev->hub_port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900369 if (!is_child_device(urb->dev->devpath))
370 r8a66597->root_hub[dev->root_port].dev = dev;
371
372 set_devadd_reg(r8a66597, dev->address,
373 get_r8a66597_usb_speed(urb->dev->speed),
374 get_parent_r8a66597_address(r8a66597, urb->dev),
375 dev->hub_port, dev->root_port);
376
377 return 0;
378}
379
380/* this function must be called with interrupt disabled */
381static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
382{
383 u8 addr; /* R8A66597's address */
384 struct r8a66597_device *dev;
385
386 if (is_hub_limit(urb->dev->devpath)) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700387 dev_err(&urb->dev->dev, "External hub limit reached.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900388 return 0;
389 }
390
391 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
392 if (dev && dev->state >= USB_STATE_ADDRESS)
393 return dev->address;
394
395 for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
396 if (r8a66597->address_map & (1 << addr))
397 continue;
398
Greg Kroah-Hartman167e4922012-05-01 21:33:46 -0700399 dev_dbg(&urb->dev->dev, "alloc_address: r8a66597_addr=%d\n", addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900400 r8a66597->address_map |= 1 << addr;
401
402 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
403 return 0;
404
405 return addr;
406 }
407
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700408 dev_err(&urb->dev->dev,
409 "cannot communicate with a USB device more than 10.(%x)\n",
410 r8a66597->address_map);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900411
412 return 0;
413}
414
415/* this function must be called with interrupt disabled */
416static void free_usb_address(struct r8a66597 *r8a66597,
Yoshihiro Shimodad8359332010-03-16 12:29:35 +0900417 struct r8a66597_device *dev, int reset)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900418{
419 int port;
420
421 if (!dev)
422 return;
423
Greg Kroah-Hartman167e4922012-05-01 21:33:46 -0700424 dev_dbg(&dev->udev->dev, "free_addr: addr=%d\n", dev->address);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900425
426 dev->state = USB_STATE_DEFAULT;
427 r8a66597->address_map &= ~(1 << dev->address);
428 dev->address = 0;
Yoshihiro Shimodad8359332010-03-16 12:29:35 +0900429 /*
430 * Only when resetting USB, it is necessary to erase drvdata. When
431 * a usb device with usb hub is disconnect, "dev->udev" is already
432 * freed on usb_desconnect(). So we cannot access the data.
433 */
434 if (reset)
435 dev_set_drvdata(&dev->udev->dev, NULL);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900436 list_del(&dev->device_list);
437 kfree(dev);
438
Magnus Damm719a72b2009-07-17 14:59:55 +0000439 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900440 if (r8a66597->root_hub[port].dev == dev) {
441 r8a66597->root_hub[port].dev = NULL;
442 break;
443 }
444 }
445}
446
447static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
448 u16 mask, u16 loop)
449{
450 u16 tmp;
451 int i = 0;
452
453 do {
454 tmp = r8a66597_read(r8a66597, reg);
455 if (i++ > 1000000) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700456 printk(KERN_ERR "r8a66597: register%lx, loop %x "
457 "is timeout\n", reg, loop);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900458 break;
459 }
460 ndelay(1);
461 } while ((tmp & mask) != loop);
462}
463
464/* this function must be called with interrupt disabled */
465static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
466{
467 u16 tmp;
468
469 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
470 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
471 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
472 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
473}
474
475/* this function must be called with interrupt disabled */
476static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
477{
478 u16 tmp;
479
480 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
481 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
482 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
483 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
484 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
485}
486
487/* this function must be called with interrupt disabled */
488static void clear_all_buffer(struct r8a66597 *r8a66597,
489 struct r8a66597_pipe *pipe)
490{
491 u16 tmp;
492
493 if (!pipe || pipe->info.pipenum == 0)
494 return;
495
496 pipe_stop(r8a66597, pipe);
497 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
498 tmp = r8a66597_read(r8a66597, pipe->pipectr);
499 tmp = r8a66597_read(r8a66597, pipe->pipectr);
500 tmp = r8a66597_read(r8a66597, pipe->pipectr);
501 r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
502}
503
504/* this function must be called with interrupt disabled */
505static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
506 struct r8a66597_pipe *pipe, int toggle)
507{
508 if (toggle)
509 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
510 else
511 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
512}
513
Magnus Damm719a72b2009-07-17 14:59:55 +0000514static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
515{
516 if (r8a66597->pdata->on_chip)
517 return MBW_32;
518 else
519 return MBW_16;
520}
521
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900522/* this function must be called with interrupt disabled */
523static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
524{
Magnus Damm719a72b2009-07-17 14:59:55 +0000525 unsigned short mbw = mbw_value(r8a66597);
526
527 r8a66597_mdfy(r8a66597, mbw | pipenum, mbw | CURPIPE, CFIFOSEL);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900528 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
529}
530
531/* this function must be called with interrupt disabled */
532static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
533 struct r8a66597_pipe *pipe)
534{
Magnus Damm719a72b2009-07-17 14:59:55 +0000535 unsigned short mbw = mbw_value(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900536
Magnus Damm719a72b2009-07-17 14:59:55 +0000537 cfifo_change(r8a66597, 0);
538 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D0FIFOSEL);
539 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D1FIFOSEL);
540
541 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum, mbw | CURPIPE,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900542 pipe->fifosel);
543 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
544}
545
546static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
547{
548 struct r8a66597_pipe *pipe = hep->hcpriv;
549
550 if (usb_pipeendpoint(urb->pipe) == 0)
551 return 0;
552 else
553 return pipe->info.pipenum;
554}
555
556static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
557{
558 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
559
560 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
561}
562
563static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
564 int urb_pipe)
565{
566 if (!dev)
567 return NULL;
568
569 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
570}
571
572/* this function must be called with interrupt disabled */
573static void pipe_toggle_set(struct r8a66597 *r8a66597,
574 struct r8a66597_pipe *pipe,
575 struct urb *urb, int set)
576{
577 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
578 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
579 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
580
581 if (!toggle)
582 return;
583
584 if (set)
585 *toggle |= 1 << endpoint;
586 else
587 *toggle &= ~(1 << endpoint);
588}
589
590/* this function must be called with interrupt disabled */
591static void pipe_toggle_save(struct r8a66597 *r8a66597,
592 struct r8a66597_pipe *pipe,
593 struct urb *urb)
594{
595 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
596 pipe_toggle_set(r8a66597, pipe, urb, 1);
597 else
598 pipe_toggle_set(r8a66597, pipe, urb, 0);
599}
600
601/* this function must be called with interrupt disabled */
602static void pipe_toggle_restore(struct r8a66597 *r8a66597,
603 struct r8a66597_pipe *pipe,
604 struct urb *urb)
605{
606 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
607 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
608 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
609
Yoshihiro Shimodaf6ace2c2007-05-30 20:42:41 +0900610 if (!toggle)
611 return;
612
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900613 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
614}
615
616/* this function must be called with interrupt disabled */
617static void pipe_buffer_setting(struct r8a66597 *r8a66597,
618 struct r8a66597_pipe_info *info)
619{
620 u16 val = 0;
621
622 if (info->pipenum == 0)
623 return;
624
625 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
626 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
627 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
628 if (!info->dir_in)
629 val |= R8A66597_DIR;
630 if (info->type == R8A66597_BULK && info->dir_in)
631 val |= R8A66597_DBLB | R8A66597_SHTNAK;
632 val |= info->type | info->epnum;
633 r8a66597_write(r8a66597, val, PIPECFG);
634
635 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
636 PIPEBUF);
637 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
638 PIPEMAXP);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900639 r8a66597_write(r8a66597, info->interval, PIPEPERI);
640}
641
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900642/* this function must be called with interrupt disabled */
643static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
644{
645 struct r8a66597_pipe_info *info;
646 struct urb *urb = td->urb;
647
648 if (td->pipenum > 0) {
649 info = &td->pipe->info;
650 cfifo_change(r8a66597, 0);
651 pipe_buffer_setting(r8a66597, info);
652
653 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
654 usb_pipeout(urb->pipe)) &&
655 !usb_pipecontrol(urb->pipe)) {
656 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
657 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
658 clear_all_buffer(r8a66597, td->pipe);
659 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
660 usb_pipeout(urb->pipe), 1);
661 }
662 pipe_toggle_restore(r8a66597, td->pipe, urb);
663 }
664}
665
666/* this function must be called with interrupt disabled */
667static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
668 struct usb_endpoint_descriptor *ep)
669{
670 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
671
672 memset(array, 0, sizeof(array));
Julia Lawall2e0fe702008-12-29 11:22:14 +0100673 switch (usb_endpoint_type(ep)) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900674 case USB_ENDPOINT_XFER_BULK:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100675 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900676 array[i++] = 4;
677 else {
678 array[i++] = 3;
679 array[i++] = 5;
680 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900681 break;
682 case USB_ENDPOINT_XFER_INT:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100683 if (usb_endpoint_dir_in(ep)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900684 array[i++] = 6;
685 array[i++] = 7;
686 array[i++] = 8;
687 } else
688 array[i++] = 9;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900689 break;
690 case USB_ENDPOINT_XFER_ISOC:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100691 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900692 array[i++] = 2;
693 else
694 array[i++] = 1;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900695 break;
696 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700697 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900698 return 0;
699 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900700
701 i = 1;
702 min = array[0];
703 while (array[i] != 0) {
704 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
705 min = array[i];
706 i++;
707 }
708
709 return min;
710}
711
712static u16 get_r8a66597_type(__u8 type)
713{
714 u16 r8a66597_type;
715
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900716 switch (type) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900717 case USB_ENDPOINT_XFER_BULK:
718 r8a66597_type = R8A66597_BULK;
719 break;
720 case USB_ENDPOINT_XFER_INT:
721 r8a66597_type = R8A66597_INT;
722 break;
723 case USB_ENDPOINT_XFER_ISOC:
724 r8a66597_type = R8A66597_ISO;
725 break;
726 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700727 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900728 r8a66597_type = 0x0000;
729 break;
730 }
731
732 return r8a66597_type;
733}
734
735static u16 get_bufnum(u16 pipenum)
736{
737 u16 bufnum = 0;
738
739 if (pipenum == 0)
740 bufnum = 0;
741 else if (check_bulk_or_isoc(pipenum))
742 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
743 else if (check_interrupt(pipenum))
744 bufnum = 4 + (pipenum - 6);
745 else
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700746 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900747
748 return bufnum;
749}
750
751static u16 get_buf_bsize(u16 pipenum)
752{
753 u16 buf_bsize = 0;
754
755 if (pipenum == 0)
756 buf_bsize = 3;
757 else if (check_bulk_or_isoc(pipenum))
758 buf_bsize = R8A66597_BUF_BSIZE - 1;
759 else if (check_interrupt(pipenum))
760 buf_bsize = 0;
761 else
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700762 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900763
764 return buf_bsize;
765}
766
767/* this function must be called with interrupt disabled */
768static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
769 struct r8a66597_device *dev,
770 struct r8a66597_pipe *pipe,
771 struct urb *urb)
772{
773 int i;
774 struct r8a66597_pipe_info *info = &pipe->info;
Magnus Damm719a72b2009-07-17 14:59:55 +0000775 unsigned short mbw = mbw_value(r8a66597);
776
777 /* pipe dma is only for external controlles */
778 if (r8a66597->pdata->on_chip)
779 return;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900780
781 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
782 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
783 if ((r8a66597->dma_map & (1 << i)) != 0)
784 continue;
785
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -0700786 dev_info(&dev->udev->dev,
787 "address %d, EndpointAddress 0x%02x use "
788 "DMA FIFO\n", usb_pipedevice(urb->pipe),
789 info->dir_in ?
790 USB_ENDPOINT_DIR_MASK + info->epnum
791 : info->epnum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900792
793 r8a66597->dma_map |= 1 << i;
794 dev->dma_map |= 1 << i;
795 set_pipe_reg_addr(pipe, i);
796
797 cfifo_change(r8a66597, 0);
Magnus Damm719a72b2009-07-17 14:59:55 +0000798 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum,
799 mbw | CURPIPE, pipe->fifosel);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900800
801 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
802 pipe->info.pipenum);
803 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
804 break;
805 }
806 }
807}
808
809/* this function must be called with interrupt disabled */
810static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
811 struct usb_host_endpoint *hep,
812 struct r8a66597_pipe_info *info)
813{
814 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
815 struct r8a66597_pipe *pipe = hep->hcpriv;
816
Greg Kroah-Hartman167e4922012-05-01 21:33:46 -0700817 dev_dbg(&dev->udev->dev, "enable_pipe:\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900818
819 pipe->info = *info;
820 set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
821 r8a66597->pipe_cnt[pipe->info.pipenum]++;
822 dev->pipe_cnt[pipe->info.pipenum]++;
823
824 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
825}
826
Paul Mundt27175682010-02-04 06:57:58 +0000827static void r8a66597_urb_done(struct r8a66597 *r8a66597, struct urb *urb,
828 int status)
829__releases(r8a66597->lock)
830__acquires(r8a66597->lock)
831{
832 if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
833 void *ptr;
834
835 for (ptr = urb->transfer_buffer;
836 ptr < urb->transfer_buffer + urb->transfer_buffer_length;
837 ptr += PAGE_SIZE)
838 flush_dcache_page(virt_to_page(ptr));
839 }
840
841 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
842 spin_unlock(&r8a66597->lock);
843 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb, status);
844 spin_lock(&r8a66597->lock);
845}
846
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900847/* this function must be called with interrupt disabled */
848static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
849{
850 struct r8a66597_td *td, *next;
851 struct urb *urb;
852 struct list_head *list = &r8a66597->pipe_queue[pipenum];
853
854 if (list_empty(list))
855 return;
856
857 list_for_each_entry_safe(td, next, list, queue) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900858 if (td->address != address)
859 continue;
860
861 urb = td->urb;
862 list_del(&td->queue);
863 kfree(td);
864
Paul Mundt27175682010-02-04 06:57:58 +0000865 if (urb)
866 r8a66597_urb_done(r8a66597, urb, -ENODEV);
Alan Sterne9df41c2007-08-08 11:48:02 -0400867
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900868 break;
869 }
870}
871
872/* this function must be called with interrupt disabled */
873static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
874 struct r8a66597_device *dev)
875{
876 int check_ep0 = 0;
877 u16 pipenum;
878
879 if (!dev)
880 return;
881
882 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
883 if (!dev->pipe_cnt[pipenum])
884 continue;
885
886 if (!check_ep0) {
887 check_ep0 = 1;
888 force_dequeue(r8a66597, 0, dev->address);
889 }
890
891 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
892 dev->pipe_cnt[pipenum] = 0;
893 force_dequeue(r8a66597, pipenum, dev->address);
894 }
895
Greg Kroah-Hartman167e4922012-05-01 21:33:46 -0700896 dev_dbg(&dev->udev->dev, "disable_pipe\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900897
898 r8a66597->dma_map &= ~(dev->dma_map);
899 dev->dma_map = 0;
900}
901
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900902static u16 get_interval(struct urb *urb, __u8 interval)
903{
904 u16 time = 1;
905 int i;
906
907 if (urb->dev->speed == USB_SPEED_HIGH) {
908 if (interval > IITV)
909 time = IITV;
910 else
911 time = interval ? interval - 1 : 0;
912 } else {
913 if (interval > 128) {
914 time = IITV;
915 } else {
916 /* calculate the nearest value for PIPEPERI */
917 for (i = 0; i < 7; i++) {
918 if ((1 << i) < interval &&
919 (1 << (i + 1) > interval))
920 time = 1 << i;
921 }
922 }
923 }
924
925 return time;
926}
927
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900928static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
929{
930 __u8 i;
931 unsigned long time = 1;
932
933 if (usb_pipeisoc(urb->pipe))
934 return 0;
935
936 if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
937 for (i = 0; i < (interval - 1); i++)
938 time *= 2;
939 time = time * 125 / 1000; /* uSOF -> msec */
940 } else {
941 time = interval;
942 }
943
944 return time;
945}
946
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900947/* this function must be called with interrupt disabled */
948static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
949 struct usb_host_endpoint *hep,
950 struct usb_endpoint_descriptor *ep)
951{
952 struct r8a66597_pipe_info info;
953
954 info.pipenum = get_empty_pipenum(r8a66597, ep);
955 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
Julia Lawall2e0fe702008-12-29 11:22:14 +0100956 info.epnum = usb_endpoint_num(ep);
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700957 info.maxpacket = usb_endpoint_maxp(ep);
Julia Lawall2e0fe702008-12-29 11:22:14 +0100958 info.type = get_r8a66597_type(usb_endpoint_type(ep));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900959 info.bufnum = get_bufnum(info.pipenum);
960 info.buf_bsize = get_buf_bsize(info.pipenum);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900961 if (info.type == R8A66597_BULK) {
962 info.interval = 0;
963 info.timer_interval = 0;
964 } else {
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900965 info.interval = get_interval(urb, ep->bInterval);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900966 info.timer_interval = get_timer_interval(urb, ep->bInterval);
967 }
Julia Lawall2e0fe702008-12-29 11:22:14 +0100968 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900969 info.dir_in = 1;
970 else
971 info.dir_in = 0;
972
973 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
974}
975
976static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
977{
978 struct r8a66597_device *dev;
979
980 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
981 dev->state = USB_STATE_CONFIGURED;
982}
983
984static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
985 u16 pipenum)
986{
987 if (pipenum == 0 && usb_pipeout(urb->pipe))
988 enable_irq_empty(r8a66597, pipenum);
989 else
990 enable_irq_ready(r8a66597, pipenum);
991
992 if (!usb_pipeisoc(urb->pipe))
993 enable_irq_nrdy(r8a66597, pipenum);
994}
995
996static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
997{
998 disable_irq_ready(r8a66597, pipenum);
999 disable_irq_nrdy(r8a66597, pipenum);
1000}
1001
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001002static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
1003{
1004 mod_timer(&r8a66597->rh_timer,
1005 jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
1006}
1007
1008static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
1009 int connect)
1010{
1011 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1012
1013 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1014 rh->scount = R8A66597_MAX_SAMPLING;
1015 if (connect)
Alan Stern749da5f2010-03-04 17:05:08 -05001016 rh->port |= USB_PORT_STAT_CONNECTION;
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001017 else
Alan Stern749da5f2010-03-04 17:05:08 -05001018 rh->port &= ~USB_PORT_STAT_CONNECTION;
1019 rh->port |= USB_PORT_STAT_C_CONNECTION << 16;
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001020
1021 r8a66597_root_hub_start_polling(r8a66597);
1022}
1023
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001024/* this function must be called with interrupt disabled */
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001025static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
1026 u16 syssts)
Paul Mundt2c940db2010-02-04 06:58:28 +00001027__releases(r8a66597->lock)
1028__acquires(r8a66597->lock)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001029{
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001030 if (syssts == SE0) {
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001031 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001032 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
Yoshihiro Shimoda1e6159f2009-10-21 20:33:39 +09001033 } else {
1034 if (syssts == FS_JSTS)
1035 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
1036 else if (syssts == LS_JSTS)
1037 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
1038
1039 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
1040 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
1041
1042 if (r8a66597->bus_suspended)
1043 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001044 }
1045
Paul Mundt2c940db2010-02-04 06:58:28 +00001046 spin_unlock(&r8a66597->lock);
Yoshihiro Shimoda1e6159f2009-10-21 20:33:39 +09001047 usb_hcd_poll_rh_status(r8a66597_to_hcd(r8a66597));
Paul Mundt2c940db2010-02-04 06:58:28 +00001048 spin_lock(&r8a66597->lock);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001049}
1050
1051/* this function must be called with interrupt disabled */
1052static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
1053{
1054 u16 speed = get_rh_usb_speed(r8a66597, port);
1055 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1056
Alan Stern288ead42010-03-04 11:32:30 -05001057 rh->port &= ~(USB_PORT_STAT_HIGH_SPEED | USB_PORT_STAT_LOW_SPEED);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001058 if (speed == HSMODE)
Alan Stern288ead42010-03-04 11:32:30 -05001059 rh->port |= USB_PORT_STAT_HIGH_SPEED;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001060 else if (speed == LSMODE)
Alan Stern288ead42010-03-04 11:32:30 -05001061 rh->port |= USB_PORT_STAT_LOW_SPEED;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001062
Nobuhiro Iwamatsua5797a62010-06-07 16:55:56 +09001063 rh->port &= ~USB_PORT_STAT_RESET;
Alan Stern749da5f2010-03-04 17:05:08 -05001064 rh->port |= USB_PORT_STAT_ENABLE;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001065}
1066
1067/* this function must be called with interrupt disabled */
1068static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
1069{
1070 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
1071
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001072 disable_r8a66597_pipe_all(r8a66597, dev);
Yoshihiro Shimodad8359332010-03-16 12:29:35 +09001073 free_usb_address(r8a66597, dev, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001074
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001075 start_root_hub_sampling(r8a66597, port, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001076}
1077
1078/* this function must be called with interrupt disabled */
1079static void prepare_setup_packet(struct r8a66597 *r8a66597,
1080 struct r8a66597_td *td)
1081{
1082 int i;
Al Virofd05e722008-04-28 07:00:16 +01001083 __le16 *p = (__le16 *)td->urb->setup_packet;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001084 unsigned long setup_addr = USBREQ;
1085
1086 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1087 DCPMAXP);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001088 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001089
1090 for (i = 0; i < 4; i++) {
Al Virofd05e722008-04-28 07:00:16 +01001091 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001092 setup_addr += 2;
1093 }
1094 r8a66597_write(r8a66597, SUREQ, DCPCTR);
1095}
1096
1097/* this function must be called with interrupt disabled */
1098static void prepare_packet_read(struct r8a66597 *r8a66597,
1099 struct r8a66597_td *td)
1100{
1101 struct urb *urb = td->urb;
1102
1103 if (usb_pipecontrol(urb->pipe)) {
1104 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1105 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1106 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1107 if (urb->actual_length == 0) {
1108 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1109 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1110 }
1111 pipe_irq_disable(r8a66597, td->pipenum);
1112 pipe_start(r8a66597, td->pipe);
1113 pipe_irq_enable(r8a66597, urb, td->pipenum);
1114 } else {
1115 if (urb->actual_length == 0) {
1116 pipe_irq_disable(r8a66597, td->pipenum);
1117 pipe_setting(r8a66597, td);
1118 pipe_stop(r8a66597, td->pipe);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001119 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001120
1121 if (td->pipe->pipetre) {
1122 r8a66597_write(r8a66597, TRCLR,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001123 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001124 r8a66597_write(r8a66597,
Julia Lawalldfa5ec72008-03-04 15:25:11 -08001125 DIV_ROUND_UP
1126 (urb->transfer_buffer_length,
1127 td->maxpacket),
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001128 td->pipe->pipetrn);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001129 r8a66597_bset(r8a66597, TRENB,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001130 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001131 }
1132
1133 pipe_start(r8a66597, td->pipe);
1134 pipe_irq_enable(r8a66597, urb, td->pipenum);
1135 }
1136 }
1137}
1138
1139/* this function must be called with interrupt disabled */
1140static void prepare_packet_write(struct r8a66597 *r8a66597,
1141 struct r8a66597_td *td)
1142{
1143 u16 tmp;
1144 struct urb *urb = td->urb;
1145
1146 if (usb_pipecontrol(urb->pipe)) {
1147 pipe_stop(r8a66597, td->pipe);
1148 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1149 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1150 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1151 if (urb->actual_length == 0) {
1152 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1153 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1154 }
1155 } else {
1156 if (urb->actual_length == 0)
1157 pipe_setting(r8a66597, td);
1158 if (td->pipe->pipetre)
1159 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1160 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001161 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001162
1163 fifo_change_from_pipe(r8a66597, td->pipe);
1164 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1165 if (unlikely((tmp & FRDY) == 0))
1166 pipe_irq_enable(r8a66597, urb, td->pipenum);
1167 else
1168 packet_write(r8a66597, td->pipenum);
1169 pipe_start(r8a66597, td->pipe);
1170}
1171
1172/* this function must be called with interrupt disabled */
1173static void prepare_status_packet(struct r8a66597 *r8a66597,
1174 struct r8a66597_td *td)
1175{
1176 struct urb *urb = td->urb;
1177
1178 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001179 pipe_stop(r8a66597, td->pipe);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001180
1181 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1182 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1183 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1184 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001185 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +09001186 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001187 enable_irq_empty(r8a66597, 0);
1188 } else {
1189 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1190 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1191 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1192 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001193 enable_irq_ready(r8a66597, 0);
1194 }
1195 enable_irq_nrdy(r8a66597, 0);
1196 pipe_start(r8a66597, td->pipe);
1197}
1198
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001199static int is_set_address(unsigned char *setup_packet)
1200{
1201 if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1202 setup_packet[1] == USB_REQ_SET_ADDRESS)
1203 return 1;
1204 else
1205 return 0;
1206}
1207
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001208/* this function must be called with interrupt disabled */
1209static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1210{
1211 BUG_ON(!td);
1212
1213 switch (td->type) {
1214 case USB_PID_SETUP:
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001215 if (is_set_address(td->urb->setup_packet)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001216 td->set_address = 1;
1217 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1218 td->urb);
1219 if (td->urb->setup_packet[2] == 0)
1220 return -EPIPE;
1221 }
1222 prepare_setup_packet(r8a66597, td);
1223 break;
1224 case USB_PID_IN:
1225 prepare_packet_read(r8a66597, td);
1226 break;
1227 case USB_PID_OUT:
1228 prepare_packet_write(r8a66597, td);
1229 break;
1230 case USB_PID_ACK:
1231 prepare_status_packet(r8a66597, td);
1232 break;
1233 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001234 printk(KERN_ERR "r8a66597: invalid type.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001235 break;
1236 }
1237
1238 return 0;
1239}
1240
1241static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1242{
1243 if (usb_pipeisoc(urb->pipe)) {
1244 if (urb->number_of_packets == td->iso_cnt)
1245 return 1;
1246 }
1247
1248 /* control or bulk or interrupt */
1249 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1250 (td->short_packet) || (td->zero_packet))
1251 return 1;
1252
1253 return 0;
1254}
1255
1256/* this function must be called with interrupt disabled */
1257static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1258{
1259 unsigned long time;
1260
1261 BUG_ON(!td);
1262
1263 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1264 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1265 r8a66597->timeout_map |= 1 << td->pipenum;
1266 switch (usb_pipetype(td->urb->pipe)) {
1267 case PIPE_INTERRUPT:
1268 case PIPE_ISOCHRONOUS:
1269 time = 30;
1270 break;
1271 default:
Chris Brandt07612c12017-04-27 12:12:49 -07001272 time = 50;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001273 break;
1274 }
1275
1276 mod_timer(&r8a66597->td_timer[td->pipenum],
1277 jiffies + msecs_to_jiffies(time));
1278 }
1279}
1280
1281/* this function must be called with interrupt disabled */
Alan Sterndfd1e532007-08-21 15:36:52 -04001282static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
Alan Stern888fda42007-08-24 15:41:18 -04001283 u16 pipenum, struct urb *urb, int status)
Alan Sterndfd1e532007-08-21 15:36:52 -04001284__releases(r8a66597->lock) __acquires(r8a66597->lock)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001285{
1286 int restart = 0;
1287 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1288
1289 r8a66597->timeout_map &= ~(1 << pipenum);
1290
1291 if (likely(td)) {
Alan Stern888fda42007-08-24 15:41:18 -04001292 if (td->set_address && (status != 0 || urb->unlinked))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001293 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1294
1295 pipe_toggle_save(r8a66597, td->pipe, urb);
1296 list_del(&td->queue);
1297 kfree(td);
1298 }
1299
1300 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1301 restart = 1;
1302
1303 if (likely(urb)) {
1304 if (usb_pipeisoc(urb->pipe))
1305 urb->start_frame = r8a66597_get_frame(hcd);
1306
Paul Mundt27175682010-02-04 06:57:58 +00001307 r8a66597_urb_done(r8a66597, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001308 }
1309
1310 if (restart) {
1311 td = r8a66597_get_td(r8a66597, pipenum);
1312 if (unlikely(!td))
1313 return;
1314
1315 start_transfer(r8a66597, td);
1316 set_td_timer(r8a66597, td);
1317 }
1318}
1319
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001320static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1321{
1322 u16 tmp;
1323 int rcv_len, bufsize, urb_len, size;
1324 u16 *buf;
1325 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1326 struct urb *urb;
1327 int finish = 0;
Alan Sterndfd1e532007-08-21 15:36:52 -04001328 int status = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001329
1330 if (unlikely(!td))
1331 return;
1332 urb = td->urb;
1333
1334 fifo_change_from_pipe(r8a66597, td->pipe);
1335 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1336 if (unlikely((tmp & FRDY) == 0)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001337 pipe_stop(r8a66597, td->pipe);
1338 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001339 printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001340 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001341 return;
1342 }
1343
1344 /* prepare parameters */
1345 rcv_len = tmp & DTLN;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001346 if (usb_pipeisoc(urb->pipe)) {
1347 buf = (u16 *)(urb->transfer_buffer +
1348 urb->iso_frame_desc[td->iso_cnt].offset);
1349 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1350 } else {
1351 buf = (void *)urb->transfer_buffer + urb->actual_length;
1352 urb_len = urb->transfer_buffer_length - urb->actual_length;
1353 }
Alan Sterndfd1e532007-08-21 15:36:52 -04001354 bufsize = min(urb_len, (int) td->maxpacket);
1355 if (rcv_len <= bufsize) {
1356 size = rcv_len;
1357 } else {
1358 size = bufsize;
1359 status = -EOVERFLOW;
1360 finish = 1;
1361 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001362
1363 /* update parameters */
1364 urb->actual_length += size;
1365 if (rcv_len == 0)
1366 td->zero_packet = 1;
Alan Sterndfd1e532007-08-21 15:36:52 -04001367 if (rcv_len < bufsize) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001368 td->short_packet = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001369 }
1370 if (usb_pipeisoc(urb->pipe)) {
1371 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
Alan Sterndfd1e532007-08-21 15:36:52 -04001372 urb->iso_frame_desc[td->iso_cnt].status = status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001373 td->iso_cnt++;
Alan Sterndfd1e532007-08-21 15:36:52 -04001374 finish = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001375 }
1376
1377 /* check transfer finish */
Alan Sternb0d9efb2007-08-21 15:39:21 -04001378 if (finish || check_transfer_finish(td, urb)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001379 pipe_stop(r8a66597, td->pipe);
1380 pipe_irq_disable(r8a66597, pipenum);
1381 finish = 1;
1382 }
1383
1384 /* read fifo */
1385 if (urb->transfer_buffer) {
1386 if (size == 0)
1387 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1388 else
1389 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1390 buf, size);
1391 }
1392
Alan Stern888fda42007-08-24 15:41:18 -04001393 if (finish && pipenum != 0)
1394 finish_request(r8a66597, td, pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001395}
1396
1397static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1398{
1399 u16 tmp;
1400 int bufsize, size;
1401 u16 *buf;
1402 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1403 struct urb *urb;
1404
1405 if (unlikely(!td))
1406 return;
1407 urb = td->urb;
1408
1409 fifo_change_from_pipe(r8a66597, td->pipe);
1410 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1411 if (unlikely((tmp & FRDY) == 0)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001412 pipe_stop(r8a66597, td->pipe);
1413 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001414 printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001415 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001416 return;
1417 }
1418
1419 /* prepare parameters */
1420 bufsize = td->maxpacket;
1421 if (usb_pipeisoc(urb->pipe)) {
1422 buf = (u16 *)(urb->transfer_buffer +
1423 urb->iso_frame_desc[td->iso_cnt].offset);
1424 size = min(bufsize,
1425 (int)urb->iso_frame_desc[td->iso_cnt].length);
1426 } else {
1427 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
Greg Kroah-Hartman16e2e5f2009-03-03 16:44:13 -08001428 size = min_t(u32, bufsize,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001429 urb->transfer_buffer_length - urb->actual_length);
1430 }
1431
1432 /* write fifo */
1433 if (pipenum > 0)
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001434 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001435 if (urb->transfer_buffer) {
Yoshihiro Shimodaf2e90392011-07-07 09:57:10 +09001436 r8a66597_write_fifo(r8a66597, td->pipe, buf, size);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001437 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1438 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1439 }
1440
1441 /* update parameters */
1442 urb->actual_length += size;
1443 if (usb_pipeisoc(urb->pipe)) {
1444 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1445 urb->iso_frame_desc[td->iso_cnt].status = 0;
1446 td->iso_cnt++;
1447 }
1448
1449 /* check transfer finish */
1450 if (check_transfer_finish(td, urb)) {
1451 disable_irq_ready(r8a66597, pipenum);
1452 enable_irq_empty(r8a66597, pipenum);
1453 if (!usb_pipeisoc(urb->pipe))
1454 enable_irq_nrdy(r8a66597, pipenum);
1455 } else
1456 pipe_irq_enable(r8a66597, urb, pipenum);
1457}
1458
1459
Alan Stern888fda42007-08-24 15:41:18 -04001460static void check_next_phase(struct r8a66597 *r8a66597, int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001461{
1462 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1463 struct urb *urb;
1464 u8 finish = 0;
1465
1466 if (unlikely(!td))
1467 return;
1468 urb = td->urb;
1469
1470 switch (td->type) {
1471 case USB_PID_IN:
1472 case USB_PID_OUT:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001473 if (check_transfer_finish(td, urb))
1474 td->type = USB_PID_ACK;
1475 break;
1476 case USB_PID_SETUP:
Alan Sterneb231052007-08-21 15:40:36 -04001477 if (urb->transfer_buffer_length == urb->actual_length)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001478 td->type = USB_PID_ACK;
Alan Sterneb231052007-08-21 15:40:36 -04001479 else if (usb_pipeout(urb->pipe))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001480 td->type = USB_PID_OUT;
1481 else
1482 td->type = USB_PID_IN;
1483 break;
1484 case USB_PID_ACK:
1485 finish = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001486 break;
1487 }
1488
Alan Stern888fda42007-08-24 15:41:18 -04001489 if (finish || status != 0 || urb->unlinked)
1490 finish_request(r8a66597, td, 0, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001491 else
1492 start_transfer(r8a66597, td);
1493}
1494
Alan Stern888fda42007-08-24 15:41:18 -04001495static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001496{
1497 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1498
Alan Stern888fda42007-08-24 15:41:18 -04001499 if (td) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001500 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1501
1502 if (pid == PID_NAK)
Alan Stern888fda42007-08-24 15:41:18 -04001503 return -ECONNRESET;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001504 else
Alan Stern888fda42007-08-24 15:41:18 -04001505 return -EPIPE;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001506 }
Alan Stern888fda42007-08-24 15:41:18 -04001507 return 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001508}
1509
1510static void irq_pipe_ready(struct r8a66597 *r8a66597)
1511{
1512 u16 check;
1513 u16 pipenum;
1514 u16 mask;
1515 struct r8a66597_td *td;
1516
1517 mask = r8a66597_read(r8a66597, BRDYSTS)
1518 & r8a66597_read(r8a66597, BRDYENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001519 r8a66597_write(r8a66597, ~mask, BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001520 if (mask & BRDY0) {
1521 td = r8a66597_get_td(r8a66597, 0);
1522 if (td && td->type == USB_PID_IN)
1523 packet_read(r8a66597, 0);
1524 else
1525 pipe_irq_disable(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001526 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001527 }
1528
1529 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1530 check = 1 << pipenum;
1531 if (mask & check) {
1532 td = r8a66597_get_td(r8a66597, pipenum);
1533 if (unlikely(!td))
1534 continue;
1535
1536 if (td->type == USB_PID_IN)
1537 packet_read(r8a66597, pipenum);
1538 else if (td->type == USB_PID_OUT)
1539 packet_write(r8a66597, pipenum);
1540 }
1541 }
1542}
1543
1544static void irq_pipe_empty(struct r8a66597 *r8a66597)
1545{
1546 u16 tmp;
1547 u16 check;
1548 u16 pipenum;
1549 u16 mask;
1550 struct r8a66597_td *td;
1551
1552 mask = r8a66597_read(r8a66597, BEMPSTS)
1553 & r8a66597_read(r8a66597, BEMPENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001554 r8a66597_write(r8a66597, ~mask, BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001555 if (mask & BEMP0) {
1556 cfifo_change(r8a66597, 0);
1557 td = r8a66597_get_td(r8a66597, 0);
1558 if (td && td->type != USB_PID_OUT)
1559 disable_irq_empty(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001560 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001561 }
1562
1563 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1564 check = 1 << pipenum;
1565 if (mask & check) {
1566 struct r8a66597_td *td;
1567 td = r8a66597_get_td(r8a66597, pipenum);
1568 if (unlikely(!td))
1569 continue;
1570
1571 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1572 if ((tmp & INBUFM) == 0) {
1573 disable_irq_empty(r8a66597, pipenum);
1574 pipe_irq_disable(r8a66597, pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001575 finish_request(r8a66597, td, pipenum, td->urb,
1576 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001577 }
1578 }
1579 }
1580}
1581
1582static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1583{
1584 u16 check;
1585 u16 pipenum;
1586 u16 mask;
Alan Stern888fda42007-08-24 15:41:18 -04001587 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001588
1589 mask = r8a66597_read(r8a66597, NRDYSTS)
1590 & r8a66597_read(r8a66597, NRDYENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001591 r8a66597_write(r8a66597, ~mask, NRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001592 if (mask & NRDY0) {
1593 cfifo_change(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001594 status = get_urb_error(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001595 pipe_irq_disable(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001596 check_next_phase(r8a66597, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001597 }
1598
1599 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1600 check = 1 << pipenum;
1601 if (mask & check) {
1602 struct r8a66597_td *td;
1603 td = r8a66597_get_td(r8a66597, pipenum);
1604 if (unlikely(!td))
1605 continue;
1606
Alan Stern888fda42007-08-24 15:41:18 -04001607 status = get_urb_error(r8a66597, pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001608 pipe_irq_disable(r8a66597, pipenum);
1609 pipe_stop(r8a66597, td->pipe);
Alan Stern888fda42007-08-24 15:41:18 -04001610 finish_request(r8a66597, td, pipenum, td->urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001611 }
1612 }
1613}
1614
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001615static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1616{
1617 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1618 u16 intsts0, intsts1, intsts2;
1619 u16 intenb0, intenb1, intenb2;
1620 u16 mask0, mask1, mask2;
Alan Stern888fda42007-08-24 15:41:18 -04001621 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001622
1623 spin_lock(&r8a66597->lock);
1624
1625 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1626 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1627 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1628 intenb0 = r8a66597_read(r8a66597, INTENB0);
1629 intenb1 = r8a66597_read(r8a66597, INTENB1);
1630 intenb2 = r8a66597_read(r8a66597, INTENB2);
1631
1632 mask2 = intsts2 & intenb2;
1633 mask1 = intsts1 & intenb1;
1634 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1635 if (mask2) {
1636 if (mask2 & ATTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001637 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001638 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1639
1640 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001641 start_root_hub_sampling(r8a66597, 1, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001642 }
1643 if (mask2 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001644 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001645 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1646 r8a66597_usb_disconnect(r8a66597, 1);
1647 }
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09001648 if (mask2 & BCHG) {
1649 r8a66597_write(r8a66597, ~BCHG, INTSTS2);
1650 r8a66597_bclr(r8a66597, BCHGE, INTENB2);
1651 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1652 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001653 }
1654
1655 if (mask1) {
1656 if (mask1 & ATTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001657 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001658 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1659
1660 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001661 start_root_hub_sampling(r8a66597, 0, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001662 }
1663 if (mask1 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001664 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001665 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1666 r8a66597_usb_disconnect(r8a66597, 0);
1667 }
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09001668 if (mask1 & BCHG) {
1669 r8a66597_write(r8a66597, ~BCHG, INTSTS1);
1670 r8a66597_bclr(r8a66597, BCHGE, INTENB1);
1671 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1672 }
1673
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001674 if (mask1 & SIGN) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001675 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001676 status = get_urb_error(r8a66597, 0);
1677 check_next_phase(r8a66597, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001678 }
1679 if (mask1 & SACK) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001680 r8a66597_write(r8a66597, ~SACK, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001681 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001682 }
1683 }
1684 if (mask0) {
1685 if (mask0 & BRDY)
1686 irq_pipe_ready(r8a66597);
1687 if (mask0 & BEMP)
1688 irq_pipe_empty(r8a66597);
1689 if (mask0 & NRDY)
1690 irq_pipe_nrdy(r8a66597);
1691 }
1692
1693 spin_unlock(&r8a66597->lock);
1694 return IRQ_HANDLED;
1695}
1696
1697/* this function must be called with interrupt disabled */
1698static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1699{
1700 u16 tmp;
1701 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1702
Alan Stern749da5f2010-03-04 17:05:08 -05001703 if (rh->port & USB_PORT_STAT_RESET) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001704 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1705
1706 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1707 if ((tmp & USBRST) == USBRST) {
1708 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1709 dvstctr_reg);
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001710 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001711 } else
1712 r8a66597_usb_connect(r8a66597, port);
1713 }
1714
Alan Stern749da5f2010-03-04 17:05:08 -05001715 if (!(rh->port & USB_PORT_STAT_CONNECTION)) {
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001716 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1717 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1718 }
1719
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001720 if (rh->scount > 0) {
1721 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1722 if (tmp == rh->old_syssts) {
1723 rh->scount--;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001724 if (rh->scount == 0)
1725 r8a66597_check_syssts(r8a66597, port, tmp);
1726 else
1727 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001728 } else {
1729 rh->scount = R8A66597_MAX_SAMPLING;
1730 rh->old_syssts = tmp;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001731 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001732 }
1733 }
1734}
1735
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001736static void r8a66597_interval_timer(unsigned long _r8a66597)
1737{
1738 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1739 unsigned long flags;
1740 u16 pipenum;
1741 struct r8a66597_td *td;
1742
1743 spin_lock_irqsave(&r8a66597->lock, flags);
1744
1745 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1746 if (!(r8a66597->interval_map & (1 << pipenum)))
1747 continue;
1748 if (timer_pending(&r8a66597->interval_timer[pipenum]))
1749 continue;
1750
1751 td = r8a66597_get_td(r8a66597, pipenum);
1752 if (td)
1753 start_transfer(r8a66597, td);
1754 }
1755
1756 spin_unlock_irqrestore(&r8a66597->lock, flags);
1757}
1758
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001759static void r8a66597_td_timer(unsigned long _r8a66597)
1760{
1761 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1762 unsigned long flags;
1763 u16 pipenum;
1764 struct r8a66597_td *td, *new_td = NULL;
1765 struct r8a66597_pipe *pipe;
1766
1767 spin_lock_irqsave(&r8a66597->lock, flags);
1768 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1769 if (!(r8a66597->timeout_map & (1 << pipenum)))
1770 continue;
1771 if (timer_pending(&r8a66597->td_timer[pipenum]))
1772 continue;
1773
1774 td = r8a66597_get_td(r8a66597, pipenum);
1775 if (!td) {
1776 r8a66597->timeout_map &= ~(1 << pipenum);
1777 continue;
1778 }
1779
1780 if (td->urb->actual_length) {
1781 set_td_timer(r8a66597, td);
1782 break;
1783 }
1784
1785 pipe = td->pipe;
1786 pipe_stop(r8a66597, pipe);
1787
Chris Brandtf75f4d12017-04-27 12:12:02 -07001788 /* Select a different address or endpoint */
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001789 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 }
Chris Brandtf75f4d12017-04-27 12:12:02 -07001798 } while (td != new_td && td->address == new_td->address &&
1799 td->pipe->info.epnum == new_td->pipe->info.epnum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001800
1801 start_transfer(r8a66597, new_td);
1802
1803 if (td == new_td)
1804 r8a66597->timeout_map &= ~(1 << pipenum);
1805 else
1806 set_td_timer(r8a66597, new_td);
1807 break;
1808 }
1809 spin_unlock_irqrestore(&r8a66597->lock, flags);
1810}
1811
1812static void r8a66597_timer(unsigned long _r8a66597)
1813{
1814 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1815 unsigned long flags;
Yoshihiro Shimoda58639642008-11-11 16:47:21 +09001816 int port;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001817
1818 spin_lock_irqsave(&r8a66597->lock, flags);
1819
Magnus Damm719a72b2009-07-17 14:59:55 +00001820 for (port = 0; port < r8a66597->max_root_hub; port++)
Yoshihiro Shimoda58639642008-11-11 16:47:21 +09001821 r8a66597_root_hub_control(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001822
1823 spin_unlock_irqrestore(&r8a66597->lock, flags);
1824}
1825
1826static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1827{
1828 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1829
1830 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1831 (urb->dev->state == USB_STATE_CONFIGURED))
1832 return 1;
1833 else
1834 return 0;
1835}
1836
1837static int r8a66597_start(struct usb_hcd *hcd)
1838{
1839 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001840
1841 hcd->state = HC_STATE_RUNNING;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001842 return enable_controller(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001843}
1844
1845static void r8a66597_stop(struct usb_hcd *hcd)
1846{
1847 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1848
1849 disable_controller(r8a66597);
1850}
1851
1852static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1853{
1854 unsigned int usb_address = usb_pipedevice(urb->pipe);
1855 u16 root_port, hub_port;
1856
1857 if (usb_address == 0) {
Magnus Damm719a72b2009-07-17 14:59:55 +00001858 get_port_number(r8a66597, urb->dev->devpath,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001859 &root_port, &hub_port);
1860 set_devadd_reg(r8a66597, 0,
1861 get_r8a66597_usb_speed(urb->dev->speed),
1862 get_parent_r8a66597_address(r8a66597, urb->dev),
1863 hub_port, root_port);
1864 }
1865}
1866
1867static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1868 struct urb *urb,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001869 struct usb_host_endpoint *hep)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001870{
1871 struct r8a66597_td *td;
1872 u16 pipenum;
1873
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001874 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001875 if (td == NULL)
1876 return NULL;
1877
1878 pipenum = r8a66597_get_pipenum(urb, hep);
1879 td->pipenum = pipenum;
1880 td->pipe = hep->hcpriv;
1881 td->urb = urb;
1882 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1883 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1884 !usb_pipein(urb->pipe));
1885 if (usb_pipecontrol(urb->pipe))
1886 td->type = USB_PID_SETUP;
1887 else if (usb_pipein(urb->pipe))
1888 td->type = USB_PID_IN;
1889 else
1890 td->type = USB_PID_OUT;
1891 INIT_LIST_HEAD(&td->queue);
1892
1893 return td;
1894}
1895
1896static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001897 struct urb *urb,
1898 gfp_t mem_flags)
1899{
Alan Sterne9df41c2007-08-08 11:48:02 -04001900 struct usb_host_endpoint *hep = urb->ep;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001901 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1902 struct r8a66597_td *td = NULL;
Alan Sterne9df41c2007-08-08 11:48:02 -04001903 int ret, request = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001904 unsigned long flags;
1905
1906 spin_lock_irqsave(&r8a66597->lock, flags);
1907 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1908 ret = -ENODEV;
Alan Sterne9df41c2007-08-08 11:48:02 -04001909 goto error_not_linked;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001910 }
1911
Alan Sterne9df41c2007-08-08 11:48:02 -04001912 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1913 if (ret)
1914 goto error_not_linked;
1915
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001916 if (!hep->hcpriv) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001917 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1918 GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001919 if (!hep->hcpriv) {
1920 ret = -ENOMEM;
1921 goto error;
1922 }
1923 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1924 if (usb_pipeendpoint(urb->pipe))
1925 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1926 }
1927
1928 if (unlikely(check_pipe_config(r8a66597, urb)))
1929 init_pipe_config(r8a66597, urb);
1930
1931 set_address_zero(r8a66597, urb);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001932 td = r8a66597_make_td(r8a66597, urb, hep);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001933 if (td == NULL) {
1934 ret = -ENOMEM;
1935 goto error;
1936 }
1937 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1938 request = 1;
1939 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001940 urb->hcpriv = td;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001941
1942 if (request) {
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001943 if (td->pipe->info.timer_interval) {
1944 r8a66597->interval_map |= 1 << td->pipenum;
1945 mod_timer(&r8a66597->interval_timer[td->pipenum],
1946 jiffies + msecs_to_jiffies(
1947 td->pipe->info.timer_interval));
1948 } else {
1949 ret = start_transfer(r8a66597, td);
1950 if (ret < 0) {
1951 list_del(&td->queue);
1952 kfree(td);
1953 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001954 }
1955 } else
1956 set_td_timer(r8a66597, td);
1957
1958error:
Alan Sterne9df41c2007-08-08 11:48:02 -04001959 if (ret)
1960 usb_hcd_unlink_urb_from_ep(hcd, urb);
1961error_not_linked:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001962 spin_unlock_irqrestore(&r8a66597->lock, flags);
1963 return ret;
1964}
1965
Alan Sterne9df41c2007-08-08 11:48:02 -04001966static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1967 int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001968{
1969 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1970 struct r8a66597_td *td;
1971 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001972 int rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001973
1974 spin_lock_irqsave(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001975 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1976 if (rc)
1977 goto done;
1978
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001979 if (urb->hcpriv) {
1980 td = urb->hcpriv;
1981 pipe_stop(r8a66597, td->pipe);
1982 pipe_irq_disable(r8a66597, td->pipenum);
1983 disable_irq_empty(r8a66597, td->pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001984 finish_request(r8a66597, td, td->pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001985 }
Alan Sterne9df41c2007-08-08 11:48:02 -04001986 done:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001987 spin_unlock_irqrestore(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001988 return rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001989}
1990
1991static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1992 struct usb_host_endpoint *hep)
1993{
1994 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1995 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1996 struct r8a66597_td *td;
1997 struct urb *urb = NULL;
1998 u16 pipenum;
1999 unsigned long flags;
2000
2001 if (pipe == NULL)
2002 return;
2003 pipenum = pipe->info.pipenum;
2004
2005 if (pipenum == 0) {
2006 kfree(hep->hcpriv);
2007 hep->hcpriv = NULL;
2008 return;
2009 }
2010
2011 spin_lock_irqsave(&r8a66597->lock, flags);
2012 pipe_stop(r8a66597, pipe);
2013 pipe_irq_disable(r8a66597, pipenum);
2014 disable_irq_empty(r8a66597, pipenum);
2015 td = r8a66597_get_td(r8a66597, pipenum);
2016 if (td)
2017 urb = td->urb;
Alan Stern888fda42007-08-24 15:41:18 -04002018 finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002019 kfree(hep->hcpriv);
2020 hep->hcpriv = NULL;
2021 spin_unlock_irqrestore(&r8a66597->lock, flags);
2022}
2023
2024static int r8a66597_get_frame(struct usb_hcd *hcd)
2025{
2026 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2027 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
2028}
2029
2030static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
2031{
2032 int chix;
Lan Tianyuff823c72012-09-05 13:44:32 +08002033 struct usb_device *childdev;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002034
2035 if (udev->state == USB_STATE_CONFIGURED &&
2036 udev->parent && udev->parent->devnum > 1 &&
2037 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
2038 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
2039
Alan Stern969ddcf2012-10-19 11:03:02 -04002040 usb_hub_for_each_child(udev, chix, childdev)
2041 collect_usb_address_map(childdev, map);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002042}
2043
2044/* this function must be called with interrupt disabled */
2045static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
2046 int addr)
2047{
2048 struct r8a66597_device *dev;
2049 struct list_head *list = &r8a66597->child_device;
2050
2051 list_for_each_entry(dev, list, device_list) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002052 if (dev->usb_address != addr)
2053 continue;
2054
2055 return dev;
2056 }
2057
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002058 printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002059 return NULL;
2060}
2061
2062static void update_usb_address_map(struct r8a66597 *r8a66597,
2063 struct usb_device *root_hub,
2064 unsigned long *map)
2065{
2066 int i, j, addr;
2067 unsigned long diff;
2068 unsigned long flags;
2069
2070 for (i = 0; i < 4; i++) {
2071 diff = r8a66597->child_connect_map[i] ^ map[i];
2072 if (!diff)
2073 continue;
2074
2075 for (j = 0; j < 32; j++) {
2076 if (!(diff & (1 << j)))
2077 continue;
2078
2079 addr = i * 32 + j;
2080 if (map[i] & (1 << j))
2081 set_child_connect_map(r8a66597, addr);
2082 else {
2083 struct r8a66597_device *dev;
2084
2085 spin_lock_irqsave(&r8a66597->lock, flags);
2086 dev = get_r8a66597_device(r8a66597, addr);
2087 disable_r8a66597_pipe_all(r8a66597, dev);
Yoshihiro Shimodad8359332010-03-16 12:29:35 +09002088 free_usb_address(r8a66597, dev, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002089 put_child_connect_map(r8a66597, addr);
2090 spin_unlock_irqrestore(&r8a66597->lock, flags);
2091 }
2092 }
2093 }
2094}
2095
2096static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
2097 struct usb_hcd *hcd)
2098{
2099 struct usb_bus *bus;
2100 unsigned long now_map[4];
2101
2102 memset(now_map, 0, sizeof(now_map));
2103
Heiner Kallweitb1249ab2016-02-03 23:36:33 +01002104 mutex_lock(&usb_bus_idr_lock);
Heiner Kallweit5363de72016-01-25 20:30:30 +01002105 bus = idr_find(&usb_bus_idr, hcd->self.busnum);
2106 if (bus && bus->root_hub) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002107 collect_usb_address_map(bus->root_hub, now_map);
2108 update_usb_address_map(r8a66597, bus->root_hub, now_map);
2109 }
Heiner Kallweitb1249ab2016-02-03 23:36:33 +01002110 mutex_unlock(&usb_bus_idr_lock);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002111}
2112
2113static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2114{
2115 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2116 unsigned long flags;
2117 int i;
2118
2119 r8a66597_check_detect_child(r8a66597, hcd);
2120
2121 spin_lock_irqsave(&r8a66597->lock, flags);
2122
2123 *buf = 0; /* initialize (no change) */
2124
Magnus Damm719a72b2009-07-17 14:59:55 +00002125 for (i = 0; i < r8a66597->max_root_hub; i++) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002126 if (r8a66597->root_hub[i].port & 0xffff0000)
2127 *buf |= 1 << (i + 1);
2128 }
2129
2130 spin_unlock_irqrestore(&r8a66597->lock, flags);
2131
2132 return (*buf != 0);
2133}
2134
2135static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2136 struct usb_hub_descriptor *desc)
2137{
Sergei Shtylyov5e77bea2015-03-29 01:24:24 +03002138 desc->bDescriptorType = USB_DT_HUB;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002139 desc->bHubContrCurrent = 0;
Magnus Damm719a72b2009-07-17 14:59:55 +00002140 desc->bNbrPorts = r8a66597->max_root_hub;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002141 desc->bDescLength = 9;
2142 desc->bPwrOn2PwrGood = 0;
Sergei Shtylyove70480f2015-01-19 01:46:58 +03002143 desc->wHubCharacteristics =
2144 cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_NO_OCPM);
John Youndbe79bb2001-09-17 00:00:00 -07002145 desc->u.hs.DeviceRemovable[0] =
2146 ((1 << r8a66597->max_root_hub) - 1) << 1;
2147 desc->u.hs.DeviceRemovable[1] = ~0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002148}
2149
2150static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2151 u16 wIndex, char *buf, u16 wLength)
2152{
2153 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2154 int ret;
2155 int port = (wIndex & 0x00FF) - 1;
2156 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2157 unsigned long flags;
2158
2159 ret = 0;
2160
2161 spin_lock_irqsave(&r8a66597->lock, flags);
2162 switch (typeReq) {
2163 case ClearHubFeature:
2164 case SetHubFeature:
2165 switch (wValue) {
2166 case C_HUB_OVER_CURRENT:
2167 case C_HUB_LOCAL_POWER:
2168 break;
2169 default:
2170 goto error;
2171 }
2172 break;
2173 case ClearPortFeature:
Magnus Damm719a72b2009-07-17 14:59:55 +00002174 if (wIndex > r8a66597->max_root_hub)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002175 goto error;
2176 if (wLength != 0)
2177 goto error;
2178
2179 switch (wValue) {
2180 case USB_PORT_FEAT_ENABLE:
Alan Stern749da5f2010-03-04 17:05:08 -05002181 rh->port &= ~USB_PORT_STAT_POWER;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002182 break;
2183 case USB_PORT_FEAT_SUSPEND:
2184 break;
2185 case USB_PORT_FEAT_POWER:
2186 r8a66597_port_power(r8a66597, port, 0);
2187 break;
2188 case USB_PORT_FEAT_C_ENABLE:
2189 case USB_PORT_FEAT_C_SUSPEND:
2190 case USB_PORT_FEAT_C_CONNECTION:
2191 case USB_PORT_FEAT_C_OVER_CURRENT:
2192 case USB_PORT_FEAT_C_RESET:
2193 break;
2194 default:
2195 goto error;
2196 }
2197 rh->port &= ~(1 << wValue);
2198 break;
2199 case GetHubDescriptor:
2200 r8a66597_hub_descriptor(r8a66597,
2201 (struct usb_hub_descriptor *)buf);
2202 break;
2203 case GetHubStatus:
2204 *buf = 0x00;
2205 break;
2206 case GetPortStatus:
Magnus Damm719a72b2009-07-17 14:59:55 +00002207 if (wIndex > r8a66597->max_root_hub)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002208 goto error;
Al Virofd05e722008-04-28 07:00:16 +01002209 *(__le32 *)buf = cpu_to_le32(rh->port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002210 break;
2211 case SetPortFeature:
Magnus Damm719a72b2009-07-17 14:59:55 +00002212 if (wIndex > r8a66597->max_root_hub)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002213 goto error;
2214 if (wLength != 0)
2215 goto error;
2216
2217 switch (wValue) {
2218 case USB_PORT_FEAT_SUSPEND:
2219 break;
2220 case USB_PORT_FEAT_POWER:
2221 r8a66597_port_power(r8a66597, port, 1);
Alan Stern749da5f2010-03-04 17:05:08 -05002222 rh->port |= USB_PORT_STAT_POWER;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002223 break;
2224 case USB_PORT_FEAT_RESET: {
2225 struct r8a66597_device *dev = rh->dev;
2226
Alan Stern749da5f2010-03-04 17:05:08 -05002227 rh->port |= USB_PORT_STAT_RESET;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002228
2229 disable_r8a66597_pipe_all(r8a66597, dev);
Yoshihiro Shimodad8359332010-03-16 12:29:35 +09002230 free_usb_address(r8a66597, dev, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002231
2232 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2233 get_dvstctr_reg(port));
2234 mod_timer(&r8a66597->rh_timer,
2235 jiffies + msecs_to_jiffies(50));
2236 }
2237 break;
2238 default:
2239 goto error;
2240 }
2241 rh->port |= 1 << wValue;
2242 break;
2243 default:
2244error:
2245 ret = -EPIPE;
2246 break;
2247 }
2248
2249 spin_unlock_irqrestore(&r8a66597->lock, flags);
2250 return ret;
2251}
2252
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002253#if defined(CONFIG_PM)
2254static int r8a66597_bus_suspend(struct usb_hcd *hcd)
2255{
2256 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2257 int port;
2258
Greg Kroah-Hartman167e4922012-05-01 21:33:46 -07002259 dev_dbg(&r8a66597->device0.udev->dev, "%s\n", __func__);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002260
Magnus Damm719a72b2009-07-17 14:59:55 +00002261 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002262 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2263 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2264
Alan Stern749da5f2010-03-04 17:05:08 -05002265 if (!(rh->port & USB_PORT_STAT_ENABLE))
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002266 continue;
2267
Greg Kroah-Hartman167e4922012-05-01 21:33:46 -07002268 dev_dbg(&rh->dev->udev->dev, "suspend port = %d\n", port);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002269 r8a66597_bclr(r8a66597, UACT, dvstctr_reg); /* suspend */
Alan Stern749da5f2010-03-04 17:05:08 -05002270 rh->port |= USB_PORT_STAT_SUSPEND;
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002271
2272 if (rh->dev->udev->do_remote_wakeup) {
2273 msleep(3); /* waiting last SOF */
2274 r8a66597_bset(r8a66597, RWUPE, dvstctr_reg);
2275 r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
2276 r8a66597_bset(r8a66597, BCHGE, get_intenb_reg(port));
2277 }
2278 }
2279
2280 r8a66597->bus_suspended = 1;
2281
2282 return 0;
2283}
2284
2285static int r8a66597_bus_resume(struct usb_hcd *hcd)
2286{
2287 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2288 int port;
2289
Greg Kroah-Hartman167e4922012-05-01 21:33:46 -07002290 dev_dbg(&r8a66597->device0.udev->dev, "%s\n", __func__);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002291
Magnus Damm719a72b2009-07-17 14:59:55 +00002292 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002293 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2294 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2295
Alan Stern749da5f2010-03-04 17:05:08 -05002296 if (!(rh->port & USB_PORT_STAT_SUSPEND))
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002297 continue;
2298
Greg Kroah-Hartman167e4922012-05-01 21:33:46 -07002299 dev_dbg(&rh->dev->udev->dev, "resume port = %d\n", port);
Alan Stern749da5f2010-03-04 17:05:08 -05002300 rh->port &= ~USB_PORT_STAT_SUSPEND;
Kuninori Morimoto35da4132011-07-07 02:36:32 -07002301 rh->port |= USB_PORT_STAT_C_SUSPEND << 16;
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002302 r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
Felipe Balbi7a606ac2015-02-13 14:58:53 -06002303 msleep(USB_RESUME_TIMEOUT);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002304 r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
2305 }
2306
2307 return 0;
2308
2309}
2310#else
2311#define r8a66597_bus_suspend NULL
2312#define r8a66597_bus_resume NULL
2313#endif
2314
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002315static struct hc_driver r8a66597_hc_driver = {
2316 .description = hcd_name,
2317 .hcd_priv_size = sizeof(struct r8a66597),
2318 .irq = r8a66597_irq,
2319
2320 /*
2321 * generic hardware linkage
2322 */
2323 .flags = HCD_USB2,
2324
2325 .start = r8a66597_start,
2326 .stop = r8a66597_stop,
2327
2328 /*
2329 * managing i/o requests and associated device resources
2330 */
2331 .urb_enqueue = r8a66597_urb_enqueue,
2332 .urb_dequeue = r8a66597_urb_dequeue,
2333 .endpoint_disable = r8a66597_endpoint_disable,
2334
2335 /*
2336 * periodic schedule support
2337 */
2338 .get_frame_number = r8a66597_get_frame,
2339
2340 /*
2341 * root hub support
2342 */
2343 .hub_status_data = r8a66597_hub_status_data,
2344 .hub_control = r8a66597_hub_control,
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002345 .bus_suspend = r8a66597_bus_suspend,
2346 .bus_resume = r8a66597_bus_resume,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002347};
2348
2349#if defined(CONFIG_PM)
Magnus Dammae1cef62009-07-17 14:52:05 +00002350static int r8a66597_suspend(struct device *dev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002351{
Magnus Dammae1cef62009-07-17 14:52:05 +00002352 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002353 int port;
2354
Greg Kroah-Hartman167e4922012-05-01 21:33:46 -07002355 dev_dbg(dev, "%s\n", __func__);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002356
2357 disable_controller(r8a66597);
2358
Magnus Damm719a72b2009-07-17 14:59:55 +00002359 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002360 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2361
2362 rh->port = 0x00000000;
2363 }
2364
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002365 return 0;
2366}
2367
Magnus Dammae1cef62009-07-17 14:52:05 +00002368static int r8a66597_resume(struct device *dev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002369{
Magnus Dammae1cef62009-07-17 14:52:05 +00002370 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002371 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2372
Greg Kroah-Hartman167e4922012-05-01 21:33:46 -07002373 dev_dbg(dev, "%s\n", __func__);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002374
2375 enable_controller(r8a66597);
2376 usb_root_hub_lost_power(hcd->self.root_hub);
2377
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002378 return 0;
2379}
Magnus Dammae1cef62009-07-17 14:52:05 +00002380
Alexey Dobriyan47145212009-12-14 18:00:08 -08002381static const struct dev_pm_ops r8a66597_dev_pm_ops = {
Magnus Dammae1cef62009-07-17 14:52:05 +00002382 .suspend = r8a66597_suspend,
2383 .resume = r8a66597_resume,
Yoshihiro Shimoda3725f282009-07-29 09:24:41 +00002384 .poweroff = r8a66597_suspend,
2385 .restore = r8a66597_resume,
Magnus Dammae1cef62009-07-17 14:52:05 +00002386};
2387
2388#define R8A66597_DEV_PM_OPS (&r8a66597_dev_pm_ops)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002389#else /* if defined(CONFIG_PM) */
Magnus Dammae1cef62009-07-17 14:52:05 +00002390#define R8A66597_DEV_PM_OPS NULL
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002391#endif
2392
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05002393static int r8a66597_remove(struct platform_device *pdev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002394{
Libo Chen9b1b0162013-08-30 11:23:15 +08002395 struct r8a66597 *r8a66597 = platform_get_drvdata(pdev);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002396 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2397
2398 del_timer_sync(&r8a66597->rh_timer);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002399 usb_remove_hcd(hcd);
Paul Mundt1c983472010-06-02 16:27:54 +09002400 iounmap(r8a66597->reg);
Magnus Damm719a72b2009-07-17 14:59:55 +00002401 if (r8a66597->pdata->on_chip)
2402 clk_put(r8a66597->clk);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002403 usb_put_hcd(hcd);
2404 return 0;
2405}
2406
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002407static int r8a66597_probe(struct platform_device *pdev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002408{
Magnus Damm765786e2008-10-31 20:22:38 +09002409 char clk_name[8];
Marc Zyngier27140212008-08-18 13:08:42 +02002410 struct resource *res = NULL, *ires;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002411 int irq = -1;
2412 void __iomem *reg = NULL;
2413 struct usb_hcd *hcd = NULL;
2414 struct r8a66597 *r8a66597;
2415 int ret = 0;
2416 int i;
Yoshihiro Shimoda0bf32b82008-06-27 19:09:55 +09002417 unsigned long irq_trigger;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002418
Tobias Klauser427914802012-02-28 12:57:22 +01002419 if (usb_disabled())
2420 return -ENODEV;
2421
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002422 if (pdev->dev.dma_mask) {
2423 ret = -EINVAL;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002424 dev_err(&pdev->dev, "dma not supported\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002425 goto clean_up;
2426 }
2427
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002428 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002429 if (!res) {
2430 ret = -ENODEV;
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002431 dev_err(&pdev->dev, "platform_get_resource error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002432 goto clean_up;
2433 }
2434
Marc Zyngier27140212008-08-18 13:08:42 +02002435 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2436 if (!ires) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002437 ret = -ENODEV;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002438 dev_err(&pdev->dev,
2439 "platform_get_resource IORESOURCE_IRQ error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002440 goto clean_up;
2441 }
2442
Marc Zyngier27140212008-08-18 13:08:42 +02002443 irq = ires->start;
2444 irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
2445
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002446 reg = ioremap(res->start, resource_size(res));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002447 if (reg == NULL) {
2448 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002449 dev_err(&pdev->dev, "ioremap error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002450 goto clean_up;
2451 }
2452
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +09002453 if (pdev->dev.platform_data == NULL) {
2454 dev_err(&pdev->dev, "no platform data\n");
2455 ret = -ENODEV;
2456 goto clean_up;
2457 }
2458
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002459 /* initialize hcd */
2460 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2461 if (!hcd) {
2462 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002463 dev_err(&pdev->dev, "Failed to create hcd\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002464 goto clean_up;
2465 }
2466 r8a66597 = hcd_to_r8a66597(hcd);
2467 memset(r8a66597, 0, sizeof(struct r8a66597));
Libo Chen9b1b0162013-08-30 11:23:15 +08002468 platform_set_drvdata(pdev, r8a66597);
Jingoo Hand4f09e22013-07-30 19:59:40 +09002469 r8a66597->pdata = dev_get_platdata(&pdev->dev);
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +09002470 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002471
Magnus Damm719a72b2009-07-17 14:59:55 +00002472 if (r8a66597->pdata->on_chip) {
Magnus Damm719a72b2009-07-17 14:59:55 +00002473 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
2474 r8a66597->clk = clk_get(&pdev->dev, clk_name);
2475 if (IS_ERR(r8a66597->clk)) {
2476 dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
2477 clk_name);
2478 ret = PTR_ERR(r8a66597->clk);
2479 goto clean_up2;
2480 }
Magnus Damm719a72b2009-07-17 14:59:55 +00002481 r8a66597->max_root_hub = 1;
2482 } else
2483 r8a66597->max_root_hub = 2;
Magnus Damm765786e2008-10-31 20:22:38 +09002484
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002485 spin_lock_init(&r8a66597->lock);
Julia Lawall886f3102014-12-26 15:35:54 +01002486 setup_timer(&r8a66597->rh_timer, r8a66597_timer,
2487 (unsigned long)r8a66597);
Paul Mundt1c983472010-06-02 16:27:54 +09002488 r8a66597->reg = reg;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002489
Magnus Damme5ff15b2010-01-27 07:41:19 +00002490 /* make sure no interrupts are pending */
2491 ret = r8a66597_clock_enable(r8a66597);
2492 if (ret < 0)
2493 goto clean_up3;
2494 disable_controller(r8a66597);
2495
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002496 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2497 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
Julia Lawall886f3102014-12-26 15:35:54 +01002498 setup_timer(&r8a66597->td_timer[i], r8a66597_td_timer,
2499 (unsigned long)r8a66597);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09002500 setup_timer(&r8a66597->interval_timer[i],
2501 r8a66597_interval_timer,
2502 (unsigned long)r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002503 }
2504 INIT_LIST_HEAD(&r8a66597->child_device);
2505
2506 hcd->rsrc_start = res->start;
Yoshihiro Shimoda87abd0a2011-06-21 13:24:42 +09002507 hcd->has_tt = 1;
Marc Zyngier27140212008-08-18 13:08:42 +02002508
Yong Zhangb5dd18d2011-09-07 16:10:52 +08002509 ret = usb_add_hcd(hcd, irq, irq_trigger);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002510 if (ret != 0) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002511 dev_err(&pdev->dev, "Failed to add hcd\n");
Magnus Damm765786e2008-10-31 20:22:38 +09002512 goto clean_up3;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002513 }
Peter Chen3c9740a2013-11-05 10:46:02 +08002514 device_wakeup_enable(hcd->self.controller);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002515
2516 return 0;
2517
Magnus Damm765786e2008-10-31 20:22:38 +09002518clean_up3:
Magnus Damm719a72b2009-07-17 14:59:55 +00002519 if (r8a66597->pdata->on_chip)
2520 clk_put(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +09002521clean_up2:
2522 usb_put_hcd(hcd);
2523
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002524clean_up:
2525 if (reg)
2526 iounmap(reg);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002527
2528 return ret;
2529}
2530
2531static struct platform_driver r8a66597_driver = {
2532 .probe = r8a66597_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05002533 .remove = r8a66597_remove,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002534 .driver = {
Geert Uytterhoevena4586772013-11-12 20:07:25 +01002535 .name = hcd_name,
Magnus Dammae1cef62009-07-17 14:52:05 +00002536 .pm = R8A66597_DEV_PM_OPS,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002537 },
2538};
2539
Tobias Klauser427914802012-02-28 12:57:22 +01002540module_platform_driver(r8a66597_driver);