dv-bfin_otp.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* Blackfin One-Time Programmable Memory (OTP) model
  2. Copyright (C) 2010-2022 Free Software Foundation, Inc.
  3. Contributed by Analog Devices, Inc.
  4. This file is part of simulators.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* This must come before any other includes. */
  16. #include "defs.h"
  17. #include "sim-main.h"
  18. #include "devices.h"
  19. #include "dv-bfin_otp.h"
  20. /* XXX: No public documentation on this interface. This seems to work
  21. with the on-chip ROM functions though and was figured out by
  22. disassembling & walking that code. */
  23. /* XXX: About only thing that should be done here are CRC fields. And
  24. supposedly there is an interrupt that could be generated. */
  25. struct bfin_otp
  26. {
  27. bu32 base;
  28. /* The actual OTP storage -- 0x200 pages, each page is 128bits.
  29. While certain pages have predefined and/or secure access, we don't
  30. bother trying to implement that coverage. All pages are open for
  31. reading & writing. */
  32. bu32 mem[0x200 * 4];
  33. /* Order after here is important -- matches hardware MMR layout. */
  34. bu16 BFIN_MMR_16(control);
  35. bu16 BFIN_MMR_16(ben);
  36. bu16 BFIN_MMR_16(status);
  37. bu32 timing;
  38. bu32 _pad0[28];
  39. bu32 data0, data1, data2, data3;
  40. };
  41. #define mmr_base() offsetof(struct bfin_otp, control)
  42. #define mmr_offset(mmr) (offsetof(struct bfin_otp, mmr) - mmr_base())
  43. #define mmr_idx(mmr) (mmr_offset (mmr) / 4)
  44. static const char * const mmr_names[] =
  45. {
  46. "OTP_CONTROL", "OTP_BEN", "OTP_STATUS", "OTP_TIMING",
  47. [mmr_idx (data0)] = "OTP_DATA0", "OTP_DATA1", "OTP_DATA2", "OTP_DATA3",
  48. };
  49. #define mmr_name(off) (mmr_names[(off) / 4] ? : "<INV>")
  50. /* XXX: This probably misbehaves with big endian hosts. */
  51. static void
  52. bfin_otp_transfer (struct bfin_otp *otp, void *vdst, void *vsrc)
  53. {
  54. bu8 *dst = vdst, *src = vsrc;
  55. int bidx;
  56. for (bidx = 0; bidx < 16; ++bidx)
  57. if (otp->ben & (1 << bidx))
  58. dst[bidx] = src[bidx];
  59. }
  60. static void
  61. bfin_otp_read_page (struct bfin_otp *otp, bu16 page)
  62. {
  63. bfin_otp_transfer (otp, &otp->data0, &otp->mem[page * 4]);
  64. }
  65. static void
  66. bfin_otp_write_page_val (struct bfin_otp *otp, bu16 page, bu64 val[2])
  67. {
  68. bfin_otp_transfer (otp, &otp->mem[page * 4], val);
  69. }
  70. static void
  71. bfin_otp_write_page_val2 (struct bfin_otp *otp, bu16 page, bu64 lo, bu64 hi)
  72. {
  73. bu64 val[2] = { lo, hi };
  74. bfin_otp_write_page_val (otp, page, val);
  75. }
  76. static void
  77. bfin_otp_write_page (struct bfin_otp *otp, bu16 page)
  78. {
  79. bfin_otp_write_page_val2 (otp, page, ((bu64)otp->data1 << 32) | otp->data0,
  80. ((bu64)otp->data3 << 32) | otp->data2);
  81. }
  82. static unsigned
  83. bfin_otp_io_write_buffer (struct hw *me, const void *source, int space,
  84. address_word addr, unsigned nr_bytes)
  85. {
  86. struct bfin_otp *otp = hw_data (me);
  87. bu32 mmr_off;
  88. bu32 value;
  89. bu16 *value16p;
  90. bu32 *value32p;
  91. void *valuep;
  92. /* Invalid access mode is higher priority than missing register. */
  93. if (!dv_bfin_mmr_require_16_32 (me, addr, nr_bytes, true))
  94. return 0;
  95. if (nr_bytes == 4)
  96. value = dv_load_4 (source);
  97. else
  98. value = dv_load_2 (source);
  99. mmr_off = addr - otp->base;
  100. valuep = (void *)((uintptr_t)otp + mmr_base() + mmr_off);
  101. value16p = valuep;
  102. value32p = valuep;
  103. HW_TRACE_WRITE ();
  104. switch (mmr_off)
  105. {
  106. case mmr_offset(control):
  107. {
  108. int page;
  109. if (!dv_bfin_mmr_require_16 (me, addr, nr_bytes, true))
  110. return 0;
  111. /* XXX: Seems like these bits aren't writable. */
  112. *value16p = value & 0x39FF;
  113. /* Low bits seem to be the page address. */
  114. page = value & PAGE_ADDR;
  115. /* Write operation. */
  116. if (value & DO_WRITE)
  117. bfin_otp_write_page (otp, page);
  118. /* Read operation. */
  119. if (value & DO_READ)
  120. bfin_otp_read_page (otp, page);
  121. otp->status |= STATUS_DONE;
  122. break;
  123. }
  124. case mmr_offset(ben):
  125. if (!dv_bfin_mmr_require_16 (me, addr, nr_bytes, true))
  126. return 0;
  127. /* XXX: All bits seem to be writable. */
  128. *value16p = value;
  129. break;
  130. case mmr_offset(status):
  131. if (!dv_bfin_mmr_require_16 (me, addr, nr_bytes, true))
  132. return 0;
  133. /* XXX: All bits seem to be W1C. */
  134. dv_w1c_2 (value16p, value, -1);
  135. break;
  136. case mmr_offset(timing):
  137. case mmr_offset(data0):
  138. case mmr_offset(data1):
  139. case mmr_offset(data2):
  140. case mmr_offset(data3):
  141. if (!dv_bfin_mmr_require_32 (me, addr, nr_bytes, true))
  142. return 0;
  143. *value32p = value;
  144. break;
  145. default:
  146. dv_bfin_mmr_invalid (me, addr, nr_bytes, true);
  147. return 0;
  148. }
  149. return nr_bytes;
  150. }
  151. static unsigned
  152. bfin_otp_io_read_buffer (struct hw *me, void *dest, int space,
  153. address_word addr, unsigned nr_bytes)
  154. {
  155. struct bfin_otp *otp = hw_data (me);
  156. bu32 mmr_off;
  157. bu16 *value16p;
  158. bu32 *value32p;
  159. void *valuep;
  160. /* Invalid access mode is higher priority than missing register. */
  161. if (!dv_bfin_mmr_require_16_32 (me, addr, nr_bytes, false))
  162. return 0;
  163. mmr_off = addr - otp->base;
  164. valuep = (void *)((uintptr_t)otp + mmr_base() + mmr_off);
  165. value16p = valuep;
  166. value32p = valuep;
  167. HW_TRACE_READ ();
  168. switch (mmr_off)
  169. {
  170. case mmr_offset(control):
  171. case mmr_offset(ben):
  172. case mmr_offset(status):
  173. if (!dv_bfin_mmr_require_16 (me, addr, nr_bytes, false))
  174. return 0;
  175. dv_store_2 (dest, *value16p);
  176. break;
  177. case mmr_offset(timing):
  178. case mmr_offset(data0):
  179. case mmr_offset(data1):
  180. case mmr_offset(data2):
  181. case mmr_offset(data3):
  182. if (!dv_bfin_mmr_require_32 (me, addr, nr_bytes, false))
  183. return 0;
  184. dv_store_4 (dest, *value32p);
  185. break;
  186. default:
  187. dv_bfin_mmr_invalid (me, addr, nr_bytes, false);
  188. return 0;
  189. }
  190. return nr_bytes;
  191. }
  192. static void
  193. attach_bfin_otp_regs (struct hw *me, struct bfin_otp *otp)
  194. {
  195. address_word attach_address;
  196. int attach_space;
  197. unsigned attach_size;
  198. reg_property_spec reg;
  199. if (hw_find_property (me, "reg") == NULL)
  200. hw_abort (me, "Missing \"reg\" property");
  201. if (!hw_find_reg_array_property (me, "reg", 0, &reg))
  202. hw_abort (me, "\"reg\" property must contain three addr/size entries");
  203. hw_unit_address_to_attach_address (hw_parent (me),
  204. &reg.address,
  205. &attach_space, &attach_address, me);
  206. hw_unit_size_to_attach_size (hw_parent (me), &reg.size, &attach_size, me);
  207. if (attach_size != BFIN_MMR_OTP_SIZE)
  208. hw_abort (me, "\"reg\" size must be %#x", BFIN_MMR_OTP_SIZE);
  209. hw_attach_address (hw_parent (me),
  210. 0, attach_space, attach_address, attach_size, me);
  211. otp->base = attach_address;
  212. }
  213. static const struct hw_port_descriptor bfin_otp_ports[] =
  214. {
  215. { "stat", 0, 0, output_port, },
  216. { NULL, 0, 0, 0, },
  217. };
  218. static void
  219. bfin_otp_finish (struct hw *me)
  220. {
  221. char part_str[16];
  222. struct bfin_otp *otp;
  223. unsigned int fps03;
  224. int type = hw_find_integer_property (me, "type");
  225. otp = HW_ZALLOC (me, struct bfin_otp);
  226. set_hw_data (me, otp);
  227. set_hw_io_read_buffer (me, bfin_otp_io_read_buffer);
  228. set_hw_io_write_buffer (me, bfin_otp_io_write_buffer);
  229. set_hw_ports (me, bfin_otp_ports);
  230. attach_bfin_otp_regs (me, otp);
  231. /* Initialize the OTP. */
  232. otp->ben = 0xFFFF;
  233. otp->timing = 0x00001485;
  234. /* Semi-random value for unique chip id. */
  235. bfin_otp_write_page_val2 (otp, FPS00, (uintptr_t)otp, ~(uintptr_t)otp);
  236. memset (part_str, 0, sizeof (part_str));
  237. sprintf (part_str, "ADSP-BF%iX", type);
  238. switch (type)
  239. {
  240. case 512:
  241. fps03 = FPS03_BF512;
  242. break;
  243. case 514:
  244. fps03 = FPS03_BF514;
  245. break;
  246. case 516:
  247. fps03 = FPS03_BF516;
  248. break;
  249. case 518:
  250. fps03 = FPS03_BF518;
  251. break;
  252. case 522:
  253. fps03 = FPS03_BF522;
  254. break;
  255. case 523:
  256. fps03 = FPS03_BF523;
  257. break;
  258. case 524:
  259. fps03 = FPS03_BF524;
  260. break;
  261. case 525:
  262. fps03 = FPS03_BF525;
  263. break;
  264. case 526:
  265. fps03 = FPS03_BF526;
  266. break;
  267. case 527:
  268. fps03 = FPS03_BF527;
  269. break;
  270. default:
  271. fps03 = 0;
  272. break;
  273. }
  274. part_str[14] = (fps03 >> 0);
  275. part_str[15] = (fps03 >> 8);
  276. bfin_otp_write_page_val (otp, FPS03, (void *)part_str);
  277. }
  278. const struct hw_descriptor dv_bfin_otp_descriptor[] =
  279. {
  280. {"bfin_otp", bfin_otp_finish,},
  281. {NULL, NULL},
  282. };