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_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_reversed_erase_regions
, NULL
},
61 {CFI_MFR_ST
, 0x22C4, cfi_fixup_reversed_erase_regions
, NULL
}, /* M29W160ET */
62 {CFI_MFR_FUJITSU
, 0x22ea, cfi_fixup_0002_unlock_addresses
, &cfi_unlock_addresses
[CFI_UNLOCK_555_2AA
]},
63 {CFI_MFR_FUJITSU
, 0x226b, cfi_fixup_0002_unlock_addresses
, &cfi_unlock_addresses
[CFI_UNLOCK_5555_2AAA
]},
64 {CFI_MFR_AMIC
, 0xb31a, cfi_fixup_0002_unlock_addresses
, &cfi_unlock_addresses
[CFI_UNLOCK_555_2AA
]},
65 {CFI_MFR_MX
, 0x225b, cfi_fixup_0002_unlock_addresses
, &cfi_unlock_addresses
[CFI_UNLOCK_555_2AA
]},
66 {CFI_MFR_AMD
, 0x225b, cfi_fixup_0002_unlock_addresses
, &cfi_unlock_addresses
[CFI_UNLOCK_555_2AA
]},
67 {CFI_MFR_ANY
, CFI_ID_ANY
, cfi_fixup_0002_erase_regions
, NULL
},
71 /* fixup after reading cmdset 0001 primary query table */
72 static const struct cfi_fixup cfi_0001_fixups
[] = {
76 static void cfi_fixup(struct flash_bank
*bank
, const struct cfi_fixup
*fixups
)
78 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
79 const struct cfi_fixup
*f
;
81 for (f
= fixups
; f
->fixup
; f
++)
83 if (((f
->mfr
== CFI_MFR_ANY
) || (f
->mfr
== cfi_info
->manufacturer
)) &&
84 ((f
->id
== CFI_ID_ANY
) || (f
->id
== cfi_info
->device_id
)))
86 f
->fixup(bank
, f
->param
);
91 /* inline uint32_t flash_address(struct flash_bank *bank, int sector, uint32_t offset) */
92 static __inline__
uint32_t flash_address(struct flash_bank
*bank
, int sector
, uint32_t offset
)
94 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
96 if (cfi_info
->x16_as_x8
) offset
*= 2;
98 /* while the sector list isn't built, only accesses to sector 0 work */
100 return bank
->base
+ offset
* bank
->bus_width
;
105 LOG_ERROR("BUG: sector list not yet built");
108 return bank
->base
+ bank
->sectors
[sector
].offset
+ offset
* bank
->bus_width
;
112 static void cfi_command(struct flash_bank
*bank
, uint8_t cmd
, uint8_t *cmd_buf
)
116 /* clear whole buffer, to ensure bits that exceed the bus_width
119 for (i
= 0; i
< CFI_MAX_BUS_WIDTH
; i
++)
122 if (bank
->target
->endianness
== TARGET_LITTLE_ENDIAN
)
124 for (i
= bank
->bus_width
; i
> 0; i
--)
126 *cmd_buf
++ = (i
& (bank
->chip_width
- 1)) ? 0x0 : cmd
;
131 for (i
= 1; i
<= bank
->bus_width
; i
++)
133 *cmd_buf
++ = (i
& (bank
->chip_width
- 1)) ? 0x0 : cmd
;
138 static int cfi_send_command(struct flash_bank
*bank
, uint8_t cmd
, uint32_t address
)
140 uint8_t command
[CFI_MAX_BUS_WIDTH
];
142 cfi_command(bank
, cmd
, command
);
143 return target_write_memory(bank
->target
, address
, bank
->bus_width
, 1, command
);
146 /* read unsigned 8-bit value from the bank
147 * flash banks are expected to be made of similar chips
148 * the query result should be the same for all
150 static int cfi_query_u8(struct flash_bank
*bank
, int sector
, uint32_t offset
, uint8_t *val
)
152 struct target
*target
= bank
->target
;
153 uint8_t data
[CFI_MAX_BUS_WIDTH
];
156 retval
= target_read_memory(target
, flash_address(bank
, sector
, offset
), bank
->bus_width
, 1, data
);
157 if (retval
!= ERROR_OK
)
160 if (bank
->target
->endianness
== TARGET_LITTLE_ENDIAN
)
163 *val
= data
[bank
->bus_width
- 1];
168 /* read unsigned 8-bit value from the bank
169 * in case of a bank made of multiple chips,
170 * the individual values are ORed
172 static int cfi_get_u8(struct flash_bank
*bank
, int sector
, uint32_t offset
, uint8_t *val
)
174 struct target
*target
= bank
->target
;
175 uint8_t data
[CFI_MAX_BUS_WIDTH
];
179 retval
= target_read_memory(target
, flash_address(bank
, sector
, offset
), bank
->bus_width
, 1, data
);
180 if (retval
!= ERROR_OK
)
183 if (bank
->target
->endianness
== TARGET_LITTLE_ENDIAN
)
185 for (i
= 0; i
< bank
->bus_width
/ bank
->chip_width
; i
++)
193 for (i
= 0; i
< bank
->bus_width
/ bank
->chip_width
; i
++)
194 value
|= data
[bank
->bus_width
- 1 - i
];
201 static int cfi_query_u16(struct flash_bank
*bank
, int sector
, uint32_t offset
, uint16_t *val
)
203 struct target
*target
= bank
->target
;
204 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
205 uint8_t data
[CFI_MAX_BUS_WIDTH
* 2];
208 if (cfi_info
->x16_as_x8
)
211 for (i
= 0;i
< 2;i
++)
213 retval
= target_read_memory(target
, flash_address(bank
, sector
, offset
+ i
), bank
->bus_width
, 1,
214 &data
[i
*bank
->bus_width
]);
215 if (retval
!= ERROR_OK
)
220 retval
= target_read_memory(target
, flash_address(bank
, sector
, offset
), bank
->bus_width
, 2, data
);
221 if (retval
!= ERROR_OK
)
225 if (bank
->target
->endianness
== TARGET_LITTLE_ENDIAN
)
226 *val
= data
[0] | data
[bank
->bus_width
] << 8;
228 *val
= data
[bank
->bus_width
- 1] | data
[(2 * bank
->bus_width
) - 1] << 8;
233 static int cfi_query_u32(struct flash_bank
*bank
, int sector
, uint32_t offset
, uint32_t *val
)
235 struct target
*target
= bank
->target
;
236 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
237 uint8_t data
[CFI_MAX_BUS_WIDTH
* 4];
240 if (cfi_info
->x16_as_x8
)
243 for (i
= 0;i
< 4;i
++)
245 retval
= target_read_memory(target
, flash_address(bank
, sector
, offset
+ i
), bank
->bus_width
, 1,
246 &data
[i
*bank
->bus_width
]);
247 if (retval
!= ERROR_OK
)
253 retval
= target_read_memory(target
, flash_address(bank
, sector
, offset
), bank
->bus_width
, 4, data
);
254 if (retval
!= ERROR_OK
)
258 if (bank
->target
->endianness
== TARGET_LITTLE_ENDIAN
)
259 *val
= data
[0] | data
[bank
->bus_width
] << 8 | data
[bank
->bus_width
* 2] << 16 | data
[bank
->bus_width
* 3] << 24;
261 *val
= data
[bank
->bus_width
- 1] | data
[(2* bank
->bus_width
) - 1] << 8 |
262 data
[(3 * bank
->bus_width
) - 1] << 16 | data
[(4 * bank
->bus_width
) - 1] << 24;
267 static int cfi_reset(struct flash_bank
*bank
)
269 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
270 int retval
= ERROR_OK
;
272 if ((retval
= cfi_send_command(bank
, 0xf0, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
277 if ((retval
= cfi_send_command(bank
, 0xff, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
282 if (cfi_info
->manufacturer
== 0x20 &&
283 (cfi_info
->device_id
== 0x227E || cfi_info
->device_id
== 0x7E))
285 /* Numonix M29W128G is cmd 0xFF intolerant - causes internal undefined state
286 * so we send an extra 0xF0 reset to fix the bug */
287 if ((retval
= cfi_send_command(bank
, 0xf0, flash_address(bank
, 0, 0x00))) != ERROR_OK
)
296 static void cfi_intel_clear_status_register(struct flash_bank
*bank
)
298 struct target
*target
= bank
->target
;
300 if (target
->state
!= TARGET_HALTED
)
302 LOG_ERROR("BUG: attempted to clear status register while target wasn't halted");
306 cfi_send_command(bank
, 0x50, flash_address(bank
, 0, 0x0));
309 static int cfi_intel_wait_status_busy(struct flash_bank
*bank
, int timeout
, uint8_t *val
)
313 int retval
= ERROR_OK
;
319 LOG_ERROR("timeout while waiting for WSM to become ready");
323 retval
= cfi_get_u8(bank
, 0, 0x0, &status
);
324 if (retval
!= ERROR_OK
)
333 /* mask out bit 0 (reserved) */
334 status
= status
& 0xfe;
336 LOG_DEBUG("status: 0x%x", status
);
340 LOG_ERROR("status register: 0x%x", status
);
342 LOG_ERROR("Block Lock-Bit Detected, Operation Abort");
344 LOG_ERROR("Program suspended");
346 LOG_ERROR("Low Programming Voltage Detected, Operation Aborted");
348 LOG_ERROR("Program Error / Error in Setting Lock-Bit");
350 LOG_ERROR("Error in Block Erasure or Clear Lock-Bits");
352 LOG_ERROR("Block Erase Suspended");
354 cfi_intel_clear_status_register(bank
);
363 static int cfi_spansion_wait_status_busy(struct flash_bank
*bank
, int timeout
)
365 uint8_t status
, oldstatus
;
366 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
369 retval
= cfi_get_u8(bank
, 0, 0x0, &oldstatus
);
370 if (retval
!= ERROR_OK
)
374 retval
= cfi_get_u8(bank
, 0, 0x0, &status
);
376 if (retval
!= ERROR_OK
)
379 if ((status
^ oldstatus
) & 0x40) {
380 if (status
& cfi_info
->status_poll_mask
& 0x20) {
381 retval
= cfi_get_u8(bank
, 0, 0x0, &oldstatus
);
382 if (retval
!= ERROR_OK
)
384 retval
= cfi_get_u8(bank
, 0, 0x0, &status
);
385 if (retval
!= ERROR_OK
)
387 if ((status
^ oldstatus
) & 0x40) {
388 LOG_ERROR("dq5 timeout, status: 0x%x", status
);
389 return(ERROR_FLASH_OPERATION_FAILED
);
391 LOG_DEBUG("status: 0x%x", status
);
395 } else { /* no toggle: finished, OK */
396 LOG_DEBUG("status: 0x%x", status
);
402 } while (timeout
-- > 0);
404 LOG_ERROR("timeout, status: 0x%x", status
);
406 return(ERROR_FLASH_BUSY
);
409 static int cfi_read_intel_pri_ext(struct flash_bank
*bank
)
412 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
413 struct cfi_intel_pri_ext
*pri_ext
;
415 if (cfi_info
->pri_ext
)
416 free(cfi_info
->pri_ext
);
418 pri_ext
= malloc(sizeof(struct cfi_intel_pri_ext
));
421 LOG_ERROR("Out of memory");
424 cfi_info
->pri_ext
= pri_ext
;
426 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 0, &pri_ext
->pri
[0]);
427 if (retval
!= ERROR_OK
)
429 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 1, &pri_ext
->pri
[1]);
430 if (retval
!= ERROR_OK
)
432 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 2, &pri_ext
->pri
[2]);
433 if (retval
!= ERROR_OK
)
436 if ((pri_ext
->pri
[0] != 'P') || (pri_ext
->pri
[1] != 'R') || (pri_ext
->pri
[2] != 'I'))
438 if ((retval
= cfi_reset(bank
)) != ERROR_OK
)
442 LOG_ERROR("Could not read bank flash bank information");
443 return ERROR_FLASH_BANK_INVALID
;
446 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 3, &pri_ext
->major_version
);
447 if (retval
!= ERROR_OK
)
449 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 4, &pri_ext
->minor_version
);
450 if (retval
!= ERROR_OK
)
453 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
);
455 retval
= cfi_query_u32(bank
, 0, cfi_info
->pri_addr
+ 5, &pri_ext
->feature_support
);
456 if (retval
!= ERROR_OK
)
458 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 9, &pri_ext
->suspend_cmd_support
);
459 if (retval
!= ERROR_OK
)
461 retval
= cfi_query_u16(bank
, 0, cfi_info
->pri_addr
+ 0xa, &pri_ext
->blk_status_reg_mask
);
462 if (retval
!= ERROR_OK
)
465 LOG_DEBUG("feature_support: 0x%" PRIx32
", suspend_cmd_support: 0x%x, blk_status_reg_mask: 0x%x",
466 pri_ext
->feature_support
,
467 pri_ext
->suspend_cmd_support
,
468 pri_ext
->blk_status_reg_mask
);
470 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 0xc, &pri_ext
->vcc_optimal
);
471 if (retval
!= ERROR_OK
)
473 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 0xd, &pri_ext
->vpp_optimal
);
474 if (retval
!= ERROR_OK
)
477 LOG_DEBUG("Vcc opt: %x.%x, Vpp opt: %u.%x",
478 (pri_ext
->vcc_optimal
& 0xf0) >> 4, pri_ext
->vcc_optimal
& 0x0f,
479 (pri_ext
->vpp_optimal
& 0xf0) >> 4, pri_ext
->vpp_optimal
& 0x0f);
481 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 0xe, &pri_ext
->num_protection_fields
);
482 if (retval
!= ERROR_OK
)
484 if (pri_ext
->num_protection_fields
!= 1)
486 LOG_WARNING("expected one protection register field, but found %i", pri_ext
->num_protection_fields
);
489 retval
= cfi_query_u16(bank
, 0, cfi_info
->pri_addr
+ 0xf, &pri_ext
->prot_reg_addr
);
490 if (retval
!= ERROR_OK
)
492 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 0x11, &pri_ext
->fact_prot_reg_size
);
493 if (retval
!= ERROR_OK
)
495 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 0x12, &pri_ext
->user_prot_reg_size
);
496 if (retval
!= ERROR_OK
)
499 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
);
504 static int cfi_read_spansion_pri_ext(struct flash_bank
*bank
)
507 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
508 struct cfi_spansion_pri_ext
*pri_ext
;
510 if (cfi_info
->pri_ext
)
511 free(cfi_info
->pri_ext
);
513 pri_ext
= malloc(sizeof(struct cfi_spansion_pri_ext
));
516 LOG_ERROR("Out of memory");
519 cfi_info
->pri_ext
= pri_ext
;
521 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 0, &pri_ext
->pri
[0]);
522 if (retval
!= ERROR_OK
)
524 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 1, &pri_ext
->pri
[1]);
525 if (retval
!= ERROR_OK
)
527 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 2, &pri_ext
->pri
[2]);
528 if (retval
!= ERROR_OK
)
531 if ((pri_ext
->pri
[0] != 'P') || (pri_ext
->pri
[1] != 'R') || (pri_ext
->pri
[2] != 'I'))
533 if ((retval
= cfi_send_command(bank
, 0xf0, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
537 LOG_ERROR("Could not read spansion bank information");
538 return ERROR_FLASH_BANK_INVALID
;
541 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 3, &pri_ext
->major_version
);
542 if (retval
!= ERROR_OK
)
544 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 4, &pri_ext
->minor_version
);
545 if (retval
!= ERROR_OK
)
548 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
);
550 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 5, &pri_ext
->SiliconRevision
);
551 if (retval
!= ERROR_OK
)
553 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 6, &pri_ext
->EraseSuspend
);
554 if (retval
!= ERROR_OK
)
556 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 7, &pri_ext
->BlkProt
);
557 if (retval
!= ERROR_OK
)
559 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 8, &pri_ext
->TmpBlkUnprotect
);
560 if (retval
!= ERROR_OK
)
562 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 9, &pri_ext
->BlkProtUnprot
);
563 if (retval
!= ERROR_OK
)
565 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 10, &pri_ext
->SimultaneousOps
);
566 if (retval
!= ERROR_OK
)
568 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 11, &pri_ext
->BurstMode
);
569 if (retval
!= ERROR_OK
)
571 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 12, &pri_ext
->PageMode
);
572 if (retval
!= ERROR_OK
)
574 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 13, &pri_ext
->VppMin
);
575 if (retval
!= ERROR_OK
)
577 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 14, &pri_ext
->VppMax
);
578 if (retval
!= ERROR_OK
)
580 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 15, &pri_ext
->TopBottom
);
581 if (retval
!= ERROR_OK
)
584 LOG_DEBUG("Silicon Revision: 0x%x, Erase Suspend: 0x%x, Block protect: 0x%x", pri_ext
->SiliconRevision
,
585 pri_ext
->EraseSuspend
, pri_ext
->BlkProt
);
587 LOG_DEBUG("Temporary Unprotect: 0x%x, Block Protect Scheme: 0x%x, Simultaneous Ops: 0x%x", pri_ext
->TmpBlkUnprotect
,
588 pri_ext
->BlkProtUnprot
, pri_ext
->SimultaneousOps
);
590 LOG_DEBUG("Burst Mode: 0x%x, Page Mode: 0x%x, ", pri_ext
->BurstMode
, pri_ext
->PageMode
);
593 LOG_DEBUG("Vpp min: %u.%x, Vpp max: %u.%x",
594 (pri_ext
->VppMin
& 0xf0) >> 4, pri_ext
->VppMin
& 0x0f,
595 (pri_ext
->VppMax
& 0xf0) >> 4, pri_ext
->VppMax
& 0x0f);
597 LOG_DEBUG("WP# protection 0x%x", pri_ext
->TopBottom
);
599 /* default values for implementation specific workarounds */
600 pri_ext
->_unlock1
= cfi_unlock_addresses
[CFI_UNLOCK_555_2AA
].unlock1
;
601 pri_ext
->_unlock2
= cfi_unlock_addresses
[CFI_UNLOCK_555_2AA
].unlock2
;
602 pri_ext
->_reversed_geometry
= 0;
607 static int cfi_read_atmel_pri_ext(struct flash_bank
*bank
)
610 struct cfi_atmel_pri_ext atmel_pri_ext
;
611 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
612 struct cfi_spansion_pri_ext
*pri_ext
;
614 if (cfi_info
->pri_ext
)
615 free(cfi_info
->pri_ext
);
617 pri_ext
= malloc(sizeof(struct cfi_spansion_pri_ext
));
620 LOG_ERROR("Out of memory");
624 /* ATMEL devices use the same CFI primary command set (0x2) as AMD/Spansion,
625 * but a different primary extended query table.
626 * We read the atmel table, and prepare a valid AMD/Spansion query table.
629 memset(pri_ext
, 0, sizeof(struct cfi_spansion_pri_ext
));
631 cfi_info
->pri_ext
= pri_ext
;
633 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 0, &atmel_pri_ext
.pri
[0]);
634 if (retval
!= ERROR_OK
)
636 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 1, &atmel_pri_ext
.pri
[1]);
637 if (retval
!= ERROR_OK
)
639 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 2, &atmel_pri_ext
.pri
[2]);
640 if (retval
!= ERROR_OK
)
643 if ((atmel_pri_ext
.pri
[0] != 'P') || (atmel_pri_ext
.pri
[1] != 'R') || (atmel_pri_ext
.pri
[2] != 'I'))
645 if ((retval
= cfi_send_command(bank
, 0xf0, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
649 LOG_ERROR("Could not read atmel bank information");
650 return ERROR_FLASH_BANK_INVALID
;
653 pri_ext
->pri
[0] = atmel_pri_ext
.pri
[0];
654 pri_ext
->pri
[1] = atmel_pri_ext
.pri
[1];
655 pri_ext
->pri
[2] = atmel_pri_ext
.pri
[2];
657 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 3, &atmel_pri_ext
.major_version
);
658 if (retval
!= ERROR_OK
)
660 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 4, &atmel_pri_ext
.minor_version
);
661 if (retval
!= ERROR_OK
)
664 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
);
666 pri_ext
->major_version
= atmel_pri_ext
.major_version
;
667 pri_ext
->minor_version
= atmel_pri_ext
.minor_version
;
669 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 5, &atmel_pri_ext
.features
);
670 if (retval
!= ERROR_OK
)
672 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 6, &atmel_pri_ext
.bottom_boot
);
673 if (retval
!= ERROR_OK
)
675 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 7, &atmel_pri_ext
.burst_mode
);
676 if (retval
!= ERROR_OK
)
678 retval
= cfi_query_u8(bank
, 0, cfi_info
->pri_addr
+ 8, &atmel_pri_ext
.page_mode
);
679 if (retval
!= ERROR_OK
)
682 LOG_DEBUG("features: 0x%2.2x, bottom_boot: 0x%2.2x, burst_mode: 0x%2.2x, page_mode: 0x%2.2x",
683 atmel_pri_ext
.features
, atmel_pri_ext
.bottom_boot
, atmel_pri_ext
.burst_mode
, atmel_pri_ext
.page_mode
);
685 if (atmel_pri_ext
.features
& 0x02)
686 pri_ext
->EraseSuspend
= 2;
688 if (atmel_pri_ext
.bottom_boot
)
689 pri_ext
->TopBottom
= 2;
691 pri_ext
->TopBottom
= 3;
693 pri_ext
->_unlock1
= cfi_unlock_addresses
[CFI_UNLOCK_555_2AA
].unlock1
;
694 pri_ext
->_unlock2
= cfi_unlock_addresses
[CFI_UNLOCK_555_2AA
].unlock2
;
699 static int cfi_read_0002_pri_ext(struct flash_bank
*bank
)
701 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
703 if (cfi_info
->manufacturer
== CFI_MFR_ATMEL
)
705 return cfi_read_atmel_pri_ext(bank
);
709 return cfi_read_spansion_pri_ext(bank
);
713 static int cfi_spansion_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
716 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
717 struct cfi_spansion_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
719 printed
= snprintf(buf
, buf_size
, "\nSpansion primary algorithm extend information:\n");
723 printed
= snprintf(buf
, buf_size
, "pri: '%c%c%c', version: %c.%c\n", pri_ext
->pri
[0],
724 pri_ext
->pri
[1], pri_ext
->pri
[2],
725 pri_ext
->major_version
, pri_ext
->minor_version
);
729 printed
= snprintf(buf
, buf_size
, "Silicon Rev.: 0x%x, Address Sensitive unlock: 0x%x\n",
730 (pri_ext
->SiliconRevision
) >> 2,
731 (pri_ext
->SiliconRevision
) & 0x03);
735 printed
= snprintf(buf
, buf_size
, "Erase Suspend: 0x%x, Sector Protect: 0x%x\n",
736 pri_ext
->EraseSuspend
,
741 printed
= snprintf(buf
, buf_size
, "VppMin: %u.%x, VppMax: %u.%x\n",
742 (pri_ext
->VppMin
& 0xf0) >> 4, pri_ext
->VppMin
& 0x0f,
743 (pri_ext
->VppMax
& 0xf0) >> 4, pri_ext
->VppMax
& 0x0f);
748 static int cfi_intel_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
751 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
752 struct cfi_intel_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
754 printed
= snprintf(buf
, buf_size
, "\nintel primary algorithm extend information:\n");
758 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
);
762 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
);
766 printed
= snprintf(buf
, buf_size
, "Vcc opt: %x.%x, Vpp opt: %u.%x\n",
767 (pri_ext
->vcc_optimal
& 0xf0) >> 4, pri_ext
->vcc_optimal
& 0x0f,
768 (pri_ext
->vpp_optimal
& 0xf0) >> 4, pri_ext
->vpp_optimal
& 0x0f);
772 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
);
777 /* flash_bank cfi <base> <size> <chip_width> <bus_width> <target#> [options]
779 FLASH_BANK_COMMAND_HANDLER(cfi_flash_bank_command
)
781 struct cfi_flash_bank
*cfi_info
;
785 LOG_WARNING("incomplete flash_bank cfi configuration");
786 return ERROR_FLASH_BANK_INVALID
;
790 * - not exceed max value;
792 * - be equal to a power of 2.
793 * bus must be wide enought to hold one chip */
794 if ((bank
->chip_width
> CFI_MAX_CHIP_WIDTH
)
795 || (bank
->bus_width
> CFI_MAX_BUS_WIDTH
)
796 || (bank
->chip_width
== 0)
797 || (bank
->bus_width
== 0)
798 || (bank
->chip_width
& (bank
->chip_width
- 1))
799 || (bank
->bus_width
& (bank
->bus_width
- 1))
800 || (bank
->chip_width
> bank
->bus_width
))
802 LOG_ERROR("chip and bus width have to specified in bytes");
803 return ERROR_FLASH_BANK_INVALID
;
806 cfi_info
= malloc(sizeof(struct cfi_flash_bank
));
807 cfi_info
->probed
= 0;
808 cfi_info
->erase_region_info
= NULL
;
809 cfi_info
->pri_ext
= NULL
;
810 bank
->driver_priv
= cfi_info
;
812 cfi_info
->write_algorithm
= NULL
;
814 cfi_info
->x16_as_x8
= 0;
815 cfi_info
->jedec_probe
= 0;
816 cfi_info
->not_cfi
= 0;
818 for (unsigned i
= 6; i
< CMD_ARGC
; i
++)
820 if (strcmp(CMD_ARGV
[i
], "x16_as_x8") == 0)
822 cfi_info
->x16_as_x8
= 1;
824 else if (strcmp(CMD_ARGV
[i
], "jedec_probe") == 0)
826 cfi_info
->jedec_probe
= 1;
830 cfi_info
->write_algorithm
= NULL
;
832 /* bank wasn't probed yet */
833 cfi_info
->qry
[0] = 0xff;
838 static int cfi_intel_erase(struct flash_bank
*bank
, int first
, int last
)
841 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
844 cfi_intel_clear_status_register(bank
);
846 for (i
= first
; i
<= last
; i
++)
848 if ((retval
= cfi_send_command(bank
, 0x20, flash_address(bank
, i
, 0x0))) != ERROR_OK
)
853 if ((retval
= cfi_send_command(bank
, 0xd0, flash_address(bank
, i
, 0x0))) != ERROR_OK
)
859 retval
= cfi_intel_wait_status_busy(bank
, 1000 * (1 << cfi_info
->block_erase_timeout_typ
), &status
);
860 if (retval
!= ERROR_OK
)
864 bank
->sectors
[i
].is_erased
= 1;
867 if ((retval
= cfi_send_command(bank
, 0xff, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
872 LOG_ERROR("couldn't erase block %i of flash bank at base 0x%" PRIx32
, i
, bank
->base
);
873 return ERROR_FLASH_OPERATION_FAILED
;
877 return cfi_send_command(bank
, 0xff, flash_address(bank
, 0, 0x0));
880 static int cfi_spansion_erase(struct flash_bank
*bank
, int first
, int last
)
883 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
884 struct cfi_spansion_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
887 for (i
= first
; i
<= last
; i
++)
889 if ((retval
= cfi_send_command(bank
, 0xaa, flash_address(bank
, 0, pri_ext
->_unlock1
))) != ERROR_OK
)
894 if ((retval
= cfi_send_command(bank
, 0x55, flash_address(bank
, 0, pri_ext
->_unlock2
))) != ERROR_OK
)
899 if ((retval
= cfi_send_command(bank
, 0x80, flash_address(bank
, 0, pri_ext
->_unlock1
))) != ERROR_OK
)
904 if ((retval
= cfi_send_command(bank
, 0xaa, flash_address(bank
, 0, pri_ext
->_unlock1
))) != ERROR_OK
)
909 if ((retval
= cfi_send_command(bank
, 0x55, flash_address(bank
, 0, pri_ext
->_unlock2
))) != ERROR_OK
)
914 if ((retval
= cfi_send_command(bank
, 0x30, flash_address(bank
, i
, 0x0))) != ERROR_OK
)
919 if (cfi_spansion_wait_status_busy(bank
, 1000 * (1 << cfi_info
->block_erase_timeout_typ
)) == ERROR_OK
)
920 bank
->sectors
[i
].is_erased
= 1;
923 if ((retval
= cfi_send_command(bank
, 0xf0, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
928 LOG_ERROR("couldn't erase block %i of flash bank at base 0x%" PRIx32
, i
, bank
->base
);
929 return ERROR_FLASH_OPERATION_FAILED
;
933 return cfi_send_command(bank
, 0xf0, flash_address(bank
, 0, 0x0));
936 static int cfi_erase(struct flash_bank
*bank
, int first
, int last
)
938 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
940 if (bank
->target
->state
!= TARGET_HALTED
)
942 LOG_ERROR("Target not halted");
943 return ERROR_TARGET_NOT_HALTED
;
946 if ((first
< 0) || (last
< first
) || (last
>= bank
->num_sectors
))
948 return ERROR_FLASH_SECTOR_INVALID
;
951 if (cfi_info
->qry
[0] != 'Q')
952 return ERROR_FLASH_BANK_NOT_PROBED
;
954 switch (cfi_info
->pri_id
)
958 return cfi_intel_erase(bank
, first
, last
);
961 return cfi_spansion_erase(bank
, first
, last
);
964 LOG_ERROR("cfi primary command set %i unsupported", cfi_info
->pri_id
);
971 static int cfi_intel_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
974 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
975 struct cfi_intel_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
979 /* if the device supports neither legacy lock/unlock (bit 3) nor
980 * instant individual block locking (bit 5).
982 if (!(pri_ext
->feature_support
& 0x28))
984 LOG_ERROR("lock/unlock not supported on flash");
985 return ERROR_FLASH_OPERATION_FAILED
;
988 cfi_intel_clear_status_register(bank
);
990 for (i
= first
; i
<= last
; i
++)
992 if ((retval
= cfi_send_command(bank
, 0x60, flash_address(bank
, i
, 0x0))) != ERROR_OK
)
998 if ((retval
= cfi_send_command(bank
, 0x01, flash_address(bank
, i
, 0x0))) != ERROR_OK
)
1002 bank
->sectors
[i
].is_protected
= 1;
1006 if ((retval
= cfi_send_command(bank
, 0xd0, flash_address(bank
, i
, 0x0))) != ERROR_OK
)
1010 bank
->sectors
[i
].is_protected
= 0;
1013 /* instant individual block locking doesn't require reading of the status register */
1014 if (!(pri_ext
->feature_support
& 0x20))
1016 /* Clear lock bits operation may take up to 1.4s */
1018 retval
= cfi_intel_wait_status_busy(bank
, 1400, &status
);
1019 if (retval
!= ERROR_OK
)
1024 uint8_t block_status
;
1025 /* read block lock bit, to verify status */
1026 if ((retval
= cfi_send_command(bank
, 0x90, flash_address(bank
, 0, 0x55))) != ERROR_OK
)
1030 retval
= cfi_get_u8(bank
, i
, 0x2, &block_status
);
1031 if (retval
!= ERROR_OK
)
1034 if ((block_status
& 0x1) != set
)
1036 LOG_ERROR("couldn't change block lock status (set = %i, block_status = 0x%2.2x)", set
, block_status
);
1037 if ((retval
= cfi_send_command(bank
, 0x70, flash_address(bank
, 0, 0x55))) != ERROR_OK
)
1042 retval
= cfi_intel_wait_status_busy(bank
, 10, &status
);
1043 if (retval
!= ERROR_OK
)
1047 return ERROR_FLASH_OPERATION_FAILED
;
1057 /* if the device doesn't support individual block lock bits set/clear,
1058 * all blocks have been unlocked in parallel, so we set those that should be protected
1060 if ((!set
) && (!(pri_ext
->feature_support
& 0x20)))
1062 /* FIX!!! this code path is broken!!!
1064 * The correct approach is:
1066 * 1. read out current protection status
1068 * 2. override read out protection status w/unprotected.
1070 * 3. re-protect what should be protected.
1073 for (i
= 0; i
< bank
->num_sectors
; i
++)
1075 if (bank
->sectors
[i
].is_protected
== 1)
1077 cfi_intel_clear_status_register(bank
);
1079 if ((retval
= cfi_send_command(bank
, 0x60, flash_address(bank
, i
, 0x0))) != ERROR_OK
)
1084 if ((retval
= cfi_send_command(bank
, 0x01, flash_address(bank
, i
, 0x0))) != ERROR_OK
)
1090 retval
= cfi_intel_wait_status_busy(bank
, 100, &status
);
1091 if (retval
!= ERROR_OK
)
1097 return cfi_send_command(bank
, 0xff, flash_address(bank
, 0, 0x0));
1100 static int cfi_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
1102 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
1104 if (bank
->target
->state
!= TARGET_HALTED
)
1106 LOG_ERROR("Target not halted");
1107 return ERROR_TARGET_NOT_HALTED
;
1110 if ((first
< 0) || (last
< first
) || (last
>= bank
->num_sectors
))
1112 LOG_ERROR("Invalid sector range");
1113 return ERROR_FLASH_SECTOR_INVALID
;
1116 if (cfi_info
->qry
[0] != 'Q')
1117 return ERROR_FLASH_BANK_NOT_PROBED
;
1119 switch (cfi_info
->pri_id
)
1123 return cfi_intel_protect(bank
, set
, first
, last
);
1126 LOG_ERROR("protect: cfi primary command set %i unsupported", cfi_info
->pri_id
);
1131 /* Convert code image to target endian */
1132 /* FIXME create general block conversion fcts in target.c?) */
1133 static void cfi_fix_code_endian(struct target
*target
, uint8_t *dest
, const uint32_t *src
, uint32_t count
)
1136 for (i
= 0; i
< count
; i
++)
1138 target_buffer_set_u32(target
, dest
, *src
);
1144 static uint32_t cfi_command_val(struct flash_bank
*bank
, uint8_t cmd
)
1146 struct target
*target
= bank
->target
;
1148 uint8_t buf
[CFI_MAX_BUS_WIDTH
];
1149 cfi_command(bank
, cmd
, buf
);
1150 switch (bank
->bus_width
)
1156 return target_buffer_get_u16(target
, buf
);
1159 return target_buffer_get_u32(target
, buf
);
1162 LOG_ERROR("Unsupported bank buswidth %d, can't do block memory writes", bank
->bus_width
);
1167 static int cfi_intel_write_block(struct flash_bank
*bank
, uint8_t *buffer
, uint32_t address
, uint32_t count
)
1169 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
1170 struct target
*target
= bank
->target
;
1171 struct reg_param reg_params
[7];
1172 struct arm_algorithm armv4_5_info
;
1173 struct working_area
*source
;
1174 uint32_t buffer_size
= 32768;
1175 uint32_t write_command_val
, busy_pattern_val
, error_pattern_val
;
1177 /* algorithm register usage:
1178 * r0: source address (in RAM)
1179 * r1: target address (in Flash)
1181 * r3: flash write command
1182 * r4: status byte (returned to host)
1183 * r5: busy test pattern
1184 * r6: error test pattern
1187 static const uint32_t word_32_code
[] = {
1188 0xe4904004, /* loop: ldr r4, [r0], #4 */
1189 0xe5813000, /* str r3, [r1] */
1190 0xe5814000, /* str r4, [r1] */
1191 0xe5914000, /* busy: ldr r4, [r1] */
1192 0xe0047005, /* and r7, r4, r5 */
1193 0xe1570005, /* cmp r7, r5 */
1194 0x1afffffb, /* bne busy */
1195 0xe1140006, /* tst r4, r6 */
1196 0x1a000003, /* bne done */
1197 0xe2522001, /* subs r2, r2, #1 */
1198 0x0a000001, /* beq done */
1199 0xe2811004, /* add r1, r1 #4 */
1200 0xeafffff2, /* b loop */
1201 0xeafffffe /* done: b -2 */
1204 static const uint32_t word_16_code
[] = {
1205 0xe0d040b2, /* loop: ldrh r4, [r0], #2 */
1206 0xe1c130b0, /* strh r3, [r1] */
1207 0xe1c140b0, /* strh r4, [r1] */
1208 0xe1d140b0, /* busy ldrh r4, [r1] */
1209 0xe0047005, /* and r7, r4, r5 */
1210 0xe1570005, /* cmp r7, r5 */
1211 0x1afffffb, /* bne busy */
1212 0xe1140006, /* tst r4, r6 */
1213 0x1a000003, /* bne done */
1214 0xe2522001, /* subs r2, r2, #1 */
1215 0x0a000001, /* beq done */
1216 0xe2811002, /* add r1, r1 #2 */
1217 0xeafffff2, /* b loop */
1218 0xeafffffe /* done: b -2 */
1221 static const uint32_t word_8_code
[] = {
1222 0xe4d04001, /* loop: ldrb r4, [r0], #1 */
1223 0xe5c13000, /* strb r3, [r1] */
1224 0xe5c14000, /* strb r4, [r1] */
1225 0xe5d14000, /* busy ldrb r4, [r1] */
1226 0xe0047005, /* and r7, r4, r5 */
1227 0xe1570005, /* cmp r7, r5 */
1228 0x1afffffb, /* bne busy */
1229 0xe1140006, /* tst r4, r6 */
1230 0x1a000003, /* bne done */
1231 0xe2522001, /* subs r2, r2, #1 */
1232 0x0a000001, /* beq done */
1233 0xe2811001, /* add r1, r1 #1 */
1234 0xeafffff2, /* b loop */
1235 0xeafffffe /* done: b -2 */
1237 uint8_t target_code
[4*CFI_MAX_INTEL_CODESIZE
];
1238 const uint32_t *target_code_src
;
1239 uint32_t target_code_size
;
1240 int retval
= ERROR_OK
;
1243 cfi_intel_clear_status_register(bank
);
1245 armv4_5_info
.common_magic
= ARM_COMMON_MAGIC
;
1246 armv4_5_info
.core_mode
= ARM_MODE_SVC
;
1247 armv4_5_info
.core_state
= ARM_STATE_ARM
;
1249 /* If we are setting up the write_algorith, we need target_code_src */
1250 /* if not we only need target_code_size. */
1252 /* However, we don't want to create multiple code paths, so we */
1253 /* do the unecessary evaluation of target_code_src, which the */
1254 /* compiler will probably nicely optimize away if not needed */
1256 /* prepare algorithm code for target endian */
1257 switch (bank
->bus_width
)
1260 target_code_src
= word_8_code
;
1261 target_code_size
= sizeof(word_8_code
);
1264 target_code_src
= word_16_code
;
1265 target_code_size
= sizeof(word_16_code
);
1268 target_code_src
= word_32_code
;
1269 target_code_size
= sizeof(word_32_code
);
1272 LOG_ERROR("Unsupported bank buswidth %d, can't do block memory writes", bank
->bus_width
);
1273 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1276 /* flash write code */
1277 if (!cfi_info
->write_algorithm
)
1279 if (target_code_size
> sizeof(target_code
))
1281 LOG_WARNING("Internal error - target code buffer to small. Increase CFI_MAX_INTEL_CODESIZE and recompile.");
1282 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1284 cfi_fix_code_endian(target
, target_code
, target_code_src
, target_code_size
/ 4);
1286 /* Get memory for block write handler */
1287 retval
= target_alloc_working_area(target
, target_code_size
, &cfi_info
->write_algorithm
);
1288 if (retval
!= ERROR_OK
)
1290 LOG_WARNING("No working area available, can't do block memory writes");
1291 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1294 /* write algorithm code to working area */
1295 retval
= target_write_buffer(target
, cfi_info
->write_algorithm
->address
, target_code_size
, target_code
);
1296 if (retval
!= ERROR_OK
)
1298 LOG_ERROR("Unable to write block write code to target");
1303 /* Get a workspace buffer for the data to flash starting with 32k size.
1304 Half size until buffer would be smaller 256 Bytem then fail back */
1305 /* FIXME Why 256 bytes, why not 32 bytes (smallest flash write page */
1306 while (target_alloc_working_area_try(target
, buffer_size
, &source
) != ERROR_OK
)
1309 if (buffer_size
<= 256)
1311 LOG_WARNING("no large enough working area available, can't do block memory writes");
1312 retval
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1317 /* setup algo registers */
1318 init_reg_param(®_params
[0], "r0", 32, PARAM_OUT
);
1319 init_reg_param(®_params
[1], "r1", 32, PARAM_OUT
);
1320 init_reg_param(®_params
[2], "r2", 32, PARAM_OUT
);
1321 init_reg_param(®_params
[3], "r3", 32, PARAM_OUT
);
1322 init_reg_param(®_params
[4], "r4", 32, PARAM_IN
);
1323 init_reg_param(®_params
[5], "r5", 32, PARAM_OUT
);
1324 init_reg_param(®_params
[6], "r6", 32, PARAM_OUT
);
1326 /* prepare command and status register patterns */
1327 write_command_val
= cfi_command_val(bank
, 0x40);
1328 busy_pattern_val
= cfi_command_val(bank
, 0x80);
1329 error_pattern_val
= cfi_command_val(bank
, 0x7e);
1331 LOG_DEBUG("Using target buffer at 0x%08" PRIx32
" and of size 0x%04" PRIx32
, source
->address
, buffer_size
);
1333 /* Programming main loop */
1336 uint32_t thisrun_count
= (count
> buffer_size
) ? buffer_size
: count
;
1339 if ((retval
= target_write_buffer(target
, source
->address
, thisrun_count
, buffer
)) != ERROR_OK
)
1344 buf_set_u32(reg_params
[0].value
, 0, 32, source
->address
);
1345 buf_set_u32(reg_params
[1].value
, 0, 32, address
);
1346 buf_set_u32(reg_params
[2].value
, 0, 32, thisrun_count
/ bank
->bus_width
);
1348 buf_set_u32(reg_params
[3].value
, 0, 32, write_command_val
);
1349 buf_set_u32(reg_params
[5].value
, 0, 32, busy_pattern_val
);
1350 buf_set_u32(reg_params
[6].value
, 0, 32, error_pattern_val
);
1352 LOG_DEBUG("Write 0x%04" PRIx32
" bytes to flash at 0x%08" PRIx32
, thisrun_count
, address
);
1354 /* Execute algorithm, assume breakpoint for last instruction */
1355 retval
= target_run_algorithm(target
, 0, NULL
, 7, reg_params
,
1356 cfi_info
->write_algorithm
->address
,
1357 cfi_info
->write_algorithm
->address
+ target_code_size
- sizeof(uint32_t),
1358 10000, /* 10s should be enough for max. 32k of data */
1361 /* On failure try a fall back to direct word writes */
1362 if (retval
!= ERROR_OK
)
1364 cfi_intel_clear_status_register(bank
);
1365 LOG_ERROR("Execution of flash algorythm failed. Can't fall back. Please report.");
1366 retval
= ERROR_FLASH_OPERATION_FAILED
;
1367 /* retval = ERROR_TARGET_RESOURCE_NOT_AVAILABLE; */
1368 /* FIXME To allow fall back or recovery, we must save the actual status
1369 somewhere, so that a higher level code can start recovery. */
1373 /* Check return value from algo code */
1374 wsm_error
= buf_get_u32(reg_params
[4].value
, 0, 32) & error_pattern_val
;
1377 /* read status register (outputs debug inforation) */
1379 cfi_intel_wait_status_busy(bank
, 100, &status
);
1380 cfi_intel_clear_status_register(bank
);
1381 retval
= ERROR_FLASH_OPERATION_FAILED
;
1385 buffer
+= thisrun_count
;
1386 address
+= thisrun_count
;
1387 count
-= thisrun_count
;
1392 /* free up resources */
1395 target_free_working_area(target
, source
);
1397 if (cfi_info
->write_algorithm
)
1399 target_free_working_area(target
, cfi_info
->write_algorithm
);
1400 cfi_info
->write_algorithm
= NULL
;
1403 destroy_reg_param(®_params
[0]);
1404 destroy_reg_param(®_params
[1]);
1405 destroy_reg_param(®_params
[2]);
1406 destroy_reg_param(®_params
[3]);
1407 destroy_reg_param(®_params
[4]);
1408 destroy_reg_param(®_params
[5]);
1409 destroy_reg_param(®_params
[6]);
1414 static int cfi_spansion_write_block(struct flash_bank
*bank
, uint8_t *buffer
, uint32_t address
, uint32_t count
)
1416 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
1417 struct cfi_spansion_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
1418 struct target
*target
= bank
->target
;
1419 struct reg_param reg_params
[10];
1420 struct arm_algorithm armv4_5_info
;
1421 struct working_area
*source
;
1422 uint32_t buffer_size
= 32768;
1424 int retval
= ERROR_OK
;
1426 /* input parameters - */
1427 /* R0 = source address */
1428 /* R1 = destination address */
1429 /* R2 = number of writes */
1430 /* R3 = flash write command */
1431 /* R4 = constant to mask DQ7 bits (also used for Dq5 with shift) */
1432 /* output parameters - */
1433 /* R5 = 0x80 ok 0x00 bad */
1434 /* temp registers - */
1435 /* R6 = value read from flash to test status */
1436 /* R7 = holding register */
1437 /* unlock registers - */
1438 /* R8 = unlock1_addr */
1439 /* R9 = unlock1_cmd */
1440 /* R10 = unlock2_addr */
1441 /* R11 = unlock2_cmd */
1443 static const uint32_t word_32_code
[] = {
1444 /* 00008100 <sp_32_code>: */
1445 0xe4905004, /* ldr r5, [r0], #4 */
1446 0xe5889000, /* str r9, [r8] */
1447 0xe58ab000, /* str r11, [r10] */
1448 0xe5883000, /* str r3, [r8] */
1449 0xe5815000, /* str r5, [r1] */
1450 0xe1a00000, /* nop */
1452 /* 00008110 <sp_32_busy>: */
1453 0xe5916000, /* ldr r6, [r1] */
1454 0xe0257006, /* eor r7, r5, r6 */
1455 0xe0147007, /* ands r7, r4, r7 */
1456 0x0a000007, /* beq 8140 <sp_32_cont> ; b if DQ7 == Data7 */
1457 0xe0166124, /* ands r6, r6, r4, lsr #2 */
1458 0x0afffff9, /* beq 8110 <sp_32_busy> ; b if DQ5 low */
1459 0xe5916000, /* ldr r6, [r1] */
1460 0xe0257006, /* eor r7, r5, r6 */
1461 0xe0147007, /* ands r7, r4, r7 */
1462 0x0a000001, /* beq 8140 <sp_32_cont> ; b if DQ7 == Data7 */
1463 0xe3a05000, /* mov r5, #0 ; 0x0 - return 0x00, error */
1464 0x1a000004, /* bne 8154 <sp_32_done> */
1466 /* 00008140 <sp_32_cont>: */
1467 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
1468 0x03a05080, /* moveq r5, #128 ; 0x80 */
1469 0x0a000001, /* beq 8154 <sp_32_done> */
1470 0xe2811004, /* add r1, r1, #4 ; 0x4 */
1471 0xeaffffe8, /* b 8100 <sp_32_code> */
1473 /* 00008154 <sp_32_done>: */
1474 0xeafffffe /* b 8154 <sp_32_done> */
1477 static const uint32_t word_16_code
[] = {
1478 /* 00008158 <sp_16_code>: */
1479 0xe0d050b2, /* ldrh r5, [r0], #2 */
1480 0xe1c890b0, /* strh r9, [r8] */
1481 0xe1cab0b0, /* strh r11, [r10] */
1482 0xe1c830b0, /* strh r3, [r8] */
1483 0xe1c150b0, /* strh r5, [r1] */
1484 0xe1a00000, /* nop (mov r0,r0) */
1486 /* 00008168 <sp_16_busy>: */
1487 0xe1d160b0, /* ldrh r6, [r1] */
1488 0xe0257006, /* eor r7, r5, r6 */
1489 0xe0147007, /* ands r7, r4, r7 */
1490 0x0a000007, /* beq 8198 <sp_16_cont> */
1491 0xe0166124, /* ands r6, r6, r4, lsr #2 */
1492 0x0afffff9, /* beq 8168 <sp_16_busy> */
1493 0xe1d160b0, /* ldrh r6, [r1] */
1494 0xe0257006, /* eor r7, r5, r6 */
1495 0xe0147007, /* ands r7, r4, r7 */
1496 0x0a000001, /* beq 8198 <sp_16_cont> */
1497 0xe3a05000, /* mov r5, #0 ; 0x0 */
1498 0x1a000004, /* bne 81ac <sp_16_done> */
1500 /* 00008198 <sp_16_cont>: */
1501 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
1502 0x03a05080, /* moveq r5, #128 ; 0x80 */
1503 0x0a000001, /* beq 81ac <sp_16_done> */
1504 0xe2811002, /* add r1, r1, #2 ; 0x2 */
1505 0xeaffffe8, /* b 8158 <sp_16_code> */
1507 /* 000081ac <sp_16_done>: */
1508 0xeafffffe /* b 81ac <sp_16_done> */
1511 static const uint32_t word_16_code_dq7only
[] = {
1513 0xe0d050b2, /* ldrh r5, [r0], #2 */
1514 0xe1c890b0, /* strh r9, [r8] */
1515 0xe1cab0b0, /* strh r11, [r10] */
1516 0xe1c830b0, /* strh r3, [r8] */
1517 0xe1c150b0, /* strh r5, [r1] */
1518 0xe1a00000, /* nop (mov r0,r0) */
1521 0xe1d160b0, /* ldrh r6, [r1] */
1522 0xe0257006, /* eor r7, r5, r6 */
1523 0xe2177080, /* ands r7, #0x80 */
1524 0x1afffffb, /* bne 8168 <sp_16_busy> */
1526 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
1527 0x03a05080, /* moveq r5, #128 ; 0x80 */
1528 0x0a000001, /* beq 81ac <sp_16_done> */
1529 0xe2811002, /* add r1, r1, #2 ; 0x2 */
1530 0xeafffff0, /* b 8158 <sp_16_code> */
1532 /* 000081ac <sp_16_done>: */
1533 0xeafffffe /* b 81ac <sp_16_done> */
1536 static const uint32_t word_8_code
[] = {
1537 /* 000081b0 <sp_16_code_end>: */
1538 0xe4d05001, /* ldrb r5, [r0], #1 */
1539 0xe5c89000, /* strb r9, [r8] */
1540 0xe5cab000, /* strb r11, [r10] */
1541 0xe5c83000, /* strb r3, [r8] */
1542 0xe5c15000, /* strb r5, [r1] */
1543 0xe1a00000, /* nop (mov r0,r0) */
1545 /* 000081c0 <sp_8_busy>: */
1546 0xe5d16000, /* ldrb r6, [r1] */
1547 0xe0257006, /* eor r7, r5, r6 */
1548 0xe0147007, /* ands r7, r4, r7 */
1549 0x0a000007, /* beq 81f0 <sp_8_cont> */
1550 0xe0166124, /* ands r6, r6, r4, lsr #2 */
1551 0x0afffff9, /* beq 81c0 <sp_8_busy> */
1552 0xe5d16000, /* ldrb r6, [r1] */
1553 0xe0257006, /* eor r7, r5, r6 */
1554 0xe0147007, /* ands r7, r4, r7 */
1555 0x0a000001, /* beq 81f0 <sp_8_cont> */
1556 0xe3a05000, /* mov r5, #0 ; 0x0 */
1557 0x1a000004, /* bne 8204 <sp_8_done> */
1559 /* 000081f0 <sp_8_cont>: */
1560 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
1561 0x03a05080, /* moveq r5, #128 ; 0x80 */
1562 0x0a000001, /* beq 8204 <sp_8_done> */
1563 0xe2811001, /* add r1, r1, #1 ; 0x1 */
1564 0xeaffffe8, /* b 81b0 <sp_16_code_end> */
1566 /* 00008204 <sp_8_done>: */
1567 0xeafffffe /* b 8204 <sp_8_done> */
1570 armv4_5_info
.common_magic
= ARM_COMMON_MAGIC
;
1571 armv4_5_info
.core_mode
= ARM_MODE_SVC
;
1572 armv4_5_info
.core_state
= ARM_STATE_ARM
;
1574 int target_code_size
;
1575 const uint32_t *target_code_src
;
1577 switch (bank
->bus_width
)
1580 target_code_src
= word_8_code
;
1581 target_code_size
= sizeof(word_8_code
);
1584 /* Check for DQ5 support */
1585 if( cfi_info
->status_poll_mask
& (1 << 5) )
1587 target_code_src
= word_16_code
;
1588 target_code_size
= sizeof(word_16_code
);
1592 /* No DQ5 support. Use DQ7 DATA# polling only. */
1593 target_code_src
= word_16_code_dq7only
;
1594 target_code_size
= sizeof(word_16_code_dq7only
);
1598 target_code_src
= word_32_code
;
1599 target_code_size
= sizeof(word_32_code
);
1602 LOG_ERROR("Unsupported bank buswidth %d, can't do block memory writes", bank
->bus_width
);
1603 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1606 /* flash write code */
1607 if (!cfi_info
->write_algorithm
)
1609 uint8_t *target_code
;
1611 /* convert bus-width dependent algorithm code to correct endiannes */
1612 target_code
= malloc(target_code_size
);
1613 if (target_code
== NULL
)
1615 LOG_ERROR("Out of memory");
1618 cfi_fix_code_endian(target
, target_code
, target_code_src
, target_code_size
/ 4);
1620 /* allocate working area */
1621 retval
= target_alloc_working_area(target
, target_code_size
,
1622 &cfi_info
->write_algorithm
);
1623 if (retval
!= ERROR_OK
)
1629 /* write algorithm code to working area */
1630 if ((retval
= target_write_buffer(target
, cfi_info
->write_algorithm
->address
,
1631 target_code_size
, target_code
)) != ERROR_OK
)
1639 /* the following code still assumes target code is fixed 24*4 bytes */
1641 while (target_alloc_working_area_try(target
, buffer_size
, &source
) != ERROR_OK
)
1644 if (buffer_size
<= 256)
1646 /* if we already allocated the writing code, but failed to get a buffer, free the algorithm */
1647 if (cfi_info
->write_algorithm
)
1648 target_free_working_area(target
, cfi_info
->write_algorithm
);
1650 LOG_WARNING("not enough working area available, can't do block memory writes");
1651 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1655 init_reg_param(®_params
[0], "r0", 32, PARAM_OUT
);
1656 init_reg_param(®_params
[1], "r1", 32, PARAM_OUT
);
1657 init_reg_param(®_params
[2], "r2", 32, PARAM_OUT
);
1658 init_reg_param(®_params
[3], "r3", 32, PARAM_OUT
);
1659 init_reg_param(®_params
[4], "r4", 32, PARAM_OUT
);
1660 init_reg_param(®_params
[5], "r5", 32, PARAM_IN
);
1661 init_reg_param(®_params
[6], "r8", 32, PARAM_OUT
);
1662 init_reg_param(®_params
[7], "r9", 32, PARAM_OUT
);
1663 init_reg_param(®_params
[8], "r10", 32, PARAM_OUT
);
1664 init_reg_param(®_params
[9], "r11", 32, PARAM_OUT
);
1668 uint32_t thisrun_count
= (count
> buffer_size
) ? buffer_size
: count
;
1670 retval
= target_write_buffer(target
, source
->address
, thisrun_count
, buffer
);
1671 if (retval
!= ERROR_OK
)
1676 buf_set_u32(reg_params
[0].value
, 0, 32, source
->address
);
1677 buf_set_u32(reg_params
[1].value
, 0, 32, address
);
1678 buf_set_u32(reg_params
[2].value
, 0, 32, thisrun_count
/ bank
->bus_width
);
1679 buf_set_u32(reg_params
[3].value
, 0, 32, cfi_command_val(bank
, 0xA0));
1680 buf_set_u32(reg_params
[4].value
, 0, 32, cfi_command_val(bank
, 0x80));
1681 buf_set_u32(reg_params
[6].value
, 0, 32, flash_address(bank
, 0, pri_ext
->_unlock1
));
1682 buf_set_u32(reg_params
[7].value
, 0, 32, 0xaaaaaaaa);
1683 buf_set_u32(reg_params
[8].value
, 0, 32, flash_address(bank
, 0, pri_ext
->_unlock2
));
1684 buf_set_u32(reg_params
[9].value
, 0, 32, 0x55555555);
1686 retval
= target_run_algorithm(target
, 0, NULL
, 10, reg_params
,
1687 cfi_info
->write_algorithm
->address
,
1688 cfi_info
->write_algorithm
->address
+ ((target_code_size
) - 4),
1689 10000, &armv4_5_info
);
1690 if (retval
!= ERROR_OK
)
1695 status
= buf_get_u32(reg_params
[5].value
, 0, 32);
1698 LOG_ERROR("flash write block failed status: 0x%" PRIx32
, status
);
1699 retval
= ERROR_FLASH_OPERATION_FAILED
;
1703 buffer
+= thisrun_count
;
1704 address
+= thisrun_count
;
1705 count
-= thisrun_count
;
1708 target_free_all_working_areas(target
);
1710 destroy_reg_param(®_params
[0]);
1711 destroy_reg_param(®_params
[1]);
1712 destroy_reg_param(®_params
[2]);
1713 destroy_reg_param(®_params
[3]);
1714 destroy_reg_param(®_params
[4]);
1715 destroy_reg_param(®_params
[5]);
1716 destroy_reg_param(®_params
[6]);
1717 destroy_reg_param(®_params
[7]);
1718 destroy_reg_param(®_params
[8]);
1719 destroy_reg_param(®_params
[9]);
1724 static int cfi_intel_write_word(struct flash_bank
*bank
, uint8_t *word
, uint32_t address
)
1727 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
1728 struct target
*target
= bank
->target
;
1730 cfi_intel_clear_status_register(bank
);
1731 if ((retval
= cfi_send_command(bank
, 0x40, address
)) != ERROR_OK
)
1736 if ((retval
= target_write_memory(target
, address
, bank
->bus_width
, 1, word
)) != ERROR_OK
)
1742 retval
= cfi_intel_wait_status_busy(bank
, 1000 * (1 << cfi_info
->word_write_timeout_max
), &status
);
1745 if ((retval
= cfi_send_command(bank
, 0xff, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
1750 LOG_ERROR("couldn't write word at base 0x%" PRIx32
", address %" PRIx32
, bank
->base
, address
);
1751 return ERROR_FLASH_OPERATION_FAILED
;
1757 static int cfi_intel_write_words(struct flash_bank
*bank
, uint8_t *word
, uint32_t wordcount
, uint32_t address
)
1760 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
1761 struct target
*target
= bank
->target
;
1763 /* Calculate buffer size and boundary mask */
1764 /* buffersize is (buffer size per chip) * (number of chips) */
1765 /* bufferwsize is buffersize in words */
1766 uint32_t buffersize
= (1UL << cfi_info
->max_buf_write_size
) * (bank
->bus_width
/ bank
->chip_width
);
1767 uint32_t buffermask
= buffersize
-1;
1768 uint32_t bufferwsize
= buffersize
/ bank
->bus_width
;
1770 /* Check for valid range */
1771 if (address
& buffermask
)
1773 LOG_ERROR("Write address at base 0x%" PRIx32
", address %" PRIx32
" not aligned to 2^%d boundary",
1774 bank
->base
, address
, cfi_info
->max_buf_write_size
);
1775 return ERROR_FLASH_OPERATION_FAILED
;
1778 /* Check for valid size */
1779 if (wordcount
> bufferwsize
)
1781 LOG_ERROR("Number of data words %" PRId32
" exceeds available buffersize %" PRId32
, wordcount
, buffersize
);
1782 return ERROR_FLASH_OPERATION_FAILED
;
1785 /* Write to flash buffer */
1786 cfi_intel_clear_status_register(bank
);
1788 /* Initiate buffer operation _*/
1789 if ((retval
= cfi_send_command(bank
, 0xe8, address
)) != ERROR_OK
)
1794 retval
= cfi_intel_wait_status_busy(bank
, 1000 * (1 << cfi_info
->buf_write_timeout_max
), &status
);
1795 if (retval
!= ERROR_OK
)
1799 if ((retval
= cfi_send_command(bank
, 0xff, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
1804 LOG_ERROR("couldn't start buffer write operation at base 0x%" PRIx32
", address %" PRIx32
, bank
->base
, address
);
1805 return ERROR_FLASH_OPERATION_FAILED
;
1808 /* Write buffer wordcount-1 and data words */
1809 if ((retval
= cfi_send_command(bank
, bufferwsize
-1, address
)) != ERROR_OK
)
1814 if ((retval
= target_write_memory(target
, address
, bank
->bus_width
, bufferwsize
, word
)) != ERROR_OK
)
1819 /* Commit write operation */
1820 if ((retval
= cfi_send_command(bank
, 0xd0, address
)) != ERROR_OK
)
1825 retval
= cfi_intel_wait_status_busy(bank
, 1000 * (1 << cfi_info
->buf_write_timeout_max
), &status
);
1826 if (retval
!= ERROR_OK
)
1831 if ((retval
= cfi_send_command(bank
, 0xff, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
1836 LOG_ERROR("Buffer write at base 0x%" PRIx32
", address %" PRIx32
" failed.", bank
->base
, address
);
1837 return ERROR_FLASH_OPERATION_FAILED
;
1843 static int cfi_spansion_write_word(struct flash_bank
*bank
, uint8_t *word
, uint32_t address
)
1846 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
1847 struct cfi_spansion_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
1848 struct target
*target
= bank
->target
;
1850 if ((retval
= cfi_send_command(bank
, 0xaa, flash_address(bank
, 0, pri_ext
->_unlock1
))) != ERROR_OK
)
1855 if ((retval
= cfi_send_command(bank
, 0x55, flash_address(bank
, 0, pri_ext
->_unlock2
))) != ERROR_OK
)
1860 if ((retval
= cfi_send_command(bank
, 0xa0, flash_address(bank
, 0, pri_ext
->_unlock1
))) != ERROR_OK
)
1865 if ((retval
= target_write_memory(target
, address
, bank
->bus_width
, 1, word
)) != ERROR_OK
)
1870 if (cfi_spansion_wait_status_busy(bank
, 1000 * (1 << cfi_info
->word_write_timeout_max
)) != ERROR_OK
)
1872 if ((retval
= cfi_send_command(bank
, 0xf0, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
1877 LOG_ERROR("couldn't write word at base 0x%" PRIx32
", address %" PRIx32
, bank
->base
, address
);
1878 return ERROR_FLASH_OPERATION_FAILED
;
1884 static int cfi_spansion_write_words(struct flash_bank
*bank
, uint8_t *word
, uint32_t wordcount
, uint32_t address
)
1887 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
1888 struct target
*target
= bank
->target
;
1889 struct cfi_spansion_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
1891 /* Calculate buffer size and boundary mask */
1892 /* buffersize is (buffer size per chip) * (number of chips) */
1893 /* bufferwsize is buffersize in words */
1894 uint32_t buffersize
= (1UL << cfi_info
->max_buf_write_size
) * (bank
->bus_width
/ bank
->chip_width
);
1895 uint32_t buffermask
= buffersize
-1;
1896 uint32_t bufferwsize
= buffersize
/ bank
->bus_width
;
1898 /* Check for valid range */
1899 if (address
& buffermask
)
1901 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
);
1902 return ERROR_FLASH_OPERATION_FAILED
;
1905 /* Check for valid size */
1906 if (wordcount
> bufferwsize
)
1908 LOG_ERROR("Number of data words %" PRId32
" exceeds available buffersize %" PRId32
, wordcount
, buffersize
);
1909 return ERROR_FLASH_OPERATION_FAILED
;
1913 if ((retval
= cfi_send_command(bank
, 0xaa, flash_address(bank
, 0, pri_ext
->_unlock1
))) != ERROR_OK
)
1918 if ((retval
= cfi_send_command(bank
, 0x55, flash_address(bank
, 0, pri_ext
->_unlock2
))) != ERROR_OK
)
1923 // Buffer load command
1924 if ((retval
= cfi_send_command(bank
, 0x25, address
)) != ERROR_OK
)
1929 /* Write buffer wordcount-1 and data words */
1930 if ((retval
= cfi_send_command(bank
, bufferwsize
-1, address
)) != ERROR_OK
)
1935 if ((retval
= target_write_memory(target
, address
, bank
->bus_width
, bufferwsize
, word
)) != ERROR_OK
)
1940 /* Commit write operation */
1941 if ((retval
= cfi_send_command(bank
, 0x29, address
)) != ERROR_OK
)
1946 if (cfi_spansion_wait_status_busy(bank
, 1000 * (1 << cfi_info
->word_write_timeout_max
)) != ERROR_OK
)
1948 if ((retval
= cfi_send_command(bank
, 0xf0, flash_address(bank
, 0, 0x0))) != ERROR_OK
)
1953 LOG_ERROR("couldn't write block at base 0x%" PRIx32
", address %" PRIx32
", size %" PRIx32
, bank
->base
, address
, bufferwsize
);
1954 return ERROR_FLASH_OPERATION_FAILED
;
1960 static int cfi_write_word(struct flash_bank
*bank
, uint8_t *word
, uint32_t address
)
1962 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
1964 switch (cfi_info
->pri_id
)
1968 return cfi_intel_write_word(bank
, word
, address
);
1971 return cfi_spansion_write_word(bank
, word
, address
);
1974 LOG_ERROR("cfi primary command set %i unsupported", cfi_info
->pri_id
);
1978 return ERROR_FLASH_OPERATION_FAILED
;
1981 static int cfi_write_words(struct flash_bank
*bank
, uint8_t *word
, uint32_t wordcount
, uint32_t address
)
1983 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
1985 switch (cfi_info
->pri_id
)
1989 return cfi_intel_write_words(bank
, word
, wordcount
, address
);
1992 return cfi_spansion_write_words(bank
, word
, wordcount
, address
);
1995 LOG_ERROR("cfi primary command set %i unsupported", cfi_info
->pri_id
);
1999 return ERROR_FLASH_OPERATION_FAILED
;
2002 static int cfi_read(struct flash_bank
*bank
, uint8_t *buffer
, uint32_t offset
, uint32_t count
)
2004 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2005 struct target
*target
= bank
->target
;
2006 uint32_t address
= bank
->base
+ offset
;
2008 int align
; /* number of unaligned bytes */
2009 uint8_t current_word
[CFI_MAX_BUS_WIDTH
];
2013 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
2014 (int)count
, (unsigned)offset
);
2016 if (bank
->target
->state
!= TARGET_HALTED
)
2018 LOG_ERROR("Target not halted");
2019 return ERROR_TARGET_NOT_HALTED
;
2022 if (offset
+ count
> bank
->size
)
2023 return ERROR_FLASH_DST_OUT_OF_BANK
;
2025 if (cfi_info
->qry
[0] != 'Q')
2026 return ERROR_FLASH_BANK_NOT_PROBED
;
2028 /* start at the first byte of the first word (bus_width size) */
2029 read_p
= address
& ~(bank
->bus_width
- 1);
2030 if ((align
= address
- read_p
) != 0)
2032 LOG_INFO("Fixup %d unaligned read head bytes", align
);
2034 /* read a complete word from flash */
2035 if ((retval
= target_read_memory(target
, read_p
, bank
->bus_width
, 1, current_word
)) != ERROR_OK
)
2038 /* take only bytes we need */
2039 for (i
= align
; (i
< bank
->bus_width
) && (count
> 0); i
++, count
--)
2040 *buffer
++ = current_word
[i
];
2042 read_p
+= bank
->bus_width
;
2045 align
= count
/ bank
->bus_width
;
2048 if ((retval
= target_read_memory(target
, read_p
, bank
->bus_width
, align
, buffer
)) != ERROR_OK
)
2051 read_p
+= align
* bank
->bus_width
;
2052 buffer
+= align
* bank
->bus_width
;
2053 count
-= align
* bank
->bus_width
;
2058 LOG_INFO("Fixup %d unaligned read tail bytes", count
);
2060 /* read a complete word from flash */
2061 if ((retval
= target_read_memory(target
, read_p
, bank
->bus_width
, 1, current_word
)) != ERROR_OK
)
2064 /* take only bytes we need */
2065 for (i
= 0; (i
< bank
->bus_width
) && (count
> 0); i
++, count
--)
2066 *buffer
++ = current_word
[i
];
2072 static int cfi_write(struct flash_bank
*bank
, uint8_t *buffer
, uint32_t offset
, uint32_t count
)
2074 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2075 struct target
*target
= bank
->target
;
2076 uint32_t address
= bank
->base
+ offset
; /* address of first byte to be programmed */
2078 int align
; /* number of unaligned bytes */
2079 int blk_count
; /* number of bus_width bytes for block copy */
2080 uint8_t current_word
[CFI_MAX_BUS_WIDTH
* 4]; /* word (bus_width size) currently being programmed */
2084 if (bank
->target
->state
!= TARGET_HALTED
)
2086 LOG_ERROR("Target not halted");
2087 return ERROR_TARGET_NOT_HALTED
;
2090 if (offset
+ count
> bank
->size
)
2091 return ERROR_FLASH_DST_OUT_OF_BANK
;
2093 if (cfi_info
->qry
[0] != 'Q')
2094 return ERROR_FLASH_BANK_NOT_PROBED
;
2096 /* start at the first byte of the first word (bus_width size) */
2097 write_p
= address
& ~(bank
->bus_width
- 1);
2098 if ((align
= address
- write_p
) != 0)
2100 LOG_INFO("Fixup %d unaligned head bytes", align
);
2102 /* read a complete word from flash */
2103 if ((retval
= target_read_memory(target
, write_p
, bank
->bus_width
, 1, current_word
)) != ERROR_OK
)
2106 /* replace only bytes that must be written */
2107 for (i
= align
; (i
< bank
->bus_width
) && (count
> 0); i
++, count
--)
2108 current_word
[i
] = *buffer
++;
2110 retval
= cfi_write_word(bank
, current_word
, write_p
);
2111 if (retval
!= ERROR_OK
)
2113 write_p
+= bank
->bus_width
;
2116 /* handle blocks of bus_size aligned bytes */
2117 blk_count
= count
& ~(bank
->bus_width
- 1); /* round down, leave tail bytes */
2118 switch (cfi_info
->pri_id
)
2120 /* try block writes (fails without working area) */
2123 retval
= cfi_intel_write_block(bank
, buffer
, write_p
, blk_count
);
2126 retval
= cfi_spansion_write_block(bank
, buffer
, write_p
, blk_count
);
2129 LOG_ERROR("cfi primary command set %i unsupported", cfi_info
->pri_id
);
2130 retval
= ERROR_FLASH_OPERATION_FAILED
;
2133 if (retval
== ERROR_OK
)
2135 /* Increment pointers and decrease count on succesful block write */
2136 buffer
+= blk_count
;
2137 write_p
+= blk_count
;
2142 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
2144 /* Calculate buffer size and boundary mask */
2145 /* buffersize is (buffer size per chip) * (number of chips) */
2146 /* bufferwsize is buffersize in words */
2147 uint32_t buffersize
= (1UL << cfi_info
->max_buf_write_size
) * (bank
->bus_width
/ bank
->chip_width
);
2148 uint32_t buffermask
= buffersize
-1;
2149 uint32_t bufferwsize
= buffersize
/ bank
->bus_width
;
2151 /* fall back to memory writes */
2152 while (count
>= (uint32_t)bank
->bus_width
)
2155 if ((write_p
& 0xff) == 0)
2157 LOG_INFO("Programming at %08" PRIx32
", count %08" PRIx32
" bytes remaining", write_p
, count
);
2160 if ((bufferwsize
> 0) && (count
>= buffersize
) && !(write_p
& buffermask
))
2162 retval
= cfi_write_words(bank
, buffer
, bufferwsize
, write_p
);
2163 if (retval
== ERROR_OK
)
2165 buffer
+= buffersize
;
2166 write_p
+= buffersize
;
2167 count
-= buffersize
;
2171 /* try the slow way? */
2174 for (i
= 0; i
< bank
->bus_width
; i
++)
2175 current_word
[i
] = *buffer
++;
2177 retval
= cfi_write_word(bank
, current_word
, write_p
);
2178 if (retval
!= ERROR_OK
)
2181 write_p
+= bank
->bus_width
;
2182 count
-= bank
->bus_width
;
2190 /* return to read array mode, so we can read from flash again for padding */
2191 if ((retval
= cfi_reset(bank
)) != ERROR_OK
)
2196 /* handle unaligned tail bytes */
2199 LOG_INFO("Fixup %" PRId32
" unaligned tail bytes", count
);
2201 /* read a complete word from flash */
2202 if ((retval
= target_read_memory(target
, write_p
, bank
->bus_width
, 1, current_word
)) != ERROR_OK
)
2205 /* replace only bytes that must be written */
2206 for (i
= 0; (i
< bank
->bus_width
) && (count
> 0); i
++, count
--)
2207 current_word
[i
] = *buffer
++;
2209 retval
= cfi_write_word(bank
, current_word
, write_p
);
2210 if (retval
!= ERROR_OK
)
2214 /* return to read array mode */
2215 return cfi_reset(bank
);
2218 static void cfi_fixup_reversed_erase_regions(struct flash_bank
*bank
, void *param
)
2221 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2222 struct cfi_spansion_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
2224 pri_ext
->_reversed_geometry
= 1;
2227 static void cfi_fixup_0002_erase_regions(struct flash_bank
*bank
, void *param
)
2230 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2231 struct cfi_spansion_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
2234 if ((pri_ext
->_reversed_geometry
) || (pri_ext
->TopBottom
== 3))
2236 LOG_DEBUG("swapping reversed erase region information on cmdset 0002 device");
2238 for (i
= 0; i
< cfi_info
->num_erase_regions
/ 2; i
++)
2240 int j
= (cfi_info
->num_erase_regions
- 1) - i
;
2243 swap
= cfi_info
->erase_region_info
[i
];
2244 cfi_info
->erase_region_info
[i
] = cfi_info
->erase_region_info
[j
];
2245 cfi_info
->erase_region_info
[j
] = swap
;
2250 static void cfi_fixup_0002_unlock_addresses(struct flash_bank
*bank
, void *param
)
2252 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2253 struct cfi_spansion_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
2254 struct cfi_unlock_addresses
*unlock_addresses
= param
;
2256 pri_ext
->_unlock1
= unlock_addresses
->unlock1
;
2257 pri_ext
->_unlock2
= unlock_addresses
->unlock2
;
2261 static int cfi_query_string(struct flash_bank
*bank
, int address
)
2263 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2266 if ((retval
= cfi_send_command(bank
, 0x98, flash_address(bank
, 0, address
))) != ERROR_OK
)
2271 retval
= cfi_query_u8(bank
, 0, 0x10, &cfi_info
->qry
[0]);
2272 if (retval
!= ERROR_OK
)
2274 retval
= cfi_query_u8(bank
, 0, 0x11, &cfi_info
->qry
[1]);
2275 if (retval
!= ERROR_OK
)
2277 retval
= cfi_query_u8(bank
, 0, 0x12, &cfi_info
->qry
[2]);
2278 if (retval
!= ERROR_OK
)
2281 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]);
2283 if ((cfi_info
->qry
[0] != 'Q') || (cfi_info
->qry
[1] != 'R') || (cfi_info
->qry
[2] != 'Y'))
2285 if ((retval
= cfi_reset(bank
)) != ERROR_OK
)
2289 LOG_ERROR("Could not probe bank: no QRY");
2290 return ERROR_FLASH_BANK_INVALID
;
2296 static int cfi_probe(struct flash_bank
*bank
)
2298 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2299 struct target
*target
= bank
->target
;
2300 int num_sectors
= 0;
2303 uint32_t unlock1
= 0x555;
2304 uint32_t unlock2
= 0x2aa;
2306 uint8_t value_buf0
[CFI_MAX_BUS_WIDTH
], value_buf1
[CFI_MAX_BUS_WIDTH
];
2308 if (bank
->target
->state
!= TARGET_HALTED
)
2310 LOG_ERROR("Target not halted");
2311 return ERROR_TARGET_NOT_HALTED
;
2314 cfi_info
->probed
= 0;
2317 free(bank
->sectors
);
2318 bank
->sectors
= NULL
;
2320 if(cfi_info
->erase_region_info
)
2322 free(cfi_info
->erase_region_info
);
2323 cfi_info
->erase_region_info
= NULL
;
2326 /* JEDEC standard JESD21C uses 0x5555 and 0x2aaa as unlock addresses,
2327 * while CFI compatible AMD/Spansion flashes use 0x555 and 0x2aa
2329 if (cfi_info
->jedec_probe
)
2335 /* switch to read identifier codes mode ("AUTOSELECT") */
2336 if ((retval
= cfi_send_command(bank
, 0xaa, flash_address(bank
, 0, unlock1
))) != ERROR_OK
)
2340 if ((retval
= cfi_send_command(bank
, 0x55, flash_address(bank
, 0, unlock2
))) != ERROR_OK
)
2344 if ((retval
= cfi_send_command(bank
, 0x90, flash_address(bank
, 0, unlock1
))) != ERROR_OK
)
2349 if ((retval
= target_read_memory(target
, flash_address(bank
, 0, 0x00), bank
->bus_width
, 1, value_buf0
)) != ERROR_OK
)
2353 if ((retval
= target_read_memory(target
, flash_address(bank
, 0, 0x01), bank
->bus_width
, 1, value_buf1
)) != ERROR_OK
)
2357 switch (bank
->chip_width
) {
2359 cfi_info
->manufacturer
= *value_buf0
;
2360 cfi_info
->device_id
= *value_buf1
;
2363 cfi_info
->manufacturer
= target_buffer_get_u16(target
, value_buf0
);
2364 cfi_info
->device_id
= target_buffer_get_u16(target
, value_buf1
);
2367 cfi_info
->manufacturer
= target_buffer_get_u32(target
, value_buf0
);
2368 cfi_info
->device_id
= target_buffer_get_u32(target
, value_buf1
);
2371 LOG_ERROR("Unsupported bank chipwidth %d, can't probe memory", bank
->chip_width
);
2372 return ERROR_FLASH_OPERATION_FAILED
;
2375 LOG_INFO("Flash Manufacturer/Device: 0x%04x 0x%04x", cfi_info
->manufacturer
, cfi_info
->device_id
);
2376 /* switch back to read array mode */
2377 if ((retval
= cfi_reset(bank
)) != ERROR_OK
)
2382 /* check device/manufacturer ID for known non-CFI flashes. */
2383 cfi_fixup_non_cfi(bank
);
2385 /* query only if this is a CFI compatible flash,
2386 * otherwise the relevant info has already been filled in
2388 if (cfi_info
->not_cfi
== 0)
2390 /* enter CFI query mode
2391 * according to JEDEC Standard No. 68.01,
2392 * a single bus sequence with address = 0x55, data = 0x98 should put
2393 * the device into CFI query mode.
2395 * SST flashes clearly violate this, and we will consider them incompatbile for now
2398 retval
= cfi_query_string(bank
, 0x55);
2399 if (retval
!= ERROR_OK
)
2402 * Spansion S29WS-N CFI query fix is to try 0x555 if 0x55 fails. Should
2403 * be harmless enough:
2405 * http://www.infradead.org/pipermail/linux-mtd/2005-September/013618.html
2407 LOG_USER("Try workaround w/0x555 instead of 0x55 to get QRY.");
2408 retval
= cfi_query_string(bank
, 0x555);
2410 if (retval
!= ERROR_OK
)
2413 retval
= cfi_query_u16(bank
, 0, 0x13, &cfi_info
->pri_id
);
2414 if (retval
!= ERROR_OK
)
2416 retval
= cfi_query_u16(bank
, 0, 0x15, &cfi_info
->pri_addr
);
2417 if (retval
!= ERROR_OK
)
2419 retval
= cfi_query_u16(bank
, 0, 0x17, &cfi_info
->alt_id
);
2420 if (retval
!= ERROR_OK
)
2422 retval
= cfi_query_u16(bank
, 0, 0x19, &cfi_info
->alt_addr
);
2423 if (retval
!= ERROR_OK
)
2426 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
);
2428 retval
= cfi_query_u8(bank
, 0, 0x1b, &cfi_info
->vcc_min
);
2429 if (retval
!= ERROR_OK
)
2431 retval
= cfi_query_u8(bank
, 0, 0x1c, &cfi_info
->vcc_max
);
2432 if (retval
!= ERROR_OK
)
2434 retval
= cfi_query_u8(bank
, 0, 0x1d, &cfi_info
->vpp_min
);
2435 if (retval
!= ERROR_OK
)
2437 retval
= cfi_query_u8(bank
, 0, 0x1e, &cfi_info
->vpp_max
);
2438 if (retval
!= ERROR_OK
)
2440 retval
= cfi_query_u8(bank
, 0, 0x1f, &cfi_info
->word_write_timeout_typ
);
2441 if (retval
!= ERROR_OK
)
2443 retval
= cfi_query_u8(bank
, 0, 0x20, &cfi_info
->buf_write_timeout_typ
);
2444 if (retval
!= ERROR_OK
)
2446 retval
= cfi_query_u8(bank
, 0, 0x21, &cfi_info
->block_erase_timeout_typ
);
2447 if (retval
!= ERROR_OK
)
2449 retval
= cfi_query_u8(bank
, 0, 0x22, &cfi_info
->chip_erase_timeout_typ
);
2450 if (retval
!= ERROR_OK
)
2452 retval
= cfi_query_u8(bank
, 0, 0x23, &cfi_info
->word_write_timeout_max
);
2453 if (retval
!= ERROR_OK
)
2455 retval
= cfi_query_u8(bank
, 0, 0x24, &cfi_info
->buf_write_timeout_max
);
2456 if (retval
!= ERROR_OK
)
2458 retval
= cfi_query_u8(bank
, 0, 0x25, &cfi_info
->block_erase_timeout_max
);
2459 if (retval
!= ERROR_OK
)
2461 retval
= cfi_query_u8(bank
, 0, 0x26, &cfi_info
->chip_erase_timeout_max
);
2462 if (retval
!= ERROR_OK
)
2465 LOG_DEBUG("Vcc min: %x.%x, Vcc max: %x.%x, Vpp min: %u.%x, Vpp max: %u.%x",
2466 (cfi_info
->vcc_min
& 0xf0) >> 4, cfi_info
->vcc_min
& 0x0f,
2467 (cfi_info
->vcc_max
& 0xf0) >> 4, cfi_info
->vcc_max
& 0x0f,
2468 (cfi_info
->vpp_min
& 0xf0) >> 4, cfi_info
->vpp_min
& 0x0f,
2469 (cfi_info
->vpp_max
& 0xf0) >> 4, cfi_info
->vpp_max
& 0x0f);
2470 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
,
2471 1 << cfi_info
->block_erase_timeout_typ
, 1 << cfi_info
->chip_erase_timeout_typ
);
2472 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
),
2473 (1 << cfi_info
->buf_write_timeout_max
) * (1 << cfi_info
->buf_write_timeout_typ
),
2474 (1 << cfi_info
->block_erase_timeout_max
) * (1 << cfi_info
->block_erase_timeout_typ
),
2475 (1 << cfi_info
->chip_erase_timeout_max
) * (1 << cfi_info
->chip_erase_timeout_typ
));
2478 retval
= cfi_query_u8(bank
, 0, 0x27, &data
);
2479 if (retval
!= ERROR_OK
)
2481 cfi_info
->dev_size
= 1 << data
;
2483 retval
= cfi_query_u16(bank
, 0, 0x28, &cfi_info
->interface_desc
);
2484 if (retval
!= ERROR_OK
)
2486 retval
= cfi_query_u16(bank
, 0, 0x2a, &cfi_info
->max_buf_write_size
);
2487 if (retval
!= ERROR_OK
)
2489 retval
= cfi_query_u8(bank
, 0, 0x2c, &cfi_info
->num_erase_regions
);
2490 if (retval
!= ERROR_OK
)
2493 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
));
2495 if (cfi_info
->num_erase_regions
)
2497 cfi_info
->erase_region_info
= malloc(sizeof(*cfi_info
->erase_region_info
)
2498 * cfi_info
->num_erase_regions
);
2499 for (i
= 0; i
< cfi_info
->num_erase_regions
; i
++)
2501 retval
= cfi_query_u32(bank
, 0, 0x2d + (4 * i
), &cfi_info
->erase_region_info
[i
]);
2502 if (retval
!= ERROR_OK
)
2504 LOG_DEBUG("erase region[%i]: %" PRIu32
" blocks of size 0x%" PRIx32
"",
2506 (cfi_info
->erase_region_info
[i
] & 0xffff) + 1,
2507 (cfi_info
->erase_region_info
[i
] >> 16) * 256);
2512 cfi_info
->erase_region_info
= NULL
;
2515 /* We need to read the primary algorithm extended query table before calculating
2516 * the sector layout to be able to apply fixups
2518 switch (cfi_info
->pri_id
)
2520 /* Intel command set (standard and extended) */
2523 cfi_read_intel_pri_ext(bank
);
2525 /* AMD/Spansion, Atmel, ... command set */
2527 cfi_info
->status_poll_mask
= CFI_STATUS_POLL_MASK_DQ5_DQ6_DQ7
; /* default for all CFI flashs */
2528 cfi_read_0002_pri_ext(bank
);
2531 LOG_ERROR("cfi primary command set %i unsupported", cfi_info
->pri_id
);
2535 /* return to read array mode
2536 * we use both reset commands, as some Intel flashes fail to recognize the 0xF0 command
2538 if ((retval
= cfi_reset(bank
)) != ERROR_OK
)
2542 } /* end CFI case */
2544 /* apply fixups depending on the primary command set */
2545 switch (cfi_info
->pri_id
)
2547 /* Intel command set (standard and extended) */
2550 cfi_fixup(bank
, cfi_0001_fixups
);
2552 /* AMD/Spansion, Atmel, ... command set */
2554 cfi_fixup(bank
, cfi_0002_fixups
);
2557 LOG_ERROR("cfi primary command set %i unsupported", cfi_info
->pri_id
);
2561 if ((cfi_info
->dev_size
* bank
->bus_width
/ bank
->chip_width
) != bank
->size
)
2563 LOG_WARNING("configuration specifies 0x%" PRIx32
" size, but a 0x%" PRIx32
" size flash was found", bank
->size
, cfi_info
->dev_size
);
2566 if (cfi_info
->num_erase_regions
== 0)
2568 /* a device might have only one erase block, spanning the whole device */
2569 bank
->num_sectors
= 1;
2570 bank
->sectors
= malloc(sizeof(struct flash_sector
));
2572 bank
->sectors
[sector
].offset
= 0x0;
2573 bank
->sectors
[sector
].size
= bank
->size
;
2574 bank
->sectors
[sector
].is_erased
= -1;
2575 bank
->sectors
[sector
].is_protected
= -1;
2579 uint32_t offset
= 0;
2581 for (i
= 0; i
< cfi_info
->num_erase_regions
; i
++)
2583 num_sectors
+= (cfi_info
->erase_region_info
[i
] & 0xffff) + 1;
2586 bank
->num_sectors
= num_sectors
;
2587 bank
->sectors
= malloc(sizeof(struct flash_sector
) * num_sectors
);
2589 for (i
= 0; i
< cfi_info
->num_erase_regions
; i
++)
2592 for (j
= 0; j
< (cfi_info
->erase_region_info
[i
] & 0xffff) + 1; j
++)
2594 bank
->sectors
[sector
].offset
= offset
;
2595 bank
->sectors
[sector
].size
= ((cfi_info
->erase_region_info
[i
] >> 16) * 256) * bank
->bus_width
/ bank
->chip_width
;
2596 offset
+= bank
->sectors
[sector
].size
;
2597 bank
->sectors
[sector
].is_erased
= -1;
2598 bank
->sectors
[sector
].is_protected
= -1;
2602 if (offset
!= (cfi_info
->dev_size
* bank
->bus_width
/ bank
->chip_width
))
2604 LOG_WARNING("CFI size is 0x%" PRIx32
", but total sector size is 0x%" PRIx32
"", \
2605 (cfi_info
->dev_size
* bank
->bus_width
/ bank
->chip_width
), offset
);
2609 cfi_info
->probed
= 1;
2614 static int cfi_auto_probe(struct flash_bank
*bank
)
2616 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2617 if (cfi_info
->probed
)
2619 return cfi_probe(bank
);
2622 static int cfi_intel_protect_check(struct flash_bank
*bank
)
2625 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2626 struct cfi_intel_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
2629 /* check if block lock bits are supported on this device */
2630 if (!(pri_ext
->blk_status_reg_mask
& 0x1))
2631 return ERROR_FLASH_OPERATION_FAILED
;
2633 if ((retval
= cfi_send_command(bank
, 0x90, flash_address(bank
, 0, 0x55))) != ERROR_OK
)
2638 for (i
= 0; i
< bank
->num_sectors
; i
++)
2640 uint8_t block_status
;
2641 retval
= cfi_get_u8(bank
, i
, 0x2, &block_status
);
2642 if (retval
!= ERROR_OK
)
2645 if (block_status
& 1)
2646 bank
->sectors
[i
].is_protected
= 1;
2648 bank
->sectors
[i
].is_protected
= 0;
2651 return cfi_send_command(bank
, 0xff, flash_address(bank
, 0, 0x0));
2654 static int cfi_spansion_protect_check(struct flash_bank
*bank
)
2657 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2658 struct cfi_spansion_pri_ext
*pri_ext
= cfi_info
->pri_ext
;
2661 if ((retval
= cfi_send_command(bank
, 0xaa, flash_address(bank
, 0, pri_ext
->_unlock1
))) != ERROR_OK
)
2666 if ((retval
= cfi_send_command(bank
, 0x55, flash_address(bank
, 0, pri_ext
->_unlock2
))) != ERROR_OK
)
2671 if ((retval
= cfi_send_command(bank
, 0x90, flash_address(bank
, 0, pri_ext
->_unlock1
))) != ERROR_OK
)
2676 for (i
= 0; i
< bank
->num_sectors
; i
++)
2678 uint8_t block_status
;
2679 retval
= cfi_get_u8(bank
, i
, 0x2, &block_status
);
2680 if (retval
!= ERROR_OK
)
2683 if (block_status
& 1)
2684 bank
->sectors
[i
].is_protected
= 1;
2686 bank
->sectors
[i
].is_protected
= 0;
2689 return cfi_send_command(bank
, 0xf0, flash_address(bank
, 0, 0x0));
2692 static int cfi_protect_check(struct flash_bank
*bank
)
2694 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2696 if (bank
->target
->state
!= TARGET_HALTED
)
2698 LOG_ERROR("Target not halted");
2699 return ERROR_TARGET_NOT_HALTED
;
2702 if (cfi_info
->qry
[0] != 'Q')
2703 return ERROR_FLASH_BANK_NOT_PROBED
;
2705 switch (cfi_info
->pri_id
)
2709 return cfi_intel_protect_check(bank
);
2712 return cfi_spansion_protect_check(bank
);
2715 LOG_ERROR("cfi primary command set %i unsupported", cfi_info
->pri_id
);
2722 static int get_cfi_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
2725 struct cfi_flash_bank
*cfi_info
= bank
->driver_priv
;
2727 if (cfi_info
->qry
[0] == 0xff)
2729 printed
= snprintf(buf
, buf_size
, "\ncfi flash bank not probed yet\n");
2733 if (cfi_info
->not_cfi
== 0)
2734 printed
= snprintf(buf
, buf_size
, "\ncfi information:\n");
2736 printed
= snprintf(buf
, buf_size
, "\nnon-cfi flash:\n");
2738 buf_size
-= printed
;
2740 printed
= snprintf(buf
, buf_size
, "\nmfr: 0x%4.4x, id:0x%4.4x\n",
2741 cfi_info
->manufacturer
, cfi_info
->device_id
);
2743 buf_size
-= printed
;
2745 if (cfi_info
->not_cfi
== 0)
2747 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
);
2749 buf_size
-= printed
;
2751 printed
= snprintf(buf
, buf_size
, "Vcc min: %x.%x, Vcc max: %x.%x, Vpp min: %u.%x, Vpp max: %u.%x\n",
2752 (cfi_info
->vcc_min
& 0xf0) >> 4, cfi_info
->vcc_min
& 0x0f,
2753 (cfi_info
->vcc_max
& 0xf0) >> 4, cfi_info
->vcc_max
& 0x0f,
2754 (cfi_info
->vpp_min
& 0xf0) >> 4, cfi_info
->vpp_min
& 0x0f,
2755 (cfi_info
->vpp_max
& 0xf0) >> 4, cfi_info
->vpp_max
& 0x0f);
2757 buf_size
-= printed
;
2759 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",
2760 1 << cfi_info
->word_write_timeout_typ
,
2761 1 << cfi_info
->buf_write_timeout_typ
,
2762 1 << cfi_info
->block_erase_timeout_typ
,
2763 1 << cfi_info
->chip_erase_timeout_typ
);
2765 buf_size
-= printed
;
2767 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",
2768 (1 << cfi_info
->word_write_timeout_max
) * (1 << cfi_info
->word_write_timeout_typ
),
2769 (1 << cfi_info
->buf_write_timeout_max
) * (1 << cfi_info
->buf_write_timeout_typ
),
2770 (1 << cfi_info
->block_erase_timeout_max
) * (1 << cfi_info
->block_erase_timeout_typ
),
2771 (1 << cfi_info
->chip_erase_timeout_max
) * (1 << cfi_info
->chip_erase_timeout_typ
));
2773 buf_size
-= printed
;
2775 printed
= snprintf(buf
, buf_size
, "size: 0x%" PRIx32
", interface desc: %i, max buffer write size: %x\n",
2777 cfi_info
->interface_desc
,
2778 1 << cfi_info
->max_buf_write_size
);
2780 buf_size
-= printed
;
2782 switch (cfi_info
->pri_id
)
2786 cfi_intel_info(bank
, buf
, buf_size
);
2789 cfi_spansion_info(bank
, buf
, buf_size
);
2792 LOG_ERROR("cfi primary command set %i unsupported", cfi_info
->pri_id
);
2800 struct flash_driver cfi_flash
= {
2802 .flash_bank_command
= cfi_flash_bank_command
,
2804 .protect
= cfi_protect
,
2808 .auto_probe
= cfi_auto_probe
,
2809 /* FIXME: access flash at bus_width size */
2810 .erase_check
= default_flash_blank_check
,
2811 .protect_check
= cfi_protect_check
,
2812 .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)