blob: c1076e98ccbbe11bcfac66979c9829028e886f85 [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 *
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"
29#include "kernel.h"
30#include "thread.h"
31#include "ata.h"
32#include "usb.h"
33#include "disk.h"
34#include "font.h"
35#include "adc.h"
36#include "backlight.h"
37#include "backlight-target.h"
38#include "button.h"
39#include "panic.h"
40#include "power.h"
Jens Arnold839ffbf2007-08-17 06:59:05 +000041#include "powermgmt.h"
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +000042#include "file.h"
43
44#include "pcf50606.h"
45#include "common.h"
46
47#include <stdarg.h>
48
49/* Maximum allowed firmware image size. 10MB is more than enough */
50#define MAX_LOADSIZE (10*1024*1024)
51
52#define DRAM_START 0x31000000
53
54int usb_screen(void)
55{
56 return 0;
57}
58
59char version[] = APPSVERSION;
60
61/* 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_firmware(void)
69{
Jens Arnolded3ff1b2008-03-17 23:47:38 +000070 adc_close();
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +000071 asm(" move.w #0x2700,%sr");
72 __reset_cookie();
73 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
74 asm(" movec.l %d0,%vbr");
75 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
76 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
77 asm(" jmp (%a0)");
78}
79
80void shutdown(void)
81{
82 printf("Shutting down...");
83
84 /* We need to gracefully spin down the disk to prevent clicks. */
85 if (ide_powered())
86 {
87 /* Make sure ATA has been initialized. */
88 ata_init();
89
90 /* And put the disk into sleep immediately. */
91 ata_sleepnow();
92 }
93
94 sleep(HZ*2);
95
96 /* Backlight OFF */
Jens Arnoldef12b3b2007-11-12 18:49:53 +000097 _backlight_off();
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +000098#ifdef HAVE_REMOTE_LCD
Jens Arnoldef12b3b2007-11-12 18:49:53 +000099 _remote_backlight_off();
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000100#endif
101
102 __reset_cookie();
103 power_off();
104}
105
106/* Print the battery voltage (and a warning message). */
107void check_battery(void)
108{
Jens Arnold0fac4922007-08-17 06:45:18 +0000109 int battery_voltage, batt_int, batt_frac;
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000110
Jens Arnold0fac4922007-08-17 06:45:18 +0000111 battery_voltage = battery_adc_voltage();
112 batt_int = battery_voltage / 1000;
113 batt_frac = (battery_voltage % 1000) / 10;
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000114
115 printf("Batt: %d.%02dV", batt_int, batt_frac);
116
Jens Arnold80ded2a2008-03-12 23:19:44 +0000117 if (battery_voltage <= 3500)
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000118 {
119 printf("WARNING! BATTERY LOW!!");
120 sleep(HZ*2);
121 }
122}
123
124void main(void)
125{
126 int i;
127 int rc;
128 bool rc_on_button = false;
129 bool on_button = false;
Jens Arnolded3ff1b2008-03-17 23:47:38 +0000130
131 /* We want to read the buttons as early as possible, before the user
132 releases the ON button */
133
134#ifdef IAUDIO_M3
135 or_l(0x80000000, &GPIO_FUNCTION); /* remote Play button */
136 and_l(~0x80000000, &GPIO_ENABLE);
137 or_l(0x00000202, &GPIO1_FUNCTION); /* main Hold & Play */
138
139 if ((GPIO1_READ & 0x000000002) == 0)
140 on_button = true;
141
142 if ((GPIO_READ & 0x80000000) == 0)
143 rc_on_button = true;
144#elif defined(IAUDIO_M5) || defined(IAUDIO_X5)
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000145 int data;
146
Jens Arnolded3ff1b2008-03-17 23:47:38 +0000147 or_l(0x0e000000, &GPIO_FUNCTION); /* main Hold & Power, remote Play */
148 and_l(~0x0e000000, &GPIO_ENABLE);
Jens Arnold80ded2a2008-03-12 23:19:44 +0000149
Jens Arnolded3ff1b2008-03-17 23:47:38 +0000150 data = GPIO_READ;
151
152 if ((data & 0x04000000) == 0)
153 on_button = true;
154
155 if ((data & 0x02000000) == 0)
156 rc_on_button = true;
157#endif
158
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000159 power_init();
160
161 system_init();
162 kernel_init();
163
164 set_cpu_frequency(CPUFREQ_NORMAL);
165 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
166
Michael Sevakisaf395f42008-03-26 01:50:41 +0000167 enable_irq();
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000168 lcd_init();
169#ifdef HAVE_REMOTE_LCD
170 lcd_remote_init();
171#endif
172 backlight_init();
173 font_init();
174 adc_init();
175 button_init();
Jens Arnolded3ff1b2008-03-17 23:47:38 +0000176
177 if ((!on_button || button_hold())
178 && (!rc_on_button || remote_button_hold())
179 && !charger_inserted())
180 {
181 /* No need to check for USB connection here, as USB is handled
182 * in the cowon loader. */
Jens Arnoldd7009702008-03-18 00:17:21 +0000183 if (on_button || rc_on_button)
184 printf("Hold switch on");
Jens Arnolded3ff1b2008-03-17 23:47:38 +0000185 shutdown();
186 }
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000187
188 printf("Rockbox boot loader");
189 printf("Version %s", version);
190
191 check_battery();
Jens Arnold80ded2a2008-03-12 23:19:44 +0000192
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000193 rc = ata_init();
194 if(rc)
195 {
196 printf("ATA error: %d", rc);
197 sleep(HZ*5);
198 power_off();
199 }
200
201 disk_init();
202
203 rc = disk_mount_all();
204 if (rc<=0)
205 {
206 printf("No partition found");
207 sleep(HZ*5);
208 power_off();
209 }
210
211 printf("Loading firmware");
212 i = load_firmware((unsigned char *)DRAM_START, BOOTFILE, MAX_LOADSIZE);
213 printf("Result: %s", strerror(i));
214
215 if (i < EOK) {
216 printf("Error!");
Jens Arnoldf8ffca42007-03-05 00:14:14 +0000217 printf("Can't load rockbox.iaudio:");
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000218 printf(strerror(rc));
219 sleep(HZ*3);
220 power_off();
221 } else {
222 start_firmware();
223 }
224}
225
226/* These functions are present in the firmware library, but we reimplement
227 them here because the originals do a lot more than we want */
228void screen_dump(void)
229{
230}