test_X plugins PLA integration
[maemo-rb.git] / bootloader / imx233.c
blob2380dfae5bafe29ebe19dedf8dd9692af5fe0da6
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"
43 #include "usb.h"
45 extern char loadaddress[];
46 extern char loadaddressend[];
48 #ifdef HAVE_BOOTLOADER_USB_MODE
49 static void usb_mode(int connect_timeout)
51 int button;
53 usb_init();
54 usb_start_monitoring();
56 /* Wait for threads to connect or cable is pulled */
57 printf("USB: Connecting");
59 long end_tick = current_tick + connect_timeout;
61 while(1)
63 button = button_get_w_tmo(HZ/10);
65 if(button == SYS_USB_CONNECTED)
66 break; /* Hit */
68 if(TIME_AFTER(current_tick, end_tick))
70 /* Timed out waiting for the connect - will happen when connected
71 * to a charger through the USB port */
72 printf("USB: Timed out");
73 break;
76 if(usb_detect() == USB_EXTRACTED)
77 break; /* Cable pulled */
80 if(button == SYS_USB_CONNECTED)
82 /* Got the message - wait for disconnect */
83 printf("Bootloader USB mode");
85 usb_acknowledge(SYS_USB_CONNECTED_ACK);
87 while(1)
89 button = button_get_w_tmo(HZ/2);
90 if(button == SYS_USB_DISCONNECTED)
91 break;
95 /* Put drivers initialized for USB connection into a known state */
96 usb_close();
98 #else /* !HAVE_BOOTLOADER_USB_MODE */
99 static void usb_mode(int connect_timeout)
101 (void) connect_timeout;
103 #endif /* HAVE_BOOTLOADER_USB_MODE */
105 void main(uint32_t arg, uint32_t addr) NORETURN_ATTR;
106 void main(uint32_t arg, uint32_t addr)
108 unsigned char* loadbuffer;
109 int buffer_size;
110 void(*kernel_entry)(void);
111 int ret;
113 system_init();
114 kernel_init();
116 power_init();
117 enable_irq();
119 lcd_init();
120 lcd_clear_display();
121 lcd_update();
123 backlight_init();
125 button_init();
127 //button_debug_screen();
128 printf("Boot version: %s", RBVERSION);
129 printf("arg=%x addr=%x", arg, addr);
130 printf("power up source: %x", __XTRACT(HW_POWER_STS, PWRUP_SOURCE));
132 #ifdef SANSA_FUZEPLUS
133 extern void imx233_mmc_disable_window(void);
134 if(arg == 0xfee1dead)
136 printf("Disable MMC window.");
137 imx233_mmc_disable_window();
139 #endif
141 ret = storage_init();
142 if(ret < 0)
143 error(EATA, ret, true);
145 /* NOTE: allow disk_init and disk_mount_all to fail since we can do USB after.
146 * We need this order to determine the correct logical sector size */
147 while(!disk_init(IF_MV(0)))
148 printf("disk_init failed!");
150 if((ret = disk_mount_all()) <= 0)
151 error(EDISK, ret, false);
153 if(usb_detect() == USB_INSERTED)
154 usb_mode(HZ);
156 printf("Loading firmware");
158 loadbuffer = (unsigned char*)loadaddress;
159 buffer_size = (int)(loadaddressend - loadaddress);
161 while((ret = load_firmware(loadbuffer, BOOTFILE, buffer_size)) < 0)
163 error(EBOOTFILE, ret, true);
166 kernel_entry = (void*) loadbuffer;
167 printf("Executing");
168 disable_interrupt(IRQ_FIQ_STATUS);
169 commit_discard_idcache();
170 kernel_entry();
171 printf("ERR: Failed to boot");
173 /* never returns */
174 while(1) ;