You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2587 lines
81 KiB

  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. * *
  7. * This program is free software; you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation; either version 2 of the License, or *
  10. * (at your option) any later version. *
  11. * *
  12. * This program is distributed in the hope that it will be useful, *
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  15. * GNU General Public License for more details. *
  16. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with this program; if not, write to the *
  19. * Free Software Foundation, Inc., *
  20. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  21. ***************************************************************************/
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include "cfi.h"
  26. #include "non_cfi.h"
  27. #include "armv4_5.h"
  28. #include "binarybuffer.h"
  29. static int cfi_register_commands(struct command_context_s *cmd_ctx);
  30. static int cfi_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
  31. static int cfi_erase(struct flash_bank_s *bank, int first, int last);
  32. static int cfi_protect(struct flash_bank_s *bank, int set, int first, int last);
  33. static int cfi_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count);
  34. static int cfi_probe(struct flash_bank_s *bank);
  35. static int cfi_auto_probe(struct flash_bank_s *bank);
  36. static int cfi_protect_check(struct flash_bank_s *bank);
  37. static int cfi_info(struct flash_bank_s *bank, char *buf, int buf_size);
  38. //static int cfi_handle_part_id_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  39. #define CFI_MAX_BUS_WIDTH 4
  40. #define CFI_MAX_CHIP_WIDTH 4
  41. /* defines internal maximum size for code fragment in cfi_intel_write_block() */
  42. #define CFI_MAX_INTEL_CODESIZE 256
  43. flash_driver_t cfi_flash =
  44. {
  45. .name = "cfi",
  46. .register_commands = cfi_register_commands,
  47. .flash_bank_command = cfi_flash_bank_command,
  48. .erase = cfi_erase,
  49. .protect = cfi_protect,
  50. .write = cfi_write,
  51. .probe = cfi_probe,
  52. .auto_probe = cfi_auto_probe,
  53. .erase_check = default_flash_blank_check,
  54. .protect_check = cfi_protect_check,
  55. .info = cfi_info
  56. };
  57. static cfi_unlock_addresses_t cfi_unlock_addresses[] =
  58. {
  59. [CFI_UNLOCK_555_2AA] = { .unlock1 = 0x555, .unlock2 = 0x2aa },
  60. [CFI_UNLOCK_5555_2AAA] = { .unlock1 = 0x5555, .unlock2 = 0x2aaa },
  61. };
  62. /* CFI fixups foward declarations */
  63. static void cfi_fixup_0002_erase_regions(flash_bank_t *flash, void *param);
  64. static void cfi_fixup_0002_unlock_addresses(flash_bank_t *flash, void *param);
  65. static void cfi_fixup_atmel_reversed_erase_regions(flash_bank_t *flash, void *param);
  66. /* fixup after reading cmdset 0002 primary query table */
  67. static cfi_fixup_t cfi_0002_fixups[] = {
  68. {CFI_MFR_SST, 0x00D4, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
  69. {CFI_MFR_SST, 0x00D5, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
  70. {CFI_MFR_SST, 0x00D6, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
  71. {CFI_MFR_SST, 0x00D7, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
  72. {CFI_MFR_SST, 0x2780, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
  73. {CFI_MFR_ATMEL, 0x00C8, cfi_fixup_atmel_reversed_erase_regions, NULL},
  74. {CFI_MFR_FUJITSU, 0x226b, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_5555_2AAA]},
  75. {CFI_MFR_AMIC, 0xb31a, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_555_2AA]},
  76. {CFI_MFR_MX, 0x225b, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_555_2AA]},
  77. {CFI_MFR_AMD, 0x225b, cfi_fixup_0002_unlock_addresses, &cfi_unlock_addresses[CFI_UNLOCK_555_2AA]},
  78. {CFI_MFR_ANY, CFI_ID_ANY, cfi_fixup_0002_erase_regions, NULL},
  79. {0, 0, NULL, NULL}
  80. };
  81. /* fixup after reading cmdset 0001 primary query table */
  82. static cfi_fixup_t cfi_0001_fixups[] = {
  83. {0, 0, NULL, NULL}
  84. };
  85. static void cfi_fixup(flash_bank_t *bank, cfi_fixup_t *fixups)
  86. {
  87. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  88. cfi_fixup_t *f;
  89. for (f = fixups; f->fixup; f++)
  90. {
  91. if (((f->mfr == CFI_MFR_ANY) || (f->mfr == cfi_info->manufacturer)) &&
  92. ((f->id == CFI_ID_ANY) || (f->id == cfi_info->device_id)))
  93. {
  94. f->fixup(bank, f->param);
  95. }
  96. }
  97. }
  98. /* inline uint32_t flash_address(flash_bank_t *bank, int sector, uint32_t offset) */
  99. static __inline__ uint32_t flash_address(flash_bank_t *bank, int sector, uint32_t offset)
  100. {
  101. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  102. if (cfi_info->x16_as_x8) offset *= 2;
  103. /* while the sector list isn't built, only accesses to sector 0 work */
  104. if (sector == 0)
  105. return bank->base + offset * bank->bus_width;
  106. else
  107. {
  108. if (!bank->sectors)
  109. {
  110. LOG_ERROR("BUG: sector list not yet built");
  111. exit(-1);
  112. }
  113. return bank->base + bank->sectors[sector].offset + offset * bank->bus_width;
  114. }
  115. }
  116. static void cfi_command(flash_bank_t *bank, uint8_t cmd, uint8_t *cmd_buf)
  117. {
  118. int i;
  119. /* clear whole buffer, to ensure bits that exceed the bus_width
  120. * are set to zero
  121. */
  122. for (i = 0; i < CFI_MAX_BUS_WIDTH; i++)
  123. cmd_buf[i] = 0;
  124. if (bank->target->endianness == TARGET_LITTLE_ENDIAN)
  125. {
  126. for (i = bank->bus_width; i > 0; i--)
  127. {
  128. *cmd_buf++ = (i & (bank->chip_width - 1)) ? 0x0 : cmd;
  129. }
  130. }
  131. else
  132. {
  133. for (i = 1; i <= bank->bus_width; i++)
  134. {
  135. *cmd_buf++ = (i & (bank->chip_width - 1)) ? 0x0 : cmd;
  136. }
  137. }
  138. }
  139. /* read unsigned 8-bit value from the bank
  140. * flash banks are expected to be made of similar chips
  141. * the query result should be the same for all
  142. */
  143. static uint8_t cfi_query_u8(flash_bank_t *bank, int sector, uint32_t offset)
  144. {
  145. target_t *target = bank->target;
  146. uint8_t data[CFI_MAX_BUS_WIDTH];
  147. target_read_memory(target, flash_address(bank, sector, offset), bank->bus_width, 1, data);
  148. if (bank->target->endianness == TARGET_LITTLE_ENDIAN)
  149. return data[0];
  150. else
  151. return data[bank->bus_width - 1];
  152. }
  153. /* read unsigned 8-bit value from the bank
  154. * in case of a bank made of multiple chips,
  155. * the individual values are ORed
  156. */
  157. static uint8_t cfi_get_u8(flash_bank_t *bank, int sector, uint32_t offset)
  158. {
  159. target_t *target = bank->target;
  160. uint8_t data[CFI_MAX_BUS_WIDTH];
  161. int i;
  162. target_read_memory(target, flash_address(bank, sector, offset), bank->bus_width, 1, data);
  163. if (bank->target->endianness == TARGET_LITTLE_ENDIAN)
  164. {
  165. for (i = 0; i < bank->bus_width / bank->chip_width; i++)
  166. data[0] |= data[i];
  167. return data[0];
  168. }
  169. else
  170. {
  171. uint8_t value = 0;
  172. for (i = 0; i < bank->bus_width / bank->chip_width; i++)
  173. value |= data[bank->bus_width - 1 - i];
  174. return value;
  175. }
  176. }
  177. static uint16_t cfi_query_u16(flash_bank_t *bank, int sector, uint32_t offset)
  178. {
  179. target_t *target = bank->target;
  180. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  181. uint8_t data[CFI_MAX_BUS_WIDTH * 2];
  182. if (cfi_info->x16_as_x8)
  183. {
  184. uint8_t i;
  185. for (i = 0;i < 2;i++)
  186. target_read_memory(target, flash_address(bank, sector, offset + i), bank->bus_width, 1,
  187. &data[i*bank->bus_width]);
  188. }
  189. else
  190. target_read_memory(target, flash_address(bank, sector, offset), bank->bus_width, 2, data);
  191. if (bank->target->endianness == TARGET_LITTLE_ENDIAN)
  192. return data[0] | data[bank->bus_width] << 8;
  193. else
  194. return data[bank->bus_width - 1] | data[(2 * bank->bus_width) - 1] << 8;
  195. }
  196. static uint32_t cfi_query_u32(flash_bank_t *bank, int sector, uint32_t offset)
  197. {
  198. target_t *target = bank->target;
  199. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  200. uint8_t data[CFI_MAX_BUS_WIDTH * 4];
  201. if (cfi_info->x16_as_x8)
  202. {
  203. uint8_t i;
  204. for (i = 0;i < 4;i++)
  205. target_read_memory(target, flash_address(bank, sector, offset + i), bank->bus_width, 1,
  206. &data[i*bank->bus_width]);
  207. }
  208. else
  209. target_read_memory(target, flash_address(bank, sector, offset), bank->bus_width, 4, data);
  210. if (bank->target->endianness == TARGET_LITTLE_ENDIAN)
  211. return data[0] | data[bank->bus_width] << 8 | data[bank->bus_width * 2] << 16 | data[bank->bus_width * 3] << 24;
  212. else
  213. return data[bank->bus_width - 1] | data[(2* bank->bus_width) - 1] << 8 |
  214. data[(3 * bank->bus_width) - 1] << 16 | data[(4 * bank->bus_width) - 1] << 24;
  215. }
  216. static void cfi_intel_clear_status_register(flash_bank_t *bank)
  217. {
  218. target_t *target = bank->target;
  219. uint8_t command[8];
  220. if (target->state != TARGET_HALTED)
  221. {
  222. LOG_ERROR("BUG: attempted to clear status register while target wasn't halted");
  223. exit(-1);
  224. }
  225. cfi_command(bank, 0x50, command);
  226. target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
  227. }
  228. uint8_t cfi_intel_wait_status_busy(flash_bank_t *bank, int timeout)
  229. {
  230. uint8_t status;
  231. while ((!((status = cfi_get_u8(bank, 0, 0x0)) & 0x80)) && (timeout-- > 0))
  232. {
  233. LOG_DEBUG("status: 0x%x", status);
  234. alive_sleep(1);
  235. }
  236. /* mask out bit 0 (reserved) */
  237. status = status & 0xfe;
  238. LOG_DEBUG("status: 0x%x", status);
  239. if ((status & 0x80) != 0x80)
  240. {
  241. LOG_ERROR("timeout while waiting for WSM to become ready");
  242. }
  243. else if (status != 0x80)
  244. {
  245. LOG_ERROR("status register: 0x%x", status);
  246. if (status & 0x2)
  247. LOG_ERROR("Block Lock-Bit Detected, Operation Abort");
  248. if (status & 0x4)
  249. LOG_ERROR("Program suspended");
  250. if (status & 0x8)
  251. LOG_ERROR("Low Programming Voltage Detected, Operation Aborted");
  252. if (status & 0x10)
  253. LOG_ERROR("Program Error / Error in Setting Lock-Bit");
  254. if (status & 0x20)
  255. LOG_ERROR("Error in Block Erasure or Clear Lock-Bits");
  256. if (status & 0x40)
  257. LOG_ERROR("Block Erase Suspended");
  258. cfi_intel_clear_status_register(bank);
  259. }
  260. return status;
  261. }
  262. int cfi_spansion_wait_status_busy(flash_bank_t *bank, int timeout)
  263. {
  264. uint8_t status, oldstatus;
  265. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  266. oldstatus = cfi_get_u8(bank, 0, 0x0);
  267. do {
  268. status = cfi_get_u8(bank, 0, 0x0);
  269. if ((status ^ oldstatus) & 0x40) {
  270. if (status & cfi_info->status_poll_mask & 0x20) {
  271. oldstatus = cfi_get_u8(bank, 0, 0x0);
  272. status = cfi_get_u8(bank, 0, 0x0);
  273. if ((status ^ oldstatus) & 0x40) {
  274. LOG_ERROR("dq5 timeout, status: 0x%x", status);
  275. return(ERROR_FLASH_OPERATION_FAILED);
  276. } else {
  277. LOG_DEBUG("status: 0x%x", status);
  278. return(ERROR_OK);
  279. }
  280. }
  281. } else { /* no toggle: finished, OK */
  282. LOG_DEBUG("status: 0x%x", status);
  283. return(ERROR_OK);
  284. }
  285. oldstatus = status;
  286. alive_sleep(1);
  287. } while (timeout-- > 0);
  288. LOG_ERROR("timeout, status: 0x%x", status);
  289. return(ERROR_FLASH_BUSY);
  290. }
  291. static int cfi_read_intel_pri_ext(flash_bank_t *bank)
  292. {
  293. int retval;
  294. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  295. cfi_intel_pri_ext_t *pri_ext = malloc(sizeof(cfi_intel_pri_ext_t));
  296. target_t *target = bank->target;
  297. uint8_t command[8];
  298. cfi_info->pri_ext = pri_ext;
  299. pri_ext->pri[0] = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0);
  300. pri_ext->pri[1] = cfi_query_u8(bank, 0, cfi_info->pri_addr + 1);
  301. pri_ext->pri[2] = cfi_query_u8(bank, 0, cfi_info->pri_addr + 2);
  302. if ((pri_ext->pri[0] != 'P') || (pri_ext->pri[1] != 'R') || (pri_ext->pri[2] != 'I'))
  303. {
  304. cfi_command(bank, 0xf0, command);
  305. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  306. {
  307. return retval;
  308. }
  309. cfi_command(bank, 0xff, command);
  310. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  311. {
  312. return retval;
  313. }
  314. LOG_ERROR("Could not read bank flash bank information");
  315. return ERROR_FLASH_BANK_INVALID;
  316. }
  317. pri_ext->major_version = cfi_query_u8(bank, 0, cfi_info->pri_addr + 3);
  318. pri_ext->minor_version = cfi_query_u8(bank, 0, cfi_info->pri_addr + 4);
  319. 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);
  320. pri_ext->feature_support = cfi_query_u32(bank, 0, cfi_info->pri_addr + 5);
  321. pri_ext->suspend_cmd_support = cfi_query_u8(bank, 0, cfi_info->pri_addr + 9);
  322. pri_ext->blk_status_reg_mask = cfi_query_u16(bank, 0, cfi_info->pri_addr + 0xa);
  323. LOG_DEBUG("feature_support: 0x%" PRIx32 ", suspend_cmd_support: 0x%x, blk_status_reg_mask: 0x%x",
  324. pri_ext->feature_support,
  325. pri_ext->suspend_cmd_support,
  326. pri_ext->blk_status_reg_mask);
  327. pri_ext->vcc_optimal = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0xc);
  328. pri_ext->vpp_optimal = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0xd);
  329. LOG_DEBUG("Vcc opt: %1.1x.%1.1x, Vpp opt: %1.1x.%1.1x",
  330. (pri_ext->vcc_optimal & 0xf0) >> 4, pri_ext->vcc_optimal & 0x0f,
  331. (pri_ext->vpp_optimal & 0xf0) >> 4, pri_ext->vpp_optimal & 0x0f);
  332. pri_ext->num_protection_fields = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0xe);
  333. if (pri_ext->num_protection_fields != 1)
  334. {
  335. LOG_WARNING("expected one protection register field, but found %i", pri_ext->num_protection_fields);
  336. }
  337. pri_ext->prot_reg_addr = cfi_query_u16(bank, 0, cfi_info->pri_addr + 0xf);
  338. pri_ext->fact_prot_reg_size = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0x11);
  339. pri_ext->user_prot_reg_size = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0x12);
  340. 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);
  341. return ERROR_OK;
  342. }
  343. static int cfi_read_spansion_pri_ext(flash_bank_t *bank)
  344. {
  345. int retval;
  346. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  347. cfi_spansion_pri_ext_t *pri_ext = malloc(sizeof(cfi_spansion_pri_ext_t));
  348. target_t *target = bank->target;
  349. uint8_t command[8];
  350. cfi_info->pri_ext = pri_ext;
  351. pri_ext->pri[0] = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0);
  352. pri_ext->pri[1] = cfi_query_u8(bank, 0, cfi_info->pri_addr + 1);
  353. pri_ext->pri[2] = cfi_query_u8(bank, 0, cfi_info->pri_addr + 2);
  354. if ((pri_ext->pri[0] != 'P') || (pri_ext->pri[1] != 'R') || (pri_ext->pri[2] != 'I'))
  355. {
  356. cfi_command(bank, 0xf0, command);
  357. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  358. {
  359. return retval;
  360. }
  361. LOG_ERROR("Could not read spansion bank information");
  362. return ERROR_FLASH_BANK_INVALID;
  363. }
  364. pri_ext->major_version = cfi_query_u8(bank, 0, cfi_info->pri_addr + 3);
  365. pri_ext->minor_version = cfi_query_u8(bank, 0, cfi_info->pri_addr + 4);
  366. 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);
  367. pri_ext->SiliconRevision = cfi_query_u8(bank, 0, cfi_info->pri_addr + 5);
  368. pri_ext->EraseSuspend = cfi_query_u8(bank, 0, cfi_info->pri_addr + 6);
  369. pri_ext->BlkProt = cfi_query_u8(bank, 0, cfi_info->pri_addr + 7);
  370. pri_ext->TmpBlkUnprotect = cfi_query_u8(bank, 0, cfi_info->pri_addr + 8);
  371. pri_ext->BlkProtUnprot = cfi_query_u8(bank, 0, cfi_info->pri_addr + 9);
  372. pri_ext->SimultaneousOps = cfi_query_u8(bank, 0, cfi_info->pri_addr + 10);
  373. pri_ext->BurstMode = cfi_query_u8(bank, 0, cfi_info->pri_addr + 11);
  374. pri_ext->PageMode = cfi_query_u8(bank, 0, cfi_info->pri_addr + 12);
  375. pri_ext->VppMin = cfi_query_u8(bank, 0, cfi_info->pri_addr + 13);
  376. pri_ext->VppMax = cfi_query_u8(bank, 0, cfi_info->pri_addr + 14);
  377. pri_ext->TopBottom = cfi_query_u8(bank, 0, cfi_info->pri_addr + 15);
  378. LOG_DEBUG("Silicon Revision: 0x%x, Erase Suspend: 0x%x, Block protect: 0x%x", pri_ext->SiliconRevision,
  379. pri_ext->EraseSuspend, pri_ext->BlkProt);
  380. LOG_DEBUG("Temporary Unprotect: 0x%x, Block Protect Scheme: 0x%x, Simultaneous Ops: 0x%x", pri_ext->TmpBlkUnprotect,
  381. pri_ext->BlkProtUnprot, pri_ext->SimultaneousOps);
  382. LOG_DEBUG("Burst Mode: 0x%x, Page Mode: 0x%x, ", pri_ext->BurstMode, pri_ext->PageMode);
  383. LOG_DEBUG("Vpp min: %2.2d.%1.1d, Vpp max: %2.2d.%1.1x",
  384. (pri_ext->VppMin & 0xf0) >> 4, pri_ext->VppMin & 0x0f,
  385. (pri_ext->VppMax & 0xf0) >> 4, pri_ext->VppMax & 0x0f);
  386. LOG_DEBUG("WP# protection 0x%x", pri_ext->TopBottom);
  387. /* default values for implementation specific workarounds */
  388. pri_ext->_unlock1 = cfi_unlock_addresses[CFI_UNLOCK_555_2AA].unlock1;
  389. pri_ext->_unlock2 = cfi_unlock_addresses[CFI_UNLOCK_555_2AA].unlock2;
  390. pri_ext->_reversed_geometry = 0;
  391. return ERROR_OK;
  392. }
  393. static int cfi_read_atmel_pri_ext(flash_bank_t *bank)
  394. {
  395. int retval;
  396. cfi_atmel_pri_ext_t atmel_pri_ext;
  397. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  398. cfi_spansion_pri_ext_t *pri_ext = malloc(sizeof(cfi_spansion_pri_ext_t));
  399. target_t *target = bank->target;
  400. uint8_t command[8];
  401. /* ATMEL devices use the same CFI primary command set (0x2) as AMD/Spansion,
  402. * but a different primary extended query table.
  403. * We read the atmel table, and prepare a valid AMD/Spansion query table.
  404. */
  405. memset(pri_ext, 0, sizeof(cfi_spansion_pri_ext_t));
  406. cfi_info->pri_ext = pri_ext;
  407. atmel_pri_ext.pri[0] = cfi_query_u8(bank, 0, cfi_info->pri_addr + 0);
  408. atmel_pri_ext.pri[1] = cfi_query_u8(bank, 0, cfi_info->pri_addr + 1);
  409. atmel_pri_ext.pri[2] = cfi_query_u8(bank, 0, cfi_info->pri_addr + 2);
  410. if ((atmel_pri_ext.pri[0] != 'P') || (atmel_pri_ext.pri[1] != 'R') || (atmel_pri_ext.pri[2] != 'I'))
  411. {
  412. cfi_command(bank, 0xf0, command);
  413. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  414. {
  415. return retval;
  416. }
  417. LOG_ERROR("Could not read atmel bank information");
  418. return ERROR_FLASH_BANK_INVALID;
  419. }
  420. pri_ext->pri[0] = atmel_pri_ext.pri[0];
  421. pri_ext->pri[1] = atmel_pri_ext.pri[1];
  422. pri_ext->pri[2] = atmel_pri_ext.pri[2];
  423. atmel_pri_ext.major_version = cfi_query_u8(bank, 0, cfi_info->pri_addr + 3);
  424. atmel_pri_ext.minor_version = cfi_query_u8(bank, 0, cfi_info->pri_addr + 4);
  425. 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);
  426. pri_ext->major_version = atmel_pri_ext.major_version;
  427. pri_ext->minor_version = atmel_pri_ext.minor_version;
  428. atmel_pri_ext.features = cfi_query_u8(bank, 0, cfi_info->pri_addr + 5);
  429. atmel_pri_ext.bottom_boot = cfi_query_u8(bank, 0, cfi_info->pri_addr + 6);
  430. atmel_pri_ext.burst_mode = cfi_query_u8(bank, 0, cfi_info->pri_addr + 7);
  431. atmel_pri_ext.page_mode = cfi_query_u8(bank, 0, cfi_info->pri_addr + 8);
  432. LOG_DEBUG("features: 0x%2.2x, bottom_boot: 0x%2.2x, burst_mode: 0x%2.2x, page_mode: 0x%2.2x",
  433. atmel_pri_ext.features, atmel_pri_ext.bottom_boot, atmel_pri_ext.burst_mode, atmel_pri_ext.page_mode);
  434. if (atmel_pri_ext.features & 0x02)
  435. pri_ext->EraseSuspend = 2;
  436. if (atmel_pri_ext.bottom_boot)
  437. pri_ext->TopBottom = 2;
  438. else
  439. pri_ext->TopBottom = 3;
  440. pri_ext->_unlock1 = cfi_unlock_addresses[CFI_UNLOCK_555_2AA].unlock1;
  441. pri_ext->_unlock2 = cfi_unlock_addresses[CFI_UNLOCK_555_2AA].unlock2;
  442. return ERROR_OK;
  443. }
  444. static int cfi_read_0002_pri_ext(flash_bank_t *bank)
  445. {
  446. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  447. if (cfi_info->manufacturer == CFI_MFR_ATMEL)
  448. {
  449. return cfi_read_atmel_pri_ext(bank);
  450. }
  451. else
  452. {
  453. return cfi_read_spansion_pri_ext(bank);
  454. }
  455. }
  456. static int cfi_spansion_info(struct flash_bank_s *bank, char *buf, int buf_size)
  457. {
  458. int printed;
  459. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  460. cfi_spansion_pri_ext_t *pri_ext = cfi_info->pri_ext;
  461. printed = snprintf(buf, buf_size, "\nSpansion primary algorithm extend information:\n");
  462. buf += printed;
  463. buf_size -= printed;
  464. printed = snprintf(buf, buf_size, "pri: '%c%c%c', version: %c.%c\n", pri_ext->pri[0],
  465. pri_ext->pri[1], pri_ext->pri[2],
  466. pri_ext->major_version, pri_ext->minor_version);
  467. buf += printed;
  468. buf_size -= printed;
  469. printed = snprintf(buf, buf_size, "Silicon Rev.: 0x%x, Address Sensitive unlock: 0x%x\n",
  470. (pri_ext->SiliconRevision) >> 2,
  471. (pri_ext->SiliconRevision) & 0x03);
  472. buf += printed;
  473. buf_size -= printed;
  474. printed = snprintf(buf, buf_size, "Erase Suspend: 0x%x, Sector Protect: 0x%x\n",
  475. pri_ext->EraseSuspend,
  476. pri_ext->BlkProt);
  477. buf += printed;
  478. buf_size -= printed;
  479. printed = snprintf(buf, buf_size, "VppMin: %2.2d.%1.1x, VppMax: %2.2d.%1.1x\n",
  480. (pri_ext->VppMin & 0xf0) >> 4, pri_ext->VppMin & 0x0f,
  481. (pri_ext->VppMax & 0xf0) >> 4, pri_ext->VppMax & 0x0f);
  482. return ERROR_OK;
  483. }
  484. static int cfi_intel_info(struct flash_bank_s *bank, char *buf, int buf_size)
  485. {
  486. int printed;
  487. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  488. cfi_intel_pri_ext_t *pri_ext = cfi_info->pri_ext;
  489. printed = snprintf(buf, buf_size, "\nintel primary algorithm extend information:\n");
  490. buf += printed;
  491. buf_size -= printed;
  492. 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);
  493. buf += printed;
  494. buf_size -= printed;
  495. 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);
  496. buf += printed;
  497. buf_size -= printed;
  498. printed = snprintf(buf, buf_size, "Vcc opt: %1.1x.%1.1x, Vpp opt: %1.1x.%1.1x\n",
  499. (pri_ext->vcc_optimal & 0xf0) >> 4, pri_ext->vcc_optimal & 0x0f,
  500. (pri_ext->vpp_optimal & 0xf0) >> 4, pri_ext->vpp_optimal & 0x0f);
  501. buf += printed;
  502. buf_size -= printed;
  503. 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);
  504. return ERROR_OK;
  505. }
  506. static int cfi_register_commands(struct command_context_s *cmd_ctx)
  507. {
  508. /*command_t *cfi_cmd = */
  509. register_command(cmd_ctx, NULL, "cfi", NULL, COMMAND_ANY, "flash bank cfi <base> <size> <chip_width> <bus_width> <targetNum> [jedec_probe/x16_as_x8]");
  510. /*
  511. register_command(cmd_ctx, cfi_cmd, "part_id", cfi_handle_part_id_command, COMMAND_EXEC,
  512. "print part id of cfi flash bank <num>");
  513. */
  514. return ERROR_OK;
  515. }
  516. /* flash_bank cfi <base> <size> <chip_width> <bus_width> <target#> [options]
  517. */
  518. static int cfi_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
  519. {
  520. cfi_flash_bank_t *cfi_info;
  521. int i;
  522. (void) cmd_ctx;
  523. (void) cmd;
  524. if (argc < 6)
  525. {
  526. LOG_WARNING("incomplete flash_bank cfi configuration");
  527. return ERROR_FLASH_BANK_INVALID;
  528. }
  529. if ((strtoul(args[4], NULL, 0) > CFI_MAX_CHIP_WIDTH)
  530. || (strtoul(args[3], NULL, 0) > CFI_MAX_BUS_WIDTH))
  531. {
  532. LOG_ERROR("chip and bus width have to specified in bytes");
  533. return ERROR_FLASH_BANK_INVALID;
  534. }
  535. cfi_info = malloc(sizeof(cfi_flash_bank_t));
  536. cfi_info->probed = 0;
  537. bank->driver_priv = cfi_info;
  538. cfi_info->write_algorithm = NULL;
  539. cfi_info->x16_as_x8 = 0;
  540. cfi_info->jedec_probe = 0;
  541. cfi_info->not_cfi = 0;
  542. for (i = 6; i < argc; i++)
  543. {
  544. if (strcmp(args[i], "x16_as_x8") == 0)
  545. {
  546. cfi_info->x16_as_x8 = 1;
  547. }
  548. else if (strcmp(args[i], "jedec_probe") == 0)
  549. {
  550. cfi_info->jedec_probe = 1;
  551. }
  552. }
  553. cfi_info->write_algorithm = NULL;
  554. /* bank wasn't probed yet */
  555. cfi_info->qry[0] = -1;
  556. return ERROR_OK;
  557. }
  558. static int cfi_intel_erase(struct flash_bank_s *bank, int first, int last)
  559. {
  560. int retval;
  561. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  562. target_t *target = bank->target;
  563. uint8_t command[8];
  564. int i;
  565. cfi_intel_clear_status_register(bank);
  566. for (i = first; i <= last; i++)
  567. {
  568. cfi_command(bank, 0x20, command);
  569. if ((retval = target_write_memory(target, flash_address(bank, i, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  570. {
  571. return retval;
  572. }
  573. cfi_command(bank, 0xd0, command);
  574. if ((retval = target_write_memory(target, flash_address(bank, i, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  575. {
  576. return retval;
  577. }
  578. if (cfi_intel_wait_status_busy(bank, 1000 * (1 << cfi_info->block_erase_timeout_typ)) == 0x80)
  579. bank->sectors[i].is_erased = 1;
  580. else
  581. {
  582. cfi_command(bank, 0xff, command);
  583. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  584. {
  585. return retval;
  586. }
  587. LOG_ERROR("couldn't erase block %i of flash bank at base 0x%" PRIx32 , i, bank->base);
  588. return ERROR_FLASH_OPERATION_FAILED;
  589. }
  590. }
  591. cfi_command(bank, 0xff, command);
  592. return target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
  593. }
  594. static int cfi_spansion_erase(struct flash_bank_s *bank, int first, int last)
  595. {
  596. int retval;
  597. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  598. cfi_spansion_pri_ext_t *pri_ext = cfi_info->pri_ext;
  599. target_t *target = bank->target;
  600. uint8_t command[8];
  601. int i;
  602. for (i = first; i <= last; i++)
  603. {
  604. cfi_command(bank, 0xaa, command);
  605. if ((retval = target_write_memory(target, flash_address(bank, 0, pri_ext->_unlock1), bank->bus_width, 1, command)) != ERROR_OK)
  606. {
  607. return retval;
  608. }
  609. cfi_command(bank, 0x55, command);
  610. if ((retval = target_write_memory(target, flash_address(bank, 0, pri_ext->_unlock2), bank->bus_width, 1, command)) != ERROR_OK)
  611. {
  612. return retval;
  613. }
  614. cfi_command(bank, 0x80, command);
  615. if ((retval = target_write_memory(target, flash_address(bank, 0, pri_ext->_unlock1), bank->bus_width, 1, command)) != ERROR_OK)
  616. {
  617. return retval;
  618. }
  619. cfi_command(bank, 0xaa, command);
  620. if ((retval = target_write_memory(target, flash_address(bank, 0, pri_ext->_unlock1), bank->bus_width, 1, command)) != ERROR_OK)
  621. {
  622. return retval;
  623. }
  624. cfi_command(bank, 0x55, command);
  625. if ((retval = target_write_memory(target, flash_address(bank, 0, pri_ext->_unlock2), bank->bus_width, 1, command)) != ERROR_OK)
  626. {
  627. return retval;
  628. }
  629. cfi_command(bank, 0x30, command);
  630. if ((retval = target_write_memory(target, flash_address(bank, i, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  631. {
  632. return retval;
  633. }
  634. if (cfi_spansion_wait_status_busy(bank, 1000 * (1 << cfi_info->block_erase_timeout_typ)) == ERROR_OK)
  635. bank->sectors[i].is_erased = 1;
  636. else
  637. {
  638. cfi_command(bank, 0xf0, command);
  639. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  640. {
  641. return retval;
  642. }
  643. LOG_ERROR("couldn't erase block %i of flash bank at base 0x%" PRIx32, i, bank->base);
  644. return ERROR_FLASH_OPERATION_FAILED;
  645. }
  646. }
  647. cfi_command(bank, 0xf0, command);
  648. return target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
  649. }
  650. static int cfi_erase(struct flash_bank_s *bank, int first, int last)
  651. {
  652. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  653. if (bank->target->state != TARGET_HALTED)
  654. {
  655. LOG_ERROR("Target not halted");
  656. return ERROR_TARGET_NOT_HALTED;
  657. }
  658. if ((first < 0) || (last < first) || (last >= bank->num_sectors))
  659. {
  660. return ERROR_FLASH_SECTOR_INVALID;
  661. }
  662. if (cfi_info->qry[0] != 'Q')
  663. return ERROR_FLASH_BANK_NOT_PROBED;
  664. switch (cfi_info->pri_id)
  665. {
  666. case 1:
  667. case 3:
  668. return cfi_intel_erase(bank, first, last);
  669. break;
  670. case 2:
  671. return cfi_spansion_erase(bank, first, last);
  672. break;
  673. default:
  674. LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
  675. break;
  676. }
  677. return ERROR_OK;
  678. }
  679. static int cfi_intel_protect(struct flash_bank_s *bank, int set, int first, int last)
  680. {
  681. int retval;
  682. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  683. cfi_intel_pri_ext_t *pri_ext = cfi_info->pri_ext;
  684. target_t *target = bank->target;
  685. uint8_t command[8];
  686. int retry = 0;
  687. int i;
  688. /* if the device supports neither legacy lock/unlock (bit 3) nor
  689. * instant individual block locking (bit 5).
  690. */
  691. if (!(pri_ext->feature_support & 0x28))
  692. return ERROR_FLASH_OPERATION_FAILED;
  693. cfi_intel_clear_status_register(bank);
  694. for (i = first; i <= last; i++)
  695. {
  696. cfi_command(bank, 0x60, command);
  697. LOG_DEBUG("address: 0x%4.4" PRIx32 ", command: 0x%4.4" PRIx32, flash_address(bank, i, 0x0), target_buffer_get_u32(target, command));
  698. if ((retval = target_write_memory(target, flash_address(bank, i, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  699. {
  700. return retval;
  701. }
  702. if (set)
  703. {
  704. cfi_command(bank, 0x01, command);
  705. LOG_DEBUG("address: 0x%4.4" PRIx32 ", command: 0x%4.4" PRIx32 , flash_address(bank, i, 0x0), target_buffer_get_u32(target, command));
  706. if ((retval = target_write_memory(target, flash_address(bank, i, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  707. {
  708. return retval;
  709. }
  710. bank->sectors[i].is_protected = 1;
  711. }
  712. else
  713. {
  714. cfi_command(bank, 0xd0, command);
  715. LOG_DEBUG("address: 0x%4.4" PRIx32 ", command: 0x%4.4" PRIx32, flash_address(bank, i, 0x0), target_buffer_get_u32(target, command));
  716. if ((retval = target_write_memory(target, flash_address(bank, i, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  717. {
  718. return retval;
  719. }
  720. bank->sectors[i].is_protected = 0;
  721. }
  722. /* instant individual block locking doesn't require reading of the status register */
  723. if (!(pri_ext->feature_support & 0x20))
  724. {
  725. /* Clear lock bits operation may take up to 1.4s */
  726. cfi_intel_wait_status_busy(bank, 1400);
  727. }
  728. else
  729. {
  730. uint8_t block_status;
  731. /* read block lock bit, to verify status */
  732. cfi_command(bank, 0x90, command);
  733. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x55), bank->bus_width, 1, command)) != ERROR_OK)
  734. {
  735. return retval;
  736. }
  737. block_status = cfi_get_u8(bank, i, 0x2);
  738. if ((block_status & 0x1) != set)
  739. {
  740. LOG_ERROR("couldn't change block lock status (set = %i, block_status = 0x%2.2x)", set, block_status);
  741. cfi_command(bank, 0x70, command);
  742. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x55), bank->bus_width, 1, command)) != ERROR_OK)
  743. {
  744. return retval;
  745. }
  746. cfi_intel_wait_status_busy(bank, 10);
  747. if (retry > 10)
  748. return ERROR_FLASH_OPERATION_FAILED;
  749. else
  750. {
  751. i--;
  752. retry++;
  753. }
  754. }
  755. }
  756. }
  757. /* if the device doesn't support individual block lock bits set/clear,
  758. * all blocks have been unlocked in parallel, so we set those that should be protected
  759. */
  760. if ((!set) && (!(pri_ext->feature_support & 0x20)))
  761. {
  762. for (i = 0; i < bank->num_sectors; i++)
  763. {
  764. if (bank->sectors[i].is_protected == 1)
  765. {
  766. cfi_intel_clear_status_register(bank);
  767. cfi_command(bank, 0x60, command);
  768. if ((retval = target_write_memory(target, flash_address(bank, i, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  769. {
  770. return retval;
  771. }
  772. cfi_command(bank, 0x01, command);
  773. if ((retval = target_write_memory(target, flash_address(bank, i, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  774. {
  775. return retval;
  776. }
  777. cfi_intel_wait_status_busy(bank, 100);
  778. }
  779. }
  780. }
  781. cfi_command(bank, 0xff, command);
  782. return target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
  783. }
  784. static int cfi_protect(struct flash_bank_s *bank, int set, int first, int last)
  785. {
  786. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  787. if (bank->target->state != TARGET_HALTED)
  788. {
  789. LOG_ERROR("Target not halted");
  790. return ERROR_TARGET_NOT_HALTED;
  791. }
  792. if ((first < 0) || (last < first) || (last >= bank->num_sectors))
  793. {
  794. return ERROR_FLASH_SECTOR_INVALID;
  795. }
  796. if (cfi_info->qry[0] != 'Q')
  797. return ERROR_FLASH_BANK_NOT_PROBED;
  798. switch (cfi_info->pri_id)
  799. {
  800. case 1:
  801. case 3:
  802. cfi_intel_protect(bank, set, first, last);
  803. break;
  804. default:
  805. LOG_ERROR("protect: cfi primary command set %i unsupported", cfi_info->pri_id);
  806. break;
  807. }
  808. return ERROR_OK;
  809. }
  810. /* FIXME Replace this by a simple memcpy() - still unsure about sideeffects */
  811. static void cfi_add_byte(struct flash_bank_s *bank, uint8_t *word, uint8_t byte)
  812. {
  813. /* target_t *target = bank->target; */
  814. int i;
  815. /* NOTE:
  816. * The data to flash must not be changed in endian! We write a bytestrem in
  817. * target byte order already. Only the control and status byte lane of the flash
  818. * WSM is interpreted by the CPU in different ways, when read a uint16_t or uint32_t
  819. * word (data seems to be in the upper or lower byte lane for uint16_t accesses).
  820. */
  821. #if 0
  822. if (target->endianness == TARGET_LITTLE_ENDIAN)
  823. {
  824. #endif
  825. /* shift bytes */
  826. for (i = 0; i < bank->bus_width - 1; i++)
  827. word[i] = word[i + 1];
  828. word[bank->bus_width - 1] = byte;
  829. #if 0
  830. }
  831. else
  832. {
  833. /* shift bytes */
  834. for (i = bank->bus_width - 1; i > 0; i--)
  835. word[i] = word[i - 1];
  836. word[0] = byte;
  837. }
  838. #endif
  839. }
  840. /* Convert code image to target endian */
  841. /* FIXME create general block conversion fcts in target.c?) */
  842. static void cfi_fix_code_endian(target_t *target, uint8_t *dest, const uint32_t *src, uint32_t count)
  843. {
  844. uint32_t i;
  845. for (i = 0; i< count; i++)
  846. {
  847. target_buffer_set_u32(target, dest, *src);
  848. dest += 4;
  849. src++;
  850. }
  851. }
  852. static uint32_t cfi_command_val(flash_bank_t *bank, uint8_t cmd)
  853. {
  854. target_t *target = bank->target;
  855. uint8_t buf[CFI_MAX_BUS_WIDTH];
  856. cfi_command(bank, cmd, buf);
  857. switch (bank->bus_width)
  858. {
  859. case 1 :
  860. return buf[0];
  861. break;
  862. case 2 :
  863. return target_buffer_get_u16(target, buf);
  864. break;
  865. case 4 :
  866. return target_buffer_get_u32(target, buf);
  867. break;
  868. default :
  869. LOG_ERROR("Unsupported bank buswidth %d, can't do block memory writes", bank->bus_width);
  870. return 0;
  871. }
  872. }
  873. static int cfi_intel_write_block(struct flash_bank_s *bank, uint8_t *buffer, uint32_t address, uint32_t count)
  874. {
  875. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  876. target_t *target = bank->target;
  877. reg_param_t reg_params[7];
  878. armv4_5_algorithm_t armv4_5_info;
  879. working_area_t *source;
  880. uint32_t buffer_size = 32768;
  881. uint32_t write_command_val, busy_pattern_val, error_pattern_val;
  882. /* algorithm register usage:
  883. * r0: source address (in RAM)
  884. * r1: target address (in Flash)
  885. * r2: count
  886. * r3: flash write command
  887. * r4: status byte (returned to host)
  888. * r5: busy test pattern
  889. * r6: error test pattern
  890. */
  891. static const uint32_t word_32_code[] = {
  892. 0xe4904004, /* loop: ldr r4, [r0], #4 */
  893. 0xe5813000, /* str r3, [r1] */
  894. 0xe5814000, /* str r4, [r1] */
  895. 0xe5914000, /* busy: ldr r4, [r1] */
  896. 0xe0047005, /* and r7, r4, r5 */
  897. 0xe1570005, /* cmp r7, r5 */
  898. 0x1afffffb, /* bne busy */
  899. 0xe1140006, /* tst r4, r6 */
  900. 0x1a000003, /* bne done */
  901. 0xe2522001, /* subs r2, r2, #1 */
  902. 0x0a000001, /* beq done */
  903. 0xe2811004, /* add r1, r1 #4 */
  904. 0xeafffff2, /* b loop */
  905. 0xeafffffe /* done: b -2 */
  906. };
  907. static const uint32_t word_16_code[] = {
  908. 0xe0d040b2, /* loop: ldrh r4, [r0], #2 */
  909. 0xe1c130b0, /* strh r3, [r1] */
  910. 0xe1c140b0, /* strh r4, [r1] */
  911. 0xe1d140b0, /* busy ldrh r4, [r1] */
  912. 0xe0047005, /* and r7, r4, r5 */
  913. 0xe1570005, /* cmp r7, r5 */
  914. 0x1afffffb, /* bne busy */
  915. 0xe1140006, /* tst r4, r6 */
  916. 0x1a000003, /* bne done */
  917. 0xe2522001, /* subs r2, r2, #1 */
  918. 0x0a000001, /* beq done */
  919. 0xe2811002, /* add r1, r1 #2 */
  920. 0xeafffff2, /* b loop */
  921. 0xeafffffe /* done: b -2 */
  922. };
  923. static const uint32_t word_8_code[] = {
  924. 0xe4d04001, /* loop: ldrb r4, [r0], #1 */
  925. 0xe5c13000, /* strb r3, [r1] */
  926. 0xe5c14000, /* strb r4, [r1] */
  927. 0xe5d14000, /* busy ldrb r4, [r1] */
  928. 0xe0047005, /* and r7, r4, r5 */
  929. 0xe1570005, /* cmp r7, r5 */
  930. 0x1afffffb, /* bne busy */
  931. 0xe1140006, /* tst r4, r6 */
  932. 0x1a000003, /* bne done */
  933. 0xe2522001, /* subs r2, r2, #1 */
  934. 0x0a000001, /* beq done */
  935. 0xe2811001, /* add r1, r1 #1 */
  936. 0xeafffff2, /* b loop */
  937. 0xeafffffe /* done: b -2 */
  938. };
  939. uint8_t target_code[4*CFI_MAX_INTEL_CODESIZE];
  940. const uint32_t *target_code_src;
  941. uint32_t target_code_size;
  942. int retval = ERROR_OK;
  943. cfi_intel_clear_status_register(bank);
  944. armv4_5_info.common_magic = ARMV4_5_COMMON_MAGIC;
  945. armv4_5_info.core_mode = ARMV4_5_MODE_SVC;
  946. armv4_5_info.core_state = ARMV4_5_STATE_ARM;
  947. /* If we are setting up the write_algorith, we need target_code_src */
  948. /* if not we only need target_code_size. */
  949. /* */
  950. /* However, we don't want to create multiple code paths, so we */
  951. /* do the unecessary evaluation of target_code_src, which the */
  952. /* compiler will probably nicely optimize away if not needed */
  953. /* prepare algorithm code for target endian */
  954. switch (bank->bus_width)
  955. {
  956. case 1 :
  957. target_code_src = word_8_code;
  958. target_code_size = sizeof(word_8_code);
  959. break;
  960. case 2 :
  961. target_code_src = word_16_code;
  962. target_code_size = sizeof(word_16_code);
  963. break;
  964. case 4 :
  965. target_code_src = word_32_code;
  966. target_code_size = sizeof(word_32_code);
  967. break;
  968. default:
  969. LOG_ERROR("Unsupported bank buswidth %d, can't do block memory writes", bank->bus_width);
  970. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  971. }
  972. /* flash write code */
  973. if (!cfi_info->write_algorithm)
  974. {
  975. if (target_code_size > sizeof(target_code))
  976. {
  977. LOG_WARNING("Internal error - target code buffer to small. Increase CFI_MAX_INTEL_CODESIZE and recompile.");
  978. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  979. }
  980. cfi_fix_code_endian(target, target_code, target_code_src, target_code_size / 4);
  981. /* Get memory for block write handler */
  982. retval = target_alloc_working_area(target, target_code_size, &cfi_info->write_algorithm);
  983. if (retval != ERROR_OK)
  984. {
  985. LOG_WARNING("No working area available, can't do block memory writes");
  986. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  987. };
  988. /* write algorithm code to working area */
  989. retval = target_write_buffer(target, cfi_info->write_algorithm->address, target_code_size, target_code);
  990. if (retval != ERROR_OK)
  991. {
  992. LOG_ERROR("Unable to write block write code to target");
  993. goto cleanup;
  994. }
  995. }
  996. /* Get a workspace buffer for the data to flash starting with 32k size.
  997. Half size until buffer would be smaller 256 Bytem then fail back */
  998. /* FIXME Why 256 bytes, why not 32 bytes (smallest flash write page */
  999. while (target_alloc_working_area(target, buffer_size, &source) != ERROR_OK)
  1000. {
  1001. buffer_size /= 2;
  1002. if (buffer_size <= 256)
  1003. {
  1004. LOG_WARNING("no large enough working area available, can't do block memory writes");
  1005. retval = ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1006. goto cleanup;
  1007. }
  1008. };
  1009. /* setup algo registers */
  1010. init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
  1011. init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
  1012. init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT);
  1013. init_reg_param(&reg_params[3], "r3", 32, PARAM_OUT);
  1014. init_reg_param(&reg_params[4], "r4", 32, PARAM_IN);
  1015. init_reg_param(&reg_params[5], "r5", 32, PARAM_OUT);
  1016. init_reg_param(&reg_params[6], "r6", 32, PARAM_OUT);
  1017. /* prepare command and status register patterns */
  1018. write_command_val = cfi_command_val(bank, 0x40);
  1019. busy_pattern_val = cfi_command_val(bank, 0x80);
  1020. error_pattern_val = cfi_command_val(bank, 0x7e);
  1021. LOG_INFO("Using target buffer at 0x%08" PRIx32 " and of size 0x%04" PRIx32, source->address, buffer_size);
  1022. /* Programming main loop */
  1023. while (count > 0)
  1024. {
  1025. uint32_t thisrun_count = (count > buffer_size) ? buffer_size : count;
  1026. uint32_t wsm_error;
  1027. if ((retval = target_write_buffer(target, source->address, thisrun_count, buffer)) != ERROR_OK)
  1028. {
  1029. goto cleanup;
  1030. }
  1031. buf_set_u32(reg_params[0].value, 0, 32, source->address);
  1032. buf_set_u32(reg_params[1].value, 0, 32, address);
  1033. buf_set_u32(reg_params[2].value, 0, 32, thisrun_count / bank->bus_width);
  1034. buf_set_u32(reg_params[3].value, 0, 32, write_command_val);
  1035. buf_set_u32(reg_params[5].value, 0, 32, busy_pattern_val);
  1036. buf_set_u32(reg_params[6].value, 0, 32, error_pattern_val);
  1037. LOG_INFO("Write 0x%04" PRIx32 " bytes to flash at 0x%08" PRIx32 , thisrun_count, address);
  1038. /* Execute algorithm, assume breakpoint for last instruction */
  1039. retval = target_run_algorithm(target, 0, NULL, 7, reg_params,
  1040. cfi_info->write_algorithm->address,
  1041. cfi_info->write_algorithm->address + target_code_size - sizeof(uint32_t),
  1042. 10000, /* 10s should be enough for max. 32k of data */
  1043. &armv4_5_info);
  1044. /* On failure try a fall back to direct word writes */
  1045. if (retval != ERROR_OK)
  1046. {
  1047. cfi_intel_clear_status_register(bank);
  1048. LOG_ERROR("Execution of flash algorythm failed. Can't fall back. Please report.");
  1049. retval = ERROR_FLASH_OPERATION_FAILED;
  1050. /* retval = ERROR_TARGET_RESOURCE_NOT_AVAILABLE; */
  1051. /* FIXME To allow fall back or recovery, we must save the actual status
  1052. somewhere, so that a higher level code can start recovery. */
  1053. goto cleanup;
  1054. }
  1055. /* Check return value from algo code */
  1056. wsm_error = buf_get_u32(reg_params[4].value, 0, 32) & error_pattern_val;
  1057. if (wsm_error)
  1058. {
  1059. /* read status register (outputs debug inforation) */
  1060. cfi_intel_wait_status_busy(bank, 100);
  1061. cfi_intel_clear_status_register(bank);
  1062. retval = ERROR_FLASH_OPERATION_FAILED;
  1063. goto cleanup;
  1064. }
  1065. buffer += thisrun_count;
  1066. address += thisrun_count;
  1067. count -= thisrun_count;
  1068. }
  1069. /* free up resources */
  1070. cleanup:
  1071. if (source)
  1072. target_free_working_area(target, source);
  1073. if (cfi_info->write_algorithm)
  1074. {
  1075. target_free_working_area(target, cfi_info->write_algorithm);
  1076. cfi_info->write_algorithm = NULL;
  1077. }
  1078. destroy_reg_param(&reg_params[0]);
  1079. destroy_reg_param(&reg_params[1]);
  1080. destroy_reg_param(&reg_params[2]);
  1081. destroy_reg_param(&reg_params[3]);
  1082. destroy_reg_param(&reg_params[4]);
  1083. destroy_reg_param(&reg_params[5]);
  1084. destroy_reg_param(&reg_params[6]);
  1085. return retval;
  1086. }
  1087. static int cfi_spansion_write_block(struct flash_bank_s *bank, uint8_t *buffer, uint32_t address, uint32_t count)
  1088. {
  1089. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  1090. cfi_spansion_pri_ext_t *pri_ext = cfi_info->pri_ext;
  1091. target_t *target = bank->target;
  1092. reg_param_t reg_params[10];
  1093. armv4_5_algorithm_t armv4_5_info;
  1094. working_area_t *source;
  1095. uint32_t buffer_size = 32768;
  1096. uint32_t status;
  1097. int retval, retvaltemp;
  1098. int exit_code = ERROR_OK;
  1099. /* input parameters - */
  1100. /* R0 = source address */
  1101. /* R1 = destination address */
  1102. /* R2 = number of writes */
  1103. /* R3 = flash write command */
  1104. /* R4 = constant to mask DQ7 bits (also used for Dq5 with shift) */
  1105. /* output parameters - */
  1106. /* R5 = 0x80 ok 0x00 bad */
  1107. /* temp registers - */
  1108. /* R6 = value read from flash to test status */
  1109. /* R7 = holding register */
  1110. /* unlock registers - */
  1111. /* R8 = unlock1_addr */
  1112. /* R9 = unlock1_cmd */
  1113. /* R10 = unlock2_addr */
  1114. /* R11 = unlock2_cmd */
  1115. static const uint32_t word_32_code[] = {
  1116. /* 00008100 <sp_32_code>: */
  1117. 0xe4905004, /* ldr r5, [r0], #4 */
  1118. 0xe5889000, /* str r9, [r8] */
  1119. 0xe58ab000, /* str r11, [r10] */
  1120. 0xe5883000, /* str r3, [r8] */
  1121. 0xe5815000, /* str r5, [r1] */
  1122. 0xe1a00000, /* nop */
  1123. /* */
  1124. /* 00008110 <sp_32_busy>: */
  1125. 0xe5916000, /* ldr r6, [r1] */
  1126. 0xe0257006, /* eor r7, r5, r6 */
  1127. 0xe0147007, /* ands r7, r4, r7 */
  1128. 0x0a000007, /* beq 8140 <sp_32_cont> ; b if DQ7 == Data7 */
  1129. 0xe0166124, /* ands r6, r6, r4, lsr #2 */
  1130. 0x0afffff9, /* beq 8110 <sp_32_busy> ; b if DQ5 low */
  1131. 0xe5916000, /* ldr r6, [r1] */
  1132. 0xe0257006, /* eor r7, r5, r6 */
  1133. 0xe0147007, /* ands r7, r4, r7 */
  1134. 0x0a000001, /* beq 8140 <sp_32_cont> ; b if DQ7 == Data7 */
  1135. 0xe3a05000, /* mov r5, #0 ; 0x0 - return 0x00, error */
  1136. 0x1a000004, /* bne 8154 <sp_32_done> */
  1137. /* */
  1138. /* 00008140 <sp_32_cont>: */
  1139. 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
  1140. 0x03a05080, /* moveq r5, #128 ; 0x80 */
  1141. 0x0a000001, /* beq 8154 <sp_32_done> */
  1142. 0xe2811004, /* add r1, r1, #4 ; 0x4 */
  1143. 0xeaffffe8, /* b 8100 <sp_32_code> */
  1144. /* */
  1145. /* 00008154 <sp_32_done>: */
  1146. 0xeafffffe /* b 8154 <sp_32_done> */
  1147. };
  1148. static const uint32_t word_16_code[] = {
  1149. /* 00008158 <sp_16_code>: */
  1150. 0xe0d050b2, /* ldrh r5, [r0], #2 */
  1151. 0xe1c890b0, /* strh r9, [r8] */
  1152. 0xe1cab0b0, /* strh r11, [r10] */
  1153. 0xe1c830b0, /* strh r3, [r8] */
  1154. 0xe1c150b0, /* strh r5, [r1] */
  1155. 0xe1a00000, /* nop (mov r0,r0) */
  1156. /* */
  1157. /* 00008168 <sp_16_busy>: */
  1158. 0xe1d160b0, /* ldrh r6, [r1] */
  1159. 0xe0257006, /* eor r7, r5, r6 */
  1160. 0xe0147007, /* ands r7, r4, r7 */
  1161. 0x0a000007, /* beq 8198 <sp_16_cont> */
  1162. 0xe0166124, /* ands r6, r6, r4, lsr #2 */
  1163. 0x0afffff9, /* beq 8168 <sp_16_busy> */
  1164. 0xe1d160b0, /* ldrh r6, [r1] */
  1165. 0xe0257006, /* eor r7, r5, r6 */
  1166. 0xe0147007, /* ands r7, r4, r7 */
  1167. 0x0a000001, /* beq 8198 <sp_16_cont> */
  1168. 0xe3a05000, /* mov r5, #0 ; 0x0 */
  1169. 0x1a000004, /* bne 81ac <sp_16_done> */
  1170. /* */
  1171. /* 00008198 <sp_16_cont>: */
  1172. 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
  1173. 0x03a05080, /* moveq r5, #128 ; 0x80 */
  1174. 0x0a000001, /* beq 81ac <sp_16_done> */
  1175. 0xe2811002, /* add r1, r1, #2 ; 0x2 */
  1176. 0xeaffffe8, /* b 8158 <sp_16_code> */
  1177. /* */
  1178. /* 000081ac <sp_16_done>: */
  1179. 0xeafffffe /* b 81ac <sp_16_done> */
  1180. };
  1181. static const uint32_t word_8_code[] = {
  1182. /* 000081b0 <sp_16_code_end>: */
  1183. 0xe4d05001, /* ldrb r5, [r0], #1 */
  1184. 0xe5c89000, /* strb r9, [r8] */
  1185. 0xe5cab000, /* strb r11, [r10] */
  1186. 0xe5c83000, /* strb r3, [r8] */
  1187. 0xe5c15000, /* strb r5, [r1] */
  1188. 0xe1a00000, /* nop (mov r0,r0) */
  1189. /* */
  1190. /* 000081c0 <sp_8_busy>: */
  1191. 0xe5d16000, /* ldrb r6, [r1] */
  1192. 0xe0257006, /* eor r7, r5, r6 */
  1193. 0xe0147007, /* ands r7, r4, r7 */
  1194. 0x0a000007, /* beq 81f0 <sp_8_cont> */
  1195. 0xe0166124, /* ands r6, r6, r4, lsr #2 */
  1196. 0x0afffff9, /* beq 81c0 <sp_8_busy> */
  1197. 0xe5d16000, /* ldrb r6, [r1] */
  1198. 0xe0257006, /* eor r7, r5, r6 */
  1199. 0xe0147007, /* ands r7, r4, r7 */
  1200. 0x0a000001, /* beq 81f0 <sp_8_cont> */
  1201. 0xe3a05000, /* mov r5, #0 ; 0x0 */
  1202. 0x1a000004, /* bne 8204 <sp_8_done> */
  1203. /* */
  1204. /* 000081f0 <sp_8_cont>: */
  1205. 0xe2522001, /* subs r2, r2, #1 ; 0x1 */
  1206. 0x03a05080, /* moveq r5, #128 ; 0x80 */
  1207. 0x0a000001, /* beq 8204 <sp_8_done> */
  1208. 0xe2811001, /* add r1, r1, #1 ; 0x1 */
  1209. 0xeaffffe8, /* b 81b0 <sp_16_code_end> */
  1210. /* */
  1211. /* 00008204 <sp_8_done>: */
  1212. 0xeafffffe /* b 8204 <sp_8_done> */
  1213. };
  1214. armv4_5_info.common_magic = ARMV4_5_COMMON_MAGIC;
  1215. armv4_5_info.core_mode = ARMV4_5_MODE_SVC;
  1216. armv4_5_info.core_state = ARMV4_5_STATE_ARM;
  1217. /* flash write code */
  1218. if (!cfi_info->write_algorithm)
  1219. {
  1220. uint8_t *target_code;
  1221. int target_code_size;
  1222. const uint32_t *src;
  1223. /* convert bus-width dependent algorithm code to correct endiannes */
  1224. switch (bank->bus_width)
  1225. {
  1226. case 1:
  1227. src = word_8_code;
  1228. target_code_size = sizeof(word_8_code);
  1229. break;
  1230. case 2:
  1231. src = word_16_code;
  1232. target_code_size = sizeof(word_16_code);
  1233. break;
  1234. case 4:
  1235. src = word_32_code;
  1236. target_code_size = sizeof(word_32_code);
  1237. break;
  1238. default:
  1239. LOG_ERROR("Unsupported bank buswidth %d, can't do block memory writes", bank->bus_width);
  1240. return ERROR_FLASH_OPERATION_FAILED;
  1241. }
  1242. target_code = malloc(target_code_size);
  1243. cfi_fix_code_endian(target, target_code, src, target_code_size / 4);
  1244. /* allocate working area */
  1245. retval = target_alloc_working_area(target, target_code_size,
  1246. &cfi_info->write_algorithm);
  1247. if (retval != ERROR_OK)
  1248. {
  1249. free(target_code);
  1250. return retval;
  1251. }
  1252. /* write algorithm code to working area */
  1253. if ((retval = target_write_buffer(target, cfi_info->write_algorithm->address,
  1254. target_code_size, target_code)) != ERROR_OK)
  1255. {
  1256. free(target_code);
  1257. return retval;
  1258. }
  1259. free(target_code);
  1260. }
  1261. /* the following code still assumes target code is fixed 24*4 bytes */
  1262. while (target_alloc_working_area(target, buffer_size, &source) != ERROR_OK)
  1263. {
  1264. buffer_size /= 2;
  1265. if (buffer_size <= 256)
  1266. {
  1267. /* if we already allocated the writing code, but failed to get a buffer, free the algorithm */
  1268. if (cfi_info->write_algorithm)
  1269. target_free_working_area(target, cfi_info->write_algorithm);
  1270. LOG_WARNING("not enough working area available, can't do block memory writes");
  1271. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1272. }
  1273. };
  1274. init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
  1275. init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
  1276. init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT);
  1277. init_reg_param(&reg_params[3], "r3", 32, PARAM_OUT);
  1278. init_reg_param(&reg_params[4], "r4", 32, PARAM_OUT);
  1279. init_reg_param(&reg_params[5], "r5", 32, PARAM_IN);
  1280. init_reg_param(&reg_params[6], "r8", 32, PARAM_OUT);
  1281. init_reg_param(&reg_params[7], "r9", 32, PARAM_OUT);
  1282. init_reg_param(&reg_params[8], "r10", 32, PARAM_OUT);
  1283. init_reg_param(&reg_params[9], "r11", 32, PARAM_OUT);
  1284. while (count > 0)
  1285. {
  1286. uint32_t thisrun_count = (count > buffer_size) ? buffer_size : count;
  1287. retvaltemp = target_write_buffer(target, source->address, thisrun_count, buffer);
  1288. buf_set_u32(reg_params[0].value, 0, 32, source->address);
  1289. buf_set_u32(reg_params[1].value, 0, 32, address);
  1290. buf_set_u32(reg_params[2].value, 0, 32, thisrun_count / bank->bus_width);
  1291. buf_set_u32(reg_params[3].value, 0, 32, cfi_command_val(bank, 0xA0));
  1292. buf_set_u32(reg_params[4].value, 0, 32, cfi_command_val(bank, 0x80));
  1293. buf_set_u32(reg_params[6].value, 0, 32, flash_address(bank, 0, pri_ext->_unlock1));
  1294. buf_set_u32(reg_params[7].value, 0, 32, 0xaaaaaaaa);
  1295. buf_set_u32(reg_params[8].value, 0, 32, flash_address(bank, 0, pri_ext->_unlock2));
  1296. buf_set_u32(reg_params[9].value, 0, 32, 0x55555555);
  1297. retval = target_run_algorithm(target, 0, NULL, 10, reg_params,
  1298. cfi_info->write_algorithm->address,
  1299. cfi_info->write_algorithm->address + ((24 * 4) - 4),
  1300. 10000, &armv4_5_info);
  1301. status = buf_get_u32(reg_params[5].value, 0, 32);
  1302. if ((retval != ERROR_OK) || (retvaltemp != ERROR_OK) || status != 0x80)
  1303. {
  1304. LOG_DEBUG("status: 0x%" PRIx32 , status);
  1305. exit_code = ERROR_FLASH_OPERATION_FAILED;
  1306. break;
  1307. }
  1308. buffer += thisrun_count;
  1309. address += thisrun_count;
  1310. count -= thisrun_count;
  1311. }
  1312. target_free_working_area(target, source);
  1313. destroy_reg_param(&reg_params[0]);
  1314. destroy_reg_param(&reg_params[1]);
  1315. destroy_reg_param(&reg_params[2]);
  1316. destroy_reg_param(&reg_params[3]);
  1317. destroy_reg_param(&reg_params[4]);
  1318. destroy_reg_param(&reg_params[5]);
  1319. destroy_reg_param(&reg_params[6]);
  1320. destroy_reg_param(&reg_params[7]);
  1321. destroy_reg_param(&reg_params[8]);
  1322. destroy_reg_param(&reg_params[9]);
  1323. return exit_code;
  1324. }
  1325. static int cfi_intel_write_word(struct flash_bank_s *bank, uint8_t *word, uint32_t address)
  1326. {
  1327. int retval;
  1328. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  1329. target_t *target = bank->target;
  1330. uint8_t command[8];
  1331. cfi_intel_clear_status_register(bank);
  1332. cfi_command(bank, 0x40, command);
  1333. if ((retval = target_write_memory(target, address, bank->bus_width, 1, command)) != ERROR_OK)
  1334. {
  1335. return retval;
  1336. }
  1337. if ((retval = target_write_memory(target, address, bank->bus_width, 1, word)) != ERROR_OK)
  1338. {
  1339. return retval;
  1340. }
  1341. if (cfi_intel_wait_status_busy(bank, 1000 * (1 << cfi_info->word_write_timeout_max)) != 0x80)
  1342. {
  1343. cfi_command(bank, 0xff, command);
  1344. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  1345. {
  1346. return retval;
  1347. }
  1348. LOG_ERROR("couldn't write word at base 0x%" PRIx32 ", address %" PRIx32 , bank->base, address);
  1349. return ERROR_FLASH_OPERATION_FAILED;
  1350. }
  1351. return ERROR_OK;
  1352. }
  1353. static int cfi_intel_write_words(struct flash_bank_s *bank, uint8_t *word, uint32_t wordcount, uint32_t address)
  1354. {
  1355. int retval;
  1356. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  1357. target_t *target = bank->target;
  1358. uint8_t command[8];
  1359. /* Calculate buffer size and boundary mask */
  1360. uint32_t buffersize = (1UL << cfi_info->max_buf_write_size) * (bank->bus_width / bank->chip_width);
  1361. uint32_t buffermask = buffersize-1;
  1362. uint32_t bufferwsize;
  1363. /* Check for valid range */
  1364. if (address & buffermask)
  1365. {
  1366. LOG_ERROR("Write address at base 0x%" PRIx32 ", address %" PRIx32 " not aligned to 2^%d boundary",
  1367. bank->base, address, cfi_info->max_buf_write_size);
  1368. return ERROR_FLASH_OPERATION_FAILED;
  1369. }
  1370. switch (bank->chip_width)
  1371. {
  1372. case 4 : bufferwsize = buffersize / 4; break;
  1373. case 2 : bufferwsize = buffersize / 2; break;
  1374. case 1 : bufferwsize = buffersize; break;
  1375. default:
  1376. LOG_ERROR("Unsupported chip width %d", bank->chip_width);
  1377. return ERROR_FLASH_OPERATION_FAILED;
  1378. }
  1379. bufferwsize/=(bank->bus_width / bank->chip_width);
  1380. /* Check for valid size */
  1381. if (wordcount > bufferwsize)
  1382. {
  1383. LOG_ERROR("Number of data words %" PRId32 " exceeds available buffersize %" PRId32 , wordcount, buffersize);
  1384. return ERROR_FLASH_OPERATION_FAILED;
  1385. }
  1386. /* Write to flash buffer */
  1387. cfi_intel_clear_status_register(bank);
  1388. /* Initiate buffer operation _*/
  1389. cfi_command(bank, 0xE8, command);
  1390. if ((retval = target_write_memory(target, address, bank->bus_width, 1, command)) != ERROR_OK)
  1391. {
  1392. return retval;
  1393. }
  1394. if (cfi_intel_wait_status_busy(bank, 1000 * (1 << cfi_info->buf_write_timeout_max)) != 0x80)
  1395. {
  1396. cfi_command(bank, 0xff, command);
  1397. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  1398. {
  1399. return retval;
  1400. }
  1401. LOG_ERROR("couldn't start buffer write operation at base 0x%" PRIx32 ", address %" PRIx32 , bank->base, address);
  1402. return ERROR_FLASH_OPERATION_FAILED;
  1403. }
  1404. /* Write buffer wordcount-1 and data words */
  1405. cfi_command(bank, bufferwsize-1, command);
  1406. if ((retval = target_write_memory(target, address, bank->bus_width, 1, command)) != ERROR_OK)
  1407. {
  1408. return retval;
  1409. }
  1410. if ((retval = target_write_memory(target, address, bank->bus_width, bufferwsize, word)) != ERROR_OK)
  1411. {
  1412. return retval;
  1413. }
  1414. /* Commit write operation */
  1415. cfi_command(bank, 0xd0, command);
  1416. if ((retval = target_write_memory(target, address, bank->bus_width, 1, command)) != ERROR_OK)
  1417. {
  1418. return retval;
  1419. }
  1420. if (cfi_intel_wait_status_busy(bank, 1000 * (1 << cfi_info->buf_write_timeout_max)) != 0x80)
  1421. {
  1422. cfi_command(bank, 0xff, command);
  1423. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  1424. {
  1425. return retval;
  1426. }
  1427. LOG_ERROR("Buffer write at base 0x%" PRIx32 ", address %" PRIx32 " failed.", bank->base, address);
  1428. return ERROR_FLASH_OPERATION_FAILED;
  1429. }
  1430. return ERROR_OK;
  1431. }
  1432. static int cfi_spansion_write_word(struct flash_bank_s *bank, uint8_t *word, uint32_t address)
  1433. {
  1434. int retval;
  1435. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  1436. cfi_spansion_pri_ext_t *pri_ext = cfi_info->pri_ext;
  1437. target_t *target = bank->target;
  1438. uint8_t command[8];
  1439. cfi_command(bank, 0xaa, command);
  1440. if ((retval = target_write_memory(target, flash_address(bank, 0, pri_ext->_unlock1), bank->bus_width, 1, command)) != ERROR_OK)
  1441. {
  1442. return retval;
  1443. }
  1444. cfi_command(bank, 0x55, command);
  1445. if ((retval = target_write_memory(target, flash_address(bank, 0, pri_ext->_unlock2), bank->bus_width, 1, command)) != ERROR_OK)
  1446. {
  1447. return retval;
  1448. }
  1449. cfi_command(bank, 0xa0, command);
  1450. if ((retval = target_write_memory(target, flash_address(bank, 0, pri_ext->_unlock1), bank->bus_width, 1, command)) != ERROR_OK)
  1451. {
  1452. return retval;
  1453. }
  1454. if ((retval = target_write_memory(target, address, bank->bus_width, 1, word)) != ERROR_OK)
  1455. {
  1456. return retval;
  1457. }
  1458. if (cfi_spansion_wait_status_busy(bank, 1000 * (1 << cfi_info->word_write_timeout_max)) != ERROR_OK)
  1459. {
  1460. cfi_command(bank, 0xf0, command);
  1461. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  1462. {
  1463. return retval;
  1464. }
  1465. LOG_ERROR("couldn't write word at base 0x%" PRIx32 ", address %" PRIx32 , bank->base, address);
  1466. return ERROR_FLASH_OPERATION_FAILED;
  1467. }
  1468. return ERROR_OK;
  1469. }
  1470. static int cfi_spansion_write_words(struct flash_bank_s *bank, uint8_t *word, uint32_t wordcount, uint32_t address)
  1471. {
  1472. int retval;
  1473. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  1474. target_t *target = bank->target;
  1475. uint8_t command[8];
  1476. cfi_spansion_pri_ext_t *pri_ext = cfi_info->pri_ext;
  1477. /* Calculate buffer size and boundary mask */
  1478. uint32_t buffersize = (1UL << cfi_info->max_buf_write_size) * (bank->bus_width / bank->chip_width);
  1479. uint32_t buffermask = buffersize-1;
  1480. uint32_t bufferwsize;
  1481. /* Check for valid range */
  1482. if (address & buffermask)
  1483. {
  1484. 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);
  1485. return ERROR_FLASH_OPERATION_FAILED;
  1486. }
  1487. switch (bank->chip_width)
  1488. {
  1489. case 4 : bufferwsize = buffersize / 4; break;
  1490. case 2 : bufferwsize = buffersize / 2; break;
  1491. case 1 : bufferwsize = buffersize; break;
  1492. default:
  1493. LOG_ERROR("Unsupported chip width %d", bank->chip_width);
  1494. return ERROR_FLASH_OPERATION_FAILED;
  1495. }
  1496. bufferwsize/=(bank->bus_width / bank->chip_width);
  1497. /* Check for valid size */
  1498. if (wordcount > bufferwsize)
  1499. {
  1500. LOG_ERROR("Number of data words %" PRId32 " exceeds available buffersize %" PRId32, wordcount, buffersize);
  1501. return ERROR_FLASH_OPERATION_FAILED;
  1502. }
  1503. // Unlock
  1504. cfi_command(bank, 0xaa, command);
  1505. if ((retval = target_write_memory(target, flash_address(bank, 0, pri_ext->_unlock1), bank->bus_width, 1, command)) != ERROR_OK)
  1506. {
  1507. return retval;
  1508. }
  1509. cfi_command(bank, 0x55, command);
  1510. if ((retval = target_write_memory(target, flash_address(bank, 0, pri_ext->_unlock2), bank->bus_width, 1, command)) != ERROR_OK)
  1511. {
  1512. return retval;
  1513. }
  1514. // Buffer load command
  1515. cfi_command(bank, 0x25, command);
  1516. if ((retval = target_write_memory(target, address, bank->bus_width, 1, command)) != ERROR_OK)
  1517. {
  1518. return retval;
  1519. }
  1520. /* Write buffer wordcount-1 and data words */
  1521. cfi_command(bank, bufferwsize-1, command);
  1522. if ((retval = target_write_memory(target, address, bank->bus_width, 1, command)) != ERROR_OK)
  1523. {
  1524. return retval;
  1525. }
  1526. if ((retval = target_write_memory(target, address, bank->bus_width, bufferwsize, word)) != ERROR_OK)
  1527. {
  1528. return retval;
  1529. }
  1530. /* Commit write operation */
  1531. cfi_command(bank, 0x29, command);
  1532. if ((retval = target_write_memory(target, address, bank->bus_width, 1, command)) != ERROR_OK)
  1533. {
  1534. return retval;
  1535. }
  1536. if (cfi_spansion_wait_status_busy(bank, 1000 * (1 << cfi_info->word_write_timeout_max)) != ERROR_OK)
  1537. {
  1538. cfi_command(bank, 0xf0, command);
  1539. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  1540. {
  1541. return retval;
  1542. }
  1543. LOG_ERROR("couldn't write block at base 0x%" PRIx32 ", address %" PRIx32 ", size %" PRIx32 , bank->base, address, bufferwsize);
  1544. return ERROR_FLASH_OPERATION_FAILED;
  1545. }
  1546. return ERROR_OK;
  1547. }
  1548. static int cfi_write_word(struct flash_bank_s *bank, uint8_t *word, uint32_t address)
  1549. {
  1550. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  1551. switch (cfi_info->pri_id)
  1552. {
  1553. case 1:
  1554. case 3:
  1555. return cfi_intel_write_word(bank, word, address);
  1556. break;
  1557. case 2:
  1558. return cfi_spansion_write_word(bank, word, address);
  1559. break;
  1560. default:
  1561. LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
  1562. break;
  1563. }
  1564. return ERROR_FLASH_OPERATION_FAILED;
  1565. }
  1566. static int cfi_write_words(struct flash_bank_s *bank, uint8_t *word, uint32_t wordcount, uint32_t address)
  1567. {
  1568. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  1569. switch (cfi_info->pri_id)
  1570. {
  1571. case 1:
  1572. case 3:
  1573. return cfi_intel_write_words(bank, word, wordcount, address);
  1574. break;
  1575. case 2:
  1576. return cfi_spansion_write_words(bank, word, wordcount, address);
  1577. break;
  1578. default:
  1579. LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
  1580. break;
  1581. }
  1582. return ERROR_FLASH_OPERATION_FAILED;
  1583. }
  1584. int cfi_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
  1585. {
  1586. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  1587. target_t *target = bank->target;
  1588. uint32_t address = bank->base + offset; /* address of first byte to be programmed */
  1589. uint32_t write_p, copy_p;
  1590. int align; /* number of unaligned bytes */
  1591. int blk_count; /* number of bus_width bytes for block copy */
  1592. uint8_t current_word[CFI_MAX_BUS_WIDTH * 4]; /* word (bus_width size) currently being programmed */
  1593. int i;
  1594. int retval;
  1595. if (bank->target->state != TARGET_HALTED)
  1596. {
  1597. LOG_ERROR("Target not halted");
  1598. return ERROR_TARGET_NOT_HALTED;
  1599. }
  1600. if (offset + count > bank->size)
  1601. return ERROR_FLASH_DST_OUT_OF_BANK;
  1602. if (cfi_info->qry[0] != 'Q')
  1603. return ERROR_FLASH_BANK_NOT_PROBED;
  1604. /* start at the first byte of the first word (bus_width size) */
  1605. write_p = address & ~(bank->bus_width - 1);
  1606. if ((align = address - write_p) != 0)
  1607. {
  1608. LOG_INFO("Fixup %d unaligned head bytes", align);
  1609. for (i = 0; i < bank->bus_width; i++)
  1610. current_word[i] = 0;
  1611. copy_p = write_p;
  1612. /* copy bytes before the first write address */
  1613. for (i = 0; i < align; ++i, ++copy_p)
  1614. {
  1615. uint8_t byte;
  1616. if ((retval = target_read_memory(target, copy_p, 1, 1, &byte)) != ERROR_OK)
  1617. {
  1618. return retval;
  1619. }
  1620. cfi_add_byte(bank, current_word, byte);
  1621. }
  1622. /* add bytes from the buffer */
  1623. for (; (i < bank->bus_width) && (count > 0); i++)
  1624. {
  1625. cfi_add_byte(bank, current_word, *buffer++);
  1626. count--;
  1627. copy_p++;
  1628. }
  1629. /* if the buffer is already finished, copy bytes after the last write address */
  1630. for (; (count == 0) && (i < bank->bus_width); ++i, ++copy_p)
  1631. {
  1632. uint8_t byte;
  1633. if ((retval = target_read_memory(target, copy_p, 1, 1, &byte)) != ERROR_OK)
  1634. {
  1635. return retval;
  1636. }
  1637. cfi_add_byte(bank, current_word, byte);
  1638. }
  1639. retval = cfi_write_word(bank, current_word, write_p);
  1640. if (retval != ERROR_OK)
  1641. return retval;
  1642. write_p = copy_p;
  1643. }
  1644. /* handle blocks of bus_size aligned bytes */
  1645. blk_count = count & ~(bank->bus_width - 1); /* round down, leave tail bytes */
  1646. switch (cfi_info->pri_id)
  1647. {
  1648. /* try block writes (fails without working area) */
  1649. case 1:
  1650. case 3:
  1651. retval = cfi_intel_write_block(bank, buffer, write_p, blk_count);
  1652. break;
  1653. case 2:
  1654. retval = cfi_spansion_write_block(bank, buffer, write_p, blk_count);
  1655. break;
  1656. default:
  1657. LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
  1658. retval = ERROR_FLASH_OPERATION_FAILED;
  1659. break;
  1660. }
  1661. if (retval == ERROR_OK)
  1662. {
  1663. /* Increment pointers and decrease count on succesful block write */
  1664. buffer += blk_count;
  1665. write_p += blk_count;
  1666. count -= blk_count;
  1667. }
  1668. else
  1669. {
  1670. if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
  1671. {
  1672. //adjust buffersize for chip width
  1673. uint32_t buffersize = (1UL << cfi_info->max_buf_write_size) * (bank->bus_width / bank->chip_width);
  1674. uint32_t buffermask = buffersize-1;
  1675. uint32_t bufferwsize;
  1676. switch (bank->chip_width)
  1677. {
  1678. case 4 : bufferwsize = buffersize / 4; break;
  1679. case 2 : bufferwsize = buffersize / 2; break;
  1680. case 1 : bufferwsize = buffersize; break;
  1681. default:
  1682. LOG_ERROR("Unsupported chip width %d", bank->chip_width);
  1683. return ERROR_FLASH_OPERATION_FAILED;
  1684. }
  1685. bufferwsize/=(bank->bus_width / bank->chip_width);
  1686. /* fall back to memory writes */
  1687. while (count >= (uint32_t)bank->bus_width)
  1688. {
  1689. int fallback;
  1690. if ((write_p & 0xff) == 0)
  1691. {
  1692. LOG_INFO("Programming at %08" PRIx32 ", count %08" PRIx32 " bytes remaining", write_p, count);
  1693. }
  1694. fallback = 1;
  1695. if ((bufferwsize > 0) && (count >= buffersize) && !(write_p & buffermask))
  1696. {
  1697. retval = cfi_write_words(bank, buffer, bufferwsize, write_p);
  1698. if (retval == ERROR_OK)
  1699. {
  1700. buffer += buffersize;
  1701. write_p += buffersize;
  1702. count -= buffersize;
  1703. fallback = 0;
  1704. }
  1705. }
  1706. /* try the slow way? */
  1707. if (fallback)
  1708. {
  1709. for (i = 0; i < bank->bus_width; i++)
  1710. current_word[i] = 0;
  1711. for (i = 0; i < bank->bus_width; i++)
  1712. {
  1713. cfi_add_byte(bank, current_word, *buffer++);
  1714. }
  1715. retval = cfi_write_word(bank, current_word, write_p);
  1716. if (retval != ERROR_OK)
  1717. return retval;
  1718. write_p += bank->bus_width;
  1719. count -= bank->bus_width;
  1720. }
  1721. }
  1722. }
  1723. else
  1724. return retval;
  1725. }
  1726. /* return to read array mode, so we can read from flash again for padding */
  1727. cfi_command(bank, 0xf0, current_word);
  1728. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, current_word)) != ERROR_OK)
  1729. {
  1730. return retval;
  1731. }
  1732. cfi_command(bank, 0xff, current_word);
  1733. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, current_word)) != ERROR_OK)
  1734. {
  1735. return retval;
  1736. }
  1737. /* handle unaligned tail bytes */
  1738. if (count > 0)
  1739. {
  1740. LOG_INFO("Fixup %" PRId32 " unaligned tail bytes", count);
  1741. copy_p = write_p;
  1742. for (i = 0; i < bank->bus_width; i++)
  1743. current_word[i] = 0;
  1744. for (i = 0; (i < bank->bus_width) && (count > 0); ++i, ++copy_p)
  1745. {
  1746. cfi_add_byte(bank, current_word, *buffer++);
  1747. count--;
  1748. }
  1749. for (; i < bank->bus_width; ++i, ++copy_p)
  1750. {
  1751. uint8_t byte;
  1752. if ((retval = target_read_memory(target, copy_p, 1, 1, &byte)) != ERROR_OK)
  1753. {
  1754. return retval;
  1755. }
  1756. cfi_add_byte(bank, current_word, byte);
  1757. }
  1758. retval = cfi_write_word(bank, current_word, write_p);
  1759. if (retval != ERROR_OK)
  1760. return retval;
  1761. }
  1762. /* return to read array mode */
  1763. cfi_command(bank, 0xf0, current_word);
  1764. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, current_word)) != ERROR_OK)
  1765. {
  1766. return retval;
  1767. }
  1768. cfi_command(bank, 0xff, current_word);
  1769. return target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, current_word);
  1770. }
  1771. static void cfi_fixup_atmel_reversed_erase_regions(flash_bank_t *bank, void *param)
  1772. {
  1773. (void) param;
  1774. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  1775. cfi_spansion_pri_ext_t *pri_ext = cfi_info->pri_ext;
  1776. pri_ext->_reversed_geometry = 1;
  1777. }
  1778. static void cfi_fixup_0002_erase_regions(flash_bank_t *bank, void *param)
  1779. {
  1780. int i;
  1781. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  1782. cfi_spansion_pri_ext_t *pri_ext = cfi_info->pri_ext;
  1783. (void) param;
  1784. if ((pri_ext->_reversed_geometry) || (pri_ext->TopBottom == 3))
  1785. {
  1786. LOG_DEBUG("swapping reversed erase region information on cmdset 0002 device");
  1787. for (i = 0; i < cfi_info->num_erase_regions / 2; i++)
  1788. {
  1789. int j = (cfi_info->num_erase_regions - 1) - i;
  1790. uint32_t swap;
  1791. swap = cfi_info->erase_region_info[i];
  1792. cfi_info->erase_region_info[i] = cfi_info->erase_region_info[j];
  1793. cfi_info->erase_region_info[j] = swap;
  1794. }
  1795. }
  1796. }
  1797. static void cfi_fixup_0002_unlock_addresses(flash_bank_t *bank, void *param)
  1798. {
  1799. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  1800. cfi_spansion_pri_ext_t *pri_ext = cfi_info->pri_ext;
  1801. cfi_unlock_addresses_t *unlock_addresses = param;
  1802. pri_ext->_unlock1 = unlock_addresses->unlock1;
  1803. pri_ext->_unlock2 = unlock_addresses->unlock2;
  1804. }
  1805. static int cfi_probe(struct flash_bank_s *bank)
  1806. {
  1807. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  1808. target_t *target = bank->target;
  1809. uint8_t command[8];
  1810. int num_sectors = 0;
  1811. int i;
  1812. int sector = 0;
  1813. uint32_t unlock1 = 0x555;
  1814. uint32_t unlock2 = 0x2aa;
  1815. int retval;
  1816. if (bank->target->state != TARGET_HALTED)
  1817. {
  1818. LOG_ERROR("Target not halted");
  1819. return ERROR_TARGET_NOT_HALTED;
  1820. }
  1821. cfi_info->probed = 0;
  1822. /* JEDEC standard JESD21C uses 0x5555 and 0x2aaa as unlock addresses,
  1823. * while CFI compatible AMD/Spansion flashes use 0x555 and 0x2aa
  1824. */
  1825. if (cfi_info->jedec_probe)
  1826. {
  1827. unlock1 = 0x5555;
  1828. unlock2 = 0x2aaa;
  1829. }
  1830. /* switch to read identifier codes mode ("AUTOSELECT") */
  1831. cfi_command(bank, 0xaa, command);
  1832. if ((retval = target_write_memory(target, flash_address(bank, 0, unlock1), bank->bus_width, 1, command)) != ERROR_OK)
  1833. {
  1834. return retval;
  1835. }
  1836. cfi_command(bank, 0x55, command);
  1837. if ((retval = target_write_memory(target, flash_address(bank, 0, unlock2), bank->bus_width, 1, command)) != ERROR_OK)
  1838. {
  1839. return retval;
  1840. }
  1841. cfi_command(bank, 0x90, command);
  1842. if ((retval = target_write_memory(target, flash_address(bank, 0, unlock1), bank->bus_width, 1, command)) != ERROR_OK)
  1843. {
  1844. return retval;
  1845. }
  1846. if (bank->chip_width == 1)
  1847. {
  1848. uint8_t manufacturer, device_id;
  1849. if ((retval = target_read_u8(target, flash_address(bank, 0, 0x00), &manufacturer)) != ERROR_OK)
  1850. {
  1851. return retval;
  1852. }
  1853. if ((retval = target_read_u8(target, flash_address(bank, 0, 0x01), &device_id)) != ERROR_OK)
  1854. {
  1855. return retval;
  1856. }
  1857. cfi_info->manufacturer = manufacturer;
  1858. cfi_info->device_id = device_id;
  1859. }
  1860. else if (bank->chip_width == 2)
  1861. {
  1862. if ((retval = target_read_u16(target, flash_address(bank, 0, 0x00), &cfi_info->manufacturer)) != ERROR_OK)
  1863. {
  1864. return retval;
  1865. }
  1866. if ((retval = target_read_u16(target, flash_address(bank, 0, 0x02), &cfi_info->device_id)) != ERROR_OK)
  1867. {
  1868. return retval;
  1869. }
  1870. }
  1871. LOG_INFO("Flash Manufacturer/Device: 0x%04x 0x%04x", cfi_info->manufacturer, cfi_info->device_id);
  1872. /* switch back to read array mode */
  1873. cfi_command(bank, 0xf0, command);
  1874. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x00), bank->bus_width, 1, command)) != ERROR_OK)
  1875. {
  1876. return retval;
  1877. }
  1878. cfi_command(bank, 0xff, command);
  1879. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x00), bank->bus_width, 1, command)) != ERROR_OK)
  1880. {
  1881. return retval;
  1882. }
  1883. /* check device/manufacturer ID for known non-CFI flashes. */
  1884. cfi_fixup_non_cfi(bank);
  1885. /* query only if this is a CFI compatible flash,
  1886. * otherwise the relevant info has already been filled in
  1887. */
  1888. if (cfi_info->not_cfi == 0)
  1889. {
  1890. /* enter CFI query mode
  1891. * according to JEDEC Standard No. 68.01,
  1892. * a single bus sequence with address = 0x55, data = 0x98 should put
  1893. * the device into CFI query mode.
  1894. *
  1895. * SST flashes clearly violate this, and we will consider them incompatbile for now
  1896. */
  1897. cfi_command(bank, 0x98, command);
  1898. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x55), bank->bus_width, 1, command)) != ERROR_OK)
  1899. {
  1900. return retval;
  1901. }
  1902. cfi_info->qry[0] = cfi_query_u8(bank, 0, 0x10);
  1903. cfi_info->qry[1] = cfi_query_u8(bank, 0, 0x11);
  1904. cfi_info->qry[2] = cfi_query_u8(bank, 0, 0x12);
  1905. 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]);
  1906. if ((cfi_info->qry[0] != 'Q') || (cfi_info->qry[1] != 'R') || (cfi_info->qry[2] != 'Y'))
  1907. {
  1908. cfi_command(bank, 0xf0, command);
  1909. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  1910. {
  1911. return retval;
  1912. }
  1913. cfi_command(bank, 0xff, command);
  1914. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  1915. {
  1916. return retval;
  1917. }
  1918. LOG_ERROR("Could not probe bank: no QRY");
  1919. return ERROR_FLASH_BANK_INVALID;
  1920. }
  1921. cfi_info->pri_id = cfi_query_u16(bank, 0, 0x13);
  1922. cfi_info->pri_addr = cfi_query_u16(bank, 0, 0x15);
  1923. cfi_info->alt_id = cfi_query_u16(bank, 0, 0x17);
  1924. cfi_info->alt_addr = cfi_query_u16(bank, 0, 0x19);
  1925. 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);
  1926. cfi_info->vcc_min = cfi_query_u8(bank, 0, 0x1b);
  1927. cfi_info->vcc_max = cfi_query_u8(bank, 0, 0x1c);
  1928. cfi_info->vpp_min = cfi_query_u8(bank, 0, 0x1d);
  1929. cfi_info->vpp_max = cfi_query_u8(bank, 0, 0x1e);
  1930. cfi_info->word_write_timeout_typ = cfi_query_u8(bank, 0, 0x1f);
  1931. cfi_info->buf_write_timeout_typ = cfi_query_u8(bank, 0, 0x20);
  1932. cfi_info->block_erase_timeout_typ = cfi_query_u8(bank, 0, 0x21);
  1933. cfi_info->chip_erase_timeout_typ = cfi_query_u8(bank, 0, 0x22);
  1934. cfi_info->word_write_timeout_max = cfi_query_u8(bank, 0, 0x23);
  1935. cfi_info->buf_write_timeout_max = cfi_query_u8(bank, 0, 0x24);
  1936. cfi_info->block_erase_timeout_max = cfi_query_u8(bank, 0, 0x25);
  1937. cfi_info->chip_erase_timeout_max = cfi_query_u8(bank, 0, 0x26);
  1938. LOG_DEBUG("Vcc min: %1.1x.%1.1x, Vcc max: %1.1x.%1.1x, Vpp min: %1.1x.%1.1x, Vpp max: %1.1x.%1.1x",
  1939. (cfi_info->vcc_min & 0xf0) >> 4, cfi_info->vcc_min & 0x0f,
  1940. (cfi_info->vcc_max & 0xf0) >> 4, cfi_info->vcc_max & 0x0f,
  1941. (cfi_info->vpp_min & 0xf0) >> 4, cfi_info->vpp_min & 0x0f,
  1942. (cfi_info->vpp_max & 0xf0) >> 4, cfi_info->vpp_max & 0x0f);
  1943. 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,
  1944. 1 << cfi_info->block_erase_timeout_typ, 1 << cfi_info->chip_erase_timeout_typ);
  1945. 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),
  1946. (1 << cfi_info->buf_write_timeout_max) * (1 << cfi_info->buf_write_timeout_typ),
  1947. (1 << cfi_info->block_erase_timeout_max) * (1 << cfi_info->block_erase_timeout_typ),
  1948. (1 << cfi_info->chip_erase_timeout_max) * (1 << cfi_info->chip_erase_timeout_typ));
  1949. cfi_info->dev_size = 1 << cfi_query_u8(bank, 0, 0x27);
  1950. cfi_info->interface_desc = cfi_query_u16(bank, 0, 0x28);
  1951. cfi_info->max_buf_write_size = cfi_query_u16(bank, 0, 0x2a);
  1952. cfi_info->num_erase_regions = cfi_query_u8(bank, 0, 0x2c);
  1953. 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));
  1954. if (cfi_info->num_erase_regions)
  1955. {
  1956. cfi_info->erase_region_info = malloc(4 * cfi_info->num_erase_regions);
  1957. for (i = 0; i < cfi_info->num_erase_regions; i++)
  1958. {
  1959. cfi_info->erase_region_info[i] = cfi_query_u32(bank, 0, 0x2d + (4 * i));
  1960. LOG_DEBUG("erase region[%i]: %" PRIu32 " blocks of size 0x%" PRIx32 "",
  1961. i,
  1962. (cfi_info->erase_region_info[i] & 0xffff) + 1,
  1963. (cfi_info->erase_region_info[i] >> 16) * 256);
  1964. }
  1965. }
  1966. else
  1967. {
  1968. cfi_info->erase_region_info = NULL;
  1969. }
  1970. /* We need to read the primary algorithm extended query table before calculating
  1971. * the sector layout to be able to apply fixups
  1972. */
  1973. switch (cfi_info->pri_id)
  1974. {
  1975. /* Intel command set (standard and extended) */
  1976. case 0x0001:
  1977. case 0x0003:
  1978. cfi_read_intel_pri_ext(bank);
  1979. break;
  1980. /* AMD/Spansion, Atmel, ... command set */
  1981. case 0x0002:
  1982. cfi_info->status_poll_mask = CFI_STATUS_POLL_MASK_DQ5_DQ6_DQ7; /* default for all CFI flashs */
  1983. cfi_read_0002_pri_ext(bank);
  1984. break;
  1985. default:
  1986. LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
  1987. break;
  1988. }
  1989. /* return to read array mode
  1990. * we use both reset commands, as some Intel flashes fail to recognize the 0xF0 command
  1991. */
  1992. cfi_command(bank, 0xf0, command);
  1993. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  1994. {
  1995. return retval;
  1996. }
  1997. cfi_command(bank, 0xff, command);
  1998. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command)) != ERROR_OK)
  1999. {
  2000. return retval;
  2001. }
  2002. } /* end CFI case */
  2003. /* apply fixups depending on the primary command set */
  2004. switch (cfi_info->pri_id)
  2005. {
  2006. /* Intel command set (standard and extended) */
  2007. case 0x0001:
  2008. case 0x0003:
  2009. cfi_fixup(bank, cfi_0001_fixups);
  2010. break;
  2011. /* AMD/Spansion, Atmel, ... command set */
  2012. case 0x0002:
  2013. cfi_fixup(bank, cfi_0002_fixups);
  2014. break;
  2015. default:
  2016. LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
  2017. break;
  2018. }
  2019. if ((cfi_info->dev_size * bank->bus_width / bank->chip_width) != bank->size)
  2020. {
  2021. LOG_WARNING("configuration specifies 0x%" PRIx32 " size, but a 0x%" PRIx32 " size flash was found", bank->size, cfi_info->dev_size);
  2022. }
  2023. if (cfi_info->num_erase_regions == 0)
  2024. {
  2025. /* a device might have only one erase block, spanning the whole device */
  2026. bank->num_sectors = 1;
  2027. bank->sectors = malloc(sizeof(flash_sector_t));
  2028. bank->sectors[sector].offset = 0x0;
  2029. bank->sectors[sector].size = bank->size;
  2030. bank->sectors[sector].is_erased = -1;
  2031. bank->sectors[sector].is_protected = -1;
  2032. }
  2033. else
  2034. {
  2035. uint32_t offset = 0;
  2036. for (i = 0; i < cfi_info->num_erase_regions; i++)
  2037. {
  2038. num_sectors += (cfi_info->erase_region_info[i] & 0xffff) + 1;
  2039. }
  2040. bank->num_sectors = num_sectors;
  2041. bank->sectors = malloc(sizeof(flash_sector_t) * num_sectors);
  2042. for (i = 0; i < cfi_info->num_erase_regions; i++)
  2043. {
  2044. uint32_t j;
  2045. for (j = 0; j < (cfi_info->erase_region_info[i] & 0xffff) + 1; j++)
  2046. {
  2047. bank->sectors[sector].offset = offset;
  2048. bank->sectors[sector].size = ((cfi_info->erase_region_info[i] >> 16) * 256) * bank->bus_width / bank->chip_width;
  2049. offset += bank->sectors[sector].size;
  2050. bank->sectors[sector].is_erased = -1;
  2051. bank->sectors[sector].is_protected = -1;
  2052. sector++;
  2053. }
  2054. }
  2055. if (offset != cfi_info->dev_size)
  2056. {
  2057. LOG_WARNING("CFI size is 0x%" PRIx32 ", but total sector size is 0x%" PRIx32 "", cfi_info->dev_size, offset);
  2058. }
  2059. }
  2060. cfi_info->probed = 1;
  2061. return ERROR_OK;
  2062. }
  2063. static int cfi_auto_probe(struct flash_bank_s *bank)
  2064. {
  2065. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  2066. if (cfi_info->probed)
  2067. return ERROR_OK;
  2068. return cfi_probe(bank);
  2069. }
  2070. static int cfi_intel_protect_check(struct flash_bank_s *bank)
  2071. {
  2072. int retval;
  2073. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  2074. cfi_intel_pri_ext_t *pri_ext = cfi_info->pri_ext;
  2075. target_t *target = bank->target;
  2076. uint8_t command[CFI_MAX_BUS_WIDTH];
  2077. int i;
  2078. /* check if block lock bits are supported on this device */
  2079. if (!(pri_ext->blk_status_reg_mask & 0x1))
  2080. return ERROR_FLASH_OPERATION_FAILED;
  2081. cfi_command(bank, 0x90, command);
  2082. if ((retval = target_write_memory(target, flash_address(bank, 0, 0x55), bank->bus_width, 1, command)) != ERROR_OK)
  2083. {
  2084. return retval;
  2085. }
  2086. for (i = 0; i < bank->num_sectors; i++)
  2087. {
  2088. uint8_t block_status = cfi_get_u8(bank, i, 0x2);
  2089. if (block_status & 1)
  2090. bank->sectors[i].is_protected = 1;
  2091. else
  2092. bank->sectors[i].is_protected = 0;
  2093. }
  2094. cfi_command(bank, 0xff, command);
  2095. return target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
  2096. }
  2097. static int cfi_spansion_protect_check(struct flash_bank_s *bank)
  2098. {
  2099. int retval;
  2100. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  2101. cfi_spansion_pri_ext_t *pri_ext = cfi_info->pri_ext;
  2102. target_t *target = bank->target;
  2103. uint8_t command[8];
  2104. int i;
  2105. cfi_command(bank, 0xaa, command);
  2106. if ((retval = target_write_memory(target, flash_address(bank, 0, pri_ext->_unlock1), bank->bus_width, 1, command)) != ERROR_OK)
  2107. {
  2108. return retval;
  2109. }
  2110. cfi_command(bank, 0x55, command);
  2111. if ((retval = target_write_memory(target, flash_address(bank, 0, pri_ext->_unlock2), bank->bus_width, 1, command)) != ERROR_OK)
  2112. {
  2113. return retval;
  2114. }
  2115. cfi_command(bank, 0x90, command);
  2116. if ((retval = target_write_memory(target, flash_address(bank, 0, pri_ext->_unlock1), bank->bus_width, 1, command)) != ERROR_OK)
  2117. {
  2118. return retval;
  2119. }
  2120. for (i = 0; i < bank->num_sectors; i++)
  2121. {
  2122. uint8_t block_status = cfi_get_u8(bank, i, 0x2);
  2123. if (block_status & 1)
  2124. bank->sectors[i].is_protected = 1;
  2125. else
  2126. bank->sectors[i].is_protected = 0;
  2127. }
  2128. cfi_command(bank, 0xf0, command);
  2129. return target_write_memory(target, flash_address(bank, 0, 0x0), bank->bus_width, 1, command);
  2130. }
  2131. static int cfi_protect_check(struct flash_bank_s *bank)
  2132. {
  2133. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  2134. if (bank->target->state != TARGET_HALTED)
  2135. {
  2136. LOG_ERROR("Target not halted");
  2137. return ERROR_TARGET_NOT_HALTED;
  2138. }
  2139. if (cfi_info->qry[0] != 'Q')
  2140. return ERROR_FLASH_BANK_NOT_PROBED;
  2141. switch (cfi_info->pri_id)
  2142. {
  2143. case 1:
  2144. case 3:
  2145. return cfi_intel_protect_check(bank);
  2146. break;
  2147. case 2:
  2148. return cfi_spansion_protect_check(bank);
  2149. break;
  2150. default:
  2151. LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
  2152. break;
  2153. }
  2154. return ERROR_OK;
  2155. }
  2156. static int cfi_info(struct flash_bank_s *bank, char *buf, int buf_size)
  2157. {
  2158. int printed;
  2159. cfi_flash_bank_t *cfi_info = bank->driver_priv;
  2160. if (cfi_info->qry[0] == (char)-1)
  2161. {
  2162. printed = snprintf(buf, buf_size, "\ncfi flash bank not probed yet\n");
  2163. return ERROR_OK;
  2164. }
  2165. if (cfi_info->not_cfi == 0)
  2166. printed = snprintf(buf, buf_size, "\ncfi information:\n");
  2167. else
  2168. printed = snprintf(buf, buf_size, "\nnon-cfi flash:\n");
  2169. buf += printed;
  2170. buf_size -= printed;
  2171. printed = snprintf(buf, buf_size, "\nmfr: 0x%4.4x, id:0x%4.4x\n",
  2172. cfi_info->manufacturer, cfi_info->device_id);
  2173. buf += printed;
  2174. buf_size -= printed;
  2175. if (cfi_info->not_cfi == 0)
  2176. {
  2177. 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);
  2178. buf += printed;
  2179. buf_size -= printed;
  2180. printed = snprintf(buf, buf_size, "Vcc min: %1.1x.%1.1x, Vcc max: %1.1x.%1.1x, Vpp min: %1.1x.%1.1x, Vpp max: %1.1x.%1.1x\n",
  2181. (cfi_info->vcc_min & 0xf0) >> 4, cfi_info->vcc_min & 0x0f,
  2182. (cfi_info->vcc_max & 0xf0) >> 4, cfi_info->vcc_max & 0x0f,
  2183. (cfi_info->vpp_min & 0xf0) >> 4, cfi_info->vpp_min & 0x0f,
  2184. (cfi_info->vpp_max & 0xf0) >> 4, cfi_info->vpp_max & 0x0f);
  2185. buf += printed;
  2186. buf_size -= printed;
  2187. 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",
  2188. 1 << cfi_info->word_write_timeout_typ,
  2189. 1 << cfi_info->buf_write_timeout_typ,
  2190. 1 << cfi_info->block_erase_timeout_typ,
  2191. 1 << cfi_info->chip_erase_timeout_typ);
  2192. buf += printed;
  2193. buf_size -= printed;
  2194. 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",
  2195. (1 << cfi_info->word_write_timeout_max) * (1 << cfi_info->word_write_timeout_typ),
  2196. (1 << cfi_info->buf_write_timeout_max) * (1 << cfi_info->buf_write_timeout_typ),
  2197. (1 << cfi_info->block_erase_timeout_max) * (1 << cfi_info->block_erase_timeout_typ),
  2198. (1 << cfi_info->chip_erase_timeout_max) * (1 << cfi_info->chip_erase_timeout_typ));
  2199. buf += printed;
  2200. buf_size -= printed;
  2201. printed = snprintf(buf, buf_size, "size: 0x%" PRIx32 ", interface desc: %i, max buffer write size: %x\n",
  2202. cfi_info->dev_size,
  2203. cfi_info->interface_desc,
  2204. 1 << cfi_info->max_buf_write_size);
  2205. buf += printed;
  2206. buf_size -= printed;
  2207. switch (cfi_info->pri_id)
  2208. {
  2209. case 1:
  2210. case 3:
  2211. cfi_intel_info(bank, buf, buf_size);
  2212. break;
  2213. case 2:
  2214. cfi_spansion_info(bank, buf, buf_size);
  2215. break;
  2216. default:
  2217. LOG_ERROR("cfi primary command set %i unsupported", cfi_info->pri_id);
  2218. break;
  2219. }
  2220. }
  2221. return ERROR_OK;
  2222. }