blob: 3349be4c2b917f60e7d2c5fe3657949cb5723883 [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"
Jens Arnold839ffbf2007-08-17 06:59:05 +000042#include "powermgmt.h"
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +000043#include "file.h"
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +000044#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
Jens Arnoldef12b3b2007-11-12 18:49:53 +0000102 _backlight_off();
103 _remote_backlight_off();
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000104
105 __reset_cookie();
106 power_off();
107}
108
109/* Print the battery voltage (and a warning message). */
110void check_battery(void)
111{
Jens Arnold0fac4922007-08-17 06:45:18 +0000112 int battery_voltage, batt_int, batt_frac;
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000113
Jens Arnold0fac4922007-08-17 06:45:18 +0000114 battery_voltage = battery_adc_voltage();
115 batt_int = battery_voltage / 1000;
116 batt_frac = (battery_voltage % 1000) / 10;
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000117
118 printf("Batt: %d.%02dV", batt_int, batt_frac);
119
120 if (battery_voltage <= 310)
121 {
122 printf("WARNING! BATTERY LOW!!");
123 sleep(HZ*2);
124 }
125}
126
127/* From the pcf50606 driver */
128extern unsigned char pcf50606_intregs[3];
129
130/* From common.c */
131extern int line;
132extern int remote_line;
133
134void main(void)
135{
136 int i;
137 int rc;
138 bool rc_on_button = false;
139 bool on_button = false;
140 bool rec_button = false;
141 bool hold_status = false;
142 int data;
143 bool rtc_alarm;
144 int button;
Christian Gmeiner21ac3112007-11-20 12:09:59 +0000145 int mask;
146
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000147 /* We want to read the buttons as early as possible, before the user
148 releases the ON button */
149
150 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
151 (The ON and Hold buttons on the main unit and the remote) */
152 or_l(0x00100062, &GPIO1_FUNCTION);
153 and_l(~0x00100062, &GPIO1_ENABLE);
154
155 data = GPIO1_READ;
156 if ((data & 0x20) == 0)
157 on_button = true;
158
159 if ((data & 0x40) == 0)
160 rc_on_button = true;
161
162 /* Set the default state of the hard drive power to OFF */
163 ide_power_enable(false);
164
165 power_init();
166
167 /* Check the interrupt registers if it was an RTC alarm */
168 rtc_alarm = (pcf50606_intregs[0] & 0x80)?true:false;
169
170 /* Turn off if we believe the start was accidental */
171 if(!(rtc_alarm || on_button || rc_on_button ||
Dave Chapman16723502007-09-04 08:03:07 +0000172 (usb_detect() == USB_INSERTED) || charger_inserted())) {
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000173 __reset_cookie();
174 power_off();
175 }
Christian Gmeiner21ac3112007-11-20 12:09:59 +0000176
177 /* get rid of a nasty humming sound during boot */
Michael Sevakisaf395f42008-03-26 01:50:41 +0000178 mask = disable_irq_save();
Christian Gmeiner21ac3112007-11-20 12:09:59 +0000179 pcf50606_write(0x3b, 0x00); /* GPOOD2 high Z */
180 pcf50606_write(0x3b, 0x07); /* GPOOD2 low */
Michael Sevakisaf395f42008-03-26 01:50:41 +0000181 restore_irq(mask);
Christian Gmeiner21ac3112007-11-20 12:09:59 +0000182
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000183 /* Start with the main backlight OFF. */
Jens Arnoldef12b3b2007-11-12 18:49:53 +0000184 _backlight_init();
185 _backlight_off();
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000186
Jens Arnoldef12b3b2007-11-12 18:49:53 +0000187 _remote_backlight_on();
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000188
189 system_init();
190 kernel_init();
191
192 /* Set up waitstates for the peripherals */
193 set_cpu_frequency(0); /* PLL off */
194 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
Michael Sevakisaf395f42008-03-26 01:50:41 +0000195 enable_irq();
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000196
Linus Nielsen Feltzing8448d3b2007-03-02 13:04:57 +0000197 isp1362_init();
198
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000199 adc_init();
200 button_init();
201
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000202 backlight_init();
203
204 lcd_init();
205 lcd_remote_init();
206 font_init();
207
208 lcd_setfont(FONT_SYSFIXED);
209
210 printf("Rockbox boot loader");
211 printf("Version %s", version);
212
213 sleep(HZ/50); /* Allow the button driver to check the buttons */
214 rec_button = ((button_status() & BUTTON_REC) == BUTTON_REC)
215 || ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC);
216
217 check_battery();
218
Steve Bavin88f249d2007-03-07 11:52:17 +0000219 if(rtc_alarm)
220 printf("RTC alarm detected");
221
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000222 /* Don't start if the Hold button is active on the device you
223 are starting with */
Steve Bavin88f249d2007-03-07 11:52:17 +0000224 if ((on_button && button_hold()) ||
225 (rc_on_button && remote_button_hold()))
226 {
227 hold_status = true;
228 }
Dave Chapman16723502007-09-04 08:03:07 +0000229 if (hold_status && !rtc_alarm && (usb_detect() != USB_INSERTED) &&
230 !charger_inserted())
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000231 {
232 if (detect_original_firmware())
233 {
234 printf("Hold switch on");
235 shutdown();
236 }
237 }
238
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000239 /* Holding REC while starting runs the original firmware */
240 if (detect_original_firmware() && rec_button)
241 {
242 printf("Starting original firmware...");
243 start_iriver_fw();
244 }
245
246 if(charger_inserted())
247 {
248 const char charging_msg[] = "Charging...";
249 const char complete_msg[] = "Charging complete";
250 const char *msg;
251 int w, h;
252 bool blink_toggle = false;
253 bool request_start = false;
254
Linus Nielsen Feltzing8448d3b2007-03-02 13:04:57 +0000255 cpu_idle_mode(true);
256
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000257 while(charger_inserted() && !request_start)
258 {
259 button = button_get_w_tmo(HZ);
260
261 switch(button)
262 {
263 case BUTTON_ON:
264 request_start = true;
265 break;
266
267 case BUTTON_NONE: /* Timeout */
268
269 if(charging_state())
270 {
271 /* To be replaced with a nice animation */
272 blink_toggle = !blink_toggle;
273 msg = charging_msg;
274 }
275 else
276 {
277 blink_toggle = true;
278 msg = complete_msg;
279 }
280
281 font_getstringsize(msg, &w, &h, FONT_SYSFIXED);
282 reset_screen();
283 if(blink_toggle)
284 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg);
285
286 check_battery();
287 break;
288 }
289
Dave Chapman16723502007-09-04 08:03:07 +0000290 if(usb_detect() == USB_INSERTED)
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000291 request_start = true;
292 }
293 if(!request_start)
294 {
295 __reset_cookie();
296 power_off();
297 }
Linus Nielsen Feltzing8448d3b2007-03-02 13:04:57 +0000298
299 cpu_idle_mode(false);
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000300 }
301
302 usb_init();
303
304 /* A hack to enter USB mode without using the USB thread */
Dave Chapman16723502007-09-04 08:03:07 +0000305 if(usb_detect() == USB_INSERTED)
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000306 {
307 const char msg[] = "Bootloader USB mode";
308 int w, h;
309 font_getstringsize(msg, &w, &h, FONT_SYSFIXED);
310 reset_screen();
311 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg);
312 lcd_update();
313
314 lcd_remote_puts(0, 3, msg);
315 lcd_remote_update();
316
317 ide_power_enable(true);
318 ata_enable(false);
319 sleep(HZ/20);
320 usb_enable(true);
321 cpu_idle_mode(true);
Dave Chapman16723502007-09-04 08:03:07 +0000322 while (usb_detect() == USB_INSERTED)
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000323 {
324 /* Print the battery status. */
325 line = 0;
326 remote_line = 0;
327 check_battery();
328
329 ata_spin(); /* Prevent the drive from spinning down */
330 sleep(HZ);
331 }
332
333 cpu_idle_mode(false);
334 usb_enable(false);
335
336 reset_screen();
337 lcd_update();
338 }
339
340 rc = ata_init();
341 if(rc)
342 {
343 reset_screen();
344 printf("ATA error: %d", rc);
345 printf("Insert USB cable and press");
346 printf("a button");
347 while(!(button_get(true) & BUTTON_REL));
348 }
349
350
351 disk_init();
352
353 rc = disk_mount_all();
354 if (rc<=0)
355 {
356 reset_screen();
357 printf("No partition found");
358 while(button_get(true) != SYS_USB_CONNECTED) {};
359 }
360
361 printf("Loading firmware");
362 i = load_firmware((unsigned char *)DRAM_START, BOOTFILE, MAX_LOADSIZE);
363 if(i < 0)
Steve Bavin0e16bd32007-06-18 06:52:29 +0000364 printf("Error: %s", strerror(i));
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000365
366 if (i == EOK)
367 start_firmware();
368
369 if (!detect_original_firmware())
370 {
371 printf("No firmware found on disk");
372 sleep(HZ*2);
373 shutdown();
374 }
375 else {
376 sleep(HZ*2);
377 start_iriver_fw();
378 }
379}
380
381/* These functions are present in the firmware library, but we reimplement
382 them here because the originals do a lot more than we want */
383void screen_dump(void)
384{
385}
386
387int usb_screen(void)
388{
389 return 0;
390}
391
392unsigned short *bidi_l2v(const unsigned char *str, int orientation)
393{
394 static unsigned short utf16_buf[SCROLL_LINE_SIZE];
395 unsigned short *target;
396 (void)orientation;
397
398 target = utf16_buf;
399
400 while (*str)
401 str = utf8decode(str, target++);
402 *target = 0;
403 return utf16_buf;
404}