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.
 
 
 
 
 
 

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