ARM system emulation (Paul Brook)
[qemu] / hw / integratorcp.c
1 /* 
2  * ARM Integrator CP System emulation.
3  *
4  * Copyright (c) 2005 CodeSourcery, LLC.
5  * Written by Paul Brook
6  *
7  * This code is licenced under the GPL
8  */
9
10 #include <vl.h>
11
12 #define KERNEL_ARGS_ADDR 0x100
13 #define KERNEL_LOAD_ADDR 0x00010000
14 #define INITRD_LOAD_ADDR 0x00800000
15
16 /* Stub functions for hardware that doesn't exist.  */
17 void pic_set_irq(int irq, int level)
18 {
19     cpu_abort (cpu_single_env, "pic_set_irq");
20 }
21
22 void pic_info(void)
23 {
24 }
25
26 void irq_info(void)
27 {
28 }
29
30 void vga_update_display(void)
31 {
32 }
33
34 void vga_screen_dump(const char *filename)
35 {
36 }
37
38 void vga_invalidate_display(void)
39 {
40 }
41
42 void DMA_run (void)
43 {
44 }
45
46 typedef struct {
47     uint32_t flash_offset;
48     uint32_t cm_osc;
49     uint32_t cm_ctrl;
50     uint32_t cm_lock;
51     uint32_t cm_auxosc;
52     uint32_t cm_sdram;
53     uint32_t cm_init;
54     uint32_t cm_flags;
55     uint32_t cm_nvflags;
56     uint32_t int_level;
57     uint32_t irq_enabled;
58     uint32_t fiq_enabled;
59 } integratorcm_state;
60
61 static uint8_t integrator_spd[128] = {
62    128, 8, 4, 11, 9, 1, 64, 0,  2, 0xa0, 0xa0, 0, 0, 8, 0, 1,
63    0xe, 4, 0x1c, 1, 2, 0x20, 0xc0, 0, 0, 0, 0, 0x30, 0x28, 0x30, 0x28, 0x40
64 };
65
66 static uint32_t integratorcm_read(void *opaque, target_phys_addr_t offset)
67 {
68     integratorcm_state *s = (integratorcm_state *)opaque;
69     offset -= 0x10000000;
70     if (offset >= 0x100 && offset < 0x200) {
71         /* CM_SPD */
72         if (offset >= 0x180)
73             return 0;
74         return integrator_spd[offset >> 2];
75     }
76     switch (offset >> 2) {
77     case 0: /* CM_ID */
78         return 0x411a3001;
79     case 1: /* CM_PROC */
80         return 0;
81     case 2: /* CM_OSC */
82         return s->cm_osc;
83     case 3: /* CM_CTRL */
84         return s->cm_ctrl;
85     case 4: /* CM_STAT */
86         return 0x00100000;
87     case 5: /* CM_LOCK */
88         if (s->cm_lock == 0xa05f) {
89             return 0x1a05f;
90         } else {
91             return s->cm_lock;
92         }
93     case 6: /* CM_LMBUSCNT */
94         /* ??? High frequency timer.  */
95         cpu_abort(cpu_single_env, "integratorcm_read: CM_LMBUSCNT");
96     case 7: /* CM_AUXOSC */
97         return s->cm_auxosc;
98     case 8: /* CM_SDRAM */
99         return s->cm_sdram;
100     case 9: /* CM_INIT */
101         return s->cm_init;
102     case 10: /* CM_REFCT */
103         /* ??? High frequency timer.  */
104         cpu_abort(cpu_single_env, "integratorcm_read: CM_REFCT");
105     case 12: /* CM_FLAGS */
106         return s->cm_flags;
107     case 14: /* CM_NVFLAGS */
108         return s->cm_nvflags;
109     case 16: /* CM_IRQ_STAT */
110         return s->int_level & s->irq_enabled;
111     case 17: /* CM_IRQ_RSTAT */
112         return s->int_level;
113     case 18: /* CM_IRQ_ENSET */
114         return s->irq_enabled;
115     case 20: /* CM_SOFT_INTSET */
116         return s->int_level & 1;
117     case 24: /* CM_FIQ_STAT */
118         return s->int_level & s->fiq_enabled;
119     case 25: /* CM_FIQ_RSTAT */
120         return s->int_level;
121     case 26: /* CM_FIQ_ENSET */
122         return s->fiq_enabled;
123     case 32: /* CM_VOLTAGE_CTL0 */
124     case 33: /* CM_VOLTAGE_CTL1 */
125     case 34: /* CM_VOLTAGE_CTL2 */
126     case 35: /* CM_VOLTAGE_CTL3 */
127         /* ??? Voltage control unimplemented.  */
128         return 0;
129     default:
130         cpu_abort (cpu_single_env,
131             "integratorcm_read: Unimplemented offset 0x%x\n", offset);
132         return 0;
133     }
134 }
135
136 static void integratorcm_do_remap(integratorcm_state *s, int flash)
137 {
138     if (flash) {
139         cpu_register_physical_memory(0, 0x100000, IO_MEM_RAM);
140     } else {
141         cpu_register_physical_memory(0, 0x100000, s->flash_offset | IO_MEM_RAM);
142     }
143     //??? tlb_flush (cpu_single_env, 1);
144 }
145
146 static void integratorcm_set_ctrl(integratorcm_state *s, uint32_t value)
147 {
148     if (value & 8) {
149         cpu_abort(cpu_single_env, "Board reset\n");
150     }
151     if ((s->cm_init ^ value) & 4) {
152         integratorcm_do_remap(s, (value & 4) == 0);
153     }
154     if ((s->cm_init ^ value) & 1) {
155         printf("Green LED %s\n", (value & 1) ? "on" : "off");
156     }
157     s->cm_init = (s->cm_init & ~ 5) | (value ^ 5);
158 }
159
160 static void integratorcm_update(integratorcm_state *s)
161 {
162     /* ??? The CPU irq/fiq is raised when either the core module or base PIC
163        are active.  */
164     if (s->int_level & (s->irq_enabled | s->fiq_enabled))
165         cpu_abort(cpu_single_env, "Core module interrupt\n");
166 }
167
168 static void integratorcm_write(void *opaque, target_phys_addr_t offset,
169                                uint32_t value)
170 {
171     integratorcm_state *s = (integratorcm_state *)opaque;
172     offset -= 0x10000000;
173     switch (offset >> 2) {
174     case 2: /* CM_OSC */
175         if (s->cm_lock == 0xa05f)
176             s->cm_osc = value;
177         break;
178     case 3: /* CM_CTRL */
179         integratorcm_set_ctrl(s, value);
180         break;
181     case 5: /* CM_LOCK */
182         s->cm_lock = value & 0xffff;
183         break;
184     case 7: /* CM_AUXOSC */
185         if (s->cm_lock == 0xa05f)
186             s->cm_auxosc = value;
187         break;
188     case 8: /* CM_SDRAM */
189         s->cm_sdram = value;
190         break;
191     case 9: /* CM_INIT */
192         /* ??? This can change the memory bus frequency.  */
193         s->cm_init = value;
194         break;
195     case 12: /* CM_FLAGSS */
196         s->cm_flags |= value;
197         break;
198     case 13: /* CM_FLAGSC */
199         s->cm_flags &= ~value;
200         break;
201     case 14: /* CM_NVFLAGSS */
202         s->cm_nvflags |= value;
203         break;
204     case 15: /* CM_NVFLAGSS */
205         s->cm_nvflags &= ~value;
206         break;
207     case 18: /* CM_IRQ_ENSET */
208         s->irq_enabled |= value;
209         integratorcm_update(s);
210         break;
211     case 19: /* CM_IRQ_ENCLR */
212         s->irq_enabled &= ~value;
213         integratorcm_update(s);
214         break;
215     case 20: /* CM_SOFT_INTSET */
216         s->int_level |= (value & 1);
217         integratorcm_update(s);
218         break;
219     case 21: /* CM_SOFT_INTCLR */
220         s->int_level &= ~(value & 1);
221         integratorcm_update(s);
222         break;
223     case 26: /* CM_FIQ_ENSET */
224         s->fiq_enabled |= value;
225         integratorcm_update(s);
226         break;
227     case 27: /* CM_FIQ_ENCLR */
228         s->fiq_enabled &= ~value;
229         integratorcm_update(s);
230         break;
231     case 32: /* CM_VOLTAGE_CTL0 */
232     case 33: /* CM_VOLTAGE_CTL1 */
233     case 34: /* CM_VOLTAGE_CTL2 */
234     case 35: /* CM_VOLTAGE_CTL3 */
235         /* ??? Voltage control unimplemented.  */
236         break;
237     default:
238         cpu_abort (cpu_single_env,
239             "integratorcm_write: Unimplemented offset 0x%x\n", offset);
240         break;
241     }
242 }
243
244 /* Integrator/CM control registers.  */
245
246 static CPUReadMemoryFunc *integratorcm_readfn[] = {
247    integratorcm_read,
248    integratorcm_read,
249    integratorcm_read
250 };
251
252 static CPUWriteMemoryFunc *integratorcm_writefn[] = {
253    integratorcm_write,
254    integratorcm_write,
255    integratorcm_write
256 };
257
258 static void integratorcm_init(int memsz, uint32_t flash_offset)
259 {
260     int iomemtype;
261     integratorcm_state *s;
262
263     s = (integratorcm_state *)qemu_mallocz(sizeof(integratorcm_state));
264     s->cm_osc = 0x01000048;
265     /* ??? What should the high bits of this value be?  */
266     s->cm_auxosc = 0x0007feff;
267     s->cm_sdram = 0x00011122;
268     if (memsz >= 256) {
269         integrator_spd[31] = 64;
270         s->cm_sdram |= 0x10;
271     } else if (memsz >= 128) {
272         integrator_spd[31] = 32;
273         s->cm_sdram |= 0x0c;
274     } else if (memsz >= 64) {
275         integrator_spd[31] = 16;
276         s->cm_sdram |= 0x08;
277     } else if (memsz >= 32) {
278         integrator_spd[31] = 4;
279         s->cm_sdram |= 0x04;
280     } else {
281         integrator_spd[31] = 2;
282     }
283     memcpy(integrator_spd + 73, "QEMU-MEMORY", 11);
284     s->cm_init = 0x00000112;
285     s->flash_offset = flash_offset;
286
287     iomemtype = cpu_register_io_memory(0, integratorcm_readfn,
288                                        integratorcm_writefn, s);
289     cpu_register_physical_memory(0x10000000, 0x007fffff, iomemtype);
290     integratorcm_do_remap(s, 1);
291     /* ??? Save/restore.  */
292 }
293
294 /* Integrator/CP hardware emulation.  */
295 /* Primary interrupt controller.  */
296
297 typedef struct icp_pic_state
298 {
299   uint32_t base;
300   uint32_t level;
301   uint32_t irq_enabled;
302   uint32_t fiq_enabled;
303   void *parent;
304   /* -1 if parent is a cpu, otherwise IRQ number on parent PIC.  */
305   int parent_irq;
306 } icp_pic_state;
307
308 static void icp_pic_set_level(icp_pic_state *, int, int);
309
310 static void icp_pic_update(icp_pic_state *s)
311 {
312     CPUState *env;
313     if (s->parent_irq != -1) {
314         uint32_t flags;
315
316         flags = (s->level & s->irq_enabled);
317         icp_pic_set_level((icp_pic_state *)s->parent, s->parent_irq,
318                           flags != 0);
319         return;
320     }
321     /* Raise CPU interrupt.  */
322     env = (CPUState *)s->parent;
323     if (s->level & s->fiq_enabled) {
324         cpu_interrupt (env, CPU_INTERRUPT_FIQ);
325     } else {
326         cpu_reset_interrupt (env, CPU_INTERRUPT_FIQ);
327     }
328     if (s->level & s->irq_enabled) {
329       cpu_interrupt (env, CPU_INTERRUPT_HARD);
330     } else {
331       cpu_reset_interrupt (env, CPU_INTERRUPT_HARD);
332     }
333 }
334
335 static void icp_pic_set_level(icp_pic_state *s, int n, int level)
336 {
337     if (level)
338         s->level |= 1 << n;
339     else
340         s->level &= ~(1 << n);
341     icp_pic_update(s);
342 }
343
344 static uint32_t icp_pic_read(void *opaque, target_phys_addr_t offset)
345 {
346     icp_pic_state *s = (icp_pic_state *)opaque;
347
348     offset -= s->base;
349     switch (offset >> 2) {
350     case 0: /* IRQ_STATUS */
351         return s->level & s->irq_enabled;
352     case 1: /* IRQ_RAWSTAT */
353         return s->level;
354     case 2: /* IRQ_ENABLESET */
355         return s->irq_enabled;
356     case 4: /* INT_SOFTSET */
357         return s->level & 1;
358     case 8: /* FRQ_STATUS */
359         return s->level & s->fiq_enabled;
360     case 9: /* FRQ_RAWSTAT */
361         return s->level;
362     case 10: /* FRQ_ENABLESET */
363         return s->fiq_enabled;
364     case 3: /* IRQ_ENABLECLR */
365     case 5: /* INT_SOFTCLR */
366     case 11: /* FRQ_ENABLECLR */
367     default:
368         printf ("icp_pic_read: Bad register offset 0x%x\n", offset);
369         return 0;
370     }
371 }
372
373 static void icp_pic_write(void *opaque, target_phys_addr_t offset,
374                           uint32_t value)
375 {
376     icp_pic_state *s = (icp_pic_state *)opaque;
377     offset -= s->base;
378
379     switch (offset >> 2) {
380     case 2: /* IRQ_ENABLESET */
381         s->irq_enabled |= value;
382         break;
383     case 3: /* IRQ_ENABLECLR */
384         s->irq_enabled &= ~value;
385         break;
386     case 4: /* INT_SOFTSET */
387         if (value & 1)
388             icp_pic_set_level(s, 0, 1);
389         break;
390     case 5: /* INT_SOFTCLR */
391         if (value & 1)
392             icp_pic_set_level(s, 0, 0);
393         break;
394     case 10: /* FRQ_ENABLESET */
395         s->fiq_enabled |= value;
396         break;
397     case 11: /* FRQ_ENABLECLR */
398         s->fiq_enabled &= ~value;
399         break;
400     case 0: /* IRQ_STATUS */
401     case 1: /* IRQ_RAWSTAT */
402     case 8: /* FRQ_STATUS */
403     case 9: /* FRQ_RAWSTAT */
404     default:
405         printf ("icp_pic_write: Bad register offset 0x%x\n", offset);
406         return;
407     }
408     icp_pic_update(s);
409 }
410
411 static CPUReadMemoryFunc *icp_pic_readfn[] = {
412    icp_pic_read,
413    icp_pic_read,
414    icp_pic_read
415 };
416
417 static CPUWriteMemoryFunc *icp_pic_writefn[] = {
418    icp_pic_write,
419    icp_pic_write,
420    icp_pic_write
421 };
422
423 static icp_pic_state *icp_pic_init(uint32_t base, void *parent,
424                                    int parent_irq)
425 {
426     icp_pic_state *s;
427     int iomemtype;
428
429     s = (icp_pic_state *)qemu_mallocz(sizeof(icp_pic_state));
430     if (!s)
431         return NULL;
432
433     s->base = base;
434     s->parent = parent;
435     s->parent_irq = parent_irq;
436     iomemtype = cpu_register_io_memory(0, icp_pic_readfn,
437                                        icp_pic_writefn, s);
438     cpu_register_physical_memory(base, 0x007fffff, iomemtype);
439     /* ??? Save/restore.  */
440     return s;
441 }
442
443 /* Timers.  */
444
445 /* System bus clock speed (40MHz) for timer 0.  Not sure about this value.  */
446 #define ICP_BUS_FREQ 40000000
447
448 typedef struct {
449     int64_t next_time;
450     int64_t expires[3];
451     int64_t loaded[3];
452     QEMUTimer *timer;
453     icp_pic_state *pic;
454     uint32_t base;
455     uint32_t control[3];
456     uint32_t count[3];
457     uint32_t limit[3];
458     int freq[3];
459     int int_level[3];
460 } icp_pit_state;
461
462 /* Calculate the new expiry time of the given timer.  */
463
464 static void icp_pit_reload(icp_pit_state *s, int n)
465 {
466     int64_t delay;
467
468     s->loaded[n] = s->expires[n];
469     delay = muldiv64(s->count[n], ticks_per_sec, s->freq[n]);
470     if (delay == 0)
471         delay = 1;
472     s->expires[n] += delay;
473 }
474
475 /* Check all active timers, and schedule the next timer interrupt.  */
476
477 static void icp_pit_update(icp_pit_state *s, int64_t now)
478 {
479     int n;
480     int64_t next;
481
482     next = now;
483     for (n = 0; n < 3; n++) {
484         /* Ignore disabled timers.  */
485         if ((s->control[n] & 0x80) == 0)
486             continue;
487         /* Ignore expired one-shot timers.  */
488         if (s->count[n] == 0 && s->control[n] & 1)
489             continue;
490         if (s->expires[n] - now <= 0) {
491             /* Timer has expired.  */
492             s->int_level[n] = 1;
493             if (s->control[n] & 1) {
494                 /* One-shot.  */
495                 s->count[n] = 0;
496             } else {
497                 if ((s->control[n] & 0x40) == 0) {
498                     /* Free running.  */
499                     if (s->control[n] & 2)
500                         s->count[n] = 0xffffffff;
501                     else
502                         s->count[n] = 0xffff;
503                 } else {
504                       /* Periodic.  */
505                       s->count[n] = s->limit[n];
506                 }
507             }
508         }
509         while (s->expires[n] - now <= 0) {
510             icp_pit_reload(s, n);
511         }
512     }
513     /* Update interrupts.  */
514     for (n = 0; n < 3; n++) {
515         if (s->int_level[n] && (s->control[n] & 0x20)) {
516             icp_pic_set_level(s->pic, 5 + n, 1);
517         } else {
518             icp_pic_set_level(s->pic, 5 + n, 0);
519         }
520         if (next - s->expires[n] < 0)
521             next = s->expires[n];
522     }
523     /* Schedule the next timer interrupt.  */
524     if (next == now) {
525         qemu_del_timer(s->timer);
526         s->next_time = 0;
527     } else if (next != s->next_time) {
528         qemu_mod_timer(s->timer, next);
529         s->next_time = next;
530     }
531 }
532
533 /* Return the current value of the timer.  */
534 static uint32_t icp_pit_getcount(icp_pit_state *s, int n, int64_t now)
535 {
536     int64_t elapsed;
537     int64_t period;
538
539     if (s->count[n] == 0)
540         return 0;
541     if ((s->control[n] & 0x80) == 0)
542         return s->count[n];
543     elapsed = now - s->loaded[n];
544     period = s->expires[n] - s->loaded[n];
545     /* If the timer should have expired then return 0.  This can happen
546        when the host timer signal doesnt occur immediately.  It's better to
547        have a timer appear to sit at zero for a while than have it wrap
548        around before the guest interrupt is raised.  */
549     /* ??? Could we trigger the interrupt here?  */
550     if (elapsed > period)
551         return 0;
552     /* We need to calculate count * elapsed / period without overfowing.
553        Scale both elapsed and period so they fit in a 32-bit int.  */
554     while (period != (int32_t)period) {
555         period >>= 1;
556         elapsed >>= 1;
557     }
558     return ((uint64_t)s->count[n] * (uint64_t)(int32_t)elapsed)
559             / (int32_t)period;
560 }
561
562 static uint32_t icp_pit_read(void *opaque, target_phys_addr_t offset)
563 {
564     int n;
565     icp_pit_state *s = (icp_pit_state *)opaque;
566
567     offset -= s->base;
568     n = offset >> 8;
569     if (n > 2)
570         cpu_abort (cpu_single_env, "icp_pit_read: Bad timer %x\n", offset);
571     switch ((offset & 0xff) >> 2) {
572     case 0: /* TimerLoad */
573     case 6: /* TimerBGLoad */
574         return s->limit[n];
575     case 1: /* TimerValue */
576         return icp_pit_getcount(s, n, qemu_get_clock(vm_clock));
577     case 2: /* TimerControl */
578         return s->control[n];
579     case 4: /* TimerRIS */
580         return s->int_level[n];
581     case 5: /* TimerMIS */
582         if ((s->control[n] & 0x20) == 0)
583             return 0;
584         return s->int_level[n];
585     default:
586         cpu_abort (cpu_single_env, "icp_pit_read: Bad offset %x\n", offset);
587         return 0;
588     }
589 }
590
591 static void icp_pit_write(void *opaque, target_phys_addr_t offset,
592                           uint32_t value)
593 {
594     icp_pit_state *s = (icp_pit_state *)opaque;
595     int n;
596     int64_t now;
597
598     now = qemu_get_clock(vm_clock);
599     offset -= s->base;
600     n = offset >> 8;
601     if (n > 2)
602         cpu_abort (cpu_single_env, "icp_pit_write: Bad offset %x\n", offset);
603
604     switch ((offset & 0xff) >> 2) {
605     case 0: /* TimerLoad */
606         s->limit[n] = value;
607         s->count[n] = value;
608         s->expires[n] = now;
609         icp_pit_reload(s, n);
610         break;
611     case 1: /* TimerValue */
612         /* ??? Linux seems to want to write to this readonly register.
613            Ignore it.  */
614         break;
615     case 2: /* TimerControl */
616         if (s->control[n] & 0x80) {
617             /* Pause the timer if it is running.  This may cause some
618                inaccuracy dure to rounding, but avoids a whole lot of other
619                messyness.  */
620             s->count[n] = icp_pit_getcount(s, n, now);
621         }
622         s->control[n] = value;
623         if (n == 0)
624             s->freq[n] = ICP_BUS_FREQ;
625         else
626             s->freq[n] = 1000000;
627         /* ??? Need to recalculate expiry time after changing divisor.  */
628         switch ((value >> 2) & 3) {
629         case 1: s->freq[n] >>= 4; break;
630         case 2: s->freq[n] >>= 8; break;
631         }
632         if (s->control[n] & 0x80) {
633             /* Restart the timer if still enabled.  */
634             s->expires[n] = now;
635             icp_pit_reload(s, n);
636         }
637         break;
638     case 3: /* TimerIntClr */
639         s->int_level[n] = 0;
640         break;
641     case 6: /* TimerBGLoad */
642         s->limit[n] = value;
643         break;
644     default:
645         cpu_abort (cpu_single_env, "icp_pit_write: Bad offset %x\n", offset);
646     }
647     icp_pit_update(s, now);
648 }
649
650 static void icp_pit_tick(void *opaque)
651 {
652     int64_t now;
653
654     now = qemu_get_clock(vm_clock);
655     icp_pit_update((icp_pit_state *)opaque, now);
656 }
657
658 static CPUReadMemoryFunc *icp_pit_readfn[] = {
659    icp_pit_read,
660    icp_pit_read,
661    icp_pit_read
662 };
663
664 static CPUWriteMemoryFunc *icp_pit_writefn[] = {
665    icp_pit_write,
666    icp_pit_write,
667    icp_pit_write
668 };
669
670 static void icp_pit_init(uint32_t base, icp_pic_state *pic)
671 {
672     int iomemtype;
673     icp_pit_state *s;
674     int n;
675
676     s = (icp_pit_state *)qemu_mallocz(sizeof(icp_pit_state));
677     s->base = base;
678     s->pic = pic;
679     s->freq[0] = ICP_BUS_FREQ;
680     s->freq[1] = 1000000;
681     s->freq[2] = 1000000;
682     for (n = 0; n < 3; n++) {
683         s->control[n] = 0x20;
684         s->count[n] = 0xffffffff;
685     }
686
687     iomemtype = cpu_register_io_memory(0, icp_pit_readfn,
688                                        icp_pit_writefn, s);
689     cpu_register_physical_memory(base, 0x007fffff, iomemtype);
690     s->timer = qemu_new_timer(vm_clock, icp_pit_tick, s);
691     /* ??? Save/restore.  */
692 }
693
694 /* ARM PrimeCell PL011 UART */
695
696 typedef struct {
697     uint32_t base;
698     uint32_t readbuff;
699     uint32_t flags;
700     uint32_t lcr;
701     uint32_t cr;
702     uint32_t dmacr;
703     uint32_t int_enabled;
704     uint32_t int_level;
705     uint32_t read_fifo[16];
706     uint32_t ilpr;
707     uint32_t ibrd;
708     uint32_t fbrd;
709     uint32_t ifl;
710     int read_pos;
711     int read_count;
712     int read_trigger;
713     CharDriverState *chr;
714     icp_pic_state *pic;
715     int irq;
716 } pl011_state;
717
718 #define PL011_INT_TX 0x20
719 #define PL011_INT_RX 0x10
720
721 #define PL011_FLAG_TXFE 0x80
722 #define PL011_FLAG_RXFF 0x40
723 #define PL011_FLAG_TXFF 0x20
724 #define PL011_FLAG_RXFE 0x10
725
726 static const unsigned char pl011_id[] =
727 { 0x11, 0x10, 0x14, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
728
729 static void pl011_update(pl011_state *s)
730 {
731     uint32_t flags;
732     
733     flags = s->int_level & s->int_enabled;
734     icp_pic_set_level(s->pic, s->irq, flags != 0);
735 }
736
737 static uint32_t pl011_read(void *opaque, target_phys_addr_t offset)
738 {
739     pl011_state *s = (pl011_state *)opaque;
740     uint32_t c;
741
742     offset -= s->base;
743     if (offset >= 0xfe0 && offset < 0x1000) {
744         return pl011_id[(offset - 0xfe0) >> 2];
745     }
746     switch (offset >> 2) {
747     case 0: /* UARTDR */
748         s->flags &= ~PL011_FLAG_RXFF;
749         c = s->read_fifo[s->read_pos];
750         if (s->read_count > 0) {
751             s->read_count--;
752             if (++s->read_pos == 16)
753                 s->read_pos = 0;
754         }
755         if (s->read_count == 0) {
756             s->flags |= PL011_FLAG_RXFE;
757         }
758         if (s->read_count == s->read_trigger - 1)
759             s->int_level &= ~ PL011_INT_RX;
760         pl011_update(s);
761         return c;
762     case 1: /* UARTCR */
763         return 0;
764     case 6: /* UARTFR */
765         return s->flags;
766     case 8: /* UARTILPR */
767         return s->ilpr;
768     case 9: /* UARTIBRD */
769         return s->ibrd;
770     case 10: /* UARTFBRD */
771         return s->fbrd;
772     case 11: /* UARTLCR_H */
773         return s->lcr;
774     case 12: /* UARTCR */
775         return s->cr;
776     case 13: /* UARTIFLS */
777         return s->ifl;
778     case 14: /* UARTIMSC */
779         return s->int_enabled;
780     case 15: /* UARTRIS */
781         return s->int_level;
782     case 16: /* UARTMIS */
783         return s->int_level & s->int_enabled;
784     case 18: /* UARTDMACR */
785         return s->dmacr;
786     default:
787         cpu_abort (cpu_single_env, "pl011_read: Bad offset %x\n", offset);
788         return 0;
789     }
790 }
791
792 static void pl011_set_read_trigger(pl011_state *s)
793 {
794 #if 0
795     /* The docs say the RX interrupt is triggered when the FIFO exceeds
796        the threshold.  However linux only reads the FIFO in response to an
797        interrupt.  Triggering the interrupt when the FIFO is non-empty seems
798        to make things work.  */
799     if (s->lcr & 0x10)
800         s->read_trigger = (s->ifl >> 1) & 0x1c;
801     else
802 #endif
803         s->read_trigger = 1;
804 }
805
806 static void pl011_write(void *opaque, target_phys_addr_t offset,
807                           uint32_t value)
808 {
809     pl011_state *s = (pl011_state *)opaque;
810     unsigned char ch;
811
812     offset -= s->base;
813     switch (offset >> 2) {
814     case 0: /* UARTDR */
815         /* ??? Check if transmitter is enabled.  */
816         ch = value;
817         if (s->chr)
818             qemu_chr_write(s->chr, &ch, 1);
819         s->int_level |= PL011_INT_TX;
820         pl011_update(s);
821         break;
822     case 1: /* UARTCR */
823         s->cr = value;
824         break;
825     case 8: /* UARTUARTILPR */
826         s->ilpr = value;
827         break;
828     case 9: /* UARTIBRD */
829         s->ibrd = value;
830         break;
831     case 10: /* UARTFBRD */
832         s->fbrd = value;
833         break;
834     case 11: /* UARTLCR_H */
835         s->lcr = value;
836         pl011_set_read_trigger(s);
837         break;
838     case 12: /* UARTCR */
839         /* ??? Need to implement the enable and loopback bits.  */
840         s->cr = value;
841         break;
842     case 13: /* UARTIFS */
843         s->ifl = value;
844         pl011_set_read_trigger(s);
845         break;
846     case 14: /* UARTIMSC */
847         s->int_enabled = value;
848         pl011_update(s);
849         break;
850     case 17: /* UARTICR */
851         s->int_level &= ~value;
852         pl011_update(s);
853         break;
854     case 18: /* UARTDMACR */
855         s->dmacr = value;
856         if (value & 3)
857             cpu_abort(cpu_single_env, "PL011: DMA not implemented\n");
858         break;
859     default:
860         cpu_abort (cpu_single_env, "pl011_write: Bad offset %x\n", offset);
861     }
862 }
863
864 static int pl011_can_recieve(void *opaque)
865 {
866     pl011_state *s = (pl011_state *)opaque;
867
868     if (s->lcr & 0x10)
869         return s->read_count < 16;
870     else
871         return s->read_count < 1;
872 }
873
874 static void pl011_recieve(void *opaque, const uint8_t *buf, int size)
875 {
876     pl011_state *s = (pl011_state *)opaque;
877     int slot;
878
879     slot = s->read_pos + s->read_count;
880     if (slot >= 16)
881         slot -= 16;
882     s->read_fifo[slot] = *buf;
883     s->read_count++;
884     s->flags &= ~PL011_FLAG_RXFE;
885     if (s->cr & 0x10 || s->read_count == 16) {
886         s->flags |= PL011_FLAG_RXFF;
887     }
888     if (s->read_count == s->read_trigger) {
889         s->int_level |= PL011_INT_RX;
890         pl011_update(s);
891     }
892 }
893
894 static void pl011_event(void *opaque, int event)
895 {
896     /* ??? Should probably implement break.  */
897 }
898
899 static CPUReadMemoryFunc *pl011_readfn[] = {
900    pl011_read,
901    pl011_read,
902    pl011_read
903 };
904
905 static CPUWriteMemoryFunc *pl011_writefn[] = {
906    pl011_write,
907    pl011_write,
908    pl011_write
909 };
910
911 static void pl011_init(uint32_t base, icp_pic_state *pic, int irq,
912                        CharDriverState *chr)
913 {
914     int iomemtype;
915     pl011_state *s;
916
917     s = (pl011_state *)qemu_mallocz(sizeof(pl011_state));
918     iomemtype = cpu_register_io_memory(0, pl011_readfn,
919                                        pl011_writefn, s);
920     cpu_register_physical_memory(base, 0x007fffff, iomemtype);
921     s->base = base;
922     s->pic = pic;
923     s->irq = irq;
924     s->chr = chr;
925     s->read_trigger = 1;
926     s->ifl = 0x12;
927     s->cr = 0x300;
928     s->flags = 0x90;
929     if (chr){ 
930         qemu_chr_add_read_handler(chr, pl011_can_recieve, pl011_recieve, s);
931         qemu_chr_add_event_handler(chr, pl011_event);
932     }
933     /* ??? Save/restore.  */
934 }
935
936 /* CP control registers.  */
937 typedef struct {
938     uint32_t base;
939 } icp_control_state;
940
941 static uint32_t icp_control_read(void *opaque, target_phys_addr_t offset)
942 {
943     icp_control_state *s = (icp_control_state *)opaque;
944     offset -= s->base;
945     switch (offset >> 2) {
946     case 0: /* CP_IDFIELD */
947         return 0x41034003;
948     case 1: /* CP_FLASHPROG */
949         return 0;
950     case 2: /* CP_INTREG */
951         return 0;
952     case 3: /* CP_DECODE */
953         return 0x11;
954     default:
955         cpu_abort (cpu_single_env, "icp_control_read: Bad offset %x\n", offset);
956         return 0;
957     }
958 }
959
960 static void icp_control_write(void *opaque, target_phys_addr_t offset,
961                           uint32_t value)
962 {
963     icp_control_state *s = (icp_control_state *)opaque;
964     offset -= s->base;
965     switch (offset >> 2) {
966     case 1: /* CP_FLASHPROG */
967     case 2: /* CP_INTREG */
968     case 3: /* CP_DECODE */
969         /* Nothing interesting implemented yet.  */
970         break;
971     default:
972         cpu_abort (cpu_single_env, "icp_control_write: Bad offset %x\n", offset);
973     }
974 }
975 static CPUReadMemoryFunc *icp_control_readfn[] = {
976    icp_control_read,
977    icp_control_read,
978    icp_control_read
979 };
980
981 static CPUWriteMemoryFunc *icp_control_writefn[] = {
982    icp_control_write,
983    icp_control_write,
984    icp_control_write
985 };
986
987 static void icp_control_init(uint32_t base)
988 {
989     int iomemtype;
990     icp_control_state *s;
991
992     s = (icp_control_state *)qemu_mallocz(sizeof(icp_control_state));
993     iomemtype = cpu_register_io_memory(0, icp_control_readfn,
994                                        icp_control_writefn, s);
995     cpu_register_physical_memory(base, 0x007fffff, iomemtype);
996     s->base = base;
997     /* ??? Save/restore.  */
998 }
999
1000
1001 /* Keyboard/Mouse Interface.  */
1002
1003 typedef struct {
1004     void *dev;
1005     uint32_t base;
1006     uint32_t cr;
1007     uint32_t clk;
1008     uint32_t last;
1009     icp_pic_state *pic;
1010     int pending;
1011     int irq;
1012     int is_mouse;
1013 } icp_kmi_state;
1014
1015 static void icp_kmi_update(void *opaque, int level)
1016 {
1017     icp_kmi_state *s = (icp_kmi_state *)opaque;
1018     int raise;
1019
1020     s->pending = level;
1021     raise = (s->pending && (s->cr & 0x10) != 0)
1022             || (s->cr & 0x08) != 0;
1023     icp_pic_set_level(s->pic, s->irq, raise);
1024 }
1025
1026 static uint32_t icp_kmi_read(void *opaque, target_phys_addr_t offset)
1027 {
1028     icp_kmi_state *s = (icp_kmi_state *)opaque;
1029     offset -= s->base;
1030     if (offset >= 0xfe0 && offset < 0x1000)
1031         return 0;
1032
1033     switch (offset >> 2) {
1034     case 0: /* KMICR */
1035         return s->cr;
1036     case 1: /* KMISTAT */
1037         /* KMIC and KMID bits not implemented.  */
1038         if (s->pending) {
1039             return 0x10;
1040         } else {
1041             return 0;
1042         }
1043     case 2: /* KMIDATA */
1044         if (s->pending)
1045             s->last = ps2_read_data(s->dev);
1046         return s->last;
1047     case 3: /* KMICLKDIV */
1048         return s->clk;
1049     case 4: /* KMIIR */
1050         return s->pending | 2;
1051     default:
1052         cpu_abort (cpu_single_env, "icp_kmi_read: Bad offset %x\n", offset);
1053         return 0;
1054     }
1055 }
1056
1057 static void icp_kmi_write(void *opaque, target_phys_addr_t offset,
1058                           uint32_t value)
1059 {
1060     icp_kmi_state *s = (icp_kmi_state *)opaque;
1061     offset -= s->base;
1062     switch (offset >> 2) {
1063     case 0: /* KMICR */
1064         s->cr = value;
1065         icp_kmi_update(s, s->pending);
1066         /* ??? Need to implement the enable/disable bit.  */
1067         break;
1068     case 2: /* KMIDATA */
1069         /* ??? This should toggle the TX interrupt line.  */
1070         /* ??? This means kbd/mouse can block each other.  */
1071         if (s->is_mouse) {
1072             ps2_write_mouse(s->dev, value);
1073         } else {
1074             ps2_write_keyboard(s->dev, value);
1075         }
1076         break;
1077     case 3: /* KMICLKDIV */
1078         s->clk = value;
1079         return;
1080     default:
1081         cpu_abort (cpu_single_env, "icp_kmi_write: Bad offset %x\n", offset);
1082     }
1083 }
1084 static CPUReadMemoryFunc *icp_kmi_readfn[] = {
1085    icp_kmi_read,
1086    icp_kmi_read,
1087    icp_kmi_read
1088 };
1089
1090 static CPUWriteMemoryFunc *icp_kmi_writefn[] = {
1091    icp_kmi_write,
1092    icp_kmi_write,
1093    icp_kmi_write
1094 };
1095
1096 static void icp_kmi_init(uint32_t base, icp_pic_state * pic, int irq,
1097                          int is_mouse)
1098 {
1099     int iomemtype;
1100     icp_kmi_state *s;
1101
1102     s = (icp_kmi_state *)qemu_mallocz(sizeof(icp_kmi_state));
1103     iomemtype = cpu_register_io_memory(0, icp_kmi_readfn,
1104                                        icp_kmi_writefn, s);
1105     cpu_register_physical_memory(base, 0x007fffff, iomemtype);
1106     s->base = base;
1107     s->pic = pic;
1108     s->irq = irq;
1109     s->is_mouse = is_mouse;
1110     if (is_mouse)
1111         s->dev = ps2_mouse_init(icp_kmi_update, s);
1112     else
1113         s->dev = ps2_kbd_init(icp_kmi_update, s);
1114     /* ??? Save/restore.  */
1115 }
1116
1117 /* The worlds second smallest bootloader.  Set r0-r2, then jump to kernel.  */
1118 static uint32_t bootloader[] = {
1119   0xe3a00000, /* mov     r0, #0 */
1120   0xe3a01013, /* mov     r1, #0x13 */
1121   0xe3811c01, /* orr     r1, r1, #0x100 */
1122   0xe59f2000, /* ldr     r2, [pc, #0] */
1123   0xe59ff000, /* ldr     pc, [pc, #0] */
1124   0, /* Address of kernel args.  Set by integratorcp_init.  */
1125   0  /* Kernel entry point.  Set by integratorcp_init.  */
1126 };
1127
1128 static void set_kernel_args(uint32_t ram_size, int initrd_size,
1129                             const char *kernel_cmdline)
1130 {
1131     uint32_t *p;
1132
1133     p = (uint32_t *)(phys_ram_base + KERNEL_ARGS_ADDR);
1134     /* ATAG_CORE */
1135     *(p++) = 5;
1136     *(p++) = 0x54410001;
1137     *(p++) = 1;
1138     *(p++) = 0x1000;
1139     *(p++) = 0;
1140     /* ATAG_MEM */
1141     *(p++) = 4;
1142     *(p++) = 0x54410002;
1143     *(p++) = ram_size;
1144     *(p++) = 0;
1145     if (initrd_size) {
1146         /* ATAG_INITRD2 */
1147         *(p++) = 4;
1148         *(p++) = 0x54420005;
1149         *(p++) = INITRD_LOAD_ADDR;
1150         *(p++) = initrd_size;
1151     }
1152     if (kernel_cmdline && *kernel_cmdline) {
1153         /* ATAG_CMDLINE */
1154         int cmdline_size;
1155
1156         cmdline_size = strlen(kernel_cmdline);
1157         memcpy (p + 2, kernel_cmdline, cmdline_size + 1);
1158         cmdline_size = (cmdline_size >> 2) + 1;
1159         *(p++) = cmdline_size + 2;
1160         *(p++) = 0x54410009;
1161         p += cmdline_size;
1162     }
1163     /* ATAG_END */
1164     *(p++) = 0;
1165     *(p++) = 0;
1166 }
1167
1168 /* Board init.  */
1169
1170 static void integratorcp_init(int ram_size, int vga_ram_size, int boot_device,
1171                      DisplayState *ds, const char **fd_filename, int snapshot,
1172                      const char *kernel_filename, const char *kernel_cmdline,
1173                      const char *initrd_filename)
1174 {
1175     CPUState *env;
1176     uint32_t bios_offset;
1177     icp_pic_state *pic;
1178     int kernel_size;
1179     int initrd_size;
1180
1181     env = cpu_init();
1182     bios_offset = ram_size + vga_ram_size;
1183     /* ??? On a real system the first 1Mb is mapped as SSRAM or boot flash.  */
1184     /* ??? RAM shoud repeat to fill physical memory space.  */
1185     /* SDRAM at address zero*/
1186     cpu_register_physical_memory(0, ram_size, IO_MEM_RAM);
1187     /* And again at address 0x80000000 */
1188     cpu_register_physical_memory(0x80000000, ram_size, IO_MEM_RAM);
1189
1190     integratorcm_init(ram_size >> 20, bios_offset);
1191     pic = icp_pic_init(0x14000000, env, -1);
1192     icp_pic_init(0xca000000, pic, 26);
1193     icp_pit_init(0x13000000, pic);
1194     pl011_init(0x16000000, pic, 1, serial_hds[0]);
1195     pl011_init(0x17000000, pic, 2, serial_hds[1]);
1196     icp_control_init(0xcb000000);
1197     icp_kmi_init(0x18000000, pic, 3, 0);
1198     icp_kmi_init(0x19000000, pic, 4, 1);
1199
1200     /* Load the kernel.  */
1201     if (!kernel_filename) {
1202         fprintf(stderr, "Kernel image must be specified\n");
1203         exit(1);
1204     }
1205     kernel_size = load_image(kernel_filename,
1206                              phys_ram_base + KERNEL_LOAD_ADDR);
1207     if (kernel_size < 0) {
1208         fprintf(stderr, "qemu: could not load kernel '%s'\n", kernel_filename);
1209         exit(1);
1210     }
1211     if (initrd_filename) {
1212         initrd_size = load_image(initrd_filename,
1213                                  phys_ram_base + INITRD_LOAD_ADDR);
1214         if (initrd_size < 0) {
1215             fprintf(stderr, "qemu: could not load initrd '%s'\n",
1216                     initrd_filename);
1217             exit(1);
1218         }
1219     } else {
1220         initrd_size = 0;
1221     }
1222     bootloader[5] = KERNEL_ARGS_ADDR;
1223     bootloader[6] = KERNEL_LOAD_ADDR;
1224     memcpy(phys_ram_base, bootloader, sizeof(bootloader));
1225     set_kernel_args(ram_size, initrd_size, kernel_cmdline);
1226 }
1227
1228 QEMUMachine integratorcp_machine = {
1229     "integratorcp",
1230     "ARM Integrator/CP",
1231     integratorcp_init,
1232 };