vserver 1.9.5.x5
[linux-2.6.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 irqreturn_t 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          * SA1111 seems to re-assert its interrupt immediately
127          * after processing an interrupt.  Always return IRQ_HANDLED.
128          */
129         return IRQ_HANDLED;
130 }
131
132 /*-------------------------------------------------------------------------*/
133
134 void usb_hcd_sa1111_remove (struct usb_hcd *, struct sa1111_dev *);
135
136 /* configure so an HC device and id are always provided */
137 /* always called with process context; sleeping is OK */
138
139
140 /**
141  * usb_hcd_sa1111_probe - initialize SA-1111-based HCDs
142  * Context: !in_interrupt()
143  *
144  * Allocates basic resources for this USB host controller, and
145  * then invokes the start() method for the HCD associated with it
146  * through the hotplug entry's driver_data.
147  *
148  * Store this function in the HCD's struct pci_driver as probe().
149  */
150 int usb_hcd_sa1111_probe (const struct hc_driver *driver,
151                           struct usb_hcd **hcd_out,
152                           struct sa1111_dev *dev)
153 {
154         int retval;
155         struct usb_hcd *hcd = 0;
156
157         if (!request_mem_region(dev->res.start, 
158                                 dev->res.end - dev->res.start + 1, hcd_name)) {
159                 dbg("request_mem_region failed");
160                 return -EBUSY;
161         }
162
163         sa1111_start_hc(dev);
164
165         hcd = usb_create_hcd (driver);
166         if (hcd == NULL){
167                 dbg ("hcd_alloc failed");
168                 retval = -ENOMEM;
169                 goto err1;
170         }
171         ohci_hcd_init(hcd_to_ohci(hcd));
172
173         hcd->irq = dev->irq[1];
174         hcd->regs = dev->mapbase;
175         hcd->self.controller = &dev->dev;
176
177         retval = hcd_buffer_create (hcd);
178         if (retval != 0) {
179                 dbg ("pool alloc fail");
180                 goto err2;
181         }
182
183         retval = request_irq (hcd->irq, usb_hcd_sa1111_hcim_irq, SA_INTERRUPT,
184                               hcd->driver->description, hcd);
185         if (retval != 0) {
186                 dbg("request_irq failed");
187                 retval = -EBUSY;
188                 goto err3;
189         }
190
191         info ("%s (SA-1111) at 0x%p, irq %d\n",
192                 hcd->driver->description, hcd->regs, hcd->irq);
193
194         hcd->self.bus_name = "sa1111";
195         usb_register_bus (&hcd->self);
196
197         if ((retval = driver->start (hcd)) < 0) 
198         {
199                 usb_hcd_sa1111_remove(hcd, dev);
200                 return retval;
201         }
202
203         *hcd_out = hcd;
204         return 0;
205
206  err3:
207         hcd_buffer_destroy (hcd);
208  err2:
209         usb_put_hcd(hcd);
210  err1:
211         sa1111_stop_hc(dev);
212         release_mem_region(dev->res.start, dev->res.end - dev->res.start + 1);
213         return retval;
214 }
215
216
217 /* may be called without controller electrically present */
218 /* may be called with controller, bus, and devices active */
219
220 /**
221  * usb_hcd_sa1111_remove - shutdown processing for SA-1111-based HCDs
222  * @dev: USB Host Controller being removed
223  * Context: !in_interrupt()
224  *
225  * Reverses the effect of usb_hcd_sa1111_probe(), first invoking
226  * the HCD's stop() method.  It is always called from a thread
227  * context, normally "rmmod", "apmd", or something similar.
228  *
229  */
230 void usb_hcd_sa1111_remove (struct usb_hcd *hcd, struct sa1111_dev *dev)
231 {
232         info ("remove: %s, state %x", hcd->self.bus_name, hcd->state);
233
234         if (in_interrupt ())
235                 BUG ();
236
237         hcd->state = USB_STATE_QUIESCING;
238
239         dbg ("%s: roothub graceful disconnect", hcd->self.bus_name);
240         usb_disconnect (&hcd->self.root_hub);
241
242         hcd->driver->stop (hcd);
243         hcd->state = USB_STATE_HALT;
244
245         free_irq (hcd->irq, hcd);
246         hcd_buffer_destroy (hcd);
247
248         usb_deregister_bus (&hcd->self);
249
250         sa1111_stop_hc(dev);
251         release_mem_region(dev->res.start, dev->res.end - dev->res.start + 1);
252 }
253
254 /*-------------------------------------------------------------------------*/
255
256 static int __devinit
257 ohci_sa1111_start (struct usb_hcd *hcd)
258 {
259         struct ohci_hcd *ohci = hcd_to_ohci (hcd);
260         int             ret;
261
262         if ((ret = ohci_init(ohci)) < 0)
263                 return ret;
264
265         if ((ret = ohci_run (ohci)) < 0) {
266                 err ("can't start %s", hcd->self.bus_name);
267                 ohci_stop (hcd);
268                 return ret;
269         }
270         return 0;
271 }
272
273 /*-------------------------------------------------------------------------*/
274
275 static const struct hc_driver ohci_sa1111_hc_driver = {
276         .description =          hcd_name,
277         .product_desc =         "SA-1111 OHCI",
278         .hcd_priv_size =        sizeof(struct ohci_hcd),
279
280         /*
281          * generic hardware linkage
282          */
283         .irq =                  ohci_irq,
284         .flags =                HCD_USB11,
285
286         /*
287          * basic lifecycle operations
288          */
289         .start =                ohci_sa1111_start,
290 #ifdef  CONFIG_PM
291         /* suspend:             ohci_sa1111_suspend,  -- tbd */
292         /* resume:              ohci_sa1111_resume,   -- tbd */
293 #endif
294         .stop =                 ohci_stop,
295
296         /*
297          * memory lifecycle (except per-request)
298          */
299         .hcd_alloc =            ohci_hcd_alloc,
300
301         /*
302          * managing i/o requests and associated device resources
303          */
304         .urb_enqueue =          ohci_urb_enqueue,
305         .urb_dequeue =          ohci_urb_dequeue,
306         .endpoint_disable =     ohci_endpoint_disable,
307
308         /*
309          * scheduling support
310          */
311         .get_frame_number =     ohci_get_frame,
312
313         /*
314          * root hub support
315          */
316         .hub_status_data =      ohci_hub_status_data,
317         .hub_control =          ohci_hub_control,
318 #ifdef  CONFIG_USB_SUSPEND
319         .hub_suspend =          ohci_hub_suspend,
320         .hub_resume =           ohci_hub_resume,
321 #endif
322 };
323
324 /*-------------------------------------------------------------------------*/
325
326 static int ohci_hcd_sa1111_drv_probe(struct sa1111_dev *dev)
327 {
328         struct usb_hcd *hcd = NULL;
329         int ret;
330
331         if (usb_disabled())
332                 return -ENODEV;
333
334         ret = usb_hcd_sa1111_probe(&ohci_sa1111_hc_driver, &hcd, dev);
335
336         if (ret == 0)
337                 sa1111_set_drvdata(dev, hcd);
338
339         return ret;
340 }
341
342 static int ohci_hcd_sa1111_drv_remove(struct sa1111_dev *dev)
343 {
344         struct usb_hcd *hcd = sa1111_get_drvdata(dev);
345
346         usb_hcd_sa1111_remove(hcd, dev);
347
348         sa1111_set_drvdata(dev, NULL);
349
350         return 0;
351 }
352
353 static struct sa1111_driver ohci_hcd_sa1111_driver = {
354         .drv = {
355                 .name   = "sa1111-ohci",
356         },
357         .devid          = SA1111_DEVID_USB,
358         .probe          = ohci_hcd_sa1111_drv_probe,
359         .remove         = ohci_hcd_sa1111_drv_remove,
360 };
361
362 static int __init ohci_hcd_sa1111_init (void)
363 {
364         dbg (DRIVER_INFO " (SA-1111)");
365         dbg ("block sizes: ed %d td %d",
366                 sizeof (struct ed), sizeof (struct td));
367
368         return sa1111_driver_register(&ohci_hcd_sa1111_driver);
369 }
370
371 static void __exit ohci_hcd_sa1111_cleanup (void)
372 {
373         sa1111_driver_unregister(&ohci_hcd_sa1111_driver);
374 }
375
376 module_init (ohci_hcd_sa1111_init);
377 module_exit (ohci_hcd_sa1111_cleanup);