2 * Samsung S3C2410A RISC Microprocessor support (ARM920T based SoC).
4 * Copyright (c) 2007 OpenMoko, Inc.
5 * Author: Andrzej Zaborowski <andrew@openedhand.com>
7 * This code is licenced under the GNU GPL v2.
11 #include "qemu-timer.h"
12 #include "qemu-char.h"
21 /* Interrupt controller */
22 struct s3c_pic_state_s
{
23 target_phys_addr_t base
;
37 static void s3c_pic_update(struct s3c_pic_state_s
*s
)
39 qemu_set_irq(s
->parent_pic
[ARM_PIC_CPU_FIQ
],
40 s
->srcpnd
& s
->intmod
);
41 qemu_set_irq(s
->parent_pic
[ARM_PIC_CPU_IRQ
],
42 s
->intpnd
& ~s
->intmsk
& ~s
->intmod
);
46 * Performs interrupt arbitration and notifies the CPU.
48 * Since it's a complex logic which cannot be relied on by the OS
49 * anyway - first because real hardware doesn't do it accurately,
50 * second because it only matters when interrupts occur at the
51 * same time which normally can't be predicted - we use a simpler
52 * version for non-debug runs.
55 static const uint32_t s3c_arbmsk
[6] = {
64 # define S3C_ARB_SEL(i) ((s->priority >> (7 + (i << 1))) & 3)
65 # define S3C_ARB_MODE(i) ((s->priority >> i) & 1)
66 # define S3C_ARB_SEL_SET(i, v) \
67 s->priority &= ~(3 << (7 + (i << 1))); \
68 s->priority |= v << (7 + (i << 1));
70 static void s3c_pic_arbitrate(struct s3c_pic_state_s
*s
)
72 uint32_t pnd
= s
->srcpnd
& ~s
->intmsk
& ~s
->intmod
;
74 if (s
->intpnd
|| !pnd
) {
79 if (pnd
& s3c_arbmsk
[0]) {
82 } else if (pnd
& 0x0ffffff0) {
85 if (!(pnd
& s3c_arbmsk
[1 + (i
& 3)]))
86 if (!(pnd
& s3c_arbmsk
[1 + (++ i
& 3)]))
87 if (!(pnd
& s3c_arbmsk
[1 + (++ i
& 3)]))
91 S3C_ARB_SEL_SET(6, ((i
+ 1) & 3));
92 offset
= (i
& 3) * 6 + 4;
93 if (pnd
& (1 << offset
))
95 else if (!(pnd
& (0x1f << offset
))) {
107 i
= S3C_ARB_SEL(arb
);
109 if (!(pnd
& (1 << (i
& 3))))
110 if (!(pnd
& (1 << (++ i
& 3))))
111 if (!(pnd
& (1 << (++ i
& 3))))
114 if (S3C_ARB_MODE(arb
))
115 S3C_ARB_SEL_SET(arb
, ((i
+ 1) & 3));
118 s
->intoffset
= offset
;
119 s
->intpnd
= 1 << offset
;
123 inline static void s3c_pic_arbitrate(struct s3c_pic_state_s
*s
)
125 uint32_t pnd
= s
->srcpnd
& ~s
->intmsk
& ~s
->intmod
;
126 if (pnd
&& !s
->intpnd
)
127 s
->intpnd
= 1 << (s
->intoffset
= ffs(pnd
) - 1);
132 static const int s3c_sub_src_map
[] = {
133 [S3C_PICS_RXD0
& 31] = S3C_PIC_UART0
,
134 [S3C_PICS_TXD0
& 31] = S3C_PIC_UART0
,
135 [S3C_PICS_ERR0
& 31] = S3C_PIC_UART0
,
136 [S3C_PICS_RXD1
& 31] = S3C_PIC_UART1
,
137 [S3C_PICS_TXD1
& 31] = S3C_PIC_UART1
,
138 [S3C_PICS_ERR1
& 31] = S3C_PIC_UART1
,
139 [S3C_PICS_RXD2
& 31] = S3C_PIC_UART2
,
140 [S3C_PICS_TXD2
& 31] = S3C_PIC_UART2
,
141 [S3C_PICS_ERR2
& 31] = S3C_PIC_UART2
,
142 [S3C_PICS_TC
& 31] = S3C_PIC_ADC
,
143 [S3C_PICS_ADC
& 31] = S3C_PIC_ADC
,
146 static void s3c_pic_subupdate(struct s3c_pic_state_s
*s
)
149 const int *sub
= &s3c_sub_src_map
[-1];
150 uint32_t pnd
= s
->subsrcpnd
& ~s
->intsubmsk
;
151 while ((next
= ffs(pnd
))) {
154 s
->srcpnd
|= 1 << *sub
;
156 s3c_pic_arbitrate(s
);
159 static void s3c_pic_set_irq(void *opaque
, int irq
, int req
)
161 struct s3c_pic_state_s
*s
= (struct s3c_pic_state_s
*) opaque
;
163 /* This interrupt controller doesn't clear any request signals
164 * or register bits automatically. */
170 s
->subsrcpnd
|= 1 << irq
;
171 if (s
->intsubmsk
& (1 << irq
))
174 irq
= s3c_sub_src_map
[irq
];
176 s
->srcpnd
|= (mask
= 1 << irq
);
179 if (s
->intmod
& mask
)
180 qemu_irq_raise(s
->parent_pic
[ARM_PIC_CPU_FIQ
]);
181 else if (!s
->intpnd
&& !(s
->intmsk
& mask
)) {
183 s3c_pic_arbitrate(s
);
187 qemu_irq_raise(s
->parent_pic
[ARM_PIC_CPU_IRQ
]);
192 static void s3c_pic_reset(struct s3c_pic_state_s
*s
)
196 s
->intmsk
= 0xffffffff;
201 s
->intsubmsk
= 0x7ff;
205 #define S3C_SRCPND 0x00 /* Source Pending register */
206 #define S3C_INTMOD 0x04 /* Source Mode register */
207 #define S3C_INTMSK 0x08 /* Interrupt Mask register */
208 #define S3C_PRIORITY 0x0c /* Priority register */
209 #define S3C_INTPND 0x10 /* Interrupt Pending register */
210 #define S3C_INTOFFSET 0x14 /* Interrupt Offset register */
211 #define S3C_SUBSRCPND 0x18 /* Sub Source Pending register */
212 #define S3C_INTSUBMSK 0x1c /* Interrupt Sub Mask register */
214 static uint32_t s3c_pic_read(void *opaque
, target_phys_addr_t addr
)
216 struct s3c_pic_state_s
*s
= (struct s3c_pic_state_s
*) opaque
;
237 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
243 static void s3c_pic_write(void *opaque
, target_phys_addr_t addr
,
246 struct s3c_pic_state_s
*s
= (struct s3c_pic_state_s
*) opaque
;
252 if (value
& s
->intmod
)
256 if (s
->intpnd
& value
) {
259 s3c_pic_arbitrate(s
);
264 if (s
->intpnd
& value
) {
268 s3c_pic_arbitrate(s
);
277 s
->subsrcpnd
&= ~value
;
280 s
->intsubmsk
= value
;
281 s3c_pic_subupdate(s
);
284 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
288 static CPUReadMemoryFunc
*s3c_pic_readfn
[] = {
294 static CPUWriteMemoryFunc
*s3c_pic_writefn
[] = {
300 static void s3c_pic_save(QEMUFile
*f
, void *opaque
)
302 struct s3c_pic_state_s
*s
= (struct s3c_pic_state_s
*) opaque
;
303 qemu_put_be32s(f
, &s
->srcpnd
);
304 qemu_put_be32s(f
, &s
->intpnd
);
305 qemu_put_be32s(f
, &s
->intmsk
);
306 qemu_put_be32s(f
, &s
->intmod
);
307 qemu_put_be32s(f
, &s
->priority
);
308 qemu_put_be32s(f
, &s
->subsrcpnd
);
309 qemu_put_be32s(f
, &s
->intsubmsk
);
310 qemu_put_be32(f
, s
->intoffset
);
313 static int s3c_pic_load(QEMUFile
*f
, void *opaque
, int version_id
)
315 struct s3c_pic_state_s
*s
= (struct s3c_pic_state_s
*) opaque
;
316 qemu_get_be32s(f
, &s
->srcpnd
);
317 qemu_get_be32s(f
, &s
->intpnd
);
318 qemu_get_be32s(f
, &s
->intmsk
);
319 qemu_get_be32s(f
, &s
->intmod
);
320 qemu_get_be32s(f
, &s
->priority
);
321 qemu_get_be32s(f
, &s
->subsrcpnd
);
322 qemu_get_be32s(f
, &s
->intsubmsk
);
323 s
->intoffset
= qemu_get_be32(f
);
328 struct s3c_pic_state_s
*s3c_pic_init(target_phys_addr_t base
,
332 struct s3c_pic_state_s
*s
= (struct s3c_pic_state_s
*)
333 qemu_mallocz(sizeof(struct s3c_pic_state_s
));
336 s
->parent_pic
= arm_pic
;
337 s
->irqs
= qemu_allocate_irqs(s3c_pic_set_irq
, s
, S3C_PIC_MAX
);
341 iomemtype
= cpu_register_io_memory(0, s3c_pic_readfn
,
343 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
345 register_savevm("s3c24xx_pic", 0, 0, s3c_pic_save
, s3c_pic_load
, s
);
350 qemu_irq
*s3c_pic_get(struct s3c_pic_state_s
*s
)
355 /* Memory controller */
356 #define S3C_BWSCON 0x00 /* Bus Width & Wait Control register */
357 #define S3C_BANKCON0 0x04 /* Bank 0 Control register */
358 #define S3C_BANKCON1 0x08 /* Bank 1 Control register */
359 #define S3C_BANKCON2 0x0c /* Bank 2 Control register */
360 #define S3C_BANKCON3 0x10 /* Bank 3 Control register */
361 #define S3C_BANKCON4 0x14 /* Bank 4 Control register */
362 #define S3C_BANKCON5 0x18 /* Bank 5 Control register */
363 #define S3C_BANKCON6 0x1c /* Bank 6 Control register */
364 #define S3C_BANKCON7 0x20 /* Bank 7 Control register */
365 #define S3C_REFRESH 0x24 /* SDRAM Refresh Control register */
366 #define S3C_BANKSIZE 0x28 /* Flexible Bank Size register */
367 #define S3C_MRSRB6 0x2c /* Bank 6 Mode Set register */
368 #define S3C_MRSRB7 0x30 /* Bank 6 Mode Set register */
370 static void s3c_mc_reset(struct s3c_state_s
*s
)
372 s
->mc_regs
[S3C_BWSCON
>> 2] = 0x0000000;
373 s
->mc_regs
[S3C_BANKCON0
>> 2] = 0x0700;
374 s
->mc_regs
[S3C_BANKCON1
>> 2] = 0x0700;
375 s
->mc_regs
[S3C_BANKCON2
>> 2] = 0x0700;
376 s
->mc_regs
[S3C_BANKCON3
>> 2] = 0x0700;
377 s
->mc_regs
[S3C_BANKCON4
>> 2] = 0x0700;
378 s
->mc_regs
[S3C_BANKCON5
>> 2] = 0x0700;
379 s
->mc_regs
[S3C_BANKCON6
>> 2] = 0x18008;
380 s
->mc_regs
[S3C_BANKCON7
>> 2] = 0x18008;
381 s
->mc_regs
[S3C_REFRESH
>> 2] = 0xac0000;
382 s
->mc_regs
[S3C_BANKSIZE
>> 2] = 0x2;
383 s
->mc_regs
[S3C_MRSRB6
>> 2] = 0x00;
384 s
->mc_regs
[S3C_MRSRB7
>> 2] = 0x00;
387 static uint32_t s3c_mc_read(void *opaque
, target_phys_addr_t addr
)
389 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
393 case S3C_BWSCON
... S3C_MRSRB7
:
394 return s
->mc_regs
[addr
>> 2];
396 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
402 static void s3c_mc_write(void *opaque
, target_phys_addr_t addr
,
405 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
409 case S3C_BWSCON
... S3C_MRSRB7
:
410 s
->mc_regs
[addr
>> 2] = value
;
413 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
417 static CPUReadMemoryFunc
*s3c_mc_readfn
[] = {
423 static CPUWriteMemoryFunc
*s3c_mc_writefn
[] = {
429 static void s3c_mc_save(QEMUFile
*f
, void *opaque
)
431 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
433 for (i
= 0; i
< 13; i
++)
434 qemu_put_be32s(f
, &s
->mc_regs
[i
]);
437 static int s3c_mc_load(QEMUFile
*f
, void *opaque
, int version_id
)
439 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
441 for (i
= 0; i
< 13; i
++)
442 qemu_get_be32s(f
, &s
->mc_regs
[i
]);
446 /* NAND Flash controller */
447 #define S3C_NFCONF 0x00 /* NAND Flash Configuration register */
448 #define S3C_NFCMD 0x04 /* NAND Flash Command Set register */
449 #define S3C_NFADDR 0x08 /* NAND Flash Address Set register */
450 #define S3C_NFDATA 0x0c /* NAND Flash Data register */
451 #define S3C_NFSTAT 0x10 /* NAND Flash Operation Status register */
452 #define S3C_NFECC 0x14 /* NAND Flash ECC register */
454 static void s3c_nand_reset(struct s3c_state_s
*s
)
459 ecc_reset(&s
->nfecc
);
462 static uint32_t s3c_nand_read(void *opaque
, target_phys_addr_t addr
)
464 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
468 addr
-= s
->nand_base
;
478 if (s
->nfconf
& (1 << 15))
479 return ecc_digest(&s
->nfecc
, nand_getio(s
->nand
));
482 nand_getpins(s
->nand
, &rb
);
484 case S3C_NFECC
+ 2: shr
+= 8;
485 case S3C_NFECC
+ 1: shr
+= 8;
487 #define ECC(shr, b, shl) ((s->nfecc.lp[b] << (shl - shr)) & (1 << shl))
489 ECC(0, 1, 0) | ECC(0, 0, 1) | ECC(1, 1, 2) | ECC(1, 0, 3) |
490 ECC(2, 1, 4) | ECC(2, 0, 5) | ECC(3, 1, 6) | ECC(3, 0, 7) |
491 ECC(4, 1, 8) | ECC(4, 0, 9) | ECC(5, 1, 10) | ECC(5, 0, 11) |
492 ECC(6, 1, 12) | ECC(6, 0, 13) | ECC(7, 1, 14) | ECC(7, 0, 15) |
493 ECC(8, 1, 16) | ECC(8, 0, 17) | (s
->nfecc
.cp
<< 18)) >> shr
) &
497 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
503 static void s3c_nand_write(void *opaque
, target_phys_addr_t addr
,
506 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
509 addr
-= s
->nand_base
;
513 s
->nfconf
= value
& 0x9fff;
514 if (value
& (1 << 12))
515 ecc_reset(&s
->nfecc
);
518 s
->nfcmd
= value
& 0xff;
519 if (s
->nfconf
& (1 << 15)) {
520 nand_setpins(s
->nand
, 1, 0, (s
->nfconf
>> 11) & 1, s
->nfwp
, 0);
521 nand_setio(s
->nand
, s
->nfcmd
);
522 nand_setpins(s
->nand
, 0, 0, (s
->nfconf
>> 11) & 1, s
->nfwp
, 0);
526 s
->nfaddr
= value
& 0xff;
527 if (s
->nfconf
& (1 << 15)) {
528 nand_setpins(s
->nand
, 0, 1, (s
->nfconf
>> 11) & 1, s
->nfwp
, 0);
529 nand_setio(s
->nand
, s
->nfaddr
);
530 nand_setpins(s
->nand
, 0, 0, (s
->nfconf
>> 11) & 1, s
->nfwp
, 0);
534 if (s
->nfconf
& (1 << 15))
535 nand_setio(s
->nand
, ecc_digest(&s
->nfecc
, value
& 0xff));
538 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
542 void s3c_nand_register(struct s3c_state_s
*s
, struct nand_flash_s
*chip
)
547 void s3c_nand_setwp(struct s3c_state_s
*s
, int wp
)
552 static CPUReadMemoryFunc
*s3c_nand_readfn
[] = {
558 static CPUWriteMemoryFunc
*s3c_nand_writefn
[] = {
564 static void s3c_nand_save(QEMUFile
*f
, void *opaque
)
566 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
567 qemu_put_be16s(f
, &s
->nfconf
);
568 qemu_put_8s(f
, &s
->nfcmd
);
569 qemu_put_8s(f
, &s
->nfaddr
);
570 qemu_put_be32(f
, s
->nfwp
);
571 ecc_put(f
, &s
->nfecc
);
574 static int s3c_nand_load(QEMUFile
*f
, void *opaque
, int version_id
)
576 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
577 qemu_get_be16s(f
, &s
->nfconf
);
578 qemu_get_8s(f
, &s
->nfcmd
);
579 qemu_get_8s(f
, &s
->nfaddr
);
580 s
->nfwp
= qemu_get_be32(f
);
581 ecc_get(f
, &s
->nfecc
);
585 /* Clock & power management */
586 #define S3C_LOCKTIME 0x00 /* PLL Lock Time Count register */
587 #define S3C_MPLLCON 0x04 /* MPLL Configuration register */
588 #define S3C_UPLLCON 0x08 /* UPLL Configuration register */
589 #define S3C_CLKCON 0x0c /* Clock Generator Control register */
590 #define S3C_CLKSLOW 0x10 /* Slow Clock Control register */
591 #define S3C_CLKDIVN 0x14 /* Clock Divider Control register */
593 static void s3c_clkpwr_reset(struct s3c_state_s
*s
)
595 s
->clkpwr_regs
[S3C_LOCKTIME
>> 2] = 0x00ffffff;
596 s
->clkpwr_regs
[S3C_MPLLCON
>> 2] = 0x0005c080;
597 s
->clkpwr_regs
[S3C_UPLLCON
>> 2] = 0x00028080;
598 s
->clkpwr_regs
[S3C_CLKCON
>> 2] = 0x0007fff0;
599 s
->clkpwr_regs
[S3C_CLKSLOW
>> 2] = 0x00000004;
600 s
->clkpwr_regs
[S3C_CLKDIVN
>> 2] = 0x00000000;
603 static uint32_t s3c_clkpwr_read(void *opaque
, target_phys_addr_t addr
)
605 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
606 addr
-= s
->clkpwr_base
;
609 case S3C_LOCKTIME
... S3C_CLKDIVN
:
610 return s
->clkpwr_regs
[addr
>> 2];
612 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
618 static void s3c_clkpwr_write(void *opaque
, target_phys_addr_t addr
,
621 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
622 addr
-= s
->clkpwr_base
;
629 s
->clkpwr_regs
[addr
>> 2] = value
;
632 if (value
& (1 << 3)) {
633 cpu_interrupt(s
->env
, CPU_INTERRUPT_HALT
);
634 printf("%s: processor powered off\n", __FUNCTION__
);
635 s3c_gpio_setpwrstat(s
->io
, 2);
638 s
->env
->regs
[15] = 0; /* XXX */
641 if (value
& (1 << 2)) /* Normal IDLE mode */
642 cpu_interrupt(s
->env
, CPU_INTERRUPT_HALT
);
643 if ((s
->clkpwr_regs
[addr
>> 2] ^ value
) & 1)
644 printf("%s: SPECIAL mode %s\n", __FUNCTION__
,
645 (value
& 1) ? "on" : "off");
646 s
->clkpwr_regs
[addr
>> 2] = value
;
649 if ((s
->clkpwr_regs
[addr
>> 2] ^ value
) & (1 << 4))
650 printf("%s: SLOW mode %s\n", __FUNCTION__
,
651 (value
& (1 << 4)) ? "on" : "off");
652 s
->clkpwr_regs
[addr
>> 2] = value
;
655 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
659 static CPUReadMemoryFunc
*s3c_clkpwr_readfn
[] = {
665 static CPUWriteMemoryFunc
*s3c_clkpwr_writefn
[] = {
671 static void s3c_clkpwr_save(QEMUFile
*f
, void *opaque
)
673 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
675 for (i
= 0; i
< 6; i
++)
676 qemu_put_be32s(f
, &s
->clkpwr_regs
[i
]);
679 static int s3c_clkpwr_load(QEMUFile
*f
, void *opaque
, int version_id
)
681 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
683 for (i
= 0; i
< 6; i
++)
684 qemu_get_be32s(f
, &s
->clkpwr_regs
[i
]);
689 #define S3C_DMA_CH_N 4
691 struct s3c_dma_ch_state_s
;
692 struct s3c_dma_state_s
{ /* Modelled as an interrupt controller */
693 target_phys_addr_t base
;
695 struct s3c_dma_ch_state_s
{
711 static inline void s3c_dma_ch_run(struct s3c_dma_state_s
*s
,
712 struct s3c_dma_ch_state_s
*ch
)
716 width
= 1 << ((ch
->con
>> 20) & 3); /* DSZ */
717 burst
= (ch
->con
& (1 << 28)) ? 4 : 1; /* TSZ */
719 while (!ch
->running
&& ch
->curr_tc
> 0 && ch
->req
&&
720 (ch
->mask
& (1 << 1))) { /* ON_OFF */
721 if (width
> sizeof(buffer
)) {
722 printf("%s: wrong access width\n", __FUNCTION__
);
726 while (ch
->curr_tc
--) {
727 for (t
= 0; t
< burst
; t
++) {
728 cpu_physical_memory_read(ch
->csrc
, buffer
, width
);
729 cpu_physical_memory_write(ch
->cdst
, buffer
, width
);
731 if (!(ch
->isrcc
& 1)) /* INT */
733 if (!(ch
->idstc
& 1)) /* INT */
737 if (!(ch
->con
& (1 << 27)) && !ch
->req
) /* SERVMODE */
742 if (!(ch
->con
& (1 << 23))) { /* SWHW_SEL */
746 if (ch
->curr_tc
<= 0) {
747 if (ch
->con
& (1 << 22)) /* RELOAD */
748 ch
->mask
&= ~(1 << 1); /* ON_OFF */
750 if (!(ch
->con
& (1 << 23))) { /* SWHW_SEL */
751 printf("%s: auto-reload software controlled transfer\n",
755 ch
->csrc
= ch
->isrc
; /* S_ADDR */
756 ch
->cdst
= ch
->idst
; /* D_ADDR */
757 ch
->curr_tc
= ch
->con
& 0xfffff; /* TC */
758 ch
->con
|= 1 << 22; /* ON_OFF */
761 if (ch
->con
& (1 << 31)) /* DMD_HS */
764 if (ch
->con
& (1 << 29)) { /* INT */
765 qemu_irq_raise(ch
->intr
);
766 /* Give the system a chance to respond. */
773 static void s3c_dma_reset(struct s3c_dma_state_s
*s
)
776 for (i
= 0; i
< S3C_DMA_CH_N
; i
++) {
777 s
->ch
[i
].curr_tc
= 0;
791 static void s3c_dma_dreq(void *opaque
, int line
, int req
)
793 struct s3c_dma_state_s
*s
= (struct s3c_dma_state_s
*) opaque
;
794 struct s3c_dma_ch_state_s
*ch
= &s
->ch
[line
>> 4];
796 if (ch
->con
& (1 << 23)) /* SWHW_SEL */
797 if (((ch
->con
>> 24) & 7) == (line
& 7)) { /* HWSRCSEL */
799 s3c_dma_ch_run(s
, ch
);
803 #define S3C_DISRC 0x00 /* DMA Initial Source register */
804 #define S3C_DISRCC 0x04 /* DMA Initial Source Control register */
805 #define S3C_DIDST 0x08 /* DMA Initial Destination register */
806 #define S3C_DIDSTC 0x0c /* DMA Initial Destination Control register */
807 #define S3C_DCON 0x10 /* DMA Control register */
808 #define S3C_DSTAT 0x14 /* DMA Count register */
809 #define S3C_DCSRC 0x18 /* DMA Current Source register */
810 #define S3C_DCDST 0x1c /* DMA Current Destination register */
811 #define S3C_DMASKTRIG 0x20 /* DMA Mask Trigger register */
813 static uint32_t s3c_dma_read(void *opaque
, target_phys_addr_t addr
)
815 struct s3c_dma_state_s
*s
= (struct s3c_dma_state_s
*) opaque
;
816 struct s3c_dma_ch_state_s
*ch
= 0;
818 if (addr
>= 0 && addr
<= (S3C_DMA_CH_N
<< 6)) {
819 ch
= &s
->ch
[addr
>> 6];
843 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
849 static void s3c_dma_write(void *opaque
, target_phys_addr_t addr
,
852 struct s3c_dma_state_s
*s
= (struct s3c_dma_state_s
*) opaque
;
853 struct s3c_dma_ch_state_s
*ch
= 0;
855 if (addr
>= 0 && addr
<= (S3C_DMA_CH_N
<< 6)) {
856 ch
= &s
->ch
[addr
>> 6];
877 if (~ch
->mask
& value
& (1 << 1)) { /* ON_OFF */
878 ch
->curr_tc
= ch
->con
& 0xfffff; /* TC */
879 ch
->csrc
= ch
->isrc
; /* S_ADDR */
880 ch
->cdst
= ch
->idst
; /* D_ADDR */
884 if (value
& (1 << 2)) { /* STOP */
885 ch
->mask
&= ~(3 << 1); /* ON_OFF */
886 } else if (!(ch
->con
& (1 << 23))) { /* SWHW_SEL */
887 ch
->req
= value
& 1; /* SW_TRIG */
888 s3c_dma_ch_run(s
, ch
);
892 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
896 static CPUReadMemoryFunc
*s3c_dma_readfn
[] = {
902 static CPUWriteMemoryFunc
*s3c_dma_writefn
[] = {
908 static void s3c_dma_save(QEMUFile
*f
, void *opaque
)
910 struct s3c_dma_state_s
*s
= (struct s3c_dma_state_s
*) opaque
;
912 for (i
= 0; i
< S3C_DMA_CH_N
; i
++) {
913 qemu_put_be32(f
, s
->ch
[i
].curr_tc
);
914 qemu_put_be32(f
, s
->ch
[i
].req
);
915 qemu_put_be32s(f
, &s
->ch
[i
].con
);
916 qemu_put_be32s(f
, &s
->ch
[i
].isrc
);
917 qemu_put_be32s(f
, &s
->ch
[i
].isrcc
);
918 qemu_put_be32s(f
, &s
->ch
[i
].idst
);
919 qemu_put_be32s(f
, &s
->ch
[i
].idstc
);
920 qemu_put_be32s(f
, &s
->ch
[i
].csrc
);
921 qemu_put_be32s(f
, &s
->ch
[i
].cdst
);
922 qemu_put_be32s(f
, &s
->ch
[i
].mask
);
926 static int s3c_dma_load(QEMUFile
*f
, void *opaque
, int version_id
)
928 struct s3c_dma_state_s
*s
= (struct s3c_dma_state_s
*) opaque
;
930 for (i
= 0; i
< S3C_DMA_CH_N
; i
++) {
931 s
->ch
[i
].curr_tc
= qemu_get_be32(f
);
932 s
->ch
[i
].req
= qemu_get_be32(f
);
933 qemu_get_be32s(f
, &s
->ch
[i
].con
);
934 qemu_get_be32s(f
, &s
->ch
[i
].isrc
);
935 qemu_get_be32s(f
, &s
->ch
[i
].isrcc
);
936 qemu_get_be32s(f
, &s
->ch
[i
].idst
);
937 qemu_get_be32s(f
, &s
->ch
[i
].idstc
);
938 qemu_get_be32s(f
, &s
->ch
[i
].csrc
);
939 qemu_get_be32s(f
, &s
->ch
[i
].cdst
);
940 qemu_get_be32s(f
, &s
->ch
[i
].mask
);
945 struct s3c_dma_state_s
*s3c_dma_init(target_phys_addr_t base
, qemu_irq
*pic
)
948 struct s3c_dma_state_s
*s
= (struct s3c_dma_state_s
*)
949 qemu_mallocz(sizeof(struct s3c_dma_state_s
));
952 s
->ch
[0].intr
= pic
[0];
953 s
->ch
[1].intr
= pic
[1];
954 s
->ch
[2].intr
= pic
[2];
955 s
->ch
[3].intr
= pic
[3];
956 s
->drqs
= qemu_allocate_irqs(s3c_dma_dreq
, s
, S3C_RQ_MAX
);
960 iomemtype
= cpu_register_io_memory(0, s3c_dma_readfn
,
962 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
964 register_savevm("s3c24xx_dma", 0, 0, s3c_dma_save
, s3c_dma_load
, s
);
969 qemu_irq
*s3c_dma_get(struct s3c_dma_state_s
*s
)
974 /* PWM timers controller */
975 struct s3c_timer_state_s
;
976 struct s3c_timers_state_s
{
977 target_phys_addr_t base
;
980 struct s3c_timer_state_s
{
982 struct s3c_timers_state_s
*s
;
989 gpio_handler_t cmp_cb
;
993 uint16_t compareb
[4];
999 static const int s3c_tm_bits
[] = { 0, 8, 12, 16, 20 };
1001 static uint16_t s3c_timers_get(struct s3c_timers_state_s
*s
, int tm
)
1004 if (!s
->timer
[tm
].running
)
1005 return s
->timer
[tm
].count
;
1007 elapsed
= muldiv64(qemu_get_clock(vm_clock
) - s
->timer
[tm
].reload
,
1008 s
->timer
[tm
].divider
, ticks_per_sec
);
1009 if (unlikely(elapsed
> s
->timer
[tm
].count
))
1010 return s
->timer
[tm
].count
;
1012 return s
->timer
[tm
].count
- elapsed
;
1015 static void s3c_timers_stop(struct s3c_timers_state_s
*s
, int tm
)
1017 s
->timer
[tm
].count
= s3c_timers_get(s
, tm
);
1018 s
->timer
[tm
].running
= 0;
1021 static void s3c_timers_start(struct s3c_timers_state_s
*s
, int tm
)
1023 if (s
->timer
[tm
].running
)
1026 s
->timer
[tm
].divider
= S3C_PCLK_FREQ
>>
1027 (((s
->config
[1] >> (tm
* 4)) & 3) + 1);
1029 s
->timer
[tm
].divider
/= ((s
->config
[0] >> 0) & 0xff) + 1;
1031 s
->timer
[tm
].divider
/= ((s
->config
[0] >> 8) & 0xff) + 1;
1032 s
->timer
[tm
].running
= 1;
1033 s
->timer
[tm
].reload
= qemu_get_clock(vm_clock
);
1034 qemu_mod_timer(s
->timer
[tm
].t
,
1035 s
->timer
[tm
].reload
+ muldiv64(s
->timer
[tm
].count
,
1036 ticks_per_sec
, s
->timer
[tm
].divider
));
1039 static void s3c_timers_reset(struct s3c_timers_state_s
*s
)
1042 s
->config
[0] = 0x00000000;
1043 s
->config
[1] = 0x00000000;
1044 s
->control
= 0x00000000;
1046 for (i
= 0; i
< 5; i
++) {
1047 if (s
->timer
[i
].running
)
1048 s3c_timers_stop(s
, i
);
1049 s
->countb
[i
] = 0x0000;
1050 s
->timer
[i
].count
= 0;
1052 for (i
= 0; i
< 4; i
++)
1053 s
->compareb
[i
] = 0x0000;
1056 static void s3c_timers_tick(void *opaque
)
1058 struct s3c_timer_state_s
*t
= (struct s3c_timer_state_s
*) opaque
;
1059 struct s3c_timers_state_s
*s
= t
->s
;
1063 if (((s
->config
[1] >> 20) & 0xf) == t
->n
+ 1) {
1064 qemu_irq_raise(s
->dma
[S3C_RQ_TIMER0
]); /* TODO */
1065 qemu_irq_raise(s
->dma
[S3C_RQ_TIMER1
]);
1066 qemu_irq_raise(s
->dma
[S3C_RQ_TIMER2
]);
1068 qemu_irq_raise(t
->irq
);
1073 if (s
->control
& (1 << ((t
->n
== 4) ? 22 : (s3c_tm_bits
[t
->n
] + 3)))) {
1075 t
->count
= s
->countb
[t
->n
];
1076 s3c_timers_start(s
, t
->n
);
1078 s
->control
&= ~(1 << s3c_tm_bits
[t
->n
]);
1081 #define S3C_TCFG0 0x00 /* Timer Configuration register 0 */
1082 #define S3C_TCFG1 0x04 /* Timer Configuration register 1 */
1083 #define S3C_TCON 0x08 /* Timer Control register */
1084 #define S3C_TCNTB0 0x0c /* Timer 0 Count Buffer register */
1085 #define S3C_TCMPB0 0x10 /* Timer 0 Compare Buffer register */
1086 #define S3C_TCNTO0 0x14 /* Timer 0 Count Observation register */
1087 #define S3C_TCNTB1 0x18 /* Timer 1 Count Buffer register */
1088 #define S3C_TCMPB1 0x1c /* Timer 1 Compare Buffer register */
1089 #define S3C_TCNTO1 0x20 /* Timer 1 Count Observation register */
1090 #define S3C_TCNTB2 0x24 /* Timer 2 Count Buffer register */
1091 #define S3C_TCMPB2 0x28 /* Timer 2 Compare Buffer register */
1092 #define S3C_TCNTO2 0x2c /* Timer 2 Count Observation register */
1093 #define S3C_TCNTB3 0x30 /* Timer 3 Count Buffer register */
1094 #define S3C_TCMPB3 0x34 /* Timer 3 Compare Buffer register */
1095 #define S3C_TCNTO3 0x38 /* Timer 3 Count Observation register */
1096 #define S3C_TCNTB4 0x3c /* Timer 4 Count Buffer register */
1097 #define S3C_TCNTO4 0x40 /* Timer 4 Count Observation register */
1099 static uint32_t s3c_timers_read(void *opaque
, target_phys_addr_t addr
)
1101 struct s3c_timers_state_s
*s
= (struct s3c_timers_state_s
*) opaque
;
1107 return s
->config
[0];
1109 return s
->config
[1];
1112 case S3C_TCMPB3
: tm
++;
1113 case S3C_TCMPB2
: tm
++;
1114 case S3C_TCMPB1
: tm
++;
1116 return s
->compareb
[tm
];
1117 case S3C_TCNTB4
: tm
++;
1118 case S3C_TCNTB3
: tm
++;
1119 case S3C_TCNTB2
: tm
++;
1120 case S3C_TCNTB1
: tm
++;
1122 return s
->countb
[tm
];
1123 case S3C_TCNTO4
: tm
++;
1124 case S3C_TCNTO3
: tm
++;
1125 case S3C_TCNTO2
: tm
++;
1126 case S3C_TCNTO1
: tm
++;
1128 return s3c_timers_get(s
, tm
);
1130 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1136 static void s3c_timers_write(void *opaque
, target_phys_addr_t addr
,
1139 struct s3c_timers_state_s
*s
= (struct s3c_timers_state_s
*) opaque
;
1145 s
->config
[0] = value
& 0x00ffffff;
1148 s
->config
[1] = value
& 0x00ffffff;
1151 for (tm
= 0; tm
< 5; tm
++) {
1152 if (value
& (2 << (s3c_tm_bits
[tm
]))) {
1153 if (s
->timer
[tm
].running
) {
1154 s3c_timers_stop(s
, tm
);
1155 s
->timer
[tm
].count
= s
->countb
[tm
];
1156 s3c_timers_start(s
, tm
);
1158 s
->timer
[tm
].count
= s
->countb
[tm
];
1160 if (((value
>> s3c_tm_bits
[tm
]) & 1) ^ s
->timer
[tm
].running
) {
1161 if (s
->timer
[tm
].running
)
1162 s3c_timers_stop(s
, tm
);
1164 s3c_timers_start(s
, tm
);
1168 s
->control
= value
& 0x007fff1f;
1170 case S3C_TCMPB3
: tm
++;
1171 case S3C_TCMPB2
: tm
++;
1172 case S3C_TCMPB1
: tm
++;
1174 s
->compareb
[tm
] = value
& 0xffff;
1175 if (s
->timer
[tm
].cmp_cb
)
1176 s
->timer
[tm
].cmp_cb(tm
, s
->compareb
[tm
], s
->timer
[tm
].cmp_opaque
);
1178 case S3C_TCNTB4
: tm
++;
1179 case S3C_TCNTB3
: tm
++;
1180 case S3C_TCNTB2
: tm
++;
1181 case S3C_TCNTB1
: tm
++;
1183 s
->countb
[tm
] = value
& 0xffff;
1186 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1190 static CPUReadMemoryFunc
*s3c_timers_readfn
[] = {
1196 static CPUWriteMemoryFunc
*s3c_timers_writefn
[] = {
1202 static void s3c_timers_save(QEMUFile
*f
, void *opaque
)
1204 struct s3c_timers_state_s
*s
= (struct s3c_timers_state_s
*) opaque
;
1206 for (i
= 0; i
< 5; i
++) {
1207 qemu_put_be32(f
, s
->timer
[i
].running
);
1208 qemu_put_be32s(f
, &s
->timer
[i
].divider
);
1209 qemu_put_be16(f
, s3c_timers_get(s
, i
));
1210 qemu_put_be64s(f
, &s
->timer
[i
].reload
);
1213 for (i
= 0; i
< 4; i
++)
1214 qemu_put_be16s(f
, &s
->compareb
[i
]);
1215 for (i
= 0; i
< 5; i
++)
1216 qemu_put_be16s(f
, &s
->countb
[i
]);
1217 for (i
= 0; i
< 2; i
++)
1218 qemu_put_be32s(f
, &s
->config
[i
]);
1219 qemu_put_be32s(f
, &s
->control
);
1222 static int s3c_timers_load(QEMUFile
*f
, void *opaque
, int version_id
)
1224 struct s3c_timers_state_s
*s
= (struct s3c_timers_state_s
*) opaque
;
1226 for (i
= 0; i
< 5; i
++) {
1227 s
->timer
[i
].running
= 0;
1228 running
[i
] = qemu_get_be32(f
);
1229 qemu_get_be32s(f
, &s
->timer
[i
].divider
);
1230 qemu_get_be16s(f
, &s
->timer
[i
].count
);
1231 qemu_get_be64s(f
, &s
->timer
[i
].reload
);
1234 for (i
= 0; i
< 4; i
++)
1235 qemu_get_be16s(f
, &s
->compareb
[i
]);
1236 for (i
= 0; i
< 5; i
++)
1237 qemu_get_be16s(f
, &s
->countb
[i
]);
1238 for (i
= 0; i
< 2; i
++)
1239 qemu_get_be32s(f
, &s
->config
[i
]);
1240 qemu_get_be32s(f
, &s
->control
);
1242 for (i
= 0; i
< 5; i
++)
1244 s3c_timers_start(s
, i
);
1249 struct s3c_timers_state_s
*s3c_timers_init(target_phys_addr_t base
,
1250 qemu_irq
*pic
, qemu_irq
*dma
)
1253 struct s3c_timers_state_s
*s
= (struct s3c_timers_state_s
*)
1254 qemu_mallocz(sizeof(struct s3c_timers_state_s
));
1259 s3c_timers_reset(s
);
1261 for (i
= 0; i
< 5; i
++) {
1262 s
->timer
[i
].t
= qemu_new_timer(vm_clock
,
1263 s3c_timers_tick
, &s
->timer
[i
]);
1266 s
->timer
[i
].cmp_cb
= 0;
1267 s
->timer
[i
].irq
= pic
[i
];
1270 iomemtype
= cpu_register_io_memory(0, s3c_timers_readfn
,
1271 s3c_timers_writefn
, s
);
1272 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
1274 register_savevm("s3c24xx_timers", 0, 0,
1275 s3c_timers_save
, s3c_timers_load
, s
);
1280 void s3c_timers_cmp_handler_set(void *opaque
, int line
,
1281 gpio_handler_t handler
, void *cmp_opaque
)
1283 struct s3c_timers_state_s
*s
= (struct s3c_timers_state_s
*) opaque
;
1284 if (line
> 4 || line
< 0) {
1285 printf("%s: Bad timer number %i.\n", __FUNCTION__
, line
);
1288 s
->timer
[line
].cmp_cb
= handler
;
1289 s
->timer
[line
].cmp_opaque
= cmp_opaque
;
1293 struct s3c_uart_state_s
{
1294 target_phys_addr_t base
;
1301 #define UART_MAX_CHR 4
1303 CharDriverState
*chr
[UART_MAX_CHR
];
1313 static void s3c_uart_reset(struct s3c_uart_state_s
*s
)
1318 s
->control
= 0x0000;
1325 static void s3c_uart_err(struct s3c_uart_state_s
*s
, int err
)
1328 if (s
->control
& (1 << 6))
1329 qemu_irq_raise(s
->irq
[2]);
1332 inline static void s3c_uart_full(struct s3c_uart_state_s
*s
, int pulse
)
1334 if (s
->fcontrol
& 1) /* FIFOEnable */
1335 if (s
->rxlen
< (((s
->fcontrol
>> 4) & 3) + 1) * 4) {
1336 if (((s
->control
>> 0) & 3) != 1 || /* ReceiveMode */
1339 if (!(s
->control
& (1 << 7))) /* RxTimeOutEnable */
1341 /* When the Rx FIFO trigger level is not reached, the interrupt
1342 * is generated anyway, just after a small timeout instead of
1346 switch ((s
->control
>> 0) & 3) { /* ReceiveMode */
1348 if ((s
->control
& (1 << 8)) || pulse
) /* RxInterruptType */
1349 qemu_irq_raise(s
->irq
[0]);
1353 qemu_irq_raise(s
->dma
[0]);
1358 inline static void s3c_uart_empty(struct s3c_uart_state_s
*s
, int pulse
)
1360 switch ((s
->control
>> 2) & 3) { /* TransmitMode */
1362 if ((s
->control
& (1 << 9)) || pulse
) /* TxInterruptType */
1363 qemu_irq_raise(s
->irq
[1]);
1367 qemu_irq_raise(s
->dma
[0]);
1372 inline static void s3c_uart_update(struct s3c_uart_state_s
*s
)
1374 s3c_uart_empty(s
, 0);
1375 s3c_uart_full(s
, 0);
1378 static void s3c_uart_params_update(struct s3c_uart_state_s
*s
)
1380 QEMUSerialSetParams ssp
;
1385 /* XXX Calculate PCLK frequency from clock manager registers */
1386 ssp
.speed
= (S3C_PCLK_FREQ
>> 4) / (s
->brdiv
+ 1);
1388 switch ((s
->lcontrol
>> 3) & 7) {
1401 ssp
.data_bits
= 5 + (s
->lcontrol
& 3);
1403 ssp
.stop_bits
= (s
->lcontrol
& (1 << 2)) ? 2 : 1;
1405 for (i
= 0; i
< s
->chr_num
; i
++)
1406 qemu_chr_ioctl(s
->chr
[i
], CHR_IOCTL_SERIAL_SET_PARAMS
, &ssp
);
1409 static int s3c_uart_is_empty(void *opaque
)
1411 struct s3c_uart_state_s
*s
= (struct s3c_uart_state_s
*) opaque
;
1412 if (s
->fcontrol
& 1) /* FIFOEnable */
1413 return 16 - s
->rxlen
;
1415 return 1 - s
->rxlen
;
1418 static void s3c_uart_rx(void *opaque
, const uint8_t *buf
, int size
)
1420 struct s3c_uart_state_s
*s
= (struct s3c_uart_state_s
*) opaque
;
1422 if (s
->fcontrol
& 1) { /* FIFOEnable */
1423 if (s
->rxlen
+ size
> 16) {
1424 size
= 16 - s
->rxlen
;
1428 left
= 16 - ((s
->rxstart
+ s
->rxlen
) & 15);
1430 memcpy(s
->rxfifo
+ ((s
->rxstart
+ s
->rxlen
) & 15), buf
, left
);
1431 memcpy(s
->rxfifo
, buf
+ left
, size
- left
);
1433 memcpy(s
->rxfifo
+ ((s
->rxstart
+ s
->rxlen
) & 15), buf
, size
);
1436 if (s
->rxlen
+ size
> 1)
1441 s3c_uart_full(s
, 1);
1444 /* S3C2410 UART doesn't seem to understand break conditions. */
1445 static void s3c_uart_event(void *opaque
, int event
)
1449 #define S3C_ULCON 0x00 /* UART Line Control register */
1450 #define S3C_UCON 0x04 /* UART Control register */
1451 #define S3C_UFCON 0x08 /* UART FIFO Control register */
1452 #define S3C_UMCON 0x0c /* UART Modem Control register */
1453 #define S3C_UTRSTAT 0x10 /* UART Tx/Rx Status register */
1454 #define S3C_UERSTAT 0x14 /* UART Error Status register */
1455 #define S3C_UFSTAT 0x18 /* UART FIFO Status register */
1456 #define S3C_UMSTAT 0x1c /* UART Modem Status register */
1457 #define S3C_UTXH 0x20 /* UART Transmit Buffer register */
1458 #define S3C_URXH 0x24 /* UART Receive Buffer register */
1459 #define S3C_UBRDIV 0x28 /* UART Baud Rate Divisor register */
1461 static uint32_t s3c_uart_read(void *opaque
, target_phys_addr_t addr
)
1463 struct s3c_uart_state_s
*s
= (struct s3c_uart_state_s
*) opaque
;
1477 return 6 | !!s
->rxlen
;
1479 /* XXX: UERSTAT[3] is Reserved but Linux thinks it is BREAK */
1486 return s
->rxlen
? s
->rxlen
| (1 << 8) : 0;
1494 if (s
->fcontrol
& 1) { /* FIFOEnable */
1495 ret
= s
->rxfifo
[s
->rxstart
++];
1505 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1511 static void s3c_uart_write(void *opaque
, target_phys_addr_t addr
,
1514 struct s3c_uart_state_s
*s
= (struct s3c_uart_state_s
*) opaque
;
1521 if ((s
->lcontrol
^ value
) & (1 << 6))
1522 printf("%s: UART Infra-red mode %s\n", __FUNCTION__
,
1523 (value
& (1 << 6)) ? "on" : "off");
1524 s
->lcontrol
= value
;
1525 s3c_uart_params_update(s
);
1529 /* XXX: UCON[4] is Reserved but Linux thinks it is BREAK */
1530 if ((s
->control
^ value
) & (1 << 5))
1531 printf("%s: UART loopback test mode %s\n", __FUNCTION__
,
1532 (value
& (1 << 5)) ? "on" : "off");
1533 s
->control
= value
& 0x7ef;
1537 if (value
& (1 << 1)) /* RxReset */
1539 s
->fcontrol
= value
& 0xf1;
1543 #ifdef CONFIG_S3C_MODEM /* not handled, openmoko modem.c not imported */
1544 if ((s
->mcontrol
^ value
) & (1 << 4)) {
1545 afc
= (value
>> 4) & 1;
1546 for (i
= 0; i
< s
->chr_num
; i
++)
1547 qemu_chr_ioctl(s
->chr
[i
], CHR_IOCTL_MODEM_HANDSHAKE
, &afc
);
1550 s
->mcontrol
= value
& 0x11;
1555 for (i
= 0; i
< s
->chr_num
; i
++)
1556 qemu_chr_write(s
->chr
[i
], &ch
, 1);
1557 s3c_uart_empty(s
, 1);
1561 s
->brdiv
= value
& 0xffff;
1562 s3c_uart_params_update(s
);
1566 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1570 static CPUReadMemoryFunc
*s3c_uart_readfn
[] = {
1576 static CPUWriteMemoryFunc
*s3c_uart_writefn
[] = {
1582 static void s3c_uart_save(QEMUFile
*f
, void *opaque
)
1584 struct s3c_uart_state_s
*s
= (struct s3c_uart_state_s
*) opaque
;
1585 qemu_put_8s(f
, &s
->data
);
1586 qemu_put_buffer(f
, s
->rxfifo
, sizeof(s
->rxfifo
));
1587 qemu_put_be32(f
, s
->rxstart
);
1588 qemu_put_be32(f
, s
->rxlen
);
1589 qemu_put_8s(f
, &s
->lcontrol
);
1590 qemu_put_8s(f
, &s
->fcontrol
);
1591 qemu_put_8s(f
, &s
->mcontrol
);
1592 qemu_put_be16s(f
, &s
->control
);
1593 qemu_put_be16s(f
, &s
->brdiv
);
1594 qemu_put_8s(f
, &s
->errstat
);
1597 static int s3c_uart_load(QEMUFile
*f
, void *opaque
, int version_id
)
1599 struct s3c_uart_state_s
*s
= (struct s3c_uart_state_s
*) opaque
;
1600 qemu_get_8s(f
, &s
->data
);
1601 qemu_get_buffer(f
, s
->rxfifo
, sizeof(s
->rxfifo
));
1602 s
->rxstart
= qemu_get_be32(f
);
1603 s
->rxlen
= qemu_get_be32(f
);
1604 qemu_get_8s(f
, &s
->lcontrol
);
1605 qemu_get_8s(f
, &s
->fcontrol
);
1606 qemu_get_8s(f
, &s
->mcontrol
);
1607 qemu_get_be16s(f
, &s
->control
);
1608 qemu_get_be16s(f
, &s
->brdiv
);
1609 qemu_get_8s(f
, &s
->errstat
);
1614 struct s3c_uart_state_s
*s3c_uart_init(target_phys_addr_t base
,
1615 qemu_irq
*irqs
, qemu_irq
*dma
)
1618 struct s3c_uart_state_s
*s
= (struct s3c_uart_state_s
*)
1619 qemu_mallocz(sizeof(struct s3c_uart_state_s
));
1627 iomemtype
= cpu_register_io_memory(0, s3c_uart_readfn
,
1628 s3c_uart_writefn
, s
);
1629 cpu_register_physical_memory(s
->base
, 0xfff, iomemtype
);
1631 register_savevm("s3c24xx_uart", base
, 0, s3c_uart_save
, s3c_uart_load
, s
);
1636 void s3c_uart_attach(struct s3c_uart_state_s
*s
, CharDriverState
*chr
)
1638 if (s
->chr_num
>= UART_MAX_CHR
)
1639 cpu_abort(cpu_single_env
, "%s: Too many devices\n", __FUNCTION__
);
1640 s
->chr
[s
->chr_num
++] = chr
;
1642 qemu_chr_add_handlers(chr
, s3c_uart_is_empty
,
1643 s3c_uart_rx
, s3c_uart_event
, s
);
1646 /* ADC & Touchscreen interface */
1647 struct s3c_adc_state_s
{
1648 target_phys_addr_t base
;
1669 static void s3c_adc_reset(struct s3c_adc_state_s
*s
)
1672 s
->control
= 0x3fc4;
1678 static void s3c_adc_start(struct s3c_adc_state_s
*s
)
1680 if (!s
->enable
|| (s
->ts
& 7) == 0)
1682 s
->control
&= ~(1 << 15);
1683 s
->in_idx
= (s
->control
>> 3) & 7;
1684 qemu_mod_timer(s
->convt
, qemu_get_clock(vm_clock
) + (ticks_per_sec
>> 5));
1687 static void s3c_adc_done(void *opaque
)
1689 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*) opaque
;
1690 s
->xdata
= s
->input
[s
->in_idx
] & 0x3ff;
1691 s
->control
|= 1 << 15;
1692 qemu_irq_raise(s
->irq
);
1695 static void s3c_adc_tick(void *opaque
)
1697 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*) opaque
;
1701 if ((s
->ts
& 3) == 3 && s
->enable
)
1702 qemu_irq_raise(s
->tcirq
);
1703 else if (s
->enable
&& ((s
->ts
& (1 << 2)) || (s
->ts
& 3))) {
1704 sx
= s
->x
* s
->scale
[0] + s
->y
* s
->scale
[1] + s
->scale
[2];
1705 sy
= s
->x
* s
->scale
[3] + s
->y
* s
->scale
[4] + s
->scale
[5];
1706 s
->xdata
= ((sx
>> 13) & 0xfff) | (1 << 14) | ((s
->ts
& 3) << 12);
1707 s
->ydata
= ((sy
>> 13) & 0xfff) | (1 << 14) | ((s
->ts
& 3) << 12);
1708 s
->xdata
^= s
->noise
>> 1;
1709 s
->ydata
^= s
->noise
>> 2;
1710 qemu_irq_raise(s
->irq
);
1714 qemu_mod_timer(s
->tst
, qemu_get_clock(vm_clock
) +
1715 (ticks_per_sec
>> 5));
1719 static void s3c_adc_event(void *opaque
,
1720 int x
, int y
, int z
, int buttons_state
)
1722 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*) opaque
;
1723 s
->down
= !!buttons_state
;
1729 #define S3C_ADCCON 0x00 /* ADC Control register */
1730 #define S3C_ADCTSC 0x04 /* ADC Touchscreen Control register */
1731 #define S3C_ADCDLY 0x08 /* ADC Start or Interval Delay register */
1732 #define S3C_ADCDAT0 0x0c /* ADC Conversion Data register 0 */
1733 #define S3C_ADCDAT1 0x10 /* ADC Conversion Data register 1 */
1735 static uint32_t s3c_adc_read(void *opaque
, target_phys_addr_t addr
)
1737 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*) opaque
;
1750 return ((!s
->down
) << 15) | s
->xdata
;
1752 return ((!s
->down
) << 15) | s
->ydata
;
1754 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1760 static void s3c_adc_write(void *opaque
, target_phys_addr_t addr
,
1763 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*) opaque
;
1768 s
->control
= (s
->control
& 0x8000) | (value
& 0x7ffe);
1769 s
->enable
= !(value
& 4);
1770 if ((value
& 1) && !(value
& 2))
1773 qemu_del_timer(s
->convt
);
1778 s
->ts
= value
& 0xff;
1782 s
->delay
= value
& 0xffff;
1785 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1789 static CPUReadMemoryFunc
*s3c_adc_readfn
[] = {
1795 static CPUWriteMemoryFunc
*s3c_adc_writefn
[] = {
1801 static void s3c_adc_save(QEMUFile
*f
, void *opaque
)
1803 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*) opaque
;
1805 qemu_put_be32(f
, s
->enable
);
1806 for (i
= 0; i
< 8; i
++)
1807 qemu_put_be32(f
, s
->input
[i
]);
1808 qemu_put_be32(f
, s
->in_idx
);
1809 qemu_put_be32(f
, s
->noise
);
1811 qemu_put_be16s(f
, &s
->control
);
1812 qemu_put_be16s(f
, &s
->ts
);
1813 qemu_put_be16s(f
, &s
->delay
);
1814 qemu_put_be16s(f
, &s
->xdata
);
1815 qemu_put_be16s(f
, &s
->ydata
);
1818 static int s3c_adc_load(QEMUFile
*f
, void *opaque
, int version_id
)
1820 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*) opaque
;
1822 s
->enable
= qemu_get_be32(f
);
1823 for (i
= 0; i
< 8; i
++)
1824 s
->input
[i
] = qemu_get_be32(f
);
1825 s
->in_idx
= qemu_get_be32(f
);
1826 s
->noise
= qemu_get_be32(f
);
1828 qemu_get_be16s(f
, &s
->control
);
1829 qemu_get_be16s(f
, &s
->ts
);
1830 qemu_get_be16s(f
, &s
->delay
);
1831 qemu_get_be16s(f
, &s
->xdata
);
1832 qemu_get_be16s(f
, &s
->ydata
);
1834 if (s
->enable
&& (s
->ts
& 7) && !(s
->control
& (1 << 15)))
1840 struct s3c_adc_state_s
*s3c_adc_init(target_phys_addr_t base
, qemu_irq irq
,
1844 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*)
1845 qemu_mallocz(sizeof(struct s3c_adc_state_s
));
1850 s
->convt
= qemu_new_timer(vm_clock
, s3c_adc_done
, s
);
1851 s
->tst
= qemu_new_timer(vm_clock
, s3c_adc_tick
, s
);
1855 iomemtype
= cpu_register_io_memory(0, s3c_adc_readfn
,
1856 s3c_adc_writefn
, s
);
1857 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
1859 /* We want absolute coordinates */
1860 qemu_add_mouse_event_handler(s3c_adc_event
, s
, 1,
1861 "QEMU S3C2410-driven Touchscreen");
1863 register_savevm("s3c24xx_adc", 0, 0, s3c_adc_save
, s3c_adc_load
, s
);
1868 void s3c_adc_setscale(struct s3c_adc_state_s
*adc
, const int m
[])
1870 memcpy(adc
->scale
, m
, 6 * sizeof(int));
1873 /* IIC-bus serial interface */
1874 struct s3c_i2c_state_s
{
1877 target_phys_addr_t base
;
1888 static void s3c_i2c_irq(struct s3c_i2c_state_s
*s
)
1890 s
->control
|= 1 << 4;
1891 if (s
->control
& (1 << 5))
1892 qemu_irq_raise(s
->irq
);
1895 static void s3c_i2c_reset(struct s3c_i2c_state_s
*s
)
1903 static void s3c_i2c_event(i2c_slave
*i2c
, enum i2c_event event
)
1905 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) i2c
;
1906 if (!(s
->status
& (1 << 4)))
1910 case I2C_START_RECV
:
1911 case I2C_START_SEND
:
1912 s
->status
|= 1 << 2;
1919 s
->status
|= 1 << 0;
1926 static int s3c_i2c_tx(i2c_slave
*i2c
, uint8_t data
)
1928 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) i2c
;
1929 if (!(s
->status
& (1 << 4)))
1932 if ((s
->status
>> 6) == 0)
1933 s
->data
= data
; /* TODO */
1934 s
->status
&= ~(1 << 0);
1937 return !(s
->control
& (1 << 7));
1940 static int s3c_i2c_rx(i2c_slave
*i2c
)
1942 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) i2c
;
1943 if (!(s
->status
& (1 << 4)))
1946 if ((s
->status
>> 6) == 1) {
1947 s
->status
&= ~(1 << 0);
1955 static void s3c_master_work(void *opaque
)
1957 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) opaque
;
1958 int start
= 0, stop
= 0, ack
= 1;
1959 if (s
->control
& (1 << 4)) /* Interrupt pending */
1961 if ((s
->status
& 0x90) != 0x90) /* Master */
1963 stop
= ~s
->status
& (1 << 5);
1964 if (s
->newstart
&& s
->status
& (1 << 5)) { /* START */
1973 ack
= !i2c_start_transfer(s
->bus
, s
->data
>> 1, (~s
->status
>> 6) & 1);
1975 i2c_end_transfer(s
->bus
);
1976 else if (s
->status
& (1 << 6))
1977 ack
= !i2c_send(s
->bus
, s
->data
);
1979 s
->data
= i2c_recv(s
->bus
);
1981 if (!(s
->control
& (1 << 7))) /* ACK */
1985 if (!(s
->status
& (1 << 5))) {
1996 #define S3C_IICCON 0x00 /* IIC-Bus Control register */
1997 #define S3C_IICSTAT 0x04 /* IIC-Bus Control / Status register */
1998 #define S3C_IICADD 0x08 /* IIC-Bus Address register */
1999 #define S3C_IICDS 0x0c /* IIC-Bus Tx / Rx Data Shift register */
2001 static uint32_t s3c_i2c_read(void *opaque
, target_phys_addr_t addr
)
2003 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) opaque
;
2010 return s
->status
& ~(1 << 5); /* Busy signal */
2016 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
2022 static void s3c_i2c_write(void *opaque
, target_phys_addr_t addr
,
2025 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) opaque
;
2030 s
->control
= (s
->control
| 0xef) & value
;
2037 s
->status
|= value
& 0xf0;
2038 if (s
->status
& (1 << 5))
2044 s
->addy
= value
& 0x7f;
2045 i2c_set_slave_address(&s
->slave
, s
->addy
);
2049 s
->data
= value
& 0xff;
2053 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
2057 static CPUReadMemoryFunc
*s3c_i2c_readfn
[] = {
2063 static CPUWriteMemoryFunc
*s3c_i2c_writefn
[] = {
2069 static void s3c_i2c_save(QEMUFile
*f
, void *opaque
)
2071 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) opaque
;
2072 qemu_put_8s(f
, &s
->control
);
2073 qemu_put_8s(f
, &s
->status
);
2074 qemu_put_8s(f
, &s
->data
);
2075 qemu_put_8s(f
, &s
->addy
);
2077 qemu_put_be32(f
, s
->busy
);
2078 qemu_put_be32(f
, s
->newstart
);
2080 // i2c_bus_save(f, s->bus);
2081 i2c_slave_save(f
, &s
->slave
);
2084 static int s3c_i2c_load(QEMUFile
*f
, void *opaque
, int version_id
)
2086 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) opaque
;
2087 qemu_get_8s(f
, &s
->control
);
2088 qemu_get_8s(f
, &s
->status
);
2089 qemu_get_8s(f
, &s
->data
);
2090 qemu_get_8s(f
, &s
->addy
);
2092 s
->busy
= qemu_get_be32(f
);
2093 s
->newstart
= qemu_get_be32(f
);
2095 // i2c_bus_load(f, s->bus);
2096 i2c_slave_load(f
, &s
->slave
);
2100 struct s3c_i2c_state_s
*s3c_i2c_init(target_phys_addr_t base
, qemu_irq irq
)
2103 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*)
2104 qemu_mallocz(sizeof(struct s3c_i2c_state_s
));
2108 s
->slave
.event
= s3c_i2c_event
;
2109 s
->slave
.send
= s3c_i2c_tx
;
2110 s
->slave
.recv
= s3c_i2c_rx
;
2111 s
->bus
= i2c_init_bus();
2115 iomemtype
= cpu_register_io_memory(0, s3c_i2c_readfn
,
2116 s3c_i2c_writefn
, s
);
2117 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
2119 register_savevm("s3c24xx_i2c", 0, 0, s3c_i2c_save
, s3c_i2c_load
, s
);
2124 i2c_bus
*s3c_i2c_bus(struct s3c_i2c_state_s
*s
)
2129 /* Serial Peripheral Interface */
2130 struct s3c_spi_state_s
{
2131 target_phys_addr_t base
;
2150 uint8_t (*txrx
[2])(void *opaque
, uint8_t value
);
2151 uint8_t (*btxrx
[2])(void *opaque
, uint8_t value
);
2155 static void s3c_spi_update(struct s3c_spi_state_s
*s
)
2158 for (i
= 0; i
< 2; i
++) {
2159 switch ((s
->chan
[i
].control
>> 5) & 3) { /* SMOD */
2161 qemu_irq_raise(s
->chan
[i
].irq
);
2164 qemu_irq_raise(s
->chan
[i
].drq
);
2170 static void s3c_spi_reset(struct s3c_spi_state_s
*s
)
2172 memset(s
->chan
, 0, sizeof(s
->chan
));
2173 s
->chan
[0].pin
= 0x02;
2174 s
->chan
[1].pin
= 0x02;
2178 #define S3C_SPCON0 0x00 /* SPI channel 0 control register */
2179 #define S3C_SPSTA0 0x04 /* SPI channel 0 status register */
2180 #define S3C_SPPIN0 0x08 /* SPI channel 0 pin control register */
2181 #define S3C_SPPRE0 0x0c /* SPI channel 0 baudrate prescaler register */
2182 #define S3C_SPTDAT0 0x10 /* SPI channel 0 Tx data register */
2183 #define S3C_SPRDAT0 0x14 /* SPI channel 0 Rx data register */
2184 #define S3C_SPCON1 0x20 /* SPI channel 1 control register */
2185 #define S3C_SPSTA1 0x24 /* SPI channel 1 status register */
2186 #define S3C_SPPIN1 0x28 /* SPI channel 1 pin control register */
2187 #define S3C_SPPRE1 0x2c /* SPI channel 1 baudrate prescaler register */
2188 #define S3C_SPTDAT1 0x30 /* SPI channel 1 Tx data register */
2189 #define S3C_SPRDAT1 0x34 /* SPI channel 1 Rx data register */
2191 static uint32_t s3c_spi_read(void *opaque
, target_phys_addr_t addr
)
2193 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*) opaque
;
2201 return s
->chan
[ch
].control
;
2209 return s
->chan
[ch
].pin
;
2213 return s
->chan
[ch
].pre
;
2217 return s
->chan
[ch
+ 2].txbuf
;
2221 if (s
->txrx
[ch
] && (s
->chan
[ch
].control
& 0x19) == 0x19)
2222 s
->chan
[ch
].rxbuf
= s
->txrx
[ch
](s
->opaque
[ch
], 'Q');
2224 return s
->chan
[ch
].rxbuf
;
2227 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
2233 static void s3c_spi_write(void *opaque
, target_phys_addr_t addr
,
2236 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*) opaque
;
2244 s
->chan
[ch
].control
= value
& 0x7f;
2250 s
->chan
[ch
].pin
= value
& 0x07;
2255 s
->chan
[ch
].pre
= value
& 0xff;
2260 s
->chan
[ch
].txbuf
= value
& 0xff;
2261 if (s
->txrx
[ch
] && (s
->chan
[ch
].control
& 0x19) == 0x18)
2262 s
->chan
[ch
].rxbuf
= s
->txrx
[ch
](s
->opaque
[ch
], value
& 0xff);
2267 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
2271 static CPUReadMemoryFunc
*s3c_spi_readfn
[] = {
2277 static CPUWriteMemoryFunc
*s3c_spi_writefn
[] = {
2283 static void s3c_spi_save(QEMUFile
*f
, void *opaque
)
2285 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*) opaque
;
2287 for (i
= 0; i
< 2; i
++) {
2288 qemu_put_8s(f
, &s
->chan
[i
].control
);
2289 qemu_put_8s(f
, &s
->chan
[i
].pin
);
2290 qemu_put_8s(f
, &s
->chan
[i
].pre
);
2292 qemu_put_8s(f
, &s
->chan
[i
].txbuf
);
2293 qemu_put_8s(f
, &s
->chan
[i
].rxbuf
);
2294 qemu_put_be32(f
, s
->chan
[i
].cs_pin
);
2295 qemu_put_be32(f
, s
->chan
[i
].clk_pin
);
2296 qemu_put_be32(f
, s
->chan
[i
].mosi_pin
);
2297 qemu_put_be32(f
, s
->chan
[i
].bit
);
2301 static int s3c_spi_load(QEMUFile
*f
, void *opaque
, int version_id
)
2303 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*) opaque
;
2305 for (i
= 0; i
< 2; i
++) {
2306 qemu_get_8s(f
, &s
->chan
[i
].control
);
2307 qemu_get_8s(f
, &s
->chan
[i
].pin
);
2308 qemu_get_8s(f
, &s
->chan
[i
].pre
);
2310 qemu_get_8s(f
, &s
->chan
[i
].txbuf
);
2311 qemu_get_8s(f
, &s
->chan
[i
].rxbuf
);
2312 s
->chan
[i
].cs_pin
= qemu_get_be32(f
);
2313 s
->chan
[i
].clk_pin
= qemu_get_be32(f
);
2314 s
->chan
[i
].mosi_pin
= qemu_get_be32(f
);
2315 s
->chan
[i
].bit
= qemu_get_be32(f
);
2321 static void s3c_spi_bitbang_cs(void *opaque
, int line
, int level
)
2323 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*) opaque
;
2325 if (s
->chan
[ch
].cs_pin
|| level
) {
2326 if (s
->chan
[ch
].bit
&& s
->txrx
[ch
] && !s
->btxrx
[ch
]) {
2327 s
->chan
[ch
].txbuf
<<= 8 - s
->chan
[ch
].bit
;
2328 s
->chan
[ch
].rxbuf
= s
->txrx
[ch
](s
->opaque
[ch
], s
->chan
[ch
].txbuf
);
2330 } else if (!s
->chan
[ch
].cs_pin
|| !level
)
2331 s
->chan
[ch
].bit
= 0;
2333 /* SSn is active low. */
2334 s
->chan
[ch
].cs_pin
= !level
;
2337 static void s3c_spi_bitbang_clk(void *opaque
, int line
, int level
)
2339 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*) opaque
;
2341 if (!s
->chan
[ch
].cs_pin
)
2344 /* Detect CLK rising edge */
2345 if (s
->chan
[ch
].clk_pin
|| !level
)
2349 qemu_set_irq(s
->chan
[ch
].miso
,
2350 s
->btxrx
[ch
](s
->opaque
[ch
], s
->chan
[ch
].mosi_pin
));
2354 s
->chan
[ch
].txbuf
<<= 1;
2355 s
->chan
[ch
].txbuf
|= s
->chan
[ch
].mosi_pin
;
2357 qemu_set_irq(s
->chan
[ch
].miso
, (s
->chan
[ch
].rxbuf
>> 7) & 1);
2358 s
->chan
[ch
].rxbuf
<<= 1;
2360 if (++ s
->chan
[ch
].bit
== 8) {
2362 s
->chan
[ch
].rxbuf
= s
->txrx
[ch
](s
->opaque
[ch
], s
->chan
[ch
].txbuf
);
2363 s
->chan
[ch
].bit
= 0;
2367 s
->chan
[ch
].clk_pin
= level
;
2370 static void s3c_spi_bitbang_mosi(void *opaque
, int line
, int level
)
2372 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*) opaque
;
2374 s
->chan
[ch
].mosi_pin
= level
;
2377 static const struct {
2378 int cs
, clk
, miso
, mosi
;
2379 } s3c_spi_pins
[2] = {
2380 { S3C_GPG(2), S3C_GPE(13), S3C_GPE(11), S3C_GPE(12) },
2381 { S3C_GPG(3), S3C_GPG(7), S3C_GPG(5), S3C_GPG(6) },
2384 static void s3c_spi_bitbang_init(struct s3c_spi_state_s
*s
,
2385 struct s3c_gpio_state_s
*gpio
)
2388 qemu_irq
*cs
= qemu_allocate_irqs(s3c_spi_bitbang_cs
, s
, 2);
2389 qemu_irq
*clk
= qemu_allocate_irqs(s3c_spi_bitbang_clk
, s
, 2);
2390 qemu_irq
*mosi
= qemu_allocate_irqs(s3c_spi_bitbang_mosi
, s
, 2);
2392 for (i
= 0; i
< 2; i
++) {
2393 s3c_gpio_out_set(gpio
, s3c_spi_pins
[i
].cs
, cs
[i
]);
2394 s3c_gpio_out_set(gpio
, s3c_spi_pins
[i
].clk
, clk
[i
]);
2395 s
->chan
[i
].miso
= s3c_gpio_in_get(gpio
)[s3c_spi_pins
[i
].miso
];
2396 s3c_gpio_out_set(gpio
, s3c_spi_pins
[i
].mosi
, mosi
[i
]);
2400 struct s3c_spi_state_s
*s3c_spi_init(target_phys_addr_t base
,
2401 qemu_irq irq0
, qemu_irq drq0
, qemu_irq irq1
, qemu_irq drq1
,
2402 struct s3c_gpio_state_s
*gpio
)
2405 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*)
2406 qemu_mallocz(sizeof(struct s3c_spi_state_s
));
2409 s
->chan
[0].irq
= irq0
;
2410 s
->chan
[0].drq
= drq0
;
2411 s
->chan
[1].irq
= irq1
;
2412 s
->chan
[1].drq
= drq1
;
2416 iomemtype
= cpu_register_io_memory(0, s3c_spi_readfn
,
2417 s3c_spi_writefn
, s
);
2418 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
2420 s3c_spi_bitbang_init(s
, gpio
);
2422 register_savevm("s3c24xx_spi", 0, 0, s3c_spi_save
, s3c_spi_load
, s
);
2427 void s3c_spi_attach(struct s3c_spi_state_s
*s
, int ch
,
2428 uint8_t (*txrx
)(void *opaque
, uint8_t value
),
2429 uint8_t (*btxrx
)(void *opaque
, uint8_t value
), void *opaque
)
2432 cpu_abort(cpu_single_env
, "%s: No channel %i\n", __FUNCTION__
, ch
);
2434 s
->btxrx
[ch
] = btxrx
;
2435 s
->opaque
[ch
] = opaque
;
2438 /* IIS-BUS interface */
2439 static inline void s3c_i2s_update(struct s3c_i2s_state_s
*s
)
2442 (s
->control
& (1 << 0)) && !(s
->control
& (1 << 3)) &&
2443 (s
->mode
& (1 << 7)) && (s
->fcontrol
& (1 << 13));
2445 (s
->control
& (1 << 0)) && !(s
->control
& (1 << 2)) &&
2446 (s
->mode
& (1 << 6)) && (s
->fcontrol
& (1 << 12));
2447 s
->control
&= ~0xc0;
2448 /* The specs are unclear about the FIFO-ready flags logic.
2449 * Implement semantics that make most sense. */
2450 if (s
->tx_en
&& s
->tx_len
)
2451 s
->control
|= (1 << 7);
2452 if (s
->rx_en
&& s
->rx_len
)
2453 s
->control
|= (1 << 6);
2455 qemu_set_irq(s
->dma
[S3C_RQ_I2SSDO
], (s
->control
>> 5) &
2456 (s
->control
>> 7) & (s
->fcontrol
>> 15) & 1);
2457 qemu_set_irq(s
->dma
[S3C_RQ_I2SSDI0
], (s
->control
>> 4) &
2458 (s
->control
>> 6) & (s
->fcontrol
>> 14) & 1);
2459 qemu_set_irq(s
->dma
[S3C_RQ_I2SSDI1
], (s
->control
>> 4) &
2460 (s
->control
>> 6) & (s
->fcontrol
>> 14) & 1);
2463 static void s3c_i2s_reset(struct s3c_i2s_state_s
*s
)
2467 s
->prescaler
= 0x000;
2468 s
->fcontrol
= 0x0000;
2474 #define S3C_IISCON 0x00 /* IIS Control register */
2475 #define S3C_IISMOD 0x04 /* IIS Mode register */
2476 #define S3C_IISPSR 0x08 /* IIS Prescaler register */
2477 #define S3C_IISFCON 0x0c /* IIS FIFO Interface register */
2478 #define S3C_IISFIFO 0x10 /* IIS FIFO register */
2480 static uint32_t s3c_i2s_read(void *opaque
, target_phys_addr_t addr
)
2482 struct s3c_i2s_state_s
*s
= (struct s3c_i2s_state_s
*) opaque
;
2492 return s
->prescaler
;
2494 return s
->fcontrol
|
2495 (MAX(32 - s
->tx_len
, 0) << 6) |
2498 if (s
->rx_len
> 0) {
2503 s
->buffer
= (uint16_t) (ret
= s
->codec_in(s
->opaque
));
2509 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
2515 static void s3c_i2s_write(void *opaque
, target_phys_addr_t addr
,
2518 struct s3c_i2s_state_s
*s
= (struct s3c_i2s_state_s
*) opaque
;
2523 s
->control
= (s
->control
& 0x100) | (value
& 0x03f);
2527 s
->mode
= value
& 0x1ff;
2531 s
->prescaler
= value
& 0x3ff;
2534 s
->fcontrol
= value
& 0xf000;
2538 if (s
->tx_len
&& s
->tx_en
) {
2542 s
->codec_out(s
->opaque
, value
| ((uint32_t) s
->buffer
<< 16));
2544 s
->buffer
= (uint16_t) value
;
2549 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
2553 static CPUReadMemoryFunc
*s3c_i2s_readfn
[] = {
2559 static CPUWriteMemoryFunc
*s3c_i2s_writefn
[] = {
2565 static void s3c_i2s_save(QEMUFile
*f
, void *opaque
)
2567 struct s3c_i2s_state_s
*s
= (struct s3c_i2s_state_s
*) opaque
;
2568 qemu_put_be16s(f
, &s
->control
);
2569 qemu_put_be16s(f
, &s
->mode
);
2570 qemu_put_be16s(f
, &s
->prescaler
);
2571 qemu_put_be16s(f
, &s
->fcontrol
);
2573 qemu_put_be32(f
, s
->tx_en
);
2574 qemu_put_be32(f
, s
->rx_en
);
2575 qemu_put_be32(f
, s
->tx_len
);
2576 qemu_put_be32(f
, s
->rx_len
);
2577 qemu_put_be16(f
, s
->buffer
);
2578 qemu_put_be32(f
, s
->cycle
);
2581 static int s3c_i2s_load(QEMUFile
*f
, void *opaque
, int version_id
)
2583 struct s3c_i2s_state_s
*s
= (struct s3c_i2s_state_s
*) opaque
;
2584 qemu_get_be16s(f
, &s
->control
);
2585 qemu_get_be16s(f
, &s
->mode
);
2586 qemu_get_be16s(f
, &s
->prescaler
);
2587 qemu_get_be16s(f
, &s
->fcontrol
);
2589 s
->tx_en
= qemu_get_be32(f
);
2590 s
->rx_en
= qemu_get_be32(f
);
2591 s
->tx_len
= qemu_get_be32(f
);
2592 s
->rx_len
= qemu_get_be32(f
);
2593 s
->buffer
= qemu_get_be16(f
);
2594 s
->cycle
= qemu_get_be32(f
);
2599 static void s3c_i2s_data_req(void *opaque
, int tx
, int rx
)
2601 struct s3c_i2s_state_s
*s
= (struct s3c_i2s_state_s
*) opaque
;
2607 struct s3c_i2s_state_s
*s3c_i2s_init(target_phys_addr_t base
, qemu_irq
*dma
)
2610 struct s3c_i2s_state_s
*s
= (struct s3c_i2s_state_s
*)
2611 qemu_mallocz(sizeof(struct s3c_i2s_state_s
));
2615 s
->data_req
= s3c_i2s_data_req
;
2619 iomemtype
= cpu_register_io_memory(0, s3c_i2s_readfn
,
2620 s3c_i2s_writefn
, s
);
2621 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
2623 register_savevm("s3c24xx_iis", 0, 0, s3c_i2s_save
, s3c_i2s_load
, s
);
2628 /* Watchdog Timer */
2629 struct s3c_wdt_state_s
{
2630 target_phys_addr_t base
;
2639 static void s3c_wdt_start(struct s3c_wdt_state_s
*s
)
2641 int enable
= s
->control
& (1 << 5);
2642 int prescaler
= (s
->control
>> 8) + 1;
2643 int divider
= prescaler
<< (((s
->control
>> 3) & 3) + 4);
2645 s
->timestamp
= qemu_get_clock(vm_clock
);
2646 qemu_mod_timer(s
->tm
, s
->timestamp
+ muldiv64(divider
* s
->count
,
2647 ticks_per_sec
, S3C_PCLK_FREQ
));
2649 qemu_del_timer(s
->tm
);
2652 static void s3c_wdt_stop(struct s3c_wdt_state_s
*s
)
2654 int prescaler
= (s
->control
>> 8) + 1;
2655 int divider
= prescaler
<< (((s
->control
>> 3) & 3) + 4);
2658 diff
= muldiv64(qemu_get_clock(vm_clock
) - s
->timestamp
, S3C_PCLK_FREQ
,
2659 ticks_per_sec
) / divider
;
2660 s
->count
-= MIN(s
->count
, diff
);
2661 s
->timestamp
= qemu_get_clock(vm_clock
);
2664 static void s3c_wdt_reset(struct s3c_wdt_state_s
*s
)
2666 s
->control
= 0x8021;
2672 static void s3c_wdt_timeout(void *opaque
)
2674 struct s3c_wdt_state_s
*s
= (struct s3c_wdt_state_s
*) opaque
;
2675 if (s
->control
& (1 << 0)) {
2676 qemu_system_reset_request();
2679 if (s
->control
& (1 << 2))
2680 qemu_irq_raise(s
->irq
);
2685 #define S3C_WTCON 0x00 /* Watchdog timer control register */
2686 #define S3C_WTDAT 0x04 /* Watchdog timer data register */
2687 #define S3C_WTCNT 0x08 /* Watchdog timer count register */
2689 static uint32_t s3c_wdt_read(void *opaque
, target_phys_addr_t addr
)
2691 struct s3c_wdt_state_s
*s
= (struct s3c_wdt_state_s
*) opaque
;
2703 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
2709 static void s3c_wdt_write(void *opaque
, target_phys_addr_t addr
,
2712 struct s3c_wdt_state_s
*s
= (struct s3c_wdt_state_s
*) opaque
;
2729 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
2733 static CPUReadMemoryFunc
*s3c_wdt_readfn
[] = {
2739 static CPUWriteMemoryFunc
*s3c_wdt_writefn
[] = {
2745 static void s3c_wdt_save(QEMUFile
*f
, void *opaque
)
2747 struct s3c_wdt_state_s
*s
= (struct s3c_wdt_state_s
*) opaque
;
2750 qemu_put_be16s(f
, &s
->control
);
2751 qemu_put_be16s(f
, &s
->data
);
2752 qemu_put_be16s(f
, &s
->count
);
2753 qemu_put_be64s(f
, &s
->timestamp
);
2756 static int s3c_wdt_load(QEMUFile
*f
, void *opaque
, int version_id
)
2758 struct s3c_wdt_state_s
*s
= (struct s3c_wdt_state_s
*) opaque
;
2760 qemu_get_be16s(f
, &s
->control
);
2761 qemu_get_be16s(f
, &s
->data
);
2762 qemu_get_be16s(f
, &s
->count
);
2763 qemu_get_be64s(f
, &s
->timestamp
);
2769 struct s3c_wdt_state_s
*s3c_wdt_init(target_phys_addr_t base
, qemu_irq irq
)
2772 struct s3c_wdt_state_s
*s
= (struct s3c_wdt_state_s
*)
2773 qemu_mallocz(sizeof(struct s3c_wdt_state_s
));
2777 s
->tm
= qemu_new_timer(vm_clock
, s3c_wdt_timeout
, s
);
2781 iomemtype
= cpu_register_io_memory(0, s3c_wdt_readfn
,
2782 s3c_wdt_writefn
, s
);
2783 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
2785 register_savevm("s3c24xx_wdt", 0, 0, s3c_wdt_save
, s3c_wdt_load
, s
);
2792 target_phys_addr_t base
;
2795 } s3c2410_uart
[] = {
2798 { S3C_PICS_RXD0
, S3C_PICS_TXD0
, S3C_PICS_ERR0
},
2803 { S3C_PICS_RXD1
, S3C_PICS_TXD1
, S3C_PICS_ERR1
},
2808 { S3C_PICS_RXD2
, S3C_PICS_TXD2
, S3C_PICS_ERR2
},
2811 { 0, { 0, 0, 0 }, { 0 } }
2814 /* General CPU reset */
2815 static void s3c2410_reset(void *opaque
)
2817 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
2820 s3c_pic_reset(s
->pic
);
2821 s3c_dma_reset(s
->dma
);
2822 s3c_gpio_reset(s
->io
);
2823 s3c_lcd_reset(s
->lcd
);
2824 s3c_timers_reset(s
->timers
);
2825 s3c_mmci_reset(s
->mmci
);
2826 s3c_adc_reset(s
->adc
);
2827 s3c_i2c_reset(s
->i2c
);
2828 s3c_i2s_reset(s
->i2s
);
2829 s3c_rtc_reset(s
->rtc
);
2830 s3c_spi_reset(s
->spi
);
2831 s3c_udc_reset(s
->udc
);
2832 s3c_wdt_reset(s
->wdt
);
2833 s3c_clkpwr_reset(s
);
2835 for (i
= 0; s3c2410_uart
[i
].base
; i
++)
2836 s3c_uart_reset(s
->uart
[i
]);
2840 /* Initialise an S3C2410A microprocessor. */
2841 struct s3c_state_s
*s3c24xx_init(uint32_t cpu_id
, unsigned int sdram_size
, DisplayState
*ds
,
2844 struct s3c_state_s
*s
;
2846 s
= (struct s3c_state_s
*) qemu_mallocz(sizeof(struct s3c_state_s
));
2850 s
->env
= cpu_init("arm920t");
2851 register_savevm("s3c24xx", 0, 0,
2852 cpu_save
, cpu_load
, s
->env
);
2854 cpu_register_physical_memory(S3C_RAM_BASE
, sdram_size
,
2855 qemu_ram_alloc(sdram_size
) | IO_MEM_RAM
);
2857 /* If OM pins are 00, SRAM is mapped at 0x0 instead. */
2858 cpu_register_physical_memory(S3C_SRAM_BASE
, S3C_SRAM_SIZE
,
2859 qemu_ram_alloc(S3C_SRAM_SIZE
) | IO_MEM_RAM
);
2861 s
->mc_base
= 0x48000000;
2863 iomemtype
= cpu_register_io_memory(0, s3c_mc_readfn
, s3c_mc_writefn
, s
);
2864 cpu_register_physical_memory(s
->mc_base
, 0xffffff, iomemtype
);
2865 register_savevm("s3c24xx_mc", 0, 0, s3c_mc_save
, s3c_mc_load
, s
);
2867 s
->pic
= s3c_pic_init(0x4a000000, arm_pic_init_cpu(s
->env
));
2868 s
->irq
= s3c_pic_get(s
->pic
);
2870 s
->dma
= s3c_dma_init(0x4b000000, &s
->irq
[S3C_PIC_DMA0
]);
2871 s
->drq
= s3c_dma_get(s
->dma
);
2873 s
->clkpwr_base
= 0x4c000000;
2874 s3c_clkpwr_reset(s
);
2875 iomemtype
= cpu_register_io_memory(0, s3c_clkpwr_readfn
,
2876 s3c_clkpwr_writefn
, s
);
2877 cpu_register_physical_memory(s
->clkpwr_base
, 0xffffff, iomemtype
);
2878 register_savevm("s3c24xx_clkpwr", 0, 0,
2879 s3c_clkpwr_save
, s3c_clkpwr_load
, s
);
2881 s
->lcd
= s3c_lcd_init(0x4d000000, ds
, s
->irq
[S3C_PIC_LCD
]);
2883 s
->nand_base
= 0x4e000000;
2885 iomemtype
= cpu_register_io_memory(0, s3c_nand_readfn
,
2886 s3c_nand_writefn
, s
);
2887 cpu_register_physical_memory(s
->nand_base
, 0xffffff, iomemtype
);
2888 register_savevm("s3c24xx_nand", 0, 0, s3c_nand_save
, s3c_nand_load
, s
);
2890 for (i
= 0; s3c2410_uart
[i
].base
; i
++) {
2891 s
->uart
[i
] = s3c_uart_init(s3c2410_uart
[i
].base
,
2892 &s
->irq
[s3c2410_uart
[i
].irq
[0]],
2893 &s
->drq
[s3c2410_uart
[i
].dma
[0]]);
2895 s3c_uart_attach(s
->uart
[i
], serial_hds
[i
]);
2898 s
->timers
= s3c_timers_init(0x51000000, &s
->irq
[S3C_PIC_TIMER0
], s
->drq
);
2900 s
->udc
= s3c_udc_init(0x52000000, s
->irq
[S3C_PIC_USBD
], s
->drq
);
2902 s
->wdt
= s3c_wdt_init(0x53000000, s
->irq
[S3C_PIC_WDT
]);
2904 s
->i2c
= s3c_i2c_init(0x54000000, s
->irq
[S3C_PIC_IIC
]);
2906 s
->i2s
= s3c_i2s_init(0x55000000, s
->drq
);
2908 s
->io
= s3c_gpio_init(0x56000000, s
->irq
, s
->cpu_id
);
2910 s
->rtc
= s3c_rtc_init(0x57000000, s
->irq
[S3C_PIC_RTC
]);
2912 s
->adc
= s3c_adc_init(0x58000000, s
->irq
[S3C_PICS_ADC
],
2913 s
->irq
[S3C_PICS_TC
]);
2915 s
->spi
= s3c_spi_init(0x59000000,
2916 s
->irq
[S3C_PIC_SPI0
], s
->drq
[S3C_RQ_SPI0
],
2917 s
->irq
[S3C_PIC_SPI1
], s
->drq
[S3C_RQ_SPI1
], s
->io
);
2919 s
->mmci
= s3c_mmci_init(0x5a000000, 0x2410, mmc
,
2920 s
->irq
[S3C_PIC_SDI
], s
->drq
);
2923 usb_ohci_init_pxa(0x49000000, 3, -1, s
->irq
[S3C_PIC_USBH
]);
2926 qemu_register_reset(s3c2410_reset
, s
);
2928 s3c_nand_setwp(s
, 1);
2930 /* Power on reset */
2931 s3c_gpio_setpwrstat(s
->io
, 1);