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.
 
 
 
 
 
 

1263 lines
36 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007,2008 by Christopher Kilgour *
  3. * techie |_at_| whiterocker |_dot_| com *
  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. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "tms470.h"
  24. static int tms470_register_commands(struct command_context_s *cmd_ctx);
  25. static int tms470_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
  26. static int tms470_erase(struct flash_bank_s *bank, int first, int last);
  27. static int tms470_protect(struct flash_bank_s *bank, int set, int first, int last);
  28. static int tms470_write(struct flash_bank_s *bank, uint8_t * buffer, uint32_t offset, uint32_t count);
  29. static int tms470_probe(struct flash_bank_s *bank);
  30. static int tms470_auto_probe(struct flash_bank_s *bank);
  31. static int tms470_erase_check(struct flash_bank_s *bank);
  32. static int tms470_protect_check(struct flash_bank_s *bank);
  33. static int tms470_info(struct flash_bank_s *bank, char *buf, int buf_size);
  34. flash_driver_t tms470_flash = {
  35. .name = "tms470",
  36. .register_commands = tms470_register_commands,
  37. .flash_bank_command = tms470_flash_bank_command,
  38. .erase = tms470_erase,
  39. .protect = tms470_protect,
  40. .write = tms470_write,
  41. .probe = tms470_probe,
  42. .auto_probe = tms470_auto_probe,
  43. .erase_check = tms470_erase_check,
  44. .protect_check = tms470_protect_check,
  45. .info = tms470_info
  46. };
  47. /* ----------------------------------------------------------------------
  48. Internal Support, Helpers
  49. ---------------------------------------------------------------------- */
  50. const flash_sector_t TMS470R1A256_SECTORS[] = {
  51. {0x00000000, 0x00002000, -1, -1},
  52. {0x00002000, 0x00002000, -1, -1},
  53. {0x00004000, 0x00002000, -1, -1},
  54. {0x00006000, 0x00002000, -1, -1},
  55. {0x00008000, 0x00008000, -1, -1},
  56. {0x00010000, 0x00008000, -1, -1},
  57. {0x00018000, 0x00008000, -1, -1},
  58. {0x00020000, 0x00008000, -1, -1},
  59. {0x00028000, 0x00008000, -1, -1},
  60. {0x00030000, 0x00008000, -1, -1},
  61. {0x00038000, 0x00002000, -1, -1},
  62. {0x0003A000, 0x00002000, -1, -1},
  63. {0x0003C000, 0x00002000, -1, -1},
  64. {0x0003E000, 0x00002000, -1, -1},
  65. };
  66. #define TMS470R1A256_NUM_SECTORS \
  67. (sizeof(TMS470R1A256_SECTORS)/sizeof(TMS470R1A256_SECTORS[0]))
  68. const flash_sector_t TMS470R1A288_BANK0_SECTORS[] = {
  69. {0x00000000, 0x00002000, -1, -1},
  70. {0x00002000, 0x00002000, -1, -1},
  71. {0x00004000, 0x00002000, -1, -1},
  72. {0x00006000, 0x00002000, -1, -1},
  73. };
  74. #define TMS470R1A288_BANK0_NUM_SECTORS \
  75. (sizeof(TMS470R1A288_BANK0_SECTORS)/sizeof(TMS470R1A288_BANK0_SECTORS[0]))
  76. const flash_sector_t TMS470R1A288_BANK1_SECTORS[] = {
  77. {0x00040000, 0x00010000, -1, -1},
  78. {0x00050000, 0x00010000, -1, -1},
  79. {0x00060000, 0x00010000, -1, -1},
  80. {0x00070000, 0x00010000, -1, -1},
  81. };
  82. #define TMS470R1A288_BANK1_NUM_SECTORS \
  83. (sizeof(TMS470R1A288_BANK1_SECTORS)/sizeof(TMS470R1A288_BANK1_SECTORS[0]))
  84. const flash_sector_t TMS470R1A384_BANK0_SECTORS[] = {
  85. {0x00000000, 0x00002000, -1, -1},
  86. {0x00002000, 0x00002000, -1, -1},
  87. {0x00004000, 0x00004000, -1, -1},
  88. {0x00008000, 0x00004000, -1, -1},
  89. {0x0000C000, 0x00004000, -1, -1},
  90. {0x00010000, 0x00004000, -1, -1},
  91. {0x00014000, 0x00004000, -1, -1},
  92. {0x00018000, 0x00002000, -1, -1},
  93. {0x0001C000, 0x00002000, -1, -1},
  94. {0x0001E000, 0x00002000, -1, -1},
  95. };
  96. #define TMS470R1A384_BANK0_NUM_SECTORS \
  97. (sizeof(TMS470R1A384_BANK0_SECTORS)/sizeof(TMS470R1A384_BANK0_SECTORS[0]))
  98. const flash_sector_t TMS470R1A384_BANK1_SECTORS[] = {
  99. {0x00020000, 0x00008000, -1, -1},
  100. {0x00028000, 0x00008000, -1, -1},
  101. {0x00030000, 0x00008000, -1, -1},
  102. {0x00038000, 0x00008000, -1, -1},
  103. };
  104. #define TMS470R1A384_BANK1_NUM_SECTORS \
  105. (sizeof(TMS470R1A384_BANK1_SECTORS)/sizeof(TMS470R1A384_BANK1_SECTORS[0]))
  106. const flash_sector_t TMS470R1A384_BANK2_SECTORS[] = {
  107. {0x00040000, 0x00008000, -1, -1},
  108. {0x00048000, 0x00008000, -1, -1},
  109. {0x00050000, 0x00008000, -1, -1},
  110. {0x00058000, 0x00008000, -1, -1},
  111. };
  112. #define TMS470R1A384_BANK2_NUM_SECTORS \
  113. (sizeof(TMS470R1A384_BANK2_SECTORS)/sizeof(TMS470R1A384_BANK2_SECTORS[0]))
  114. /* ---------------------------------------------------------------------- */
  115. static int tms470_read_part_info(struct flash_bank_s *bank)
  116. {
  117. tms470_flash_bank_t *tms470_info = bank->driver_priv;
  118. target_t *target = bank->target;
  119. uint32_t device_ident_reg;
  120. uint32_t silicon_version;
  121. uint32_t technology_family;
  122. uint32_t rom_flash;
  123. uint32_t part_number;
  124. char *part_name;
  125. /* we shall not rely on the caller in this test, this function allocates memory,
  126. thus and executing the code more than once may cause memory leak */
  127. if (tms470_info->device_ident_reg)
  128. return ERROR_OK;
  129. /* read and parse the device identification register */
  130. target_read_u32(target, 0xFFFFFFF0, &device_ident_reg);
  131. LOG_INFO("device_ident_reg = 0x%08" PRIx32 "", device_ident_reg);
  132. if ((device_ident_reg & 7) == 0)
  133. {
  134. LOG_WARNING("Cannot identify target as a TMS470 family.");
  135. return ERROR_FLASH_OPERATION_FAILED;
  136. }
  137. silicon_version = (device_ident_reg >> 12) & 0xF;
  138. technology_family = (device_ident_reg >> 11) & 1;
  139. rom_flash = (device_ident_reg >> 10) & 1;
  140. part_number = (device_ident_reg >> 3) & 0x7f;
  141. /*
  142. * If the part number is known, determine if the flash bank is valid
  143. * based on the base address being within the known flash bank
  144. * ranges. Then fixup/complete the remaining fields of the flash
  145. * bank structure.
  146. */
  147. switch (part_number)
  148. {
  149. case 0x0a:
  150. part_name = "TMS470R1A256";
  151. if (bank->base >= 0x00040000)
  152. {
  153. LOG_ERROR("No %s flash bank contains base address 0x%08" PRIx32 ".", part_name, bank->base);
  154. return ERROR_FLASH_OPERATION_FAILED;
  155. }
  156. tms470_info->ordinal = 0;
  157. bank->base = 0x00000000;
  158. bank->size = 256 * 1024;
  159. bank->num_sectors = TMS470R1A256_NUM_SECTORS;
  160. bank->sectors = malloc(sizeof(TMS470R1A256_SECTORS));
  161. if (!bank->sectors)
  162. {
  163. return ERROR_FLASH_OPERATION_FAILED;
  164. }
  165. (void)memcpy(bank->sectors, TMS470R1A256_SECTORS, sizeof(TMS470R1A256_SECTORS));
  166. break;
  167. case 0x2b:
  168. part_name = "TMS470R1A288";
  169. if (bank->base < 0x00008000)
  170. {
  171. tms470_info->ordinal = 0;
  172. bank->base = 0x00000000;
  173. bank->size = 32 * 1024;
  174. bank->num_sectors = TMS470R1A288_BANK0_NUM_SECTORS;
  175. bank->sectors = malloc(sizeof(TMS470R1A288_BANK0_SECTORS));
  176. if (!bank->sectors)
  177. {
  178. return ERROR_FLASH_OPERATION_FAILED;
  179. }
  180. (void)memcpy(bank->sectors, TMS470R1A288_BANK0_SECTORS, sizeof(TMS470R1A288_BANK0_SECTORS));
  181. }
  182. else if ((bank->base >= 0x00040000) && (bank->base < 0x00080000))
  183. {
  184. tms470_info->ordinal = 1;
  185. bank->base = 0x00040000;
  186. bank->size = 256 * 1024;
  187. bank->num_sectors = TMS470R1A288_BANK1_NUM_SECTORS;
  188. bank->sectors = malloc(sizeof(TMS470R1A288_BANK1_SECTORS));
  189. if (!bank->sectors)
  190. {
  191. return ERROR_FLASH_OPERATION_FAILED;
  192. }
  193. (void)memcpy(bank->sectors, TMS470R1A288_BANK1_SECTORS, sizeof(TMS470R1A288_BANK1_SECTORS));
  194. }
  195. else
  196. {
  197. LOG_ERROR("No %s flash bank contains base address 0x%08" PRIx32 ".", part_name, bank->base);
  198. return ERROR_FLASH_OPERATION_FAILED;
  199. }
  200. break;
  201. case 0x2d:
  202. part_name = "TMS470R1A384";
  203. if (bank->base < 0x00020000)
  204. {
  205. tms470_info->ordinal = 0;
  206. bank->base = 0x00000000;
  207. bank->size = 128 * 1024;
  208. bank->num_sectors = TMS470R1A384_BANK0_NUM_SECTORS;
  209. bank->sectors = malloc(sizeof(TMS470R1A384_BANK0_SECTORS));
  210. if (!bank->sectors)
  211. {
  212. return ERROR_FLASH_OPERATION_FAILED;
  213. }
  214. (void)memcpy(bank->sectors, TMS470R1A384_BANK0_SECTORS, sizeof(TMS470R1A384_BANK0_SECTORS));
  215. }
  216. else if ((bank->base >= 0x00020000) && (bank->base < 0x00040000))
  217. {
  218. tms470_info->ordinal = 1;
  219. bank->base = 0x00020000;
  220. bank->size = 128 * 1024;
  221. bank->num_sectors = TMS470R1A384_BANK1_NUM_SECTORS;
  222. bank->sectors = malloc(sizeof(TMS470R1A384_BANK1_SECTORS));
  223. if (!bank->sectors)
  224. {
  225. return ERROR_FLASH_OPERATION_FAILED;
  226. }
  227. (void)memcpy(bank->sectors, TMS470R1A384_BANK1_SECTORS, sizeof(TMS470R1A384_BANK1_SECTORS));
  228. }
  229. else if ((bank->base >= 0x00040000) && (bank->base < 0x00060000))
  230. {
  231. tms470_info->ordinal = 2;
  232. bank->base = 0x00040000;
  233. bank->size = 128 * 1024;
  234. bank->num_sectors = TMS470R1A384_BANK2_NUM_SECTORS;
  235. bank->sectors = malloc(sizeof(TMS470R1A384_BANK2_SECTORS));
  236. if (!bank->sectors)
  237. {
  238. return ERROR_FLASH_OPERATION_FAILED;
  239. }
  240. (void)memcpy(bank->sectors, TMS470R1A384_BANK2_SECTORS, sizeof(TMS470R1A384_BANK2_SECTORS));
  241. }
  242. else
  243. {
  244. LOG_ERROR("No %s flash bank contains base address 0x%08" PRIx32 ".", part_name, bank->base);
  245. return ERROR_FLASH_OPERATION_FAILED;
  246. }
  247. break;
  248. default:
  249. LOG_WARNING("Could not identify part 0x%02x as a member of the TMS470 family.", (unsigned)part_number);
  250. return ERROR_FLASH_OPERATION_FAILED;
  251. }
  252. /* turn off memory selects */
  253. target_write_u32(target, 0xFFFFFFE4, 0x00000000);
  254. target_write_u32(target, 0xFFFFFFE0, 0x00000000);
  255. bank->chip_width = 32;
  256. bank->bus_width = 32;
  257. LOG_INFO("Identified %s, ver=%d, core=%s, nvmem=%s.",
  258. part_name,
  259. (int)(silicon_version),
  260. (technology_family ? "1.8v" : "3.3v"),
  261. (rom_flash ? "rom" : "flash"));
  262. tms470_info->device_ident_reg = device_ident_reg;
  263. tms470_info->silicon_version = silicon_version;
  264. tms470_info->technology_family = technology_family;
  265. tms470_info->rom_flash = rom_flash;
  266. tms470_info->part_number = part_number;
  267. tms470_info->part_name = part_name;
  268. /*
  269. * Disable reset on address access violation.
  270. */
  271. target_write_u32(target, 0xFFFFFFE0, 0x00004007);
  272. return ERROR_OK;
  273. }
  274. /* ---------------------------------------------------------------------- */
  275. static uint32_t keysSet = 0;
  276. static uint32_t flashKeys[4];
  277. static int tms470_handle_flash_keyset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  278. {
  279. if (argc > 4)
  280. {
  281. command_print(cmd_ctx, "tms470 flash_keyset <key0> <key1> <key2> <key3>");
  282. return ERROR_INVALID_ARGUMENTS;
  283. }
  284. else if (argc == 4)
  285. {
  286. int i;
  287. for (i = 0; i < 4; i++)
  288. {
  289. int start = (0 == strncmp(args[i], "0x", 2)) ? 2 : 0;
  290. if (1 != sscanf(&args[i][start], "%" SCNx32 "", &flashKeys[i]))
  291. {
  292. command_print(cmd_ctx, "could not process flash key %s", args[i]);
  293. LOG_ERROR("could not process flash key %s", args[i]);
  294. return ERROR_INVALID_ARGUMENTS;
  295. }
  296. }
  297. keysSet = 1;
  298. }
  299. else if (argc != 0)
  300. {
  301. command_print(cmd_ctx, "tms470 flash_keyset <key0> <key1> <key2> <key3>");
  302. return ERROR_INVALID_ARGUMENTS;
  303. }
  304. if (keysSet)
  305. {
  306. command_print(cmd_ctx, "using flash keys 0x%08" PRIx32 ", 0x%08" PRIx32 ", 0x%08" PRIx32 ", 0x%08" PRIx32 "",
  307. flashKeys[0], flashKeys[1], flashKeys[2], flashKeys[3]);
  308. }
  309. else
  310. {
  311. command_print(cmd_ctx, "flash keys not set");
  312. }
  313. return ERROR_OK;
  314. }
  315. static const uint32_t FLASH_KEYS_ALL_ONES[] = { 0xFFFFFFFF, 0xFFFFFFFF,
  316. 0xFFFFFFFF, 0xFFFFFFFF,
  317. };
  318. static const uint32_t FLASH_KEYS_ALL_ZEROS[] = { 0x00000000, 0x00000000,
  319. 0x00000000, 0x00000000,
  320. };
  321. static const uint32_t FLASH_KEYS_MIX1[] = { 0xf0fff0ff, 0xf0fff0ff,
  322. 0xf0fff0ff, 0xf0fff0ff
  323. };
  324. static const uint32_t FLASH_KEYS_MIX2[] = { 0x0000ffff, 0x0000ffff,
  325. 0x0000ffff, 0x0000ffff
  326. };
  327. /* ---------------------------------------------------------------------- */
  328. static int oscMHz = 12;
  329. static int tms470_handle_osc_megahertz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  330. {
  331. if (argc > 1)
  332. {
  333. command_print(cmd_ctx, "tms470 osc_megahertz <MHz>");
  334. return ERROR_INVALID_ARGUMENTS;
  335. }
  336. else if (argc == 1)
  337. {
  338. sscanf(args[0], "%d", &oscMHz);
  339. }
  340. if (oscMHz <= 0)
  341. {
  342. LOG_ERROR("osc_megahertz must be positive and non-zero!");
  343. command_print(cmd_ctx, "osc_megahertz must be positive and non-zero!");
  344. oscMHz = 12;
  345. return ERROR_INVALID_ARGUMENTS;
  346. }
  347. command_print(cmd_ctx, "osc_megahertz=%d", oscMHz);
  348. return ERROR_OK;
  349. }
  350. /* ---------------------------------------------------------------------- */
  351. static int plldis = 0;
  352. static int tms470_handle_plldis_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  353. {
  354. if (argc > 1)
  355. {
  356. command_print(cmd_ctx, "tms470 plldis <0 | 1>");
  357. return ERROR_INVALID_ARGUMENTS;
  358. }
  359. else if (argc == 1)
  360. {
  361. sscanf(args[0], "%d", &plldis);
  362. plldis = plldis ? 1 : 0;
  363. }
  364. command_print(cmd_ctx, "plldis=%d", plldis);
  365. return ERROR_OK;
  366. }
  367. /* ---------------------------------------------------------------------- */
  368. static int tms470_check_flash_unlocked(target_t * target)
  369. {
  370. uint32_t fmbbusy;
  371. target_read_u32(target, 0xFFE89C08, &fmbbusy);
  372. LOG_INFO("tms470 fmbbusy = 0x%08" PRIx32 " -> %s", fmbbusy, fmbbusy & 0x8000 ? "unlocked" : "LOCKED");
  373. return fmbbusy & 0x8000 ? ERROR_OK : ERROR_FLASH_OPERATION_FAILED;
  374. }
  375. /* ---------------------------------------------------------------------- */
  376. static int tms470_try_flash_keys(target_t * target, const uint32_t * key_set)
  377. {
  378. uint32_t glbctrl, fmmstat;
  379. int retval = ERROR_FLASH_OPERATION_FAILED;
  380. /* set GLBCTRL.4 */
  381. target_read_u32(target, 0xFFFFFFDC, &glbctrl);
  382. target_write_u32(target, 0xFFFFFFDC, glbctrl | 0x10);
  383. /* only perform the key match when 3VSTAT is clear */
  384. target_read_u32(target, 0xFFE8BC0C, &fmmstat);
  385. if (!(fmmstat & 0x08))
  386. {
  387. unsigned i;
  388. uint32_t fmbptr, fmbac2, orig_fmregopt;
  389. target_write_u32(target, 0xFFE8BC04, fmmstat & ~0x07);
  390. /* wait for pump ready */
  391. do
  392. {
  393. target_read_u32(target, 0xFFE8A814, &fmbptr);
  394. alive_sleep(1);
  395. }
  396. while (!(fmbptr & 0x0200));
  397. /* force max wait states */
  398. target_read_u32(target, 0xFFE88004, &fmbac2);
  399. target_write_u32(target, 0xFFE88004, fmbac2 | 0xff);
  400. /* save current access mode, force normal read mode */
  401. target_read_u32(target, 0xFFE89C00, &orig_fmregopt);
  402. target_write_u32(target, 0xFFE89C00, 0x00);
  403. for (i = 0; i < 4; i++)
  404. {
  405. uint32_t tmp;
  406. /* There is no point displaying the value of tmp, it is
  407. * filtered by the chip. The purpose of this read is to
  408. * prime the unlocking logic rather than read out the value.
  409. */
  410. target_read_u32(target, 0x00001FF0 + 4 * i, &tmp);
  411. LOG_INFO("tms470 writing fmpkey = 0x%08" PRIx32 "", key_set[i]);
  412. target_write_u32(target, 0xFFE89C0C, key_set[i]);
  413. }
  414. if (ERROR_OK == tms470_check_flash_unlocked(target))
  415. {
  416. /*
  417. * There seems to be a side-effect of reading the FMPKEY
  418. * register in that it re-enables the protection. So we
  419. * re-enable it.
  420. */
  421. for (i = 0; i < 4; i++)
  422. {
  423. uint32_t tmp;
  424. target_read_u32(target, 0x00001FF0 + 4 * i, &tmp);
  425. target_write_u32(target, 0xFFE89C0C, key_set[i]);
  426. }
  427. retval = ERROR_OK;
  428. }
  429. /* restore settings */
  430. target_write_u32(target, 0xFFE89C00, orig_fmregopt);
  431. target_write_u32(target, 0xFFE88004, fmbac2);
  432. }
  433. /* clear config bit */
  434. target_write_u32(target, 0xFFFFFFDC, glbctrl);
  435. return retval;
  436. }
  437. /* ---------------------------------------------------------------------- */
  438. static int tms470_unlock_flash(struct flash_bank_s *bank)
  439. {
  440. target_t *target = bank->target;
  441. const uint32_t *p_key_sets[5];
  442. unsigned i, key_set_count;
  443. if (keysSet)
  444. {
  445. key_set_count = 5;
  446. p_key_sets[0] = flashKeys;
  447. p_key_sets[1] = FLASH_KEYS_ALL_ONES;
  448. p_key_sets[2] = FLASH_KEYS_ALL_ZEROS;
  449. p_key_sets[3] = FLASH_KEYS_MIX1;
  450. p_key_sets[4] = FLASH_KEYS_MIX2;
  451. }
  452. else
  453. {
  454. key_set_count = 4;
  455. p_key_sets[0] = FLASH_KEYS_ALL_ONES;
  456. p_key_sets[1] = FLASH_KEYS_ALL_ZEROS;
  457. p_key_sets[2] = FLASH_KEYS_MIX1;
  458. p_key_sets[3] = FLASH_KEYS_MIX2;
  459. }
  460. for (i = 0; i < key_set_count; i++)
  461. {
  462. if (tms470_try_flash_keys(target, p_key_sets[i]) == ERROR_OK)
  463. {
  464. LOG_INFO("tms470 flash is unlocked");
  465. return ERROR_OK;
  466. }
  467. }
  468. LOG_WARNING("tms470 could not unlock flash memory protection level 2");
  469. return ERROR_FLASH_OPERATION_FAILED;
  470. }
  471. /* ---------------------------------------------------------------------- */
  472. static int tms470_flash_initialize_internal_state_machine(struct flash_bank_s *bank)
  473. {
  474. uint32_t fmmac2, fmmac1, fmmaxep, k, delay, glbctrl, sysclk;
  475. target_t *target = bank->target;
  476. tms470_flash_bank_t *tms470_info = bank->driver_priv;
  477. int result = ERROR_OK;
  478. /*
  479. * Select the desired bank to be programmed by writing BANK[2:0] of
  480. * FMMAC2.
  481. */
  482. target_read_u32(target, 0xFFE8BC04, &fmmac2);
  483. fmmac2 &= ~0x0007;
  484. fmmac2 |= (tms470_info->ordinal & 7);
  485. target_write_u32(target, 0xFFE8BC04, fmmac2);
  486. LOG_DEBUG("set fmmac2 = 0x%04" PRIx32 "", fmmac2);
  487. /*
  488. * Disable level 1 sector protection by setting bit 15 of FMMAC1.
  489. */
  490. target_read_u32(target, 0xFFE8BC00, &fmmac1);
  491. fmmac1 |= 0x8000;
  492. target_write_u32(target, 0xFFE8BC00, fmmac1);
  493. LOG_DEBUG("set fmmac1 = 0x%04" PRIx32 "", fmmac1);
  494. /*
  495. * FMTCREG = 0x2fc0;
  496. */
  497. target_write_u32(target, 0xFFE8BC10, 0x2fc0);
  498. LOG_DEBUG("set fmtcreg = 0x2fc0");
  499. /*
  500. * MAXPP = 50
  501. */
  502. target_write_u32(target, 0xFFE8A07C, 50);
  503. LOG_DEBUG("set fmmaxpp = 50");
  504. /*
  505. * MAXCP = 0xf000 + 2000
  506. */
  507. target_write_u32(target, 0xFFE8A084, 0xf000 + 2000);
  508. LOG_DEBUG("set fmmaxcp = 0x%04x", 0xf000 + 2000);
  509. /*
  510. * configure VHV
  511. */
  512. target_read_u32(target, 0xFFE8A080, &fmmaxep);
  513. if (fmmaxep == 0xf000)
  514. {
  515. fmmaxep = 0xf000 + 4095;
  516. target_write_u32(target, 0xFFE8A80C, 0x9964);
  517. LOG_DEBUG("set fmptr3 = 0x9964");
  518. }
  519. else
  520. {
  521. fmmaxep = 0xa000 + 4095;
  522. target_write_u32(target, 0xFFE8A80C, 0x9b64);
  523. LOG_DEBUG("set fmptr3 = 0x9b64");
  524. }
  525. target_write_u32(target, 0xFFE8A080, fmmaxep);
  526. LOG_DEBUG("set fmmaxep = 0x%04" PRIx32 "", fmmaxep);
  527. /*
  528. * FMPTR4 = 0xa000
  529. */
  530. target_write_u32(target, 0xFFE8A810, 0xa000);
  531. LOG_DEBUG("set fmptr4 = 0xa000");
  532. /*
  533. * FMPESETUP, delay parameter selected based on clock frequency.
  534. *
  535. * According to the TI App Note SPNU257 and flashing code, delay is
  536. * int((sysclk(MHz) + 1) / 2), with a minimum of 5. The system
  537. * clock is usually derived from the ZPLL module, and selected by
  538. * the plldis global.
  539. */
  540. target_read_u32(target, 0xFFFFFFDC, &glbctrl);
  541. sysclk = (plldis ? 1 : (glbctrl & 0x08) ? 4 : 8) * oscMHz / (1 + (glbctrl & 7));
  542. delay = (sysclk > 10) ? (sysclk + 1) / 2 : 5;
  543. target_write_u32(target, 0xFFE8A018, (delay << 4) | (delay << 8));
  544. LOG_DEBUG("set fmpsetup = 0x%04" PRIx32 "", (delay << 4) | (delay << 8));
  545. /*
  546. * FMPVEVACCESS, based on delay.
  547. */
  548. k = delay | (delay << 8);
  549. target_write_u32(target, 0xFFE8A05C, k);
  550. LOG_DEBUG("set fmpvevaccess = 0x%04" PRIx32 "", k);
  551. /*
  552. * FMPCHOLD, FMPVEVHOLD, FMPVEVSETUP, based on delay.
  553. */
  554. k <<= 1;
  555. target_write_u32(target, 0xFFE8A034, k);
  556. LOG_DEBUG("set fmpchold = 0x%04" PRIx32 "", k);
  557. target_write_u32(target, 0xFFE8A040, k);
  558. LOG_DEBUG("set fmpvevhold = 0x%04" PRIx32 "", k);
  559. target_write_u32(target, 0xFFE8A024, k);
  560. LOG_DEBUG("set fmpvevsetup = 0x%04" PRIx32 "", k);
  561. /*
  562. * FMCVACCESS, based on delay.
  563. */
  564. k = delay * 16;
  565. target_write_u32(target, 0xFFE8A060, k);
  566. LOG_DEBUG("set fmcvaccess = 0x%04" PRIx32 "", k);
  567. /*
  568. * FMCSETUP, based on delay.
  569. */
  570. k = 0x3000 | delay * 20;
  571. target_write_u32(target, 0xFFE8A020, k);
  572. LOG_DEBUG("set fmcsetup = 0x%04" PRIx32 "", k);
  573. /*
  574. * FMEHOLD, based on delay.
  575. */
  576. k = (delay * 20) << 2;
  577. target_write_u32(target, 0xFFE8A038, k);
  578. LOG_DEBUG("set fmehold = 0x%04" PRIx32 "", k);
  579. /*
  580. * PWIDTH, CWIDTH, EWIDTH, based on delay.
  581. */
  582. target_write_u32(target, 0xFFE8A050, delay * 8);
  583. LOG_DEBUG("set fmpwidth = 0x%04" PRIx32 "", delay * 8);
  584. target_write_u32(target, 0xFFE8A058, delay * 1000);
  585. LOG_DEBUG("set fmcwidth = 0x%04" PRIx32 "", delay * 1000);
  586. target_write_u32(target, 0xFFE8A054, delay * 5400);
  587. LOG_DEBUG("set fmewidth = 0x%04" PRIx32 "", delay * 5400);
  588. return result;
  589. }
  590. /* ---------------------------------------------------------------------- */
  591. int tms470_flash_status(struct flash_bank_s *bank)
  592. {
  593. target_t *target = bank->target;
  594. int result = ERROR_OK;
  595. uint32_t fmmstat;
  596. target_read_u32(target, 0xFFE8BC0C, &fmmstat);
  597. LOG_DEBUG("set fmmstat = 0x%04" PRIx32 "", fmmstat);
  598. if (fmmstat & 0x0080)
  599. {
  600. LOG_WARNING("tms470 flash command: erase still active after busy clear.");
  601. result = ERROR_FLASH_OPERATION_FAILED;
  602. }
  603. if (fmmstat & 0x0040)
  604. {
  605. LOG_WARNING("tms470 flash command: program still active after busy clear.");
  606. result = ERROR_FLASH_OPERATION_FAILED;
  607. }
  608. if (fmmstat & 0x0020)
  609. {
  610. LOG_WARNING("tms470 flash command: invalid data command.");
  611. result = ERROR_FLASH_OPERATION_FAILED;
  612. }
  613. if (fmmstat & 0x0010)
  614. {
  615. LOG_WARNING("tms470 flash command: program, erase or validate sector failed.");
  616. result = ERROR_FLASH_OPERATION_FAILED;
  617. }
  618. if (fmmstat & 0x0008)
  619. {
  620. LOG_WARNING("tms470 flash command: voltage instability detected.");
  621. result = ERROR_FLASH_OPERATION_FAILED;
  622. }
  623. if (fmmstat & 0x0006)
  624. {
  625. LOG_WARNING("tms470 flash command: command suspend detected.");
  626. result = ERROR_FLASH_OPERATION_FAILED;
  627. }
  628. if (fmmstat & 0x0001)
  629. {
  630. LOG_WARNING("tms470 flash command: sector was locked.");
  631. result = ERROR_FLASH_OPERATION_FAILED;
  632. }
  633. return result;
  634. }
  635. /* ---------------------------------------------------------------------- */
  636. static int tms470_erase_sector(struct flash_bank_s *bank, int sector)
  637. {
  638. uint32_t glbctrl, orig_fmregopt, fmbsea, fmbseb, fmmstat;
  639. target_t *target = bank->target;
  640. uint32_t flashAddr = bank->base + bank->sectors[sector].offset;
  641. int result = ERROR_OK;
  642. /*
  643. * Set the bit GLBCTRL4 of the GLBCTRL register (in the System
  644. * module) to enable writing to the flash registers }.
  645. */
  646. target_read_u32(target, 0xFFFFFFDC, &glbctrl);
  647. target_write_u32(target, 0xFFFFFFDC, glbctrl | 0x10);
  648. LOG_DEBUG("set glbctrl = 0x%08" PRIx32 "", glbctrl | 0x10);
  649. /* Force normal read mode. */
  650. target_read_u32(target, 0xFFE89C00, &orig_fmregopt);
  651. target_write_u32(target, 0xFFE89C00, 0);
  652. LOG_DEBUG("set fmregopt = 0x%08x", 0);
  653. (void)tms470_flash_initialize_internal_state_machine(bank);
  654. /*
  655. * Select one or more bits in FMBSEA or FMBSEB to disable Level 1
  656. * protection for the particular sector to be erased/written.
  657. */
  658. if (sector < 16)
  659. {
  660. target_read_u32(target, 0xFFE88008, &fmbsea);
  661. target_write_u32(target, 0xFFE88008, fmbsea | (1 << sector));
  662. LOG_DEBUG("set fmbsea = 0x%04" PRIx32 "", fmbsea | (1 << sector));
  663. }
  664. else
  665. {
  666. target_read_u32(target, 0xFFE8800C, &fmbseb);
  667. target_write_u32(target, 0xFFE8800C, fmbseb | (1 << (sector - 16)));
  668. LOG_DEBUG("set fmbseb = 0x%04" PRIx32 "", fmbseb | (1 << (sector - 16)));
  669. }
  670. bank->sectors[sector].is_protected = 0;
  671. /*
  672. * clear status regiser, sent erase command, kickoff erase
  673. */
  674. target_write_u16(target, flashAddr, 0x0040);
  675. LOG_DEBUG("write *(uint16_t *)0x%08" PRIx32 "=0x0040", flashAddr);
  676. target_write_u16(target, flashAddr, 0x0020);
  677. LOG_DEBUG("write *(uint16_t *)0x%08" PRIx32 "=0x0020", flashAddr);
  678. target_write_u16(target, flashAddr, 0xffff);
  679. LOG_DEBUG("write *(uint16_t *)0x%08" PRIx32 "=0xffff", flashAddr);
  680. /*
  681. * Monitor FMMSTAT, busy until clear, then check and other flags for
  682. * ultimate result of the operation.
  683. */
  684. do
  685. {
  686. target_read_u32(target, 0xFFE8BC0C, &fmmstat);
  687. if (fmmstat & 0x0100)
  688. {
  689. alive_sleep(1);
  690. }
  691. }
  692. while (fmmstat & 0x0100);
  693. result = tms470_flash_status(bank);
  694. if (sector < 16)
  695. {
  696. target_write_u32(target, 0xFFE88008, fmbsea);
  697. LOG_DEBUG("set fmbsea = 0x%04" PRIx32 "", fmbsea);
  698. bank->sectors[sector].is_protected = fmbsea & (1 << sector) ? 0 : 1;
  699. }
  700. else
  701. {
  702. target_write_u32(target, 0xFFE8800C, fmbseb);
  703. LOG_DEBUG("set fmbseb = 0x%04" PRIx32 "", fmbseb);
  704. bank->sectors[sector].is_protected = fmbseb & (1 << (sector - 16)) ? 0 : 1;
  705. }
  706. target_write_u32(target, 0xFFE89C00, orig_fmregopt);
  707. LOG_DEBUG("set fmregopt = 0x%08" PRIx32 "", orig_fmregopt);
  708. target_write_u32(target, 0xFFFFFFDC, glbctrl);
  709. LOG_DEBUG("set glbctrl = 0x%08" PRIx32 "", glbctrl);
  710. if (result == ERROR_OK)
  711. {
  712. bank->sectors[sector].is_erased = 1;
  713. }
  714. return result;
  715. }
  716. /* ----------------------------------------------------------------------
  717. Implementation of Flash Driver Interfaces
  718. ---------------------------------------------------------------------- */
  719. static int tms470_register_commands(struct command_context_s *cmd_ctx)
  720. {
  721. command_t *tms470_cmd = register_command(cmd_ctx, NULL, "tms470", NULL, COMMAND_ANY, "applies to TI tms470 family");
  722. register_command(cmd_ctx, tms470_cmd, "flash_keyset", tms470_handle_flash_keyset_command, COMMAND_ANY, "tms470 flash_keyset <key0> <key1> <key2> <key3>");
  723. register_command(cmd_ctx, tms470_cmd, "osc_megahertz", tms470_handle_osc_megahertz_command, COMMAND_ANY, "tms470 osc_megahertz <MHz>");
  724. register_command(cmd_ctx, tms470_cmd, "plldis", tms470_handle_plldis_command, COMMAND_ANY, "tms470 plldis <0/1>");
  725. return ERROR_OK;
  726. }
  727. /* ---------------------------------------------------------------------- */
  728. static int tms470_erase(struct flash_bank_s *bank, int first, int last)
  729. {
  730. tms470_flash_bank_t *tms470_info = bank->driver_priv;
  731. int sector, result = ERROR_OK;
  732. if (bank->target->state != TARGET_HALTED)
  733. {
  734. LOG_ERROR("Target not halted");
  735. return ERROR_TARGET_NOT_HALTED;
  736. }
  737. tms470_read_part_info(bank);
  738. if ((first < 0) || (first >= bank->num_sectors) || (last < 0) || (last >= bank->num_sectors) || (first > last))
  739. {
  740. LOG_ERROR("Sector range %d to %d invalid.", first, last);
  741. return ERROR_FLASH_SECTOR_INVALID;
  742. }
  743. result = tms470_unlock_flash(bank);
  744. if (result != ERROR_OK)
  745. {
  746. return result;
  747. }
  748. for (sector = first; sector <= last; sector++)
  749. {
  750. LOG_INFO("Erasing tms470 bank %d sector %d...", tms470_info->ordinal, sector);
  751. result = tms470_erase_sector(bank, sector);
  752. if (result != ERROR_OK)
  753. {
  754. LOG_ERROR("tms470 could not erase flash sector.");
  755. break;
  756. }
  757. else
  758. {
  759. LOG_INFO("sector erased successfully.");
  760. }
  761. }
  762. return result;
  763. }
  764. /* ---------------------------------------------------------------------- */
  765. static int tms470_protect(struct flash_bank_s *bank, int set, int first, int last)
  766. {
  767. tms470_flash_bank_t *tms470_info = bank->driver_priv;
  768. target_t *target = bank->target;
  769. uint32_t fmmac2, fmbsea, fmbseb;
  770. int sector;
  771. if (target->state != TARGET_HALTED)
  772. {
  773. LOG_ERROR("Target not halted");
  774. return ERROR_TARGET_NOT_HALTED;
  775. }
  776. tms470_read_part_info(bank);
  777. if ((first < 0) || (first >= bank->num_sectors) || (last < 0) || (last >= bank->num_sectors) || (first > last))
  778. {
  779. LOG_ERROR("Sector range %d to %d invalid.", first, last);
  780. return ERROR_FLASH_SECTOR_INVALID;
  781. }
  782. /* enable the appropriate bank */
  783. target_read_u32(target, 0xFFE8BC04, &fmmac2);
  784. target_write_u32(target, 0xFFE8BC04, (fmmac2 & ~7) | tms470_info->ordinal);
  785. /* get the original sector proection flags for this bank */
  786. target_read_u32(target, 0xFFE88008, &fmbsea);
  787. target_read_u32(target, 0xFFE8800C, &fmbseb);
  788. for (sector = 0; sector < bank->num_sectors; sector++)
  789. {
  790. if (sector < 16)
  791. {
  792. fmbsea = set ? fmbsea & ~(1 << sector) : fmbsea | (1 << sector);
  793. bank->sectors[sector].is_protected = set ? 1 : 0;
  794. }
  795. else
  796. {
  797. fmbseb = set ? fmbseb & ~(1 << (sector - 16)) : fmbseb | (1 << (sector - 16));
  798. bank->sectors[sector].is_protected = set ? 1 : 0;
  799. }
  800. }
  801. /* update the protection bits */
  802. target_write_u32(target, 0xFFE88008, fmbsea);
  803. target_write_u32(target, 0xFFE8800C, fmbseb);
  804. return ERROR_OK;
  805. }
  806. /* ---------------------------------------------------------------------- */
  807. static int tms470_write(struct flash_bank_s *bank, uint8_t * buffer, uint32_t offset, uint32_t count)
  808. {
  809. target_t *target = bank->target;
  810. uint32_t glbctrl, fmbac2, orig_fmregopt, fmbsea, fmbseb, fmmaxpp, fmmstat;
  811. int result = ERROR_OK;
  812. uint32_t i;
  813. if (target->state != TARGET_HALTED)
  814. {
  815. LOG_ERROR("Target not halted");
  816. return ERROR_TARGET_NOT_HALTED;
  817. }
  818. tms470_read_part_info(bank);
  819. LOG_INFO("Writing %" PRId32 " bytes starting at 0x%08" PRIx32 "", count, bank->base + offset);
  820. /* set GLBCTRL.4 */
  821. target_read_u32(target, 0xFFFFFFDC, &glbctrl);
  822. target_write_u32(target, 0xFFFFFFDC, glbctrl | 0x10);
  823. (void)tms470_flash_initialize_internal_state_machine(bank);
  824. /* force max wait states */
  825. target_read_u32(target, 0xFFE88004, &fmbac2);
  826. target_write_u32(target, 0xFFE88004, fmbac2 | 0xff);
  827. /* save current access mode, force normal read mode */
  828. target_read_u32(target, 0xFFE89C00, &orig_fmregopt);
  829. target_write_u32(target, 0xFFE89C00, 0x00);
  830. /*
  831. * Disable Level 1 protection for all sectors to be erased/written.
  832. */
  833. target_read_u32(target, 0xFFE88008, &fmbsea);
  834. target_write_u32(target, 0xFFE88008, 0xffff);
  835. target_read_u32(target, 0xFFE8800C, &fmbseb);
  836. target_write_u32(target, 0xFFE8800C, 0xffff);
  837. /* read MAXPP */
  838. target_read_u32(target, 0xFFE8A07C, &fmmaxpp);
  839. for (i = 0; i < count; i += 2)
  840. {
  841. uint32_t addr = bank->base + offset + i;
  842. uint16_t word = (((uint16_t) buffer[i]) << 8) | (uint16_t) buffer[i + 1];
  843. if (word != 0xffff)
  844. {
  845. LOG_INFO("writing 0x%04x at 0x%08" PRIx32 "", word, addr);
  846. /* clear status register */
  847. target_write_u16(target, addr, 0x0040);
  848. /* program flash command */
  849. target_write_u16(target, addr, 0x0010);
  850. /* burn the 16-bit word (big-endian) */
  851. target_write_u16(target, addr, word);
  852. /*
  853. * Monitor FMMSTAT, busy until clear, then check and other flags
  854. * for ultimate result of the operation.
  855. */
  856. do
  857. {
  858. target_read_u32(target, 0xFFE8BC0C, &fmmstat);
  859. if (fmmstat & 0x0100)
  860. {
  861. alive_sleep(1);
  862. }
  863. }
  864. while (fmmstat & 0x0100);
  865. if (fmmstat & 0x3ff)
  866. {
  867. LOG_ERROR("fmstat = 0x%04" PRIx32 "", fmmstat);
  868. LOG_ERROR("Could not program word 0x%04x at address 0x%08" PRIx32 ".", word, addr);
  869. result = ERROR_FLASH_OPERATION_FAILED;
  870. break;
  871. }
  872. }
  873. else
  874. {
  875. LOG_INFO("skipping 0xffff at 0x%08" PRIx32 "", addr);
  876. }
  877. }
  878. /* restore */
  879. target_write_u32(target, 0xFFE88008, fmbsea);
  880. target_write_u32(target, 0xFFE8800C, fmbseb);
  881. target_write_u32(target, 0xFFE88004, fmbac2);
  882. target_write_u32(target, 0xFFE89C00, orig_fmregopt);
  883. target_write_u32(target, 0xFFFFFFDC, glbctrl);
  884. return result;
  885. }
  886. /* ---------------------------------------------------------------------- */
  887. static int tms470_probe(struct flash_bank_s *bank)
  888. {
  889. if (bank->target->state != TARGET_HALTED)
  890. {
  891. LOG_WARNING("Cannot communicate... target not halted.");
  892. return ERROR_TARGET_NOT_HALTED;
  893. }
  894. return tms470_read_part_info(bank);
  895. }
  896. static int tms470_auto_probe(struct flash_bank_s *bank)
  897. {
  898. tms470_flash_bank_t *tms470_info = bank->driver_priv;
  899. if (tms470_info->device_ident_reg)
  900. return ERROR_OK;
  901. return tms470_probe(bank);
  902. }
  903. /* ---------------------------------------------------------------------- */
  904. static int tms470_erase_check(struct flash_bank_s *bank)
  905. {
  906. target_t *target = bank->target;
  907. tms470_flash_bank_t *tms470_info = bank->driver_priv;
  908. int sector, result = ERROR_OK;
  909. uint32_t fmmac2, fmbac2, glbctrl, orig_fmregopt;
  910. static uint8_t buffer[64 * 1024];
  911. if (target->state != TARGET_HALTED)
  912. {
  913. LOG_ERROR("Target not halted");
  914. return ERROR_TARGET_NOT_HALTED;
  915. }
  916. if (!tms470_info->device_ident_reg)
  917. {
  918. tms470_read_part_info(bank);
  919. }
  920. /* set GLBCTRL.4 */
  921. target_read_u32(target, 0xFFFFFFDC, &glbctrl);
  922. target_write_u32(target, 0xFFFFFFDC, glbctrl | 0x10);
  923. /* save current access mode, force normal read mode */
  924. target_read_u32(target, 0xFFE89C00, &orig_fmregopt);
  925. target_write_u32(target, 0xFFE89C00, 0x00);
  926. /* enable the appropriate bank */
  927. target_read_u32(target, 0xFFE8BC04, &fmmac2);
  928. target_write_u32(target, 0xFFE8BC04, (fmmac2 & ~7) | tms470_info->ordinal);
  929. /* TCR = 0 */
  930. target_write_u32(target, 0xFFE8BC10, 0x2fc0);
  931. /* clear TEZ in fmbrdy */
  932. target_write_u32(target, 0xFFE88010, 0x0b);
  933. /* save current wait states, force max */
  934. target_read_u32(target, 0xFFE88004, &fmbac2);
  935. target_write_u32(target, 0xFFE88004, fmbac2 | 0xff);
  936. /*
  937. * The TI primitives inspect the flash memory by reading one 32-bit
  938. * word at a time. Here we read an entire sector and inspect it in
  939. * an attempt to reduce the JTAG overhead.
  940. */
  941. for (sector = 0; sector < bank->num_sectors; sector++)
  942. {
  943. if (bank->sectors[sector].is_erased != 1)
  944. {
  945. uint32_t i, addr = bank->base + bank->sectors[sector].offset;
  946. LOG_INFO("checking flash bank %d sector %d", tms470_info->ordinal, sector);
  947. target_read_buffer(target, addr, bank->sectors[sector].size, buffer);
  948. bank->sectors[sector].is_erased = 1;
  949. for (i = 0; i < bank->sectors[sector].size; i++)
  950. {
  951. if (buffer[i] != 0xff)
  952. {
  953. LOG_WARNING("tms470 bank %d, sector %d, not erased.", tms470_info->ordinal, sector);
  954. LOG_WARNING("at location 0x%08" PRIx32 ": flash data is 0x%02x.", addr + i, buffer[i]);
  955. bank->sectors[sector].is_erased = 0;
  956. break;
  957. }
  958. }
  959. }
  960. if (bank->sectors[sector].is_erased != 1)
  961. {
  962. result = ERROR_FLASH_SECTOR_NOT_ERASED;
  963. break;
  964. }
  965. else
  966. {
  967. LOG_INFO("sector erased");
  968. }
  969. }
  970. /* reset TEZ, wait states, read mode, GLBCTRL.4 */
  971. target_write_u32(target, 0xFFE88010, 0x0f);
  972. target_write_u32(target, 0xFFE88004, fmbac2);
  973. target_write_u32(target, 0xFFE89C00, orig_fmregopt);
  974. target_write_u32(target, 0xFFFFFFDC, glbctrl);
  975. return result;
  976. }
  977. /* ---------------------------------------------------------------------- */
  978. static int tms470_protect_check(struct flash_bank_s *bank)
  979. {
  980. target_t *target = bank->target;
  981. tms470_flash_bank_t *tms470_info = bank->driver_priv;
  982. int sector, result = ERROR_OK;
  983. uint32_t fmmac2, fmbsea, fmbseb;
  984. if (target->state != TARGET_HALTED)
  985. {
  986. LOG_ERROR("Target not halted");
  987. return ERROR_TARGET_NOT_HALTED;
  988. }
  989. if (!tms470_info->device_ident_reg)
  990. {
  991. tms470_read_part_info(bank);
  992. }
  993. /* enable the appropriate bank */
  994. target_read_u32(target, 0xFFE8BC04, &fmmac2);
  995. target_write_u32(target, 0xFFE8BC04, (fmmac2 & ~7) | tms470_info->ordinal);
  996. target_read_u32(target, 0xFFE88008, &fmbsea);
  997. target_read_u32(target, 0xFFE8800C, &fmbseb);
  998. for (sector = 0; sector < bank->num_sectors; sector++)
  999. {
  1000. int protected;
  1001. if (sector < 16)
  1002. {
  1003. protected = fmbsea & (1 << sector) ? 0 : 1;
  1004. bank->sectors[sector].is_protected = protected;
  1005. }
  1006. else
  1007. {
  1008. protected = fmbseb & (1 << (sector - 16)) ? 0 : 1;
  1009. bank->sectors[sector].is_protected = protected;
  1010. }
  1011. LOG_DEBUG("bank %d sector %d is %s", tms470_info->ordinal, sector, protected ? "protected" : "not protected");
  1012. }
  1013. return result;
  1014. }
  1015. /* ---------------------------------------------------------------------- */
  1016. static int tms470_info(struct flash_bank_s *bank, char *buf, int buf_size)
  1017. {
  1018. int used = 0;
  1019. tms470_flash_bank_t *tms470_info = bank->driver_priv;
  1020. if (!tms470_info->device_ident_reg)
  1021. {
  1022. tms470_read_part_info(bank);
  1023. }
  1024. if (!tms470_info->device_ident_reg)
  1025. {
  1026. (void)snprintf(buf, buf_size, "Cannot identify target as a TMS470\n");
  1027. return ERROR_FLASH_OPERATION_FAILED;
  1028. }
  1029. used += snprintf(buf, buf_size, "\ntms470 information: Chip is %s\n", tms470_info->part_name);
  1030. buf += used;
  1031. buf_size -= used;
  1032. used += snprintf(buf, buf_size, "Flash protection level 2 is %s\n", tms470_check_flash_unlocked(bank->target) == ERROR_OK ? "disabled" : "enabled");
  1033. buf += used;
  1034. buf_size -= used;
  1035. return ERROR_OK;
  1036. }
  1037. /* ---------------------------------------------------------------------- */
  1038. /*
  1039. * flash bank tms470 <base> <size> <chip_width> <bus_width> <target>
  1040. * [options...]
  1041. */
  1042. static int tms470_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
  1043. {
  1044. bank->driver_priv = malloc(sizeof(tms470_flash_bank_t));
  1045. if (!bank->driver_priv)
  1046. {
  1047. return ERROR_FLASH_OPERATION_FAILED;
  1048. }
  1049. (void)memset(bank->driver_priv, 0, sizeof(tms470_flash_bank_t));
  1050. return ERROR_OK;
  1051. }