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.
 
 
 
 
 
 

1210 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, 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 timimg 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. struct flash_bank *fb = bank;
  324. struct flash_bank *t_bank = bank;
  325. while (t_bank) {
  326. /* re-calculate master clock frequency */
  327. at91sam7_read_clock_info(t_bank);
  328. /* no timming */
  329. at91sam7_set_flash_mode(t_bank, FMR_TIMING_NONE);
  330. /* check protect state */
  331. at91sam7_protect_check(t_bank);
  332. t_bank = fb->next;
  333. fb = t_bank;
  334. }
  335. return ERROR_OK;
  336. }
  337. /* Read and parse chip identification register */
  338. target_read_u32(target, DBGU_CIDR, &cidr);
  339. if (cidr == 0) {
  340. LOG_WARNING("Cannot identify target as an AT91SAM");
  341. return ERROR_FLASH_OPERATION_FAILED;
  342. }
  343. if (at91sam7_info->flash_autodetection == 0) {
  344. /* banks and sectors are already created, based on data from input file */
  345. struct flash_bank *fb = bank;
  346. struct flash_bank *t_bank = bank;
  347. while (t_bank) {
  348. at91sam7_info = t_bank->driver_priv;
  349. at91sam7_info->cidr = cidr;
  350. at91sam7_info->cidr_ext = (cidr >> 31)&0x0001;
  351. at91sam7_info->cidr_nvptyp = (cidr >> 28)&0x0007;
  352. at91sam7_info->cidr_arch = (cidr >> 20)&0x00FF;
  353. at91sam7_info->cidr_sramsiz = (cidr >> 16)&0x000F;
  354. at91sam7_info->cidr_nvpsiz2 = (cidr >> 12)&0x000F;
  355. at91sam7_info->cidr_nvpsiz = (cidr >> 8)&0x000F;
  356. at91sam7_info->cidr_eproc = (cidr >> 5)&0x0007;
  357. at91sam7_info->cidr_version = cidr&0x001F;
  358. /* calculate master clock frequency */
  359. at91sam7_read_clock_info(t_bank);
  360. /* no timming */
  361. at91sam7_set_flash_mode(t_bank, FMR_TIMING_NONE);
  362. /* check protect state */
  363. at91sam7_protect_check(t_bank);
  364. t_bank = fb->next;
  365. fb = t_bank;
  366. }
  367. return ERROR_OK;
  368. }
  369. arch = (cidr >> 20)&0x00FF;
  370. /* check flash size */
  371. switch ((cidr >> 8)&0x000F) {
  372. case FLASH_SIZE_8KB:
  373. break;
  374. case FLASH_SIZE_16KB:
  375. banks_num = 1;
  376. sectors_num = 8;
  377. pages_per_sector = 32;
  378. page_size = 64;
  379. base_address = 0x00100000;
  380. if (arch == 0x70) {
  381. num_nvmbits = 2;
  382. target_name_t = "AT91SAM7S161/16";
  383. }
  384. break;
  385. case FLASH_SIZE_32KB:
  386. banks_num = 1;
  387. sectors_num = 8;
  388. pages_per_sector = 32;
  389. page_size = 128;
  390. base_address = 0x00100000;
  391. if (arch == 0x70) {
  392. num_nvmbits = 2;
  393. target_name_t = "AT91SAM7S321/32";
  394. }
  395. if (arch == 0x72) {
  396. num_nvmbits = 3;
  397. target_name_t = "AT91SAM7SE32";
  398. }
  399. break;
  400. case FLASH_SIZE_64KB:
  401. banks_num = 1;
  402. sectors_num = 16;
  403. pages_per_sector = 32;
  404. page_size = 128;
  405. base_address = 0x00100000;
  406. if (arch == 0x70) {
  407. num_nvmbits = 2;
  408. target_name_t = "AT91SAM7S64";
  409. }
  410. break;
  411. case FLASH_SIZE_128KB:
  412. banks_num = 1;
  413. sectors_num = 8;
  414. pages_per_sector = 64;
  415. page_size = 256;
  416. base_address = 0x00100000;
  417. if (arch == 0x70) {
  418. num_nvmbits = 2;
  419. target_name_t = "AT91SAM7S128";
  420. }
  421. if (arch == 0x71) {
  422. num_nvmbits = 3;
  423. target_name_t = "AT91SAM7XC128";
  424. }
  425. if (arch == 0x72) {
  426. num_nvmbits = 3;
  427. target_name_t = "AT91SAM7SE128";
  428. }
  429. if (arch == 0x75) {
  430. num_nvmbits = 3;
  431. target_name_t = "AT91SAM7X128";
  432. }
  433. break;
  434. case FLASH_SIZE_256KB:
  435. banks_num = 1;
  436. sectors_num = 16;
  437. pages_per_sector = 64;
  438. page_size = 256;
  439. base_address = 0x00100000;
  440. if (arch == 0x60) {
  441. num_nvmbits = 3;
  442. target_name_t = "AT91SAM7A3";
  443. }
  444. if (arch == 0x70) {
  445. num_nvmbits = 2;
  446. target_name_t = "AT91SAM7S256";
  447. }
  448. if (arch == 0x71) {
  449. num_nvmbits = 3;
  450. target_name_t = "AT91SAM7XC256";
  451. }
  452. if (arch == 0x72) {
  453. num_nvmbits = 3;
  454. target_name_t = "AT91SAM7SE256";
  455. }
  456. if (arch == 0x75) {
  457. num_nvmbits = 3;
  458. target_name_t = "AT91SAM7X256";
  459. }
  460. break;
  461. case FLASH_SIZE_512KB:
  462. banks_num = 2;
  463. sectors_num = 16;
  464. pages_per_sector = 64;
  465. page_size = 256;
  466. base_address = 0x00100000;
  467. if (arch == 0x70) {
  468. num_nvmbits = 2;
  469. target_name_t = "AT91SAM7S512";
  470. }
  471. if (arch == 0x71) {
  472. num_nvmbits = 3;
  473. target_name_t = "AT91SAM7XC512";
  474. }
  475. if (arch == 0x72) {
  476. num_nvmbits = 3;
  477. target_name_t = "AT91SAM7SE512";
  478. }
  479. if (arch == 0x75) {
  480. num_nvmbits = 3;
  481. target_name_t = "AT91SAM7X512";
  482. }
  483. break;
  484. case FLASH_SIZE_1024KB:
  485. break;
  486. case FLASH_SIZE_2048KB:
  487. break;
  488. }
  489. if (strcmp(target_name_t, "Unknown") == 0) {
  490. LOG_ERROR(
  491. "Target autodetection failed! Please specify target parameters in configuration file");
  492. return ERROR_FLASH_OPERATION_FAILED;
  493. }
  494. ext_freq = at91sam7_info->ext_freq;
  495. /* calculate bank size */
  496. bank_size = sectors_num * pages_per_sector * page_size;
  497. for (bnk = 0; bnk < banks_num; bnk++) {
  498. struct flash_bank *t_bank = bank;
  499. if (bnk > 0) {
  500. if (!t_bank->next) {
  501. /* create a new flash bank element */
  502. struct flash_bank *fb = malloc(sizeof(struct flash_bank));
  503. fb->target = target;
  504. fb->driver = bank->driver;
  505. fb->driver_priv = malloc(sizeof(struct at91sam7_flash_bank));
  506. fb->name = "sam7_probed";
  507. fb->next = NULL;
  508. /* link created bank in 'flash_banks' list */
  509. t_bank->next = fb;
  510. }
  511. t_bank = t_bank->next;
  512. }
  513. t_bank->bank_number = bnk;
  514. t_bank->base = base_address + bnk * bank_size;
  515. t_bank->size = bank_size;
  516. t_bank->chip_width = 0;
  517. t_bank->bus_width = 4;
  518. t_bank->num_sectors = sectors_num;
  519. /* allocate sectors */
  520. t_bank->sectors = malloc(sectors_num * sizeof(struct flash_sector));
  521. for (sec = 0; sec < sectors_num; sec++) {
  522. t_bank->sectors[sec].offset = sec * pages_per_sector * page_size;
  523. t_bank->sectors[sec].size = pages_per_sector * page_size;
  524. t_bank->sectors[sec].is_erased = -1;
  525. t_bank->sectors[sec].is_protected = -1;
  526. }
  527. at91sam7_info = t_bank->driver_priv;
  528. at91sam7_info->cidr = cidr;
  529. at91sam7_info->cidr_ext = (cidr >> 31)&0x0001;
  530. at91sam7_info->cidr_nvptyp = (cidr >> 28)&0x0007;
  531. at91sam7_info->cidr_arch = (cidr >> 20)&0x00FF;
  532. at91sam7_info->cidr_sramsiz = (cidr >> 16)&0x000F;
  533. at91sam7_info->cidr_nvpsiz2 = (cidr >> 12)&0x000F;
  534. at91sam7_info->cidr_nvpsiz = (cidr >> 8)&0x000F;
  535. at91sam7_info->cidr_eproc = (cidr >> 5)&0x0007;
  536. at91sam7_info->cidr_version = cidr&0x001F;
  537. at91sam7_info->target_name = target_name_t;
  538. at91sam7_info->flashmode = 0;
  539. at91sam7_info->ext_freq = ext_freq;
  540. at91sam7_info->num_nvmbits = num_nvmbits;
  541. at91sam7_info->num_nvmbits_on = 0;
  542. at91sam7_info->pagesize = page_size;
  543. at91sam7_info->pages_per_sector = pages_per_sector;
  544. /* calculate master clock frequency */
  545. at91sam7_read_clock_info(t_bank);
  546. /* no timming */
  547. at91sam7_set_flash_mode(t_bank, FMR_TIMING_NONE);
  548. /* check protect state */
  549. at91sam7_protect_check(t_bank);
  550. }
  551. LOG_DEBUG("nvptyp: 0x%3.3x, arch: 0x%4.4x",
  552. at91sam7_info->cidr_nvptyp,
  553. at91sam7_info->cidr_arch);
  554. return ERROR_OK;
  555. }
  556. static int at91sam7_erase_check(struct flash_bank *bank)
  557. {
  558. struct target *target = bank->target;
  559. uint16_t retval;
  560. uint32_t blank;
  561. uint16_t fast_check;
  562. uint8_t *buffer;
  563. uint16_t nSector;
  564. uint16_t nByte;
  565. if (bank->target->state != TARGET_HALTED) {
  566. LOG_ERROR("Target not halted");
  567. return ERROR_TARGET_NOT_HALTED;
  568. }
  569. /* Configure the flash controller timing */
  570. at91sam7_read_clock_info(bank);
  571. at91sam7_set_flash_mode(bank, FMR_TIMING_FLASH);
  572. fast_check = 1;
  573. for (nSector = 0; nSector < bank->num_sectors; nSector++) {
  574. retval = target_blank_check_memory(target,
  575. bank->base + bank->sectors[nSector].offset,
  576. bank->sectors[nSector].size,
  577. &blank);
  578. if (retval != ERROR_OK) {
  579. fast_check = 0;
  580. break;
  581. }
  582. if (blank == 0xFF)
  583. bank->sectors[nSector].is_erased = 1;
  584. else
  585. bank->sectors[nSector].is_erased = 0;
  586. }
  587. if (fast_check)
  588. return ERROR_OK;
  589. LOG_USER("Running slow fallback erase check - add working memory");
  590. buffer = malloc(bank->sectors[0].size);
  591. for (nSector = 0; nSector < bank->num_sectors; nSector++) {
  592. bank->sectors[nSector].is_erased = 1;
  593. retval = target_read_memory(target, bank->base + bank->sectors[nSector].offset, 4,
  594. bank->sectors[nSector].size/4, buffer);
  595. if (retval != ERROR_OK)
  596. return retval;
  597. for (nByte = 0; nByte < bank->sectors[nSector].size; nByte++) {
  598. if (buffer[nByte] != 0xFF) {
  599. bank->sectors[nSector].is_erased = 0;
  600. break;
  601. }
  602. }
  603. }
  604. free(buffer);
  605. return ERROR_OK;
  606. }
  607. static int at91sam7_protect_check(struct flash_bank *bank)
  608. {
  609. uint8_t lock_pos, gpnvm_pos;
  610. uint32_t status;
  611. struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
  612. if (at91sam7_info->cidr == 0)
  613. return ERROR_FLASH_BANK_NOT_PROBED;
  614. if (bank->target->state != TARGET_HALTED) {
  615. LOG_ERROR("Target not halted");
  616. return ERROR_TARGET_NOT_HALTED;
  617. }
  618. status = at91sam7_get_flash_status(bank->target, bank->bank_number);
  619. at91sam7_info->lockbits = (status >> 16);
  620. at91sam7_info->num_lockbits_on = 0;
  621. for (lock_pos = 0; lock_pos < bank->num_sectors; lock_pos++) {
  622. if (((status >> (16 + lock_pos))&(0x0001)) == 1) {
  623. at91sam7_info->num_lockbits_on++;
  624. bank->sectors[lock_pos].is_protected = 1;
  625. } else
  626. bank->sectors[lock_pos].is_protected = 0;
  627. }
  628. /* GPNVM and SECURITY bits apply only for MC_FSR of EFC0 */
  629. status = at91sam7_get_flash_status(bank->target, 0);
  630. at91sam7_info->securitybit = (status >> 4)&0x01;
  631. at91sam7_info->nvmbits = (status >> 8)&0xFF;
  632. at91sam7_info->num_nvmbits_on = 0;
  633. for (gpnvm_pos = 0; gpnvm_pos < at91sam7_info->num_nvmbits; gpnvm_pos++) {
  634. if (((status >> (8 + gpnvm_pos))&(0x01)) == 1)
  635. at91sam7_info->num_nvmbits_on++;
  636. }
  637. return ERROR_OK;
  638. }
  639. FLASH_BANK_COMMAND_HANDLER(at91sam7_flash_bank_command)
  640. {
  641. struct flash_bank *t_bank = bank;
  642. struct at91sam7_flash_bank *at91sam7_info;
  643. struct target *target = t_bank->target;
  644. uint32_t base_address;
  645. uint32_t bank_size;
  646. uint32_t ext_freq = 0;
  647. int chip_width;
  648. int bus_width;
  649. int banks_num;
  650. int num_sectors;
  651. uint16_t pages_per_sector;
  652. uint16_t page_size;
  653. uint16_t num_nvmbits;
  654. char *target_name_t;
  655. int bnk, sec;
  656. at91sam7_info = malloc(sizeof(struct at91sam7_flash_bank));
  657. t_bank->driver_priv = at91sam7_info;
  658. /* part wasn't probed for info yet */
  659. at91sam7_info->cidr = 0;
  660. at91sam7_info->flashmode = 0;
  661. at91sam7_info->ext_freq = 0;
  662. at91sam7_info->flash_autodetection = 0;
  663. if (CMD_ARGC < 13) {
  664. at91sam7_info->flash_autodetection = 1;
  665. return ERROR_OK;
  666. }
  667. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], base_address);
  668. COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], chip_width);
  669. COMMAND_PARSE_NUMBER(int, CMD_ARGV[4], bus_width);
  670. COMMAND_PARSE_NUMBER(int, CMD_ARGV[8], banks_num);
  671. COMMAND_PARSE_NUMBER(int, CMD_ARGV[9], num_sectors);
  672. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[10], pages_per_sector);
  673. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[11], page_size);
  674. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[12], num_nvmbits);
  675. if (CMD_ARGC == 14) {
  676. unsigned long freq;
  677. COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[13], freq);
  678. ext_freq = freq * 1000;
  679. at91sam7_info->ext_freq = ext_freq;
  680. }
  681. if ((bus_width == 0) || (banks_num == 0) || (num_sectors == 0) ||
  682. (pages_per_sector == 0) || (page_size == 0) || (num_nvmbits == 0)) {
  683. at91sam7_info->flash_autodetection = 1;
  684. return ERROR_OK;
  685. }
  686. target_name_t = calloc(strlen(CMD_ARGV[7]) + 1, sizeof(char));
  687. strcpy(target_name_t, CMD_ARGV[7]);
  688. /* calculate bank size */
  689. bank_size = num_sectors * pages_per_sector * page_size;
  690. for (bnk = 0; bnk < banks_num; bnk++) {
  691. if (bnk > 0) {
  692. if (!t_bank->next) {
  693. /* create a new bank element */
  694. struct flash_bank *fb = malloc(sizeof(struct flash_bank));
  695. fb->target = target;
  696. fb->driver = bank->driver;
  697. fb->driver_priv = malloc(sizeof(struct at91sam7_flash_bank));
  698. fb->name = "sam7_probed";
  699. fb->next = NULL;
  700. /* link created bank in 'flash_banks' list */
  701. t_bank->next = fb;
  702. }
  703. t_bank = t_bank->next;
  704. }
  705. t_bank->bank_number = bnk;
  706. t_bank->base = base_address + bnk * bank_size;
  707. t_bank->size = bank_size;
  708. t_bank->chip_width = chip_width;
  709. t_bank->bus_width = bus_width;
  710. t_bank->num_sectors = num_sectors;
  711. /* allocate sectors */
  712. t_bank->sectors = malloc(num_sectors * sizeof(struct flash_sector));
  713. for (sec = 0; sec < num_sectors; sec++) {
  714. t_bank->sectors[sec].offset = sec * pages_per_sector * page_size;
  715. t_bank->sectors[sec].size = pages_per_sector * page_size;
  716. t_bank->sectors[sec].is_erased = -1;
  717. t_bank->sectors[sec].is_protected = -1;
  718. }
  719. at91sam7_info = t_bank->driver_priv;
  720. at91sam7_info->target_name = target_name_t;
  721. at91sam7_info->flashmode = 0;
  722. at91sam7_info->ext_freq = ext_freq;
  723. at91sam7_info->num_nvmbits = num_nvmbits;
  724. at91sam7_info->num_nvmbits_on = 0;
  725. at91sam7_info->pagesize = page_size;
  726. at91sam7_info->pages_per_sector = pages_per_sector;
  727. }
  728. return ERROR_OK;
  729. }
  730. static int at91sam7_erase(struct flash_bank *bank, int first, int last)
  731. {
  732. struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
  733. int sec;
  734. uint32_t nbytes, pos;
  735. uint8_t *buffer;
  736. uint8_t erase_all;
  737. if (at91sam7_info->cidr == 0)
  738. return ERROR_FLASH_BANK_NOT_PROBED;
  739. if (bank->target->state != TARGET_HALTED) {
  740. LOG_ERROR("Target not halted");
  741. return ERROR_TARGET_NOT_HALTED;
  742. }
  743. if ((first < 0) || (last < first) || (last >= bank->num_sectors))
  744. return ERROR_FLASH_SECTOR_INVALID;
  745. erase_all = 0;
  746. if ((first == 0) && (last == (bank->num_sectors-1)))
  747. erase_all = 1;
  748. /* Configure the flash controller timing */
  749. at91sam7_read_clock_info(bank);
  750. at91sam7_set_flash_mode(bank, FMR_TIMING_FLASH);
  751. if (erase_all) {
  752. if (at91sam7_flash_command(bank, EA, 0) != ERROR_OK)
  753. return ERROR_FLASH_OPERATION_FAILED;
  754. } else {
  755. /* allocate and clean buffer */
  756. nbytes = (last - first + 1) * bank->sectors[first].size;
  757. buffer = malloc(nbytes * sizeof(uint8_t));
  758. for (pos = 0; pos < nbytes; pos++)
  759. buffer[pos] = 0xFF;
  760. if (at91sam7_write(bank, buffer, bank->sectors[first].offset, nbytes) != ERROR_OK) {
  761. free(buffer);
  762. return ERROR_FLASH_OPERATION_FAILED;
  763. }
  764. free(buffer);
  765. }
  766. /* mark erased sectors */
  767. for (sec = first; sec <= last; sec++)
  768. bank->sectors[sec].is_erased = 1;
  769. return ERROR_OK;
  770. }
  771. static int at91sam7_protect(struct flash_bank *bank, int set, int first, int last)
  772. {
  773. uint32_t cmd;
  774. int sector;
  775. uint32_t pagen;
  776. struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
  777. if (at91sam7_info->cidr == 0)
  778. return ERROR_FLASH_BANK_NOT_PROBED;
  779. if (bank->target->state != TARGET_HALTED) {
  780. LOG_ERROR("Target not halted");
  781. return ERROR_TARGET_NOT_HALTED;
  782. }
  783. if ((first < 0) || (last < first) || (last >= bank->num_sectors))
  784. return ERROR_FLASH_SECTOR_INVALID;
  785. /* Configure the flash controller timing */
  786. at91sam7_read_clock_info(bank);
  787. at91sam7_set_flash_mode(bank, FMR_TIMING_NVBITS);
  788. for (sector = first; sector <= last; sector++) {
  789. if (set)
  790. cmd = SLB;
  791. else
  792. cmd = CLB;
  793. /* if we lock a page from one sector then entire sector will be locked, also,
  794. * if we unlock a page from a locked sector, entire sector will be unlocked */
  795. pagen = sector * at91sam7_info->pages_per_sector;
  796. if (at91sam7_flash_command(bank, cmd, pagen) != ERROR_OK)
  797. return ERROR_FLASH_OPERATION_FAILED;
  798. }
  799. at91sam7_protect_check(bank);
  800. return ERROR_OK;
  801. }
  802. static int at91sam7_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
  803. {
  804. int retval;
  805. struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
  806. struct target *target = bank->target;
  807. uint32_t dst_min_alignment, wcount, bytes_remaining = count;
  808. uint32_t first_page, last_page, pagen, buffer_pos;
  809. if (at91sam7_info->cidr == 0)
  810. return ERROR_FLASH_BANK_NOT_PROBED;
  811. if (bank->target->state != TARGET_HALTED) {
  812. LOG_ERROR("Target not halted");
  813. return ERROR_TARGET_NOT_HALTED;
  814. }
  815. if (offset + count > bank->size)
  816. return ERROR_FLASH_DST_OUT_OF_BANK;
  817. dst_min_alignment = at91sam7_info->pagesize;
  818. if (offset % dst_min_alignment) {
  819. LOG_WARNING("offset 0x%" PRIx32 " breaks required alignment 0x%" PRIx32 "",
  820. offset,
  821. dst_min_alignment);
  822. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  823. }
  824. if (at91sam7_info->cidr_arch == 0)
  825. return ERROR_FLASH_BANK_NOT_PROBED;
  826. first_page = offset/dst_min_alignment;
  827. last_page = DIV_ROUND_UP(offset + count, dst_min_alignment);
  828. LOG_DEBUG("first_page: %i, last_page: %i, count %i",
  829. (int)first_page,
  830. (int)last_page,
  831. (int)count);
  832. /* Configure the flash controller timing */
  833. at91sam7_read_clock_info(bank);
  834. at91sam7_set_flash_mode(bank, FMR_TIMING_FLASH);
  835. for (pagen = first_page; pagen < last_page; pagen++) {
  836. if (bytes_remaining < dst_min_alignment)
  837. count = bytes_remaining;
  838. else
  839. count = dst_min_alignment;
  840. bytes_remaining -= count;
  841. /* Write one block to the PageWriteBuffer */
  842. buffer_pos = (pagen-first_page)*dst_min_alignment;
  843. wcount = DIV_ROUND_UP(count, 4);
  844. retval = target_write_memory(target, bank->base + pagen*dst_min_alignment, 4,
  845. wcount, buffer + buffer_pos);
  846. if (retval != ERROR_OK)
  847. return retval;
  848. /* Send Write Page command to Flash Controller */
  849. if (at91sam7_flash_command(bank, WP, pagen) != ERROR_OK)
  850. return ERROR_FLASH_OPERATION_FAILED;
  851. LOG_DEBUG("Write flash bank:%i page number:%" PRIi32 "", bank->bank_number, pagen);
  852. }
  853. return ERROR_OK;
  854. }
  855. static int at91sam7_probe(struct flash_bank *bank)
  856. {
  857. /* we can't probe on an at91sam7
  858. * if this is an at91sam7, it has the configured flash */
  859. int retval;
  860. if (bank->target->state != TARGET_HALTED) {
  861. LOG_ERROR("Target not halted");
  862. return ERROR_TARGET_NOT_HALTED;
  863. }
  864. retval = at91sam7_read_part_info(bank);
  865. if (retval != ERROR_OK)
  866. return retval;
  867. return ERROR_OK;
  868. }
  869. static int get_at91sam7_info(struct flash_bank *bank, char *buf, int buf_size)
  870. {
  871. int printed;
  872. struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
  873. if (at91sam7_info->cidr == 0)
  874. return ERROR_FLASH_BANK_NOT_PROBED;
  875. printed = snprintf(buf, buf_size,
  876. "\n at91sam7 driver information: Chip is %s\n",
  877. at91sam7_info->target_name);
  878. buf += printed;
  879. buf_size -= printed;
  880. printed = snprintf(buf,
  881. buf_size,
  882. " Cidr: 0x%8.8" PRIx32 " | Arch: 0x%4.4x | Eproc: %s | Version: 0x%3.3x | "
  883. "Flashsize: 0x%8.8" PRIx32 "\n",
  884. at91sam7_info->cidr,
  885. at91sam7_info->cidr_arch,
  886. EPROC[at91sam7_info->cidr_eproc],
  887. at91sam7_info->cidr_version,
  888. bank->size);
  889. buf += printed;
  890. buf_size -= printed;
  891. printed = snprintf(buf, buf_size,
  892. " Master clock (estimated): %u KHz | External clock: %u KHz\n",
  893. (unsigned)(at91sam7_info->mck_freq / 1000),
  894. (unsigned)(at91sam7_info->ext_freq / 1000));
  895. buf += printed;
  896. buf_size -= printed;
  897. printed = snprintf(buf,
  898. buf_size,
  899. " Pagesize: %i bytes | Lockbits(%i): %i 0x%4.4x | Pages in lock region: %i\n",
  900. at91sam7_info->pagesize,
  901. bank->num_sectors,
  902. at91sam7_info->num_lockbits_on,
  903. at91sam7_info->lockbits,
  904. at91sam7_info->pages_per_sector*at91sam7_info->num_lockbits_on);
  905. buf += printed;
  906. buf_size -= printed;
  907. snprintf(buf, buf_size,
  908. " Securitybit: %i | Nvmbits(%i): %i 0x%1.1x\n",
  909. at91sam7_info->securitybit, at91sam7_info->num_nvmbits,
  910. at91sam7_info->num_nvmbits_on, at91sam7_info->nvmbits);
  911. return ERROR_OK;
  912. }
  913. /*
  914. * On AT91SAM7S: When the gpnvm bits are set with
  915. * > at91sam7 gpnvm bitnr set
  916. * the changes are not visible in the flash controller status register MC_FSR
  917. * until the processor has been reset.
  918. * On the Olimex board this requires a power cycle.
  919. * Note that the AT91SAM7S has the following errata (doc6175.pdf sec 14.1.3):
  920. * The maximum number of write/erase cycles for Non volatile Memory bits is 100. this includes
  921. * Lock Bits (LOCKx), General Purpose NVM bits (GPNVMx) and the Security Bit.
  922. */
  923. COMMAND_HANDLER(at91sam7_handle_gpnvm_command)
  924. {
  925. struct flash_bank *bank;
  926. int bit;
  927. uint8_t flashcmd;
  928. uint32_t status;
  929. struct at91sam7_flash_bank *at91sam7_info;
  930. int retval;
  931. if (CMD_ARGC != 2)
  932. return ERROR_COMMAND_SYNTAX_ERROR;
  933. bank = get_flash_bank_by_num_noprobe(0);
  934. if (bank == NULL)
  935. return ERROR_FLASH_BANK_INVALID;
  936. if (strcmp(bank->driver->name, "at91sam7")) {
  937. command_print(CMD_CTX, "not an at91sam7 flash bank '%s'", CMD_ARGV[0]);
  938. return ERROR_FLASH_BANK_INVALID;
  939. }
  940. if (bank->target->state != TARGET_HALTED) {
  941. LOG_ERROR("target has to be halted to perform flash operation");
  942. return ERROR_TARGET_NOT_HALTED;
  943. }
  944. if (strcmp(CMD_ARGV[1], "set") == 0)
  945. flashcmd = SGPB;
  946. else if (strcmp(CMD_ARGV[1], "clear") == 0)
  947. flashcmd = CGPB;
  948. else
  949. return ERROR_COMMAND_SYNTAX_ERROR;
  950. at91sam7_info = bank->driver_priv;
  951. if (at91sam7_info->cidr == 0) {
  952. retval = at91sam7_read_part_info(bank);
  953. if (retval != ERROR_OK)
  954. return retval;
  955. }
  956. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], bit);
  957. if ((bit < 0) || (bit >= at91sam7_info->num_nvmbits)) {
  958. command_print(CMD_CTX,
  959. "gpnvm bit '#%s' is out of bounds for target %s",
  960. CMD_ARGV[0],
  961. at91sam7_info->target_name);
  962. return ERROR_OK;
  963. }
  964. /* Configure the flash controller timing */
  965. at91sam7_read_clock_info(bank);
  966. at91sam7_set_flash_mode(bank, FMR_TIMING_NVBITS);
  967. if (at91sam7_flash_command(bank, flashcmd, bit) != ERROR_OK)
  968. return ERROR_FLASH_OPERATION_FAILED;
  969. /* GPNVM and SECURITY bits apply only for MC_FSR of EFC0 */
  970. status = at91sam7_get_flash_status(bank->target, 0);
  971. LOG_DEBUG("at91sam7_handle_gpnvm_command: cmd 0x%x, value %d, status 0x%" PRIx32,
  972. flashcmd,
  973. bit,
  974. status);
  975. /* check protect state */
  976. at91sam7_protect_check(bank);
  977. return ERROR_OK;
  978. }
  979. static const struct command_registration at91sam7_exec_command_handlers[] = {
  980. {
  981. .name = "gpnvm",
  982. .handler = at91sam7_handle_gpnvm_command,
  983. .mode = COMMAND_EXEC,
  984. .help = "set or clear one General Purpose Non-Volatile Memory "
  985. "(gpnvm) bit",
  986. .usage = "bitnum ('set'|'clear')",
  987. },
  988. COMMAND_REGISTRATION_DONE
  989. };
  990. static const struct command_registration at91sam7_command_handlers[] = {
  991. {
  992. .name = "at91sam7",
  993. .mode = COMMAND_ANY,
  994. .help = "at91sam7 flash command group",
  995. .usage = "",
  996. .chain = at91sam7_exec_command_handlers,
  997. },
  998. COMMAND_REGISTRATION_DONE
  999. };
  1000. struct flash_driver at91sam7_flash = {
  1001. .name = "at91sam7",
  1002. .usage = "gpnvm <bit> <set | clear>",
  1003. .commands = at91sam7_command_handlers,
  1004. .flash_bank_command = at91sam7_flash_bank_command,
  1005. .erase = at91sam7_erase,
  1006. .protect = at91sam7_protect,
  1007. .write = at91sam7_write,
  1008. .read = default_flash_read,
  1009. .probe = at91sam7_probe,
  1010. .auto_probe = at91sam7_probe,
  1011. .erase_check = at91sam7_erase_check,
  1012. .protect_check = at91sam7_protect_check,
  1013. .info = get_at91sam7_info,
  1014. };