vserver 1.9.3
[linux-2.6.git] / drivers / usb / host / ohci-omap.c
1 /*
2  * OHCI HCD (Host Controller Driver) for USB.
3  *
4  * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5  * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
6  * (C) Copyright 2002 Hewlett-Packard Company
7  * 
8  * OMAP Bus Glue
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 OMAP from ohci-sa1111.c by Tony Lindgren <tony@atomide.com>
14  * Based on the 2.4 OMAP OHCI driver originally done by MontaVista Software Inc.
15  *
16  * This file is licenced under the GPL.
17  */
18
19 #include <asm/hardware.h>
20 #include <asm/io.h>
21 #include <asm/mach-types.h>
22
23 #include <asm/arch/hardware.h>
24 #include <asm/arch/mux.h>
25 #include <asm/arch/irqs.h>
26 #include <asm/arch/gpio.h>
27 #include <asm/arch/fpga.h>
28 #include <asm/arch/usb.h>
29
30 #include "ohci-omap.h"
31
32 #ifndef CONFIG_ARCH_OMAP
33 #error "This file is OMAP bus glue.  CONFIG_OMAP must be defined."
34 #endif
35
36 extern int usb_disabled(void);
37 extern int ocpi_enable(void);
38
39 /*
40  * OHCI clock initialization for OMAP-1510 and 1610
41  */
42 static int omap_ohci_clock_power(int on)
43 {
44         if (on) {
45                 if (cpu_is_omap1510()) {
46                         /* Use DPLL, not APLL */
47                         omap_writel(omap_readl(ULPD_APLL_CTRL) & ~APLL_NDPLL_SWITCH,
48                                ULPD_APLL_CTRL);
49
50                         /* Enable DPLL */
51                         omap_writel(omap_readl(ULPD_DPLL_CTRL) | DPLL_PLL_ENABLE,
52                                ULPD_DPLL_CTRL);
53
54                         /* Software request for USB 48MHz clock */
55                         omap_writel(omap_readl(ULPD_SOFT_REQ) | SOFT_REQ_REG_REQ,
56                                ULPD_SOFT_REQ);
57
58                         while (!(omap_readl(ULPD_DPLL_CTRL) & DPLL_LOCK));
59                 }
60
61                 if (cpu_is_omap16xx()) {
62                         /* Enable OHCI */
63                         omap_writel(omap_readl(ULPD_SOFT_REQ) | SOFT_USB_OTG_REQ,
64                                 ULPD_SOFT_REQ);
65
66                         /* USB host clock request if not using OTG */
67                         omap_writel(omap_readl(ULPD_SOFT_REQ) | SOFT_USB_REQ,
68                                 ULPD_SOFT_REQ);
69
70                         omap_writel(omap_readl(ULPD_STATUS_REQ) | USB_HOST_DPLL_REQ,
71                              ULPD_STATUS_REQ);
72                 }
73
74                 /* Enable 48MHz clock to USB */
75                 omap_writel(omap_readl(ULPD_CLOCK_CTRL) | USB_MCLK_EN,
76                        ULPD_CLOCK_CTRL);
77
78                 omap_writel(omap_readl(ARM_IDLECT2) | (1 << EN_LBFREECK) | (1 << EN_LBCK),
79                        ARM_IDLECT2);
80
81                 omap_writel(omap_readl(MOD_CONF_CTRL_0) | USB_HOST_HHC_UHOST_EN,
82                        MOD_CONF_CTRL_0);
83         } else {
84                 /* Disable 48MHz clock to USB */
85                 omap_writel(omap_readl(ULPD_CLOCK_CTRL) & ~USB_MCLK_EN,
86                        ULPD_CLOCK_CTRL);
87
88                 /* FIXME: The DPLL stays on for now */
89         }
90
91         return 0;
92 }
93
94 /*
95  * Hardware specific transceiver power on/off
96  */
97 static int omap_ohci_transceiver_power(int on)
98 {
99         if (on) {
100                 if (machine_is_omap_innovator() && cpu_is_omap1510())
101                         fpga_write(fpga_read(INNOVATOR_FPGA_CAM_USB_CONTROL) | 0x20, 
102                                INNOVATOR_FPGA_CAM_USB_CONTROL);
103                 else if (machine_is_omap_osk()) {
104                         /* FIXME: GPIO1 -> 1 on the TPS65010 I2C chip */
105                 }
106         } else {
107                 if (machine_is_omap_innovator() && cpu_is_omap1510())
108                         fpga_write(fpga_read(INNOVATOR_FPGA_CAM_USB_CONTROL) & ~0x20, 
109                                INNOVATOR_FPGA_CAM_USB_CONTROL);
110                 else if (machine_is_omap_osk()) {
111                         /* FIXME: GPIO1 -> 0 on the TPS65010 I2C chip */
112                 }
113         }
114
115         return 0;
116 }
117
118 /*
119  * OMAP-1510 specific Local Bus clock on/off
120  */
121 static int omap_1510_local_bus_power(int on)
122 {
123         if (on) {
124                 omap_writel((1 << 1) | (1 << 0), OMAP1510_LB_MMU_CTL);
125                 udelay(200);
126         } else {
127                 omap_writel(0, OMAP1510_LB_MMU_CTL);
128         }
129
130         return 0;
131 }
132
133 /*
134  * OMAP-1510 specific Local Bus initialization
135  * NOTE: This assumes 32MB memory size in OMAP1510LB_MEMSIZE.
136  *       See also arch/mach-omap/memory.h for __virt_to_dma() and 
137  *       __dma_to_virt() which need to match with the physical 
138  *       Local Bus address below.
139  */
140 static int omap_1510_local_bus_init(void)
141 {
142         unsigned int tlb;
143         unsigned long lbaddr, physaddr;
144
145         omap_writel((omap_readl(OMAP1510_LB_CLOCK_DIV) & 0xfffffff8) | 0x4, 
146                OMAP1510_LB_CLOCK_DIV);
147
148         /* Configure the Local Bus MMU table */
149         for (tlb = 0; tlb < OMAP1510_LB_MEMSIZE; tlb++) {
150                 lbaddr = tlb * 0x00100000 + OMAP1510_LB_OFFSET;
151                 physaddr = tlb * 0x00100000 + PHYS_OFFSET;
152                 omap_writel((lbaddr & 0x0fffffff) >> 22, OMAP1510_LB_MMU_CAM_H);
153                 omap_writel(((lbaddr & 0x003ffc00) >> 6) | 0xc, 
154                        OMAP1510_LB_MMU_CAM_L);
155                 omap_writel(physaddr >> 16, OMAP1510_LB_MMU_RAM_H);
156                 omap_writel((physaddr & 0x0000fc00) | 0x300, OMAP1510_LB_MMU_RAM_L);
157                 omap_writel(tlb << 4, OMAP1510_LB_MMU_LCK);
158                 omap_writel(0x1, OMAP1510_LB_MMU_LD_TLB);
159         }
160
161         /* Enable the walking table */
162         omap_writel(omap_readl(OMAP1510_LB_MMU_CTL) | (1 << 3), OMAP1510_LB_MMU_CTL);
163         udelay(200);
164
165         return 0;
166 }
167
168 #ifdef  CONFIG_USB_OTG
169
170 static void start_hnp(struct ohci_hcd *ohci)
171 {
172         const unsigned  port = ohci->hcd.self.otg_port - 1;
173         unsigned long   flags;
174
175         otg_start_hnp(ohci->transceiver);
176
177         local_irq_save(flags);
178         ohci->transceiver->state = OTG_STATE_A_SUSPEND;
179         writel (RH_PS_PSS, &ohci->regs->roothub.portstatus [port]);
180         OTG_CTRL_REG &= ~OTG_A_BUSREQ;
181         local_irq_restore(flags);
182 }
183
184 #endif
185
186 /*-------------------------------------------------------------------------*/
187
188 static int omap_start_hc(struct ohci_hcd *ohci, struct platform_device *pdev)
189 {
190         struct omap_usb_config  *config = pdev->dev.platform_data;
191         int                     need_transceiver = (config->otg != 0);
192
193         dev_dbg(&pdev->dev, "starting USB Controller\n");
194
195         if (config->otg) {
196                 ohci->hcd.self.otg_port = config->otg;
197                 /* default/minimum OTG power budget:  8 mA */
198                 ohci->power_budget = 8;
199         }
200
201         /* boards can use OTG transceivers in non-OTG modes */
202         need_transceiver = need_transceiver
203                         || machine_is_omap_h2();
204
205         if (cpu_is_omap16xx())
206                 ocpi_enable();
207
208 #ifdef  CONFIG_ARCH_OMAP_OTG
209         if (need_transceiver) {
210                 ohci->transceiver = otg_get_transceiver();
211                 if (ohci->transceiver) {
212                         int     status = otg_set_host(ohci->transceiver,
213                                                 &ohci->hcd.self);
214                         dev_dbg(&pdev->dev, "init %s transceiver, status %d\n",
215                                         ohci->transceiver->label, status);
216                         if (status) {
217                                 if (ohci->transceiver)
218                                         put_device(ohci->transceiver->dev);
219                                 return status;
220                         }
221                 } else {
222                         dev_err(&pdev->dev, "can't find transceiver\n");
223                         return -ENODEV;
224                 }
225         }
226 #endif
227
228         if (machine_is_omap_osk()) {
229                 omap_request_gpio(9);
230                 omap_set_gpio_direction(9, 1);
231                 omap_set_gpio_dataout(9, 1);
232         }
233
234         omap_ohci_clock_power(1);
235
236         omap_ohci_transceiver_power(1);
237
238         if (cpu_is_omap1510()) {
239                 omap_1510_local_bus_power(1);
240                 omap_1510_local_bus_init();
241         }
242
243         /* board init will have already handled HMC and mux setup.
244          * any external transceiver should already be initialized
245          * too, so all configured ports use the right signaling now.
246          */
247
248         return 0;
249 }
250
251 static void omap_stop_hc(struct platform_device *pdev)
252 {
253         dev_dbg(&pdev->dev, "stopping USB Controller\n");
254
255         /*
256          * FIXME: Put the USB host controller into reset.
257          */
258
259         /*
260          * FIXME: Stop the USB clock.
261          */
262         //omap_disable_device(dev);
263
264 }
265
266
267 /*-------------------------------------------------------------------------*/
268
269 void usb_hcd_omap_remove (struct usb_hcd *, struct platform_device *);
270
271 /* configure so an HC device and id are always provided */
272 /* always called with process context; sleeping is OK */
273
274
275 /**
276  * usb_hcd_omap_probe - initialize OMAP-based HCDs
277  * Context: !in_interrupt()
278  *
279  * Allocates basic resources for this USB host controller, and
280  * then invokes the start() method for the HCD associated with it
281  * through the hotplug entry's driver_data.
282  */
283 int usb_hcd_omap_probe (const struct hc_driver *driver,
284                           struct platform_device *pdev)
285 {
286         int retval;
287         struct usb_hcd *hcd = 0;
288         struct ohci_hcd *ohci;
289
290         if (pdev->num_resources != 2) {
291                 printk(KERN_ERR "hcd probe: invalid num_resources: %i\n", 
292                        pdev->num_resources);
293                 return -ENODEV;
294         }
295
296         if (pdev->resource[0].flags != IORESOURCE_MEM 
297             || pdev->resource[1].flags != IORESOURCE_IRQ) {
298                 printk(KERN_ERR "hcd probe: invalid resource type\n");
299                 return -ENODEV;
300         }
301
302         if (!request_mem_region(pdev->resource[0].start, 
303                                 pdev->resource[0].end - pdev->resource[0].start + 1, hcd_name)) {
304                 dev_dbg(&pdev->dev, "request_mem_region failed\n");
305                 return -EBUSY;
306         }
307
308         hcd = driver->hcd_alloc ();
309         if (hcd == NULL){
310                 dev_dbg(&pdev->dev, "hcd_alloc failed\n");
311                 retval = -ENOMEM;
312                 goto err1;
313         }
314         dev_set_drvdata(&pdev->dev, hcd);
315         ohci = hcd_to_ohci(hcd);
316
317         hcd->driver = (struct hc_driver *) driver;
318         hcd->description = driver->description;
319         hcd->irq = pdev->resource[1].start;
320         hcd->regs = (void *)pdev->resource[0].start;
321         hcd->self.controller = &pdev->dev;
322
323         retval = omap_start_hc(ohci, pdev);
324         if (retval < 0)
325                 goto err2;
326
327         retval = hcd_buffer_create (hcd);
328         if (retval != 0) {
329                 dev_dbg(&pdev->dev, "pool alloc fail\n");
330                 goto err1;
331         }
332
333         retval = request_irq (hcd->irq, usb_hcd_irq, 
334                               SA_INTERRUPT, hcd->description, hcd);
335         if (retval != 0) {
336                 dev_dbg(&pdev->dev, "request_irq failed\n");
337                 retval = -EBUSY;
338                 goto err2;
339         }
340
341         dev_info(&pdev->dev, "at 0x%p, irq %d\n", hcd->regs, hcd->irq);
342
343         usb_bus_init (&hcd->self);
344         hcd->self.op = &usb_hcd_operations;
345         hcd->self.hcpriv = (void *) hcd;
346         hcd->self.bus_name = pdev->dev.bus_id;
347         hcd->product_desc = "OMAP OHCI";
348
349         INIT_LIST_HEAD (&hcd->dev_list);
350         usb_register_bus (&hcd->self);
351
352         if ((retval = driver->start (hcd)) < 0) 
353         {
354                 usb_hcd_omap_remove(hcd, pdev);
355                 return retval;
356         }
357
358         return 0;
359
360  err2:
361         hcd_buffer_destroy (hcd);
362         if (hcd)
363                 driver->hcd_free(hcd);
364  err1:
365         omap_stop_hc(pdev);
366
367         release_mem_region(pdev->resource[0].start, 
368                            pdev->resource[0].end - pdev->resource[0].start + 1);
369
370         dev_set_drvdata(&pdev->dev, 0);
371         return retval;
372 }
373
374
375 /* may be called without controller electrically present */
376 /* may be called with controller, bus, and devices active */
377
378 /**
379  * usb_hcd_omap_remove - shutdown processing for OMAP-based HCDs
380  * @dev: USB Host Controller being removed
381  * Context: !in_interrupt()
382  *
383  * Reverses the effect of usb_hcd_omap_probe(), first invoking
384  * the HCD's stop() method.  It is always called from a thread
385  * context, normally "rmmod", "apmd", or something similar.
386  *
387  */
388 void usb_hcd_omap_remove (struct usb_hcd *hcd, struct platform_device *pdev)
389 {
390         void *base;
391
392         dev_info(&pdev->dev, "remove: state %x\n", hcd->state);
393
394         if (in_interrupt ())
395                 BUG ();
396
397         hcd->state = USB_STATE_QUIESCING;
398
399         dev_dbg(&pdev->dev, "roothub graceful disconnect\n");
400         usb_disconnect (&hcd->self.root_hub);
401
402         hcd->driver->stop (hcd);
403         hcd_buffer_destroy (hcd);
404         hcd->state = USB_STATE_HALT;
405
406         if (machine_is_omap_osk())
407                 omap_free_gpio(9);
408
409         free_irq (hcd->irq, hcd);
410
411         usb_deregister_bus (&hcd->self);
412
413         base = hcd->regs;
414         hcd->driver->hcd_free (hcd);
415
416         omap_stop_hc(pdev);
417
418         release_mem_region(pdev->resource[0].start, 
419                            pdev->resource[0].end - pdev->resource[0].start + 1);
420 }
421
422 /*-------------------------------------------------------------------------*/
423
424 static int __devinit
425 ohci_omap_start (struct usb_hcd *hcd)
426 {
427         struct omap_usb_config *config;
428         struct ohci_hcd *ohci = hcd_to_ohci (hcd);
429         int             ret;
430
431         config = hcd->self.controller->platform_data;
432         ohci->hcca = dma_alloc_coherent (hcd->self.controller,
433                         sizeof *ohci->hcca, &ohci->hcca_dma, 0);
434         if (!ohci->hcca)
435                 return -ENOMEM;
436
437         memset (ohci->hcca, 0, sizeof (struct ohci_hcca));
438         if ((ret = ohci_mem_init (ohci)) < 0) {
439                 ohci_stop (hcd);
440                 return ret;
441         }
442         ohci->regs = hcd->regs;
443
444         if (config->otg || config->rwc)
445                 writel(OHCI_CTRL_RWC, &ohci->regs->control);
446
447         if (hc_reset (ohci) < 0) {
448                 ohci_stop (hcd);
449                 return -ENODEV;
450         }
451
452         if (hc_start (ohci) < 0) {
453                 err ("can't start %s", ohci->hcd.self.bus_name);
454                 ohci_stop (hcd);
455                 return -EBUSY;
456         }
457         if (ohci->power_budget)
458                 hub_set_power_budget(ohci->hcd.self.root_hub,
459                                         ohci->power_budget);
460         create_debug_files (ohci);
461
462 #ifdef  DEBUG
463         ohci_dump (ohci, 1);
464 #endif
465         return 0;
466 }
467
468 /*-------------------------------------------------------------------------*/
469
470 static const struct hc_driver ohci_omap_hc_driver = {
471         .description =          hcd_name,
472
473         /*
474          * generic hardware linkage
475          */
476         .irq =                  ohci_irq,
477         .flags =                HCD_USB11,
478
479         /*
480          * basic lifecycle operations
481          */
482         .start =                ohci_omap_start,
483         .stop =                 ohci_stop,
484
485         /*
486          * memory lifecycle (except per-request)
487          */
488         .hcd_alloc =            ohci_hcd_alloc,
489         .hcd_free =             ohci_hcd_free,
490
491         /*
492          * managing i/o requests and associated device resources
493          */
494         .urb_enqueue =          ohci_urb_enqueue,
495         .urb_dequeue =          ohci_urb_dequeue,
496         .endpoint_disable =     ohci_endpoint_disable,
497
498         /*
499          * scheduling support
500          */
501         .get_frame_number =     ohci_get_frame,
502
503         /*
504          * root hub support
505          */
506         .hub_status_data =      ohci_hub_status_data,
507         .hub_control =          ohci_hub_control,
508 #ifdef  CONFIG_USB_SUSPEND
509         .hub_suspend =          ohci_hub_suspend,
510         .hub_resume =           ohci_hub_resume,
511 #endif
512         .start_port_reset =     ohci_start_port_reset,
513 };
514
515 /*-------------------------------------------------------------------------*/
516
517 static int ohci_hcd_omap_drv_probe(struct device *dev)
518 {
519         return usb_hcd_omap_probe(&ohci_omap_hc_driver,
520                                 to_platform_device(dev));
521 }
522
523 static int ohci_hcd_omap_drv_remove(struct device *dev)
524 {
525         struct platform_device  *pdev = to_platform_device(dev);
526         struct usb_hcd          *hcd = dev_get_drvdata(dev);
527         struct ohci_hcd         *ohci = hcd_to_ohci (hcd);
528
529         usb_hcd_omap_remove(hcd, pdev);
530         if (ohci->transceiver) {
531                 (void) otg_set_host(ohci->transceiver, 0);
532                 put_device(ohci->transceiver->dev);
533         }
534         dev_set_drvdata(dev, NULL);
535
536         return 0;
537 }
538
539 /*-------------------------------------------------------------------------*/
540
541 #if     defined(CONFIG_USB_SUSPEND) || defined(CONFIG_PM)
542
543 /* states match PCI usage, always suspending the root hub except that
544  * 4 ~= D3cold (ACPI D3) with clock off (resume sees reset).
545  */
546
547 static int ohci_omap_suspend(struct device *dev, u32 state, u32 level)
548 {
549         struct ohci_hcd *ohci = hcd_to_ohci(dev_get_drvdata(dev));
550         int             status = -EINVAL;
551
552         if (state <= dev->power.power_state)
553                 return 0;
554
555         dev_dbg(dev, "suspend to %d\n", state);
556         down(&ohci->hcd.self.root_hub->serialize);
557         status = ohci_hub_suspend(&ohci->hcd);
558         if (status == 0) {
559                 if (state >= 4) {
560                         /* power off + reset */
561                         OTG_SYSCON_2_REG &= ~UHOST_EN;
562                         ohci->hcd.self.root_hub->state = USB_STATE_SUSPENDED;
563                         state = 4;
564                 }
565                 ohci->hcd.state = HCD_STATE_SUSPENDED;
566                 dev->power.power_state = state;
567         }
568         up(&ohci->hcd.self.root_hub->serialize);
569         return status;
570 }
571
572 static int ohci_omap_resume(struct device *dev, u32 level)
573 {
574         struct ohci_hcd *ohci = hcd_to_ohci(dev_get_drvdata(dev));
575         int             status = 0;
576
577         switch (dev->power.power_state) {
578         case 0:
579                 break;
580         case 4:
581                 if (time_before(jiffies, ohci->next_statechange))
582                         msleep(5);
583                 ohci->next_statechange = jiffies;
584                 OTG_SYSCON_2_REG |= UHOST_EN;
585                 /* FALLTHROUGH */
586         default:
587                 dev_dbg(dev, "resume from %d\n", dev->power.power_state);
588 #ifdef  CONFIG_USB_SUSPEND
589                 /* get extra cleanup even if remote wakeup isn't in use */
590                 status = usb_resume_device(ohci->hcd.self.root_hub);
591 #else
592                 down(&ohci->hcd.self.root_hub->serialize);
593                 status = ohci_hub_resume(&ohci->hcd);
594                 up(&ohci->hcd.self.root_hub->serialize);
595 #endif
596                 if (status == 0)
597                         dev->power.power_state = 0;
598                 break;
599         }
600         return status;
601 }
602
603 #endif
604
605 /*-------------------------------------------------------------------------*/
606
607 /*
608  * Driver definition to register with the OMAP bus
609  */
610 static struct device_driver ohci_hcd_omap_driver = {
611         .name           = "ohci",
612         .bus            = &platform_bus_type,
613         .probe          = ohci_hcd_omap_drv_probe,
614         .remove         = ohci_hcd_omap_drv_remove,
615 #if     defined(CONFIG_USB_SUSPEND) || defined(CONFIG_PM)
616         .suspend        = ohci_omap_suspend,
617         .resume         = ohci_omap_resume,
618 #endif
619 };
620
621 static int __init ohci_hcd_omap_init (void)
622 {
623         printk (KERN_DEBUG "%s: " DRIVER_INFO " (OMAP)\n", hcd_name);
624         if (usb_disabled())
625                 return -ENODEV;
626
627         pr_debug("%s: block sizes: ed %Zd td %Zd\n", hcd_name,
628                 sizeof (struct ed), sizeof (struct td));
629
630         return driver_register(&ohci_hcd_omap_driver);
631 }
632
633 static void __exit ohci_hcd_omap_cleanup (void)
634 {
635         driver_unregister(&ohci_hcd_omap_driver);
636 }
637
638 module_init (ohci_hcd_omap_init);
639 module_exit (ohci_hcd_omap_cleanup);