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