Add PowerPC 405 input pins (IRQ, resets, ...) model.
[qemu] / hw / ppc.c
1 /*
2  * QEMU generic PowerPC hardware System Emulator
3  * 
4  * Copyright (c) 2003-2007 Jocelyn Mayer
5  * 
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #include "vl.h"
25 #include "m48t59.h"
26
27 //#define PPC_DEBUG_IRQ
28
29 extern FILE *logfile;
30 extern int loglevel;
31
32 void ppc_set_irq (CPUState *env, int n_IRQ, int level)
33 {
34     if (level) {
35         env->pending_interrupts |= 1 << n_IRQ;
36         cpu_interrupt(env, CPU_INTERRUPT_HARD);
37     } else {
38         env->pending_interrupts &= ~(1 << n_IRQ);
39         if (env->pending_interrupts == 0)
40             cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
41     }
42 #if defined(PPC_DEBUG_IRQ)
43     printf("%s: %p n_IRQ %d level %d => pending %08x req %08x\n", __func__,
44            env, n_IRQ, level, env->pending_interrupts, env->interrupt_request);
45 #endif
46 }
47
48 /* PowerPC 6xx / 7xx internal IRQ controller */
49 static void ppc6xx_set_irq (void *opaque, int pin, int level)
50 {
51     CPUState *env = opaque;
52     int cur_level;
53
54 #if defined(PPC_DEBUG_IRQ)
55     printf("%s: env %p pin %d level %d\n", __func__, env, pin, level);
56 #endif
57     cur_level = (env->irq_input_state >> pin) & 1;
58     /* Don't generate spurious events */
59     if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
60         switch (pin) {
61         case PPC6xx_INPUT_INT:
62             /* Level sensitive - active high */
63 #if defined(PPC_DEBUG_IRQ)
64             printf("%s: set the external IRQ state to %d\n", __func__, level);
65 #endif
66             ppc_set_irq(env, PPC_INTERRUPT_EXT, level);
67             break;
68         case PPC6xx_INPUT_SMI:
69             /* Level sensitive - active high */
70 #if defined(PPC_DEBUG_IRQ)
71             printf("%s: set the SMI IRQ state to %d\n", __func__, level);
72 #endif
73             ppc_set_irq(env, PPC_INTERRUPT_SMI, level);
74             break;
75         case PPC6xx_INPUT_MCP:
76             /* Negative edge sensitive */
77             /* XXX: TODO: actual reaction may depends on HID0 status
78              *            603/604/740/750: check HID0[EMCP]
79              */
80             if (cur_level == 1 && level == 0) {
81 #if defined(PPC_DEBUG_IRQ)
82                 printf("%s: raise machine check state\n", __func__);
83 #endif
84                 ppc_set_irq(env, PPC_INTERRUPT_MCK, 1);
85             }
86             break;
87         case PPC6xx_INPUT_CKSTP_IN:
88             /* Level sensitive - active low */
89             /* XXX: TODO: relay the signal to CKSTP_OUT pin */
90             if (level) {
91 #if defined(PPC_DEBUG_IRQ)
92                 printf("%s: stop the CPU\n", __func__);
93 #endif
94                 env->halted = 1;
95             } else {
96 #if defined(PPC_DEBUG_IRQ)
97                 printf("%s: restart the CPU\n", __func__);
98 #endif
99                 env->halted = 0;
100             }
101             break;
102         case PPC6xx_INPUT_HRESET:
103             /* Level sensitive - active low */
104             if (level) {
105 #if 0 // XXX: TOFIX
106 #if defined(PPC_DEBUG_IRQ)
107                 printf("%s: reset the CPU\n", __func__);
108 #endif
109                 cpu_reset(env);
110 #endif
111             }
112             break;
113         case PPC6xx_INPUT_SRESET:
114 #if defined(PPC_DEBUG_IRQ)
115             printf("%s: set the RESET IRQ state to %d\n", __func__, level);
116 #endif
117             ppc_set_irq(env, PPC_INTERRUPT_RESET, level);
118             break;
119         default:
120             /* Unknown pin - do nothing */
121 #if defined(PPC_DEBUG_IRQ)
122             printf("%s: unknown IRQ pin %d\n", __func__, pin);
123 #endif
124             return;
125         }
126         if (level)
127             env->irq_input_state |= 1 << pin;
128         else
129             env->irq_input_state &= ~(1 << pin);
130     }
131 }
132
133 void ppc6xx_irq_init (CPUState *env)
134 {
135     env->irq_inputs = (void **)qemu_allocate_irqs(&ppc6xx_set_irq, env, 6);
136 }
137
138 /* PowerPC 405 internal IRQ controller */
139 static void ppc405_set_irq (void *opaque, int pin, int level)
140 {
141     CPUState *env = opaque;
142     int cur_level;
143
144 #if defined(PPC_DEBUG_IRQ)
145     printf("%s: env %p pin %d level %d\n", __func__, env, pin, level);
146 #endif
147     cur_level = (env->irq_input_state >> pin) & 1;
148     /* Don't generate spurious events */
149     if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
150         switch (pin) {
151         case PPC405_INPUT_RESET_SYS:
152             /* XXX: TODO: reset all peripherals */
153             /* No break here */
154         case PPC405_INPUT_RESET_CHIP:
155             /* XXX: TODO: reset on-chip peripherals */
156             /* No break here */
157         case PPC405_INPUT_RESET_CORE:
158             /* XXX: TODO: update DBSR[MRR] */
159             if (level) {
160 #if 0 // XXX: TOFIX
161 #if defined(PPC_DEBUG_IRQ)
162                 printf("%s: reset the CPU\n", __func__);
163 #endif
164                 cpu_reset(env);
165 #endif
166             }
167             break;
168         case PPC405_INPUT_CINT:
169             /* Level sensitive - active high */
170 #if defined(PPC_DEBUG_IRQ)
171             printf("%s: set the critical IRQ state to %d\n", __func__, level);
172 #endif
173             /* XXX: TOFIX */
174             ppc_set_irq(env, PPC_INTERRUPT_RESET, level);
175             break;
176         case PPC405_INPUT_INT:
177             /* Level sensitive - active high */
178 #if defined(PPC_DEBUG_IRQ)
179             printf("%s: set the external IRQ state to %d\n", __func__, level);
180 #endif
181             ppc_set_irq(env, PPC_INTERRUPT_EXT, level);
182             break;
183         case PPC405_INPUT_HALT:
184             /* Level sensitive - active low */
185             if (level) {
186 #if defined(PPC_DEBUG_IRQ)
187                 printf("%s: stop the CPU\n", __func__);
188 #endif
189                 env->halted = 1;
190             } else {
191 #if defined(PPC_DEBUG_IRQ)
192                 printf("%s: restart the CPU\n", __func__);
193 #endif
194                 env->halted = 0;
195             }
196             break;
197         case PPC405_INPUT_DEBUG:
198             /* Level sensitive - active high */
199 #if defined(PPC_DEBUG_IRQ)
200             printf("%s: set the external IRQ state to %d\n", __func__, level);
201 #endif
202             ppc_set_irq(env, EXCP_40x_DEBUG, level);
203             break;
204         default:
205             /* Unknown pin - do nothing */
206 #if defined(PPC_DEBUG_IRQ)
207             printf("%s: unknown IRQ pin %d\n", __func__, pin);
208 #endif
209             return;
210         }
211         if (level)
212             env->irq_input_state |= 1 << pin;
213         else
214             env->irq_input_state &= ~(1 << pin);
215     }
216 }
217
218 void ppc405_irq_init (CPUState *env)
219 {
220     printf("%s\n", __func__);
221     env->irq_inputs = (void **)qemu_allocate_irqs(&ppc405_set_irq, env, 7);
222 }
223
224 /*****************************************************************************/
225 /* PowerPC time base and decrementer emulation */
226 //#define DEBUG_TB
227
228 struct ppc_tb_t {
229     /* Time base management */
230     int64_t  tb_offset;    /* Compensation               */
231     uint32_t tb_freq;      /* TB frequency               */
232     /* Decrementer management */
233     uint64_t decr_next;    /* Tick for next decr interrupt  */
234     struct QEMUTimer *decr_timer;
235     void *opaque;
236 };
237
238 static inline uint64_t cpu_ppc_get_tb (ppc_tb_t *tb_env)
239 {
240     /* TB time in tb periods */
241     return muldiv64(qemu_get_clock(vm_clock) + tb_env->tb_offset,
242                     tb_env->tb_freq, ticks_per_sec);
243 }
244
245 uint32_t cpu_ppc_load_tbl (CPUState *env)
246 {
247     ppc_tb_t *tb_env = env->tb_env;
248     uint64_t tb;
249
250     tb = cpu_ppc_get_tb(tb_env);
251 #ifdef DEBUG_TB
252     {
253         static int last_time;
254         int now;
255         now = time(NULL);
256         if (last_time != now) {
257             last_time = now;
258             printf("%s: tb=0x%016lx %d %08lx\n",
259                    __func__, tb, now, tb_env->tb_offset);
260         }
261     }
262 #endif
263
264     return tb & 0xFFFFFFFF;
265 }
266
267 uint32_t cpu_ppc_load_tbu (CPUState *env)
268 {
269     ppc_tb_t *tb_env = env->tb_env;
270     uint64_t tb;
271
272     tb = cpu_ppc_get_tb(tb_env);
273 #ifdef DEBUG_TB
274     printf("%s: tb=0x%016lx\n", __func__, tb);
275 #endif
276
277     return tb >> 32;
278 }
279
280 static void cpu_ppc_store_tb (ppc_tb_t *tb_env, uint64_t value)
281 {
282     tb_env->tb_offset = muldiv64(value, ticks_per_sec, tb_env->tb_freq)
283         - qemu_get_clock(vm_clock);
284 #ifdef DEBUG_TB
285     printf("%s: tb=0x%016lx offset=%08x\n", __func__, value);
286 #endif
287 }
288
289 void cpu_ppc_store_tbu (CPUState *env, uint32_t value)
290 {
291     ppc_tb_t *tb_env = env->tb_env;
292
293     cpu_ppc_store_tb(tb_env,
294                      ((uint64_t)value << 32) | cpu_ppc_load_tbl(env));
295 }
296
297 void cpu_ppc_store_tbl (CPUState *env, uint32_t value)
298 {
299     ppc_tb_t *tb_env = env->tb_env;
300
301     cpu_ppc_store_tb(tb_env,
302                      ((uint64_t)cpu_ppc_load_tbu(env) << 32) | value);
303 }
304
305 uint32_t cpu_ppc_load_decr (CPUState *env)
306 {
307     ppc_tb_t *tb_env = env->tb_env;
308     uint32_t decr;
309     int64_t diff;
310
311     diff = tb_env->decr_next - qemu_get_clock(vm_clock);
312     if (diff >= 0)
313         decr = muldiv64(diff, tb_env->tb_freq, ticks_per_sec);
314     else
315         decr = -muldiv64(-diff, tb_env->tb_freq, ticks_per_sec);
316 #if defined(DEBUG_TB)
317     printf("%s: 0x%08x\n", __func__, decr);
318 #endif
319
320     return decr;
321 }
322
323 /* When decrementer expires,
324  * all we need to do is generate or queue a CPU exception
325  */
326 static inline void cpu_ppc_decr_excp (CPUState *env)
327 {
328     /* Raise it */
329 #ifdef DEBUG_TB
330     printf("raise decrementer exception\n");
331 #endif
332     ppc_set_irq(env, PPC_INTERRUPT_DECR, 1);
333 }
334
335 static void _cpu_ppc_store_decr (CPUState *env, uint32_t decr,
336                                  uint32_t value, int is_excp)
337 {
338     ppc_tb_t *tb_env = env->tb_env;
339     uint64_t now, next;
340
341 #ifdef DEBUG_TB
342     printf("%s: 0x%08x => 0x%08x\n", __func__, decr, value);
343 #endif
344     now = qemu_get_clock(vm_clock);
345     next = now + muldiv64(value, ticks_per_sec, tb_env->tb_freq);
346     if (is_excp)
347         next += tb_env->decr_next - now;
348     if (next == now)
349         next++;
350     tb_env->decr_next = next;
351     /* Adjust timer */
352     qemu_mod_timer(tb_env->decr_timer, next);
353     /* If we set a negative value and the decrementer was positive,
354      * raise an exception.
355      */
356     if ((value & 0x80000000) && !(decr & 0x80000000))
357         cpu_ppc_decr_excp(env);
358 }
359
360 void cpu_ppc_store_decr (CPUState *env, uint32_t value)
361 {
362     _cpu_ppc_store_decr(env, cpu_ppc_load_decr(env), value, 0);
363 }
364
365 static void cpu_ppc_decr_cb (void *opaque)
366 {
367     _cpu_ppc_store_decr(opaque, 0x00000000, 0xFFFFFFFF, 1);
368 }
369
370 /* Set up (once) timebase frequency (in Hz) */
371 ppc_tb_t *cpu_ppc_tb_init (CPUState *env, uint32_t freq)
372 {
373     ppc_tb_t *tb_env;
374
375     tb_env = qemu_mallocz(sizeof(ppc_tb_t));
376     if (tb_env == NULL)
377         return NULL;
378     env->tb_env = tb_env;
379     if (tb_env->tb_freq == 0 || 1) {
380         tb_env->tb_freq = freq;
381         /* Create new timer */
382         tb_env->decr_timer =
383             qemu_new_timer(vm_clock, &cpu_ppc_decr_cb, env);
384         /* There is a bug in Linux 2.4 kernels:
385          * if a decrementer exception is pending when it enables msr_ee,
386          * it's not ready to handle it...
387          */
388         _cpu_ppc_store_decr(env, 0xFFFFFFFF, 0xFFFFFFFF, 0);
389     }
390
391     return tb_env;
392 }
393
394 /* Specific helpers for POWER & PowerPC 601 RTC */
395 ppc_tb_t *cpu_ppc601_rtc_init (CPUState *env)
396 {
397     return cpu_ppc_tb_init(env, 7812500);
398 }
399
400 void cpu_ppc601_store_rtcu (CPUState *env, uint32_t value)
401 __attribute__ (( alias ("cpu_ppc_store_tbu") ));
402
403 uint32_t cpu_ppc601_load_rtcu (CPUState *env)
404 __attribute__ (( alias ("cpu_ppc_load_tbu") ));
405
406 void cpu_ppc601_store_rtcl (CPUState *env, uint32_t value)
407 {
408     cpu_ppc_store_tbl(env, value & 0x3FFFFF80);
409 }
410
411 uint32_t cpu_ppc601_load_rtcl (CPUState *env)
412 {
413     return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
414 }
415
416 /*****************************************************************************/
417 /* Embedded PowerPC timers */
418
419 /* PIT, FIT & WDT */
420 typedef struct ppcemb_timer_t ppcemb_timer_t;
421 struct ppcemb_timer_t {
422     uint64_t pit_reload;  /* PIT auto-reload value        */
423     uint64_t fit_next;    /* Tick for next FIT interrupt  */
424     struct QEMUTimer *fit_timer;
425     uint64_t wdt_next;    /* Tick for next WDT interrupt  */
426     struct QEMUTimer *wdt_timer;
427 };
428    
429 /* Fixed interval timer */
430 static void cpu_4xx_fit_cb (void *opaque)
431 {
432     CPUState *env;
433     ppc_tb_t *tb_env;
434     ppcemb_timer_t *ppcemb_timer;
435     uint64_t now, next;
436
437     env = opaque;
438     tb_env = env->tb_env;
439     ppcemb_timer = tb_env->opaque;
440     now = qemu_get_clock(vm_clock);
441     switch ((env->spr[SPR_40x_TCR] >> 24) & 0x3) {
442     case 0:
443         next = 1 << 9;
444         break;
445     case 1:
446         next = 1 << 13;
447         break;
448     case 2:
449         next = 1 << 17;
450         break;
451     case 3:
452         next = 1 << 21;
453         break;
454     default:
455         /* Cannot occur, but makes gcc happy */
456         return;
457     }
458     next = now + muldiv64(next, ticks_per_sec, tb_env->tb_freq);
459     if (next == now)
460         next++;
461     qemu_mod_timer(ppcemb_timer->fit_timer, next);
462     tb_env->decr_next = next;
463     env->spr[SPR_40x_TSR] |= 1 << 26;
464     if ((env->spr[SPR_40x_TCR] >> 23) & 0x1)
465         ppc_set_irq(env, PPC_INTERRUPT_FIT, 1);
466     if (loglevel) {
467         fprintf(logfile, "%s: ir %d TCR %08x TSR %08x\n", __func__,
468                 (env->spr[SPR_40x_TCR] >> 23) & 0x1,
469                 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
470     }
471 }
472
473 /* Programmable interval timer */
474 static void cpu_4xx_pit_cb (void *opaque)
475 {
476     CPUState *env;
477     ppc_tb_t *tb_env;
478     ppcemb_timer_t *ppcemb_timer;
479     uint64_t now, next;
480
481     env = opaque;
482     tb_env = env->tb_env;
483     ppcemb_timer = tb_env->opaque;
484     now = qemu_get_clock(vm_clock);
485     if ((env->spr[SPR_40x_TCR] >> 22) & 0x1) {
486         /* Auto reload */
487         next = now + muldiv64(ppcemb_timer->pit_reload,
488                               ticks_per_sec, tb_env->tb_freq);
489         if (next == now)
490             next++;
491         qemu_mod_timer(tb_env->decr_timer, next);
492         tb_env->decr_next = next;
493     }
494     env->spr[SPR_40x_TSR] |= 1 << 27;
495     if ((env->spr[SPR_40x_TCR] >> 26) & 0x1)
496         ppc_set_irq(env, PPC_INTERRUPT_PIT, 1);
497     if (loglevel) {
498         fprintf(logfile, "%s: ar %d ir %d TCR %08x TSR %08x %08lx\n", __func__,
499                 (env->spr[SPR_40x_TCR] >> 22) & 0x1,
500                 (env->spr[SPR_40x_TCR] >> 26) & 0x1,
501                 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR],
502                 ppcemb_timer->pit_reload);
503     }
504 }
505
506 /* Watchdog timer */
507 static void cpu_4xx_wdt_cb (void *opaque)
508 {
509     CPUState *env;
510     ppc_tb_t *tb_env;
511     ppcemb_timer_t *ppcemb_timer;
512     uint64_t now, next;
513
514     env = opaque;
515     tb_env = env->tb_env;
516     ppcemb_timer = tb_env->opaque;
517     now = qemu_get_clock(vm_clock);
518     switch ((env->spr[SPR_40x_TCR] >> 30) & 0x3) {
519     case 0:
520         next = 1 << 17;
521         break;
522     case 1:
523         next = 1 << 21;
524         break;
525     case 2:
526         next = 1 << 25;
527         break;
528     case 3:
529         next = 1 << 29;
530         break;
531     default:
532         /* Cannot occur, but makes gcc happy */
533         return;
534     }
535     next = now + muldiv64(next, ticks_per_sec, tb_env->tb_freq);
536     if (next == now)
537         next++;
538     if (loglevel) {
539         fprintf(logfile, "%s: TCR %08x TSR %08x\n", __func__,
540                 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
541     }
542     switch ((env->spr[SPR_40x_TSR] >> 30) & 0x3) {
543     case 0x0:
544     case 0x1:
545         qemu_mod_timer(ppcemb_timer->wdt_timer, next);
546         ppcemb_timer->wdt_next = next;
547         env->spr[SPR_40x_TSR] |= 1 << 31;
548         break;
549     case 0x2:
550         qemu_mod_timer(ppcemb_timer->wdt_timer, next);
551         ppcemb_timer->wdt_next = next;
552         env->spr[SPR_40x_TSR] |= 1 << 30;
553         if ((env->spr[SPR_40x_TCR] >> 27) & 0x1)
554             ppc_set_irq(env, PPC_INTERRUPT_WDT, 1);
555         break;
556     case 0x3:
557         env->spr[SPR_40x_TSR] &= ~0x30000000;
558         env->spr[SPR_40x_TSR] |= env->spr[SPR_40x_TCR] & 0x30000000;
559         switch ((env->spr[SPR_40x_TCR] >> 28) & 0x3) {
560         case 0x0:
561             /* No reset */
562             break;
563         case 0x1: /* Core reset */
564         case 0x2: /* Chip reset */
565         case 0x3: /* System reset */
566             qemu_system_reset_request();
567             return;
568         }
569     }
570 }
571
572 void store_40x_pit (CPUState *env, target_ulong val)
573 {
574     ppc_tb_t *tb_env;
575     ppcemb_timer_t *ppcemb_timer;
576     uint64_t now, next;
577
578     tb_env = env->tb_env;
579     ppcemb_timer = tb_env->opaque;
580     if (loglevel)
581         fprintf(logfile, "%s %p %p\n", __func__, tb_env, ppcemb_timer);
582     ppcemb_timer->pit_reload = val;
583     if (val == 0) {
584         /* Stop PIT */
585         if (loglevel)
586             fprintf(logfile, "%s: stop PIT\n", __func__);
587         qemu_del_timer(tb_env->decr_timer);
588     } else {
589         if (loglevel)
590             fprintf(logfile, "%s: start PIT 0x%08x\n", __func__, val);
591         now = qemu_get_clock(vm_clock);
592         next = now + muldiv64(val, ticks_per_sec, tb_env->tb_freq);
593          if (next == now)
594             next++;
595         qemu_mod_timer(tb_env->decr_timer, next);
596         tb_env->decr_next = next;
597     }
598 }
599
600 target_ulong load_40x_pit (CPUState *env)
601 {
602     return cpu_ppc_load_decr(env);
603 }
604
605 void store_booke_tsr (CPUState *env, target_ulong val)
606 {
607     env->spr[SPR_40x_TSR] = val & 0xFC000000;
608 }
609
610 void store_booke_tcr (CPUState *env, target_ulong val)
611 {
612     /* We don't update timers now. Maybe we should... */
613     env->spr[SPR_40x_TCR] = val & 0xFF800000;
614 }
615
616 void ppc_emb_timers_init (CPUState *env)
617 {
618     ppc_tb_t *tb_env;
619     ppcemb_timer_t *ppcemb_timer;
620
621     tb_env = env->tb_env;
622     ppcemb_timer = qemu_mallocz(sizeof(ppcemb_timer_t));
623     tb_env->opaque = ppcemb_timer;
624     if (loglevel)
625         fprintf(logfile, "%s %p %p\n", __func__, tb_env, ppcemb_timer);
626     if (ppcemb_timer != NULL) {
627         /* We use decr timer for PIT */
628         tb_env->decr_timer = qemu_new_timer(vm_clock, &cpu_4xx_pit_cb, env);
629         ppcemb_timer->fit_timer =
630             qemu_new_timer(vm_clock, &cpu_4xx_fit_cb, env);
631         ppcemb_timer->wdt_timer =
632             qemu_new_timer(vm_clock, &cpu_4xx_wdt_cb, env);
633     }
634 }
635
636 /*****************************************************************************/
637 /* Embedded PowerPC Device Control Registers */
638 typedef struct ppc_dcrn_t ppc_dcrn_t;
639 struct ppc_dcrn_t {
640     dcr_read_cb dcr_read;
641     dcr_write_cb dcr_write;
642     void *opaque;
643 };
644
645 #define DCRN_NB 1024
646 struct ppc_dcr_t {
647     ppc_dcrn_t dcrn[DCRN_NB];
648     int (*read_error)(int dcrn);
649     int (*write_error)(int dcrn);
650 };
651
652 int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, target_ulong *valp)
653 {
654     ppc_dcrn_t *dcr;
655
656     if (dcrn < 0 || dcrn >= DCRN_NB)
657         goto error;
658     dcr = &dcr_env->dcrn[dcrn];
659     if (dcr->dcr_read == NULL)
660         goto error;
661     *valp = (*dcr->dcr_read)(dcr->opaque, dcrn);
662
663     return 0;
664
665  error:
666     if (dcr_env->read_error != NULL)
667         return (*dcr_env->read_error)(dcrn);
668
669     return -1;
670 }
671
672 int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, target_ulong val)
673 {
674     ppc_dcrn_t *dcr;
675
676     if (dcrn < 0 || dcrn >= DCRN_NB)
677         goto error;
678     dcr = &dcr_env->dcrn[dcrn];
679     if (dcr->dcr_write == NULL)
680         goto error;
681     (*dcr->dcr_write)(dcr->opaque, dcrn, val);
682
683     return 0;
684
685  error:
686     if (dcr_env->write_error != NULL)
687         return (*dcr_env->write_error)(dcrn);
688
689     return -1;
690 }
691
692 int ppc_dcr_register (CPUState *env, int dcrn, void *opaque,
693                       dcr_read_cb dcr_read, dcr_write_cb dcr_write)
694 {
695     ppc_dcr_t *dcr_env;
696     ppc_dcrn_t *dcr;
697
698     dcr_env = env->dcr_env;
699     if (dcr_env == NULL)
700         return -1;
701     if (dcrn < 0 || dcrn >= DCRN_NB)
702         return -1;
703     dcr = &dcr_env->dcrn[dcrn];
704     if (dcr->opaque != NULL ||
705         dcr->dcr_read != NULL ||
706         dcr->dcr_write != NULL)
707         return -1;
708     dcr->opaque = opaque;
709     dcr->dcr_read = dcr_read;
710     dcr->dcr_write = dcr_write;
711
712     return 0;
713 }
714
715 int ppc_dcr_init (CPUState *env, int (*read_error)(int dcrn),
716                   int (*write_error)(int dcrn))
717 {
718     ppc_dcr_t *dcr_env;
719
720     dcr_env = qemu_mallocz(sizeof(ppc_dcr_t));
721     if (dcr_env == NULL)
722         return -1;
723     dcr_env->read_error = read_error;
724     dcr_env->write_error = write_error;
725     env->dcr_env = dcr_env;
726
727     return 0;
728 }
729
730
731 #if 0
732 /*****************************************************************************/
733 /* Handle system reset (for now, just stop emulation) */
734 void cpu_ppc_reset (CPUState *env)
735 {
736     printf("Reset asked... Stop emulation\n");
737     abort();
738 }
739 #endif
740
741 /*****************************************************************************/
742 /* Debug port */
743 void PPC_debug_write (void *opaque, uint32_t addr, uint32_t val)
744 {
745     addr &= 0xF;
746     switch (addr) {
747     case 0:
748         printf("%c", val);
749         break;
750     case 1:
751         printf("\n");
752         fflush(stdout);
753         break;
754     case 2:
755         printf("Set loglevel to %04x\n", val);
756         cpu_set_log(val | 0x100);
757         break;
758     }
759 }
760
761 /*****************************************************************************/
762 /* NVRAM helpers */
763 void NVRAM_set_byte (m48t59_t *nvram, uint32_t addr, uint8_t value)
764 {
765     m48t59_write(nvram, addr, value);
766 }
767
768 uint8_t NVRAM_get_byte (m48t59_t *nvram, uint32_t addr)
769 {
770     return m48t59_read(nvram, addr);
771 }
772
773 void NVRAM_set_word (m48t59_t *nvram, uint32_t addr, uint16_t value)
774 {
775     m48t59_write(nvram, addr, value >> 8);
776     m48t59_write(nvram, addr + 1, value & 0xFF);
777 }
778
779 uint16_t NVRAM_get_word (m48t59_t *nvram, uint32_t addr)
780 {
781     uint16_t tmp;
782
783     tmp = m48t59_read(nvram, addr) << 8;
784     tmp |= m48t59_read(nvram, addr + 1);
785     return tmp;
786 }
787
788 void NVRAM_set_lword (m48t59_t *nvram, uint32_t addr, uint32_t value)
789 {
790     m48t59_write(nvram, addr, value >> 24);
791     m48t59_write(nvram, addr + 1, (value >> 16) & 0xFF);
792     m48t59_write(nvram, addr + 2, (value >> 8) & 0xFF);
793     m48t59_write(nvram, addr + 3, value & 0xFF);
794 }
795
796 uint32_t NVRAM_get_lword (m48t59_t *nvram, uint32_t addr)
797 {
798     uint32_t tmp;
799
800     tmp = m48t59_read(nvram, addr) << 24;
801     tmp |= m48t59_read(nvram, addr + 1) << 16;
802     tmp |= m48t59_read(nvram, addr + 2) << 8;
803     tmp |= m48t59_read(nvram, addr + 3);
804
805     return tmp;
806 }
807
808 void NVRAM_set_string (m48t59_t *nvram, uint32_t addr,
809                        const unsigned char *str, uint32_t max)
810 {
811     int i;
812
813     for (i = 0; i < max && str[i] != '\0'; i++) {
814         m48t59_write(nvram, addr + i, str[i]);
815     }
816     m48t59_write(nvram, addr + max - 1, '\0');
817 }
818
819 int NVRAM_get_string (m48t59_t *nvram, uint8_t *dst, uint16_t addr, int max)
820 {
821     int i;
822
823     memset(dst, 0, max);
824     for (i = 0; i < max; i++) {
825         dst[i] = NVRAM_get_byte(nvram, addr + i);
826         if (dst[i] == '\0')
827             break;
828     }
829
830     return i;
831 }
832
833 static uint16_t NVRAM_crc_update (uint16_t prev, uint16_t value)
834 {
835     uint16_t tmp;
836     uint16_t pd, pd1, pd2;
837
838     tmp = prev >> 8;
839     pd = prev ^ value;
840     pd1 = pd & 0x000F;
841     pd2 = ((pd >> 4) & 0x000F) ^ pd1;
842     tmp ^= (pd1 << 3) | (pd1 << 8);
843     tmp ^= pd2 | (pd2 << 7) | (pd2 << 12);
844
845     return tmp;
846 }
847
848 uint16_t NVRAM_compute_crc (m48t59_t *nvram, uint32_t start, uint32_t count)
849 {
850     uint32_t i;
851     uint16_t crc = 0xFFFF;
852     int odd;
853
854     odd = count & 1;
855     count &= ~1;
856     for (i = 0; i != count; i++) {
857         crc = NVRAM_crc_update(crc, NVRAM_get_word(nvram, start + i));
858     }
859     if (odd) {
860         crc = NVRAM_crc_update(crc, NVRAM_get_byte(nvram, start + i) << 8);
861     }
862
863     return crc;
864 }
865
866 #define CMDLINE_ADDR 0x017ff000
867
868 int PPC_NVRAM_set_params (m48t59_t *nvram, uint16_t NVRAM_size,
869                           const unsigned char *arch,
870                           uint32_t RAM_size, int boot_device,
871                           uint32_t kernel_image, uint32_t kernel_size,
872                           const char *cmdline,
873                           uint32_t initrd_image, uint32_t initrd_size,
874                           uint32_t NVRAM_image,
875                           int width, int height, int depth)
876 {
877     uint16_t crc;
878
879     /* Set parameters for Open Hack'Ware BIOS */
880     NVRAM_set_string(nvram, 0x00, "QEMU_BIOS", 16);
881     NVRAM_set_lword(nvram,  0x10, 0x00000002); /* structure v2 */
882     NVRAM_set_word(nvram,   0x14, NVRAM_size);
883     NVRAM_set_string(nvram, 0x20, arch, 16);
884     NVRAM_set_lword(nvram,  0x30, RAM_size);
885     NVRAM_set_byte(nvram,   0x34, boot_device);
886     NVRAM_set_lword(nvram,  0x38, kernel_image);
887     NVRAM_set_lword(nvram,  0x3C, kernel_size);
888     if (cmdline) {
889         /* XXX: put the cmdline in NVRAM too ? */
890         strcpy(phys_ram_base + CMDLINE_ADDR, cmdline);
891         NVRAM_set_lword(nvram,  0x40, CMDLINE_ADDR);
892         NVRAM_set_lword(nvram,  0x44, strlen(cmdline));
893     } else {
894         NVRAM_set_lword(nvram,  0x40, 0);
895         NVRAM_set_lword(nvram,  0x44, 0);
896     }
897     NVRAM_set_lword(nvram,  0x48, initrd_image);
898     NVRAM_set_lword(nvram,  0x4C, initrd_size);
899     NVRAM_set_lword(nvram,  0x50, NVRAM_image);
900
901     NVRAM_set_word(nvram,   0x54, width);
902     NVRAM_set_word(nvram,   0x56, height);
903     NVRAM_set_word(nvram,   0x58, depth);
904     crc = NVRAM_compute_crc(nvram, 0x00, 0xF8);
905     NVRAM_set_word(nvram,  0xFC, crc);
906
907     return 0;
908 }