aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/ports/SAMA/SAMA5D2x/sama_secumod.h
blob: 5ac8c3c115ef7814bccbf0d3992c43f8831ded43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*
    ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

/**
 * @file    SAMA5D2x/sama_secumod.h
 * @brief   SAMA SECUMOD support macros and structures.
 *
 * @addtogroup SAMA5D2x_SECUMOD
 * @{
 */

#ifndef SAMA_SECUMOD_LLD_H
#define SAMA_SECUMOD_LLD_H

#if HAL_USE_SECUMOD || defined(__DOXYGEN__)

#include <string.h>
/*===========================================================================*/
/* Driver constants.                                                         */
/*===========================================================================*/
/**
 * @name    RAM ACCESS mode macros
 * @{
 */
/**
 * @brief   No access allowed.
 */
#define RAMACC_NO_ACCESS                        0x0U

/**
 * @brief   Only write access allowed.
 */
#define RAMACC_WR_ACCESS                        0x1U

/**
 * @brief   Only read access allowed.
 */
#define RAMACC_RD_ACCESS                        0x2U

/**
 * @brief   Read and Write access allowed.
 */
#define RAMACC_WR_RD_ACCESS                     0x3U
/** @} */

/**
 * @name    SOURCE INTERRUPT macros
 * @{
 */
/*
 * @brief Shield Monitor Protection Interrupt Source.
 */
#define SECUMOD_SHLDM                           (0x1u << 0)

/*
 * @brief Double Frequency Monitor Protection Interrupt Source.
 */
#define SECUMOD_DBLFM                           (0x1u << 1)

/*
 * @brief Test Pin Protection Interrupt Source.
 */
#define SECUMOD_TST                             (0x1u << 2)

/*
 * @brief JTAG Pins Protection Interrupt Source.
 */
#define SECUMOD_JTAG                            (0x1u << 3)

/*
 * @brief Master Clock Monitor Protection Interrupt Source.
 */
#define SECUMOD_MCKM                            (0x1u << 5)

/*
 * @brief Low Temperature Monitor Protection Interrupt Source.
 */
#define SECUMOD_TPML                            (0x1u << 6)

/*
 * @brief High Temperature Monitor Protection Interrupt Source.
 */
#define SECUMOD_TPMH                            (0x1u << 7)

/*
 * @brief Low VDDBU Voltage Monitor Protection Interrupt Source.
 */
#define SECUMOD_VDDBUL                          (0x1u << 10)

/*
 * @brief High VDDBU Voltage Monitor Protection Interrupt Source.
 */
#define SECUMOD_VDDBUH                          (0x1u << 11)

/*
 * @brief Low VDDCORE Voltage Monitor Protection Interrupt Source.
 */
#define SECUMOD_VDDCOREL                        (0x1u << 12)

/*
 * @brief High VDDCORE Voltage Monitor Protection Interrupt Source.
 */
#define SECUMOD_VDDCOREH                        (0x1u << 13)

/*
 * @brief PIOBUx Intrusion Detector Protection Interrupt Source.
 */
#define SECUMOD_DET0                            (0x1u << 16)
#define SECUMOD_DET1                            (0x1u << 17)
#define SECUMOD_DET2                            (0x1u << 18)
#define SECUMOD_DET3                            (0x1u << 19)
#define SECUMOD_DET4                            (0x1u << 20)
#define SECUMOD_DET5                            (0x1u << 21)
#define SECUMOD_DET6                            (0x1u << 22)
#define SECUMOD_DET7                            (0x1u << 23)
/** @} */

/**
 * @name    RAM STATUS mode macros
 * @{
 */
/**
 * @brief   No access violation occurred.
 */
#define RAMACCSR_NO_VIOLATION                   0x0U

/**
 * @brief   Write access violation occurred.
 */
#define RAMACCSR_WR_VIOLATION                   0x1U

/**
 * @brief   Read access violation occurred.
 */
#define RAMACCSR_RD_VIOLATION                   0x2U

/**
 * @brief   Read and Write access violation occurred.
 */
#define RAMACCSR_WR_RD_VIOLATION                0x3U
/** @} */

/**
 * @name    SCRAMB mode macros
 * @{
 */
/**
 * @brief   SCRAMB ENABLE.
 */
#define SCRAMB_ENABLE                           0x1U

/**
 * @brief   SCRAMB DISABLE.
 */
