dv-bfin_ebiu_sdc.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Blackfin External Bus Interface Unit (EBIU) SDRAM Controller (SDC) 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_ebiu_sdc.h"
  20. struct bfin_ebiu_sdc
  21. {
  22. bu32 base;
  23. int type;
  24. bu32 reg_size, bank_size;
  25. /* Order after here is important -- matches hardware MMR layout. */
  26. bu32 sdgctl;
  27. bu32 sdbctl; /* 16bit on most parts ... */
  28. bu16 BFIN_MMR_16(sdrrc);
  29. bu16 BFIN_MMR_16(sdstat);
  30. };
  31. #define mmr_base() offsetof(struct bfin_ebiu_sdc, sdgctl)
  32. #define mmr_offset(mmr) (offsetof(struct bfin_ebiu_sdc, mmr) - mmr_base())
  33. static const char * const mmr_names[] =
  34. {
  35. "EBIU_SDGCTL", "EBIU_SDBCTL", "EBIU_SDRRC", "EBIU_SDSTAT",
  36. };
  37. #define mmr_name(off) mmr_names[(off) / 4]
  38. static unsigned
  39. bfin_ebiu_sdc_io_write_buffer (struct hw *me, const void *source,
  40. int space, address_word addr, unsigned nr_bytes)
  41. {
  42. struct bfin_ebiu_sdc *sdc = hw_data (me);
  43. bu32 mmr_off;
  44. bu32 value;
  45. bu16 *value16p;
  46. bu32 *value32p;
  47. void *valuep;
  48. /* Invalid access mode is higher priority than missing register. */
  49. if (!dv_bfin_mmr_require_16_32 (me, addr, nr_bytes, true))
  50. return 0;
  51. if (nr_bytes == 4)
  52. value = dv_load_4 (source);
  53. else
  54. value = dv_load_2 (source);
  55. mmr_off = addr - sdc->base;
  56. valuep = (void *)((uintptr_t)sdc + mmr_base() + mmr_off);
  57. value16p = valuep;
  58. value32p = valuep;
  59. HW_TRACE_WRITE ();
  60. switch (mmr_off)
  61. {
  62. case mmr_offset(sdgctl):
  63. /* XXX: SRFS should make external mem unreadable. */
  64. *value32p = value;
  65. break;
  66. case mmr_offset(sdbctl):
  67. if (sdc->type == 561)
  68. {
  69. if (!dv_bfin_mmr_require_32 (me, addr, nr_bytes, true))
  70. return 0;
  71. *value32p = value;
  72. }
  73. else
  74. {
  75. if (!dv_bfin_mmr_require_16 (me, addr, nr_bytes, true))
  76. return 0;
  77. *value16p = value;
  78. }
  79. break;
  80. case mmr_offset(sdrrc):
  81. if (!dv_bfin_mmr_require_16 (me, addr, nr_bytes, true))
  82. return 0;
  83. *value16p = value;
  84. break;
  85. case mmr_offset(sdstat):
  86. if (!dv_bfin_mmr_require_16 (me, addr, nr_bytes, true))
  87. return 0;
  88. /* XXX: Some bits are W1C ... */
  89. break;
  90. }
  91. return nr_bytes;
  92. }
  93. static unsigned
  94. bfin_ebiu_sdc_io_read_buffer (struct hw *me, void *dest,
  95. int space, address_word addr, unsigned nr_bytes)
  96. {
  97. struct bfin_ebiu_sdc *sdc = hw_data (me);
  98. bu32 mmr_off;
  99. bu32 *value32p;
  100. bu16 *value16p;
  101. void *valuep;
  102. /* Invalid access mode is higher priority than missing register. */
  103. if (!dv_bfin_mmr_require_16_32 (me, addr, nr_bytes, false))
  104. return 0;
  105. mmr_off = addr - sdc->base;
  106. valuep = (void *)((uintptr_t)sdc + mmr_base() + mmr_off);
  107. value16p = valuep;
  108. value32p = valuep;
  109. HW_TRACE_READ ();
  110. switch (mmr_off)
  111. {
  112. case mmr_offset(sdgctl):
  113. dv_store_4 (dest, *value32p);
  114. break;
  115. case mmr_offset(sdbctl):
  116. if (sdc->type == 561)
  117. {
  118. if (!dv_bfin_mmr_require_32 (me, addr, nr_bytes, false))
  119. return 0;
  120. dv_store_4 (dest, *value32p);
  121. }
  122. else
  123. {
  124. if (!dv_bfin_mmr_require_16 (me, addr, nr_bytes, false))
  125. return 0;
  126. dv_store_2 (dest, *value16p);
  127. }
  128. break;
  129. case mmr_offset(sdrrc):
  130. case mmr_offset(sdstat):
  131. if (!dv_bfin_mmr_require_16 (me, addr, nr_bytes, false))
  132. return 0;
  133. dv_store_2 (dest, *value16p);
  134. break;
  135. }
  136. return nr_bytes;
  137. }
  138. static void
  139. attach_bfin_ebiu_sdc_regs (struct hw *me, struct bfin_ebiu_sdc *sdc)
  140. {
  141. address_word attach_address;
  142. int attach_space;
  143. unsigned attach_size;
  144. reg_property_spec reg;
  145. if (hw_find_property (me, "reg") == NULL)
  146. hw_abort (me, "Missing \"reg\" property");
  147. if (!hw_find_reg_array_property (me, "reg", 0, &reg))
  148. hw_abort (me, "\"reg\" property must contain three addr/size entries");
  149. hw_unit_address_to_attach_address (hw_parent (me),
  150. &reg.address,
  151. &attach_space, &attach_address, me);
  152. hw_unit_size_to_attach_size (hw_parent (me), &reg.size, &attach_size, me);
  153. if (attach_size != BFIN_MMR_EBIU_SDC_SIZE)
  154. hw_abort (me, "\"reg\" size must be %#x", BFIN_MMR_EBIU_SDC_SIZE);
  155. hw_attach_address (hw_parent (me),
  156. 0, attach_space, attach_address, attach_size, me);
  157. sdc->base = attach_address;
  158. }
  159. static void
  160. bfin_ebiu_sdc_finish (struct hw *me)
  161. {
  162. struct bfin_ebiu_sdc *sdc;
  163. sdc = HW_ZALLOC (me, struct bfin_ebiu_sdc);
  164. set_hw_data (me, sdc);
  165. set_hw_io_read_buffer (me, bfin_ebiu_sdc_io_read_buffer);
  166. set_hw_io_write_buffer (me, bfin_ebiu_sdc_io_write_buffer);
  167. attach_bfin_ebiu_sdc_regs (me, sdc);
  168. sdc->type = hw_find_integer_property (me, "type");
  169. /* Initialize the SDC. */
  170. sdc->sdgctl = 0xE0088849;
  171. sdc->sdbctl = 0x00000000;
  172. sdc->sdrrc = 0x081A;
  173. sdc->sdstat = 0x0008;
  174. /* XXX: We boot with 64M external memory by default ... */
  175. sdc->sdbctl |= EBE | EBSZ_64 | EBCAW_10;
  176. }
  177. const struct hw_descriptor dv_bfin_ebiu_sdc_descriptor[] =
  178. {
  179. {"bfin_ebiu_sdc", bfin_ebiu_sdc_finish,},
  180. {NULL, NULL},
  181. };