xhci: correct burst count field for isoc transfers on 1.0 xhci hosts
[pandora-kernel.git] / drivers / usb / host / ohci-at91.c
1 /*
2  * OHCI HCD (Host Controller Driver) for USB.
3  *
4  *  Copyright (C) 2004 SAN People (Pty) Ltd.
5  *  Copyright (C) 2005 Thibaut VARENE <varenet@parisc-linux.org>
6  *
7  * AT91 Bus Glue
8  *
9  * Based on fragments of 2.4 driver by Rick Bronson.
10  * Based on ohci-omap.c
11  *
12  * This file is licenced under the GPL.
13  */
14
15 #include <linux/clk.h>
16 #include <linux/platform_device.h>
17
18 #include <mach/hardware.h>
19 #include <asm/gpio.h>
20
21 #include <mach/board.h>
22 #include <mach/cpu.h>
23
24 #ifndef CONFIG_ARCH_AT91
25 #error "CONFIG_ARCH_AT91 must be defined."
26 #endif
27
28 /* interface and function clocks; sometimes also an AHB clock */
29 static struct clk *iclk, *fclk, *hclk;
30 static int clocked;
31
32 extern int usb_disabled(void);
33
34 /*-------------------------------------------------------------------------*/
35
36 static void at91_start_clock(void)
37 {
38         clk_enable(hclk);
39         clk_enable(iclk);
40         clk_enable(fclk);
41         clocked = 1;
42 }
43
44 static void at91_stop_clock(void)
45 {
46         clk_disable(fclk);
47         clk_disable(iclk);
48         clk_disable(hclk);
49         clocked = 0;
50 }
51
52 static void at91_start_hc(struct platform_device *pdev)
53 {
54         struct usb_hcd *hcd = platform_get_drvdata(pdev);
55         struct ohci_regs __iomem *regs = hcd->regs;
56
57         dev_dbg(&pdev->dev, "start\n");
58
59         /*
60          * Start the USB clocks.
61          */
62         at91_start_clock();
63
64         /*
65          * The USB host controller must remain in reset.
66          */
67         writel(0, &regs->control);
68 }
69
70 static void at91_stop_hc(struct platform_device *pdev)
71 {
72         struct usb_hcd *hcd = platform_get_drvdata(pdev);
73         struct ohci_regs __iomem *regs = hcd->regs;
74
75         dev_dbg(&pdev->dev, "stop\n");
76
77         /*
78          * Put the USB host controller into reset.
79          */
80         writel(0, &regs->control);
81
82         /*
83          * Stop the USB clocks.
84          */
85         at91_stop_clock();
86 }
87
88
89 /*-------------------------------------------------------------------------*/
90
91 static void usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
92
93 /* configure so an HC device and id are always provided */
94 /* always called with process context; sleeping is OK */
95
96
97 /**
98  * usb_hcd_at91_probe - initialize AT91-based HCDs
99  * Context: !in_interrupt()
100  *
101  * Allocates basic resources for this USB host controller, and
102  * then invokes the start() method for the HCD associated with it
103  * through the hotplug entry's driver_data.
104  */
105 static int usb_hcd_at91_probe(const struct hc_driver *driver,
106                         struct platform_device *pdev)
107 {
108         int retval;
109         struct usb_hcd *hcd = NULL;
110
111         if (pdev->num_resources != 2) {
112                 pr_debug("hcd probe: invalid num_resources");
113                 return -ENODEV;
114         }
115
116         if ((pdev->resource[0].flags != IORESOURCE_MEM)
117                         || (pdev->resource[1].flags != IORESOURCE_IRQ)) {
118                 pr_debug("hcd probe: invalid resource type\n");
119                 return -ENODEV;
120         }
121
122         hcd = usb_create_hcd(driver, &pdev->dev, "at91");
123         if (!hcd)
124                 return -ENOMEM;
125         hcd->rsrc_start = pdev->resource[0].start;
126         hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;
127
128         if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
129                 pr_debug("request_mem_region failed\n");
130                 retval = -EBUSY;
131                 goto err1;
132         }
133
134         hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
135         if (!hcd->regs) {
136                 pr_debug("ioremap failed\n");
137                 retval = -EIO;
138                 goto err2;
139         }
140
141         iclk = clk_get(&pdev->dev, "ohci_clk");
142         fclk = clk_get(&pdev->dev, "uhpck");
143         hclk = clk_get(&pdev->dev, "hclk");
144
145         at91_start_hc(pdev);
146         ohci_hcd_init(hcd_to_ohci(hcd));
147
148         retval = usb_add_hcd(hcd, pdev->resource[1].start, IRQF_SHARED);
149         if (retval == 0)
150                 return retval;
151
152         /* Error handling */
153         at91_stop_hc(pdev);
154
155         clk_put(hclk);
156         clk_put(fclk);
157         clk_put(iclk);
158
159         iounmap(hcd->regs);
160
161  err2:
162         release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
163
164  err1:
165         usb_put_hcd(hcd);
166         return retval;
167 }
168
169
170 /* may be called with controller, bus, and devices active */
171
172 /**
173  * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
174  * @dev: USB Host Controller being removed
175  * Context: !in_interrupt()
176  *
177  * Reverses the effect of usb_hcd_at91_probe(), first invoking
178  * the HCD's stop() method.  It is always called from a thread
179  * context, "rmmod" or something similar.
180  *
181  */
182 static void usb_hcd_at91_remove(struct usb_hcd *hcd,
183                                 struct platform_device *pdev)
184 {
185         usb_remove_hcd(hcd);
186         at91_stop_hc(pdev);
187         iounmap(hcd->regs);
188         release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
189         usb_put_hcd(hcd);
190
191         clk_put(hclk);
192         clk_put(fclk);
193         clk_put(iclk);
194         fclk = iclk = hclk = NULL;
195
196         dev_set_drvdata(&pdev->dev, NULL);
197 }
198
199 /*-------------------------------------------------------------------------*/
200
201 static int __devinit
202 ohci_at91_reset (struct usb_hcd *hcd)
203 {
204         struct at91_usbh_data   *board = hcd->self.controller->platform_data;
205         struct ohci_hcd         *ohci = hcd_to_ohci (hcd);
206         int                     ret;
207
208         if ((ret = ohci_init(ohci)) < 0)
209                 return ret;
210
211         ohci->num_ports = board->ports;
212         return 0;
213 }
214
215 static int __devinit
216 ohci_at91_start (struct usb_hcd *hcd)
217 {
218         struct ohci_hcd         *ohci = hcd_to_ohci (hcd);
219         int                     ret;
220
221         if ((ret = ohci_run(ohci)) < 0) {
222                 err("can't start %s", hcd->self.bus_name);
223                 ohci_stop(hcd);
224                 return ret;
225         }
226         return 0;
227 }
228
229 static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
230 {
231         if (port < 0 || port >= 2)
232                 return;
233
234         if (pdata->vbus_pin[port] <= 0)
235                 return;
236
237         gpio_set_value(pdata->vbus_pin[port], !pdata->vbus_pin_inverted ^ enable);
238 }
239
240 static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
241 {
242         if (port < 0 || port >= 2)
243                 return -EINVAL;
244
245         if (pdata->vbus_pin[port] <= 0)
246                 return -EINVAL;
247
248         return gpio_get_value(pdata->vbus_pin[port]) ^ !pdata->vbus_pin_inverted;
249 }
250
251 /*
252  * Update the status data from the hub with the over-current indicator change.
253  */
254 static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
255 {
256         struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
257         int length = ohci_hub_status_data(hcd, buf);
258         int port;
259
260         for (port = 0; port < ARRAY_SIZE(pdata->overcurrent_pin); port++) {
261                 if (pdata->overcurrent_changed[port]) {
262                         if (! length)
263                                 length = 1;
264                         buf[0] |= 1 << (port + 1);
265                 }
266         }
267
268         return length;
269 }
270
271 /*
272  * Look at the control requests to the root hub and see if we need to override.
273  */
274 static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
275                                  u16 wIndex, char *buf, u16 wLength)
276 {
277         struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
278         struct usb_hub_descriptor *desc;
279         int ret = -EINVAL;
280         u32 *data = (u32 *)buf;
281
282         dev_dbg(hcd->self.controller,
283                 "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
284                 hcd, typeReq, wValue, wIndex, buf, wLength);
285
286         switch (typeReq) {
287         case SetPortFeature:
288                 if (wValue == USB_PORT_FEAT_POWER) {
289                         dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
290                         ohci_at91_usb_set_power(pdata, wIndex - 1, 1);
291                         goto out;
292                 }
293                 break;
294
295         case ClearPortFeature:
296                 switch (wValue) {
297                 case USB_PORT_FEAT_C_OVER_CURRENT:
298                         dev_dbg(hcd->self.controller,
299                                 "ClearPortFeature: C_OVER_CURRENT\n");
300
301                         if (wIndex == 1 || wIndex == 2) {
302                                 pdata->overcurrent_changed[wIndex-1] = 0;
303                                 pdata->overcurrent_status[wIndex-1] = 0;
304                         }
305
306                         goto out;
307
308                 case USB_PORT_FEAT_OVER_CURRENT:
309                         dev_dbg(hcd->self.controller,
310                                 "ClearPortFeature: OVER_CURRENT\n");
311
312                         if (wIndex == 1 || wIndex == 2) {
313                                 pdata->overcurrent_status[wIndex-1] = 0;
314                         }
315
316                         goto out;
317
318                 case USB_PORT_FEAT_POWER:
319                         dev_dbg(hcd->self.controller,
320                                 "ClearPortFeature: POWER\n");
321
322                         if (wIndex == 1 || wIndex == 2) {
323                                 ohci_at91_usb_set_power(pdata, wIndex - 1, 0);
324                                 return 0;
325                         }
326                 }
327                 break;
328         }
329
330         ret = ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
331         if (ret)
332                 goto out;
333
334         switch (typeReq) {
335         case GetHubDescriptor:
336
337                 /* update the hub's descriptor */
338
339                 desc = (struct usb_hub_descriptor *)buf;
340
341                 dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
342                         desc->wHubCharacteristics);
343
344                 /* remove the old configurations for power-switching, and
345                  * over-current protection, and insert our new configuration
346                  */
347
348                 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
349                 desc->wHubCharacteristics |= cpu_to_le16(0x0001);
350
351                 if (pdata->overcurrent_supported) {
352                         desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
353                         desc->wHubCharacteristics |=  cpu_to_le16(0x0008|0x0001);
354                 }
355
356                 dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
357                         desc->wHubCharacteristics);
358
359                 return ret;
360
361         case GetPortStatus:
362                 /* check port status */
363
364                 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
365
366                 if (wIndex == 1 || wIndex == 2) {
367                         if (! ohci_at91_usb_get_power(pdata, wIndex-1)) {
368                                 *data &= ~cpu_to_le32(RH_PS_PPS);
369                         }
370
371                         if (pdata->overcurrent_changed[wIndex-1]) {
372                                 *data |= cpu_to_le32(RH_PS_OCIC);
373                         }
374
375                         if (pdata->overcurrent_status[wIndex-1]) {
376                                 *data |= cpu_to_le32(RH_PS_POCI);
377                         }
378                 }
379         }
380
381  out:
382         return ret;
383 }
384
385 /*-------------------------------------------------------------------------*/
386
387 static const struct hc_driver ohci_at91_hc_driver = {
388         .description =          hcd_name,
389         .product_desc =         "AT91 OHCI",
390         .hcd_priv_size =        sizeof(struct ohci_hcd),
391
392         /*
393          * generic hardware linkage
394          */
395         .irq =                  ohci_irq,
396         .flags =                HCD_USB11 | HCD_MEMORY,
397
398         /*
399          * basic lifecycle operations
400          */
401         .reset =                ohci_at91_reset,
402         .start =                ohci_at91_start,
403         .stop =                 ohci_stop,
404         .shutdown =             ohci_shutdown,
405
406         /*
407          * managing i/o requests and associated device resources
408          */
409         .urb_enqueue =          ohci_urb_enqueue,
410         .urb_dequeue =          ohci_urb_dequeue,
411         .endpoint_disable =     ohci_endpoint_disable,
412
413         /*
414          * scheduling support
415          */
416         .get_frame_number =     ohci_get_frame,
417
418         /*
419          * root hub support
420          */
421         .hub_status_data =      ohci_at91_hub_status_data,
422         .hub_control =          ohci_at91_hub_control,
423 #ifdef CONFIG_PM
424         .bus_suspend =          ohci_bus_suspend,
425         .bus_resume =           ohci_bus_resume,
426 #endif
427         .start_port_reset =     ohci_start_port_reset,
428 };
429
430 /*-------------------------------------------------------------------------*/
431
432 static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
433 {
434         struct platform_device *pdev = data;
435         struct at91_usbh_data *pdata = pdev->dev.platform_data;
436         int val, gpio, port;
437
438         /* From the GPIO notifying the over-current situation, find
439          * out the corresponding port */
440         gpio = irq_to_gpio(irq);
441         for (port = 0; port < ARRAY_SIZE(pdata->overcurrent_pin); port++) {
442                 if (pdata->overcurrent_pin[port] == gpio)
443                         break;
444         }
445
446         if (port == ARRAY_SIZE(pdata->overcurrent_pin)) {
447                 dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
448                 return IRQ_HANDLED;
449         }
450
451         val = gpio_get_value(gpio);
452
453         /* When notified of an over-current situation, disable power
454            on the corresponding port, and mark this port in
455            over-current. */
456         if (! val) {
457                 ohci_at91_usb_set_power(pdata, port, 0);
458                 pdata->overcurrent_status[port]  = 1;
459                 pdata->overcurrent_changed[port] = 1;
460         }
461
462         dev_dbg(& pdev->dev, "overcurrent situation %s\n",
463                 val ? "exited" : "notified");
464
465         return IRQ_HANDLED;
466 }
467
468 /*-------------------------------------------------------------------------*/
469
470 static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
471 {
472         struct at91_usbh_data   *pdata = pdev->dev.platform_data;
473         int                     i;
474
475         if (pdata) {
476                 for (i = 0; i < ARRAY_SIZE(pdata->vbus_pin); i++) {
477                         if (pdata->vbus_pin[i] <= 0)
478                                 continue;
479                         gpio_request(pdata->vbus_pin[i], "ohci_vbus");
480                         ohci_at91_usb_set_power(pdata, i, 1);
481                 }
482
483                 for (i = 0; i < ARRAY_SIZE(pdata->overcurrent_pin); i++) {
484                         int ret;
485
486                         if (pdata->overcurrent_pin[i] <= 0)
487                                 continue;
488                         gpio_request(pdata->overcurrent_pin[i], "ohci_overcurrent");
489
490                         ret = request_irq(gpio_to_irq(pdata->overcurrent_pin[i]),
491                                           ohci_hcd_at91_overcurrent_irq,
492                                           IRQF_SHARED, "ohci_overcurrent", pdev);
493                         if (ret) {
494                                 gpio_free(pdata->overcurrent_pin[i]);
495                                 dev_warn(& pdev->dev, "cannot get GPIO IRQ for overcurrent\n");
496                         }
497                 }
498         }
499
500         device_init_wakeup(&pdev->dev, 1);
501         return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
502 }
503
504 static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
505 {
506         struct at91_usbh_data   *pdata = pdev->dev.platform_data;
507         int                     i;
508
509         if (pdata) {
510                 for (i = 0; i < ARRAY_SIZE(pdata->vbus_pin); i++) {
511                         if (pdata->vbus_pin[i] <= 0)
512                                 continue;
513                         ohci_at91_usb_set_power(pdata, i, 0);
514                         gpio_free(pdata->vbus_pin[i]);
515                 }
516
517                 for (i = 0; i < ARRAY_SIZE(pdata->overcurrent_pin); i++) {
518                         if (pdata->overcurrent_pin[i] <= 0)
519                                 continue;
520                         free_irq(gpio_to_irq(pdata->overcurrent_pin[i]), pdev);
521                         gpio_free(pdata->overcurrent_pin[i]);
522                 }
523         }
524
525         device_init_wakeup(&pdev->dev, 0);
526         usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
527         return 0;
528 }
529
530 #ifdef CONFIG_PM
531
532 static int
533 ohci_hcd_at91_drv_suspend(struct platform_device *pdev, pm_message_t mesg)
534 {
535         struct usb_hcd  *hcd = platform_get_drvdata(pdev);
536         struct ohci_hcd *ohci = hcd_to_ohci(hcd);
537
538         if (device_may_wakeup(&pdev->dev))
539                 enable_irq_wake(hcd->irq);
540
541         /*
542          * The integrated transceivers seem unable to notice disconnect,
543          * reconnect, or wakeup without the 48 MHz clock active.  so for
544          * correctness, always discard connection state (using reset).
545          *
546          * REVISIT: some boards will be able to turn VBUS off...
547          */
548         if (at91_suspend_entering_slow_clock()) {
549                 ohci_usb_reset (ohci);
550                 /* flush the writes */
551                 (void) ohci_readl (ohci, &ohci->regs->control);
552                 at91_stop_clock();
553         }
554
555         return 0;
556 }
557
558 static int ohci_hcd_at91_drv_resume(struct platform_device *pdev)
559 {
560         struct usb_hcd  *hcd = platform_get_drvdata(pdev);
561
562         if (device_may_wakeup(&pdev->dev))
563                 disable_irq_wake(hcd->irq);
564
565         if (!clocked)
566                 at91_start_clock();
567
568         ohci_finish_controller_resume(hcd);
569         return 0;
570 }
571 #else
572 #define ohci_hcd_at91_drv_suspend NULL
573 #define ohci_hcd_at91_drv_resume  NULL
574 #endif
575
576 MODULE_ALIAS("platform:at91_ohci");
577
578 static struct platform_driver ohci_hcd_at91_driver = {
579         .probe          = ohci_hcd_at91_drv_probe,
580         .remove         = ohci_hcd_at91_drv_remove,
581         .shutdown       = usb_hcd_platform_shutdown,
582         .suspend        = ohci_hcd_at91_drv_suspend,
583         .resume         = ohci_hcd_at91_drv_resume,
584         .driver         = {
585                 .name   = "at91_ohci",
586                 .owner  = THIS_MODULE,
587         },
588 };