Patchwork [18/20,MIPS] qdev: convert ds1225y nvram to sysbus device

login
register
mail settings
Submitter Hervé Poussineau
Date Aug. 1, 2010, 5:37 p.m.
Message ID <1280684242-19611-18-git-send-email-hpoussin@reactos.org>
Download mbox | patch
Permalink /patch/60491/
State New
Headers show

Comments

Hervé Poussineau - Aug. 1, 2010, 5:37 p.m.
Use it in Jazz emulation
Remove protection stuff, which doesn't belong to this device
Remove ds1225y_init() and ds1225y_set_protection() functions, which are not used anymore

Signed-off-by: Hervé Poussineau <hpoussin@reactos.org>
---
 hw/ds1225y.c   |  137 ++++++++++++++++++++++++++++++--------------------------
 hw/mips.h      |    4 --
 hw/mips_jazz.c |    5 +-
 3 files changed, 76 insertions(+), 70 deletions(-)

Patch

diff --git a/hw/ds1225y.c b/hw/ds1225y.c
index 009d127..afbfcb0 100644
--- a/hw/ds1225y.c
+++ b/hw/ds1225y.c
@@ -25,15 +25,20 @@ 
 #include "hw.h"
 #include "mips.h"
 #include "nvram.h"
+#include "sysbus.h"
+#include "qdev-addr.h"
 
 //#define DEBUG_NVRAM
 
 typedef struct ds1225y_t
 {
-    uint32_t chip_size;
+    SysBusDevice busdev;
+
+    target_phys_addr_t iobase;
+    uint32_t size;
+    char *filename;
     QEMUFile *file;
     uint8_t *contents;
-    uint8_t protection;
 } ds1225y_t;
 
 
@@ -98,34 +103,6 @@  static void nvram_writel (void *opaque, target_phys_addr_t addr, uint32_t val)
     nvram_writeb(opaque, addr + 3, (val >> 24) & 0xff);
 }
 
