| 1 | /*************************************************************** |
|---|
| 2 | ** |
|---|
| 3 | ** Broadcom Corp. Confidential |
|---|
| 4 | ** Copyright 1998-2000 Broadcom Corp. All Rights Reserved. |
|---|
| 5 | ** |
|---|
| 6 | ** THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED |
|---|
| 7 | ** SOFTWARE LICENSE AGREEMENT BETWEEN THE USER AND BROADCOM. |
|---|
| 8 | ** YOU HAVE NO RIGHT TO USE OR EXPLOIT THIS MATERIAL EXCEPT |
|---|
| 9 | ** SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 10 | ** |
|---|
| 11 | ** File: fstore.c |
|---|
| 12 | ** Description: Flash storage manager |
|---|
| 13 | ** |
|---|
| 14 | ****************************************************************/ |
|---|
| 15 | /* include files */ |
|---|
| 16 | #include "fstore.h" |
|---|
| 17 | #include "ministd.h" |
|---|
| 18 | #include "bstd.h" |
|---|
| 19 | #include "bspi_flash.h" |
|---|
| 20 | #if (BCHP_CHIP==7550) ||(BCHP_CHIP==7552) |
|---|
| 21 | #include "bchp_hif_mspi.h" |
|---|
| 22 | #include "bcm_mips_defs.h" |
|---|
| 23 | #endif |
|---|
| 24 | #include "bchp_sun_top_ctrl.h" |
|---|
| 25 | |
|---|
| 26 | BDBG_MODULE(fstore); /* Register software module with debug interface */ |
|---|
| 27 | |
|---|
| 28 | unsigned int ReadReg32(unsigned int offset); |
|---|
| 29 | void WriteReg32(unsigned int offset, unsigned int value); |
|---|
| 30 | |
|---|
| 31 | #define FSTORE_VERSION 0xFC1A |
|---|
| 32 | #define FSTORE_VALID 0xBC07 |
|---|
| 33 | |
|---|
| 34 | typedef enum fs_flash_type_t |
|---|
| 35 | { |
|---|
| 36 | eFS_SP25_BSPI, /* Spansion serial flash on BSPI bus */ |
|---|
| 37 | eFS_MX25_BSPI, /* Micronix serial flash on BSPI bus */ |
|---|
| 38 | eFS_TYPE_MAX |
|---|
| 39 | }fs_flash_type_t; |
|---|
| 40 | |
|---|
| 41 | typedef struct fstore_record_t |
|---|
| 42 | { |
|---|
| 43 | unsigned short version; /* Version number of data stored, if it does not match expected value use defaults */ |
|---|
| 44 | unsigned short valid; /* Pattern indicating data is valid 0xBC07 */ |
|---|
| 45 | unsigned int length; /* Length in bytes of the record */ |
|---|
| 46 | unsigned int checksum; /* 32bit checksum */ |
|---|
| 47 | }fstore_record_t; |
|---|
| 48 | |
|---|
| 49 | static bresult fstore_read(void *p_fs, unsigned int offset, |
|---|
| 50 | unsigned char *data, unsigned int size); |
|---|
| 51 | //static bresult fstore_read_spi(void *p_fs, unsigned int offset, unsigned char *data, unsigned int size); |
|---|
| 52 | static bresult fstore_erase_spi(void *p_fs, unsigned int offset, unsigned int size); |
|---|
| 53 | static bresult fstore_prog_spi(void *p_fs, unsigned int offset,void *buffer, unsigned int size); |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | /* IMPORTANT align app flash sector on sector boundary */ |
|---|
| 57 | static fstore_t s_fstore[eFS_TYPE_MAX] = |
|---|
| 58 | { |
|---|
| 59 | #if (BCHP_CHIP==7550) ||(BCHP_CHIP==7552) |
|---|
| 60 | /* read_base,write_base, {boot offset,init offset,app offset},sector_size,block_size,max_size */ |
|---|
| 61 | /* more room for the binary with whole applicaiton in the flash loader offset */ |
|---|
| 62 | /* we don't have patch section in flash so set it same as APP_DATA */ |
|---|
| 63 | {0xBFC00000, 0x00000000, { 0, (2 * 1024 * 1024), (2 * 1024 * 1024)}, (4 * 1024), FLASH_SPI_MAX_PAGE_SIZE, (64 * 1024),fstore_read, fstore_erase_spi,fstore_prog_spi},/* eFS_S25FL129P (Model 00) */ |
|---|
| 64 | {0xBFC00000, 0x00000000, { 0, (2 * 1024 * 1024), (2 * 1024 * 1024)}, (64 * 1024),FLASH_SPI_MAX_PAGE_SIZE, (64 * 1024),fstore_read, fstore_erase_spi,fstore_prog_spi},/* eFS_MX25_SPI */ |
|---|
| 65 | #else |
|---|
| 66 | /* read_base,write_base, {boot offset,init offset,app offset},sector_size,block_size,max_size */ |
|---|
| 67 | {0xBFC00000,0x00000000, { 0, (640 * 1024), (3456 * 1024)}, (4 * 1024) , 4,(64 * 1024),fstore_read, fstore_erase_spi,fstore_prog_spi},/* eFS_MX25_SPI */ |
|---|
| 68 | {0xBFC00000,0x00000000, { 0, (640 * 1024), (3456 * 1024)}, (64 * 1024) ,4,(64 * 1024),fstore_read, fstore_erase_spi,fstore_prog_spi},/* eFS_MX25_SPI */ |
|---|
| 69 | #endif |
|---|
| 70 | }; |
|---|
| 71 | |
|---|
| 72 | #if (BCHP_CHIP==7550) ||(BCHP_CHIP==7552) |
|---|
| 73 | /* |
|---|
| 74 | Summary: |
|---|
| 75 | get flash read address (taking care of size > 4M |
|---|
| 76 | */ |
|---|
| 77 | static unsigned char *fstore_get_read_addr(void *p_v, fstore_data_type_t type, unsigned int size) |
|---|
| 78 | { |
|---|
| 79 | fstore_t *p_fs = (fstore_t *)p_v; |
|---|
| 80 | uint32_t offset = p_fs->offset[type]; |
|---|
| 81 | |
|---|
| 82 | /*TODO, handle address cross 4M boundry case */ |
|---|
| 83 | if (offset < 0x00400000) |
|---|
| 84 | { |
|---|
| 85 | if ((offset + size) > 0x00400000) |
|---|
| 86 | printf("%s: address cross 4M boundry\n", __func__); |
|---|
| 87 | offset += p_fs->read_base; |
|---|
| 88 | } |
|---|
| 89 | else { |
|---|
| 90 | /* check size to make sure not exceed 16M */ |
|---|
| 91 | offset = BCM_PHYS_TO_KSEG1(0x20000000 - 16*1024*1024) + (offset - 0x00400000); |
|---|
| 92 | } |
|---|
| 93 | return (unsigned char *)offset; |
|---|
| 94 | } |
|---|
| 95 | #endif |
|---|
| 96 | |
|---|
| 97 | /* |
|---|
| 98 | Summary: |
|---|
| 99 | Calculate 32 bit checksum |
|---|
| 100 | */ |
|---|
| 101 | static inline unsigned int fstore_checksum(unsigned int *addr, unsigned int len) |
|---|
| 102 | { |
|---|
| 103 | unsigned int checksum = 0; |
|---|
| 104 | unsigned int idx; |
|---|
| 105 | BDBG_MSG(("Calc checksum for array @ 0x%08x, %d bytes (0x%08x)",addr,len,addr[0] )); |
|---|
| 106 | |
|---|
| 107 | for (idx = 0; idx < len/sizeof(unsigned int); ++idx) |
|---|
| 108 | { |
|---|
| 109 | checksum += addr[idx]; |
|---|
| 110 | } |
|---|
| 111 | BDBG_MSG(("0x%08x\n",checksum )); |
|---|
| 112 | return checksum; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | /* |
|---|
| 116 | Summary: |
|---|
| 117 | Read data from flash into the buffer. |
|---|
| 118 | */ |
|---|
| 119 | |
|---|
| 120 | static bresult fstore_read(void *p_v, unsigned int offset, |
|---|
| 121 | unsigned char *data, unsigned int size) |
|---|
| 122 | { |
|---|
| 123 | fstore_t *p_fs = (fstore_t *)p_v; |
|---|
| 124 | #if (BCHP_CHIP==7550) ||(BCHP_CHIP==7552) |
|---|
| 125 | mspi_enable_bspi(); |
|---|
| 126 | if (offset < 0x00400000) |
|---|
| 127 | { |
|---|
| 128 | if ((offset + size) > 0x00400000) { |
|---|
| 129 | memcpy(data,(unsigned char*)(p_fs->read_base + offset), (0x00400000 - offset)); |
|---|
| 130 | data += (offset + size) - 0x00400000; |
|---|
| 131 | size = (offset + size) - 0x00400000; |
|---|
| 132 | offset = BCM_PHYS_TO_KSEG1(0x20000000 - 16*1024*1024); |
|---|
| 133 | } |
|---|
| 134 | else |
|---|
| 135 | offset += p_fs->read_base; |
|---|
| 136 | } |
|---|
| 137 | else { |
|---|
| 138 | /* check size to make sure not exceed 16M */ |
|---|
| 139 | offset = BCM_PHYS_TO_KSEG1(0x20000000 - 16*1024*1024) + (offset - 0x00400000); |
|---|
| 140 | } |
|---|
| 141 | memcpy(data,(unsigned char*)offset,size); |
|---|
| 142 | #else |
|---|
| 143 | /* max 4M */ |
|---|
| 144 | memcpy(data,(unsigned char*)(p_fs->read_base + offset),size); |
|---|
| 145 | #endif |
|---|
| 146 | return b_ok; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | /* |
|---|
| 150 | Summary: |
|---|
| 151 | Read data from flash into the buffer. |
|---|
| 152 | */ |
|---|
| 153 | #if 0 |
|---|
| 154 | static bresult fstore_read_spi(void *p_v, unsigned int offset, |
|---|
| 155 | unsigned char *data, unsigned int size) |
|---|
| 156 | { |
|---|
| 157 | int i; |
|---|
| 158 | fstore_t *p_fs = (fstore_t *)p_v; |
|---|
| 159 | for (i = 0; i < size; ++i) |
|---|
| 160 | { |
|---|
| 161 | data[i] = 0; |
|---|
| 162 | if (bspi_read(0,p_fs->read_base + offset + i,&(data[i])) != b_ok) |
|---|
| 163 | { |
|---|
| 164 | BDBG_MSG(("%s:%d(0x%08x,0x%02x,%d)\n",__FUNCTION__,__LINE__,offset+i,data[i],1 )); |
|---|
| 165 | return berr_not_supported; |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | return b_ok; |
|---|
| 169 | } |
|---|
| 170 | #endif |
|---|
| 171 | |
|---|
| 172 | /* |
|---|
| 173 | Summary: |
|---|
| 174 | Calculate 32 bit checksum |
|---|
| 175 | */ |
|---|
| 176 | #define TS_READ_32( buf ) ((uint32_t)((buf)[0]<<24|(buf)[1]<<16|(buf)[2]<<8|(buf)[3])) |
|---|
| 177 | static inline unsigned int fstore_checksum_flash(fstore_t *p_fs, unsigned int offset, unsigned int len) |
|---|
| 178 | { |
|---|
| 179 | unsigned int checksum = 0; |
|---|
| 180 | unsigned int val; |
|---|
| 181 | unsigned int idx; |
|---|
| 182 | BDBG_MSG(("Calc checksum for array @ 0x%08x, %d bytes",offset,len )); |
|---|
| 183 | |
|---|
| 184 | for (idx = 0; idx < len/sizeof(unsigned int); ++idx) |
|---|
| 185 | { |
|---|
| 186 | if (p_fs->read(p_fs, offset + (idx * 4),(unsigned char*) &val, 4) != b_ok) |
|---|
| 187 | return 0; |
|---|
| 188 | |
|---|
| 189 | checksum += val; |
|---|
| 190 | } |
|---|
| 191 | BDBG_MSG(("0x%08x\n",checksum )); |
|---|
| 192 | return checksum; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | /* |
|---|
| 196 | Summary: |
|---|
| 197 | Erase the flash at the given offset. Offset is in bytes |
|---|
| 198 | but must be aligned to a word boundary. Size is in bytes but must be |
|---|
| 199 | sector aligned. It will erase all data in sector even if size |
|---|
| 200 | is less than a sector in length. |
|---|
| 201 | */ |
|---|
| 202 | |
|---|
| 203 | bresult fstore_erase_spi(void *p_v, unsigned int offset, unsigned int size) |
|---|
| 204 | { |
|---|
| 205 | fstore_t *p_fs = (fstore_t *)p_v; |
|---|
| 206 | int i,scnt; |
|---|
| 207 | bresult result = b_ok; |
|---|
| 208 | scnt = size/p_fs->sector_size; |
|---|
| 209 | if (size % p_fs->sector_size) |
|---|
| 210 | scnt++; |
|---|
| 211 | |
|---|
| 212 | BDBG_MSG(("%s %d sectors\n",__FUNCTION__,scnt )); |
|---|
| 213 | |
|---|
| 214 | #if (BCHP_CHIP==7550) ||(BCHP_CHIP==7552) |
|---|
| 215 | mspi_disable_bspi(); |
|---|
| 216 | #endif |
|---|
| 217 | for (i = 0; i < scnt; ++i) |
|---|
| 218 | { |
|---|
| 219 | BDBG_MSG(("%s sector %d of %d\n",__FUNCTION__,i,scnt )); |
|---|
| 220 | /* Erase the header sector */ |
|---|
| 221 | result = bspi_sector_erase(0,p_fs->write_base + offset + (i * p_fs->sector_size)); |
|---|
| 222 | if (b_ok != result) |
|---|
| 223 | break; |
|---|
| 224 | } |
|---|
| 225 | #if (BCHP_CHIP==7550) ||(BCHP_CHIP==7552) |
|---|
| 226 | mspi_enable_bspi(); |
|---|
| 227 | #endif |
|---|
| 228 | if (b_ok == result) |
|---|
| 229 | BDBG_MSG(("%s done\n",__FUNCTION__)); |
|---|
| 230 | else |
|---|
| 231 | BDBG_ERR(("%s failed\n",__FUNCTION__)); |
|---|
| 232 | return result; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | /* |
|---|
| 236 | Summary: |
|---|
| 237 | Program the buffer into flash, flash must have been erased before calling this function. |
|---|
| 238 | Offset and size are in bytes but must be aligned to word boundries. |
|---|
| 239 | */ |
|---|
| 240 | |
|---|
| 241 | bresult fstore_prog_spi(void *p_v, unsigned int offset,void *buffer, unsigned int size) |
|---|
| 242 | { |
|---|
| 243 | int i,towrite,j = 0; |
|---|
| 244 | unsigned char *data_buf = (unsigned char*)buffer; |
|---|
| 245 | fstore_t *p_fs = (fstore_t *)p_v; |
|---|
| 246 | bresult result; |
|---|
| 247 | |
|---|
| 248 | BDBG_MSG(("%s(0x%08x,%d)\n",__FUNCTION__, offset,size)); |
|---|
| 249 | |
|---|
| 250 | #if (BCHP_CHIP==7550) ||(BCHP_CHIP==7552) |
|---|
| 251 | mspi_disable_bspi(); |
|---|
| 252 | #endif |
|---|
| 253 | |
|---|
| 254 | for (i = 0; i < size; i += towrite) |
|---|
| 255 | { |
|---|
| 256 | if (i + p_fs->block_size >= size) |
|---|
| 257 | { |
|---|
| 258 | towrite = size - i; |
|---|
| 259 | BDBG_MSG(("%s:%d(%d, %d, %d, %d) \n",__FUNCTION__,__LINE__,i,p_fs->block_size,size,towrite)); |
|---|
| 260 | } else |
|---|
| 261 | { |
|---|
| 262 | towrite = p_fs->block_size; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | #if (BCHP_CHIP==7550) ||(BCHP_CHIP==7552) |
|---|
| 266 | /* to prevent address cross the page boundary of 256 bytes */ |
|---|
| 267 | towrite = towrite <= (256 - (i&0xFF)) ? towrite : (256 - (i&0xFF)); |
|---|
| 268 | #endif |
|---|
| 269 | |
|---|
| 270 | if ((result = bspi_page_program(0,p_fs->write_base + offset + i,&data_buf[i],towrite)) != b_ok) |
|---|
| 271 | { |
|---|
| 272 | BDBG_ERR(("page program failed at %d \n",i,result)); |
|---|
| 273 | #if (BCHP_CHIP==7550) ||(BCHP_CHIP==7552) |
|---|
| 274 | mspi_enable_bspi(); |
|---|
| 275 | #endif |
|---|
| 276 | return result; |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | #if (BCHP_CHIP==7550) ||(BCHP_CHIP==7552) |
|---|
| 280 | if ((i % 4096) == 0) |
|---|
| 281 | #else |
|---|
| 282 | if ((i % 512) == 0) |
|---|
| 283 | #endif |
|---|
| 284 | { |
|---|
| 285 | //bos_sleep(1); |
|---|
| 286 | if (0 == (j++ % 2)) |
|---|
| 287 | printf(("+\n")); |
|---|
| 288 | else |
|---|
| 289 | printf(("-\n")); |
|---|
| 290 | } |
|---|
| 291 | } |
|---|
| 292 | #if (BCHP_CHIP==7550) ||(BCHP_CHIP==7552) |
|---|
| 293 | mspi_enable_bspi(); |
|---|
| 294 | /* just verify first 4M */ |
|---|
| 295 | if (offset > 0x00400000) |
|---|
| 296 | return b_ok; |
|---|
| 297 | if ((offset + size) > 0x00400000) |
|---|
| 298 | size = (0x00400000 - offset); |
|---|
| 299 | #endif |
|---|
| 300 | |
|---|
| 301 | #ifdef FSTORE_VALIDATE |
|---|
| 302 | for (i = 0; i < size; ++i) |
|---|
| 303 | { |
|---|
| 304 | unsigned char val; |
|---|
| 305 | val = 0; |
|---|
| 306 | if (bspi_read(0,p_fs->read_base + offset + i,&val) != b_ok) |
|---|
| 307 | { |
|---|
| 308 | BDBG_ERR(("Read check failed at %d \n",i)); |
|---|
| 309 | break; |
|---|
| 310 | } |
|---|
| 311 | if (val != data_buf[i]) |
|---|
| 312 | { |
|---|
| 313 | BDBG_ERR(("Write failed at %d (0x%02x(0x%08x) != 0x%02x)...\n",i,val,(p_fs->read_base + offset + i),data_buf[i])); |
|---|
| 314 | } |
|---|
| 315 | } |
|---|
| 316 | BDBG_ERR(("verify - done\n")); |
|---|
| 317 | #endif |
|---|
| 318 | return b_ok; |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | /* |
|---|
| 322 | Summary: |
|---|
| 323 | Initialize MSPI |
|---|
| 324 | */ |
|---|
| 325 | |
|---|
| 326 | static void mspi_init() |
|---|
| 327 | { |
|---|
| 328 | unsigned int lval; |
|---|
| 329 | #if (BCHP_CHIP == 3560) |
|---|
| 330 | lval = ReadReg32( BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_5); |
|---|
| 331 | lval &= ~(BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_5_gpio_50_MASK | |
|---|
| 332 | BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_5_gpio_51_MASK | |
|---|
| 333 | BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_5_gpio_52_MASK | |
|---|
| 334 | BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_5_gpio_53_MASK); |
|---|
| 335 | |
|---|
| 336 | lval |= (1UL << BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_5_gpio_50_SHIFT) | |
|---|
| 337 | (1UL << BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_5_gpio_51_SHIFT) | |
|---|
| 338 | (1UL << BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_5_gpio_52_SHIFT) | |
|---|
| 339 | (1UL << BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_5_gpio_53_SHIFT); |
|---|
| 340 | WriteReg32(BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_5, lval); |
|---|
| 341 | #elif (BCHP_CHIP == 3543) |
|---|
| 342 | lval = ReadReg32( BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_0); |
|---|
| 343 | lval &= ~(BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_0_spi_m_ssb_MASK | |
|---|
| 344 | BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_0_spi_m_miso_MASK | |
|---|
| 345 | BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_0_spi_m_mosi_MASK | |
|---|
| 346 | BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_0_spi_m_sck_MASK); |
|---|
| 347 | WriteReg32(BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_0, lval); |
|---|
| 348 | #elif (BCHP_CHIP == 7002) |
|---|
| 349 | lval = ReadReg32( BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_0); |
|---|
| 350 | lval &= ~(BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_0_spi_m_ssb_MASK | |
|---|
| 351 | BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_0_spi_m_miso_MASK | |
|---|
| 352 | BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_0_spi_m_mosi_MASK | |
|---|
| 353 | BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_0_spi_m_sck_MASK); |
|---|
| 354 | WriteReg32(BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_0, lval); |
|---|
| 355 | /* clear write protect on 7003 board */ |
|---|
| 356 | lval = ReadReg32(BCHP_GIO_IODIR_LO); |
|---|
| 357 | lval &= 0x7fffffff; |
|---|
| 358 | WriteReg32(BCHP_GIO_IODIR_LO, lval); |
|---|
| 359 | lval = ReadReg32(BCHP_GIO_DATA_LO); |
|---|
| 360 | lval |= 0x80000000; |
|---|
| 361 | WriteReg32(BCHP_GIO_DATA_LO, lval); |
|---|
| 362 | #elif (BCHP_CHIP == 7550) |
|---|
| 363 | lval = ReadReg32(BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_0); |
|---|
| 364 | lval &= ~(BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_0_spi_s_ssb_MASK | |
|---|
| 365 | BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_0_spi_s_miso_MASK | |
|---|
| 366 | BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_0_sf_sck_MASK | |
|---|
| 367 | BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_0_sf_mosi_MASK | |
|---|
| 368 | BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_0_sf_miso_MASK); |
|---|
| 369 | WriteReg32(BCHP_SUN_TOP_CTRL_PIN_MUX_CTRL_0, lval); |
|---|
| 370 | #endif |
|---|
| 371 | } |
|---|
| 372 | /* |
|---|
| 373 | Summary: |
|---|
| 374 | Detect the flash type and initialze the fstore structure. |
|---|
| 375 | */ |
|---|
| 376 | bresult fstore_init(fstore_t *p_fs) |
|---|
| 377 | { |
|---|
| 378 | fs_flash_type_t ftype = eFS_SP25_BSPI; /* hard code for 93560 and old Aonvision */ |
|---|
| 379 | mspi_init(); |
|---|
| 380 | |
|---|
| 381 | #if (BCHP_CHIP==7550) ||(BCHP_CHIP==7552) |
|---|
| 382 | /* check strap for boot rom */ |
|---|
| 383 | if (0 == (ReadReg32(BCHP_SUN_TOP_CTRL_STRAP_VALUE_0) & BCHP_SUN_TOP_CTRL_STRAP_VALUE_0_strap_boot_config_MASK)) { |
|---|
| 384 | /* if 32K security code is the beginning 32 K part of bootloader, if not then it will be 0xb8c08000 */ |
|---|
| 385 | /*TODO verify */ |
|---|
| 386 | s_fstore[ftype].read_base = 0xb8c00000; |
|---|
| 387 | } |
|---|
| 388 | #endif |
|---|
| 389 | memcpy(p_fs,&(s_fstore[ftype]), sizeof(fstore_t)); |
|---|
| 390 | BDBG_ERR(("%s(type = %d)\n",__FUNCTION__,ftype )); |
|---|
| 391 | |
|---|
| 392 | bspi_identify(); |
|---|
| 393 | |
|---|
| 394 | BDBG_SetModuleLevel("fstore",BDBG_eTrace); |
|---|
| 395 | // BDBG_SetModuleLevel("fstore",BDBG_eWrn); |
|---|
| 396 | |
|---|
| 397 | return b_ok; |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | /* |
|---|
| 401 | Summary: |
|---|
| 402 | Get the pointer to the flash type |
|---|
| 403 | */ |
|---|
| 404 | fstore_t *fstore_get_flash_info(void) |
|---|
| 405 | { |
|---|
| 406 | fs_flash_type_t ftype = eFS_SP25_BSPI; /* hard code for 93560 and old Aonvision */ |
|---|
| 407 | |
|---|
| 408 | return (fstore_t *)&s_fstore[ftype]; |
|---|
| 409 | } |
|---|
| 410 | |
|---|
| 411 | /* |
|---|
| 412 | Summary: |
|---|
| 413 | Get the fstore record for type |
|---|
| 414 | */ |
|---|
| 415 | static bresult fstore_get_record(fstore_t *p_fs, fstore_data_type_t type, fstore_record_t *p_record) |
|---|
| 416 | { |
|---|
| 417 | unsigned int csum; |
|---|
| 418 | bresult result; |
|---|
| 419 | |
|---|
| 420 | if ((result = p_fs->read(p_fs, p_fs->offset[type], |
|---|
| 421 | (unsigned char*) p_record,sizeof(fstore_record_t))) != b_ok) |
|---|
| 422 | return result; |
|---|
| 423 | BDBG_MSG(("p_record->version = 0x%04x",p_record->version )); |
|---|
| 424 | BDBG_MSG(("p_record->signature = 0x%04x",p_record->valid )); |
|---|
| 425 | BDBG_MSG(("p_record->length = %d",p_record->length )); |
|---|
| 426 | BDBG_MSG(("p_record->checksum = 0x%08x",p_record->checksum )); |
|---|
| 427 | |
|---|
| 428 | /* validate header */ |
|---|
| 429 | if ((p_record->version != FSTORE_VERSION) || (p_record->valid != FSTORE_VALID) || |
|---|
| 430 | (p_record->length == 0) || (p_record->length > p_fs->max_size)) |
|---|
| 431 | { |
|---|
| 432 | BDBG_WRN(("Invalid record (0x%04x,0x%04x)\n",p_record->version,p_record->valid )); |
|---|
| 433 | return berr_not_available; |
|---|
| 434 | } |
|---|
| 435 | /* Validate checksum */ |
|---|
| 436 | if ((csum = fstore_checksum_flash(p_fs, p_fs->offset[type] + sizeof(fstore_record_t), |
|---|
| 437 | p_record->length)) != p_record->checksum) |
|---|
| 438 | { |
|---|
| 439 | BDBG_WRN(("Invalid record checksum (0x%08x != 0x%08x)\n",p_record->checksum,csum )); |
|---|
| 440 | return berr_not_available; |
|---|
| 441 | } |
|---|
| 442 | |
|---|
| 443 | return b_ok; |
|---|
| 444 | } |
|---|
| 445 | /* |
|---|
| 446 | Summary: |
|---|
| 447 | Update the contents of flash. Length is in bytes |
|---|
| 448 | */ |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | bresult fstore_update(fstore_t *p_fs, |
|---|
| 452 | fstore_data_type_t type, |
|---|
| 453 | void *data, |
|---|
| 454 | unsigned int length) |
|---|
| 455 | { |
|---|
| 456 | bresult result = berr_not_supported; |
|---|
| 457 | fstore_record_t record; |
|---|
| 458 | BDBG_MSG(("fstore_update(%d,%d)\n", type,length)); |
|---|
| 459 | bos_sleep(100); |
|---|
| 460 | if (fstore_get_record(p_fs,type,&record) != b_ok) |
|---|
| 461 | { |
|---|
| 462 | BDBG_MSG(("Create new record...\n")); |
|---|
| 463 | record.version = FSTORE_VERSION; |
|---|
| 464 | record.valid = FSTORE_VALID; |
|---|
| 465 | } |
|---|
| 466 | record.length = length; |
|---|
| 467 | record.checksum = fstore_checksum(data,length); |
|---|
| 468 | |
|---|
| 469 | BDBG_MSG(("record %d\n", type)); |
|---|
| 470 | BDBG_MSG(("record.version 0x%04x\n", record.version)); |
|---|
| 471 | BDBG_MSG(("record.valid 0x%04x\n", record.valid)); |
|---|
| 472 | BDBG_MSG(("record.offset 0x%08x\n", p_fs->offset[type])); |
|---|
| 473 | BDBG_MSG(("record.length %d\n", record.length)); |
|---|
| 474 | BDBG_MSG(("record.checksum 0x%08x\n", record.checksum)); |
|---|
| 475 | |
|---|
| 476 | #if (BCHP_CHIP==7550) ||(BCHP_CHIP==7552) |
|---|
| 477 | /* only APP_DATA is used for this function */ |
|---|
| 478 | if ((eFS_APP_DATA == type) && ((length + sizeof(fstore_record_t) > p_fs->max_size))) |
|---|
| 479 | #else |
|---|
| 480 | if ((p_fs->offset[type] + length + sizeof(fstore_record_t)) > p_fs->max_size) |
|---|
| 481 | #endif |
|---|
| 482 | { |
|---|
| 483 | BDBG_ERR(("Invalid record length %d, offset = 0x%08x, maximum = 0x%08x\n",length,p_fs->offset[type],p_fs->max_size)); |
|---|
| 484 | return result; |
|---|
| 485 | } |
|---|
| 486 | |
|---|
| 487 | if (p_fs->erase(p_fs,p_fs->offset[type],length + sizeof(fstore_record_t)) != b_ok) |
|---|
| 488 | { |
|---|
| 489 | BDBG_ERR(("fstore_update erase failed\n")); |
|---|
| 490 | return result; |
|---|
| 491 | } |
|---|
| 492 | bos_sleep(100); |
|---|
| 493 | |
|---|
| 494 | if (p_fs->program(p_fs,p_fs->offset[type],&record,sizeof(fstore_record_t)) != b_ok) |
|---|
| 495 | { |
|---|
| 496 | BDBG_ERR(("fstore_update program record failed\n")); |
|---|
| 497 | return result; |
|---|
| 498 | } |
|---|
| 499 | bos_sleep(100); |
|---|
| 500 | |
|---|
| 501 | if (p_fs->program(p_fs,p_fs->offset[type] + sizeof(fstore_record_t),data,length) != b_ok) |
|---|
| 502 | { |
|---|
| 503 | BDBG_ERR(("fstore_update program failed\n")); |
|---|
| 504 | return result; |
|---|
| 505 | } |
|---|
| 506 | bos_sleep(100); |
|---|
| 507 | |
|---|
| 508 | result = b_ok; |
|---|
| 509 | return result; |
|---|
| 510 | |
|---|
| 511 | } |
|---|
| 512 | /* |
|---|
| 513 | Summary: |
|---|
| 514 | Get the stored data for type. Returns b_ok on success. |
|---|
| 515 | */ |
|---|
| 516 | bresult fstore_get(fstore_t *p_fs, |
|---|
| 517 | fstore_data_type_t type, |
|---|
| 518 | void **data, |
|---|
| 519 | unsigned int *length) |
|---|
| 520 | { |
|---|
| 521 | bresult result = berr_not_supported; |
|---|
| 522 | fstore_record_t record; |
|---|
| 523 | |
|---|
| 524 | BDBG_MSG(("fstore_get (%d)\n", type)); |
|---|
| 525 | *data = NULL; |
|---|
| 526 | *length = 0; |
|---|
| 527 | |
|---|
| 528 | if ((result = fstore_get_record(p_fs,type,&record)) != b_ok) |
|---|
| 529 | return result; |
|---|
| 530 | |
|---|
| 531 | BDBG_MSG(("record %d\n",type)); |
|---|
| 532 | BDBG_MSG(("record.version 0x%08x\n", record.version)); |
|---|
| 533 | BDBG_MSG(("record.length %d\n", record.length)); |
|---|
| 534 | BDBG_MSG(("record.checksum 0x%08x\n", record.checksum)); |
|---|
| 535 | |
|---|
| 536 | #ifdef BCM_DEBUG |
|---|
| 537 | |
|---|
| 538 | /* TEMPORARY HACK TO TEST MSPI NVM ACCESS UNTIL WE HAVE MEMORY MAPPED BSPI */ |
|---|
| 539 | if (p_fs->read_base == 0x00000000) |
|---|
| 540 | { |
|---|
| 541 | BDBG_MSG(("MEMORY LEAK!!!!! MSPI STORAGE ONLY FOR TESTING NVM %d\n",type)); |
|---|
| 542 | *data = malloc(record.length); |
|---|
| 543 | if (*data) |
|---|
| 544 | { |
|---|
| 545 | if ((result = p_fs->read(p_fs, p_fs->offset[type] + sizeof(fstore_record_t), *data,record.length)) != b_ok) |
|---|
| 546 | return result; |
|---|
| 547 | |
|---|
| 548 | } |
|---|
| 549 | } else |
|---|
| 550 | #endif |
|---|
| 551 | { |
|---|
| 552 | #if (BCHP_CHIP==7550) ||(BCHP_CHIP==7552) |
|---|
| 553 | *data = (void*)fstore_get_read_addr(p_fs, type, sizeof(fstore_record_t)); |
|---|
| 554 | *data += sizeof(fstore_record_t); |
|---|
| 555 | #else |
|---|
| 556 | *data = (void*)(p_fs->read_base + p_fs->offset[type] + sizeof(fstore_record_t)); |
|---|
| 557 | #endif |
|---|
| 558 | *length = record.length; |
|---|
| 559 | } |
|---|
| 560 | |
|---|
| 561 | return result; |
|---|
| 562 | } |
|---|
| 563 | |
|---|
| 564 | /* |
|---|
| 565 | Summary: |
|---|
| 566 | Perform a test to validate the fstore module. |
|---|
| 567 | */ |
|---|
| 568 | #ifdef BCM_FSTORE_TEST |
|---|
| 569 | void fstore_write_pattern(unsigned int * data_ptr, unsigned int len, unsigned int pattern) |
|---|
| 570 | { |
|---|
| 571 | int i; |
|---|
| 572 | BDBG_MSG(("Write pattern 0x%08x, %d words...\n",pattern,len/sizeof(unsigned int))); |
|---|
| 573 | for (i = 0; i < len/sizeof(unsigned int); i++) |
|---|
| 574 | { |
|---|
| 575 | data_ptr[i] = pattern; |
|---|
| 576 | } |
|---|
| 577 | } |
|---|
| 578 | |
|---|
| 579 | bresult fstore_test(fstore_t *p_fs,int reserved) |
|---|
| 580 | { |
|---|
| 581 | bresult result = berr_not_supported; |
|---|
| 582 | unsigned int len = sizeof(fstore_record_t); |
|---|
| 583 | void *data_ptr; |
|---|
| 584 | unsigned char* get_ptr; |
|---|
| 585 | unsigned int get_len; |
|---|
| 586 | BDBG_SetModuleLevel("fstore",BDBG_eTrace); |
|---|
| 587 | |
|---|
| 588 | //memcpy(p_fs,&(s_fstore[eFS_SP25_SPI]), sizeof(fstore_t)); |
|---|
| 589 | //memcpy(p_fs,&(s_fstore[eFS_MX29_EBI]), sizeof(fstore_t)); |
|---|
| 590 | |
|---|
| 591 | BDBG_ERR(("Setup for testing flash\n")); |
|---|
| 592 | BDBG_ERR(("read_base = 0x%08x\n",p_fs->read_base)); /* Read base address of device (read/write base to support BSPI on 3543 */ |
|---|
| 593 | BDBG_ERR(("write_base = 0x%08x\n",p_fs->write_base)); /* Write base address of device */ |
|---|
| 594 | BDBG_ERR(("offsets = %d,%d,%d\n",p_fs->offset[0],p_fs->offset[1],p_fs->offset[2])); |
|---|
| 595 | BDBG_ERR(("sector_size = %d\n", p_fs->sector_size)); /* Sector size in bytes */ |
|---|
| 596 | BDBG_ERR(("block_size = %d\n", p_fs->block_size)); /* Block size in bytes */ |
|---|
| 597 | BDBG_ERR(("max_size = %d", p_fs->max_size)); /* Block size in bytes */ |
|---|
| 598 | BDBG_ERR(("read = 0x%08x\n",p_fs->read)); |
|---|
| 599 | BDBG_ERR(("erase = 0x%08x\n",p_fs->erase)); |
|---|
| 600 | BDBG_ERR(("program = 0x%08x\n",p_fs->program)); |
|---|
| 601 | |
|---|
| 602 | BDBG_ERR(("Update eFS_APP_DATA...\n")); |
|---|
| 603 | |
|---|
| 604 | len = (64 * 1024) - sizeof(fstore_record_t); |
|---|
| 605 | if ((data_ptr = malloc(len)) == NULL) |
|---|
| 606 | { |
|---|
| 607 | BDBG_ERR(("Could not allocate memory for test %d\n",len)); |
|---|
| 608 | return result; |
|---|
| 609 | } |
|---|
| 610 | BDBG_ERR(("Write pattern(0x%08x,%d)\n",data_ptr,len)); |
|---|
| 611 | fstore_write_pattern(data_ptr,len,0xDEAD0ACE); |
|---|
| 612 | |
|---|
| 613 | BDBG_ERR(("Update1 eFS_APP_DATA...\n")); |
|---|
| 614 | if ((result = fstore_update(p_fs,eFS_APP_DATA, data_ptr,len)) != b_ok) |
|---|
| 615 | { |
|---|
| 616 | BDBG_ERR(("Update eFS_APP_DATA failed %d\n",result)); |
|---|
| 617 | return result; |
|---|
| 618 | } |
|---|
| 619 | |
|---|
| 620 | if ((result = fstore_get(p_fs,eFS_APP_DATA, (void**)&get_ptr,&get_len)) != b_ok) |
|---|
| 621 | { |
|---|
| 622 | BDBG_ERR(("Get eFS_APP_DATA failed %d\n",result)); |
|---|
| 623 | return result; |
|---|
| 624 | } |
|---|
| 625 | BDBG_ERR(("Update2 eFS_APP_DATA...\n")); |
|---|
| 626 | if ((result = fstore_update(p_fs,eFS_APP_DATA, data_ptr,len)) != b_ok) |
|---|
| 627 | { |
|---|
| 628 | BDBG_ERR(("Update eFS_APP_DATA failed %d\n",result)); |
|---|
| 629 | return result; |
|---|
| 630 | } |
|---|
| 631 | |
|---|
| 632 | if ((result = fstore_get(p_fs,eFS_APP_DATA, (void**)&get_ptr,&get_len)) != b_ok) |
|---|
| 633 | { |
|---|
| 634 | BDBG_ERR(("Get eFS_APP_DATA 2 failed %d\n",result)); |
|---|
| 635 | return result; |
|---|
| 636 | } |
|---|
| 637 | free(data_ptr); |
|---|
| 638 | |
|---|
| 639 | // BDBG_SetModuleLevel("fstore",BDBG_eWrn); |
|---|
| 640 | |
|---|
| 641 | return b_ok; |
|---|
| 642 | } |
|---|
| 643 | #endif |
|---|
| 644 | |
|---|