xref: /openbmc/qemu/hw/arm/pxa2xx.c (revision 5a894dd7)
1 /*
2  * Intel XScale PXA255/270 processor support.
3  *
4  * Copyright (c) 2006 Openedhand Ltd.
5  * Written by Andrzej Zaborowski <balrog@zabor.org>
6  *
7  * This code is licensed under the GPL.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "qemu-common.h"
12 #include "qemu/error-report.h"
13 #include "qemu/module.h"
14 #include "qapi/error.h"
15 #include "cpu.h"
16 #include "hw/sysbus.h"
17 #include "migration/vmstate.h"
18 #include "hw/arm/pxa.h"
19 #include "sysemu/sysemu.h"
20 #include "hw/char/serial.h"
21 #include "hw/i2c/i2c.h"
22 #include "hw/irq.h"
23 #include "hw/qdev-properties.h"
24 #include "hw/ssi/ssi.h"
25 #include "hw/sd/sd.h"
26 #include "chardev/char-fe.h"
27 #include "sysemu/blockdev.h"
28 #include "sysemu/qtest.h"
29 #include "qemu/cutils.h"
30 #include "qemu/log.h"
31 
32 static struct {
33     hwaddr io_base;
34     int irqn;
35 } pxa255_serial[] = {
36     { 0x40100000, PXA2XX_PIC_FFUART },
37     { 0x40200000, PXA2XX_PIC_BTUART },
38     { 0x40700000, PXA2XX_PIC_STUART },
39     { 0x41600000, PXA25X_PIC_HWUART },
40     { 0, 0 }
41 }, pxa270_serial[] = {
42     { 0x40100000, PXA2XX_PIC_FFUART },
43     { 0x40200000, PXA2XX_PIC_BTUART },
44     { 0x40700000, PXA2XX_PIC_STUART },
45     { 0, 0 }
46 };
47 
48 typedef struct PXASSPDef {
49     hwaddr io_base;
50     int irqn;
51 } PXASSPDef;
52 
53 #if 0
54 static PXASSPDef pxa250_ssp[] = {
55     { 0x41000000, PXA2XX_PIC_SSP },
56     { 0, 0 }
57 };
58 #endif
59 
60 static PXASSPDef pxa255_ssp[] = {
61     { 0x41000000, PXA2XX_PIC_SSP },
62     { 0x41400000, PXA25X_PIC_NSSP },
63     { 0, 0 }
64 };
65 
66 #if 0
67 static PXASSPDef pxa26x_ssp[] = {
68     { 0x41000000, PXA2XX_PIC_SSP },
69     { 0x41400000, PXA25X_PIC_NSSP },
70     { 0x41500000, PXA26X_PIC_ASSP },
71     { 0, 0 }
72 };
73 #endif
74 
75 static PXASSPDef pxa27x_ssp[] = {
76     { 0x41000000, PXA2XX_PIC_SSP },
77     { 0x41700000, PXA27X_PIC_SSP2 },
78     { 0x41900000, PXA2XX_PIC_SSP3 },
79     { 0, 0 }
80 };
81 
82 #define PMCR	0x00	/* Power Manager Control register */
83 #define PSSR	0x04	/* Power Manager Sleep Status register */
84 #define PSPR	0x08	/* Power Manager Scratch-Pad register */
85 #define PWER	0x0c	/* Power Manager Wake-Up Enable register */
86 #define PRER	0x10	/* Power Manager Rising-Edge Detect Enable register */
87 #define PFER	0x14	/* Power Manager Falling-Edge Detect Enable register */
88 #define PEDR	0x18	/* Power Manager Edge-Detect Status register */
89 #define PCFR	0x1c	/* Power Manager General Configuration register */
90 #define PGSR0	0x20	/* Power Manager GPIO Sleep-State register 0 */
91 #define PGSR1	0x24	/* Power Manager GPIO Sleep-State register 1 */
92 #define PGSR2	0x28	/* Power Manager GPIO Sleep-State register 2 */
93 #define PGSR3	0x2c	/* Power Manager GPIO Sleep-State register 3 */
94 #define RCSR	0x30	/* Reset Controller Status register */
95 #define PSLR	0x34	/* Power Manager Sleep Configuration register */
96 #define PTSR	0x38	/* Power Manager Standby Configuration register */
97 #define PVCR	0x40	/* Power Manager Voltage Change Control register */
98 #define PUCR	0x4c	/* Power Manager USIM Card Control/Status register */
99 #define PKWR	0x50	/* Power Manager Keyboard Wake-Up Enable register */
100 #define PKSR	0x54	/* Power Manager Keyboard Level-Detect Status */
101 #define PCMD0	0x80	/* Power Manager I2C Command register File 0 */
102 #define PCMD31	0xfc	/* Power Manager I2C Command register File 31 */
103 
104 static uint64_t pxa2xx_pm_read(void *opaque, hwaddr addr,
105                                unsigned size)
106 {
107     PXA2xxState *s = (PXA2xxState *) opaque;
108 
109     switch (addr) {
110     case PMCR ... PCMD31:
111         if (addr & 3)
112             goto fail;
113 
114         return s->pm_regs[addr >> 2];
115     default:
116     fail:
117         qemu_log_mask(LOG_GUEST_ERROR,
118                       "%s: Bad read offset 0x%"HWADDR_PRIx"\n",
119                       __func__, addr);
120         break;
121     }
122     return 0;
123 }
124 
125 static void pxa2xx_pm_write(void *opaque, hwaddr addr,
126                             uint64_t value, unsigned size)
127 {
128     PXA2xxState *s = (PXA2xxState *) opaque;
129 
130     switch (addr) {
131     case PMCR:
132         /* Clear the write-one-to-clear bits... */
133         s->pm_regs[addr >> 2] &= ~(value & 0x2a);
134         /* ...and set the plain r/w bits */
135         s->pm_regs[addr >> 2] &= ~0x15;
136         s->pm_regs[addr >> 2] |= value & 0x15;
137         break;
138 
139     case PSSR:	/* Read-clean registers */
140     case RCSR:
141     case PKSR:
142         s->pm_regs[addr >> 2] &= ~value;
143         break;
144 
145     default:	/* Read-write registers */
146         if (!(addr & 3)) {
147             s->pm_regs[addr >> 2] = value;
148             break;
149         }
150         qemu_log_mask(LOG_GUEST_ERROR,
151                       "%s: Bad write offset 0x%"HWADDR_PRIx"\n",
152                       __func__, addr);
153         break;
154     }
155 }
156 
157 static const MemoryRegionOps pxa2xx_pm_ops = {
158     .read = pxa2xx_pm_read,
159     .write = pxa2xx_pm_write,
160     .endianness = DEVICE_NATIVE_ENDIAN,
161 };
162 
163 static const VMStateDescription vmstate_pxa2xx_pm = {
164     .name = "pxa2xx_pm",
165     .version_id = 0,
166     .minimum_version_id = 0,
167     .fields = (VMStateField[]) {
168         VMSTATE_UINT32_ARRAY(pm_regs, PXA2xxState, 0x40),
169         VMSTATE_END_OF_LIST()
170     }
171 };
172 
173 #define CCCR	0x00	/* Core Clock Configuration register */
174 #define CKEN	0x04	/* Clock Enable register */
175 #define OSCC	0x08	/* Oscillator Configuration register */
176 #define CCSR	0x0c	/* Core Clock Status register */
177 
178 static uint64_t pxa2xx_cm_read(void *opaque, hwaddr addr,
179                                unsigned size)
180 {
181     PXA2xxState *s = (PXA2xxState *) opaque;
182 
183     switch (addr) {
184     case CCCR:
185     case CKEN:
186     case OSCC:
187         return s->cm_regs[addr >> 2];
188 
189     case CCSR:
190         return s->cm_regs[CCCR >> 2] | (3 << 28);
191 
192     default:
193         qemu_log_mask(LOG_GUEST_ERROR,
194                       "%s: Bad read offset 0x%"HWADDR_PRIx"\n",
195                       __func__, addr);
196         break;
197     }
198     return 0;
199 }
200 
201 static void pxa2xx_cm_write(void *opaque, hwaddr addr,
202                             uint64_t value, unsigned size)
203 {
204     PXA2xxState *s = (PXA2xxState *) opaque;
205 
206     switch (addr) {
207     case CCCR:
208     case CKEN:
209         s->cm_regs[addr >> 2] = value;
210         break;
211 
212     case OSCC:
213         s->cm_regs[addr >> 2] &= ~0x6c;
214         s->cm_regs[addr >> 2] |= value & 0x6e;
215         if ((value >> 1) & 1)			/* OON */
216             s->cm_regs[addr >> 2] |= 1 << 0;	/* Oscillator is now stable */
217         break;
218 
219     default:
220         qemu_log_mask(LOG_GUEST_ERROR,
221                       "%s: Bad write offset 0x%"HWADDR_PRIx"\n",
222                       __func__, addr);
223         break;
224     }
225 }
226 
227 static const MemoryRegionOps pxa2xx_cm_ops = {
228     .read = pxa2xx_cm_read,
229     .write = pxa2xx_cm_write,
230     .endianness = DEVICE_NATIVE_ENDIAN,
231 };
232 
233 static const VMStateDescription vmstate_pxa2xx_cm = {
234     .name = "pxa2xx_cm",
235     .version_id = 0,
236     .minimum_version_id = 0,
237     .fields = (VMStateField[]) {
238         VMSTATE_UINT32_ARRAY(cm_regs, PXA2xxState, 4),
239         VMSTATE_UINT32(clkcfg, PXA2xxState),
240         VMSTATE_UINT32(pmnc, PXA2xxState),
241         VMSTATE_END_OF_LIST()
242     }
243 };
244 
245 static uint64_t pxa2xx_clkcfg_read(CPUARMState *env, const ARMCPRegInfo *ri)
246 {
247     PXA2xxState *s = (PXA2xxState *)ri->opaque;
248     return s->clkcfg;
249 }
250 
251 static void pxa2xx_clkcfg_write(CPUARMState *env, const ARMCPRegInfo *ri,
252                                 uint64_t value)
253 {
254     PXA2xxState *s = (PXA2xxState *)ri->opaque;
255     s->clkcfg = value & 0xf;
256     if (value & 2) {
257         printf("%s: CPU frequency change attempt\n", __func__);
258     }
259 }
260 
261 static void pxa2xx_pwrmode_write(CPUARMState *env, const ARMCPRegInfo *ri,
262                                  uint64_t value)
263 {
264     PXA2xxState *s = (PXA2xxState *)ri->opaque;
265     static const char *pwrmode[8] = {
266         "Normal", "Idle", "Deep-idle", "Standby",
267         "Sleep", "reserved (!)", "reserved (!)", "Deep-sleep",
268     };
269 
270     if (value & 8) {
271         printf("%s: CPU voltage change attempt\n", __func__);
272     }
273     switch (value & 7) {
274     case 0:
275         /* Do nothing */
276         break;
277 
278     case 1:
279         /* Idle */
280         if (!(s->cm_regs[CCCR >> 2] & (1U << 31))) { /* CPDIS */
281             cpu_interrupt(CPU(s->cpu), CPU_INTERRUPT_HALT);
282             break;
283         }
284         /* Fall through.  */
285 
286     case 2:
287         /* Deep-Idle */
288         cpu_interrupt(CPU(s->cpu), CPU_INTERRUPT_HALT);
289         s->pm_regs[RCSR >> 2] |= 0x8; /* Set GPR */
290         goto message;
291 
292     case 3:
293         s->cpu->env.uncached_cpsr = ARM_CPU_MODE_SVC;
294         s->cpu->env.daif = PSTATE_A | PSTATE_F | PSTATE_I;
295         s->cpu->env.cp15.sctlr_ns = 0;
296         s->cpu->env.cp15.cpacr_el1 = 0;
297         s->cpu->env.cp15.ttbr0_el[1] = 0;
298         s->cpu->env.cp15.dacr_ns = 0;
299         s->pm_regs[PSSR >> 2] |= 0x8; /* Set STS */
300         s->pm_regs[RCSR >> 2] |= 0x8; /* Set GPR */
301 
302         /*
303          * The scratch-pad register is almost universally used
304          * for storing the return address on suspend.  For the
305          * lack of a resuming bootloader, perform a jump
306          * directly to that address.
307          */
308         memset(s->cpu->env.regs, 0, 4 * 15);
309         s->cpu->env.regs[15] = s->pm_regs[PSPR >> 2];
310 
311 #if 0
312         buffer = 0xe59ff000; /* ldr     pc, [pc, #0] */
313         cpu_physical_memory_write(0, &buffer, 4);
314         buffer = s->pm_regs[PSPR >> 2];
315         cpu_physical_memory_write(8, &buffer, 4);
316 #endif
317 
318         /* Suspend */
319         cpu_interrupt(current_cpu, CPU_INTERRUPT_HALT);
320 
321         goto message;
322 
323     default:
324     message:
325         printf("%s: machine entered %s mode\n", __func__,
326                pwrmode[value & 7]);
327     }
328 }
329 
330 static uint64_t pxa2xx_cppmnc_read(CPUARMState *env, const ARMCPRegInfo *ri)
331 {
332     PXA2xxState *s = (PXA2xxState *)ri->opaque;
333     return s->pmnc;
334 }
335 
336 static void pxa2xx_cppmnc_write(CPUARMState *env, const ARMCPRegInfo *ri,
337                                 uint64_t value)
338 {
339     PXA2xxState *s = (PXA2xxState *)ri->opaque;
340     s->pmnc = value;
341 }
342 
343 static uint64_t pxa2xx_cpccnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
344 {
345     PXA2xxState *s = (PXA2xxState *)ri->opaque;
346     if (s->pmnc & 1) {
347         return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
348     } else {
349         return 0;
350     }
351 }
352 
353 static const ARMCPRegInfo pxa_cp_reginfo[] = {
354     /* cp14 crm==1: perf registers */
355     { .name = "CPPMNC", .cp = 14, .crn = 0, .crm = 1, .opc1 = 0, .opc2 = 0,
356       .access = PL1_RW, .type = ARM_CP_IO,
357       .readfn = pxa2xx_cppmnc_read, .writefn = pxa2xx_cppmnc_write },
358     { .name = "CPCCNT", .cp = 14, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
359       .access = PL1_RW, .type = ARM_CP_IO,
360       .readfn = pxa2xx_cpccnt_read, .writefn = arm_cp_write_ignore },
361     { .name = "CPINTEN", .cp = 14, .crn = 4, .crm = 1, .opc1 = 0, .opc2 = 0,
362       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
363     { .name = "CPFLAG", .cp = 14, .crn = 5, .crm = 1, .opc1 = 0, .opc2 = 0,
364       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
365     { .name = "CPEVTSEL", .cp = 14, .crn = 8, .crm = 1, .opc1 = 0, .opc2 = 0,
366       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
367     /* cp14 crm==2: performance count registers */
368     { .name = "CPPMN0", .cp = 14, .crn = 0, .crm = 2, .opc1 = 0, .opc2 = 0,
369       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
370     { .name = "CPPMN1", .cp = 14, .crn = 1, .crm = 2, .opc1 = 0, .opc2 = 0,
371       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
372     { .name = "CPPMN2", .cp = 14, .crn = 2, .crm = 2, .opc1 = 0, .opc2 = 0,
373       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
374     { .name = "CPPMN3", .cp = 14, .crn = 2, .crm = 3, .opc1 = 0, .opc2 = 0,
375       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
376     /* cp14 crn==6: CLKCFG */
377     { .name = "CLKCFG", .cp = 14, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
378       .access = PL1_RW, .type = ARM_CP_IO,
379       .readfn = pxa2xx_clkcfg_read, .writefn = pxa2xx_clkcfg_write },
380     /* cp14 crn==7: PWRMODE */
381     { .name = "PWRMODE", .cp = 14, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 0,
382       .access = PL1_RW, .type = ARM_CP_IO,
383       .readfn = arm_cp_read_zero, .writefn = pxa2xx_pwrmode_write },
384     REGINFO_SENTINEL
385 };
386 
387 static void pxa2xx_setup_cp14(PXA2xxState *s)
388 {
389     define_arm_cp_regs_with_opaque(s->cpu, pxa_cp_reginfo, s);
390 }
391 
392 #define MDCNFG		0x00	/* SDRAM Configuration register */
393 #define MDREFR		0x04	/* SDRAM Refresh Control register */
394 #define MSC0		0x08	/* Static Memory Control register 0 */
395 #define MSC1		0x0c	/* Static Memory Control register 1 */
396 #define MSC2		0x10	/* Static Memory Control register 2 */
397 #define MECR		0x14	/* Expansion Memory Bus Config register */
398 #define SXCNFG		0x1c	/* Synchronous Static Memory Config register */
399 #define MCMEM0		0x28	/* PC Card Memory Socket 0 Timing register */
400 #define MCMEM1		0x2c	/* PC Card Memory Socket 1 Timing register */
401 #define MCATT0		0x30	/* PC Card Attribute Socket 0 register */
402 #define MCATT1		0x34	/* PC Card Attribute Socket 1 register */
403 #define MCIO0		0x38	/* PC Card I/O Socket 0 Timing register */
404 #define MCIO1		0x3c	/* PC Card I/O Socket 1 Timing register */
405 #define MDMRS		0x40	/* SDRAM Mode Register Set Config register */
406 #define BOOT_DEF	0x44	/* Boot-time Default Configuration register */
407 #define ARB_CNTL	0x48	/* Arbiter Control register */
408 #define BSCNTR0		0x4c	/* Memory Buffer Strength Control register 0 */
409 #define BSCNTR1		0x50	/* Memory Buffer Strength Control register 1 */
410 #define LCDBSCNTR	0x54	/* LCD Buffer Strength Control register */
411 #define MDMRSLP		0x58	/* Low Power SDRAM Mode Set Config register */
412 #define BSCNTR2		0x5c	/* Memory Buffer Strength Control register 2 */
413 #define BSCNTR3		0x60	/* Memory Buffer Strength Control register 3 */
414 #define SA1110		0x64	/* SA-1110 Memory Compatibility register */
415 
416 static uint64_t pxa2xx_mm_read(void *opaque, hwaddr addr,
417                                unsigned size)
418 {
419     PXA2xxState *s = (PXA2xxState *) opaque;
420 
421     switch (addr) {
422     case MDCNFG ... SA1110:
423         if ((addr & 3) == 0)
424             return s->mm_regs[addr >> 2];
425         /* fall through */
426     default:
427         qemu_log_mask(LOG_GUEST_ERROR,
428                       "%s: Bad read offset 0x%"HWADDR_PRIx"\n",
429                       __func__, addr);
430         break;
431     }
432     return 0;
433 }
434 
435 static void pxa2xx_mm_write(void *opaque, hwaddr addr,
436                             uint64_t value, unsigned size)
437 {
438     PXA2xxState *s = (PXA2xxState *) opaque;
439 
440     switch (addr) {
441     case MDCNFG ... SA1110:
442         if ((addr & 3) == 0) {
443             s->mm_regs[addr >> 2] = value;
444             break;
445         }
446 
447     default:
448         qemu_log_mask(LOG_GUEST_ERROR,
449                       "%s: Bad write offset 0x%"HWADDR_PRIx"\n",
450                       __func__, addr);
451         break;
452     }
453 }
454 
455 static const MemoryRegionOps pxa2xx_mm_ops = {
456     .read = pxa2xx_mm_read,
457     .write = pxa2xx_mm_write,
458     .endianness = DEVICE_NATIVE_ENDIAN,
459 };
460 
461 static const VMStateDescription vmstate_pxa2xx_mm = {
462     .name = "pxa2xx_mm",
463     .version_id = 0,
464     .minimum_version_id = 0,
465     .fields = (VMStateField[]) {
466         VMSTATE_UINT32_ARRAY(mm_regs, PXA2xxState, 0x1a),
467         VMSTATE_END_OF_LIST()
468     }
469 };
470 
471 #define TYPE_PXA2XX_SSP "pxa2xx-ssp"
472 #define PXA2XX_SSP(obj) \
473     OBJECT_CHECK(PXA2xxSSPState, (obj), TYPE_PXA2XX_SSP)
474 
475 /* Synchronous Serial Ports */
476 typedef struct {
477     /*< private >*/
478     SysBusDevice parent_obj;
479     /*< public >*/
480 
481     MemoryRegion iomem;
482     qemu_irq irq;
483     uint32_t enable;
484     SSIBus *bus;
485 
486     uint32_t sscr[2];
487     uint32_t sspsp;
488     uint32_t ssto;
489     uint32_t ssitr;
490     uint32_t sssr;
491     uint8_t sstsa;
492     uint8_t ssrsa;
493     uint8_t ssacd;
494 
495     uint32_t rx_fifo[16];
496     uint32_t rx_level;
497     uint32_t rx_start;
498 } PXA2xxSSPState;
499 
500 static bool pxa2xx_ssp_vmstate_validate(void *opaque, int version_id)
501 {
502     PXA2xxSSPState *s = opaque;
503 
504     return s->rx_start < sizeof(s->rx_fifo);
505 }
506 
507 static const VMStateDescription vmstate_pxa2xx_ssp = {
508     .name = "pxa2xx-ssp",
509     .version_id = 1,
510     .minimum_version_id = 1,
511     .fields = (VMStateField[]) {
512         VMSTATE_UINT32(enable, PXA2xxSSPState),
513         VMSTATE_UINT32_ARRAY(sscr, PXA2xxSSPState, 2),
514         VMSTATE_UINT32(sspsp, PXA2xxSSPState),
515         VMSTATE_UINT32(ssto, PXA2xxSSPState),
516         VMSTATE_UINT32(ssitr, PXA2xxSSPState),
517         VMSTATE_UINT32(sssr, PXA2xxSSPState),
518         VMSTATE_UINT8(sstsa, PXA2xxSSPState),
519         VMSTATE_UINT8(ssrsa, PXA2xxSSPState),
520         VMSTATE_UINT8(ssacd, PXA2xxSSPState),
521         VMSTATE_UINT32(rx_level, PXA2xxSSPState),
522         VMSTATE_UINT32(rx_start, PXA2xxSSPState),
523         VMSTATE_VALIDATE("fifo is 16 bytes", pxa2xx_ssp_vmstate_validate),
524         VMSTATE_UINT32_ARRAY(rx_fifo, PXA2xxSSPState, 16),
525         VMSTATE_END_OF_LIST()
526     }
527 };
528 
529 #define SSCR0	0x00	/* SSP Control register 0 */
530 #define SSCR1	0x04	/* SSP Control register 1 */
531 #define SSSR	0x08	/* SSP Status register */
532 #define SSITR	0x0c	/* SSP Interrupt Test register */
533 #define SSDR	0x10	/* SSP Data register */
534 #define SSTO	0x28	/* SSP Time-Out register */
535 #define SSPSP	0x2c	/* SSP Programmable Serial Protocol register */
536 #define SSTSA	0x30	/* SSP TX Time Slot Active register */
537 #define SSRSA	0x34	/* SSP RX Time Slot Active register */
538 #define SSTSS	0x38	/* SSP Time Slot Status register */
539 #define SSACD	0x3c	/* SSP Audio Clock Divider register */
540 
541 /* Bitfields for above registers */
542 #define SSCR0_SPI(x)	(((x) & 0x30) == 0x00)
543 #define SSCR0_SSP(x)	(((x) & 0x30) == 0x10)
544 #define SSCR0_UWIRE(x)	(((x) & 0x30) == 0x20)
545 #define SSCR0_PSP(x)	(((x) & 0x30) == 0x30)
546 #define SSCR0_SSE	(1 << 7)
547 #define SSCR0_RIM	(1 << 22)
548 #define SSCR0_TIM	(1 << 23)
549 #define SSCR0_MOD       (1U << 31)
550 #define SSCR0_DSS(x)	(((((x) >> 16) & 0x10) | ((x) & 0xf)) + 1)
551 #define SSCR1_RIE	(1 << 0)
552 #define SSCR1_TIE	(1 << 1)
553 #define SSCR1_LBM	(1 << 2)
554 #define SSCR1_MWDS	(1 << 5)
555 #define SSCR1_TFT(x)	((((x) >> 6) & 0xf) + 1)
556 #define SSCR1_RFT(x)	((((x) >> 10) & 0xf) + 1)
557 #define SSCR1_EFWR	(1 << 14)
558 #define SSCR1_PINTE	(1 << 18)
559 #define SSCR1_TINTE	(1 << 19)
560 #define SSCR1_RSRE	(1 << 20)
561 #define SSCR1_TSRE	(1 << 21)
562 #define SSCR1_EBCEI	(1 << 29)
563 #define SSITR_INT	(7 << 5)
564 #define SSSR_TNF	(1 << 2)
565 #define SSSR_RNE	(1 << 3)
566 #define SSSR_TFS	(1 << 5)
567 #define SSSR_RFS	(1 << 6)
568 #define SSSR_ROR	(1 << 7)
569 #define SSSR_PINT	(1 << 18)
570 #define SSSR_TINT	(1 << 19)
571 #define SSSR_EOC	(1 << 20)
572 #define SSSR_TUR	(1 << 21)
573 #define SSSR_BCE	(1 << 23)
574 #define SSSR_RW		0x00bc0080
575 
576 static void pxa2xx_ssp_int_update(PXA2xxSSPState *s)
577 {
578     int level = 0;
579 
580     level |= s->ssitr & SSITR_INT;
581     level |= (s->sssr & SSSR_BCE)  &&  (s->sscr[1] & SSCR1_EBCEI);
582     level |= (s->sssr & SSSR_TUR)  && !(s->sscr[0] & SSCR0_TIM);
583     level |= (s->sssr & SSSR_EOC)  &&  (s->sssr & (SSSR_TINT | SSSR_PINT));
584     level |= (s->sssr & SSSR_TINT) &&  (s->sscr[1] & SSCR1_TINTE);
585     level |= (s->sssr & SSSR_PINT) &&  (s->sscr[1] & SSCR1_PINTE);
586     level |= (s->sssr & SSSR_ROR)  && !(s->sscr[0] & SSCR0_RIM);
587     level |= (s->sssr & SSSR_RFS)  &&  (s->sscr[1] & SSCR1_RIE);
588     level |= (s->sssr & SSSR_TFS)  &&  (s->sscr[1] & SSCR1_TIE);
589     qemu_set_irq(s->irq, !!level);
590 }
591 
592 static void pxa2xx_ssp_fifo_update(PXA2xxSSPState *s)
593 {
594     s->sssr &= ~(0xf << 12);	/* Clear RFL */
595     s->sssr &= ~(0xf << 8);	/* Clear TFL */
596     s->sssr &= ~SSSR_TFS;
597     s->sssr &= ~SSSR_TNF;
598     if (s->enable) {
599         s->sssr |= ((s->rx_level - 1) & 0xf) << 12;
600         if (s->rx_level >= SSCR1_RFT(s->sscr[1]))
601             s->sssr |= SSSR_RFS;
602         else
603             s->sssr &= ~SSSR_RFS;
604         if (s->rx_level)
605             s->sssr |= SSSR_RNE;
606         else
607             s->sssr &= ~SSSR_RNE;
608         /* TX FIFO is never filled, so it is always in underrun
609            condition if SSP is enabled */
610         s->sssr |= SSSR_TFS;
611         s->sssr |= SSSR_TNF;
612     }
613 
614     pxa2xx_ssp_int_update(s);
615 }
616 
617 static uint64_t pxa2xx_ssp_read(void *opaque, hwaddr addr,
618                                 unsigned size)
619 {
620     PXA2xxSSPState *s = (PXA2xxSSPState *) opaque;
621     uint32_t retval;
622 
623     switch (addr) {
624     case SSCR0:
625         return s->sscr[0];
626     case SSCR1:
627         return s->sscr[1];
628     case SSPSP:
629         return s->sspsp;
630     case SSTO:
631         return s->ssto;
632     case SSITR:
633         return s->ssitr;
634     case SSSR:
635         return s->sssr | s->ssitr;
636     case SSDR:
637         if (!s->enable)
638             return 0xffffffff;
639         if (s->rx_level < 1) {
640             printf("%s: SSP Rx Underrun\n", __func__);
641             return 0xffffffff;
642         }
643         s->rx_level --;
644         retval = s->rx_fifo[s->rx_start ++];
645         s->rx_start &= 0xf;
646         pxa2xx_ssp_fifo_update(s);
647         return retval;
648     case SSTSA:
649         return s->sstsa;
650     case SSRSA:
651         return s->ssrsa;
652     case SSTSS:
653         return 0;
654     case SSACD:
655         return s->ssacd;
656     default:
657         qemu_log_mask(LOG_GUEST_ERROR,
658                       "%s: Bad read offset 0x%"HWADDR_PRIx"\n",
659                       __func__, addr);
660         break;
661     }
662     return 0;
663 }
664 
665 static void pxa2xx_ssp_write(void *opaque, hwaddr addr,
666                              uint64_t value64, unsigned size)
667 {
668     PXA2xxSSPState *s = (PXA2xxSSPState *) opaque;
669     uint32_t value = value64;
670 
671     switch (addr) {
672     case SSCR0:
673         s->sscr[0] = value & 0xc7ffffff;
674         s->enable = value & SSCR0_SSE;
675         if (value & SSCR0_MOD)
676             printf("%s: Attempt to use network mode\n", __func__);
677         if (s->enable && SSCR0_DSS(value) < 4)
678             printf("%s: Wrong data size: %i bits\n", __func__,
679                             SSCR0_DSS(value));
680         if (!(value & SSCR0_SSE)) {
681             s->sssr = 0;
682             s->ssitr = 0;
683             s->rx_level = 0;
684         }
685         pxa2xx_ssp_fifo_update(s);
686         break;
687 
688     case SSCR1:
689         s->sscr[1] = value;
690         if (value & (SSCR1_LBM | SSCR1_EFWR))
691             printf("%s: Attempt to use SSP test mode\n", __func__);
692         pxa2xx_ssp_fifo_update(s);
693         break;
694 
695     case SSPSP:
696         s->sspsp = value;
697         break;
698 
699     case SSTO:
700         s->ssto = value;
701         break;
702 
703     case SSITR:
704         s->ssitr = value & SSITR_INT;
705         pxa2xx_ssp_int_update(s);
706         break;
707 
708     case SSSR:
709         s->sssr &= ~(value & SSSR_RW);
710         pxa2xx_ssp_int_update(s);
711         break;
712 
713     case SSDR:
714         if (SSCR0_UWIRE(s->sscr[0])) {
715             if (s->sscr[1] & SSCR1_MWDS)
716                 value &= 0xffff;
717             else
718                 value &= 0xff;
719         } else
720             /* Note how 32bits overflow does no harm here */
721             value &= (1 << SSCR0_DSS(s->sscr[0])) - 1;
722 
723         /* Data goes from here to the Tx FIFO and is shifted out from
724          * there directly to the slave, no need to buffer it.
725          */
726         if (s->enable) {
727             uint32_t readval;
728             readval = ssi_transfer(s->bus, value);
729             if (s->rx_level < 0x10) {
730                 s->rx_fifo[(s->rx_start + s->rx_level ++) & 0xf] = readval;
731             } else {
732                 s->sssr |= SSSR_ROR;
733             }
734         }
735         pxa2xx_ssp_fifo_update(s);
736         break;
737 
738     case SSTSA:
739         s->sstsa = value;
740         break;
741 
742     case SSRSA:
743         s->ssrsa = value;
744         break;
745 
746     case SSACD:
747         s->ssacd = value;
748         break;
749 
750     default:
751         qemu_log_mask(LOG_GUEST_ERROR,
752                       "%s: Bad write offset 0x%"HWADDR_PRIx"\n",
753                       __func__, addr);
754         break;
755     }
756 }
757 
758 static const MemoryRegionOps pxa2xx_ssp_ops = {
759     .read = pxa2xx_ssp_read,
760     .write = pxa2xx_ssp_write,
761     .endianness = DEVICE_NATIVE_ENDIAN,
762 };
763 
764 static void pxa2xx_ssp_reset(DeviceState *d)
765 {
766     PXA2xxSSPState *s = PXA2XX_SSP(d);
767 
768     s->enable = 0;
769     s->sscr[0] = s->sscr[1] = 0;
770     s->sspsp = 0;
771     s->ssto = 0;
772     s->ssitr = 0;
773     s->sssr = 0;
774     s->sstsa = 0;
775     s->ssrsa = 0;
776     s->ssacd = 0;
777     s->rx_start = s->rx_level = 0;
778 }
779 
780 static void pxa2xx_ssp_init(Object *obj)
781 {
782     DeviceState *dev = DEVICE(obj);
783     PXA2xxSSPState *s = PXA2XX_SSP(obj);
784     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
785     sysbus_init_irq(sbd, &s->irq);
786 
787     memory_region_init_io(&s->iomem, obj, &pxa2xx_ssp_ops, s,
788                           "pxa2xx-ssp", 0x1000);
789     sysbus_init_mmio(sbd, &s->iomem);
790 
791     s->bus = ssi_create_bus(dev, "ssi");
792 }
793 
794 /* Real-Time Clock */
795 #define RCNR		0x00	/* RTC Counter register */
796 #define RTAR		0x04	/* RTC Alarm register */
797 #define RTSR		0x08	/* RTC Status register */
798 #define RTTR		0x0c	/* RTC Timer Trim register */
799 #define RDCR		0x10	/* RTC Day Counter register */
800 #define RYCR		0x14	/* RTC Year Counter register */
801 #define RDAR1		0x18	/* RTC Wristwatch Day Alarm register 1 */
802 #define RYAR1		0x1c	/* RTC Wristwatch Year Alarm register 1 */
803 #define RDAR2		0x20	/* RTC Wristwatch Day Alarm register 2 */
804 #define RYAR2		0x24	/* RTC Wristwatch Year Alarm register 2 */
805 #define SWCR		0x28	/* RTC Stopwatch Counter register */
806 #define SWAR1		0x2c	/* RTC Stopwatch Alarm register 1 */
807 #define SWAR2		0x30	/* RTC Stopwatch Alarm register 2 */
808 #define RTCPICR		0x34	/* RTC Periodic Interrupt Counter register */
809 #define PIAR		0x38	/* RTC Periodic Interrupt Alarm register */
810 
811 #define TYPE_PXA2XX_RTC "pxa2xx_rtc"
812 #define PXA2XX_RTC(obj) \
813     OBJECT_CHECK(PXA2xxRTCState, (obj), TYPE_PXA2XX_RTC)
814 
815 typedef struct {
816     /*< private >*/
817     SysBusDevice parent_obj;
818     /*< public >*/
819 
820     MemoryRegion iomem;
821     uint32_t rttr;
822     uint32_t rtsr;
823     uint32_t rtar;
824     uint32_t rdar1;
825     uint32_t rdar2;
826     uint32_t ryar1;
827     uint32_t ryar2;
828     uint32_t swar1;
829     uint32_t swar2;
830     uint32_t piar;
831     uint32_t last_rcnr;
832     uint32_t last_rdcr;
833     uint32_t last_rycr;
834     uint32_t last_swcr;
835     uint32_t last_rtcpicr;
836     int64_t last_hz;
837     int64_t last_sw;
838     int64_t last_pi;
839     QEMUTimer *rtc_hz;
840     QEMUTimer *rtc_rdal1;
841     QEMUTimer *rtc_rdal2;
842     QEMUTimer *rtc_swal1;
843     QEMUTimer *rtc_swal2;
844     QEMUTimer *rtc_pi;
845     qemu_irq rtc_irq;
846 } PXA2xxRTCState;
847 
848 static inline void pxa2xx_rtc_int_update(PXA2xxRTCState *s)
849 {
850     qemu_set_irq(s->rtc_irq, !!(s->rtsr & 0x2553));
851 }
852 
853 static void pxa2xx_rtc_hzupdate(PXA2xxRTCState *s)
854 {
855     int64_t rt = qemu_clock_get_ms(rtc_clock);
856     s->last_rcnr += ((rt - s->last_hz) << 15) /
857             (1000 * ((s->rttr & 0xffff) + 1));
858     s->last_rdcr += ((rt - s->last_hz) << 15) /
859             (1000 * ((s->rttr & 0xffff) + 1));
860     s->last_hz = rt;
861 }
862 
863 static void pxa2xx_rtc_swupdate(PXA2xxRTCState *s)
864 {
865     int64_t rt = qemu_clock_get_ms(rtc_clock);
866     if (s->rtsr & (1 << 12))
867         s->last_swcr += (rt - s->last_sw) / 10;
868     s->last_sw = rt;
869 }
870 
871 static void pxa2xx_rtc_piupdate(PXA2xxRTCState *s)
872 {
873     int64_t rt = qemu_clock_get_ms(rtc_clock);
874     if (s->rtsr & (1 << 15))
875         s->last_swcr += rt - s->last_pi;
876     s->last_pi = rt;
877 }
878 
879 static inline void pxa2xx_rtc_alarm_update(PXA2xxRTCState *s,
880                 uint32_t rtsr)
881 {
882     if ((rtsr & (1 << 2)) && !(rtsr & (1 << 0)))
883         timer_mod(s->rtc_hz, s->last_hz +
884                 (((s->rtar - s->last_rcnr) * 1000 *
885                   ((s->rttr & 0xffff) + 1)) >> 15));
886     else
887         timer_del(s->rtc_hz);
888 
889     if ((rtsr & (1 << 5)) && !(rtsr & (1 << 4)))
890         timer_mod(s->rtc_rdal1, s->last_hz +
891                 (((s->rdar1 - s->last_rdcr) * 1000 *
892                   ((s->rttr & 0xffff) + 1)) >> 15)); /* TODO: fixup */
893     else
894         timer_del(s->rtc_rdal1);
895 
896     if ((rtsr & (1 << 7)) && !(rtsr & (1 << 6)))
897         timer_mod(s->rtc_rdal2, s->last_hz +
898                 (((s->rdar2 - s->last_rdcr) * 1000 *
899                   ((s->rttr & 0xffff) + 1)) >> 15)); /* TODO: fixup */
900     else
901         timer_del(s->rtc_rdal2);
902 
903     if ((rtsr & 0x1200) == 0x1200 && !(rtsr & (1 << 8)))
904         timer_mod(s->rtc_swal1, s->last_sw +
905                         (s->swar1 - s->last_swcr) * 10); /* TODO: fixup */
906     else
907         timer_del(s->rtc_swal1);
908 
909     if ((rtsr & 0x1800) == 0x1800 && !(rtsr & (1 << 10)))
910         timer_mod(s->rtc_swal2, s->last_sw +
911                         (s->swar2 - s->last_swcr) * 10); /* TODO: fixup */
912     else
913         timer_del(s->rtc_swal2);
914 
915     if ((rtsr & 0xc000) == 0xc000 && !(rtsr & (1 << 13)))
916         timer_mod(s->rtc_pi, s->last_pi +
917                         (s->piar & 0xffff) - s->last_rtcpicr);
918     else
919         timer_del(s->rtc_pi);
920 }
921 
922 static inline void pxa2xx_rtc_hz_tick(void *opaque)
923 {
924     PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
925     s->rtsr |= (1 << 0);
926     pxa2xx_rtc_alarm_update(s, s->rtsr);
927     pxa2xx_rtc_int_update(s);
928 }
929 
930 static inline void pxa2xx_rtc_rdal1_tick(void *opaque)
931 {
932     PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
933     s->rtsr |= (1 << 4);
934     pxa2xx_rtc_alarm_update(s, s->rtsr);
935     pxa2xx_rtc_int_update(s);
936 }
937 
938 static inline void pxa2xx_rtc_rdal2_tick(void *opaque)
939 {
940     PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
941     s->rtsr |= (1 << 6);
942     pxa2xx_rtc_alarm_update(s, s->rtsr);
943     pxa2xx_rtc_int_update(s);
944 }
945 
946 static inline void pxa2xx_rtc_swal1_tick(void *opaque)
947 {
948     PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
949     s->rtsr |= (1 << 8);
950     pxa2xx_rtc_alarm_update(s, s->rtsr);
951     pxa2xx_rtc_int_update(s);
952 }
953 
954 static inline void pxa2xx_rtc_swal2_tick(void *opaque)
955 {
956     PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
957     s->rtsr |= (1 << 10);
958     pxa2xx_rtc_alarm_update(s, s->rtsr);
959     pxa2xx_rtc_int_update(s);
960 }
961 
962 static inline void pxa2xx_rtc_pi_tick(void *opaque)
963 {
964     PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
965     s->rtsr |= (1 << 13);
966     pxa2xx_rtc_piupdate(s);
967     s->last_rtcpicr = 0;
968     pxa2xx_rtc_alarm_update(s, s->rtsr);
969     pxa2xx_rtc_int_update(s);
970 }
971 
972 static uint64_t pxa2xx_rtc_read(void *opaque, hwaddr addr,
973                                 unsigned size)
974 {
975     PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
976 
977     switch (addr) {
978     case RTTR:
979         return s->rttr;
980     case RTSR:
981         return s->rtsr;
982     case RTAR:
983         return s->rtar;
984     case RDAR1:
985         return s->rdar1;
986     case RDAR2:
987         return s->rdar2;
988     case RYAR1:
989         return s->ryar1;
990     case RYAR2:
991         return s->ryar2;
992     case SWAR1:
993         return s->swar1;
994     case SWAR2:
995         return s->swar2;
996     case PIAR:
997         return s->piar;
998     case RCNR:
999         return s->last_rcnr +
1000             ((qemu_clock_get_ms(rtc_clock) - s->last_hz) << 15) /
1001             (1000 * ((s->rttr & 0xffff) + 1));
1002     case RDCR:
1003         return s->last_rdcr +
1004             ((qemu_clock_get_ms(rtc_clock) - s->last_hz) << 15) /
1005             (1000 * ((s->rttr & 0xffff) + 1));
1006     case RYCR:
1007         return s->last_rycr;
1008     case SWCR:
1009         if (s->rtsr & (1 << 12))
1010             return s->last_swcr +
1011                 (qemu_clock_get_ms(rtc_clock) - s->last_sw) / 10;
1012         else
1013             return s->last_swcr;
1014     default:
1015         qemu_log_mask(LOG_GUEST_ERROR,
1016                       "%s: Bad read offset 0x%"HWADDR_PRIx"\n",
1017                       __func__, addr);
1018         break;
1019     }
1020     return 0;
1021 }
1022 
1023 static void pxa2xx_rtc_write(void *opaque, hwaddr addr,
1024                              uint64_t value64, unsigned size)
1025 {
1026     PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
1027     uint32_t value = value64;
1028 
1029     switch (addr) {
1030     case RTTR:
1031         if (!(s->rttr & (1U << 31))) {
1032             pxa2xx_rtc_hzupdate(s);
1033             s->rttr = value;
1034             pxa2xx_rtc_alarm_update(s, s->rtsr);
1035         }
1036         break;
1037 
1038     case RTSR:
1039         if ((s->rtsr ^ value) & (1 << 15))
1040             pxa2xx_rtc_piupdate(s);
1041 
1042         if ((s->rtsr ^ value) & (1 << 12))
1043             pxa2xx_rtc_swupdate(s);
1044 
1045         if (((s->rtsr ^ value) & 0x4aac) | (value & ~0xdaac))
1046             pxa2xx_rtc_alarm_update(s, value);
1047 
1048         s->rtsr = (value & 0xdaac) | (s->rtsr & ~(value & ~0xdaac));
1049         pxa2xx_rtc_int_update(s);
1050         break;
1051 
1052     case RTAR:
1053         s->rtar = value;
1054         pxa2xx_rtc_alarm_update(s, s->rtsr);
1055         break;
1056 
1057     case RDAR1:
1058         s->rdar1 = value;
1059         pxa2xx_rtc_alarm_update(s, s->rtsr);
1060         break;
1061 
1062     case RDAR2:
1063         s->rdar2 = value;
1064         pxa2xx_rtc_alarm_update(s, s->rtsr);
1065         break;
1066 
1067     case RYAR1:
1068         s->ryar1 = value;
1069         pxa2xx_rtc_alarm_update(s, s->rtsr);
1070         break;
1071 
1072     case RYAR2:
1073         s->ryar2 = value;
1074         pxa2xx_rtc_alarm_update(s, s->rtsr);
1075         break;
1076 
1077     case SWAR1:
1078         pxa2xx_rtc_swupdate(s);
1079         s->swar1 = value;
1080         s->last_swcr = 0;
1081         pxa2xx_rtc_alarm_update(s, s->rtsr);
1082         break;
1083 
1084     case SWAR2:
1085         s->swar2 = value;
1086         pxa2xx_rtc_alarm_update(s, s->rtsr);
1087         break;
1088 
1089     case PIAR:
1090         s->piar = value;
1091         pxa2xx_rtc_alarm_update(s, s->rtsr);
1092         break;
1093 
1094     case RCNR:
1095         pxa2xx_rtc_hzupdate(s);
1096         s->last_rcnr = value;
1097         pxa2xx_rtc_alarm_update(s, s->rtsr);
1098         break;
1099 
1100     case RDCR:
1101         pxa2xx_rtc_hzupdate(s);
1102         s->last_rdcr = value;
1103         pxa2xx_rtc_alarm_update(s, s->rtsr);
1104         break;
1105 
1106     case RYCR:
1107         s->last_rycr = value;
1108         break;
1109 
1110     case SWCR:
1111         pxa2xx_rtc_swupdate(s);
1112         s->last_swcr = value;
1113         pxa2xx_rtc_alarm_update(s, s->rtsr);
1114         break;
1115 
1116     case RTCPICR:
1117         pxa2xx_rtc_piupdate(s);
1118         s->last_rtcpicr = value & 0xffff;
1119         pxa2xx_rtc_alarm_update(s, s->rtsr);
1120         break;
1121 
1122     default:
1123         qemu_log_mask(LOG_GUEST_ERROR,
1124                       "%s: Bad write offset 0x%"HWADDR_PRIx"\n",
1125                       __func__, addr);
1126     }
1127 }
1128 
1129 static const MemoryRegionOps pxa2xx_rtc_ops = {
1130     .read = pxa2xx_rtc_read,
1131     .write = pxa2xx_rtc_write,
1132     .endianness = DEVICE_NATIVE_ENDIAN,
1133 };
1134 
1135 static void pxa2xx_rtc_init(Object *obj)
1136 {
1137     PXA2xxRTCState *s = PXA2XX_RTC(obj);
1138     SysBusDevice *dev = SYS_BUS_DEVICE(obj);
1139     struct tm tm;
1140     int wom;
1141 
1142     s->rttr = 0x7fff;
1143     s->rtsr = 0;
1144 
1145     qemu_get_timedate(&tm, 0);
1146     wom = ((tm.tm_mday - 1) / 7) + 1;
1147 
1148     s->last_rcnr = (uint32_t) mktimegm(&tm);
1149     s->last_rdcr = (wom << 20) | ((tm.tm_wday + 1) << 17) |
1150             (tm.tm_hour << 12) | (tm.tm_min << 6) | tm.tm_sec;
1151     s->last_rycr = ((tm.tm_year + 1900) << 9) |
1152             ((tm.tm_mon + 1) << 5) | tm.tm_mday;
1153     s->last_swcr = (tm.tm_hour << 19) |
1154             (tm.tm_min << 13) | (tm.tm_sec << 7);
1155     s->last_rtcpicr = 0;
1156     s->last_hz = s->last_sw = s->last_pi = qemu_clock_get_ms(rtc_clock);
1157 
1158     sysbus_init_irq(dev, &s->rtc_irq);
1159 
1160     memory_region_init_io(&s->iomem, obj, &pxa2xx_rtc_ops, s,
1161                           "pxa2xx-rtc", 0x10000);
1162     sysbus_init_mmio(dev, &s->iomem);
1163 }
1164 
1165 static void pxa2xx_rtc_realize(DeviceState *dev, Error **errp)
1166 {
1167     PXA2xxRTCState *s = PXA2XX_RTC(dev);
1168     s->rtc_hz    = timer_new_ms(rtc_clock, pxa2xx_rtc_hz_tick,    s);
1169     s->rtc_rdal1 = timer_new_ms(rtc_clock, pxa2xx_rtc_rdal1_tick, s);
1170     s->rtc_rdal2 = timer_new_ms(rtc_clock, pxa2xx_rtc_rdal2_tick, s);
1171     s->rtc_swal1 = timer_new_ms(rtc_clock, pxa2xx_rtc_swal1_tick, s);
1172     s->rtc_swal2 = timer_new_ms(rtc_clock, pxa2xx_rtc_swal2_tick, s);
1173     s->rtc_pi    = timer_new_ms(rtc_clock, pxa2xx_rtc_pi_tick,    s);
1174 }
1175 
1176 static int pxa2xx_rtc_pre_save(void *opaque)
1177 {
1178     PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
1179 
1180     pxa2xx_rtc_hzupdate(s);
1181     pxa2xx_rtc_piupdate(s);
1182     pxa2xx_rtc_swupdate(s);
1183 
1184     return 0;
1185 }
1186 
1187 static int pxa2xx_rtc_post_load(void *opaque, int version_id)
1188 {
1189     PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
1190 
1191     pxa2xx_rtc_alarm_update(s, s->rtsr);
1192 
1193     return 0;
1194 }
1195 
1196 static const VMStateDescription vmstate_pxa2xx_rtc_regs = {
1197     .name = "pxa2xx_rtc",
1198     .version_id = 0,
1199     .minimum_version_id = 0,
1200     .pre_save = pxa2xx_rtc_pre_save,
1201     .post_load = pxa2xx_rtc_post_load,
1202     .fields = (VMStateField[]) {
1203         VMSTATE_UINT32(rttr, PXA2xxRTCState),
1204         VMSTATE_UINT32(rtsr, PXA2xxRTCState),
1205         VMSTATE_UINT32(rtar, PXA2xxRTCState),
1206         VMSTATE_UINT32(rdar1, PXA2xxRTCState),
1207         VMSTATE_UINT32(rdar2, PXA2xxRTCState),
1208         VMSTATE_UINT32(ryar1, PXA2xxRTCState),
1209         VMSTATE_UINT32(ryar2, PXA2xxRTCState),
1210         VMSTATE_UINT32(swar1, PXA2xxRTCState),
1211         VMSTATE_UINT32(swar2, PXA2xxRTCState),
1212         VMSTATE_UINT32(piar, PXA2xxRTCState),
1213         VMSTATE_UINT32(last_rcnr, PXA2xxRTCState),
1214         VMSTATE_UINT32(last_rdcr, PXA2xxRTCState),
1215         VMSTATE_UINT32(last_rycr, PXA2xxRTCState),
1216         VMSTATE_UINT32(last_swcr, PXA2xxRTCState),
1217         VMSTATE_UINT32(last_rtcpicr, PXA2xxRTCState),
1218         VMSTATE_INT64(last_hz, PXA2xxRTCState),
1219         VMSTATE_INT64(last_sw, PXA2xxRTCState),
1220         VMSTATE_INT64(last_pi, PXA2xxRTCState),
1221         VMSTATE_END_OF_LIST(),
1222     },
1223 };
1224 
1225 static void pxa2xx_rtc_sysbus_class_init(ObjectClass *klass, void *data)
1226 {
1227     DeviceClass *dc = DEVICE_CLASS(klass);
1228 
1229     dc->desc = "PXA2xx RTC Controller";
1230     dc->vmsd = &vmstate_pxa2xx_rtc_regs;
1231     dc->realize = pxa2xx_rtc_realize;
1232 }
1233 
1234 static const TypeInfo pxa2xx_rtc_sysbus_info = {
1235     .name          = TYPE_PXA2XX_RTC,
1236     .parent        = TYPE_SYS_BUS_DEVICE,
1237     .instance_size = sizeof(PXA2xxRTCState),
1238     .instance_init = pxa2xx_rtc_init,
1239     .class_init    = pxa2xx_rtc_sysbus_class_init,
1240 };
1241 
1242 /* I2C Interface */
1243 
1244 #define TYPE_PXA2XX_I2C_SLAVE "pxa2xx-i2c-slave"
1245 #define PXA2XX_I2C_SLAVE(obj) \
1246     OBJECT_CHECK(PXA2xxI2CSlaveState, (obj), TYPE_PXA2XX_I2C_SLAVE)
1247 
1248 typedef struct PXA2xxI2CSlaveState {
1249     I2CSlave parent_obj;
1250 
1251     PXA2xxI2CState *host;
1252 } PXA2xxI2CSlaveState;
1253 
1254 #define TYPE_PXA2XX_I2C "pxa2xx_i2c"
1255 #define PXA2XX_I2C(obj) \
1256     OBJECT_CHECK(PXA2xxI2CState, (obj), TYPE_PXA2XX_I2C)
1257 
1258 struct PXA2xxI2CState {
1259     /*< private >*/
1260     SysBusDevice parent_obj;
1261     /*< public >*/
1262 
1263     MemoryRegion iomem;
1264     PXA2xxI2CSlaveState *slave;
1265     I2CBus *bus;
1266     qemu_irq irq;
1267     uint32_t offset;
1268     uint32_t region_size;
1269 
1270     uint16_t control;
1271     uint16_t status;
1272     uint8_t ibmr;
1273     uint8_t data;
1274 };
1275 
1276 #define IBMR	0x80	/* I2C Bus Monitor register */
1277 #define IDBR	0x88	/* I2C Data Buffer register */
1278 #define ICR	0x90	/* I2C Control register */
1279 #define ISR	0x98	/* I2C Status register */
1280 #define ISAR	0xa0	/* I2C Slave Address register */
1281 
1282 static void pxa2xx_i2c_update(PXA2xxI2CState *s)
1283 {
1284     uint16_t level = 0;
1285     level |= s->status & s->control & (1 << 10);		/* BED */
1286     level |= (s->status & (1 << 7)) && (s->control & (1 << 9));	/* IRF */
1287     level |= (s->status & (1 << 6)) && (s->control & (1 << 8));	/* ITE */
1288     level |= s->status & (1 << 9);				/* SAD */
1289     qemu_set_irq(s->irq, !!level);
1290 }
1291 
1292 /* These are only stubs now.  */
1293 static int pxa2xx_i2c_event(I2CSlave *i2c, enum i2c_event event)
1294 {
1295     PXA2xxI2CSlaveState *slave = PXA2XX_I2C_SLAVE(i2c);
1296     PXA2xxI2CState *s = slave->host;
1297 
1298     switch (event) {
1299     case I2C_START_SEND:
1300         s->status |= (1 << 9);				/* set SAD */
1301         s->status &= ~(1 << 0);				/* clear RWM */
1302         break;
1303     case I2C_START_RECV:
1304         s->status |= (1 << 9);				/* set SAD */
1305         s->status |= 1 << 0;				/* set RWM */
1306         break;
1307     case I2C_FINISH:
1308         s->status |= (1 << 4);				/* set SSD */
1309         break;
1310     case I2C_NACK:
1311         s->status |= 1 << 1;				/* set ACKNAK */
1312         break;
1313     }
1314     pxa2xx_i2c_update(s);
1315 
1316     return 0;
1317 }
1318 
1319 static uint8_t pxa2xx_i2c_rx(I2CSlave *i2c)
1320 {
1321     PXA2xxI2CSlaveState *slave = PXA2XX_I2C_SLAVE(i2c);
1322     PXA2xxI2CState *s = slave->host;
1323 
1324     if ((s->control & (1 << 14)) || !(s->control & (1 << 6))) {
1325         return 0;
1326     }
1327 
1328     if (s->status & (1 << 0)) {			/* RWM */
1329         s->status |= 1 << 6;			/* set ITE */
1330     }
1331     pxa2xx_i2c_update(s);
1332 
1333     return s->data;
1334 }
1335 
1336 static int pxa2xx_i2c_tx(I2CSlave *i2c, uint8_t data)
1337 {
1338     PXA2xxI2CSlaveState *slave = PXA2XX_I2C_SLAVE(i2c);
1339     PXA2xxI2CState *s = slave->host;
1340 
1341     if ((s->control & (1 << 14)) || !(s->control & (1 << 6))) {
1342         return 1;
1343     }
1344 
1345     if (!(s->status & (1 << 0))) {		/* RWM */
1346         s->status |= 1 << 7;			/* set IRF */
1347         s->data = data;
1348     }
1349     pxa2xx_i2c_update(s);
1350 
1351     return 1;
1352 }
1353 
1354 static uint64_t pxa2xx_i2c_read(void *opaque, hwaddr addr,
1355                                 unsigned size)
1356 {
1357     PXA2xxI2CState *s = (PXA2xxI2CState *) opaque;
1358     I2CSlave *slave;
1359 
1360     addr -= s->offset;
1361     switch (addr) {
1362     case ICR:
1363         return s->control;
1364     case ISR:
1365         return s->status | (i2c_bus_busy(s->bus) << 2);
1366     case ISAR:
1367         slave = I2C_SLAVE(s->slave);
1368         return slave->address;
1369     case IDBR:
1370         return s->data;
1371     case IBMR:
1372         if (s->status & (1 << 2))
1373             s->ibmr ^= 3;	/* Fake SCL and SDA pin changes */
1374         else
1375             s->ibmr = 0;
1376         return s->ibmr;
1377     default:
1378         qemu_log_mask(LOG_GUEST_ERROR,
1379                       "%s: Bad read offset 0x%"HWADDR_PRIx"\n",
1380                       __func__, addr);
1381         break;
1382     }
1383     return 0;
1384 }
1385 
1386 static void pxa2xx_i2c_write(void *opaque, hwaddr addr,
1387                              uint64_t value64, unsigned size)
1388 {
1389     PXA2xxI2CState *s = (PXA2xxI2CState *) opaque;
1390     uint32_t value = value64;
1391     int ack;
1392 
1393     addr -= s->offset;
1394     switch (addr) {
1395     case ICR:
1396         s->control = value & 0xfff7;
1397         if ((value & (1 << 3)) && (value & (1 << 6))) {	/* TB and IUE */
1398             /* TODO: slave mode */
1399             if (value & (1 << 0)) {			/* START condition */
1400                 if (s->data & 1)
1401                     s->status |= 1 << 0;		/* set RWM */
1402                 else
1403                     s->status &= ~(1 << 0);		/* clear RWM */
1404                 ack = !i2c_start_transfer(s->bus, s->data >> 1, s->data & 1);
1405             } else {
1406                 if (s->status & (1 << 0)) {		/* RWM */
1407                     s->data = i2c_recv(s->bus);
1408                     if (value & (1 << 2))		/* ACKNAK */
1409                         i2c_nack(s->bus);
1410                     ack = 1;
1411                 } else
1412                     ack = !i2c_send(s->bus, s->data);
1413             }
1414 
1415             if (value & (1 << 1))			/* STOP condition */
1416                 i2c_end_transfer(s->bus);
1417 
1418             if (ack) {
1419                 if (value & (1 << 0))			/* START condition */
1420                     s->status |= 1 << 6;		/* set ITE */
1421                 else
1422                     if (s->status & (1 << 0))		/* RWM */
1423                         s->status |= 1 << 7;		/* set IRF */
1424                     else
1425                         s->status |= 1 << 6;		/* set ITE */
1426                 s->status &= ~(1 << 1);			/* clear ACKNAK */
1427             } else {
1428                 s->status |= 1 << 6;			/* set ITE */
1429                 s->status |= 1 << 10;			/* set BED */
1430                 s->status |= 1 << 1;			/* set ACKNAK */
1431             }
1432         }
1433         if (!(value & (1 << 3)) && (value & (1 << 6)))	/* !TB and IUE */
1434             if (value & (1 << 4))			/* MA */
1435                 i2c_end_transfer(s->bus);
1436         pxa2xx_i2c_update(s);
1437         break;
1438 
1439     case ISR:
1440         s->status &= ~(value & 0x07f0);
1441         pxa2xx_i2c_update(s);
1442         break;
1443 
1444     case ISAR:
1445         i2c_set_slave_address(I2C_SLAVE(s->slave), value & 0x7f);
1446         break;
1447 
1448     case IDBR:
1449         s->data = value & 0xff;
1450         break;
1451 
1452     default:
1453         qemu_log_mask(LOG_GUEST_ERROR,
1454                       "%s: Bad write offset 0x%"HWADDR_PRIx"\n",
1455                       __func__, addr);
1456     }
1457 }
1458 
1459 static const MemoryRegionOps pxa2xx_i2c_ops = {
1460     .read = pxa2xx_i2c_read,
1461     .write = pxa2xx_i2c_write,
1462     .endianness = DEVICE_NATIVE_ENDIAN,
1463 };
1464 
1465 static const VMStateDescription vmstate_pxa2xx_i2c_slave = {
1466     .name = "pxa2xx_i2c_slave",
1467     .version_id = 1,
1468     .minimum_version_id = 1,
1469     .fields = (VMStateField[]) {
1470         VMSTATE_I2C_SLAVE(parent_obj, PXA2xxI2CSlaveState),
1471         VMSTATE_END_OF_LIST()
1472     }
1473 };
1474 
1475 static const VMStateDescription vmstate_pxa2xx_i2c = {
1476     .name = "pxa2xx_i2c",
1477     .version_id = 1,
1478     .minimum_version_id = 1,
1479     .fields = (VMStateField[]) {
1480         VMSTATE_UINT16(control, PXA2xxI2CState),
1481         VMSTATE_UINT16(status, PXA2xxI2CState),
1482         VMSTATE_UINT8(ibmr, PXA2xxI2CState),
1483         VMSTATE_UINT8(data, PXA2xxI2CState),
1484         VMSTATE_STRUCT_POINTER(slave, PXA2xxI2CState,
1485                                vmstate_pxa2xx_i2c_slave, PXA2xxI2CSlaveState),
1486         VMSTATE_END_OF_LIST()
1487     }
1488 };
1489 
1490 static void pxa2xx_i2c_slave_class_init(ObjectClass *klass, void *data)
1491 {
1492     I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
1493 
1494     k->event = pxa2xx_i2c_event;
1495     k->recv = pxa2xx_i2c_rx;
1496     k->send = pxa2xx_i2c_tx;
1497 }
1498 
1499 static const TypeInfo pxa2xx_i2c_slave_info = {
1500     .name          = TYPE_PXA2XX_I2C_SLAVE,
1501     .parent        = TYPE_I2C_SLAVE,
1502     .instance_size = sizeof(PXA2xxI2CSlaveState),
1503     .class_init    = pxa2xx_i2c_slave_class_init,
1504 };
1505 
1506 PXA2xxI2CState *pxa2xx_i2c_init(hwaddr base,
1507                 qemu_irq irq, uint32_t region_size)
1508 {
1509     DeviceState *dev;
1510     SysBusDevice *i2c_dev;
1511     PXA2xxI2CState *s;
1512     I2CBus *i2cbus;
1513 
1514     dev = qdev_new(TYPE_PXA2XX_I2C);
1515     qdev_prop_set_uint32(dev, "size", region_size + 1);
1516     qdev_prop_set_uint32(dev, "offset", base & region_size);
1517 
1518     i2c_dev = SYS_BUS_DEVICE(dev);
1519     sysbus_realize_and_unref(i2c_dev, &error_fatal);
1520     sysbus_mmio_map(i2c_dev, 0, base & ~region_size);
1521     sysbus_connect_irq(i2c_dev, 0, irq);
1522 
1523     s = PXA2XX_I2C(i2c_dev);
1524     /* FIXME: Should the slave device really be on a separate bus?  */
1525     i2cbus = i2c_init_bus(dev, "dummy");
1526     s->slave = PXA2XX_I2C_SLAVE(i2c_slave_create_simple(i2cbus,
1527                                                         TYPE_PXA2XX_I2C_SLAVE,
1528                                                         0));
1529     s->slave->host = s;
1530 
1531     return s;
1532 }
1533 
1534 static void pxa2xx_i2c_initfn(Object *obj)
1535 {
1536     DeviceState *dev = DEVICE(obj);
1537     PXA2xxI2CState *s = PXA2XX_I2C(obj);
1538     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
1539 
1540     s->bus = i2c_init_bus(dev, NULL);
1541 
1542     memory_region_init_io(&s->iomem, obj, &pxa2xx_i2c_ops, s,
1543                           "pxa2xx-i2c", s->region_size);
1544     sysbus_init_mmio(sbd, &s->iomem);
1545     sysbus_init_irq(sbd, &s->irq);
1546 }
1547 
1548 I2CBus *pxa2xx_i2c_bus(PXA2xxI2CState *s)
1549 {
1550     return s->bus;
1551 }
1552 
1553 static Property pxa2xx_i2c_properties[] = {
1554     DEFINE_PROP_UINT32("size", PXA2xxI2CState, region_size, 0x10000),
1555     DEFINE_PROP_UINT32("offset", PXA2xxI2CState, offset, 0),
1556     DEFINE_PROP_END_OF_LIST(),
1557 };
1558 
1559 static void pxa2xx_i2c_class_init(ObjectClass *klass, void *data)
1560 {
1561     DeviceClass *dc = DEVICE_CLASS(klass);
1562 
1563     dc->desc = "PXA2xx I2C Bus Controller";
1564     dc->vmsd = &vmstate_pxa2xx_i2c;
1565     device_class_set_props(dc, pxa2xx_i2c_properties);
1566 }
1567 
1568 static const TypeInfo pxa2xx_i2c_info = {
1569     .name          = TYPE_PXA2XX_I2C,
1570     .parent        = TYPE_SYS_BUS_DEVICE,
1571     .instance_size = sizeof(PXA2xxI2CState),
1572     .instance_init = pxa2xx_i2c_initfn,
1573     .class_init    = pxa2xx_i2c_class_init,
1574 };
1575 
1576 /* PXA Inter-IC Sound Controller */
1577 static void pxa2xx_i2s_reset(PXA2xxI2SState *i2s)
1578 {
1579     i2s->rx_len = 0;
1580     i2s->tx_len = 0;
1581     i2s->fifo_len = 0;
1582     i2s->clk = 0x1a;
1583     i2s->control[0] = 0x00;
1584     i2s->control[1] = 0x00;
1585     i2s->status = 0x00;
1586     i2s->mask = 0x00;
1587 }
1588 
1589 #define SACR_TFTH(val)	((val >> 8) & 0xf)
1590 #define SACR_RFTH(val)	((val >> 12) & 0xf)
1591 #define SACR_DREC(val)	(val & (1 << 3))
1592 #define SACR_DPRL(val)	(val & (1 << 4))
1593 
1594 static inline void pxa2xx_i2s_update(PXA2xxI2SState *i2s)
1595 {
1596     int rfs, tfs;
1597     rfs = SACR_RFTH(i2s->control[0]) < i2s->rx_len &&
1598             !SACR_DREC(i2s->control[1]);
1599     tfs = (i2s->tx_len || i2s->fifo_len < SACR_TFTH(i2s->control[0])) &&
1600             i2s->enable && !SACR_DPRL(i2s->control[1]);
1601 
1602     qemu_set_irq(i2s->rx_dma, rfs);
1603     qemu_set_irq(i2s->tx_dma, tfs);
1604 
1605     i2s->status &= 0xe0;
1606     if (i2s->fifo_len < 16 || !i2s->enable)
1607         i2s->status |= 1 << 0;			/* TNF */
1608     if (i2s->rx_len)
1609         i2s->status |= 1 << 1;			/* RNE */
1610     if (i2s->enable)
1611         i2s->status |= 1 << 2;			/* BSY */
1612     if (tfs)
1613         i2s->status |= 1 << 3;			/* TFS */
1614     if (rfs)
1615         i2s->status |= 1 << 4;			/* RFS */
1616     if (!(i2s->tx_len && i2s->enable))
1617         i2s->status |= i2s->fifo_len << 8;	/* TFL */
1618     i2s->status |= MAX(i2s->rx_len, 0xf) << 12;	/* RFL */
1619 
1620     qemu_set_irq(i2s->irq, i2s->status & i2s->mask);
1621 }
1622 
1623 #define SACR0	0x00	/* Serial Audio Global Control register */
1624 #define SACR1	0x04	/* Serial Audio I2S/MSB-Justified Control register */
1625 #define SASR0	0x0c	/* Serial Audio Interface and FIFO Status register */
1626 #define SAIMR	0x14	/* Serial Audio Interrupt Mask register */
1627 #define SAICR	0x18	/* Serial Audio Interrupt Clear register */
1628 #define SADIV	0x60	/* Serial Audio Clock Divider register */
1629 #define SADR	0x80	/* Serial Audio Data register */
1630 
1631 static uint64_t pxa2xx_i2s_read(void *opaque, hwaddr addr,
1632                                 unsigned size)
1633 {
1634     PXA2xxI2SState *s = (PXA2xxI2SState *) opaque;
1635 
1636     switch (addr) {
1637     case SACR0:
1638         return s->control[0];
1639     case SACR1:
1640         return s->control[1];
1641     case SASR0:
1642         return s->status;
1643     case SAIMR:
1644         return s->mask;
1645     case SAICR:
1646         return 0;
1647     case SADIV:
1648         return s->clk;
1649     case SADR:
1650         if (s->rx_len > 0) {
1651             s->rx_len --;
1652             pxa2xx_i2s_update(s);
1653             return s->codec_in(s->opaque);
1654         }
1655         return 0;
1656     default:
1657         qemu_log_mask(LOG_GUEST_ERROR,
1658                       "%s: Bad read offset 0x%"HWADDR_PRIx"\n",
1659                       __func__, addr);
1660         break;
1661     }
1662     return 0;
1663 }
1664 
1665 static void pxa2xx_i2s_write(void *opaque, hwaddr addr,
1666                              uint64_t value, unsigned size)
1667 {
1668     PXA2xxI2SState *s = (PXA2xxI2SState *) opaque;
1669     uint32_t *sample;
1670 
1671     switch (addr) {
1672     case SACR0:
1673         if (value & (1 << 3))				/* RST */
1674             pxa2xx_i2s_reset(s);
1675         s->control[0] = value & 0xff3d;
1676         if (!s->enable && (value & 1) && s->tx_len) {	/* ENB */
1677             for (sample = s->fifo; s->fifo_len > 0; s->fifo_len --, sample ++)
1678                 s->codec_out(s->opaque, *sample);
1679             s->status &= ~(1 << 7);			/* I2SOFF */
1680         }
1681         if (value & (1 << 4))				/* EFWR */
1682             printf("%s: Attempt to use special function\n", __func__);
1683         s->enable = (value & 9) == 1;			/* ENB && !RST*/
1684         pxa2xx_i2s_update(s);
1685         break;
1686     case SACR1:
1687         s->control[1] = value & 0x0039;
1688         if (value & (1 << 5))				/* ENLBF */
1689             printf("%s: Attempt to use loopback function\n", __func__);
1690         if (value & (1 << 4))				/* DPRL */
1691             s->fifo_len = 0;
1692         pxa2xx_i2s_update(s);
1693         break;
1694     case SAIMR:
1695         s->mask = value & 0x0078;
1696         pxa2xx_i2s_update(s);
1697         break;
1698     case SAICR:
1699         s->status &= ~(value & (3 << 5));
1700         pxa2xx_i2s_update(s);
1701         break;
1702     case SADIV:
1703         s->clk = value & 0x007f;
1704         break;
1705     case SADR:
1706         if (s->tx_len && s->enable) {
1707             s->tx_len --;
1708             pxa2xx_i2s_update(s);
1709             s->codec_out(s->opaque, value);
1710         } else if (s->fifo_len < 16) {
1711             s->fifo[s->fifo_len ++] = value;
1712             pxa2xx_i2s_update(s);
1713         }
1714         break;
1715     default:
1716         qemu_log_mask(LOG_GUEST_ERROR,
1717                       "%s: Bad write offset 0x%"HWADDR_PRIx"\n",
1718                       __func__, addr);
1719     }
1720 }
1721 
1722 static const MemoryRegionOps pxa2xx_i2s_ops = {
1723     .read = pxa2xx_i2s_read,
1724     .write = pxa2xx_i2s_write,
1725     .endianness = DEVICE_NATIVE_ENDIAN,
1726 };
1727 
1728 static const VMStateDescription vmstate_pxa2xx_i2s = {
1729     .name = "pxa2xx_i2s",
1730     .version_id = 0,
1731     .minimum_version_id = 0,
1732     .fields = (VMStateField[]) {
1733         VMSTATE_UINT32_ARRAY(control, PXA2xxI2SState, 2),
1734         VMSTATE_UINT32(status, PXA2xxI2SState),
1735         VMSTATE_UINT32(mask, PXA2xxI2SState),
1736         VMSTATE_UINT32(clk, PXA2xxI2SState),
1737         VMSTATE_INT32(enable, PXA2xxI2SState),
1738         VMSTATE_INT32(rx_len, PXA2xxI2SState),
1739         VMSTATE_INT32(tx_len, PXA2xxI2SState),
1740         VMSTATE_INT32(fifo_len, PXA2xxI2SState),
1741         VMSTATE_END_OF_LIST()
1742     }
1743 };
1744 
1745 static void pxa2xx_i2s_data_req(void *opaque, int tx, int rx)
1746 {
1747     PXA2xxI2SState *s = (PXA2xxI2SState *) opaque;
1748     uint32_t *sample;
1749 
1750     /* Signal FIFO errors */
1751     if (s->enable && s->tx_len)
1752         s->status |= 1 << 5;		/* TUR */
1753     if (s->enable && s->rx_len)
1754         s->status |= 1 << 6;		/* ROR */
1755 
1756     /* Should be tx - MIN(tx, s->fifo_len) but we don't really need to
1757      * handle the cases where it makes a difference.  */
1758     s->tx_len = tx - s->fifo_len;
1759     s->rx_len = rx;
1760     /* Note that is s->codec_out wasn't set, we wouldn't get called.  */
1761     if (s->enable)
1762         for (sample = s->fifo; s->fifo_len; s->fifo_len --, sample ++)
1763             s->codec_out(s->opaque, *sample);
1764     pxa2xx_i2s_update(s);
1765 }
1766 
1767 static PXA2xxI2SState *pxa2xx_i2s_init(MemoryRegion *sysmem,
1768                 hwaddr base,
1769                 qemu_irq irq, qemu_irq rx_dma, qemu_irq tx_dma)
1770 {
1771     PXA2xxI2SState *s = g_new0(PXA2xxI2SState, 1);
1772 
1773     s->irq = irq;
1774     s->rx_dma = rx_dma;
1775     s->tx_dma = tx_dma;
1776     s->data_req = pxa2xx_i2s_data_req;
1777 
1778     pxa2xx_i2s_reset(s);
1779 
1780     memory_region_init_io(&s->iomem, NULL, &pxa2xx_i2s_ops, s,
1781                           "pxa2xx-i2s", 0x100000);
1782     memory_region_add_subregion(sysmem, base, &s->iomem);
1783 
1784     vmstate_register(NULL, base, &vmstate_pxa2xx_i2s, s);
1785 
1786     return s;
1787 }
1788 
1789 /* PXA Fast Infra-red Communications Port */
1790 #define TYPE_PXA2XX_FIR "pxa2xx-fir"
1791 #define PXA2XX_FIR(obj) OBJECT_CHECK(PXA2xxFIrState, (obj), TYPE_PXA2XX_FIR)
1792 
1793 struct PXA2xxFIrState {
1794     /*< private >*/
1795     SysBusDevice parent_obj;
1796     /*< public >*/
1797 
1798     MemoryRegion iomem;
1799     qemu_irq irq;
1800     qemu_irq rx_dma;
1801     qemu_irq tx_dma;
1802     uint32_t enable;
1803     CharBackend chr;
1804 
1805     uint8_t control[3];
1806     uint8_t status[2];
1807 
1808     uint32_t rx_len;
1809     uint32_t rx_start;
1810     uint8_t rx_fifo[64];
1811 };
1812 
1813 static void pxa2xx_fir_reset(DeviceState *d)
1814 {
1815     PXA2xxFIrState *s = PXA2XX_FIR(d);
1816 
1817     s->control[0] = 0x00;
1818     s->control[1] = 0x00;
1819     s->control[2] = 0x00;
1820     s->status[0] = 0x00;
1821     s->status[1] = 0x00;
1822     s->enable = 0;
1823 }
1824 
1825 static inline void pxa2xx_fir_update(PXA2xxFIrState *s)
1826 {
1827     static const int tresh[4] = { 8, 16, 32, 0 };
1828     int intr = 0;
1829     if ((s->control[0] & (1 << 4)) &&			/* RXE */
1830                     s->rx_len >= tresh[s->control[2] & 3])	/* TRIG */
1831         s->status[0] |= 1 << 4;				/* RFS */
1832     else
1833         s->status[0] &= ~(1 << 4);			/* RFS */
1834     if (s->control[0] & (1 << 3))			/* TXE */
1835         s->status[0] |= 1 << 3;				/* TFS */
1836     else
1837         s->status[0] &= ~(1 << 3);			/* TFS */
1838     if (s->rx_len)
1839         s->status[1] |= 1 << 2;				/* RNE */
1840     else
1841         s->status[1] &= ~(1 << 2);			/* RNE */
1842     if (s->control[0] & (1 << 4))			/* RXE */
1843         s->status[1] |= 1 << 0;				/* RSY */
1844     else
1845         s->status[1] &= ~(1 << 0);			/* RSY */
1846 
1847     intr |= (s->control[0] & (1 << 5)) &&		/* RIE */
1848             (s->status[0] & (1 << 4));			/* RFS */
1849     intr |= (s->control[0] & (1 << 6)) &&		/* TIE */
1850             (s->status[0] & (1 << 3));			/* TFS */
1851     intr |= (s->control[2] & (1 << 4)) &&		/* TRAIL */
1852             (s->status[0] & (1 << 6));			/* EOC */
1853     intr |= (s->control[0] & (1 << 2)) &&		/* TUS */
1854             (s->status[0] & (1 << 1));			/* TUR */
1855     intr |= s->status[0] & 0x25;			/* FRE, RAB, EIF */
1856 
1857     qemu_set_irq(s->rx_dma, (s->status[0] >> 4) & 1);
1858     qemu_set_irq(s->tx_dma, (s->status[0] >> 3) & 1);
1859 
1860     qemu_set_irq(s->irq, intr && s->enable);
1861 }
1862 
1863 #define ICCR0	0x00	/* FICP Control register 0 */
1864 #define ICCR1	0x04	/* FICP Control register 1 */
1865 #define ICCR2	0x08	/* FICP Control register 2 */
1866 #define ICDR	0x0c	/* FICP Data register */
1867 #define ICSR0	0x14	/* FICP Status register 0 */
1868 #define ICSR1	0x18	/* FICP Status register 1 */
1869 #define ICFOR	0x1c	/* FICP FIFO Occupancy Status register */
1870 
1871 static uint64_t pxa2xx_fir_read(void *opaque, hwaddr addr,
1872                                 unsigned size)
1873 {
1874     PXA2xxFIrState *s = (PXA2xxFIrState *) opaque;
1875     uint8_t ret;
1876 
1877     switch (addr) {
1878     case ICCR0:
1879         return s->control[0];
1880     case ICCR1:
1881         return s->control[1];
1882     case ICCR2:
1883         return s->control[2];
1884     case ICDR:
1885         s->status[0] &= ~0x01;
1886         s->status[1] &= ~0x72;
1887         if (s->rx_len) {
1888             s->rx_len --;
1889             ret = s->rx_fifo[s->rx_start ++];
1890             s->rx_start &= 63;
1891             pxa2xx_fir_update(s);
1892             return ret;
1893         }
1894         printf("%s: Rx FIFO underrun.\n", __func__);
1895         break;
1896     case ICSR0:
1897         return s->status[0];
1898     case ICSR1:
1899         return s->status[1] | (1 << 3);			/* TNF */
1900     case ICFOR:
1901         return s->rx_len;
1902     default:
1903         qemu_log_mask(LOG_GUEST_ERROR,
1904                       "%s: Bad read offset 0x%"HWADDR_PRIx"\n",
1905                       __func__, addr);
1906         break;
1907     }
1908     return 0;
1909 }
1910 
1911 static void pxa2xx_fir_write(void *opaque, hwaddr addr,
1912                              uint64_t value64, unsigned size)
1913 {
1914     PXA2xxFIrState *s = (PXA2xxFIrState *) opaque;
1915     uint32_t value = value64;
1916     uint8_t ch;
1917 
1918     switch (addr) {
1919     case ICCR0:
1920         s->control[0] = value;
1921         if (!(value & (1 << 4)))			/* RXE */
1922             s->rx_len = s->rx_start = 0;
1923         if (!(value & (1 << 3))) {                      /* TXE */
1924             /* Nop */
1925         }
1926         s->enable = value & 1;				/* ITR */
1927         if (!s->enable)
1928             s->status[0] = 0;
1929         pxa2xx_fir_update(s);
1930         break;
1931     case ICCR1:
1932         s->control[1] = value;
1933         break;
1934     case ICCR2:
1935         s->control[2] = value & 0x3f;
1936         pxa2xx_fir_update(s);
1937         break;
1938     case ICDR:
1939         if (s->control[2] & (1 << 2)) { /* TXP */
1940             ch = value;
1941         } else {
1942             ch = ~value;
1943         }
1944         if (s->enable && (s->control[0] & (1 << 3))) { /* TXE */
1945             /* XXX this blocks entire thread. Rewrite to use
1946              * qemu_chr_fe_write and background I/O callbacks */
1947             qemu_chr_fe_write_all(&s->chr, &ch, 1);
1948         }
1949         break;
1950     case ICSR0:
1951         s->status[0] &= ~(value & 0x66);
1952         pxa2xx_fir_update(s);
1953         break;
1954     case ICFOR:
1955         break;
1956     default:
1957         qemu_log_mask(LOG_GUEST_ERROR,
1958                       "%s: Bad write offset 0x%"HWADDR_PRIx"\n",
1959                       __func__, addr);
1960     }
1961 }
1962 
1963 static const MemoryRegionOps pxa2xx_fir_ops = {
1964     .read = pxa2xx_fir_read,
1965     .write = pxa2xx_fir_write,
1966     .endianness = DEVICE_NATIVE_ENDIAN,
1967 };
1968 
1969 static int pxa2xx_fir_is_empty(void *opaque)
1970 {
1971     PXA2xxFIrState *s = (PXA2xxFIrState *) opaque;
1972     return (s->rx_len < 64);
1973 }
1974 
1975 static void pxa2xx_fir_rx(void *opaque, const uint8_t *buf, int size)
1976 {
1977     PXA2xxFIrState *s = (PXA2xxFIrState *) opaque;
1978     if (!(s->control[0] & (1 << 4)))			/* RXE */
1979         return;
1980 
1981     while (size --) {
1982         s->status[1] |= 1 << 4;				/* EOF */
1983         if (s->rx_len >= 64) {
1984             s->status[1] |= 1 << 6;			/* ROR */
1985             break;
1986         }
1987 
1988         if (s->control[2] & (1 << 3))			/* RXP */
1989             s->rx_fifo[(s->rx_start + s->rx_len ++) & 63] = *(buf ++);
1990         else
1991             s->rx_fifo[(s->rx_start + s->rx_len ++) & 63] = ~*(buf ++);
1992     }
1993 
1994     pxa2xx_fir_update(s);
1995 }
1996 
1997 static void pxa2xx_fir_event(void *opaque, QEMUChrEvent event)
1998 {
1999 }
2000 
2001 static void pxa2xx_fir_instance_init(Object *obj)
2002 {
2003     PXA2xxFIrState *s = PXA2XX_FIR(obj);
2004     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
2005 
2006     memory_region_init_io(&s->iomem, obj, &pxa2xx_fir_ops, s,
2007                           "pxa2xx-fir", 0x1000);
2008     sysbus_init_mmio(sbd, &s->iomem);
2009     sysbus_init_irq(sbd, &s->irq);
2010     sysbus_init_irq(sbd, &s->rx_dma);
2011     sysbus_init_irq(sbd, &s->tx_dma);
2012 }
2013 
2014 static void pxa2xx_fir_realize(DeviceState *dev, Error **errp)
2015 {
2016     PXA2xxFIrState *s = PXA2XX_FIR(dev);
2017 
2018     qemu_chr_fe_set_handlers(&s->chr, pxa2xx_fir_is_empty,
2019                              pxa2xx_fir_rx, pxa2xx_fir_event, NULL, s, NULL,
2020                              true);
2021 }
2022 
2023 static bool pxa2xx_fir_vmstate_validate(void *opaque, int version_id)
2024 {
2025     PXA2xxFIrState *s = opaque;
2026 
2027     return s->rx_start < ARRAY_SIZE(s->rx_fifo);
2028 }
2029 
2030 static const VMStateDescription pxa2xx_fir_vmsd = {
2031     .name = "pxa2xx-fir",
2032     .version_id = 1,
2033     .minimum_version_id = 1,
2034     .fields = (VMStateField[]) {
2035         VMSTATE_UINT32(enable, PXA2xxFIrState),
2036         VMSTATE_UINT8_ARRAY(control, PXA2xxFIrState, 3),
2037         VMSTATE_UINT8_ARRAY(status, PXA2xxFIrState, 2),
2038         VMSTATE_UINT32(rx_len, PXA2xxFIrState),
2039         VMSTATE_UINT32(rx_start, PXA2xxFIrState),
2040         VMSTATE_VALIDATE("fifo is 64 bytes", pxa2xx_fir_vmstate_validate),
2041         VMSTATE_UINT8_ARRAY(rx_fifo, PXA2xxFIrState, 64),
2042         VMSTATE_END_OF_LIST()
2043     }
2044 };
2045 
2046 static Property pxa2xx_fir_properties[] = {
2047     DEFINE_PROP_CHR("chardev", PXA2xxFIrState, chr),
2048     DEFINE_PROP_END_OF_LIST(),
2049 };
2050 
2051 static void pxa2xx_fir_class_init(ObjectClass *klass, void *data)
2052 {
2053     DeviceClass *dc = DEVICE_CLASS(klass);
2054 
2055     dc->realize = pxa2xx_fir_realize;
2056     dc->vmsd = &pxa2xx_fir_vmsd;
2057     device_class_set_props(dc, pxa2xx_fir_properties);
2058     dc->reset = pxa2xx_fir_reset;
2059 }
2060 
2061 static const TypeInfo pxa2xx_fir_info = {
2062     .name = TYPE_PXA2XX_FIR,
2063     .parent = TYPE_SYS_BUS_DEVICE,
2064     .instance_size = sizeof(PXA2xxFIrState),
2065     .class_init = pxa2xx_fir_class_init,
2066     .instance_init = pxa2xx_fir_instance_init,
2067 };
2068 
2069 static PXA2xxFIrState *pxa2xx_fir_init(MemoryRegion *sysmem,
2070                                        hwaddr base,
2071                                        qemu_irq irq, qemu_irq rx_dma,
2072                                        qemu_irq tx_dma,
2073                                        Chardev *chr)
2074 {
2075     DeviceState *dev;
2076     SysBusDevice *sbd;
2077 
2078     dev = qdev_new(TYPE_PXA2XX_FIR);
2079     qdev_prop_set_chr(dev, "chardev", chr);
2080     sbd = SYS_BUS_DEVICE(dev);
2081     sysbus_realize_and_unref(sbd, &error_fatal);
2082     sysbus_mmio_map(sbd, 0, base);
2083     sysbus_connect_irq(sbd, 0, irq);
2084     sysbus_connect_irq(sbd, 1, rx_dma);
2085     sysbus_connect_irq(sbd, 2, tx_dma);
2086     return PXA2XX_FIR(dev);
2087 }
2088 
2089 static void pxa2xx_reset(void *opaque, int line, int level)
2090 {
2091     PXA2xxState *s = (PXA2xxState *) opaque;
2092 
2093     if (level && (s->pm_regs[PCFR >> 2] & 0x10)) {	/* GPR_EN */
2094         cpu_reset(CPU(s->cpu));
2095         /* TODO: reset peripherals */
2096     }
2097 }
2098 
2099 /* Initialise a PXA270 integrated chip (ARM based core).  */
2100 PXA2xxState *pxa270_init(MemoryRegion *address_space,
2101                          unsigned int sdram_size, const char *cpu_type)
2102 {
2103     PXA2xxState *s;
2104     int i;
2105     DriveInfo *dinfo;
2106     s = g_new0(PXA2xxState, 1);
2107 
2108     if (strncmp(cpu_type, "pxa27", 5)) {
2109         error_report("Machine requires a PXA27x processor");
2110         exit(1);
2111     }
2112 
2113     s->cpu = ARM_CPU(cpu_create(cpu_type));
2114     s->reset = qemu_allocate_irq(pxa2xx_reset, s, 0);
2115 
2116     /* SDRAM & Internal Memory Storage */
2117     memory_region_init_ram(&s->sdram, NULL, "pxa270.sdram", sdram_size,
2118                            &error_fatal);
2119     memory_region_add_subregion(address_space, PXA2XX_SDRAM_BASE, &s->sdram);
2120     memory_region_init_ram(&s->internal, NULL, "pxa270.internal", 0x40000,
2121                            &error_fatal);
2122     memory_region_add_subregion(address_space, PXA2XX_INTERNAL_BASE,
2123                                 &s->internal);
2124 
2125     s->pic = pxa2xx_pic_init(0x40d00000, s->cpu);
2126 
2127     s->dma = pxa27x_dma_init(0x40000000,
2128                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_DMA));
2129 
2130     sysbus_create_varargs("pxa27x-timer", 0x40a00000,
2131                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 0),
2132                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 1),
2133                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 2),
2134                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 3),
2135                     qdev_get_gpio_in(s->pic, PXA27X_PIC_OST_4_11),
2136                     NULL);
2137 
2138     s->gpio = pxa2xx_gpio_init(0x40e00000, s->cpu, s->pic, 121);
2139 
2140     s->mmc = pxa2xx_mmci_init(address_space, 0x41100000,
2141                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_MMC),
2142                     qdev_get_gpio_in(s->dma, PXA2XX_RX_RQ_MMCI),
2143                     qdev_get_gpio_in(s->dma, PXA2XX_TX_RQ_MMCI));
2144     dinfo = drive_get(IF_SD, 0, 0);
2145     if (dinfo) {
2146         DeviceState *carddev;
2147 
2148         /* Create and plug in the sd card */
2149         carddev = qdev_new(TYPE_SD_CARD);
2150         qdev_prop_set_drive_err(carddev, "drive",
2151                                 blk_by_legacy_dinfo(dinfo), &error_fatal);
2152         qdev_realize_and_unref(carddev, qdev_get_child_bus(DEVICE(s->mmc),
2153                                                            "sd-bus"),
2154                                &error_fatal);
2155     } else if (!qtest_enabled()) {
2156         warn_report("missing SecureDigital device");
2157     }
2158 
2159     for (i = 0; pxa270_serial[i].io_base; i++) {
2160         if (serial_hd(i)) {
2161             serial_mm_init(address_space, pxa270_serial[i].io_base, 2,
2162                            qdev_get_gpio_in(s->pic, pxa270_serial[i].irqn),
2163                            14857000 / 16, serial_hd(i),
2164                            DEVICE_NATIVE_ENDIAN);
2165         } else {
2166             break;
2167         }
2168     }
2169     if (serial_hd(i))
2170         s->fir = pxa2xx_fir_init(address_space, 0x40800000,
2171                         qdev_get_gpio_in(s->pic, PXA2XX_PIC_ICP),
2172                         qdev_get_gpio_in(s->dma, PXA2XX_RX_RQ_ICP),
2173                         qdev_get_gpio_in(s->dma, PXA2XX_TX_RQ_ICP),
2174                         serial_hd(i));
2175 
2176     s->lcd = pxa2xx_lcdc_init(address_space, 0x44000000,
2177                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_LCD));
2178 
2179     s->cm_base = 0x41300000;
2180     s->cm_regs[CCCR >> 2] = 0x02000210;	/* 416.0 MHz */
2181     s->clkcfg = 0x00000009;		/* Turbo mode active */
2182     memory_region_init_io(&s->cm_iomem, NULL, &pxa2xx_cm_ops, s, "pxa2xx-cm", 0x1000);
2183     memory_region_add_subregion(address_space, s->cm_base, &s->cm_iomem);
2184     vmstate_register(NULL, 0, &vmstate_pxa2xx_cm, s);
2185 
2186     pxa2xx_setup_cp14(s);
2187 
2188     s->mm_base = 0x48000000;
2189     s->mm_regs[MDMRS >> 2] = 0x00020002;
2190     s->mm_regs[MDREFR >> 2] = 0x03ca4000;
2191     s->mm_regs[MECR >> 2] = 0x00000001;	/* Two PC Card sockets */
2192     memory_region_init_io(&s->mm_iomem, NULL, &pxa2xx_mm_ops, s, "pxa2xx-mm", 0x1000);
2193     memory_region_add_subregion(address_space, s->mm_base, &s->mm_iomem);
2194     vmstate_register(NULL, 0, &vmstate_pxa2xx_mm, s);
2195 
2196     s->pm_base = 0x40f00000;
2197     memory_region_init_io(&s->pm_iomem, NULL, &pxa2xx_pm_ops, s, "pxa2xx-pm", 0x100);
2198     memory_region_add_subregion(address_space, s->pm_base, &s->pm_iomem);
2199     vmstate_register(NULL, 0, &vmstate_pxa2xx_pm, s);
2200 
2201     for (i = 0; pxa27x_ssp[i].io_base; i ++);
2202     s->ssp = g_new0(SSIBus *, i);
2203     for (i = 0; pxa27x_ssp[i].io_base; i ++) {
2204         DeviceState *dev;
2205         dev = sysbus_create_simple(TYPE_PXA2XX_SSP, pxa27x_ssp[i].io_base,
2206                         qdev_get_gpio_in(s->pic, pxa27x_ssp[i].irqn));
2207         s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
2208     }
2209 
2210     sysbus_create_simple("sysbus-ohci", 0x4c000000,
2211                          qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
2212 
2213     s->pcmcia[0] = pxa2xx_pcmcia_init(address_space, 0x20000000);
2214     s->pcmcia[1] = pxa2xx_pcmcia_init(address_space, 0x30000000);
2215 
2216     sysbus_create_simple(TYPE_PXA2XX_RTC, 0x40900000,
2217                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_RTCALARM));
2218 
2219     s->i2c[0] = pxa2xx_i2c_init(0x40301600,
2220                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_I2C), 0xffff);
2221     s->i2c[1] = pxa2xx_i2c_init(0x40f00100,
2222                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_PWRI2C), 0xff);
2223 
2224     s->i2s = pxa2xx_i2s_init(address_space, 0x40400000,
2225                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_I2S),
2226                     qdev_get_gpio_in(s->dma, PXA2XX_RX_RQ_I2S),
2227                     qdev_get_gpio_in(s->dma, PXA2XX_TX_RQ_I2S));
2228 
2229     s->kp = pxa27x_keypad_init(address_space, 0x41500000,
2230                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_KEYPAD));
2231 
2232     /* GPIO1 resets the processor */
2233     /* The handler can be overridden by board-specific code */
2234     qdev_connect_gpio_out(s->gpio, 1, s->reset);
2235     return s;
2236 }
2237 
2238 /* Initialise a PXA255 integrated chip (ARM based core).  */
2239 PXA2xxState *pxa255_init(MemoryRegion *address_space, unsigned int sdram_size)
2240 {
2241     PXA2xxState *s;
2242     int i;
2243     DriveInfo *dinfo;
2244 
2245     s = g_new0(PXA2xxState, 1);
2246 
2247     s->cpu = ARM_CPU(cpu_create(ARM_CPU_TYPE_NAME("pxa255")));
2248     s->reset = qemu_allocate_irq(pxa2xx_reset, s, 0);
2249 
2250     /* SDRAM & Internal Memory Storage */
2251     memory_region_init_ram(&s->sdram, NULL, "pxa255.sdram", sdram_size,
2252                            &error_fatal);
2253     memory_region_add_subregion(address_space, PXA2XX_SDRAM_BASE, &s->sdram);
2254     memory_region_init_ram(&s->internal, NULL, "pxa255.internal",
2255                            PXA2XX_INTERNAL_SIZE, &error_fatal);
2256     memory_region_add_subregion(address_space, PXA2XX_INTERNAL_BASE,
2257                                 &s->internal);
2258 
2259     s->pic = pxa2xx_pic_init(0x40d00000, s->cpu);
2260 
2261     s->dma = pxa255_dma_init(0x40000000,
2262                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_DMA));
2263 
2264     sysbus_create_varargs("pxa25x-timer", 0x40a00000,
2265                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 0),
2266                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 1),
2267                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 2),
2268                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 3),
2269                     NULL);
2270 
2271     s->gpio = pxa2xx_gpio_init(0x40e00000, s->cpu, s->pic, 85);
2272 
2273     s->mmc = pxa2xx_mmci_init(address_space, 0x41100000,
2274                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_MMC),
2275                     qdev_get_gpio_in(s->dma, PXA2XX_RX_RQ_MMCI),
2276                     qdev_get_gpio_in(s->dma, PXA2XX_TX_RQ_MMCI));
2277     dinfo = drive_get(IF_SD, 0, 0);
2278     if (dinfo) {
2279         DeviceState *carddev;
2280 
2281         /* Create and plug in the sd card */
2282         carddev = qdev_new(TYPE_SD_CARD);
2283         qdev_prop_set_drive_err(carddev, "drive",
2284                                 blk_by_legacy_dinfo(dinfo), &error_fatal);
2285         qdev_realize_and_unref(carddev, qdev_get_child_bus(DEVICE(s->mmc),
2286                                                            "sd-bus"),
2287                                &error_fatal);
2288     } else if (!qtest_enabled()) {
2289         warn_report("missing SecureDigital device");
2290     }
2291 
2292     for (i = 0; pxa255_serial[i].io_base; i++) {
2293         if (serial_hd(i)) {
2294             serial_mm_init(address_space, pxa255_serial[i].io_base, 2,
2295                            qdev_get_gpio_in(s->pic, pxa255_serial[i].irqn),
2296                            14745600 / 16, serial_hd(i),
2297                            DEVICE_NATIVE_ENDIAN);
2298         } else {
2299             break;
2300         }
2301     }
2302     if (serial_hd(i))
2303         s->fir = pxa2xx_fir_init(address_space, 0x40800000,
2304                         qdev_get_gpio_in(s->pic, PXA2XX_PIC_ICP),
2305                         qdev_get_gpio_in(s->dma, PXA2XX_RX_RQ_ICP),
2306                         qdev_get_gpio_in(s->dma, PXA2XX_TX_RQ_ICP),
2307                         serial_hd(i));
2308 
2309     s->lcd = pxa2xx_lcdc_init(address_space, 0x44000000,
2310                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_LCD));
2311 
2312     s->cm_base = 0x41300000;
2313     s->cm_regs[CCCR >> 2] = 0x00000121;         /* from datasheet */
2314     s->cm_regs[CKEN >> 2] = 0x00017def;         /* from datasheet */
2315 
2316     s->clkcfg = 0x00000009;		/* Turbo mode active */
2317     memory_region_init_io(&s->cm_iomem, NULL, &pxa2xx_cm_ops, s, "pxa2xx-cm", 0x1000);
2318     memory_region_add_subregion(address_space, s->cm_base, &s->cm_iomem);
2319     vmstate_register(NULL, 0, &vmstate_pxa2xx_cm, s);
2320 
2321     pxa2xx_setup_cp14(s);
2322 
2323     s->mm_base = 0x48000000;
2324     s->mm_regs[MDMRS >> 2] = 0x00020002;
2325     s->mm_regs[MDREFR >> 2] = 0x03ca4000;
2326     s->mm_regs[MECR >> 2] = 0x00000001;	/* Two PC Card sockets */
2327     memory_region_init_io(&s->mm_iomem, NULL, &pxa2xx_mm_ops, s, "pxa2xx-mm", 0x1000);
2328     memory_region_add_subregion(address_space, s->mm_base, &s->mm_iomem);
2329     vmstate_register(NULL, 0, &vmstate_pxa2xx_mm, s);
2330 
2331     s->pm_base = 0x40f00000;
2332     memory_region_init_io(&s->pm_iomem, NULL, &pxa2xx_pm_ops, s, "pxa2xx-pm", 0x100);
2333     memory_region_add_subregion(address_space, s->pm_base, &s->pm_iomem);
2334     vmstate_register(NULL, 0, &vmstate_pxa2xx_pm, s);
2335 
2336     for (i = 0; pxa255_ssp[i].io_base; i ++);
2337     s->ssp = g_new0(SSIBus *, i);
2338     for (i = 0; pxa255_ssp[i].io_base; i ++) {
2339         DeviceState *dev;
2340         dev = sysbus_create_simple(TYPE_PXA2XX_SSP, pxa255_ssp[i].io_base,
2341                         qdev_get_gpio_in(s->pic, pxa255_ssp[i].irqn));
2342         s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
2343     }
2344 
2345     s->pcmcia[0] = pxa2xx_pcmcia_init(address_space, 0x20000000);
2346     s->pcmcia[1] = pxa2xx_pcmcia_init(address_space, 0x30000000);
2347 
2348     sysbus_create_simple(TYPE_PXA2XX_RTC, 0x40900000,
2349                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_RTCALARM));
2350 
2351     s->i2c[0] = pxa2xx_i2c_init(0x40301600,
2352                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_I2C), 0xffff);
2353     s->i2c[1] = pxa2xx_i2c_init(0x40f00100,
2354                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_PWRI2C), 0xff);
2355 
2356     s->i2s = pxa2xx_i2s_init(address_space, 0x40400000,
2357                     qdev_get_gpio_in(s->pic, PXA2XX_PIC_I2S),
2358                     qdev_get_gpio_in(s->dma, PXA2XX_RX_RQ_I2S),
2359                     qdev_get_gpio_in(s->dma, PXA2XX_TX_RQ_I2S));
2360 
2361     /* GPIO1 resets the processor */
2362     /* The handler can be overridden by board-specific code */
2363     qdev_connect_gpio_out(s->gpio, 1, s->reset);
2364     return s;
2365 }
2366 
2367 static void pxa2xx_ssp_class_init(ObjectClass *klass, void *data)
2368 {
2369     DeviceClass *dc = DEVICE_CLASS(klass);
2370 
2371     dc->reset = pxa2xx_ssp_reset;
2372     dc->vmsd = &vmstate_pxa2xx_ssp;
2373 }
2374 
2375 static const TypeInfo pxa2xx_ssp_info = {
2376     .name          = TYPE_PXA2XX_SSP,
2377     .parent        = TYPE_SYS_BUS_DEVICE,
2378     .instance_size = sizeof(PXA2xxSSPState),
2379     .instance_init = pxa2xx_ssp_init,
2380     .class_init    = pxa2xx_ssp_class_init,
2381 };
2382 
2383 static void pxa2xx_register_types(void)
2384 {
2385     type_register_static(&pxa2xx_i2c_slave_info);
2386     type_register_static(&pxa2xx_ssp_info);
2387     type_register_static(&pxa2xx_i2c_info);
2388     type_register_static(&pxa2xx_rtc_sysbus_info);
2389     type_register_static(&pxa2xx_fir_info);
2390 }
2391 
2392 type_init(pxa2xx_register_types)
2393