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 <helper/binarybuffer.h>
33 #include <target/algorithm.h>
36 #define CFI_MAX_BUS_WIDTH 4
37 #define CFI_MAX_CHIP_WIDTH 4
39 /* defines internal maximum size for code fragment in cfi_intel_write_block() */
40 #define CFI_MAX_INTEL_CODESIZE 256
42 static struct cfi_unlock_addresses cfi_unlock_addresses
[] =
44 [CFI_UNLOCK_555_2AA
] = { .unlock1
= 0x555, .unlock2
= 0x2aa },
45 [CFI_UNLOCK_5555_2AAA
] = { .unlock1
= 0x5555, .unlock2
= 0x2aaa },
48 /* CFI fixups foward declarations */
49 static void cfi_fixup_0002_erase_regions(struct flash_bank
*flash
, void *param
);
50 static void cfi_fixup_0002_unlock_addresses(struct flash_bank
*flash
, void *param
);
51 static void cfi_fixup_atmel_reversed_erase_regions(struct flash_bank
*flash
, void *param
);
53 /* fixup after reading cmdset 0002 primary query table */
54 static const struct cfi_fixup cfi_0002_fixups
[] = {
55 {CFI_MFR_SST
, 0x00D4, cfi_fixup_0002_unlock_addresses
, &cfi_unlock_addresses
[CFI_UNLOCK_5555_2AAA
]},
56 {CFI_MFR_SST
, 0x00D5, cfi_fixup_0002_unlock_addresses
, &cfi_unlock_addresses
[CFI_UNLOCK_5555_2AAA
]},
57 {CFI_MFR_SST
, 0x00D6, cfi_fixup_0002_unlock_addresses
, &cfi_unlock_addresses
[CFI_UNLOCK_5555_2AAA
]},
58 {CFI_MFR_SST
, 0x00D7, cfi_fixup_0002_unlock_addresses
, &cfi_unlock_addresses
[CFI_UNLOCK_5555_2AAA
]},
59 {CFI_MFR_SST
, 0x2780, cfi_fixup_0002_unlock_addresses
, &cfi_unlock_addresses
[CFI_UNLOCK_5555_2AAA
]},
60 {CFI_MFR_ATMEL
, 0x00C8, cfi_fixup_atmel_reversed_erase_regions
, NULL
},
61 {CFI_MFR_FUJITSU
, 0x22ea, cfi_fixup_0002_unlock_addresses
, &cfi_unlock_addresses
[CFI_UNLOCK_555_2AA
]},
62 {CFI_MFR_FUJITSU
, 0x226b, cfi_fixup_0002_unlock_addresses
, &cfi_unlock_addresses
[CFI_UNLOCK_5555_2AAA
]},
63 {CFI_MFR_AMIC
, 0xb31a, cfi_fixup_0002_unlock_addresses
, &cfi_unlock_addresses
[CFI_UNLOCK_555_2AA
]},
64 {CFI_MFR_MX
, 0x225b, cfi_fixup_0002_unlock_addresses
, &cfi_unlock_addresses
[CFI_UNLOCK_555_2AA
]},
65 {CFI_MFR_AMD
, 0x225b, cfi_fixup_0002_unlock_addresses
, &cfi_unlock_addresses
[CFI_UNLOCK_555_2AA
]},
66 {CFI_MFR_ANY
, CFI_ID_ANY
, cfi_fixup_0002_erase_regions
, NULL
},
70 /* fixup after reading cmdset 0001 primary query table */
71 static const struct cfi_fixup cfi_0001_fixups
[] = {
75 static void cfi_fixup(struct flash_bank
*bank
, const struct cfi_fixup
*fixups
)
77 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
78 const struct cfi_fixup
*f
;
80 for (f
= fixups
; f
->fixup
; f
++)
82 if (((f
->mfr
== CFI_MFR_ANY
) || (f
->mfr
== cfi_info
->manufacturer
)) &&
83 ((f
->id
== CFI_ID_ANY
) || (f
->id
== cfi_info
->device_id
)))
85 f
->fixup(bank
, f
->param
);
90 /* inline uint32_t flash_address(struct flash_bank *bank, int sector, uint32_t offset) */
91 static __inline__
uint32_t flash_address(struct flash_bank
*bank
, int sector
, uint32_t offset
)
93 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
95 if (cfi_info
->x16_as_x8
) offset
*= 2;
97 /* while the sector list isn't built, only accesses to sector 0 work */
99 return bank
->base
+ offset
* bank
->bus_width
;
104 LOG_ERROR("BUG: sector list not yet built");
107 return bank
->base
+ bank
->sectors
[sector
].offset
+ offset
* bank
->bus_width
;
111 static void cfi_command(struct flash_bank
*bank
, uint8_t cmd
, uint8_t *cmd_buf
)
115 /* clear whole buffer, to ensure bits that exceed the bus_width
118 for (i
= 0; i
< CFI_MAX_BUS_WIDTH
; i
++)
121 if (bank
->target
->endianness
== TARGET_LITTLE_ENDIAN
)
123 for (i
= bank
->bus_width
; i
> 0; i
--)
125 *cmd_buf
++ = (i
& (bank
->chip_width
- 1)) ? 0x0 : cmd
;
130 for (i
= 1; i
<= bank
->bus_width
; i
++)
132 *cmd_buf
++ = (i
& (bank
->chip_width
- 1)) ? 0x0 : cmd
;
137 static int cfi_send_command(struct flash_bank
*bank
, uint8_t cmd
, uint32_t address
)
139 uint8_t command
[CFI_MAX_BUS_WIDTH
];
141 cfi_command(bank
, cmd
, command
);
142 return target_write_memory(bank
->target
, address
, bank
->bus_width
, 1, command
);
145 /* read unsigned 8-bit value from the bank
146 * flash banks are expected to be made of similar chips
147 * the query result should be the same for all
149 static int cfi_query_u8(struct flash_bank
*bank
, int sector
, uint32_t offset
, uint8_t *val
)
151 struct target
*target
= bank
->target
;
152 uint8_t data
[CFI_MAX_BUS_WIDTH
];
155 retval
= target_read_memory(target
, flash_address(bank
, sector
, offset
), bank
->bus_width
, 1, data
);
156 if (retval
!= ERROR_OK
)
159 if (bank
->target
->endianness
== TARGET_LITTLE_ENDIAN
)
162 *val
= data
[bank
->bus_width
- 1];
167 /* read unsigned 8-bit value from the bank
168 * in case of a bank made of multiple chips,
169 * the individual values are ORed
171 static int cfi_get_u8(struct flash_bank
*bank
, int sector
, uint32_t offset
, uint8_t *val
)
173 struct target
*target
= bank
->target
;
174 uint8_t data
[CFI_MAX_BUS_WIDTH
];
178 retval
= target_read_memory(target
, flash_address(bank
, sector
, offset
), bank
->bus_width
, 1, data
);
179 if (retval
!= ERROR_OK
)
182 if (bank
->target
->endianness
== TARGET_LITTLE_ENDIAN
)
184 for (i
= 0; i
< bank
->bus_width
/ bank
->chip_width
; i
++)
192 for (i
= 0; i
< bank
->bus_width
/ bank
->chip_width
; i
++)
193 value
|= data
[bank
->bus_width
- 1 - i
];
200 static int cfi_query_u16(struct flash_bank
*bank
, int sector
, uint32_t offset
, uint16_t *val
)
202 struct target
*target
= bank
->target
;
203 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
204 uint8_t data
[CFI_MAX_BUS_WIDTH
* 2];
207 if (cfi_info
->x16_as_x8
)
210 for (i
= 0;i
< 2;i
++)
212 retval
= target_read_memory(target
, flash_address(bank
, sector
, offset
+ i
), bank
->bus_width
, 1,
213 &data
[i
*bank
->bus_width
]);
214 if (retval
!= ERROR_OK
)
219 retval
= target_read_memory(target
, flash_address(bank
, sector
, offset
), bank
->bus_width
, 2, data
);
220 if (retval
!= ERROR_OK
)
224 if (bank
->target
->endianness
== TARGET_LITTLE_ENDIAN
)
225 *val
= data
[0] | data
[bank
->bus_width
] << 8;
227 *val
= data
[bank
->bus_width
- 1] | data
[(2 * bank
->bus_width
) - 1] << 8;
232 static int cfi_query_u32(struct flash_bank
*bank
, int sector
, uint32_t offset
, uint32_t *val
)
234 struct target
*target
= bank
->target
;
235 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
236 uint8_t data
[CFI_MAX_BUS_WIDTH
* 4];
239 if (cfi_info
->x16_as_x8
)
242 for (i
= 0;i
< 4;i
++)
244 retval
= target_read_memory(target
, flash_address(bank
, sector
, offset
+ i
), bank
->bus_width
, 1,
245 &data
[i
*bank
->bus_width
]);
246 if (retval
!= ERROR_OK
)
252 retval
= target_read_memory(target
, flash_address(bank
, sector
, offset
), bank
->bus_width
, 4, data
);
253 if (retval
!= ERROR_OK
)
257 if (bank
->target
->endianness
== TARGET_LITTLE_ENDIAN
)
258 *val
= data
[0] | data
[bank
->bus_width
] << 8 | data
[bank
->bus_width
* 2] << 16 | data
[bank
->bus_width
* 3] << 24;
260 *val
= data
[bank
->bus_width
- 1] | data
[(2* bank
->bus_width
) - 1] << 8 |
261 data
[(3 * bank
->bus_width
) - 1] << 16 | data
[(4 * bank
->bus_width
) - 1] << 24;
266 static int cfi_reset(struct flash_bank
*bank
)
268 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
269 int retval
= ERROR_OK
;
271 if ((retval
= cfi_send_command(bank
, 0xf0, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
276 if ((retval
= cfi_send_command(bank
, 0xff, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
281 if (cfi_info
->manufacturer
== 0x20 &&
282 (cfi_info
->device_id
== 0x227E || cfi_info
->device_id
== 0x7E))
284 /* Numonix M29W128G is cmd 0xFF intolerant - causes internal undefined state
285 * so we send an extra 0xF0 reset to fix the bug */
286 if ((retval
= cfi_send_command(bank
, 0xf0, flash_address(bank
, 0, 0x00))) != ERROR_OK
)
295 static void cfi_intel_clear_status_register(struct flash_bank
*bank
)
297 struct target
*target
= bank
->target
;
299 if (target
->state
!= TARGET_HALTED
)
301 LOG_ERROR("BUG: attempted to clear status register while target wasn't halted");
305 cfi_send_command(bank
, 0x50, flash_address(bank
, 0, 0x0));
308 static int cfi_intel_wait_status_busy(struct flash_bank
*bank
, int timeout
, uint8_t *val
)
312 int retval
= ERROR_OK
;
318 LOG_ERROR("timeout while waiting for WSM to become ready");
322 retval
= cfi_get_u8(bank
, 0, 0x0, &status
);
323 if (retval
!= ERROR_OK
)
332 /* mask out bit 0 (reserved) */
333 status
= status
& 0xfe;
335 LOG_DEBUG("status: 0x%x", status
);
339 LOG_ERROR("status register: 0x%x", status
);
341 LOG_ERROR("Block Lock-Bit Detected, Operation Abort");
343 LOG_ERROR("Program suspended");
345 LOG_ERROR("Low Programming Voltage Detected, Operation Aborted");
347 LOG_ERROR("Program Error / Error in Setting Lock-Bit");
349 LOG_ERROR("Error in Block Erasure or Clear Lock-Bits");
351 LOG_ERROR("Block Erase Suspended");
353 cfi_intel_clear_status_register(bank
);
362 static int cfi_spansion_wait_status_busy(struct flash_bank
*bank
, int timeout
)
364 uint8_t status
, oldstatus
;
365 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
368 retval
= cfi_get_u8(bank
, 0, 0x0, &oldstatus
);
369 if (retval
!= ERROR_OK
)
373 retval
= cfi_get_u8(bank
, 0, 0x0, &status
);
375 if (retval
!= ERROR_OK
)
378 if ((status
^ oldstatus
) & 0x40) {
379 if (status
& cfi_info
->status_poll_mask
& 0x20) {
380 retval
= cfi_get_u8(bank
, 0, 0x0, &oldstatus
);
381 if (retval
!= ERROR_OK
)
383 retval
= cfi_get_u8(bank
, 0, 0x0, &status
);
384 if (retval
!= ERROR_OK
)
386 if ((status
^ oldstatus
) & 0x40) {
387 LOG_ERROR("dq5 timeout, status: 0x%x", status
);
388 return(ERROR_FLASH_OPERATION_FAILED
);
390 LOG_DEBUG("status: 0x%x", status
);
394 } else { /* no toggle: finished, OK */
395 LOG_DEBUG("status: 0x%x", status
);
401 } while (timeout
-- > 0);
403 LOG_ERROR("timeout, status: 0x%x", status
);
405 return(ERROR_FLASH_BUSY
);
408 static int cfi_read_intel_pri_ext(struct flash_bank
*bank
)
411 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
412 struct cfi_intel_pri_ext
*pri_ext
;
414 if (cfi_info
->pri_ext
)
415 free(cfi_info
->pri_ext
);
417 pri_ext
= malloc(sizeof(struct cfi_intel_pri_ext
));
420 LOG_ERROR("Out of memory");
423 cfi_info
->pri_ext
= pri_ext
;
425 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 0, &pri_ext
->pri
[0]);
426 if (retval
!= ERROR_OK
)
428 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 1, &pri_ext
->pri
[1]);
429 if (retval
!= ERROR_OK
)
431 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 2, &pri_ext
->pri
[2]);
432 if (retval
!= ERROR_OK
)
435 if ((pri_ext
->pri
[0] != 'P') || (pri_ext
->pri
[1] != 'R') || (pri_ext
->pri
[2] != 'I'))
437 if ((retval
= cfi_reset(bank
)) != ERROR_OK
)
441 LOG_ERROR("Could not read bank flash bank information");
442 return ERROR_FLASH_BANK_INVALID
;
445 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 3, &pri_ext
->major_version
);
446 if (retval
!= ERROR_OK
)
448 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 4, &pri_ext
->minor_version
);
449 if (retval
!= ERROR_OK
)
452 LOG_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
);
454 retval
= cfi_query_u32(bank
, 0, cfi_info
->pri_addr
+ 5, &pri_ext
->feature_support
);
455 if (retval
!= ERROR_OK
)
457 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 9, &pri_ext
->suspend_cmd_support
);
458 if (retval
!= ERROR_OK
)
460 retval
= cfi_query_u16(bank
, 0, cfi_info
->pri_addr
+ 0xa, &pri_ext
->blk_status_reg_mask
);
461 if (retval
!= ERROR_OK
)
464 LOG_DEBUG("feature_support: 0x%" PRIx32
", suspend_cmd_support: 0x%x, blk_status_reg_mask: 0x%x",
465 pri_ext
->feature_support
,
466 pri_ext
->suspend_cmd_support
,
467 pri_ext
->blk_status_reg_mask
);
469 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 0xc, &pri_ext
->vcc_optimal
);
470 if (retval
!= ERROR_OK
)
472 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 0xd, &pri_ext
->vpp_optimal
);
473 if (retval
!= ERROR_OK
)
476 LOG_DEBUG("Vcc opt: %x.%x, Vpp opt: %u.%x",
477 (pri_ext
->vcc_optimal
& 0xf0) >> 4, pri_ext
->vcc_optimal
& 0x0f,
478 (pri_ext
->vpp_optimal
& 0xf0) >> 4, pri_ext
->vpp_optimal
& 0x0f);
480 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 0xe, &pri_ext
->num_protection_fields
);
481 if (retval
!= ERROR_OK
)
483 if (pri_ext
->num_protection_fields
!= 1)
485 LOG_WARNING("expected one protection register field, but found %i", pri_ext
->num_protection_fields
);
488 retval
= cfi_query_u16(bank
, 0, cfi_info
->pri_addr
+ 0xf, &pri_ext
->prot_reg_addr
);
489 if (retval
!= ERROR_OK
)
491 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 0x11, &pri_ext
->fact_prot_reg_size
);
492 if (retval
!= ERROR_OK
)
494 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 0x12, &pri_ext
->user_prot_reg_size
);
495 if (retval
!= ERROR_OK
)
498 LOG_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
);
503 static int cfi_read_spansion_pri_ext(struct flash_bank
*bank
)
506 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
507 struct cfi_spansion_pri_ext
*pri_ext
;
509 if (cfi_info
->pri_ext
)
510 free(cfi_info
->pri_ext
);
512 pri_ext
= malloc(sizeof(struct cfi_spansion_pri_ext
));
515 LOG_ERROR("Out of memory");
518 cfi_info
->pri_ext
= pri_ext
;
520 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 0, &pri_ext
->pri
[0]);
521 if (retval
!= ERROR_OK
)
523 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 1, &pri_ext
->pri
[1]);
524 if (retval
!= ERROR_OK
)
526 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 2, &pri_ext
->pri
[2]);
527 if (retval
!= ERROR_OK
)
530 if ((pri_ext
->pri
[0] != 'P') || (pri_ext
->pri
[1] != 'R') || (pri_ext
->pri
[2] != 'I'))
532 if ((retval
= cfi_send_command(bank
, 0xf0, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
536 LOG_ERROR("Could not read spansion bank information");
537 return ERROR_FLASH_BANK_INVALID
;
540 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 3, &pri_ext
->major_version
);
541 if (retval
!= ERROR_OK
)
543 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 4, &pri_ext
->minor_version
);
544 if (retval
!= ERROR_OK
)
547 LOG_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
);
549 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 5, &pri_ext
->SiliconRevision
);
550 if (retval
!= ERROR_OK
)
552 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 6, &pri_ext
->EraseSuspend
);
553 if (retval
!= ERROR_OK
)
555 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 7, &pri_ext
->BlkProt
);
556 if (retval
!= ERROR_OK
)
558 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 8, &pri_ext
->TmpBlkUnprotect
);
559 if (retval
!= ERROR_OK
)
561 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 9, &pri_ext
->BlkProtUnprot
);
562 if (retval
!= ERROR_OK
)
564 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 10, &pri_ext
->SimultaneousOps
);
565 if (retval
!= ERROR_OK
)
567 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 11, &pri_ext
->BurstMode
);
568 if (retval
!= ERROR_OK
)
570 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 12, &pri_ext
->PageMode
);
571 if (retval
!= ERROR_OK
)
573 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 13, &pri_ext
->VppMin
);
574 if (retval
!= ERROR_OK
)
576 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 14, &pri_ext
->VppMax
);
577 if (retval
!= ERROR_OK
)
579 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 15, &pri_ext
->TopBottom
);
580 if (retval
!= ERROR_OK
)
583 LOG_DEBUG("Silicon Revision: 0x%x, Erase Suspend: 0x%x, Block protect: 0x%x", pri_ext
->SiliconRevision
,
584 pri_ext
->EraseSuspend
, pri_ext
->BlkProt
);
586 LOG_DEBUG("Temporary Unprotect: 0x%x, Block Protect Scheme: 0x%x, Simultaneous Ops: 0x%x", pri_ext
->TmpBlkUnprotect
,
587 pri_ext
->BlkProtUnprot
, pri_ext
->SimultaneousOps
);
589 LOG_DEBUG("Burst Mode: 0x%x, Page Mode: 0x%x, ", pri_ext
->BurstMode
, pri_ext
->PageMode
);
592 LOG_DEBUG("Vpp min: %u.%x, Vpp max: %u.%x",
593 (pri_ext
->VppMin
& 0xf0) >> 4, pri_ext
->VppMin
& 0x0f,
594 (pri_ext
->VppMax
& 0xf0) >> 4, pri_ext
->VppMax
& 0x0f);
596 LOG_DEBUG("WP# protection 0x%x", pri_ext
->TopBottom
);
598 /* default values for implementation specific workarounds */
599 pri_ext
->_unlock1
= cfi_unlock_addresses
[CFI_UNLOCK_555_2AA
].unlock1
;
600 pri_ext
->_unlock2
= cfi_unlock_addresses
[CFI_UNLOCK_555_2AA
].unlock2
;
601 pri_ext
->_reversed_geometry
= 0;
606 static int cfi_read_atmel_pri_ext(struct flash_bank
*bank
)
609 struct cfi_atmel_pri_ext atmel_pri_ext
;
610 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
611 struct cfi_spansion_pri_ext
*pri_ext
;
613 if (cfi_info
->pri_ext
)
614 free(cfi_info
->pri_ext
);
616 pri_ext
= malloc(sizeof(struct cfi_spansion_pri_ext
));
619 LOG_ERROR("Out of memory");
623 /* ATMEL devices use the same CFI primary command set (0x2) as AMD/Spansion,
624 * but a different primary extended query table.
625 * We read the atmel table, and prepare a valid AMD/Spansion query table.
628 memset(pri_ext
, 0, sizeof(struct cfi_spansion_pri_ext
));
630 cfi_info
->pri_ext
= pri_ext
;
632 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 0, &atmel_pri_ext
.pri
[0]);
633 if (retval
!= ERROR_OK
)
635 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 1, &atmel_pri_ext
.pri
[1]);
636 if (retval
!= ERROR_OK
)
638 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 2, &atmel_pri_ext
.pri
[2]);
639 if (retval
!= ERROR_OK
)
642 if ((atmel_pri_ext
.pri
[0] != 'P') || (atmel_pri_ext
.pri
[1] != 'R') || (atmel_pri_ext
.pri
[2] != 'I'))
644 if ((retval
= cfi_send_command(bank
, 0xf0, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
648 LOG_ERROR("Could not read atmel bank information");
649 return ERROR_FLASH_BANK_INVALID
;
652 pri_ext
->pri
[0] = atmel_pri_ext
.pri
[0];
653 pri_ext
->pri
[1] = atmel_pri_ext
.pri
[1];
654 pri_ext
->pri
[2] = atmel_pri_ext
.pri
[2];
656 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 3, &atmel_pri_ext
.major_version
);
657 if (retval
!= ERROR_OK
)
659 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 4, &atmel_pri_ext
.minor_version
);
660 if (retval
!= ERROR_OK
)
663 LOG_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
);
665 pri_ext
->major_version
= atmel_pri_ext
.major_version
;
666 pri_ext
->minor_version
= atmel_pri_ext
.minor_version
;
668 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 5, &atmel_pri_ext
.features
);
669 if (retval
!= ERROR_OK
)
671 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 6, &atmel_pri_ext
.bottom_boot
);
672 if (retval
!= ERROR_OK
)
674 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 7, &atmel_pri_ext
.burst_mode
);
675 if (retval
!= ERROR_OK
)
677 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 8, &atmel_pri_ext
.page_mode
);
678 if (retval
!= ERROR_OK
)
681 LOG_DEBUG("features: 0x%2.2x, bottom_boot: 0x%2.2x, burst_mode: 0x%2.2x, page_mode: 0x%2.2x",
682 atmel_pri_ext
.features
, atmel_pri_ext
.bottom_boot
, atmel_pri_ext
.burst_mode
, atmel_pri_ext
.page_mode
);
684 if (atmel_pri_ext
.features
& 0x02)
685 pri_ext
->EraseSuspend
= 2;
687 if (atmel_pri_ext
.bottom_boot
)
688 pri_ext
->TopBottom
= 2;
690 pri_ext
->TopBottom
= 3;
692 pri_ext
->_unlock1
= cfi_unlock_addresses
[CFI_UNLOCK_555_2AA
].unlock1
;
693 pri_ext
->_unlock2
= cfi_unlock_addresses
[CFI_UNLOCK_555_2AA
].unlock2
;
698 static int cfi_read_0002_pri_ext(struct flash_bank
*bank
)
700 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
702 if (cfi_info
->manufacturer
== CFI_MFR_ATMEL
)
704 return cfi_read_atmel_pri_ext(bank
);
708 return cfi_read_spansion_pri_ext(bank
);
712 static int cfi_spansion_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
715 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
716 struct cfi_spansion_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
718 printed
= snprintf(buf
, buf_size
, "\nSpansion primary algorithm extend information:\n");
722 printed
= snprintf(buf
, buf_size
, "pri: '%c%c%c', version: %c.%c\n", pri_ext
->pri
[0],
723 pri_ext
->pri
[1], pri_ext
->pri
[2],
724 pri_ext
->major_version
, pri_ext
->minor_version
);
728 printed
= snprintf(buf
, buf_size
, "Silicon Rev.: 0x%x, Address Sensitive unlock: 0x%x\n",
729 (pri_ext
->SiliconRevision
) >> 2,
730 (pri_ext
->SiliconRevision
) & 0x03);
734 printed
= snprintf(buf
, buf_size
, "Erase Suspend: 0x%x, Sector Protect: 0x%x\n",
735 pri_ext
->EraseSuspend
,
740 printed
= snprintf(buf
, buf_size
, "VppMin: %u.%x, VppMax: %u.%x\n",
741 (pri_ext
->VppMin
& 0xf0) >> 4, pri_ext
->VppMin
& 0x0f,
742 (pri_ext
->VppMax
& 0xf0) >> 4, pri_ext
->VppMax
& 0x0f);
747 static int cfi_intel_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
750 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
751 struct cfi_intel_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
753 printed
= snprintf(buf
, buf_size
, "\nintel primary algorithm extend information:\n");
757 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
);
761 printed
= snprintf(buf
, buf_size
, "feature_support: 0x%" PRIx32
", 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
);
765 printed
= snprintf(buf
, buf_size
, "Vcc opt: %x.%x, Vpp opt: %u.%x\n",
766 (pri_ext
->vcc_optimal
& 0xf0) >> 4, pri_ext
->vcc_optimal
& 0x0f,
767 (pri_ext
->vpp_optimal
& 0xf0) >> 4, pri_ext
->vpp_optimal
& 0x0f);
771 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
);
776 /* flash_bank cfi <base> <size> <chip_width> <bus_width> <target#> [options]
778 FLASH_BANK_COMMAND_HANDLER(cfi_flash_bank_command
)
780 struct cfi_flash_bank
*cfi_info
;
784 LOG_WARNING("incomplete flash_bank cfi configuration");
785 return ERROR_FLASH_BANK_INVALID
;
789 * - not exceed max value;
791 * - be equal to a power of 2.
792 * bus must be wide enought to hold one chip */
793 if ((bank
->chip_width
> CFI_MAX_CHIP_WIDTH
)
794 || (bank
->bus_width
> CFI_MAX_BUS_WIDTH
)
795 || (bank
->chip_width
== 0)
796 || (bank
->bus_width
== 0)
797 || (bank
->chip_width
& (bank
->chip_width
- 1))
798 || (bank
->bus_width
& (bank
->bus_width
- 1))
799 || (bank
->chip_width
> bank
->bus_width
))
801 LOG_ERROR("chip and bus width have to specified in bytes");
802 return ERROR_FLASH_BANK_INVALID
;
805 cfi_info
= malloc(sizeof(struct cfi_flash_bank
));
806 cfi_info
->probed
= 0;
807 cfi_info
->erase_region_info
= 0;
808 cfi_info
->pri_ext
= NULL
;
809 bank
->driver_priv
= cfi_info
;
811 cfi_info
->write_algorithm
= NULL
;
813 cfi_info
->x16_as_x8
= 0;
814 cfi_info
->jedec_probe
= 0;
815 cfi_info
->not_cfi
= 0;
817 for (unsigned i
= 6; i
< CMD_ARGC
; i
++)
819 if (strcmp(CMD_ARGV
[i
], "x16_as_x8") == 0)
821 cfi_info
->x16_as_x8
= 1;
823 else if (strcmp(CMD_ARGV
[i
], "jedec_probe") == 0)
825 cfi_info
->jedec_probe
= 1;
829 cfi_info
->write_algorithm
= NULL
;
831 /* bank wasn't probed yet */
832 cfi_info
->qry
[0] = 0xff;
837 static int cfi_intel_erase(struct flash_bank
*bank
, int first
, int last
)
840 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
843 cfi_intel_clear_status_register(bank
);
845 for (i
= first
; i
<= last
; i
++)
847 if ((retval
= cfi_send_command(bank
, 0x20, flash_address(bank
, i
, 0x0))) != ERROR_OK
)
852 if ((retval
= cfi_send_command(bank
, 0xd0, flash_address(bank
, i
, 0x0))) != ERROR_OK
)
858 retval
= cfi_intel_wait_status_busy(bank
, 1000 * (1 << cfi_info
->block_erase_timeout_typ
), &status
);
859 if (retval
!= ERROR_OK
)
863 bank
->sectors
[i
].is_erased
= 1;
866 if ((retval
= cfi_send_command(bank
, 0xff, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
871 LOG_ERROR("couldn't erase block %i of flash bank at base 0x%" PRIx32
, i
, bank
->base
);
872 return ERROR_FLASH_OPERATION_FAILED
;
876 return cfi_send_command(bank
, 0xff, flash_address(bank
, 0, 0x0));
879 static int cfi_spansion_erase(struct flash_bank
*bank
, int first
, int last
)
882 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
883 struct cfi_spansion_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
886 for (i
= first
; i
<= last
; i
++)
888 if ((retval
= cfi_send_command(bank
, 0xaa, flash_address(bank
, 0, pri_ext
->_unlock1
))) != ERROR_OK
)
893 if ((retval
= cfi_send_command(bank
, 0x55, flash_address(bank
, 0, pri_ext
->_unlock2
))) != ERROR_OK
)
898 if ((retval
= cfi_send_command(bank
, 0x80, flash_address(bank
, 0, pri_ext
->_unlock1
))) != ERROR_OK
)
903 if ((retval
= cfi_send_command(bank
, 0xaa, flash_address(bank
, 0, pri_ext
->_unlock1
))) != ERROR_OK
)
908 if ((retval
= cfi_send_command(bank
, 0x55, flash_address(bank
, 0, pri_ext
->_unlock2
))) != ERROR_OK
)
913 if ((retval
= cfi_send_command(bank
, 0x30, flash_address(bank
, i
, 0x0))) != ERROR_OK
)
918 if (cfi_spansion_wait_status_busy(bank
, 1000 * (1 << cfi_info
->block_erase_timeout_typ
)) == ERROR_OK
)
919 bank
->sectors
[i
].is_erased
= 1;
922 if ((retval
= cfi_send_command(bank
, 0xf0, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
927 LOG_ERROR("couldn't erase block %i of flash bank at base 0x%" PRIx32
, i
, bank
->base
);
928 return ERROR_FLASH_OPERATION_FAILED
;
932 return cfi_send_command(bank
, 0xf0, flash_address(bank
, 0, 0x0));
935 static int cfi_erase(struct flash_bank
*bank
, int first
, int last
)
937 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
939 if (bank
->target
->state
!= TARGET_HALTED
)
941 LOG_ERROR("Target not halted");
942 return ERROR_TARGET_NOT_HALTED
;
945 if ((first
< 0) || (last
< first
) || (last
>= bank
->num_sectors
))
947 return ERROR_FLASH_SECTOR_INVALID
;
950 if (cfi_info
->qry
[0] != 'Q')
951 return ERROR_FLASH_BANK_NOT_PROBED
;
953 switch (cfi_info
->pri_id
)
957 return cfi_intel_erase(bank
, first
, last
);
960 return cfi_spansion_erase(bank
, first
, last
);
963 LOG_ERROR("cfi primary command set %i unsupported", cfi_info
->pri_id
);
970 static int cfi_intel_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
973 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
974 struct cfi_intel_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
978 /* if the device supports neither legacy lock/unlock (bit 3) nor
979 * instant individual block locking (bit 5).
981 if (!(pri_ext
->feature_support
& 0x28))
983 LOG_ERROR("lock/unlock not supported on flash");
984 return ERROR_FLASH_OPERATION_FAILED
;
987 cfi_intel_clear_status_register(bank
);
989 for (i
= first
; i
<= last
; i
++)
991 if ((retval
= cfi_send_command(bank
, 0x60, flash_address(bank
, i
, 0x0))) != ERROR_OK
)
997 if ((retval
= cfi_send_command(bank
, 0x01, flash_address(bank
, i
, 0x0))) != ERROR_OK
)
1001 bank
->sectors
[i
].is_protected
= 1;
1005 if ((retval
= cfi_send_command(bank
, 0xd0, flash_address(bank
, i
, 0x0))) != ERROR_OK
)
1009 bank
->sectors
[i
].is_protected
= 0;
1012 /* instant individual block locking doesn't require reading of the status register */
1013 if (!(pri_ext
->feature_support
& 0x20))
1015 /* Clear lock bits operation may take up to 1.4s */
1017 retval
= cfi_intel_wait_status_busy(bank
, 1400, &status
);
1018 if (retval
!= ERROR_OK
)
1023 uint8_t block_status
;
1024 /* read block lock bit, to verify status */
1025 if ((retval
= cfi_send_command(bank
, 0x90, flash_address(bank
, 0, 0x55))) != ERROR_OK
)
1029 retval
= cfi_get_u8(bank
, i
, 0x2, &block_status
);
1030 if (retval
!= ERROR_OK
)
1033 if ((block_status
& 0x1) != set
)
1035 LOG_ERROR("couldn't change block lock status (set = %i, block_status = 0x%2.2x)", set
, block_status
);
1036 if ((retval
= cfi_send_command(bank
, 0x70, flash_address(bank
, 0, 0x55))) != ERROR_OK
)
1041 retval
= cfi_intel_wait_status_busy(bank
, 10, &status
);
1042 if (retval
!= ERROR_OK
)
1046 return ERROR_FLASH_OPERATION_FAILED
;
1056 /* if the device doesn't support individual block lock bits set/clear,
1057 * all blocks have been unlocked in parallel, so we set those that should be protected
1059 if ((!set
) && (!(pri_ext
->feature_support
& 0x20)))
1061 /* FIX!!! this code path is broken!!!
1063 * The correct approach is:
1065 * 1. read out current protection status
1067 * 2. override read out protection status w/unprotected.
1069 * 3. re-protect what should be protected.
1072 for (i
= 0; i
< bank
->num_sectors
; i
++)
1074 if (bank
->sectors
[i
].is_protected
== 1)
1076 cfi_intel_clear_status_register(bank
);
1078 if ((retval
= cfi_send_command(bank
, 0x60, flash_address(bank
, i
, 0x0))) != ERROR_OK
)
1083 if ((retval
= cfi_send_command(bank
, 0x01, flash_address(bank
, i
, 0x0))) != ERROR_OK
)
1089 retval
= cfi_intel_wait_status_busy(bank
, 100, &status
);
1090 if (retval
!= ERROR_OK
)
1096 return cfi_send_command(bank
, 0xff, flash_address(bank
, 0, 0x0));
1099 static int cfi_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
1101 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
1103 if (bank
->target
->state
!= TARGET_HALTED
)
1105 LOG_ERROR("Target not halted");
1106 return ERROR_TARGET_NOT_HALTED
;
1109 if ((first
< 0) || (last
< first
) || (last
>= bank
->num_sectors
))
1111 LOG_ERROR("Invalid sector range");
1112 return ERROR_FLASH_SECTOR_INVALID
;
1115 if (cfi_info
->qry
[0] != 'Q')
1116 return ERROR_FLASH_BANK_NOT_PROBED
;
1118 switch (cfi_info
->pri_id
)
1122 return cfi_intel_protect(bank
, set
, first
, last
);
1125 LOG_ERROR("protect: cfi primary command set %i unsupported", cfi_info
->pri_id
);
1130 /* Convert code image to target endian */
1131 /* FIXME create general block conversion fcts in target.c?) */
1132 static void cfi_fix_code_endian(struct target
*target
, uint8_t *dest
, const uint32_t *src
, uint32_t count
)
1135 for (i
= 0; i
< count
; i
++)
1137 target_buffer_set_u32(target
, dest
, *src
);
1143 static uint32_t cfi_command_val(struct flash_bank
*bank
, uint8_t cmd
)
1145 struct target
*target
= bank
->target
;
1147 uint8_t buf
[CFI_MAX_BUS_WIDTH
];
1148 cfi_command(bank
, cmd
, buf
);
1149 switch (bank
->bus_width
)
1155 return target_buffer_get_u16(target
, buf
);
1158 return target_buffer_get_u32(target
, buf
);
1161 LOG_ERROR("Unsupported bank buswidth %d, can't do block memory writes", bank
->bus_width
);
1166 static int cfi_intel_write_block(struct flash_bank
*bank
, uint8_t *buffer
, uint32_t address
, uint32_t count
)
1168 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
1169 struct target
*target
= bank
->target
;
1170 struct reg_param reg_params
[7];
1171 struct arm_algorithm armv4_5_info
;
1172 struct working_area
*source
;
1173 uint32_t buffer_size
= 32768;
1174 uint32_t write_command_val
, busy_pattern_val
, error_pattern_val
;
1176 /* algorithm register usage:
1177 * r0: source address (in RAM)
1178 * r1: target address (in Flash)
1180 * r3: flash write command
1181 * r4: status byte (returned to host)
1182 * r5: busy test pattern
1183 * r6: error test pattern
1186 static const uint32_t word_32_code
[] = {
1187 0xe4904004, /* loop: ldr r4, [r0], #4 */
1188 0xe5813000, /* str r3, [r1] */
1189 0xe5814000, /* str r4, [r1] */
1190 0xe5914000, /* busy: ldr r4, [r1] */
1191 0xe0047005, /* and r7, r4, r5 */
1192 0xe1570005, /* cmp r7, r5 */
1193 0x1afffffb, /* bne busy */
1194 0xe1140006, /* tst r4, r6 */
1195 0x1a000003, /* bne done */
1196 0xe2522001, /* subs r2, r2, #1 */
1197 0x0a000001, /* beq done */
1198 0xe2811004, /* add r1, r1 #4 */
1199 0xeafffff2, /* b loop */
1200 0xeafffffe /* done: b -2 */
1203 static const uint32_t word_16_code
[] = {
1204 0xe0d040b2, /* loop: ldrh r4, [r0], #2 */
1205 0xe1c130b0, /* strh r3, [r1] */
1206 0xe1c140b0, /* strh r4, [r1] */
1207 0xe1d140b0, /* busy ldrh r4, [r1] */
1208 0xe0047005, /* and r7, r4, r5 */
1209 0xe1570005, /* cmp r7, r5 */
1210 0x1afffffb, /* bne busy */
1211 0xe1140006, /* tst r4, r6 */
1212 0x1a000003, /* bne done */
1213 0xe2522001, /* subs r2, r2, #1 */
1214 0x0a000001, /* beq done */
1215 0xe2811002, /* add r1, r1 #2 */
1216 0xeafffff2, /* b loop */
1217 0xeafffffe /* done: b -2 */
1220 static const uint32_t word_8_code
[] = {
1221 0xe4d04001, /* loop: ldrb r4, [r0], #1 */
1222 0xe5c13000, /* strb r3, [r1] */
1223 0xe5c14000, /* strb r4, [r1] */
1224 0xe5d14000, /* busy ldrb r4, [r1] */
1225 0xe0047005, /* and r7, r4, r5 */
1226 0xe1570005, /* cmp r7, r5 */
1227 0x1afffffb, /* bne busy */
1228 0xe1140006, /* tst r4, r6 */
1229 0x1a000003, /* bne done */
1230 0xe2522001, /* subs r2, r2, #1 */
1231 0x0a000001, /* beq done */
1232 0xe2811001, /* add r1, r1 #1 */
1233 0xeafffff2, /* b loop */
1234 0xeafffffe /* done: b -2 */
1236 uint8_t target_code
[4*CFI_MAX_INTEL_CODESIZE
];
1237 const uint32_t *target_code_src
;
1238 uint32_t target_code_size
;
1239 int retval
= ERROR_OK
;
1242 cfi_intel_clear_status_register(bank
);
1244 armv4_5_info
.common_magic
= ARM_COMMON_MAGIC
;
1245 armv4_5_info
.core_mode
= ARM_MODE_SVC
;
1246 armv4_5_info
.core_state
= ARM_STATE_ARM
;
1248 /* If we are setting up the write_algorith, we need target_code_src */
1249 /* if not we only need target_code_size. */
1251 /* However, we don't want to create multiple code paths, so we */
1252 /* do the unecessary evaluation of target_code_src, which the */
1253 /* compiler will probably nicely optimize away if not needed */
1255 /* prepare algorithm code for target endian */
1256 switch (bank
->bus_width
)
1259 target_code_src
= word_8_code
;
1260 target_code_size
= sizeof(word_8_code
);
1263 target_code_src
= word_16_code
;
1264 target_code_size
= sizeof(word_16_code
);
1267 target_code_src
= word_32_code
;
1268 target_code_size
= sizeof(word_32_code
);
1271 LOG_ERROR("Unsupported bank buswidth %d, can't do block memory writes", bank
->bus_width
);
1272 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1275 /* flash write code */
1276 if (!cfi_info
->write_algorithm
)
1278 if (target_code_size
> sizeof(target_code
))
1280 LOG_WARNING("Internal error - target code buffer to small. Increase CFI_MAX_INTEL_CODESIZE and recompile.");
1281 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1283 cfi_fix_code_endian(target
, target_code
, target_code_src
, target_code_size
/ 4);
1285 /* Get memory for block write handler */
1286 retval
= target_alloc_working_area(target
, target_code_size
, &cfi_info
->write_algorithm
);
1287 if (retval
!= ERROR_OK
)
1289 LOG_WARNING("No working area available, can't do block memory writes");
1290 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1293 /* write algorithm code to working area */
1294 retval
= target_write_buffer(target
, cfi_info
->write_algorithm
->address
, target_code_size
, target_code
);
1295 if (retval
!= ERROR_OK
)
1297 LOG_ERROR("Unable to write block write code to target");
1302 /* Get a workspace buffer for the data to flash starting with 32k size.
1303 Half size until buffer would be smaller 256 Bytem then fail back */
1304 /* FIXME Why 256 bytes, why not 32 bytes (smallest flash write page */
1305 while (target_alloc_working_area_try(target
, buffer_size
, &source
) != ERROR_OK
)
1308 if (buffer_size
<= 256)
1310 LOG_WARNING("no large enough working area available, can't do block memory writes");
1311 retval
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1316 /* setup algo registers */
1317 init_reg_param(®_params
[0], "r0", 32, PARAM_OUT
);
1318 init_reg_param(®_params
[1], "r1", 32, PARAM_OUT
);
1319 init_reg_param(®_params
[2], "r2", 32, PARAM_OUT
);
1320 init_reg_param(®_params
[3], "r3", 32, PARAM_OUT
);
1321 init_reg_param(®_params
[4], "r4", 32, PARAM_IN
);
1322 init_reg_param(®_params
[5], "r5", 32, PARAM_OUT
);
1323 init_reg_param(®_params
[6], "r6", 32, PARAM_OUT
);
1325 /* prepare command and status register patterns */
1326 write_command_val
= cfi_command_val(bank
, 0x40);
1327 busy_pattern_val
= cfi_command_val(bank
, 0x80);
1328 error_pattern_val
= cfi_command_val(bank
, 0x7e);
1330 LOG_DEBUG("Using target buffer at 0x%08" PRIx32
" and of size 0x%04" PRIx32
, source
->address
, buffer_size
);
1332 /* Programming main loop */
1335 uint32_t thisrun_count
= (count
> buffer_size
) ? buffer_size
: count
;
1338 if ((retval
= target_write_buffer(target
, source
->address
, thisrun_count
, buffer
)) != ERROR_OK
)
1343 buf_set_u32(reg_params
[0].value
, 0, 32, source
->address
);
1344 buf_set_u32(reg_params
[1].value
, 0, 32, address
);
1345 buf_set_u32(reg_params
[2].value
, 0, 32, thisrun_count
/ bank
->bus_width
);
1347 buf_set_u32(reg_params
[3].value
, 0, 32, write_command_val
);
1348 buf_set_u32(reg_params
[5].value
, 0, 32, busy_pattern_val
);
1349 buf_set_u32(reg_params
[6].value
, 0, 32, error_pattern_val
);
1351 LOG_DEBUG("Write 0x%04" PRIx32
" bytes to flash at 0x%08" PRIx32
, thisrun_count
, address
);
1353 /* Execute algorithm, assume breakpoint for last instruction */
1354 retval
= target_run_algorithm(target
, 0, NULL
, 7, reg_params
,
1355 cfi_info
->write_algorithm
->address
,
1356 cfi_info
->write_algorithm
->address
+ target_code_size
- sizeof(uint32_t),
1357 10000, /* 10s should be enough for max. 32k of data */
1360 /* On failure try a fall back to direct word writes */
1361 if (retval
!= ERROR_OK
)
1363 cfi_intel_clear_status_register(bank
);
1364 LOG_ERROR("Execution of flash algorythm failed. Can't fall back. Please report.");
1365 retval
= ERROR_FLASH_OPERATION_FAILED
;
1366 /* retval = ERROR_TARGET_RESOURCE_NOT_AVAILABLE; */
1367 /* FIXME To allow fall back or recovery, we must save the actual status
1368 somewhere, so that a higher level code can start recovery. */
1372 /* Check return value from algo code */
1373 wsm_error
= buf_get_u32(reg_params
[4].value
, 0, 32) & error_pattern_val
;
1376 /* read status register (outputs debug inforation) */
1378 cfi_intel_wait_status_busy(bank
, 100, &status
);
1379 cfi_intel_clear_status_register(bank
);
1380 retval
= ERROR_FLASH_OPERATION_FAILED
;
1384 buffer
+= thisrun_count
;
1385 address
+= thisrun_count
;
1386 count
-= thisrun_count
;
1391 /* free up resources */
1394 target_free_working_area(target
, source
);
1396 if (cfi_info
->write_algorithm
)
1398 target_free_working_area(target
, cfi_info
->write_algorithm
);
1399 cfi_info
->write_algorithm
= NULL
;
1402 destroy_reg_param(®_params
[0]);
1403 destroy_reg_param(®_params
[1]);
1404 destroy_reg_param(®_params
[2]);
1405 destroy_reg_param(®_params
[3]);
1406 destroy_reg_param(®_params
[4]);
1407 destroy_reg_param(®_params
[5]);
1408 destroy_reg_param(®_params
[6]);
1413 static int cfi_spansion_write_block(struct flash_bank
*bank
, uint8_t *buffer
, uint32_t address
, uint32_t count
)
1415 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
1416 struct cfi_spansion_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
1417 struct target
*target
= bank
->target
;
1418 struct reg_param reg_params
[10];
1419 struct arm_algorithm armv4_5_info
;
1420 struct working_area
*source
;
1421 uint32_t buffer_size
= 32768;
1423 int retval
= ERROR_OK
;
1425 /* input parameters - */
1426 /* R0 = source address */
1427 /* R1 = destination address */
1428 /* R2 = number of writes */
1429 /* R3 = flash write command */
1430 /* R4 = constant to mask DQ7 bits (also used for Dq5 with shift) */
1431 /* output parameters - */
1432 /* R5 = 0x80 ok 0x00 bad */
1433 /* temp registers - */
1434 /* R6 = value read from flash to test status */
1435 /* R7 = holding register */
1436 /* unlock registers - */
1437 /* R8 = unlock1_addr */
1438 /* R9 = unlock1_cmd */
1439 /* R10 = unlock2_addr */
1440 /* R11 = unlock2_cmd */
1442 static const uint32_t word_32_code
[] = {
1443 /* 00008100 <sp_32_code>: */
1444 0xe4905004, /* ldr r5, [r0], #4 */
1445 0xe5889000, /* str r9, [r8] */
1446 0xe58ab000, /* str r11, [r10] */
1447 0xe5883000, /* str r3, [r8] */
1448 0xe5815000, /* str r5, [r1] */
1449 0xe1a00000, /* nop */
1451 /* 00008110 <sp_32_busy>: */
1452 0xe5916000, /* ldr r6, [r1] */
1453 0xe0257006, /* eor r7, r5, r6 */
1454 0xe0147007, /* ands r7, r4, r7 */
1455 0x0a000007, /* beq 8140 <sp_32_cont> ; b if DQ7 == Data7 */
1456 0xe0166124, /* ands r6, r6, r4, lsr #2 */
1457 0x0afffff9, /* beq 8110 <sp_32_busy> ; b if DQ5 low */
1458 0xe5916000, /* ldr r6, [r1] */
1459 0xe0257006, /* eor r7, r5, r6 */
1460 0xe0147007, /* ands r7, r4, r7 */
1461 0x0a000001, /* beq 8140 <sp_32_cont> ; b if DQ7 == Data7 */
1462 0xe3a05000, /* mov r5, #0 ; 0x0 - return 0x00, error */
1463 0x1a000004, /* bne 8154 <sp_32_done> */
1465 /* 00008140 <sp_32_cont>: */
1466 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
1467 0x03a05080, /* moveq r5, #128 ; 0x80 */
1468 0x0a000001, /* beq 8154 <sp_32_done> */
1469 0xe2811004, /* add r1, r1, #4 ; 0x4 */
1470 0xeaffffe8, /* b 8100 <sp_32_code> */
1472 /* 00008154 <sp_32_done>: */
1473 0xeafffffe /* b 8154 <sp_32_done> */
1476 static const uint32_t word_16_code
[] = {
1477 /* 00008158 <sp_16_code>: */
1478 0xe0d050b2, /* ldrh r5, [r0], #2 */
1479 0xe1c890b0, /* strh r9, [r8] */
1480 0xe1cab0b0, /* strh r11, [r10] */
1481 0xe1c830b0, /* strh r3, [r8] */
1482 0xe1c150b0, /* strh r5, [r1] */
1483 0xe1a00000, /* nop (mov r0,r0) */
1485 /* 00008168 <sp_16_busy>: */
1486 0xe1d160b0, /* ldrh r6, [r1] */
1487 0xe0257006, /* eor r7, r5, r6 */
1488 0xe0147007, /* ands r7, r4, r7 */
1489 0x0a000007, /* beq 8198 <sp_16_cont> */
1490 0xe0166124, /* ands r6, r6, r4, lsr #2 */
1491 0x0afffff9, /* beq 8168 <sp_16_busy> */
1492 0xe1d160b0, /* ldrh r6, [r1] */
1493 0xe0257006, /* eor r7, r5, r6 */
1494 0xe0147007, /* ands r7, r4, r7 */
1495 0x0a000001, /* beq 8198 <sp_16_cont> */
1496 0xe3a05000, /* mov r5, #0 ; 0x0 */
1497 0x1a000004, /* bne 81ac <sp_16_done> */
1499 /* 00008198 <sp_16_cont>: */
1500 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
1501 0x03a05080, /* moveq r5, #128 ; 0x80 */
1502 0x0a000001, /* beq 81ac <sp_16_done> */
1503 0xe2811002, /* add r1, r1, #2 ; 0x2 */
1504 0xeaffffe8, /* b 8158 <sp_16_code> */
1506 /* 000081ac <sp_16_done>: */
1507 0xeafffffe /* b 81ac <sp_16_done> */
1510 static const uint32_t word_16_code_dq7only
[] = {
1512 0xe0d050b2, /* ldrh r5, [r0], #2 */
1513 0xe1c890b0, /* strh r9, [r8] */
1514 0xe1cab0b0, /* strh r11, [r10] */
1515 0xe1c830b0, /* strh r3, [r8] */
1516 0xe1c150b0, /* strh r5, [r1] */
1517 0xe1a00000, /* nop (mov r0,r0) */
1520 0xe1d160b0, /* ldrh r6, [r1] */
1521 0xe0257006, /* eor r7, r5, r6 */
1522 0xe2177080, /* ands r7, #0x80 */
1523 0x1afffffb, /* bne 8168 <sp_16_busy> */
1525 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
1526 0x03a05080, /* moveq r5, #128 ; 0x80 */
1527 0x0a000001, /* beq 81ac <sp_16_done> */
1528 0xe2811002, /* add r1, r1, #2 ; 0x2 */
1529 0xeafffff0, /* b 8158 <sp_16_code> */
1531 /* 000081ac <sp_16_done>: */
1532 0xeafffffe /* b 81ac <sp_16_done> */
1535 static const uint32_t word_8_code
[] = {
1536 /* 000081b0 <sp_16_code_end>: */
1537 0xe4d05001, /* ldrb r5, [r0], #1 */
1538 0xe5c89000, /* strb r9, [r8] */
1539 0xe5cab000, /* strb r11, [r10] */
1540 0xe5c83000, /* strb r3, [r8] */
1541 0xe5c15000, /* strb r5, [r1] */
1542 0xe1a00000, /* nop (mov r0,r0) */
1544 /* 000081c0 <sp_8_busy>: */
1545 0xe5d16000, /* ldrb r6, [r1] */
1546 0xe0257006, /* eor r7, r5, r6 */
1547 0xe0147007, /* ands r7, r4, r7 */
1548 0x0a000007, /* beq 81f0 <sp_8_cont> */
1549 0xe0166124, /* ands r6, r6, r4, lsr #2 */
1550 0x0afffff9, /* beq 81c0 <sp_8_busy> */
1551 0xe5d16000, /* ldrb r6, [r1] */
1552 0xe0257006, /* eor r7, r5, r6 */
1553 0xe0147007, /* ands r7, r4, r7 */
1554 0x0a000001, /* beq 81f0 <sp_8_cont> */
1555 0xe3a05000, /* mov r5, #0 ; 0x0 */
1556 0x1a000004, /* bne 8204 <sp_8_done> */
1558 /* 000081f0 <sp_8_cont>: */
1559 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
1560 0x03a05080, /* moveq r5, #128 ; 0x80 */
1561 0x0a000001, /* beq 8204 <sp_8_done> */
1562 0xe2811001, /* add r1, r1, #1 ; 0x1 */
1563 0xeaffffe8, /* b 81b0 <sp_16_code_end> */
1565 /* 00008204 <sp_8_done>: */
1566 0xeafffffe /* b 8204 <sp_8_done> */
1569 armv4_5_info
.common_magic
= ARM_COMMON_MAGIC
;
1570 armv4_5_info
.core_mode
= ARM_MODE_SVC
;
1571 armv4_5_info
.core_state
= ARM_STATE_ARM
;
1573 int target_code_size
;
1574 const uint32_t *target_code_src
;
1576 switch (bank
->bus_width
)
1579 target_code_src
= word_8_code
;
1580 target_code_size
= sizeof(word_8_code
);
1583 /* Check for DQ5 support */
1584 if( cfi_info
->status_poll_mask
& (1 << 5) )
1586 target_code_src
= word_16_code
;
1587 target_code_size
= sizeof(word_16_code
);
1591 /* No DQ5 support. Use DQ7 DATA# polling only. */
1592 target_code_src
= word_16_code_dq7only
;
1593 target_code_size
= sizeof(word_16_code_dq7only
);
1597 target_code_src
= word_32_code
;
1598 target_code_size
= sizeof(word_32_code
);
1601 LOG_ERROR("Unsupported bank buswidth %d, can't do block memory writes", bank
->bus_width
);
1602 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1605 /* flash write code */
1606 if (!cfi_info
->write_algorithm
)
1608 uint8_t *target_code
;
1610 /* convert bus-width dependent algorithm code to correct endiannes */
1611 target_code
= malloc(target_code_size
);
1612 if (target_code
== NULL
)
1614 LOG_ERROR("Out of memory");
1617 cfi_fix_code_endian(target
, target_code
, target_code_src
, target_code_size
/ 4);
1619 /* allocate working area */
1620 retval
= target_alloc_working_area(target
, target_code_size
,
1621 &cfi_info
->write_algorithm
);
1622 if (retval
!= ERROR_OK
)
1628 /* write algorithm code to working area */
1629 if ((retval
= target_write_buffer(target
, cfi_info
->write_algorithm
->address
,
1630 target_code_size
, target_code
)) != ERROR_OK
)
1638 /* the following code still assumes target code is fixed 24*4 bytes */
1640 while (target_alloc_working_area_try(target
, buffer_size
, &source
) != ERROR_OK
)
1643 if (buffer_size
<= 256)
1645 /* if we already allocated the writing code, but failed to get a buffer, free the algorithm */
1646 if (cfi_info
->write_algorithm
)
1647 target_free_working_area(target
, cfi_info
->write_algorithm
);
1649 LOG_WARNING("not enough working area available, can't do block memory writes");
1650 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1654 init_reg_param(®_params
[0], "r0", 32, PARAM_OUT
);
1655 init_reg_param(®_params
[1], "r1", 32, PARAM_OUT
);
1656 init_reg_param(®_params
[2], "r2", 32, PARAM_OUT
);
1657 init_reg_param(®_params
[3], "r3", 32, PARAM_OUT
);
1658 init_reg_param(®_params
[4], "r4", 32, PARAM_OUT
);
1659 init_reg_param(®_params
[5], "r5", 32, PARAM_IN
);
1660 init_reg_param(®_params
[6], "r8", 32, PARAM_OUT
);
1661 init_reg_param(®_params
[7], "r9", 32, PARAM_OUT
);
1662 init_reg_param(®_params
[8], "r10", 32, PARAM_OUT
);
1663 init_reg_param(®_params
[9], "r11", 32, PARAM_OUT
);
1667 uint32_t thisrun_count
= (count
> buffer_size
) ? buffer_size
: count
;
1669 retval
= target_write_buffer(target
, source
->address
, thisrun_count
, buffer
);
1670 if (retval
!= ERROR_OK
)
1675 buf_set_u32(reg_params
[0].value
, 0, 32, source
->address
);
1676 buf_set_u32(reg_params
[1].value
, 0, 32, address
);
1677 buf_set_u32(reg_params
[2].value
, 0, 32, thisrun_count
/ bank
->bus_width
);
1678 buf_set_u32(reg_params
[3].value
, 0, 32, cfi_command_val(bank
, 0xA0));
1679 buf_set_u32(reg_params
[4].value
, 0, 32, cfi_command_val(bank
, 0x80));
1680 buf_set_u32(reg_params
[6].value
, 0, 32, flash_address(bank
, 0, pri_ext
->_unlock1
));
1681 buf_set_u32(reg_params
[7].value
, 0, 32, 0xaaaaaaaa);
1682 buf_set_u32(reg_params
[8].value
, 0, 32, flash_address(bank
, 0, pri_ext
->_unlock2
));
1683 buf_set_u32(reg_params
[9].value
, 0, 32, 0x55555555);
1685 retval
= target_run_algorithm(target
, 0, NULL
, 10, reg_params
,
1686 cfi_info
->write_algorithm
->address
,
1687 cfi_info
->write_algorithm
->address
+ ((target_code_size
) - 4),
1688 10000, &armv4_5_info
);
1689 if (retval
!= ERROR_OK
)
1694 status
= buf_get_u32(reg_params
[5].value
, 0, 32);
1697 LOG_ERROR("flash write block failed status: 0x%" PRIx32
, status
);
1698 retval
= ERROR_FLASH_OPERATION_FAILED
;
1702 buffer
+= thisrun_count
;
1703 address
+= thisrun_count
;
1704 count
-= thisrun_count
;
1707 target_free_all_working_areas(target
);
1709 destroy_reg_param(®_params
[0]);
1710 destroy_reg_param(®_params
[1]);
1711 destroy_reg_param(®_params
[2]);
1712 destroy_reg_param(®_params
[3]);
1713 destroy_reg_param(®_params
[4]);
1714 destroy_reg_param(®_params
[5]);
1715 destroy_reg_param(®_params
[6]);
1716 destroy_reg_param(®_params
[7]);
1717 destroy_reg_param(®_params
[8]);
1718 destroy_reg_param(®_params
[9]);
1723 static int cfi_intel_write_word(struct flash_bank
*bank
, uint8_t *word
, uint32_t address
)
1726 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
1727 struct target
*target
= bank
->target
;
1729 cfi_intel_clear_status_register(bank
);
1730 if ((retval
= cfi_send_command(bank
, 0x40, address
)) != ERROR_OK
)
1735 if ((retval
= target_write_memory(target
, address
, bank
->bus_width
, 1, word
)) != ERROR_OK
)
1741 retval
= cfi_intel_wait_status_busy(bank
, 1000 * (1 << cfi_info
->word_write_timeout_max
), &status
);
1744 if ((retval
= cfi_send_command(bank
, 0xff, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
1749 LOG_ERROR("couldn't write word at base 0x%" PRIx32
", address %" PRIx32
, bank
->base
, address
);
1750 return ERROR_FLASH_OPERATION_FAILED
;
1756 static int cfi_intel_write_words(struct flash_bank
*bank
, uint8_t *word
, uint32_t wordcount
, uint32_t address
)
1759 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
1760 struct target
*target
= bank
->target
;
1762 /* Calculate buffer size and boundary mask */
1763 /* buffersize is (buffer size per chip) * (number of chips) */
1764 /* bufferwsize is buffersize in words */
1765 uint32_t buffersize
= (1UL << cfi_info
->max_buf_write_size
) * (bank
->bus_width
/ bank
->chip_width
);
1766 uint32_t buffermask
= buffersize
-1;
1767 uint32_t bufferwsize
= buffersize
/ bank
->bus_width
;
1769 /* Check for valid range */
1770 if (address
& buffermask
)
1772 LOG_ERROR("Write address at base 0x%" PRIx32
", address %" PRIx32
" not aligned to 2^%d boundary",
1773 bank
->base
, address
, cfi_info
->max_buf_write_size
);
1774 return ERROR_FLASH_OPERATION_FAILED
;
1777 /* Check for valid size */
1778 if (wordcount
> bufferwsize
)
1780 LOG_ERROR("Number of data words %" PRId32
" exceeds available buffersize %" PRId32
, wordcount
, buffersize
);
1781 return ERROR_FLASH_OPERATION_FAILED
;
1784 /* Write to flash buffer */
1785 cfi_intel_clear_status_register(bank
);
1787 /* Initiate buffer operation _*/
1788 if ((retval
= cfi_send_command(bank
, 0xe8, address
)) != ERROR_OK
)
1793 retval
= cfi_intel_wait_status_busy(bank
, 1000 * (1 << cfi_info
->buf_write_timeout_max
), &status
);
1794 if (retval
!= ERROR_OK
)
1798 if ((retval
= cfi_send_command(bank
, 0xff, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
1803 LOG_ERROR("couldn't start buffer write operation at base 0x%" PRIx32
", address %" PRIx32
, bank
->base
, address
);
1804 return ERROR_FLASH_OPERATION_FAILED
;
1807 /* Write buffer wordcount-1 and data words */
1808 if ((retval
= cfi_send_command(bank
, bufferwsize
-1, address
)) != ERROR_OK
)
1813 if ((retval
= target_write_memory(target
, address
, bank
->bus_width
, bufferwsize
, word
)) != ERROR_OK
)
1818 /* Commit write operation */
1819 if ((retval
= cfi_send_command(bank
, 0xd0, address
)) != ERROR_OK
)
1824 retval
= cfi_intel_wait_status_busy(bank
, 1000 * (1 << cfi_info
->buf_write_timeout_max
), &status
);
1825 if (retval
!= ERROR_OK
)
1830 if ((retval
= cfi_send_command(bank
, 0xff, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
1835 LOG_ERROR("Buffer write at base 0x%" PRIx32
", address %" PRIx32
" failed.", bank
->base
, address
);
1836 return ERROR_FLASH_OPERATION_FAILED
;
1842 static int cfi_spansion_write_word(struct flash_bank
*bank
, uint8_t *word
, uint32_t address
)
1845 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
1846 struct cfi_spansion_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
1847 struct target
*target
= bank
->target
;
1849 if ((retval
= cfi_send_command(bank
, 0xaa, flash_address(bank
, 0, pri_ext
->_unlock1
))) != ERROR_OK
)
1854 if ((retval
= cfi_send_command(bank
, 0x55, flash_address(bank
, 0, pri_ext
->_unlock2
))) != ERROR_OK
)
1859 if ((retval
= cfi_send_command(bank
, 0xa0, flash_address(bank
, 0, pri_ext
->_unlock1
))) != ERROR_OK
)
1864 if ((retval
= target_write_memory(target
, address
, bank
->bus_width
, 1, word
)) != ERROR_OK
)
1869 if (cfi_spansion_wait_status_busy(bank
, 1000 * (1 << cfi_info
->word_write_timeout_max
)) != ERROR_OK
)
1871 if ((retval
= cfi_send_command(bank
, 0xf0, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
1876 LOG_ERROR("couldn't write word at base 0x%" PRIx32
", address %" PRIx32
, bank
->base
, address
);
1877 return ERROR_FLASH_OPERATION_FAILED
;
1883 static int cfi_spansion_write_words(struct flash_bank
*bank
, uint8_t *word
, uint32_t wordcount
, uint32_t address
)
1886 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
1887 struct target
*target
= bank
->target
;
1888 struct cfi_spansion_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
1890 /* Calculate buffer size and boundary mask */
1891 /* buffersize is (buffer size per chip) * (number of chips) */
1892 /* bufferwsize is buffersize in words */
1893 uint32_t buffersize
= (1UL << cfi_info
->max_buf_write_size
) * (bank
->bus_width
/ bank
->chip_width
);
1894 uint32_t buffermask
= buffersize
-1;
1895 uint32_t bufferwsize
= buffersize
/ bank
->bus_width
;
1897 /* Check for valid range */
1898 if (address
& buffermask
)
1900 LOG_ERROR("Write address at base 0x%" PRIx32
", address %" PRIx32
" not aligned to 2^%d boundary", bank
->base
, address
, cfi_info
->max_buf_write_size
);
1901 return ERROR_FLASH_OPERATION_FAILED
;
1904 /* Check for valid size */
1905 if (wordcount
> bufferwsize
)
1907 LOG_ERROR("Number of data words %" PRId32
" exceeds available buffersize %" PRId32
, wordcount
, buffersize
);
1908 return ERROR_FLASH_OPERATION_FAILED
;
1912 if ((retval
= cfi_send_command(bank
, 0xaa, flash_address(bank
, 0, pri_ext
->_unlock1
))) != ERROR_OK
)
1917 if ((retval
= cfi_send_command(bank
, 0x55, flash_address(bank
, 0, pri_ext
->_unlock2
))) != ERROR_OK
)
1922 // Buffer load command
1923 if ((retval
= cfi_send_command(bank
, 0x25, address
)) != ERROR_OK
)
1928 /* Write buffer wordcount-1 and data words */
1929 if ((retval
= cfi_send_command(bank
, bufferwsize
-1, address
)) != ERROR_OK
)
1934 if ((retval
= target_write_memory(target
, address
, bank
->bus_width
, bufferwsize
, word
)) != ERROR_OK
)
1939 /* Commit write operation */
1940 if ((retval
= cfi_send_command(bank
, 0x29, address
)) != ERROR_OK
)
1945 if (cfi_spansion_wait_status_busy(bank
, 1000 * (1 << cfi_info
->word_write_timeout_max
)) != ERROR_OK
)
1947 if ((retval
= cfi_send_command(bank
, 0xf0, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
1952 LOG_ERROR("couldn't write block at base 0x%" PRIx32
", address %" PRIx32
", size %" PRIx32
, bank
->base
, address
, bufferwsize
);
1953 return ERROR_FLASH_OPERATION_FAILED
;
1959 static int cfi_write_word(struct flash_bank
*bank
, uint8_t *word
, uint32_t address
)
1961 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
1963 switch (cfi_info
->pri_id
)
1967 return cfi_intel_write_word(bank
, word
, address
);
1970 return cfi_spansion_write_word(bank
, word
, address
);
1973 LOG_ERROR("cfi primary command set %i unsupported", cfi_info
->pri_id
);
1977 return ERROR_FLASH_OPERATION_FAILED
;
1980 static int cfi_write_words(struct flash_bank
*bank
, uint8_t *word
, uint32_t wordcount
, uint32_t address
)
1982 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
1984 switch (cfi_info
->pri_id
)
1988 return cfi_intel_write_words(bank
, word
, wordcount
, address
);
1991 return cfi_spansion_write_words(bank
, word
, wordcount
, address
);
1994 LOG_ERROR("cfi primary command set %i unsupported", cfi_info
->pri_id
);
1998 return ERROR_FLASH_OPERATION_FAILED
;
2001 static int cfi_read(struct flash_bank
*bank
, uint8_t *buffer
, uint32_t offset
, uint32_t count
)
2003 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2004 struct target
*target
= bank
->target
;
2005 uint32_t address
= bank
->base
+ offset
;
2007 int align
; /* number of unaligned bytes */
2008 uint8_t current_word
[CFI_MAX_BUS_WIDTH
];
2012 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
2013 (int)count
, (unsigned)offset
);
2015 if (bank
->target
->state
!= TARGET_HALTED
)
2017 LOG_ERROR("Target not halted");
2018 return ERROR_TARGET_NOT_HALTED
;
2021 if (offset
+ count
> bank
->size
)
2022 return ERROR_FLASH_DST_OUT_OF_BANK
;
2024 if (cfi_info
->qry
[0] != 'Q')
2025 return ERROR_FLASH_BANK_NOT_PROBED
;
2027 /* start at the first byte of the first word (bus_width size) */
2028 read_p
= address
& ~(bank
->bus_width
- 1);
2029 if ((align
= address
- read_p
) != 0)
2031 LOG_INFO("Fixup %d unaligned read head bytes", align
);
2033 /* read a complete word from flash */
2034 if ((retval
= target_read_memory(target
, read_p
, bank
->bus_width
, 1, current_word
)) != ERROR_OK
)
2037 /* take only bytes we need */
2038 for (i
= align
; (i
< bank
->bus_width
) && (count
> 0); i
++, count
--)
2039 *buffer
++ = current_word
[i
];
2041 read_p
+= bank
->bus_width
;
2044 align
= count
/ bank
->bus_width
;
2047 if ((retval
= target_read_memory(target
, read_p
, bank
->bus_width
, align
, buffer
)) != ERROR_OK
)
2050 read_p
+= align
* bank
->bus_width
;
2051 buffer
+= align
* bank
->bus_width
;
2052 count
-= align
* bank
->bus_width
;
2057 LOG_INFO("Fixup %d unaligned read tail bytes", count
);
2059 /* read a complete word from flash */
2060 if ((retval
= target_read_memory(target
, read_p
, bank
->bus_width
, 1, current_word
)) != ERROR_OK
)
2063 /* take only bytes we need */
2064 for (i
= 0; (i
< bank
->bus_width
) && (count
> 0); i
++, count
--)
2065 *buffer
++ = current_word
[i
];
2071 static int cfi_write(struct flash_bank
*bank
, uint8_t *buffer
, uint32_t offset
, uint32_t count
)
2073 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2074 struct target
*target
= bank
->target
;
2075 uint32_t address
= bank
->base
+ offset
; /* address of first byte to be programmed */
2077 int align
; /* number of unaligned bytes */
2078 int blk_count
; /* number of bus_width bytes for block copy */
2079 uint8_t current_word
[CFI_MAX_BUS_WIDTH
* 4]; /* word (bus_width size) currently being programmed */
2083 if (bank
->target
->state
!= TARGET_HALTED
)
2085 LOG_ERROR("Target not halted");
2086 return ERROR_TARGET_NOT_HALTED
;
2089 if (offset
+ count
> bank
->size
)
2090 return ERROR_FLASH_DST_OUT_OF_BANK
;
2092 if (cfi_info
->qry
[0] != 'Q')
2093 return ERROR_FLASH_BANK_NOT_PROBED
;
2095 /* start at the first byte of the first word (bus_width size) */
2096 write_p
= address
& ~(bank
->bus_width
- 1);
2097 if ((align
= address
- write_p
) != 0)
2099 LOG_INFO("Fixup %d unaligned head bytes", align
);
2101 /* read a complete word from flash */
2102 if ((retval
= target_read_memory(target
, write_p
, bank
->bus_width
, 1, current_word
)) != ERROR_OK
)
2105 /* replace only bytes that must be written */
2106 for (i
= align
; (i
< bank
->bus_width
) && (count
> 0); i
++, count
--)
2107 current_word
[i
] = *buffer
++;
2109 retval
= cfi_write_word(bank
, current_word
, write_p
);
2110 if (retval
!= ERROR_OK
)
2112 write_p
+= bank
->bus_width
;
2115 /* handle blocks of bus_size aligned bytes */
2116 blk_count
= count
& ~(bank
->bus_width
- 1); /* round down, leave tail bytes */
2117 switch (cfi_info
->pri_id
)
2119 /* try block writes (fails without working area) */
2122 retval
= cfi_intel_write_block(bank
, buffer
, write_p
, blk_count
);
2125 retval
= cfi_spansion_write_block(bank
, buffer
, write_p
, blk_count
);
2128 LOG_ERROR("cfi primary command set %i unsupported", cfi_info
->pri_id
);
2129 retval
= ERROR_FLASH_OPERATION_FAILED
;
2132 if (retval
== ERROR_OK
)
2134 /* Increment pointers and decrease count on succesful block write */
2135 buffer
+= blk_count
;
2136 write_p
+= blk_count
;
2141 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
2143 /* Calculate buffer size and boundary mask */
2144 /* buffersize is (buffer size per chip) * (number of chips) */
2145 /* bufferwsize is buffersize in words */
2146 uint32_t buffersize
= (1UL << cfi_info
->max_buf_write_size
) * (bank
->bus_width
/ bank
->chip_width
);
2147 uint32_t buffermask
= buffersize
-1;
2148 uint32_t bufferwsize
= buffersize
/ bank
->bus_width
;
2150 /* fall back to memory writes */
2151 while (count
>= (uint32_t)bank
->bus_width
)
2154 if ((write_p
& 0xff) == 0)
2156 LOG_INFO("Programming at %08" PRIx32
", count %08" PRIx32
" bytes remaining", write_p
, count
);
2159 if ((bufferwsize
> 0) && (count
>= buffersize
) && !(write_p
& buffermask
))
2161 retval
= cfi_write_words(bank
, buffer
, bufferwsize
, write_p
);
2162 if (retval
== ERROR_OK
)
2164 buffer
+= buffersize
;
2165 write_p
+= buffersize
;
2166 count
-= buffersize
;
2170 /* try the slow way? */
2173 for (i
= 0; i
< bank
->bus_width
; i
++)
2174 current_word
[i
] = *buffer
++;
2176 retval
= cfi_write_word(bank
, current_word
, write_p
);
2177 if (retval
!= ERROR_OK
)
2180 write_p
+= bank
->bus_width
;
2181 count
-= bank
->bus_width
;
2189 /* return to read array mode, so we can read from flash again for padding */
2190 if ((retval
= cfi_reset(bank
)) != ERROR_OK
)
2195 /* handle unaligned tail bytes */
2198 LOG_INFO("Fixup %" PRId32
" unaligned tail bytes", count
);
2200 /* read a complete word from flash */
2201 if ((retval
= target_read_memory(target
, write_p
, bank
->bus_width
, 1, current_word
)) != ERROR_OK
)
2204 /* replace only bytes that must be written */
2205 for (i
= 0; (i
< bank
->bus_width
) && (count
> 0); i
++, count
--)
2206 current_word
[i
] = *buffer
++;
2208 retval
= cfi_write_word(bank
, current_word
, write_p
);
2209 if (retval
!= ERROR_OK
)
2213 /* return to read array mode */
2214 return cfi_reset(bank
);
2217 static void cfi_fixup_atmel_reversed_erase_regions(struct flash_bank
*bank
, void *param
)
2220 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2221 struct cfi_spansion_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
2223 pri_ext
->_reversed_geometry
= 1;
2226 static void cfi_fixup_0002_erase_regions(struct flash_bank
*bank
, void *param
)
2229 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2230 struct cfi_spansion_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
2233 if ((pri_ext
->_reversed_geometry
) || (pri_ext
->TopBottom
== 3))
2235 LOG_DEBUG("swapping reversed erase region information on cmdset 0002 device");
2237 for (i
= 0; i
< cfi_info
->num_erase_regions
/ 2; i
++)
2239 int j
= (cfi_info
->num_erase_regions
- 1) - i
;
2242 swap
= cfi_info
->erase_region_info
[i
];
2243 cfi_info
->erase_region_info
[i
] = cfi_info
->erase_region_info
[j
];
2244 cfi_info
->erase_region_info
[j
] = swap
;
2249 static void cfi_fixup_0002_unlock_addresses(struct flash_bank
*bank
, void *param
)
2251 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2252 struct cfi_spansion_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
2253 struct cfi_unlock_addresses
*unlock_addresses
= param
;
2255 pri_ext
->_unlock1
= unlock_addresses
->unlock1
;
2256 pri_ext
->_unlock2
= unlock_addresses
->unlock2
;
2260 static int cfi_query_string(struct flash_bank
*bank
, int address
)
2262 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2265 if ((retval
= cfi_send_command(bank
, 0x98, flash_address(bank
, 0, address
))) != ERROR_OK
)
2270 retval
= cfi_query_u8(bank
, 0, 0x10, &cfi_info
->qry
[0]);
2271 if (retval
!= ERROR_OK
)
2273 retval
= cfi_query_u8(bank
, 0, 0x11, &cfi_info
->qry
[1]);
2274 if (retval
!= ERROR_OK
)
2276 retval
= cfi_query_u8(bank
, 0, 0x12, &cfi_info
->qry
[2]);
2277 if (retval
!= ERROR_OK
)
2280 LOG_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]);
2282 if ((cfi_info
->qry
[0] != 'Q') || (cfi_info
->qry
[1] != 'R') || (cfi_info
->qry
[2] != 'Y'))
2284 if ((retval
= cfi_reset(bank
)) != ERROR_OK
)
2288 LOG_ERROR("Could not probe bank: no QRY");
2289 return ERROR_FLASH_BANK_INVALID
;
2295 static int cfi_probe(struct flash_bank
*bank
)
2297 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2298 struct target
*target
= bank
->target
;
2299 int num_sectors
= 0;
2302 uint32_t unlock1
= 0x555;
2303 uint32_t unlock2
= 0x2aa;
2305 uint8_t value_buf0
[CFI_MAX_BUS_WIDTH
], value_buf1
[CFI_MAX_BUS_WIDTH
];
2307 if (bank
->target
->state
!= TARGET_HALTED
)
2309 LOG_ERROR("Target not halted");
2310 return ERROR_TARGET_NOT_HALTED
;
2313 cfi_info
->probed
= 0;
2316 free(bank
->sectors
);
2317 bank
->sectors
= NULL
;
2319 if(cfi_info
->erase_region_info
)
2321 free(cfi_info
->erase_region_info
);
2322 cfi_info
->erase_region_info
= NULL
;
2325 /* JEDEC standard JESD21C uses 0x5555 and 0x2aaa as unlock addresses,
2326 * while CFI compatible AMD/Spansion flashes use 0x555 and 0x2aa
2328 if (cfi_info
->jedec_probe
)
2334 /* switch to read identifier codes mode ("AUTOSELECT") */
2335 if ((retval
= cfi_send_command(bank
, 0xaa, flash_address(bank
, 0, unlock1
))) != ERROR_OK
)
2339 if ((retval
= cfi_send_command(bank
, 0x55, flash_address(bank
, 0, unlock2
))) != ERROR_OK
)
2343 if ((retval
= cfi_send_command(bank
, 0x90, flash_address(bank
, 0, unlock1
))) != ERROR_OK
)
2348 if ((retval
= target_read_memory(target
, flash_address(bank
, 0, 0x00), bank
->bus_width
, 1, value_buf0
)) != ERROR_OK
)
2352 if ((retval
= target_read_memory(target
, flash_address(bank
, 0, 0x01), bank
->bus_width
, 1, value_buf1
)) != ERROR_OK
)
2356 switch (bank
->chip_width
) {
2358 cfi_info
->manufacturer
= *value_buf0
;
2359 cfi_info
->device_id
= *value_buf1
;
2362 cfi_info
->manufacturer
= target_buffer_get_u16(target
, value_buf0
);
2363 cfi_info
->device_id
= target_buffer_get_u16(target
, value_buf1
);
2366 cfi_info
->manufacturer
= target_buffer_get_u32(target
, value_buf0
);
2367 cfi_info
->device_id
= target_buffer_get_u32(target
, value_buf1
);
2370 LOG_ERROR("Unsupported bank chipwidth %d, can't probe memory", bank
->chip_width
);
2371 return ERROR_FLASH_OPERATION_FAILED
;
2374 LOG_INFO("Flash Manufacturer/Device: 0x%04x 0x%04x", cfi_info
->manufacturer
, cfi_info
->device_id
);
2375 /* switch back to read array mode */
2376 if ((retval
= cfi_reset(bank
)) != ERROR_OK
)
2381 /* check device/manufacturer ID for known non-CFI flashes. */
2382 cfi_fixup_non_cfi(bank
);
2384 /* query only if this is a CFI compatible flash,
2385 * otherwise the relevant info has already been filled in
2387 if (cfi_info
->not_cfi
== 0)
2389 /* enter CFI query mode
2390 * according to JEDEC Standard No. 68.01,
2391 * a single bus sequence with address = 0x55, data = 0x98 should put
2392 * the device into CFI query mode.
2394 * SST flashes clearly violate this, and we will consider them incompatbile for now
2397 retval
= cfi_query_string(bank
, 0x55);
2398 if (retval
!= ERROR_OK
)
2401 * Spansion S29WS-N CFI query fix is to try 0x555 if 0x55 fails. Should
2402 * be harmless enough:
2404 * http://www.infradead.org/pipermail/linux-mtd/2005-September/013618.html
2406 LOG_USER("Try workaround w/0x555 instead of 0x55 to get QRY.");
2407 retval
= cfi_query_string(bank
, 0x555);
2409 if (retval
!= ERROR_OK
)
2412 retval
= cfi_query_u16(bank
, 0, 0x13, &cfi_info
->pri_id
);
2413 if (retval
!= ERROR_OK
)
2415 retval
= cfi_query_u16(bank
, 0, 0x15, &cfi_info
->pri_addr
);
2416 if (retval
!= ERROR_OK
)
2418 retval
= cfi_query_u16(bank
, 0, 0x17, &cfi_info
->alt_id
);
2419 if (retval
!= ERROR_OK
)
2421 retval
= cfi_query_u16(bank
, 0, 0x19, &cfi_info
->alt_addr
);
2422 if (retval
!= ERROR_OK
)
2425 LOG_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
);
2427 retval
= cfi_query_u8(bank
, 0, 0x1b, &cfi_info
->vcc_min
);
2428 if (retval
!= ERROR_OK
)
2430 retval
= cfi_query_u8(bank
, 0, 0x1c, &cfi_info
->vcc_max
);
2431 if (retval
!= ERROR_OK
)
2433 retval
= cfi_query_u8(bank
, 0, 0x1d, &cfi_info
->vpp_min
);
2434 if (retval
!= ERROR_OK
)
2436 retval
= cfi_query_u8(bank
, 0, 0x1e, &cfi_info
->vpp_max
);
2437 if (retval
!= ERROR_OK
)
2439 retval
= cfi_query_u8(bank
, 0, 0x1f, &cfi_info
->word_write_timeout_typ
);
2440 if (retval
!= ERROR_OK
)
2442 retval
= cfi_query_u8(bank
, 0, 0x20, &cfi_info
->buf_write_timeout_typ
);
2443 if (retval
!= ERROR_OK
)
2445 retval
= cfi_query_u8(bank
, 0, 0x21, &cfi_info
->block_erase_timeout_typ
);
2446 if (retval
!= ERROR_OK
)
2448 retval
= cfi_query_u8(bank
, 0, 0x22, &cfi_info
->chip_erase_timeout_typ
);
2449 if (retval
!= ERROR_OK
)
2451 retval
= cfi_query_u8(bank
, 0, 0x23, &cfi_info
->word_write_timeout_max
);
2452 if (retval
!= ERROR_OK
)
2454 retval
= cfi_query_u8(bank
, 0, 0x24, &cfi_info
->buf_write_timeout_max
);
2455 if (retval
!= ERROR_OK
)
2457 retval
= cfi_query_u8(bank
, 0, 0x25, &cfi_info
->block_erase_timeout_max
);
2458 if (retval
!= ERROR_OK
)
2460 retval
= cfi_query_u8(bank
, 0, 0x26, &cfi_info
->chip_erase_timeout_max
);
2461 if (retval
!= ERROR_OK
)
2464 LOG_DEBUG("Vcc min: %x.%x, Vcc max: %x.%x, Vpp min: %u.%x, Vpp max: %u.%x",
2465 (cfi_info
->vcc_min
& 0xf0) >> 4, cfi_info
->vcc_min
& 0x0f,
2466 (cfi_info
->vcc_max
& 0xf0) >> 4, cfi_info
->vcc_max
& 0x0f,
2467 (cfi_info
->vpp_min
& 0xf0) >> 4, cfi_info
->vpp_min
& 0x0f,
2468 (cfi_info
->vpp_max
& 0xf0) >> 4, cfi_info
->vpp_max
& 0x0f);
2469 LOG_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
,
2470 1 << cfi_info
->block_erase_timeout_typ
, 1 << cfi_info
->chip_erase_timeout_typ
);
2471 LOG_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
),
2472 (1 << cfi_info
->buf_write_timeout_max
) * (1 << cfi_info
->buf_write_timeout_typ
),
2473 (1 << cfi_info
->block_erase_timeout_max
) * (1 << cfi_info
->block_erase_timeout_typ
),
2474 (1 << cfi_info
->chip_erase_timeout_max
) * (1 << cfi_info
->chip_erase_timeout_typ
));
2477 retval
= cfi_query_u8(bank
, 0, 0x27, &data
);
2478 if (retval
!= ERROR_OK
)
2480 cfi_info
->dev_size
= 1 << data
;
2482 retval
= cfi_query_u16(bank
, 0, 0x28, &cfi_info
->interface_desc
);
2483 if (retval
!= ERROR_OK
)
2485 retval
= cfi_query_u16(bank
, 0, 0x2a, &cfi_info
->max_buf_write_size
);
2486 if (retval
!= ERROR_OK
)
2488 retval
= cfi_query_u8(bank
, 0, 0x2c, &cfi_info
->num_erase_regions
);
2489 if (retval
!= ERROR_OK
)
2492 LOG_DEBUG("size: 0x%" PRIx32
", interface desc: %i, max buffer write size: %x", cfi_info
->dev_size
, cfi_info
->interface_desc
, (1 << cfi_info
->max_buf_write_size
));
2494 if (cfi_info
->num_erase_regions
)
2496 cfi_info
->erase_region_info
= malloc(4 * cfi_info
->num_erase_regions
);
2497 for (i
= 0; i
< cfi_info
->num_erase_regions
; i
++)
2499 retval
= cfi_query_u32(bank
, 0, 0x2d + (4 * i
), &cfi_info
->erase_region_info
[i
]);
2500 if (retval
!= ERROR_OK
)
2502 LOG_DEBUG("erase region[%i]: %" PRIu32
" blocks of size 0x%" PRIx32
"",
2504 (cfi_info
->erase_region_info
[i
] & 0xffff) + 1,
2505 (cfi_info
->erase_region_info
[i
] >> 16) * 256);
2510 cfi_info
->erase_region_info
= NULL
;
2513 /* We need to read the primary algorithm extended query table before calculating
2514 * the sector layout to be able to apply fixups
2516 switch (cfi_info
->pri_id
)
2518 /* Intel command set (standard and extended) */
2521 cfi_read_intel_pri_ext(bank
);
2523 /* AMD/Spansion, Atmel, ... command set */
2525 cfi_info
->status_poll_mask
= CFI_STATUS_POLL_MASK_DQ5_DQ6_DQ7
; /* default for all CFI flashs */
2526 cfi_read_0002_pri_ext(bank
);
2529 LOG_ERROR("cfi primary command set %i unsupported", cfi_info
->pri_id
);
2533 /* return to read array mode
2534 * we use both reset commands, as some Intel flashes fail to recognize the 0xF0 command
2536 if ((retval
= cfi_reset(bank
)) != ERROR_OK
)
2540 } /* end CFI case */
2542 /* apply fixups depending on the primary command set */
2543 switch (cfi_info
->pri_id
)
2545 /* Intel command set (standard and extended) */
2548 cfi_fixup(bank
, cfi_0001_fixups
);
2550 /* AMD/Spansion, Atmel, ... command set */
2552 cfi_fixup(bank
, cfi_0002_fixups
);
2555 LOG_ERROR("cfi primary command set %i unsupported", cfi_info
->pri_id
);
2559 if ((cfi_info
->dev_size
* bank
->bus_width
/ bank
->chip_width
) != bank
->size
)
2561 LOG_WARNING("configuration specifies 0x%" PRIx32
" size, but a 0x%" PRIx32
" size flash was found", bank
->size
, cfi_info
->dev_size
);
2564 if (cfi_info
->num_erase_regions
== 0)
2566 /* a device might have only one erase block, spanning the whole device */
2567 bank
->num_sectors
= 1;
2568 bank
->sectors
= malloc(sizeof(struct flash_sector
));
2570 bank
->sectors
[sector
].offset
= 0x0;
2571 bank
->sectors
[sector
].size
= bank
->size
;
2572 bank
->sectors
[sector
].is_erased
= -1;
2573 bank
->sectors
[sector
].is_protected
= -1;
2577 uint32_t offset
= 0;
2579 for (i
= 0; i
< cfi_info
->num_erase_regions
; i
++)
2581 num_sectors
+= (cfi_info
->erase_region_info
[i
] & 0xffff) + 1;
2584 bank
->num_sectors
= num_sectors
;
2585 bank
->sectors
= malloc(sizeof(struct flash_sector
) * num_sectors
);
2587 for (i
= 0; i
< cfi_info
->num_erase_regions
; i
++)
2590 for (j
= 0; j
< (cfi_info
->erase_region_info
[i
] & 0xffff) + 1; j
++)
2592 bank
->sectors
[sector
].offset
= offset
;
2593 bank
->sectors
[sector
].size
= ((cfi_info
->erase_region_info
[i
] >> 16) * 256) * bank
->bus_width
/ bank
->chip_width
;
2594 offset
+= bank
->sectors
[sector
].size
;
2595 bank
->sectors
[sector
].is_erased
= -1;
2596 bank
->sectors
[sector
].is_protected
= -1;
2600 if (offset
!= (cfi_info
->dev_size
* bank
->bus_width
/ bank
->chip_width
))
2602 LOG_WARNING("CFI size is 0x%" PRIx32
", but total sector size is 0x%" PRIx32
"", \
2603 (cfi_info
->dev_size
* bank
->bus_width
/ bank
->chip_width
), offset
);
2607 cfi_info
->probed
= 1;
2612 static int cfi_auto_probe(struct flash_bank
*bank
)
2614 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2615 if (cfi_info
->probed
)
2617 return cfi_probe(bank
);
2620 static int cfi_intel_protect_check(struct flash_bank
*bank
)
2623 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2624 struct cfi_intel_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
2627 /* check if block lock bits are supported on this device */
2628 if (!(pri_ext
->blk_status_reg_mask
& 0x1))
2629 return ERROR_FLASH_OPERATION_FAILED
;
2631 if ((retval
= cfi_send_command(bank
, 0x90, flash_address(bank
, 0, 0x55))) != ERROR_OK
)
2636 for (i
= 0; i
< bank
->num_sectors
; i
++)
2638 uint8_t block_status
;
2639 retval
= cfi_get_u8(bank
, i
, 0x2, &block_status
);
2640 if (retval
!= ERROR_OK
)
2643 if (block_status
& 1)
2644 bank
->sectors
[i
].is_protected
= 1;
2646 bank
->sectors
[i
].is_protected
= 0;
2649 return cfi_send_command(bank
, 0xff, flash_address(bank
, 0, 0x0));
2652 static int cfi_spansion_protect_check(struct flash_bank
*bank
)
2655 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2656 struct cfi_spansion_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
2659 if ((retval
= cfi_send_command(bank
, 0xaa, flash_address(bank
, 0, pri_ext
->_unlock1
))) != ERROR_OK
)
2664 if ((retval
= cfi_send_command(bank
, 0x55, flash_address(bank
, 0, pri_ext
->_unlock2
))) != ERROR_OK
)
2669 if ((retval
= cfi_send_command(bank
, 0x90, flash_address(bank
, 0, pri_ext
->_unlock1
))) != ERROR_OK
)
2674 for (i
= 0; i
< bank
->num_sectors
; i
++)
2676 uint8_t block_status
;
2677 retval
= cfi_get_u8(bank
, i
, 0x2, &block_status
);
2678 if (retval
!= ERROR_OK
)
2681 if (block_status
& 1)
2682 bank
->sectors
[i
].is_protected
= 1;
2684 bank
->sectors
[i
].is_protected
= 0;
2687 return cfi_send_command(bank
, 0xf0, flash_address(bank
, 0, 0x0));
2690 static int cfi_protect_check(struct flash_bank
*bank
)
2692 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2694 if (bank
->target
->state
!= TARGET_HALTED
)
2696 LOG_ERROR("Target not halted");
2697 return ERROR_TARGET_NOT_HALTED
;
2700 if (cfi_info
->qry
[0] != 'Q')
2701 return ERROR_FLASH_BANK_NOT_PROBED
;
2703 switch (cfi_info
->pri_id
)
2707 return cfi_intel_protect_check(bank
);
2710 return cfi_spansion_protect_check(bank
);
2713 LOG_ERROR("cfi primary command set %i unsupported", cfi_info
->pri_id
);
2720 static int get_cfi_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
2723 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2725 if (cfi_info
->qry
[0] == 0xff)
2727 printed
= snprintf(buf
, buf_size
, "\ncfi flash bank not probed yet\n");
2731 if (cfi_info
->not_cfi
== 0)
2732 printed
= snprintf(buf
, buf_size
, "\ncfi information:\n");
2734 printed
= snprintf(buf
, buf_size
, "\nnon-cfi flash:\n");
2736 buf_size
-= printed
;
2738 printed
= snprintf(buf
, buf_size
, "\nmfr: 0x%4.4x, id:0x%4.4x\n",
2739 cfi_info
->manufacturer
, cfi_info
->device_id
);
2741 buf_size
-= printed
;
2743 if (cfi_info
->not_cfi
== 0)
2745 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
);
2747 buf_size
-= printed
;
2749 printed
= snprintf(buf
, buf_size
, "Vcc min: %x.%x, Vcc max: %x.%x, Vpp min: %u.%x, Vpp max: %u.%x\n",
2750 (cfi_info
->vcc_min
& 0xf0) >> 4, cfi_info
->vcc_min
& 0x0f,
2751 (cfi_info
->vcc_max
& 0xf0) >> 4, cfi_info
->vcc_max
& 0x0f,
2752 (cfi_info
->vpp_min
& 0xf0) >> 4, cfi_info
->vpp_min
& 0x0f,
2753 (cfi_info
->vpp_max
& 0xf0) >> 4, cfi_info
->vpp_max
& 0x0f);
2755 buf_size
-= printed
;
2757 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",
2758 1 << cfi_info
->word_write_timeout_typ
,
2759 1 << cfi_info
->buf_write_timeout_typ
,
2760 1 << cfi_info
->block_erase_timeout_typ
,
2761 1 << cfi_info
->chip_erase_timeout_typ
);
2763 buf_size
-= printed
;
2765 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",
2766 (1 << cfi_info
->word_write_timeout_max
) * (1 << cfi_info
->word_write_timeout_typ
),
2767 (1 << cfi_info
->buf_write_timeout_max
) * (1 << cfi_info
->buf_write_timeout_typ
),
2768 (1 << cfi_info
->block_erase_timeout_max
) * (1 << cfi_info
->block_erase_timeout_typ
),
2769 (1 << cfi_info
->chip_erase_timeout_max
) * (1 << cfi_info
->chip_erase_timeout_typ
));
2771 buf_size
-= printed
;
2773 printed
= snprintf(buf
, buf_size
, "size: 0x%" PRIx32
", interface desc: %i, max buffer write size: %x\n",
2775 cfi_info
->interface_desc
,
2776 1 << cfi_info
->max_buf_write_size
);
2778 buf_size
-= printed
;
2780 switch (cfi_info
->pri_id
)
2784 cfi_intel_info(bank
, buf
, buf_size
);
2787 cfi_spansion_info(bank
, buf
, buf_size
);
2790 LOG_ERROR("cfi primary command set %i unsupported", cfi_info
->pri_id
);
2798 struct flash_driver cfi_flash
= {
2800 .flash_bank_command
= cfi_flash_bank_command
,
2802 .protect
= cfi_protect
,
2806 .auto_probe
= cfi_auto_probe
,
2807 /* FIXME: access flash at bus_width size */
2808 .erase_check
= default_flash_blank_check
,
2809 .protect_check
= cfi_protect_check
,
2810 .info
= get_cfi_info
,
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)