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.
 
 
 
 
 
 

847 lines
24 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2006 by Magnus Lundin *
  3. * lundin@mlu.mine.nu *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. /***************************************************************************
  21. * STELLARIS is tested on LM3S811
  22. *
  23. *
  24. *
  25. ***************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #include "replacements.h"
  30. #include "stellaris.h"
  31. #include "cortex_m3.h"
  32. #include "flash.h"
  33. #include "target.h"
  34. #include "log.h"
  35. #include "binarybuffer.h"
  36. #include "types.h"
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <unistd.h>
  40. int stellaris_register_commands(struct command_context_s *cmd_ctx);
  41. int stellaris_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
  42. int stellaris_erase(struct flash_bank_s *bank, int first, int last);
  43. int stellaris_protect(struct flash_bank_s *bank, int set, int first, int last);
  44. int stellaris_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count);
  45. int stellaris_probe(struct flash_bank_s *bank);
  46. int stellaris_erase_check(struct flash_bank_s *bank);
  47. int stellaris_protect_check(struct flash_bank_s *bank);
  48. int stellaris_info(struct flash_bank_s *bank, char *buf, int buf_size);
  49. u32 stellaris_get_flash_status(flash_bank_t *bank);
  50. void stellaris_set_flash_mode(flash_bank_t *bank,int mode);
  51. u32 stellaris_wait_status_busy(flash_bank_t *bank, u32 waitbits, int timeout);
  52. flash_driver_t stellaris_flash =
  53. {
  54. .name = "stellaris",
  55. .register_commands = stellaris_register_commands,
  56. .flash_bank_command = stellaris_flash_bank_command,
  57. .erase = stellaris_erase,
  58. .protect = stellaris_protect,
  59. .write = stellaris_write,
  60. .probe = stellaris_probe,
  61. .erase_check = stellaris_erase_check,
  62. .protect_check = stellaris_protect_check,
  63. .info = stellaris_info
  64. };
  65. struct {
  66. u32 partno;
  67. char *partname;
  68. } StellarisParts[] =
  69. {
  70. {0x01,"LM3S101"},
  71. {0x02,"LM3S102"},
  72. {0x11,"LM3S301"},
  73. {0x12,"LM3S310"},
  74. {0x13,"LM3S315"},
  75. {0x14,"LM3S316"},
  76. {0x15,"LM3S328"},
  77. {0x21,"LM3S601"},
  78. {0x22,"LM3S610"},
  79. {0x23,"LM3S611"},
  80. {0x24,"LM3S612"},
  81. {0x25,"LM3S613"},
  82. {0x26,"LM3S615"},
  83. {0x27,"LM3S628"},
  84. {0x31,"LM3S801"},
  85. {0x32,"LM3S811"},
  86. {0x33,"LM3S812"},
  87. {0x34,"LM3S815"},
  88. {0x35,"LM3S828"},
  89. {0x51,"LM3S2110"},
  90. {0x84,"LM3S2139"},
  91. {0xa2,"LM3S2410"},
  92. {0x59,"LM3S2412"},
  93. {0x56,"LM3S2432"},
  94. {0x5a,"LM3S2533"},
  95. {0x57,"LM3S2620"},
  96. {0x85,"LM3S2637"},
  97. {0x53,"LM3S2651"},
  98. {0xa4,"LM3S2730"},
  99. {0x52,"LM3S2739"},
  100. {0x54,"LM3S2939"},
  101. {0x8f,"LM3S2948"},
  102. {0x58,"LM3S2950"},
  103. {0x55,"LM3S2965"},
  104. {0xa1,"LM3S6100"},
  105. {0x74,"LM3S6110"},
  106. {0xa5,"LM3S6420"},
  107. {0x82,"LM3S6422"},
  108. {0x75,"LM3S6432"},
  109. {0x71,"LM3S6610"},
  110. {0x83,"LM3S6633"},
  111. {0x8b,"LM3S6637"},
  112. {0xa3,"LM3S6730"},
  113. {0x89,"LM3S6938"},
  114. {0x78,"LM3S6952"},
  115. {0x73,"LM3S6965"},
  116. {0,"Unknown part"}
  117. };
  118. /***************************************************************************
  119. * openocd command interface *
  120. ***************************************************************************/
  121. int stellaris_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
  122. {
  123. stellaris_flash_bank_t *stellaris_info;
  124. if (argc < 6)
  125. {
  126. WARNING("incomplete flash_bank stellaris configuration");
  127. return ERROR_FLASH_BANK_INVALID;
  128. }
  129. stellaris_info = calloc(sizeof(stellaris_flash_bank_t),1);
  130. bank->base = 0x0;
  131. bank->driver_priv = stellaris_info;
  132. stellaris_info->target_name ="Unknown target";
  133. stellaris_info->target = get_target_by_num(strtoul(args[5], NULL, 0));
  134. if (!stellaris_info->target)
  135. {
  136. ERROR("no target '%s' configured", args[5]);
  137. exit(-1);
  138. }
  139. /* part wasn't probed for info yet */
  140. stellaris_info->did1 = 0;
  141. /* TODO Use an optional main oscillator clock rate in kHz from arg[6] */
  142. return ERROR_OK;
  143. }
  144. int stellaris_register_commands(struct command_context_s *cmd_ctx)
  145. {
  146. /*
  147. command_t *stellaris_cmd = register_command(cmd_ctx, NULL, "stellaris", NULL, COMMAND_ANY, NULL);
  148. register_command(cmd_ctx, stellaris_cmd, "gpnvm", stellaris_handle_gpnvm_command, COMMAND_EXEC,
  149. "stellaris gpnvm <num> <bit> set|clear, set or clear stellaris gpnvm bit");
  150. */
  151. return ERROR_OK;
  152. }
  153. int stellaris_info(struct flash_bank_s *bank, char *buf, int buf_size)
  154. {
  155. int printed;
  156. stellaris_flash_bank_t *stellaris_info = bank->driver_priv;
  157. stellaris_read_part_info(bank);
  158. if (stellaris_info->did1 == 0)
  159. {
  160. printed = snprintf(buf, buf_size, "Cannot identify target as a Stellaris\n");
  161. buf += printed;
  162. buf_size -= printed;
  163. return ERROR_FLASH_OPERATION_FAILED;
  164. }
  165. printed = snprintf(buf, buf_size, "\nLMI Stellaris information: Chip is class %i %s v%c.%i\n",
  166. (stellaris_info->did0>>16)&0xff, stellaris_info->target_name,
  167. 'A' + (stellaris_info->did0>>8)&0xFF, (stellaris_info->did0)&0xFF);
  168. buf += printed;
  169. buf_size -= printed;
  170. printed = snprintf(buf, buf_size, "did1: 0x%8.8x, arch: 0x%4.4x, eproc: %s, ramsize:%ik, flashsize: %ik\n",
  171. stellaris_info->did1, stellaris_info->did1, "ARMV7M", (1+(stellaris_info->dc0>>16)&0xFFFF)/4, (1+stellaris_info->dc0&0xFFFF)*2);
  172. buf += printed;
  173. buf_size -= printed;
  174. printed = snprintf(buf, buf_size, "master clock(estimated): %ikHz, rcc is 0x%x \n", stellaris_info->mck_freq / 1000, stellaris_info->rcc);
  175. buf += printed;
  176. buf_size -= printed;
  177. if (stellaris_info->num_lockbits>0) {
  178. printed = snprintf(buf, buf_size, "pagesize: %i, lockbits: %i 0x%4.4x, pages in lock region: %i \n", stellaris_info->pagesize, stellaris_info->num_lockbits, stellaris_info->lockbits,stellaris_info->num_pages/stellaris_info->num_lockbits);
  179. buf += printed;
  180. buf_size -= printed;
  181. }
  182. return ERROR_OK;
  183. }
  184. /***************************************************************************
  185. * chip identification and status *
  186. ***************************************************************************/
  187. u32 stellaris_get_flash_status(flash_bank_t *bank)
  188. {
  189. stellaris_flash_bank_t *stellaris_info = bank->driver_priv;
  190. target_t *target = stellaris_info->target;
  191. u32 fmc;
  192. target_read_u32(target, FLASH_CONTROL_BASE|FLASH_FMC, &fmc);
  193. return fmc;
  194. }
  195. /** Read clock configuration and set stellaris_info->usec_clocks*/
  196. void stellaris_read_clock_info(flash_bank_t *bank)
  197. {
  198. stellaris_flash_bank_t *stellaris_info = bank->driver_priv;
  199. target_t *target = stellaris_info->target;
  200. u32 rcc, pllcfg, sysdiv, usesysdiv, bypass, oscsrc;
  201. unsigned long tmp, mainfreq;
  202. target_read_u32(target, SCB_BASE|RCC, &rcc);
  203. DEBUG("Stellaris RCC %x",rcc);
  204. target_read_u32(target, SCB_BASE|PLLCFG, &pllcfg);
  205. DEBUG("Stellaris PLLCFG %x",pllcfg);
  206. stellaris_info->rcc = rcc;
  207. sysdiv = (rcc>>23)&0xF;
  208. usesysdiv = (rcc>>22)&0x1;
  209. bypass = (rcc>>11)&0x1;
  210. oscsrc = (rcc>>4)&0x3;
  211. /* xtal = (rcc>>6)&0xF; */
  212. switch (oscsrc)
  213. {
  214. case 0:
  215. mainfreq = 6000000; /* Default xtal */
  216. break;
  217. case 1:
  218. mainfreq = 22500000; /* Internal osc. 15 MHz +- 50% */
  219. break;
  220. case 2:
  221. mainfreq = 5625000; /* Internal osc. / 4 */
  222. break;
  223. case 3:
  224. WARNING("Invalid oscsrc (3) in rcc register");
  225. mainfreq = 6000000;
  226. break;
  227. }
  228. if (!bypass)
  229. mainfreq = 200000000; /* PLL out frec */
  230. if (usesysdiv)
  231. stellaris_info->mck_freq = mainfreq/(1+sysdiv);
  232. else
  233. stellaris_info->mck_freq = mainfreq;
  234. /* Forget old flash timing */
  235. stellaris_set_flash_mode(bank,0);
  236. }
  237. /* Setup the timimg registers */
  238. void stellaris_set_flash_mode(flash_bank_t *bank,int mode)
  239. {
  240. stellaris_flash_bank_t *stellaris_info = bank->driver_priv;
  241. target_t *target = stellaris_info->target;
  242. u32 usecrl = (stellaris_info->mck_freq/1000000ul-1);
  243. DEBUG("usecrl = %i",usecrl);
  244. target_write_u32(target, SCB_BASE|USECRL , usecrl);
  245. }
  246. u32 stellaris_wait_status_busy(flash_bank_t *bank, u32 waitbits, int timeout)
  247. {
  248. u32 status;
  249. /* Stellaris waits for cmdbit to clear */
  250. while (((status = stellaris_get_flash_status(bank)) & waitbits) && (timeout-- > 0))
  251. {
  252. DEBUG("status: 0x%x", status);
  253. usleep(1000);
  254. }
  255. /* Flash errors are reflected in the FLASH_CRIS register */
  256. return status;
  257. }
  258. /* Send one command to the flash controller */
  259. int stellaris_flash_command(struct flash_bank_s *bank,u8 cmd,u16 pagen)
  260. {
  261. u32 fmc;
  262. stellaris_flash_bank_t *stellaris_info = bank->driver_priv;
  263. target_t *target = stellaris_info->target;
  264. fmc = FMC_WRKEY | cmd;
  265. target_write_u32(target, FLASH_CONTROL_BASE|FLASH_FMC, fmc);
  266. DEBUG("Flash command: 0x%x", fmc);
  267. if (stellaris_wait_status_busy(bank, cmd, 100))
  268. {
  269. return ERROR_FLASH_OPERATION_FAILED;
  270. }
  271. return ERROR_OK;
  272. }
  273. /* Read device id register, main clock frequency register and fill in driver info structure */
  274. int stellaris_read_part_info(struct flash_bank_s *bank)
  275. {
  276. stellaris_flash_bank_t *stellaris_info = bank->driver_priv;
  277. target_t *target = stellaris_info->target;
  278. u32 did0,did1, ver, fam, status;
  279. int i;
  280. /* Read and parse chip identification register */
  281. target_read_u32(target, SCB_BASE|DID0, &did0);
  282. target_read_u32(target, SCB_BASE|DID1, &did1);
  283. target_read_u32(target, SCB_BASE|DC0, &stellaris_info->dc0);
  284. target_read_u32(target, SCB_BASE|DC1, &stellaris_info->dc1);
  285. DEBUG("did0 0x%x, did1 0x%x, dc0 0x%x, dc1 0x%x",did0, did1, stellaris_info->dc0,stellaris_info->dc1);
  286. ver = did0 >> 28;
  287. if((ver != 0) && (ver != 1))
  288. {
  289. WARNING("Unknown did0 version, cannot identify target");
  290. return ERROR_FLASH_OPERATION_FAILED;
  291. }
  292. ver = did1 >> 28;
  293. fam = (did1 >> 24) & 0xF;
  294. if(((ver != 0) && (ver != 1)) || (fam != 0))
  295. {
  296. WARNING("Unknown did1 version/family, cannot positively identify target as a Stellaris");
  297. }
  298. if (did1 == 0)
  299. {
  300. WARNING("Cannot identify target as a Stellaris");
  301. return ERROR_FLASH_OPERATION_FAILED;
  302. }
  303. for (i=0;StellarisParts[i].partno;i++)
  304. {
  305. if (StellarisParts[i].partno==((did1>>16)&0xFF))
  306. break;
  307. }
  308. stellaris_info->target_name = StellarisParts[i].partname;
  309. stellaris_info->did0 = did0;
  310. stellaris_info->did1 = did1;
  311. stellaris_info->num_lockbits = 1+stellaris_info->dc0&0xFFFF;
  312. stellaris_info->num_pages = 2*(1+stellaris_info->dc0&0xFFFF);
  313. stellaris_info->pagesize = 1024;
  314. bank->size = 1024*stellaris_info->num_pages;
  315. stellaris_info->pages_in_lockregion = 2;
  316. target_read_u32(target, SCB_BASE|FMPPE, &stellaris_info->lockbits);
  317. // Read main and master clock freqency register
  318. stellaris_read_clock_info(bank);
  319. status = stellaris_get_flash_status(bank);
  320. WARNING("stellaris flash only tested for LM3S811 series");
  321. return ERROR_OK;
  322. }
  323. /***************************************************************************
  324. * flash operations *
  325. ***************************************************************************/
  326. int stellaris_erase_check(struct flash_bank_s *bank)
  327. {
  328. stellaris_flash_bank_t *stellaris_info = bank->driver_priv;
  329. target_t *target = stellaris_info->target;
  330. int i;
  331. /* */
  332. return ERROR_OK;
  333. }
  334. int stellaris_protect_check(struct flash_bank_s *bank)
  335. {
  336. u32 status;
  337. stellaris_flash_bank_t *stellaris_info = bank->driver_priv;
  338. target_t *target = stellaris_info->target;
  339. if (stellaris_info->did1 == 0)
  340. {
  341. stellaris_read_part_info(bank);
  342. }
  343. if (stellaris_info->did1 == 0)
  344. {
  345. WARNING("Cannot identify target as an AT91SAM");
  346. return ERROR_FLASH_OPERATION_FAILED;
  347. }
  348. status = stellaris_get_flash_status(bank);
  349. stellaris_info->lockbits = status >> 16;
  350. return ERROR_OK;
  351. }
  352. int stellaris_erase(struct flash_bank_s *bank, int first, int last)
  353. {
  354. int banknr;
  355. u32 flash_fmc, flash_cris;
  356. stellaris_flash_bank_t *stellaris_info = bank->driver_priv;
  357. target_t *target = stellaris_info->target;
  358. if (stellaris_info->target->state != TARGET_HALTED)
  359. {
  360. return ERROR_TARGET_NOT_HALTED;
  361. }
  362. if (stellaris_info->did1 == 0)
  363. {
  364. stellaris_read_part_info(bank);
  365. }
  366. if (stellaris_info->did1 == 0)
  367. {
  368. WARNING("Cannot identify target as Stellaris");
  369. return ERROR_FLASH_OPERATION_FAILED;
  370. }
  371. if ((first < 0) || (last < first) || (last >= stellaris_info->num_pages))
  372. {
  373. return ERROR_FLASH_SECTOR_INVALID;
  374. }
  375. /* Configure the flash controller timing */
  376. stellaris_read_clock_info(bank);
  377. stellaris_set_flash_mode(bank,0);
  378. /* Clear and disable flash programming interrupts */
  379. target_write_u32(target, FLASH_CIM, 0);
  380. target_write_u32(target, FLASH_MISC, PMISC|AMISC);
  381. if ((first == 0) && (last == (stellaris_info->num_pages-1)))
  382. {
  383. target_write_u32(target, FLASH_FMA, 0);
  384. target_write_u32(target, FLASH_FMC, FMC_WRKEY | FMC_MERASE);
  385. /* Wait until erase complete */
  386. do
  387. {
  388. target_read_u32(target, FLASH_FMC, &flash_fmc);
  389. }
  390. while(flash_fmc & FMC_MERASE);
  391. /* if device has > 128k, then second erase cycle is needed */
  392. if(stellaris_info->num_pages * stellaris_info->pagesize > 0x20000)
  393. {
  394. target_write_u32(target, FLASH_FMA, 0x20000);
  395. target_write_u32(target, FLASH_FMC, FMC_WRKEY | FMC_MERASE);
  396. /* Wait until erase complete */
  397. do
  398. {
  399. target_read_u32(target, FLASH_FMC, &flash_fmc);
  400. }
  401. while(flash_fmc & FMC_MERASE);
  402. }
  403. return ERROR_OK;
  404. }
  405. for (banknr=first;banknr<=last;banknr++)
  406. {
  407. /* Address is first word in page */
  408. target_write_u32(target, FLASH_FMA, banknr*stellaris_info->pagesize);
  409. /* Write erase command */
  410. target_write_u32(target, FLASH_FMC, FMC_WRKEY | FMC_ERASE);
  411. /* Wait until erase complete */
  412. do
  413. {
  414. target_read_u32(target, FLASH_FMC, &flash_fmc);
  415. }
  416. while(flash_fmc & FMC_ERASE);
  417. /* Check acess violations */
  418. target_read_u32(target, FLASH_CRIS, &flash_cris);
  419. if(flash_cris & (AMASK))
  420. {
  421. WARNING("Error erasing flash page %i, flash_cris 0x%x", banknr, flash_cris);
  422. target_write_u32(target, FLASH_CRIS, 0);
  423. return ERROR_FLASH_OPERATION_FAILED;
  424. }
  425. }
  426. return ERROR_OK;
  427. }
  428. int stellaris_protect(struct flash_bank_s *bank, int set, int first, int last)
  429. {
  430. u32 cmd, fmppe, flash_fmc, flash_cris;
  431. int lockregion;
  432. stellaris_flash_bank_t *stellaris_info = bank->driver_priv;
  433. target_t *target = stellaris_info->target;
  434. if (stellaris_info->target->state != TARGET_HALTED)
  435. {
  436. return ERROR_TARGET_NOT_HALTED;
  437. }
  438. if ((first < 0) || (last < first) || (last >= stellaris_info->num_lockbits))
  439. {
  440. return ERROR_FLASH_SECTOR_INVALID;
  441. }
  442. if (stellaris_info->did1 == 0)
  443. {
  444. stellaris_read_part_info(bank);
  445. }
  446. if (stellaris_info->did1 == 0)
  447. {
  448. WARNING("Cannot identify target as an Stellaris MCU");
  449. return ERROR_FLASH_OPERATION_FAILED;
  450. }
  451. /* Configure the flash controller timing */
  452. stellaris_read_clock_info(bank);
  453. stellaris_set_flash_mode(bank,0);
  454. fmppe = stellaris_info->lockbits;
  455. for (lockregion=first;lockregion<=last;lockregion++)
  456. {
  457. if (set)
  458. fmppe &= ~(1<<lockregion);
  459. else
  460. fmppe |= (1<<lockregion);
  461. }
  462. /* Clear and disable flash programming interrupts */
  463. target_write_u32(target, FLASH_CIM, 0);
  464. target_write_u32(target, FLASH_MISC, PMISC|AMISC);
  465. DEBUG("fmppe 0x%x",fmppe);
  466. target_write_u32(target, SCB_BASE|FMPPE, fmppe);
  467. /* Commit FMPPE */
  468. target_write_u32(target, FLASH_FMA, 1);
  469. /* Write commit command */
  470. /* TODO safety check, sice this cannot be undone */
  471. WARNING("Flash protection cannot be removed once commited, commit is NOT executed !");
  472. /* target_write_u32(target, FLASH_FMC, FMC_WRKEY | FMC_COMT); */
  473. /* Wait until erase complete */
  474. do
  475. {
  476. target_read_u32(target, FLASH_FMC, &flash_fmc);
  477. }
  478. while(flash_fmc & FMC_COMT);
  479. /* Check acess violations */
  480. target_read_u32(target, FLASH_CRIS, &flash_cris);
  481. if(flash_cris & (AMASK))
  482. {
  483. WARNING("Error setting flash page protection, flash_cris 0x%x", flash_cris);
  484. target_write_u32(target, FLASH_CRIS, 0);
  485. return ERROR_FLASH_OPERATION_FAILED;
  486. }
  487. target_read_u32(target, SCB_BASE|FMPPE, &stellaris_info->lockbits);
  488. return ERROR_OK;
  489. }
  490. u8 stellaris_write_code[] =
  491. {
  492. /* Call with :
  493. r0 = buffer address
  494. r1 = destination address
  495. r2 = bytecount (in) - endaddr (work)
  496. r3 = pFLASH_CTRL_BASE
  497. r4 = FLASHWRITECMD
  498. r5 = #1
  499. r6 = scratch
  500. r7
  501. */
  502. 0x07,0x4B, /* ldr r3,pFLASH_CTRL_BASE */
  503. 0x08,0x4C, /* ldr r4,FLASHWRITECMD */
  504. 0x01,0x25, /* movs r5, 1 */
  505. 0x00,0x26, /* movs r6, #0 */
  506. /* mainloop: */
  507. 0x19,0x60, /* str r1, [r3, #0] */
  508. 0x87,0x59, /* ldr r7, [r0, r6] */
  509. 0x5F,0x60, /* str r7, [r3, #4] */
  510. 0x9C,0x60, /* str r4, [r3, #8] */
  511. /* waitloop: */
  512. 0x9F,0x68, /* ldr r7, [r3, #8] */
  513. 0x2F,0x42, /* tst r7, r5 */
  514. 0xFC,0xD1, /* bne waitloop */
  515. 0x04,0x31, /* adds r1, r1, #4 */
  516. 0x04,0x36, /* adds r6, r6, #4 */
  517. 0x96,0x42, /* cmp r6, r2 */
  518. 0xF4,0xD1, /* bne mainloop */
  519. 0x00,0xBE, /* bkpt #0 */
  520. /* pFLASH_CTRL_BASE: */
  521. 0x00,0xD0,0x0F,0x40, /* .word 0x400FD000 */
  522. /* FLASHWRITECMD: */
  523. 0x01,0x00,0x42,0xA4 /* .word 0xA4420001 */
  524. };
  525. int stellaris_write_block(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 wcount)
  526. {
  527. stellaris_flash_bank_t *stellaris_info = bank->driver_priv;
  528. target_t *target = stellaris_info->target;
  529. u32 buffer_size = 8192;
  530. working_area_t *source;
  531. working_area_t *write_algorithm;
  532. u32 address = bank->base + offset;
  533. reg_param_t reg_params[8];
  534. armv7m_algorithm_t armv7m_info;
  535. int retval;
  536. DEBUG("(bank=%08X buffer=%08X offset=%08X wcount=%08X)",
  537. bank, buffer, offset, wcount);
  538. /* flash write code */
  539. if (target_alloc_working_area(target, sizeof(stellaris_write_code), &write_algorithm) != ERROR_OK)
  540. {
  541. WARNING("no working area available, can't do block memory writes");
  542. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  543. };
  544. target_write_buffer(target, write_algorithm->address, sizeof(stellaris_write_code), stellaris_write_code);
  545. /* memory buffer */
  546. while (target_alloc_working_area(target, buffer_size, &source) != ERROR_OK)
  547. {
  548. DEBUG("called target_alloc_working_area(target=%08X buffer_size=%08X source=%08X)",
  549. target, buffer_size, source);
  550. buffer_size /= 2;
  551. if (buffer_size <= 256)
  552. {
  553. /* if we already allocated the writing code, but failed to get a buffer, free the algorithm */
  554. if (write_algorithm)
  555. target_free_working_area(target, write_algorithm);
  556. WARNING("no large enough working area available, can't do block memory writes");
  557. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  558. }
  559. };
  560. armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
  561. armv7m_info.core_mode = ARMV7M_MODE_ANY;
  562. armv7m_info.core_state = ARMV7M_STATE_THUMB;
  563. init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
  564. init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
  565. init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT);
  566. init_reg_param(&reg_params[3], "r3", 32, PARAM_OUT);
  567. init_reg_param(&reg_params[4], "r4", 32, PARAM_OUT);
  568. init_reg_param(&reg_params[5], "r5", 32, PARAM_OUT);
  569. init_reg_param(&reg_params[6], "r6", 32, PARAM_OUT);
  570. init_reg_param(&reg_params[7], "r7", 32, PARAM_OUT);
  571. while (wcount > 0)
  572. {
  573. u32 thisrun_count = (wcount > (buffer_size / 4)) ? (buffer_size / 4) : wcount;
  574. target_write_buffer(target, source->address, thisrun_count * 4, buffer);
  575. buf_set_u32(reg_params[0].value, 0, 32, source->address);
  576. buf_set_u32(reg_params[1].value, 0, 32, address);
  577. buf_set_u32(reg_params[2].value, 0, 32, 4*thisrun_count);
  578. WARNING("Algorithm flash write %i words to 0x%x, %i remaining",thisrun_count,address, wcount);
  579. DEBUG("Algorithm flash write %i words to 0x%x, %i remaining",thisrun_count,address, wcount);
  580. if ((retval = target->type->run_algorithm(target, 0, NULL, 3, reg_params, write_algorithm->address, write_algorithm->address + sizeof(stellaris_write_code)-10, 10000, &armv7m_info)) != ERROR_OK)
  581. {
  582. ERROR("error executing stellaris flash write algorithm");
  583. target_free_working_area(target, source);
  584. destroy_reg_param(&reg_params[0]);
  585. destroy_reg_param(&reg_params[1]);
  586. destroy_reg_param(&reg_params[2]);
  587. return ERROR_FLASH_OPERATION_FAILED;
  588. }
  589. buffer += thisrun_count * 4;
  590. address += thisrun_count * 4;
  591. wcount -= thisrun_count;
  592. }
  593. target_free_working_area(target, write_algorithm);
  594. target_free_working_area(target, source);
  595. destroy_reg_param(&reg_params[0]);
  596. destroy_reg_param(&reg_params[1]);
  597. destroy_reg_param(&reg_params[2]);
  598. destroy_reg_param(&reg_params[3]);
  599. destroy_reg_param(&reg_params[4]);
  600. destroy_reg_param(&reg_params[5]);
  601. destroy_reg_param(&reg_params[6]);
  602. destroy_reg_param(&reg_params[7]);
  603. return ERROR_OK;
  604. }
  605. int stellaris_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
  606. {
  607. stellaris_flash_bank_t *stellaris_info = bank->driver_priv;
  608. target_t *target = stellaris_info->target;
  609. u32 dst_min_alignment, wcount, bytes_remaining = count;
  610. u32 address = offset;
  611. u32 fcr,flash_cris,flash_fmc;
  612. u32 retval;
  613. DEBUG("(bank=%08X buffer=%08X offset=%08X count=%08X)",
  614. bank, buffer, offset, count);
  615. if (stellaris_info->target->state != TARGET_HALTED)
  616. {
  617. return ERROR_TARGET_NOT_HALTED;
  618. }
  619. if (stellaris_info->did1 == 0)
  620. {
  621. stellaris_read_part_info(bank);
  622. }
  623. if (stellaris_info->did1 == 0)
  624. {
  625. WARNING("Cannot identify target as a Stellaris processor");
  626. return ERROR_FLASH_OPERATION_FAILED;
  627. }
  628. if((offset & 3) || (count & 3))
  629. {
  630. WARNING("offset size must be word aligned");
  631. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  632. }
  633. if (offset + count > bank->size)
  634. return ERROR_FLASH_DST_OUT_OF_BANK;
  635. /* Configure the flash controller timing */
  636. stellaris_read_clock_info(bank);
  637. stellaris_set_flash_mode(bank,0);
  638. /* Clear and disable flash programming interrupts */
  639. target_write_u32(target, FLASH_CIM, 0);
  640. target_write_u32(target, FLASH_MISC, PMISC|AMISC);
  641. /* multiple words to be programmed? */
  642. if (count > 0)
  643. {
  644. /* try using a block write */
  645. if ((retval = stellaris_write_block(bank, buffer, offset, count/4)) != ERROR_OK)
  646. {
  647. if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
  648. {
  649. /* if block write failed (no sufficient working area),
  650. * we use normal (slow) single dword accesses */
  651. WARNING("couldn't use block writes, falling back to single memory accesses");
  652. }
  653. else if (retval == ERROR_FLASH_OPERATION_FAILED)
  654. {
  655. /* if an error occured, we examine the reason, and quit */
  656. target_read_u32(target, FLASH_CRIS, &flash_cris);
  657. ERROR("flash writing failed with CRIS: 0x%x", flash_cris);
  658. return ERROR_FLASH_OPERATION_FAILED;
  659. }
  660. }
  661. else
  662. {
  663. buffer += count * 4;
  664. address += count * 4;
  665. count = 0;
  666. }
  667. }
  668. while(count>0)
  669. {
  670. if (!(address&0xff)) DEBUG("0x%x",address);
  671. /* Program one word */
  672. target_write_u32(target, FLASH_FMA, address);
  673. target_write_buffer(target, FLASH_FMD, 4, buffer);
  674. target_write_u32(target, FLASH_FMC, FMC_WRKEY | FMC_WRITE);
  675. //DEBUG("0x%x 0x%x 0x%x",address,buf_get_u32(buffer, 0, 32),FMC_WRKEY | FMC_WRITE);
  676. /* Wait until write complete */
  677. do
  678. {
  679. target_read_u32(target, FLASH_FMC, &flash_fmc);
  680. }
  681. while(flash_fmc & FMC_WRITE);
  682. buffer += 4;
  683. address += 4;
  684. count -= 4;
  685. }
  686. /* Check acess violations */
  687. target_read_u32(target, FLASH_CRIS, &flash_cris);
  688. if(flash_cris & (AMASK))
  689. {
  690. DEBUG("flash_cris 0x%x", flash_cris);
  691. return ERROR_FLASH_OPERATION_FAILED;
  692. }
  693. return ERROR_OK;
  694. }
  695. int stellaris_probe(struct flash_bank_s *bank)
  696. {
  697. /* we can't probe on an stellaris
  698. * if this is an stellaris, it has the configured flash
  699. */
  700. stellaris_flash_bank_t *stellaris_info = bank->driver_priv;
  701. if (stellaris_info->did1 == 0)
  702. {
  703. stellaris_read_part_info(bank);
  704. }
  705. if (stellaris_info->did1 == 0)
  706. {
  707. WARNING("Cannot identify target as a LMI Stellaris");
  708. return ERROR_FLASH_OPERATION_FAILED;
  709. }
  710. return ERROR_OK;
  711. }