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.
 
 
 
 
 
 

711 lines
20 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2013 by Andrey Yurovsky *
  3. * Andrey Yurovsky <yurovsky@gmail.com> *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "imp.h"
  24. #include <target/cortex_m.h>
  25. /* At this time, the SAM4L Flash is available in these capacities:
  26. * ATSAM4Lx4xx: 256KB (512 pages)
  27. * ATSAM4Lx2xx: 128KB (256 pages)
  28. * ATSAM4Lx8xx: 512KB (1024 pages)
  29. */
  30. /* There are 16 lockable regions regardless of overall capacity. The number
  31. * of pages per sector is therefore dependant on capacity. */
  32. #define SAM4L_NUM_SECTORS 16
  33. /* Locations in memory map */
  34. #define SAM4L_FLASH ((uint32_t)0x00000000) /* Flash region */
  35. #define SAM4L_FLASH_USER 0x00800000 /* Flash user page region */
  36. #define SAM4L_FLASHCALW 0x400A0000 /* Flash controller */
  37. #define SAM4L_CHIPID 0x400E0740 /* Chip Identification */
  38. /* Offsets from SAM4L_FLASHCALW */
  39. #define SAM4L_FCR 0x00 /* Flash Control Register (RW) */
  40. #define SAM4L_FCMD 0x04 /* Flash Command Register (RW) */
  41. #define SAM4L_FSR 0x08 /* Flash Status Register (RO) */
  42. #define SAM4L_FPR 0x0C /* Flash Parameter Register (RO) */
  43. #define SAM4L_FVR 0x10 /* Flash Version Register (RO) */
  44. #define SAM4L_FGPFRHI 0x14 /* Flash General Purpose Register High (RO) */
  45. #define SAM4L_FGPFRLO 0x18 /* Flash General Purpose Register Low (RO) */
  46. /* Offsets from SAM4L_CHIPID */
  47. #define SAM4L_CIDR 0x00 /* Chip ID Register (RO) */
  48. #define SAM4L_EXID 0x04 /* Chip ID Extension Register (RO) */
  49. /* Flash commands (for SAM4L_FCMD), see Table 14-5 */
  50. #define SAM4L_FCMD_NOP 0 /* No Operation */
  51. #define SAM4L_FCMD_WP 1 /* Write Page */
  52. #define SAM4L_FCMD_EP 2 /* Erase Page */
  53. #define SAM4L_FCMD_CPB 3 /* Clear Page Buffer */
  54. #define SAM4L_FCMD_LP 4 /* Lock region containing given page */
  55. #define SAM4L_FCMD_UP 5 /* Unlock region containing given page */
  56. #define SAM4L_FCMD_EA 6 /* Erase All */
  57. #define SAM4L_FCMD_WGPB 7 /* Write general-purpose fuse bit */
  58. #define SAM4L_FCMD_EGPB 8 /* Erase general-purpose fuse bit */
  59. #define SAM4L_FCMD_SSB 9 /* Set security fuses */
  60. #define SAM4L_FCMD_PGPFB 10 /* Program general-purpose fuse byte */
  61. #define SAM4L_FCMD_EAGPF 11 /* Erase all general-purpose fuse bits */
  62. #define SAM4L_FCMD_QPR 12 /* Quick page read */
  63. #define SAM4L_FCMD_WUP 13 /* Write user page */
  64. #define SAM4L_FCMD_EUP 14 /* Erase user page */
  65. #define SAM4L_FCMD_QPRUP 15 /* Quick page read (user page) */
  66. #define SAM4L_FCMD_HSEN 16 /* High speed mode enable */
  67. #define SAM4L_FCMD_HSDIS 17 /* High speed mode disable */
  68. #define SAM4L_FMCD_CMDKEY 0xA5UL /* 'key' to issue commands, see 14.10.2 */
  69. /* SMAP registers and bits */
  70. #define SMAP_BASE 0x400A3000
  71. #define SMAP_SCR (SMAP_BASE + 8)
  72. #define SMAP_SCR_HCR (1 << 1)
  73. struct sam4l_chip_info {
  74. uint32_t id;
  75. uint32_t exid;
  76. const char *name;
  77. };
  78. /* These are taken from Table 9-1 in 42023E-SAM-07/2013 */
  79. static const struct sam4l_chip_info sam4l_known_chips[] = {
  80. { 0xAB0B0AE0, 0x1400000F, "ATSAM4LC8C" },
  81. { 0xAB0A09E0, 0x0400000F, "ATSAM4LC4C" },
  82. { 0xAB0A07E0, 0x0400000F, "ATSAM4LC2C" },
  83. { 0xAB0B0AE0, 0x1300000F, "ATSAM4LC8B" },
  84. { 0xAB0A09E0, 0x0300000F, "ATSAM4LC4B" },
  85. { 0xAB0A07E0, 0x0300000F, "ATSAM4LC2B" },
  86. { 0xAB0B0AE0, 0x1200000F, "ATSAM4LC8A" },
  87. { 0xAB0A09E0, 0x0200000F, "ATSAM4LC4A" },
  88. { 0xAB0A07E0, 0x0200000F, "ATSAM4LC2A" },
  89. { 0xAB0B0AE0, 0x14000002, "ATSAM4LS8C" },
  90. { 0xAB0A09E0, 0x04000002, "ATSAM4LS4C" },
  91. { 0xAB0A07E0, 0x04000002, "ATSAM4LS2C" },
  92. { 0xAB0B0AE0, 0x13000002, "ATSAM4LS8B" },
  93. { 0xAB0A09E0, 0x03000002, "ATSAM4LS4B" },
  94. { 0xAB0A07E0, 0x03000002, "ATSAM4LS2B" },
  95. { 0xAB0B0AE0, 0x12000002, "ATSAM4LS8A" },
  96. { 0xAB0A09E0, 0x02000002, "ATSAM4LS4A" },
  97. { 0xAB0A07E0, 0x02000002, "ATSAM4LS2A" },
  98. };
  99. /* Meaning of SRAMSIZ field in CHIPID, see 9.3.1 in 42023E-SAM-07/2013 */
  100. static const uint16_t sam4l_ram_sizes[16] = { 48, 1, 2, 6, 24, 4, 80, 160, 8, 16, 32, 64, 128, 256, 96, 512 };
  101. /* Meaning of PSZ field in FPR, see 14.10.4 in 42023E-SAM-07/2013 */
  102. static const uint16_t sam4l_page_sizes[8] = { 32, 64, 128, 256, 512, 1024, 2048, 4096 };
  103. struct sam4l_info {
  104. const struct sam4l_chip_info *details;
  105. uint32_t flash_kb;
  106. uint32_t ram_kb;
  107. uint32_t page_size;
  108. int num_pages;
  109. int sector_size;
  110. int pages_per_sector;
  111. bool probed;
  112. struct target *target;
  113. struct sam4l_info *next;
  114. };
  115. static struct sam4l_info *sam4l_chips;
  116. static int sam4l_flash_wait_until_ready(struct target *target)
  117. {
  118. volatile unsigned int t = 0;
  119. uint32_t st;
  120. int res;
  121. /* Poll the status register until the FRDY bit is set */
  122. do {
  123. res = target_read_u32(target, SAM4L_FLASHCALW + SAM4L_FSR, &st);
  124. } while (res == ERROR_OK && !(st & (1<<0)) && ++t < 10);
  125. return res;
  126. }
  127. static int sam4l_flash_check_error(struct target *target, uint32_t *err)
  128. {
  129. uint32_t st;
  130. int res;
  131. res = target_read_u32(target, SAM4L_FLASHCALW + SAM4L_FSR, &st);
  132. if (res == ERROR_OK)
  133. *err = st & ((1<<3) | (1<<2)); /* grab PROGE and LOCKE bits */
  134. return res;
  135. }
  136. static int sam4l_flash_command(struct target *target, uint8_t cmd, int page)
  137. {
  138. int res;
  139. uint32_t fcmd;
  140. uint32_t err;
  141. res = sam4l_flash_wait_until_ready(target);
  142. if (res != ERROR_OK)
  143. return res;
  144. if (page >= 0) {
  145. /* Set the page number. For some commands, the page number is just an
  146. * argument (ex: fuse bit number). */
  147. fcmd = (SAM4L_FMCD_CMDKEY << 24) | ((page & 0xFFFF) << 8) | (cmd & 0x3F);
  148. } else {
  149. /* Reuse the page number that was read from the flash command register. */
  150. res = target_read_u32(target, SAM4L_FLASHCALW + SAM4L_FCMD, &fcmd);
  151. if (res != ERROR_OK)
  152. return res;
  153. fcmd &= ~0x3F; /* clear out the command code */
  154. fcmd |= (SAM4L_FMCD_CMDKEY << 24) | (cmd & 0x3F);
  155. }
  156. /* Send the command */
  157. res = target_write_u32(target, SAM4L_FLASHCALW + SAM4L_FCMD, fcmd);
  158. if (res != ERROR_OK)
  159. return res;
  160. res = sam4l_flash_check_error(target, &err);
  161. if (res != ERROR_OK)
  162. return res;
  163. if (err != 0)
  164. LOG_ERROR("%s got error status 0x%08" PRIx32, __func__, err);
  165. res = sam4l_flash_wait_until_ready(target);
  166. return res;
  167. }
  168. FLASH_BANK_COMMAND_HANDLER(sam4l_flash_bank_command)
  169. {
  170. struct sam4l_info *chip = sam4l_chips;
  171. while (chip) {
  172. if (chip->target == bank->target)
  173. break;
  174. chip = chip->next;
  175. }
  176. if (!chip) {
  177. /* Create a new chip */
  178. chip = calloc(1, sizeof(*chip));
  179. if (!chip)
  180. return ERROR_FAIL;
  181. chip->target = bank->target;
  182. chip->probed = false;
  183. bank->driver_priv = chip;
  184. /* Insert it into the chips list (at head) */
  185. chip->next = sam4l_chips;
  186. sam4l_chips = chip;
  187. }
  188. if (bank->base != SAM4L_FLASH) {
  189. LOG_ERROR("Address 0x%08" PRIx32 " invalid bank address (try 0x%08" PRIx32
  190. "[at91sam4l series] )",
  191. bank->base, SAM4L_FLASH);
  192. return ERROR_FAIL;
  193. }
  194. return ERROR_OK;
  195. }
  196. static const struct sam4l_chip_info *sam4l_find_chip_name(uint32_t id, uint32_t exid)
  197. {
  198. unsigned int i;
  199. id &= ~0xF;
  200. for (i = 0; i < ARRAY_SIZE(sam4l_known_chips); i++) {
  201. if (sam4l_known_chips[i].id == id && sam4l_known_chips[i].exid == exid)
  202. return &sam4l_known_chips[i];
  203. }
  204. return NULL;
  205. }
  206. static int sam4l_check_page_erased(struct flash_bank *bank, uint32_t pn,
  207. bool *is_erased_p)
  208. {
  209. int res;
  210. uint32_t st;
  211. /* Issue a quick page read to verify that we've erased this page */
  212. res = sam4l_flash_command(bank->target, SAM4L_FCMD_QPR, pn);
  213. if (res != ERROR_OK) {
  214. LOG_ERROR("Quick page read %" PRIu32 " failed", pn);
  215. return res;
  216. }
  217. /* Retrieve the flash status */
  218. res = target_read_u32(bank->target, SAM4L_FLASHCALW + SAM4L_FSR, &st);
  219. if (res != ERROR_OK) {
  220. LOG_ERROR("Couldn't read erase status");
  221. return res;
  222. }
  223. /* Is the page in question really erased? */
  224. *is_erased_p = !!(st & (1<<5));
  225. return ERROR_OK;
  226. }
  227. static int sam4l_probe(struct flash_bank *bank)
  228. {
  229. uint32_t id, exid, param;
  230. int res;
  231. struct sam4l_info *chip = (struct sam4l_info *)bank->driver_priv;
  232. if (chip->probed)
  233. return ERROR_OK;
  234. res = target_read_u32(bank->target, SAM4L_CHIPID + SAM4L_CIDR, &id);
  235. if (res != ERROR_OK) {
  236. LOG_ERROR("Couldn't read chip ID");
  237. return res;
  238. }
  239. res = target_read_u32(bank->target, SAM4L_CHIPID + SAM4L_EXID, &exid);
  240. if (res != ERROR_OK) {
  241. LOG_ERROR("Couldn't read extended chip ID");
  242. return res;
  243. }
  244. chip->details = sam4l_find_chip_name(id, exid);
  245. /* The RAM capacity is in a lookup table. */
  246. chip->ram_kb = sam4l_ram_sizes[0xF & (id >> 16)];
  247. switch (0xF & (id >> 8)) {
  248. case 0x07:
  249. chip->flash_kb = 128;
  250. break;
  251. case 0x09:
  252. chip->flash_kb = 256;
  253. break;
  254. case 0x0A:
  255. chip->flash_kb = 512;
  256. break;
  257. default:
  258. LOG_ERROR("Unknown flash size (chip ID is %08" PRIx32 "), assuming 128K", id);
  259. chip->flash_kb = 128;
  260. break;
  261. }
  262. /* Retrieve the Flash parameters */
  263. res = target_read_u32(bank->target, SAM4L_FLASHCALW + SAM4L_FPR, &param);
  264. if (res != ERROR_OK) {
  265. LOG_ERROR("Couldn't read Flash parameters");
  266. return res;
  267. }
  268. /* Fetch the page size from the parameter register. Technically the flash
  269. * capacity is there too though the manual mentions that not all parts will
  270. * have it set so we use the Chip ID capacity information instead. */
  271. chip->page_size = sam4l_page_sizes[0x7 & (param >> 8)];
  272. assert(chip->page_size);
  273. chip->num_pages = chip->flash_kb * 1024 / chip->page_size;
  274. chip->sector_size = (chip->flash_kb * 1024) / SAM4L_NUM_SECTORS;
  275. chip->pages_per_sector = chip->sector_size / chip->page_size;
  276. /* Make sure the bank size is correct */
  277. bank->size = chip->flash_kb * 1024;
  278. /* Allocate the sector table. */
  279. bank->num_sectors = SAM4L_NUM_SECTORS;
  280. bank->sectors = calloc(bank->num_sectors, (sizeof((bank->sectors)[0])));
  281. if (!bank->sectors)
  282. return ERROR_FAIL;
  283. /* Fill out the sector information: all SAM4L sectors are the same size and
  284. * there is always a fixed number of them. */
  285. for (int i = 0; i < bank->num_sectors; i++) {
  286. bank->sectors[i].size = chip->sector_size;
  287. bank->sectors[i].offset = i * chip->sector_size;
  288. /* mark as unknown */
  289. bank->sectors[i].is_erased = -1;
  290. bank->sectors[i].is_protected = -1;
  291. }
  292. /* Done */
  293. chip->probed = true;
  294. LOG_INFO("SAM4L MCU: %s (Rev %c) (%" PRIu32 "KB Flash with %d %" PRId32 "B pages, %" PRIu32 "KB RAM)",
  295. chip->details ? chip->details->name : "unknown", (char)('A' + (id & 0xF)),
  296. chip->flash_kb, chip->num_pages, chip->page_size, chip->ram_kb);
  297. return ERROR_OK;
  298. }
  299. static int sam4l_protect_check(struct flash_bank *bank)
  300. {
  301. int res;
  302. uint32_t st;
  303. struct sam4l_info *chip = (struct sam4l_info *)bank->driver_priv;
  304. if (bank->target->state != TARGET_HALTED) {
  305. LOG_ERROR("Target not halted");
  306. return ERROR_TARGET_NOT_HALTED;
  307. }
  308. if (!chip->probed) {
  309. if (sam4l_probe(bank) != ERROR_OK)
  310. return ERROR_FLASH_BANK_NOT_PROBED;
  311. }
  312. res = target_read_u32(bank->target, SAM4L_FLASHCALW + SAM4L_FSR, &st);
  313. if (res != ERROR_OK)
  314. return res;
  315. st >>= 16; /* There are 16 lock region bits in the upper half word */
  316. for (int i = 0; i < bank->num_sectors; i++)
  317. bank->sectors[i].is_protected = !!(st & (1<<i));
  318. return ERROR_OK;
  319. }
  320. static int sam4l_protect(struct flash_bank *bank, int set, int first, int last)
  321. {
  322. struct sam4l_info *chip = sam4l_chips;
  323. if (bank->target->state != TARGET_HALTED) {
  324. LOG_ERROR("Target not halted");
  325. return ERROR_TARGET_NOT_HALTED;
  326. }
  327. if (!chip->probed) {
  328. if (sam4l_probe(bank) != ERROR_OK)
  329. return ERROR_FLASH_BANK_NOT_PROBED;
  330. }
  331. /* Make sure the pages make sense. */
  332. if (first >= bank->num_sectors || last >= bank->num_sectors) {
  333. LOG_ERROR("Protect range %d - %d not valid (%d sectors total)", first, last,
  334. bank->num_sectors);
  335. return ERROR_FAIL;
  336. }
  337. /* Try to lock or unlock each sector in the range. This is done by locking
  338. * a region containing one page in that sector, we arbitrarily choose the 0th
  339. * page in the sector. */
  340. for (int i = first; i <= last; i++) {
  341. int res;
  342. res = sam4l_flash_command(bank->target,
  343. set ? SAM4L_FCMD_LP : SAM4L_FCMD_UP, i * chip->pages_per_sector);
  344. if (res != ERROR_OK) {
  345. LOG_ERROR("Can't %slock region containing page %d", set ? "" : "un", i);
  346. return res;
  347. }
  348. }
  349. return ERROR_OK;
  350. }
  351. static int sam4l_erase(struct flash_bank *bank, int first, int last)
  352. {
  353. int ret;
  354. struct sam4l_info *chip = (struct sam4l_info *)bank->driver_priv;
  355. if (bank->target->state != TARGET_HALTED) {
  356. LOG_ERROR("Target not halted");
  357. return ERROR_TARGET_NOT_HALTED;
  358. }
  359. if (!chip->probed) {
  360. if (sam4l_probe(bank) != ERROR_OK)
  361. return ERROR_FLASH_BANK_NOT_PROBED;
  362. }
  363. /* Make sure the pages make sense. */
  364. if (first >= bank->num_sectors || last >= bank->num_sectors) {
  365. LOG_ERROR("Erase range %d - %d not valid (%d sectors total)", first, last,
  366. bank->num_sectors);
  367. return ERROR_FAIL;
  368. }
  369. /* Erase */
  370. if ((first == 0) && ((last + 1) == bank->num_sectors)) {
  371. LOG_DEBUG("Erasing the whole chip");
  372. ret = sam4l_flash_command(bank->target, SAM4L_FCMD_EA, -1);
  373. if (ret != ERROR_OK) {
  374. LOG_ERROR("Erase All failed");
  375. return ret;
  376. }
  377. } else {
  378. LOG_DEBUG("Erasing sectors %d through %d...\n", first, last);
  379. /* For each sector... */
  380. for (int i = first; i <= last; i++) {
  381. /* For each page in that sector... */
  382. for (int j = 0; j < chip->pages_per_sector; j++) {
  383. int pn = i * chip->pages_per_sector + j;
  384. bool is_erased = false;
  385. /* Issue the page erase */
  386. ret = sam4l_flash_command(bank->target, SAM4L_FCMD_EP, pn);
  387. if (ret != ERROR_OK) {
  388. LOG_ERROR("Erasing page %d failed", pn);
  389. return ret;
  390. }
  391. ret = sam4l_check_page_erased(bank, pn, &is_erased);
  392. if (ret != ERROR_OK)
  393. return ret;
  394. if (!is_erased) {
  395. LOG_DEBUG("Page %d was not erased.", pn);
  396. return ERROR_FAIL;
  397. }
  398. }
  399. /* This sector is definitely erased. */
  400. bank->sectors[i].is_erased = 1;
  401. }
  402. }
  403. return ERROR_OK;
  404. }
  405. /* Write an entire page from host buffer 'buf' to page-aligned 'address' in the
  406. * Flash. */
  407. static int sam4l_write_page(struct sam4l_info *chip, struct target *target,
  408. uint32_t address, const uint8_t *buf)
  409. {
  410. int res;
  411. LOG_DEBUG("sam4l_write_page address=%08" PRIx32, address);
  412. /* Clear the page buffer before we write to it */
  413. res = sam4l_flash_command(target, SAM4L_FCMD_CPB, -1);
  414. if (res != ERROR_OK) {
  415. LOG_ERROR("%s: can't clear page buffer", __func__);
  416. return res;
  417. }
  418. /* Write the modified page back to the target's page buffer */
  419. res = target_write_memory(target, address, 4, chip->page_size / 4, buf);
  420. if (res != ERROR_OK) {
  421. LOG_ERROR("%s: %d", __func__, __LINE__);
  422. return res;
  423. }
  424. /* Commit the page contents to Flash: erase the current page and then
  425. * write it out. */
  426. res = sam4l_flash_command(target, SAM4L_FCMD_EP, -1);
  427. if (res != ERROR_OK)
  428. return res;
  429. res = sam4l_flash_command(target, SAM4L_FCMD_WP, -1);
  430. return res;
  431. }
  432. /* Write partial contents into page-aligned 'address' on the Flash from host
  433. * buffer 'buf' by writing 'nb' of 'buf' at 'offset' into the Flash page. */
  434. static int sam4l_write_page_partial(struct sam4l_info *chip,
  435. struct flash_bank *bank, uint32_t address, const uint8_t *buf,
  436. uint32_t page_offset, uint32_t nb)
  437. {
  438. int res;
  439. uint8_t *pg = malloc(chip->page_size);
  440. if (!pg)
  441. return ERROR_FAIL;
  442. LOG_DEBUG("sam4l_write_page_partial address=%08" PRIx32 " nb=%08" PRIx32, address, nb);
  443. assert(page_offset + nb < chip->page_size);
  444. assert((address % chip->page_size) == 0);
  445. /* Retrieve the full page contents from Flash */
  446. res = target_read_memory(bank->target, address, 4,
  447. chip->page_size / 4, pg);
  448. if (res != ERROR_OK) {
  449. free(pg);
  450. return res;
  451. }
  452. /* Insert our partial page over the data from Flash */
  453. memcpy(pg + (page_offset % chip->page_size), buf, nb);
  454. /* Write the page back out */
  455. res = sam4l_write_page(chip, bank->target, address, pg);
  456. free(pg);
  457. return res;
  458. }
  459. static int sam4l_write(struct flash_bank *bank, const uint8_t *buffer,
  460. uint32_t offset, uint32_t count)
  461. {
  462. int res;
  463. uint32_t nb = 0;
  464. struct sam4l_info *chip = (struct sam4l_info *)bank->driver_priv;
  465. LOG_DEBUG("sam4l_write offset=%08" PRIx32 " count=%08" PRIx32, offset, count);
  466. if (bank->target->state != TARGET_HALTED) {
  467. LOG_ERROR("Target not halted");
  468. return ERROR_TARGET_NOT_HALTED;
  469. }
  470. if (!chip->probed) {
  471. if (sam4l_probe(bank) != ERROR_OK)
  472. return ERROR_FLASH_BANK_NOT_PROBED;
  473. }
  474. if (offset % chip->page_size) {
  475. /* We're starting at an unaligned offset so we'll write a partial page
  476. * comprising that offset and up to the end of that page. */
  477. nb = chip->page_size - (offset % chip->page_size);
  478. if (nb > count)
  479. nb = count;
  480. } else if (count < chip->page_size) {
  481. /* We're writing an aligned but partial page. */
  482. nb = count;
  483. }
  484. if (nb > 0) {
  485. res = sam4l_write_page_partial(chip, bank,
  486. (offset / chip->page_size) * chip->page_size + bank->base,
  487. buffer,
  488. offset % chip->page_size, nb);
  489. if (res != ERROR_OK)
  490. return res;
  491. /* We're done with the page contents */
  492. count -= nb;
  493. offset += nb;
  494. }
  495. /* There's at least one aligned page to write out. */
  496. if (count >= chip->page_size) {
  497. int np = count / chip->page_size + ((count % chip->page_size) ? 1 : 0);
  498. for (int i = 0; i < np; i++) {
  499. if (count >= chip->page_size) {
  500. res = sam4l_write_page(chip, bank->target,
  501. bank->base + offset,
  502. buffer + (i * chip->page_size));
  503. /* Advance one page */
  504. offset += chip->page_size;
  505. count -= chip->page_size;
  506. } else {
  507. res = sam4l_write_page_partial(chip, bank,
  508. bank->base + offset,
  509. buffer + (i * chip->page_size), 0, count);
  510. /* We're done after this. */
  511. offset += count;
  512. count = 0;
  513. }
  514. if (res != ERROR_OK)
  515. return res;
  516. }
  517. }
  518. return ERROR_OK;
  519. }
  520. COMMAND_HANDLER(sam4l_handle_reset_deassert)
  521. {
  522. struct target *target = get_current_target(CMD_CTX);
  523. struct armv7m_common *armv7m = target_to_armv7m(target);
  524. struct adiv5_dap *swjdp = armv7m->arm.dap;
  525. int retval = ERROR_OK;
  526. enum reset_types jtag_reset_config = jtag_get_reset_config();
  527. /* In case of sysresetreq, debug retains state set in cortex_m_assert_reset()
  528. * so we just release reset held by SMAP
  529. *
  530. * n_RESET (srst) clears the DP, so reenable debug and set vector catch here
  531. *
  532. * After vectreset SMAP release is not needed however makes no harm
  533. */
  534. if (target->reset_halt && (jtag_reset_config & RESET_HAS_SRST)) {
  535. retval = mem_ap_write_u32(swjdp, DCB_DHCSR, DBGKEY | C_HALT | C_DEBUGEN);
  536. if (retval == ERROR_OK)
  537. retval = mem_ap_write_atomic_u32(swjdp, DCB_DEMCR,
  538. TRCENA | VC_HARDERR | VC_BUSERR | VC_CORERESET);
  539. /* do not return on error here, releasing SMAP reset is more important */
  540. }
  541. int retval2 = mem_ap_write_atomic_u32(swjdp, SMAP_SCR, SMAP_SCR_HCR);
  542. if (retval2 != ERROR_OK)
  543. return retval2;
  544. return retval;
  545. }
  546. static const struct command_registration at91sam4l_exec_command_handlers[] = {
  547. {
  548. .name = "smap_reset_deassert",
  549. .handler = sam4l_handle_reset_deassert,
  550. .mode = COMMAND_EXEC,
  551. .help = "deasert internal reset held by SMAP"
  552. },
  553. COMMAND_REGISTRATION_DONE
  554. };
  555. static const struct command_registration at91sam4l_command_handlers[] = {
  556. {
  557. .name = "at91sam4l",
  558. .mode = COMMAND_ANY,
  559. .help = "at91sam4l flash command group",
  560. .usage = "",
  561. .chain = at91sam4l_exec_command_handlers,
  562. },
  563. COMMAND_REGISTRATION_DONE
  564. };
  565. struct flash_driver at91sam4l_flash = {
  566. .name = "at91sam4l",
  567. .commands = at91sam4l_command_handlers,
  568. .flash_bank_command = sam4l_flash_bank_command,
  569. .erase = sam4l_erase,
  570. .protect = sam4l_protect,
  571. .write = sam4l_write,
  572. .read = default_flash_read,
  573. .probe = sam4l_probe,
  574. .auto_probe = sam4l_probe,
  575. .erase_check = default_flash_blank_check,
  576. .protect_check = sam4l_protect_check,
  577. };