MINI2440: General update
[qemu/mini2440.git] / hw / s3c2410.c
blobd1e4c92c3f34218ba24696133a4a30aa130528f5
1 /*
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.
9 */
11 #include "s3c.h"
12 #include "qemu-timer.h"
13 #include "qemu-char.h"
14 #include "console.h"
15 #include "devices.h"
16 #include "arm-misc.h"
17 #include "i2c.h"
18 #include "pxa.h"
19 #include "sysemu.h"
21 /* Interrupt controller */
22 struct s3c_pic_state_s {
23 target_phys_addr_t base;
24 qemu_irq *parent_pic;
25 qemu_irq *irqs;
27 uint32_t srcpnd;
28 uint32_t intpnd;
29 uint32_t intmsk;
30 uint32_t intmod;
31 uint32_t priority;
32 int intoffset;
33 uint32_t subsrcpnd;
34 uint32_t intsubmsk;
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.
54 #ifdef DEBUG
55 static const uint32_t s3c_arbmsk[6] = {
56 0x0000000f,
57 0x000003f0,
58 0x0000fc00,
59 0x003f0000,
60 0x0fc00000,
61 0xf0000000,
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;
73 int offset, i, arb;
74 if (s->intpnd || !pnd) {
75 s3c_pic_update(s);
76 return;
79 if (pnd & s3c_arbmsk[0]) {
80 offset = 0;
81 arb = 0;
82 } else if (pnd & 0x0ffffff0) {
83 i = S3C_ARB_SEL(6);
84 i ^= i << 1;
85 if (!(pnd & s3c_arbmsk[1 + (i & 3)]))
86 if (!(pnd & s3c_arbmsk[1 + (++ i & 3)]))
87 if (!(pnd & s3c_arbmsk[1 + (++ i & 3)]))
88 i ++;
90 if (S3C_ARB_MODE(6))
91 S3C_ARB_SEL_SET(6, ((i + 1) & 3));
92 offset = (i & 3) * 6 + 4;
93 if (pnd & (1 << offset))
94 goto known_offset;
95 else if (!(pnd & (0x1f << offset))) {
96 offset += 5;
97 goto known_offset;
99 offset ++;
100 arb = (i & 3) + 1;
101 } else {
102 arb = 5;
103 offset = 28;
106 pnd >>= offset;
107 i = S3C_ARB_SEL(arb);
108 i ^= i << 1;
109 if (!(pnd & (1 << (i & 3))))
110 if (!(pnd & (1 << (++ i & 3))))
111 if (!(pnd & (1 << (++ i & 3))))
112 i ++;
114 if (S3C_ARB_MODE(arb))
115 S3C_ARB_SEL_SET(arb, ((i + 1) & 3));
116 offset += i & 3;
117 known_offset:
118 s->intoffset = offset;
119 s->intpnd = 1 << offset;
120 s3c_pic_update(s);
122 #else
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);
128 s3c_pic_update(s);
130 #endif
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)
148 int next;
149 const int *sub = &s3c_sub_src_map[-1];
150 uint32_t pnd = s->subsrcpnd & ~s->intsubmsk;
151 while ((next = ffs(pnd))) {
152 sub += next;
153 pnd >>= next;
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;
162 uint32_t mask;
163 /* This interrupt controller doesn't clear any request signals
164 * or register bits automatically. */
165 if (!req)
166 return;
168 if (irq & 32) {
169 irq &= 31;
170 s->subsrcpnd |= 1 << irq;
171 if (s->intsubmsk & (1 << irq))
172 return;
173 else
174 irq = s3c_sub_src_map[irq];
176 s->srcpnd |= (mask = 1 << irq);
178 /* A FIQ */
179 if (s->intmod & mask)
180 qemu_irq_raise(s->parent_pic[ARM_PIC_CPU_FIQ]);
181 else if (!s->intpnd && !(s->intmsk & mask)) {
182 #ifdef DEBUG
183 s3c_pic_arbitrate(s);
184 #else
185 s->intpnd = mask;
186 s->intoffset = irq;
187 qemu_irq_raise(s->parent_pic[ARM_PIC_CPU_IRQ]);
188 #endif
192 static void s3c_pic_reset(struct s3c_pic_state_s *s)
194 s->srcpnd = 0;
195 s->intpnd = 0;
196 s->intmsk = 0xffffffff;
197 s->intmod = 0;
198 s->priority = 0x7f;
199 s->intoffset = 0;
200 s->subsrcpnd = 0;
201 s->intsubmsk = 0x7ff;
202 s3c_pic_update(s);
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;
218 switch (addr) {
219 case S3C_SRCPND:
220 return s->srcpnd;
221 case S3C_INTPND:
222 return s->intpnd;
223 case S3C_INTMSK:
224 return s->intmsk;
225 case S3C_INTMOD:
226 return s->intmod;
227 case S3C_PRIORITY:
228 return s->priority;
229 case S3C_INTOFFSET:
230 return s->intoffset;
231 case S3C_SUBSRCPND:
232 return s->subsrcpnd;
233 case S3C_INTSUBMSK:
234 return s->intsubmsk;
235 default:
236 printf("%s: Bad register 0x%lx\n", __FUNCTION__, (unsigned long)addr);
237 break;
239 return 0;
242 static void s3c_pic_write(void *opaque, target_phys_addr_t addr,
243 uint32_t value)
245 struct s3c_pic_state_s *s = (struct s3c_pic_state_s *) opaque;
247 switch (addr) {
248 case S3C_SRCPND:
249 s->srcpnd &= ~value;
250 if (value & s->intmod)
251 s3c_pic_update(s);
252 break;
253 case S3C_INTPND:
254 if (s->intpnd & value) {
255 s->intpnd = 0;
256 s->intoffset = 0;
257 s3c_pic_arbitrate(s);
259 break;
260 case S3C_INTMSK:
261 s->intmsk = value;
262 if (s->intpnd & value) {
263 s->intpnd = 0;
264 s->intoffset = 0;
266 s3c_pic_arbitrate(s);
267 break;
268 case S3C_INTMOD:
269 s->intmod = value;
270 break;
271 case S3C_PRIORITY:
272 s->priority = value;
273 break;
274 case S3C_SUBSRCPND:
275 s->subsrcpnd &= ~value;
276 break;
277 case S3C_INTSUBMSK:
278 s->intsubmsk = value;
279 s3c_pic_subupdate(s);
280 break;
281 default:
282 printf("%s: Bad register 0x%lx\n", __FUNCTION__, (unsigned long)addr);
286 static CPUReadMemoryFunc *s3c_pic_readfn[] = {
287 s3c_pic_read,
288 s3c_pic_read,
289 s3c_pic_read,
292 static CPUWriteMemoryFunc *s3c_pic_writefn[] = {
293 s3c_pic_write,
294 s3c_pic_write,
295 s3c_pic_write,
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);
322 s3c_pic_update(s);
323 return 0;
326 struct s3c_pic_state_s *s3c_pic_init(target_phys_addr_t base,
327 qemu_irq *arm_pic)
329 int iomemtype;
330 struct s3c_pic_state_s *s = (struct s3c_pic_state_s *)
331 qemu_mallocz(sizeof(struct s3c_pic_state_s));
333 s->base = base;
334 s->parent_pic = arm_pic;
335 s->irqs = qemu_allocate_irqs(s3c_pic_set_irq, s, S3C_PIC_MAX);
337 s3c_pic_reset(s);
339 iomemtype = cpu_register_io_memory(0, s3c_pic_readfn,
340 s3c_pic_writefn, s);
341 cpu_register_physical_memory(s->base, 0xffffff, iomemtype);
343 register_savevm("s3c24xx_pic", 0, 0, s3c_pic_save, s3c_pic_load, s);
345 return s;
348 qemu_irq *s3c_pic_get(struct s3c_pic_state_s *s)
350 return s->irqs;
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;
389 switch (addr >> 2) {
390 case S3C_BWSCON ... S3C_MRSRB7:
391 return s->mc_regs[addr >> 2];
392 default:
393 printf("%s: Bad register 0x%lx\n", __FUNCTION__, (unsigned long)addr);
394 break;
396 return 0;
399 static void s3c_mc_write(void *opaque, target_phys_addr_t addr,
400 uint32_t value)
402 struct s3c_state_s *s = (struct s3c_state_s *) opaque;
404 switch (addr >> 2) {
405 case S3C_BWSCON ... S3C_MRSRB7:
406 s->mc_regs[addr >> 2] = value;
407 break;
408 default:
409 printf("%s: Bad register 0x%lx\n", __FUNCTION__, (unsigned long)addr);
413 static CPUReadMemoryFunc *s3c_mc_readfn[] = {
414 s3c_mc_read,
415 s3c_mc_read,
416 s3c_mc_read,
419 static CPUWriteMemoryFunc *s3c_mc_writefn[] = {
420 s3c_mc_write,
421 s3c_mc_write,
422 s3c_mc_write,
425 static void s3c_mc_save(QEMUFile *f, void *opaque)
427 struct s3c_state_s *s = (struct s3c_state_s *) opaque;
428 int i;
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;
436 int i;
437 for (i = 0; i < 13; i ++)
438 qemu_get_be32s(f, &s->mc_regs[i]);
439 return 0;
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] };
455 uint32_t clk[2];
456 int i;
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 ) {
469 case 0:
470 s->clock.hclk = s->clock.clk;
471 break;
472 case 1:
473 s->clock.hclk = s->clock.clk/2;
474 break;
475 case 2:
476 s->clock.hclk = s->clock.clk/4;
477 break;
478 case 3:
479 s->clock.hclk = s->clock.clk/3;
480 break;
482 switch ( ratio&0x1) {
483 case 0:
484 s->clock.pclk = s->clock.hclk;
485 break;
486 case 1:
487 s->clock.pclk = s->clock.hclk/2;
488 break;
490 s->clock.uclk = clk[1] / 2;
491 #define MHZ 1000000
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,
494 s->clock.uclk/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;
513 switch (addr) {
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];
519 default:
520 printf("%s: Bad register 0x%lx\n", __FUNCTION__, (unsigned long)addr);
521 break;
523 return 0;
526 static void s3c_clkpwr_write(void *opaque, target_phys_addr_t addr,
527 uint32_t value)
529 struct s3c_state_s *s = (struct s3c_state_s *) opaque;
531 switch (addr) {
532 case S3C_LOCKTIME:
533 case S3C_MPLLCON:
534 case S3C_UPLLCON:
535 case S3C_CLKDIVN:
536 s->clkpwr_regs[addr >> 2] = value;
537 if (addr != S3C_LOCKTIME)
538 s3c_clkpwr_update(s);
539 break;
540 case S3C_CLKCON:
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);
545 #if 0
546 cpu_reset(s->env);
547 s->env->regs[15] = 0; /* XXX */
548 #endif
549 } else
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;
556 break;
557 case S3C_CLKSLOW:
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;
562 break;
563 case S3C2440_CAMDIVN:
564 if (s->cpu_id == S3C_CPU_2440) {
565 s->clkpwr_regs[addr >> 2] = value;
566 break;
568 default:
569 printf("%s: Bad register 0x%x (cpu %08x)\n", __FUNCTION__, /*(unsigned long)*/addr, s->cpu_id);
573 static CPUReadMemoryFunc *s3c_clkpwr_readfn[] = {
574 s3c_clkpwr_read,
575 s3c_clkpwr_read,
576 s3c_clkpwr_read,
579 static CPUWriteMemoryFunc *s3c_clkpwr_writefn[] = {
580 s3c_clkpwr_write,
581 s3c_clkpwr_write,
582 s3c_clkpwr_write,
585 static void s3c_clkpwr_save(QEMUFile *f, void *opaque)
587 struct s3c_state_s *s = (struct s3c_state_s *) opaque;
588 int i;
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;
596 int i;
597 for (i = 0; i < 7; i ++)
598 qemu_get_be32s(f, &s->clkpwr_regs[i]);
599 return 0;
602 /* DMA controller */
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;
608 qemu_irq *drqs;
609 struct s3c_dma_ch_state_s {
610 qemu_irq intr;
611 int curr_tc;
612 int req;
613 int running;
614 uint32_t con;
615 uint32_t isrc;
616 uint32_t isrcc;
617 uint32_t idst;
618 uint32_t idstc;
619 uint32_t csrc;
620 uint32_t cdst;
621 uint32_t mask;
622 } ch[S3C_DMA_CH_N];
625 static inline void s3c_dma_ch_run(struct s3c_dma_state_s *s,
626 struct s3c_dma_ch_state_s *ch)
628 int width, burst, t;
629 uint8_t buffer[4];
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__);
637 return;
639 ch->running = 1;
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 */
646 ch->csrc += width;
647 if (!(ch->idstc & 1)) /* INT */
648 ch->cdst += width;
651 if (!(ch->con & (1 << 27)) && !ch->req) /* SERVMODE */
652 break;
654 ch->running = 0;
656 if (!(ch->con & (1 << 23))) { /* SWHW_SEL */
657 ch->req = 0;
660 if (ch->curr_tc <= 0) {
661 if (ch->con & (1 << 22)) /* RELOAD */
662 ch->mask &= ~(1 << 1); /* ON_OFF */
663 else {
664 if (!(ch->con & (1 << 23))) { /* SWHW_SEL */
665 printf("%s: auto-reload software controlled transfer\n",
666 __FUNCTION__);
667 break;
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 */
676 ch->req = 0;
678 if (ch->con & (1 << 29)) { /* INT */
679 qemu_irq_raise(ch->intr);
680 /* Give the system a chance to respond. */
681 break;
687 static void s3c_dma_reset(struct s3c_dma_state_s *s)
689 int i;
690 for (i = 0; i < S3C_DMA_CH_N; i ++) {
691 s->ch[i].curr_tc = 0;
692 s->ch[i].csrc = 0;
693 s->ch[i].isrc = 0;
694 s->ch[i].isrcc = 0;
695 s->ch[i].cdst = 0;
696 s->ch[i].idst = 0;
697 s->ch[i].idstc = 0;
698 s->ch[i].con = 0;
699 s->ch[i].csrc = 0;
700 s->ch[i].cdst = 0;
701 s->ch[i].mask = 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 */
712 ch->req = req;
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];
734 addr &= 0x3f;
737 switch (addr) {
738 case S3C_DISRC:
739 return ch->isrc;
740 case S3C_DISRCC:
741 return ch->isrcc;
742 case S3C_DIDST:
743 return ch->idst;
744 case S3C_DIDSTC:
745 return ch->idstc;
746 case S3C_DCON:
747 return ch->con;
748 case S3C_DSTAT:
749 return ch->curr_tc;
750 case S3C_DCSRC:
751 return ch->csrc;
752 case S3C_DCDST:
753 return ch->cdst;
754 case S3C_DMASKTRIG:
755 return ch->mask;
756 default:
757 printf("%s: Bad register 0x%lx\n", __FUNCTION__, (unsigned long)addr);
758 break;
760 return 0;
763 static void s3c_dma_write(void *opaque, target_phys_addr_t addr,
764 uint32_t value)
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];
771 addr &= 0x3f;
774 switch (addr) {
775 case S3C_DCON:
776 ch->con = value;
777 break;
778 case S3C_DISRC:
779 ch->isrc = value;
780 break;
781 case S3C_DISRCC:
782 ch->isrcc = value;
783 break;
784 case S3C_DIDST:
785 ch->idst = value;
786 break;
787 case S3C_DIDSTC:
788 ch->idstc = value;
789 break;
790 case S3C_DMASKTRIG:
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 */
797 ch->mask = value;
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);
804 break;
805 default:
806 printf("%s: Bad register 0x%lx\n", __FUNCTION__, (unsigned long)addr);
810 static CPUReadMemoryFunc *s3c_dma_readfn[] = {
811 s3c_dma_read,
812 s3c_dma_read,
813 s3c_dma_read,
816 static CPUWriteMemoryFunc *s3c_dma_writefn[] = {
817 s3c_dma_write,
818 s3c_dma_write,
819 s3c_dma_write,
822 static void s3c_dma_save(QEMUFile *f, void *opaque)
824 struct s3c_dma_state_s *s = (struct s3c_dma_state_s *) opaque;
825 int i;
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;
843 int i;
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);
856 return 0;
859 struct s3c_dma_state_s *s3c_dma_init(target_phys_addr_t base, qemu_irq *pic)
861 int iomemtype;
862 struct s3c_dma_state_s *s = (struct s3c_dma_state_s *)
863 qemu_mallocz(sizeof(struct s3c_dma_state_s));
865 s->base = base;
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);
872 s3c_dma_reset(s);
874 iomemtype = cpu_register_io_memory(0, s3c_dma_readfn,
875 s3c_dma_writefn, s);
876 cpu_register_physical_memory(s->base, 0xffffff, iomemtype);
878 register_savevm("s3c24xx_dma", 0, 0, s3c_dma_save, s3c_dma_load, s);
880 return s;
883 qemu_irq *s3c_dma_get(struct s3c_dma_state_s *s)
885 return s->drqs;
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;
893 qemu_irq *dma;
894 DisplayState *ds;
895 struct s3c_timer_state_s {
896 QEMUTimer *t;
897 struct s3c_timers_state_s *s;
898 int n;
899 int running;
900 uint32_t divider;
901 uint16_t count;
902 int64_t reload;
903 qemu_irq irq;
904 gpio_handler_t cmp_cb;
905 void *cmp_opaque;
906 } timer[5];
908 uint16_t compareb[4];
909 uint16_t countb[5];
910 uint32_t config[2];
911 uint32_t control;
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)
918 uint16_t elapsed;
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)
939 return;
941 s->timer[tm].divider = s->freq->pclk >>
942 (((s->config[1] >> (tm * 4)) & 3) + 1);
943 if (tm < 2)
944 s->timer[tm].divider /= ((s->config[0] >> 0) & 0xff) + 1;
945 else
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)
956 int i;
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;
975 if (!t->running)
976 return;
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]);
982 } else
983 qemu_irq_raise(t->irq);
985 t->running = 0;
986 t->count = 0;
988 if (s->control & (1 << ((t->n == 4) ? 22 : (s3c_tm_bits[t->n] + 3)))) {
989 /* Auto-reload */
990 t->count = s->countb[t->n];
991 s3c_timers_start(s, t->n);
992 } else
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;
1017 int tm = 0;
1019 switch (addr) {
1020 case S3C_TCFG0:
1021 return s->config[0];
1022 case S3C_TCFG1:
1023 return s->config[1];
1024 case S3C_TCON:
1025 return s->control;
1026 case S3C_TCMPB3: tm ++;
1027 case S3C_TCMPB2: tm ++;
1028 case S3C_TCMPB1: tm ++;
1029 case S3C_TCMPB0:
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 ++;
1035 case S3C_TCNTB0:
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 ++;
1041 case S3C_TCNTO0:
1042 return s3c_timers_get(s, tm);
1043 default:
1044 printf("%s: Bad register 0x%lx\n", __FUNCTION__, (unsigned long)addr);
1045 break;
1047 return 0;
1050 static void s3c_timers_write(void *opaque, target_phys_addr_t addr,
1051 uint32_t value)
1053 struct s3c_timers_state_s *s = (struct s3c_timers_state_s *) opaque;
1054 int tm = 0;
1056 switch (addr) {
1057 case S3C_TCFG0:
1058 s->config[0] = value & 0x00ffffff;
1059 break;
1060 case S3C_TCFG1:
1061 s->config[1] = value & 0x00ffffff;
1062 break;
1063 case S3C_TCON:
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);
1070 } else
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);
1076 else
1077 s3c_timers_start(s, tm);
1081 s->control = value & 0x007fff1f;
1082 break;
1083 case S3C_TCMPB3: tm ++;
1084 case S3C_TCMPB2: tm ++;
1085 case S3C_TCMPB1: tm ++;
1086 case S3C_TCMPB0:
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);
1090 break;
1091 case S3C_TCNTB4: tm ++;
1092 case S3C_TCNTB3: tm ++;
1093 case S3C_TCNTB2: tm ++;
1094 case S3C_TCNTB1: tm ++;
1095 case S3C_TCNTB0:
1096 s->countb[tm] = value & 0xffff;
1097 break;
1098 default:
1099 printf("%s: Bad register 0x%lx\n", __FUNCTION__, (unsigned long)addr);
1103 static CPUReadMemoryFunc *s3c_timers_readfn[] = {
1104 s3c_timers_read,
1105 s3c_timers_read,
1106 s3c_timers_read,
1109 static CPUWriteMemoryFunc *s3c_timers_writefn[] = {
1110 s3c_timers_write,
1111 s3c_timers_write,
1112 s3c_timers_write,
1115 static void s3c_timers_save(QEMUFile *f, void *opaque)
1117 struct s3c_timers_state_s *s = (struct s3c_timers_state_s *) opaque;
1118 int i;
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;
1138 int i, running[5];
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 ++)
1156 if (running[i])
1157 s3c_timers_start(s, i);
1159 return 0;
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)
1165 int i, iomemtype;
1166 struct s3c_timers_state_s *s = (struct s3c_timers_state_s *)
1167 qemu_mallocz(sizeof(struct s3c_timers_state_s));
1169 s->freq = freq;
1170 s->base = base;
1171 s->dma = dma;
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]);
1178 s->timer[i].s = s;
1179 s->timer[i].n = 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);
1191 return 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);
1200 exit(-1);
1202 s->timer[line].cmp_cb = handler;
1203 s->timer[line].cmp_opaque = cmp_opaque;
1206 /* UART */
1207 struct s3c_uart_state_s {
1208 struct s3c_freq_s * freq;
1209 target_phys_addr_t base;
1210 qemu_irq *irq;
1211 qemu_irq *dma;
1212 uint8_t data;
1213 uint8_t rxfifo[16];
1214 int rxstart;
1215 int rxlen;
1216 #define UART_MAX_CHR 4
1217 int chr_num;
1218 CharDriverState *chr[UART_MAX_CHR];
1220 uint8_t lcontrol;
1221 uint8_t fcontrol;
1222 uint8_t mcontrol;
1223 uint16_t control;
1224 uint16_t brdiv;
1225 uint8_t errstat;
1228 static void s3c_uart_reset(struct s3c_uart_state_s *s)
1230 s->lcontrol = 0x00;
1231 s->fcontrol = 0x00;
1232 s->mcontrol = 0x00;
1233 s->control = 0x0000;
1234 s->errstat = 0;
1236 s->rxstart = 0;
1237 s->rxlen = 0;
1240 static void s3c_uart_err(struct s3c_uart_state_s *s, int err)
1242 s->errstat |= 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 */
1252 !s->rxlen)
1253 return;
1254 if (!(s->control & (1 << 7))) /* RxTimeOutEnable */
1255 return;
1256 /* When the Rx FIFO trigger level is not reached, the interrupt
1257 * is generated anyway, just after a small timeout instead of
1258 * immediately. */
1261 switch ((s->control >> 0) & 3) { /* ReceiveMode */
1262 case 1:
1263 if ((s->control & (1 << 8)) || pulse) /* RxInterruptType */
1264 qemu_irq_raise(s->irq[0]);
1265 break;
1266 case 2:
1267 case 3:
1268 qemu_irq_raise(s->dma[0]);
1269 break;
1273 inline static void s3c_uart_empty(struct s3c_uart_state_s *s, int pulse)
1275 switch ((s->control >> 2) & 3) { /* TransmitMode */
1276 case 1:
1277 if ((s->control & (1 << 9)) || pulse) /* TxInterruptType */
1278 qemu_irq_raise(s->irq[1]);
1279 break;
1280 case 2:
1281 case 3:
1282 qemu_irq_raise(s->dma[0]);
1283 break;
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;
1296 int i;
1297 if (!s->chr)
1298 return;
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) {
1304 case 4:
1305 case 6:
1306 ssp.parity = 'O';
1307 break;
1308 case 5:
1309 case 7:
1310 ssp.parity = 'E';
1311 break;
1312 default:
1313 ssp.parity = 'N';
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;
1329 else
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;
1336 int left;
1337 if (s->fcontrol & 1) { /* FIFOEnable */
1338 if (s->rxlen + size > 16) {
1339 size = 16 - s->rxlen;
1340 s3c_uart_err(s, 1);
1343 left = 16 - ((s->rxstart + s->rxlen) & 15);
1344 if (size > left) {
1345 memcpy(s->rxfifo + ((s->rxstart + s->rxlen) & 15), buf, left);
1346 memcpy(s->rxfifo, buf + left, size - left);
1347 } else
1348 memcpy(s->rxfifo + ((s->rxstart + s->rxlen) & 15), buf, size);
1349 s->rxlen += size;
1350 } else {
1351 if (s->rxlen + size > 1)
1352 s3c_uart_err(s, 1);
1353 s->rxlen = 1;
1354 s->data = buf[0];
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;
1379 uint8_t ret;
1381 switch (addr) {
1382 case S3C_ULCON:
1383 return s->lcontrol;
1384 case S3C_UCON:
1385 return s->control;
1386 case S3C_UFCON:
1387 return s->fcontrol;
1388 case S3C_UMCON:
1389 return s->mcontrol;
1390 case S3C_UTRSTAT:
1391 return 6 | !!s->rxlen;
1392 case S3C_UERSTAT:
1393 /* XXX: UERSTAT[3] is Reserved but Linux thinks it is BREAK */
1394 ret = s->errstat;
1395 s->errstat = 0;
1396 s3c_uart_update(s);
1397 return ret;
1398 case S3C_UFSTAT:
1399 s3c_uart_update(s);
1400 return s->rxlen ? s->rxlen | (1 << 8) : 0;
1401 case S3C_UMSTAT:
1402 s3c_uart_update(s);
1403 return 0x11;
1404 case S3C_UTXH: /* why this is called by u-boot is not clear */
1405 return 0;
1406 case S3C_URXH:
1407 s3c_uart_update(s);
1408 if (s->rxlen) {
1409 s->rxlen --;
1410 if (s->fcontrol & 1) { /* FIFOEnable */
1411 ret = s->rxfifo[s->rxstart ++];
1412 s->rxstart &= 15;
1413 } else
1414 ret = s->data;
1415 return ret;
1417 return 0;
1418 case S3C_UBRDIV:
1419 return s->brdiv;
1420 default:
1421 printf("%s: Bad register 0x%lx\n", __FUNCTION__, (unsigned long)addr);
1422 break;
1424 return 0;
1427 static void s3c_uart_write(void *opaque, target_phys_addr_t addr,
1428 uint32_t value)
1430 struct s3c_uart_state_s *s = (struct s3c_uart_state_s *) opaque;
1431 uint8_t ch;
1432 int i;
1434 switch (addr) {
1435 case S3C_ULCON:
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);
1441 s3c_uart_update(s);
1442 break;
1443 case S3C_UCON:
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;
1449 s3c_uart_update(s);
1450 break;
1451 case S3C_UFCON:
1452 if (value & (1 << 1)) /* RxReset */
1453 s->rxlen = 0;
1454 s->fcontrol = value & 0xf1;
1455 s3c_uart_update(s);
1456 break;
1457 case S3C_UMCON:
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);
1464 #endif
1465 s->mcontrol = value & 0x11;
1466 s3c_uart_update(s);
1467 break;
1468 case S3C_UTXH:
1469 ch = value & 0xff;
1470 for (i = 0; i < s->chr_num; i ++)
1471 qemu_chr_write(s->chr[i], &ch, 1);
1472 s3c_uart_empty(s, 1);
1473 s3c_uart_update(s);
1474 break;
1475 case S3C_UBRDIV:
1476 s->brdiv = value & 0xffff;
1477 s3c_uart_params_update(s);
1478 s3c_uart_update(s);
1479 break;
1480 default:
1481 printf("%s: Bad register 0x%lx\n", __FUNCTION__, (unsigned long)addr);
1485 static CPUReadMemoryFunc *s3c_uart_readfn[] = {
1486 s3c_uart_read,
1487 s3c_uart_read,
1488 s3c_uart_read,
1491 static CPUWriteMemoryFunc *s3c_uart_writefn[] = {
1492 s3c_uart_write,
1493 s3c_uart_write,
1494 s3c_uart_write,
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);
1526 return 0;
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)
1532 int iomemtype;
1533 struct s3c_uart_state_s *s = (struct s3c_uart_state_s *)
1534 qemu_mallocz(sizeof(struct s3c_uart_state_s));
1536 s->freq = freq;
1537 s->base = base;
1538 s->irq = irqs;
1539 s->dma = dma;
1541 s3c_uart_reset(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);
1549 return 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;
1565 qemu_irq irq;
1566 qemu_irq tcirq;
1567 QEMUTimer *convt;
1568 QEMUTimer *tst;
1569 int x;
1570 int y;
1571 int down;
1572 int enable;
1573 int input[8];
1574 int in_idx;
1575 int noise;
1576 int scale[6];
1578 uint16_t control;
1579 uint16_t ts;
1580 uint16_t delay;
1581 int16_t xdata;
1582 int16_t ydata;
1585 static void s3c_adc_reset(struct s3c_adc_state_s *s)
1587 s->down = 0;
1588 s->control = 0x3fc4;
1589 s->ts = 0x58;
1590 s->delay = 0xff;
1591 s->enable = 1;
1594 static void s3c_adc_start(struct s3c_adc_state_s *s)
1596 if (!s->enable || (s->ts & 7) == 0)
1597 return;
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;
1614 int sx, sy;
1616 if (s->down) {
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);
1627 s->noise ++;
1628 s->noise &= 7;
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;
1640 s->x = x;
1641 s->y = y;
1642 s3c_adc_tick(s);
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;
1655 switch (addr) {
1656 case S3C_ADCCON:
1657 return s->control;
1658 case S3C_ADCTSC:
1659 return s->ts;
1660 case S3C_ADCDLY:
1661 return s->delay;
1662 case S3C_ADCDAT0:
1663 if (s->control & 2)
1664 s3c_adc_start(s);
1665 return ((!s->down) << 15) | s->xdata;
1666 case S3C_ADCDAT1:
1667 return ((!s->down) << 15) | s->ydata;
1668 default:
1669 printf("%s: Bad register 0x%lx\n", __FUNCTION__, (unsigned long)addr);
1670 break;
1672 return 0;
1675 static void s3c_adc_write(void *opaque, target_phys_addr_t addr,
1676 uint32_t value)
1678 struct s3c_adc_state_s *s = (struct s3c_adc_state_s *) opaque;
1680 switch (addr) {
1681 case S3C_ADCCON:
1682 s->control = (s->control & 0x8000) | (value & 0x7ffe);
1683 s->enable = !(value & 4);
1684 if ((value & 1) && !(value & 2))
1685 s3c_adc_start(s);
1686 if (!s->enable)
1687 qemu_del_timer(s->convt);
1688 s3c_adc_tick(s);
1689 break;
1691 case S3C_ADCTSC:
1692 s->ts = value & 0xff;
1693 break;
1695 case S3C_ADCDLY:
1696 s->delay = value & 0xffff;
1697 break;
1699 default:
1700 printf("%s: Bad register 0x%lx\n", __FUNCTION__, (unsigned long)addr);
1704 static CPUReadMemoryFunc *s3c_adc_readfn[] = {
1705 s3c_adc_read,
1706 s3c_adc_read,
1707 s3c_adc_read,
1710 static CPUWriteMemoryFunc *s3c_adc_writefn[] = {
1711 s3c_adc_write,
1712 s3c_adc_write,
1713 s3c_adc_write,
1716 static void s3c_adc_save(QEMUFile *f, void *opaque)
1718 struct s3c_adc_state_s *s = (struct s3c_adc_state_s *) opaque;
1719 int i;
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;
1736 int i;
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)))
1750 s3c_adc_start(s);
1752 return 0;
1755 struct s3c_adc_state_s *s3c_adc_init(target_phys_addr_t base, qemu_irq irq,
1756 qemu_irq tcirq)
1758 int iomemtype;
1759 struct s3c_adc_state_s *s = (struct s3c_adc_state_s *)
1760 qemu_mallocz(sizeof(struct s3c_adc_state_s));
1762 s->base = base;
1763 s->irq = irq;
1764 s->tcirq = tcirq;
1765 s->convt = qemu_new_timer(vm_clock, s3c_adc_done, s);
1766 s->tst = qemu_new_timer(vm_clock, s3c_adc_tick, s);
1768 s3c_adc_reset(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);
1780 return 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;
1791 i2c_slave slave;
1792 i2c_bus *bus;
1793 target_phys_addr_t base;
1794 qemu_irq irq;
1796 uint8_t control;
1797 uint8_t status;
1798 uint8_t data;
1799 uint8_t addy;
1800 uint8_t mmaster;
1801 int busy;
1802 int newstart;
1803 } s3c_i2c_state_s;
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)
1814 s->control = 0x00;
1815 s->status = 0x00;
1816 s->busy = 0;
1817 s->newstart = 0;
1818 s->mmaster = 0;
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)))
1825 return;
1827 switch (event) {
1828 case I2C_START_RECV:
1829 case I2C_START_SEND:
1830 s->status |= 1 << 2;
1831 s3c_i2c_irq(s);
1832 break;
1833 case I2C_FINISH:
1834 s->status &= ~6;
1835 break;
1836 case I2C_NACK:
1837 s->status |= 1 << 0;
1838 break;
1839 default:
1840 break;
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)))
1848 return 1;
1850 if ((s->status >> 6) == 0)
1851 s->data = data; /* TODO */
1852 s->status &= ~(1 << 0);
1853 s3c_i2c_irq(s);
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)))
1862 return 1;
1864 if ((s->status >> 6) == 1) {
1865 s->status &= ~(1 << 0);
1866 s3c_i2c_irq(s);
1867 return s->data;
1870 return 0x00;
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 */
1878 return;
1879 if ((s->status & 0x90) != 0x90) /* Master */
1880 return;
1881 stop = ~s->status & (1 << 5);
1882 if (s->newstart && s->status & (1 << 5)) { /* START */
1883 s->busy = 1;
1884 start = 1;
1886 s->newstart = 0;
1887 if (!s->busy)
1888 return;
1890 if (start)
1891 ack = !i2c_start_transfer(s->bus, s->data >> 1, (~s->status >> 6) & 1);
1892 else if (stop)
1893 i2c_end_transfer(s->bus);
1894 else if (s->status & (1 << 6))
1895 ack = !i2c_send(s->bus, s->data);
1896 else {
1897 s->data = i2c_recv(s->bus);
1899 if (!(s->control & (1 << 7))) /* ACK */
1900 i2c_nack(s->bus);
1903 if (!(s->status & (1 << 5))) {
1904 s->busy = 0;
1905 return;
1907 s->status &= ~1;
1908 s->status |= !ack;
1909 if (!ack)
1910 s->busy = 0;
1911 s3c_i2c_irq(s);
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;
1925 switch (addr) {
1926 case S3C_IICCON:
1927 return s->control;
1928 case S3C_IICSTAT:
1929 return s->status & ~(1 << 5); /* Busy signal */
1930 case S3C_IICADD:
1931 return s->addy;
1932 case S3C_IICDS:
1933 return s->data;
1934 case S3C2440_IICLC: /* s3c2440 only ! */
1935 return s->mmaster;
1936 default:
1937 printf("%s: Bad register 0x%lx\n", __FUNCTION__, (unsigned long)addr);
1938 break;
1940 return 0;
1943 static void s3c_i2c_write(void *opaque, target_phys_addr_t addr,
1944 uint32_t value)
1946 struct s3c_i2c_state_s *s = (struct s3c_i2c_state_s *) opaque;
1948 switch (addr) {
1949 case S3C_IICCON:
1950 s->control = (s->control | 0xef) & value;
1951 if (s->busy)
1952 s3c_master_work(s);
1953 break;
1955 case S3C_IICSTAT:
1956 s->status &= 0x0f;
1957 s->status |= value & 0xf0;
1958 if (s->status & (1 << 5))
1959 s->newstart = 1;
1960 s3c_master_work(s);
1961 break;
1963 case S3C_IICADD:
1964 s->addy = value & 0x7f;
1965 i2c_set_slave_address(&s->slave, s->addy);
1966 break;
1968 case S3C_IICDS:
1969 s->data = value & 0xff;
1970 break;
1972 case S3C2440_IICLC: /* s3c2440 only ! */
1973 s->mmaster = value & 0xff;
1974 break;
1976 default:
1977 printf("%s: Bad register 0x%lx\n", __FUNCTION__, (unsigned long)addr);
1981 static CPUReadMemoryFunc *s3c_i2c_readfn[] = {
1982 s3c_i2c_read,
1983 s3c_i2c_read,
1984 s3c_i2c_read,
1987 static CPUWriteMemoryFunc *s3c_i2c_writefn[] = {
1988 s3c_i2c_write,
1989 s3c_i2c_write,
1990 s3c_i2c_write,
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);
2021 return 0;
2024 //struct s3c_i2c_state_s *s3c_i2c_init(target_phys_addr_t base, qemu_irq irq)
2025 void s3c_i2c_init(SysBusDevice * dev)
2027 int iomemtype;
2028 struct s3c_i2c_state_s *s = FROM_SYSBUS(struct s3c_i2c_state_s, dev);
2029 #if 0
2030 s->slave.event = s3c_i2c_event;
2031 s->slave.send = s3c_i2c_tx;
2032 s->slave.recv = s3c_i2c_rx;
2033 #endif
2034 s->bus = i2c_init_bus();
2035 sysbus_init_irq(dev, &s->irq);
2036 qdev_attach_child_bus(&dev->qdev, "i2c", s->bus);
2038 s3c_i2c_reset(s);
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)
2049 return s->bus;
2052 /* Serial Peripheral Interface */
2053 struct s3c_spi_state_s {
2054 target_phys_addr_t base;
2056 struct {
2057 qemu_irq irq;
2058 qemu_irq drq;
2059 qemu_irq miso;
2061 uint8_t control;
2062 uint8_t pin;
2063 uint8_t pre;
2065 int cs_pin;
2066 int clk_pin;
2067 int mosi_pin;
2068 uint8_t txbuf;
2069 uint8_t rxbuf;
2070 int bit;
2071 } chan[2];
2073 uint8_t (*txrx[2])(void *opaque, uint8_t value);
2074 uint8_t (*btxrx[2])(void *opaque, uint8_t value);
2075 void *opaque[2];
2078 static void s3c_spi_update(struct s3c_spi_state_s *s)
2080 int i;
2081 for (i = 0; i < 2; i ++) {
2082 switch ((s->chan[i].control >> 5) & 3) { /* SMOD */
2083 case 1:
2084 qemu_irq_raise(s->chan[i].irq);
2085 break;
2086 case 2:
2087 qemu_irq_raise(s->chan[i].drq);
2088 break;
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;
2098 s3c_spi_update(s);
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;
2117 int ch;
2119 ch = addr >> 5;
2121 switch (addr) {
2122 case S3C_SPCON0:
2123 case S3C_SPCON1:
2124 return s->chan[ch].control;
2126 case S3C_SPSTA0:
2127 case S3C_SPSTA1:
2128 return 0x01;
2130 case S3C_SPPIN0:
2131 case S3C_SPPIN1:
2132 return s->chan[ch].pin;
2134 case S3C_SPPRE0:
2135 case S3C_SPPRE1:
2136 return s->chan[ch].pre;
2138 case S3C_SPTDAT0:
2139 case S3C_SPTDAT1:
2140 return s->chan[ch + 2].txbuf;
2142 case S3C_SPRDAT0:
2143 case S3C_SPRDAT1:
2144 if (s->txrx[ch] && (s->chan[ch].control & 0x19) == 0x19)
2145 s->chan[ch].rxbuf = s->txrx[ch](s->opaque[ch], 'Q');
2146 s3c_spi_update(s);
2147 return s->chan[ch].rxbuf;
2149 default:
2150 printf("%s: Bad register 0x%lx\n", __FUNCTION__, (unsigned long)addr);
2151 break;
2153 return 0;
2156 static void s3c_spi_write(void *opaque, target_phys_addr_t addr,
2157 uint32_t value)
2159 struct s3c_spi_state_s *s = (struct s3c_spi_state_s *) opaque;
2160 int ch;
2162 ch = addr >> 5;
2164 switch (addr) {
2165 case S3C_SPCON0:
2166 case S3C_SPCON1:
2167 s->chan[ch].control = value & 0x7f;
2168 s3c_spi_update(s);
2169 break;
2171 case S3C_SPPIN0:
2172 case S3C_SPPIN1:
2173 s->chan[ch].pin = value & 0x07;
2174 break;
2176 case S3C_SPPRE0:
2177 case S3C_SPPRE1:
2178 s->chan[ch].pre = value & 0xff;
2179 break;
2181 case S3C_SPTDAT0:
2182 case S3C_SPTDAT1:
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);
2186 s3c_spi_update(s);
2187 break;
2189 default:
2190 printf("%s: Bad register 0x%lx\n", __FUNCTION__, (unsigned long)addr);
2194 static CPUReadMemoryFunc *s3c_spi_readfn[] = {
2195 s3c_spi_read,
2196 s3c_spi_read,
2197 s3c_spi_read,
2200 static CPUWriteMemoryFunc *s3c_spi_writefn[] = {
2201 s3c_spi_write,
2202 s3c_spi_write,
2203 s3c_spi_write,
2206 static void s3c_spi_save(QEMUFile *f, void *opaque)
2208 struct s3c_spi_state_s *s = (struct s3c_spi_state_s *) opaque;
2209 int i;
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;
2227 int i;
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);
2241 return 0;
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;
2247 int ch = line;
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;
2263 int ch = line;
2264 if (!s->chan[ch].cs_pin)
2265 goto done;
2267 /* Detect CLK rising edge */
2268 if (s->chan[ch].clk_pin || !level)
2269 goto done;
2271 if (s->btxrx[ch]) {
2272 qemu_set_irq(s->chan[ch].miso,
2273 s->btxrx[ch](s->opaque[ch], s->chan[ch].mosi_pin));
2274 goto done;
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) {
2284 if (s->txrx[ch])
2285 s->chan[ch].rxbuf = s->txrx[ch](s->opaque[ch], s->chan[ch].txbuf);
2286 s->chan[ch].bit = 0;
2289 done:
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;
2296 int ch = line;
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)
2310 int i;
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)
2327 int iomemtype;
2328 struct s3c_spi_state_s *s = (struct s3c_spi_state_s *)
2329 qemu_mallocz(sizeof(struct s3c_spi_state_s));
2331 s->base = base;
2332 s->chan[0].irq = irq0;
2333 s->chan[0].drq = drq0;
2334 s->chan[1].irq = irq1;
2335 s->chan[1].drq = drq1;
2337 s3c_spi_reset(s);
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);
2347 return 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)
2354 if (ch & ~1)
2355 cpu_abort(cpu_single_env, "%s: No channel %i\n", __FUNCTION__, ch);
2356 s->txrx[ch] = txrx;
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)
2364 s->tx_en =
2365 (s->control & (1 << 0)) && !(s->control & (1 << 3)) &&
2366 (s->mode & (1 << 7)) && (s->fcontrol & (1 << 13));
2367 s->rx_en =
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)
2388 s->control = 0x100;
2389 s->mode = 0x000;
2390 s->prescaler = 0x000;
2391 s->fcontrol = 0x0000;
2392 s->tx_len = 0;
2393 s->rx_len = 0;
2394 s3c_i2s_update(s);
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;
2406 uint32_t ret;
2408 switch (addr) {
2409 case S3C_IISCON:
2410 return s->control;
2411 case S3C_IISMOD:
2412 return s->mode;
2413 case S3C_IISPSR:
2414 return s->prescaler;
2415 case S3C_IISFCON:
2416 return s->fcontrol |
2417 (MAX(32 - s->tx_len, 0) << 6) |
2418 MIN(s->rx_len, 32);
2419 case S3C_IISFIFO:
2420 if (s->rx_len > 0) {
2421 s->rx_len --;
2422 s3c_i2s_update(s);
2423 s->cycle ^= 1;
2424 if (s->cycle) {
2425 s->buffer = (uint16_t) (ret = s->codec_in(s->opaque));
2426 return ret >> 16;
2427 } else
2428 return s->buffer;
2430 default:
2431 printf("%s: Bad register 0x%lx\n", __FUNCTION__, (unsigned long)addr);
2432 break;
2434 return 0;
2437 static void s3c_i2s_write(void *opaque, target_phys_addr_t addr,
2438 uint32_t value)
2440 struct s3c_i2s_state_s *s = (struct s3c_i2s_state_s *) opaque;
2442 switch (addr) {
2443 case S3C_IISCON:
2444 s->control = (s->control & 0x100) | (value & 0x03f);
2445 s3c_i2s_update(s);
2446 break;
2447 case S3C_IISMOD:
2448 s->mode = value & 0x1ff;
2449 s3c_i2s_update(s);
2450 break;
2451 case S3C_IISPSR:
2452 s->prescaler = value & 0x3ff;
2453 break;
2454 case S3C_IISFCON:
2455 s->fcontrol = value & 0xf000;
2456 s3c_i2s_update(s);
2457 break;
2458 case S3C_IISFIFO:
2459 if (s->tx_len && s->tx_en) {
2460 s->tx_len --;
2461 s3c_i2s_update(s);
2462 if (s->cycle)
2463 s->codec_out(s->opaque, value | ((uint32_t) s->buffer << 16));
2464 else
2465 s->buffer = (uint16_t) value;
2466 s->cycle ^= 1;
2468 break;
2469 default:
2470 printf("%s: Bad register 0x%lx\n", __FUNCTION__, (unsigned long)addr);
2474 static CPUReadMemoryFunc *s3c_i2s_readfn[] = {
2475 s3c_i2s_read,
2476 s3c_i2s_read,
2477 s3c_i2s_read,
2480 static CPUWriteMemoryFunc *s3c_i2s_writefn[] = {
2481 s3c_i2s_write,
2482 s3c_i2s_write,
2483 s3c_i2s_write,
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);
2517 return 0;
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;
2523 s->tx_len = tx;
2524 s->rx_len = rx;
2525 s3c_i2s_update(s);
2528 struct s3c_i2s_state_s *s3c_i2s_init(target_phys_addr_t base, qemu_irq *dma)
2530 int iomemtype;
2531 struct s3c_i2s_state_s *s = (struct s3c_i2s_state_s *)
2532 qemu_mallocz(sizeof(struct s3c_i2s_state_s));
2534 s->base = base;
2535 s->dma = dma;
2536 s->data_req = s3c_i2s_data_req;
2538 s3c_i2s_reset(s);
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);
2546 return s;
2549 /* Watchdog Timer */
2550 struct s3c_wdt_state_s {
2551 struct s3c_freq_s * freq;
2552 target_phys_addr_t base;
2553 qemu_irq irq;
2554 uint16_t control;
2555 uint16_t data;
2556 uint16_t count;
2557 QEMUTimer *tm;
2558 int64_t timestamp;
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);
2566 if (enable) {
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));
2570 } else
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);
2578 int diff;
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;
2589 s->data = 0x8000;
2590 s->count = 0x8000;
2591 s3c_wdt_start(s);
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();
2599 return;
2601 if (s->control & (1 << 2))
2602 qemu_irq_raise(s->irq);
2603 s->count = s->data;
2604 s3c_wdt_start(s);
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;
2615 switch (addr) {
2616 case S3C_WTCON:
2617 return s->control;
2618 case S3C_WTDAT:
2619 return s->data;
2620 case S3C_WTCNT:
2621 s3c_wdt_stop(s);
2622 return s->count;
2623 default:
2624 printf("%s: Bad register 0x%lx\n", __FUNCTION__, (unsigned long)addr);
2625 break;
2627 return 0;
2630 static void s3c_wdt_write(void *opaque, target_phys_addr_t addr,
2631 uint32_t value)
2633 struct s3c_wdt_state_s *s = (struct s3c_wdt_state_s *) opaque;
2635 switch (addr) {
2636 case S3C_WTCON:
2637 s3c_wdt_stop(s);
2638 s->control = value;
2639 s3c_wdt_start(s);
2640 break;
2641 case S3C_WTDAT:
2642 s->data = value;
2643 break;
2644 case S3C_WTCNT:
2645 s->count = value;
2646 s3c_wdt_start(s);
2647 break;
2648 default:
2649 printf("%s: Bad register 0x%lx\n", __FUNCTION__, (unsigned long)addr);
2653 static CPUReadMemoryFunc *s3c_wdt_readfn[] = {
2654 s3c_wdt_read,
2655 s3c_wdt_read,
2656 s3c_wdt_read,
2659 static CPUWriteMemoryFunc *s3c_wdt_writefn[] = {
2660 s3c_wdt_write,
2661 s3c_wdt_write,
2662 s3c_wdt_write,
2665 static void s3c_wdt_save(QEMUFile *f, void *opaque)
2667 struct s3c_wdt_state_s *s = (struct s3c_wdt_state_s *) opaque;
2669 s3c_wdt_stop(s);
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);
2684 s3c_wdt_start(s);
2686 return 0;
2689 struct s3c_wdt_state_s *s3c_wdt_init(struct s3c_freq_s * freq, target_phys_addr_t base, qemu_irq irq)
2691 int iomemtype;
2692 struct s3c_wdt_state_s *s = (struct s3c_wdt_state_s *)
2693 qemu_mallocz(sizeof(struct s3c_wdt_state_s));
2695 s->freq = freq;
2696 s->base = base;
2697 s->irq = irq;
2698 s->tm = qemu_new_timer(vm_clock, s3c_wdt_timeout, s);
2700 s3c_wdt_reset(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);
2708 return s;
2711 /* On-chip UARTs */
2712 static struct {
2713 target_phys_addr_t base;
2714 int irq[3];
2715 int dma[1];
2716 } s3c2410_uart[] = {
2718 0x50000000,
2719 { S3C_PICS_RXD0, S3C_PICS_TXD0, S3C_PICS_ERR0 },
2720 { S3C_RQ_UART0 },
2723 0x50004000,
2724 { S3C_PICS_RXD1, S3C_PICS_TXD1, S3C_PICS_ERR1 },
2725 { S3C_RQ_UART1 },
2728 0x50008000,
2729 { S3C_PICS_RXD2, S3C_PICS_TXD2, S3C_PICS_ERR2 },
2730 { S3C_RQ_UART2 },
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;
2739 int i;
2740 s3c_mc_reset(s);
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]);
2758 cpu_reset(s->env);
2761 struct s3c_state_s * g_s3c;
2763 /* Initialise an S3C24XX microprocessor. */
2764 struct s3c_state_s *s3c24xx_init(
2765 uint32_t cpu_id,
2766 uint32_t xtal,
2767 unsigned int sdram_size,
2768 uint32_t sram_address,
2769 SDState *mmc)
2771 struct s3c_state_s *s;
2772 int iomemtype, i;
2773 s = (struct s3c_state_s *) qemu_mallocz(sizeof(struct s3c_state_s));
2775 g_s3c = s;
2777 s->cpu_id = cpu_id;
2778 s->clock.xtal = xtal;
2779 s->clock.pclk = 66500000; // S3C_PCLK_FREQ; // TEMP
2781 s->env = cpu_init("arm920t");
2782 if (!s->env) {
2783 fprintf(stderr, "Unable to initialize ARM920T\n");
2784 exit(2);
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;
2797 s3c_mc_reset(s);
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();
2821 else
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]]);
2829 if (serial_hds[i])
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]);
2840 // s->i2c = NULL;
2841 DeviceState * dev;
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);
2863 if (usb_enabled) {
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);
2873 return s;
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);