Install keymaps from new location
[qemu] / hw / stellaris.c
index cc47b9d..a107db7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Luminary Micro Stellaris preipherals
+ * Luminary Micro Stellaris peripherals
  *
  * Copyright (c) 2006 CodeSourcery.
  * Written by Paul Brook
@@ -7,15 +7,29 @@
  * This code is licenced under the GPL.
  */
 
-#include "hw.h"
+#include "sysbus.h"
+#include "ssi.h"
 #include "arm-misc.h"
 #include "primecell.h"
 #include "devices.h"
 #include "qemu-timer.h"
 #include "i2c.h"
+#include "net.h"
 #include "sysemu.h"
 #include "boards.h"
 
+#define GPIO_A 0
+#define GPIO_B 1
+#define GPIO_C 2
+#define GPIO_D 3
+#define GPIO_E 4
+#define GPIO_F 5
+#define GPIO_G 6
+
+#define BP_OLED_I2C  0x01
+#define BP_OLED_SSI  0x02
+#define BP_GAMEPAD   0x04
+
 typedef const struct {
     const char *name;
     uint32_t did0;
@@ -25,15 +39,11 @@ typedef const struct {
     uint32_t dc2;
     uint32_t dc3;
     uint32_t dc4;
-    enum {OLED_I2C, OLED_SSI} oled;
+    uint32_t peripherals;
 } stellaris_board_info;
 
 /* General purpose timer module.  */
 
-/* Multiplication factor to convert from GPTM timer ticks to qemu timer
-   ticks.  */
-static int stellaris_clock_scale;
-
 typedef struct gptm_state {
     uint32_t config;
     uint32_t mode[2];
@@ -47,7 +57,6 @@ typedef struct gptm_state {
     uint32_t rtc;
     int64_t tick[2];
     struct gptm_state *opaque[2];
-    uint32_t base;
     QEMUTimer *timer[2];
     /* The timers have an alternate output used to trigger the ADC.  */
     qemu_irq trigger;
@@ -78,15 +87,14 @@ static void gptm_reload(gptm_state *s, int n, int reset)
         /* 32-bit CountDown.  */
         uint32_t count;
         count = s->load[0] | (s->load[1] << 16);
-        tick += (int64_t)count * stellaris_clock_scale;
+        tick += (int64_t)count * system_clock_scale;
     } else if (s->config == 1) {
         /* 32-bit RTC.  1Hz tick.  */
         tick += ticks_per_sec;
     } else if (s->mode[n] == 0xa) {
         /* PWM mode.  Not implemented.  */
     } else {
-        cpu_abort(cpu_single_env, "TODO: 16-bit timer mode 0x%x\n",
-                  s->mode[n]);
+        hw_error("TODO: 16-bit timer mode 0x%x\n", s->mode[n]);
     }
     s->tick[n] = tick;
     qemu_mod_timer(s->timer[n], tick);
@@ -128,8 +136,7 @@ static void gptm_tick(void *opaque)
     } else if (s->mode[n] == 0xa) {
         /* PWM mode.  Not implemented.  */
     } else {
-        cpu_abort(cpu_single_env, "TODO: 16-bit timer mode 0x%x\n",
-                  s->mode[n]);
+        hw_error("TODO: 16-bit timer mode 0x%x\n", s->mode[n]);
     }
     gptm_update_irq(s);
 }
@@ -138,7 +145,6 @@ static uint32_t gptm_read(void *opaque, target_phys_addr_t offset)
 {
     gptm_state *s = (gptm_state *)opaque;
 
-    offset -= s->base;
     switch (offset) {
     case 0x00: /* CFG */
         return s->config;
@@ -176,9 +182,9 @@ static uint32_t gptm_read(void *opaque, target_phys_addr_t offset)
         if (s->control == 1)
             return s->rtc;
     case 0x4c: /* TBR */
-        cpu_abort(cpu_single_env, "TODO: Timer value read\n");
+        hw_error("TODO: Timer value read\n");
     default:
-        cpu_abort(cpu_single_env, "gptm_read: Bad offset 0x%x\n", (int)offset);
+        hw_error("gptm_read: Bad offset 0x%x\n", (int)offset);
         return 0;
     }
 }
@@ -188,7 +194,6 @@ static void gptm_write(void *opaque, target_phys_addr_t offset, uint32_t value)
     gptm_state *s = (gptm_state *)opaque;
     uint32_t oldval;
 
-    offset -= s->base;
     /* The timers should be disabled before changing the configuration.
        We take advantage of this and defer everything until the timer
        is enabled.  */
@@ -259,7 +264,7 @@ static void gptm_write(void *opaque, target_phys_addr_t offset, uint32_t value)
         s->match_prescale[0] = value;
         break;
     default:
