Cope with arch-specific page protection flags in mmap (Richard Purdie).
[qemu/mini2440/sniper_sniper_test.git] / hw / omap1.c
blobb4394230358f4bad78be5ac0be306c0b20d3be9b
1 /*
2 * TI OMAP processors emulation.
4 * Copyright (C) 2006-2008 Andrzej Zaborowski <balrog@zabor.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 or
9 * (at your option) version 3 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19 * MA 02111-1307 USA
21 #include "hw.h"
22 #include "arm-misc.h"
23 #include "omap.h"
24 #include "sysemu.h"
25 #include "qemu-timer.h"
26 #include "qemu-char.h"
27 /* We use pc-style serial ports. */
28 #include "pc.h"
30 /* Should signal the TCMI/GPMC */
31 uint32_t omap_badwidth_read8(void *opaque, target_phys_addr_t addr)
33 uint8_t ret;
35 OMAP_8B_REG(addr);
36 cpu_physical_memory_read(addr, (void *) &ret, 1);
37 return ret;
40 void omap_badwidth_write8(void *opaque, target_phys_addr_t addr,
41 uint32_t value)
43 uint8_t val8 = value;
45 OMAP_8B_REG(addr);
46 cpu_physical_memory_write(addr, (void *) &val8, 1);
49 uint32_t omap_badwidth_read16(void *opaque, target_phys_addr_t addr)
51 uint16_t ret;
53 OMAP_16B_REG(addr);
54 cpu_physical_memory_read(addr, (void *) &ret, 2);
55 return ret;
58 void omap_badwidth_write16(void *opaque, target_phys_addr_t addr,
59 uint32_t value)
61 uint16_t val16 = value;
63 OMAP_16B_REG(addr);
64 cpu_physical_memory_write(addr, (void *) &val16, 2);
67 uint32_t omap_badwidth_read32(void *opaque, target_phys_addr_t addr)
69 uint32_t ret;
71 OMAP_32B_REG(addr);
72 cpu_physical_memory_read(addr, (void *) &ret, 4);
73 return ret;
76 void omap_badwidth_write32(void *opaque, target_phys_addr_t addr,
77 uint32_t value)
79 OMAP_32B_REG(addr);
80 cpu_physical_memory_write(addr, (void *) &value, 4);
83 /* Interrupt Handlers */
84 struct omap_intr_handler_bank_s {
85 uint32_t irqs;
86 uint32_t inputs;
87 uint32_t mask;
88 uint32_t fiq;
89 uint32_t sens_edge;
90 uint32_t swi;
91 unsigned char priority[32];
94 struct omap_intr_handler_s {
95 qemu_irq *pins;
96 qemu_irq parent_intr[2];
97 target_phys_addr_t base;
98 unsigned char nbanks;
99 int level_only;
101 /* state */
102 uint32_t new_agr[2];
103 int sir_intr[2];
104 int autoidle;
105 uint32_t mask;
106 struct omap_intr_handler_bank_s bank[];
109 static void omap_inth_sir_update(struct omap_intr_handler_s *s, int is_fiq)
111 int i, j, sir_intr, p_intr, p, f;
112 uint32_t level;
113 sir_intr = 0;
114 p_intr = 255;
116 /* Find the interrupt line with the highest dynamic priority.
117 * Note: 0 denotes the hightest priority.
118 * If all interrupts have the same priority, the default order is IRQ_N,
119 * IRQ_N-1,...,IRQ_0. */
120 for (j = 0; j < s->nbanks; ++j) {
121 level = s->bank[j].irqs & ~s->bank[j].mask &
122 (is_fiq ? s->bank[j].fiq : ~s->bank[j].fiq);
123 for (f = ffs(level), i = f - 1, level >>= f - 1; f; i += f,
124 level >>= f) {
125 p = s->bank[j].priority[i];
126 if (p <= p_intr) {
127 p_intr = p;
128 sir_intr = 32 * j + i;
130 f = ffs(level >> 1);
133 s->sir_intr[is_fiq] = sir_intr;
136 static inline void omap_inth_update(struct omap_intr_handler_s *s, int is_fiq)
138 int i;
139 uint32_t has_intr = 0;
141 for (i = 0; i < s->nbanks; ++i)
142 has_intr |= s->bank[i].irqs & ~s->bank[i].mask &
143 (is_fiq ? s->bank[i].fiq : ~s->bank[i].fiq);
145 if (s->new_agr[is_fiq] & has_intr & s->mask) {
146 s->new_agr[is_fiq] = 0;
147 omap_inth_sir_update(s, is_fiq);
148 qemu_set_irq(s->parent_intr[is_fiq], 1);
152 #define INT_FALLING_EDGE 0
153 #define INT_LOW_LEVEL 1
155 static void omap_set_intr(void *opaque, int irq, int req)
157 struct omap_intr_handler_s *ih = (struct omap_intr_handler_s *) opaque;
158 uint32_t rise;
160 struct omap_intr_handler_bank_s *bank = &ih->bank[irq >> 5];
161 int n = irq & 31;
163 if (req) {
164 rise = ~bank->irqs & (1 << n);
165 if (~bank->sens_edge & (1 << n))
166 rise &= ~bank->inputs;
168 bank->inputs |= (1 << n);
169 if (rise) {
170 bank->irqs |= rise;
171 omap_inth_update(ih, 0);
172 omap_inth_update(ih, 1);
174 } else {
175 rise = bank->sens_edge & bank->irqs & (1 << n);
176 bank->irqs &= ~rise;
177 bank->inputs &= ~(1 << n);
181 /* Simplified version with no edge detection */
182 static void omap_set_intr_noedge(void *opaque, int irq, int req)
184 struct omap_intr_handler_s *ih = (struct omap_intr_handler_s *) opaque;
185 uint32_t rise;
187 struct omap_intr_handler_bank_s *bank = &ih->bank[irq >> 5];
188 int n = irq & 31;
190 if (req) {
191 rise = ~bank->inputs & (1 << n);
192 if (rise) {
193 bank->irqs |= bank->inputs |= rise;
194 omap_inth_update(ih, 0);
195 omap_inth_update(ih, 1);
197 } else
198 bank->irqs = (bank->inputs &= ~(1 << n)) | bank->swi;
201 static uint32_t omap_inth_read(void *opaque, target_phys_addr_t addr)
203 struct omap_intr_handler_s *s = (struct omap_intr_handler_s *) opaque;
204 int i, offset = addr - s->base;
205 int bank_no = offset >> 8;
206 int line_no;
207 struct omap_intr_handler_bank_s *bank = &s->bank[bank_no];
208 offset &= 0xff;
210 switch (offset) {
211 case 0x00: /* ITR */
212 return bank->irqs;
214 case 0x04: /* MIR */
215 return bank->mask;
217 case 0x10: /* SIR_IRQ_CODE */
218 case 0x14: /* SIR_FIQ_CODE */
219 if (bank_no != 0)
220 break;
221 line_no = s->sir_intr[(offset - 0x10) >> 2];
222 bank = &s->bank[line_no >> 5];
223 i = line_no & 31;
224 if (((bank->sens_edge >> i) & 1) == INT_FALLING_EDGE)
225 bank->irqs &= ~(1 << i);
226 return line_no;
228 case 0x18: /* CONTROL_REG */
229 if (bank_no != 0)
230 break;
231 return 0;
233 case 0x1c: /* ILR0 */
234 case 0x20: /* ILR1 */
235 case 0x24: /* ILR2 */
236 case 0x28: /* ILR3 */
237 case 0x2c: /* ILR4 */
238 case 0x30: /* ILR5 */
239 case 0x34: /* ILR6 */
240 case 0x38: /* ILR7 */
241 case 0x3c: /* ILR8 */
242 case 0x40: /* ILR9 */
243 case 0x44: /* ILR10 */
244 case 0x48: /* ILR11 */
245 case 0x4c: /* ILR12 */
246 case 0x50: /* ILR13 */
247 case 0x54: /* ILR14 */
248 case 0x58: /* ILR15 */
249 case 0x5c: /* ILR16 */
250 case 0x60: /* ILR17 */
251 case 0x64: /* ILR18 */
252 case 0x68: /* ILR19 */
253 case 0x6c: /* ILR20 */
254 case 0x70: /* ILR21 */
255 case 0x74: /* ILR22 */
256 case 0x78: /* ILR23 */
257 case 0x7c: /* ILR24 */
258 case 0x80: /* ILR25 */
259 case 0x84: /* ILR26 */
260 case 0x88: /* ILR27 */
261 case 0x8c: /* ILR28 */
262 case 0x90: /* ILR29 */
263 case 0x94: /* ILR30 */
264 case 0x98: /* ILR31 */
265 i = (offset - 0x1c) >> 2;
266 return (bank->priority[i] << 2) |
267 (((bank->sens_edge >> i) & 1) << 1) |
268 ((bank->fiq >> i) & 1);
270 case 0x9c: /* ISR */
271 return 0x00000000;
274 OMAP_BAD_REG(addr);
275 return 0;
278 static void omap_inth_write(void *opaque, target_phys_addr_t addr,
279 uint32_t value)
281 struct omap_intr_handler_s *s = (struct omap_intr_handler_s *) opaque;
282 int i, offset = addr - s->base;
283 int bank_no = offset >> 8;
284 struct omap_intr_handler_bank_s *bank = &s->bank[bank_no];
285 offset &= 0xff;
287 switch (offset) {
288 case 0x00: /* ITR */
289 /* Important: ignore the clearing if the IRQ is level-triggered and
290 the input bit is 1 */
291 bank->irqs &= value | (bank->inputs & bank->sens_edge);
292 return;
294 case 0x04: /* MIR */
295 bank->mask = value;
296 omap_inth_update(s, 0);
297 omap_inth_update(s, 1);
298 return;
300 case 0x10: /* SIR_IRQ_CODE */
301 case 0x14: /* SIR_FIQ_CODE */
302 OMAP_RO_REG(addr);
303 break;
305 case 0x18: /* CONTROL_REG */
306 if (bank_no != 0)
307 break;
308 if (value & 2) {
309 qemu_set_irq(s->parent_intr[1], 0);
310 s->new_agr[1] = ~0;
311 omap_inth_update(s, 1);
313 if (value & 1) {
314 qemu_set_irq(s->parent_intr[0], 0);
315 s->new_agr[0] = ~0;
316 omap_inth_update(s, 0);
318 return;
320 case 0x1c: /* ILR0 */
321 case 0x20: /* ILR1 */
322 case 0x24: /* ILR2 */
323 case 0x28: /* ILR3 */
324 case 0x2c: /* ILR4 */
325 case 0x30: /* ILR5 */
326 case 0x34: /* ILR6 */
327 case 0x38: /* ILR7 */
328 case 0x3c: /* ILR8 */
329 case 0x40: /* ILR9 */
330 case 0x44: /* ILR10 */
331 case 0x48: /* ILR11 */
332 case 0x4c: /* ILR12 */
333 case 0x50: /* ILR13 */
334 case 0x54: /* ILR14 */
335 case 0x58: /* ILR15 */
336 case 0x5c: /* ILR16 */
337 case 0x60: /* ILR17 */
338 case 0x64: /* ILR18 */
339 case 0x68: /* ILR19 */
340 case 0x6c: /* ILR20 */
341 case 0x70: /* ILR21 */
342 case 0x74: /* ILR22 */
343 case 0x78: /* ILR23 */
344 case 0x7c: /* ILR24 */
345 case 0x80: /* ILR25 */
346 case 0x84: /* ILR26 */
347 case 0x88: /* ILR27 */
348 case 0x8c: /* ILR28 */
349 case 0x90: /* ILR29 */
350 case 0x94: /* ILR30 */
351 case 0x98: /* ILR31 */
352 i = (offset - 0x1c) >> 2;
353 bank->priority[i] = (value >> 2) & 0x1f;
354 bank->sens_edge &= ~(1 << i);
355 bank->sens_edge |= ((value >> 1) & 1) << i;
356 bank->fiq &= ~(1 << i);
357 bank->fiq |= (value & 1) << i;
358 return;
360 case 0x9c: /* ISR */
361 for (i = 0; i < 32; i ++)
362 if (value & (1 << i)) {
363 omap_set_intr(s, 32 * bank_no + i, 1);
364 return;
366 return;
368 OMAP_BAD_REG(addr);
371 static CPUReadMemoryFunc *omap_inth_readfn[] = {
372 omap_badwidth_read32,
373 omap_badwidth_read32,
374 omap_inth_read,
377 static CPUWriteMemoryFunc *omap_inth_writefn[] = {
378 omap_inth_write,
379 omap_inth_write,
380 omap_inth_write,
383 void omap_inth_reset(struct omap_intr_handler_s *s)
385 int i;
387 for (i = 0; i < s->nbanks; ++i){
388 s->bank[i].irqs = 0x00000000;
389 s->bank[i].mask = 0xffffffff;
390 s->bank[i].sens_edge = 0x00000000;
391 s->bank[i].fiq = 0x00000000;
392 s->bank[i].inputs = 0x00000000;
393 s->bank[i].swi = 0x00000000;
394 memset(s->bank[i].priority, 0, sizeof(s->bank[i].priority));
396 if (s->level_only)
397 s->bank[i].sens_edge = 0xffffffff;
400 s->new_agr[0] = ~0;
401 s->new_agr[1] = ~0;
402 s->sir_intr[0] = 0;
403 s->sir_intr[1] = 0;
404 s->autoidle = 0;
405 s->mask = ~0;
407 qemu_set_irq(s->parent_intr[0], 0);
408 qemu_set_irq(s->parent_intr[1], 0);
411 struct omap_intr_handler_s *omap_inth_init(target_phys_addr_t base,
412 unsigned long size, unsigned char nbanks, qemu_irq **pins,
413 qemu_irq parent_irq, qemu_irq parent_fiq, omap_clk clk)
415 int iomemtype;
416 struct omap_intr_handler_s *s = (struct omap_intr_handler_s *)
417 qemu_mallocz(sizeof(struct omap_intr_handler_s) +
418 sizeof(struct omap_intr_handler_bank_s) * nbanks);
420 s->parent_intr[0] = parent_irq;
421 s->parent_intr[1] = parent_fiq;
422 s->base = base;
423 s->nbanks = nbanks;
424 s->pins = qemu_allocate_irqs(omap_set_intr, s, nbanks * 32);
425 if (pins)
426 *pins = s->pins;
428 omap_inth_reset(s);
430 iomemtype = cpu_register_io_memory(0, omap_inth_readfn,
431 omap_inth_writefn, s);
432 cpu_register_physical_memory(s->base, size, iomemtype);
434 return s;
437 static uint32_t omap2_inth_read(void *opaque, target_phys_addr_t addr)
439 struct omap_intr_handler_s *s = (struct omap_intr_handler_s *) opaque;
440 int offset = addr - s->base;
441 int bank_no, line_no;
442 struct omap_intr_handler_bank_s *bank = 0;
444 if ((offset & 0xf80) == 0x80) {
445 bank_no = (offset & 0x60) >> 5;
446 if (bank_no < s->nbanks) {
447 offset &= ~0x60;
448 bank = &s->bank[bank_no];
452 switch (offset) {
453 case 0x00: /* INTC_REVISION */
454 return 0x21;
456 case 0x10: /* INTC_SYSCONFIG */
457 return (s->autoidle >> 2) & 1;
459 case 0x14: /* INTC_SYSSTATUS */
460 return 1; /* RESETDONE */
462 case 0x40: /* INTC_SIR_IRQ */
463 return s->sir_intr[0];
465 case 0x44: /* INTC_SIR_FIQ */
466 return s->sir_intr[1];
468 case 0x48: /* INTC_CONTROL */
469 return (!s->mask) << 2; /* GLOBALMASK */
471 case 0x4c: /* INTC_PROTECTION */
472 return 0;
474 case 0x50: /* INTC_IDLE */
475 return s->autoidle & 3;
477 /* Per-bank registers */
478 case 0x80: /* INTC_ITR */
479 return bank->inputs;
481 case 0x84: /* INTC_MIR */
482 return bank->mask;
484 case 0x88: /* INTC_MIR_CLEAR */
485 case 0x8c: /* INTC_MIR_SET */
486 return 0;
488 case 0x90: /* INTC_ISR_SET */
489 return bank->swi;
491 case 0x94: /* INTC_ISR_CLEAR */
492 return 0;
494 case 0x98: /* INTC_PENDING_IRQ */
495 return bank->irqs & ~bank->mask & ~bank->fiq;
497 case 0x9c: /* INTC_PENDING_FIQ */
498 return bank->irqs & ~bank->mask & bank->fiq;
500 /* Per-line registers */
501 case 0x100 ... 0x300: /* INTC_ILR */
502 bank_no = (offset - 0x100) >> 7;
503 if (bank_no > s->nbanks)
504 break;
505 bank = &s->bank[bank_no];
506 line_no = (offset & 0x7f) >> 2;
507 return (bank->priority[line_no] << 2) |
508 ((bank->fiq >> line_no) & 1);
510 OMAP_BAD_REG(addr);
511 return 0;
514 static void omap2_inth_write(void *opaque, target_phys_addr_t addr,
515 uint32_t value)
517 struct omap_intr_handler_s *s = (struct omap_intr_handler_s *) opaque;
518 int offset = addr - s->base;
519 int bank_no, line_no;
520 struct omap_intr_handler_bank_s *bank = 0;
522 if ((offset & 0xf80) == 0x80) {
523 bank_no = (offset & 0x60) >> 5;
524 if (bank_no < s->nbanks) {
525 offset &= ~0x60;
526 bank = &s->bank[bank_no];
530 switch (offset) {
531 case 0x10: /* INTC_SYSCONFIG */
532 s->autoidle &= 4;
533 s->autoidle |= (value & 1) << 2;
534 if (value & 2) /* SOFTRESET */
535 omap_inth_reset(s);
536 return;
538 case 0x48: /* INTC_CONTROL */
539 s->mask = (value & 4) ? 0 : ~0; /* GLOBALMASK */
540 if (value & 2) { /* NEWFIQAGR */
541 qemu_set_irq(s->parent_intr[1], 0);
542 s->new_agr[1] = ~0;
543 omap_inth_update(s, 1);
545 if (value & 1) { /* NEWIRQAGR */
546 qemu_set_irq(s->parent_intr[0], 0);
547 s->new_agr[0] = ~0;
548 omap_inth_update(s, 0);
550 return;
552 case 0x4c: /* INTC_PROTECTION */
553 /* TODO: Make a bitmap (or sizeof(char)map) of access privileges
554 * for every register, see Chapter 3 and 4 for privileged mode. */
555 if (value & 1)
556 fprintf(stderr, "%s: protection mode enable attempt\n",
557 __FUNCTION__);
558 return;
560 case 0x50: /* INTC_IDLE */
561 s->autoidle &= ~3;
562 s->autoidle |= value & 3;
563 return;
565 /* Per-bank registers */
566 case 0x84: /* INTC_MIR */
567 bank->mask = value;
568 omap_inth_update(s, 0);
569 omap_inth_update(s, 1);
570 return;
572 case 0x88: /* INTC_MIR_CLEAR */
573 bank->mask &= ~value;
574 omap_inth_update(s, 0);
575 omap_inth_update(s, 1);
576 return;
578 case 0x8c: /* INTC_MIR_SET */
579 bank->mask |= value;
580 return;
582 case 0x90: /* INTC_ISR_SET */
583 bank->irqs |= bank->swi |= value;
584 omap_inth_update(s, 0);
585 omap_inth_update(s, 1);
586 return;
588 case 0x94: /* INTC_ISR_CLEAR */
589 bank->swi &= ~value;
590 bank->irqs = bank->swi & bank->inputs;
591 return;
593 /* Per-line registers */
594 case 0x100 ... 0x300: /* INTC_ILR */
595 bank_no = (offset - 0x100) >> 7;
596 if (bank_no > s->nbanks)
597 break;
598 bank = &s->bank[bank_no];
599 line_no = (offset & 0x7f) >> 2;
600 bank->priority[line_no] = (value >> 2) & 0x3f;
601 bank->fiq &= ~(1 << line_no);
602 bank->fiq |= (value & 1) << line_no;
603 return;
605 case 0x00: /* INTC_REVISION */
606 case 0x14: /* INTC_SYSSTATUS */
607 case 0x40: /* INTC_SIR_IRQ */
608 case 0x44: /* INTC_SIR_FIQ */
609 case 0x80: /* INTC_ITR */
610 case 0x98: /* INTC_PENDING_IRQ */
611 case 0x9c: /* INTC_PENDING_FIQ */
612 OMAP_RO_REG(addr);
613 return;
615 OMAP_BAD_REG(addr);
618 static CPUReadMemoryFunc *omap2_inth_readfn[] = {
619 omap_badwidth_read32,
620 omap_badwidth_read32,
621 omap2_inth_read,
624 static CPUWriteMemoryFunc *omap2_inth_writefn[] = {
625 omap2_inth_write,
626 omap2_inth_write,
627 omap2_inth_write,
630 struct omap_intr_handler_s *omap2_inth_init(target_phys_addr_t base,
631 int size, int nbanks, qemu_irq **pins,
632 qemu_irq parent_irq, qemu_irq parent_fiq,
633 omap_clk fclk, omap_clk iclk)
635 int iomemtype;
636 struct omap_intr_handler_s *s = (struct omap_intr_handler_s *)
637 qemu_mallocz(sizeof(struct omap_intr_handler_s) +
638 sizeof(struct omap_intr_handler_bank_s) * nbanks);
640 s->parent_intr[0] = parent_irq;
641 s->parent_intr[1] = parent_fiq;
642 s->base = base;
643 s->nbanks = nbanks;
644 s->level_only = 1;
645 s->pins = qemu_allocate_irqs(omap_set_intr_noedge, s, nbanks * 32);
646 if (pins)
647 *pins = s->pins;
649 omap_inth_reset(s);
651 iomemtype = cpu_register_io_memory(0, omap2_inth_readfn,
652 omap2_inth_writefn, s);
653 cpu_register_physical_memory(s->base, size, iomemtype);
655 return s;
658 /* MPU OS timers */
659 struct omap_mpu_timer_s {
660 qemu_irq irq;
661 omap_clk clk;
662 target_phys_addr_t base;
663 uint32_t val;
664 int64_t time;
665 QEMUTimer *timer;
666 int64_t rate;
667 int it_ena;
669 int enable;
670 int ptv;
671 int ar;
672 int st;
673 uint32_t reset_val;
676 static inline uint32_t omap_timer_read(struct omap_mpu_timer_s *timer)
678 uint64_t distance = qemu_get_clock(vm_clock) - timer->time;
680 if (timer->st && timer->enable && timer->rate)
681 return timer->val - muldiv64(distance >> (timer->ptv + 1),
682 timer->rate, ticks_per_sec);
683 else
684 return timer->val;
687 static inline void omap_timer_sync(struct omap_mpu_timer_s *timer)
689 timer->val = omap_timer_read(timer);
690 timer->time = qemu_get_clock(vm_clock);
693 static inline void omap_timer_update(struct omap_mpu_timer_s *timer)
695 int64_t expires;
697 if (timer->enable && timer->st && timer->rate) {
698 timer->val = timer->reset_val; /* Should skip this on clk enable */
699 expires = muldiv64(timer->val << (timer->ptv + 1),
700 ticks_per_sec, timer->rate);
702 /* If timer expiry would be sooner than in about 1 ms and
703 * auto-reload isn't set, then fire immediately. This is a hack
704 * to make systems like PalmOS run in acceptable time. PalmOS
705 * sets the interval to a very low value and polls the status bit
706 * in a busy loop when it wants to sleep just a couple of CPU
707 * ticks. */
708 if (expires > (ticks_per_sec >> 10) || timer->ar)
709 qemu_mod_timer(timer->timer, timer->time + expires);
710 else {
711 timer->val = 0;
712 timer->st = 0;
713 if (timer->it_ena)
714 /* Edge-triggered irq */
715 qemu_irq_pulse(timer->irq);
717 } else
718 qemu_del_timer(timer->timer);
721 static void omap_timer_tick(void *opaque)
723 struct omap_mpu_timer_s *timer = (struct omap_mpu_timer_s *) opaque;
724 omap_timer_sync(timer);
726 if (!timer->ar) {
727 timer->val = 0;
728 timer->st = 0;
731 if (timer->it_ena)
732 /* Edge-triggered irq */
733 qemu_irq_pulse(timer->irq);
734 omap_timer_update(timer);
737 static void omap_timer_clk_update(void *opaque, int line, int on)
739 struct omap_mpu_timer_s *timer = (struct omap_mpu_timer_s *) opaque;
741 omap_timer_sync(timer);
742 timer->rate = on ? omap_clk_getrate(timer->clk) : 0;
743 omap_timer_update(timer);
746 static void omap_timer_clk_setup(struct omap_mpu_timer_s *timer)
748 omap_clk_adduser(timer->clk,
749 qemu_allocate_irqs(omap_timer_clk_update, timer, 1)[0]);
750 timer->rate = omap_clk_getrate(timer->clk);
753 static uint32_t omap_mpu_timer_read(void *opaque, target_phys_addr_t addr)
755 struct omap_mpu_timer_s *s = (struct omap_mpu_timer_s *) opaque;
756 int offset = addr - s->base;
758 switch (offset) {
759 case 0x00: /* CNTL_TIMER */
760 return (s->enable << 5) | (s->ptv << 2) | (s->ar << 1) | s->st;
762 case 0x04: /* LOAD_TIM */
763 break;
765 case 0x08: /* READ_TIM */
766 return omap_timer_read(s);
769 OMAP_BAD_REG(addr);
770 return 0;
773 static void omap_mpu_timer_write(void *opaque, target_phys_addr_t addr,
774 uint32_t value)
776 struct omap_mpu_timer_s *s = (struct omap_mpu_timer_s *) opaque;
777 int offset = addr - s->base;
779 switch (offset) {
780 case 0x00: /* CNTL_TIMER */
781 omap_timer_sync(s);
782 s->enable = (value >> 5) & 1;
783 s->ptv = (value >> 2) & 7;
784 s->ar = (value >> 1) & 1;
785 s->st = value & 1;
786 omap_timer_update(s);
787 return;
789 case 0x04: /* LOAD_TIM */
790 s->reset_val = value;
791 return;
793 case 0x08: /* READ_TIM */
794 OMAP_RO_REG(addr);
795 break;
797 default:
798 OMAP_BAD_REG(addr);
802 static CPUReadMemoryFunc *omap_mpu_timer_readfn[] = {
803 omap_badwidth_read32,
804 omap_badwidth_read32,
805 omap_mpu_timer_read,
808 static CPUWriteMemoryFunc *omap_mpu_timer_writefn[] = {
809 omap_badwidth_write32,
810 omap_badwidth_write32,
811 omap_mpu_timer_write,
814 static void omap_mpu_timer_reset(struct omap_mpu_timer_s *s)
816 qemu_del_timer(s->timer);
817 s->enable = 0;
818 s->reset_val = 31337;
819 s->val = 0;
820 s->ptv = 0;
821 s->ar = 0;
822 s->st = 0;
823 s->it_ena = 1;
826 struct omap_mpu_timer_s *omap_mpu_timer_init(target_phys_addr_t base,
827 qemu_irq irq, omap_clk clk)
829 int iomemtype;
830 struct omap_mpu_timer_s *s = (struct omap_mpu_timer_s *)
831 qemu_mallocz(sizeof(struct omap_mpu_timer_s));
833 s->irq = irq;
834 s->clk = clk;
835 s->base = base;
836 s->timer = qemu_new_timer(vm_clock, omap_timer_tick, s);
837 omap_mpu_timer_reset(s);
838 omap_timer_clk_setup(s);
840 iomemtype = cpu_register_io_memory(0, omap_mpu_timer_readfn,
841 omap_mpu_timer_writefn, s);
842 cpu_register_physical_memory(s->base, 0x100, iomemtype);
844 return s;
847 /* Watchdog timer */
848 struct omap_watchdog_timer_s {
849 struct omap_mpu_timer_s timer;
850 uint8_t last_wr;
851 int mode;
852 int free;
853 int reset;
856 static uint32_t omap_wd_timer_read(void *opaque, target_phys_addr_t addr)
858 struct omap_watchdog_timer_s *s = (struct omap_watchdog_timer_s *) opaque;
859 int offset = addr - s->timer.base;
861 switch (offset) {
862 case 0x00: /* CNTL_TIMER */
863 return (s->timer.ptv << 9) | (s->timer.ar << 8) |
864 (s->timer.st << 7) | (s->free << 1);
866 case 0x04: /* READ_TIMER */
867 return omap_timer_read(&s->timer);
869 case 0x08: /* TIMER_MODE */
870 return s->mode << 15;
873 OMAP_BAD_REG(addr);
874 return 0;
877 static void omap_wd_timer_write(void *opaque, target_phys_addr_t addr,
878 uint32_t value)
880 struct omap_watchdog_timer_s *s = (struct omap_watchdog_timer_s *) opaque;
881 int offset = addr - s->timer.base;
883 switch (offset) {
884 case 0x00: /* CNTL_TIMER */
885 omap_timer_sync(&s->timer);
886 s->timer.ptv = (value >> 9) & 7;
887 s->timer.ar = (value >> 8) & 1;
888 s->timer.st = (value >> 7) & 1;
889 s->free = (value >> 1) & 1;
890 omap_timer_update(&s->timer);
891 break;
893 case 0x04: /* LOAD_TIMER */
894 s->timer.reset_val = value & 0xffff;
895 break;
897 case 0x08: /* TIMER_MODE */
898 if (!s->mode && ((value >> 15) & 1))
899 omap_clk_get(s->timer.clk);
900 s->mode |= (value >> 15) & 1;
901 if (s->last_wr == 0xf5) {
902 if ((value & 0xff) == 0xa0) {
903 if (s->mode) {
904 s->mode = 0;
905 omap_clk_put(s->timer.clk);
907 } else {
908 /* XXX: on T|E hardware somehow this has no effect,
909 * on Zire 71 it works as specified. */
910 s->reset = 1;
911 qemu_system_reset_request();
914 s->last_wr = value & 0xff;
915 break;
917 default:
918 OMAP_BAD_REG(addr);
922 static CPUReadMemoryFunc *omap_wd_timer_readfn[] = {
923 omap_badwidth_read16,
924 omap_wd_timer_read,
925 omap_badwidth_read16,
928 static CPUWriteMemoryFunc *omap_wd_timer_writefn[] = {
929 omap_badwidth_write16,
930 omap_wd_timer_write,
931 omap_badwidth_write16,
934 static void omap_wd_timer_reset(struct omap_watchdog_timer_s *s)
936 qemu_del_timer(s->timer.timer);
937 if (!s->mode)
938 omap_clk_get(s->timer.clk);
939 s->mode = 1;
940 s->free = 1;
941 s->reset = 0;
942 s->timer.enable = 1;
943 s->timer.it_ena = 1;
944 s->timer.reset_val = 0xffff;
945 s->timer.val = 0;
946 s->timer.st = 0;
947 s->timer.ptv = 0;
948 s->timer.ar = 0;
949 omap_timer_update(&s->timer);
952 struct omap_watchdog_timer_s *omap_wd_timer_init(target_phys_addr_t base,
953 qemu_irq irq, omap_clk clk)
955 int iomemtype;
956 struct omap_watchdog_timer_s *s = (struct omap_watchdog_timer_s *)
957 qemu_mallocz(sizeof(struct omap_watchdog_timer_s));
959 s->timer.irq = irq;
960 s->timer.clk = clk;
961 s->timer.base = base;
962 s->timer.timer = qemu_new_timer(vm_clock, omap_timer_tick, &s->timer);
963 omap_wd_timer_reset(s);
964 omap_timer_clk_setup(&s->timer);
966 iomemtype = cpu_register_io_memory(0, omap_wd_timer_readfn,
967 omap_wd_timer_writefn, s);
968 cpu_register_physical_memory(s->timer.base, 0x100, iomemtype);
970 return s;
973 /* 32-kHz timer */
974 struct omap_32khz_timer_s {
975 struct omap_mpu_timer_s timer;
978 static uint32_t omap_os_timer_read(void *opaque, target_phys_addr_t addr)
980 struct omap_32khz_timer_s *s = (struct omap_32khz_timer_s *) opaque;
981 int offset = addr & OMAP_MPUI_REG_MASK;
983 switch (offset) {
984 case 0x00: /* TVR */
985 return s->timer.reset_val;
987 case 0x04: /* TCR */
988 return omap_timer_read(&s->timer);
990 case 0x08: /* CR */
991 return (s->timer.ar << 3) | (s->timer.it_ena << 2) | s->timer.st;
993 default:
994 break;
996 OMAP_BAD_REG(addr);
997 return 0;
1000 static void omap_os_timer_write(void *opaque, target_phys_addr_t addr,
1001 uint32_t value)
1003 struct omap_32khz_timer_s *s = (struct omap_32khz_timer_s *) opaque;
1004 int offset = addr & OMAP_MPUI_REG_MASK;
1006 switch (offset) {
1007 case 0x00: /* TVR */
1008 s->timer.reset_val = value & 0x00ffffff;
1009 break;
1011 case 0x04: /* TCR */
1012 OMAP_RO_REG(addr);
1013 break;
1015 case 0x08: /* CR */
1016 s->timer.ar = (value >> 3) & 1;
1017 s->timer.it_ena = (value >> 2) & 1;
1018 if (s->timer.st != (value & 1) || (value & 2)) {
1019 omap_timer_sync(&s->timer);
1020 s->timer.enable = value & 1;
1021 s->timer.st = value & 1;
1022 omap_timer_update(&s->timer);
1024 break;
1026 default:
1027 OMAP_BAD_REG(addr);
1031 static CPUReadMemoryFunc *omap_os_timer_readfn[] = {
1032 omap_badwidth_read32,
1033 omap_badwidth_read32,
1034 omap_os_timer_read,
1037 static CPUWriteMemoryFunc *omap_os_timer_writefn[] = {
1038 omap_badwidth_write32,
1039 omap_badwidth_write32,
1040 omap_os_timer_write,
1043 static void omap_os_timer_reset(struct omap_32khz_timer_s *s)
1045 qemu_del_timer(s->timer.timer);
1046 s->timer.enable = 0;
1047 s->timer.it_ena = 0;
1048 s->timer.reset_val = 0x00ffffff;
1049 s->timer.val = 0;
1050 s->timer.st = 0;
1051 s->timer.ptv = 0;
1052 s->timer.ar = 1;
1055 struct omap_32khz_timer_s *omap_os_timer_init(target_phys_addr_t base,
1056 qemu_irq irq, omap_clk clk)
1058 int iomemtype;
1059 struct omap_32khz_timer_s *s = (struct omap_32khz_timer_s *)
1060 qemu_mallocz(sizeof(struct omap_32khz_timer_s));
1062 s->timer.irq = irq;
1063 s->timer.clk = clk;
1064 s->timer.base = base;
1065 s->timer.timer = qemu_new_timer(vm_clock, omap_timer_tick, &s->timer);
1066 omap_os_timer_reset(s);
1067 omap_timer_clk_setup(&s->timer);
1069 iomemtype = cpu_register_io_memory(0, omap_os_timer_readfn,
1070 omap_os_timer_writefn, s);
1071 cpu_register_physical_memory(s->timer.base, 0x800, iomemtype);
1073 return s;
1076 /* Ultra Low-Power Device Module */
1077 static uint32_t omap_ulpd_pm_read(void *opaque, target_phys_addr_t addr)
1079 struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1080 int offset = addr - s->ulpd_pm_base;
1081 uint16_t ret;
1083 switch (offset) {
1084 case 0x14: /* IT_STATUS */
1085 ret = s->ulpd_pm_regs[offset >> 2];
1086 s->ulpd_pm_regs[offset >> 2] = 0;
1087 qemu_irq_lower(s->irq[1][OMAP_INT_GAUGE_32K]);
1088 return ret;
1090 case 0x18: /* Reserved */
1091 case 0x1c: /* Reserved */
1092 case 0x20: /* Reserved */
1093 case 0x28: /* Reserved */
1094 case 0x2c: /* Reserved */
1095 OMAP_BAD_REG(addr);
1096 case 0x00: /* COUNTER_32_LSB */
1097 case 0x04: /* COUNTER_32_MSB */
1098 case 0x08: /* COUNTER_HIGH_FREQ_LSB */
1099 case 0x0c: /* COUNTER_HIGH_FREQ_MSB */
1100 case 0x10: /* GAUGING_CTRL */
1101 case 0x24: /* SETUP_ANALOG_CELL3_ULPD1 */
1102 case 0x30: /* CLOCK_CTRL */
1103 case 0x34: /* SOFT_REQ */
1104 case 0x38: /* COUNTER_32_FIQ */
1105 case 0x3c: /* DPLL_CTRL */
1106 case 0x40: /* STATUS_REQ */
1107 /* XXX: check clk::usecount state for every clock */
1108 case 0x48: /* LOCL_TIME */
1109 case 0x4c: /* APLL_CTRL */
1110 case 0x50: /* POWER_CTRL */
1111 return s->ulpd_pm_regs[offset >> 2];
1114 OMAP_BAD_REG(addr);
1115 return 0;
1118 static inline void omap_ulpd_clk_update(struct omap_mpu_state_s *s,
1119 uint16_t diff, uint16_t value)
1121 if (diff & (1 << 4)) /* USB_MCLK_EN */
1122 omap_clk_onoff(omap_findclk(s, "usb_clk0"), (value >> 4) & 1);
1123 if (diff & (1 << 5)) /* DIS_USB_PVCI_CLK */
1124 omap_clk_onoff(omap_findclk(s, "usb_w2fc_ck"), (~value >> 5) & 1);
1127 static inline void omap_ulpd_req_update(struct omap_mpu_state_s *s,
1128 uint16_t diff, uint16_t value)
1130 if (diff & (1 << 0)) /* SOFT_DPLL_REQ */
1131 omap_clk_canidle(omap_findclk(s, "dpll4"), (~value >> 0) & 1);
1132 if (diff & (1 << 1)) /* SOFT_COM_REQ */
1133 omap_clk_canidle(omap_findclk(s, "com_mclk_out"), (~value >> 1) & 1);
1134 if (diff & (1 << 2)) /* SOFT_SDW_REQ */
1135 omap_clk_canidle(omap_findclk(s, "bt_mclk_out"), (~value >> 2) & 1);
1136 if (diff & (1 << 3)) /* SOFT_USB_REQ */
1137 omap_clk_canidle(omap_findclk(s, "usb_clk0"), (~value >> 3) & 1);
1140 static void omap_ulpd_pm_write(void *opaque, target_phys_addr_t addr,
1141 uint32_t value)
1143 struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1144 int offset = addr - s->ulpd_pm_base;
1145 int64_t now, ticks;
1146 int div, mult;
1147 static const int bypass_div[4] = { 1, 2, 4, 4 };
1148 uint16_t diff;
1150 switch (offset) {
1151 case 0x00: /* COUNTER_32_LSB */
1152 case 0x04: /* COUNTER_32_MSB */
1153 case 0x08: /* COUNTER_HIGH_FREQ_LSB */
1154 case 0x0c: /* COUNTER_HIGH_FREQ_MSB */
1155 case 0x14: /* IT_STATUS */
1156 case 0x40: /* STATUS_REQ */
1157 OMAP_RO_REG(addr);
1158 break;
1160 case 0x10: /* GAUGING_CTRL */
1161 /* Bits 0 and 1 seem to be confused in the OMAP 310 TRM */
1162 if ((s->ulpd_pm_regs[offset >> 2] ^ value) & 1) {
1163 now = qemu_get_clock(vm_clock);
1165 if (value & 1)
1166 s->ulpd_gauge_start = now;
1167 else {
1168 now -= s->ulpd_gauge_start;
1170 /* 32-kHz ticks */
1171 ticks = muldiv64(now, 32768, ticks_per_sec);
1172 s->ulpd_pm_regs[0x00 >> 2] = (ticks >> 0) & 0xffff;
1173 s->ulpd_pm_regs[0x04 >> 2] = (ticks >> 16) & 0xffff;
1174 if (ticks >> 32) /* OVERFLOW_32K */
1175 s->ulpd_pm_regs[0x14 >> 2] |= 1 << 2;
1177 /* High frequency ticks */
1178 ticks = muldiv64(now, 12000000, ticks_per_sec);
1179 s->ulpd_pm_regs[0x08 >> 2] = (ticks >> 0) & 0xffff;
1180 s->ulpd_pm_regs[0x0c >> 2] = (ticks >> 16) & 0xffff;
1181 if (ticks >> 32) /* OVERFLOW_HI_FREQ */
1182 s->ulpd_pm_regs[0x14 >> 2] |= 1 << 1;
1184 s->ulpd_pm_regs[0x14 >> 2] |= 1 << 0; /* IT_GAUGING */
1185 qemu_irq_raise(s->irq[1][OMAP_INT_GAUGE_32K]);
1188 s->ulpd_pm_regs[offset >> 2] = value;
1189 break;
1191 case 0x18: /* Reserved */
1192 case 0x1c: /* Reserved */
1193 case 0x20: /* Reserved */
1194 case 0x28: /* Reserved */
1195 case 0x2c: /* Reserved */
1196 OMAP_BAD_REG(addr);
1197 case 0x24: /* SETUP_ANALOG_CELL3_ULPD1 */
1198 case 0x38: /* COUNTER_32_FIQ */
1199 case 0x48: /* LOCL_TIME */
1200 case 0x50: /* POWER_CTRL */
1201 s->ulpd_pm_regs[offset >> 2] = value;
1202 break;
1204 case 0x30: /* CLOCK_CTRL */
1205 diff = s->ulpd_pm_regs[offset >> 2] ^ value;
1206 s->ulpd_pm_regs[offset >> 2] = value & 0x3f;
1207 omap_ulpd_clk_update(s, diff, value);
1208 break;
1210 case 0x34: /* SOFT_REQ */
1211 diff = s->ulpd_pm_regs[offset >> 2] ^ value;
1212 s->ulpd_pm_regs[offset >> 2] = value & 0x1f;
1213 omap_ulpd_req_update(s, diff, value);
1214 break;
1216 case 0x3c: /* DPLL_CTRL */
1217 /* XXX: OMAP310 TRM claims bit 3 is PLL_ENABLE, and bit 4 is
1218 * omitted altogether, probably a typo. */
1219 /* This register has identical semantics with DPLL(1:3) control
1220 * registers, see omap_dpll_write() */
1221 diff = s->ulpd_pm_regs[offset >> 2] & value;
1222 s->ulpd_pm_regs[offset >> 2] = value & 0x2fff;
1223 if (diff & (0x3ff << 2)) {
1224 if (value & (1 << 4)) { /* PLL_ENABLE */
1225 div = ((value >> 5) & 3) + 1; /* PLL_DIV */
1226 mult = MIN((value >> 7) & 0x1f, 1); /* PLL_MULT */
1227 } else {
1228 div = bypass_div[((value >> 2) & 3)]; /* BYPASS_DIV */
1229 mult = 1;
1231 omap_clk_setrate(omap_findclk(s, "dpll4"), div, mult);
1234 /* Enter the desired mode. */
1235 s->ulpd_pm_regs[offset >> 2] =
1236 (s->ulpd_pm_regs[offset >> 2] & 0xfffe) |
1237 ((s->ulpd_pm_regs[offset >> 2] >> 4) & 1);
1239 /* Act as if the lock is restored. */
1240 s->ulpd_pm_regs[offset >> 2] |= 2;
1241 break;
1243 case 0x4c: /* APLL_CTRL */
1244 diff = s->ulpd_pm_regs[offset >> 2] & value;
1245 s->ulpd_pm_regs[offset >> 2] = value & 0xf;
1246 if (diff & (1 << 0)) /* APLL_NDPLL_SWITCH */
1247 omap_clk_reparent(omap_findclk(s, "ck_48m"), omap_findclk(s,
1248 (value & (1 << 0)) ? "apll" : "dpll4"));
1249 break;
1251 default:
1252 OMAP_BAD_REG(addr);
1256 static CPUReadMemoryFunc *omap_ulpd_pm_readfn[] = {
1257 omap_badwidth_read16,
1258 omap_ulpd_pm_read,
1259 omap_badwidth_read16,
1262 static CPUWriteMemoryFunc *omap_ulpd_pm_writefn[] = {
1263 omap_badwidth_write16,
1264 omap_ulpd_pm_write,
1265 omap_badwidth_write16,
1268 static void omap_ulpd_pm_reset(struct omap_mpu_state_s *mpu)
1270 mpu->ulpd_pm_regs[0x00 >> 2] = 0x0001;
1271 mpu->ulpd_pm_regs[0x04 >> 2] = 0x0000;
1272 mpu->ulpd_pm_regs[0x08 >> 2] = 0x0001;
1273 mpu->ulpd_pm_regs[0x0c >> 2] = 0x0000;
1274 mpu->ulpd_pm_regs[0x10 >> 2] = 0x0000;
1275 mpu->ulpd_pm_regs[0x18 >> 2] = 0x01;
1276 mpu->ulpd_pm_regs[0x1c >> 2] = 0x01;
1277 mpu->ulpd_pm_regs[0x20 >> 2] = 0x01;
1278 mpu->ulpd_pm_regs[0x24 >> 2] = 0x03ff;
1279 mpu->ulpd_pm_regs[0x28 >> 2] = 0x01;
1280 mpu->ulpd_pm_regs[0x2c >> 2] = 0x01;
1281 omap_ulpd_clk_update(mpu, mpu->ulpd_pm_regs[0x30 >> 2], 0x0000);
1282 mpu->ulpd_pm_regs[0x30 >> 2] = 0x0000;
1283 omap_ulpd_req_update(mpu, mpu->ulpd_pm_regs[0x34 >> 2], 0x0000);
1284 mpu->ulpd_pm_regs[0x34 >> 2] = 0x0000;
1285 mpu->ulpd_pm_regs[0x38 >> 2] = 0x0001;
1286 mpu->ulpd_pm_regs[0x3c >> 2] = 0x2211;
1287 mpu->ulpd_pm_regs[0x40 >> 2] = 0x0000; /* FIXME: dump a real STATUS_REQ */
1288 mpu->ulpd_pm_regs[0x48 >> 2] = 0x960;
1289 mpu->ulpd_pm_regs[0x4c >> 2] = 0x08;
1290 mpu->ulpd_pm_regs[0x50 >> 2] = 0x08;
1291 omap_clk_setrate(omap_findclk(mpu, "dpll4"), 1, 4);
1292 omap_clk_reparent(omap_findclk(mpu, "ck_48m"), omap_findclk(mpu, "dpll4"));
1295 static void omap_ulpd_pm_init(target_phys_addr_t base,
1296 struct omap_mpu_state_s *mpu)
1298 int iomemtype = cpu_register_io_memory(0, omap_ulpd_pm_readfn,
1299 omap_ulpd_pm_writefn, mpu);
1301 mpu->ulpd_pm_base = base;
1302 cpu_register_physical_memory(mpu->ulpd_pm_base, 0x800, iomemtype);
1303 omap_ulpd_pm_reset(mpu);
1306 /* OMAP Pin Configuration */
1307 static uint32_t omap_pin_cfg_read(void *opaque, target_phys_addr_t addr)
1309 struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1310 int offset = addr - s->pin_cfg_base;
1312 switch (offset) {
1313 case 0x00: /* FUNC_MUX_CTRL_0 */
1314 case 0x04: /* FUNC_MUX_CTRL_1 */
1315 case 0x08: /* FUNC_MUX_CTRL_2 */
1316 return s->func_mux_ctrl[offset >> 2];
1318 case 0x0c: /* COMP_MODE_CTRL_0 */
1319 return s->comp_mode_ctrl[0];
1321 case 0x10: /* FUNC_MUX_CTRL_3 */
1322 case 0x14: /* FUNC_MUX_CTRL_4 */
1323 case 0x18: /* FUNC_MUX_CTRL_5 */
1324 case 0x1c: /* FUNC_MUX_CTRL_6 */
1325 case 0x20: /* FUNC_MUX_CTRL_7 */
1326 case 0x24: /* FUNC_MUX_CTRL_8 */
1327 case 0x28: /* FUNC_MUX_CTRL_9 */
1328 case 0x2c: /* FUNC_MUX_CTRL_A */
1329 case 0x30: /* FUNC_MUX_CTRL_B */
1330 case 0x34: /* FUNC_MUX_CTRL_C */
1331 case 0x38: /* FUNC_MUX_CTRL_D */
1332 return s->func_mux_ctrl[(offset >> 2) - 1];
1334 case 0x40: /* PULL_DWN_CTRL_0 */
1335 case 0x44: /* PULL_DWN_CTRL_1 */
1336 case 0x48: /* PULL_DWN_CTRL_2 */
1337 case 0x4c: /* PULL_DWN_CTRL_3 */
1338 return s->pull_dwn_ctrl[(offset & 0xf) >> 2];
1340 case 0x50: /* GATE_INH_CTRL_0 */
1341 return s->gate_inh_ctrl[0];
1343 case 0x60: /* VOLTAGE_CTRL_0 */
1344 return s->voltage_ctrl[0];
1346 case 0x70: /* TEST_DBG_CTRL_0 */
1347 return s->test_dbg_ctrl[0];
1349 case 0x80: /* MOD_CONF_CTRL_0 */
1350 return s->mod_conf_ctrl[0];
1353 OMAP_BAD_REG(addr);
1354 return 0;
1357 static inline void omap_pin_funcmux0_update(struct omap_mpu_state_s *s,
1358 uint32_t diff, uint32_t value)
1360 if (s->compat1509) {
1361 if (diff & (1 << 9)) /* BLUETOOTH */
1362 omap_clk_onoff(omap_findclk(s, "bt_mclk_out"),
1363 (~value >> 9) & 1);
1364 if (diff & (1 << 7)) /* USB.CLKO */
1365 omap_clk_onoff(omap_findclk(s, "usb.clko"),
1366 (value >> 7) & 1);
1370 static inline void omap_pin_funcmux1_update(struct omap_mpu_state_s *s,
1371 uint32_t diff, uint32_t value)
1373 if (s->compat1509) {
1374 if (diff & (1 << 31)) /* MCBSP3_CLK_HIZ_DI */
1375 omap_clk_onoff(omap_findclk(s, "mcbsp3.clkx"),
1376 (value >> 31) & 1);
1377 if (diff & (1 << 1)) /* CLK32K */
1378 omap_clk_onoff(omap_findclk(s, "clk32k_out"),
1379 (~value >> 1) & 1);
1383 static inline void omap_pin_modconf1_update(struct omap_mpu_state_s *s,
1384 uint32_t diff, uint32_t value)
1386 if (diff & (1 << 31)) /* CONF_MOD_UART3_CLK_MODE_R */
1387 omap_clk_reparent(omap_findclk(s, "uart3_ck"),
1388 omap_findclk(s, ((value >> 31) & 1) ?
1389 "ck_48m" : "armper_ck"));
1390 if (diff & (1 << 30)) /* CONF_MOD_UART2_CLK_MODE_R */
1391 omap_clk_reparent(omap_findclk(s, "uart2_ck"),
1392 omap_findclk(s, ((value >> 30) & 1) ?
1393 "ck_48m" : "armper_ck"));
1394 if (diff & (1 << 29)) /* CONF_MOD_UART1_CLK_MODE_R */
1395 omap_clk_reparent(omap_findclk(s, "uart1_ck"),
1396 omap_findclk(s, ((value >> 29) & 1) ?
1397 "ck_48m" : "armper_ck"));
1398 if (diff & (1 << 23)) /* CONF_MOD_MMC_SD_CLK_REQ_R */
1399 omap_clk_reparent(omap_findclk(s, "mmc_ck"),
1400 omap_findclk(s, ((value >> 23) & 1) ?
1401 "ck_48m" : "armper_ck"));
1402 if (diff & (1 << 12)) /* CONF_MOD_COM_MCLK_12_48_S */
1403 omap_clk_reparent(omap_findclk(s, "com_mclk_out"),
1404 omap_findclk(s, ((value >> 12) & 1) ?
1405 "ck_48m" : "armper_ck"));
1406 if (diff & (1 << 9)) /* CONF_MOD_USB_HOST_HHC_UHO */
1407 omap_clk_onoff(omap_findclk(s, "usb_hhc_ck"), (value >> 9) & 1);
1410 static void omap_pin_cfg_write(void *opaque, target_phys_addr_t addr,
1411 uint32_t value)
1413 struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1414 int offset = addr - s->pin_cfg_base;
1415 uint32_t diff;
1417 switch (offset) {
1418 case 0x00: /* FUNC_MUX_CTRL_0 */
1419 diff = s->func_mux_ctrl[offset >> 2] ^ value;
1420 s->func_mux_ctrl[offset >> 2] = value;
1421 omap_pin_funcmux0_update(s, diff, value);
1422 return;
1424 case 0x04: /* FUNC_MUX_CTRL_1 */
1425 diff = s->func_mux_ctrl[offset >> 2] ^ value;
1426 s->func_mux_ctrl[offset >> 2] = value;
1427 omap_pin_funcmux1_update(s, diff, value);
1428 return;
1430 case 0x08: /* FUNC_MUX_CTRL_2 */
1431 s->func_mux_ctrl[offset >> 2] = value;
1432 return;
1434 case 0x0c: /* COMP_MODE_CTRL_0 */
1435 s->comp_mode_ctrl[0] = value;
1436 s->compat1509 = (value != 0x0000eaef);
1437 omap_pin_funcmux0_update(s, ~0, s->func_mux_ctrl[0]);
1438 omap_pin_funcmux1_update(s, ~0, s->func_mux_ctrl[1]);
1439 return;
1441 case 0x10: /* FUNC_MUX_CTRL_3 */
1442 case 0x14: /* FUNC_MUX_CTRL_4 */
1443 case 0x18: /* FUNC_MUX_CTRL_5 */
1444 case 0x1c: /* FUNC_MUX_CTRL_6 */
1445 case 0x20: /* FUNC_MUX_CTRL_7 */
1446 case 0x24: /* FUNC_MUX_CTRL_8 */
1447 case 0x28: /* FUNC_MUX_CTRL_9 */
1448 case 0x2c: /* FUNC_MUX_CTRL_A */
1449 case 0x30: /* FUNC_MUX_CTRL_B */
1450 case 0x34: /* FUNC_MUX_CTRL_C */
1451 case 0x38: /* FUNC_MUX_CTRL_D */
1452 s->func_mux_ctrl[(offset >> 2) - 1] = value;
1453 return;
1455 case 0x40: /* PULL_DWN_CTRL_0 */
1456 case 0x44: /* PULL_DWN_CTRL_1 */
1457 case 0x48: /* PULL_DWN_CTRL_2 */
1458 case 0x4c: /* PULL_DWN_CTRL_3 */
1459 s->pull_dwn_ctrl[(offset & 0xf) >> 2] = value;
1460 return;
1462 case 0x50: /* GATE_INH_CTRL_0 */
1463 s->gate_inh_ctrl[0] = value;
1464 return;
1466 case 0x60: /* VOLTAGE_CTRL_0 */
1467 s->voltage_ctrl[0] = value;
1468 return;
1470 case 0x70: /* TEST_DBG_CTRL_0 */
1471 s->test_dbg_ctrl[0] = value;
1472 return;
1474 case 0x80: /* MOD_CONF_CTRL_0 */
1475 diff = s->mod_conf_ctrl[0] ^ value;
1476 s->mod_conf_ctrl[0] = value;
1477 omap_pin_modconf1_update(s, diff, value);
1478 return;
1480 default:
1481 OMAP_BAD_REG(addr);
1485 static CPUReadMemoryFunc *omap_pin_cfg_readfn[] = {
1486 omap_badwidth_read32,
1487 omap_badwidth_read32,
1488 omap_pin_cfg_read,
1491 static CPUWriteMemoryFunc *omap_pin_cfg_writefn[] = {
1492 omap_badwidth_write32,
1493 omap_badwidth_write32,
1494 omap_pin_cfg_write,
1497 static void omap_pin_cfg_reset(struct omap_mpu_state_s *mpu)
1499 /* Start in Compatibility Mode. */
1500 mpu->compat1509 = 1;
1501 omap_pin_funcmux0_update(mpu, mpu->func_mux_ctrl[0], 0);
1502 omap_pin_funcmux1_update(mpu, mpu->func_mux_ctrl[1], 0);
1503 omap_pin_modconf1_update(mpu, mpu->mod_conf_ctrl[0], 0);
1504 memset(mpu->func_mux_ctrl, 0, sizeof(mpu->func_mux_ctrl));
1505 memset(mpu->comp_mode_ctrl, 0, sizeof(mpu->comp_mode_ctrl));
1506 memset(mpu->pull_dwn_ctrl, 0, sizeof(mpu->pull_dwn_ctrl));
1507 memset(mpu->gate_inh_ctrl, 0, sizeof(mpu->gate_inh_ctrl));
1508 memset(mpu->voltage_ctrl, 0, sizeof(mpu->voltage_ctrl));
1509 memset(mpu->test_dbg_ctrl, 0, sizeof(mpu->test_dbg_ctrl));
1510 memset(mpu->mod_conf_ctrl, 0, sizeof(mpu->mod_conf_ctrl));
1513 static void omap_pin_cfg_init(target_phys_addr_t base,
1514 struct omap_mpu_state_s *mpu)
1516 int iomemtype = cpu_register_io_memory(0, omap_pin_cfg_readfn,
1517 omap_pin_cfg_writefn, mpu);
1519 mpu->pin_cfg_base = base;
1520 cpu_register_physical_memory(mpu->pin_cfg_base, 0x800, iomemtype);
1521 omap_pin_cfg_reset(mpu);
1524 /* Device Identification, Die Identification */
1525 static uint32_t omap_id_read(void *opaque, target_phys_addr_t addr)
1527 struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1529 switch (addr) {
1530 case 0xfffe1800: /* DIE_ID_LSB */
1531 return 0xc9581f0e;
1532 case 0xfffe1804: /* DIE_ID_MSB */
1533 return 0xa8858bfa;
1535 case 0xfffe2000: /* PRODUCT_ID_LSB */
1536 return 0x00aaaafc;
1537 case 0xfffe2004: /* PRODUCT_ID_MSB */
1538 return 0xcafeb574;
1540 case 0xfffed400: /* JTAG_ID_LSB */
1541 switch (s->mpu_model) {
1542 case omap310:
1543 return 0x03310315;
1544 case omap1510:
1545 return 0x03310115;
1546 default:
1547 cpu_abort(cpu_single_env, "%s: bad mpu model\n", __FUNCTION__);
1549 break;
1551 case 0xfffed404: /* JTAG_ID_MSB */
1552 switch (s->mpu_model) {
1553 case omap310:
1554 return 0xfb57402f;
1555 case omap1510:
1556 return 0xfb47002f;
1557 default:
1558 cpu_abort(cpu_single_env, "%s: bad mpu model\n", __FUNCTION__);
1560 break;
1563 OMAP_BAD_REG(addr);
1564 return 0;
1567 static void omap_id_write(void *opaque, target_phys_addr_t addr,
1568 uint32_t value)
1570 OMAP_BAD_REG(addr);
1573 static CPUReadMemoryFunc *omap_id_readfn[] = {
1574 omap_badwidth_read32,
1575 omap_badwidth_read32,
1576 omap_id_read,
1579 static CPUWriteMemoryFunc *omap_id_writefn[] = {
1580 omap_badwidth_write32,
1581 omap_badwidth_write32,
1582 omap_id_write,
1585 static void omap_id_init(struct omap_mpu_state_s *mpu)
1587 int iomemtype = cpu_register_io_memory(0, omap_id_readfn,
1588 omap_id_writefn, mpu);
1589 cpu_register_physical_memory(0xfffe1800, 0x800, iomemtype);
1590 cpu_register_physical_memory(0xfffed400, 0x100, iomemtype);
1591 if (!cpu_is_omap15xx(mpu))
1592 cpu_register_physical_memory(0xfffe2000, 0x800, iomemtype);
1595 /* MPUI Control (Dummy) */
1596 static uint32_t omap_mpui_read(void *opaque, target_phys_addr_t addr)
1598 struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1599 int offset = addr - s->mpui_base;
1601 switch (offset) {
1602 case 0x00: /* CTRL */
1603 return s->mpui_ctrl;
1604 case 0x04: /* DEBUG_ADDR */
1605 return 0x01ffffff;
1606 case 0x08: /* DEBUG_DATA */
1607 return 0xffffffff;
1608 case 0x0c: /* DEBUG_FLAG */
1609 return 0x00000800;
1610 case 0x10: /* STATUS */
1611 return 0x00000000;
1613 /* Not in OMAP310 */
1614 case 0x14: /* DSP_STATUS */
1615 case 0x18: /* DSP_BOOT_CONFIG */
1616 return 0x00000000;
1617 case 0x1c: /* DSP_MPUI_CONFIG */
1618 return 0x0000ffff;
1621 OMAP_BAD_REG(addr);
1622 return 0;
1625 static void omap_mpui_write(void *opaque, target_phys_addr_t addr,
1626 uint32_t value)
1628 struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1629 int offset = addr - s->mpui_base;
1631 switch (offset) {
1632 case 0x00: /* CTRL */
1633 s->mpui_ctrl = value & 0x007fffff;
1634 break;
1636 case 0x04: /* DEBUG_ADDR */
1637 case 0x08: /* DEBUG_DATA */
1638 case 0x0c: /* DEBUG_FLAG */
1639 case 0x10: /* STATUS */
1640 /* Not in OMAP310 */
1641 case 0x14: /* DSP_STATUS */
1642 OMAP_RO_REG(addr);
1643 case 0x18: /* DSP_BOOT_CONFIG */
1644 case 0x1c: /* DSP_MPUI_CONFIG */
1645 break;
1647 default:
1648 OMAP_BAD_REG(addr);
1652 static CPUReadMemoryFunc *omap_mpui_readfn[] = {
1653 omap_badwidth_read32,
1654 omap_badwidth_read32,
1655 omap_mpui_read,
1658 static CPUWriteMemoryFunc *omap_mpui_writefn[] = {
1659 omap_badwidth_write32,
1660 omap_badwidth_write32,
1661 omap_mpui_write,
1664 static void omap_mpui_reset(struct omap_mpu_state_s *s)
1666 s->mpui_ctrl = 0x0003ff1b;
1669 static void omap_mpui_init(target_phys_addr_t base,
1670 struct omap_mpu_state_s *mpu)
1672 int iomemtype = cpu_register_io_memory(0, omap_mpui_readfn,
1673 omap_mpui_writefn, mpu);
1675 mpu->mpui_base = base;
1676 cpu_register_physical_memory(mpu->mpui_base, 0x100, iomemtype);
1678 omap_mpui_reset(mpu);
1681 /* TIPB Bridges */
1682 struct omap_tipb_bridge_s {
1683 target_phys_addr_t base;
1684 qemu_irq abort;
1686 int width_intr;
1687 uint16_t control;
1688 uint16_t alloc;
1689 uint16_t buffer;
1690 uint16_t enh_control;
1693 static uint32_t omap_tipb_bridge_read(void *opaque, target_phys_addr_t addr)
1695 struct omap_tipb_bridge_s *s = (struct omap_tipb_bridge_s *) opaque;
1696 int offset = addr - s->base;
1698 switch (offset) {
1699 case 0x00: /* TIPB_CNTL */
1700 return s->control;
1701 case 0x04: /* TIPB_BUS_ALLOC */
1702 return s->alloc;
1703 case 0x08: /* MPU_TIPB_CNTL */
1704 return s->buffer;
1705 case 0x0c: /* ENHANCED_TIPB_CNTL */
1706 return s->enh_control;
1707 case 0x10: /* ADDRESS_DBG */
1708 case 0x14: /* DATA_DEBUG_LOW */
1709 case 0x18: /* DATA_DEBUG_HIGH */
1710 return 0xffff;
1711 case 0x1c: /* DEBUG_CNTR_SIG */
1712 return 0x00f8;
1715 OMAP_BAD_REG(addr);
1716 return 0;
1719 static void omap_tipb_bridge_write(void *opaque, target_phys_addr_t addr,
1720 uint32_t value)
1722 struct omap_tipb_bridge_s *s = (struct omap_tipb_bridge_s *) opaque;
1723 int offset = addr - s->base;
1725 switch (offset) {
1726 case 0x00: /* TIPB_CNTL */
1727 s->control = value & 0xffff;
1728 break;
1730 case 0x04: /* TIPB_BUS_ALLOC */
1731 s->alloc = value & 0x003f;
1732 break;
1734 case 0x08: /* MPU_TIPB_CNTL */
1735 s->buffer = value & 0x0003;
1736 break;
1738 case 0x0c: /* ENHANCED_TIPB_CNTL */
1739 s->width_intr = !(value & 2);
1740 s->enh_control = value & 0x000f;
1741 break;
1743 case 0x10: /* ADDRESS_DBG */
1744 case 0x14: /* DATA_DEBUG_LOW */
1745 case 0x18: /* DATA_DEBUG_HIGH */
1746 case 0x1c: /* DEBUG_CNTR_SIG */
1747 OMAP_RO_REG(addr);
1748 break;
1750 default:
1751 OMAP_BAD_REG(addr);
1755 static CPUReadMemoryFunc *omap_tipb_bridge_readfn[] = {
1756 omap_badwidth_read16,
1757 omap_tipb_bridge_read,
1758 omap_tipb_bridge_read,
1761 static CPUWriteMemoryFunc *omap_tipb_bridge_writefn[] = {
1762 omap_badwidth_write16,
1763 omap_tipb_bridge_write,
1764 omap_tipb_bridge_write,
1767 static void omap_tipb_bridge_reset(struct omap_tipb_bridge_s *s)
1769 s->control = 0xffff;
1770 s->alloc = 0x0009;
1771 s->buffer = 0x0000;
1772 s->enh_control = 0x000f;
1775 struct omap_tipb_bridge_s *omap_tipb_bridge_init(target_phys_addr_t base,
1776 qemu_irq abort_irq, omap_clk clk)
1778 int iomemtype;
1779 struct omap_tipb_bridge_s *s = (struct omap_tipb_bridge_s *)
1780 qemu_mallocz(sizeof(struct omap_tipb_bridge_s));
1782 s->abort = abort_irq;
1783 s->base = base;
1784 omap_tipb_bridge_reset(s);
1786 iomemtype = cpu_register_io_memory(0, omap_tipb_bridge_readfn,
1787 omap_tipb_bridge_writefn, s);
1788 cpu_register_physical_memory(s->base, 0x100, iomemtype);
1790 return s;
1793 /* Dummy Traffic Controller's Memory Interface */
1794 static uint32_t omap_tcmi_read(void *opaque, target_phys_addr_t addr)
1796 struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1797 int offset = addr - s->tcmi_base;
1798 uint32_t ret;
1800 switch (offset) {
1801 case 0x00: /* IMIF_PRIO */
1802 case 0x04: /* EMIFS_PRIO */
1803 case 0x08: /* EMIFF_PRIO */
1804 case 0x0c: /* EMIFS_CONFIG */
1805 case 0x10: /* EMIFS_CS0_CONFIG */
1806 case 0x14: /* EMIFS_CS1_CONFIG */
1807 case 0x18: /* EMIFS_CS2_CONFIG */
1808 case 0x1c: /* EMIFS_CS3_CONFIG */
1809 case 0x24: /* EMIFF_MRS */
1810 case 0x28: /* TIMEOUT1 */
1811 case 0x2c: /* TIMEOUT2 */
1812 case 0x30: /* TIMEOUT3 */
1813 case 0x3c: /* EMIFF_SDRAM_CONFIG_2 */
1814 case 0x40: /* EMIFS_CFG_DYN_WAIT */
1815 return s->tcmi_regs[offset >> 2];
1817 case 0x20: /* EMIFF_SDRAM_CONFIG */
1818 ret = s->tcmi_regs[offset >> 2];
1819 s->tcmi_regs[offset >> 2] &= ~1; /* XXX: Clear SLRF on SDRAM access */
1820 /* XXX: We can try using the VGA_DIRTY flag for this */
1821 return ret;
1824 OMAP_BAD_REG(addr);
1825 return 0;
1828 static void omap_tcmi_write(void *opaque, target_phys_addr_t addr,
1829 uint32_t value)
1831 struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1832 int offset = addr - s->tcmi_base;
1834 switch (offset) {
1835 case 0x00: /* IMIF_PRIO */
1836 case 0x04: /* EMIFS_PRIO */
1837 case 0x08: /* EMIFF_PRIO */
1838 case 0x10: /* EMIFS_CS0_CONFIG */
1839 case 0x14: /* EMIFS_CS1_CONFIG */
1840 case 0x18: /* EMIFS_CS2_CONFIG */
1841 case 0x1c: /* EMIFS_CS3_CONFIG */
1842 case 0x20: /* EMIFF_SDRAM_CONFIG */
1843 case 0x24: /* EMIFF_MRS */
1844 case 0x28: /* TIMEOUT1 */
1845 case 0x2c: /* TIMEOUT2 */
1846 case 0x30: /* TIMEOUT3 */
1847 case 0x3c: /* EMIFF_SDRAM_CONFIG_2 */
1848 case 0x40: /* EMIFS_CFG_DYN_WAIT */
1849 s->tcmi_regs[offset >> 2] = value;
1850 break;
1851 case 0x0c: /* EMIFS_CONFIG */
1852 s->tcmi_regs[offset >> 2] = (value & 0xf) | (1 << 4);
1853 break;
1855 default:
1856 OMAP_BAD_REG(addr);
1860 static CPUReadMemoryFunc *omap_tcmi_readfn[] = {
1861 omap_badwidth_read32,
1862 omap_badwidth_read32,
1863 omap_tcmi_read,
1866 static CPUWriteMemoryFunc *omap_tcmi_writefn[] = {
1867 omap_badwidth_write32,
1868 omap_badwidth_write32,
1869 omap_tcmi_write,
1872 static void omap_tcmi_reset(struct omap_mpu_state_s *mpu)
1874 mpu->tcmi_regs[0x00 >> 2] = 0x00000000;
1875 mpu->tcmi_regs[0x04 >> 2] = 0x00000000;
1876 mpu->tcmi_regs[0x08 >> 2] = 0x00000000;
1877 mpu->tcmi_regs[0x0c >> 2] = 0x00000010;
1878 mpu->tcmi_regs[0x10 >> 2] = 0x0010fffb;
1879 mpu->tcmi_regs[0x14 >> 2] = 0x0010fffb;
1880 mpu->tcmi_regs[0x18 >> 2] = 0x0010fffb;
1881 mpu->tcmi_regs[0x1c >> 2] = 0x0010fffb;
1882 mpu->tcmi_regs[0x20 >> 2] = 0x00618800;
1883 mpu->tcmi_regs[0x24 >> 2] = 0x00000037;
1884 mpu->tcmi_regs[0x28 >> 2] = 0x00000000;
1885 mpu->tcmi_regs[0x2c >> 2] = 0x00000000;
1886 mpu->tcmi_regs[0x30 >> 2] = 0x00000000;
1887 mpu->tcmi_regs[0x3c >> 2] = 0x00000003;
1888 mpu->tcmi_regs[0x40 >> 2] = 0x00000000;
1891 static void omap_tcmi_init(target_phys_addr_t base,
1892 struct omap_mpu_state_s *mpu)
1894 int iomemtype = cpu_register_io_memory(0, omap_tcmi_readfn,
1895 omap_tcmi_writefn, mpu);
1897 mpu->tcmi_base = base;
1898 cpu_register_physical_memory(mpu->tcmi_base, 0x100, iomemtype);
1899 omap_tcmi_reset(mpu);
1902 /* Digital phase-locked loops control */
1903 static uint32_t omap_dpll_read(void *opaque, target_phys_addr_t addr)
1905 struct dpll_ctl_s *s = (struct dpll_ctl_s *) opaque;
1906 int offset = addr - s->base;
1908 if (offset == 0x00) /* CTL_REG */
1909 return s->mode;
1911 OMAP_BAD_REG(addr);
1912 return 0;
1915 static void omap_dpll_write(void *opaque, target_phys_addr_t addr,
1916 uint32_t value)
1918 struct dpll_ctl_s *s = (struct dpll_ctl_s *) opaque;
1919 uint16_t diff;
1920 int offset = addr - s->base;
1921 static const int bypass_div[4] = { 1, 2, 4, 4 };
1922 int div, mult;
1924 if (offset == 0x00) { /* CTL_REG */
1925 /* See omap_ulpd_pm_write() too */
1926 diff = s->mode & value;
1927 s->mode = value & 0x2fff;
1928 if (diff & (0x3ff << 2)) {
1929 if (value & (1 << 4)) { /* PLL_ENABLE */
1930 div = ((value >> 5) & 3) + 1; /* PLL_DIV */
1931 mult = MIN((value >> 7) & 0x1f, 1); /* PLL_MULT */
1932 } else {
1933 div = bypass_div[((value >> 2) & 3)]; /* BYPASS_DIV */
1934 mult = 1;
1936 omap_clk_setrate(s->dpll, div, mult);
1939 /* Enter the desired mode. */
1940 s->mode = (s->mode & 0xfffe) | ((s->mode >> 4) & 1);
1942 /* Act as if the lock is restored. */
1943 s->mode |= 2;
1944 } else {
1945 OMAP_BAD_REG(addr);
1949 static CPUReadMemoryFunc *omap_dpll_readfn[] = {
1950 omap_badwidth_read16,
1951 omap_dpll_read,
1952 omap_badwidth_read16,
1955 static CPUWriteMemoryFunc *omap_dpll_writefn[] = {
1956 omap_badwidth_write16,
1957 omap_dpll_write,
1958 omap_badwidth_write16,
1961 static void omap_dpll_reset(struct dpll_ctl_s *s)
1963 s->mode = 0x2002;
1964 omap_clk_setrate(s->dpll, 1, 1);
1967 static void omap_dpll_init(struct dpll_ctl_s *s, target_phys_addr_t base,
1968 omap_clk clk)
1970 int iomemtype = cpu_register_io_memory(0, omap_dpll_readfn,
1971 omap_dpll_writefn, s);
1973 s->base = base;
1974 s->dpll = clk;
1975 omap_dpll_reset(s);
1977 cpu_register_physical_memory(s->base, 0x100, iomemtype);
1980 /* UARTs */
1981 struct omap_uart_s {
1982 SerialState *serial; /* TODO */
1983 struct omap_target_agent_s *ta;
1984 target_phys_addr_t base;
1986 uint8_t eblr;
1987 uint8_t syscontrol;
1988 uint8_t wkup;
1989 uint8_t cfps;
1992 void omap_uart_reset(struct omap_uart_s *s)
1994 s->eblr = 0x00;
1995 s->syscontrol = 0;
1996 s->wkup = 0x3f;
1997 s->cfps = 0x69;
2000 struct omap_uart_s *omap_uart_init(target_phys_addr_t base,
2001 qemu_irq irq, omap_clk fclk, omap_clk iclk,
2002 qemu_irq txdma, qemu_irq rxdma, CharDriverState *chr)
2004 struct omap_uart_s *s = (struct omap_uart_s *)
2005 qemu_mallocz(sizeof(struct omap_uart_s));
2007 s->serial = serial_mm_init(base, 2, irq, chr ?: qemu_chr_open("null"), 1);
2009 return s;
2012 static uint32_t omap_uart_read(void *opaque, target_phys_addr_t addr)
2014 struct omap_uart_s *s = (struct omap_uart_s *) opaque;
2015 int offset = addr - s->base;
2017 switch (offset) {
2018 case 0x48: /* EBLR */
2019 return s->eblr;
2020 case 0x50: /* MVR */
2021 return 0x30;
2022 case 0x54: /* SYSC */
2023 return s->syscontrol;
2024 case 0x58: /* SYSS */
2025 return 1;
2026 case 0x5c: /* WER */
2027 return s->wkup;
2028 case 0x60: /* CFPS */
2029 return s->cfps;
2032 OMAP_BAD_REG(addr);
2033 return 0;
2036 static void omap_uart_write(void *opaque, target_phys_addr_t addr,
2037 uint32_t value)
2039 struct omap_uart_s *s = (struct omap_uart_s *) opaque;
2040 int offset = addr - s->base;
2042 switch (offset) {
2043 case 0x48: /* EBLR */
2044 s->eblr = value & 0xff;
2045 break;
2046 case 0x50: /* MVR */
2047 case 0x58: /* SYSS */
2048 OMAP_RO_REG(addr);
2049 break;
2050 case 0x54: /* SYSC */
2051 s->syscontrol = value & 0x1d;
2052 if (value & 2)
2053 omap_uart_reset(s);
2054 break;
2055 case 0x5c: /* WER */
2056 s->wkup = value & 0x7f;
2057 break;
2058 case 0x60: /* CFPS */
2059 s->cfps = value & 0xff;
2060 break;
2061 default:
2062 OMAP_BAD_REG(addr);
2066 static CPUReadMemoryFunc *omap_uart_readfn[] = {
2067 omap_uart_read,
2068 omap_uart_read,
2069 omap_badwidth_read8,
2072 static CPUWriteMemoryFunc *omap_uart_writefn[] = {
2073 omap_uart_write,
2074 omap_uart_write,
2075 omap_badwidth_write8,
2078 struct omap_uart_s *omap2_uart_init(struct omap_target_agent_s *ta,
2079 qemu_irq irq, omap_clk fclk, omap_clk iclk,
2080 qemu_irq txdma, qemu_irq rxdma, CharDriverState *chr)
2082 target_phys_addr_t base = omap_l4_attach(ta, 0, 0);
2083 struct omap_uart_s *s = omap_uart_init(base, irq,
2084 fclk, iclk, txdma, rxdma, chr);
2085 int iomemtype = cpu_register_io_memory(0, omap_uart_readfn,
2086 omap_uart_writefn, s);
2088 s->ta = ta;
2089 s->base = base;
2091 cpu_register_physical_memory(s->base + 0x20, 0x100, iomemtype);
2093 return s;
2096 /* MPU Clock/Reset/Power Mode Control */
2097 static uint32_t omap_clkm_read(void *opaque, target_phys_addr_t addr)
2099 struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
2100 int offset = addr - s->clkm.mpu_base;
2102 switch (offset) {
2103 case 0x00: /* ARM_CKCTL */
2104 return s->clkm.arm_ckctl;
2106 case 0x04: /* ARM_IDLECT1 */
2107 return s->clkm.arm_idlect1;
2109 case 0x08: /* ARM_IDLECT2 */
2110 return s->clkm.arm_idlect2;
2112 case 0x0c: /* ARM_EWUPCT */
2113 return s->clkm.arm_ewupct;
2115 case 0x10: /* ARM_RSTCT1 */
2116 return s->clkm.arm_rstct1;
2118 case 0x14: /* ARM_RSTCT2 */
2119 return s->clkm.arm_rstct2;
2121 case 0x18: /* ARM_SYSST */
2122 return (s->clkm.clocking_scheme << 11) | s->clkm.cold_start;
2124 case 0x1c: /* ARM_CKOUT1 */
2125 return s->clkm.arm_ckout1;
2127 case 0x20: /* ARM_CKOUT2 */
2128 break;
2131 OMAP_BAD_REG(addr);
2132 return 0;
2135 static inline void omap_clkm_ckctl_update(struct omap_mpu_state_s *s,
2136 uint16_t diff, uint16_t value)
2138 omap_clk clk;
2140 if (diff & (1 << 14)) { /* ARM_INTHCK_SEL */
2141 if (value & (1 << 14))
2142 /* Reserved */;
2143 else {
2144 clk = omap_findclk(s, "arminth_ck");
2145 omap_clk_reparent(clk, omap_findclk(s, "tc_ck"));
2148 if (diff & (1 << 12)) { /* ARM_TIMXO */
2149 clk = omap_findclk(s, "armtim_ck");
2150 if (value & (1 << 12))
2151 omap_clk_reparent(clk, omap_findclk(s, "clkin"));
2152 else
2153 omap_clk_reparent(clk, omap_findclk(s, "ck_gen1"));
2155 /* XXX: en_dspck */
2156 if (diff & (3 << 10)) { /* DSPMMUDIV */
2157 clk = omap_findclk(s, "dspmmu_ck");
2158 omap_clk_setrate(clk, 1 << ((value >> 10) & 3), 1);
2160 if (diff & (3 << 8)) { /* TCDIV */
2161 clk = omap_findclk(s, "tc_ck");
2162 omap_clk_setrate(clk, 1 << ((value >> 8) & 3), 1);
2164 if (diff & (3 << 6)) { /* DSPDIV */
2165 clk = omap_findclk(s, "dsp_ck");
2166 omap_clk_setrate(clk, 1 << ((value >> 6) & 3), 1);
2168 if (diff & (3 << 4)) { /* ARMDIV */
2169 clk = omap_findclk(s, "arm_ck");
2170 omap_clk_setrate(clk, 1 << ((value >> 4) & 3), 1);
2172 if (diff & (3 << 2)) { /* LCDDIV */
2173 clk = omap_findclk(s, "lcd_ck");
2174 omap_clk_setrate(clk, 1 << ((value >> 2) & 3), 1);
2176 if (diff & (3 << 0)) { /* PERDIV */
2177 clk = omap_findclk(s, "armper_ck");
2178 omap_clk_setrate(clk, 1 << ((value >> 0) & 3), 1);
2182 static inline void omap_clkm_idlect1_update(struct omap_mpu_state_s *s,
2183 uint16_t diff, uint16_t value)
2185 omap_clk clk;
2187 if (value & (1 << 11)) /* SETARM_IDLE */
2188 cpu_interrupt(s->env, CPU_INTERRUPT_HALT);
2189 if (!(value & (1 << 10))) /* WKUP_MODE */
2190 qemu_system_shutdown_request(); /* XXX: disable wakeup from IRQ */
2192 #define SET_CANIDLE(clock, bit) \
2193 if (diff & (1 << bit)) { \
2194 clk = omap_findclk(s, clock); \
2195 omap_clk_canidle(clk, (value >> bit) & 1); \
2197 SET_CANIDLE("mpuwd_ck", 0) /* IDLWDT_ARM */
2198 SET_CANIDLE("armxor_ck", 1) /* IDLXORP_ARM */
2199 SET_CANIDLE("mpuper_ck", 2) /* IDLPER_ARM */
2200 SET_CANIDLE("lcd_ck", 3) /* IDLLCD_ARM */
2201 SET_CANIDLE("lb_ck", 4) /* IDLLB_ARM */
2202 SET_CANIDLE("hsab_ck", 5) /* IDLHSAB_ARM */
2203 SET_CANIDLE("tipb_ck", 6) /* IDLIF_ARM */
2204 SET_CANIDLE("dma_ck", 6) /* IDLIF_ARM */
2205 SET_CANIDLE("tc_ck", 6) /* IDLIF_ARM */
2206 SET_CANIDLE("dpll1", 7) /* IDLDPLL_ARM */
2207 SET_CANIDLE("dpll2", 7) /* IDLDPLL_ARM */
2208 SET_CANIDLE("dpll3", 7) /* IDLDPLL_ARM */
2209 SET_CANIDLE("mpui_ck", 8) /* IDLAPI_ARM */
2210 SET_CANIDLE("armtim_ck", 9) /* IDLTIM_ARM */
2213 static inline void omap_clkm_idlect2_update(struct omap_mpu_state_s *s,
2214 uint16_t diff, uint16_t value)
2216 omap_clk clk;
2218 #define SET_ONOFF(clock, bit) \
2219 if (diff & (1 << bit)) { \
2220 clk = omap_findclk(s, clock); \
2221 omap_clk_onoff(clk, (value >> bit) & 1); \
2223 SET_ONOFF("mpuwd_ck", 0) /* EN_WDTCK */
2224 SET_ONOFF("armxor_ck", 1) /* EN_XORPCK */
2225 SET_ONOFF("mpuper_ck", 2) /* EN_PERCK */
2226 SET_ONOFF("lcd_ck", 3) /* EN_LCDCK */
2227 SET_ONOFF("lb_ck", 4) /* EN_LBCK */
2228 SET_ONOFF("hsab_ck", 5) /* EN_HSABCK */
2229 SET_ONOFF("mpui_ck", 6) /* EN_APICK */
2230 SET_ONOFF("armtim_ck", 7) /* EN_TIMCK */
2231 SET_CANIDLE("dma_ck", 8) /* DMACK_REQ */
2232 SET_ONOFF("arm_gpio_ck", 9) /* EN_GPIOCK */
2233 SET_ONOFF("lbfree_ck", 10) /* EN_LBFREECK */
2236 static inline void omap_clkm_ckout1_update(struct omap_mpu_state_s *s,
2237 uint16_t diff, uint16_t value)
2239 omap_clk clk;
2241 if (diff & (3 << 4)) { /* TCLKOUT */
2242 clk = omap_findclk(s, "tclk_out");
2243 switch ((value >> 4) & 3) {
2244 case 1:
2245 omap_clk_reparent(clk, omap_findclk(s, "ck_gen3"));
2246 omap_clk_onoff(clk, 1);
2247 break;
2248 case 2:
2249 omap_clk_reparent(clk, omap_findclk(s, "tc_ck"));
2250 omap_clk_onoff(clk, 1);
2251 break;
2252 default:
2253 omap_clk_onoff(clk, 0);
2256 if (diff & (3 << 2)) { /* DCLKOUT */
2257 clk = omap_findclk(s, "dclk_out");
2258 switch ((value >> 2) & 3) {
2259 case 0:
2260 omap_clk_reparent(clk, omap_findclk(s, "dspmmu_ck"));
2261 break;
2262 case 1:
2263 omap_clk_reparent(clk, omap_findclk(s, "ck_gen2"));
2264 break;
2265 case 2:
2266 omap_clk_reparent(clk, omap_findclk(s, "dsp_ck"));
2267 break;
2268 case 3:
2269 omap_clk_reparent(clk, omap_findclk(s, "ck_ref14"));
2270 break;
2273 if (diff & (3 << 0)) { /* ACLKOUT */
2274 clk = omap_findclk(s, "aclk_out");
2275 switch ((value >> 0) & 3) {
2276 case 1:
2277 omap_clk_reparent(clk, omap_findclk(s, "ck_gen1"));
2278 omap_clk_onoff(clk, 1);
2279 break;
2280 case 2:
2281 omap_clk_reparent(clk, omap_findclk(s, "arm_ck"));
2282 omap_clk_onoff(clk, 1);
2283 break;
2284 case 3:
2285 omap_clk_reparent(clk, omap_findclk(s, "ck_ref14"));
2286 omap_clk_onoff(clk, 1);
2287 break;
2288 default:
2289 omap_clk_onoff(clk, 0);
2294 static void omap_clkm_write(void *opaque, target_phys_addr_t addr,
2295 uint32_t value)
2297 struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
2298 int offset = addr - s->clkm.mpu_base;
2299 uint16_t diff;
2300 omap_clk clk;
2301 static const char *clkschemename[8] = {
2302 "fully synchronous", "fully asynchronous", "synchronous scalable",
2303 "mix mode 1", "mix mode 2", "bypass mode", "mix mode 3", "mix mode 4",
2306 switch (offset) {
2307 case 0x00: /* ARM_CKCTL */
2308 diff = s->clkm.arm_ckctl ^ value;
2309 s->clkm.arm_ckctl = value & 0x7fff;
2310 omap_clkm_ckctl_update(s, diff, value);
2311 return;
2313 case 0x04: /* ARM_IDLECT1 */
2314 diff = s->clkm.arm_idlect1 ^ value;
2315 s->clkm.arm_idlect1 = value & 0x0fff;
2316 omap_clkm_idlect1_update(s, diff, value);
2317 return;
2319 case 0x08: /* ARM_IDLECT2 */
2320 diff = s->clkm.arm_idlect2 ^ value;
2321 s->clkm.arm_idlect2 = value & 0x07ff;
2322 omap_clkm_idlect2_update(s, diff, value);
2323 return;
2325 case 0x0c: /* ARM_EWUPCT */
2326 diff = s->clkm.arm_ewupct ^ value;
2327 s->clkm.arm_ewupct = value & 0x003f;
2328 return;
2330 case 0x10: /* ARM_RSTCT1 */
2331 diff = s->clkm.arm_rstct1 ^ value;
2332 s->clkm.arm_rstct1 = value & 0x0007;
2333 if (value & 9) {
2334 qemu_system_reset_request();
2335 s->clkm.cold_start = 0xa;
2337 if (diff & ~value & 4) { /* DSP_RST */
2338 omap_mpui_reset(s);
2339 omap_tipb_bridge_reset(s->private_tipb);
2340 omap_tipb_bridge_reset(s->public_tipb);
2342 if (diff & 2) { /* DSP_EN */
2343 clk = omap_findclk(s, "dsp_ck");
2344 omap_clk_canidle(clk, (~value >> 1) & 1);
2346 return;
2348 case 0x14: /* ARM_RSTCT2 */
2349 s->clkm.arm_rstct2 = value & 0x0001;
2350 return;
2352 case 0x18: /* ARM_SYSST */
2353 if ((s->clkm.clocking_scheme ^ (value >> 11)) & 7) {
2354 s->clkm.clocking_scheme = (value >> 11) & 7;
2355 printf("%s: clocking scheme set to %s\n", __FUNCTION__,
2356 clkschemename[s->clkm.clocking_scheme]);
2358 s->clkm.cold_start &= value & 0x3f;
2359 return;
2361 case 0x1c: /* ARM_CKOUT1 */
2362 diff = s->clkm.arm_ckout1 ^ value;
2363 s->clkm.arm_ckout1 = value & 0x003f;
2364 omap_clkm_ckout1_update(s, diff, value);
2365 return;
2367 case 0x20: /* ARM_CKOUT2 */
2368 default:
2369 OMAP_BAD_REG(addr);
2373 static CPUReadMemoryFunc *omap_clkm_readfn[] = {
2374 omap_badwidth_read16,
2375 omap_clkm_read,
2376 omap_badwidth_read16,
2379 static CPUWriteMemoryFunc *omap_clkm_writefn[] = {
2380 omap_badwidth_write16,
2381 omap_clkm_write,
2382 omap_badwidth_write16,
2385 static uint32_t omap_clkdsp_read(void *opaque, target_phys_addr_t addr)
2387 struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
2388 int offset = addr - s->clkm.dsp_base;
2390 switch (offset) {
2391 case 0x04: /* DSP_IDLECT1 */
2392 return s->clkm.dsp_idlect1;
2394 case 0x08: /* DSP_IDLECT2 */
2395 return s->clkm.dsp_idlect2;
2397 case 0x14: /* DSP_RSTCT2 */
2398 return s->clkm.dsp_rstct2;
2400 case 0x18: /* DSP_SYSST */
2401 return (s->clkm.clocking_scheme << 11) | s->clkm.cold_start |
2402 (s->env->halted << 6); /* Quite useless... */
2405 OMAP_BAD_REG(addr);
2406 return 0;
2409 static inline void omap_clkdsp_idlect1_update(struct omap_mpu_state_s *s,
2410 uint16_t diff, uint16_t value)
2412 omap_clk clk;
2414 SET_CANIDLE("dspxor_ck", 1); /* IDLXORP_DSP */
2417 static inline void omap_clkdsp_idlect2_update(struct omap_mpu_state_s *s,
2418 uint16_t diff, uint16_t value)
2420 omap_clk clk;
2422 SET_ONOFF("dspxor_ck", 1); /* EN_XORPCK */
2425 static void omap_clkdsp_write(void *opaque, target_phys_addr_t addr,
2426 uint32_t value)
2428 struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
2429 int offset = addr - s->clkm.dsp_base;
2430 uint16_t diff;
2432 switch (offset) {
2433 case 0x04: /* DSP_IDLECT1 */
2434 diff = s->clkm.dsp_idlect1 ^ value;
2435 s->clkm.dsp_idlect1 = value & 0x01f7;
2436 omap_clkdsp_idlect1_update(s, diff, value);
2437 break;
2439 case 0x08: /* DSP_IDLECT2 */
2440 s->clkm.dsp_idlect2 = value & 0x0037;
2441 diff = s->clkm.dsp_idlect1 ^ value;
2442 omap_clkdsp_idlect2_update(s, diff, value);
2443 break;
2445 case 0x14: /* DSP_RSTCT2 */
2446 s->clkm.dsp_rstct2 = value & 0x0001;
2447 break;
2449 case 0x18: /* DSP_SYSST */
2450 s->clkm.cold_start &= value & 0x3f;
2451 break;
2453 default:
2454 OMAP_BAD_REG(addr);
2458 static CPUReadMemoryFunc *omap_clkdsp_readfn[] = {
2459 omap_badwidth_read16,
2460 omap_clkdsp_read,
2461 omap_badwidth_read16,
2464 static CPUWriteMemoryFunc *omap_clkdsp_writefn[] = {
2465 omap_badwidth_write16,
2466 omap_clkdsp_write,
2467 omap_badwidth_write16,
2470 static void omap_clkm_reset(struct omap_mpu_state_s *s)
2472 if (s->wdt && s->wdt->reset)
2473 s->clkm.cold_start = 0x6;
2474 s->clkm.clocking_scheme = 0;
2475 omap_clkm_ckctl_update(s, ~0, 0x3000);
2476 s->clkm.arm_ckctl = 0x3000;
2477 omap_clkm_idlect1_update(s, s->clkm.arm_idlect1 ^ 0x0400, 0x0400);
2478 s->clkm.arm_idlect1 = 0x0400;
2479 omap_clkm_idlect2_update(s, s->clkm.arm_idlect2 ^ 0x0100, 0x0100);
2480 s->clkm.arm_idlect2 = 0x0100;
2481 s->clkm.arm_ewupct = 0x003f;
2482 s->clkm.arm_rstct1 = 0x0000;
2483 s->clkm.arm_rstct2 = 0x0000;
2484 s->clkm.arm_ckout1 = 0x0015;
2485 s->clkm.dpll1_mode = 0x2002;
2486 omap_clkdsp_idlect1_update(s, s->clkm.dsp_idlect1 ^ 0x0040, 0x0040);
2487 s->clkm.dsp_idlect1 = 0x0040;
2488 omap_clkdsp_idlect2_update(s, ~0, 0x0000);
2489 s->clkm.dsp_idlect2 = 0x0000;
2490 s->clkm.dsp_rstct2 = 0x0000;
2493 static void omap_clkm_init(target_phys_addr_t mpu_base,
2494 target_phys_addr_t dsp_base, struct omap_mpu_state_s *s)
2496 int iomemtype[2] = {
2497 cpu_register_io_memory(0, omap_clkm_readfn, omap_clkm_writefn, s),
2498 cpu_register_io_memory(0, omap_clkdsp_readfn, omap_clkdsp_writefn, s),
2501 s->clkm.mpu_base = mpu_base;
2502 s->clkm.dsp_base = dsp_base;
2503 s->clkm.arm_idlect1 = 0x03ff;
2504 s->clkm.arm_idlect2 = 0x0100;
2505 s->clkm.dsp_idlect1 = 0x0002;
2506 omap_clkm_reset(s);
2507 s->clkm.cold_start = 0x3a;
2509 cpu_register_physical_memory(s->clkm.mpu_base, 0x100, iomemtype[0]);
2510 cpu_register_physical_memory(s->clkm.dsp_base, 0x1000, iomemtype[1]);
2513 /* MPU I/O */
2514 struct omap_mpuio_s {
2515 target_phys_addr_t base;
2516 qemu_irq irq;
2517 qemu_irq kbd_irq;
2518 qemu_irq *in;
2519 qemu_irq handler[16];
2520 qemu_irq wakeup;
2522 uint16_t inputs;
2523 uint16_t outputs;
2524 uint16_t dir;
2525 uint16_t edge;
2526 uint16_t mask;
2527 uint16_t ints;
2529 uint16_t debounce;
2530 uint16_t latch;
2531 uint8_t event;
2533 uint8_t buttons[5];
2534 uint8_t row_latch;
2535 uint8_t cols;
2536 int kbd_mask;
2537 int clk;
2540 static void omap_mpuio_set(void *opaque, int line, int level)
2542 struct omap_mpuio_s *s = (struct omap_mpuio_s *) opaque;
2543 uint16_t prev = s->inputs;
2545 if (level)
2546 s->inputs |= 1 << line;
2547 else
2548 s->inputs &= ~(1 << line);
2550 if (((1 << line) & s->dir & ~s->mask) && s->clk) {
2551 if ((s->edge & s->inputs & ~prev) | (~s->edge & ~s->inputs & prev)) {
2552 s->ints |= 1 << line;
2553 qemu_irq_raise(s->irq);
2554 /* TODO: wakeup */
2556 if ((s->event & (1 << 0)) && /* SET_GPIO_EVENT_MODE */
2557 (s->event >> 1) == line) /* PIN_SELECT */
2558 s->latch = s->inputs;
2562 static void omap_mpuio_kbd_update(struct omap_mpuio_s *s)
2564 int i;
2565 uint8_t *row, rows = 0, cols = ~s->cols;
2567 for (row = s->buttons + 4, i = 1 << 4; i; row --, i >>= 1)
2568 if (*row & cols)
2569 rows |= i;
2571 qemu_set_irq(s->kbd_irq, rows && !s->kbd_mask && s->clk);
2572 s->row_latch = ~rows;
2575 static uint32_t omap_mpuio_read(void *opaque, target_phys_addr_t addr)
2577 struct omap_mpuio_s *s = (struct omap_mpuio_s *) opaque;
2578 int offset = addr & OMAP_MPUI_REG_MASK;
2579 uint16_t ret;
2581 switch (offset) {
2582 case 0x00: /* INPUT_LATCH */
2583 return s->inputs;
2585 case 0x04: /* OUTPUT_REG */
2586 return s->outputs;
2588 case 0x08: /* IO_CNTL */
2589 return s->dir;
2591 case 0x10: /* KBR_LATCH */
2592 return s->row_latch;
2594 case 0x14: /* KBC_REG */
2595 return s->cols;
2597 case 0x18: /* GPIO_EVENT_MODE_REG */
2598 return s->event;
2600 case 0x1c: /* GPIO_INT_EDGE_REG */
2601 return s->edge;
2603 case 0x20: /* KBD_INT */
2604 return (~s->row_latch & 0x1f) && !s->kbd_mask;
2606 case 0x24: /* GPIO_INT */
2607 ret = s->ints;
2608 s->ints &= s->mask;
2609 if (ret)
2610 qemu_irq_lower(s->irq);
2611 return ret;
2613 case 0x28: /* KBD_MASKIT */
2614 return s->kbd_mask;
2616 case 0x2c: /* GPIO_MASKIT */
2617 return s->mask;
2619 case 0x30: /* GPIO_DEBOUNCING_REG */
2620 return s->debounce;
2622 case 0x34: /* GPIO_LATCH_REG */
2623 return s->latch;
2626 OMAP_BAD_REG(addr);
2627 return 0;
2630 static void omap_mpuio_write(void *opaque, target_phys_addr_t addr,
2631 uint32_t value)
2633 struct omap_mpuio_s *s = (struct omap_mpuio_s *) opaque;
2634 int offset = addr & OMAP_MPUI_REG_MASK;
2635 uint16_t diff;
2636 int ln;
2638 switch (offset) {
2639 case 0x04: /* OUTPUT_REG */
2640 diff = (s->outputs ^ value) & ~s->dir;
2641 s->outputs = value;
2642 while ((ln = ffs(diff))) {
2643 ln --;
2644 if (s->handler[ln])
2645 qemu_set_irq(s->handler[ln], (value >> ln) & 1);
2646 diff &= ~(1 << ln);
2648 break;
2650 case 0x08: /* IO_CNTL */
2651 diff = s->outputs & (s->dir ^ value);
2652 s->dir = value;
2654 value = s->outputs & ~s->dir;
2655 while ((ln = ffs(diff))) {
2656 ln --;
2657 if (s->handler[ln])
2658 qemu_set_irq(s->handler[ln], (value >> ln) & 1);
2659 diff &= ~(1 << ln);
2661 break;
2663 case 0x14: /* KBC_REG */
2664 s->cols = value;
2665 omap_mpuio_kbd_update(s);
2666 break;
2668 case 0x18: /* GPIO_EVENT_MODE_REG */
2669 s->event = value & 0x1f;
2670 break;
2672 case 0x1c: /* GPIO_INT_EDGE_REG */
2673 s->edge = value;
2674 break;
2676 case 0x28: /* KBD_MASKIT */
2677 s->kbd_mask = value & 1;
2678 omap_mpuio_kbd_update(s);
2679 break;
2681 case 0x2c: /* GPIO_MASKIT */
2682 s->mask = value;
2683 break;
2685 case 0x30: /* GPIO_DEBOUNCING_REG */
2686 s->debounce = value & 0x1ff;
2687 break;
2689 case 0x00: /* INPUT_LATCH */
2690 case 0x10: /* KBR_LATCH */
2691 case 0x20: /* KBD_INT */
2692 case 0x24: /* GPIO_INT */
2693 case 0x34: /* GPIO_LATCH_REG */
2694 OMAP_RO_REG(addr);
2695 return;
2697 default:
2698 OMAP_BAD_REG(addr);
2699 return;
2703 static CPUReadMemoryFunc *omap_mpuio_readfn[] = {
2704 omap_badwidth_read16,
2705 omap_mpuio_read,
2706 omap_badwidth_read16,
2709 static CPUWriteMemoryFunc *omap_mpuio_writefn[] = {
2710 omap_badwidth_write16,
2711 omap_mpuio_write,
2712 omap_badwidth_write16,
2715 static void omap_mpuio_reset(struct omap_mpuio_s *s)
2717 s->inputs = 0;
2718 s->outputs = 0;
2719 s->dir = ~0;
2720 s->event = 0;
2721 s->edge = 0;
2722 s->kbd_mask = 0;
2723 s->mask = 0;
2724 s->debounce = 0;
2725 s->latch = 0;
2726 s->ints = 0;
2727 s->row_latch = 0x1f;
2728 s->clk = 1;
2731 static void omap_mpuio_onoff(void *opaque, int line, int on)
2733 struct omap_mpuio_s *s = (struct omap_mpuio_s *) opaque;
2735 s->clk = on;
2736 if (on)
2737 omap_mpuio_kbd_update(s);
2740 struct omap_mpuio_s *omap_mpuio_init(target_phys_addr_t base,
2741 qemu_irq kbd_int, qemu_irq gpio_int, qemu_irq wakeup,
2742 omap_clk clk)
2744 int iomemtype;
2745 struct omap_mpuio_s *s = (struct omap_mpuio_s *)
2746 qemu_mallocz(sizeof(struct omap_mpuio_s));
2748 s->base = base;
2749 s->irq = gpio_int;
2750 s->kbd_irq = kbd_int;
2751 s->wakeup = wakeup;
2752 s->in = qemu_allocate_irqs(omap_mpuio_set, s, 16);
2753 omap_mpuio_reset(s);
2755 iomemtype = cpu_register_io_memory(0, omap_mpuio_readfn,
2756 omap_mpuio_writefn, s);
2757 cpu_register_physical_memory(s->base, 0x800, iomemtype);
2759 omap_clk_adduser(clk, qemu_allocate_irqs(omap_mpuio_onoff, s, 1)[0]);
2761 return s;
2764 qemu_irq *omap_mpuio_in_get(struct omap_mpuio_s *s)
2766 return s->in;
2769 void omap_mpuio_out_set(struct omap_mpuio_s *s, int line, qemu_irq handler)
2771 if (line >= 16 || line < 0)
2772 cpu_abort(cpu_single_env, "%s: No GPIO line %i\n", __FUNCTION__, line);
2773 s->handler[line] = handler;
2776 void omap_mpuio_key(struct omap_mpuio_s *s, int row, int col, int down)
2778 if (row >= 5 || row < 0)
2779 cpu_abort(cpu_single_env, "%s: No key %i-%i\n",
2780 __FUNCTION__, col, row);
2782 if (down)
2783 s->buttons[row] |= 1 << col;
2784 else
2785 s->buttons[row] &= ~(1 << col);
2787 omap_mpuio_kbd_update(s);
2790 /* General-Purpose I/O */
2791 struct omap_gpio_s {
2792 target_phys_addr_t base;
2793 qemu_irq irq;
2794 qemu_irq *in;
2795 qemu_irq handler[16];
2797 uint16_t inputs;
2798 uint16_t outputs;
2799 uint16_t dir;
2800 uint16_t edge;
2801 uint16_t mask;
2802 uint16_t ints;
2803 uint16_t pins;
2806 static void omap_gpio_set(void *opaque, int line, int level)
2808 struct omap_gpio_s *s = (struct omap_gpio_s *) opaque;
2809 uint16_t prev = s->inputs;
2811 if (level)
2812 s->inputs |= 1 << line;
2813 else
2814 s->inputs &= ~(1 << line);
2816 if (((s->edge & s->inputs & ~prev) | (~s->edge & ~s->inputs & prev)) &
2817 (1 << line) & s->dir & ~s->mask) {
2818 s->ints |= 1 << line;
2819 qemu_irq_raise(s->irq);
2823 static uint32_t omap_gpio_read(void *opaque, target_phys_addr_t addr)
2825 struct omap_gpio_s *s = (struct omap_gpio_s *) opaque;
2826 int offset = addr & OMAP_MPUI_REG_MASK;
2828 switch (offset) {
2829 case 0x00: /* DATA_INPUT */
2830 return s->inputs & s->pins;
2832 case 0x04: /* DATA_OUTPUT */
2833 return s->outputs;
2835 case 0x08: /* DIRECTION_CONTROL */
2836 return s->dir;
2838 case 0x0c: /* INTERRUPT_CONTROL */
2839 return s->edge;
2841 case 0x10: /* INTERRUPT_MASK */
2842 return s->mask;
2844 case 0x14: /* INTERRUPT_STATUS */
2845 return s->ints;
2847 case 0x18: /* PIN_CONTROL (not in OMAP310) */
2848 OMAP_BAD_REG(addr);
2849 return s->pins;
2852 OMAP_BAD_REG(addr);
2853 return 0;
2856 static void omap_gpio_write(void *opaque, target_phys_addr_t addr,
2857 uint32_t value)
2859 struct omap_gpio_s *s = (struct omap_gpio_s *) opaque;
2860 int offset = addr & OMAP_MPUI_REG_MASK;
2861 uint16_t diff;
2862 int ln;
2864 switch (offset) {
2865 case 0x00: /* DATA_INPUT */
2866 OMAP_RO_REG(addr);
2867 return;
2869 case 0x04: /* DATA_OUTPUT */
2870 diff = (s->outputs ^ value) & ~s->dir;
2871 s->outputs = value;
2872 while ((ln = ffs(diff))) {
2873 ln --;
2874 if (s->handler[ln])
2875 qemu_set_irq(s->handler[ln], (value >> ln) & 1);
2876 diff &= ~(1 << ln);
2878 break;
2880 case 0x08: /* DIRECTION_CONTROL */
2881 diff = s->outputs & (s->dir ^ value);
2882 s->dir = value;
2884 value = s->outputs & ~s->dir;
2885 while ((ln = ffs(diff))) {
2886 ln --;
2887 if (s->handler[ln])
2888 qemu_set_irq(s->handler[ln], (value >> ln) & 1);
2889 diff &= ~(1 << ln);
2891 break;
2893 case 0x0c: /* INTERRUPT_CONTROL */
2894 s->edge = value;
2895 break;
2897 case 0x10: /* INTERRUPT_MASK */
2898 s->mask = value;
2899 break;
2901 case 0x14: /* INTERRUPT_STATUS */
2902 s->ints &= ~value;
2903 if (!s->ints)
2904 qemu_irq_lower(s->irq);
2905 break;
2907 case 0x18: /* PIN_CONTROL (not in OMAP310 TRM) */
2908 OMAP_BAD_REG(addr);
2909 s->pins = value;
2910 break;
2912 default:
2913 OMAP_BAD_REG(addr);
2914 return;
2918 /* *Some* sources say the memory region is 32-bit. */
2919 static CPUReadMemoryFunc *omap_gpio_readfn[] = {
2920 omap_badwidth_read16,
2921 omap_gpio_read,
2922 omap_badwidth_read16,
2925 static CPUWriteMemoryFunc *omap_gpio_writefn[] = {
2926 omap_badwidth_write16,
2927 omap_gpio_write,
2928 omap_badwidth_write16,
2931 static void omap_gpio_reset(struct omap_gpio_s *s)
2933 s->inputs = 0;
2934 s->outputs = ~0;
2935 s->dir = ~0;
2936 s->edge = ~0;
2937 s->mask = ~0;
2938 s->ints = 0;
2939 s->pins = ~0;
2942 struct omap_gpio_s *omap_gpio_init(target_phys_addr_t base,
2943 qemu_irq irq, omap_clk clk)
2945 int iomemtype;
2946 struct omap_gpio_s *s = (struct omap_gpio_s *)
2947 qemu_mallocz(sizeof(struct omap_gpio_s));
2949 s->base = base;
2950 s->irq = irq;
2951 s->in = qemu_allocate_irqs(omap_gpio_set, s, 16);
2952 omap_gpio_reset(s);
2954 iomemtype = cpu_register_io_memory(0, omap_gpio_readfn,
2955 omap_gpio_writefn, s);
2956 cpu_register_physical_memory(s->base, 0x1000, iomemtype);
2958 return s;
2961 qemu_irq *omap_gpio_in_get(struct omap_gpio_s *s)
2963 return s->in;
2966 void omap_gpio_out_set(struct omap_gpio_s *s, int line, qemu_irq handler)
2968 if (line >= 16 || line < 0)
2969 cpu_abort(cpu_single_env, "%s: No GPIO line %i\n", __FUNCTION__, line);
2970 s->handler[line] = handler;
2973 /* MicroWire Interface */
2974 struct omap_uwire_s {
2975 target_phys_addr_t base;
2976 qemu_irq txirq;
2977 qemu_irq rxirq;
2978 qemu_irq txdrq;
2980 uint16_t txbuf;
2981 uint16_t rxbuf;
2982 uint16_t control;
2983 uint16_t setup[5];
2985 struct uwire_slave_s *chip[4];
2988 static void omap_uwire_transfer_start(struct omap_uwire_s *s)
2990 int chipselect = (s->control >> 10) & 3; /* INDEX */
2991 struct uwire_slave_s *slave = s->chip[chipselect];
2993 if ((s->control >> 5) & 0x1f) { /* NB_BITS_WR */
2994 if (s->control & (1 << 12)) /* CS_CMD */
2995 if (slave && slave->send)
2996 slave->send(slave->opaque,
2997 s->txbuf >> (16 - ((s->control >> 5) & 0x1f)));
2998 s->control &= ~(1 << 14); /* CSRB */
2999 /* TODO: depending on s->setup[4] bits [1:0] assert an IRQ or
3000 * a DRQ. When is the level IRQ supposed to be reset? */
3003 if ((s->control >> 0) & 0x1f) { /* NB_BITS_RD */
3004 if (s->control & (1 << 12)) /* CS_CMD */
3005 if (slave && slave->receive)
3006 s->rxbuf = slave->receive(slave->opaque);
3007 s->control |= 1 << 15; /* RDRB */
3008 /* TODO: depending on s->setup[4] bits [1:0] assert an IRQ or
3009 * a DRQ. When is the level IRQ supposed to be reset? */
3013 static uint32_t omap_uwire_read(void *opaque, target_phys_addr_t addr)
3015 struct omap_uwire_s *s = (struct omap_uwire_s *) opaque;
3016 int offset = addr & OMAP_MPUI_REG_MASK;
3018 switch (offset) {
3019 case 0x00: /* RDR */
3020 s->control &= ~(1 << 15); /* RDRB */
3021 return s->rxbuf;
3023 case 0x04: /* CSR */
3024 return s->control;
3026 case 0x08: /* SR1 */
3027 return s->setup[0];
3028 case 0x0c: /* SR2 */
3029 return s->setup[1];
3030 case 0x10: /* SR3 */
3031 return s->setup[2];
3032 case 0x14: /* SR4 */
3033 return s->setup[3];
3034 case 0x18: /* SR5 */
3035 return s->setup[4];
3038 OMAP_BAD_REG(addr);
3039 return 0;
3042 static void omap_uwire_write(void *opaque, target_phys_addr_t addr,
3043 uint32_t value)
3045 struct omap_uwire_s *s = (struct omap_uwire_s *) opaque;
3046 int offset = addr & OMAP_MPUI_REG_MASK;
3048 switch (offset) {
3049 case 0x00: /* TDR */
3050 s->txbuf = value; /* TD */
3051 if ((s->setup[4] & (1 << 2)) && /* AUTO_TX_EN */
3052 ((s->setup[4] & (1 << 3)) || /* CS_TOGGLE_TX_EN */
3053 (s->control & (1 << 12)))) { /* CS_CMD */
3054 s->control |= 1 << 14; /* CSRB */
3055 omap_uwire_transfer_start(s);
3057 break;
3059 case 0x04: /* CSR */
3060 s->control = value & 0x1fff;
3061 if (value & (1 << 13)) /* START */
3062 omap_uwire_transfer_start(s);
3063 break;
3065 case 0x08: /* SR1 */
3066 s->setup[0] = value & 0x003f;
3067 break;
3069 case 0x0c: /* SR2 */
3070 s->setup[1] = value & 0x0fc0;
3071 break;
3073 case 0x10: /* SR3 */
3074 s->setup[2] = value & 0x0003;
3075 break;
3077 case 0x14: /* SR4 */
3078 s->setup[3] = value & 0x0001;
3079 break;
3081 case 0x18: /* SR5 */
3082 s->setup[4] = value & 0x000f;
3083 break;
3085 default:
3086 OMAP_BAD_REG(addr);
3087 return;
3091 static CPUReadMemoryFunc *omap_uwire_readfn[] = {
3092 omap_badwidth_read16,
3093 omap_uwire_read,
3094 omap_badwidth_read16,
3097 static CPUWriteMemoryFunc *omap_uwire_writefn[] = {
3098 omap_badwidth_write16,
3099 omap_uwire_write,
3100 omap_badwidth_write16,
3103 static void omap_uwire_reset(struct omap_uwire_s *s)
3105 s->control = 0;
3106 s->setup[0] = 0;
3107 s->setup[1] = 0;
3108 s->setup[2] = 0;
3109 s->setup[3] = 0;
3110 s->setup[4] = 0;
3113 struct omap_uwire_s *omap_uwire_init(target_phys_addr_t base,
3114 qemu_irq *irq, qemu_irq dma, omap_clk clk)
3116 int iomemtype;
3117 struct omap_uwire_s *s = (struct omap_uwire_s *)
3118 qemu_mallocz(sizeof(struct omap_uwire_s));
3120 s->base = base;
3121 s->txirq = irq[0];
3122 s->rxirq = irq[1];
3123 s->txdrq = dma;
3124 omap_uwire_reset(s);
3126 iomemtype = cpu_register_io_memory(0, omap_uwire_readfn,
3127 omap_uwire_writefn, s);
3128 cpu_register_physical_memory(s->base, 0x800, iomemtype);
3130 return s;
3133 void omap_uwire_attach(struct omap_uwire_s *s,
3134 struct uwire_slave_s *slave, int chipselect)
3136 if (chipselect < 0 || chipselect > 3) {
3137 fprintf(stderr, "%s: Bad chipselect %i\n", __FUNCTION__, chipselect);
3138 exit(-1);
3141 s->chip[chipselect] = slave;
3144 /* Pseudonoise Pulse-Width Light Modulator */
3145 static void omap_pwl_update(struct omap_mpu_state_s *s)
3147 int output = (s->pwl.clk && s->pwl.enable) ? s->pwl.level : 0;
3149 if (output != s->pwl.output) {
3150 s->pwl.output = output;
3151 printf("%s: Backlight now at %i/256\n", __FUNCTION__, output);
3155 static uint32_t omap_pwl_read(void *opaque, target_phys_addr_t addr)
3157 struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
3158 int offset = addr & OMAP_MPUI_REG_MASK;
3160 switch (offset) {
3161 case 0x00: /* PWL_LEVEL */
3162 return s->pwl.level;
3163 case 0x04: /* PWL_CTRL */
3164 return s->pwl.enable;
3166 OMAP_BAD_REG(addr);
3167 return 0;
3170 static void omap_pwl_write(void *opaque, target_phys_addr_t addr,
3171 uint32_t value)
3173 struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
3174 int offset = addr & OMAP_MPUI_REG_MASK;
3176 switch (offset) {
3177 case 0x00: /* PWL_LEVEL */
3178 s->pwl.level = value;
3179 omap_pwl_update(s);
3180 break;
3181 case 0x04: /* PWL_CTRL */
3182 s->pwl.enable = value & 1;
3183 omap_pwl_update(s);
3184 break;
3185 default:
3186 OMAP_BAD_REG(addr);
3187 return;
3191 static CPUReadMemoryFunc *omap_pwl_readfn[] = {
3192 omap_pwl_read,
3193 omap_badwidth_read8,
3194 omap_badwidth_read8,
3197 static CPUWriteMemoryFunc *omap_pwl_writefn[] = {
3198 omap_pwl_write,
3199 omap_badwidth_write8,
3200 omap_badwidth_write8,
3203 static void omap_pwl_reset(struct omap_mpu_state_s *s)
3205 s->pwl.output = 0;
3206 s->pwl.level = 0;
3207 s->pwl.enable = 0;
3208 s->pwl.clk = 1;
3209 omap_pwl_update(s);
3212 static void omap_pwl_clk_update(void *opaque, int line, int on)
3214 struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
3216 s->pwl.clk = on;
3217 omap_pwl_update(s);
3220 static void omap_pwl_init(target_phys_addr_t base, struct omap_mpu_state_s *s,
3221 omap_clk clk)
3223 int iomemtype;
3225 omap_pwl_reset(s);
3227 iomemtype = cpu_register_io_memory(0, omap_pwl_readfn,
3228 omap_pwl_writefn, s);
3229 cpu_register_physical_memory(base, 0x800, iomemtype);
3231 omap_clk_adduser(clk, qemu_allocate_irqs(omap_pwl_clk_update, s, 1)[0]);
3234 /* Pulse-Width Tone module */
3235 static uint32_t omap_pwt_read(void *opaque, target_phys_addr_t addr)
3237 struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
3238 int offset = addr & OMAP_MPUI_REG_MASK;
3240 switch (offset) {
3241 case 0x00: /* FRC */
3242 return s->pwt.frc;
3243 case 0x04: /* VCR */
3244 return s->pwt.vrc;
3245 case 0x08: /* GCR */
3246 return s->pwt.gcr;
3248 OMAP_BAD_REG(addr);
3249 return 0;
3252 static void omap_pwt_write(void *opaque, target_phys_addr_t addr,
3253 uint32_t value)
3255 struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
3256 int offset = addr & OMAP_MPUI_REG_MASK;
3258 switch (offset) {
3259 case 0x00: /* FRC */
3260 s->pwt.frc = value & 0x3f;
3261 break;
3262 case 0x04: /* VRC */
3263 if ((value ^ s->pwt.vrc) & 1) {
3264 if (value & 1)
3265 printf("%s: %iHz buzz on\n", __FUNCTION__, (int)
3266 /* 1.5 MHz from a 12-MHz or 13-MHz PWT_CLK */
3267 ((omap_clk_getrate(s->pwt.clk) >> 3) /
3268 /* Pre-multiplexer divider */
3269 ((s->pwt.gcr & 2) ? 1 : 154) /
3270 /* Octave multiplexer */
3271 (2 << (value & 3)) *
3272 /* 101/107 divider */
3273 ((value & (1 << 2)) ? 101 : 107) *
3274 /* 49/55 divider */
3275 ((value & (1 << 3)) ? 49 : 55) *
3276 /* 50/63 divider */
3277 ((value & (1 << 4)) ? 50 : 63) *
3278 /* 80/127 divider */
3279 ((value & (1 << 5)) ? 80 : 127) /
3280 (107 * 55 * 63 * 127)));
3281 else
3282 printf("%s: silence!\n", __FUNCTION__);
3284 s->pwt.vrc = value & 0x7f;
3285 break;
3286 case 0x08: /* GCR */
3287 s->pwt.gcr = value & 3;
3288 break;
3289 default:
3290 OMAP_BAD_REG(addr);
3291 return;
3295 static CPUReadMemoryFunc *omap_pwt_readfn[] = {
3296 omap_pwt_read,
3297 omap_badwidth_read8,
3298 omap_badwidth_read8,
3301 static CPUWriteMemoryFunc *omap_pwt_writefn[] = {
3302 omap_pwt_write,
3303 omap_badwidth_write8,
3304 omap_badwidth_write8,
3307 static void omap_pwt_reset(struct omap_mpu_state_s *s)
3309 s->pwt.frc = 0;
3310 s->pwt.vrc = 0;
3311 s->pwt.gcr = 0;
3314 static void omap_pwt_init(target_phys_addr_t base, struct omap_mpu_state_s *s,
3315 omap_clk clk)
3317 int iomemtype;
3319 s->pwt.clk = clk;
3320 omap_pwt_reset(s);
3322 iomemtype = cpu_register_io_memory(0, omap_pwt_readfn,
3323 omap_pwt_writefn, s);
3324 cpu_register_physical_memory(base, 0x800, iomemtype);
3327 /* Real-time Clock module */
3328 struct omap_rtc_s {
3329 target_phys_addr_t base;
3330 qemu_irq irq;
3331 qemu_irq alarm;
3332 QEMUTimer *clk;
3334 uint8_t interrupts;
3335 uint8_t status;
3336 int16_t comp_reg;
3337 int running;
3338 int pm_am;
3339 int auto_comp;
3340 int round;
3341 struct tm alarm_tm;
3342 time_t alarm_ti;
3344 struct tm current_tm;
3345 time_t ti;
3346 uint64_t tick;
3349 static void omap_rtc_interrupts_update(struct omap_rtc_s *s)
3351 /* s->alarm is level-triggered */
3352 qemu_set_irq(s->alarm, (s->status >> 6) & 1);
3355 static void omap_rtc_alarm_update(struct omap_rtc_s *s)
3357 s->alarm_ti = mktime(&s->alarm_tm);
3358 if (s->alarm_ti == -1)
3359 printf("%s: conversion failed\n", __FUNCTION__);
3362 static inline uint8_t omap_rtc_bcd(int num)
3364 return ((num / 10) << 4) | (num % 10);
3367 static inline int omap_rtc_bin(uint8_t num)
3369 return (num & 15) + 10 * (num >> 4);
3372 static uint32_t omap_rtc_read(void *opaque, target_phys_addr_t addr)
3374 struct omap_rtc_s *s = (struct omap_rtc_s *) opaque;
3375 int offset = addr & OMAP_MPUI_REG_MASK;
3376 uint8_t i;
3378 switch (offset) {
3379 case 0x00: /* SECONDS_REG */
3380 return omap_rtc_bcd(s->current_tm.tm_sec);
3382 case 0x04: /* MINUTES_REG */
3383 return omap_rtc_bcd(s->current_tm.tm_min);
3385 case 0x08: /* HOURS_REG */
3386 if (s->pm_am)
3387 return ((s->current_tm.tm_hour > 11) << 7) |
3388 omap_rtc_bcd(((s->current_tm.tm_hour - 1) % 12) + 1);
3389 else
3390 return omap_rtc_bcd(s->current_tm.tm_hour);
3392 case 0x0c: /* DAYS_REG */
3393 return omap_rtc_bcd(s->current_tm.tm_mday);
3395 case 0x10: /* MONTHS_REG */
3396 return omap_rtc_bcd(s->current_tm.tm_mon + 1);
3398 case 0x14: /* YEARS_REG */
3399 return omap_rtc_bcd(s->current_tm.tm_year % 100);
3401 case 0x18: /* WEEK_REG */
3402 return s->current_tm.tm_wday;
3404 case 0x20: /* ALARM_SECONDS_REG */
3405 return omap_rtc_bcd(s->alarm_tm.tm_sec);
3407 case 0x24: /* ALARM_MINUTES_REG */
3408 return omap_rtc_bcd(s->alarm_tm.tm_min);
3410 case 0x28: /* ALARM_HOURS_REG */
3411 if (s->pm_am)
3412 return ((s->alarm_tm.tm_hour > 11) << 7) |
3413 omap_rtc_bcd(((s->alarm_tm.tm_hour - 1) % 12) + 1);
3414 else
3415 return omap_rtc_bcd(s->alarm_tm.tm_hour);
3417 case 0x2c: /* ALARM_DAYS_REG */
3418 return omap_rtc_bcd(s->alarm_tm.tm_mday);
3420 case 0x30: /* ALARM_MONTHS_REG */
3421 return omap_rtc_bcd(s->alarm_tm.tm_mon + 1);
3423 case 0x34: /* ALARM_YEARS_REG */
3424 return omap_rtc_bcd(s->alarm_tm.tm_year % 100);
3426 case 0x40: /* RTC_CTRL_REG */
3427 return (s->pm_am << 3) | (s->auto_comp << 2) |
3428 (s->round << 1) | s->running;
3430 case 0x44: /* RTC_STATUS_REG */
3431 i = s->status;
3432 s->status &= ~0x3d;
3433 return i;
3435 case 0x48: /* RTC_INTERRUPTS_REG */
3436 return s->interrupts;
3438 case 0x4c: /* RTC_COMP_LSB_REG */
3439 return ((uint16_t) s->comp_reg) & 0xff;
3441 case 0x50: /* RTC_COMP_MSB_REG */
3442 return ((uint16_t) s->comp_reg) >> 8;
3445 OMAP_BAD_REG(addr);
3446 return 0;
3449 static void omap_rtc_write(void *opaque, target_phys_addr_t addr,
3450 uint32_t value)
3452 struct omap_rtc_s *s = (struct omap_rtc_s *) opaque;
3453 int offset = addr & OMAP_MPUI_REG_MASK;
3454 struct tm new_tm;
3455 time_t ti[2];
3457 switch (offset) {
3458 case 0x00: /* SECONDS_REG */
3459 #if ALMDEBUG
3460 printf("RTC SEC_REG <-- %02x\n", value);
3461 #endif
3462 s->ti -= s->current_tm.tm_sec;
3463 s->ti += omap_rtc_bin(value);
3464 return;
3466 case 0x04: /* MINUTES_REG */
3467 #if ALMDEBUG
3468 printf("RTC MIN_REG <-- %02x\n", value);
3469 #endif
3470 s->ti -= s->current_tm.tm_min * 60;
3471 s->ti += omap_rtc_bin(value) * 60;
3472 return;
3474 case 0x08: /* HOURS_REG */
3475 #if ALMDEBUG
3476 printf("RTC HRS_REG <-- %02x\n", value);
3477 #endif
3478 s->ti -= s->current_tm.tm_hour * 3600;
3479 if (s->pm_am) {
3480 s->ti += (omap_rtc_bin(value & 0x3f) & 12) * 3600;
3481 s->ti += ((value >> 7) & 1) * 43200;
3482 } else
3483 s->ti += omap_rtc_bin(value & 0x3f) * 3600;
3484 return;
3486 case 0x0c: /* DAYS_REG */
3487 #if ALMDEBUG
3488 printf("RTC DAY_REG <-- %02x\n", value);
3489 #endif
3490 s->ti -= s->current_tm.tm_mday * 86400;
3491 s->ti += omap_rtc_bin(value) * 86400;
3492 return;
3494 case 0x10: /* MONTHS_REG */
3495 #if ALMDEBUG
3496 printf("RTC MTH_REG <-- %02x\n", value);
3497 #endif
3498 memcpy(&new_tm, &s->current_tm, sizeof(new_tm));
3499 new_tm.tm_mon = omap_rtc_bin(value);
3500 ti[0] = mktime(&s->current_tm);
3501 ti[1] = mktime(&new_tm);
3503 if (ti[0] != -1 && ti[1] != -1) {
3504 s->ti -= ti[0];
3505 s->ti += ti[1];
3506 } else {
3507 /* A less accurate version */
3508 s->ti -= s->current_tm.tm_mon * 2592000;
3509 s->ti += omap_rtc_bin(value) * 2592000;
3511 return;
3513 case 0x14: /* YEARS_REG */
3514 #if ALMDEBUG
3515 printf("RTC YRS_REG <-- %02x\n", value);
3516 #endif
3517 memcpy(&new_tm, &s->current_tm, sizeof(new_tm));
3518 new_tm.tm_year += omap_rtc_bin(value) - (new_tm.tm_year % 100);
3519 ti[0] = mktime(&s->current_tm);
3520 ti[1] = mktime(&new_tm);
3522 if (ti[0] != -1 && ti[1] != -1) {
3523 s->ti -= ti[0];
3524 s->ti += ti[1];
3525 } else {
3526 /* A less accurate version */
3527 s->ti -= (s->current_tm.tm_year % 100) * 31536000;
3528 s->ti += omap_rtc_bin(value) * 31536000;
3530 return;
3532 case 0x18: /* WEEK_REG */
3533 return; /* Ignored */
3535 case 0x20: /* ALARM_SECONDS_REG */
3536 #if ALMDEBUG
3537 printf("ALM SEC_REG <-- %02x\n", value);
3538 #endif
3539 s->alarm_tm.tm_sec = omap_rtc_bin(value);
3540 omap_rtc_alarm_update(s);
3541 return;
3543 case 0x24: /* ALARM_MINUTES_REG */
3544 #if ALMDEBUG
3545 printf("ALM MIN_REG <-- %02x\n", value);
3546 #endif
3547 s->alarm_tm.tm_min = omap_rtc_bin(value);
3548 omap_rtc_alarm_update(s);
3549 return;
3551 case 0x28: /* ALARM_HOURS_REG */
3552 #if ALMDEBUG
3553 printf("ALM HRS_REG <-- %02x\n", value);
3554 #endif
3555 if (s->pm_am)
3556 s->alarm_tm.tm_hour =
3557 ((omap_rtc_bin(value & 0x3f)) % 12) +
3558 ((value >> 7) & 1) * 12;
3559 else
3560 s->alarm_tm.tm_hour = omap_rtc_bin(value);
3561 omap_rtc_alarm_update(s);
3562 return;
3564 case 0x2c: /* ALARM_DAYS_REG */
3565 #if ALMDEBUG
3566 printf("ALM DAY_REG <-- %02x\n", value);
3567 #endif
3568 s->alarm_tm.tm_mday = omap_rtc_bin(value);
3569 omap_rtc_alarm_update(s);
3570 return;
3572 case 0x30: /* ALARM_MONTHS_REG */
3573 #if ALMDEBUG
3574 printf("ALM MON_REG <-- %02x\n", value);
3575 #endif
3576 s->alarm_tm.tm_mon = omap_rtc_bin(value);
3577 omap_rtc_alarm_update(s);
3578 return;
3580 case 0x34: /* ALARM_YEARS_REG */
3581 #if ALMDEBUG
3582 printf("ALM YRS_REG <-- %02x\n", value);
3583 #endif
3584 s->alarm_tm.tm_year = omap_rtc_bin(value);
3585 omap_rtc_alarm_update(s);
3586 return;
3588 case 0x40: /* RTC_CTRL_REG */
3589 #if ALMDEBUG
3590 printf("RTC CONTROL <-- %02x\n", value);
3591 #endif
3592 s->pm_am = (value >> 3) & 1;
3593 s->auto_comp = (value >> 2) & 1;
3594 s->round = (value >> 1) & 1;
3595 s->running = value & 1;
3596 s->status &= 0xfd;
3597 s->status |= s->running << 1;
3598 return;
3600 case 0x44: /* RTC_STATUS_REG */
3601 #if ALMDEBUG
3602 printf("RTC STATUSL <-- %02x\n", value);
3603 #endif
3604 s->status &= ~((value & 0xc0) ^ 0x80);
3605 omap_rtc_interrupts_update(s);
3606 return;
3608 case 0x48: /* RTC_INTERRUPTS_REG */
3609 #if ALMDEBUG
3610 printf("RTC INTRS <-- %02x\n", value);
3611 #endif
3612 s->interrupts = value;
3613 return;
3615 case 0x4c: /* RTC_COMP_LSB_REG */
3616 #if ALMDEBUG
3617 printf("RTC COMPLSB <-- %02x\n", value);
3618 #endif
3619 s->comp_reg &= 0xff00;
3620 s->comp_reg |= 0x00ff & value;
3621 return;
3623 case 0x50: /* RTC_COMP_MSB_REG */
3624 #if ALMDEBUG
3625 printf("RTC COMPMSB <-- %02x\n", value);
3626 #endif
3627 s->comp_reg &= 0x00ff;
3628 s->comp_reg |= 0xff00 & (value << 8);
3629 return;
3631 default:
3632 OMAP_BAD_REG(addr);
3633 return;
3637 static CPUReadMemoryFunc *omap_rtc_readfn[] = {
3638 omap_rtc_read,
3639 omap_badwidth_read8,
3640 omap_badwidth_read8,
3643 static CPUWriteMemoryFunc *omap_rtc_writefn[] = {
3644 omap_rtc_write,
3645 omap_badwidth_write8,
3646 omap_badwidth_write8,
3649 static void omap_rtc_tick(void *opaque)
3651 struct omap_rtc_s *s = opaque;
3653 if (s->round) {
3654 /* Round to nearest full minute. */
3655 if (s->current_tm.tm_sec < 30)
3656 s->ti -= s->current_tm.tm_sec;
3657 else
3658 s->ti += 60 - s->current_tm.tm_sec;
3660 s->round = 0;
3663 memcpy(&s->current_tm, localtime(&s->ti), sizeof(s->current_tm));
3665 if ((s->interrupts & 0x08) && s->ti == s->alarm_ti) {
3666 s->status |= 0x40;
3667 omap_rtc_interrupts_update(s);
3670 if (s->interrupts & 0x04)
3671 switch (s->interrupts & 3) {
3672 case 0:
3673 s->status |= 0x04;
3674 qemu_irq_pulse(s->irq);
3675 break;
3676 case 1:
3677 if (s->current_tm.tm_sec)
3678 break;
3679 s->status |= 0x08;
3680 qemu_irq_pulse(s->irq);
3681 break;
3682 case 2:
3683 if (s->current_tm.tm_sec || s->current_tm.tm_min)
3684 break;
3685 s->status |= 0x10;
3686 qemu_irq_pulse(s->irq);
3687 break;
3688 case 3:
3689 if (s->current_tm.tm_sec ||
3690 s->current_tm.tm_min || s->current_tm.tm_hour)
3691 break;
3692 s->status |= 0x20;
3693 qemu_irq_pulse(s->irq);
3694 break;
3697 /* Move on */
3698 if (s->running)
3699 s->ti ++;
3700 s->tick += 1000;
3703 * Every full hour add a rough approximation of the compensation
3704 * register to the 32kHz Timer (which drives the RTC) value.
3706 if (s->auto_comp && !s->current_tm.tm_sec && !s->current_tm.tm_min)
3707 s->tick += s->comp_reg * 1000 / 32768;
3709 qemu_mod_timer(s->clk, s->tick);
3712 static void omap_rtc_reset(struct omap_rtc_s *s)
3714 struct tm tm;
3716 s->interrupts = 0;
3717 s->comp_reg = 0;
3718 s->running = 0;
3719 s->pm_am = 0;
3720 s->auto_comp = 0;
3721 s->round = 0;
3722 s->tick = qemu_get_clock(rt_clock);
3723 memset(&s->alarm_tm, 0, sizeof(s->alarm_tm));
3724 s->alarm_tm.tm_mday = 0x01;
3725 s->status = 1 << 7;
3726 qemu_get_timedate(&tm, 0);
3727 s->ti = mktime(&tm);
3729 omap_rtc_alarm_update(s);
3730 omap_rtc_tick(s);
3733 struct omap_rtc_s *omap_rtc_init(target_phys_addr_t base,
3734 qemu_irq *irq, omap_clk clk)
3736 int iomemtype;
3737 struct omap_rtc_s *s = (struct omap_rtc_s *)
3738 qemu_mallocz(sizeof(struct omap_rtc_s));
3740 s->base = base;
3741 s->irq = irq[0];
3742 s->alarm = irq[1];
3743 s->clk = qemu_new_timer(rt_clock, omap_rtc_tick, s);
3745 omap_rtc_reset(s);
3747 iomemtype = cpu_register_io_memory(0, omap_rtc_readfn,
3748 omap_rtc_writefn, s);
3749 cpu_register_physical_memory(s->base, 0x800, iomemtype);
3751 return s;
3754 /* Multi-channel Buffered Serial Port interfaces */
3755 struct omap_mcbsp_s {
3756 target_phys_addr_t base;
3757 qemu_irq txirq;
3758 qemu_irq rxirq;
3759 qemu_irq txdrq;
3760 qemu_irq rxdrq;
3762 uint16_t spcr[2];
3763 uint16_t rcr[2];
3764 uint16_t xcr[2];
3765 uint16_t srgr[2];
3766 uint16_t mcr[2];
3767 uint16_t pcr;
3768 uint16_t rcer[8];
3769 uint16_t xcer[8];
3770 int tx_rate;
3771 int rx_rate;
3772 int tx_req;
3773 int rx_req;
3775 struct i2s_codec_s *codec;
3776 QEMUTimer *source_timer;
3777 QEMUTimer *sink_timer;
3780 static void omap_mcbsp_intr_update(struct omap_mcbsp_s *s)
3782 int irq;
3784 switch ((s->spcr[0] >> 4) & 3) { /* RINTM */
3785 case 0:
3786 irq = (s->spcr[0] >> 1) & 1; /* RRDY */
3787 break;
3788 case 3:
3789 irq = (s->spcr[0] >> 3) & 1; /* RSYNCERR */
3790 break;
3791 default:
3792 irq = 0;
3793 break;
3796 if (irq)
3797 qemu_irq_pulse(s->rxirq);
3799 switch ((s->spcr[1] >> 4) & 3) { /* XINTM */
3800 case 0:
3801 irq = (s->spcr[1] >> 1) & 1; /* XRDY */
3802 break;
3803 case 3:
3804 irq = (s->spcr[1] >> 3) & 1; /* XSYNCERR */
3805 break;
3806 default:
3807 irq = 0;
3808 break;
3811 if (irq)
3812 qemu_irq_pulse(s->txirq);
3815 static void omap_mcbsp_rx_newdata(struct omap_mcbsp_s *s)
3817 if ((s->spcr[0] >> 1) & 1) /* RRDY */
3818 s->spcr[0] |= 1 << 2; /* RFULL */
3819 s->spcr[0] |= 1 << 1; /* RRDY */
3820 qemu_irq_raise(s->rxdrq);
3821 omap_mcbsp_intr_update(s);
3824 static void omap_mcbsp_source_tick(void *opaque)
3826 struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;
3827 static const int bps[8] = { 0, 1, 1, 2, 2, 2, -255, -255 };
3829 if (!s->rx_rate)
3830 return;
3831 if (s->rx_req)
3832 printf("%s: Rx FIFO overrun\n", __FUNCTION__);
3834 s->rx_req = s->rx_rate << bps[(s->rcr[0] >> 5) & 7];
3836 omap_mcbsp_rx_newdata(s);
3837 qemu_mod_timer(s->source_timer, qemu_get_clock(vm_clock) + ticks_per_sec);
3840 static void omap_mcbsp_rx_start(struct omap_mcbsp_s *s)
3842 if (!s->codec || !s->codec->rts)
3843 omap_mcbsp_source_tick(s);
3844 else if (s->codec->in.len) {
3845 s->rx_req = s->codec->in.len;
3846 omap_mcbsp_rx_newdata(s);
3850 static void omap_mcbsp_rx_stop(struct omap_mcbsp_s *s)
3852 qemu_del_timer(s->source_timer);
3855 static void omap_mcbsp_rx_done(struct omap_mcbsp_s *s)
3857 s->spcr[0] &= ~(1 << 1); /* RRDY */
3858 qemu_irq_lower(s->rxdrq);
3859 omap_mcbsp_intr_update(s);
3862 static void omap_mcbsp_tx_newdata(struct omap_mcbsp_s *s)
3864 s->spcr[1] |= 1 << 1; /* XRDY */
3865 qemu_irq_raise(s->txdrq);
3866 omap_mcbsp_intr_update(s);
3869 static void omap_mcbsp_sink_tick(void *opaque)
3871 struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;
3872 static const int bps[8] = { 0, 1, 1, 2, 2, 2, -255, -255 };
3874 if (!s->tx_rate)
3875 return;
3876 if (s->tx_req)
3877 printf("%s: Tx FIFO underrun\n", __FUNCTION__);
3879 s->tx_req = s->tx_rate << bps[(s->xcr[0] >> 5) & 7];
3881 omap_mcbsp_tx_newdata(s);
3882 qemu_mod_timer(s->sink_timer, qemu_get_clock(vm_clock) + ticks_per_sec);
3885 static void omap_mcbsp_tx_start(struct omap_mcbsp_s *s)
3887 if (!s->codec || !s->codec->cts)
3888 omap_mcbsp_sink_tick(s);
3889 else if (s->codec->out.size) {
3890 s->tx_req = s->codec->out.size;
3891 omap_mcbsp_tx_newdata(s);
3895 static void omap_mcbsp_tx_done(struct omap_mcbsp_s *s)
3897 s->spcr[1] &= ~(1 << 1); /* XRDY */
3898 qemu_irq_lower(s->txdrq);
3899 omap_mcbsp_intr_update(s);
3900 if (s->codec && s->codec->cts)
3901 s->codec->tx_swallow(s->codec->opaque);
3904 static void omap_mcbsp_tx_stop(struct omap_mcbsp_s *s)
3906 s->tx_req = 0;
3907 omap_mcbsp_tx_done(s);
3908 qemu_del_timer(s->sink_timer);
3911 static void omap_mcbsp_req_update(struct omap_mcbsp_s *s)
3913 int prev_rx_rate, prev_tx_rate;
3914 int rx_rate = 0, tx_rate = 0;
3915 int cpu_rate = 1500000; /* XXX */
3917 /* TODO: check CLKSTP bit */
3918 if (s->spcr[1] & (1 << 6)) { /* GRST */
3919 if (s->spcr[0] & (1 << 0)) { /* RRST */
3920 if ((s->srgr[1] & (1 << 13)) && /* CLKSM */
3921 (s->pcr & (1 << 8))) { /* CLKRM */
3922 if (~s->pcr & (1 << 7)) /* SCLKME */
3923 rx_rate = cpu_rate /
3924 ((s->srgr[0] & 0xff) + 1); /* CLKGDV */
3925 } else
3926 if (s->codec)
3927 rx_rate = s->codec->rx_rate;
3930 if (s->spcr[1] & (1 << 0)) { /* XRST */
3931 if ((s->srgr[1] & (1 << 13)) && /* CLKSM */
3932 (s->pcr & (1 << 9))) { /* CLKXM */
3933 if (~s->pcr & (1 << 7)) /* SCLKME */
3934 tx_rate = cpu_rate /
3935 ((s->srgr[0] & 0xff) + 1); /* CLKGDV */
3936 } else
3937 if (s->codec)
3938 tx_rate = s->codec->tx_rate;
3941 prev_tx_rate = s->tx_rate;
3942 prev_rx_rate = s->rx_rate;
3943 s->tx_rate = tx_rate;
3944 s->rx_rate = rx_rate;
3946 if (s->codec)
3947 s->codec->set_rate(s->codec->opaque, rx_rate, tx_rate);
3949 if (!prev_tx_rate && tx_rate)
3950 omap_mcbsp_tx_start(s);
3951 else if (s->tx_rate && !tx_rate)
3952 omap_mcbsp_tx_stop(s);
3954 if (!prev_rx_rate && rx_rate)
3955 omap_mcbsp_rx_start(s);
3956 else if (prev_tx_rate && !tx_rate)
3957 omap_mcbsp_rx_stop(s);
3960 static uint32_t omap_mcbsp_read(void *opaque, target_phys_addr_t addr)
3962 struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;
3963 int offset = addr & OMAP_MPUI_REG_MASK;
3964 uint16_t ret;
3966 switch (offset) {
3967 case 0x00: /* DRR2 */
3968 if (((s->rcr[0] >> 5) & 7) < 3) /* RWDLEN1 */
3969 return 0x0000;
3970 /* Fall through. */
3971 case 0x02: /* DRR1 */
3972 if (s->rx_req < 2) {
3973 printf("%s: Rx FIFO underrun\n", __FUNCTION__);
3974 omap_mcbsp_rx_done(s);
3975 } else {
3976 s->tx_req -= 2;
3977 if (s->codec && s->codec->in.len >= 2) {
3978 ret = s->codec->in.fifo[s->codec->in.start ++] << 8;
3979 ret |= s->codec->in.fifo[s->codec->in.start ++];
3980 s->codec->in.len -= 2;
3981 } else
3982 ret = 0x0000;
3983 if (!s->tx_req)
3984 omap_mcbsp_rx_done(s);
3985 return ret;
3987 return 0x0000;
3989 case 0x04: /* DXR2 */
3990 case 0x06: /* DXR1 */
3991 return 0x0000;
3993 case 0x08: /* SPCR2 */
3994 return s->spcr[1];
3995 case 0x0a: /* SPCR1 */
3996 return s->spcr[0];
3997 case 0x0c: /* RCR2 */
3998 return s->rcr[1];
3999 case 0x0e: /* RCR1 */
4000 return s->rcr[0];
4001 case 0x10: /* XCR2 */
4002 return s->xcr[1];
4003 case 0x12: /* XCR1 */
4004 return s->xcr[0];
4005 case 0x14: /* SRGR2 */
4006 return s->srgr[1];
4007 case 0x16: /* SRGR1 */
4008 return s->srgr[0];
4009 case 0x18: /* MCR2 */
4010 return s->mcr[1];
4011 case 0x1a: /* MCR1 */
4012 return s->mcr[0];
4013 case 0x1c: /* RCERA */
4014 return s->rcer[0];
4015 case 0x1e: /* RCERB */
4016 return s->rcer[1];
4017 case 0x20: /* XCERA */
4018 return s->xcer[0];
4019 case 0x22: /* XCERB */
4020 return s->xcer[1];
4021 case 0x24: /* PCR0 */
4022 return s->pcr;
4023 case 0x26: /* RCERC */
4024 return s->rcer[2];
4025 case 0x28: /* RCERD */
4026 return s->rcer[3];
4027 case 0x2a: /* XCERC */
4028 return s->xcer[2];
4029 case 0x2c: /* XCERD */
4030 return s->xcer[3];
4031 case 0x2e: /* RCERE */
4032 return s->rcer[4];
4033 case 0x30: /* RCERF */
4034 return s->rcer[5];
4035 case 0x32: /* XCERE */
4036 return s->xcer[4];
4037 case 0x34: /* XCERF */
4038 return s->xcer[5];
4039 case 0x36: /* RCERG */
4040 return s->rcer[6];
4041 case 0x38: /* RCERH */
4042 return s->rcer[7];
4043 case 0x3a: /* XCERG */
4044 return s->xcer[6];
4045 case 0x3c: /* XCERH */
4046 return s->xcer[7];
4049 OMAP_BAD_REG(addr);
4050 return 0;
4053 static void omap_mcbsp_writeh(void *opaque, target_phys_addr_t addr,
4054 uint32_t value)
4056 struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;
4057 int offset = addr & OMAP_MPUI_REG_MASK;
4059 switch (offset) {
4060 case 0x00: /* DRR2 */
4061 case 0x02: /* DRR1 */
4062 OMAP_RO_REG(addr);
4063 return;
4065 case 0x04: /* DXR2 */
4066 if (((s->xcr[0] >> 5) & 7) < 3) /* XWDLEN1 */
4067 return;
4068 /* Fall through. */
4069 case 0x06: /* DXR1 */
4070 if (s->tx_req > 1) {
4071 s->tx_req -= 2;
4072 if (s->codec && s->codec->cts) {
4073 s->codec->out.fifo[s->codec->out.len ++] = (value >> 8) & 0xff;
4074 s->codec->out.fifo[s->codec->out.len ++] = (value >> 0) & 0xff;
4076 if (s->tx_req < 2)
4077 omap_mcbsp_tx_done(s);
4078 } else
4079 printf("%s: Tx FIFO overrun\n", __FUNCTION__);
4080 return;
4082 case 0x08: /* SPCR2 */
4083 s->spcr[1] &= 0x0002;
4084 s->spcr[1] |= 0x03f9 & value;
4085 s->spcr[1] |= 0x0004 & (value << 2); /* XEMPTY := XRST */
4086 if (~value & 1) /* XRST */
4087 s->spcr[1] &= ~6;
4088 omap_mcbsp_req_update(s);
4089 return;
4090 case 0x0a: /* SPCR1 */
4091 s->spcr[0] &= 0x0006;
4092 s->spcr[0] |= 0xf8f9 & value;
4093 if (value & (1 << 15)) /* DLB */
4094 printf("%s: Digital Loopback mode enable attempt\n", __FUNCTION__);
4095 if (~value & 1) { /* RRST */
4096 s->spcr[0] &= ~6;
4097 s->rx_req = 0;
4098 omap_mcbsp_rx_done(s);
4100 omap_mcbsp_req_update(s);
4101 return;
4103 case 0x0c: /* RCR2 */
4104 s->rcr[1] = value & 0xffff;
4105 return;
4106 case 0x0e: /* RCR1 */
4107 s->rcr[0] = value & 0x7fe0;
4108 return;
4109 case 0x10: /* XCR2 */
4110 s->xcr[1] = value & 0xffff;
4111 return;
4112 case 0x12: /* XCR1 */
4113 s->xcr[0] = value & 0x7fe0;
4114 return;
4115 case 0x14: /* SRGR2 */
4116 s->srgr[1] = value & 0xffff;
4117 omap_mcbsp_req_update(s);
4118 return;
4119 case 0x16: /* SRGR1 */
4120 s->srgr[0] = value & 0xffff;
4121 omap_mcbsp_req_update(s);
4122 return;
4123 case 0x18: /* MCR2 */
4124 s->mcr[1] = value & 0x03e3;
4125 if (value & 3) /* XMCM */
4126 printf("%s: Tx channel selection mode enable attempt\n",
4127 __FUNCTION__);
4128 return;
4129 case 0x1a: /* MCR1 */
4130 s->mcr[0] = value & 0x03e1;
4131 if (value & 1) /* RMCM */
4132 printf("%s: Rx channel selection mode enable attempt\n",
4133 __FUNCTION__);
4134 return;
4135 case 0x1c: /* RCERA */
4136 s->rcer[0] = value & 0xffff;
4137 return;
4138 case 0x1e: /* RCERB */
4139 s->rcer[1] = value & 0xffff;
4140 return;
4141 case 0x20: /* XCERA */
4142 s->xcer[0] = value & 0xffff;
4143 return;
4144 case 0x22: /* XCERB */
4145 s->xcer[1] = value & 0xffff;
4146 return;
4147 case 0x24: /* PCR0 */
4148 s->pcr = value & 0x7faf;
4149 return;
4150 case 0x26: /* RCERC */
4151 s->rcer[2] = value & 0xffff;
4152 return;
4153 case 0x28: /* RCERD */
4154 s->rcer[3] = value & 0xffff;
4155 return;
4156 case 0x2a: /* XCERC */
4157 s->xcer[2] = value & 0xffff;
4158 return;
4159 case 0x2c: /* XCERD */
4160 s->xcer[3] = value & 0xffff;
4161 return;
4162 case 0x2e: /* RCERE */
4163 s->rcer[4] = value & 0xffff;
4164 return;
4165 case 0x30: /* RCERF */
4166 s->rcer[5] = value & 0xffff;
4167 return;
4168 case 0x32: /* XCERE */
4169 s->xcer[4] = value & 0xffff;
4170 return;
4171 case 0x34: /* XCERF */
4172 s->xcer[5] = value & 0xffff;
4173 return;
4174 case 0x36: /* RCERG */
4175 s->rcer[6] = value & 0xffff;
4176 return;
4177 case 0x38: /* RCERH */
4178 s->rcer[7] = value & 0xffff;
4179 return;
4180 case 0x3a: /* XCERG */
4181 s->xcer[6] = value & 0xffff;
4182 return;
4183 case 0x3c: /* XCERH */
4184 s->xcer[7] = value & 0xffff;
4185 return;
4188 OMAP_BAD_REG(addr);
4191 static void omap_mcbsp_writew(void *opaque, target_phys_addr_t addr,
4192 uint32_t value)
4194 struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;
4195 int offset = addr & OMAP_MPUI_REG_MASK;
4197 if (offset == 0x04) { /* DXR */
4198 if (((s->xcr[0] >> 5) & 7) < 3) /* XWDLEN1 */
4199 return;
4200 if (s->tx_req > 3) {
4201 s->tx_req -= 4;
4202 if (s->codec && s->codec->cts) {
4203 s->codec->out.fifo[s->codec->out.len ++] =
4204 (value >> 24) & 0xff;
4205 s->codec->out.fifo[s->codec->out.len ++] =
4206 (value >> 16) & 0xff;
4207 s->codec->out.fifo[s->codec->out.len ++] =
4208 (value >> 8) & 0xff;
4209 s->codec->out.fifo[s->codec->out.len ++] =
4210 (value >> 0) & 0xff;
4212 if (s->tx_req < 4)
4213 omap_mcbsp_tx_done(s);
4214 } else
4215 printf("%s: Tx FIFO overrun\n", __FUNCTION__);
4216 return;
4219 omap_badwidth_write16(opaque, addr, value);
4222 static CPUReadMemoryFunc *omap_mcbsp_readfn[] = {
4223 omap_badwidth_read16,
4224 omap_mcbsp_read,
4225 omap_badwidth_read16,
4228 static CPUWriteMemoryFunc *omap_mcbsp_writefn[] = {
4229 omap_badwidth_write16,
4230 omap_mcbsp_writeh,
4231 omap_mcbsp_writew,
4234 static void omap_mcbsp_reset(struct omap_mcbsp_s *s)
4236 memset(&s->spcr, 0, sizeof(s->spcr));
4237 memset(&s->rcr, 0, sizeof(s->rcr));
4238 memset(&s->xcr, 0, sizeof(s->xcr));
4239 s->srgr[0] = 0x0001;
4240 s->srgr[1] = 0x2000;
4241 memset(&s->mcr, 0, sizeof(s->mcr));
4242 memset(&s->pcr, 0, sizeof(s->pcr));
4243 memset(&s->rcer, 0, sizeof(s->rcer));
4244 memset(&s->xcer, 0, sizeof(s->xcer));
4245 s->tx_req = 0;
4246 s->rx_req = 0;
4247 s->tx_rate = 0;
4248 s->rx_rate = 0;
4249 qemu_del_timer(s->source_timer);
4250 qemu_del_timer(s->sink_timer);
4253 struct omap_mcbsp_s *omap_mcbsp_init(target_phys_addr_t base,
4254 qemu_irq *irq, qemu_irq *dma, omap_clk clk)
4256 int iomemtype;
4257 struct omap_mcbsp_s *s = (struct omap_mcbsp_s *)
4258 qemu_mallocz(sizeof(struct omap_mcbsp_s));
4260 s->base = base;
4261 s->txirq = irq[0];
4262 s->rxirq = irq[1];
4263 s->txdrq = dma[0];
4264 s->rxdrq = dma[1];
4265 s->sink_timer = qemu_new_timer(vm_clock, omap_mcbsp_sink_tick, s);
4266 s->source_timer = qemu_new_timer(vm_clock, omap_mcbsp_source_tick, s);
4267 omap_mcbsp_reset(s);
4269 iomemtype = cpu_register_io_memory(0, omap_mcbsp_readfn,
4270 omap_mcbsp_writefn, s);
4271 cpu_register_physical_memory(s->base, 0x800, iomemtype);
4273 return s;
4276 static void omap_mcbsp_i2s_swallow(void *opaque, int line, int level)
4278 struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;
4280 if (s->rx_rate) {
4281 s->rx_req = s->codec->in.len;
4282 omap_mcbsp_rx_newdata(s);
4286 static void omap_mcbsp_i2s_start(void *opaque, int line, int level)
4288 struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;
4290 if (s->tx_rate) {
4291 s->tx_req = s->codec->out.size;
4292 omap_mcbsp_tx_newdata(s);
4296 void omap_mcbsp_i2s_attach(struct omap_mcbsp_s *s, struct i2s_codec_s *slave)
4298 s->codec = slave;
4299 slave->rx_swallow = qemu_allocate_irqs(omap_mcbsp_i2s_swallow, s, 1)[0];
4300 slave->tx_start = qemu_allocate_irqs(omap_mcbsp_i2s_start, s, 1)[0];
4303 /* LED Pulse Generators */
4304 struct omap_lpg_s {
4305 target_phys_addr_t base;
4306 QEMUTimer *tm;
4308 uint8_t control;
4309 uint8_t power;
4310 int64_t on;
4311 int64_t period;
4312 int clk;
4313 int cycle;
4316 static void omap_lpg_tick(void *opaque)
4318 struct omap_lpg_s *s = opaque;
4320 if (s->cycle)
4321 qemu_mod_timer(s->tm, qemu_get_clock(rt_clock) + s->period - s->on);
4322 else
4323 qemu_mod_timer(s->tm, qemu_get_clock(rt_clock) + s->on);
4325 s->cycle = !s->cycle;
4326 printf("%s: LED is %s\n", __FUNCTION__, s->cycle ? "on" : "off");
4329 static void omap_lpg_update(struct omap_lpg_s *s)
4331 int64_t on, period = 1, ticks = 1000;
4332 static const int per[8] = { 1, 2, 4, 8, 12, 16, 20, 24 };
4334 if (~s->control & (1 << 6)) /* LPGRES */
4335 on = 0;
4336 else if (s->control & (1 << 7)) /* PERM_ON */
4337 on = period;
4338 else {
4339 period = muldiv64(ticks, per[s->control & 7], /* PERCTRL */
4340 256 / 32);
4341 on = (s->clk && s->power) ? muldiv64(ticks,
4342 per[(s->control >> 3) & 7], 256) : 0; /* ONCTRL */
4345 qemu_del_timer(s->tm);
4346 if (on == period && s->on < s->period)
4347 printf("%s: LED is on\n", __FUNCTION__);
4348 else if (on == 0 && s->on)
4349 printf("%s: LED is off\n", __FUNCTION__);
4350 else if (on && (on != s->on || period != s->period)) {
4351 s->cycle = 0;
4352 s->on = on;
4353 s->period = period;
4354 omap_lpg_tick(s);
4355 return;
4358 s->on = on;
4359 s->period = period;
4362 static void omap_lpg_reset(struct omap_lpg_s *s)
4364 s->control = 0x00;
4365 s->power = 0x00;
4366 s->clk = 1;
4367 omap_lpg_update(s);
4370 static uint32_t omap_lpg_read(void *opaque, target_phys_addr_t addr)
4372 struct omap_lpg_s *s = (struct omap_lpg_s *) opaque;
4373 int offset = addr & OMAP_MPUI_REG_MASK;
4375 switch (offset) {
4376 case 0x00: /* LCR */
4377 return s->control;
4379 case 0x04: /* PMR */
4380 return s->power;
4383 OMAP_BAD_REG(addr);
4384 return 0;
4387 static void omap_lpg_write(void *opaque, target_phys_addr_t addr,
4388 uint32_t value)
4390 struct omap_lpg_s *s = (struct omap_lpg_s *) opaque;
4391 int offset = addr & OMAP_MPUI_REG_MASK;
4393 switch (offset) {
4394 case 0x00: /* LCR */
4395 if (~value & (1 << 6)) /* LPGRES */
4396 omap_lpg_reset(s);
4397 s->control = value & 0xff;
4398 omap_lpg_update(s);
4399 return;
4401 case 0x04: /* PMR */
4402 s->power = value & 0x01;
4403 omap_lpg_update(s);
4404 return;
4406 default:
4407 OMAP_BAD_REG(addr);
4408 return;
4412 static CPUReadMemoryFunc *omap_lpg_readfn[] = {
4413 omap_lpg_read,
4414 omap_badwidth_read8,
4415 omap_badwidth_read8,
4418 static CPUWriteMemoryFunc *omap_lpg_writefn[] = {
4419 omap_lpg_write,
4420 omap_badwidth_write8,
4421 omap_badwidth_write8,
4424 static void omap_lpg_clk_update(void *opaque, int line, int on)
4426 struct omap_lpg_s *s = (struct omap_lpg_s *) opaque;
4428 s->clk = on;
4429 omap_lpg_update(s);
4432 struct omap_lpg_s *omap_lpg_init(target_phys_addr_t base, omap_clk clk)
4434 int iomemtype;
4435 struct omap_lpg_s *s = (struct omap_lpg_s *)
4436 qemu_mallocz(sizeof(struct omap_lpg_s));
4438 s->base = base;
4439 s->tm = qemu_new_timer(rt_clock, omap_lpg_tick, s);
4441 omap_lpg_reset(s);
4443 iomemtype = cpu_register_io_memory(0, omap_lpg_readfn,
4444 omap_lpg_writefn, s);
4445 cpu_register_physical_memory(s->base, 0x800, iomemtype);
4447 omap_clk_adduser(clk, qemu_allocate_irqs(omap_lpg_clk_update, s, 1)[0]);
4449 return s;
4452 /* MPUI Peripheral Bridge configuration */
4453 static uint32_t omap_mpui_io_read(void *opaque, target_phys_addr_t addr)
4455 if (addr == OMAP_MPUI_BASE) /* CMR */
4456 return 0xfe4d;
4458 OMAP_BAD_REG(addr);
4459 return 0;
4462 static CPUReadMemoryFunc *omap_mpui_io_readfn[] = {
4463 omap_badwidth_read16,
4464 omap_mpui_io_read,
4465 omap_badwidth_read16,
4468 static CPUWriteMemoryFunc *omap_mpui_io_writefn[] = {
4469 omap_badwidth_write16,
4470 omap_badwidth_write16,
4471 omap_badwidth_write16,
4474 static void omap_setup_mpui_io(struct omap_mpu_state_s *mpu)
4476 int iomemtype = cpu_register_io_memory(0, omap_mpui_io_readfn,
4477 omap_mpui_io_writefn, mpu);
4478 cpu_register_physical_memory(OMAP_MPUI_BASE, 0x7fff, iomemtype);
4481 /* General chip reset */
4482 static void omap1_mpu_reset(void *opaque)
4484 struct omap_mpu_state_s *mpu = (struct omap_mpu_state_s *) opaque;
4486 omap_inth_reset(mpu->ih[0]);
4487 omap_inth_reset(mpu->ih[1]);
4488 omap_dma_reset(mpu->dma);
4489 omap_mpu_timer_reset(mpu->timer[0]);
4490 omap_mpu_timer_reset(mpu->timer[1]);
4491 omap_mpu_timer_reset(mpu->timer[2]);
4492 omap_wd_timer_reset(mpu->wdt);
4493 omap_os_timer_reset(mpu->os_timer);
4494 omap_lcdc_reset(mpu->lcd);
4495 omap_ulpd_pm_reset(mpu);
4496 omap_pin_cfg_reset(mpu);
4497 omap_mpui_reset(mpu);
4498 omap_tipb_bridge_reset(mpu->private_tipb);
4499 omap_tipb_bridge_reset(mpu->public_tipb);
4500 omap_dpll_reset(&mpu->dpll[0]);
4501 omap_dpll_reset(&mpu->dpll[1]);
4502 omap_dpll_reset(&mpu->dpll[2]);
4503 omap_uart_reset(mpu->uart[0]);
4504 omap_uart_reset(mpu->uart[1]);
4505 omap_uart_reset(mpu->uart[2]);
4506 omap_mmc_reset(mpu->mmc);
4507 omap_mpuio_reset(mpu->mpuio);
4508 omap_gpio_reset(mpu->gpio);
4509 omap_uwire_reset(mpu->microwire);
4510 omap_pwl_reset(mpu);
4511 omap_pwt_reset(mpu);
4512 omap_i2c_reset(mpu->i2c[0]);
4513 omap_rtc_reset(mpu->rtc);
4514 omap_mcbsp_reset(mpu->mcbsp1);
4515 omap_mcbsp_reset(mpu->mcbsp2);
4516 omap_mcbsp_reset(mpu->mcbsp3);
4517 omap_lpg_reset(mpu->led[0]);
4518 omap_lpg_reset(mpu->led[1]);
4519 omap_clkm_reset(mpu);
4520 cpu_reset(mpu->env);
4523 static const struct omap_map_s {
4524 target_phys_addr_t phys_dsp;
4525 target_phys_addr_t phys_mpu;
4526 uint32_t size;
4527 const char *name;
4528 } omap15xx_dsp_mm[] = {
4529 /* Strobe 0 */
4530 { 0xe1010000, 0xfffb0000, 0x800, "UART1 BT" }, /* CS0 */
4531 { 0xe1010800, 0xfffb0800, 0x800, "UART2 COM" }, /* CS1 */
4532 { 0xe1011800, 0xfffb1800, 0x800, "McBSP1 audio" }, /* CS3 */
4533 { 0xe1012000, 0xfffb2000, 0x800, "MCSI2 communication" }, /* CS4 */
4534 { 0xe1012800, 0xfffb2800, 0x800, "MCSI1 BT u-Law" }, /* CS5 */
4535 { 0xe1013000, 0xfffb3000, 0x800, "uWire" }, /* CS6 */
4536 { 0xe1013800, 0xfffb3800, 0x800, "I^2C" }, /* CS7 */
4537 { 0xe1014000, 0xfffb4000, 0x800, "USB W2FC" }, /* CS8 */
4538 { 0xe1014800, 0xfffb4800, 0x800, "RTC" }, /* CS9 */
4539 { 0xe1015000, 0xfffb5000, 0x800, "MPUIO" }, /* CS10 */
4540 { 0xe1015800, 0xfffb5800, 0x800, "PWL" }, /* CS11 */
4541 { 0xe1016000, 0xfffb6000, 0x800, "PWT" }, /* CS12 */
4542 { 0xe1017000, 0xfffb7000, 0x800, "McBSP3" }, /* CS14 */
4543 { 0xe1017800, 0xfffb7800, 0x800, "MMC" }, /* CS15 */
4544 { 0xe1019000, 0xfffb9000, 0x800, "32-kHz timer" }, /* CS18 */
4545 { 0xe1019800, 0xfffb9800, 0x800, "UART3" }, /* CS19 */
4546 { 0xe101c800, 0xfffbc800, 0x800, "TIPB switches" }, /* CS25 */
4547 /* Strobe 1 */
4548 { 0xe101e000, 0xfffce000, 0x800, "GPIOs" }, /* CS28 */
4550 { 0 }
4553 static void omap_setup_dsp_mapping(const struct omap_map_s *map)
4555 int io;
4557 for (; map->phys_dsp; map ++) {
4558 io = cpu_get_physical_page_desc(map->phys_mpu);
4560 cpu_register_physical_memory(map->phys_dsp, map->size, io);
4564 void omap_mpu_wakeup(void *opaque, int irq, int req)
4566 struct omap_mpu_state_s *mpu = (struct omap_mpu_state_s *) opaque;
4568 if (mpu->env->halted)
4569 cpu_interrupt(mpu->env, CPU_INTERRUPT_EXITTB);
4572 static const struct dma_irq_map omap1_dma_irq_map[] = {
4573 { 0, OMAP_INT_DMA_CH0_6 },
4574 { 0, OMAP_INT_DMA_CH1_7 },
4575 { 0, OMAP_INT_DMA_CH2_8 },
4576 { 0, OMAP_INT_DMA_CH3 },
4577 { 0, OMAP_INT_DMA_CH4 },
4578 { 0, OMAP_INT_DMA_CH5 },
4579 { 1, OMAP_INT_1610_DMA_CH6 },
4580 { 1, OMAP_INT_1610_DMA_CH7 },
4581 { 1, OMAP_INT_1610_DMA_CH8 },
4582 { 1, OMAP_INT_1610_DMA_CH9 },
4583 { 1, OMAP_INT_1610_DMA_CH10 },
4584 { 1, OMAP_INT_1610_DMA_CH11 },
4585 { 1, OMAP_INT_1610_DMA_CH12 },
4586 { 1, OMAP_INT_1610_DMA_CH13 },
4587 { 1, OMAP_INT_1610_DMA_CH14 },
4588 { 1, OMAP_INT_1610_DMA_CH15 }
4591 /* DMA ports for OMAP1 */
4592 static int omap_validate_emiff_addr(struct omap_mpu_state_s *s,
4593 target_phys_addr_t addr)
4595 return addr >= OMAP_EMIFF_BASE && addr < OMAP_EMIFF_BASE + s->sdram_size;
4598 static int omap_validate_emifs_addr(struct omap_mpu_state_s *s,
4599 target_phys_addr_t addr)
4601 return addr >= OMAP_EMIFS_BASE && addr < OMAP_EMIFF_BASE;
4604 static int omap_validate_imif_addr(struct omap_mpu_state_s *s,
4605 target_phys_addr_t addr)
4607 return addr >= OMAP_IMIF_BASE && addr < OMAP_IMIF_BASE + s->sram_size;
4610 static int omap_validate_tipb_addr(struct omap_mpu_state_s *s,
4611 target_phys_addr_t addr)
4613 return addr >= 0xfffb0000 && addr < 0xffff0000;
4616 static int omap_validate_local_addr(struct omap_mpu_state_s *s,
4617 target_phys_addr_t addr)
4619 return addr >= OMAP_LOCALBUS_BASE && addr < OMAP_LOCALBUS_BASE + 0x1000000;
4622 static int omap_validate_tipb_mpui_addr(struct omap_mpu_state_s *s,
4623 target_phys_addr_t addr)
4625 return addr >= 0xe1010000 && addr < 0xe1020004;
4628 struct omap_mpu_state_s *omap310_mpu_init(unsigned long sdram_size,
4629 DisplayState *ds, const char *core)
4631 int i;
4632 struct omap_mpu_state_s *s = (struct omap_mpu_state_s *)
4633 qemu_mallocz(sizeof(struct omap_mpu_state_s));
4634 ram_addr_t imif_base, emiff_base;
4635 qemu_irq *cpu_irq;
4636 qemu_irq dma_irqs[6];
4637 int sdindex;
4639 if (!core)
4640 core = "ti925t";
4642 /* Core */
4643 s->mpu_model = omap310;
4644 s->env = cpu_init(core);
4645 if (!s->env) {
4646 fprintf(stderr, "Unable to find CPU definition\n");
4647 exit(1);
4649 s->sdram_size = sdram_size;
4650 s->sram_size = OMAP15XX_SRAM_SIZE;
4652 s->wakeup = qemu_allocate_irqs(omap_mpu_wakeup, s, 1)[0];
4654 /* Clocks */
4655 omap_clk_init(s);
4657 /* Memory-mapped stuff */
4658 cpu_register_physical_memory(OMAP_EMIFF_BASE, s->sdram_size,
4659 (emiff_base = qemu_ram_alloc(s->sdram_size)) | IO_MEM_RAM);
4660 cpu_register_physical_memory(OMAP_IMIF_BASE, s->sram_size,
4661 (imif_base = qemu_ram_alloc(s->sram_size)) | IO_MEM_RAM);
4663 omap_clkm_init(0xfffece00, 0xe1008000, s);
4665 cpu_irq = arm_pic_init_cpu(s->env);
4666 s->ih[0] = omap_inth_init(0xfffecb00, 0x100, 1, &s->irq[0],
4667 cpu_irq[ARM_PIC_CPU_IRQ], cpu_irq[ARM_PIC_CPU_FIQ],
4668 omap_findclk(s, "arminth_ck"));
4669 s->ih[1] = omap_inth_init(0xfffe0000, 0x800, 1, &s->irq[1],
4670 s->ih[0]->pins[OMAP_INT_15XX_IH2_IRQ], NULL,
4671 omap_findclk(s, "arminth_ck"));
4673 for (i = 0; i < 6; i ++)
4674 dma_irqs[i] =
4675 s->irq[omap1_dma_irq_map[i].ih][omap1_dma_irq_map[i].intr];
4676 s->dma = omap_dma_init(0xfffed800, dma_irqs, s->irq[0][OMAP_INT_DMA_LCD],
4677 s, omap_findclk(s, "dma_ck"), omap_dma_3_1);
4679 s->port[emiff ].addr_valid = omap_validate_emiff_addr;
4680 s->port[emifs ].addr_valid = omap_validate_emifs_addr;
4681 s->port[imif ].addr_valid = omap_validate_imif_addr;
4682 s->port[tipb ].addr_valid = omap_validate_tipb_addr;
4683 s->port[local ].addr_valid = omap_validate_local_addr;
4684 s->port[tipb_mpui].addr_valid = omap_validate_tipb_mpui_addr;
4686 s->timer[0] = omap_mpu_timer_init(0xfffec500,
4687 s->irq[0][OMAP_INT_TIMER1],
4688 omap_findclk(s, "mputim_ck"));
4689 s->timer[1] = omap_mpu_timer_init(0xfffec600,
4690 s->irq[0][OMAP_INT_TIMER2],
4691 omap_findclk(s, "mputim_ck"));
4692 s->timer[2] = omap_mpu_timer_init(0xfffec700,
4693 s->irq[0][OMAP_INT_TIMER3],
4694 omap_findclk(s, "mputim_ck"));
4696 s->wdt = omap_wd_timer_init(0xfffec800,
4697 s->irq[0][OMAP_INT_WD_TIMER],
4698 omap_findclk(s, "armwdt_ck"));
4700 s->os_timer = omap_os_timer_init(0xfffb9000,
4701 s->irq[1][OMAP_INT_OS_TIMER],
4702 omap_findclk(s, "clk32-kHz"));
4704 s->lcd = omap_lcdc_init(0xfffec000, s->irq[0][OMAP_INT_LCD_CTRL],
4705 omap_dma_get_lcdch(s->dma), ds, imif_base, emiff_base,
4706 omap_findclk(s, "lcd_ck"));
4708 omap_ulpd_pm_init(0xfffe0800, s);
4709 omap_pin_cfg_init(0xfffe1000, s);
4710 omap_id_init(s);
4712 omap_mpui_init(0xfffec900, s);
4714 s->private_tipb = omap_tipb_bridge_init(0xfffeca00,
4715 s->irq[0][OMAP_INT_BRIDGE_PRIV],
4716 omap_findclk(s, "tipb_ck"));
4717 s->public_tipb = omap_tipb_bridge_init(0xfffed300,
4718 s->irq[0][OMAP_INT_BRIDGE_PUB],
4719 omap_findclk(s, "tipb_ck"));
4721 omap_tcmi_init(0xfffecc00, s);
4723 s->uart[0] = omap_uart_init(0xfffb0000, s->irq[1][OMAP_INT_UART1],
4724 omap_findclk(s, "uart1_ck"),
4725 omap_findclk(s, "uart1_ck"),
4726 s->drq[OMAP_DMA_UART1_TX], s->drq[OMAP_DMA_UART1_RX],
4727 serial_hds[0]);
4728 s->uart[1] = omap_uart_init(0xfffb0800, s->irq[1][OMAP_INT_UART2],
4729 omap_findclk(s, "uart2_ck"),
4730 omap_findclk(s, "uart2_ck"),
4731 s->drq[OMAP_DMA_UART2_TX], s->drq[OMAP_DMA_UART2_RX],
4732 serial_hds[0] ? serial_hds[1] : 0);
4733 s->uart[2] = omap_uart_init(0xe1019800, s->irq[0][OMAP_INT_UART3],
4734 omap_findclk(s, "uart3_ck"),
4735 omap_findclk(s, "uart3_ck"),
4736 s->drq[OMAP_DMA_UART3_TX], s->drq[OMAP_DMA_UART3_RX],
4737 serial_hds[0] && serial_hds[1] ? serial_hds[2] : 0);
4739 omap_dpll_init(&s->dpll[0], 0xfffecf00, omap_findclk(s, "dpll1"));
4740 omap_dpll_init(&s->dpll[1], 0xfffed000, omap_findclk(s, "dpll2"));
4741 omap_dpll_init(&s->dpll[2], 0xfffed100, omap_findclk(s, "dpll3"));
4743 sdindex = drive_get_index(IF_SD, 0, 0);
4744 if (sdindex == -1) {
4745 fprintf(stderr, "qemu: missing SecureDigital device\n");
4746 exit(1);
4748 s->mmc = omap_mmc_init(0xfffb7800, drives_table[sdindex].bdrv,
4749 s->irq[1][OMAP_INT_OQN], &s->drq[OMAP_DMA_MMC_TX],
4750 omap_findclk(s, "mmc_ck"));
4752 s->mpuio = omap_mpuio_init(0xfffb5000,
4753 s->irq[1][OMAP_INT_KEYBOARD], s->irq[1][OMAP_INT_MPUIO],
4754 s->wakeup, omap_findclk(s, "clk32-kHz"));
4756 s->gpio = omap_gpio_init(0xfffce000, s->irq[0][OMAP_INT_GPIO_BANK1],
4757 omap_findclk(s, "arm_gpio_ck"));
4759 s->microwire = omap_uwire_init(0xfffb3000, &s->irq[1][OMAP_INT_uWireTX],
4760 s->drq[OMAP_DMA_UWIRE_TX], omap_findclk(s, "mpuper_ck"));
4762 omap_pwl_init(0xfffb5800, s, omap_findclk(s, "armxor_ck"));
4763 omap_pwt_init(0xfffb6000, s, omap_findclk(s, "armxor_ck"));
4765 s->i2c[0] = omap_i2c_init(0xfffb3800, s->irq[1][OMAP_INT_I2C],
4766 &s->drq[OMAP_DMA_I2C_RX], omap_findclk(s, "mpuper_ck"));
4768 s->rtc = omap_rtc_init(0xfffb4800, &s->irq[1][OMAP_INT_RTC_TIMER],
4769 omap_findclk(s, "clk32-kHz"));
4771 s->mcbsp1 = omap_mcbsp_init(0xfffb1800, &s->irq[1][OMAP_INT_McBSP1TX],
4772 &s->drq[OMAP_DMA_MCBSP1_TX], omap_findclk(s, "dspxor_ck"));
4773 s->mcbsp2 = omap_mcbsp_init(0xfffb1000, &s->irq[0][OMAP_INT_310_McBSP2_TX],
4774 &s->drq[OMAP_DMA_MCBSP2_TX], omap_findclk(s, "mpuper_ck"));
4775 s->mcbsp3 = omap_mcbsp_init(0xfffb7000, &s->irq[1][OMAP_INT_McBSP3TX],
4776 &s->drq[OMAP_DMA_MCBSP3_TX], omap_findclk(s, "dspxor_ck"));
4778 s->led[0] = omap_lpg_init(0xfffbd000, omap_findclk(s, "clk32-kHz"));
4779 s->led[1] = omap_lpg_init(0xfffbd800, omap_findclk(s, "clk32-kHz"));
4781 /* Register mappings not currenlty implemented:
4782 * MCSI2 Comm fffb2000 - fffb27ff (not mapped on OMAP310)
4783 * MCSI1 Bluetooth fffb2800 - fffb2fff (not mapped on OMAP310)
4784 * USB W2FC fffb4000 - fffb47ff
4785 * Camera Interface fffb6800 - fffb6fff
4786 * USB Host fffba000 - fffba7ff
4787 * FAC fffba800 - fffbafff
4788 * HDQ/1-Wire fffbc000 - fffbc7ff
4789 * TIPB switches fffbc800 - fffbcfff
4790 * Mailbox fffcf000 - fffcf7ff
4791 * Local bus IF fffec100 - fffec1ff
4792 * Local bus MMU fffec200 - fffec2ff
4793 * DSP MMU fffed200 - fffed2ff
4796 omap_setup_dsp_mapping(omap15xx_dsp_mm);
4797 omap_setup_mpui_io(s);
4799 qemu_register_reset(omap1_mpu_reset, s);
4801 return s;