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.
 
 
 
 
 
 

2516 lines
58 KiB

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