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) || defined(PHILIPS_HDD6330)
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(TATUNG_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
91 #elif defined(PBELL_VIBE500)
92 /* GPIO L bit 3 is usb detect */
93 #define USB_GPIO GPIOL
94 #define USB_GPIO_MASK 0x04
95 #define USB_GPIO_VAL 0x04
98 #error No USB GPIO config specified
101 #define USB_GPIO_ENABLE GPIO_ENABLE(USB_GPIO)
102 #define USB_GPIO_OUTPUT_EN GPIO_OUTPUT_EN(USB_GPIO)
103 #define USB_GPIO_INPUT_VAL GPIO_INPUT_VAL(USB_GPIO)
104 #define USB_GPIO_INT_EN GPIO_INT_EN(USB_GPIO)
105 #define USB_GPIO_INT_LEV GPIO_INT_LEV(USB_GPIO)
106 #define USB_GPIO_INT_CLR GPIO_INT_CLR(USB_GPIO)
107 #define USB_GPIO_HI_INT_MASK GPIO_HI_INT_MASK(USB_GPIO)
109 void usb_init_device(void)
111 /* enable usb module */
112 outl(inl(0x7000002C) | 0x3000000, 0x7000002C);
117 /* reset both USBs */
123 DEV_INIT2
|= INIT_USB
;
125 while ((inl(0x70000028) & 0x80) == 0);
126 outl(inl(0x70000028) | 0x2, 0x70000028);
128 XMB_RAM_CFG
|= 0x47A;
130 /* Do one-time inits */
133 /* disable USB-devices until USB is detected via GPIO */
135 /* Disabling USB0 in the bootloader makes the OF not load,
136 Also something here breaks usb pin detect in bootloader.
137 leave it all enabled untill rockbox main loads */
140 DEV_INIT2
&= ~INIT_USB
;
143 /* These set INV_LEV to the inserted level so it will fire if already
144 * inserted at the time they are enabled. */
145 #ifdef USB_STATUS_BY_EVENT
146 GPIO_CLEAR_BITWISE(USB_GPIO_INT_EN
, USB_GPIO_MASK
);
147 GPIO_CLEAR_BITWISE(USB_GPIO_OUTPUT_EN
, USB_GPIO_MASK
);
148 GPIO_SET_BITWISE(USB_GPIO_ENABLE
, USB_GPIO_MASK
);
149 GPIO_WRITE_BITWISE(USB_GPIO_INT_LEV
, USB_GPIO_VAL
, USB_GPIO_MASK
);
150 USB_GPIO_INT_CLR
= USB_GPIO_MASK
;
151 GPIO_SET_BITWISE(USB_GPIO_INT_EN
, USB_GPIO_MASK
);
152 CPU_HI_INT_EN
= USB_GPIO_HI_INT_MASK
;
154 #ifdef USB_FIREWIRE_HANDLING
155 /* GPIO C bit 1 is firewire detect */
156 GPIO_CLEAR_BITWISE(GPIOC_INT_EN
, 0x02);
157 GPIO_CLEAR_BITWISE(GPIOC_OUTPUT_EN
, 0x02);
158 GPIO_SET_BITWISE(GPIOC_ENABLE
, 0x02);
159 GPIO_WRITE_BITWISE(GPIOC_INT_LEV
, 0x00, 0x02);
160 GPIOC_INT_CLR
= 0x02;
161 GPIO_SET_BITWISE(GPIOC_INT_EN
, 0x02);
162 CPU_HI_INT_EN
= GPIO0_MASK
;
164 CPU_INT_EN
= HI_MASK
;
166 /* No interrupt - setup pin read only (BOOTLOADER) */
167 GPIO_CLEAR_BITWISE(USB_GPIO_OUTPUT_EN
, USB_GPIO_MASK
);
168 GPIO_SET_BITWISE(USB_GPIO_ENABLE
, USB_GPIO_MASK
);
169 #ifdef USB_FIREWIRE_HANDLING
170 /* GPIO C bit 1 is firewire detect */
171 GPIO_CLEAR_BITWISE(GPIOC_OUTPUT_EN
, 0x02);
172 GPIO_SET_BITWISE(GPIOC_ENABLE
, 0x02);
174 #endif /* USB_STATUS_BY_EVENT */
177 void usb_enable(bool on
)
180 /* if USB is detected, re-enable the USB-devices, otherwise make sure it's disabled */
183 DEV_INIT2
|= INIT_USB
;
188 /* Disable USB devices */
191 DEV_INIT2
&=~ INIT_USB
;
195 void usb_attach(void)
200 static bool usb_pin_state(void)
202 return (USB_GPIO_INPUT_VAL
& USB_GPIO_MASK
) == USB_GPIO_VAL
;
205 #ifdef USB_STATUS_BY_EVENT
206 /* Cannot always tell power pin from USB pin */
207 static int usb_status
= USB_EXTRACTED
;
209 static int usb_timeout_event(struct timeout
*tmo
)
211 usb_status_event(tmo
->data
== USB_GPIO_VAL
? USB_POWERED
: USB_UNPOWERED
);
215 void usb_insert_int(void)
217 static struct timeout usb_oneshot
;
218 unsigned long val
= USB_GPIO_INPUT_VAL
& USB_GPIO_MASK
;
219 usb_status
= (val
== USB_GPIO_VAL
) ? USB_INSERTED
: USB_EXTRACTED
;
220 GPIO_WRITE_BITWISE(USB_GPIO_INT_LEV
, val
^ USB_GPIO_MASK
, USB_GPIO_MASK
);
221 USB_GPIO_INT_CLR
= USB_GPIO_MASK
;
222 timeout_register(&usb_oneshot
, usb_timeout_event
, HZ
/5, val
);
225 /* Called during the bus reset interrupt when in detect mode - filter for
226 * invalid bus reset when unplugging by checking the pin state. */
227 void usb_drv_usb_detect_event(void)
229 if(usb_pin_state()) {
230 usb_status_event(USB_INSERTED
);
233 #endif /* USB_STATUS_BY_EVENT */
235 void usb_drv_int_enable(bool enable
)
237 /* enable/disable USB IRQ in CPU */
239 CPU_INT_EN
= USB_MASK
;
242 CPU_INT_DIS
= USB_MASK
;
246 /* detect host or charger (INSERTED or EXTRACTED) */
249 #ifdef USB_STATUS_BY_EVENT
252 return usb_pin_state() ? USB_INSERTED
: USB_EXTRACTED
;
256 #ifdef USB_FIREWIRE_HANDLING
257 #ifdef USB_STATUS_BY_EVENT
258 static bool firewire_status
= false;
261 bool firewire_detect(void)
263 #ifdef USB_STATUS_BY_EVENT
264 return firewire_status
;
266 /* GPIO C bit 1 is firewire detect */
267 /* no charger detection needed for firewire */
268 return (GPIOC_INPUT_VAL
& 0x02) == 0x00;
272 #ifdef USB_STATUS_BY_EVENT
273 static int firewire_timeout_event(struct timeout
*tmo
)
275 if (tmo
->data
== 0x00)
276 usb_firewire_connect_event();
280 void firewire_insert_int(void)
282 static struct timeout firewire_oneshot
;
283 unsigned long val
= GPIOC_INPUT_VAL
& 0x02;
284 firewire_status
= val
== 0x00;
285 GPIO_WRITE_BITWISE(GPIOC_INT_LEV
, val
^ 0x02, 0x02);
286 GPIOC_INT_CLR
= 0x02;
287 timeout_register(&firewire_oneshot
, firewire_timeout_event
, HZ
/5, val
);
289 #endif /* USB_STATUS_BY_EVENT */
290 #endif /* USB_FIREWIRE_HANDLING */