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.
 
 
 
 
 
 

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