Merge master.kernel.org:/pub/scm/linux/kernel/git/wim/linux-2.6-watchdog
[sfrench/cifs-2.6.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 <asm/mach-types.h>
19 #include <asm/hardware.h>
20 #include <asm/arch/board.h>
21 #include <asm/arch/cpu.h>
22
23 #ifndef CONFIG_ARCH_AT91
24 #error "CONFIG_ARCH_AT91 must be defined."
25 #endif
26
27 /* interface and function clocks; sometimes also an AHB clock */
28 static struct clk *iclk, *fclk, *hclk;
29 static int clocked;
30
31 extern int usb_disabled(void);
32
33 /*-------------------------------------------------------------------------*/
34
35 static 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
44 static 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
53 static 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
58         dev_dbg(&pdev->dev, "start\n");
59
60         /*
61          * Start the USB clocks.
62          */
63         at91_start_clock();
64
65         /*
66          * The USB host controller must remain in reset.
67          */
68         writel(0, &regs->control);
69 }
70
71 static 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
76         dev_dbg(&pdev->dev, "stop\n");
77
78         /*
79          * Put the USB host controller into reset.
80          */
81         writel(0, &regs->control);
82
83         /*
84          * Stop the USB clocks.
85          */
86         at91_stop_clock();
87 }
88
89
90 /*-------------------------------------------------------------------------*/
91
92 static 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 /**
99  * usb_hcd_at91_probe - initialize AT91-based HCDs
100  * 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.
105  */
106 static int usb_hcd_at91_probe(const struct hc_driver *driver,
107                         struct platform_device *pdev)
108 {
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
117         if ((pdev->resource[0].flags != IORESOURCE_MEM)
118                         || (pdev->resource[1].flags != IORESOURCE_IRQ)) {
119                 pr_debug("hcd probe: invalid resource type\n");
120                 return -ENODEV;
121         }
122
123         hcd = usb_create_hcd(driver, &pdev->dev, "at91");
124         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");
144         if (cpu_is_at91sam9261())
145                 hclk = clk_get(&pdev->dev, "hck0");
146
147         at91_start_hc(pdev);
148         ohci_hcd_init(hcd_to_ohci(hcd));
149
150         retval = usb_add_hcd(hcd, pdev->resource[1].start, IRQF_DISABLED);
151         if (retval == 0)
152                 return retval;
153
154         /* Error handling */
155         at91_stop_hc(pdev);
156
157         if (cpu_is_at91sam9261())
158                 clk_put(hclk);
159         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
173 /* may be called with controller, bus, and devices active */
174
175 /**
176  * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
177  * @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
182  * context, "rmmod" or something similar.
183  *
184  */
185 static int usb_hcd_at91_remove(struct usb_hcd *hcd,
186                                 struct platform_device *pdev)
187 {
188         usb_remove_hcd(hcd);
189         at91_stop_hc(pdev);
190         iounmap(hcd->regs);
191         release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
192
193         if (cpu_is_at91sam9261())
194                 clk_put(hclk);
195         clk_put(fclk);
196         clk_put(iclk);
197         fclk = iclk = hclk = NULL;
198
199         dev_set_drvdata(&pdev->dev, NULL);
200         return 0;
201 }
202
203 /*-------------------------------------------------------------------------*/
204
205 static int __devinit
206 ohci_at91_start (struct usb_hcd *hcd)
207 {
208         struct at91_usbh_data   *board = hcd->self.controller->platform_data;
209         struct ohci_hcd         *ohci = hcd_to_ohci (hcd);
210         int                     ret;
211
212         if ((ret = ohci_init(ohci)) < 0)
213                 return ret;
214
215         ohci->num_ports = board->ports;
216
217         if ((ret = ohci_run(ohci)) < 0) {
218                 err("can't start %s", hcd->self.bus_name);
219                 ohci_stop(hcd);
220                 return ret;
221         }
222         return 0;
223 }
224
225 /*-------------------------------------------------------------------------*/
226
227 static const struct hc_driver ohci_at91_hc_driver = {
228         .description =          hcd_name,
229         .product_desc =         "AT91 OHCI",
230         .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,
243         .shutdown =             ohci_shutdown,
244
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,
262         .hub_irq_enable =       ohci_rhsc_enable,
263 #ifdef CONFIG_PM
264         .bus_suspend =          ohci_bus_suspend,
265         .bus_resume =           ohci_bus_resume,
266 #endif
267         .start_port_reset =     ohci_start_port_reset,
268 };
269
270 /*-------------------------------------------------------------------------*/
271
272 static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
273 {
274         device_init_wakeup(&pdev->dev, 1);
275         return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
276 }
277
278 static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
279 {
280         device_init_wakeup(&pdev->dev, 0);
281         return usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
282 }
283
284 #ifdef CONFIG_PM
285
286 static int
287 ohci_hcd_at91_drv_suspend(struct platform_device *pdev, pm_message_t mesg)
288 {
289         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);
294
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);
304                 at91_stop_clock();
305         }
306
307         return 0;
308 }
309
310 static int ohci_hcd_at91_drv_resume(struct platform_device *pdev)
311 {
312         struct usb_hcd  *hcd = platform_get_drvdata(pdev);
313
314         if (device_may_wakeup(&pdev->dev))
315                 disable_irq_wake(hcd->irq);
316
317         if (!clocked)
318                 at91_start_clock();
319
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
327 MODULE_ALIAS("at91_ohci");
328
329 static struct platform_driver ohci_hcd_at91_driver = {
330         .probe          = ohci_hcd_at91_drv_probe,
331         .remove         = ohci_hcd_at91_drv_remove,
332         .shutdown       = usb_hcd_platform_shutdown,
333         .suspend        = ohci_hcd_at91_drv_suspend,
334         .resume         = ohci_hcd_at91_drv_resume,
335         .driver         = {
336                 .name   = "at91_ohci",
337                 .owner  = THIS_MODULE,
338         },
339 };
340