e3b24faa07ca9c4c1c6e2a4ec7c419e293093469
1 /**
2 * @file iblinit.c
3 *
4 * @brief
5 * This file contains code which runs prior to loading the full IBL
6 *
7 * @details
8 * The IBL loads itself in a two stage process. The ROM boot loader
9 * loads this first stage IBL first. This entire program must be
10 * endian independent in execution.
11 *
12 * This first loader reads the IBL parameters, and will endian
13 * switch them if required. The PLL is configured if indicated
14 * by the parameters.
15 *
16 * The I2C block which contains the I2C EEPROM address for both
17 * the big and little endian images is then read. Based on the
18 * endianness of the device the rest of the IBL is read from
19 * the I2C EEPROM, and execution is transferred to the full
20 * IBL.
21 *
22 * The subsequent reads are allowed to cross 16 bit i2c EEPROM
23 * addresses. When the boundary is crossed the i2c address
24 * field is incremented.
25 *
26 */
28 #include "ibl.h"
29 #include "iblloc.h"
30 #include "iblcfg.h"
31 #include "device.h"
32 #include "iblbtbl.h"
33 #include "i2c.h"
34 #include <string.h>
37 /**
38 * @brief
39 * Data structures shared between the 1st and 2nd stage IBL load
40 * are declared in a single header file, included in both stages
41 */
42 #include "iblStage.h"
44 /**
45 * @brief
46 * byte swapping of i2c data must be done when in little endian mode
47 */
48 bool littleEndian;
50 /**
51 * @brief
52 * The boot table processing status is declared in the boot table wrapper,
53 * and used here in the main status fields.
54 */
55 extern Int32 btblWrapEcode;
57 /**
58 * @brief
59 * Ones complement addition
60 */
61 inline uint16 onesComplementAdd (uint16 value1, uint16 value2)
62 {
63 uint32 result;
65 result = (uint32)value1 + (uint32)value2;
67 result = (result >> 16) + (result & 0xFFFF); /* add in carry */
68 result += (result >> 16); /* maybe one more */
69 return ((uint16)result);
70 }
73 /**
74 * @brief
75 * Ones complement checksum computation
76 */
77 uint16 onesComplementChksum (uint16 * restrict p_data, uint16 len)
78 {
79 uint16 chksum = 0;
81 while (len > 0)
82 {
83 chksum = onesComplementAdd(chksum, *p_data);
84 p_data++;
85 len--;
86 }
87 return (chksum);
88 }
92 /**
93 * @brief
94 * Do a 4 byte endian swap
95 */
96 uint32 swap32val (uint32 v)
97 {
98 v = (((v >> 24) & 0xff) << 0) |
99 (((v >> 16) & 0xff) << 8) |
100 (((v >> 8) & 0xff) << 16) |
101 (((v >> 0) & 0xff) << 24);
103 return (v);
105 }
107 /**
108 * @brief
109 * Do a 2 byte endian swap
110 */
111 uint16 swap16val (uint16 v)
112 {
113 v = (((v >> 8) & 0xff) << 0) |
114 (((v >> 0) & 0xff) << 8);
116 return (v);
118 }
120 /**
121 * @brief
122 * Do an endian swap on the ibl structure
123 */
124 void iblSwap (void)
125 {
126 int i;
128 ibl.iblMagic = swap32val (ibl.iblMagic);
130 for (i = 0; i < ibl_N_PLL_CFGS; i++) {
131 ibl.pllConfig[i].doEnable = swap16val (ibl.pllConfig[i].doEnable);
132 ibl.pllConfig[i].prediv = swap32val (ibl.pllConfig[i].prediv);
133 ibl.pllConfig[i].mult = swap32val (ibl.pllConfig[i].mult);
134 ibl.pllConfig[i].postdiv = swap32val (ibl.pllConfig[i].postdiv);
135 ibl.pllConfig[i].pllOutFreqMhz = swap32val (ibl.pllConfig[i].pllOutFreqMhz);
136 }
138 ibl.ddrConfig.configDdr = swap16val (ibl.ddrConfig.configDdr);
140 ibl.ddrConfig.uEmif.emif3p1.sdcfg = swap32val(ibl.ddrConfig.uEmif.emif3p1.sdcfg);
141 ibl.ddrConfig.uEmif.emif3p1.sdrfc = swap32val(ibl.ddrConfig.uEmif.emif3p1.sdrfc);
142 ibl.ddrConfig.uEmif.emif3p1.sdtim1 = swap32val(ibl.ddrConfig.uEmif.emif3p1.sdtim1);
143 ibl.ddrConfig.uEmif.emif3p1.sdtim2 = swap32val(ibl.ddrConfig.uEmif.emif3p1.sdtim2);
144 ibl.ddrConfig.uEmif.emif3p1.dmcctl = swap32val(ibl.ddrConfig.uEmif.emif3p1.dmcctl);
146 for (i = 0; i < ibl_N_ETH_PORTS; i++) {
147 ibl.ethConfig[i].ethPriority = swap32val (ibl.ethConfig[i].ethPriority);
148 ibl.ethConfig[i].port = swap32val (ibl.ethConfig[i].port);
149 ibl.ethConfig[i].doBootp = swap16val (ibl.ethConfig[i].doBootp);
150 ibl.ethConfig[i].useBootpServerIp = swap16val (ibl.ethConfig[i].useBootpServerIp);
151 ibl.ethConfig[i].useBootpFileName = swap16val (ibl.ethConfig[i].useBootpFileName);
152 ibl.ethConfig[i].bootFormat = swap32val (ibl.ethConfig[i].bootFormat);
153 ibl.ethConfig[i].blob.startAddress = swap32val (ibl.ethConfig[i].blob.startAddress);
154 ibl.ethConfig[i].blob.sizeBytes = swap32val (ibl.ethConfig[i].blob.sizeBytes);
155 ibl.ethConfig[i].blob.branchAddress = swap32val (ibl.ethConfig[i].blob.branchAddress);
157 ibl.sgmiiConfig[i].adviseAbility = swap32val (ibl.sgmiiConfig[i].adviseAbility);
158 ibl.sgmiiConfig[i].control = swap32val (ibl.sgmiiConfig[i].control);
159 ibl.sgmiiConfig[i].txConfig = swap32val (ibl.sgmiiConfig[i].txConfig);
160 ibl.sgmiiConfig[i].rxConfig = swap32val (ibl.sgmiiConfig[i].rxConfig);
161 ibl.sgmiiConfig[i].auxConfig = swap32val (ibl.sgmiiConfig[i].auxConfig);
162 }
164 ibl.nandConfig.nandPriority = swap32val (ibl.nandConfig.nandPriority);
165 ibl.nandConfig.bootFormat = swap32val (ibl.nandConfig.bootFormat);
166 ibl.nandConfig.blob.startAddress = swap32val (ibl.nandConfig.blob.startAddress);
167 ibl.nandConfig.blob.sizeBytes = swap32val (ibl.nandConfig.blob.sizeBytes);
168 ibl.nandConfig.blob.branchAddress = swap32val (ibl.nandConfig.blob.branchAddress);
170 ibl.nandConfig.nandInfo.busWidthBits = swap32val (ibl.nandConfig.nandInfo.busWidthBits);
171 ibl.nandConfig.nandInfo.pageSizeBytes = swap32val (ibl.nandConfig.nandInfo.pageSizeBytes);
172 ibl.nandConfig.nandInfo.pageEccBytes = swap32val (ibl.nandConfig.nandInfo.pageEccBytes);
173 ibl.nandConfig.nandInfo.pagesPerBlock = swap32val (ibl.nandConfig.nandInfo.pagesPerBlock);
174 ibl.nandConfig.nandInfo.totalBlocks = swap32val (ibl.nandConfig.nandInfo.totalBlocks);
175 ibl.nandConfig.nandInfo.addressBytes = swap32val (ibl.nandConfig.nandInfo.addressBytes);
176 ibl.nandConfig.nandInfo.lsbFirst = swap16val (ibl.nandConfig.nandInfo.lsbFirst);
177 ibl.nandConfig.nandInfo.blockOffset = swap32val (ibl.nandConfig.nandInfo.blockOffset);
178 ibl.nandConfig.nandInfo.pageOffset = swap32val (ibl.nandConfig.nandInfo.pageOffset);
179 ibl.nandConfig.nandInfo.columnOffset = swap32val (ibl.nandConfig.nandInfo.columnOffset);
180 ibl.nandConfig.nandInfo.postCommand = swap16val (ibl.nandConfig.nandInfo.postCommand);
182 ibl.chkSum = swap16val (ibl.chkSum);
183 }
187 /**
188 * @brief
189 * The i2c load context consists of the address of the next block
190 * to read, and a simple fifo holding any existing data.
191 */
192 #define I2C_MAX_BLOCK_SIZE 0x80
193 uint32 i2cReadAddress;
195 uint32 i2cFifoIn = 0;
196 uint32 i2cFifoOut = 0;
197 uint8 i2cData[I2C_MAX_BLOCK_SIZE];
198 uint16 i2cSum[I2C_MAX_BLOCK_SIZE >> 1];
201 /**
202 * @brief
203 * Return the number of elements in the fifo
204 */
205 Uint32 i2cFifoCount (void)
206 {
207 Int32 count;
209 if (i2cFifoIn >= i2cFifoOut)
210 count = i2cFifoIn - i2cFifoOut;
211 else
212 count = i2cFifoIn + I2C_MAX_BLOCK_SIZE - i2cFifoOut;
214 return (count);
216 }
219 /**
220 * @brief
221 * Read a byte from the fifo
222 */
223 Uint8 i2cFifoRead(void)
224 {
225 Uint8 v;
227 v = i2cData[i2cFifoOut];
229 i2cFifoOut += 1;
231 if (i2cFifoOut == i2cFifoIn)
232 i2cFifoOut = i2cFifoIn = 0;
234 if (i2cFifoOut >= I2C_MAX_BLOCK_SIZE)
235 i2cFifoOut = 0;
237 return (v);
239 }
241 /**
242 * @brief
243 * Read a block of data from the I2C eeprom and put it in the fifo
244 */
245 void i2cReadBlock (void)
246 {
247 uint16 len;
248 int32 i, j;
249 uint32 v;
251 for (;;) {
252 while (hwI2cMasterRead (i2cReadAddress & 0xffff, /* The address on the eeprom of the table */
253 4, /* The number of bytes to read */
254 i2cData, /* Where to store the bytes */
255 i2cReadAddress >> 16, /* The bus address of the eeprom */
256 IBL_I2C_CFG_ADDR_DELAY) /* The delay between sending the address and reading data */
258 != I2C_RET_OK) {
260 iblStatus.i2cDataRetries += 1;
261 }
263 /* Form the length. The received bytes are always in big endian format */
264 len = (i2cData[0] << 8) | i2cData[1];
267 if (len > I2C_MAX_BLOCK_SIZE)
268 continue;
271 while (hwI2cMasterRead (i2cReadAddress & 0xffff, /* The address on the eeprom of the table */
272 len, /* The number of bytes to read */
273 i2cData, /* Where to store the bytes */
274 i2cReadAddress >> 16, /* The bus address of the eeprom */
275 IBL_I2C_CFG_ADDR_DELAY) /* The delay between sending the address and reading data */
277 != I2C_RET_OK) {
279 iblStatus.i2cDataRetries += 1;
280 }
283 /* Must do endian conversion to verify the checksum */
284 for (i = j = 0; i < len; i += 2, j += 1)
285 i2cSum[j] = (i2cData[i+0] << 8) | i2cData[i+1];
287 v = onesComplementChksum (i2cSum, j);
288 if ((v == 0) || (v == 0xffff))
289 break;
292 iblStatus.i2cDataRetries += 1;
294 }
297 i2cReadAddress += len;
299 i2cFifoIn = len;
300 i2cFifoOut = 4; /* The i2c header is effectively removed */
302 }
307 /**
308 * @brief
309 * Read data from the I2C to pass to the interpreter
310 */
311 Int32 iblI2cRead (Uint8 *buf, Uint32 num_bytes)
312 {
313 int i;
315 for (i = 0; i < num_bytes; i++) {
317 if (i2cFifoCount() == 0)
318 i2cReadBlock ();
320 buf[i] = i2cFifoRead();
321 }
323 return (0);
325 }
329 /**
330 * @brief
331 * The module function table used for boot from i2c
332 */
333 BOOT_MODULE_FXN_TABLE i2cinit_boot_module =
334 {
335 NULL, /* Open API */
336 NULL, /* Close API */
337 iblI2cRead, /* Read API */
338 NULL, /* Write API */
339 NULL, /* Peek API */
340 NULL, /* Seek API */
341 NULL /* Query API */
342 };
346 /**
347 * @brief
348 * The main function
349 *
350 * @details
351 * The ibl configuration parameters are read from the i2c,
352 * followed by the i2c mapping information. The second stage
353 * of the IBL is then loaded, and execution transferred
354 * to the second stage.
355 */
356 void main (void)
357 {
359 uint16 v;
360 uint32 entry;
361 void (*exit)();
362 iblI2cMap_t map;
364 memset (&iblStatus, 0, sizeof(iblStatus_t));
365 iblStatus.iblMagic = ibl_MAGIC_VALUE;
367 /* Read the endianness setting of the device */
368 littleEndian = deviceIsLittleEndian();
370 /* Load the default configuration table from the i2c. The actual speed of the device
371 * isn't really known here, since it is part of the table, so a compile time
372 * value is used (the pll may have been configured during the initial load) */
373 hwI2Cinit (IBL_I2C_DEV_FREQ_MHZ, /* The CPU frequency during I2C data load */
374 DEVICE_I2C_MODULE_DIVISOR, /* The divide down of CPU that drives the i2c */
375 IBL_I2C_CLK_FREQ_KHZ, /* The I2C data rate used during table load */
376 IBL_I2C_OWN_ADDR); /* The address used by this device on the i2c bus */
379 /* Read the i2c configuration tables until the checksum passes and the magic number
380 * matches. The checksum must be verified before the endian re-ordering is done */
381 for (;;) {
383 if (hwI2cMasterRead (IBL_I2C_CFG_TABLE_DATA_ADDR, /* The address on the eeprom of the table */
384 sizeof(ibl_t), /* The number of bytes to read */
385 (UINT8 *)&ibl, /* Where to store the bytes */
386 IBL_I2C_CFG_EEPROM_BUS_ADDR, /* The bus address of the eeprom */
387 IBL_I2C_CFG_ADDR_DELAY) /* The delay between sending the address and reading data */
389 == I2C_RET_OK) {
391 if (ibl.chkSum != 0) {
393 v = onesComplementChksum ((UINT16 *)&ibl, sizeof(ibl_t) / sizeof(UINT16));
394 if ((v != 0) && (v != 0xffff)) {
395 iblStatus.i2cRetries += 1;
396 continue;
397 }
399 }
402 if (ibl.iblMagic == ibl_MAGIC_VALUE)
403 break;
405 if (swap32val (ibl.iblMagic) == ibl_MAGIC_VALUE) {
406 iblSwap ();
407 break;
408 }
410 iblStatus.magicRetries += 1;
412 }
414 iblStatus.i2cRetries += 1;
415 }
417 /* Pll configuration is device specific */
418 devicePllConfig ();
420 /* The IBL table is in place. Read the I2C map information from the eeprom */
421 for (;;) {
422 if (hwI2cMasterRead (IBL_I2C_MAP_TABLE_DATA_ADDR, /* The address on the eeprom of the data mapping */
423 sizeof(iblI2cMap_t), /* The number of bytes to read */
424 (UINT8 *)&map, /* Where to store the bytes */
425 IBL_I2C_CFG_EEPROM_BUS_ADDR, /* The bus address of the eeprom */
426 IBL_I2C_CFG_ADDR_DELAY) /* The delay between sending the address and reading data */
428 == I2C_RET_OK) {
430 /* On the I2C EEPROM the table is always formatted with the most significant
431 * byte first. So if the device is running little endain the endian must be
432 * swapped */
433 if (littleEndian == TRUE) {
434 map.length = swap16val (map.length);
435 map.chkSum = swap16val (map.chkSum);
436 map.addrLe = swap32val (map.addrLe);
437 map.addrBe = swap32val (map.addrBe);
438 }
440 if (map.length != sizeof(iblI2cMap_t)) {
441 iblStatus.mapSizeFail += 1;
442 continue;
443 }
445 if (map.chkSum != 0) {
447 v = onesComplementChksum ((UINT16 *)&map, sizeof(iblI2cMap_t));
448 if ((v != 0) && (v != 0xffff)) {
449 iblStatus.mapRetries += 1;
450 continue;
451 }
452 }
454 break;
455 }
457 iblStatus.mapRetries += 1;
459 }
462 /* The rest of the IBL is in boot table format. Read and process the data */
463 if (littleEndian == TRUE)
464 i2cReadAddress = map.addrLe;
465 else
466 i2cReadAddress = map.addrBe;
468 if (i2cReadAddress == 0xffffffff) {
469 iblStatus.iblFail = ibl_FAIL_CODE_INVALID_I2C_ADDRESS;
470 for (;;);
471 }
474 /* Pass control to the boot table processor */
475 iblBootBtbl (&i2cinit_boot_module, &entry);
477 if (btblWrapEcode != 0) {
478 iblStatus.iblFail = ibl_FAIL_CODE_BTBL_FAIL;
479 for (;;);
480 }
482 /* jump to the exit point, which will be the entry point for the full IBL */
483 exit = (void (*)())entry;
484 (*exit)();
487 }