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 <target/mips32.h>
35 #include <helper/binarybuffer.h>
36 #include <target/algorithm.h>
39 #define CFI_MAX_BUS_WIDTH 4
40 #define CFI_MAX_CHIP_WIDTH 4
42 /* defines internal maximum size for code fragment in cfi_intel_write_block() */
43 #define CFI_MAX_INTEL_CODESIZE 256
45 /* some id-types with specific handling */
46 #define AT49BV6416 0x00d6
47 #define AT49BV6416T 0x00d2
49 static struct cfi_unlock_addresses cfi_unlock_addresses[] =
51 [CFI_UNLOCK_555_2AA] = { .unlock1 = 0x555, .unlock2 = 0x2aa },
52 [CFI_UNLOCK_5555_2AAA] = { .unlock1 = 0x5555, .unlock2 = 0x2aaa },
55 /* CFI fixups foward declarations */
56 static void cfi_fixup_0002_erase_regions(struct flash_bank *bank, void *param);
57 static void cfi_fixup_0002_unlock_addresses(struct flash_bank *bank, void *param);
58 static void cfi_fixup_reversed_erase_regions(struct flash_bank *bank, void *param);
59 static void cfi_fixup_0002_write_buffer(struct flash_bank *bank, void *param);
61 /* fixup after reading cmdset 0002 primary query table */
62 static const struct cfi_fixup cfi_0002_fixups[] = {
63 {CFI_MFR_SST, 0x00D4, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
64 {CFI_MFR_SST, 0x00D5, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
65 {CFI_MFR_SST, 0x00D6, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
66 {CFI_MFR_SST, 0x00D7, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
67 {CFI_MFR_SST, 0x2780, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
68 {CFI_MFR_SST, 0x274b, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
69 {CFI_MFR_SST, 0x236d, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_555_2AA]},
70 {CFI_MFR_ATMEL, 0x00C8, cfi_fixup_reversed_erase_regions, NULL},
71 {CFI_MFR_ST, 0x22C4, cfi_fixup_reversed_erase_regions, NULL}, /* M29W160ET */
72 {CFI_MFR_FUJITSU, 0x22ea, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_555_2AA]},
73 {CFI_MFR_FUJITSU, 0x226b, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
74 {CFI_MFR_AMIC, 0xb31a, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_555_2AA]},
75 {CFI_MFR_MX, 0x225b, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_555_2AA]},
76 {CFI_MFR_EON, 0x225b, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_555_2AA]},
77 {CFI_MFR_AMD, 0x225b, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_555_2AA]},
78 {CFI_MFR_ANY, CFI_ID_ANY, cfi_fixup_0002_erase_regions, NULL},
79 {CFI_MFR_ST, 0x227E, cfi_fixup_0002_write_buffer, NULL}, /* M29W128G */
83 /* fixup after reading cmdset 0001 primary query table */
84 static const struct cfi_fixup cfi_0001_fixups[] = {
88 static void cfi_fixup(struct flash_bank *bank, const struct cfi_fixup *fixups)
90 struct cfi_flash_bank *cfi_info = bank->driver_priv;
91 const struct cfi_fixup *f;
93 for (f = fixups; f->fixup; f++)
95 if (((f->mfr == CFI_MFR_ANY) || (f->mfr == cfi_info->manufacturer)) &&
96 ((f->id == CFI_ID_ANY) || (f->id == cfi_info->device_id)))
98 f->fixup(bank, f->param);
103 /* inline uint32_t flash_address(struct flash_bank *bank, int sector, uint32_t offset) */
104 static __inline__ uint32_t flash_address(struct flash_bank *bank, int sector, uint32_t offset)
106 struct cfi_flash_bank *cfi_info = bank->driver_priv;
108 if (cfi_info->x16_as_x8) offset *= 2;
110 /* while the sector list isn't built, only accesses to sector 0 work */
112 return bank->base + offset * bank->bus_width;
117 LOG_ERROR("BUG: sector list not yet built");
120 return bank->base + bank->sectors[sector].offset + offset * bank->bus_width;
124 static void cfi_command(struct flash_bank *bank, uint8_t cmd, uint8_t *cmd_buf)
128 /* clear whole buffer, to ensure bits that exceed the bus_width
131 for (i = 0; i < CFI_MAX_BUS_WIDTH; i++)
134 if (bank->target->endianness == TARGET_LITTLE_ENDIAN)
136 for (i = bank->bus_width; i > 0; i--)
138 *cmd_buf++ = (i & (bank->chip_width - 1)) ? 0x0 : cmd;
143 for (i = 1; i <= bank->bus_width; i++)
145 *cmd_buf++ = (i & (bank->chip_width - 1)) ? 0x0 : cmd;
150 static int cfi_send_command(struct flash_bank *bank, uint8_t cmd, uint32_t address)
152 uint8_t command[CFI_MAX_BUS_WIDTH];
154 cfi_command(bank, cmd, command);
155 return target_write_memory(bank->target, address, bank->bus_width, 1, command);
158 /* read unsigned 8-bit value from the bank
159 * flash banks are expected to be made of similar chips
160 * the query result should be the same for all
162 static int cfi_query_u8(struct flash_bank *bank, int sector, uint32_t offset, uint8_t *val)
164 struct target *target = bank->target;
165 uint8_t data[CFI_MAX_BUS_WIDTH];
168 retval = target_read_memory(target, flash_address(bank, sector, offset),
169 bank->bus_width, 1, data);
170 if (retval != ERROR_OK)
173 if (bank->target->endianness == TARGET_LITTLE_ENDIAN)
176 *val = data[bank->bus_width - 1];
181 /* read unsigned 8-bit value from the bank
182 * in case of a bank made of multiple chips,
183 * the individual values are ORed
185 static int cfi_get_u8(struct flash_bank *bank, int sector, uint32_t offset, uint8_t *val)
187 struct target *target = bank->target;
188 uint8_t data[CFI_MAX_BUS_WIDTH];
192 retval = target_read_memory(target, flash_address(bank, sector, offset),
193 bank->bus_width, 1, data);
194 if (retval != ERROR_OK)
197 if (bank->target->endianness == TARGET_LITTLE_ENDIAN)
199 for (i = 0; i < bank->bus_width / bank->chip_width; i++)
207 for (i = 0; i < bank->bus_width / bank->chip_width; i++)
208 value |= data[bank->bus_width - 1 - i];
215 static int cfi_query_u16(struct flash_bank *bank, int sector, uint32_t offset, uint16_t *val)
217 struct target *target = bank->target;
218 struct cfi_flash_bank *cfi_info = bank->driver_priv;
219 uint8_t data[CFI_MAX_BUS_WIDTH * 2];
222 if (cfi_info->x16_as_x8)
225 for (i = 0;i < 2;i++)
227 retval = target_read_memory(target, flash_address(bank, sector, offset + i),
228 bank->bus_width, 1, &data[i * bank->bus_width]);
229 if (retval != ERROR_OK)
234 retval = target_read_memory(target, flash_address(bank, sector, offset),
235 bank->bus_width, 2, data);
236 if (retval != ERROR_OK)
240 if (bank->target->endianness == TARGET_LITTLE_ENDIAN)
241 *val = data[0] | data[bank->bus_width] << 8;
243 *val = data[bank->bus_width - 1] | data[(2 * bank->bus_width) - 1] << 8;
248 static int cfi_query_u32(struct flash_bank *bank, int sector, uint32_t offset, uint32_t *val)
250 struct target *target = bank->target;
251 struct cfi_flash_bank *cfi_info = bank->driver_priv;
252 uint8_t data[CFI_MAX_BUS_WIDTH * 4];
255 if (cfi_info->x16_as_x8)
258 for (i = 0;i < 4;i++)
260 retval = target_read_memory(target, flash_address(bank, sector, offset + i),
261 bank->bus_width, 1, &data[i * bank->bus_width]);
262 if (retval != ERROR_OK)
268 retval = target_read_memory(target, flash_address(bank, sector, offset),
269 bank->bus_width, 4, data);
270 if (retval != ERROR_OK)
274 if (bank->target->endianness == TARGET_LITTLE_ENDIAN)
275 *val = data[0] | data[bank->bus_width] << 8 |
276 data[bank->bus_width * 2] << 16 | data[bank->bus_width * 3] << 24;
278 *val = data[bank->bus_width - 1] | data[(2* bank->bus_width) - 1] << 8 |
279 data[(3 * bank->bus_width) - 1] << 16 | data[(4 * bank->bus_width) - 1] << 24;
284 static int cfi_reset(struct flash_bank *bank)
286 struct cfi_flash_bank *cfi_info = bank->driver_priv;
287 int retval = ERROR_OK;
289 if ((retval = cfi_send_command(bank, 0xf0, flash_address(bank, 0, 0x0))) != ERROR_OK)
294 if ((retval = cfi_send_command(bank, 0xff, flash_address(bank, 0, 0x0))) != ERROR_OK)
299 if (cfi_info->manufacturer == 0x20 &&
300 (cfi_info->device_id == 0x227E || cfi_info->device_id == 0x7E))
302 /* Numonix M29W128G is cmd 0xFF intolerant - causes internal undefined state
303 * so we send an extra 0xF0 reset to fix the bug */
304 if ((retval = cfi_send_command(bank, 0xf0, flash_address(bank, 0, 0x00))) != ERROR_OK)
313 static void cfi_intel_clear_status_register(struct flash_bank *bank)
315 cfi_send_command(bank, 0x50, flash_address(bank, 0, 0x0));
318 static int cfi_intel_wait_status_busy(struct flash_bank *bank, int timeout, uint8_t *val)
322 int retval = ERROR_OK;
328 LOG_ERROR("timeout while waiting for WSM to become ready");
332 retval = cfi_get_u8(bank, 0, 0x0, &status);
333 if (retval != ERROR_OK)
342 /* mask out bit 0 (reserved) */
343 status = status & 0xfe;
345 LOG_DEBUG("status: 0x%x", status);
349 LOG_ERROR("status register: 0x%x", status);
351 LOG_ERROR("Block Lock-Bit Detected, Operation Abort");
353 LOG_ERROR("Program suspended");
355 LOG_ERROR("Low Programming Voltage Detected, Operation Aborted");
357 LOG_ERROR("Program Error / Error in Setting Lock-Bit");
359 LOG_ERROR("Error in Block Erasure or Clear Lock-Bits");
361 LOG_ERROR("Block Erase Suspended");
363 cfi_intel_clear_status_register(bank);
372 static int cfi_spansion_wait_status_busy(struct flash_bank *bank, int timeout)
374 uint8_t status, oldstatus;
375 struct cfi_flash_bank *cfi_info = bank->driver_priv;
378 retval = cfi_get_u8(bank, 0, 0x0, &oldstatus);
379 if (retval != ERROR_OK)
383 retval = cfi_get_u8(bank, 0, 0x0, &status);
385 if (retval != ERROR_OK)
388 if ((status ^ oldstatus) & 0x40) {
389 if (status & cfi_info->status_poll_mask & 0x20) {
390 retval = cfi_get_u8(bank, 0, 0x0, &oldstatus);
391 if (retval != ERROR_OK)
393 retval = cfi_get_u8(bank, 0, 0x0, &status);
394 if (retval != ERROR_OK)
396 if ((status ^ oldstatus) & 0x40) {
397 LOG_ERROR("dq5 timeout, status: 0x%x", status);
398 return(ERROR_FLASH_OPERATION_FAILED);
400 LOG_DEBUG("status: 0x%x", status);
404 } else { /* no toggle: finished, OK */
405 LOG_DEBUG("status: 0x%x", status);
411 } while (timeout-- > 0);
413 LOG_ERROR("timeout, status: 0x%x", status);
415 return(ERROR_FLASH_BUSY);
418 static int cfi_read_intel_pri_ext(struct flash_bank *bank)
421 struct cfi_flash_bank *cfi_info = bank->driver_priv;
422 struct cfi_intel_pri_ext *pri_ext;
424 if (cfi_info->pri_ext)
425 free(cfi_info->pri_ext);
427 pri_ext = malloc(sizeof(struct cfi_intel_pri_ext));
430 LOG_ERROR("Out of memory");
433 cfi_info->pri_ext = pri_ext;
435 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0, &pri_ext->pri[0]);
436 if (retval != ERROR_OK)
438 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 1, &pri_ext->pri[1]);
439 if (retval != ERROR_OK)
441 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 2, &pri_ext->pri[2]);
442 if (retval != ERROR_OK)
445 if ((pri_ext->pri[0] != 'P') || (pri_ext->pri[1] != 'R') || (pri_ext->pri[2] != 'I'))
447 if ((retval = cfi_reset(bank)) != ERROR_OK)
451 LOG_ERROR("Could not read bank flash bank information");
452 return ERROR_FLASH_BANK_INVALID;
455 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 3, &pri_ext->major_version);
456 if (retval != ERROR_OK)
458 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 4, &pri_ext->minor_version);
459 if (retval != ERROR_OK)
462 LOG_DEBUG("pri: '%c%c%c', version: %c.%c", pri_ext->pri[0], pri_ext->pri[1],
463 pri_ext->pri[2], pri_ext->major_version, pri_ext->minor_version);
465 retval = cfi_query_u32(bank, 0, cfi_info->pri_addr + 5, &pri_ext->feature_support);
466 if (retval != ERROR_OK)
468 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 9, &pri_ext->suspend_cmd_support);
469 if (retval != ERROR_OK)
471 retval = cfi_query_u16(bank, 0, cfi_info->pri_addr + 0xa, &pri_ext->blk_status_reg_mask);
472 if (retval != ERROR_OK)
475 LOG_DEBUG("feature_support: 0x%" PRIx32 ", suspend_cmd_support: "
476 "0x%x, blk_status_reg_mask: 0x%x",
477 pri_ext->feature_support,
478 pri_ext->suspend_cmd_support,
479 pri_ext->blk_status_reg_mask);
481 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0xc, &pri_ext->vcc_optimal);
482 if (retval != ERROR_OK)
484 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0xd, &pri_ext->vpp_optimal);
485 if (retval != ERROR_OK)
488 LOG_DEBUG("Vcc opt: %x.%x, Vpp opt: %u.%x",
489 (pri_ext->vcc_optimal & 0xf0) >> 4, pri_ext->vcc_optimal & 0x0f,
490 (pri_ext->vpp_optimal & 0xf0) >> 4, pri_ext->vpp_optimal & 0x0f);
492 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0xe, &pri_ext->num_protection_fields);
493 if (retval != ERROR_OK)
495 if (pri_ext->num_protection_fields != 1)
497 LOG_WARNING("expected one protection register field, but found %i",
498 pri_ext->num_protection_fields);
501 retval = cfi_query_u16(bank, 0, cfi_info->pri_addr + 0xf, &pri_ext->prot_reg_addr);
502 if (retval != ERROR_OK)
504 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0x11, &pri_ext->fact_prot_reg_size);
505 if (retval != ERROR_OK)
507 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0x12, &pri_ext->user_prot_reg_size);
508 if (retval != ERROR_OK)
511 LOG_DEBUG("protection_fields: %i, prot_reg_addr: 0x%x, "
512 "factory pre-programmed: %i, user programmable: %i",
513 pri_ext->num_protection_fields, pri_ext->prot_reg_addr,
514 1 << pri_ext->fact_prot_reg_size, 1 << pri_ext->user_prot_reg_size);
519 static int cfi_read_spansion_pri_ext(struct flash_bank *bank)
522 struct cfi_flash_bank *cfi_info = bank->driver_priv;
523 struct cfi_spansion_pri_ext *pri_ext;
525 if (cfi_info->pri_ext)
526 free(cfi_info->pri_ext);
528 pri_ext = malloc(sizeof(struct cfi_spansion_pri_ext));
531 LOG_ERROR("Out of memory");
534 cfi_info->pri_ext = pri_ext;
536 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0, &pri_ext->pri[0]);
537 if (retval != ERROR_OK)
539 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 1, &pri_ext->pri[1]);
540 if (retval != ERROR_OK)
542 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 2, &pri_ext->pri[2]);
543 if (retval != ERROR_OK)
546 if ((pri_ext->pri[0] != 'P') || (pri_ext->pri[1] != 'R') || (pri_ext->pri[2] != 'I'))
548 if ((retval = cfi_send_command(bank, 0xf0, flash_address(bank, 0, 0x0))) != ERROR_OK)
552 LOG_ERROR("Could not read spansion bank information");
553 return ERROR_FLASH_BANK_INVALID;
556 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 3, &pri_ext->major_version);
557 if (retval != ERROR_OK)
559 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 4, &pri_ext->minor_version);
560 if (retval != ERROR_OK)
563 LOG_DEBUG("pri: '%c%c%c', version: %c.%c", pri_ext->pri[0], pri_ext->pri[1],
564 pri_ext->pri[2], pri_ext->major_version, pri_ext->minor_version);
566 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 5, &pri_ext->SiliconRevision);
567 if (retval != ERROR_OK)
569 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 6, &pri_ext->EraseSuspend);
570 if (retval != ERROR_OK)
572 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 7, &pri_ext->BlkProt);
573 if (retval != ERROR_OK)
575 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 8, &pri_ext->TmpBlkUnprotect);
576 if (retval != ERROR_OK)
578 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 9, &pri_ext->BlkProtUnprot);
579 if (retval != ERROR_OK)
581 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 10, &pri_ext->SimultaneousOps);
582 if (retval != ERROR_OK)
584 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 11, &pri_ext->BurstMode);
585 if (retval != ERROR_OK)
587 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 12, &pri_ext->PageMode);
588 if (retval != ERROR_OK)
590 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 13, &pri_ext->VppMin);
591 if (retval != ERROR_OK)
593 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 14, &pri_ext->VppMax);
594 if (retval != ERROR_OK)
596 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 15, &pri_ext->TopBottom);
597 if (retval != ERROR_OK)
600 LOG_DEBUG("Silicon Revision: 0x%x, Erase Suspend: 0x%x, Block protect: 0x%x",
601 pri_ext->SiliconRevision, pri_ext->EraseSuspend, pri_ext->BlkProt);
603 LOG_DEBUG("Temporary Unprotect: 0x%x, Block Protect Scheme: 0x%x, "
604 "Simultaneous Ops: 0x%x", pri_ext->TmpBlkUnprotect,
605 pri_ext->BlkProtUnprot, pri_ext->SimultaneousOps);
607 LOG_DEBUG("Burst Mode: 0x%x, Page Mode: 0x%x, ", pri_ext->BurstMode, pri_ext->PageMode);
610 LOG_DEBUG("Vpp min: %u.%x, Vpp max: %u.%x",
611 (pri_ext->VppMin & 0xf0) >> 4, pri_ext->VppMin & 0x0f,
612 (pri_ext->VppMax & 0xf0) >> 4, pri_ext->VppMax & 0x0f);
614 LOG_DEBUG("WP# protection 0x%x", pri_ext->TopBottom);
616 /* default values for implementation specific workarounds */
617 pri_ext->_unlock1 = cfi_unlock_addresses[CFI_UNLOCK_555_2AA].unlock1;
618 pri_ext->_unlock2 = cfi_unlock_addresses[CFI_UNLOCK_555_2AA].unlock2;
619 pri_ext->_reversed_geometry = 0;
624 static int cfi_read_atmel_pri_ext(struct flash_bank *bank)
627 struct cfi_atmel_pri_ext atmel_pri_ext;
628 struct cfi_flash_bank *cfi_info = bank->driver_priv;
629 struct cfi_spansion_pri_ext *pri_ext;
631 if (cfi_info->pri_ext)
632 free(cfi_info->pri_ext);
634 pri_ext = malloc(sizeof(struct cfi_spansion_pri_ext));
637 LOG_ERROR("Out of memory");
641 /* ATMEL devices use the same CFI primary command set (0x2) as AMD/Spansion,
642 * but a different primary extended query table.
643 * We read the atmel table, and prepare a valid AMD/Spansion query table.
646 memset(pri_ext, 0, sizeof(struct cfi_spansion_pri_ext));
648 cfi_info->pri_ext = pri_ext;
650 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0, &atmel_pri_ext.pri[0]);
651 if (retval != ERROR_OK)
653 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 1, &atmel_pri_ext.pri[1]);
654 if (retval != ERROR_OK)
656 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 2, &atmel_pri_ext.pri[2]);
657 if (retval != ERROR_OK)
660 if ((atmel_pri_ext.pri[0] != 'P') || (atmel_pri_ext.pri[1] != 'R')
661 || (atmel_pri_ext.pri[2] != 'I'))
663 if ((retval = cfi_send_command(bank, 0xf0, flash_address(bank, 0, 0x0))) != ERROR_OK)
667 LOG_ERROR("Could not read atmel bank information");
668 return ERROR_FLASH_BANK_INVALID;
671 pri_ext->pri[0] = atmel_pri_ext.pri[0];
672 pri_ext->pri[1] = atmel_pri_ext.pri[1];
673 pri_ext->pri[2] = atmel_pri_ext.pri[2];
675 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 3, &atmel_pri_ext.major_version);
676 if (retval != ERROR_OK)
678 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 4, &atmel_pri_ext.minor_version);
679 if (retval != ERROR_OK)
682 LOG_DEBUG("pri: '%c%c%c', version: %c.%c", atmel_pri_ext.pri[0],
683 atmel_pri_ext.pri[1], atmel_pri_ext.pri[2],
684 atmel_pri_ext.major_version, atmel_pri_ext.minor_version);
686 pri_ext->major_version = atmel_pri_ext.major_version;
687 pri_ext->minor_version = atmel_pri_ext.minor_version;
689 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 5, &atmel_pri_ext.features);
690 if (retval != ERROR_OK)
692 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 6, &atmel_pri_ext.bottom_boot);
693 if (retval != ERROR_OK)
695 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 7, &atmel_pri_ext.burst_mode);
696 if (retval != ERROR_OK)
698 retval = cfi_query_u8(bank, 0, cfi_info->pri_addr + 8, &atmel_pri_ext.page_mode);
699 if (retval != ERROR_OK)
702 LOG_DEBUG("features: 0x%2.2x, bottom_boot: 0x%2.2x, burst_mode: 0x%2.2x, page_mode: 0x%2.2x",
703 atmel_pri_ext.features, atmel_pri_ext.bottom_boot,
704 atmel_pri_ext.burst_mode, atmel_pri_ext.page_mode);
706 if (atmel_pri_ext.features & 0x02)
707 pri_ext->EraseSuspend = 2;
709 /* some chips got it backwards... */
710 if (cfi_info->device_id == AT49BV6416 ||
711 cfi_info->device_id == AT49BV6416T) {
712 if (atmel_pri_ext.bottom_boot)
713 pri_ext->TopBottom = 3;
715 pri_ext->TopBottom = 2;
717 if (atmel_pri_ext.bottom_boot)
718 pri_ext->TopBottom = 2;
720 pri_ext->TopBottom = 3;
723 pri_ext->_unlock1 = cfi_unlock_addresses[CFI_UNLOCK_555_2AA].unlock1;
724 pri_ext->_unlock2 = cfi_unlock_addresses[CFI_UNLOCK_555_2AA].unlock2;
729 static int cfi_read_0002_pri_ext(struct flash_bank *bank)
731 struct cfi_flash_bank *cfi_info = bank->driver_priv;
733 if (cfi_info->manufacturer == CFI_MFR_ATMEL)
735 return cfi_read_atmel_pri_ext(bank);
739 return cfi_read_spansion_pri_ext(bank);
743 static int cfi_spansion_info(struct flash_bank *bank, char *buf, int buf_size)
746 struct cfi_flash_bank *cfi_info = bank->driver_priv;
747 struct cfi_spansion_pri_ext *pri_ext = cfi_info->pri_ext;
749 printed = snprintf(buf, buf_size, "\nSpansion primary algorithm extend information:\n");
753 printed = snprintf(buf, buf_size, "pri: '%c%c%c', version: %c.%c\n", pri_ext->pri[0],
754 pri_ext->pri[1], pri_ext->pri[2],
755 pri_ext->major_version, pri_ext->minor_version);
759 printed = snprintf(buf, buf_size, "Silicon Rev.: 0x%x, Address Sensitive unlock: 0x%x\n",
760 (pri_ext->SiliconRevision) >> 2,
761 (pri_ext->SiliconRevision) & 0x03);
765 printed = snprintf(buf, buf_size, "Erase Suspend: 0x%x, Sector Protect: 0x%x\n",
766 pri_ext->EraseSuspend,
771 snprintf(buf, buf_size, "VppMin: %u.%x, VppMax: %u.%x\n",
772 (pri_ext->VppMin & 0xf0) >> 4, pri_ext->VppMin & 0x0f,
773 (pri_ext->VppMax & 0xf0) >> 4, pri_ext->VppMax & 0x0f);
778 static int cfi_intel_info(struct flash_bank *bank, char *buf, int buf_size)
781 struct cfi_flash_bank *cfi_info = bank->driver_priv;
782 struct cfi_intel_pri_ext *pri_ext = cfi_info->pri_ext;
784 printed = snprintf(buf, buf_size, "\nintel primary algorithm extend information:\n");
788 printed = snprintf(buf, buf_size, "pri: '%c%c%c', version: %c.%c\n", pri_ext->pri[0],
789 pri_ext->pri[1], pri_ext->pri[2], pri_ext->major_version, pri_ext->minor_version);
793 printed = snprintf(buf, buf_size, "feature_support: 0x%" PRIx32 ", "
794 "suspend_cmd_support: 0x%x, blk_status_reg_mask: 0x%x\n",
795 pri_ext->feature_support, pri_ext->suspend_cmd_support, pri_ext->blk_status_reg_mask);
799 printed = snprintf(buf, buf_size, "Vcc opt: %x.%x, Vpp opt: %u.%x\n",
800 (pri_ext->vcc_optimal & 0xf0) >> 4, pri_ext->vcc_optimal & 0x0f,
801 (pri_ext->vpp_optimal & 0xf0) >> 4, pri_ext->vpp_optimal & 0x0f);
805 snprintf(buf, buf_size, "protection_fields: %i, prot_reg_addr: 0x%x, "
806 "factory pre-programmed: %i, user programmable: %i\n",
807 pri_ext->num_protection_fields, pri_ext->prot_reg_addr,
808 1 << pri_ext->fact_prot_reg_size, 1 << pri_ext->user_prot_reg_size);
813 /* flash_bank cfi <base> <size> <chip_width> <bus_width> <target#> [options]
815 FLASH_BANK_COMMAND_HANDLER(cfi_flash_bank_command)
817 struct cfi_flash_bank *cfi_info;
821 return ERROR_COMMAND_SYNTAX_ERROR;
825 * - not exceed max value;
827 * - be equal to a power of 2.
828 * bus must be wide enought to hold one chip */
829 if ((bank->chip_width > CFI_MAX_CHIP_WIDTH)
830 || (bank->bus_width > CFI_MAX_BUS_WIDTH)
831 || (bank->chip_width == 0)
832 || (bank->bus_width == 0)
833 || (bank->chip_width & (bank->chip_width - 1))
834 || (bank->bus_width & (bank->bus_width - 1))
835 || (bank->chip_width > bank->bus_width))
837 LOG_ERROR("chip and bus width have to specified in bytes");
838 return ERROR_FLASH_BANK_INVALID;
841 cfi_info = malloc(sizeof(struct cfi_flash_bank));
842 cfi_info->probed = 0;
843 cfi_info->erase_region_info = NULL;
844 cfi_info->pri_ext = NULL;
845 bank->driver_priv = cfi_info;
847 cfi_info->write_algorithm = NULL;
849 cfi_info->x16_as_x8 = 0;
850 cfi_info->jedec_probe = 0;
851 cfi_info->not_cfi = 0;
853 for (unsigned i = 6; i < CMD_ARGC; i++)
855 if (strcmp(CMD_ARGV[i], "x16_as_x8") == 0)
857 cfi_info->x16_as_x8 = 1;
859 else if (strcmp(CMD_ARGV[i], "jedec_probe") == 0)
861 cfi_info->jedec_probe = 1;
865 cfi_info->write_algorithm = NULL;
867 /* bank wasn't probed yet */
868 cfi_info->qry[0] = 0xff;
873 static int cfi_intel_erase(struct flash_bank *bank, int first, int last)
876 struct cfi_flash_bank *cfi_info = bank->driver_priv;
879 cfi_intel_clear_status_register(bank);
881 for (i = first; i <= last; i++)
883 if ((retval = cfi_send_command(bank, 0x20, flash_address(bank, i, 0x0))) != ERROR_OK)
888 if ((retval = cfi_send_command(bank, 0xd0, flash_address(bank, i, 0x0))) != ERROR_OK)
894 retval = cfi_intel_wait_status_busy(bank, cfi_info->block_erase_timeout, &status);
895 if (retval != ERROR_OK)
899 bank->sectors[i].is_erased = 1;
902 if ((retval = cfi_send_command(bank, 0xff, flash_address(bank, 0, 0x0))) != ERROR_OK)
907 LOG_ERROR("couldn't erase block %i of flash bank at base 0x%" PRIx32 , i, bank->base);
908 return ERROR_FLASH_OPERATION_FAILED;
912 return cfi_send_command(bank, 0xff, flash_address(bank, 0, 0x0));
915 static int cfi_spansion_erase(struct flash_bank *bank, int first, int last)
918 struct cfi_flash_bank *cfi_info = bank->driver_priv;
919 struct cfi_spansion_pri_ext *pri_ext = cfi_info->pri_ext;
922 for (i = first; i <= last; i++)
924 if ((retval = cfi_send_command(bank, 0xaa,
925 flash_address(bank, 0, pri_ext->_unlock1))) != ERROR_OK)
930 if ((retval = cfi_send_command(bank, 0x55,
931 flash_address(bank, 0, pri_ext->_unlock2))) != ERROR_OK)
936 if ((retval = cfi_send_command(bank, 0x80,
937 flash_address(bank, 0, pri_ext->_unlock1))) != ERROR_OK)
942 if ((retval = cfi_send_command(bank, 0xaa,
943 flash_address(bank, 0, pri_ext->_unlock1))) != ERROR_OK)
948 if ((retval = cfi_send_command(bank, 0x55,
949 flash_address(bank, 0, pri_ext->_unlock2))) != ERROR_OK)
954 if ((retval = cfi_send_command(bank, 0x30,
955 flash_address(bank, i, 0x0))) != ERROR_OK)
960 if (cfi_spansion_wait_status_busy(bank, cfi_info->block_erase_timeout) == ERROR_OK)
962 bank->sectors[i].is_erased = 1;
966 if ((retval = cfi_send_command(bank, 0xf0,
967 flash_address(bank, 0, 0x0))) != ERROR_OK)
972 LOG_ERROR("couldn't erase block %i of flash bank at base 0x%"
973 PRIx32, i, bank->base);
974 return ERROR_FLASH_OPERATION_FAILED;
978 return cfi_send_command(bank, 0xf0, flash_address(bank, 0, 0x0));
981 static int cfi_erase(struct flash_bank *bank, int first, int last)
983 struct cfi_flash_bank *cfi_info = bank->driver_priv;
985 if (bank->target->state != TARGET_HALTED)
987 LOG_ERROR("Target not halted");
988 return ERROR_TARGET_NOT_HALTED;
991 if ((first < 0) || (last < first) || (last >= bank->num_sectors))
993 return ERROR_FLASH_SECTOR_INVALID;
996 if (cfi_info->qry[0] != 'Q')
997 return ERROR_FLASH_BANK_NOT_PROBED;
999 switch (cfi_info->pri_id)
1003 return cfi_intel_erase(bank, first, last);
1006 return cfi_spansion_erase(bank, first, last);
1009 LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
1016 static int cfi_intel_protect(struct flash_bank *bank, int set, int first, int last)
1019 struct cfi_flash_bank *cfi_info = bank->driver_priv;
1020 struct cfi_intel_pri_ext *pri_ext = cfi_info->pri_ext;
1024 /* if the device supports neither legacy lock/unlock (bit 3) nor
1025 * instant individual block locking (bit 5).
1027 if (!(pri_ext->feature_support & 0x28))
1029 LOG_ERROR("lock/unlock not supported on flash");
1030 return ERROR_FLASH_OPERATION_FAILED;
1033 cfi_intel_clear_status_register(bank);
1035 for (i = first; i <= last; i++)
1037 if ((retval = cfi_send_command(bank, 0x60, flash_address(bank, i, 0x0))) != ERROR_OK)
1043 if ((retval = cfi_send_command(bank, 0x01, flash_address(bank, i, 0x0))) != ERROR_OK)
1047 bank->sectors[i].is_protected = 1;
1051 if ((retval = cfi_send_command(bank, 0xd0, flash_address(bank, i, 0x0))) != ERROR_OK)
1055 bank->sectors[i].is_protected = 0;
1058 /* instant individual block locking doesn't require reading of the status register */
1059 if (!(pri_ext->feature_support & 0x20))
1061 /* Clear lock bits operation may take up to 1.4s */
1063 retval = cfi_intel_wait_status_busy(bank, 1400, &status);
1064 if (retval != ERROR_OK)
1069 uint8_t block_status;
1070 /* read block lock bit, to verify status */
1071 if ((retval = cfi_send_command(bank, 0x90, flash_address(bank, 0, 0x55))) != ERROR_OK)
1075 retval = cfi_get_u8(bank, i, 0x2, &block_status);
1076 if (retval != ERROR_OK)
1079 if ((block_status & 0x1) != set)
1081 LOG_ERROR("couldn't change block lock status (set = %i, block_status = 0x%2.2x)",
1083 if ((retval = cfi_send_command(bank, 0x70,
1084 flash_address(bank, 0, 0x55))) != ERROR_OK)
1089 retval = cfi_intel_wait_status_busy(bank, 10, &status);
1090 if (retval != ERROR_OK)
1094 return ERROR_FLASH_OPERATION_FAILED;
1104 /* if the device doesn't support individual block lock bits set/clear,
1105 * all blocks have been unlocked in parallel, so we set those that should be protected
1107 if ((!set) && (!(pri_ext->feature_support & 0x20)))
1109 /* FIX!!! this code path is broken!!!
1111 * The correct approach is:
1113 * 1. read out current protection status
1115 * 2. override read out protection status w/unprotected.
1117 * 3. re-protect what should be protected.
1120 for (i = 0; i < bank->num_sectors; i++)
1122 if (bank->sectors[i].is_protected == 1)
1124 cfi_intel_clear_status_register(bank);
1126 if ((retval = cfi_send_command(bank, 0x60,
1127 flash_address(bank, i, 0x0))) != ERROR_OK)
1132 if ((retval = cfi_send_command(bank, 0x01,
1133 flash_address(bank, i, 0x0))) != ERROR_OK)
1139 retval = cfi_intel_wait_status_busy(bank, 100, &status);
1140 if (retval != ERROR_OK)
1146 return cfi_send_command(bank, 0xff, flash_address(bank, 0, 0x0));
1149 static int cfi_protect(struct flash_bank *bank, int set, int first, int last)
1151 struct cfi_flash_bank *cfi_info = bank->driver_priv;
1153 if (bank->target->state != TARGET_HALTED)
1155 LOG_ERROR("Target not halted");
1156 return ERROR_TARGET_NOT_HALTED;
1159 if ((first < 0) || (last < first) || (last >= bank->num_sectors))
1161 LOG_ERROR("Invalid sector range");
1162 return ERROR_FLASH_SECTOR_INVALID;
1165 if (cfi_info->qry[0] != 'Q')
1166 return ERROR_FLASH_BANK_NOT_PROBED;
1168 switch (cfi_info->pri_id)
1172 return cfi_intel_protect(bank, set, first, last);
1175 LOG_WARNING("protect: cfi primary command set %i unsupported", cfi_info->pri_id);
1180 /* Convert code image to target endian */
1181 /* FIXME create general block conversion fcts in target.c?) */
1182 static void cfi_fix_code_endian(struct target *target, uint8_t *dest,
1183 const uint32_t *src, uint32_t count)
1186 for (i = 0; i< count; i++)
1188 target_buffer_set_u32(target, dest, *src);
1194 static uint32_t cfi_command_val(struct flash_bank *bank, uint8_t cmd)
1196 struct target *target = bank->target;
1198 uint8_t buf[CFI_MAX_BUS_WIDTH];
1199 cfi_command(bank, cmd, buf);
1200 switch (bank->bus_width)
1206 return target_buffer_get_u16(target, buf);
1209 return target_buffer_get_u32(target, buf);
1212 LOG_ERROR("Unsupported bank buswidth %d, can't do block memory writes", bank->bus_width);
1217 static int cfi_intel_write_block(struct flash_bank *bank, uint8_t *buffer,
1218 uint32_t address, uint32_t count)
1220 struct cfi_flash_bank *cfi_info = bank->driver_priv;
1221 struct target *target = bank->target;
1222 struct reg_param reg_params[7];
1223 struct arm_algorithm armv4_5_info;
1224 struct working_area *source = NULL;
1225 uint32_t buffer_size = 32768;
1226 uint32_t write_command_val, busy_pattern_val, error_pattern_val;
1228 /* algorithm register usage:
1229 * r0: source address (in RAM)
1230 * r1: target address (in Flash)
1232 * r3: flash write command
1233 * r4: status byte (returned to host)
1234 * r5: busy test pattern
1235 * r6: error test pattern
1238 /* see contib/loaders/flash/armv4_5_cfi_intel_32.s for src */
1239 static const uint32_t word_32_code[] = {
1240 0xe4904004, /* loop: ldr r4, [r0], #4 */
1241 0xe5813000, /* str r3, [r1] */
1242 0xe5814000, /* str r4, [r1] */
1243 0xe5914000, /* busy: ldr r4, [r1] */
1244 0xe0047005, /* and r7, r4, r5 */
1245 0xe1570005, /* cmp r7, r5 */
1246 0x1afffffb, /* bne busy */
1247 0xe1140006, /* tst r4, r6 */
1248 0x1a000003, /* bne done */
1249 0xe2522001, /* subs r2, r2, #1 */
1250 0x0a000001, /* beq done */
1251 0xe2811004, /* add r1, r1 #4 */
1252 0xeafffff2, /* b loop */
1253 0xeafffffe /* done: b -2 */
1256 /* see contib/loaders/flash/armv4_5_cfi_intel_16.s for src */
1257 static const uint32_t word_16_code[] = {
1258 0xe0d040b2, /* loop: ldrh r4, [r0], #2 */
1259 0xe1c130b0, /* strh r3, [r1] */
1260 0xe1c140b0, /* strh r4, [r1] */
1261 0xe1d140b0, /* busy ldrh r4, [r1] */
1262 0xe0047005, /* and r7, r4, r5 */
1263 0xe1570005, /* cmp r7, r5 */
1264 0x1afffffb, /* bne busy */
1265 0xe1140006, /* tst r4, r6 */
1266 0x1a000003, /* bne done */
1267 0xe2522001, /* subs r2, r2, #1 */
1268 0x0a000001, /* beq done */
1269 0xe2811002, /* add r1, r1 #2 */
1270 0xeafffff2, /* b loop */
1271 0xeafffffe /* done: b -2 */
1274 /* see contib/loaders/flash/armv4_5_cfi_intel_8.s for src */
1275 static const uint32_t word_8_code[] = {
1276 0xe4d04001, /* loop: ldrb r4, [r0], #1 */
1277 0xe5c13000, /* strb r3, [r1] */
1278 0xe5c14000, /* strb r4, [r1] */
1279 0xe5d14000, /* busy ldrb r4, [r1] */
1280 0xe0047005, /* and r7, r4, r5 */
1281 0xe1570005, /* cmp r7, r5 */
1282 0x1afffffb, /* bne busy */
1283 0xe1140006, /* tst r4, r6 */
1284 0x1a000003, /* bne done */
1285 0xe2522001, /* subs r2, r2, #1 */
1286 0x0a000001, /* beq done */
1287 0xe2811001, /* add r1, r1 #1 */
1288 0xeafffff2, /* b loop */
1289 0xeafffffe /* done: b -2 */
1291 uint8_t target_code[4*CFI_MAX_INTEL_CODESIZE];
1292 const uint32_t *target_code_src;
1293 uint32_t target_code_size;
1294 int retval = ERROR_OK;
1296 /* todo: if ( (!is_armv7m(target_to_armv7m(target)) && (!is_arm(target_to_arm(target)) ) */
1297 if (strncmp(target_type_name(target),"mips_m4k",8) == 0)
1299 LOG_ERROR("Your target has no flash block write support yet.");
1300 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1303 cfi_intel_clear_status_register(bank);
1305 armv4_5_info.common_magic = ARM_COMMON_MAGIC;
1306 armv4_5_info.core_mode = ARM_MODE_SVC;
1307 armv4_5_info.core_state = ARM_STATE_ARM;
1309 /* If we are setting up the write_algorith, we need target_code_src */
1310 /* if not we only need target_code_size. */
1312 /* However, we don't want to create multiple code paths, so we */
1313 /* do the unecessary evaluation of target_code_src, which the */
1314 /* compiler will probably nicely optimize away if not needed */
1316 /* prepare algorithm code for target endian */
1317 switch (bank->bus_width)
1320 target_code_src = word_8_code;
1321 target_code_size = sizeof(word_8_code);
1324 target_code_src = word_16_code;
1325 target_code_size = sizeof(word_16_code);
1328 target_code_src = word_32_code;
1329 target_code_size = sizeof(word_32_code);
1332 LOG_ERROR("Unsupported bank buswidth %d, can't do block memory writes", bank->bus_width);
1333 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1336 /* flash write code */
1337 if (!cfi_info->write_algorithm)
1339 if (target_code_size > sizeof(target_code))
1341 LOG_WARNING("Internal error - target code buffer to small. "
1342 "Increase CFI_MAX_INTEL_CODESIZE and recompile.");
1343 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1345 cfi_fix_code_endian(target, target_code, target_code_src, target_code_size / 4);
1347 /* Get memory for block write handler */
1348 retval = target_alloc_working_area(target, target_code_size, &cfi_info->write_algorithm);
1349 if (retval != ERROR_OK)
1351 LOG_WARNING("No working area available, can't do block memory writes");
1352 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1355 /* write algorithm code to working area */
1356 retval = target_write_buffer(target, cfi_info->write_algorithm->address,
1357 target_code_size, target_code);
1358 if (retval != ERROR_OK)
1360 LOG_ERROR("Unable to write block write code to target");
1365 /* Get a workspace buffer for the data to flash starting with 32k size.
1366 Half size until buffer would be smaller 256 Bytem then fail back */
1367 /* FIXME Why 256 bytes, why not 32 bytes (smallest flash write page */
1368 while (target_alloc_working_area_try(target, buffer_size, &source) != ERROR_OK)
1371 if (buffer_size <= 256)
1373 LOG_WARNING("no large enough working area available, can't do block memory writes");
1374 retval = ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1379 /* setup algo registers */
1380 init_reg_param(®_params[0], "r0", 32, PARAM_OUT);
1381 init_reg_param(®_params[1], "r1", 32, PARAM_OUT);
1382 init_reg_param(®_params[2], "r2", 32, PARAM_OUT);
1383 init_reg_param(®_params[3], "r3", 32, PARAM_OUT);
1384 init_reg_param(®_params[4], "r4", 32, PARAM_IN);
1385 init_reg_param(®_params[5], "r5", 32, PARAM_OUT);
1386 init_reg_param(®_params[6], "r6", 32, PARAM_OUT);
1388 /* prepare command and status register patterns */
1389 write_command_val = cfi_command_val(bank, 0x40);
1390 busy_pattern_val = cfi_command_val(bank, 0x80);
1391 error_pattern_val = cfi_command_val(bank, 0x7e);
1393 LOG_DEBUG("Using target buffer at 0x%08" PRIx32 " and of size 0x%04" PRIx32,
1394 source->address, buffer_size);
1396 /* Programming main loop */
1399 uint32_t thisrun_count = (count > buffer_size) ? buffer_size : count;
1402 if ((retval = target_write_buffer(target, source->address,
1403 thisrun_count, buffer)) != ERROR_OK)
1408 buf_set_u32(reg_params[0].value, 0, 32, source->address);
1409 buf_set_u32(reg_params[1].value, 0, 32, address);
1410 buf_set_u32(reg_params[2].value, 0, 32, thisrun_count / bank->bus_width);
1412 buf_set_u32(reg_params[3].value, 0, 32, write_command_val);
1413 buf_set_u32(reg_params[5].value, 0, 32, busy_pattern_val);
1414 buf_set_u32(reg_params[6].value, 0, 32, error_pattern_val);
1416 LOG_DEBUG("Write 0x%04" PRIx32 " bytes to flash at 0x%08" PRIx32 , thisrun_count, address);
1418 /* Execute algorithm, assume breakpoint for last instruction */
1419 retval = target_run_algorithm(target, 0, NULL, 7, reg_params,
1420 cfi_info->write_algorithm->address,
1421 cfi_info->write_algorithm->address + target_code_size - sizeof(uint32_t),
1422 10000, /* 10s should be enough for max. 32k of data */
1425 /* On failure try a fall back to direct word writes */
1426 if (retval != ERROR_OK)
1428 cfi_intel_clear_status_register(bank);
1429 LOG_ERROR("Execution of flash algorythm failed. Can't fall back. Please report.");
1430 retval = ERROR_FLASH_OPERATION_FAILED;
1431 /* retval = ERROR_TARGET_RESOURCE_NOT_AVAILABLE; */
1432 /* FIXME To allow fall back or recovery, we must save the actual status
1433 * somewhere, so that a higher level code can start recovery. */
1437 /* Check return value from algo code */
1438 wsm_error = buf_get_u32(reg_params[4].value, 0, 32) & error_pattern_val;
1441 /* read status register (outputs debug inforation) */
1443 cfi_intel_wait_status_busy(bank, 100, &status);
1444 cfi_intel_clear_status_register(bank);
1445 retval = ERROR_FLASH_OPERATION_FAILED;
1449 buffer += thisrun_count;
1450 address += thisrun_count;
1451 count -= thisrun_count;
1456 /* free up resources */
1459 target_free_working_area(target, source);
1461 if (cfi_info->write_algorithm)
1463 target_free_working_area(target, cfi_info->write_algorithm);
1464 cfi_info->write_algorithm = NULL;
1467 destroy_reg_param(®_params[0]);
1468 destroy_reg_param(®_params[1]);
1469 destroy_reg_param(®_params[2]);
1470 destroy_reg_param(®_params[3]);
1471 destroy_reg_param(®_params[4]);
1472 destroy_reg_param(®_params[5]);
1473 destroy_reg_param(®_params[6]);
1478 static int cfi_spansion_write_block_mips(struct flash_bank *bank, uint8_t *buffer,
1479 uint32_t address, uint32_t count)
1481 struct cfi_flash_bank *cfi_info = bank->driver_priv;
1482 struct cfi_spansion_pri_ext *pri_ext = cfi_info->pri_ext;
1483 struct target *target = bank->target;
1484 struct reg_param reg_params[10];
1485 struct mips32_algorithm mips32_info;
1486 struct working_area *source;
1487 uint32_t buffer_size = 32768;
1489 int retval = ERROR_OK;
1491 /* input parameters - */
1492 /* 4 A0 = source address */
1493 /* 5 A1 = destination address */
1494 /* 6 A2 = number of writes */
1495 /* 7 A3 = flash write command */
1496 /* 8 T0 = constant to mask DQ7 bits (also used for Dq5 with shift) */
1497 /* output parameters - */
1498 /* 9 T1 = 0x80 ok 0x00 bad */
1499 /* temp registers - */
1500 /* 10 T2 = value read from flash to test status */
1501 /* 11 T3 = holding register */
1502 /* unlock registers - */
1503 /* 12 T4 = unlock1_addr */
1504 /* 13 T5 = unlock1_cmd */
1505 /* 14 T6 = unlock2_addr */
1506 /* 15 T7 = unlock2_cmd */
1508 static const uint32_t mips_word_16_code[] = {
1510 MIPS32_LHU(9,0,4), /* lhu $t1, ($a0) ; out = &saddr */
1511 MIPS32_ADDI(4,4,2), /* addi $a0, $a0, 2 ; saddr += 2 */
1512 MIPS32_SH(13,0,12), /* sh $t5, ($t4) ; *fl_unl_addr1 = fl_unl_cmd1 */
1513 MIPS32_SH(15,0,14), /* sh $t7, ($t6) ; *fl_unl_addr2 = fl_unl_cmd2 */
1514 MIPS32_SH(7,0,12), /* sh $a3, ($t4) ; *fl_unl_addr1 = fl_write_cmd */
1515 MIPS32_SH(9,0,5), /* sh $t1, ($a1) ; *daddr = out */
1516 MIPS32_NOP, /* nop */
1518 MIPS32_LHU(10,0,5), /* lhu $t2, ($a1) ; temp1 = *daddr */
1519 MIPS32_XOR(11,9,10), /* xor $t3, $a0, $t2 ; temp2 = out ^ temp1; */
1520 MIPS32_AND(11,8,11), /* and $t3, $t0, $t3 ; temp2 = temp2 & DQ7mask */
1521 MIPS32_BNE(11,8, 13), /* bne $t3, $t0, cont ; if (temp2 != DQ7mask) goto cont */
1522 MIPS32_NOP, /* nop */
1524 MIPS32_SRL(10,8,2), /* srl $t2,$t0,2 ; temp1 = DQ7mask >> 2 */
1525 MIPS32_AND(11,10,11), /* and $t3, $t2, $t3 ; temp2 = temp2 & temp1 */
1526 MIPS32_BNE(11,10, NEG16(8)), /* bne $t3, $t2, busy ; if (temp2 != temp1) goto busy */
1527 MIPS32_NOP, /* nop */
1529 MIPS32_LHU(10,0,5), /* lhu $t2, ($a1) ; temp1 = *daddr */
1530 MIPS32_XOR(11,9,10), /* xor $t3, $a0, $t2 ; temp2 = out ^ temp1; */
1531 MIPS32_AND(11,8,11), /* and $t3, $t0, $t3 ; temp2 = temp2 & DQ7mask */
1532 MIPS32_BNE(11,8, 4), /* bne $t3, $t0, cont ; if (temp2 != DQ7mask) goto cont */
1533 MIPS32_NOP, /* nop */
1535 MIPS32_XOR(9,9,9), /* xor $t1, $t1, $t1 ; out = 0 */
1536 MIPS32_BEQ(9,0, 11), /* beq $t1, $zero, done ; if (out == 0) goto done */
1537 MIPS32_NOP, /* nop */
1539 MIPS32_ADDI(6,6,NEG16(1)), /* addi, $a2, $a2, -1 ; numwrites-- */
1540 MIPS32_BNE(6,0, 5), /* bne $a2, $zero, cont2 ; if (numwrite != 0) goto cont2 */
1541 MIPS32_NOP, /* nop */
1543 MIPS32_LUI(9,0), /* lui $t1, 0 */
1544 MIPS32_ORI(9,9,0x80), /* ori $t1, $t1, 0x80 ; out = 0x80 */
1546 MIPS32_B(4), /* b done ; goto done */
1547 MIPS32_NOP, /* nop */
1549 MIPS32_ADDI(5,5,2), /* addi $a0, $a0, 2 ; daddr += 2 */
1550 MIPS32_B(NEG16(33)), /* b start ; goto start */
1551 MIPS32_NOP, /* nop */
1553 /*MIPS32_B(NEG16(1)), */ /* b done ; goto done */
1554 MIPS32_SDBBP, /* sdbbp ; break(); */
1555 /*MIPS32_B(NEG16(33)), */ /* b start ; goto start */
1559 mips32_info.common_magic = MIPS32_COMMON_MAGIC;
1560 mips32_info.isa_mode = MIPS32_ISA_MIPS32;
1562 int target_code_size = 0;
1563 const uint32_t *target_code_src = NULL;
1565 switch (bank->bus_width)
1568 /* Check for DQ5 support */
1569 if( cfi_info->status_poll_mask & (1 << 5) )
1571 target_code_src = mips_word_16_code;
1572 target_code_size = sizeof(mips_word_16_code);
1576 LOG_ERROR("Need DQ5 support");
1577 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1578 //target_code_src = mips_word_16_code_dq7only;
1579 //target_code_size = sizeof(mips_word_16_code_dq7only);
1583 LOG_ERROR("Unsupported bank buswidth %d, can't do block memory writes", bank->bus_width);
1584 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1587 /* flash write code */
1588 if (!cfi_info->write_algorithm)
1590 uint8_t *target_code;
1592 /* convert bus-width dependent algorithm code to correct endiannes */
1593 target_code = malloc(target_code_size);
1594 if (target_code == NULL)
1596 LOG_ERROR("Out of memory");
1599 cfi_fix_code_endian(target, target_code, target_code_src, target_code_size / 4);
1601 /* allocate working area */
1602 retval = target_alloc_working_area(target, target_code_size,
1603 &cfi_info->write_algorithm);
1604 if (retval != ERROR_OK)
1610 /* write algorithm code to working area */
1611 if ((retval = target_write_buffer(target, cfi_info->write_algorithm->address,
1612 target_code_size, target_code)) != ERROR_OK)
1620 /* the following code still assumes target code is fixed 24*4 bytes */
1622 while (target_alloc_working_area_try(target, buffer_size, &source) != ERROR_OK)
1625 if (buffer_size <= 256)
1627 /* if we already allocated the writing code, but failed to get a
1628 * buffer, free the algorithm */
1629 if (cfi_info->write_algorithm)
1630 target_free_working_area(target, cfi_info->write_algorithm);
1632 LOG_WARNING("not enough working area available, can't do block memory writes");
1633 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1637 init_reg_param(®_params[0], "a0", 32, PARAM_OUT);
1638 init_reg_param(®_params[1], "a1", 32, PARAM_OUT);
1639 init_reg_param(®_params[2], "a2", 32, PARAM_OUT);
1640 init_reg_param(®_params[3], "a3", 32, PARAM_OUT);
1641 init_reg_param(®_params[4], "t0", 32, PARAM_OUT);
1642 init_reg_param(®_params[5], "t1", 32, PARAM_IN);
1643 init_reg_param(®_params[6], "t4", 32, PARAM_OUT);
1644 init_reg_param(®_params[7], "t5", 32, PARAM_OUT);
1645 init_reg_param(®_params[8], "t6", 32, PARAM_OUT);
1646 init_reg_param(®_params[9], "t7", 32, PARAM_OUT);
1650 uint32_t thisrun_count = (count > buffer_size) ? buffer_size : count;
1652 retval = target_write_buffer(target, source->address, thisrun_count, buffer);
1653 if (retval != ERROR_OK)
1658 buf_set_u32(reg_params[0].value, 0, 32, source->address);
1659 buf_set_u32(reg_params[1].value, 0, 32, address);
1660 buf_set_u32(reg_params[2].value, 0, 32, thisrun_count / bank->bus_width);
1661 buf_set_u32(reg_params[3].value, 0, 32, cfi_command_val(bank, 0xA0));
1662 buf_set_u32(reg_params[4].value, 0, 32, cfi_command_val(bank, 0x80));
1663 buf_set_u32(reg_params[6].value, 0, 32, flash_address(bank, 0, pri_ext->_unlock1));
1664 buf_set_u32(reg_params[7].value, 0, 32, 0xaaaaaaaa);
1665 buf_set_u32(reg_params[8].value, 0, 32, flash_address(bank, 0, pri_ext->_unlock2));
1666 buf_set_u32(reg_params[9].value, 0, 32, 0x55555555);
1668 retval = target_run_algorithm(target, 0, NULL, 10, reg_params,
1669 cfi_info->write_algorithm->address,
1670 cfi_info->write_algorithm->address + ((target_code_size) - 4),
1671 10000, &mips32_info);
1672 if (retval != ERROR_OK)
1677 status = buf_get_u32(reg_params[5].value, 0, 32);
1680 LOG_ERROR("flash write block failed status: 0x%" PRIx32 , status);
1681 retval = ERROR_FLASH_OPERATION_FAILED;
1685 buffer += thisrun_count;
1686 address += thisrun_count;
1687 count -= thisrun_count;
1690 target_free_all_working_areas(target);
1692 destroy_reg_param(®_params[0]);
1693 destroy_reg_param(®_params[1]);
1694 destroy_reg_param(®_params[2]);
1695 destroy_reg_param(®_params[3]);
1696 destroy_reg_param(®_params[4]);
1697 destroy_reg_param(®_params[5]);
1698 destroy_reg_param(®_params[6]);
1699 destroy_reg_param(®_params[7]);
1700 destroy_reg_param(®_params[8]);
1701 destroy_reg_param(®_params[9]);
1706 static int cfi_spansion_write_block(struct flash_bank *bank, uint8_t *buffer,
1707 uint32_t address, uint32_t count)
1709 struct cfi_flash_bank *cfi_info = bank->driver_priv;
1710 struct cfi_spansion_pri_ext *pri_ext = cfi_info->pri_ext;
1711 struct target *target = bank->target;
1712 struct reg_param reg_params[10];
1713 struct arm_algorithm armv4_5_info;
1714 struct working_area *source;
1715 uint32_t buffer_size = 32768;
1717 int retval = ERROR_OK;
1719 /* input parameters - */
1720 /* R0 = source address */
1721 /* R1 = destination address */
1722 /* R2 = number of writes */
1723 /* R3 = flash write command */
1724 /* R4 = constant to mask DQ7 bits (also used for Dq5 with shift) */
1725 /* output parameters - */
1726 /* R5 = 0x80 ok 0x00 bad */
1727 /* temp registers - */
1728 /* R6 = value read from flash to test status */
1729 /* R7 = holding register */
1730 /* unlock registers - */
1731 /* R8 = unlock1_addr */
1732 /* R9 = unlock1_cmd */
1733 /* R10 = unlock2_addr */
1734 /* R11 = unlock2_cmd */
1736 /* see contib/loaders/flash/armv4_5_cfi_span_32.s for src */
1737 static const uint32_t armv4_5_word_32_code[] = {
1738 /* 00008100 <sp_32_code>: */
1739 0xe4905004, /* ldr r5, [r0], #4 */
1740 0xe5889000, /* str r9, [r8] */
1741 0xe58ab000, /* str r11, [r10] */
1742 0xe5883000, /* str r3, [r8] */
1743 0xe5815000, /* str r5, [r1] */
1744 0xe1a00000, /* nop */
1746 /* 00008110 <sp_32_busy>: */
1747 0xe5916000, /* ldr r6, [r1] */
1748 0xe0257006, /* eor r7, r5, r6 */
1749 0xe0147007, /* ands r7, r4, r7 */
1750 0x0a000007, /* beq 8140 <sp_32_cont> ; b if DQ7 == Data7 */
1751 0xe0166124, /* ands r6, r6, r4, lsr #2 */
1752 0x0afffff9, /* beq 8110 <sp_32_busy> ; b if DQ5 low */
1753 0xe5916000, /* ldr r6, [r1] */
1754 0xe0257006, /* eor r7, r5, r6 */
1755 0xe0147007, /* ands r7, r4, r7 */
1756 0x0a000001, /* beq 8140 <sp_32_cont> ; b if DQ7 == Data7 */
1757 0xe3a05000, /* mov r5, #0 ; 0x0 - return 0x00, error */
1758 0x1a000004, /* bne 8154 <sp_32_done> */
1760 /* 00008140 <sp_32_cont>: */
1761 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
1762 0x03a05080, /* moveq r5, #128 ; 0x80 */
1763 0x0a000001, /* beq 8154 <sp_32_done> */
1764 0xe2811004, /* add r1, r1, #4 ; 0x4 */
1765 0xeaffffe8, /* b 8100 <sp_32_code> */
1767 /* 00008154 <sp_32_done>: */
1768 0xeafffffe /* b 8154 <sp_32_done> */
1771 /* see contib/loaders/flash/armv4_5_cfi_span_16.s for src */
1772 static const uint32_t armv4_5_word_16_code[] = {
1773 /* 00008158 <sp_16_code>: */
1774 0xe0d050b2, /* ldrh r5, [r0], #2 */
1775 0xe1c890b0, /* strh r9, [r8] */
1776 0xe1cab0b0, /* strh r11, [r10] */
1777 0xe1c830b0, /* strh r3, [r8] */
1778 0xe1c150b0, /* strh r5, [r1] */
1779 0xe1a00000, /* nop (mov r0,r0) */
1781 /* 00008168 <sp_16_busy>: */
1782 0xe1d160b0, /* ldrh r6, [r1] */
1783 0xe0257006, /* eor r7, r5, r6 */
1784 0xe0147007, /* ands r7, r4, r7 */
1785 0x0a000007, /* beq 8198 <sp_16_cont> */
1786 0xe0166124, /* ands r6, r6, r4, lsr #2 */
1787 0x0afffff9, /* beq 8168 <sp_16_busy> */
1788 0xe1d160b0, /* ldrh r6, [r1] */
1789 0xe0257006, /* eor r7, r5, r6 */
1790 0xe0147007, /* ands r7, r4, r7 */
1791 0x0a000001, /* beq 8198 <sp_16_cont> */
1792 0xe3a05000, /* mov r5, #0 ; 0x0 */
1793 0x1a000004, /* bne 81ac <sp_16_done> */
1795 /* 00008198 <sp_16_cont>: */
1796 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
1797 0x03a05080, /* moveq r5, #128 ; 0x80 */
1798 0x0a000001, /* beq 81ac <sp_16_done> */
1799 0xe2811002, /* add r1, r1, #2 ; 0x2 */
1800 0xeaffffe8, /* b 8158 <sp_16_code> */
1802 /* 000081ac <sp_16_done>: */
1803 0xeafffffe /* b 81ac <sp_16_done> */
1806 /* see contib/loaders/flash/armv7m_cfi_span_16.s for src */
1807 static const uint32_t armv7m_word_16_code[] = {
1828 /* see contib/loaders/flash/armv4_5_cfi_span_16_dq7.s for src */
1829 static const uint32_t armv4_5_word_16_code_dq7only[] = {
1831 0xe0d050b2, /* ldrh r5, [r0], #2 */
1832 0xe1c890b0, /* strh r9, [r8] */
1833 0xe1cab0b0, /* strh r11, [r10] */
1834 0xe1c830b0, /* strh r3, [r8] */
1835 0xe1c150b0, /* strh r5, [r1] */
1836 0xe1a00000, /* nop (mov r0,r0) */
1839 0xe1d160b0, /* ldrh r6, [r1] */
1840 0xe0257006, /* eor r7, r5, r6 */
1841 0xe2177080, /* ands r7, #0x80 */
1842 0x1afffffb, /* bne 8168 <sp_16_busy> */
1844 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
1845 0x03a05080, /* moveq r5, #128 ; 0x80 */
1846 0x0a000001, /* beq 81ac <sp_16_done> */
1847 0xe2811002, /* add r1, r1, #2 ; 0x2 */
1848 0xeafffff0, /* b 8158 <sp_16_code> */
1850 /* 000081ac <sp_16_done>: */
1851 0xeafffffe /* b 81ac <sp_16_done> */
1854 /* see contib/loaders/flash/armv4_5_cfi_span_8.s for src */
1855 static const uint32_t armv4_5_word_8_code[] = {
1856 /* 000081b0 <sp_16_code_end>: */
1857 0xe4d05001, /* ldrb r5, [r0], #1 */
1858 0xe5c89000, /* strb r9, [r8] */
1859 0xe5cab000, /* strb r11, [r10] */
1860 0xe5c83000, /* strb r3, [r8] */
1861 0xe5c15000, /* strb r5, [r1] */
1862 0xe1a00000, /* nop (mov r0,r0) */
1864 /* 000081c0 <sp_8_busy>: */
1865 0xe5d16000, /* ldrb r6, [r1] */
1866 0xe0257006, /* eor r7, r5, r6 */
1867 0xe0147007, /* ands r7, r4, r7 */
1868 0x0a000007, /* beq 81f0 <sp_8_cont> */
1869 0xe0166124, /* ands r6, r6, r4, lsr #2 */
1870 0x0afffff9, /* beq 81c0 <sp_8_busy> */
1871 0xe5d16000, /* ldrb r6, [r1] */
1872 0xe0257006, /* eor r7, r5, r6 */
1873 0xe0147007, /* ands r7, r4, r7 */
1874 0x0a000001, /* beq 81f0 <sp_8_cont> */
1875 0xe3a05000, /* mov r5, #0 ; 0x0 */
1876 0x1a000004, /* bne 8204 <sp_8_done> */
1878 /* 000081f0 <sp_8_cont>: */
1879 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
1880 0x03a05080, /* moveq r5, #128 ; 0x80 */
1881 0x0a000001, /* beq 8204 <sp_8_done> */
1882 0xe2811001, /* add r1, r1, #1 ; 0x1 */
1883 0xeaffffe8, /* b 81b0 <sp_16_code_end> */
1885 /* 00008204 <sp_8_done>: */
1886 0xeafffffe /* b 8204 <sp_8_done> */
1889 if (strncmp(target_type_name(target),"mips_m4k",8) == 0)
1891 return cfi_spansion_write_block_mips(bank,buffer,address,count);
1894 if (is_armv7m(target_to_armv7m(target))) /* Cortex-M3 target */
1896 armv4_5_info.common_magic = ARMV7M_COMMON_MAGIC;
1897 armv4_5_info.core_mode = ARMV7M_MODE_HANDLER;
1898 armv4_5_info.core_state = ARM_STATE_ARM;
1899 } else if (is_arm7_9(target_to_arm7_9(target)))
1901 /* All other ARM CPUs have 32 bit instructions */
1902 armv4_5_info.common_magic = ARM_COMMON_MAGIC;
1903 armv4_5_info.core_mode = ARM_MODE_SVC;
1904 armv4_5_info.core_state = ARM_STATE_ARM;
1906 LOG_ERROR("Unknown ARM architecture");
1910 int target_code_size = 0;
1911 const uint32_t *target_code_src = NULL;
1913 switch (bank->bus_width)
1916 if (armv4_5_info.common_magic != ARM_COMMON_MAGIC) {
1917 LOG_ERROR("Unknown ARM architecture");
1920 target_code_src = armv4_5_word_8_code;
1921 target_code_size = sizeof(armv4_5_word_8_code);
1924 /* Check for DQ5 support */
1925 if( cfi_info->status_poll_mask & (1 << 5) )
1927 if(armv4_5_info.common_magic == ARM_COMMON_MAGIC) /* armv4_5 target */
1929 target_code_src = armv4_5_word_16_code;
1930 target_code_size = sizeof(armv4_5_word_16_code);
1932 else if (armv4_5_info.common_magic == ARMV7M_COMMON_MAGIC) /* cortex-m3 target */
1934 target_code_src = armv7m_word_16_code;
1935 target_code_size = sizeof(armv7m_word_16_code);
1940 /* No DQ5 support. Use DQ7 DATA# polling only. */
1941 if (armv4_5_info.common_magic != ARM_COMMON_MAGIC) {
1942 LOG_ERROR("Unknown ARM architecture");
1945 target_code_src = armv4_5_word_16_code_dq7only;
1946 target_code_size = sizeof(armv4_5_word_16_code_dq7only);
1950 if (armv4_5_info.common_magic != ARM_COMMON_MAGIC) {
1951 LOG_ERROR("Unknown ARM architecture");
1954 target_code_src = armv4_5_word_32_code;
1955 target_code_size = sizeof(armv4_5_word_32_code);
1958 LOG_ERROR("Unsupported bank buswidth %d, can't do block memory writes", bank->bus_width);
1959 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1962 /* flash write code */
1963 if (!cfi_info->write_algorithm)
1965 uint8_t *target_code;
1967 /* convert bus-width dependent algorithm code to correct endiannes */
1968 target_code = malloc(target_code_size);
1969 if (target_code == NULL)
1971 LOG_ERROR("Out of memory");
1974 cfi_fix_code_endian(target, target_code, target_code_src, target_code_size / 4);
1976 /* allocate working area */
1977 retval = target_alloc_working_area(target, target_code_size,
1978 &cfi_info->write_algorithm);
1979 if (retval != ERROR_OK)
1985 /* write algorithm code to working area */
1986 if ((retval = target_write_buffer(target, cfi_info->write_algorithm->address,
1987 target_code_size, target_code)) != ERROR_OK)
1995 /* the following code still assumes target code is fixed 24*4 bytes */
1997 while (target_alloc_working_area_try(target, buffer_size, &source) != ERROR_OK)
2000 if (buffer_size <= 256)
2002 /* if we already allocated the writing code, but failed to get a
2003 * buffer, free the algorithm */
2004 if (cfi_info->write_algorithm)
2005 target_free_working_area(target, cfi_info->write_algorithm);
2007 LOG_WARNING("not enough working area available, can't do block memory writes");
2008 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
2012 init_reg_param(®_params[0], "r0", 32, PARAM_OUT);
2013 init_reg_param(®_params[1], "r1", 32, PARAM_OUT);
2014 init_reg_param(®_params[2], "r2", 32, PARAM_OUT);
2015 init_reg_param(®_params[3], "r3", 32, PARAM_OUT);
2016 init_reg_param(®_params[4], "r4", 32, PARAM_OUT);
2017 init_reg_param(®_params[5], "r5", 32, PARAM_IN);
2018 init_reg_param(®_params[6], "r8", 32, PARAM_OUT);
2019 init_reg_param(®_params[7], "r9", 32, PARAM_OUT);
2020 init_reg_param(®_params[8], "r10", 32, PARAM_OUT);
2021 init_reg_param(®_params[9], "r11", 32, PARAM_OUT);
2025 uint32_t thisrun_count = (count > buffer_size) ? buffer_size : count;
2027 retval = target_write_buffer(target, source->address, thisrun_count, buffer);
2028 if (retval != ERROR_OK)
2033 buf_set_u32(reg_params[0].value, 0, 32, source->address);
2034 buf_set_u32(reg_params[1].value, 0, 32, address);
2035 buf_set_u32(reg_params[2].value, 0, 32, thisrun_count / bank->bus_width);
2036 buf_set_u32(reg_params[3].value, 0, 32, cfi_command_val(bank, 0xA0));
2037 buf_set_u32(reg_params[4].value, 0, 32, cfi_command_val(bank, 0x80));
2038 buf_set_u32(reg_params[6].value, 0, 32, flash_address(bank, 0, pri_ext->_unlock1));
2039 buf_set_u32(reg_params[7].value, 0, 32, 0xaaaaaaaa);
2040 buf_set_u32(reg_params[8].value, 0, 32, flash_address(bank, 0, pri_ext->_unlock2));
2041 buf_set_u32(reg_params[9].value, 0, 32, 0x55555555);
2043 retval = target_run_algorithm(target, 0, NULL, 10, reg_params,
2044 cfi_info->write_algorithm->address,
2045 cfi_info->write_algorithm->address + ((target_code_size) - 4),
2046 10000, &armv4_5_info);
2047 if (retval != ERROR_OK)
2052 status = buf_get_u32(reg_params[5].value, 0, 32);
2055 LOG_ERROR("flash write block failed status: 0x%" PRIx32 , status);
2056 retval = ERROR_FLASH_OPERATION_FAILED;
2060 buffer += thisrun_count;
2061 address += thisrun_count;
2062 count -= thisrun_count;
2065 target_free_all_working_areas(target);
2067 destroy_reg_param(®_params[0]);
2068 destroy_reg_param(®_params[1]);
2069 destroy_reg_param(®_params[2]);
2070 destroy_reg_param(®_params[3]);
2071 destroy_reg_param(®_params[4]);
2072 destroy_reg_param(®_params[5]);
2073 destroy_reg_param(®_params[6]);
2074 destroy_reg_param(®_params[7]);
2075 destroy_reg_param(®_params[8]);
2076 destroy_reg_param(®_params[9]);
2081 static int cfi_intel_write_word(struct flash_bank *bank, uint8_t *word, uint32_t address)
2084 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2085 struct target *target = bank->target;
2087 cfi_intel_clear_status_register(bank);
2088 if ((retval = cfi_send_command(bank, 0x40, address)) != ERROR_OK)
2093 if ((retval = target_write_memory(target, address, bank->bus_width, 1, word)) != ERROR_OK)
2099 retval = cfi_intel_wait_status_busy(bank, cfi_info->word_write_timeout, &status);
2102 if ((retval = cfi_send_command(bank, 0xff, flash_address(bank, 0, 0x0))) != ERROR_OK)
2107 LOG_ERROR("couldn't write word at base 0x%" PRIx32 ", address 0x%" PRIx32,
2108 bank->base, address);
2109 return ERROR_FLASH_OPERATION_FAILED;
2115 static int cfi_intel_write_words(struct flash_bank *bank, uint8_t *word,
2116 uint32_t wordcount, uint32_t address)
2119 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2120 struct target *target = bank->target;
2122 /* Calculate buffer size and boundary mask */
2123 /* buffersize is (buffer size per chip) * (number of chips) */
2124 /* bufferwsize is buffersize in words */
2125 uint32_t buffersize = (1UL << cfi_info->max_buf_write_size) * (bank->bus_width / bank->chip_width);
2126 uint32_t buffermask = buffersize-1;
2127 uint32_t bufferwsize = buffersize / bank->bus_width;
2129 /* Check for valid range */
2130 if (address & buffermask)
2132 LOG_ERROR("Write address at base 0x%" PRIx32 ", address 0x%" PRIx32
2133 " not aligned to 2^%d boundary",
2134 bank->base, address, cfi_info->max_buf_write_size);
2135 return ERROR_FLASH_OPERATION_FAILED;
2138 /* Check for valid size */
2139 if (wordcount > bufferwsize)
2141 LOG_ERROR("Number of data words %" PRId32 " exceeds available buffersize %" PRId32,
2142 wordcount, buffersize);
2143 return ERROR_FLASH_OPERATION_FAILED;
2146 /* Write to flash buffer */
2147 cfi_intel_clear_status_register(bank);
2149 /* Initiate buffer operation _*/
2150 if ((retval = cfi_send_command(bank, 0xe8, address)) != ERROR_OK)
2155 retval = cfi_intel_wait_status_busy(bank, cfi_info->buf_write_timeout, &status);
2156 if (retval != ERROR_OK)
2160 if ((retval = cfi_send_command(bank, 0xff, flash_address(bank, 0, 0x0))) != ERROR_OK)
2165 LOG_ERROR("couldn't start buffer write operation at base 0x%" PRIx32 ", address 0x%" PRIx32,
2166 bank->base, address);
2167 return ERROR_FLASH_OPERATION_FAILED;
2170 /* Write buffer wordcount-1 and data words */
2171 if ((retval = cfi_send_command(bank, bufferwsize-1, address)) != ERROR_OK)
2176 if ((retval = target_write_memory(target,
2177 address, bank->bus_width, bufferwsize, word)) != ERROR_OK)
2182 /* Commit write operation */
2183 if ((retval = cfi_send_command(bank, 0xd0, address)) != ERROR_OK)
2188 retval = cfi_intel_wait_status_busy(bank, cfi_info->buf_write_timeout, &status);
2189 if (retval != ERROR_OK)
2194 if ((retval = cfi_send_command(bank, 0xff,
2195 flash_address(bank, 0, 0x0))) != ERROR_OK)
2200 LOG_ERROR("Buffer write at base 0x%" PRIx32
2201 ", address 0x%" PRIx32 " failed.", bank->base, address);
2202 return ERROR_FLASH_OPERATION_FAILED;
2208 static int cfi_spansion_write_word(struct flash_bank *bank, uint8_t *word, uint32_t address)
2211 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2212 struct cfi_spansion_pri_ext *pri_ext = cfi_info->pri_ext;
2213 struct target *target = bank->target;
2215 if ((retval = cfi_send_command(bank, 0xaa,
2216 flash_address(bank, 0, pri_ext->_unlock1))) != ERROR_OK)
2221 if ((retval = cfi_send_command(bank, 0x55,
2222 flash_address(bank, 0, pri_ext->_unlock2))) != ERROR_OK)
2227 if ((retval = cfi_send_command(bank, 0xa0,
2228 flash_address(bank, 0, pri_ext->_unlock1))) != ERROR_OK)
2233 if ((retval = target_write_memory(target,
2234 address, bank->bus_width, 1, word)) != ERROR_OK)
2239 if (cfi_spansion_wait_status_busy(bank, cfi_info->word_write_timeout) != ERROR_OK)
2241 if ((retval = cfi_send_command(bank, 0xf0, flash_address(bank, 0, 0x0))) != ERROR_OK)
2246 LOG_ERROR("couldn't write word at base 0x%" PRIx32
2247 ", address 0x%" PRIx32 , bank->base, address);
2248 return ERROR_FLASH_OPERATION_FAILED;
2254 static int cfi_spansion_write_words(struct flash_bank *bank, uint8_t *word,
2255 uint32_t wordcount, uint32_t address)
2258 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2259 struct target *target = bank->target;
2260 struct cfi_spansion_pri_ext *pri_ext = cfi_info->pri_ext;
2262 /* Calculate buffer size and boundary mask */
2263 /* buffersize is (buffer size per chip) * (number of chips) */
2264 /* bufferwsize is buffersize in words */
2265 uint32_t buffersize = (1UL << cfi_info->max_buf_write_size) * (bank->bus_width / bank->chip_width);
2266 uint32_t buffermask = buffersize-1;
2267 uint32_t bufferwsize = buffersize / bank->bus_width;
2269 /* Check for valid range */
2270 if (address & buffermask)
2272 LOG_ERROR("Write address at base 0x%" PRIx32
2273 ", address 0x%" PRIx32 " not aligned to 2^%d boundary",
2274 bank->base, address, cfi_info->max_buf_write_size);
2275 return ERROR_FLASH_OPERATION_FAILED;
2278 /* Check for valid size */
2279 if (wordcount > bufferwsize)
2281 LOG_ERROR("Number of data words %" PRId32 " exceeds available buffersize %"
2282 PRId32, wordcount, buffersize);
2283 return ERROR_FLASH_OPERATION_FAILED;
2287 if ((retval = cfi_send_command(bank, 0xaa,
2288 flash_address(bank, 0, pri_ext->_unlock1))) != ERROR_OK)
2293 if ((retval = cfi_send_command(bank, 0x55,
2294 flash_address(bank, 0, pri_ext->_unlock2))) != ERROR_OK)
2299 /* Buffer load command */
2300 if ((retval = cfi_send_command(bank, 0x25, address)) != ERROR_OK)
2305 /* Write buffer wordcount-1 and data words */
2306 if ((retval = cfi_send_command(bank, bufferwsize-1, address)) != ERROR_OK)
2311 if ((retval = target_write_memory(target,
2312 address, bank->bus_width, bufferwsize, word)) != ERROR_OK)
2317 /* Commit write operation */
2318 if ((retval = cfi_send_command(bank, 0x29, address)) != ERROR_OK)
2323 if (cfi_spansion_wait_status_busy(bank, cfi_info->buf_write_timeout) != ERROR_OK)
2325 if ((retval = cfi_send_command(bank, 0xf0,
2326 flash_address(bank, 0, 0x0))) != ERROR_OK)
2331 LOG_ERROR("couldn't write block at base 0x%" PRIx32
2332 ", address 0x%" PRIx32 ", size 0x%" PRIx32, bank->base, address, bufferwsize);
2333 return ERROR_FLASH_OPERATION_FAILED;
2339 static int cfi_write_word(struct flash_bank *bank, uint8_t *word, uint32_t address)
2341 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2343 switch (cfi_info->pri_id)
2347 return cfi_intel_write_word(bank, word, address);
2350 return cfi_spansion_write_word(bank, word, address);
2353 LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
2357 return ERROR_FLASH_OPERATION_FAILED;
2360 static int cfi_write_words(struct flash_bank *bank, uint8_t *word,
2361 uint32_t wordcount, uint32_t address)
2363 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2365 if (cfi_info->buf_write_timeout_typ == 0)
2367 /* buffer writes are not supported */
2368 LOG_DEBUG("Buffer Writes Not Supported");
2369 return ERROR_FLASH_OPER_UNSUPPORTED;
2372 switch (cfi_info->pri_id)
2376 return cfi_intel_write_words(bank, word, wordcount, address);
2379 return cfi_spansion_write_words(bank, word, wordcount, address);
2382 LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
2386 return ERROR_FLASH_OPERATION_FAILED;
2389 static int cfi_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
2391 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2392 struct target *target = bank->target;
2393 uint32_t address = bank->base + offset;
2395 int align; /* number of unaligned bytes */
2396 uint8_t current_word[CFI_MAX_BUS_WIDTH];
2400 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
2401 (int)count, (unsigned)offset);
2403 if (bank->target->state != TARGET_HALTED)
2405 LOG_ERROR("Target not halted");
2406 return ERROR_TARGET_NOT_HALTED;
2409 if (offset + count > bank->size)
2410 return ERROR_FLASH_DST_OUT_OF_BANK;
2412 if (cfi_info->qry[0] != 'Q')
2413 return ERROR_FLASH_BANK_NOT_PROBED;
2415 /* start at the first byte of the first word (bus_width size) */
2416 read_p = address & ~(bank->bus_width - 1);
2417 if ((align = address - read_p) != 0)
2419 LOG_INFO("Fixup %d unaligned read head bytes", align);
2421 /* read a complete word from flash */
2422 if ((retval = target_read_memory(target, read_p,
2423 bank->bus_width, 1, current_word)) != ERROR_OK)
2426 /* take only bytes we need */
2427 for (i = align; (i < bank->bus_width) && (count > 0); i++, count--)
2428 *buffer++ = current_word[i];
2430 read_p += bank->bus_width;
2433 align = count / bank->bus_width;
2436 if ((retval = target_read_memory(target, read_p,
2437 bank->bus_width, align, buffer)) != ERROR_OK)
2440 read_p += align * bank->bus_width;
2441 buffer += align * bank->bus_width;
2442 count -= align * bank->bus_width;
2447 LOG_INFO("Fixup %d unaligned read tail bytes", count);
2449 /* read a complete word from flash */
2450 if ((retval = target_read_memory(target, read_p,
2451 bank->bus_width, 1, current_word)) != ERROR_OK)
2454 /* take only bytes we need */
2455 for (i = 0; (i < bank->bus_width) && (count > 0); i++, count--)
2456 *buffer++ = current_word[i];
2462 static int cfi_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
2464 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2465 struct target *target = bank->target;
2466 uint32_t address = bank->base + offset; /* address of first byte to be programmed */
2468 int align; /* number of unaligned bytes */
2469 int blk_count; /* number of bus_width bytes for block copy */
2470 uint8_t current_word[CFI_MAX_BUS_WIDTH * 4]; /* word (bus_width size) currently being programmed */
2474 if (bank->target->state != TARGET_HALTED)
2476 LOG_ERROR("Target not halted");
2477 return ERROR_TARGET_NOT_HALTED;
2480 if (offset + count > bank->size)
2481 return ERROR_FLASH_DST_OUT_OF_BANK;
2483 if (cfi_info->qry[0] != 'Q')
2484 return ERROR_FLASH_BANK_NOT_PROBED;
2486 /* start at the first byte of the first word (bus_width size) */
2487 write_p = address & ~(bank->bus_width - 1);
2488 if ((align = address - write_p) != 0)
2490 LOG_INFO("Fixup %d unaligned head bytes", align);
2492 /* read a complete word from flash */
2493 if ((retval = target_read_memory(target, write_p,
2494 bank->bus_width, 1, current_word)) != ERROR_OK)
2497 /* replace only bytes that must be written */
2498 for (i = align; (i < bank->bus_width) && (count > 0); i++, count--)
2499 current_word[i] = *buffer++;
2501 retval = cfi_write_word(bank, current_word, write_p);
2502 if (retval != ERROR_OK)
2504 write_p += bank->bus_width;
2507 /* handle blocks of bus_size aligned bytes */
2508 blk_count = count & ~(bank->bus_width - 1); /* round down, leave tail bytes */
2509 switch (cfi_info->pri_id)
2511 /* try block writes (fails without working area) */
2514 retval = cfi_intel_write_block(bank, buffer, write_p, blk_count);
2517 retval = cfi_spansion_write_block(bank, buffer, write_p, blk_count);
2520 LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
2521 retval = ERROR_FLASH_OPERATION_FAILED;
2524 if (retval == ERROR_OK)
2526 /* Increment pointers and decrease count on succesful block write */
2527 buffer += blk_count;
2528 write_p += blk_count;
2533 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
2535 /* Calculate buffer size and boundary mask */
2536 /* buffersize is (buffer size per chip) * (number of chips) */
2537 /* bufferwsize is buffersize in words */
2538 uint32_t buffersize = (1UL << cfi_info->max_buf_write_size) * (bank->bus_width / bank->chip_width);
2539 uint32_t buffermask = buffersize-1;
2540 uint32_t bufferwsize = buffersize / bank->bus_width;
2542 /* fall back to memory writes */
2543 while (count >= (uint32_t)bank->bus_width)
2546 if ((write_p & 0xff) == 0)
2548 LOG_INFO("Programming at 0x%08" PRIx32 ", count 0x%08"
2549 PRIx32 " bytes remaining", write_p, count);
2552 if ((bufferwsize > 0) && (count >= buffersize) && !(write_p & buffermask))
2554 retval = cfi_write_words(bank, buffer, bufferwsize, write_p);
2555 if (retval == ERROR_OK)
2557 buffer += buffersize;
2558 write_p += buffersize;
2559 count -= buffersize;
2562 else if (retval != ERROR_FLASH_OPER_UNSUPPORTED)
2565 /* try the slow way? */
2568 for (i = 0; i < bank->bus_width; i++)
2569 current_word[i] = *buffer++;
2571 retval = cfi_write_word(bank, current_word, write_p);
2572 if (retval != ERROR_OK)
2575 write_p += bank->bus_width;
2576 count -= bank->bus_width;
2584 /* return to read array mode, so we can read from flash again for padding */
2585 if ((retval = cfi_reset(bank)) != ERROR_OK)
2590 /* handle unaligned tail bytes */
2593 LOG_INFO("Fixup %" PRId32 " unaligned tail bytes", count);
2595 /* read a complete word from flash */
2596 if ((retval = target_read_memory(target, write_p,
2597 bank->bus_width, 1, current_word)) != ERROR_OK)
2600 /* replace only bytes that must be written */
2601 for (i = 0; (i < bank->bus_width) && (count > 0); i++, count--)
2602 current_word[i] = *buffer++;
2604 retval = cfi_write_word(bank, current_word, write_p);
2605 if (retval != ERROR_OK)
2609 /* return to read array mode */
2610 return cfi_reset(bank);
2613 static void cfi_fixup_reversed_erase_regions(struct flash_bank *bank, void *param)
2616 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2617 struct cfi_spansion_pri_ext *pri_ext = cfi_info->pri_ext;
2619 pri_ext->_reversed_geometry = 1;
2622 static void cfi_fixup_0002_erase_regions(struct flash_bank *bank, void *param)
2625 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2626 struct cfi_spansion_pri_ext *pri_ext = cfi_info->pri_ext;
2629 if ((pri_ext->_reversed_geometry) || (pri_ext->TopBottom == 3))
2631 LOG_DEBUG("swapping reversed erase region information on cmdset 0002 device");
2633 for (i = 0; i < cfi_info->num_erase_regions / 2; i++)
2635 int j = (cfi_info->num_erase_regions - 1) - i;
2638 swap = cfi_info->erase_region_info[i];
2639 cfi_info->erase_region_info[i] = cfi_info->erase_region_info[j];
2640 cfi_info->erase_region_info[j] = swap;
2645 static void cfi_fixup_0002_unlock_addresses(struct flash_bank *bank, void *param)
2647 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2648 struct cfi_spansion_pri_ext *pri_ext = cfi_info->pri_ext;
2649 struct cfi_unlock_addresses *unlock_addresses = param;
2651 pri_ext->_unlock1 = unlock_addresses->unlock1;
2652 pri_ext->_unlock2 = unlock_addresses->unlock2;
2656 static int cfi_query_string(struct flash_bank *bank, int address)
2658 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2661 if ((retval = cfi_send_command(bank, 0x98, flash_address(bank, 0, address))) != ERROR_OK)
2666 retval = cfi_query_u8(bank, 0, 0x10, &cfi_info->qry[0]);
2667 if (retval != ERROR_OK)
2669 retval = cfi_query_u8(bank, 0, 0x11, &cfi_info->qry[1]);
2670 if (retval != ERROR_OK)
2672 retval = cfi_query_u8(bank, 0, 0x12, &cfi_info->qry[2]);
2673 if (retval != ERROR_OK)
2676 LOG_DEBUG("CFI qry returned: 0x%2.2x 0x%2.2x 0x%2.2x",
2677 cfi_info->qry[0], cfi_info->qry[1], cfi_info->qry[2]);
2679 if ((cfi_info->qry[0] != 'Q') || (cfi_info->qry[1] != 'R') || (cfi_info->qry[2] != 'Y'))
2681 if ((retval = cfi_reset(bank)) != ERROR_OK)
2685 LOG_ERROR("Could not probe bank: no QRY");
2686 return ERROR_FLASH_BANK_INVALID;
2692 static int cfi_probe(struct flash_bank *bank)
2694 struct cfi_flash_bank *cfi_info = bank->driver_priv;
2695 struct target *target = bank->target;
2696 int num_sectors = 0;
2699 uint32_t unlock1 = 0x555;
2700 uint32_t unlock2 = 0x2aa;
2702 uint8_t value_buf0[CFI_MAX_BUS_WIDTH], value_buf1[CFI_MAX_BUS_WIDTH];
2704 if (bank->target->state != TARGET_HALTED)
2706 LOG_ERROR("Target not halted");
2707 return ERROR_TARGET_NOT_HALTED;
2710 cfi_info->probed = 0;
2711 cfi_info->num_erase_regions = 0;
2714 free(bank->sectors);
2715 bank->sectors = NULL;
2717 if(cfi_info->erase_region_info)
2719 free(cfi_info->erase_region_info);
2720 cfi_info->erase_region_info = NULL;
2723 /* JEDEC standard JESD21C uses 0x5555 and 0x2aaa as unlock addresses,
2724 * while CFI compatible AMD/Spansion flashes use 0x555 and 0x2aa
2726 if (cfi_info->jedec_probe)
2732 /* switch to read identifier codes mode ("AUTOSELECT") */
2733 if ((retval = cfi_send_command(bank, 0xaa, flash_address(bank, 0, unlock1))) != ERROR_OK)
2737 if ((retval = cfi_send_command(bank, 0x55, flash_address(bank, 0, unlock2))) != ERROR_OK)
2741 if ((retval = cfi_send_command(bank, 0x90, flash_address(bank, 0, unlock1))) != ERROR_OK)
2746 if ((retval = target_read_memory(target, flash_address(bank, 0, 0x00),
2747 bank->bus_width, 1, value_buf0)) != ERROR_OK)
2751 if ((retval = target_read_memory(target, flash_address(bank, 0, 0x01),
2752 bank->bus_width, 1, value_buf1)) != ERROR_OK)
2756 switch (bank->chip_width) {
2758 cfi_info->manufacturer = *value_buf0;
2759 cfi_info->device_id = *value_buf1;
2762 cfi_info->manufacturer = target_buffer_get_u16(target, value_buf0);
2763 cfi_info->device_id = target_buffer_get_u16(target, value_buf1);
2766 cfi_info->manufacturer = target_buffer_get_u32(target, value_buf0);
2767 cfi_info->device_id = target_buffer_get_u32(target, value_buf1);
2770 LOG_ERROR("Unsupported bank chipwidth %d, can't probe memory", bank->chip_width);
2771 return ERROR_FLASH_OPERATION_FAILED;
2774 LOG_INFO("Flash Manufacturer/Device: 0x%04x 0x%04x",
2775 cfi_info->manufacturer, cfi_info->device_id);
2776 /* switch back to read array mode */
2777 if ((retval = cfi_reset(bank)) != ERROR_OK)
2782 /* check device/manufacturer ID for known non-CFI flashes. */
2783 cfi_fixup_non_cfi(bank);
2785 /* query only if this is a CFI compatible flash,
2786 * otherwise the relevant info has already been filled in
2788 if (cfi_info->not_cfi == 0)
2790 /* enter CFI query mode
2791 * according to JEDEC Standard No. 68.01,
2792 * a single bus sequence with address = 0x55, data = 0x98 should put
2793 * the device into CFI query mode.
2795 * SST flashes clearly violate this, and we will consider them incompatbile for now
2798 retval = cfi_query_string(bank, 0x55);
2799 if (retval != ERROR_OK)
2802 * Spansion S29WS-N CFI query fix is to try 0x555 if 0x55 fails. Should
2803 * be harmless enough:
2805 * http://www.infradead.org/pipermail/linux-mtd/2005-September/013618.html
2807 LOG_USER("Try workaround w/0x555 instead of 0x55 to get QRY.");
2808 retval = cfi_query_string(bank, 0x555);
2810 if (retval != ERROR_OK)
2813 retval = cfi_query_u16(bank, 0, 0x13, &cfi_info->pri_id);
2814 if (retval != ERROR_OK)
2816 retval = cfi_query_u16(bank, 0, 0x15, &cfi_info->pri_addr);
2817 if (retval != ERROR_OK)
2819 retval = cfi_query_u16(bank, 0, 0x17, &cfi_info->alt_id);
2820 if (retval != ERROR_OK)
2822 retval = cfi_query_u16(bank, 0, 0x19, &cfi_info->alt_addr);
2823 if (retval != ERROR_OK)
2826 LOG_DEBUG("qry: '%c%c%c', pri_id: 0x%4.4x, pri_addr: 0x%4.4x, alt_id: "
2827 "0x%4.4x, alt_addr: 0x%4.4x", cfi_info->qry[0], cfi_info->qry[1],
2828 cfi_info->qry[2], cfi_info->pri_id, cfi_info->pri_addr,
2829 cfi_info->alt_id, cfi_info->alt_addr);
2831 retval = cfi_query_u8(bank, 0, 0x1b, &cfi_info->vcc_min);
2832 if (retval != ERROR_OK)
2834 retval = cfi_query_u8(bank, 0, 0x1c, &cfi_info->vcc_max);
2835 if (retval != ERROR_OK)
2837 retval = cfi_query_u8(bank, 0, 0x1d, &cfi_info->vpp_min);
2838 if (retval != ERROR_OK)
2840 retval = cfi_query_u8(bank, 0, 0x1e, &cfi_info->vpp_max);
2841 if (retval != ERROR_OK)
2844 retval = cfi_query_u8(bank, 0, 0x1f, &cfi_info->word_write_timeout_typ);
2845 if (retval != ERROR_OK)
2847 retval = cfi_query_u8(bank, 0, 0x20, &cfi_info->buf_write_timeout_typ);
2848 if (retval != ERROR_OK)
2850 retval = cfi_query_u8(bank, 0, 0x21, &cfi_info->block_erase_timeout_typ);
2851 if (retval != ERROR_OK)
2853 retval = cfi_query_u8(bank, 0, 0x22, &cfi_info->chip_erase_timeout_typ);
2854 if (retval != ERROR_OK)
2856 retval = cfi_query_u8(bank, 0, 0x23, &cfi_info->word_write_timeout_max);
2857 if (retval != ERROR_OK)
2859 retval = cfi_query_u8(bank, 0, 0x24, &cfi_info->buf_write_timeout_max);
2860 if (retval != ERROR_OK)
2862 retval = cfi_query_u8(bank, 0, 0x25, &cfi_info->block_erase_timeout_max);
2863 if (retval != ERROR_OK)
2865 retval = cfi_query_u8(bank, 0, 0x26, &cfi_info->chip_erase_timeout_max);
2866 if (retval != ERROR_OK)
2870 retval = cfi_query_u8(bank, 0, 0x27, &data);
2871 if (retval != ERROR_OK)
2873 cfi_info->dev_size = 1 << data;
2875 retval = cfi_query_u16(bank, 0, 0x28, &cfi_info->interface_desc);
2876 if (retval != ERROR_OK)
2878 retval = cfi_query_u16(bank, 0, 0x2a, &cfi_info->max_buf_write_size);
2879 if (retval != ERROR_OK)
2881 retval = cfi_query_u8(bank, 0, 0x2c, &cfi_info->num_erase_regions);
2882 if (retval != ERROR_OK)
2885 LOG_DEBUG("size: 0x%" PRIx32 ", interface desc: %i, max buffer write size: 0x%x",
2886 cfi_info->dev_size, cfi_info->interface_desc, (1 << cfi_info->max_buf_write_size));
2888 if (cfi_info->num_erase_regions)
2890 cfi_info->erase_region_info = malloc(sizeof(*cfi_info->erase_region_info)
2891 * cfi_info->num_erase_regions);
2892 for (i = 0; i < cfi_info->num_erase_regions; i++)
2894 retval = cfi_query_u32(bank, 0, 0x2d + (4 * i), &cfi_info->erase_region_info[i]);
2895 if (retval != ERROR_OK)
2897 LOG_DEBUG("erase region[%i]: %" PRIu32 " blocks of size 0x%" PRIx32 "", i,
2898 (cfi_info->erase_region_info[i] & 0xffff) + 1,
2899 (cfi_info->erase_region_info[i] >> 16) * 256);
2904 cfi_info->erase_region_info = NULL;
2907 /* We need to read the primary algorithm extended query table before calculating
2908 * the sector layout to be able to apply fixups
2910 switch (cfi_info->pri_id)
2912 /* Intel command set (standard and extended) */
2915 cfi_read_intel_pri_ext(bank);
2917 /* AMD/Spansion, Atmel, ... command set */
2919 cfi_info->status_poll_mask = CFI_STATUS_POLL_MASK_DQ5_DQ6_DQ7; /* default for all CFI flashs */
2920 cfi_read_0002_pri_ext(bank);
2923 LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
2927 /* return to read array mode
2928 * we use both reset commands, as some Intel flashes fail to recognize the 0xF0 command
2930 if ((retval = cfi_reset(bank)) != ERROR_OK)
2934 } /* end CFI case */
2936 LOG_DEBUG("Vcc min: %x.%x, Vcc max: %x.%x, Vpp min: %u.%x, Vpp max: %u.%x",
2937 (cfi_info->vcc_min & 0xf0) >> 4, cfi_info->vcc_min & 0x0f,
2938 (cfi_info->vcc_max & 0xf0) >> 4, cfi_info->vcc_max & 0x0f,
2939 (cfi_info->vpp_min & 0xf0) >> 4, cfi_info->vpp_min & 0x0f,
2940 (cfi_info->vpp_max & 0xf0) >> 4, cfi_info->vpp_max & 0x0f);
2942 LOG_DEBUG("typ. word write timeout: %u us, typ. buf write timeout: %u us, "
2943 "typ. block erase timeout: %u ms, typ. chip erase timeout: %u ms",
2944 1 << cfi_info->word_write_timeout_typ, 1 << cfi_info->buf_write_timeout_typ,
2945 1 << cfi_info->block_erase_timeout_typ, 1 << cfi_info->chip_erase_timeout_typ);
2947 LOG_DEBUG("max. word write timeout: %u us, max. buf write timeout: %u us, "
2948 "max. block erase timeout: %u ms, max. chip erase timeout: %u ms",
2949 (1 << cfi_info->word_write_timeout_max) * (1 << cfi_info->word_write_timeout_typ),
2950 (1 << cfi_info->buf_write_timeout_max) * (1 << cfi_info->buf_write_timeout_typ),
2951 (1 << cfi_info->block_erase_timeout_max) * (1 << cfi_info->block_erase_timeout_typ),
2952 (1 << cfi_info->chip_erase_timeout_max) * (1 << cfi_info->chip_erase_timeout_typ));
2954 /* convert timeouts to real values in ms */
2955 cfi_info->word_write_timeout = DIV_ROUND_UP((1L << cfi_info->word_write_timeout_typ) *
2956 (1L << cfi_info->word_write_timeout_max), 1000);
2957 cfi_info->buf_write_timeout = DIV_ROUND_UP((1L << cfi_info->buf_write_timeout_typ) *
2958 (1L << cfi_info->buf_write_timeout_max), 1000);
2959 cfi_info->block_erase_timeout = (1L << cfi_info->block_erase_timeout_typ) *
2960 (1L << cfi_info->block_erase_timeout_max);
2961 cfi_info->chip_erase_timeout = (1L << cfi_info->chip_erase_timeout_typ) *
2962 (1L << cfi_info->chip_erase_timeout_max);
2964 LOG_DEBUG("calculated word write timeout: %u ms, buf write timeout: %u ms, "
2965 "block erase timeout: %u ms, chip erase timeout: %u ms",
2966 cfi_info->word_write_timeout, cfi_info->buf_write_timeout,
2967 cfi_info->block_erase_timeout, cfi_info->chip_erase_timeout);
2969 /* apply fixups depending on the primary command set */
2970 switch (cfi_info->pri_id)
2972 /* Intel command set (standard and extended) */
2975 cfi_fixup(bank, cfi_0001_fixups);
2977 /* AMD/Spansion, Atmel, ... command set */
2979 cfi_fixup(bank, cfi_0002_fixups);
2982 LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
2986 if ((cfi_info->dev_size * bank->bus_width / bank->chip_width) != bank->size)
2988 LOG_WARNING("configuration specifies 0x%" PRIx32 " size, but a 0x%" PRIx32
2989 " size flash was found", bank->size, cfi_info->dev_size);
2992 if (cfi_info->num_erase_regions == 0)
2994 /* a device might have only one erase block, spanning the whole device */
2995 bank->num_sectors = 1;
2996 bank->sectors = malloc(sizeof(struct flash_sector));
2998 bank->sectors[sector].offset = 0x0;
2999 bank->sectors[sector].size = bank->size;
3000 bank->sectors[sector].is_erased = -1;
3001 bank->sectors[sector].is_protected = -1;
3005 uint32_t offset = 0;
3007 for (i = 0; i < cfi_info->num_erase_regions; i++)
3009 num_sectors += (cfi_info->erase_region_info[i] & 0xffff) + 1;
3012 bank->num_sectors = num_sectors;
3013 bank->sectors = malloc(sizeof(struct flash_sector) * num_sectors);
3015 for (i = 0; i < cfi_info->num_erase_regions; i++)
3018 for (j = 0; j < (cfi_info->erase_region_info[i] & 0xffff) + 1; j++)
3020 bank->sectors[sector].offset = offset;
3021 bank->sectors[sector].size = ((cfi_info->erase_region_info[i] >> 16) * 256)
3022 * bank->bus_width / bank->chip_width;
3023 offset += bank->sectors[sector].size;
3024 bank->sectors[sector].is_erased = -1;
3025 bank->sectors[sector].is_protected = -1;
3029 if (offset != (cfi_info->dev_size * bank->bus_width / bank->chip_width))
3031 LOG_WARNING("CFI size is 0x%" PRIx32 ", but total sector size is 0x%" PRIx32 "", \
3032 (cfi_info->dev_size * bank->bus_width / bank->chip_width), offset);
3036 cfi_info->probed = 1;
3041 static int cfi_auto_probe(struct flash_bank *bank)
3043 struct cfi_flash_bank *cfi_info = bank->driver_priv;
3044 if (cfi_info->probed)
3046 return cfi_probe(bank);
3049 static int cfi_intel_protect_check(struct flash_bank *bank)
3052 struct cfi_flash_bank *cfi_info = bank->driver_priv;
3053 struct cfi_intel_pri_ext *pri_ext = cfi_info->pri_ext;
3056 /* check if block lock bits are supported on this device */
3057 if (!(pri_ext->blk_status_reg_mask & 0x1))
3058 return ERROR_FLASH_OPERATION_FAILED;
3060 if ((retval = cfi_send_command(bank, 0x90, flash_address(bank, 0, 0x55))) != ERROR_OK)
3065 for (i = 0; i < bank->num_sectors; i++)
3067 uint8_t block_status;
3068 retval = cfi_get_u8(bank, i, 0x2, &block_status);
3069 if (retval != ERROR_OK)
3072 if (block_status & 1)
3073 bank->sectors[i].is_protected = 1;
3075 bank->sectors[i].is_protected = 0;
3078 return cfi_send_command(bank, 0xff, flash_address(bank, 0, 0x0));
3081 static int cfi_spansion_protect_check(struct flash_bank *bank)
3084 struct cfi_flash_bank *cfi_info = bank->driver_priv;
3085 struct cfi_spansion_pri_ext *pri_ext = cfi_info->pri_ext;
3088 if ((retval = cfi_send_command(bank, 0xaa,
3089 flash_address(bank, 0, pri_ext->_unlock1))) != ERROR_OK)
3094 if ((retval = cfi_send_command(bank, 0x55,
3095 flash_address(bank, 0, pri_ext->_unlock2))) != ERROR_OK)
3100 if ((retval = cfi_send_command(bank, 0x90,
3101 flash_address(bank, 0, pri_ext->_unlock1))) != ERROR_OK)
3106 for (i = 0; i < bank->num_sectors; i++)
3108 uint8_t block_status;
3109 retval = cfi_get_u8(bank, i, 0x2, &block_status);
3110 if (retval != ERROR_OK)
3113 if (block_status & 1)
3114 bank->sectors[i].is_protected = 1;
3116 bank->sectors[i].is_protected = 0;
3119 return cfi_send_command(bank, 0xf0, flash_address(bank, 0, 0x0));
3122 static int cfi_protect_check(struct flash_bank *bank)
3124 struct cfi_flash_bank *cfi_info = bank->driver_priv;
3126 if (bank->target->state != TARGET_HALTED)
3128 LOG_ERROR("Target not halted");
3129 return ERROR_TARGET_NOT_HALTED;
3132 if (cfi_info->qry[0] != 'Q')
3133 return ERROR_FLASH_BANK_NOT_PROBED;
3135 switch (cfi_info->pri_id)
3139 return cfi_intel_protect_check(bank);
3142 return cfi_spansion_protect_check(bank);
3145 LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
3152 static int get_cfi_info(struct flash_bank *bank, char *buf, int buf_size)
3155 struct cfi_flash_bank *cfi_info = bank->driver_priv;
3157 if (cfi_info->qry[0] == 0xff)
3159 snprintf(buf, buf_size, "\ncfi flash bank not probed yet\n");
3163 if (cfi_info->not_cfi == 0)
3164 printed = snprintf(buf, buf_size, "\nCFI flash: ");
3166 printed = snprintf(buf, buf_size, "\nnon-CFI flash: ");
3168 buf_size -= printed;
3170 printed = snprintf(buf, buf_size, "mfr: 0x%4.4x, id:0x%4.4x\n\n",
3171 cfi_info->manufacturer, cfi_info->device_id);
3173 buf_size -= printed;
3175 printed = snprintf(buf, buf_size, "qry: '%c%c%c', pri_id: 0x%4.4x, pri_addr: "
3176 "0x%4.4x, alt_id: 0x%4.4x, alt_addr: 0x%4.4x\n",
3177 cfi_info->qry[0], cfi_info->qry[1], cfi_info->qry[2],
3178 cfi_info->pri_id, cfi_info->pri_addr, cfi_info->alt_id, cfi_info->alt_addr);
3180 buf_size -= printed;
3182 printed = snprintf(buf, buf_size, "Vcc min: %x.%x, Vcc max: %x.%x, "
3183 "Vpp min: %u.%x, Vpp max: %u.%x\n",
3184 (cfi_info->vcc_min & 0xf0) >> 4, cfi_info->vcc_min & 0x0f,
3185 (cfi_info->vcc_max & 0xf0) >> 4, cfi_info->vcc_max & 0x0f,
3186 (cfi_info->vpp_min & 0xf0) >> 4, cfi_info->vpp_min & 0x0f,
3187 (cfi_info->vpp_max & 0xf0) >> 4, cfi_info->vpp_max & 0x0f);
3189 buf_size -= printed;
3191 printed = snprintf(buf, buf_size, "typ. word write timeout: %u us, "
3192 "typ. buf write timeout: %u us, "
3193 "typ. block erase timeout: %u ms, "
3194 "typ. chip erase timeout: %u ms\n",
3195 1 << cfi_info->word_write_timeout_typ,
3196 1 << cfi_info->buf_write_timeout_typ,
3197 1 << cfi_info->block_erase_timeout_typ,
3198 1 << cfi_info->chip_erase_timeout_typ);
3200 buf_size -= printed;
3202 printed = snprintf(buf, buf_size, "max. word write timeout: %u us, "
3203 "max. buf write timeout: %u us, max. "
3204 "block erase timeout: %u ms, max. chip erase timeout: %u ms\n",
3205 (1 << cfi_info->word_write_timeout_max) * (1 << cfi_info->word_write_timeout_typ),
3206 (1 << cfi_info->buf_write_timeout_max) * (1 << cfi_info->buf_write_timeout_typ),
3207 (1 << cfi_info->block_erase_timeout_max) * (1 << cfi_info->block_erase_timeout_typ),
3208 (1 << cfi_info->chip_erase_timeout_max) * (1 << cfi_info->chip_erase_timeout_typ));
3210 buf_size -= printed;
3212 printed = snprintf(buf, buf_size, "size: 0x%" PRIx32 ", interface desc: %i, "
3213 "max buffer write size: 0x%x\n",
3215 cfi_info->interface_desc,
3216 1 << cfi_info->max_buf_write_size);
3218 buf_size -= printed;
3220 switch (cfi_info->pri_id)
3224 cfi_intel_info(bank, buf, buf_size);
3227 cfi_spansion_info(bank, buf, buf_size);
3230 LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
3237 static void cfi_fixup_0002_write_buffer(struct flash_bank *bank, void *param)
3239 struct cfi_flash_bank *cfi_info = bank->driver_priv;
3241 /* disable write buffer for M29W128G */
3242 cfi_info->buf_write_timeout_typ = 0;
3245 struct flash_driver cfi_flash = {
3247 .flash_bank_command = cfi_flash_bank_command,
3249 .protect = cfi_protect,
3253 .auto_probe = cfi_auto_probe,
3254 /* FIXME: access flash at bus_width size */
3255 .erase_check = default_flash_blank_check,
3256 .protect_check = cfi_protect_check,
3257 .info = get_cfi_info,