dv-bfin_ebiu_amc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /* Blackfin External Bus Interface Unit (EBIU) Asynchronous Memory Controller
  2. (AMC) model.
  3. Copyright (C) 2010-2015 Free Software Foundation, Inc.
  4. Contributed by Analog Devices, Inc.
  5. This file is part of simulators.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "sim-main.h"
  18. #include "devices.h"
  19. #include "dv-bfin_ebiu_amc.h"
  20. struct bfin_ebiu_amc
  21. {
  22. bu32 base;
  23. int type;
  24. bu32 bank_base, bank_size;
  25. unsigned (*io_write) (struct hw *, const void *, int, address_word,
  26. unsigned, struct bfin_ebiu_amc *, bu32, bu32);
  27. unsigned (*io_read) (struct hw *, void *, int, address_word, unsigned,
  28. struct bfin_ebiu_amc *, bu32, void *, bu16 *, bu32 *);
  29. struct hw *slaves[4];
  30. /* Order after here is important -- matches hardware MMR layout. */
  31. bu16 BFIN_MMR_16(amgctl);
  32. union {
  33. struct {
  34. bu32 ambctl0, ambctl1;
  35. bu32 _pad0[5];
  36. bu16 BFIN_MMR_16(mode);
  37. bu16 BFIN_MMR_16(fctl);
  38. } bf50x;
  39. struct {
  40. bu32 ambctl0, ambctl1;
  41. } bf53x;
  42. struct {
  43. bu32 ambctl0, ambctl1;
  44. bu32 mbsctl, arbstat, mode, fctl;
  45. } bf54x;
  46. };
  47. };
  48. #define mmr_base() offsetof(struct bfin_ebiu_amc, amgctl)
  49. #define mmr_offset(mmr) (offsetof(struct bfin_ebiu_amc, mmr) - mmr_base())
  50. #define mmr_idx(mmr) (mmr_offset (mmr) / 4)
  51. static const char * const bf50x_mmr_names[] =
  52. {
  53. "EBIU_AMGCTL", "EBIU_AMBCTL0", "EBIU_AMBCTL1",
  54. [mmr_idx (bf50x.mode)] = "EBIU_MODE", "EBIU_FCTL",
  55. };
  56. static const char * const bf53x_mmr_names[] =
  57. {
  58. "EBIU_AMGCTL", "EBIU_AMBCTL0", "EBIU_AMBCTL1",
  59. };
  60. static const char * const bf54x_mmr_names[] =
  61. {
  62. "EBIU_AMGCTL", "EBIU_AMBCTL0", "EBIU_AMBCTL1",
  63. "EBIU_MSBCTL", "EBIU_ARBSTAT", "EBIU_MODE", "EBIU_FCTL",
  64. };
  65. static const char * const *mmr_names;
  66. #define mmr_name(off) (mmr_names[(off) / 4] ? : "<INV>")
  67. static void
  68. bfin_ebiu_amc_write_amgctl (struct hw *me, struct bfin_ebiu_amc *amc,
  69. bu16 amgctl)
  70. {
  71. bu32 amben_old, amben, addr, i;
  72. amben_old = MIN ((amc->amgctl >> 1) & 0x7, 4);
  73. amben = MIN ((amgctl >> 1) & 0x7, 4);
  74. HW_TRACE ((me, "reattaching banks: AMGCTL 0x%04x[%u] -> 0x%04x[%u]",
  75. amc->amgctl, amben_old, amgctl, amben));
  76. for (i = 0; i < 4; ++i)
  77. {
  78. addr = amc->bank_base + i * amc->bank_size;
  79. if (i < amben_old)
  80. {
  81. HW_TRACE ((me, "detaching bank %u (%#x base)", i, addr));
  82. sim_core_detach (hw_system (me), NULL, 0, 0, addr);
  83. }
  84. if (i < amben)
  85. {
  86. struct hw *slave = amc->slaves[i];
  87. HW_TRACE ((me, "attaching bank %u (%#x base) to %s", i, addr,
  88. slave ? hw_path (slave) : "<floating pins>"));
  89. sim_core_attach (hw_system (me), NULL, 0, access_read_write_exec,
  90. 0, addr, amc->bank_size, 0, slave, NULL);
  91. }
  92. }
  93. amc->amgctl = amgctl;
  94. }
  95. static unsigned
  96. bf50x_ebiu_amc_io_write_buffer (struct hw *me, const void *source, int space,
  97. address_word addr, unsigned nr_bytes,
  98. struct bfin_ebiu_amc *amc, bu32 mmr_off,
  99. bu32 value)
  100. {
  101. switch (mmr_off)
  102. {
  103. case mmr_offset(amgctl):
  104. dv_bfin_mmr_require_16 (me, addr, nr_bytes, true);
  105. bfin_ebiu_amc_write_amgctl (me, amc, value);
  106. break;
  107. case mmr_offset(bf50x.ambctl0):
  108. amc->bf50x.ambctl0 = value;
  109. break;
  110. case mmr_offset(bf50x.ambctl1):
  111. amc->bf50x.ambctl1 = value;
  112. break;
  113. case mmr_offset(bf50x.mode):
  114. /* XXX: implement this. */
  115. dv_bfin_mmr_require_16 (me, addr, nr_bytes, true);
  116. break;
  117. case mmr_offset(bf50x.fctl):
  118. /* XXX: implement this. */
  119. dv_bfin_mmr_require_16 (me, addr, nr_bytes, true);
  120. break;
  121. default:
  122. dv_bfin_mmr_invalid (me, addr, nr_bytes, true);
  123. break;
  124. }
  125. return nr_bytes;
  126. }
  127. static unsigned
  128. bf53x_ebiu_amc_io_write_buffer (struct hw *me, const void *source, int space,
  129. address_word addr, unsigned nr_bytes,
  130. struct bfin_ebiu_amc *amc, bu32 mmr_off,
  131. bu32 value)
  132. {
  133. switch (mmr_off)
  134. {
  135. case mmr_offset(amgctl):
  136. dv_bfin_mmr_require_16 (me, addr, nr_bytes, true);
  137. bfin_ebiu_amc_write_amgctl (me, amc, value);
  138. break;
  139. case mmr_offset(bf53x.ambctl0):
  140. amc->bf53x.ambctl0 = value;
  141. break;
  142. case mmr_offset(bf53x.ambctl1):
  143. amc->bf53x.ambctl1 = value;
  144. break;
  145. default:
  146. dv_bfin_mmr_invalid (me, addr, nr_bytes, true);
  147. break;
  148. }
  149. return nr_bytes;
  150. }
  151. static unsigned
  152. bf54x_ebiu_amc_io_write_buffer (struct hw *me, const void *source, int space,
  153. address_word addr, unsigned nr_bytes,
  154. struct bfin_ebiu_amc *amc, bu32 mmr_off,
  155. bu32 value)
  156. {
  157. switch (mmr_off)
  158. {
  159. case mmr_offset(amgctl):
  160. dv_bfin_mmr_require_16 (me, addr, nr_bytes, true);
  161. bfin_ebiu_amc_write_amgctl (me, amc, value);
  162. break;
  163. case mmr_offset(bf54x.ambctl0):
  164. amc->bf54x.ambctl0 = value;
  165. break;
  166. case mmr_offset(bf54x.ambctl1):
  167. amc->bf54x.ambctl1 = value;
  168. break;
  169. case mmr_offset(bf54x.mbsctl):
  170. /* XXX: implement this. */
  171. break;
  172. case mmr_offset(bf54x.arbstat):
  173. /* XXX: implement this. */
  174. break;
  175. case mmr_offset(bf54x.mode):
  176. /* XXX: implement this. */
  177. break;
  178. case mmr_offset(bf54x.fctl):
  179. /* XXX: implement this. */
  180. break;
  181. default:
  182. dv_bfin_mmr_invalid (me, addr, nr_bytes, true);
  183. break;
  184. }
  185. return nr_bytes;
  186. }
  187. static unsigned
  188. bfin_ebiu_amc_io_write_buffer (struct hw *me, const void *source, int space,
  189. address_word addr, unsigned nr_bytes)
  190. {
  191. struct bfin_ebiu_amc *amc = hw_data (me);
  192. bu32 mmr_off;
  193. bu32 value;
  194. value = dv_load_4 (source);
  195. mmr_off = addr - amc->base;
  196. HW_TRACE_WRITE ();
  197. return amc->io_write (me, source, space, addr, nr_bytes,
  198. amc, mmr_off, value);
  199. }
  200. static unsigned
  201. bf50x_ebiu_amc_io_read_buffer (struct hw *me, void *dest, int space,
  202. address_word addr, unsigned nr_bytes,
  203. struct bfin_ebiu_amc *amc, bu32 mmr_off,
  204. void *valuep, bu16 *value16, bu32 *value32)
  205. {
  206. switch (mmr_off)
  207. {
  208. case mmr_offset(amgctl):
  209. case mmr_offset(bf50x.fctl):
  210. dv_bfin_mmr_require_16 (me, addr, nr_bytes, false);
  211. dv_store_2 (dest, *value16);
  212. break;
  213. case mmr_offset(bf50x.ambctl0):
  214. case mmr_offset(bf50x.ambctl1):
  215. case mmr_offset(bf50x.mode):
  216. dv_store_4 (dest, *value32);
  217. break;
  218. default:
  219. dv_bfin_mmr_invalid (me, addr, nr_bytes, false);
  220. break;
  221. }
  222. return nr_bytes;
  223. }
  224. static unsigned
  225. bf53x_ebiu_amc_io_read_buffer (struct hw *me, void *dest, int space,
  226. address_word addr, unsigned nr_bytes,
  227. struct bfin_ebiu_amc *amc, bu32 mmr_off,
  228. void *valuep, bu16 *value16, bu32 *value32)
  229. {
  230. switch (mmr_off)
  231. {
  232. case mmr_offset(amgctl):
  233. dv_bfin_mmr_require_16 (me, addr, nr_bytes, false);
  234. dv_store_2 (dest, *value16);
  235. break;
  236. case mmr_offset(bf53x.ambctl0):
  237. case mmr_offset(bf53x.ambctl1):
  238. dv_store_4 (dest, *value32);
  239. break;
  240. default:
  241. dv_bfin_mmr_invalid (me, addr, nr_bytes, false);
  242. break;
  243. }
  244. return nr_bytes;
  245. }
  246. static unsigned
  247. bf54x_ebiu_amc_io_read_buffer (struct hw *me, void *dest, int space,
  248. address_word addr, unsigned nr_bytes,
  249. struct bfin_ebiu_amc *amc, bu32 mmr_off,
  250. void *valuep, bu16 *value16, bu32 *value32)
  251. {
  252. switch (mmr_off)
  253. {
  254. case mmr_offset(amgctl):
  255. dv_bfin_mmr_require_16 (me, addr, nr_bytes, false);
  256. dv_store_2 (dest, *value16);
  257. break;
  258. case mmr_offset(bf54x.ambctl0):
  259. case mmr_offset(bf54x.ambctl1):
  260. case mmr_offset(bf54x.mbsctl):
  261. case mmr_offset(bf54x.arbstat):
  262. case mmr_offset(bf54x.mode):
  263. case mmr_offset(bf54x.fctl):
  264. dv_store_4 (dest, *value32);
  265. break;
  266. default:
  267. dv_bfin_mmr_invalid (me, addr, nr_bytes, false);
  268. break;
  269. }
  270. return nr_bytes;
  271. }
  272. static unsigned
  273. bfin_ebiu_amc_io_read_buffer (struct hw *me, void *dest, int space,
  274. address_word addr, unsigned nr_bytes)
  275. {
  276. struct bfin_ebiu_amc *amc = hw_data (me);
  277. bu32 mmr_off;
  278. void *valuep;
  279. mmr_off = addr - amc->base;
  280. valuep = (void *)((unsigned long)amc + mmr_base() + mmr_off);
  281. HW_TRACE_READ ();
  282. return amc->io_read (me, dest, space, addr, nr_bytes, amc,
  283. mmr_off, valuep, valuep, valuep);
  284. }
  285. static void
  286. bfin_ebiu_amc_attach_address_callback (struct hw *me,
  287. int level,
  288. int space,
  289. address_word addr,
  290. address_word nr_bytes,
  291. struct hw *client)
  292. {
  293. struct bfin_ebiu_amc *amc = hw_data (me);
  294. HW_TRACE ((me, "attach - level=%d, space=%d, addr=0x%lx, nr_bytes=%lu, client=%s",
  295. level, space, (unsigned long) addr, (unsigned long) nr_bytes, hw_path (client)));
  296. if (addr + nr_bytes > ARRAY_SIZE (amc->slaves))
  297. hw_abort (me, "ebiu amc attaches are done in terms of banks");
  298. while (nr_bytes--)
  299. amc->slaves[addr + nr_bytes] = client;
  300. bfin_ebiu_amc_write_amgctl (me, amc, amc->amgctl);
  301. }
  302. static void
  303. attach_bfin_ebiu_amc_regs (struct hw *me, struct bfin_ebiu_amc *amc,
  304. unsigned reg_size)
  305. {
  306. address_word attach_address;
  307. int attach_space;
  308. unsigned attach_size;
  309. reg_property_spec reg;
  310. if (hw_find_property (me, "reg") == NULL)
  311. hw_abort (me, "Missing \"reg\" property");
  312. if (!hw_find_reg_array_property (me, "reg", 0, &reg))
  313. hw_abort (me, "\"reg\" property must contain three addr/size entries");
  314. if (hw_find_property (me, "type") == NULL)
  315. hw_abort (me, "Missing \"type\" property");
  316. hw_unit_address_to_attach_address (hw_parent (me),
  317. &reg.address,
  318. &attach_space, &attach_address, me);
  319. hw_unit_size_to_attach_size (hw_parent (me), &reg.size, &attach_size, me);
  320. if (attach_size != reg_size)
  321. hw_abort (me, "\"reg\" size must be %#x", reg_size);
  322. hw_attach_address (hw_parent (me),
  323. 0, attach_space, attach_address, attach_size, me);
  324. amc->base = attach_address;
  325. }
  326. static void
  327. bfin_ebiu_amc_finish (struct hw *me)
  328. {
  329. struct bfin_ebiu_amc *amc;
  330. bu32 amgctl;
  331. unsigned reg_size;
  332. amc = HW_ZALLOC (me, struct bfin_ebiu_amc);
  333. set_hw_data (me, amc);
  334. set_hw_io_read_buffer (me, bfin_ebiu_amc_io_read_buffer);
  335. set_hw_io_write_buffer (me, bfin_ebiu_amc_io_write_buffer);
  336. set_hw_attach_address (me, bfin_ebiu_amc_attach_address_callback);
  337. amc->type = hw_find_integer_property (me, "type");
  338. switch (amc->type)
  339. {
  340. case 500 ... 509:
  341. amc->io_write = bf50x_ebiu_amc_io_write_buffer;
  342. amc->io_read = bf50x_ebiu_amc_io_read_buffer;
  343. mmr_names = bf50x_mmr_names;
  344. reg_size = sizeof (amc->bf50x) + 4;
  345. /* Initialize the AMC. */
  346. amc->bank_base = BFIN_EBIU_AMC_BASE;
  347. amc->bank_size = 1 * 1024 * 1024;
  348. amgctl = 0x00F3;
  349. amc->bf50x.ambctl0 = 0x0000FFC2;
  350. amc->bf50x.ambctl1 = 0x0000FFC2;
  351. amc->bf50x.mode = 0x0001;
  352. amc->bf50x.fctl = 0x0002;
  353. break;
  354. case 540 ... 549:
  355. amc->io_write = bf54x_ebiu_amc_io_write_buffer;
  356. amc->io_read = bf54x_ebiu_amc_io_read_buffer;
  357. mmr_names = bf54x_mmr_names;
  358. reg_size = sizeof (amc->bf54x) + 4;
  359. /* Initialize the AMC. */
  360. amc->bank_base = BFIN_EBIU_AMC_BASE;
  361. amc->bank_size = 64 * 1024 * 1024;
  362. amgctl = 0x0002;
  363. amc->bf54x.ambctl0 = 0xFFC2FFC2;
  364. amc->bf54x.ambctl1 = 0xFFC2FFC2;
  365. amc->bf54x.fctl = 0x0006;
  366. break;
  367. case 510 ... 519:
  368. case 522 ... 527:
  369. case 531 ... 533:
  370. case 534:
  371. case 536:
  372. case 537:
  373. case 538 ... 539:
  374. case 561:
  375. amc->io_write = bf53x_ebiu_amc_io_write_buffer;
  376. amc->io_read = bf53x_ebiu_amc_io_read_buffer;
  377. mmr_names = bf53x_mmr_names;
  378. reg_size = sizeof (amc->bf53x) + 4;
  379. /* Initialize the AMC. */
  380. amc->bank_base = BFIN_EBIU_AMC_BASE;
  381. if (amc->type == 561)
  382. amc->bank_size = 64 * 1024 * 1024;
  383. else
  384. amc->bank_size = 1 * 1024 * 1024;
  385. amgctl = 0x00F2;
  386. amc->bf53x.ambctl0 = 0xFFC2FFC2;
  387. amc->bf53x.ambctl1 = 0xFFC2FFC2;
  388. break;
  389. case 590 ... 599: /* BF59x has no AMC. */
  390. default:
  391. hw_abort (me, "no support for EBIU AMC on this Blackfin model yet");
  392. }
  393. attach_bfin_ebiu_amc_regs (me, amc, reg_size);
  394. bfin_ebiu_amc_write_amgctl (me, amc, amgctl);
  395. }
  396. const struct hw_descriptor dv_bfin_ebiu_amc_descriptor[] =
  397. {
  398. {"bfin_ebiu_amc", bfin_ebiu_amc_finish,},
  399. {NULL, NULL},
  400. };