Embedded PowerPC Device Control Registers infrastructure.
[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) || 0) {
60         switch (pin) {
61         case PPC_INPUT_INT:
62             /* Level sensitive - asserted 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 PPC_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 PPC_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 PPC_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 PPC_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 PPC_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 /*****************************************************************************/
139 /* PowerPC time base and decrementer emulation */
140 //#define DEBUG_TB
141
142 struct ppc_tb_t {
143     /* Time base management */
144     int64_t  tb_offset;    /* Compensation               */
145     uint32_t tb_freq;      /* TB frequency               */
146     /* Decrementer management */
147     uint64_t decr_next;    /* Tick for next decr interrupt  */
148     struct QEMUTimer *decr_timer;
149     void *opaque;
150 };
151
152 static inline uint64_t cpu_ppc_get_tb (ppc_tb_t *tb_env)
153 {
154     /* TB time in tb periods */
155     return muldiv64(qemu_get_clock(vm_clock) + tb_env->tb_offset,
156                     tb_env->tb_freq, ticks_per_sec);
157 }
158
159 uint32_t cpu_ppc_load_tbl (CPUState *env)
160 {
161     ppc_tb_t *tb_env = env->tb_env;
162     uint64_t tb;
163
164     tb = cpu_ppc_get_tb(tb_env);
165 #ifdef DEBUG_TB
166     {
167         static int last_time;
168         int now;
169         now = time(NULL);
170         if (last_time != now) {
171             last_time = now;
172             printf("%s: tb=0x%016lx %d %08lx\n",
173                    __func__, tb, now, tb_env->tb_offset);
174         }
175     }
176 #endif
177
178     return tb & 0xFFFFFFFF;
179 }
180
181 uint32_t cpu_ppc_load_tbu (CPUState *env)
182 {
183     ppc_tb_t *tb_env = env->tb_env;
184     uint64_t tb;
185
186     tb = cpu_ppc_get_tb(tb_env);
187 #ifdef DEBUG_TB
188     printf("%s: tb=0x%016lx\n", __func__, tb);
189 #endif
190
191     return tb >> 32;
192 }
193
194 static void cpu_ppc_store_tb (ppc_tb_t *tb_env, uint64_t value)
195 {
196     tb_env->tb_offset = muldiv64(value, ticks_per_sec, tb_env->tb_freq)
197         - qemu_get_clock(vm_clock);
198 #ifdef DEBUG_TB
199     printf("%s: tb=0x%016lx offset=%08x\n", __func__, value);
200 #endif
201 }
202
203 void cpu_ppc_store_tbu (CPUState *env, uint32_t value)
204 {
205     ppc_tb_t *tb_env = env->tb_env;
206
207     cpu_ppc_store_tb(tb_env,
208                      ((uint64_t)value << 32) | cpu_ppc_load_tbl(env));
209 }
210
211 void cpu_ppc_store_tbl (CPUState *env, uint32_t value)
212 {
213     ppc_tb_t *tb_env = env->tb_env;
214
215     cpu_ppc_store_tb(tb_env,
216                      ((uint64_t)cpu_ppc_load_tbu(env) << 32) | value);
217 }
218
219 uint32_t cpu_ppc_load_decr (CPUState *env)
220 {
221     ppc_tb_t *tb_env = env->tb_env;
222     uint32_t decr;
223     int64_t diff;
224
225     diff = tb_env->decr_next - qemu_get_clock(vm_clock);
226     if (diff >= 0)
227         decr = muldiv64(diff, tb_env->tb_freq, ticks_per_sec);
228     else
229         decr = -muldiv64(-diff, tb_env->tb_freq, ticks_per_sec);
230 #if defined(DEBUG_TB)
231     printf("%s: 0x%08x\n", __func__, decr);
232 #endif
233
234     return decr;
235 }
236
237 /* When decrementer expires,
238  * all we need to do is generate or queue a CPU exception
239  */
240 static inline void cpu_ppc_decr_excp (CPUState *env)
241 {
242     /* Raise it */
243 #ifdef DEBUG_TB
244     printf("raise decrementer exception\n");
245 #endif
246     ppc_set_irq(env, PPC_INTERRUPT_DECR, 1);
247 }
248
249 static void _cpu_ppc_store_decr (CPUState *env, uint32_t decr,
250                                  uint32_t value, int is_excp)
251 {
252     ppc_tb_t *tb_env = env->tb_env;
253     uint64_t now, next;
254
255 #ifdef DEBUG_TB
256     printf("%s: 0x%08x => 0x%08x\n", __func__, decr, value);
257 #endif
258     now = qemu_get_clock(vm_clock);
259     next = now + muldiv64(value, ticks_per_sec, tb_env->tb_freq);
260     if (is_excp)
261         next += tb_env->decr_next - now;
262     if (next == now)
263         next++;
264     tb_env->decr_next = next;
265     /* Adjust timer */
266     qemu_mod_timer(tb_env->decr_timer, next);
267     /* If we set a negative value and the decrementer was positive,
268      * raise an exception.
269      */
270     if ((value & 0x80000000) && !(decr & 0x80000000))
271         cpu_ppc_decr_excp(env);
272 }
273
274 void cpu_ppc_store_decr (CPUState *env, uint32_t value)
275 {
276     _cpu_ppc_store_decr(env, cpu_ppc_load_decr(env), value, 0);
277 }
278
279 static void cpu_ppc_decr_cb (void *opaque)
280 {
281     _cpu_ppc_store_decr(opaque, 0x00000000, 0xFFFFFFFF, 1);
282 }
283
284 /* Set up (once) timebase frequency (in Hz) */
285 ppc_tb_t *cpu_ppc_tb_init (CPUState *env, uint32_t freq)
286 {
287     ppc_tb_t *tb_env;
288
289     tb_env = qemu_mallocz(sizeof(ppc_tb_t));
290     if (tb_env == NULL)
291         return NULL;
292     env->tb_env = tb_env;
293     if (tb_env->tb_freq == 0 || 1) {
294         tb_env->tb_freq = freq;
295         /* Create new timer */
296         tb_env->decr_timer =
297             qemu_new_timer(vm_clock, &cpu_ppc_decr_cb, env);
298         /* There is a bug in Linux 2.4 kernels:
299          * if a decrementer exception is pending when it enables msr_ee,
300          * it's not ready to handle it...
301          */
302         _cpu_ppc_store_decr(env, 0xFFFFFFFF, 0xFFFFFFFF, 0);
303     }
304
305     return tb_env;
306 }
307
308 /* Specific helpers for POWER & PowerPC 601 RTC */
309 ppc_tb_t *cpu_ppc601_rtc_init (CPUState *env)
310 {
311     return cpu_ppc_tb_init(env, 7812500);
312 }
313
314 void cpu_ppc601_store_rtcu (CPUState *env, uint32_t value)
315 __attribute__ (( alias ("cpu_ppc_store_tbu") ));
316
317 uint32_t cpu_ppc601_load_rtcu (CPUState *env)
318 __attribute__ (( alias ("cpu_ppc_load_tbu") ));
319
320 void cpu_ppc601_store_rtcl (CPUState *env, uint32_t value)
321 {
322     cpu_ppc_store_tbl(env, value & 0x3FFFFF80);
323 }
324
325 uint32_t cpu_ppc601_load_rtcl (CPUState *env)
326 {
327     return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
328 }
329
330 /*****************************************************************************/
331 /* Embedded PowerPC timers */
332
333 /* PIT, FIT & WDT */
334 typedef struct ppcemb_timer_t ppcemb_timer_t;
335 struct ppcemb_timer_t {
336     uint64_t pit_reload;  /* PIT auto-reload value        */
337     uint64_t fit_next;    /* Tick for next FIT interrupt  */
338     struct QEMUTimer *fit_timer;
339     uint64_t wdt_next;    /* Tick for next WDT interrupt  */
340     struct QEMUTimer *wdt_timer;
341 };
342    
343 /* Fixed interval timer */
344 static void cpu_4xx_fit_cb (void *opaque)
345 {
346     CPUState *env;
347     ppc_tb_t *tb_env;
348     ppcemb_timer_t *ppcemb_timer;
349     uint64_t now, next;
350
351     env = opaque;
352     tb_env = env->tb_env;
353     ppcemb_timer = tb_env->opaque;
354     now = qemu_get_clock(vm_clock);
355     switch ((env->spr[SPR_40x_TCR] >> 24) & 0x3) {
356     case 0:
357         next = 1 << 9;
358         break;
359     case 1:
360         next = 1 << 13;
361         break;
362     case 2:
363         next = 1 << 17;
364         break;
365     case 3:
366         next = 1 << 21;
367         break;
368     default:
369         /* Cannot occur, but makes gcc happy */
370         return;
371     }
372     next = now + muldiv64(next, ticks_per_sec, tb_env->tb_freq);
373     if (next == now)
374         next++;
375     qemu_mod_timer(ppcemb_timer->fit_timer, next);
376     tb_env->decr_next = next;
377     env->spr[SPR_40x_TSR] |= 1 << 26;
378     if ((env->spr[SPR_40x_TCR] >> 23) & 0x1)
379         ppc_set_irq(env, PPC_INTERRUPT_FIT, 1);
380     if (loglevel) {
381         fprintf(logfile, "%s: ir %d TCR %08x TSR %08x\n", __func__,
382                 (env->spr[SPR_40x_TCR] >> 23) & 0x1,
383                 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
384     }
385 }
386
387 /* Programmable interval timer */
388 static void cpu_4xx_pit_cb (void *opaque)
389 {
390     CPUState *env;
391     ppc_tb_t *tb_env;
392     ppcemb_timer_t *ppcemb_timer;
393     uint64_t now, next;
394
395     env = opaque;
396     tb_env = env->tb_env;
397     ppcemb_timer = tb_env->opaque;
398     now = qemu_get_clock(vm_clock);
399     if ((env->spr[SPR_40x_TCR] >> 22) & 0x1) {
400         /* Auto reload */
401         next = now + muldiv64(ppcemb_timer->pit_reload,
402                               ticks_per_sec, tb_env->tb_freq);
403         if (next == now)
404             next++;
405         qemu_mod_timer(tb_env->decr_timer, next);
406         tb_env->decr_next = next;
407     }
408     env->spr[SPR_40x_TSR] |= 1 << 27;
409     if ((env->spr[SPR_40x_TCR] >> 26) & 0x1)
410         ppc_set_irq(env, PPC_INTERRUPT_PIT, 1);
411     if (loglevel) {
412         fprintf(logfile, "%s: ar %d ir %d TCR %08x TSR %08x %08lx\n", __func__,
413                 (env->spr[SPR_40x_TCR] >> 22) & 0x1,
414                 (env->spr[SPR_40x_TCR] >> 26) & 0x1,
415                 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR],
416                 ppcemb_timer->pit_reload);
417     }
418 }
419
420 /* Watchdog timer */
421 static void cpu_4xx_wdt_cb (void *opaque)
422 {
423     CPUState *env;
424     ppc_tb_t *tb_env;
425     ppcemb_timer_t *ppcemb_timer;
426     uint64_t now, next;
427
428     env = opaque;
429     tb_env = env->tb_env;
430     ppcemb_timer = tb_env->opaque;
431     now = qemu_get_clock(vm_clock);
432     switch ((env->spr[SPR_40x_TCR] >> 30) & 0x3) {
433     case 0:
434         next = 1 << 17;
435         break;
436     case 1:
437         next = 1 << 21;
438         break;
439     case 2:
440         next = 1 << 25;
441         break;
442     case 3:
443         next = 1 << 29;
444         break;
445     default:
446         /* Cannot occur, but makes gcc happy */
447         return;
448     }
449     next = now + muldiv64(next, ticks_per_sec, tb_env->tb_freq);
450     if (next == now)
451         next++;
452     if (loglevel) {
453         fprintf(logfile, "%s: TCR %08x TSR %08x\n", __func__,
454                 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
455     }
456     switch ((env->spr[SPR_40x_TSR] >> 30) & 0x3) {
457     case 0x0:
458     case 0x1:
459         qemu_mod_timer(ppcemb_timer->wdt_timer, next);
460         ppcemb_timer->wdt_next = next;
461         env->spr[SPR_40x_TSR] |= 1 << 31;
462         break;
463     case 0x2:
464         qemu_mod_timer(ppcemb_timer->wdt_timer, next);
465         ppcemb_timer->wdt_next = next;
466         env->spr[SPR_40x_TSR] |= 1 << 30;
467         if ((env->spr[SPR_40x_TCR] >> 27) & 0x1)
468             ppc_set_irq(env, PPC_INTERRUPT_WDT, 1);
469         break;
470     case 0x3:
471         env->spr[SPR_40x_TSR] &= ~0x30000000;
472         env->spr[SPR_40x_TSR] |= env->spr[SPR_40x_TCR] & 0x30000000;
473         switch ((env->spr[SPR_40x_TCR] >> 28) & 0x3) {
474         case 0x0:
475             /* No reset */
476             break;
477         case 0x1: /* Core reset */
478         case 0x2: /* Chip reset */
479         case 0x3: /* System reset */
480             qemu_system_reset_request();
481             return;
482         }
483     }
484 }
485
486 void store_40x_pit (CPUState *env, target_ulong val)
487 {
488     ppc_tb_t *tb_env;
489     ppcemb_timer_t *ppcemb_timer;
490     uint64_t now, next;
491
492     tb_env = env->tb_env;
493     ppcemb_timer = tb_env->opaque;
494     if (loglevel)
495         fprintf(logfile, "%s %p %p\n", __func__, tb_env, ppcemb_timer);
496     ppcemb_timer->pit_reload = val;
497     if (val == 0) {
498         /* Stop PIT */
499         if (loglevel)
500             fprintf(logfile, "%s: stop PIT\n", __func__);
501         qemu_del_timer(tb_env->decr_timer);
502     } else {
503         if (loglevel)
504             fprintf(logfile, "%s: start PIT 0x%08x\n", __func__, val);
505         now = qemu_get_clock(vm_clock);
506         next = now + muldiv64(val, ticks_per_sec, tb_env->tb_freq);
507          if (next == now)
508             next++;
509         qemu_mod_timer(tb_env->decr_timer, next);
510         tb_env->decr_next = next;
511     }
512 }
513
514 target_ulong load_40x_pit (CPUState *env)
515 {
516     return cpu_ppc_load_decr(env);
517 }
518
519 void store_booke_tsr (CPUState *env, target_ulong val)
520 {
521     env->spr[SPR_40x_TSR] = val & 0xFC000000;
522 }
523
524 void store_booke_tcr (CPUState *env, target_ulong val)
525 {
526     /* We don't update timers now. Maybe we should... */
527     env->spr[SPR_40x_TCR] = val & 0xFF800000;
528 }
529
530 void ppc_emb_timers_init (CPUState *env)
531 {
532     ppc_tb_t *tb_env;
533     ppcemb_timer_t *ppcemb_timer;
534
535     tb_env = env->tb_env;
536     ppcemb_timer = qemu_mallocz(sizeof(ppcemb_timer_t));
537     tb_env->opaque = ppcemb_timer;
538     if (loglevel)
539         fprintf(logfile, "%s %p %p\n", __func__, tb_env, ppcemb_timer);
540     if (ppcemb_timer != NULL) {
541         /* We use decr timer for PIT */
542         tb_env->decr_timer = qemu_new_timer(vm_clock, &cpu_4xx_pit_cb, env);
543         ppcemb_timer->fit_timer =
544             qemu_new_timer(vm_clock, &cpu_4xx_fit_cb, env);
545         ppcemb_timer->wdt_timer =
546             qemu_new_timer(vm_clock, &cpu_4xx_wdt_cb, env);
547     }
548 }
549
550 /*****************************************************************************/
551 /* Embedded PowerPC Device Control Registers */
552 typedef struct ppc_dcrn_t ppc_dcrn_t;
553 struct ppc_dcrn_t {
554     dcr_read_cb dcr_read;
555     dcr_write_cb dcr_write;
556     void *opaque;
557 };
558
559 #define DCRN_NB 1024
560 struct ppc_dcr_t {
561     ppc_dcrn_t dcrn[DCRN_NB];
562     int (*read_error)(int dcrn);
563     int (*write_error)(int dcrn);
564 };
565
566 int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, target_ulong *valp)
567 {
568     ppc_dcrn_t *dcr;
569
570     if (dcrn < 0 || dcrn >= DCRN_NB)
571         goto error;
572     dcr = &dcr_env->dcrn[dcrn];
573     if (dcr->dcr_read == NULL)
574         goto error;
575     *valp = (*dcr->dcr_read)(dcr->opaque, dcrn);
576
577     return 0;
578
579  error:
580     if (dcr_env->read_error != NULL)
581         return (*dcr_env->read_error)(dcrn);
582
583     return -1;
584 }
585
586 int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, target_ulong val)
587 {
588     ppc_dcrn_t *dcr;
589
590     if (dcrn < 0 || dcrn >= DCRN_NB)
591         goto error;
592     dcr = &dcr_env->dcrn[dcrn];
593     if (dcr->dcr_write == NULL)
594         goto error;
595     (*dcr->dcr_write)(dcr->opaque, dcrn, val);
596
597     return 0;
598
599  error:
600     if (dcr_env->write_error != NULL)
601         return (*dcr_env->write_error)(dcrn);
602
603     return -1;
604 }
605
606 int ppc_dcr_register (CPUState *env, int dcrn, void *opaque,
607                       dcr_read_cb dcr_read, dcr_write_cb dcr_write)
608 {
609     ppc_dcr_t *dcr_env;
610     ppc_dcrn_t *dcr;
611
612     dcr_env = env->dcr_env;
613     if (dcr_env == NULL)
614         return -1;
615     if (dcrn < 0 || dcrn >= DCRN_NB)
616         return -1;
617     dcr = &dcr_env->dcrn[dcrn];
618     if (dcr->opaque != NULL ||
619         dcr->dcr_read != NULL ||
620         dcr->dcr_write != NULL)
621         return -1;
622     dcr->opaque = opaque;
623     dcr->dcr_read = dcr_read;
624     dcr->dcr_write = dcr_write;
625
626     return 0;
627 }
628
629 int ppc_dcr_init (CPUState *env, int (*read_error)(int dcrn),
630                   int (*write_error)(int dcrn))
631 {
632     ppc_dcr_t *dcr_env;
633
634     dcr_env = qemu_mallocz(sizeof(ppc_dcr_t));
635     if (dcr_env == NULL)
636         return -1;
637     dcr_env->read_error = read_error;
638     dcr_env->write_error = write_error;
639     env->dcr_env = dcr_env;
640
641     return 0;
642 }
643
644
645 #if 0
646 /*****************************************************************************/
647 /* Handle system reset (for now, just stop emulation) */
648 void cpu_ppc_reset (CPUState *env)
649 {
650     printf("Reset asked... Stop emulation\n");
651     abort();
652 }
653 #endif
654
655 /*****************************************************************************/
656 /* Debug port */
657 void PPC_debug_write (void *opaque, uint32_t addr, uint32_t val)
658 {
659     addr &= 0xF;
660     switch (addr) {
661     case 0:
662         printf("%c", val);
663         break;
664     case 1:
665         printf("\n");
666         fflush(stdout);
667         break;
668     case 2:
669         printf("Set loglevel to %04x\n", val);
670         cpu_set_log(val | 0x100);
671         break;
672     }
673 }
674
675 /*****************************************************************************/
676 /* NVRAM helpers */
677 void NVRAM_set_byte (m48t59_t *nvram, uint32_t addr, uint8_t value)
678 {
679     m48t59_write(nvram, addr, value);
680 }
681
682 uint8_t NVRAM_get_byte (m48t59_t *nvram, uint32_t addr)
683 {
684     return m48t59_read(nvram, addr);
685 }
686
687 void NVRAM_set_word (m48t59_t *nvram, uint32_t addr, uint16_t value)
688 {
689     m48t59_write(nvram, addr, value >> 8);
690     m48t59_write(nvram, addr + 1, value & 0xFF);
691 }
692
693 uint16_t NVRAM_get_word (m48t59_t *nvram, uint32_t addr)
694 {
695     uint16_t tmp;
696
697     tmp = m48t59_read(nvram, addr) << 8;
698     tmp |= m48t59_read(nvram, addr + 1);
699     return tmp;
700 }
701
702 void NVRAM_set_lword (m48t59_t *nvram, uint32_t addr, uint32_t value)
703 {
704     m48t59_write(nvram, addr, value >> 24);
705     m48t59_write(nvram, addr + 1, (value >> 16) & 0xFF);
706     m48t59_write(nvram, addr + 2, (value >> 8) & 0xFF);
707     m48t59_write(nvram, addr + 3, value & 0xFF);
708 }
709
710 uint32_t NVRAM_get_lword (m48t59_t *nvram, uint32_t addr)
711 {
712     uint32_t tmp;
713
714     tmp = m48t59_read(nvram, addr) << 24;
715     tmp |= m48t59_read(nvram, addr + 1) << 16;
716     tmp |= m48t59_read(nvram, addr + 2) << 8;
717     tmp |= m48t59_read(nvram, addr + 3);
718
719     return tmp;
720 }
721
722 void NVRAM_set_string (m48t59_t *nvram, uint32_t addr,
723                        const unsigned char *str, uint32_t max)
724 {
725     int i;
726
727     for (i = 0; i < max && str[i] != '\0'; i++) {
728         m48t59_write(nvram, addr + i, str[i]);
729     }
730     m48t59_write(nvram, addr + max - 1, '\0');
731 }
732
733 int NVRAM_get_string (m48t59_t *nvram, uint8_t *dst, uint16_t addr, int max)
734 {
735     int i;
736
737     memset(dst, 0, max);
738     for (i = 0; i < max; i++) {
739         dst[i] = NVRAM_get_byte(nvram, addr + i);
740         if (dst[i] == '\0')
741             break;
742     }
743
744     return i;
745 }
746
747 static uint16_t NVRAM_crc_update (uint16_t prev, uint16_t value)
748 {
749     uint16_t tmp;
750     uint16_t pd, pd1, pd2;
751
752     tmp = prev >> 8;
753     pd = prev ^ value;
754     pd1 = pd & 0x000F;
755     pd2 = ((pd >> 4) & 0x000F) ^ pd1;
756     tmp ^= (pd1 << 3) | (pd1 << 8);
757     tmp ^= pd2 | (pd2 << 7) | (pd2 << 12);
758
759     return tmp;
760 }
761
762 uint16_t NVRAM_compute_crc (m48t59_t *nvram, uint32_t start, uint32_t count)
763 {
764     uint32_t i;
765     uint16_t crc = 0xFFFF;
766     int odd;
767
768     odd = count & 1;
769     count &= ~1;
770     for (i = 0; i != count; i++) {
771         crc = NVRAM_crc_update(crc, NVRAM_get_word(nvram, start + i));
772     }
773     if (odd) {
774         crc = NVRAM_crc_update(crc, NVRAM_get_byte(nvram, start + i) << 8);
775     }
776
777     return crc;
778 }
779
780 #define CMDLINE_ADDR 0x017ff000
781
782 int PPC_NVRAM_set_params (m48t59_t *nvram, uint16_t NVRAM_size,
783                           const unsigned char *arch,
784                           uint32_t RAM_size, int boot_device,
785                           uint32_t kernel_image, uint32_t kernel_size,
786                           const char *cmdline,
787                           uint32_t initrd_image, uint32_t initrd_size,
788                           uint32_t NVRAM_image,
789                           int width, int height, int depth)
790 {
791     uint16_t crc;
792
793     /* Set parameters for Open Hack'Ware BIOS */
794     NVRAM_set_string(nvram, 0x00, "QEMU_BIOS", 16);
795     NVRAM_set_lword(nvram,  0x10, 0x00000002); /* structure v2 */
796     NVRAM_set_word(nvram,   0x14, NVRAM_size);
797     NVRAM_set_string(nvram, 0x20, arch, 16);
798     NVRAM_set_lword(nvram,  0x30, RAM_size);
799     NVRAM_set_byte(nvram,   0x34, boot_device);
800     NVRAM_set_lword(nvram,  0x38, kernel_image);
801     NVRAM_set_lword(nvram,  0x3C, kernel_size);
802     if (cmdline) {
803         /* XXX: put the cmdline in NVRAM too ? */
804         strcpy(phys_ram_base + CMDLINE_ADDR, cmdline);
805         NVRAM_set_lword(nvram,  0x40, CMDLINE_ADDR);
806         NVRAM_set_lword(nvram,  0x44, strlen(cmdline));
807     } else {
808         NVRAM_set_lword(nvram,  0x40, 0);
809         NVRAM_set_lword(nvram,  0x44, 0);
810     }
811     NVRAM_set_lword(nvram,  0x48, initrd_image);
812     NVRAM_set_lword(nvram,  0x4C, initrd_size);
813     NVRAM_set_lword(nvram,  0x50, NVRAM_image);
814
815     NVRAM_set_word(nvram,   0x54, width);
816     NVRAM_set_word(nvram,   0x56, height);
817     NVRAM_set_word(nvram,   0x58, depth);
818     crc = NVRAM_compute_crc(nvram, 0x00, 0xF8);
819     NVRAM_set_word(nvram,  0xFC, crc);
820
821     return 0;
822 }