ds1225y.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 "qemu/osdep.h"
  25. #include "hw/qdev-properties.h"
  26. #include "hw/sysbus.h"
  27. #include "migration/vmstate.h"
  28. #include "trace.h"
  29. #include "qemu/error-report.h"
  30. #include "qemu/module.h"
  31. #include "qom/object.h"
  32. typedef struct {
  33. MemoryRegion iomem;
  34. uint32_t chip_size;
  35. char *filename;
  36. FILE *file;
  37. uint8_t *contents;
  38. } NvRamState;
  39. static uint64_t nvram_read(void *opaque, hwaddr addr, unsigned size)
  40. {
  41. NvRamState *s = opaque;
  42. uint32_t val;
  43. val = s->contents[addr];
  44. trace_nvram_read(addr, val);
  45. return val;
  46. }
  47. static void nvram_write(void *opaque, hwaddr addr, uint64_t val,
  48. unsigned size)
  49. {
  50. NvRamState *s = opaque;
  51. val &= 0xff;
  52. trace_nvram_write(addr, s->contents[addr], val);
  53. s->contents[addr] = val;
  54. if (s->file) {
  55. fseek(s->file, addr, SEEK_SET);
  56. fputc(val, s->file);
  57. fflush(s->file);
  58. }
  59. }
  60. static const MemoryRegionOps nvram_ops = {
  61. .read = nvram_read,
  62. .write = nvram_write,
  63. .impl = {
  64. .min_access_size = 1,
  65. .max_access_size = 1,
  66. },
  67. .endianness = DEVICE_LITTLE_ENDIAN,
  68. };
  69. static int nvram_post_load(void *opaque, int version_id)
  70. {
  71. NvRamState *s = opaque;
  72. /* Close file, as filename may has changed in load/store process */
  73. if (s->file) {
  74. fclose(s->file);
  75. }
  76. /* Write back nvram contents */
  77. s->file = s->filename ? fopen(s->filename, "wb") : NULL;
  78. if (s->file) {
  79. /* Write back contents, as 'wb' mode cleaned the file */
  80. if (fwrite(s->contents, s->chip_size, 1, s->file) != 1) {
  81. printf("nvram_post_load: short write\n");
  82. }
  83. fflush(s->file);
  84. }
  85. return 0;
  86. }
  87. static const VMStateDescription vmstate_nvram = {
  88. .name = "nvram",
  89. .version_id = 0,
  90. .minimum_version_id = 0,
  91. .post_load = nvram_post_load,
  92. .fields = (const VMStateField[]) {
  93. VMSTATE_VARRAY_UINT32(contents, NvRamState, chip_size, 0,
  94. vmstate_info_uint8, uint8_t),
  95. VMSTATE_END_OF_LIST()
  96. }
  97. };
  98. #define TYPE_DS1225Y "ds1225y"
  99. OBJECT_DECLARE_SIMPLE_TYPE(SysBusNvRamState, DS1225Y)
  100. struct SysBusNvRamState {
  101. SysBusDevice parent_obj;
  102. NvRamState nvram;
  103. };
  104. static void nvram_sysbus_realize(DeviceState *dev, Error **errp)
  105. {
  106. SysBusNvRamState *sys = DS1225Y(dev);
  107. NvRamState *s = &sys->nvram;
  108. FILE *file;
  109. s->contents = g_malloc0(s->chip_size);
  110. memory_region_init_io(&s->iomem, OBJECT(s), &nvram_ops, s,
  111. "nvram", s->chip_size);
  112. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
  113. /* Read current file */
  114. file = s->filename ? fopen(s->filename, "rb") : NULL;
  115. if (file) {
  116. /* Read nvram contents */
  117. if (fread(s->contents, s->chip_size, 1, file) != 1) {
  118. error_report("nvram_sysbus_realize: short read");
  119. }
  120. fclose(file);
  121. }
  122. nvram_post_load(s, 0);
  123. }
  124. static const Property nvram_sysbus_properties[] = {
  125. DEFINE_PROP_UINT32("size", SysBusNvRamState, nvram.chip_size, 0x2000),
  126. DEFINE_PROP_STRING("filename", SysBusNvRamState, nvram.filename),
  127. };
  128. static void nvram_sysbus_class_init(ObjectClass *klass, void *data)
  129. {
  130. DeviceClass *dc = DEVICE_CLASS(klass);
  131. dc->realize = nvram_sysbus_realize;
  132. dc->vmsd = &vmstate_nvram;
  133. device_class_set_props(dc, nvram_sysbus_properties);
  134. }
  135. static const TypeInfo nvram_sysbus_info = {
  136. .name = TYPE_DS1225Y,
  137. .parent = TYPE_SYS_BUS_DEVICE,
  138. .instance_size = sizeof(SysBusNvRamState),
  139. .class_init = nvram_sysbus_class_init,
  140. };
  141. static void nvram_register_types(void)
  142. {
  143. type_register_static(&nvram_sysbus_info);
  144. }
  145. type_init(nvram_register_types)