hacc_mach.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* to avoid disclosing any secret and let customer know we have hacc hardware,
  2. the file name 'hacc' is changed to 'hacc_hw' in kernel driver */
  3. #ifndef HACC_MACH_H
  4. #define HACC_MACH_H
  5. #include "sec_osal_light.h"
  6. /******************************************************************************
  7. * CHIP SELECTION
  8. ******************************************************************************/
  9. /*
  10. #include <mach/mt_typedefs.h>
  11. #include <mach/mt_reg_base.h>
  12. #include <mach/mt_clkmgr.h>
  13. */
  14. #ifdef CONFIG_ARM64
  15. extern unsigned long long hacc_base;
  16. #else
  17. extern unsigned int hacc_base;
  18. #endif
  19. #if 0
  20. #ifdef CONFIG_ARM64
  21. extern unsigned long long es_base;
  22. #else
  23. extern unsigned int es_base;
  24. #endif
  25. #endif
  26. /******************************************************************************
  27. * MACROS DEFINITIONS
  28. ******************************************************************************/
  29. #define AES_BLK_SZ_ALIGN(size) ((size) & ~((AES_BLK_SZ << 3) - 1))
  30. /******************************************************************************
  31. * HARDWARE DEFINITIONS
  32. ******************************************************************************/
  33. #define HACC_CG (0x1 << 10)
  34. #define HACC_AES_TEST_SRC (0x02000000)
  35. #define HACC_AES_TEST_TMP (0x02100000)
  36. #define HACC_AES_TEST_DST (0x02200000)
  37. #define HACC_CFG_0 (0x5a5a3257) /* CHECKME */
  38. #define HACC_CFG_1 (0x66975412) /* CHECKME */
  39. #define HACC_CFG_2 (0x66975412) /* CHECKME */
  40. #define HACC_CFG_3 (0x5a5a3257) /* CHECKME */
  41. #define HACC_CON (hacc_base+0x0000)
  42. #define HACC_ACON (hacc_base+0x0004)
  43. #define HACC_ACON2 (hacc_base+0x0008)
  44. #define HACC_ACONK (hacc_base+0x000C)
  45. #define HACC_ASRC0 (hacc_base+0x0010)
  46. #define HACC_ASRC1 (hacc_base+0x0014)
  47. #define HACC_ASRC2 (hacc_base+0x0018)
  48. #define HACC_ASRC3 (hacc_base+0x001C)
  49. #define HACC_AKEY0 (hacc_base+0x0020)
  50. #define HACC_AKEY1 (hacc_base+0x0024)
  51. #define HACC_AKEY2 (hacc_base+0x0028)
  52. #define HACC_AKEY3 (hacc_base+0x002C)
  53. #define HACC_AKEY4 (hacc_base+0x0030)
  54. #define HACC_AKEY5 (hacc_base+0x0034)
  55. #define HACC_AKEY6 (hacc_base+0x0038)
  56. #define HACC_AKEY7 (hacc_base+0x003C)
  57. #define HACC_ACFG0 (hacc_base+0x0040)
  58. #define HACC_AOUT0 (hacc_base+0x0050)
  59. #define HACC_AOUT1 (hacc_base+0x0054)
  60. #define HACC_AOUT2 (hacc_base+0x0058)
  61. #define HACC_AOUT3 (hacc_base+0x005C)
  62. #define HACC_SW_OTP0 (hacc_base+0x0060)
  63. #define HACC_SW_OTP1 (hacc_base+0x0064)
  64. #define HACC_SW_OTP2 (hacc_base+0x0068)
  65. #define HACC_SW_OTP3 (hacc_base+0x006c)
  66. #define HACC_SW_OTP4 (hacc_base+0x0070)
  67. #define HACC_SW_OTP5 (hacc_base+0x0074)
  68. #define HACC_SW_OTP6 (hacc_base+0x0078)
  69. #define HACC_SW_OTP7 (hacc_base+0x007c)
  70. #define HACC_SECINIT0 (hacc_base+0x0080)
  71. #define HACC_SECINIT1 (hacc_base+0x0084)
  72. #define HACC_SECINIT2 (hacc_base+0x0088)
  73. #define HACC_MKJ (hacc_base+0x00a0)
  74. /* AES */
  75. #define HACC_AES_DEC 0x00000000
  76. #define HACC_AES_ENC 0x00000001
  77. #define HACC_AES_MODE_MASK 0x00000002
  78. #define HACC_AES_ECB 0x00000000
  79. #define HACC_AES_CBC 0x00000002
  80. #define HACC_AES_TYPE_MASK 0x00000030
  81. #define HACC_AES_128 0x00000000
  82. #define HACC_AES_192 0x00000010
  83. #define HACC_AES_256 0x00000020
  84. #define HACC_AES_CHG_BO_MASK 0x00001000
  85. #define HACC_AES_CHG_BO_OFF 0x00000000
  86. #define HACC_AES_CHG_BO_ON 0x00001000
  87. #define HACC_AES_START 0x00000001
  88. #define HACC_AES_CLR 0x00000002
  89. #define HACC_AES_RDY 0x00008000
  90. /* AES key relevant */
  91. #define HACC_AES_BK2C 0x00000010
  92. #define HACC_AES_R2K 0x00000100
  93. /* SECINIT magic */
  94. #define HACC_SECINIT0_MAGIC 0xAE0ACBEA
  95. #define HACC_SECINIT1_MAGIC 0xCD957018
  96. #define HACC_SECINIT2_MAGIC 0x46293911
  97. /******************************************************************************
  98. * CONSTANT DEFINITIONS
  99. ******************************************************************************/
  100. #define HACC_AES_MAX_KEY_SZ (32)
  101. #define AES_CFG_SZ (16)
  102. #define AES_BLK_SZ (16)
  103. #define HACC_HW_KEY_SZ (16)
  104. #define _CRYPTO_SEED_LEN (16)
  105. /* In order to support NAND writer and keep MTK secret,
  106. use MTK HACC seed and custom crypto seed to generate SW key
  107. to encrypt SEC_CFG */
  108. #define MTK_HACC_SEED (0x1)
  109. /******************************************************************************
  110. * TYPE DEFINITIONS
  111. ******************************************************************************/
  112. typedef enum {
  113. AES_ECB_MODE,
  114. AES_CBC_MODE
  115. } AES_MODE;
  116. typedef enum {
  117. AES_DEC,
  118. AES_ENC
  119. } AES_OPS;
  120. typedef enum {
  121. AES_KEY_128 = 16,
  122. AES_KEY_192 = 24,
  123. AES_KEY_256 = 32
  124. } AES_KEY;
  125. typedef enum {
  126. AES_SW_KEY,
  127. AES_HW_KEY,
  128. AES_HW_WRAP_KEY
  129. } AES_KEY_ID;
  130. typedef struct {
  131. unsigned char config[AES_CFG_SZ];
  132. } AES_CFG;
  133. typedef struct {
  134. unsigned int size;
  135. unsigned char seed[HACC_AES_MAX_KEY_SZ];
  136. } AES_KEY_SEED;
  137. struct hacc_context {
  138. AES_CFG cfg;
  139. unsigned int blk_sz;
  140. unsigned char sw_key[HACC_AES_MAX_KEY_SZ];
  141. unsigned char hw_key[HACC_AES_MAX_KEY_SZ];
  142. };
  143. /* --------------------------------------------------------------------------- */
  144. /* Register Manipulations */
  145. /* --------------------------------------------------------------------------- */
  146. #define READ_REGISTER_UINT32(reg) \
  147. (*(volatile unsigned int * const)(reg))
  148. #define WRITE_REGISTER_UINT32(reg, val) \
  149. ((*(volatile unsigned int * const)(reg)) = (val))
  150. #define INREG32(x) READ_REGISTER_UINT32((unsigned int *)((void *)(x)))
  151. #define OUTREG32(x, y) WRITE_REGISTER_UINT32((unsigned int *)((void *)(x)), (unsigned int)(y))
  152. #define SETREG32(x, y) OUTREG32(x, INREG32(x)|(y))
  153. #define CLRREG32(x, y) OUTREG32(x, INREG32(x)&~(y))
  154. #define MASKREG32(x, y, z) OUTREG32(x, (INREG32(x)&~(y))|(z))
  155. #define DRV_Reg32(addr) INREG32(addr)
  156. #define DRV_WriteReg32(addr, data) OUTREG32(addr, data)
  157. #define DRV_SetReg32(addr, data) SETREG32(addr, data)
  158. #define DRV_ClrReg32(addr, data) CLRREG32(addr, data)
  159. /******************************************************************************
  160. * EXPORT FUNCTION
  161. ******************************************************************************/
  162. extern unsigned int hacc_set_key(AES_KEY_ID id, AES_KEY key);
  163. extern unsigned int hacc_do_aes(AES_OPS ops, unsigned char *src, unsigned char *dst,
  164. unsigned int size);
  165. extern unsigned int hacc_init(AES_KEY_SEED *keyseed);
  166. extern unsigned int hacc_deinit(void);
  167. extern void HACC_V3_Init(bool encode, const unsigned int g_AC_CFG[]);
  168. extern void HACC_V3_Run(volatile unsigned int *p_src, unsigned int src_len,
  169. volatile unsigned int *p_dst);
  170. extern void HACC_V3_Terminate(void);
  171. /******************************************************************************
  172. * EXTERNAL VARIABLE
  173. ******************************************************************************/
  174. extern bool bHACC_HWWrapKeyInit;
  175. extern bool bHACC_SWKeyInit;
  176. #endif