This commit was manufactured by cvs2svn to create branch 'vserver'.
[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)
52  *  - 1510 Innovator with built-in transceiver (custom cable feeding 5V VBUS)
53  *  - 1710 custom development board using alternate pin group
54  */
55
56 /*-------------------------------------------------------------------------*/
57
58 #ifdef  CONFIG_ARCH_OMAP_OTG
59
60 static struct otg_transceiver *xceiv;
61
62 /**
63  * otg_get_transceiver - find the (single) OTG transceiver driver
64  *
65  * Returns the transceiver driver, after getting a refcount to it; or
66  * null if there is no such transceiver.  The caller is responsible for
67  * releasing that count.
68  */
69 struct otg_transceiver *otg_get_transceiver(void)
70 {
71         if (xceiv)
72                 get_device(xceiv->dev);
73         return xceiv;
74 }
75 EXPORT_SYMBOL(otg_get_transceiver);
76
77 int otg_set_transceiver(struct otg_transceiver *x)
78 {
79         if (xceiv && x)
80                 return -EBUSY;
81         xceiv = x;
82         return 0;
83 }
84 EXPORT_SYMBOL(otg_set_transceiver);
85
86 #endif
87
88 /*-------------------------------------------------------------------------*/
89
90 static u32 __init omap_usb0_init(unsigned nwires, unsigned is_device)
91 {
92         u32     syscon1 = 0;
93
94         if (nwires == 0) {
95                 USB_TRANSCEIVER_CTRL_REG &= ~(1 << 3);
96                 return 0;
97         }
98
99         /*
100          * VP and VM are needed for all active usb0 configurations.
101          * USB0_VP and USB0_VM are always set on 1510, there's no muxing
102          * available for them.
103          */
104         if (nwires >= 2 && !cpu_is_omap1510()) {
105                 omap_cfg_reg(AA9_USB0_VP);
106                 omap_cfg_reg(R9_USB0_VM);
107         }
108
109         /* internal transceiver */
110         if (nwires == 2) {
111                 if (cpu_is_omap1510()) {
112                         /* This works for OHCI on 1510-Innovator, nothing to mux */
113                         return 0;
114                 }
115
116 #if 0
117                 /* NOTE:  host OR device mode for now, no OTG */
118                 USB_TRANSCEIVER_CTRL_REG &= ~(3 << 4);
119                 if (is_device) {
120                         omap_cfg_reg(W4_USB_PUEN);
121                         omap_cfg_reg(R18_1510_USB_GPIO0);
122                         // omap_cfg_reg(USB0_VBUS);
123                         // omap_cfg_reg(USB0_PUEN);
124                         // USB_TRANSCEIVER_CTRL_REG.CONF_USB0_PORT_R = 7
125                         // when USB0_PUEN is needed
126                 } else /* host mode needs D+ and D- pulldowns */
127                         USB_TRANSCEIVER_CTRL_REG &= ~(3 << 1);
128                 return 3 << 16;
129 #else
130                 /* FIXME: 1610 needs to return the right value here */
131                 printk(KERN_ERR "usb0 internal transceiver, nyet\n");
132                 return 0;
133 #endif
134         }
135
136         /* alternate pin config, external transceiver */
137         omap_cfg_reg(V6_USB0_TXD);
138         omap_cfg_reg(W9_USB0_TXEN);
139         omap_cfg_reg(W5_USB0_SE0);
140
141 #ifdef CONFIG_ARCH_OMAP_USB_SPEED
142         /* FIXME: there's good chance that pin V9 is used for MMC2 port cmddir */
143         omap_cfg_reg(V9_USB0_SPEED);
144         // omap_cfg_reg(V9_USB0_SUSP);
145 #endif
146
147         if (nwires != 3)
148                 omap_cfg_reg(Y5_USB0_RCV);
149
150         switch (nwires) {
151         case 3:
152                 syscon1 = 2;
153                 break;
154         case 4:
155                 syscon1 = 1;
156                 break;
157         case 6:
158                 syscon1 = 3;
159                 /* REVISIT: Is CONF_USB2_UNI_R only needed when nwires = 6? */
160                 USB_TRANSCEIVER_CTRL_REG |= CONF_USB2_UNI_R;
161                 break;
162         default:
163                 printk(KERN_ERR "illegal usb%d %d-wire transceiver\n",
164                         0, nwires);
165         }
166         return syscon1 << 16;
167 }
168
169 static u32 __init omap_usb1_init(unsigned nwires)
170 {
171         u32     syscon1 = 0;
172
173         if (nwires != 6)
174                 USB_TRANSCEIVER_CTRL_REG &= ~CONF_USB1_UNI_R;
175         if (nwires == 0)
176                 return 0;
177
178         /* external transceiver */
179         omap_cfg_reg(USB1_TXD);
180         omap_cfg_reg(USB1_TXEN);
181         if (cpu_is_omap1510()) {
182                 omap_cfg_reg(USB1_SEO);
183                 omap_cfg_reg(USB1_SPEED);
184                 // SUSP
185         } else if (cpu_is_omap1610() || cpu_is_omap5912() || cpu_is_omap1710()) {
186                 omap_cfg_reg(W13_1610_USB1_SE0);
187                 omap_cfg_reg(R13_1610_USB1_SPEED);
188                 // SUSP
189         } else {
190                 pr_debug("usb unrecognized\n");
191         }
192         if (nwires != 3)
193                 omap_cfg_reg(USB1_RCV);
194
195         switch (nwires) {
196         case 3:
197                 syscon1 = 2;
198                 break;
199         case 4:
200                 syscon1 = 1;
201                 break;
202         case 6:
203                 syscon1 = 3;
204                 omap_cfg_reg(USB1_VP);
205                 omap_cfg_reg(USB1_VM);
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)
220                 return 0;
221         if (nwires != 6)
222                 USB_TRANSCEIVER_CTRL_REG &= ~CONF_USB2_UNI_R;
223         if (nwires == 0)
224                 return 0;
225
226         /* external transceiver */
227         if (cpu_is_omap1510()) {
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         } else if (cpu_is_omap1610() || cpu_is_omap5912() || cpu_is_omap1710()) {
234                 omap_cfg_reg(V6_USB2_TXD);
235                 omap_cfg_reg(W9_USB2_TXEN);
236                 omap_cfg_reg(W5_USB2_SE0);
237                 if (nwires != 3)
238                         omap_cfg_reg(Y5_USB2_RCV);
239         } else {
240                 pr_debug("usb unrecognized\n");
241         }
242         // omap_cfg_reg(USB2_SUSP);
243         // FIXME omap_cfg_reg(USB2_SPEED);
244
245         switch (nwires) {
246         case 3:
247                 syscon1 = 2;
248                 break;
249         case 4:
250                 syscon1 = 1;
251                 break;
252         case 6:
253                 syscon1 = 3;
254                 if (cpu_is_omap1510()) {
255                         omap_cfg_reg(USB2_VP);
256                         omap_cfg_reg(USB2_VM);
257                 } else {
258                         omap_cfg_reg(AA9_USB2_VP);
259                         omap_cfg_reg(R9_USB2_VM);
260                 }
261                 USB_TRANSCEIVER_CTRL_REG |= CONF_USB2_UNI_R;
262                 break;
263         default:
264                 printk(KERN_ERR "illegal usb%d %d-wire transceiver\n",
265                         2, nwires);
266         }
267         return syscon1 << 24;
268 }
269
270 /*-------------------------------------------------------------------------*/
271
272 #if     defined(CONFIG_USB_GADGET_OMAP) || \
273         defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE) || \
274         (defined(CONFIG_USB_OTG) && defined(CONFIG_ARCH_OMAP_OTG))
275 static void usb_release(struct device *dev)
276 {
277         /* normally not freed */
278 }
279 #endif
280
281 #ifdef  CONFIG_USB_GADGET_OMAP
282
283 static struct resource udc_resources[] = {
284         /* order is significant! */
285         {               /* registers */
286                 .start          = IO_ADDRESS(UDC_BASE),
287                 .end            = IO_ADDRESS(UDC_BASE + 0xff),
288                 .flags          = IORESOURCE_MEM,
289         }, {            /* general IRQ */
290                 .start          = IH2_BASE + 20,
291                 .flags          = IORESOURCE_IRQ,
292         }, {            /* PIO IRQ */
293                 .start          = IH2_BASE + 30,
294                 .flags          = IORESOURCE_IRQ,
295         }, {            /* SOF IRQ */
296                 .start          = IH2_BASE + 29,
297                 .flags          = IORESOURCE_IRQ,
298         },
299 };
300
301 static u64 udc_dmamask = ~(u32)0;
302
303 static struct platform_device udc_device = {
304         .name           = "omap_udc",
305         .id             = -1,
306         .dev = {
307                 .release                = usb_release,
308                 .dma_mask               = &udc_dmamask,
309                 .coherent_dma_mask      = 0xffffffff,
310         },
311         .num_resources  = ARRAY_SIZE(udc_resources),
312         .resource       = udc_resources,
313 };
314
315 #endif
316
317 #if     defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
318
319 /* The dmamask must be set for OHCI to work */
320 static u64 ohci_dmamask = ~(u32)0;
321
322 static struct resource ohci_resources[] = {
323         {
324                 .start  = IO_ADDRESS(OMAP_OHCI_BASE),
325                 .end    = IO_ADDRESS(OMAP_OHCI_BASE + 4096),
326                 .flags  = IORESOURCE_MEM,
327         },
328         {
329                 .start  = INT_USB_HHC_1,
330                 .flags  = IORESOURCE_IRQ,
331         },
332 };
333
334 static struct platform_device ohci_device = {
335         .name                   = "ohci",
336         .id                     = -1,
337         .dev = {
338                 .release                = usb_release,
339                 .dma_mask               = &ohci_dmamask,
340                 .coherent_dma_mask      = 0x0fffffff,
341         },
342         .num_resources  = ARRAY_SIZE(ohci_resources),
343         .resource               = ohci_resources,
344 };
345
346 #endif
347
348 #if     defined(CONFIG_USB_OTG) && defined(CONFIG_ARCH_OMAP_OTG)
349
350 static struct resource otg_resources[] = {
351         /* order is significant! */
352         {
353                 .start          = IO_ADDRESS(OTG_BASE),
354                 .end            = IO_ADDRESS(OTG_BASE + 0xff),
355                 .flags          = IORESOURCE_MEM,
356         }, {
357                 .start          = IH2_BASE + 8,
358                 .flags          = IORESOURCE_IRQ,
359         },
360 };
361
362 static struct platform_device otg_device = {
363         .name           = "omap_otg",
364         .id             = -1,
365         .dev = {
366                 .release                = usb_release,
367         },
368         .num_resources  = ARRAY_SIZE(otg_resources),
369         .resource       = otg_resources,
370 };
371
372 #endif
373
374 /*-------------------------------------------------------------------------*/
375
376 // FIXME correct answer depends on hmc_mode,
377 // as does any nonzero value for config->otg port number
378 #define is_usb0_device(config)  0
379
380 /*-------------------------------------------------------------------------*/
381
382 #ifdef  CONFIG_ARCH_OMAP_OTG
383
384 void __init
385 omap_otg_init(struct omap_usb_config *config)
386 {
387         u32             syscon = OTG_SYSCON_1_REG & 0xffff;
388         int             status;
389         int             alt_pingroup = 0;
390
391         /* NOTE:  no bus or clock setup (yet?) */
392
393         syscon = OTG_SYSCON_1_REG & 0xffff;
394         if (!(syscon & OTG_RESET_DONE))
395                 pr_debug("USB resets not complete?\n");
396
397         // OTG_IRQ_EN_REG = 0;
398
399         /* pin muxing and transceiver pinouts */
400         if (config->pins[0] > 2)        /* alt pingroup 2 */
401                 alt_pingroup = 1;
402         syscon |= omap_usb0_init(config->pins[0], is_usb0_device(config));
403         syscon |= omap_usb1_init(config->pins[1]);
404         syscon |= omap_usb2_init(config->pins[2], alt_pingroup);
405         pr_debug("OTG_SYSCON_1_REG = %08x\n", syscon);
406         OTG_SYSCON_1_REG = syscon;
407
408         syscon = config->hmc_mode;
409         syscon |= USBX_SYNCHRO | (4 << 16) /* B_ASE0_BRST */;
410         if (config->otg || config->register_host)
411                 syscon |= UHOST_EN;
412 #ifdef  CONFIG_USB_OTG
413         if (config->otg)
414                 syscon |= OTG_EN;
415 #endif
416         pr_debug("OTG_SYSCON_2_REG = %08x\n", syscon);
417         OTG_SYSCON_2_REG = syscon;
418
419         printk("USB: hmc %d", config->hmc_mode);
420         if (alt_pingroup)
421                 printk(", usb2 alt %d wires", config->pins[2]);
422         else if (config->pins[0])
423                 printk(", usb0 %d wires%s", config->pins[2],
424                         is_usb0_device(config) ? " (dev)" : "");
425         if (config->pins[1])
426                 printk(", usb1 %d wires", config->pins[1]);
427         if (!alt_pingroup && config->pins[2])
428                 printk(", usb2 %d wires", config->pins[2]);
429         if (config->otg)
430                 printk(", Mini-AB on usb%d", config->otg - 1);
431         printk("\n");
432
433         /* don't clock unused USB controllers  */
434         syscon = OTG_SYSCON_1_REG;
435         syscon |= HST_IDLE_EN|DEV_IDLE_EN|OTG_IDLE_EN;
436
437 #ifdef  CONFIG_USB_GADGET_OMAP
438         if (config->otg || config->register_dev) {
439                 syscon &= ~DEV_IDLE_EN;
440                 udc_device.dev.platform_data = config;
441                 status = platform_device_register(&udc_device);
442                 if (status)
443                         pr_debug("can't register UDC device, %d\n", status);
444         }
445 #endif
446
447 #if     defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
448         if (config->otg || config->register_host) {
449                 syscon &= ~HST_IDLE_EN;
450                 ohci_device.dev.platform_data = config;
451                 status = platform_device_register(&ohci_device);
452                 if (status)
453                         pr_debug("can't register OHCI device, %d\n", status);
454         }
455 #endif
456
457 #ifdef  CONFIG_USB_OTG
458         if (config->otg) {
459                 syscon &= ~OTG_IDLE_EN;
460                 if (cpu_is_omap730())
461                         otg_resources[1].start = INT_730_USB_OTG;
462                 status = platform_device_register(&otg_device);
463                 // ...
464         }
465 #endif
466         pr_debug("OTG_SYSCON_1_REG = %08x\n", syscon);
467         OTG_SYSCON_1_REG = syscon;
468
469         status = 0;
470 }
471
472 #else
473 static inline void omap_otg_init(struct omap_usb_config *config) {}
474 #endif
475
476 /*-------------------------------------------------------------------------*/
477
478 #ifdef  CONFIG_ARCH_OMAP1510
479
480 static void __init omap_1510_usb_init(struct omap_usb_config *config)
481 {
482         int status;
483         unsigned int val;
484
485         omap_usb0_init(config->pins[0], is_usb0_device(config));
486         omap_usb1_init(config->pins[1]);
487         omap_usb2_init(config->pins[2], 0);
488
489         val = omap_readl(MOD_CONF_CTRL_0) & ~(0x3f << 1);
490         val |= (config->hmc_mode << 1);
491         omap_writel(val, MOD_CONF_CTRL_0);
492
493         // FIXME this has a UDC controller too
494
495 #if     defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
496         if (config->otg || config->register_host) {
497                 ohci_device.dev.platform_data = config;
498                 status = platform_device_register(&ohci_device);
499                 if (status)
500                         pr_debug("can't register OHCI device, %d\n", status);
501         }
502         // FIXME completely untested ...
503 #endif
504
505 }
506
507 #else
508 static inline void omap_1510_usb_init(struct omap_usb_config *config) {}
509 #endif
510
511 /*-------------------------------------------------------------------------*/
512
513 static struct omap_usb_config platform_data;
514
515 static int __init
516 omap_usb_init(void)
517 {
518         const struct omap_usb_config *config;
519
520         config = omap_get_config(OMAP_TAG_USB, struct omap_usb_config);
521         if (config == NULL) {
522                 printk(KERN_ERR "USB: No board-specific platform config found\n");
523                 return -ENODEV;
524         }
525         platform_data = *config;
526
527         if (cpu_is_omap730()
528                         || cpu_is_omap1610()
529                         || cpu_is_omap1710()
530                         || cpu_is_omap5912())
531                 omap_otg_init(&platform_data);
532         else if (cpu_is_omap1510())
533                 omap_1510_usb_init(&platform_data);
534         else {
535                 printk(KERN_ERR "USB: No init for your chip yet\n");
536                 return -ENODEV;
537         }
538         return 0;
539 }
540
541 subsys_initcall(omap_usb_init);