vserver 1.9.5.x5
[linux-2.6.git] / drivers / usb / host / ohci-pxa27x.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  * Bus Glue for pxa27x
9  *
10  * Written by Christopher Hoover <ch@hpl.hp.com>
11  * Based on fragments of previous driver by Russell King et al.
12  *
13  * Modified for LH7A404 from ohci-sa1111.c
14  *  by Durgesh Pattamatta <pattamattad@sharpsec.com>
15  *
16  * Modified for pxa27x from ohci-lh7a404.c
17  *  by Nick Bane <nick@cecomputing.co.uk> 26-8-2004
18  *
19  * This file is licenced under the GPL.
20  */
21
22 #include <linux/device.h>
23 #include <asm/mach-types.h>
24 #include <asm/hardware.h>
25 #include <asm/arch/pxa-regs.h>
26
27
28 #define PMM_NPS_MODE           1
29 #define PMM_GLOBAL_MODE        2
30 #define PMM_PERPORT_MODE       3
31
32 #define PXA_UHC_MAX_PORTNUM    3
33
34 #define UHCRHPS(x)              __REG2( 0x4C000050, (x)<<2 )
35
36 static int pxa27x_ohci_pmm_state;
37
38 /*
39   PMM_NPS_MODE -- PMM Non-power switching mode
40       Ports are powered continuously.
41
42   PMM_GLOBAL_MODE -- PMM global switching mode
43       All ports are powered at the same time.
44
45   PMM_PERPORT_MODE -- PMM per port switching mode
46       Ports are powered individually.
47  */
48 static int pxa27x_ohci_select_pmm( int mode )
49 {
50         pxa27x_ohci_pmm_state = mode;
51
52         switch ( mode ) {
53         case PMM_NPS_MODE:
54                 UHCRHDA |= RH_A_NPS;
55                 break; 
56         case PMM_GLOBAL_MODE:
57                 UHCRHDA &= ~(RH_A_NPS & RH_A_PSM);
58                 break;
59         case PMM_PERPORT_MODE:
60                 UHCRHDA &= ~(RH_A_NPS);
61                 UHCRHDA |= RH_A_PSM;
62
63                 /* Set port power control mask bits, only 3 ports. */
64                 UHCRHDB |= (0x7<<17);
65                 break;
66         default:
67                 printk( KERN_ERR
68                         "Invalid mode %d, set to non-power switch mode.\n", 
69                         mode );
70
71                 pxa27x_ohci_pmm_state = PMM_NPS_MODE;
72                 UHCRHDA |= RH_A_NPS;
73         }
74
75         return 0;
76 }
77
78 /*
79   If you select PMM_PERPORT_MODE, you should set the port power
80  */
81 static int pxa27x_ohci_set_port_power( int port )
82 {
83         if ( (pxa27x_ohci_pmm_state==PMM_PERPORT_MODE)
84              && (port>0) && (port<PXA_UHC_MAX_PORTNUM) ) {
85                 UHCRHPS(port) |= 0x100;
86                 return 0;
87         }
88         return -1;
89 }
90
91 /*
92   If you select PMM_PERPORT_MODE, you should set the port power
93  */
94 static int pxa27x_ohci_clear_port_power( int port )
95 {
96         if ( (pxa27x_ohci_pmm_state==PMM_PERPORT_MODE) 
97              && (port>0) && (port<PXA_UHC_MAX_PORTNUM) ) {
98                 UHCRHPS(port) |= 0x200;
99                 return 0;
100         }
101          
102         return -1;
103 }
104
105 extern int usb_disabled(void);
106
107 /*-------------------------------------------------------------------------*/
108
109 static void pxa27x_start_hc(struct platform_device *dev)
110 {
111         pxa_set_cken(CKEN10_USBHOST, 1);
112
113         UHCHR |= UHCHR_FHR;
114         udelay(11);
115         UHCHR &= ~UHCHR_FHR;
116
117         UHCHR |= UHCHR_FSBIR;
118         while (UHCHR & UHCHR_FSBIR)
119                 cpu_relax();
120
121         /* This could be properly abstracted away through the
122            device data the day more machines are supported and
123            their differences can be figured out correctly. */
124         if (machine_is_mainstone()) {
125                 /* setup Port1 GPIO pin. */
126                 pxa_gpio_mode( 88 | GPIO_ALT_FN_1_IN);  /* USBHPWR1 */
127                 pxa_gpio_mode( 89 | GPIO_ALT_FN_2_OUT); /* USBHPEN1 */
128
129                 /* Set the Power Control Polarity Low and Power Sense
130                    Polarity Low to active low. Supply power to USB ports. */
131                 UHCHR = (UHCHR | UHCHR_PCPL | UHCHR_PSPL) &
132                         ~(UHCHR_SSEP1 | UHCHR_SSEP2 | UHCHR_SSEP3 | UHCHR_SSE);
133         }
134
135         UHCHR &= ~UHCHR_SSE;
136
137         UHCHIE = (UHCHIE_UPRIE | UHCHIE_RWIE);
138 }
139
140 static void pxa27x_stop_hc(struct platform_device *dev)
141 {
142         UHCHR |= UHCHR_FHR;
143         udelay(11);
144         UHCHR &= ~UHCHR_FHR;
145
146         UHCCOMS |= 1;
147         udelay(10);
148
149         pxa_set_cken(CKEN10_USBHOST, 0);
150 }
151
152
153 /*-------------------------------------------------------------------------*/
154
155 void usb_hcd_pxa27x_remove (struct usb_hcd *, struct platform_device *);
156
157 /* configure so an HC device and id are always provided */
158 /* always called with process context; sleeping is OK */
159
160
161 /**
162  * usb_hcd_pxa27x_probe - initialize pxa27x-based HCDs
163  * Context: !in_interrupt()
164  *
165  * Allocates basic resources for this USB host controller, and
166  * then invokes the start() method for the HCD associated with it
167  * through the hotplug entry's driver_data.
168  *
169  */
170 int usb_hcd_pxa27x_probe (const struct hc_driver *driver,
171                           struct usb_hcd **hcd_out,
172                           struct platform_device *dev)
173 {
174         int retval;
175         struct usb_hcd *hcd = 0;
176
177         unsigned int *addr = NULL;
178
179         if (!request_mem_region(dev->resource[0].start,
180                                 dev->resource[0].end
181                                 - dev->resource[0].start + 1, hcd_name)) {
182                 pr_debug("request_mem_region failed");
183                 return -EBUSY;
184         }
185
186         pxa27x_start_hc(dev);
187
188         /* Select Power Management Mode */
189         pxa27x_ohci_select_pmm( PMM_PERPORT_MODE );
190
191         /* If choosing PMM_PERPORT_MODE, we should set the port power before we use it. */
192         if (pxa27x_ohci_set_port_power(1) < 0)
193                 printk(KERN_ERR "Setting port 1 power failed.\n");
194
195         if (pxa27x_ohci_clear_port_power(2) < 0)
196                 printk(KERN_ERR "Setting port 2 power failed.\n");
197
198         if (pxa27x_ohci_clear_port_power(3) < 0)
199                 printk(KERN_ERR "Setting port 3 power failed.\n");
200
201         addr = ioremap(dev->resource[0].start,
202                        dev->resource[0].end - dev->resource[0].start + 1);
203         if (!addr) {
204                 pr_debug("ioremap failed");
205                 retval = -ENOMEM;
206                 goto err1;
207         }
208
209         if(dev->resource[1].flags != IORESOURCE_IRQ){
210                 pr_debug ("resource[1] is not IORESOURCE_IRQ");
211                 retval = -ENOMEM;
212                 goto err1;
213         }
214
215         hcd = usb_create_hcd (driver);
216         if (hcd == NULL){
217                 pr_debug ("hcd_alloc failed");
218                 retval = -ENOMEM;
219                 goto err1;
220         }
221         ohci_hcd_init(hcd_to_ohci(hcd));
222
223         hcd->irq = dev->resource[1].start;
224         hcd->regs = addr;
225         hcd->self.controller = &dev->dev;
226
227         retval = hcd_buffer_create (hcd);
228         if (retval != 0) {
229                 pr_debug ("pool alloc fail");
230                 goto err2;
231         }
232
233         retval = request_irq (hcd->irq, usb_hcd_irq, SA_INTERRUPT,
234                               hcd->driver->description, hcd);
235         if (retval != 0) {
236                 pr_debug("request_irq(%d) failed with retval %d\n",hcd->irq,retval);
237                 retval = -EBUSY;
238                 goto err3;
239         }
240
241         pr_debug ("%s (pxa27x) at 0x%p, irq %d",
242                 hcd->driver->description, hcd->regs, hcd->irq);
243
244         hcd->self.bus_name = "pxa27x";
245         usb_register_bus (&hcd->self);
246
247         if ((retval = driver->start (hcd)) < 0) {
248                 usb_hcd_pxa27x_remove(hcd, dev);
249                 return retval;
250         }
251
252         *hcd_out = hcd;
253         return 0;
254
255  err3:
256         hcd_buffer_destroy (hcd);
257  err2:
258         usb_put_hcd(hcd);
259  err1:
260         pxa27x_stop_hc(dev);
261         release_mem_region(dev->resource[0].start,
262                                 dev->resource[0].end
263                            - dev->resource[0].start + 1);
264         return retval;
265 }
266
267
268 /* may be called without controller electrically present */
269 /* may be called with controller, bus, and devices active */
270
271 /**
272  * usb_hcd_pxa27x_remove - shutdown processing for pxa27x-based HCDs
273  * @dev: USB Host Controller being removed
274  * Context: !in_interrupt()
275  *
276  * Reverses the effect of usb_hcd_pxa27x_probe(), first invoking
277  * the HCD's stop() method.  It is always called from a thread
278  * context, normally "rmmod", "apmd", or something similar.
279  *
280  */
281 void usb_hcd_pxa27x_remove (struct usb_hcd *hcd, struct platform_device *dev)
282 {
283         pr_debug ("remove: %s, state %x", hcd->self.bus_name, hcd->state);
284
285         if (in_interrupt ())
286                 BUG ();
287
288         hcd->state = USB_STATE_QUIESCING;
289
290         pr_debug ("%s: roothub graceful disconnect", hcd->self.bus_name);
291         usb_disconnect (&hcd->self.root_hub);
292
293         hcd->driver->stop (hcd);
294         hcd->state = USB_STATE_HALT;
295
296         free_irq (hcd->irq, hcd);
297         hcd_buffer_destroy (hcd);
298
299         usb_deregister_bus (&hcd->self);
300
301         pxa27x_stop_hc(dev);
302         release_mem_region(dev->resource[0].start,
303                            dev->resource[0].end - dev->resource[0].start + 1);
304 }
305
306 /*-------------------------------------------------------------------------*/
307
308 static int __devinit
309 ohci_pxa27x_start (struct usb_hcd *hcd)
310 {
311         struct ohci_hcd *ohci = hcd_to_ohci (hcd);
312         int             ret;
313
314         ohci_dbg (ohci, "ohci_pxa27x_start, ohci:%p", ohci);
315
316         if ((ret = ohci_init(ohci)) < 0)
317                 return ret;
318
319         if ((ret = ohci_run (ohci)) < 0) {
320                 err ("can't start %s", hcd->self.bus_name);
321                 ohci_stop (hcd);
322                 return ret;
323         }
324
325         return 0;
326 }
327
328 /*-------------------------------------------------------------------------*/
329
330 static const struct hc_driver ohci_pxa27x_hc_driver = {
331         .description =          hcd_name,
332         .product_desc =         "PXA27x OHCI",
333         .hcd_priv_size =        sizeof(struct ohci_hcd),
334
335         /*
336          * generic hardware linkage
337          */
338         .irq =                  ohci_irq,
339         .flags =                HCD_USB11,
340
341         /*
342          * basic lifecycle operations
343          */
344         .start =                ohci_pxa27x_start,
345         .stop =                 ohci_stop,
346
347         /*
348          * managing i/o requests and associated device resources
349          */
350         .urb_enqueue =          ohci_urb_enqueue,
351         .urb_dequeue =          ohci_urb_dequeue,
352         .endpoint_disable =     ohci_endpoint_disable,
353
354         /*
355          * scheduling support
356          */
357         .get_frame_number =     ohci_get_frame,
358
359         /*
360          * root hub support
361          */
362         .hub_status_data =      ohci_hub_status_data,
363         .hub_control =          ohci_hub_control,
364 #ifdef  CONFIG_USB_SUSPEND
365         .hub_suspend =          ohci_hub_suspend,
366         .hub_resume =           ohci_hub_resume,
367 #endif
368 };
369
370 /*-------------------------------------------------------------------------*/
371
372 static int ohci_hcd_pxa27x_drv_probe(struct device *dev)
373 {
374         struct platform_device *pdev = to_platform_device(dev);
375         struct usb_hcd *hcd = NULL;
376         int ret;
377
378         pr_debug ("In ohci_hcd_pxa27x_drv_probe");
379
380         if (usb_disabled())
381                 return -ENODEV;
382
383         ret = usb_hcd_pxa27x_probe(&ohci_pxa27x_hc_driver, &hcd, pdev);
384
385         if (ret == 0)
386                 dev_set_drvdata(dev, hcd);
387
388         return ret;
389 }
390
391 static int ohci_hcd_pxa27x_drv_remove(struct device *dev)
392 {
393         struct platform_device *pdev = to_platform_device(dev);
394         struct usb_hcd *hcd = dev_get_drvdata(dev);
395
396         usb_hcd_pxa27x_remove(hcd, pdev);
397         dev_set_drvdata(dev, NULL);
398         return 0;
399 }
400
401 static int ohci_hcd_pxa27x_drv_suspend(struct device *dev, u32 state, u32 level)
402 {
403 //      struct platform_device *pdev = to_platform_device(dev);
404 //      struct usb_hcd *hcd = dev_get_drvdata(dev);
405         printk("%s: not implemented yet\n", __FUNCTION__);
406
407         return 0;
408 }
409
410 static int ohci_hcd_pxa27x_drv_resume(struct device *dev, u32 state)
411 {
412 //      struct platform_device *pdev = to_platform_device(dev);
413 //      struct usb_hcd *hcd = dev_get_drvdata(dev);
414         printk("%s: not implemented yet\n", __FUNCTION__);
415
416         return 0;
417 }
418
419
420 static struct device_driver ohci_hcd_pxa27x_driver = {
421         .name           = "pxa27x-ohci",
422         .bus            = &platform_bus_type,
423         .probe          = ohci_hcd_pxa27x_drv_probe,
424         .remove         = ohci_hcd_pxa27x_drv_remove,
425         .suspend        = ohci_hcd_pxa27x_drv_suspend, 
426         .resume         = ohci_hcd_pxa27x_drv_resume, 
427 };
428
429 static int __init ohci_hcd_pxa27x_init (void)
430 {
431         pr_debug (DRIVER_INFO " (pxa27x)");
432         pr_debug ("block sizes: ed %d td %d\n",
433                 sizeof (struct ed), sizeof (struct td));
434
435         return driver_register(&ohci_hcd_pxa27x_driver);
436 }
437
438 static void __exit ohci_hcd_pxa27x_cleanup (void)
439 {
440         driver_unregister(&ohci_hcd_pxa27x_driver);
441 }
442
443 module_init (ohci_hcd_pxa27x_init);
444 module_exit (ohci_hcd_pxa27x_cleanup);