ohci-at91.c (118cb990cd66791e5ae86bfae22b8b9c64bd5fd9) ohci-at91.c (22d9d8e8316d7f69046c8805ce9aa8d9c43d4e5b)
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>
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>
16#include <linux/platform_device.h>
17#include <linux/of_platform.h>
18#include <linux/of_gpio.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>
19#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 <mach/hardware.h>
28#include <asm/gpio.h>
29
30#include <mach/cpu.h>
31
20
21#include <mach/hardware.h>
22#include <asm/gpio.h>
23
24#include <mach/cpu.h>
25
26#ifndef CONFIG_ARCH_AT91
27#error "CONFIG_ARCH_AT91 must be defined."
28#endif
32
29
33#include "ohci.h"
34
35#define valid_port(index) ((index) >= 0 && (index) < AT91_MAX_USBH_PORTS)
36#define at91_for_each_port(index) \
37 for ((index) = 0; (index) < AT91_MAX_USBH_PORTS; (index)++)
38
39/* interface, function and usb clocks; sometimes also an AHB clock */
40static struct clk *iclk, *fclk, *uclk, *hclk;
30#define valid_port(index) ((index) >= 0 && (index) < AT91_MAX_USBH_PORTS)
31#define at91_for_each_port(index) \
32 for ((index) = 0; (index) < AT91_MAX_USBH_PORTS; (index)++)
33
34/* interface, function and usb clocks; sometimes also an AHB clock */
35static struct clk *iclk, *fclk, *uclk, *hclk;
41/* interface and function clocks; sometimes also an AHB clock */
42
43#define DRIVER_DESC "OHCI Atmel driver"
44
45static const char hcd_name[] = "ohci-atmel";
46
47static struct hc_driver __read_mostly ohci_at91_hc_driver;
48static int clocked;
36static int clocked;
49static int (*orig_ohci_hub_control)(struct usb_hcd *hcd, u16 typeReq,
50 u16 wValue, u16 wIndex, char *buf, u16 wLength);
51static int (*orig_ohci_hub_status_data)(struct usb_hcd *hcd, char *buf);
52
53extern int usb_disabled(void);
54
55/*-------------------------------------------------------------------------*/
56
57static void at91_start_clock(void)
58{
59 if (IS_ENABLED(CONFIG_COMMON_CLK)) {

--- 67 unchanged lines hidden (view full) ---

127 *
128 * Allocates basic resources for this USB host controller, and
129 * then invokes the start() method for the HCD associated with it
130 * through the hotplug entry's driver_data.
131 */
132static int usb_hcd_at91_probe(const struct hc_driver *driver,
133 struct platform_device *pdev)
134{
37
38extern int usb_disabled(void);
39
40/*-------------------------------------------------------------------------*/
41
42static void at91_start_clock(void)
43{
44 if (IS_ENABLED(CONFIG_COMMON_CLK)) {

--- 67 unchanged lines hidden (view full) ---

112 *
113 * Allocates basic resources for this USB host controller, and
114 * then invokes the start() method for the HCD associated with it
115 * through the hotplug entry's driver_data.
116 */
117static int usb_hcd_at91_probe(const struct hc_driver *driver,
118 struct platform_device *pdev)
119{
135 struct at91_usbh_data *board;
136 struct ohci_hcd *ohci;
137 int retval;
138 struct usb_hcd *hcd = NULL;
139
140 if (pdev->num_resources != 2) {
141 pr_debug("hcd probe: invalid num_resources");
142 return -ENODEV;
143 }
144

--- 44 unchanged lines hidden (view full) ---

189 uclk = clk_get(&pdev->dev, "usb_clk");
190 if (IS_ERR(uclk)) {
191 dev_err(&pdev->dev, "failed to get uclk\n");
192 retval = PTR_ERR(uclk);
193 goto err6;
194 }
195 }
196
120 int retval;
121 struct usb_hcd *hcd = NULL;
122
123 if (pdev->num_resources != 2) {
124 pr_debug("hcd probe: invalid num_resources");
125 return -ENODEV;
126 }
127

--- 44 unchanged lines hidden (view full) ---

172 uclk = clk_get(&pdev->dev, "usb_clk");
173 if (IS_ERR(uclk)) {
174 dev_err(&pdev->dev, "failed to get uclk\n");
175 retval = PTR_ERR(uclk);
176 goto err6;
177 }
178 }
179
197 board = hcd->self.controller->platform_data;
198 ohci = hcd_to_ohci(hcd);
199 ohci->num_ports = board->ports;
200 at91_start_hc(pdev);
180 at91_start_hc(pdev);
181 ohci_hcd_init(hcd_to_ohci(hcd));
201
202 retval = usb_add_hcd(hcd, pdev->resource[1].start, IRQF_SHARED);
203 if (retval == 0)
204 return retval;
205
206 /* Error handling */
207 at91_stop_hc(pdev);
208

--- 43 unchanged lines hidden (view full) ---

252 clk_put(uclk);
253 clk_put(hclk);
254 clk_put(fclk);
255 clk_put(iclk);
256 fclk = iclk = hclk = NULL;
257}
258
259/*-------------------------------------------------------------------------*/
182
183 retval = usb_add_hcd(hcd, pdev->resource[1].start, IRQF_SHARED);
184 if (retval == 0)
185 return retval;
186
187 /* Error handling */
188 at91_stop_hc(pdev);
189

--- 43 unchanged lines hidden (view full) ---

233 clk_put(uclk);
234 clk_put(hclk);
235 clk_put(fclk);
236 clk_put(iclk);
237 fclk = iclk = hclk = NULL;
238}
239
240/*-------------------------------------------------------------------------*/
241
242static int
243ohci_at91_reset (struct usb_hcd *hcd)
244{
245 struct at91_usbh_data *board = dev_get_platdata(hcd->self.controller);
246 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
247 int ret;
248
249 if ((ret = ohci_init(ohci)) < 0)
250 return ret;
251
252 ohci->num_ports = board->ports;
253 return 0;
254}
255
256static int
257ohci_at91_start (struct usb_hcd *hcd)
258{
259 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
260 int ret;
261
262 if ((ret = ohci_run(ohci)) < 0) {
263 dev_err(hcd->self.controller, "can't start %s\n",
264 hcd->self.bus_name);
265 ohci_stop(hcd);
266 return ret;
267 }
268 return 0;
269}
270
260static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
261{
262 if (!valid_port(port))
263 return;
264
265 if (!gpio_is_valid(pdata->vbus_pin[port]))
266 return;
267

--- 13 unchanged lines hidden (view full) ---

281 pdata->vbus_pin_active_low[port];
282}
283
284/*
285 * Update the status data from the hub with the over-current indicator change.
286 */
287static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
288{
271static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
272{
273 if (!valid_port(port))
274 return;
275
276 if (!gpio_is_valid(pdata->vbus_pin[port]))
277 return;
278

--- 13 unchanged lines hidden (view full) ---

292 pdata->vbus_pin_active_low[port];
293}
294
295/*
296 * Update the status data from the hub with the over-current indicator change.
297 */
298static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
299{
289 struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
290 int length = orig_ohci_hub_status_data(hcd, buf);
300 struct at91_usbh_data *pdata = dev_get_platdata(hcd->self.controller);
301 int length = ohci_hub_status_data(hcd, buf);
291 int port;
292
293 at91_for_each_port(port) {
294 if (pdata->overcurrent_changed[port]) {
295 if (!length)
296 length = 1;
297 buf[0] |= 1 << (port + 1);
298 }

--- 61 unchanged lines hidden (view full) ---

360 if (valid_port(wIndex)) {
361 ohci_at91_usb_set_power(pdata, wIndex, 0);
362 return 0;
363 }
364 }
365 break;
366 }
367
302 int port;
303
304 at91_for_each_port(port) {
305 if (pdata->overcurrent_changed[port]) {
306 if (!length)
307 length = 1;
308 buf[0] |= 1 << (port + 1);
309 }

--- 61 unchanged lines hidden (view full) ---

371 if (valid_port(wIndex)) {
372 ohci_at91_usb_set_power(pdata, wIndex, 0);
373 return 0;
374 }
375 }
376 break;
377 }
378
368 ret = orig_ohci_hub_control(hcd, typeReq, wValue, wIndex + 1,
369 buf, wLength);
379 ret = ohci_hub_control(hcd, typeReq, wValue, wIndex + 1, buf, wLength);
370 if (ret)
371 goto out;
372
373 switch (typeReq) {
374 case GetHubDescriptor:
375
376 /* update the hub's descriptor */
377

--- 37 unchanged lines hidden (view full) ---

415 }
416
417 out:
418 return ret;
419}
420
421/*-------------------------------------------------------------------------*/
422
380 if (ret)
381 goto out;
382
383 switch (typeReq) {
384 case GetHubDescriptor:
385
386 /* update the hub's descriptor */
387

--- 37 unchanged lines hidden (view full) ---

425 }
426
427 out:
428 return ret;
429}
430
431/*-------------------------------------------------------------------------*/
432
433static const struct hc_driver ohci_at91_hc_driver = {
434 .description = hcd_name,
435 .product_desc = "AT91 OHCI",
436 .hcd_priv_size = sizeof(struct ohci_hcd),
437
438 /*
439 * generic hardware linkage
440 */
441 .irq = ohci_irq,
442 .flags = HCD_USB11 | HCD_MEMORY,
443
444 /*
445 * basic lifecycle operations
446 */
447 .reset = ohci_at91_reset,
448 .start = ohci_at91_start,
449 .stop = ohci_stop,
450 .shutdown = ohci_shutdown,
451
452 /*
453 * managing i/o requests and associated device resources
454 */
455 .urb_enqueue = ohci_urb_enqueue,
456 .urb_dequeue = ohci_urb_dequeue,
457 .endpoint_disable = ohci_endpoint_disable,
458
459 /*
460 * scheduling support
461 */
462 .get_frame_number = ohci_get_frame,
463
464 /*
465 * root hub support
466 */
467 .hub_status_data = ohci_at91_hub_status_data,
468 .hub_control = ohci_at91_hub_control,
469#ifdef CONFIG_PM
470 .bus_suspend = ohci_bus_suspend,
471 .bus_resume = ohci_bus_resume,
472#endif
473 .start_port_reset = ohci_start_port_reset,
474};
475
476/*-------------------------------------------------------------------------*/
477
423static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
424{
425 struct platform_device *pdev = data;
426 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
427 int val, gpio, port;
428
429 /* From the GPIO notifying the over-current situation, find
430 * out the corresponding port */

--- 33 unchanged lines hidden (view full) ---

464 { /* sentinel */ }
465};
466
467MODULE_DEVICE_TABLE(of, at91_ohci_dt_ids);
468
469static int ohci_at91_of_init(struct platform_device *pdev)
470{
471 struct device_node *np = pdev->dev.of_node;
478static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
479{
480 struct platform_device *pdev = data;
481 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
482 int val, gpio, port;
483
484 /* From the GPIO notifying the over-current situation, find
485 * out the corresponding port */

--- 33 unchanged lines hidden (view full) ---

519 { /* sentinel */ }
520};
521
522MODULE_DEVICE_TABLE(of, at91_ohci_dt_ids);
523
524static int ohci_at91_of_init(struct platform_device *pdev)
525{
526 struct device_node *np = pdev->dev.of_node;
472 int i, gpio;
527 int i, gpio, ret;
473 enum of_gpio_flags flags;
474 struct at91_usbh_data *pdata;
475 u32 ports;
476
477 if (!np)
478 return 0;
479
480 /* Right now device-tree probed devices don't get dma_mask set.
481 * Since shared usb code relies on it, set it here for now.
482 * Once we have dma capability bindings this can go away.
483 */
484 if (!pdev->dev.dma_mask)
485 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
528 enum of_gpio_flags flags;
529 struct at91_usbh_data *pdata;
530 u32 ports;
531
532 if (!np)
533 return 0;
534
535 /* Right now device-tree probed devices don't get dma_mask set.
536 * Since shared usb code relies on it, set it here for now.
537 * Once we have dma capability bindings this can go away.
538 */
539 if (!pdev->dev.dma_mask)
540 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
486 if (!pdev->dev.coherent_dma_mask)
487 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
541 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
542 if (ret)
543 return ret;
488
489 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
490 if (!pdata)
491 return -ENOMEM;
492
493 if (!of_property_read_u32(np, "num-ports", &ports))
494 pdata->ports = ports;
495

--- 147 unchanged lines hidden (view full) ---

643 /*
644 * The integrated transceivers seem unable to notice disconnect,
645 * reconnect, or wakeup without the 48 MHz clock active. so for
646 * correctness, always discard connection state (using reset).
647 *
648 * REVISIT: some boards will be able to turn VBUS off...
649 */
650 if (at91_suspend_entering_slow_clock()) {
544
545 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
546 if (!pdata)
547 return -ENOMEM;
548
549 if (!of_property_read_u32(np, "num-ports", &ports))
550 pdata->ports = ports;
551

--- 147 unchanged lines hidden (view full) ---

699 /*
700 * The integrated transceivers seem unable to notice disconnect,
701 * reconnect, or wakeup without the 48 MHz clock active. so for
702 * correctness, always discard connection state (using reset).
703 *
704 * REVISIT: some boards will be able to turn VBUS off...
705 */
706 if (at91_suspend_entering_slow_clock()) {
651 ohci->hc_control = ohci_readl(ohci, &ohci->regs->control);
652 ohci->hc_control &= OHCI_CTRL_RWC;
653 ohci_writel(ohci, ohci->hc_control, &ohci->regs->control);
654 ohci->rh_state = OHCI_RH_HALTED;
655
707 ohci_usb_reset (ohci);
656 /* flush the writes */
657 (void) ohci_readl (ohci, &ohci->regs->control);
658 at91_stop_clock();
659 }
660
661 return 0;
662}
663

--- 10 unchanged lines hidden (view full) ---

674 ohci_resume(hcd, false);
675 return 0;
676}
677#else
678#define ohci_hcd_at91_drv_suspend NULL
679#define ohci_hcd_at91_drv_resume NULL
680#endif
681
708 /* flush the writes */
709 (void) ohci_readl (ohci, &ohci->regs->control);
710 at91_stop_clock();
711 }
712
713 return 0;
714}
715

