ds1225y.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * QEMU NVRAM emulation for DS1225Y chip
  3. *
  4. * Copyright (c) 2007-2008 Hervé Poussineau
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "sysbus.h"
  25. #include "trace.h"
  26. typedef struct {
  27. DeviceState qdev;
  28. MemoryRegion iomem;
  29. uint32_t chip_size;
  30. char *filename;
  31. FILE *file;
  32. uint8_t *contents;
  33. } NvRamState;
  34. static uint64_t nvram_read(void *opaque, hwaddr addr, unsigned size)
  35. {
  36. NvRamState *s = opaque;
  37. uint32_t val;
  38. val = s->contents[addr];
  39. trace_nvram_read(addr, val);
  40. return val;
  41. }
  42. static void nvram_write(void *opaque, hwaddr addr, uint64_t val,
  43. unsigned size)
  44. {
  45. NvRamState *s = opaque;
  46. val &= 0xff;
  47. trace_nvram_write(addr, s->contents[addr], val);
  48. s->contents[addr] = val;
  49. if (s->file) {
  50. fseek(s->file, addr, SEEK_SET);
  51. fputc(val, s->file);
  52. fflush(s->file);
  53. }
  54. }
  55. static const MemoryRegionOps nvram_ops = {
  56. .read = nvram_read,
  57. .write = nvram_write,
  58. .impl = {
  59. .min_access_size = 1,
  60. .max_access_size = 1,
  61. },
  62. .endianness = DEVICE_LITTLE_ENDIAN,
  63. };
  64. static int nvram_post_load(void *opaque, int version_id)
  65. {
  66. NvRamState *s = opaque;
  67. /* Close file, as filename may has changed in load/store process */
  68. if (s->file) {
  69. fclose(s->file);
  70. }
  71. /* Write back nvram contents */
  72. s->file = fopen(s->filename, "wb");
  73. if (s->file) {
  74. /* Write back contents, as 'wb' mode cleaned the file */
  75. if (fwrite(s->contents, s->chip_size, 1, s->file) != 1) {
  76. printf("nvram_post_load: short write\n");
  77. }
  78. fflush(s->file);
  79. }
  80. return 0;
  81. }
  82. static const VMStateDescription vmstate_nvram = {
  83. .name = "nvram",
  84. .version_id = 0,
  85. .minimum_version_id = 0,
  86. .minimum_version_id_old = 0,
  87. .post_load = nvram_post_load,
  88. .fields = (VMStateField[]) {
  89. VMSTATE_VARRAY_UINT32(contents, NvRamState, chip_size, 0,
  90. vmstate_info_uint8, uint8_t),
  91. VMSTATE_END_OF_LIST()
  92. }
  93. };
  94. typedef struct {
  95. SysBusDevice busdev;
  96. NvRamState nvram;
  97. } SysBusNvRamState;
  98. static int nvram_sysbus_initfn(SysBusDevice *dev)
  99. {
  100. NvRamState *s = &FROM_SYSBUS(SysBusNvRamState, dev)->nvram;
  101. FILE *file;
  102. s->contents = g_malloc0(s->chip_size);
  103. memory_region_init_io(&s->iomem, &nvram_ops, s, "nvram", s->chip_size);
  104. sysbus_init_mmio(dev, &s->iomem);
  105. /* Read current file */
  106. file = fopen(s->filename, "rb");
  107. if (file) {
  108. /* Read nvram contents */
  109. if (fread(s->contents, s->chip_size, 1, file) != 1) {
  110. printf("nvram_sysbus_initfn: short read\n");
  111. }
  112. fclose(file);
  113. }
  114. nvram_post_load(s, 0);
  115. return 0;
  116. }
  117. static Property nvram_sysbus_properties[] = {
  118. DEFINE_PROP_UINT32("size", SysBusNvRamState, nvram.chip_size, 0x2000),
  119. DEFINE_PROP_STRING("filename", SysBusNvRamState, nvram.filename),
  120. DEFINE_PROP_END_OF_LIST(),
  121. };
  122. static void nvram_sysbus_class_init(ObjectClass *klass, void *data)
  123. {
  124. DeviceClass *dc = DEVICE_CLASS(klass);
  125. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  126. k->init = nvram_sysbus_initfn;
  127. dc->vmsd = &vmstate_nvram;
  128. dc->props = nvram_sysbus_properties;
  129. }
  130. static const TypeInfo nvram_sysbus_info = {
  131. .name = "ds1225y",
  132. .parent = TYPE_SYS_BUS_DEVICE,
  133. .instance_size = sizeof(SysBusNvRamState),
  134. .class_init = nvram_sysbus_class_init,
  135. };
  136. static void nvram_register_types(void)
  137. {
  138. type_register_static(&nvram_sysbus_info);
  139. }
  140. type_init(nvram_register_types)