blob: d849c809acbdca97394fa7b8dcd8225733a91f5a [file] [log] [blame]
Andrew Victor39a269c2006-01-22 10:32:13 -08001/*
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 *
David Brownell0365ee02006-06-19 14:27:20 -07007 * AT91 Bus Glue
Andrew Victor39a269c2006-01-22 10:32:13 -08008 *
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 <asm/mach-types.h>
19#include <asm/hardware.h>
20#include <asm/arch/board.h>
Andrew Victored077bb2007-02-16 10:18:58 -080021#include <asm/arch/cpu.h>
Andrew Victor39a269c2006-01-22 10:32:13 -080022
David Brownell0365ee02006-06-19 14:27:20 -070023#ifndef CONFIG_ARCH_AT91
24#error "CONFIG_ARCH_AT91 must be defined."
Andrew Victor39a269c2006-01-22 10:32:13 -080025#endif
26
Andrew Victored077bb2007-02-16 10:18:58 -080027/* interface and function clocks; sometimes also an AHB clock */
28static struct clk *iclk, *fclk, *hclk;
David Brownell0365ee02006-06-19 14:27:20 -070029static int clocked;
Andrew Victor39a269c2006-01-22 10:32:13 -080030
31extern int usb_disabled(void);
32
33/*-------------------------------------------------------------------------*/
34
Andrew Victored077bb2007-02-16 10:18:58 -080035static void at91_start_clock(void)
36{
37 if (cpu_is_at91sam9261())
38 clk_enable(hclk);
39 clk_enable(iclk);
40 clk_enable(fclk);
41 clocked = 1;
42}
43
44static void at91_stop_clock(void)
45{
46 clk_disable(fclk);
47 clk_disable(iclk);
48 if (cpu_is_at91sam9261())
49 clk_disable(hclk);
50 clocked = 0;
51}
52
Andrew Victor39a269c2006-01-22 10:32:13 -080053static void at91_start_hc(struct platform_device *pdev)
54{
55 struct usb_hcd *hcd = platform_get_drvdata(pdev);
56 struct ohci_regs __iomem *regs = hcd->regs;
57
David Brownell0365ee02006-06-19 14:27:20 -070058 dev_dbg(&pdev->dev, "start\n");
Andrew Victor39a269c2006-01-22 10:32:13 -080059
60 /*
61 * Start the USB clocks.
62 */
Andrew Victored077bb2007-02-16 10:18:58 -080063 at91_start_clock();
Andrew Victor39a269c2006-01-22 10:32:13 -080064
65 /*
66 * The USB host controller must remain in reset.
67 */
68 writel(0, &regs->control);
69}
70
71static void at91_stop_hc(struct platform_device *pdev)
72{
73 struct usb_hcd *hcd = platform_get_drvdata(pdev);
74 struct ohci_regs __iomem *regs = hcd->regs;
75
David Brownell0365ee02006-06-19 14:27:20 -070076 dev_dbg(&pdev->dev, "stop\n");
Andrew Victor39a269c2006-01-22 10:32:13 -080077
78 /*
79 * Put the USB host controller into reset.
80 */
81 writel(0, &regs->control);
82
83 /*
84 * Stop the USB clocks.
85 */
Andrew Victored077bb2007-02-16 10:18:58 -080086 at91_stop_clock();
Andrew Victor39a269c2006-01-22 10:32:13 -080087}
88
89
90/*-------------------------------------------------------------------------*/
91
92static int usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
93
94/* configure so an HC device and id are always provided */
95/* always called with process context; sleeping is OK */
96
97
98/**
David Brownell0365ee02006-06-19 14:27:20 -070099 * usb_hcd_at91_probe - initialize AT91-based HCDs
Andrew Victor39a269c2006-01-22 10:32:13 -0800100 * Context: !in_interrupt()
101 *
102 * Allocates basic resources for this USB host controller, and
103 * then invokes the start() method for the HCD associated with it
104 * through the hotplug entry's driver_data.
Andrew Victor39a269c2006-01-22 10:32:13 -0800105 */
David Brownell0365ee02006-06-19 14:27:20 -0700106static int usb_hcd_at91_probe(const struct hc_driver *driver,
107 struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800108{
109 int retval;
110 struct usb_hcd *hcd = NULL;
111
112 if (pdev->num_resources != 2) {
113 pr_debug("hcd probe: invalid num_resources");
114 return -ENODEV;
115 }
116
David Brownell0365ee02006-06-19 14:27:20 -0700117 if ((pdev->resource[0].flags != IORESOURCE_MEM)
118 || (pdev->resource[1].flags != IORESOURCE_IRQ)) {
Andrew Victor39a269c2006-01-22 10:32:13 -0800119 pr_debug("hcd probe: invalid resource type\n");
120 return -ENODEV;
121 }
122
David Brownell0365ee02006-06-19 14:27:20 -0700123 hcd = usb_create_hcd(driver, &pdev->dev, "at91");
Andrew Victor39a269c2006-01-22 10:32:13 -0800124 if (!hcd)
125 return -ENOMEM;
126 hcd->rsrc_start = pdev->resource[0].start;
127 hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;
128
129 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
130 pr_debug("request_mem_region failed\n");
131 retval = -EBUSY;
132 goto err1;
133 }
134
135 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
136 if (!hcd->regs) {
137 pr_debug("ioremap failed\n");
138 retval = -EIO;
139 goto err2;
140 }
141
142 iclk = clk_get(&pdev->dev, "ohci_clk");
143 fclk = clk_get(&pdev->dev, "uhpck");
Andrew Victored077bb2007-02-16 10:18:58 -0800144 if (cpu_is_at91sam9261())
145 hclk = clk_get(&pdev->dev, "hck0");
Andrew Victor39a269c2006-01-22 10:32:13 -0800146
147 at91_start_hc(pdev);
148 ohci_hcd_init(hcd_to_ohci(hcd));
149
Thomas Gleixnerd54b5ca2006-07-01 19:29:44 -0700150 retval = usb_add_hcd(hcd, pdev->resource[1].start, IRQF_DISABLED);
Andrew Victor39a269c2006-01-22 10:32:13 -0800151 if (retval == 0)
152 return retval;
153
154 /* Error handling */
155 at91_stop_hc(pdev);
156
Andrew Victored077bb2007-02-16 10:18:58 -0800157 if (cpu_is_at91sam9261())
158 clk_put(hclk);
Andrew Victor39a269c2006-01-22 10:32:13 -0800159 clk_put(fclk);
160 clk_put(iclk);
161
162 iounmap(hcd->regs);
163
164 err2:
165 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
166
167 err1:
168 usb_put_hcd(hcd);
169 return retval;
170}
171
172
Andrew Victor39a269c2006-01-22 10:32:13 -0800173/* may be called with controller, bus, and devices active */
174
175/**
David Brownell0365ee02006-06-19 14:27:20 -0700176 * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
Andrew Victor39a269c2006-01-22 10:32:13 -0800177 * @dev: USB Host Controller being removed
178 * Context: !in_interrupt()
179 *
180 * Reverses the effect of usb_hcd_at91_probe(), first invoking
181 * the HCD's stop() method. It is always called from a thread
David Brownell0365ee02006-06-19 14:27:20 -0700182 * context, "rmmod" or something similar.
Andrew Victor39a269c2006-01-22 10:32:13 -0800183 *
184 */
David Brownell0365ee02006-06-19 14:27:20 -0700185static int usb_hcd_at91_remove(struct usb_hcd *hcd,
186 struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800187{
188 usb_remove_hcd(hcd);
189 at91_stop_hc(pdev);
190 iounmap(hcd->regs);
David Brownell68ba61b2006-04-02 20:26:21 -0800191 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
Andrew Victor39a269c2006-01-22 10:32:13 -0800192
Andrew Victored077bb2007-02-16 10:18:58 -0800193 if (cpu_is_at91sam9261())
194 clk_put(hclk);
David Brownell68ba61b2006-04-02 20:26:21 -0800195 clk_put(fclk);
196 clk_put(iclk);
Andrew Victored077bb2007-02-16 10:18:58 -0800197 fclk = iclk = hclk = NULL;
Andrew Victor39a269c2006-01-22 10:32:13 -0800198
199 dev_set_drvdata(&pdev->dev, NULL);
200 return 0;
201}
202
203/*-------------------------------------------------------------------------*/
204
205static int __devinit
206ohci_at91_start (struct usb_hcd *hcd)
207{
David Brownell0365ee02006-06-19 14:27:20 -0700208 struct at91_usbh_data *board = hcd->self.controller->platform_data;
Andrew Victor39a269c2006-01-22 10:32:13 -0800209 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
210 int ret;
211
212 if ((ret = ohci_init(ohci)) < 0)
213 return ret;
214
David Brownellcd22afd2006-09-03 12:21:50 -0700215 ohci->num_ports = board->ports;
David Brownell0365ee02006-06-19 14:27:20 -0700216
Andrew Victor39a269c2006-01-22 10:32:13 -0800217 if ((ret = ohci_run(ohci)) < 0) {
218 err("can't start %s", hcd->self.bus_name);
219 ohci_stop(hcd);
220 return ret;
221 }
Andrew Victor39a269c2006-01-22 10:32:13 -0800222 return 0;
223}
224
225/*-------------------------------------------------------------------------*/
226
227static const struct hc_driver ohci_at91_hc_driver = {
228 .description = hcd_name,
David Brownell0365ee02006-06-19 14:27:20 -0700229 .product_desc = "AT91 OHCI",
Andrew Victor39a269c2006-01-22 10:32:13 -0800230 .hcd_priv_size = sizeof(struct ohci_hcd),
231
232 /*
233 * generic hardware linkage
234 */
235 .irq = ohci_irq,
236 .flags = HCD_USB11 | HCD_MEMORY,
237
238 /*
239 * basic lifecycle operations
240 */
241 .start = ohci_at91_start,
242 .stop = ohci_stop,
David Brownelldd9048a2006-12-05 03:18:31 -0800243 .shutdown = ohci_shutdown,
Andrew Victor39a269c2006-01-22 10:32:13 -0800244
245 /*
246 * managing i/o requests and associated device resources
247 */
248 .urb_enqueue = ohci_urb_enqueue,
249 .urb_dequeue = ohci_urb_dequeue,
250 .endpoint_disable = ohci_endpoint_disable,
251
252 /*
253 * scheduling support
254 */
255 .get_frame_number = ohci_get_frame,
256
257 /*
258 * root hub support
259 */
260 .hub_status_data = ohci_hub_status_data,
261 .hub_control = ohci_hub_control,
David Brownelld4139842006-08-04 11:31:55 -0700262 .hub_irq_enable = ohci_rhsc_enable,
Andrew Victor39a269c2006-01-22 10:32:13 -0800263#ifdef CONFIG_PM
David Brownell68ba61b2006-04-02 20:26:21 -0800264 .bus_suspend = ohci_bus_suspend,
265 .bus_resume = ohci_bus_resume,
Andrew Victor39a269c2006-01-22 10:32:13 -0800266#endif
267 .start_port_reset = ohci_start_port_reset,
268};
269
270/*-------------------------------------------------------------------------*/
271
David Brownell0365ee02006-06-19 14:27:20 -0700272static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800273{
David Brownell0365ee02006-06-19 14:27:20 -0700274 device_init_wakeup(&pdev->dev, 1);
275 return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
Andrew Victor39a269c2006-01-22 10:32:13 -0800276}
277
David Brownell0365ee02006-06-19 14:27:20 -0700278static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800279{
David Brownell0365ee02006-06-19 14:27:20 -0700280 device_init_wakeup(&pdev->dev, 0);
281 return usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
Andrew Victor39a269c2006-01-22 10:32:13 -0800282}
283
284#ifdef CONFIG_PM
Andrew Victor39a269c2006-01-22 10:32:13 -0800285
David Brownell68ba61b2006-04-02 20:26:21 -0800286static int
David Brownell0365ee02006-06-19 14:27:20 -0700287ohci_hcd_at91_drv_suspend(struct platform_device *pdev, pm_message_t mesg)
David Brownell68ba61b2006-04-02 20:26:21 -0800288{
David Brownell0365ee02006-06-19 14:27:20 -0700289 struct usb_hcd *hcd = platform_get_drvdata(pdev);
290 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
291
292 if (device_may_wakeup(&pdev->dev))
293 enable_irq_wake(hcd->irq);
David Brownell0365ee02006-06-19 14:27:20 -0700294
295 /*
296 * The integrated transceivers seem unable to notice disconnect,
297 * reconnect, or wakeup without the 48 MHz clock active. so for
298 * correctness, always discard connection state (using reset).
299 *
300 * REVISIT: some boards will be able to turn VBUS off...
301 */
302 if (at91_suspend_entering_slow_clock()) {
303 ohci_usb_reset (ohci);
Andrew Victored077bb2007-02-16 10:18:58 -0800304 at91_stop_clock();
David Brownell0365ee02006-06-19 14:27:20 -0700305 }
Andrew Victor39a269c2006-01-22 10:32:13 -0800306
307 return 0;
308}
309
David Brownell0365ee02006-06-19 14:27:20 -0700310static int ohci_hcd_at91_drv_resume(struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800311{
Marc Pignat6dde8962007-01-09 14:00:11 -0800312 struct usb_hcd *hcd = platform_get_drvdata(pdev);
313
314 if (device_may_wakeup(&pdev->dev))
315 disable_irq_wake(hcd->irq);
316
Andrew Victored077bb2007-02-16 10:18:58 -0800317 if (!clocked)
318 at91_start_clock();
Andrew Victor39a269c2006-01-22 10:32:13 -0800319
320 return 0;
321}
322#else
323#define ohci_hcd_at91_drv_suspend NULL
324#define ohci_hcd_at91_drv_resume NULL
325#endif
326
David Brownell0365ee02006-06-19 14:27:20 -0700327MODULE_ALIAS("at91_ohci");
David Brownell68ba61b2006-04-02 20:26:21 -0800328
Andrew Victor39a269c2006-01-22 10:32:13 -0800329static struct platform_driver ohci_hcd_at91_driver = {
330 .probe = ohci_hcd_at91_drv_probe,
331 .remove = ohci_hcd_at91_drv_remove,
Aleksey Gorelov64a21d02006-08-08 17:24:08 -0700332 .shutdown = usb_hcd_platform_shutdown,
Andrew Victor39a269c2006-01-22 10:32:13 -0800333 .suspend = ohci_hcd_at91_drv_suspend,
334 .resume = ohci_hcd_at91_drv_resume,
335 .driver = {
David Brownell0365ee02006-06-19 14:27:20 -0700336 .name = "at91_ohci",
Andrew Victor39a269c2006-01-22 10:32:13 -0800337 .owner = THIS_MODULE,
338 },
339};
340