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.
 
 
 
 
 
 

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