Fix tuning into weird frequencies at unpause after changing region setting
[Rockbox.git] / firmware / usb.c
blobf83cb44e174d7da04a8ee405550983e3d273a065
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 #elif CONFIG_KEYPAD == GIGABEAT_PAD
81 #define USBPOWER_BUTTON BUTTON_MENU
82 #define USBPOWER_BTN_IGNORE BUTTON_POWER
83 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
84 #define USBPOWER_BUTTON BUTTON_NONE
85 #define USBPOWER_BTN_IGNORE BUTTON_POWER
86 #endif
87 #endif /* HAVE_USB_POWER */
89 /* The ADC tick reads one channel per tick, and we want to check 3 successive
90 readings on the USB voltage channel. This doesn't apply to the Player, but
91 debouncing the USB detection port won't hurt us either. */
92 #define NUM_POLL_READINGS (NUM_ADC_CHANNELS * 3)
93 static int countdown;
95 static int usb_state;
97 #if defined(HAVE_MMC) && !defined(BOOTLOADER)
98 static int usb_mmc_countdown = 0;
99 #endif
101 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
102 needs updating */
103 #ifndef BOOTLOADER
104 static long usb_stack[(DEFAULT_STACK_SIZE + 0x800)/sizeof(long)];
105 static const char usb_thread_name[] = "usb";
106 #endif
107 static struct event_queue usb_queue;
108 static bool last_usb_status;
109 static bool usb_monitor_enabled;
111 #ifndef TARGET_TREE
112 void usb_enable(bool on)
114 #ifdef USB_ENABLE_ONDIOSTYLE
115 PACR2 &= ~0x04C0; /* use PA3, PA5 as GPIO */
116 if(on)
118 #ifdef HAVE_MMC
119 mmc_enable_int_flash_clock(!mmc_detect());
120 #endif
121 if (!(read_hw_mask() & MMC_CLOCK_POLARITY))
122 and_b(~0x20, &PBDRH); /* old circuit needs SCK1 = low while on USB */
123 or_b(0x20, &PADRL); /* enable USB */
124 and_b(~0x08, &PADRL); /* assert card detect */
126 else
128 if (!(read_hw_mask() & MMC_CLOCK_POLARITY))
129 or_b(0x20, &PBDRH); /* reset SCK1 = high for old circuit */
130 and_b(~0x20, &PADRL); /* disable USB */
131 or_b(0x08, &PADRL); /* deassert card detect */
133 or_b(0x28, &PAIORL); /* output for USB enable and card detect */
134 #elif defined(USB_GMINISTYLE)
136 int smsc_ver = smsc_version();
137 if (on) {
138 if (smsc_ver < 4) {
139 P6 &= ~0x04;
140 P10 &= ~0x20;
142 smsc_delay();
144 P6 |= 0x08;
145 P10 |= 0x20;
147 smsc_delay();
149 P6 |= 0x10;
150 } else {
151 P6 &= ~0x10;
152 if (smsc_ver < 4) {
153 P6 &= ~0x04;
154 P10 &= ~0x20;
158 #elif defined(USB_IRIVERSTYLE)
159 if(on)
161 /* Power on the Cypress chip */
162 #ifdef IRIVER_H100_SERIES
163 or_l(0x01000040, &GPIO_OUT);
164 #else
165 and_l(~0x00000008,&GPIO1_OUT);
166 #endif
167 sleep(2);
169 else
171 /* Power off the Cypress chip */
172 #ifdef IRIVER_H100_SERIES
173 and_l(~0x01000040, &GPIO_OUT);
174 #else
175 or_l(0x00000008,&GPIO1_OUT);
176 #endif
178 #elif defined(USB_ISP1582)
179 /* TODO: Implement USB_ISP1582 */
180 (void) on;
181 #elif defined(USB_X5STYLE)
182 /* TODO X5 */
183 #elif defined(USB_GIGABEAT_STYLE)
184 /* TODO gigabeat */
185 #else
186 #ifdef HAVE_LCD_BITMAP
187 if(read_hw_mask() & USB_ACTIVE_HIGH)
188 on = !on;
189 #endif
190 if(on)
192 and_b(~0x04, &PADRH); /* enable USB */
194 else
196 or_b(0x04, &PADRH);
198 or_b(0x04, &PAIORH);
199 #endif
201 #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 #ifndef TARGET_TREE
381 bool usb_detect(void)
383 bool current_status;
385 #ifdef USB_RECORDERSTYLE
386 current_status = (adc_read(ADC_USB_POWER) > 500)?true:false;
387 #endif
388 #ifdef USB_FMRECORDERSTYLE
389 current_status = (adc_read(ADC_USB_POWER) <= 512)?true:false;
390 #endif
391 #ifdef USB_PLAYERSTYLE
392 current_status = (PADR & 0x8000)?false:true;
393 #endif
394 #ifdef USB_IRIVERSTYLE
395 current_status = (GPIO1_READ & 0x80)?true:false;
396 #endif
397 #ifdef USB_GMINISTYLE
398 current_status = (P5 & 0x10)?true:false;
399 #endif
400 #ifdef USB_IPODSTYLE
401 /* The following check is in the ipodlinux source, with the
402 comment "USB2D_IDENT is bad" if USB2D_IDENT != 0x22FA05 */
403 if (USB2D_IDENT != 0x22FA05) {
404 return false;
406 current_status = (USB_STATUS & 0x800)?true:false;
407 #endif
408 #ifdef USB_ISP1582
409 /* TODO: Implement USB_ISP1582 */
410 current_status = false;
411 #endif
412 return current_status;
414 #endif
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 TARGET_TREE
483 usb_init_device();
484 #elif defined USB_IRIVERSTYLE
485 or_l(0x00000080, &GPIO1_FUNCTION); /* GPIO39 is the USB detect input */
487 #ifdef IRIVER_H300_SERIES
488 /* ISD300 3.3V ON */
489 or_l(8,&GPIO1_FUNCTION);
490 or_l(8,&GPIO1_OUT);
491 or_l(8,&GPIO1_ENABLE);
493 /* Tristate the SCK/SDA to the ISD300 config EEPROM */
494 and_l(~0x03000000, &GPIO_ENABLE);
495 or_l(0x03000000, &GPIO_FUNCTION);
496 #else
497 and_l(~0x01000040, &GPIO_OUT); /* GPIO24 is the Cypress chip power */
498 or_l(0x01000040, &GPIO_ENABLE);
499 or_l(0x01000040, &GPIO_FUNCTION);
500 #endif
502 #endif
504 usb_enable(false);
506 /* We assume that the USB cable is extracted */
507 last_usb_status = false;
509 #ifndef BOOTLOADER
510 queue_init(&usb_queue, true);
511 create_thread(usb_thread, usb_stack, sizeof(usb_stack),
512 usb_thread_name IF_PRIO(, PRIORITY_SYSTEM));
514 tick_add_task(usb_tick);
515 #endif
519 void usb_wait_for_disconnect(struct event_queue *q)
521 struct event ev;
523 /* Don't return until we get SYS_USB_DISCONNECTED */
524 while(1)
526 queue_wait(q, &ev);
527 if(ev.id == SYS_USB_DISCONNECTED)
529 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
530 return;
535 int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
537 struct event ev;
539 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
540 while(1)
542 queue_wait_w_tmo(q, &ev, ticks);
543 switch(ev.id)
545 case SYS_USB_DISCONNECTED:
546 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
547 return 0;
548 break;
549 case SYS_TIMEOUT:
550 return 1;
551 break;
556 void usb_start_monitoring(void)
558 usb_monitor_enabled = true;
561 bool usb_inserted(void)
563 #ifdef HAVE_USB_POWER
564 return usb_state == USB_INSERTED || usb_state == USB_POWERED;
565 #else
566 return usb_state == USB_INSERTED;
567 #endif
570 #ifdef HAVE_USB_POWER
571 bool usb_powered(void)
573 return usb_state == USB_POWERED;
575 #endif
577 #else
579 #ifdef USB_NONE
580 bool usb_inserted(void)
582 return false;
584 #endif
586 /* Dummy simulator functions */
587 void usb_acknowledge(long id)
589 id = id;
592 void usb_init(void)
596 void usb_start_monitoring(void)
600 bool usb_detect(void)
602 return false;
605 void usb_wait_for_disconnect(struct event_queue *q)
607 (void)q;
610 #endif /* USB_NONE or SIMULATOR */