--- 10 unchanged lines hidden (view full) ---

726 ohci_resume(hcd, false);
727 return 0;
728}
729#else
730#define ohci_hcd_at91_drv_suspend NULL
731#define ohci_hcd_at91_drv_resume NULL
732#endif
733
734MODULE_ALIAS("platform:at91_ohci");
735
682static struct platform_driver ohci_hcd_at91_driver = {
683 .probe = ohci_hcd_at91_drv_probe,
684 .remove = ohci_hcd_at91_drv_remove,
685 .shutdown = usb_hcd_platform_shutdown,
686 .suspend = ohci_hcd_at91_drv_suspend,
687 .resume = ohci_hcd_at91_drv_resume,
688 .driver = {
689 .name = "at91_ohci",
690 .owner = THIS_MODULE,
691 .of_match_table = of_match_ptr(at91_ohci_dt_ids),
692 },
693};
736static struct platform_driver ohci_hcd_at91_driver = {
737 .probe = ohci_hcd_at91_drv_probe,
738 .remove = ohci_hcd_at91_drv_remove,
739 .shutdown = usb_hcd_platform_shutdown,
740 .suspend = ohci_hcd_at91_drv_suspend,
741 .resume = ohci_hcd_at91_drv_resume,
742 .driver = {
743 .name = "at91_ohci",
744 .owner = THIS_MODULE,
745 .of_match_table = of_match_ptr(at91_ohci_dt_ids),
746 },
747};
694
695static int __init ohci_at91_init(void)
696{
697 if (usb_disabled())
698 return -ENODEV;
699
700 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
701 ohci_init_driver(&ohci_at91_hc_driver, NULL);
702
703 /*
704 * The Atmel HW has some unusual quirks, which require Atmel-specific
705 * workarounds. We override certain hc_driver functions here to
706 * achieve that. We explicitly do not enhance ohci_driver_overrides to
707 * allow this more easily, since this is an unusual case, and we don't
708 * want to encourage others to override these functions by making it
709 * too easy.
710 */
711
712 orig_ohci_hub_control = ohci_at91_hc_driver.hub_control;
713 orig_ohci_hub_status_data = ohci_at91_hc_driver.hub_status_data;
714
715 ohci_at91_hc_driver.hub_status_data = ohci_at91_hub_status_data;
716 ohci_at91_hc_driver.hub_control = ohci_at91_hub_control;
717
718 return platform_driver_register(&ohci_hcd_at91_driver);
719}
720module_init(ohci_at91_init);
721
722static void __exit ohci_at91_cleanup(void)
723{
724 platform_driver_unregister(&ohci_hcd_at91_driver);
725}
726module_exit(ohci_at91_cleanup);
727
728MODULE_DESCRIPTION(DRIVER_DESC);
729MODULE_LICENSE("GPL");
730MODULE_ALIAS("platform:at91_ohci");