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.
 
 
 
 
 
 

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