allwinner-h3-dramc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Allwinner H3 SDRAM Controller emulation
  3. *
  4. * Copyright (C) 2019 Niek Linnenbank <nieklinnenbank@gmail.com>
  5. *
  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 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/units.h"
  21. #include "qemu/error-report.h"
  22. #include "hw/sysbus.h"
  23. #include "migration/vmstate.h"
  24. #include "qemu/log.h"
  25. #include "qemu/module.h"
  26. #include "exec/address-spaces.h"
  27. #include "hw/qdev-properties.h"
  28. #include "qapi/error.h"
  29. #include "hw/misc/allwinner-h3-dramc.h"
  30. #include "trace.h"
  31. #define REG_INDEX(offset) (offset / sizeof(uint32_t))
  32. /* DRAMCOM register offsets */
  33. enum {
  34. REG_DRAMCOM_CR = 0x0000, /* Control Register */
  35. };
  36. /* DRAMCTL register offsets */
  37. enum {
  38. REG_DRAMCTL_PIR = 0x0000, /* PHY Initialization Register */
  39. REG_DRAMCTL_PGSR = 0x0010, /* PHY General Status Register */
  40. REG_DRAMCTL_STATR = 0x0018, /* Status Register */
  41. };
  42. /* DRAMCTL register flags */
  43. enum {
  44. REG_DRAMCTL_PGSR_INITDONE = (1 << 0),
  45. };
  46. enum {
  47. REG_DRAMCTL_STATR_ACTIVE = (1 << 0),
  48. };
  49. static void allwinner_h3_dramc_map_rows(AwH3DramCtlState *s, uint8_t row_bits,
  50. uint8_t bank_bits, uint16_t page_size)
  51. {
  52. /*
  53. * This function simulates row addressing behavior when bootloader
  54. * software attempts to detect the amount of available SDRAM. In U-Boot
  55. * the controller is configured with the widest row addressing available.
  56. * Then a pattern is written to RAM at an offset on the row boundary size.
  57. * If the value read back equals the value read back from the
  58. * start of RAM, the bootloader knows the amount of row bits.
  59. *
  60. * This function inserts a mirrored memory region when the configured row
  61. * bits are not matching the actual emulated memory, to simulate the
  62. * same behavior on hardware as expected by the bootloader.
  63. */
  64. uint8_t row_bits_actual = 0;
  65. /* Calculate the actual row bits using the ram_size property */
  66. for (uint8_t i = 8; i < 12; i++) {
  67. if (1 << i == s->ram_size) {
  68. row_bits_actual = i + 3;
  69. break;
  70. }
  71. }
  72. if (s->ram_size == (1 << (row_bits - 3))) {
  73. /* When row bits is the expected value, remove the mirror */
  74. memory_region_set_enabled(&s->row_mirror_alias, false);
  75. trace_allwinner_h3_dramc_rowmirror_disable();
  76. } else if (row_bits_actual) {
  77. /* Row bits not matching ram_size, install the rows mirror */
  78. hwaddr row_mirror = s->ram_addr + ((1ULL << (row_bits_actual +
  79. bank_bits)) * page_size);
  80. memory_region_set_enabled(&s->row_mirror_alias, true);
  81. memory_region_set_address(&s->row_mirror_alias, row_mirror);
  82. trace_allwinner_h3_dramc_rowmirror_enable(row_mirror);
  83. }
  84. }
  85. static uint64_t allwinner_h3_dramcom_read(void *opaque, hwaddr offset,
  86. unsigned size)
  87. {
  88. const AwH3DramCtlState *s = AW_H3_DRAMC(opaque);
  89. const uint32_t idx = REG_INDEX(offset);
  90. if (idx >= AW_H3_DRAMCOM_REGS_NUM) {
  91. qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset 0x%04x\n",
  92. __func__, (uint32_t)offset);
  93. return 0;
  94. }
  95. trace_allwinner_h3_dramcom_read(offset, s->dramcom[idx], size);
  96. return s->dramcom[idx];
  97. }
  98. static void allwinner_h3_dramcom_write(void *opaque, hwaddr offset,
  99. uint64_t val, unsigned size)
  100. {
  101. AwH3DramCtlState *s = AW_H3_DRAMC(opaque);
  102. const uint32_t idx = REG_INDEX(offset);
  103. trace_allwinner_h3_dramcom_write(offset, val, size);
  104. if (idx >= AW_H3_DRAMCOM_REGS_NUM) {
  105. qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset 0x%04x\n",
  106. __func__, (uint32_t)offset);
  107. return;
  108. }
  109. switch (offset) {
  110. case REG_DRAMCOM_CR: /* Control Register */
  111. allwinner_h3_dramc_map_rows(s, ((val >> 4) & 0xf) + 1,
  112. ((val >> 2) & 0x1) + 2,
  113. 1 << (((val >> 8) & 0xf) + 3));
  114. break;
  115. default:
  116. break;
  117. };
  118. s->dramcom[idx] = (uint32_t) val;
  119. }
  120. static uint64_t allwinner_h3_dramctl_read(void *opaque, hwaddr offset,
  121. unsigned size)
  122. {
  123. const AwH3DramCtlState *s = AW_H3_DRAMC(opaque);
  124. const uint32_t idx = REG_INDEX(offset);
  125. if (idx >= AW_H3_DRAMCTL_REGS_NUM) {
  126. qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset 0x%04x\n",
  127. __func__, (uint32_t)offset);
  128. return 0;
  129. }
  130. trace_allwinner_h3_dramctl_read(offset, s->dramctl[idx], size);
  131. return s->dramctl[idx];
  132. }
  133. static void allwinner_h3_dramctl_write(void *opaque, hwaddr offset,
  134. uint64_t val, unsigned size)
  135. {
  136. AwH3DramCtlState *s = AW_H3_DRAMC(opaque);
  137. const uint32_t idx = REG_INDEX(offset);
  138. trace_allwinner_h3_dramctl_write(offset, val, size);
  139. if (idx >= AW_H3_DRAMCTL_REGS_NUM) {
  140. qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset 0x%04x\n",
  141. __func__, (uint32_t)offset);
  142. return;
  143. }
  144. switch (offset) {
  145. case REG_DRAMCTL_PIR: /* PHY Initialization Register */
  146. s->dramctl[REG_INDEX(REG_DRAMCTL_PGSR)] |= REG_DRAMCTL_PGSR_INITDONE;
  147. s->dramctl[REG_INDEX(REG_DRAMCTL_STATR)] |= REG_DRAMCTL_STATR_ACTIVE;
  148. break;
  149. default:
  150. break;
  151. }
  152. s->dramctl[idx] = (uint32_t) val;
  153. }
  154. static uint64_t allwinner_h3_dramphy_read(void *opaque, hwaddr offset,
  155. unsigned size)
  156. {
  157. const AwH3DramCtlState *s = AW_H3_DRAMC(opaque);
  158. const uint32_t idx = REG_INDEX(offset);
  159. if (idx >= AW_H3_DRAMPHY_REGS_NUM) {
  160. qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset 0x%04x\n",
  161. __func__, (uint32_t)offset);
  162. return 0;
  163. }
  164. trace_allwinner_h3_dramphy_read(offset, s->dramphy[idx], size);
  165. return s->dramphy[idx];
  166. }
  167. static void allwinner_h3_dramphy_write(void *opaque, hwaddr offset,
  168. uint64_t val, unsigned size)
  169. {
  170. AwH3DramCtlState *s = AW_H3_DRAMC(opaque);
  171. const uint32_t idx = REG_INDEX(offset);
  172. trace_allwinner_h3_dramphy_write(offset, val, size);
  173. if (idx >= AW_H3_DRAMPHY_REGS_NUM) {
  174. qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset 0x%04x\n",
  175. __func__, (uint32_t)offset);
  176. return;
  177. }
  178. s->dramphy[idx] = (uint32_t) val;
  179. }
  180. static const MemoryRegionOps allwinner_h3_dramcom_ops = {
  181. .read = allwinner_h3_dramcom_read,
  182. .write = allwinner_h3_dramcom_write,
  183. .endianness = DEVICE_LITTLE_ENDIAN,
  184. .valid = {
  185. .min_access_size = 4,
  186. .max_access_size = 4,
  187. },
  188. .impl.min_access_size = 4,
  189. };
  190. static const MemoryRegionOps allwinner_h3_dramctl_ops = {
  191. .read = allwinner_h3_dramctl_read,
  192. .write = allwinner_h3_dramctl_write,
  193. .endianness = DEVICE_LITTLE_ENDIAN,
  194. .valid = {
  195. .min_access_size = 4,
  196. .max_access_size = 4,
  197. },
  198. .impl.min_access_size = 4,
  199. };
  200. static const MemoryRegionOps allwinner_h3_dramphy_ops = {
  201. .read = allwinner_h3_dramphy_read,
  202. .write = allwinner_h3_dramphy_write,
  203. .endianness = DEVICE_LITTLE_ENDIAN,
  204. .valid = {
  205. .min_access_size = 4,
  206. .max_access_size = 4,
  207. },
  208. .impl.min_access_size = 4,
  209. };
  210. static void allwinner_h3_dramc_reset(DeviceState *dev)
  211. {
  212. AwH3DramCtlState *s = AW_H3_DRAMC(dev);
  213. /* Set default values for registers */
  214. memset(&s->dramcom, 0, sizeof(s->dramcom));
  215. memset(&s->dramctl, 0, sizeof(s->dramctl));
  216. memset(&s->dramphy, 0, sizeof(s->dramphy));
  217. }
  218. static void allwinner_h3_dramc_realize(DeviceState *dev, Error **errp)
  219. {
  220. AwH3DramCtlState *s = AW_H3_DRAMC(dev);
  221. /* Only power of 2 RAM sizes from 256MiB up to 2048MiB are supported */
  222. for (uint8_t i = 8; i < 13; i++) {
  223. if (1 << i == s->ram_size) {
  224. break;
  225. } else if (i == 12) {
  226. error_report("%s: ram-size %u MiB is not supported",
  227. __func__, s->ram_size);
  228. exit(1);
  229. }
  230. }
  231. /* Setup row mirror mappings */
  232. memory_region_init_ram(&s->row_mirror, OBJECT(s),
  233. "allwinner-h3-dramc.row-mirror",
  234. 4 * KiB, &error_abort);
  235. memory_region_add_subregion_overlap(get_system_memory(), s->ram_addr,
  236. &s->row_mirror, 10);
  237. memory_region_init_alias(&s->row_mirror_alias, OBJECT(s),
  238. "allwinner-h3-dramc.row-mirror-alias",
  239. &s->row_mirror, 0, 4 * KiB);
  240. memory_region_add_subregion_overlap(get_system_memory(),
  241. s->ram_addr + 1 * MiB,
  242. &s->row_mirror_alias, 10);
  243. memory_region_set_enabled(&s->row_mirror_alias, false);
  244. }
  245. static void allwinner_h3_dramc_init(Object *obj)
  246. {
  247. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  248. AwH3DramCtlState *s = AW_H3_DRAMC(obj);
  249. /* DRAMCOM registers */
  250. memory_region_init_io(&s->dramcom_iomem, OBJECT(s),
  251. &allwinner_h3_dramcom_ops, s,
  252. TYPE_AW_H3_DRAMC, 4 * KiB);
  253. sysbus_init_mmio(sbd, &s->dramcom_iomem);
  254. /* DRAMCTL registers */
  255. memory_region_init_io(&s->dramctl_iomem, OBJECT(s),
  256. &allwinner_h3_dramctl_ops, s,
  257. TYPE_AW_H3_DRAMC, 4 * KiB);
  258. sysbus_init_mmio(sbd, &s->dramctl_iomem);
  259. /* DRAMPHY registers */
  260. memory_region_init_io(&s->dramphy_iomem, OBJECT(s),
  261. &allwinner_h3_dramphy_ops, s,
  262. TYPE_AW_H3_DRAMC, 4 * KiB);
  263. sysbus_init_mmio(sbd, &s->dramphy_iomem);
  264. }
  265. static const Property allwinner_h3_dramc_properties[] = {
  266. DEFINE_PROP_UINT64("ram-addr", AwH3DramCtlState, ram_addr, 0x0),
  267. DEFINE_PROP_UINT32("ram-size", AwH3DramCtlState, ram_size, 256 * MiB),
  268. };
  269. static const VMStateDescription allwinner_h3_dramc_vmstate = {
  270. .name = "allwinner-h3-dramc",
  271. .version_id = 1,
  272. .minimum_version_id = 1,
  273. .fields = (const VMStateField[]) {
  274. VMSTATE_UINT32_ARRAY(dramcom, AwH3DramCtlState, AW_H3_DRAMCOM_REGS_NUM),
  275. VMSTATE_UINT32_ARRAY(dramctl, AwH3DramCtlState, AW_H3_DRAMCTL_REGS_NUM),
  276. VMSTATE_UINT32_ARRAY(dramphy, AwH3DramCtlState, AW_H3_DRAMPHY_REGS_NUM),
  277. VMSTATE_END_OF_LIST()
  278. }
  279. };
  280. static void allwinner_h3_dramc_class_init(ObjectClass *klass, void *data)
  281. {
  282. DeviceClass *dc = DEVICE_CLASS(klass);
  283. device_class_set_legacy_reset(dc, allwinner_h3_dramc_reset);
  284. dc->vmsd = &allwinner_h3_dramc_vmstate;
  285. dc->realize = allwinner_h3_dramc_realize;
  286. device_class_set_props(dc, allwinner_h3_dramc_properties);
  287. }
  288. static const TypeInfo allwinner_h3_dramc_info = {
  289. .name = TYPE_AW_H3_DRAMC,
  290. .parent = TYPE_SYS_BUS_DEVICE,
  291. .instance_init = allwinner_h3_dramc_init,
  292. .instance_size = sizeof(AwH3DramCtlState),
  293. .class_init = allwinner_h3_dramc_class_init,
  294. };
  295. static void allwinner_h3_dramc_register(void)
  296. {
  297. type_register_static(&allwinner_h3_dramc_info);
  298. }
  299. type_init(allwinner_h3_dramc_register)