1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
29 #include "gcc_extensions.h"
31 #include "backlight.h"
32 #include "button-target.h"
38 #include "power-imx233.h"
39 #include "system-target.h"
40 #include "fmradio_i2c.h"
42 #include "powermgmt.h"
43 #include "partitions-imx233.h"
44 #include "adc-imx233.h"
48 extern char loadaddress
[];
49 extern char loadaddressend
[];
51 #ifdef HAVE_BOOTLOADER_USB_MODE
52 static void usb_mode(int connect_timeout
)
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
;
66 button
= button_get_w_tmo(HZ
/10);
68 if(button
== SYS_USB_CONNECTED
)
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");
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 */
91 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
95 button
= button_get_w_tmo(HZ
/2);
96 if(button
== SYS_USB_DISCONNECTED
)
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%%", battery_level());
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
);
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
);
119 /* Put drivers initialized for USB connection into a known state */
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
;
134 void(*kernel_entry
)(void);
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();
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
)
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
;
188 disable_interrupt(IRQ_FIQ_STATUS
);
189 commit_discard_idcache();
191 printf("ERR: Failed to boot");