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.
 
 
 
 
 
 

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