sifive_u_otp.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * QEMU SiFive U OTP (One-Time Programmable) Memory interface
  3. *
  4. * Copyright (c) 2019 Bin Meng <bmeng.cn@gmail.com>
  5. *
  6. * Simple model of the OTP to emulate register reads made by the SDK BSP
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2 or later, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qapi/error.h"
  22. #include "hw/qdev-properties.h"
  23. #include "hw/qdev-properties-system.h"
  24. #include "hw/sysbus.h"
  25. #include "qemu/error-report.h"
  26. #include "qemu/log.h"
  27. #include "qemu/module.h"
  28. #include "hw/misc/sifive_u_otp.h"
  29. #include "sysemu/blockdev.h"
  30. #include "sysemu/block-backend.h"
  31. #define WRITTEN_BIT_ON 0x1
  32. #define SET_FUSEARRAY_BIT(map, i, off, bit) \
  33. map[i] = bit ? (map[i] | bit << off) : (map[i] & ~(0x1 << off))
  34. #define GET_FUSEARRAY_BIT(map, i, off) \
  35. ((map[i] >> off) & 0x1)
  36. static uint64_t sifive_u_otp_read(void *opaque, hwaddr addr, unsigned int size)
  37. {
  38. SiFiveUOTPState *s = opaque;
  39. switch (addr) {
  40. case SIFIVE_U_OTP_PA:
  41. return s->pa;
  42. case SIFIVE_U_OTP_PAIO:
  43. return s->paio;
  44. case SIFIVE_U_OTP_PAS:
  45. return s->pas;
  46. case SIFIVE_U_OTP_PCE:
  47. return s->pce;
  48. case SIFIVE_U_OTP_PCLK:
  49. return s->pclk;
  50. case SIFIVE_U_OTP_PDIN:
  51. return s->pdin;
  52. case SIFIVE_U_OTP_PDOUT:
  53. if ((s->pce & SIFIVE_U_OTP_PCE_EN) &&
  54. (s->pdstb & SIFIVE_U_OTP_PDSTB_EN) &&
  55. (s->ptrim & SIFIVE_U_OTP_PTRIM_EN)) {
  56. /* read from backend */
  57. if (s->blk) {
  58. int32_t buf;
  59. if (blk_pread(s->blk, s->pa * SIFIVE_U_OTP_FUSE_WORD,
  60. SIFIVE_U_OTP_FUSE_WORD, &buf, 0) < 0) {
  61. error_report("read error index<%d>", s->pa);
  62. return 0xff;
  63. }
  64. return buf;
  65. }
  66. return s->fuse[s->pa & SIFIVE_U_OTP_PA_MASK];
  67. } else {
  68. return 0xff;
  69. }
  70. case SIFIVE_U_OTP_PDSTB:
  71. return s->pdstb;
  72. case SIFIVE_U_OTP_PPROG:
  73. return s->pprog;
  74. case SIFIVE_U_OTP_PTC:
  75. return s->ptc;
  76. case SIFIVE_U_OTP_PTM:
  77. return s->ptm;
  78. case SIFIVE_U_OTP_PTM_REP:
  79. return s->ptm_rep;
  80. case SIFIVE_U_OTP_PTR:
  81. return s->ptr;
  82. case SIFIVE_U_OTP_PTRIM:
  83. return s->ptrim;
  84. case SIFIVE_U_OTP_PWE:
  85. return s->pwe;
  86. }
  87. qemu_log_mask(LOG_GUEST_ERROR, "%s: read: addr=0x%" HWADDR_PRIx "\n",
  88. __func__, addr);
  89. return 0;
  90. }
  91. static void sifive_u_otp_write(void *opaque, hwaddr addr,
  92. uint64_t val64, unsigned int size)
  93. {
  94. SiFiveUOTPState *s = opaque;
  95. uint32_t val32 = (uint32_t)val64;
  96. switch (addr) {
  97. case SIFIVE_U_OTP_PA:
  98. s->pa = val32 & SIFIVE_U_OTP_PA_MASK;
  99. break;
  100. case SIFIVE_U_OTP_PAIO:
  101. s->paio = val32;
  102. break;
  103. case SIFIVE_U_OTP_PAS:
  104. s->pas = val32;
  105. break;
  106. case SIFIVE_U_OTP_PCE:
  107. s->pce = val32;
  108. break;
  109. case SIFIVE_U_OTP_PCLK:
  110. s->pclk = val32;
  111. break;
  112. case SIFIVE_U_OTP_PDIN:
  113. s->pdin = val32;
  114. break;
  115. case SIFIVE_U_OTP_PDOUT:
  116. /* read-only */
  117. break;
  118. case SIFIVE_U_OTP_PDSTB:
  119. s->pdstb = val32;
  120. break;
  121. case SIFIVE_U_OTP_PPROG:
  122. s->pprog = val32;
  123. break;
  124. case SIFIVE_U_OTP_PTC:
  125. s->ptc = val32;
  126. break;
  127. case SIFIVE_U_OTP_PTM:
  128. s->ptm = val32;
  129. break;
  130. case SIFIVE_U_OTP_PTM_REP:
  131. s->ptm_rep = val32;
  132. break;
  133. case SIFIVE_U_OTP_PTR:
  134. s->ptr = val32;
  135. break;
  136. case SIFIVE_U_OTP_PTRIM:
  137. s->ptrim = val32;
  138. break;
  139. case SIFIVE_U_OTP_PWE:
  140. s->pwe = val32 & SIFIVE_U_OTP_PWE_EN;
  141. /* PWE is enabled. Ignore PAS=1 (no redundancy cell) */
  142. if (s->pwe && !s->pas) {
  143. if (GET_FUSEARRAY_BIT(s->fuse_wo, s->pa, s->paio)) {
  144. qemu_log_mask(LOG_GUEST_ERROR,
  145. "write once error: idx<%u>, bit<%u>\n",
  146. s->pa, s->paio);
  147. break;
  148. }
  149. /* write bit data */
  150. SET_FUSEARRAY_BIT(s->fuse, s->pa, s->paio, s->pdin);
  151. /* write to backend */
  152. if (s->blk) {
  153. if (blk_pwrite(s->blk, s->pa * SIFIVE_U_OTP_FUSE_WORD,
  154. SIFIVE_U_OTP_FUSE_WORD, &s->fuse[s->pa], 0)
  155. < 0) {
  156. error_report("write error index<%d>", s->pa);
  157. }
  158. }
  159. /* update written bit */
  160. SET_FUSEARRAY_BIT(s->fuse_wo, s->pa, s->paio, WRITTEN_BIT_ON);
  161. }
  162. break;
  163. default:
  164. qemu_log_mask(LOG_GUEST_ERROR, "%s: bad write: addr=0x%" HWADDR_PRIx
  165. " v=0x%x\n", __func__, addr, val32);
  166. }
  167. }
  168. static const MemoryRegionOps sifive_u_otp_ops = {
  169. .read = sifive_u_otp_read,
  170. .write = sifive_u_otp_write,
  171. .endianness = DEVICE_NATIVE_ENDIAN,
  172. .valid = {
  173. .min_access_size = 4,
  174. .max_access_size = 4
  175. }
  176. };
  177. static Property sifive_u_otp_properties[] = {
  178. DEFINE_PROP_UINT32("serial", SiFiveUOTPState, serial, 0),
  179. DEFINE_PROP_DRIVE("drive", SiFiveUOTPState, blk),
  180. DEFINE_PROP_END_OF_LIST(),
  181. };
  182. static void sifive_u_otp_realize(DeviceState *dev, Error **errp)
  183. {
  184. SiFiveUOTPState *s = SIFIVE_U_OTP(dev);
  185. DriveInfo *dinfo;
  186. memory_region_init_io(&s->mmio, OBJECT(dev), &sifive_u_otp_ops, s,
  187. TYPE_SIFIVE_U_OTP, SIFIVE_U_OTP_REG_SIZE);
  188. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mmio);
  189. dinfo = drive_get(IF_PFLASH, 0, 0);
  190. if (dinfo) {
  191. int ret;
  192. uint64_t perm;
  193. int filesize;
  194. BlockBackend *blk;
  195. blk = blk_by_legacy_dinfo(dinfo);
  196. filesize = SIFIVE_U_OTP_NUM_FUSES * SIFIVE_U_OTP_FUSE_WORD;
  197. if (blk_getlength(blk) < filesize) {
  198. error_setg(errp, "OTP drive size < 16K");
  199. return;
  200. }
  201. qdev_prop_set_drive_err(dev, "drive", blk, errp);
  202. if (s->blk) {
  203. perm = BLK_PERM_CONSISTENT_READ |
  204. (blk_supports_write_perm(s->blk) ? BLK_PERM_WRITE : 0);
  205. ret = blk_set_perm(s->blk, perm, BLK_PERM_ALL, errp);
  206. if (ret < 0) {
  207. return;
  208. }
  209. if (blk_pread(s->blk, 0, filesize, s->fuse, 0) < 0) {
  210. error_setg(errp, "failed to read the initial flash content");
  211. return;
  212. }
  213. }
  214. }
  215. /* Initialize all fuses' initial value to 0xFFs */
  216. memset(s->fuse, 0xff, sizeof(s->fuse));
  217. /* Make a valid content of serial number */
  218. s->fuse[SIFIVE_U_OTP_SERIAL_ADDR] = s->serial;
  219. s->fuse[SIFIVE_U_OTP_SERIAL_ADDR + 1] = ~(s->serial);
  220. if (s->blk) {
  221. /* Put serial number to backend as well*/
  222. uint32_t serial_data;
  223. int index = SIFIVE_U_OTP_SERIAL_ADDR;
  224. serial_data = s->serial;
  225. if (blk_pwrite(s->blk, index * SIFIVE_U_OTP_FUSE_WORD,
  226. SIFIVE_U_OTP_FUSE_WORD, &serial_data, 0) < 0) {
  227. error_setg(errp, "failed to write index<%d>", index);
  228. return;
  229. }
  230. serial_data = ~(s->serial);
  231. if (blk_pwrite(s->blk, (index + 1) * SIFIVE_U_OTP_FUSE_WORD,
  232. SIFIVE_U_OTP_FUSE_WORD, &serial_data, 0) < 0) {
  233. error_setg(errp, "failed to write index<%d>", index + 1);
  234. return;
  235. }
  236. }
  237. /* Initialize write-once map */
  238. memset(s->fuse_wo, 0x00, sizeof(s->fuse_wo));
  239. }
  240. static void sifive_u_otp_class_init(ObjectClass *klass, void *data)
  241. {
  242. DeviceClass *dc = DEVICE_CLASS(klass);
  243. device_class_set_props(dc, sifive_u_otp_properties);
  244. dc->realize = sifive_u_otp_realize;
  245. }
  246. static const TypeInfo sifive_u_otp_info = {
  247. .name = TYPE_SIFIVE_U_OTP,
  248. .parent = TYPE_SYS_BUS_DEVICE,
  249. .instance_size = sizeof(SiFiveUOTPState),
  250. .class_init = sifive_u_otp_class_init,
  251. };
  252. static void sifive_u_otp_register_types(void)
  253. {
  254. type_register_static(&sifive_u_otp_info);
  255. }
  256. type_init(sifive_u_otp_register_types)