-static void nvram_writeb_protected (void *opaque, target_phys_addr_t addr, uint32_t val)
-{
-    ds1225y_t *s = opaque;
-
-    if (s->protection != 7) {
-#ifdef DEBUG_NVRAM
-    printf("nvram: prevent write of 0x%x at " TARGET_FMT_lx "\n", val, addr);
-#endif
-        return;
-    }
-
-    nvram_writeb(opaque, addr, val);
-}
-
-static void nvram_writew_protected (void *opaque, target_phys_addr_t addr, uint32_t val)
-{
-    nvram_writeb_protected(opaque, addr, val & 0xff);
-    nvram_writeb_protected(opaque, addr + 1, (val >> 8) & 0xff);
-}
-
-static void nvram_writel_protected (void *opaque, target_phys_addr_t addr, uint32_t val)
-{
-    nvram_writeb_protected(opaque, addr, val & 0xff);
-    nvram_writeb_protected(opaque, addr + 1, (val >> 8) & 0xff);
-    nvram_writeb_protected(opaque, addr + 2, (val >> 16) & 0xff);
-    nvram_writeb_protected(opaque, addr + 3, (val >> 24) & 0xff);
-}
-
 static CPUReadMemoryFunc * const nvram_read[] = {
     &nvram_readb,
     &nvram_readw,
@@ -138,43 +115,75 @@  static CPUWriteMemoryFunc * const nvram_write[] = {
     &nvram_writel,
 };
 
-static CPUWriteMemoryFunc * const nvram_write_protected[] = {
-    &nvram_writeb_protected,
-    &nvram_writew_protected,
-    &nvram_writel_protected,
-};
-
-/* Initialisation routine */
-void *ds1225y_init(target_phys_addr_t mem_base, const char *filename)
+static int ds1225y_init(SysBusDevice *dev)
 {
-    ds1225y_t *s;
-    int mem_indexRW, mem_indexRP;
+    ds1225y_t *s = FROM_SYSBUS(ds1225y_t, dev);
+    int mem_index;
     QEMUFile *file;
 
-    s = qemu_mallocz(sizeof(ds1225y_t));
-    s->chip_size = 0x2000; /* Fixed for ds1225y chip: 8 KiB */
-    s->contents = qemu_mallocz(s->chip_size);
-    s->protection = 7;
-
-    /* Read current file */
-    file = qemu_fopen(filename, "rb");
-    if (file) {
-        /* Read nvram contents */
-        qemu_get_buffer(file, s->contents, s->chip_size);
-        qemu_fclose(file);
-    }
-    s->file = qemu_fopen(filename, "wb");
-    if (s->file) {
-        /* Write back contents, as 'wb' mode cleaned the file */
-        qemu_put_buffer(s->file, s->contents, s->chip_size);
-        qemu_fflush(s->file);
+    if (s->iobase == -1)
+      return -1;
+
+    s->contents = qemu_mallocz(s->size);
+
+    if (s->filename) {
+        /* Read current file */
+        file = qemu_fopen(s->filename, "rb");
+        if (file) {
+            /* Read nvram contents */
+            qemu_get_buffer(file, s->contents, s->size);
+            qemu_fclose(file);
+        }
+        s->file = qemu_fopen(s->filename, "wb");
+        if (s->file) {
+            /* Write back contents, as 'wb' mode cleaned the file */
+            qemu_put_buffer(s->file, s->contents, s->size);
+            qemu_fflush(s->file);
+        }
     }
 
-    /* Read/write memory */
-    mem_indexRW = cpu_register_io_memory(nvram_read, nvram_write, s);
-    cpu_register_physical_memory(mem_base, s->chip_size, mem_indexRW);
-    /* Read/write protected memory */
-    mem_indexRP = cpu_register_io_memory(nvram_read, nvram_write_protected, s);
-    cpu_register_physical_memory(mem_base + s->chip_size, s->chip_size, mem_indexRP);
-    return s;
+    mem_index = cpu_register_io_memory(nvram_read, nvram_write, s);
+    cpu_register_physical_memory(s->iobase, s->size, mem_index);
+
+    return 0;
+}
+
+static int ds1225y_init_nvram(SysBusDevice *dev)
+{
+  ds1225y_t *s = FROM_SYSBUS(ds1225y_t, dev);
+  s->filename = (char*)"nvram";
+  return ds1225y_init(dev);
+}
+
+static SysBusDeviceInfo ds1225y_device_info = {
+    .qdev.name  = "ds1225y",
+    .qdev.size  = sizeof(ds1225y_t),
+    .init       = ds1225y_init,
+    .qdev.props = (Property[]) {
+        DEFINE_PROP_TADDR("iobase", ds1225y_t, iobase, -1),
+        DEFINE_PROP_HEX32("size", ds1225y_t, size, 0x2000),
+        DEFINE_PROP_STRING("filename", ds1225y_t, filename),
+        DEFINE_PROP_END_OF_LIST(),
+    },
+};
+
+static SysBusDeviceInfo ds1225y_device_info_nvram = {
+    .qdev.name    =   "ds1225y,filename=nvram",
+    .qdev.size    = sizeof(ds1225y_t),
+    .qdev.no_user = 1,
+    .init         = ds1225y_init_nvram,
+    .qdev.props   = (Property[]) {
+        DEFINE_PROP_TADDR("iobase", ds1225y_t, iobase, 0x80009000),
+        DEFINE_PROP_HEX32("size", ds1225y_t, size, 0x2000),
+        DEFINE_PROP_STRING("filename", ds1225y_t, filename),
+        DEFINE_PROP_END_OF_LIST(),
+    },
+};
+
+static void ds1225y_register_devices(void)
+{
+    sysbus_register_withprop(&ds1225y_device_info);
+    sysbus_register_withprop(&ds1225y_device_info_nvram);
 }
+
+device_init(ds1225y_register_devices)
diff --git a/hw/mips.h b/hw/mips.h
index d8e4954..5d59b1e 100644
--- a/hw/mips.h
+++ b/hw/mips.h
@@ -8,10 +8,6 @@  PCIBus *pci_gt64120_init(qemu_irq *pic);
 /* bonito.c */
 PCIBus *bonito_init(qemu_irq *pic);
 
-/* ds1225y.c */
-void *ds1225y_init(target_phys_addr_t mem_base, const char *filename);
-void ds1225y_set_protection(void *opaque, int protection);
-
 /* mipsnet.c */
 void mipsnet_init(int base, qemu_irq irq, NICInfo *nd);
 
diff --git a/hw/mips_jazz.c b/hw/mips_jazz.c
index 08175ab..6daf76e 100644
--- a/hw/mips_jazz.c
+++ b/hw/mips_jazz.c
@@ -247,8 +247,9 @@  void mips_jazz_init (ram_addr_t ram_size,
     /* FIXME: missing Jazz sound at 0x8000c000, rc4030[2] */
     audio_init(i8259);
 
-    /* NVRAM: Unprotected at 0x9000, Protected at 0xa000, Read only at 0xb000 */
-    ds1225y_init(0x80009000, "nvram");
+    /* NVRAM */
+    dev = qdev_create(NULL, "ds1225y,filename=nvram");
+    qdev_init_nofail(dev);
 
     /* LED indicator */
     dev = qdev_create(NULL, "jazz-led");