wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 1 | /* |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 2 | * (C) Copyright 2007 Michal Simek |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 3 | * (C) Copyright 2004 Atmark Techno, Inc. |
| 4 | * |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 5 | * Michal SIMEK <monstr@monstr.eu> |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 6 | * Yasushi SHOJI <yashi@atmark-techno.com> |
| 7 | * |
| 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | #include <common.h> |
| 28 | #include <command.h> |
| 29 | #include <malloc.h> |
| 30 | #include <version.h> |
wdenk | 78924a7 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 31 | #include <watchdog.h> |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 32 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 33 | DECLARE_GLOBAL_DATA_PTR; |
| 34 | |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 35 | const char version_string[] = U_BOOT_VERSION " (" __DATE__ " - " __TIME__ ")"; |
| 36 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 37 | #ifdef CONFIG_SYS_GPIO_0 |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 38 | extern int gpio_init (void); |
| 39 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 40 | #ifdef CONFIG_SYS_INTC_0 |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 41 | extern int interrupts_init (void); |
| 42 | #endif |
Jon Loeliger | 6b23305 | 2007-07-09 18:02:11 -0500 | [diff] [blame] | 43 | #if defined(CONFIG_CMD_NET) |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 44 | extern int eth_init (bd_t * bis); |
| 45 | extern int getenv_IPaddr (char *); |
| 46 | #endif |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 47 | |
| 48 | /* |
| 49 | * Begin and End of memory area for malloc(), and current "brk" |
| 50 | */ |
| 51 | static ulong mem_malloc_start; |
| 52 | static ulong mem_malloc_end; |
| 53 | static ulong mem_malloc_brk; |
| 54 | |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 55 | /* |
| 56 | * The Malloc area is immediately below the monitor copy in DRAM |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 57 | * aka CONFIG_SYS_MONITOR_BASE - Note there is no need for reloc_off |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 58 | * as our monitory code is run from SDRAM |
| 59 | */ |
| 60 | static void mem_malloc_init (void) |
| 61 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 62 | mem_malloc_end = (CONFIG_SYS_MALLOC_BASE + CONFIG_SYS_MALLOC_LEN); |
| 63 | mem_malloc_start = CONFIG_SYS_MALLOC_BASE; |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 64 | mem_malloc_brk = mem_malloc_start; |
| 65 | memset ((void *)mem_malloc_start, 0, mem_malloc_end - mem_malloc_start); |
| 66 | } |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 67 | |
| 68 | void *sbrk (ptrdiff_t increment) |
| 69 | { |
| 70 | ulong old = mem_malloc_brk; |
| 71 | ulong new = old + increment; |
| 72 | |
| 73 | if ((new < mem_malloc_start) || (new > mem_malloc_end)) { |
| 74 | return (NULL); |
| 75 | } |
| 76 | mem_malloc_brk = new; |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 77 | return ((void *)old); |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /* |
| 81 | * All attempts to come up with a "common" initialization sequence |
| 82 | * that works for all boards and architectures failed: some of the |
| 83 | * requirements are just _too_ different. To get rid of the resulting |
| 84 | * mess of board dependend #ifdef'ed code we now make the whole |
| 85 | * initialization sequence configurable to the user. |
| 86 | * |
| 87 | * The requirements for any new initalization function is simple: it |
| 88 | * receives a pointer to the "global data" structure as it's only |
| 89 | * argument, and returns an integer return code, where 0 means |
| 90 | * "continue" and != 0 means "fatal error, hang the system". |
| 91 | */ |
| 92 | typedef int (init_fnc_t) (void); |
| 93 | |
| 94 | init_fnc_t *init_sequence[] = { |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 95 | env_init, |
| 96 | serial_init, |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 97 | #ifdef CONFIG_SYS_GPIO_0 |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 98 | gpio_init, |
| 99 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 100 | #ifdef CONFIG_SYS_INTC_0 |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 101 | interrupts_init, |
| 102 | #endif |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 103 | NULL, |
| 104 | }; |
| 105 | |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 106 | void board_init (void) |
wdenk | 78924a7 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 107 | { |
wdenk | 20a6122 | 2004-07-10 23:48:41 +0000 | [diff] [blame] | 108 | bd_t *bd; |
wdenk | 78924a7 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 109 | init_fnc_t **init_fnc_ptr; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 110 | gd = (gd_t *) CONFIG_SYS_GBL_DATA_OFFSET; |
Jon Loeliger | 6b23305 | 2007-07-09 18:02:11 -0500 | [diff] [blame] | 111 | #if defined(CONFIG_CMD_FLASH) |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 112 | ulong flash_size = 0; |
| 113 | #endif |
| 114 | asm ("nop"); /* FIXME gd is not initialize - wait */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 115 | memset ((void *)gd, 0, CONFIG_SYS_GBL_DATA_SIZE); |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 116 | gd->bd = (bd_t *) (gd + 1); /* At end of global data */ |
wdenk | 20a6122 | 2004-07-10 23:48:41 +0000 | [diff] [blame] | 117 | gd->baudrate = CONFIG_BAUDRATE; |
wdenk | 20a6122 | 2004-07-10 23:48:41 +0000 | [diff] [blame] | 118 | bd = gd->bd; |
| 119 | bd->bi_baudrate = CONFIG_BAUDRATE; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 120 | bd->bi_memstart = CONFIG_SYS_SDRAM_BASE; |
| 121 | bd->bi_memsize = CONFIG_SYS_SDRAM_SIZE; |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 122 | |
| 123 | /* Initialise malloc() area */ |
| 124 | mem_malloc_init (); |
wdenk | 20a6122 | 2004-07-10 23:48:41 +0000 | [diff] [blame] | 125 | |
wdenk | 78924a7 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 126 | for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) { |
| 127 | WATCHDOG_RESET (); |
| 128 | if ((*init_fnc_ptr) () != 0) { |
| 129 | hang (); |
| 130 | } |
| 131 | } |
| 132 | |
Jon Loeliger | 6b23305 | 2007-07-09 18:02:11 -0500 | [diff] [blame] | 133 | #if defined(CONFIG_CMD_FLASH) |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 134 | bd->bi_flashstart = CONFIG_SYS_FLASH_BASE; |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 135 | if (0 < (flash_size = flash_init ())) { |
| 136 | bd->bi_flashsize = flash_size; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 137 | bd->bi_flashoffset = CONFIG_SYS_FLASH_BASE + flash_size; |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 138 | } else { |
| 139 | puts ("Flash init FAILED"); |
| 140 | bd->bi_flashstart = 0; |
| 141 | bd->bi_flashsize = 0; |
| 142 | bd->bi_flashoffset = 0; |
| 143 | } |
| 144 | #endif |
| 145 | |
Jon Loeliger | 6b23305 | 2007-07-09 18:02:11 -0500 | [diff] [blame] | 146 | #if defined(CONFIG_CMD_NET) |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 147 | char *s, *e; |
| 148 | int i; |
| 149 | /* board MAC address */ |
| 150 | s = getenv ("ethaddr"); |
| 151 | for (i = 0; i < 6; ++i) { |
| 152 | bd->bi_enetaddr[i] = s ? simple_strtoul (s, &e, 16) : 0; |
| 153 | if (s) |
| 154 | s = (*e) ? e + 1 : e; |
| 155 | } |
| 156 | /* IP Address */ |
| 157 | bd->bi_ip_addr = getenv_IPaddr ("ipaddr"); |
| 158 | eth_init (bd); |
| 159 | #endif |
| 160 | |
| 161 | /* relocate environment function pointers etc. */ |
| 162 | env_relocate (); |
| 163 | |
wdenk | 78924a7 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 164 | /* main_loop */ |
| 165 | for (;;) { |
| 166 | WATCHDOG_RESET (); |
| 167 | main_loop (); |
| 168 | } |
| 169 | } |
| 170 | |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 171 | void hang (void) |
| 172 | { |
| 173 | puts ("### ERROR ### Please RESET the board ###\n"); |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 174 | for (;;) ; |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 175 | } |