-        cpu_abort(cpu_single_env, "gptm_write: Bad offset 0x%x\n", (int)offset);
+        hw_error("gptm_write: Bad offset 0x%x\n", (int)offset);
     }
     gptm_update_irq(s);
 }
@@ -276,13 +281,71 @@ static CPUWriteMemoryFunc *gptm_writefn[] = {
    gptm_write
 };
 
+static void gptm_save(QEMUFile *f, void *opaque)
+{
+    gptm_state *s = (gptm_state *)opaque;
+
+    qemu_put_be32(f, s->config);
+    qemu_put_be32(f, s->mode[0]);
+    qemu_put_be32(f, s->mode[1]);
+    qemu_put_be32(f, s->control);
+    qemu_put_be32(f, s->state);
+    qemu_put_be32(f, s->mask);
+    qemu_put_be32(f, s->mode[0]);
+    qemu_put_be32(f, s->mode[0]);
+    qemu_put_be32(f, s->load[0]);
+    qemu_put_be32(f, s->load[1]);
+    qemu_put_be32(f, s->match[0]);
+    qemu_put_be32(f, s->match[1]);
+    qemu_put_be32(f, s->prescale[0]);
+    qemu_put_be32(f, s->prescale[1]);
+    qemu_put_be32(f, s->match_prescale[0]);
+    qemu_put_be32(f, s->match_prescale[1]);
+    qemu_put_be32(f, s->rtc);
+    qemu_put_be64(f, s->tick[0]);
+    qemu_put_be64(f, s->tick[1]);
+    qemu_put_timer(f, s->timer[0]);
+    qemu_put_timer(f, s->timer[1]);
+}
+
+static int gptm_load(QEMUFile *f, void *opaque, int version_id)
+{
+    gptm_state *s = (gptm_state *)opaque;
+
+    if (version_id != 1)
+        return -EINVAL;
+
+    s->config = qemu_get_be32(f);
+    s->mode[0] = qemu_get_be32(f);
+    s->mode[1] = qemu_get_be32(f);
+    s->control = qemu_get_be32(f);
+    s->state = qemu_get_be32(f);
+    s->mask = qemu_get_be32(f);
+    s->mode[0] = qemu_get_be32(f);
+    s->mode[0] = qemu_get_be32(f);
+    s->load[0] = qemu_get_be32(f);
+    s->load[1] = qemu_get_be32(f);
+    s->match[0] = qemu_get_be32(f);
+    s->match[1] = qemu_get_be32(f);
+    s->prescale[0] = qemu_get_be32(f);
+    s->prescale[1] = qemu_get_be32(f);
+    s->match_prescale[0] = qemu_get_be32(f);
+    s->match_prescale[1] = qemu_get_be32(f);
+    s->rtc = qemu_get_be32(f);
+    s->tick[0] = qemu_get_be64(f);
+    s->tick[1] = qemu_get_be64(f);
+    qemu_get_timer(f, s->timer[0]);
+    qemu_get_timer(f, s->timer[1]);
+
+    return 0;
+}
+
 static void stellaris_gptm_init(uint32_t base, qemu_irq irq, qemu_irq trigger)
 {
     int iomemtype;
     gptm_state *s;
 
     s = (gptm_state *)qemu_mallocz(sizeof(gptm_state));
-    s->base = base;
     s->irq = irq;
     s->trigger = trigger;
     s->opaque[0] = s->opaque[1] = s;
@@ -292,14 +355,13 @@ static void stellaris_gptm_init(uint32_t base, qemu_irq irq, qemu_irq trigger)
     cpu_register_physical_memory(base, 0x00001000, iomemtype);
     s->timer[0] = qemu_new_timer(vm_clock, gptm_tick, &s->opaque[0]);
     s->timer[1] = qemu_new_timer(vm_clock, gptm_tick, &s->opaque[1]);
-    /* ??? Save/restore.  */
+    register_savevm("stellaris_gptm", -1, 1, gptm_save, gptm_load, s);
 }
 
 
 /* System controller.  */
 
 typedef struct {
-    uint32_t base;
     uint32_t pborctl;
     uint32_t ldopctl;
     uint32_t int_status;
@@ -311,6 +373,8 @@ typedef struct {
     uint32_t dcgc[3];
     uint32_t clkvclr;
     uint32_t ldoarst;
+    uint32_t user0;
+    uint32_t user1;
     qemu_irq irq;
     stellaris_board_info *board;
 } ssys_state;
@@ -362,7 +426,6 @@ static uint32_t ssys_read(void *opaque, target_phys_addr_t offset)
 {
     ssys_state *s = (ssys_state *)opaque;
 
-    offset -= s->base;
     switch (offset) {
     case 0x000: /* DID0 */
         return s->board->did0;
@@ -430,17 +493,25 @@ static uint32_t ssys_read(void *opaque, target_phys_addr_t offset)
         return s->clkvclr;
     case 0x160: /* LDOARST */
         return s->ldoarst;
+    case 0x1e0: /* USER0 */
+        return s->user0;
+    case 0x1e4: /* USER1 */
+        return s->user1;
     default:
-        cpu_abort(cpu_single_env, "gptm_read: Bad offset 0x%x\n", (int)offset);
+        hw_error("ssys_read: Bad offset 0x%x\n", (int)offset);
         return 0;
     }
 }
 
+static void ssys_calculate_system_clock(ssys_state *s)
+{
+    system_clock_scale = 5 * (((s->rcc >> 23) & 0xf) + 1);
+}
+
 static void ssys_write(void *opaque, target_phys_addr_t offset, uint32_t value)
 {
     ssys_state *s = (ssys_state *)opaque;
 
-    offset -= s->base;
     switch (offset) {
     case 0x030: /* PBORCTL */
         s->pborctl = value & 0xffff;
@@ -468,7 +539,7 @@ static void ssys_write(void *opaque, target_phys_addr_t offset, uint32_t value)
             s->int_status |= (1 << 6);
         }
         s->rcc = value;
-        stellaris_clock_scale = 5 * (((s->rcc >> 23) & 0xf) + 1);
+        ssys_calculate_system_clock(s);
         break;
     case 0x100: /* RCGC0 */
         s->rcgc[0] = value;
@@ -504,7 +575,7 @@ static void ssys_write(void *opaque, target_phys_addr_t offset, uint32_t value)
         s->ldoarst = value;
         break;
     default:
-        cpu_abort(cpu_single_env, "gptm_write: Bad offset 0x%x\n", (int)offset);
+        hw_error("ssys_write: Bad offset 0x%x\n", (int)offset);
     }
     ssys_update(s);
 }
