Amaury Pouly | 08fb3f6 | 2011-05-01 13:02:46 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
Amaury Pouly | 3f20d13 | 2013-09-05 23:12:04 +0200 | [diff] [blame] | 10 | * Copyright (C) 2011 by Amaury Pouly |
Amaury Pouly | 08fb3f6 | 2011-05-01 13:02:46 +0000 | [diff] [blame] | 11 | * |
| 12 | * Based on Rockbox iriver bootloader by Linus Nielsen Feltzing |
| 13 | * and the ipodlinux bootloader by Daniel Palffy and Bernard Leach |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU General Public License |
| 17 | * as published by the Free Software Foundation; either version 2 |
| 18 | * of the License, or (at your option) any later version. |
| 19 | * |
| 20 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 21 | * KIND, either express or implied. |
| 22 | * |
| 23 | ****************************************************************************/ |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <system.h> |
| 27 | #include <inttypes.h> |
| 28 | #include "config.h" |
Michael Sevakis | 4ea4cdf | 2014-08-08 02:28:11 -0400 | [diff] [blame] | 29 | #include "../kernel-internal.h" |
Amaury Pouly | 08fb3f6 | 2011-05-01 13:02:46 +0000 | [diff] [blame] | 30 | #include "gcc_extensions.h" |
| 31 | #include "lcd.h" |
| 32 | #include "backlight.h" |
| 33 | #include "button-target.h" |
| 34 | #include "common.h" |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 35 | #include "rb-loader.h" |
| 36 | #include "loader_strerror.h" |
Amaury Pouly | 08fb3f6 | 2011-05-01 13:02:46 +0000 | [diff] [blame] | 37 | #include "storage.h" |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 38 | #include "file_internal.h" |
Amaury Pouly | 08fb3f6 | 2011-05-01 13:02:46 +0000 | [diff] [blame] | 39 | #include "disk.h" |
| 40 | #include "panic.h" |
| 41 | #include "power.h" |
Amaury Pouly | c4450b9 | 2012-02-28 22:44:57 +0100 | [diff] [blame] | 42 | #include "power-imx233.h" |
Amaury Pouly | 86e8c28 | 2011-05-11 22:38:09 +0000 | [diff] [blame] | 43 | #include "system-target.h" |
Amaury Pouly | 5a26811 | 2011-07-09 17:14:05 +0000 | [diff] [blame] | 44 | #include "fmradio_i2c.h" |
Amaury Pouly | 0b885f2 | 2011-12-26 19:13:29 +0000 | [diff] [blame] | 45 | #include "version.h" |
Amaury Pouly | da7be39 | 2013-11-19 22:12:42 +0000 | [diff] [blame] | 46 | #include "powermgmt-imx233.h" |
Amaury Pouly | 07138ba | 2012-05-19 13:33:45 +0200 | [diff] [blame] | 47 | #include "partitions-imx233.h" |
Amaury Pouly | e1003b1 | 2014-02-10 22:29:31 +0100 | [diff] [blame] | 48 | #include "backlight-target.h" |
Amaury Pouly | 0cb76de | 2013-11-19 21:28:35 +0000 | [diff] [blame] | 49 | #include "adc.h" |
Amaury Pouly | 2cf3313 | 2011-06-17 22:30:58 +0000 | [diff] [blame] | 50 | |
| 51 | #include "usb.h" |
Amaury Pouly | 7d4fed5 | 2011-09-05 11:29:32 +0000 | [diff] [blame] | 52 | |
Amaury Pouly | d945c36 | 2011-09-13 23:38:08 +0000 | [diff] [blame] | 53 | extern char loadaddress[]; |
| 54 | extern char loadaddressend[]; |
Amaury Pouly | 7d4fed5 | 2011-09-05 11:29:32 +0000 | [diff] [blame] | 55 | |
Amaury Pouly | 7e03dbb | 2013-09-25 14:11:56 +0200 | [diff] [blame] | 56 | #define MSG(width, short, long) (LCD_WIDTH < (width) ? short : long) |
| 57 | |
Amaury Pouly | 7d4fed5 | 2011-09-05 11:29:32 +0000 | [diff] [blame] | 58 | #ifdef HAVE_BOOTLOADER_USB_MODE |
| 59 | static void usb_mode(int connect_timeout) |
| 60 | { |
| 61 | int button; |
Amaury Pouly | 2f6c060 | 2013-06-18 21:26:23 +0200 | [diff] [blame] | 62 | |
Amaury Pouly | 7d4fed5 | 2011-09-05 11:29:32 +0000 | [diff] [blame] | 63 | usb_init(); |
| 64 | usb_start_monitoring(); |
| 65 | |
| 66 | /* Wait for threads to connect or cable is pulled */ |
| 67 | printf("USB: Connecting"); |
| 68 | |
| 69 | long end_tick = current_tick + connect_timeout; |
| 70 | |
| 71 | while(1) |
| 72 | { |
| 73 | button = button_get_w_tmo(HZ/10); |
| 74 | |
| 75 | if(button == SYS_USB_CONNECTED) |
| 76 | break; /* Hit */ |
| 77 | |
| 78 | if(TIME_AFTER(current_tick, end_tick)) |
| 79 | { |
| 80 | /* Timed out waiting for the connect - will happen when connected |
| 81 | * to a charger through the USB port */ |
| 82 | printf("USB: Timed out"); |
| 83 | break; |
| 84 | } |
| 85 | |
Rafaël Carré | 2115eac | 2011-12-31 18:43:59 +0000 | [diff] [blame] | 86 | if(usb_detect() == USB_EXTRACTED) |
Amaury Pouly | 7d4fed5 | 2011-09-05 11:29:32 +0000 | [diff] [blame] | 87 | break; /* Cable pulled */ |
| 88 | } |
| 89 | |
| 90 | if(button == SYS_USB_CONNECTED) |
| 91 | { |
| 92 | /* Got the message - wait for disconnect */ |
| 93 | printf("Bootloader USB mode"); |
Amaury Pouly | a4fd5bf | 2012-03-15 14:58:15 +0100 | [diff] [blame] | 94 | /* Enable power management to charge */ |
| 95 | powermgmt_init(); |
Amaury Pouly | 07138ba | 2012-05-19 13:33:45 +0200 | [diff] [blame] | 96 | adc_init(); |
Amaury Pouly | 7d4fed5 | 2011-09-05 11:29:32 +0000 | [diff] [blame] | 97 | |
Amaury Pouly | 246026c | 2015-01-08 22:18:22 +0100 | [diff] [blame] | 98 | /* ack the SYS_USB_CONNECTED polled from the button queue */ |
Amaury Pouly | 7d4fed5 | 2011-09-05 11:29:32 +0000 | [diff] [blame] | 99 | usb_acknowledge(SYS_USB_CONNECTED_ACK); |
| 100 | |
| 101 | while(1) |
| 102 | { |
| 103 | button = button_get_w_tmo(HZ/2); |
| 104 | if(button == SYS_USB_DISCONNECTED) |
| 105 | break; |
Amaury Pouly | a4fd5bf | 2012-03-15 14:58:15 +0100 | [diff] [blame] | 106 | struct imx233_powermgmt_info_t info = imx233_powermgmt_get_info(); |
Amaury Pouly | 7e03dbb | 2013-09-25 14:11:56 +0200 | [diff] [blame] | 107 | lcd_putsf(0, 7, "%s: %s", MSG(240, "Status", "Charging status"), |
Amaury Pouly | a4fd5bf | 2012-03-15 14:58:15 +0100 | [diff] [blame] | 108 | info.state == CHARGE_STATE_DISABLED ? "disabled" : |
| 109 | info.state == CHARGE_STATE_ERROR ? "error" : |
| 110 | info.state == DISCHARGING ? "discharging" : |
| 111 | info.state == TRICKLE ? "trickle" : |
| 112 | info.state == TOPOFF ? "topoff" : |
| 113 | info.state == CHARGING ? "charging" : "<unknown>"); |
Amaury Pouly | 2724888 | 2012-08-29 16:23:20 +0200 | [diff] [blame] | 114 | lcd_putsf(0, 8, "Battery: %d%% (%d mV)", battery_level(), battery_voltage()); |
Amaury Pouly | 7e03dbb | 2013-09-25 14:11:56 +0200 | [diff] [blame] | 115 | lcd_putsf(0, 9, "%s: %d 'C [%d, %d]", MSG(240, "Die", "Die temp"), |
Amaury Pouly | 07138ba | 2012-05-19 13:33:45 +0200 | [diff] [blame] | 116 | adc_read(ADC_DIE_TEMP), IMX233_DIE_TEMP_HIGH, |
| 117 | IMX233_DIE_TEMP_LOW); |
Amaury Pouly | a4fd5bf | 2012-03-15 14:58:15 +0100 | [diff] [blame] | 118 | lcd_update(); |
Amaury Pouly | 7d4fed5 | 2011-09-05 11:29:32 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | /* Put drivers initialized for USB connection into a known state */ |
| 123 | usb_close(); |
Amaury Pouly | 7d4fed5 | 2011-09-05 11:29:32 +0000 | [diff] [blame] | 124 | } |
| 125 | #else /* !HAVE_BOOTLOADER_USB_MODE */ |
| 126 | static void usb_mode(int connect_timeout) |
| 127 | { |
| 128 | (void) connect_timeout; |
| 129 | } |
| 130 | #endif /* HAVE_BOOTLOADER_USB_MODE */ |
Amaury Pouly | 08fb3f6 | 2011-05-01 13:02:46 +0000 | [diff] [blame] | 131 | |
Amaury Pouly | 1322b58 | 2011-09-23 20:40:52 +0000 | [diff] [blame] | 132 | void main(uint32_t arg, uint32_t addr) NORETURN_ATTR; |
| 133 | void main(uint32_t arg, uint32_t addr) |
Amaury Pouly | 08fb3f6 | 2011-05-01 13:02:46 +0000 | [diff] [blame] | 134 | { |
| 135 | unsigned char* loadbuffer; |
| 136 | int buffer_size; |
| 137 | void(*kernel_entry)(void); |
| 138 | int ret; |
| 139 | |
| 140 | system_init(); |
| 141 | kernel_init(); |
| 142 | |
Amaury Pouly | 2f5f2ba | 2013-09-25 14:12:48 +0200 | [diff] [blame] | 143 | /* some ixm233 targets needs this because the cpu and/or memory is clocked |
| 144 | * at 24MHz, resulting in terribly slow boots and unusable usb mode. |
| 145 | * While we are at it, clock at maximum speed to minimise boot time. */ |
| 146 | imx233_set_cpu_frequency(CPUFREQ_MAX); |
| 147 | |
Amaury Pouly | 7d4fed5 | 2011-09-05 11:29:32 +0000 | [diff] [blame] | 148 | power_init(); |
Amaury Pouly | 08fb3f6 | 2011-05-01 13:02:46 +0000 | [diff] [blame] | 149 | enable_irq(); |
| 150 | |
| 151 | lcd_init(); |
Amaury Pouly | 82ecc75 | 2011-07-02 02:12:10 +0000 | [diff] [blame] | 152 | lcd_clear_display(); |
| 153 | lcd_update(); |
Amaury Pouly | 08fb3f6 | 2011-05-01 13:02:46 +0000 | [diff] [blame] | 154 | |
| 155 | backlight_init(); |
| 156 | |
Amaury Pouly | 7d4fed5 | 2011-09-05 11:29:32 +0000 | [diff] [blame] | 157 | button_init(); |
Amaury Pouly | 08fb3f6 | 2011-05-01 13:02:46 +0000 | [diff] [blame] | 158 | |
Michael Sevakis | 95a4c3a | 2014-08-28 10:26:45 -0400 | [diff] [blame] | 159 | printf("%s: %s", MSG(240, "Ver", "Boot version"), rbversion); |
Amaury Pouly | 7e03dbb | 2013-09-25 14:11:56 +0200 | [diff] [blame] | 160 | printf("%s: %x ", MSG(240, "Arg", "Boot arg"), arg); |
| 161 | printf("%s: %x", MSG(240, "Addr", "Boot addr"), addr); |
Amaury Pouly | 2f6c060 | 2013-06-18 21:26:23 +0200 | [diff] [blame] | 162 | #if IMX233_SUBTARGET >= 3780 |
Amaury Pouly | 7e03dbb | 2013-09-25 14:11:56 +0200 | [diff] [blame] | 163 | printf("Power up source: %x", BF_RD(POWER_STS, PWRUP_SOURCE)); |
Amaury Pouly | 2f6c060 | 2013-06-18 21:26:23 +0200 | [diff] [blame] | 164 | #endif |
Amaury Pouly | 7d4fed5 | 2011-09-05 11:29:32 +0000 | [diff] [blame] | 165 | |
Amaury Pouly | 7d4fed5 | 2011-09-05 11:29:32 +0000 | [diff] [blame] | 166 | if(arg == 0xfee1dead) |
| 167 | { |
Amaury Pouly | 7e03dbb | 2013-09-25 14:11:56 +0200 | [diff] [blame] | 168 | printf("%s", MSG(240, "Disable window", "Disable partitions window")); |
Amaury Pouly | 07138ba | 2012-05-19 13:33:45 +0200 | [diff] [blame] | 169 | imx233_partitions_enable_window(false); |
Amaury Pouly | 7d4fed5 | 2011-09-05 11:29:32 +0000 | [diff] [blame] | 170 | } |
Amaury Pouly | 86e8c28 | 2011-05-11 22:38:09 +0000 | [diff] [blame] | 171 | |
Amaury Pouly | 08fb3f6 | 2011-05-01 13:02:46 +0000 | [diff] [blame] | 172 | ret = storage_init(); |
| 173 | if(ret < 0) |
| 174 | error(EATA, ret, true); |
| 175 | |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 176 | filesystem_init(); |
Amaury Pouly | 08fb3f6 | 2011-05-01 13:02:46 +0000 | [diff] [blame] | 177 | |
Amaury Pouly | efc591e | 2013-10-20 18:11:24 +0200 | [diff] [blame] | 178 | /* NOTE: disk_mount_all to fail since we can do USB after. |
| 179 | * We need this order to determine the correct logical sector size */ |
Amaury Pouly | 7d4fed5 | 2011-09-05 11:29:32 +0000 | [diff] [blame] | 180 | if((ret = disk_mount_all()) <= 0) |
| 181 | error(EDISK, ret, false); |
Amaury Pouly | 08fb3f6 | 2011-05-01 13:02:46 +0000 | [diff] [blame] | 182 | |
Rafaël Carré | 2115eac | 2011-12-31 18:43:59 +0000 | [diff] [blame] | 183 | if(usb_detect() == USB_INSERTED) |
Amaury Pouly | 25f0060 | 2011-09-05 13:58:29 +0000 | [diff] [blame] | 184 | usb_mode(HZ); |
Amaury Pouly | 82f70b8 | 2011-07-22 15:45:58 +0000 | [diff] [blame] | 185 | |
Amaury Pouly | f753105 | 2013-09-26 18:30:36 +0200 | [diff] [blame] | 186 | /* dummy read, might be necessary to init things */ |
| 187 | #ifdef HAVE_BUTTON_DATA |
| 188 | int data; |
| 189 | button_read_device(&data); |
| 190 | #else |
| 191 | button_read_device(); |
| 192 | #endif |
| 193 | |
| 194 | #ifdef HAS_BUTTON_HOLD |
| 195 | if(button_hold()) |
| 196 | { |
| 197 | printf("Hold switch on"); |
| 198 | printf("Shutting down..."); |
| 199 | sleep(HZ); |
| 200 | power_off(); |
| 201 | } |
| 202 | #endif |
| 203 | |
Amaury Pouly | 08fb3f6 | 2011-05-01 13:02:46 +0000 | [diff] [blame] | 204 | printf("Loading firmware"); |
| 205 | |
Amaury Pouly | d945c36 | 2011-09-13 23:38:08 +0000 | [diff] [blame] | 206 | loadbuffer = (unsigned char*)loadaddress; |
| 207 | buffer_size = (int)(loadaddressend - loadaddress); |
Amaury Pouly | 08fb3f6 | 2011-05-01 13:02:46 +0000 | [diff] [blame] | 208 | |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 209 | while((ret = load_firmware(loadbuffer, BOOTFILE, buffer_size)) <= EFILE_EMPTY) |
Amaury Pouly | 08fb3f6 | 2011-05-01 13:02:46 +0000 | [diff] [blame] | 210 | { |
| 211 | error(EBOOTFILE, ret, true); |
| 212 | } |
| 213 | |
| 214 | kernel_entry = (void*) loadbuffer; |
Amaury Pouly | 08fb3f6 | 2011-05-01 13:02:46 +0000 | [diff] [blame] | 215 | printf("Executing"); |
Amaury Pouly | 935c0ad | 2014-02-10 22:28:46 +0100 | [diff] [blame] | 216 | /* stop what was initialized to start from clean state */ |
| 217 | system_prepare_fw_start(); |
Amaury Pouly | e1003b1 | 2014-02-10 22:29:31 +0100 | [diff] [blame] | 218 | /* if target defines lcd_enable() in bootloader, take this as a hint that |
| 219 | * we should use it to properly stop the lcd before moving one, the |
Marcin Bukat | 89ba7e8 | 2015-01-09 00:22:40 +0100 | [diff] [blame] | 220 | * backlight_hw_off() routine is supposed to disable the lcd at the same time */ |
Amaury Pouly | e1003b1 | 2014-02-10 22:29:31 +0100 | [diff] [blame] | 221 | #ifdef HAVE_LCD_ENABLE |
Marcin Bukat | 89ba7e8 | 2015-01-09 00:22:40 +0100 | [diff] [blame] | 222 | backlight_hw_off(); |
Amaury Pouly | e1003b1 | 2014-02-10 22:29:31 +0100 | [diff] [blame] | 223 | #endif |
Amaury Pouly | d945c36 | 2011-09-13 23:38:08 +0000 | [diff] [blame] | 224 | disable_interrupt(IRQ_FIQ_STATUS); |
| 225 | commit_discard_idcache(); |
Amaury Pouly | 08fb3f6 | 2011-05-01 13:02:46 +0000 | [diff] [blame] | 226 | kernel_entry(); |
| 227 | printf("ERR: Failed to boot"); |
| 228 | |
| 229 | /* never returns */ |
| 230 | while(1) ; |
| 231 | } |