]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - keystone-rtos/ibl.git/blob - src/main/iblmain.c
Merge branch 'master' of git@gtgit01.gt.design.ti.com:git/teams/mmi/ibl
[keystone-rtos/ibl.git] / src / main / iblmain.c
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>
69 extern cregister unsigned int IER;
71 uint32 iblEndianIdx = 0;
72 uint32 iblImageIdx = 0;
74 /**
75  *  @brief
76  *      Data structures shared between the 1st and 2nd stage IBL load
77  *      are declared in a single header file, included in both stages
78  */
79 #include "iblStage.h"
83 /* Eat printfs */
84 void mprintf(char *x, ...) { }
86 /**
87  * @b Description
88  * @n
89  *
90  *  Returns TRUE if the input priority is valid and enabled
91  */
92 BOOL iblPriorityIsValid (uint32 priority)
93 {
94     if ( (priority >= ibl_HIGHEST_PRIORITY)  &&
95          (priority <= ibl_LOWEST_PRIORITY)   )
97         return (TRUE);
100     return (FALSE);
104 /**
105  * @b Description
106  * @n
107  *
108  *  Returns TRUE if the mac address is 0
109  */
110 BOOL iblMacAddrIsZero (uint8 *maddr)
112     int32 i;
114     for (i = 0; i < 6; i++)
115         if (maddr[i] != 0)
116             return (FALSE);
118     return (TRUE);
122 /**
123  *  @b Description
124  *  @n
125  *  
126  *  For NAND and NOR boots, configure the specified peripheral or memory interface
127  */
128 void iblPmemCfg (int32 interface, int32 port, bool enableNand)
130     int32 ret;
132     switch (interface)  {
134         #if (!defined(EXCLUDE_NAND_GPIO))
136         case ibl_PMEM_IF_GPIO:
137                 ret = devicePowerPeriph (TARGET_PWR_GPIO);
138                 break;
139         #endif
141         #if (!defined(EXCLUDE_NOR_SPI) && !defined(EXCLUDE_NAND_SPI))
143             case ibl_PMEM_IF_SPI:  {
145                     Uint32      v;
146                     spiConfig_t cfg;
148                     ret = devicePowerPeriph (TARGET_PWR_SPI);
149                     if (ret != 0)
150                         break;
152                     cfg.port      = port;
153                     cfg.mode      = ibl.spiConfig.mode;
154                     cfg.addrWidth = ibl.spiConfig.addrWidth;
155                     cfg.npin      = ibl.spiConfig.nPins;
156                     cfg.csel      = ibl.spiConfig.csel;
157                     cfg.c2tdelay  = ibl.spiConfig.c2tdelay;
159                     /* On c661x devices the PLL module has a built in divide by 6, and the SPI
160                      * has a maximum clock divider value of 0xff */
161                     v = ibl.pllConfig[ibl_MAIN_PLL].pllOutFreqMhz / (DEVICE_SPI_MOD_DIVIDER * ibl.spiConfig.busFreqMHz);
162                     if (v > 0xff)
163                         v = 0xff;
165                     cfg.clkdiv =  (UINT16) v;
167                     ret = hwSpiConfig (&cfg);
168                     if (ret != 0)  {
169                         iblStatus.iblFail = ibl_FAIL_CODE_SPI_PARAMS;
170                         return;
171                     }
173                 }
174                 break;
175         #endif
177         #if (!defined(EXCLUDE_NOR_EMIF) || !defined(EXCLUDE_NAND_EMIF))
179             case ibl_PMEM_IF_CHIPSEL_2:
180             case ibl_PMEM_IF_CHIPSEL_3:
181             case ibl_PMEM_IF_CHIPSEL_4:
182             case ibl_PMEM_IF_CHIPSEL_5:  {
184                     int i;
186                     /* Locate the configuration corresponding to this chip select space */
187                     for (i = 0; i < ibl_MAX_EMIF_PMEM; i++) 
188                         if (ibl.emifConfig[i].csSpace == interface)
189                             break;
190                         
191                     if (i == ibl_MAX_EMIF_PMEM)  {
192                         iblStatus.iblFail = ibl_FAIL_CODE_NO_EMIF_CFG;
193                         return;
194                     }
196                     ret = devicePowerPeriph (TARGET_PWR_EMIF);
197                     if (ret != 0)
198                         break;
200                     if (hwEmif25Init (interface, ibl.emifConfig[i].busWidth, ibl.emifConfig[i].waitEnable, enableNand) != 0)
201                         iblStatus.iblFail = ibl_FAIL_CODE_EMIF_CFG_FAIL;
203                 }
204                 break;
206             #endif
208             default:
212                 iblStatus.iblFail = ibl_FAIL_CODE_INVALID_NAND_PERIPH;
213                 return;
214     }
216     if (ret != 0)  {
217         iblStatus.iblFail = ibl_FAIL_CODE_PERIPH_POWER_UP;
218         return;
219     }
225 /**
226  * @b Description
227  * @n
228  *
229  * The main function kicks off the boot. If it does not find the magic value in the
230  *     configuration array then default values are loaded. This default load
231  *     is done only once at the start of boot. 
232  *
233  * @retval
234  *  None
235  */
236 void main (void)
238     int32 i, j;
239     UINT32 v, boot_mode_idx, boot_para_idx;
241     /* Initialize the status structure */
242     iblMemset (&iblStatus, 0, sizeof(iblStatus_t));
243     iblStatus.iblMagic   = ibl_MAGIC_VALUE;
244     iblStatus.iblVersion = ibl_VERSION;
247     /* Power up the timer */
248     devicePowerPeriph (TARGET_PWR_TIMER_0);
250     /* Initialize the system timer (software tracking of the hardware timer state) */
251     timer_init ();
253     /* Load default mac addresses for ethernet boot if requested */
254     for (i = 0; i < ibl_N_BOOT_MODES; i++)  {
256         if (ibl.bootModes[i].bootMode == ibl_BOOT_MODE_TFTP)  {
258             if (iblMacAddrIsZero (ibl.bootModes[i].u.ethBoot.ethInfo.hwAddress))
260                 deviceLoadDefaultEthAddress (ibl.bootModes[i].u.ethBoot.ethInfo.hwAddress);
261         }
262     }
265     /* DDR configuration is device specific */
266     deviceDdrConfig ();
268     /* Try booting forever */
269     for (;;)  {
271 #ifndef EXCLUDE_MULTI_BOOT
272         v = DEVICE_REG32_R(DEVICE_JTAG_ID_REG);
273         if (
274             (v == DEVICE_C6618_JTAG_ID_VAL)         || 
275             (v == DEVICE_C6616_JTAG_ID_VAL)
276            )
277         {
278             IER = 0;
280             /* For C66x devices, check the DEVSTAT register to find which image on which device to boot. */
281             v = DEVICE_REG32_R(DEVICE_REG_DEVSTAT);
282             
283             /* Get the Endianness */
284             if (ibl_N_ENDIANS == 1)
285             {
286                 iblEndianIdx = 0;
287             }
288             else
289             {
290                 if (v & ibl_ENDIAN_LITTLE)
291                 {
292                     iblEndianIdx = 0;
293                 }
294                 else
295                 {
296                     iblEndianIdx = 1;
297                 }
298             }
300             /* Get the boot mode index */
301             boot_para_idx = BOOT_READ_BITFIELD(v,8,4);
303             /* Only 1 image supported for TFTP boot */
304             if (boot_para_idx > (ibl_N_IMAGES*(ibl_N_BOOT_MODES-1)))
305             {
306                 /* boot parameter index not supported */
307                 continue;
308             }
309             boot_mode_idx = boot_para_idx/ibl_N_IMAGES;
310             /* Get the boot image index */
311             iblImageIdx == boot_para_idx & (ibl_N_IMAGES - 1);
313             iblStatus.activeBoot = ibl.bootModes[boot_mode_idx].bootMode;
315             switch (ibl.bootModes[boot_mode_idx].bootMode)  
316             {
317 #ifndef EXCLUDE_ETH
318             case ibl_BOOT_MODE_TFTP:
319                 iblStatus.activeDevice = ibl_ACTIVE_DEVICE_ETH;
320                 iblMemcpy (&iblStatus.ethParams, &ibl.bootModes[boot_mode_idx].u.ethBoot.ethInfo, sizeof(iblEthBootInfo_t));
321                 iblEthBoot (boot_mode_idx);
322                 break;
323 #endif
324                 
325 #if ((!defined(EXCLUDE_NAND_EMIF)) || (!defined(EXCLUDE_NAND_GPIO)))
326             case ibl_BOOT_MODE_NAND:
327                 iblPmemCfg (ibl.bootModes[boot_mode_idx].u.nandBoot.interface, ibl.bootModes[boot_mode_idx].port, TRUE);
328                 memset ((void *)0x80000000, 0, 0x20000000);
329                 iblNandBoot (boot_mode_idx);
330                 break;
331 #endif
332                 
333 #if (!defined(EXCLUDE_NOR_EMIF) && !defined(EXCLUDE_NOR_SPI))
334             case ibl_BOOT_MODE_NOR:
335                 iblPmemCfg (ibl.bootModes[boot_mode_idx].u.norBoot.interface, ibl.bootModes[boot_mode_idx].port, TRUE);
336                 iblNorBoot (boot_mode_idx);
337                 break;
338 #endif
339             }
340             iblStatus.heartBeat += 1;
341         }
342         else
343 #endif
344         {
345             
346            /* For C64x devices, loop through the boot modes to find the one with the highest priority
347             * value, and try to boot it. */
348             for (i = ibl_HIGHEST_PRIORITY; i < ibl_LOWEST_PRIORITY; i++)  {
349                 
350                 for (j = 0; j < ibl_N_BOOT_MODES; j++)  {
351                     
352                     if (ibl.bootModes[j].priority == i)  {
353                         
354                         iblStatus.activeBoot = ibl.bootModes[j].bootMode;
355                         
356                         switch (ibl.bootModes[j].bootMode)  {
357                             
358                             
359 #ifndef EXCLUDE_ETH
360                         case ibl_BOOT_MODE_TFTP:
361                             iblStatus.activeDevice = ibl_ACTIVE_DEVICE_ETH;
362                             iblMemcpy (&iblStatus.ethParams, &ibl.bootModes[j].u.ethBoot.ethInfo, sizeof(iblEthBootInfo_t));
363                             iblEthBoot (j);
364                             break;
365 #endif
366                             
367 #if ((!defined(EXCLUDE_NAND_EMIF)) || (!defined(EXCLUDE_NAND_GPIO)))
368                         case ibl_BOOT_MODE_NAND:
369                             iblPmemCfg (ibl.bootModes[j].u.nandBoot.interface, ibl.bootModes[j].port, TRUE);
370                             iblNandBoot (j);
371                             break;
372 #endif
373                             
374 #if (!defined(EXCLUDE_NOR_EMIF) && !defined(EXCLUDE_NOR_SPI))
375                         case ibl_BOOT_MODE_NOR:
376                             iblPmemCfg (ibl.bootModes[j].u.norBoot.interface, ibl.bootModes[j].port, TRUE);
377                             iblNorBoot (j);
378                             break;
379 #endif
380                             
381                         }
382                     }
383                     
384                     iblStatus.heartBeat += 1;
385                     
386                 }
387             }
388         }
389     }
392 } /* main */
395     
396 /**
397  * @b Description
398  * @n
399  * 
400  * The ibl boot function links a device to a data format. The data format
401  * parser pulls data from the boot device
402  *
403  * @param[in] bootFxn      The structure containing the boot device functions
404  *
405  * @retval
406  *  None
407  */
408 Uint32 iblBoot (BOOT_MODULE_FXN_TABLE *bootFxn, Int32 dataFormat, void *formatParams)
409
410     Uint32  entry = 0;
411     Uint32  value32;
412     Uint8   dataBuf[4];   
413     Uint16  value16;
415     /* Determine the data format if required */
416     if (dataFormat == ibl_BOOT_FORMAT_AUTO)  {
418         (*bootFxn->peek)(dataBuf, sizeof(dataBuf));
419         value32 = (dataBuf[0] << 24) | (dataBuf[1] << 16) | (dataBuf[2] << 8) | (dataBuf[3] << 0);
420         value16 = (dataBuf[0] <<  8) | (dataBuf[1] <<  0);
422         /* BIS */
423 #ifndef EXCLUDE_BIS
424         if (value32 == BIS_MAGIC_NUMBER)
425             dataFormat = ibl_BOOT_FORMAT_BIS;
426 #endif
428 #ifndef EXCLUDE_COFF
429         if (iblIsCoff (value16))
430             dataFormat = ibl_BOOT_FORMAT_COFF;
431 #endif
433 #ifndef EXCLUDE_ELF
434         if (iblIsElf (dataBuf))
435             dataFormat = ibl_BOOT_FORMAT_ELF;
436 #endif
438         if (dataFormat == ibl_BOOT_FORMAT_AUTO)  {
439             iblStatus.autoDetectFailCnt += 1;
440             return (0);
441         }
442     }        
445     iblStatus.activeFileFormat = dataFormat;
448     /* Invoke the parser */
449     switch (dataFormat)  {
451 #ifndef EXCLUDE_BIS
452         case ibl_BOOT_FORMAT_BIS:
453             iblBootBis (bootFxn, &entry);
454             break;
455 #endif
457 #ifndef EXCLUDE_COFF
458         case ibl_BOOT_FORMAT_COFF:
459             iblBootCoff (bootFxn, &entry);
460             break;
461 #endif
463         case ibl_BOOT_FORMAT_BTBL:
464             iblBootBtbl (bootFxn, &entry);
465             break;
467 #ifndef EXCLUDE_BLOB
468         case ibl_BOOT_FORMAT_BBLOB:
469             iblBootBlob (bootFxn, &entry, formatParams);
470             break;
471 #endif
473 #ifndef EXCLUDE_ELF
474         case ibl_BOOT_FORMAT_ELF:
475             iblBootElf (bootFxn, &entry);
476             break;
477 #endif
479         default:
480             iblStatus.invalidDataFormatSpec += 1;
481             break;
483     }
484     
486     return (entry);