blob: ca5c630e30d2ae549ff0b5f51d11f4e35f486d0e [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 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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.
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include "config.h"
22
23#include <stdlib.h>
24#include <stdio.h>
25#include "inttypes.h"
26#include "string.h"
27#include "cpu.h"
28#include "system.h"
29#include "lcd.h"
30#include "lcd-remote.h"
Michael Sevakis0a1b2482007-07-28 08:58:05 +000031#include "scroll_engine.h"
Michael Sevakis4ea4cdf2014-08-08 02:28:11 -040032#include "../kernel-internal.h"
Frank Gevaerts2f8a0082008-11-01 16:14:28 +000033#include "storage.h"
Michael Sevakis7d1a47c2013-08-05 22:02:45 -040034#include "file_internal.h"
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +000035#include "usb.h"
36#include "disk.h"
37#include "font.h"
38#include "adc.h"
39#include "backlight.h"
40#include "backlight-target.h"
41#include "button.h"
42#include "panic.h"
43#include "power.h"
Jens Arnold839ffbf2007-08-17 06:59:05 +000044#include "powermgmt.h"
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +000045#include "file.h"
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +000046#include "pcf50606.h"
47#include "common.h"
Marcin Bukat0b296912012-03-04 15:34:29 +010048#include "rb-loader.h"
49#include "loader_strerror.h"
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +000050#include "rbunicode.h"
Linus Nielsen Feltzing8448d3b2007-03-02 13:04:57 +000051#include "isp1362.h"
Rafaël Carré5d236b22010-05-27 09:41:46 +000052#include "version.h"
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +000053
54#include <stdarg.h>
55
56/* Maximum allowed firmware image size. 10MB is more than enough */
57#define MAX_LOADSIZE (10*1024*1024)
58
59#define DRAM_START 0x31000000
60
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +000061/* Reset the cookie for the crt0 crash check */
62inline void __reset_cookie(void)
63{
64 asm(" move.l #0,%d0");
65 asm(" move.l %d0,0x10017ffc");
66}
67
68void start_iriver_fw(void)
69{
70 asm(" move.w #0x2700,%sr");
71 __reset_cookie();
72 asm(" movec.l %d0,%vbr");
73 asm(" move.l 0,%sp");
74 asm(" lea.l 8,%a0");
75 asm(" jmp (%a0)");
76}
77
78void start_firmware(void)
79{
80 asm(" move.w #0x2700,%sr");
81 __reset_cookie();
82 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
83 asm(" movec.l %d0,%vbr");
84 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
85 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
86 asm(" jmp (%a0)");
87}
88
89void shutdown(void)
90{
91 printf("Shutting down...");
92
93 /* We need to gracefully spin down the disk to prevent clicks. */
94 if (ide_powered())
95 {
96 /* Make sure ATA has been initialized. */
Frank Gevaerts2f8a0082008-11-01 16:14:28 +000097 storage_init();
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +000098
99 /* And put the disk into sleep immediately. */
Frank Gevaerts2f8a0082008-11-01 16:14:28 +0000100 storage_sleepnow();
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000101 }
102
103 sleep(HZ*2);
104
Marcin Bukat89ba7e82015-01-09 00:22:40 +0100105 backlight_hw_off();
106 remote_backlight_hw_off();
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000107
108 __reset_cookie();
109 power_off();
110}
111
112/* Print the battery voltage (and a warning message). */
113void check_battery(void)
114{
Jens Arnold0fac4922007-08-17 06:45:18 +0000115 int battery_voltage, batt_int, batt_frac;
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000116
Thomas Martitzc1bd9b02012-01-03 23:44:38 +0000117 battery_voltage = _battery_voltage();
Jens Arnold0fac4922007-08-17 06:45:18 +0000118 batt_int = battery_voltage / 1000;
119 batt_frac = (battery_voltage % 1000) / 10;
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000120
121 printf("Batt: %d.%02dV", batt_int, batt_frac);
122
123 if (battery_voltage <= 310)
124 {
125 printf("WARNING! BATTERY LOW!!");
126 sleep(HZ*2);
127 }
128}
129
130/* From the pcf50606 driver */
131extern unsigned char pcf50606_intregs[3];
132
133/* From common.c */
134extern int line;
135extern int remote_line;
136
137void main(void)
138{
139 int i;
140 int rc;
141 bool rc_on_button = false;
142 bool on_button = false;
143 bool rec_button = false;
144 bool hold_status = false;
145 int data;
146 bool rtc_alarm;
147 int button;
Christian Gmeiner21ac3112007-11-20 12:09:59 +0000148 int mask;
149
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000150 /* We want to read the buttons as early as possible, before the user
151 releases the ON button */
152
153 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
154 (The ON and Hold buttons on the main unit and the remote) */
155 or_l(0x00100062, &GPIO1_FUNCTION);
156 and_l(~0x00100062, &GPIO1_ENABLE);
157
158 data = GPIO1_READ;
159 if ((data & 0x20) == 0)
160 on_button = true;
161
162 if ((data & 0x40) == 0)
163 rc_on_button = true;
164
165 /* Set the default state of the hard drive power to OFF */
166 ide_power_enable(false);
167
168 power_init();
169
170 /* Check the interrupt registers if it was an RTC alarm */
171 rtc_alarm = (pcf50606_intregs[0] & 0x80)?true:false;
172
173 /* Turn off if we believe the start was accidental */
174 if(!(rtc_alarm || on_button || rc_on_button ||
Dave Chapman16723502007-09-04 08:03:07 +0000175 (usb_detect() == USB_INSERTED) || charger_inserted())) {
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000176 __reset_cookie();
177 power_off();
178 }
Christian Gmeiner21ac3112007-11-20 12:09:59 +0000179
180 /* get rid of a nasty humming sound during boot */
Michael Sevakisaf395f42008-03-26 01:50:41 +0000181 mask = disable_irq_save();
Christian Gmeiner21ac3112007-11-20 12:09:59 +0000182 pcf50606_write(0x3b, 0x00); /* GPOOD2 high Z */
183 pcf50606_write(0x3b, 0x07); /* GPOOD2 low */
Michael Sevakisaf395f42008-03-26 01:50:41 +0000184 restore_irq(mask);
Christian Gmeiner21ac3112007-11-20 12:09:59 +0000185
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000186 /* Start with the main backlight OFF. */
Marcin Bukat89ba7e82015-01-09 00:22:40 +0100187 backlight_hw_init();
188 backlight_hw_off();
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000189
Marcin Bukat89ba7e82015-01-09 00:22:40 +0100190 remote_backlight_hw_on();
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000191
192 system_init();
193 kernel_init();
194
195 /* Set up waitstates for the peripherals */
196 set_cpu_frequency(0); /* PLL off */
197 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
Michael Sevakisaf395f42008-03-26 01:50:41 +0000198 enable_irq();
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000199
Linus Nielsen Feltzing8448d3b2007-03-02 13:04:57 +0000200 isp1362_init();
201
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000202 adc_init();
203 button_init();
204
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000205 backlight_init();
206
207 lcd_init();
208 lcd_remote_init();
209 font_init();
210
211 lcd_setfont(FONT_SYSFIXED);
212
213 printf("Rockbox boot loader");
Michael Sevakis95a4c3a2014-08-28 10:26:45 -0400214 printf("Version %s", rbversion);
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000215
216 sleep(HZ/50); /* Allow the button driver to check the buttons */
217 rec_button = ((button_status() & BUTTON_REC) == BUTTON_REC)
218 || ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC);
219
220 check_battery();
221
Steve Bavin88f249d2007-03-07 11:52:17 +0000222 if(rtc_alarm)
223 printf("RTC alarm detected");
224
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000225 /* Don't start if the Hold button is active on the device you
226 are starting with */
Steve Bavin88f249d2007-03-07 11:52:17 +0000227 if ((on_button && button_hold()) ||
228 (rc_on_button && remote_button_hold()))
229 {
230 hold_status = true;
231 }
Dave Chapman16723502007-09-04 08:03:07 +0000232 if (hold_status && !rtc_alarm && (usb_detect() != USB_INSERTED) &&
233 !charger_inserted())
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000234 {
235 if (detect_original_firmware())
236 {
237 printf("Hold switch on");
238 shutdown();
239 }
240 }
241
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000242 /* Holding REC while starting runs the original firmware */
243 if (detect_original_firmware() && rec_button)
244 {
245 printf("Starting original firmware...");
246 start_iriver_fw();
247 }
248
249 if(charger_inserted())
250 {
251 const char charging_msg[] = "Charging...";
252 const char complete_msg[] = "Charging complete";
253 const char *msg;
254 int w, h;
255 bool blink_toggle = false;
256 bool request_start = false;
257
Linus Nielsen Feltzing8448d3b2007-03-02 13:04:57 +0000258 cpu_idle_mode(true);
259
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000260 while(charger_inserted() && !request_start)
261 {
262 button = button_get_w_tmo(HZ);
263
264 switch(button)
265 {
266 case BUTTON_ON:
267 request_start = true;
268 break;
269
270 case BUTTON_NONE: /* Timeout */
271
272 if(charging_state())
273 {
274 /* To be replaced with a nice animation */
275 blink_toggle = !blink_toggle;
276 msg = charging_msg;
277 }
278 else
279 {
280 blink_toggle = true;
281 msg = complete_msg;
282 }
283
284 font_getstringsize(msg, &w, &h, FONT_SYSFIXED);
285 reset_screen();
286 if(blink_toggle)
287 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg);
288
289 check_battery();
290 break;
291 }
292
Dave Chapman16723502007-09-04 08:03:07 +0000293 if(usb_detect() == USB_INSERTED)
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000294 request_start = true;
295 }
296 if(!request_start)
297 {
298 __reset_cookie();
299 power_off();
300 }
Linus Nielsen Feltzing8448d3b2007-03-02 13:04:57 +0000301
302 cpu_idle_mode(false);
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000303 }
304
305 usb_init();
306
307 /* A hack to enter USB mode without using the USB thread */
Dave Chapman16723502007-09-04 08:03:07 +0000308 if(usb_detect() == USB_INSERTED)
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000309 {
310 const char msg[] = "Bootloader USB mode";
311 int w, h;
312 font_getstringsize(msg, &w, &h, FONT_SYSFIXED);
313 reset_screen();
314 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg);
315 lcd_update();
316
317 lcd_remote_puts(0, 3, msg);
318 lcd_remote_update();
319
320 ide_power_enable(true);
Frank Gevaerts2f8a0082008-11-01 16:14:28 +0000321 storage_enable(false);
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000322 sleep(HZ/20);
323 usb_enable(true);
324 cpu_idle_mode(true);
Dave Chapman16723502007-09-04 08:03:07 +0000325 while (usb_detect() == USB_INSERTED)
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000326 {
327 /* Print the battery status. */
328 line = 0;
329 remote_line = 0;
330 check_battery();
331
Frank Gevaerts2f8a0082008-11-01 16:14:28 +0000332 storage_spin(); /* Prevent the drive from spinning down */
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000333 sleep(HZ);
334 }
335
336 cpu_idle_mode(false);
337 usb_enable(false);
338
339 reset_screen();
340 lcd_update();
341 }
342
Frank Gevaerts2f8a0082008-11-01 16:14:28 +0000343 rc = storage_init();
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000344 if(rc)
345 {
346 reset_screen();
347 printf("ATA error: %d", rc);
348 printf("Insert USB cable and press");
349 printf("a button");
350 while(!(button_get(true) & BUTTON_REL));
351 }
352
Michael Sevakis7d1a47c2013-08-05 22:02:45 -0400353 filesystem_init();
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000354
355 rc = disk_mount_all();
356 if (rc<=0)
357 {
358 reset_screen();
359 printf("No partition found");
360 while(button_get(true) != SYS_USB_CONNECTED) {};
361 }
362
363 printf("Loading firmware");
364 i = load_firmware((unsigned char *)DRAM_START, BOOTFILE, MAX_LOADSIZE);
Marcin Bukat0b296912012-03-04 15:34:29 +0100365 if(i <= EFILE_EMPTY)
366 printf("Error: %s", loader_strerror(i));
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000367
Marcin Bukat0b296912012-03-04 15:34:29 +0100368 if (i > 0)
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000369 start_firmware();
370
371 if (!detect_original_firmware())
372 {
373 printf("No firmware found on disk");
374 sleep(HZ*2);
375 shutdown();
376 }
377 else {
378 sleep(HZ*2);
379 start_iriver_fw();
380 }
381}
382
383/* These functions are present in the firmware library, but we reimplement
384 them here because the originals do a lot more than we want */
385void screen_dump(void)
386{
387}
388
389int usb_screen(void)
390{
391 return 0;
392}
393
394unsigned short *bidi_l2v(const unsigned char *str, int orientation)
395{
396 static unsigned short utf16_buf[SCROLL_LINE_SIZE];
397 unsigned short *target;
398 (void)orientation;
399
400 target = utf16_buf;
401
402 while (*str)
403 str = utf8decode(str, target++);
404 *target = 0;
405 return utf16_buf;
406}