Of course there's no mdelay on c200(v1), so just use udelay
[kugel-rb.git] / firmware / target / arm / ipod / power-ipod.c
bloba82a18f6991e2f88a14a49358155eb2aefc9fbe6
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
22 #include "cpu.h"
23 #include <stdbool.h>
24 #include "kernel.h"
25 #include "system.h"
26 #include "power.h"
27 #include "logf.h"
28 #include "pcf50605.h"
29 #include "usb.h"
30 #include "lcd.h"
31 #include "string.h"
33 void power_init(void)
35 #if defined(IPOD_1G2G) || defined(IPOD_3G)
36 GPIOC_ENABLE |= 0x40; /* GPIO C6 is HDD power (low active) */
37 GPIOC_OUTPUT_VAL &= ~0x40; /* on by default */
38 GPIOC_OUTPUT_EN |= 0x40; /* enable output */
39 #endif
40 #ifndef IPOD_1G2G
41 pcf50605_init();
42 #endif
45 #if CONFIG_CHARGING
46 unsigned int power_input_status(void)
48 unsigned int status = POWER_INPUT_NONE;
50 #if defined(IPOD_NANO) || defined(IPOD_VIDEO)
51 if ((GPIOL_INPUT_VAL & 0x08) == 0)
52 status = POWER_INPUT_MAIN_CHARGER;
54 if ((GPIOL_INPUT_VAL & 0x10) != 0)
55 status |= POWER_INPUT_USB_CHARGER;
56 /* */
57 #elif defined(IPOD_4G) || defined(IPOD_COLOR) \
58 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
59 /* C2 is firewire power */
60 if ((GPIOC_INPUT_VAL & 0x04) == 0)
61 status = POWER_INPUT_MAIN_CHARGER;
63 if ((GPIOD_INPUT_VAL & 0x08) != 0)
64 status |= POWER_INPUT_USB_CHARGER;
65 /* */
66 #elif defined(IPOD_3G)
67 /* firewire power */
68 if ((GPIOC_INPUT_VAL & 0x10) == 0)
69 status = POWER_INPUT_MAIN_CHARGER;
70 /* */
71 #else
72 /* This needs filling in for other ipods. */
73 #endif
75 return status;
78 /* Returns true if the unit is charging the batteries. */
79 bool charging_state(void) {
80 #if defined(IPOD_COLOR)
81 /* 0x70000088 appears to be the input value for GPO32 bits.
82 Write a zero to 0x70000088 before reading.
83 To enable input set the corresponding 0x7000008C bit,
84 and clear the corresponding GPO32_ENABLE bit. */
85 outl(0, 0x70000088);
86 return (inl(0x70000088) & 1)?false:true;
87 #else
88 return (GPIOB_INPUT_VAL & 0x01)?false:true;
89 #endif
91 #endif /* CONFIG_CHARGING */
94 void ide_power_enable(bool on)
96 #if defined(IPOD_1G2G) || defined(IPOD_3G)
97 if (on)
98 GPIOC_OUTPUT_VAL &= ~0x40;
99 else
100 GPIOC_OUTPUT_VAL |= 0x40;
101 #elif defined(IPOD_4G) || defined(IPOD_COLOR) \
102 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
103 if (on)
105 GPIO_CLEAR_BITWISE(GPIOJ_OUTPUT_VAL, 0x04);
106 DEV_EN |= DEV_IDE0;
108 else
110 DEV_EN &= ~DEV_IDE0;
111 GPIO_SET_BITWISE(GPIOJ_OUTPUT_VAL, 0x04);
113 #elif defined(IPOD_VIDEO)
114 if (on)
116 GPO32_VAL &= ~0x40000000;
117 sleep(1); /* only need 4 ms */
118 DEV_EN |= DEV_IDE0;
119 GPIOG_ENABLE = 0;
120 GPIOH_ENABLE = 0;
121 GPIO_CLEAR_BITWISE(GPIOI_ENABLE, 0xBF);
122 GPIO_CLEAR_BITWISE(GPIOK_ENABLE, 0x1F);
123 udelay(10);
125 else
127 DEV_EN &= ~DEV_IDE0;
128 udelay(10);
129 GPIOG_ENABLE = 0xFF;
130 GPIOH_ENABLE = 0xFF;
131 GPIO_SET_BITWISE(GPIOI_ENABLE, 0xBF);
132 GPIO_SET_BITWISE(GPIOK_ENABLE, 0x1F);
133 GPO32_VAL |= 0x40000000;
135 #else /* Nano */
136 (void)on; /* Do nothing. */
137 #endif
140 bool ide_powered(void)
142 #if defined(IPOD_1G2G) || defined(IPOD_3G)
143 return !(GPIOC_OUTPUT_VAL & 0x40);
144 #elif defined(IPOD_4G) || defined(IPOD_COLOR) \
145 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
146 return !(GPIOJ_OUTPUT_VAL & 0x04);
147 #elif defined(IPOD_VIDEO)
148 return !(GPO32_VAL & 0x40000000);
149 #else /* Nano */
150 return true; /* Pretend we are always powered */
151 #endif
154 void power_off(void)
156 #if defined(HAVE_LCD_COLOR) && !defined(HAVE_LCD_SHUTDOWN)
157 /* Clear the screen and backdrop to
158 remove ghosting effect on shutdown */
159 lcd_set_backdrop(NULL);
160 lcd_set_background(LCD_WHITE);
161 lcd_clear_display();
162 lcd_update();
163 sleep(HZ/16);
164 #endif
166 #ifndef BOOTLOADER
167 #ifdef IPOD_1G2G
168 /* we cannot turn off the 1st gen/ 2nd gen yet. Need to figure out sleep mode. */
169 system_reboot();
170 #else
171 /* We don't turn off the ipod, we put it in a deep sleep */
172 /* Clear latter part of iram (the part used by plugins/codecs) to ensure
173 * that the OF behaves properly on boot. There is some kind of boot
174 * failure flag there which otherwise may not be cleared.
176 #if CONFIG_CPU == PP5022
177 memset((void*)0x4000c000, 0, 0x14000);
178 #elif CONFIG_CPU == PP5020
179 memset((void*)0x4000c000, 0, 0xc000);
180 #endif
181 pcf50605_standby_mode();
182 #endif
183 #endif
186 #ifdef HAVE_USB_CHARGING_ENABLE
187 void usb_charging_maxcurrent_change(int maxcurrent)
189 bool suspend_charging = (maxcurrent < 100);
190 bool fast_charging = (maxcurrent >= 500);
192 /* This GPIO is connected to the LTC4066's SUSP pin */
193 /* Setting it high prevents any power being drawn over USB */
194 /* which supports USB suspend */
195 #if defined(IPOD_VIDEO) || defined(IPOD_NANO)
196 if (suspend_charging)
197 GPIO_SET_BITWISE(GPIOL_OUTPUT_VAL, 4);
198 else
199 GPIO_CLEAR_BITWISE(GPIOL_OUTPUT_VAL, 4);
200 #elif defined(IPOD_MINI2G)
201 if (suspend_charging)
202 GPIO_SET_BITWISE(GPIOJ_OUTPUT_VAL, 2);
203 else
204 GPIO_CLEAR_BITWISE(GPIOJ_OUTPUT_VAL, 2);
205 #else /* Color, 4G, Mini G1 */
206 if (suspend_charging)
207 GPO32_VAL |= 0x8000000;
208 else
209 GPO32_VAL &= ~0x8000000;
210 #endif
212 /* This GPIO is connected to the LTC4066's HPWR pin */
213 /* Setting it low limits current to 100mA, setting it high allows 500mA */
214 #if defined(IPOD_VIDEO) || defined(IPOD_NANO)
215 if (fast_charging)
216 GPIO_SET_BITWISE(GPIOA_OUTPUT_VAL, 4);
217 else
218 GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_VAL, 4);
219 #else
220 if (fast_charging)
221 GPO32_VAL |= 0x40;
222 else
223 GPO32_VAL &= ~0x40;
224 #endif
226 /* This GPIO is connected to the LTC4066's CLDIS pin */
227 /* Setting it high allows up to 1.5A of current to be drawn */
228 /* This doesn't appear to actually be safe even with an AC charger */
229 /* so for now it is disabled. It's not known (or maybe doesn't exist) */
230 /* on all models. */
231 #if 0
232 #if defined(IPOD_VIDEO)
233 if (unlimited_charging)
234 GPO32_VAL |= 0x10000000;
235 else
236 GPO32_VAL &= ~0x10000000;
237 #elif defined(IPOD_4G) || defined(IPOD_COLOR)
238 if (unlimited_charging)
239 GPO32_VAL |= 0x200;
240 else
241 GPO32_VAL &= ~0x200;
242 #endif
243 /* This might be GPIOD & 40 on 2G */
244 #endif
246 #endif /* HAVE_USB_CHARGING_ENABLE */