1 /*
2 *
3 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the
16 * distribution.
17 *
18 * Neither the name of Texas Instruments Incorporated nor the names of
19 * its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
38 /*****************************************************************************************
39 * FILE PURPOSE: Perform the top level boot
40 *****************************************************************************************
41 * FILE NAME: iblmain.c
42 *
43 * DESCRIPTION: The top level boot examines the boot configuration and performs boot
44 * based on this configuration
45 *
46 * @file iblmain.c
47 *
48 * @brief
49 * This file is used to launch a boot based on the boot configuration structure
50 *
51 *****************************************************************************************/
52 #include "ibl.h"
53 #include "iblloc.h"
54 #include "iblcfg.h"
55 #include "device.h"
56 #include "ethboot.h"
57 #include "nandboot.h"
58 #include "norboot.h"
59 #include "bis.h"
60 #include "coffwrap.h"
61 #include "iblbtbl.h"
62 #include "iblblob.h"
63 #include "timer.h"
64 #include "i2c.h"
65 #include "spi_api.h"
66 #include "ibl_elf.h"
67 #include <string.h>
68 #include "c66x_uart.h"
70 extern cregister unsigned int IER;
72 uint32 iblEndianIdx = 0;
73 uint32 iblImageIdx = 0;
75 /**
76 * @brief
77 * Data structures shared between the 1st and 2nd stage IBL load
78 * are declared in a single header file, included in both stages
79 */
80 #include "iblStage.h"
84 /* Eat printfs */
85 void mprintf(char *x, ...) { }
87 /**
88 * @b Description
89 * @n
90 *
91 * Returns TRUE if the input priority is valid and enabled
92 */
93 BOOL iblPriorityIsValid (uint32 priority)
94 {
95 if ( (priority >= ibl_HIGHEST_PRIORITY) &&
96 (priority <= ibl_LOWEST_PRIORITY) )
98 return (TRUE);
101 return (FALSE);
103 }
105 /**
106 * @b Description
107 * @n
108 *
109 * Returns TRUE if the mac address is 0
110 */
111 BOOL iblMacAddrIsZero (uint8 *maddr)
112 {
113 int32 i;
115 for (i = 0; i < 6; i++)
116 if (maddr[i] != 0)
117 return (FALSE);
119 return (TRUE);
121 }
123 /**
124 * @b Description
125 * @n
126 *
127 * For NAND and NOR boots, configure the specified peripheral or memory interface
128 */
129 void iblPmemCfg (int32 interface, int32 port, bool enableNand)
130 {
131 int32 ret;
133 switch (interface) {
135 #if (!defined(EXCLUDE_NAND_GPIO))
137 case ibl_PMEM_IF_GPIO:
138 ret = devicePowerPeriph (TARGET_PWR_GPIO);
139 break;
140 #endif
142 #if (!defined(EXCLUDE_NOR_SPI) && !defined(EXCLUDE_NAND_SPI))
144 case ibl_PMEM_IF_SPI: {
146 Uint32 v;
147 spiConfig_t cfg;
149 ret = devicePowerPeriph (TARGET_PWR_SPI);
150 if (ret != 0)
151 break;
153 cfg.port = port;
154 cfg.mode = ibl.spiConfig.mode;
155 cfg.addrWidth = ibl.spiConfig.addrWidth;
156 cfg.npin = ibl.spiConfig.nPins;
157 cfg.csel = ibl.spiConfig.csel;
158 cfg.c2tdelay = ibl.spiConfig.c2tdelay;
160 /* On c66x devices the PLL module has a built in divide by 6, and the SPI
161 * has a maximum clock divider value of 0xff */
162 v = ibl.pllConfig[ibl_MAIN_PLL].pllOutFreqMhz / (DEVICE_SPI_MOD_DIVIDER * ibl.spiConfig.busFreqMHz);
163 if (v > 0xff)
164 v = 0xff;
166 cfg.clkdiv = (UINT16) v;
168 ret = hwSpiConfig (&cfg);
169 if (ret != 0) {
170 iblStatus.iblFail = ibl_FAIL_CODE_SPI_PARAMS;
171 return;
172 }
174 }
175 break;
176 #endif
178 #if (!defined(EXCLUDE_NOR_EMIF) || !defined(EXCLUDE_NAND_EMIF))
180 case ibl_PMEM_IF_CHIPSEL_2:
181 case ibl_PMEM_IF_CHIPSEL_3:
182 case ibl_PMEM_IF_CHIPSEL_4:
183 case ibl_PMEM_IF_CHIPSEL_5: {
185 int i;
187 /* Locate the configuration corresponding to this chip select space */
188 for (i = 0; i < ibl_MAX_EMIF_PMEM; i++)
189 if (ibl.emifConfig[i].csSpace == interface)
190 break;
192 if (i == ibl_MAX_EMIF_PMEM) {
193 iblStatus.iblFail = ibl_FAIL_CODE_NO_EMIF_CFG;
194 return;
195 }
197 ret = devicePowerPeriph (TARGET_PWR_EMIF);
198 if (ret != 0)
199 break;
201 if (hwEmif25Init (interface, ibl.emifConfig[i].busWidth, ibl.emifConfig[i].waitEnable, enableNand) != 0)
202 iblStatus.iblFail = ibl_FAIL_CODE_EMIF_CFG_FAIL;
204 }
205 break;
207 #endif
209 default:
213 iblStatus.iblFail = ibl_FAIL_CODE_INVALID_NAND_PERIPH;
214 return;
215 }
217 if (ret != 0) {
218 iblStatus.iblFail = ibl_FAIL_CODE_PERIPH_POWER_UP;
219 return;
220 }
222 }
226 /**
227 * @b Description
228 * @n
229 *
230 * The main function kicks off the boot. If it does not find the magic value in the
231 * configuration array then default values are loaded. This default load
232 * is done only once at the start of boot.
233 *
234 * @retval
235 * None
236 */
237 void main (void)
238 {
239 int32 i, j;
240 UINT32 v, boot_mode_idx, boot_para_idx;
241 unsigned int dip_setting;
243 /* Initialize the status structure */
244 iblMemset (&iblStatus, 0, sizeof(iblStatus_t));
245 iblStatus.iblMagic = ibl_MAGIC_VALUE;
246 iblStatus.iblVersion = ibl_VERSION;
249 /* Power up the timer */
250 devicePowerPeriph (TARGET_PWR_TIMER_0);
252 /* Initialize the system timer (software tracking of the hardware timer state) */
253 timer_init ();
255 /* Load default mac addresses for ethernet boot if requested */
256 for (i = 0; i < ibl_N_BOOT_MODES; i++) {
258 if (ibl.bootModes[i].bootMode == ibl_BOOT_MODE_TFTP) {
260 if (iblMacAddrIsZero (ibl.bootModes[i].u.ethBoot.ethInfo.hwAddress))
262 deviceLoadDefaultEthAddress (ibl.bootModes[i].u.ethBoot.ethInfo.hwAddress);
263 }
264 }
267 /* DDR configuration is device specific */
268 deviceDdrConfig ();
270 /* Try booting forever */
271 for (;;) {
273 #ifndef EXCLUDE_MULTI_BOOT
274 v = DEVICE_REG32_R(DEVICE_JTAG_ID_REG);
275 v &= DEVICE_JTAG_ID_MASK;
276 if (
277 (v == DEVICE_C6678_JTAG_ID_VAL) ||
278 (v == DEVICE_C6670_JTAG_ID_VAL)
279 )
280 {
281 IER = 0;
283 /* For C66x devices, check the DEVSTAT register to find which image on which device to boot. */
284 v = DEVICE_REG32_R(DEVICE_REG_DEVSTAT);
286 /* Get the Endianness */
287 if (ibl_N_ENDIANS == 1)
288 {
289 iblEndianIdx = 0;
290 }
291 else
292 {
293 if (v & ibl_ENDIAN_LITTLE)
294 {
295 iblEndianIdx = 0;
296 }
297 else
298 {
299 iblEndianIdx = 1;
300 }
301 }
303 /* Get the boot mode index */
304 boot_para_idx = BOOT_READ_BITFIELD(v,8,4);
306 /* Only 1 image supported for TFTP boot */
307 if (boot_para_idx > (ibl_N_IMAGES*(ibl_N_BOOT_MODES-1)))
308 {
309 /* boot parameter index not supported */
310 continue;
311 }
312 boot_mode_idx = boot_para_idx/ibl_N_IMAGES;
313 /* Get the boot image index */
314 iblImageIdx = boot_para_idx & (ibl_N_IMAGES - 1);
316 iblStatus.activeBoot = ibl.bootModes[boot_mode_idx].bootMode;
318 switch (ibl.bootModes[boot_mode_idx].bootMode)
319 {
320 #ifndef EXCLUDE_ETH
321 case ibl_BOOT_MODE_TFTP:
322 iblStatus.activeDevice = ibl_ACTIVE_DEVICE_ETH;
323 /*Print something to UART, max len=80, if len is pased as 0 UART prints entire string upto '\n' or max len */
324 uart_write_string("IBL: Booting from ethernet",0);
325 iblMemcpy (&iblStatus.ethParams, &ibl.bootModes[boot_mode_idx].u.ethBoot.ethInfo, sizeof(iblEthBootInfo_t));
326 iblEthBoot (boot_mode_idx);
327 break;
328 #endif
330 #if ((!defined(EXCLUDE_NAND_EMIF)) || (!defined(EXCLUDE_NAND_GPIO)))
331 case ibl_BOOT_MODE_NAND:
332 iblPmemCfg (ibl.bootModes[boot_mode_idx].u.nandBoot.interface, ibl.bootModes[boot_mode_idx].port, TRUE);
333 memset ((void *)0x80000000, 0, 0x20000000);
334 /*Print something to UART, max len=80, if len is pased as 0 UART prints entire string upto '\n' or max len */
335 uart_write_string("IBL: Booting from NAND",0);
336 iblNandBoot (boot_mode_idx);
337 break;
338 #endif
340 #if (!defined(EXCLUDE_NOR_EMIF) && !defined(EXCLUDE_NOR_SPI))
341 case ibl_BOOT_MODE_NOR:
342 iblPmemCfg (ibl.bootModes[boot_mode_idx].u.norBoot.interface, ibl.bootModes[boot_mode_idx].port, TRUE);
343 /*Print something to UART, max len=80, if len is pased as 0 UART prints entire string upto '\n' or max len */
344 uart_write_string("IBL: Booting from NOR",0);
345 iblNorBoot (boot_mode_idx);
346 break;
347 #endif
348 }
349 iblStatus.heartBeat += 1;
350 }
351 #else
353 dip_setting = get_device_switch_setting();
355 if (dip_setting == 0)
356 boot_mode_idx = 0;
357 else if (dip_setting == 1)
358 boot_mode_idx = 1;
360 iblStatus.activeBoot = ibl.bootModes[boot_mode_idx].bootMode;
362 switch (ibl.bootModes[boot_mode_idx].bootMode) {
363 #ifndef EXCLUDE_ETH
364 case ibl_BOOT_MODE_TFTP:
365 iblStatus.activeDevice = ibl_ACTIVE_DEVICE_ETH;
366 iblMemcpy (&iblStatus.ethParams, &ibl.bootModes[boot_mode_idx].u.ethBoot.ethInfo, sizeof(iblEthBootInfo_t));
367 /*Print something to UART, max len=80, if len is pased as 0 UART prints entire string upto '\n' or max len */
368 uart_write_string("IBL: Booting from Ethernet",0);
369 iblEthBoot (boot_mode_idx);
370 break;
371 #endif
373 #if ((!defined(EXCLUDE_NAND_EMIF)) || (!defined(EXCLUDE_NAND_GPIO)))
374 case ibl_BOOT_MODE_NAND:
375 iblPmemCfg (ibl.bootModes[boot_mode_idx].u.nandBoot.interface, ibl.bootModes[boot_mode_idx].port, TRUE);
376 /*Print something to UART, max len=80, if len is pased as 0 UART prints entire string upto '\n' or max len */
377 uart_write_string("IBL: Booting from NAND",0);
378 iblNandBoot (boot_mode_idx);
379 break;
380 #endif
381 }
382 iblStatus.heartBeat += 1;
383 #endif
385 }
386 } /* main */
390 /**
391 * @b Description
392 * @n
393 *
394 * The ibl boot function links a device to a data format. The data format
395 * parser pulls data from the boot device
396 *
397 * @param[in] bootFxn The structure containing the boot device functions
398 *
399 * @retval
400 * None
401 */
402 Uint32 iblBoot (BOOT_MODULE_FXN_TABLE *bootFxn, Int32 dataFormat, void *formatParams)
403 {
404 Uint32 entry = 0;
405 Uint32 value32;
406 Uint8 dataBuf[4];
407 Uint16 value16;
409 /* Determine the data format if required */
410 if (dataFormat == ibl_BOOT_FORMAT_AUTO) {
412 (*bootFxn->peek)(dataBuf, sizeof(dataBuf));
413 value32 = (dataBuf[0] << 24) | (dataBuf[1] << 16) | (dataBuf[2] << 8) | (dataBuf[3] << 0);
414 value16 = (dataBuf[0] << 8) | (dataBuf[1] << 0);
416 /* BIS */
417 #ifndef EXCLUDE_BIS
418 if (value32 == BIS_MAGIC_NUMBER)
419 dataFormat = ibl_BOOT_FORMAT_BIS;
420 #endif
422 #ifndef EXCLUDE_COFF
423 if (iblIsCoff (value16))
424 dataFormat = ibl_BOOT_FORMAT_COFF;
425 #endif
427 #ifndef EXCLUDE_ELF
428 if (iblIsElf (dataBuf))
429 dataFormat = ibl_BOOT_FORMAT_ELF;
430 #endif
432 if (dataFormat == ibl_BOOT_FORMAT_AUTO) {
433 iblStatus.autoDetectFailCnt += 1;
434 return (0);
435 }
436 }
439 iblStatus.activeFileFormat = dataFormat;
442 /* Invoke the parser */
443 switch (dataFormat) {
445 #ifndef EXCLUDE_BIS
446 case ibl_BOOT_FORMAT_BIS:
447 iblBootBis (bootFxn, &entry);
448 break;
449 #endif
451 #ifndef EXCLUDE_COFF
452 case ibl_BOOT_FORMAT_COFF:
453 iblBootCoff (bootFxn, &entry);
454 break;
455 #endif
457 case ibl_BOOT_FORMAT_BTBL:
458 iblBootBtbl (bootFxn, &entry);
459 break;
461 #ifndef EXCLUDE_BLOB
462 case ibl_BOOT_FORMAT_BBLOB:
463 iblBootBlob (bootFxn, &entry, formatParams);
464 break;
465 #endif
467 #ifndef EXCLUDE_ELF
468 case ibl_BOOT_FORMAT_ELF:
469 iblBootElf (bootFxn, &entry);
470 break;
471 #endif
473 default:
474 iblStatus.invalidDataFormatSpec += 1;
475 break;
477 }
480 return (entry);
482 }