sama_secumod.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file SAMA5D2x/sama_secumod.h
  15. * @brief SAMA SECUMOD support macros and structures.
  16. *
  17. * @addtogroup SAMA5D2x_SECUMOD
  18. * @{
  19. */
  20. #ifndef SAMA_SECUMOD_LLD_H
  21. #define SAMA_SECUMOD_LLD_H
  22. /**
  23. * @brief Using the SECUMOD driver.
  24. */
  25. #if !defined(SAMA_USE_SECUMOD) || defined(__DOXYGEN__)
  26. #define SAMA_USE_SECUMOD FALSE
  27. #endif
  28. #if SAMA_USE_SECUMOD || defined(__DOXYGEN__)
  29. #include <string.h>
  30. /*===========================================================================*/
  31. /* Driver constants. */
  32. /*===========================================================================*/
  33. /**
  34. * @name RAM ACCESS mode macros
  35. * @{
  36. */
  37. /**
  38. * @brief No access allowed.
  39. */
  40. #define RAMACC_NO_ACCESS 0x0U
  41. /**
  42. * @brief Only write access allowed.
  43. */
  44. #define RAMACC_WR_ACCESS 0x1U
  45. /**
  46. * @brief Only read access allowed.
  47. */
  48. #define RAMACC_RD_ACCESS 0x2U
  49. /**
  50. * @brief Read and Write access allowed.
  51. */
  52. #define RAMACC_WR_RD_ACCESS 0x3U
  53. /** @} */
  54. /**
  55. * @name SOURCE INTERRUPT macros
  56. * @{
  57. */
  58. /*
  59. * @brief Shield Monitor Protection Interrupt Source.
  60. */
  61. #define SECUMOD_SHLDM (0x1u << 0)
  62. /*
  63. * @brief Double Frequency Monitor Protection Interrupt Source.
  64. */
  65. #define SECUMOD_DBLFM (0x1u << 1)
  66. /*
  67. * @brief Test Pin Protection Interrupt Source.
  68. */
  69. #define SECUMOD_TST (0x1u << 2)
  70. /*
  71. * @brief JTAG Pins Protection Interrupt Source.
  72. */
  73. #define SECUMOD_JTAG (0x1u << 3)
  74. /*
  75. * @brief Master Clock Monitor Protection Interrupt Source.
  76. */
  77. #define SECUMOD_MCKM (0x1u << 5)
  78. /*
  79. * @brief Low Temperature Monitor Protection Interrupt Source.
  80. */
  81. #define SECUMOD_TPML (0x1u << 6)
  82. /*
  83. * @brief High Temperature Monitor Protection Interrupt Source.
  84. */
  85. #define SECUMOD_TPMH (0x1u << 7)
  86. /*
  87. * @brief Low VDDBU Voltage Monitor Protection Interrupt Source.
  88. */
  89. #define SECUMOD_VDDBUL (0x1u << 10)
  90. /*
  91. * @brief High VDDBU Voltage Monitor Protection Interrupt Source.
  92. */
  93. #define SECUMOD_VDDBUH (0x1u << 11)
  94. /*
  95. * @brief Low VDDCORE Voltage Monitor Protection Interrupt Source.
  96. */
  97. #define SECUMOD_VDDCOREL (0x1u << 12)
  98. /*
  99. * @brief High VDDCORE Voltage Monitor Protection Interrupt Source.
  100. */
  101. #define SECUMOD_VDDCOREH (0x1u << 13)
  102. /*
  103. * @brief PIOBUx Intrusion Detector Protection Interrupt Source.
  104. */
  105. #define SECUMOD_DET0 (0x1u << 16)
  106. #define SECUMOD_DET1 (0x1u << 17)
  107. #define SECUMOD_DET2 (0x1u << 18)
  108. #define SECUMOD_DET3 (0x1u << 19)
  109. #define SECUMOD_DET4 (0x1u << 20)
  110. #define SECUMOD_DET5 (0x1u << 21)
  111. #define SECUMOD_DET6 (0x1u << 22)
  112. #define SECUMOD_DET7 (0x1u << 23)
  113. /** @} */
  114. /**
  115. * @name RAM STATUS mode macros
  116. * @{
  117. */
  118. /**
  119. * @brief No access violation occurred.
  120. */
  121. #define RAMACCSR_NO_VIOLATION 0x0U
  122. /**
  123. * @brief Write access violation occurred.
  124. */
  125. #define RAMACCSR_WR_VIOLATION 0x1U
  126. /**
  127. * @brief Read access violation occurred.
  128. */
  129. #define RAMACCSR_RD_VIOLATION 0x2U
  130. /**
  131. * @brief Read and Write access violation occurred.
  132. */
  133. #define RAMACCSR_WR_RD_VIOLATION 0x3U
  134. /** @} */
  135. /**
  136. * @name SCRAMB mode macros
  137. * @{
  138. */
  139. /**
  140. * @brief SCRAMB ENABLE.
  141. */
  142. #define SCRAMB_ENABLE 0x1U
  143. /**
  144. * @brief SCRAMB DISABLE.
  145. */
  146. #define SCRAMB_DISABLE 0x2U
  147. /** @} */
  148. /*
  149. * @brief RAM regions of SECUMOD
  150. */
  151. #define SECUMOD_RAM_REGIONS 6
  152. /*===========================================================================*/
  153. /* Driver pre-compile time settings. */
  154. /*===========================================================================*/
  155. /**
  156. * @name Configuration options
  157. * @{
  158. */
  159. /**
  160. * @brief SECUMOD interrupt priority level setting.
  161. */
  162. #if !defined(SAMA_SECUMOD_IRQ_PRIORITY) || defined(__DOXYGEN__)
  163. #define SAMA_SECUMOD_IRQ_PRIORITY 7
  164. #endif
  165. /**
  166. * @brief SECURAM interrupt priority level setting.
  167. */
  168. #if !defined(SAMA_SECURAM_IRQ_PRIORITY) || defined(__DOXYGEN__)
  169. #define SAMA_SECURAM_IRQ_PRIORITY 7
  170. #endif
  171. /** @} */
  172. /*===========================================================================*/
  173. /* Derived constants and error checks. */
  174. /*===========================================================================*/
  175. /*===========================================================================*/
  176. /* Driver data structures and types. */
  177. /*===========================================================================*/
  178. /**
  179. * @brief Driver state machine possible states.
  180. */
  181. typedef enum {
  182. SEC_UNINIT = 0, /**< Not initialized. */
  183. SEC_STOP = 1, /**< Stopped. */
  184. SEC_ACTIVE = 2 /**< Active. */
  185. } secstate_t;
  186. /**
  187. * @brief Type of an SECUMOD event.
  188. */
  189. typedef enum {
  190. SEC_EVENT_SHLDM = 0, /* Triggered on Shield Monitor. */
  191. SEC_EVENT_DBLFM = 1, /* Triggered on Double Frequency Monitor. */
  192. SEC_EVENT_TST = 2, /* Triggered on Test Pin Monitor. */
  193. SEC_EVENT_JTAG = 3, /* Triggered on JTAG Pins Monitor. */
  194. SEC_EVENT_MCKM = 4, /* Triggered on Master Clock Monitor. */
  195. SEC_EVENT_TPML = 5, /* Triggered on Low Temperature Monitor. */
  196. SEC_EVENT_TPMH = 6, /* Triggered on High Temperature Monitor. */
  197. SEC_EVENT_VDDBUL = 7, /* Triggered on Low VDDBU Voltage Monitor. */
  198. SEC_EVENT_VDDBUH = 8, /* Triggered on High VDDBU Voltage Monitor. */
  199. SEC_EVENT_VDDCOREL = 9, /* Triggered on Low VDDCORE Voltage Monitor. */
  200. SEC_EVENT_VDDCOREH = 10, /* Triggered on High VDDCORE Voltage Monitor. */
  201. SEC_EVENT_PIOBU0 = 11, /* Triggered on PIOBUx intrusion. */
  202. SEC_EVENT_PIOBU1 = 12, /* Triggered on PIOBUx intrusion. */
  203. SEC_EVENT_PIOBU2 = 13, /* Triggered on PIOBUx intrusion. */
  204. SEC_EVENT_PIOBU3 = 14, /* Triggered on PIOBUx intrusion. */
  205. SEC_EVENT_PIOBU4 = 15, /* Triggered on PIOBUx intrusion. */
  206. SEC_EVENT_PIOBU5 = 16, /* Triggered on PIOBUx intrusion. */
  207. SEC_EVENT_PIOBU6 = 17, /* Triggered on PIOBUx intrusion. */
  208. SEC_EVENT_PIOBU7 = 18 /* Triggered on PIOBUx intrusion. */
  209. } secevent_t;
  210. /**
  211. * @brief Type of a structure representing an SEC driver.
  212. */
  213. typedef struct SECDriver SECDriver;
  214. /**
  215. * @brief SECURAM notification callback type.
  216. *
  217. * @param[in] secp pointer to a @p SECDriver object
  218. */
  219. typedef void (*securam_callback_t)(SECDriver *secp);
  220. /**
  221. * @brief SECUMOD notification callback type.
  222. *
  223. * @param[in] secp pointer to a @p SECDriver object
  224. */
  225. typedef void (*secumod_callback_t)(SECDriver *secp, secevent_t event);
  226. /**
  227. * @brief SECUMOD erase callback type.
  228. *
  229. * @param[in] secp pointer to a @p SECDriver object
  230. */
  231. typedef void (*erased_callback_t)(SECDriver *secp);
  232. /**
  233. * @brief Type of RAM access mode.
  234. */
  235. typedef uint32_t ram_access_mode_t;
  236. /**
  237. * @brief PIOBU configuration structure.
  238. */
  239. typedef struct {
  240. /*
  241. * @brief PIOBU pin's index
  242. */
  243. uint32_t pinIndex:3;
  244. /*
  245. * @brief alarm filter value
  246. */
  247. uint32_t afv:4;
  248. /*
  249. * @brief reset filter value
  250. */
  251. uint32_t rfv:4;
  252. /*
  253. * @brief I/O line mode
  254. * @note 0: pure input, 1: enabled in output
  255. */
  256. uint32_t mode:1;
  257. /*
  258. * @brief Configure the I/O line in output mode
  259. * @note 0: clear, 1: set
  260. */
  261. uint32_t outputLevel:1;
  262. /*
  263. * @brief programmable pull-up state
  264. * @note 0: none, 1: pull-up; 2: pull-down; 3: reserved
  265. */
  266. uint32_t pullUpState:2;
  267. /*
  268. * @brief Pull-up/Down Scheduled:
  269. * @note 0: no; 1: yes
  270. */
  271. uint32_t scheduled:1;
  272. /*
  273. * @brief switch input default state
  274. */
  275. uint32_t inputDefaultLevel:1;
  276. /*
  277. * @brief Mode of detection intrusion.
  278. * @note 0: static, 1: dynamic
  279. */
  280. uint32_t dynamic:1;
  281. /*
  282. * @brief filter for dynamic signatures input
  283. * @note 0: 3 stages majority vote, 1: 5 stages
  284. */
  285. uint32_t filter3_5:1;
  286. } PIOBUConfig;
  287. typedef struct {
  288. /**
  289. * @brief RAM Access Right
  290. */
  291. ram_access_mode_t mode;
  292. /* End of the mandatory fields.*/
  293. } RAMAccessConfig;
  294. /**
  295. * @brief Driver configuration structure.
  296. * @note It could be empty on some architectures.
  297. */
  298. typedef struct {
  299. /**
  300. * @brief Callback for memory violation
  301. */
  302. securam_callback_t securam_callback;
  303. /**
  304. * @brief Callback after memory erasing
  305. */
  306. erased_callback_t erased_callback;
  307. /**
  308. * @brief lenght of PIOBUConfig array
  309. * @note Number of pads to configure
  310. */
  311. size_t length;
  312. /**
  313. * @brief pointer to PIOBUConfig array
  314. */
  315. PIOBUConfig *list;
  316. /**
  317. * @brief RAM Access Rights
  318. */
  319. RAMAccessConfig region[SECUMOD_RAM_REGIONS];
  320. /**
  321. * @brief SECUMOD CR register initialization data.
  322. */
  323. uint32_t cr;
  324. /**
  325. * @brief SECUMOD JTAGCR register initialization data.
  326. */
  327. uint32_t jtagcr;
  328. } SECConfig;
  329. /**
  330. * @brief Structure representing a SEC driver.
  331. */
  332. struct SECDriver {
  333. /**
  334. * @brief Driver state.
  335. */
  336. secstate_t state;
  337. /**
  338. * @brief Current configuration data.
  339. */
  340. const SECConfig *config;
  341. /* End of the mandatory fields.*/
  342. /**
  343. * @brief Pointer to the SECUMOD registers block.
  344. */
  345. Secumod *sec;
  346. /**
  347. * @brief Callback pointer.
  348. */
  349. secumod_callback_t secumod_callback;
  350. };
  351. /*===========================================================================*/
  352. /* Driver macros. */
  353. /*===========================================================================*/
  354. /**
  355. * @brief Switch to Backup Mode.
  356. *
  357. * @api
  358. */
  359. #define secumodSwitch2BackupMode() { \
  360. SECUMOD->SECUMOD_CR = SECUMOD_CR_BACKUP; \
  361. }
  362. /**
  363. * @brief Switch to Normal Mode.
  364. *
  365. * @api
  366. */
  367. #define secumodSwitch2NormalMode() { \
  368. SECUMOD->SECUMOD_CR = SECUMOD_CR_NORMAL; \
  369. }
  370. /**
  371. * @brief Start clear content of SECUMOD internal RAM 4Kbyte and 256bits.
  372. *
  373. * @api
  374. */
  375. #define secumodSoftwareProtection() { \
  376. SECUMOD->SECUMOD_CR = SECUMOD_CR_SWPROT; \
  377. }
  378. /**
  379. * @brief Enable/Disable Auto-Backup.
  380. *
  381. * @param[in] enable Enable auto-backup if true, disable otherwise.
  382. *
  383. * @api
  384. */
  385. #define secumodSetAutoBackup(enable) { \
  386. if (enable) { \
  387. SECUMOD->SECUMOD_CR = SECUMOD_CR_AUTOBKP_AUTO_SWITCH; \
  388. } \
  389. else { \
  390. SECUMOD->SECUMOD_CR = SECUMOD_CR_AUTOBKP_SW_SWITCH; \
  391. } \
  392. }
  393. /**
  394. * @brief Enable/Disable Memory Scrambling.
  395. *
  396. * @param[in] enable Enable memory scrambling if true, disable otherwise.
  397. *
  398. * @api
  399. */
  400. #define secumodSetScrambling(enable) { \
  401. if (enable) { \
  402. SECUMOD->SECUMOD_CR = SECUMOD_CR_SCRAMB_ENABLE; \
  403. } \
  404. else { \
  405. SECUMOD->SECUMOD_CR = SECUMOD_CR_SCRAMB_DISABLE; \
  406. } \
  407. }
  408. /**
  409. * @brief Toggle normal or backup protection registers appear and disappear.
  410. *
  411. * @api
  412. */
  413. #define secumodToggleProtectionReg() { \
  414. SECUMOD->SECUMOD_CR = SECUMOD_CR_KEY_TOGGLE; \
  415. }
  416. /**
  417. * @brief Set scrambling key for secure RAM in SECUMOD.
  418. *
  419. * @param[in] key Scrambling key.
  420. *
  421. * @api
  422. */
  423. #define secumodSetScramblingKey(key) { \
  424. SECUMOD->SECUMOD_SCRKEY = key; \
  425. }
  426. /**
  427. * @brief Get scrambling key for secure RAM in SECUMOD.
  428. *
  429. * @return Scrambling key.
  430. *
  431. * @api
  432. */
  433. #define secumodGetScramblingKey() { \
  434. SECUMOD->SECUMOD_SCRKEY; \
  435. }
  436. /**
  437. * @brief Set protections enabled in backup mode.
  438. *
  439. * @note Make sure registers appears before call this function, to toggle the
  440. * appearance of the registers using secumodToggleProtectionReg().
  441. *
  442. * @param[in] sources Bitwise OR of protections.
  443. *
  444. * @api
  445. */
  446. #define secumodSetBackupModeProtections(sources) { \
  447. SECUMOD->SECUMOD_BMPR = sources; \
  448. if (SECUMOD->SECUMOD_BMPR != sources) { \
  449. secumodToggleProtectionReg(); \
  450. SECUMOD->SECUMOD_BMPR = sources; \
  451. } \
  452. }
  453. /**
  454. * @brief Set protections enabled in normal mode.
  455. *
  456. * @note Make sure registers appears before call this function, to toggle the
  457. * appearance of the registers using secumodToggleProtectionReg().
  458. *
  459. * @param[in] sources Bitwise OR of protections.
  460. *
  461. * @api
  462. */
  463. #define secumodSetNormalModeProtections(sources) { \
  464. SECUMOD->SECUMOD_NMPR = sources; \
  465. if (SECUMOD->SECUMOD_NMPR != sources) { \
  466. secumodToggleProtectionReg(); \
  467. SECUMOD->SECUMOD_NMPR = sources; \
  468. } \
  469. }
  470. /**
  471. * @brief Set protection sources which can cause wake up signal generated.
  472. *
  473. * @param[in] sources Bitwise OR of protection sources.
  474. *
  475. * @api
  476. */
  477. #define secumodSetWakeupProtections(sources) { \
  478. SECUMOD->SECUMOD_WKPR = sources; \
  479. }
  480. /**
  481. * @brief Wait availability status of memory.
  482. *
  483. * @api
  484. */
  485. #define secumodMemReady() { \
  486. while (!(SECUMOD->SECUMOD_RAMRDY & SECUMOD_RAMRDY_READY)); \
  487. }
  488. /*===========================================================================*/
  489. /* External declarations. */
  490. /*===========================================================================*/
  491. extern SECDriver SECD0;
  492. #ifdef __cplusplus
  493. extern "C" {
  494. #endif
  495. void secInit(void);
  496. void secObjectInit(SECDriver *secp);
  497. void secStart(SECDriver *secp, const SECConfig *config);
  498. void secStop(SECDriver *secp);
  499. void secSetCallback(SECDriver *secp, uint32_t sources, secumod_callback_t callback);
  500. void secumodSetJtagProtection(bool reset, uint8_t permissions, bool ack);
  501. void secumodDynamicSignaturesTuning(uint16_t period, uint8_t detectionThr, uint8_t resetThr);
  502. void secumodPeriodicAlarm(bool enable);
  503. void secumodSetRamAccessRights(uint32_t region, uint8_t rights);
  504. uint32_t secumodReadInternalMemory(uint8_t *data, uint32_t addr, uint32_t size);
  505. uint32_t secumodWriteInternalMemory(uint8_t *data, uint32_t addr, uint32_t size);
  506. #ifdef __cplusplus
  507. }
  508. #endif
  509. #endif /* SAMA_USE_SECUMOD */
  510. #endif /* SAMA_SECUMOD_LLD_H */
  511. /** @} */