1 /***************************************************************************
2 * Copyright (C) 2005, 2007 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * Copyright (C) 2009 Michael Schwingen *
5 * michael@schwingen.org *
6 * Copyright (C) 2010 Øyvind Harboe <oyvind.harboe@zylin.com> *
7 * Copyright (C) 2010 by Antonio Borneo <borneo.antonio@gmail.com> *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU General Public License for more details. *
19 * You should have received a copy of the GNU General Public License *
20 * along with this program; if not, write to the *
21 * Free Software Foundation, Inc., *
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
23 ***************************************************************************/
31 #include <target/arm.h>
32 #include <target/arm7_9_common.h>
33 #include <target/armv7m.h>
34 #include <helper/binarybuffer.h>
35 #include <target/algorithm.h>
38 #define CFI_MAX_BUS_WIDTH 4
39 #define CFI_MAX_CHIP_WIDTH 4
41 /* defines internal maximum size for code fragment in cfi_intel_write_block() */
42 #define CFI_MAX_INTEL_CODESIZE 256
44 static struct cfi_unlock_addresses cfi_unlock_addresses[] =
46 [CFI_UNLOCK_555_2AA] = { .unlock1 = 0x555, .unlock2 = 0x2aa },
47 [CFI_UNLOCK_5555_2AAA] = { .unlock1 = 0x5555, .unlock2 = 0x2aaa },
50 /* CFI fixups foward declarations */
51 static void cfi_fixup_0002_erase_regions(struct flash_bank *bank, void *param);
52 static void cfi_fixup_0002_unlock_addresses(struct flash_bank *bank, void *param);
53 static void cfi_fixup_reversed_erase_regions(struct flash_bank *bank, void *param);
54 static void cfi_fixup_0002_write_buffer(struct flash_bank *bank, void *param);
56 /* fixup after reading cmdset 0002 primary query table */
57 static const struct cfi_fixup cfi_0002_fixups[] = {
58 {CFI_MFR_SST, 0x00D4, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
59 {CFI_MFR_SST, 0x00D5, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
60 {CFI_MFR_SST, 0x00D6, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
61 {CFI_MFR_SST, 0x00D7, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
62 {CFI_MFR_SST, 0x2780, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
63 {CFI_MFR_SST, 0x236d, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_555_2AA]},
64 {CFI_MFR_ATMEL, 0x00C8, cfi_fixup_reversed_erase_regions, NULL},
65 {CFI_MFR_ST, 0x22C4, cfi_fixup_reversed_erase_regions, NULL}, /* M29W160ET */
66 {CFI_MFR_FUJITSU, 0x22ea, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_555_2AA]},
67 {CFI_MFR_FUJITSU, 0x226b, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
68 {CFI_MFR_AMIC, 0xb31a, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_555_2AA]},
69 {CFI_MFR_MX, 0x225b, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_555_2AA]},
70 {CFI_MFR_AMD, 0x225b, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_555_2AA]},
71 {CFI_MFR_ANY, CFI_ID_ANY, cfi_fixup_0002_erase_regions, NULL},
72 {CFI_MFR_ST, 0x227E, cfi_fixup_0002_write_buffer, NULL}, /* M29W128G */
76 /* fixup after reading cmdset 0001 primary query table */
77 static const struct cfi_fixup cfi_0001_fixups[] = {
81 static void cfi_fixup(struct flash_bank *bank, const struct cfi_fixup *fixups)
83 struct cfi_flash_bank *cfi_info = bank->driver_priv;
84 const struct cfi_fixup *f;
86 for (f = fixups; f->fixup; f++)
88 if (((f->mfr == CFI_MFR_ANY) || (f->mfr == cfi_info->manufacturer)) &&
89 ((f->id == CFI_ID_ANY) || (f->id == cfi_info->device_id)))
91 f->fixup(bank, f->param);
96 /* inline uint32_t flash_address(struct flash_bank *bank, int sector, uint32_t offset) */
97 static __inline__ uint32_t flash_address(struct flash_bank *bank, int sector, uint32_t offset)
99 struct cfi_flash_bank *cfi_info = bank->driver_priv;
101 if (cfi_info->x16_as_x8) offset *= 2;
103 /* while the sector list isn't built, only accesses to sector 0 work */
105 return bank->base + offset * bank->bus_width;
110 LOG_ERROR("BUG: sector list not yet built");
113 return bank->base + bank->sectors[sector].offset + offset * bank->bus_width;
117 static void cfi_command(struct flash_bank *bank, uint8_t cmd, uint8_t *cmd_buf)
121 /* clear whole buffer, to ensure bits that exceed the bus_width
124 for (i = 0; i < CFI_MAX_BUS_WIDTH; i++)
127 if (bank->target->endianness == TARGET_LITTLE_ENDIAN)
129 for (i = bank->bus_width; i > 0; i--)
131 *cmd_buf++ = (i & (bank->chip_width - 1)) ? 0x0 : cmd;
136 for (i = 1; i <= bank->bus_width; i++)
138 *cmd_buf++ = (i & (bank->chip_width - 1)) ? 0x0 : cmd;
143 static int cfi_send_command(struct flash_bank *bank, uint8_t cmd, uint32_t address)
145 uint8_t command[CFI_MAX_BUS_WIDTH];
147 cfi_command(bank, cmd, command);
148 return target_write_memory(bank->target, address, bank->bus_width, 1, command);
151 /* read unsigned 8-bit value from the bank
152 * flash banks are expected to be made of similar chips
153 * the query result should be the same for all
155 static int cfi_query_u8(struct flash_bank *bank, int sector, uint32_t offset, uint8_t *val)
157 struct target *target = bank->target;
158 uint8_t data[CFI_MAX_BUS_WIDTH];
161 retval = target_read_memory(target, flash_address(bank, sector, offset),
162 bank->bus_width, 1, data);
163 if (retval != ERROR_OK)
166 if (bank->target->endianness == TARGET_LITTLE_ENDIAN)
169 *val = data[bank->bus_width - 1];
174 /* read unsigned 8-bit value from the bank
175 * in case of a bank made of multiple chips,
176 * the individual values are ORed
178 static int cfi_get_u8(struct flash_bank *bank, int sector, uint32_t offset, uint8_t *val)
180 struct target *target = bank->target;
181 uint8_t data[CFI_MAX_BUS_WIDTH];
185 retval = target_read_memory(target, flash_address(bank, sector, offset),
186 bank->bus_width, 1, data);
187 if (retval != ERROR_OK)
190 if (bank->target->endianness == TARGET_LITTLE_ENDIAN)
192 for (i = 0; i < bank->bus_width / bank->chip_width; i++)
200 for (i = 0; i < bank->bus_width / bank->chip_width; i++)
201 value |= data[bank->bus_width - 1 - i];
208 static int cfi_query_u16(struct flash_bank *bank, int sector, uint32_t offset, uint16_t *val)
210 struct target *target = bank->target;
211 struct cfi_flash_bank *cfi_info = bank->driver_priv;
212 uint8_t data[CFI_MAX_BUS_WIDTH * 2];
215 if (cfi_info->x16_as_x8)
218 for (i = 0;i < 2;i++)
220 retval = target_read_memory(target, flash_address(bank, sector, offset + i),
221 bank->bus_width, 1, &data[i * bank->bus_width]);
222 if (retval != ERROR_OK)
227 retval = target_read_memory(target, flash_address(bank, sector, offset),
228 bank->bus_width, 2, data);
229 if (retval != ERROR_OK)
233 if (bank->target->endianness == TARGET_LITTLE_ENDIAN)
234 *val = data[0] | data[bank->bus_width] << 8;
236 *val = data[bank->bus_width - 1] | data[(2 * bank->bus_width) - 1] << 8;
241 static int cfi_query_u32(struct flash_bank *bank, int sector, uint32_t offset, uint32_t *val)
243 struct target *target = bank->target;
244 struct cfi_flash_bank *cfi_info = bank->driver_priv;
245 uint8_t data[CFI_MAX_BUS_WIDTH * 4];
248 if (cfi_info->x16_as_x8)
251 for (i = 0;i < 4;i++)
253 retval = target_read_memory(target, flash_address(bank, sector, offset + i),
254 bank->bus_width, 1, &data[i * bank->bus_width]);
255 if (retval != ERROR_OK)
261 retval = target_read_memory(target, flash_address(bank, sector, offset),
262 bank->bus_width, 4, data);
263 if (retval != ERROR_OK)
267 if (bank->target->endianness == TARGET_LITTLE_ENDIAN)
268 *val = data[0] | data[bank->bus_width] << 8 |
269 data[bank->bus_width * 2] << 16 | data[bank->bus_width * 3] << 24;
271 *val = data[bank->bus_width - 1] | data[(2* bank->bus_width) - 1] << 8 |
272 data[(3 * bank->bus_width) - 1] << 16 | data[(4 * bank->bus_width) - 1] << 24;
277 static int cfi_reset(struct flash_bank *bank)
279 struct cfi_flash_bank *cfi_info = bank->driver_priv;
280 int retval = ERROR_OK;
282 if ((retval = cfi_send_command(bank, 0xf0, flash_address(bank, 0, 0x0))) != ERROR_OK)
287 if ((retval = cfi_send_command(bank, 0xff, flash_address(bank, 0, 0x0))) != ERROR_OK)
292 if (cfi_info->manufacturer == 0x20 &&
293 (cfi_info->device_id == 0x227E || cfi_info->device_id == 0x7E))
295 /* Numonix M29W128G is cmd 0xFF intolerant - causes internal undefined state
296 * so we send an extra 0xF0 reset to fix the bug */
297 if ((retval = cfi_send_command(bank, 0xf0, flash_address(bank, 0, 0x00))) != ERROR_OK)
306 static void cfi_intel_clear_status_register(struct flash_bank *bank)
308 struct target *target = bank->target;
310 if (target->state != TARGET_HALTED)
312 LOG_ERROR("BUG: attempted to clear status register while target wasn't halted");
316 cfi_send_command(bank, 0x50, flash_address(bank, 0, 0x0));
319 static int cfi_intel_wait_status_busy(struct flash_bank *bank, int timeout, uint8_t *val)
323 int retval = ERROR_OK;
329 LOG_ERROR("timeout while waiting for WSM to become ready");
333 retval = cfi_get_u8(bank, 0, 0x0, &status);
334 if (retval != ERROR_OK)
343 /* mask out bit 0 (reserved) */
344 status = status & 0xfe;
346 LOG_DEBUG("status: 0x%x", status);
350 LOG_ERROR("status register: 0x%x", status);
352 LOG_ERROR("Block Lock-Bit Detected, Operation Abort");
354 LOG_ERROR("Program suspended");
356 LOG_ERROR("Low Programming Voltage Detected, Operation Aborted");
358 LOG_ERROR("Program Error / Error in Setting Lock-Bit");
360 LOG_ERROR("Error in Block Erasure or Clear Lock-Bits");
362 LOG_ERROR("Block Erase Suspended");
364 cfi_intel_clear_status_register(bank);
373 static int cfi_spansion_wait_status_busy(struct flash_bank *bank, int timeout)
375 uint8_t status, oldstatus;
376 struct cfi_flash_bank *cfi_info = bank->driver_priv;
379 retval = cfi_get_u8(bank, 0, 0x0, &oldstatus);
380 if (retval != ERROR_OK)
384 retval = cfi_get_u8(bank, 0, 0x0, &status);
386 if (retval != ERROR_OK)
389 if ((status ^ oldstatus) & 0x40) {
390 if (status & cfi_info->status_poll_mask & 0x20) {
391 retval = cfi_get_u8(bank, 0, 0x0, &oldstatus);
392 if (retval != ERROR_OK)
394 retval = cfi_get_u8(bank, 0, 0x0, &status);
395 if (retval != ERROR_OK)
397 if ((status ^ oldstatus) & 0x40) {
398 LOG_ERROR("dq5 timeout, status: 0x%x", status);
399 return(ERROR_FLASH_OPERATION_FAILED);
401 LOG_DEBUG("status: 0x%x", status);
405 } else { /* no toggle: finished, OK */
406 LOG_DEBUG("status: 0x%x", status);
412 } while (timeout-- > 0);
414 LOG_ERROR("timeout, status: 0x%x", status);
416 return(ERROR_FLASH_BUSY);
419 static int cfi_read_intel_pri_ext(struct flash_bank *bank)
422 struct cfi_flash_bank *cfi_info = bank->driver_priv;
423 struct cfi_intel_pri_ext *pri_ext;
425 if (cfi_info->pri_ext)
426 free(cfi_info->pri_ext);
428 pri_ext = malloc(sizeof(struct cfi_intel_pri_ext));
431 LOG_ERROR("Out of memory");
434 cfi_info->pri_ext = pri_ext;
436 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0, &pri_ext->pri[0]);
437 if (retval != ERROR_OK)
439 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 1, &pri_ext->pri[1]);
440 if (retval != ERROR_OK)
442 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 2, &pri_ext->pri[2]);
443 if (retval != ERROR_OK)
446 if ((pri_ext->pri[0] != 'P') || (pri_ext->pri[1] != 'R') || (pri_ext->pri[2] != 'I'))
448 if ((retval = cfi_reset(bank)) != ERROR_OK)
452 LOG_ERROR("Could not read bank flash bank information");
453 return ERROR_FLASH_BANK_INVALID;
456 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 3, &pri_ext->major_version);
457 if (retval != ERROR_OK)
459 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 4, &pri_ext->minor_version);
460 if (retval != ERROR_OK)
463 LOG_DEBUG("pri: '%c%c%c', version: %c.%c", pri_ext->pri[0], pri_ext->pri[1],
464 pri_ext->pri[2], pri_ext->major_version, pri_ext->minor_version);
466 retval = cfi_query_u32(bank, 0, cfi_info->pri_addr + 5, &pri_ext->feature_support);
467 if (retval != ERROR_OK)
469 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 9, &pri_ext->suspend_cmd_support);
470 if (retval != ERROR_OK)
472 retval = cfi_query_u16(bank, 0, cfi_info->pri_addr + 0xa, &pri_ext->blk_status_reg_mask);
473 if (retval != ERROR_OK)
476 LOG_DEBUG("feature_support: 0x%" PRIx32 ", suspend_cmd_support: "
477 "0x%x, blk_status_reg_mask: 0x%x",
478 pri_ext->feature_support,
479 pri_ext->suspend_cmd_support,
480 pri_ext->blk_status_reg_mask);
482 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0xc, &pri_ext->vcc_optimal);
483 if (retval != ERROR_OK)
485 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0xd, &pri_ext->vpp_optimal);
486 if (retval != ERROR_OK)
489 LOG_DEBUG("Vcc opt: %x.%x, Vpp opt: %u.%x",
490 (pri_ext->vcc_optimal & 0xf0) >> 4, pri_ext->vcc_optimal & 0x0f,
491 (pri_ext->vpp_optimal & 0xf0) >> 4, pri_ext->vpp_optimal & 0x0f);
493 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0xe, &pri_ext->num_protection_fields);
494 if (retval != ERROR_OK)
496 if (pri_ext->num_protection_fields != 1)
498 LOG_WARNING("expected one protection register field, but found %i",
499 pri_ext->num_protection_fields);
502 retval = cfi_query_u16(bank, 0, cfi_info->pri_addr + 0xf, &pri_ext->prot_reg_addr);
503 if (retval != ERROR_OK)
505 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0x11, &pri_ext->fact_prot_reg_size);
506 if (retval != ERROR_OK)
508 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0x12, &pri_ext->user_prot_reg_size);
509 if (retval != ERROR_OK)
512 LOG_DEBUG("protection_fields: %i, prot_reg_addr: 0x%x, "
513 "factory pre-programmed: %i, user programmable: %i",
514 pri_ext->num_protection_fields, pri_ext->prot_reg_addr,
515 1 << pri_ext->fact_prot_reg_size, 1 << pri_ext->user_prot_reg_size);
520 static int cfi_read_spansion_pri_ext(struct flash_bank *bank)
523 struct cfi_flash_bank *cfi_info = bank->driver_priv;
524 struct cfi_spansion_pri_ext *pri_ext;
526 if (cfi_info->pri_ext)
527 free(cfi_info->pri_ext);
529 pri_ext = malloc(sizeof(struct cfi_spansion_pri_ext));
532 LOG_ERROR("Out of memory");
535 cfi_info->pri_ext = pri_ext;
537 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0, &pri_ext->pri[0]);
538 if (retval != ERROR_OK)
540 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 1, &pri_ext->pri[1]);
541 if (retval != ERROR_OK)
543 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 2, &pri_ext->pri[2]);
544 if (retval != ERROR_OK)
547 if ((pri_ext->pri[0] != 'P') || (pri_ext->pri[1] != 'R') || (pri_ext->pri[2] != 'I'))
549 if ((retval = cfi_send_command(bank, 0xf0, flash_address(bank, 0, 0x0))) != ERROR_OK)
553 LOG_ERROR("Could not read spansion bank information");
554 return ERROR_FLASH_BANK_INVALID;
557 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 3, &pri_ext->major_version);
558 if (retval != ERROR_OK)
560 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 4, &pri_ext->minor_version);
561 if (retval != ERROR_OK)
564 LOG_DEBUG("pri: '%c%c%c', version: %c.%c", pri_ext->pri[0], pri_ext->pri[1],
565 pri_ext->pri[2], pri_ext->major_version, pri_ext->minor_version);
567 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 5, &pri_ext->SiliconRevision);
568 if (retval != ERROR_OK)
570 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 6, &pri_ext->EraseSuspend);
571 if (retval != ERROR_OK)
573 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 7, &pri_ext->BlkProt);
574 if (retval != ERROR_OK)
576 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 8, &pri_ext->TmpBlkUnprotect);
577 if (retval != ERROR_OK)
579 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 9, &pri_ext->BlkProtUnprot);
580 if (retval != ERROR_OK)
582 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 10, &pri_ext->SimultaneousOps);
583 if (retval != ERROR_OK)
585 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 11, &pri_ext->BurstMode);
586 if (retval != ERROR_OK)
588 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 12, &pri_ext->PageMode);
589 if (retval != ERROR_OK)
591 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 13, &pri_ext->VppMin);
592 if (retval != ERROR_OK)
594 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 14, &pri_ext->VppMax);
595 if (retval != ERROR_OK)
597 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 15, &pri_ext->TopBottom);
598 if (retval != ERROR_OK)
601 LOG_DEBUG("Silicon Revision: 0x%x, Erase Suspend: 0x%x, Block protect: 0x%x",
602 pri_ext->SiliconRevision, pri_ext->EraseSuspend, pri_ext->BlkProt);
604 LOG_DEBUG("Temporary Unprotect: 0x%x, Block Protect Scheme: 0x%x, "
605 "Simultaneous Ops: 0x%x", pri_ext->TmpBlkUnprotect,
606 pri_ext->BlkProtUnprot, pri_ext->SimultaneousOps);
608 LOG_DEBUG("Burst Mode: 0x%x, Page Mode: 0x%x, ", pri_ext->BurstMode, pri_ext->PageMode);
611 LOG_DEBUG("Vpp min: %u.%x, Vpp max: %u.%x",
612 (pri_ext->VppMin & 0xf0) >> 4, pri_ext->VppMin & 0x0f,
613 (pri_ext->VppMax & 0xf0) >> 4, pri_ext->VppMax & 0x0f);
615 LOG_DEBUG("WP# protection 0x%x", pri_ext->TopBottom);
617 /* default values for implementation specific workarounds */
618 pri_ext->_unlock1 = cfi_unlock_addresses[CFI_UNLOCK_555_2AA].unlock1;
619 pri_ext->_unlock2 = cfi_unlock_addresses[CFI_UNLOCK_555_2AA].unlock2;
620 pri_ext->_reversed_geometry = 0;
625 static int cfi_read_atmel_pri_ext(struct flash_bank *bank)
628 struct cfi_atmel_pri_ext atmel_pri_ext;
629 struct cfi_flash_bank *cfi_info = bank->driver_priv;
630 struct cfi_spansion_pri_ext *pri_ext;
632 if (cfi_info->pri_ext)
633 free(cfi_info->pri_ext);
635 pri_ext = malloc(sizeof(struct cfi_spansion_pri_ext));
638 LOG_ERROR("Out of memory");
642 /* ATMEL devices use the same CFI primary command set (0x2) as AMD/Spansion,
643 * but a different primary extended query table.
644 * We read the atmel table, and prepare a valid AMD/Spansion query table.
647 memset(pri_ext, 0, sizeof(struct cfi_spansion_pri_ext));
649 cfi_info->pri_ext = pri_ext;
651 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0, &atmel_pri_ext.pri[0]);
652 if (retval != ERROR_OK)
654 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 1, &atmel_pri_ext.pri[1]);
655 if (retval != ERROR_OK)
657 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 2, &atmel_pri_ext.pri[2]);
658 if (retval != ERROR_OK)
661 if ((atmel_pri_ext.pri[0] != 'P') || (atmel_pri_ext.pri[1] != 'R')
662 || (atmel_pri_ext.pri[2] != 'I'))
664 if ((retval = cfi_send_command(bank, 0xf0, flash_address(bank, 0, 0x0))) != ERROR_OK)
668 LOG_ERROR("Could not read atmel bank information");
669 return ERROR_FLASH_BANK_INVALID;
672 pri_ext->pri[0] = atmel_pri_ext.pri[0];
673 pri_ext->pri[1] = atmel_pri_ext.pri[1];
674 pri_ext->pri[2] = atmel_pri_ext.pri[2];
676 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 3, &atmel_pri_ext.major_version);
677 if (retval != ERROR_OK)
679 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 4, &atmel_pri_ext.minor_version);
680 if (retval != ERROR_OK)
683 LOG_DEBUG("pri: '%c%c%c', version: %c.%c", atmel_pri_ext.pri[0],
684 atmel_pri_ext.pri[1], atmel_pri_ext.pri[2],
685 atmel_pri_ext.major_version, atmel_pri_ext.minor_version);
687 pri_ext->major_version = atmel_pri_ext.major_version;
688 pri_ext->minor_version = atmel_pri_ext.minor_version;
690 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 5, &atmel_pri_ext.features);
691 if (retval != ERROR_OK)
693 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 6, &atmel_pri_ext.bottom_boot);
694 if (retval != ERROR_OK)
696 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 7, &atmel_pri_ext.burst_mode);
697 if (retval != ERROR_OK)
699 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 8, &atmel_pri_ext.page_mode);
700 if (retval != ERROR_OK)
703 LOG_DEBUG("features: 0x%2.2x, bottom_boot: 0x%2.2x, burst_mode: 0x%2.2x, page_mode: 0x%2.2x",
704 atmel_pri_ext.features, atmel_pri_ext.bottom_boot,
705 atmel_pri_ext.burst_mode, atmel_pri_ext.page_mode);
707 if (atmel_pri_ext.features & 0x02)
708 pri_ext->EraseSuspend = 2;
710 if (atmel_pri_ext.bottom_boot)
711 pri_ext->TopBottom = 2;
713 pri_ext->TopBottom = 3;
715 pri_ext->_unlock1 = cfi_unlock_addresses[CFI_UNLOCK_555_2AA].unlock1;
716 pri_ext->_unlock2 = cfi_unlock_addresses[CFI_UNLOCK_555_2AA].unlock2;
721 static int cfi_read_0002_pri_ext(struct flash_bank *bank)
723 struct cfi_flash_bank *cfi_info = bank->driver_priv;
725 if (cfi_info->manufacturer == CFI_MFR_ATMEL)
727 return cfi_read_atmel_pri_ext(bank);
731 return cfi_read_spansion_pri_ext(bank);
735 static int cfi_spansion_info(struct flash_bank *bank, char *buf, int buf_size)
738 struct cfi_flash_bank *cfi_info = bank->driver_priv;
739 struct cfi_spansion_pri_ext *pri_ext = cfi_info->pri_ext;
741 printed = snprintf(buf, buf_size, "\nSpansion primary algorithm extend information:\n");
745 printed = snprintf(buf, buf_size, "pri: '%c%c%c', version: %c.%c\n", pri_ext->pri[0],
746 pri_ext->pri[1], pri_ext->pri[2],
747 pri_ext->major_version, pri_ext->minor_version);
751 printed = snprintf(buf, buf_size, "Silicon Rev.: 0x%x, Address Sensitive unlock: 0x%x\n",
752 (pri_ext->SiliconRevision) >> 2,
753 (pri_ext->SiliconRevision) & 0x03);
757 printed = snprintf(buf, buf_size, "Erase Suspend: 0x%x, Sector Protect: 0x%x\n",
758 pri_ext->EraseSuspend,
763 printed = snprintf(buf, buf_size, "VppMin: %u.%x, VppMax: %u.%x\n",
764 (pri_ext->VppMin & 0xf0) >> 4, pri_ext->VppMin & 0x0f,
765 (pri_ext->VppMax & 0xf0) >> 4, pri_ext->VppMax & 0x0f);
770 static int cfi_intel_info(struct flash_bank *bank, char *buf, int buf_size)
773 struct cfi_flash_bank *cfi_info = bank->driver_priv;
774 struct cfi_intel_pri_ext *pri_ext = cfi_info->pri_ext;
776 printed = snprintf(buf, buf_size, "\nintel primary algorithm extend information:\n");
780 printed = snprintf(buf, buf_size, "pri: '%c%c%c', version: %c.%c\n", pri_ext->pri[0],
781 pri_ext->pri[1], pri_ext->pri[2], pri_ext->major_version, pri_ext->minor_version);
785 printed = snprintf(buf, buf_size, "feature_support: 0x%" PRIx32 ", "
786 "suspend_cmd_support: 0x%x, blk_status_reg_mask: 0x%x\n",
787 pri_ext->feature_support, pri_ext->suspend_cmd_support, pri_ext->blk_status_reg_mask);
791 printed = snprintf(buf, buf_size, "Vcc opt: %x.%x, Vpp opt: %u.%x\n",
792 (pri_ext->vcc_optimal & 0xf0) >> 4, pri_ext->vcc_optimal & 0x0f,
793 (pri_ext->vpp_optimal & 0xf0) >> 4, pri_ext->vpp_optimal & 0x0f);
797 printed = snprintf(buf, buf_size, "protection_fields: %i, prot_reg_addr: 0x%x, "
798 "factory pre-programmed: %i, user programmable: %i\n",
799 pri_ext->num_protection_fields, pri_ext->prot_reg_addr,
800 1 << pri_ext->fact_prot_reg_size, 1 << pri_ext->user_prot_reg_size);
805 /* flash_bank cfi <base> <size> <chip_width> <bus_width> <target#> [options]
807 FLASH_BANK_COMMAND_HANDLER(cfi_flash_bank_command)
809 struct cfi_flash_bank *cfi_info;
813 LOG_WARNING("incomplete flash_bank cfi configuration");
814 return ERROR_FLASH_BANK_INVALID;
818 * - not exceed max value;
820 * - be equal to a power of 2.
821 * bus must be wide enought to hold one chip */
822 if ((bank->chip_width > CFI_MAX_CHIP_WIDTH)
823 || (bank->bus_width > CFI_MAX_BUS_WIDTH)
824 || (bank->chip_width == 0)
825 || (bank->bus_width == 0)
826 || (bank->chip_width & (bank->chip_width - 1))
827 || (bank->bus_width & (bank->bus_width - 1))
828 || (bank->chip_width > bank->bus_width))
830 LOG_ERROR("chip and bus width have to specified in bytes");
831 return ERROR_FLASH_BANK_INVALID;
834 cfi_info = malloc(sizeof(struct cfi_flash_bank));
835 cfi_info->probed = 0;
836 cfi_info->erase_region_info = NULL;
837 cfi_info->pri_ext = NULL;
838 bank->driver_priv = cfi_info;
840 cfi_info->write_algorithm = NULL;
842 cfi_info->x16_as_x8 = 0;
843 cfi_info->jedec_probe = 0;
844 cfi_info->not_cfi = 0;
846 for (unsigned i = 6; i < CMD_ARGC; i++)
848 if (strcmp(CMD_ARGV[i], "x16_as_x8") == 0)
850 cfi_info->x16_as_x8 = 1;
852 else if (strcmp(CMD_ARGV[i], "jedec_probe") == 0)
854 cfi_info->jedec_probe = 1;
858 cfi_info->write_algorithm = NULL;
860 /* bank wasn't probed yet */
861 cfi_info->qry[0] = 0xff;
866 static int cfi_intel_erase(struct flash_bank *bank, int first, int last)
869 struct cfi_flash_bank *cfi_info = bank->driver_priv;
872 cfi_intel_clear_status_register(bank);
874 for (i = first; i <= last; i++)
876 if ((retval = cfi_send_command(bank, 0x20, flash_address(bank, i, 0x0))) != ERROR_OK)
881 if ((retval = cfi_send_command(bank, 0xd0, flash_address(bank, i, 0x0))) != ERROR_OK)
887 retval = cfi_intel_wait_status_busy(bank, cfi_info->block_erase_timeout, &status);
888 if (retval != ERROR_OK)
892 bank->sectors[i].is_erased = 1;
895 if ((retval = cfi_send_command(bank, 0xff, flash_address(bank, 0, 0x0))) != ERROR_OK)
900 LOG_ERROR("couldn't erase block %i of flash bank at base 0x%" PRIx32 , i, bank->base);
901 return ERROR_FLASH_OPERATION_FAILED;
905 return cfi_send_command(bank, 0xff, flash_address(bank, 0, 0x0));
908 static int cfi_spansion_erase(struct flash_bank *bank, int first, int last)
911 struct cfi_flash_bank *cfi_info = bank->driver_priv;
912 struct cfi_spansion_pri_ext *pri_ext = cfi_info->pri_ext;
915 for (i = first; i <= last; i++)
917 if ((retval = cfi_send_command(bank, 0xaa,
918 flash_address(bank, 0, pri_ext->_unlock1))) != ERROR_OK)
923 if ((retval = cfi_send_command(bank, 0x55,
924 flash_address(bank, 0, pri_ext->_unlock2))) != ERROR_OK)
929 if ((retval = cfi_send_command(bank, 0x80,
930 flash_address(bank, 0, pri_ext->_unlock1))) != ERROR_OK)
935 if ((retval = cfi_send_command(bank, 0xaa,
936 flash_address(bank, 0, pri_ext->_unlock1))) != ERROR_OK)
941 if ((retval = cfi_send_command(bank, 0x55,
942 flash_address(bank, 0, pri_ext->_unlock2))) != ERROR_OK)
947 if ((retval = cfi_send_command(bank, 0x30,
948 flash_address(bank, i, 0x0))) != ERROR_OK)
953 if (cfi_spansion_wait_status_busy(bank, cfi_info->block_erase_timeout) == ERROR_OK)
955 bank->sectors[i].is_erased = 1;
959 if ((retval = cfi_send_command(bank, 0xf0,
960 flash_address(bank, 0, 0x0))) != ERROR_OK)
965 LOG_ERROR("couldn't erase block %i of flash bank at base 0x%"
966 PRIx32, i, bank->base);
967 return ERROR_FLASH_OPERATION_FAILED;
971 return cfi_send_command(bank, 0xf0, flash_address(bank, 0, 0x0));
974 static int cfi_erase(struct flash_bank *bank, int first, int last)
976 struct cfi_flash_bank *cfi_info = bank->driver_priv;
978 if (bank->target->state != TARGET_HALTED)
980 LOG_ERROR("Target not halted");
981 return ERROR_TARGET_NOT_HALTED;
984 if ((first < 0) || (last < first) || (last >= bank->num_sectors))
986 return ERROR_FLASH_SECTOR_INVALID;
989 if (cfi_info->qry[0] != 'Q')
990 return ERROR_FLASH_BANK_NOT_PROBED;
992 switch (cfi_info->pri_id)
996 return cfi_intel_erase(bank, first, last);
999 return cfi_spansion_erase(bank, first, last);
1002 LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
1009 static int cfi_intel_protect(struct flash_bank *bank, int set, int first, int last)
1012 struct cfi_flash_bank *cfi_info = bank->driver_priv;
1013 struct cfi_intel_pri_ext *pri_ext = cfi_info->pri_ext;
1017 /* if the device supports neither legacy lock/unlock (bit 3) nor
1018 * instant individual block locking (bit 5).
1020 if (!(pri_ext->feature_support & 0x28))
1022 LOG_ERROR("lock/unlock not supported on flash");
1023 return ERROR_FLASH_OPERATION_FAILED;
1026 cfi_intel_clear_status_register(bank);
1028 for (i = first; i <= last; i++)
1030 if ((retval = cfi_send_command(bank, 0x60, flash_address(bank, i, 0x0))) != ERROR_OK)
1036 if ((retval = cfi_send_command(bank, 0x01, flash_address(bank, i, 0x0))) != ERROR_OK)
1040 bank->sectors[i].is_protected = 1;
1044 if ((retval = cfi_send_command(bank, 0xd0, flash_address(bank, i, 0x0))) != ERROR_OK)
1048 bank->sectors[i].is_protected = 0;
1051 /* instant individual block locking doesn't require reading of the status register */
1052 if (!(pri_ext->feature_support & 0x20))
1054 /* Clear lock bits operation may take up to 1.4s */
1056 retval = cfi_intel_wait_status_busy(bank, 1400, &status);
1057 if (retval != ERROR_OK)
1062 uint8_t block_status;
1063 /* read block lock bit, to verify status */
1064 if ((retval = cfi_send_command(bank, 0x90, flash_address(bank, 0, 0x55))) != ERROR_OK)
1068 retval = cfi_get_u8(bank, i, 0x2, &block_status);
1069 if (retval != ERROR_OK)
1072 if ((block_status & 0x1) != set)
1074 LOG_ERROR("couldn't change block lock status (set = %i, block_status = 0x%2.2x)",
1076 if ((retval = cfi_send_command(bank, 0x70,
1077 flash_address(bank, 0, 0x55))) != ERROR_OK)
1082 retval = cfi_intel_wait_status_busy(bank, 10, &status);
1083 if (retval != ERROR_OK)
1087 return ERROR_FLASH_OPERATION_FAILED;
1097 /* if the device doesn't support individual block lock bits set/clear,
1098 * all blocks have been unlocked in parallel, so we set those that should be protected
1100 if ((!set) && (!(pri_ext->feature_support & 0x20)))
1102 /* FIX!!! this code path is broken!!!
1104 * The correct approach is:
1106 * 1. read out current protection status
1108 * 2. override read out protection status w/unprotected.
1110 * 3. re-protect what should be protected.
1113 for (i = 0; i < bank->num_sectors; i++)
1115 if (bank->sectors[i].is_protected == 1)
1117 cfi_intel_clear_status_register(bank);
1119 if ((retval = cfi_send_command(bank, 0x60,
1120 flash_address(bank, i, 0x0))) != ERROR_OK)
1125 if ((retval = cfi_send_command(bank, 0x01,
1126 flash_address(bank, i, 0x0))) != ERROR_OK)
1132 retval = cfi_intel_wait_status_busy(bank, 100, &status);
1133 if (retval != ERROR_OK)
1139 return cfi_send_command(bank, 0xff, flash_address(bank, 0, 0x0));
1142 static int cfi_protect(struct flash_bank *bank, int set, int first, int last)
1144 struct cfi_flash_bank *cfi_info = bank->driver_priv;
1146 if (bank->target->state != TARGET_HALTED)
1148 LOG_ERROR("Target not halted");
1149 return ERROR_TARGET_NOT_HALTED;
1152 if ((first < 0) || (last < first) || (last >= bank->num_sectors))
1154 LOG_ERROR("Invalid sector range");
1155 return ERROR_FLASH_SECTOR_INVALID;
1158 if (cfi_info->qry[0] != 'Q')
1159 return ERROR_FLASH_BANK_NOT_PROBED;
1161 switch (cfi_info->pri_id)
1165 return cfi_intel_protect(bank, set, first, last);
1168 LOG_WARNING("protect: cfi primary command set %i unsupported", cfi_info->pri_id);
1173 /* Convert code image to target endian */
1174 /* FIXME create general block conversion fcts in target.c?) */
1175 static void cfi_fix_code_endian(struct target *target, uint8_t *dest,
1176 const uint32_t *src, uint32_t count)
1179 for (i = 0; i< count; i++)
1181 target_buffer_set_u32(target, dest, *src);
1187 static uint32_t cfi_command_val(struct flash_bank *bank, uint8_t cmd)
1189 struct target *target = bank->target;
1191 uint8_t buf[CFI_MAX_BUS_WIDTH];
1192 cfi_command(bank, cmd, buf);
1193 switch (bank->bus_width)
1199 return target_buffer_get_u16(target, buf);
1202 return target_buffer_get_u32(target, buf);
1205 LOG_ERROR("Unsupported bank buswidth %d, can't do block memory writes", bank->bus_width);
1210 static int cfi_intel_write_block(struct flash_bank *bank, uint8_t *buffer,
1211 uint32_t address, uint32_t count)
1213 struct cfi_flash_bank *cfi_info = bank->driver_priv;
1214 struct target *target = bank->target;
1215 struct reg_param reg_params[7];
1216 struct arm_algorithm armv4_5_info;
1217 struct working_area *source;
1218 uint32_t buffer_size = 32768;
1219 uint32_t write_command_val, busy_pattern_val, error_pattern_val;
1221 /* algorithm register usage:
1222 * r0: source address (in RAM)
1223 * r1: target address (in Flash)
1225 * r3: flash write command
1226 * r4: status byte (returned to host)
1227 * r5: busy test pattern
1228 * r6: error test pattern
1231 /* see contib/loaders/flash/armv4_5_cfi_intel_32.s for src */
1232 static const uint32_t word_32_code[] = {
1233 0xe4904004, /* loop: ldr r4, [r0], #4 */
1234 0xe5813000, /* str r3, [r1] */
1235 0xe5814000, /* str r4, [r1] */
1236 0xe5914000, /* busy: ldr r4, [r1] */
1237 0xe0047005, /* and r7, r4, r5 */
1238 0xe1570005, /* cmp r7, r5 */
1239 0x1afffffb, /* bne busy */
1240 0xe1140006, /* tst r4, r6 */
1241 0x1a000003, /* bne done */
1242 0xe2522001, /* subs r2, r2, #1 */
1243 0x0a000001, /* beq done */
1244 0xe2811004, /* add r1, r1 #4 */
1245 0xeafffff2, /* b loop */
1246 0xeafffffe /* done: b -2 */
1249 /* see contib/loaders/flash/armv4_5_cfi_intel_16.s for src */
1250 static const uint32_t word_16_code[] = {
1251 0xe0d040b2, /* loop: ldrh r4, [r0], #2 */
1252 0xe1c130b0, /* strh r3, [r1] */
1253 0xe1c140b0, /* strh r4, [r1] */
1254 0xe1d140b0, /* busy ldrh r4, [r1] */
1255 0xe0047005, /* and r7, r4, r5 */
1256 0xe1570005, /* cmp r7, r5 */
1257 0x1afffffb, /* bne busy */
1258 0xe1140006, /* tst r4, r6 */
1259 0x1a000003, /* bne done */
1260 0xe2522001, /* subs r2, r2, #1 */
1261 0x0a000001, /* beq done */
1262 0xe2811002, /* add r1, r1 #2 */
1263 0xeafffff2, /* b loop */
1264 0xeafffffe /* done: b -2 */
1267 /* see contib/loaders/flash/armv4_5_cfi_intel_8.s for src */
1268 static const uint32_t word_8_code[] = {
1269 0xe4d04001, /* loop: ldrb r4, [r0], #1 */
1270 0xe5c13000, /* strb r3, [r1] */
1271 0xe5c14000, /* strb r4, [r1] */
1272 0xe5d14000, /* busy ldrb r4, [r1] */
1273 0xe0047005, /* and r7, r4, r5 */
1274 0xe1570005, /* cmp r7, r5 */
1275 0x1afffffb, /* bne busy */
1276 0xe1140006, /* tst r4, r6 */
1277 0x1a000003, /* bne done */
1278 0xe2522001, /* subs r2, r2, #1 */
1279 0x0a000001, /* beq done */
1280 0xe2811001, /* add r1, r1 #1 */
1281 0xeafffff2, /* b loop */
1282 0xeafffffe /* done: b -2 */
1284 uint8_t target_code[4*CFI_MAX_INTEL_CODESIZE];
1285 const uint32_t *target_code_src;
1286 uint32_t target_code_size;
1287 int retval = ERROR_OK;
1290 cfi_intel_clear_status_register(bank);
1292 armv4_5_info.common_magic = ARM_COMMON_MAGIC;
1293 armv4_5_info.core_mode = ARM_MODE_SVC;
1294 armv4_5_info.core_state = ARM_STATE_ARM;
1296 /* If we are setting up the write_algorith, we need target_code_src */
1297 /* if not we only need target_code_size. */
1299 /* However, we don't want to create multiple code paths, so we */
1300 /* do the unecessary evaluation of target_code_src, which the */
1301 /* compiler will probably nicely optimize away if not needed */
1303 /* prepare algorithm code for target endian */
1304 switch (bank->bus_width)
1307 target_code_src = word_8_code;
1308 target_code_size = sizeof(word_8_code);
1311 target_code_src = word_16_code;
1312 target_code_size = sizeof(word_16_code);
1315 target_code_src = word_32_code;
1316 target_code_size = sizeof(word_32_code);
1319 LOG_ERROR("Unsupported bank buswidth %d, can't do block memory writes", bank->bus_width);
1320 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1323 /* flash write code */
1324 if (!cfi_info->write_algorithm)
1326 if (target_code_size > sizeof(target_code))
1328 LOG_WARNING("Internal error - target code buffer to small. "
1329 "Increase CFI_MAX_INTEL_CODESIZE and recompile.");
1330 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1332 cfi_fix_code_endian(target, target_code, target_code_src, target_code_size / 4);
1334 /* Get memory for block write handler */
1335 retval = target_alloc_working_area(target, target_code_size, &cfi_info->write_algorithm);
1336 if (retval != ERROR_OK)
1338 LOG_WARNING("No working area available, can't do block memory writes");
1339 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1342 /* write algorithm code to working area */
1343 retval = target_write_buffer(target, cfi_info->write_algorithm->address,
1344 target_code_size, target_code);
1345 if (retval != ERROR_OK)
1347 LOG_ERROR("Unable to write block write code to target");
1352 /* Get a workspace buffer for the data to flash starting with 32k size.
1353 Half size until buffer would be smaller 256 Bytem then fail back */
1354 /* FIXME Why 256 bytes, why not 32 bytes (smallest flash write page */
1355 while (target_alloc_working_area_try(target, buffer_size, &source) != ERROR_OK)
1358 if (buffer_size <= 256)
1360 LOG_WARNING("no large enough working area available, can't do block memory writes");
1361 retval = ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1366 /* setup algo registers */
1367 init_reg_param(®_params[0], "r0", 32, PARAM_OUT);
1368 init_reg_param(®_params[1], "r1", 32, PARAM_OUT);
1369 init_reg_param(®_params[2], "r2", 32, PARAM_OUT);
1370 init_reg_param(®_params[3], "r3", 32, PARAM_OUT);
1371 init_reg_param(®_params[4], "r4", 32, PARAM_IN);
1372 init_reg_param(®_params[5], "r5", 32, PARAM_OUT);
1373 init_reg_param(®_params[6], "r6", 32, PARAM_OUT);
1375 /* prepare command and status register patterns */
1376 write_command_val = cfi_command_val(bank, 0x40);
1377 busy_pattern_val = cfi_command_val(bank, 0x80);
1378 error_pattern_val = cfi_command_val(bank, 0x7e);
1380 LOG_DEBUG("Using target buffer at 0x%08" PRIx32 " and of size 0x%04" PRIx32,
1381 source->address, buffer_size);
1383 /* Programming main loop */
1386 uint32_t thisrun_count = (count > buffer_size) ? buffer_size : count;
1389 if ((retval = target_write_buffer(target, source->address,
1390 thisrun_count, buffer)) != ERROR_OK)
1395 buf_set_u32(reg_params[0].value, 0, 32, source->address);
1396 buf_set_u32(reg_params[1].value, 0, 32, address);
1397 buf_set_u32(reg_params[2].value, 0, 32, thisrun_count / bank->bus_width);
1399 buf_set_u32(reg_params[3].value, 0, 32, write_command_val);
1400 buf_set_u32(reg_params[5].value, 0, 32, busy_pattern_val);
1401 buf_set_u32(reg_params[6].value, 0, 32, error_pattern_val);
1403 LOG_DEBUG("Write 0x%04" PRIx32 " bytes to flash at 0x%08" PRIx32 , thisrun_count, address);
1405 /* Execute algorithm, assume breakpoint for last instruction */
1406 retval = target_run_algorithm(target, 0, NULL, 7, reg_params,
1407 cfi_info->write_algorithm->address,
1408 cfi_info->write_algorithm->address + target_code_size - sizeof(uint32_t),
1409 10000, /* 10s should be enough for max. 32k of data */
1412 /* On failure try a fall back to direct word writes */
1413 if (retval != ERROR_OK)
1415 cfi_intel_clear_status_register(bank);
1416 LOG_ERROR("Execution of flash algorythm failed. Can't fall back. Please report.");
1417 retval = ERROR_FLASH_OPERATION_FAILED;
1418 /* retval = ERROR_TARGET_RESOURCE_NOT_AVAILABLE; */
1419 /* FIXME To allow fall back or recovery, we must save the actual status
1420 * somewhere, so that a higher level code can start recovery. */
1424 /* Check return value from algo code */
1425 wsm_error = buf_get_u32(reg_params[4].value, 0, 32) & error_pattern_val;
1428 /* read status register (outputs debug inforation) */
1430 cfi_intel_wait_status_busy(bank, 100, &status);
1431 cfi_intel_clear_status_register(bank);
1432 retval = ERROR_FLASH_OPERATION_FAILED;
1436 buffer += thisrun_count;
1437 address += thisrun_count;
1438 count -= thisrun_count;
1443 /* free up resources */
1446 target_free_working_area(target, source);
1448 if (cfi_info->write_algorithm)
1450 target_free_working_area(target, cfi_info->write_algorithm);
1451 cfi_info->write_algorithm = NULL;
1454 destroy_reg_param(®_params[0]);
1455 destroy_reg_param(®_params[1]);
1456 destroy_reg_param(®_params[2]);
1457 destroy_reg_param(®_params[3]);
1458 destroy_reg_param(®_params[4]);
1459 destroy_reg_param(®_params[5]);
1460 destroy_reg_param(®_params[6]);
1465 static int cfi_spansion_write_block(struct flash_bank *bank, uint8_t *buffer,
1466 uint32_t address, uint32_t count)
1468 struct cfi_flash_bank *cfi_info = bank->driver_priv;
1469 struct cfi_spansion_pri_ext *pri_ext = cfi_info->pri_ext;
1470 struct target *target = bank->target;
1471 struct reg_param reg_params[10];
1472 struct arm_algorithm armv4_5_info;
1473 struct working_area *source;
1474 uint32_t buffer_size = 32768;
1476 int retval = ERROR_OK;
1478 /* input parameters - */
1479 /* R0 = source address */
1480 /* R1 = destination address */
1481 /* R2 = number of writes */
1482 /* R3 = flash write command */
1483 /* R4 = constant to mask DQ7 bits (also used for Dq5 with shift) */
1484 /* output parameters - */
1485 /* R5 = 0x80 ok 0x00 bad */
1486 /* temp registers - */
1487 /* R6 = value read from flash to test status */
1488 /* R7 = holding register */
1489 /* unlock registers - */
1490 /* R8 = unlock1_addr */
1491 /* R9 = unlock1_cmd */
1492 /* R10 = unlock2_addr */
1493 /* R11 = unlock2_cmd */
1495 /* see contib/loaders/flash/armv4_5_cfi_span_32.s for src */
1496 static const uint32_t armv4_5_word_32_code[] = {
1497 /* 00008100 <sp_32_code>: */
1498 0xe4905004, /* ldr r5, [r0], #4 */
1499 0xe5889000, /* str r9, [r8] */
1500 0xe58ab000, /* str r11, [r10] */
1501 0xe5883000, /* str r3, [r8] */
1502 0xe5815000, /* str r5, [r1] */
1503 0xe1a00000, /* nop */
1505 /* 00008110 <sp_32_busy>: */
1506 0xe5916000, /* ldr r6, [r1] */
1507 0xe0257006, /* eor r7, r5, r6 */
1508 0xe0147007, /* ands r7, r4, r7 */
1509 0x0a000007, /* beq 8140 <sp_32_cont> ; b if DQ7 == Data7 */
1510 0xe0166124, /* ands r6, r6, r4, lsr #2 */
1511 0x0afffff9, /* beq 8110 <sp_32_busy> ; b if DQ5 low */
1512 0xe5916000, /* ldr r6, [r1] */
1513 0xe0257006, /* eor r7, r5, r6 */
1514 0xe0147007, /* ands r7, r4, r7 */
1515 0x0a000001, /* beq 8140 <sp_32_cont> ; b if DQ7 == Data7 */
1516 0xe3a05000, /* mov r5, #0 ; 0x0 - return 0x00, error */
1517 0x1a000004, /* bne 8154 <sp_32_done> */
1519 /* 00008140 <sp_32_cont>: */
1520 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
1521 0x03a05080, /* moveq r5, #128 ; 0x80 */
1522 0x0a000001, /* beq 8154 <sp_32_done> */
1523 0xe2811004, /* add r1, r1, #4 ; 0x4 */
1524 0xeaffffe8, /* b 8100 <sp_32_code> */
1526 /* 00008154 <sp_32_done>: */
1527 0xeafffffe /* b 8154 <sp_32_done> */
1530 /* see contib/loaders/flash/armv4_5_cfi_span_16.s for src */
1531 static const uint32_t armv4_5_word_16_code[] = {
1532 /* 00008158 <sp_16_code>: */
1533 0xe0d050b2, /* ldrh r5, [r0], #2 */
1534 0xe1c890b0, /* strh r9, [r8] */
1535 0xe1cab0b0, /* strh r11, [r10] */
1536 0xe1c830b0, /* strh r3, [r8] */
1537 0xe1c150b0, /* strh r5, [r1] */
1538 0xe1a00000, /* nop (mov r0,r0) */
1540 /* 00008168 <sp_16_busy>: */
1541 0xe1d160b0, /* ldrh r6, [r1] */
1542 0xe0257006, /* eor r7, r5, r6 */
1543 0xe0147007, /* ands r7, r4, r7 */
1544 0x0a000007, /* beq 8198 <sp_16_cont> */
1545 0xe0166124, /* ands r6, r6, r4, lsr #2 */
1546 0x0afffff9, /* beq 8168 <sp_16_busy> */
1547 0xe1d160b0, /* ldrh r6, [r1] */
1548 0xe0257006, /* eor r7, r5, r6 */
1549 0xe0147007, /* ands r7, r4, r7 */
1550 0x0a000001, /* beq 8198 <sp_16_cont> */
1551 0xe3a05000, /* mov r5, #0 ; 0x0 */
1552 0x1a000004, /* bne 81ac <sp_16_done> */
1554 /* 00008198 <sp_16_cont>: */
1555 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
1556 0x03a05080, /* moveq r5, #128 ; 0x80 */
1557 0x0a000001, /* beq 81ac <sp_16_done> */
1558 0xe2811002, /* add r1, r1, #2 ; 0x2 */
1559 0xeaffffe8, /* b 8158 <sp_16_code> */
1561 /* 000081ac <sp_16_done>: */
1562 0xeafffffe /* b 81ac <sp_16_done> */
1565 /* see contib/loaders/flash/armv7m_cfi_span_16.s for src */
1566 static const uint32_t armv7m_word_16_code[] = {
1587 /* see contib/loaders/flash/armv4_5_cfi_span_16_dq7.s for src */
1588 static const uint32_t armv4_5_word_16_code_dq7only[] = {
1590 0xe0d050b2, /* ldrh r5, [r0], #2 */
1591 0xe1c890b0, /* strh r9, [r8] */
1592 0xe1cab0b0, /* strh r11, [r10] */
1593 0xe1c830b0, /* strh r3, [r8] */
1594 0xe1c150b0, /* strh r5, [r1] */
1595 0xe1a00000, /* nop (mov r0,r0) */
1598 0xe1d160b0, /* ldrh r6, [r1] */
1599 0xe0257006, /* eor r7, r5, r6 */
1600 0xe2177080, /* ands r7, #0x80 */
1601 0x1afffffb, /* bne 8168 <sp_16_busy> */
1603 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
1604 0x03a05080, /* moveq r5, #128 ; 0x80 */
1605 0x0a000001, /* beq 81ac <sp_16_done> */
1606 0xe2811002, /* add r1, r1, #2 ; 0x2 */
1607 0xeafffff0, /* b 8158 <sp_16_code> */
1609 /* 000081ac <sp_16_done>: */
1610 0xeafffffe /* b 81ac <sp_16_done> */
1613 /* see contib/loaders/flash/armv4_5_cfi_span_8.s for src */
1614 static const uint32_t armv4_5_word_8_code[] = {
1615 /* 000081b0 <sp_16_code_end>: */
1616 0xe4d05001, /* ldrb r5, [r0], #1 */
1617 0xe5c89000, /* strb r9, [r8] */
1618 0xe5cab000, /* strb r11, [r10] */
1619 0xe5c83000, /* strb r3, [r8] */
1620 0xe5c15000, /* strb r5, [r1] */
1621 0xe1a00000, /* nop (mov r0,r0) */
1623 /* 000081c0 <sp_8_busy>: */
1624 0xe5d16000, /* ldrb r6, [r1] */
1625 0xe0257006, /* eor r7, r5, r6 */
1626 0xe0147007, /* ands r7, r4, r7 */
1627 0x0a000007, /* beq 81f0 <sp_8_cont> */
1628 0xe0166124, /* ands r6, r6, r4, lsr #2 */
1629 0x0afffff9, /* beq 81c0 <sp_8_busy> */
1630 0xe5d16000, /* ldrb r6, [r1] */
1631 0xe0257006, /* eor r7, r5, r6 */
1632 0xe0147007, /* ands r7, r4, r7 */
1633 0x0a000001, /* beq 81f0 <sp_8_cont> */
1634 0xe3a05000, /* mov r5, #0 ; 0x0 */
1635 0x1a000004, /* bne 8204 <sp_8_done> */
1637 /* 000081f0 <sp_8_cont>: */
1638 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
1639 0x03a05080, /* moveq r5, #128 ; 0x80 */
1640 0x0a000001, /* beq 8204 <sp_8_done> */
1641 0xe2811001, /* add r1, r1, #1 ; 0x1 */
1642 0xeaffffe8, /* b 81b0 <sp_16_code_end> */
1644 /* 00008204 <sp_8_done>: */
1645 0xeafffffe /* b 8204 <sp_8_done> */
1648 if (is_armv7m(target_to_armv7m(target))) /* Cortex-M3 target */
1650 armv4_5_info.common_magic = ARMV7M_COMMON_MAGIC;
1651 armv4_5_info.core_mode = ARMV7M_MODE_HANDLER;
1652 armv4_5_info.core_state = ARM_STATE_ARM;
1656 /* All other ARM CPUs have 32 bit instructions */
1657 armv4_5_info.common_magic = ARM_COMMON_MAGIC;
1658 armv4_5_info.core_mode = ARM_MODE_SVC;
1659 armv4_5_info.core_state = ARM_STATE_ARM;
1662 int target_code_size = 0;
1663 const uint32_t *target_code_src = NULL;
1665 switch (bank->bus_width)
1668 if(armv4_5_info.common_magic == ARM_COMMON_MAGIC) /* armv4_5 target */
1670 target_code_src = armv4_5_word_8_code;
1671 target_code_size = sizeof(armv4_5_word_8_code);
1675 /* Check for DQ5 support */
1676 if( cfi_info->status_poll_mask & (1 << 5) )
1678 if(armv4_5_info.common_magic == ARM_COMMON_MAGIC) /* armv4_5 target */
1680 target_code_src = armv4_5_word_16_code;
1681 target_code_size = sizeof(armv4_5_word_16_code);
1683 else if (armv4_5_info.common_magic == ARMV7M_COMMON_MAGIC) /* cortex-m3 target */
1685 target_code_src = armv7m_word_16_code;
1686 target_code_size = sizeof(armv7m_word_16_code);
1691 /* No DQ5 support. Use DQ7 DATA# polling only. */
1692 if(armv4_5_info.common_magic == ARM_COMMON_MAGIC) // armv4_5 target
1694 target_code_src = armv4_5_word_16_code_dq7only;
1695 target_code_size = sizeof(armv4_5_word_16_code_dq7only);
1700 if(armv4_5_info.common_magic == ARM_COMMON_MAGIC) // armv4_5 target
1702 target_code_src = armv4_5_word_32_code;
1703 target_code_size = sizeof(armv4_5_word_32_code);
1707 LOG_ERROR("Unsupported bank buswidth %d, can't do block memory writes", bank->bus_width);
1708 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1711 /* flash write code */
1712 if (!cfi_info->write_algorithm)
1714 uint8_t *target_code;
1716 /* convert bus-width dependent algorithm code to correct endiannes */
1717 target_code = malloc(target_code_size);
1718 if (target_code == NULL)
1720 LOG_ERROR("Out of memory");
1723 cfi_fix_code_endian(target, target_code, target_code_src, target_code_size / 4);
1725 /* allocate working area */
1726 retval = target_alloc_working_area(target, target_code_size,
1727 &cfi_info->write_algorithm);
1728 if (retval != ERROR_OK)
1734 /* write algorithm code to working area */
1735 if ((retval = target_write_buffer(target, cfi_info->write_algorithm->address,
1736 target_code_size, target_code)) != ERROR_OK)
1744 /* the following code still assumes target code is fixed 24*4 bytes */
1746 while (target_alloc_working_area_try(target, buffer_size, &source) != ERROR_OK)
1749 if (buffer_size <= 256)
1751 /* if we already allocated the writing code, but failed to get a
1752 * buffer, free the algorithm */
1753 if (cfi_info->write_algorithm)
1754 target_free_working_area(target, cfi_info->write_algorithm);
1756 LOG_WARNING("not enough working area available, can't do block memory writes");
1757 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1761 init_reg_param(®_params[0], "r0", 32, PARAM_OUT);
1762 init_reg_param(®_params[1], "r1", 32, PARAM_OUT);
1763 init_reg_param(®_params[2], "r2", 32, PARAM_OUT);
1764 init_reg_param(®_params[3], "r3", 32, PARAM_OUT);
1765 init_reg_param(®_params[4], "r4", 32, PARAM_OUT);
1766 init_reg_param(®_params[5], "r5", 32, PARAM_IN);
1767 init_reg_param(®_params[6], "r8", 32, PARAM_OUT);
1768 init_reg_param(®_params[7], "r9", 32, PARAM_OUT);
1769 init_reg_param(®_params[8], "r10", 32, PARAM_OUT);
1770 init_reg_param(®_params[9], "r11", 32, PARAM_OUT);
1774 uint32_t thisrun_count = (count > buffer_size) ? buffer_size : count;
1776 retval = target_write_buffer(target, source->address, thisrun_count, buffer);
1777 if (retval != ERROR_OK)
1782 buf_set_u32(reg_params[0].value, 0, 32, source->address);
1783 buf_set_u32(reg_params[1].value, 0, 32, address);
1784 buf_set_u32(reg_params[2].value, 0, 32, thisrun_count / bank->bus_width);
1785 buf_set_u32(reg_params[3].value, 0, 32, cfi_command_val(bank, 0xA0));
1786 buf_set_u32(reg_params[4].value, 0, 32, cfi_command_val(bank, 0x80));
1787 buf_set_u32(reg_params[6].value, 0, 32, flash_address(bank, 0, pri_ext->_unlock1));
1788 buf_set_u32(reg_params[7].value, 0, 32, 0xaaaaaaaa);
1789 buf_set_u32(reg_params[8].value, 0, 32, flash_address(bank, 0, pri_ext->_unlock2));
1790 buf_set_u32(reg_params[9].value, 0, 32, 0x55555555);
1792 retval = target_run_algorithm(target, 0, NULL, 10, reg_params,
1793 cfi_info->write_algorithm->address,
1794 cfi_info->write_algorithm->address + ((target_code_size) - 4),
1795 10000, &armv4_5_info);
1796 if (retval != ERROR_OK)
1801 status = buf_get_u32(reg_params[5].value, 0, 32);
1804 LOG_ERROR("flash write block failed status: 0x%" PRIx32 , status);
1805 retval = ERROR_FLASH_OPERATION_FAILED;
1809 buffer += thisrun_count;
1810 address += thisrun_count;
1811 count -= thisrun_count;
1814 target_free_all_working_areas(target);
1816 destroy_reg_param(®_params[0]);
1817 destroy_reg_param(®_params[1]);
1818 destroy_reg_param(®_params[2]);
1819 destroy_reg_param(®_params[3]);
1820 destroy_reg_param(®_params[4]);
1821 destroy_reg_param(®_params[5]);
1822 destroy_reg_param(®_params[6]);
1823 destroy_reg_param(®_params[7]);
1824 destroy_reg_param(®_params[8]);
1825 destroy_reg_param(®_params[9]);
1830 static int cfi_intel_write_word(struct flash_bank *bank, uint8_t *word, uint32_t address)
1833 struct cfi_flash_bank *cfi_info = bank->driver_priv;
1834 struct target *target = bank->target;
1836 cfi_intel_clear_status_register(bank);
1837 if ((retval = cfi_send_command(bank, 0x40, address)) != ERROR_OK)
1842 if ((retval = target_write_memory(target, address, bank->bus_width, 1, word)) != ERROR_OK)
1848 retval = cfi_intel_wait_status_busy(bank, cfi_info->word_write_timeout, &status);
1851 if ((retval = cfi_send_command(bank, 0xff, flash_address(bank, 0, 0x0))) != ERROR_OK)
1856 LOG_ERROR("couldn't write word at base 0x%" PRIx32 ", address 0x%" PRIx32,
1857 bank->base, address);
1858 return ERROR_FLASH_OPERATION_FAILED;
1864 static int cfi_intel_write_words(struct flash_bank *bank, uint8_t *word,
1865 uint32_t wordcount, uint32_t address)
1868 struct cfi_flash_bank *cfi_info = bank->driver_priv;
1869 struct target *target = bank->target;
1871 /* Calculate buffer size and boundary mask */
1872 /* buffersize is (buffer size per chip) * (number of chips) */
1873 /* bufferwsize is buffersize in words */
1874 uint32_t buffersize = (1UL << cfi_info->max_buf_write_size) * (bank->bus_width / bank->chip_width);
1875 uint32_t buffermask = buffersize-1;
1876 uint32_t bufferwsize = buffersize / bank->bus_width;
1878 /* Check for valid range */
1879 if (address & buffermask)
1881 LOG_ERROR("Write address at base 0x%" PRIx32 ", address 0x%" PRIx32
1882 " not aligned to 2^%d boundary",
1883 bank->base, address, cfi_info->max_buf_write_size);
1884 return ERROR_FLASH_OPERATION_FAILED;
1887 /* Check for valid size */
1888 if (wordcount > bufferwsize)
1890 LOG_ERROR("Number of data words %" PRId32 " exceeds available buffersize %" PRId32,
1891 wordcount, buffersize);
1892 return ERROR_FLASH_OPERATION_FAILED;
1895 /* Write to flash buffer */
1896 cfi_intel_clear_status_register(bank);
1898 /* Initiate buffer operation _*/
1899 if ((retval = cfi_send_command(bank, 0xe8, address)) != ERROR_OK)
1904 retval = cfi_intel_wait_status_busy(bank, cfi_info->buf_write_timeout, &status);
1905 if (retval != ERROR_OK)
1909 if ((retval = cfi_send_command(bank, 0xff, flash_address(bank, 0, 0x0))) != ERROR_OK)
1914 LOG_ERROR("couldn't start buffer write operation at base 0x%" PRIx32 ", address 0x%" PRIx32,
1915 bank->base, address);
1916 return ERROR_FLASH_OPERATION_FAILED;
1919 /* Write buffer wordcount-1 and data words */
1920 if ((retval = cfi_send_command(bank, bufferwsize-1, address)) != ERROR_OK)
1925 if ((retval = target_write_memory(target,
1926 address, bank->bus_width, bufferwsize, word)) != ERROR_OK)
1931 /* Commit write operation */
1932 if ((retval = cfi_send_command(bank, 0xd0, address)) != ERROR_OK)
1937 retval = cfi_intel_wait_status_busy(bank, cfi_info->buf_write_timeout, &status);
1938 if (retval != ERROR_OK)
1943 if ((retval = cfi_send_command(bank, 0xff,
1944 flash_address(bank, 0, 0x0))) != ERROR_OK)
1949 LOG_ERROR("Buffer write at base 0x%" PRIx32
1950 ", address 0x%" PRIx32 " failed.", bank->base, address);
1951 return ERROR_FLASH_OPERATION_FAILED;
1957 static int cfi_spansion_write_word(struct flash_bank *bank, uint8_t *word, uint32_t address)
1960 struct cfi_flash_bank *cfi_info = bank->driver_priv;
1961 struct cfi_spansion_pri_ext *pri_ext = cfi_info->pri_ext;
1962 struct target *target = bank->target;
1964 if ((retval = cfi_send_command(bank, 0xaa,
1965 flash_address(bank, 0, pri_ext->_unlock1))) != ERROR_OK)
1970 if ((retval = cfi_send_command(bank, 0x55,
1971 flash_address(bank, 0, pri_ext->_unlock2))) != ERROR_OK)
1976 if ((retval = cfi_send_command(bank, 0xa0,
1977 flash_address(bank, 0, pri_ext->_unlock1))) != ERROR_OK)
1982 if ((retval = target_write_memory(target,
1983 address, bank->bus_width, 1, word)) != ERROR_OK)
1988 if (cfi_spansion_wait_status_busy(bank, cfi_info->word_write_timeout) != ERROR_OK)
1990 if ((retval = cfi_send_command(bank, 0xf0, flash_address(bank, 0, 0x0))) != ERROR_OK)
1995 LOG_ERROR("couldn't write word at base 0x%" PRIx32
1996 ", address 0x%" PRIx32 , bank->base, address);
1997 return ERROR_FLASH_OPERATION_FAILED;
2003 static int cfi_spansion_write_words(struct flash_bank *bank, uint8_t *word,
2004 uint32_t wordcount, uint32_t address)
2007 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2008 struct target *target = bank->target;
2009 struct cfi_spansion_pri_ext *pri_ext = cfi_info->pri_ext;
2011 /* Calculate buffer size and boundary mask */
2012 /* buffersize is (buffer size per chip) * (number of chips) */
2013 /* bufferwsize is buffersize in words */
2014 uint32_t buffersize = (1UL << cfi_info->max_buf_write_size) * (bank->bus_width / bank->chip_width);
2015 uint32_t buffermask = buffersize-1;
2016 uint32_t bufferwsize = buffersize / bank->bus_width;
2018 /* Check for valid range */
2019 if (address & buffermask)
2021 LOG_ERROR("Write address at base 0x%" PRIx32
2022 ", address 0x%" PRIx32 " not aligned to 2^%d boundary",
2023 bank->base, address, cfi_info->max_buf_write_size);
2024 return ERROR_FLASH_OPERATION_FAILED;
2027 /* Check for valid size */
2028 if (wordcount > bufferwsize)
2030 LOG_ERROR("Number of data words %" PRId32 " exceeds available buffersize %"
2031 PRId32, wordcount, buffersize);
2032 return ERROR_FLASH_OPERATION_FAILED;
2036 if ((retval = cfi_send_command(bank, 0xaa,
2037 flash_address(bank, 0, pri_ext->_unlock1))) != ERROR_OK)
2042 if ((retval = cfi_send_command(bank, 0x55,
2043 flash_address(bank, 0, pri_ext->_unlock2))) != ERROR_OK)
2048 /* Buffer load command */
2049 if ((retval = cfi_send_command(bank, 0x25, address)) != ERROR_OK)
2054 /* Write buffer wordcount-1 and data words */
2055 if ((retval = cfi_send_command(bank, bufferwsize-1, address)) != ERROR_OK)
2060 if ((retval = target_write_memory(target,
2061 address, bank->bus_width, bufferwsize, word)) != ERROR_OK)
2066 /* Commit write operation */
2067 if ((retval = cfi_send_command(bank, 0x29, address)) != ERROR_OK)
2072 if (cfi_spansion_wait_status_busy(bank, cfi_info->buf_write_timeout) != ERROR_OK)
2074 if ((retval = cfi_send_command(bank, 0xf0,
2075 flash_address(bank, 0, 0x0))) != ERROR_OK)
2080 LOG_ERROR("couldn't write block at base 0x%" PRIx32
2081 ", address 0x%" PRIx32 ", size 0x%" PRIx32, bank->base, address, bufferwsize);
2082 return ERROR_FLASH_OPERATION_FAILED;
2088 static int cfi_write_word(struct flash_bank *bank, uint8_t *word, uint32_t address)
2090 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2092 switch (cfi_info->pri_id)
2096 return cfi_intel_write_word(bank, word, address);
2099 return cfi_spansion_write_word(bank, word, address);
2102 LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
2106 return ERROR_FLASH_OPERATION_FAILED;
2109 static int cfi_write_words(struct flash_bank *bank, uint8_t *word,
2110 uint32_t wordcount, uint32_t address)
2112 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2114 if (cfi_info->buf_write_timeout_typ == 0)
2116 /* buffer writes are not supported */
2117 LOG_DEBUG("Buffer Writes Not Supported");
2118 return ERROR_FLASH_OPER_UNSUPPORTED;
2121 switch (cfi_info->pri_id)
2125 return cfi_intel_write_words(bank, word, wordcount, address);
2128 return cfi_spansion_write_words(bank, word, wordcount, address);
2131 LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
2135 return ERROR_FLASH_OPERATION_FAILED;
2138 static int cfi_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
2140 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2141 struct target *target = bank->target;
2142 uint32_t address = bank->base + offset;
2144 int align; /* number of unaligned bytes */
2145 uint8_t current_word[CFI_MAX_BUS_WIDTH];
2149 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
2150 (int)count, (unsigned)offset);
2152 if (bank->target->state != TARGET_HALTED)
2154 LOG_ERROR("Target not halted");
2155 return ERROR_TARGET_NOT_HALTED;
2158 if (offset + count > bank->size)
2159 return ERROR_FLASH_DST_OUT_OF_BANK;
2161 if (cfi_info->qry[0] != 'Q')
2162 return ERROR_FLASH_BANK_NOT_PROBED;
2164 /* start at the first byte of the first word (bus_width size) */
2165 read_p = address & ~(bank->bus_width - 1);
2166 if ((align = address - read_p) != 0)
2168 LOG_INFO("Fixup %d unaligned read head bytes", align);
2170 /* read a complete word from flash */
2171 if ((retval = target_read_memory(target, read_p,
2172 bank->bus_width, 1, current_word)) != ERROR_OK)
2175 /* take only bytes we need */
2176 for (i = align; (i < bank->bus_width) && (count > 0); i++, count--)
2177 *buffer++ = current_word[i];
2179 read_p += bank->bus_width;
2182 align = count / bank->bus_width;
2185 if ((retval = target_read_memory(target, read_p,
2186 bank->bus_width, align, buffer)) != ERROR_OK)
2189 read_p += align * bank->bus_width;
2190 buffer += align * bank->bus_width;
2191 count -= align * bank->bus_width;
2196 LOG_INFO("Fixup %d unaligned read tail bytes", count);
2198 /* read a complete word from flash */
2199 if ((retval = target_read_memory(target, read_p,
2200 bank->bus_width, 1, current_word)) != ERROR_OK)
2203 /* take only bytes we need */
2204 for (i = 0; (i < bank->bus_width) && (count > 0); i++, count--)
2205 *buffer++ = current_word[i];
2211 static int cfi_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
2213 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2214 struct target *target = bank->target;
2215 uint32_t address = bank->base + offset; /* address of first byte to be programmed */
2217 int align; /* number of unaligned bytes */
2218 int blk_count; /* number of bus_width bytes for block copy */
2219 uint8_t current_word[CFI_MAX_BUS_WIDTH * 4]; /* word (bus_width size) currently being programmed */
2223 if (bank->target->state != TARGET_HALTED)
2225 LOG_ERROR("Target not halted");
2226 return ERROR_TARGET_NOT_HALTED;
2229 if (offset + count > bank->size)
2230 return ERROR_FLASH_DST_OUT_OF_BANK;
2232 if (cfi_info->qry[0] != 'Q')
2233 return ERROR_FLASH_BANK_NOT_PROBED;
2235 /* start at the first byte of the first word (bus_width size) */
2236 write_p = address & ~(bank->bus_width - 1);
2237 if ((align = address - write_p) != 0)
2239 LOG_INFO("Fixup %d unaligned head bytes", align);
2241 /* read a complete word from flash */
2242 if ((retval = target_read_memory(target, write_p,
2243 bank->bus_width, 1, current_word)) != ERROR_OK)
2246 /* replace only bytes that must be written */
2247 for (i = align; (i < bank->bus_width) && (count > 0); i++, count--)
2248 current_word[i] = *buffer++;
2250 retval = cfi_write_word(bank, current_word, write_p);
2251 if (retval != ERROR_OK)
2253 write_p += bank->bus_width;
2256 /* handle blocks of bus_size aligned bytes */
2257 blk_count = count & ~(bank->bus_width - 1); /* round down, leave tail bytes */
2258 switch (cfi_info->pri_id)
2260 /* try block writes (fails without working area) */
2263 retval = cfi_intel_write_block(bank, buffer, write_p, blk_count);
2266 retval = cfi_spansion_write_block(bank, buffer, write_p, blk_count);
2269 LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
2270 retval = ERROR_FLASH_OPERATION_FAILED;
2273 if (retval == ERROR_OK)
2275 /* Increment pointers and decrease count on succesful block write */
2276 buffer += blk_count;
2277 write_p += blk_count;
2282 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
2284 /* Calculate buffer size and boundary mask */
2285 /* buffersize is (buffer size per chip) * (number of chips) */
2286 /* bufferwsize is buffersize in words */
2287 uint32_t buffersize = (1UL << cfi_info->max_buf_write_size) * (bank->bus_width / bank->chip_width);
2288 uint32_t buffermask = buffersize-1;
2289 uint32_t bufferwsize = buffersize / bank->bus_width;
2291 /* fall back to memory writes */
2292 while (count >= (uint32_t)bank->bus_width)
2295 if ((write_p & 0xff) == 0)
2297 LOG_INFO("Programming at 0x%08" PRIx32 ", count 0x%08"
2298 PRIx32 " bytes remaining", write_p, count);
2301 if ((bufferwsize > 0) && (count >= buffersize) && !(write_p & buffermask))
2303 retval = cfi_write_words(bank, buffer, bufferwsize, write_p);
2304 if (retval == ERROR_OK)
2306 buffer += buffersize;
2307 write_p += buffersize;
2308 count -= buffersize;
2311 else if (retval != ERROR_FLASH_OPER_UNSUPPORTED)
2314 /* try the slow way? */
2317 for (i = 0; i < bank->bus_width; i++)
2318 current_word[i] = *buffer++;
2320 retval = cfi_write_word(bank, current_word, write_p);
2321 if (retval != ERROR_OK)
2324 write_p += bank->bus_width;
2325 count -= bank->bus_width;
2333 /* return to read array mode, so we can read from flash again for padding */
2334 if ((retval = cfi_reset(bank)) != ERROR_OK)
2339 /* handle unaligned tail bytes */
2342 LOG_INFO("Fixup %" PRId32 " unaligned tail bytes", count);
2344 /* read a complete word from flash */
2345 if ((retval = target_read_memory(target, write_p,
2346 bank->bus_width, 1, current_word)) != ERROR_OK)
2349 /* replace only bytes that must be written */
2350 for (i = 0; (i < bank->bus_width) && (count > 0); i++, count--)
2351 current_word[i] = *buffer++;
2353 retval = cfi_write_word(bank, current_word, write_p);
2354 if (retval != ERROR_OK)
2358 /* return to read array mode */
2359 return cfi_reset(bank);
2362 static void cfi_fixup_reversed_erase_regions(struct flash_bank *bank, void *param)
2365 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2366 struct cfi_spansion_pri_ext *pri_ext = cfi_info->pri_ext;
2368 pri_ext->_reversed_geometry = 1;
2371 static void cfi_fixup_0002_erase_regions(struct flash_bank *bank, void *param)
2374 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2375 struct cfi_spansion_pri_ext *pri_ext = cfi_info->pri_ext;
2378 if ((pri_ext->_reversed_geometry) || (pri_ext->TopBottom == 3))
2380 LOG_DEBUG("swapping reversed erase region information on cmdset 0002 device");
2382 for (i = 0; i < cfi_info->num_erase_regions / 2; i++)
2384 int j = (cfi_info->num_erase_regions - 1) - i;
2387 swap = cfi_info->erase_region_info[i];
2388 cfi_info->erase_region_info[i] = cfi_info->erase_region_info[j];
2389 cfi_info->erase_region_info[j] = swap;
2394 static void cfi_fixup_0002_unlock_addresses(struct flash_bank *bank, void *param)
2396 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2397 struct cfi_spansion_pri_ext *pri_ext = cfi_info->pri_ext;
2398 struct cfi_unlock_addresses *unlock_addresses = param;
2400 pri_ext->_unlock1 = unlock_addresses->unlock1;
2401 pri_ext->_unlock2 = unlock_addresses->unlock2;
2405 static int cfi_query_string(struct flash_bank *bank, int address)
2407 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2410 if ((retval = cfi_send_command(bank, 0x98, flash_address(bank, 0, address))) != ERROR_OK)
2415 retval = cfi_query_u8(bank, 0, 0x10, &cfi_info->qry[0]);
2416 if (retval != ERROR_OK)
2418 retval = cfi_query_u8(bank, 0, 0x11, &cfi_info->qry[1]);
2419 if (retval != ERROR_OK)
2421 retval = cfi_query_u8(bank, 0, 0x12, &cfi_info->qry[2]);
2422 if (retval != ERROR_OK)
2425 LOG_DEBUG("CFI qry returned: 0x%2.2x 0x%2.2x 0x%2.2x",
2426 cfi_info->qry[0], cfi_info->qry[1], cfi_info->qry[2]);
2428 if ((cfi_info->qry[0] != 'Q') || (cfi_info->qry[1] != 'R') || (cfi_info->qry[2] != 'Y'))
2430 if ((retval = cfi_reset(bank)) != ERROR_OK)
2434 LOG_ERROR("Could not probe bank: no QRY");
2435 return ERROR_FLASH_BANK_INVALID;
2441 static int cfi_probe(struct flash_bank *bank)
2443 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2444 struct target *target = bank->target;
2445 int num_sectors = 0;
2448 uint32_t unlock1 = 0x555;
2449 uint32_t unlock2 = 0x2aa;
2451 uint8_t value_buf0[CFI_MAX_BUS_WIDTH], value_buf1[CFI_MAX_BUS_WIDTH];
2453 if (bank->target->state != TARGET_HALTED)
2455 LOG_ERROR("Target not halted");
2456 return ERROR_TARGET_NOT_HALTED;
2459 cfi_info->probed = 0;
2462 free(bank->sectors);
2463 bank->sectors = NULL;
2465 if(cfi_info->erase_region_info)
2467 free(cfi_info->erase_region_info);
2468 cfi_info->erase_region_info = NULL;
2471 /* JEDEC standard JESD21C uses 0x5555 and 0x2aaa as unlock addresses,
2472 * while CFI compatible AMD/Spansion flashes use 0x555 and 0x2aa
2474 if (cfi_info->jedec_probe)
2480 /* switch to read identifier codes mode ("AUTOSELECT") */
2481 if ((retval = cfi_send_command(bank, 0xaa, flash_address(bank, 0, unlock1))) != ERROR_OK)
2485 if ((retval = cfi_send_command(bank, 0x55, flash_address(bank, 0, unlock2))) != ERROR_OK)
2489 if ((retval = cfi_send_command(bank, 0x90, flash_address(bank, 0, unlock1))) != ERROR_OK)
2494 if ((retval = target_read_memory(target, flash_address(bank, 0, 0x00),
2495 bank->bus_width, 1, value_buf0)) != ERROR_OK)
2499 if ((retval = target_read_memory(target, flash_address(bank, 0, 0x01),
2500 bank->bus_width, 1, value_buf1)) != ERROR_OK)
2504 switch (bank->chip_width) {
2506 cfi_info->manufacturer = *value_buf0;
2507 cfi_info->device_id = *value_buf1;
2510 cfi_info->manufacturer = target_buffer_get_u16(target, value_buf0);
2511 cfi_info->device_id = target_buffer_get_u16(target, value_buf1);
2514 cfi_info->manufacturer = target_buffer_get_u32(target, value_buf0);
2515 cfi_info->device_id = target_buffer_get_u32(target, value_buf1);
2518 LOG_ERROR("Unsupported bank chipwidth %d, can't probe memory", bank->chip_width);
2519 return ERROR_FLASH_OPERATION_FAILED;
2522 LOG_INFO("Flash Manufacturer/Device: 0x%04x 0x%04x",
2523 cfi_info->manufacturer, cfi_info->device_id);
2524 /* switch back to read array mode */
2525 if ((retval = cfi_reset(bank)) != ERROR_OK)
2530 /* check device/manufacturer ID for known non-CFI flashes. */
2531 cfi_fixup_non_cfi(bank);
2533 /* query only if this is a CFI compatible flash,
2534 * otherwise the relevant info has already been filled in
2536 if (cfi_info->not_cfi == 0)
2538 /* enter CFI query mode
2539 * according to JEDEC Standard No. 68.01,
2540 * a single bus sequence with address = 0x55, data = 0x98 should put
2541 * the device into CFI query mode.
2543 * SST flashes clearly violate this, and we will consider them incompatbile for now
2546 retval = cfi_query_string(bank, 0x55);
2547 if (retval != ERROR_OK)
2550 * Spansion S29WS-N CFI query fix is to try 0x555 if 0x55 fails. Should
2551 * be harmless enough:
2553 * http://www.infradead.org/pipermail/linux-mtd/2005-September/013618.html
2555 LOG_USER("Try workaround w/0x555 instead of 0x55 to get QRY.");
2556 retval = cfi_query_string(bank, 0x555);
2558 if (retval != ERROR_OK)
2561 retval = cfi_query_u16(bank, 0, 0x13, &cfi_info->pri_id);
2562 if (retval != ERROR_OK)
2564 retval = cfi_query_u16(bank, 0, 0x15, &cfi_info->pri_addr);
2565 if (retval != ERROR_OK)
2567 retval = cfi_query_u16(bank, 0, 0x17, &cfi_info->alt_id);
2568 if (retval != ERROR_OK)
2570 retval = cfi_query_u16(bank, 0, 0x19, &cfi_info->alt_addr);
2571 if (retval != ERROR_OK)
2574 LOG_DEBUG("qry: '%c%c%c', pri_id: 0x%4.4x, pri_addr: 0x%4.4x, alt_id: "
2575 "0x%4.4x, alt_addr: 0x%4.4x", cfi_info->qry[0], cfi_info->qry[1],
2576 cfi_info->qry[2], cfi_info->pri_id, cfi_info->pri_addr,
2577 cfi_info->alt_id, cfi_info->alt_addr);
2579 retval = cfi_query_u8(bank, 0, 0x1b, &cfi_info->vcc_min);
2580 if (retval != ERROR_OK)
2582 retval = cfi_query_u8(bank, 0, 0x1c, &cfi_info->vcc_max);
2583 if (retval != ERROR_OK)
2585 retval = cfi_query_u8(bank, 0, 0x1d, &cfi_info->vpp_min);
2586 if (retval != ERROR_OK)
2588 retval = cfi_query_u8(bank, 0, 0x1e, &cfi_info->vpp_max);
2589 if (retval != ERROR_OK)
2592 retval = cfi_query_u8(bank, 0, 0x1f, &cfi_info->word_write_timeout_typ);
2593 if (retval != ERROR_OK)
2595 retval = cfi_query_u8(bank, 0, 0x20, &cfi_info->buf_write_timeout_typ);
2596 if (retval != ERROR_OK)
2598 retval = cfi_query_u8(bank, 0, 0x21, &cfi_info->block_erase_timeout_typ);
2599 if (retval != ERROR_OK)
2601 retval = cfi_query_u8(bank, 0, 0x22, &cfi_info->chip_erase_timeout_typ);
2602 if (retval != ERROR_OK)
2604 retval = cfi_query_u8(bank, 0, 0x23, &cfi_info->word_write_timeout_max);
2605 if (retval != ERROR_OK)
2607 retval = cfi_query_u8(bank, 0, 0x24, &cfi_info->buf_write_timeout_max);
2608 if (retval != ERROR_OK)
2610 retval = cfi_query_u8(bank, 0, 0x25, &cfi_info->block_erase_timeout_max);
2611 if (retval != ERROR_OK)
2613 retval = cfi_query_u8(bank, 0, 0x26, &cfi_info->chip_erase_timeout_max);
2614 if (retval != ERROR_OK)
2618 retval = cfi_query_u8(bank, 0, 0x27, &data);
2619 if (retval != ERROR_OK)
2621 cfi_info->dev_size = 1 << data;
2623 retval = cfi_query_u16(bank, 0, 0x28, &cfi_info->interface_desc);
2624 if (retval != ERROR_OK)
2626 retval = cfi_query_u16(bank, 0, 0x2a, &cfi_info->max_buf_write_size);
2627 if (retval != ERROR_OK)
2629 retval = cfi_query_u8(bank, 0, 0x2c, &cfi_info->num_erase_regions);
2630 if (retval != ERROR_OK)
2633 LOG_DEBUG("size: 0x%" PRIx32 ", interface desc: %i, max buffer write size: 0x%x",
2634 cfi_info->dev_size, cfi_info->interface_desc, (1 << cfi_info->max_buf_write_size));
2636 if (cfi_info->num_erase_regions)
2638 cfi_info->erase_region_info = malloc(sizeof(*cfi_info->erase_region_info)
2639 * cfi_info->num_erase_regions);
2640 for (i = 0; i < cfi_info->num_erase_regions; i++)
2642 retval = cfi_query_u32(bank, 0, 0x2d + (4 * i), &cfi_info->erase_region_info[i]);
2643 if (retval != ERROR_OK)
2645 LOG_DEBUG("erase region[%i]: %" PRIu32 " blocks of size 0x%" PRIx32 "", i,
2646 (cfi_info->erase_region_info[i] & 0xffff) + 1,
2647 (cfi_info->erase_region_info[i] >> 16) * 256);
2652 cfi_info->erase_region_info = NULL;
2655 /* We need to read the primary algorithm extended query table before calculating
2656 * the sector layout to be able to apply fixups
2658 switch (cfi_info->pri_id)
2660 /* Intel command set (standard and extended) */
2663 cfi_read_intel_pri_ext(bank);
2665 /* AMD/Spansion, Atmel, ... command set */
2667 cfi_info->status_poll_mask = CFI_STATUS_POLL_MASK_DQ5_DQ6_DQ7; /* default for all CFI flashs */
2668 cfi_read_0002_pri_ext(bank);
2671 LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
2675 /* return to read array mode
2676 * we use both reset commands, as some Intel flashes fail to recognize the 0xF0 command
2678 if ((retval = cfi_reset(bank)) != ERROR_OK)
2682 } /* end CFI case */
2684 LOG_DEBUG("Vcc min: %x.%x, Vcc max: %x.%x, Vpp min: %u.%x, Vpp max: %u.%x",
2685 (cfi_info->vcc_min & 0xf0) >> 4, cfi_info->vcc_min & 0x0f,
2686 (cfi_info->vcc_max & 0xf0) >> 4, cfi_info->vcc_max & 0x0f,
2687 (cfi_info->vpp_min & 0xf0) >> 4, cfi_info->vpp_min & 0x0f,
2688 (cfi_info->vpp_max & 0xf0) >> 4, cfi_info->vpp_max & 0x0f);
2690 LOG_DEBUG("typ. word write timeout: %u us, typ. buf write timeout: %u us, "
2691 "typ. block erase timeout: %u ms, typ. chip erase timeout: %u ms",
2692 1 << cfi_info->word_write_timeout_typ, 1 << cfi_info->buf_write_timeout_typ,
2693 1 << cfi_info->block_erase_timeout_typ, 1 << cfi_info->chip_erase_timeout_typ);
2695 LOG_DEBUG("max. word write timeout: %u us, max. buf write timeout: %u us, "
2696 "max. block erase timeout: %u ms, max. chip erase timeout: %u ms",
2697 (1 << cfi_info->word_write_timeout_max) * (1 << cfi_info->word_write_timeout_typ),
2698 (1 << cfi_info->buf_write_timeout_max) * (1 << cfi_info->buf_write_timeout_typ),
2699 (1 << cfi_info->block_erase_timeout_max) * (1 << cfi_info->block_erase_timeout_typ),
2700 (1 << cfi_info->chip_erase_timeout_max) * (1 << cfi_info->chip_erase_timeout_typ));
2702 /* convert timeouts to real values in ms */
2703 cfi_info->word_write_timeout = DIV_ROUND_UP((1L << cfi_info->word_write_timeout_typ) *
2704 (1L << cfi_info->word_write_timeout_max), 1000);
2705 cfi_info->buf_write_timeout = DIV_ROUND_UP((1L << cfi_info->buf_write_timeout_typ) *
2706 (1L << cfi_info->buf_write_timeout_max), 1000);
2707 cfi_info->block_erase_timeout = (1L << cfi_info->block_erase_timeout_typ) *
2708 (1L << cfi_info->block_erase_timeout_max);
2709 cfi_info->chip_erase_timeout = (1L << cfi_info->chip_erase_timeout_typ) *
2710 (1L << cfi_info->chip_erase_timeout_max);
2712 LOG_DEBUG("calculated word write timeout: %u ms, buf write timeout: %u ms, "
2713 "block erase timeout: %u ms, chip erase timeout: %u ms",
2714 cfi_info->word_write_timeout, cfi_info->buf_write_timeout,
2715 cfi_info->block_erase_timeout, cfi_info->chip_erase_timeout);
2717 /* apply fixups depending on the primary command set */
2718 switch (cfi_info->pri_id)
2720 /* Intel command set (standard and extended) */
2723 cfi_fixup(bank, cfi_0001_fixups);
2725 /* AMD/Spansion, Atmel, ... command set */
2727 cfi_fixup(bank, cfi_0002_fixups);
2730 LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
2734 if ((cfi_info->dev_size * bank->bus_width / bank->chip_width) != bank->size)
2736 LOG_WARNING("configuration specifies 0x%" PRIx32 " size, but a 0x%" PRIx32
2737 " size flash was found", bank->size, cfi_info->dev_size);
2740 if (cfi_info->num_erase_regions == 0)
2742 /* a device might have only one erase block, spanning the whole device */
2743 bank->num_sectors = 1;
2744 bank->sectors = malloc(sizeof(struct flash_sector));
2746 bank->sectors[sector].offset = 0x0;
2747 bank->sectors[sector].size = bank->size;
2748 bank->sectors[sector].is_erased = -1;
2749 bank->sectors[sector].is_protected = -1;
2753 uint32_t offset = 0;
2755 for (i = 0; i < cfi_info->num_erase_regions; i++)
2757 num_sectors += (cfi_info->erase_region_info[i] & 0xffff) + 1;
2760 bank->num_sectors = num_sectors;
2761 bank->sectors = malloc(sizeof(struct flash_sector) * num_sectors);
2763 for (i = 0; i < cfi_info->num_erase_regions; i++)
2766 for (j = 0; j < (cfi_info->erase_region_info[i] & 0xffff) + 1; j++)
2768 bank->sectors[sector].offset = offset;
2769 bank->sectors[sector].size = ((cfi_info->erase_region_info[i] >> 16) * 256)
2770 * bank->bus_width / bank->chip_width;
2771 offset += bank->sectors[sector].size;
2772 bank->sectors[sector].is_erased = -1;
2773 bank->sectors[sector].is_protected = -1;
2777 if (offset != (cfi_info->dev_size * bank->bus_width / bank->chip_width))
2779 LOG_WARNING("CFI size is 0x%" PRIx32 ", but total sector size is 0x%" PRIx32 "", \
2780 (cfi_info->dev_size * bank->bus_width / bank->chip_width), offset);
2784 cfi_info->probed = 1;
2789 static int cfi_auto_probe(struct flash_bank *bank)
2791 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2792 if (cfi_info->probed)
2794 return cfi_probe(bank);
2797 static int cfi_intel_protect_check(struct flash_bank *bank)
2800 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2801 struct cfi_intel_pri_ext *pri_ext = cfi_info->pri_ext;
2804 /* check if block lock bits are supported on this device */
2805 if (!(pri_ext->blk_status_reg_mask & 0x1))
2806 return ERROR_FLASH_OPERATION_FAILED;
2808 if ((retval = cfi_send_command(bank, 0x90, flash_address(bank, 0, 0x55))) != ERROR_OK)
2813 for (i = 0; i < bank->num_sectors; i++)
2815 uint8_t block_status;
2816 retval = cfi_get_u8(bank, i, 0x2, &block_status);
2817 if (retval != ERROR_OK)
2820 if (block_status & 1)
2821 bank->sectors[i].is_protected = 1;
2823 bank->sectors[i].is_protected = 0;
2826 return cfi_send_command(bank, 0xff, flash_address(bank, 0, 0x0));
2829 static int cfi_spansion_protect_check(struct flash_bank *bank)
2832 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2833 struct cfi_spansion_pri_ext *pri_ext = cfi_info->pri_ext;
2836 if ((retval = cfi_send_command(bank, 0xaa,
2837 flash_address(bank, 0, pri_ext->_unlock1))) != ERROR_OK)
2842 if ((retval = cfi_send_command(bank, 0x55,
2843 flash_address(bank, 0, pri_ext->_unlock2))) != ERROR_OK)
2848 if ((retval = cfi_send_command(bank, 0x90,
2849 flash_address(bank, 0, pri_ext->_unlock1))) != ERROR_OK)
2854 for (i = 0; i < bank->num_sectors; i++)
2856 uint8_t block_status;
2857 retval = cfi_get_u8(bank, i, 0x2, &block_status);
2858 if (retval != ERROR_OK)
2861 if (block_status & 1)
2862 bank->sectors[i].is_protected = 1;
2864 bank->sectors[i].is_protected = 0;
2867 return cfi_send_command(bank, 0xf0, flash_address(bank, 0, 0x0));
2870 static int cfi_protect_check(struct flash_bank *bank)
2872 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2874 if (bank->target->state != TARGET_HALTED)
2876 LOG_ERROR("Target not halted");
2877 return ERROR_TARGET_NOT_HALTED;
2880 if (cfi_info->qry[0] != 'Q')
2881 return ERROR_FLASH_BANK_NOT_PROBED;
2883 switch (cfi_info->pri_id)
2887 return cfi_intel_protect_check(bank);
2890 return cfi_spansion_protect_check(bank);
2893 LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
2900 static int get_cfi_info(struct flash_bank *bank, char *buf, int buf_size)
2903 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2905 if (cfi_info->qry[0] == 0xff)
2907 printed = snprintf(buf, buf_size, "\ncfi flash bank not probed yet\n");
2911 if (cfi_info->not_cfi == 0)
2912 printed = snprintf(buf, buf_size, "\nCFI flash: ");
2914 printed = snprintf(buf, buf_size, "\nnon-CFI flash: ");
2916 buf_size -= printed;
2918 printed = snprintf(buf, buf_size, "mfr: 0x%4.4x, id:0x%4.4x\n\n",
2919 cfi_info->manufacturer, cfi_info->device_id);
2921 buf_size -= printed;
2923 printed = snprintf(buf, buf_size, "qry: '%c%c%c', pri_id: 0x%4.4x, pri_addr: "
2924 "0x%4.4x, alt_id: 0x%4.4x, alt_addr: 0x%4.4x\n",
2925 cfi_info->qry[0], cfi_info->qry[1], cfi_info->qry[2],
2926 cfi_info->pri_id, cfi_info->pri_addr, cfi_info->alt_id, cfi_info->alt_addr);
2928 buf_size -= printed;
2930 printed = snprintf(buf, buf_size, "Vcc min: %x.%x, Vcc max: %x.%x, "
2931 "Vpp min: %u.%x, Vpp max: %u.%x\n",
2932 (cfi_info->vcc_min & 0xf0) >> 4, cfi_info->vcc_min & 0x0f,
2933 (cfi_info->vcc_max & 0xf0) >> 4, cfi_info->vcc_max & 0x0f,
2934 (cfi_info->vpp_min & 0xf0) >> 4, cfi_info->vpp_min & 0x0f,
2935 (cfi_info->vpp_max & 0xf0) >> 4, cfi_info->vpp_max & 0x0f);
2937 buf_size -= printed;
2939 printed = snprintf(buf, buf_size, "typ. word write timeout: %u us, "
2940 "typ. buf write timeout: %u us, "
2941 "typ. block erase timeout: %u ms, "
2942 "typ. chip erase timeout: %u ms\n",
2943 1 << cfi_info->word_write_timeout_typ,
2944 1 << cfi_info->buf_write_timeout_typ,
2945 1 << cfi_info->block_erase_timeout_typ,
2946 1 << cfi_info->chip_erase_timeout_typ);
2948 buf_size -= printed;
2950 printed = snprintf(buf, buf_size, "max. word write timeout: %u us, "
2951 "max. buf write timeout: %u us, max. "
2952 "block erase timeout: %u ms, max. chip erase timeout: %u ms\n",
2953 (1 << cfi_info->word_write_timeout_max) * (1 << cfi_info->word_write_timeout_typ),
2954 (1 << cfi_info->buf_write_timeout_max) * (1 << cfi_info->buf_write_timeout_typ),
2955 (1 << cfi_info->block_erase_timeout_max) * (1 << cfi_info->block_erase_timeout_typ),
2956 (1 << cfi_info->chip_erase_timeout_max) * (1 << cfi_info->chip_erase_timeout_typ));
2958 buf_size -= printed;
2960 printed = snprintf(buf, buf_size, "size: 0x%" PRIx32 ", interface desc: %i, "
2961 "max buffer write size: 0x%x\n",
2963 cfi_info->interface_desc,
2964 1 << cfi_info->max_buf_write_size);
2966 buf_size -= printed;
2968 switch (cfi_info->pri_id)
2972 cfi_intel_info(bank, buf, buf_size);
2975 cfi_spansion_info(bank, buf, buf_size);
2978 LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
2985 static void cfi_fixup_0002_write_buffer(struct flash_bank *bank, void *param)
2987 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2989 /* disable write buffer for M29W128G */
2990 cfi_info->buf_write_timeout_typ = 0;
2993 struct flash_driver cfi_flash = {
2995 .flash_bank_command = cfi_flash_bank_command,
2997 .protect = cfi_protect,
3001 .auto_probe = cfi_auto_probe,
3002 /* FIXME: access flash at bus_width size */
3003 .erase_check = default_flash_blank_check,
3004 .protect_check = cfi_protect_check,
3005 .info = get_cfi_info,