fix event fallout in sh_serial.c
[qemu/aliguori.git] / hw / pxa2xx.c
bloba5b7f413756c48c0310dee3ea7d0650ab381e368
1 /*
2 * Intel XScale PXA255/270 processor support.
4 * Copyright (c) 2006 Openedhand Ltd.
5 * Written by Andrzej Zaborowski <balrog@zabor.org>
7 * This code is licensed under the GPL.
8 */
10 #include "sysbus.h"
11 #include "pxa.h"
12 #include "sysemu.h"
13 #include "pc.h"
14 #include "i2c.h"
15 #include "ssi.h"
16 #include "qemu-char.h"
17 #include "blockdev.h"
19 static struct {
20 target_phys_addr_t io_base;
21 int irqn;
22 } pxa255_serial[] = {
23 { 0x40100000, PXA2XX_PIC_FFUART },
24 { 0x40200000, PXA2XX_PIC_BTUART },
25 { 0x40700000, PXA2XX_PIC_STUART },
26 { 0x41600000, PXA25X_PIC_HWUART },
27 { 0, 0 }
28 }, pxa270_serial[] = {
29 { 0x40100000, PXA2XX_PIC_FFUART },
30 { 0x40200000, PXA2XX_PIC_BTUART },
31 { 0x40700000, PXA2XX_PIC_STUART },
32 { 0, 0 }
35 typedef struct PXASSPDef {
36 target_phys_addr_t io_base;
37 int irqn;
38 } PXASSPDef;
40 #if 0
41 static PXASSPDef pxa250_ssp[] = {
42 { 0x41000000, PXA2XX_PIC_SSP },
43 { 0, 0 }
45 #endif
47 static PXASSPDef pxa255_ssp[] = {
48 { 0x41000000, PXA2XX_PIC_SSP },
49 { 0x41400000, PXA25X_PIC_NSSP },
50 { 0, 0 }
53 #if 0
54 static PXASSPDef pxa26x_ssp[] = {
55 { 0x41000000, PXA2XX_PIC_SSP },
56 { 0x41400000, PXA25X_PIC_NSSP },
57 { 0x41500000, PXA26X_PIC_ASSP },
58 { 0, 0 }
60 #endif
62 static PXASSPDef pxa27x_ssp[] = {
63 { 0x41000000, PXA2XX_PIC_SSP },
64 { 0x41700000, PXA27X_PIC_SSP2 },
65 { 0x41900000, PXA2XX_PIC_SSP3 },
66 { 0, 0 }
69 #define PMCR 0x00 /* Power Manager Control register */
70 #define PSSR 0x04 /* Power Manager Sleep Status register */
71 #define PSPR 0x08 /* Power Manager Scratch-Pad register */
72 #define PWER 0x0c /* Power Manager Wake-Up Enable register */
73 #define PRER 0x10 /* Power Manager Rising-Edge Detect Enable register */
74 #define PFER 0x14 /* Power Manager Falling-Edge Detect Enable register */
75 #define PEDR 0x18 /* Power Manager Edge-Detect Status register */
76 #define PCFR 0x1c /* Power Manager General Configuration register */
77 #define PGSR0 0x20 /* Power Manager GPIO Sleep-State register 0 */
78 #define PGSR1 0x24 /* Power Manager GPIO Sleep-State register 1 */
79 #define PGSR2 0x28 /* Power Manager GPIO Sleep-State register 2 */
80 #define PGSR3 0x2c /* Power Manager GPIO Sleep-State register 3 */
81 #define RCSR 0x30 /* Reset Controller Status register */
82 #define PSLR 0x34 /* Power Manager Sleep Configuration register */
83 #define PTSR 0x38 /* Power Manager Standby Configuration register */
84 #define PVCR 0x40 /* Power Manager Voltage Change Control register */
85 #define PUCR 0x4c /* Power Manager USIM Card Control/Status register */
86 #define PKWR 0x50 /* Power Manager Keyboard Wake-Up Enable register */
87 #define PKSR 0x54 /* Power Manager Keyboard Level-Detect Status */
88 #define PCMD0 0x80 /* Power Manager I2C Command register File 0 */
89 #define PCMD31 0xfc /* Power Manager I2C Command register File 31 */
91 static uint32_t pxa2xx_pm_read(void *opaque, target_phys_addr_t addr)
93 PXA2xxState *s = (PXA2xxState *) opaque;
95 switch (addr) {
96 case PMCR ... PCMD31:
97 if (addr & 3)
98 goto fail;
100 return s->pm_regs[addr >> 2];
101 default:
102 fail:
103 printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
104 break;
106 return 0;
109 static void pxa2xx_pm_write(void *opaque, target_phys_addr_t addr,
110 uint32_t value)
112 PXA2xxState *s = (PXA2xxState *) opaque;
114 switch (addr) {
115 case PMCR:
116 s->pm_regs[addr >> 2] &= 0x15 & ~(value & 0x2a);
117 s->pm_regs[addr >> 2] |= value & 0x15;
118 break;
120 case PSSR: /* Read-clean registers */
121 case RCSR:
122 case PKSR:
123 s->pm_regs[addr >> 2] &= ~value;
124 break;
126 default: /* Read-write registers */
127 if (!(addr & 3)) {
128 s->pm_regs[addr >> 2] = value;
129 break;
132 printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
133 break;
137 static CPUReadMemoryFunc * const pxa2xx_pm_readfn[] = {
138 pxa2xx_pm_read,
139 pxa2xx_pm_read,
140 pxa2xx_pm_read,
143 static CPUWriteMemoryFunc * const pxa2xx_pm_writefn[] = {
144 pxa2xx_pm_write,
145 pxa2xx_pm_write,
146 pxa2xx_pm_write,
149 static const VMStateDescription vmstate_pxa2xx_pm = {
150 .name = "pxa2xx_pm",
151 .version_id = 0,
152 .minimum_version_id = 0,
153 .minimum_version_id_old = 0,
154 .fields = (VMStateField[]) {
155 VMSTATE_UINT32_ARRAY(pm_regs, PXA2xxState, 0x40),
156 VMSTATE_END_OF_LIST()
160 #define CCCR 0x00 /* Core Clock Configuration register */
161 #define CKEN 0x04 /* Clock Enable register */
162 #define OSCC 0x08 /* Oscillator Configuration register */
163 #define CCSR 0x0c /* Core Clock Status register */
165 static uint32_t pxa2xx_cm_read(void *opaque, target_phys_addr_t addr)
167 PXA2xxState *s = (PXA2xxState *) opaque;
169 switch (addr) {
170 case CCCR:
171 case CKEN:
172 case OSCC:
173 return s->cm_regs[addr >> 2];
175 case CCSR:
176 return s->cm_regs[CCCR >> 2] | (3 << 28);
178 default:
179 printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
180 break;
182 return 0;
185 static void pxa2xx_cm_write(void *opaque, target_phys_addr_t addr,
186 uint32_t value)
188 PXA2xxState *s = (PXA2xxState *) opaque;
190 switch (addr) {
191 case CCCR:
192 case CKEN:
193 s->cm_regs[addr >> 2] = value;
194 break;
196 case OSCC:
197 s->cm_regs[addr >> 2] &= ~0x6c;
198 s->cm_regs[addr >> 2] |= value & 0x6e;
199 if ((value >> 1) & 1) /* OON */
200 s->cm_regs[addr >> 2] |= 1 << 0; /* Oscillator is now stable */
201 break;
203 default:
204 printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
205 break;
209 static CPUReadMemoryFunc * const pxa2xx_cm_readfn[] = {
210 pxa2xx_cm_read,
211 pxa2xx_cm_read,
212 pxa2xx_cm_read,
215 static CPUWriteMemoryFunc * const pxa2xx_cm_writefn[] = {
216 pxa2xx_cm_write,
217 pxa2xx_cm_write,
218 pxa2xx_cm_write,
221 static const VMStateDescription vmstate_pxa2xx_cm = {
222 .name = "pxa2xx_cm",
223 .version_id = 0,
224 .minimum_version_id = 0,
225 .minimum_version_id_old = 0,
226 .fields = (VMStateField[]) {
227 VMSTATE_UINT32_ARRAY(cm_regs, PXA2xxState, 4),
228 VMSTATE_UINT32(clkcfg, PXA2xxState),
229 VMSTATE_UINT32(pmnc, PXA2xxState),
230 VMSTATE_END_OF_LIST()
234 static uint32_t pxa2xx_clkpwr_read(void *opaque, int op2, int reg, int crm)
236 PXA2xxState *s = (PXA2xxState *) opaque;
238 switch (reg) {
239 case 6: /* Clock Configuration register */
240 return s->clkcfg;
242 case 7: /* Power Mode register */
243 return 0;
245 default:
246 printf("%s: Bad register 0x%x\n", __FUNCTION__, reg);
247 break;
249 return 0;
252 static void pxa2xx_clkpwr_write(void *opaque, int op2, int reg, int crm,
253 uint32_t value)
255 PXA2xxState *s = (PXA2xxState *) opaque;
256 static const char *pwrmode[8] = {
257 "Normal", "Idle", "Deep-idle", "Standby",
258 "Sleep", "reserved (!)", "reserved (!)", "Deep-sleep",
261 switch (reg) {
262 case 6: /* Clock Configuration register */
263 s->clkcfg = value & 0xf;
264 if (value & 2)
265 printf("%s: CPU frequency change attempt\n", __FUNCTION__);
266 break;
268 case 7: /* Power Mode register */
269 if (value & 8)
270 printf("%s: CPU voltage change attempt\n", __FUNCTION__);
271 switch (value & 7) {
272 case 0:
273 /* Do nothing */
274 break;
276 case 1:
277 /* Idle */
278 if (!(s->cm_regs[CCCR >> 2] & (1 << 31))) { /* CPDIS */
279 cpu_interrupt(s->env, CPU_INTERRUPT_HALT);
280 break;
282 /* Fall through. */
284 case 2:
285 /* Deep-Idle */
286 cpu_interrupt(s->env, CPU_INTERRUPT_HALT);
287 s->pm_regs[RCSR >> 2] |= 0x8; /* Set GPR */
288 goto message;
290 case 3:
291 s->env->uncached_cpsr =
292 ARM_CPU_MODE_SVC | CPSR_A | CPSR_F | CPSR_I;
293 s->env->cp15.c1_sys = 0;
294 s->env->cp15.c1_coproc = 0;
295 s->env->cp15.c2_base0 = 0;
296 s->env->cp15.c3 = 0;
297 s->pm_regs[PSSR >> 2] |= 0x8; /* Set STS */
298 s->pm_regs[RCSR >> 2] |= 0x8; /* Set GPR */
301 * The scratch-pad register is almost universally used
302 * for storing the return address on suspend. For the
303 * lack of a resuming bootloader, perform a jump
304 * directly to that address.
306 memset(s->env->regs, 0, 4 * 15);
307 s->env->regs[15] = s->pm_regs[PSPR >> 2];
309 #if 0
310 buffer = 0xe59ff000; /* ldr pc, [pc, #0] */
311 cpu_physical_memory_write(0, &buffer, 4);
312 buffer = s->pm_regs[PSPR >> 2];
313 cpu_physical_memory_write(8, &buffer, 4);
314 #endif
316 /* Suspend */
317 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_HALT);
319 goto message;
321 default:
322 message:
323 printf("%s: machine entered %s mode\n", __FUNCTION__,
324 pwrmode[value & 7]);
326 break;
328 default:
329 printf("%s: Bad register 0x%x\n", __FUNCTION__, reg);
330 break;
334 /* Performace Monitoring Registers */
335 #define CPPMNC 0 /* Performance Monitor Control register */
336 #define CPCCNT 1 /* Clock Counter register */
337 #define CPINTEN 4 /* Interrupt Enable register */
338 #define CPFLAG 5 /* Overflow Flag register */
339 #define CPEVTSEL 8 /* Event Selection register */
341 #define CPPMN0 0 /* Performance Count register 0 */
342 #define CPPMN1 1 /* Performance Count register 1 */
343 #define CPPMN2 2 /* Performance Count register 2 */
344 #define CPPMN3 3 /* Performance Count register 3 */
346 static uint32_t pxa2xx_perf_read(void *opaque, int op2, int reg, int crm)
348 PXA2xxState *s = (PXA2xxState *) opaque;
350 switch (reg) {
351 case CPPMNC:
352 return s->pmnc;
353 case CPCCNT:
354 if (s->pmnc & 1)
355 return qemu_get_clock_ns(vm_clock);
356 else
357 return 0;
358 case CPINTEN:
359 case CPFLAG:
360 case CPEVTSEL:
361 return 0;
363 default:
364 printf("%s: Bad register 0x%x\n", __FUNCTION__, reg);
365 break;
367 return 0;
370 static void pxa2xx_perf_write(void *opaque, int op2, int reg, int crm,
371 uint32_t value)
373 PXA2xxState *s = (PXA2xxState *) opaque;
375 switch (reg) {
376 case CPPMNC:
377 s->pmnc = value;
378 break;
380 case CPCCNT:
381 case CPINTEN:
382 case CPFLAG:
383 case CPEVTSEL:
384 break;
386 default:
387 printf("%s: Bad register 0x%x\n", __FUNCTION__, reg);
388 break;
392 static uint32_t pxa2xx_cp14_read(void *opaque, int op2, int reg, int crm)
394 switch (crm) {
395 case 0:
396 return pxa2xx_clkpwr_read(opaque, op2, reg, crm);
397 case 1:
398 return pxa2xx_perf_read(opaque, op2, reg, crm);
399 case 2:
400 switch (reg) {
401 case CPPMN0:
402 case CPPMN1:
403 case CPPMN2:
404 case CPPMN3:
405 return 0;
407 /* Fall through */
408 default:
409 printf("%s: Bad register 0x%x\n", __FUNCTION__, reg);
410 break;
412 return 0;
415 static void pxa2xx_cp14_write(void *opaque, int op2, int reg, int crm,
416 uint32_t value)
418 switch (crm) {
419 case 0:
420 pxa2xx_clkpwr_write(opaque, op2, reg, crm, value);
421 break;
422 case 1:
423 pxa2xx_perf_write(opaque, op2, reg, crm, value);
424 break;
425 case 2:
426 switch (reg) {
427 case CPPMN0:
428 case CPPMN1:
429 case CPPMN2:
430 case CPPMN3:
431 return;
433 /* Fall through */
434 default:
435 printf("%s: Bad register 0x%x\n", __FUNCTION__, reg);
436 break;
440 #define MDCNFG 0x00 /* SDRAM Configuration register */
441 #define MDREFR 0x04 /* SDRAM Refresh Control register */
442 #define MSC0 0x08 /* Static Memory Control register 0 */
443 #define MSC1 0x0c /* Static Memory Control register 1 */
444 #define MSC2 0x10 /* Static Memory Control register 2 */
445 #define MECR 0x14 /* Expansion Memory Bus Config register */
446 #define SXCNFG 0x1c /* Synchronous Static Memory Config register */
447 #define MCMEM0 0x28 /* PC Card Memory Socket 0 Timing register */
448 #define MCMEM1 0x2c /* PC Card Memory Socket 1 Timing register */
449 #define MCATT0 0x30 /* PC Card Attribute Socket 0 register */
450 #define MCATT1 0x34 /* PC Card Attribute Socket 1 register */
451 #define MCIO0 0x38 /* PC Card I/O Socket 0 Timing register */
452 #define MCIO1 0x3c /* PC Card I/O Socket 1 Timing register */
453 #define MDMRS 0x40 /* SDRAM Mode Register Set Config register */
454 #define BOOT_DEF 0x44 /* Boot-time Default Configuration register */
455 #define ARB_CNTL 0x48 /* Arbiter Control register */
456 #define BSCNTR0 0x4c /* Memory Buffer Strength Control register 0 */
457 #define BSCNTR1 0x50 /* Memory Buffer Strength Control register 1 */
458 #define LCDBSCNTR 0x54 /* LCD Buffer Strength Control register */
459 #define MDMRSLP 0x58 /* Low Power SDRAM Mode Set Config register */
460 #define BSCNTR2 0x5c /* Memory Buffer Strength Control register 2 */
461 #define BSCNTR3 0x60 /* Memory Buffer Strength Control register 3 */
462 #define SA1110 0x64 /* SA-1110 Memory Compatibility register */
464 static uint32_t pxa2xx_mm_read(void *opaque, target_phys_addr_t addr)
466 PXA2xxState *s = (PXA2xxState *) opaque;
468 switch (addr) {
469 case MDCNFG ... SA1110:
470 if ((addr & 3) == 0)
471 return s->mm_regs[addr >> 2];
473 default:
474 printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
475 break;
477 return 0;
480 static void pxa2xx_mm_write(void *opaque, target_phys_addr_t addr,
481 uint32_t value)
483 PXA2xxState *s = (PXA2xxState *) opaque;
485 switch (addr) {
486 case MDCNFG ... SA1110:
487 if ((addr & 3) == 0) {
488 s->mm_regs[addr >> 2] = value;
489 break;
492 default:
493 printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
494 break;
498 static CPUReadMemoryFunc * const pxa2xx_mm_readfn[] = {
499 pxa2xx_mm_read,
500 pxa2xx_mm_read,
501 pxa2xx_mm_read,
504 static CPUWriteMemoryFunc * const pxa2xx_mm_writefn[] = {
505 pxa2xx_mm_write,
506 pxa2xx_mm_write,
507 pxa2xx_mm_write,
510 static const VMStateDescription vmstate_pxa2xx_mm = {
511 .name = "pxa2xx_mm",
512 .version_id = 0,
513 .minimum_version_id = 0,
514 .minimum_version_id_old = 0,
515 .fields = (VMStateField[]) {
516 VMSTATE_UINT32_ARRAY(mm_regs, PXA2xxState, 0x1a),
517 VMSTATE_END_OF_LIST()
521 /* Synchronous Serial Ports */
522 typedef struct {
523 SysBusDevice busdev;
524 qemu_irq irq;
525 int enable;
526 SSIBus *bus;
528 uint32_t sscr[2];
529 uint32_t sspsp;
530 uint32_t ssto;
531 uint32_t ssitr;
532 uint32_t sssr;
533 uint8_t sstsa;
534 uint8_t ssrsa;
535 uint8_t ssacd;
537 uint32_t rx_fifo[16];
538 int rx_level;
539 int rx_start;
540 } PXA2xxSSPState;
542 #define SSCR0 0x00 /* SSP Control register 0 */
543 #define SSCR1 0x04 /* SSP Control register 1 */
544 #define SSSR 0x08 /* SSP Status register */
545 #define SSITR 0x0c /* SSP Interrupt Test register */
546 #define SSDR 0x10 /* SSP Data register */
547 #define SSTO 0x28 /* SSP Time-Out register */
548 #define SSPSP 0x2c /* SSP Programmable Serial Protocol register */
549 #define SSTSA 0x30 /* SSP TX Time Slot Active register */
550 #define SSRSA 0x34 /* SSP RX Time Slot Active register */
551 #define SSTSS 0x38 /* SSP Time Slot Status register */
552 #define SSACD 0x3c /* SSP Audio Clock Divider register */
554 /* Bitfields for above registers */
555 #define SSCR0_SPI(x) (((x) & 0x30) == 0x00)
556 #define SSCR0_SSP(x) (((x) & 0x30) == 0x10)
557 #define SSCR0_UWIRE(x) (((x) & 0x30) == 0x20)
558 #define SSCR0_PSP(x) (((x) & 0x30) == 0x30)
559 #define SSCR0_SSE (1 << 7)
560 #define SSCR0_RIM (1 << 22)
561 #define SSCR0_TIM (1 << 23)
562 #define SSCR0_MOD (1 << 31)
563 #define SSCR0_DSS(x) (((((x) >> 16) & 0x10) | ((x) & 0xf)) + 1)
564 #define SSCR1_RIE (1 << 0)
565 #define SSCR1_TIE (1 << 1)
566 #define SSCR1_LBM (1 << 2)
567 #define SSCR1_MWDS (1 << 5)
568 #define SSCR1_TFT(x) ((((x) >> 6) & 0xf) + 1)
569 #define SSCR1_RFT(x) ((((x) >> 10) & 0xf) + 1)
570 #define SSCR1_EFWR (1 << 14)
571 #define SSCR1_PINTE (1 << 18)
572 #define SSCR1_TINTE (1 << 19)
573 #define SSCR1_RSRE (1 << 20)
574 #define SSCR1_TSRE (1 << 21)
575 #define SSCR1_EBCEI (1 << 29)
576 #define SSITR_INT (7 << 5)
577 #define SSSR_TNF (1 << 2)
578 #define SSSR_RNE (1 << 3)
579 #define SSSR_TFS (1 << 5)
580 #define SSSR_RFS (1 << 6)
581 #define SSSR_ROR (1 << 7)
582 #define SSSR_PINT (1 << 18)
583 #define SSSR_TINT (1 << 19)
584 #define SSSR_EOC (1 << 20)
585 #define SSSR_TUR (1 << 21)
586 #define SSSR_BCE (1 << 23)
587 #define SSSR_RW 0x00bc0080
589 static void pxa2xx_ssp_int_update(PXA2xxSSPState *s)
591 int level = 0;
593 level |= s->ssitr & SSITR_INT;
594 level |= (s->sssr & SSSR_BCE) && (s->sscr[1] & SSCR1_EBCEI);
595 level |= (s->sssr & SSSR_TUR) && !(s->sscr[0] & SSCR0_TIM);
596 level |= (s->sssr & SSSR_EOC) && (s->sssr & (SSSR_TINT | SSSR_PINT));
597 level |= (s->sssr & SSSR_TINT) && (s->sscr[1] & SSCR1_TINTE);
598 level |= (s->sssr & SSSR_PINT) && (s->sscr[1] & SSCR1_PINTE);
599 level |= (s->sssr & SSSR_ROR) && !(s->sscr[0] & SSCR0_RIM);
600 level |= (s->sssr & SSSR_RFS) && (s->sscr[1] & SSCR1_RIE);
601 level |= (s->sssr & SSSR_TFS) && (s->sscr[1] & SSCR1_TIE);
602 qemu_set_irq(s->irq, !!level);
605 static void pxa2xx_ssp_fifo_update(PXA2xxSSPState *s)
607 s->sssr &= ~(0xf << 12); /* Clear RFL */
608 s->sssr &= ~(0xf << 8); /* Clear TFL */
609 s->sssr &= ~SSSR_TFS;
610 s->sssr &= ~SSSR_TNF;
611 if (s->enable) {
612 s->sssr |= ((s->rx_level - 1) & 0xf) << 12;
613 if (s->rx_level >= SSCR1_RFT(s->sscr[1]))
614 s->sssr |= SSSR_RFS;
615 else
616 s->sssr &= ~SSSR_RFS;
617 if (s->rx_level)
618 s->sssr |= SSSR_RNE;
619 else
620 s->sssr &= ~SSSR_RNE;
621 /* TX FIFO is never filled, so it is always in underrun
622 condition if SSP is enabled */
623 s->sssr |= SSSR_TFS;
624 s->sssr |= SSSR_TNF;
627 pxa2xx_ssp_int_update(s);
630 static uint32_t pxa2xx_ssp_read(void *opaque, target_phys_addr_t addr)
632 PXA2xxSSPState *s = (PXA2xxSSPState *) opaque;
633 uint32_t retval;
635 switch (addr) {
636 case SSCR0:
637 return s->sscr[0];
638 case SSCR1:
639 return s->sscr[1];
640 case SSPSP:
641 return s->sspsp;
642 case SSTO:
643 return s->ssto;
644 case SSITR:
645 return s->ssitr;
646 case SSSR:
647 return s->sssr | s->ssitr;
648 case SSDR:
649 if (!s->enable)
650 return 0xffffffff;
651 if (s->rx_level < 1) {
652 printf("%s: SSP Rx Underrun\n", __FUNCTION__);
653 return 0xffffffff;
655 s->rx_level --;
656 retval = s->rx_fifo[s->rx_start ++];
657 s->rx_start &= 0xf;
658 pxa2xx_ssp_fifo_update(s);
659 return retval;
660 case SSTSA:
661 return s->sstsa;
662 case SSRSA:
663 return s->ssrsa;
664 case SSTSS:
665 return 0;
666 case SSACD:
667 return s->ssacd;
668 default:
669 printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
670 break;
672 return 0;
675 static void pxa2xx_ssp_write(void *opaque, target_phys_addr_t addr,
676 uint32_t value)
678 PXA2xxSSPState *s = (PXA2xxSSPState *) opaque;
680 switch (addr) {
681 case SSCR0:
682 s->sscr[0] = value & 0xc7ffffff;
683 s->enable = value & SSCR0_SSE;
684 if (value & SSCR0_MOD)
685 printf("%s: Attempt to use network mode\n", __FUNCTION__);
686 if (s->enable && SSCR0_DSS(value) < 4)
687 printf("%s: Wrong data size: %i bits\n", __FUNCTION__,
688 SSCR0_DSS(value));
689 if (!(value & SSCR0_SSE)) {
690 s->sssr = 0;
691 s->ssitr = 0;
692 s->rx_level = 0;
694 pxa2xx_ssp_fifo_update(s);
695 break;
697 case SSCR1:
698 s->sscr[1] = value;
699 if (value & (SSCR1_LBM | SSCR1_EFWR))
700 printf("%s: Attempt to use SSP test mode\n", __FUNCTION__);
701 pxa2xx_ssp_fifo_update(s);
702 break;
704 case SSPSP:
705 s->sspsp = value;
706 break;
708 case SSTO:
709 s->ssto = value;
710 break;
712 case SSITR:
713 s->ssitr = value & SSITR_INT;
714 pxa2xx_ssp_int_update(s);
715 break;
717 case SSSR:
718 s->sssr &= ~(value & SSSR_RW);
719 pxa2xx_ssp_int_update(s);
720 break;
722 case SSDR:
723 if (SSCR0_UWIRE(s->sscr[0])) {
724 if (s->sscr[1] & SSCR1_MWDS)
725 value &= 0xffff;
726 else
727 value &= 0xff;
728 } else
729 /* Note how 32bits overflow does no harm here */
730 value &= (1 << SSCR0_DSS(s->sscr[0])) - 1;
732 /* Data goes from here to the Tx FIFO and is shifted out from
733 * there directly to the slave, no need to buffer it.
735 if (s->enable) {
736 uint32_t readval;
737 readval = ssi_transfer(s->bus, value);
738 if (s->rx_level < 0x10) {
739 s->rx_fifo[(s->rx_start + s->rx_level ++) & 0xf] = readval;
740 } else {
741 s->sssr |= SSSR_ROR;
744 pxa2xx_ssp_fifo_update(s);
745 break;
747 case SSTSA:
748 s->sstsa = value;
749 break;
751 case SSRSA:
752 s->ssrsa = value;
753 break;
755 case SSACD:
756 s->ssacd = value;
757 break;
759 default:
760 printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
761 break;
765 static CPUReadMemoryFunc * const pxa2xx_ssp_readfn[] = {
766 pxa2xx_ssp_read,
767 pxa2xx_ssp_read,
768 pxa2xx_ssp_read,
771 static CPUWriteMemoryFunc * const pxa2xx_ssp_writefn[] = {
772 pxa2xx_ssp_write,
773 pxa2xx_ssp_write,
774 pxa2xx_ssp_write,
777 static void pxa2xx_ssp_save(QEMUFile *f, void *opaque)
779 PXA2xxSSPState *s = (PXA2xxSSPState *) opaque;
780 int i;
782 qemu_put_be32(f, s->enable);
784 qemu_put_be32s(f, &s->sscr[0]);
785 qemu_put_be32s(f, &s->sscr[1]);
786 qemu_put_be32s(f, &s->sspsp);
787 qemu_put_be32s(f, &s->ssto);
788 qemu_put_be32s(f, &s->ssitr);
789 qemu_put_be32s(f, &s->sssr);
790 qemu_put_8s(f, &s->sstsa);
791 qemu_put_8s(f, &s->ssrsa);
792 qemu_put_8s(f, &s->ssacd);
794 qemu_put_byte(f, s->rx_level);
795 for (i = 0; i < s->rx_level; i ++)
796 qemu_put_byte(f, s->rx_fifo[(s->rx_start + i) & 0xf]);
799 static int pxa2xx_ssp_load(QEMUFile *f, void *opaque, int version_id)
801 PXA2xxSSPState *s = (PXA2xxSSPState *) opaque;
802 int i;
804 s->enable = qemu_get_be32(f);
806 qemu_get_be32s(f, &s->sscr[0]);
807 qemu_get_be32s(f, &s->sscr[1]);
808 qemu_get_be32s(f, &s->sspsp);
809 qemu_get_be32s(f, &s->ssto);
810 qemu_get_be32s(f, &s->ssitr);
811 qemu_get_be32s(f, &s->sssr);
812 qemu_get_8s(f, &s->sstsa);
813 qemu_get_8s(f, &s->ssrsa);
814 qemu_get_8s(f, &s->ssacd);
816 s->rx_level = qemu_get_byte(f);
817 s->rx_start = 0;
818 for (i = 0; i < s->rx_level; i ++)
819 s->rx_fifo[i] = qemu_get_byte(f);
821 return 0;
824 static int pxa2xx_ssp_init(SysBusDevice *dev)
826 int iomemtype;
827 PXA2xxSSPState *s = FROM_SYSBUS(PXA2xxSSPState, dev);
829 sysbus_init_irq(dev, &s->irq);
831 iomemtype = cpu_register_io_memory(pxa2xx_ssp_readfn,
832 pxa2xx_ssp_writefn, s,
833 DEVICE_NATIVE_ENDIAN);
834 sysbus_init_mmio(dev, 0x1000, iomemtype);
835 register_savevm(&dev->qdev, "pxa2xx_ssp", -1, 0,
836 pxa2xx_ssp_save, pxa2xx_ssp_load, s);
838 s->bus = ssi_create_bus(&dev->qdev, "ssi");
839 return 0;
842 /* Real-Time Clock */
843 #define RCNR 0x00 /* RTC Counter register */
844 #define RTAR 0x04 /* RTC Alarm register */
845 #define RTSR 0x08 /* RTC Status register */
846 #define RTTR 0x0c /* RTC Timer Trim register */
847 #define RDCR 0x10 /* RTC Day Counter register */
848 #define RYCR 0x14 /* RTC Year Counter register */
849 #define RDAR1 0x18 /* RTC Wristwatch Day Alarm register 1 */
850 #define RYAR1 0x1c /* RTC Wristwatch Year Alarm register 1 */
851 #define RDAR2 0x20 /* RTC Wristwatch Day Alarm register 2 */
852 #define RYAR2 0x24 /* RTC Wristwatch Year Alarm register 2 */
853 #define SWCR 0x28 /* RTC Stopwatch Counter register */
854 #define SWAR1 0x2c /* RTC Stopwatch Alarm register 1 */
855 #define SWAR2 0x30 /* RTC Stopwatch Alarm register 2 */
856 #define RTCPICR 0x34 /* RTC Periodic Interrupt Counter register */
857 #define PIAR 0x38 /* RTC Periodic Interrupt Alarm register */
859 typedef struct {
860 SysBusDevice busdev;
861 uint32_t rttr;
862 uint32_t rtsr;
863 uint32_t rtar;
864 uint32_t rdar1;
865 uint32_t rdar2;
866 uint32_t ryar1;
867 uint32_t ryar2;
868 uint32_t swar1;
869 uint32_t swar2;
870 uint32_t piar;
871 uint32_t last_rcnr;
872 uint32_t last_rdcr;
873 uint32_t last_rycr;
874 uint32_t last_swcr;
875 uint32_t last_rtcpicr;
876 int64_t last_hz;
877 int64_t last_sw;
878 int64_t last_pi;
879 QEMUTimer *rtc_hz;
880 QEMUTimer *rtc_rdal1;
881 QEMUTimer *rtc_rdal2;
882 QEMUTimer *rtc_swal1;
883 QEMUTimer *rtc_swal2;
884 QEMUTimer *rtc_pi;
885 qemu_irq rtc_irq;
886 } PXA2xxRTCState;
888 static inline void pxa2xx_rtc_int_update(PXA2xxRTCState *s)
890 qemu_set_irq(s->rtc_irq, !!(s->rtsr & 0x2553));
893 static void pxa2xx_rtc_hzupdate(PXA2xxRTCState *s)
895 int64_t rt = qemu_get_clock_ms(rt_clock);
896 s->last_rcnr += ((rt - s->last_hz) << 15) /
897 (1000 * ((s->rttr & 0xffff) + 1));
898 s->last_rdcr += ((rt - s->last_hz) << 15) /
899 (1000 * ((s->rttr & 0xffff) + 1));
900 s->last_hz = rt;
903 static void pxa2xx_rtc_swupdate(PXA2xxRTCState *s)
905 int64_t rt = qemu_get_clock_ms(rt_clock);
906 if (s->rtsr & (1 << 12))
907 s->last_swcr += (rt - s->last_sw) / 10;
908 s->last_sw = rt;
911 static void pxa2xx_rtc_piupdate(PXA2xxRTCState *s)
913 int64_t rt = qemu_get_clock_ms(rt_clock);
914 if (s->rtsr & (1 << 15))
915 s->last_swcr += rt - s->last_pi;
916 s->last_pi = rt;
919 static inline void pxa2xx_rtc_alarm_update(PXA2xxRTCState *s,
920 uint32_t rtsr)
922 if ((rtsr & (1 << 2)) && !(rtsr & (1 << 0)))
923 qemu_mod_timer(s->rtc_hz, s->last_hz +
924 (((s->rtar - s->last_rcnr) * 1000 *
925 ((s->rttr & 0xffff) + 1)) >> 15));
926 else
927 qemu_del_timer(s->rtc_hz);
929 if ((rtsr & (1 << 5)) && !(rtsr & (1 << 4)))
930 qemu_mod_timer(s->rtc_rdal1, s->last_hz +
931 (((s->rdar1 - s->last_rdcr) * 1000 *
932 ((s->rttr & 0xffff) + 1)) >> 15)); /* TODO: fixup */
933 else
934 qemu_del_timer(s->rtc_rdal1);
936 if ((rtsr & (1 << 7)) && !(rtsr & (1 << 6)))
937 qemu_mod_timer(s->rtc_rdal2, s->last_hz +
938 (((s->rdar2 - s->last_rdcr) * 1000 *
939 ((s->rttr & 0xffff) + 1)) >> 15)); /* TODO: fixup */
940 else
941 qemu_del_timer(s->rtc_rdal2);
943 if ((rtsr & 0x1200) == 0x1200 && !(rtsr & (1 << 8)))
944 qemu_mod_timer(s->rtc_swal1, s->last_sw +
945 (s->swar1 - s->last_swcr) * 10); /* TODO: fixup */
946 else
947 qemu_del_timer(s->rtc_swal1);
949 if ((rtsr & 0x1800) == 0x1800 && !(rtsr & (1 << 10)))
950 qemu_mod_timer(s->rtc_swal2, s->last_sw +
951 (s->swar2 - s->last_swcr) * 10); /* TODO: fixup */
952 else
953 qemu_del_timer(s->rtc_swal2);
955 if ((rtsr & 0xc000) == 0xc000 && !(rtsr & (1 << 13)))
956 qemu_mod_timer(s->rtc_pi, s->last_pi +
957 (s->piar & 0xffff) - s->last_rtcpicr);
958 else
959 qemu_del_timer(s->rtc_pi);
962 static inline void pxa2xx_rtc_hz_tick(void *opaque)
964 PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
965 s->rtsr |= (1 << 0);
966 pxa2xx_rtc_alarm_update(s, s->rtsr);
967 pxa2xx_rtc_int_update(s);
970 static inline void pxa2xx_rtc_rdal1_tick(void *opaque)
972 PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
973 s->rtsr |= (1 << 4);
974 pxa2xx_rtc_alarm_update(s, s->rtsr);
975 pxa2xx_rtc_int_update(s);
978 static inline void pxa2xx_rtc_rdal2_tick(void *opaque)
980 PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
981 s->rtsr |= (1 << 6);
982 pxa2xx_rtc_alarm_update(s, s->rtsr);
983 pxa2xx_rtc_int_update(s);
986 static inline void pxa2xx_rtc_swal1_tick(void *opaque)
988 PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
989 s->rtsr |= (1 << 8);
990 pxa2xx_rtc_alarm_update(s, s->rtsr);
991 pxa2xx_rtc_int_update(s);
994 static inline void pxa2xx_rtc_swal2_tick(void *opaque)
996 PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
997 s->rtsr |= (1 << 10);
998 pxa2xx_rtc_alarm_update(s, s->rtsr);
999 pxa2xx_rtc_int_update(s);
1002 static inline void pxa2xx_rtc_pi_tick(void *opaque)
1004 PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
1005 s->rtsr |= (1 << 13);
1006 pxa2xx_rtc_piupdate(s);
1007 s->last_rtcpicr = 0;
1008 pxa2xx_rtc_alarm_update(s, s->rtsr);
1009 pxa2xx_rtc_int_update(s);
1012 static uint32_t pxa2xx_rtc_read(void *opaque, target_phys_addr_t addr)
1014 PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
1016 switch (addr) {
1017 case RTTR:
1018 return s->rttr;
1019 case RTSR:
1020 return s->rtsr;
1021 case RTAR:
1022 return s->rtar;
1023 case RDAR1:
1024 return s->rdar1;
1025 case RDAR2:
1026 return s->rdar2;
1027 case RYAR1:
1028 return s->ryar1;
1029 case RYAR2:
1030 return s->ryar2;
1031 case SWAR1:
1032 return s->swar1;
1033 case SWAR2:
1034 return s->swar2;
1035 case PIAR:
1036 return s->piar;
1037 case RCNR:
1038 return s->last_rcnr + ((qemu_get_clock_ms(rt_clock) - s->last_hz) << 15) /
1039 (1000 * ((s->rttr & 0xffff) + 1));
1040 case RDCR:
1041 return s->last_rdcr + ((qemu_get_clock_ms(rt_clock) - s->last_hz) << 15) /
1042 (1000 * ((s->rttr & 0xffff) + 1));
1043 case RYCR:
1044 return s->last_rycr;
1045 case SWCR:
1046 if (s->rtsr & (1 << 12))
1047 return s->last_swcr + (qemu_get_clock_ms(rt_clock) - s->last_sw) / 10;
1048 else
1049 return s->last_swcr;
1050 default:
1051 printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
1052 break;
1054 return 0;
1057 static void pxa2xx_rtc_write(void *opaque, target_phys_addr_t addr,
1058 uint32_t value)
1060 PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
1062 switch (addr) {
1063 case RTTR:
1064 if (!(s->rttr & (1 << 31))) {
1065 pxa2xx_rtc_hzupdate(s);
1066 s->rttr = value;
1067 pxa2xx_rtc_alarm_update(s, s->rtsr);
1069 break;
1071 case RTSR:
1072 if ((s->rtsr ^ value) & (1 << 15))
1073 pxa2xx_rtc_piupdate(s);
1075 if ((s->rtsr ^ value) & (1 << 12))
1076 pxa2xx_rtc_swupdate(s);
1078 if (((s->rtsr ^ value) & 0x4aac) | (value & ~0xdaac))
1079 pxa2xx_rtc_alarm_update(s, value);
1081 s->rtsr = (value & 0xdaac) | (s->rtsr & ~(value & ~0xdaac));
1082 pxa2xx_rtc_int_update(s);
1083 break;
1085 case RTAR:
1086 s->rtar = value;
1087 pxa2xx_rtc_alarm_update(s, s->rtsr);
1088 break;
1090 case RDAR1:
1091 s->rdar1 = value;
1092 pxa2xx_rtc_alarm_update(s, s->rtsr);
1093 break;
1095 case RDAR2:
1096 s->rdar2 = value;
1097 pxa2xx_rtc_alarm_update(s, s->rtsr);
1098 break;
1100 case RYAR1:
1101 s->ryar1 = value;
1102 pxa2xx_rtc_alarm_update(s, s->rtsr);
1103 break;
1105 case RYAR2:
1106 s->ryar2 = value;
1107 pxa2xx_rtc_alarm_update(s, s->rtsr);
1108 break;
1110 case SWAR1:
1111 pxa2xx_rtc_swupdate(s);
1112 s->swar1 = value;
1113 s->last_swcr = 0;
1114 pxa2xx_rtc_alarm_update(s, s->rtsr);
1115 break;
1117 case SWAR2:
1118 s->swar2 = value;
1119 pxa2xx_rtc_alarm_update(s, s->rtsr);
1120 break;
1122 case PIAR:
1123 s->piar = value;
1124 pxa2xx_rtc_alarm_update(s, s->rtsr);
1125 break;
1127 case RCNR:
1128 pxa2xx_rtc_hzupdate(s);
1129 s->last_rcnr = value;
1130 pxa2xx_rtc_alarm_update(s, s->rtsr);
1131 break;
1133 case RDCR:
1134 pxa2xx_rtc_hzupdate(s);
1135 s->last_rdcr = value;
1136 pxa2xx_rtc_alarm_update(s, s->rtsr);
1137 break;
1139 case RYCR:
1140 s->last_rycr = value;
1141 break;
1143 case SWCR:
1144 pxa2xx_rtc_swupdate(s);
1145 s->last_swcr = value;
1146 pxa2xx_rtc_alarm_update(s, s->rtsr);
1147 break;
1149 case RTCPICR:
1150 pxa2xx_rtc_piupdate(s);
1151 s->last_rtcpicr = value & 0xffff;
1152 pxa2xx_rtc_alarm_update(s, s->rtsr);
1153 break;
1155 default:
1156 printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
1160 static CPUReadMemoryFunc * const pxa2xx_rtc_readfn[] = {
1161 pxa2xx_rtc_read,
1162 pxa2xx_rtc_read,
1163 pxa2xx_rtc_read,
1166 static CPUWriteMemoryFunc * const pxa2xx_rtc_writefn[] = {
1167 pxa2xx_rtc_write,
1168 pxa2xx_rtc_write,
1169 pxa2xx_rtc_write,
1172 static int pxa2xx_rtc_init(SysBusDevice *dev)
1174 PXA2xxRTCState *s = FROM_SYSBUS(PXA2xxRTCState, dev);
1175 struct tm tm;
1176 int wom;
1177 int iomemtype;
1179 s->rttr = 0x7fff;
1180 s->rtsr = 0;
1182 qemu_get_timedate(&tm, 0);
1183 wom = ((tm.tm_mday - 1) / 7) + 1;
1185 s->last_rcnr = (uint32_t) mktimegm(&tm);
1186 s->last_rdcr = (wom << 20) | ((tm.tm_wday + 1) << 17) |
1187 (tm.tm_hour << 12) | (tm.tm_min << 6) | tm.tm_sec;
1188 s->last_rycr = ((tm.tm_year + 1900) << 9) |
1189 ((tm.tm_mon + 1) << 5) | tm.tm_mday;
1190 s->last_swcr = (tm.tm_hour << 19) |
1191 (tm.tm_min << 13) | (tm.tm_sec << 7);
1192 s->last_rtcpicr = 0;
1193 s->last_hz = s->last_sw = s->last_pi = qemu_get_clock_ms(rt_clock);
1195 s->rtc_hz = qemu_new_timer_ms(rt_clock, pxa2xx_rtc_hz_tick, s);
1196 s->rtc_rdal1 = qemu_new_timer_ms(rt_clock, pxa2xx_rtc_rdal1_tick, s);
1197 s->rtc_rdal2 = qemu_new_timer_ms(rt_clock, pxa2xx_rtc_rdal2_tick, s);
1198 s->rtc_swal1 = qemu_new_timer_ms(rt_clock, pxa2xx_rtc_swal1_tick, s);
1199 s->rtc_swal2 = qemu_new_timer_ms(rt_clock, pxa2xx_rtc_swal2_tick, s);
1200 s->rtc_pi = qemu_new_timer_ms(rt_clock, pxa2xx_rtc_pi_tick, s);
1202 sysbus_init_irq(dev, &s->rtc_irq);
1204 iomemtype = cpu_register_io_memory(pxa2xx_rtc_readfn,
1205 pxa2xx_rtc_writefn, s, DEVICE_NATIVE_ENDIAN);
1206 sysbus_init_mmio(dev, 0x10000, iomemtype);
1208 return 0;
1211 static void pxa2xx_rtc_pre_save(void *opaque)
1213 PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
1215 pxa2xx_rtc_hzupdate(s);
1216 pxa2xx_rtc_piupdate(s);
1217 pxa2xx_rtc_swupdate(s);
1220 static int pxa2xx_rtc_post_load(void *opaque, int version_id)
1222 PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
1224 pxa2xx_rtc_alarm_update(s, s->rtsr);
1226 return 0;
1229 static const VMStateDescription vmstate_pxa2xx_rtc_regs = {
1230 .name = "pxa2xx_rtc",
1231 .version_id = 0,
1232 .minimum_version_id = 0,
1233 .minimum_version_id_old = 0,
1234 .pre_save = pxa2xx_rtc_pre_save,
1235 .post_load = pxa2xx_rtc_post_load,
1236 .fields = (VMStateField[]) {
1237 VMSTATE_UINT32(rttr, PXA2xxRTCState),
1238 VMSTATE_UINT32(rtsr, PXA2xxRTCState),
1239 VMSTATE_UINT32(rtar, PXA2xxRTCState),
1240 VMSTATE_UINT32(rdar1, PXA2xxRTCState),
1241 VMSTATE_UINT32(rdar2, PXA2xxRTCState),
1242 VMSTATE_UINT32(ryar1, PXA2xxRTCState),
1243 VMSTATE_UINT32(ryar2, PXA2xxRTCState),
1244 VMSTATE_UINT32(swar1, PXA2xxRTCState),
1245 VMSTATE_UINT32(swar2, PXA2xxRTCState),
1246 VMSTATE_UINT32(piar, PXA2xxRTCState),
1247 VMSTATE_UINT32(last_rcnr, PXA2xxRTCState),
1248 VMSTATE_UINT32(last_rdcr, PXA2xxRTCState),
1249 VMSTATE_UINT32(last_rycr, PXA2xxRTCState),
1250 VMSTATE_UINT32(last_swcr, PXA2xxRTCState),
1251 VMSTATE_UINT32(last_rtcpicr, PXA2xxRTCState),
1252 VMSTATE_INT64(last_hz, PXA2xxRTCState),
1253 VMSTATE_INT64(last_sw, PXA2xxRTCState),
1254 VMSTATE_INT64(last_pi, PXA2xxRTCState),
1255 VMSTATE_END_OF_LIST(),
1259 static SysBusDeviceInfo pxa2xx_rtc_sysbus_info = {
1260 .init = pxa2xx_rtc_init,
1261 .qdev.name = "pxa2xx_rtc",
1262 .qdev.desc = "PXA2xx RTC Controller",
1263 .qdev.size = sizeof(PXA2xxRTCState),
1264 .qdev.vmsd = &vmstate_pxa2xx_rtc_regs,
1267 /* I2C Interface */
1268 typedef struct {
1269 i2c_slave i2c;
1270 PXA2xxI2CState *host;
1271 } PXA2xxI2CSlaveState;
1273 struct PXA2xxI2CState {
1274 SysBusDevice busdev;
1275 PXA2xxI2CSlaveState *slave;
1276 i2c_bus *bus;
1277 qemu_irq irq;
1278 uint32_t offset;
1279 uint32_t region_size;
1281 uint16_t control;
1282 uint16_t status;
1283 uint8_t ibmr;
1284 uint8_t data;
1287 #define IBMR 0x80 /* I2C Bus Monitor register */
1288 #define IDBR 0x88 /* I2C Data Buffer register */
1289 #define ICR 0x90 /* I2C Control register */
1290 #define ISR 0x98 /* I2C Status register */
1291 #define ISAR 0xa0 /* I2C Slave Address register */
1293 static void pxa2xx_i2c_update(PXA2xxI2CState *s)
1295 uint16_t level = 0;
1296 level |= s->status & s->control & (1 << 10); /* BED */
1297 level |= (s->status & (1 << 7)) && (s->control & (1 << 9)); /* IRF */
1298 level |= (s->status & (1 << 6)) && (s->control & (1 << 8)); /* ITE */
1299 level |= s->status & (1 << 9); /* SAD */
1300 qemu_set_irq(s->irq, !!level);
1303 /* These are only stubs now. */
1304 static void pxa2xx_i2c_event(i2c_slave *i2c, enum i2c_event event)
1306 PXA2xxI2CSlaveState *slave = FROM_I2C_SLAVE(PXA2xxI2CSlaveState, i2c);
1307 PXA2xxI2CState *s = slave->host;
1309 switch (event) {
1310 case I2C_START_SEND:
1311 s->status |= (1 << 9); /* set SAD */
1312 s->status &= ~(1 << 0); /* clear RWM */
1313 break;
1314 case I2C_START_RECV:
1315 s->status |= (1 << 9); /* set SAD */
1316 s->status |= 1 << 0; /* set RWM */
1317 break;
1318 case I2C_FINISH:
1319 s->status |= (1 << 4); /* set SSD */
1320 break;
1321 case I2C_NACK:
1322 s->status |= 1 << 1; /* set ACKNAK */
1323 break;
1325 pxa2xx_i2c_update(s);
1328 static int pxa2xx_i2c_rx(i2c_slave *i2c)
1330 PXA2xxI2CSlaveState *slave = FROM_I2C_SLAVE(PXA2xxI2CSlaveState, i2c);
1331 PXA2xxI2CState *s = slave->host;
1332 if ((s->control & (1 << 14)) || !(s->control & (1 << 6)))
1333 return 0;
1335 if (s->status & (1 << 0)) { /* RWM */
1336 s->status |= 1 << 6; /* set ITE */
1338 pxa2xx_i2c_update(s);
1340 return s->data;
1343 static int pxa2xx_i2c_tx(i2c_slave *i2c, uint8_t data)
1345 PXA2xxI2CSlaveState *slave = FROM_I2C_SLAVE(PXA2xxI2CSlaveState, i2c);
1346 PXA2xxI2CState *s = slave->host;
1347 if ((s->control & (1 << 14)) || !(s->control & (1 << 6)))
1348 return 1;
1350 if (!(s->status & (1 << 0))) { /* RWM */
1351 s->status |= 1 << 7; /* set IRF */
1352 s->data = data;
1354 pxa2xx_i2c_update(s);
1356 return 1;
1359 static uint32_t pxa2xx_i2c_read(void *opaque, target_phys_addr_t addr)
1361 PXA2xxI2CState *s = (PXA2xxI2CState *) opaque;
1363 addr -= s->offset;
1364 switch (addr) {
1365 case ICR:
1366 return s->control;
1367 case ISR:
1368 return s->status | (i2c_bus_busy(s->bus) << 2);
1369 case ISAR:
1370 return s->slave->i2c.address;
1371 case IDBR:
1372 return s->data;
1373 case IBMR:
1374 if (s->status & (1 << 2))
1375 s->ibmr ^= 3; /* Fake SCL and SDA pin changes */
1376 else
1377 s->ibmr = 0;
1378 return s->ibmr;
1379 default:
1380 printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
1381 break;
1383 return 0;
1386 static void pxa2xx_i2c_write(void *opaque, target_phys_addr_t addr,
1387 uint32_t value)
1389 PXA2xxI2CState *s = (PXA2xxI2CState *) opaque;
1390 int ack;
1392 addr -= s->offset;
1393 switch (addr) {
1394 case ICR:
1395 s->control = value & 0xfff7;
1396 if ((value & (1 << 3)) && (value & (1 << 6))) { /* TB and IUE */
1397 /* TODO: slave mode */
1398 if (value & (1 << 0)) { /* START condition */
1399 if (s->data & 1)
1400 s->status |= 1 << 0; /* set RWM */
1401 else
1402 s->status &= ~(1 << 0); /* clear RWM */
1403 ack = !i2c_start_transfer(s->bus, s->data >> 1, s->data & 1);
1404 } else {
1405 if (s->status & (1 << 0)) { /* RWM */
1406 s->data = i2c_recv(s->bus);
1407 if (value & (1 << 2)) /* ACKNAK */
1408 i2c_nack(s->bus);
1409 ack = 1;
1410 } else
1411 ack = !i2c_send(s->bus, s->data);
1414 if (value & (1 << 1)) /* STOP condition */
1415 i2c_end_transfer(s->bus);
1417 if (ack) {
1418 if (value & (1 << 0)) /* START condition */
1419 s->status |= 1 << 6; /* set ITE */
1420 else
1421 if (s->status & (1 << 0)) /* RWM */
1422 s->status |= 1 << 7; /* set IRF */
1423 else
1424 s->status |= 1 << 6; /* set ITE */
1425 s->status &= ~(1 << 1); /* clear ACKNAK */
1426 } else {
1427 s->status |= 1 << 6; /* set ITE */
1428 s->status |= 1 << 10; /* set BED */
1429 s->status |= 1 << 1; /* set ACKNAK */
1432 if (!(value & (1 << 3)) && (value & (1 << 6))) /* !TB and IUE */
1433 if (value & (1 << 4)) /* MA */
1434 i2c_end_transfer(s->bus);
1435 pxa2xx_i2c_update(s);
1436 break;
1438 case ISR:
1439 s->status &= ~(value & 0x07f0);
1440 pxa2xx_i2c_update(s);
1441 break;
1443 case ISAR:
1444 i2c_set_slave_address(&s->slave->i2c, value & 0x7f);
1445 break;
1447 case IDBR:
1448 s->data = value & 0xff;
1449 break;
1451 default:
1452 printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
1456 static CPUReadMemoryFunc * const pxa2xx_i2c_readfn[] = {
1457 pxa2xx_i2c_read,
1458 pxa2xx_i2c_read,
1459 pxa2xx_i2c_read,
1462 static CPUWriteMemoryFunc * const pxa2xx_i2c_writefn[] = {
1463 pxa2xx_i2c_write,
1464 pxa2xx_i2c_write,
1465 pxa2xx_i2c_write,
1468 static const VMStateDescription vmstate_pxa2xx_i2c_slave = {
1469 .name = "pxa2xx_i2c_slave",
1470 .version_id = 1,
1471 .minimum_version_id = 1,
1472 .minimum_version_id_old = 1,
1473 .fields = (VMStateField []) {
1474 VMSTATE_I2C_SLAVE(i2c, PXA2xxI2CSlaveState),
1475 VMSTATE_END_OF_LIST()
1479 static const VMStateDescription vmstate_pxa2xx_i2c = {
1480 .name = "pxa2xx_i2c",
1481 .version_id = 1,
1482 .minimum_version_id = 1,
1483 .minimum_version_id_old = 1,
1484 .fields = (VMStateField []) {
1485 VMSTATE_UINT16(control, PXA2xxI2CState),
1486 VMSTATE_UINT16(status, PXA2xxI2CState),
1487 VMSTATE_UINT8(ibmr, PXA2xxI2CState),
1488 VMSTATE_UINT8(data, PXA2xxI2CState),
1489 VMSTATE_STRUCT_POINTER(slave, PXA2xxI2CState,
1490 vmstate_pxa2xx_i2c_slave, PXA2xxI2CSlaveState *),
1491 VMSTATE_END_OF_LIST()
1495 static int pxa2xx_i2c_slave_init(i2c_slave *i2c)
1497 /* Nothing to do. */
1498 return 0;
1501 static I2CSlaveInfo pxa2xx_i2c_slave_info = {
1502 .qdev.name = "pxa2xx-i2c-slave",
1503 .qdev.size = sizeof(PXA2xxI2CSlaveState),
1504 .init = pxa2xx_i2c_slave_init,
1505 .event = pxa2xx_i2c_event,
1506 .recv = pxa2xx_i2c_rx,
1507 .send = pxa2xx_i2c_tx
1510 PXA2xxI2CState *pxa2xx_i2c_init(target_phys_addr_t base,
1511 qemu_irq irq, uint32_t region_size)
1513 DeviceState *dev;
1514 SysBusDevice *i2c_dev;
1515 PXA2xxI2CState *s;
1517 i2c_dev = sysbus_from_qdev(qdev_create(NULL, "pxa2xx_i2c"));
1518 qdev_prop_set_uint32(&i2c_dev->qdev, "size", region_size + 1);
1519 qdev_prop_set_uint32(&i2c_dev->qdev, "offset",
1520 base - (base & (~region_size) & TARGET_PAGE_MASK));
1522 qdev_init_nofail(&i2c_dev->qdev);
1524 sysbus_mmio_map(i2c_dev, 0, base & ~region_size);
1525 sysbus_connect_irq(i2c_dev, 0, irq);
1527 s = FROM_SYSBUS(PXA2xxI2CState, i2c_dev);
1528 /* FIXME: Should the slave device really be on a separate bus? */
1529 dev = i2c_create_slave(i2c_init_bus(NULL, "dummy"), "pxa2xx-i2c-slave", 0);
1530 s->slave = FROM_I2C_SLAVE(PXA2xxI2CSlaveState, I2C_SLAVE_FROM_QDEV(dev));
1531 s->slave->host = s;
1533 return s;
1536 static int pxa2xx_i2c_initfn(SysBusDevice *dev)
1538 PXA2xxI2CState *s = FROM_SYSBUS(PXA2xxI2CState, dev);
1539 int iomemtype;
1541 s->bus = i2c_init_bus(&dev->qdev, "i2c");
1543 iomemtype = cpu_register_io_memory(pxa2xx_i2c_readfn,
1544 pxa2xx_i2c_writefn, s, DEVICE_NATIVE_ENDIAN);
1545 sysbus_init_mmio(dev, s->region_size, iomemtype);
1546 sysbus_init_irq(dev, &s->irq);
1548 return 0;
1551 i2c_bus *pxa2xx_i2c_bus(PXA2xxI2CState *s)
1553 return s->bus;
1556 static SysBusDeviceInfo pxa2xx_i2c_info = {
1557 .init = pxa2xx_i2c_initfn,
1558 .qdev.name = "pxa2xx_i2c",
1559 .qdev.desc = "PXA2xx I2C Bus Controller",
1560 .qdev.size = sizeof(PXA2xxI2CState),
1561 .qdev.vmsd = &vmstate_pxa2xx_i2c,
1562 .qdev.props = (Property[]) {
1563 DEFINE_PROP_UINT32("size", PXA2xxI2CState, region_size, 0x10000),
1564 DEFINE_PROP_UINT32("offset", PXA2xxI2CState, offset, 0),
1565 DEFINE_PROP_END_OF_LIST(),
1569 /* PXA Inter-IC Sound Controller */
1570 static void pxa2xx_i2s_reset(PXA2xxI2SState *i2s)
1572 i2s->rx_len = 0;
1573 i2s->tx_len = 0;
1574 i2s->fifo_len = 0;
1575 i2s->clk = 0x1a;
1576 i2s->control[0] = 0x00;
1577 i2s->control[1] = 0x00;
1578 i2s->status = 0x00;
1579 i2s->mask = 0x00;
1582 #define SACR_TFTH(val) ((val >> 8) & 0xf)
1583 #define SACR_RFTH(val) ((val >> 12) & 0xf)
1584 #define SACR_DREC(val) (val & (1 << 3))
1585 #define SACR_DPRL(val) (val & (1 << 4))
1587 static inline void pxa2xx_i2s_update(PXA2xxI2SState *i2s)
1589 int rfs, tfs;
1590 rfs = SACR_RFTH(i2s->control[0]) < i2s->rx_len &&
1591 !SACR_DREC(i2s->control[1]);
1592 tfs = (i2s->tx_len || i2s->fifo_len < SACR_TFTH(i2s->control[0])) &&
1593 i2s->enable && !SACR_DPRL(i2s->control[1]);
1595 qemu_set_irq(i2s->rx_dma, rfs);
1596 qemu_set_irq(i2s->tx_dma, tfs);
1598 i2s->status &= 0xe0;
1599 if (i2s->fifo_len < 16 || !i2s->enable)
1600 i2s->status |= 1 << 0; /* TNF */
1601 if (i2s->rx_len)
1602 i2s->status |= 1 << 1; /* RNE */
1603 if (i2s->enable)
1604 i2s->status |= 1 << 2; /* BSY */
1605 if (tfs)
1606 i2s->status |= 1 << 3; /* TFS */
1607 if (rfs)
1608 i2s->status |= 1 << 4; /* RFS */
1609 if (!(i2s->tx_len && i2s->enable))
1610 i2s->status |= i2s->fifo_len << 8; /* TFL */
1611 i2s->status |= MAX(i2s->rx_len, 0xf) << 12; /* RFL */
1613 qemu_set_irq(i2s->irq, i2s->status & i2s->mask);
1616 #define SACR0 0x00 /* Serial Audio Global Control register */
1617 #define SACR1 0x04 /* Serial Audio I2S/MSB-Justified Control register */
1618 #define SASR0 0x0c /* Serial Audio Interface and FIFO Status register */
1619 #define SAIMR 0x14 /* Serial Audio Interrupt Mask register */
1620 #define SAICR 0x18 /* Serial Audio Interrupt Clear register */
1621 #define SADIV 0x60 /* Serial Audio Clock Divider register */
1622 #define SADR 0x80 /* Serial Audio Data register */
1624 static uint32_t pxa2xx_i2s_read(void *opaque, target_phys_addr_t addr)
1626 PXA2xxI2SState *s = (PXA2xxI2SState *) opaque;
1628 switch (addr) {
1629 case SACR0:
1630 return s->control[0];
1631 case SACR1:
1632 return s->control[1];
1633 case SASR0:
1634 return s->status;
1635 case SAIMR:
1636 return s->mask;
1637 case SAICR:
1638 return 0;
1639 case SADIV:
1640 return s->clk;
1641 case SADR:
1642 if (s->rx_len > 0) {
1643 s->rx_len --;
1644 pxa2xx_i2s_update(s);
1645 return s->codec_in(s->opaque);
1647 return 0;
1648 default:
1649 printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
1650 break;
1652 return 0;
1655 static void pxa2xx_i2s_write(void *opaque, target_phys_addr_t addr,
1656 uint32_t value)
1658 PXA2xxI2SState *s = (PXA2xxI2SState *) opaque;
1659 uint32_t *sample;
1661 switch (addr) {
1662 case SACR0:
1663 if (value & (1 << 3)) /* RST */
1664 pxa2xx_i2s_reset(s);
1665 s->control[0] = value & 0xff3d;
1666 if (!s->enable && (value & 1) && s->tx_len) { /* ENB */
1667 for (sample = s->fifo; s->fifo_len > 0; s->fifo_len --, sample ++)
1668 s->codec_out(s->opaque, *sample);
1669 s->status &= ~(1 << 7); /* I2SOFF */
1671 if (value & (1 << 4)) /* EFWR */
1672 printf("%s: Attempt to use special function\n", __FUNCTION__);
1673 s->enable = (value & 9) == 1; /* ENB && !RST*/
1674 pxa2xx_i2s_update(s);
1675 break;
1676 case SACR1:
1677 s->control[1] = value & 0x0039;
1678 if (value & (1 << 5)) /* ENLBF */
1679 printf("%s: Attempt to use loopback function\n", __FUNCTION__);
1680 if (value & (1 << 4)) /* DPRL */
1681 s->fifo_len = 0;
1682 pxa2xx_i2s_update(s);
1683 break;
1684 case SAIMR:
1685 s->mask = value & 0x0078;
1686 pxa2xx_i2s_update(s);
1687 break;
1688 case SAICR:
1689 s->status &= ~(value & (3 << 5));
1690 pxa2xx_i2s_update(s);
1691 break;
1692 case SADIV:
1693 s->clk = value & 0x007f;
1694 break;
1695 case SADR:
1696 if (s->tx_len && s->enable) {
1697 s->tx_len --;
1698 pxa2xx_i2s_update(s);
1699 s->codec_out(s->opaque, value);
1700 } else if (s->fifo_len < 16) {
1701 s->fifo[s->fifo_len ++] = value;
1702 pxa2xx_i2s_update(s);
1704 break;
1705 default:
1706 printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
1710 static CPUReadMemoryFunc * const pxa2xx_i2s_readfn[] = {
1711 pxa2xx_i2s_read,
1712 pxa2xx_i2s_read,
1713 pxa2xx_i2s_read,
1716 static CPUWriteMemoryFunc * const pxa2xx_i2s_writefn[] = {
1717 pxa2xx_i2s_write,
1718 pxa2xx_i2s_write,
1719 pxa2xx_i2s_write,
1722 static const VMStateDescription vmstate_pxa2xx_i2s = {
1723 .name = "pxa2xx_i2s",
1724 .version_id = 0,
1725 .minimum_version_id = 0,
1726 .minimum_version_id_old = 0,
1727 .fields = (VMStateField[]) {
1728 VMSTATE_UINT32_ARRAY(control, PXA2xxI2SState, 2),
1729 VMSTATE_UINT32(status, PXA2xxI2SState),
1730 VMSTATE_UINT32(mask, PXA2xxI2SState),
1731 VMSTATE_UINT32(clk, PXA2xxI2SState),
1732 VMSTATE_INT32(enable, PXA2xxI2SState),
1733 VMSTATE_INT32(rx_len, PXA2xxI2SState),
1734 VMSTATE_INT32(tx_len, PXA2xxI2SState),
1735 VMSTATE_INT32(fifo_len, PXA2xxI2SState),
1736 VMSTATE_END_OF_LIST()
1740 static void pxa2xx_i2s_data_req(void *opaque, int tx, int rx)
1742 PXA2xxI2SState *s = (PXA2xxI2SState *) opaque;
1743 uint32_t *sample;
1745 /* Signal FIFO errors */
1746 if (s->enable && s->tx_len)
1747 s->status |= 1 << 5; /* TUR */
1748 if (s->enable && s->rx_len)
1749 s->status |= 1 << 6; /* ROR */
1751 /* Should be tx - MIN(tx, s->fifo_len) but we don't really need to
1752 * handle the cases where it makes a difference. */
1753 s->tx_len = tx - s->fifo_len;
1754 s->rx_len = rx;
1755 /* Note that is s->codec_out wasn't set, we wouldn't get called. */
1756 if (s->enable)
1757 for (sample = s->fifo; s->fifo_len; s->fifo_len --, sample ++)
1758 s->codec_out(s->opaque, *sample);
1759 pxa2xx_i2s_update(s);
1762 static PXA2xxI2SState *pxa2xx_i2s_init(target_phys_addr_t base,
1763 qemu_irq irq, qemu_irq rx_dma, qemu_irq tx_dma)
1765 int iomemtype;
1766 PXA2xxI2SState *s = (PXA2xxI2SState *)
1767 qemu_mallocz(sizeof(PXA2xxI2SState));
1769 s->irq = irq;
1770 s->rx_dma = rx_dma;
1771 s->tx_dma = tx_dma;
1772 s->data_req = pxa2xx_i2s_data_req;
1774 pxa2xx_i2s_reset(s);
1776 iomemtype = cpu_register_io_memory(pxa2xx_i2s_readfn,
1777 pxa2xx_i2s_writefn, s, DEVICE_NATIVE_ENDIAN);
1778 cpu_register_physical_memory(base, 0x100000, iomemtype);
1780 vmstate_register(NULL, base, &vmstate_pxa2xx_i2s, s);
1782 return s;
1785 /* PXA Fast Infra-red Communications Port */
1786 struct PXA2xxFIrState {
1787 qemu_irq irq;
1788 qemu_irq rx_dma;
1789 qemu_irq tx_dma;
1790 int enable;
1791 CharDriverState *chr;
1793 uint8_t control[3];
1794 uint8_t status[2];
1796 int rx_len;
1797 int rx_start;
1798 uint8_t rx_fifo[64];
1801 static void pxa2xx_fir_update_handlers(PXA2xxFIrState *s);
1803 static void pxa2xx_fir_reset(PXA2xxFIrState *s)
1805 s->control[0] = 0x00;
1806 s->control[1] = 0x00;
1807 s->control[2] = 0x00;
1808 s->status[0] = 0x00;
1809 s->status[1] = 0x00;
1810 s->enable = 0;
1813 static inline void pxa2xx_fir_update(PXA2xxFIrState *s)
1815 static const int tresh[4] = { 8, 16, 32, 0 };
1816 int intr = 0;
1817 if ((s->control[0] & (1 << 4)) && /* RXE */
1818 s->rx_len >= tresh[s->control[2] & 3]) /* TRIG */
1819 s->status[0] |= 1 << 4; /* RFS */
1820 else
1821 s->status[0] &= ~(1 << 4); /* RFS */
1822 if (s->control[0] & (1 << 3)) /* TXE */
1823 s->status[0] |= 1 << 3; /* TFS */
1824 else
1825 s->status[0] &= ~(1 << 3); /* TFS */
1826 if (s->rx_len)
1827 s->status[1] |= 1 << 2; /* RNE */
1828 else
1829 s->status[1] &= ~(1 << 2); /* RNE */
1830 if (s->control[0] & (1 << 4)) /* RXE */
1831 s->status[1] |= 1 << 0; /* RSY */
1832 else
1833 s->status[1] &= ~(1 << 0); /* RSY */
1835 intr |= (s->control[0] & (1 << 5)) && /* RIE */
1836 (s->status[0] & (1 << 4)); /* RFS */
1837 intr |= (s->control[0] & (1 << 6)) && /* TIE */
1838 (s->status[0] & (1 << 3)); /* TFS */
1839 intr |= (s->control[2] & (1 << 4)) && /* TRAIL */
1840 (s->status[0] & (1 << 6)); /* EOC */
1841 intr |= (s->control[0] & (1 << 2)) && /* TUS */
1842 (s->status[0] & (1 << 1)); /* TUR */
1843 intr |= s->status[0] & 0x25; /* FRE, RAB, EIF */
1845 qemu_set_irq(s->rx_dma, (s->status[0] >> 4) & 1);
1846 qemu_set_irq(s->tx_dma, (s->status[0] >> 3) & 1);
1848 qemu_set_irq(s->irq, intr && s->enable);
1851 #define ICCR0 0x00 /* FICP Control register 0 */
1852 #define ICCR1 0x04 /* FICP Control register 1 */
1853 #define ICCR2 0x08 /* FICP Control register 2 */
1854 #define ICDR 0x0c /* FICP Data register */
1855 #define ICSR0 0x14 /* FICP Status register 0 */
1856 #define ICSR1 0x18 /* FICP Status register 1 */
1857 #define ICFOR 0x1c /* FICP FIFO Occupancy Status register */
1859 static uint32_t pxa2xx_fir_read(void *opaque, target_phys_addr_t addr)
1861 PXA2xxFIrState *s = (PXA2xxFIrState *) opaque;
1862 uint8_t ret;
1864 switch (addr) {
1865 case ICCR0:
1866 return s->control[0];
1867 case ICCR1:
1868 return s->control[1];
1869 case ICCR2:
1870 return s->control[2];
1871 case ICDR:
1872 s->status[0] &= ~0x01;
1873 s->status[1] &= ~0x72;
1874 if (s->rx_len) {
1875 s->rx_len --;
1876 ret = s->rx_fifo[s->rx_start ++];
1877 s->rx_start &= 63;
1878 pxa2xx_fir_update_handlers(s);
1879 pxa2xx_fir_update(s);
1880 return ret;
1882 printf("%s: Rx FIFO underrun.\n", __FUNCTION__);
1883 break;
1884 case ICSR0:
1885 return s->status[0];
1886 case ICSR1:
1887 return s->status[1] | (1 << 3); /* TNF */
1888 case ICFOR:
1889 return s->rx_len;
1890 default:
1891 printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
1892 break;
1894 return 0;
1897 static void pxa2xx_fir_write(void *opaque, target_phys_addr_t addr,
1898 uint32_t value)
1900 PXA2xxFIrState *s = (PXA2xxFIrState *) opaque;
1901 uint8_t ch;
1903 switch (addr) {
1904 case ICCR0:
1905 s->control[0] = value;
1906 if (!(value & (1 << 4))) /* RXE */
1907 s->rx_len = s->rx_start = 0;
1908 if (!(value & (1 << 3))) { /* TXE */
1909 /* Nop */
1911 s->enable = value & 1; /* ITR */
1912 if (!s->enable)
1913 s->status[0] = 0;
1914 pxa2xx_fir_update_handlers(s);
1915 pxa2xx_fir_update(s);
1916 break;
1917 case ICCR1:
1918 s->control[1] = value;
1919 break;
1920 case ICCR2:
1921 s->control[2] = value & 0x3f;
1922 pxa2xx_fir_update(s);
1923 break;
1924 case ICDR:
1925 if (s->control[2] & (1 << 2)) /* TXP */
1926 ch = value;
1927 else
1928 ch = ~value;
1929 if (s->chr && s->enable && (s->control[0] & (1 << 3))) /* TXE */
1930 qemu_chr_fe_write(s->chr, &ch, 1);
1931 break;
1932 case ICSR0:
1933 s->status[0] &= ~(value & 0x66);
1934 pxa2xx_fir_update(s);
1935 break;
1936 case ICFOR:
1937 break;
1938 default:
1939 printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
1943 static CPUReadMemoryFunc * const pxa2xx_fir_readfn[] = {
1944 pxa2xx_fir_read,
1945 pxa2xx_fir_read,
1946 pxa2xx_fir_read,
1949 static CPUWriteMemoryFunc * const pxa2xx_fir_writefn[] = {
1950 pxa2xx_fir_write,
1951 pxa2xx_fir_write,
1952 pxa2xx_fir_write,
1955 static int pxa2xx_fir_can_rx(PXA2xxFIrState *s)
1957 return (64 - s->rx_len);
1960 static void pxa2xx_fir_rx(PXA2xxFIrState *s, const uint8_t *buf, int size)
1962 if (!(s->control[0] & (1 << 4))) /* RXE */
1963 return;
1965 while (size --) {
1966 s->status[1] |= 1 << 4; /* EOF */
1967 if (s->rx_len >= 64) {
1968 s->status[1] |= 1 << 6; /* ROR */
1969 break;
1972 if (s->control[2] & (1 << 3)) /* RXP */
1973 s->rx_fifo[(s->rx_start + s->rx_len ++) & 63] = *(buf ++);
1974 else
1975 s->rx_fifo[(s->rx_start + s->rx_len ++) & 63] = ~*(buf ++);
1978 pxa2xx_fir_update_handlers(s);
1979 pxa2xx_fir_update(s);
1982 static void pxa2xx_fir_rx_handler(void *opaque)
1984 PXA2xxFIrState *s = opaque;
1985 uint8_t buf[32];
1986 int size;
1988 size = pxa2xx_fir_can_rx(s);
1989 size = MIN(size, sizeof(buf));
1990 size = qemu_chr_fe_read(s->chr, buf, size);
1992 pxa2xx_fir_rx(s, buf, size);
1995 static void pxa2xx_fir_update_handlers(PXA2xxFIrState *s)
1997 if (pxa2xx_fir_can_rx(s) > 0) {
1998 qemu_chr_fe_set_handlers(s->chr, pxa2xx_fir_rx_handler, NULL, NULL, s);
1999 } else {
2000 qemu_chr_fe_set_handlers(s->chr, NULL, NULL, NULL, s);
2004 static void pxa2xx_fir_save(QEMUFile *f, void *opaque)
2006 PXA2xxFIrState *s = (PXA2xxFIrState *) opaque;
2007 int i;
2009 qemu_put_be32(f, s->enable);
2011 qemu_put_8s(f, &s->control[0]);
2012 qemu_put_8s(f, &s->control[1]);
2013 qemu_put_8s(f, &s->control[2]);
2014 qemu_put_8s(f, &s->status[0]);
2015 qemu_put_8s(f, &s->status[1]);
2017 qemu_put_byte(f, s->rx_len);
2018 for (i = 0; i < s->rx_len; i ++)
2019 qemu_put_byte(f, s->rx_fifo[(s->rx_start + i) & 63]);
2022 static int pxa2xx_fir_load(QEMUFile *f, void *opaque, int version_id)
2024 PXA2xxFIrState *s = (PXA2xxFIrState *) opaque;
2025 int i;
2027 s->enable = qemu_get_be32(f);
2029 qemu_get_8s(f, &s->control[0]);
2030 qemu_get_8s(f, &s->control[1]);
2031 qemu_get_8s(f, &s->control[2]);
2032 qemu_get_8s(f, &s->status[0]);
2033 qemu_get_8s(f, &s->status[1]);
2035 s->rx_len = qemu_get_byte(f);
2036 s->rx_start = 0;
2037 for (i = 0; i < s->rx_len; i ++)
2038 s->rx_fifo[i] = qemu_get_byte(f);
2040 pxa2xx_fir_update_handlers(s);
2042 return 0;
2045 static PXA2xxFIrState *pxa2xx_fir_init(target_phys_addr_t base,
2046 qemu_irq irq, qemu_irq rx_dma, qemu_irq tx_dma,
2047 CharDriverState *chr)
2049 int iomemtype;
2050 PXA2xxFIrState *s = (PXA2xxFIrState *)
2051 qemu_mallocz(sizeof(PXA2xxFIrState));
2053 s->irq = irq;
2054 s->rx_dma = rx_dma;
2055 s->tx_dma = tx_dma;
2056 s->chr = chr;
2058 pxa2xx_fir_reset(s);
2060 iomemtype = cpu_register_io_memory(pxa2xx_fir_readfn,
2061 pxa2xx_fir_writefn, s, DEVICE_NATIVE_ENDIAN);
2062 cpu_register_physical_memory(base, 0x1000, iomemtype);
2064 if (chr) {
2065 qemu_chr_fe_open(chr);
2068 register_savevm(NULL, "pxa2xx_fir", 0, 0, pxa2xx_fir_save,
2069 pxa2xx_fir_load, s);
2071 return s;
2074 static void pxa2xx_reset(void *opaque, int line, int level)
2076 PXA2xxState *s = (PXA2xxState *) opaque;
2078 if (level && (s->pm_regs[PCFR >> 2] & 0x10)) { /* GPR_EN */
2079 cpu_reset(s->env);
2080 /* TODO: reset peripherals */
2084 /* Initialise a PXA270 integrated chip (ARM based core). */
2085 PXA2xxState *pxa270_init(unsigned int sdram_size, const char *revision)
2087 PXA2xxState *s;
2088 int iomemtype, i;
2089 DriveInfo *dinfo;
2090 s = (PXA2xxState *) qemu_mallocz(sizeof(PXA2xxState));
2092 if (revision && strncmp(revision, "pxa27", 5)) {
2093 fprintf(stderr, "Machine requires a PXA27x processor.\n");
2094 exit(1);
2096 if (!revision)
2097 revision = "pxa270";
2099 s->env = cpu_init(revision);
2100 if (!s->env) {
2101 fprintf(stderr, "Unable to find CPU definition\n");
2102 exit(1);
2104 s->reset = qemu_allocate_irqs(pxa2xx_reset, s, 1)[0];
2106 /* SDRAM & Internal Memory Storage */
2107 cpu_register_physical_memory(PXA2XX_SDRAM_BASE,
2108 sdram_size, qemu_ram_alloc(NULL, "pxa270.sdram",
2109 sdram_size) | IO_MEM_RAM);
2110 cpu_register_physical_memory(PXA2XX_INTERNAL_BASE,
2111 0x40000, qemu_ram_alloc(NULL, "pxa270.internal",
2112 0x40000) | IO_MEM_RAM);
2114 s->pic = pxa2xx_pic_init(0x40d00000, s->env);
2116 s->dma = pxa27x_dma_init(0x40000000,
2117 qdev_get_gpio_in(s->pic, PXA2XX_PIC_DMA));
2119 sysbus_create_varargs("pxa27x-timer", 0x40a00000,
2120 qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 0),
2121 qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 1),
2122 qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 2),
2123 qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 3),
2124 qdev_get_gpio_in(s->pic, PXA27X_PIC_OST_4_11),
2125 NULL);
2127 s->gpio = pxa2xx_gpio_init(0x40e00000, s->env, s->pic, 121);
2129 dinfo = drive_get(IF_SD, 0, 0);
2130 if (!dinfo) {
2131 fprintf(stderr, "qemu: missing SecureDigital device\n");
2132 exit(1);
2134 s->mmc = pxa2xx_mmci_init(0x41100000, dinfo->bdrv,
2135 qdev_get_gpio_in(s->pic, PXA2XX_PIC_MMC),
2136 qdev_get_gpio_in(s->dma, PXA2XX_RX_RQ_MMCI),
2137 qdev_get_gpio_in(s->dma, PXA2XX_TX_RQ_MMCI));
2139 for (i = 0; pxa270_serial[i].io_base; i ++)
2140 if (serial_hds[i])
2141 #ifdef TARGET_WORDS_BIGENDIAN
2142 serial_mm_init(pxa270_serial[i].io_base, 2,
2143 qdev_get_gpio_in(s->pic, pxa270_serial[i].irqn),
2144 14857000 / 16, serial_hds[i], 1, 1);
2145 #else
2146 serial_mm_init(pxa270_serial[i].io_base, 2,
2147 qdev_get_gpio_in(s->pic, pxa270_serial[i].irqn),
2148 14857000 / 16, serial_hds[i], 1, 0);
2149 #endif
2150 else
2151 break;
2152 if (serial_hds[i])
2153 s->fir = pxa2xx_fir_init(0x40800000,
2154 qdev_get_gpio_in(s->pic, PXA2XX_PIC_ICP),
2155 qdev_get_gpio_in(s->dma, PXA2XX_RX_RQ_ICP),
2156 qdev_get_gpio_in(s->dma, PXA2XX_TX_RQ_ICP),
2157 serial_hds[i]);
2159 s->lcd = pxa2xx_lcdc_init(0x44000000,
2160 qdev_get_gpio_in(s->pic, PXA2XX_PIC_LCD));
2162 s->cm_base = 0x41300000;
2163 s->cm_regs[CCCR >> 2] = 0x02000210; /* 416.0 MHz */
2164 s->clkcfg = 0x00000009; /* Turbo mode active */
2165 iomemtype = cpu_register_io_memory(pxa2xx_cm_readfn,
2166 pxa2xx_cm_writefn, s, DEVICE_NATIVE_ENDIAN);
2167 cpu_register_physical_memory(s->cm_base, 0x1000, iomemtype);
2168 vmstate_register(NULL, 0, &vmstate_pxa2xx_cm, s);
2170 cpu_arm_set_cp_io(s->env, 14, pxa2xx_cp14_read, pxa2xx_cp14_write, s);
2172 s->mm_base = 0x48000000;
2173 s->mm_regs[MDMRS >> 2] = 0x00020002;
2174 s->mm_regs[MDREFR >> 2] = 0x03ca4000;
2175 s->mm_regs[MECR >> 2] = 0x00000001; /* Two PC Card sockets */
2176 iomemtype = cpu_register_io_memory(pxa2xx_mm_readfn,
2177 pxa2xx_mm_writefn, s, DEVICE_NATIVE_ENDIAN);
2178 cpu_register_physical_memory(s->mm_base, 0x1000, iomemtype);
2179 vmstate_register(NULL, 0, &vmstate_pxa2xx_mm, s);
2181 s->pm_base = 0x40f00000;
2182 iomemtype = cpu_register_io_memory(pxa2xx_pm_readfn,
2183 pxa2xx_pm_writefn, s, DEVICE_NATIVE_ENDIAN);
2184 cpu_register_physical_memory(s->pm_base, 0x100, iomemtype);
2185 vmstate_register(NULL, 0, &vmstate_pxa2xx_pm, s);
2187 for (i = 0; pxa27x_ssp[i].io_base; i ++);
2188 s->ssp = (SSIBus **)qemu_mallocz(sizeof(SSIBus *) * i);
2189 for (i = 0; pxa27x_ssp[i].io_base; i ++) {
2190 DeviceState *dev;
2191 dev = sysbus_create_simple("pxa2xx-ssp", pxa27x_ssp[i].io_base,
2192 qdev_get_gpio_in(s->pic, pxa27x_ssp[i].irqn));
2193 s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
2196 if (usb_enabled) {
2197 sysbus_create_simple("sysbus-ohci", 0x4c000000,
2198 qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
2201 s->pcmcia[0] = pxa2xx_pcmcia_init(0x20000000);
2202 s->pcmcia[1] = pxa2xx_pcmcia_init(0x30000000);
2204 sysbus_create_simple("pxa2xx_rtc", 0x40900000,
2205 qdev_get_gpio_in(s->pic, PXA2XX_PIC_RTCALARM));
2207 s->i2c[0] = pxa2xx_i2c_init(0x40301600,
2208 qdev_get_gpio_in(s->pic, PXA2XX_PIC_I2C), 0xffff);
2209 s->i2c[1] = pxa2xx_i2c_init(0x40f00100,
2210 qdev_get_gpio_in(s->pic, PXA2XX_PIC_PWRI2C), 0xff);
2212 s->i2s = pxa2xx_i2s_init(0x40400000,
2213 qdev_get_gpio_in(s->pic, PXA2XX_PIC_I2S),
2214 qdev_get_gpio_in(s->dma, PXA2XX_RX_RQ_I2S),
2215 qdev_get_gpio_in(s->dma, PXA2XX_TX_RQ_I2S));
2217 s->kp = pxa27x_keypad_init(0x41500000,
2218 qdev_get_gpio_in(s->pic, PXA2XX_PIC_KEYPAD));
2220 /* GPIO1 resets the processor */
2221 /* The handler can be overridden by board-specific code */
2222 qdev_connect_gpio_out(s->gpio, 1, s->reset);
2223 return s;
2226 /* Initialise a PXA255 integrated chip (ARM based core). */
2227 PXA2xxState *pxa255_init(unsigned int sdram_size)
2229 PXA2xxState *s;
2230 int iomemtype, i;
2231 DriveInfo *dinfo;
2233 s = (PXA2xxState *) qemu_mallocz(sizeof(PXA2xxState));
2235 s->env = cpu_init("pxa255");
2236 if (!s->env) {
2237 fprintf(stderr, "Unable to find CPU definition\n");
2238 exit(1);
2240 s->reset = qemu_allocate_irqs(pxa2xx_reset, s, 1)[0];
2242 /* SDRAM & Internal Memory Storage */
2243 cpu_register_physical_memory(PXA2XX_SDRAM_BASE, sdram_size,
2244 qemu_ram_alloc(NULL, "pxa255.sdram",
2245 sdram_size) | IO_MEM_RAM);
2246 cpu_register_physical_memory(PXA2XX_INTERNAL_BASE, PXA2XX_INTERNAL_SIZE,
2247 qemu_ram_alloc(NULL, "pxa255.internal",
2248 PXA2XX_INTERNAL_SIZE) | IO_MEM_RAM);
2250 s->pic = pxa2xx_pic_init(0x40d00000, s->env);
2252 s->dma = pxa255_dma_init(0x40000000,
2253 qdev_get_gpio_in(s->pic, PXA2XX_PIC_DMA));
2255 sysbus_create_varargs("pxa25x-timer", 0x40a00000,
2256 qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 0),
2257 qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 1),
2258 qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 2),
2259 qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 3),
2260 NULL);
2262 s->gpio = pxa2xx_gpio_init(0x40e00000, s->env, s->pic, 85);
2264 dinfo = drive_get(IF_SD, 0, 0);
2265 if (!dinfo) {
2266 fprintf(stderr, "qemu: missing SecureDigital device\n");
2267 exit(1);
2269 s->mmc = pxa2xx_mmci_init(0x41100000, dinfo->bdrv,
2270 qdev_get_gpio_in(s->pic, PXA2XX_PIC_MMC),
2271 qdev_get_gpio_in(s->dma, PXA2XX_RX_RQ_MMCI),
2272 qdev_get_gpio_in(s->dma, PXA2XX_TX_RQ_MMCI));
2274 for (i = 0; pxa255_serial[i].io_base; i ++)
2275 if (serial_hds[i]) {
2276 #ifdef TARGET_WORDS_BIGENDIAN
2277 serial_mm_init(pxa255_serial[i].io_base, 2,
2278 qdev_get_gpio_in(s->pic, pxa255_serial[i].irqn),
2279 14745600 / 16, serial_hds[i], 1, 1);
2280 #else
2281 serial_mm_init(pxa255_serial[i].io_base, 2,
2282 qdev_get_gpio_in(s->pic, pxa255_serial[i].irqn),
2283 14745600 / 16, serial_hds[i], 1, 0);
2284 #endif
2285 } else {
2286 break;
2288 if (serial_hds[i])
2289 s->fir = pxa2xx_fir_init(0x40800000,
2290 qdev_get_gpio_in(s->pic, PXA2XX_PIC_ICP),
2291 qdev_get_gpio_in(s->dma, PXA2XX_RX_RQ_ICP),
2292 qdev_get_gpio_in(s->dma, PXA2XX_TX_RQ_ICP),
2293 serial_hds[i]);
2295 s->lcd = pxa2xx_lcdc_init(0x44000000,
2296 qdev_get_gpio_in(s->pic, PXA2XX_PIC_LCD));
2298 s->cm_base = 0x41300000;
2299 s->cm_regs[CCCR >> 2] = 0x02000210; /* 416.0 MHz */
2300 s->clkcfg = 0x00000009; /* Turbo mode active */
2301 iomemtype = cpu_register_io_memory(pxa2xx_cm_readfn,
2302 pxa2xx_cm_writefn, s, DEVICE_NATIVE_ENDIAN);
2303 cpu_register_physical_memory(s->cm_base, 0x1000, iomemtype);
2304 vmstate_register(NULL, 0, &vmstate_pxa2xx_cm, s);
2306 cpu_arm_set_cp_io(s->env, 14, pxa2xx_cp14_read, pxa2xx_cp14_write, s);
2308 s->mm_base = 0x48000000;
2309 s->mm_regs[MDMRS >> 2] = 0x00020002;
2310 s->mm_regs[MDREFR >> 2] = 0x03ca4000;
2311 s->mm_regs[MECR >> 2] = 0x00000001; /* Two PC Card sockets */
2312 iomemtype = cpu_register_io_memory(pxa2xx_mm_readfn,
2313 pxa2xx_mm_writefn, s, DEVICE_NATIVE_ENDIAN);
2314 cpu_register_physical_memory(s->mm_base, 0x1000, iomemtype);
2315 vmstate_register(NULL, 0, &vmstate_pxa2xx_mm, s);
2317 s->pm_base = 0x40f00000;
2318 iomemtype = cpu_register_io_memory(pxa2xx_pm_readfn,
2319 pxa2xx_pm_writefn, s, DEVICE_NATIVE_ENDIAN);
2320 cpu_register_physical_memory(s->pm_base, 0x100, iomemtype);
2321 vmstate_register(NULL, 0, &vmstate_pxa2xx_pm, s);
2323 for (i = 0; pxa255_ssp[i].io_base; i ++);
2324 s->ssp = (SSIBus **)qemu_mallocz(sizeof(SSIBus *) * i);
2325 for (i = 0; pxa255_ssp[i].io_base; i ++) {
2326 DeviceState *dev;
2327 dev = sysbus_create_simple("pxa2xx-ssp", pxa255_ssp[i].io_base,
2328 qdev_get_gpio_in(s->pic, pxa255_ssp[i].irqn));
2329 s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
2332 if (usb_enabled) {
2333 sysbus_create_simple("sysbus-ohci", 0x4c000000,
2334 qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
2337 s->pcmcia[0] = pxa2xx_pcmcia_init(0x20000000);
2338 s->pcmcia[1] = pxa2xx_pcmcia_init(0x30000000);
2340 sysbus_create_simple("pxa2xx_rtc", 0x40900000,
2341 qdev_get_gpio_in(s->pic, PXA2XX_PIC_RTCALARM));
2343 s->i2c[0] = pxa2xx_i2c_init(0x40301600,
2344 qdev_get_gpio_in(s->pic, PXA2XX_PIC_I2C), 0xffff);
2345 s->i2c[1] = pxa2xx_i2c_init(0x40f00100,
2346 qdev_get_gpio_in(s->pic, PXA2XX_PIC_PWRI2C), 0xff);
2348 s->i2s = pxa2xx_i2s_init(0x40400000,
2349 qdev_get_gpio_in(s->pic, PXA2XX_PIC_I2S),
2350 qdev_get_gpio_in(s->dma, PXA2XX_RX_RQ_I2S),
2351 qdev_get_gpio_in(s->dma, PXA2XX_TX_RQ_I2S));
2353 /* GPIO1 resets the processor */
2354 /* The handler can be overridden by board-specific code */
2355 qdev_connect_gpio_out(s->gpio, 1, s->reset);
2356 return s;
2359 static void pxa2xx_register_devices(void)
2361 i2c_register_slave(&pxa2xx_i2c_slave_info);
2362 sysbus_register_dev("pxa2xx-ssp", sizeof(PXA2xxSSPState), pxa2xx_ssp_init);
2363 sysbus_register_withprop(&pxa2xx_i2c_info);
2364 sysbus_register_withprop(&pxa2xx_rtc_sysbus_info);
2367 device_init(pxa2xx_register_devices)