Further iPod 3G work from Seven Le Mesle
[Rockbox.git] / firmware / usb.c
blob32e98ef6ce972c12ef3af9426d49e2ef4d87a221
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
45 extern void dbg_ports(void); /* NASTY! defined in apps/ */
47 #ifdef HAVE_LCD_BITMAP
48 bool do_screendump_instead_of_usb = false;
49 void screen_dump(void); /* Nasty again. Defined in apps/ too */
50 #endif
52 #define USB_REALLY_BRAVE
54 #if !defined(SIMULATOR) && !defined(USB_NONE)
56 /* Messages from usb_tick and thread states */
57 #define USB_INSERTED 1
58 #define USB_EXTRACTED 2
59 #ifdef HAVE_MMC
60 #define USB_REENABLE 3
61 #endif
62 #ifdef HAVE_USB_POWER
63 #define USB_POWERED 4
65 #if CONFIG_KEYPAD == RECORDER_PAD
66 #define USBPOWER_BUTTON BUTTON_F1
67 #define USBPOWER_BTN_IGNORE BUTTON_ON
68 #elif CONFIG_KEYPAD == ONDIO_PAD
69 #define USBPOWER_BUTTON BUTTON_MENU
70 #define USBPOWER_BTN_IGNORE BUTTON_OFF
71 #elif (CONFIG_KEYPAD == IPOD_3G_PAD) || (CONFIG_KEYPAD == IPOD_4G_PAD)
72 #define USBPOWER_BUTTON BUTTON_MENU
73 #define USBPOWER_BTN_IGNORE BUTTON_PLAY
74 #elif CONFIG_KEYPAD == IRIVER_H300_PAD
75 #define USBPOWER_BUTTON BUTTON_REC
76 #define USBPOWER_BTN_IGNORE BUTTON_ON
77 #endif
78 #endif /* HAVE_USB_POWER */
80 /* The ADC tick reads one channel per tick, and we want to check 3 successive
81 readings on the USB voltage channel. This doesn't apply to the Player, but
82 debouncing the USB detection port won't hurt us either. */
83 #define NUM_POLL_READINGS (NUM_ADC_CHANNELS * 3)
84 static int countdown;
86 static int usb_state;
88 #ifdef HAVE_MMC
89 static int usb_mmc_countdown = 0;
90 #endif
92 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
93 needs updating */
94 #ifndef BOOTLOADER
95 static long usb_stack[(DEFAULT_STACK_SIZE + 0x800)/sizeof(long)];
96 static const char usb_thread_name[] = "usb";
97 #endif
98 static struct event_queue usb_queue;
99 static bool last_usb_status;
100 static bool usb_monitor_enabled;
102 void usb_enable(bool on)
104 #ifdef USB_ENABLE_ONDIOSTYLE
105 PACR2 &= ~0x04C0; /* use PA3, PA5 as GPIO */
106 if(on)
108 #ifdef HAVE_MMC
109 mmc_enable_int_flash_clock(!mmc_detect());
110 #endif
111 if (!(read_hw_mask() & MMC_CLOCK_POLARITY))
112 and_b(~0x20, &PBDRH); /* old circuit needs SCK1 = low while on USB */
113 or_b(0x20, &PADRL); /* enable USB */
114 and_b(~0x08, &PADRL); /* assert card detect */
116 else
118 if (!(read_hw_mask() & MMC_CLOCK_POLARITY))
119 or_b(0x20, &PBDRH); /* reset SCK1 = high for old circuit */
120 and_b(~0x20, &PADRL); /* disable USB */
121 or_b(0x08, &PADRL); /* deassert card detect */
123 or_b(0x28, &PAIORL); /* output for USB enable and card detect */
124 #elif defined(USB_GMINISTYLE)
126 int smsc_ver = smsc_version();
127 if (on) {
128 if (smsc_ver < 4) {
129 P6 &= ~0x04;
130 P10 &= ~0x20;
132 smsc_delay();
134 P6 |= 0x08;
135 P10 |= 0x20;
137 smsc_delay();
139 P6 |= 0x10;
140 } else {
141 P6 &= ~0x10;
142 if (smsc_ver < 4) {
143 P6 &= ~0x04;
144 P10 &= ~0x20;
148 #elif defined(USB_IRIVERSTYLE)
149 if(on)
151 /* Power on the Cypress chip */
152 #ifdef IRIVER_H100_SERIES
153 or_l(0x01000040, &GPIO_OUT);
154 #else
155 and_l(~0x00000008,&GPIO1_OUT);
156 #endif
157 sleep(2);
159 else
161 /* Power off the Cypress chip */
162 #ifdef IRIVER_H100_SERIES
163 and_l(~0x01000040, &GPIO_OUT);
164 #else
165 or_l(0x00000008,&GPIO1_OUT);
166 #endif
169 #elif defined(USB_IPODSTYLE)
170 /* For the ipod, we can only do one thing with USB mode - reboot
171 into Apple's flash-based disk-mode. This does not return. */
172 if (on)
174 /* The following code is copied from ipodlinux */
175 #if defined (APPLE_IPODCOLOR) || defined(APPLE_IPOD3G)
176 unsigned char* storage_ptr = (unsigned char *)0x40017F00;
177 #elif defined(APPLE_IPODNANO) || defined(APPLE_IPODVIDEO)
178 unsigned char* storage_ptr = (unsigned char *)0x4001FF00;
179 #endif
180 memcpy(storage_ptr, "diskmode\0\0hotstuff\0\0\1", 21);
181 DEV_RS |= 4; /* Reboot */
183 #elif defined(USB_ISP1582)
184 /* TODO: Implement USB_ISP1582 */
185 (void) on;
186 #elif defined(USB_X5STYLE)
187 /* TODO X5 */
188 #else
189 #ifdef HAVE_LCD_BITMAP
190 if(read_hw_mask() & USB_ACTIVE_HIGH)
191 on = !on;
192 #endif
193 if(on)
195 and_b(~0x04, &PADRH); /* enable USB */
197 else
199 or_b(0x04, &PADRH);
201 or_b(0x04, &PAIORH);
202 #endif
205 #ifndef BOOTLOADER
206 static void usb_slave_mode(bool on)
208 int rc;
210 if(on)
212 DEBUGF("Entering USB slave mode\n");
213 ata_soft_reset();
214 ata_init();
215 ata_enable(false);
216 usb_enable(true);
218 else
220 DEBUGF("Leaving USB slave mode\n");
222 /* Let the ISDx00 settle */
223 sleep(HZ*1);
225 usb_enable(false);
227 rc = ata_init();
228 if(rc)
230 /* fixme: can we remove this? (already such in main.c) */
231 char str[32];
232 lcd_clear_display();
233 snprintf(str, 31, "ATA error: %d", rc);
234 lcd_puts(0, 0, str);
235 lcd_puts(0, 1, "Press ON to debug");
236 lcd_update();
237 while(!(button_get(true) & BUTTON_REL)) {};
238 dbg_ports();
239 panicf("ata: %d",rc);
242 rc = disk_mount_all();
243 if (rc <= 0) /* no partition */
244 panicf("mount: %d",rc);
249 static void usb_thread(void)
251 int num_acks_to_expect = -1;
252 bool waiting_for_ack;
253 struct event ev;
255 waiting_for_ack = false;
257 while(1)
259 queue_wait(&usb_queue, &ev);
260 switch(ev.id)
262 case USB_INSERTED:
263 #ifdef HAVE_LCD_BITMAP
264 if(do_screendump_instead_of_usb)
266 screen_dump();
268 else
269 #endif
270 #ifdef HAVE_USB_POWER
271 if((button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON)
273 usb_state = USB_POWERED;
275 else
276 #endif
278 /* Tell all threads that they have to back off the ATA.
279 We subtract one for our own thread. */
280 num_acks_to_expect =
281 queue_broadcast(SYS_USB_CONNECTED, NULL) - 1;
282 waiting_for_ack = true;
283 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
284 num_acks_to_expect);
286 break;
288 case SYS_USB_CONNECTED_ACK:
289 if(waiting_for_ack)
291 num_acks_to_expect--;
292 if(num_acks_to_expect == 0)
294 DEBUGF("All threads have acknowledged the connect.\n");
295 #ifdef USB_REALLY_BRAVE
296 usb_slave_mode(true);
297 usb_state = USB_INSERTED;
298 cpu_idle_mode(true);
299 #else
300 system_reboot();
301 #endif
303 else
305 DEBUGF("usb: got ack, %d to go...\n",
306 num_acks_to_expect);
309 break;
311 case USB_EXTRACTED:
312 #ifdef HAVE_LCD_BITMAP
313 if(do_screendump_instead_of_usb)
314 break;
315 #endif
316 #ifdef HAVE_USB_POWER
317 if(usb_state == USB_POWERED)
319 usb_state = USB_EXTRACTED;
320 break;
322 #endif
323 if(usb_state == USB_INSERTED)
325 /* Only disable the USB mode if we really have enabled it
326 some threads might not have acknowledged the
327 insertion */
328 usb_slave_mode(false);
329 cpu_idle_mode(false);
332 usb_state = USB_EXTRACTED;
334 /* Tell all threads that we are back in business */
335 num_acks_to_expect =
336 queue_broadcast(SYS_USB_DISCONNECTED, NULL) - 1;
337 waiting_for_ack = true;
338 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
339 num_acks_to_expect);
340 #ifdef HAVE_LCD_CHARCELLS
341 lcd_icon(ICON_USB, false);
342 #endif
343 break;
345 case SYS_USB_DISCONNECTED_ACK:
346 if(waiting_for_ack)
348 num_acks_to_expect--;
349 if(num_acks_to_expect == 0)
351 DEBUGF("All threads have acknowledged. "
352 "We're in business.\n");
354 else
356 DEBUGF("usb: got ack, %d to go...\n",
357 num_acks_to_expect);
360 break;
362 #ifdef HAVE_MMC
363 case SYS_MMC_INSERTED:
364 case SYS_MMC_EXTRACTED:
365 if(usb_state == USB_INSERTED)
367 usb_enable(false);
368 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
370 break;
372 case USB_REENABLE:
373 if(usb_state == USB_INSERTED)
374 usb_enable(true); /* reenable only if still inserted */
375 break;
376 #endif
380 #endif
382 bool usb_detect(void)
384 bool current_status;
386 #ifdef USB_RECORDERSTYLE
387 current_status = (adc_read(ADC_USB_POWER) > 500)?true:false;
388 #endif
389 #ifdef USB_FMRECORDERSTYLE
390 current_status = (adc_read(ADC_USB_POWER) <= 512)?true:false;
391 #endif
392 #ifdef USB_PLAYERSTYLE
393 current_status = (PADR & 0x8000)?false:true;
394 #endif
395 #ifdef USB_IRIVERSTYLE
396 current_status = (GPIO1_READ & 0x80)?true:false;
397 #endif
398 #ifdef USB_GMINISTYLE
399 current_status = (P5 & 0x10)?true:false;
400 #endif
401 #ifdef USB_IPODSTYLE
402 /* The following check is in the ipodlinux source, with the
403 comment "USB2D_IDENT is bad" if USB2D_IDENT != 0x22FA05 */
404 if (USB2D_IDENT != 0x22FA05) {
405 return false;
407 current_status = (USB_STATUS & 0x800)?true:false;
408 #endif
409 #ifdef USB_ISP1582
410 /* TODO: Implement USB_ISP1582 */
411 current_status = false;
412 #endif
413 return current_status;
416 #ifndef BOOTLOADER
417 static void usb_tick(void)
419 bool current_status;
421 #ifdef USB_GMINISTYLE
422 /* Keep usb chip in usb state (?) */
423 if (P5 & 0x10) {
424 if ((P10 & 0x20) == 0 || (P6 & 0x08) == 0) {
425 if (smsc_version() < 4) {
426 P6 |= 0x08;
427 P10 |= 0x20;
431 #endif
433 if(usb_monitor_enabled)
435 current_status = usb_detect();
437 /* Only report when the status has changed */
438 if(current_status != last_usb_status)
440 last_usb_status = current_status;
441 countdown = NUM_POLL_READINGS;
443 else
445 /* Count down until it gets negative */
446 if(countdown >= 0)
447 countdown--;
449 /* Report to the thread if we have had 3 identical status
450 readings in a row */
451 if(countdown == 0)
453 if(current_status)
454 queue_post(&usb_queue, USB_INSERTED, NULL);
455 else
456 queue_post(&usb_queue, USB_EXTRACTED, NULL);
460 #ifdef HAVE_MMC
461 if(usb_mmc_countdown > 0)
463 usb_mmc_countdown--;
464 if (usb_mmc_countdown == 0)
465 queue_post(&usb_queue, USB_REENABLE, NULL);
467 #endif
469 #endif
471 void usb_acknowledge(long id)
473 queue_post(&usb_queue, id, NULL);
476 void usb_init(void)
478 usb_state = USB_EXTRACTED;
479 usb_monitor_enabled = false;
480 countdown = -1;
482 #ifdef USB_IRIVERSTYLE
483 or_l(0x00000080, &GPIO1_FUNCTION); /* GPIO39 is the USB detect input */
485 #ifdef IRIVER_H300_SERIES
486 /* ISD300 3.3V ON */
487 or_l(8,&GPIO1_FUNCTION);
488 or_l(8,&GPIO1_OUT);
489 or_l(8,&GPIO1_ENABLE);
491 /* Tristate the SCK/SDA to the ISD300 config EEPROM */
492 and_l(~0x03000000, &GPIO_ENABLE);
493 or_l(0x03000000, &GPIO_FUNCTION);
494 #else
495 and_l(~0x01000040, &GPIO_OUT); /* GPIO24 is the Cypress chip power */
496 or_l(0x01000040, &GPIO_ENABLE);
497 or_l(0x01000040, &GPIO_FUNCTION);
498 #endif
500 #elif defined(USB_IPODSTYLE)
501 int r0;
502 outl(inl(0x70000084) | 0x200, 0x70000084);
504 outl(inl(0x7000002C) | 0x3000000, 0x7000002C);
505 outl(inl(0x6000600C) | 0x400000, 0x6000600C);
507 outl(inl(0x60006004) | 0x400000, 0x60006004); /* reset usb start */
508 outl(inl(0x60006004) & ~0x400000, 0x60006004); /* reset usb end */
510 outl(inl(0x70000020) | 0x80000000, 0x70000020);
511 while ((inl(0x70000028) & 0x80) == 0);
513 outl(inl(0xc5000184) | 0x100, 0xc5000184);
514 while ((inl(0xc5000184) & 0x100) != 0);
516 outl(inl(0xc50001A4) | 0x5F000000, 0xc50001A4);
517 if ((inl(0xc50001A4) & 0x100) == 0) {
518 outl(inl(0xc50001A8) & ~0x3, 0xc50001A8);
519 outl(inl(0xc50001A8) | 0x2, 0xc50001A8);
520 outl(inl(0x70000028) | 0x4000, 0x70000028);
521 outl(inl(0x70000028) | 0x2, 0x70000028);
522 } else {
523 outl(inl(0xc50001A8) | 0x3, 0xc50001A8);
524 outl(inl(0x70000028) &~0x4000, 0x70000028);
525 outl(inl(0x70000028) | 0x2, 0x70000028);
527 outl(inl(0xc5000140) | 0x2, 0xc5000140);
528 while((inl(0xc5000140) & 0x2) != 0);
529 r0 = inl(0xc5000184);
531 /* Note from IPL source (referring to next 5 lines of code:
532 THIS NEEDS TO BE CHANGED ONCE THERE IS KERNEL USB */
533 outl(inl(0x70000020) | 0x80000000, 0x70000020);
534 outl(inl(0x6000600C) | 0x400000, 0x6000600C);
535 while ((inl(0x70000028) & 0x80) == 0);
536 outl(inl(0x70000028) | 0x2, 0x70000028);
538 udelay(0x186A0);
539 #endif
541 usb_enable(false);
543 /* We assume that the USB cable is extracted */
544 last_usb_status = false;
546 #ifndef BOOTLOADER
547 queue_init(&usb_queue);
548 create_thread(usb_thread, usb_stack, sizeof(usb_stack), usb_thread_name);
550 tick_add_task(usb_tick);
551 #endif
554 void usb_wait_for_disconnect(struct event_queue *q)
556 struct event ev;
558 /* Don't return until we get SYS_USB_DISCONNECTED */
559 while(1)
561 queue_wait(q, &ev);
562 if(ev.id == SYS_USB_DISCONNECTED)
564 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
565 return;
570 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
572 struct event ev;
574 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
575 while(1)
577 queue_wait_w_tmo(q, &ev, ticks);
578 switch(ev.id)
580 case SYS_USB_DISCONNECTED:
581 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
582 return 0;
583 break;
584 case SYS_TIMEOUT:
585 return 1;
586 break;
591 void usb_start_monitoring(void)
593 usb_monitor_enabled = true;
596 bool usb_inserted(void)
598 return usb_state == USB_INSERTED;
601 #ifdef HAVE_USB_POWER
602 bool usb_powered(void)
604 return usb_state == USB_POWERED;
606 #endif
608 #else
610 #ifdef USB_NONE
611 bool usb_inserted(void)
613 return false;
615 #endif
617 /* Dummy simulator functions */
618 void usb_acknowledge(long id)
620 id = id;
623 void usb_init(void)
627 void usb_start_monitoring(void)
631 bool usb_detect(void)
633 return false;
636 #endif /* USB_NONE or SIMULATOR */