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 */
36 #include "ibl.h"
37 #include "iblloc.h"
38 #include "iblcfg.h"
39 #include "device.h"
40 #include "iblbtbl.h"
41 #include "spi_api.h"
42 #include "iblinit.h"
43 #include <string.h>
46 /**
47 * @brief
48 * The next read address on the SPI flash is stored in a global for
49 * access through the boot call chain.
50 */
51 uint32 spiReadAddress;
53 /**
54 * @brief
55 * Read a block of data from the SPI eeprom and put it in the fifo
56 */
57 void spiReadBlock (void)
58 {
59 uint16 len;
60 int32 i, j;
61 uint32 v;
63 for (;;) {
64 while (hwSpiRead (spiReadAddress, /* The address on the eeprom of the table */
65 4, /* The number of bytes to read */
66 iData) /* Where to store the bytes */
68 != 0) {
70 iblStatus.spiDataRetries += 1;
71 }
73 /* Form the length. The received bytes are always in big endian format */
74 len = (iData[0] << 8) | iData[1];
77 if (len > I_MAX_BLOCK_SIZE)
78 continue;
81 while (hwSpiRead (spiReadAddress, /* The address on the eeprom of the table */
82 len, /* The number of bytes to read */
83 iData) /* Where to store the bytes */
85 != 0) {
87 iblStatus.spiDataRetries += 1;
88 }
91 /* Must do endian conversion to verify the checksum */
92 for (i = j = 0; i < len; i += 2, j += 1)
93 iSum[j] = (iData[i+0] << 8) | iData[i+1];
95 v = onesComplementChksum (iSum, j);
96 if ((v == 0) || (v == 0xffff))
97 break;
100 iblStatus.spiDataRetries += 1;
102 }
105 spiReadAddress += len;
107 iFifoIn = len;
108 iFifoOut = 4; /* The spi header is effectively removed */
110 }
114 /**
115 * @brief
116 * Read data from the SPI to pass to the interpreter
117 */
118 Int32 iblSpiRead (Uint8 *buf, Uint32 num_bytes)
119 {
120 int i;
122 for (i = 0; i < num_bytes; i++) {
124 if (iFifoCount() == 0)
125 spiReadBlock ();
127 buf[i] = iFifoRead();
128 }
130 return (0);
132 }
135 /**
136 * @brief
137 * The module function table used for boot from spi
138 */
139 BOOT_MODULE_FXN_TABLE spiinit_boot_module =
140 {
141 NULL, /* Open API */
142 NULL, /* Close API */
143 iblSpiRead, /* Read API */
144 NULL, /* Write API */
145 NULL, /* Peek API */
146 NULL, /* Seek API */
147 NULL /* Query API */
148 };
153 /**
154 * @brief
155 * Configure the SPI, then read the parameters from the SPI and
156 * pass them to the second stage boot
157 */
158 BOOT_MODULE_FXN_TABLE *iblInitSpiNor (void)
159 {
160 iblBootMap_t map;
161 spiConfig_t cfg;
162 uint32 configAddr;
164 bool littleEndian;
165 uint16 v;
167 /* Read the endianness of the device */
168 littleEndian = deviceIsLittleEndian();
170 /* Load the default configuration table from the SPI. The actual speed of
171 * the device isn't really known here, since it is part of the table, so a
172 * compile time value is used (the pll may have been configured during the initial load) */
173 deviceLoadInitSpiConfig ((void *)&cfg);
175 if (hwSpiConfig (&cfg) != 0) {
177 iblStatus.iblFail = ibl_FAIL_CODE_SPI_PARAMS;
178 for (;;);
180 }
183 /* Read the SPI mapping information from the nor flash */
184 for (;;) {
186 if (hwSpiRead (IBL_CFG_SPI_MAP_TABLE_DATA_ADDR, /* The address on the flash of the data mapping */
187 sizeof(iblBootMap_t), /* The number of bytes to read */
188 (UINT8 *)&map) /* Where to store the data */
190 == 0) {
192 /* On the flash the table is always formatted with the most significant
193 * byte first. So if the device is running little endian, the endian
194 * must be swapped */
195 if (littleEndian == TRUE) {
196 map.length = swap16val (map.length);
197 map.chkSum = swap16val (map.chkSum);
198 map.addrLe = swap32val (map.addrLe);
199 map.configLe = swap32val (map.configLe);
200 map.addrBe = swap32val (map.addrBe);
201 map.configBe = swap32val (map.configBe);
203 configAddr = map.configLe;
204 spiReadAddress = map.addrLe;
206 } else {
208 configAddr = map.configBe;
209 spiReadAddress = map.addrBe;
211 }
214 if (map.length != sizeof(iblBootMap_t)) {
215 iblStatus.mapSizeFail += 1;
216 continue;
217 }
219 if (map.chkSum != 0) {
221 v = onesComplementChksum ((UINT16 *)&map, sizeof(iblBootMap_t));
222 if ((v != 0) && (v != 0xffff)) {
223 iblStatus.mapRetries += 1;
224 continue;
225 }
226 }
228 break;
230 }
232 iblStatus.mapRetries += 1;
234 }
237 /* Read the SPI configuration tables until the checksum passes and the magic
238 * number matches. The checksum must be verified before the endian re-ordering */
239 for (;;) {
241 if (hwSpiRead (configAddr, /* The address on the flash of the table */
242 sizeof(ibl_t), /* The number of bytes to read */
243 (UINT8 *)&ibl) /* Where to store the bytes */
244 == 0) {
246 if (ibl.chkSum != 0) {
248 v = onesComplementChksum ((UINT16 *)&ibl, sizeof(ibl_t) / sizeof(UINT16));
249 if ((v != 0) && (v != 0xffff)) {
250 iblStatus.spiRetries += 1;
251 continue;
252 }
253 }
255 if (ibl.iblMagic == ibl_MAGIC_VALUE)
256 break;
258 if (swap32val (ibl.iblMagic) == ibl_MAGIC_VALUE) {
259 iblSwap ();
260 break;
261 }
263 iblStatus.magicRetries += 1;
265 }
267 iblStatus.spiRetries += 1;
269 }
272 /* the rest of the IBL is in boot table format. Read and process the data */
273 if (spiReadAddress == 0xffffffff) {
274 iblStatus.iblFail = ibl_FAIL_CODE_INVALID_SPI_ADDRESS;
275 for (;;);
276 }
278 return (&spiinit_boot_module);
280 }