Merge tag 'driver-core-3.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.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/dma-mapping.h>
17 #include <linux/of_platform.h>
18 #include <linux/of_gpio.h>
19 #include <linux/platform_device.h>
20 #include <linux/platform_data/atmel.h>
21 #include <linux/io.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/usb.h>
25 #include <linux/usb/hcd.h>
26
27 #include <asm/gpio.h>
28
29 #include "ohci.h"
30
31 #define valid_port(index)       ((index) >= 0 && (index) < AT91_MAX_USBH_PORTS)
32 #define at91_for_each_port(index)       \
33                 for ((index) = 0; (index) < AT91_MAX_USBH_PORTS; (index)++)
34
35 /* interface, function and usb clocks; sometimes also an AHB clock */
36 static struct clk *iclk, *fclk, *uclk, *hclk;
37 /* interface and function clocks; sometimes also an AHB clock */
38
39 #define DRIVER_DESC "OHCI Atmel driver"
40
41 static const char hcd_name[] = "ohci-atmel";
42
43 static struct hc_driver __read_mostly ohci_at91_hc_driver;
44 static int clocked;
45
46 extern int usb_disabled(void);
47
48 /*-------------------------------------------------------------------------*/
49
50 static void at91_start_clock(void)
51 {
52         if (IS_ENABLED(CONFIG_COMMON_CLK)) {
53                 clk_set_rate(uclk, 48000000);
54                 clk_prepare_enable(uclk);
55         }
56         clk_prepare_enable(hclk);
57         clk_prepare_enable(iclk);
58         clk_prepare_enable(fclk);
59         clocked = 1;
60 }
61
62 static void at91_stop_clock(void)
63 {
64         clk_disable_unprepare(fclk);
65         clk_disable_unprepare(iclk);
66         clk_disable_unprepare(hclk);
67         if (IS_ENABLED(CONFIG_COMMON_CLK))
68                 clk_disable_unprepare(uclk);
69         clocked = 0;
70 }
71
72 static void at91_start_hc(struct platform_device *pdev)
73 {
74         struct usb_hcd *hcd = platform_get_drvdata(pdev);
75         struct ohci_regs __iomem *regs = hcd->regs;
76
77         dev_dbg(&pdev->dev, "start\n");
78
79         /*
80          * Start the USB clocks.
81          */
82         at91_start_clock();
83
84         /*
85          * The USB host controller must remain in reset.
86          */
87         writel(0, &regs->control);
88 }
89
90 static void at91_stop_hc(struct platform_device *pdev)
91 {
92         struct usb_hcd *hcd = platform_get_drvdata(pdev);
93         struct ohci_regs __iomem *regs = hcd->regs;
94
95         dev_dbg(&pdev->dev, "stop\n");
96
97         /*
98          * Put the USB host controller into reset.
99          */
100         writel(0, &regs->control);
101
102         /*
103          * Stop the USB clocks.
104          */
105         at91_stop_clock();
106 }
107
108
109 /*-------------------------------------------------------------------------*/
110
111 static void usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
112
113 /* configure so an HC device and id are always provided */
114 /* always called with process context; sleeping is OK */
115
116
117 /**
118  * usb_hcd_at91_probe - initialize AT91-based HCDs
119  * Context: !in_interrupt()
120  *
121  * Allocates basic resources for this USB host controller, and
122  * then invokes the start() method for the HCD associated with it
123  * through the hotplug entry's driver_data.
124  */
125 static int usb_hcd_at91_probe(const struct hc_driver *driver,
126                         struct platform_device *pdev)
127 {
128         struct at91_usbh_data *board;
129         struct ohci_hcd *ohci;
130         int retval;
131         struct usb_hcd *hcd = NULL;
132         struct device *dev = &pdev->dev;
133         struct resource *res;
134         int irq;
135
136         irq = platform_get_irq(pdev, 0);
137         if (irq < 0) {
138                 dev_dbg(dev, "hcd probe: missing irq resource\n");
139                 return irq;
140         }
141
142         hcd = usb_create_hcd(driver, dev, "at91");
143         if (!hcd)
144                 return -ENOMEM;
145
146         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
147         hcd->regs = devm_ioremap_resource(dev, res);
148         if (IS_ERR(hcd->regs)) {
149                 retval = PTR_ERR(hcd->regs);
150                 goto err;
151         }
152         hcd->rsrc_start = res->start;
153         hcd->rsrc_len = resource_size(res);
154
155         iclk = devm_clk_get(dev, "ohci_clk");
156         if (IS_ERR(iclk)) {
157                 dev_err(dev, "failed to get ohci_clk\n");
158                 retval = PTR_ERR(iclk);
159                 goto err;
160         }
161         fclk = devm_clk_get(dev, "uhpck");
162         if (IS_ERR(fclk)) {
163                 dev_err(dev, "failed to get uhpck\n");
164                 retval = PTR_ERR(fclk);
165                 goto err;
166         }
167         hclk = devm_clk_get(dev, "hclk");
168         if (IS_ERR(hclk)) {
169                 dev_err(dev, "failed to get hclk\n");
170                 retval = PTR_ERR(hclk);
171                 goto err;
172         }
173         if (IS_ENABLED(CONFIG_COMMON_CLK)) {
174                 uclk = devm_clk_get(dev, "usb_clk");
175                 if (IS_ERR(uclk)) {
176                         dev_err(dev, "failed to get uclk\n");
177                         retval = PTR_ERR(uclk);
178                         goto err;
179                 }
180         }
181
182         board = hcd->self.controller->platform_data;
183         ohci = hcd_to_ohci(hcd);
184         ohci->num_ports = board->ports;
185         at91_start_hc(pdev);
186
187         retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
188         if (retval == 0) {
189                 device_wakeup_enable(hcd->self.controller);
190                 return retval;
191         }
192
193         /* Error handling */
194         at91_stop_hc(pdev);
195
196  err:
197         usb_put_hcd(hcd);
198         return retval;
199 }
200
201
202 /* may be called with controller, bus, and devices active */
203
204 /**
205  * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
206  * @dev: USB Host Controller being removed
207  * Context: !in_interrupt()
208  *
209  * Reverses the effect of usb_hcd_at91_probe(), first invoking
210  * the HCD's stop() method.  It is always called from a thread
211  * context, "rmmod" or something similar.
212  *
213  */
214 static void usb_hcd_at91_remove(struct usb_hcd *hcd,
215                                 struct platform_device *pdev)
216 {
217         usb_remove_hcd(hcd);
218         at91_stop_hc(pdev);
219         usb_put_hcd(hcd);
220 }
221
222 /*-------------------------------------------------------------------------*/
223 static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
224 {
225         if (!valid_port(port))
226                 return;
227
228         if (!gpio_is_valid(pdata->vbus_pin[port]))
229                 return;
230
231         gpio_set_value(pdata->vbus_pin[port],
232                        pdata->vbus_pin_active_low[port] ^ enable);
233 }
234
235 static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
236 {
237         if (!valid_port(port))
238                 return -EINVAL;
239
240         if (!gpio_is_valid(pdata->vbus_pin[port]))
241                 return -EINVAL;
242
243         return gpio_get_value(pdata->vbus_pin[port]) ^
244                 pdata->vbus_pin_active_low[port];
245 }
246
247 /*
248  * Update the status data from the hub with the over-current indicator change.
249  */
250 static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
251 {
252         struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
253         int length = ohci_hub_status_data(hcd, buf);
254         int port;
255
256         at91_for_each_port(port) {
257                 if (pdata->overcurrent_changed[port]) {
258                         if (!length)
259                                 length = 1;
260                         buf[0] |= 1 << (port + 1);
261                 }
262         }
263
264         return length;
265 }
266
267 /*
268  * Look at the control requests to the root hub and see if we need to override.
269  */
270 static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
271                                  u16 wIndex, char *buf, u16 wLength)
272 {
273         struct at91_usbh_data *pdata = dev_get_platdata(hcd->self.controller);
274         struct usb_hub_descriptor *desc;
275         int ret = -EINVAL;
276         u32 *data = (u32 *)buf;
277
278         dev_dbg(hcd->self.controller,
279                 "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
280                 hcd, typeReq, wValue, wIndex, buf, wLength);
281
282         wIndex--;
283
284         switch (typeReq) {
285         case SetPortFeature:
286                 if (wValue == USB_PORT_FEAT_POWER) {
287                         dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
288                         if (valid_port(wIndex)) {
289                                 ohci_at91_usb_set_power(pdata, wIndex, 1);
290                                 ret = 0;
291                         }
292
293                         goto out;
294                 }
295                 break;
296
297         case ClearPortFeature:
298                 switch (wValue) {
299                 case USB_PORT_FEAT_C_OVER_CURRENT:
300                         dev_dbg(hcd->self.controller,
301                                 "ClearPortFeature: C_OVER_CURRENT\n");
302
303                         if (valid_port(wIndex)) {
304                                 pdata->overcurrent_changed[wIndex] = 0;
305                                 pdata->overcurrent_status[wIndex] = 0;
306                         }
307
308                         goto out;
309
310                 case USB_PORT_FEAT_OVER_CURRENT:
311                         dev_dbg(hcd->self.controller,
312                                 "ClearPortFeature: OVER_CURRENT\n");
313
314                         if (valid_port(wIndex))
315                                 pdata->overcurrent_status[wIndex] = 0;
316
317                         goto out;
318
319                 case USB_PORT_FEAT_POWER:
320                         dev_dbg(hcd->self.controller,
321                                 "ClearPortFeature: POWER\n");
322
323                         if (valid_port(wIndex)) {
324                                 ohci_at91_usb_set_power(pdata, wIndex, 0);
325                                 return 0;
326                         }
327                 }
328                 break;
329         }
330
331         ret = ohci_hub_control(hcd, typeReq, wValue, wIndex + 1, buf, wLength);
332         if (ret)
333                 goto out;
334
335         switch (typeReq) {
336         case GetHubDescriptor:
337
338                 /* update the hub's descriptor */
339
340                 desc = (struct usb_hub_descriptor *)buf;
341
342                 dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
343                         desc->wHubCharacteristics);
344
345                 /* remove the old configurations for power-switching, and
346                  * over-current protection, and insert our new configuration
347                  */
348
349                 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
350                 desc->wHubCharacteristics |= cpu_to_le16(0x0001);
351
352                 if (pdata->overcurrent_supported) {
353                         desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
354                         desc->wHubCharacteristics |=  cpu_to_le16(0x0008|0x0001);
355                 }
356
357                 dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
358                         desc->wHubCharacteristics);
359
360                 return ret;
361
362         case GetPortStatus:
363                 /* check port status */
364
365                 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
366
367                 if (valid_port(wIndex)) {
368                         if (!ohci_at91_usb_get_power(pdata, wIndex))
369                                 *data &= ~cpu_to_le32(RH_PS_PPS);
370
371                         if (pdata->overcurrent_changed[wIndex])
372                                 *data |= cpu_to_le32(RH_PS_OCIC);
373
374                         if (pdata->overcurrent_status[wIndex])
375                                 *data |= cpu_to_le32(RH_PS_POCI);
376                 }
377         }
378
379  out:
380         return ret;
381 }
382
383 /*-------------------------------------------------------------------------*/
384
385 static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
386 {
387         struct platform_device *pdev = data;
388         struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
389         int val, gpio, port;
390
391         /* From the GPIO notifying the over-current situation, find
392          * out the corresponding port */
393         at91_for_each_port(port) {
394                 if (gpio_is_valid(pdata->overcurrent_pin[port]) &&
395                                 gpio_to_irq(pdata->overcurrent_pin[port]) == irq) {
396                         gpio = pdata->overcurrent_pin[port];
397                         break;
398                 }
399         }
400
401         if (port == AT91_MAX_USBH_PORTS) {
402                 dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
403                 return IRQ_HANDLED;
404         }
405
406         val = gpio_get_value(gpio);
407
408         /* When notified of an over-current situation, disable power
409            on the corresponding port, and mark this port in
410            over-current. */
411         if (!val) {
412                 ohci_at91_usb_set_power(pdata, port, 0);
413                 pdata->overcurrent_status[port]  = 1;
414                 pdata->overcurrent_changed[port] = 1;
415         }
416
417         dev_dbg(& pdev->dev, "overcurrent situation %s\n",
418                 val ? "exited" : "notified");
419
420         return IRQ_HANDLED;
421 }
422
423 #ifdef CONFIG_OF
424 static const struct of_device_id at91_ohci_dt_ids[] = {
425         { .compatible = "atmel,at91rm9200-ohci" },
426         { /* sentinel */ }
427 };
428
429 MODULE_DEVICE_TABLE(of, at91_ohci_dt_ids);
430
431 static int ohci_at91_of_init(struct platform_device *pdev)
432 {
433         struct device_node *np = pdev->dev.of_node;
434         int i, gpio, ret;
435         enum of_gpio_flags flags;
436         struct at91_usbh_data   *pdata;
437         u32 ports;
438
439         if (!np)
440                 return 0;
441
442         /* Right now device-tree probed devices don't get dma_mask set.
443          * Since shared usb code relies on it, set it here for now.
444          * Once we have dma capability bindings this can go away.
445          */
446         ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
447         if (ret)
448                 return ret;
449
450         pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
451         if (!pdata)
452                 return -ENOMEM;
453
454         if (!of_property_read_u32(np, "num-ports", &ports))
455                 pdata->ports = ports;
456
457         at91_for_each_port(i) {
458                 gpio = of_get_named_gpio_flags(np, "atmel,vbus-gpio", i, &flags);
459                 pdata->vbus_pin[i] = gpio;
460                 if (!gpio_is_valid(gpio))
461                         continue;
462                 pdata->vbus_pin_active_low[i] = flags & OF_GPIO_ACTIVE_LOW;
463         }
464
465         at91_for_each_port(i)
466                 pdata->overcurrent_pin[i] =
467                         of_get_named_gpio_flags(np, "atmel,oc-gpio", i, &flags);
468
469         pdev->dev.platform_data = pdata;
470
471         return 0;
472 }
473 #else
474 static int ohci_at91_of_init(struct platform_device *pdev)
475 {
476         return 0;
477 }
478 #endif
479
480 /*-------------------------------------------------------------------------*/
481
482 static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
483 {
484         struct at91_usbh_data   *pdata;
485         int                     i;
486         int                     gpio;
487         int                     ret;
488
489         ret = ohci_at91_of_init(pdev);
490         if (ret)
491                 return ret;
492
493         pdata = dev_get_platdata(&pdev->dev);
494
495         if (pdata) {
496                 at91_for_each_port(i) {
497                         /*
498                          * do not configure PIO if not in relation with
499                          * real USB port on board
500                          */
501                         if (i >= pdata->ports) {
502                                 pdata->vbus_pin[i] = -EINVAL;
503                                 pdata->overcurrent_pin[i] = -EINVAL;
504                                 break;
505                         }
506
507                         if (!gpio_is_valid(pdata->vbus_pin[i]))
508                                 continue;
509                         gpio = pdata->vbus_pin[i];
510
511                         ret = gpio_request(gpio, "ohci_vbus");
512                         if (ret) {
513                                 dev_err(&pdev->dev,
514                                         "can't request vbus gpio %d\n", gpio);
515                                 continue;
516                         }
517                         ret = gpio_direction_output(gpio,
518                                                 !pdata->vbus_pin_active_low[i]);
519                         if (ret) {
520                                 dev_err(&pdev->dev,
521                                         "can't put vbus gpio %d as output %d\n",
522                                         gpio, !pdata->vbus_pin_active_low[i]);
523                                 gpio_free(gpio);
524                                 continue;
525                         }
526
527                         ohci_at91_usb_set_power(pdata, i, 1);
528                 }
529
530                 at91_for_each_port(i) {
531                         if (!gpio_is_valid(pdata->overcurrent_pin[i]))
532                                 continue;
533                         gpio = pdata->overcurrent_pin[i];
534
535                         ret = gpio_request(gpio, "ohci_overcurrent");
536                         if (ret) {
537                                 dev_err(&pdev->dev,
538                                         "can't request overcurrent gpio %d\n",
539                                         gpio);
540                                 continue;
541                         }
542
543                         ret = gpio_direction_input(gpio);
544                         if (ret) {
545                                 dev_err(&pdev->dev,
546                                         "can't configure overcurrent gpio %d as input\n",
547                                         gpio);
548                                 gpio_free(gpio);
549                                 continue;
550                         }
551
552                         ret = request_irq(gpio_to_irq(gpio),
553                                           ohci_hcd_at91_overcurrent_irq,
554                                           IRQF_SHARED, "ohci_overcurrent", pdev);
555                         if (ret) {
556                                 gpio_free(gpio);
557                                 dev_err(&pdev->dev,
558                                         "can't get gpio IRQ for overcurrent\n");
559                         }
560                 }
561         }
562
563         device_init_wakeup(&pdev->dev, 1);
564         return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
565 }
566
567 static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
568 {
569         struct at91_usbh_data   *pdata = dev_get_platdata(&pdev->dev);
570         int                     i;
571
572         if (pdata) {
573                 at91_for_each_port(i) {
574                         if (!gpio_is_valid(pdata->vbus_pin[i]))
575                                 continue;
576                         ohci_at91_usb_set_power(pdata, i, 0);
577                         gpio_free(pdata->vbus_pin[i]);
578                 }
579
580                 at91_for_each_port(i) {
581                         if (!gpio_is_valid(pdata->overcurrent_pin[i]))
582                                 continue;
583                         free_irq(gpio_to_irq(pdata->overcurrent_pin[i]), pdev);
584                         gpio_free(pdata->overcurrent_pin[i]);
585                 }
586         }
587
588         device_init_wakeup(&pdev->dev, 0);
589         usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
590         return 0;
591 }
592
593 #ifdef CONFIG_PM
594
595 static int
596 ohci_hcd_at91_drv_suspend(struct platform_device *pdev, pm_message_t mesg)
597 {
598         struct usb_hcd  *hcd = platform_get_drvdata(pdev);
599         struct ohci_hcd *ohci = hcd_to_ohci(hcd);
600         bool            do_wakeup = device_may_wakeup(&pdev->dev);
601         int             ret;
602
603         if (do_wakeup)
604                 enable_irq_wake(hcd->irq);
605
606         ret = ohci_suspend(hcd, do_wakeup);
607         if (ret) {
608                 disable_irq_wake(hcd->irq);
609                 return ret;
610         }
611         /*
612          * The integrated transceivers seem unable to notice disconnect,
613          * reconnect, or wakeup without the 48 MHz clock active.  so for
614          * correctness, always discard connection state (using reset).
615          *
616          * REVISIT: some boards will be able to turn VBUS off...
617          */
618         if (at91_suspend_entering_slow_clock()) {
619                 ohci->hc_control = ohci_readl(ohci, &ohci->regs->control);
620                 ohci->hc_control &= OHCI_CTRL_RWC;
621                 ohci_writel(ohci, ohci->hc_control, &ohci->regs->control);
622                 ohci->rh_state = OHCI_RH_HALTED;
623
624                 /* flush the writes */
625                 (void) ohci_readl (ohci, &ohci->regs->control);
626                 at91_stop_clock();
627         }
628
629         return ret;
630 }
631
632 static int ohci_hcd_at91_drv_resume(struct platform_device *pdev)
633 {
634         struct usb_hcd  *hcd = platform_get_drvdata(pdev);
635
636         if (device_may_wakeup(&pdev->dev))
637                 disable_irq_wake(hcd->irq);
638
639         if (!clocked)
640                 at91_start_clock();
641
642         ohci_resume(hcd, false);
643         return 0;
644 }
645 #else
646 #define ohci_hcd_at91_drv_suspend NULL
647 #define ohci_hcd_at91_drv_resume  NULL
648 #endif
649
650 static struct platform_driver ohci_hcd_at91_driver = {
651         .probe          = ohci_hcd_at91_drv_probe,
652         .remove         = ohci_hcd_at91_drv_remove,
653         .shutdown       = usb_hcd_platform_shutdown,
654         .suspend        = ohci_hcd_at91_drv_suspend,
655         .resume         = ohci_hcd_at91_drv_resume,
656         .driver         = {
657                 .name   = "at91_ohci",
658                 .of_match_table = of_match_ptr(at91_ohci_dt_ids),
659         },
660 };
661
662 static int __init ohci_at91_init(void)
663 {
664         if (usb_disabled())
665                 return -ENODEV;
666
667         pr_info("%s: " DRIVER_DESC "\n", hcd_name);
668         ohci_init_driver(&ohci_at91_hc_driver, NULL);
669
670         /*
671          * The Atmel HW has some unusual quirks, which require Atmel-specific
672          * workarounds. We override certain hc_driver functions here to
673          * achieve that. We explicitly do not enhance ohci_driver_overrides to
674          * allow this more easily, since this is an unusual case, and we don't
675          * want to encourage others to override these functions by making it
676          * too easy.
677          */
678
679         ohci_at91_hc_driver.hub_status_data     = ohci_at91_hub_status_data;
680         ohci_at91_hc_driver.hub_control         = ohci_at91_hub_control;
681
682         return platform_driver_register(&ohci_hcd_at91_driver);
683 }
684 module_init(ohci_at91_init);
685
686 static void __exit ohci_at91_cleanup(void)
687 {
688         platform_driver_unregister(&ohci_hcd_at91_driver);
689 }
690 module_exit(ohci_at91_cleanup);
691
692 MODULE_DESCRIPTION(DRIVER_DESC);
693 MODULE_LICENSE("GPL");
694 MODULE_ALIAS("platform:at91_ohci");