ocotp.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2017 - 2020, Broadcom
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <stdint.h>
  7. #include <common/debug.h>
  8. #include <drivers/delay_timer.h>
  9. #include <lib/mmio.h>
  10. #include <ocotp.h>
  11. #include <platform_def.h>
  12. #define OTP_MAP 2
  13. #define OTP_NUM_WORDS 2048
  14. /*
  15. * # of tries for OTP Status. The time to execute a command varies. The slowest
  16. * commands are writes which also vary based on the # of bits turned on. Writing
  17. * 0xffffffff takes ~3800 us.
  18. */
  19. #define OTPC_RETRIES_US 5000
  20. /* Sequence to enable OTP program */
  21. #define OTPC_PROG_EN_SEQ { 0xf, 0x4, 0x8, 0xd }
  22. /* OTPC Commands */
  23. #define OTPC_CMD_READ 0x0
  24. #define OTPC_CMD_OTP_PROG_ENABLE 0x2
  25. #define OTPC_CMD_OTP_PROG_DISABLE 0x3
  26. #define OTPC_CMD_PROGRAM 0x8
  27. #define OTPC_CMD_ECC 0x10
  28. #define OTPC_ECC_ADDR 0x1A
  29. #define OTPC_ECC_VAL 0x00EC0000
  30. /* OTPC Status Bits */
  31. #define OTPC_STAT_CMD_DONE BIT(1)
  32. #define OTPC_STAT_PROG_OK BIT(2)
  33. /* OTPC register definition */
  34. #define OTPC_MODE_REG_OFFSET 0x0
  35. #define OTPC_MODE_REG_OTPC_MODE 0
  36. #define OTPC_COMMAND_OFFSET 0x4
  37. #define OTPC_COMMAND_COMMAND_WIDTH 6
  38. #define OTPC_CMD_START_OFFSET 0x8
  39. #define OTPC_CMD_START_START 0
  40. #define OTPC_CPU_STATUS_OFFSET 0xc
  41. #define OTPC_CPUADDR_REG_OFFSET 0x28
  42. #define OTPC_CPUADDR_REG_OTPC_CPU_ADDRESS_WIDTH 16
  43. #define OTPC_CPU_WRITE_REG_OFFSET 0x2c
  44. #define OTPC_CMD_MASK (BIT(OTPC_COMMAND_COMMAND_WIDTH) - 1)
  45. #define OTPC_ADDR_MASK (BIT(OTPC_CPUADDR_REG_OTPC_CPU_ADDRESS_WIDTH) - 1)
  46. #define OTPC_MODE_REG OCOTP_REGS_BASE
  47. struct chip_otp_cfg {
  48. uint32_t base;
  49. uint32_t num_words;
  50. };
  51. struct chip_otp_cfg ocotp_cfg = {
  52. .base = OTPC_MODE_REG,
  53. .num_words = 2048,
  54. };
  55. struct otpc_priv {
  56. uint32_t base;
  57. struct otpc_map *map;
  58. int size;
  59. int state;
  60. };
  61. struct otpc_priv otpc_info;
  62. static inline void set_command(uint32_t base, uint32_t command)
  63. {
  64. mmio_write_32(base + OTPC_COMMAND_OFFSET, command & OTPC_CMD_MASK);
  65. }
  66. static inline void set_cpu_address(uint32_t base, uint32_t addr)
  67. {
  68. mmio_write_32(base + OTPC_CPUADDR_REG_OFFSET, addr & OTPC_ADDR_MASK);
  69. }
  70. static inline void set_start_bit(uint32_t base)
  71. {
  72. mmio_write_32(base + OTPC_CMD_START_OFFSET, 1 << OTPC_CMD_START_START);
  73. }
  74. static inline void reset_start_bit(uint32_t base)
  75. {
  76. mmio_write_32(base + OTPC_CMD_START_OFFSET, 0);
  77. }
  78. static inline void write_cpu_data(uint32_t base, uint32_t value)
  79. {
  80. mmio_write_32(base + OTPC_CPU_WRITE_REG_OFFSET, value);
  81. }
  82. static int poll_cpu_status(uint32_t base, uint32_t value)
  83. {
  84. uint32_t status;
  85. uint32_t retries;
  86. for (retries = 0; retries < OTPC_RETRIES_US; retries++) {
  87. status = mmio_read_32(base + OTPC_CPU_STATUS_OFFSET);
  88. if (status & value)
  89. break;
  90. udelay(1);
  91. }
  92. if (retries == OTPC_RETRIES_US)
  93. return -1;
  94. return 0;
  95. }
  96. static int bcm_otpc_ecc(uint32_t enable)
  97. {
  98. struct otpc_priv *priv = &otpc_info;
  99. int ret;
  100. set_command(priv->base, OTPC_CMD_ECC);
  101. set_cpu_address(priv->base, OTPC_ECC_ADDR);
  102. if (!enable)
  103. write_cpu_data(priv->base, OTPC_ECC_VAL);
  104. else
  105. write_cpu_data(priv->base, ~OTPC_ECC_VAL);
  106. set_start_bit(priv->base);
  107. ret = poll_cpu_status(priv->base, OTPC_STAT_CMD_DONE);
  108. if (ret) {
  109. ERROR("otp ecc op error: 0x%x", ret);
  110. return -1;
  111. }
  112. reset_start_bit(priv->base);
  113. return 0;
  114. }
  115. /*
  116. * bcm_otpc_read read otp data in the size of 8 byte rows.
  117. * bytes has to be the multiple of 8.
  118. * return -1 in error case, return read bytes in success.
  119. */
  120. int bcm_otpc_read(unsigned int offset, void *val, uint32_t bytes,
  121. uint32_t ecc_flag)
  122. {
  123. struct otpc_priv *priv = &otpc_info;
  124. uint32_t *buf = val;
  125. uint32_t bytes_read;
  126. uint32_t address = offset / priv->map->word_size;
  127. int i, ret;
  128. if (!priv->state) {
  129. ERROR("OCOTP read failed\n");
  130. return -1;
  131. }
  132. bcm_otpc_ecc(ecc_flag);
  133. for (bytes_read = 0; (bytes_read + priv->map->word_size) <= bytes;) {
  134. set_command(priv->base, OTPC_CMD_READ);
  135. set_cpu_address(priv->base, address++);
  136. set_start_bit(priv->base);
  137. ret = poll_cpu_status(priv->base, OTPC_STAT_CMD_DONE);
  138. if (ret) {
  139. ERROR("otp read error: 0x%x", ret);
  140. return -1;
  141. }
  142. for (i = 0; i < priv->map->otpc_row_size; i++) {
  143. *buf++ = mmio_read_32(priv->base +
  144. priv->map->data_r_offset[i]);
  145. bytes_read += sizeof(*buf);
  146. }
  147. reset_start_bit(priv->base);
  148. }
  149. return bytes_read;
  150. }
  151. int bcm_otpc_init(struct otpc_map *map)
  152. {
  153. struct otpc_priv *priv;
  154. priv = &otpc_info;
  155. priv->base = ocotp_cfg.base;
  156. priv->map = map;
  157. priv->size = 4 * ocotp_cfg.num_words;
  158. /* Enable CPU access to OTPC. */
  159. mmio_setbits_32(priv->base + OTPC_MODE_REG_OFFSET,
  160. BIT(OTPC_MODE_REG_OTPC_MODE));
  161. reset_start_bit(priv->base);
  162. priv->state = 1;
  163. VERBOSE("OTPC Initialization done\n");
  164. return 0;
  165. }