vserver 1.9.5.x5
[linux-2.6.git] / arch / arm / mach-omap / usb.c
1 /*
2  * arch/arm/mach-omap/usb.c -- platform level USB initialization
3  *
4  * Copyright (C) 2004 Texas Instruments, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #undef  DEBUG
22
23 #include <linux/config.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/types.h>
27 #include <linux/errno.h>
28 #include <linux/init.h>
29 #include <linux/device.h>
30 #include <linux/usb_otg.h>
31
32 #include <asm/io.h>
33 #include <asm/irq.h>
34 #include <asm/system.h>
35 #include <asm/hardware.h>
36 #include <asm/mach-types.h>
37
38 #include <asm/arch/mux.h>
39 #include <asm/arch/usb.h>
40 #include <asm/arch/board.h>
41
42 /* These routines should handle the standard chip-specific modes
43  * for usb0/1/2 ports, covering basic mux and transceiver setup.
44  * Call omap_usb_init() once, from INIT_MACHINE().
45  *
46  * Some board-*.c files will need to set up additional mux options,
47  * like for suspend handling, vbus sensing, GPIOs, and the D+ pullup.
48  */
49
50 /* TESTED ON:
51  *  - 1611B H2 (with usb1 mini-AB) using standard Mini-B or OTG cables
52  *  - 1510 Innovator UDC with bundled usb0 cable
53  *  - 1510 Innovator OHCI with bundled usb1/usb2 cable
54  *  - 1510 Innovator OHCI with custom usb0 cable, feeding 5V VBUS
55  *  - 1710 custom development board using alternate pin group
56  */
57
58 /*-------------------------------------------------------------------------*/
59
60 #ifdef  CONFIG_ARCH_OMAP_OTG
61
62 static struct otg_transceiver *xceiv;
63
64 /**
65  * otg_get_transceiver - find the (single) OTG transceiver driver
66  *
67  * Returns the transceiver driver, after getting a refcount to it; or
68  * null if there is no such transceiver.  The caller is responsible for
69  * releasing that count.
70  */
71 struct otg_transceiver *otg_get_transceiver(void)
72 {
73         if (xceiv)
74                 get_device(xceiv->dev);
75         return xceiv;
76 }
77 EXPORT_SYMBOL(otg_get_transceiver);
78
79 int otg_set_transceiver(struct otg_transceiver *x)
80 {
81         if (xceiv && x)
82                 return -EBUSY;
83         xceiv = x;
84         return 0;
85 }
86 EXPORT_SYMBOL(otg_set_transceiver);
87
88 #endif
89
90 /*-------------------------------------------------------------------------*/
91
92 static u32 __init omap_usb0_init(unsigned nwires, unsigned is_device)
93 {
94         u32     syscon1 = 0;
95
96         if (nwires == 0) {
97                 if (!cpu_is_omap15xx()) {
98                         /* pulldown D+/D- */
99                         USB_TRANSCEIVER_CTRL_REG &= ~(3 << 1);
100                 }
101                 return 0;
102         }
103
104         /*
105          * VP and VM are needed for all active usb0 configurations.
106          * USB0_VP and USB0_VM are always set on 1510, there's no muxing
107          * available for them.
108          */
109         if (nwires >= 2 && !cpu_is_omap15xx()) {
110                 omap_cfg_reg(AA9_USB0_VP);
111                 omap_cfg_reg(R9_USB0_VM);
112         }
113         if (is_device)
114                 omap_cfg_reg(W4_USB_PUEN);
115
116         /* internal transceiver */
117         if (nwires == 2) {
118                 if (cpu_is_omap15xx()) {
119                         /* This works for OHCI on 1510-Innovator */
120                         return 0;
121                 }
122
123                 /* NOTE:  host OR device mode for now, no OTG */
124                 USB_TRANSCEIVER_CTRL_REG &= ~(7 << 4);
125                 if (is_device) {
126                         omap_cfg_reg(R18_1510_USB_GPIO0);
127                         // omap_cfg_reg(USB0_VBUS);
128                         // USB_TRANSCEIVER_CTRL_REG.CONF_USB0_PORT_R = 7
129                 } else /* host mode needs D+ and D- pulldowns */
130                         USB_TRANSCEIVER_CTRL_REG &= ~(3 << 1);
131
132                 return 3 << 16;
133         }
134
135         /* alternate pin config, external transceiver */
136         omap_cfg_reg(V6_USB0_TXD);
137         omap_cfg_reg(W9_USB0_TXEN);
138         omap_cfg_reg(W5_USB0_SE0);
139
140 #ifdef CONFIG_ARCH_OMAP_USB_SPEED
141         /* FIXME: there's good chance that pin V9 is used for MMC2 port cmddir */
142         omap_cfg_reg(V9_USB0_SPEED);
143         // omap_cfg_reg(V9_USB0_SUSP);
144 #endif
145
146         if (nwires != 3)
147                 omap_cfg_reg(Y5_USB0_RCV);
148
149         switch (nwires) {
150         case 3:
151                 syscon1 = 2;
152                 break;
153         case 4:
154                 syscon1 = 1;
155                 break;
156         case 6:
157                 syscon1 = 3;
158                 /* REVISIT: Is CONF_USB2_UNI_R only needed when nwires = 6? */
159                 USB_TRANSCEIVER_CTRL_REG |= CONF_USB2_UNI_R;
160                 break;
161         default:
162                 printk(KERN_ERR "illegal usb%d %d-wire transceiver\n",
163                         0, nwires);
164         }
165         return syscon1 << 16;
166 }
167
168 static u32 __init omap_usb1_init(unsigned nwires)
169 {
170         u32     syscon1 = 0;
171
172         if (nwires != 6 && !cpu_is_omap15xx())
173                 USB_TRANSCEIVER_CTRL_REG &= ~CONF_USB1_UNI_R;
174         if (nwires == 0)
175                 return 0;
176
177         /* external transceiver */
178         omap_cfg_reg(USB1_TXD);
179         omap_cfg_reg(USB1_TXEN);
180         if (cpu_is_omap15xx()) {
181                 omap_cfg_reg(USB1_SEO);
182                 omap_cfg_reg(USB1_SPEED);
183                 // SUSP
184         } else if (cpu_is_omap16xx()) {
185                 omap_cfg_reg(W13_1610_USB1_SE0);
186                 omap_cfg_reg(R13_1610_USB1_SPEED);
187                 // SUSP
188         } else {
189                 pr_debug("usb unrecognized\n");
190         }
191         if (nwires != 3)
192                 omap_cfg_reg(USB1_RCV);
193
194         switch (nwires) {
195         case 3:
196                 syscon1 = 2;
197                 break;
198         case 4:
199                 syscon1 = 1;
200                 break;
201         case 6:
202                 syscon1 = 3;
203                 omap_cfg_reg(USB1_VP);
204                 omap_cfg_reg(USB1_VM);
205                 if (!cpu_is_omap15xx())
206                         USB_TRANSCEIVER_CTRL_REG |= CONF_USB1_UNI_R;
207                 break;
208         default:
209                 printk(KERN_ERR "illegal usb%d %d-wire transceiver\n",
210                         1, nwires);
211         }
212         return syscon1 << 20;
213 }
214
215 static u32 __init omap_usb2_init(unsigned nwires, unsigned alt_pingroup)
216 {
217         u32     syscon1 = 0;
218
219         if (alt_pingroup || nwires == 0)
220                 return 0;
221         if (nwires != 6 && !cpu_is_omap15xx())
222                 USB_TRANSCEIVER_CTRL_REG &= ~CONF_USB2_UNI_R;
223         if (nwires == 0)
224                 return 0;
225
226         /* external transceiver */
227         if (cpu_is_omap15xx()) {
228                 omap_cfg_reg(USB2_TXD);
229                 omap_cfg_reg(USB2_TXEN);
230                 omap_cfg_reg(USB2_SEO);
231                 if (nwires != 3)
232                         omap_cfg_reg(USB2_RCV);
233                 /* there is no USB2_SPEED */
234         } else if (cpu_is_omap16xx()) {
235                 omap_cfg_reg(V6_USB2_TXD);
236                 omap_cfg_reg(W9_USB2_TXEN);
237                 omap_cfg_reg(W5_USB2_SE0);
238                 if (nwires != 3)
239                         omap_cfg_reg(Y5_USB2_RCV);
240                 // FIXME omap_cfg_reg(USB2_SPEED);
241         } else {
242                 pr_debug("usb unrecognized\n");
243         }
244         // omap_cfg_reg(USB2_SUSP);
245
246         switch (nwires) {
247         case 3:
248                 syscon1 = 2;
249                 break;
250         case 4:
251                 syscon1 = 1;
252                 break;
253         case 6:
254                 syscon1 = 3;
255                 if (cpu_is_omap15xx()) {
256                         omap_cfg_reg(USB2_VP);
257                         omap_cfg_reg(USB2_VM);
258                 } else {
259                         omap_cfg_reg(AA9_USB2_VP);
260                         omap_cfg_reg(R9_USB2_VM);
261                         USB_TRANSCEIVER_CTRL_REG |= CONF_USB2_UNI_R;
262                 }
263                 break;
264         default:
265                 printk(KERN_ERR "illegal usb%d %d-wire transceiver\n",
266                         2, nwires);
267         }
268         return syscon1 << 24;
269 }
270
271 /*-------------------------------------------------------------------------*/
272
273 #if     defined(CONFIG_USB_GADGET_OMAP) || \
274         defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE) || \
275         (defined(CONFIG_USB_OTG) && defined(CONFIG_ARCH_OMAP_OTG))
276 static void usb_release(struct device *dev)
277 {
278         /* normally not freed */
279 }
280 #endif
281
282 #ifdef  CONFIG_USB_GADGET_OMAP
283
284 static struct resource udc_resources[] = {
285         /* order is significant! */
286         {               /* registers */
287                 .start          = IO_ADDRESS(UDC_BASE),
288                 .end            = IO_ADDRESS(UDC_BASE + 0xff),
289                 .flags          = IORESOURCE_MEM,
290         }, {            /* general IRQ */
291                 .start          = IH2_BASE + 20,
292                 .flags          = IORESOURCE_IRQ,
293         }, {            /* PIO IRQ */
294                 .start          = IH2_BASE + 30,
295                 .flags          = IORESOURCE_IRQ,
296         }, {            /* SOF IRQ */
297                 .start          = IH2_BASE + 29,
298                 .flags          = IORESOURCE_IRQ,
299         },
300 };
301
302 static u64 udc_dmamask = ~(u32)0;
303
304 static struct platform_device udc_device = {
305         .name           = "omap_udc",
306         .id             = -1,
307         .dev = {
308                 .release                = usb_release,
309                 .dma_mask               = &udc_dmamask,
310                 .coherent_dma_mask      = 0xffffffff,
311         },
312         .num_resources  = ARRAY_SIZE(udc_resources),
313         .resource       = udc_resources,
314 };
315
316 #endif
317
318 #if     defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
319
320 /* The dmamask must be set for OHCI to work */
321 static u64 ohci_dmamask = ~(u32)0;
322
323 static struct resource ohci_resources[] = {
324         {
325                 .start  = IO_ADDRESS(OMAP_OHCI_BASE),
326                 .end    = IO_ADDRESS(OMAP_OHCI_BASE + 4096),
327                 .flags  = IORESOURCE_MEM,
328         },
329         {
330                 .start  = INT_USB_HHC_1,
331                 .flags  = IORESOURCE_IRQ,
332         },
333 };
334
335 static struct platform_device ohci_device = {
336         .name                   = "ohci",
337         .id                     = -1,
338         .dev = {
339                 .release                = usb_release,
340                 .dma_mask               = &ohci_dmamask,
341                 .coherent_dma_mask      = 0xffffffff,
342         },
343         .num_resources  = ARRAY_SIZE(ohci_resources),
344         .resource               = ohci_resources,
345 };
346
347 #endif
348
349 #if     defined(CONFIG_USB_OTG) && defined(CONFIG_ARCH_OMAP_OTG)
350
351 static struct resource otg_resources[] = {
352         /* order is significant! */
353         {
354                 .start          = IO_ADDRESS(OTG_BASE),
355                 .end            = IO_ADDRESS(OTG_BASE + 0xff),
356                 .flags          = IORESOURCE_MEM,
357         }, {
358                 .start          = IH2_BASE + 8,
359                 .flags          = IORESOURCE_IRQ,
360         },
361 };
362
363 static struct platform_device otg_device = {
364         .name           = "omap_otg",
365         .id             = -1,
366         .dev = {
367                 .release                = usb_release,
368         },
369         .num_resources  = ARRAY_SIZE(otg_resources),
370         .resource       = otg_resources,
371 };
372
373 #endif
374
375 /*-------------------------------------------------------------------------*/
376
377 // FIXME correct answer depends on hmc_mode,
378 // as does any nonzero value for config->otg port number
379 #ifdef  CONFIG_USB_GADGET_OMAP
380 #define is_usb0_device(config)  1
381 #else
382 #define is_usb0_device(config)  0
383 #endif
384
385 /*-------------------------------------------------------------------------*/
386
387 #ifdef  CONFIG_ARCH_OMAP_OTG
388
389 void __init
390 omap_otg_init(struct omap_usb_config *config)
391 {
392         u32             syscon = OTG_SYSCON_1_REG & 0xffff;
393         int             status;
394         int             alt_pingroup = 0;
395
396         /* NOTE:  no bus or clock setup (yet?) */
397
398         syscon = OTG_SYSCON_1_REG & 0xffff;
399         if (!(syscon & OTG_RESET_DONE))
400                 pr_debug("USB resets not complete?\n");
401
402         // OTG_IRQ_EN_REG = 0;
403
404         /* pin muxing and transceiver pinouts */
405         if (config->pins[0] > 2)        /* alt pingroup 2 */
406                 alt_pingroup = 1;
407         syscon |= omap_usb0_init(config->pins[0], is_usb0_device(config));
408         syscon |= omap_usb1_init(config->pins[1]);
409         syscon |= omap_usb2_init(config->pins[2], alt_pingroup);
410         pr_debug("OTG_SYSCON_1_REG = %08x\n", syscon);
411         OTG_SYSCON_1_REG = syscon;
412
413         syscon = config->hmc_mode;
414         syscon |= USBX_SYNCHRO | (4 << 16) /* B_ASE0_BRST */;
415         if (config->otg || config->register_host)
416                 syscon |= UHOST_EN;
417 #ifdef  CONFIG_USB_OTG
418         if (config->otg)
419                 syscon |= OTG_EN;
420 #endif
421         pr_debug("OTG_SYSCON_2_REG = %08x\n", syscon);
422         OTG_SYSCON_2_REG = syscon;
423
424         printk("USB: hmc %d", config->hmc_mode);
425         if (alt_pingroup)
426                 printk(", usb2 alt %d wires", config->pins[2]);
427         else if (config->pins[0])
428                 printk(", usb0 %d wires%s", config->pins[0],
429                         is_usb0_device(config) ? " (dev)" : "");
430         if (config->pins[1])
431                 printk(", usb1 %d wires", config->pins[1]);
432         if (!alt_pingroup && config->pins[2])
433                 printk(", usb2 %d wires", config->pins[2]);
434         if (config->otg)
435                 printk(", Mini-AB on usb%d", config->otg - 1);
436         printk("\n");
437
438         /* don't clock unused USB controllers  */
439         syscon = OTG_SYSCON_1_REG;
440         syscon |= HST_IDLE_EN|DEV_IDLE_EN|OTG_IDLE_EN;
441
442 #ifdef  CONFIG_USB_GADGET_OMAP
443         if (config->otg || config->register_dev) {
444                 syscon &= ~DEV_IDLE_EN;
445                 udc_device.dev.platform_data = config;
446                 status = platform_device_register(&udc_device);
447                 if (status)
448                         pr_debug("can't register UDC device, %d\n", status);
449         }
450 #endif
451
452 #if     defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
453         if (config->otg || config->register_host) {
454                 syscon &= ~HST_IDLE_EN;
455                 ohci_device.dev.platform_data = config;
456                 status = platform_device_register(&ohci_device);
457                 if (status)
458                         pr_debug("can't register OHCI device, %d\n", status);
459         }
460 #endif
461
462 #ifdef  CONFIG_USB_OTG
463         if (config->otg) {
464                 syscon &= ~OTG_IDLE_EN;
465                 if (cpu_is_omap730())
466                         otg_resources[1].start = INT_730_USB_OTG;
467                 status = platform_device_register(&otg_device);
468                 // ...
469         }
470 #endif
471         pr_debug("OTG_SYSCON_1_REG = %08x\n", syscon);
472         OTG_SYSCON_1_REG = syscon;
473
474         status = 0;
475 }
476
477 #else
478 static inline void omap_otg_init(struct omap_usb_config *config) {}
479 #endif
480
481 /*-------------------------------------------------------------------------*/
482
483 #ifdef  CONFIG_ARCH_OMAP1510
484
485 #define ULPD_SOFT_REQ_REG       __REG16(ULPD_SOFT_REQ)
486 #define SOFT_UDC_REQ            (1 << 4)
487 #define SOFT_DPLL_REQ           (1 << 0)
488
489 #define ULPD_DPLL_CTRL_REG      __REG16(ULPD_DPLL_CTRL)
490 #define DPLL_IOB                (1 << 13)
491 #define DPLL_PLL_ENABLE         (1 << 4)
492 #define DPLL_LOCK               (1 << 0)
493
494 #define ULPD_APLL_CTRL_REG      __REG16(ULPD_APLL_CTRL)
495 #define APLL_NDPLL_SWITCH       (1 << 0)
496
497
498 static void __init omap_1510_usb_init(struct omap_usb_config *config)
499 {
500         int status;
501         unsigned int val;
502
503         omap_usb0_init(config->pins[0], is_usb0_device(config));
504         omap_usb1_init(config->pins[1]);
505         omap_usb2_init(config->pins[2], 0);
506
507         val = omap_readl(MOD_CONF_CTRL_0) & ~(0x3f << 1);
508         val |= (config->hmc_mode << 1);
509         omap_writel(val, MOD_CONF_CTRL_0);
510
511         printk("USB: hmc %d", config->hmc_mode);
512         if (config->pins[0])
513                 printk(", usb0 %d wires%s", config->pins[0],
514                         is_usb0_device(config) ? " (dev)" : "");
515         if (config->pins[1])
516                 printk(", usb1 %d wires", config->pins[1]);
517         if (config->pins[2])
518                 printk(", usb2 %d wires", config->pins[2]);
519         printk("\n");
520
521         /* use DPLL for 48 MHz function clock */
522         pr_debug("APLL %04x DPLL %04x REQ %04x\n", ULPD_APLL_CTRL_REG,
523                         ULPD_DPLL_CTRL_REG, ULPD_SOFT_REQ_REG);
524         ULPD_APLL_CTRL_REG &= ~APLL_NDPLL_SWITCH;
525         ULPD_DPLL_CTRL_REG |= DPLL_IOB | DPLL_PLL_ENABLE;
526         ULPD_SOFT_REQ_REG |= SOFT_UDC_REQ | SOFT_DPLL_REQ;
527         while (!(ULPD_DPLL_CTRL_REG & DPLL_LOCK))
528                 cpu_relax();
529
530 #ifdef  CONFIG_USB_GADGET_OMAP
531         if (config->register_dev) {
532                 udc_device.dev.platform_data = config;
533                 status = platform_device_register(&udc_device);
534                 if (status)
535                         pr_debug("can't register UDC device, %d\n", status);
536                 /* udc driver gates 48MHz by D+ pullup */
537         }
538 #endif
539
540 #if     defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
541         if (config->register_host) {
542                 ohci_device.dev.platform_data = config;
543                 status = platform_device_register(&ohci_device);
544                 if (status)
545                         pr_debug("can't register OHCI device, %d\n", status);
546                 /* hcd explicitly gates 48MHz */
547         }
548 #endif
549
550 }
551
552 #else
553 static inline void omap_1510_usb_init(struct omap_usb_config *config) {}
554 #endif
555
556 /*-------------------------------------------------------------------------*/
557
558 static struct omap_usb_config platform_data;
559
560 static int __init
561 omap_usb_init(void)
562 {
563         const struct omap_usb_config *config;
564
565         config = omap_get_config(OMAP_TAG_USB, struct omap_usb_config);
566         if (config == NULL) {
567                 printk(KERN_ERR "USB: No board-specific platform config found\n");
568                 return -ENODEV;
569         }
570         platform_data = *config;
571
572         if (cpu_is_omap730() || cpu_is_omap16xx())
573                 omap_otg_init(&platform_data);
574         else if (cpu_is_omap15xx())
575                 omap_1510_usb_init(&platform_data);
576         else {
577                 printk(KERN_ERR "USB: No init for your chip yet\n");
578                 return -ENODEV;
579         }
580         return 0;
581 }
582
583 subsys_initcall(omap_usb_init);