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.
 
 
 
 
 
 

1209 lines
33 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2006 by Magnus Lundin *
  3. * lundin@mlu.mine.nu *
  4. * *
  5. * Copyright (C) 2008 by Gheorghe Guran (atlas) *
  6. * *
  7. * This program is free software; you can redistribute it and/or modify *
  8. * it under the terms of the GNU General public License as published by *
  9. * the Free Software Foundation; either version 2 of the License, or *
  10. * (at your option) any later version. *
  11. * *
  12. * This program is distributed in the hope that it will be useful, *
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14. * MERCHANTABILITY or FITNESS for A PARTICULAR PURPOSE. See the *
  15. * GNU General public License for more details. *
  16. * *
  17. * You should have received a copy of the GNU General public License *
  18. * along with this program; if not, write to the *
  19. * Free Software Foundation, Inc., *
  20. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  21. ****************************************************************************/
  22. /***************************************************************************************************************************************************************************************
  23. *
  24. * New flash setup command:
  25. *
  26. * flash bank <driver> <base_addr> <size> <chip_width> <bus_width> <target_number> [<target_name> <banks> <sectors_per_bank> <pages_per_sector> <page_size> <num_nvmbits> <ext_freq_khz>]
  27. *
  28. * <ext_freq_khz> - MUST be used if clock is from external source,
  29. * CAN be used if main oscillator frequency is known (recomended)
  30. * Examples:
  31. * flash bank at91sam7 0x00100000 0 0 4 0 0 AT91SAM7XC256 1 16 64 256 3 25000 ==== RECOMENDED ============
  32. * flash bank at91sam7 0 0 0 0 0 0 0 0 0 0 0 0 25000 (auto-detection, except for clock) ==== RECOMENDED ============
  33. * flash bank at91sam7 0x00100000 0 0 4 0 0 AT91SAM7XC256 1 16 64 256 3 0 ==== NOT RECOMENDED !!! ====
  34. * flash bank at91sam7 0 0 0 0 0 (old style, full auto-detection) ==== NOT RECOMENDED !!! ====
  35. ****************************************************************************************************************************************************************************************/
  36. #ifdef HAVE_CONFIG_H
  37. #include "config.h"
  38. #endif
  39. #include "at91sam7.h"
  40. #include "binarybuffer.h"
  41. static int at91sam7_register_commands(struct command_context_s *cmd_ctx);
  42. static int at91sam7_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
  43. static int at91sam7_erase(struct flash_bank_s *bank, int first, int last);
  44. static int at91sam7_protect(struct flash_bank_s *bank, int set, int first, int last);
  45. static int at91sam7_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count);
  46. static int at91sam7_probe(struct flash_bank_s *bank);
  47. //static int at91sam7_auto_probe(struct flash_bank_s *bank);
  48. static int at91sam7_erase_check(struct flash_bank_s *bank);
  49. static int at91sam7_protect_check(struct flash_bank_s *bank);
  50. static int at91sam7_info(struct flash_bank_s *bank, char *buf, int buf_size);
  51. static uint32_t at91sam7_get_flash_status(target_t *target, int bank_number);
  52. static void at91sam7_set_flash_mode(flash_bank_t *bank, int mode);
  53. static uint32_t at91sam7_wait_status_busy(flash_bank_t *bank, uint32_t waitbits, int timeout);
  54. static int at91sam7_flash_command(struct flash_bank_s *bank, uint8_t cmd, uint16_t pagen);
  55. static int at91sam7_handle_gpnvm_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  56. flash_driver_t at91sam7_flash =
  57. {
  58. .name = "at91sam7",
  59. .register_commands = at91sam7_register_commands,
  60. .flash_bank_command = at91sam7_flash_bank_command,
  61. .erase = at91sam7_erase,
  62. .protect = at91sam7_protect,
  63. .write = at91sam7_write,
  64. .probe = at91sam7_probe,
  65. .auto_probe = at91sam7_probe,
  66. .erase_check = at91sam7_erase_check,
  67. .protect_check = at91sam7_protect_check,
  68. .info = at91sam7_info
  69. };
  70. static uint32_t MC_FMR[4] = { 0xFFFFFF60, 0xFFFFFF70, 0xFFFFFF80, 0xFFFFFF90 };
  71. static uint32_t MC_FCR[4] = { 0xFFFFFF64, 0xFFFFFF74, 0xFFFFFF84, 0xFFFFFF94 };
  72. static uint32_t MC_FSR[4] = { 0xFFFFFF68, 0xFFFFFF78, 0xFFFFFF88, 0xFFFFFF98 };
  73. static char * EPROC[8]= {"Unknown","ARM946-E","ARM7TDMI","Unknown","ARM920T","ARM926EJ-S","Unknown","Unknown"};
  74. #if 0
  75. static long SRAMSIZ[16] = {
  76. -1,
  77. 0x0400, /* 1K */
  78. 0x0800, /* 2K */
  79. -1,
  80. 0x1c000, /* 112K */
  81. 0x1000, /* 4K */
  82. 0x14000, /* 80K */
  83. 0x28000, /* 160K */
  84. 0x2000, /* 8K */
  85. 0x4000, /* 16K */
  86. 0x8000, /* 32K */
  87. 0x10000, /* 64K */
  88. 0x20000, /* 128K */
  89. 0x40000, /* 256K */
  90. 0x18000, /* 96K */
  91. 0x80000, /* 512K */
  92. };
  93. #endif
  94. static int at91sam7_register_commands(struct command_context_s *cmd_ctx)
  95. {
  96. command_t *at91sam7_cmd = register_command(cmd_ctx, NULL, "at91sam7", NULL, COMMAND_ANY, NULL);
  97. register_command(cmd_ctx, at91sam7_cmd, "gpnvm", at91sam7_handle_gpnvm_command, COMMAND_EXEC,
  98. "at91sam7 gpnvm <bit> set | clear, set or clear one gpnvm bit");
  99. return ERROR_OK;
  100. }
  101. static uint32_t at91sam7_get_flash_status(target_t *target, int bank_number)
  102. {
  103. uint32_t fsr;
  104. target_read_u32(target, MC_FSR[bank_number], &fsr);
  105. return fsr;
  106. }
  107. /* Read clock configuration and set at91sam7_info->mck_freq */
  108. static void at91sam7_read_clock_info(flash_bank_t *bank)
  109. {
  110. at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
  111. target_t *target = bank->target;
  112. uint32_t mckr, mcfr, pllr, mor;
  113. unsigned long tmp = 0, mainfreq;
  114. /* Read Clock Generator Main Oscillator Register */
  115. target_read_u32(target, CKGR_MOR, &mor);
  116. /* Read Clock Generator Main Clock Frequency Register */
  117. target_read_u32(target, CKGR_MCFR, &mcfr);
  118. /* Read Master Clock Register*/
  119. target_read_u32(target, PMC_MCKR, &mckr);
  120. /* Read Clock Generator PLL Register */
  121. target_read_u32(target, CKGR_PLLR, &pllr);
  122. at91sam7_info->mck_valid = 0;
  123. at91sam7_info->mck_freq = 0;
  124. switch (mckr & PMC_MCKR_CSS)
  125. {
  126. case 0: /* Slow Clock */
  127. at91sam7_info->mck_valid = 1;
  128. tmp = RC_FREQ;
  129. break;
  130. case 1: /* Main Clock */
  131. if ((mcfr & CKGR_MCFR_MAINRDY) &&
  132. (at91sam7_info->ext_freq == 0))
  133. {
  134. at91sam7_info->mck_valid = 1;
  135. tmp = RC_FREQ / 16ul * (mcfr & 0xffff);
  136. }
  137. else if (at91sam7_info->ext_freq != 0)
  138. {
  139. at91sam7_info->mck_valid = 1;
  140. tmp = at91sam7_info->ext_freq;
  141. }
  142. break;
  143. case 2: /* Reserved */
  144. break;
  145. case 3: /* PLL Clock */
  146. if ((mcfr & CKGR_MCFR_MAINRDY) &&
  147. (at91sam7_info->ext_freq == 0))
  148. {
  149. target_read_u32(target, CKGR_PLLR, &pllr);
  150. if (!(pllr & CKGR_PLLR_DIV))
  151. break; /* 0 Hz */
  152. at91sam7_info->mck_valid = 1;
  153. mainfreq = RC_FREQ / 16ul * (mcfr & 0xffff);
  154. /* Integer arithmetic should have sufficient precision
  155. * as long as PLL is properly configured. */
  156. tmp = mainfreq / (pllr & CKGR_PLLR_DIV)*
  157. (((pllr & CKGR_PLLR_MUL) >> 16) + 1);
  158. }
  159. else if ((at91sam7_info->ext_freq != 0) &&
  160. ((pllr&CKGR_PLLR_DIV) != 0))
  161. {
  162. at91sam7_info->mck_valid = 1;
  163. tmp = at91sam7_info->ext_freq / (pllr&CKGR_PLLR_DIV)*
  164. (((pllr & CKGR_PLLR_MUL) >> 16) + 1);
  165. }
  166. break;
  167. }
  168. /* Prescaler adjust */
  169. if ((((mckr & PMC_MCKR_PRES) >> 2) == 7) || (tmp == 0))
  170. {
  171. at91sam7_info->mck_valid = 0;
  172. at91sam7_info->mck_freq = 0;
  173. }
  174. else if (((mckr & PMC_MCKR_PRES) >> 2) != 0)
  175. at91sam7_info->mck_freq = tmp >> ((mckr & PMC_MCKR_PRES) >> 2);
  176. else
  177. at91sam7_info->mck_freq = tmp;
  178. }
  179. /* Setup the timimg registers for nvbits or normal flash */
  180. static void at91sam7_set_flash_mode(flash_bank_t *bank, int mode)
  181. {
  182. uint32_t fmr, fmcn = 0, fws = 0;
  183. at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
  184. target_t *target = bank->target;
  185. if (mode && (mode != at91sam7_info->flashmode))
  186. {
  187. /* Always round up (ceil) */
  188. if (mode == FMR_TIMING_NVBITS)
  189. {
  190. if (at91sam7_info->cidr_arch == 0x60)
  191. {
  192. /* AT91SAM7A3 uses master clocks in 100 ns */
  193. fmcn = (at91sam7_info->mck_freq/10000000ul) + 1;
  194. }
  195. else
  196. {
  197. /* master clocks in 1uS for ARCH 0x7 types */
  198. fmcn = (at91sam7_info->mck_freq/1000000ul) + 1;
  199. }
  200. }
  201. else if (mode == FMR_TIMING_FLASH)
  202. {
  203. /* main clocks in 1.5uS */
  204. fmcn = (at91sam7_info->mck_freq/1000000ul)+
  205. (at91sam7_info->mck_freq/2000000ul) + 1;
  206. }
  207. /* hard overclocking */
  208. if (fmcn > 0xFF)
  209. fmcn = 0xFF;
  210. /* Only allow fmcn = 0 if clock period is > 30 us = 33kHz. */
  211. if (at91sam7_info->mck_freq <= 33333ul)
  212. fmcn = 0;
  213. /* Only allow fws = 0 if clock frequency is < 30 MHz. */
  214. if (at91sam7_info->mck_freq > 30000000ul)
  215. fws = 1;
  216. LOG_DEBUG("fmcn[%i]: %i", bank->bank_number, (int)(fmcn));
  217. fmr = fmcn << 16 | fws << 8;
  218. target_write_u32(target, MC_FMR[bank->bank_number], fmr);
  219. }
  220. at91sam7_info->flashmode = mode;
  221. }
  222. static uint32_t at91sam7_wait_status_busy(flash_bank_t *bank, uint32_t waitbits, int timeout)
  223. {
  224. uint32_t status;
  225. while ((!((status = at91sam7_get_flash_status(bank->target, bank->bank_number)) & waitbits)) && (timeout-- > 0))
  226. {
  227. LOG_DEBUG("status[%i]: 0x%" PRIx32 "", (int)bank->bank_number, status);
  228. alive_sleep(1);
  229. }
  230. LOG_DEBUG("status[%i]: 0x%" PRIx32 "", bank->bank_number, status);
  231. if (status & 0x0C)
  232. {
  233. LOG_ERROR("status register: 0x%" PRIx32 "", status);
  234. if (status & 0x4)
  235. LOG_ERROR("Lock Error Bit Detected, Operation Abort");
  236. if (status & 0x8)
  237. LOG_ERROR("Invalid command and/or bad keyword, Operation Abort");
  238. if (status & 0x10)
  239. LOG_ERROR("Security Bit Set, Operation Abort");
  240. }
  241. return status;
  242. }
  243. /* Send one command to the AT91SAM flash controller */
  244. static int at91sam7_flash_command(struct flash_bank_s *bank, uint8_t cmd, uint16_t pagen)
  245. {
  246. uint32_t fcr;
  247. at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
  248. target_t *target = bank->target;
  249. fcr = (0x5A << 24) | ((pagen&0x3FF) << 8) | cmd;
  250. target_write_u32(target, MC_FCR[bank->bank_number], fcr);
  251. LOG_DEBUG("Flash command: 0x%" PRIx32 ", flash bank: %i, page number: %u", fcr, bank->bank_number + 1, pagen);
  252. if ((at91sam7_info->cidr_arch == 0x60) && ((cmd == SLB) | (cmd == CLB)))
  253. {
  254. /* Lock bit manipulation on AT91SAM7A3 waits for FC_FSR bit 1, EOL */
  255. if (at91sam7_wait_status_busy(bank, MC_FSR_EOL, 10)&0x0C)
  256. {
  257. return ERROR_FLASH_OPERATION_FAILED;
  258. }
  259. return ERROR_OK;
  260. }
  261. if (at91sam7_wait_status_busy(bank, MC_FSR_FRDY, 10)&0x0C)
  262. {
  263. return ERROR_FLASH_OPERATION_FAILED;
  264. }
  265. return ERROR_OK;
  266. }
  267. /* Read device id register, main clock frequency register and fill in driver info structure */
  268. static int at91sam7_read_part_info(struct flash_bank_s *bank)
  269. {
  270. flash_bank_t *t_bank = bank;
  271. at91sam7_flash_bank_t *at91sam7_info;
  272. target_t *target = t_bank->target;
  273. uint16_t bnk, sec;
  274. uint16_t arch;
  275. uint32_t cidr;
  276. uint8_t banks_num = 0;
  277. uint16_t num_nvmbits = 0;
  278. uint16_t sectors_num = 0;
  279. uint16_t pages_per_sector = 0;
  280. uint16_t page_size = 0;
  281. uint32_t ext_freq;
  282. uint32_t bank_size;
  283. uint32_t base_address = 0;
  284. char *target_name = "Unknown";
  285. at91sam7_info = t_bank->driver_priv;
  286. if (at91sam7_info->cidr != 0)
  287. {
  288. /* flash already configured, update clock and check for protected sectors */
  289. flash_bank_t *fb = bank;
  290. t_bank = fb;
  291. while (t_bank)
  292. {
  293. /* re-calculate master clock frequency */
  294. at91sam7_read_clock_info(t_bank);
  295. /* no timming */
  296. at91sam7_set_flash_mode(t_bank, FMR_TIMING_NONE);
  297. /* check protect state */
  298. at91sam7_protect_check(t_bank);
  299. t_bank = fb->next;
  300. fb = t_bank;
  301. }
  302. return ERROR_OK;
  303. }
  304. /* Read and parse chip identification register */
  305. target_read_u32(target, DBGU_CIDR, &cidr);
  306. if (cidr == 0)
  307. {
  308. LOG_WARNING("Cannot identify target as an AT91SAM");
  309. return ERROR_FLASH_OPERATION_FAILED;
  310. }
  311. if (at91sam7_info->flash_autodetection == 0)
  312. {
  313. /* banks and sectors are already created, based on data from input file */
  314. flash_bank_t *fb = bank;
  315. t_bank = fb;
  316. while (t_bank)
  317. {
  318. at91sam7_info = t_bank->driver_priv;
  319. at91sam7_info->cidr = cidr;
  320. at91sam7_info->cidr_ext = (cidr >> 31)&0x0001;
  321. at91sam7_info->cidr_nvptyp = (cidr >> 28)&0x0007;
  322. at91sam7_info->cidr_arch = (cidr >> 20)&0x00FF;
  323. at91sam7_info->cidr_sramsiz = (cidr >> 16)&0x000F;
  324. at91sam7_info->cidr_nvpsiz2 = (cidr >> 12)&0x000F;
  325. at91sam7_info->cidr_nvpsiz = (cidr >> 8)&0x000F;
  326. at91sam7_info->cidr_eproc = (cidr >> 5)&0x0007;
  327. at91sam7_info->cidr_version = cidr&0x001F;
  328. /* calculate master clock frequency */
  329. at91sam7_read_clock_info(t_bank);
  330. /* no timming */
  331. at91sam7_set_flash_mode(t_bank, FMR_TIMING_NONE);
  332. /* check protect state */
  333. at91sam7_protect_check(t_bank);
  334. t_bank = fb->next;
  335. fb = t_bank;
  336. }
  337. return ERROR_OK;
  338. }
  339. arch = (cidr >> 20)&0x00FF;
  340. /* check flash size */
  341. switch ((cidr >> 8)&0x000F)
  342. {
  343. case FLASH_SIZE_8KB:
  344. break;
  345. case FLASH_SIZE_16KB:
  346. banks_num = 1;
  347. sectors_num = 8;
  348. pages_per_sector = 32;
  349. page_size = 64;
  350. base_address = 0x00100000;
  351. if (arch == 0x70)
  352. {
  353. num_nvmbits = 2;
  354. target_name = "AT91SAM7S161/16";
  355. }
  356. break;
  357. case FLASH_SIZE_32KB:
  358. banks_num = 1;
  359. sectors_num = 8;
  360. pages_per_sector = 32;
  361. page_size = 128;
  362. base_address = 0x00100000;
  363. if (arch == 0x70)
  364. {
  365. num_nvmbits = 2;
  366. target_name = "AT91SAM7S321/32";
  367. }
  368. if (arch == 0x72)
  369. {
  370. num_nvmbits = 3;
  371. target_name = "AT91SAM7SE32";
  372. }
  373. break;
  374. case FLASH_SIZE_64KB:
  375. banks_num = 1;
  376. sectors_num = 16;
  377. pages_per_sector = 32;
  378. page_size = 128;
  379. base_address = 0x00100000;
  380. if (arch == 0x70)
  381. {
  382. num_nvmbits = 2;
  383. target_name = "AT91SAM7S64";
  384. }
  385. break;
  386. case FLASH_SIZE_128KB:
  387. banks_num = 1;
  388. sectors_num = 8;
  389. pages_per_sector = 64;
  390. page_size = 256;
  391. base_address = 0x00100000;
  392. if (arch == 0x70)
  393. {
  394. num_nvmbits = 2;
  395. target_name = "AT91SAM7S128";
  396. }
  397. if (arch == 0x71)
  398. {
  399. num_nvmbits = 3;
  400. target_name = "AT91SAM7XC128";
  401. }
  402. if (arch == 0x72)
  403. {
  404. num_nvmbits = 3;
  405. target_name = "AT91SAM7SE128";
  406. }
  407. if (arch == 0x75)
  408. {
  409. num_nvmbits = 3;
  410. target_name = "AT91SAM7X128";
  411. }
  412. break;
  413. case FLASH_SIZE_256KB:
  414. banks_num = 1;
  415. sectors_num = 16;
  416. pages_per_sector = 64;
  417. page_size = 256;
  418. base_address = 0x00100000;
  419. if (arch == 0x60)
  420. {
  421. num_nvmbits = 3;
  422. target_name = "AT91SAM7A3";
  423. }
  424. if (arch == 0x70)
  425. {
  426. num_nvmbits = 2;
  427. target_name = "AT91SAM7S256";
  428. }
  429. if (arch == 0x71)
  430. {
  431. num_nvmbits = 3;
  432. target_name = "AT91SAM7XC256";
  433. }
  434. if (arch == 0x72)
  435. {
  436. num_nvmbits = 3;
  437. target_name = "AT91SAM7SE256";
  438. }
  439. if (arch == 0x75)
  440. {
  441. num_nvmbits = 3;
  442. target_name = "AT91SAM7X256";
  443. }
  444. break;
  445. case FLASH_SIZE_512KB:
  446. banks_num = 2;
  447. sectors_num = 16;
  448. pages_per_sector = 64;
  449. page_size = 256;
  450. base_address = 0x00100000;
  451. if (arch == 0x70)
  452. {
  453. num_nvmbits = 2;
  454. target_name = "AT91SAM7S512";
  455. }
  456. if (arch == 0x71)
  457. {
  458. num_nvmbits = 3;
  459. target_name = "AT91SAM7XC512";
  460. }
  461. if (arch == 0x72)
  462. {
  463. num_nvmbits = 3;
  464. target_name = "AT91SAM7SE512";
  465. }
  466. if (arch == 0x75)
  467. {
  468. num_nvmbits = 3;
  469. target_name = "AT91SAM7X512";
  470. }
  471. break;
  472. case FLASH_SIZE_1024KB:
  473. break;
  474. case FLASH_SIZE_2048KB:
  475. break;
  476. }
  477. if (strcmp(target_name, "Unknown") == 0)
  478. {
  479. LOG_ERROR("Target autodetection failed! Please specify target parameters in configuration file");
  480. return ERROR_FLASH_OPERATION_FAILED;
  481. }
  482. ext_freq = at91sam7_info->ext_freq;
  483. /* calculate bank size */
  484. bank_size = sectors_num * pages_per_sector * page_size;
  485. for (bnk = 0; bnk < banks_num; bnk++)
  486. {
  487. if (bnk > 0)
  488. {
  489. /* create a new flash bank element */
  490. flash_bank_t *fb = malloc(sizeof(flash_bank_t));
  491. fb->target = target;
  492. fb->driver = &at91sam7_flash;
  493. fb->driver_priv = malloc(sizeof(at91sam7_flash_bank_t));
  494. fb->next = NULL;
  495. /* link created bank in 'flash_banks' list and redirect t_bank */
  496. t_bank->next = fb;
  497. t_bank = fb;
  498. }
  499. t_bank->bank_number = bnk;
  500. t_bank->base = base_address + bnk * bank_size;
  501. t_bank->size = bank_size;
  502. t_bank->chip_width = 0;
  503. t_bank->bus_width = 4;
  504. t_bank->num_sectors = sectors_num;
  505. /* allocate sectors */
  506. t_bank->sectors = malloc(sectors_num * sizeof(flash_sector_t));
  507. for (sec = 0; sec < sectors_num; sec++)
  508. {
  509. t_bank->sectors[sec].offset = sec * pages_per_sector * page_size;
  510. t_bank->sectors[sec].size = pages_per_sector * page_size;
  511. t_bank->sectors[sec].is_erased = -1;
  512. t_bank->sectors[sec].is_protected = -1;
  513. }
  514. at91sam7_info = t_bank->driver_priv;
  515. at91sam7_info->cidr = cidr;
  516. at91sam7_info->cidr_ext = (cidr >> 31)&0x0001;
  517. at91sam7_info->cidr_nvptyp = (cidr >> 28)&0x0007;
  518. at91sam7_info->cidr_arch = (cidr >> 20)&0x00FF;
  519. at91sam7_info->cidr_sramsiz = (cidr >> 16)&0x000F;
  520. at91sam7_info->cidr_nvpsiz2 = (cidr >> 12)&0x000F;
  521. at91sam7_info->cidr_nvpsiz = (cidr >> 8)&0x000F;
  522. at91sam7_info->cidr_eproc = (cidr >> 5)&0x0007;
  523. at91sam7_info->cidr_version = cidr&0x001F;
  524. at91sam7_info->target_name = target_name;
  525. at91sam7_info->flashmode = 0;
  526. at91sam7_info->ext_freq = ext_freq;
  527. at91sam7_info->num_nvmbits = num_nvmbits;
  528. at91sam7_info->num_nvmbits_on = 0;
  529. at91sam7_info->pagesize = page_size;
  530. at91sam7_info->pages_per_sector = pages_per_sector;
  531. /* calculate master clock frequency */
  532. at91sam7_read_clock_info(t_bank);
  533. /* no timming */
  534. at91sam7_set_flash_mode(t_bank, FMR_TIMING_NONE);
  535. /* check protect state */
  536. at91sam7_protect_check(t_bank);
  537. }
  538. LOG_DEBUG("nvptyp: 0x%3.3x, arch: 0x%4.4x", at91sam7_info->cidr_nvptyp, at91sam7_info->cidr_arch);
  539. return ERROR_OK;
  540. }
  541. static int at91sam7_erase_check(struct flash_bank_s *bank)
  542. {
  543. target_t *target = bank->target;
  544. uint16_t retval;
  545. uint32_t blank;
  546. uint16_t fast_check;
  547. uint8_t *buffer;
  548. uint16_t nSector;
  549. uint16_t nByte;
  550. if (bank->target->state != TARGET_HALTED)
  551. {
  552. LOG_ERROR("Target not halted");
  553. return ERROR_TARGET_NOT_HALTED;
  554. }
  555. /* Configure the flash controller timing */
  556. at91sam7_read_clock_info(bank);
  557. at91sam7_set_flash_mode(bank, FMR_TIMING_FLASH);
  558. fast_check = 1;
  559. for (nSector = 0; nSector < bank->num_sectors; nSector++)
  560. {
  561. retval = target_blank_check_memory(target, bank->base + bank->sectors[nSector].offset,
  562. bank->sectors[nSector].size, &blank);
  563. if (retval != ERROR_OK)
  564. {
  565. fast_check = 0;
  566. break;
  567. }
  568. if (blank == 0xFF)
  569. bank->sectors[nSector].is_erased = 1;
  570. else
  571. bank->sectors[nSector].is_erased = 0;
  572. }
  573. if (fast_check)
  574. {
  575. return ERROR_OK;
  576. }
  577. LOG_USER("Running slow fallback erase check - add working memory");
  578. buffer = malloc(bank->sectors[0].size);
  579. for (nSector = 0; nSector < bank->num_sectors; nSector++)
  580. {
  581. bank->sectors[nSector].is_erased = 1;
  582. retval = target_read_memory(target, bank->base + bank->sectors[nSector].offset, 4,
  583. bank->sectors[nSector].size/4, buffer);
  584. if (retval != ERROR_OK)
  585. return retval;
  586. for (nByte = 0; nByte < bank->sectors[nSector].size; nByte++)
  587. {
  588. if (buffer[nByte] != 0xFF)
  589. {
  590. bank->sectors[nSector].is_erased = 0;
  591. break;
  592. }
  593. }
  594. }
  595. free(buffer);
  596. return ERROR_OK;
  597. }
  598. static int at91sam7_protect_check(struct flash_bank_s *bank)
  599. {
  600. uint8_t lock_pos, gpnvm_pos;
  601. uint32_t status;
  602. at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
  603. if (at91sam7_info->cidr == 0)
  604. {
  605. return ERROR_FLASH_BANK_NOT_PROBED;
  606. }
  607. if (bank->target->state != TARGET_HALTED)
  608. {
  609. LOG_ERROR("Target not halted");
  610. return ERROR_TARGET_NOT_HALTED;
  611. }
  612. status = at91sam7_get_flash_status(bank->target, bank->bank_number);
  613. at91sam7_info->lockbits = (status >> 16);
  614. at91sam7_info->num_lockbits_on = 0;
  615. for (lock_pos = 0; lock_pos < bank->num_sectors; lock_pos++)
  616. {
  617. if (((status >> (16 + lock_pos))&(0x0001)) == 1)
  618. {
  619. at91sam7_info->num_lockbits_on++;
  620. bank->sectors[lock_pos].is_protected = 1;
  621. }
  622. else
  623. bank->sectors[lock_pos].is_protected = 0;
  624. }
  625. /* GPNVM and SECURITY bits apply only for MC_FSR of EFC0 */
  626. status = at91sam7_get_flash_status(bank->target, 0);
  627. at91sam7_info->securitybit = (status >> 4)&0x01;
  628. at91sam7_info->nvmbits = (status >> 8)&0xFF;
  629. at91sam7_info->num_nvmbits_on = 0;
  630. for (gpnvm_pos = 0; gpnvm_pos < at91sam7_info->num_nvmbits; gpnvm_pos++)
  631. {
  632. if (((status >> (8 + gpnvm_pos))&(0x01)) == 1)
  633. {
  634. at91sam7_info->num_nvmbits_on++;
  635. }
  636. }
  637. return ERROR_OK;
  638. }
  639. /***************************************************************************************************************************************************************************************
  640. # flash bank <driver> <base_addr> <size> <chip_width> <bus_width> <target_number> [<target_name> <banks> <sectors_per_bank> <pages_per_sector> <page_size> <num_nvmbits> <ext_freq_khz>]
  641. # <ext_freq_khz> - MUST be used if clock is from external source
  642. # CAN be used if main oscillator frequency is known
  643. # Examples:
  644. # flash bank at91sam7 0x00100000 0 0 4 0 0 AT91SAM7XC256 1 16 64 256 3 25000 ==== RECOMENDED ============
  645. # flash bank at91sam7 0 0 0 0 0 0 0 0 0 0 0 0 25000 (auto-detection, except for clock) ==== RECOMENDED ============
  646. # flash bank at91sam7 0x00100000 0 0 4 0 0 AT91SAM7XC256 1 16 64 256 3 0 ==== NOT RECOMENDED !!! ====
  647. # flash bank at91sam7 0 0 0 0 0 (old style, full auto-detection) ==== NOT RECOMENDED !!! ====
  648. ****************************************************************************************************************************************************************************************/
  649. static int at91sam7_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
  650. {
  651. flash_bank_t *t_bank = bank;
  652. at91sam7_flash_bank_t *at91sam7_info;
  653. target_t *target = t_bank->target;
  654. uint32_t base_address;
  655. uint32_t bank_size;
  656. uint32_t ext_freq;
  657. int chip_width;
  658. int bus_width;
  659. int banks_num;
  660. int num_sectors;
  661. uint16_t pages_per_sector;
  662. uint16_t page_size;
  663. uint16_t num_nvmbits;
  664. char *target_name;
  665. int bnk, sec;
  666. at91sam7_info = malloc(sizeof(at91sam7_flash_bank_t));
  667. t_bank->driver_priv = at91sam7_info;
  668. /* part wasn't probed for info yet */
  669. at91sam7_info->cidr = 0;
  670. at91sam7_info->flashmode = 0;
  671. at91sam7_info->ext_freq = 0;
  672. at91sam7_info->flash_autodetection = 0;
  673. if (argc == 14)
  674. {
  675. ext_freq = atol(args[13]) * 1000;
  676. at91sam7_info->ext_freq = ext_freq;
  677. }
  678. if ((argc != 14) ||
  679. (atoi(args[4]) == 0) || /* bus width */
  680. (atoi(args[8]) == 0) || /* banks number */
  681. (atoi(args[9]) == 0) || /* sectors per bank */
  682. (atoi(args[10]) == 0) || /* pages per sector */
  683. (atoi(args[11]) == 0) || /* page size */
  684. (atoi(args[12]) == 0)) /* nvmbits number */
  685. {
  686. at91sam7_info->flash_autodetection = 1;
  687. return ERROR_OK;
  688. }
  689. base_address = strtoul(args[1], NULL, 0);
  690. chip_width = atoi(args[3]);
  691. bus_width = atoi(args[4]);
  692. banks_num = atoi(args[8]);
  693. num_sectors = atoi(args[9]);
  694. pages_per_sector = atoi(args[10]);
  695. page_size = atoi(args[11]);
  696. num_nvmbits = atoi(args[12]);
  697. target_name = calloc(strlen(args[7]) + 1, sizeof(char));
  698. strcpy(target_name, args[7]);
  699. /* calculate bank size */
  700. bank_size = num_sectors * pages_per_sector * page_size;
  701. for (bnk = 0; bnk < banks_num; bnk++)
  702. {
  703. if (bnk > 0)
  704. {
  705. /* create a new bank element */
  706. flash_bank_t *fb = malloc(sizeof(flash_bank_t));
  707. fb->target = target;
  708. fb->driver = &at91sam7_flash;
  709. fb->driver_priv = malloc(sizeof(at91sam7_flash_bank_t));
  710. fb->next = NULL;
  711. /* link created bank in 'flash_banks' list and redirect t_bank */
  712. t_bank->next = fb;
  713. t_bank = fb;
  714. }
  715. t_bank->bank_number = bnk;
  716. t_bank->base = base_address + bnk * bank_size;
  717. t_bank->size = bank_size;
  718. t_bank->chip_width = chip_width;
  719. t_bank->bus_width = bus_width;
  720. t_bank->num_sectors = num_sectors;
  721. /* allocate sectors */
  722. t_bank->sectors = malloc(num_sectors * sizeof(flash_sector_t));
  723. for (sec = 0; sec < num_sectors; sec++)
  724. {
  725. t_bank->sectors[sec].offset = sec * pages_per_sector * page_size;
  726. t_bank->sectors[sec].size = pages_per_sector * page_size;
  727. t_bank->sectors[sec].is_erased = -1;
  728. t_bank->sectors[sec].is_protected = -1;
  729. }
  730. at91sam7_info = t_bank->driver_priv;
  731. at91sam7_info->target_name = target_name;
  732. at91sam7_info->flashmode = 0;
  733. at91sam7_info->ext_freq = ext_freq;
  734. at91sam7_info->num_nvmbits = num_nvmbits;
  735. at91sam7_info->num_nvmbits_on = 0;
  736. at91sam7_info->pagesize = page_size;
  737. at91sam7_info->pages_per_sector = pages_per_sector;
  738. }
  739. return ERROR_OK;
  740. }
  741. static int at91sam7_erase(struct flash_bank_s *bank, int first, int last)
  742. {
  743. at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
  744. int sec;
  745. uint32_t nbytes, pos;
  746. uint8_t *buffer;
  747. uint8_t erase_all;
  748. if (at91sam7_info->cidr == 0)
  749. {
  750. return ERROR_FLASH_BANK_NOT_PROBED;
  751. }
  752. if (bank->target->state != TARGET_HALTED)
  753. {
  754. LOG_ERROR("Target not halted");
  755. return ERROR_TARGET_NOT_HALTED;
  756. }
  757. if ((first < 0) || (last < first) || (last >= bank->num_sectors))
  758. {
  759. return ERROR_FLASH_SECTOR_INVALID;
  760. }
  761. erase_all = 0;
  762. if ((first == 0) && (last == (bank->num_sectors-1)))
  763. {
  764. erase_all = 1;
  765. }
  766. /* Configure the flash controller timing */
  767. at91sam7_read_clock_info(bank);
  768. at91sam7_set_flash_mode(bank, FMR_TIMING_FLASH);
  769. if (erase_all)
  770. {
  771. if (at91sam7_flash_command(bank, EA, 0) != ERROR_OK)
  772. {
  773. return ERROR_FLASH_OPERATION_FAILED;
  774. }
  775. }
  776. else
  777. {
  778. /* allocate and clean buffer */
  779. nbytes = (last - first + 1) * bank->sectors[first].size;
  780. buffer = malloc(nbytes * sizeof(uint8_t));
  781. for (pos = 0; pos < nbytes; pos++)
  782. {
  783. buffer[pos] = 0xFF;
  784. }
  785. if (at91sam7_write(bank, buffer, bank->sectors[first].offset, nbytes) != ERROR_OK)
  786. {
  787. return ERROR_FLASH_OPERATION_FAILED;
  788. }
  789. free(buffer);
  790. }
  791. /* mark erased sectors */
  792. for (sec = first; sec <= last; sec++)
  793. {
  794. bank->sectors[sec].is_erased = 1;
  795. }
  796. return ERROR_OK;
  797. }
  798. static int at91sam7_protect(struct flash_bank_s *bank, int set, int first, int last)
  799. {
  800. uint32_t cmd;
  801. int sector;
  802. uint32_t pagen;
  803. at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
  804. if (at91sam7_info->cidr == 0)
  805. {
  806. return ERROR_FLASH_BANK_NOT_PROBED;
  807. }
  808. if (bank->target->state != TARGET_HALTED)
  809. {
  810. LOG_ERROR("Target not halted");
  811. return ERROR_TARGET_NOT_HALTED;
  812. }
  813. if ((first < 0) || (last < first) || (last >= bank->num_sectors))
  814. {
  815. return ERROR_FLASH_SECTOR_INVALID;
  816. }
  817. /* Configure the flash controller timing */
  818. at91sam7_read_clock_info(bank);
  819. at91sam7_set_flash_mode(bank, FMR_TIMING_NVBITS);
  820. for (sector = first; sector <= last; sector++)
  821. {
  822. if (set)
  823. cmd = SLB;
  824. else
  825. cmd = CLB;
  826. /* if we lock a page from one sector then entire sector will be locked, also,
  827. * if we unlock a page from a locked sector, entire sector will be unlocked */
  828. pagen = sector * at91sam7_info->pages_per_sector;
  829. if (at91sam7_flash_command(bank, cmd, pagen) != ERROR_OK)
  830. {
  831. return ERROR_FLASH_OPERATION_FAILED;
  832. }
  833. }
  834. at91sam7_protect_check(bank);
  835. return ERROR_OK;
  836. }
  837. static int at91sam7_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
  838. {
  839. int retval;
  840. at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
  841. target_t *target = bank->target;
  842. uint32_t dst_min_alignment, wcount, bytes_remaining = count;
  843. uint32_t first_page, last_page, pagen, buffer_pos;
  844. if (at91sam7_info->cidr == 0)
  845. {
  846. return ERROR_FLASH_BANK_NOT_PROBED;
  847. }
  848. if (bank->target->state != TARGET_HALTED)
  849. {
  850. LOG_ERROR("Target not halted");
  851. return ERROR_TARGET_NOT_HALTED;
  852. }
  853. if (offset + count > bank->size)
  854. return ERROR_FLASH_DST_OUT_OF_BANK;
  855. dst_min_alignment = at91sam7_info->pagesize;
  856. if (offset % dst_min_alignment)
  857. {
  858. LOG_WARNING("offset 0x%" PRIx32 " breaks required alignment 0x%" PRIx32 "", offset, dst_min_alignment);
  859. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  860. }
  861. if (at91sam7_info->cidr_arch == 0)
  862. return ERROR_FLASH_BANK_NOT_PROBED;
  863. first_page = offset/dst_min_alignment;
  864. last_page = CEIL(offset + count, dst_min_alignment);
  865. LOG_DEBUG("first_page: %i, last_page: %i, count %i", (int)first_page, (int)last_page, (int)count);
  866. /* Configure the flash controller timing */
  867. at91sam7_read_clock_info(bank);
  868. at91sam7_set_flash_mode(bank, FMR_TIMING_FLASH);
  869. for (pagen = first_page; pagen < last_page; pagen++)
  870. {
  871. if (bytes_remaining < dst_min_alignment)
  872. count = bytes_remaining;
  873. else
  874. count = dst_min_alignment;
  875. bytes_remaining -= count;
  876. /* Write one block to the PageWriteBuffer */
  877. buffer_pos = (pagen-first_page)*dst_min_alignment;
  878. wcount = CEIL(count,4);
  879. if ((retval = target_write_memory(target, bank->base + pagen*dst_min_alignment, 4, wcount, buffer + buffer_pos)) != ERROR_OK)
  880. {
  881. return retval;
  882. }
  883. /* Send Write Page command to Flash Controller */
  884. if (at91sam7_flash_command(bank, WP, pagen) != ERROR_OK)
  885. {
  886. return ERROR_FLASH_OPERATION_FAILED;
  887. }
  888. LOG_DEBUG("Write flash bank:%i page number:%" PRIi32 "", bank->bank_number, pagen);
  889. }
  890. return ERROR_OK;
  891. }
  892. static int at91sam7_probe(struct flash_bank_s *bank)
  893. {
  894. /* we can't probe on an at91sam7
  895. * if this is an at91sam7, it has the configured flash */
  896. int retval;
  897. if (bank->target->state != TARGET_HALTED)
  898. {
  899. LOG_ERROR("Target not halted");
  900. return ERROR_TARGET_NOT_HALTED;
  901. }
  902. retval = at91sam7_read_part_info(bank);
  903. if (retval != ERROR_OK)
  904. return retval;
  905. return ERROR_OK;
  906. }
  907. static int at91sam7_info(struct flash_bank_s *bank, char *buf, int buf_size)
  908. {
  909. int printed;
  910. at91sam7_flash_bank_t *at91sam7_info = bank->driver_priv;
  911. if (at91sam7_info->cidr == 0)
  912. {
  913. return ERROR_FLASH_BANK_NOT_PROBED;
  914. }
  915. printed = snprintf(buf, buf_size,
  916. "\n at91sam7 driver information: Chip is %s\n",
  917. at91sam7_info->target_name);
  918. buf += printed;
  919. buf_size -= printed;
  920. printed = snprintf(buf,
  921. buf_size,
  922. " Cidr: 0x%8.8" PRIx32 " | Arch: 0x%4.4x | Eproc: %s | Version: 0x%3.3x | Flashsize: 0x%8.8" PRIx32 "\n",
  923. at91sam7_info->cidr,
  924. at91sam7_info->cidr_arch,
  925. EPROC[at91sam7_info->cidr_eproc],
  926. at91sam7_info->cidr_version,
  927. bank->size);
  928. buf += printed;
  929. buf_size -= printed;
  930. printed = snprintf(buf, buf_size,
  931. " Master clock (estimated): %u KHz | External clock: %u KHz\n",
  932. (unsigned)(at91sam7_info->mck_freq / 1000), (unsigned)(at91sam7_info->ext_freq / 1000));
  933. buf += printed;
  934. buf_size -= printed;
  935. printed = snprintf(buf, buf_size,
  936. " Pagesize: %i bytes | Lockbits(%i): %i 0x%4.4x | Pages in lock region: %i \n",
  937. at91sam7_info->pagesize, bank->num_sectors, at91sam7_info->num_lockbits_on,
  938. at91sam7_info->lockbits, at91sam7_info->pages_per_sector*at91sam7_info->num_lockbits_on);
  939. buf += printed;
  940. buf_size -= printed;
  941. printed = snprintf(buf, buf_size,
  942. " Securitybit: %i | Nvmbits(%i): %i 0x%1.1x\n",
  943. at91sam7_info->securitybit, at91sam7_info->num_nvmbits,
  944. at91sam7_info->num_nvmbits_on, at91sam7_info->nvmbits);
  945. buf += printed;
  946. buf_size -= printed;
  947. return ERROR_OK;
  948. }
  949. /*
  950. * On AT91SAM7S: When the gpnvm bits are set with
  951. * > at91sam7 gpnvm bitnr set
  952. * the changes are not visible in the flash controller status register MC_FSR
  953. * until the processor has been reset.
  954. * On the Olimex board this requires a power cycle.
  955. * Note that the AT91SAM7S has the following errata (doc6175.pdf sec 14.1.3):
  956. * The maximum number of write/erase cycles for Non volatile Memory bits is 100. this includes
  957. * Lock Bits (LOCKx), General Purpose NVM bits (GPNVMx) and the Security Bit.
  958. */
  959. static int at91sam7_handle_gpnvm_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  960. {
  961. flash_bank_t *bank;
  962. int bit;
  963. uint8_t flashcmd;
  964. uint32_t status;
  965. at91sam7_flash_bank_t *at91sam7_info;
  966. int retval;
  967. if (argc != 2)
  968. {
  969. command_print(cmd_ctx, "at91sam7 gpnvm <bit> <set | clear>");
  970. return ERROR_OK;
  971. }
  972. bank = get_flash_bank_by_num_noprobe(0);
  973. if (bank == NULL)
  974. {
  975. return ERROR_FLASH_BANK_INVALID;
  976. }
  977. if (bank->driver != &at91sam7_flash)
  978. {
  979. command_print(cmd_ctx, "not an at91sam7 flash bank '%s'", args[0]);
  980. return ERROR_FLASH_BANK_INVALID;
  981. }
  982. if (bank->target->state != TARGET_HALTED)
  983. {
  984. LOG_ERROR("target has to be halted to perform flash operation");
  985. return ERROR_TARGET_NOT_HALTED;
  986. }
  987. if (strcmp(args[1], "set") == 0)
  988. {
  989. flashcmd = SGPB;
  990. }
  991. else if (strcmp(args[1], "clear") == 0)
  992. {
  993. flashcmd = CGPB;
  994. }
  995. else
  996. {
  997. return ERROR_COMMAND_SYNTAX_ERROR;
  998. }
  999. at91sam7_info = bank->driver_priv;
  1000. if (at91sam7_info->cidr == 0)
  1001. {
  1002. retval = at91sam7_read_part_info(bank);
  1003. if (retval != ERROR_OK)
  1004. {
  1005. return retval;
  1006. }
  1007. }
  1008. bit = atoi(args[0]);
  1009. if ((bit < 0) || (bit >= at91sam7_info->num_nvmbits))
  1010. {
  1011. command_print(cmd_ctx, "gpnvm bit '#%s' is out of bounds for target %s", args[0], at91sam7_info->target_name);
  1012. return ERROR_OK;
  1013. }
  1014. /* Configure the flash controller timing */
  1015. at91sam7_read_clock_info(bank);
  1016. at91sam7_set_flash_mode(bank, FMR_TIMING_NVBITS);
  1017. if (at91sam7_flash_command(bank, flashcmd, bit) != ERROR_OK)
  1018. {
  1019. return ERROR_FLASH_OPERATION_FAILED;
  1020. }
  1021. /* GPNVM and SECURITY bits apply only for MC_FSR of EFC0 */
  1022. status = at91sam7_get_flash_status(bank->target, 0);
  1023. LOG_DEBUG("at91sam7_handle_gpnvm_command: cmd 0x%x, value %d, status 0x%" PRIx32 " \n", flashcmd, bit, status);
  1024. /* check protect state */
  1025. at91sam7_protect_check(bank);
  1026. return ERROR_OK;
  1027. }