sotp.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (c) 2016-2020, Broadcom
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <string.h>
  7. #include <common/debug.h>
  8. #include <lib/mmio.h>
  9. #include <sotp.h>
  10. #include <platform_def.h>
  11. #include <platform_sotp.h>
  12. #ifdef USE_SOFT_SOTP
  13. extern uint64_t soft_sotp[];
  14. #endif
  15. #define SOTP_PROG_CONTROL (SOTP_REGS_OTP_BASE + 0x0000)
  16. #define SOTP_PROG_CONTROL__OTP_CPU_MODE_EN 15
  17. #define SOTP_PROG_CONTROL__OTP_DISABLE_ECC 9
  18. #define SOTP_PROG_CONTROL__OTP_ECC_WREN 8
  19. #define SOTP_WRDATA_0 (SOTP_REGS_OTP_BASE + 0x0004)
  20. #define SOTP_WRDATA_1 (SOTP_REGS_OTP_BASE + 0x0008)
  21. #define SOTP_ADDR (SOTP_REGS_OTP_BASE + 0x000c)
  22. #define SOTP_ADDR__OTP_ROW_ADDR_R 6
  23. #define SOTP_ADDR_MASK 0x3FF
  24. #define SOTP_CTRL_0 (SOTP_REGS_OTP_BASE + 0x0010)
  25. #define SOTP_CTRL_0__START 0
  26. #define SOTP_CTRL_0__OTP_CMD 1
  27. #define SOTP_STATUS_0 (SOTP_REGS_OTP_BASE + 0x0018)
  28. #define SOTP_STATUS__FDONE 3
  29. #define SOTP_STATUS_1 (SOTP_REGS_OTP_BASE + 0x001c)
  30. #define SOTP_STATUS_1__CMD_DONE 1
  31. #define SOTP_STATUS_1__ECC_DET 17
  32. #define SOTP_RDDATA_0 (SOTP_REGS_OTP_BASE + 0x0020)
  33. #define SOTP_RDDATA_1 (SOTP_REGS_OTP_BASE + 0x0024)
  34. #define SOTP_READ 0
  35. #define SOTP_PROG_WORD 10
  36. #define SOTP_STATUS__PROGOK 2
  37. #define SOTP_PROG_ENABLE 2
  38. #define SOTP_ROW_DATA_MASK 0xffffffff
  39. #define SOTP_ECC_ERR_BITS_MASK 0x1ff00000000
  40. #define SOTP_CHIP_CTRL_SW_OVERRIDE_CHIP_STATES 4
  41. #define SOTP_CHIP_CTRL_SW_MANU_PROG 5
  42. #define SOTP_CHIP_CTRL_SW_CID_PROG 6
  43. #define SOTP_CHIP_CTRL_SW_AB_DEVICE 8
  44. #define SOTP_CHIP_CTRL_SW_AB_DEV_MODE 9
  45. #define CHIP_STATE_UNPROGRAMMED 0x1
  46. #define CHIP_STATE_UNASSIGNED 0x2
  47. uint64_t sotp_mem_read(uint32_t offset, uint32_t sotp_add_ecc)
  48. {
  49. #ifdef USE_SOFT_SOTP
  50. (void)sotp_add_ecc;
  51. return soft_sotp[offset];
  52. #else
  53. uint64_t read_data = 0;
  54. uint64_t read_data1 = 0;
  55. uint64_t read_data2 = 0;
  56. /* Check for FDONE status */
  57. while ((mmio_read_32(SOTP_STATUS_0) & BIT(SOTP_STATUS__FDONE)) !=
  58. BIT(SOTP_STATUS__FDONE))
  59. ;
  60. /* Enable OTP access by CPU */
  61. mmio_setbits_32(SOTP_PROG_CONTROL,
  62. BIT(SOTP_PROG_CONTROL__OTP_CPU_MODE_EN));
  63. if (sotp_add_ecc == 1) {
  64. mmio_clrbits_32(SOTP_PROG_CONTROL,
  65. BIT(SOTP_PROG_CONTROL__OTP_DISABLE_ECC));
  66. }
  67. if (sotp_add_ecc == 0) {
  68. mmio_setbits_32(SOTP_PROG_CONTROL,
  69. BIT(SOTP_PROG_CONTROL__OTP_DISABLE_ECC));
  70. }
  71. mmio_write_32(SOTP_ADDR,
  72. ((offset & SOTP_ADDR_MASK) << SOTP_ADDR__OTP_ROW_ADDR_R));
  73. mmio_write_32(SOTP_CTRL_0, (SOTP_READ << SOTP_CTRL_0__OTP_CMD));
  74. /* Start bit to tell SOTP to send command to the OTP controller */
  75. mmio_setbits_32(SOTP_CTRL_0, BIT(SOTP_CTRL_0__START));
  76. /* Wait for SOTP command done to be set */
  77. while ((mmio_read_32(SOTP_STATUS_1) & BIT(SOTP_STATUS_1__CMD_DONE)) !=
  78. BIT(SOTP_STATUS_1__CMD_DONE))
  79. ;
  80. /* Clr Start bit after command done */
  81. mmio_clrbits_32(SOTP_CTRL_0, BIT(SOTP_CTRL_0__START));
  82. if ((offset > SOTP_DEVICE_SECURE_CFG3_ROW) &&
  83. (mmio_read_32(SOTP_STATUS_1) & BIT(SOTP_STATUS_1__ECC_DET))) {
  84. ERROR("SOTP ECC ERROR Detected row offset %d\n", offset);
  85. read_data = SOTP_ECC_ERR_DETECT;
  86. } else {
  87. read_data1 = (uint64_t)mmio_read_32(SOTP_RDDATA_0);
  88. read_data1 = read_data1 & 0xFFFFFFFF;
  89. read_data2 = (uint64_t)mmio_read_32(SOTP_RDDATA_1);
  90. read_data2 = (read_data2 & 0x1ff) << 32;
  91. read_data = read_data1 | read_data2;
  92. }
  93. /* Command done is cleared */
  94. mmio_setbits_32(SOTP_STATUS_1, BIT(SOTP_STATUS_1__CMD_DONE));
  95. /* disable OTP access by CPU */
  96. mmio_clrbits_32(SOTP_PROG_CONTROL,
  97. BIT(SOTP_PROG_CONTROL__OTP_CPU_MODE_EN));
  98. return read_data;
  99. #endif
  100. }
  101. void sotp_mem_write(uint32_t addr, uint32_t sotp_add_ecc, uint64_t wdata)
  102. {
  103. #ifdef USE_SOFT_SOTP
  104. (void)sotp_add_ecc;
  105. soft_sotp[addr] = wdata;
  106. #else
  107. uint32_t loop;
  108. uint8_t prog_array[4] = { 0x0F, 0x04, 0x08, 0x0D };
  109. uint32_t chip_state_default =
  110. (CHIP_STATE_UNASSIGNED|CHIP_STATE_UNPROGRAMMED);
  111. uint32_t chip_state = mmio_read_32(SOTP_REGS_SOTP_CHIP_STATES);
  112. uint32_t chip_ctrl_default = 0;
  113. /*
  114. * The override settings is required to allow the customer to program
  115. * the application specific keys into SOTP, before the conversion to
  116. * one of the AB modes.
  117. * At the end of write operation, the chip ctrl settings will restored
  118. * to the state prior to write call
  119. */
  120. if (chip_state & chip_state_default) {
  121. uint32_t chip_ctrl;
  122. chip_ctrl_default = mmio_read_32(SOTP_CHIP_CTRL);
  123. INFO("SOTP: enable special prog mode\n");
  124. chip_ctrl = BIT(SOTP_CHIP_CTRL_SW_OVERRIDE_CHIP_STATES) |
  125. BIT(SOTP_CHIP_CTRL_SW_MANU_PROG) |
  126. BIT(SOTP_CHIP_CTRL_SW_CID_PROG) |
  127. BIT(SOTP_CHIP_CTRL_SW_AB_DEVICE);
  128. mmio_write_32(SOTP_CHIP_CTRL, chip_ctrl);
  129. }
  130. /* Check for FDONE status */
  131. while ((mmio_read_32(SOTP_STATUS_0) & BIT(SOTP_STATUS__FDONE)) !=
  132. BIT(SOTP_STATUS__FDONE))
  133. ;
  134. /* Enable OTP access by CPU */
  135. mmio_setbits_32(SOTP_PROG_CONTROL,
  136. BIT(SOTP_PROG_CONTROL__OTP_CPU_MODE_EN));
  137. if (addr > SOTP_DEVICE_SECURE_CFG3_ROW) {
  138. if (sotp_add_ecc == 0) {
  139. mmio_clrbits_32(SOTP_PROG_CONTROL,
  140. BIT(SOTP_PROG_CONTROL__OTP_ECC_WREN));
  141. }
  142. if (sotp_add_ecc == 1) {
  143. mmio_setbits_32(SOTP_PROG_CONTROL,
  144. BIT(SOTP_PROG_CONTROL__OTP_ECC_WREN));
  145. }
  146. } else {
  147. mmio_clrbits_32(SOTP_PROG_CONTROL,
  148. BIT(SOTP_PROG_CONTROL__OTP_ECC_WREN));
  149. }
  150. mmio_write_32(SOTP_CTRL_0, (SOTP_PROG_ENABLE << 1));
  151. /*
  152. * In order to avoid unintentional writes / programming of the OTP
  153. * array, the OTP Controller must be put into programming mode before
  154. * it will accept program commands. This is done by writing 0xF, 0x4,
  155. * 0x8, 0xD with program commands prior to starting the actual
  156. * programming sequence
  157. */
  158. for (loop = 0; loop < 4; loop++) {
  159. mmio_write_32(SOTP_WRDATA_0, prog_array[loop]);
  160. /*
  161. * Start bit to tell SOTP to send command to the OTP controller
  162. */
  163. mmio_setbits_32(SOTP_CTRL_0, BIT(SOTP_CTRL_0__START));
  164. /* Wait for SOTP command done to <-- be set */
  165. while ((mmio_read_32(SOTP_STATUS_1) &
  166. BIT(SOTP_STATUS_1__CMD_DONE)) !=
  167. BIT(SOTP_STATUS_1__CMD_DONE))
  168. ;
  169. /* Command done is cleared w1c */
  170. mmio_setbits_32(SOTP_STATUS_1, BIT(SOTP_STATUS_1__CMD_DONE));
  171. /* Clr Start bit after command done */
  172. mmio_clrbits_32(SOTP_CTRL_0, BIT(SOTP_CTRL_0__START));
  173. }
  174. /* Check for PROGOK */
  175. while ((mmio_read_32(SOTP_STATUS_0) & 0x4) != BIT(SOTP_STATUS__PROGOK))
  176. ;
  177. /* Set 10 bit row address */
  178. mmio_write_32(SOTP_ADDR,
  179. ((addr & SOTP_ADDR_MASK) << SOTP_ADDR__OTP_ROW_ADDR_R));
  180. /* Set SOTP Row data */
  181. mmio_write_32(SOTP_WRDATA_0, (wdata & SOTP_ROW_DATA_MASK));
  182. /* Set SOTP ECC and error bits */
  183. mmio_write_32(SOTP_WRDATA_1, ((wdata & SOTP_ECC_ERR_BITS_MASK) >> 32));
  184. /* Set prog_word command */
  185. mmio_write_32(SOTP_CTRL_0, (SOTP_PROG_WORD << 1));
  186. /* Start bit to tell SOTP to send command to the OTP controller */
  187. mmio_setbits_32(SOTP_CTRL_0, BIT(SOTP_CTRL_0__START));
  188. /* Wait for SOTP command done to be set */
  189. while ((mmio_read_32(SOTP_STATUS_1) & BIT(SOTP_STATUS_1__CMD_DONE)) !=
  190. BIT(SOTP_STATUS_1__CMD_DONE))
  191. ;
  192. /* Command done is cleared w1c */
  193. mmio_setbits_32(SOTP_STATUS_1, BIT(SOTP_STATUS_1__CMD_DONE));
  194. /* disable OTP access by CPU */
  195. mmio_clrbits_32(SOTP_PROG_CONTROL,
  196. BIT(SOTP_PROG_CONTROL__OTP_CPU_MODE_EN));
  197. /* Clr Start bit after command done */
  198. mmio_clrbits_32(SOTP_CTRL_0, BIT(SOTP_CTRL_0__START));
  199. if (chip_state & chip_state_default)
  200. mmio_write_32(SOTP_CHIP_CTRL, chip_ctrl_default);
  201. #endif
  202. }
  203. int sotp_read_key(uint8_t *key, size_t keysize, int start_row, int end_row)
  204. {
  205. int row;
  206. uint32_t status = 0;
  207. uint32_t status2 = 0xFFFFFFFF;
  208. uint64_t row_data;
  209. uint32_t data;
  210. uint32_t *temp_key = (uint32_t *)key;
  211. row = start_row;
  212. while ((keysize > 0) && (row <= end_row)) {
  213. row_data = sotp_mem_read(row, SOTP_ROW_ECC);
  214. if (!(row_data & (SOTP_ECC_ERR_DETECT | SOTP_FAIL_BITS))) {
  215. memcpy(temp_key++, &row_data, sizeof(uint32_t));
  216. keysize -= sizeof(uint32_t);
  217. data = (uint32_t)(row_data & SOTP_ROW_DATA_MASK);
  218. status |= data;
  219. status2 &= data;
  220. }
  221. row++;
  222. }
  223. if ((status2 == 0xFFFFFFFF) || (status == 0) || (row > end_row))
  224. return -1;
  225. return 0;
  226. }
  227. int sotp_key_erased(void)
  228. {
  229. uint64_t row_data;
  230. int status = 0;
  231. row_data = sotp_mem_read(SOTP_DEVICE_SECURE_CFG0_ROW, 0);
  232. if (row_data & SOTP_DEVICE_SECURE_CFG0_OTP_ERASED_MASK)
  233. status = 1;
  234. else if (mmio_read_32(SOTP_REGS_SOTP_CHIP_STATES) &
  235. SOTP_REGS_SOTP_CHIP_STATES_OTP_ERASED_MASK)
  236. status = 1;
  237. return status;
  238. }
  239. /*
  240. * This function optimise the SOTP redundancy
  241. * by considering the 00- zero and 01,10,11 - one
  242. */
  243. uint32_t sotp_redundancy_reduction(uint32_t sotp_row_data)
  244. {
  245. uint32_t opt_data;
  246. uint32_t opt_loop;
  247. uint32_t temp_data;
  248. opt_data = 0;
  249. for (opt_loop = 0; opt_loop < 16; opt_loop = opt_loop + 1) {
  250. temp_data = ((sotp_row_data >> (opt_loop * 2)) & 0x3);
  251. if (temp_data != 0x0)
  252. opt_data = (opt_data | (1 << opt_loop));
  253. }
  254. return opt_data;
  255. }