Work-in-progress iriver iFP-7xx port by Tomasz Malesinski
[Rockbox.git] / firmware / usb.c
blobbd2fde3ab81abfb7505a0162409e14ed649803b9
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_4G_PAD
72 #define USBPOWER_BUTTON BUTTON_MENU
73 #define USBPOWER_BTN_IGNORE BUTTON_PLAY
74 #endif
75 #endif /* HAVE_USB_POWER */
77 /* The ADC tick reads one channel per tick, and we want to check 3 successive
78 readings on the USB voltage channel. This doesn't apply to the Player, but
79 debouncing the USB detection port won't hurt us either. */
80 #define NUM_POLL_READINGS (NUM_ADC_CHANNELS * 3)
81 static int countdown;
83 static int usb_state;
85 #ifdef HAVE_MMC
86 static int usb_mmc_countdown = 0;
87 #endif
89 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
90 needs updating */
91 #ifndef BOOTLOADER
92 static long usb_stack[(DEFAULT_STACK_SIZE + 0x800)/sizeof(long)];
93 static const char usb_thread_name[] = "usb";
94 #endif
95 static struct event_queue usb_queue;
96 static bool last_usb_status;
97 static bool usb_monitor_enabled;
99 void usb_enable(bool on)
101 #ifdef USB_ENABLE_ONDIOSTYLE
102 PACR2 &= ~0x04C0; /* use PA3, PA5 as GPIO */
103 if(on)
105 #ifdef HAVE_MMC
106 mmc_enable_int_flash_clock(!mmc_detect());
107 #endif
108 if (!(read_hw_mask() & MMC_CLOCK_POLARITY))
109 and_b(~0x20, &PBDRH); /* old circuit needs SCK1 = low while on USB */
110 or_b(0x20, &PADRL); /* enable USB */
111 and_b(~0x08, &PADRL); /* assert card detect */
113 else
115 if (!(read_hw_mask() & MMC_CLOCK_POLARITY))
116 or_b(0x20, &PBDRH); /* reset SCK1 = high for old circuit */
117 and_b(~0x20, &PADRL); /* disable USB */
118 or_b(0x08, &PADRL); /* deassert card detect */
120 or_b(0x28, &PAIORL); /* output for USB enable and card detect */
121 #elif defined(USB_GMINISTYLE)
123 int smsc_ver = smsc_version();
124 if (on) {
125 if (smsc_ver < 4) {
126 P6 &= ~0x04;
127 P10 &= ~0x20;
129 smsc_delay();
131 P6 |= 0x08;
132 P10 |= 0x20;
134 smsc_delay();
136 P6 |= 0x10;
137 } else {
138 P6 &= ~0x10;
139 if (smsc_ver < 4) {
140 P6 &= ~0x04;
141 P10 &= ~0x20;
145 #elif defined(USB_IRIVERSTYLE)
146 if(on)
148 /* Power on the Cypress chip */
149 #ifdef IRIVER_H100_SERIES
150 or_l(0x01000040, &GPIO_OUT);
151 #else
152 and_l(~0x00000008,&GPIO1_OUT);
153 #endif
154 sleep(2);
156 else
158 /* Power off the Cypress chip */
159 #ifdef IRIVER_H100_SERIES
160 and_l(~0x01000040, &GPIO_OUT);
161 #else
162 or_l(0x00000008,&GPIO1_OUT);
163 #endif
166 #elif defined(USB_IPODSTYLE)
167 /* For the ipod, we can only do one thing with USB mode - reboot
168 into Apple's flash-based disk-mode. This does not return. */
169 if (on)
171 /* The following code is copied from ipodlinux - it doesn't work on the
172 iPod Video */
173 unsigned char* storage_ptr = (unsigned char *)0x40017F00;
174 char* diskmode = "diskmode\0";
175 char* hotstuff = "hotstuff\0";
177 memcpy(storage_ptr, diskmode, 9);
178 storage_ptr = (unsigned char *)0x40017f08;
179 memcpy(storage_ptr, hotstuff, 9);
180 outl(1, 0x40017F10);
181 outl(inl(0x60006004) | 0x4, 0x60006004);
183 #elif defined(USB_ISP1582)
184 /* TODO: Implement USB_ISP1582 */
185 (void) on;
186 #else
187 #ifdef HAVE_LCD_BITMAP
188 if(read_hw_mask() & USB_ACTIVE_HIGH)
189 on = !on;
190 #endif
191 if(on)
193 and_b(~0x04, &PADRH); /* enable USB */
195 else
197 or_b(0x04, &PADRH);
199 or_b(0x04, &PAIORH);
200 #endif
203 #ifndef BOOTLOADER
204 static void usb_slave_mode(bool on)
206 int rc;
208 if(on)
210 DEBUGF("Entering USB slave mode\n");
211 ata_soft_reset();
212 ata_init();
213 ata_enable(false);
214 usb_enable(true);
216 else
218 DEBUGF("Leaving USB slave mode\n");
220 /* Let the ISDx00 settle */
221 sleep(HZ*1);
223 usb_enable(false);
225 rc = ata_init();
226 if(rc)
228 /* fixme: can we remove this? (already such in main.c) */
229 char str[32];
230 lcd_clear_display();
231 snprintf(str, 31, "ATA error: %d", rc);
232 lcd_puts(0, 0, str);
233 lcd_puts(0, 1, "Press ON to debug");
234 lcd_update();
235 while(!(button_get(true) & BUTTON_REL)) {};
236 dbg_ports();
237 panicf("ata: %d",rc);
240 rc = disk_mount_all();
241 if (rc <= 0) /* no partition */
242 panicf("mount: %d",rc);
247 static void usb_thread(void)
249 int num_acks_to_expect = -1;
250 bool waiting_for_ack;
251 struct event ev;
253 waiting_for_ack = false;
255 while(1)
257 queue_wait(&usb_queue, &ev);
258 switch(ev.id)
260 case USB_INSERTED:
261 #ifdef HAVE_LCD_BITMAP
262 if(do_screendump_instead_of_usb)
264 screen_dump();
266 else
267 #endif
268 #ifdef HAVE_USB_POWER
269 if((button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON)
271 usb_state = USB_POWERED;
273 else
274 #endif
276 /* Tell all threads that they have to back off the ATA.
277 We subtract one for our own thread. */
278 num_acks_to_expect =
279 queue_broadcast(SYS_USB_CONNECTED, NULL) - 1;
280 waiting_for_ack = true;
281 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
282 num_acks_to_expect);
284 break;
286 case SYS_USB_CONNECTED_ACK:
287 if(waiting_for_ack)
289 num_acks_to_expect--;
290 if(num_acks_to_expect == 0)
292 DEBUGF("All threads have acknowledged the connect.\n");
293 #ifdef USB_REALLY_BRAVE
294 usb_slave_mode(true);
295 usb_state = USB_INSERTED;
296 cpu_idle_mode(true);
297 #else
298 system_reboot();
299 #endif
301 else
303 DEBUGF("usb: got ack, %d to go...\n",
304 num_acks_to_expect);
307 break;
309 case USB_EXTRACTED:
310 #ifdef HAVE_LCD_BITMAP
311 if(do_screendump_instead_of_usb)
312 break;
313 #endif
314 #ifdef HAVE_USB_POWER
315 if(usb_state == USB_POWERED)
317 usb_state = USB_EXTRACTED;
318 break;
320 #endif
321 if(usb_state == USB_INSERTED)
323 /* Only disable the USB mode if we really have enabled it
324 some threads might not have acknowledged the
325 insertion */
326 usb_slave_mode(false);
327 cpu_idle_mode(false);
330 usb_state = USB_EXTRACTED;
332 /* Tell all threads that we are back in business */
333 num_acks_to_expect =
334 queue_broadcast(SYS_USB_DISCONNECTED, NULL) - 1;
335 waiting_for_ack = true;
336 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
337 num_acks_to_expect);
338 #ifdef HAVE_LCD_CHARCELLS
339 lcd_icon(ICON_USB, false);
340 #endif
341 break;
343 case SYS_USB_DISCONNECTED_ACK:
344 if(waiting_for_ack)
346 num_acks_to_expect--;
347 if(num_acks_to_expect == 0)
349 DEBUGF("All threads have acknowledged. "
350 "We're in business.\n");
352 else
354 DEBUGF("usb: got ack, %d to go...\n",
355 num_acks_to_expect);
358 break;
360 #ifdef HAVE_MMC
361 case SYS_MMC_INSERTED:
362 case SYS_MMC_EXTRACTED:
363 if(usb_state == USB_INSERTED)
365 usb_enable(false);
366 usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
368 break;
370 case USB_REENABLE:
371 if(usb_state == USB_INSERTED)
372 usb_enable(true); /* reenable only if still inserted */
373 break;
374 #endif
378 #endif
380 bool usb_detect(void)
382 bool current_status;
384 #ifdef USB_RECORDERSTYLE
385 current_status = (adc_read(ADC_USB_POWER) > 500)?true:false;
386 #endif
387 #ifdef USB_FMRECORDERSTYLE
388 current_status = (adc_read(ADC_USB_POWER) <= 512)?true:false;
389 #endif
390 #ifdef USB_PLAYERSTYLE
391 current_status = (PADR & 0x8000)?false:true;
392 #endif
393 #ifdef USB_IRIVERSTYLE
394 current_status = (GPIO1_READ & 0x80)?true:false;
395 #endif
396 #ifdef USB_GMINISTYLE
397 current_status = (P5 & 0x10)?true:false;
398 #endif
399 #ifdef IRIVER_H300
400 current_status = (GPIO1_READ & 0x80)?true:false;
401 #endif
402 #ifdef USB_IPODSTYLE
403 /* The following check is in the ipodlinux source, with the
404 comment "USB2D_IDENT is bad" if USB2D_IDENT != 0x22FA05 */
405 if (USB2D_IDENT != 0x22FA05) {
406 return false;
408 current_status = (USB_STATUS & 0x800)?true:false;
409 #endif
410 #ifdef USB_ISP1582
411 /* TODO: Implement USB_ISP1582 */
412 current_status = false;
413 #endif
414 return current_status;
417 #ifndef BOOTLOADER
418 static void usb_tick(void)
420 bool current_status;
422 #ifdef USB_GMINISTYLE
423 /* Keep usb chip in usb state (?) */
424 if (P5 & 0x10) {
425 if ((P10 & 0x20) == 0 || (P6 & 0x08) == 0) {
426 if (smsc_version() < 4) {
427 P6 |= 0x08;
428 P10 |= 0x20;
432 #endif
434 if(usb_monitor_enabled)
436 current_status = usb_detect();
438 /* Only report when the status has changed */
439 if(current_status != last_usb_status)
441 last_usb_status = current_status;
442 countdown = NUM_POLL_READINGS;
444 else
446 /* Count down until it gets negative */
447 if(countdown >= 0)
448 countdown--;
450 /* Report to the thread if we have had 3 identical status
451 readings in a row */
452 if(countdown == 0)
454 if(current_status)
455 queue_post(&usb_queue, USB_INSERTED, NULL);
456 else
457 queue_post(&usb_queue, USB_EXTRACTED, NULL);
461 #ifdef HAVE_MMC
462 if(usb_mmc_countdown > 0)
464 usb_mmc_countdown--;
465 if (usb_mmc_countdown == 0)
466 queue_post(&usb_queue, USB_REENABLE, NULL);
468 #endif
470 #endif
472 void usb_acknowledge(long id)
474 queue_post(&usb_queue, id, NULL);
477 void usb_init(void)
479 usb_state = USB_EXTRACTED;
480 usb_monitor_enabled = false;
481 countdown = -1;
483 #ifdef USB_IRIVERSTYLE
484 or_l(0x00000080, &GPIO1_FUNCTION); /* GPIO39 is the USB detect input */
486 #ifdef IRIVER_H300_SERIES
487 /* ISD300 3.3V ON */
488 or_l(8,&GPIO1_FUNCTION);
489 or_l(8,&GPIO1_OUT);
490 or_l(8,&GPIO1_ENABLE);
492 /* Tristate the SCK/SDA to the ISD300 config EEPROM */
493 and_l(~0x03000000, &GPIO_ENABLE);
494 or_l(0x03000000, &GPIO_FUNCTION);
495 #else
496 and_l(~0x01000040, &GPIO_OUT); /* GPIO24 is the Cypress chip power */
497 or_l(0x01000040, &GPIO_ENABLE);
498 or_l(0x01000040, &GPIO_FUNCTION);
499 #endif
501 #elif defined(USB_IPODSTYLE)
502 int r0;
503 outl(inl(0x70000084) | 0x200, 0x70000084);
505 outl(inl(0x7000002C) | 0x3000000, 0x7000002C);
506 outl(inl(0x6000600C) | 0x400000, 0x6000600C);
508 outl(inl(0x60006004) | 0x400000, 0x60006004); /* reset usb start */
509 outl(inl(0x60006004) & ~0x400000, 0x60006004); /* reset usb end */
511 outl(inl(0x70000020) | 0x80000000, 0x70000020);
512 while ((inl(0x70000028) & 0x80) == 0);
514 outl(inl(0xc5000184) | 0x100, 0xc5000184);
515 while ((inl(0xc5000184) & 0x100) != 0);
517 outl(inl(0xc50001A4) | 0x5F000000, 0xc50001A4);
518 if ((inl(0xc50001A4) & 0x100) == 0) {
519 outl(inl(0xc50001A8) & ~0x3, 0xc50001A8);
520 outl(inl(0xc50001A8) | 0x2, 0xc50001A8);
521 outl(inl(0x70000028) | 0x4000, 0x70000028);
522 outl(inl(0x70000028) | 0x2, 0x70000028);
523 } else {
524 outl(inl(0xc50001A8) | 0x3, 0xc50001A8);
525 outl(inl(0x70000028) &~0x4000, 0x70000028);
526 outl(inl(0x70000028) | 0x2, 0x70000028);
528 outl(inl(0xc5000140) | 0x2, 0xc5000140);
529 while((inl(0xc5000140) & 0x2) != 0);
530 r0 = inl(0xc5000184);
532 /* Note from IPL source (referring to next 5 lines of code:
533 THIS NEEDS TO BE CHANGED ONCE THERE IS KERNEL USB */
534 outl(inl(0x70000020) | 0x80000000, 0x70000020);
535 outl(inl(0x6000600C) | 0x400000, 0x6000600C);
536 while ((inl(0x70000028) & 0x80) == 0);
537 outl(inl(0x70000028) | 0x2, 0x70000028);
539 udelay(0x186A0);
540 #endif
542 usb_enable(false);
544 /* We assume that the USB cable is extracted */
545 last_usb_status = false;
547 #ifndef BOOTLOADER
548 queue_init(&usb_queue);
549 create_thread(usb_thread, usb_stack, sizeof(usb_stack), usb_thread_name);
551 tick_add_task(usb_tick);
552 #endif
555 void usb_wait_for_disconnect(struct event_queue *q)
557 struct event ev;
559 /* Don't return until we get SYS_USB_DISCONNECTED */
560 while(1)
562 queue_wait(q, &ev);
563 if(ev.id == SYS_USB_DISCONNECTED)
565 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
566 return;
571 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
573 struct event ev;
575 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
576 while(1)
578 queue_wait_w_tmo(q, &ev, ticks);
579 switch(ev.id)
581 case SYS_USB_DISCONNECTED:
582 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
583 return 0;
584 break;
585 case SYS_TIMEOUT:
586 return 1;
587 break;
592 void usb_start_monitoring(void)
594 usb_monitor_enabled = true;
597 bool usb_inserted(void)
599 return usb_state == USB_INSERTED;
602 #ifdef HAVE_USB_POWER
603 bool usb_powered(void)
605 return usb_state == USB_POWERED;
607 #endif
609 #else
611 #ifdef USB_NONE
612 bool usb_inserted(void)
614 return false;
616 #endif
618 /* Dummy simulator functions */
619 void usb_acknowledge(long id)
621 id = id;
624 void usb_init(void)
628 void usb_start_monitoring(void)
632 bool usb_detect(void)
634 return false;
637 #endif /* USB_NONE or SIMULATOR */