2 * Samsung S3C2410A RISC Microprocessor support (ARM920T based SoC).
4 * Copyright (c) 2007 OpenMoko, Inc.
5 * Author: Andrzej Zaborowski <andrew@openedhand.com>
6 * With: Michel Pollet <buserror@gmail.com>
8 * This code is licenced under the GNU GPL v2.
12 #include "qemu-timer.h"
13 #include "qemu-char.h"
22 /* Interrupt controller */
23 struct s3c_pic_state_s
{
24 target_phys_addr_t base
;
38 static void s3c_pic_update(struct s3c_pic_state_s
*s
)
40 qemu_set_irq(s
->parent_pic
[ARM_PIC_CPU_FIQ
],
41 s
->srcpnd
& s
->intmod
);
42 qemu_set_irq(s
->parent_pic
[ARM_PIC_CPU_IRQ
],
43 s
->intpnd
& ~s
->intmsk
& ~s
->intmod
);
47 * Performs interrupt arbitration and notifies the CPU.
49 * Since it's a complex logic which cannot be relied on by the OS
50 * anyway - first because real hardware doesn't do it accurately,
51 * second because it only matters when interrupts occur at the
52 * same time which normally can't be predicted - we use a simpler
53 * version for non-debug runs.
56 static const uint32_t s3c_arbmsk
[6] = {
65 # define S3C_ARB_SEL(i) ((s->priority >> (7 + (i << 1))) & 3)
66 # define S3C_ARB_MODE(i) ((s->priority >> i) & 1)
67 # define S3C_ARB_SEL_SET(i, v) \
68 s->priority &= ~(3 << (7 + (i << 1))); \
69 s->priority |= v << (7 + (i << 1));
71 static void s3c_pic_arbitrate(struct s3c_pic_state_s
*s
)
73 uint32_t pnd
= s
->srcpnd
& ~s
->intmsk
& ~s
->intmod
;
75 if (s
->intpnd
|| !pnd
) {
80 if (pnd
& s3c_arbmsk
[0]) {
83 } else if (pnd
& 0x0ffffff0) {
86 if (!(pnd
& s3c_arbmsk
[1 + (i
& 3)]))
87 if (!(pnd
& s3c_arbmsk
[1 + (++ i
& 3)]))
88 if (!(pnd
& s3c_arbmsk
[1 + (++ i
& 3)]))
92 S3C_ARB_SEL_SET(6, ((i
+ 1) & 3));
93 offset
= (i
& 3) * 6 + 4;
94 if (pnd
& (1 << offset
))
96 else if (!(pnd
& (0x1f << offset
))) {
108 i
= S3C_ARB_SEL(arb
);
110 if (!(pnd
& (1 << (i
& 3))))
111 if (!(pnd
& (1 << (++ i
& 3))))
112 if (!(pnd
& (1 << (++ i
& 3))))
115 if (S3C_ARB_MODE(arb
))
116 S3C_ARB_SEL_SET(arb
, ((i
+ 1) & 3));
119 s
->intoffset
= offset
;
120 s
->intpnd
= 1 << offset
;
124 inline static void s3c_pic_arbitrate(struct s3c_pic_state_s
*s
)
126 uint32_t pnd
= s
->srcpnd
& ~s
->intmsk
& ~s
->intmod
;
127 if (pnd
&& !s
->intpnd
)
128 s
->intpnd
= 1 << (s
->intoffset
= ffs(pnd
) - 1);
133 static const int s3c_sub_src_map
[] = {
134 [S3C_PICS_RXD0
& 31] = S3C_PIC_UART0
,
135 [S3C_PICS_TXD0
& 31] = S3C_PIC_UART0
,
136 [S3C_PICS_ERR0
& 31] = S3C_PIC_UART0
,
137 [S3C_PICS_RXD1
& 31] = S3C_PIC_UART1
,
138 [S3C_PICS_TXD1
& 31] = S3C_PIC_UART1
,
139 [S3C_PICS_ERR1
& 31] = S3C_PIC_UART1
,
140 [S3C_PICS_RXD2
& 31] = S3C_PIC_UART2
,
141 [S3C_PICS_TXD2
& 31] = S3C_PIC_UART2
,
142 [S3C_PICS_ERR2
& 31] = S3C_PIC_UART2
,
143 [S3C_PICS_TC
& 31] = S3C_PIC_ADC
,
144 [S3C_PICS_ADC
& 31] = S3C_PIC_ADC
,
147 static void s3c_pic_subupdate(struct s3c_pic_state_s
*s
)
150 const int *sub
= &s3c_sub_src_map
[-1];
151 uint32_t pnd
= s
->subsrcpnd
& ~s
->intsubmsk
;
152 while ((next
= ffs(pnd
))) {
155 s
->srcpnd
|= 1 << *sub
;
157 s3c_pic_arbitrate(s
);
160 static void s3c_pic_set_irq(void *opaque
, int irq
, int req
)
162 struct s3c_pic_state_s
*s
= (struct s3c_pic_state_s
*) opaque
;
164 /* This interrupt controller doesn't clear any request signals
165 * or register bits automatically. */
171 s
->subsrcpnd
|= 1 << irq
;
172 if (s
->intsubmsk
& (1 << irq
))
175 irq
= s3c_sub_src_map
[irq
];
177 s
->srcpnd
|= (mask
= 1 << irq
);
180 if (s
->intmod
& mask
)
181 qemu_irq_raise(s
->parent_pic
[ARM_PIC_CPU_FIQ
]);
182 else if (!s
->intpnd
&& !(s
->intmsk
& mask
)) {
184 s3c_pic_arbitrate(s
);
188 qemu_irq_raise(s
->parent_pic
[ARM_PIC_CPU_IRQ
]);
193 static void s3c_pic_reset(struct s3c_pic_state_s
*s
)
197 s
->intmsk
= 0xffffffff;
202 s
->intsubmsk
= 0x7ff;
206 #define S3C_SRCPND 0x00 /* Source Pending register */
207 #define S3C_INTMOD 0x04 /* Source Mode register */
208 #define S3C_INTMSK 0x08 /* Interrupt Mask register */
209 #define S3C_PRIORITY 0x0c /* Priority register */
210 #define S3C_INTPND 0x10 /* Interrupt Pending register */
211 #define S3C_INTOFFSET 0x14 /* Interrupt Offset register */
212 #define S3C_SUBSRCPND 0x18 /* Sub Source Pending register */
213 #define S3C_INTSUBMSK 0x1c /* Interrupt Sub Mask register */
215 static uint32_t s3c_pic_read(void *opaque
, target_phys_addr_t addr
)
217 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
;
251 if (value
& s
->intmod
)
255 if (s
->intpnd
& value
) {
258 s3c_pic_arbitrate(s
);
263 if (s
->intpnd
& value
) {
267 s3c_pic_arbitrate(s
);
276 s
->subsrcpnd
&= ~value
;
279 s
->intsubmsk
= value
;
280 s3c_pic_subupdate(s
);
283 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
287 static CPUReadMemoryFunc
*s3c_pic_readfn
[] = {
293 static CPUWriteMemoryFunc
*s3c_pic_writefn
[] = {
299 static void s3c_pic_save(QEMUFile
*f
, void *opaque
)
301 struct s3c_pic_state_s
*s
= (struct s3c_pic_state_s
*) opaque
;
302 qemu_put_be32s(f
, &s
->srcpnd
);
303 qemu_put_be32s(f
, &s
->intpnd
);
304 qemu_put_be32s(f
, &s
->intmsk
);
305 qemu_put_be32s(f
, &s
->intmod
);
306 qemu_put_be32s(f
, &s
->priority
);
307 qemu_put_be32s(f
, &s
->subsrcpnd
);
308 qemu_put_be32s(f
, &s
->intsubmsk
);
309 qemu_put_be32(f
, s
->intoffset
);
312 static int s3c_pic_load(QEMUFile
*f
, void *opaque
, int version_id
)
314 struct s3c_pic_state_s
*s
= (struct s3c_pic_state_s
*) opaque
;
315 qemu_get_be32s(f
, &s
->srcpnd
);
316 qemu_get_be32s(f
, &s
->intpnd
);
317 qemu_get_be32s(f
, &s
->intmsk
);
318 qemu_get_be32s(f
, &s
->intmod
);
319 qemu_get_be32s(f
, &s
->priority
);
320 qemu_get_be32s(f
, &s
->subsrcpnd
);
321 qemu_get_be32s(f
, &s
->intsubmsk
);
322 s
->intoffset
= qemu_get_be32(f
);
327 struct s3c_pic_state_s
*s3c_pic_init(target_phys_addr_t base
,
331 struct s3c_pic_state_s
*s
= (struct s3c_pic_state_s
*)
332 qemu_mallocz(sizeof(struct s3c_pic_state_s
));
335 s
->parent_pic
= arm_pic
;
336 s
->irqs
= qemu_allocate_irqs(s3c_pic_set_irq
, s
, S3C_PIC_MAX
);
340 iomemtype
= cpu_register_io_memory(0, s3c_pic_readfn
,
342 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
344 register_savevm("s3c24xx_pic", 0, 0, s3c_pic_save
, s3c_pic_load
, s
);
349 qemu_irq
*s3c_pic_get(struct s3c_pic_state_s
*s
)
354 /* Memory controller */
355 #define S3C_BWSCON 0x00 /* Bus Width & Wait Control register */
356 #define S3C_BANKCON0 0x04 /* Bank 0 Control register */
357 #define S3C_BANKCON1 0x08 /* Bank 1 Control register */
358 #define S3C_BANKCON2 0x0c /* Bank 2 Control register */
359 #define S3C_BANKCON3 0x10 /* Bank 3 Control register */
360 #define S3C_BANKCON4 0x14 /* Bank 4 Control register */
361 #define S3C_BANKCON5 0x18 /* Bank 5 Control register */
362 #define S3C_BANKCON6 0x1c /* Bank 6 Control register */
363 #define S3C_BANKCON7 0x20 /* Bank 7 Control register */
364 #define S3C_REFRESH 0x24 /* SDRAM Refresh Control register */
365 #define S3C_BANKSIZE 0x28 /* Flexible Bank Size register */
366 #define S3C_MRSRB6 0x2c /* Bank 6 Mode Set register */
367 #define S3C_MRSRB7 0x30 /* Bank 6 Mode Set register */
369 static void s3c_mc_reset(struct s3c_state_s
*s
)
371 s
->mc_regs
[S3C_BWSCON
>> 2] = 0x0000000;
372 s
->mc_regs
[S3C_BANKCON0
>> 2] = 0x0700;
373 s
->mc_regs
[S3C_BANKCON1
>> 2] = 0x0700;
374 s
->mc_regs
[S3C_BANKCON2
>> 2] = 0x0700;
375 s
->mc_regs
[S3C_BANKCON3
>> 2] = 0x0700;
376 s
->mc_regs
[S3C_BANKCON4
>> 2] = 0x0700;
377 s
->mc_regs
[S3C_BANKCON5
>> 2] = 0x0700;
378 s
->mc_regs
[S3C_BANKCON6
>> 2] = 0x18008;
379 s
->mc_regs
[S3C_BANKCON7
>> 2] = 0x18008;
380 s
->mc_regs
[S3C_REFRESH
>> 2] = 0xac0000;
381 s
->mc_regs
[S3C_BANKSIZE
>> 2] = 0x2;
382 s
->mc_regs
[S3C_MRSRB6
>> 2] = 0x00;
383 s
->mc_regs
[S3C_MRSRB7
>> 2] = 0x00;
386 static uint32_t s3c_mc_read(void *opaque
, target_phys_addr_t addr
)
388 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
391 case S3C_BWSCON
... S3C_MRSRB7
:
392 return s
->mc_regs
[addr
>> 2];
394 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
400 static void s3c_mc_write(void *opaque
, target_phys_addr_t addr
,
403 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
406 case S3C_BWSCON
... S3C_MRSRB7
:
407 s
->mc_regs
[addr
>> 2] = value
;
410 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
414 static CPUReadMemoryFunc
*s3c_mc_readfn
[] = {
420 static CPUWriteMemoryFunc
*s3c_mc_writefn
[] = {
426 static void s3c_mc_save(QEMUFile
*f
, void *opaque
)
428 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
430 for (i
= 0; i
< 13; i
++)
431 qemu_put_be32s(f
, &s
->mc_regs
[i
]);
434 static int s3c_mc_load(QEMUFile
*f
, void *opaque
, int version_id
)
436 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
438 for (i
= 0; i
< 13; i
++)
439 qemu_get_be32s(f
, &s
->mc_regs
[i
]);
443 /* Clock & power management */
444 #define S3C_LOCKTIME 0x00 /* PLL Lock Time Count register */
445 #define S3C_MPLLCON 0x04 /* MPLL Configuration register */
446 #define S3C_UPLLCON 0x08 /* UPLL Configuration register */
447 #define S3C_CLKCON 0x0c /* Clock Generator Control register */
448 #define S3C_CLKSLOW 0x10 /* Slow Clock Control register */
449 #define S3C_CLKDIVN 0x14 /* Clock Divider Control register */
451 #define S3C2440_CAMDIVN 0x18 /* Camera Clock Divider register */
453 static void s3c_clkpwr_update(struct s3c_state_s
*s
)
455 uint32_t mpll
= s
->clkpwr_regs
[S3C_MPLLCON
>> 2],
456 ratio
= s
->clkpwr_regs
[S3C_CLKDIVN
>> 2];
457 uint32_t mdiv
= (mpll
>> 12) & 0xff,
458 pdiv
= (mpll
>> 4) & 0x3f,
461 s
->clock
.clk
= ((mdiv
+ 8) * s
->clock
.xtal
* 2) /
462 ((pdiv
+ 2) * (1 << sdiv
));
464 switch( (ratio
& 0x6) >> 1 ) {
466 s
->clock
.hclk
= s
->clock
.clk
;
469 s
->clock
.hclk
= s
->clock
.clk
/2;
472 s
->clock
.hclk
= s
->clock
.clk
/4;
475 s
->clock
.hclk
= s
->clock
.clk
/3;
478 switch ( ratio
&0x1) {
480 s
->clock
.pclk
= s
->clock
.hclk
;
483 s
->clock
.pclk
= s
->clock
.hclk
/2;
488 static void s3c_clkpwr_reset(struct s3c_state_s
*s
)
490 s
->clkpwr_regs
[S3C_LOCKTIME
>> 2] = 0x00ffffff;
491 s
->clkpwr_regs
[S3C_MPLLCON
>> 2] = 0x0005c080;
492 s
->clkpwr_regs
[S3C_UPLLCON
>> 2] = 0x00028080;
493 s
->clkpwr_regs
[S3C_CLKCON
>> 2] = 0x0007fff0;
494 s
->clkpwr_regs
[S3C_CLKSLOW
>> 2] = 0x00000004;
495 s
->clkpwr_regs
[S3C_CLKDIVN
>> 2] = 0x00000000;
496 s
->clkpwr_regs
[S3C2440_CAMDIVN
>> 2] = 0x00000000;
497 s3c_clkpwr_update(s
);
500 static uint32_t s3c_clkpwr_read(void *opaque
, target_phys_addr_t addr
)
502 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
505 case S3C_LOCKTIME
... S3C_CLKDIVN
:
506 return s
->clkpwr_regs
[addr
>> 2];
507 case S3C2440_CAMDIVN
:
508 if (s
->cpu_id
== S3C_CPU_2440
)
509 return s
->clkpwr_regs
[addr
>> 2];
511 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
517 static void s3c_clkpwr_write(void *opaque
, target_phys_addr_t addr
,
520 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
527 s
->clkpwr_regs
[addr
>> 2] = value
;
528 s3c_clkpwr_update(s
);
531 if (value
& (1 << 3)) {
532 cpu_interrupt(s
->env
, CPU_INTERRUPT_HALT
);
533 printf("%s: processor powered off\n", __FUNCTION__
);
534 s3c_gpio_setpwrstat(s
->io
, 2);
537 s
->env
->regs
[15] = 0; /* XXX */
540 if (value
& (1 << 2)) /* Normal IDLE mode */
541 cpu_interrupt(s
->env
, CPU_INTERRUPT_HALT
);
542 if ((s
->clkpwr_regs
[addr
>> 2] ^ value
) & 1)
543 printf("%s: SPECIAL mode %s\n", __FUNCTION__
,
544 (value
& 1) ? "on" : "off");
545 s
->clkpwr_regs
[addr
>> 2] = value
;
548 if ((s
->clkpwr_regs
[addr
>> 2] ^ value
) & (1 << 4))
549 printf("%s: SLOW mode %s\n", __FUNCTION__
,
550 (value
& (1 << 4)) ? "on" : "off");
551 s
->clkpwr_regs
[addr
>> 2] = value
;
553 case S3C2440_CAMDIVN
:
554 if (s
->cpu_id
== S3C_CPU_2440
) {
555 s
->clkpwr_regs
[addr
>> 2] = value
;
559 printf("%s: Bad register 0x%x (cpu %08x)\n", __FUNCTION__
, /*(unsigned long)*/addr
, s
->cpu_id
);
563 static CPUReadMemoryFunc
*s3c_clkpwr_readfn
[] = {
569 static CPUWriteMemoryFunc
*s3c_clkpwr_writefn
[] = {
575 static void s3c_clkpwr_save(QEMUFile
*f
, void *opaque
)
577 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
579 for (i
= 0; i
< 7; i
++)
580 qemu_put_be32s(f
, &s
->clkpwr_regs
[i
]);
583 static int s3c_clkpwr_load(QEMUFile
*f
, void *opaque
, int version_id
)
585 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
587 for (i
= 0; i
< 7; i
++)
588 qemu_get_be32s(f
, &s
->clkpwr_regs
[i
]);
593 #define S3C_DMA_CH_N 4
595 struct s3c_dma_ch_state_s
;
596 struct s3c_dma_state_s
{ /* Modelled as an interrupt controller */
597 target_phys_addr_t base
;
599 struct s3c_dma_ch_state_s
{
615 static inline void s3c_dma_ch_run(struct s3c_dma_state_s
*s
,
616 struct s3c_dma_ch_state_s
*ch
)
620 width
= 1 << ((ch
->con
>> 20) & 3); /* DSZ */
621 burst
= (ch
->con
& (1 << 28)) ? 4 : 1; /* TSZ */
623 while (!ch
->running
&& ch
->curr_tc
> 0 && ch
->req
&&
624 (ch
->mask
& (1 << 1))) { /* ON_OFF */
625 if (width
> sizeof(buffer
)) {
626 printf("%s: wrong access width\n", __FUNCTION__
);
630 while (ch
->curr_tc
--) {
631 for (t
= 0; t
< burst
; t
++) {
632 cpu_physical_memory_read(ch
->csrc
, buffer
, width
);
633 cpu_physical_memory_write(ch
->cdst
, buffer
, width
);
635 if (!(ch
->isrcc
& 1)) /* INT */
637 if (!(ch
->idstc
& 1)) /* INT */
641 if (!(ch
->con
& (1 << 27)) && !ch
->req
) /* SERVMODE */
646 if (!(ch
->con
& (1 << 23))) { /* SWHW_SEL */
650 if (ch
->curr_tc
<= 0) {
651 if (ch
->con
& (1 << 22)) /* RELOAD */
652 ch
->mask
&= ~(1 << 1); /* ON_OFF */
654 if (!(ch
->con
& (1 << 23))) { /* SWHW_SEL */
655 printf("%s: auto-reload software controlled transfer\n",
659 ch
->csrc
= ch
->isrc
; /* S_ADDR */
660 ch
->cdst
= ch
->idst
; /* D_ADDR */
661 ch
->curr_tc
= ch
->con
& 0xfffff; /* TC */
662 ch
->con
|= 1 << 22; /* ON_OFF */
665 if (ch
->con
& (1 << 31)) /* DMD_HS */
668 if (ch
->con
& (1 << 29)) { /* INT */
669 qemu_irq_raise(ch
->intr
);
670 /* Give the system a chance to respond. */
677 static void s3c_dma_reset(struct s3c_dma_state_s
*s
)
680 for (i
= 0; i
< S3C_DMA_CH_N
; i
++) {
681 s
->ch
[i
].curr_tc
= 0;
695 static void s3c_dma_dreq(void *opaque
, int line
, int req
)
697 struct s3c_dma_state_s
*s
= (struct s3c_dma_state_s
*) opaque
;
698 struct s3c_dma_ch_state_s
*ch
= &s
->ch
[line
>> 4];
700 if (ch
->con
& (1 << 23)) /* SWHW_SEL */
701 if (((ch
->con
>> 24) & 7) == (line
& 7)) { /* HWSRCSEL */
703 s3c_dma_ch_run(s
, ch
);
707 #define S3C_DISRC 0x00 /* DMA Initial Source register */
708 #define S3C_DISRCC 0x04 /* DMA Initial Source Control register */
709 #define S3C_DIDST 0x08 /* DMA Initial Destination register */
710 #define S3C_DIDSTC 0x0c /* DMA Initial Destination Control register */
711 #define S3C_DCON 0x10 /* DMA Control register */
712 #define S3C_DSTAT 0x14 /* DMA Count register */
713 #define S3C_DCSRC 0x18 /* DMA Current Source register */
714 #define S3C_DCDST 0x1c /* DMA Current Destination register */
715 #define S3C_DMASKTRIG 0x20 /* DMA Mask Trigger register */
717 static uint32_t s3c_dma_read(void *opaque
, target_phys_addr_t addr
)
719 struct s3c_dma_state_s
*s
= (struct s3c_dma_state_s
*) opaque
;
720 struct s3c_dma_ch_state_s
*ch
= 0;
722 if (addr
>= 0 && addr
<= (S3C_DMA_CH_N
<< 6)) {
723 ch
= &s
->ch
[addr
>> 6];
747 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
753 static void s3c_dma_write(void *opaque
, target_phys_addr_t addr
,
756 struct s3c_dma_state_s
*s
= (struct s3c_dma_state_s
*) opaque
;
757 struct s3c_dma_ch_state_s
*ch
= 0;
759 if (addr
>= 0 && addr
<= (S3C_DMA_CH_N
<< 6)) {
760 ch
= &s
->ch
[addr
>> 6];
781 if (~ch
->mask
& value
& (1 << 1)) { /* ON_OFF */
782 ch
->curr_tc
= ch
->con
& 0xfffff; /* TC */
783 ch
->csrc
= ch
->isrc
; /* S_ADDR */
784 ch
->cdst
= ch
->idst
; /* D_ADDR */
788 if (value
& (1 << 2)) { /* STOP */
789 ch
->mask
&= ~(3 << 1); /* ON_OFF */
790 } else if (!(ch
->con
& (1 << 23))) { /* SWHW_SEL */
791 ch
->req
= value
& 1; /* SW_TRIG */
792 s3c_dma_ch_run(s
, ch
);
796 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
800 static CPUReadMemoryFunc
*s3c_dma_readfn
[] = {
806 static CPUWriteMemoryFunc
*s3c_dma_writefn
[] = {
812 static void s3c_dma_save(QEMUFile
*f
, void *opaque
)
814 struct s3c_dma_state_s
*s
= (struct s3c_dma_state_s
*) opaque
;
816 for (i
= 0; i
< S3C_DMA_CH_N
; i
++) {
817 qemu_put_be32(f
, s
->ch
[i
].curr_tc
);
818 qemu_put_be32(f
, s
->ch
[i
].req
);
819 qemu_put_be32s(f
, &s
->ch
[i
].con
);
820 qemu_put_be32s(f
, &s
->ch
[i
].isrc
);
821 qemu_put_be32s(f
, &s
->ch
[i
].isrcc
);
822 qemu_put_be32s(f
, &s
->ch
[i
].idst
);
823 qemu_put_be32s(f
, &s
->ch
[i
].idstc
);
824 qemu_put_be32s(f
, &s
->ch
[i
].csrc
);
825 qemu_put_be32s(f
, &s
->ch
[i
].cdst
);
826 qemu_put_be32s(f
, &s
->ch
[i
].mask
);
830 static int s3c_dma_load(QEMUFile
*f
, void *opaque
, int version_id
)
832 struct s3c_dma_state_s
*s
= (struct s3c_dma_state_s
*) opaque
;
834 for (i
= 0; i
< S3C_DMA_CH_N
; i
++) {
835 s
->ch
[i
].curr_tc
= qemu_get_be32(f
);
836 s
->ch
[i
].req
= qemu_get_be32(f
);
837 qemu_get_be32s(f
, &s
->ch
[i
].con
);
838 qemu_get_be32s(f
, &s
->ch
[i
].isrc
);
839 qemu_get_be32s(f
, &s
->ch
[i
].isrcc
);
840 qemu_get_be32s(f
, &s
->ch
[i
].idst
);
841 qemu_get_be32s(f
, &s
->ch
[i
].idstc
);
842 qemu_get_be32s(f
, &s
->ch
[i
].csrc
);
843 qemu_get_be32s(f
, &s
->ch
[i
].cdst
);
844 qemu_get_be32s(f
, &s
->ch
[i
].mask
);
849 struct s3c_dma_state_s
*s3c_dma_init(target_phys_addr_t base
, qemu_irq
*pic
)
852 struct s3c_dma_state_s
*s
= (struct s3c_dma_state_s
*)
853 qemu_mallocz(sizeof(struct s3c_dma_state_s
));
856 s
->ch
[0].intr
= pic
[0];
857 s
->ch
[1].intr
= pic
[1];
858 s
->ch
[2].intr
= pic
[2];
859 s
->ch
[3].intr
= pic
[3];
860 s
->drqs
= qemu_allocate_irqs(s3c_dma_dreq
, s
, S3C_RQ_MAX
);
864 iomemtype
= cpu_register_io_memory(0, s3c_dma_readfn
,
866 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
868 register_savevm("s3c24xx_dma", 0, 0, s3c_dma_save
, s3c_dma_load
, s
);
873 qemu_irq
*s3c_dma_get(struct s3c_dma_state_s
*s
)
878 /* PWM timers controller */
879 struct s3c_timer_state_s
;
880 struct s3c_timers_state_s
{
881 struct s3c_freq_s
* freq
;
882 target_phys_addr_t base
;
885 struct s3c_timer_state_s
{
887 struct s3c_timers_state_s
*s
;
894 gpio_handler_t cmp_cb
;
898 uint16_t compareb
[4];
904 static const int s3c_tm_bits
[] = { 0, 8, 12, 16, 20 };
906 static uint16_t s3c_timers_get(struct s3c_timers_state_s
*s
, int tm
)
909 if (!s
->timer
[tm
].running
)
910 return s
->timer
[tm
].count
;
912 elapsed
= muldiv64(qemu_get_clock(vm_clock
) - s
->timer
[tm
].reload
,
913 s
->timer
[tm
].divider
, ticks_per_sec
);
914 if (unlikely(elapsed
> s
->timer
[tm
].count
))
915 return s
->timer
[tm
].count
;
917 return s
->timer
[tm
].count
- elapsed
;
920 static void s3c_timers_stop(struct s3c_timers_state_s
*s
, int tm
)
922 s
->timer
[tm
].count
= s3c_timers_get(s
, tm
);
923 s
->timer
[tm
].running
= 0;
926 static void s3c_timers_start(struct s3c_timers_state_s
*s
, int tm
)
928 if (s
->timer
[tm
].running
)
931 s
->timer
[tm
].divider
= s
->freq
->pclk
>>
932 (((s
->config
[1] >> (tm
* 4)) & 3) + 1);
934 s
->timer
[tm
].divider
/= ((s
->config
[0] >> 0) & 0xff) + 1;
936 s
->timer
[tm
].divider
/= ((s
->config
[0] >> 8) & 0xff) + 1;
937 s
->timer
[tm
].running
= 1;
938 s
->timer
[tm
].reload
= qemu_get_clock(vm_clock
);
939 qemu_mod_timer(s
->timer
[tm
].t
,
940 s
->timer
[tm
].reload
+ muldiv64(s
->timer
[tm
].count
,
941 ticks_per_sec
, s
->timer
[tm
].divider
));
944 static void s3c_timers_reset(struct s3c_timers_state_s
*s
)
947 s
->config
[0] = 0x00000000;
948 s
->config
[1] = 0x00000000;
949 s
->control
= 0x00000000;
951 for (i
= 0; i
< 5; i
++) {
952 if (s
->timer
[i
].running
)
953 s3c_timers_stop(s
, i
);
954 s
->countb
[i
] = 0x0000;
955 s
->timer
[i
].count
= 0;
957 for (i
= 0; i
< 4; i
++)
958 s
->compareb
[i
] = 0x0000;
961 static void s3c_timers_tick(void *opaque
)
963 struct s3c_timer_state_s
*t
= (struct s3c_timer_state_s
*) opaque
;
964 struct s3c_timers_state_s
*s
= t
->s
;
968 if (((s
->config
[1] >> 20) & 0xf) == t
->n
+ 1) {
969 qemu_irq_raise(s
->dma
[S3C_RQ_TIMER0
]); /* TODO */
970 qemu_irq_raise(s
->dma
[S3C_RQ_TIMER1
]);
971 qemu_irq_raise(s
->dma
[S3C_RQ_TIMER2
]);
973 qemu_irq_raise(t
->irq
);
978 if (s
->control
& (1 << ((t
->n
== 4) ? 22 : (s3c_tm_bits
[t
->n
] + 3)))) {
980 t
->count
= s
->countb
[t
->n
];
981 s3c_timers_start(s
, t
->n
);
983 s
->control
&= ~(1 << s3c_tm_bits
[t
->n
]);
986 #define S3C_TCFG0 0x00 /* Timer Configuration register 0 */
987 #define S3C_TCFG1 0x04 /* Timer Configuration register 1 */
988 #define S3C_TCON 0x08 /* Timer Control register */
989 #define S3C_TCNTB0 0x0c /* Timer 0 Count Buffer register */
990 #define S3C_TCMPB0 0x10 /* Timer 0 Compare Buffer register */
991 #define S3C_TCNTO0 0x14 /* Timer 0 Count Observation register */
992 #define S3C_TCNTB1 0x18 /* Timer 1 Count Buffer register */
993 #define S3C_TCMPB1 0x1c /* Timer 1 Compare Buffer register */
994 #define S3C_TCNTO1 0x20 /* Timer 1 Count Observation register */
995 #define S3C_TCNTB2 0x24 /* Timer 2 Count Buffer register */
996 #define S3C_TCMPB2 0x28 /* Timer 2 Compare Buffer register */
997 #define S3C_TCNTO2 0x2c /* Timer 2 Count Observation register */
998 #define S3C_TCNTB3 0x30 /* Timer 3 Count Buffer register */
999 #define S3C_TCMPB3 0x34 /* Timer 3 Compare Buffer register */
1000 #define S3C_TCNTO3 0x38 /* Timer 3 Count Observation register */
1001 #define S3C_TCNTB4 0x3c /* Timer 4 Count Buffer register */
1002 #define S3C_TCNTO4 0x40 /* Timer 4 Count Observation register */
1004 static uint32_t s3c_timers_read(void *opaque
, target_phys_addr_t addr
)
1006 struct s3c_timers_state_s
*s
= (struct s3c_timers_state_s
*) opaque
;
1011 return s
->config
[0];
1013 return s
->config
[1];
1016 case S3C_TCMPB3
: tm
++;
1017 case S3C_TCMPB2
: tm
++;
1018 case S3C_TCMPB1
: tm
++;
1020 return s
->compareb
[tm
];
1021 case S3C_TCNTB4
: tm
++;
1022 case S3C_TCNTB3
: tm
++;
1023 case S3C_TCNTB2
: tm
++;
1024 case S3C_TCNTB1
: tm
++;
1026 return s
->countb
[tm
];
1027 case S3C_TCNTO4
: tm
++;
1028 case S3C_TCNTO3
: tm
++;
1029 case S3C_TCNTO2
: tm
++;
1030 case S3C_TCNTO1
: tm
++;
1032 return s3c_timers_get(s
, tm
);
1034 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1040 static void s3c_timers_write(void *opaque
, target_phys_addr_t addr
,
1043 struct s3c_timers_state_s
*s
= (struct s3c_timers_state_s
*) opaque
;
1048 s
->config
[0] = value
& 0x00ffffff;
1051 s
->config
[1] = value
& 0x00ffffff;
1054 for (tm
= 0; tm
< 5; tm
++) {
1055 if (value
& (2 << (s3c_tm_bits
[tm
]))) {
1056 if (s
->timer
[tm
].running
) {
1057 s3c_timers_stop(s
, tm
);
1058 s
->timer
[tm
].count
= s
->countb
[tm
];
1059 s3c_timers_start(s
, tm
);
1061 s
->timer
[tm
].count
= s
->countb
[tm
];
1063 if (((value
>> s3c_tm_bits
[tm
]) & 1) ^ s
->timer
[tm
].running
) {
1064 if (s
->timer
[tm
].running
)
1065 s3c_timers_stop(s
, tm
);
1067 s3c_timers_start(s
, tm
);
1071 s
->control
= value
& 0x007fff1f;
1073 case S3C_TCMPB3
: tm
++;
1074 case S3C_TCMPB2
: tm
++;
1075 case S3C_TCMPB1
: tm
++;
1077 s
->compareb
[tm
] = value
& 0xffff;
1078 if (s
->timer
[tm
].cmp_cb
)
1079 s
->timer
[tm
].cmp_cb(tm
, s
->compareb
[tm
], s
->timer
[tm
].cmp_opaque
);
1081 case S3C_TCNTB4
: tm
++;
1082 case S3C_TCNTB3
: tm
++;
1083 case S3C_TCNTB2
: tm
++;
1084 case S3C_TCNTB1
: tm
++;
1086 s
->countb
[tm
] = value
& 0xffff;
1089 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1093 static CPUReadMemoryFunc
*s3c_timers_readfn
[] = {
1099 static CPUWriteMemoryFunc
*s3c_timers_writefn
[] = {
1105 static void s3c_timers_save(QEMUFile
*f
, void *opaque
)
1107 struct s3c_timers_state_s
*s
= (struct s3c_timers_state_s
*) opaque
;
1109 for (i
= 0; i
< 5; i
++) {
1110 qemu_put_be32(f
, s
->timer
[i
].running
);
1111 qemu_put_be32s(f
, &s
->timer
[i
].divider
);
1112 qemu_put_be16(f
, s3c_timers_get(s
, i
));
1113 qemu_put_sbe64s(f
, &s
->timer
[i
].reload
);
1116 for (i
= 0; i
< 4; i
++)
1117 qemu_put_be16s(f
, &s
->compareb
[i
]);
1118 for (i
= 0; i
< 5; i
++)
1119 qemu_put_be16s(f
, &s
->countb
[i
]);
1120 for (i
= 0; i
< 2; i
++)
1121 qemu_put_be32s(f
, &s
->config
[i
]);
1122 qemu_put_be32s(f
, &s
->control
);
1125 static int s3c_timers_load(QEMUFile
*f
, void *opaque
, int version_id
)
1127 struct s3c_timers_state_s
*s
= (struct s3c_timers_state_s
*) opaque
;
1129 for (i
= 0; i
< 5; i
++) {
1130 s
->timer
[i
].running
= 0;
1131 running
[i
] = qemu_get_be32(f
);
1132 qemu_get_be32s(f
, &s
->timer
[i
].divider
);
1133 qemu_get_be16s(f
, &s
->timer
[i
].count
);
1134 qemu_get_sbe64s(f
, &s
->timer
[i
].reload
);
1137 for (i
= 0; i
< 4; i
++)
1138 qemu_get_be16s(f
, &s
->compareb
[i
]);
1139 for (i
= 0; i
< 5; i
++)
1140 qemu_get_be16s(f
, &s
->countb
[i
]);
1141 for (i
= 0; i
< 2; i
++)
1142 qemu_get_be32s(f
, &s
->config
[i
]);
1143 qemu_get_be32s(f
, &s
->control
);
1145 for (i
= 0; i
< 5; i
++)
1147 s3c_timers_start(s
, i
);
1152 struct s3c_timers_state_s
*s3c_timers_init(struct s3c_freq_s
* freq
, target_phys_addr_t base
,
1153 qemu_irq
*pic
, qemu_irq
*dma
)
1156 struct s3c_timers_state_s
*s
= (struct s3c_timers_state_s
*)
1157 qemu_mallocz(sizeof(struct s3c_timers_state_s
));
1163 s3c_timers_reset(s
);
1165 for (i
= 0; i
< 5; i
++) {
1166 s
->timer
[i
].t
= qemu_new_timer(vm_clock
,
1167 s3c_timers_tick
, &s
->timer
[i
]);
1170 s
->timer
[i
].cmp_cb
= 0;
1171 s
->timer
[i
].irq
= pic
[i
];
1174 iomemtype
= cpu_register_io_memory(0, s3c_timers_readfn
,
1175 s3c_timers_writefn
, s
);
1176 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
1178 register_savevm("s3c24xx_timers", 0, 0,
1179 s3c_timers_save
, s3c_timers_load
, s
);
1184 void s3c_timers_cmp_handler_set(void *opaque
, int line
,
1185 gpio_handler_t handler
, void *cmp_opaque
)
1187 struct s3c_timers_state_s
*s
= (struct s3c_timers_state_s
*) opaque
;
1188 if (line
> 4 || line
< 0) {
1189 printf("%s: Bad timer number %i.\n", __FUNCTION__
, line
);
1192 s
->timer
[line
].cmp_cb
= handler
;
1193 s
->timer
[line
].cmp_opaque
= cmp_opaque
;
1197 struct s3c_uart_state_s
{
1198 struct s3c_freq_s
* freq
;
1199 target_phys_addr_t base
;
1206 #define UART_MAX_CHR 4
1208 CharDriverState
*chr
[UART_MAX_CHR
];
1218 static void s3c_uart_reset(struct s3c_uart_state_s
*s
)
1223 s
->control
= 0x0000;
1230 static void s3c_uart_err(struct s3c_uart_state_s
*s
, int err
)
1233 if (s
->control
& (1 << 6))
1234 qemu_irq_raise(s
->irq
[2]);
1237 inline static void s3c_uart_full(struct s3c_uart_state_s
*s
, int pulse
)
1239 if (s
->fcontrol
& 1) /* FIFOEnable */
1240 if (s
->rxlen
< (((s
->fcontrol
>> 4) & 3) + 1) * 4) {
1241 if (((s
->control
>> 0) & 3) != 1 || /* ReceiveMode */
1244 if (!(s
->control
& (1 << 7))) /* RxTimeOutEnable */
1246 /* When the Rx FIFO trigger level is not reached, the interrupt
1247 * is generated anyway, just after a small timeout instead of
1251 switch ((s
->control
>> 0) & 3) { /* ReceiveMode */
1253 if ((s
->control
& (1 << 8)) || pulse
) /* RxInterruptType */
1254 qemu_irq_raise(s
->irq
[0]);
1258 qemu_irq_raise(s
->dma
[0]);
1263 inline static void s3c_uart_empty(struct s3c_uart_state_s
*s
, int pulse
)
1265 switch ((s
->control
>> 2) & 3) { /* TransmitMode */
1267 if ((s
->control
& (1 << 9)) || pulse
) /* TxInterruptType */
1268 qemu_irq_raise(s
->irq
[1]);
1272 qemu_irq_raise(s
->dma
[0]);
1277 inline static void s3c_uart_update(struct s3c_uart_state_s
*s
)
1279 s3c_uart_empty(s
, 0);
1280 s3c_uart_full(s
, 0);
1283 static void s3c_uart_params_update(struct s3c_uart_state_s
*s
)
1285 QEMUSerialSetParams ssp
;
1290 /* XXX Calculate PCLK frequency from clock manager registers */
1291 ssp
.speed
= (s
->freq
->pclk
>> 4) / (s
->brdiv
+ 1);
1293 switch ((s
->lcontrol
>> 3) & 7) {
1306 ssp
.data_bits
= 5 + (s
->lcontrol
& 3);
1308 ssp
.stop_bits
= (s
->lcontrol
& (1 << 2)) ? 2 : 1;
1310 for (i
= 0; i
< s
->chr_num
; i
++)
1311 qemu_chr_ioctl(s
->chr
[i
], CHR_IOCTL_SERIAL_SET_PARAMS
, &ssp
);
1314 static int s3c_uart_is_empty(void *opaque
)
1316 struct s3c_uart_state_s
*s
= (struct s3c_uart_state_s
*) opaque
;
1317 if (s
->fcontrol
& 1) /* FIFOEnable */
1318 return 16 - s
->rxlen
;
1320 return 1 - s
->rxlen
;
1323 static void s3c_uart_rx(void *opaque
, const uint8_t *buf
, int size
)
1325 struct s3c_uart_state_s
*s
= (struct s3c_uart_state_s
*) opaque
;
1327 if (s
->fcontrol
& 1) { /* FIFOEnable */
1328 if (s
->rxlen
+ size
> 16) {
1329 size
= 16 - s
->rxlen
;
1333 left
= 16 - ((s
->rxstart
+ s
->rxlen
) & 15);
1335 memcpy(s
->rxfifo
+ ((s
->rxstart
+ s
->rxlen
) & 15), buf
, left
);
1336 memcpy(s
->rxfifo
, buf
+ left
, size
- left
);
1338 memcpy(s
->rxfifo
+ ((s
->rxstart
+ s
->rxlen
) & 15), buf
, size
);
1341 if (s
->rxlen
+ size
> 1)
1346 s3c_uart_full(s
, 1);
1349 /* S3C2410 UART doesn't seem to understand break conditions. */
1350 static void s3c_uart_event(void *opaque
, int event
)
1354 #define S3C_ULCON 0x00 /* UART Line Control register */
1355 #define S3C_UCON 0x04 /* UART Control register */
1356 #define S3C_UFCON 0x08 /* UART FIFO Control register */
1357 #define S3C_UMCON 0x0c /* UART Modem Control register */
1358 #define S3C_UTRSTAT 0x10 /* UART Tx/Rx Status register */
1359 #define S3C_UERSTAT 0x14 /* UART Error Status register */
1360 #define S3C_UFSTAT 0x18 /* UART FIFO Status register */
1361 #define S3C_UMSTAT 0x1c /* UART Modem Status register */
1362 #define S3C_UTXH 0x20 /* UART Transmit Buffer register */
1363 #define S3C_URXH 0x24 /* UART Receive Buffer register */
1364 #define S3C_UBRDIV 0x28 /* UART Baud Rate Divisor register */
1366 static uint32_t s3c_uart_read(void *opaque
, target_phys_addr_t addr
)
1368 struct s3c_uart_state_s
*s
= (struct s3c_uart_state_s
*) opaque
;
1381 return 6 | !!s
->rxlen
;
1383 /* XXX: UERSTAT[3] is Reserved but Linux thinks it is BREAK */
1390 return s
->rxlen
? s
->rxlen
| (1 << 8) : 0;
1394 case S3C_UTXH
: /* why this is called by u-boot is not clear */
1400 if (s
->fcontrol
& 1) { /* FIFOEnable */
1401 ret
= s
->rxfifo
[s
->rxstart
++];
1411 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1417 static void s3c_uart_write(void *opaque
, target_phys_addr_t addr
,
1420 struct s3c_uart_state_s
*s
= (struct s3c_uart_state_s
*) opaque
;
1426 if ((s
->lcontrol
^ value
) & (1 << 6))
1427 printf("%s: UART Infra-red mode %s\n", __FUNCTION__
,
1428 (value
& (1 << 6)) ? "on" : "off");
1429 s
->lcontrol
= value
;
1430 s3c_uart_params_update(s
);
1434 /* XXX: UCON[4] is Reserved but Linux thinks it is BREAK */
1435 if ((s
->control
^ value
) & (1 << 5))
1436 printf("%s: UART loopback test mode %s\n", __FUNCTION__
,
1437 (value
& (1 << 5)) ? "on" : "off");
1438 s
->control
= value
& 0x7ef;
1442 if (value
& (1 << 1)) /* RxReset */
1444 s
->fcontrol
= value
& 0xf1;
1448 #ifdef CONFIG_S3C_MODEM /* not handled, openmoko modem.c not imported */
1449 if ((s
->mcontrol
^ value
) & (1 << 4)) {
1450 int afc
= (value
>> 4) & 1;
1451 for (i
= 0; i
< s
->chr_num
; i
++)
1452 qemu_chr_ioctl(s
->chr
[i
], CHR_IOCTL_MODEM_HANDSHAKE
, &afc
);
1455 s
->mcontrol
= value
& 0x11;
1460 for (i
= 0; i
< s
->chr_num
; i
++)
1461 qemu_chr_write(s
->chr
[i
], &ch
, 1);
1462 s3c_uart_empty(s
, 1);
1466 s
->brdiv
= value
& 0xffff;
1467 s3c_uart_params_update(s
);
1471 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1475 static CPUReadMemoryFunc
*s3c_uart_readfn
[] = {
1481 static CPUWriteMemoryFunc
*s3c_uart_writefn
[] = {
1487 static void s3c_uart_save(QEMUFile
*f
, void *opaque
)
1489 struct s3c_uart_state_s
*s
= (struct s3c_uart_state_s
*) opaque
;
1490 qemu_put_8s(f
, &s
->data
);
1491 qemu_put_buffer(f
, s
->rxfifo
, sizeof(s
->rxfifo
));
1492 qemu_put_be32(f
, s
->rxstart
);
1493 qemu_put_be32(f
, s
->rxlen
);
1494 qemu_put_8s(f
, &s
->lcontrol
);
1495 qemu_put_8s(f
, &s
->fcontrol
);
1496 qemu_put_8s(f
, &s
->mcontrol
);
1497 qemu_put_be16s(f
, &s
->control
);
1498 qemu_put_be16s(f
, &s
->brdiv
);
1499 qemu_put_8s(f
, &s
->errstat
);
1502 static int s3c_uart_load(QEMUFile
*f
, void *opaque
, int version_id
)
1504 struct s3c_uart_state_s
*s
= (struct s3c_uart_state_s
*) opaque
;
1505 qemu_get_8s(f
, &s
->data
);
1506 qemu_get_buffer(f
, s
->rxfifo
, sizeof(s
->rxfifo
));
1507 s
->rxstart
= qemu_get_be32(f
);
1508 s
->rxlen
= qemu_get_be32(f
);
1509 qemu_get_8s(f
, &s
->lcontrol
);
1510 qemu_get_8s(f
, &s
->fcontrol
);
1511 qemu_get_8s(f
, &s
->mcontrol
);
1512 qemu_get_be16s(f
, &s
->control
);
1513 qemu_get_be16s(f
, &s
->brdiv
);
1514 qemu_get_8s(f
, &s
->errstat
);
1519 struct s3c_uart_state_s
*s3c_uart_init(struct s3c_freq_s
* freq
, target_phys_addr_t base
,
1520 qemu_irq
*irqs
, qemu_irq
*dma
)
1523 struct s3c_uart_state_s
*s
= (struct s3c_uart_state_s
*)
1524 qemu_mallocz(sizeof(struct s3c_uart_state_s
));
1533 iomemtype
= cpu_register_io_memory(0, s3c_uart_readfn
,
1534 s3c_uart_writefn
, s
);
1535 cpu_register_physical_memory(s
->base
, 0xfff, iomemtype
);
1537 register_savevm("s3c24xx_uart", base
, 0, s3c_uart_save
, s3c_uart_load
, s
);
1542 void s3c_uart_attach(struct s3c_uart_state_s
*s
, CharDriverState
*chr
)
1544 if (s
->chr_num
>= UART_MAX_CHR
)
1545 cpu_abort(cpu_single_env
, "%s: Too many devices\n", __FUNCTION__
);
1546 s
->chr
[s
->chr_num
++] = chr
;
1548 qemu_chr_add_handlers(chr
, s3c_uart_is_empty
,
1549 s3c_uart_rx
, s3c_uart_event
, s
);
1552 /* ADC & Touchscreen interface */
1553 struct s3c_adc_state_s
{
1554 target_phys_addr_t base
;
1575 static void s3c_adc_reset(struct s3c_adc_state_s
*s
)
1578 s
->control
= 0x3fc4;
1584 static void s3c_adc_start(struct s3c_adc_state_s
*s
)
1586 if (!s
->enable
|| (s
->ts
& 7) == 0)
1588 s
->control
&= ~(1 << 15);
1589 s
->in_idx
= (s
->control
>> 3) & 7;
1590 qemu_mod_timer(s
->convt
, qemu_get_clock(vm_clock
) + (ticks_per_sec
>> 5));
1593 static void s3c_adc_done(void *opaque
)
1595 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*) opaque
;
1596 s
->xdata
= s
->input
[s
->in_idx
] & 0x3ff;
1597 s
->control
|= 1 << 15;
1598 qemu_irq_raise(s
->irq
);
1601 static void s3c_adc_tick(void *opaque
)
1603 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*) opaque
;
1607 if ((s
->ts
& 3) == 3 && s
->enable
)
1608 qemu_irq_raise(s
->tcirq
);
1609 else if (s
->enable
&& ((s
->ts
& (1 << 2)) || (s
->ts
& 3))) {
1610 sx
= s
->x
* s
->scale
[0] + s
->y
* s
->scale
[1] + s
->scale
[2];
1611 sy
= s
->x
* s
->scale
[3] + s
->y
* s
->scale
[4] + s
->scale
[5];
1612 s
->xdata
= ((sx
>> 13) & 0xfff) | (1 << 14) | ((s
->ts
& 3) << 12);
1613 s
->ydata
= ((sy
>> 13) & 0xfff) | (1 << 14) | ((s
->ts
& 3) << 12);
1614 s
->xdata
^= s
->noise
>> 1;
1615 s
->ydata
^= s
->noise
>> 2;
1616 qemu_irq_raise(s
->irq
);
1620 qemu_mod_timer(s
->tst
, qemu_get_clock(vm_clock
) +
1621 (ticks_per_sec
>> 5));
1625 static void s3c_adc_event(void *opaque
,
1626 int x
, int y
, int z
, int buttons_state
)
1628 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*) opaque
;
1629 s
->down
= !!buttons_state
;
1635 #define S3C_ADCCON 0x00 /* ADC Control register */
1636 #define S3C_ADCTSC 0x04 /* ADC Touchscreen Control register */
1637 #define S3C_ADCDLY 0x08 /* ADC Start or Interval Delay register */
1638 #define S3C_ADCDAT0 0x0c /* ADC Conversion Data register 0 */
1639 #define S3C_ADCDAT1 0x10 /* ADC Conversion Data register 1 */
1641 static uint32_t s3c_adc_read(void *opaque
, target_phys_addr_t addr
)
1643 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*) opaque
;
1655 return ((!s
->down
) << 15) | s
->xdata
;
1657 return ((!s
->down
) << 15) | s
->ydata
;
1659 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1665 static void s3c_adc_write(void *opaque
, target_phys_addr_t addr
,
1668 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*) opaque
;
1672 s
->control
= (s
->control
& 0x8000) | (value
& 0x7ffe);
1673 s
->enable
= !(value
& 4);
1674 if ((value
& 1) && !(value
& 2))
1677 qemu_del_timer(s
->convt
);
1682 s
->ts
= value
& 0xff;
1686 s
->delay
= value
& 0xffff;
1690 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1694 static CPUReadMemoryFunc
*s3c_adc_readfn
[] = {
1700 static CPUWriteMemoryFunc
*s3c_adc_writefn
[] = {
1706 static void s3c_adc_save(QEMUFile
*f
, void *opaque
)
1708 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*) opaque
;
1710 qemu_put_be32(f
, s
->enable
);
1711 for (i
= 0; i
< 8; i
++)
1712 qemu_put_be32(f
, s
->input
[i
]);
1713 qemu_put_be32(f
, s
->in_idx
);
1714 qemu_put_be32(f
, s
->noise
);
1716 qemu_put_be16s(f
, &s
->control
);
1717 qemu_put_be16s(f
, &s
->ts
);
1718 qemu_put_be16s(f
, &s
->delay
);
1719 qemu_put_sbe16s(f
, &s
->xdata
);
1720 qemu_put_sbe16s(f
, &s
->ydata
);
1723 static int s3c_adc_load(QEMUFile
*f
, void *opaque
, int version_id
)
1725 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*) opaque
;
1727 s
->enable
= qemu_get_be32(f
);
1728 for (i
= 0; i
< 8; i
++)
1729 s
->input
[i
] = qemu_get_be32(f
);
1730 s
->in_idx
= qemu_get_be32(f
);
1731 s
->noise
= qemu_get_be32(f
);
1733 qemu_get_be16s(f
, &s
->control
);
1734 qemu_get_be16s(f
, &s
->ts
);
1735 qemu_get_be16s(f
, &s
->delay
);
1736 qemu_get_sbe16s(f
, &s
->xdata
);
1737 qemu_get_sbe16s(f
, &s
->ydata
);
1739 if (s
->enable
&& (s
->ts
& 7) && !(s
->control
& (1 << 15)))
1745 struct s3c_adc_state_s
*s3c_adc_init(target_phys_addr_t base
, qemu_irq irq
,
1749 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*)
1750 qemu_mallocz(sizeof(struct s3c_adc_state_s
));
1755 s
->convt
= qemu_new_timer(vm_clock
, s3c_adc_done
, s
);
1756 s
->tst
= qemu_new_timer(vm_clock
, s3c_adc_tick
, s
);
1760 iomemtype
= cpu_register_io_memory(0, s3c_adc_readfn
,
1761 s3c_adc_writefn
, s
);
1762 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
1764 /* We want absolute coordinates */
1765 qemu_add_mouse_event_handler(s3c_adc_event
, s
, 1,
1766 "QEMU S3C2410-driven Touchscreen");
1768 register_savevm("s3c24xx_adc", 0, 0, s3c_adc_save
, s3c_adc_load
, s
);
1773 void s3c_adc_setscale(struct s3c_adc_state_s
*adc
, const int m
[])
1775 memcpy(adc
->scale
, m
, 6 * sizeof(int));
1778 /* IIC-bus serial interface */
1779 struct s3c_i2c_state_s
{
1782 target_phys_addr_t base
;
1794 static void s3c_i2c_irq(struct s3c_i2c_state_s
*s
)
1796 s
->control
|= 1 << 4;
1797 if (s
->control
& (1 << 5))
1798 qemu_irq_raise(s
->irq
);
1801 static void s3c_i2c_reset(struct s3c_i2c_state_s
*s
)
1810 static void s3c_i2c_event(i2c_slave
*i2c
, enum i2c_event event
)
1812 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) i2c
;
1813 if (!(s
->status
& (1 << 4)))
1817 case I2C_START_RECV
:
1818 case I2C_START_SEND
:
1819 s
->status
|= 1 << 2;
1826 s
->status
|= 1 << 0;
1833 static int s3c_i2c_tx(i2c_slave
*i2c
, uint8_t data
)
1835 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) i2c
;
1836 if (!(s
->status
& (1 << 4)))
1839 if ((s
->status
>> 6) == 0)
1840 s
->data
= data
; /* TODO */
1841 s
->status
&= ~(1 << 0);
1844 return !(s
->control
& (1 << 7));
1847 static int s3c_i2c_rx(i2c_slave
*i2c
)
1849 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) i2c
;
1850 if (!(s
->status
& (1 << 4)))
1853 if ((s
->status
>> 6) == 1) {
1854 s
->status
&= ~(1 << 0);
1862 static void s3c_master_work(void *opaque
)
1864 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) opaque
;
1865 int start
= 0, stop
= 0, ack
= 1;
1866 if (s
->control
& (1 << 4)) /* Interrupt pending */
1868 if ((s
->status
& 0x90) != 0x90) /* Master */
1870 stop
= ~s
->status
& (1 << 5);
1871 if (s
->newstart
&& s
->status
& (1 << 5)) { /* START */
1880 ack
= !i2c_start_transfer(s
->bus
, s
->data
>> 1, (~s
->status
>> 6) & 1);
1882 i2c_end_transfer(s
->bus
);
1883 else if (s
->status
& (1 << 6))
1884 ack
= !i2c_send(s
->bus
, s
->data
);
1886 s
->data
= i2c_recv(s
->bus
);
1888 if (!(s
->control
& (1 << 7))) /* ACK */
1892 if (!(s
->status
& (1 << 5))) {
1903 #define S3C_IICCON 0x00 /* IIC-Bus Control register */
1904 #define S3C_IICSTAT 0x04 /* IIC-Bus Control / Status register */
1905 #define S3C_IICADD 0x08 /* IIC-Bus Address register */
1906 #define S3C_IICDS 0x0c /* IIC-Bus Tx / Rx Data Shift register */
1908 #define S3C2440_IICLC 0x10 /* IIC-Bus multi-master line control register */
1910 static uint32_t s3c_i2c_read(void *opaque
, target_phys_addr_t addr
)
1912 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) opaque
;
1918 return s
->status
& ~(1 << 5); /* Busy signal */
1923 case S3C2440_IICLC
: /* s3c2440 only ! */
1926 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1932 static void s3c_i2c_write(void *opaque
, target_phys_addr_t addr
,
1935 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) opaque
;
1939 s
->control
= (s
->control
| 0xef) & value
;
1946 s
->status
|= value
& 0xf0;
1947 if (s
->status
& (1 << 5))
1953 s
->addy
= value
& 0x7f;
1954 i2c_set_slave_address(&s
->slave
, s
->addy
);
1958 s
->data
= value
& 0xff;
1961 case S3C2440_IICLC
: /* s3c2440 only ! */
1962 s
->mmaster
= value
& 0xff;
1966 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1970 static CPUReadMemoryFunc
*s3c_i2c_readfn
[] = {
1976 static CPUWriteMemoryFunc
*s3c_i2c_writefn
[] = {
1982 static void s3c_i2c_save(QEMUFile
*f
, void *opaque
)
1984 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) opaque
;
1985 qemu_put_8s(f
, &s
->control
);
1986 qemu_put_8s(f
, &s
->status
);
1987 qemu_put_8s(f
, &s
->data
);
1988 qemu_put_8s(f
, &s
->addy
);
1989 qemu_put_8s(f
, &s
->mmaster
);
1991 qemu_put_be32(f
, s
->busy
);
1992 qemu_put_be32(f
, s
->newstart
);
1994 i2c_slave_save(f
, &s
->slave
);
1997 static int s3c_i2c_load(QEMUFile
*f
, void *opaque
, int version_id
)
1999 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) opaque
;
2000 qemu_get_8s(f
, &s
->control
);
2001 qemu_get_8s(f
, &s
->status
);
2002 qemu_get_8s(f
, &s
->data
);
2003 qemu_get_8s(f
, &s
->addy
);
2004 qemu_get_8s(f
, &s
->mmaster
);
2006 s
->busy
= qemu_get_be32(f
);
2007 s
->newstart
= qemu_get_be32(f
);
2009 i2c_slave_load(f
, &s
->slave
);
2013 struct s3c_i2c_state_s
*s3c_i2c_init(target_phys_addr_t base
, qemu_irq irq
)
2016 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*)
2017 qemu_mallocz(sizeof(struct s3c_i2c_state_s
));
2021 s
->slave
.event
= s3c_i2c_event
;
2022 s
->slave
.send
= s3c_i2c_tx
;
2023 s
->slave
.recv
= s3c_i2c_rx
;
2024 s
->bus
= i2c_init_bus();
2028 iomemtype
= cpu_register_io_memory(0, s3c_i2c_readfn
,
2029 s3c_i2c_writefn
, s
);
2030 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
2032 register_savevm("s3c24xx_i2c", 0, 0, s3c_i2c_save
, s3c_i2c_load
, s
);
2037 i2c_bus
*s3c_i2c_bus(struct s3c_i2c_state_s
*s
)
2042 /* Serial Peripheral Interface */
2043 struct s3c_spi_state_s
{
2044 target_phys_addr_t base
;
2063 uint8_t (*txrx
[2])(void *opaque
, uint8_t value
);
2064 uint8_t (*btxrx
[2])(void *opaque
, uint8_t value
);
2068 static void s3c_spi_update(struct s3c_spi_state_s
*s
)
2071 for (i
= 0; i
< 2; i
++) {
2072 switch ((s
->chan
[i
].control
>> 5) & 3) { /* SMOD */
2074 qemu_irq_raise(s
->chan
[i
].irq
);
2077 qemu_irq_raise(s
->chan
[i
].drq
);
2083 static void s3c_spi_reset(struct s3c_spi_state_s
*s
)
2085 memset(s
->chan
, 0, sizeof(s
->chan
));
2086 s
->chan
[0].pin
= 0x02;
2087 s
->chan
[1].pin
= 0x02;
2091 #define S3C_SPCON0 0x00 /* SPI channel 0 control register */
2092 #define S3C_SPSTA0 0x04 /* SPI channel 0 status register */
2093 #define S3C_SPPIN0 0x08 /* SPI channel 0 pin control register */
2094 #define S3C_SPPRE0 0x0c /* SPI channel 0 baudrate prescaler register */
2095 #define S3C_SPTDAT0 0x10 /* SPI channel 0 Tx data register */
2096 #define S3C_SPRDAT0 0x14 /* SPI channel 0 Rx data register */
2097 #define S3C_SPCON1 0x20 /* SPI channel 1 control register */
2098 #define S3C_SPSTA1 0x24 /* SPI channel 1 status register */
2099 #define S3C_SPPIN1 0x28 /* SPI channel 1 pin control register */
2100 #define S3C_SPPRE1 0x2c /* SPI channel 1 baudrate prescaler register */
2101 #define S3C_SPTDAT1 0x30 /* SPI channel 1 Tx data register */
2102 #define S3C_SPRDAT1 0x34 /* SPI channel 1 Rx data register */
2104 static uint32_t s3c_spi_read(void *opaque
, target_phys_addr_t addr
)
2106 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*) opaque
;
2114 return s
->chan
[ch
].control
;
2122 return s
->chan
[ch
].pin
;
2126 return s
->chan
[ch
].pre
;
2130 return s
->chan
[ch
+ 2].txbuf
;
2134 if (s
->txrx
[ch
] && (s
->chan
[ch
].control
& 0x19) == 0x19)
2135 s
->chan
[ch
].rxbuf
= s
->txrx
[ch
](s
->opaque
[ch
], 'Q');
2137 return s
->chan
[ch
].rxbuf
;
2140 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
2146 static void s3c_spi_write(void *opaque
, target_phys_addr_t addr
,
2149 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*) opaque
;
2157 s
->chan
[ch
].control
= value
& 0x7f;
2163 s
->chan
[ch
].pin
= value
& 0x07;
2168 s
->chan
[ch
].pre
= value
& 0xff;
2173 s
->chan
[ch
].txbuf
= value
& 0xff;
2174 if (s
->txrx
[ch
] && (s
->chan
[ch
].control
& 0x19) == 0x18)
2175 s
->chan
[ch
].rxbuf
= s
->txrx
[ch
](s
->opaque
[ch
], value
& 0xff);
2180 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
2184 static CPUReadMemoryFunc
*s3c_spi_readfn
[] = {
2190 static CPUWriteMemoryFunc
*s3c_spi_writefn
[] = {
2196 static void s3c_spi_save(QEMUFile
*f
, void *opaque
)
2198 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*) opaque
;
2200 for (i
= 0; i
< 2; i
++) {
2201 qemu_put_8s(f
, &s
->chan
[i
].control
);
2202 qemu_put_8s(f
, &s
->chan
[i
].pin
);
2203 qemu_put_8s(f
, &s
->chan
[i
].pre
);
2205 qemu_put_8s(f
, &s
->chan
[i
].txbuf
);
2206 qemu_put_8s(f
, &s
->chan
[i
].rxbuf
);
2207 qemu_put_be32(f
, s
->chan
[i
].cs_pin
);
2208 qemu_put_be32(f
, s
->chan
[i
].clk_pin
);
2209 qemu_put_be32(f
, s
->chan
[i
].mosi_pin
);
2210 qemu_put_be32(f
, s
->chan
[i
].bit
);
2214 static int s3c_spi_load(QEMUFile
*f
, void *opaque
, int version_id
)
2216 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*) opaque
;
2218 for (i
= 0; i
< 2; i
++) {
2219 qemu_get_8s(f
, &s
->chan
[i
].control
);
2220 qemu_get_8s(f
, &s
->chan
[i
].pin
);
2221 qemu_get_8s(f
, &s
->chan
[i
].pre
);
2223 qemu_get_8s(f
, &s
->chan
[i
].txbuf
);
2224 qemu_get_8s(f
, &s
->chan
[i
].rxbuf
);
2225 s
->chan
[i
].cs_pin
= qemu_get_be32(f
);
2226 s
->chan
[i
].clk_pin
= qemu_get_be32(f
);
2227 s
->chan
[i
].mosi_pin
= qemu_get_be32(f
);
2228 s
->chan
[i
].bit
= qemu_get_be32(f
);
2234 static void s3c_spi_bitbang_cs(void *opaque
, int line
, int level
)
2236 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*) opaque
;
2238 if (s
->chan
[ch
].cs_pin
|| level
) {
2239 if (s
->chan
[ch
].bit
&& s
->txrx
[ch
] && !s
->btxrx
[ch
]) {
2240 s
->chan
[ch
].txbuf
<<= 8 - s
->chan
[ch
].bit
;
2241 s
->chan
[ch
].rxbuf
= s
->txrx
[ch
](s
->opaque
[ch
], s
->chan
[ch
].txbuf
);
2243 } else if (!s
->chan
[ch
].cs_pin
|| !level
)
2244 s
->chan
[ch
].bit
= 0;
2246 /* SSn is active low. */
2247 s
->chan
[ch
].cs_pin
= !level
;
2250 static void s3c_spi_bitbang_clk(void *opaque
, int line
, int level
)
2252 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*) opaque
;
2254 if (!s
->chan
[ch
].cs_pin
)
2257 /* Detect CLK rising edge */
2258 if (s
->chan
[ch
].clk_pin
|| !level
)
2262 qemu_set_irq(s
->chan
[ch
].miso
,
2263 s
->btxrx
[ch
](s
->opaque
[ch
], s
->chan
[ch
].mosi_pin
));
2267 s
->chan
[ch
].txbuf
<<= 1;
2268 s
->chan
[ch
].txbuf
|= s
->chan
[ch
].mosi_pin
;
2270 qemu_set_irq(s
->chan
[ch
].miso
, (s
->chan
[ch
].rxbuf
>> 7) & 1);
2271 s
->chan
[ch
].rxbuf
<<= 1;
2273 if (++ s
->chan
[ch
].bit
== 8) {
2275 s
->chan
[ch
].rxbuf
= s
->txrx
[ch
](s
->opaque
[ch
], s
->chan
[ch
].txbuf
);
2276 s
->chan
[ch
].bit
= 0;
2280 s
->chan
[ch
].clk_pin
= level
;
2283 static void s3c_spi_bitbang_mosi(void *opaque
, int line
, int level
)
2285 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*) opaque
;
2287 s
->chan
[ch
].mosi_pin
= level
;
2290 static const struct {
2291 int cs
, clk
, miso
, mosi
;
2292 } s3c_spi_pins
[2] = {
2293 { S3C_GPG(2), S3C_GPE(13), S3C_GPE(11), S3C_GPE(12) },
2294 { S3C_GPG(3), S3C_GPG(7), S3C_GPG(5), S3C_GPG(6) },
2297 static void s3c_spi_bitbang_init(struct s3c_spi_state_s
*s
,
2298 struct s3c_gpio_state_s
*gpio
)
2301 qemu_irq
*cs
= qemu_allocate_irqs(s3c_spi_bitbang_cs
, s
, 2);
2302 qemu_irq
*clk
= qemu_allocate_irqs(s3c_spi_bitbang_clk
, s
, 2);
2303 qemu_irq
*mosi
= qemu_allocate_irqs(s3c_spi_bitbang_mosi
, s
, 2);
2305 for (i
= 0; i
< 2; i
++) {
2306 s3c_gpio_out_set(gpio
, s3c_spi_pins
[i
].cs
, cs
[i
]);
2307 s3c_gpio_out_set(gpio
, s3c_spi_pins
[i
].clk
, clk
[i
]);
2308 s
->chan
[i
].miso
= s3c_gpio_in_get(gpio
)[s3c_spi_pins
[i
].miso
];
2309 s3c_gpio_out_set(gpio
, s3c_spi_pins
[i
].mosi
, mosi
[i
]);
2313 struct s3c_spi_state_s
*s3c_spi_init(target_phys_addr_t base
,
2314 qemu_irq irq0
, qemu_irq drq0
, qemu_irq irq1
, qemu_irq drq1
,
2315 struct s3c_gpio_state_s
*gpio
)
2318 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*)
2319 qemu_mallocz(sizeof(struct s3c_spi_state_s
));
2322 s
->chan
[0].irq
= irq0
;
2323 s
->chan
[0].drq
= drq0
;
2324 s
->chan
[1].irq
= irq1
;
2325 s
->chan
[1].drq
= drq1
;
2329 iomemtype
= cpu_register_io_memory(0, s3c_spi_readfn
,
2330 s3c_spi_writefn
, s
);
2331 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
2333 s3c_spi_bitbang_init(s
, gpio
);
2335 register_savevm("s3c24xx_spi", 0, 0, s3c_spi_save
, s3c_spi_load
, s
);
2340 void s3c_spi_attach(struct s3c_spi_state_s
*s
, int ch
,
2341 uint8_t (*txrx
)(void *opaque
, uint8_t value
),
2342 uint8_t (*btxrx
)(void *opaque
, uint8_t value
), void *opaque
)
2345 cpu_abort(cpu_single_env
, "%s: No channel %i\n", __FUNCTION__
, ch
);
2347 s
->btxrx
[ch
] = btxrx
;
2348 s
->opaque
[ch
] = opaque
;
2351 /* IIS-BUS interface */
2352 static inline void s3c_i2s_update(struct s3c_i2s_state_s
*s
)
2355 (s
->control
& (1 << 0)) && !(s
->control
& (1 << 3)) &&
2356 (s
->mode
& (1 << 7)) && (s
->fcontrol
& (1 << 13));
2358 (s
->control
& (1 << 0)) && !(s
->control
& (1 << 2)) &&
2359 (s
->mode
& (1 << 6)) && (s
->fcontrol
& (1 << 12));
2360 s
->control
&= ~0xc0;
2361 /* The specs are unclear about the FIFO-ready flags logic.
2362 * Implement semantics that make most sense. */
2363 if (s
->tx_en
&& s
->tx_len
)
2364 s
->control
|= (1 << 7);
2365 if (s
->rx_en
&& s
->rx_len
)
2366 s
->control
|= (1 << 6);
2368 qemu_set_irq(s
->dma
[S3C_RQ_I2SSDO
], (s
->control
>> 5) &
2369 (s
->control
>> 7) & (s
->fcontrol
>> 15) & 1);
2370 qemu_set_irq(s
->dma
[S3C_RQ_I2SSDI0
], (s
->control
>> 4) &
2371 (s
->control
>> 6) & (s
->fcontrol
>> 14) & 1);
2372 qemu_set_irq(s
->dma
[S3C_RQ_I2SSDI1
], (s
->control
>> 4) &
2373 (s
->control
>> 6) & (s
->fcontrol
>> 14) & 1);
2376 static void s3c_i2s_reset(struct s3c_i2s_state_s
*s
)
2380 s
->prescaler
= 0x000;
2381 s
->fcontrol
= 0x0000;
2387 #define S3C_IISCON 0x00 /* IIS Control register */
2388 #define S3C_IISMOD 0x04 /* IIS Mode register */
2389 #define S3C_IISPSR 0x08 /* IIS Prescaler register */
2390 #define S3C_IISFCON 0x0c /* IIS FIFO Interface register */
2391 #define S3C_IISFIFO 0x10 /* IIS FIFO register */
2393 static uint32_t s3c_i2s_read(void *opaque
, target_phys_addr_t addr
)
2395 struct s3c_i2s_state_s
*s
= (struct s3c_i2s_state_s
*) opaque
;
2404 return s
->prescaler
;
2406 return s
->fcontrol
|
2407 (MAX(32 - s
->tx_len
, 0) << 6) |
2410 if (s
->rx_len
> 0) {
2415 s
->buffer
= (uint16_t) (ret
= s
->codec_in(s
->opaque
));
2421 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
2427 static void s3c_i2s_write(void *opaque
, target_phys_addr_t addr
,
2430 struct s3c_i2s_state_s
*s
= (struct s3c_i2s_state_s
*) opaque
;
2434 s
->control
= (s
->control
& 0x100) | (value
& 0x03f);
2438 s
->mode
= value
& 0x1ff;
2442 s
->prescaler
= value
& 0x3ff;
2445 s
->fcontrol
= value
& 0xf000;
2449 if (s
->tx_len
&& s
->tx_en
) {
2453 s
->codec_out(s
->opaque
, value
| ((uint32_t) s
->buffer
<< 16));
2455 s
->buffer
= (uint16_t) value
;
2460 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
2464 static CPUReadMemoryFunc
*s3c_i2s_readfn
[] = {
2470 static CPUWriteMemoryFunc
*s3c_i2s_writefn
[] = {
2476 static void s3c_i2s_save(QEMUFile
*f
, void *opaque
)
2478 struct s3c_i2s_state_s
*s
= (struct s3c_i2s_state_s
*) opaque
;
2479 qemu_put_be16s(f
, &s
->control
);
2480 qemu_put_be16s(f
, &s
->mode
);
2481 qemu_put_be16s(f
, &s
->prescaler
);
2482 qemu_put_be16s(f
, &s
->fcontrol
);
2484 qemu_put_be32(f
, s
->tx_en
);
2485 qemu_put_be32(f
, s
->rx_en
);
2486 qemu_put_be32(f
, s
->tx_len
);
2487 qemu_put_be32(f
, s
->rx_len
);
2488 qemu_put_be16(f
, s
->buffer
);
2489 qemu_put_be32(f
, s
->cycle
);
2492 static int s3c_i2s_load(QEMUFile
*f
, void *opaque
, int version_id
)
2494 struct s3c_i2s_state_s
*s
= (struct s3c_i2s_state_s
*) opaque
;
2495 qemu_get_be16s(f
, &s
->control
);
2496 qemu_get_be16s(f
, &s
->mode
);
2497 qemu_get_be16s(f
, &s
->prescaler
);
2498 qemu_get_be16s(f
, &s
->fcontrol
);
2500 s
->tx_en
= qemu_get_be32(f
);
2501 s
->rx_en
= qemu_get_be32(f
);
2502 s
->tx_len
= qemu_get_be32(f
);
2503 s
->rx_len
= qemu_get_be32(f
);
2504 s
->buffer
= qemu_get_be16(f
);
2505 s
->cycle
= qemu_get_be32(f
);
2510 static void s3c_i2s_data_req(void *opaque
, int tx
, int rx
)
2512 struct s3c_i2s_state_s
*s
= (struct s3c_i2s_state_s
*) opaque
;
2518 struct s3c_i2s_state_s
*s3c_i2s_init(target_phys_addr_t base
, qemu_irq
*dma
)
2521 struct s3c_i2s_state_s
*s
= (struct s3c_i2s_state_s
*)
2522 qemu_mallocz(sizeof(struct s3c_i2s_state_s
));
2526 s
->data_req
= s3c_i2s_data_req
;
2530 iomemtype
= cpu_register_io_memory(0, s3c_i2s_readfn
,
2531 s3c_i2s_writefn
, s
);
2532 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
2534 register_savevm("s3c24xx_iis", 0, 0, s3c_i2s_save
, s3c_i2s_load
, s
);
2539 /* Watchdog Timer */
2540 struct s3c_wdt_state_s
{
2541 struct s3c_freq_s
* freq
;
2542 target_phys_addr_t base
;
2551 static void s3c_wdt_start(struct s3c_wdt_state_s
*s
)
2553 int enable
= s
->control
& (1 << 5);
2554 int prescaler
= (s
->control
>> 8) + 1;
2555 int divider
= prescaler
<< (((s
->control
>> 3) & 3) + 4);
2557 s
->timestamp
= qemu_get_clock(vm_clock
);
2558 qemu_mod_timer(s
->tm
, s
->timestamp
+ muldiv64(divider
* s
->count
,
2559 ticks_per_sec
, s
->freq
->pclk
));
2561 qemu_del_timer(s
->tm
);
2564 static void s3c_wdt_stop(struct s3c_wdt_state_s
*s
)
2566 int prescaler
= (s
->control
>> 8) + 1;
2567 int divider
= prescaler
<< (((s
->control
>> 3) & 3) + 4);
2570 diff
= muldiv64(qemu_get_clock(vm_clock
) - s
->timestamp
, s
->freq
->pclk
,
2571 ticks_per_sec
) / divider
;
2572 s
->count
-= MIN(s
->count
, diff
);
2573 s
->timestamp
= qemu_get_clock(vm_clock
);
2576 static void s3c_wdt_reset(struct s3c_wdt_state_s
*s
)
2578 s
->control
= 0x8021;
2584 static void s3c_wdt_timeout(void *opaque
)
2586 struct s3c_wdt_state_s
*s
= (struct s3c_wdt_state_s
*) opaque
;
2587 if (s
->control
& (1 << 0)) {
2588 qemu_system_reset_request();
2591 if (s
->control
& (1 << 2))
2592 qemu_irq_raise(s
->irq
);
2597 #define S3C_WTCON 0x00 /* Watchdog timer control register */
2598 #define S3C_WTDAT 0x04 /* Watchdog timer data register */
2599 #define S3C_WTCNT 0x08 /* Watchdog timer count register */
2601 static uint32_t s3c_wdt_read(void *opaque
, target_phys_addr_t addr
)
2603 struct s3c_wdt_state_s
*s
= (struct s3c_wdt_state_s
*) opaque
;
2614 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
2620 static void s3c_wdt_write(void *opaque
, target_phys_addr_t addr
,
2623 struct s3c_wdt_state_s
*s
= (struct s3c_wdt_state_s
*) opaque
;
2639 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
2643 static CPUReadMemoryFunc
*s3c_wdt_readfn
[] = {
2649 static CPUWriteMemoryFunc
*s3c_wdt_writefn
[] = {
2655 static void s3c_wdt_save(QEMUFile
*f
, void *opaque
)
2657 struct s3c_wdt_state_s
*s
= (struct s3c_wdt_state_s
*) opaque
;
2660 qemu_put_be16s(f
, &s
->control
);
2661 qemu_put_be16s(f
, &s
->data
);
2662 qemu_put_be16s(f
, &s
->count
);
2663 qemu_put_sbe64s(f
, &s
->timestamp
);
2666 static int s3c_wdt_load(QEMUFile
*f
, void *opaque
, int version_id
)
2668 struct s3c_wdt_state_s
*s
= (struct s3c_wdt_state_s
*) opaque
;
2670 qemu_get_be16s(f
, &s
->control
);
2671 qemu_get_be16s(f
, &s
->data
);
2672 qemu_get_be16s(f
, &s
->count
);
2673 qemu_get_sbe64s(f
, &s
->timestamp
);
2679 struct s3c_wdt_state_s
*s3c_wdt_init(struct s3c_freq_s
* freq
, target_phys_addr_t base
, qemu_irq irq
)
2682 struct s3c_wdt_state_s
*s
= (struct s3c_wdt_state_s
*)
2683 qemu_mallocz(sizeof(struct s3c_wdt_state_s
));
2688 s
->tm
= qemu_new_timer(vm_clock
, s3c_wdt_timeout
, s
);
2692 iomemtype
= cpu_register_io_memory(0, s3c_wdt_readfn
,
2693 s3c_wdt_writefn
, s
);
2694 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
2696 register_savevm("s3c24xx_wdt", 0, 0, s3c_wdt_save
, s3c_wdt_load
, s
);
2703 target_phys_addr_t base
;
2706 } s3c2410_uart
[] = {
2709 { S3C_PICS_RXD0
, S3C_PICS_TXD0
, S3C_PICS_ERR0
},
2714 { S3C_PICS_RXD1
, S3C_PICS_TXD1
, S3C_PICS_ERR1
},
2719 { S3C_PICS_RXD2
, S3C_PICS_TXD2
, S3C_PICS_ERR2
},
2722 { 0, { 0, 0, 0 }, { 0 } }
2725 /* General CPU reset */
2726 static void s3c2410_reset(void *opaque
)
2728 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
2731 s3c_pic_reset(s
->pic
);
2732 s3c_dma_reset(s
->dma
);
2733 s3c_gpio_reset(s
->io
);
2734 s3c_lcd_reset(s
->lcd
);
2735 s3c_timers_reset(s
->timers
);
2736 s3c_mmci_reset(s
->mmci
);
2737 s3c_adc_reset(s
->adc
);
2738 s3c_i2c_reset(s
->i2c
);
2739 s3c_i2s_reset(s
->i2s
);
2740 s3c_rtc_reset(s
->rtc
);
2741 s3c_spi_reset(s
->spi
);
2742 s3c_udc_reset(s
->udc
);
2743 s3c_wdt_reset(s
->wdt
);
2744 s3c_clkpwr_reset(s
);
2745 s
->nand
->reset(s
->nand
);
2746 for (i
= 0; s3c2410_uart
[i
].base
; i
++)
2747 s3c_uart_reset(s
->uart
[i
]);
2751 struct s3c_state_s
* g_s3c
;
2753 /* Initialise an S3C24XX microprocessor. */
2754 struct s3c_state_s
*s3c24xx_init(
2757 unsigned int sdram_size
,
2758 uint32_t sram_address
,
2761 struct s3c_state_s
*s
;
2763 s
= (struct s3c_state_s
*) qemu_mallocz(sizeof(struct s3c_state_s
));
2768 s
->clock
.xtal
= xtal
;
2769 s
->clock
.pclk
= 66500000; // S3C_PCLK_FREQ; // TEMP
2771 s
->env
= cpu_init("arm920t");
2773 fprintf(stderr
, "Unable to initialize ARM920T\n");
2776 register_savevm("s3c24xx", 0, 0,
2777 cpu_save
, cpu_load
, s
->env
);
2779 cpu_register_physical_memory(S3C_RAM_BASE
, sdram_size
,
2780 qemu_ram_alloc(sdram_size
) | IO_MEM_RAM
);
2782 /* If OM pins are 00, SRAM is mapped at 0x0 instead. */
2783 cpu_register_physical_memory(sram_address
, S3C_SRAM_SIZE
,
2784 qemu_ram_alloc(S3C_SRAM_SIZE
) | IO_MEM_RAM
);
2786 s
->mc_base
= 0x48000000;
2788 iomemtype
= cpu_register_io_memory(0, s3c_mc_readfn
, s3c_mc_writefn
, s
);
2789 cpu_register_physical_memory(s
->mc_base
, 0xffffff, iomemtype
);
2790 register_savevm("s3c24xx_mc", 0, 0, s3c_mc_save
, s3c_mc_load
, s
);
2792 s
->pic
= s3c_pic_init(0x4a000000, arm_pic_init_cpu(s
->env
));
2793 s
->irq
= s3c_pic_get(s
->pic
);
2795 s
->dma
= s3c_dma_init(0x4b000000, &s
->irq
[S3C_PIC_DMA0
]);
2796 s
->drq
= s3c_dma_get(s
->dma
);
2798 s
->clkpwr_base
= 0x4c000000;
2799 s3c_clkpwr_reset(s
);
2801 iomemtype
= cpu_register_io_memory(0, s3c_clkpwr_readfn
,
2802 s3c_clkpwr_writefn
, s
);
2803 cpu_register_physical_memory(s
->clkpwr_base
, 0xffffff, iomemtype
);
2804 register_savevm("s3c24xx_clkpwr", 0, 0,
2805 s3c_clkpwr_save
, s3c_clkpwr_load
, s
);
2807 s
->lcd
= s3c_lcd_init(0x4d000000, s
->irq
[S3C_PIC_LCD
]);
2809 if (s
->cpu_id
== S3C_CPU_2440
)
2810 s
->nand
= s3c2440_nand_init();
2812 s
->nand
= s3c2410_nand_init();
2814 for (i
= 0; s3c2410_uart
[i
].base
; i
++) {
2815 s
->uart
[i
] = s3c_uart_init(&s
->clock
,
2816 s3c2410_uart
[i
].base
,
2817 &s
->irq
[s3c2410_uart
[i
].irq
[0]],
2818 &s
->drq
[s3c2410_uart
[i
].dma
[0]]);
2820 s3c_uart_attach(s
->uart
[i
], serial_hds
[i
]);
2823 s
->timers
= s3c_timers_init(&s
->clock
, 0x51000000, &s
->irq
[S3C_PIC_TIMER0
], s
->drq
);
2825 s
->udc
= s3c_udc_init(0x52000000, s
->irq
[S3C_PIC_USBD
], s
->drq
);
2827 s
->wdt
= s3c_wdt_init(&s
->clock
, 0x53000000, s
->irq
[S3C_PIC_WDT
]);
2829 s
->i2c
= s3c_i2c_init(0x54000000, s
->irq
[S3C_PIC_IIC
]);
2831 s
->i2s
= s3c_i2s_init(0x55000000, s
->drq
);
2833 s
->io
= s3c_gpio_init(0x56000000, s
->irq
, s
->cpu_id
);
2835 s
->rtc
= s3c_rtc_init(0x57000000, s
->irq
[S3C_PIC_RTC
]);
2837 s
->adc
= s3c_adc_init(0x58000000, s
->irq
[S3C_PICS_ADC
],
2838 s
->irq
[S3C_PICS_TC
]);
2840 s
->spi
= s3c_spi_init(0x59000000,
2841 s
->irq
[S3C_PIC_SPI0
], s
->drq
[S3C_RQ_SPI0
],
2842 s
->irq
[S3C_PIC_SPI1
], s
->drq
[S3C_RQ_SPI1
], s
->io
);
2844 s
->mmci
= s3c_mmci_init(0x5a000000, s
->cpu_id
, mmc
,
2845 s
->irq
[S3C_PIC_SDI
], s
->drq
);
2848 usb_ohci_init_pxa(0x49000000, 3, -1, s
->irq
[S3C_PIC_USBH
]);
2851 qemu_register_reset(s3c2410_reset
, s
);
2853 s
->nand
->setwp(s
->nand
, 1);
2855 /* Power on reset */
2856 s3c_gpio_setpwrstat(s
->io
, 1);