@@ -521,7 +592,7 @@ static CPUWriteMemoryFunc *ssys_writefn[] = {
    ssys_write
 };
 
-void ssys_reset(void *opaque)
+static void ssys_reset(void *opaque)
 {
     ssys_state *s = (ssys_state *)opaque;
 
@@ -532,31 +603,86 @@ void ssys_reset(void *opaque)
     s->dcgc[0] = 1;
 }
 
+static void ssys_save(QEMUFile *f, void *opaque)
+{
+    ssys_state *s = (ssys_state *)opaque;
+
+    qemu_put_be32(f, s->pborctl);
+    qemu_put_be32(f, s->ldopctl);
+    qemu_put_be32(f, s->int_mask);
+    qemu_put_be32(f, s->int_status);
+    qemu_put_be32(f, s->resc);
+    qemu_put_be32(f, s->rcc);
+    qemu_put_be32(f, s->rcgc[0]);
+    qemu_put_be32(f, s->rcgc[1]);
+    qemu_put_be32(f, s->rcgc[2]);
+    qemu_put_be32(f, s->scgc[0]);
+    qemu_put_be32(f, s->scgc[1]);
+    qemu_put_be32(f, s->scgc[2]);
+    qemu_put_be32(f, s->dcgc[0]);
+    qemu_put_be32(f, s->dcgc[1]);
+    qemu_put_be32(f, s->dcgc[2]);
+    qemu_put_be32(f, s->clkvclr);
+    qemu_put_be32(f, s->ldoarst);
+}
+
+static int ssys_load(QEMUFile *f, void *opaque, int version_id)
+{
+    ssys_state *s = (ssys_state *)opaque;
+
+    if (version_id != 1)
+        return -EINVAL;
+
+    s->pborctl = qemu_get_be32(f);
+    s->ldopctl = qemu_get_be32(f);
+    s->int_mask = qemu_get_be32(f);
+    s->int_status = qemu_get_be32(f);
+    s->resc = qemu_get_be32(f);
+    s->rcc = qemu_get_be32(f);
+    s->rcgc[0] = qemu_get_be32(f);
+    s->rcgc[1] = qemu_get_be32(f);
+    s->rcgc[2] = qemu_get_be32(f);
+    s->scgc[0] = qemu_get_be32(f);
+    s->scgc[1] = qemu_get_be32(f);
+    s->scgc[2] = qemu_get_be32(f);
+    s->dcgc[0] = qemu_get_be32(f);
+    s->dcgc[1] = qemu_get_be32(f);
+    s->dcgc[2] = qemu_get_be32(f);
+    s->clkvclr = qemu_get_be32(f);
+    s->ldoarst = qemu_get_be32(f);
+    ssys_calculate_system_clock(s);
+
+    return 0;
+}
+
 static void stellaris_sys_init(uint32_t base, qemu_irq irq,
-                               stellaris_board_info * board)
+                               stellaris_board_info * board,
+                               uint8_t *macaddr)
 {
     int iomemtype;
     ssys_state *s;
 
     s = (ssys_state *)qemu_mallocz(sizeof(ssys_state));
-    s->base = base;
     s->irq = irq;
     s->board = board;
+    /* Most devices come preprogrammed with a MAC address in the user data. */
+    s->user0 = macaddr[0] | (macaddr[1] << 8) | (macaddr[2] << 16);
+    s->user1 = macaddr[3] | (macaddr[4] << 8) | (macaddr[5] << 16);
 
     iomemtype = cpu_register_io_memory(0, ssys_readfn,
                                        ssys_writefn, s);
     cpu_register_physical_memory(base, 0x00001000, iomemtype);
     ssys_reset(s);
-    /* ??? Save/restore.  */
+    register_savevm("stellaris_sys", -1, 1, ssys_save, ssys_load, s);
 }
 
 
 /* I2C controller.  */
 
 typedef struct {
+    SysBusDevice busdev;
     i2c_bus *bus;
     qemu_irq irq;
-    uint32_t base;
     uint32_t msa;
     uint32_t mcs;
     uint32_t mdr;
@@ -578,7 +704,6 @@ static uint32_t stellaris_i2c_read(void *opaque, target_phys_addr_t offset)
 {
     stellaris_i2c_state *s = (stellaris_i2c_state *)opaque;
 
-    offset -= s->base;
     switch (offset) {
     case 0x00: /* MSA */
         return s->msa;
@@ -598,8 +723,7 @@ static uint32_t stellaris_i2c_read(void *opaque, target_phys_addr_t offset)
     case 0x20: /* MCR */
         return s->mcr;
     default:
-        cpu_abort(cpu_single_env, "strllaris_i2c_read: Bad offset 0x%x\n",
-                  (int)offset);
+        hw_error("strllaris_i2c_read: Bad offset 0x%x\n", (int)offset);
         return 0;
     }
 }
@@ -617,7 +741,6 @@ static void stellaris_i2c_write(void *opaque, target_phys_addr_t offset,
 {
     stellaris_i2c_state *s = (stellaris_i2c_state *)opaque;
 
-    offset -= s->base;
     switch (offset) {
     case 0x00: /* MSA */
         s->msa = value & 0xff;
@@ -676,15 +799,15 @@ static void stellaris_i2c_write(void *opaque, target_phys_addr_t offset,
         break;
     case 0x20: /* MCR */
         if (value & 1)
-            cpu_abort(cpu_single_env,
+            hw_error(
                       "stellaris_i2c_write: Loopback not implemented\n");
         if (value & 0x20)
-            cpu_abort(cpu_single_env,
+            hw_error(
                       "stellaris_i2c_write: Slave mode not implemented\n");
         s->mcr = value & 0x31;
         break;
     default:
-        cpu_abort(cpu_single_env, "stellaris_i2c_write: Bad offset 0x%x\n",
+        hw_error("stellaris_i2c_write: Bad offset 0x%x\n",
                   (int)offset);
     }
     stellaris_i2c_update(s);
@@ -717,21 +840,54 @@ static CPUWriteMemoryFunc *stellaris_i2c_writefn[] = {
    stellaris_i2c_write
 };
 
-static void stellaris_i2c_init(uint32_t base, qemu_irq irq, i2c_bus *bus)
+static void stellaris_i2c_save(QEMUFile *f, void *opaque)
+{
+    stellaris_i2c_state *s = (stellaris_i2c_state *)opaque;
+
+    qemu_put_be32(f, s->msa);
+    qemu_put_be32(f, s->mcs);
+    qemu_put_be32(f, s->mdr);
+    qemu_put_be32(f, s->mtpr);
+    qemu_put_be32(f, s->mimr);
+    qemu_put_be32(f, s->mris);
+    qemu_put_be32(f, s->mcr);
+}
+
+static int stellaris_i2c_load(QEMUFile *f, void *opaque, int version_id)
+{
+    stellaris_i2c_state *s = (stellaris_i2c_state *)opaque;
+
+    if (version_id != 1)
+        return -EINVAL;
+
+    s->msa = qemu_get_be32(f);
+    s->mcs = qemu_get_be32(f);
+    s->mdr = qemu_get_be32(f);
+    s->mtpr = qemu_get_be32(f);
+    s->mimr = qemu_get_be32(f);
+    s->mris = qemu_get_be32(f);
+    s->mcr = qemu_get_be32(f);
+
+    return 0;
+}
+
+static void stellaris_i2c_init(SysBusDevice * dev)
 {
-    stellaris_i2c_state *s;
+    stellaris_i2c_state *s = FROM_SYSBUS(stellaris_i2c_state, dev);
+    i2c_bus *bus;
     int iomemtype;
 
-    s = (stellaris_i2c_state *)qemu_mallocz(sizeof(stellaris_i2c_state));
-    s->base = base;
-    s->irq = irq;
+    sysbus_init_irq(dev, &s->irq);
+    bus = i2c_init_bus(&dev->qdev, "i2c");
     s->bus = bus;
 
     iomemtype = cpu_register_io_memory(0, stellaris_i2c_readfn,
                                        stellaris_i2c_writefn, s);
-    cpu_register_physical_memory(base, 0x00001000, iomemtype);
+    sysbus_init_mmio(dev, 0x1000, iomemtype);
     /* ??? For now we only implement the master interface.  */
     stellaris_i2c_reset(s);
+    register_savevm("stellaris_i2c", -1, 1,
+                    stellaris_i2c_save, stellaris_i2c_load, s);
 }
 
 /* Analogue to Digital Converter.  This is only partially implemented,
@@ -750,7 +906,6 @@ static void stellaris_i2c_init(uint32_t base, qemu_irq irq, i2c_bus *bus)
 
 typedef struct
 {
-    uint32_t base;
     uint32_t actss;
     uint32_t ris;
     uint32_t im;
@@ -765,6 +920,7 @@ typedef struct
     } fifo[4];
     uint32_t ssmux[4];
     uint32_t ssctl[4];
+    uint32_t noise;
     qemu_irq irq;
 } stellaris_adc_state;
 
@@ -813,17 +969,16 @@ static void stellaris_adc_update(stellaris_adc_state *s)
 static void stellaris_adc_trigger(void *opaque, int irq, int level)
 {
     stellaris_adc_state *s = (stellaris_adc_state *)opaque;
-    /* Some applications use the ADC as a random number source, so introduce
-       some variation into the signal.  */
-    static uint32_t noise = 0;
 
     if ((s->actss & 1) == 0) {
         return;
     }
 
-    noise = noise * 314159 + 1;
+    /* Some applications use the ADC as a random number source, so introduce
+       some variation into the signal.  */
+    s->noise = s->noise * 314159 + 1;
     /* ??? actual inputs not implemented.  Return an arbitrary value.  */
-    stellaris_adc_fifo_write(s, 0, 0x200 + ((noise >> 16) & 7));
+    stellaris_adc_fifo_write(s, 0, 0x200 + ((s->noise >> 16) & 7));
     s->ris |= 1;
     stellaris_adc_update(s);
 }
@@ -844,7 +999,6 @@ static uint32_t stellaris_adc_read(void *opaque, target_phys_addr_t offset)
     stellaris_adc_state *s = (stellaris_adc_state *)opaque;
 
     /* TODO: Implement this.  */
-    offset -= s->base;
     if (offset >= 0x40 && offset < 0xc0) {
         int n;
         n = (offset - 0x40) >> 5;
@@ -881,7 +1035,7 @@ static uint32_t stellaris_adc_read(void *opaque, target_phys_addr_t offset)
     case 0x30: /* SAC */
         return s->sac;
     default:
-        cpu_abort(cpu_single_env, "strllaris_adc_read: Bad offset 0x%x\n",
+        hw_error("strllaris_adc_read: Bad offset 0x%x\n",
                   (int)offset);
         return 0;
     }
@@ -893,7 +1047,6 @@ static void stellaris_adc_write(void *opaque, target_phys_addr_t offset,
     stellaris_adc_state *s = (stellaris_adc_state *)opaque;
 
     /* TODO: Implement this.  */
-    offset -= s->base;
     if (offset >= 0x40 && offset < 0xc0) {
         int n;
         n = (offset - 0x40) >> 5;
@@ -903,7 +1056,7 @@ static void stellaris_adc_write(void *opaque, target_phys_addr_t offset,
             return;
         case 0x04: /* SSCTL */
             if (value != 6) {
-                cpu_abort(cpu_single_env, "ADC: Unimplemented sequence %x\n",
+                hw_error("ADC: Unimplemented sequence %x\n",
                           value);
             }
             s->ssctl[n] = value;
@@ -916,8 +1069,7 @@ static void stellaris_adc_write(void *opaque, target_phys_addr_t offset,
     case 0x00: /* ACTSS */
         s->actss = value & 0xf;
         if (value & 0xe) {
-            cpu_abort(cpu_single_env,
-                      "Not implemented:  ADC sequencers 1-3\n");
+            hw_error("Not implemented:  ADC sequencers 1-3\n");
         }
         break;
     case 0x08: /* IM */
@@ -939,14 +1091,13 @@ static void stellaris_adc_write(void *opaque, target_phys_addr_t offset,
         s->sspri = value;
         break;
     case 0x28: /* PSSI */
-        cpu_abort(cpu_single_env, "Not implemented:  ADC sample initiate\n");
+        hw_error("Not implemented:  ADC sample initiate\n");
         break;
     case 0x30: /* SAC */
         s->sac = value;
         break;
     default:
-        cpu_abort(cpu_single_env, "stellaris_adc_write: Bad offset 0x%x\n",
-                  (int)offset);
+        hw_error("stellaris_adc_write: Bad offset 0x%x\n", (int)offset);
     }
     stellaris_adc_update(s);
 }
@@ -963,6 +1114,61 @@ static CPUWriteMemoryFunc *stellaris_adc_writefn[] = {
    stellaris_adc_write
 };
 
+static void stellaris_adc_save(QEMUFile *f, void *opaque)
+{
+    stellaris_adc_state *s = (stellaris_adc_state *)opaque;
+    int i;
+    int j;
+
+    qemu_put_be32(f, s->actss);
+    qemu_put_be32(f, s->ris);
+    qemu_put_be32(f, s->im);
+    qemu_put_be32(f, s->emux);
+    qemu_put_be32(f, s->ostat);
+    qemu_put_be32(f, s->ustat);
+    qemu_put_be32(f, s->sspri);
+    qemu_put_be32(f, s->sac);
+    for (i = 0; i < 4; i++) {
+        qemu_put_be32(f, s->fifo[i].state);
+        for (j = 0; j < 16; j++) {
+            qemu_put_be32(f, s->fifo[i].data[j]);
+        }
+        qemu_put_be32(f, s->ssmux[i]);
+        qemu_put_be32(f, s->ssctl[i]);
+    }
+    qemu_put_be32(f, s->noise);
+}
+
+static int stellaris_adc_load(QEMUFile *f, void *opaque, int version_id)
+{
+    stellaris_adc_state *s = (stellaris_adc_state *)opaque;
+    int i;
+    int j;
+
+    if (version_id != 1)
+        return -EINVAL;
+
+    s->actss = qemu_get_be32(f);
+    s->ris = qemu_get_be32(f);
+    s->im = qemu_get_be32(f);
+    s->emux = qemu_get_be32(f);
+    s->ostat = qemu_get_be32(f);
+    s->ustat = qemu_get_be32(f);
+    s->sspri = qemu_get_be32(f);
+    s->sac = qemu_get_be32(f);
+    for (i = 0; i < 4; i++) {
+        s->fifo[i].state = qemu_get_be32(f);
+        for (j = 0; j < 16; j++) {
+            s->fifo[i].data[j] = qemu_get_be32(f);
+        }
+        s->ssmux[i] = qemu_get_be32(f);
+        s->ssctl[i] = qemu_get_be32(f);
+    }
+    s->noise = qemu_get_be32(f);
+
+    return 0;
+}
+
 static qemu_irq stellaris_adc_init(uint32_t base, qemu_irq irq)
 {
     stellaris_adc_state *s;
@@ -970,7 +1176,6 @@ static qemu_irq stellaris_adc_init(uint32_t base, qemu_irq irq)
     qemu_irq *qi;
 
     s = (stellaris_adc_state *)qemu_mallocz(sizeof(stellaris_adc_state));
-    s->base = base;
     s->irq = irq;
 
     iomemtype = cpu_register_io_memory(0, stellaris_adc_readfn,
@@ -978,9 +1183,70 @@ static qemu_irq stellaris_adc_init(uint32_t base, qemu_irq irq)
     cpu_register_physical_memory(base, 0x00001000, iomemtype);
     stellaris_adc_reset(s);
     qi = qemu_allocate_irqs(stellaris_adc_trigger, s, 1);
+    register_savevm("stellaris_adc", -1, 1,
+                    stellaris_adc_save, stellaris_adc_load, s);
     return qi[0];
 }
 
+/* Some boards have both an OLED controller and SD card connected to
+   the same SSI port, with the SD card chip select connected to a
+   GPIO pin.  Technically the OLED chip select is connected to the SSI
+   Fss pin.  We do not bother emulating that as both devices should
+   never be selected simultaneously, and our OLED controller ignores stray
+   0xff commands that occur when deselecting the SD card.  */
+
+typedef struct {
+    SSISlave ssidev;
+    qemu_irq irq;
+    int current_dev;
+    SSIBus *bus[2];
+} stellaris_ssi_bus_state;
+
+static void stellaris_ssi_bus_select(void *opaque, int irq, int level)
+{
+    stellaris_ssi_bus_state *s = (stellaris_ssi_bus_state *)opaque;
+
+    s->current_dev = level;
+}
+
+static uint32_t stellaris_ssi_bus_transfer(SSISlave *dev, uint32_t val)
+{
+    stellaris_ssi_bus_state *s = FROM_SSI_SLAVE(stellaris_ssi_bus_state, dev);
+
+    return ssi_transfer(s->bus[s->current_dev], val);
+}
+
+static void stellaris_ssi_bus_save(QEMUFile *f, void *opaque)
+{
+    stellaris_ssi_bus_state *s = (stellaris_ssi_bus_state *)opaque;
+
+    qemu_put_be32(f, s->current_dev);
+}
+
+static int stellaris_ssi_bus_load(QEMUFile *f, void *opaque, int version_id)
+{
+    stellaris_ssi_bus_state *s = (stellaris_ssi_bus_state *)opaque;
+
+    if (version_id != 1)
+        return -EINVAL;
+
+    s->current_dev = qemu_get_be32(f);
+
+    return 0;
+}
+
+static void stellaris_ssi_bus_init(SSISlave *dev)
+{
+    stellaris_ssi_bus_state *s = FROM_SSI_SLAVE(stellaris_ssi_bus_state, dev);
+
+    s->bus[0] = ssi_create_bus(&dev->qdev, "ssi0");
+    s->bus[1] = ssi_create_bus(&dev->qdev, "ssi1");
+    qdev_init_gpio_in(&dev->qdev, stellaris_ssi_bus_select, 1);
+
+    register_savevm("stellaris_ssi_bus", -1, 1,
+                    stellaris_ssi_bus_save, stellaris_ssi_bus_load, s);
+}
+
 /* Board init.  */
 static stellaris_board_info stellaris_boards[] = {
   { "LM3S811EVB",
@@ -991,7 +1257,7 @@ static stellaris_board_info stellaris_boards[] = {
     0x01071013,
     0x3f0f01ff,
     0x0000001f,
-    OLED_I2C
+    BP_OLED_I2C
   },
   { "LM3S6965EVB",
     0x10010002,
@@ -1001,12 +1267,12 @@ static stellaris_board_info stellaris_boards[] = {
     0x030f5317,
     0x0f0f87ff,
     0x5000007f,
-    OLED_SSI
+    BP_OLED_SSI | BP_GAMEPAD
   }
 };
 
 static void stellaris_init(const char *kernel_filename, const char *cpu_model,
-                           DisplayState *ds, stellaris_board_info *board)
+                           stellaris_board_info *board)
 {
     static const int uart_irq[] = {5, 6, 33, 34};
     static const int timer_irq[] = {19, 21, 23, 35};
@@ -1016,8 +1282,8 @@ static void stellaris_init(const char *kernel_filename, const char *cpu_model,
     static const int gpio_irq[7] = {0, 1, 2, 3, 4, 30, 31};
 
     qemu_irq *pic;
-    qemu_irq *gpio_in[5];
-    qemu_irq *gpio_out[5];
+    qemu_irq *gpio_in[7];
+    qemu_irq *gpio_out[7];
     qemu_irq adc;
     int sram_size;
     int flash_size;
@@ -1040,7 +1306,7 @@ static void stellaris_init(const char *kernel_filename, const char *cpu_model,
         }
     }
 
-    stellaris_sys_init(0x400fe000, pic[28], board);
+    stellaris_sys_init(0x400fe000, pic[28], board, nd_table[0].macaddr);
 
     for (i = 0; i < 7; i++) {
         if (board->dc4 & (1 << i)) {
@@ -1050,58 +1316,115 @@ static void stellaris_init(const char *kernel_filename, const char *cpu_model,
     }
 
     if (board->dc2 & (1 << 12)) {
-        i2c = i2c_init_bus();
-        stellaris_i2c_init(0x40020000, pic[8], i2c);
-        if (board->oled == OLED_I2C) {
-            ssd0303_init(ds, i2c, 0x3d);
+        DeviceState *dev;
+        dev = sysbus_create_simple("stellaris-i2c", 0x40020000, pic[8]);
+        i2c = (i2c_bus *)qdev_get_child_bus(dev, "i2c");
+        if (board->peripherals & BP_OLED_I2C) {
+            i2c_create_slave(i2c, "ssd0303", 0x3d);
         }
     }
 
     for (i = 0; i < 4; i++) {
         if (board->dc2 & (1 << i)) {
-            pl011_init(0x4000c000 + i * 0x1000, pic[uart_irq[i]],
-                       serial_hds[i], PL011_LUMINARY);
+            sysbus_create_simple("pl011_luminary", 0x4000c000 + i * 0x1000,
+                                 pic[uart_irq[i]]);
         }
     }
     if (board->dc2 & (1 << 4)) {
-        if (board->oled == OLED_SSI) {
-            void * oled;
-            /* FIXME: Implement chip select for OLED/MMC.  */
-            oled = ssd0323_init(ds, &gpio_out[2][7]);
-            pl022_init(0x40008000, pic[7], ssd0323_xfer_ssi, oled);
-        } else {
-            pl022_init(0x40008000, pic[7], NULL, NULL);
+        DeviceState *dev;
+        dev = sysbus_create_simple("pl022", 0x40008000, pic[7]);
+        if (board->peripherals & BP_OLED_SSI) {
+            DeviceState *mux;
+            void *bus;
+
+            bus = qdev_get_child_bus(dev, "ssi");
+            mux = ssi_create_slave(bus, "evb6965-ssi");
+            gpio_out[GPIO_D][0] = qdev_get_gpio_in(mux, 0);
+
+            bus = qdev_get_child_bus(mux, "ssi0");
+            dev = ssi_create_slave(bus, "ssi-sd");
+
+            bus = qdev_get_child_bus(mux, "ssi1");
+            dev = ssi_create_slave(bus, "ssd0323");
+            gpio_out[GPIO_C][7] = qdev_get_gpio_in(dev, 0);
+
+            /* Make sure the select pin is high.  */
+            qemu_irq_raise(gpio_out[GPIO_D][0]);
         }
     }
+    if (board->dc4 & (1 << 28)) {
+        DeviceState *enet;
+
+        qemu_check_nic_model(&nd_table[0], "stellaris");
+
+        enet = qdev_create(NULL, "stellaris_enet");
+        qdev_set_netdev(enet, &nd_table[0]);
+        qdev_init(enet);
+        sysbus_mmio_map(sysbus_from_qdev(enet), 0, 0x40048000);
+        sysbus_connect_irq(sysbus_from_qdev(enet), 0, pic[42]);
+    }
+    if (board->peripherals & BP_GAMEPAD) {
+        qemu_irq gpad_irq[5];
+        static const int gpad_keycode[5] = { 0xc8, 0xd0, 0xcb, 0xcd, 0x1d };
+
+        gpad_irq[0] = qemu_irq_invert(gpio_in[GPIO_E][0]); /* up */
+        gpad_irq[1] = qemu_irq_invert(gpio_in[GPIO_E][1]); /* down */
+        gpad_irq[2] = qemu_irq_invert(gpio_in[GPIO_E][2]); /* left */
+        gpad_irq[3] = qemu_irq_invert(gpio_in[GPIO_E][3]); /* right */
+        gpad_irq[4] = qemu_irq_invert(gpio_in[GPIO_F][1]); /* select */
+
+        stellaris_gamepad_init(5, gpad_irq, gpad_keycode);
+    }
 }
 
 /* FIXME: Figure out how to generate these from stellaris_boards.  */
-static void lm3s811evb_init(int ram_size, int vga_ram_size,
-                     const char *boot_device, DisplayState *ds,
-                     const char **fd_filename, int snapshot,
+static void lm3s811evb_init(ram_addr_t ram_size,
+                     const char *boot_device,
                      const char *kernel_filename, const char *kernel_cmdline,
                      const char *initrd_filename, const char *cpu_model)
 {
-    stellaris_init(kernel_filename, cpu_model, ds, &stellaris_boards[0]);
+    stellaris_init(kernel_filename, cpu_model, &stellaris_boards[0]);
 }
 
-static void lm3s6965evb_init(int ram_size, int vga_ram_size,
-                     const char *boot_device, DisplayState *ds,
-                     const char **fd_filename, int snapshot,
+static void lm3s6965evb_init(ram_addr_t ram_size,
+                     const char *boot_device,
                      const char *kernel_filename, const char *kernel_cmdline,
                      const char *initrd_filename, const char *cpu_model)
 {
-    stellaris_init(kernel_filename, cpu_model, ds, &stellaris_boards[1]);
+    stellaris_init(kernel_filename, cpu_model, &stellaris_boards[1]);
 }
 
-QEMUMachine lm3s811evb_machine = {
-    "lm3s811evb",
-    "Stellaris LM3S811EVB",
-    lm3s811evb_init,
+static QEMUMachine lm3s811evb_machine = {
+    .name = "lm3s811evb",
+    .desc = "Stellaris LM3S811EVB",
+    .init = lm3s811evb_init,
 };
 
-QEMUMachine lm3s6965evb_machine = {
-    "lm3s6965evb",
-    "Stellaris LM3S6965EVB",
-    lm3s6965evb_init,
+static QEMUMachine lm3s6965evb_machine = {
+    .name = "lm3s6965evb",
+    .desc = "Stellaris LM3S6965EVB",
+    .init = lm3s6965evb_init,
+};
+
+static void stellaris_machine_init(void)
+{
+    qemu_register_machine(&lm3s811evb_machine);
+    qemu_register_machine(&lm3s6965evb_machine);
+}
+
+machine_init(stellaris_machine_init);
+
+static SSISlaveInfo stellaris_ssi_bus_info = {
+    .init = stellaris_ssi_bus_init,
+    .transfer = stellaris_ssi_bus_transfer
 };
+
+static void stellaris_register_devices(void)
+{
+    sysbus_register_dev("stellaris-i2c", sizeof(stellaris_i2c_state),
+                        stellaris_i2c_init);
+    ssi_register_slave("evb6965-ssi", sizeof(stellaris_ssi_bus_state),
+                       &stellaris_ssi_bus_info);
+}
+
+device_init(stellaris_register_devices)