Add resume support for AC3 files.
[kugel-rb.git] / firmware / usb.c
bloba8a3e6b3eb4a7291a16a0955c613c600cee0bec7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 * iPod driver based on code from the ipodlinux project - http://ipodlinux.org
13 * Adapted for Rockbox in January 2006
14 * Original file: podzilla/usb.c
15 * Copyright (C) 2005 Adam Johnston
17 * All files in this archive are subject to the GNU General Public License.
18 * See the file COPYING in the source tree root for full license agreement.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
24 #include "config.h"
25 #include "cpu.h"
26 #include "kernel.h"
27 #include "thread.h"
28 #include "system.h"
29 #include "debug.h"
30 #include "ata.h"
31 #include "fat.h"
32 #include "disk.h"
33 #include "panic.h"
34 #include "lcd.h"
35 #include "adc.h"
36 #include "usb.h"
37 #include "button.h"
38 #include "sprintf.h"
39 #include "string.h"
40 #include "hwcompat.h"
41 #ifdef HAVE_MMC
42 #include "ata_mmc.h"
43 #endif
44 #ifdef TARGET_TREE
45 #include "usb-target.h"
46 #endif
48 extern void dbg_ports(void); /* NASTY! defined in apps/ */
50 #ifdef HAVE_LCD_BITMAP
51 bool do_screendump_instead_of_usb = false;
52 void screen_dump(void); /* Nasty again. Defined in apps/ too */
53 #endif
55 #define USB_REALLY_BRAVE
57 #if !defined(SIMULATOR) && !defined(USB_NONE)
59 /* Messages from usb_tick and thread states */
60 #define USB_INSERTED 1
61 #define USB_EXTRACTED 2
62 #ifdef HAVE_MMC
63 #define USB_REENABLE 3
64 #endif
65 #ifdef HAVE_USB_POWER
66 #define USB_POWERED 4
68 #if CONFIG_KEYPAD == RECORDER_PAD
69 #define USBPOWER_BUTTON BUTTON_F1
70 #define USBPOWER_BTN_IGNORE BUTTON_ON
71 #elif CONFIG_KEYPAD == ONDIO_PAD
72 #define USBPOWER_BUTTON BUTTON_MENU
73 #define USBPOWER_BTN_IGNORE BUTTON_OFF
74 #elif (CONFIG_KEYPAD == IPOD_4G_PAD)
75 #define USBPOWER_BUTTON BUTTON_MENU
76 #define USBPOWER_BTN_IGNORE BUTTON_PLAY
77 #elif CONFIG_KEYPAD == IRIVER_H300_PAD
78 #define USBPOWER_BUTTON BUTTON_REC
79 #define USBPOWER_BTN_IGNORE BUTTON_ON
80 #endif
81 #endif /* HAVE_USB_POWER */
83 /* The ADC tick reads one channel per tick, and we want to check 3 successive
84 readings on the USB voltage channel. This doesn't apply to the Player, but
85 debouncing the USB detection port won't hurt us either. */
86 #define NUM_POLL_READINGS (NUM_ADC_CHANNELS * 3)
87 static int countdown;
89 static int usb_state;
91 #if defined(HAVE_MMC) && !defined(BOOTLOADER)
92 static int usb_mmc_countdown = 0;
93 #endif
95 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
96 needs updating */
97 #ifndef BOOTLOADER
98 static long usb_stack[(DEFAULT_STACK_SIZE + 0x800)/sizeof(long)];
99 static const char usb_thread_name[] = "usb";
100 #endif
101 static struct event_queue usb_queue;
102 static bool last_usb_status;
103 static bool usb_monitor_enabled;
105 #ifndef TARGET_TREE
106 void usb_enable(bool on)
108 #ifdef USB_ENABLE_ONDIOSTYLE
109 PACR2 &= ~0x04C0; /* use PA3, PA5 as GPIO */
110 if(on)
112 #ifdef HAVE_MMC
113 mmc_enable_int_flash_clock(!mmc_detect());
114 #endif
115 if (!(read_hw_mask() & MMC_CLOCK_POLARITY))
116 and_b(~0x20, &PBDRH); /* old circuit needs SCK1 = low while on USB */
117 or_b(0x20, &PADRL); /* enable USB */
118 and_b(~0x08, &PADRL); /* assert card detect */
120 else
122 if (!(read_hw_mask() & MMC_CLOCK_POLARITY))
123 or_b(0x20, &PBDRH); /* reset SCK1 = high for old circuit */
124 and_b(~0x20, &PADRL); /* disable USB */
125 or_b(0x08, &PADRL); /* deassert card detect */
127 or_b(0x28, &PAIORL); /* output for USB enable and card detect */
128 #elif defined(USB_GMINISTYLE)
130 int smsc_ver = smsc_version();
131 if (on) {
132 if (smsc_ver < 4) {
133 P6 &= ~0x04;
134 P10 &= ~0x20;
136 smsc_delay();
138 P6 |= 0x08;
139 P10 |= 0x20;
141 smsc_delay();
143 P6 |= 0x10;
144 } else {
145 P6 &= ~0x10;
146 if (smsc_ver < 4) {
147 P6 &= ~0x04;
148 P10 &= ~0x20;
152 #elif defined(USB_IRIVERSTYLE)
153 if(on)
155 /* Power on the Cypress chip */
156 #ifdef IRIVER_H100_SERIES
157 or_l(0x01000040, &GPIO_OUT);
158 #else
159 and_l(~0x00000008,&GPIO1_OUT);
160 #endif
161 sleep(2);
163 else
165 /* Power off the Cypress chip */
166 #ifdef IRIVER_H100_SERIES
167 and_l(~0x01000040, &GPIO_OUT);
168 #else
169 or_l(0x00000008,&GPIO1_OUT);
170 #endif
173 #elif defined(USB_IPODSTYLE)
174 /* For the ipod, we can only do one thing with USB mode - reboot
175 into Apple's flash-based disk-mode. This does not return. */
176 if (on)
178 /* The following code is copied from ipodlinux */
179 #if defined(IPOD_COLOR) || defined(IPOD_3G) || \
180 defined(IPOD_4G) || defined(IPOD_MINI)
181 unsigned char* storage_ptr = (unsigned char *)0x40017F00;
182 #elif defined(IPOD_NANO) || defined(IPOD_VIDEO) || defined(IPOD_MINI2G)
183 unsigned char* storage_ptr = (unsigned char *)0x4001FF00;
184 #endif
185 memcpy(storage_ptr, "diskmode\0\0hotstuff\0\0\1", 21);
186 DEV_RS |= 4; /* Reboot */
189 #elif defined(USB_ISP1582)
190 /* TODO: Implement USB_ISP1582 */
191 (void) on;
192 #elif defined(USB_X5STYLE)
193 /* TODO X5 */
194 #else
195 #ifdef HAVE_LCD_BITMAP
196 if(read_hw_mask() & USB_ACTIVE_HIGH)
197 on = !on;
198 #endif
199 if(on)
201 and_b(~0x04, &PADRH); /* enable USB */
203 else
205 or_b(0x04, &PADRH);
207 or_b(0x04, &PAIORH);
208 #endif
210 #endif
212 #ifndef BOOTLOADER
213 static void usb_slave_mode(bool on)
215 int rc;
217 if(on)
219 DEBUGF("Entering USB slave mode\n");
220 ata_soft_reset();
221 ata_init();
222 ata_enable(false);
223 usb_enable(true);
225 else
227 DEBUGF("Leaving USB slave mode\n");
229 /* Let the ISDx00 settle */
230 sleep(HZ*1);
232 usb_enable(false);
234 rc = ata_init();
235 if(rc)
237 /* fixme: can we remove this? (already such in main.c) */
238 char str[32];
239 lcd_clear_display();
240 snprintf(str, 31, "ATA error: %d", rc);
241 lcd_puts(0, 0, str);
242 lcd_puts(0, 1, "Press ON to debug");
243 lcd_update();
244 while(!(button_get(true) & BUTTON_REL)) {};
245 dbg_ports();
246 panicf("ata: %d",rc);
249 rc = disk_mount_all();
250 if (rc <= 0) /* no partition */
251 panicf("mount: %d",rc);
256 static void usb_thread(void)
258 int num_acks_to_expect = -1;
259 bool waiting_for_ack;
260 struct event ev;
262 waiting_for_ack = false;
264 while(1)
266 queue_wait(&usb_queue, &ev);
267 switch(ev.id)
269 case USB_INSERTED:
270 #ifdef HAVE_LCD_BITMAP
271 if(do_screendump_instead_of_usb)
273 screen_dump();
275 else
276 #endif
277 #ifdef HAVE_USB_POWER
278 if((button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON)
280 usb_state = USB_POWERED;
282 else
283 #endif
285 /* Tell all threads that they have to back off the ATA.
286 We subtract one for our own thread. */
287 num_acks_to_expect =
288 queue_broadcast(SYS_USB_CONNECTED, NULL) - 1;
289 waiting_for_ack = true;
290 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
291 num_acks_to_expect);
293 break;
295 case SYS_USB_CONNECTED_ACK:
296 if(waiting_for_ack)
298 num_acks_to_expect--;
299 if(num_acks_to_expect == 0)
301 DEBUGF("All threads have acknowledged the connect.\n");
302 #ifdef USB_REALLY_BRAVE
303 usb_slave_mode(true);
304 usb_state = USB_INSERTED;
305 cpu_idle_mode(true);
306 #else
307 system_reboot();
308 #endif
310 else
312 DEBUGF("usb: got ack, %d to go...\n",
313 num_acks_to_expect);
316 break;
318 case USB_EXTRACTED:
319 #ifdef HAVE_LCD_BITMAP
320 if(do_screendump_instead_of_usb)
321 break;
322 #endif
323 #ifdef HAVE_USB_POWER
324 if(usb_state == USB_POWERED)
326 usb_state = USB_EXTRACTED;
327 break;
329 #endif
330 if(usb_state == USB_INSERTED)
332 /* Only disable the USB mode if we really have enabled it
333 some threads might not have acknowledged the
334 insertion */
335 usb_slave_mode(false);
336 cpu_idle_mode(false);
339 usb_state = USB_EXTRACTED;
341 /* Tell all threads that we are back in business */
342 num_acks_to_expect =
343 queue_broadcast(SYS_USB_DISCONNECTED, NULL) - 1;
344 waiting_for_ack = true;
345 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
346 num_acks_to_expect);
347 #ifdef HAVE_LCD_CHARCELLS
348 lcd_icon(ICON_USB, false);
349 #endif
350 break;
352 case SYS_USB_DISCONNECTED_ACK:
353 if(waiting_for_ack)
355 num_acks_to_expect--;
356 if(num_acks_to_expect == 0)
358 DEBUGF("All threads have acknowledged. "
359 "We're in business.\n");
361 else
363 DEBUGF("usb: got ack, %d to go...\n",
364 num_acks_to_expect);
367 break;
369 #ifdef HAVE_MMC
370 case SYS_MMC_INSERTED:
371 case SYS_MMC_EXTRACTED:
372 if(usb_state == USB_INSERTED)
374 usb_enable(false);
375 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
377 break;
379 case USB_REENABLE:
380 if(usb_state == USB_INSERTED)
381 usb_enable(true); /* reenable only if still inserted */
382 break;
383 #endif
387 #endif
389 #ifndef TARGET_TREE
390 bool usb_detect(void)
392 bool current_status;
394 #ifdef USB_RECORDERSTYLE
395 current_status = (adc_read(ADC_USB_POWER) > 500)?true:false;
396 #endif
397 #ifdef USB_FMRECORDERSTYLE
398 current_status = (adc_read(ADC_USB_POWER) <= 512)?true:false;
399 #endif
400 #ifdef USB_PLAYERSTYLE
401 current_status = (PADR & 0x8000)?false:true;
402 #endif
403 #ifdef USB_IRIVERSTYLE
404 current_status = (GPIO1_READ & 0x80)?true:false;
405 #endif
406 #ifdef USB_GMINISTYLE
407 current_status = (P5 & 0x10)?true:false;
408 #endif
409 #ifdef USB_IPODSTYLE
410 /* The following check is in the ipodlinux source, with the
411 comment "USB2D_IDENT is bad" if USB2D_IDENT != 0x22FA05 */
412 if (USB2D_IDENT != 0x22FA05) {
413 return false;
415 current_status = (USB_STATUS & 0x800)?true:false;
416 #endif
417 #ifdef USB_ISP1582
418 /* TODO: Implement USB_ISP1582 */
419 current_status = false;
420 #endif
421 return current_status;
423 #endif
425 #ifndef BOOTLOADER
426 static void usb_tick(void)
428 bool current_status;
430 #ifdef USB_GMINISTYLE
431 /* Keep usb chip in usb state (?) */
432 if (P5 & 0x10) {
433 if ((P10 & 0x20) == 0 || (P6 & 0x08) == 0) {
434 if (smsc_version() < 4) {
435 P6 |= 0x08;
436 P10 |= 0x20;
440 #endif
442 if(usb_monitor_enabled)
444 current_status = usb_detect();
446 /* Only report when the status has changed */
447 if(current_status != last_usb_status)
449 last_usb_status = current_status;
450 countdown = NUM_POLL_READINGS;
452 else
454 /* Count down until it gets negative */
455 if(countdown >= 0)
456 countdown--;
458 /* Report to the thread if we have had 3 identical status
459 readings in a row */
460 if(countdown == 0)
462 if(current_status)
463 queue_post(&usb_queue, USB_INSERTED, NULL);
464 else
465 queue_post(&usb_queue, USB_EXTRACTED, NULL);
469 #ifdef HAVE_MMC
470 if(usb_mmc_countdown > 0)
472 usb_mmc_countdown--;
473 if (usb_mmc_countdown == 0)
474 queue_post(&usb_queue, USB_REENABLE, NULL);
476 #endif
478 #endif
480 void usb_acknowledge(long id)
482 queue_post(&usb_queue, id, NULL);
485 void usb_init(void)
487 usb_state = USB_EXTRACTED;
488 usb_monitor_enabled = false;
489 countdown = -1;
491 #ifdef USB_IRIVERSTYLE
492 or_l(0x00000080, &GPIO1_FUNCTION); /* GPIO39 is the USB detect input */
494 #ifdef IRIVER_H300_SERIES
495 /* ISD300 3.3V ON */
496 or_l(8,&GPIO1_FUNCTION);
497 or_l(8,&GPIO1_OUT);
498 or_l(8,&GPIO1_ENABLE);
500 /* Tristate the SCK/SDA to the ISD300 config EEPROM */
501 and_l(~0x03000000, &GPIO_ENABLE);
502 or_l(0x03000000, &GPIO_FUNCTION);
503 #else
504 and_l(~0x01000040, &GPIO_OUT); /* GPIO24 is the Cypress chip power */
505 or_l(0x01000040, &GPIO_ENABLE);
506 or_l(0x01000040, &GPIO_FUNCTION);
507 #endif
509 #elif defined(USB_IPODSTYLE)
510 int r0;
511 outl(inl(0x70000084) | 0x200, 0x70000084);
513 outl(inl(0x7000002C) | 0x3000000, 0x7000002C);
514 outl(inl(0x6000600C) | 0x400000, 0x6000600C);
516 outl(inl(0x60006004) | 0x400000, 0x60006004); /* reset usb start */
517 outl(inl(0x60006004) & ~0x400000, 0x60006004); /* reset usb end */
519 outl(inl(0x70000020) | 0x80000000, 0x70000020);
520 while ((inl(0x70000028) & 0x80) == 0);
522 outl(inl(0xc5000184) | 0x100, 0xc5000184);
523 while ((inl(0xc5000184) & 0x100) != 0);
525 outl(inl(0xc50001A4) | 0x5F000000, 0xc50001A4);
526 if ((inl(0xc50001A4) & 0x100) == 0) {
527 outl(inl(0xc50001A8) & ~0x3, 0xc50001A8);
528 outl(inl(0xc50001A8) | 0x2, 0xc50001A8);
529 outl(inl(0x70000028) | 0x4000, 0x70000028);
530 outl(inl(0x70000028) | 0x2, 0x70000028);
531 } else {
532 outl(inl(0xc50001A8) | 0x3, 0xc50001A8);
533 outl(inl(0x70000028) &~0x4000, 0x70000028);
534 outl(inl(0x70000028) | 0x2, 0x70000028);
536 outl(inl(0xc5000140) | 0x2, 0xc5000140);
537 while((inl(0xc5000140) & 0x2) != 0);
538 r0 = inl(0xc5000184);
540 /* Note from IPL source (referring to next 5 lines of code:
541 THIS NEEDS TO BE CHANGED ONCE THERE IS KERNEL USB */
542 outl(inl(0x70000020) | 0x80000000, 0x70000020);
543 outl(inl(0x6000600C) | 0x400000, 0x6000600C);
544 while ((inl(0x70000028) & 0x80) == 0);
545 outl(inl(0x70000028) | 0x2, 0x70000028);
547 udelay(0x186A0);
548 #endif
550 usb_enable(false);
552 /* We assume that the USB cable is extracted */
553 last_usb_status = false;
555 #ifndef BOOTLOADER
556 queue_init(&usb_queue);
557 create_thread(usb_thread, usb_stack, sizeof(usb_stack), usb_thread_name);
559 tick_add_task(usb_tick);
560 #endif
564 void usb_wait_for_disconnect(struct event_queue *q)
566 struct event ev;
568 /* Don't return until we get SYS_USB_DISCONNECTED */
569 while(1)
571 queue_wait(q, &ev);
572 if(ev.id == SYS_USB_DISCONNECTED)
574 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
575 return;
580 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
582 struct event ev;
584 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
585 while(1)
587 queue_wait_w_tmo(q, &ev, ticks);
588 switch(ev.id)
590 case SYS_USB_DISCONNECTED:
591 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
592 return 0;
593 break;
594 case SYS_TIMEOUT:
595 return 1;
596 break;
601 void usb_start_monitoring(void)
603 usb_monitor_enabled = true;
606 bool usb_inserted(void)
608 #ifdef HAVE_USB_POWER
609 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
610 #else
611 return usb_state == USB_INSERTED;
612 #endif
615 #ifdef HAVE_USB_POWER
616 bool usb_powered(void)
618 return usb_state == USB_POWERED;
620 #endif
622 #else
624 #ifdef USB_NONE
625 bool usb_inserted(void)
627 return false;
629 #endif
631 /* Dummy simulator functions */
632 void usb_acknowledge(long id)
634 id = id;
637 void usb_init(void)
641 void usb_start_monitoring(void)
645 bool usb_detect(void)
647 return false;
650 void usb_wait_for_disconnect(struct event_queue *q)
652 (void)q;
655 #endif /* USB_NONE or SIMULATOR */