Reinsert CACHEALIGN_SIZE to imx31l.h, r28619 expected another local change.
[kugel-rb.git] / bootloader / mpio_hd200.c
blob98c83239f72d7d6896616d16590d0f39e1b9d1f8
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Marcin Bukat
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
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 "kernel.h"
31 #include "thread.h"
32 #include "storage.h"
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"
42 #include "powermgmt.h"
43 #include "file.h"
45 #include "common.h"
46 #include "version.h"
48 #include <stdarg.h>
50 /* Maximum allowed firmware image size. 10MB is more than enough */
51 #define MAX_LOADSIZE (10*1024*1024)
53 #define DRAM_START 0x31000000
55 #define BOOTMENU_TIMEOUT (10*HZ)
56 #define BOOTMENU_OPTIONS 3
58 #define EVENT_NONE 0x00
59 #define EVENT_ON 0x01
60 #define EVENT_AC 0x02
61 #define EVENT_USB 0x04
63 /* From common.c */
64 extern int line;
65 static const char *bootmenu_options[] = {
66 "Boot rockbox",
67 "Boot MPIO firmware",
68 "Shutdown"
71 enum option_t {
72 rockbox,
73 mpio_firmware,
74 shutdown
77 int usb_screen(void)
79 return 0;
82 static inline bool _charger_inserted(void)
84 return (GPIO1_READ & (1<<14)) ? false : true;
87 static inline bool _battery_full(void)
89 return (GPIO_READ & (1<<30)) ? true : false;
92 /* Reset the cookie for the crt0 crash check */
93 static inline void __reset_cookie(void)
95 asm(" move.l #0,%d0");
96 asm(" move.l %d0,0x10017ffc");
99 static void start_rockbox(void)
101 adc_close();
102 asm(" move.w #0x2700,%sr");
103 __reset_cookie();
104 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
105 asm(" movec.l %d0,%vbr");
106 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
107 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
108 asm(" jmp (%a0)");
111 static void start_mpio_firmware(void)
113 asm(" move.w #0x2700,%sr");
114 __reset_cookie();
115 asm(" movec.l %d0,%vbr");
116 asm(" move.l 0,%sp");
117 asm(" jmp 8");
120 static void __shutdown(void)
122 if (_charger_inserted())
123 /* if AC power do nothing */
124 return;
126 /* We need to gracefully spin down the disk to prevent clicks. */
127 if (ide_powered())
129 /* Make sure ATA has been initialized. */
130 storage_init();
132 /* And put the disk into sleep immediately. */
133 storage_sleepnow();
136 /* Backlight OFF */
137 _backlight_off();
138 __reset_cookie();
140 power_off();
143 /* Print the battery voltage (and a warning message). */
144 static void check_battery(void)
147 int battery_voltage, batt_int, batt_frac;
149 battery_voltage = battery_adc_voltage();
150 batt_int = battery_voltage / 1000;
151 batt_frac = (battery_voltage % 1000) / 10;
153 printf("Battery: %d.%02dV", batt_int, batt_frac);
155 if (battery_voltage <= 3500)
157 printf("WARNING! BATTERY LOW!!");
158 sleep(HZ*2);
164 static void lcd_putstring_centered(const char *string)
166 int w,h;
167 font_getstringsize(string, &w, &h, FONT_SYSFIXED);
168 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, string);
171 static void rb_boot(void)
173 int rc;
175 /* boost to speedup rb image loading */
176 cpu_boost(true);
178 rc = storage_init();
179 if(rc)
181 printf("ATA error: %d", rc);
182 sleep(HZ*5);
183 return;
186 disk_init();
188 rc = disk_mount_all();
189 if (rc<=0)
191 printf("No partition found");
192 sleep(HZ*5);
193 return;
196 printf("Loading firmware");
198 rc = load_firmware((unsigned char *)DRAM_START,
199 BOOTFILE, MAX_LOADSIZE);
201 if (rc < EOK)
203 printf("Error!");
204 printf("Can't load " BOOTFILE ": ");
205 printf("Result: %s", strerror(rc));
206 sleep(HZ*5);
207 return;
210 cpu_boost(false);
211 start_rockbox();
214 static void bootmenu(void)
216 enum option_t i;
217 enum option_t option = rockbox;
218 int button;
219 const char select[] = "->";
220 long start_tick = current_tick;
222 /* backbone of menu */
223 /* run the loader */
224 printf("Rockbox boot loader");
225 printf("Ver: " RBVERSION);
227 check_battery();
229 printf("");
230 printf("=========================");
232 line += BOOTMENU_OPTIONS+2; /* skip lines */
234 printf("=========================");
235 printf("");
236 printf(" [FF] [REW] to move ");
237 printf(" [PLAY] to confirm ");
239 /* content of menu and keys handling */
240 while (TIME_BEFORE(current_tick,start_tick + BOOTMENU_TIMEOUT))
242 /* Draw the menu. */
243 line = 6; /* move below header */
245 for (i=0;i<BOOTMENU_OPTIONS;i++)
247 if (i != option)
248 printf(" %s",bootmenu_options[i]);
249 else
250 printf("%s %s",select,bootmenu_options[i]);
253 line = 15;
255 printf("Time left: %ds",(BOOTMENU_TIMEOUT -
256 (current_tick - start_tick))/HZ);
258 lcd_update();
260 button = BUTTON_NONE;
261 button = button_get_w_tmo(HZ);
263 switch (button)
265 case BUTTON_REW:
266 case BUTTON_RC_REW:
267 if (option > rockbox)
268 option--;
269 else
270 option = shutdown;
271 break;
273 case BUTTON_FF:
274 case BUTTON_RC_FF:
275 if (option < shutdown)
276 option++;
277 else
278 option = rockbox;
279 break;
281 case BUTTON_PLAY:
282 case BUTTON_RC_PLAY:
283 case (BUTTON_PLAY|BUTTON_REC):
284 reset_screen();
286 switch (option)
288 case rockbox:
289 rb_boot();
290 break;
292 case mpio_firmware:
293 start_mpio_firmware();
294 break;
296 default:
297 return;
298 break;
302 /* timeout */
305 void main(void)
307 /* messages */
308 const char usb_connect_msg[] = "Bootloader USB mode";
309 const char charging_msg[] = "Charging...";
310 const char complete_msg[] = "Charging complete";
312 /* helper variable for messages */
313 bool blink_toggle = false;
315 int button;
316 unsigned int event = EVENT_NONE;
317 unsigned int last_event = EVENT_NONE;
319 /* this is default mode after power_init() */
320 bool high_current_charging = true;
322 /* setup GPIOs related to power functions */
323 power_init();
325 system_init();
326 kernel_init();
328 /* run at 45MHz */
329 set_cpu_frequency(CPUFREQ_NORMAL);
331 /* IRQs are needed by button driver */
332 enable_irq();
334 lcd_init();
336 /* only lowlevel functions no queue init */
337 _backlight_init();
338 _backlight_hw_on();
340 /* setup font system*/
341 font_init();
342 lcd_setfont(FONT_SYSFIXED);
344 /* buttons reading init*/
345 adc_init();
346 button_init();
348 usb_init();
349 cpu_idle_mode(true);
351 /* Handle wakeup event. Possibilities are:
352 * ON button (PLAY)
353 * USB insert
354 * AC charger plug
357 while(1)
359 /* read buttons */
360 event = EVENT_NONE;
361 button = button_get_w_tmo(HZ);
363 if ( (button & BUTTON_PLAY) || (button & BUTTON_RC_PLAY) )
364 event |= EVENT_ON;
366 if ( usb_detect() == USB_INSERTED )
367 event |= EVENT_USB;
369 if ( _charger_inserted() )
370 event |= EVENT_AC;
372 reset_screen();
373 switch (event)
375 case EVENT_ON:
376 case (EVENT_ON | EVENT_AC):
377 /* hold is handled in button driver */
378 cpu_idle_mode(false);
379 ide_power_enable(true);
381 if (button == (BUTTON_PLAY|BUTTON_REC))
382 bootmenu();
383 else
384 rb_boot();
386 break;
388 case EVENT_AC:
389 /* AC plug in */
390 if (!(last_event & EVENT_AC))
392 /* reset charging circuit */
393 and_l(~(1<<23), &GPIO_ENABLE);
396 /* USB unplug */
397 if (last_event & EVENT_USB)
399 usb_enable(false);
400 sleep(HZ);
401 ide_power_enable(false);
402 sleep(HZ);
405 if(!_battery_full())
407 if (blink_toggle)
408 lcd_putstring_centered(charging_msg);
410 blink_toggle = !blink_toggle;
412 else /* end of charge condition */
414 /* put LTC1733 into shutdown mode */
415 or_l((1<<23), &GPIO_ENABLE);
417 if (high_current_charging)
419 /* switch to low current mode */
420 and_l(~(1<<15), &GPIO_OUT);
422 /* reset charging circuit */
423 and_l(~(1<<23), &GPIO_ENABLE);
425 high_current_charging = false;
427 else
429 lcd_putstring_centered(complete_msg);
432 check_battery();
433 break;
435 case EVENT_USB:
436 case (EVENT_USB | EVENT_AC):
437 /* AC plug in while in USB mode */
438 if (!(last_event & EVENT_AC))
440 /* reset charger circuit */
441 and_l(~(1<<23), &GPIO_ENABLE);
444 /* USB plug in */
445 if (!(last_event & EVENT_USB))
447 /* init USB */
448 ide_power_enable(true);
449 sleep(HZ/20);
450 usb_enable(true);
453 /* display blinking USB indicator */
454 line = 0;
456 if (blink_toggle)
457 lcd_putstring_centered(usb_connect_msg);
459 check_battery();
460 blink_toggle = !blink_toggle;
461 storage_spin();
462 break;
464 default:
465 /* USB unplug */
466 if (last_event & EVENT_USB)
468 /* disable USB */
469 usb_enable(false);
470 sleep(HZ);
471 ide_power_enable(false);
472 sleep(HZ);
475 /* spurious wakeup ?*/
476 __shutdown();
477 break;
479 lcd_update();
480 last_event = event;
485 /* These functions are present in the firmware library, but we reimplement
486 them here because the originals do a lot more than we want */
487 void screen_dump(void)