1 /***************************************************************************
2 * Copyright (C) 2005, 2007 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
24 #include "replacements.h"
32 #include "algorithm.h"
33 #include "binarybuffer.h"
40 int cfi_register_commands(struct command_context_s *cmd_ctx);
41 int cfi_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
42 int cfi_erase(struct flash_bank_s *bank, int first, int last);
43 int cfi_protect(struct flash_bank_s *bank, int set, int first, int last);
44 int cfi_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count);
45 int cfi_probe(struct flash_bank_s *bank);
46 int cfi_erase_check(struct flash_bank_s *bank);
47 int cfi_protect_check(struct flash_bank_s *bank);
48 int cfi_info(struct flash_bank_s *bank, char *buf, int buf_size);
50 int cfi_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
52 #define CFI_MAX_BUS_WIDTH 4
53 #define CFI_MAX_CHIP_WIDTH 4
55 flash_driver_t cfi_flash =
58 .register_commands = cfi_register_commands,
59 .flash_bank_command = cfi_flash_bank_command,
61 .protect = cfi_protect,
64 .erase_check = cfi_erase_check,
65 .protect_check = cfi_protect_check,
69 cfi_unlock_addresses_t cfi_unlock_addresses[] =
71 [CFI_UNLOCK_555_2AA] = { .unlock1 = 0x555, .unlock2 = 0x2aa },
72 [CFI_UNLOCK_5555_2AAA] = { .unlock1 = 0x5555, .unlock2 = 0x2aaa },
75 /* CFI fixups foward declarations */
76 void cfi_fixup_non_cfi(flash_bank_t *flash, void *param);
77 void cfi_fixup_0002_erase_regions(flash_bank_t *flash, void *param);
78 void cfi_fixup_0002_unlock_addresses(flash_bank_t *flash, void *param);
79 void cfi_fixup_atmel_reversed_erase_regions(flash_bank_t *flash, void *param);
81 /* fixup after identifying JEDEC manufactuer and ID */
82 cfi_fixup_t cfi_jedec_fixups[] = {
83 {CFI_MFR_SST, 0x00D4, cfi_fixup_non_cfi, NULL},
84 {CFI_MFR_SST, 0x00D5, cfi_fixup_non_cfi, NULL},
85 {CFI_MFR_SST, 0x00D6, cfi_fixup_non_cfi, NULL},
86 {CFI_MFR_SST, 0x00D7, cfi_fixup_non_cfi, NULL},
90 /* fixup after reading cmdset 0002 primary query table */
91 cfi_fixup_t cfi_0002_fixups[] = {
92 {CFI_MFR_SST, 0x00D4, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
93 {CFI_MFR_SST, 0x00D5, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
94 {CFI_MFR_SST, 0x00D6, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
95 {CFI_MFR_SST, 0x00D7, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
96 {CFI_MFR_ATMEL, 0x00C8, cfi_fixup_atmel_reversed_erase_regions, NULL},
97 {CFI_MFR_ANY, CFI_ID_ANY, cfi_fixup_0002_erase_regions, NULL},
101 /* fixup after reading cmdset 0001 primary query table */
102 cfi_fixup_t cfi_0001_fixups[] = {
106 void cfi_fixup(flash_bank_t *bank, cfi_fixup_t *fixups)
108 cfi_flash_bank_t *cfi_info = bank->driver_priv;
111 for (f = fixups; f->fixup; f++)
113 if (((f->mfr == CFI_MFR_ANY) || (f->mfr == cfi_info->manufacturer)) &&
114 ((f->id == CFI_ID_ANY) || (f->id == cfi_info->device_id)))
116 f->fixup(bank, f->param);
121 inline u32 flash_address(flash_bank_t *bank, int sector, u32 offset)
123 /* while the sector list isn't built, only accesses to sector 0 work */
125 return bank->base + offset * bank->bus_width;
130 ERROR("BUG: sector list not yet built");
133 return bank->base + bank->sectors[sector].offset + offset * bank->bus_width;
138 void cfi_command(flash_bank_t *bank, u8 cmd, u8 *cmd_buf)
140 cfi_flash_bank_t *cfi_info = bank->driver_priv;
143 /* clear whole buffer, to ensure bits that exceed the bus_width
146 for (i = 0; i < CFI_MAX_BUS_WIDTH; i++)
149 if (cfi_info->target->endianness == TARGET_LITTLE_ENDIAN)
151 for (i = bank->bus_width; i > 0; i--)
153 *cmd_buf++ = (i & (bank->chip_width - 1)) ? 0x0 : cmd;
158 for (i = 1; i <= bank->bus_width; i++)
160 *cmd_buf++ = (i & (bank->chip_width - 1)) ? 0x0 : cmd;
165 /* read unsigned 8-bit value from the bank
166 * flash banks are expected to be made of similar chips
167 * the query result should be the same for all
169 u8 cfi_query_u8(flash_bank_t *bank, int sector, u32 offset)
171 cfi_flash_bank_t *cfi_info = bank->driver_priv;
172 target_t *target = cfi_info->target;
173 u8 data[CFI_MAX_BUS_WIDTH];
175 target->type->read_memory(target, flash_address(bank, sector, offset), bank->bus_width, 1, data);
177 if (cfi_info->target->endianness == TARGET_LITTLE_ENDIAN)
180 return data[bank->bus_width - 1];
183 /* read unsigned 8-bit value from the bank
184 * in case of a bank made of multiple chips,
185 * the individual values are ORed
187 u8 cfi_get_u8(flash_bank_t *bank, int sector, u32 offset)
189 cfi_flash_bank_t *cfi_info = bank->driver_priv;
190 target_t *target = cfi_info->target;
191 u8 data[CFI_MAX_BUS_WIDTH];
194 target->type->read_memory(target, flash_address(bank, sector, offset), bank->bus_width, 1, data);
196 if (cfi_info->target->endianness == TARGET_LITTLE_ENDIAN)
198 for (i = 0; i < bank->bus_width / bank->chip_width; i++)
206 for (i = 0; i < bank->bus_width / bank->chip_width; i++)
207 value |= data[bank->bus_width - 1 - i];
213 u16 cfi_query_u16(flash_bank_t *bank, int sector, u32 offset)
215 cfi_flash_bank_t *cfi_info = bank->driver_priv;
216 target_t *target = cfi_info->target;
217 u8 data[CFI_MAX_BUS_WIDTH * 2];
219 target->type->read_memory(target, flash_address(bank, sector, offset), bank->bus_width, 2, data);
221 if (cfi_info->target->endianness == TARGET_LITTLE_ENDIAN)
222 return data[0] | data[bank->bus_width] << 8;
224 return data[bank->bus_width - 1] | data[(2 * bank->bus_width) - 1] << 8;
227 u32 cfi_query_u32(flash_bank_t *bank, int sector, u32 offset)
229 cfi_flash_bank_t *cfi_info = bank->driver_priv;
230 target_t *target = cfi_info->target;
231 u8 data[CFI_MAX_BUS_WIDTH * 4];
233 target->type->read_memory(target, flash_address(bank, sector, offset), bank->bus_width, 4, data);
235 if (cfi_info->target->endianness == TARGET_LITTLE_ENDIAN)
236 return data[0] | data[bank->bus_width] << 8 | data[bank->bus_width * 2] << 16 | data[bank->bus_width * 3] << 24;
238 return data[bank->bus_width - 1] | data[(2* bank->bus_width) - 1] << 8 |
239 data[(3 * bank->bus_width) - 1] << 16 | data[(4 * bank->bus_width) - 1] << 24;
242 void cfi_intel_clear_status_register(flash_bank_t *bank)
244 cfi_flash_bank_t *cfi_info = bank->driver_priv;
245 target_t *target = cfi_info->target;
248 if (target->state != TARGET_HALTED)
250 ERROR("BUG: attempted to clear status register while target wasn't halted");
254 cfi_command(bank, 0x50, command);
255 target->type->write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
258 u8 cfi_intel_wait_status_busy(flash_bank_t *bank, int timeout)
262 while ((!((status = cfi_get_u8(bank, 0, 0x0)) & 0x80)) && (timeout-- > 0))
264 DEBUG("status: 0x%x", status);
268 /* mask out bit 0 (reserved) */
269 status = status & 0xfe;
271 DEBUG("status: 0x%x", status);
273 if ((status & 0x80) != 0x80)
275 ERROR("timeout while waiting for WSM to become ready");
277 else if (status != 0x80)
279 ERROR("status register: 0x%x", status);
281 ERROR("Block Lock-Bit Detected, Operation Abort");
283 ERROR("Program suspended");
285 ERROR("Low Programming Voltage Detected, Operation Aborted");
287 ERROR("Program Error / Error in Setting Lock-Bit");
289 ERROR("Error in Block Erasure or Clear Lock-Bits");
291 ERROR("Block Erase Suspended");
293 cfi_intel_clear_status_register(bank);
299 int cfi_spansion_wait_status_busy(flash_bank_t *bank, int timeout)
301 u8 status, oldstatus;
303 oldstatus = cfi_get_u8(bank, 0, 0x0);
306 status = cfi_get_u8(bank, 0, 0x0);
307 if ((status ^ oldstatus) & 0x40) {
309 oldstatus = cfi_get_u8(bank, 0, 0x0);
310 status = cfi_get_u8(bank, 0, 0x0);
311 if ((status ^ oldstatus) & 0x40) {
312 ERROR("dq5 timeout, status: 0x%x", status);
313 return(ERROR_FLASH_OPERATION_FAILED);
315 DEBUG("status: 0x%x", status);
320 DEBUG("status: 0x%x", status);
326 } while (timeout-- > 0);
328 ERROR("timeout, status: 0x%x", status);
330 return(ERROR_FLASH_BUSY);
333 int cfi_read_intel_pri_ext(flash_bank_t *bank)
335 cfi_flash_bank_t *cfi_info = bank->driver_priv;
336 cfi_intel_pri_ext_t *pri_ext = malloc(sizeof(cfi_intel_pri_ext_t));
337 target_t *target = cfi_info->target;
340 cfi_info->pri_ext = pri_ext;
342 pri_ext->pri[0] = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0);
343 pri_ext->pri[1] = cfi_query_u8(bank, 0, cfi_info->pri_addr + 1);
344 pri_ext->pri[2] = cfi_query_u8(bank, 0, cfi_info->pri_addr + 2);
346 if ((pri_ext->pri[0] != 'P') || (pri_ext->pri[1] != 'R') || (pri_ext->pri[2] != 'I'))
348 cfi_command(bank, 0xf0, command);
349 target->type->write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
350 cfi_command(bank, 0xff, command);
351 target->type->write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
352 return ERROR_FLASH_BANK_INVALID;
355 pri_ext->major_version = cfi_query_u8(bank, 0, cfi_info->pri_addr + 3);
356 pri_ext->minor_version = cfi_query_u8(bank, 0, cfi_info->pri_addr + 4);
358 DEBUG("pri: '%c%c%c', version: %c.%c", pri_ext->pri[0], pri_ext->pri[1], pri_ext->pri[2], pri_ext->major_version, pri_ext->minor_version);
360 pri_ext->feature_support = cfi_query_u32(bank, 0, cfi_info->pri_addr + 5);
361 pri_ext->suspend_cmd_support = cfi_query_u8(bank, 0, cfi_info->pri_addr + 9);
362 pri_ext->blk_status_reg_mask = cfi_query_u16(bank, 0, cfi_info->pri_addr + 0xa);
364 DEBUG("feature_support: 0x%x, suspend_cmd_support: 0x%x, blk_status_reg_mask: 0x%x", pri_ext->feature_support, pri_ext->suspend_cmd_support, pri_ext->blk_status_reg_mask);
366 pri_ext->vcc_optimal = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0xc);
367 pri_ext->vpp_optimal = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0xd);
369 DEBUG("Vcc opt: %1.1x.%1.1x, Vpp opt: %1.1x.%1.1x",
370 (pri_ext->vcc_optimal & 0xf0) >> 4, pri_ext->vcc_optimal & 0x0f,
371 (pri_ext->vpp_optimal & 0xf0) >> 4, pri_ext->vpp_optimal & 0x0f);
373 pri_ext->num_protection_fields = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0xe);
374 if (pri_ext->num_protection_fields != 1)
376 WARNING("expected one protection register field, but found %i", pri_ext->num_protection_fields);
379 pri_ext->prot_reg_addr = cfi_query_u16(bank, 0, cfi_info->pri_addr + 0xf);
380 pri_ext->fact_prot_reg_size = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0x11);
381 pri_ext->user_prot_reg_size = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0x12);
383 DEBUG("protection_fields: %i, prot_reg_addr: 0x%x, factory pre-programmed: %i, user programmable: %i", pri_ext->num_protection_fields, pri_ext->prot_reg_addr, 1 << pri_ext->fact_prot_reg_size, 1 << pri_ext->user_prot_reg_size);
388 int cfi_read_spansion_pri_ext(flash_bank_t *bank)
390 cfi_flash_bank_t *cfi_info = bank->driver_priv;
391 cfi_spansion_pri_ext_t *pri_ext = malloc(sizeof(cfi_spansion_pri_ext_t));
392 target_t *target = cfi_info->target;
395 cfi_info->pri_ext = pri_ext;
397 pri_ext->pri[0] = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0);
398 pri_ext->pri[1] = cfi_query_u8(bank, 0, cfi_info->pri_addr + 1);
399 pri_ext->pri[2] = cfi_query_u8(bank, 0, cfi_info->pri_addr + 2);
401 if ((pri_ext->pri[0] != 'P') || (pri_ext->pri[1] != 'R') || (pri_ext->pri[2] != 'I'))
403 cfi_command(bank, 0xf0, command);
404 target->type->write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
405 return ERROR_FLASH_BANK_INVALID;
408 pri_ext->major_version = cfi_query_u8(bank, 0, cfi_info->pri_addr + 3);
409 pri_ext->minor_version = cfi_query_u8(bank, 0, cfi_info->pri_addr + 4);
411 DEBUG("pri: '%c%c%c', version: %c.%c", pri_ext->pri[0], pri_ext->pri[1], pri_ext->pri[2], pri_ext->major_version, pri_ext->minor_version);
413 pri_ext->SiliconRevision = cfi_query_u8(bank, 0, cfi_info->pri_addr + 5);
414 pri_ext->EraseSuspend = cfi_query_u8(bank, 0, cfi_info->pri_addr + 6);
415 pri_ext->BlkProt = cfi_query_u8(bank, 0, cfi_info->pri_addr + 7);
416 pri_ext->TmpBlkUnprotect = cfi_query_u8(bank, 0, cfi_info->pri_addr + 8);
417 pri_ext->BlkProtUnprot = cfi_query_u8(bank, 0, cfi_info->pri_addr + 9);
418 pri_ext->SimultaneousOps = cfi_query_u8(bank, 0, cfi_info->pri_addr + 10);
419 pri_ext->BurstMode = cfi_query_u8(bank, 0, cfi_info->pri_addr + 11);
420 pri_ext->PageMode = cfi_query_u8(bank, 0, cfi_info->pri_addr + 12);
421 pri_ext->VppMin = cfi_query_u8(bank, 0, cfi_info->pri_addr + 13);
422 pri_ext->VppMax = cfi_query_u8(bank, 0, cfi_info->pri_addr + 14);
423 pri_ext->TopBottom = cfi_query_u8(bank, 0, cfi_info->pri_addr + 15);
425 DEBUG("Silicon Revision: 0x%x, Erase Suspend: 0x%x, Block protect: 0x%x", pri_ext->SiliconRevision,
426 pri_ext->EraseSuspend, pri_ext->BlkProt);
428 DEBUG("Temporary Unprotect: 0x%x, Block Protect Scheme: 0x%x, Simultaneous Ops: 0x%x", pri_ext->TmpBlkUnprotect,
429 pri_ext->BlkProtUnprot, pri_ext->SimultaneousOps);
431 DEBUG("Burst Mode: 0x%x, Page Mode: 0x%x, ", pri_ext->BurstMode, pri_ext->PageMode);
434 DEBUG("Vpp min: %2.2d.%1.1d, Vpp max: %2.2d.%1.1x",
435 (pri_ext->VppMin & 0xf0) >> 4, pri_ext->VppMin & 0x0f,
436 (pri_ext->VppMax & 0xf0) >> 4, pri_ext->VppMax & 0x0f);
438 DEBUG("WP# protection 0x%x", pri_ext->TopBottom);
440 /* default values for implementation specific workarounds */
441 pri_ext->_unlock1 = cfi_unlock_addresses[CFI_UNLOCK_555_2AA].unlock1;
442 pri_ext->_unlock2 = cfi_unlock_addresses[CFI_UNLOCK_555_2AA].unlock2;
443 pri_ext->_reversed_geometry = 0;
448 int cfi_read_atmel_pri_ext(flash_bank_t *bank)
450 cfi_atmel_pri_ext_t atmel_pri_ext;
451 cfi_flash_bank_t *cfi_info = bank->driver_priv;
452 cfi_spansion_pri_ext_t *pri_ext = malloc(sizeof(cfi_spansion_pri_ext_t));
453 target_t *target = cfi_info->target;
456 /* ATMEL devices use the same CFI primary command set (0x2) as AMD/Spansion,
457 * but a different primary extended query table.
458 * We read the atmel table, and prepare a valid AMD/Spansion query table.
461 memset(pri_ext, 0, sizeof(cfi_spansion_pri_ext_t));
463 cfi_info->pri_ext = pri_ext;
465 atmel_pri_ext.pri[0] = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0);
466 atmel_pri_ext.pri[1] = cfi_query_u8(bank, 0, cfi_info->pri_addr + 1);
467 atmel_pri_ext.pri[2] = cfi_query_u8(bank, 0, cfi_info->pri_addr + 2);
469 if ((atmel_pri_ext.pri[0] != 'P') || (atmel_pri_ext.pri[1] != 'R') || (atmel_pri_ext.pri[2] != 'I'))
471 cfi_command(bank, 0xf0, command);
472 target->type->write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
473 return ERROR_FLASH_BANK_INVALID;
476 pri_ext->pri[0] = atmel_pri_ext.pri[0];
477 pri_ext->pri[1] = atmel_pri_ext.pri[1];
478 pri_ext->pri[2] = atmel_pri_ext.pri[2];
480 atmel_pri_ext.major_version = cfi_query_u8(bank, 0, cfi_info->pri_addr + 3);
481 atmel_pri_ext.minor_version = cfi_query_u8(bank, 0, cfi_info->pri_addr + 4);
483 DEBUG("pri: '%c%c%c', version: %c.%c", atmel_pri_ext.pri[0], atmel_pri_ext.pri[1], atmel_pri_ext.pri[2], atmel_pri_ext.major_version, atmel_pri_ext.minor_version);
485 pri_ext->major_version = atmel_pri_ext.major_version;
486 pri_ext->minor_version = atmel_pri_ext.minor_version;
488 atmel_pri_ext.features = cfi_query_u8(bank, 0, cfi_info->pri_addr + 5);
489 atmel_pri_ext.bottom_boot = cfi_query_u8(bank, 0, cfi_info->pri_addr + 6);
490 atmel_pri_ext.burst_mode = cfi_query_u8(bank, 0, cfi_info->pri_addr + 7);
491 atmel_pri_ext.page_mode = cfi_query_u8(bank, 0, cfi_info->pri_addr + 8);
493 DEBUG("features: 0x%2.2x, bottom_boot: 0x%2.2x, burst_mode: 0x%2.2x, page_mode: 0x%2.2x",
494 atmel_pri_ext.features, atmel_pri_ext.bottom_boot, atmel_pri_ext.burst_mode, atmel_pri_ext.page_mode);
496 if (atmel_pri_ext.features & 0x02)
497 pri_ext->EraseSuspend = 2;
499 if (atmel_pri_ext.bottom_boot)
500 pri_ext->TopBottom = 2;
502 pri_ext->TopBottom = 3;
507 int cfi_read_0002_pri_ext(flash_bank_t *bank)
509 cfi_flash_bank_t *cfi_info = bank->driver_priv;
511 if (cfi_info->manufacturer == CFI_MFR_ATMEL)
513 return cfi_read_atmel_pri_ext(bank);
517 return cfi_read_spansion_pri_ext(bank);
521 int cfi_spansion_info(struct flash_bank_s *bank, char *buf, int buf_size)
524 cfi_flash_bank_t *cfi_info = bank->driver_priv;
525 cfi_spansion_pri_ext_t *pri_ext = cfi_info->pri_ext;
527 printed = snprintf(buf, buf_size, "\nSpansion primary algorithm extend information:\n");
531 printed = snprintf(buf, buf_size, "pri: '%c%c%c', version: %c.%c\n", pri_ext->pri[0],
532 pri_ext->pri[1], pri_ext->pri[2],
533 pri_ext->major_version, pri_ext->minor_version);
537 printed = snprintf(buf, buf_size, "Silicon Rev.: 0x%x, Address Sensitive unlock: 0x%x\n",
538 (pri_ext->SiliconRevision) >> 2,
539 (pri_ext->SiliconRevision) & 0x03);
543 printed = snprintf(buf, buf_size, "Erase Suspend: 0x%x, Sector Protect: 0x%x\n",
544 pri_ext->EraseSuspend,
549 printed = snprintf(buf, buf_size, "VppMin: %2.2d.%1.1x, VppMax: %2.2d.%1.1x\n",
550 (pri_ext->VppMin & 0xf0) >> 4, pri_ext->VppMin & 0x0f,
551 (pri_ext->VppMax & 0xf0) >> 4, pri_ext->VppMax & 0x0f);
556 int cfi_intel_info(struct flash_bank_s *bank, char *buf, int buf_size)
559 cfi_flash_bank_t *cfi_info = bank->driver_priv;
560 cfi_intel_pri_ext_t *pri_ext = cfi_info->pri_ext;
562 printed = snprintf(buf, buf_size, "\nintel primary algorithm extend information:\n");
566 printed = snprintf(buf, buf_size, "pri: '%c%c%c', version: %c.%c\n", pri_ext->pri[0], pri_ext->pri[1], pri_ext->pri[2], pri_ext->major_version, pri_ext->minor_version);
570 printed = snprintf(buf, buf_size, "feature_support: 0x%x, suspend_cmd_support: 0x%x, blk_status_reg_mask: 0x%x\n", pri_ext->feature_support, pri_ext->suspend_cmd_support, pri_ext->blk_status_reg_mask);
574 printed = snprintf(buf, buf_size, "Vcc opt: %1.1x.%1.1x, Vpp opt: %1.1x.%1.1x\n",
575 (pri_ext->vcc_optimal & 0xf0) >> 4, pri_ext->vcc_optimal & 0x0f,
576 (pri_ext->vpp_optimal & 0xf0) >> 4, pri_ext->vpp_optimal & 0x0f);
580 printed = snprintf(buf, buf_size, "protection_fields: %i, prot_reg_addr: 0x%x, factory pre-programmed: %i, user programmable: %i\n", pri_ext->num_protection_fields, pri_ext->prot_reg_addr, 1 << pri_ext->fact_prot_reg_size, 1 << pri_ext->user_prot_reg_size);
585 int cfi_register_commands(struct command_context_s *cmd_ctx)
587 /*command_t *cfi_cmd = */register_command(cmd_ctx, NULL, "cfi", NULL, COMMAND_ANY, NULL);
589 register_command(cmd_ctx, cfi_cmd, "part_id", cfi_handle_part_id_command, COMMAND_EXEC,
590 "print part id of cfi flash bank <num>");
595 /* flash_bank cfi <base> <size> <chip_width> <bus_width> <target#> [options]
597 int cfi_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
599 cfi_flash_bank_t *cfi_info;
604 WARNING("incomplete flash_bank cfi configuration");
605 return ERROR_FLASH_BANK_INVALID;
608 if ((strtoul(args[4], NULL, 0) > CFI_MAX_CHIP_WIDTH)
609 || (strtoul(args[3], NULL, 0) > CFI_MAX_BUS_WIDTH))
611 ERROR("chip and bus width have to specified in byte");
612 return ERROR_FLASH_BANK_INVALID;
615 cfi_info = malloc(sizeof(cfi_flash_bank_t));
616 bank->driver_priv = cfi_info;
618 cfi_info->x16_as_x8 = 0;
619 cfi_info->jedec_probe = 0;
620 cfi_info->not_cfi = 0;
622 cfi_info->target = get_target_by_num(strtoul(args[5], NULL, 0));
623 if (!cfi_info->target)
625 ERROR("no target '%s' configured", args[5]);
629 for (i = 6; i < argc; i++)
631 if (strcmp(args[i], "x16_as_x8") == 0)
633 cfi_info->x16_as_x8 = 1;
635 else if (strcmp(args[i], "jedec_probe") == 0)
637 cfi_info->jedec_probe = 1;
641 cfi_info->write_algorithm = NULL;
643 /* bank wasn't probed yet */
644 cfi_info->qry[0] = -1;
649 int cfi_intel_erase(struct flash_bank_s *bank, int first, int last)
651 cfi_flash_bank_t *cfi_info = bank->driver_priv;
652 target_t *target = cfi_info->target;
656 cfi_intel_clear_status_register(bank);
658 for (i = first; i <= last; i++)
660 cfi_command(bank, 0x20, command);
661 target->type->write_memory(target, flash_address(bank, i, 0x0), bank->bus_width, 1, command);
663 cfi_command(bank, 0xd0, command);
664 target->type->write_memory(target, flash_address(bank, i, 0x0), bank->bus_width, 1, command);
666 if (cfi_intel_wait_status_busy(bank, 1000 * (1 << cfi_info->block_erase_timeout_typ)) == 0x80)
667 bank->sectors[i].is_erased = 1;
670 cfi_command(bank, 0xff, command);
671 target->type->write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
673 ERROR("couldn't erase block %i of flash bank at base 0x%x", i, bank->base);
674 return ERROR_FLASH_OPERATION_FAILED;
678 cfi_command(bank, 0xff, command);
679 target->type->write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
684 int cfi_spansion_erase(struct flash_bank_s *bank, int first, int last)
686 cfi_flash_bank_t *cfi_info = bank->driver_priv;
687 cfi_spansion_pri_ext_t *pri_ext = cfi_info->pri_ext;
688 target_t *target = cfi_info->target;
692 for (i = first; i <= last; i++)
694 cfi_command(bank, 0xaa, command);
695 target->type->write_memory(target, flash_address(bank, 0, pri_ext->_unlock1), bank->bus_width, 1, command);
697 cfi_command(bank, 0x55, command);
698 target->type->write_memory(target, flash_address(bank, 0, pri_ext->_unlock2), bank->bus_width, 1, command);
700 cfi_command(bank, 0x80, command);
701 target->type->write_memory(target, flash_address(bank, 0, pri_ext->_unlock1), bank->bus_width, 1, command);
703 cfi_command(bank, 0xaa, command);
704 target->type->write_memory(target, flash_address(bank, 0, pri_ext->_unlock1), bank->bus_width, 1, command);
706 cfi_command(bank, 0x55, command);
707 target->type->write_memory(target, flash_address(bank, 0, pri_ext->_unlock2), bank->bus_width, 1, command);
709 cfi_command(bank, 0x30, command);
710 target->type->write_memory(target, flash_address(bank, i, 0x0), bank->bus_width, 1, command);
712 if (cfi_spansion_wait_status_busy(bank, 1000 * (1 << cfi_info->block_erase_timeout_typ)) == ERROR_OK)
713 bank->sectors[i].is_erased = 1;
716 cfi_command(bank, 0xf0, command);
717 target->type->write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
719 ERROR("couldn't erase block %i of flash bank at base 0x%x", i, bank->base);
720 return ERROR_FLASH_OPERATION_FAILED;
724 cfi_command(bank, 0xf0, command);
725 target->type->write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
730 int cfi_erase(struct flash_bank_s *bank, int first, int last)
732 cfi_flash_bank_t *cfi_info = bank->driver_priv;
734 if (cfi_info->target->state != TARGET_HALTED)
736 return ERROR_TARGET_NOT_HALTED;
739 if ((first < 0) || (last < first) || (last >= bank->num_sectors))
741 return ERROR_FLASH_SECTOR_INVALID;
744 if (cfi_info->qry[0] != 'Q')
745 return ERROR_FLASH_BANK_NOT_PROBED;
747 switch(cfi_info->pri_id)
751 return cfi_intel_erase(bank, first, last);
754 return cfi_spansion_erase(bank, first, last);
757 ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
764 int cfi_intel_protect(struct flash_bank_s *bank, int set, int first, int last)
766 cfi_flash_bank_t *cfi_info = bank->driver_priv;
767 cfi_intel_pri_ext_t *pri_ext = cfi_info->pri_ext;
768 target_t *target = cfi_info->target;
773 /* if the device supports neither legacy lock/unlock (bit 3) nor
774 * instant individual block locking (bit 5).
776 if (!(pri_ext->feature_support & 0x28))
777 return ERROR_FLASH_OPERATION_FAILED;
779 cfi_intel_clear_status_register(bank);
781 for (i = first; i <= last; i++)
783 cfi_command(bank, 0x60, command);
784 DEBUG("address: 0x%4.4x, command: 0x%4.4x", flash_address(bank, i, 0x0), target_buffer_get_u32(target, command));
785 target->type->write_memory(target, flash_address(bank, i, 0x0), bank->bus_width, 1, command);
788 cfi_command(bank, 0x01, command);
789 DEBUG("address: 0x%4.4x, command: 0x%4.4x", flash_address(bank, i, 0x0), target_buffer_get_u32(target, command));
790 target->type->write_memory(target, flash_address(bank, i, 0x0), bank->bus_width, 1, command);
791 bank->sectors[i].is_protected = 1;
795 cfi_command(bank, 0xd0, command);
796 DEBUG("address: 0x%4.4x, command: 0x%4.4x", flash_address(bank, i, 0x0), target_buffer_get_u32(target, command));
797 target->type->write_memory(target, flash_address(bank, i, 0x0), bank->bus_width, 1, command);
798 bank->sectors[i].is_protected = 0;
801 /* instant individual block locking doesn't require reading of the status register */
802 if (!(pri_ext->feature_support & 0x20))
804 /* Clear lock bits operation may take up to 1.4s */
805 cfi_intel_wait_status_busy(bank, 1400);
810 /* read block lock bit, to verify status */
811 cfi_command(bank, 0x90, command);
812 target->type->write_memory(target, flash_address(bank, 0, 0x55), bank->bus_width, 1, command);
813 block_status = cfi_get_u8(bank, i, 0x2);
815 if ((block_status & 0x1) != set)
817 ERROR("couldn't change block lock status (set = %i, block_status = 0x%2.2x)", set, block_status);
818 cfi_command(bank, 0x70, command);
819 target->type->write_memory(target, flash_address(bank, 0, 0x55), bank->bus_width, 1, command);
820 cfi_intel_wait_status_busy(bank, 10);
823 return ERROR_FLASH_OPERATION_FAILED;
833 /* if the device doesn't support individual block lock bits set/clear,
834 * all blocks have been unlocked in parallel, so we set those that should be protected
836 if ((!set) && (!(pri_ext->feature_support & 0x20)))
838 for (i = 0; i < bank->num_sectors; i++)
840 cfi_intel_clear_status_register(bank);
841 cfi_command(bank, 0x60, command);
842 target->type->write_memory(target, flash_address(bank, i, 0x0), bank->bus_width, 1, command);
843 if (bank->sectors[i].is_protected == 1)
845 cfi_command(bank, 0x01, command);
846 target->type->write_memory(target, flash_address(bank, i, 0x0), bank->bus_width, 1, command);
849 cfi_intel_wait_status_busy(bank, 100);
853 cfi_command(bank, 0xff, command);
854 target->type->write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
859 int cfi_protect(struct flash_bank_s *bank, int set, int first, int last)
861 cfi_flash_bank_t *cfi_info = bank->driver_priv;
863 if (cfi_info->target->state != TARGET_HALTED)
865 return ERROR_TARGET_NOT_HALTED;
868 if ((first < 0) || (last < first) || (last >= bank->num_sectors))
870 return ERROR_FLASH_SECTOR_INVALID;
873 if (cfi_info->qry[0] != 'Q')
874 return ERROR_FLASH_BANK_NOT_PROBED;
876 switch(cfi_info->pri_id)
880 cfi_intel_protect(bank, set, first, last);
883 ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
890 void cfi_add_byte(struct flash_bank_s *bank, u8 *word, u8 byte)
892 cfi_flash_bank_t *cfi_info = bank->driver_priv;
893 target_t *target = cfi_info->target;
897 if (target->endianness == TARGET_LITTLE_ENDIAN)
900 for (i = 0; i < bank->bus_width - 1; i++)
901 word[i] = word[i + 1];
902 word[bank->bus_width - 1] = byte;
907 for (i = bank->bus_width - 1; i > 0; i--)
908 word[i] = word[i - 1];
913 int cfi_intel_write_block(struct flash_bank_s *bank, u8 *buffer, u32 address, u32 count)
915 cfi_flash_bank_t *cfi_info = bank->driver_priv;
916 target_t *target = cfi_info->target;
917 reg_param_t reg_params[7];
918 armv4_5_algorithm_t armv4_5_info;
919 working_area_t *source;
920 u32 buffer_size = 32768;
921 u8 write_command_buf[CFI_MAX_BUS_WIDTH];
922 u8 busy_pattern_buf[CFI_MAX_BUS_WIDTH];
923 u8 error_pattern_buf[CFI_MAX_BUS_WIDTH];
924 u32 write_command_val, busy_pattern_val, error_pattern_val;
927 /* algorithm register usage:
928 * r0: source address (in RAM)
929 * r1: target address (in Flash)
931 * r3: flash write command
932 * r4: status byte (returned to host)
933 * r5: busy test pattern
934 * r6: error test pattern
937 static const u32 word_32_code[] = {
938 0xe4904004, /* loop: ldr r4, [r0], #4 */
939 0xe5813000, /* str r3, [r1] */
940 0xe5814000, /* str r4, [r1] */
941 0xe5914000, /* busy: ldr r4, [r1] */
942 0xe0047005, /* and r7, r4, r5 */
943 0xe1570005, /* cmp r7, r5 */
944 0x1afffffb, /* bne busy */
945 0xe1140006, /* tst r4, r6 */
946 0x1a000003, /* bne done */
947 0xe2522001, /* subs r2, r2, #1 */
948 0x0a000001, /* beq done */
949 0xe2811004, /* add r1, r1 #4 */
950 0xeafffff2, /* b loop */
951 0xeafffffe, /* done: b -2 */
954 static const u32 word_16_code[] = {
955 0xe0d040b2, /* loop: ldrh r4, [r0], #2 */
956 0xe1c130b0, /* strh r3, [r1] */
957 0xe1c140b0, /* strh r4, [r1] */
958 0xe1d140b0, /* busy ldrh r4, [r1] */
959 0xe0047005, /* and r7, r4, r5 */
960 0xe1570005, /* cmp r7, r5 */
961 0x1afffffb, /* bne busy */
962 0xe1140006, /* tst r4, r6 */
963 0x1a000003, /* bne done */
964 0xe2522001, /* subs r2, r2, #1 */
965 0x0a000001, /* beq done */
966 0xe2811002, /* add r1, r1 #2 */
967 0xeafffff2, /* b loop */
968 0xeafffffe, /* done: b -2 */
971 static const u32 word_8_code[] = {
972 0xe4d04001, /* loop: ldrb r4, [r0], #1 */
973 0xe5c13000, /* strb r3, [r1] */
974 0xe5c14000, /* strb r4, [r1] */
975 0xe5d14000, /* busy ldrb r4, [r1] */
976 0xe0047005, /* and r7, r4, r5 */
977 0xe1570005, /* cmp r7, r5 */
978 0x1afffffb, /* bne busy */
979 0xe1140006, /* tst r4, r6 */
980 0x1a000003, /* bne done */
981 0xe2522001, /* subs r2, r2, #1 */
982 0x0a000001, /* beq done */
983 0xe2811001, /* add r1, r1 #1 */
984 0xeafffff2, /* b loop */
985 0xeafffffe, /* done: b -2 */
988 cfi_intel_clear_status_register(bank);
990 armv4_5_info.common_magic = ARMV4_5_COMMON_MAGIC;
991 armv4_5_info.core_mode = ARMV4_5_MODE_SVC;
992 armv4_5_info.core_state = ARMV4_5_STATE_ARM;
994 /* flash write code */
995 if (!cfi_info->write_algorithm)
997 u8 write_code_buf[14 * 4];
1000 if (target_alloc_working_area(target, 4 * 14, &cfi_info->write_algorithm) != ERROR_OK)
1002 WARNING("no working area available, can't do block memory writes");
1003 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1006 /* write algorithm code to working area */
1007 if (bank->bus_width == 1)
1009 for (i = 0; i < 14; i++)
1010 target_buffer_set_u32(target, write_code_buf + (i*4), word_8_code[i]);
1012 else if (bank->bus_width == 2)
1014 for (i = 0; i < 14; i++)
1015 target_buffer_set_u32(target, write_code_buf + (i*4), word_16_code[i]);
1017 else if (bank->bus_width == 4)
1019 for (i = 0; i < 14; i++)
1020 target_buffer_set_u32(target, write_code_buf + (i*4), word_32_code[i]);
1024 return ERROR_FLASH_OPERATION_FAILED;
1027 target_write_buffer(target, cfi_info->write_algorithm->address, 14 * 4, write_code_buf);
1030 while (target_alloc_working_area(target, buffer_size, &source) != ERROR_OK)
1033 if (buffer_size <= 256)
1035 /* if we already allocated the writing code, but failed to get a buffer, free the algorithm */
1036 if (cfi_info->write_algorithm)
1037 target_free_working_area(target, cfi_info->write_algorithm);
1039 WARNING("no large enough working area available, can't do block memory writes");
1040 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1044 init_reg_param(®_params[0], "r0", 32, PARAM_OUT);
1045 init_reg_param(®_params[1], "r1", 32, PARAM_OUT);
1046 init_reg_param(®_params[2], "r2", 32, PARAM_OUT);
1047 init_reg_param(®_params[3], "r3", 32, PARAM_OUT);
1048 init_reg_param(®_params[4], "r4", 32, PARAM_IN);
1049 init_reg_param(®_params[5], "r5", 32, PARAM_OUT);
1050 init_reg_param(®_params[6], "r6", 32, PARAM_OUT);
1052 /* prepare command and status register patterns */
1053 cfi_command(bank, 0x40, write_command_buf);
1054 cfi_command(bank, 0x80, busy_pattern_buf);
1055 cfi_command(bank, 0x7e, error_pattern_buf);
1057 if (bank->bus_width == 1)
1059 write_command_val = write_command_buf[0];
1060 busy_pattern_val = busy_pattern_buf[0];
1061 error_pattern_val = error_pattern_buf[0];
1063 else if (bank->bus_width == 2)
1065 write_command_val = target_buffer_get_u16(target, write_command_buf);
1066 busy_pattern_val = target_buffer_get_u16(target, busy_pattern_buf);
1067 error_pattern_val = target_buffer_get_u16(target, error_pattern_buf);
1069 else if (bank->bus_width == 4)
1071 write_command_val = target_buffer_get_u32(target, write_command_buf);
1072 busy_pattern_val = target_buffer_get_u32(target, busy_pattern_buf);
1073 error_pattern_val = target_buffer_get_u32(target, error_pattern_buf);
1078 u32 thisrun_count = (count > buffer_size) ? buffer_size : count;
1080 target_write_buffer(target, source->address, thisrun_count, buffer);
1082 buf_set_u32(reg_params[0].value, 0, 32, source->address);
1083 buf_set_u32(reg_params[1].value, 0, 32, address);
1084 buf_set_u32(reg_params[2].value, 0, 32, thisrun_count / bank->bus_width);
1085 buf_set_u32(reg_params[3].value, 0, 32, write_command_val);
1086 buf_set_u32(reg_params[5].value, 0, 32, busy_pattern_val);
1087 buf_set_u32(reg_params[6].value, 0, 32, error_pattern_val);
1089 if ((retval = target->type->run_algorithm(target, 0, NULL, 7, reg_params, cfi_info->write_algorithm->address, cfi_info->write_algorithm->address + (13 * 4), 10000, &armv4_5_info)) != ERROR_OK)
1091 cfi_intel_clear_status_register(bank);
1092 return ERROR_FLASH_OPERATION_FAILED;
1095 if (buf_get_u32(reg_params[4].value, 0, 32) & error_pattern_val)
1097 /* read status register (outputs debug inforation) */
1098 cfi_intel_wait_status_busy(bank, 100);
1099 cfi_intel_clear_status_register(bank);
1100 return ERROR_FLASH_OPERATION_FAILED;
1103 buffer += thisrun_count;
1104 address += thisrun_count;
1105 count -= thisrun_count;
1108 target_free_working_area(target, source);
1110 destroy_reg_param(®_params[0]);
1111 destroy_reg_param(®_params[1]);
1112 destroy_reg_param(®_params[2]);
1113 destroy_reg_param(®_params[3]);
1114 destroy_reg_param(®_params[4]);
1115 destroy_reg_param(®_params[5]);
1116 destroy_reg_param(®_params[6]);
1121 int cfi_spansion_write_block(struct flash_bank_s *bank, u8 *buffer, u32 address, u32 count)
1123 cfi_flash_bank_t *cfi_info = bank->driver_priv;
1124 cfi_spansion_pri_ext_t *pri_ext = cfi_info->pri_ext;
1125 target_t *target = cfi_info->target;
1126 reg_param_t reg_params[10];
1127 armv4_5_algorithm_t armv4_5_info;
1128 working_area_t *source;
1129 u32 buffer_size = 32768;
1130 u8 write_command[CFI_MAX_BUS_WIDTH];
1134 int exit_code = ERROR_OK;
1136 /* input parameters - */
1137 /* R0 = source address */
1138 /* R1 = destination address */
1139 /* R2 = number of writes */
1140 /* R3 = flash write command */
1141 /* R4 = constant to mask DQ7 bits (also used for Dq5 with shift) */
1142 /* output parameters - */
1143 /* R5 = 0x80 ok 0x00 bad */
1144 /* temp registers - */
1145 /* R6 = value read from flash to test status */
1146 /* R7 = holding register */
1147 /* unlock registers - */
1148 /* R8 = unlock1_addr */
1149 /* R9 = unlock1_cmd */
1150 /* R10 = unlock2_addr */
1151 /* R11 = unlock2_cmd */
1153 u32 word_32_code[] = {
1154 /* 00008100 <sp_32_code>: */
1155 0xe4905004, /* ldr r5, [r0], #4 */
1156 0xe5889000, /* str r9, [r8] */
1157 0xe58ab000, /* str r11, [r10] */
1158 0xe5883000, /* str r3, [r8] */
1159 0xe5815000, /* str r5, [r1] */
1160 0xe1a00000, /* nop */
1162 /* 00008110 <sp_32_busy>: */
1163 0xe5916000, /* ldr r6, [r1] */
1164 0xe0257006, /* eor r7, r5, r6 */
1165 0xe0147007, /* ands r7, r4, r7 */
1166 0x0a000007, /* beq 8140 <sp_32_cont> ; b if DQ7 == Data7 */
1167 0xe0166124, /* ands r6, r6, r4, lsr #2 */
1168 0x0afffff9, /* beq 8110 <sp_32_busy> ; b if DQ5 low */
1169 0xe5916000, /* ldr r6, [r1] */
1170 0xe0257006, /* eor r7, r5, r6 */
1171 0xe0147007, /* ands r7, r4, r7 */
1172 0x0a000001, /* beq 8140 <sp_32_cont> ; b if DQ7 == Data7 */
1173 0xe3a05000, /* mov r5, #0 ; 0x0 - return 0x00, error */
1174 0x1a000004, /* bne 8154 <sp_32_done> */
1176 /* 00008140 <sp_32_cont>: */
1177 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
1178 0x03a05080, /* moveq r5, #128 ; 0x80 */
1179 0x0a000001, /* beq 8154 <sp_32_done> */
1180 0xe2811004, /* add r1, r1, #4 ; 0x4 */
1181 0xeaffffe8, /* b 8100 <sp_32_code> */
1183 /* 00008154 <sp_32_done>: */
1184 0xeafffffe /* b 8154 <sp_32_done> */
1187 u32 word_16_code[] = {
1188 /* 00008158 <sp_16_code>: */
1189 0xe0d050b2, /* ldrh r5, [r0], #2 */
1190 0xe1c890b0, /* strh r9, [r8] */
1191 0xe1cab0b0, /* strh r11, [r10] */
1192 0xe1c830b0, /* strh r3, [r8] */
1193 0xe1c150b0, /* strh r5, [r1] */
1194 0xe1a00000, /* nop (mov r0,r0) */
1196 /* 00008168 <sp_16_busy>: */
1197 0xe1d160b0, /* ldrh r6, [r1] */
1198 0xe0257006, /* eor r7, r5, r6 */
1199 0xe0147007, /* ands r7, r4, r7 */
1200 0x0a000007, /* beq 8198 <sp_16_cont> */
1201 0xe0166124, /* ands r6, r6, r4, lsr #2 */
1202 0x0afffff9, /* beq 8168 <sp_16_busy> */
1203 0xe1d160b0, /* ldrh r6, [r1] */
1204 0xe0257006, /* eor r7, r5, r6 */
1205 0xe0147007, /* ands r7, r4, r7 */
1206 0x0a000001, /* beq 8198 <sp_16_cont> */
1207 0xe3a05000, /* mov r5, #0 ; 0x0 */
1208 0x1a000004, /* bne 81ac <sp_16_done> */
1210 /* 00008198 <sp_16_cont>: */
1211 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
1212 0x03a05080, /* moveq r5, #128 ; 0x80 */
1213 0x0a000001, /* beq 81ac <sp_16_done> */
1214 0xe2811002, /* add r1, r1, #2 ; 0x2 */
1215 0xeaffffe8, /* b 8158 <sp_16_code> */
1217 /* 000081ac <sp_16_done>: */
1218 0xeafffffe /* b 81ac <sp_16_done> */
1221 u32 word_8_code[] = {
1222 /* 000081b0 <sp_16_code_end>: */
1223 0xe4d05001, /* ldrb r5, [r0], #1 */
1224 0xe5c89000, /* strb r9, [r8] */
1225 0xe5cab000, /* strb r11, [r10] */
1226 0xe5c83000, /* strb r3, [r8] */
1227 0xe5c15000, /* strb r5, [r1] */
1228 0xe1a00000, /* nop (mov r0,r0) */
1230 /* 000081c0 <sp_8_busy>: */
1231 0xe5d16000, /* ldrb r6, [r1] */
1232 0xe0257006, /* eor r7, r5, r6 */
1233 0xe0147007, /* ands r7, r4, r7 */
1234 0x0a000007, /* beq 81f0 <sp_8_cont> */
1235 0xe0166124, /* ands r6, r6, r4, lsr #2 */
1236 0x0afffff9, /* beq 81c0 <sp_8_busy> */
1237 0xe5d16000, /* ldrb r6, [r1] */
1238 0xe0257006, /* eor r7, r5, r6 */
1239 0xe0147007, /* ands r7, r4, r7 */
1240 0x0a000001, /* beq 81f0 <sp_8_cont> */
1241 0xe3a05000, /* mov r5, #0 ; 0x0 */
1242 0x1a000004, /* bne 8204 <sp_8_done> */
1244 /* 000081f0 <sp_8_cont>: */
1245 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
1246 0x03a05080, /* moveq r5, #128 ; 0x80 */
1247 0x0a000001, /* beq 8204 <sp_8_done> */
1248 0xe2811001, /* add r1, r1, #1 ; 0x1 */
1249 0xeaffffe8, /* b 81b0 <sp_16_code_end> */
1251 /* 00008204 <sp_8_done>: */
1252 0xeafffffe /* b 8204 <sp_8_done> */
1255 armv4_5_info.common_magic = ARMV4_5_COMMON_MAGIC;
1256 armv4_5_info.core_mode = ARMV4_5_MODE_SVC;
1257 armv4_5_info.core_state = ARMV4_5_STATE_ARM;
1259 /* flash write code */
1260 if (!cfi_info->write_algorithm)
1264 /* convert bus-width dependent algorithm code to correct endiannes */
1265 if (bank->bus_width == 1)
1267 code_p = malloc(24 * 4);
1269 for (i = 0; i < 24; i++)
1270 target_buffer_set_u32(target, code_p + (i*4), word_8_code[i]);
1272 else if (bank->bus_width == 2)
1274 code_p = malloc(24 * 4);
1276 for (i = 0; i < 24; i++)
1277 target_buffer_set_u32(target, code_p + (i*4), word_16_code[i]);
1279 else if (bank->bus_width == 4)
1281 code_p = malloc(24 * 4);
1283 for (i = 0; i < 24; i++)
1284 target_buffer_set_u32(target, code_p + (i*4), word_32_code[i]);
1288 return ERROR_FLASH_OPERATION_FAILED;
1291 /* allocate working area */
1292 if (target_alloc_working_area(target, 24 * 4,
1293 &cfi_info->write_algorithm) != ERROR_OK)
1295 WARNING("no working area available, can't do block memory writes");
1296 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1299 /* write algorithm code to working area */
1300 target_write_buffer(target, cfi_info->write_algorithm->address, 24 * 4, code_p);
1305 while (target_alloc_working_area(target, buffer_size, &source) != ERROR_OK)
1308 if (buffer_size <= 256)
1310 /* if we already allocated the writing code, but failed to get a buffer, free the algorithm */
1311 if (cfi_info->write_algorithm)
1312 target_free_working_area(target, cfi_info->write_algorithm);
1314 WARNING("not enough working area available, can't do block memory writes");
1315 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1319 init_reg_param(®_params[0], "r0", 32, PARAM_OUT);
1320 init_reg_param(®_params[1], "r1", 32, PARAM_OUT);
1321 init_reg_param(®_params[2], "r2", 32, PARAM_OUT);
1322 init_reg_param(®_params[3], "r3", 32, PARAM_OUT);
1323 init_reg_param(®_params[4], "r4", 32, PARAM_OUT);
1324 init_reg_param(®_params[5], "r5", 32, PARAM_IN);
1325 init_reg_param(®_params[6], "r8", 32, PARAM_OUT);
1326 init_reg_param(®_params[7], "r9", 32, PARAM_OUT);
1327 init_reg_param(®_params[8], "r10", 32, PARAM_OUT);
1328 init_reg_param(®_params[9], "r11", 32, PARAM_OUT);
1332 u32 thisrun_count = (count > buffer_size) ? buffer_size : count;
1334 target_write_buffer(target, source->address, thisrun_count, buffer);
1336 buf_set_u32(reg_params[0].value, 0, 32, source->address);
1337 buf_set_u32(reg_params[1].value, 0, 32, address);
1338 buf_set_u32(reg_params[2].value, 0, 32, thisrun_count / bank->bus_width);
1339 cfi_command(bank, 0xA0, write_command);
1340 buf_set_u32(reg_params[3].value, 0, 32, buf_get_u32(write_command, 0, 32));
1341 cfi_command(bank, 0x80, write_command);
1342 buf_set_u32(reg_params[4].value, 0, 32, buf_get_u32(write_command, 0, 32));
1343 buf_set_u32(reg_params[6].value, 0, 32, flash_address(bank, 0, pri_ext->_unlock1));
1344 buf_set_u32(reg_params[7].value, 0, 32, 0xaa);
1345 buf_set_u32(reg_params[8].value, 0, 32, flash_address(bank, 0, pri_ext->_unlock2));
1346 buf_set_u32(reg_params[9].value, 0, 32, 0x55);
1348 retval = target->type->run_algorithm(target, 0, NULL, 10, reg_params,
1349 cfi_info->write_algorithm->address,
1350 cfi_info->write_algorithm->address + ((24 * 4) - 4),
1351 10000, &armv4_5_info);
1353 status = buf_get_u32(reg_params[5].value, 0, 32);
1355 if ((retval != ERROR_OK) || status != 0x80)
1357 DEBUG("status: 0x%x", status);
1358 exit_code = ERROR_FLASH_OPERATION_FAILED;
1362 buffer += thisrun_count;
1363 address += thisrun_count;
1364 count -= thisrun_count;
1367 target_free_working_area(target, source);
1369 destroy_reg_param(®_params[0]);
1370 destroy_reg_param(®_params[1]);
1371 destroy_reg_param(®_params[2]);
1372 destroy_reg_param(®_params[3]);
1373 destroy_reg_param(®_params[4]);
1374 destroy_reg_param(®_params[5]);
1375 destroy_reg_param(®_params[6]);
1376 destroy_reg_param(®_params[7]);
1377 destroy_reg_param(®_params[8]);
1378 destroy_reg_param(®_params[9]);
1383 int cfi_intel_write_word(struct flash_bank_s *bank, u8 *word, u32 address)
1385 cfi_flash_bank_t *cfi_info = bank->driver_priv;
1386 target_t *target = cfi_info->target;
1389 cfi_intel_clear_status_register(bank);
1390 cfi_command(bank, 0x40, command);
1391 target->type->write_memory(target, address, bank->bus_width, 1, command);
1393 target->type->write_memory(target, address, bank->bus_width, 1, word);
1395 if (cfi_intel_wait_status_busy(bank, 1000 * (1 << cfi_info->word_write_timeout_max)) != 0x80)
1397 cfi_command(bank, 0xff, command);
1398 target->type->write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
1400 ERROR("couldn't write word at base 0x%x, address %x", bank->base, address);
1401 return ERROR_FLASH_OPERATION_FAILED;
1407 int cfi_spansion_write_word(struct flash_bank_s *bank, u8 *word, u32 address)
1409 cfi_flash_bank_t *cfi_info = bank->driver_priv;
1410 cfi_spansion_pri_ext_t *pri_ext = cfi_info->pri_ext;
1411 target_t *target = cfi_info->target;
1414 cfi_command(bank, 0xaa, command);
1415 target->type->write_memory(target, flash_address(bank, 0, pri_ext->_unlock1), bank->bus_width, 1, command);
1417 cfi_command(bank, 0x55, command);
1418 target->type->write_memory(target, flash_address(bank, 0, pri_ext->_unlock2), bank->bus_width, 1, command);
1420 cfi_command(bank, 0xa0, command);
1421 target->type->write_memory(target, flash_address(bank, 0, pri_ext->_unlock1), bank->bus_width, 1, command);
1423 target->type->write_memory(target, address, bank->bus_width, 1, word);
1425 if (cfi_spansion_wait_status_busy(bank, 1000 * (1 << cfi_info->word_write_timeout_max)) != ERROR_OK)
1427 cfi_command(bank, 0xf0, command);
1428 target->type->write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
1430 ERROR("couldn't write word at base 0x%x, address %x", bank->base, address);
1431 return ERROR_FLASH_OPERATION_FAILED;
1437 int cfi_write_word(struct flash_bank_s *bank, u8 *word, u32 address)
1439 cfi_flash_bank_t *cfi_info = bank->driver_priv;
1441 switch(cfi_info->pri_id)
1445 return cfi_intel_write_word(bank, word, address);
1448 return cfi_spansion_write_word(bank, word, address);
1451 ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
1455 return ERROR_FLASH_OPERATION_FAILED;
1458 int cfi_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
1460 cfi_flash_bank_t *cfi_info = bank->driver_priv;
1461 target_t *target = cfi_info->target;
1462 u32 address = bank->base + offset; /* address of first byte to be programmed */
1463 u32 write_p, copy_p;
1464 int align; /* number of unaligned bytes */
1465 u8 current_word[CFI_MAX_BUS_WIDTH * 4]; /* word (bus_width size) currently being programmed */
1469 if (cfi_info->target->state != TARGET_HALTED)
1471 return ERROR_TARGET_NOT_HALTED;
1474 if (offset + count > bank->size)
1475 return ERROR_FLASH_DST_OUT_OF_BANK;
1477 if (cfi_info->qry[0] != 'Q')
1478 return ERROR_FLASH_BANK_NOT_PROBED;
1480 /* start at the first byte of the first word (bus_width size) */
1481 write_p = address & ~(bank->bus_width - 1);
1482 if ((align = address - write_p) != 0)
1484 for (i = 0; i < bank->bus_width; i++)
1485 current_word[i] = 0;
1488 /* copy bytes before the first write address */
1489 for (i = 0; i < align; ++i, ++copy_p)
1492 target->type->read_memory(target, copy_p, 1, 1, &byte);
1493 cfi_add_byte(bank, current_word, byte);
1496 /* add bytes from the buffer */
1497 for (; (i < bank->bus_width) && (count > 0); i++)
1499 cfi_add_byte(bank, current_word, *buffer++);
1504 /* if the buffer is already finished, copy bytes after the last write address */
1505 for (; (count == 0) && (i < bank->bus_width); ++i, ++copy_p)
1508 target->type->read_memory(target, copy_p, 1, 1, &byte);
1509 cfi_add_byte(bank, current_word, byte);
1512 retval = cfi_write_word(bank, current_word, write_p);
1513 if (retval != ERROR_OK)
1518 /* handle blocks of bus_size aligned bytes */
1519 switch(cfi_info->pri_id)
1521 /* try block writes (fails without working area) */
1524 retval = cfi_intel_write_block(bank, buffer, write_p, count);
1527 retval = cfi_spansion_write_block(bank, buffer, write_p, count);
1530 ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
1531 retval = ERROR_FLASH_OPERATION_FAILED;
1534 if (retval != ERROR_OK)
1536 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1538 /* fall back to memory writes */
1539 while (count > bank->bus_width)
1541 for (i = 0; i < bank->bus_width; i++)
1542 current_word[i] = 0;
1544 for (i = 0; i < bank->bus_width; i++)
1546 cfi_add_byte(bank, current_word, *buffer++);
1549 retval = cfi_write_word(bank, current_word, write_p);
1550 if (retval != ERROR_OK)
1552 write_p += bank->bus_width;
1553 count -= bank->bus_width;
1560 /* handle unaligned tail bytes */
1564 for (i = 0; i < bank->bus_width; i++)
1565 current_word[i] = 0;
1567 for (i = 0; (i < bank->bus_width) && (count > 0); ++i, ++copy_p)
1569 cfi_add_byte(bank, current_word, *buffer++);
1572 for (; i < bank->bus_width; ++i, ++copy_p)
1575 target->type->read_memory(target, copy_p, 1, 1, &byte);
1576 cfi_add_byte(bank, current_word, byte);
1578 retval = cfi_write_word(bank, current_word, write_p);
1579 if (retval != ERROR_OK)
1583 /* return to read array mode */
1584 cfi_command(bank, 0xf0, current_word);
1585 target->type->write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, current_word);
1586 cfi_command(bank, 0xff, current_word);
1587 target->type->write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, current_word);
1592 void cfi_fixup_atmel_reversed_erase_regions(flash_bank_t *bank, void *param)
1594 cfi_flash_bank_t *cfi_info = bank->driver_priv;
1595 cfi_spansion_pri_ext_t *pri_ext = cfi_info->pri_ext;
1597 pri_ext->_reversed_geometry = 1;
1600 void cfi_fixup_0002_erase_regions(flash_bank_t *bank, void *param)
1603 cfi_flash_bank_t *cfi_info = bank->driver_priv;
1604 cfi_spansion_pri_ext_t *pri_ext = cfi_info->pri_ext;
1606 if ((pri_ext->_reversed_geometry) || (pri_ext->TopBottom == 3))
1608 DEBUG("swapping reversed erase region information on cmdset 0002 device");
1610 for (i = 0; i < cfi_info->num_erase_regions / 2; i++)
1612 int j = (cfi_info->num_erase_regions - 1) - i;
1615 swap = cfi_info->erase_region_info[i];
1616 cfi_info->erase_region_info[i] = cfi_info->erase_region_info[j];
1617 cfi_info->erase_region_info[j] = swap;
1622 void cfi_fixup_0002_unlock_addresses(flash_bank_t *bank, void *param)
1624 cfi_flash_bank_t *cfi_info = bank->driver_priv;
1625 cfi_spansion_pri_ext_t *pri_ext = cfi_info->pri_ext;
1626 cfi_unlock_addresses_t *unlock_addresses = param;
1628 pri_ext->_unlock1 = unlock_addresses->unlock1;
1629 pri_ext->_unlock2 = unlock_addresses->unlock2;
1632 int cfi_probe(struct flash_bank_s *bank)
1634 cfi_flash_bank_t *cfi_info = bank->driver_priv;
1635 target_t *target = cfi_info->target;
1637 int num_sectors = 0;
1641 u32 unlock1 = 0x555;
1642 u32 unlock2 = 0x2aa;
1644 /* JEDEC standard JESD21C uses 0x5555 and 0x2aaa as unlock addresses,
1645 * while CFI compatible AMD/Spansion flashes use 0x555 and 0x2aa
1647 if (cfi_info->jedec_probe)
1653 /* switch to read identifier codes mode ("AUTOSELECT") */
1654 cfi_command(bank, 0xaa, command);
1655 target->type->write_memory(target, flash_address(bank, 0, unlock1), bank->bus_width, 1, command);
1656 cfi_command(bank, 0x55, command);
1657 target->type->write_memory(target, flash_address(bank, 0, unlock2), bank->bus_width, 1, command);
1658 cfi_command(bank, 0x90, command);
1659 target->type->write_memory(target, flash_address(bank, 0, unlock1), bank->bus_width, 1, command);
1661 if (bank->chip_width == 1)
1663 u8 manufacturer, device_id;
1664 target_read_u8(target, bank->base + 0x0, &manufacturer);
1665 target_read_u8(target, bank->base + 0x1, &device_id);
1666 cfi_info->manufacturer = manufacturer;
1667 cfi_info->device_id = device_id;
1669 else if (bank->chip_width == 2)
1671 target_read_u16(target, bank->base + 0x0, &cfi_info->manufacturer);
1672 target_read_u16(target, bank->base + 0x2, &cfi_info->device_id);
1675 /* switch back to read array mode */
1676 cfi_command(bank, 0xf0, command);
1677 target->type->write_memory(target, flash_address(bank, 0, 0x00), bank->bus_width, 1, command);
1678 cfi_command(bank, 0xff, command);
1679 target->type->write_memory(target, flash_address(bank, 0, 0x00), bank->bus_width, 1, command);
1681 cfi_fixup(bank, cfi_jedec_fixups);
1683 /* query only if this is a CFI compatible flash,
1684 * otherwise the relevant info has already been filled in
1686 if (cfi_info->not_cfi == 0)
1688 /* enter CFI query mode
1689 * according to JEDEC Standard No. 68.01,
1690 * a single bus sequence with address = 0x55, data = 0x98 should put
1691 * the device into CFI query mode.
1693 * SST flashes clearly violate this, and we will consider them incompatbile for now
1695 cfi_command(bank, 0x98, command);
1696 target->type->write_memory(target, flash_address(bank, 0, 0x55), bank->bus_width, 1, command);
1698 cfi_info->qry[0] = cfi_query_u8(bank, 0, 0x10);
1699 cfi_info->qry[1] = cfi_query_u8(bank, 0, 0x11);
1700 cfi_info->qry[2] = cfi_query_u8(bank, 0, 0x12);
1702 DEBUG("CFI qry returned: 0x%2.2x 0x%2.2x 0x%2.2x", cfi_info->qry[0], cfi_info->qry[1], cfi_info->qry[2]);
1704 if ((cfi_info->qry[0] != 'Q') || (cfi_info->qry[1] != 'R') || (cfi_info->qry[2] != 'Y'))
1706 cfi_command(bank, 0xf0, command);
1707 target->type->write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
1708 cfi_command(bank, 0xff, command);
1709 target->type->write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
1710 return ERROR_FLASH_BANK_INVALID;
1713 cfi_info->pri_id = cfi_query_u16(bank, 0, 0x13);
1714 cfi_info->pri_addr = cfi_query_u16(bank, 0, 0x15);
1715 cfi_info->alt_id = cfi_query_u16(bank, 0, 0x17);
1716 cfi_info->alt_addr = cfi_query_u16(bank, 0, 0x19);
1718 DEBUG("qry: '%c%c%c', pri_id: 0x%4.4x, pri_addr: 0x%4.4x, alt_id: 0x%4.4x, alt_addr: 0x%4.4x", cfi_info->qry[0], cfi_info->qry[1], cfi_info->qry[2], cfi_info->pri_id, cfi_info->pri_addr, cfi_info->alt_id, cfi_info->alt_addr);
1720 cfi_info->vcc_min = cfi_query_u8(bank, 0, 0x1b);
1721 cfi_info->vcc_max = cfi_query_u8(bank, 0, 0x1c);
1722 cfi_info->vpp_min = cfi_query_u8(bank, 0, 0x1d);
1723 cfi_info->vpp_max = cfi_query_u8(bank, 0, 0x1e);
1724 cfi_info->word_write_timeout_typ = cfi_query_u8(bank, 0, 0x1f);
1725 cfi_info->buf_write_timeout_typ = cfi_query_u8(bank, 0, 0x20);
1726 cfi_info->block_erase_timeout_typ = cfi_query_u8(bank, 0, 0x21);
1727 cfi_info->chip_erase_timeout_typ = cfi_query_u8(bank, 0, 0x22);
1728 cfi_info->word_write_timeout_max = cfi_query_u8(bank, 0, 0x23);
1729 cfi_info->buf_write_timeout_max = cfi_query_u8(bank, 0, 0x24);
1730 cfi_info->block_erase_timeout_max = cfi_query_u8(bank, 0, 0x25);
1731 cfi_info->chip_erase_timeout_max = cfi_query_u8(bank, 0, 0x26);
1733 DEBUG("Vcc min: %1.1x.%1.1x, Vcc max: %1.1x.%1.1x, Vpp min: %1.1x.%1.1x, Vpp max: %1.1x.%1.1x",
1734 (cfi_info->vcc_min & 0xf0) >> 4, cfi_info->vcc_min & 0x0f,
1735 (cfi_info->vcc_max & 0xf0) >> 4, cfi_info->vcc_max & 0x0f,
1736 (cfi_info->vpp_min & 0xf0) >> 4, cfi_info->vpp_min & 0x0f,
1737 (cfi_info->vpp_max & 0xf0) >> 4, cfi_info->vpp_max & 0x0f);
1738 DEBUG("typ. word write timeout: %u, typ. buf write timeout: %u, typ. block erase timeout: %u, typ. chip erase timeout: %u", 1 << cfi_info->word_write_timeout_typ, 1 << cfi_info->buf_write_timeout_typ,
1739 1 << cfi_info->block_erase_timeout_typ, 1 << cfi_info->chip_erase_timeout_typ);
1740 DEBUG("max. word write timeout: %u, max. buf write timeout: %u, max. block erase timeout: %u, max. chip erase timeout: %u", (1 << cfi_info->word_write_timeout_max) * (1 << cfi_info->word_write_timeout_typ),
1741 (1 << cfi_info->buf_write_timeout_max) * (1 << cfi_info->buf_write_timeout_typ),
1742 (1 << cfi_info->block_erase_timeout_max) * (1 << cfi_info->block_erase_timeout_typ),
1743 (1 << cfi_info->chip_erase_timeout_max) * (1 << cfi_info->chip_erase_timeout_typ));
1745 cfi_info->dev_size = cfi_query_u8(bank, 0, 0x27);
1746 cfi_info->interface_desc = cfi_query_u16(bank, 0, 0x28);
1747 cfi_info->max_buf_write_size = cfi_query_u16(bank, 0, 0x2a);
1748 cfi_info->num_erase_regions = cfi_query_u8(bank, 0, 0x2c);
1750 DEBUG("size: 0x%x, interface desc: %i, max buffer write size: %x", 1 << cfi_info->dev_size, cfi_info->interface_desc, (1 << cfi_info->max_buf_write_size));
1752 if (((1 << cfi_info->dev_size) * bank->bus_width / bank->chip_width) != bank->size)
1754 WARNING("configuration specifies 0x%x size, but a 0x%x size flash was found", bank->size, 1 << cfi_info->dev_size);
1757 if (cfi_info->num_erase_regions)
1759 cfi_info->erase_region_info = malloc(4 * cfi_info->num_erase_regions);
1760 for (i = 0; i < cfi_info->num_erase_regions; i++)
1762 cfi_info->erase_region_info[i] = cfi_query_u32(bank, 0, 0x2d + (4 * i));
1763 DEBUG("erase region[%i]: %i blocks of size 0x%x", i, (cfi_info->erase_region_info[i] & 0xffff) + 1, (cfi_info->erase_region_info[i] >> 16) * 256);
1768 cfi_info->erase_region_info = NULL;
1771 /* We need to read the primary algorithm extended query table before calculating
1772 * the sector layout to be able to apply fixups
1774 switch(cfi_info->pri_id)
1776 /* Intel command set (standard and extended) */
1779 cfi_read_intel_pri_ext(bank);
1781 /* AMD/Spansion, Atmel, ... command set */
1783 cfi_read_0002_pri_ext(bank);
1786 ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
1790 /* return to read array mode
1791 * we use both reset commands, as some Intel flashes fail to recognize the 0xF0 command
1793 cfi_command(bank, 0xf0, command);
1794 target->type->write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
1795 cfi_command(bank, 0xff, command);
1796 target->type->write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
1799 /* apply fixups depending on the primary command set */
1800 switch(cfi_info->pri_id)
1802 /* Intel command set (standard and extended) */
1805 cfi_fixup(bank, cfi_0001_fixups);
1807 /* AMD/Spansion, Atmel, ... command set */
1809 cfi_fixup(bank, cfi_0002_fixups);
1812 ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
1816 if (cfi_info->num_erase_regions == 0)
1818 /* a device might have only one erase block, spanning the whole device */
1819 bank->num_sectors = 1;
1820 bank->sectors = malloc(sizeof(flash_sector_t));
1822 bank->sectors[sector].offset = 0x0;
1823 bank->sectors[sector].size = bank->size;
1824 bank->sectors[sector].is_erased = -1;
1825 bank->sectors[sector].is_protected = -1;
1829 for (i = 0; i < cfi_info->num_erase_regions; i++)
1831 num_sectors += (cfi_info->erase_region_info[i] & 0xffff) + 1;
1834 bank->num_sectors = num_sectors;
1835 bank->sectors = malloc(sizeof(flash_sector_t) * num_sectors);
1837 for (i = 0; i < cfi_info->num_erase_regions; i++)
1840 for (j = 0; j < (cfi_info->erase_region_info[i] & 0xffff) + 1; j++)
1842 bank->sectors[sector].offset = offset;
1843 bank->sectors[sector].size = ((cfi_info->erase_region_info[i] >> 16) * 256) * bank->bus_width / bank->chip_width;
1844 offset += bank->sectors[sector].size;
1845 bank->sectors[sector].is_erased = -1;
1846 bank->sectors[sector].is_protected = -1;
1855 int cfi_erase_check(struct flash_bank_s *bank)
1857 cfi_flash_bank_t *cfi_info = bank->driver_priv;
1858 target_t *target = cfi_info->target;
1862 if (!cfi_info->erase_check_algorithm)
1864 u32 erase_check_code[] =
1866 0xe4d03001, /* ldrb r3, [r0], #1 */
1867 0xe0022003, /* and r2, r2, r3 */
1868 0xe2511001, /* subs r1, r1, #1 */
1869 0x1afffffb, /* b -4 */
1870 0xeafffffe /* b 0 */
1873 /* make sure we have a working area */
1874 if (target_alloc_working_area(target, 20, &cfi_info->erase_check_algorithm) != ERROR_OK)
1876 WARNING("no working area available, falling back to slow memory reads");
1880 u8 erase_check_code_buf[5 * 4];
1882 for (i = 0; i < 5; i++)
1883 target_buffer_set_u32(target, erase_check_code_buf + (i*4), erase_check_code[i]);
1885 /* write algorithm code to working area */
1886 target->type->write_memory(target, cfi_info->erase_check_algorithm->address, 4, 5, erase_check_code_buf);
1890 if (!cfi_info->erase_check_algorithm)
1892 u32 *buffer = malloc(4096);
1894 for (i = 0; i < bank->num_sectors; i++)
1896 u32 address = bank->base + bank->sectors[i].offset;
1897 u32 size = bank->sectors[i].size;
1898 u32 check = 0xffffffffU;
1903 u32 thisrun_size = (size > 4096) ? 4096 : size;
1906 target->type->read_memory(target, address, 4, thisrun_size / 4, (u8*)buffer);
1908 for (j = 0; j < thisrun_size / 4; j++)
1911 if (check != 0xffffffff)
1917 size -= thisrun_size;
1918 address += thisrun_size;
1921 bank->sectors[i].is_erased = erased;
1928 for (i = 0; i < bank->num_sectors; i++)
1930 u32 address = bank->base + bank->sectors[i].offset;
1931 u32 size = bank->sectors[i].size;
1933 reg_param_t reg_params[3];
1934 armv4_5_algorithm_t armv4_5_info;
1936 armv4_5_info.common_magic = ARMV4_5_COMMON_MAGIC;
1937 armv4_5_info.core_mode = ARMV4_5_MODE_SVC;
1938 armv4_5_info.core_state = ARMV4_5_STATE_ARM;
1940 init_reg_param(®_params[0], "r0", 32, PARAM_OUT);
1941 buf_set_u32(reg_params[0].value, 0, 32, address);
1943 init_reg_param(®_params[1], "r1", 32, PARAM_OUT);
1944 buf_set_u32(reg_params[1].value, 0, 32, size);
1946 init_reg_param(®_params[2], "r2", 32, PARAM_IN_OUT);
1947 buf_set_u32(reg_params[2].value, 0, 32, 0xff);
1949 if ((retval = target->type->run_algorithm(target, 0, NULL, 3, reg_params, cfi_info->erase_check_algorithm->address, cfi_info->erase_check_algorithm->address + 0x10, 10000, &armv4_5_info)) != ERROR_OK)
1950 return ERROR_FLASH_OPERATION_FAILED;
1952 if (buf_get_u32(reg_params[2].value, 0, 32) == 0xff)
1953 bank->sectors[i].is_erased = 1;
1955 bank->sectors[i].is_erased = 0;
1957 destroy_reg_param(®_params[0]);
1958 destroy_reg_param(®_params[1]);
1959 destroy_reg_param(®_params[2]);
1966 int cfi_intel_protect_check(struct flash_bank_s *bank)
1968 cfi_flash_bank_t *cfi_info = bank->driver_priv;
1969 cfi_intel_pri_ext_t *pri_ext = cfi_info->pri_ext;
1970 target_t *target = cfi_info->target;
1971 u8 command[CFI_MAX_BUS_WIDTH];
1974 /* check if block lock bits are supported on this device */
1975 if (!(pri_ext->blk_status_reg_mask & 0x1))
1976 return ERROR_FLASH_OPERATION_FAILED;
1978 cfi_command(bank, 0x90, command);
1979 target->type->write_memory(target, flash_address(bank, 0, 0x55), bank->bus_width, 1, command);
1981 for (i = 0; i < bank->num_sectors; i++)
1983 u8 block_status = cfi_get_u8(bank, i, 0x2);
1985 if (block_status & 1)
1986 bank->sectors[i].is_protected = 1;
1988 bank->sectors[i].is_protected = 0;
1991 cfi_command(bank, 0xff, command);
1992 target->type->write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
1997 int cfi_spansion_protect_check(struct flash_bank_s *bank)
1999 cfi_flash_bank_t *cfi_info = bank->driver_priv;
2000 cfi_spansion_pri_ext_t *pri_ext = cfi_info->pri_ext;
2001 target_t *target = cfi_info->target;
2005 cfi_command(bank, 0xaa, command);
2006 target->type->write_memory(target, flash_address(bank, 0, pri_ext->_unlock1), bank->bus_width, 1, command);
2008 cfi_command(bank, 0x55, command);
2009 target->type->write_memory(target, flash_address(bank, 0, pri_ext->_unlock2), bank->bus_width, 1, command);
2011 cfi_command(bank, 0x90, command);
2012 target->type->write_memory(target, flash_address(bank, 0, pri_ext->_unlock1), bank->bus_width, 1, command);
2014 for (i = 0; i < bank->num_sectors; i++)
2016 u8 block_status = cfi_get_u8(bank, i, 0x2);
2018 if (block_status & 1)
2019 bank->sectors[i].is_protected = 1;
2021 bank->sectors[i].is_protected = 0;
2024 cfi_command(bank, 0xf0, command);
2025 target->type->write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
2030 int cfi_protect_check(struct flash_bank_s *bank)
2032 cfi_flash_bank_t *cfi_info = bank->driver_priv;
2034 if (cfi_info->qry[0] != 'Q')
2035 return ERROR_FLASH_BANK_NOT_PROBED;
2037 switch(cfi_info->pri_id)
2041 return cfi_intel_protect_check(bank);
2044 return cfi_spansion_protect_check(bank);
2047 ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
2054 int cfi_info(struct flash_bank_s *bank, char *buf, int buf_size)
2057 cfi_flash_bank_t *cfi_info = bank->driver_priv;
2059 if (cfi_info->qry[0] == -1)
2061 printed = snprintf(buf, buf_size, "\ncfi flash bank not probed yet\n");
2065 printed = snprintf(buf, buf_size, "\ncfi information:\n");
2067 buf_size -= printed;
2069 printed = snprintf(buf, buf_size, "\nmfr: 0x%4.4x, id:0x%4.4x\n",
2070 cfi_info->manufacturer, cfi_info->device_id);
2072 buf_size -= printed;
2074 printed = snprintf(buf, buf_size, "qry: '%c%c%c', pri_id: 0x%4.4x, pri_addr: 0x%4.4x, alt_id: 0x%4.4x, alt_addr: 0x%4.4x\n", cfi_info->qry[0], cfi_info->qry[1], cfi_info->qry[2], cfi_info->pri_id, cfi_info->pri_addr, cfi_info->alt_id, cfi_info->alt_addr);
2076 buf_size -= printed;
2078 printed = snprintf(buf, buf_size, "Vcc min: %1.1x.%1.1x, Vcc max: %1.1x.%1.1x, Vpp min: %1.1x.%1.1x, Vpp max: %1.1x.%1.1x\n", (cfi_info->vcc_min & 0xf0) >> 4, cfi_info->vcc_min & 0x0f,
2079 (cfi_info->vcc_max & 0xf0) >> 4, cfi_info->vcc_max & 0x0f,
2080 (cfi_info->vpp_min & 0xf0) >> 4, cfi_info->vpp_min & 0x0f,
2081 (cfi_info->vpp_max & 0xf0) >> 4, cfi_info->vpp_max & 0x0f);
2083 buf_size -= printed;
2085 printed = snprintf(buf, buf_size, "typ. word write timeout: %u, typ. buf write timeout: %u, typ. block erase timeout: %u, typ. chip erase timeout: %u\n", 1 << cfi_info->word_write_timeout_typ, 1 << cfi_info->buf_write_timeout_typ,
2086 1 << cfi_info->block_erase_timeout_typ, 1 << cfi_info->chip_erase_timeout_typ);
2088 buf_size -= printed;
2090 printed = snprintf(buf, buf_size, "max. word write timeout: %u, max. buf write timeout: %u, max. block erase timeout: %u, max. chip erase timeout: %u\n", (1 << cfi_info->word_write_timeout_max) * (1 << cfi_info->word_write_timeout_typ),
2091 (1 << cfi_info->buf_write_timeout_max) * (1 << cfi_info->buf_write_timeout_typ),
2092 (1 << cfi_info->block_erase_timeout_max) * (1 << cfi_info->block_erase_timeout_typ),
2093 (1 << cfi_info->chip_erase_timeout_max) * (1 << cfi_info->chip_erase_timeout_typ));
2095 buf_size -= printed;
2097 printed = snprintf(buf, buf_size, "size: 0x%x, interface desc: %i, max buffer write size: %x\n", 1 << cfi_info->dev_size, cfi_info->interface_desc, cfi_info->max_buf_write_size);
2099 buf_size -= printed;
2101 switch(cfi_info->pri_id)
2105 cfi_intel_info(bank, buf, buf_size);
2108 cfi_spansion_info(bank, buf, buf_size);
2111 ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);