Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
Linus Nielsen Feltzing | 4fb389f | 2007-02-22 21:44:12 +0000 | [diff] [blame] | 8 | * $Id$ |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 9 | * |
| 10 | * Copyright (C) 2007 by Linus Nielsen Feltzing |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 12 | * 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 Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 16 | * |
| 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 Arnold | 839ffbf | 2007-08-17 06:59:05 +0000 | [diff] [blame] | 43 | #include "powermgmt.h" |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 44 | #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 | |
| 56 | int usb_screen(void) |
| 57 | { |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | char version[] = APPSVERSION; |
| 62 | |
| 63 | /* Reset the cookie for the crt0 crash check */ |
| 64 | inline void __reset_cookie(void) |
| 65 | { |
| 66 | asm(" move.l #0,%d0"); |
| 67 | asm(" move.l %d0,0x10017ffc"); |
| 68 | } |
| 69 | |
| 70 | void start_firmware(void) |
| 71 | { |
Jens Arnold | ed3ff1b | 2008-03-17 23:47:38 +0000 | [diff] [blame] | 72 | adc_close(); |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 73 | 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 | |
| 82 | void 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 Arnold | ef12b3b | 2007-11-12 18:49:53 +0000 | [diff] [blame] | 99 | _backlight_off(); |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 100 | #ifdef HAVE_REMOTE_LCD |
Jens Arnold | ef12b3b | 2007-11-12 18:49:53 +0000 | [diff] [blame] | 101 | _remote_backlight_off(); |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 102 | #endif |
| 103 | |
| 104 | __reset_cookie(); |
| 105 | power_off(); |
| 106 | } |
| 107 | |
| 108 | /* Print the battery voltage (and a warning message). */ |
| 109 | void check_battery(void) |
| 110 | { |
Jens Arnold | 0fac492 | 2007-08-17 06:45:18 +0000 | [diff] [blame] | 111 | int battery_voltage, batt_int, batt_frac; |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 112 | |
Jens Arnold | 0fac492 | 2007-08-17 06:45:18 +0000 | [diff] [blame] | 113 | battery_voltage = battery_adc_voltage(); |
| 114 | batt_int = battery_voltage / 1000; |
| 115 | batt_frac = (battery_voltage % 1000) / 10; |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 116 | |
| 117 | printf("Batt: %d.%02dV", batt_int, batt_frac); |
| 118 | |
Jens Arnold | 80ded2a | 2008-03-12 23:19:44 +0000 | [diff] [blame] | 119 | if (battery_voltage <= 3500) |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 120 | { |
| 121 | printf("WARNING! BATTERY LOW!!"); |
| 122 | sleep(HZ*2); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void main(void) |
| 127 | { |
| 128 | int i; |
| 129 | int rc; |
| 130 | bool rc_on_button = false; |
| 131 | bool on_button = false; |
Jens Arnold | ed3ff1b | 2008-03-17 23:47:38 +0000 | [diff] [blame] | 132 | |
| 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 Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 147 | int data; |
| 148 | |
Jens Arnold | ed3ff1b | 2008-03-17 23:47:38 +0000 | [diff] [blame] | 149 | or_l(0x0e000000, &GPIO_FUNCTION); /* main Hold & Power, remote Play */ |
| 150 | and_l(~0x0e000000, &GPIO_ENABLE); |
Jens Arnold | 80ded2a | 2008-03-12 23:19:44 +0000 | [diff] [blame] | 151 | |
Jens Arnold | ed3ff1b | 2008-03-17 23:47:38 +0000 | [diff] [blame] | 152 | 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 Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 161 | 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 Sevakis | af395f4 | 2008-03-26 01:50:41 +0000 | [diff] [blame] | 169 | enable_irq(); |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 170 | 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 Arnold | ed3ff1b | 2008-03-17 23:47:38 +0000 | [diff] [blame] | 178 | |
| 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 Arnold | d700970 | 2008-03-18 00:17:21 +0000 | [diff] [blame] | 185 | if (on_button || rc_on_button) |
| 186 | printf("Hold switch on"); |
Jens Arnold | ed3ff1b | 2008-03-17 23:47:38 +0000 | [diff] [blame] | 187 | shutdown(); |
| 188 | } |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 189 | |
| 190 | printf("Rockbox boot loader"); |
| 191 | printf("Version %s", version); |
| 192 | |
| 193 | check_battery(); |
Jens Arnold | 80ded2a | 2008-03-12 23:19:44 +0000 | [diff] [blame] | 194 | |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 195 | 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 Arnold | f8ffca4 | 2007-03-05 00:14:14 +0000 | [diff] [blame] | 219 | printf("Can't load rockbox.iaudio:"); |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 220 | 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 */ |
| 230 | void screen_dump(void) |
| 231 | { |
| 232 | } |