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.
 
 
 
 
 
 

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