#define SCRAMB_DISABLE                          0x2U
/** @} */

/*
 * @brief RAM regions of SECUMOD
 */
#define SECUMOD_RAM_REGIONS      6

/*===========================================================================*/
/* Driver pre-compile time settings.                                         */
/*===========================================================================*/
/**
 * @name    Configuration options
 * @{
 */
/**
 * @brief   SECUMOD interrupt priority level setting.
 */
#if !defined(SAMA_SECUMOD_IRQ_PRIORITY) || defined(__DOXYGEN__)
#define SAMA_SECUMOD_IRQ_PRIORITY           7
#endif

/**
 * @brief   SECURAM interrupt priority level setting.
 */
#if !defined(SAMA_SECURAM_IRQ_PRIORITY) || defined(__DOXYGEN__)
#define SAMA_SECURAM_IRQ_PRIORITY           7
#endif

/** @} */
/*===========================================================================*/
/* Derived constants and error checks.                                       */
/*===========================================================================*/

/*===========================================================================*/
/* Driver data structures and types.                                         */
/*===========================================================================*/
/**
 * @brief   Driver state machine possible states.
 */
typedef enum {
  SEC_UNINIT = 0,                   /**< Not initialized.                   */
  SEC_STOP = 1,                     /**< Stopped.                           */
  SEC_ACTIVE = 2                    /**< Active.                            */
} secstate_t;

/**
  * @brief   Type of an SECUMOD event.
  */
typedef enum {
  SEC_EVENT_SHLDM    = 0,           /* Triggered on Shield Monitor.               */
  SEC_EVENT_DBLFM    = 1,           /* Triggered on Double Frequency Monitor.     */
  SEC_EVENT_TST      = 2,           /* Triggered on Test Pin Monitor.             */
  SEC_EVENT_JTAG     = 3,           /* Triggered on JTAG Pins Monitor.            */
  SEC_EVENT_MCKM     = 4,           /* Triggered on Master Clock Monitor.         */
  SEC_EVENT_TPML     = 5,           /* Triggered on Low Temperature Monitor.      */
  SEC_EVENT_TPMH     = 6,           /* Triggered on High Temperature Monitor.     */
  SEC_EVENT_VDDBUL   = 7,           /* Triggered on Low VDDBU Voltage Monitor.    */
  SEC_EVENT_VDDBUH   = 8,           /* Triggered on High VDDBU Voltage Monitor.   */
  SEC_EVENT_VDDCOREL = 9,           /* Triggered on Low VDDCORE Voltage Monitor.  */
  SEC_EVENT_VDDCOREH = 10,          /* Triggered on High VDDCORE Voltage Monitor. */
  SEC_EVENT_PIOBU0   = 11,          /* Triggered on PIOBUx intrusion.             */
  SEC_EVENT_PIOBU1   = 12,          /* Triggered on PIOBUx intrusion.             */
  SEC_EVENT_PIOBU2   = 13,          /* Triggered on PIOBUx intrusion.             */
  SEC_EVENT_PIOBU3   = 14,          /* Triggered on PIOBUx intrusion.             */
  SEC_EVENT_PIOBU4   = 15,          /* Triggered on PIOBUx intrusion.             */
  SEC_EVENT_PIOBU5   = 16,          /* Triggered on PIOBUx intrusion.             */
  SEC_EVENT_PIOBU6   = 17,          /* Triggered on PIOBUx intrusion.             */
  SEC_EVENT_PIOBU7   = 18           /* Triggered on PIOBUx intrusion.             */
} secevent_t;

/**
 * @brief   Type of a structure representing an SEC driver.
 */
typedef struct SECDriver SECDriver;

/**
 * @brief   SECURAM notification callback type.
 *
 * @param[in] secp      pointer to a @p SECDriver object
 */
typedef void (*securam_callback_t)(SECDriver *secp);

/**
 * @brief   SECUMOD notification callback type.
 *
 * @param[in] secp      pointer to a @p SECDriver object
 */
typedef void (*secumod_callback_t)(SECDriver *secp, secevent_t event);

/**
 * @brief   SECUMOD erase callback type.
 *
 * @param[in] secp      pointer to a @p SECDriver object
 */
typedef void (*erased_callback_t)(SECDriver *secp);

/**
 * @brief   Type of RAM access mode.
 */
