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"
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
;
236 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
242 static void s3c_pic_write(void *opaque
, target_phys_addr_t addr
,
245 struct s3c_pic_state_s
*s
= (struct s3c_pic_state_s
*) opaque
;
250 if (value
& s
->intmod
)
254 if (s
->intpnd
& value
) {
257 s3c_pic_arbitrate(s
);
262 if (s
->intpnd
& value
) {
266 s3c_pic_arbitrate(s
);
275 s
->subsrcpnd
&= ~value
;
278 s
->intsubmsk
= value
;
279 s3c_pic_subupdate(s
);
282 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
286 static CPUReadMemoryFunc
*s3c_pic_readfn
[] = {
292 static CPUWriteMemoryFunc
*s3c_pic_writefn
[] = {
298 static void s3c_pic_save(QEMUFile
*f
, void *opaque
)
300 struct s3c_pic_state_s
*s
= (struct s3c_pic_state_s
*) opaque
;
301 qemu_put_be32s(f
, &s
->srcpnd
);
302 qemu_put_be32s(f
, &s
->intpnd
);
303 qemu_put_be32s(f
, &s
->intmsk
);
304 qemu_put_be32s(f
, &s
->intmod
);
305 qemu_put_be32s(f
, &s
->priority
);
306 qemu_put_be32s(f
, &s
->subsrcpnd
);
307 qemu_put_be32s(f
, &s
->intsubmsk
);
308 qemu_put_be32(f
, s
->intoffset
);
311 static int s3c_pic_load(QEMUFile
*f
, void *opaque
, int version_id
)
313 struct s3c_pic_state_s
*s
= (struct s3c_pic_state_s
*) opaque
;
314 qemu_get_be32s(f
, &s
->srcpnd
);
315 qemu_get_be32s(f
, &s
->intpnd
);
316 qemu_get_be32s(f
, &s
->intmsk
);
317 qemu_get_be32s(f
, &s
->intmod
);
318 qemu_get_be32s(f
, &s
->priority
);
319 qemu_get_be32s(f
, &s
->subsrcpnd
);
320 qemu_get_be32s(f
, &s
->intsubmsk
);
321 s
->intoffset
= qemu_get_be32(f
);
326 struct s3c_pic_state_s
*s3c_pic_init(target_phys_addr_t base
,
330 struct s3c_pic_state_s
*s
= (struct s3c_pic_state_s
*)
331 qemu_mallocz(sizeof(struct s3c_pic_state_s
));
334 s
->parent_pic
= arm_pic
;
335 s
->irqs
= qemu_allocate_irqs(s3c_pic_set_irq
, s
, S3C_PIC_MAX
);
339 iomemtype
= cpu_register_io_memory(0, s3c_pic_readfn
,
341 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
343 register_savevm("s3c24xx_pic", 0, 0, s3c_pic_save
, s3c_pic_load
, s
);
348 qemu_irq
*s3c_pic_get(struct s3c_pic_state_s
*s
)
353 /* Memory controller */
354 #define S3C_BWSCON 0x00 /* Bus Width & Wait Control register */
355 #define S3C_BANKCON0 0x04 /* Bank 0 Control register */
356 #define S3C_BANKCON1 0x08 /* Bank 1 Control register */
357 #define S3C_BANKCON2 0x0c /* Bank 2 Control register */
358 #define S3C_BANKCON3 0x10 /* Bank 3 Control register */
359 #define S3C_BANKCON4 0x14 /* Bank 4 Control register */
360 #define S3C_BANKCON5 0x18 /* Bank 5 Control register */
361 #define S3C_BANKCON6 0x1c /* Bank 6 Control register */
362 #define S3C_BANKCON7 0x20 /* Bank 7 Control register */
363 #define S3C_REFRESH 0x24 /* SDRAM Refresh Control register */
364 #define S3C_BANKSIZE 0x28 /* Flexible Bank Size register */
365 #define S3C_MRSRB6 0x2c /* Bank 6 Mode Set register */
366 #define S3C_MRSRB7 0x30 /* Bank 6 Mode Set register */
368 static void s3c_mc_reset(struct s3c_state_s
*s
)
370 s
->mc_regs
[S3C_BWSCON
>> 2] = 0x0000000;
371 s
->mc_regs
[S3C_BANKCON0
>> 2] = 0x0700;
372 s
->mc_regs
[S3C_BANKCON1
>> 2] = 0x0700;
373 s
->mc_regs
[S3C_BANKCON2
>> 2] = 0x0700;
374 s
->mc_regs
[S3C_BANKCON3
>> 2] = 0x0700;
375 s
->mc_regs
[S3C_BANKCON4
>> 2] = 0x0700;
376 s
->mc_regs
[S3C_BANKCON5
>> 2] = 0x0700;
377 s
->mc_regs
[S3C_BANKCON6
>> 2] = 0x18008;
378 s
->mc_regs
[S3C_BANKCON7
>> 2] = 0x18008;
379 s
->mc_regs
[S3C_REFRESH
>> 2] = 0xac0000;
380 s
->mc_regs
[S3C_BANKSIZE
>> 2] = 0x2;
381 s
->mc_regs
[S3C_MRSRB6
>> 2] = 0x00;
382 s
->mc_regs
[S3C_MRSRB7
>> 2] = 0x00;
385 static uint32_t s3c_mc_read(void *opaque
, target_phys_addr_t addr
)
387 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
390 case S3C_BWSCON
... S3C_MRSRB7
:
391 return s
->mc_regs
[addr
>> 2];
393 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
399 static void s3c_mc_write(void *opaque
, target_phys_addr_t addr
,
402 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
405 case S3C_BWSCON
... S3C_MRSRB7
:
406 s
->mc_regs
[addr
>> 2] = value
;
409 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
413 static CPUReadMemoryFunc
*s3c_mc_readfn
[] = {
419 static CPUWriteMemoryFunc
*s3c_mc_writefn
[] = {
425 static void s3c_mc_save(QEMUFile
*f
, void *opaque
)
427 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
429 for (i
= 0; i
< 13; i
++)
430 qemu_put_be32s(f
, &s
->mc_regs
[i
]);
433 static int s3c_mc_load(QEMUFile
*f
, void *opaque
, int version_id
)
435 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
437 for (i
= 0; i
< 13; i
++)
438 qemu_get_be32s(f
, &s
->mc_regs
[i
]);
442 /* Clock & power management */
443 #define S3C_LOCKTIME 0x00 /* PLL Lock Time Count register */
444 #define S3C_MPLLCON 0x04 /* MPLL Configuration register */
445 #define S3C_UPLLCON 0x08 /* UPLL Configuration register */
446 #define S3C_CLKCON 0x0c /* Clock Generator Control register */
447 #define S3C_CLKSLOW 0x10 /* Slow Clock Control register */
448 #define S3C_CLKDIVN 0x14 /* Clock Divider Control register */
450 #define S3C2440_CAMDIVN 0x18 /* Camera Clock Divider register */
452 static void s3c_clkpwr_update(struct s3c_state_s
*s
)
454 uint32_t mpll
[2] = { s
->clkpwr_regs
[S3C_MPLLCON
>> 2], s
->clkpwr_regs
[S3C_UPLLCON
>> 2] };
458 for (i
= 0; i
< 2; i
++) {
459 uint32_t mdiv
= ((mpll
[i
] >> 12) & 0xff) + 8,
460 pdiv
= ((mpll
[i
] >> 4) & 0x3f) + 2,
461 sdiv
= (mpll
[i
]) & 0x3;
462 clk
[i
] = (mdiv
* s
->clock
.xtal
* 2) / (pdiv
* (1 << sdiv
));
465 s
->clock
.clk
= clk
[0];
466 uint32_t ratio
= s
->clkpwr_regs
[S3C_CLKDIVN
>> 2];
468 switch( (ratio
& 0x6) >> 1 ) {
470 s
->clock
.hclk
= s
->clock
.clk
;
473 s
->clock
.hclk
= s
->clock
.clk
/2;
476 s
->clock
.hclk
= s
->clock
.clk
/4;
479 s
->clock
.hclk
= s
->clock
.clk
/3;
482 switch ( ratio
&0x1) {
484 s
->clock
.pclk
= s
->clock
.hclk
;
487 s
->clock
.pclk
= s
->clock
.hclk
/2;
490 s
->clock
.uclk
= clk
[1] / 2;
492 printf("S3C: CLK=%d HCLK=%d PCLK=%d UCLK=%d\n",
493 s
->clock
.clk
/MHZ
, s
->clock
.hclk
/MHZ
, s
->clock
.pclk
/MHZ
,
497 static void s3c_clkpwr_reset(struct s3c_state_s
*s
)
499 s
->clkpwr_regs
[S3C_LOCKTIME
>> 2] = 0x00ffffff;
500 s
->clkpwr_regs
[S3C_MPLLCON
>> 2] = 0x0005c080;
501 s
->clkpwr_regs
[S3C_UPLLCON
>> 2] = 0x00028080;
502 s
->clkpwr_regs
[S3C_CLKCON
>> 2] = 0x0007fff0;
503 s
->clkpwr_regs
[S3C_CLKSLOW
>> 2] = 0x00000004;
504 s
->clkpwr_regs
[S3C_CLKDIVN
>> 2] = 0x00000000;
505 s
->clkpwr_regs
[S3C2440_CAMDIVN
>> 2] = 0x00000000;
506 s3c_clkpwr_update(s
);
509 static uint32_t s3c_clkpwr_read(void *opaque
, target_phys_addr_t addr
)
511 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
514 case S3C_LOCKTIME
... S3C_CLKDIVN
:
515 return s
->clkpwr_regs
[addr
>> 2];
516 case S3C2440_CAMDIVN
:
517 if (s
->cpu_id
== S3C_CPU_2440
)
518 return s
->clkpwr_regs
[addr
>> 2];
520 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
526 static void s3c_clkpwr_write(void *opaque
, target_phys_addr_t addr
,
529 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
536 s
->clkpwr_regs
[addr
>> 2] = value
;
537 if (addr
!= S3C_LOCKTIME
)
538 s3c_clkpwr_update(s
);
541 if (value
& (1 << 3)) {
542 cpu_interrupt(s
->env
, CPU_INTERRUPT_HALT
);
543 printf("%s: processor powered off\n", __FUNCTION__
);
544 s3c_gpio_setpwrstat(s
->io
, 2);
547 s
->env
->regs
[15] = 0; /* XXX */
550 if (value
& (1 << 2)) /* Normal IDLE mode */
551 cpu_interrupt(s
->env
, CPU_INTERRUPT_HALT
);
552 if ((s
->clkpwr_regs
[addr
>> 2] ^ value
) & 1)
553 printf("%s: SPECIAL mode %s\n", __FUNCTION__
,
554 (value
& 1) ? "on" : "off");
555 s
->clkpwr_regs
[addr
>> 2] = value
;
558 if ((s
->clkpwr_regs
[addr
>> 2] ^ value
) & (1 << 4))
559 printf("%s: SLOW mode %s\n", __FUNCTION__
,
560 (value
& (1 << 4)) ? "on" : "off");
561 s
->clkpwr_regs
[addr
>> 2] = value
;
563 case S3C2440_CAMDIVN
:
564 if (s
->cpu_id
== S3C_CPU_2440
) {
565 s
->clkpwr_regs
[addr
>> 2] = value
;
569 printf("%s: Bad register 0x%x (cpu %08x)\n", __FUNCTION__
, /*(unsigned long)*/addr
, s
->cpu_id
);
573 static CPUReadMemoryFunc
*s3c_clkpwr_readfn
[] = {
579 static CPUWriteMemoryFunc
*s3c_clkpwr_writefn
[] = {
585 static void s3c_clkpwr_save(QEMUFile
*f
, void *opaque
)
587 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
589 for (i
= 0; i
< 7; i
++)
590 qemu_put_be32s(f
, &s
->clkpwr_regs
[i
]);
593 static int s3c_clkpwr_load(QEMUFile
*f
, void *opaque
, int version_id
)
595 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
597 for (i
= 0; i
< 7; i
++)
598 qemu_get_be32s(f
, &s
->clkpwr_regs
[i
]);
603 #define S3C_DMA_CH_N 4
605 struct s3c_dma_ch_state_s
;
606 struct s3c_dma_state_s
{ /* Modelled as an interrupt controller */
607 target_phys_addr_t base
;
609 struct s3c_dma_ch_state_s
{
625 static inline void s3c_dma_ch_run(struct s3c_dma_state_s
*s
,
626 struct s3c_dma_ch_state_s
*ch
)
630 width
= 1 << ((ch
->con
>> 20) & 3); /* DSZ */
631 burst
= (ch
->con
& (1 << 28)) ? 4 : 1; /* TSZ */
633 while (!ch
->running
&& ch
->curr_tc
> 0 && ch
->req
&&
634 (ch
->mask
& (1 << 1))) { /* ON_OFF */
635 if (width
> sizeof(buffer
)) {
636 printf("%s: wrong access width\n", __FUNCTION__
);
640 while (ch
->curr_tc
--) {
641 for (t
= 0; t
< burst
; t
++) {
642 cpu_physical_memory_read(ch
->csrc
, buffer
, width
);
643 cpu_physical_memory_write(ch
->cdst
, buffer
, width
);
645 if (!(ch
->isrcc
& 1)) /* INT */
647 if (!(ch
->idstc
& 1)) /* INT */
651 if (!(ch
->con
& (1 << 27)) && !ch
->req
) /* SERVMODE */
656 if (!(ch
->con
& (1 << 23))) { /* SWHW_SEL */
660 if (ch
->curr_tc
<= 0) {
661 if (ch
->con
& (1 << 22)) /* RELOAD */
662 ch
->mask
&= ~(1 << 1); /* ON_OFF */
664 if (!(ch
->con
& (1 << 23))) { /* SWHW_SEL */
665 printf("%s: auto-reload software controlled transfer\n",
669 ch
->csrc
= ch
->isrc
; /* S_ADDR */
670 ch
->cdst
= ch
->idst
; /* D_ADDR */
671 ch
->curr_tc
= ch
->con
& 0xfffff; /* TC */
672 ch
->con
|= 1 << 22; /* ON_OFF */
675 if (ch
->con
& (1 << 31)) /* DMD_HS */
678 if (ch
->con
& (1 << 29)) { /* INT */
679 qemu_irq_raise(ch
->intr
);
680 /* Give the system a chance to respond. */
687 static void s3c_dma_reset(struct s3c_dma_state_s
*s
)
690 for (i
= 0; i
< S3C_DMA_CH_N
; i
++) {
691 s
->ch
[i
].curr_tc
= 0;
705 static void s3c_dma_dreq(void *opaque
, int line
, int req
)
707 struct s3c_dma_state_s
*s
= (struct s3c_dma_state_s
*) opaque
;
708 struct s3c_dma_ch_state_s
*ch
= &s
->ch
[line
>> 4];
710 if (ch
->con
& (1 << 23)) /* SWHW_SEL */
711 if (((ch
->con
>> 24) & 7) == (line
& 7)) { /* HWSRCSEL */
713 s3c_dma_ch_run(s
, ch
);
717 #define S3C_DISRC 0x00 /* DMA Initial Source register */
718 #define S3C_DISRCC 0x04 /* DMA Initial Source Control register */
719 #define S3C_DIDST 0x08 /* DMA Initial Destination register */
720 #define S3C_DIDSTC 0x0c /* DMA Initial Destination Control register */
721 #define S3C_DCON 0x10 /* DMA Control register */
722 #define S3C_DSTAT 0x14 /* DMA Count register */
723 #define S3C_DCSRC 0x18 /* DMA Current Source register */
724 #define S3C_DCDST 0x1c /* DMA Current Destination register */
725 #define S3C_DMASKTRIG 0x20 /* DMA Mask Trigger register */
727 static uint32_t s3c_dma_read(void *opaque
, target_phys_addr_t addr
)
729 struct s3c_dma_state_s
*s
= (struct s3c_dma_state_s
*) opaque
;
730 struct s3c_dma_ch_state_s
*ch
= 0;
732 if (addr
>= 0 && addr
<= (S3C_DMA_CH_N
<< 6)) {
733 ch
= &s
->ch
[addr
>> 6];
757 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
763 static void s3c_dma_write(void *opaque
, target_phys_addr_t addr
,
766 struct s3c_dma_state_s
*s
= (struct s3c_dma_state_s
*) opaque
;
767 struct s3c_dma_ch_state_s
*ch
= 0;
769 if (addr
>= 0 && addr
<= (S3C_DMA_CH_N
<< 6)) {
770 ch
= &s
->ch
[addr
>> 6];
791 if (~ch
->mask
& value
& (1 << 1)) { /* ON_OFF */
792 ch
->curr_tc
= ch
->con
& 0xfffff; /* TC */
793 ch
->csrc
= ch
->isrc
; /* S_ADDR */
794 ch
->cdst
= ch
->idst
; /* D_ADDR */
798 if (value
& (1 << 2)) { /* STOP */
799 ch
->mask
&= ~(3 << 1); /* ON_OFF */
800 } else if (!(ch
->con
& (1 << 23))) { /* SWHW_SEL */
801 ch
->req
= value
& 1; /* SW_TRIG */
802 s3c_dma_ch_run(s
, ch
);
806 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
810 static CPUReadMemoryFunc
*s3c_dma_readfn
[] = {
816 static CPUWriteMemoryFunc
*s3c_dma_writefn
[] = {
822 static void s3c_dma_save(QEMUFile
*f
, void *opaque
)
824 struct s3c_dma_state_s
*s
= (struct s3c_dma_state_s
*) opaque
;
826 for (i
= 0; i
< S3C_DMA_CH_N
; i
++) {
827 qemu_put_be32(f
, s
->ch
[i
].curr_tc
);
828 qemu_put_be32(f
, s
->ch
[i
].req
);
829 qemu_put_be32s(f
, &s
->ch
[i
].con
);
830 qemu_put_be32s(f
, &s
->ch
[i
].isrc
);
831 qemu_put_be32s(f
, &s
->ch
[i
].isrcc
);
832 qemu_put_be32s(f
, &s
->ch
[i
].idst
);
833 qemu_put_be32s(f
, &s
->ch
[i
].idstc
);
834 qemu_put_be32s(f
, &s
->ch
[i
].csrc
);
835 qemu_put_be32s(f
, &s
->ch
[i
].cdst
);
836 qemu_put_be32s(f
, &s
->ch
[i
].mask
);
840 static int s3c_dma_load(QEMUFile
*f
, void *opaque
, int version_id
)
842 struct s3c_dma_state_s
*s
= (struct s3c_dma_state_s
*) opaque
;
844 for (i
= 0; i
< S3C_DMA_CH_N
; i
++) {
845 s
->ch
[i
].curr_tc
= qemu_get_be32(f
);
846 s
->ch
[i
].req
= qemu_get_be32(f
);
847 qemu_get_be32s(f
, &s
->ch
[i
].con
);
848 qemu_get_be32s(f
, &s
->ch
[i
].isrc
);
849 qemu_get_be32s(f
, &s
->ch
[i
].isrcc
);
850 qemu_get_be32s(f
, &s
->ch
[i
].idst
);
851 qemu_get_be32s(f
, &s
->ch
[i
].idstc
);
852 qemu_get_be32s(f
, &s
->ch
[i
].csrc
);
853 qemu_get_be32s(f
, &s
->ch
[i
].cdst
);
854 qemu_get_be32s(f
, &s
->ch
[i
].mask
);
859 struct s3c_dma_state_s
*s3c_dma_init(target_phys_addr_t base
, qemu_irq
*pic
)
862 struct s3c_dma_state_s
*s
= (struct s3c_dma_state_s
*)
863 qemu_mallocz(sizeof(struct s3c_dma_state_s
));
866 s
->ch
[0].intr
= pic
[0];
867 s
->ch
[1].intr
= pic
[1];
868 s
->ch
[2].intr
= pic
[2];
869 s
->ch
[3].intr
= pic
[3];
870 s
->drqs
= qemu_allocate_irqs(s3c_dma_dreq
, s
, S3C_RQ_MAX
);
874 iomemtype
= cpu_register_io_memory(0, s3c_dma_readfn
,
876 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
878 register_savevm("s3c24xx_dma", 0, 0, s3c_dma_save
, s3c_dma_load
, s
);
883 qemu_irq
*s3c_dma_get(struct s3c_dma_state_s
*s
)
888 /* PWM timers controller */
889 struct s3c_timer_state_s
;
890 struct s3c_timers_state_s
{
891 struct s3c_freq_s
* freq
;
892 target_phys_addr_t base
;
895 struct s3c_timer_state_s
{
897 struct s3c_timers_state_s
*s
;
904 gpio_handler_t cmp_cb
;
908 uint16_t compareb
[4];
914 static const int s3c_tm_bits
[] = { 0, 8, 12, 16, 20 };
916 static uint16_t s3c_timers_get(struct s3c_timers_state_s
*s
, int tm
)
919 if (!s
->timer
[tm
].running
)
920 return s
->timer
[tm
].count
;
922 elapsed
= muldiv64(qemu_get_clock(vm_clock
) - s
->timer
[tm
].reload
,
923 s
->timer
[tm
].divider
, ticks_per_sec
);
924 if (unlikely(elapsed
> s
->timer
[tm
].count
))
925 return s
->timer
[tm
].count
;
927 return s
->timer
[tm
].count
- elapsed
;
930 static void s3c_timers_stop(struct s3c_timers_state_s
*s
, int tm
)
932 s
->timer
[tm
].count
= s3c_timers_get(s
, tm
);
933 s
->timer
[tm
].running
= 0;
936 static void s3c_timers_start(struct s3c_timers_state_s
*s
, int tm
)
938 if (s
->timer
[tm
].running
)
941 s
->timer
[tm
].divider
= s
->freq
->pclk
>>
942 (((s
->config
[1] >> (tm
* 4)) & 3) + 1);
944 s
->timer
[tm
].divider
/= ((s
->config
[0] >> 0) & 0xff) + 1;
946 s
->timer
[tm
].divider
/= ((s
->config
[0] >> 8) & 0xff) + 1;
947 s
->timer
[tm
].running
= 1;
948 s
->timer
[tm
].reload
= qemu_get_clock(vm_clock
);
949 qemu_mod_timer(s
->timer
[tm
].t
,
950 s
->timer
[tm
].reload
+ muldiv64(s
->timer
[tm
].count
,
951 ticks_per_sec
, s
->timer
[tm
].divider
));
954 static void s3c_timers_reset(struct s3c_timers_state_s
*s
)
957 s
->config
[0] = 0x00000000;
958 s
->config
[1] = 0x00000000;
959 s
->control
= 0x00000000;
961 for (i
= 0; i
< 5; i
++) {
962 if (s
->timer
[i
].running
)
963 s3c_timers_stop(s
, i
);
964 s
->countb
[i
] = 0x0000;
965 s
->timer
[i
].count
= 0;
967 for (i
= 0; i
< 4; i
++)
968 s
->compareb
[i
] = 0x0000;
971 static void s3c_timers_tick(void *opaque
)
973 struct s3c_timer_state_s
*t
= (struct s3c_timer_state_s
*) opaque
;
974 struct s3c_timers_state_s
*s
= t
->s
;
978 if (((s
->config
[1] >> 20) & 0xf) == t
->n
+ 1) {
979 qemu_irq_raise(s
->dma
[S3C_RQ_TIMER0
]); /* TODO */
980 qemu_irq_raise(s
->dma
[S3C_RQ_TIMER1
]);
981 qemu_irq_raise(s
->dma
[S3C_RQ_TIMER2
]);
983 qemu_irq_raise(t
->irq
);
988 if (s
->control
& (1 << ((t
->n
== 4) ? 22 : (s3c_tm_bits
[t
->n
] + 3)))) {
990 t
->count
= s
->countb
[t
->n
];
991 s3c_timers_start(s
, t
->n
);
993 s
->control
&= ~(1 << s3c_tm_bits
[t
->n
]);
996 #define S3C_TCFG0 0x00 /* Timer Configuration register 0 */
997 #define S3C_TCFG1 0x04 /* Timer Configuration register 1 */
998 #define S3C_TCON 0x08 /* Timer Control register */
999 #define S3C_TCNTB0 0x0c /* Timer 0 Count Buffer register */
1000 #define S3C_TCMPB0 0x10 /* Timer 0 Compare Buffer register */
1001 #define S3C_TCNTO0 0x14 /* Timer 0 Count Observation register */
1002 #define S3C_TCNTB1 0x18 /* Timer 1 Count Buffer register */
1003 #define S3C_TCMPB1 0x1c /* Timer 1 Compare Buffer register */
1004 #define S3C_TCNTO1 0x20 /* Timer 1 Count Observation register */
1005 #define S3C_TCNTB2 0x24 /* Timer 2 Count Buffer register */
1006 #define S3C_TCMPB2 0x28 /* Timer 2 Compare Buffer register */
1007 #define S3C_TCNTO2 0x2c /* Timer 2 Count Observation register */
1008 #define S3C_TCNTB3 0x30 /* Timer 3 Count Buffer register */
1009 #define S3C_TCMPB3 0x34 /* Timer 3 Compare Buffer register */
1010 #define S3C_TCNTO3 0x38 /* Timer 3 Count Observation register */
1011 #define S3C_TCNTB4 0x3c /* Timer 4 Count Buffer register */
1012 #define S3C_TCNTO4 0x40 /* Timer 4 Count Observation register */
1014 static uint32_t s3c_timers_read(void *opaque
, target_phys_addr_t addr
)
1016 struct s3c_timers_state_s
*s
= (struct s3c_timers_state_s
*) opaque
;
1021 return s
->config
[0];
1023 return s
->config
[1];
1026 case S3C_TCMPB3
: tm
++;
1027 case S3C_TCMPB2
: tm
++;
1028 case S3C_TCMPB1
: tm
++;
1030 return s
->compareb
[tm
];
1031 case S3C_TCNTB4
: tm
++;
1032 case S3C_TCNTB3
: tm
++;
1033 case S3C_TCNTB2
: tm
++;
1034 case S3C_TCNTB1
: tm
++;
1036 return s
->countb
[tm
];
1037 case S3C_TCNTO4
: tm
++;
1038 case S3C_TCNTO3
: tm
++;
1039 case S3C_TCNTO2
: tm
++;
1040 case S3C_TCNTO1
: tm
++;
1042 return s3c_timers_get(s
, tm
);
1044 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1050 static void s3c_timers_write(void *opaque
, target_phys_addr_t addr
,
1053 struct s3c_timers_state_s
*s
= (struct s3c_timers_state_s
*) opaque
;
1058 s
->config
[0] = value
& 0x00ffffff;
1061 s
->config
[1] = value
& 0x00ffffff;
1064 for (tm
= 0; tm
< 5; tm
++) {
1065 if (value
& (2 << (s3c_tm_bits
[tm
]))) {
1066 if (s
->timer
[tm
].running
) {
1067 s3c_timers_stop(s
, tm
);
1068 s
->timer
[tm
].count
= s
->countb
[tm
];
1069 s3c_timers_start(s
, tm
);
1071 s
->timer
[tm
].count
= s
->countb
[tm
];
1073 if (((value
>> s3c_tm_bits
[tm
]) & 1) ^ s
->timer
[tm
].running
) {
1074 if (s
->timer
[tm
].running
)
1075 s3c_timers_stop(s
, tm
);
1077 s3c_timers_start(s
, tm
);
1081 s
->control
= value
& 0x007fff1f;
1083 case S3C_TCMPB3
: tm
++;
1084 case S3C_TCMPB2
: tm
++;
1085 case S3C_TCMPB1
: tm
++;
1087 s
->compareb
[tm
] = value
& 0xffff;
1088 if (s
->timer
[tm
].cmp_cb
)
1089 s
->timer
[tm
].cmp_cb(tm
, s
->compareb
[tm
], s
->timer
[tm
].cmp_opaque
);
1091 case S3C_TCNTB4
: tm
++;
1092 case S3C_TCNTB3
: tm
++;
1093 case S3C_TCNTB2
: tm
++;
1094 case S3C_TCNTB1
: tm
++;
1096 s
->countb
[tm
] = value
& 0xffff;
1099 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1103 static CPUReadMemoryFunc
*s3c_timers_readfn
[] = {
1109 static CPUWriteMemoryFunc
*s3c_timers_writefn
[] = {
1115 static void s3c_timers_save(QEMUFile
*f
, void *opaque
)
1117 struct s3c_timers_state_s
*s
= (struct s3c_timers_state_s
*) opaque
;
1119 for (i
= 0; i
< 5; i
++) {
1120 qemu_put_be32(f
, s
->timer
[i
].running
);
1121 qemu_put_be32s(f
, &s
->timer
[i
].divider
);
1122 qemu_put_be16(f
, s3c_timers_get(s
, i
));
1123 qemu_put_sbe64s(f
, &s
->timer
[i
].reload
);
1126 for (i
= 0; i
< 4; i
++)
1127 qemu_put_be16s(f
, &s
->compareb
[i
]);
1128 for (i
= 0; i
< 5; i
++)
1129 qemu_put_be16s(f
, &s
->countb
[i
]);
1130 for (i
= 0; i
< 2; i
++)
1131 qemu_put_be32s(f
, &s
->config
[i
]);
1132 qemu_put_be32s(f
, &s
->control
);
1135 static int s3c_timers_load(QEMUFile
*f
, void *opaque
, int version_id
)
1137 struct s3c_timers_state_s
*s
= (struct s3c_timers_state_s
*) opaque
;
1139 for (i
= 0; i
< 5; i
++) {
1140 s
->timer
[i
].running
= 0;
1141 running
[i
] = qemu_get_be32(f
);
1142 qemu_get_be32s(f
, &s
->timer
[i
].divider
);
1143 qemu_get_be16s(f
, &s
->timer
[i
].count
);
1144 qemu_get_sbe64s(f
, &s
->timer
[i
].reload
);
1147 for (i
= 0; i
< 4; i
++)
1148 qemu_get_be16s(f
, &s
->compareb
[i
]);
1149 for (i
= 0; i
< 5; i
++)
1150 qemu_get_be16s(f
, &s
->countb
[i
]);
1151 for (i
= 0; i
< 2; i
++)
1152 qemu_get_be32s(f
, &s
->config
[i
]);
1153 qemu_get_be32s(f
, &s
->control
);
1155 for (i
= 0; i
< 5; i
++)
1157 s3c_timers_start(s
, i
);
1162 struct s3c_timers_state_s
*s3c_timers_init(struct s3c_freq_s
* freq
, target_phys_addr_t base
,
1163 qemu_irq
*pic
, qemu_irq
*dma
)
1166 struct s3c_timers_state_s
*s
= (struct s3c_timers_state_s
*)
1167 qemu_mallocz(sizeof(struct s3c_timers_state_s
));
1173 s3c_timers_reset(s
);
1175 for (i
= 0; i
< 5; i
++) {
1176 s
->timer
[i
].t
= qemu_new_timer(vm_clock
,
1177 s3c_timers_tick
, &s
->timer
[i
]);
1180 s
->timer
[i
].cmp_cb
= 0;
1181 s
->timer
[i
].irq
= pic
[i
];
1184 iomemtype
= cpu_register_io_memory(0, s3c_timers_readfn
,
1185 s3c_timers_writefn
, s
);
1186 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
1188 register_savevm("s3c24xx_timers", 0, 0,
1189 s3c_timers_save
, s3c_timers_load
, s
);
1194 void s3c_timers_cmp_handler_set(void *opaque
, int line
,
1195 gpio_handler_t handler
, void *cmp_opaque
)
1197 struct s3c_timers_state_s
*s
= (struct s3c_timers_state_s
*) opaque
;
1198 if (line
> 4 || line
< 0) {
1199 printf("%s: Bad timer number %i.\n", __FUNCTION__
, line
);
1202 s
->timer
[line
].cmp_cb
= handler
;
1203 s
->timer
[line
].cmp_opaque
= cmp_opaque
;
1207 struct s3c_uart_state_s
{
1208 struct s3c_freq_s
* freq
;
1209 target_phys_addr_t base
;
1216 #define UART_MAX_CHR 4
1218 CharDriverState
*chr
[UART_MAX_CHR
];
1228 static void s3c_uart_reset(struct s3c_uart_state_s
*s
)
1233 s
->control
= 0x0000;
1240 static void s3c_uart_err(struct s3c_uart_state_s
*s
, int err
)
1243 if (s
->control
& (1 << 6))
1244 qemu_irq_raise(s
->irq
[2]);
1247 inline static void s3c_uart_full(struct s3c_uart_state_s
*s
, int pulse
)
1249 if (s
->fcontrol
& 1) /* FIFOEnable */
1250 if (s
->rxlen
< (((s
->fcontrol
>> 4) & 3) + 1) * 4) {
1251 if (((s
->control
>> 0) & 3) != 1 || /* ReceiveMode */
1254 if (!(s
->control
& (1 << 7))) /* RxTimeOutEnable */
1256 /* When the Rx FIFO trigger level is not reached, the interrupt
1257 * is generated anyway, just after a small timeout instead of
1261 switch ((s
->control
>> 0) & 3) { /* ReceiveMode */
1263 if ((s
->control
& (1 << 8)) || pulse
) /* RxInterruptType */
1264 qemu_irq_raise(s
->irq
[0]);
1268 qemu_irq_raise(s
->dma
[0]);
1273 inline static void s3c_uart_empty(struct s3c_uart_state_s
*s
, int pulse
)
1275 switch ((s
->control
>> 2) & 3) { /* TransmitMode */
1277 if ((s
->control
& (1 << 9)) || pulse
) /* TxInterruptType */
1278 qemu_irq_raise(s
->irq
[1]);
1282 qemu_irq_raise(s
->dma
[0]);
1287 inline static void s3c_uart_update(struct s3c_uart_state_s
*s
)
1289 s3c_uart_empty(s
, 0);
1290 s3c_uart_full(s
, 0);
1293 static void s3c_uart_params_update(struct s3c_uart_state_s
*s
)
1295 QEMUSerialSetParams ssp
;
1300 /* XXX Calculate PCLK frequency from clock manager registers */
1301 ssp
.speed
= (s
->freq
->pclk
>> 4) / (s
->brdiv
+ 1);
1303 switch ((s
->lcontrol
>> 3) & 7) {
1316 ssp
.data_bits
= 5 + (s
->lcontrol
& 3);
1318 ssp
.stop_bits
= (s
->lcontrol
& (1 << 2)) ? 2 : 1;
1320 for (i
= 0; i
< s
->chr_num
; i
++)
1321 qemu_chr_ioctl(s
->chr
[i
], CHR_IOCTL_SERIAL_SET_PARAMS
, &ssp
);
1324 static int s3c_uart_is_empty(void *opaque
)
1326 struct s3c_uart_state_s
*s
= (struct s3c_uart_state_s
*) opaque
;
1327 if (s
->fcontrol
& 1) /* FIFOEnable */
1328 return 16 - s
->rxlen
;
1330 return 1 - s
->rxlen
;
1333 static void s3c_uart_rx(void *opaque
, const uint8_t *buf
, int size
)
1335 struct s3c_uart_state_s
*s
= (struct s3c_uart_state_s
*) opaque
;
1337 if (s
->fcontrol
& 1) { /* FIFOEnable */
1338 if (s
->rxlen
+ size
> 16) {
1339 size
= 16 - s
->rxlen
;
1343 left
= 16 - ((s
->rxstart
+ s
->rxlen
) & 15);
1345 memcpy(s
->rxfifo
+ ((s
->rxstart
+ s
->rxlen
) & 15), buf
, left
);
1346 memcpy(s
->rxfifo
, buf
+ left
, size
- left
);
1348 memcpy(s
->rxfifo
+ ((s
->rxstart
+ s
->rxlen
) & 15), buf
, size
);
1351 if (s
->rxlen
+ size
> 1)
1356 s3c_uart_full(s
, 1);
1359 /* S3C2410 UART doesn't seem to understand break conditions. */
1360 static void s3c_uart_event(void *opaque
, int event
)
1364 #define S3C_ULCON 0x00 /* UART Line Control register */
1365 #define S3C_UCON 0x04 /* UART Control register */
1366 #define S3C_UFCON 0x08 /* UART FIFO Control register */
1367 #define S3C_UMCON 0x0c /* UART Modem Control register */
1368 #define S3C_UTRSTAT 0x10 /* UART Tx/Rx Status register */
1369 #define S3C_UERSTAT 0x14 /* UART Error Status register */
1370 #define S3C_UFSTAT 0x18 /* UART FIFO Status register */
1371 #define S3C_UMSTAT 0x1c /* UART Modem Status register */
1372 #define S3C_UTXH 0x20 /* UART Transmit Buffer register */
1373 #define S3C_URXH 0x24 /* UART Receive Buffer register */
1374 #define S3C_UBRDIV 0x28 /* UART Baud Rate Divisor register */
1376 static uint32_t s3c_uart_read(void *opaque
, target_phys_addr_t addr
)
1378 struct s3c_uart_state_s
*s
= (struct s3c_uart_state_s
*) opaque
;
1391 return 6 | !!s
->rxlen
;
1393 /* XXX: UERSTAT[3] is Reserved but Linux thinks it is BREAK */
1400 return s
->rxlen
? s
->rxlen
| (1 << 8) : 0;
1404 case S3C_UTXH
: /* why this is called by u-boot is not clear */
1410 if (s
->fcontrol
& 1) { /* FIFOEnable */
1411 ret
= s
->rxfifo
[s
->rxstart
++];
1421 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1427 static void s3c_uart_write(void *opaque
, target_phys_addr_t addr
,
1430 struct s3c_uart_state_s
*s
= (struct s3c_uart_state_s
*) opaque
;
1436 if ((s
->lcontrol
^ value
) & (1 << 6))
1437 printf("%s: UART Infra-red mode %s\n", __FUNCTION__
,
1438 (value
& (1 << 6)) ? "on" : "off");
1439 s
->lcontrol
= value
;
1440 s3c_uart_params_update(s
);
1444 /* XXX: UCON[4] is Reserved but Linux thinks it is BREAK */
1445 if ((s
->control
^ value
) & (1 << 5))
1446 printf("%s: UART loopback test mode %s\n", __FUNCTION__
,
1447 (value
& (1 << 5)) ? "on" : "off");
1448 s
->control
= value
& 0x7ef;
1452 if (value
& (1 << 1)) /* RxReset */
1454 s
->fcontrol
= value
& 0xf1;
1458 #ifdef CONFIG_S3C_MODEM /* not handled, openmoko modem.c not imported */
1459 if ((s
->mcontrol
^ value
) & (1 << 4)) {
1460 int afc
= (value
>> 4) & 1;
1461 for (i
= 0; i
< s
->chr_num
; i
++)
1462 qemu_chr_ioctl(s
->chr
[i
], CHR_IOCTL_MODEM_HANDSHAKE
, &afc
);
1465 s
->mcontrol
= value
& 0x11;
1470 for (i
= 0; i
< s
->chr_num
; i
++)
1471 qemu_chr_write(s
->chr
[i
], &ch
, 1);
1472 s3c_uart_empty(s
, 1);
1476 s
->brdiv
= value
& 0xffff;
1477 s3c_uart_params_update(s
);
1481 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1485 static CPUReadMemoryFunc
*s3c_uart_readfn
[] = {
1491 static CPUWriteMemoryFunc
*s3c_uart_writefn
[] = {
1497 static void s3c_uart_save(QEMUFile
*f
, void *opaque
)
1499 struct s3c_uart_state_s
*s
= (struct s3c_uart_state_s
*) opaque
;
1500 qemu_put_8s(f
, &s
->data
);
1501 qemu_put_buffer(f
, s
->rxfifo
, sizeof(s
->rxfifo
));
1502 qemu_put_be32(f
, s
->rxstart
);
1503 qemu_put_be32(f
, s
->rxlen
);
1504 qemu_put_8s(f
, &s
->lcontrol
);
1505 qemu_put_8s(f
, &s
->fcontrol
);
1506 qemu_put_8s(f
, &s
->mcontrol
);
1507 qemu_put_be16s(f
, &s
->control
);
1508 qemu_put_be16s(f
, &s
->brdiv
);
1509 qemu_put_8s(f
, &s
->errstat
);
1512 static int s3c_uart_load(QEMUFile
*f
, void *opaque
, int version_id
)
1514 struct s3c_uart_state_s
*s
= (struct s3c_uart_state_s
*) opaque
;
1515 qemu_get_8s(f
, &s
->data
);
1516 qemu_get_buffer(f
, s
->rxfifo
, sizeof(s
->rxfifo
));
1517 s
->rxstart
= qemu_get_be32(f
);
1518 s
->rxlen
= qemu_get_be32(f
);
1519 qemu_get_8s(f
, &s
->lcontrol
);
1520 qemu_get_8s(f
, &s
->fcontrol
);
1521 qemu_get_8s(f
, &s
->mcontrol
);
1522 qemu_get_be16s(f
, &s
->control
);
1523 qemu_get_be16s(f
, &s
->brdiv
);
1524 qemu_get_8s(f
, &s
->errstat
);
1529 struct s3c_uart_state_s
*s3c_uart_init(struct s3c_freq_s
* freq
, target_phys_addr_t base
,
1530 qemu_irq
*irqs
, qemu_irq
*dma
)
1533 struct s3c_uart_state_s
*s
= (struct s3c_uart_state_s
*)
1534 qemu_mallocz(sizeof(struct s3c_uart_state_s
));
1543 iomemtype
= cpu_register_io_memory(0, s3c_uart_readfn
,
1544 s3c_uart_writefn
, s
);
1545 cpu_register_physical_memory(s
->base
, 0xfff, iomemtype
);
1547 register_savevm("s3c24xx_uart", base
, 0, s3c_uart_save
, s3c_uart_load
, s
);
1552 void s3c_uart_attach(struct s3c_uart_state_s
*s
, CharDriverState
*chr
)
1554 if (s
->chr_num
>= UART_MAX_CHR
)
1555 cpu_abort(cpu_single_env
, "%s: Too many devices\n", __FUNCTION__
);
1556 s
->chr
[s
->chr_num
++] = chr
;
1558 qemu_chr_add_handlers(chr
, s3c_uart_is_empty
,
1559 s3c_uart_rx
, s3c_uart_event
, s
);
1562 /* ADC & Touchscreen interface */
1563 struct s3c_adc_state_s
{
1564 target_phys_addr_t base
;
1585 static void s3c_adc_reset(struct s3c_adc_state_s
*s
)
1588 s
->control
= 0x3fc4;
1594 static void s3c_adc_start(struct s3c_adc_state_s
*s
)
1596 if (!s
->enable
|| (s
->ts
& 7) == 0)
1598 s
->control
&= ~(1 << 15);
1599 s
->in_idx
= (s
->control
>> 3) & 7;
1600 qemu_mod_timer(s
->convt
, qemu_get_clock(vm_clock
) + (ticks_per_sec
>> 5));
1603 static void s3c_adc_done(void *opaque
)
1605 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*) opaque
;
1606 s
->xdata
= s
->input
[s
->in_idx
] & 0x3ff;
1607 s
->control
|= 1 << 15;
1608 qemu_irq_raise(s
->irq
);
1611 static void s3c_adc_tick(void *opaque
)
1613 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*) opaque
;
1617 if ((s
->ts
& 3) == 3 && s
->enable
)
1618 qemu_irq_raise(s
->tcirq
);
1619 else if (s
->enable
&& ((s
->ts
& (1 << 2)) || (s
->ts
& 3))) {
1620 sx
= s
->x
* s
->scale
[0] + s
->y
* s
->scale
[1] + s
->scale
[2];
1621 sy
= s
->x
* s
->scale
[3] + s
->y
* s
->scale
[4] + s
->scale
[5];
1622 s
->xdata
= ((sx
>> 13) & 0xfff) | (1 << 14) | ((s
->ts
& 3) << 12);
1623 s
->ydata
= ((sy
>> 13) & 0xfff) | (1 << 14) | ((s
->ts
& 3) << 12);
1624 s
->xdata
^= s
->noise
>> 1;
1625 s
->ydata
^= s
->noise
>> 2;
1626 qemu_irq_raise(s
->irq
);
1630 qemu_mod_timer(s
->tst
, qemu_get_clock(vm_clock
) +
1631 (ticks_per_sec
>> 5));
1635 static void s3c_adc_event(void *opaque
,
1636 int x
, int y
, int z
, int buttons_state
)
1638 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*) opaque
;
1639 s
->down
= !!buttons_state
;
1645 #define S3C_ADCCON 0x00 /* ADC Control register */
1646 #define S3C_ADCTSC 0x04 /* ADC Touchscreen Control register */
1647 #define S3C_ADCDLY 0x08 /* ADC Start or Interval Delay register */
1648 #define S3C_ADCDAT0 0x0c /* ADC Conversion Data register 0 */
1649 #define S3C_ADCDAT1 0x10 /* ADC Conversion Data register 1 */
1651 static uint32_t s3c_adc_read(void *opaque
, target_phys_addr_t addr
)
1653 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*) opaque
;
1665 return ((!s
->down
) << 15) | s
->xdata
;
1667 return ((!s
->down
) << 15) | s
->ydata
;
1669 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1675 static void s3c_adc_write(void *opaque
, target_phys_addr_t addr
,
1678 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*) opaque
;
1682 s
->control
= (s
->control
& 0x8000) | (value
& 0x7ffe);
1683 s
->enable
= !(value
& 4);
1684 if ((value
& 1) && !(value
& 2))
1687 qemu_del_timer(s
->convt
);
1692 s
->ts
= value
& 0xff;
1696 s
->delay
= value
& 0xffff;
1700 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1704 static CPUReadMemoryFunc
*s3c_adc_readfn
[] = {
1710 static CPUWriteMemoryFunc
*s3c_adc_writefn
[] = {
1716 static void s3c_adc_save(QEMUFile
*f
, void *opaque
)
1718 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*) opaque
;
1720 qemu_put_be32(f
, s
->enable
);
1721 for (i
= 0; i
< 8; i
++)
1722 qemu_put_be32(f
, s
->input
[i
]);
1723 qemu_put_be32(f
, s
->in_idx
);
1724 qemu_put_be32(f
, s
->noise
);
1726 qemu_put_be16s(f
, &s
->control
);
1727 qemu_put_be16s(f
, &s
->ts
);
1728 qemu_put_be16s(f
, &s
->delay
);
1729 qemu_put_sbe16s(f
, &s
->xdata
);
1730 qemu_put_sbe16s(f
, &s
->ydata
);
1733 static int s3c_adc_load(QEMUFile
*f
, void *opaque
, int version_id
)
1735 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*) opaque
;
1737 s
->enable
= qemu_get_be32(f
);
1738 for (i
= 0; i
< 8; i
++)
1739 s
->input
[i
] = qemu_get_be32(f
);
1740 s
->in_idx
= qemu_get_be32(f
);
1741 s
->noise
= qemu_get_be32(f
);
1743 qemu_get_be16s(f
, &s
->control
);
1744 qemu_get_be16s(f
, &s
->ts
);
1745 qemu_get_be16s(f
, &s
->delay
);
1746 qemu_get_sbe16s(f
, &s
->xdata
);
1747 qemu_get_sbe16s(f
, &s
->ydata
);
1749 if (s
->enable
&& (s
->ts
& 7) && !(s
->control
& (1 << 15)))
1755 struct s3c_adc_state_s
*s3c_adc_init(target_phys_addr_t base
, qemu_irq irq
,
1759 struct s3c_adc_state_s
*s
= (struct s3c_adc_state_s
*)
1760 qemu_mallocz(sizeof(struct s3c_adc_state_s
));
1765 s
->convt
= qemu_new_timer(vm_clock
, s3c_adc_done
, s
);
1766 s
->tst
= qemu_new_timer(vm_clock
, s3c_adc_tick
, s
);
1770 iomemtype
= cpu_register_io_memory(0, s3c_adc_readfn
,
1771 s3c_adc_writefn
, s
);
1772 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
1774 /* We want absolute coordinates */
1775 qemu_add_mouse_event_handler(s3c_adc_event
, s
, 1,
1776 "QEMU S3C2410-driven Touchscreen");
1778 register_savevm("s3c24xx_adc", 0, 0, s3c_adc_save
, s3c_adc_load
, s
);
1783 void s3c_adc_setscale(struct s3c_adc_state_s
*adc
, const int m
[])
1785 memcpy(adc
->scale
, m
, 6 * sizeof(int));
1788 /* IIC-bus serial interface */
1789 typedef struct s3c_i2c_state_s
{
1790 SysBusDevice busdev
;
1793 target_phys_addr_t base
;
1805 static void s3c_i2c_irq(struct s3c_i2c_state_s
*s
)
1807 s
->control
|= 1 << 4;
1808 if (s
->control
& (1 << 5))
1809 qemu_irq_raise(s
->irq
);
1812 static void s3c_i2c_reset(struct s3c_i2c_state_s
*s
)
1821 static void s3c_i2c_event(i2c_slave
*i2c
, enum i2c_event event
)
1823 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) i2c
;
1824 if (!(s
->status
& (1 << 4)))
1828 case I2C_START_RECV
:
1829 case I2C_START_SEND
:
1830 s
->status
|= 1 << 2;
1837 s
->status
|= 1 << 0;
1844 static int s3c_i2c_tx(i2c_slave
*i2c
, uint8_t data
)
1846 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) i2c
;
1847 if (!(s
->status
& (1 << 4)))
1850 if ((s
->status
>> 6) == 0)
1851 s
->data
= data
; /* TODO */
1852 s
->status
&= ~(1 << 0);
1855 return !(s
->control
& (1 << 7));
1858 static int s3c_i2c_rx(i2c_slave
*i2c
)
1860 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) i2c
;
1861 if (!(s
->status
& (1 << 4)))
1864 if ((s
->status
>> 6) == 1) {
1865 s
->status
&= ~(1 << 0);
1873 static void s3c_master_work(void *opaque
)
1875 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) opaque
;
1876 int start
= 0, stop
= 0, ack
= 1;
1877 if (s
->control
& (1 << 4)) /* Interrupt pending */
1879 if ((s
->status
& 0x90) != 0x90) /* Master */
1881 stop
= ~s
->status
& (1 << 5);
1882 if (s
->newstart
&& s
->status
& (1 << 5)) { /* START */
1891 ack
= !i2c_start_transfer(s
->bus
, s
->data
>> 1, (~s
->status
>> 6) & 1);
1893 i2c_end_transfer(s
->bus
);
1894 else if (s
->status
& (1 << 6))
1895 ack
= !i2c_send(s
->bus
, s
->data
);
1897 s
->data
= i2c_recv(s
->bus
);
1899 if (!(s
->control
& (1 << 7))) /* ACK */
1903 if (!(s
->status
& (1 << 5))) {
1914 #define S3C_IICCON 0x00 /* IIC-Bus Control register */
1915 #define S3C_IICSTAT 0x04 /* IIC-Bus Control / Status register */
1916 #define S3C_IICADD 0x08 /* IIC-Bus Address register */
1917 #define S3C_IICDS 0x0c /* IIC-Bus Tx / Rx Data Shift register */
1919 #define S3C2440_IICLC 0x10 /* IIC-Bus multi-master line control register */
1921 static uint32_t s3c_i2c_read(void *opaque
, target_phys_addr_t addr
)
1923 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) opaque
;
1929 return s
->status
& ~(1 << 5); /* Busy signal */
1934 case S3C2440_IICLC
: /* s3c2440 only ! */
1937 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1943 static void s3c_i2c_write(void *opaque
, target_phys_addr_t addr
,
1946 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) opaque
;
1950 s
->control
= (s
->control
| 0xef) & value
;
1957 s
->status
|= value
& 0xf0;
1958 if (s
->status
& (1 << 5))
1964 s
->addy
= value
& 0x7f;
1965 i2c_set_slave_address(&s
->slave
, s
->addy
);
1969 s
->data
= value
& 0xff;
1972 case S3C2440_IICLC
: /* s3c2440 only ! */
1973 s
->mmaster
= value
& 0xff;
1977 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
1981 static CPUReadMemoryFunc
*s3c_i2c_readfn
[] = {
1987 static CPUWriteMemoryFunc
*s3c_i2c_writefn
[] = {
1993 static void s3c_i2c_save(QEMUFile
*f
, void *opaque
)
1995 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) opaque
;
1996 qemu_put_8s(f
, &s
->control
);
1997 qemu_put_8s(f
, &s
->status
);
1998 qemu_put_8s(f
, &s
->data
);
1999 qemu_put_8s(f
, &s
->addy
);
2000 qemu_put_8s(f
, &s
->mmaster
);
2002 qemu_put_be32(f
, s
->busy
);
2003 qemu_put_be32(f
, s
->newstart
);
2005 i2c_slave_save(f
, &s
->slave
);
2008 static int s3c_i2c_load(QEMUFile
*f
, void *opaque
, int version_id
)
2010 struct s3c_i2c_state_s
*s
= (struct s3c_i2c_state_s
*) opaque
;
2011 qemu_get_8s(f
, &s
->control
);
2012 qemu_get_8s(f
, &s
->status
);
2013 qemu_get_8s(f
, &s
->data
);
2014 qemu_get_8s(f
, &s
->addy
);
2015 qemu_get_8s(f
, &s
->mmaster
);
2017 s
->busy
= qemu_get_be32(f
);
2018 s
->newstart
= qemu_get_be32(f
);
2020 i2c_slave_load(f
, &s
->slave
);
2024 //struct s3c_i2c_state_s *s3c_i2c_init(target_phys_addr_t base, qemu_irq irq)
2025 void s3c_i2c_init(SysBusDevice
* dev
)
2028 struct s3c_i2c_state_s
*s
= FROM_SYSBUS(struct s3c_i2c_state_s
, dev
);
2030 s
->slave
.event
= s3c_i2c_event
;
2031 s
->slave
.send
= s3c_i2c_tx
;
2032 s
->slave
.recv
= s3c_i2c_rx
;
2034 s
->bus
= i2c_init_bus();
2035 sysbus_init_irq(dev
, &s
->irq
);
2036 qdev_attach_child_bus(&dev
->qdev
, "i2c", s
->bus
);
2040 iomemtype
= cpu_register_io_memory(0, s3c_i2c_readfn
,
2041 s3c_i2c_writefn
, s
);
2042 sysbus_init_mmio(dev
, 0xffffff, iomemtype
);
2044 register_savevm("s3c24xx_i2c", 0, 0, s3c_i2c_save
, s3c_i2c_load
, s
);
2047 i2c_bus
*s3c_i2c_bus(struct s3c_i2c_state_s
*s
)
2052 /* Serial Peripheral Interface */
2053 struct s3c_spi_state_s
{
2054 target_phys_addr_t base
;
2073 uint8_t (*txrx
[2])(void *opaque
, uint8_t value
);
2074 uint8_t (*btxrx
[2])(void *opaque
, uint8_t value
);
2078 static void s3c_spi_update(struct s3c_spi_state_s
*s
)
2081 for (i
= 0; i
< 2; i
++) {
2082 switch ((s
->chan
[i
].control
>> 5) & 3) { /* SMOD */
2084 qemu_irq_raise(s
->chan
[i
].irq
);
2087 qemu_irq_raise(s
->chan
[i
].drq
);
2093 static void s3c_spi_reset(struct s3c_spi_state_s
*s
)
2095 memset(s
->chan
, 0, sizeof(s
->chan
));
2096 s
->chan
[0].pin
= 0x02;
2097 s
->chan
[1].pin
= 0x02;
2101 #define S3C_SPCON0 0x00 /* SPI channel 0 control register */
2102 #define S3C_SPSTA0 0x04 /* SPI channel 0 status register */
2103 #define S3C_SPPIN0 0x08 /* SPI channel 0 pin control register */
2104 #define S3C_SPPRE0 0x0c /* SPI channel 0 baudrate prescaler register */
2105 #define S3C_SPTDAT0 0x10 /* SPI channel 0 Tx data register */
2106 #define S3C_SPRDAT0 0x14 /* SPI channel 0 Rx data register */
2107 #define S3C_SPCON1 0x20 /* SPI channel 1 control register */
2108 #define S3C_SPSTA1 0x24 /* SPI channel 1 status register */
2109 #define S3C_SPPIN1 0x28 /* SPI channel 1 pin control register */
2110 #define S3C_SPPRE1 0x2c /* SPI channel 1 baudrate prescaler register */
2111 #define S3C_SPTDAT1 0x30 /* SPI channel 1 Tx data register */
2112 #define S3C_SPRDAT1 0x34 /* SPI channel 1 Rx data register */
2114 static uint32_t s3c_spi_read(void *opaque
, target_phys_addr_t addr
)
2116 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*) opaque
;
2124 return s
->chan
[ch
].control
;
2132 return s
->chan
[ch
].pin
;
2136 return s
->chan
[ch
].pre
;
2140 return s
->chan
[ch
+ 2].txbuf
;
2144 if (s
->txrx
[ch
] && (s
->chan
[ch
].control
& 0x19) == 0x19)
2145 s
->chan
[ch
].rxbuf
= s
->txrx
[ch
](s
->opaque
[ch
], 'Q');
2147 return s
->chan
[ch
].rxbuf
;
2150 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
2156 static void s3c_spi_write(void *opaque
, target_phys_addr_t addr
,
2159 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*) opaque
;
2167 s
->chan
[ch
].control
= value
& 0x7f;
2173 s
->chan
[ch
].pin
= value
& 0x07;
2178 s
->chan
[ch
].pre
= value
& 0xff;
2183 s
->chan
[ch
].txbuf
= value
& 0xff;
2184 if (s
->txrx
[ch
] && (s
->chan
[ch
].control
& 0x19) == 0x18)
2185 s
->chan
[ch
].rxbuf
= s
->txrx
[ch
](s
->opaque
[ch
], value
& 0xff);
2190 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
2194 static CPUReadMemoryFunc
*s3c_spi_readfn
[] = {
2200 static CPUWriteMemoryFunc
*s3c_spi_writefn
[] = {
2206 static void s3c_spi_save(QEMUFile
*f
, void *opaque
)
2208 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*) opaque
;
2210 for (i
= 0; i
< 2; i
++) {
2211 qemu_put_8s(f
, &s
->chan
[i
].control
);
2212 qemu_put_8s(f
, &s
->chan
[i
].pin
);
2213 qemu_put_8s(f
, &s
->chan
[i
].pre
);
2215 qemu_put_8s(f
, &s
->chan
[i
].txbuf
);
2216 qemu_put_8s(f
, &s
->chan
[i
].rxbuf
);
2217 qemu_put_be32(f
, s
->chan
[i
].cs_pin
);
2218 qemu_put_be32(f
, s
->chan
[i
].clk_pin
);
2219 qemu_put_be32(f
, s
->chan
[i
].mosi_pin
);
2220 qemu_put_be32(f
, s
->chan
[i
].bit
);
2224 static int s3c_spi_load(QEMUFile
*f
, void *opaque
, int version_id
)
2226 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*) opaque
;
2228 for (i
= 0; i
< 2; i
++) {
2229 qemu_get_8s(f
, &s
->chan
[i
].control
);
2230 qemu_get_8s(f
, &s
->chan
[i
].pin
);
2231 qemu_get_8s(f
, &s
->chan
[i
].pre
);
2233 qemu_get_8s(f
, &s
->chan
[i
].txbuf
);
2234 qemu_get_8s(f
, &s
->chan
[i
].rxbuf
);
2235 s
->chan
[i
].cs_pin
= qemu_get_be32(f
);
2236 s
->chan
[i
].clk_pin
= qemu_get_be32(f
);
2237 s
->chan
[i
].mosi_pin
= qemu_get_be32(f
);
2238 s
->chan
[i
].bit
= qemu_get_be32(f
);
2244 static void s3c_spi_bitbang_cs(void *opaque
, int line
, int level
)
2246 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*) opaque
;
2248 if (s
->chan
[ch
].cs_pin
|| level
) {
2249 if (s
->chan
[ch
].bit
&& s
->txrx
[ch
] && !s
->btxrx
[ch
]) {
2250 s
->chan
[ch
].txbuf
<<= 8 - s
->chan
[ch
].bit
;
2251 s
->chan
[ch
].rxbuf
= s
->txrx
[ch
](s
->opaque
[ch
], s
->chan
[ch
].txbuf
);
2253 } else if (!s
->chan
[ch
].cs_pin
|| !level
)
2254 s
->chan
[ch
].bit
= 0;
2256 /* SSn is active low. */
2257 s
->chan
[ch
].cs_pin
= !level
;
2260 static void s3c_spi_bitbang_clk(void *opaque
, int line
, int level
)
2262 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*) opaque
;
2264 if (!s
->chan
[ch
].cs_pin
)
2267 /* Detect CLK rising edge */
2268 if (s
->chan
[ch
].clk_pin
|| !level
)
2272 qemu_set_irq(s
->chan
[ch
].miso
,
2273 s
->btxrx
[ch
](s
->opaque
[ch
], s
->chan
[ch
].mosi_pin
));
2277 s
->chan
[ch
].txbuf
<<= 1;
2278 s
->chan
[ch
].txbuf
|= s
->chan
[ch
].mosi_pin
;
2280 qemu_set_irq(s
->chan
[ch
].miso
, (s
->chan
[ch
].rxbuf
>> 7) & 1);
2281 s
->chan
[ch
].rxbuf
<<= 1;
2283 if (++ s
->chan
[ch
].bit
== 8) {
2285 s
->chan
[ch
].rxbuf
= s
->txrx
[ch
](s
->opaque
[ch
], s
->chan
[ch
].txbuf
);
2286 s
->chan
[ch
].bit
= 0;
2290 s
->chan
[ch
].clk_pin
= level
;
2293 static void s3c_spi_bitbang_mosi(void *opaque
, int line
, int level
)
2295 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*) opaque
;
2297 s
->chan
[ch
].mosi_pin
= level
;
2300 static const struct {
2301 int cs
, clk
, miso
, mosi
;
2302 } s3c_spi_pins
[2] = {
2303 { S3C_GPG(2), S3C_GPE(13), S3C_GPE(11), S3C_GPE(12) },
2304 { S3C_GPG(3), S3C_GPG(7), S3C_GPG(5), S3C_GPG(6) },
2307 static void s3c_spi_bitbang_init(struct s3c_spi_state_s
*s
,
2308 struct s3c_gpio_state_s
*gpio
)
2311 qemu_irq
*cs
= qemu_allocate_irqs(s3c_spi_bitbang_cs
, s
, 2);
2312 qemu_irq
*clk
= qemu_allocate_irqs(s3c_spi_bitbang_clk
, s
, 2);
2313 qemu_irq
*mosi
= qemu_allocate_irqs(s3c_spi_bitbang_mosi
, s
, 2);
2315 for (i
= 0; i
< 2; i
++) {
2316 s3c_gpio_out_set(gpio
, s3c_spi_pins
[i
].cs
, cs
[i
]);
2317 s3c_gpio_out_set(gpio
, s3c_spi_pins
[i
].clk
, clk
[i
]);
2318 s
->chan
[i
].miso
= s3c_gpio_in_get(gpio
)[s3c_spi_pins
[i
].miso
];
2319 s3c_gpio_out_set(gpio
, s3c_spi_pins
[i
].mosi
, mosi
[i
]);
2323 struct s3c_spi_state_s
*s3c_spi_init(target_phys_addr_t base
,
2324 qemu_irq irq0
, qemu_irq drq0
, qemu_irq irq1
, qemu_irq drq1
,
2325 struct s3c_gpio_state_s
*gpio
)
2328 struct s3c_spi_state_s
*s
= (struct s3c_spi_state_s
*)
2329 qemu_mallocz(sizeof(struct s3c_spi_state_s
));
2332 s
->chan
[0].irq
= irq0
;
2333 s
->chan
[0].drq
= drq0
;
2334 s
->chan
[1].irq
= irq1
;
2335 s
->chan
[1].drq
= drq1
;
2339 iomemtype
= cpu_register_io_memory(0, s3c_spi_readfn
,
2340 s3c_spi_writefn
, s
);
2341 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
2343 s3c_spi_bitbang_init(s
, gpio
);
2345 register_savevm("s3c24xx_spi", 0, 0, s3c_spi_save
, s3c_spi_load
, s
);
2350 void s3c_spi_attach(struct s3c_spi_state_s
*s
, int ch
,
2351 uint8_t (*txrx
)(void *opaque
, uint8_t value
),
2352 uint8_t (*btxrx
)(void *opaque
, uint8_t value
), void *opaque
)
2355 cpu_abort(cpu_single_env
, "%s: No channel %i\n", __FUNCTION__
, ch
);
2357 s
->btxrx
[ch
] = btxrx
;
2358 s
->opaque
[ch
] = opaque
;
2361 /* IIS-BUS interface */
2362 static inline void s3c_i2s_update(struct s3c_i2s_state_s
*s
)
2365 (s
->control
& (1 << 0)) && !(s
->control
& (1 << 3)) &&
2366 (s
->mode
& (1 << 7)) && (s
->fcontrol
& (1 << 13));
2368 (s
->control
& (1 << 0)) && !(s
->control
& (1 << 2)) &&
2369 (s
->mode
& (1 << 6)) && (s
->fcontrol
& (1 << 12));
2370 s
->control
&= ~0xc0;
2371 /* The specs are unclear about the FIFO-ready flags logic.
2372 * Implement semantics that make most sense. */
2373 if (s
->tx_en
&& s
->tx_len
)
2374 s
->control
|= (1 << 7);
2375 if (s
->rx_en
&& s
->rx_len
)
2376 s
->control
|= (1 << 6);
2378 qemu_set_irq(s
->dma
[S3C_RQ_I2SSDO
], (s
->control
>> 5) &
2379 (s
->control
>> 7) & (s
->fcontrol
>> 15) & 1);
2380 qemu_set_irq(s
->dma
[S3C_RQ_I2SSDI0
], (s
->control
>> 4) &
2381 (s
->control
>> 6) & (s
->fcontrol
>> 14) & 1);
2382 qemu_set_irq(s
->dma
[S3C_RQ_I2SSDI1
], (s
->control
>> 4) &
2383 (s
->control
>> 6) & (s
->fcontrol
>> 14) & 1);
2386 static void s3c_i2s_reset(struct s3c_i2s_state_s
*s
)
2390 s
->prescaler
= 0x000;
2391 s
->fcontrol
= 0x0000;
2397 #define S3C_IISCON 0x00 /* IIS Control register */
2398 #define S3C_IISMOD 0x04 /* IIS Mode register */
2399 #define S3C_IISPSR 0x08 /* IIS Prescaler register */
2400 #define S3C_IISFCON 0x0c /* IIS FIFO Interface register */
2401 #define S3C_IISFIFO 0x10 /* IIS FIFO register */
2403 static uint32_t s3c_i2s_read(void *opaque
, target_phys_addr_t addr
)
2405 struct s3c_i2s_state_s
*s
= (struct s3c_i2s_state_s
*) opaque
;
2414 return s
->prescaler
;
2416 return s
->fcontrol
|
2417 (MAX(32 - s
->tx_len
, 0) << 6) |
2420 if (s
->rx_len
> 0) {
2425 s
->buffer
= (uint16_t) (ret
= s
->codec_in(s
->opaque
));
2431 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
2437 static void s3c_i2s_write(void *opaque
, target_phys_addr_t addr
,
2440 struct s3c_i2s_state_s
*s
= (struct s3c_i2s_state_s
*) opaque
;
2444 s
->control
= (s
->control
& 0x100) | (value
& 0x03f);
2448 s
->mode
= value
& 0x1ff;
2452 s
->prescaler
= value
& 0x3ff;
2455 s
->fcontrol
= value
& 0xf000;
2459 if (s
->tx_len
&& s
->tx_en
) {
2463 s
->codec_out(s
->opaque
, value
| ((uint32_t) s
->buffer
<< 16));
2465 s
->buffer
= (uint16_t) value
;
2470 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
2474 static CPUReadMemoryFunc
*s3c_i2s_readfn
[] = {
2480 static CPUWriteMemoryFunc
*s3c_i2s_writefn
[] = {
2486 static void s3c_i2s_save(QEMUFile
*f
, void *opaque
)
2488 struct s3c_i2s_state_s
*s
= (struct s3c_i2s_state_s
*) opaque
;
2489 qemu_put_be16s(f
, &s
->control
);
2490 qemu_put_be16s(f
, &s
->mode
);
2491 qemu_put_be16s(f
, &s
->prescaler
);
2492 qemu_put_be16s(f
, &s
->fcontrol
);
2494 qemu_put_be32(f
, s
->tx_en
);
2495 qemu_put_be32(f
, s
->rx_en
);
2496 qemu_put_be32(f
, s
->tx_len
);
2497 qemu_put_be32(f
, s
->rx_len
);
2498 qemu_put_be16(f
, s
->buffer
);
2499 qemu_put_be32(f
, s
->cycle
);
2502 static int s3c_i2s_load(QEMUFile
*f
, void *opaque
, int version_id
)
2504 struct s3c_i2s_state_s
*s
= (struct s3c_i2s_state_s
*) opaque
;
2505 qemu_get_be16s(f
, &s
->control
);
2506 qemu_get_be16s(f
, &s
->mode
);
2507 qemu_get_be16s(f
, &s
->prescaler
);
2508 qemu_get_be16s(f
, &s
->fcontrol
);
2510 s
->tx_en
= qemu_get_be32(f
);
2511 s
->rx_en
= qemu_get_be32(f
);
2512 s
->tx_len
= qemu_get_be32(f
);
2513 s
->rx_len
= qemu_get_be32(f
);
2514 s
->buffer
= qemu_get_be16(f
);
2515 s
->cycle
= qemu_get_be32(f
);
2520 static void s3c_i2s_data_req(void *opaque
, int tx
, int rx
)
2522 struct s3c_i2s_state_s
*s
= (struct s3c_i2s_state_s
*) opaque
;
2528 struct s3c_i2s_state_s
*s3c_i2s_init(target_phys_addr_t base
, qemu_irq
*dma
)
2531 struct s3c_i2s_state_s
*s
= (struct s3c_i2s_state_s
*)
2532 qemu_mallocz(sizeof(struct s3c_i2s_state_s
));
2536 s
->data_req
= s3c_i2s_data_req
;
2540 iomemtype
= cpu_register_io_memory(0, s3c_i2s_readfn
,
2541 s3c_i2s_writefn
, s
);
2542 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
2544 register_savevm("s3c24xx_iis", 0, 0, s3c_i2s_save
, s3c_i2s_load
, s
);
2549 /* Watchdog Timer */
2550 struct s3c_wdt_state_s
{
2551 struct s3c_freq_s
* freq
;
2552 target_phys_addr_t base
;
2561 static void s3c_wdt_start(struct s3c_wdt_state_s
*s
)
2563 int enable
= s
->control
& (1 << 5);
2564 int prescaler
= (s
->control
>> 8) + 1;
2565 int divider
= prescaler
<< (((s
->control
>> 3) & 3) + 4);
2567 s
->timestamp
= qemu_get_clock(vm_clock
);
2568 qemu_mod_timer(s
->tm
, s
->timestamp
+ muldiv64(divider
* s
->count
,
2569 ticks_per_sec
, s
->freq
->pclk
));
2571 qemu_del_timer(s
->tm
);
2574 static void s3c_wdt_stop(struct s3c_wdt_state_s
*s
)
2576 int prescaler
= (s
->control
>> 8) + 1;
2577 int divider
= prescaler
<< (((s
->control
>> 3) & 3) + 4);
2580 diff
= muldiv64(qemu_get_clock(vm_clock
) - s
->timestamp
, s
->freq
->pclk
,
2581 ticks_per_sec
) / divider
;
2582 s
->count
-= MIN(s
->count
, diff
);
2583 s
->timestamp
= qemu_get_clock(vm_clock
);
2586 static void s3c_wdt_reset(struct s3c_wdt_state_s
*s
)
2588 s
->control
= 0x8021;
2594 static void s3c_wdt_timeout(void *opaque
)
2596 struct s3c_wdt_state_s
*s
= (struct s3c_wdt_state_s
*) opaque
;
2597 if (s
->control
& (1 << 0)) {
2598 qemu_system_reset_request();
2601 if (s
->control
& (1 << 2))
2602 qemu_irq_raise(s
->irq
);
2607 #define S3C_WTCON 0x00 /* Watchdog timer control register */
2608 #define S3C_WTDAT 0x04 /* Watchdog timer data register */
2609 #define S3C_WTCNT 0x08 /* Watchdog timer count register */
2611 static uint32_t s3c_wdt_read(void *opaque
, target_phys_addr_t addr
)
2613 struct s3c_wdt_state_s
*s
= (struct s3c_wdt_state_s
*) opaque
;
2624 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
2630 static void s3c_wdt_write(void *opaque
, target_phys_addr_t addr
,
2633 struct s3c_wdt_state_s
*s
= (struct s3c_wdt_state_s
*) opaque
;
2649 printf("%s: Bad register 0x%lx\n", __FUNCTION__
, (unsigned long)addr
);
2653 static CPUReadMemoryFunc
*s3c_wdt_readfn
[] = {
2659 static CPUWriteMemoryFunc
*s3c_wdt_writefn
[] = {
2665 static void s3c_wdt_save(QEMUFile
*f
, void *opaque
)
2667 struct s3c_wdt_state_s
*s
= (struct s3c_wdt_state_s
*) opaque
;
2670 qemu_put_be16s(f
, &s
->control
);
2671 qemu_put_be16s(f
, &s
->data
);
2672 qemu_put_be16s(f
, &s
->count
);
2673 qemu_put_sbe64s(f
, &s
->timestamp
);
2676 static int s3c_wdt_load(QEMUFile
*f
, void *opaque
, int version_id
)
2678 struct s3c_wdt_state_s
*s
= (struct s3c_wdt_state_s
*) opaque
;
2680 qemu_get_be16s(f
, &s
->control
);
2681 qemu_get_be16s(f
, &s
->data
);
2682 qemu_get_be16s(f
, &s
->count
);
2683 qemu_get_sbe64s(f
, &s
->timestamp
);
2689 struct s3c_wdt_state_s
*s3c_wdt_init(struct s3c_freq_s
* freq
, target_phys_addr_t base
, qemu_irq irq
)
2692 struct s3c_wdt_state_s
*s
= (struct s3c_wdt_state_s
*)
2693 qemu_mallocz(sizeof(struct s3c_wdt_state_s
));
2698 s
->tm
= qemu_new_timer(vm_clock
, s3c_wdt_timeout
, s
);
2702 iomemtype
= cpu_register_io_memory(0, s3c_wdt_readfn
,
2703 s3c_wdt_writefn
, s
);
2704 cpu_register_physical_memory(s
->base
, 0xffffff, iomemtype
);
2706 register_savevm("s3c24xx_wdt", 0, 0, s3c_wdt_save
, s3c_wdt_load
, s
);
2713 target_phys_addr_t base
;
2716 } s3c2410_uart
[] = {
2719 { S3C_PICS_RXD0
, S3C_PICS_TXD0
, S3C_PICS_ERR0
},
2724 { S3C_PICS_RXD1
, S3C_PICS_TXD1
, S3C_PICS_ERR1
},
2729 { S3C_PICS_RXD2
, S3C_PICS_TXD2
, S3C_PICS_ERR2
},
2732 { 0, { 0, 0, 0 }, { 0 } }
2735 /* General CPU reset */
2736 static void s3c2410_reset(void *opaque
)
2738 struct s3c_state_s
*s
= (struct s3c_state_s
*) opaque
;
2741 s3c_pic_reset(s
->pic
);
2742 s3c_dma_reset(s
->dma
);
2743 s3c_gpio_reset(s
->io
);
2744 s3c_lcd_reset(s
->lcd
);
2745 s3c_timers_reset(s
->timers
);
2746 s3c_mmci_reset(s
->mmci
);
2747 s3c_adc_reset(s
->adc
);
2748 s3c_i2c_reset(s
->i2c
);
2749 s3c_i2s_reset(s
->i2s
);
2750 s3c_rtc_reset(s
->rtc
);
2751 s3c_spi_reset(s
->spi
);
2752 s3c_udc_reset(s
->udc
);
2753 s3c_wdt_reset(s
->wdt
);
2754 s3c_clkpwr_reset(s
);
2755 s
->nand
->reset(s
->nand
);
2756 for (i
= 0; s3c2410_uart
[i
].base
; i
++)
2757 s3c_uart_reset(s
->uart
[i
]);
2761 struct s3c_state_s
* g_s3c
;
2763 /* Initialise an S3C24XX microprocessor. */
2764 struct s3c_state_s
*s3c24xx_init(
2767 unsigned int sdram_size
,
2768 uint32_t sram_address
,
2771 struct s3c_state_s
*s
;
2773 s
= (struct s3c_state_s
*) qemu_mallocz(sizeof(struct s3c_state_s
));
2778 s
->clock
.xtal
= xtal
;
2779 s
->clock
.pclk
= 66500000; // S3C_PCLK_FREQ; // TEMP
2781 s
->env
= cpu_init("arm920t");
2783 fprintf(stderr
, "Unable to initialize ARM920T\n");
2786 register_savevm("s3c24xx", 0, 0,
2787 cpu_save
, cpu_load
, s
->env
);
2789 cpu_register_physical_memory(S3C_RAM_BASE
, sdram_size
,
2790 qemu_ram_alloc(sdram_size
) | IO_MEM_RAM
);
2792 /* If OM pins are 00, SRAM is mapped at 0x0 instead. */
2793 cpu_register_physical_memory(sram_address
, S3C_SRAM_SIZE
,
2794 qemu_ram_alloc(S3C_SRAM_SIZE
) | IO_MEM_RAM
);
2796 s
->mc_base
= 0x48000000;
2798 iomemtype
= cpu_register_io_memory(0, s3c_mc_readfn
, s3c_mc_writefn
, s
);
2799 cpu_register_physical_memory(s
->mc_base
, 0xffffff, iomemtype
);
2800 register_savevm("s3c24xx_mc", 0, 0, s3c_mc_save
, s3c_mc_load
, s
);
2802 s
->pic
= s3c_pic_init(0x4a000000, arm_pic_init_cpu(s
->env
));
2803 s
->irq
= s3c_pic_get(s
->pic
);
2805 s
->dma
= s3c_dma_init(0x4b000000, &s
->irq
[S3C_PIC_DMA0
]);
2806 s
->drq
= s3c_dma_get(s
->dma
);
2808 s
->clkpwr_base
= 0x4c000000;
2809 s3c_clkpwr_reset(s
);
2811 iomemtype
= cpu_register_io_memory(0, s3c_clkpwr_readfn
,
2812 s3c_clkpwr_writefn
, s
);
2813 cpu_register_physical_memory(s
->clkpwr_base
, 0xffffff, iomemtype
);
2814 register_savevm("s3c24xx_clkpwr", 0, 0,
2815 s3c_clkpwr_save
, s3c_clkpwr_load
, s
);
2817 s
->lcd
= s3c_lcd_init(0x4d000000, s
->irq
[S3C_PIC_LCD
]);
2819 if (s
->cpu_id
== S3C_CPU_2440
)
2820 s
->nand
= s3c2440_nand_init();
2822 s
->nand
= s3c2410_nand_init();
2824 for (i
= 0; s3c2410_uart
[i
].base
; i
++) {
2825 s
->uart
[i
] = s3c_uart_init(&s
->clock
,
2826 s3c2410_uart
[i
].base
,
2827 &s
->irq
[s3c2410_uart
[i
].irq
[0]],
2828 &s
->drq
[s3c2410_uart
[i
].dma
[0]]);
2830 s3c_uart_attach(s
->uart
[i
], serial_hds
[i
]);
2833 s
->timers
= s3c_timers_init(&s
->clock
, 0x51000000, &s
->irq
[S3C_PIC_TIMER0
], s
->drq
);
2835 s
->udc
= s3c_udc_init(0x52000000, s
->irq
[S3C_PIC_USBD
], s
->drq
);
2837 s
->wdt
= s3c_wdt_init(&s
->clock
, 0x53000000, s
->irq
[S3C_PIC_WDT
]);
2842 dev
= sysbus_create_simple("s3c-i2c", 0x54000000, s
->irq
[S3C_PIC_IIC
]);
2843 s
->i2c
= FROM_SYSBUS(s3c_i2c_state_s
, dev
);
2845 /* s->i2c = s3c_i2c_init(0x54000000, s->irq[S3C_PIC_IIC]); */
2847 s
->i2s
= s3c_i2s_init(0x55000000, s
->drq
);
2849 s
->io
= s3c_gpio_init(0x56000000, s
->irq
, s
->cpu_id
);
2851 s
->rtc
= s3c_rtc_init(0x57000000, s
->irq
[S3C_PIC_RTC
]);
2853 s
->adc
= s3c_adc_init(0x58000000, s
->irq
[S3C_PICS_ADC
],
2854 s
->irq
[S3C_PICS_TC
]);
2856 s
->spi
= s3c_spi_init(0x59000000,
2857 s
->irq
[S3C_PIC_SPI0
], s
->drq
[S3C_RQ_SPI0
],
2858 s
->irq
[S3C_PIC_SPI1
], s
->drq
[S3C_RQ_SPI1
], s
->io
);
2860 s
->mmci
= s3c_mmci_init(0x5a000000, s
->cpu_id
, mmc
,
2861 s
->irq
[S3C_PIC_SDI
], s
->drq
);
2864 usb_ohci_init_pxa(0x49000000, 3, -1, s
->irq
[S3C_PIC_USBH
]);
2867 qemu_register_reset(s3c2410_reset
, s
);
2869 s
->nand
->setwp(s
->nand
, 1);
2871 /* Power on reset */
2872 s3c_gpio_setpwrstat(s
->io
, 1);
2877 static void s3c_register_devices(void)
2879 sysbus_register_dev("s3c-i2c", sizeof(s3c_i2c_state_s
), s3c_i2c_init
);
2882 device_init(s3c_register_devices
);