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. typedef struct {
  32. MemoryRegion iomem;
  33. uint32_t chip_size;
  34. char *filename;
  35. FILE *file;
  36. uint8_t *contents;
  37. } NvRamState;
  38. static uint64_t nvram_read(void *opaque, hwaddr addr, unsigned size)
  39. {
  40. NvRamState *s = opaque;
  41. uint32_t val;
  42. val = s->contents[addr];
  43. trace_nvram_read(addr, val);
  44. return val;
  45. }
  46. static void nvram_write(void *opaque, hwaddr addr, uint64_t val,
  47. unsigned size)
  48. {
  49. NvRamState *s = opaque;
  50. val &= 0xff;
  51. trace_nvram_write(addr, s->contents[addr], val);
  52. s->contents[addr] = val;
  53. if (s->file) {
  54. fseek(s->file, addr, SEEK_SET);
  55. fputc(val, s->file);
  56. fflush(s->file);
  57. }
  58. }
  59. static const MemoryRegionOps nvram_ops = {
  60. .read = nvram_read,
  61. .write = nvram_write,
  62. .impl = {
  63. .min_access_size = 1,
  64. .max_access_size = 1,
  65. },
  66. .endianness = DEVICE_LITTLE_ENDIAN,
  67. };
  68. static int nvram_post_load(void *opaque, int version_id)
  69. {
  70. NvRamState *s = opaque;
  71. /* Close file, as filename may has changed in load/store process */
  72. if (s->file) {
  73. fclose(s->file);
  74. }
  75. /* Write back nvram contents */
  76. s->file = s->filename ? fopen(s->filename, "wb") : NULL;
  77. if (s->file) {
  78. /* Write back contents, as 'wb' mode cleaned the file */
  79. if (fwrite(s->contents, s->chip_size, 1, s->file) != 1) {
  80. printf("nvram_post_load: short write\n");
  81. }
  82. fflush(s->file);
  83. }
  84. return 0;
  85. }
  86. static const VMStateDescription vmstate_nvram = {
  87. .name = "nvram",
  88. .version_id = 0,
  89. .minimum_version_id = 0,
  90. .post_load = nvram_post_load,
  91. .fields = (VMStateField[]) {
  92. VMSTATE_VARRAY_UINT32(contents, NvRamState, chip_size, 0,
  93. vmstate_info_uint8, uint8_t),
  94. VMSTATE_END_OF_LIST()
  95. }
  96. };
  97. #define TYPE_DS1225Y "ds1225y"
  98. #define DS1225Y(obj) OBJECT_CHECK(SysBusNvRamState, (obj), TYPE_DS1225Y)
  99. typedef struct {
  100. SysBusDevice parent_obj;
  101. NvRamState nvram;
  102. } SysBusNvRamState;
  103. static void nvram_sysbus_realize(DeviceState *dev, Error **errp)
  104. {
  105. SysBusNvRamState *sys = DS1225Y(dev);
  106. NvRamState *s = &sys->nvram;
  107. FILE *file;
  108. s->contents = g_malloc0(s->chip_size);
  109. memory_region_init_io(&s->iomem, OBJECT(s), &nvram_ops, s,
  110. "nvram", s->chip_size);
  111. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
  112. /* Read current file */
  113. file = s->filename ? fopen(s->filename, "rb") : NULL;
  114. if (file) {
  115. /* Read nvram contents */
  116. if (fread(s->contents, s->chip_size, 1, file) != 1) {
  117. error_report("nvram_sysbus_realize: short read");
  118. }
  119. fclose(file);
  120. }
  121. nvram_post_load(s, 0);
  122. }
  123. static Property nvram_sysbus_properties[] = {
  124. DEFINE_PROP_UINT32("size", SysBusNvRamState, nvram.chip_size, 0x2000),
  125. DEFINE_PROP_STRING("filename", SysBusNvRamState, nvram.filename),
  126. DEFINE_PROP_END_OF_LIST(),
  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. dc->props = 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)