typedef uint32_t ram_access_mode_t;

/**
 * @brief   PIOBU configuration structure.
 */
typedef struct {
  /*
   * @brief PIOBU pin's index
   */
  uint32_t pinIndex:3;
  /*
   * @brief alarm filter value
   */
  uint32_t afv:4;
  /*
   * @brief reset filter value
   */
  uint32_t rfv:4;
  /*
   * @brief I/O line mode
   * @note 0: pure input, 1: enabled in output
   */
  uint32_t mode:1;
  /*
   * @brief Configure the I/O line in output mode
   * @note 0: clear, 1: set
   */
  uint32_t outputLevel:1;
  /*
   * @brief programmable pull-up state
   * @note 0: none, 1: pull-up; 2: pull-down; 3: reserved
   */
  uint32_t pullUpState:2;
  /*
   * @brief Pull-up/Down Scheduled:
   * @note 0: no; 1: yes
   */
  uint32_t scheduled:1;
  /*
   * @brief switch input default state
   */
  uint32_t inputDefaultLevel:1;
  /*
   * @brief Mode of detection intrusion.
   * @note 0: static, 1: dynamic
   */
  uint32_t dynamic:1;
  /*
   * @brief filter for dynamic signatures input
   * @note 0: 3 stages majority vote, 1: 5 stages
   */
  uint32_t filter3_5:1;
} PIOBUConfig;

typedef struct {
  /**
   * @brief RAM Access Right
   */
  ram_access_mode_t         mode;
  /* End of the mandatory fields.*/
} RAMAccessConfig;

/**
 * @brief   Driver configuration structure.
 * @note    It could be empty on some architectures.
 */
typedef struct {
  /**
   * @brief Callback for memory violation
   */
  securam_callback_t        securam_callback;
  /**
   * @brief Callback after memory erasing
   */
  erased_callback_t          erased_callback;
  /**
   * @brief lenght of PIOBUConfig array
   * @note  Number of pads to configure
   */
  size_t                    length;
  /**
   * @brief pointer to PIOBUConfig array
   */
  PIOBUConfig               *list;
  /**
   * @brief RAM Access Rights
   */
  RAMAccessConfig           region[SECUMOD_RAM_REGIONS];
  /**
   * @brief SECUMOD CR register initialization data.
   */
  uint32_t                  cr;
  /**
   * @brief SECUMOD JTAGCR register initialization data.
   */
  uint32_t                  jtagcr;
} SECConfig;

/**
 * @brief   Structure representing a SEC driver.
 */
struct SECDriver {
  /**
   * @brief Driver state.
   */
  secstate_t                state;
  /**
   * @brief Current configuration data.
   */
  const SECConfig           *config;
  /* End of the mandatory fields.*/
  /**
   * @brief Pointer to the SECUMOD registers block.
   */
  Secumod                   *sec;
  /**
   * @brief   Callback pointer.
   */
  secumod_callback_t        secumod_callback;
};

/*===========================================================================*/
/* Driver macros.                                                            */
/*===========================================================================*/
/**
 * @brief   Switch to Backup Mode.
 *
 * @api
 */
#define secumodSwitch2BackupMode() {                                          \
  SECUMOD->SECUMOD_CR = SECUMOD_CR_BACKUP;                                    \
}

/**
 * @brief   Switch to Normal Mode.
 *
 * @api
 */
#define secumodSwitch2NormalMode() {                                           \
  SECUMOD->SECUMOD_CR = SECUMOD_CR_NORMAL;                                     \
}

/**
 * @brief   Start clear content of SECUMOD internal RAM 4Kbyte and 256bits.
 *
 * @api
 */
#define secumodSoftwareProtection() {                                          \
  SECUMOD->SECUMOD_CR = SECUMOD_CR_SWPROT;                                     \
}

/**
 * @brief Enable/Disable Auto-Backup.
 *
 * @param[in] enable Enable auto-backup if true, disable otherwise.
 *
 * @api
 */
#define secumodSetAutoBackup(enable) {                                         \
  if (enable) {                                                                \
    SECUMOD->SECUMOD_CR = SECUMOD_CR_AUTOBKP_AUTO_SWITCH;                      \
  }                                                                            \
  else {                                                                       \
    SECUMOD->SECUMOD_CR = SECUMOD_CR_AUTOBKP_SW_SWITCH;                        \
  }                                                                            \
}

