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.
 
 
 
 
 
 

1368 lines
36 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2006 by Magnus Lundin *
  3. * lundin@mlu.mine.nu *
  4. * *
  5. * Copyright (C) 2008 by Spencer Oliver *
  6. * spen@spen-soft.co.uk *
  7. * *
  8. * This program is free software; you can redistribute it and/or modify *
  9. * it under the terms of the GNU General Public License as published by *
  10. * the Free Software Foundation; either version 2 of the License, or *
  11. * (at your option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, *
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU General Public License for more details. *
  17. * *
  18. * You should have received a copy of the GNU General Public License *
  19. * along with this program; if not, write to the *
  20. * Free Software Foundation, Inc., *
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  22. ***************************************************************************/
  23. /***************************************************************************
  24. * STELLARIS flash is tested on LM3S811, LM3S6965, LM3s3748, more.
  25. ***************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #include "imp.h"
  30. #include <target/algorithm.h>
  31. #include <target/armv7m.h>
  32. #define DID0_VER(did0) ((did0 >> 28)&0x07)
  33. /* STELLARIS control registers */
  34. #define SCB_BASE 0x400FE000
  35. #define DID0 0x000
  36. #define DID1 0x004
  37. #define DC0 0x008
  38. #define DC1 0x010
  39. #define DC2 0x014
  40. #define DC3 0x018
  41. #define DC4 0x01C
  42. #define RIS 0x050
  43. #define RCC 0x060
  44. #define PLLCFG 0x064
  45. #define RCC2 0x070
  46. #define NVMSTAT 0x1a0
  47. /* "legacy" flash memory protection registers (64KB max) */
  48. #define FMPRE 0x130
  49. #define FMPPE 0x134
  50. /* new flash memory protection registers (for more than 64KB) */
  51. #define FMPRE0 0x200 /* PRE1 = PRE0 + 4, etc */
  52. #define FMPPE0 0x400 /* PPE1 = PPE0 + 4, etc */
  53. #define USECRL 0x140
  54. #define FLASH_CONTROL_BASE 0x400FD000
  55. #define FLASH_FMA (FLASH_CONTROL_BASE | 0x000)
  56. #define FLASH_FMD (FLASH_CONTROL_BASE | 0x004)
  57. #define FLASH_FMC (FLASH_CONTROL_BASE | 0x008)
  58. #define FLASH_CRIS (FLASH_CONTROL_BASE | 0x00C)
  59. #define FLASH_CIM (FLASH_CONTROL_BASE | 0x010)
  60. #define FLASH_MISC (FLASH_CONTROL_BASE | 0x014)
  61. #define AMISC 1
  62. #define PMISC 2
  63. #define AMASK 1
  64. #define PMASK 2
  65. /* Flash Controller Command bits */
  66. #define FMC_WRKEY (0xA442 << 16)
  67. #define FMC_COMT (1 << 3)
  68. #define FMC_MERASE (1 << 2)
  69. #define FMC_ERASE (1 << 1)
  70. #define FMC_WRITE (1 << 0)
  71. /* STELLARIS constants */
  72. /* values to write in FMA to commit write-"once" values */
  73. #define FLASH_FMA_PRE(x) (2 * (x)) /* for FMPPREx */
  74. #define FLASH_FMA_PPE(x) (2 * (x) + 1) /* for FMPPPEx */
  75. static void stellaris_read_clock_info(struct flash_bank *bank);
  76. static int stellaris_mass_erase(struct flash_bank *bank);
  77. struct stellaris_flash_bank
  78. {
  79. /* chip id register */
  80. uint32_t did0;
  81. uint32_t did1;
  82. uint32_t dc0;
  83. uint32_t dc1;
  84. const char * target_name;
  85. uint32_t sramsiz;
  86. uint32_t flshsz;
  87. /* flash geometry */
  88. uint32_t num_pages;
  89. uint32_t pagesize;
  90. uint32_t pages_in_lockregion;
  91. /* nv memory bits */
  92. uint16_t num_lockbits;
  93. /* main clock status */
  94. uint32_t rcc;
  95. uint32_t rcc2;
  96. uint8_t mck_valid;
  97. uint8_t xtal_mask;
  98. uint32_t iosc_freq;
  99. uint32_t mck_freq;
  100. const char *iosc_desc;
  101. const char *mck_desc;
  102. };
  103. // Autogenerated by contrib/gen-stellaris-part-header.pl
  104. // From Stellaris Firmware Development Package revision 6734
  105. static struct {
  106. uint32_t partno;
  107. const char *partname;
  108. } StellarisParts[] =
  109. {
  110. {0x0001,"LM3S101"},
  111. {0x0002,"LM3S102"},
  112. {0x0019,"LM3S300"},
  113. {0x0011,"LM3S301"},
  114. {0x001A,"LM3S308"},
  115. {0x0012,"LM3S310"},
  116. {0x0013,"LM3S315"},
  117. {0x0014,"LM3S316"},
  118. {0x0017,"LM3S317"},
  119. {0x0015,"LM3S328"},
  120. {0x002A,"LM3S600"},
  121. {0x0021,"LM3S601"},
  122. {0x002B,"LM3S608"},
  123. {0x0022,"LM3S610"},
  124. {0x0023,"LM3S611"},
  125. {0x0024,"LM3S612"},
  126. {0x0025,"LM3S613"},
  127. {0x0026,"LM3S615"},
  128. {0x0028,"LM3S617"},
  129. {0x0029,"LM3S618"},
  130. {0x0027,"LM3S628"},
  131. {0x0038,"LM3S800"},
  132. {0x0031,"LM3S801"},
  133. {0x0039,"LM3S808"},
  134. {0x0032,"LM3S811"},
  135. {0x0033,"LM3S812"},
  136. {0x0034,"LM3S815"},
  137. {0x0036,"LM3S817"},
  138. {0x0037,"LM3S818"},
  139. {0x0035,"LM3S828"},
  140. {0x10BF,"LM3S1110"},
  141. {0x10C3,"LM3S1133"},
  142. {0x10C5,"LM3S1138"},
  143. {0x10C1,"LM3S1150"},
  144. {0x10C4,"LM3S1162"},
  145. {0x10C2,"LM3S1165"},
  146. {0x10C6,"LM3S1332"},
  147. {0x10BC,"LM3S1435"},
  148. {0x10BA,"LM3S1439"},
  149. {0x10BB,"LM3S1512"},
  150. {0x10C7,"LM3S1538"},
  151. {0x10DB,"LM3S1601"},
  152. {0x1006,"LM3S1607"},
  153. {0x10DA,"LM3S1608"},
  154. {0x10C0,"LM3S1620"},
  155. {0x1003,"LM3S1625"},
  156. {0x1004,"LM3S1626"},
  157. {0x1005,"LM3S1627"},
  158. {0x10B3,"LM3S1635"},
  159. {0x10BD,"LM3S1637"},
  160. {0x10B1,"LM3S1651"},
  161. {0x10B9,"LM3S1751"},
  162. {0x1010,"LM3S1776"},
  163. {0x1016,"LM3S1811"},
  164. {0x103D,"LM3S1816"},
  165. {0x10B4,"LM3S1850"},
  166. {0x10DD,"LM3S1911"},
  167. {0x10DC,"LM3S1918"},
  168. {0x10B7,"LM3S1937"},
  169. {0x10BE,"LM3S1958"},
  170. {0x10B5,"LM3S1960"},
  171. {0x10B8,"LM3S1968"},
  172. {0x100F,"LM3S1J11"},
  173. {0x103C,"LM3S1J16"},
  174. {0x100E,"LM3S1N11"},
  175. {0x103B,"LM3S1N16"},
  176. {0x10B2,"LM3S1P51"},
  177. {0x109E,"LM3S1R21"},
  178. {0x1030,"LM3S1W16"},
  179. {0x102F,"LM3S1Z16"},
  180. {0x1051,"LM3S2110"},
  181. {0x1084,"LM3S2139"},
  182. {0x1039,"LM3S2276"},
  183. {0x10A2,"LM3S2410"},
  184. {0x1059,"LM3S2412"},
  185. {0x1056,"LM3S2432"},
  186. {0x105A,"LM3S2533"},
  187. {0x10E1,"LM3S2601"},
  188. {0x10E0,"LM3S2608"},
  189. {0x1033,"LM3S2616"},
  190. {0x1057,"LM3S2620"},
  191. {0x1085,"LM3S2637"},
  192. {0x1053,"LM3S2651"},
  193. {0x1080,"LM3S2671"},
  194. {0x1050,"LM3S2678"},
  195. {0x10A4,"LM3S2730"},
  196. {0x1052,"LM3S2739"},
  197. {0x103A,"LM3S2776"},
  198. {0x106D,"LM3S2793"},
  199. {0x10E3,"LM3S2911"},
  200. {0x10E2,"LM3S2918"},
  201. {0x1054,"LM3S2939"},
  202. {0x108F,"LM3S2948"},
  203. {0x1058,"LM3S2950"},
  204. {0x1055,"LM3S2965"},
  205. {0x106C,"LM3S2B93"},
  206. {0x1008,"LM3S3634"},
  207. {0x1043,"LM3S3651"},
  208. {0x1044,"LM3S3739"},
  209. {0x1049,"LM3S3748"},
  210. {0x1045,"LM3S3749"},
  211. {0x1042,"LM3S3826"},
  212. {0x1041,"LM3S3J26"},
  213. {0x1040,"LM3S3N26"},
  214. {0x103F,"LM3S3W26"},
  215. {0x103E,"LM3S3Z26"},
  216. {0x1081,"LM3S5632"},
  217. {0x100C,"LM3S5651"},
  218. {0x108A,"LM3S5652"},
  219. {0x104D,"LM3S5656"},
  220. {0x1091,"LM3S5662"},
  221. {0x1096,"LM3S5732"},
  222. {0x1097,"LM3S5737"},
  223. {0x10A0,"LM3S5739"},
  224. {0x1099,"LM3S5747"},
  225. {0x10A7,"LM3S5749"},
  226. {0x109A,"LM3S5752"},
  227. {0x109C,"LM3S5762"},
  228. {0x1069,"LM3S5791"},
  229. {0x100B,"LM3S5951"},
  230. {0x104E,"LM3S5956"},
  231. {0x1068,"LM3S5B91"},
  232. {0x1009,"LM3S5K31"},
  233. {0x104A,"LM3S5K36"},
  234. {0x100A,"LM3S5P31"},
  235. {0x1048,"LM3S5P36"},
  236. {0x100D,"LM3S5P51"},
  237. {0x104C,"LM3S5P56"},
  238. {0x1007,"LM3S5R31"},
  239. {0x104B,"LM3S5R36"},
  240. {0x1047,"LM3S5T36"},
  241. {0x1046,"LM3S5Y36"},
  242. {0x10A1,"LM3S6100"},
  243. {0x1074,"LM3S6110"},
  244. {0x10A5,"LM3S6420"},
  245. {0x1082,"LM3S6422"},
  246. {0x1075,"LM3S6432"},
  247. {0x1076,"LM3S6537"},
  248. {0x1071,"LM3S6610"},
  249. {0x10E7,"LM3S6611"},
  250. {0x10E6,"LM3S6618"},
  251. {0x1083,"LM3S6633"},
  252. {0x108B,"LM3S6637"},
  253. {0x10A3,"LM3S6730"},
  254. {0x1077,"LM3S6753"},
  255. {0x10E9,"LM3S6911"},
  256. {0x10E8,"LM3S6918"},
  257. {0x1089,"LM3S6938"},
  258. {0x1072,"LM3S6950"},
  259. {0x1078,"LM3S6952"},
  260. {0x1073,"LM3S6965"},
  261. {0x1064,"LM3S8530"},
  262. {0x108E,"LM3S8538"},
  263. {0x1061,"LM3S8630"},
  264. {0x1063,"LM3S8730"},
  265. {0x108D,"LM3S8733"},
  266. {0x1086,"LM3S8738"},
  267. {0x1065,"LM3S8930"},
  268. {0x108C,"LM3S8933"},
  269. {0x1088,"LM3S8938"},
  270. {0x10A6,"LM3S8962"},
  271. {0x1062,"LM3S8970"},
  272. {0x10D7,"LM3S8971"},
  273. {0x1067,"LM3S9790"},
  274. {0x106B,"LM3S9792"},
  275. {0x1020,"LM3S9997"},
  276. {0x1066,"LM3S9B90"},
  277. {0x106A,"LM3S9B92"},
  278. {0x106E,"LM3S9B95"},
  279. {0x106F,"LM3S9B96"},
  280. {0x1018,"LM3S9L97"},
  281. {0,"Unknown part"}
  282. };
  283. static char * StellarisClassname[5] =
  284. {
  285. "Sandstorm",
  286. "Fury",
  287. "Unknown",
  288. "DustDevil",
  289. "Tempest"
  290. };
  291. /***************************************************************************
  292. * openocd command interface *
  293. ***************************************************************************/
  294. /* flash_bank stellaris <base> <size> 0 0 <target#>
  295. */
  296. FLASH_BANK_COMMAND_HANDLER(stellaris_flash_bank_command)
  297. {
  298. struct stellaris_flash_bank *stellaris_info;
  299. if (CMD_ARGC < 6)
  300. {
  301. LOG_WARNING("incomplete flash_bank stellaris configuration");
  302. return ERROR_FLASH_BANK_INVALID;
  303. }
  304. stellaris_info = calloc(sizeof(struct stellaris_flash_bank), 1);
  305. bank->base = 0x0;
  306. bank->driver_priv = stellaris_info;
  307. stellaris_info->target_name = "Unknown target";
  308. /* part wasn't probed for info yet */
  309. stellaris_info->did1 = 0;
  310. /* TODO Specify the main crystal speed in kHz using an optional
  311. * argument; ditto, the speed of an external oscillator used
  312. * instead of a crystal. Avoid programming flash using IOSC.
  313. */
  314. return ERROR_OK;
  315. }
  316. static int get_stellaris_info(struct flash_bank *bank, char *buf, int buf_size)
  317. {
  318. int printed, device_class;
  319. struct stellaris_flash_bank *stellaris_info = bank->driver_priv;
  320. if (stellaris_info->did1 == 0)
  321. return ERROR_FLASH_BANK_NOT_PROBED;
  322. /* Read main and master clock freqency register */
  323. stellaris_read_clock_info(bank);
  324. if (DID0_VER(stellaris_info->did0) > 0)
  325. {
  326. device_class = (stellaris_info->did0 >> 16) & 0xFF;
  327. }
  328. else
  329. {
  330. device_class = 0;
  331. }
  332. printed = snprintf(buf,
  333. buf_size,
  334. "\nTI/LMI Stellaris information: Chip is "
  335. "class %i (%s) %s rev %c%i\n",
  336. device_class,
  337. StellarisClassname[device_class],
  338. stellaris_info->target_name,
  339. (int)('A' + ((stellaris_info->did0 >> 8) & 0xFF)),
  340. (int)((stellaris_info->did0) & 0xFF));
  341. buf += printed;
  342. buf_size -= printed;
  343. printed = snprintf(buf,
  344. buf_size,
  345. "did1: 0x%8.8" PRIx32 ", arch: 0x%4.4" PRIx32
  346. ", eproc: %s, ramsize: %ik, flashsize: %ik\n",
  347. stellaris_info->did1,
  348. stellaris_info->did1,
  349. "ARMv7M",
  350. (int)((1 + ((stellaris_info->dc0 >> 16) & 0xFFFF))/4),
  351. (int)((1 + (stellaris_info->dc0 & 0xFFFF))*2));
  352. buf += printed;
  353. buf_size -= printed;
  354. printed = snprintf(buf,
  355. buf_size,
  356. "master clock: %ikHz%s, "
  357. "rcc is 0x%" PRIx32 ", rcc2 is 0x%" PRIx32 "\n",
  358. (int)(stellaris_info->mck_freq / 1000),
  359. stellaris_info->mck_desc,
  360. stellaris_info->rcc,
  361. stellaris_info->rcc2);
  362. buf += printed;
  363. buf_size -= printed;
  364. if (stellaris_info->num_lockbits > 0)
  365. {
  366. printed = snprintf(buf,
  367. buf_size,
  368. "pagesize: %" PRIi32 ", pages: %d, "
  369. "lockbits: %i, pages per lockbit: %i\n",
  370. stellaris_info->pagesize,
  371. (unsigned) stellaris_info->num_pages,
  372. stellaris_info->num_lockbits,
  373. (unsigned) stellaris_info->pages_in_lockregion);
  374. buf += printed;
  375. buf_size -= printed;
  376. }
  377. return ERROR_OK;
  378. }
  379. /***************************************************************************
  380. * chip identification and status *
  381. ***************************************************************************/
  382. /* Set the flash timimg register to match current clocking */
  383. static void stellaris_set_flash_timing(struct flash_bank *bank)
  384. {
  385. struct stellaris_flash_bank *stellaris_info = bank->driver_priv;
  386. struct target *target = bank->target;
  387. uint32_t usecrl = (stellaris_info->mck_freq/1000000ul-1);
  388. LOG_DEBUG("usecrl = %i",(int)(usecrl));
  389. target_write_u32(target, SCB_BASE | USECRL, usecrl);
  390. }
  391. static const unsigned rcc_xtal[32] = {
  392. [0x00] = 1000000, /* no pll */
  393. [0x01] = 1843200, /* no pll */
  394. [0x02] = 2000000, /* no pll */
  395. [0x03] = 2457600, /* no pll */
  396. [0x04] = 3579545,
  397. [0x05] = 3686400,
  398. [0x06] = 4000000, /* usb */
  399. [0x07] = 4096000,
  400. [0x08] = 4915200,
  401. [0x09] = 5000000, /* usb */
  402. [0x0a] = 5120000,
  403. [0x0b] = 6000000, /* (reset) usb */
  404. [0x0c] = 6144000,
  405. [0x0d] = 7372800,
  406. [0x0e] = 8000000, /* usb */
  407. [0x0f] = 8192000,
  408. /* parts before DustDevil use just 4 bits for xtal spec */
  409. [0x10] = 10000000, /* usb */
  410. [0x11] = 12000000, /* usb */
  411. [0x12] = 12288000,
  412. [0x13] = 13560000,
  413. [0x14] = 14318180,
  414. [0x15] = 16000000, /* usb */
  415. [0x16] = 16384000,
  416. };
  417. /** Read clock configuration and set stellaris_info->usec_clocks. */
  418. static void stellaris_read_clock_info(struct flash_bank *bank)
  419. {
  420. struct stellaris_flash_bank *stellaris_info = bank->driver_priv;
  421. struct target *target = bank->target;
  422. uint32_t rcc, rcc2, pllcfg, sysdiv, usesysdiv, bypass, oscsrc;
  423. unsigned xtal;
  424. unsigned long mainfreq;
  425. target_read_u32(target, SCB_BASE | RCC, &rcc);
  426. LOG_DEBUG("Stellaris RCC %" PRIx32 "", rcc);
  427. target_read_u32(target, SCB_BASE | RCC2, &rcc2);
  428. LOG_DEBUG("Stellaris RCC2 %" PRIx32 "", rcc);
  429. target_read_u32(target, SCB_BASE | PLLCFG, &pllcfg);
  430. LOG_DEBUG("Stellaris PLLCFG %" PRIx32 "", pllcfg);
  431. stellaris_info->rcc = rcc;
  432. stellaris_info->rcc = rcc2;
  433. sysdiv = (rcc >> 23) & 0xF;
  434. usesysdiv = (rcc >> 22) & 0x1;
  435. bypass = (rcc >> 11) & 0x1;
  436. oscsrc = (rcc >> 4) & 0x3;
  437. xtal = (rcc >> 6) & stellaris_info->xtal_mask;
  438. /* NOTE: post-Sandstorm parts have RCC2 which may override
  439. * parts of RCC ... with more sysdiv options, option for
  440. * 32768 Hz mainfreq, PLL controls. On Sandstorm it reads
  441. * as zero, so the "use RCC2" flag is always clear.
  442. */
  443. if (rcc2 & (1 << 31)) {
  444. sysdiv = (rcc2 >> 23) & 0x3F;
  445. bypass = (rcc2 >> 11) & 0x1;
  446. oscsrc = (rcc2 >> 4) & 0x7;
  447. /* FIXME Tempest parts have an additional lsb for
  448. * fractional sysdiv (200 MHz / 2.5 == 80 MHz)
  449. */
  450. }
  451. stellaris_info->mck_desc = "";
  452. switch (oscsrc)
  453. {
  454. case 0: /* MOSC */
  455. mainfreq = rcc_xtal[xtal];
  456. break;
  457. case 1: /* IOSC */
  458. mainfreq = stellaris_info->iosc_freq;
  459. stellaris_info->mck_desc = stellaris_info->iosc_desc;
  460. break;
  461. case 2: /* IOSC/4 */
  462. mainfreq = stellaris_info->iosc_freq / 4;
  463. stellaris_info->mck_desc = stellaris_info->iosc_desc;
  464. break;
  465. case 3: /* lowspeed */
  466. /* Sandstorm doesn't have this 30K +/- 30% osc */
  467. mainfreq = 30000;
  468. stellaris_info->mck_desc = " (±30%)";
  469. break;
  470. case 8: /* hibernation osc */
  471. /* not all parts support hibernation */
  472. mainfreq = 32768;
  473. break;
  474. default: /* NOTREACHED */
  475. mainfreq = 0;
  476. break;
  477. }
  478. /* PLL is used if it's not bypassed; its output is 200 MHz
  479. * even when it runs at 400 MHz (adds divide-by-two stage).
  480. */
  481. if (!bypass)
  482. mainfreq = 200000000;
  483. if (usesysdiv)
  484. stellaris_info->mck_freq = mainfreq/(1 + sysdiv);
  485. else
  486. stellaris_info->mck_freq = mainfreq;
  487. }
  488. /* Read device id register, main clock frequency register and fill in driver info structure */
  489. static int stellaris_read_part_info(struct flash_bank *bank)
  490. {
  491. struct stellaris_flash_bank *stellaris_info = bank->driver_priv;
  492. struct target *target = bank->target;
  493. uint32_t did0, did1, ver, fam;
  494. int i;
  495. /* Read and parse chip identification register */
  496. target_read_u32(target, SCB_BASE | DID0, &did0);
  497. target_read_u32(target, SCB_BASE | DID1, &did1);
  498. target_read_u32(target, SCB_BASE | DC0, &stellaris_info->dc0);
  499. target_read_u32(target, SCB_BASE | DC1, &stellaris_info->dc1);
  500. LOG_DEBUG("did0 0x%" PRIx32 ", did1 0x%" PRIx32 ", dc0 0x%" PRIx32 ", dc1 0x%" PRIx32 "",
  501. did0, did1, stellaris_info->dc0, stellaris_info->dc1);
  502. ver = did0 >> 28;
  503. if ((ver != 0) && (ver != 1))
  504. {
  505. LOG_WARNING("Unknown did0 version, cannot identify target");
  506. return ERROR_FLASH_OPERATION_FAILED;
  507. }
  508. if (did1 == 0)
  509. {
  510. LOG_WARNING("Cannot identify target as a Stellaris");
  511. return ERROR_FLASH_OPERATION_FAILED;
  512. }
  513. ver = did1 >> 28;
  514. fam = (did1 >> 24) & 0xF;
  515. if (((ver != 0) && (ver != 1)) || (fam != 0))
  516. {
  517. LOG_WARNING("Unknown did1 version/family.");
  518. return ERROR_FLASH_OPERATION_FAILED;
  519. }
  520. /* For Sandstorm, Fury, DustDevil: current data sheets say IOSC
  521. * is 12 MHz, but some older parts have 15 MHz. A few data sheets
  522. * even give _both_ numbers! We'll use current numbers; IOSC is
  523. * always approximate.
  524. *
  525. * For Tempest: IOSC is calibrated, 16 MHz
  526. */
  527. stellaris_info->iosc_freq = 12000000;
  528. stellaris_info->iosc_desc = " (±30%)";
  529. stellaris_info->xtal_mask = 0x0f;
  530. switch ((did0 >> 28) & 0x7) {
  531. case 0: /* Sandstorm */
  532. /*
  533. * Current (2009-August) parts seem to be rev C2 and use 12 MHz.
  534. * Parts before rev C0 used 15 MHz; some C0 parts use 15 MHz
  535. * (LM3S618), but some other C0 parts are 12 MHz (LM3S811).
  536. */
  537. if (((did0 >> 8) & 0xff) < 2) {
  538. stellaris_info->iosc_freq = 15000000;
  539. stellaris_info->iosc_desc = " (±50%)";
  540. }
  541. break;
  542. case 1:
  543. switch ((did0 >> 16) & 0xff) {
  544. case 1: /* Fury */
  545. break;
  546. case 4: /* Tempest */
  547. stellaris_info->iosc_freq = 16000000; /* +/- 1% */
  548. stellaris_info->iosc_desc = " (±1%)";
  549. /* FALL THROUGH */
  550. case 3: /* DustDevil */
  551. stellaris_info->xtal_mask = 0x1f;
  552. break;
  553. default:
  554. LOG_WARNING("Unknown did0 class");
  555. }
  556. break;
  557. default:
  558. LOG_WARNING("Unknown did0 version");
  559. break;
  560. }
  561. for (i = 0; StellarisParts[i].partno; i++)
  562. {
  563. if (StellarisParts[i].partno == ((did1 >> 16) & 0xFFFF))
  564. break;
  565. }
  566. stellaris_info->target_name = StellarisParts[i].partname;
  567. stellaris_info->did0 = did0;
  568. stellaris_info->did1 = did1;
  569. stellaris_info->num_lockbits = 1 + (stellaris_info->dc0 & 0xFFFF);
  570. stellaris_info->num_pages = 2 *(1 + (stellaris_info->dc0 & 0xFFFF));
  571. stellaris_info->pagesize = 1024;
  572. stellaris_info->pages_in_lockregion = 2;
  573. /* REVISIT for at least Tempest parts, read NVMSTAT.FWB too.
  574. * That exposes a 32-word Flash Write Buffer ... enabling
  575. * writes of more than one word at a time.
  576. */
  577. return ERROR_OK;
  578. }
  579. /***************************************************************************
  580. * flash operations *
  581. ***************************************************************************/
  582. static int stellaris_protect_check(struct flash_bank *bank)
  583. {
  584. struct stellaris_flash_bank *stellaris = bank->driver_priv;
  585. int status = ERROR_OK;
  586. unsigned i;
  587. unsigned page;
  588. if (stellaris->did1 == 0)
  589. return ERROR_FLASH_BANK_NOT_PROBED;
  590. for (i = 0; i < (unsigned) bank->num_sectors; i++)
  591. bank->sectors[i].is_protected = -1;
  592. /* Read each Flash Memory Protection Program Enable (FMPPE) register
  593. * to report any pages that we can't write. Ignore the Read Enable
  594. * register (FMPRE).
  595. */
  596. for (i = 0, page = 0;
  597. i < DIV_ROUND_UP(stellaris->num_lockbits, 32u);
  598. i++) {
  599. uint32_t lockbits;
  600. status = target_read_u32(bank->target,
  601. SCB_BASE + (i ? (FMPPE0 + 4 * i) : FMPPE),
  602. &lockbits);
  603. LOG_DEBUG("FMPPE%d = %#8.8x (status %d)", i,
  604. (unsigned) lockbits, status);
  605. if (status != ERROR_OK)
  606. goto done;
  607. for (unsigned j = 0; j < 32; j++) {
  608. unsigned k;
  609. for (k = 0; k < stellaris->pages_in_lockregion; k++) {
  610. if (page >= (unsigned) bank->num_sectors)
  611. goto done;
  612. bank->sectors[page++].is_protected =
  613. !(lockbits & (1 << j));
  614. }
  615. }
  616. }
  617. done:
  618. return status;
  619. }
  620. static int stellaris_erase(struct flash_bank *bank, int first, int last)
  621. {
  622. int banknr;
  623. uint32_t flash_fmc, flash_cris;
  624. struct stellaris_flash_bank *stellaris_info = bank->driver_priv;
  625. struct target *target = bank->target;
  626. if (bank->target->state != TARGET_HALTED)
  627. {
  628. LOG_ERROR("Target not halted");
  629. return ERROR_TARGET_NOT_HALTED;
  630. }
  631. if (stellaris_info->did1 == 0)
  632. return ERROR_FLASH_BANK_NOT_PROBED;
  633. if ((first < 0) || (last < first) || (last >= (int)stellaris_info->num_pages))
  634. {
  635. return ERROR_FLASH_SECTOR_INVALID;
  636. }
  637. if ((first == 0) && (last == ((int)stellaris_info->num_pages-1)))
  638. {
  639. return stellaris_mass_erase(bank);
  640. }
  641. /* Refresh flash controller timing */
  642. stellaris_read_clock_info(bank);
  643. stellaris_set_flash_timing(bank);
  644. /* Clear and disable flash programming interrupts */
  645. target_write_u32(target, FLASH_CIM, 0);
  646. target_write_u32(target, FLASH_MISC, PMISC | AMISC);
  647. /* REVISIT this clobbers state set by any halted firmware ...
  648. * it might want to process those IRQs.
  649. */
  650. for (banknr = first; banknr <= last; banknr++)
  651. {
  652. /* Address is first word in page */
  653. target_write_u32(target, FLASH_FMA, banknr * stellaris_info->pagesize);
  654. /* Write erase command */
  655. target_write_u32(target, FLASH_FMC, FMC_WRKEY | FMC_ERASE);
  656. /* Wait until erase complete */
  657. do
  658. {
  659. target_read_u32(target, FLASH_FMC, &flash_fmc);
  660. }
  661. while (flash_fmc & FMC_ERASE);
  662. /* Check acess violations */
  663. target_read_u32(target, FLASH_CRIS, &flash_cris);
  664. if (flash_cris & (AMASK))
  665. {
  666. LOG_WARNING("Error erasing flash page %i, flash_cris 0x%" PRIx32 "", banknr, flash_cris);
  667. target_write_u32(target, FLASH_CRIS, 0);
  668. return ERROR_FLASH_OPERATION_FAILED;
  669. }
  670. bank->sectors[banknr].is_erased = 1;
  671. }
  672. return ERROR_OK;
  673. }
  674. static int stellaris_protect(struct flash_bank *bank, int set, int first, int last)
  675. {
  676. uint32_t fmppe, flash_fmc, flash_cris;
  677. int lockregion;
  678. struct stellaris_flash_bank *stellaris_info = bank->driver_priv;
  679. struct target *target = bank->target;
  680. if (bank->target->state != TARGET_HALTED)
  681. {
  682. LOG_ERROR("Target not halted");
  683. return ERROR_TARGET_NOT_HALTED;
  684. }
  685. if (!set)
  686. {
  687. LOG_ERROR("Hardware doesn't suppport page-level unprotect. "
  688. "Try the 'recover' command.");
  689. return ERROR_INVALID_ARGUMENTS;
  690. }
  691. if (stellaris_info->did1 == 0)
  692. return ERROR_FLASH_BANK_NOT_PROBED;
  693. /* lockregions are 2 pages ... must protect [even..odd] */
  694. if ((first < 0) || (first & 1)
  695. || (last < first) || !(last & 1)
  696. || (last >= 2 * stellaris_info->num_lockbits))
  697. {
  698. LOG_ERROR("Can't protect unaligned or out-of-range pages.");
  699. return ERROR_FLASH_SECTOR_INVALID;
  700. }
  701. /* Refresh flash controller timing */
  702. stellaris_read_clock_info(bank);
  703. stellaris_set_flash_timing(bank);
  704. /* convert from pages to lockregions */
  705. first /= 2;
  706. last /= 2;
  707. /* FIXME this assumes single FMPPE, for a max of 64K of flash!!
  708. * Current parts can be much bigger.
  709. */
  710. if (last >= 32) {
  711. LOG_ERROR("No support yet for protection > 64K");
  712. return ERROR_FLASH_OPERATION_FAILED;
  713. }
  714. target_read_u32(target, SCB_BASE | FMPPE, &fmppe);
  715. for (lockregion = first; lockregion <= last; lockregion++)
  716. fmppe &= ~(1 << lockregion);
  717. /* Clear and disable flash programming interrupts */
  718. target_write_u32(target, FLASH_CIM, 0);
  719. target_write_u32(target, FLASH_MISC, PMISC | AMISC);
  720. /* REVISIT this clobbers state set by any halted firmware ...
  721. * it might want to process those IRQs.
  722. */
  723. LOG_DEBUG("fmppe 0x%" PRIx32 "",fmppe);
  724. target_write_u32(target, SCB_BASE | FMPPE, fmppe);
  725. /* Commit FMPPE */
  726. target_write_u32(target, FLASH_FMA, 1);
  727. /* Write commit command */
  728. /* REVISIT safety check, since this cannot be undone
  729. * except by the "Recover a locked device" procedure.
  730. * REVISIT DustDevil-A0 parts have an erratum making FMPPE commits
  731. * inadvisable ... it makes future mass erase operations fail.
  732. */
  733. LOG_WARNING("Flash protection cannot be removed once commited, commit is NOT executed !");
  734. /* target_write_u32(target, FLASH_FMC, FMC_WRKEY | FMC_COMT); */
  735. /* Wait until erase complete */
  736. do
  737. {
  738. target_read_u32(target, FLASH_FMC, &flash_fmc);
  739. }
  740. while (flash_fmc & FMC_COMT);
  741. /* Check acess violations */
  742. target_read_u32(target, FLASH_CRIS, &flash_cris);
  743. if (flash_cris & (AMASK))
  744. {
  745. LOG_WARNING("Error setting flash page protection, flash_cris 0x%" PRIx32 "", flash_cris);
  746. target_write_u32(target, FLASH_CRIS, 0);
  747. return ERROR_FLASH_OPERATION_FAILED;
  748. }
  749. return ERROR_OK;
  750. }
  751. /* see contib/loaders/flash/stellaris.s for src */
  752. static const uint8_t stellaris_write_code[] =
  753. {
  754. /*
  755. Call with :
  756. r0 = buffer address
  757. r1 = destination address
  758. r2 = bytecount (in) - endaddr (work)
  759. Used registers:
  760. r3 = pFLASH_CTRL_BASE
  761. r4 = FLASHWRITECMD
  762. r5 = #1
  763. r6 = bytes written
  764. r7 = temp reg
  765. */
  766. 0x07,0x4B, /* ldr r3,pFLASH_CTRL_BASE */
  767. 0x08,0x4C, /* ldr r4,FLASHWRITECMD */
  768. 0x01,0x25, /* movs r5, 1 */
  769. 0x00,0x26, /* movs r6, #0 */
  770. /* mainloop: */
  771. 0x19,0x60, /* str r1, [r3, #0] */
  772. 0x87,0x59, /* ldr r7, [r0, r6] */
  773. 0x5F,0x60, /* str r7, [r3, #4] */
  774. 0x9C,0x60, /* str r4, [r3, #8] */
  775. /* waitloop: */
  776. 0x9F,0x68, /* ldr r7, [r3, #8] */
  777. 0x2F,0x42, /* tst r7, r5 */
  778. 0xFC,0xD1, /* bne waitloop */
  779. 0x04,0x31, /* adds r1, r1, #4 */
  780. 0x04,0x36, /* adds r6, r6, #4 */
  781. 0x96,0x42, /* cmp r6, r2 */
  782. 0xF4,0xD1, /* bne mainloop */
  783. 0x00,0xBE, /* bkpt #0 */
  784. /* pFLASH_CTRL_BASE: */
  785. 0x00,0xD0,0x0F,0x40, /* .word 0x400FD000 */
  786. /* FLASHWRITECMD: */
  787. 0x01,0x00,0x42,0xA4 /* .word 0xA4420001 */
  788. };
  789. static int stellaris_write_block(struct flash_bank *bank,
  790. uint8_t *buffer, uint32_t offset, uint32_t wcount)
  791. {
  792. struct target *target = bank->target;
  793. uint32_t buffer_size = 16384;
  794. struct working_area *source;
  795. struct working_area *write_algorithm;
  796. uint32_t address = bank->base + offset;
  797. struct reg_param reg_params[3];
  798. struct armv7m_algorithm armv7m_info;
  799. int retval = ERROR_OK;
  800. /* power of two, and multiple of word size */
  801. static const unsigned buf_min = 128;
  802. /* for small buffers it's faster not to download an algorithm */
  803. if (wcount * 4 < buf_min)
  804. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  805. LOG_DEBUG("(bank=%p buffer=%p offset=%08" PRIx32 " wcount=%08" PRIx32 "",
  806. bank, buffer, offset, wcount);
  807. /* flash write code */
  808. if (target_alloc_working_area(target, sizeof(stellaris_write_code), &write_algorithm) != ERROR_OK)
  809. {
  810. LOG_DEBUG("no working area for block memory writes");
  811. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  812. };
  813. /* plus a buffer big enough for this data */
  814. if (wcount * 4 < buffer_size)
  815. buffer_size = wcount * 4;
  816. /* memory buffer */
  817. while (target_alloc_working_area_try(target, buffer_size, &source) != ERROR_OK)
  818. {
  819. buffer_size /= 2;
  820. if (buffer_size <= buf_min)
  821. {
  822. target_free_working_area(target, write_algorithm);
  823. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  824. }
  825. LOG_DEBUG("retry target_alloc_working_area(%s, size=%u)",
  826. target_name(target), (unsigned) buffer_size);
  827. };
  828. retval = target_write_buffer(target, write_algorithm->address,
  829. sizeof(stellaris_write_code),
  830. (uint8_t *) stellaris_write_code);
  831. armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
  832. armv7m_info.core_mode = ARMV7M_MODE_ANY;
  833. init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
  834. init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
  835. init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT);
  836. while (wcount > 0)
  837. {
  838. uint32_t thisrun_count = (wcount > (buffer_size / 4)) ? (buffer_size / 4) : wcount;
  839. target_write_buffer(target, source->address, thisrun_count * 4, buffer);
  840. buf_set_u32(reg_params[0].value, 0, 32, source->address);
  841. buf_set_u32(reg_params[1].value, 0, 32, address);
  842. buf_set_u32(reg_params[2].value, 0, 32, 4*thisrun_count);
  843. LOG_DEBUG("Algorithm flash write %u words to 0x%" PRIx32
  844. ", %u remaining",
  845. (unsigned) thisrun_count, address,
  846. (unsigned) (wcount - thisrun_count));
  847. retval = target_run_algorithm(target, 0, NULL, 3, reg_params,
  848. write_algorithm->address,
  849. 0,
  850. 10000, &armv7m_info);
  851. if (retval != ERROR_OK)
  852. {
  853. LOG_ERROR("error %d executing stellaris "
  854. "flash write algorithm",
  855. retval);
  856. retval = ERROR_FLASH_OPERATION_FAILED;
  857. break;
  858. }
  859. buffer += thisrun_count * 4;
  860. address += thisrun_count * 4;
  861. wcount -= thisrun_count;
  862. }
  863. /* REVISIT we could speed up writing multi-section images by
  864. * not freeing the initialized write_algorithm this way.
  865. */
  866. target_free_working_area(target, write_algorithm);
  867. target_free_working_area(target, source);
  868. destroy_reg_param(&reg_params[0]);
  869. destroy_reg_param(&reg_params[1]);
  870. destroy_reg_param(&reg_params[2]);
  871. return retval;
  872. }
  873. static int stellaris_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
  874. {
  875. struct stellaris_flash_bank *stellaris_info = bank->driver_priv;
  876. struct target *target = bank->target;
  877. uint32_t address = offset;
  878. uint32_t flash_cris, flash_fmc;
  879. uint32_t words_remaining = (count / 4);
  880. uint32_t bytes_remaining = (count & 0x00000003);
  881. uint32_t bytes_written = 0;
  882. int retval;
  883. if (bank->target->state != TARGET_HALTED)
  884. {
  885. LOG_ERROR("Target not halted");
  886. return ERROR_TARGET_NOT_HALTED;
  887. }
  888. LOG_DEBUG("(bank=%p buffer=%p offset=%08" PRIx32 " count=%08" PRIx32 "",
  889. bank, buffer, offset, count);
  890. if (stellaris_info->did1 == 0)
  891. return ERROR_FLASH_BANK_NOT_PROBED;
  892. if (offset & 0x3)
  893. {
  894. LOG_WARNING("offset size must be word aligned");
  895. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  896. }
  897. if (offset + count > bank->size)
  898. return ERROR_FLASH_DST_OUT_OF_BANK;
  899. /* Refresh flash controller timing */
  900. stellaris_read_clock_info(bank);
  901. stellaris_set_flash_timing(bank);
  902. /* Clear and disable flash programming interrupts */
  903. target_write_u32(target, FLASH_CIM, 0);
  904. target_write_u32(target, FLASH_MISC, PMISC | AMISC);
  905. /* REVISIT this clobbers state set by any halted firmware ...
  906. * it might want to process those IRQs.
  907. */
  908. /* multiple words to be programmed? */
  909. if (words_remaining > 0)
  910. {
  911. /* try using a block write */
  912. retval = stellaris_write_block(bank, buffer, offset,
  913. words_remaining);
  914. if (retval != ERROR_OK)
  915. {
  916. if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
  917. {
  918. LOG_DEBUG("writing flash word-at-a-time");
  919. }
  920. else if (retval == ERROR_FLASH_OPERATION_FAILED)
  921. {
  922. /* if an error occured, we examine the reason, and quit */
  923. target_read_u32(target, FLASH_CRIS, &flash_cris);
  924. LOG_ERROR("flash writing failed with CRIS: 0x%" PRIx32 "", flash_cris);
  925. return ERROR_FLASH_OPERATION_FAILED;
  926. }
  927. }
  928. else
  929. {
  930. buffer += words_remaining * 4;
  931. address += words_remaining * 4;
  932. words_remaining = 0;
  933. }
  934. }
  935. while (words_remaining > 0)
  936. {
  937. if (!(address & 0xff))
  938. LOG_DEBUG("0x%" PRIx32 "", address);
  939. /* Program one word */
  940. target_write_u32(target, FLASH_FMA, address);
  941. target_write_buffer(target, FLASH_FMD, 4, buffer);
  942. target_write_u32(target, FLASH_FMC, FMC_WRKEY | FMC_WRITE);
  943. /* LOG_DEBUG("0x%x 0x%x 0x%x",address,buf_get_u32(buffer, 0, 32),FMC_WRKEY | FMC_WRITE); */
  944. /* Wait until write complete */
  945. do
  946. {
  947. target_read_u32(target, FLASH_FMC, &flash_fmc);
  948. } while (flash_fmc & FMC_WRITE);
  949. buffer += 4;
  950. address += 4;
  951. words_remaining--;
  952. }
  953. if (bytes_remaining)
  954. {
  955. uint8_t last_word[4] = {0xff, 0xff, 0xff, 0xff};
  956. int i = 0;
  957. while (bytes_remaining > 0)
  958. {
  959. last_word[i++] = *(buffer + bytes_written);
  960. bytes_remaining--;
  961. bytes_written++;
  962. }
  963. if (!(address & 0xff))
  964. LOG_DEBUG("0x%" PRIx32 "", address);
  965. /* Program one word */
  966. target_write_u32(target, FLASH_FMA, address);
  967. target_write_buffer(target, FLASH_FMD, 4, last_word);
  968. target_write_u32(target, FLASH_FMC, FMC_WRKEY | FMC_WRITE);
  969. /* LOG_DEBUG("0x%x 0x%x 0x%x",address,buf_get_u32(buffer, 0, 32),FMC_WRKEY | FMC_WRITE); */
  970. /* Wait until write complete */
  971. do
  972. {
  973. target_read_u32(target, FLASH_FMC, &flash_fmc);
  974. } while (flash_fmc & FMC_WRITE);
  975. }
  976. /* Check access violations */
  977. target_read_u32(target, FLASH_CRIS, &flash_cris);
  978. if (flash_cris & (AMASK))
  979. {
  980. LOG_DEBUG("flash_cris 0x%" PRIx32 "", flash_cris);
  981. return ERROR_FLASH_OPERATION_FAILED;
  982. }
  983. return ERROR_OK;
  984. }
  985. static int stellaris_probe(struct flash_bank *bank)
  986. {
  987. struct stellaris_flash_bank *stellaris_info = bank->driver_priv;
  988. int retval;
  989. /* If this is a stellaris chip, it has flash; probe() is just
  990. * to figure out how much is present. Only do it once.
  991. */
  992. if (stellaris_info->did1 != 0)
  993. return ERROR_OK;
  994. /* stellaris_read_part_info() already handled error checking and
  995. * reporting. Note that it doesn't write, so we don't care about
  996. * whether the target is halted or not.
  997. */
  998. retval = stellaris_read_part_info(bank);
  999. if (retval != ERROR_OK)
  1000. return retval;
  1001. if (bank->sectors)
  1002. {
  1003. free(bank->sectors);
  1004. bank->sectors = NULL;
  1005. }
  1006. /* provide this for the benefit of the NOR flash framework */
  1007. bank->size = 1024 * stellaris_info->num_pages;
  1008. bank->num_sectors = stellaris_info->num_pages;
  1009. bank->sectors = calloc(bank->num_sectors, sizeof(struct flash_sector));
  1010. for (int i = 0; i < bank->num_sectors; i++)
  1011. {
  1012. bank->sectors[i].offset = i * stellaris_info->pagesize;
  1013. bank->sectors[i].size = stellaris_info->pagesize;
  1014. bank->sectors[i].is_erased = -1;
  1015. bank->sectors[i].is_protected = -1;
  1016. }
  1017. return retval;
  1018. }
  1019. static int stellaris_mass_erase(struct flash_bank *bank)
  1020. {
  1021. struct target *target = NULL;
  1022. struct stellaris_flash_bank *stellaris_info = NULL;
  1023. uint32_t flash_fmc;
  1024. stellaris_info = bank->driver_priv;
  1025. target = bank->target;
  1026. if (target->state != TARGET_HALTED)
  1027. {
  1028. LOG_ERROR("Target not halted");
  1029. return ERROR_TARGET_NOT_HALTED;
  1030. }
  1031. if (stellaris_info->did1 == 0)
  1032. return ERROR_FLASH_BANK_NOT_PROBED;
  1033. /* Refresh flash controller timing */
  1034. stellaris_read_clock_info(bank);
  1035. stellaris_set_flash_timing(bank);
  1036. /* Clear and disable flash programming interrupts */
  1037. target_write_u32(target, FLASH_CIM, 0);
  1038. target_write_u32(target, FLASH_MISC, PMISC | AMISC);
  1039. /* REVISIT this clobbers state set by any halted firmware ...
  1040. * it might want to process those IRQs.
  1041. */
  1042. target_write_u32(target, FLASH_FMA, 0);
  1043. target_write_u32(target, FLASH_FMC, FMC_WRKEY | FMC_MERASE);
  1044. /* Wait until erase complete */
  1045. do
  1046. {
  1047. target_read_u32(target, FLASH_FMC, &flash_fmc);
  1048. }
  1049. while (flash_fmc & FMC_MERASE);
  1050. /* if device has > 128k, then second erase cycle is needed
  1051. * this is only valid for older devices, but will not hurt */
  1052. if (stellaris_info->num_pages * stellaris_info->pagesize > 0x20000)
  1053. {
  1054. target_write_u32(target, FLASH_FMA, 0x20000);
  1055. target_write_u32(target, FLASH_FMC, FMC_WRKEY | FMC_MERASE);
  1056. /* Wait until erase complete */
  1057. do
  1058. {
  1059. target_read_u32(target, FLASH_FMC, &flash_fmc);
  1060. }
  1061. while (flash_fmc & FMC_MERASE);
  1062. }
  1063. return ERROR_OK;
  1064. }
  1065. COMMAND_HANDLER(stellaris_handle_mass_erase_command)
  1066. {
  1067. int i;
  1068. if (CMD_ARGC < 1)
  1069. {
  1070. command_print(CMD_CTX, "stellaris mass_erase <bank>");
  1071. return ERROR_OK;
  1072. }
  1073. struct flash_bank *bank;
  1074. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
  1075. if (ERROR_OK != retval)
  1076. return retval;
  1077. if (stellaris_mass_erase(bank) == ERROR_OK)
  1078. {
  1079. /* set all sectors as erased */
  1080. for (i = 0; i < bank->num_sectors; i++)
  1081. {
  1082. bank->sectors[i].is_erased = 1;
  1083. }
  1084. command_print(CMD_CTX, "stellaris mass erase complete");
  1085. }
  1086. else
  1087. {
  1088. command_print(CMD_CTX, "stellaris mass erase failed");
  1089. }
  1090. return ERROR_OK;
  1091. }
  1092. /**
  1093. * Perform the Stellaris "Recovering a 'Locked' Device procedure.
  1094. * This performs a mass erase and then restores all nonvolatile registers
  1095. * (including USER_* registers and flash lock bits) to their defaults.
  1096. * Accordingly, flash can be reprogrammed, and JTAG can be used.
  1097. *
  1098. * NOTE that DustDevil parts (at least rev A0 silicon) have errata which
  1099. * can affect this operation if flash protection has been enabled.
  1100. */
  1101. COMMAND_HANDLER(stellaris_handle_recover_command)
  1102. {
  1103. struct flash_bank *bank;
  1104. int retval;
  1105. retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
  1106. if (retval != ERROR_OK)
  1107. return retval;
  1108. /* REVISIT ... it may be worth sanity checking that the AP is
  1109. * inactive before we start. ARM documents that switching a DP's
  1110. * mode while it's active can cause fault modes that need a power
  1111. * cycle to recover.
  1112. */
  1113. /* assert SRST */
  1114. if (!(jtag_get_reset_config() & RESET_HAS_SRST)) {
  1115. LOG_ERROR("Can't recover Stellaris flash without SRST");
  1116. return ERROR_FAIL;
  1117. }
  1118. jtag_add_reset(0, 1);
  1119. for (int i = 0; i < 5; i++) {
  1120. retval = dap_to_swd(bank->target);
  1121. if (retval != ERROR_OK)
  1122. goto done;
  1123. retval = dap_to_jtag(bank->target);
  1124. if (retval != ERROR_OK)
  1125. goto done;
  1126. }
  1127. /* de-assert SRST */
  1128. jtag_add_reset(0, 0);
  1129. retval = jtag_execute_queue();
  1130. /* wait 400+ msec ... OK, "1+ second" is simpler */
  1131. usleep(1000);
  1132. /* USER INTERVENTION required for the power cycle
  1133. * Restarting OpenOCD is likely needed because of mode switching.
  1134. */
  1135. LOG_INFO("USER ACTION: "
  1136. "power cycle Stellaris chip, then restart OpenOCD.");
  1137. done:
  1138. return retval;
  1139. }
  1140. static const struct command_registration stellaris_exec_command_handlers[] = {
  1141. {
  1142. .name = "mass_erase",
  1143. .handler = stellaris_handle_mass_erase_command,
  1144. .mode = COMMAND_EXEC,
  1145. .usage = "bank_id",
  1146. .help = "erase entire device",
  1147. },
  1148. {
  1149. .name = "recover",
  1150. .handler = stellaris_handle_recover_command,
  1151. .mode = COMMAND_EXEC,
  1152. .usage = "bank_id",
  1153. .help = "recover (and erase) locked device",
  1154. },
  1155. COMMAND_REGISTRATION_DONE
  1156. };
  1157. static const struct command_registration stellaris_command_handlers[] = {
  1158. {
  1159. .name = "stellaris",
  1160. .mode = COMMAND_EXEC,
  1161. .help = "Stellaris flash command group",
  1162. .chain = stellaris_exec_command_handlers,
  1163. },
  1164. COMMAND_REGISTRATION_DONE
  1165. };
  1166. struct flash_driver stellaris_flash = {
  1167. .name = "stellaris",
  1168. .commands = stellaris_command_handlers,
  1169. .flash_bank_command = stellaris_flash_bank_command,
  1170. .erase = stellaris_erase,
  1171. .protect = stellaris_protect,
  1172. .write = stellaris_write,
  1173. .read = default_flash_read,
  1174. .probe = stellaris_probe,
  1175. .auto_probe = stellaris_probe,
  1176. .erase_check = default_flash_mem_blank_check,
  1177. .protect_check = stellaris_protect_check,
  1178. .info = get_stellaris_info,
  1179. };