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