VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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-2002 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 Rusell 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
22 #include <asm/arch/bus.h>
23 #include <asm/arch/hardware.h>
24 #include <asm/arch/mux.h>
25 #include <asm/arch/irqs.h>
26
27 #include "ohci-omap.h"
28
29 #ifndef CONFIG_ARCH_OMAP
30 #error "This file is OMAP bus glue.  CONFIG_OMAP must be defined."
31 #endif
32
33 extern int usb_disabled(void);
34 extern int ocpi_enable(void);
35
36 /*
37  * Use the first port only by default. Override with hmc_mode option.
38  *
39  * NOTE: Many OMAP-1510 Innovators supposedly have bad wiring for the USB ports
40  *       1 & 2, so only port 0 will work. To use the OHCI on the first port, use 
41  *       the Innovator USB client cable with a client-to-client connector and modify
42  *       either the cable or the hub to feed 5V VBUS back to Innovator. VBUS should
43  *       be the red lead in the cable.
44  *
45  *       To mount USB hard disk as root, see the patch for do_mounts.c that tries 
46  *       remounting the root, and use root=0801 if your root is on sda1. Does not 
47  *       work with devfs.
48  */
49 static int default_hmc_mode = 16;
50 static int hmc_mode = 1234;
51
52 /*
53  * Set the USB host pin multiplexing and the selected HMC mode
54  */
55 static int omap_usb_set_hmc_mode(int hmc_mode)
56 {
57         unsigned int val;
58
59         switch (hmc_mode) {
60         case 0:
61                 /* 0: function, 1: disabled, 2: disabled */
62                 omap_cfg_reg(W4_USB_PUEN);
63                 omap_cfg_reg(R18_1510_USB_GPIO0);
64                 break;
65         case 4:
66                 /* 0: function 1: host 2: host */
67                 omap_cfg_reg(usb1_speed);
68                 omap_cfg_reg(usb1_susp);
69                 omap_cfg_reg(usb1_seo);
70                 omap_cfg_reg(usb1_txen);
71                 omap_cfg_reg(usb1_txd);
72                 omap_cfg_reg(usb1_vp);
73                 omap_cfg_reg(usb1_vm);
74                 omap_cfg_reg(usb1_rcv);
75                 omap_cfg_reg(usb2_susp);
76                 omap_cfg_reg(usb2_seo);
77                 omap_cfg_reg(usb2_txen);
78                 omap_cfg_reg(usb2_txd);
79                 omap_cfg_reg(usb2_vp);
80                 omap_cfg_reg(usb2_vm);
81                 omap_cfg_reg(usb2_rcv);
82                 break;
83         case 16:
84                 /* 0: host, 1: disabled, 2: disabled */
85                 omap_cfg_reg(W9_USB0_TXEN);
86                 omap_cfg_reg(AA9_USB0_VP);
87                 omap_cfg_reg(Y5_USB0_RCV);
88                 omap_cfg_reg(R9_USB0_VM);
89                 omap_cfg_reg(V6_USB0_TXD);
90                 omap_cfg_reg(W5_USB0_SE0);
91                 break;
92         default:
93                 printk("Unknown USB host configuration: %i\n", hmc_mode);
94                 return -ENODEV;
95         }
96
97         /* Write the selected HMC mode */
98         val = readl(MOD_CONF_CTRL_0) & ~HMC_CLEAR;
99         val |= (hmc_mode << 1);
100         writel(val, MOD_CONF_CTRL_0);
101
102         return 0;
103 }
104
105 /*
106  * OHCI clock initialization for OMAP-1510 and 1610
107  */
108 static int omap_ohci_clock_power(int on)
109 {
110         if (on) {
111                 if (cpu_is_omap_1510()) {
112                         /* Use DPLL, not APLL */
113                         writel(readl(ULPD_APLL_CTRL_REG) & ~APLL_NDPLL_SWITCH,
114                                ULPD_APLL_CTRL_REG);
115
116                         /* Enable DPLL */
117                         writel(readl(ULPD_DPLL_CTRL_REG) | DPLL_PLL_ENABLE,
118                                ULPD_DPLL_CTRL_REG);
119
120                         /* Software request for USB 48MHz clock */
121                         writel(readl(ULPD_SOFT_REQ_REG) | SOFT_REQ_REG_REQ,
122                                ULPD_SOFT_REQ_REG);
123
124                         while (!(readl(ULPD_DPLL_CTRL_REG) & DPLL_LOCK));
125                 }
126
127                 if (cpu_is_omap_1610()) {
128                         /* Enable OHCI */
129                         writel(readl(ULPD_SOFT_REQ_REG) | SOFT_USB_OTG_REQ,
130                                 ULPD_SOFT_REQ_REG);
131
132                         /* USB host clock request if not using OTG */
133                         writel(readl(ULPD_SOFT_REQ_REG) | SOFT_USB_REQ,
134                                 ULPD_SOFT_REQ_REG);
135
136                         writel(readl(ULPD_STATUS_REQ_REG) | USB_HOST_DPLL_REQ,
137                              ULPD_STATUS_REQ_REG);
138                 }
139
140                 /* Enable 48MHz clock to USB */
141                 writel(readl(ULPD_CLOCK_CTRL_REG) | USB_MCLK_EN,
142                        ULPD_CLOCK_CTRL_REG);
143
144                 writel(readl(ARM_IDLECT2) | (1 << EN_LBFREECK) | (1 << EN_LBCK),
145                        ARM_IDLECT2);
146
147                 writel(readl(MOD_CONF_CTRL_0) | USB_HOST_HHC_UHOST_EN,
148                        MOD_CONF_CTRL_0);
149         } else {
150                 /* Disable 48MHz clock to USB */
151                 writel(readl(ULPD_CLOCK_CTRL_REG) & ~USB_MCLK_EN,
152                        ULPD_CLOCK_CTRL_REG);
153
154                 /* FIXME: The DPLL stays on for now */
155         }
156
157         return 0;
158 }
159
160 /*
161  * Hardware specific transceiver power on/off
162  */
163 static int omap_ohci_transceiver_power(int on)
164 {
165         if (on) {
166                 if (omap_is_innovator())
167                         writel(readl(OMAP1510_FPGA_HOST_CTRL) | 0x20, 
168                                OMAP1510_FPGA_HOST_CTRL);
169         } else {
170                 if (omap_is_innovator())
171                         writel(readl(OMAP1510_FPGA_HOST_CTRL) & ~0x20, 
172                                OMAP1510_FPGA_HOST_CTRL);
173         }
174
175         return 0;
176 }
177
178 /*
179  * OMAP-1510 specific Local Bus clock on/off
180  */
181 static int omap_1510_local_bus_power(int on)
182 {
183         if (on) {
184                 writel((1 << 1) | (1 << 0), OMAP1510_LB_MMU_CTL);
185                 udelay(200);
186         } else {
187                 writel(0, OMAP1510_LB_MMU_CTL);
188         }
189
190         return 0;
191 }
192
193 /*
194  * OMAP-1510 specific Local Bus initialization
195  * NOTE: This assumes 32MB memory size in OMAP1510LB_MEMSIZE.
196  *       See also arch/mach-omap/memory.h for __virt_to_bus() and 
197  *       __bus_to_virt() which need to match with the physical 
198  *       Local Bus address below.
199  */
200 static int omap_1510_local_bus_init(void)
201 {
202         unsigned int tlb;
203         unsigned long lbaddr, physaddr;
204
205         writel((readl(OMAP1510_LB_CLOCK_DIV) & 0xfffffff8) | 0x4, 
206                OMAP1510_LB_CLOCK_DIV);
207
208         /* Configure the Local Bus MMU table */
209         for (tlb = 0; tlb < OMAP1510_LB_MEMSIZE; tlb++) {
210                 lbaddr = tlb * 0x00100000 + OMAP1510_LB_OFFSET;
211                 physaddr = tlb * 0x00100000 + PHYS_OFFSET;
212                 writel((lbaddr & 0x0fffffff) >> 22, OMAP1510_LB_MMU_CAM_H);
213                 writel(((lbaddr & 0x003ffc00) >> 6) | 0xc, 
214                        OMAP1510_LB_MMU_CAM_L);
215                 writel(physaddr >> 16, OMAP1510_LB_MMU_RAM_H);
216                 writel((physaddr & 0x0000fc00) | 0x300, OMAP1510_LB_MMU_RAM_L);
217                 writel(tlb << 4, OMAP1510_LB_MMU_LCK);
218                 writel(0x1, OMAP1510_LB_MMU_LD_TLB);
219         }
220
221         /* Enable the walking table */
222         writel(readl(OMAP1510_LB_MMU_CTL) | (1 << 3), OMAP1510_LB_MMU_CTL);
223         udelay(200);
224
225         return 0;
226 }
227
228 /*
229  * OMAP-1610 specific hardware initialization
230  *
231  * Intended to configure OMAP-1610 USB host and OTG ports depending on 
232  * the HMC mode selected.
233  *
234  * FIXME: Currently only supports alternate ping group 2 mode, should
235  *        be easy to modify for other configurations once there is some
236  *        hardware to test with.
237  */
238 static int omap_1610_usb_init(int mode)
239 {
240         u_int val = 0;
241
242         /* Configure the OMAP transceiver settings */
243         val |= (1 << 8); /* CONF_USB2_UNI TRM p 15-205*/
244         val |= (4 << 4); /* TRM p 5-59, p 15-157 (1224) */
245
246         //val |= (1 << 3); /* Isolate integrated transceiver from port 0 */
247         val |= (1 << 2); /* Disable pulldown on integrated transceiver DM */
248         val |= (1 << 1); /* Disable pulldown on integraded transceiver DP */
249
250         writel(val, USB_TRANSCEIVER_CTRL);
251
252         /* Set the USB0_TRX_MODE */
253         val = 0;
254         val &= ~OTG_IDLE_EN;
255         val &= ~DEV_IDLE_EN;
256         val &= ~(7 << 16);      /* Clear USB0_TRX_MODE */
257         val |= (3 << 16);       /* 0 or 3, 6-wire DAT/SE0, TRM p 15-159 */
258         writel(val, OTG_SYSCON_1);
259
260         /* 
261          * Control via OTG, see TRM p 15-163
262          */
263         val = 0;
264         //val |= 1;             /* REVISIT: Enable OTG = 1 */
265
266         /* Control via OTG */
267         val &= ~HMC_PADEN;
268         val &= ~OTG_PADEN;
269         val |= UHOST_EN;        
270
271         val &= ~0x3f;           /* Clear HMC mode */
272         val |= mode;            /* Set HMC mode */
273         val &= ~(7 << 16);      /* Clear ASE0_BRST */
274         val |= (4 << 16);       /* Must be 4 */
275         val |= USBX_SYNCHRO;    /* Must be set */
276         val |= SRP_VBUS;
277         writel(val, OTG_SYSCON_2);
278
279         /* Enable OTG idle */
280         //writel(readl(OTG_SYSCON_1) | OTG_IDLE_EN, OTG_SYSCON_1);
281
282         return 0;
283 }
284
285 /*-------------------------------------------------------------------------*/
286
287 static void omap_start_hc(struct omap_dev *dev)
288 {
289         printk(KERN_DEBUG __FILE__ 
290                ": starting OMAP OHCI USB Controller\n");
291
292         /* 
293          * Set the HMC mode for the USB ports
294          */
295 #if 0
296         /* See note about the Innovator wiring above */
297         if (omap_is_innovator())
298                 hmc_mode = 4;   /* 0: function 1: host 2: host */
299 #endif
300
301         if (cpu_is_omap_1610())
302                 ocpi_enable();
303
304         omap_usb_set_hmc_mode(hmc_mode);
305
306         omap_ohci_clock_power(1);
307         omap_ohci_transceiver_power(1);
308
309         if (cpu_is_omap_1510()) {
310                 omap_1510_local_bus_power(1);
311                 omap_1510_local_bus_init();
312         }
313
314         if (cpu_is_omap_1610())
315                 omap_1610_usb_init(hmc_mode);
316
317         //omap_enable_device(dev);
318 }
319
320 static void omap_stop_hc(struct omap_dev *dev)
321 {
322         printk(KERN_DEBUG __FILE__ 
323                ": stopping OMAP OHCI USB Controller\n");
324
325         /*
326          * FIXME: Put the USB host controller into reset.
327          */
328
329         /*
330          * FIXME: Stop the USB clock.
331          */
332         //omap_disable_device(dev);
333
334 }
335
336
337 /*-------------------------------------------------------------------------*/
338
339 static irqreturn_t usb_hcd_omap_hcim_irq (int irq, void *__hcd, struct pt_regs * r)
340 {
341         struct usb_hcd *hcd = __hcd;
342
343         return usb_hcd_irq(irq, hcd, r);
344 }
345
346 /*-------------------------------------------------------------------------*/
347
348 void usb_hcd_omap_remove (struct usb_hcd *, struct omap_dev *);
349
350 /* configure so an HC device and id are always provided */
351 /* always called with process context; sleeping is OK */
352
353
354 /**
355  * usb_hcd_omap_probe - initialize OMAP-based HCDs
356  * Context: !in_interrupt()
357  *
358  * Allocates basic resources for this USB host controller, and
359  * then invokes the start() method for the HCD associated with it
360  * through the hotplug entry's driver_data.
361  *
362  * Store this function in the HCD's struct pci_driver as probe().
363  */
364 int usb_hcd_omap_probe (const struct hc_driver *driver,
365                           struct usb_hcd **hcd_out,
366                           struct omap_dev *dev)
367 {
368         int retval;
369         struct usb_hcd *hcd = 0;
370
371         if (!request_mem_region(dev->res.start, 
372                                 dev->res.end - dev->res.start + 1, hcd_name)) {
373                 dbg("request_mem_region failed");
374                 return -EBUSY;
375         }
376
377         omap_start_hc(dev);
378
379         hcd = driver->hcd_alloc ();
380         if (hcd == NULL){
381                 dbg ("hcd_alloc failed");
382                 retval = -ENOMEM;
383                 goto err1;
384         }
385
386         hcd->driver = (struct hc_driver *) driver;
387         hcd->description = driver->description;
388         hcd->irq = dev->irq[0];
389         hcd->regs = dev->mapbase;
390         hcd->self.controller = &dev->dev;
391
392         retval = hcd_buffer_create (hcd);
393         if (retval != 0) {
394                 dbg ("pool alloc fail");
395                 goto err1;
396         }
397
398         retval = request_irq (hcd->irq, 
399                               usb_hcd_omap_hcim_irq, 
400                               SA_INTERRUPT, hcd->description, hcd);
401         if (retval != 0) {
402                 dbg("request_irq failed");
403                 retval = -EBUSY;
404                 goto err2;
405         }
406
407         info ("%s (OMAP) at 0x%p, irq %d\n",
408               hcd->description, hcd->regs, hcd->irq);
409
410         usb_bus_init (&hcd->self);
411         hcd->self.op = &usb_hcd_operations;
412         hcd->self.hcpriv = (void *) hcd;
413         hcd->self.bus_name = "omap";
414         hcd->product_desc = "OMAP OHCI";
415
416         INIT_LIST_HEAD (&hcd->dev_list);
417         usb_register_bus (&hcd->self);
418
419         if ((retval = driver->start (hcd)) < 0) 
420         {
421                 usb_hcd_omap_remove(hcd, dev);
422                 return retval;
423         }
424
425         *hcd_out = hcd;
426         return 0;
427
428  err2:
429         hcd_buffer_destroy (hcd);
430         if (hcd)
431                 driver->hcd_free(hcd);
432  err1:
433         omap_stop_hc(dev);
434
435         release_mem_region(dev->res.start, dev->res.end - dev->res.start + 1);
436
437         return retval;
438 }
439
440
441 /* may be called without controller electrically present */
442 /* may be called with controller, bus, and devices active */
443
444 /**
445  * usb_hcd_omap_remove - shutdown processing for OMAP-based HCDs
446  * @dev: USB Host Controller being removed
447  * Context: !in_interrupt()
448  *
449  * Reverses the effect of usb_hcd_omap_probe(), first invoking
450  * the HCD's stop() method.  It is always called from a thread
451  * context, normally "rmmod", "apmd", or something similar.
452  *
453  */
454 void usb_hcd_omap_remove (struct usb_hcd *hcd, struct omap_dev *dev)
455 {
456         void *base;
457
458         info ("remove: %s, state %x", hcd->self.bus_name, hcd->state);
459
460         if (in_interrupt ())
461                 BUG ();
462
463         hcd->state = USB_STATE_QUIESCING;
464
465         dbg ("%s: roothub graceful disconnect", hcd->self.bus_name);
466         usb_disconnect (&hcd->self.root_hub);
467
468         hcd->driver->stop (hcd);
469         hcd_buffer_destroy (hcd);
470         hcd->state = USB_STATE_HALT;
471
472         free_irq (hcd->irq, hcd);
473
474         usb_deregister_bus (&hcd->self);
475
476         base = hcd->regs;
477         hcd->driver->hcd_free (hcd);
478
479         omap_stop_hc(dev);
480
481         release_mem_region(dev->res.start, dev->res.end - dev->res.start + 1);
482 }
483
484 /*-------------------------------------------------------------------------*/
485
486 static int __devinit
487 ohci_omap_start (struct usb_hcd *hcd)
488 {
489         struct ohci_hcd *ohci = hcd_to_ohci (hcd);
490         int             ret;
491
492         ohci->hcca = dma_alloc_consistent (hcd->self.controller,
493                         sizeof *ohci->hcca, &ohci->hcca_dma);
494         if (!ohci->hcca)
495                 return -ENOMEM;
496
497         memset (ohci->hcca, 0, sizeof (struct ohci_hcca));
498         if ((ret = ohci_mem_init (ohci)) < 0) {
499                 ohci_stop (hcd);
500                 return ret;
501         }
502         ohci->regs = hcd->regs;
503         if (hc_reset (ohci) < 0) {
504                 ohci_stop (hcd);
505                 return -ENODEV;
506         }
507
508         if (hc_start (ohci) < 0) {
509                 err ("can't start %s", ohci->hcd.self.bus_name);
510                 ohci_stop (hcd);
511                 return -EBUSY;
512         }
513         create_debug_files (ohci);
514
515 #ifdef  DEBUG
516         ohci_dump (ohci, 1);
517 #endif
518         return 0;
519 }
520
521 /*-------------------------------------------------------------------------*/
522
523 static const struct hc_driver ohci_omap_hc_driver = {
524         .description =          hcd_name,
525
526         /*
527          * generic hardware linkage
528          */
529         .irq =                  ohci_irq,
530         .flags =                HCD_USB11,
531
532         /*
533          * basic lifecycle operations
534          */
535         .start =                ohci_omap_start,
536 #ifdef  CONFIG_PM
537         /* suspend:             ohci_omap_suspend,  -- tbd */
538         /* resume:              ohci_omap_resume,   -- tbd */
539 #endif
540         .stop =                 ohci_stop,
541
542         /*
543          * memory lifecycle (except per-request)
544          */
545         .hcd_alloc =            ohci_hcd_alloc,
546         .hcd_free =             ohci_hcd_free,
547
548         /*
549          * managing i/o requests and associated device resources
550          */
551         .urb_enqueue =          ohci_urb_enqueue,
552         .urb_dequeue =          ohci_urb_dequeue,
553         .endpoint_disable =     ohci_endpoint_disable,
554
555         /*
556          * scheduling support
557          */
558         .get_frame_number =     ohci_get_frame,
559
560         /*
561          * root hub support
562          */
563         .hub_status_data =      ohci_hub_status_data,
564         .hub_control =          ohci_hub_control,
565 #ifdef  CONFIG_USB_SUSPEND
566         .hub_suspend =          ohci_hub_suspend,
567         .hub_resume =           ohci_hub_resume,
568 #endif
569 };
570
571 /*-------------------------------------------------------------------------*/
572
573 static int ohci_hcd_omap_drv_probe(struct omap_dev *dev)
574 {
575         struct usb_hcd *hcd = NULL;
576         int ret;
577
578         if (usb_disabled())
579                 return -ENODEV;
580
581         ret = usb_hcd_omap_probe(&ohci_omap_hc_driver, &hcd, dev);
582
583         if (ret == 0)
584                 omap_set_drvdata(dev, hcd);
585
586         return ret;
587 }
588
589 static int ohci_hcd_omap_drv_remove(struct omap_dev *dev)
590 {
591         struct usb_hcd *hcd = omap_get_drvdata(dev);
592
593         usb_hcd_omap_remove(hcd, dev);
594
595         omap_set_drvdata(dev, NULL);
596
597         return 0;
598 }
599
600 /*
601  * Driver definition to register with the OMAP bus
602  */
603 static struct omap_driver ohci_hcd_omap_driver = {
604         .drv = {
605                 .name   = OMAP_OHCI_NAME,
606         },
607         .devid          = OMAP_OCP_DEVID_USB,
608         .busid          = OMAP_BUS_OCP,
609         .clocks         = 0,
610         .probe          = ohci_hcd_omap_drv_probe,
611         .remove         = ohci_hcd_omap_drv_remove,
612 };
613
614 /* Any dma_mask must be set for OHCI to work */
615 static u64 omap_dmamask = 0xffffffffUL; 
616
617 /*
618  * Device definition to match the driver above
619  */
620 static struct omap_dev ohci_hcd_omap_device = {
621         .name           = OMAP_OHCI_NAME,
622         .devid          = OMAP_OCP_DEVID_USB,
623         .busid          = OMAP_BUS_OCP,
624         .mapbase        = (void *)OMAP_OHCI_BASE,
625         .dma_mask       = &omap_dmamask,        /* Needed only for OHCI */
626         .res = {
627                 .start  = OMAP_OHCI_BASE,
628                 .end    = OMAP_OHCI_BASE + OMAP_OHCI_SIZE,
629         },
630         .irq = {
631                 INT_USB_HHC_1,
632         },
633 };
634
635 static int __init ohci_hcd_omap_init (void)
636 {
637         int ret;
638
639         dbg (DRIVER_INFO " (OMAP)");
640         dbg ("block sizes: ed %d td %d\n",
641                 sizeof (struct ed), sizeof (struct td));
642
643         if (hmc_mode < 0 || hmc_mode > 25)
644                 hmc_mode = default_hmc_mode;
645
646         /* Register the driver with OMAP bus */
647         ret = omap_driver_register(&ohci_hcd_omap_driver);
648         if (ret != 0)
649                 return -ENODEV;
650
651         /* Register the device with OMAP bus */
652         ret = omap_device_register(&ohci_hcd_omap_device);
653         if (ret != 0) {
654                 omap_driver_unregister(&ohci_hcd_omap_driver);
655                 return -ENODEV;
656         }
657
658         return ret;
659 }
660
661 MODULE_PARM(hmc_mode, "hmc_mode");
662
663 static void __exit ohci_hcd_omap_cleanup (void)
664 {
665         omap_device_unregister(&ohci_hcd_omap_device);
666         omap_driver_unregister(&ohci_hcd_omap_driver);
667 }
668
669 module_init (ohci_hcd_omap_init);
670 module_exit (ohci_hcd_omap_cleanup);