1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version 2
20 * of the License, or (at your option) any later version.
22 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
23 * KIND, either express or implied.
25 ****************************************************************************/
28 #include "usb-target.h"
36 #if defined(IPOD_4G) || defined(IPOD_COLOR) \
37 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
38 /* GPIO D bit 3 is usb detect */
39 #define USB_GPIO GPIOD
40 #define USB_GPIO_MASK 0x08
41 #define USB_GPIO_VAL 0x08
43 #elif defined(IPOD_NANO) || defined(IPOD_VIDEO)
44 /* GPIO L bit 4 is usb detect */
45 #define USB_GPIO GPIOL
46 #define USB_GPIO_MASK 0x10
47 #define USB_GPIO_VAL 0x10
49 #elif defined(SANSA_C200)
50 /* GPIO H bit 1 is usb/charger detect */
51 #define USB_GPIO GPIOH
52 #define USB_GPIO_MASK 0x02
53 #define USB_GPIO_VAL 0x02
55 #elif defined(SANSA_E200)
56 /* GPIO B bit 4 is usb/charger detect */
57 #define USB_GPIO GPIOB
58 #define USB_GPIO_MASK 0x10
59 #define USB_GPIO_VAL 0x10
61 #elif defined(IRIVER_H10) || defined(IRIVER_H10_5GB) || defined(MROBE_100)
62 /* GPIO L bit 2 is usb detect */
63 #define USB_GPIO GPIOL
64 #define USB_GPIO_MASK 0x04
65 #define USB_GPIO_VAL 0x04
67 #elif defined(PHILIPS_SA9200)
68 /* GPIO F bit 7 (low) is usb detect */
69 #define USB_GPIO GPIOF
70 #define USB_GPIO_MASK 0x80
71 #define USB_GPIO_VAL 0x00
73 #elif defined(PHILIPS_HDD1630)
74 /* GPIO E bit 2 is usb detect */
75 #define USB_GPIO GPIOE
76 #define USB_GPIO_MASK 0x04
77 #define USB_GPIO_VAL 0x04
79 #elif defined(SAMSUNG_YH820) || defined(SAMSUNG_YH920) || defined(SAMSUNG_YH925)
80 /* GPIO D bit 4 is usb detect */
81 #define USB_GPIO GPIOD
82 #define USB_GPIO_MASK 0x10
83 #define USB_GPIO_VAL 0x10
85 #elif defined(ELIO_TPJ1022)
86 /* GPIO ? bit ? is usb detect (dummy value)*/
87 #define USB_GPIO GPIOD
88 #define USB_GPIO_MASK 0x10
89 #define USB_GPIO_VAL 0x10
92 #error No USB GPIO config specified
95 #define USB_GPIO_ENABLE GPIO_ENABLE(USB_GPIO)
96 #define USB_GPIO_OUTPUT_EN GPIO_OUTPUT_EN(USB_GPIO)
97 #define USB_GPIO_INPUT_VAL GPIO_INPUT_VAL(USB_GPIO)
98 #define USB_GPIO_INT_EN GPIO_INT_EN(USB_GPIO)
99 #define USB_GPIO_INT_LEV GPIO_INT_LEV(USB_GPIO)
100 #define USB_GPIO_INT_CLR GPIO_INT_CLR(USB_GPIO)
101 #define USB_GPIO_HI_INT_MASK GPIO_HI_INT_MASK(USB_GPIO)
103 void usb_init_device(void)
105 /* enable usb module */
106 outl(inl(0x7000002C) | 0x3000000, 0x7000002C);
111 /* reset both USBs */
117 DEV_INIT2
|= INIT_USB
;
119 while ((inl(0x70000028) & 0x80) == 0);
120 outl(inl(0x70000028) | 0x2, 0x70000028);
122 XMB_RAM_CFG
|= 0x47A;
124 /* Do one-time inits */
127 /* disable USB-devices until USB is detected via GPIO */
129 /* Disabling USB0 in the bootloader makes the OF not load,
130 Also something here breaks usb pin detect in bootloader.
131 leave it all enabled untill rockbox main loads */
134 DEV_INIT2
&= ~INIT_USB
;
137 /* These set INV_LEV to the inserted level so it will fire if already
138 * inserted at the time they are enabled. */
139 #ifdef USB_STATUS_BY_EVENT
140 GPIO_CLEAR_BITWISE(USB_GPIO_INT_EN
, USB_GPIO_MASK
);
141 GPIO_CLEAR_BITWISE(USB_GPIO_OUTPUT_EN
, USB_GPIO_MASK
);
142 GPIO_SET_BITWISE(USB_GPIO_ENABLE
, USB_GPIO_MASK
);
143 GPIO_WRITE_BITWISE(USB_GPIO_INT_LEV
, USB_GPIO_VAL
, USB_GPIO_MASK
);
144 USB_GPIO_INT_CLR
= USB_GPIO_MASK
;
145 GPIO_SET_BITWISE(USB_GPIO_INT_EN
, USB_GPIO_MASK
);
146 CPU_HI_INT_EN
= USB_GPIO_HI_INT_MASK
;
148 #ifdef USB_FIREWIRE_HANDLING
149 /* GPIO C bit 1 is firewire detect */
150 GPIO_CLEAR_BITWISE(GPIOC_INT_EN
, 0x02);
151 GPIO_CLEAR_BITWISE(GPIOC_OUTPUT_EN
, 0x02);
152 GPIO_SET_BITWISE(GPIOC_ENABLE
, 0x02);
153 GPIO_WRITE_BITWISE(GPIOC_INT_LEV
, 0x00, 0x02);
154 GPIOC_INT_CLR
= 0x02;
155 GPIO_SET_BITWISE(GPIOC_INT_EN
, 0x02);
156 CPU_HI_INT_EN
= GPIO0_MASK
;
158 CPU_INT_EN
= HI_MASK
;
160 /* No interrupt - setup pin read only (BOOTLOADER) */
161 GPIO_CLEAR_BITWISE(USB_GPIO_OUTPUT_EN
, USB_GPIO_MASK
);
162 GPIO_SET_BITWISE(USB_GPIO_ENABLE
, USB_GPIO_MASK
);
163 #ifdef USB_FIREWIRE_HANDLING
164 /* GPIO C bit 1 is firewire detect */
165 GPIO_CLEAR_BITWISE(GPIOC_OUTPUT_EN
, 0x02);
166 GPIO_SET_BITWISE(GPIOC_ENABLE
, 0x02);
168 #endif /* USB_STATUS_BY_EVENT */
171 void usb_enable(bool on
)
174 /* if USB is detected, re-enable the USB-devices, otherwise make sure it's disabled */
177 DEV_INIT2
|= INIT_USB
;
182 /* Disable USB devices */
185 DEV_INIT2
&=~ INIT_USB
;
189 void usb_attach(void)
194 static bool usb_pin_state(void)
196 return (USB_GPIO_INPUT_VAL
& USB_GPIO_MASK
) == USB_GPIO_VAL
;
199 #ifdef USB_STATUS_BY_EVENT
200 /* Cannot always tell power pin from USB pin */
201 static int usb_status
= USB_EXTRACTED
;
203 static int usb_timeout_event(struct timeout
*tmo
)
205 usb_status_event(tmo
->data
== USB_GPIO_VAL
? USB_POWERED
: USB_UNPOWERED
);
209 void usb_insert_int(void)
211 static struct timeout usb_oneshot
;
212 unsigned long val
= USB_GPIO_INPUT_VAL
& USB_GPIO_MASK
;
213 usb_status
= (val
== USB_GPIO_VAL
) ? USB_INSERTED
: USB_EXTRACTED
;
214 GPIO_WRITE_BITWISE(USB_GPIO_INT_LEV
, val
^ USB_GPIO_MASK
, USB_GPIO_MASK
);
215 USB_GPIO_INT_CLR
= USB_GPIO_MASK
;
216 timeout_register(&usb_oneshot
, usb_timeout_event
, HZ
/5, val
);
219 /* Called during the bus reset interrupt when in detect mode - filter for
220 * invalid bus reset when unplugging by checking the pin state. */
221 void usb_drv_usb_detect_event(void)
223 if(usb_pin_state()) {
224 usb_status_event(USB_INSERTED
);
227 #endif /* USB_STATUS_BY_EVENT */
229 void usb_drv_int_enable(bool enable
)
231 /* enable/disable USB IRQ in CPU */
233 CPU_INT_EN
= USB_MASK
;
236 CPU_INT_DIS
= USB_MASK
;
240 /* detect host or charger (INSERTED or EXTRACTED) */
243 #ifdef USB_STATUS_BY_EVENT
246 return usb_pin_state() ? USB_INSERTED
: USB_EXTRACTED
;
250 #ifdef USB_FIREWIRE_HANDLING
251 #ifdef USB_STATUS_BY_EVENT
252 static bool firewire_status
= false;
255 bool firewire_detect(void)
257 #ifdef USB_STATUS_BY_EVENT
258 return firewire_status
;
260 /* GPIO C bit 1 is firewire detect */
261 /* no charger detection needed for firewire */
262 return (GPIOC_INPUT_VAL
& 0x02) == 0x00;
266 #ifdef USB_STATUS_BY_EVENT
267 static int firewire_timeout_event(struct timeout
*tmo
)
269 if (tmo
->data
== 0x00)
270 usb_firewire_connect_event();
274 void firewire_insert_int(void)
276 static struct timeout firewire_oneshot
;
277 unsigned long val
= GPIOC_INPUT_VAL
& 0x02;
278 firewire_status
= val
== 0x00;
279 GPIO_WRITE_BITWISE(GPIOC_INT_LEV
, val
^ 0x02, 0x02);
280 GPIOC_INT_CLR
= 0x02;
281 timeout_register(&firewire_oneshot
, firewire_timeout_event
, HZ
/5, val
);
283 #endif /* USB_STATUS_BY_EVENT */
284 #endif /* USB_FIREWIRE_HANDLING */