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.
 
 
 
 
 
 

1460 lines
43 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, see <http://www.gnu.org/licenses/>. *
  20. ***************************************************************************/
  21. /***************************************************************************
  22. * STELLARIS flash is tested on LM3S811, LM3S6965, LM3s3748, more.
  23. ***************************************************************************/
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include "jtag/interface.h"
  28. #include "imp.h"
  29. #include <target/algorithm.h>
  30. #include <target/armv7m.h>
  31. #define DID0_VER(did0) ((did0 >> 28)&0x07)
  32. /* STELLARIS control registers */
  33. #define SCB_BASE 0x400FE000
  34. #define DID0 0x000
  35. #define DID1 0x004
  36. #define DC0 0x008
  37. #define DC1 0x010
  38. #define DC2 0x014
  39. #define DC3 0x018
  40. #define DC4 0x01C
  41. #define RIS 0x050
  42. #define RCC 0x060
  43. #define PLLCFG 0x064
  44. #define RCC2 0x070
  45. #define NVMSTAT 0x1a0
  46. /* "legacy" flash memory protection registers (64KB max) */
  47. #define FMPRE 0x130
  48. #define FMPPE 0x134
  49. /* new flash memory protection registers (for more than 64KB) */
  50. #define FMPRE0 0x200 /* PRE1 = PRE0 + 4, etc */
  51. #define FMPPE0 0x400 /* PPE1 = PPE0 + 4, etc */
  52. #define USECRL 0x140
  53. #define FLASH_CONTROL_BASE 0x400FD000
  54. #define FLASH_FMA (FLASH_CONTROL_BASE | 0x000)
  55. #define FLASH_FMD (FLASH_CONTROL_BASE | 0x004)
  56. #define FLASH_FMC (FLASH_CONTROL_BASE | 0x008)
  57. #define FLASH_CRIS (FLASH_CONTROL_BASE | 0x00C)
  58. #define FLASH_CIM (FLASH_CONTROL_BASE | 0x010)
  59. #define FLASH_MISC (FLASH_CONTROL_BASE | 0x014)
  60. #define FLASH_FSIZE (FLASH_CONTROL_BASE | 0xFC0)
  61. #define FLASH_SSIZE (FLASH_CONTROL_BASE | 0xFC4)
  62. #define AMISC 1
  63. #define PMISC 2
  64. #define AMASK 1
  65. #define PMASK 2
  66. /* Flash Controller Command bits */
  67. #define FMC_WRKEY (0xA442 << 16)
  68. #define FMC_COMT (1 << 3)
  69. #define FMC_MERASE (1 << 2)
  70. #define FMC_ERASE (1 << 1)
  71. #define FMC_WRITE (1 << 0)
  72. /* STELLARIS constants */
  73. /* values to write in FMA to commit write-"once" values */
  74. #define FLASH_FMA_PRE(x) (2 * (x)) /* for FMPPREx */
  75. #define FLASH_FMA_PPE(x) (2 * (x) + 1) /* for FMPPPEx */
  76. static void stellaris_read_clock_info(struct flash_bank *bank);
  77. static int stellaris_mass_erase(struct flash_bank *bank);
  78. struct stellaris_flash_bank {
  79. /* chip id register */
  80. uint32_t did0;
  81. uint32_t did1;
  82. uint32_t dc0;
  83. uint32_t dc1;
  84. uint32_t fsize;
  85. uint32_t ssize;
  86. const char *target_name;
  87. uint8_t target_class;
  88. uint32_t sramsiz;
  89. /* flash geometry */
  90. uint32_t num_pages;
  91. uint32_t pagesize;
  92. /* main clock status */
  93. uint32_t rcc;
  94. uint32_t rcc2;
  95. uint8_t mck_valid;
  96. uint8_t xtal_mask;
  97. uint32_t iosc_freq;
  98. uint32_t mck_freq;
  99. const char *iosc_desc;
  100. const char *mck_desc;
  101. };
  102. /* Autogenerated by contrib/gen-stellaris-part-header.pl */
  103. /* From Stellaris Firmware Development Package revision 9453 */
  104. static const struct {
  105. uint8_t class;
  106. uint8_t partno;
  107. const char *partname;
  108. } StellarisParts[] = {
  109. {0x00, 0x01, "LM3S101"},
  110. {0x00, 0x02, "LM3S102"},
  111. {0x01, 0xBF, "LM3S1110"},
  112. {0x01, 0xC3, "LM3S1133"},
  113. {0x01, 0xC5, "LM3S1138"},
  114. {0x01, 0xC1, "LM3S1150"},
  115. {0x01, 0xC4, "LM3S1162"},
  116. {0x01, 0xC2, "LM3S1165"},
  117. {0x01, 0xEC, "LM3S1166"},
  118. {0x01, 0xC6, "LM3S1332"},
  119. {0x01, 0xBC, "LM3S1435"},
  120. {0x01, 0xBA, "LM3S1439"},
  121. {0x01, 0xBB, "LM3S1512"},
  122. {0x01, 0xC7, "LM3S1538"},
  123. {0x01, 0xDB, "LM3S1601"},
  124. {0x03, 0x06, "LM3S1607"},
  125. {0x01, 0xDA, "LM3S1608"},
  126. {0x01, 0xC0, "LM3S1620"},
  127. {0x04, 0xCD, "LM3S1621"},
  128. {0x03, 0x03, "LM3S1625"},
  129. {0x03, 0x04, "LM3S1626"},
  130. {0x03, 0x05, "LM3S1627"},
  131. {0x01, 0xB3, "LM3S1635"},
  132. {0x01, 0xEB, "LM3S1636"},
  133. {0x01, 0xBD, "LM3S1637"},
  134. {0x04, 0xB1, "LM3S1651"},
  135. {0x01, 0xB9, "LM3S1751"},
  136. {0x03, 0x10, "LM3S1776"},
  137. {0x04, 0x16, "LM3S1811"},
  138. {0x04, 0x3D, "LM3S1816"},
  139. {0x01, 0xB4, "LM3S1850"},
  140. {0x01, 0xDD, "LM3S1911"},
  141. {0x01, 0xDC, "LM3S1918"},
  142. {0x01, 0xB7, "LM3S1937"},
  143. {0x01, 0xBE, "LM3S1958"},
  144. {0x01, 0xB5, "LM3S1960"},
  145. {0x01, 0xB8, "LM3S1968"},
  146. {0x01, 0xEA, "LM3S1969"},
  147. {0x04, 0xCE, "LM3S1B21"},
  148. {0x06, 0xCA, "LM3S1C21"},
  149. {0x06, 0xCB, "LM3S1C26"},
  150. {0x06, 0x98, "LM3S1C58"},
  151. {0x06, 0xB0, "LM3S1D21"},
  152. {0x06, 0xCC, "LM3S1D26"},
  153. {0x06, 0x1D, "LM3S1F11"},
  154. {0x06, 0x1B, "LM3S1F16"},
  155. {0x06, 0xAF, "LM3S1G21"},
  156. {0x06, 0x95, "LM3S1G58"},
  157. {0x06, 0x1E, "LM3S1H11"},
  158. {0x06, 0x1C, "LM3S1H16"},
  159. {0x04, 0x0F, "LM3S1J11"},
  160. {0x04, 0x3C, "LM3S1J16"},
  161. {0x04, 0x0E, "LM3S1N11"},
  162. {0x04, 0x3B, "LM3S1N16"},
  163. {0x04, 0xB2, "LM3S1P51"},
  164. {0x04, 0x9E, "LM3S1R21"},
  165. {0x04, 0xC9, "LM3S1R26"},
  166. {0x04, 0x30, "LM3S1W16"},
  167. {0x04, 0x2F, "LM3S1Z16"},
  168. {0x01, 0x51, "LM3S2110"},
  169. {0x01, 0x84, "LM3S2139"},
  170. {0x03, 0x39, "LM3S2276"},
  171. {0x01, 0xA2, "LM3S2410"},
  172. {0x01, 0x59, "LM3S2412"},
  173. {0x01, 0x56, "LM3S2432"},
  174. {0x01, 0x5A, "LM3S2533"},
  175. {0x01, 0xE1, "LM3S2601"},
  176. {0x01, 0xE0, "LM3S2608"},
  177. {0x03, 0x33, "LM3S2616"},
  178. {0x01, 0x57, "LM3S2620"},
  179. {0x01, 0x85, "LM3S2637"},
  180. {0x01, 0x53, "LM3S2651"},
  181. {0x03, 0x80, "LM3S2671"},
  182. {0x03, 0x50, "LM3S2678"},
  183. {0x01, 0xA4, "LM3S2730"},
  184. {0x01, 0x52, "LM3S2739"},
  185. {0x03, 0x3A, "LM3S2776"},
  186. {0x04, 0x6D, "LM3S2793"},
  187. {0x01, 0xE3, "LM3S2911"},
  188. {0x01, 0xE2, "LM3S2918"},
  189. {0x01, 0xED, "LM3S2919"},
  190. {0x01, 0x54, "LM3S2939"},
  191. {0x01, 0x8F, "LM3S2948"},
  192. {0x01, 0x58, "LM3S2950"},
  193. {0x01, 0x55, "LM3S2965"},
  194. {0x04, 0x6C, "LM3S2B93"},
  195. {0x06, 0x94, "LM3S2D93"},
  196. {0x06, 0x93, "LM3S2U93"},
  197. {0x00, 0x19, "LM3S300"},
  198. {0x00, 0x11, "LM3S301"},
  199. {0x00, 0x1A, "LM3S308"},
  200. {0x00, 0x12, "LM3S310"},
  201. {0x00, 0x13, "LM3S315"},
  202. {0x00, 0x14, "LM3S316"},
  203. {0x00, 0x17, "LM3S317"},
  204. {0x00, 0x15, "LM3S328"},
  205. {0x03, 0x08, "LM3S3634"},
  206. {0x03, 0x43, "LM3S3651"},
  207. {0x04, 0xC8, "LM3S3654"},
  208. {0x03, 0x44, "LM3S3739"},
  209. {0x03, 0x49, "LM3S3748"},
  210. {0x03, 0x45, "LM3S3749"},
  211. {0x04, 0x42, "LM3S3826"},
  212. {0x04, 0x41, "LM3S3J26"},
  213. {0x04, 0x40, "LM3S3N26"},
  214. {0x04, 0x3F, "LM3S3W26"},
  215. {0x04, 0x3E, "LM3S3Z26"},
  216. {0x03, 0x81, "LM3S5632"},
  217. {0x04, 0x0C, "LM3S5651"},
  218. {0x03, 0x8A, "LM3S5652"},
  219. {0x04, 0x4D, "LM3S5656"},
  220. {0x03, 0x91, "LM3S5662"},
  221. {0x03, 0x96, "LM3S5732"},
  222. {0x03, 0x97, "LM3S5737"},
  223. {0x03, 0xA0, "LM3S5739"},
  224. {0x03, 0x99, "LM3S5747"},
  225. {0x03, 0xA7, "LM3S5749"},
  226. {0x03, 0x9A, "LM3S5752"},
  227. {0x03, 0x9C, "LM3S5762"},
  228. {0x04, 0x69, "LM3S5791"},
  229. {0x04, 0x0B, "LM3S5951"},
  230. {0x04, 0x4E, "LM3S5956"},
  231. {0x04, 0x68, "LM3S5B91"},
  232. {0x06, 0x2E, "LM3S5C31"},
  233. {0x06, 0x2C, "LM3S5C36"},
  234. {0x06, 0x5E, "LM3S5C51"},
  235. {0x06, 0x5B, "LM3S5C56"},
  236. {0x06, 0x5F, "LM3S5D51"},
  237. {0x06, 0x5C, "LM3S5D56"},
  238. {0x06, 0x87, "LM3S5D91"},
  239. {0x06, 0x2D, "LM3S5G31"},
  240. {0x06, 0x1F, "LM3S5G36"},
  241. {0x06, 0x5D, "LM3S5G51"},
  242. {0x06, 0x4F, "LM3S5G56"},
  243. {0x04, 0x09, "LM3S5K31"},
  244. {0x04, 0x4A, "LM3S5K36"},
  245. {0x04, 0x0A, "LM3S5P31"},
  246. {0x04, 0x48, "LM3S5P36"},
  247. {0x04, 0xB6, "LM3S5P3B"},
  248. {0x04, 0x0D, "LM3S5P51"},
  249. {0x04, 0x4C, "LM3S5P56"},
  250. {0x04, 0x07, "LM3S5R31"},
  251. {0x04, 0x4B, "LM3S5R36"},
  252. {0x04, 0x47, "LM3S5T36"},
  253. {0x06, 0x7F, "LM3S5U91"},
  254. {0x04, 0x46, "LM3S5Y36"},
  255. {0x00, 0x2A, "LM3S600"},
  256. {0x00, 0x21, "LM3S601"},
  257. {0x00, 0x2B, "LM3S608"},
  258. {0x00, 0x22, "LM3S610"},
  259. {0x01, 0xA1, "LM3S6100"},
  260. {0x00, 0x23, "LM3S611"},
  261. {0x01, 0x74, "LM3S6110"},
  262. {0x00, 0x24, "LM3S612"},
  263. {0x00, 0x25, "LM3S613"},
  264. {0x00, 0x26, "LM3S615"},
  265. {0x00, 0x28, "LM3S617"},
  266. {0x00, 0x29, "LM3S618"},
  267. {0x00, 0x27, "LM3S628"},
  268. {0x01, 0xA5, "LM3S6420"},
  269. {0x01, 0x82, "LM3S6422"},
  270. {0x01, 0x75, "LM3S6432"},
  271. {0x01, 0x76, "LM3S6537"},
  272. {0x01, 0x71, "LM3S6610"},
  273. {0x01, 0xE7, "LM3S6611"},
  274. {0x01, 0xE6, "LM3S6618"},
  275. {0x01, 0x83, "LM3S6633"},
  276. {0x01, 0x8B, "LM3S6637"},
  277. {0x01, 0xA3, "LM3S6730"},
  278. {0x01, 0x77, "LM3S6753"},
  279. {0x01, 0xE9, "LM3S6911"},
  280. {0x01, 0xE8, "LM3S6918"},
  281. {0x01, 0x89, "LM3S6938"},
  282. {0x01, 0x72, "LM3S6950"},
  283. {0x01, 0x78, "LM3S6952"},
  284. {0x01, 0x73, "LM3S6965"},
  285. {0x06, 0xAA, "LM3S6C11"},
  286. {0x06, 0xAC, "LM3S6C65"},
  287. {0x06, 0x9F, "LM3S6G11"},
  288. {0x06, 0xAB, "LM3S6G65"},
  289. {0x00, 0x38, "LM3S800"},
  290. {0x00, 0x31, "LM3S801"},
  291. {0x00, 0x39, "LM3S808"},
  292. {0x00, 0x32, "LM3S811"},
  293. {0x00, 0x33, "LM3S812"},
  294. {0x00, 0x34, "LM3S815"},
  295. {0x00, 0x36, "LM3S817"},
  296. {0x00, 0x37, "LM3S818"},
  297. {0x00, 0x35, "LM3S828"},
  298. {0x01, 0x64, "LM3S8530"},
  299. {0x01, 0x8E, "LM3S8538"},
  300. {0x01, 0x61, "LM3S8630"},
  301. {0x01, 0x63, "LM3S8730"},
  302. {0x01, 0x8D, "LM3S8733"},
  303. {0x01, 0x86, "LM3S8738"},
  304. {0x01, 0x65, "LM3S8930"},
  305. {0x01, 0x8C, "LM3S8933"},
  306. {0x01, 0x88, "LM3S8938"},
  307. {0x01, 0xA6, "LM3S8962"},
  308. {0x01, 0x62, "LM3S8970"},
  309. {0x01, 0xD7, "LM3S8971"},
  310. {0x06, 0xAE, "LM3S8C62"},
  311. {0x06, 0xAD, "LM3S8G62"},
  312. {0x04, 0xCF, "LM3S9781"},
  313. {0x04, 0x67, "LM3S9790"},
  314. {0x04, 0x6B, "LM3S9792"},
  315. {0x04, 0x2D, "LM3S9971"},
  316. {0x04, 0x20, "LM3S9997"},
  317. {0x04, 0xD0, "LM3S9B81"},
  318. {0x04, 0x66, "LM3S9B90"},
  319. {0x04, 0x6A, "LM3S9B92"},
  320. {0x04, 0x6E, "LM3S9B95"},
  321. {0x04, 0x6F, "LM3S9B96"},
  322. {0x04, 0x1D, "LM3S9BN2"},
  323. {0x04, 0x1E, "LM3S9BN5"},
  324. {0x04, 0x1F, "LM3S9BN6"},
  325. {0x06, 0x70, "LM3S9C97"},
  326. {0x06, 0xA9, "LM3S9D81"},
  327. {0x06, 0x7E, "LM3S9D90"},
  328. {0x06, 0x92, "LM3S9D92"},
  329. {0x06, 0x9D, "LM3S9D96"},
  330. {0x06, 0x7B, "LM3S9DN5"},
  331. {0x06, 0x7C, "LM3S9DN6"},
  332. {0x06, 0x60, "LM3S9G97"},
  333. {0x06, 0x79, "LM3S9GN5"},
  334. {0x04, 0x1B, "LM3S9L71"},
  335. {0x04, 0x18, "LM3S9L97"},
  336. {0x06, 0xA8, "LM3S9U81"},
  337. {0x06, 0x7D, "LM3S9U90"},
  338. {0x06, 0x90, "LM3S9U92"},
  339. {0x06, 0x9B, "LM3S9U96"},
  340. {0x05, 0x01, "LM4F120B2QR/TM4C1233C3PM"},
  341. {0x05, 0x02, "LM4F120C4QR/TM4C1233D5PM"},
  342. {0x05, 0x03, "LM4F120E5QR/TM4C1233E6PM"},
  343. {0x05, 0x04, "LM4F120H5QR/TM4C1233H6PM"},
  344. {0x05, 0x08, "LM4F121B2QR/TM4C1232C3PM"},
  345. {0x05, 0x09, "LM4F121C4QR/TM4C1232D5PM"},
  346. {0x05, 0x0A, "LM4F121E5QR/TM4C1232E6PM"},
  347. {0x05, 0x0B, "LM4F121H5QR/TM4C1232H6PM"},
  348. {0x05, 0x10, "LM4F110E5QR/TM4C1231E6PM"},
  349. {0x05, 0x11, "LM4F110H5QR/TM4C1231H6PM"},
  350. {0x05, 0x18, "LM4F110B2QR/TM4C1231C3PM"},
  351. {0x05, 0x19, "LM4F110C4QR/TM4C1231D5PM"},
  352. {0x05, 0x20, "LM4F111E5QR/TM4C1230E6PM"},
  353. {0x05, 0x21, "LM4F111H5QR/TM4C1230H6PM"},
  354. {0x05, 0x22, "LM4F111B2QR/TM4C1230C3PM"},
  355. {0x05, 0x23, "LM4F111C4QR/TM4C1230D5PM"},
  356. {0x05, 0x30, "LM4F112E5QC/TM4C1231E6PZ"},
  357. {0x05, 0x31, "LM4F112H5QC/TM4C1231H6PZ"},
  358. {0x05, 0x35, "LM4F112H5QD/TM4C1231H6PGE"},
  359. {0x05, 0x36, "LM4F112C4QC/TM4C1231D5PZ"},
  360. {0x05, 0x40, "LM4F130E5QR/TM4C1237E6PM"},
  361. {0x05, 0x41, "LM4F130H5QR/TM4C1237H6PM"},
  362. {0x05, 0x48, "LM4F130C4QR/TM4C1237D5PM"},
  363. {0x05, 0x50, "LM4F131E5QR/TM4C1236E6PM"},
  364. {0x05, 0x51, "LM4F131H5QR/TM4C1236H6PM"},
  365. {0x05, 0x52, "LM4F131C4QR/TM4C1236D5PM"},
  366. {0x05, 0x60, "LM4F132E5QC/TM4C1237E6PZ"},
  367. {0x05, 0x61, "LM4F132H5QC/TM4C1237H6PZ"},
  368. {0x05, 0x65, "LM4F132H5QD/TM4C1237H6PGE"},
  369. {0x05, 0x66, "LM4F132C4QC/TM4C1237D5PZ"},
  370. {0x05, 0x70, "LM4F210E5QR/TM4C123BE6PM"},
  371. {0x05, 0x73, "LM4F210H5QR/TM4C123BH6PM"},
  372. {0x05, 0x80, "LM4F211E5QR/TM4C123AE6PM"},
  373. {0x05, 0x83, "LM4F211H5QR/TM4C123AH6PM"},
  374. {0x05, 0xA0, "LM4F230E5QR/TM4C123GE6PM"},
  375. {0x05, 0xA1, "LM4F230H5QR/TM4C123GH6PM"},
  376. {0x05, 0xB0, "LM4F231E5QR/TM4C123FE6PM"},
  377. {0x05, 0xB1, "LM4F231H5QR/TM4C123FH6PM"},
  378. {0x05, 0xC0, "LM4F232E5QC/TM4C123GE6PZ"},
  379. {0x05, 0xC1, "LM4F232H5QC/TM4C123GH6PZ"},
  380. {0x05, 0xC3, "LM4F212E5QC/TM4C123BE6PZ"},
  381. {0x05, 0xC4, "LM4F212H5QC/TM4C123BH6PZ"},
  382. {0x05, 0xC5, "LM4F232H5QD/TM4C123GH6PGE"},
  383. {0x05, 0xC6, "LM4F212H5QD/TM4C123BH6PGE"},
  384. {0x05, 0xD0, "LM4F122C4QC/TM4C1233D5PZ"},
  385. {0x05, 0xD1, "LM4F122E5QC/TM4C1233E6PZ"},
  386. {0x05, 0xD2, "LM4F122H5QC/TM4C1233H6PZ"},
  387. {0x05, 0xD6, "LM4F122H5QD/TM4C1233H6PGE"},
  388. {0x05, 0xE1, "LM4FSXLH5BB"},
  389. {0x05, 0xE3, "LM4F232H5BB/TM4C123GH6ZRB"},
  390. {0x05, 0xE4, "LM4FS99H5BB"},
  391. {0x05, 0xE5, "LM4FS1AH5BB"},
  392. {0x05, 0xE9, "LM4F212H5BB/TM4C123BH6ZRB"},
  393. {0x05, 0xEA, "LM4FS1GH5BB"},
  394. {0x05, 0xF0, "TM4C123GH6ZXR"},
  395. {0x0A, 0x19, "TM4C1290NCPDT"},
  396. {0x0A, 0x1B, "TM4C1290NCZAD"},
  397. {0x0A, 0x1C, "TM4C1292NCPDT"},
  398. {0x0A, 0x1E, "TM4C1292NCZAD"},
  399. {0x0A, 0x1F, "TM4C1294NCPDT"},
  400. {0x0A, 0x21, "TM4C1294NCZAD"},
  401. {0x0A, 0x22, "TM4C1297NCZAD"},
  402. {0x0A, 0x23, "TM4C1299NCZAD"},
  403. {0x0A, 0x24, "TM4C129CNCPDT"},
  404. {0x0A, 0x26, "TM4C129CNCZAD"},
  405. {0x0A, 0x27, "TM4C129DNCPDT"},
  406. {0x0A, 0x29, "TM4C129DNCZAD"},
  407. {0x0A, 0x2D, "TM4C129ENCPDT"},
  408. {0x0A, 0x2F, "TM4C129ENCZAD"},
  409. {0x0A, 0x30, "TM4C129LNCZAD"},
  410. {0x0A, 0x32, "TM4C129XNCZAD"},
  411. {0x0A, 0x34, "TM4C1294KCPDT"},
  412. {0x0A, 0x35, "TM4C129EKCPDT"},
  413. {0x0A, 0x36, "TM4C1299KCZAD"},
  414. {0x0A, 0x37, "TM4C129XKCZAD"},
  415. {0xFF, 0x00, "Unknown Part"}
  416. };
  417. static const char * const StellarisClassname[] = {
  418. "Sandstorm",
  419. "Fury",
  420. "Unknown",
  421. "DustDevil",
  422. "Tempest",
  423. "Blizzard/TM4C123x",
  424. "Firestorm",
  425. "",
  426. "",
  427. "",
  428. "Snowflake",
  429. };
  430. /***************************************************************************
  431. * openocd command interface *
  432. ***************************************************************************/
  433. /* flash_bank stellaris <base> <size> 0 0 <target#>
  434. */
  435. FLASH_BANK_COMMAND_HANDLER(stellaris_flash_bank_command)
  436. {
  437. struct stellaris_flash_bank *stellaris_info;
  438. if (CMD_ARGC < 6)
  439. return ERROR_COMMAND_SYNTAX_ERROR;
  440. stellaris_info = calloc(sizeof(struct stellaris_flash_bank), 1);
  441. bank->base = 0x0;
  442. bank->driver_priv = stellaris_info;
  443. stellaris_info->target_name = "Unknown target";
  444. /* part wasn't probed for info yet */
  445. stellaris_info->did1 = 0;
  446. /* TODO Specify the main crystal speed in kHz using an optional
  447. * argument; ditto, the speed of an external oscillator used
  448. * instead of a crystal. Avoid programming flash using IOSC.
  449. */
  450. return ERROR_OK;
  451. }
  452. static int get_stellaris_info(struct flash_bank *bank, char *buf, int buf_size)
  453. {
  454. int printed;
  455. struct stellaris_flash_bank *stellaris_info = bank->driver_priv;
  456. if (stellaris_info->did1 == 0)
  457. return ERROR_FLASH_BANK_NOT_PROBED;
  458. /* Read main and master clock freqency register */
  459. stellaris_read_clock_info(bank);
  460. printed = snprintf(buf,
  461. buf_size,
  462. "\nTI/LMI Stellaris information: Chip is "
  463. "class %i (%s) %s rev %c%i\n",
  464. stellaris_info->target_class,
  465. StellarisClassname[stellaris_info->target_class],
  466. stellaris_info->target_name,
  467. (int)('A' + ((stellaris_info->did0 >> 8) & 0xFF)),
  468. (int)((stellaris_info->did0) & 0xFF));
  469. buf += printed;
  470. buf_size -= printed;
  471. printed = snprintf(buf,
  472. buf_size,
  473. "did1: 0x%8.8" PRIx32 ", arch: 0x%4.4" PRIx32
  474. ", eproc: %s, ramsize: %" PRIu32 "k, flashsize: %" PRIu32 "k\n",
  475. stellaris_info->did1,
  476. stellaris_info->did1,
  477. "ARMv7M",
  478. stellaris_info->sramsiz,
  479. (uint32_t)(stellaris_info->num_pages * stellaris_info->pagesize / 1024));
  480. buf += printed;
  481. buf_size -= printed;
  482. snprintf(buf,
  483. buf_size,
  484. "master clock: %ikHz%s, "
  485. "rcc is 0x%" PRIx32 ", rcc2 is 0x%" PRIx32 ", "
  486. "pagesize: %" PRIu32 ", pages: %" PRIu32,
  487. (int)(stellaris_info->mck_freq / 1000),
  488. stellaris_info->mck_desc,
  489. stellaris_info->rcc,
  490. stellaris_info->rcc2,
  491. stellaris_info->pagesize,
  492. stellaris_info->num_pages);
  493. return ERROR_OK;
  494. }
  495. /***************************************************************************
  496. * chip identification and status *
  497. ***************************************************************************/
  498. /* Set the flash timimg register to match current clocking */
  499. static void stellaris_set_flash_timing(struct flash_bank *bank)
  500. {
  501. struct stellaris_flash_bank *stellaris_info = bank->driver_priv;
  502. struct target *target = bank->target;
  503. uint32_t usecrl = (stellaris_info->mck_freq/1000000ul-1);
  504. /* only valid for Sandstorm and Fury class devices */
  505. if (stellaris_info->target_class > 1)
  506. return;
  507. LOG_DEBUG("usecrl = %i", (int)(usecrl));
  508. target_write_u32(target, SCB_BASE | USECRL, usecrl);
  509. }
  510. static const unsigned rcc_xtal[32] = {
  511. [0x00] = 1000000, /* no pll */
  512. [0x01] = 1843200, /* no pll */
  513. [0x02] = 2000000, /* no pll */
  514. [0x03] = 2457600, /* no pll */
  515. [0x04] = 3579545,
  516. [0x05] = 3686400,
  517. [0x06] = 4000000, /* usb */
  518. [0x07] = 4096000,
  519. [0x08] = 4915200,
  520. [0x09] = 5000000, /* usb */
  521. [0x0a] = 5120000,
  522. [0x0b] = 6000000, /* (reset) usb */
  523. [0x0c] = 6144000,
  524. [0x0d] = 7372800,
  525. [0x0e] = 8000000, /* usb */
  526. [0x0f] = 8192000,
  527. /* parts before DustDevil use just 4 bits for xtal spec */
  528. [0x10] = 10000000, /* usb */
  529. [0x11] = 12000000, /* usb */
  530. [0x12] = 12288000,
  531. [0x13] = 13560000,
  532. [0x14] = 14318180,
  533. [0x15] = 16000000, /* usb */
  534. [0x16] = 16384000,
  535. };
  536. /** Read clock configuration and set stellaris_info->usec_clocks. */
  537. static void stellaris_read_clock_info(struct flash_bank *bank)
  538. {
  539. struct stellaris_flash_bank *stellaris_info = bank->driver_priv;
  540. struct target *target = bank->target;
  541. uint32_t rcc, rcc2, pllcfg, sysdiv, usesysdiv, bypass, oscsrc;
  542. unsigned xtal;
  543. unsigned long mainfreq;
  544. target_read_u32(target, SCB_BASE | RCC, &rcc);
  545. LOG_DEBUG("Stellaris RCC %" PRIx32 "", rcc);
  546. target_read_u32(target, SCB_BASE | RCC2, &rcc2);
  547. LOG_DEBUG("Stellaris RCC2 %" PRIx32 "", rcc);
  548. target_read_u32(target, SCB_BASE | PLLCFG, &pllcfg);
  549. LOG_DEBUG("Stellaris PLLCFG %" PRIx32 "", pllcfg);
  550. stellaris_info->rcc = rcc;
  551. stellaris_info->rcc2 = rcc2;
  552. sysdiv = (rcc >> 23) & 0xF;
  553. usesysdiv = (rcc >> 22) & 0x1;
  554. bypass = (rcc >> 11) & 0x1;
  555. oscsrc = (rcc >> 4) & 0x3;
  556. xtal = (rcc >> 6) & stellaris_info->xtal_mask;
  557. /* NOTE: post-Sandstorm parts have RCC2 which may override
  558. * parts of RCC ... with more sysdiv options, option for
  559. * 32768 Hz mainfreq, PLL controls. On Sandstorm it reads
  560. * as zero, so the "use RCC2" flag is always clear.
  561. */
  562. if (rcc2 & (1 << 31)) {
  563. sysdiv = (rcc2 >> 23) & 0x3F;
  564. bypass = (rcc2 >> 11) & 0x1;
  565. oscsrc = (rcc2 >> 4) & 0x7;
  566. /* FIXME Tempest parts have an additional lsb for
  567. * fractional sysdiv (200 MHz / 2.5 == 80 MHz)
  568. */
  569. }
  570. stellaris_info->mck_desc = "";
  571. switch (oscsrc) {
  572. case 0: /* MOSC */
  573. mainfreq = rcc_xtal[xtal];
  574. break;
  575. case 1: /* IOSC */
  576. mainfreq = stellaris_info->iosc_freq;
  577. stellaris_info->mck_desc = stellaris_info->iosc_desc;
  578. break;
  579. case 2: /* IOSC/4 */
  580. mainfreq = stellaris_info->iosc_freq / 4;
  581. stellaris_info->mck_desc = stellaris_info->iosc_desc;
  582. break;
  583. case 3: /* lowspeed */
  584. /* Sandstorm doesn't have this 30K +/- 30% osc */
  585. mainfreq = 30000;
  586. stellaris_info->mck_desc = " (±30%)";
  587. break;
  588. case 8: /* hibernation osc */
  589. /* not all parts support hibernation */
  590. mainfreq = 32768;
  591. break;
  592. default: /* NOTREACHED */
  593. mainfreq = 0;
  594. break;
  595. }
  596. /* PLL is used if it's not bypassed; its output is 200 MHz
  597. * even when it runs at 400 MHz (adds divide-by-two stage).
  598. */
  599. if (!bypass)
  600. mainfreq = 200000000;
  601. if (usesysdiv)
  602. stellaris_info->mck_freq = mainfreq/(1 + sysdiv);
  603. else
  604. stellaris_info->mck_freq = mainfreq;
  605. }
  606. /* Read device id register, main clock frequency register and fill in driver info structure */
  607. static int stellaris_read_part_info(struct flash_bank *bank)
  608. {
  609. struct stellaris_flash_bank *stellaris_info = bank->driver_priv;
  610. struct target *target = bank->target;
  611. uint32_t did0, did1, ver, fam;
  612. int i;
  613. /* Read and parse chip identification register */
  614. target_read_u32(target, SCB_BASE | DID0, &did0);
  615. target_read_u32(target, SCB_BASE | DID1, &did1);
  616. target_read_u32(target, SCB_BASE | DC0, &stellaris_info->dc0);
  617. target_read_u32(target, SCB_BASE | DC1, &stellaris_info->dc1);
  618. LOG_DEBUG("did0 0x%" PRIx32 ", did1 0x%" PRIx32 ", dc0 0x%" PRIx32 ", dc1 0x%" PRIx32 "",
  619. did0, did1, stellaris_info->dc0, stellaris_info->dc1);
  620. ver = DID0_VER(did0);
  621. if ((ver != 0) && (ver != 1)) {
  622. LOG_WARNING("Unknown did0 version, cannot identify target");
  623. return ERROR_FLASH_OPERATION_FAILED;
  624. }
  625. if (did1 == 0) {
  626. LOG_WARNING("Cannot identify target as a Stellaris");
  627. return ERROR_FLASH_OPERATION_FAILED;
  628. }
  629. ver = did1 >> 28;
  630. fam = (did1 >> 24) & 0xF;
  631. if (((ver != 0) && (ver != 1)) || (fam != 0)) {
  632. LOG_WARNING("Unknown did1 version/family.");
  633. return ERROR_FLASH_OPERATION_FAILED;
  634. }
  635. /* For Sandstorm, Fury, DustDevil: current data sheets say IOSC
  636. * is 12 MHz, but some older parts have 15 MHz. A few data sheets
  637. * even give _both_ numbers! We'll use current numbers; IOSC is
  638. * always approximate.
  639. *
  640. * For Tempest: IOSC is calibrated, 16 MHz
  641. * For Blizzard: IOSC is calibrated, 16 MHz
  642. * For Firestorm: IOSC is calibrated, 16 MHz
  643. */
  644. stellaris_info->iosc_freq = 12000000;
  645. stellaris_info->iosc_desc = " (±30%)";
  646. stellaris_info->xtal_mask = 0x0f;
  647. /* get device class */
  648. if (DID0_VER(did0) > 0) {
  649. stellaris_info->target_class = (did0 >> 16) & 0xFF;
  650. } else {
  651. /* Sandstorm class */
  652. stellaris_info->target_class = 0;
  653. }
  654. switch (stellaris_info->target_class) {
  655. case 0: /* Sandstorm */
  656. /*
  657. * Current (2009-August) parts seem to be rev C2 and use 12 MHz.
  658. * Parts before rev C0 used 15 MHz; some C0 parts use 15 MHz
  659. * (LM3S618), but some other C0 parts are 12 MHz (LM3S811).
  660. */
  661. if (((did0 >> 8) & 0xff) < 2) {
  662. stellaris_info->iosc_freq = 15000000;
  663. stellaris_info->iosc_desc = " (±50%)";
  664. }
  665. break;
  666. case 1: /* Fury */
  667. break;
  668. case 4: /* Tempest */
  669. case 5: /* Blizzard */
  670. case 6: /* Firestorm */
  671. case 0xa: /* Snowflake */
  672. stellaris_info->iosc_freq = 16000000; /* +/- 1% */
  673. stellaris_info->iosc_desc = " (±1%)";
  674. /* FALL THROUGH */
  675. case 3: /* DustDevil */
  676. stellaris_info->xtal_mask = 0x1f;
  677. break;
  678. default:
  679. LOG_WARNING("Unknown did0 class");
  680. }
  681. for (i = 0; StellarisParts[i].partno; i++) {
  682. if ((StellarisParts[i].partno == ((did1 >> 16) & 0xFF)) &&
  683. (StellarisParts[i].class == stellaris_info->target_class))
  684. break;
  685. }
  686. stellaris_info->target_name = StellarisParts[i].partname;
  687. stellaris_info->did0 = did0;
  688. stellaris_info->did1 = did1;
  689. if (stellaris_info->target_class == 5) { /* Blizzard */
  690. target_read_u32(target, FLASH_FSIZE, &stellaris_info->fsize);
  691. target_read_u32(target, FLASH_SSIZE, &stellaris_info->ssize);
  692. stellaris_info->num_pages = 2 * (1 + (stellaris_info->fsize & 0xFFFF));
  693. stellaris_info->sramsiz = (1 + (stellaris_info->ssize & 0xFFFF)) / 4;
  694. stellaris_info->pagesize = 1024;
  695. } else if (stellaris_info->target_class == 0xa) { /* Snowflake */
  696. target_read_u32(target, FLASH_FSIZE, &stellaris_info->fsize);
  697. target_read_u32(target, FLASH_SSIZE, &stellaris_info->ssize);
  698. stellaris_info->pagesize = (1 << ((stellaris_info->fsize >> 16) & 7)) * 1024;
  699. stellaris_info->num_pages = 2048 * (1 + (stellaris_info->fsize & 0xFFFF)) /
  700. stellaris_info->pagesize;
  701. stellaris_info->sramsiz = (1 + (stellaris_info->ssize & 0xFFFF)) / 4;
  702. } else {
  703. stellaris_info->num_pages = 2 * (1 + (stellaris_info->dc0 & 0xFFFF));
  704. stellaris_info->sramsiz = (1 + ((stellaris_info->dc0 >> 16) & 0xFFFF)) / 4;
  705. stellaris_info->pagesize = 1024;
  706. }
  707. /* REVISIT for at least Tempest parts, read NVMSTAT.FWB too.
  708. * That exposes a 32-word Flash Write Buffer ... enabling
  709. * writes of more than one word at a time.
  710. */
  711. return ERROR_OK;
  712. }
  713. /***************************************************************************
  714. * flash operations *
  715. ***************************************************************************/
  716. static int stellaris_protect_check(struct flash_bank *bank)
  717. {
  718. struct stellaris_flash_bank *stellaris = bank->driver_priv;
  719. struct target *target = bank->target;
  720. uint32_t flash_sizek = stellaris->pagesize / 1024 *
  721. stellaris->num_pages;
  722. uint32_t fmppe_addr;
  723. int status = ERROR_OK;
  724. unsigned i;
  725. if (stellaris->did1 == 0)
  726. return ERROR_FLASH_BANK_NOT_PROBED;
  727. for (i = 0; i < (unsigned) bank->num_sectors; i++)
  728. bank->sectors[i].is_protected = -1;
  729. /* Read each Flash Memory Protection Program Enable (FMPPE) register
  730. * to report any pages that we can't write. Ignore the Read Enable
  731. * register (FMPRE).
  732. */
  733. if (stellaris->target_class >= 0x0a || flash_sizek > 64)
  734. fmppe_addr = SCB_BASE | FMPPE0;
  735. else
  736. fmppe_addr = SCB_BASE | FMPPE;
  737. unsigned int page = 0, lockbitnum, lockbitcnt = flash_sizek / 2;
  738. unsigned int bits_per_page = stellaris->pagesize / 2048;
  739. /* Every lock bit always corresponds to a 2k region */
  740. for (lockbitnum = 0; lockbitnum < lockbitcnt; lockbitnum += 32) {
  741. uint32_t fmppe;
  742. target_read_u32(target, fmppe_addr, &fmppe);
  743. for (i = 0; i < 32 && lockbitnum + i < lockbitcnt; i++) {
  744. bool protect = !(fmppe & (1 << i));
  745. if (bits_per_page) {
  746. bank->sectors[page++].is_protected = protect;
  747. i += bits_per_page - 1;
  748. } else { /* 1024k pages, every lockbit covers 2 pages */
  749. bank->sectors[page++].is_protected = protect;
  750. bank->sectors[page++].is_protected = protect;
  751. }
  752. }
  753. fmppe_addr += 4;
  754. }
  755. return status;
  756. }
  757. static int stellaris_erase(struct flash_bank *bank, int first, int last)
  758. {
  759. int banknr;
  760. uint32_t flash_fmc, flash_cris;
  761. struct stellaris_flash_bank *stellaris_info = bank->driver_priv;
  762. struct target *target = bank->target;
  763. if (bank->target->state != TARGET_HALTED) {
  764. LOG_ERROR("Target not halted");
  765. return ERROR_TARGET_NOT_HALTED;
  766. }
  767. if (stellaris_info->did1 == 0)
  768. return ERROR_FLASH_BANK_NOT_PROBED;
  769. if ((first < 0) || (last < first) || (last >= (int)stellaris_info->num_pages))
  770. return ERROR_FLASH_SECTOR_INVALID;
  771. if ((first == 0) && (last == ((int)stellaris_info->num_pages-1)))
  772. return stellaris_mass_erase(bank);
  773. /* Refresh flash controller timing */
  774. stellaris_read_clock_info(bank);
  775. stellaris_set_flash_timing(bank);
  776. /* Clear and disable flash programming interrupts */
  777. target_write_u32(target, FLASH_CIM, 0);
  778. target_write_u32(target, FLASH_MISC, PMISC | AMISC);
  779. /* REVISIT this clobbers state set by any halted firmware ...
  780. * it might want to process those IRQs.
  781. */
  782. for (banknr = first; banknr <= last; banknr++) {
  783. /* Address is first word in page */
  784. target_write_u32(target, FLASH_FMA, banknr * stellaris_info->pagesize);
  785. /* Write erase command */
  786. target_write_u32(target, FLASH_FMC, FMC_WRKEY | FMC_ERASE);
  787. /* Wait until erase complete */
  788. do {
  789. target_read_u32(target, FLASH_FMC, &flash_fmc);
  790. } while (flash_fmc & FMC_ERASE);
  791. /* Check acess violations */
  792. target_read_u32(target, FLASH_CRIS, &flash_cris);
  793. if (flash_cris & (AMASK)) {
  794. LOG_WARNING("Error erasing flash page %i, flash_cris 0x%" PRIx32 "",
  795. banknr, flash_cris);
  796. target_write_u32(target, FLASH_CRIS, 0);
  797. return ERROR_FLASH_OPERATION_FAILED;
  798. }
  799. bank->sectors[banknr].is_erased = 1;
  800. }
  801. return ERROR_OK;
  802. }
  803. static int stellaris_protect(struct flash_bank *bank, int set, int first, int last)
  804. {
  805. struct stellaris_flash_bank *stellaris = bank->driver_priv;
  806. struct target *target = bank->target;
  807. uint32_t flash_fmc, flash_cris;
  808. unsigned int bits_per_page = stellaris->pagesize / 2048;
  809. if (target->state != TARGET_HALTED) {
  810. LOG_ERROR("Target not halted");
  811. return ERROR_TARGET_NOT_HALTED;
  812. }
  813. if (!set) {
  814. LOG_ERROR("Hardware doesn't support page-level unprotect. "
  815. "Try the 'recover' command.");
  816. return ERROR_COMMAND_SYNTAX_ERROR;
  817. }
  818. if (stellaris->did1 == 0)
  819. return ERROR_FLASH_BANK_NOT_PROBED;
  820. if (stellaris->target_class == 0x03 &&
  821. !((stellaris->did0 >> 8) & 0xFF) &&
  822. !((stellaris->did0) & 0xFF)) {
  823. LOG_ERROR("DustDevil A0 parts can't be unprotected, see errata; refusing to proceed");
  824. return ERROR_FLASH_OPERATION_FAILED;
  825. }
  826. if (!bits_per_page && (first % 2 || !(last % 2))) {
  827. LOG_ERROR("Can't protect unaligned pages");
  828. return ERROR_FLASH_SECTOR_INVALID;
  829. }
  830. /* Refresh flash controller timing */
  831. stellaris_read_clock_info(bank);
  832. stellaris_set_flash_timing(bank);
  833. /* Clear and disable flash programming interrupts */
  834. target_write_u32(target, FLASH_CIM, 0);
  835. target_write_u32(target, FLASH_MISC, PMISC | AMISC);
  836. uint32_t flash_sizek = stellaris->pagesize / 1024 *
  837. stellaris->num_pages;
  838. uint32_t fmppe_addr;
  839. if (stellaris->target_class >= 0x0a || flash_sizek > 64)
  840. fmppe_addr = SCB_BASE | FMPPE0;
  841. else
  842. fmppe_addr = SCB_BASE | FMPPE;
  843. int page = 0;
  844. unsigned int lockbitnum, lockbitcnt = flash_sizek / 2;
  845. /* Every lock bit always corresponds to a 2k region */
  846. for (lockbitnum = 0; lockbitnum < lockbitcnt; lockbitnum += 32) {
  847. uint32_t fmppe;
  848. target_read_u32(target, fmppe_addr, &fmppe);
  849. for (unsigned int i = 0;
  850. i < 32 && lockbitnum + i < lockbitcnt;
  851. i++) {
  852. if (page >= first && page <= last)
  853. fmppe &= ~(1 << i);
  854. if (bits_per_page) {
  855. if (!((i + 1) % bits_per_page))
  856. page++;
  857. } else { /* 1024k pages, every lockbit covers 2 pages */
  858. page += 2;
  859. }
  860. }
  861. target_write_u32(target, fmppe_addr, fmppe);
  862. /* Commit FMPPE* */
  863. target_write_u32(target, FLASH_FMA, 1 + lockbitnum / 16);
  864. /* Write commit command */
  865. target_write_u32(target, FLASH_FMC, FMC_WRKEY | FMC_COMT);
  866. /* Wait until commit complete */
  867. do {
  868. target_read_u32(target, FLASH_FMC, &flash_fmc);
  869. } while (flash_fmc & FMC_COMT);
  870. /* Check access violations */
  871. target_read_u32(target, FLASH_CRIS, &flash_cris);
  872. if (flash_cris & (AMASK)) {
  873. LOG_WARNING("Error setting flash page protection, flash_cris 0x%" PRIx32 "", flash_cris);
  874. target_write_u32(target, FLASH_CRIS, 0);
  875. return ERROR_FLASH_OPERATION_FAILED;
  876. }
  877. fmppe_addr += 4;
  878. }
  879. return ERROR_OK;
  880. }
  881. /* see contib/loaders/flash/stellaris.s for src */
  882. static const uint8_t stellaris_write_code[] = {
  883. /* write: */
  884. 0xDF, 0xF8, 0x40, 0x40, /* ldr r4, pFLASH_CTRL_BASE */
  885. 0xDF, 0xF8, 0x40, 0x50, /* ldr r5, FLASHWRITECMD */
  886. /* wait_fifo: */
  887. 0xD0, 0xF8, 0x00, 0x80, /* ldr r8, [r0, #0] */
  888. 0xB8, 0xF1, 0x00, 0x0F, /* cmp r8, #0 */
  889. 0x17, 0xD0, /* beq exit */
  890. 0x47, 0x68, /* ldr r7, [r0, #4] */
  891. 0x47, 0x45, /* cmp r7, r8 */
  892. 0xF7, 0xD0, /* beq wait_fifo */
  893. /* mainloop: */
  894. 0x22, 0x60, /* str r2, [r4, #0] */
  895. 0x02, 0xF1, 0x04, 0x02, /* add r2, r2, #4 */
  896. 0x57, 0xF8, 0x04, 0x8B, /* ldr r8, [r7], #4 */
  897. 0xC4, 0xF8, 0x04, 0x80, /* str r8, [r4, #4] */
  898. 0xA5, 0x60, /* str r5, [r4, #8] */
  899. /* busy: */
  900. 0xD4, 0xF8, 0x08, 0x80, /* ldr r8, [r4, #8] */
  901. 0x18, 0xF0, 0x01, 0x0F, /* tst r8, #1 */
  902. 0xFA, 0xD1, /* bne busy */
  903. 0x8F, 0x42, /* cmp r7, r1 */
  904. 0x28, 0xBF, /* it cs */
  905. 0x00, 0xF1, 0x08, 0x07, /* addcs r7, r0, #8 */
  906. 0x47, 0x60, /* str r7, [r0, #4] */
  907. 0x01, 0x3B, /* subs r3, r3, #1 */
  908. 0x03, 0xB1, /* cbz r3, exit */
  909. 0xE2, 0xE7, /* b wait_fifo */
  910. /* exit: */
  911. 0x00, 0xBE, /* bkpt #0 */
  912. /* pFLASH_CTRL_BASE: */
  913. 0x00, 0xD0, 0x0F, 0x40, /* .word 0x400FD000 */
  914. /* FLASHWRITECMD: */
  915. 0x01, 0x00, 0x42, 0xA4 /* .word 0xA4420001 */
  916. };
  917. static int stellaris_write_block(struct flash_bank *bank,
  918. const uint8_t *buffer, uint32_t offset, uint32_t wcount)
  919. {
  920. struct target *target = bank->target;
  921. uint32_t buffer_size = 16384;
  922. struct working_area *source;
  923. struct working_area *write_algorithm;
  924. uint32_t address = bank->base + offset;
  925. struct reg_param reg_params[4];
  926. struct armv7m_algorithm armv7m_info;
  927. int retval = ERROR_OK;
  928. /* power of two, and multiple of word size */
  929. static const unsigned buf_min = 128;
  930. /* for small buffers it's faster not to download an algorithm */
  931. if (wcount * 4 < buf_min)
  932. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  933. LOG_DEBUG("(bank=%p buffer=%p offset=%08" PRIx32 " wcount=%08" PRIx32 "",
  934. bank, buffer, offset, wcount);
  935. /* flash write code */
  936. if (target_alloc_working_area(target, sizeof(stellaris_write_code),
  937. &write_algorithm) != ERROR_OK) {
  938. LOG_DEBUG("no working area for block memory writes");
  939. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  940. }
  941. /* plus a buffer big enough for this data */
  942. if (wcount * 4 < buffer_size)
  943. buffer_size = wcount * 4;
  944. /* memory buffer */
  945. while (target_alloc_working_area_try(target, buffer_size, &source) != ERROR_OK) {
  946. buffer_size /= 2;
  947. if (buffer_size <= buf_min) {
  948. target_free_working_area(target, write_algorithm);
  949. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  950. }
  951. LOG_DEBUG("retry target_alloc_working_area(%s, size=%u)",
  952. target_name(target), (unsigned) buffer_size);
  953. }
  954. target_write_buffer(target, write_algorithm->address,
  955. sizeof(stellaris_write_code),
  956. stellaris_write_code);
  957. armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
  958. armv7m_info.core_mode = ARM_MODE_THREAD;
  959. init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
  960. init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
  961. init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT);
  962. init_reg_param(&reg_params[3], "r3", 32, PARAM_OUT);
  963. buf_set_u32(reg_params[0].value, 0, 32, source->address);
  964. buf_set_u32(reg_params[1].value, 0, 32, source->address + source->size);
  965. buf_set_u32(reg_params[2].value, 0, 32, address);
  966. buf_set_u32(reg_params[3].value, 0, 32, wcount);
  967. retval = target_run_flash_async_algorithm(target, buffer, wcount, 4,
  968. 0, NULL,
  969. 4, reg_params,
  970. source->address, source->size,
  971. write_algorithm->address, 0,
  972. &armv7m_info);
  973. if (retval == ERROR_FLASH_OPERATION_FAILED)
  974. LOG_ERROR("error %d executing stellaris flash write algorithm", retval);
  975. target_free_working_area(target, write_algorithm);
  976. target_free_working_area(target, source);
  977. destroy_reg_param(&reg_params[0]);
  978. destroy_reg_param(&reg_params[1]);
  979. destroy_reg_param(&reg_params[2]);
  980. destroy_reg_param(&reg_params[3]);
  981. return retval;
  982. }
  983. static int stellaris_write(struct flash_bank *bank, const uint8_t *buffer,
  984. uint32_t offset, uint32_t count)
  985. {
  986. struct stellaris_flash_bank *stellaris_info = bank->driver_priv;
  987. struct target *target = bank->target;
  988. uint32_t address = offset;
  989. uint32_t flash_cris, flash_fmc;
  990. uint32_t words_remaining = (count / 4);
  991. uint32_t bytes_remaining = (count & 0x00000003);
  992. uint32_t bytes_written = 0;
  993. int retval;
  994. if (bank->target->state != TARGET_HALTED) {
  995. LOG_ERROR("Target not halted");
  996. return ERROR_TARGET_NOT_HALTED;
  997. }
  998. LOG_DEBUG("(bank=%p buffer=%p offset=%08" PRIx32 " count=%08" PRIx32 "",
  999. bank, buffer, offset, count);
  1000. if (stellaris_info->did1 == 0)
  1001. return ERROR_FLASH_BANK_NOT_PROBED;
  1002. if (offset & 0x3) {
  1003. LOG_WARNING("offset size must be word aligned");
  1004. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  1005. }
  1006. if (offset + count > bank->size)
  1007. return ERROR_FLASH_DST_OUT_OF_BANK;
  1008. /* Refresh flash controller timing */
  1009. stellaris_read_clock_info(bank);
  1010. stellaris_set_flash_timing(bank);
  1011. /* Clear and disable flash programming interrupts */
  1012. target_write_u32(target, FLASH_CIM, 0);
  1013. target_write_u32(target, FLASH_MISC, PMISC | AMISC);
  1014. /* REVISIT this clobbers state set by any halted firmware ...
  1015. * it might want to process those IRQs.
  1016. */
  1017. /* multiple words to be programmed? */
  1018. if (words_remaining > 0) {
  1019. /* try using a block write */
  1020. retval = stellaris_write_block(bank, buffer, offset,
  1021. words_remaining);
  1022. if (retval != ERROR_OK) {
  1023. if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
  1024. LOG_DEBUG("writing flash word-at-a-time");
  1025. } else if (retval == ERROR_FLASH_OPERATION_FAILED) {
  1026. /* if an error occured, we examine the reason, and quit */
  1027. target_read_u32(target, FLASH_CRIS, &flash_cris);
  1028. LOG_ERROR("flash writing failed with CRIS: 0x%" PRIx32 "", flash_cris);
  1029. return ERROR_FLASH_OPERATION_FAILED;
  1030. }
  1031. } else {
  1032. buffer += words_remaining * 4;
  1033. address += words_remaining * 4;
  1034. words_remaining = 0;
  1035. }
  1036. }
  1037. while (words_remaining > 0) {
  1038. if (!(address & 0xff))
  1039. LOG_DEBUG("0x%" PRIx32 "", address);
  1040. /* Program one word */
  1041. target_write_u32(target, FLASH_FMA, address);
  1042. target_write_buffer(target, FLASH_FMD, 4, buffer);
  1043. target_write_u32(target, FLASH_FMC, FMC_WRKEY | FMC_WRITE);
  1044. /* LOG_DEBUG("0x%x 0x%x 0x%x",address,buf_get_u32(buffer, 0, 32),FMC_WRKEY | FMC_WRITE); */
  1045. /* Wait until write complete */
  1046. do {
  1047. target_read_u32(target, FLASH_FMC, &flash_fmc);
  1048. } while (flash_fmc & FMC_WRITE);
  1049. buffer += 4;
  1050. address += 4;
  1051. words_remaining--;
  1052. }
  1053. if (bytes_remaining) {
  1054. uint8_t last_word[4] = {0xff, 0xff, 0xff, 0xff};
  1055. /* copy the last remaining bytes into the write buffer */
  1056. memcpy(last_word, buffer+bytes_written, bytes_remaining);
  1057. if (!(address & 0xff))
  1058. LOG_DEBUG("0x%" PRIx32 "", address);
  1059. /* Program one word */
  1060. target_write_u32(target, FLASH_FMA, address);
  1061. target_write_buffer(target, FLASH_FMD, 4, last_word);
  1062. target_write_u32(target, FLASH_FMC, FMC_WRKEY | FMC_WRITE);
  1063. /* LOG_DEBUG("0x%x 0x%x 0x%x",address,buf_get_u32(buffer, 0, 32),FMC_WRKEY | FMC_WRITE); */
  1064. /* Wait until write complete */
  1065. do {
  1066. target_read_u32(target, FLASH_FMC, &flash_fmc);
  1067. } while (flash_fmc & FMC_WRITE);
  1068. }
  1069. /* Check access violations */
  1070. target_read_u32(target, FLASH_CRIS, &flash_cris);
  1071. if (flash_cris & (AMASK)) {
  1072. LOG_DEBUG("flash_cris 0x%" PRIx32 "", flash_cris);
  1073. return ERROR_FLASH_OPERATION_FAILED;
  1074. }
  1075. return ERROR_OK;
  1076. }
  1077. static int stellaris_probe(struct flash_bank *bank)
  1078. {
  1079. struct stellaris_flash_bank *stellaris_info = bank->driver_priv;
  1080. int retval;
  1081. /* If this is a stellaris chip, it has flash; probe() is just
  1082. * to figure out how much is present. Only do it once.
  1083. */
  1084. if (stellaris_info->did1 != 0)
  1085. return ERROR_OK;
  1086. /* stellaris_read_part_info() already handled error checking and
  1087. * reporting. Note that it doesn't write, so we don't care about
  1088. * whether the target is halted or not.
  1089. */
  1090. retval = stellaris_read_part_info(bank);
  1091. if (retval != ERROR_OK)
  1092. return retval;
  1093. if (bank->sectors) {
  1094. free(bank->sectors);
  1095. bank->sectors = NULL;
  1096. }
  1097. /* provide this for the benefit of the NOR flash framework */
  1098. bank->size = stellaris_info->num_pages * stellaris_info->pagesize;
  1099. bank->num_sectors = stellaris_info->num_pages;
  1100. bank->sectors = calloc(bank->num_sectors, sizeof(struct flash_sector));
  1101. for (int i = 0; i < bank->num_sectors; i++) {
  1102. bank->sectors[i].offset = i * stellaris_info->pagesize;
  1103. bank->sectors[i].size = stellaris_info->pagesize;
  1104. bank->sectors[i].is_erased = -1;
  1105. bank->sectors[i].is_protected = -1;
  1106. }
  1107. return retval;
  1108. }
  1109. static int stellaris_mass_erase(struct flash_bank *bank)
  1110. {
  1111. struct target *target = NULL;
  1112. struct stellaris_flash_bank *stellaris_info = NULL;
  1113. uint32_t flash_fmc;
  1114. stellaris_info = bank->driver_priv;
  1115. target = bank->target;
  1116. if (target->state != TARGET_HALTED) {
  1117. LOG_ERROR("Target not halted");
  1118. return ERROR_TARGET_NOT_HALTED;
  1119. }
  1120. if (stellaris_info->did1 == 0)
  1121. return ERROR_FLASH_BANK_NOT_PROBED;
  1122. /* Refresh flash controller timing */
  1123. stellaris_read_clock_info(bank);
  1124. stellaris_set_flash_timing(bank);
  1125. /* Clear and disable flash programming interrupts */
  1126. target_write_u32(target, FLASH_CIM, 0);
  1127. target_write_u32(target, FLASH_MISC, PMISC | AMISC);
  1128. /* REVISIT this clobbers state set by any halted firmware ...
  1129. * it might want to process those IRQs.
  1130. */
  1131. target_write_u32(target, FLASH_FMA, 0);
  1132. target_write_u32(target, FLASH_FMC, FMC_WRKEY | FMC_MERASE);
  1133. /* Wait until erase complete */
  1134. do {
  1135. target_read_u32(target, FLASH_FMC, &flash_fmc);
  1136. } while (flash_fmc & FMC_MERASE);
  1137. /* if device has > 128k, then second erase cycle is needed
  1138. * this is only valid for older devices, but will not hurt */
  1139. if (stellaris_info->num_pages * stellaris_info->pagesize > 0x20000) {
  1140. target_write_u32(target, FLASH_FMA, 0x20000);
  1141. target_write_u32(target, FLASH_FMC, FMC_WRKEY | FMC_MERASE);
  1142. /* Wait until erase complete */
  1143. do {
  1144. target_read_u32(target, FLASH_FMC, &flash_fmc);
  1145. } while (flash_fmc & FMC_MERASE);
  1146. }
  1147. return ERROR_OK;
  1148. }
  1149. COMMAND_HANDLER(stellaris_handle_mass_erase_command)
  1150. {
  1151. int i;
  1152. if (CMD_ARGC < 1)
  1153. return ERROR_COMMAND_SYNTAX_ERROR;
  1154. struct flash_bank *bank;
  1155. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
  1156. if (ERROR_OK != retval)
  1157. return retval;
  1158. if (stellaris_mass_erase(bank) == ERROR_OK) {
  1159. /* set all sectors as erased */
  1160. for (i = 0; i < bank->num_sectors; i++)
  1161. bank->sectors[i].is_erased = 1;
  1162. command_print(CMD_CTX, "stellaris mass erase complete");
  1163. } else
  1164. command_print(CMD_CTX, "stellaris mass erase failed");
  1165. return ERROR_OK;
  1166. }
  1167. /**
  1168. * Perform the Stellaris "Recovering a 'Locked' Device procedure.
  1169. * This performs a mass erase and then restores all nonvolatile registers
  1170. * (including USER_* registers and flash lock bits) to their defaults.
  1171. * Accordingly, flash can be reprogrammed, and JTAG can be used.
  1172. *
  1173. * NOTE that DustDevil parts (at least rev A0 silicon) have errata which
  1174. * can affect this operation if flash protection has been enabled.
  1175. */
  1176. COMMAND_HANDLER(stellaris_handle_recover_command)
  1177. {
  1178. struct flash_bank *bank;
  1179. int retval;
  1180. const char *tmp;
  1181. if (CMD_ARGC != 0)
  1182. return ERROR_COMMAND_SYNTAX_ERROR;
  1183. bank = get_flash_bank_by_num_noprobe(0);
  1184. if (!bank)
  1185. return ERROR_FAIL;
  1186. /* REVISIT ... it may be worth sanity checking that the AP is
  1187. * inactive before we start. ARM documents that switching a DP's
  1188. * mode while it's active can cause fault modes that need a power
  1189. * cycle to recover.
  1190. */
  1191. Jim_Eval_Named(CMD_CTX->interp->interp,
  1192. "catch { hla_command \"debug unlock\" }", 0, 0);
  1193. tmp = Jim_GetString(Jim_GetResult(CMD_CTX->interp->interp), NULL);
  1194. if (!strcmp(tmp, "0")) {
  1195. retval = ERROR_OK;
  1196. goto user_action;
  1197. }
  1198. /* assert SRST */
  1199. if (!(jtag_get_reset_config() & RESET_HAS_SRST)) {
  1200. LOG_ERROR("Can't recover Stellaris flash without SRST");
  1201. return ERROR_FAIL;
  1202. }
  1203. adapter_assert_reset();
  1204. for (int i = 0; i < 5; i++) {
  1205. retval = dap_to_swd(bank->target);
  1206. if (retval != ERROR_OK)
  1207. goto done;
  1208. retval = dap_to_jtag(bank->target);
  1209. if (retval != ERROR_OK)
  1210. goto done;
  1211. }
  1212. /* de-assert SRST */
  1213. adapter_deassert_reset();
  1214. retval = jtag_execute_queue();
  1215. /* wait 400+ msec ... OK, "1+ second" is simpler */
  1216. usleep(1000);
  1217. user_action:
  1218. /* USER INTERVENTION required for the power cycle
  1219. * Restarting OpenOCD is likely needed because of mode switching.
  1220. */
  1221. LOG_INFO("USER ACTION: "
  1222. "power cycle Stellaris chip, then restart OpenOCD.");
  1223. done:
  1224. return retval;
  1225. }
  1226. static const struct command_registration stellaris_exec_command_handlers[] = {
  1227. {
  1228. .name = "mass_erase",
  1229. .usage = "<bank>",
  1230. .handler = stellaris_handle_mass_erase_command,
  1231. .mode = COMMAND_EXEC,
  1232. .help = "erase entire device",
  1233. },
  1234. {
  1235. .name = "recover",
  1236. .handler = stellaris_handle_recover_command,
  1237. .mode = COMMAND_EXEC,
  1238. .usage = "",
  1239. .help = "recover (and erase) locked device",
  1240. },
  1241. COMMAND_REGISTRATION_DONE
  1242. };
  1243. static const struct command_registration stellaris_command_handlers[] = {
  1244. {
  1245. .name = "stellaris",
  1246. .mode = COMMAND_EXEC,
  1247. .help = "Stellaris flash command group",
  1248. .usage = "",
  1249. .chain = stellaris_exec_command_handlers,
  1250. },
  1251. COMMAND_REGISTRATION_DONE
  1252. };
  1253. struct flash_driver stellaris_flash = {
  1254. .name = "stellaris",
  1255. .commands = stellaris_command_handlers,
  1256. .flash_bank_command = stellaris_flash_bank_command,
  1257. .erase = stellaris_erase,
  1258. .protect = stellaris_protect,
  1259. .write = stellaris_write,
  1260. .read = default_flash_read,
  1261. .probe = stellaris_probe,
  1262. .auto_probe = stellaris_probe,
  1263. .erase_check = default_flash_blank_check,
  1264. .protect_check = stellaris_protect_check,
  1265. .info = get_stellaris_info,
  1266. };