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.
 
 
 
 
 
 

3768 lines
92 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2009 by Duane Ellis *
  3. * openocd@duaneellis.com *
  4. * *
  5. * Copyright (C) 2010 by Olaf Lüke (at91sam3s* support) *
  6. * olaf@uni-paderborn.de *
  7. * *
  8. * Copyright (C) 2011 by Olivier Schonken (at91sam3x* support) * *
  9. * and Jim Norris *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. * This program is distributed in the hope that it will be useful, *
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  18. * GNU General Public License for more details. *
  19. * *
  20. * You should have received a copy of the GNU General Public License *
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  22. ****************************************************************************/
  23. /* Some of the lower level code was based on code supplied by
  24. * ATMEL under this copyright. */
  25. /* BEGIN ATMEL COPYRIGHT */
  26. /* ----------------------------------------------------------------------------
  27. * ATMEL Microcontroller Software Support
  28. * ----------------------------------------------------------------------------
  29. * Copyright (c) 2009, Atmel Corporation
  30. *
  31. * All rights reserved.
  32. *
  33. * Redistribution and use in source and binary forms, with or without
  34. * modification, are permitted provided that the following conditions are met:
  35. *
  36. * - Redistributions of source code must retain the above copyright notice,
  37. * this list of conditions and the disclaimer below.
  38. *
  39. * Atmel's name may not be used to endorse or promote products derived from
  40. * this software without specific prior written permission.
  41. *
  42. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
  43. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  44. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  45. * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
  46. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  47. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  48. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  49. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  50. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  51. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ----------------------------------------------------------------------------
  53. */
  54. /* END ATMEL COPYRIGHT */
  55. #ifdef HAVE_CONFIG_H
  56. #include "config.h"
  57. #endif
  58. #include "imp.h"
  59. #include <helper/time_support.h>
  60. #define REG_NAME_WIDTH (12)
  61. /* at91sam3u series (has one or two flash banks) */
  62. #define FLASH_BANK0_BASE_U 0x00080000
  63. #define FLASH_BANK1_BASE_U 0x00100000
  64. /* at91sam3s series (has always one flash bank) */
  65. #define FLASH_BANK_BASE_S 0x00400000
  66. /* at91sam3sd series (has always two flash banks) */
  67. #define FLASH_BANK0_BASE_SD FLASH_BANK_BASE_S
  68. #define FLASH_BANK1_BASE_512K_SD (FLASH_BANK0_BASE_SD+(512*1024/2))
  69. /* at91sam3n series (has always one flash bank) */
  70. #define FLASH_BANK_BASE_N 0x00400000
  71. /* at91sam3a/x series has two flash banks*/
  72. #define FLASH_BANK0_BASE_AX 0x00080000
  73. /*Bank 1 of the at91sam3a/x series starts at 0x00080000 + half flash size*/
  74. #define FLASH_BANK1_BASE_256K_AX 0x000A0000
  75. #define FLASH_BANK1_BASE_512K_AX 0x000C0000
  76. #define AT91C_EFC_FCMD_GETD (0x0) /* (EFC) Get Flash Descriptor */
  77. #define AT91C_EFC_FCMD_WP (0x1) /* (EFC) Write Page */
  78. #define AT91C_EFC_FCMD_WPL (0x2) /* (EFC) Write Page and Lock */
  79. #define AT91C_EFC_FCMD_EWP (0x3) /* (EFC) Erase Page and Write Page */
  80. #define AT91C_EFC_FCMD_EWPL (0x4) /* (EFC) Erase Page and Write Page then Lock */
  81. #define AT91C_EFC_FCMD_EA (0x5) /* (EFC) Erase All */
  82. /* cmd6 is not present in the at91sam3u4/2/1 data sheet table 17-2 */
  83. /* #define AT91C_EFC_FCMD_EPL (0x6) // (EFC) Erase plane? */
  84. /* cmd7 is not present in the at91sam3u4/2/1 data sheet table 17-2 */
  85. /* #define AT91C_EFC_FCMD_EPA (0x7) // (EFC) Erase pages? */
  86. #define AT91C_EFC_FCMD_SLB (0x8) /* (EFC) Set Lock Bit */
  87. #define AT91C_EFC_FCMD_CLB (0x9) /* (EFC) Clear Lock Bit */
  88. #define AT91C_EFC_FCMD_GLB (0xA) /* (EFC) Get Lock Bit */
  89. #define AT91C_EFC_FCMD_SFB (0xB) /* (EFC) Set Fuse Bit */
  90. #define AT91C_EFC_FCMD_CFB (0xC) /* (EFC) Clear Fuse Bit */
  91. #define AT91C_EFC_FCMD_GFB (0xD) /* (EFC) Get Fuse Bit */
  92. #define AT91C_EFC_FCMD_STUI (0xE) /* (EFC) Start Read Unique ID */
  93. #define AT91C_EFC_FCMD_SPUI (0xF) /* (EFC) Stop Read Unique ID */
  94. #define OFFSET_EFC_FMR 0
  95. #define OFFSET_EFC_FCR 4
  96. #define OFFSET_EFC_FSR 8
  97. #define OFFSET_EFC_FRR 12
  98. extern const struct flash_driver at91sam3_flash;
  99. static float _tomhz(uint32_t freq_hz)
  100. {
  101. float f;
  102. f = ((float)(freq_hz)) / 1000000.0;
  103. return f;
  104. }
  105. /* How the chip is configured. */
  106. struct sam3_cfg {
  107. uint32_t unique_id[4];
  108. uint32_t slow_freq;
  109. uint32_t rc_freq;
  110. uint32_t mainosc_freq;
  111. uint32_t plla_freq;
  112. uint32_t mclk_freq;
  113. uint32_t cpu_freq;
  114. uint32_t fclk_freq;
  115. uint32_t pclk0_freq;
  116. uint32_t pclk1_freq;
  117. uint32_t pclk2_freq;
  118. #define SAM3_CHIPID_CIDR (0x400E0740)
  119. uint32_t CHIPID_CIDR;
  120. #define SAM3_CHIPID_CIDR2 (0x400E0940) /*SAM3X and SAM3A cidr at this address*/
  121. uint32_t CHIPID_CIDR2;
  122. #define SAM3_CHIPID_EXID (0x400E0744)
  123. uint32_t CHIPID_EXID;
  124. #define SAM3_CHIPID_EXID2 (0x400E0944) /*SAM3X and SAM3A cidr at this address*/
  125. uint32_t CHIPID_EXID2;
  126. #define SAM3_PMC_BASE (0x400E0400)
  127. #define SAM3_PMC_SCSR (SAM3_PMC_BASE + 0x0008)
  128. uint32_t PMC_SCSR;
  129. #define SAM3_PMC_PCSR (SAM3_PMC_BASE + 0x0018)
  130. uint32_t PMC_PCSR;
  131. #define SAM3_CKGR_UCKR (SAM3_PMC_BASE + 0x001c)
  132. uint32_t CKGR_UCKR;
  133. #define SAM3_CKGR_MOR (SAM3_PMC_BASE + 0x0020)
  134. uint32_t CKGR_MOR;
  135. #define SAM3_CKGR_MCFR (SAM3_PMC_BASE + 0x0024)
  136. uint32_t CKGR_MCFR;
  137. #define SAM3_CKGR_PLLAR (SAM3_PMC_BASE + 0x0028)
  138. uint32_t CKGR_PLLAR;
  139. #define SAM3_PMC_MCKR (SAM3_PMC_BASE + 0x0030)
  140. uint32_t PMC_MCKR;
  141. #define SAM3_PMC_PCK0 (SAM3_PMC_BASE + 0x0040)
  142. uint32_t PMC_PCK0;
  143. #define SAM3_PMC_PCK1 (SAM3_PMC_BASE + 0x0044)
  144. uint32_t PMC_PCK1;
  145. #define SAM3_PMC_PCK2 (SAM3_PMC_BASE + 0x0048)
  146. uint32_t PMC_PCK2;
  147. #define SAM3_PMC_SR (SAM3_PMC_BASE + 0x0068)
  148. uint32_t PMC_SR;
  149. #define SAM3_PMC_IMR (SAM3_PMC_BASE + 0x006c)
  150. uint32_t PMC_IMR;
  151. #define SAM3_PMC_FSMR (SAM3_PMC_BASE + 0x0070)
  152. uint32_t PMC_FSMR;
  153. #define SAM3_PMC_FSPR (SAM3_PMC_BASE + 0x0074)
  154. uint32_t PMC_FSPR;
  155. };
  156. /*
  157. * The AT91SAM3N data sheet 04-Oct-2010, AT91SAM3U data sheet 22-Aug-2011
  158. * and AT91SAM3S data sheet 09-Feb-2011 state that for flash writes
  159. * the flash wait state (FWS) should be set to 6. It seems like that the
  160. * cause of the problem is not the flash itself, but the flash write
  161. * buffer. Ie the wait states have to be set before writing into the
  162. * buffer.
  163. * Tested and confirmed with SAM3N and SAM3U
  164. */
  165. struct sam3_bank_private {
  166. bool probed;
  167. /* DANGER: THERE ARE DRAGONS HERE.. */
  168. /* NOTE: If you add more 'ghost' pointers */
  169. /* be aware that you must *manually* update */
  170. /* these pointers in the function sam3_get_details() */
  171. /* See the comment "Here there be dragons" */
  172. /* so we can find the chip we belong to */
  173. struct sam3_chip *chip;
  174. /* so we can find the original bank pointer */
  175. struct flash_bank *bank;
  176. unsigned bank_number;
  177. uint32_t controller_address;
  178. uint32_t base_address;
  179. uint32_t flash_wait_states;
  180. bool present;
  181. unsigned size_bytes;
  182. unsigned nsectors;
  183. unsigned sector_size;
  184. unsigned page_size;
  185. };
  186. struct sam3_chip_details {
  187. /* THERE ARE DRAGONS HERE.. */
  188. /* note: If you add pointers here */
  189. /* be careful about them as they */
  190. /* may need to be updated inside */
  191. /* the function: "sam3_get_details() */
  192. /* which copy/overwrites the */
  193. /* 'runtime' copy of this structure */
  194. uint32_t chipid_cidr;
  195. const char *name;
  196. unsigned n_gpnvms;
  197. #define SAM3_N_NVM_BITS 3
  198. unsigned gpnvm[SAM3_N_NVM_BITS];
  199. unsigned total_flash_size;
  200. unsigned total_sram_size;
  201. unsigned n_banks;
  202. #define SAM3_MAX_FLASH_BANKS 2
  203. /* these are "initialized" from the global const data */
  204. struct sam3_bank_private bank[SAM3_MAX_FLASH_BANKS];
  205. };
  206. struct sam3_chip {
  207. struct sam3_chip *next;
  208. bool probed;
  209. /* this is "initialized" from the global const structure */
  210. struct sam3_chip_details details;
  211. struct target *target;
  212. struct sam3_cfg cfg;
  213. };
  214. struct sam3_reg_list {
  215. uint32_t address; size_t struct_offset; const char *name;
  216. void (*explain_func)(struct sam3_chip *chip);
  217. };
  218. static struct sam3_chip *all_sam3_chips;
  219. static struct sam3_chip *get_current_sam3(struct command_invocation *cmd)
  220. {
  221. struct target *t;
  222. static struct sam3_chip *p;
  223. t = get_current_target(cmd->ctx);
  224. if (!t) {
  225. command_print_sameline(cmd, "No current target?\n");
  226. return NULL;
  227. }
  228. p = all_sam3_chips;
  229. if (!p) {
  230. /* this should not happen */
  231. /* the command is not registered until the chip is created? */
  232. command_print_sameline(cmd, "No SAM3 chips exist?\n");
  233. return NULL;
  234. }
  235. while (p) {
  236. if (p->target == t)
  237. return p;
  238. p = p->next;
  239. }
  240. command_print_sameline(cmd, "Cannot find SAM3 chip?\n");
  241. return NULL;
  242. }
  243. /* these are used to *initialize* the "chip->details" structure. */
  244. static const struct sam3_chip_details all_sam3_details[] = {
  245. /* Start at91sam3u* series */
  246. {
  247. .chipid_cidr = 0x28100960,
  248. .name = "at91sam3u4e",
  249. .total_flash_size = 256 * 1024,
  250. .total_sram_size = 52 * 1024,
  251. .n_gpnvms = 3,
  252. .n_banks = 2,
  253. /* System boots at address 0x0 */
  254. /* gpnvm[1] = selects boot code */
  255. /* if gpnvm[1] == 0 */
  256. /* boot is via "SAMBA" (rom) */
  257. /* else */
  258. /* boot is via FLASH */
  259. /* Selection is via gpnvm[2] */
  260. /* endif */
  261. /* */
  262. /* NOTE: banks 0 & 1 switch places */
  263. /* if gpnvm[2] == 0 */
  264. /* Bank0 is the boot rom */
  265. /* else */
  266. /* Bank1 is the boot rom */
  267. /* endif */
  268. /* .bank[0] = { */
  269. {
  270. {
  271. .probed = false,
  272. .chip = NULL,
  273. .bank = NULL,
  274. .bank_number = 0,
  275. .base_address = FLASH_BANK0_BASE_U,
  276. .controller_address = 0x400e0800,
  277. .flash_wait_states = 6, /* workaround silicon bug */
  278. .present = 1,
  279. .size_bytes = 128 * 1024,
  280. .nsectors = 16,
  281. .sector_size = 8192,
  282. .page_size = 256,
  283. },
  284. /* .bank[1] = { */
  285. {
  286. .probed = false,
  287. .chip = NULL,
  288. .bank = NULL,
  289. .bank_number = 1,
  290. .base_address = FLASH_BANK1_BASE_U,
  291. .controller_address = 0x400e0a00,
  292. .flash_wait_states = 6, /* workaround silicon bug */
  293. .present = 1,
  294. .size_bytes = 128 * 1024,
  295. .nsectors = 16,
  296. .sector_size = 8192,
  297. .page_size = 256,
  298. },
  299. },
  300. },
  301. {
  302. .chipid_cidr = 0x281a0760,
  303. .name = "at91sam3u2e",
  304. .total_flash_size = 128 * 1024,
  305. .total_sram_size = 36 * 1024,
  306. .n_gpnvms = 2,
  307. .n_banks = 1,
  308. /* System boots at address 0x0 */
  309. /* gpnvm[1] = selects boot code */
  310. /* if gpnvm[1] == 0 */
  311. /* boot is via "SAMBA" (rom) */
  312. /* else */
  313. /* boot is via FLASH */
  314. /* Selection is via gpnvm[2] */
  315. /* endif */
  316. /* .bank[0] = { */
  317. {
  318. {
  319. .probed = false,
  320. .chip = NULL,
  321. .bank = NULL,
  322. .bank_number = 0,
  323. .base_address = FLASH_BANK0_BASE_U,
  324. .controller_address = 0x400e0800,
  325. .flash_wait_states = 6, /* workaround silicon bug */
  326. .present = 1,
  327. .size_bytes = 128 * 1024,
  328. .nsectors = 16,
  329. .sector_size = 8192,
  330. .page_size = 256,
  331. },
  332. /* .bank[1] = { */
  333. {
  334. .present = 0,
  335. .probed = false,
  336. .bank_number = 1,
  337. },
  338. },
  339. },
  340. {
  341. .chipid_cidr = 0x28190560,
  342. .name = "at91sam3u1e",
  343. .total_flash_size = 64 * 1024,
  344. .total_sram_size = 20 * 1024,
  345. .n_gpnvms = 2,
  346. .n_banks = 1,
  347. /* System boots at address 0x0 */
  348. /* gpnvm[1] = selects boot code */
  349. /* if gpnvm[1] == 0 */
  350. /* boot is via "SAMBA" (rom) */
  351. /* else */
  352. /* boot is via FLASH */
  353. /* Selection is via gpnvm[2] */
  354. /* endif */
  355. /* */
  356. /* .bank[0] = { */
  357. {
  358. {
  359. .probed = false,
  360. .chip = NULL,
  361. .bank = NULL,
  362. .bank_number = 0,
  363. .base_address = FLASH_BANK0_BASE_U,
  364. .controller_address = 0x400e0800,
  365. .flash_wait_states = 6, /* workaround silicon bug */
  366. .present = 1,
  367. .size_bytes = 64 * 1024,
  368. .nsectors = 8,
  369. .sector_size = 8192,
  370. .page_size = 256,
  371. },
  372. /* .bank[1] = { */
  373. {
  374. .present = 0,
  375. .probed = false,
  376. .bank_number = 1,
  377. },
  378. },
  379. },
  380. {
  381. .chipid_cidr = 0x28000960,
  382. .name = "at91sam3u4c",
  383. .total_flash_size = 256 * 1024,
  384. .total_sram_size = 52 * 1024,
  385. .n_gpnvms = 3,
  386. .n_banks = 2,
  387. /* System boots at address 0x0 */
  388. /* gpnvm[1] = selects boot code */
  389. /* if gpnvm[1] == 0 */
  390. /* boot is via "SAMBA" (rom) */
  391. /* else */
  392. /* boot is via FLASH */
  393. /* Selection is via gpnvm[2] */
  394. /* endif */
  395. /* */
  396. /* NOTE: banks 0 & 1 switch places */
  397. /* if gpnvm[2] == 0 */
  398. /* Bank0 is the boot rom */
  399. /* else */
  400. /* Bank1 is the boot rom */
  401. /* endif */
  402. {
  403. {
  404. /* .bank[0] = { */
  405. .probed = false,
  406. .chip = NULL,
  407. .bank = NULL,
  408. .bank_number = 0,
  409. .base_address = FLASH_BANK0_BASE_U,
  410. .controller_address = 0x400e0800,
  411. .flash_wait_states = 6, /* workaround silicon bug */
  412. .present = 1,
  413. .size_bytes = 128 * 1024,
  414. .nsectors = 16,
  415. .sector_size = 8192,
  416. .page_size = 256,
  417. },
  418. /* .bank[1] = { */
  419. {
  420. .probed = false,
  421. .chip = NULL,
  422. .bank = NULL,
  423. .bank_number = 1,
  424. .base_address = FLASH_BANK1_BASE_U,
  425. .controller_address = 0x400e0a00,
  426. .flash_wait_states = 6, /* workaround silicon bug */
  427. .present = 1,
  428. .size_bytes = 128 * 1024,
  429. .nsectors = 16,
  430. .sector_size = 8192,
  431. .page_size = 256,
  432. },
  433. },
  434. },
  435. {
  436. .chipid_cidr = 0x280a0760,
  437. .name = "at91sam3u2c",
  438. .total_flash_size = 128 * 1024,
  439. .total_sram_size = 36 * 1024,
  440. .n_gpnvms = 2,
  441. .n_banks = 1,
  442. /* System boots at address 0x0 */
  443. /* gpnvm[1] = selects boot code */
  444. /* if gpnvm[1] == 0 */
  445. /* boot is via "SAMBA" (rom) */
  446. /* else */
  447. /* boot is via FLASH */
  448. /* Selection is via gpnvm[2] */
  449. /* endif */
  450. {
  451. /* .bank[0] = { */
  452. {
  453. .probed = false,
  454. .chip = NULL,
  455. .bank = NULL,
  456. .bank_number = 0,
  457. .base_address = FLASH_BANK0_BASE_U,
  458. .controller_address = 0x400e0800,
  459. .flash_wait_states = 6, /* workaround silicon bug */
  460. .present = 1,
  461. .size_bytes = 128 * 1024,
  462. .nsectors = 16,
  463. .sector_size = 8192,
  464. .page_size = 256,
  465. },
  466. /* .bank[1] = { */
  467. {
  468. .present = 0,
  469. .probed = false,
  470. .bank_number = 1,
  471. },
  472. },
  473. },
  474. {
  475. .chipid_cidr = 0x28090560,
  476. .name = "at91sam3u1c",
  477. .total_flash_size = 64 * 1024,
  478. .total_sram_size = 20 * 1024,
  479. .n_gpnvms = 2,
  480. .n_banks = 1,
  481. /* System boots at address 0x0 */
  482. /* gpnvm[1] = selects boot code */
  483. /* if gpnvm[1] == 0 */
  484. /* boot is via "SAMBA" (rom) */
  485. /* else */
  486. /* boot is via FLASH */
  487. /* Selection is via gpnvm[2] */
  488. /* endif */
  489. /* */
  490. {
  491. /* .bank[0] = { */
  492. {
  493. .probed = false,
  494. .chip = NULL,
  495. .bank = NULL,
  496. .bank_number = 0,
  497. .base_address = FLASH_BANK0_BASE_U,
  498. .controller_address = 0x400e0800,
  499. .flash_wait_states = 6, /* workaround silicon bug */
  500. .present = 1,
  501. .size_bytes = 64 * 1024,
  502. .nsectors = 8,
  503. .sector_size = 8192,
  504. .page_size = 256,
  505. },
  506. /* .bank[1] = { */
  507. {
  508. .present = 0,
  509. .probed = false,
  510. .bank_number = 1,
  511. },
  512. },
  513. },
  514. /* Start at91sam3s* series */
  515. /* Note: The preliminary at91sam3s datasheet says on page 302 */
  516. /* that the flash controller is at address 0x400E0800. */
  517. /* This is _not_ the case, the controller resides at address 0x400e0a00. */
  518. {
  519. .chipid_cidr = 0x28A00960,
  520. .name = "at91sam3s4c",
  521. .total_flash_size = 256 * 1024,
  522. .total_sram_size = 48 * 1024,
  523. .n_gpnvms = 2,
  524. .n_banks = 1,
  525. {
  526. /* .bank[0] = { */
  527. {
  528. .probed = false,
  529. .chip = NULL,
  530. .bank = NULL,
  531. .bank_number = 0,
  532. .base_address = FLASH_BANK_BASE_S,
  533. .controller_address = 0x400e0a00,
  534. .flash_wait_states = 6, /* workaround silicon bug */
  535. .present = 1,
  536. .size_bytes = 256 * 1024,
  537. .nsectors = 16,
  538. .sector_size = 16384,
  539. .page_size = 256,
  540. },
  541. /* .bank[1] = { */
  542. {
  543. .present = 0,
  544. .probed = false,
  545. .bank_number = 1,
  546. },
  547. },
  548. },
  549. {
  550. .chipid_cidr = 0x28900960,
  551. .name = "at91sam3s4b",
  552. .total_flash_size = 256 * 1024,
  553. .total_sram_size = 48 * 1024,
  554. .n_gpnvms = 2,
  555. .n_banks = 1,
  556. {
  557. /* .bank[0] = { */
  558. {
  559. .probed = false,
  560. .chip = NULL,
  561. .bank = NULL,
  562. .bank_number = 0,
  563. .base_address = FLASH_BANK_BASE_S,
  564. .controller_address = 0x400e0a00,
  565. .flash_wait_states = 6, /* workaround silicon bug */
  566. .present = 1,
  567. .size_bytes = 256 * 1024,
  568. .nsectors = 16,
  569. .sector_size = 16384,
  570. .page_size = 256,
  571. },
  572. /* .bank[1] = { */
  573. {
  574. .present = 0,
  575. .probed = false,
  576. .bank_number = 1,
  577. },
  578. },
  579. },
  580. {
  581. .chipid_cidr = 0x28800960,
  582. .name = "at91sam3s4a",
  583. .total_flash_size = 256 * 1024,
  584. .total_sram_size = 48 * 1024,
  585. .n_gpnvms = 2,
  586. .n_banks = 1,
  587. {
  588. /* .bank[0] = { */
  589. {
  590. .probed = false,
  591. .chip = NULL,
  592. .bank = NULL,
  593. .bank_number = 0,
  594. .base_address = FLASH_BANK_BASE_S,
  595. .controller_address = 0x400e0a00,
  596. .flash_wait_states = 6, /* workaround silicon bug */
  597. .present = 1,
  598. .size_bytes = 256 * 1024,
  599. .nsectors = 16,
  600. .sector_size = 16384,
  601. .page_size = 256,
  602. },
  603. /* .bank[1] = { */
  604. {
  605. .present = 0,
  606. .probed = false,
  607. .bank_number = 1,
  608. },
  609. },
  610. },
  611. {
  612. .chipid_cidr = 0x28AA0760,
  613. .name = "at91sam3s2c",
  614. .total_flash_size = 128 * 1024,
  615. .total_sram_size = 32 * 1024,
  616. .n_gpnvms = 2,
  617. .n_banks = 1,
  618. {
  619. /* .bank[0] = { */
  620. {
  621. .probed = false,
  622. .chip = NULL,
  623. .bank = NULL,
  624. .bank_number = 0,
  625. .base_address = FLASH_BANK_BASE_S,
  626. .controller_address = 0x400e0a00,
  627. .flash_wait_states = 6, /* workaround silicon bug */
  628. .present = 1,
  629. .size_bytes = 128 * 1024,
  630. .nsectors = 8,
  631. .sector_size = 16384,
  632. .page_size = 256,
  633. },
  634. /* .bank[1] = { */
  635. {
  636. .present = 0,
  637. .probed = false,
  638. .bank_number = 1,
  639. },
  640. },
  641. },
  642. {
  643. .chipid_cidr = 0x289A0760,
  644. .name = "at91sam3s2b",
  645. .total_flash_size = 128 * 1024,
  646. .total_sram_size = 32 * 1024,
  647. .n_gpnvms = 2,
  648. .n_banks = 1,
  649. {
  650. /* .bank[0] = { */
  651. {
  652. .probed = false,
  653. .chip = NULL,
  654. .bank = NULL,
  655. .bank_number = 0,
  656. .base_address = FLASH_BANK_BASE_S,
  657. .controller_address = 0x400e0a00,
  658. .flash_wait_states = 6, /* workaround silicon bug */
  659. .present = 1,
  660. .size_bytes = 128 * 1024,
  661. .nsectors = 8,
  662. .sector_size = 16384,
  663. .page_size = 256,
  664. },
  665. /* .bank[1] = { */
  666. {
  667. .present = 0,
  668. .probed = false,
  669. .bank_number = 1,
  670. },
  671. },
  672. },
  673. {
  674. .chipid_cidr = 0x298B0A60,
  675. .name = "at91sam3sd8a",
  676. .total_flash_size = 512 * 1024,
  677. .total_sram_size = 64 * 1024,
  678. .n_gpnvms = 3,
  679. .n_banks = 2,
  680. {
  681. /* .bank[0] = { */
  682. {
  683. .probed = false,
  684. .chip = NULL,
  685. .bank = NULL,
  686. .bank_number = 0,
  687. .base_address = FLASH_BANK0_BASE_SD,
  688. .controller_address = 0x400e0a00,
  689. .flash_wait_states = 6, /* workaround silicon bug */
  690. .present = 1,
  691. .size_bytes = 256 * 1024,
  692. .nsectors = 16,
  693. .sector_size = 32768,
  694. .page_size = 256,
  695. },
  696. /* .bank[1] = { */
  697. {
  698. .probed = false,
  699. .chip = NULL,
  700. .bank = NULL,
  701. .bank_number = 1,
  702. .base_address = FLASH_BANK1_BASE_512K_SD,
  703. .controller_address = 0x400e0a00,
  704. .flash_wait_states = 6, /* workaround silicon bug */
  705. .present = 1,
  706. .size_bytes = 256 * 1024,
  707. .nsectors = 16,
  708. .sector_size = 32768,
  709. .page_size = 256,
  710. },
  711. },
  712. },
  713. {
  714. .chipid_cidr = 0x299B0A60,
  715. .name = "at91sam3sd8b",
  716. .total_flash_size = 512 * 1024,
  717. .total_sram_size = 64 * 1024,
  718. .n_gpnvms = 3,
  719. .n_banks = 2,
  720. {
  721. /* .bank[0] = { */
  722. {
  723. .probed = false,
  724. .chip = NULL,
  725. .bank = NULL,
  726. .bank_number = 0,
  727. .base_address = FLASH_BANK0_BASE_SD,
  728. .controller_address = 0x400e0a00,
  729. .flash_wait_states = 6, /* workaround silicon bug */
  730. .present = 1,
  731. .size_bytes = 256 * 1024,
  732. .nsectors = 16,
  733. .sector_size = 32768,
  734. .page_size = 256,
  735. },
  736. /* .bank[1] = { */
  737. {
  738. .probed = false,
  739. .chip = NULL,
  740. .bank = NULL,
  741. .bank_number = 1,
  742. .base_address = FLASH_BANK1_BASE_512K_SD,
  743. .controller_address = 0x400e0a00,
  744. .flash_wait_states = 6, /* workaround silicon bug */
  745. .present = 1,
  746. .size_bytes = 256 * 1024,
  747. .nsectors = 16,
  748. .sector_size = 32768,
  749. .page_size = 256,
  750. },
  751. },
  752. },
  753. {
  754. .chipid_cidr = 0x29ab0a60,
  755. .name = "at91sam3sd8c",
  756. .total_flash_size = 512 * 1024,
  757. .total_sram_size = 64 * 1024,
  758. .n_gpnvms = 3,
  759. .n_banks = 2,
  760. {
  761. /* .bank[0] = { */
  762. {
  763. .probed = false,
  764. .chip = NULL,
  765. .bank = NULL,
  766. .bank_number = 0,
  767. .base_address = FLASH_BANK0_BASE_SD,
  768. .controller_address = 0x400e0a00,
  769. .flash_wait_states = 6, /* workaround silicon bug */
  770. .present = 1,
  771. .size_bytes = 256 * 1024,
  772. .nsectors = 16,
  773. .sector_size = 32768,
  774. .page_size = 256,
  775. },
  776. /* .bank[1] = { */
  777. {
  778. .probed = false,
  779. .chip = NULL,
  780. .bank = NULL,
  781. .bank_number = 1,
  782. .base_address = FLASH_BANK1_BASE_512K_SD,
  783. .controller_address = 0x400e0a00,
  784. .flash_wait_states = 6, /* workaround silicon bug */
  785. .present = 1,
  786. .size_bytes = 256 * 1024,
  787. .nsectors = 16,
  788. .sector_size = 32768,
  789. .page_size = 256,
  790. },
  791. },
  792. },
  793. {
  794. .chipid_cidr = 0x288A0760,
  795. .name = "at91sam3s2a",
  796. .total_flash_size = 128 * 1024,
  797. .total_sram_size = 32 * 1024,
  798. .n_gpnvms = 2,
  799. .n_banks = 1,
  800. {
  801. /* .bank[0] = { */
  802. {
  803. .probed = false,
  804. .chip = NULL,
  805. .bank = NULL,
  806. .bank_number = 0,
  807. .base_address = FLASH_BANK_BASE_S,
  808. .controller_address = 0x400e0a00,
  809. .flash_wait_states = 6, /* workaround silicon bug */
  810. .present = 1,
  811. .size_bytes = 128 * 1024,
  812. .nsectors = 8,
  813. .sector_size = 16384,
  814. .page_size = 256,
  815. },
  816. /* .bank[1] = { */
  817. {
  818. .present = 0,
  819. .probed = false,
  820. .bank_number = 1,
  821. },
  822. },
  823. },
  824. {
  825. .chipid_cidr = 0x28A90560,
  826. .name = "at91sam3s1c",
  827. .total_flash_size = 64 * 1024,
  828. .total_sram_size = 16 * 1024,
  829. .n_gpnvms = 2,
  830. .n_banks = 1,
  831. {
  832. /* .bank[0] = { */
  833. {
  834. .probed = false,
  835. .chip = NULL,
  836. .bank = NULL,
  837. .bank_number = 0,
  838. .base_address = FLASH_BANK_BASE_S,
  839. .controller_address = 0x400e0a00,
  840. .flash_wait_states = 6, /* workaround silicon bug */
  841. .present = 1,
  842. .size_bytes = 64 * 1024,
  843. .nsectors = 4,
  844. .sector_size = 16384,
  845. .page_size = 256,
  846. },
  847. /* .bank[1] = { */
  848. {
  849. .present = 0,
  850. .probed = false,
  851. .bank_number = 1,
  852. },
  853. },
  854. },
  855. {
  856. .chipid_cidr = 0x28990560,
  857. .name = "at91sam3s1b",
  858. .total_flash_size = 64 * 1024,
  859. .total_sram_size = 16 * 1024,
  860. .n_gpnvms = 2,
  861. .n_banks = 1,
  862. {
  863. /* .bank[0] = { */
  864. {
  865. .probed = false,
  866. .chip = NULL,
  867. .bank = NULL,
  868. .bank_number = 0,
  869. .base_address = FLASH_BANK_BASE_S,
  870. .controller_address = 0x400e0a00,
  871. .flash_wait_states = 6, /* workaround silicon bug */
  872. .present = 1,
  873. .size_bytes = 64 * 1024,
  874. .nsectors = 4,
  875. .sector_size = 16384,
  876. .page_size = 256,
  877. },
  878. /* .bank[1] = { */
  879. {
  880. .present = 0,
  881. .probed = false,
  882. .bank_number = 1,
  883. },
  884. },
  885. },
  886. {
  887. .chipid_cidr = 0x28890560,
  888. .name = "at91sam3s1a",
  889. .total_flash_size = 64 * 1024,
  890. .total_sram_size = 16 * 1024,
  891. .n_gpnvms = 2,
  892. .n_banks = 1,
  893. {
  894. /* .bank[0] = { */
  895. {
  896. .probed = false,
  897. .chip = NULL,
  898. .bank = NULL,
  899. .bank_number = 0,
  900. .base_address = FLASH_BANK_BASE_S,
  901. .controller_address = 0x400e0a00,
  902. .flash_wait_states = 6, /* workaround silicon bug */
  903. .present = 1,
  904. .size_bytes = 64 * 1024,
  905. .nsectors = 4,
  906. .sector_size = 16384,
  907. .page_size = 256,
  908. },
  909. /* .bank[1] = { */
  910. {
  911. .present = 0,
  912. .probed = false,
  913. .bank_number = 1,
  914. },
  915. },
  916. },
  917. {
  918. .chipid_cidr = 0x288B0A60,
  919. .name = "at91sam3s8a",
  920. .total_flash_size = 256 * 2048,
  921. .total_sram_size = 64 * 1024,
  922. .n_gpnvms = 2,
  923. .n_banks = 1,
  924. {
  925. /* .bank[0] = { */
  926. {
  927. .probed = false,
  928. .chip = NULL,
  929. .bank = NULL,
  930. .bank_number = 0,
  931. .base_address = FLASH_BANK_BASE_S,
  932. .controller_address = 0x400e0a00,
  933. .flash_wait_states = 6, /* workaround silicon bug */
  934. .present = 1,
  935. .size_bytes = 256 * 2048,
  936. .nsectors = 16,
  937. .sector_size = 32768,
  938. .page_size = 256,
  939. },
  940. /* .bank[1] = { */
  941. {
  942. .present = 0,
  943. .probed = false,
  944. .bank_number = 1,
  945. },
  946. },
  947. },
  948. {
  949. .chipid_cidr = 0x289B0A60,
  950. .name = "at91sam3s8b",
  951. .total_flash_size = 256 * 2048,
  952. .total_sram_size = 64 * 1024,
  953. .n_gpnvms = 2,
  954. .n_banks = 1,
  955. {
  956. /* .bank[0] = { */
  957. {
  958. .probed = false,
  959. .chip = NULL,
  960. .bank = NULL,
  961. .bank_number = 0,
  962. .base_address = FLASH_BANK_BASE_S,
  963. .controller_address = 0x400e0a00,
  964. .flash_wait_states = 6, /* workaround silicon bug */
  965. .present = 1,
  966. .size_bytes = 256 * 2048,
  967. .nsectors = 16,
  968. .sector_size = 32768,
  969. .page_size = 256,
  970. },
  971. /* .bank[1] = { */
  972. {
  973. .present = 0,
  974. .probed = false,
  975. .bank_number = 1,
  976. },
  977. },
  978. },
  979. {
  980. .chipid_cidr = 0x28AB0A60,
  981. .name = "at91sam3s8c",
  982. .total_flash_size = 256 * 2048,
  983. .total_sram_size = 64 * 1024,
  984. .n_gpnvms = 2,
  985. .n_banks = 1,
  986. {
  987. /* .bank[0] = { */
  988. {
  989. .probed = false,
  990. .chip = NULL,
  991. .bank = NULL,
  992. .bank_number = 0,
  993. .base_address = FLASH_BANK_BASE_S,
  994. .controller_address = 0x400e0a00,
  995. .flash_wait_states = 6, /* workaround silicon bug */
  996. .present = 1,
  997. .size_bytes = 256 * 2048,
  998. .nsectors = 16,
  999. .sector_size = 32768,
  1000. .page_size = 256,
  1001. },
  1002. /* .bank[1] = { */
  1003. {
  1004. .present = 0,
  1005. .probed = false,
  1006. .bank_number = 1,
  1007. },
  1008. },
  1009. },
  1010. /* Start at91sam3n* series */
  1011. {
  1012. .chipid_cidr = 0x29540960,
  1013. .name = "at91sam3n4c",
  1014. .total_flash_size = 256 * 1024,
  1015. .total_sram_size = 24 * 1024,
  1016. .n_gpnvms = 3,
  1017. .n_banks = 1,
  1018. /* System boots at address 0x0 */
  1019. /* gpnvm[1] = selects boot code */
  1020. /* if gpnvm[1] == 0 */
  1021. /* boot is via "SAMBA" (rom) */
  1022. /* else */
  1023. /* boot is via FLASH */
  1024. /* Selection is via gpnvm[2] */
  1025. /* endif */
  1026. /* */
  1027. /* NOTE: banks 0 & 1 switch places */
  1028. /* if gpnvm[2] == 0 */
  1029. /* Bank0 is the boot rom */
  1030. /* else */
  1031. /* Bank1 is the boot rom */
  1032. /* endif */
  1033. /* .bank[0] = { */
  1034. {
  1035. {
  1036. .probed = false,
  1037. .chip = NULL,
  1038. .bank = NULL,
  1039. .bank_number = 0,
  1040. .base_address = FLASH_BANK_BASE_N,
  1041. .controller_address = 0x400e0A00,
  1042. .flash_wait_states = 6, /* workaround silicon bug */
  1043. .present = 1,
  1044. .size_bytes = 256 * 1024,
  1045. .nsectors = 16,
  1046. .sector_size = 16384,
  1047. .page_size = 256,
  1048. },
  1049. /* .bank[1] = { */
  1050. {
  1051. .present = 0,
  1052. .probed = false,
  1053. .bank_number = 1,
  1054. },
  1055. },
  1056. },
  1057. {
  1058. .chipid_cidr = 0x29440960,
  1059. .name = "at91sam3n4b",
  1060. .total_flash_size = 256 * 1024,
  1061. .total_sram_size = 24 * 1024,
  1062. .n_gpnvms = 3,
  1063. .n_banks = 1,
  1064. /* System boots at address 0x0 */
  1065. /* gpnvm[1] = selects boot code */
  1066. /* if gpnvm[1] == 0 */
  1067. /* boot is via "SAMBA" (rom) */
  1068. /* else */
  1069. /* boot is via FLASH */
  1070. /* Selection is via gpnvm[2] */
  1071. /* endif */
  1072. /* */
  1073. /* NOTE: banks 0 & 1 switch places */
  1074. /* if gpnvm[2] == 0 */
  1075. /* Bank0 is the boot rom */
  1076. /* else */
  1077. /* Bank1 is the boot rom */
  1078. /* endif */
  1079. /* .bank[0] = { */
  1080. {
  1081. {
  1082. .probed = false,
  1083. .chip = NULL,
  1084. .bank = NULL,
  1085. .bank_number = 0,
  1086. .base_address = FLASH_BANK_BASE_N,
  1087. .controller_address = 0x400e0A00,
  1088. .flash_wait_states = 6, /* workaround silicon bug */
  1089. .present = 1,
  1090. .size_bytes = 256 * 1024,
  1091. .nsectors = 16,
  1092. .sector_size = 16384,
  1093. .page_size = 256,
  1094. },
  1095. /* .bank[1] = { */
  1096. {
  1097. .present = 0,
  1098. .probed = false,
  1099. .bank_number = 1,
  1100. },
  1101. },
  1102. },
  1103. {
  1104. .chipid_cidr = 0x29340960,
  1105. .name = "at91sam3n4a",
  1106. .total_flash_size = 256 * 1024,
  1107. .total_sram_size = 24 * 1024,
  1108. .n_gpnvms = 3,
  1109. .n_banks = 1,
  1110. /* System boots at address 0x0 */
  1111. /* gpnvm[1] = selects boot code */
  1112. /* if gpnvm[1] == 0 */
  1113. /* boot is via "SAMBA" (rom) */
  1114. /* else */
  1115. /* boot is via FLASH */
  1116. /* Selection is via gpnvm[2] */
  1117. /* endif */
  1118. /* */
  1119. /* NOTE: banks 0 & 1 switch places */
  1120. /* if gpnvm[2] == 0 */
  1121. /* Bank0 is the boot rom */
  1122. /* else */
  1123. /* Bank1 is the boot rom */
  1124. /* endif */
  1125. /* .bank[0] = { */
  1126. {
  1127. {
  1128. .probed = false,
  1129. .chip = NULL,
  1130. .bank = NULL,
  1131. .bank_number = 0,
  1132. .base_address = FLASH_BANK_BASE_N,
  1133. .controller_address = 0x400e0A00,
  1134. .flash_wait_states = 6, /* workaround silicon bug */
  1135. .present = 1,
  1136. .size_bytes = 256 * 1024,
  1137. .nsectors = 16,
  1138. .sector_size = 16384,
  1139. .page_size = 256,
  1140. },
  1141. /* .bank[1] = { */
  1142. {
  1143. .present = 0,
  1144. .probed = false,
  1145. .bank_number = 1,
  1146. },
  1147. },
  1148. },
  1149. {
  1150. .chipid_cidr = 0x29590760,
  1151. .name = "at91sam3n2c",
  1152. .total_flash_size = 128 * 1024,
  1153. .total_sram_size = 16 * 1024,
  1154. .n_gpnvms = 3,
  1155. .n_banks = 1,
  1156. /* System boots at address 0x0 */
  1157. /* gpnvm[1] = selects boot code */
  1158. /* if gpnvm[1] == 0 */
  1159. /* boot is via "SAMBA" (rom) */
  1160. /* else */
  1161. /* boot is via FLASH */
  1162. /* Selection is via gpnvm[2] */
  1163. /* endif */
  1164. /* */
  1165. /* NOTE: banks 0 & 1 switch places */
  1166. /* if gpnvm[2] == 0 */
  1167. /* Bank0 is the boot rom */
  1168. /* else */
  1169. /* Bank1 is the boot rom */
  1170. /* endif */
  1171. /* .bank[0] = { */
  1172. {
  1173. {
  1174. .probed = false,
  1175. .chip = NULL,
  1176. .bank = NULL,
  1177. .bank_number = 0,
  1178. .base_address = FLASH_BANK_BASE_N,
  1179. .controller_address = 0x400e0A00,
  1180. .flash_wait_states = 6, /* workaround silicon bug */
  1181. .present = 1,
  1182. .size_bytes = 128 * 1024,
  1183. .nsectors = 8,
  1184. .sector_size = 16384,
  1185. .page_size = 256,
  1186. },
  1187. /* .bank[1] = { */
  1188. {
  1189. .present = 0,
  1190. .probed = false,
  1191. .bank_number = 1,
  1192. },
  1193. },
  1194. },
  1195. {
  1196. .chipid_cidr = 0x29490760,
  1197. .name = "at91sam3n2b",
  1198. .total_flash_size = 128 * 1024,
  1199. .total_sram_size = 16 * 1024,
  1200. .n_gpnvms = 3,
  1201. .n_banks = 1,
  1202. /* System boots at address 0x0 */
  1203. /* gpnvm[1] = selects boot code */
  1204. /* if gpnvm[1] == 0 */
  1205. /* boot is via "SAMBA" (rom) */
  1206. /* else */
  1207. /* boot is via FLASH */
  1208. /* Selection is via gpnvm[2] */
  1209. /* endif */
  1210. /* */
  1211. /* NOTE: banks 0 & 1 switch places */
  1212. /* if gpnvm[2] == 0 */
  1213. /* Bank0 is the boot rom */
  1214. /* else */
  1215. /* Bank1 is the boot rom */
  1216. /* endif */
  1217. /* .bank[0] = { */
  1218. {
  1219. {
  1220. .probed = false,
  1221. .chip = NULL,
  1222. .bank = NULL,
  1223. .bank_number = 0,
  1224. .base_address = FLASH_BANK_BASE_N,
  1225. .controller_address = 0x400e0A00,
  1226. .flash_wait_states = 6, /* workaround silicon bug */
  1227. .present = 1,
  1228. .size_bytes = 128 * 1024,
  1229. .nsectors = 8,
  1230. .sector_size = 16384,
  1231. .page_size = 256,
  1232. },
  1233. /* .bank[1] = { */
  1234. {
  1235. .present = 0,
  1236. .probed = false,
  1237. .bank_number = 1,
  1238. },
  1239. },
  1240. },
  1241. {
  1242. .chipid_cidr = 0x29390760,
  1243. .name = "at91sam3n2a",
  1244. .total_flash_size = 128 * 1024,
  1245. .total_sram_size = 16 * 1024,
  1246. .n_gpnvms = 3,
  1247. .n_banks = 1,
  1248. /* System boots at address 0x0 */
  1249. /* gpnvm[1] = selects boot code */
  1250. /* if gpnvm[1] == 0 */
  1251. /* boot is via "SAMBA" (rom) */
  1252. /* else */
  1253. /* boot is via FLASH */
  1254. /* Selection is via gpnvm[2] */
  1255. /* endif */
  1256. /* */
  1257. /* NOTE: banks 0 & 1 switch places */
  1258. /* if gpnvm[2] == 0 */
  1259. /* Bank0 is the boot rom */
  1260. /* else */
  1261. /* Bank1 is the boot rom */
  1262. /* endif */
  1263. /* .bank[0] = { */
  1264. {
  1265. {
  1266. .probed = false,
  1267. .chip = NULL,
  1268. .bank = NULL,
  1269. .bank_number = 0,
  1270. .base_address = FLASH_BANK_BASE_N,
  1271. .controller_address = 0x400e0A00,
  1272. .flash_wait_states = 6, /* workaround silicon bug */
  1273. .present = 1,
  1274. .size_bytes = 128 * 1024,
  1275. .nsectors = 8,
  1276. .sector_size = 16384,
  1277. .page_size = 256,
  1278. },
  1279. /* .bank[1] = { */
  1280. {
  1281. .present = 0,
  1282. .probed = false,
  1283. .bank_number = 1,
  1284. },
  1285. },
  1286. },
  1287. {
  1288. .chipid_cidr = 0x29580560,
  1289. .name = "at91sam3n1c",
  1290. .total_flash_size = 64 * 1024,
  1291. .total_sram_size = 8 * 1024,
  1292. .n_gpnvms = 3,
  1293. .n_banks = 1,
  1294. /* System boots at address 0x0 */
  1295. /* gpnvm[1] = selects boot code */
  1296. /* if gpnvm[1] == 0 */
  1297. /* boot is via "SAMBA" (rom) */
  1298. /* else */
  1299. /* boot is via FLASH */
  1300. /* Selection is via gpnvm[2] */
  1301. /* endif */
  1302. /* */
  1303. /* NOTE: banks 0 & 1 switch places */
  1304. /* if gpnvm[2] == 0 */
  1305. /* Bank0 is the boot rom */
  1306. /* else */
  1307. /* Bank1 is the boot rom */
  1308. /* endif */
  1309. /* .bank[0] = { */
  1310. {
  1311. {
  1312. .probed = false,
  1313. .chip = NULL,
  1314. .bank = NULL,
  1315. .bank_number = 0,
  1316. .base_address = FLASH_BANK_BASE_N,
  1317. .controller_address = 0x400e0A00,
  1318. .flash_wait_states = 6, /* workaround silicon bug */
  1319. .present = 1,
  1320. .size_bytes = 64 * 1024,
  1321. .nsectors = 4,
  1322. .sector_size = 16384,
  1323. .page_size = 256,
  1324. },
  1325. /* .bank[1] = { */
  1326. {
  1327. .present = 0,
  1328. .probed = false,
  1329. .bank_number = 1,
  1330. },
  1331. },
  1332. },
  1333. {
  1334. .chipid_cidr = 0x29480560,
  1335. .name = "at91sam3n1b",
  1336. .total_flash_size = 64 * 1024,
  1337. .total_sram_size = 8 * 1024,
  1338. .n_gpnvms = 3,
  1339. .n_banks = 1,
  1340. /* System boots at address 0x0 */
  1341. /* gpnvm[1] = selects boot code */
  1342. /* if gpnvm[1] == 0 */
  1343. /* boot is via "SAMBA" (rom) */
  1344. /* else */
  1345. /* boot is via FLASH */
  1346. /* Selection is via gpnvm[2] */
  1347. /* endif */
  1348. /* */
  1349. /* NOTE: banks 0 & 1 switch places */
  1350. /* if gpnvm[2] == 0 */
  1351. /* Bank0 is the boot rom */
  1352. /* else */
  1353. /* Bank1 is the boot rom */
  1354. /* endif */
  1355. /* .bank[0] = { */
  1356. {
  1357. {
  1358. .probed = false,
  1359. .chip = NULL,
  1360. .bank = NULL,
  1361. .bank_number = 0,
  1362. .base_address = FLASH_BANK_BASE_N,
  1363. .controller_address = 0x400e0A00,
  1364. .flash_wait_states = 6, /* workaround silicon bug */
  1365. .present = 1,
  1366. .size_bytes = 64 * 1024,
  1367. .nsectors = 4,
  1368. .sector_size = 16384,
  1369. .page_size = 256,
  1370. },
  1371. /* .bank[1] = { */
  1372. {
  1373. .present = 0,
  1374. .probed = false,
  1375. .bank_number = 1,
  1376. },
  1377. },
  1378. },
  1379. {
  1380. .chipid_cidr = 0x29380560,
  1381. .name = "at91sam3n1a",
  1382. .total_flash_size = 64 * 1024,
  1383. .total_sram_size = 8 * 1024,
  1384. .n_gpnvms = 3,
  1385. .n_banks = 1,
  1386. /* System boots at address 0x0 */
  1387. /* gpnvm[1] = selects boot code */
  1388. /* if gpnvm[1] == 0 */
  1389. /* boot is via "SAMBA" (rom) */
  1390. /* else */
  1391. /* boot is via FLASH */
  1392. /* Selection is via gpnvm[2] */
  1393. /* endif */
  1394. /* */
  1395. /* NOTE: banks 0 & 1 switch places */
  1396. /* if gpnvm[2] == 0 */
  1397. /* Bank0 is the boot rom */
  1398. /* else */
  1399. /* Bank1 is the boot rom */
  1400. /* endif */
  1401. /* .bank[0] = { */
  1402. {
  1403. {
  1404. .probed = false,
  1405. .chip = NULL,
  1406. .bank = NULL,
  1407. .bank_number = 0,
  1408. .base_address = FLASH_BANK_BASE_N,
  1409. .controller_address = 0x400e0A00,
  1410. .flash_wait_states = 6, /* workaround silicon bug */
  1411. .present = 1,
  1412. .size_bytes = 64 * 1024,
  1413. .nsectors = 4,
  1414. .sector_size = 16384,
  1415. .page_size = 256,
  1416. },
  1417. /* .bank[1] = { */
  1418. {
  1419. .present = 0,
  1420. .probed = false,
  1421. .bank_number = 1,
  1422. },
  1423. },
  1424. },
  1425. {
  1426. .chipid_cidr = 0x29480360,
  1427. .name = "at91sam3n0b",
  1428. .total_flash_size = 32 * 1024,
  1429. .total_sram_size = 8 * 1024,
  1430. .n_gpnvms = 3,
  1431. .n_banks = 1,
  1432. /* .bank[0] = { */
  1433. {
  1434. {
  1435. .probed = false,
  1436. .chip = NULL,
  1437. .bank = NULL,
  1438. .bank_number = 0,
  1439. .base_address = FLASH_BANK_BASE_N,
  1440. .controller_address = 0x400e0A00,
  1441. .flash_wait_states = 6, /* workaround silicon bug */
  1442. .present = 1,
  1443. .size_bytes = 32 * 1024,
  1444. .nsectors = 2,
  1445. .sector_size = 16384,
  1446. .page_size = 256,
  1447. },
  1448. /* .bank[1] = { */
  1449. {
  1450. .present = 0,
  1451. .probed = false,
  1452. .bank_number = 1,
  1453. },
  1454. },
  1455. },
  1456. {
  1457. .chipid_cidr = 0x29380360,
  1458. .name = "at91sam3n0a",
  1459. .total_flash_size = 32 * 1024,
  1460. .total_sram_size = 8 * 1024,
  1461. .n_gpnvms = 3,
  1462. .n_banks = 1,
  1463. /* .bank[0] = { */
  1464. {
  1465. {
  1466. .probed = false,
  1467. .chip = NULL,
  1468. .bank = NULL,
  1469. .bank_number = 0,
  1470. .base_address = FLASH_BANK_BASE_N,
  1471. .controller_address = 0x400e0A00,
  1472. .flash_wait_states = 6, /* workaround silicon bug */
  1473. .present = 1,
  1474. .size_bytes = 32 * 1024,
  1475. .nsectors = 2,
  1476. .sector_size = 16384,
  1477. .page_size = 256,
  1478. },
  1479. /* .bank[1] = { */
  1480. {
  1481. .present = 0,
  1482. .probed = false,
  1483. .bank_number = 1,
  1484. },
  1485. },
  1486. },
  1487. {
  1488. .chipid_cidr = 0x29450260,
  1489. .name = "at91sam3n00b",
  1490. .total_flash_size = 16 * 1024,
  1491. .total_sram_size = 4 * 1024,
  1492. .n_gpnvms = 3,
  1493. .n_banks = 1,
  1494. /* .bank[0] = { */
  1495. {
  1496. {
  1497. .probed = false,
  1498. .chip = NULL,
  1499. .bank = NULL,
  1500. .bank_number = 0,
  1501. .base_address = FLASH_BANK_BASE_N,
  1502. .controller_address = 0x400e0A00,
  1503. .flash_wait_states = 6, /* workaround silicon bug */
  1504. .present = 1,
  1505. .size_bytes = 16 * 1024,
  1506. .nsectors = 1,
  1507. .sector_size = 16384,
  1508. .page_size = 256,
  1509. },
  1510. /* .bank[1] = { */
  1511. {
  1512. .present = 0,
  1513. .probed = false,
  1514. .bank_number = 1,
  1515. },
  1516. },
  1517. },
  1518. {
  1519. .chipid_cidr = 0x29350260,
  1520. .name = "at91sam3n00a",
  1521. .total_flash_size = 16 * 1024,
  1522. .total_sram_size = 4 * 1024,
  1523. .n_gpnvms = 3,
  1524. .n_banks = 1,
  1525. /* .bank[0] = { */
  1526. {
  1527. {
  1528. .probed = false,
  1529. .chip = NULL,
  1530. .bank = NULL,
  1531. .bank_number = 0,
  1532. .base_address = FLASH_BANK_BASE_N,
  1533. .controller_address = 0x400e0A00,
  1534. .flash_wait_states = 6, /* workaround silicon bug */
  1535. .present = 1,
  1536. .size_bytes = 16 * 1024,
  1537. .nsectors = 1,
  1538. .sector_size = 16384,
  1539. .page_size = 256,
  1540. },
  1541. /* .bank[1] = { */
  1542. {
  1543. .present = 0,
  1544. .probed = false,
  1545. .bank_number = 1,
  1546. },
  1547. },
  1548. },
  1549. /* Start at91sam3a series*/
  1550. /* System boots at address 0x0 */
  1551. /* gpnvm[1] = selects boot code */
  1552. /* if gpnvm[1] == 0 */
  1553. /* boot is via "SAMBA" (rom) */
  1554. /* else */
  1555. /* boot is via FLASH */
  1556. /* Selection is via gpnvm[2] */
  1557. /* endif */
  1558. /* */
  1559. /* NOTE: banks 0 & 1 switch places */
  1560. /* if gpnvm[2] == 0 */
  1561. /* Bank0 is the boot rom */
  1562. /* else */
  1563. /* Bank1 is the boot rom */
  1564. /* endif */
  1565. {
  1566. .chipid_cidr = 0x283E0A60,
  1567. .name = "at91sam3a8c",
  1568. .total_flash_size = 512 * 1024,
  1569. .total_sram_size = 96 * 1024,
  1570. .n_gpnvms = 3,
  1571. .n_banks = 2,
  1572. {
  1573. /* .bank[0] = { */
  1574. {
  1575. .probed = false,
  1576. .chip = NULL,
  1577. .bank = NULL,
  1578. .bank_number = 0,
  1579. .base_address = FLASH_BANK0_BASE_AX,
  1580. .controller_address = 0x400e0a00,
  1581. .flash_wait_states = 6, /* workaround silicon bug */
  1582. .present = 1,
  1583. .size_bytes = 256 * 1024,
  1584. .nsectors = 16,
  1585. .sector_size = 16384,
  1586. .page_size = 256,
  1587. },
  1588. /* .bank[1] = { */
  1589. {
  1590. .probed = false,
  1591. .chip = NULL,
  1592. .bank = NULL,
  1593. .bank_number = 1,
  1594. .base_address = FLASH_BANK1_BASE_512K_AX,
  1595. .controller_address = 0x400e0c00,
  1596. .flash_wait_states = 6, /* workaround silicon bug */
  1597. .present = 1,
  1598. .size_bytes = 256 * 1024,
  1599. .nsectors = 16,
  1600. .sector_size = 16384,
  1601. .page_size = 256,
  1602. },
  1603. },
  1604. },
  1605. {
  1606. .chipid_cidr = 0x283B0960,
  1607. .name = "at91sam3a4c",
  1608. .total_flash_size = 256 * 1024,
  1609. .total_sram_size = 64 * 1024,
  1610. .n_gpnvms = 3,
  1611. .n_banks = 2,
  1612. {
  1613. /* .bank[0] = { */
  1614. {
  1615. .probed = false,
  1616. .chip = NULL,
  1617. .bank = NULL,
  1618. .bank_number = 0,
  1619. .base_address = FLASH_BANK0_BASE_AX,
  1620. .controller_address = 0x400e0a00,
  1621. .flash_wait_states = 6, /* workaround silicon bug */
  1622. .present = 1,
  1623. .size_bytes = 128 * 1024,
  1624. .nsectors = 8,
  1625. .sector_size = 16384,
  1626. .page_size = 256,
  1627. },
  1628. /* .bank[1] = { */
  1629. {
  1630. .probed = false,
  1631. .chip = NULL,
  1632. .bank = NULL,
  1633. .bank_number = 1,
  1634. .base_address = FLASH_BANK1_BASE_256K_AX,
  1635. .controller_address = 0x400e0c00,
  1636. .flash_wait_states = 6, /* workaround silicon bug */
  1637. .present = 1,
  1638. .size_bytes = 128 * 1024,
  1639. .nsectors = 8,
  1640. .sector_size = 16384,
  1641. .page_size = 256,
  1642. },
  1643. },
  1644. },
  1645. /* Start at91sam3x* series */
  1646. /* System boots at address 0x0 */
  1647. /* gpnvm[1] = selects boot code */
  1648. /* if gpnvm[1] == 0 */
  1649. /* boot is via "SAMBA" (rom) */
  1650. /* else */
  1651. /* boot is via FLASH */
  1652. /* Selection is via gpnvm[2] */
  1653. /* endif */
  1654. /* */
  1655. /* NOTE: banks 0 & 1 switch places */
  1656. /* if gpnvm[2] == 0 */
  1657. /* Bank0 is the boot rom */
  1658. /* else */
  1659. /* Bank1 is the boot rom */
  1660. /* endif */
  1661. /*at91sam3x8h - ES has an incorrect CIDR of 0x286E0A20*/
  1662. {
  1663. .chipid_cidr = 0x286E0A20,
  1664. .name = "at91sam3x8h - ES",
  1665. .total_flash_size = 512 * 1024,
  1666. .total_sram_size = 96 * 1024,
  1667. .n_gpnvms = 3,
  1668. .n_banks = 2,
  1669. {
  1670. /* .bank[0] = { */
  1671. {
  1672. .probed = false,
  1673. .chip = NULL,
  1674. .bank = NULL,
  1675. .bank_number = 0,
  1676. .base_address = FLASH_BANK0_BASE_AX,
  1677. .controller_address = 0x400e0a00,
  1678. .flash_wait_states = 6, /* workaround silicon bug */
  1679. .present = 1,
  1680. .size_bytes = 256 * 1024,
  1681. .nsectors = 16,
  1682. .sector_size = 16384,
  1683. .page_size = 256,
  1684. },
  1685. /* .bank[1] = { */
  1686. {
  1687. .probed = false,
  1688. .chip = NULL,
  1689. .bank = NULL,
  1690. .bank_number = 1,
  1691. .base_address = FLASH_BANK1_BASE_512K_AX,
  1692. .controller_address = 0x400e0c00,
  1693. .flash_wait_states = 6, /* workaround silicon bug */
  1694. .present = 1,
  1695. .size_bytes = 256 * 1024,
  1696. .nsectors = 16,
  1697. .sector_size = 16384,
  1698. .page_size = 256,
  1699. },
  1700. },
  1701. },
  1702. /*at91sam3x8h - ES2 and up uses the correct CIDR of 0x286E0A60*/
  1703. {
  1704. .chipid_cidr = 0x286E0A60,
  1705. .name = "at91sam3x8h",
  1706. .total_flash_size = 512 * 1024,
  1707. .total_sram_size = 96 * 1024,
  1708. .n_gpnvms = 3,
  1709. .n_banks = 2,
  1710. {
  1711. /* .bank[0] = { */
  1712. {
  1713. .probed = false,
  1714. .chip = NULL,
  1715. .bank = NULL,
  1716. .bank_number = 0,
  1717. .base_address = FLASH_BANK0_BASE_AX,
  1718. .controller_address = 0x400e0a00,
  1719. .flash_wait_states = 6, /* workaround silicon bug */
  1720. .present = 1,
  1721. .size_bytes = 256 * 1024,
  1722. .nsectors = 16,
  1723. .sector_size = 16384,
  1724. .page_size = 256,
  1725. },
  1726. /* .bank[1] = { */
  1727. {
  1728. .probed = false,
  1729. .chip = NULL,
  1730. .bank = NULL,
  1731. .bank_number = 1,
  1732. .base_address = FLASH_BANK1_BASE_512K_AX,
  1733. .controller_address = 0x400e0c00,
  1734. .flash_wait_states = 6, /* workaround silicon bug */
  1735. .present = 1,
  1736. .size_bytes = 256 * 1024,
  1737. .nsectors = 16,
  1738. .sector_size = 16384,
  1739. .page_size = 256,
  1740. },
  1741. },
  1742. },
  1743. {
  1744. .chipid_cidr = 0x285E0A60,
  1745. .name = "at91sam3x8e",
  1746. .total_flash_size = 512 * 1024,
  1747. .total_sram_size = 96 * 1024,
  1748. .n_gpnvms = 3,
  1749. .n_banks = 2,
  1750. {
  1751. /* .bank[0] = { */
  1752. {
  1753. .probed = false,
  1754. .chip = NULL,
  1755. .bank = NULL,
  1756. .bank_number = 0,
  1757. .base_address = FLASH_BANK0_BASE_AX,
  1758. .controller_address = 0x400e0a00,
  1759. .flash_wait_states = 6, /* workaround silicon bug */
  1760. .present = 1,
  1761. .size_bytes = 256 * 1024,
  1762. .nsectors = 16,
  1763. .sector_size = 16384,
  1764. .page_size = 256,
  1765. },
  1766. /* .bank[1] = { */
  1767. {
  1768. .probed = false,
  1769. .chip = NULL,
  1770. .bank = NULL,
  1771. .bank_number = 1,
  1772. .base_address = FLASH_BANK1_BASE_512K_AX,
  1773. .controller_address = 0x400e0c00,
  1774. .flash_wait_states = 6, /* workaround silicon bug */
  1775. .present = 1,
  1776. .size_bytes = 256 * 1024,
  1777. .nsectors = 16,
  1778. .sector_size = 16384,
  1779. .page_size = 256,
  1780. },
  1781. },
  1782. },
  1783. {
  1784. .chipid_cidr = 0x284E0A60,
  1785. .name = "at91sam3x8c",
  1786. .total_flash_size = 512 * 1024,
  1787. .total_sram_size = 96 * 1024,
  1788. .n_gpnvms = 3,
  1789. .n_banks = 2,
  1790. {
  1791. /* .bank[0] = { */
  1792. {
  1793. .probed = false,
  1794. .chip = NULL,
  1795. .bank = NULL,
  1796. .bank_number = 0,
  1797. .base_address = FLASH_BANK0_BASE_AX,
  1798. .controller_address = 0x400e0a00,
  1799. .flash_wait_states = 6, /* workaround silicon bug */
  1800. .present = 1,
  1801. .size_bytes = 256 * 1024,
  1802. .nsectors = 16,
  1803. .sector_size = 16384,
  1804. .page_size = 256,
  1805. },
  1806. /* .bank[1] = { */
  1807. {
  1808. .probed = false,
  1809. .chip = NULL,
  1810. .bank = NULL,
  1811. .bank_number = 1,
  1812. .base_address = FLASH_BANK1_BASE_512K_AX,
  1813. .controller_address = 0x400e0c00,
  1814. .flash_wait_states = 6, /* workaround silicon bug */
  1815. .present = 1,
  1816. .size_bytes = 256 * 1024,
  1817. .nsectors = 16,
  1818. .sector_size = 16384,
  1819. .page_size = 256,
  1820. },
  1821. },
  1822. },
  1823. {
  1824. .chipid_cidr = 0x285B0960,
  1825. .name = "at91sam3x4e",
  1826. .total_flash_size = 256 * 1024,
  1827. .total_sram_size = 64 * 1024,
  1828. .n_gpnvms = 3,
  1829. .n_banks = 2,
  1830. {
  1831. /* .bank[0] = { */
  1832. {
  1833. .probed = false,
  1834. .chip = NULL,
  1835. .bank = NULL,
  1836. .bank_number = 0,
  1837. .base_address = FLASH_BANK0_BASE_AX,
  1838. .controller_address = 0x400e0a00,
  1839. .flash_wait_states = 6, /* workaround silicon bug */
  1840. .present = 1,
  1841. .size_bytes = 128 * 1024,
  1842. .nsectors = 8,
  1843. .sector_size = 16384,
  1844. .page_size = 256,
  1845. },
  1846. /* .bank[1] = { */
  1847. {
  1848. .probed = false,
  1849. .chip = NULL,
  1850. .bank = NULL,
  1851. .bank_number = 1,
  1852. .base_address = FLASH_BANK1_BASE_256K_AX,
  1853. .controller_address = 0x400e0c00,
  1854. .flash_wait_states = 6, /* workaround silicon bug */
  1855. .present = 1,
  1856. .size_bytes = 128 * 1024,
  1857. .nsectors = 8,
  1858. .sector_size = 16384,
  1859. .page_size = 256,
  1860. },
  1861. },
  1862. },
  1863. {
  1864. .chipid_cidr = 0x284B0960,
  1865. .name = "at91sam3x4c",
  1866. .total_flash_size = 256 * 1024,
  1867. .total_sram_size = 64 * 1024,
  1868. .n_gpnvms = 3,
  1869. .n_banks = 2,
  1870. {
  1871. /* .bank[0] = { */
  1872. {
  1873. .probed = false,
  1874. .chip = NULL,
  1875. .bank = NULL,
  1876. .bank_number = 0,
  1877. .base_address = FLASH_BANK0_BASE_AX,
  1878. .controller_address = 0x400e0a00,
  1879. .flash_wait_states = 6, /* workaround silicon bug */
  1880. .present = 1,
  1881. .size_bytes = 128 * 1024,
  1882. .nsectors = 8,
  1883. .sector_size = 16384,
  1884. .page_size = 256,
  1885. },
  1886. /* .bank[1] = { */
  1887. {
  1888. .probed = false,
  1889. .chip = NULL,
  1890. .bank = NULL,
  1891. .bank_number = 1,
  1892. .base_address = FLASH_BANK1_BASE_256K_AX,
  1893. .controller_address = 0x400e0c00,
  1894. .flash_wait_states = 6, /* workaround silicon bug */
  1895. .present = 1,
  1896. .size_bytes = 128 * 1024,
  1897. .nsectors = 8,
  1898. .sector_size = 16384,
  1899. .page_size = 256,
  1900. },
  1901. },
  1902. },
  1903. /* terminate */
  1904. {
  1905. .chipid_cidr = 0,
  1906. .name = NULL,
  1907. }
  1908. };
  1909. /* Globals above */
  1910. /***********************************************************************
  1911. **********************************************************************
  1912. **********************************************************************
  1913. **********************************************************************
  1914. **********************************************************************
  1915. **********************************************************************/
  1916. /* *ATMEL* style code - from the SAM3 driver code */
  1917. /**
  1918. * Get the current status of the EEFC and
  1919. * the value of some status bits (LOCKE, PROGE).
  1920. * @param private - info about the bank
  1921. * @param v - result goes here
  1922. */
  1923. static int efc_get_status(struct sam3_bank_private *private, uint32_t *v)
  1924. {
  1925. int r;
  1926. r = target_read_u32(private->chip->target,
  1927. private->controller_address + OFFSET_EFC_FSR,
  1928. v);
  1929. LOG_DEBUG("Status: 0x%08x (lockerror: %d, cmderror: %d, ready: %d)",
  1930. (unsigned int)(*v),
  1931. ((unsigned int)((*v >> 2) & 1)),
  1932. ((unsigned int)((*v >> 1) & 1)),
  1933. ((unsigned int)((*v >> 0) & 1)));
  1934. return r;
  1935. }
  1936. /**
  1937. * Get the result of the last executed command.
  1938. * @param private - info about the bank
  1939. * @param v - result goes here
  1940. */
  1941. static int efc_get_result(struct sam3_bank_private *private, uint32_t *v)
  1942. {
  1943. int r;
  1944. uint32_t rv;
  1945. r = target_read_u32(private->chip->target,
  1946. private->controller_address + OFFSET_EFC_FRR,
  1947. &rv);
  1948. if (v)
  1949. *v = rv;
  1950. LOG_DEBUG("Result: 0x%08x", ((unsigned int)(rv)));
  1951. return r;
  1952. }
  1953. static int efc_start_command(struct sam3_bank_private *private,
  1954. unsigned command, unsigned argument)
  1955. {
  1956. uint32_t n, v;
  1957. int r;
  1958. int retry;
  1959. retry = 0;
  1960. do_retry:
  1961. /* Check command & argument */
  1962. switch (command) {
  1963. case AT91C_EFC_FCMD_WP:
  1964. case AT91C_EFC_FCMD_WPL:
  1965. case AT91C_EFC_FCMD_EWP:
  1966. case AT91C_EFC_FCMD_EWPL:
  1967. /* case AT91C_EFC_FCMD_EPL: */
  1968. /* case AT91C_EFC_FCMD_EPA: */
  1969. case AT91C_EFC_FCMD_SLB:
  1970. case AT91C_EFC_FCMD_CLB:
  1971. n = (private->size_bytes / private->page_size);
  1972. if (argument >= n)
  1973. LOG_ERROR("*BUG*: Embedded flash has only %u pages", (unsigned)(n));
  1974. break;
  1975. case AT91C_EFC_FCMD_SFB:
  1976. case AT91C_EFC_FCMD_CFB:
  1977. if (argument >= private->chip->details.n_gpnvms) {
  1978. LOG_ERROR("*BUG*: Embedded flash has only %d GPNVMs",
  1979. private->chip->details.n_gpnvms);
  1980. }
  1981. break;
  1982. case AT91C_EFC_FCMD_GETD:
  1983. case AT91C_EFC_FCMD_EA:
  1984. case AT91C_EFC_FCMD_GLB:
  1985. case AT91C_EFC_FCMD_GFB:
  1986. case AT91C_EFC_FCMD_STUI:
  1987. case AT91C_EFC_FCMD_SPUI:
  1988. if (argument != 0)
  1989. LOG_ERROR("Argument is meaningless for cmd: %d", command);
  1990. break;
  1991. default:
  1992. LOG_ERROR("Unknown command %d", command);
  1993. break;
  1994. }
  1995. if (command == AT91C_EFC_FCMD_SPUI) {
  1996. /* this is a very special situation. */
  1997. /* Situation (1) - error/retry - see below */
  1998. /* And we are being called recursively */
  1999. /* Situation (2) - normal, finished reading unique id */
  2000. } else {
  2001. /* it should be "ready" */
  2002. efc_get_status(private, &v);
  2003. if (v & 1) {
  2004. /* then it is ready */
  2005. /* we go on */
  2006. } else {
  2007. if (retry) {
  2008. /* we have done this before */
  2009. /* the controller is not responding. */
  2010. LOG_ERROR("flash controller(%d) is not ready! Error",
  2011. private->bank_number);
  2012. return ERROR_FAIL;
  2013. } else {
  2014. retry++;
  2015. LOG_ERROR("Flash controller(%d) is not ready, attempting reset",
  2016. private->bank_number);
  2017. /* we do that by issuing the *STOP* command */
  2018. efc_start_command(private, AT91C_EFC_FCMD_SPUI, 0);
  2019. /* above is recursive, and further recursion is blocked by */
  2020. /* if (command == AT91C_EFC_FCMD_SPUI) above */
  2021. goto do_retry;
  2022. }
  2023. }
  2024. }
  2025. v = (0x5A << 24) | (argument << 8) | command;
  2026. LOG_DEBUG("Command: 0x%08x", ((unsigned int)(v)));
  2027. r = target_write_u32(private->bank->target,
  2028. private->controller_address + OFFSET_EFC_FCR, v);
  2029. if (r != ERROR_OK)
  2030. LOG_DEBUG("Error Write failed");
  2031. return r;
  2032. }
  2033. /**
  2034. * Performs the given command and wait until its completion (or an error).
  2035. * @param private - info about the bank
  2036. * @param command - Command to perform.
  2037. * @param argument - Optional command argument.
  2038. * @param status - put command status bits here
  2039. */
  2040. static int efc_perform_command(struct sam3_bank_private *private,
  2041. unsigned command,
  2042. unsigned argument,
  2043. uint32_t *status)
  2044. {
  2045. int r;
  2046. uint32_t v;
  2047. int64_t ms_now, ms_end;
  2048. /* default */
  2049. if (status)
  2050. *status = 0;
  2051. r = efc_start_command(private, command, argument);
  2052. if (r != ERROR_OK)
  2053. return r;
  2054. ms_end = 500 + timeval_ms();
  2055. do {
  2056. r = efc_get_status(private, &v);
  2057. if (r != ERROR_OK)
  2058. return r;
  2059. ms_now = timeval_ms();
  2060. if (ms_now > ms_end) {
  2061. /* error */
  2062. LOG_ERROR("Command timeout");
  2063. return ERROR_FAIL;
  2064. }
  2065. } while ((v & 1) == 0);
  2066. /* error bits.. */
  2067. if (status)
  2068. *status = (v & 0x6);
  2069. return ERROR_OK;
  2070. }
  2071. /**
  2072. * Read the unique ID.
  2073. * @param private - info about the bank
  2074. * The unique ID is stored in the 'private' structure.
  2075. */
  2076. static int flashd_read_uid(struct sam3_bank_private *private)
  2077. {
  2078. int r;
  2079. uint32_t v;
  2080. int x;
  2081. /* assume 0 */
  2082. private->chip->cfg.unique_id[0] = 0;
  2083. private->chip->cfg.unique_id[1] = 0;
  2084. private->chip->cfg.unique_id[2] = 0;
  2085. private->chip->cfg.unique_id[3] = 0;
  2086. LOG_DEBUG("Begin");
  2087. r = efc_start_command(private, AT91C_EFC_FCMD_STUI, 0);
  2088. if (r < 0)
  2089. return r;
  2090. for (x = 0; x < 4; x++) {
  2091. r = target_read_u32(private->chip->target,
  2092. private->bank->base + (x * 4),
  2093. &v);
  2094. if (r < 0)
  2095. return r;
  2096. private->chip->cfg.unique_id[x] = v;
  2097. }
  2098. r = efc_perform_command(private, AT91C_EFC_FCMD_SPUI, 0, NULL);
  2099. LOG_DEBUG("End: R=%d, id = 0x%08x, 0x%08x, 0x%08x, 0x%08x",
  2100. r,
  2101. (unsigned int)(private->chip->cfg.unique_id[0]),
  2102. (unsigned int)(private->chip->cfg.unique_id[1]),
  2103. (unsigned int)(private->chip->cfg.unique_id[2]),
  2104. (unsigned int)(private->chip->cfg.unique_id[3]));
  2105. return r;
  2106. }
  2107. /**
  2108. * Erases the entire flash.
  2109. * @param private - the info about the bank.
  2110. */
  2111. static int flashd_erase_entire_bank(struct sam3_bank_private *private)
  2112. {
  2113. LOG_DEBUG("Here");
  2114. return efc_perform_command(private, AT91C_EFC_FCMD_EA, 0, NULL);
  2115. }
  2116. /**
  2117. * Gets current GPNVM state.
  2118. * @param private - info about the bank.
  2119. * @param gpnvm - GPNVM bit index.
  2120. * @param puthere - result stored here.
  2121. */
  2122. /* ------------------------------------------------------------------------------ */
  2123. static int flashd_get_gpnvm(struct sam3_bank_private *private, unsigned gpnvm, unsigned *puthere)
  2124. {
  2125. uint32_t v;
  2126. int r;
  2127. LOG_DEBUG("Here");
  2128. if (private->bank_number != 0) {
  2129. LOG_ERROR("GPNVM only works with Bank0");
  2130. return ERROR_FAIL;
  2131. }
  2132. if (gpnvm >= private->chip->details.n_gpnvms) {
  2133. LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
  2134. gpnvm, private->chip->details.n_gpnvms);
  2135. return ERROR_FAIL;
  2136. }
  2137. /* Get GPNVMs status */
  2138. r = efc_perform_command(private, AT91C_EFC_FCMD_GFB, 0, NULL);
  2139. if (r != ERROR_OK) {
  2140. LOG_ERROR("Failed");
  2141. return r;
  2142. }
  2143. r = efc_get_result(private, &v);
  2144. if (puthere) {
  2145. /* Check if GPNVM is set */
  2146. /* get the bit and make it a 0/1 */
  2147. *puthere = (v >> gpnvm) & 1;
  2148. }
  2149. return r;
  2150. }
  2151. /**
  2152. * Clears the selected GPNVM bit.
  2153. * @param private info about the bank
  2154. * @param gpnvm GPNVM index.
  2155. * @returns 0 if successful; otherwise returns an error code.
  2156. */
  2157. static int flashd_clr_gpnvm(struct sam3_bank_private *private, unsigned gpnvm)
  2158. {
  2159. int r;
  2160. unsigned v;
  2161. LOG_DEBUG("Here");
  2162. if (private->bank_number != 0) {
  2163. LOG_ERROR("GPNVM only works with Bank0");
  2164. return ERROR_FAIL;
  2165. }
  2166. if (gpnvm >= private->chip->details.n_gpnvms) {
  2167. LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
  2168. gpnvm, private->chip->details.n_gpnvms);
  2169. return ERROR_FAIL;
  2170. }
  2171. r = flashd_get_gpnvm(private, gpnvm, &v);
  2172. if (r != ERROR_OK) {
  2173. LOG_DEBUG("Failed: %d", r);
  2174. return r;
  2175. }
  2176. r = efc_perform_command(private, AT91C_EFC_FCMD_CFB, gpnvm, NULL);
  2177. LOG_DEBUG("End: %d", r);
  2178. return r;
  2179. }
  2180. /**
  2181. * Sets the selected GPNVM bit.
  2182. * @param private info about the bank
  2183. * @param gpnvm GPNVM index.
  2184. */
  2185. static int flashd_set_gpnvm(struct sam3_bank_private *private, unsigned gpnvm)
  2186. {
  2187. int r;
  2188. unsigned v;
  2189. if (private->bank_number != 0) {
  2190. LOG_ERROR("GPNVM only works with Bank0");
  2191. return ERROR_FAIL;
  2192. }
  2193. if (gpnvm >= private->chip->details.n_gpnvms) {
  2194. LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
  2195. gpnvm, private->chip->details.n_gpnvms);
  2196. return ERROR_FAIL;
  2197. }
  2198. r = flashd_get_gpnvm(private, gpnvm, &v);
  2199. if (r != ERROR_OK)
  2200. return r;
  2201. if (v) {
  2202. /* already set */
  2203. r = ERROR_OK;
  2204. } else {
  2205. /* set it */
  2206. r = efc_perform_command(private, AT91C_EFC_FCMD_SFB, gpnvm, NULL);
  2207. }
  2208. return r;
  2209. }
  2210. /**
  2211. * Returns a bit field (at most 64) of locked regions within a page.
  2212. * @param private info about the bank
  2213. * @param v where to store locked bits
  2214. */
  2215. static int flashd_get_lock_bits(struct sam3_bank_private *private, uint32_t *v)
  2216. {
  2217. int r;
  2218. LOG_DEBUG("Here");
  2219. r = efc_perform_command(private, AT91C_EFC_FCMD_GLB, 0, NULL);
  2220. if (r == ERROR_OK)
  2221. r = efc_get_result(private, v);
  2222. LOG_DEBUG("End: %d", r);
  2223. return r;
  2224. }
  2225. /**
  2226. * Unlocks all the regions in the given address range.
  2227. * @param private info about the bank
  2228. * @param start_sector first sector to unlock
  2229. * @param end_sector last (inclusive) to unlock
  2230. */
  2231. static int flashd_unlock(struct sam3_bank_private *private,
  2232. unsigned start_sector,
  2233. unsigned end_sector)
  2234. {
  2235. int r;
  2236. uint32_t status;
  2237. uint32_t pg;
  2238. uint32_t pages_per_sector;
  2239. pages_per_sector = private->sector_size / private->page_size;
  2240. /* Unlock all pages */
  2241. while (start_sector <= end_sector) {
  2242. pg = start_sector * pages_per_sector;
  2243. r = efc_perform_command(private, AT91C_EFC_FCMD_CLB, pg, &status);
  2244. if (r != ERROR_OK)
  2245. return r;
  2246. start_sector++;
  2247. }
  2248. return ERROR_OK;
  2249. }
  2250. /**
  2251. * Locks regions
  2252. * @param private - info about the bank
  2253. * @param start_sector - first sector to lock
  2254. * @param end_sector - last sector (inclusive) to lock
  2255. */
  2256. static int flashd_lock(struct sam3_bank_private *private,
  2257. unsigned start_sector,
  2258. unsigned end_sector)
  2259. {
  2260. uint32_t status;
  2261. uint32_t pg;
  2262. uint32_t pages_per_sector;
  2263. int r;
  2264. pages_per_sector = private->sector_size / private->page_size;
  2265. /* Lock all pages */
  2266. while (start_sector <= end_sector) {
  2267. pg = start_sector * pages_per_sector;
  2268. r = efc_perform_command(private, AT91C_EFC_FCMD_SLB, pg, &status);
  2269. if (r != ERROR_OK)
  2270. return r;
  2271. start_sector++;
  2272. }
  2273. return ERROR_OK;
  2274. }
  2275. /****** END SAM3 CODE ********/
  2276. /* begin helpful debug code */
  2277. /* print the fieldname, the field value, in dec & hex, and return field value */
  2278. static uint32_t sam3_reg_fieldname(struct sam3_chip *chip,
  2279. const char *regname,
  2280. uint32_t value,
  2281. unsigned shift,
  2282. unsigned width)
  2283. {
  2284. uint32_t v;
  2285. int hwidth, dwidth;
  2286. /* extract the field */
  2287. v = value >> shift;
  2288. v = v & ((1 << width)-1);
  2289. if (width <= 16) {
  2290. hwidth = 4;
  2291. dwidth = 5;
  2292. } else {
  2293. hwidth = 8;
  2294. dwidth = 12;
  2295. }
  2296. /* show the basics */
  2297. LOG_USER_N("\t%*s: %*" PRIu32 " [0x%0*" PRIx32 "] ",
  2298. REG_NAME_WIDTH, regname,
  2299. dwidth, v,
  2300. hwidth, v);
  2301. return v;
  2302. }
  2303. static const char _unknown[] = "unknown";
  2304. static const char *const eproc_names[] = {
  2305. _unknown, /* 0 */
  2306. "arm946es", /* 1 */
  2307. "arm7tdmi", /* 2 */
  2308. "Cortex-M3", /* 3 */
  2309. "arm920t", /* 4 */
  2310. "arm926ejs", /* 5 */
  2311. _unknown, /* 6 */
  2312. _unknown, /* 7 */
  2313. _unknown, /* 8 */
  2314. _unknown, /* 9 */
  2315. _unknown, /* 10 */
  2316. _unknown, /* 11 */
  2317. _unknown, /* 12 */
  2318. _unknown, /* 13 */
  2319. _unknown, /* 14 */
  2320. _unknown, /* 15 */
  2321. };
  2322. #define nvpsize2 nvpsize /* these two tables are identical */
  2323. static const char *const nvpsize[] = {
  2324. "none", /* 0 */
  2325. "8K bytes", /* 1 */
  2326. "16K bytes", /* 2 */
  2327. "32K bytes", /* 3 */
  2328. _unknown, /* 4 */
  2329. "64K bytes", /* 5 */
  2330. _unknown, /* 6 */
  2331. "128K bytes", /* 7 */
  2332. _unknown, /* 8 */
  2333. "256K bytes", /* 9 */
  2334. "512K bytes", /* 10 */
  2335. _unknown, /* 11 */
  2336. "1024K bytes", /* 12 */
  2337. _unknown, /* 13 */
  2338. "2048K bytes", /* 14 */
  2339. _unknown, /* 15 */
  2340. };
  2341. static const char *const sramsize[] = {
  2342. "48K Bytes", /* 0 */
  2343. "1K Bytes", /* 1 */
  2344. "2K Bytes", /* 2 */
  2345. "6K Bytes", /* 3 */
  2346. "112K Bytes", /* 4 */
  2347. "4K Bytes", /* 5 */
  2348. "80K Bytes", /* 6 */
  2349. "160K Bytes", /* 7 */
  2350. "8K Bytes", /* 8 */
  2351. "16K Bytes", /* 9 */
  2352. "32K Bytes", /* 10 */
  2353. "64K Bytes", /* 11 */
  2354. "128K Bytes", /* 12 */
  2355. "256K Bytes", /* 13 */
  2356. "96K Bytes", /* 14 */
  2357. "512K Bytes", /* 15 */
  2358. };
  2359. static const struct archnames { unsigned value; const char *name; } archnames[] = {
  2360. { 0x19, "AT91SAM9xx Series" },
  2361. { 0x29, "AT91SAM9XExx Series" },
  2362. { 0x34, "AT91x34 Series" },
  2363. { 0x37, "CAP7 Series" },
  2364. { 0x39, "CAP9 Series" },
  2365. { 0x3B, "CAP11 Series" },
  2366. { 0x40, "AT91x40 Series" },
  2367. { 0x42, "AT91x42 Series" },
  2368. { 0x55, "AT91x55 Series" },
  2369. { 0x60, "AT91SAM7Axx Series" },
  2370. { 0x61, "AT91SAM7AQxx Series" },
  2371. { 0x63, "AT91x63 Series" },
  2372. { 0x70, "AT91SAM7Sxx Series" },
  2373. { 0x71, "AT91SAM7XCxx Series" },
  2374. { 0x72, "AT91SAM7SExx Series" },
  2375. { 0x73, "AT91SAM7Lxx Series" },
  2376. { 0x75, "AT91SAM7Xxx Series" },
  2377. { 0x76, "AT91SAM7SLxx Series" },
  2378. { 0x80, "ATSAM3UxC Series (100-pin version)" },
  2379. { 0x81, "ATSAM3UxE Series (144-pin version)" },
  2380. { 0x83, "ATSAM3AxC Series (100-pin version)" },
  2381. { 0x84, "ATSAM3XxC Series (100-pin version)" },
  2382. { 0x85, "ATSAM3XxE Series (144-pin version)" },
  2383. { 0x86, "ATSAM3XxG Series (208/217-pin version)" },
  2384. { 0x88, "ATSAM3SxA Series (48-pin version)" },
  2385. { 0x89, "ATSAM3SxB Series (64-pin version)" },
  2386. { 0x8A, "ATSAM3SxC Series (100-pin version)" },
  2387. { 0x92, "AT91x92 Series" },
  2388. { 0x93, "ATSAM3NxA Series (48-pin version)" },
  2389. { 0x94, "ATSAM3NxB Series (64-pin version)" },
  2390. { 0x95, "ATSAM3NxC Series (100-pin version)" },
  2391. { 0x98, "ATSAM3SDxA Series (48-pin version)" },
  2392. { 0x99, "ATSAM3SDxB Series (64-pin version)" },
  2393. { 0x9A, "ATSAM3SDxC Series (100-pin version)" },
  2394. { 0xA5, "ATSAM5A" },
  2395. { 0xF0, "AT75Cxx Series" },
  2396. { -1, NULL },
  2397. };
  2398. static const char *const nvptype[] = {
  2399. "rom", /* 0 */
  2400. "romless or onchip flash", /* 1 */
  2401. "embedded flash memory",/* 2 */
  2402. "rom(nvpsiz) + embedded flash (nvpsiz2)", /* 3 */
  2403. "sram emulating flash", /* 4 */
  2404. _unknown, /* 5 */
  2405. _unknown, /* 6 */
  2406. _unknown, /* 7 */
  2407. };
  2408. static const char *_yes_or_no(uint32_t v)
  2409. {
  2410. if (v)
  2411. return "YES";
  2412. else
  2413. return "NO";
  2414. }
  2415. static const char *const _rc_freq[] = {
  2416. "4 MHz", "8 MHz", "12 MHz", "reserved"
  2417. };
  2418. static void sam3_explain_ckgr_mor(struct sam3_chip *chip)
  2419. {
  2420. uint32_t v;
  2421. uint32_t rcen;
  2422. v = sam3_reg_fieldname(chip, "MOSCXTEN", chip->cfg.CKGR_MOR, 0, 1);
  2423. LOG_USER("(main xtal enabled: %s)", _yes_or_no(v));
  2424. v = sam3_reg_fieldname(chip, "MOSCXTBY", chip->cfg.CKGR_MOR, 1, 1);
  2425. LOG_USER("(main osc bypass: %s)", _yes_or_no(v));
  2426. rcen = sam3_reg_fieldname(chip, "MOSCRCEN", chip->cfg.CKGR_MOR, 3, 1);
  2427. LOG_USER("(onchip RC-OSC enabled: %s)", _yes_or_no(rcen));
  2428. v = sam3_reg_fieldname(chip, "MOSCRCF", chip->cfg.CKGR_MOR, 4, 3);
  2429. LOG_USER("(onchip RC-OSC freq: %s)", _rc_freq[v]);
  2430. chip->cfg.rc_freq = 0;
  2431. if (rcen) {
  2432. switch (v) {
  2433. default:
  2434. chip->cfg.rc_freq = 0;
  2435. break;
  2436. case 0:
  2437. chip->cfg.rc_freq = 4 * 1000 * 1000;
  2438. break;
  2439. case 1:
  2440. chip->cfg.rc_freq = 8 * 1000 * 1000;
  2441. break;
  2442. case 2:
  2443. chip->cfg.rc_freq = 12 * 1000 * 1000;
  2444. break;
  2445. }
  2446. }
  2447. v = sam3_reg_fieldname(chip, "MOSCXTST", chip->cfg.CKGR_MOR, 8, 8);
  2448. LOG_USER("(startup clks, time= %f uSecs)",
  2449. ((float)(v * 1000000)) / ((float)(chip->cfg.slow_freq)));
  2450. v = sam3_reg_fieldname(chip, "MOSCSEL", chip->cfg.CKGR_MOR, 24, 1);
  2451. LOG_USER("(mainosc source: %s)",
  2452. v ? "external xtal" : "internal RC");
  2453. v = sam3_reg_fieldname(chip, "CFDEN", chip->cfg.CKGR_MOR, 25, 1);
  2454. LOG_USER("(clock failure enabled: %s)",
  2455. _yes_or_no(v));
  2456. }
  2457. static void sam3_explain_chipid_cidr(struct sam3_chip *chip)
  2458. {
  2459. int x;
  2460. uint32_t v;
  2461. const char *cp;
  2462. sam3_reg_fieldname(chip, "Version", chip->cfg.CHIPID_CIDR, 0, 5);
  2463. LOG_USER_N("\n");
  2464. v = sam3_reg_fieldname(chip, "EPROC", chip->cfg.CHIPID_CIDR, 5, 3);
  2465. LOG_USER("%s", eproc_names[v]);
  2466. v = sam3_reg_fieldname(chip, "NVPSIZE", chip->cfg.CHIPID_CIDR, 8, 4);
  2467. LOG_USER("%s", nvpsize[v]);
  2468. v = sam3_reg_fieldname(chip, "NVPSIZE2", chip->cfg.CHIPID_CIDR, 12, 4);
  2469. LOG_USER("%s", nvpsize2[v]);
  2470. v = sam3_reg_fieldname(chip, "SRAMSIZE", chip->cfg.CHIPID_CIDR, 16, 4);
  2471. LOG_USER("%s", sramsize[v]);
  2472. v = sam3_reg_fieldname(chip, "ARCH", chip->cfg.CHIPID_CIDR, 20, 8);
  2473. cp = _unknown;
  2474. for (x = 0; archnames[x].name; x++) {
  2475. if (v == archnames[x].value) {
  2476. cp = archnames[x].name;
  2477. break;
  2478. }
  2479. }
  2480. LOG_USER("%s", cp);
  2481. v = sam3_reg_fieldname(chip, "NVPTYP", chip->cfg.CHIPID_CIDR, 28, 3);
  2482. LOG_USER("%s", nvptype[v]);
  2483. v = sam3_reg_fieldname(chip, "EXTID", chip->cfg.CHIPID_CIDR, 31, 1);
  2484. LOG_USER("(exists: %s)", _yes_or_no(v));
  2485. }
  2486. static void sam3_explain_ckgr_mcfr(struct sam3_chip *chip)
  2487. {
  2488. uint32_t v;
  2489. v = sam3_reg_fieldname(chip, "MAINFRDY", chip->cfg.CKGR_MCFR, 16, 1);
  2490. LOG_USER("(main ready: %s)", _yes_or_no(v));
  2491. v = sam3_reg_fieldname(chip, "MAINF", chip->cfg.CKGR_MCFR, 0, 16);
  2492. v = (v * chip->cfg.slow_freq) / 16;
  2493. chip->cfg.mainosc_freq = v;
  2494. LOG_USER("(%3.03f Mhz (%" PRIu32 ".%03" PRIu32 "khz slowclk)",
  2495. _tomhz(v),
  2496. (uint32_t)(chip->cfg.slow_freq / 1000),
  2497. (uint32_t)(chip->cfg.slow_freq % 1000));
  2498. }
  2499. static void sam3_explain_ckgr_plla(struct sam3_chip *chip)
  2500. {
  2501. uint32_t mula, diva;
  2502. diva = sam3_reg_fieldname(chip, "DIVA", chip->cfg.CKGR_PLLAR, 0, 8);
  2503. LOG_USER_N("\n");
  2504. mula = sam3_reg_fieldname(chip, "MULA", chip->cfg.CKGR_PLLAR, 16, 11);
  2505. LOG_USER_N("\n");
  2506. chip->cfg.plla_freq = 0;
  2507. if (mula == 0)
  2508. LOG_USER("\tPLLA Freq: (Disabled,mula = 0)");
  2509. else if (diva == 0)
  2510. LOG_USER("\tPLLA Freq: (Disabled,diva = 0)");
  2511. else if (diva >= 1) {
  2512. chip->cfg.plla_freq = (chip->cfg.mainosc_freq * (mula + 1) / diva);
  2513. LOG_USER("\tPLLA Freq: %3.03f MHz",
  2514. _tomhz(chip->cfg.plla_freq));
  2515. }
  2516. }
  2517. static void sam3_explain_mckr(struct sam3_chip *chip)
  2518. {
  2519. uint32_t css, pres, fin = 0;
  2520. int pdiv = 0;
  2521. const char *cp = NULL;
  2522. css = sam3_reg_fieldname(chip, "CSS", chip->cfg.PMC_MCKR, 0, 2);
  2523. switch (css & 3) {
  2524. case 0:
  2525. fin = chip->cfg.slow_freq;
  2526. cp = "slowclk";
  2527. break;
  2528. case 1:
  2529. fin = chip->cfg.mainosc_freq;
  2530. cp = "mainosc";
  2531. break;
  2532. case 2:
  2533. fin = chip->cfg.plla_freq;
  2534. cp = "plla";
  2535. break;
  2536. case 3:
  2537. if (chip->cfg.CKGR_UCKR & (1 << 16)) {
  2538. fin = 480 * 1000 * 1000;
  2539. cp = "upll";
  2540. } else {
  2541. fin = 0;
  2542. cp = "upll (*ERROR* UPLL is disabled)";
  2543. }
  2544. break;
  2545. default:
  2546. assert(0);
  2547. break;
  2548. }
  2549. LOG_USER("%s (%3.03f Mhz)",
  2550. cp,
  2551. _tomhz(fin));
  2552. pres = sam3_reg_fieldname(chip, "PRES", chip->cfg.PMC_MCKR, 4, 3);
  2553. switch (pres & 0x07) {
  2554. case 0:
  2555. pdiv = 1;
  2556. cp = "selected clock";
  2557. break;
  2558. case 1:
  2559. pdiv = 2;
  2560. cp = "clock/2";
  2561. break;
  2562. case 2:
  2563. pdiv = 4;
  2564. cp = "clock/4";
  2565. break;
  2566. case 3:
  2567. pdiv = 8;
  2568. cp = "clock/8";
  2569. break;
  2570. case 4:
  2571. pdiv = 16;
  2572. cp = "clock/16";
  2573. break;
  2574. case 5:
  2575. pdiv = 32;
  2576. cp = "clock/32";
  2577. break;
  2578. case 6:
  2579. pdiv = 64;
  2580. cp = "clock/64";
  2581. break;
  2582. case 7:
  2583. pdiv = 6;
  2584. cp = "clock/6";
  2585. break;
  2586. default:
  2587. assert(0);
  2588. break;
  2589. }
  2590. LOG_USER("(%s)", cp);
  2591. fin = fin / pdiv;
  2592. /* sam3 has a *SINGLE* clock - */
  2593. /* other at91 series parts have divisors for these. */
  2594. chip->cfg.cpu_freq = fin;
  2595. chip->cfg.mclk_freq = fin;
  2596. chip->cfg.fclk_freq = fin;
  2597. LOG_USER("\t\tResult CPU Freq: %3.03f",
  2598. _tomhz(fin));
  2599. }
  2600. #if 0
  2601. static struct sam3_chip *target2sam3(struct target *target)
  2602. {
  2603. struct sam3_chip *chip;
  2604. if (!target)
  2605. return NULL;
  2606. chip = all_sam3_chips;
  2607. while (chip) {
  2608. if (chip->target == target)
  2609. break; /* return below */
  2610. else
  2611. chip = chip->next;
  2612. }
  2613. return chip;
  2614. }
  2615. #endif
  2616. static uint32_t *sam3_get_reg_ptr(struct sam3_cfg *cfg, const struct sam3_reg_list *list)
  2617. {
  2618. /* this function exists to help */
  2619. /* keep funky offsetof() errors */
  2620. /* and casting from causing bugs */
  2621. /* By using prototypes - we can detect what would */
  2622. /* be casting errors. */
  2623. return (uint32_t *)(void *)(((char *)(cfg)) + list->struct_offset);
  2624. }
  2625. #define SAM3_ENTRY(NAME, FUNC) { .address = SAM3_ ## NAME, .struct_offset = offsetof( \
  2626. struct sam3_cfg, \
  2627. NAME), # NAME, FUNC }
  2628. static const struct sam3_reg_list sam3_all_regs[] = {
  2629. SAM3_ENTRY(CKGR_MOR, sam3_explain_ckgr_mor),
  2630. SAM3_ENTRY(CKGR_MCFR, sam3_explain_ckgr_mcfr),
  2631. SAM3_ENTRY(CKGR_PLLAR, sam3_explain_ckgr_plla),
  2632. SAM3_ENTRY(CKGR_UCKR, NULL),
  2633. SAM3_ENTRY(PMC_FSMR, NULL),
  2634. SAM3_ENTRY(PMC_FSPR, NULL),
  2635. SAM3_ENTRY(PMC_IMR, NULL),
  2636. SAM3_ENTRY(PMC_MCKR, sam3_explain_mckr),
  2637. SAM3_ENTRY(PMC_PCK0, NULL),
  2638. SAM3_ENTRY(PMC_PCK1, NULL),
  2639. SAM3_ENTRY(PMC_PCK2, NULL),
  2640. SAM3_ENTRY(PMC_PCSR, NULL),
  2641. SAM3_ENTRY(PMC_SCSR, NULL),
  2642. SAM3_ENTRY(PMC_SR, NULL),
  2643. SAM3_ENTRY(CHIPID_CIDR, sam3_explain_chipid_cidr),
  2644. SAM3_ENTRY(CHIPID_CIDR2, sam3_explain_chipid_cidr),
  2645. SAM3_ENTRY(CHIPID_EXID, NULL),
  2646. SAM3_ENTRY(CHIPID_EXID2, NULL),
  2647. /* TERMINATE THE LIST */
  2648. { .name = NULL }
  2649. };
  2650. #undef SAM3_ENTRY
  2651. static struct sam3_bank_private *get_sam3_bank_private(struct flash_bank *bank)
  2652. {
  2653. return bank->driver_priv;
  2654. }
  2655. /**
  2656. * Given a pointer to where it goes in the structure,
  2657. * determine the register name, address from the all registers table.
  2658. */
  2659. static const struct sam3_reg_list *sam3_get_reg(struct sam3_chip *chip, uint32_t *goes_here)
  2660. {
  2661. const struct sam3_reg_list *reg;
  2662. reg = &(sam3_all_regs[0]);
  2663. while (reg->name) {
  2664. uint32_t *possible;
  2665. /* calculate where this one go.. */
  2666. /* it is "possibly" this register. */
  2667. possible = ((uint32_t *)(void *)(((char *)(&(chip->cfg))) + reg->struct_offset));
  2668. /* well? Is it this register */
  2669. if (possible == goes_here) {
  2670. /* Jump for joy! */
  2671. return reg;
  2672. }
  2673. /* next... */
  2674. reg++;
  2675. }
  2676. /* This is *TOTAL*PANIC* - we are totally screwed. */
  2677. LOG_ERROR("INVALID SAM3 REGISTER");
  2678. return NULL;
  2679. }
  2680. static int sam3_read_this_reg(struct sam3_chip *chip, uint32_t *goes_here)
  2681. {
  2682. const struct sam3_reg_list *reg;
  2683. int r;
  2684. reg = sam3_get_reg(chip, goes_here);
  2685. if (!reg)
  2686. return ERROR_FAIL;
  2687. r = target_read_u32(chip->target, reg->address, goes_here);
  2688. if (r != ERROR_OK) {
  2689. LOG_ERROR("Cannot read SAM3 register: %s @ 0x%08x, Err: %d",
  2690. reg->name, (unsigned)(reg->address), r);
  2691. }
  2692. return r;
  2693. }
  2694. static int sam3_read_all_regs(struct sam3_chip *chip)
  2695. {
  2696. int r;
  2697. const struct sam3_reg_list *reg;
  2698. reg = &(sam3_all_regs[0]);
  2699. while (reg->name) {
  2700. r = sam3_read_this_reg(chip,
  2701. sam3_get_reg_ptr(&(chip->cfg), reg));
  2702. if (r != ERROR_OK) {
  2703. LOG_ERROR("Cannot read SAM3 register: %s @ 0x%08x, Error: %d",
  2704. reg->name, ((unsigned)(reg->address)), r);
  2705. return r;
  2706. }
  2707. reg++;
  2708. }
  2709. /* Chip identification register
  2710. *
  2711. * Unfortunately, the chip identification register is not at
  2712. * a constant address across all of the SAM3 series'. As a
  2713. * consequence, a simple heuristic is used to find where it's
  2714. * at...
  2715. *
  2716. * If the contents at the first address is zero, then we know
  2717. * that the second address is where the chip id register is.
  2718. * We can deduce this because for those SAM's that have the
  2719. * chip id @ 0x400e0940, the first address, 0x400e0740, is
  2720. * located in the memory map of the Power Management Controller
  2721. * (PMC). Furthermore, the address is not used by the PMC.
  2722. * So when read, the memory controller returns zero.*/
  2723. if (chip->cfg.CHIPID_CIDR == 0) {
  2724. /*Put the correct CIDR and EXID values in the chip structure */
  2725. chip->cfg.CHIPID_CIDR = chip->cfg.CHIPID_CIDR2;
  2726. chip->cfg.CHIPID_EXID = chip->cfg.CHIPID_EXID2;
  2727. }
  2728. return ERROR_OK;
  2729. }
  2730. static int sam3_get_info(struct sam3_chip *chip)
  2731. {
  2732. const struct sam3_reg_list *reg;
  2733. uint32_t regval;
  2734. reg = &(sam3_all_regs[0]);
  2735. while (reg->name) {
  2736. /* display all regs */
  2737. LOG_DEBUG("Start: %s", reg->name);
  2738. regval = *sam3_get_reg_ptr(&(chip->cfg), reg);
  2739. LOG_USER("%*s: [0x%08" PRIx32 "] -> 0x%08" PRIx32,
  2740. REG_NAME_WIDTH,
  2741. reg->name,
  2742. reg->address,
  2743. regval);
  2744. if (reg->explain_func)
  2745. (*(reg->explain_func))(chip);
  2746. LOG_DEBUG("End: %s", reg->name);
  2747. reg++;
  2748. }
  2749. LOG_USER(" rc-osc: %3.03f MHz", _tomhz(chip->cfg.rc_freq));
  2750. LOG_USER(" mainosc: %3.03f MHz", _tomhz(chip->cfg.mainosc_freq));
  2751. LOG_USER(" plla: %3.03f MHz", _tomhz(chip->cfg.plla_freq));
  2752. LOG_USER(" cpu-freq: %3.03f MHz", _tomhz(chip->cfg.cpu_freq));
  2753. LOG_USER("mclk-freq: %3.03f MHz", _tomhz(chip->cfg.mclk_freq));
  2754. LOG_USER(" UniqueId: 0x%08" PRIx32 " 0x%08" PRIx32 " 0x%08" PRIx32 " 0x%08" PRIx32,
  2755. chip->cfg.unique_id[0],
  2756. chip->cfg.unique_id[1],
  2757. chip->cfg.unique_id[2],
  2758. chip->cfg.unique_id[3]);
  2759. return ERROR_OK;
  2760. }
  2761. static int sam3_protect_check(struct flash_bank *bank)
  2762. {
  2763. int r;
  2764. uint32_t v = 0;
  2765. unsigned x;
  2766. struct sam3_bank_private *private;
  2767. LOG_DEBUG("Begin");
  2768. if (bank->target->state != TARGET_HALTED) {
  2769. LOG_ERROR("Target not halted");
  2770. return ERROR_TARGET_NOT_HALTED;
  2771. }
  2772. private = get_sam3_bank_private(bank);
  2773. if (!private) {
  2774. LOG_ERROR("no private for this bank?");
  2775. return ERROR_FAIL;
  2776. }
  2777. if (!(private->probed))
  2778. return ERROR_FLASH_BANK_NOT_PROBED;
  2779. r = flashd_get_lock_bits(private, &v);
  2780. if (r != ERROR_OK) {
  2781. LOG_DEBUG("Failed: %d", r);
  2782. return r;
  2783. }
  2784. for (x = 0; x < private->nsectors; x++)
  2785. bank->sectors[x].is_protected = (!!(v & (1 << x)));
  2786. LOG_DEBUG("Done");
  2787. return ERROR_OK;
  2788. }
  2789. FLASH_BANK_COMMAND_HANDLER(sam3_flash_bank_command)
  2790. {
  2791. struct sam3_chip *chip;
  2792. chip = all_sam3_chips;
  2793. /* is this an existing chip? */
  2794. while (chip) {
  2795. if (chip->target == bank->target)
  2796. break;
  2797. chip = chip->next;
  2798. }
  2799. if (!chip) {
  2800. /* this is a *NEW* chip */
  2801. chip = calloc(1, sizeof(struct sam3_chip));
  2802. if (!chip) {
  2803. LOG_ERROR("NO RAM!");
  2804. return ERROR_FAIL;
  2805. }
  2806. chip->target = bank->target;
  2807. /* insert at head */
  2808. chip->next = all_sam3_chips;
  2809. all_sam3_chips = chip;
  2810. chip->target = bank->target;
  2811. /* assumption is this runs at 32khz */
  2812. chip->cfg.slow_freq = 32768;
  2813. chip->probed = false;
  2814. }
  2815. switch (bank->base) {
  2816. default:
  2817. LOG_ERROR("Address 0x%08x invalid bank address (try 0x%08x or 0x%08x "
  2818. "[at91sam3u series] or 0x%08x [at91sam3s series] or "
  2819. "0x%08x [at91sam3n series] or 0x%08x or 0x%08x or 0x%08x[at91sam3ax series] )",
  2820. ((unsigned int)(bank->base)),
  2821. ((unsigned int)(FLASH_BANK0_BASE_U)),
  2822. ((unsigned int)(FLASH_BANK1_BASE_U)),
  2823. ((unsigned int)(FLASH_BANK_BASE_S)),
  2824. ((unsigned int)(FLASH_BANK_BASE_N)),
  2825. ((unsigned int)(FLASH_BANK0_BASE_AX)),
  2826. ((unsigned int)(FLASH_BANK1_BASE_256K_AX)),
  2827. ((unsigned int)(FLASH_BANK1_BASE_512K_AX)));
  2828. return ERROR_FAIL;
  2829. /* at91sam3s and at91sam3n series only has bank 0*/
  2830. /* at91sam3u and at91sam3ax series has the same address for bank 0*/
  2831. case FLASH_BANK_BASE_S:
  2832. case FLASH_BANK0_BASE_U:
  2833. bank->driver_priv = &(chip->details.bank[0]);
  2834. bank->bank_number = 0;
  2835. chip->details.bank[0].chip = chip;
  2836. chip->details.bank[0].bank = bank;
  2837. break;
  2838. /* Bank 1 of at91sam3u or at91sam3ax series */
  2839. case FLASH_BANK1_BASE_U:
  2840. case FLASH_BANK1_BASE_256K_AX:
  2841. case FLASH_BANK1_BASE_512K_AX:
  2842. bank->driver_priv = &(chip->details.bank[1]);
  2843. bank->bank_number = 1;
  2844. chip->details.bank[1].chip = chip;
  2845. chip->details.bank[1].bank = bank;
  2846. break;
  2847. }
  2848. /* we initialize after probing. */
  2849. return ERROR_OK;
  2850. }
  2851. /**
  2852. * Remove all chips from the internal list without distinguishing which one
  2853. * is owned by this bank. This simplification works only for one shot
  2854. * deallocation like current flash_free_all_banks()
  2855. */
  2856. static void sam3_free_driver_priv(struct flash_bank *bank)
  2857. {
  2858. struct sam3_chip *chip = all_sam3_chips;
  2859. while (chip) {
  2860. struct sam3_chip *next = chip->next;
  2861. free(chip);
  2862. chip = next;
  2863. }
  2864. all_sam3_chips = NULL;
  2865. }
  2866. static int sam3_get_details(struct sam3_bank_private *private)
  2867. {
  2868. const struct sam3_chip_details *details;
  2869. struct sam3_chip *chip;
  2870. struct flash_bank *saved_banks[SAM3_MAX_FLASH_BANKS];
  2871. unsigned x;
  2872. LOG_DEBUG("Begin");
  2873. details = all_sam3_details;
  2874. while (details->name) {
  2875. /* Compare cidr without version bits */
  2876. if (((details->chipid_cidr ^ private->chip->cfg.CHIPID_CIDR) & 0xFFFFFFE0) == 0)
  2877. break;
  2878. else
  2879. details++;
  2880. }
  2881. if (!details->name) {
  2882. LOG_ERROR("SAM3 ChipID 0x%08x not found in table (perhaps you can ID this chip?)",
  2883. (unsigned int)(private->chip->cfg.CHIPID_CIDR));
  2884. /* Help the victim, print details about the chip */
  2885. LOG_INFO("SAM3 CHIPID_CIDR: 0x%08" PRIx32 " decodes as follows",
  2886. private->chip->cfg.CHIPID_CIDR);
  2887. sam3_explain_chipid_cidr(private->chip);
  2888. return ERROR_FAIL;
  2889. }
  2890. /* DANGER: THERE ARE DRAGONS HERE */
  2891. /* get our chip - it is going */
  2892. /* to be over-written shortly */
  2893. chip = private->chip;
  2894. /* Note that, in reality: */
  2895. /* */
  2896. /* private = &(chip->details.bank[0]) */
  2897. /* or private = &(chip->details.bank[1]) */
  2898. /* */
  2899. /* save the "bank" pointers */
  2900. for (x = 0; x < SAM3_MAX_FLASH_BANKS; x++)
  2901. saved_banks[x] = chip->details.bank[x].bank;
  2902. /* Overwrite the "details" structure. */
  2903. memcpy(&(private->chip->details),
  2904. details,
  2905. sizeof(private->chip->details));
  2906. /* now fix the ghosted pointers */
  2907. for (x = 0; x < SAM3_MAX_FLASH_BANKS; x++) {
  2908. chip->details.bank[x].chip = chip;
  2909. chip->details.bank[x].bank = saved_banks[x];
  2910. }
  2911. /* update the *BANK*SIZE* */
  2912. LOG_DEBUG("End");
  2913. return ERROR_OK;
  2914. }
  2915. static int _sam3_probe(struct flash_bank *bank, int noise)
  2916. {
  2917. int r;
  2918. struct sam3_bank_private *private;
  2919. LOG_DEBUG("Begin: Bank: %u, Noise: %d", bank->bank_number, noise);
  2920. if (bank->target->state != TARGET_HALTED) {
  2921. LOG_ERROR("Target not halted");
  2922. return ERROR_TARGET_NOT_HALTED;
  2923. }
  2924. private = get_sam3_bank_private(bank);
  2925. if (!private) {
  2926. LOG_ERROR("Invalid/unknown bank number");
  2927. return ERROR_FAIL;
  2928. }
  2929. r = sam3_read_all_regs(private->chip);
  2930. if (r != ERROR_OK)
  2931. return r;
  2932. LOG_DEBUG("Here");
  2933. if (private->chip->probed)
  2934. r = sam3_get_info(private->chip);
  2935. else
  2936. r = sam3_get_details(private);
  2937. if (r != ERROR_OK)
  2938. return r;
  2939. /* update the flash bank size */
  2940. for (unsigned int x = 0; x < SAM3_MAX_FLASH_BANKS; x++) {
  2941. if (bank->base == private->chip->details.bank[x].base_address) {
  2942. bank->size = private->chip->details.bank[x].size_bytes;
  2943. break;
  2944. }
  2945. }
  2946. if (!bank->sectors) {
  2947. bank->sectors = calloc(private->nsectors, (sizeof((bank->sectors)[0])));
  2948. if (!bank->sectors) {
  2949. LOG_ERROR("No memory!");
  2950. return ERROR_FAIL;
  2951. }
  2952. bank->num_sectors = private->nsectors;
  2953. for (unsigned int x = 0; x < bank->num_sectors; x++) {
  2954. bank->sectors[x].size = private->sector_size;
  2955. bank->sectors[x].offset = x * (private->sector_size);
  2956. /* mark as unknown */
  2957. bank->sectors[x].is_erased = -1;
  2958. bank->sectors[x].is_protected = -1;
  2959. }
  2960. }
  2961. private->probed = true;
  2962. r = sam3_protect_check(bank);
  2963. if (r != ERROR_OK)
  2964. return r;
  2965. LOG_DEBUG("Bank = %d, nbanks = %d",
  2966. private->bank_number, private->chip->details.n_banks);
  2967. if ((private->bank_number + 1) == private->chip->details.n_banks) {
  2968. /* read unique id, */
  2969. /* it appears to be associated with the *last* flash bank. */
  2970. flashd_read_uid(private);
  2971. }
  2972. return r;
  2973. }
  2974. static int sam3_probe(struct flash_bank *bank)
  2975. {
  2976. return _sam3_probe(bank, 1);
  2977. }
  2978. static int sam3_auto_probe(struct flash_bank *bank)
  2979. {
  2980. return _sam3_probe(bank, 0);
  2981. }
  2982. static int sam3_erase(struct flash_bank *bank, unsigned int first,
  2983. unsigned int last)
  2984. {
  2985. struct sam3_bank_private *private;
  2986. int r;
  2987. LOG_DEBUG("Here");
  2988. if (bank->target->state != TARGET_HALTED) {
  2989. LOG_ERROR("Target not halted");
  2990. return ERROR_TARGET_NOT_HALTED;
  2991. }
  2992. r = sam3_auto_probe(bank);
  2993. if (r != ERROR_OK) {
  2994. LOG_DEBUG("Here,r=%d", r);
  2995. return r;
  2996. }
  2997. private = get_sam3_bank_private(bank);
  2998. if (!(private->probed))
  2999. return ERROR_FLASH_BANK_NOT_PROBED;
  3000. if ((first == 0) && ((last + 1) == private->nsectors)) {
  3001. /* whole chip */
  3002. LOG_DEBUG("Here");
  3003. return flashd_erase_entire_bank(private);
  3004. }
  3005. LOG_INFO("sam3 auto-erases while programming (request ignored)");
  3006. return ERROR_OK;
  3007. }
  3008. static int sam3_protect(struct flash_bank *bank, int set, unsigned int first,
  3009. unsigned int last)
  3010. {
  3011. struct sam3_bank_private *private;
  3012. int r;
  3013. LOG_DEBUG("Here");
  3014. if (bank->target->state != TARGET_HALTED) {
  3015. LOG_ERROR("Target not halted");
  3016. return ERROR_TARGET_NOT_HALTED;
  3017. }
  3018. private = get_sam3_bank_private(bank);
  3019. if (!(private->probed))
  3020. return ERROR_FLASH_BANK_NOT_PROBED;
  3021. if (set)
  3022. r = flashd_lock(private, first, last);
  3023. else
  3024. r = flashd_unlock(private, first, last);
  3025. LOG_DEBUG("End: r=%d", r);
  3026. return r;
  3027. }
  3028. static int sam3_page_read(struct sam3_bank_private *private, unsigned pagenum, uint8_t *buf)
  3029. {
  3030. uint32_t adr;
  3031. int r;
  3032. adr = pagenum * private->page_size;
  3033. adr += private->base_address;
  3034. r = target_read_memory(private->chip->target,
  3035. adr,
  3036. 4, /* THIS*MUST*BE* in 32bit values */
  3037. private->page_size / 4,
  3038. buf);
  3039. if (r != ERROR_OK)
  3040. LOG_ERROR("SAM3: Flash program failed to read page phys address: 0x%08x",
  3041. (unsigned int)(adr));
  3042. return r;
  3043. }
  3044. static int sam3_page_write(struct sam3_bank_private *private, unsigned pagenum, const uint8_t *buf)
  3045. {
  3046. uint32_t adr;
  3047. uint32_t status;
  3048. uint32_t fmr; /* EEFC Flash Mode Register */
  3049. int r;
  3050. adr = pagenum * private->page_size;
  3051. adr += private->base_address;
  3052. /* Get flash mode register value */
  3053. r = target_read_u32(private->chip->target, private->controller_address, &fmr);
  3054. if (r != ERROR_OK)
  3055. LOG_DEBUG("Error Read failed: read flash mode register");
  3056. /* Clear flash wait state field */
  3057. fmr &= 0xfffff0ff;
  3058. /* set FWS (flash wait states) field in the FMR (flash mode register) */
  3059. fmr |= (private->flash_wait_states << 8);
  3060. LOG_DEBUG("Flash Mode: 0x%08x", ((unsigned int)(fmr)));
  3061. r = target_write_u32(private->bank->target, private->controller_address, fmr);
  3062. if (r != ERROR_OK)
  3063. LOG_DEBUG("Error Write failed: set flash mode register");
  3064. LOG_DEBUG("Wr Page %u @ phys address: 0x%08x", pagenum, (unsigned int)(adr));
  3065. r = target_write_memory(private->chip->target,
  3066. adr,
  3067. 4, /* THIS*MUST*BE* in 32bit values */
  3068. private->page_size / 4,
  3069. buf);
  3070. if (r != ERROR_OK) {
  3071. LOG_ERROR("SAM3: Failed to write (buffer) page at phys address 0x%08x",
  3072. (unsigned int)(adr));
  3073. return r;
  3074. }
  3075. r = efc_perform_command(private,
  3076. /* send Erase & Write Page */
  3077. AT91C_EFC_FCMD_EWP,
  3078. pagenum,
  3079. &status);
  3080. if (r != ERROR_OK)
  3081. LOG_ERROR("SAM3: Error performing Erase & Write page @ phys address 0x%08x",
  3082. (unsigned int)(adr));
  3083. if (status & (1 << 2)) {
  3084. LOG_ERROR("SAM3: Page @ Phys address 0x%08x is locked", (unsigned int)(adr));
  3085. return ERROR_FAIL;
  3086. }
  3087. if (status & (1 << 1)) {
  3088. LOG_ERROR("SAM3: Flash Command error @phys address 0x%08x", (unsigned int)(adr));
  3089. return ERROR_FAIL;
  3090. }
  3091. return ERROR_OK;
  3092. }
  3093. static int sam3_write(struct flash_bank *bank,
  3094. const uint8_t *buffer,
  3095. uint32_t offset,
  3096. uint32_t count)
  3097. {
  3098. int n;
  3099. unsigned page_cur;
  3100. unsigned page_end;
  3101. int r;
  3102. unsigned page_offset;
  3103. struct sam3_bank_private *private;
  3104. uint8_t *pagebuffer;
  3105. /* in case we bail further below, set this to null */
  3106. pagebuffer = NULL;
  3107. /* ignore dumb requests */
  3108. if (count == 0) {
  3109. r = ERROR_OK;
  3110. goto done;
  3111. }
  3112. if (bank->target->state != TARGET_HALTED) {
  3113. LOG_ERROR("Target not halted");
  3114. r = ERROR_TARGET_NOT_HALTED;
  3115. goto done;
  3116. }
  3117. private = get_sam3_bank_private(bank);
  3118. if (!(private->probed)) {
  3119. r = ERROR_FLASH_BANK_NOT_PROBED;
  3120. goto done;
  3121. }
  3122. if ((offset + count) > private->size_bytes) {
  3123. LOG_ERROR("Flash write error - past end of bank");
  3124. LOG_ERROR(" offset: 0x%08x, count 0x%08x, BankEnd: 0x%08x",
  3125. (unsigned int)(offset),
  3126. (unsigned int)(count),
  3127. (unsigned int)(private->size_bytes));
  3128. r = ERROR_FAIL;
  3129. goto done;
  3130. }
  3131. pagebuffer = malloc(private->page_size);
  3132. if (!pagebuffer) {
  3133. LOG_ERROR("No memory for %d Byte page buffer", (int)(private->page_size));
  3134. r = ERROR_FAIL;
  3135. goto done;
  3136. }
  3137. /* what page do we start & end in? */
  3138. page_cur = offset / private->page_size;
  3139. page_end = (offset + count - 1) / private->page_size;
  3140. LOG_DEBUG("Offset: 0x%08x, Count: 0x%08x", (unsigned int)(offset), (unsigned int)(count));
  3141. LOG_DEBUG("Page start: %d, Page End: %d", (int)(page_cur), (int)(page_end));
  3142. /* Special case: all one page */
  3143. /* */
  3144. /* Otherwise: */
  3145. /* (1) non-aligned start */
  3146. /* (2) body pages */
  3147. /* (3) non-aligned end. */
  3148. /* Handle special case - all one page. */
  3149. if (page_cur == page_end) {
  3150. LOG_DEBUG("Special case, all in one page");
  3151. r = sam3_page_read(private, page_cur, pagebuffer);
  3152. if (r != ERROR_OK)
  3153. goto done;
  3154. page_offset = (offset & (private->page_size-1));
  3155. memcpy(pagebuffer + page_offset,
  3156. buffer,
  3157. count);
  3158. r = sam3_page_write(private, page_cur, pagebuffer);
  3159. if (r != ERROR_OK)
  3160. goto done;
  3161. r = ERROR_OK;
  3162. goto done;
  3163. }
  3164. /* non-aligned start */
  3165. page_offset = offset & (private->page_size - 1);
  3166. if (page_offset) {
  3167. LOG_DEBUG("Not-Aligned start");
  3168. /* read the partial */
  3169. r = sam3_page_read(private, page_cur, pagebuffer);
  3170. if (r != ERROR_OK)
  3171. goto done;
  3172. /* over-write with new data */
  3173. n = (private->page_size - page_offset);
  3174. memcpy(pagebuffer + page_offset,
  3175. buffer,
  3176. n);
  3177. r = sam3_page_write(private, page_cur, pagebuffer);
  3178. if (r != ERROR_OK)
  3179. goto done;
  3180. count -= n;
  3181. offset += n;
  3182. buffer += n;
  3183. page_cur++;
  3184. }
  3185. /* By checking that offset is correct here, we also
  3186. fix a clang warning */
  3187. assert(offset % private->page_size == 0);
  3188. /* intermediate large pages */
  3189. /* also - the final *terminal* */
  3190. /* if that terminal page is a full page */
  3191. LOG_DEBUG("Full Page Loop: cur=%d, end=%d, count = 0x%08x",
  3192. (int)page_cur, (int)page_end, (unsigned int)(count));
  3193. while ((page_cur < page_end) &&
  3194. (count >= private->page_size)) {
  3195. r = sam3_page_write(private, page_cur, buffer);
  3196. if (r != ERROR_OK)
  3197. goto done;
  3198. count -= private->page_size;
  3199. buffer += private->page_size;
  3200. page_cur += 1;
  3201. }
  3202. /* terminal partial page? */
  3203. if (count) {
  3204. LOG_DEBUG("Terminal partial page, count = 0x%08x", (unsigned int)(count));
  3205. /* we have a partial page */
  3206. r = sam3_page_read(private, page_cur, pagebuffer);
  3207. if (r != ERROR_OK)
  3208. goto done;
  3209. /* data goes at start */
  3210. memcpy(pagebuffer, buffer, count);
  3211. r = sam3_page_write(private, page_cur, pagebuffer);
  3212. if (r != ERROR_OK)
  3213. goto done;
  3214. }
  3215. LOG_DEBUG("Done!");
  3216. r = ERROR_OK;
  3217. done:
  3218. free(pagebuffer);
  3219. return r;
  3220. }
  3221. COMMAND_HANDLER(sam3_handle_info_command)
  3222. {
  3223. struct sam3_chip *chip;
  3224. chip = get_current_sam3(CMD);
  3225. if (!chip)
  3226. return ERROR_OK;
  3227. unsigned x;
  3228. int r;
  3229. /* bank0 must exist before we can do anything */
  3230. if (!chip->details.bank[0].bank) {
  3231. x = 0;
  3232. need_define:
  3233. command_print(CMD,
  3234. "Please define bank %d via command: flash bank %s ... ",
  3235. x,
  3236. at91sam3_flash.name);
  3237. return ERROR_FAIL;
  3238. }
  3239. /* if bank 0 is not probed, then probe it */
  3240. if (!(chip->details.bank[0].probed)) {
  3241. r = sam3_auto_probe(chip->details.bank[0].bank);
  3242. if (r != ERROR_OK)
  3243. return ERROR_FAIL;
  3244. }
  3245. /* above guarantees the "chip details" structure is valid */
  3246. /* and thus, bank private areas are valid */
  3247. /* and we have a SAM3 chip, what a concept! */
  3248. /* auto-probe other banks, 0 done above */
  3249. for (x = 1; x < SAM3_MAX_FLASH_BANKS; x++) {
  3250. /* skip banks not present */
  3251. if (!(chip->details.bank[x].present))
  3252. continue;
  3253. if (!chip->details.bank[x].bank)
  3254. goto need_define;
  3255. if (chip->details.bank[x].probed)
  3256. continue;
  3257. r = sam3_auto_probe(chip->details.bank[x].bank);
  3258. if (r != ERROR_OK)
  3259. return r;
  3260. }
  3261. r = sam3_get_info(chip);
  3262. if (r != ERROR_OK) {
  3263. LOG_DEBUG("Sam3Info, Failed %d", r);
  3264. return r;
  3265. }
  3266. return ERROR_OK;
  3267. }
  3268. COMMAND_HANDLER(sam3_handle_gpnvm_command)
  3269. {
  3270. unsigned x, v;
  3271. int r, who;
  3272. struct sam3_chip *chip;
  3273. chip = get_current_sam3(CMD);
  3274. if (!chip)
  3275. return ERROR_OK;
  3276. if (chip->target->state != TARGET_HALTED) {
  3277. LOG_ERROR("sam3 - target not halted");
  3278. return ERROR_TARGET_NOT_HALTED;
  3279. }
  3280. if (!chip->details.bank[0].bank) {
  3281. command_print(CMD, "Bank0 must be defined first via: flash bank %s ...",
  3282. at91sam3_flash.name);
  3283. return ERROR_FAIL;
  3284. }
  3285. if (!chip->details.bank[0].probed) {
  3286. r = sam3_auto_probe(chip->details.bank[0].bank);
  3287. if (r != ERROR_OK)
  3288. return r;
  3289. }
  3290. switch (CMD_ARGC) {
  3291. default:
  3292. return ERROR_COMMAND_SYNTAX_ERROR;
  3293. case 0:
  3294. goto showall;
  3295. case 1:
  3296. who = -1;
  3297. break;
  3298. case 2:
  3299. if ((strcmp(CMD_ARGV[0], "show") == 0) && (strcmp(CMD_ARGV[1], "all") == 0))
  3300. who = -1;
  3301. else {
  3302. uint32_t v32;
  3303. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], v32);
  3304. who = v32;
  3305. }
  3306. break;
  3307. }
  3308. if (strcmp("show", CMD_ARGV[0]) == 0) {
  3309. if (who == -1) {
  3310. showall:
  3311. r = ERROR_OK;
  3312. for (x = 0; x < chip->details.n_gpnvms; x++) {
  3313. r = flashd_get_gpnvm(&(chip->details.bank[0]), x, &v);
  3314. if (r != ERROR_OK)
  3315. break;
  3316. command_print(CMD, "sam3-gpnvm%u: %u", x, v);
  3317. }
  3318. return r;
  3319. }
  3320. if ((who >= 0) && (((unsigned)(who)) < chip->details.n_gpnvms)) {
  3321. r = flashd_get_gpnvm(&(chip->details.bank[0]), who, &v);
  3322. if (r == ERROR_OK)
  3323. command_print(CMD, "sam3-gpnvm%u: %u", who, v);
  3324. return r;
  3325. } else {
  3326. command_print(CMD, "sam3-gpnvm invalid GPNVM: %u", who);
  3327. return ERROR_COMMAND_SYNTAX_ERROR;
  3328. }
  3329. }
  3330. if (who == -1) {
  3331. command_print(CMD, "Missing GPNVM number");
  3332. return ERROR_COMMAND_SYNTAX_ERROR;
  3333. }
  3334. if (strcmp("set", CMD_ARGV[0]) == 0)
  3335. r = flashd_set_gpnvm(&(chip->details.bank[0]), who);
  3336. else if ((strcmp("clr", CMD_ARGV[0]) == 0) ||
  3337. (strcmp("clear", CMD_ARGV[0]) == 0)) /* quietly accept both */
  3338. r = flashd_clr_gpnvm(&(chip->details.bank[0]), who);
  3339. else {
  3340. command_print(CMD, "Unknown command: %s", CMD_ARGV[0]);
  3341. r = ERROR_COMMAND_SYNTAX_ERROR;
  3342. }
  3343. return r;
  3344. }
  3345. COMMAND_HANDLER(sam3_handle_slowclk_command)
  3346. {
  3347. struct sam3_chip *chip;
  3348. chip = get_current_sam3(CMD);
  3349. if (!chip)
  3350. return ERROR_OK;
  3351. switch (CMD_ARGC) {
  3352. case 0:
  3353. /* show */
  3354. break;
  3355. case 1:
  3356. {
  3357. /* set */
  3358. uint32_t v;
  3359. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], v);
  3360. if (v > 200000) {
  3361. /* absurd slow clock of 200Khz? */
  3362. command_print(CMD, "Absurd/illegal slow clock freq: %d\n", (int)(v));
  3363. return ERROR_COMMAND_SYNTAX_ERROR;
  3364. }
  3365. chip->cfg.slow_freq = v;
  3366. break;
  3367. }
  3368. default:
  3369. /* error */
  3370. command_print(CMD, "Too many parameters");
  3371. return ERROR_COMMAND_SYNTAX_ERROR;
  3372. }
  3373. command_print(CMD, "Slowclk freq: %d.%03dkhz",
  3374. (int)(chip->cfg.slow_freq / 1000),
  3375. (int)(chip->cfg.slow_freq % 1000));
  3376. return ERROR_OK;
  3377. }
  3378. static const struct command_registration at91sam3_exec_command_handlers[] = {
  3379. {
  3380. .name = "gpnvm",
  3381. .handler = sam3_handle_gpnvm_command,
  3382. .mode = COMMAND_EXEC,
  3383. .usage = "[('clr'|'set'|'show') bitnum]",
  3384. .help = "Without arguments, shows all bits in the gpnvm "
  3385. "register. Otherwise, clears, sets, or shows one "
  3386. "General Purpose Non-Volatile Memory (gpnvm) bit.",
  3387. },
  3388. {
  3389. .name = "info",
  3390. .handler = sam3_handle_info_command,
  3391. .mode = COMMAND_EXEC,
  3392. .help = "Print information about the current at91sam3 chip "
  3393. "and its flash configuration.",
  3394. .usage = "",
  3395. },
  3396. {
  3397. .name = "slowclk",
  3398. .handler = sam3_handle_slowclk_command,
  3399. .mode = COMMAND_EXEC,
  3400. .usage = "[clock_hz]",
  3401. .help = "Display or set the slowclock frequency "
  3402. "(default 32768 Hz).",
  3403. },
  3404. COMMAND_REGISTRATION_DONE
  3405. };
  3406. static const struct command_registration at91sam3_command_handlers[] = {
  3407. {
  3408. .name = "at91sam3",
  3409. .mode = COMMAND_ANY,
  3410. .help = "at91sam3 flash command group",
  3411. .usage = "",
  3412. .chain = at91sam3_exec_command_handlers,
  3413. },
  3414. COMMAND_REGISTRATION_DONE
  3415. };
  3416. const struct flash_driver at91sam3_flash = {
  3417. .name = "at91sam3",
  3418. .commands = at91sam3_command_handlers,
  3419. .flash_bank_command = sam3_flash_bank_command,
  3420. .erase = sam3_erase,
  3421. .protect = sam3_protect,
  3422. .write = sam3_write,
  3423. .read = default_flash_read,
  3424. .probe = sam3_probe,
  3425. .auto_probe = sam3_auto_probe,
  3426. .erase_check = default_flash_blank_check,
  3427. .protect_check = sam3_protect_check,
  3428. .free_driver_priv = sam3_free_driver_priv,
  3429. };