Add initial Packard Bell Vibe 500 port, by Szymon Dziok
[kugel-rb.git] / firmware / target / arm / usb-fw-pp502x.c
blob0b474c96bf61d27369e38c5656277f3b8f057b7a
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 * 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 ****************************************************************************/
26 #include "config.h"
27 #include "system.h"
28 #include "usb-target.h"
29 #include "usb.h"
30 #include "button.h"
31 #include "ata.h"
32 #include "string.h"
33 #include "usb_core.h"
34 #include "usb_drv.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
97 #else
98 #error No USB GPIO config specified
99 #endif
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);
114 DEV_EN |= DEV_USB0;
115 DEV_EN |= DEV_USB1;
117 /* reset both USBs */
118 DEV_RS |= DEV_USB0;
119 DEV_RS &=~DEV_USB0;
120 DEV_RS |= DEV_USB1;
121 DEV_RS &=~DEV_USB1;
123 DEV_INIT2 |= INIT_USB;
125 while ((inl(0x70000028) & 0x80) == 0);
126 outl(inl(0x70000028) | 0x2, 0x70000028);
127 udelay(100000);
128 XMB_RAM_CFG |= 0x47A;
130 /* Do one-time inits */
131 usb_drv_startup();
133 /* disable USB-devices until USB is detected via GPIO */
134 #ifndef BOOTLOADER
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 */
138 DEV_EN &= ~DEV_USB0;
139 DEV_EN &= ~DEV_USB1;
140 DEV_INIT2 &= ~INIT_USB;
141 #endif
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;
163 #endif
164 CPU_INT_EN = HI_MASK;
165 #else
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);
173 #endif
174 #endif /* USB_STATUS_BY_EVENT */
177 void usb_enable(bool on)
179 if (on) {
180 /* if USB is detected, re-enable the USB-devices, otherwise make sure it's disabled */
181 DEV_EN |= DEV_USB0;
182 DEV_EN |= DEV_USB1;
183 DEV_INIT2 |= INIT_USB;
184 usb_core_init();
186 else {
187 usb_core_exit();
188 /* Disable USB devices */
189 DEV_EN &=~ DEV_USB0;
190 DEV_EN &=~ DEV_USB1;
191 DEV_INIT2 &=~ INIT_USB;
195 void usb_attach(void)
197 usb_drv_attach();
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);
212 return 0;
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 */
238 if(enable) {
239 CPU_INT_EN = USB_MASK;
241 else {
242 CPU_INT_DIS = USB_MASK;
246 /* detect host or charger (INSERTED or EXTRACTED) */
247 int usb_detect(void)
249 #ifdef USB_STATUS_BY_EVENT
250 return usb_status;
251 #else
252 return usb_pin_state() ? USB_INSERTED : USB_EXTRACTED;
253 #endif
256 #ifdef USB_FIREWIRE_HANDLING
257 #ifdef USB_STATUS_BY_EVENT
258 static bool firewire_status = false;
259 #endif
261 bool firewire_detect(void)
263 #ifdef USB_STATUS_BY_EVENT
264 return firewire_status;
265 #else
266 /* GPIO C bit 1 is firewire detect */
267 /* no charger detection needed for firewire */
268 return (GPIOC_INPUT_VAL & 0x02) == 0x00;
269 #endif
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();
277 return 0;
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 */