Aidan Thornton | db4e87d | 2013-08-27 18:01:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Support for Atmel AT45DB series DataFlash chips. |
| 3 | * This file is part of the flashrom project. |
| 4 | * |
| 5 | * Copyright (C) 2012 Aidan Thornton |
| 6 | * Copyright (C) 2013 Stefan Tauner |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include <string.h> |
| 23 | #include "flash.h" |
| 24 | #include "chipdrivers.h" |
| 25 | #include "programmer.h" |
| 26 | #include "spi.h" |
| 27 | |
| 28 | /* Status register bits */ |
| 29 | #define AT45DB_READY (1<<7) |
| 30 | #define AT45DB_CMP (1<<6) |
| 31 | #define AT45DB_PROT (1<<1) |
| 32 | #define AT45DB_POWEROF2 (1<<0) |
| 33 | |
| 34 | /* Opcodes */ |
| 35 | #define AT45DB_STATUS 0xD7 /* NB: this is a block erase command on most other chips(!). */ |
| 36 | #define AT45DB_DISABLE_PROTECT 0x3D, 0x2A, 0x7F, 0x9A |
Stefan Tauner | fdc4f7e | 2013-08-27 18:02:12 +0000 | [diff] [blame] | 37 | #define AT45DB_READ_ARRAY 0xE8 |
Aidan Thornton | db4e87d | 2013-08-27 18:01:53 +0000 | [diff] [blame] | 38 | #define AT45DB_READ_PROTECT 0x32 |
| 39 | #define AT45DB_READ_LOCKDOWN 0x35 |
| 40 | #define AT45DB_PAGE_ERASE 0x81 |
| 41 | #define AT45DB_BLOCK_ERASE 0x50 |
| 42 | #define AT45DB_SECTOR_ERASE 0x7C |
| 43 | #define AT45DB_CHIP_ERASE 0xC7 |
| 44 | #define AT45DB_CHIP_ERASE_ADDR 0x94809A /* Magic address. See usage. */ |
| 45 | #define AT45DB_BUFFER1_WRITE 0x84 |
| 46 | #define AT45DB_BUFFER1_PAGE_PROGRAM 0x88 |
| 47 | /* Buffer 2 is unused yet. |
| 48 | #define AT45DB_BUFFER2_WRITE 0x87 |
| 49 | #define AT45DB_BUFFER2_PAGE_PROGRAM 0x89 |
| 50 | */ |
| 51 | |
| 52 | static uint8_t at45db_read_status_register(struct flashctx *flash, uint8_t *status) |
| 53 | { |
| 54 | static const uint8_t cmd[] = { AT45DB_STATUS }; |
| 55 | |
| 56 | int ret = spi_send_command(flash, sizeof(cmd), 1, cmd, status); |
| 57 | if (ret != 0) |
| 58 | msg_cerr("Reading the status register failed!\n"); |
| 59 | else |
| 60 | msg_cspew("Status register: 0x%02x.\n", *status); |
| 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | int spi_disable_blockprotect_at45db(struct flashctx *flash) |
| 65 | { |
| 66 | static const uint8_t cmd[4] = { AT45DB_DISABLE_PROTECT }; /* NB: 4 bytes magic number */ |
| 67 | int ret = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL); |
| 68 | if (ret != 0) { |
| 69 | msg_cerr("Sending disable lockdown failed!\n"); |
| 70 | return ret; |
| 71 | } |
| 72 | uint8_t status; |
| 73 | ret = at45db_read_status_register(flash, &status); |
| 74 | if (ret != 0 || ((status & AT45DB_PROT) != 0)) { |
| 75 | msg_cerr("Disabling lockdown failed!\n"); |
| 76 | return 1; |
| 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static unsigned int at45db_get_sector_count(struct flashctx *flash) |
| 83 | { |
| 84 | unsigned int i, j; |
| 85 | unsigned int cnt = 0; |
| 86 | for (i = 0; i < NUM_ERASEFUNCTIONS; i++) { |
| 87 | if (flash->chip->block_erasers[i].block_erase == &spi_erase_at45db_sector) { |
| 88 | for (j = 0; j < NUM_ERASEREGIONS; j++) { |
| 89 | cnt += flash->chip->block_erasers[i].eraseblocks[j].count; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | msg_cspew("%s: number of sectors=%u\n", __func__, cnt); |
| 94 | return cnt; |
| 95 | } |
| 96 | |
| 97 | /* Reads and prettyprints protection/lockdown registers. |
| 98 | * Some elegance of the printouts had to be cut down a bit to share this code. */ |
| 99 | static uint8_t at45db_prettyprint_protection_register(struct flashctx *flash, uint8_t opcode, const char *regname) |
| 100 | { |
| 101 | const uint8_t cmd[] = { opcode, 0, 0, 0 }; |
| 102 | /* The first two sectors share the first result byte. */ |
| 103 | uint8_t buf[at45db_get_sector_count(flash) - 1]; |
| 104 | |
| 105 | int ret = spi_send_command(flash, sizeof(cmd), sizeof(buf), cmd, buf); |
| 106 | if (ret != 0) { |
| 107 | msg_cerr("Reading the %s register failed!\n", regname); |
| 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | unsigned int i; |
| 112 | for (i = 0; i < sizeof(buf); i++) { |
| 113 | if (buf[i] != 0x00) |
| 114 | break; |
| 115 | if (i == sizeof(buf) - 1) { |
| 116 | msg_cdbg("No Sector is %sed.\n", regname); |
| 117 | return 0; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /* TODO: print which addresses are mapped to (un)locked sectors. */ |
| 122 | msg_cdbg("Sector 0a is %s%sed.\n", ((buf[0] & 0xC0) == 0x00) ? "un" : "", regname); |
| 123 | msg_cdbg("Sector 0b is %s%sed.\n", ((buf[0] & 0x30) == 0x00) ? "un" : "", regname); |
| 124 | for (i = 1; i < sizeof(buf); i++) |
| 125 | msg_cdbg("Sector %2u is %s%sed.\n", i, (buf[i] == 0x00) ? "un" : "", regname); |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | /* bit 7: busy flag |
| 131 | * bit 6: memory/buffer compare result |
| 132 | * bit 5-2: density (encoding see below) |
| 133 | * bit 1: protection enabled (soft or hard) |
| 134 | * bit 0: "power of 2" page size indicator (e.g. 1 means 256B; 0 means 264B) |
| 135 | * |
| 136 | * 5-2 encoding: bit 2 is always 1, bits 3-5 encode the density as "2^(bits - 1)" in Mb e.g.: |
| 137 | * AT45DB161D 1011 16Mb */ |
| 138 | int spi_prettyprint_status_register_at45db(struct flashctx *flash) |
| 139 | { |
| 140 | uint8_t status; |
| 141 | if (at45db_read_status_register(flash, &status) != 0) { |
| 142 | return 1; |
| 143 | } |
| 144 | |
Stefan Tauner | fdc4f7e | 2013-08-27 18:02:12 +0000 | [diff] [blame] | 145 | /* AT45DB321C does not support lockdown or a page size of a power of 2... */ |
| 146 | const bool isAT45DB321C = (strcmp(flash->chip->name, "AT45DB321C") == 0); |
Aidan Thornton | db4e87d | 2013-08-27 18:01:53 +0000 | [diff] [blame] | 147 | msg_cdbg("Chip status register is 0x%02x\n", status); |
| 148 | msg_cdbg("Chip status register: Bit 7 / Ready is %sset\n", (status & AT45DB_READY) ? "" : "not "); |
| 149 | msg_cdbg("Chip status register: Bit 6 / Compare match is %sset\n", (status & AT45DB_CMP) ? "" : "not "); |
| 150 | spi_prettyprint_status_register_bit(status, 5); |
| 151 | spi_prettyprint_status_register_bit(status, 4); |
| 152 | spi_prettyprint_status_register_bit(status, 3); |
| 153 | spi_prettyprint_status_register_bit(status, 2); |
| 154 | const uint8_t dens = (status >> 3) & 0x7; /* Bit 2 is always 1, we use the other bits only */ |
| 155 | msg_cdbg("Chip status register: Density is %u Mb\n", 1 << (dens - 1)); |
| 156 | msg_cdbg("Chip status register: Bit 1 / Protection is %sset\n", (status & AT45DB_PROT) ? "" : "not "); |
Stefan Tauner | fdc4f7e | 2013-08-27 18:02:12 +0000 | [diff] [blame] | 157 | |
| 158 | if (isAT45DB321C) |
| 159 | spi_prettyprint_status_register_bit(status, 0); |
| 160 | else |
| 161 | msg_cdbg("Chip status register: Bit 0 / \"Power of 2\" is %sset\n", |
| 162 | (status & AT45DB_POWEROF2) ? "" : "not "); |
| 163 | |
Aidan Thornton | db4e87d | 2013-08-27 18:01:53 +0000 | [diff] [blame] | 164 | if (status & AT45DB_PROT) |
| 165 | at45db_prettyprint_protection_register(flash, AT45DB_READ_PROTECT, "protect"); |
| 166 | |
Stefan Tauner | fdc4f7e | 2013-08-27 18:02:12 +0000 | [diff] [blame] | 167 | if (!isAT45DB321C) |
| 168 | at45db_prettyprint_protection_register(flash, AT45DB_READ_LOCKDOWN, "lock"); |
Aidan Thornton | db4e87d | 2013-08-27 18:01:53 +0000 | [diff] [blame] | 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | /* Probe function for AT45DB* chips that support multiple page sizes. */ |
| 174 | int probe_spi_at45db(struct flashctx *flash) |
| 175 | { |
| 176 | uint8_t status; |
| 177 | struct flashchip *chip = flash->chip; |
| 178 | |
| 179 | if (!probe_spi_rdid(flash)) |
| 180 | return 0; |
| 181 | |
| 182 | /* Some AT45DB* chips support two different page sizes each (e.g. 264 and 256 B). In order to tell which |
| 183 | * page size this chip has we need to read the status register. */ |
| 184 | if (at45db_read_status_register(flash, &status) != 0) |
| 185 | return 0; |
| 186 | |
| 187 | /* We assume sane power-of-2 page sizes and adjust the chip attributes in case this is not the case. */ |
| 188 | if ((status & AT45DB_POWEROF2) == 0) { |
| 189 | chip->total_size = (chip->total_size / 32) * 33; |
| 190 | chip->page_size = (chip->page_size / 32) * 33; |
| 191 | |
| 192 | unsigned int i, j; |
| 193 | for (i = 0; i < NUM_ERASEFUNCTIONS; i++) { |
| 194 | struct block_eraser *eraser = &chip->block_erasers[i]; |
| 195 | for (j = 0; j < NUM_ERASEREGIONS; j++) { |
| 196 | eraser->eraseblocks[j].size = (eraser->eraseblocks[j].size / 32) * 33; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | switch (chip->page_size) { |
| 202 | case 256: chip->gran = write_gran_256bytes; break; |
| 203 | case 264: chip->gran = write_gran_264bytes; break; |
| 204 | case 512: chip->gran = write_gran_512bytes; break; |
| 205 | case 528: chip->gran = write_gran_528bytes; break; |
| 206 | case 1024: chip->gran = write_gran_1024bytes; break; |
| 207 | case 1056: chip->gran = write_gran_1056bytes; break; |
| 208 | default: |
| 209 | msg_cerr("%s: unknown page size %d.\n", __func__, chip->page_size); |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | msg_cdbg2("%s: total size %i kB, page size %i B\n", __func__, chip->total_size * 1024, chip->page_size); |
| 214 | |
| 215 | return 1; |
| 216 | } |
| 217 | |
| 218 | /* Returns the minimum number of bits needed to represent the given address. |
| 219 | * FIXME: use mind-blowing implementation. |
| 220 | * FIXME: move to utility module. */ |
| 221 | static uint32_t address_to_bits(uint32_t addr) |
| 222 | { |
| 223 | unsigned int lzb = 0; |
| 224 | while (((1 << (31 - lzb)) & ~addr) != 0) |
| 225 | lzb++; |
| 226 | return 32 - lzb; |
| 227 | } |
| 228 | |
| 229 | /* In case of non-power-of-two page sizes we need to convert the address flashrom uses to the address the |
| 230 | * DataFlash chips use. The latter uses a segmented address space where the page address is encoded in the |
| 231 | * more significant bits and the offset within the page is encoded in the less significant bits. The exact |
| 232 | * partition depends on the page size. |
| 233 | */ |
| 234 | static unsigned int at45db_convert_addr(unsigned int addr, unsigned int page_size) |
| 235 | { |
| 236 | unsigned int page_bits = address_to_bits(page_size - 1); |
| 237 | unsigned int at45db_addr = ((addr / page_size) << page_bits) | (addr % page_size); |
| 238 | msg_cspew("%s: addr=0x%x, page_size=%u, page_bits=%u -> at45db_addr=0x%x\n", |
| 239 | __func__, addr, page_size, page_bits, at45db_addr); |
| 240 | return at45db_addr; |
| 241 | } |
| 242 | |
| 243 | int spi_read_at45db(struct flashctx *flash, uint8_t *buf, unsigned int addr, unsigned int len) |
| 244 | { |
| 245 | const unsigned int page_size = flash->chip->page_size; |
| 246 | const unsigned int total_size = flash->chip->total_size * 1024; |
| 247 | if ((addr + len) > total_size) { |
| 248 | msg_cerr("%s: tried to read beyond flash boundary: addr=%u, len=%u, size=%u\n", |
| 249 | __func__, addr, len, total_size); |
| 250 | return 1; |
| 251 | } |
| 252 | |
| 253 | /* We have to split this up into chunks to fit within the programmer's read size limit, but those |
| 254 | * chunks can cross page boundaries. */ |
| 255 | const unsigned int max_data_read = flash->pgm->spi.max_data_read; |
| 256 | const unsigned int max_chunk = (max_data_read > 0) ? max_data_read : page_size; |
| 257 | while (addr < len) { |
| 258 | unsigned int chunk = min(max_chunk, len); |
| 259 | int ret = spi_nbyte_read(flash, at45db_convert_addr(addr, page_size), buf + addr, chunk); |
| 260 | if (ret) { |
| 261 | msg_cerr("%s: error sending read command!\n", __func__); |
| 262 | return ret; |
| 263 | } |
| 264 | addr += chunk; |
| 265 | } |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
Stefan Tauner | fdc4f7e | 2013-08-27 18:02:12 +0000 | [diff] [blame] | 270 | /* Legacy continuous read, used where spi_read_at45db() is not available. |
| 271 | * The first 4 (dummy) bytes read need to be discarded. */ |
| 272 | int spi_read_at45db_e8(struct flashctx *flash, uint8_t *buf, unsigned int addr, unsigned int len) |
| 273 | { |
| 274 | const unsigned int page_size = flash->chip->page_size; |
| 275 | const unsigned int total_size = flash->chip->total_size * 1024; |
| 276 | if ((addr + len) > total_size) { |
| 277 | msg_cerr("%s: tried to read beyond flash boundary: addr=%u, len=%u, size=%u\n", |
| 278 | __func__, addr, len, total_size); |
| 279 | return 1; |
| 280 | } |
| 281 | |
| 282 | /* We have to split this up into chunks to fit within the programmer's read size limit, but those |
| 283 | * chunks can cross page boundaries. */ |
| 284 | const unsigned int max_data_read = flash->pgm->spi.max_data_read; |
| 285 | const unsigned int max_chunk = (max_data_read > 0) ? max_data_read : page_size; |
| 286 | while (addr < len) { |
| 287 | const unsigned int addr_at45 = at45db_convert_addr(addr, page_size); |
| 288 | const unsigned char cmd[] = { |
| 289 | AT45DB_READ_ARRAY, |
| 290 | (addr_at45 >> 16) & 0xff, |
| 291 | (addr_at45 >> 8) & 0xff, |
| 292 | (addr_at45 >> 0) & 0xff |
| 293 | }; |
| 294 | /* We need to leave place for 4 dummy bytes and handle them explicitly. */ |
| 295 | unsigned int chunk = min(max_chunk, len + 4); |
| 296 | uint8_t tmp[chunk]; |
| 297 | int ret = spi_send_command(flash, sizeof(cmd), chunk, cmd, tmp); |
| 298 | if (ret) { |
| 299 | msg_cerr("%s: error sending read command!\n", __func__); |
| 300 | return ret; |
| 301 | } |
| 302 | /* Copy result without dummy bytes into buf and advance address counter respectively. */ |
| 303 | memcpy(buf + addr, tmp + 4, chunk - 4); |
| 304 | addr += chunk - 4; |
| 305 | } |
| 306 | return 0; |
| 307 | } |
| 308 | |
Aidan Thornton | db4e87d | 2013-08-27 18:01:53 +0000 | [diff] [blame] | 309 | /* Returns 0 when ready, 1 on errors and timeouts. */ |
| 310 | static int at45db_wait_ready (struct flashctx *flash, unsigned int us, unsigned int retries) |
| 311 | { |
| 312 | while (true) { |
| 313 | uint8_t status; |
| 314 | int ret = at45db_read_status_register(flash, &status); |
| 315 | if ((status & AT45DB_READY) == AT45DB_READY) |
| 316 | return 0; |
| 317 | if (ret != 0 || retries-- == 0) |
| 318 | return 1; |
| 319 | programmer_delay(us); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | static int at45db_erase(struct flashctx *flash, uint8_t opcode, unsigned int at45db_addr, unsigned int stepsize, unsigned int retries) |
| 324 | { |
| 325 | const uint8_t cmd[] = { |
| 326 | opcode, |
| 327 | (at45db_addr >> 16) & 0xff, |
| 328 | (at45db_addr >> 8) & 0xff, |
| 329 | (at45db_addr >> 0) & 0xff |
| 330 | }; |
| 331 | |
| 332 | /* Send erase command. */ |
| 333 | int ret = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL); |
| 334 | if (ret != 0) { |
| 335 | msg_cerr("%s: error sending erase command!\n", __func__); |
| 336 | return ret; |
| 337 | } |
| 338 | |
| 339 | /* Wait for completion. */ |
| 340 | ret = at45db_wait_ready(flash, stepsize, retries); |
| 341 | if (ret != 0) |
| 342 | msg_cerr("%s: chip did not became ready again after sending the erase command!\n", __func__); |
| 343 | |
| 344 | return ret; |
| 345 | } |
| 346 | |
| 347 | int spi_erase_at45db_page(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 348 | { |
| 349 | const unsigned int page_size = flash->chip->page_size; |
| 350 | const unsigned int total_size = flash->chip->total_size * 1024; |
| 351 | |
| 352 | if ((addr % page_size) != 0 || (blocklen % page_size) != 0) { |
| 353 | msg_cerr("%s: cannot erase partial pages: addr=%u, blocklen=%u\n", __func__, addr, blocklen); |
| 354 | return 1; |
| 355 | } |
| 356 | |
| 357 | if ((addr + blocklen) > total_size) { |
| 358 | msg_cerr("%s: tried to erase a block beyond flash boundary: addr=%u, blocklen=%u, size=%u\n", |
| 359 | __func__, addr, blocklen, total_size); |
| 360 | return 1; |
| 361 | } |
| 362 | |
| 363 | /* Needs typically about 35 ms for completion, so let's wait 100 ms in 500 us steps. */ |
| 364 | return at45db_erase(flash, AT45DB_PAGE_ERASE, at45db_convert_addr(addr, page_size), 500, 200); |
| 365 | } |
| 366 | |
| 367 | int spi_erase_at45db_block(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 368 | { |
| 369 | const unsigned int page_size = flash->chip->page_size; |
| 370 | const unsigned int total_size = flash->chip->total_size * 1024; |
| 371 | |
| 372 | if ((addr % page_size) != 0 || (blocklen % page_size) != 0) { // FIXME: should check blocks not pages |
| 373 | msg_cerr("%s: cannot erase partial pages: addr=%u, blocklen=%u\n", __func__, addr, blocklen); |
| 374 | return 1; |
| 375 | } |
| 376 | |
| 377 | if ((addr + blocklen) > total_size) { |
| 378 | msg_cerr("%s: tried to erase a block beyond flash boundary: addr=%u, blocklen=%u, size=%u\n", |
| 379 | __func__, addr, blocklen, total_size); |
| 380 | return 1; |
| 381 | } |
| 382 | |
| 383 | /* Needs typically between 20 and 100 ms for completion, so let's wait 300 ms in 1 ms steps. */ |
| 384 | return at45db_erase(flash, AT45DB_BLOCK_ERASE, at45db_convert_addr(addr, page_size), 1000, 300); |
| 385 | } |
| 386 | |
| 387 | int spi_erase_at45db_sector(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 388 | { |
| 389 | const unsigned int page_size = flash->chip->page_size; |
| 390 | const unsigned int total_size = flash->chip->total_size * 1024; |
| 391 | |
| 392 | if ((addr % page_size) != 0 || (blocklen % page_size) != 0) { // FIXME: should check sectors not pages |
| 393 | msg_cerr("%s: cannot erase partial pages: addr=%u, blocklen=%u\n", __func__, addr, blocklen); |
| 394 | return 1; |
| 395 | } |
| 396 | |
| 397 | if ((addr + blocklen) > total_size) { |
| 398 | msg_cerr("%s: tried to erase a sector beyond flash boundary: addr=%u, blocklen=%u, size=%u\n", |
| 399 | __func__, addr, blocklen, total_size); |
| 400 | return 1; |
| 401 | } |
| 402 | |
| 403 | /* Needs typically about 5 s for completion, so let's wait 20 seconds in 200 ms steps. */ |
| 404 | return at45db_erase(flash, AT45DB_SECTOR_ERASE, at45db_convert_addr(addr, page_size), 200000, 100); |
| 405 | } |
| 406 | |
| 407 | int spi_erase_at45db_chip(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 408 | { |
| 409 | const unsigned int total_size = flash->chip->total_size * 1024; |
| 410 | |
| 411 | if ((addr + blocklen) > total_size) { |
| 412 | msg_cerr("%s: tried to erase beyond flash boundary: addr=%u, blocklen=%u, size=%u\n", |
| 413 | __func__, addr, blocklen, total_size); |
| 414 | return 1; |
| 415 | } |
| 416 | |
| 417 | /* Needs typically from about 5 to over 60 s for completion, so let's wait 100 s in 500 ms steps. |
| 418 | * NB: the address is not a real address but a magic number. This hack allows to share code. */ |
| 419 | return at45db_erase(flash, AT45DB_CHIP_ERASE, AT45DB_CHIP_ERASE_ADDR, 500000, 200); |
| 420 | } |
| 421 | |
Stefan Tauner | 1dd5d3a | 2013-08-27 18:02:19 +0000 | [diff] [blame] | 422 | /* This one is really special and works only for AT45CS1282. It uses two different opcodes depending on the |
| 423 | * address and has an asymmetric layout. */ |
| 424 | int spi_erase_at45cs_sector(struct flashctx *flash, unsigned int addr, unsigned int blocklen) |
| 425 | { |
| 426 | const unsigned int page_size = flash->chip->page_size; |
| 427 | const unsigned int total_size = flash->chip->total_size * 1024; |
| 428 | const struct block_eraser be = flash->chip->block_erasers[0]; |
| 429 | const unsigned int sec_0a_top = be.eraseblocks[0].size; |
| 430 | const unsigned int sec_0b_top = be.eraseblocks[0].size + be.eraseblocks[1].size; |
| 431 | |
| 432 | if ((addr + blocklen) > total_size) { |
| 433 | msg_cerr("%s: tried to erase a sector beyond flash boundary: addr=%u, blocklen=%u, size=%u\n", |
| 434 | __func__, addr, blocklen, total_size); |
| 435 | return 1; |
| 436 | } |
| 437 | |
| 438 | bool partial_range = false; |
| 439 | uint8_t opcode = 0x7C; /* Used for all but sector 0a. */ |
| 440 | if (addr < sec_0a_top) { |
| 441 | opcode = 0x50; |
| 442 | /* One single sector of 8 pages at address 0. */ |
| 443 | if (addr != 0 || blocklen != (8 * page_size)) |
| 444 | partial_range = true; |
| 445 | } else if (addr < sec_0b_top) { |
| 446 | /* One single sector of 248 pages adjacent to the first. */ |
| 447 | if (addr != sec_0a_top || blocklen != (248 * page_size)) |
| 448 | partial_range = true; |
| 449 | } else { |
| 450 | /* The rest is filled by 63 aligned sectors of 256 pages. */ |
| 451 | if ((addr % (256 * page_size)) != 0 || (blocklen % (256 * page_size)) != 0) |
| 452 | partial_range = true; |
| 453 | } |
| 454 | if (partial_range) { |
| 455 | msg_cerr("%s: cannot erase partial sectors: addr=%u, blocklen=%u\n", __func__, addr, blocklen); |
| 456 | return 1; |
| 457 | } |
| 458 | |
| 459 | /* Needs up to 4 s for completion, so let's wait 20 seconds in 200 ms steps. */ |
| 460 | return at45db_erase(flash, opcode, at45db_convert_addr(addr, page_size), 200000, 100); |
| 461 | } |
| 462 | |
Aidan Thornton | db4e87d | 2013-08-27 18:01:53 +0000 | [diff] [blame] | 463 | static int at45db_fill_buffer1(struct flashctx *flash, uint8_t *bytes, unsigned int off, unsigned int len) |
| 464 | { |
| 465 | const unsigned int page_size = flash->chip->page_size; |
| 466 | if ((off + len) > page_size) { |
| 467 | msg_cerr("Tried to write %u bytes at offset %u into a buffer of only %u B.\n", |
| 468 | len, off, page_size); |
| 469 | return 1; |
| 470 | } |
| 471 | |
| 472 | /* Create a suitable buffer to store opcode, address and data chunks for buffer1. */ |
| 473 | const unsigned int max_data_write = flash->pgm->spi.max_data_write; |
| 474 | const unsigned int max_chunk = (max_data_write > 0 && max_data_write <= page_size) ? |
| 475 | max_data_write : page_size; |
| 476 | uint8_t buf[4 + max_chunk]; |
| 477 | |
| 478 | buf[0] = AT45DB_BUFFER1_WRITE; |
| 479 | while (off < page_size) { |
| 480 | unsigned int cur_chunk = min(max_chunk, page_size - off); |
| 481 | buf[1] = (off >> 16) & 0xff; |
| 482 | buf[2] = (off >> 8) & 0xff; |
| 483 | buf[3] = (off >> 0) & 0xff; |
| 484 | memcpy(&buf[4], bytes + off, cur_chunk); |
| 485 | int ret = spi_send_command(flash, 4 + cur_chunk, 0, buf, NULL); |
| 486 | if (ret != 0) { |
| 487 | msg_cerr("%s: error sending buffer write!\n", __func__); |
| 488 | return ret; |
| 489 | } |
| 490 | off += cur_chunk; |
| 491 | } |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | static int at45db_commit_buffer1(struct flashctx *flash, unsigned int at45db_addr) |
| 496 | { |
| 497 | const uint8_t cmd[] = { |
| 498 | AT45DB_BUFFER1_PAGE_PROGRAM, |
| 499 | (at45db_addr >> 16) & 0xff, |
| 500 | (at45db_addr >> 8) & 0xff, |
| 501 | (at45db_addr >> 0) & 0xff |
| 502 | }; |
| 503 | |
| 504 | /* Send buffer to device. */ |
| 505 | int ret = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL); |
| 506 | if (ret != 0) { |
| 507 | msg_cerr("%s: error sending buffer to main memory command!\n", __func__); |
| 508 | return ret; |
| 509 | } |
| 510 | |
| 511 | /* Wait for completion (typically a few ms). */ |
| 512 | ret = at45db_wait_ready(flash, 250, 200); // 50 ms |
| 513 | if (ret != 0) { |
| 514 | msg_cerr("%s: chip did not became ready again!\n", __func__); |
| 515 | return ret; |
| 516 | } |
| 517 | |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | static int at45db_program_page(struct flashctx *flash, uint8_t *buf, unsigned int at45db_addr) |
| 522 | { |
| 523 | int ret = at45db_fill_buffer1(flash, buf, 0, flash->chip->page_size); |
| 524 | if (ret != 0) { |
| 525 | msg_cerr("%s: filling the buffer failed!\n", __func__); |
| 526 | return ret; |
| 527 | } |
| 528 | |
| 529 | ret = at45db_commit_buffer1(flash, at45db_addr); |
| 530 | if (ret != 0) { |
| 531 | msg_cerr("%s: committing page failed!\n", __func__); |
| 532 | return ret; |
| 533 | } |
| 534 | |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | int spi_write_at45db(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len) |
| 539 | { |
| 540 | const unsigned int page_size = flash->chip->page_size; |
| 541 | const unsigned int total_size = flash->chip->total_size; |
| 542 | |
| 543 | if ((start % page_size) != 0 || (len % page_size) != 0) { |
| 544 | msg_cerr("%s: cannot write partial pages: start=%u, len=%u\n", __func__, start, len); |
| 545 | return 1; |
| 546 | } |
| 547 | |
| 548 | if ((start + len) > (total_size * 1024)) { |
| 549 | msg_cerr("%s: tried to write beyond flash boundary: start=%u, len=%u, size=%u\n", |
| 550 | __func__, start, len, total_size); |
| 551 | return 1; |
| 552 | } |
| 553 | |
| 554 | unsigned int i; |
| 555 | for (i = 0; i < len; i += page_size) { |
| 556 | if (at45db_program_page(flash, buf + i, at45db_convert_addr(start + i, page_size)) != 0) { |
| 557 | msg_cerr("Writing page %u failed!\n", i); |
| 558 | return 1; |
| 559 | } |
| 560 | } |
| 561 | return 0; |
| 562 | } |