/**
 * @brief Enable/Disable Memory Scrambling.
 *
 * @param[in] enable Enable memory scrambling if true, disable otherwise.
 *
 * @api
 */
#define secumodSetScrambling(enable) {                                         \
  if (enable) {                                                                \
    SECUMOD->SECUMOD_CR = SECUMOD_CR_SCRAMB_ENABLE;                            \
    }                                                                          \
  else {                                                                       \
    SECUMOD->SECUMOD_CR = SECUMOD_CR_SCRAMB_DISABLE;                           \
  }                                                                            \
}

/**
 * @brief Toggle normal or backup protection registers appear and disappear.
 *
 * @api
 */
#define secumodToggleProtectionReg() {                                         \
  SECUMOD->SECUMOD_CR = SECUMOD_CR_KEY_TOGGLE;                                 \
}

/**
 * @brief Set scrambling key for secure RAM in SECUMOD.
 *
 * @param[in] key Scrambling key.
 *
 * @api
 */
#define secumodSetScramblingKey(key) {                                         \
  SECUMOD->SECUMOD_SCRKEY = key;                                               \
}

/**
 * @brief Get scrambling key for secure RAM in SECUMOD.
 *
 * @return Scrambling key.
 *
 * @api
 */
#define secumodGetScramblingKey() {                                            \
  SECUMOD->SECUMOD_SCRKEY;                                                     \
}

/**
 * @brief Set protections enabled in backup mode.
 *
 * @note Make sure registers appears before call this function, to toggle the
 *       appearance of the registers using secumodToggleProtectionReg().
 *
 * @param[in] sources Bitwise OR of protections.
 *
 * @api
 */
#define secumodSetBackupModeProtections(sources) {                             \
  SECUMOD->SECUMOD_BMPR = sources;                                             \
  if (SECUMOD->SECUMOD_BMPR != sources) {                                      \
    secumodToggleProtectionReg();                                              \
    SECUMOD->SECUMOD_BMPR = sources;                                           \
  }                                                                            \
}

/**
 * @brief Set protections enabled in normal mode.
 *
 * @note Make sure registers appears before call this function, to toggle the
 *       appearance of the registers using secumodToggleProtectionReg().
 *
 * @param[in] sources Bitwise OR of protections.
 *
 * @api
 */
#define secumodSetNormalModeProtections(sources) {                             \
  SECUMOD->SECUMOD_NMPR = sources;                                             \
  if (SECUMOD->SECUMOD_NMPR != sources) {                                      \
    secumodToggleProtectionReg();                                              \
    SECUMOD->SECUMOD_NMPR = sources;                                           \
  }                                                                            \
}

/**
 * @brief Set protection sources which can cause wake up signal generated.
 *
 * @param[in] sources Bitwise OR of protection sources.
 *
 * @api
 */
#define secumodSetWakeupProtections(sources) {                                 \
  SECUMOD->SECUMOD_WKPR = sources;                                             \
}

/**
 * @brief Wait availability status of memory.
 *
 * @api
 */
#define secumodMemReady() {                                                    \
  while (!(SECUMOD->SECUMOD_RAMRDY & SECUMOD_RAMRDY_READY));                   \
}

/*===========================================================================*/
/* External declarations.                                                    */
/*===========================================================================*/
extern SECDriver SECD0;

#ifdef __cplusplus
extern "C" {
#endif
  void secInit(void);
  void secObjectInit(SECDriver *secp);
  void secStart(SECDriver *secp, const SECConfig *config);
  void secStop(SECDriver *secp);
  void secSetCallback(SECDriver *secp, uint32_t sources, secumod_callback_t callback);
  void secumodSetJtagProtection(bool reset, uint8_t permissions, bool ack);
  void secumodDynamicSignaturesTuning(uint16_t period, uint8_t detectionThr, uint8_t resetThr);
  void secumodPeriodicAlarm(bool enable);
  void secumodSetRamAccessRights(uint32_t region, uint8_t rights);
  uint32_t secumodReadInternalMemory(uint8_t *data, uint32_t addr, uint32_t size);
  uint32_t secumodWriteInternalMemory(uint8_t *data, uint32_t addr, uint32_t size);
#ifdef __cplusplus
}
#endif

#endif /* HAL_USE_SECUMOD */

#endif /* SAMA_SECUMOD_LLD_H */

/** @} */