qemu-kvm: Remove kvm subdirectory (Jan Kiszka)
[qemu-kvm.git] / hw / mcf5206.c
blob7b6d501447c339afb18cc419891fc450bfe661d4
1 /*
2 * Motorola ColdFire MCF5206 SoC embedded peripheral emulation.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licensed under the GPL
7 */
8 #include "hw.h"
9 #include "mcf.h"
10 #include "qemu-timer.h"
11 #include "sysemu.h"
12 #include "exec-memory.h"
14 /* General purpose timer module. */
15 typedef struct {
16 uint16_t tmr;
17 uint16_t trr;
18 uint16_t tcr;
19 uint16_t ter;
20 ptimer_state *timer;
21 qemu_irq irq;
22 int irq_state;
23 } m5206_timer_state;
25 #define TMR_RST 0x01
26 #define TMR_CLK 0x06
27 #define TMR_FRR 0x08
28 #define TMR_ORI 0x10
29 #define TMR_OM 0x20
30 #define TMR_CE 0xc0
32 #define TER_CAP 0x01
33 #define TER_REF 0x02
35 static void m5206_timer_update(m5206_timer_state *s)
37 if ((s->tmr & TMR_ORI) != 0 && (s->ter & TER_REF))
38 qemu_irq_raise(s->irq);
39 else
40 qemu_irq_lower(s->irq);
43 static void m5206_timer_reset(m5206_timer_state *s)
45 s->tmr = 0;
46 s->trr = 0;
49 static void m5206_timer_recalibrate(m5206_timer_state *s)
51 int prescale;
52 int mode;
54 ptimer_stop(s->timer);
56 if ((s->tmr & TMR_RST) == 0)
57 return;
59 prescale = (s->tmr >> 8) + 1;
60 mode = (s->tmr >> 1) & 3;
61 if (mode == 2)
62 prescale *= 16;
64 if (mode == 3 || mode == 0)
65 hw_error("m5206_timer: mode %d not implemented\n", mode);
66 if ((s->tmr & TMR_FRR) == 0)
67 hw_error("m5206_timer: free running mode not implemented\n");
69 /* Assume 66MHz system clock. */
70 ptimer_set_freq(s->timer, 66000000 / prescale);
72 ptimer_set_limit(s->timer, s->trr, 0);
74 ptimer_run(s->timer, 0);
77 static void m5206_timer_trigger(void *opaque)
79 m5206_timer_state *s = (m5206_timer_state *)opaque;
80 s->ter |= TER_REF;
81 m5206_timer_update(s);
84 static uint32_t m5206_timer_read(m5206_timer_state *s, uint32_t addr)
86 switch (addr) {
87 case 0:
88 return s->tmr;
89 case 4:
90 return s->trr;
91 case 8:
92 return s->tcr;
93 case 0xc:
94 return s->trr - ptimer_get_count(s->timer);
95 case 0x11:
96 return s->ter;
97 default:
98 return 0;
102 static void m5206_timer_write(m5206_timer_state *s, uint32_t addr, uint32_t val)
104 switch (addr) {
105 case 0:
106 if ((s->tmr & TMR_RST) != 0 && (val & TMR_RST) == 0) {
107 m5206_timer_reset(s);
109 s->tmr = val;
110 m5206_timer_recalibrate(s);
111 break;
112 case 4:
113 s->trr = val;
114 m5206_timer_recalibrate(s);
115 break;
116 case 8:
117 s->tcr = val;
118 break;
119 case 0xc:
120 ptimer_set_count(s->timer, val);
121 break;
122 case 0x11:
123 s->ter &= ~val;
124 break;
125 default:
126 break;
128 m5206_timer_update(s);
131 static m5206_timer_state *m5206_timer_init(qemu_irq irq)
133 m5206_timer_state *s;
134 QEMUBH *bh;
136 s = (m5206_timer_state *)g_malloc0(sizeof(m5206_timer_state));
137 bh = qemu_bh_new(m5206_timer_trigger, s);
138 s->timer = ptimer_init(bh);
139 s->irq = irq;
140 m5206_timer_reset(s);
141 return s;
144 /* System Integration Module. */
146 typedef struct {
147 CPUState *env;
148 MemoryRegion iomem;
149 m5206_timer_state *timer[2];
150 void *uart[2];
151 uint8_t scr;
152 uint8_t icr[14];
153 uint16_t imr; /* 1 == interrupt is masked. */
154 uint16_t ipr;
155 uint8_t rsr;
156 uint8_t swivr;
157 uint8_t par;
158 /* Include the UART vector registers here. */
159 uint8_t uivr[2];
160 } m5206_mbar_state;
162 /* Interrupt controller. */
164 static int m5206_find_pending_irq(m5206_mbar_state *s)
166 int level;
167 int vector;
168 uint16_t active;
169 int i;
171 level = 0;
172 vector = 0;
173 active = s->ipr & ~s->imr;
174 if (!active)
175 return 0;
177 for (i = 1; i < 14; i++) {
178 if (active & (1 << i)) {
179 if ((s->icr[i] & 0x1f) > level) {
180 level = s->icr[i] & 0x1f;
181 vector = i;
186 if (level < 4)
187 vector = 0;
189 return vector;
192 static void m5206_mbar_update(m5206_mbar_state *s)
194 int irq;
195 int vector;
196 int level;
198 irq = m5206_find_pending_irq(s);
199 if (irq) {
200 int tmp;
201 tmp = s->icr[irq];
202 level = (tmp >> 2) & 7;
203 if (tmp & 0x80) {
204 /* Autovector. */
205 vector = 24 + level;
206 } else {
207 switch (irq) {
208 case 8: /* SWT */
209 vector = s->swivr;
210 break;
211 case 12: /* UART1 */
212 vector = s->uivr[0];
213 break;
214 case 13: /* UART2 */
215 vector = s->uivr[1];
216 break;
217 default:
218 /* Unknown vector. */
219 fprintf(stderr, "Unhandled vector for IRQ %d\n", irq);
220 vector = 0xf;
221 break;
224 } else {
225 level = 0;
226 vector = 0;
228 m68k_set_irq_level(s->env, level, vector);
231 static void m5206_mbar_set_irq(void *opaque, int irq, int level)
233 m5206_mbar_state *s = (m5206_mbar_state *)opaque;
234 if (level) {
235 s->ipr |= 1 << irq;
236 } else {
237 s->ipr &= ~(1 << irq);
239 m5206_mbar_update(s);
242 /* System Integration Module. */
244 static void m5206_mbar_reset(m5206_mbar_state *s)
246 s->scr = 0xc0;
247 s->icr[1] = 0x04;
248 s->icr[2] = 0x08;
249 s->icr[3] = 0x0c;
250 s->icr[4] = 0x10;
251 s->icr[5] = 0x14;
252 s->icr[6] = 0x18;
253 s->icr[7] = 0x1c;
254 s->icr[8] = 0x1c;
255 s->icr[9] = 0x80;
256 s->icr[10] = 0x80;
257 s->icr[11] = 0x80;
258 s->icr[12] = 0x00;
259 s->icr[13] = 0x00;
260 s->imr = 0x3ffe;
261 s->rsr = 0x80;
262 s->swivr = 0x0f;
263 s->par = 0;
266 static uint64_t m5206_mbar_read(m5206_mbar_state *s,
267 uint64_t offset, unsigned size)
269 if (offset >= 0x100 && offset < 0x120) {
270 return m5206_timer_read(s->timer[0], offset - 0x100);
271 } else if (offset >= 0x120 && offset < 0x140) {
272 return m5206_timer_read(s->timer[1], offset - 0x120);
273 } else if (offset >= 0x140 && offset < 0x160) {
274 return mcf_uart_read(s->uart[0], offset - 0x140, size);
275 } else if (offset >= 0x180 && offset < 0x1a0) {
276 return mcf_uart_read(s->uart[1], offset - 0x180, size);
278 switch (offset) {
279 case 0x03: return s->scr;
280 case 0x14 ... 0x20: return s->icr[offset - 0x13];
281 case 0x36: return s->imr;
282 case 0x3a: return s->ipr;
283 case 0x40: return s->rsr;
284 case 0x41: return 0;
285 case 0x42: return s->swivr;
286 case 0x50:
287 /* DRAM mask register. */
288 /* FIXME: currently hardcoded to 128Mb. */
290 uint32_t mask = ~0;
291 while (mask > ram_size)
292 mask >>= 1;
293 return mask & 0x0ffe0000;
295 case 0x5c: return 1; /* DRAM bank 1 empty. */
296 case 0xcb: return s->par;
297 case 0x170: return s->uivr[0];
298 case 0x1b0: return s->uivr[1];
300 hw_error("Bad MBAR read offset 0x%x", (int)offset);
301 return 0;
304 static void m5206_mbar_write(m5206_mbar_state *s, uint32_t offset,
305 uint64_t value, unsigned size)
307 if (offset >= 0x100 && offset < 0x120) {
308 m5206_timer_write(s->timer[0], offset - 0x100, value);
309 return;
310 } else if (offset >= 0x120 && offset < 0x140) {
311 m5206_timer_write(s->timer[1], offset - 0x120, value);
312 return;
313 } else if (offset >= 0x140 && offset < 0x160) {
314 mcf_uart_write(s->uart[0], offset - 0x140, value, size);
315 return;
316 } else if (offset >= 0x180 && offset < 0x1a0) {
317 mcf_uart_write(s->uart[1], offset - 0x180, value, size);
318 return;
320 switch (offset) {
321 case 0x03:
322 s->scr = value;
323 break;
324 case 0x14 ... 0x20:
325 s->icr[offset - 0x13] = value;
326 m5206_mbar_update(s);
327 break;
328 case 0x36:
329 s->imr = value;
330 m5206_mbar_update(s);
331 break;
332 case 0x40:
333 s->rsr &= ~value;
334 break;
335 case 0x41:
336 /* TODO: implement watchdog. */
337 break;
338 case 0x42:
339 s->swivr = value;
340 break;
341 case 0xcb:
342 s->par = value;
343 break;
344 case 0x170:
345 s->uivr[0] = value;
346 break;
347 case 0x178: case 0x17c: case 0x1c8: case 0x1bc:
348 /* Not implemented: UART Output port bits. */
349 break;
350 case 0x1b0:
351 s->uivr[1] = value;
352 break;
353 default:
354 hw_error("Bad MBAR write offset 0x%x", (int)offset);
355 break;
359 /* Internal peripherals use a variety of register widths.
360 This lookup table allows a single routine to handle all of them. */
361 static const int m5206_mbar_width[] =
363 /* 000-040 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
364 /* 040-080 */ 1, 2, 2, 2, 4, 1, 2, 4, 1, 2, 4, 2, 2, 4, 2, 2,
365 /* 080-0c0 */ 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4, 2, 2, 4,
366 /* 0c0-100 */ 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
367 /* 100-140 */ 2, 2, 2, 2, 1, 0, 0, 0, 2, 2, 2, 2, 1, 0, 0, 0,
368 /* 140-180 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
369 /* 180-1c0 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
370 /* 1c0-200 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
373 static uint32_t m5206_mbar_readw(void *opaque, target_phys_addr_t offset);
374 static uint32_t m5206_mbar_readl(void *opaque, target_phys_addr_t offset);
376 static uint32_t m5206_mbar_readb(void *opaque, target_phys_addr_t offset)
378 m5206_mbar_state *s = (m5206_mbar_state *)opaque;
379 offset &= 0x3ff;
380 if (offset > 0x200) {
381 hw_error("Bad MBAR read offset 0x%x", (int)offset);
383 if (m5206_mbar_width[offset >> 2] > 1) {
384 uint16_t val;
385 val = m5206_mbar_readw(opaque, offset & ~1);
386 if ((offset & 1) == 0) {
387 val >>= 8;
389 return val & 0xff;
391 return m5206_mbar_read(s, offset, 1);
394 static uint32_t m5206_mbar_readw(void *opaque, target_phys_addr_t offset)
396 m5206_mbar_state *s = (m5206_mbar_state *)opaque;
397 int width;
398 offset &= 0x3ff;
399 if (offset > 0x200) {
400 hw_error("Bad MBAR read offset 0x%x", (int)offset);
402 width = m5206_mbar_width[offset >> 2];
403 if (width > 2) {
404 uint32_t val;
405 val = m5206_mbar_readl(opaque, offset & ~3);
406 if ((offset & 3) == 0)
407 val >>= 16;
408 return val & 0xffff;
409 } else if (width < 2) {
410 uint16_t val;
411 val = m5206_mbar_readb(opaque, offset) << 8;
412 val |= m5206_mbar_readb(opaque, offset + 1);
413 return val;
415 return m5206_mbar_read(s, offset, 2);
418 static uint32_t m5206_mbar_readl(void *opaque, target_phys_addr_t offset)
420 m5206_mbar_state *s = (m5206_mbar_state *)opaque;
421 int width;
422 offset &= 0x3ff;
423 if (offset > 0x200) {
424 hw_error("Bad MBAR read offset 0x%x", (int)offset);
426 width = m5206_mbar_width[offset >> 2];
427 if (width < 4) {
428 uint32_t val;
429 val = m5206_mbar_readw(opaque, offset) << 16;
430 val |= m5206_mbar_readw(opaque, offset + 2);
431 return val;
433 return m5206_mbar_read(s, offset, 4);
436 static void m5206_mbar_writew(void *opaque, target_phys_addr_t offset,
437 uint32_t value);
438 static void m5206_mbar_writel(void *opaque, target_phys_addr_t offset,
439 uint32_t value);
441 static void m5206_mbar_writeb(void *opaque, target_phys_addr_t offset,
442 uint32_t value)
444 m5206_mbar_state *s = (m5206_mbar_state *)opaque;
445 int width;
446 offset &= 0x3ff;
447 if (offset > 0x200) {
448 hw_error("Bad MBAR write offset 0x%x", (int)offset);
450 width = m5206_mbar_width[offset >> 2];
451 if (width > 1) {
452 uint32_t tmp;
453 tmp = m5206_mbar_readw(opaque, offset & ~1);
454 if (offset & 1) {
455 tmp = (tmp & 0xff00) | value;
456 } else {
457 tmp = (tmp & 0x00ff) | (value << 8);
459 m5206_mbar_writew(opaque, offset & ~1, tmp);
460 return;
462 m5206_mbar_write(s, offset, value, 1);
465 static void m5206_mbar_writew(void *opaque, target_phys_addr_t offset,
466 uint32_t value)
468 m5206_mbar_state *s = (m5206_mbar_state *)opaque;
469 int width;
470 offset &= 0x3ff;
471 if (offset > 0x200) {
472 hw_error("Bad MBAR write offset 0x%x", (int)offset);
474 width = m5206_mbar_width[offset >> 2];
475 if (width > 2) {
476 uint32_t tmp;
477 tmp = m5206_mbar_readl(opaque, offset & ~3);
478 if (offset & 3) {
479 tmp = (tmp & 0xffff0000) | value;
480 } else {
481 tmp = (tmp & 0x0000ffff) | (value << 16);
483 m5206_mbar_writel(opaque, offset & ~3, tmp);
484 return;
485 } else if (width < 2) {
486 m5206_mbar_writeb(opaque, offset, value >> 8);
487 m5206_mbar_writeb(opaque, offset + 1, value & 0xff);
488 return;
490 m5206_mbar_write(s, offset, value, 2);
493 static void m5206_mbar_writel(void *opaque, target_phys_addr_t offset,
494 uint32_t value)
496 m5206_mbar_state *s = (m5206_mbar_state *)opaque;
497 int width;
498 offset &= 0x3ff;
499 if (offset > 0x200) {
500 hw_error("Bad MBAR write offset 0x%x", (int)offset);
502 width = m5206_mbar_width[offset >> 2];
503 if (width < 4) {
504 m5206_mbar_writew(opaque, offset, value >> 16);
505 m5206_mbar_writew(opaque, offset + 2, value & 0xffff);
506 return;
508 m5206_mbar_write(s, offset, value, 4);
511 static const MemoryRegionOps m5206_mbar_ops = {
512 .old_mmio = {
513 .read = {
514 m5206_mbar_readb,
515 m5206_mbar_readw,
516 m5206_mbar_readl,
518 .write = {
519 m5206_mbar_writeb,
520 m5206_mbar_writew,
521 m5206_mbar_writel,
524 .endianness = DEVICE_NATIVE_ENDIAN,
527 qemu_irq *mcf5206_init(MemoryRegion *sysmem, uint32_t base, CPUState *env)
529 m5206_mbar_state *s;
530 qemu_irq *pic;
532 s = (m5206_mbar_state *)g_malloc0(sizeof(m5206_mbar_state));
534 memory_region_init_io(&s->iomem, &m5206_mbar_ops, s,
535 "mbar", 0x00001000);
536 memory_region_add_subregion(sysmem, base, &s->iomem);
538 pic = qemu_allocate_irqs(m5206_mbar_set_irq, s, 14);
539 s->timer[0] = m5206_timer_init(pic[9]);
540 s->timer[1] = m5206_timer_init(pic[10]);
541 s->uart[0] = mcf_uart_init(pic[12], serial_hds[0]);
542 s->uart[1] = mcf_uart_init(pic[13], serial_hds[1]);
543 s->env = env;
545 m5206_mbar_reset(s);
546 return pic;