[PATCH] all HCDs provide root hub suspend/resume methods
[linux-2.6] / 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 extern int usb_disabled(void);
79
80 /*-------------------------------------------------------------------------*/
81
82 static void pxa27x_start_hc(struct platform_device *dev)
83 {
84         pxa_set_cken(CKEN10_USBHOST, 1);
85
86         UHCHR |= UHCHR_FHR;
87         udelay(11);
88         UHCHR &= ~UHCHR_FHR;
89
90         UHCHR |= UHCHR_FSBIR;
91         while (UHCHR & UHCHR_FSBIR)
92                 cpu_relax();
93
94         /* This could be properly abstracted away through the
95            device data the day more machines are supported and
96            their differences can be figured out correctly. */
97         if (machine_is_mainstone()) {
98                 /* setup Port1 GPIO pin. */
99                 pxa_gpio_mode( 88 | GPIO_ALT_FN_1_IN);  /* USBHPWR1 */
100                 pxa_gpio_mode( 89 | GPIO_ALT_FN_2_OUT); /* USBHPEN1 */
101
102                 /* Set the Power Control Polarity Low and Power Sense
103                    Polarity Low to active low. Supply power to USB ports. */
104                 UHCHR = (UHCHR | UHCHR_PCPL | UHCHR_PSPL) &
105                         ~(UHCHR_SSEP1 | UHCHR_SSEP2 | UHCHR_SSEP3 | UHCHR_SSE);
106
107                 pxa27x_ohci_pmm_state = PMM_PERPORT_MODE;
108         }
109
110         UHCHR &= ~UHCHR_SSE;
111
112         UHCHIE = (UHCHIE_UPRIE | UHCHIE_RWIE);
113
114         /* Clear any OTG Pin Hold */
115         if (PSSR & PSSR_OTGPH)
116                 PSSR |= PSSR_OTGPH;
117 }
118
119 static void pxa27x_stop_hc(struct platform_device *dev)
120 {
121         UHCHR |= UHCHR_FHR;
122         udelay(11);
123         UHCHR &= ~UHCHR_FHR;
124
125         UHCCOMS |= 1;
126         udelay(10);
127
128         pxa_set_cken(CKEN10_USBHOST, 0);
129 }
130
131
132 /*-------------------------------------------------------------------------*/
133
134 /* configure so an HC device and id are always provided */
135 /* always called with process context; sleeping is OK */
136
137
138 /**
139  * usb_hcd_pxa27x_probe - initialize pxa27x-based HCDs
140  * Context: !in_interrupt()
141  *
142  * Allocates basic resources for this USB host controller, and
143  * then invokes the start() method for the HCD associated with it
144  * through the hotplug entry's driver_data.
145  *
146  */
147 int usb_hcd_pxa27x_probe (const struct hc_driver *driver,
148                           struct platform_device *dev)
149 {
150         int retval;
151         struct usb_hcd *hcd;
152
153         if (dev->resource[1].flags != IORESOURCE_IRQ) {
154                 pr_debug ("resource[1] is not IORESOURCE_IRQ");
155                 return -ENOMEM;
156         }
157
158         hcd = usb_create_hcd (driver, &dev->dev, "pxa27x");
159         if (!hcd)
160                 return -ENOMEM;
161         hcd->rsrc_start = dev->resource[0].start;
162         hcd->rsrc_len = dev->resource[0].end - dev->resource[0].start + 1;
163
164         if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
165                 pr_debug("request_mem_region failed");
166                 retval = -EBUSY;
167                 goto err1;
168         }
169
170         hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
171         if (!hcd->regs) {
172                 pr_debug("ioremap failed");
173                 retval = -ENOMEM;
174                 goto err2;
175         }
176
177         pxa27x_start_hc(dev);
178
179         /* Select Power Management Mode */
180         pxa27x_ohci_select_pmm(pxa27x_ohci_pmm_state);
181
182         ohci_hcd_init(hcd_to_ohci(hcd));
183
184         retval = usb_add_hcd(hcd, dev->resource[1].start, SA_INTERRUPT);
185         if (retval == 0)
186                 return retval;
187
188         pxa27x_stop_hc(dev);
189         iounmap(hcd->regs);
190  err2:
191         release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
192  err1:
193         usb_put_hcd(hcd);
194         return retval;
195 }
196
197
198 /* may be called without controller electrically present */
199 /* may be called with controller, bus, and devices active */
200
201 /**
202  * usb_hcd_pxa27x_remove - shutdown processing for pxa27x-based HCDs
203  * @dev: USB Host Controller being removed
204  * Context: !in_interrupt()
205  *
206  * Reverses the effect of usb_hcd_pxa27x_probe(), first invoking
207  * the HCD's stop() method.  It is always called from a thread
208  * context, normally "rmmod", "apmd", or something similar.
209  *
210  */
211 void usb_hcd_pxa27x_remove (struct usb_hcd *hcd, struct platform_device *dev)
212 {
213         usb_remove_hcd(hcd);
214         pxa27x_stop_hc(dev);
215         iounmap(hcd->regs);
216         release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
217         usb_put_hcd(hcd);
218 }
219
220 /*-------------------------------------------------------------------------*/
221
222 static int __devinit
223 ohci_pxa27x_start (struct usb_hcd *hcd)
224 {
225         struct ohci_hcd *ohci = hcd_to_ohci (hcd);
226         int             ret;
227
228         ohci_dbg (ohci, "ohci_pxa27x_start, ohci:%p", ohci);
229
230         /* The value of NDP in roothub_a is incorrect on this hardware */
231         ohci->num_ports = 3;
232
233         if ((ret = ohci_init(ohci)) < 0)
234                 return ret;
235
236         if ((ret = ohci_run (ohci)) < 0) {
237                 err ("can't start %s", hcd->self.bus_name);
238                 ohci_stop (hcd);
239                 return ret;
240         }
241
242         return 0;
243 }
244
245 /*-------------------------------------------------------------------------*/
246
247 static const struct hc_driver ohci_pxa27x_hc_driver = {
248         .description =          hcd_name,
249         .product_desc =         "PXA27x OHCI",
250         .hcd_priv_size =        sizeof(struct ohci_hcd),
251
252         /*
253          * generic hardware linkage
254          */
255         .irq =                  ohci_irq,
256         .flags =                HCD_USB11 | HCD_MEMORY,
257
258         /*
259          * basic lifecycle operations
260          */
261         .start =                ohci_pxa27x_start,
262         .stop =                 ohci_stop,
263
264         /*
265          * managing i/o requests and associated device resources
266          */
267         .urb_enqueue =          ohci_urb_enqueue,
268         .urb_dequeue =          ohci_urb_dequeue,
269         .endpoint_disable =     ohci_endpoint_disable,
270
271         /*
272          * scheduling support
273          */
274         .get_frame_number =     ohci_get_frame,
275
276         /*
277          * root hub support
278          */
279         .hub_status_data =      ohci_hub_status_data,
280         .hub_control =          ohci_hub_control,
281 #ifdef  CONFIG_PM
282         .hub_suspend =          ohci_hub_suspend,
283         .hub_resume =           ohci_hub_resume,
284 #endif
285         .start_port_reset =     ohci_start_port_reset,
286 };
287
288 /*-------------------------------------------------------------------------*/
289
290 static int ohci_hcd_pxa27x_drv_probe(struct device *dev)
291 {
292         struct platform_device *pdev = to_platform_device(dev);
293         int ret;
294
295         pr_debug ("In ohci_hcd_pxa27x_drv_probe");
296
297         if (usb_disabled())
298                 return -ENODEV;
299
300         ret = usb_hcd_pxa27x_probe(&ohci_pxa27x_hc_driver, pdev);
301         return ret;
302 }
303
304 static int ohci_hcd_pxa27x_drv_remove(struct device *dev)
305 {
306         struct platform_device *pdev = to_platform_device(dev);
307         struct usb_hcd *hcd = dev_get_drvdata(dev);
308
309         usb_hcd_pxa27x_remove(hcd, pdev);
310         return 0;
311 }
312
313 static int ohci_hcd_pxa27x_drv_suspend(struct device *dev, pm_message_t state)
314 {
315 //      struct platform_device *pdev = to_platform_device(dev);
316 //      struct usb_hcd *hcd = dev_get_drvdata(dev);
317         printk("%s: not implemented yet\n", __FUNCTION__);
318
319         return 0;
320 }
321
322 static int ohci_hcd_pxa27x_drv_resume(struct device *dev)
323 {
324 //      struct platform_device *pdev = to_platform_device(dev);
325 //      struct usb_hcd *hcd = dev_get_drvdata(dev);
326         printk("%s: not implemented yet\n", __FUNCTION__);
327
328         return 0;
329 }
330
331
332 static struct device_driver ohci_hcd_pxa27x_driver = {
333         .name           = "pxa27x-ohci",
334         .bus            = &platform_bus_type,
335         .probe          = ohci_hcd_pxa27x_drv_probe,
336         .remove         = ohci_hcd_pxa27x_drv_remove,
337         .suspend        = ohci_hcd_pxa27x_drv_suspend, 
338         .resume         = ohci_hcd_pxa27x_drv_resume, 
339 };
340
341 static int __init ohci_hcd_pxa27x_init (void)
342 {
343         pr_debug (DRIVER_INFO " (pxa27x)");
344         pr_debug ("block sizes: ed %d td %d\n",
345                 sizeof (struct ed), sizeof (struct td));
346
347         return driver_register(&ohci_hcd_pxa27x_driver);
348 }
349
350 static void __exit ohci_hcd_pxa27x_cleanup (void)
351 {
352         driver_unregister(&ohci_hcd_pxa27x_driver);
353 }
354
355 module_init (ohci_hcd_pxa27x_init);
356 module_exit (ohci_hcd_pxa27x_cleanup);