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.
 
 
 
 
 
 

2514 lines
64 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, write to the *
  23. * Free Software Foundation, Inc., *
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  25. ****************************************************************************/
  26. /* Some of the the lower level code was based on code supplied by
  27. * ATMEL under this copyright. */
  28. /* BEGIN ATMEL COPYRIGHT */
  29. /* ----------------------------------------------------------------------------
  30. * ATMEL Microcontroller Software Support
  31. * ----------------------------------------------------------------------------
  32. * Copyright (c) 2009, Atmel Corporation
  33. *
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions are met:
  38. *
  39. * - Redistributions of source code must retain the above copyright notice,
  40. * this list of conditions and the disclaimer below.
  41. *
  42. * Atmel's name may not be used to endorse or promote products derived from
  43. * this software without specific prior written permission.
  44. *
  45. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
  46. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  47. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  48. * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
  49. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  50. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  51. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  52. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  53. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  54. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  55. * ----------------------------------------------------------------------------
  56. */
  57. /* END ATMEL COPYRIGHT */
  58. #ifdef HAVE_CONFIG_H
  59. #include "config.h"
  60. #endif
  61. #include "imp.h"
  62. #include <helper/time_support.h>
  63. #define REG_NAME_WIDTH (12)
  64. /* at91sam4s/at91sam4e series (has always one flash bank)*/
  65. #define FLASH_BANK_BASE_S 0x00400000
  66. /* at91sam4sd series (two one flash banks), first bank address */
  67. #define FLASH_BANK0_BASE_SD FLASH_BANK_BASE_S
  68. /* at91sam4sd16x, second bank address */
  69. #define FLASH_BANK1_BASE_1024K_SD (FLASH_BANK0_BASE_SD+(1024*1024/2))
  70. /* at91sam4sd32x, second bank address */
  71. #define FLASH_BANK1_BASE_2048K_SD (FLASH_BANK0_BASE_SD+(2048*1024/2))
  72. #define AT91C_EFC_FCMD_GETD (0x0) /* (EFC) Get Flash Descriptor */
  73. #define AT91C_EFC_FCMD_WP (0x1) /* (EFC) Write Page */
  74. #define AT91C_EFC_FCMD_WPL (0x2) /* (EFC) Write Page and Lock */
  75. #define AT91C_EFC_FCMD_EWP (0x3) /* (EFC) Erase Page and Write Page */
  76. #define AT91C_EFC_FCMD_EWPL (0x4) /* (EFC) Erase Page and Write Page then Lock */
  77. #define AT91C_EFC_FCMD_EA (0x5) /* (EFC) Erase All */
  78. /* cmd6 is not present in the at91sam4u4/2/1 data sheet table 19-2 */
  79. /* #define AT91C_EFC_FCMD_EPL (0x6) // (EFC) Erase plane? */
  80. #define AT91C_EFC_FCMD_EPA (0x7) /* (EFC) Erase pages */
  81. #define AT91C_EFC_FCMD_SLB (0x8) /* (EFC) Set Lock Bit */
  82. #define AT91C_EFC_FCMD_CLB (0x9) /* (EFC) Clear Lock Bit */
  83. #define AT91C_EFC_FCMD_GLB (0xA) /* (EFC) Get Lock Bit */
  84. #define AT91C_EFC_FCMD_SFB (0xB) /* (EFC) Set Fuse Bit */
  85. #define AT91C_EFC_FCMD_CFB (0xC) /* (EFC) Clear Fuse Bit */
  86. #define AT91C_EFC_FCMD_GFB (0xD) /* (EFC) Get Fuse Bit */
  87. #define AT91C_EFC_FCMD_STUI (0xE) /* (EFC) Start Read Unique ID */
  88. #define AT91C_EFC_FCMD_SPUI (0xF) /* (EFC) Stop Read Unique ID */
  89. #define offset_EFC_FMR 0
  90. #define offset_EFC_FCR 4
  91. #define offset_EFC_FSR 8
  92. #define offset_EFC_FRR 12
  93. extern struct flash_driver at91sam4_flash;
  94. static float _tomhz(uint32_t freq_hz)
  95. {
  96. float f;
  97. f = ((float)(freq_hz)) / 1000000.0;
  98. return f;
  99. }
  100. /* How the chip is configured. */
  101. struct sam4_cfg {
  102. uint32_t unique_id[4];
  103. uint32_t slow_freq;
  104. uint32_t rc_freq;
  105. uint32_t mainosc_freq;
  106. uint32_t plla_freq;
  107. uint32_t mclk_freq;
  108. uint32_t cpu_freq;
  109. uint32_t fclk_freq;
  110. uint32_t pclk0_freq;
  111. uint32_t pclk1_freq;
  112. uint32_t pclk2_freq;
  113. #define SAM4_CHIPID_CIDR (0x400E0740)
  114. uint32_t CHIPID_CIDR;
  115. #define SAM4_CHIPID_EXID (0x400E0744)
  116. uint32_t CHIPID_EXID;
  117. #define SAM4_PMC_BASE (0x400E0400)
  118. #define SAM4_PMC_SCSR (SAM4_PMC_BASE + 0x0008)
  119. uint32_t PMC_SCSR;
  120. #define SAM4_PMC_PCSR (SAM4_PMC_BASE + 0x0018)
  121. uint32_t PMC_PCSR;
  122. #define SAM4_CKGR_UCKR (SAM4_PMC_BASE + 0x001c)
  123. uint32_t CKGR_UCKR;
  124. #define SAM4_CKGR_MOR (SAM4_PMC_BASE + 0x0020)
  125. uint32_t CKGR_MOR;
  126. #define SAM4_CKGR_MCFR (SAM4_PMC_BASE + 0x0024)
  127. uint32_t CKGR_MCFR;
  128. #define SAM4_CKGR_PLLAR (SAM4_PMC_BASE + 0x0028)
  129. uint32_t CKGR_PLLAR;
  130. #define SAM4_PMC_MCKR (SAM4_PMC_BASE + 0x0030)
  131. uint32_t PMC_MCKR;
  132. #define SAM4_PMC_PCK0 (SAM4_PMC_BASE + 0x0040)
  133. uint32_t PMC_PCK0;
  134. #define SAM4_PMC_PCK1 (SAM4_PMC_BASE + 0x0044)
  135. uint32_t PMC_PCK1;
  136. #define SAM4_PMC_PCK2 (SAM4_PMC_BASE + 0x0048)
  137. uint32_t PMC_PCK2;
  138. #define SAM4_PMC_SR (SAM4_PMC_BASE + 0x0068)
  139. uint32_t PMC_SR;
  140. #define SAM4_PMC_IMR (SAM4_PMC_BASE + 0x006c)
  141. uint32_t PMC_IMR;
  142. #define SAM4_PMC_FSMR (SAM4_PMC_BASE + 0x0070)
  143. uint32_t PMC_FSMR;
  144. #define SAM4_PMC_FSPR (SAM4_PMC_BASE + 0x0074)
  145. uint32_t PMC_FSPR;
  146. };
  147. struct sam4_bank_private {
  148. int probed;
  149. /* DANGER: THERE ARE DRAGONS HERE.. */
  150. /* NOTE: If you add more 'ghost' pointers */
  151. /* be aware that you must *manually* update */
  152. /* these pointers in the function sam4_GetDetails() */
  153. /* See the comment "Here there be dragons" */
  154. /* so we can find the chip we belong to */
  155. struct sam4_chip *pChip;
  156. /* so we can find the original bank pointer */
  157. struct flash_bank *pBank;
  158. unsigned bank_number;
  159. uint32_t controller_address;
  160. uint32_t base_address;
  161. uint32_t flash_wait_states;
  162. bool present;
  163. unsigned size_bytes;
  164. unsigned nsectors;
  165. unsigned sector_size;
  166. unsigned page_size;
  167. };
  168. struct sam4_chip_details {
  169. /* THERE ARE DRAGONS HERE.. */
  170. /* note: If you add pointers here */
  171. /* be careful about them as they */
  172. /* may need to be updated inside */
  173. /* the function: "sam4_GetDetails() */
  174. /* which copy/overwrites the */
  175. /* 'runtime' copy of this structure */
  176. uint32_t chipid_cidr;
  177. const char *name;
  178. unsigned n_gpnvms;
  179. #define SAM4_N_NVM_BITS 3
  180. unsigned gpnvm[SAM4_N_NVM_BITS];
  181. unsigned total_flash_size;
  182. unsigned total_sram_size;
  183. unsigned n_banks;
  184. #define SAM4_MAX_FLASH_BANKS 2
  185. /* these are "initialized" from the global const data */
  186. struct sam4_bank_private bank[SAM4_MAX_FLASH_BANKS];
  187. };
  188. struct sam4_chip {
  189. struct sam4_chip *next;
  190. int probed;
  191. /* this is "initialized" from the global const structure */
  192. struct sam4_chip_details details;
  193. struct target *target;
  194. struct sam4_cfg cfg;
  195. };
  196. struct sam4_reg_list {
  197. uint32_t address; size_t struct_offset; const char *name;
  198. void (*explain_func)(struct sam4_chip *pInfo);
  199. };
  200. static struct sam4_chip *all_sam4_chips;
  201. static struct sam4_chip *get_current_sam4(struct command_context *cmd_ctx)
  202. {
  203. struct target *t;
  204. static struct sam4_chip *p;
  205. t = get_current_target(cmd_ctx);
  206. if (!t) {
  207. command_print(cmd_ctx, "No current target?");
  208. return NULL;
  209. }
  210. p = all_sam4_chips;
  211. if (!p) {
  212. /* this should not happen */
  213. /* the command is not registered until the chip is created? */
  214. command_print(cmd_ctx, "No SAM4 chips exist?");
  215. return NULL;
  216. }
  217. while (p) {
  218. if (p->target == t)
  219. return p;
  220. p = p->next;
  221. }
  222. command_print(cmd_ctx, "Cannot find SAM4 chip?");
  223. return NULL;
  224. }
  225. /*The actual sector size of the SAM4S flash memory is 65536 bytes. 16 sectors for a 1024KB device*/
  226. /*The lockregions are 8KB per lock region, with a 1024KB device having 128 lock regions. */
  227. /*For the best results, nsectors are thus set to the amount of lock regions, and the sector_size*/
  228. /*set to the lock region size. Page erases are used to erase 8KB sections when programming*/
  229. /* these are used to *initialize* the "pChip->details" structure. */
  230. static const struct sam4_chip_details all_sam4_details[] = {
  231. /* Start at91sam4e* series */
  232. /*atsam4e16e - LQFP144/LFBGA144*/
  233. {
  234. .chipid_cidr = 0xA3CC0CE0,
  235. .name = "at91sam4e16e",
  236. .total_flash_size = 1024 * 1024,
  237. .total_sram_size = 128 * 1024,
  238. .n_gpnvms = 2,
  239. .n_banks = 1,
  240. {
  241. /* .bank[0] = {*/
  242. {
  243. .probed = 0,
  244. .pChip = NULL,
  245. .pBank = NULL,
  246. .bank_number = 0,
  247. .base_address = FLASH_BANK_BASE_S,
  248. .controller_address = 0x400e0a00,
  249. .flash_wait_states = 6, /* workaround silicon bug */
  250. .present = 1,
  251. .size_bytes = 1024 * 1024,
  252. .nsectors = 128,
  253. .sector_size = 8192,
  254. .page_size = 512,
  255. },
  256. /* .bank[1] = {*/
  257. {
  258. .present = 0,
  259. .probed = 0,
  260. .bank_number = 1,
  261. },
  262. },
  263. },
  264. /* Start at91sam4s* series */
  265. /*atsam4s16c - LQFP100/BGA100*/
  266. {
  267. .chipid_cidr = 0x28AC0CE0,
  268. .name = "at91sam4s16c",
  269. .total_flash_size = 1024 * 1024,
  270. .total_sram_size = 128 * 1024,
  271. .n_gpnvms = 2,
  272. .n_banks = 1,
  273. {
  274. /* .bank[0] = {*/
  275. {
  276. .probed = 0,
  277. .pChip = NULL,
  278. .pBank = NULL,
  279. .bank_number = 0,
  280. .base_address = FLASH_BANK_BASE_S,
  281. .controller_address = 0x400e0a00,
  282. .flash_wait_states = 6, /* workaround silicon bug */
  283. .present = 1,
  284. .size_bytes = 1024 * 1024,
  285. .nsectors = 128,
  286. .sector_size = 8192,
  287. .page_size = 512,
  288. },
  289. /* .bank[1] = {*/
  290. {
  291. .present = 0,
  292. .probed = 0,
  293. .bank_number = 1,
  294. },
  295. },
  296. },
  297. /*atsam4s16b - LQFP64/QFN64*/
  298. {
  299. .chipid_cidr = 0x289C0CE0,
  300. .name = "at91sam4s16b",
  301. .total_flash_size = 1024 * 1024,
  302. .total_sram_size = 128 * 1024,
  303. .n_gpnvms = 2,
  304. .n_banks = 1,
  305. {
  306. /* .bank[0] = {*/
  307. {
  308. .probed = 0,
  309. .pChip = NULL,
  310. .pBank = NULL,
  311. .bank_number = 0,
  312. .base_address = FLASH_BANK_BASE_S,
  313. .controller_address = 0x400e0a00,
  314. .flash_wait_states = 6, /* workaround silicon bug */
  315. .present = 1,
  316. .size_bytes = 1024 * 1024,
  317. .nsectors = 128,
  318. .sector_size = 8192,
  319. .page_size = 512,
  320. },
  321. /* .bank[1] = {*/
  322. {
  323. .present = 0,
  324. .probed = 0,
  325. .bank_number = 1,
  326. },
  327. },
  328. },
  329. /*atsam4sa16b - LQFP64/QFN64*/
  330. {
  331. .chipid_cidr = 0x28970CE0,
  332. .name = "at91sam4sa16b",
  333. .total_flash_size = 1024 * 1024,
  334. .total_sram_size = 160 * 1024,
  335. .n_gpnvms = 2,
  336. .n_banks = 1,
  337. {
  338. /* .bank[0] = {*/
  339. {
  340. .probed = 0,
  341. .pChip = NULL,
  342. .pBank = NULL,
  343. .bank_number = 0,
  344. .base_address = FLASH_BANK_BASE_S,
  345. .controller_address = 0x400e0a00,
  346. .flash_wait_states = 6, /* workaround silicon bug */
  347. .present = 1,
  348. .size_bytes = 1024 * 1024,
  349. .nsectors = 128,
  350. .sector_size = 8192,
  351. .page_size = 512,
  352. },
  353. /* .bank[1] = {*/
  354. {
  355. .present = 0,
  356. .probed = 0,
  357. .bank_number = 1,
  358. },
  359. },
  360. },
  361. /*atsam4s16a - LQFP48/QFN48*/
  362. {
  363. .chipid_cidr = 0x288C0CE0,
  364. .name = "at91sam4s16a",
  365. .total_flash_size = 1024 * 1024,
  366. .total_sram_size = 128 * 1024,
  367. .n_gpnvms = 2,
  368. .n_banks = 1,
  369. {
  370. /* .bank[0] = {*/
  371. {
  372. .probed = 0,
  373. .pChip = NULL,
  374. .pBank = NULL,
  375. .bank_number = 0,
  376. .base_address = FLASH_BANK_BASE_S,
  377. .controller_address = 0x400e0a00,
  378. .flash_wait_states = 6, /* workaround silicon bug */
  379. .present = 1,
  380. .size_bytes = 1024 * 1024,
  381. .nsectors = 128,
  382. .sector_size = 8192,
  383. .page_size = 512,
  384. },
  385. /* .bank[1] = {*/
  386. {
  387. .present = 0,
  388. .probed = 0,
  389. .bank_number = 1,
  390. },
  391. },
  392. },
  393. /*atsam4s8c - LQFP100/BGA100*/
  394. {
  395. .chipid_cidr = 0x28AC0AE0,
  396. .name = "at91sam4s8c",
  397. .total_flash_size = 512 * 1024,
  398. .total_sram_size = 128 * 1024,
  399. .n_gpnvms = 2,
  400. .n_banks = 1,
  401. {
  402. /* .bank[0] = {*/
  403. {
  404. .probed = 0,
  405. .pChip = NULL,
  406. .pBank = NULL,
  407. .bank_number = 0,
  408. .base_address = FLASH_BANK_BASE_S,
  409. .controller_address = 0x400e0a00,
  410. .flash_wait_states = 6, /* workaround silicon bug */
  411. .present = 1,
  412. .size_bytes = 512 * 1024,
  413. .nsectors = 64,
  414. .sector_size = 8192,
  415. .page_size = 512,
  416. },
  417. /* .bank[1] = {*/
  418. {
  419. .present = 0,
  420. .probed = 0,
  421. .bank_number = 1,
  422. },
  423. },
  424. },
  425. /*atsam4s8b - LQFP64/BGA64*/
  426. {
  427. .chipid_cidr = 0x289C0AE0,
  428. .name = "at91sam4s8b",
  429. .total_flash_size = 512 * 1024,
  430. .total_sram_size = 128 * 1024,
  431. .n_gpnvms = 2,
  432. .n_banks = 1,
  433. {
  434. /* .bank[0] = {*/
  435. {
  436. .probed = 0,
  437. .pChip = NULL,
  438. .pBank = NULL,
  439. .bank_number = 0,
  440. .base_address = FLASH_BANK_BASE_S,
  441. .controller_address = 0x400e0a00,
  442. .flash_wait_states = 6, /* workaround silicon bug */
  443. .present = 1,
  444. .size_bytes = 512 * 1024,
  445. .nsectors = 64,
  446. .sector_size = 8192,
  447. .page_size = 512,
  448. },
  449. /* .bank[1] = {*/
  450. {
  451. .present = 0,
  452. .probed = 0,
  453. .bank_number = 1,
  454. },
  455. },
  456. },
  457. /*atsam4s8a - LQFP48/BGA48*/
  458. {
  459. .chipid_cidr = 0x288C0AE0,
  460. .name = "at91sam4s8a",
  461. .total_flash_size = 512 * 1024,
  462. .total_sram_size = 128 * 1024,
  463. .n_gpnvms = 2,
  464. .n_banks = 1,
  465. {
  466. /* .bank[0] = {*/
  467. {
  468. .probed = 0,
  469. .pChip = NULL,
  470. .pBank = NULL,
  471. .bank_number = 0,
  472. .base_address = FLASH_BANK_BASE_S,
  473. .controller_address = 0x400e0a00,
  474. .flash_wait_states = 6, /* workaround silicon bug */
  475. .present = 1,
  476. .size_bytes = 512 * 1024,
  477. .nsectors = 64,
  478. .sector_size = 8192,
  479. .page_size = 512,
  480. },
  481. /* .bank[1] = {*/
  482. {
  483. .present = 0,
  484. .probed = 0,
  485. .bank_number = 1,
  486. },
  487. },
  488. },
  489. /*atsam4s4a - LQFP48/BGA48*/
  490. {
  491. .chipid_cidr = 0x288b09e0,
  492. .name = "at91sam4s4a",
  493. .total_flash_size = 256 * 1024,
  494. .total_sram_size = 64 * 1024,
  495. .n_gpnvms = 2,
  496. .n_banks = 1,
  497. {
  498. /* .bank[0] = {*/
  499. {
  500. .probed = 0,
  501. .pChip = NULL,
  502. .pBank = NULL,
  503. .bank_number = 0,
  504. .base_address = FLASH_BANK_BASE_S,
  505. .controller_address = 0x400e0a00,
  506. .flash_wait_states = 6, /* workaround silicon bug */
  507. .present = 1,
  508. .size_bytes = 256 * 1024,
  509. .nsectors = 32,
  510. .sector_size = 8192,
  511. .page_size = 512,
  512. },
  513. /* .bank[1] = {*/
  514. {
  515. .present = 0,
  516. .probed = 0,
  517. .bank_number = 1,
  518. },
  519. },
  520. },
  521. /*at91sam4sd32c*/
  522. {
  523. .chipid_cidr = 0x29a70ee0,
  524. .name = "at91sam4sd32c",
  525. .total_flash_size = 2048 * 1024,
  526. .total_sram_size = 160 * 1024,
  527. .n_gpnvms = 3,
  528. .n_banks = 2,
  529. /* .bank[0] = { */
  530. {
  531. {
  532. .probed = 0,
  533. .pChip = NULL,
  534. .pBank = NULL,
  535. .bank_number = 0,
  536. .base_address = FLASH_BANK0_BASE_SD,
  537. .controller_address = 0x400e0a00,
  538. .flash_wait_states = 6, /* workaround silicon bug */
  539. .present = 1,
  540. .size_bytes = 1024 * 1024,
  541. .nsectors = 128,
  542. .sector_size = 8192,
  543. .page_size = 512,
  544. },
  545. /* .bank[1] = { */
  546. {
  547. .probed = 0,
  548. .pChip = NULL,
  549. .pBank = NULL,
  550. .bank_number = 1,
  551. .base_address = FLASH_BANK1_BASE_2048K_SD,
  552. .controller_address = 0x400e0c00,
  553. .flash_wait_states = 6, /* workaround silicon bug */
  554. .present = 1,
  555. .size_bytes = 1024 * 1024,
  556. .nsectors = 128,
  557. .sector_size = 8192,
  558. .page_size = 512,
  559. },
  560. },
  561. },
  562. /*at91sam4sd16c*/
  563. {
  564. .chipid_cidr = 0x29a70ce0,
  565. .name = "at91sam4sd16c",
  566. .total_flash_size = 1024 * 1024,
  567. .total_sram_size = 160 * 1024,
  568. .n_gpnvms = 3,
  569. .n_banks = 2,
  570. /* .bank[0] = { */
  571. {
  572. {
  573. .probed = 0,
  574. .pChip = NULL,
  575. .pBank = NULL,
  576. .bank_number = 0,
  577. .base_address = FLASH_BANK0_BASE_SD,
  578. .controller_address = 0x400e0a00,
  579. .flash_wait_states = 6, /* workaround silicon bug */
  580. .present = 1,
  581. .size_bytes = 512 * 1024,
  582. .nsectors = 64,
  583. .sector_size = 8192,
  584. .page_size = 512,
  585. },
  586. /* .bank[1] = { */
  587. {
  588. .probed = 0,
  589. .pChip = NULL,
  590. .pBank = NULL,
  591. .bank_number = 1,
  592. .base_address = FLASH_BANK1_BASE_1024K_SD,
  593. .controller_address = 0x400e0c00,
  594. .flash_wait_states = 6, /* workaround silicon bug */
  595. .present = 1,
  596. .size_bytes = 512 * 1024,
  597. .nsectors = 64,
  598. .sector_size = 8192,
  599. .page_size = 512,
  600. },
  601. },
  602. },
  603. /*at91sam4sa16c*/
  604. {
  605. .chipid_cidr = 0x28a70ce0,
  606. .name = "at91sam4sa16c",
  607. .total_flash_size = 1024 * 1024,
  608. .total_sram_size = 160 * 1024,
  609. .n_gpnvms = 3,
  610. .n_banks = 2,
  611. /* .bank[0] = { */
  612. {
  613. {
  614. .probed = 0,
  615. .pChip = NULL,
  616. .pBank = NULL,
  617. .bank_number = 0,
  618. .base_address = FLASH_BANK0_BASE_SD,
  619. .controller_address = 0x400e0a00,
  620. .flash_wait_states = 6, /* workaround silicon bug */
  621. .present = 1,
  622. .size_bytes = 512 * 1024,
  623. .nsectors = 64,
  624. .sector_size = 8192,
  625. .page_size = 512,
  626. },
  627. /* .bank[1] = { */
  628. {
  629. .probed = 0,
  630. .pChip = NULL,
  631. .pBank = NULL,
  632. .bank_number = 1,
  633. .base_address = FLASH_BANK1_BASE_1024K_SD,
  634. .controller_address = 0x400e0c00,
  635. .flash_wait_states = 6, /* workaround silicon bug */
  636. .present = 1,
  637. .size_bytes = 512 * 1024,
  638. .nsectors = 64,
  639. .sector_size = 8192,
  640. .page_size = 512,
  641. },
  642. },
  643. },
  644. /* at91samg53n19 */
  645. {
  646. .chipid_cidr = 0x247e0ae0,
  647. .name = "at91samg53n19",
  648. .total_flash_size = 512 * 1024,
  649. .total_sram_size = 96 * 1024,
  650. .n_gpnvms = 2,
  651. .n_banks = 1,
  652. /* .bank[0] = {*/
  653. {
  654. {
  655. .probed = 0,
  656. .pChip = NULL,
  657. .pBank = NULL,
  658. .bank_number = 0,
  659. .base_address = FLASH_BANK_BASE_S,
  660. .controller_address = 0x400e0a00,
  661. .flash_wait_states = 6, /* workaround silicon bug */
  662. .present = 1,
  663. .size_bytes = 512 * 1024,
  664. .nsectors = 64,
  665. .sector_size = 8192,
  666. .page_size = 512,
  667. },
  668. /* .bank[1] = {*/
  669. {
  670. .present = 0,
  671. .probed = 0,
  672. .bank_number = 1,
  673. },
  674. }
  675. },
  676. /* terminate */
  677. {
  678. .chipid_cidr = 0,
  679. .name = NULL,
  680. }
  681. };
  682. /* Globals above */
  683. /***********************************************************************
  684. **********************************************************************
  685. **********************************************************************
  686. **********************************************************************
  687. **********************************************************************
  688. **********************************************************************/
  689. /* *ATMEL* style code - from the SAM4 driver code */
  690. /**
  691. * Get the current status of the EEFC and
  692. * the value of some status bits (LOCKE, PROGE).
  693. * @param pPrivate - info about the bank
  694. * @param v - result goes here
  695. */
  696. static int EFC_GetStatus(struct sam4_bank_private *pPrivate, uint32_t *v)
  697. {
  698. int r;
  699. r = target_read_u32(pPrivate->pChip->target,
  700. pPrivate->controller_address + offset_EFC_FSR,
  701. v);
  702. LOG_DEBUG("Status: 0x%08x (lockerror: %d, cmderror: %d, ready: %d)",
  703. (unsigned int)(*v),
  704. ((unsigned int)((*v >> 2) & 1)),
  705. ((unsigned int)((*v >> 1) & 1)),
  706. ((unsigned int)((*v >> 0) & 1)));
  707. return r;
  708. }
  709. /**
  710. * Get the result of the last executed command.
  711. * @param pPrivate - info about the bank
  712. * @param v - result goes here
  713. */
  714. static int EFC_GetResult(struct sam4_bank_private *pPrivate, uint32_t *v)
  715. {
  716. int r;
  717. uint32_t rv;
  718. r = target_read_u32(pPrivate->pChip->target,
  719. pPrivate->controller_address + offset_EFC_FRR,
  720. &rv);
  721. if (v)
  722. *v = rv;
  723. LOG_DEBUG("Result: 0x%08x", ((unsigned int)(rv)));
  724. return r;
  725. }
  726. static int EFC_StartCommand(struct sam4_bank_private *pPrivate,
  727. unsigned command, unsigned argument)
  728. {
  729. uint32_t n, v;
  730. int r;
  731. int retry;
  732. retry = 0;
  733. do_retry:
  734. /* Check command & argument */
  735. switch (command) {
  736. case AT91C_EFC_FCMD_WP:
  737. case AT91C_EFC_FCMD_WPL:
  738. case AT91C_EFC_FCMD_EWP:
  739. case AT91C_EFC_FCMD_EWPL:
  740. /* case AT91C_EFC_FCMD_EPL: */
  741. case AT91C_EFC_FCMD_EPA:
  742. case AT91C_EFC_FCMD_SLB:
  743. case AT91C_EFC_FCMD_CLB:
  744. n = (pPrivate->size_bytes / pPrivate->page_size);
  745. if (argument >= n)
  746. LOG_ERROR("*BUG*: Embedded flash has only %u pages", (unsigned)(n));
  747. break;
  748. case AT91C_EFC_FCMD_SFB:
  749. case AT91C_EFC_FCMD_CFB:
  750. if (argument >= pPrivate->pChip->details.n_gpnvms) {
  751. LOG_ERROR("*BUG*: Embedded flash has only %d GPNVMs",
  752. pPrivate->pChip->details.n_gpnvms);
  753. }
  754. break;
  755. case AT91C_EFC_FCMD_GETD:
  756. case AT91C_EFC_FCMD_EA:
  757. case AT91C_EFC_FCMD_GLB:
  758. case AT91C_EFC_FCMD_GFB:
  759. case AT91C_EFC_FCMD_STUI:
  760. case AT91C_EFC_FCMD_SPUI:
  761. if (argument != 0)
  762. LOG_ERROR("Argument is meaningless for cmd: %d", command);
  763. break;
  764. default:
  765. LOG_ERROR("Unknown command %d", command);
  766. break;
  767. }
  768. if (command == AT91C_EFC_FCMD_SPUI) {
  769. /* this is a very special situation. */
  770. /* Situation (1) - error/retry - see below */
  771. /* And we are being called recursively */
  772. /* Situation (2) - normal, finished reading unique id */
  773. } else {
  774. /* it should be "ready" */
  775. EFC_GetStatus(pPrivate, &v);
  776. if (v & 1) {
  777. /* then it is ready */
  778. /* we go on */
  779. } else {
  780. if (retry) {
  781. /* we have done this before */
  782. /* the controller is not responding. */
  783. LOG_ERROR("flash controller(%d) is not ready! Error",
  784. pPrivate->bank_number);
  785. return ERROR_FAIL;
  786. } else {
  787. retry++;
  788. LOG_ERROR("Flash controller(%d) is not ready, attempting reset",
  789. pPrivate->bank_number);
  790. /* we do that by issuing the *STOP* command */
  791. EFC_StartCommand(pPrivate, AT91C_EFC_FCMD_SPUI, 0);
  792. /* above is recursive, and further recursion is blocked by */
  793. /* if (command == AT91C_EFC_FCMD_SPUI) above */
  794. goto do_retry;
  795. }
  796. }
  797. }
  798. v = (0x5A << 24) | (argument << 8) | command;
  799. LOG_DEBUG("Command: 0x%08x", ((unsigned int)(v)));
  800. r = target_write_u32(pPrivate->pBank->target,
  801. pPrivate->controller_address + offset_EFC_FCR, v);
  802. if (r != ERROR_OK)
  803. LOG_DEBUG("Error Write failed");
  804. return r;
  805. }
  806. /**
  807. * Performs the given command and wait until its completion (or an error).
  808. * @param pPrivate - info about the bank
  809. * @param command - Command to perform.
  810. * @param argument - Optional command argument.
  811. * @param status - put command status bits here
  812. */
  813. static int EFC_PerformCommand(struct sam4_bank_private *pPrivate,
  814. unsigned command,
  815. unsigned argument,
  816. uint32_t *status)
  817. {
  818. int r;
  819. uint32_t v;
  820. long long ms_now, ms_end;
  821. /* default */
  822. if (status)
  823. *status = 0;
  824. r = EFC_StartCommand(pPrivate, command, argument);
  825. if (r != ERROR_OK)
  826. return r;
  827. ms_end = 10000 + timeval_ms();
  828. do {
  829. r = EFC_GetStatus(pPrivate, &v);
  830. if (r != ERROR_OK)
  831. return r;
  832. ms_now = timeval_ms();
  833. if (ms_now > ms_end) {
  834. /* error */
  835. LOG_ERROR("Command timeout");
  836. return ERROR_FAIL;
  837. }
  838. } while ((v & 1) == 0);
  839. /* error bits.. */
  840. if (status)
  841. *status = (v & 0x6);
  842. return ERROR_OK;
  843. }
  844. /**
  845. * Read the unique ID.
  846. * @param pPrivate - info about the bank
  847. * The unique ID is stored in the 'pPrivate' structure.
  848. */
  849. static int FLASHD_ReadUniqueID(struct sam4_bank_private *pPrivate)
  850. {
  851. int r;
  852. uint32_t v;
  853. int x;
  854. /* assume 0 */
  855. pPrivate->pChip->cfg.unique_id[0] = 0;
  856. pPrivate->pChip->cfg.unique_id[1] = 0;
  857. pPrivate->pChip->cfg.unique_id[2] = 0;
  858. pPrivate->pChip->cfg.unique_id[3] = 0;
  859. LOG_DEBUG("Begin");
  860. r = EFC_StartCommand(pPrivate, AT91C_EFC_FCMD_STUI, 0);
  861. if (r < 0)
  862. return r;
  863. for (x = 0; x < 4; x++) {
  864. r = target_read_u32(pPrivate->pChip->target,
  865. pPrivate->pBank->base + (x * 4),
  866. &v);
  867. if (r < 0)
  868. return r;
  869. pPrivate->pChip->cfg.unique_id[x] = v;
  870. }
  871. r = EFC_PerformCommand(pPrivate, AT91C_EFC_FCMD_SPUI, 0, NULL);
  872. LOG_DEBUG("End: R=%d, id = 0x%08x, 0x%08x, 0x%08x, 0x%08x",
  873. r,
  874. (unsigned int)(pPrivate->pChip->cfg.unique_id[0]),
  875. (unsigned int)(pPrivate->pChip->cfg.unique_id[1]),
  876. (unsigned int)(pPrivate->pChip->cfg.unique_id[2]),
  877. (unsigned int)(pPrivate->pChip->cfg.unique_id[3]));
  878. return r;
  879. }
  880. /**
  881. * Erases the entire flash.
  882. * @param pPrivate - the info about the bank.
  883. */
  884. static int FLASHD_EraseEntireBank(struct sam4_bank_private *pPrivate)
  885. {
  886. LOG_DEBUG("Here");
  887. return EFC_PerformCommand(pPrivate, AT91C_EFC_FCMD_EA, 0, NULL);
  888. }
  889. /**
  890. * Erases the entire flash.
  891. * @param pPrivate - the info about the bank.
  892. */
  893. static int FLASHD_ErasePages(struct sam4_bank_private *pPrivate,
  894. int firstPage,
  895. int numPages,
  896. uint32_t *status)
  897. {
  898. LOG_DEBUG("Here");
  899. uint8_t erasePages;
  900. switch (numPages) {
  901. case 4:
  902. erasePages = 0x00;
  903. break;
  904. case 8:
  905. erasePages = 0x01;
  906. break;
  907. case 16:
  908. erasePages = 0x02;
  909. break;
  910. case 32:
  911. erasePages = 0x03;
  912. break;
  913. default:
  914. erasePages = 0x00;
  915. break;
  916. }
  917. /* AT91C_EFC_FCMD_EPA
  918. * According to the datasheet FARG[15:2] defines the page from which
  919. * the erase will start.This page must be modulo 4, 8, 16 or 32
  920. * according to the number of pages to erase. FARG[1:0] defines the
  921. * number of pages to be erased. Previously (firstpage << 2) was used
  922. * to conform to this, seems it should not be shifted...
  923. */
  924. return EFC_PerformCommand(pPrivate,
  925. /* send Erase Page */
  926. AT91C_EFC_FCMD_EPA,
  927. (firstPage) | erasePages,
  928. status);
  929. }
  930. /**
  931. * Gets current GPNVM state.
  932. * @param pPrivate - info about the bank.
  933. * @param gpnvm - GPNVM bit index.
  934. * @param puthere - result stored here.
  935. */
  936. /* ------------------------------------------------------------------------------ */
  937. static int FLASHD_GetGPNVM(struct sam4_bank_private *pPrivate, unsigned gpnvm, unsigned *puthere)
  938. {
  939. uint32_t v;
  940. int r;
  941. LOG_DEBUG("Here");
  942. if (pPrivate->bank_number != 0) {
  943. LOG_ERROR("GPNVM only works with Bank0");
  944. return ERROR_FAIL;
  945. }
  946. if (gpnvm >= pPrivate->pChip->details.n_gpnvms) {
  947. LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
  948. gpnvm, pPrivate->pChip->details.n_gpnvms);
  949. return ERROR_FAIL;
  950. }
  951. /* Get GPNVMs status */
  952. r = EFC_PerformCommand(pPrivate, AT91C_EFC_FCMD_GFB, 0, NULL);
  953. if (r != ERROR_OK) {
  954. LOG_ERROR("Failed");
  955. return r;
  956. }
  957. r = EFC_GetResult(pPrivate, &v);
  958. if (puthere) {
  959. /* Check if GPNVM is set */
  960. /* get the bit and make it a 0/1 */
  961. *puthere = (v >> gpnvm) & 1;
  962. }
  963. return r;
  964. }
  965. /**
  966. * Clears the selected GPNVM bit.
  967. * @param pPrivate info about the bank
  968. * @param gpnvm GPNVM index.
  969. * @returns 0 if successful; otherwise returns an error code.
  970. */
  971. static int FLASHD_ClrGPNVM(struct sam4_bank_private *pPrivate, unsigned gpnvm)
  972. {
  973. int r;
  974. unsigned v;
  975. LOG_DEBUG("Here");
  976. if (pPrivate->bank_number != 0) {
  977. LOG_ERROR("GPNVM only works with Bank0");
  978. return ERROR_FAIL;
  979. }
  980. if (gpnvm >= pPrivate->pChip->details.n_gpnvms) {
  981. LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
  982. gpnvm, pPrivate->pChip->details.n_gpnvms);
  983. return ERROR_FAIL;
  984. }
  985. r = FLASHD_GetGPNVM(pPrivate, gpnvm, &v);
  986. if (r != ERROR_OK) {
  987. LOG_DEBUG("Failed: %d", r);
  988. return r;
  989. }
  990. r = EFC_PerformCommand(pPrivate, AT91C_EFC_FCMD_CFB, gpnvm, NULL);
  991. LOG_DEBUG("End: %d", r);
  992. return r;
  993. }
  994. /**
  995. * Sets the selected GPNVM bit.
  996. * @param pPrivate info about the bank
  997. * @param gpnvm GPNVM index.
  998. */
  999. static int FLASHD_SetGPNVM(struct sam4_bank_private *pPrivate, unsigned gpnvm)
  1000. {
  1001. int r;
  1002. unsigned v;
  1003. if (pPrivate->bank_number != 0) {
  1004. LOG_ERROR("GPNVM only works with Bank0");
  1005. return ERROR_FAIL;
  1006. }
  1007. if (gpnvm >= pPrivate->pChip->details.n_gpnvms) {
  1008. LOG_ERROR("Invalid GPNVM %d, max: %d, ignored",
  1009. gpnvm, pPrivate->pChip->details.n_gpnvms);
  1010. return ERROR_FAIL;
  1011. }
  1012. r = FLASHD_GetGPNVM(pPrivate, gpnvm, &v);
  1013. if (r != ERROR_OK)
  1014. return r;
  1015. if (v) {
  1016. /* already set */
  1017. r = ERROR_OK;
  1018. } else {
  1019. /* set it */
  1020. r = EFC_PerformCommand(pPrivate, AT91C_EFC_FCMD_SFB, gpnvm, NULL);
  1021. }
  1022. return r;
  1023. }
  1024. /**
  1025. * Returns a bit field (at most 64) of locked regions within a page.
  1026. * @param pPrivate info about the bank
  1027. * @param v where to store locked bits
  1028. */
  1029. static int FLASHD_GetLockBits(struct sam4_bank_private *pPrivate, uint32_t *v)
  1030. {
  1031. int r;
  1032. LOG_DEBUG("Here");
  1033. r = EFC_PerformCommand(pPrivate, AT91C_EFC_FCMD_GLB, 0, NULL);
  1034. if (r == ERROR_OK) {
  1035. EFC_GetResult(pPrivate, v);
  1036. EFC_GetResult(pPrivate, v);
  1037. EFC_GetResult(pPrivate, v);
  1038. r = EFC_GetResult(pPrivate, v);
  1039. }
  1040. LOG_DEBUG("End: %d", r);
  1041. return r;
  1042. }
  1043. /**
  1044. * Unlocks all the regions in the given address range.
  1045. * @param pPrivate info about the bank
  1046. * @param start_sector first sector to unlock
  1047. * @param end_sector last (inclusive) to unlock
  1048. */
  1049. static int FLASHD_Unlock(struct sam4_bank_private *pPrivate,
  1050. unsigned start_sector,
  1051. unsigned end_sector)
  1052. {
  1053. int r;
  1054. uint32_t status;
  1055. uint32_t pg;
  1056. uint32_t pages_per_sector;
  1057. pages_per_sector = pPrivate->sector_size / pPrivate->page_size;
  1058. /* Unlock all pages */
  1059. while (start_sector <= end_sector) {
  1060. pg = start_sector * pages_per_sector;
  1061. r = EFC_PerformCommand(pPrivate, AT91C_EFC_FCMD_CLB, pg, &status);
  1062. if (r != ERROR_OK)
  1063. return r;
  1064. start_sector++;
  1065. }
  1066. return ERROR_OK;
  1067. }
  1068. /**
  1069. * Locks regions
  1070. * @param pPrivate - info about the bank
  1071. * @param start_sector - first sector to lock
  1072. * @param end_sector - last sector (inclusive) to lock
  1073. */
  1074. static int FLASHD_Lock(struct sam4_bank_private *pPrivate,
  1075. unsigned start_sector,
  1076. unsigned end_sector)
  1077. {
  1078. uint32_t status;
  1079. uint32_t pg;
  1080. uint32_t pages_per_sector;
  1081. int r;
  1082. pages_per_sector = pPrivate->sector_size / pPrivate->page_size;
  1083. /* Lock all pages */
  1084. while (start_sector <= end_sector) {
  1085. pg = start_sector * pages_per_sector;
  1086. r = EFC_PerformCommand(pPrivate, AT91C_EFC_FCMD_SLB, pg, &status);
  1087. if (r != ERROR_OK)
  1088. return r;
  1089. start_sector++;
  1090. }
  1091. return ERROR_OK;
  1092. }
  1093. /****** END SAM4 CODE ********/
  1094. /* begin helpful debug code */
  1095. /* print the fieldname, the field value, in dec & hex, and return field value */
  1096. static uint32_t sam4_reg_fieldname(struct sam4_chip *pChip,
  1097. const char *regname,
  1098. uint32_t value,
  1099. unsigned shift,
  1100. unsigned width)
  1101. {
  1102. uint32_t v;
  1103. int hwidth, dwidth;
  1104. /* extract the field */
  1105. v = value >> shift;
  1106. v = v & ((1 << width)-1);
  1107. if (width <= 16) {
  1108. hwidth = 4;
  1109. dwidth = 5;
  1110. } else {
  1111. hwidth = 8;
  1112. dwidth = 12;
  1113. }
  1114. /* show the basics */
  1115. LOG_USER_N("\t%*s: %*" PRId32 " [0x%0*" PRIx32 "] ",
  1116. REG_NAME_WIDTH, regname,
  1117. dwidth, v,
  1118. hwidth, v);
  1119. return v;
  1120. }
  1121. static const char _unknown[] = "unknown";
  1122. static const char *const eproc_names[] = {
  1123. _unknown, /* 0 */
  1124. "arm946es", /* 1 */
  1125. "arm7tdmi", /* 2 */
  1126. "cortex-m3", /* 3 */
  1127. "arm920t", /* 4 */
  1128. "arm926ejs", /* 5 */
  1129. "cortex-a5", /* 6 */
  1130. "cortex-m4", /* 7 */
  1131. _unknown, /* 8 */
  1132. _unknown, /* 9 */
  1133. _unknown, /* 10 */
  1134. _unknown, /* 11 */
  1135. _unknown, /* 12 */
  1136. _unknown, /* 13 */
  1137. _unknown, /* 14 */
  1138. _unknown, /* 15 */
  1139. };
  1140. #define nvpsize2 nvpsize /* these two tables are identical */
  1141. static const char *const nvpsize[] = {
  1142. "none", /* 0 */
  1143. "8K bytes", /* 1 */
  1144. "16K bytes", /* 2 */
  1145. "32K bytes", /* 3 */
  1146. _unknown, /* 4 */
  1147. "64K bytes", /* 5 */
  1148. _unknown, /* 6 */
  1149. "128K bytes", /* 7 */
  1150. _unknown, /* 8 */
  1151. "256K bytes", /* 9 */
  1152. "512K bytes", /* 10 */
  1153. _unknown, /* 11 */
  1154. "1024K bytes", /* 12 */
  1155. _unknown, /* 13 */
  1156. "2048K bytes", /* 14 */
  1157. _unknown, /* 15 */
  1158. };
  1159. static const char *const sramsize[] = {
  1160. "48K Bytes", /* 0 */
  1161. "1K Bytes", /* 1 */
  1162. "2K Bytes", /* 2 */
  1163. "6K Bytes", /* 3 */
  1164. "112K Bytes", /* 4 */
  1165. "4K Bytes", /* 5 */
  1166. "80K Bytes", /* 6 */
  1167. "160K Bytes", /* 7 */
  1168. "8K Bytes", /* 8 */
  1169. "16K Bytes", /* 9 */
  1170. "32K Bytes", /* 10 */
  1171. "64K Bytes", /* 11 */
  1172. "128K Bytes", /* 12 */
  1173. "256K Bytes", /* 13 */
  1174. "96K Bytes", /* 14 */
  1175. "512K Bytes", /* 15 */
  1176. };
  1177. static const struct archnames { unsigned value; const char *name; } archnames[] = {
  1178. { 0x19, "AT91SAM9xx Series" },
  1179. { 0x29, "AT91SAM9XExx Series" },
  1180. { 0x34, "AT91x34 Series" },
  1181. { 0x37, "CAP7 Series" },
  1182. { 0x39, "CAP9 Series" },
  1183. { 0x3B, "CAP11 Series" },
  1184. { 0x3C, "ATSAM4E" },
  1185. { 0x40, "AT91x40 Series" },
  1186. { 0x42, "AT91x42 Series" },
  1187. { 0x43, "SAMG51 Series"
  1188. },
  1189. { 0x47, "SAMG53 Series"
  1190. },
  1191. { 0x55, "AT91x55 Series" },
  1192. { 0x60, "AT91SAM7Axx Series" },
  1193. { 0x61, "AT91SAM7AQxx Series" },
  1194. { 0x63, "AT91x63 Series" },
  1195. { 0x70, "AT91SAM7Sxx Series" },
  1196. { 0x71, "AT91SAM7XCxx Series" },
  1197. { 0x72, "AT91SAM7SExx Series" },
  1198. { 0x73, "AT91SAM7Lxx Series" },
  1199. { 0x75, "AT91SAM7Xxx Series" },
  1200. { 0x76, "AT91SAM7SLxx Series" },
  1201. { 0x80, "ATSAM3UxC Series (100-pin version)" },
  1202. { 0x81, "ATSAM3UxE Series (144-pin version)" },
  1203. { 0x83, "ATSAM3A/SAM4A xC Series (100-pin version)"},
  1204. { 0x84, "ATSAM3X/SAM4X xC Series (100-pin version)"},
  1205. { 0x85, "ATSAM3X/SAM4X xE Series (144-pin version)"},
  1206. { 0x86, "ATSAM3X/SAM4X xG Series (208/217-pin version)" },
  1207. { 0x88, "ATSAM3S/SAM4S xA Series (48-pin version)" },
  1208. { 0x89, "ATSAM3S/SAM4S xB Series (64-pin version)" },
  1209. { 0x8A, "ATSAM3S/SAM4S xC Series (100-pin version)"},
  1210. { 0x92, "AT91x92 Series" },
  1211. { 0x93, "ATSAM3NxA Series (48-pin version)" },
  1212. { 0x94, "ATSAM3NxB Series (64-pin version)" },
  1213. { 0x95, "ATSAM3NxC Series (100-pin version)" },
  1214. { 0x98, "ATSAM3SDxA Series (48-pin version)" },
  1215. { 0x99, "ATSAM3SDxB Series (64-pin version)" },
  1216. { 0x9A, "ATSAM3SDxC Series (100-pin version)" },
  1217. { 0xA5, "ATSAM5A" },
  1218. { 0xF0, "AT75Cxx Series" },
  1219. { -1, NULL },
  1220. };
  1221. static const char *const nvptype[] = {
  1222. "rom", /* 0 */
  1223. "romless or onchip flash", /* 1 */
  1224. "embedded flash memory",/* 2 */
  1225. "rom(nvpsiz) + embedded flash (nvpsiz2)", /* 3 */
  1226. "sram emulating flash", /* 4 */
  1227. _unknown, /* 5 */
  1228. _unknown, /* 6 */
  1229. _unknown, /* 7 */
  1230. };
  1231. static const char *_yes_or_no(uint32_t v)
  1232. {
  1233. if (v)
  1234. return "YES";
  1235. else
  1236. return "NO";
  1237. }
  1238. static const char *const _rc_freq[] = {
  1239. "4 MHz", "8 MHz", "12 MHz", "reserved"
  1240. };
  1241. static void sam4_explain_ckgr_mor(struct sam4_chip *pChip)
  1242. {
  1243. uint32_t v;
  1244. uint32_t rcen;
  1245. v = sam4_reg_fieldname(pChip, "MOSCXTEN", pChip->cfg.CKGR_MOR, 0, 1);
  1246. LOG_USER("(main xtal enabled: %s)", _yes_or_no(v));
  1247. v = sam4_reg_fieldname(pChip, "MOSCXTBY", pChip->cfg.CKGR_MOR, 1, 1);
  1248. LOG_USER("(main osc bypass: %s)", _yes_or_no(v));
  1249. rcen = sam4_reg_fieldname(pChip, "MOSCRCEN", pChip->cfg.CKGR_MOR, 3, 1);
  1250. LOG_USER("(onchip RC-OSC enabled: %s)", _yes_or_no(rcen));
  1251. v = sam4_reg_fieldname(pChip, "MOSCRCF", pChip->cfg.CKGR_MOR, 4, 3);
  1252. LOG_USER("(onchip RC-OSC freq: %s)", _rc_freq[v]);
  1253. pChip->cfg.rc_freq = 0;
  1254. if (rcen) {
  1255. switch (v) {
  1256. default:
  1257. pChip->cfg.rc_freq = 0;
  1258. break;
  1259. case 0:
  1260. pChip->cfg.rc_freq = 4 * 1000 * 1000;
  1261. break;
  1262. case 1:
  1263. pChip->cfg.rc_freq = 8 * 1000 * 1000;
  1264. break;
  1265. case 2:
  1266. pChip->cfg.rc_freq = 12 * 1000 * 1000;
  1267. break;
  1268. }
  1269. }
  1270. v = sam4_reg_fieldname(pChip, "MOSCXTST", pChip->cfg.CKGR_MOR, 8, 8);
  1271. LOG_USER("(startup clks, time= %f uSecs)",
  1272. ((float)(v * 1000000)) / ((float)(pChip->cfg.slow_freq)));
  1273. v = sam4_reg_fieldname(pChip, "MOSCSEL", pChip->cfg.CKGR_MOR, 24, 1);
  1274. LOG_USER("(mainosc source: %s)",
  1275. v ? "external xtal" : "internal RC");
  1276. v = sam4_reg_fieldname(pChip, "CFDEN", pChip->cfg.CKGR_MOR, 25, 1);
  1277. LOG_USER("(clock failure enabled: %s)",
  1278. _yes_or_no(v));
  1279. }
  1280. static void sam4_explain_chipid_cidr(struct sam4_chip *pChip)
  1281. {
  1282. int x;
  1283. uint32_t v;
  1284. const char *cp;
  1285. sam4_reg_fieldname(pChip, "Version", pChip->cfg.CHIPID_CIDR, 0, 5);
  1286. LOG_USER_N("\n");
  1287. v = sam4_reg_fieldname(pChip, "EPROC", pChip->cfg.CHIPID_CIDR, 5, 3);
  1288. LOG_USER("%s", eproc_names[v]);
  1289. v = sam4_reg_fieldname(pChip, "NVPSIZE", pChip->cfg.CHIPID_CIDR, 8, 4);
  1290. LOG_USER("%s", nvpsize[v]);
  1291. v = sam4_reg_fieldname(pChip, "NVPSIZE2", pChip->cfg.CHIPID_CIDR, 12, 4);
  1292. LOG_USER("%s", nvpsize2[v]);
  1293. v = sam4_reg_fieldname(pChip, "SRAMSIZE", pChip->cfg.CHIPID_CIDR, 16, 4);
  1294. LOG_USER("%s", sramsize[v]);
  1295. v = sam4_reg_fieldname(pChip, "ARCH", pChip->cfg.CHIPID_CIDR, 20, 8);
  1296. cp = _unknown;
  1297. for (x = 0; archnames[x].name; x++) {
  1298. if (v == archnames[x].value) {
  1299. cp = archnames[x].name;
  1300. break;
  1301. }
  1302. }
  1303. LOG_USER("%s", cp);
  1304. v = sam4_reg_fieldname(pChip, "NVPTYP", pChip->cfg.CHIPID_CIDR, 28, 3);
  1305. LOG_USER("%s", nvptype[v]);
  1306. v = sam4_reg_fieldname(pChip, "EXTID", pChip->cfg.CHIPID_CIDR, 31, 1);
  1307. LOG_USER("(exists: %s)", _yes_or_no(v));
  1308. }
  1309. static void sam4_explain_ckgr_mcfr(struct sam4_chip *pChip)
  1310. {
  1311. uint32_t v;
  1312. v = sam4_reg_fieldname(pChip, "MAINFRDY", pChip->cfg.CKGR_MCFR, 16, 1);
  1313. LOG_USER("(main ready: %s)", _yes_or_no(v));
  1314. v = sam4_reg_fieldname(pChip, "MAINF", pChip->cfg.CKGR_MCFR, 0, 16);
  1315. v = (v * pChip->cfg.slow_freq) / 16;
  1316. pChip->cfg.mainosc_freq = v;
  1317. LOG_USER("(%3.03f Mhz (%" PRIu32 ".%03" PRIu32 "khz slowclk)",
  1318. _tomhz(v),
  1319. (uint32_t)(pChip->cfg.slow_freq / 1000),
  1320. (uint32_t)(pChip->cfg.slow_freq % 1000));
  1321. }
  1322. static void sam4_explain_ckgr_plla(struct sam4_chip *pChip)
  1323. {
  1324. uint32_t mula, diva;
  1325. diva = sam4_reg_fieldname(pChip, "DIVA", pChip->cfg.CKGR_PLLAR, 0, 8);
  1326. LOG_USER_N("\n");
  1327. mula = sam4_reg_fieldname(pChip, "MULA", pChip->cfg.CKGR_PLLAR, 16, 11);
  1328. LOG_USER_N("\n");
  1329. pChip->cfg.plla_freq = 0;
  1330. if (mula == 0)
  1331. LOG_USER("\tPLLA Freq: (Disabled,mula = 0)");
  1332. else if (diva == 0)
  1333. LOG_USER("\tPLLA Freq: (Disabled,diva = 0)");
  1334. else if (diva >= 1) {
  1335. pChip->cfg.plla_freq = (pChip->cfg.mainosc_freq * (mula + 1) / diva);
  1336. LOG_USER("\tPLLA Freq: %3.03f MHz",
  1337. _tomhz(pChip->cfg.plla_freq));
  1338. }
  1339. }
  1340. static void sam4_explain_mckr(struct sam4_chip *pChip)
  1341. {
  1342. uint32_t css, pres, fin = 0;
  1343. int pdiv = 0;
  1344. const char *cp = NULL;
  1345. css = sam4_reg_fieldname(pChip, "CSS", pChip->cfg.PMC_MCKR, 0, 2);
  1346. switch (css & 3) {
  1347. case 0:
  1348. fin = pChip->cfg.slow_freq;
  1349. cp = "slowclk";
  1350. break;
  1351. case 1:
  1352. fin = pChip->cfg.mainosc_freq;
  1353. cp = "mainosc";
  1354. break;
  1355. case 2:
  1356. fin = pChip->cfg.plla_freq;
  1357. cp = "plla";
  1358. break;
  1359. case 3:
  1360. if (pChip->cfg.CKGR_UCKR & (1 << 16)) {
  1361. fin = 480 * 1000 * 1000;
  1362. cp = "upll";
  1363. } else {
  1364. fin = 0;
  1365. cp = "upll (*ERROR* UPLL is disabled)";
  1366. }
  1367. break;
  1368. default:
  1369. assert(0);
  1370. break;
  1371. }
  1372. LOG_USER("%s (%3.03f Mhz)",
  1373. cp,
  1374. _tomhz(fin));
  1375. pres = sam4_reg_fieldname(pChip, "PRES", pChip->cfg.PMC_MCKR, 4, 3);
  1376. switch (pres & 0x07) {
  1377. case 0:
  1378. pdiv = 1;
  1379. cp = "selected clock";
  1380. break;
  1381. case 1:
  1382. pdiv = 2;
  1383. cp = "clock/2";
  1384. break;
  1385. case 2:
  1386. pdiv = 4;
  1387. cp = "clock/4";
  1388. break;
  1389. case 3:
  1390. pdiv = 8;
  1391. cp = "clock/8";
  1392. break;
  1393. case 4:
  1394. pdiv = 16;
  1395. cp = "clock/16";
  1396. break;
  1397. case 5:
  1398. pdiv = 32;
  1399. cp = "clock/32";
  1400. break;
  1401. case 6:
  1402. pdiv = 64;
  1403. cp = "clock/64";
  1404. break;
  1405. case 7:
  1406. pdiv = 6;
  1407. cp = "clock/6";
  1408. break;
  1409. default:
  1410. assert(0);
  1411. break;
  1412. }
  1413. LOG_USER("(%s)", cp);
  1414. fin = fin / pdiv;
  1415. /* sam4 has a *SINGLE* clock - */
  1416. /* other at91 series parts have divisors for these. */
  1417. pChip->cfg.cpu_freq = fin;
  1418. pChip->cfg.mclk_freq = fin;
  1419. pChip->cfg.fclk_freq = fin;
  1420. LOG_USER("\t\tResult CPU Freq: %3.03f",
  1421. _tomhz(fin));
  1422. }
  1423. #if 0
  1424. static struct sam4_chip *target2sam4(struct target *pTarget)
  1425. {
  1426. struct sam4_chip *pChip;
  1427. if (pTarget == NULL)
  1428. return NULL;
  1429. pChip = all_sam4_chips;
  1430. while (pChip) {
  1431. if (pChip->target == pTarget)
  1432. break; /* return below */
  1433. else
  1434. pChip = pChip->next;
  1435. }
  1436. return pChip;
  1437. }
  1438. #endif
  1439. static uint32_t *sam4_get_reg_ptr(struct sam4_cfg *pCfg, const struct sam4_reg_list *pList)
  1440. {
  1441. /* this function exists to help */
  1442. /* keep funky offsetof() errors */
  1443. /* and casting from causing bugs */
  1444. /* By using prototypes - we can detect what would */
  1445. /* be casting errors. */
  1446. return (uint32_t *)(void *)(((char *)(pCfg)) + pList->struct_offset);
  1447. }
  1448. #define SAM4_ENTRY(NAME, FUNC) { .address = SAM4_ ## NAME, .struct_offset = offsetof( \
  1449. struct sam4_cfg, \
  1450. NAME), # NAME, FUNC }
  1451. static const struct sam4_reg_list sam4_all_regs[] = {
  1452. SAM4_ENTRY(CKGR_MOR, sam4_explain_ckgr_mor),
  1453. SAM4_ENTRY(CKGR_MCFR, sam4_explain_ckgr_mcfr),
  1454. SAM4_ENTRY(CKGR_PLLAR, sam4_explain_ckgr_plla),
  1455. SAM4_ENTRY(CKGR_UCKR, NULL),
  1456. SAM4_ENTRY(PMC_FSMR, NULL),
  1457. SAM4_ENTRY(PMC_FSPR, NULL),
  1458. SAM4_ENTRY(PMC_IMR, NULL),
  1459. SAM4_ENTRY(PMC_MCKR, sam4_explain_mckr),
  1460. SAM4_ENTRY(PMC_PCK0, NULL),
  1461. SAM4_ENTRY(PMC_PCK1, NULL),
  1462. SAM4_ENTRY(PMC_PCK2, NULL),
  1463. SAM4_ENTRY(PMC_PCSR, NULL),
  1464. SAM4_ENTRY(PMC_SCSR, NULL),
  1465. SAM4_ENTRY(PMC_SR, NULL),
  1466. SAM4_ENTRY(CHIPID_CIDR, sam4_explain_chipid_cidr),
  1467. SAM4_ENTRY(CHIPID_EXID, NULL),
  1468. /* TERMINATE THE LIST */
  1469. { .name = NULL }
  1470. };
  1471. #undef SAM4_ENTRY
  1472. static struct sam4_bank_private *get_sam4_bank_private(struct flash_bank *bank)
  1473. {
  1474. return bank->driver_priv;
  1475. }
  1476. /**
  1477. * Given a pointer to where it goes in the structure,
  1478. * determine the register name, address from the all registers table.
  1479. */
  1480. static const struct sam4_reg_list *sam4_GetReg(struct sam4_chip *pChip, uint32_t *goes_here)
  1481. {
  1482. const struct sam4_reg_list *pReg;
  1483. pReg = &(sam4_all_regs[0]);
  1484. while (pReg->name) {
  1485. uint32_t *pPossible;
  1486. /* calculate where this one go.. */
  1487. /* it is "possibly" this register. */
  1488. pPossible = ((uint32_t *)(void *)(((char *)(&(pChip->cfg))) + pReg->struct_offset));
  1489. /* well? Is it this register */
  1490. if (pPossible == goes_here) {
  1491. /* Jump for joy! */
  1492. return pReg;
  1493. }
  1494. /* next... */
  1495. pReg++;
  1496. }
  1497. /* This is *TOTAL*PANIC* - we are totally screwed. */
  1498. LOG_ERROR("INVALID SAM4 REGISTER");
  1499. return NULL;
  1500. }
  1501. static int sam4_ReadThisReg(struct sam4_chip *pChip, uint32_t *goes_here)
  1502. {
  1503. const struct sam4_reg_list *pReg;
  1504. int r;
  1505. pReg = sam4_GetReg(pChip, goes_here);
  1506. if (!pReg)
  1507. return ERROR_FAIL;
  1508. r = target_read_u32(pChip->target, pReg->address, goes_here);
  1509. if (r != ERROR_OK) {
  1510. LOG_ERROR("Cannot read SAM4 register: %s @ 0x%08x, Err: %d",
  1511. pReg->name, (unsigned)(pReg->address), r);
  1512. }
  1513. return r;
  1514. }
  1515. static int sam4_ReadAllRegs(struct sam4_chip *pChip)
  1516. {
  1517. int r;
  1518. const struct sam4_reg_list *pReg;
  1519. pReg = &(sam4_all_regs[0]);
  1520. while (pReg->name) {
  1521. r = sam4_ReadThisReg(pChip,
  1522. sam4_get_reg_ptr(&(pChip->cfg), pReg));
  1523. if (r != ERROR_OK) {
  1524. LOG_ERROR("Cannot read SAM4 register: %s @ 0x%08x, Error: %d",
  1525. pReg->name, ((unsigned)(pReg->address)), r);
  1526. return r;
  1527. }
  1528. pReg++;
  1529. }
  1530. return ERROR_OK;
  1531. }
  1532. static int sam4_GetInfo(struct sam4_chip *pChip)
  1533. {
  1534. const struct sam4_reg_list *pReg;
  1535. uint32_t regval;
  1536. pReg = &(sam4_all_regs[0]);
  1537. while (pReg->name) {
  1538. /* display all regs */
  1539. LOG_DEBUG("Start: %s", pReg->name);
  1540. regval = *sam4_get_reg_ptr(&(pChip->cfg), pReg);
  1541. LOG_USER("%*s: [0x%08" PRIx32 "] -> 0x%08" PRIx32,
  1542. REG_NAME_WIDTH,
  1543. pReg->name,
  1544. pReg->address,
  1545. regval);
  1546. if (pReg->explain_func)
  1547. (*(pReg->explain_func))(pChip);
  1548. LOG_DEBUG("End: %s", pReg->name);
  1549. pReg++;
  1550. }
  1551. LOG_USER(" rc-osc: %3.03f MHz", _tomhz(pChip->cfg.rc_freq));
  1552. LOG_USER(" mainosc: %3.03f MHz", _tomhz(pChip->cfg.mainosc_freq));
  1553. LOG_USER(" plla: %3.03f MHz", _tomhz(pChip->cfg.plla_freq));
  1554. LOG_USER(" cpu-freq: %3.03f MHz", _tomhz(pChip->cfg.cpu_freq));
  1555. LOG_USER("mclk-freq: %3.03f MHz", _tomhz(pChip->cfg.mclk_freq));
  1556. LOG_USER(" UniqueId: 0x%08" PRIx32 " 0x%08" PRIx32 " 0x%08" PRIx32 " 0x%08"PRIx32,
  1557. pChip->cfg.unique_id[0],
  1558. pChip->cfg.unique_id[1],
  1559. pChip->cfg.unique_id[2],
  1560. pChip->cfg.unique_id[3]);
  1561. return ERROR_OK;
  1562. }
  1563. static int sam4_protect_check(struct flash_bank *bank)
  1564. {
  1565. int r;
  1566. uint32_t v[4] = {0};
  1567. unsigned x;
  1568. struct sam4_bank_private *pPrivate;
  1569. LOG_DEBUG("Begin");
  1570. if (bank->target->state != TARGET_HALTED) {
  1571. LOG_ERROR("Target not halted");
  1572. return ERROR_TARGET_NOT_HALTED;
  1573. }
  1574. pPrivate = get_sam4_bank_private(bank);
  1575. if (!pPrivate) {
  1576. LOG_ERROR("no private for this bank?");
  1577. return ERROR_FAIL;
  1578. }
  1579. if (!(pPrivate->probed))
  1580. return ERROR_FLASH_BANK_NOT_PROBED;
  1581. r = FLASHD_GetLockBits(pPrivate, v);
  1582. if (r != ERROR_OK) {
  1583. LOG_DEBUG("Failed: %d", r);
  1584. return r;
  1585. }
  1586. for (x = 0; x < pPrivate->nsectors; x++)
  1587. bank->sectors[x].is_protected = (!!(v[x >> 5] & (1 << (x % 32))));
  1588. LOG_DEBUG("Done");
  1589. return ERROR_OK;
  1590. }
  1591. FLASH_BANK_COMMAND_HANDLER(sam4_flash_bank_command)
  1592. {
  1593. struct sam4_chip *pChip;
  1594. pChip = all_sam4_chips;
  1595. /* is this an existing chip? */
  1596. while (pChip) {
  1597. if (pChip->target == bank->target)
  1598. break;
  1599. pChip = pChip->next;
  1600. }
  1601. if (!pChip) {
  1602. /* this is a *NEW* chip */
  1603. pChip = calloc(1, sizeof(struct sam4_chip));
  1604. if (!pChip) {
  1605. LOG_ERROR("NO RAM!");
  1606. return ERROR_FAIL;
  1607. }
  1608. pChip->target = bank->target;
  1609. /* insert at head */
  1610. pChip->next = all_sam4_chips;
  1611. all_sam4_chips = pChip;
  1612. pChip->target = bank->target;
  1613. /* assumption is this runs at 32khz */
  1614. pChip->cfg.slow_freq = 32768;
  1615. pChip->probed = 0;
  1616. }
  1617. switch (bank->base) {
  1618. default:
  1619. LOG_ERROR("Address 0x%08x invalid bank address (try 0x%08x"
  1620. "[at91sam4s series] )",
  1621. ((unsigned int)(bank->base)),
  1622. ((unsigned int)(FLASH_BANK_BASE_S)));
  1623. return ERROR_FAIL;
  1624. break;
  1625. /* at91sam4s series only has bank 0*/
  1626. /* at91sam4sd series has the same address for bank 0 (FLASH_BANK0_BASE_SD)*/
  1627. case FLASH_BANK_BASE_S:
  1628. bank->driver_priv = &(pChip->details.bank[0]);
  1629. bank->bank_number = 0;
  1630. pChip->details.bank[0].pChip = pChip;
  1631. pChip->details.bank[0].pBank = bank;
  1632. break;
  1633. /* Bank 1 of at91sam4sd series */
  1634. case FLASH_BANK1_BASE_1024K_SD:
  1635. case FLASH_BANK1_BASE_2048K_SD:
  1636. bank->driver_priv = &(pChip->details.bank[1]);
  1637. bank->bank_number = 1;
  1638. pChip->details.bank[1].pChip = pChip;
  1639. pChip->details.bank[1].pBank = bank;
  1640. break;
  1641. }
  1642. /* we initialize after probing. */
  1643. return ERROR_OK;
  1644. }
  1645. static int sam4_GetDetails(struct sam4_bank_private *pPrivate)
  1646. {
  1647. const struct sam4_chip_details *pDetails;
  1648. struct sam4_chip *pChip;
  1649. struct flash_bank *saved_banks[SAM4_MAX_FLASH_BANKS];
  1650. unsigned x;
  1651. LOG_DEBUG("Begin");
  1652. pDetails = all_sam4_details;
  1653. while (pDetails->name) {
  1654. /* Compare cidr without version bits */
  1655. if (pDetails->chipid_cidr == (pPrivate->pChip->cfg.CHIPID_CIDR & 0xFFFFFFE0))
  1656. break;
  1657. else
  1658. pDetails++;
  1659. }
  1660. if (pDetails->name == NULL) {
  1661. LOG_ERROR("SAM4 ChipID 0x%08x not found in table (perhaps you can ID this chip?)",
  1662. (unsigned int)(pPrivate->pChip->cfg.CHIPID_CIDR));
  1663. /* Help the victim, print details about the chip */
  1664. LOG_INFO("SAM4 CHIPID_CIDR: 0x%08" PRIx32 " decodes as follows",
  1665. pPrivate->pChip->cfg.CHIPID_CIDR);
  1666. sam4_explain_chipid_cidr(pPrivate->pChip);
  1667. return ERROR_FAIL;
  1668. }
  1669. /* DANGER: THERE ARE DRAGONS HERE */
  1670. /* get our pChip - it is going */
  1671. /* to be over-written shortly */
  1672. pChip = pPrivate->pChip;
  1673. /* Note that, in reality: */
  1674. /* */
  1675. /* pPrivate = &(pChip->details.bank[0]) */
  1676. /* or pPrivate = &(pChip->details.bank[1]) */
  1677. /* */
  1678. /* save the "bank" pointers */
  1679. for (x = 0; x < SAM4_MAX_FLASH_BANKS; x++)
  1680. saved_banks[x] = pChip->details.bank[x].pBank;
  1681. /* Overwrite the "details" structure. */
  1682. memcpy(&(pPrivate->pChip->details),
  1683. pDetails,
  1684. sizeof(pPrivate->pChip->details));
  1685. /* now fix the ghosted pointers */
  1686. for (x = 0; x < SAM4_MAX_FLASH_BANKS; x++) {
  1687. pChip->details.bank[x].pChip = pChip;
  1688. pChip->details.bank[x].pBank = saved_banks[x];
  1689. }
  1690. /* update the *BANK*SIZE* */
  1691. LOG_DEBUG("End");
  1692. return ERROR_OK;
  1693. }
  1694. static int _sam4_probe(struct flash_bank *bank, int noise)
  1695. {
  1696. unsigned x;
  1697. int r;
  1698. struct sam4_bank_private *pPrivate;
  1699. LOG_DEBUG("Begin: Bank: %d, Noise: %d", bank->bank_number, noise);
  1700. if (bank->target->state != TARGET_HALTED) {
  1701. LOG_ERROR("Target not halted");
  1702. return ERROR_TARGET_NOT_HALTED;
  1703. }
  1704. pPrivate = get_sam4_bank_private(bank);
  1705. if (!pPrivate) {
  1706. LOG_ERROR("Invalid/unknown bank number");
  1707. return ERROR_FAIL;
  1708. }
  1709. r = sam4_ReadAllRegs(pPrivate->pChip);
  1710. if (r != ERROR_OK)
  1711. return r;
  1712. LOG_DEBUG("Here");
  1713. if (pPrivate->pChip->probed)
  1714. r = sam4_GetInfo(pPrivate->pChip);
  1715. else
  1716. r = sam4_GetDetails(pPrivate);
  1717. if (r != ERROR_OK)
  1718. return r;
  1719. /* update the flash bank size */
  1720. for (x = 0; x < SAM4_MAX_FLASH_BANKS; x++) {
  1721. if (bank->base == pPrivate->pChip->details.bank[x].base_address) {
  1722. bank->size = pPrivate->pChip->details.bank[x].size_bytes;
  1723. break;
  1724. }
  1725. }
  1726. if (bank->sectors == NULL) {
  1727. bank->sectors = calloc(pPrivate->nsectors, (sizeof((bank->sectors)[0])));
  1728. if (bank->sectors == NULL) {
  1729. LOG_ERROR("No memory!");
  1730. return ERROR_FAIL;
  1731. }
  1732. bank->num_sectors = pPrivate->nsectors;
  1733. for (x = 0; ((int)(x)) < bank->num_sectors; x++) {
  1734. bank->sectors[x].size = pPrivate->sector_size;
  1735. bank->sectors[x].offset = x * (pPrivate->sector_size);
  1736. /* mark as unknown */
  1737. bank->sectors[x].is_erased = -1;
  1738. bank->sectors[x].is_protected = -1;
  1739. }
  1740. }
  1741. pPrivate->probed = 1;
  1742. r = sam4_protect_check(bank);
  1743. if (r != ERROR_OK)
  1744. return r;
  1745. LOG_DEBUG("Bank = %d, nbanks = %d",
  1746. pPrivate->bank_number, pPrivate->pChip->details.n_banks);
  1747. if ((pPrivate->bank_number + 1) == pPrivate->pChip->details.n_banks) {
  1748. /* read unique id, */
  1749. /* it appears to be associated with the *last* flash bank. */
  1750. FLASHD_ReadUniqueID(pPrivate);
  1751. }
  1752. return r;
  1753. }
  1754. static int sam4_probe(struct flash_bank *bank)
  1755. {
  1756. return _sam4_probe(bank, 1);
  1757. }
  1758. static int sam4_auto_probe(struct flash_bank *bank)
  1759. {
  1760. return _sam4_probe(bank, 0);
  1761. }
  1762. static int sam4_erase(struct flash_bank *bank, int first, int last)
  1763. {
  1764. struct sam4_bank_private *pPrivate;
  1765. int r;
  1766. int i;
  1767. int pageCount;
  1768. /*16 pages equals 8KB - Same size as a lock region*/
  1769. pageCount = 16;
  1770. uint32_t status;
  1771. LOG_DEBUG("Here");
  1772. if (bank->target->state != TARGET_HALTED) {
  1773. LOG_ERROR("Target not halted");
  1774. return ERROR_TARGET_NOT_HALTED;
  1775. }
  1776. r = sam4_auto_probe(bank);
  1777. if (r != ERROR_OK) {
  1778. LOG_DEBUG("Here,r=%d", r);
  1779. return r;
  1780. }
  1781. pPrivate = get_sam4_bank_private(bank);
  1782. if (!(pPrivate->probed))
  1783. return ERROR_FLASH_BANK_NOT_PROBED;
  1784. if ((first == 0) && ((last + 1) == ((int)(pPrivate->nsectors)))) {
  1785. /* whole chip */
  1786. LOG_DEBUG("Here");
  1787. return FLASHD_EraseEntireBank(pPrivate);
  1788. }
  1789. LOG_INFO("sam4 does not auto-erase while programming (Erasing relevant sectors)");
  1790. LOG_INFO("sam4 First: 0x%08x Last: 0x%08x", (unsigned int)(first), (unsigned int)(last));
  1791. for (i = first; i <= last; i++) {
  1792. /*16 pages equals 8KB - Same size as a lock region*/
  1793. r = FLASHD_ErasePages(pPrivate, (i * pageCount), pageCount, &status);
  1794. LOG_INFO("Erasing sector: 0x%08x", (unsigned int)(i));
  1795. if (r != ERROR_OK)
  1796. LOG_ERROR("SAM4: Error performing Erase page @ lock region number %d",
  1797. (unsigned int)(i));
  1798. if (status & (1 << 2)) {
  1799. LOG_ERROR("SAM4: Lock Region %d is locked", (unsigned int)(i));
  1800. return ERROR_FAIL;
  1801. }
  1802. if (status & (1 << 1)) {
  1803. LOG_ERROR("SAM4: Flash Command error @lock region %d", (unsigned int)(i));
  1804. return ERROR_FAIL;
  1805. }
  1806. }
  1807. return ERROR_OK;
  1808. }
  1809. static int sam4_protect(struct flash_bank *bank, int set, int first, int last)
  1810. {
  1811. struct sam4_bank_private *pPrivate;
  1812. int r;
  1813. LOG_DEBUG("Here");
  1814. if (bank->target->state != TARGET_HALTED) {
  1815. LOG_ERROR("Target not halted");
  1816. return ERROR_TARGET_NOT_HALTED;
  1817. }
  1818. pPrivate = get_sam4_bank_private(bank);
  1819. if (!(pPrivate->probed))
  1820. return ERROR_FLASH_BANK_NOT_PROBED;
  1821. if (set)
  1822. r = FLASHD_Lock(pPrivate, (unsigned)(first), (unsigned)(last));
  1823. else
  1824. r = FLASHD_Unlock(pPrivate, (unsigned)(first), (unsigned)(last));
  1825. LOG_DEBUG("End: r=%d", r);
  1826. return r;
  1827. }
  1828. static int sam4_page_read(struct sam4_bank_private *pPrivate, unsigned pagenum, uint8_t *buf)
  1829. {
  1830. uint32_t adr;
  1831. int r;
  1832. adr = pagenum * pPrivate->page_size;
  1833. adr = adr + pPrivate->base_address;
  1834. r = target_read_memory(pPrivate->pChip->target,
  1835. adr,
  1836. 4, /* THIS*MUST*BE* in 32bit values */
  1837. pPrivate->page_size / 4,
  1838. buf);
  1839. if (r != ERROR_OK)
  1840. LOG_ERROR("SAM4: Flash program failed to read page phys address: 0x%08x",
  1841. (unsigned int)(adr));
  1842. return r;
  1843. }
  1844. static int sam4_page_write(struct sam4_bank_private *pPrivate, unsigned pagenum, const uint8_t *buf)
  1845. {
  1846. uint32_t adr;
  1847. uint32_t status;
  1848. uint32_t fmr; /* EEFC Flash Mode Register */
  1849. int r;
  1850. adr = pagenum * pPrivate->page_size;
  1851. adr = (adr + pPrivate->base_address);
  1852. /* Get flash mode register value */
  1853. r = target_read_u32(pPrivate->pChip->target, pPrivate->controller_address, &fmr);
  1854. if (r != ERROR_OK)
  1855. LOG_DEBUG("Error Read failed: read flash mode register");
  1856. /* Clear flash wait state field */
  1857. fmr &= 0xfffff0ff;
  1858. /* set FWS (flash wait states) field in the FMR (flash mode register) */
  1859. fmr |= (pPrivate->flash_wait_states << 8);
  1860. LOG_DEBUG("Flash Mode: 0x%08x", ((unsigned int)(fmr)));
  1861. r = target_write_u32(pPrivate->pBank->target, pPrivate->controller_address, fmr);
  1862. if (r != ERROR_OK)
  1863. LOG_DEBUG("Error Write failed: set flash mode register");
  1864. /* 1st sector 8kBytes - page 0 - 15*/
  1865. /* 2nd sector 8kBytes - page 16 - 30*/
  1866. /* 3rd sector 48kBytes - page 31 - 127*/
  1867. LOG_DEBUG("Wr Page %u @ phys address: 0x%08x", pagenum, (unsigned int)(adr));
  1868. r = target_write_memory(pPrivate->pChip->target,
  1869. adr,
  1870. 4, /* THIS*MUST*BE* in 32bit values */
  1871. pPrivate->page_size / 4,
  1872. buf);
  1873. if (r != ERROR_OK) {
  1874. LOG_ERROR("SAM4: Failed to write (buffer) page at phys address 0x%08x",
  1875. (unsigned int)(adr));
  1876. return r;
  1877. }
  1878. r = EFC_PerformCommand(pPrivate,
  1879. /* send Erase & Write Page */
  1880. AT91C_EFC_FCMD_WP, /*AT91C_EFC_FCMD_EWP only works on first two 8kb sectors*/
  1881. pagenum,
  1882. &status);
  1883. if (r != ERROR_OK)
  1884. LOG_ERROR("SAM4: Error performing Write page @ phys address 0x%08x",
  1885. (unsigned int)(adr));
  1886. if (status & (1 << 2)) {
  1887. LOG_ERROR("SAM4: Page @ Phys address 0x%08x is locked", (unsigned int)(adr));
  1888. return ERROR_FAIL;
  1889. }
  1890. if (status & (1 << 1)) {
  1891. LOG_ERROR("SAM4: Flash Command error @phys address 0x%08x", (unsigned int)(adr));
  1892. return ERROR_FAIL;
  1893. }
  1894. return ERROR_OK;
  1895. }
  1896. static int sam4_write(struct flash_bank *bank,
  1897. const uint8_t *buffer,
  1898. uint32_t offset,
  1899. uint32_t count)
  1900. {
  1901. int n;
  1902. unsigned page_cur;
  1903. unsigned page_end;
  1904. int r;
  1905. unsigned page_offset;
  1906. struct sam4_bank_private *pPrivate;
  1907. uint8_t *pagebuffer;
  1908. /* incase we bail further below, set this to null */
  1909. pagebuffer = NULL;
  1910. /* ignore dumb requests */
  1911. if (count == 0) {
  1912. r = ERROR_OK;
  1913. goto done;
  1914. }
  1915. if (bank->target->state != TARGET_HALTED) {
  1916. LOG_ERROR("Target not halted");
  1917. r = ERROR_TARGET_NOT_HALTED;
  1918. goto done;
  1919. }
  1920. pPrivate = get_sam4_bank_private(bank);
  1921. if (!(pPrivate->probed)) {
  1922. r = ERROR_FLASH_BANK_NOT_PROBED;
  1923. goto done;
  1924. }
  1925. if ((offset + count) > pPrivate->size_bytes) {
  1926. LOG_ERROR("Flash write error - past end of bank");
  1927. LOG_ERROR(" offset: 0x%08x, count 0x%08x, BankEnd: 0x%08x",
  1928. (unsigned int)(offset),
  1929. (unsigned int)(count),
  1930. (unsigned int)(pPrivate->size_bytes));
  1931. r = ERROR_FAIL;
  1932. goto done;
  1933. }
  1934. pagebuffer = malloc(pPrivate->page_size);
  1935. if (!pagebuffer) {
  1936. LOG_ERROR("No memory for %d Byte page buffer", (int)(pPrivate->page_size));
  1937. r = ERROR_FAIL;
  1938. goto done;
  1939. }
  1940. /* what page do we start & end in? */
  1941. page_cur = offset / pPrivate->page_size;
  1942. page_end = (offset + count - 1) / pPrivate->page_size;
  1943. LOG_DEBUG("Offset: 0x%08x, Count: 0x%08x", (unsigned int)(offset), (unsigned int)(count));
  1944. LOG_DEBUG("Page start: %d, Page End: %d", (int)(page_cur), (int)(page_end));
  1945. /* Special case: all one page */
  1946. /* */
  1947. /* Otherwise: */
  1948. /* (1) non-aligned start */
  1949. /* (2) body pages */
  1950. /* (3) non-aligned end. */
  1951. /* Handle special case - all one page. */
  1952. if (page_cur == page_end) {
  1953. LOG_DEBUG("Special case, all in one page");
  1954. r = sam4_page_read(pPrivate, page_cur, pagebuffer);
  1955. if (r != ERROR_OK)
  1956. goto done;
  1957. page_offset = (offset & (pPrivate->page_size-1));
  1958. memcpy(pagebuffer + page_offset,
  1959. buffer,
  1960. count);
  1961. r = sam4_page_write(pPrivate, page_cur, pagebuffer);
  1962. if (r != ERROR_OK)
  1963. goto done;
  1964. r = ERROR_OK;
  1965. goto done;
  1966. }
  1967. /* non-aligned start */
  1968. page_offset = offset & (pPrivate->page_size - 1);
  1969. if (page_offset) {
  1970. LOG_DEBUG("Not-Aligned start");
  1971. /* read the partial */
  1972. r = sam4_page_read(pPrivate, page_cur, pagebuffer);
  1973. if (r != ERROR_OK)
  1974. goto done;
  1975. /* over-write with new data */
  1976. n = (pPrivate->page_size - page_offset);
  1977. memcpy(pagebuffer + page_offset,
  1978. buffer,
  1979. n);
  1980. r = sam4_page_write(pPrivate, page_cur, pagebuffer);
  1981. if (r != ERROR_OK)
  1982. goto done;
  1983. count -= n;
  1984. offset += n;
  1985. buffer += n;
  1986. page_cur++;
  1987. }
  1988. /* By checking that offset is correct here, we also
  1989. fix a clang warning */
  1990. assert(offset % pPrivate->page_size == 0);
  1991. /* intermediate large pages */
  1992. /* also - the final *terminal* */
  1993. /* if that terminal page is a full page */
  1994. LOG_DEBUG("Full Page Loop: cur=%d, end=%d, count = 0x%08x",
  1995. (int)page_cur, (int)page_end, (unsigned int)(count));
  1996. while ((page_cur < page_end) &&
  1997. (count >= pPrivate->page_size)) {
  1998. r = sam4_page_write(pPrivate, page_cur, buffer);
  1999. if (r != ERROR_OK)
  2000. goto done;
  2001. count -= pPrivate->page_size;
  2002. buffer += pPrivate->page_size;
  2003. page_cur += 1;
  2004. }
  2005. /* terminal partial page? */
  2006. if (count) {
  2007. LOG_DEBUG("Terminal partial page, count = 0x%08x", (unsigned int)(count));
  2008. /* we have a partial page */
  2009. r = sam4_page_read(pPrivate, page_cur, pagebuffer);
  2010. if (r != ERROR_OK)
  2011. goto done;
  2012. /* data goes at start */
  2013. memcpy(pagebuffer, buffer, count);
  2014. r = sam4_page_write(pPrivate, page_cur, pagebuffer);
  2015. if (r != ERROR_OK)
  2016. goto done;
  2017. }
  2018. LOG_DEBUG("Done!");
  2019. r = ERROR_OK;
  2020. done:
  2021. if (pagebuffer)
  2022. free(pagebuffer);
  2023. return r;
  2024. }
  2025. COMMAND_HANDLER(sam4_handle_info_command)
  2026. {
  2027. struct sam4_chip *pChip;
  2028. pChip = get_current_sam4(CMD_CTX);
  2029. if (!pChip)
  2030. return ERROR_OK;
  2031. unsigned x;
  2032. int r;
  2033. /* bank0 must exist before we can do anything */
  2034. if (pChip->details.bank[0].pBank == NULL) {
  2035. x = 0;
  2036. need_define:
  2037. command_print(CMD_CTX,
  2038. "Please define bank %d via command: flash bank %s ... ",
  2039. x,
  2040. at91sam4_flash.name);
  2041. return ERROR_FAIL;
  2042. }
  2043. /* if bank 0 is not probed, then probe it */
  2044. if (!(pChip->details.bank[0].probed)) {
  2045. r = sam4_auto_probe(pChip->details.bank[0].pBank);
  2046. if (r != ERROR_OK)
  2047. return ERROR_FAIL;
  2048. }
  2049. /* above guarantees the "chip details" structure is valid */
  2050. /* and thus, bank private areas are valid */
  2051. /* and we have a SAM4 chip, what a concept! */
  2052. /* auto-probe other banks, 0 done above */
  2053. for (x = 1; x < SAM4_MAX_FLASH_BANKS; x++) {
  2054. /* skip banks not present */
  2055. if (!(pChip->details.bank[x].present))
  2056. continue;
  2057. if (pChip->details.bank[x].pBank == NULL)
  2058. goto need_define;
  2059. if (pChip->details.bank[x].probed)
  2060. continue;
  2061. r = sam4_auto_probe(pChip->details.bank[x].pBank);
  2062. if (r != ERROR_OK)
  2063. return r;
  2064. }
  2065. r = sam4_GetInfo(pChip);
  2066. if (r != ERROR_OK) {
  2067. LOG_DEBUG("Sam4Info, Failed %d", r);
  2068. return r;
  2069. }
  2070. return ERROR_OK;
  2071. }
  2072. COMMAND_HANDLER(sam4_handle_gpnvm_command)
  2073. {
  2074. unsigned x, v;
  2075. int r, who;
  2076. struct sam4_chip *pChip;
  2077. pChip = get_current_sam4(CMD_CTX);
  2078. if (!pChip)
  2079. return ERROR_OK;
  2080. if (pChip->target->state != TARGET_HALTED) {
  2081. LOG_ERROR("sam4 - target not halted");
  2082. return ERROR_TARGET_NOT_HALTED;
  2083. }
  2084. if (pChip->details.bank[0].pBank == NULL) {
  2085. command_print(CMD_CTX, "Bank0 must be defined first via: flash bank %s ...",
  2086. at91sam4_flash.name);
  2087. return ERROR_FAIL;
  2088. }
  2089. if (!pChip->details.bank[0].probed) {
  2090. r = sam4_auto_probe(pChip->details.bank[0].pBank);
  2091. if (r != ERROR_OK)
  2092. return r;
  2093. }
  2094. switch (CMD_ARGC) {
  2095. default:
  2096. return ERROR_COMMAND_SYNTAX_ERROR;
  2097. break;
  2098. case 0:
  2099. goto showall;
  2100. break;
  2101. case 1:
  2102. who = -1;
  2103. break;
  2104. case 2:
  2105. if ((0 == strcmp(CMD_ARGV[0], "show")) && (0 == strcmp(CMD_ARGV[1], "all")))
  2106. who = -1;
  2107. else {
  2108. uint32_t v32;
  2109. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], v32);
  2110. who = v32;
  2111. }
  2112. break;
  2113. }
  2114. if (0 == strcmp("show", CMD_ARGV[0])) {
  2115. if (who == -1) {
  2116. showall:
  2117. r = ERROR_OK;
  2118. for (x = 0; x < pChip->details.n_gpnvms; x++) {
  2119. r = FLASHD_GetGPNVM(&(pChip->details.bank[0]), x, &v);
  2120. if (r != ERROR_OK)
  2121. break;
  2122. command_print(CMD_CTX, "sam4-gpnvm%u: %u", x, v);
  2123. }
  2124. return r;
  2125. }
  2126. if ((who >= 0) && (((unsigned)(who)) < pChip->details.n_gpnvms)) {
  2127. r = FLASHD_GetGPNVM(&(pChip->details.bank[0]), who, &v);
  2128. command_print(CMD_CTX, "sam4-gpnvm%u: %u", who, v);
  2129. return r;
  2130. } else {
  2131. command_print(CMD_CTX, "sam4-gpnvm invalid GPNVM: %u", who);
  2132. return ERROR_COMMAND_SYNTAX_ERROR;
  2133. }
  2134. }
  2135. if (who == -1) {
  2136. command_print(CMD_CTX, "Missing GPNVM number");
  2137. return ERROR_COMMAND_SYNTAX_ERROR;
  2138. }
  2139. if (0 == strcmp("set", CMD_ARGV[0]))
  2140. r = FLASHD_SetGPNVM(&(pChip->details.bank[0]), who);
  2141. else if ((0 == strcmp("clr", CMD_ARGV[0])) ||
  2142. (0 == strcmp("clear", CMD_ARGV[0]))) /* quietly accept both */
  2143. r = FLASHD_ClrGPNVM(&(pChip->details.bank[0]), who);
  2144. else {
  2145. command_print(CMD_CTX, "Unknown command: %s", CMD_ARGV[0]);
  2146. r = ERROR_COMMAND_SYNTAX_ERROR;
  2147. }
  2148. return r;
  2149. }
  2150. COMMAND_HANDLER(sam4_handle_slowclk_command)
  2151. {
  2152. struct sam4_chip *pChip;
  2153. pChip = get_current_sam4(CMD_CTX);
  2154. if (!pChip)
  2155. return ERROR_OK;
  2156. switch (CMD_ARGC) {
  2157. case 0:
  2158. /* show */
  2159. break;
  2160. case 1:
  2161. {
  2162. /* set */
  2163. uint32_t v;
  2164. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], v);
  2165. if (v > 200000) {
  2166. /* absurd slow clock of 200Khz? */
  2167. command_print(CMD_CTX, "Absurd/illegal slow clock freq: %d\n", (int)(v));
  2168. return ERROR_COMMAND_SYNTAX_ERROR;
  2169. }
  2170. pChip->cfg.slow_freq = v;
  2171. break;
  2172. }
  2173. default:
  2174. /* error */
  2175. command_print(CMD_CTX, "Too many parameters");
  2176. return ERROR_COMMAND_SYNTAX_ERROR;
  2177. break;
  2178. }
  2179. command_print(CMD_CTX, "Slowclk freq: %d.%03dkhz",
  2180. (int)(pChip->cfg.slow_freq / 1000),
  2181. (int)(pChip->cfg.slow_freq % 1000));
  2182. return ERROR_OK;
  2183. }
  2184. static const struct command_registration at91sam4_exec_command_handlers[] = {
  2185. {
  2186. .name = "gpnvm",
  2187. .handler = sam4_handle_gpnvm_command,
  2188. .mode = COMMAND_EXEC,
  2189. .usage = "[('clr'|'set'|'show') bitnum]",
  2190. .help = "Without arguments, shows all bits in the gpnvm "
  2191. "register. Otherwise, clears, sets, or shows one "
  2192. "General Purpose Non-Volatile Memory (gpnvm) bit.",
  2193. },
  2194. {
  2195. .name = "info",
  2196. .handler = sam4_handle_info_command,
  2197. .mode = COMMAND_EXEC,
  2198. .help = "Print information about the current at91sam4 chip"
  2199. "and its flash configuration.",
  2200. },
  2201. {
  2202. .name = "slowclk",
  2203. .handler = sam4_handle_slowclk_command,
  2204. .mode = COMMAND_EXEC,
  2205. .usage = "[clock_hz]",
  2206. .help = "Display or set the slowclock frequency "
  2207. "(default 32768 Hz).",
  2208. },
  2209. COMMAND_REGISTRATION_DONE
  2210. };
  2211. static const struct command_registration at91sam4_command_handlers[] = {
  2212. {
  2213. .name = "at91sam4",
  2214. .mode = COMMAND_ANY,
  2215. .help = "at91sam4 flash command group",
  2216. .usage = "",
  2217. .chain = at91sam4_exec_command_handlers,
  2218. },
  2219. COMMAND_REGISTRATION_DONE
  2220. };
  2221. struct flash_driver at91sam4_flash = {
  2222. .name = "at91sam4",
  2223. .commands = at91sam4_command_handlers,
  2224. .flash_bank_command = sam4_flash_bank_command,
  2225. .erase = sam4_erase,
  2226. .protect = sam4_protect,
  2227. .write = sam4_write,
  2228. .read = default_flash_read,
  2229. .probe = sam4_probe,
  2230. .auto_probe = sam4_auto_probe,
  2231. .erase_check = default_flash_blank_check,
  2232. .protect_check = sam4_protect_check,
  2233. };