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.
 
 
 
 
 
 

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