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.
 
 
 
 
 
 

1605 lines
48 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2009 by *
  3. * Rolf Meeser <rolfm_9dq@yahoo.de> *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "imp.h"
  24. #include <helper/binarybuffer.h>
  25. #include <target/algorithm.h>
  26. #include <target/arm.h>
  27. #include <target/image.h>
  28. /* 1024 bytes */
  29. #define KiB 1024
  30. /* Some flash constants */
  31. #define FLASH_PAGE_SIZE 512 /* bytes */
  32. #define FLASH_ERASE_TIME 100000 /* microseconds */
  33. #define FLASH_PROGRAM_TIME 1000 /* microseconds */
  34. /* Chip ID / Feature Registers */
  35. #define CHIPID 0xE0000000 /* Chip ID */
  36. #define FEAT0 0xE0000100 /* Chip feature 0 */
  37. #define FEAT1 0xE0000104 /* Chip feature 1 */
  38. #define FEAT2 0xE0000108 /* Chip feature 2 (contains flash size indicator) */
  39. #define FEAT3 0xE000010C /* Chip feature 3 */
  40. #define EXPECTED_CHIPID 0x209CE02B /* Chip ID of all LPC2900 devices */
  41. /* Flash/EEPROM Control Registers */
  42. #define FCTR 0x20200000 /* Flash control */
  43. #define FPTR 0x20200008 /* Flash program-time */
  44. #define FTCTR 0x2020000C /* Flash test control */
  45. #define FBWST 0x20200010 /* Flash bridge wait-state */
  46. #define FCRA 0x2020001C /* Flash clock divider */
  47. #define FMSSTART 0x20200020 /* Flash Built-In Selft Test start address */
  48. #define FMSSTOP 0x20200024 /* Flash Built-In Selft Test stop address */
  49. #define FMS16 0x20200028 /* Flash 16-bit signature */
  50. #define FMSW0 0x2020002C /* Flash 128-bit signature Word 0 */
  51. #define FMSW1 0x20200030 /* Flash 128-bit signature Word 1 */
  52. #define FMSW2 0x20200034 /* Flash 128-bit signature Word 2 */
  53. #define FMSW3 0x20200038 /* Flash 128-bit signature Word 3 */
  54. #define EECMD 0x20200080 /* EEPROM command */
  55. #define EEADDR 0x20200084 /* EEPROM address */
  56. #define EEWDATA 0x20200088 /* EEPROM write data */
  57. #define EERDATA 0x2020008C /* EEPROM read data */
  58. #define EEWSTATE 0x20200090 /* EEPROM wait state */
  59. #define EECLKDIV 0x20200094 /* EEPROM clock divider */
  60. #define EEPWRDWN 0x20200098 /* EEPROM power-down/start */
  61. #define EEMSSTART 0x2020009C /* EEPROM BIST start address */
  62. #define EEMSSTOP 0x202000A0 /* EEPROM BIST stop address */
  63. #define EEMSSIG 0x202000A4 /* EEPROM 24-bit BIST signature */
  64. #define INT_CLR_ENABLE 0x20200FD8 /* Flash/EEPROM interrupt clear enable */
  65. #define INT_SET_ENABLE 0x20200FDC /* Flash/EEPROM interrupt set enable */
  66. #define INT_STATUS 0x20200FE0 /* Flash/EEPROM interrupt status */
  67. #define INT_ENABLE 0x20200FE4 /* Flash/EEPROM interrupt enable */
  68. #define INT_CLR_STATUS 0x20200FE8 /* Flash/EEPROM interrupt clear status */
  69. #define INT_SET_STATUS 0x20200FEC /* Flash/EEPROM interrupt set status */
  70. /* Interrupt sources */
  71. #define INTSRC_END_OF_PROG (1 << 28)
  72. #define INTSRC_END_OF_BIST (1 << 27)
  73. #define INTSRC_END_OF_RDWR (1 << 26)
  74. #define INTSRC_END_OF_MISR (1 << 2)
  75. #define INTSRC_END_OF_BURN (1 << 1)
  76. #define INTSRC_END_OF_ERASE (1 << 0)
  77. /* FCTR bits */
  78. #define FCTR_FS_LOADREQ (1 << 15)
  79. #define FCTR_FS_CACHECLR (1 << 14)
  80. #define FCTR_FS_CACHEBYP (1 << 13)
  81. #define FCTR_FS_PROGREQ (1 << 12)
  82. #define FCTR_FS_RLS (1 << 11)
  83. #define FCTR_FS_PDL (1 << 10)
  84. #define FCTR_FS_PD (1 << 9)
  85. #define FCTR_FS_WPB (1 << 7)
  86. #define FCTR_FS_ISS (1 << 6)
  87. #define FCTR_FS_RLD (1 << 5)
  88. #define FCTR_FS_DCR (1 << 4)
  89. #define FCTR_FS_WEB (1 << 2)
  90. #define FCTR_FS_WRE (1 << 1)
  91. #define FCTR_FS_CS (1 << 0)
  92. /* FPTR bits */
  93. #define FPTR_EN_T (1 << 15)
  94. /* FTCTR bits */
  95. #define FTCTR_FS_BYPASS_R (1 << 29)
  96. #define FTCTR_FS_BYPASS_W (1 << 28)
  97. /* FMSSTOP bits */
  98. #define FMSSTOP_MISR_START (1 << 17)
  99. /* EEMSSTOP bits */
  100. #define EEMSSTOP_STRTBIST (1 << 31)
  101. /* Index sector */
  102. #define ISS_CUSTOMER_START1 (0x830)
  103. #define ISS_CUSTOMER_END1 (0xA00)
  104. #define ISS_CUSTOMER_SIZE1 (ISS_CUSTOMER_END1 - ISS_CUSTOMER_START1)
  105. #define ISS_CUSTOMER_NWORDS1 (ISS_CUSTOMER_SIZE1 / 4)
  106. #define ISS_CUSTOMER_START2 (0xA40)
  107. #define ISS_CUSTOMER_END2 (0xC00)
  108. #define ISS_CUSTOMER_SIZE2 (ISS_CUSTOMER_END2 - ISS_CUSTOMER_START2)
  109. #define ISS_CUSTOMER_NWORDS2 (ISS_CUSTOMER_SIZE2 / 4)
  110. #define ISS_CUSTOMER_SIZE (ISS_CUSTOMER_SIZE1 + ISS_CUSTOMER_SIZE2)
  111. /**
  112. * Private data for \c lpc2900 flash driver.
  113. */
  114. struct lpc2900_flash_bank {
  115. /**
  116. * This flag is set when the device has been successfully probed.
  117. */
  118. bool is_probed;
  119. /**
  120. * Holds the value read from CHIPID register.
  121. * The driver will not load if the chipid doesn't match the expected
  122. * value of 0x209CE02B of the LPC2900 family. A probe will only be done
  123. * if the chipid does not yet contain the expected value.
  124. */
  125. uint32_t chipid;
  126. /**
  127. * String holding device name.
  128. * This string is set by the probe function to the type number of the
  129. * device. It takes the form "LPC29xx".
  130. */
  131. char *target_name;
  132. /**
  133. * System clock frequency.
  134. * Holds the clock frequency in Hz, as passed by the configuration file
  135. * to the <tt>flash bank</tt> command.
  136. */
  137. uint32_t clk_sys_fmc;
  138. /**
  139. * Flag to indicate that dangerous operations are possible.
  140. * This flag can be set by passing the correct password to the
  141. * <tt>lpc2900 password</tt> command. If set, other dangerous commands,
  142. * which operate on the index sector, can be executed.
  143. */
  144. uint32_t risky;
  145. /**
  146. * Maximum contiguous block of internal SRAM (bytes).
  147. * Autodetected by the driver. Not the total amount of SRAM, only the
  148. * the largest \em contiguous block!
  149. */
  150. uint32_t max_ram_block;
  151. };
  152. static uint32_t lpc2900_wait_status(struct flash_bank *bank, uint32_t mask, int timeout);
  153. static void lpc2900_setup(struct flash_bank *bank);
  154. static uint32_t lpc2900_is_ready(struct flash_bank *bank);
  155. static uint32_t lpc2900_read_security_status(struct flash_bank *bank);
  156. static uint32_t lpc2900_run_bist128(struct flash_bank *bank,
  157. uint32_t addr_from, uint32_t addr_to,
  158. uint32_t signature[4]);
  159. static uint32_t lpc2900_address2sector(struct flash_bank *bank, uint32_t offset);
  160. static uint32_t lpc2900_calc_tr(uint32_t clock_var, uint32_t time_var);
  161. /*********************** Helper functions **************************/
  162. /**
  163. * Wait for an event in mask to occur in INT_STATUS.
  164. *
  165. * Return when an event occurs, or after a timeout.
  166. *
  167. * @param[in] bank Pointer to the flash bank descriptor
  168. * @param[in] mask Mask to be used for INT_STATUS
  169. * @param[in] timeout Timeout in ms
  170. */
  171. static uint32_t lpc2900_wait_status(struct flash_bank *bank,
  172. uint32_t mask,
  173. int timeout)
  174. {
  175. uint32_t int_status;
  176. struct target *target = bank->target;
  177. do {
  178. alive_sleep(1);
  179. timeout--;
  180. target_read_u32(target, INT_STATUS, &int_status);
  181. } while (((int_status & mask) == 0) && (timeout != 0));
  182. if (timeout == 0) {
  183. LOG_DEBUG("Timeout!");
  184. return ERROR_FLASH_OPERATION_FAILED;
  185. }
  186. return ERROR_OK;
  187. }
  188. /**
  189. * Set up the flash for erase/program operations.
  190. *
  191. * Enable the flash, and set the correct CRA clock of 66 kHz.
  192. *
  193. * @param bank Pointer to the flash bank descriptor
  194. */
  195. static void lpc2900_setup(struct flash_bank *bank)
  196. {
  197. uint32_t fcra;
  198. struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
  199. /* Power up the flash block */
  200. target_write_u32(bank->target, FCTR, FCTR_FS_WEB | FCTR_FS_CS);
  201. fcra = (lpc2900_info->clk_sys_fmc / (3 * 66000)) - 1;
  202. target_write_u32(bank->target, FCRA, fcra);
  203. }
  204. /**
  205. * Check if device is ready.
  206. *
  207. * Check if device is ready for flash operation:
  208. * Must have been successfully probed.
  209. * Must be halted.
  210. */
  211. static uint32_t lpc2900_is_ready(struct flash_bank *bank)
  212. {
  213. struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
  214. if (!lpc2900_info->is_probed)
  215. return ERROR_FLASH_BANK_NOT_PROBED;
  216. if (bank->target->state != TARGET_HALTED) {
  217. LOG_ERROR("Target not halted");
  218. return ERROR_TARGET_NOT_HALTED;
  219. }
  220. return ERROR_OK;
  221. }
  222. /**
  223. * Read the status of sector security from the index sector.
  224. *
  225. * @param bank Pointer to the flash bank descriptor
  226. */
  227. static uint32_t lpc2900_read_security_status(struct flash_bank *bank)
  228. {
  229. uint32_t status = lpc2900_is_ready(bank);
  230. if (status != ERROR_OK)
  231. return status;
  232. struct target *target = bank->target;
  233. /* Enable ISS access */
  234. target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB | FCTR_FS_ISS);
  235. /* Read the relevant block of memory from the ISS sector */
  236. uint32_t iss_secured_field[0x230/16][4];
  237. target_read_memory(target, bank->base + 0xC00, 4, 0x230/4,
  238. (uint8_t *)iss_secured_field);
  239. /* Disable ISS access */
  240. target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);
  241. /* Check status of each sector. Note that the sector numbering in the LPC2900
  242. * is different from the logical sector numbers used in OpenOCD!
  243. * Refer to the user manual for details.
  244. *
  245. * All zeros (16x 0x00) are treated as a secured sector (is_protected = 1)
  246. * All ones (16x 0xFF) are treated as a non-secured sector (is_protected = 0)
  247. * Anything else is undefined (is_protected = -1). This is treated as
  248. * a protected sector!
  249. */
  250. int sector;
  251. int index_t;
  252. for (sector = 0; sector < bank->num_sectors; sector++) {
  253. /* Convert logical sector number to physical sector number */
  254. if (sector <= 4)
  255. index_t = sector + 11;
  256. else if (sector <= 7)
  257. index_t = sector + 27;
  258. else
  259. index_t = sector - 8;
  260. bank->sectors[sector].is_protected = -1;
  261. if ((iss_secured_field[index_t][0] == 0x00000000) &&
  262. (iss_secured_field[index_t][1] == 0x00000000) &&
  263. (iss_secured_field[index_t][2] == 0x00000000) &&
  264. (iss_secured_field[index_t][3] == 0x00000000))
  265. bank->sectors[sector].is_protected = 1;
  266. if ((iss_secured_field[index_t][0] == 0xFFFFFFFF) &&
  267. (iss_secured_field[index_t][1] == 0xFFFFFFFF) &&
  268. (iss_secured_field[index_t][2] == 0xFFFFFFFF) &&
  269. (iss_secured_field[index_t][3] == 0xFFFFFFFF))
  270. bank->sectors[sector].is_protected = 0;
  271. }
  272. return ERROR_OK;
  273. }
  274. /**
  275. * Use BIST to calculate a 128-bit hash value over a range of flash.
  276. *
  277. * @param bank Pointer to the flash bank descriptor
  278. * @param addr_from
  279. * @param addr_to
  280. * @param signature
  281. */
  282. static uint32_t lpc2900_run_bist128(struct flash_bank *bank,
  283. uint32_t addr_from,
  284. uint32_t addr_to,
  285. uint32_t signature[4])
  286. {
  287. struct target *target = bank->target;
  288. /* Clear END_OF_MISR interrupt status */
  289. target_write_u32(target, INT_CLR_STATUS, INTSRC_END_OF_MISR);
  290. /* Start address */
  291. target_write_u32(target, FMSSTART, addr_from >> 4);
  292. /* End address, and issue start command */
  293. target_write_u32(target, FMSSTOP, (addr_to >> 4) | FMSSTOP_MISR_START);
  294. /* Poll for end of operation. Calculate a reasonable timeout. */
  295. if (lpc2900_wait_status(bank, INTSRC_END_OF_MISR, 1000) != ERROR_OK)
  296. return ERROR_FLASH_OPERATION_FAILED;
  297. /* Return the signature */
  298. uint8_t sig_buf[4 * 4];
  299. target_read_memory(target, FMSW0, 4, 4, sig_buf);
  300. target_buffer_get_u32_array(target, sig_buf, 4, signature);
  301. return ERROR_OK;
  302. }
  303. /**
  304. * Return sector number for given address.
  305. *
  306. * Return the (logical) sector number for a given relative address.
  307. * No sanity check is done. It assumed that the address is valid.
  308. *
  309. * @param bank Pointer to the flash bank descriptor
  310. * @param offset Offset address relative to bank start
  311. */
  312. static uint32_t lpc2900_address2sector(struct flash_bank *bank,
  313. uint32_t offset)
  314. {
  315. uint32_t address = bank->base + offset;
  316. /* Run through all sectors of this bank */
  317. int sector;
  318. for (sector = 0; sector < bank->num_sectors; sector++) {
  319. /* Return immediately if address is within the current sector */
  320. if (address < (bank->sectors[sector].offset + bank->sectors[sector].size))
  321. return sector;
  322. }
  323. /* We should never come here. If we do, return an arbitrary sector number. */
  324. return 0;
  325. }
  326. /**
  327. * Write one page to the index sector.
  328. *
  329. * @param bank Pointer to the flash bank descriptor
  330. * @param pagenum Page number (0...7)
  331. * @param page Page array (FLASH_PAGE_SIZE bytes)
  332. */
  333. static int lpc2900_write_index_page(struct flash_bank *bank,
  334. int pagenum,
  335. uint8_t page[FLASH_PAGE_SIZE])
  336. {
  337. /* Only pages 4...7 are user writable */
  338. if ((pagenum < 4) || (pagenum > 7)) {
  339. LOG_ERROR("Refuse to burn index sector page %d", pagenum);
  340. return ERROR_COMMAND_ARGUMENT_INVALID;
  341. }
  342. /* Get target, and check if it's halted */
  343. struct target *target = bank->target;
  344. if (target->state != TARGET_HALTED) {
  345. LOG_ERROR("Target not halted");
  346. return ERROR_TARGET_NOT_HALTED;
  347. }
  348. /* Private info */
  349. struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
  350. /* Enable flash block and set the correct CRA clock of 66 kHz */
  351. lpc2900_setup(bank);
  352. /* Un-protect the index sector */
  353. target_write_u32(target, bank->base, 0);
  354. target_write_u32(target, FCTR,
  355. FCTR_FS_LOADREQ | FCTR_FS_WPB | FCTR_FS_ISS |
  356. FCTR_FS_WEB | FCTR_FS_WRE | FCTR_FS_CS);
  357. /* Set latch load mode */
  358. target_write_u32(target, FCTR,
  359. FCTR_FS_ISS | FCTR_FS_WEB | FCTR_FS_WRE | FCTR_FS_CS);
  360. /* Write whole page to flash data latches */
  361. if (target_write_memory(target,
  362. bank->base + pagenum * FLASH_PAGE_SIZE,
  363. 4, FLASH_PAGE_SIZE / 4, page) != ERROR_OK) {
  364. LOG_ERROR("Index sector write failed @ page %d", pagenum);
  365. target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);
  366. return ERROR_FLASH_OPERATION_FAILED;
  367. }
  368. /* Clear END_OF_BURN interrupt status */
  369. target_write_u32(target, INT_CLR_STATUS, INTSRC_END_OF_BURN);
  370. /* Set the program/erase time to FLASH_PROGRAM_TIME */
  371. target_write_u32(target, FPTR,
  372. FPTR_EN_T | lpc2900_calc_tr(lpc2900_info->clk_sys_fmc,
  373. FLASH_PROGRAM_TIME));
  374. /* Trigger flash write */
  375. target_write_u32(target, FCTR,
  376. FCTR_FS_PROGREQ | FCTR_FS_ISS |
  377. FCTR_FS_WPB | FCTR_FS_WRE | FCTR_FS_CS);
  378. /* Wait for the end of the write operation. If it's not over after one
  379. * second, something went dreadfully wrong... :-(
  380. */
  381. if (lpc2900_wait_status(bank, INTSRC_END_OF_BURN, 1000) != ERROR_OK) {
  382. LOG_ERROR("Index sector write failed @ page %d", pagenum);
  383. target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);
  384. return ERROR_FLASH_OPERATION_FAILED;
  385. }
  386. target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);
  387. return ERROR_OK;
  388. }
  389. /**
  390. * Calculate FPTR.TR register value for desired program/erase time.
  391. *
  392. * @param clock System clock in Hz
  393. * @param time Program/erase time in µs
  394. */
  395. static uint32_t lpc2900_calc_tr(uint32_t clock_var, uint32_t time_var)
  396. {
  397. /* ((time[µs]/1e6) * f[Hz]) + 511
  398. * FPTR.TR = -------------------------------
  399. * 512
  400. */
  401. uint32_t tr_val = (uint32_t)((((time_var / 1e6) * clock_var) + 511.0) / 512.0);
  402. return tr_val;
  403. }
  404. /*********************** Private flash commands **************************/
  405. /**
  406. * Command to determine the signature of the whole flash.
  407. *
  408. * Uses the Built-In-Self-Test (BIST) to generate a 128-bit hash value
  409. * of the flash content.
  410. */
  411. COMMAND_HANDLER(lpc2900_handle_signature_command)
  412. {
  413. uint32_t status;
  414. uint32_t signature[4];
  415. if (CMD_ARGC < 1)
  416. return ERROR_COMMAND_SYNTAX_ERROR;
  417. struct flash_bank *bank;
  418. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
  419. if (ERROR_OK != retval)
  420. return retval;
  421. if (bank->target->state != TARGET_HALTED) {
  422. LOG_ERROR("Target not halted");
  423. return ERROR_TARGET_NOT_HALTED;
  424. }
  425. /* Run BIST over whole flash range */
  426. status = lpc2900_run_bist128(bank, bank->base, bank->base + (bank->size - 1), signature);
  427. if (status != ERROR_OK)
  428. return status;
  429. command_print(CMD_CTX, "signature: 0x%8.8" PRIx32
  430. ":0x%8.8" PRIx32
  431. ":0x%8.8" PRIx32
  432. ":0x%8.8" PRIx32,
  433. signature[3], signature[2], signature[1], signature[0]);
  434. return ERROR_OK;
  435. }
  436. /**
  437. * Store customer info in file.
  438. *
  439. * Read customer info from index sector, and store that block of data into
  440. * a disk file. The format is binary.
  441. */
  442. COMMAND_HANDLER(lpc2900_handle_read_custom_command)
  443. {
  444. if (CMD_ARGC < 2)
  445. return ERROR_COMMAND_SYNTAX_ERROR;
  446. struct flash_bank *bank;
  447. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
  448. if (ERROR_OK != retval)
  449. return retval;
  450. struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
  451. lpc2900_info->risky = 0;
  452. /* Get target, and check if it's halted */
  453. struct target *target = bank->target;
  454. if (target->state != TARGET_HALTED) {
  455. LOG_ERROR("Target not halted");
  456. return ERROR_TARGET_NOT_HALTED;
  457. }
  458. /* Storage for customer info. Read in two parts */
  459. uint8_t customer[4 * (ISS_CUSTOMER_NWORDS1 + ISS_CUSTOMER_NWORDS2)];
  460. /* Enable access to index sector */
  461. target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB | FCTR_FS_ISS);
  462. /* Read two parts */
  463. target_read_memory(target, bank->base+ISS_CUSTOMER_START1, 4,
  464. ISS_CUSTOMER_NWORDS1,
  465. &customer[0]);
  466. target_read_memory(target, bank->base+ISS_CUSTOMER_START2, 4,
  467. ISS_CUSTOMER_NWORDS2,
  468. &customer[4 * ISS_CUSTOMER_NWORDS1]);
  469. /* Deactivate access to index sector */
  470. target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);
  471. /* Try and open the file */
  472. struct fileio fileio;
  473. const char *filename = CMD_ARGV[1];
  474. int ret = fileio_open(&fileio, filename, FILEIO_WRITE, FILEIO_BINARY);
  475. if (ret != ERROR_OK) {
  476. LOG_WARNING("Could not open file %s", filename);
  477. return ret;
  478. }
  479. size_t nwritten;
  480. ret = fileio_write(&fileio, sizeof(customer), customer, &nwritten);
  481. if (ret != ERROR_OK) {
  482. LOG_ERROR("Write operation to file %s failed", filename);
  483. fileio_close(&fileio);
  484. return ret;
  485. }
  486. fileio_close(&fileio);
  487. return ERROR_OK;
  488. }
  489. /**
  490. * Enter password to enable potentially dangerous options.
  491. */
  492. COMMAND_HANDLER(lpc2900_handle_password_command)
  493. {
  494. if (CMD_ARGC < 2)
  495. return ERROR_COMMAND_SYNTAX_ERROR;
  496. struct flash_bank *bank;
  497. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
  498. if (ERROR_OK != retval)
  499. return retval;
  500. struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
  501. #define ISS_PASSWORD "I_know_what_I_am_doing"
  502. lpc2900_info->risky = !strcmp(CMD_ARGV[1], ISS_PASSWORD);
  503. if (!lpc2900_info->risky) {
  504. command_print(CMD_CTX, "Wrong password (use '%s')", ISS_PASSWORD);
  505. return ERROR_COMMAND_ARGUMENT_INVALID;
  506. }
  507. command_print(CMD_CTX,
  508. "Potentially dangerous operation allowed in next command!");
  509. return ERROR_OK;
  510. }
  511. /**
  512. * Write customer info from file to the index sector.
  513. */
  514. COMMAND_HANDLER(lpc2900_handle_write_custom_command)
  515. {
  516. if (CMD_ARGC < 2)
  517. return ERROR_COMMAND_SYNTAX_ERROR;
  518. struct flash_bank *bank;
  519. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
  520. if (ERROR_OK != retval)
  521. return retval;
  522. struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
  523. /* Check if command execution is allowed. */
  524. if (!lpc2900_info->risky) {
  525. command_print(CMD_CTX, "Command execution not allowed!");
  526. return ERROR_COMMAND_ARGUMENT_INVALID;
  527. }
  528. lpc2900_info->risky = 0;
  529. /* Get target, and check if it's halted */
  530. struct target *target = bank->target;
  531. if (target->state != TARGET_HALTED) {
  532. LOG_ERROR("Target not halted");
  533. return ERROR_TARGET_NOT_HALTED;
  534. }
  535. /* The image will always start at offset 0 */
  536. struct image image;
  537. image.base_address_set = 1;
  538. image.base_address = 0;
  539. image.start_address_set = 0;
  540. const char *filename = CMD_ARGV[1];
  541. const char *type = (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL;
  542. retval = image_open(&image, filename, type);
  543. if (retval != ERROR_OK)
  544. return retval;
  545. /* Do a sanity check: The image must be exactly the size of the customer
  546. programmable area. Any other size is rejected. */
  547. if (image.num_sections != 1) {
  548. LOG_ERROR("Only one section allowed in image file.");
  549. return ERROR_COMMAND_SYNTAX_ERROR;
  550. }
  551. if ((image.sections[0].base_address != 0) ||
  552. (image.sections[0].size != ISS_CUSTOMER_SIZE)) {
  553. LOG_ERROR("Incorrect image file size. Expected %d, "
  554. "got %" PRIu32,
  555. ISS_CUSTOMER_SIZE, image.sections[0].size);
  556. return ERROR_COMMAND_SYNTAX_ERROR;
  557. }
  558. /* Well boys, I reckon this is it... */
  559. /* Customer info is split into two blocks in pages 4 and 5. */
  560. uint8_t page[FLASH_PAGE_SIZE];
  561. /* Page 4 */
  562. uint32_t offset = ISS_CUSTOMER_START1 % FLASH_PAGE_SIZE;
  563. memset(page, 0xff, FLASH_PAGE_SIZE);
  564. size_t size_read;
  565. retval = image_read_section(&image, 0, 0,
  566. ISS_CUSTOMER_SIZE1, &page[offset], &size_read);
  567. if (retval != ERROR_OK) {
  568. LOG_ERROR("couldn't read from file '%s'", filename);
  569. image_close(&image);
  570. return retval;
  571. }
  572. retval = lpc2900_write_index_page(bank, 4, page);
  573. if (retval != ERROR_OK) {
  574. image_close(&image);
  575. return retval;
  576. }
  577. /* Page 5 */
  578. offset = ISS_CUSTOMER_START2 % FLASH_PAGE_SIZE;
  579. memset(page, 0xff, FLASH_PAGE_SIZE);
  580. retval = image_read_section(&image, 0, ISS_CUSTOMER_SIZE1,
  581. ISS_CUSTOMER_SIZE2, &page[offset], &size_read);
  582. if (retval != ERROR_OK) {
  583. LOG_ERROR("couldn't read from file '%s'", filename);
  584. image_close(&image);
  585. return retval;
  586. }
  587. retval = lpc2900_write_index_page(bank, 5, page);
  588. if (retval != ERROR_OK) {
  589. image_close(&image);
  590. return retval;
  591. }
  592. image_close(&image);
  593. return ERROR_OK;
  594. }
  595. /**
  596. * Activate 'sector security' for a range of sectors.
  597. */
  598. COMMAND_HANDLER(lpc2900_handle_secure_sector_command)
  599. {
  600. if (CMD_ARGC < 3)
  601. return ERROR_COMMAND_SYNTAX_ERROR;
  602. /* Get the bank descriptor */
  603. struct flash_bank *bank;
  604. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
  605. if (ERROR_OK != retval)
  606. return retval;
  607. struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
  608. /* Check if command execution is allowed. */
  609. if (!lpc2900_info->risky) {
  610. command_print(CMD_CTX, "Command execution not allowed! "
  611. "(use 'password' command first)");
  612. return ERROR_COMMAND_ARGUMENT_INVALID;
  613. }
  614. lpc2900_info->risky = 0;
  615. /* Read sector range, and do a sanity check. */
  616. int first, last;
  617. COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], first);
  618. COMMAND_PARSE_NUMBER(int, CMD_ARGV[2], last);
  619. if ((first >= bank->num_sectors) ||
  620. (last >= bank->num_sectors) ||
  621. (first > last)) {
  622. command_print(CMD_CTX, "Illegal sector range");
  623. return ERROR_COMMAND_ARGUMENT_INVALID;
  624. }
  625. uint8_t page[FLASH_PAGE_SIZE];
  626. int sector;
  627. /* Sectors in page 6 */
  628. if ((first <= 4) || (last >= 8)) {
  629. memset(&page, 0xff, FLASH_PAGE_SIZE);
  630. for (sector = first; sector <= last; sector++) {
  631. if (sector <= 4)
  632. memset(&page[0xB0 + 16*sector], 0, 16);
  633. else if (sector >= 8)
  634. memset(&page[0x00 + 16*(sector - 8)], 0, 16);
  635. }
  636. retval = lpc2900_write_index_page(bank, 6, page);
  637. if (retval != ERROR_OK) {
  638. LOG_ERROR("failed to update index sector page 6");
  639. return retval;
  640. }
  641. }
  642. /* Sectors in page 7 */
  643. if ((first <= 7) && (last >= 5)) {
  644. memset(&page, 0xff, FLASH_PAGE_SIZE);
  645. for (sector = first; sector <= last; sector++) {
  646. if ((sector >= 5) && (sector <= 7))
  647. memset(&page[0x00 + 16*(sector - 5)], 0, 16);
  648. }
  649. retval = lpc2900_write_index_page(bank, 7, page);
  650. if (retval != ERROR_OK) {
  651. LOG_ERROR("failed to update index sector page 7");
  652. return retval;
  653. }
  654. }
  655. command_print(CMD_CTX,
  656. "Sectors security will become effective after next power cycle");
  657. /* Update the sector security status */
  658. if (lpc2900_read_security_status(bank) != ERROR_OK) {
  659. LOG_ERROR("Cannot determine sector security status");
  660. return ERROR_FLASH_OPERATION_FAILED;
  661. }
  662. return ERROR_OK;
  663. }
  664. /**
  665. * Activate JTAG protection.
  666. */
  667. COMMAND_HANDLER(lpc2900_handle_secure_jtag_command)
  668. {
  669. if (CMD_ARGC < 1)
  670. return ERROR_COMMAND_SYNTAX_ERROR;
  671. /* Get the bank descriptor */
  672. struct flash_bank *bank;
  673. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
  674. if (ERROR_OK != retval)
  675. return retval;
  676. struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
  677. /* Check if command execution is allowed. */
  678. if (!lpc2900_info->risky) {
  679. command_print(CMD_CTX, "Command execution not allowed! "
  680. "(use 'password' command first)");
  681. return ERROR_COMMAND_ARGUMENT_INVALID;
  682. }
  683. lpc2900_info->risky = 0;
  684. /* Prepare page */
  685. uint8_t page[FLASH_PAGE_SIZE];
  686. memset(&page, 0xff, FLASH_PAGE_SIZE);
  687. /* Insert "soft" protection word */
  688. page[0x30 + 15] = 0x7F;
  689. page[0x30 + 11] = 0x7F;
  690. page[0x30 + 7] = 0x7F;
  691. page[0x30 + 3] = 0x7F;
  692. /* Write to page 5 */
  693. retval = lpc2900_write_index_page(bank, 5, page);
  694. if (retval != ERROR_OK) {
  695. LOG_ERROR("failed to update index sector page 5");
  696. return retval;
  697. }
  698. LOG_INFO("JTAG security set. Good bye!");
  699. return ERROR_OK;
  700. }
  701. /*********************** Flash interface functions **************************/
  702. static const struct command_registration lpc2900_exec_command_handlers[] = {
  703. {
  704. .name = "signature",
  705. .usage = "<bank>",
  706. .handler = lpc2900_handle_signature_command,
  707. .mode = COMMAND_EXEC,
  708. .help = "Calculate and display signature of flash bank.",
  709. },
  710. {
  711. .name = "read_custom",
  712. .handler = lpc2900_handle_read_custom_command,
  713. .mode = COMMAND_EXEC,
  714. .usage = "bank_id filename",
  715. .help = "Copies 912 bytes of customer information "
  716. "from index sector into file.",
  717. },
  718. {
  719. .name = "password",
  720. .handler = lpc2900_handle_password_command,
  721. .mode = COMMAND_EXEC,
  722. .usage = "bank_id password",
  723. .help = "Enter fixed password to enable 'dangerous' options.",
  724. },
  725. {
  726. .name = "write_custom",
  727. .handler = lpc2900_handle_write_custom_command,
  728. .mode = COMMAND_EXEC,
  729. .usage = "bank_id filename ('bin'|'ihex'|'elf'|'s19')",
  730. .help = "Copies 912 bytes of customer info from file "
  731. "to index sector.",
  732. },
  733. {
  734. .name = "secure_sector",
  735. .handler = lpc2900_handle_secure_sector_command,
  736. .mode = COMMAND_EXEC,
  737. .usage = "bank_id first_sector last_sector",
  738. .help = "Activate sector security for a range of sectors. "
  739. "It will be effective after a power cycle.",
  740. },
  741. {
  742. .name = "secure_jtag",
  743. .handler = lpc2900_handle_secure_jtag_command,
  744. .mode = COMMAND_EXEC,
  745. .usage = "bank_id",
  746. .help = "Disable the JTAG port. "
  747. "It will be effective after a power cycle.",
  748. },
  749. COMMAND_REGISTRATION_DONE
  750. };
  751. static const struct command_registration lpc2900_command_handlers[] = {
  752. {
  753. .name = "lpc2900",
  754. .mode = COMMAND_ANY,
  755. .help = "LPC2900 flash command group",
  756. .usage = "",
  757. .chain = lpc2900_exec_command_handlers,
  758. },
  759. COMMAND_REGISTRATION_DONE
  760. };
  761. /** Evaluate flash bank command. */
  762. FLASH_BANK_COMMAND_HANDLER(lpc2900_flash_bank_command)
  763. {
  764. struct lpc2900_flash_bank *lpc2900_info;
  765. if (CMD_ARGC < 6)
  766. return ERROR_COMMAND_SYNTAX_ERROR;
  767. lpc2900_info = malloc(sizeof(struct lpc2900_flash_bank));
  768. bank->driver_priv = lpc2900_info;
  769. /* Get flash clock.
  770. * Reject it if we can't meet the requirements for program time
  771. * (if clock too slow), or for erase time (clock too fast).
  772. */
  773. uint32_t clk_sys_fmc;
  774. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[6], clk_sys_fmc);
  775. lpc2900_info->clk_sys_fmc = clk_sys_fmc * 1000;
  776. uint32_t clock_limit;
  777. /* Check program time limit */
  778. clock_limit = 512000000l / FLASH_PROGRAM_TIME;
  779. if (lpc2900_info->clk_sys_fmc < clock_limit) {
  780. LOG_WARNING("flash clock must be at least %" PRIu32 " kHz",
  781. (clock_limit / 1000));
  782. return ERROR_FLASH_BANK_INVALID;
  783. }
  784. /* Check erase time limit */
  785. clock_limit = (uint32_t)((32767.0 * 512.0 * 1e6) / FLASH_ERASE_TIME);
  786. if (lpc2900_info->clk_sys_fmc > clock_limit) {
  787. LOG_WARNING("flash clock must be a maximum of %" PRIu32 " kHz",
  788. (clock_limit / 1000));
  789. return ERROR_FLASH_BANK_INVALID;
  790. }
  791. /* Chip ID will be obtained by probing the device later */
  792. lpc2900_info->chipid = 0;
  793. lpc2900_info->is_probed = false;
  794. return ERROR_OK;
  795. }
  796. /**
  797. * Erase sector(s).
  798. *
  799. * @param bank Pointer to the flash bank descriptor
  800. * @param first First sector to be erased
  801. * @param last Last sector (including) to be erased
  802. */
  803. static int lpc2900_erase(struct flash_bank *bank, int first, int last)
  804. {
  805. uint32_t status;
  806. int sector;
  807. int last_unsecured_sector;
  808. struct target *target = bank->target;
  809. struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
  810. status = lpc2900_is_ready(bank);
  811. if (status != ERROR_OK)
  812. return status;
  813. /* Sanity check on sector range */
  814. if ((first < 0) || (last < first) || (last >= bank->num_sectors)) {
  815. LOG_INFO("Bad sector range");
  816. return ERROR_FLASH_SECTOR_INVALID;
  817. }
  818. /* Update the info about secured sectors */
  819. lpc2900_read_security_status(bank);
  820. /* The selected sector range might include secured sectors. An attempt
  821. * to erase such a sector will cause the erase to fail also for unsecured
  822. * sectors. It is necessary to determine the last unsecured sector now,
  823. * because we have to treat the last relevant sector in the list in
  824. * a special way.
  825. */
  826. last_unsecured_sector = -1;
  827. for (sector = first; sector <= last; sector++) {
  828. if (!bank->sectors[sector].is_protected)
  829. last_unsecured_sector = sector;
  830. }
  831. /* Exit now, in case of the rare constellation where all sectors in range
  832. * are secured. This is regarded a success, since erasing/programming of
  833. * secured sectors shall be handled transparently.
  834. */
  835. if (last_unsecured_sector == -1)
  836. return ERROR_OK;
  837. /* Enable flash block and set the correct CRA clock of 66 kHz */
  838. lpc2900_setup(bank);
  839. /* Clear END_OF_ERASE interrupt status */
  840. target_write_u32(target, INT_CLR_STATUS, INTSRC_END_OF_ERASE);
  841. /* Set the program/erase timer to FLASH_ERASE_TIME */
  842. target_write_u32(target, FPTR,
  843. FPTR_EN_T | lpc2900_calc_tr(lpc2900_info->clk_sys_fmc,
  844. FLASH_ERASE_TIME));
  845. /* Sectors are marked for erasure, then erased all together */
  846. for (sector = first; sector <= last_unsecured_sector; sector++) {
  847. /* Only mark sectors that aren't secured. Any attempt to erase a group
  848. * of sectors will fail if any single one of them is secured!
  849. */
  850. if (!bank->sectors[sector].is_protected) {
  851. /* Unprotect the sector */
  852. target_write_u32(target, bank->sectors[sector].offset, 0);
  853. target_write_u32(target, FCTR,
  854. FCTR_FS_LOADREQ | FCTR_FS_WPB |
  855. FCTR_FS_WEB | FCTR_FS_WRE | FCTR_FS_CS);
  856. /* Mark the sector for erasure. The last sector in the list
  857. triggers the erasure. */
  858. target_write_u32(target, bank->sectors[sector].offset, 0);
  859. if (sector == last_unsecured_sector) {
  860. target_write_u32(target, FCTR,
  861. FCTR_FS_PROGREQ | FCTR_FS_WPB | FCTR_FS_CS);
  862. } else {
  863. target_write_u32(target, FCTR,
  864. FCTR_FS_LOADREQ | FCTR_FS_WPB |
  865. FCTR_FS_WEB | FCTR_FS_CS);
  866. }
  867. }
  868. }
  869. /* Wait for the end of the erase operation. If it's not over after two seconds,
  870. * something went dreadfully wrong... :-(
  871. */
  872. if (lpc2900_wait_status(bank, INTSRC_END_OF_ERASE, 2000) != ERROR_OK)
  873. return ERROR_FLASH_OPERATION_FAILED;
  874. /* Normal flash operating mode */
  875. target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);
  876. return ERROR_OK;
  877. }
  878. static int lpc2900_protect(struct flash_bank *bank, int set, int first, int last)
  879. {
  880. /* This command is not supported.
  881. * "Protection" in LPC2900 terms is handled transparently. Sectors will
  882. * automatically be unprotected as needed.
  883. * Instead we use the concept of sector security. A secured sector is shown
  884. * as "protected" in OpenOCD. Sector security is a permanent feature, and
  885. * cannot be disabled once activated.
  886. */
  887. return ERROR_OK;
  888. }
  889. /**
  890. * Write data to flash.
  891. *
  892. * @param bank Pointer to the flash bank descriptor
  893. * @param buffer Buffer with data
  894. * @param offset Start address (relative to bank start)
  895. * @param count Number of bytes to be programmed
  896. */
  897. static int lpc2900_write(struct flash_bank *bank, const uint8_t *buffer,
  898. uint32_t offset, uint32_t count)
  899. {
  900. uint8_t page[FLASH_PAGE_SIZE];
  901. uint32_t status;
  902. uint32_t num_bytes;
  903. struct target *target = bank->target;
  904. struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
  905. int sector;
  906. int retval;
  907. static const uint32_t write_target_code[] = {
  908. /* Set auto latch mode: FCTR=CS|WRE|WEB */
  909. 0xe3a0a007, /* loop mov r10, #0x007 */
  910. 0xe583a000, /* str r10,[r3,#0] */
  911. /* Load complete page into latches */
  912. 0xe3a06020, /* mov r6,#(512/16) */
  913. 0xe8b00f00, /* next ldmia r0!,{r8-r11} */
  914. 0xe8a10f00, /* stmia r1!,{r8-r11} */
  915. 0xe2566001, /* subs r6,#1 */
  916. 0x1afffffb, /* bne next */
  917. /* Clear END_OF_BURN interrupt status */
  918. 0xe3a0a002, /* mov r10,#(1 << 1) */
  919. 0xe583afe8, /* str r10,[r3,#0xfe8] */
  920. /* Set the erase time to FLASH_PROGRAM_TIME */
  921. 0xe5834008, /* str r4,[r3,#8] */
  922. /* Trigger flash write
  923. * FCTR = CS | WRE | WPB | PROGREQ */
  924. 0xe3a0a083, /* mov r10,#0x83 */
  925. 0xe38aaa01, /* orr r10,#0x1000 */
  926. 0xe583a000, /* str r10,[r3,#0] */
  927. /* Wait for end of burn */
  928. 0xe593afe0, /* wait ldr r10,[r3,#0xfe0] */
  929. 0xe21aa002, /* ands r10,#(1 << 1) */
  930. 0x0afffffc, /* beq wait */
  931. /* End? */
  932. 0xe2522001, /* subs r2,#1 */
  933. 0x1affffed, /* bne loop */
  934. 0xeafffffe /* done b done */
  935. };
  936. status = lpc2900_is_ready(bank);
  937. if (status != ERROR_OK)
  938. return status;
  939. /* Enable flash block and set the correct CRA clock of 66 kHz */
  940. lpc2900_setup(bank);
  941. /* Update the info about secured sectors */
  942. lpc2900_read_security_status(bank);
  943. /* Unprotect all involved sectors */
  944. for (sector = 0; sector < bank->num_sectors; sector++) {
  945. /* Start address in or before this sector?
  946. * End address in or behind this sector? */
  947. if (((bank->base + offset) <
  948. (bank->sectors[sector].offset + bank->sectors[sector].size)) &&
  949. ((bank->base + (offset + count - 1)) >= bank->sectors[sector].offset)) {
  950. /* This sector is involved and needs to be unprotected.
  951. * Don't do it for secured sectors.
  952. */
  953. if (!bank->sectors[sector].is_protected) {
  954. target_write_u32(target, bank->sectors[sector].offset, 0);
  955. target_write_u32(target, FCTR,
  956. FCTR_FS_LOADREQ | FCTR_FS_WPB |
  957. FCTR_FS_WEB | FCTR_FS_WRE | FCTR_FS_CS);
  958. }
  959. }
  960. }
  961. /* Set the program/erase time to FLASH_PROGRAM_TIME */
  962. uint32_t prog_time = FPTR_EN_T | lpc2900_calc_tr(lpc2900_info->clk_sys_fmc, FLASH_PROGRAM_TIME);
  963. /* If there is a working area of reasonable size, use it to program via
  964. * a target algorithm. If not, fall back to host programming. */
  965. /* We need some room for target code. */
  966. const uint32_t target_code_size = sizeof(write_target_code);
  967. /* Try working area allocation. Start with a large buffer, and try with
  968. * reduced size if that fails. */
  969. struct working_area *warea;
  970. uint32_t buffer_size = lpc2900_info->max_ram_block - 1 * KiB;
  971. while ((retval = target_alloc_working_area_try(target,
  972. buffer_size + target_code_size,
  973. &warea)) != ERROR_OK) {
  974. /* Try a smaller buffer now, and stop if it's too small. */
  975. buffer_size -= 1 * KiB;
  976. if (buffer_size < 2 * KiB) {
  977. LOG_INFO("no (large enough) working area, falling back to host mode");
  978. warea = NULL;
  979. break;
  980. }
  981. }
  982. ;
  983. if (warea) {
  984. struct reg_param reg_params[5];
  985. struct arm_algorithm arm_algo;
  986. /* We can use target mode. Download the algorithm. */
  987. uint8_t code[sizeof(write_target_code)];
  988. target_buffer_set_u32_array(target, code, ARRAY_SIZE(write_target_code),
  989. write_target_code);
  990. retval = target_write_buffer(target, (warea->address) + buffer_size, sizeof(code), code);
  991. if (retval != ERROR_OK) {
  992. LOG_ERROR("Unable to write block write code to target");
  993. target_free_all_working_areas(target);
  994. return ERROR_FLASH_OPERATION_FAILED;
  995. }
  996. init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
  997. init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
  998. init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT);
  999. init_reg_param(&reg_params[3], "r3", 32, PARAM_OUT);
  1000. init_reg_param(&reg_params[4], "r4", 32, PARAM_OUT);
  1001. /* Write to flash in large blocks */
  1002. while (count != 0) {
  1003. uint32_t this_npages;
  1004. const uint8_t *this_buffer;
  1005. int start_sector = lpc2900_address2sector(bank, offset);
  1006. /* First page / last page / rest */
  1007. if (offset % FLASH_PAGE_SIZE) {
  1008. /* Block doesn't start on page boundary.
  1009. * Burn first partial page separately. */
  1010. memset(&page, 0xff, sizeof(page));
  1011. memcpy(&page[offset % FLASH_PAGE_SIZE],
  1012. buffer,
  1013. FLASH_PAGE_SIZE - (offset % FLASH_PAGE_SIZE));
  1014. this_npages = 1;
  1015. this_buffer = &page[0];
  1016. count = count + (offset % FLASH_PAGE_SIZE);
  1017. offset = offset - (offset % FLASH_PAGE_SIZE);
  1018. } else if (count < FLASH_PAGE_SIZE) {
  1019. /* Download last incomplete page separately. */
  1020. memset(&page, 0xff, sizeof(page));
  1021. memcpy(&page, buffer, count);
  1022. this_npages = 1;
  1023. this_buffer = &page[0];
  1024. count = FLASH_PAGE_SIZE;
  1025. } else {
  1026. /* Download as many full pages as possible */
  1027. this_npages = (count < buffer_size) ?
  1028. count / FLASH_PAGE_SIZE :
  1029. buffer_size / FLASH_PAGE_SIZE;
  1030. this_buffer = buffer;
  1031. /* Make sure we stop at the next secured sector */
  1032. sector = start_sector + 1;
  1033. while (sector < bank->num_sectors) {
  1034. /* Secured? */
  1035. if (bank->sectors[sector].is_protected) {
  1036. /* Is that next sector within the current block? */
  1037. if ((bank->sectors[sector].offset - bank->base) <
  1038. (offset + (this_npages * FLASH_PAGE_SIZE))) {
  1039. /* Yes! Split the block */
  1040. this_npages =
  1041. (bank->sectors[sector].offset -
  1042. bank->base - offset)
  1043. / FLASH_PAGE_SIZE;
  1044. break;
  1045. }
  1046. }
  1047. sector++;
  1048. }
  1049. }
  1050. /* Skip the current sector if it is secured */
  1051. if (bank->sectors[start_sector].is_protected) {
  1052. LOG_DEBUG("Skip secured sector %d",
  1053. start_sector);
  1054. /* Stop if this is the last sector */
  1055. if (start_sector == bank->num_sectors - 1)
  1056. break;
  1057. /* Skip */
  1058. uint32_t nskip = bank->sectors[start_sector].size -
  1059. (offset % bank->sectors[start_sector].size);
  1060. offset += nskip;
  1061. buffer += nskip;
  1062. count = (count >= nskip) ? (count - nskip) : 0;
  1063. continue;
  1064. }
  1065. /* Execute buffer download */
  1066. retval = target_write_buffer(target, warea->address,
  1067. this_npages * FLASH_PAGE_SIZE, this_buffer);
  1068. if (retval != ERROR_OK) {
  1069. LOG_ERROR("Unable to write data to target");
  1070. target_free_all_working_areas(target);
  1071. return ERROR_FLASH_OPERATION_FAILED;
  1072. }
  1073. /* Prepare registers */
  1074. buf_set_u32(reg_params[0].value, 0, 32, warea->address);
  1075. buf_set_u32(reg_params[1].value, 0, 32, offset);
  1076. buf_set_u32(reg_params[2].value, 0, 32, this_npages);
  1077. buf_set_u32(reg_params[3].value, 0, 32, FCTR);
  1078. buf_set_u32(reg_params[4].value, 0, 32, FPTR_EN_T | prog_time);
  1079. /* Execute algorithm, assume breakpoint for last instruction */
  1080. arm_algo.common_magic = ARM_COMMON_MAGIC;
  1081. arm_algo.core_mode = ARM_MODE_SVC;
  1082. arm_algo.core_state = ARM_STATE_ARM;
  1083. retval = target_run_algorithm(target, 0, NULL, 5, reg_params,
  1084. (warea->address) + buffer_size,
  1085. (warea->address) + buffer_size + target_code_size - 4,
  1086. 10000, /* 10s should be enough for max. 16 KiB of data */
  1087. &arm_algo);
  1088. if (retval != ERROR_OK) {
  1089. LOG_ERROR("Execution of flash algorithm failed.");
  1090. target_free_all_working_areas(target);
  1091. retval = ERROR_FLASH_OPERATION_FAILED;
  1092. break;
  1093. }
  1094. count -= this_npages * FLASH_PAGE_SIZE;
  1095. buffer += this_npages * FLASH_PAGE_SIZE;
  1096. offset += this_npages * FLASH_PAGE_SIZE;
  1097. }
  1098. /* Free all resources */
  1099. destroy_reg_param(&reg_params[0]);
  1100. destroy_reg_param(&reg_params[1]);
  1101. destroy_reg_param(&reg_params[2]);
  1102. destroy_reg_param(&reg_params[3]);
  1103. destroy_reg_param(&reg_params[4]);
  1104. target_free_all_working_areas(target);
  1105. } else {
  1106. /* Write to flash memory page-wise */
  1107. while (count != 0) {
  1108. /* How many bytes do we copy this time? */
  1109. num_bytes = (count >= FLASH_PAGE_SIZE) ?
  1110. FLASH_PAGE_SIZE - (offset % FLASH_PAGE_SIZE) :
  1111. count;
  1112. /* Don't do anything with it if the page is in a secured sector. */
  1113. if (!bank->sectors[lpc2900_address2sector(bank, offset)].is_protected) {
  1114. /* Set latch load mode */
  1115. target_write_u32(target, FCTR,
  1116. FCTR_FS_CS | FCTR_FS_WRE | FCTR_FS_WEB);
  1117. /* Always clear the buffer (a little overhead, but who cares) */
  1118. memset(page, 0xFF, FLASH_PAGE_SIZE);
  1119. /* Copy them to the buffer */
  1120. memcpy(&page[offset % FLASH_PAGE_SIZE],
  1121. &buffer[offset % FLASH_PAGE_SIZE],
  1122. num_bytes);
  1123. /* Write whole page to flash data latches */
  1124. if (target_write_memory(target,
  1125. bank->base + (offset - (offset % FLASH_PAGE_SIZE)),
  1126. 4, FLASH_PAGE_SIZE / 4, page) != ERROR_OK) {
  1127. LOG_ERROR("Write failed @ 0x%8.8" PRIx32, offset);
  1128. target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);
  1129. return ERROR_FLASH_OPERATION_FAILED;
  1130. }
  1131. /* Clear END_OF_BURN interrupt status */
  1132. target_write_u32(target, INT_CLR_STATUS, INTSRC_END_OF_BURN);
  1133. /* Set the programming time */
  1134. target_write_u32(target, FPTR, FPTR_EN_T | prog_time);
  1135. /* Trigger flash write */
  1136. target_write_u32(target, FCTR,
  1137. FCTR_FS_CS | FCTR_FS_WRE | FCTR_FS_WPB | FCTR_FS_PROGREQ);
  1138. /* Wait for the end of the write operation. If it's not over
  1139. * after one second, something went dreadfully wrong... :-(
  1140. */
  1141. if (lpc2900_wait_status(bank, INTSRC_END_OF_BURN, 1000) != ERROR_OK) {
  1142. LOG_ERROR("Write failed @ 0x%8.8" PRIx32, offset);
  1143. target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);
  1144. return ERROR_FLASH_OPERATION_FAILED;
  1145. }
  1146. }
  1147. /* Update pointers and counters */
  1148. offset += num_bytes;
  1149. buffer += num_bytes;
  1150. count -= num_bytes;
  1151. }
  1152. retval = ERROR_OK;
  1153. }
  1154. /* Normal flash operating mode */
  1155. target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);
  1156. return retval;
  1157. }
  1158. /**
  1159. * Try and identify the device.
  1160. *
  1161. * Determine type number and its memory layout.
  1162. *
  1163. * @param bank Pointer to the flash bank descriptor
  1164. */
  1165. static int lpc2900_probe(struct flash_bank *bank)
  1166. {
  1167. struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
  1168. struct target *target = bank->target;
  1169. int i = 0;
  1170. uint32_t offset;
  1171. if (target->state != TARGET_HALTED) {
  1172. LOG_ERROR("Target not halted");
  1173. return ERROR_TARGET_NOT_HALTED;
  1174. }
  1175. /* We want to do this only once. */
  1176. if (lpc2900_info->is_probed)
  1177. return ERROR_OK;
  1178. /* Probing starts with reading the CHIPID register. We will continue only
  1179. * if this identifies as an LPC2900 device.
  1180. */
  1181. target_read_u32(target, CHIPID, &lpc2900_info->chipid);
  1182. if (lpc2900_info->chipid != EXPECTED_CHIPID) {
  1183. LOG_WARNING("Device is not an LPC29xx");
  1184. return ERROR_FLASH_OPERATION_FAILED;
  1185. }
  1186. /* It's an LPC29xx device. Now read the feature register FEAT0...FEAT3. */
  1187. uint32_t feat0, feat1, feat2, feat3;
  1188. target_read_u32(target, FEAT0, &feat0);
  1189. target_read_u32(target, FEAT1, &feat1);
  1190. target_read_u32(target, FEAT2, &feat2);
  1191. target_read_u32(target, FEAT3, &feat3);
  1192. /* Base address */
  1193. bank->base = 0x20000000;
  1194. /* Determine flash layout from FEAT2 register */
  1195. uint32_t num_64k_sectors = (feat2 >> 16) & 0xFF;
  1196. uint32_t num_8k_sectors = (feat2 >> 0) & 0xFF;
  1197. bank->num_sectors = num_64k_sectors + num_8k_sectors;
  1198. bank->size = KiB * (64 * num_64k_sectors + 8 * num_8k_sectors);
  1199. /* Determine maximum contiguous RAM block */
  1200. lpc2900_info->max_ram_block = 16 * KiB;
  1201. if ((feat1 & 0x30) == 0x30) {
  1202. lpc2900_info->max_ram_block = 32 * KiB;
  1203. if ((feat1 & 0x0C) == 0x0C)
  1204. lpc2900_info->max_ram_block = 48 * KiB;
  1205. }
  1206. /* Determine package code and ITCM size */
  1207. uint32_t package_code = feat0 & 0x0F;
  1208. uint32_t itcm_code = (feat1 >> 16) & 0x1F;
  1209. /* Determine the exact type number. */
  1210. uint32_t found = 1;
  1211. if ((package_code == 4) && (itcm_code == 5)) {
  1212. /* Old LPC2917 or LPC2919 (non-/01 devices) */
  1213. lpc2900_info->target_name = (bank->size == 768*KiB) ? "LPC2919" : "LPC2917";
  1214. } else {
  1215. if (package_code == 2) {
  1216. /* 100-pin package */
  1217. if (bank->size == 128*KiB)
  1218. lpc2900_info->target_name = "LPC2921";
  1219. else if (bank->size == 256*KiB)
  1220. lpc2900_info->target_name = "LPC2923";
  1221. else if (bank->size == 512*KiB)
  1222. lpc2900_info->target_name = "LPC2925";
  1223. else
  1224. found = 0;
  1225. } else if (package_code == 4) {
  1226. /* 144-pin package */
  1227. if ((bank->size == 256*KiB) && (feat3 == 0xFFFFFFE9))
  1228. lpc2900_info->target_name = "LPC2926";
  1229. else if ((bank->size == 512*KiB) && (feat3 == 0xFFFFFCF0))
  1230. lpc2900_info->target_name = "LPC2917/01";
  1231. else if ((bank->size == 512*KiB) && (feat3 == 0xFFFFFFF1))
  1232. lpc2900_info->target_name = "LPC2927";
  1233. else if ((bank->size == 768*KiB) && (feat3 == 0xFFFFFCF8))
  1234. lpc2900_info->target_name = "LPC2919/01";
  1235. else if ((bank->size == 768*KiB) && (feat3 == 0xFFFFFFF9))
  1236. lpc2900_info->target_name = "LPC2929";
  1237. else
  1238. found = 0;
  1239. } else if (package_code == 5) {
  1240. /* 208-pin package */
  1241. lpc2900_info->target_name = (bank->size == 0) ? "LPC2930" : "LPC2939";
  1242. } else
  1243. found = 0;
  1244. }
  1245. if (!found) {
  1246. LOG_WARNING("Unknown LPC29xx derivative (FEATx="
  1247. "%08" PRIx32 ":%08" PRIx32 ":%08" PRIx32 ":%08" PRIx32 ")",
  1248. feat0, feat1, feat2, feat3);
  1249. return ERROR_FLASH_OPERATION_FAILED;
  1250. }
  1251. /* Show detected device */
  1252. LOG_INFO("Flash bank %d: Device %s, %" PRIu32
  1253. " KiB in %d sectors",
  1254. bank->bank_number,
  1255. lpc2900_info->target_name, bank->size / KiB,
  1256. bank->num_sectors);
  1257. /* Flashless devices cannot be handled */
  1258. if (bank->num_sectors == 0) {
  1259. LOG_WARNING("Flashless device cannot be handled");
  1260. return ERROR_FLASH_OPERATION_FAILED;
  1261. }
  1262. /* Sector layout.
  1263. * These are logical sector numbers. When doing real flash operations,
  1264. * the logical flash number are translated into the physical flash numbers
  1265. * of the device.
  1266. */
  1267. bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
  1268. offset = 0;
  1269. for (i = 0; i < bank->num_sectors; i++) {
  1270. bank->sectors[i].offset = offset;
  1271. bank->sectors[i].is_erased = -1;
  1272. bank->sectors[i].is_protected = -1;
  1273. if (i <= 7)
  1274. bank->sectors[i].size = 8 * KiB;
  1275. else if (i <= 18)
  1276. bank->sectors[i].size = 64 * KiB;
  1277. else {
  1278. /* We shouldn't come here. But there might be a new part out there
  1279. * that has more than 19 sectors. Politely ask for a fix then.
  1280. */
  1281. bank->sectors[i].size = 0;
  1282. LOG_ERROR("Never heard about sector %d", i);
  1283. }
  1284. offset += bank->sectors[i].size;
  1285. }
  1286. lpc2900_info->is_probed = true;
  1287. /* Read sector security status */
  1288. if (lpc2900_read_security_status(bank) != ERROR_OK) {
  1289. LOG_ERROR("Cannot determine sector security status");
  1290. return ERROR_FLASH_OPERATION_FAILED;
  1291. }
  1292. return ERROR_OK;
  1293. }
  1294. /**
  1295. * Run a blank check for each sector.
  1296. *
  1297. * For speed reasons, the device isn't read word by word.
  1298. * A hash value is calculated by the hardware ("BIST") for each sector.
  1299. * This value is then compared against the known hash of an empty sector.
  1300. *
  1301. * @param bank Pointer to the flash bank descriptor
  1302. */
  1303. static int lpc2900_erase_check(struct flash_bank *bank)
  1304. {
  1305. uint32_t status = lpc2900_is_ready(bank);
  1306. if (status != ERROR_OK) {
  1307. LOG_INFO("Processor not halted/not probed");
  1308. return status;
  1309. }
  1310. /* Use the BIST (Built-In Selft Test) to generate a signature of each flash
  1311. * sector. Compare against the expected signature of an empty sector.
  1312. */
  1313. int sector;
  1314. for (sector = 0; sector < bank->num_sectors; sector++) {
  1315. uint32_t signature[4];
  1316. status = lpc2900_run_bist128(bank, bank->sectors[sector].offset,
  1317. bank->sectors[sector].offset + (bank->sectors[sector].size - 1), signature);
  1318. if (status != ERROR_OK)
  1319. return status;
  1320. /* The expected signatures for an empty sector are different
  1321. * for 8 KiB and 64 KiB sectors.
  1322. */
  1323. if (bank->sectors[sector].size == 8*KiB) {
  1324. bank->sectors[sector].is_erased =
  1325. (signature[3] == 0x01ABAAAA) &&
  1326. (signature[2] == 0xAAAAAAAA) &&
  1327. (signature[1] == 0xAAAAAAAA) &&
  1328. (signature[0] == 0xAAA00AAA);
  1329. }
  1330. if (bank->sectors[sector].size == 64*KiB) {
  1331. bank->sectors[sector].is_erased =
  1332. (signature[3] == 0x11801222) &&
  1333. (signature[2] == 0xB88844FF) &&
  1334. (signature[1] == 0x11A22008) &&
  1335. (signature[0] == 0x2B1BFE44);
  1336. }
  1337. }
  1338. return ERROR_OK;
  1339. }
  1340. /**
  1341. * Get protection (sector security) status.
  1342. *
  1343. * Determine the status of "sector security" for each sector.
  1344. * A secured sector is one that can never be erased/programmed again.
  1345. *
  1346. * @param bank Pointer to the flash bank descriptor
  1347. */
  1348. static int lpc2900_protect_check(struct flash_bank *bank)
  1349. {
  1350. return lpc2900_read_security_status(bank);
  1351. }
  1352. struct flash_driver lpc2900_flash = {
  1353. .name = "lpc2900",
  1354. .commands = lpc2900_command_handlers,
  1355. .flash_bank_command = lpc2900_flash_bank_command,
  1356. .erase = lpc2900_erase,
  1357. .protect = lpc2900_protect,
  1358. .write = lpc2900_write,
  1359. .read = default_flash_read,
  1360. .probe = lpc2900_probe,
  1361. .auto_probe = lpc2900_probe,
  1362. .erase_check = lpc2900_erase_check,
  1363. .protect_check = lpc2900_protect_check,
  1364. };