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" |
Michael Sevakis | 4ea4cdf | 2014-08-08 02:28:11 -0400 | [diff] [blame] | 31 | #include "../kernel-internal.h" |
Frank Gevaerts | 2f8a008 | 2008-11-01 16:14:28 +0000 | [diff] [blame] | 32 | #include "storage.h" |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 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 Arnold | 839ffbf | 2007-08-17 06:59:05 +0000 | [diff] [blame] | 42 | #include "powermgmt.h" |
Michael Sevakis | f3d60ae | 2014-08-29 23:36:11 -0400 | [diff] [blame] | 43 | #include "file_internal.h" |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 44 | #include "file.h" |
Rafaël Carré | 5d236b2 | 2010-05-27 09:41:46 +0000 | [diff] [blame] | 45 | #include "version.h" |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 46 | #include "loader_strerror.h" |
| 47 | #include "rb-loader.h" |
Rafaël Carré | 5d236b2 | 2010-05-27 09:41:46 +0000 | [diff] [blame] | 48 | |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 49 | |
| 50 | #include "pcf50606.h" |
| 51 | #include "common.h" |
| 52 | |
| 53 | #include <stdarg.h> |
| 54 | |
| 55 | /* Maximum allowed firmware image size. 10MB is more than enough */ |
| 56 | #define MAX_LOADSIZE (10*1024*1024) |
| 57 | |
| 58 | #define DRAM_START 0x31000000 |
| 59 | |
| 60 | int usb_screen(void) |
| 61 | { |
| 62 | return 0; |
| 63 | } |
| 64 | |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 65 | /* Reset the cookie for the crt0 crash check */ |
| 66 | inline void __reset_cookie(void) |
| 67 | { |
| 68 | asm(" move.l #0,%d0"); |
| 69 | asm(" move.l %d0,0x10017ffc"); |
| 70 | } |
| 71 | |
| 72 | void start_firmware(void) |
| 73 | { |
Jens Arnold | ed3ff1b | 2008-03-17 23:47:38 +0000 | [diff] [blame] | 74 | adc_close(); |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 75 | asm(" move.w #0x2700,%sr"); |
| 76 | __reset_cookie(); |
| 77 | asm(" move.l %0,%%d0" :: "i"(DRAM_START)); |
| 78 | asm(" movec.l %d0,%vbr"); |
| 79 | asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START)); |
| 80 | asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4))); |
| 81 | asm(" jmp (%a0)"); |
| 82 | } |
| 83 | |
| 84 | void shutdown(void) |
| 85 | { |
| 86 | printf("Shutting down..."); |
| 87 | |
| 88 | /* We need to gracefully spin down the disk to prevent clicks. */ |
| 89 | if (ide_powered()) |
| 90 | { |
| 91 | /* Make sure ATA has been initialized. */ |
Frank Gevaerts | 2f8a008 | 2008-11-01 16:14:28 +0000 | [diff] [blame] | 92 | storage_init(); |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 93 | |
| 94 | /* And put the disk into sleep immediately. */ |
Frank Gevaerts | 2f8a008 | 2008-11-01 16:14:28 +0000 | [diff] [blame] | 95 | storage_sleepnow(); |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | sleep(HZ*2); |
| 99 | |
| 100 | /* Backlight OFF */ |
Jens Arnold | ef12b3b | 2007-11-12 18:49:53 +0000 | [diff] [blame] | 101 | _backlight_off(); |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 102 | #ifdef HAVE_REMOTE_LCD |
Jens Arnold | ef12b3b | 2007-11-12 18:49:53 +0000 | [diff] [blame] | 103 | _remote_backlight_off(); |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 104 | #endif |
| 105 | |
| 106 | __reset_cookie(); |
| 107 | power_off(); |
| 108 | } |
| 109 | |
| 110 | /* Print the battery voltage (and a warning message). */ |
| 111 | void check_battery(void) |
| 112 | { |
Jens Arnold | 0fac492 | 2007-08-17 06:45:18 +0000 | [diff] [blame] | 113 | int battery_voltage, batt_int, batt_frac; |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 114 | |
Thomas Martitz | c1bd9b0 | 2012-01-03 23:44:38 +0000 | [diff] [blame] | 115 | battery_voltage = _battery_voltage(); |
Jens Arnold | 0fac492 | 2007-08-17 06:45:18 +0000 | [diff] [blame] | 116 | batt_int = battery_voltage / 1000; |
| 117 | batt_frac = (battery_voltage % 1000) / 10; |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 118 | |
| 119 | printf("Batt: %d.%02dV", batt_int, batt_frac); |
| 120 | |
Jens Arnold | 80ded2a | 2008-03-12 23:19:44 +0000 | [diff] [blame] | 121 | if (battery_voltage <= 3500) |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 122 | { |
| 123 | printf("WARNING! BATTERY LOW!!"); |
| 124 | sleep(HZ*2); |
| 125 | } |
| 126 | } |
| 127 | |
Jens Arnold | 91ce4b2 | 2011-06-19 17:23:18 +0000 | [diff] [blame] | 128 | #if defined(IAUDIO_M5) || defined(IAUDIO_X5) |
| 129 | int initial_gpio_read; |
| 130 | #endif |
| 131 | |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 132 | void main(void) |
| 133 | { |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 134 | int rc; |
| 135 | bool rc_on_button = false; |
| 136 | bool on_button = false; |
Jens Arnold | ed3ff1b | 2008-03-17 23:47:38 +0000 | [diff] [blame] | 137 | |
| 138 | /* We want to read the buttons as early as possible, before the user |
| 139 | releases the ON button */ |
| 140 | |
| 141 | #ifdef IAUDIO_M3 |
| 142 | or_l(0x80000000, &GPIO_FUNCTION); /* remote Play button */ |
| 143 | and_l(~0x80000000, &GPIO_ENABLE); |
| 144 | or_l(0x00000202, &GPIO1_FUNCTION); /* main Hold & Play */ |
| 145 | |
| 146 | if ((GPIO1_READ & 0x000000002) == 0) |
| 147 | on_button = true; |
| 148 | |
| 149 | if ((GPIO_READ & 0x80000000) == 0) |
| 150 | rc_on_button = true; |
| 151 | #elif defined(IAUDIO_M5) || defined(IAUDIO_X5) |
Jens Arnold | ed3ff1b | 2008-03-17 23:47:38 +0000 | [diff] [blame] | 152 | or_l(0x0e000000, &GPIO_FUNCTION); /* main Hold & Power, remote Play */ |
| 153 | and_l(~0x0e000000, &GPIO_ENABLE); |
Jens Arnold | 91ce4b2 | 2011-06-19 17:23:18 +0000 | [diff] [blame] | 154 | |
| 155 | if ((initial_gpio_read & 0x04000000) == 0) |
Jens Arnold | ed3ff1b | 2008-03-17 23:47:38 +0000 | [diff] [blame] | 156 | on_button = true; |
Jens Arnold | 91ce4b2 | 2011-06-19 17:23:18 +0000 | [diff] [blame] | 157 | |
| 158 | if ((initial_gpio_read & 0x02000000) == 0) |
Jens Arnold | ed3ff1b | 2008-03-17 23:47:38 +0000 | [diff] [blame] | 159 | rc_on_button = true; |
| 160 | #endif |
| 161 | |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 162 | power_init(); |
| 163 | |
| 164 | system_init(); |
| 165 | kernel_init(); |
| 166 | |
| 167 | set_cpu_frequency(CPUFREQ_NORMAL); |
| 168 | coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS); |
| 169 | |
Michael Sevakis | af395f4 | 2008-03-26 01:50:41 +0000 | [diff] [blame] | 170 | enable_irq(); |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 171 | lcd_init(); |
| 172 | #ifdef HAVE_REMOTE_LCD |
| 173 | lcd_remote_init(); |
| 174 | #endif |
| 175 | backlight_init(); |
| 176 | font_init(); |
| 177 | adc_init(); |
| 178 | button_init(); |
Jens Arnold | ed3ff1b | 2008-03-17 23:47:38 +0000 | [diff] [blame] | 179 | |
| 180 | if ((!on_button || button_hold()) |
| 181 | && (!rc_on_button || remote_button_hold()) |
| 182 | && !charger_inserted()) |
| 183 | { |
| 184 | /* No need to check for USB connection here, as USB is handled |
| 185 | * in the cowon loader. */ |
Jens Arnold | d700970 | 2008-03-18 00:17:21 +0000 | [diff] [blame] | 186 | if (on_button || rc_on_button) |
| 187 | printf("Hold switch on"); |
Jens Arnold | ed3ff1b | 2008-03-17 23:47:38 +0000 | [diff] [blame] | 188 | shutdown(); |
| 189 | } |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 190 | |
| 191 | printf("Rockbox boot loader"); |
Michael Sevakis | 95a4c3a | 2014-08-28 10:26:45 -0400 | [diff] [blame] | 192 | printf("Version %s", rbversion); |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 193 | |
| 194 | check_battery(); |
Jens Arnold | 80ded2a | 2008-03-12 23:19:44 +0000 | [diff] [blame] | 195 | |
Frank Gevaerts | 2f8a008 | 2008-11-01 16:14:28 +0000 | [diff] [blame] | 196 | rc = storage_init(); |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 197 | if(rc) |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 198 | error(EATA, rc, true); |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 199 | |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 200 | filesystem_init(); |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 201 | |
| 202 | rc = disk_mount_all(); |
| 203 | if (rc<=0) |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 204 | error(EDISK, rc, true); |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 205 | |
| 206 | printf("Loading firmware"); |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 207 | |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 208 | rc = load_firmware((unsigned char *)DRAM_START, BOOTFILE, MAX_LOADSIZE); |
| 209 | |
| 210 | if (rc <= EFILE_EMPTY) |
| 211 | error(EBOOTFILE, rc, true); |
| 212 | |
| 213 | start_firmware(); |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* These functions are present in the firmware library, but we reimplement |
| 217 | them here because the originals do a lot more than we want */ |
| 218 | void screen_dump(void) |
| 219 | { |
| 220 | } |