blob: 32950a58dcef331be4f6019a297f41665981a325 [file] [log] [blame]
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2007 by Linus Nielsen Feltzing
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19#include "config.h"
20
21#include <stdlib.h>
22#include <stdio.h>
23#include "inttypes.h"
24#include "string.h"
25#include "cpu.h"
26#include "system.h"
27#include "lcd.h"
28#include "lcd-remote.h"
Michael Sevakis0a1b2482007-07-28 08:58:05 +000029#include "scroll_engine.h"
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +000030#include "kernel.h"
31#include "thread.h"
32#include "ata.h"
33#include "usb.h"
34#include "disk.h"
35#include "font.h"
36#include "adc.h"
37#include "backlight.h"
38#include "backlight-target.h"
39#include "button.h"
40#include "panic.h"
41#include "power.h"
42#include "file.h"
43#include "uda1380.h"
44#include "pcf50606.h"
45#include "common.h"
46#include "rbunicode.h"
Linus Nielsen Feltzing8448d3b2007-03-02 13:04:57 +000047#include "isp1362.h"
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +000048
49#include <stdarg.h>
50
51/* Maximum allowed firmware image size. 10MB is more than enough */
52#define MAX_LOADSIZE (10*1024*1024)
53
54#define DRAM_START 0x31000000
55
56char version[] = APPSVERSION;
57
58/* Reset the cookie for the crt0 crash check */
59inline void __reset_cookie(void)
60{
61 asm(" move.l #0,%d0");
62 asm(" move.l %d0,0x10017ffc");
63}
64
65void start_iriver_fw(void)
66{
67 asm(" move.w #0x2700,%sr");
68 __reset_cookie();
69 asm(" movec.l %d0,%vbr");
70 asm(" move.l 0,%sp");
71 asm(" lea.l 8,%a0");
72 asm(" jmp (%a0)");
73}
74
75void start_firmware(void)
76{
77 asm(" move.w #0x2700,%sr");
78 __reset_cookie();
79 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
80 asm(" movec.l %d0,%vbr");
81 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
82 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
83 asm(" jmp (%a0)");
84}
85
86void shutdown(void)
87{
88 printf("Shutting down...");
89
90 /* We need to gracefully spin down the disk to prevent clicks. */
91 if (ide_powered())
92 {
93 /* Make sure ATA has been initialized. */
94 ata_init();
95
96 /* And put the disk into sleep immediately. */
97 ata_sleepnow();
98 }
99
100 sleep(HZ*2);
101
102 __backlight_off();
103 __remote_backlight_off();
104
105 __reset_cookie();
106 power_off();
107}
108
109/* Print the battery voltage (and a warning message). */
110void check_battery(void)
111{
112 int adc_battery, battery_voltage, batt_int, batt_frac;
113
114 adc_battery = adc_read(ADC_BATTERY);
115
116 battery_voltage = (adc_battery * BATTERY_SCALE_FACTOR) / 10000;
117 batt_int = battery_voltage / 100;
118 batt_frac = battery_voltage % 100;
119
120 printf("Batt: %d.%02dV", batt_int, batt_frac);
121
122 if (battery_voltage <= 310)
123 {
124 printf("WARNING! BATTERY LOW!!");
125 sleep(HZ*2);
126 }
127}
128
129/* From the pcf50606 driver */
130extern unsigned char pcf50606_intregs[3];
131
132/* From common.c */
133extern int line;
134extern int remote_line;
135
136void main(void)
137{
138 int i;
139 int rc;
140 bool rc_on_button = false;
141 bool on_button = false;
142 bool rec_button = false;
143 bool hold_status = false;
144 int data;
145 bool rtc_alarm;
146 int button;
147
148 /* We want to read the buttons as early as possible, before the user
149 releases the ON button */
150
151 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
152 (The ON and Hold buttons on the main unit and the remote) */
153 or_l(0x00100062, &GPIO1_FUNCTION);
154 and_l(~0x00100062, &GPIO1_ENABLE);
155
156 data = GPIO1_READ;
157 if ((data & 0x20) == 0)
158 on_button = true;
159
160 if ((data & 0x40) == 0)
161 rc_on_button = true;
162
163 /* Set the default state of the hard drive power to OFF */
164 ide_power_enable(false);
165
166 power_init();
167
168 /* Check the interrupt registers if it was an RTC alarm */
169 rtc_alarm = (pcf50606_intregs[0] & 0x80)?true:false;
170
171 /* Turn off if we believe the start was accidental */
172 if(!(rtc_alarm || on_button || rc_on_button ||
173 usb_detect() || charger_inserted())) {
174 __reset_cookie();
175 power_off();
176 }
177
178 audiohw_reset();
179
180 /* Start with the main backlight OFF. */
181 __backlight_init();
182 __backlight_off();
183
184 __remote_backlight_on();
185
186 system_init();
187 kernel_init();
188
189 /* Set up waitstates for the peripherals */
190 set_cpu_frequency(0); /* PLL off */
191 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
192 set_irq_level(0);
193
Linus Nielsen Feltzing8448d3b2007-03-02 13:04:57 +0000194 isp1362_init();
195
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000196 adc_init();
197 button_init();
198
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000199 backlight_init();
200
201 lcd_init();
202 lcd_remote_init();
203 font_init();
204
205 lcd_setfont(FONT_SYSFIXED);
206
207 printf("Rockbox boot loader");
208 printf("Version %s", version);
209
210 sleep(HZ/50); /* Allow the button driver to check the buttons */
211 rec_button = ((button_status() & BUTTON_REC) == BUTTON_REC)
212 || ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC);
213
214 check_battery();
215
Steve Bavin88f249d2007-03-07 11:52:17 +0000216 if(rtc_alarm)
217 printf("RTC alarm detected");
218
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000219 /* Don't start if the Hold button is active on the device you
220 are starting with */
Steve Bavin88f249d2007-03-07 11:52:17 +0000221 if ((on_button && button_hold()) ||
222 (rc_on_button && remote_button_hold()))
223 {
224 hold_status = true;
225 }
226 if (hold_status && !rtc_alarm && !usb_detect() && !charger_inserted())
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000227 {
228 if (detect_original_firmware())
229 {
230 printf("Hold switch on");
231 shutdown();
232 }
233 }
234
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000235 /* Holding REC while starting runs the original firmware */
236 if (detect_original_firmware() && rec_button)
237 {
238 printf("Starting original firmware...");
239 start_iriver_fw();
240 }
241
242 if(charger_inserted())
243 {
244 const char charging_msg[] = "Charging...";
245 const char complete_msg[] = "Charging complete";
246 const char *msg;
247 int w, h;
248 bool blink_toggle = false;
249 bool request_start = false;
250
Linus Nielsen Feltzing8448d3b2007-03-02 13:04:57 +0000251 cpu_idle_mode(true);
252
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000253 while(charger_inserted() && !request_start)
254 {
255 button = button_get_w_tmo(HZ);
256
257 switch(button)
258 {
259 case BUTTON_ON:
260 request_start = true;
261 break;
262
263 case BUTTON_NONE: /* Timeout */
264
265 if(charging_state())
266 {
267 /* To be replaced with a nice animation */
268 blink_toggle = !blink_toggle;
269 msg = charging_msg;
270 }
271 else
272 {
273 blink_toggle = true;
274 msg = complete_msg;
275 }
276
277 font_getstringsize(msg, &w, &h, FONT_SYSFIXED);
278 reset_screen();
279 if(blink_toggle)
280 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg);
281
282 check_battery();
283 break;
284 }
285
286 if(usb_detect())
287 request_start = true;
288 }
289 if(!request_start)
290 {
291 __reset_cookie();
292 power_off();
293 }
Linus Nielsen Feltzing8448d3b2007-03-02 13:04:57 +0000294
295 cpu_idle_mode(false);
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000296 }
297
298 usb_init();
299
300 /* A hack to enter USB mode without using the USB thread */
301 if(usb_detect())
302 {
303 const char msg[] = "Bootloader USB mode";
304 int w, h;
305 font_getstringsize(msg, &w, &h, FONT_SYSFIXED);
306 reset_screen();
307 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg);
308 lcd_update();
309
310 lcd_remote_puts(0, 3, msg);
311 lcd_remote_update();
312
313 ide_power_enable(true);
314 ata_enable(false);
315 sleep(HZ/20);
316 usb_enable(true);
317 cpu_idle_mode(true);
318 while (usb_detect())
319 {
320 /* Print the battery status. */
321 line = 0;
322 remote_line = 0;
323 check_battery();
324
325 ata_spin(); /* Prevent the drive from spinning down */
326 sleep(HZ);
327 }
328
329 cpu_idle_mode(false);
330 usb_enable(false);
331
332 reset_screen();
333 lcd_update();
334 }
335
336 rc = ata_init();
337 if(rc)
338 {
339 reset_screen();
340 printf("ATA error: %d", rc);
341 printf("Insert USB cable and press");
342 printf("a button");
343 while(!(button_get(true) & BUTTON_REL));
344 }
345
346
347 disk_init();
348
349 rc = disk_mount_all();
350 if (rc<=0)
351 {
352 reset_screen();
353 printf("No partition found");
354 while(button_get(true) != SYS_USB_CONNECTED) {};
355 }
356
357 printf("Loading firmware");
358 i = load_firmware((unsigned char *)DRAM_START, BOOTFILE, MAX_LOADSIZE);
359 if(i < 0)
Steve Bavin0e16bd32007-06-18 06:52:29 +0000360 printf("Error: %s", strerror(i));
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000361
362 if (i == EOK)
363 start_firmware();
364
365 if (!detect_original_firmware())
366 {
367 printf("No firmware found on disk");
368 sleep(HZ*2);
369 shutdown();
370 }
371 else {
372 sleep(HZ*2);
373 start_iriver_fw();
374 }
375}
376
377/* These functions are present in the firmware library, but we reimplement
378 them here because the originals do a lot more than we want */
379void screen_dump(void)
380{
381}
382
383int usb_screen(void)
384{
385 return 0;
386}
387
388unsigned short *bidi_l2v(const unsigned char *str, int orientation)
389{
390 static unsigned short utf16_buf[SCROLL_LINE_SIZE];
391 unsigned short *target;
392 (void)orientation;
393
394 target = utf16_buf;
395
396 while (*str)
397 str = utf8decode(str, target++);
398 *target = 0;
399 return utf16_buf;
400}