blob: 8e85dc9deb5fa699f517f364017c8a76fe0e69a8 [file] [log] [blame]
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
Linus Nielsen Feltzing4fb389f2007-02-22 21:44:12 +00008 * $Id$
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +00009 *
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 Feltzingec549ec2007-02-22 21:38:59 +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"
31#include "kernel.h"
32#include "thread.h"
33#include "ata.h"
34#include "usb.h"
35#include "disk.h"
36#include "font.h"
37#include "adc.h"
38#include "backlight.h"
39#include "backlight-target.h"
40#include "button.h"
41#include "panic.h"
42#include "power.h"
Jens Arnold839ffbf2007-08-17 06:59:05 +000043#include "powermgmt.h"
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +000044#include "file.h"
45
46#include "pcf50606.h"
47#include "common.h"
48
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
56int usb_screen(void)
57{
58 return 0;
59}
60
61char version[] = APPSVERSION;
62
63/* Reset the cookie for the crt0 crash check */
64inline void __reset_cookie(void)
65{
66 asm(" move.l #0,%d0");
67 asm(" move.l %d0,0x10017ffc");
68}
69
70void start_firmware(void)
71{
Jens Arnolded3ff1b2008-03-17 23:47:38 +000072 adc_close();
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +000073 asm(" move.w #0x2700,%sr");
74 __reset_cookie();
75 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
76 asm(" movec.l %d0,%vbr");
77 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
78 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
79 asm(" jmp (%a0)");
80}
81
82void shutdown(void)
83{
84 printf("Shutting down...");
85
86 /* We need to gracefully spin down the disk to prevent clicks. */
87 if (ide_powered())
88 {
89 /* Make sure ATA has been initialized. */
90 ata_init();
91
92 /* And put the disk into sleep immediately. */
93 ata_sleepnow();
94 }
95
96 sleep(HZ*2);
97
98 /* Backlight OFF */
Jens Arnoldef12b3b2007-11-12 18:49:53 +000099 _backlight_off();
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000100#ifdef HAVE_REMOTE_LCD
Jens Arnoldef12b3b2007-11-12 18:49:53 +0000101 _remote_backlight_off();
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000102#endif
103
104 __reset_cookie();
105 power_off();
106}
107
108/* Print the battery voltage (and a warning message). */
109void check_battery(void)
110{
Jens Arnold0fac4922007-08-17 06:45:18 +0000111 int battery_voltage, batt_int, batt_frac;
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000112
Jens Arnold0fac4922007-08-17 06:45:18 +0000113 battery_voltage = battery_adc_voltage();
114 batt_int = battery_voltage / 1000;
115 batt_frac = (battery_voltage % 1000) / 10;
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000116
117 printf("Batt: %d.%02dV", batt_int, batt_frac);
118
Jens Arnold80ded2a2008-03-12 23:19:44 +0000119 if (battery_voltage <= 3500)
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000120 {
121 printf("WARNING! BATTERY LOW!!");
122 sleep(HZ*2);
123 }
124}
125
126void main(void)
127{
128 int i;
129 int rc;
130 bool rc_on_button = false;
131 bool on_button = false;
Jens Arnolded3ff1b2008-03-17 23:47:38 +0000132
133 /* We want to read the buttons as early as possible, before the user
134 releases the ON button */
135
136#ifdef IAUDIO_M3
137 or_l(0x80000000, &GPIO_FUNCTION); /* remote Play button */
138 and_l(~0x80000000, &GPIO_ENABLE);
139 or_l(0x00000202, &GPIO1_FUNCTION); /* main Hold & Play */
140
141 if ((GPIO1_READ & 0x000000002) == 0)
142 on_button = true;
143
144 if ((GPIO_READ & 0x80000000) == 0)
145 rc_on_button = true;
146#elif defined(IAUDIO_M5) || defined(IAUDIO_X5)
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000147 int data;
148
Jens Arnolded3ff1b2008-03-17 23:47:38 +0000149 or_l(0x0e000000, &GPIO_FUNCTION); /* main Hold & Power, remote Play */
150 and_l(~0x0e000000, &GPIO_ENABLE);
Jens Arnold80ded2a2008-03-12 23:19:44 +0000151
Jens Arnolded3ff1b2008-03-17 23:47:38 +0000152 data = GPIO_READ;
153
154 if ((data & 0x04000000) == 0)
155 on_button = true;
156
157 if ((data & 0x02000000) == 0)
158 rc_on_button = true;
159#endif
160
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000161 power_init();
162
163 system_init();
164 kernel_init();
165
166 set_cpu_frequency(CPUFREQ_NORMAL);
167 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
168
Michael Sevakisaf395f42008-03-26 01:50:41 +0000169 enable_irq();
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000170 lcd_init();
171#ifdef HAVE_REMOTE_LCD
172 lcd_remote_init();
173#endif
174 backlight_init();
175 font_init();
176 adc_init();
177 button_init();
Jens Arnolded3ff1b2008-03-17 23:47:38 +0000178
179 if ((!on_button || button_hold())
180 && (!rc_on_button || remote_button_hold())
181 && !charger_inserted())
182 {
183 /* No need to check for USB connection here, as USB is handled
184 * in the cowon loader. */
Jens Arnoldd7009702008-03-18 00:17:21 +0000185 if (on_button || rc_on_button)
186 printf("Hold switch on");
Jens Arnolded3ff1b2008-03-17 23:47:38 +0000187 shutdown();
188 }
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000189
190 printf("Rockbox boot loader");
191 printf("Version %s", version);
192
193 check_battery();
Jens Arnold80ded2a2008-03-12 23:19:44 +0000194
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000195 rc = ata_init();
196 if(rc)
197 {
198 printf("ATA error: %d", rc);
199 sleep(HZ*5);
200 power_off();
201 }
202
203 disk_init();
204
205 rc = disk_mount_all();
206 if (rc<=0)
207 {
208 printf("No partition found");
209 sleep(HZ*5);
210 power_off();
211 }
212
213 printf("Loading firmware");
214 i = load_firmware((unsigned char *)DRAM_START, BOOTFILE, MAX_LOADSIZE);
215 printf("Result: %s", strerror(i));
216
217 if (i < EOK) {
218 printf("Error!");
Jens Arnoldf8ffca42007-03-05 00:14:14 +0000219 printf("Can't load rockbox.iaudio:");
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000220 printf(strerror(rc));
221 sleep(HZ*3);
222 power_off();
223 } else {
224 start_firmware();
225 }
226}
227
228/* These functions are present in the firmware library, but we reimplement
229 them here because the originals do a lot more than we want */
230void screen_dump(void)
231{
232}