[ARM] ohci-pxa27x: move OHCI controller specific registers into the driver
[pandora-kernel.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 <linux/signal.h>
24 #include <linux/platform_device.h>
25 #include <linux/clk.h>
26
27 #include <mach/hardware.h>
28 #include <mach/pxa2xx-regs.h> /* FIXME: for PSSR */
29 #include <mach/ohci.h>
30
31 /*
32  * UHC: USB Host Controller (OHCI-like) register definitions
33  */
34 #define UHC_BASE_PHYS   (0x4C000000)
35 #define UHCREV          __REG(0x4C000000) /* UHC HCI Spec Revision */
36 #define UHCHCON         __REG(0x4C000004) /* UHC Host Control Register */
37 #define UHCCOMS         __REG(0x4C000008) /* UHC Command Status Register */
38 #define UHCINTS         __REG(0x4C00000C) /* UHC Interrupt Status Register */
39 #define UHCINTE         __REG(0x4C000010) /* UHC Interrupt Enable */
40 #define UHCINTD         __REG(0x4C000014) /* UHC Interrupt Disable */
41 #define UHCHCCA         __REG(0x4C000018) /* UHC Host Controller Comm. Area */
42 #define UHCPCED         __REG(0x4C00001C) /* UHC Period Current Endpt Descr */
43 #define UHCCHED         __REG(0x4C000020) /* UHC Control Head Endpt Descr */
44 #define UHCCCED         __REG(0x4C000024) /* UHC Control Current Endpt Descr */
45 #define UHCBHED         __REG(0x4C000028) /* UHC Bulk Head Endpt Descr */
46 #define UHCBCED         __REG(0x4C00002C) /* UHC Bulk Current Endpt Descr */
47 #define UHCDHEAD        __REG(0x4C000030) /* UHC Done Head */
48 #define UHCFMI          __REG(0x4C000034) /* UHC Frame Interval */
49 #define UHCFMR          __REG(0x4C000038) /* UHC Frame Remaining */
50 #define UHCFMN          __REG(0x4C00003C) /* UHC Frame Number */
51 #define UHCPERS         __REG(0x4C000040) /* UHC Periodic Start */
52 #define UHCLS           __REG(0x4C000044) /* UHC Low Speed Threshold */
53
54 #define UHCRHDA         __REG(0x4C000048) /* UHC Root Hub Descriptor A */
55 #define UHCRHDA_NOCP    (1 << 12)       /* No over current protection */
56 #define UHCRHDA_OCPM    (1 << 11)       /* Over Current Protection Mode */
57 #define UHCRHDA_POTPGT(x) \
58                         (((x) & 0xff) << 24) /* Power On To Power Good Time */
59
60 #define UHCRHDB         __REG(0x4C00004C) /* UHC Root Hub Descriptor B */
61 #define UHCRHS          __REG(0x4C000050) /* UHC Root Hub Status */
62 #define UHCRHPS1        __REG(0x4C000054) /* UHC Root Hub Port 1 Status */
63 #define UHCRHPS2        __REG(0x4C000058) /* UHC Root Hub Port 2 Status */
64 #define UHCRHPS3        __REG(0x4C00005C) /* UHC Root Hub Port 3 Status */
65
66 #define UHCSTAT         __REG(0x4C000060) /* UHC Status Register */
67 #define UHCSTAT_UPS3    (1 << 16)       /* USB Power Sense Port3 */
68 #define UHCSTAT_SBMAI   (1 << 15)       /* System Bus Master Abort Interrupt*/
69 #define UHCSTAT_SBTAI   (1 << 14)       /* System Bus Target Abort Interrupt*/
70 #define UHCSTAT_UPRI    (1 << 13)       /* USB Port Resume Interrupt */
71 #define UHCSTAT_UPS2    (1 << 12)       /* USB Power Sense Port 2 */
72 #define UHCSTAT_UPS1    (1 << 11)       /* USB Power Sense Port 1 */
73 #define UHCSTAT_HTA     (1 << 10)       /* HCI Target Abort */
74 #define UHCSTAT_HBA     (1 << 8)        /* HCI Buffer Active */
75 #define UHCSTAT_RWUE    (1 << 7)        /* HCI Remote Wake Up Event */
76
77 #define UHCHR           __REG(0x4C000064) /* UHC Reset Register */
78 #define UHCHR_SSEP3     (1 << 11)       /* Sleep Standby Enable for Port3 */
79 #define UHCHR_SSEP2     (1 << 10)       /* Sleep Standby Enable for Port2 */
80 #define UHCHR_SSEP1     (1 << 9)        /* Sleep Standby Enable for Port1 */
81 #define UHCHR_PCPL      (1 << 7)        /* Power control polarity low */
82 #define UHCHR_PSPL      (1 << 6)        /* Power sense polarity low */
83 #define UHCHR_SSE       (1 << 5)        /* Sleep Standby Enable */
84 #define UHCHR_UIT       (1 << 4)        /* USB Interrupt Test */
85 #define UHCHR_SSDC      (1 << 3)        /* Simulation Scale Down Clock */
86 #define UHCHR_CGR       (1 << 2)        /* Clock Generation Reset */
87 #define UHCHR_FHR       (1 << 1)        /* Force Host Controller Reset */
88 #define UHCHR_FSBIR     (1 << 0)        /* Force System Bus Iface Reset */
89
90 #define UHCHIE          __REG(0x4C000068) /* UHC Interrupt Enable Register*/
91 #define UHCHIE_UPS3IE   (1 << 14)       /* Power Sense Port3 IntEn */
92 #define UHCHIE_UPRIE    (1 << 13)       /* Port Resume IntEn */
93 #define UHCHIE_UPS2IE   (1 << 12)       /* Power Sense Port2 IntEn */
94 #define UHCHIE_UPS1IE   (1 << 11)       /* Power Sense Port1 IntEn */
95 #define UHCHIE_TAIE     (1 << 10)       /* HCI Interface Transfer Abort
96                                            Interrupt Enable*/
97 #define UHCHIE_HBAIE    (1 << 8)        /* HCI Buffer Active IntEn */
98 #define UHCHIE_RWIE     (1 << 7)        /* Remote Wake-up IntEn */
99
100 #define UHCHIT          __REG(0x4C00006C) /* UHC Interrupt Test register */
101
102
103 #define PXA_UHC_MAX_PORTNUM    3
104
105 #define UHCRHPS(x)              __REG2( 0x4C000050, (x)<<2 )
106
107 static struct clk *usb_clk;
108
109 /*
110   PMM_NPS_MODE -- PMM Non-power switching mode
111       Ports are powered continuously.
112
113   PMM_GLOBAL_MODE -- PMM global switching mode
114       All ports are powered at the same time.
115
116   PMM_PERPORT_MODE -- PMM per port switching mode
117       Ports are powered individually.
118  */
119 static int pxa27x_ohci_select_pmm( int mode )
120 {
121         switch ( mode ) {
122         case PMM_NPS_MODE:
123                 UHCRHDA |= RH_A_NPS;
124                 break;
125         case PMM_GLOBAL_MODE:
126                 UHCRHDA &= ~(RH_A_NPS & RH_A_PSM);
127                 break;
128         case PMM_PERPORT_MODE:
129                 UHCRHDA &= ~(RH_A_NPS);
130                 UHCRHDA |= RH_A_PSM;
131
132                 /* Set port power control mask bits, only 3 ports. */
133                 UHCRHDB |= (0x7<<17);
134                 break;
135         default:
136                 printk( KERN_ERR
137                         "Invalid mode %d, set to non-power switch mode.\n",
138                         mode );
139
140                 UHCRHDA |= RH_A_NPS;
141         }
142
143         return 0;
144 }
145
146 extern int usb_disabled(void);
147
148 /*-------------------------------------------------------------------------*/
149
150 static inline void pxa27x_setup_hc(struct pxaohci_platform_data *inf)
151 {
152         uint32_t uhchr = UHCHR;
153         uint32_t uhcrhda = UHCRHDA;
154
155         if (inf->flags & ENABLE_PORT1)
156                 uhchr &= ~UHCHR_SSEP1;
157
158         if (inf->flags & ENABLE_PORT2)
159                 uhchr &= ~UHCHR_SSEP2;
160
161         if (inf->flags & ENABLE_PORT3)
162                 uhchr &= ~UHCHR_SSEP3;
163
164         if (inf->flags & POWER_CONTROL_LOW)
165                 uhchr |= UHCHR_PCPL;
166
167         if (inf->flags & POWER_SENSE_LOW)
168                 uhchr |= UHCHR_PSPL;
169
170         if (inf->flags & NO_OC_PROTECTION)
171                 uhcrhda |= UHCRHDA_NOCP;
172
173         if (inf->flags & OC_MODE_PERPORT)
174                 uhcrhda |= UHCRHDA_OCPM;
175
176         if (inf->power_on_delay) {
177                 uhcrhda &= ~UHCRHDA_POTPGT(0xff);
178                 uhcrhda |= UHCRHDA_POTPGT(inf->power_on_delay / 2);
179         }
180
181         UHCHR = uhchr;
182         UHCRHDA = uhcrhda;
183 }
184
185 static int pxa27x_start_hc(struct device *dev)
186 {
187         int retval = 0;
188         struct pxaohci_platform_data *inf;
189
190         inf = dev->platform_data;
191
192         clk_enable(usb_clk);
193
194         UHCHR |= UHCHR_FHR;
195         udelay(11);
196         UHCHR &= ~UHCHR_FHR;
197
198         UHCHR |= UHCHR_FSBIR;
199         while (UHCHR & UHCHR_FSBIR)
200                 cpu_relax();
201
202         pxa27x_setup_hc(inf);
203
204         if (inf->init)
205                 retval = inf->init(dev);
206
207         if (retval < 0)
208                 return retval;
209
210         UHCHR &= ~UHCHR_SSE;
211
212         UHCHIE = (UHCHIE_UPRIE | UHCHIE_RWIE);
213
214         /* Clear any OTG Pin Hold */
215         if (cpu_is_pxa27x() && (PSSR & PSSR_OTGPH))
216                 PSSR |= PSSR_OTGPH;
217
218         return 0;
219 }
220
221 static void pxa27x_stop_hc(struct device *dev)
222 {
223         struct pxaohci_platform_data *inf;
224
225         inf = dev->platform_data;
226
227         if (inf->exit)
228                 inf->exit(dev);
229
230         UHCHR |= UHCHR_FHR;
231         udelay(11);
232         UHCHR &= ~UHCHR_FHR;
233
234         UHCCOMS |= 1;
235         udelay(10);
236
237         clk_disable(usb_clk);
238 }
239
240
241 /*-------------------------------------------------------------------------*/
242
243 /* configure so an HC device and id are always provided */
244 /* always called with process context; sleeping is OK */
245
246
247 /**
248  * usb_hcd_pxa27x_probe - initialize pxa27x-based HCDs
249  * Context: !in_interrupt()
250  *
251  * Allocates basic resources for this USB host controller, and
252  * then invokes the start() method for the HCD associated with it
253  * through the hotplug entry's driver_data.
254  *
255  */
256 int usb_hcd_pxa27x_probe (const struct hc_driver *driver, struct platform_device *pdev)
257 {
258         int retval;
259         struct usb_hcd *hcd;
260         struct pxaohci_platform_data *inf;
261
262         inf = pdev->dev.platform_data;
263
264         if (!inf)
265                 return -ENODEV;
266
267         if (pdev->resource[1].flags != IORESOURCE_IRQ) {
268                 pr_debug ("resource[1] is not IORESOURCE_IRQ");
269                 return -ENOMEM;
270         }
271
272         usb_clk = clk_get(&pdev->dev, "USBCLK");
273         if (IS_ERR(usb_clk))
274                 return PTR_ERR(usb_clk);
275
276         hcd = usb_create_hcd (driver, &pdev->dev, "pxa27x");
277         if (!hcd)
278                 return -ENOMEM;
279         hcd->rsrc_start = pdev->resource[0].start;
280         hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;
281
282         if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
283                 pr_debug("request_mem_region failed");
284                 retval = -EBUSY;
285                 goto err1;
286         }
287
288         hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
289         if (!hcd->regs) {
290                 pr_debug("ioremap failed");
291                 retval = -ENOMEM;
292                 goto err2;
293         }
294
295         if ((retval = pxa27x_start_hc(&pdev->dev)) < 0) {
296                 pr_debug("pxa27x_start_hc failed");
297                 goto err3;
298         }
299
300         /* Select Power Management Mode */
301         pxa27x_ohci_select_pmm(inf->port_mode);
302
303         if (inf->power_budget)
304                 hcd->power_budget = inf->power_budget;
305
306         ohci_hcd_init(hcd_to_ohci(hcd));
307
308         retval = usb_add_hcd(hcd, pdev->resource[1].start, IRQF_DISABLED);
309         if (retval == 0)
310                 return retval;
311
312         pxa27x_stop_hc(&pdev->dev);
313  err3:
314         iounmap(hcd->regs);
315  err2:
316         release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
317  err1:
318         usb_put_hcd(hcd);
319         clk_put(usb_clk);
320         return retval;
321 }
322
323
324 /* may be called without controller electrically present */
325 /* may be called with controller, bus, and devices active */
326
327 /**
328  * usb_hcd_pxa27x_remove - shutdown processing for pxa27x-based HCDs
329  * @dev: USB Host Controller being removed
330  * Context: !in_interrupt()
331  *
332  * Reverses the effect of usb_hcd_pxa27x_probe(), first invoking
333  * the HCD's stop() method.  It is always called from a thread
334  * context, normally "rmmod", "apmd", or something similar.
335  *
336  */
337 void usb_hcd_pxa27x_remove (struct usb_hcd *hcd, struct platform_device *pdev)
338 {
339         usb_remove_hcd(hcd);
340         pxa27x_stop_hc(&pdev->dev);
341         iounmap(hcd->regs);
342         release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
343         usb_put_hcd(hcd);
344         clk_put(usb_clk);
345 }
346
347 /*-------------------------------------------------------------------------*/
348
349 static int __devinit
350 ohci_pxa27x_start (struct usb_hcd *hcd)
351 {
352         struct ohci_hcd *ohci = hcd_to_ohci (hcd);
353         int             ret;
354
355         ohci_dbg (ohci, "ohci_pxa27x_start, ohci:%p", ohci);
356
357         /* The value of NDP in roothub_a is incorrect on this hardware */
358         ohci->num_ports = 3;
359
360         if ((ret = ohci_init(ohci)) < 0)
361                 return ret;
362
363         if ((ret = ohci_run (ohci)) < 0) {
364                 err ("can't start %s", hcd->self.bus_name);
365                 ohci_stop (hcd);
366                 return ret;
367         }
368
369         return 0;
370 }
371
372 /*-------------------------------------------------------------------------*/
373
374 static const struct hc_driver ohci_pxa27x_hc_driver = {
375         .description =          hcd_name,
376         .product_desc =         "PXA27x OHCI",
377         .hcd_priv_size =        sizeof(struct ohci_hcd),
378
379         /*
380          * generic hardware linkage
381          */
382         .irq =                  ohci_irq,
383         .flags =                HCD_USB11 | HCD_MEMORY,
384
385         /*
386          * basic lifecycle operations
387          */
388         .start =                ohci_pxa27x_start,
389         .stop =                 ohci_stop,
390         .shutdown =             ohci_shutdown,
391
392         /*
393          * managing i/o requests and associated device resources
394          */
395         .urb_enqueue =          ohci_urb_enqueue,
396         .urb_dequeue =          ohci_urb_dequeue,
397         .endpoint_disable =     ohci_endpoint_disable,
398
399         /*
400          * scheduling support
401          */
402         .get_frame_number =     ohci_get_frame,
403
404         /*
405          * root hub support
406          */
407         .hub_status_data =      ohci_hub_status_data,
408         .hub_control =          ohci_hub_control,
409 #ifdef  CONFIG_PM
410         .bus_suspend =          ohci_bus_suspend,
411         .bus_resume =           ohci_bus_resume,
412 #endif
413         .start_port_reset =     ohci_start_port_reset,
414 };
415
416 /*-------------------------------------------------------------------------*/
417
418 static int ohci_hcd_pxa27x_drv_probe(struct platform_device *pdev)
419 {
420         pr_debug ("In ohci_hcd_pxa27x_drv_probe");
421
422         if (usb_disabled())
423                 return -ENODEV;
424
425         return usb_hcd_pxa27x_probe(&ohci_pxa27x_hc_driver, pdev);
426 }
427
428 static int ohci_hcd_pxa27x_drv_remove(struct platform_device *pdev)
429 {
430         struct usb_hcd *hcd = platform_get_drvdata(pdev);
431
432         usb_hcd_pxa27x_remove(hcd, pdev);
433         platform_set_drvdata(pdev, NULL);
434         return 0;
435 }
436
437 #ifdef  CONFIG_PM
438 static int ohci_hcd_pxa27x_drv_suspend(struct platform_device *pdev, pm_message_t state)
439 {
440         struct usb_hcd *hcd = platform_get_drvdata(pdev);
441         struct ohci_hcd *ohci = hcd_to_ohci(hcd);
442
443         if (time_before(jiffies, ohci->next_statechange))
444                 msleep(5);
445         ohci->next_statechange = jiffies;
446
447         pxa27x_stop_hc(&pdev->dev);
448         hcd->state = HC_STATE_SUSPENDED;
449
450         return 0;
451 }
452
453 static int ohci_hcd_pxa27x_drv_resume(struct platform_device *pdev)
454 {
455         struct usb_hcd *hcd = platform_get_drvdata(pdev);
456         struct ohci_hcd *ohci = hcd_to_ohci(hcd);
457         int status;
458
459         if (time_before(jiffies, ohci->next_statechange))
460                 msleep(5);
461         ohci->next_statechange = jiffies;
462
463         if ((status = pxa27x_start_hc(&pdev->dev)) < 0)
464                 return status;
465
466         ohci_finish_controller_resume(hcd);
467         return 0;
468 }
469 #endif
470
471 /* work with hotplug and coldplug */
472 MODULE_ALIAS("platform:pxa27x-ohci");
473
474 static struct platform_driver ohci_hcd_pxa27x_driver = {
475         .probe          = ohci_hcd_pxa27x_drv_probe,
476         .remove         = ohci_hcd_pxa27x_drv_remove,
477         .shutdown       = usb_hcd_platform_shutdown,
478 #ifdef CONFIG_PM
479         .suspend        = ohci_hcd_pxa27x_drv_suspend,
480         .resume         = ohci_hcd_pxa27x_drv_resume,
481 #endif
482         .driver         = {
483                 .name   = "pxa27x-ohci",
484                 .owner  = THIS_MODULE,
485         },
486 };
487