commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / drivers / usb / host / ohci-sa1111.c
1 /*
2  * OHCI HCD (Host Controller Driver) for USB.
3  *
4  * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5  * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
6  * (C) Copyright 2002 Hewlett-Packard Company
7  * 
8  * SA1111 Bus Glue
9  *
10  * Written by Christopher Hoover <ch@hpl.hp.com>
11  * Based on fragments of previous driver by Rusell King et al.
12  *
13  * This file is licenced under the GPL.
14  */
15  
16 #include <asm/hardware.h>
17 #include <asm/mach-types.h>
18 #include <asm/arch/assabet.h>
19 #include <asm/arch/badge4.h>
20 #include <asm/hardware/sa1111.h>
21
22 #ifndef CONFIG_SA1111
23 #error "This file is SA-1111 bus glue.  CONFIG_SA1111 must be defined."
24 #endif
25
26 extern int usb_disabled(void);
27
28 /*-------------------------------------------------------------------------*/
29
30 static void sa1111_start_hc(struct sa1111_dev *dev)
31 {
32         unsigned int usb_rst = 0;
33
34         printk(KERN_DEBUG __FILE__ 
35                ": starting SA-1111 OHCI USB Controller\n");
36
37 #ifdef CONFIG_SA1100_BADGE4
38         if (machine_is_badge4()) {
39                 badge4_set_5V(BADGE4_5V_USB, 1);
40         }
41 #endif
42
43         if (machine_is_xp860() ||
44             machine_has_neponset() ||
45             machine_is_pfs168() ||
46             machine_is_badge4())
47                 usb_rst = USB_RESET_PWRSENSELOW | USB_RESET_PWRCTRLLOW;
48
49         /*
50          * Configure the power sense and control lines.  Place the USB
51          * host controller in reset.
52          */
53         sa1111_writel(usb_rst | USB_RESET_FORCEIFRESET | USB_RESET_FORCEHCRESET,
54                       dev->mapbase + SA1111_USB_RESET);
55
56         /*
57          * Now, carefully enable the USB clock, and take
58          * the USB host controller out of reset.
59          */
60         sa1111_enable_device(dev);
61         udelay(11);
62         sa1111_writel(usb_rst, dev->mapbase + SA1111_USB_RESET);
63 }
64
65 static void sa1111_stop_hc(struct sa1111_dev *dev)
66 {
67         unsigned int usb_rst;
68         printk(KERN_DEBUG __FILE__ 
69                ": stopping SA-1111 OHCI USB Controller\n");
70
71         /*
72          * Put the USB host controller into reset.
73          */
74         usb_rst = sa1111_readl(dev->mapbase + SA1111_USB_RESET);
75         sa1111_writel(usb_rst | USB_RESET_FORCEIFRESET | USB_RESET_FORCEHCRESET,
76                       dev->mapbase + SA1111_USB_RESET);
77
78         /*
79          * Stop the USB clock.
80          */
81         sa1111_disable_device(dev);
82
83 #ifdef CONFIG_SA1100_BADGE4
84         if (machine_is_badge4()) {
85                 /* Disable power to the USB bus */
86                 badge4_set_5V(BADGE4_5V_USB, 0);
87         }
88 #endif
89 }
90
91
92 /*-------------------------------------------------------------------------*/
93
94 #if 0
95 static void dump_hci_status(struct usb_hcd *hcd, const char *label)
96 {
97         unsigned long status = sa1111_readl(hcd->regs + SA1111_USB_STATUS);
98
99         dbg ("%s USB_STATUS = { %s%s%s%s%s}", label,
100              ((status & USB_STATUS_IRQHCIRMTWKUP) ? "IRQHCIRMTWKUP " : ""),
101              ((status & USB_STATUS_IRQHCIBUFFACC) ? "IRQHCIBUFFACC " : ""),
102              ((status & USB_STATUS_NIRQHCIM) ? "" : "IRQHCIM "),
103              ((status & USB_STATUS_NHCIMFCLR) ? "" : "HCIMFCLR "),
104              ((status & USB_STATUS_USBPWRSENSE) ? "USBPWRSENSE " : ""));
105 }
106 #endif
107
108 static void usb_hcd_sa1111_hcim_irq (int irq, void *__hcd, struct pt_regs * r)
109 {
110         struct usb_hcd *hcd = __hcd;
111 //      unsigned long status = sa1111_readl(hcd->regs + SA1111_USB_STATUS);
112
113         //dump_hci_status(hcd, "irq");
114
115 #if 0
116         /* may work better this way -- need to investigate further */
117         if (status & USB_STATUS_NIRQHCIM) {
118                 //dbg ("not normal HC interrupt; ignoring");
119                 return;
120         }
121 #endif
122
123         usb_hcd_irq(irq, hcd, r);
124 }
125
126 /*-------------------------------------------------------------------------*/
127
128 void usb_hcd_sa1111_remove (struct usb_hcd *, struct sa1111_dev *);
129
130 /* configure so an HC device and id are always provided */
131 /* always called with process context; sleeping is OK */
132
133
134 /**
135  * usb_hcd_sa1111_probe - initialize SA-1111-based HCDs
136  * Context: !in_interrupt()
137  *
138  * Allocates basic resources for this USB host controller, and
139  * then invokes the start() method for the HCD associated with it
140  * through the hotplug entry's driver_data.
141  *
142  * Store this function in the HCD's struct pci_driver as probe().
143  */
144 int usb_hcd_sa1111_probe (const struct hc_driver *driver,
145                           struct usb_hcd **hcd_out,
146                           struct sa1111_dev *dev)
147 {
148         int retval;
149         struct usb_hcd *hcd = 0;
150
151         if (!request_mem_region(dev->res.start, 
152                                 dev->res.end - dev->res.start + 1, hcd_name)) {
153                 dbg("request_mem_region failed");
154                 return -EBUSY;
155         }
156
157         sa1111_start_hc(dev);
158
159         hcd = driver->hcd_alloc ();
160         if (hcd == NULL){
161                 dbg ("hcd_alloc failed");
162                 retval = -ENOMEM;
163                 goto err1;
164         }
165
166         hcd->driver = (struct hc_driver *) driver;
167         hcd->description = driver->description;
168         hcd->irq = dev->irq[1];
169         hcd->regs = dev->mapbase;
170         hcd->pdev = SA1111_FAKE_PCIDEV;
171         hcd->self.controller = &dev->dev;
172         hcd->controller = hcd->self.controller;
173
174         retval = hcd_buffer_create (hcd);
175         if (retval != 0) {
176                 dbg ("pool alloc fail");
177                 goto err1;
178         }
179
180         retval = request_irq (hcd->irq, usb_hcd_sa1111_hcim_irq, SA_INTERRUPT,
181                               hcd->description, hcd);
182         if (retval != 0) {
183                 dbg("request_irq failed");
184                 retval = -EBUSY;
185                 goto err2;
186         }
187
188         info ("%s (SA-1111) at 0x%p, irq %d\n",
189               hcd->description, hcd->regs, hcd->irq);
190
191         usb_bus_init (&hcd->self);
192         hcd->self.op = &usb_hcd_operations;
193         hcd->self.hcpriv = (void *) hcd;
194         hcd->self.bus_name = "sa1111";
195         hcd->product_desc = "SA-1111 OHCI";
196
197         INIT_LIST_HEAD (&hcd->dev_list);
198
199         usb_register_bus (&hcd->self);
200
201         if ((retval = driver->start (hcd)) < 0) 
202         {
203                 usb_hcd_sa1111_remove(hcd, dev);
204                 return retval;
205         }
206
207         *hcd_out = hcd;
208         return 0;
209
210  err2:
211         hcd_buffer_destroy (hcd);
212         if (hcd)
213                 driver->hcd_free(hcd);
214  err1:
215         sa1111_stop_hc(dev);
216         release_mem_region(dev->res.start, dev->res.end - dev->res.start + 1);
217         return retval;
218 }
219
220
221 /* may be called without controller electrically present */
222 /* may be called with controller, bus, and devices active */
223
224 /**
225  * usb_hcd_sa1111_remove - shutdown processing for SA-1111-based HCDs
226  * @dev: USB Host Controller being removed
227  * Context: !in_interrupt()
228  *
229  * Reverses the effect of usb_hcd_sa1111_probe(), first invoking
230  * the HCD's stop() method.  It is always called from a thread
231  * context, normally "rmmod", "apmd", or something similar.
232  *
233  */
234 void usb_hcd_sa1111_remove (struct usb_hcd *hcd, struct sa1111_dev *dev)
235 {
236         struct usb_device       *hub;
237         void *base;
238
239         info ("remove: %s, state %x", hcd->self.bus_name, hcd->state);
240
241         if (in_interrupt ())
242                 BUG ();
243
244         hub = hcd->self.root_hub;
245         hcd->state = USB_STATE_QUIESCING;
246
247         dbg ("%s: roothub graceful disconnect", hcd->self.bus_name);
248         usb_disconnect (&hub);
249
250         hcd->driver->stop (hcd);
251         hcd->state = USB_STATE_HALT;
252
253         free_irq (hcd->irq, hcd);
254         hcd_buffer_destroy (hcd);
255
256         usb_deregister_bus (&hcd->self);
257         if (atomic_read (&hcd->self.refcnt) != 1)
258                 err ("%s: %s, count != 1", __FUNCTION__, hcd->self.bus_name);
259
260         base = hcd->regs;
261         hcd->driver->hcd_free (hcd);
262
263         sa1111_stop_hc(dev);
264         release_mem_region(dev->res.start, dev->res.end - dev->res.start + 1);
265 }
266
267 /*-------------------------------------------------------------------------*/
268
269 static int __devinit
270 ohci_sa1111_start (struct usb_hcd *hcd)
271 {
272         struct ohci_hcd *ohci = hcd_to_ohci (hcd);
273         int             ret;
274
275         if (hcd->pdev) {
276                 ohci->hcca = pci_alloc_consistent (hcd->pdev,
277                                 sizeof *ohci->hcca, &ohci->hcca_dma);
278                 if (!ohci->hcca)
279                         return -ENOMEM;
280         }
281
282         memset (ohci->hcca, 0, sizeof (struct ohci_hcca));
283         if ((ret = ohci_mem_init (ohci)) < 0) {
284                 ohci_stop (hcd);
285                 return ret;
286         }
287         ohci->regs = hcd->regs;
288
289         if (hc_reset (ohci) < 0) {
290                 ohci_stop (hcd);
291                 return -ENODEV;
292         }
293
294         if (hc_start (ohci) < 0) {
295                 err ("can't start %s", ohci->hcd.self.bus_name);
296                 ohci_stop (hcd);
297                 return -EBUSY;
298         }
299         create_debug_files (ohci);
300
301 #ifdef  DEBUG
302         ohci_dump (ohci, 1);
303 #endif
304         return 0;
305 }
306
307 /*-------------------------------------------------------------------------*/
308
309 static const struct hc_driver ohci_sa1111_hc_driver = {
310         .description =          hcd_name,
311
312         /*
313          * generic hardware linkage
314          */
315         .irq =                  ohci_irq,
316         .flags =                HCD_USB11,
317
318         /*
319          * basic lifecycle operations
320          */
321         .start =                ohci_sa1111_start,
322 #ifdef  CONFIG_PM
323         /* suspend:             ohci_sa1111_suspend,  -- tbd */
324         /* resume:              ohci_sa1111_resume,   -- tbd */
325 #endif
326         .stop =                 ohci_stop,
327
328         /*
329          * memory lifecycle (except per-request)
330          */
331         .hcd_alloc =            ohci_hcd_alloc,
332         .hcd_free =             ohci_hcd_free,
333
334         /*
335          * managing i/o requests and associated device resources
336          */
337         .urb_enqueue =          ohci_urb_enqueue,
338         .urb_dequeue =          ohci_urb_dequeue,
339         .endpoint_disable =     ohci_endpoint_disable,
340
341         /*
342          * scheduling support
343          */
344         .get_frame_number =     ohci_get_frame,
345
346         /*
347          * root hub support
348          */
349         .hub_status_data =      ohci_hub_status_data,
350         .hub_control =          ohci_hub_control,
351 };
352
353 /*-------------------------------------------------------------------------*/
354
355 static int ohci_hcd_sa1111_drv_probe(struct device *_dev)
356 {
357         struct sa1111_dev *dev = SA1111_DEV(_dev);
358         struct usb_hcd *hcd = NULL;
359         int ret;
360
361         if (usb_disabled())
362                 return -ENODEV;
363
364         ret = usb_hcd_sa1111_probe(&ohci_sa1111_hc_driver, &hcd, dev);
365
366         if (ret == 0)
367                 dev->dev.driver_data = hcd;
368
369         return ret;
370 }
371
372 static int ohci_hcd_sa1111_drv_remove(struct device *_dev)
373 {
374         struct sa1111_dev *dev = SA1111_DEV(_dev);
375         struct usb_hcd *hcd = dev->dev.driver_data;
376
377         usb_hcd_sa1111_remove(hcd, dev);
378
379         dev->dev.driver_data = NULL;
380
381         return 0;
382 }
383
384 static int ohci_hcd_sa1111_drv_suspend(struct device *dev, u32 state, u32 level)
385 {
386         return 0;
387 }
388
389 static int ohci_hcd_sa1111_drv_resume(struct device *dev, u32 level)
390 {
391         return 0;
392 }
393
394 static struct sa1111_driver ohci_hcd_sa1111_driver = {
395         .drv = {
396                 .name           = "sa1111-ohci",
397                 .bus            = &sa1111_bus_type,
398                 .probe          = ohci_hcd_sa1111_drv_probe,
399                 .remove         = ohci_hcd_sa1111_drv_remove,
400                 .suspend        = ohci_hcd_sa1111_drv_suspend,
401                 .resume         = ohci_hcd_sa1111_drv_resume,
402         },
403         .devid                  = SA1111_DEVID_USB,
404 };
405
406 static int __init ohci_hcd_sa1111_init (void)
407 {
408         dbg (DRIVER_INFO " (SA-1111)");
409         dbg ("block sizes: ed %d td %d",
410                 sizeof (struct ed), sizeof (struct td));
411
412         return driver_register(&ohci_hcd_sa1111_driver.drv);
413 }
414
415 static void __exit ohci_hcd_sa1111_cleanup (void)
416 {
417         driver_unregister(&ohci_hcd_sa1111_driver.drv);
418 }
419
420 module_init (ohci_hcd_sa1111_init);
421 module_exit (ohci_hcd_sa1111_cleanup);