Bump version numbers for 3.13
[maemo-rb.git] / bootloader / imx233.c
blob9f6525e735e7edc05eddbba7ccdf488a594f39ba
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2011 by amaury Pouly
12 * Based on Rockbox iriver bootloader by Linus Nielsen Feltzing
13 * and the ipodlinux bootloader by Daniel Palffy and Bernard Leach
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.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
25 #include <stdio.h>
26 #include <system.h>
27 #include <inttypes.h>
28 #include "config.h"
29 #include "gcc_extensions.h"
30 #include "lcd.h"
31 #include "backlight.h"
32 #include "button-target.h"
33 #include "common.h"
34 #include "storage.h"
35 #include "disk.h"
36 #include "panic.h"
37 #include "power.h"
38 #include "power-imx233.h"
39 #include "system-target.h"
40 #include "fmradio_i2c.h"
41 #include "version.h"
42 #include "powermgmt.h"
43 #include "partitions-imx233.h"
44 #include "adc-imx233.h"
46 #include "usb.h"
48 extern char loadaddress[];
49 extern char loadaddressend[];
51 #ifdef HAVE_BOOTLOADER_USB_MODE
52 static void usb_mode(int connect_timeout)
54 int button;
56 usb_init();
57 usb_start_monitoring();
59 /* Wait for threads to connect or cable is pulled */
60 printf("USB: Connecting");
62 long end_tick = current_tick + connect_timeout;
64 while(1)
66 button = button_get_w_tmo(HZ/10);
68 if(button == SYS_USB_CONNECTED)
69 break; /* Hit */
71 if(TIME_AFTER(current_tick, end_tick))
73 /* Timed out waiting for the connect - will happen when connected
74 * to a charger through the USB port */
75 printf("USB: Timed out");
76 break;
79 if(usb_detect() == USB_EXTRACTED)
80 break; /* Cable pulled */
83 if(button == SYS_USB_CONNECTED)
85 /* Got the message - wait for disconnect */
86 printf("Bootloader USB mode");
87 /* Enable power management to charge */
88 powermgmt_init();
89 adc_init();
91 usb_acknowledge(SYS_USB_CONNECTED_ACK);
93 while(1)
95 button = button_get_w_tmo(HZ/2);
96 if(button == SYS_USB_DISCONNECTED)
97 break;
98 struct imx233_powermgmt_info_t info = imx233_powermgmt_get_info();
99 lcd_putsf(0, 7, "Charging status: %s",
100 info.state == CHARGE_STATE_DISABLED ? "disabled" :
101 info.state == CHARGE_STATE_ERROR ? "error" :
102 info.state == DISCHARGING ? "discharging" :
103 info.state == TRICKLE ? "trickle" :
104 info.state == TOPOFF ? "topoff" :
105 info.state == CHARGING ? "charging" : "<unknown>");
106 lcd_putsf(0, 8, "Battery: %d%% (%d mV)", battery_level(), battery_voltage());
107 lcd_putsf(0, 9, "Die temp: %d°C [%d, %d]",
108 adc_read(ADC_DIE_TEMP), IMX233_DIE_TEMP_HIGH,
109 IMX233_DIE_TEMP_LOW);
110 #ifdef ADC_BATT_TEMP
111 lcd_putsf(0, 10, "Batt temp: %d [%d, %d]",
112 adc_read(ADC_BATT_TEMP), IMX233_BATT_TEMP_HIGH,
113 IMX233_BATT_TEMP_LOW);
114 #endif
115 lcd_update();
119 /* Put drivers initialized for USB connection into a known state */
120 usb_close();
122 #else /* !HAVE_BOOTLOADER_USB_MODE */
123 static void usb_mode(int connect_timeout)
125 (void) connect_timeout;
127 #endif /* HAVE_BOOTLOADER_USB_MODE */
129 void main(uint32_t arg, uint32_t addr) NORETURN_ATTR;
130 void main(uint32_t arg, uint32_t addr)
132 unsigned char* loadbuffer;
133 int buffer_size;
134 void(*kernel_entry)(void);
135 int ret;
137 system_init();
138 kernel_init();
140 power_init();
141 enable_irq();
143 lcd_init();
144 lcd_clear_display();
145 lcd_update();
147 backlight_init();
149 button_init();
151 printf("Boot version: %s", RBVERSION);
152 printf("arg=%x addr=%x", arg, addr);
153 printf("power up source: %x", __XTRACT(HW_POWER_STS, PWRUP_SOURCE));
155 if(arg == 0xfee1dead)
157 printf("Disable partitions window.");
158 imx233_partitions_enable_window(false);
161 ret = storage_init();
162 if(ret < 0)
163 error(EATA, ret, true);
165 /* NOTE: allow disk_init and disk_mount_all to fail since we can do USB after.
166 * We need this order to determine the correct logical sector size */
167 while(!disk_init(IF_MV(0)))
168 printf("disk_init failed!");
170 if((ret = disk_mount_all()) <= 0)
171 error(EDISK, ret, false);
173 if(usb_detect() == USB_INSERTED)
174 usb_mode(HZ);
176 printf("Loading firmware");
178 loadbuffer = (unsigned char*)loadaddress;
179 buffer_size = (int)(loadaddressend - loadaddress);
181 while((ret = load_firmware(loadbuffer, BOOTFILE, buffer_size)) < 0)
183 error(EBOOTFILE, ret, true);
186 kernel_entry = (void*) loadbuffer;
187 printf("Executing");
188 disable_interrupt(IRQ_FIQ_STATUS);
189 commit_discard_idcache();
190 kernel_entry();
191 printf("ERR: Failed to boot");
193 /* never returns */
194 while(1) ;