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.
 
 
 
 
 
 

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