Merge master.kernel.org:/pub/scm/linux/kernel/git/wim/linux-2.6-watchdog
[linux-2.6] / arch / arm / plat-omap / usb.c
1 /*
2  * arch/arm/plat-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
37 #include <asm/arch/mux.h>
38 #include <asm/arch/usb.h>
39 #include <asm/arch/board.h>
40
41 /* These routines should handle the standard chip-specific modes
42  * for usb0/1/2 ports, covering basic mux and transceiver setup.
43  * Call omap_usb_init() once, from INIT_MACHINE().
44  *
45  * Some board-*.c files will need to set up additional mux options,
46  * like for suspend handling, vbus sensing, GPIOs, and the D+ pullup.
47  */
48
49 /* TESTED ON:
50  *  - 1611B H2 (with usb1 mini-AB) using standard Mini-B or OTG cables
51  *  - 5912 OSK OHCI (with usb0 standard-A), standard A-to-B cables
52  *  - 5912 OSK UDC, with *nonstandard* A-to-A cable
53  *  - 1510 Innovator UDC with bundled usb0 cable
54  *  - 1510 Innovator OHCI with bundled usb1/usb2 cable
55  *  - 1510 Innovator OHCI with custom usb0 cable, feeding 5V VBUS
56  *  - 1710 custom development board using alternate pin group
57  *  - 1710 H3 (with usb1 mini-AB) using standard Mini-B or OTG cables
58  */
59
60 /*-------------------------------------------------------------------------*/
61
62 #ifdef  CONFIG_ARCH_OMAP_OTG
63
64 static struct otg_transceiver *xceiv;
65
66 /**
67  * otg_get_transceiver - find the (single) OTG transceiver driver
68  *
69  * Returns the transceiver driver, after getting a refcount to it; or
70  * null if there is no such transceiver.  The caller is responsible for
71  * releasing that count.
72  */
73 struct otg_transceiver *otg_get_transceiver(void)
74 {
75         if (xceiv)
76                 get_device(xceiv->dev);
77         return xceiv;
78 }
79 EXPORT_SYMBOL(otg_get_transceiver);
80
81 int otg_set_transceiver(struct otg_transceiver *x)
82 {
83         if (xceiv && x)
84                 return -EBUSY;
85         xceiv = x;
86         return 0;
87 }
88 EXPORT_SYMBOL(otg_set_transceiver);
89
90 #endif
91
92 /*-------------------------------------------------------------------------*/
93
94 static u32 __init omap_usb0_init(unsigned nwires, unsigned is_device)
95 {
96         u32     syscon1 = 0;
97
98         if (nwires == 0) {
99                 if (!cpu_is_omap15xx()) {
100                         /* pulldown D+/D- */
101                         USB_TRANSCEIVER_CTRL_REG &= ~(3 << 1);
102                 }
103                 return 0;
104         }
105
106         if (is_device)
107                 omap_cfg_reg(W4_USB_PUEN);
108
109         /* internal transceiver */
110         if (nwires == 2) {
111                 // omap_cfg_reg(P9_USB_DP);
112                 // omap_cfg_reg(R8_USB_DM);
113
114                 if (cpu_is_omap15xx()) {
115                         /* This works on 1510-Innovator */
116                         return 0;
117                 }
118
119                 /* NOTES:
120                  *  - peripheral should configure VBUS detection!
121                  *  - only peripherals may use the internal D+/D- pulldowns
122                  *  - OTG support on this port not yet written
123                  */
124
125                 USB_TRANSCEIVER_CTRL_REG &= ~(7 << 4);
126                 if (!is_device)
127                         USB_TRANSCEIVER_CTRL_REG |= (3 << 1);
128
129                 return 3 << 16;
130         }
131
132         /* alternate pin config, external transceiver */
133         if (cpu_is_omap15xx()) {
134                 printk(KERN_ERR "no usb0 alt pin config on 15xx\n");
135                 return 0;
136         }
137
138         omap_cfg_reg(V6_USB0_TXD);
139         omap_cfg_reg(W9_USB0_TXEN);
140         omap_cfg_reg(W5_USB0_SE0);
141
142         /* NOTE:  SPEED and SUSP aren't configured here */
143
144         if (nwires != 3)
145                 omap_cfg_reg(Y5_USB0_RCV);
146         if (nwires != 6)
147                 USB_TRANSCEIVER_CTRL_REG &= ~CONF_USB2_UNI_R;
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                 omap_cfg_reg(AA9_USB0_VP);
159                 omap_cfg_reg(R9_USB0_VM);
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 && !cpu_is_omap15xx())
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_omap15xx()) {
182                 omap_cfg_reg(USB1_SEO);
183                 omap_cfg_reg(USB1_SPEED);
184                 // SUSP
185         } else if (cpu_is_omap1610() || cpu_is_omap5912()) {
186                 omap_cfg_reg(W13_1610_USB1_SE0);
187                 omap_cfg_reg(R13_1610_USB1_SPEED);
188                 // SUSP
189         } else if (cpu_is_omap1710()) {
190                 omap_cfg_reg(R13_1710_USB1_SE0);
191                 // SUSP
192         } else {
193                 pr_debug("usb unrecognized\n");
194         }
195         if (nwires != 3)
196                 omap_cfg_reg(USB1_RCV);
197
198         switch (nwires) {
199         case 3:
200                 syscon1 = 2;
201                 break;
202         case 4:
203                 syscon1 = 1;
204                 break;
205         case 6:
206                 syscon1 = 3;
207                 omap_cfg_reg(USB1_VP);
208                 omap_cfg_reg(USB1_VM);
209                 if (!cpu_is_omap15xx())
210                         USB_TRANSCEIVER_CTRL_REG |= CONF_USB1_UNI_R;
211                 break;
212         default:
213                 printk(KERN_ERR "illegal usb%d %d-wire transceiver\n",
214                         1, nwires);
215         }
216         return syscon1 << 20;
217 }
218
219 static u32 __init omap_usb2_init(unsigned nwires, unsigned alt_pingroup)
220 {
221         u32     syscon1 = 0;
222
223         /* NOTE erratum: must leave USB2_UNI_R set if usb0 in use */
224         if (alt_pingroup || nwires == 0)
225                 return 0;
226         if (nwires != 6 && !cpu_is_omap15xx())
227                 USB_TRANSCEIVER_CTRL_REG &= ~CONF_USB2_UNI_R;
228
229         /* external transceiver */
230         if (cpu_is_omap15xx()) {
231                 omap_cfg_reg(USB2_TXD);
232                 omap_cfg_reg(USB2_TXEN);
233                 omap_cfg_reg(USB2_SEO);
234                 if (nwires != 3)
235                         omap_cfg_reg(USB2_RCV);
236                 /* there is no USB2_SPEED */
237         } else if (cpu_is_omap16xx()) {
238                 omap_cfg_reg(V6_USB2_TXD);
239                 omap_cfg_reg(W9_USB2_TXEN);
240                 omap_cfg_reg(W5_USB2_SE0);
241                 if (nwires != 3)
242                         omap_cfg_reg(Y5_USB2_RCV);
243                 // FIXME omap_cfg_reg(USB2_SPEED);
244         } else {
245                 pr_debug("usb unrecognized\n");
246         }
247         // omap_cfg_reg(USB2_SUSP);
248
249         switch (nwires) {
250         case 3:
251                 syscon1 = 2;
252                 break;
253         case 4:
254                 syscon1 = 1;
255                 break;
256         case 6:
257                 syscon1 = 3;
258                 if (cpu_is_omap15xx()) {
259                         omap_cfg_reg(USB2_VP);
260                         omap_cfg_reg(USB2_VM);
261                 } else {
262                         omap_cfg_reg(AA9_USB2_VP);
263                         omap_cfg_reg(R9_USB2_VM);
264                         USB_TRANSCEIVER_CTRL_REG |= CONF_USB2_UNI_R;
265                 }
266                 break;
267         default:
268                 printk(KERN_ERR "illegal usb%d %d-wire transceiver\n",
269                         2, nwires);
270         }
271         return syscon1 << 24;
272 }
273
274 /*-------------------------------------------------------------------------*/
275
276 #if     defined(CONFIG_USB_GADGET_OMAP) || \
277         defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE) || \
278         (defined(CONFIG_USB_OTG) && defined(CONFIG_ARCH_OMAP_OTG))
279 static void usb_release(struct device *dev)
280 {
281         /* normally not freed */
282 }
283 #endif
284
285 #ifdef  CONFIG_USB_GADGET_OMAP
286
287 static struct resource udc_resources[] = {
288         /* order is significant! */
289         {               /* registers */
290                 .start          = UDC_BASE,
291                 .end            = UDC_BASE + 0xff,
292                 .flags          = IORESOURCE_MEM,
293         }, {            /* general IRQ */
294                 .start          = IH2_BASE + 20,
295                 .flags          = IORESOURCE_IRQ,
296         }, {            /* PIO IRQ */
297                 .start          = IH2_BASE + 30,
298                 .flags          = IORESOURCE_IRQ,
299         }, {            /* SOF IRQ */
300                 .start          = IH2_BASE + 29,
301                 .flags          = IORESOURCE_IRQ,
302         },
303 };
304
305 static u64 udc_dmamask = ~(u32)0;
306
307 static struct platform_device udc_device = {
308         .name           = "omap_udc",
309         .id             = -1,
310         .dev = {
311                 .release                = usb_release,
312                 .dma_mask               = &udc_dmamask,
313                 .coherent_dma_mask      = 0xffffffff,
314         },
315         .num_resources  = ARRAY_SIZE(udc_resources),
316         .resource       = udc_resources,
317 };
318
319 #endif
320
321 #if     defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
322
323 /* The dmamask must be set for OHCI to work */
324 static u64 ohci_dmamask = ~(u32)0;
325
326 static struct resource ohci_resources[] = {
327         {
328                 .start  = OMAP_OHCI_BASE,
329                 .end    = OMAP_OHCI_BASE + 4096 - 1,
330                 .flags  = IORESOURCE_MEM,
331         },
332         {
333                 .start  = INT_USB_HHC_1,
334                 .flags  = IORESOURCE_IRQ,
335         },
336 };
337
338 static struct platform_device ohci_device = {
339         .name                   = "ohci",
340         .id                     = -1,
341         .dev = {
342                 .release                = usb_release,
343                 .dma_mask               = &ohci_dmamask,
344                 .coherent_dma_mask      = 0xffffffff,
345         },
346         .num_resources  = ARRAY_SIZE(ohci_resources),
347         .resource               = ohci_resources,
348 };
349
350 #endif
351
352 #if     defined(CONFIG_USB_OTG) && defined(CONFIG_ARCH_OMAP_OTG)
353
354 static struct resource otg_resources[] = {
355         /* order is significant! */
356         {
357                 .start          = OTG_BASE,
358                 .end            = OTG_BASE + 0xff,
359                 .flags          = IORESOURCE_MEM,
360         }, {
361                 .start          = IH2_BASE + 8,
362                 .flags          = IORESOURCE_IRQ,
363         },
364 };
365
366 static struct platform_device otg_device = {
367         .name           = "omap_otg",
368         .id             = -1,
369         .dev = {
370                 .release                = usb_release,
371         },
372         .num_resources  = ARRAY_SIZE(otg_resources),
373         .resource       = otg_resources,
374 };
375
376 #endif
377
378 /*-------------------------------------------------------------------------*/
379
380 #define ULPD_CLOCK_CTRL_REG     __REG16(ULPD_CLOCK_CTRL)
381 #define ULPD_SOFT_REQ_REG       __REG16(ULPD_SOFT_REQ)
382
383
384 // FIXME correct answer depends on hmc_mode,
385 // as does any nonzero value for config->otg port number
386 #ifdef  CONFIG_USB_GADGET_OMAP
387 #define is_usb0_device(config)  1
388 #else
389 #define is_usb0_device(config)  0
390 #endif
391
392 /*-------------------------------------------------------------------------*/
393
394 #ifdef  CONFIG_ARCH_OMAP_OTG
395
396 void __init
397 omap_otg_init(struct omap_usb_config *config)
398 {
399         u32             syscon = OTG_SYSCON_1_REG & 0xffff;
400         int             status;
401         int             alt_pingroup = 0;
402
403         /* NOTE:  no bus or clock setup (yet?) */
404
405         syscon = OTG_SYSCON_1_REG & 0xffff;
406         if (!(syscon & OTG_RESET_DONE))
407                 pr_debug("USB resets not complete?\n");
408
409         // OTG_IRQ_EN_REG = 0;
410
411         /* pin muxing and transceiver pinouts */
412         if (config->pins[0] > 2)        /* alt pingroup 2 */
413                 alt_pingroup = 1;
414         syscon |= omap_usb0_init(config->pins[0], is_usb0_device(config));
415         syscon |= omap_usb1_init(config->pins[1]);
416         syscon |= omap_usb2_init(config->pins[2], alt_pingroup);
417         pr_debug("OTG_SYSCON_1_REG = %08x\n", syscon);
418         OTG_SYSCON_1_REG = syscon;
419
420         syscon = config->hmc_mode;
421         syscon |= USBX_SYNCHRO | (4 << 16) /* B_ASE0_BRST */;
422 #ifdef  CONFIG_USB_OTG
423         if (config->otg)
424                 syscon |= OTG_EN;
425 #endif
426         pr_debug("USB_TRANSCEIVER_CTRL_REG = %03x\n", USB_TRANSCEIVER_CTRL_REG);
427         pr_debug("OTG_SYSCON_2_REG = %08x\n", syscon);
428         OTG_SYSCON_2_REG = syscon;
429
430         printk("USB: hmc %d", config->hmc_mode);
431         if (alt_pingroup)
432                 printk(", usb2 alt %d wires", config->pins[2]);
433         else if (config->pins[0])
434                 printk(", usb0 %d wires%s", config->pins[0],
435                         is_usb0_device(config) ? " (dev)" : "");
436         if (config->pins[1])
437                 printk(", usb1 %d wires", config->pins[1]);
438         if (!alt_pingroup && config->pins[2])
439                 printk(", usb2 %d wires", config->pins[2]);
440         if (config->otg)
441                 printk(", Mini-AB on usb%d", config->otg - 1);
442         printk("\n");
443
444         /* leave USB clocks/controllers off until needed */
445         ULPD_SOFT_REQ_REG &= ~SOFT_USB_CLK_REQ;
446         ULPD_CLOCK_CTRL_REG &= ~USB_MCLK_EN;
447         ULPD_CLOCK_CTRL_REG |= DIS_USB_PVCI_CLK;
448         syscon = OTG_SYSCON_1_REG;
449         syscon |= HST_IDLE_EN|DEV_IDLE_EN|OTG_IDLE_EN;
450
451 #ifdef  CONFIG_USB_GADGET_OMAP
452         if (config->otg || config->register_dev) {
453                 syscon &= ~DEV_IDLE_EN;
454                 udc_device.dev.platform_data = config;
455                 /* FIXME patch IRQ numbers for omap730 */
456                 status = platform_device_register(&udc_device);
457                 if (status)
458                         pr_debug("can't register UDC device, %d\n", status);
459         }
460 #endif
461
462 #if     defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
463         if (config->otg || config->register_host) {
464                 syscon &= ~HST_IDLE_EN;
465                 ohci_device.dev.platform_data = config;
466                 if (cpu_is_omap730())
467                         ohci_resources[1].start = INT_730_USB_HHC_1;
468                 status = platform_device_register(&ohci_device);
469                 if (status)
470                         pr_debug("can't register OHCI device, %d\n", status);
471         }
472 #endif
473
474 #ifdef  CONFIG_USB_OTG
475         if (config->otg) {
476                 syscon &= ~OTG_IDLE_EN;
477                 otg_device.dev.platform_data = config;
478                 if (cpu_is_omap730())
479                         otg_resources[1].start = INT_730_USB_OTG;
480                 status = platform_device_register(&otg_device);
481                 if (status)
482                         pr_debug("can't register OTG device, %d\n", status);
483         }
484 #endif
485         pr_debug("OTG_SYSCON_1_REG = %08x\n", syscon);
486         OTG_SYSCON_1_REG = syscon;
487
488         status = 0;
489 }
490
491 #else
492 static inline void omap_otg_init(struct omap_usb_config *config) {}
493 #endif
494
495 /*-------------------------------------------------------------------------*/
496
497 #ifdef  CONFIG_ARCH_OMAP1510
498
499 #define ULPD_DPLL_CTRL_REG      __REG16(ULPD_DPLL_CTRL)
500 #define DPLL_IOB                (1 << 13)
501 #define DPLL_PLL_ENABLE         (1 << 4)
502 #define DPLL_LOCK               (1 << 0)
503
504 #define ULPD_APLL_CTRL_REG      __REG16(ULPD_APLL_CTRL)
505 #define APLL_NDPLL_SWITCH       (1 << 0)
506
507
508 static void __init omap_1510_usb_init(struct omap_usb_config *config)
509 {
510         int status;
511         unsigned int val;
512
513         omap_usb0_init(config->pins[0], is_usb0_device(config));
514         omap_usb1_init(config->pins[1]);
515         omap_usb2_init(config->pins[2], 0);
516
517         val = omap_readl(MOD_CONF_CTRL_0) & ~(0x3f << 1);
518         val |= (config->hmc_mode << 1);
519         omap_writel(val, MOD_CONF_CTRL_0);
520
521         printk("USB: hmc %d", config->hmc_mode);
522         if (config->pins[0])
523                 printk(", usb0 %d wires%s", config->pins[0],
524                         is_usb0_device(config) ? " (dev)" : "");
525         if (config->pins[1])
526                 printk(", usb1 %d wires", config->pins[1]);
527         if (config->pins[2])
528                 printk(", usb2 %d wires", config->pins[2]);
529         printk("\n");
530
531         /* use DPLL for 48 MHz function clock */
532         pr_debug("APLL %04x DPLL %04x REQ %04x\n", ULPD_APLL_CTRL_REG,
533                         ULPD_DPLL_CTRL_REG, ULPD_SOFT_REQ_REG);
534         ULPD_APLL_CTRL_REG &= ~APLL_NDPLL_SWITCH;
535         ULPD_DPLL_CTRL_REG |= DPLL_IOB | DPLL_PLL_ENABLE;
536         ULPD_SOFT_REQ_REG |= SOFT_UDC_REQ | SOFT_DPLL_REQ;
537         while (!(ULPD_DPLL_CTRL_REG & DPLL_LOCK))
538                 cpu_relax();
539
540 #ifdef  CONFIG_USB_GADGET_OMAP
541         if (config->register_dev) {
542                 udc_device.dev.platform_data = config;
543                 status = platform_device_register(&udc_device);
544                 if (status)
545                         pr_debug("can't register UDC device, %d\n", status);
546                 /* udc driver gates 48MHz by D+ pullup */
547         }
548 #endif
549
550 #if     defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
551         if (config->register_host) {
552                 ohci_device.dev.platform_data = config;
553                 status = platform_device_register(&ohci_device);
554                 if (status)
555                         pr_debug("can't register OHCI device, %d\n", status);
556                 /* hcd explicitly gates 48MHz */
557         }
558 #endif
559 }
560
561 #else
562 static inline void omap_1510_usb_init(struct omap_usb_config *config) {}
563 #endif
564
565 /*-------------------------------------------------------------------------*/
566
567 static struct omap_usb_config platform_data;
568
569 static int __init
570 omap_usb_init(void)
571 {
572         const struct omap_usb_config *config;
573
574         config = omap_get_config(OMAP_TAG_USB, struct omap_usb_config);
575         if (config == NULL) {
576                 printk(KERN_ERR "USB: No board-specific "
577                                 "platform config found\n");
578                 return -ENODEV;
579         }
580         platform_data = *config;
581
582         if (cpu_is_omap730() || cpu_is_omap16xx())
583                 omap_otg_init(&platform_data);
584         else if (cpu_is_omap15xx())
585                 omap_1510_usb_init(&platform_data);
586         else {
587                 printk(KERN_ERR "USB: No init for your chip yet\n");
588                 return -ENODEV;
589         }
590         return 0;
591 }
592
593 subsys_initcall(omap_usb_init);