Add common BusState
[qemu] / hw / pxa2xx.c
index a1c54b9..febe527 100644 (file)
@@ -7,7 +7,14 @@
  * This code is licenced under the GPL.
  */
 
-# include "vl.h"
+#include "sysbus.h"
+#include "pxa.h"
+#include "sysemu.h"
+#include "pc.h"
+#include "i2c.h"
+#include "ssi.h"
+#include "qemu-timer.h"
+#include "qemu-char.h"
 
 static struct {
     target_phys_addr_t io_base;
@@ -25,22 +32,34 @@ static struct {
     { 0, 0 }
 };
 
-static struct {
+typedef struct PXASSPDef {
     target_phys_addr_t io_base;
     int irqn;
-} pxa250_ssp[] = {
+} PXASSPDef;
+
+#if 0
+static PXASSPDef pxa250_ssp[] = {
     { 0x41000000, PXA2XX_PIC_SSP },
     { 0, 0 }
-}, pxa255_ssp[] = {
+};
+#endif
+
+static PXASSPDef pxa255_ssp[] = {
     { 0x41000000, PXA2XX_PIC_SSP },
     { 0x41400000, PXA25X_PIC_NSSP },
     { 0, 0 }
-}, pxa26x_ssp[] = {
+};
+
+#if 0
+static PXASSPDef pxa26x_ssp[] = {
     { 0x41000000, PXA2XX_PIC_SSP },
     { 0x41400000, PXA25X_PIC_NSSP },
     { 0x41500000, PXA26X_PIC_ASSP },
     { 0, 0 }
-}, pxa27x_ssp[] = {
+};
+#endif
+
+static PXASSPDef pxa27x_ssp[] = {
     { 0x41000000, PXA2XX_PIC_SSP },
     { 0x41700000, PXA27X_PIC_SSP2 },
     { 0x41900000, PXA2XX_PIC_SSP3 },
@@ -71,8 +90,7 @@ static struct {
 
 static uint32_t pxa2xx_pm_read(void *opaque, target_phys_addr_t addr)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
-    addr -= s->pm_base;
+    PXA2xxState *s = (PXA2xxState *) opaque;
 
     switch (addr) {
     case PMCR ... PCMD31:
@@ -91,8 +109,7 @@ static uint32_t pxa2xx_pm_read(void *opaque, target_phys_addr_t addr)
 static void pxa2xx_pm_write(void *opaque, target_phys_addr_t addr,
                 uint32_t value)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
-    addr -= s->pm_base;
+    PXA2xxState *s = (PXA2xxState *) opaque;
 
     switch (addr) {
     case PMCR:
@@ -131,7 +148,7 @@ static CPUWriteMemoryFunc *pxa2xx_pm_writefn[] = {
 
 static void pxa2xx_pm_save(QEMUFile *f, void *opaque)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
+    PXA2xxState *s = (PXA2xxState *) opaque;
     int i;
 
     for (i = 0; i < 0x40; i ++)
@@ -140,7 +157,7 @@ static void pxa2xx_pm_save(QEMUFile *f, void *opaque)
 
 static int pxa2xx_pm_load(QEMUFile *f, void *opaque, int version_id)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
+    PXA2xxState *s = (PXA2xxState *) opaque;
     int i;
 
     for (i = 0; i < 0x40; i ++)
@@ -156,8 +173,7 @@ static int pxa2xx_pm_load(QEMUFile *f, void *opaque, int version_id)
 
 static uint32_t pxa2xx_cm_read(void *opaque, target_phys_addr_t addr)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
-    addr -= s->cm_base;
+    PXA2xxState *s = (PXA2xxState *) opaque;
 
     switch (addr) {
     case CCCR:
@@ -178,8 +194,7 @@ static uint32_t pxa2xx_cm_read(void *opaque, target_phys_addr_t addr)
 static void pxa2xx_cm_write(void *opaque, target_phys_addr_t addr,
                 uint32_t value)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
-    addr -= s->cm_base;
+    PXA2xxState *s = (PXA2xxState *) opaque;
 
     switch (addr) {
     case CCCR:
@@ -214,7 +229,7 @@ static CPUWriteMemoryFunc *pxa2xx_cm_writefn[] = {
 
 static void pxa2xx_cm_save(QEMUFile *f, void *opaque)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
+    PXA2xxState *s = (PXA2xxState *) opaque;
     int i;
 
     for (i = 0; i < 4; i ++)
@@ -225,7 +240,7 @@ static void pxa2xx_cm_save(QEMUFile *f, void *opaque)
 
 static int pxa2xx_cm_load(QEMUFile *f, void *opaque, int version_id)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
+    PXA2xxState *s = (PXA2xxState *) opaque;
     int i;
 
     for (i = 0; i < 4; i ++)
@@ -238,7 +253,7 @@ static int pxa2xx_cm_load(QEMUFile *f, void *opaque, int version_id)
 
 static uint32_t pxa2xx_clkpwr_read(void *opaque, int op2, int reg, int crm)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
+    PXA2xxState *s = (PXA2xxState *) opaque;
 
     switch (reg) {
     case 6:    /* Clock Configuration register */
@@ -257,7 +272,7 @@ static uint32_t pxa2xx_clkpwr_read(void *opaque, int op2, int reg, int crm)
 static void pxa2xx_clkpwr_write(void *opaque, int op2, int reg, int crm,
                 uint32_t value)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
+    PXA2xxState *s = (PXA2xxState *) opaque;
     static const char *pwrmode[8] = {
         "Normal", "Idle", "Deep-idle", "Standby",
         "Sleep", "reserved (!)", "reserved (!)", "Deep-sleep",
@@ -297,7 +312,7 @@ static void pxa2xx_clkpwr_write(void *opaque, int op2, int reg, int crm,
                     ARM_CPU_MODE_SVC | CPSR_A | CPSR_F | CPSR_I;
             s->env->cp15.c1_sys = 0;
             s->env->cp15.c1_coproc = 0;
-            s->env->cp15.c2_base = 0;
+            s->env->cp15.c2_base0 = 0;
             s->env->cp15.c3 = 0;
             s->pm_regs[PSSR >> 2] |= 0x8;      /* Set STS */
             s->pm_regs[RCSR >> 2] |= 0x8;      /* Set GPR */
@@ -350,7 +365,7 @@ static void pxa2xx_clkpwr_write(void *opaque, int op2, int reg, int crm,
 
 static uint32_t pxa2xx_perf_read(void *opaque, int op2, int reg, int crm)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
+    PXA2xxState *s = (PXA2xxState *) opaque;
 
     switch (reg) {
     case CPPMNC:
@@ -375,7 +390,7 @@ static uint32_t pxa2xx_perf_read(void *opaque, int op2, int reg, int crm)
 static void pxa2xx_perf_write(void *opaque, int op2, int reg, int crm,
                 uint32_t value)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
+    PXA2xxState *s = (PXA2xxState *) opaque;
 
     switch (reg) {
     case CPPMNC:
@@ -468,8 +483,7 @@ static void pxa2xx_cp14_write(void *opaque, int op2, int reg, int crm,
 
 static uint32_t pxa2xx_mm_read(void *opaque, target_phys_addr_t addr)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
-    addr -= s->mm_base;
+    PXA2xxState *s = (PXA2xxState *) opaque;
 
     switch (addr) {
     case MDCNFG ... SA1110:
@@ -486,8 +500,7 @@ static uint32_t pxa2xx_mm_read(void *opaque, target_phys_addr_t addr)
 static void pxa2xx_mm_write(void *opaque, target_phys_addr_t addr,
                 uint32_t value)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
-    addr -= s->mm_base;
+    PXA2xxState *s = (PXA2xxState *) opaque;
 
     switch (addr) {
     case MDCNFG ... SA1110:
@@ -516,7 +529,7 @@ static CPUWriteMemoryFunc *pxa2xx_mm_writefn[] = {
 
 static void pxa2xx_mm_save(QEMUFile *f, void *opaque)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
+    PXA2xxState *s = (PXA2xxState *) opaque;
     int i;
 
     for (i = 0; i < 0x1a; i ++)
@@ -525,7 +538,7 @@ static void pxa2xx_mm_save(QEMUFile *f, void *opaque)
 
 static int pxa2xx_mm_load(QEMUFile *f, void *opaque, int version_id)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
+    PXA2xxState *s = (PXA2xxState *) opaque;
     int i;
 
     for (i = 0; i < 0x1a; i ++)
@@ -535,10 +548,11 @@ static int pxa2xx_mm_load(QEMUFile *f, void *opaque, int version_id)
 }
 
 /* Synchronous Serial Ports */
-struct pxa2xx_ssp_s {
-    target_phys_addr_t base;
+typedef struct {
+    SysBusDevice busdev;
     qemu_irq irq;
     int enable;
+    SSIBus *bus;
 
     uint32_t sscr[2];
     uint32_t sspsp;
@@ -552,11 +566,7 @@ struct pxa2xx_ssp_s {
     uint32_t rx_fifo[16];
     int rx_level;
     int rx_start;
-
-    uint32_t (*readfn)(void *opaque);
-    void (*writefn)(void *opaque, uint32_t value);
-    void *opaque;
-};
+} PXA2xxSSPState;
 
 #define SSCR0  0x00    /* SSP Control register 0 */
 #define SSCR1  0x04    /* SSP Control register 1 */
@@ -605,7 +615,7 @@ struct pxa2xx_ssp_s {
 #define SSSR_BCE       (1 << 23)
 #define SSSR_RW                0x00bc0080
 
-static void pxa2xx_ssp_int_update(struct pxa2xx_ssp_s *s)
+static void pxa2xx_ssp_int_update(PXA2xxSSPState *s)
 {
     int level = 0;
 
@@ -621,7 +631,7 @@ static void pxa2xx_ssp_int_update(struct pxa2xx_ssp_s *s)
     qemu_set_irq(s->irq, !!level);
 }
 
-static void pxa2xx_ssp_fifo_update(struct pxa2xx_ssp_s *s)
+static void pxa2xx_ssp_fifo_update(PXA2xxSSPState *s)
 {
     s->sssr &= ~(0xf << 12);   /* Clear RFL */
     s->sssr &= ~(0xf << 8);    /* Clear TFL */
@@ -648,9 +658,8 @@ static void pxa2xx_ssp_fifo_update(struct pxa2xx_ssp_s *s)
 
 static uint32_t pxa2xx_ssp_read(void *opaque, target_phys_addr_t addr)
 {
-    struct pxa2xx_ssp_s *s = (struct pxa2xx_ssp_s *) opaque;
+    PXA2xxSSPState *s = (PXA2xxSSPState *) opaque;
     uint32_t retval;
-    addr -= s->base;
 
     switch (addr) {
     case SSCR0:
@@ -695,8 +704,7 @@ static uint32_t pxa2xx_ssp_read(void *opaque, target_phys_addr_t addr)
 static void pxa2xx_ssp_write(void *opaque, target_phys_addr_t addr,
                 uint32_t value)
 {
-    struct pxa2xx_ssp_s *s = (struct pxa2xx_ssp_s *) opaque;
-    addr -= s->base;
+    PXA2xxSSPState *s = (PXA2xxSSPState *) opaque;
 
     switch (addr) {
     case SSCR0:
@@ -754,17 +762,13 @@ static void pxa2xx_ssp_write(void *opaque, target_phys_addr_t addr,
          * there directly to the slave, no need to buffer it.
          */
         if (s->enable) {
-            if (s->writefn)
-                s->writefn(s->opaque, value);
-
+            uint32_t readval;
+            readval = ssi_transfer(s->bus, value);
             if (s->rx_level < 0x10) {
-                if (s->readfn)
-                    s->rx_fifo[(s->rx_start + s->rx_level ++) & 0xf] =
-                            s->readfn(s->opaque);
-                else
-                    s->rx_fifo[(s->rx_start + s->rx_level ++) & 0xf] = 0x0;
-            } else
+                s->rx_fifo[(s->rx_start + s->rx_level ++) & 0xf] = readval;
+            } else {
                 s->sssr |= SSSR_ROR;
+            }
         }
         pxa2xx_ssp_fifo_update(s);
         break;
@@ -787,20 +791,6 @@ static void pxa2xx_ssp_write(void *opaque, target_phys_addr_t addr,
     }
 }
 
-void pxa2xx_ssp_attach(struct pxa2xx_ssp_s *port,
-                uint32_t (*readfn)(void *opaque),
-                void (*writefn)(void *opaque, uint32_t value), void *opaque)
-{
-    if (!port) {
-        printf("%s: no such SSP\n", __FUNCTION__);
-        exit(-1);
-    }
-
-    port->opaque = opaque;
-    port->readfn = readfn;
-    port->writefn = writefn;
-}
-
 static CPUReadMemoryFunc *pxa2xx_ssp_readfn[] = {
     pxa2xx_ssp_read,
     pxa2xx_ssp_read,
@@ -815,7 +805,7 @@ static CPUWriteMemoryFunc *pxa2xx_ssp_writefn[] = {
 
 static void pxa2xx_ssp_save(QEMUFile *f, void *opaque)
 {
-    struct pxa2xx_ssp_s *s = (struct pxa2xx_ssp_s *) opaque;
+    PXA2xxSSPState *s = (PXA2xxSSPState *) opaque;
     int i;
 
     qemu_put_be32(f, s->enable);
@@ -837,7 +827,7 @@ static void pxa2xx_ssp_save(QEMUFile *f, void *opaque)
 
 static int pxa2xx_ssp_load(QEMUFile *f, void *opaque, int version_id)
 {
-    struct pxa2xx_ssp_s *s = (struct pxa2xx_ssp_s *) opaque;
+    PXA2xxSSPState *s = (PXA2xxSSPState *) opaque;
     int i;
 
     s->enable = qemu_get_be32(f);
@@ -860,6 +850,22 @@ static int pxa2xx_ssp_load(QEMUFile *f, void *opaque, int version_id)
     return 0;
 }
 
+static void pxa2xx_ssp_init(SysBusDevice *dev)
+{
+    int iomemtype;
+    PXA2xxSSPState *s = FROM_SYSBUS(PXA2xxSSPState, dev);
+
+    sysbus_init_irq(dev, &s->irq);
+
+    iomemtype = cpu_register_io_memory(0, pxa2xx_ssp_readfn,
+                                       pxa2xx_ssp_writefn, s);
+    sysbus_init_mmio(dev, 0x1000, iomemtype);
+    register_savevm("pxa2xx_ssp", -1, 0,
+                    pxa2xx_ssp_save, pxa2xx_ssp_load, s);
+
+    s->bus = ssi_create_bus(&dev->qdev, "ssi");
+}
+
 /* Real-Time Clock */
 #define RCNR           0x00    /* RTC Counter register */
 #define RTAR           0x04    /* RTC Alarm register */
@@ -877,12 +883,12 @@ static int pxa2xx_ssp_load(QEMUFile *f, void *opaque, int version_id)
 #define RTCPICR                0x34    /* RTC Periodic Interrupt Counter register */
 #define PIAR           0x38    /* RTC Periodic Interrupt Alarm register */
 
-static inline void pxa2xx_rtc_int_update(struct pxa2xx_state_s *s)
+static inline void pxa2xx_rtc_int_update(PXA2xxState *s)
 {
     qemu_set_irq(s->pic[PXA2XX_PIC_RTCALARM], !!(s->rtsr & 0x2553));
 }
 
-static void pxa2xx_rtc_hzupdate(struct pxa2xx_state_s *s)
+static void pxa2xx_rtc_hzupdate(PXA2xxState *s)
 {
     int64_t rt = qemu_get_clock(rt_clock);
     s->last_rcnr += ((rt - s->last_hz) << 15) /
@@ -892,7 +898,7 @@ static void pxa2xx_rtc_hzupdate(struct pxa2xx_state_s *s)
     s->last_hz = rt;
 }
 
-static void pxa2xx_rtc_swupdate(struct pxa2xx_state_s *s)
+static void pxa2xx_rtc_swupdate(PXA2xxState *s)
 {
     int64_t rt = qemu_get_clock(rt_clock);
     if (s->rtsr & (1 << 12))
@@ -900,7 +906,7 @@ static void pxa2xx_rtc_swupdate(struct pxa2xx_state_s *s)
     s->last_sw = rt;
 }
 
-static void pxa2xx_rtc_piupdate(struct pxa2xx_state_s *s)
+static void pxa2xx_rtc_piupdate(PXA2xxState *s)
 {
     int64_t rt = qemu_get_clock(rt_clock);
     if (s->rtsr & (1 << 15))
@@ -908,7 +914,7 @@ static void pxa2xx_rtc_piupdate(struct pxa2xx_state_s *s)
     s->last_pi = rt;
 }
 
-static inline void pxa2xx_rtc_alarm_update(struct pxa2xx_state_s *s,
+static inline void pxa2xx_rtc_alarm_update(PXA2xxState *s,
                 uint32_t rtsr)
 {
     if ((rtsr & (1 << 2)) && !(rtsr & (1 << 0)))
@@ -953,7 +959,7 @@ static inline void pxa2xx_rtc_alarm_update(struct pxa2xx_state_s *s,
 
 static inline void pxa2xx_rtc_hz_tick(void *opaque)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
+    PXA2xxState *s = (PXA2xxState *) opaque;
     s->rtsr |= (1 << 0);
     pxa2xx_rtc_alarm_update(s, s->rtsr);
     pxa2xx_rtc_int_update(s);
@@ -961,7 +967,7 @@ static inline void pxa2xx_rtc_hz_tick(void *opaque)
 
 static inline void pxa2xx_rtc_rdal1_tick(void *opaque)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
+    PXA2xxState *s = (PXA2xxState *) opaque;
     s->rtsr |= (1 << 4);
     pxa2xx_rtc_alarm_update(s, s->rtsr);
     pxa2xx_rtc_int_update(s);
@@ -969,7 +975,7 @@ static inline void pxa2xx_rtc_rdal1_tick(void *opaque)
 
 static inline void pxa2xx_rtc_rdal2_tick(void *opaque)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
+    PXA2xxState *s = (PXA2xxState *) opaque;
     s->rtsr |= (1 << 6);
     pxa2xx_rtc_alarm_update(s, s->rtsr);
     pxa2xx_rtc_int_update(s);
@@ -977,7 +983,7 @@ static inline void pxa2xx_rtc_rdal2_tick(void *opaque)
 
 static inline void pxa2xx_rtc_swal1_tick(void *opaque)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
+    PXA2xxState *s = (PXA2xxState *) opaque;
     s->rtsr |= (1 << 8);
     pxa2xx_rtc_alarm_update(s, s->rtsr);
     pxa2xx_rtc_int_update(s);
@@ -985,7 +991,7 @@ static inline void pxa2xx_rtc_swal1_tick(void *opaque)
 
 static inline void pxa2xx_rtc_swal2_tick(void *opaque)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
+    PXA2xxState *s = (PXA2xxState *) opaque;
     s->rtsr |= (1 << 10);
     pxa2xx_rtc_alarm_update(s, s->rtsr);
     pxa2xx_rtc_int_update(s);
@@ -993,7 +999,7 @@ static inline void pxa2xx_rtc_swal2_tick(void *opaque)
 
 static inline void pxa2xx_rtc_pi_tick(void *opaque)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
+    PXA2xxState *s = (PXA2xxState *) opaque;
     s->rtsr |= (1 << 13);
     pxa2xx_rtc_piupdate(s);
     s->last_rtcpicr = 0;
@@ -1003,8 +1009,7 @@ static inline void pxa2xx_rtc_pi_tick(void *opaque)
 
 static uint32_t pxa2xx_rtc_read(void *opaque, target_phys_addr_t addr)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
-    addr -= s->rtc_base;
+    PXA2xxState *s = (PXA2xxState *) opaque;
 
     switch (addr) {
     case RTTR:
@@ -1050,8 +1055,7 @@ static uint32_t pxa2xx_rtc_read(void *opaque, target_phys_addr_t addr)
 static void pxa2xx_rtc_write(void *opaque, target_phys_addr_t addr,
                 uint32_t value)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
-    addr -= s->rtc_base;
+    PXA2xxState *s = (PXA2xxState *) opaque;
 
     switch (addr) {
     case RTTR:
@@ -1163,29 +1167,24 @@ static CPUWriteMemoryFunc *pxa2xx_rtc_writefn[] = {
     pxa2xx_rtc_write,
 };
 
-static void pxa2xx_rtc_init(struct pxa2xx_state_s *s)
+static void pxa2xx_rtc_init(PXA2xxState *s)
 {
-    struct tm *tm;
-    time_t ti;
+    struct tm tm;
     int wom;
 
     s->rttr = 0x7fff;
     s->rtsr = 0;
 
-    time(&ti);
-    if (rtc_utc)
-        tm = gmtime(&ti);
-    else
-        tm = localtime(&ti);
-    wom = ((tm->tm_mday - 1) / 7) + 1;
-
-    s->last_rcnr = (uint32_t) ti;
-    s->last_rdcr = (wom << 20) | ((tm->tm_wday + 1) << 17) |
-            (tm->tm_hour << 12) | (tm->tm_min << 6) | tm->tm_sec;
-    s->last_rycr = ((tm->tm_year + 1900) << 9) |
-            ((tm->tm_mon + 1) << 5) | tm->tm_mday;
-    s->last_swcr = (tm->tm_hour << 19) |
-            (tm->tm_min << 13) | (tm->tm_sec << 7);
+    qemu_get_timedate(&tm, 0);
+    wom = ((tm.tm_mday - 1) / 7) + 1;
+
+    s->last_rcnr = (uint32_t) mktimegm(&tm);
+    s->last_rdcr = (wom << 20) | ((tm.tm_wday + 1) << 17) |
+            (tm.tm_hour << 12) | (tm.tm_min << 6) | tm.tm_sec;
+    s->last_rycr = ((tm.tm_year + 1900) << 9) |
+            ((tm.tm_mon + 1) << 5) | tm.tm_mday;
+    s->last_swcr = (tm.tm_hour << 19) |
+            (tm.tm_min << 13) | (tm.tm_sec << 7);
     s->last_rtcpicr = 0;
     s->last_hz = s->last_sw = s->last_pi = qemu_get_clock(rt_clock);
 
@@ -1199,7 +1198,7 @@ static void pxa2xx_rtc_init(struct pxa2xx_state_s *s)
 
 static void pxa2xx_rtc_save(QEMUFile *f, void *opaque)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
+    PXA2xxState *s = (PXA2xxState *) opaque;
 
     pxa2xx_rtc_hzupdate(s);
     pxa2xx_rtc_piupdate(s);
@@ -1220,14 +1219,14 @@ static void pxa2xx_rtc_save(QEMUFile *f, void *opaque)
     qemu_put_be32s(f, &s->last_rycr);
     qemu_put_be32s(f, &s->last_swcr);
     qemu_put_be32s(f, &s->last_rtcpicr);
-    qemu_put_be64s(f, &s->last_hz);
-    qemu_put_be64s(f, &s->last_sw);
-    qemu_put_be64s(f, &s->last_pi);
+    qemu_put_sbe64s(f, &s->last_hz);
+    qemu_put_sbe64s(f, &s->last_sw);
+    qemu_put_sbe64s(f, &s->last_pi);
 }
 
 static int pxa2xx_rtc_load(QEMUFile *f, void *opaque, int version_id)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
+    PXA2xxState *s = (PXA2xxState *) opaque;
 
     qemu_get_be32s(f, &s->rttr);
     qemu_get_be32s(f, &s->rtsr);
@@ -1244,9 +1243,9 @@ static int pxa2xx_rtc_load(QEMUFile *f, void *opaque, int version_id)
     qemu_get_be32s(f, &s->last_rycr);
     qemu_get_be32s(f, &s->last_swcr);
     qemu_get_be32s(f, &s->last_rtcpicr);
-    qemu_get_be64s(f, &s->last_hz);
-    qemu_get_be64s(f, &s->last_sw);
-    qemu_get_be64s(f, &s->last_pi);
+    qemu_get_sbe64s(f, &s->last_hz);
+    qemu_get_sbe64s(f, &s->last_sw);
+    qemu_get_sbe64s(f, &s->last_pi);
 
     pxa2xx_rtc_alarm_update(s, s->rtsr);
 
@@ -1254,11 +1253,16 @@ static int pxa2xx_rtc_load(QEMUFile *f, void *opaque, int version_id)
 }
 
 /* I2C Interface */
-struct pxa2xx_i2c_s {
-    i2c_slave slave;
+typedef struct {
+    i2c_slave i2c;
+    PXA2xxI2CState *host;
+} PXA2xxI2CSlaveState;
+
+struct PXA2xxI2CState {
+    PXA2xxI2CSlaveState *slave;
     i2c_bus *bus;
-    target_phys_addr_t base;
     qemu_irq irq;
+    target_phys_addr_t offset;
 
     uint16_t control;
     uint16_t status;
@@ -1272,7 +1276,7 @@ struct pxa2xx_i2c_s {
 #define ISR    0x98    /* I2C Status register */
 #define ISAR   0xa0    /* I2C Slave Address register */
 
-static void pxa2xx_i2c_update(struct pxa2xx_i2c_s *s)
+static void pxa2xx_i2c_update(PXA2xxI2CState *s)
 {
     uint16_t level = 0;
     level |= s->status & s->control & (1 << 10);               /* BED */
@@ -1285,7 +1289,8 @@ static void pxa2xx_i2c_update(struct pxa2xx_i2c_s *s)
 /* These are only stubs now.  */
 static void pxa2xx_i2c_event(i2c_slave *i2c, enum i2c_event event)
 {
-    struct pxa2xx_i2c_s *s = (struct pxa2xx_i2c_s *) i2c;
+    PXA2xxI2CSlaveState *slave = FROM_I2C_SLAVE(PXA2xxI2CSlaveState, i2c);
+    PXA2xxI2CState *s = slave->host;
 
     switch (event) {
     case I2C_START_SEND:
@@ -1308,7 +1313,8 @@ static void pxa2xx_i2c_event(i2c_slave *i2c, enum i2c_event event)
 
 static int pxa2xx_i2c_rx(i2c_slave *i2c)
 {
-    struct pxa2xx_i2c_s *s = (struct pxa2xx_i2c_s *) i2c;
+    PXA2xxI2CSlaveState *slave = FROM_I2C_SLAVE(PXA2xxI2CSlaveState, i2c);
+    PXA2xxI2CState *s = slave->host;
     if ((s->control & (1 << 14)) || !(s->control & (1 << 6)))
         return 0;
 
@@ -1322,7 +1328,8 @@ static int pxa2xx_i2c_rx(i2c_slave *i2c)
 
 static int pxa2xx_i2c_tx(i2c_slave *i2c, uint8_t data)
 {
-    struct pxa2xx_i2c_s *s = (struct pxa2xx_i2c_s *) i2c;
+    PXA2xxI2CSlaveState *slave = FROM_I2C_SLAVE(PXA2xxI2CSlaveState, i2c);
+    PXA2xxI2CState *s = slave->host;
     if ((s->control & (1 << 14)) || !(s->control & (1 << 6)))
         return 1;
 
@@ -1337,16 +1344,16 @@ static int pxa2xx_i2c_tx(i2c_slave *i2c, uint8_t data)
 
 static uint32_t pxa2xx_i2c_read(void *opaque, target_phys_addr_t addr)
 {
-    struct pxa2xx_i2c_s *s = (struct pxa2xx_i2c_s *) opaque;
-    addr -= s->base;
+    PXA2xxI2CState *s = (PXA2xxI2CState *) opaque;
 
+    addr -= s->offset;
     switch (addr) {
     case ICR:
         return s->control;
     case ISR:
         return s->status | (i2c_bus_busy(s->bus) << 2);
     case ISAR:
-        return s->slave.address;
+        return s->slave->i2c.address;
     case IDBR:
         return s->data;
     case IBMR:
@@ -1365,10 +1372,10 @@ static uint32_t pxa2xx_i2c_read(void *opaque, target_phys_addr_t addr)
 static void pxa2xx_i2c_write(void *opaque, target_phys_addr_t addr,
                 uint32_t value)
 {
-    struct pxa2xx_i2c_s *s = (struct pxa2xx_i2c_s *) opaque;
+    PXA2xxI2CState *s = (PXA2xxI2CState *) opaque;
     int ack;
-    addr -= s->base;
 
+    addr -= s->offset;
     switch (addr) {
     case ICR:
         s->control = value & 0xfff7;
@@ -1420,7 +1427,7 @@ static void pxa2xx_i2c_write(void *opaque, target_phys_addr_t addr,
         break;
 
     case ISAR:
-        i2c_set_slave_address(&s->slave, value & 0x7f);
+        i2c_set_slave_address(&s->slave->i2c, value & 0x7f);
         break;
 
     case IDBR:
@@ -1446,62 +1453,78 @@ static CPUWriteMemoryFunc *pxa2xx_i2c_writefn[] = {
 
 static void pxa2xx_i2c_save(QEMUFile *f, void *opaque)
 {
-    struct pxa2xx_i2c_s *s = (struct pxa2xx_i2c_s *) opaque;
+    PXA2xxI2CState *s = (PXA2xxI2CState *) opaque;
 
     qemu_put_be16s(f, &s->control);
     qemu_put_be16s(f, &s->status);
     qemu_put_8s(f, &s->ibmr);
     qemu_put_8s(f, &s->data);
 
-    i2c_bus_save(f, s->bus);
-    i2c_slave_save(f, &s->slave);
+    i2c_slave_save(f, &s->slave->i2c);
 }
 
 static int pxa2xx_i2c_load(QEMUFile *f, void *opaque, int version_id)
 {
-    struct pxa2xx_i2c_s *s = (struct pxa2xx_i2c_s *) opaque;
+    PXA2xxI2CState *s = (PXA2xxI2CState *) opaque;
+
+    if (version_id != 1)
+        return -EINVAL;
 
     qemu_get_be16s(f, &s->control);
     qemu_get_be16s(f, &s->status);
     qemu_get_8s(f, &s->ibmr);
     qemu_get_8s(f, &s->data);
 
-    i2c_bus_load(f, s->bus);
-    i2c_slave_load(f, &s->slave);
+    i2c_slave_load(f, &s->slave->i2c);
     return 0;
 }
 
-struct pxa2xx_i2c_s *pxa2xx_i2c_init(target_phys_addr_t base,
-                qemu_irq irq, uint32_t page_size)
+static void pxa2xx_i2c_slave_init(i2c_slave *i2c)
+{
+    /* Nothing to do.  */
+}
+
+static I2CSlaveInfo pxa2xx_i2c_slave_info = {
+    .init = pxa2xx_i2c_slave_init,
+    .event = pxa2xx_i2c_event,
+    .recv = pxa2xx_i2c_rx,
+    .send = pxa2xx_i2c_tx
+};
+
+PXA2xxI2CState *pxa2xx_i2c_init(target_phys_addr_t base,
+                qemu_irq irq, uint32_t region_size)
 {
     int iomemtype;
-    struct pxa2xx_i2c_s *s = (struct pxa2xx_i2c_s *)
-            i2c_slave_init(i2c_init_bus(), 0, sizeof(struct pxa2xx_i2c_s));
+    DeviceState *dev;
+    PXA2xxI2CState *s = qemu_mallocz(sizeof(PXA2xxI2CState));
+
+    /* FIXME: Should the slave device really be on a separate bus?  */
+    dev = i2c_create_slave(i2c_init_bus(NULL, "dummy"), "pxa2xx-i2c-slave", 0);
+    s->slave = FROM_I2C_SLAVE(PXA2xxI2CSlaveState, I2C_SLAVE_FROM_QDEV(dev));
+    s->slave->host = s;
 
-    s->base = base;
     s->irq = irq;
-    s->slave.event = pxa2xx_i2c_event;
-    s->slave.recv = pxa2xx_i2c_rx;
-    s->slave.send = pxa2xx_i2c_tx;
-    s->bus = i2c_init_bus();
+    s->bus = i2c_init_bus(NULL, "i2c");
+    s->offset = base - (base & (~region_size) & TARGET_PAGE_MASK);
 
     iomemtype = cpu_register_io_memory(0, pxa2xx_i2c_readfn,
                     pxa2xx_i2c_writefn, s);
-    cpu_register_physical_memory(s->base & ~page_size, page_size, iomemtype);
+    cpu_register_physical_memory(base & ~region_size,
+                    region_size + 1, iomemtype);
 
-    register_savevm("pxa2xx_i2c", base, 0,
+    register_savevm("pxa2xx_i2c", base, 1,
                     pxa2xx_i2c_save, pxa2xx_i2c_load, s);
 
     return s;
 }
 
-i2c_bus *pxa2xx_i2c_bus(struct pxa2xx_i2c_s *s)
+i2c_bus *pxa2xx_i2c_bus(PXA2xxI2CState *s)
 {
     return s->bus;
 }
 
 /* PXA Inter-IC Sound Controller */
-static void pxa2xx_i2s_reset(struct pxa2xx_i2s_s *i2s)
+static void pxa2xx_i2s_reset(PXA2xxI2SState *i2s)
 {
     i2s->rx_len = 0;
     i2s->tx_len = 0;
@@ -1518,7 +1541,7 @@ static void pxa2xx_i2s_reset(struct pxa2xx_i2s_s *i2s)
 #define SACR_DREC(val) (val & (1 << 3))
 #define SACR_DPRL(val) (val & (1 << 4))
 
-static inline void pxa2xx_i2s_update(struct pxa2xx_i2s_s *i2s)
+static inline void pxa2xx_i2s_update(PXA2xxI2SState *i2s)
 {
     int rfs, tfs;
     rfs = SACR_RFTH(i2s->control[0]) < i2s->rx_len &&
@@ -1530,6 +1553,8 @@ static inline void pxa2xx_i2s_update(struct pxa2xx_i2s_s *i2s)
     pxa2xx_dma_request(i2s->dma, PXA2XX_TX_RQ_I2S, tfs);
 
     i2s->status &= 0xe0;
+    if (i2s->fifo_len < 16 || !i2s->enable)
+        i2s->status |= 1 << 0;                 /* TNF */
     if (i2s->rx_len)
         i2s->status |= 1 << 1;                 /* RNE */
     if (i2s->enable)
@@ -1555,8 +1580,7 @@ static inline void pxa2xx_i2s_update(struct pxa2xx_i2s_s *i2s)
 
 static uint32_t pxa2xx_i2s_read(void *opaque, target_phys_addr_t addr)
 {
-    struct pxa2xx_i2s_s *s = (struct pxa2xx_i2s_s *) opaque;
-    addr -= s->base;
+    PXA2xxI2SState *s = (PXA2xxI2SState *) opaque;
 
     switch (addr) {
     case SACR0:
@@ -1588,9 +1612,8 @@ static uint32_t pxa2xx_i2s_read(void *opaque, target_phys_addr_t addr)
 static void pxa2xx_i2s_write(void *opaque, target_phys_addr_t addr,
                 uint32_t value)
 {
-    struct pxa2xx_i2s_s *s = (struct pxa2xx_i2s_s *) opaque;
+    PXA2xxI2SState *s = (PXA2xxI2SState *) opaque;
     uint32_t *sample;
-    addr -= s->base;
 
     switch (addr) {
     case SACR0:
@@ -1655,7 +1678,7 @@ static CPUWriteMemoryFunc *pxa2xx_i2s_writefn[] = {
 
 static void pxa2xx_i2s_save(QEMUFile *f, void *opaque)
 {
-    struct pxa2xx_i2s_s *s = (struct pxa2xx_i2s_s *) opaque;
+    PXA2xxI2SState *s = (PXA2xxI2SState *) opaque;
 
     qemu_put_be32s(f, &s->control[0]);
     qemu_put_be32s(f, &s->control[1]);
@@ -1671,7 +1694,7 @@ static void pxa2xx_i2s_save(QEMUFile *f, void *opaque)
 
 static int pxa2xx_i2s_load(QEMUFile *f, void *opaque, int version_id)
 {
-    struct pxa2xx_i2s_s *s = (struct pxa2xx_i2s_s *) opaque;
+    PXA2xxI2SState *s = (PXA2xxI2SState *) opaque;
 
     qemu_get_be32s(f, &s->control[0]);
     qemu_get_be32s(f, &s->control[1]);
@@ -1689,7 +1712,7 @@ static int pxa2xx_i2s_load(QEMUFile *f, void *opaque, int version_id)
 
 static void pxa2xx_i2s_data_req(void *opaque, int tx, int rx)
 {
-    struct pxa2xx_i2s_s *s = (struct pxa2xx_i2s_s *) opaque;
+    PXA2xxI2SState *s = (PXA2xxI2SState *) opaque;
     uint32_t *sample;
 
     /* Signal FIFO errors */
@@ -1709,14 +1732,13 @@ static void pxa2xx_i2s_data_req(void *opaque, int tx, int rx)
     pxa2xx_i2s_update(s);
 }
 
-static struct pxa2xx_i2s_s *pxa2xx_i2s_init(target_phys_addr_t base,
-                qemu_irq irq, struct pxa2xx_dma_state_s *dma)
+static PXA2xxI2SState *pxa2xx_i2s_init(target_phys_addr_t base,
+                qemu_irq irq, PXA2xxDMAState *dma)
 {
     int iomemtype;
-    struct pxa2xx_i2s_s *s = (struct pxa2xx_i2s_s *)
-            qemu_mallocz(sizeof(struct pxa2xx_i2s_s));
+    PXA2xxI2SState *s = (PXA2xxI2SState *)
+            qemu_mallocz(sizeof(PXA2xxI2SState));
 
-    s->base = base;
     s->irq = irq;
     s->dma = dma;
     s->data_req = pxa2xx_i2s_data_req;
@@ -1725,7 +1747,7 @@ static struct pxa2xx_i2s_s *pxa2xx_i2s_init(target_phys_addr_t base,
 
     iomemtype = cpu_register_io_memory(0, pxa2xx_i2s_readfn,
                     pxa2xx_i2s_writefn, s);
-    cpu_register_physical_memory(s->base & 0xfff00000, 0x100000, iomemtype);
+    cpu_register_physical_memory(base, 0x100000, iomemtype);
 
     register_savevm("pxa2xx_i2s", base, 0,
                     pxa2xx_i2s_save, pxa2xx_i2s_load, s);
@@ -1734,10 +1756,9 @@ static struct pxa2xx_i2s_s *pxa2xx_i2s_init(target_phys_addr_t base,
 }
 
 /* PXA Fast Infra-red Communications Port */
-struct pxa2xx_fir_s {
-    target_phys_addr_t base;
+struct PXA2xxFIrState {
     qemu_irq irq;
-    struct pxa2xx_dma_state_s *dma;
+    PXA2xxDMAState *dma;
     int enable;
     CharDriverState *chr;
 
@@ -1749,7 +1770,7 @@ struct pxa2xx_fir_s {
     uint8_t rx_fifo[64];
 };
 
-static void pxa2xx_fir_reset(struct pxa2xx_fir_s *s)
+static void pxa2xx_fir_reset(PXA2xxFIrState *s)
 {
     s->control[0] = 0x00;
     s->control[1] = 0x00;
@@ -1759,7 +1780,7 @@ static void pxa2xx_fir_reset(struct pxa2xx_fir_s *s)
     s->enable = 0;
 }
 
-static inline void pxa2xx_fir_update(struct pxa2xx_fir_s *s)
+static inline void pxa2xx_fir_update(PXA2xxFIrState *s)
 {
     static const int tresh[4] = { 8, 16, 32, 0 };
     int intr = 0;
@@ -1807,9 +1828,8 @@ static inline void pxa2xx_fir_update(struct pxa2xx_fir_s *s)
 
 static uint32_t pxa2xx_fir_read(void *opaque, target_phys_addr_t addr)
 {
-    struct pxa2xx_fir_s *s = (struct pxa2xx_fir_s *) opaque;
+    PXA2xxFIrState *s = (PXA2xxFIrState *) opaque;
     uint8_t ret;
-    addr -= s->base;
 
     switch (addr) {
     case ICCR0:
@@ -1846,9 +1866,8 @@ static uint32_t pxa2xx_fir_read(void *opaque, target_phys_addr_t addr)
 static void pxa2xx_fir_write(void *opaque, target_phys_addr_t addr,
                 uint32_t value)
 {
-    struct pxa2xx_fir_s *s = (struct pxa2xx_fir_s *) opaque;
+    PXA2xxFIrState *s = (PXA2xxFIrState *) opaque;
     uint8_t ch;
-    addr -= s->base;
 
     switch (addr) {
     case ICCR0:
@@ -1902,13 +1921,13 @@ static CPUWriteMemoryFunc *pxa2xx_fir_writefn[] = {
 
 static int pxa2xx_fir_is_empty(void *opaque)
 {
-    struct pxa2xx_fir_s *s = (struct pxa2xx_fir_s *) opaque;
+    PXA2xxFIrState *s = (PXA2xxFIrState *) opaque;
     return (s->rx_len < 64);
 }
 
 static void pxa2xx_fir_rx(void *opaque, const uint8_t *buf, int size)
 {
-    struct pxa2xx_fir_s *s = (struct pxa2xx_fir_s *) opaque;
+    PXA2xxFIrState *s = (PXA2xxFIrState *) opaque;
     if (!(s->control[0] & (1 << 4)))                   /* RXE */
         return;
 
@@ -1934,7 +1953,7 @@ static void pxa2xx_fir_event(void *opaque, int event)
 
 static void pxa2xx_fir_save(QEMUFile *f, void *opaque)
 {
-    struct pxa2xx_fir_s *s = (struct pxa2xx_fir_s *) opaque;
+    PXA2xxFIrState *s = (PXA2xxFIrState *) opaque;
     int i;
 
     qemu_put_be32(f, s->enable);
@@ -1952,7 +1971,7 @@ static void pxa2xx_fir_save(QEMUFile *f, void *opaque)
 
 static int pxa2xx_fir_load(QEMUFile *f, void *opaque, int version_id)
 {
-    struct pxa2xx_fir_s *s = (struct pxa2xx_fir_s *) opaque;
+    PXA2xxFIrState *s = (PXA2xxFIrState *) opaque;
     int i;
 
     s->enable = qemu_get_be32(f);
@@ -1971,15 +1990,14 @@ static int pxa2xx_fir_load(QEMUFile *f, void *opaque, int version_id)
     return 0;
 }
 
-static struct pxa2xx_fir_s *pxa2xx_fir_init(target_phys_addr_t base,
-                qemu_irq irq, struct pxa2xx_dma_state_s *dma,
+static PXA2xxFIrState *pxa2xx_fir_init(target_phys_addr_t base,
+                qemu_irq irq, PXA2xxDMAState *dma,
                 CharDriverState *chr)
 {
     int iomemtype;
-    struct pxa2xx_fir_s *s = (struct pxa2xx_fir_s *)
-            qemu_mallocz(sizeof(struct pxa2xx_fir_s));
+    PXA2xxFIrState *s = (PXA2xxFIrState *)
+            qemu_mallocz(sizeof(PXA2xxFIrState));
 
-    s->base = base;
     s->irq = irq;
     s->dma = dma;
     s->chr = chr;
@@ -1988,7 +2006,7 @@ static struct pxa2xx_fir_s *pxa2xx_fir_init(target_phys_addr_t base,
 
     iomemtype = cpu_register_io_memory(0, pxa2xx_fir_readfn,
                     pxa2xx_fir_writefn, s);
-    cpu_register_physical_memory(s->base, 0x1000, iomemtype);
+    cpu_register_physical_memory(base, 0x1000, iomemtype);
 
     if (chr)
         qemu_chr_add_handlers(chr, pxa2xx_fir_is_empty,
@@ -1999,9 +2017,10 @@ static struct pxa2xx_fir_s *pxa2xx_fir_init(target_phys_addr_t base,
     return s;
 }
 
-void pxa2xx_reset(int line, int level, void *opaque)
+static void pxa2xx_reset(void *opaque, int line, int level)
 {
-    struct pxa2xx_state_s *s = (struct pxa2xx_state_s *) opaque;
+    PXA2xxState *s = (PXA2xxState *) opaque;
+
     if (level && (s->pm_regs[PCFR >> 2] & 0x10)) {     /* GPR_EN */
         cpu_reset(s->env);
         /* TODO: reset peripherals */
@@ -2009,22 +2028,26 @@ void pxa2xx_reset(int line, int level, void *opaque)
 }
 
 /* Initialise a PXA270 integrated chip (ARM based core).  */
-struct pxa2xx_state_s *pxa270_init(unsigned int sdram_size,
-                DisplayState *ds, const char *revision)
+PXA2xxState *pxa270_init(unsigned int sdram_size, const char *revision)
 {
-    struct pxa2xx_state_s *s;
-    struct pxa2xx_ssp_s *ssp;
+    PXA2xxState *s;
     int iomemtype, i;
-    s = (struct pxa2xx_state_s *) qemu_mallocz(sizeof(struct pxa2xx_state_s));
+    int index;
+    s = (PXA2xxState *) qemu_mallocz(sizeof(PXA2xxState));
 
     if (revision && strncmp(revision, "pxa27", 5)) {
         fprintf(stderr, "Machine requires a PXA27x processor.\n");
         exit(1);
     }
-
-    s->env = cpu_init();
-    cpu_arm_set_model(s->env, revision ?: "pxa270");
-    register_savevm("cpu", 0, 0, cpu_save, cpu_load, s->env);
+    if (!revision)
+        revision = "pxa270";
+    
+    s->env = cpu_init(revision);
+    if (!s->env) {
+        fprintf(stderr, "Unable to find CPU definition\n");
+        exit(1);
+    }
+    s->reset = qemu_allocate_irqs(pxa2xx_reset, s, 1)[0];
 
     /* SDRAM & Internal Memory Storage */
     cpu_register_physical_memory(PXA2XX_SDRAM_BASE,
@@ -2041,20 +2064,26 @@ struct pxa2xx_state_s *pxa270_init(unsigned int sdram_size,
 
     s->gpio = pxa2xx_gpio_init(0x40e00000, s->env, s->pic, 121);
 
-    s->mmc = pxa2xx_mmci_init(0x41100000, s->pic[PXA2XX_PIC_MMC], s->dma);
+    index = drive_get_index(IF_SD, 0, 0);
+    if (index == -1) {
+        fprintf(stderr, "qemu: missing SecureDigital device\n");
+        exit(1);
+    }
+    s->mmc = pxa2xx_mmci_init(0x41100000, drives_table[index].bdrv,
+                              s->pic[PXA2XX_PIC_MMC], s->dma);
 
     for (i = 0; pxa270_serial[i].io_base; i ++)
         if (serial_hds[i])
             serial_mm_init(pxa270_serial[i].io_base, 2,
-                            s->pic[pxa270_serial[i].irqn], serial_hds[i], 1);
+                           s->pic[pxa270_serial[i].irqn], 14857000/16,
+                           serial_hds[i], 1);
         else
             break;
     if (serial_hds[i])
         s->fir = pxa2xx_fir_init(0x40800000, s->pic[PXA2XX_PIC_ICP],
                         s->dma, serial_hds[i]);
 
-    if (ds)
-        s->lcd = pxa2xx_lcdc_init(0x44000000, s->pic[PXA2XX_PIC_LCD], ds);
+    s->lcd = pxa2xx_lcdc_init(0x44000000, s->pic[PXA2XX_PIC_LCD]);
 
     s->cm_base = 0x41300000;
     s->cm_regs[CCCR >> 2] = 0x02000210;        /* 416.0 MHz */
@@ -2082,20 +2111,12 @@ struct pxa2xx_state_s *pxa270_init(unsigned int sdram_size,
     register_savevm("pxa2xx_pm", 0, 0, pxa2xx_pm_save, pxa2xx_pm_load, s);
 
     for (i = 0; pxa27x_ssp[i].io_base; i ++);
-    s->ssp = (struct pxa2xx_ssp_s **)
-            qemu_mallocz(sizeof(struct pxa2xx_ssp_s *) * i);
-    ssp = (struct pxa2xx_ssp_s *)
-            qemu_mallocz(sizeof(struct pxa2xx_ssp_s) * i);
+    s->ssp = (SSIBus **)qemu_mallocz(sizeof(SSIBus *) * i);
     for (i = 0; pxa27x_ssp[i].io_base; i ++) {
-        s->ssp[i] = &ssp[i];
-        ssp[i].base = pxa27x_ssp[i].io_base;
-        ssp[i].irq = s->pic[pxa27x_ssp[i].irqn];
-
-        iomemtype = cpu_register_io_memory(0, pxa2xx_ssp_readfn,
-                        pxa2xx_ssp_writefn, &ssp[i]);
-        cpu_register_physical_memory(ssp[i].base, 0x1000, iomemtype);
-        register_savevm("pxa2xx_ssp", i, 0,
-                        pxa2xx_ssp_save, pxa2xx_ssp_load, s);
+        DeviceState *dev;
+        dev = sysbus_create_simple("pxa2xx-ssp", pxa27x_ssp[i].io_base,
+                                   s->pic[pxa27x_ssp[i].irqn]);
+        s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
     }
 
     if (usb_enabled) {
@@ -2117,24 +2138,29 @@ struct pxa2xx_state_s *pxa270_init(unsigned int sdram_size,
 
     s->i2s = pxa2xx_i2s_init(0x40400000, s->pic[PXA2XX_PIC_I2S], s->dma);
 
+    s->kp = pxa27x_keypad_init(0x41500000, s->pic[PXA2XX_PIC_KEYPAD]);
+
     /* GPIO1 resets the processor */
     /* The handler can be overridden by board-specific code */
-    pxa2xx_gpio_handler_set(s->gpio, 1, pxa2xx_reset, s);
+    pxa2xx_gpio_out_set(s->gpio, 1, s->reset);
     return s;
 }
 
 /* Initialise a PXA255 integrated chip (ARM based core).  */
-struct pxa2xx_state_s *pxa255_init(unsigned int sdram_size,
-                DisplayState *ds)
+PXA2xxState *pxa255_init(unsigned int sdram_size)
 {
-    struct pxa2xx_state_s *s;
-    struct pxa2xx_ssp_s *ssp;
+    PXA2xxState *s;
     int iomemtype, i;
-    s = (struct pxa2xx_state_s *) qemu_mallocz(sizeof(struct pxa2xx_state_s));
+    int index;
+
+    s = (PXA2xxState *) qemu_mallocz(sizeof(PXA2xxState));
 
-    s->env = cpu_init();
-    cpu_arm_set_model(s->env, "pxa255");
-    register_savevm("cpu", 0, 0, cpu_save, cpu_load, s->env);
+    s->env = cpu_init("pxa255");
+    if (!s->env) {
+        fprintf(stderr, "Unable to find CPU definition\n");
+        exit(1);
+    }
+    s->reset = qemu_allocate_irqs(pxa2xx_reset, s, 1)[0];
 
     /* SDRAM & Internal Memory Storage */
     cpu_register_physical_memory(PXA2XX_SDRAM_BASE, sdram_size,
@@ -2150,20 +2176,26 @@ struct pxa2xx_state_s *pxa255_init(unsigned int sdram_size,
 
     s->gpio = pxa2xx_gpio_init(0x40e00000, s->env, s->pic, 85);
 
-    s->mmc = pxa2xx_mmci_init(0x41100000, s->pic[PXA2XX_PIC_MMC], s->dma);
+    index = drive_get_index(IF_SD, 0, 0);
+    if (index == -1) {
+        fprintf(stderr, "qemu: missing SecureDigital device\n");
+        exit(1);
+    }
+    s->mmc = pxa2xx_mmci_init(0x41100000, drives_table[index].bdrv,
+                              s->pic[PXA2XX_PIC_MMC], s->dma);
 
     for (i = 0; pxa255_serial[i].io_base; i ++)
         if (serial_hds[i])
             serial_mm_init(pxa255_serial[i].io_base, 2,
-                            s->pic[pxa255_serial[i].irqn], serial_hds[i], 1);
+                           s->pic[pxa255_serial[i].irqn], 14745600/16,
+                           serial_hds[i], 1);
         else
             break;
     if (serial_hds[i])
         s->fir = pxa2xx_fir_init(0x40800000, s->pic[PXA2XX_PIC_ICP],
                         s->dma, serial_hds[i]);
 
-    if (ds)
-        s->lcd = pxa2xx_lcdc_init(0x44000000, s->pic[PXA2XX_PIC_LCD], ds);
+    s->lcd = pxa2xx_lcdc_init(0x44000000, s->pic[PXA2XX_PIC_LCD]);
 
     s->cm_base = 0x41300000;
     s->cm_regs[CCCR >> 2] = 0x02000210;        /* 416.0 MHz */
@@ -2191,20 +2223,12 @@ struct pxa2xx_state_s *pxa255_init(unsigned int sdram_size,
     register_savevm("pxa2xx_pm", 0, 0, pxa2xx_pm_save, pxa2xx_pm_load, s);
 
     for (i = 0; pxa255_ssp[i].io_base; i ++);
-    s->ssp = (struct pxa2xx_ssp_s **)
-            qemu_mallocz(sizeof(struct pxa2xx_ssp_s *) * i);
-    ssp = (struct pxa2xx_ssp_s *)
-            qemu_mallocz(sizeof(struct pxa2xx_ssp_s) * i);
+    s->ssp = (SSIBus **)qemu_mallocz(sizeof(SSIBus *) * i);
     for (i = 0; pxa255_ssp[i].io_base; i ++) {
-        s->ssp[i] = &ssp[i];
-        ssp[i].base = pxa255_ssp[i].io_base;
-        ssp[i].irq = s->pic[pxa255_ssp[i].irqn];
-
-        iomemtype = cpu_register_io_memory(0, pxa2xx_ssp_readfn,
-                        pxa2xx_ssp_writefn, &ssp[i]);
-        cpu_register_physical_memory(ssp[i].base, 0x1000, iomemtype);
-        register_savevm("pxa2xx_ssp", i, 0,
-                        pxa2xx_ssp_save, pxa2xx_ssp_load, s);
+        DeviceState *dev;
+        dev = sysbus_create_simple("pxa2xx-ssp", pxa255_ssp[i].io_base,
+                                   s->pic[pxa255_ssp[i].irqn]);
+        s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
     }
 
     if (usb_enabled) {
@@ -2228,6 +2252,15 @@ struct pxa2xx_state_s *pxa255_init(unsigned int sdram_size,
 
     /* GPIO1 resets the processor */
     /* The handler can be overridden by board-specific code */
-    pxa2xx_gpio_handler_set(s->gpio, 1, pxa2xx_reset, s);
+    pxa2xx_gpio_out_set(s->gpio, 1, s->reset);
     return s;
 }
+
+static void pxa2xx_register_devices(void)
+{
+    i2c_register_slave("pxa2xx-i2c-slave", sizeof(PXA2xxI2CSlaveState),
+                       &pxa2xx_i2c_slave_info);
+    sysbus_register_dev("pxa2xx-ssp", sizeof(PXA2xxSSPState), pxa2xx_ssp_init);
+}
+
+device_init(pxa2xx_register_devices)