]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc.c
Fix PowerPC 405 BIOS instanciation: is a 32 bits only target.
[mirror_qemu.git] / hw / ppc.c
CommitLineData
a541f297 1/*
e9df014c 2 * QEMU generic PowerPC hardware System Emulator
5fafdf24 3 *
76a66253 4 * Copyright (c) 2003-2007 Jocelyn Mayer
5fafdf24 5 *
a541f297
FB
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 */
a541f297 24#include "vl.h"
fd0bbb12 25#include "m48t59.h"
a541f297 26
e9df014c 27//#define PPC_DEBUG_IRQ
4b6d0a4c 28//#define PPC_DEBUG_TB
e9df014c 29
47103572
JM
30extern FILE *logfile;
31extern int loglevel;
32
e9df014c 33void ppc_set_irq (CPUState *env, int n_IRQ, int level)
47103572 34{
47103572
JM
35 if (level) {
36 env->pending_interrupts |= 1 << n_IRQ;
37 cpu_interrupt(env, CPU_INTERRUPT_HARD);
38 } else {
39 env->pending_interrupts &= ~(1 << n_IRQ);
40 if (env->pending_interrupts == 0)
41 cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
42 }
e9df014c 43#if defined(PPC_DEBUG_IRQ)
a496775f
JM
44 if (loglevel & CPU_LOG_INT) {
45 fprintf(logfile, "%s: %p n_IRQ %d level %d => pending %08x req %08x\n",
46 __func__, env, n_IRQ, level,
47 env->pending_interrupts, env->interrupt_request);
48 }
47103572
JM
49#endif
50}
51
e9df014c
JM
52/* PowerPC 6xx / 7xx internal IRQ controller */
53static void ppc6xx_set_irq (void *opaque, int pin, int level)
d537cf6c 54{
e9df014c
JM
55 CPUState *env = opaque;
56 int cur_level;
d537cf6c 57
e9df014c 58#if defined(PPC_DEBUG_IRQ)
a496775f
JM
59 if (loglevel & CPU_LOG_INT) {
60 fprintf(logfile, "%s: env %p pin %d level %d\n", __func__,
61 env, pin, level);
62 }
e9df014c
JM
63#endif
64 cur_level = (env->irq_input_state >> pin) & 1;
65 /* Don't generate spurious events */
24be5ae3 66 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
e9df014c 67 switch (pin) {
24be5ae3
JM
68 case PPC6xx_INPUT_INT:
69 /* Level sensitive - active high */
e9df014c 70#if defined(PPC_DEBUG_IRQ)
a496775f
JM
71 if (loglevel & CPU_LOG_INT) {
72 fprintf(logfile, "%s: set the external IRQ state to %d\n",
73 __func__, level);
74 }
e9df014c
JM
75#endif
76 ppc_set_irq(env, PPC_INTERRUPT_EXT, level);
77 break;
24be5ae3 78 case PPC6xx_INPUT_SMI:
e9df014c
JM
79 /* Level sensitive - active high */
80#if defined(PPC_DEBUG_IRQ)
a496775f
JM
81 if (loglevel & CPU_LOG_INT) {
82 fprintf(logfile, "%s: set the SMI IRQ state to %d\n",
83 __func__, level);
84 }
e9df014c
JM
85#endif
86 ppc_set_irq(env, PPC_INTERRUPT_SMI, level);
87 break;
24be5ae3 88 case PPC6xx_INPUT_MCP:
e9df014c
JM
89 /* Negative edge sensitive */
90 /* XXX: TODO: actual reaction may depends on HID0 status
91 * 603/604/740/750: check HID0[EMCP]
92 */
93 if (cur_level == 1 && level == 0) {
94#if defined(PPC_DEBUG_IRQ)
a496775f
JM
95 if (loglevel & CPU_LOG_INT) {
96 fprintf(logfile, "%s: raise machine check state\n",
97 __func__);
98 }
e9df014c
JM
99#endif
100 ppc_set_irq(env, PPC_INTERRUPT_MCK, 1);
101 }
102 break;
24be5ae3 103 case PPC6xx_INPUT_CKSTP_IN:
e9df014c
JM
104 /* Level sensitive - active low */
105 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
106 if (level) {
107#if defined(PPC_DEBUG_IRQ)
a496775f
JM
108 if (loglevel & CPU_LOG_INT) {
109 fprintf(logfile, "%s: stop the CPU\n", __func__);
110 }
e9df014c
JM
111#endif
112 env->halted = 1;
113 } else {
114#if defined(PPC_DEBUG_IRQ)
a496775f
JM
115 if (loglevel & CPU_LOG_INT) {
116 fprintf(logfile, "%s: restart the CPU\n", __func__);
117 }
e9df014c
JM
118#endif
119 env->halted = 0;
120 }
121 break;
24be5ae3 122 case PPC6xx_INPUT_HRESET:
e9df014c
JM
123 /* Level sensitive - active low */
124 if (level) {
125#if 0 // XXX: TOFIX
126#if defined(PPC_DEBUG_IRQ)
a496775f
JM
127 if (loglevel & CPU_LOG_INT) {
128 fprintf(logfile, "%s: reset the CPU\n", __func__);
129 }
e9df014c
JM
130#endif
131 cpu_reset(env);
132#endif
133 }
134 break;
24be5ae3 135 case PPC6xx_INPUT_SRESET:
e9df014c 136#if defined(PPC_DEBUG_IRQ)
a496775f
JM
137 if (loglevel & CPU_LOG_INT) {
138 fprintf(logfile, "%s: set the RESET IRQ state to %d\n",
139 __func__, level);
140 }
e9df014c
JM
141#endif
142 ppc_set_irq(env, PPC_INTERRUPT_RESET, level);
143 break;
144 default:
145 /* Unknown pin - do nothing */
146#if defined(PPC_DEBUG_IRQ)
a496775f
JM
147 if (loglevel & CPU_LOG_INT) {
148 fprintf(logfile, "%s: unknown IRQ pin %d\n", __func__, pin);
149 }
e9df014c
JM
150#endif
151 return;
152 }
153 if (level)
154 env->irq_input_state |= 1 << pin;
155 else
156 env->irq_input_state &= ~(1 << pin);
d537cf6c
PB
157 }
158}
159
e9df014c 160void ppc6xx_irq_init (CPUState *env)
47103572 161{
e9df014c 162 env->irq_inputs = (void **)qemu_allocate_irqs(&ppc6xx_set_irq, env, 6);
47103572
JM
163}
164
d0dfae6e
JM
165/* PowerPC 970 internal IRQ controller */
166static void ppc970_set_irq (void *opaque, int pin, int level)
167{
168 CPUState *env = opaque;
169 int cur_level;
170
171#if defined(PPC_DEBUG_IRQ)
172 if (loglevel & CPU_LOG_INT) {
173 fprintf(logfile, "%s: env %p pin %d level %d\n", __func__,
174 env, pin, level);
175 }
176#endif
177 cur_level = (env->irq_input_state >> pin) & 1;
178 /* Don't generate spurious events */
179 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
180 switch (pin) {
181 case PPC970_INPUT_INT:
182 /* Level sensitive - active high */
183#if defined(PPC_DEBUG_IRQ)
184 if (loglevel & CPU_LOG_INT) {
185 fprintf(logfile, "%s: set the external IRQ state to %d\n",
186 __func__, level);
187 }
188#endif
189 ppc_set_irq(env, PPC_INTERRUPT_EXT, level);
190 break;
191 case PPC970_INPUT_THINT:
192 /* Level sensitive - active high */
193#if defined(PPC_DEBUG_IRQ)
194 if (loglevel & CPU_LOG_INT) {
195 fprintf(logfile, "%s: set the SMI IRQ state to %d\n", __func__,
196 level);
197 }
198#endif
199 ppc_set_irq(env, PPC_INTERRUPT_THERM, level);
200 break;
201 case PPC970_INPUT_MCP:
202 /* Negative edge sensitive */
203 /* XXX: TODO: actual reaction may depends on HID0 status
204 * 603/604/740/750: check HID0[EMCP]
205 */
206 if (cur_level == 1 && level == 0) {
207#if defined(PPC_DEBUG_IRQ)
208 if (loglevel & CPU_LOG_INT) {
209 fprintf(logfile, "%s: raise machine check state\n",
210 __func__);
211 }
212#endif
213 ppc_set_irq(env, PPC_INTERRUPT_MCK, 1);
214 }
215 break;
216 case PPC970_INPUT_CKSTP:
217 /* Level sensitive - active low */
218 /* XXX: TODO: relay the signal to CKSTP_OUT pin */
219 if (level) {
220#if defined(PPC_DEBUG_IRQ)
221 if (loglevel & CPU_LOG_INT) {
222 fprintf(logfile, "%s: stop the CPU\n", __func__);
223 }
224#endif
225 env->halted = 1;
226 } else {
227#if defined(PPC_DEBUG_IRQ)
228 if (loglevel & CPU_LOG_INT) {
229 fprintf(logfile, "%s: restart the CPU\n", __func__);
230 }
231#endif
232 env->halted = 0;
233 }
234 break;
235 case PPC970_INPUT_HRESET:
236 /* Level sensitive - active low */
237 if (level) {
238#if 0 // XXX: TOFIX
239#if defined(PPC_DEBUG_IRQ)
240 if (loglevel & CPU_LOG_INT) {
241 fprintf(logfile, "%s: reset the CPU\n", __func__);
242 }
243#endif
244 cpu_reset(env);
245#endif
246 }
247 break;
248 case PPC970_INPUT_SRESET:
249#if defined(PPC_DEBUG_IRQ)
250 if (loglevel & CPU_LOG_INT) {
251 fprintf(logfile, "%s: set the RESET IRQ state to %d\n",
252 __func__, level);
253 }
254#endif
255 ppc_set_irq(env, PPC_INTERRUPT_RESET, level);
256 break;
257 case PPC970_INPUT_TBEN:
258#if defined(PPC_DEBUG_IRQ)
259 if (loglevel & CPU_LOG_INT) {
260 fprintf(logfile, "%s: set the TBEN state to %d\n", __func__,
261 level);
262 }
263#endif
264 /* XXX: TODO */
265 break;
266 default:
267 /* Unknown pin - do nothing */
268#if defined(PPC_DEBUG_IRQ)
269 if (loglevel & CPU_LOG_INT) {
270 fprintf(logfile, "%s: unknown IRQ pin %d\n", __func__, pin);
271 }
272#endif
273 return;
274 }
275 if (level)
276 env->irq_input_state |= 1 << pin;
277 else
278 env->irq_input_state &= ~(1 << pin);
279 }
280}
281
282void ppc970_irq_init (CPUState *env)
283{
284 env->irq_inputs = (void **)qemu_allocate_irqs(&ppc970_set_irq, env, 7);
285}
286
4e290a0b
JM
287/* PowerPC 40x internal IRQ controller */
288static void ppc40x_set_irq (void *opaque, int pin, int level)
24be5ae3
JM
289{
290 CPUState *env = opaque;
291 int cur_level;
292
293#if defined(PPC_DEBUG_IRQ)
8ecc7913
JM
294 if (loglevel & CPU_LOG_INT) {
295 fprintf(logfile, "%s: env %p pin %d level %d\n", __func__,
296 env, pin, level);
297 }
24be5ae3
JM
298#endif
299 cur_level = (env->irq_input_state >> pin) & 1;
300 /* Don't generate spurious events */
301 if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) {
302 switch (pin) {
4e290a0b 303 case PPC40x_INPUT_RESET_SYS:
8ecc7913
JM
304 if (level) {
305#if defined(PPC_DEBUG_IRQ)
306 if (loglevel & CPU_LOG_INT) {
307 fprintf(logfile, "%s: reset the PowerPC system\n",
308 __func__);
309 }
310#endif
311 ppc40x_system_reset(env);
312 }
313 break;
4e290a0b 314 case PPC40x_INPUT_RESET_CHIP:
8ecc7913
JM
315 if (level) {
316#if defined(PPC_DEBUG_IRQ)
317 if (loglevel & CPU_LOG_INT) {
318 fprintf(logfile, "%s: reset the PowerPC chip\n", __func__);
319 }
320#endif
321 ppc40x_chip_reset(env);
322 }
323 break;
4e290a0b 324 case PPC40x_INPUT_RESET_CORE:
24be5ae3
JM
325 /* XXX: TODO: update DBSR[MRR] */
326 if (level) {
24be5ae3 327#if defined(PPC_DEBUG_IRQ)
8ecc7913
JM
328 if (loglevel & CPU_LOG_INT) {
329 fprintf(logfile, "%s: reset the PowerPC core\n", __func__);
330 }
24be5ae3 331#endif
8ecc7913 332 ppc40x_core_reset(env);
24be5ae3
JM
333 }
334 break;
4e290a0b 335 case PPC40x_INPUT_CINT:
24be5ae3
JM
336 /* Level sensitive - active high */
337#if defined(PPC_DEBUG_IRQ)
8ecc7913
JM
338 if (loglevel & CPU_LOG_INT) {
339 fprintf(logfile, "%s: set the critical IRQ state to %d\n",
340 __func__, level);
341 }
24be5ae3 342#endif
4e290a0b 343 ppc_set_irq(env, PPC_INTERRUPT_CEXT, level);
24be5ae3 344 break;
4e290a0b 345 case PPC40x_INPUT_INT:
24be5ae3
JM
346 /* Level sensitive - active high */
347#if defined(PPC_DEBUG_IRQ)
a496775f
JM
348 if (loglevel & CPU_LOG_INT) {
349 fprintf(logfile, "%s: set the external IRQ state to %d\n",
350 __func__, level);
351 }
24be5ae3
JM
352#endif
353 ppc_set_irq(env, PPC_INTERRUPT_EXT, level);
354 break;
4e290a0b 355 case PPC40x_INPUT_HALT:
24be5ae3
JM
356 /* Level sensitive - active low */
357 if (level) {
358#if defined(PPC_DEBUG_IRQ)
a496775f
JM
359 if (loglevel & CPU_LOG_INT) {
360 fprintf(logfile, "%s: stop the CPU\n", __func__);
361 }
24be5ae3
JM
362#endif
363 env->halted = 1;
364 } else {
365#if defined(PPC_DEBUG_IRQ)
a496775f
JM
366 if (loglevel & CPU_LOG_INT) {
367 fprintf(logfile, "%s: restart the CPU\n", __func__);
368 }
24be5ae3
JM
369#endif
370 env->halted = 0;
371 }
372 break;
4e290a0b 373 case PPC40x_INPUT_DEBUG:
24be5ae3
JM
374 /* Level sensitive - active high */
375#if defined(PPC_DEBUG_IRQ)
a496775f 376 if (loglevel & CPU_LOG_INT) {
a750fc0b 377 fprintf(logfile, "%s: set the debug pin state to %d\n",
a496775f
JM
378 __func__, level);
379 }
24be5ae3 380#endif
a750fc0b 381 ppc_set_irq(env, PPC_INTERRUPT_DEBUG, level);
24be5ae3
JM
382 break;
383 default:
384 /* Unknown pin - do nothing */
385#if defined(PPC_DEBUG_IRQ)
a496775f
JM
386 if (loglevel & CPU_LOG_INT) {
387 fprintf(logfile, "%s: unknown IRQ pin %d\n", __func__, pin);
388 }
24be5ae3
JM
389#endif
390 return;
391 }
392 if (level)
393 env->irq_input_state |= 1 << pin;
394 else
395 env->irq_input_state &= ~(1 << pin);
396 }
397}
398
4e290a0b 399void ppc40x_irq_init (CPUState *env)
24be5ae3 400{
4e290a0b
JM
401 env->irq_inputs = (void **)qemu_allocate_irqs(&ppc40x_set_irq,
402 env, PPC40x_INPUT_NB);
24be5ae3
JM
403}
404
9fddaa0c 405/*****************************************************************************/
e9df014c 406/* PowerPC time base and decrementer emulation */
9fddaa0c
FB
407struct ppc_tb_t {
408 /* Time base management */
409 int64_t tb_offset; /* Compensation */
a062e36c 410 int64_t atb_offset; /* Compensation */
9fddaa0c
FB
411 uint32_t tb_freq; /* TB frequency */
412 /* Decrementer management */
413 uint64_t decr_next; /* Tick for next decr interrupt */
414 struct QEMUTimer *decr_timer;
58a7d328
JM
415#if defined(TARGET_PPC64H)
416 /* Hypervisor decrementer management */
417 uint64_t hdecr_next; /* Tick for next hdecr interrupt */
418 struct QEMUTimer *hdecr_timer;
419 uint64_t purr_load;
420 uint64_t purr_start;
421#endif
47103572 422 void *opaque;
9fddaa0c
FB
423};
424
a062e36c 425static inline uint64_t cpu_ppc_get_tb (ppc_tb_t *tb_env, int64_t tb_offset)
9fddaa0c
FB
426{
427 /* TB time in tb periods */
428 return muldiv64(qemu_get_clock(vm_clock) + tb_env->tb_offset,
76a66253 429 tb_env->tb_freq, ticks_per_sec);
9fddaa0c
FB
430}
431
432uint32_t cpu_ppc_load_tbl (CPUState *env)
433{
434 ppc_tb_t *tb_env = env->tb_env;
435 uint64_t tb;
436
a062e36c
JM
437 tb = cpu_ppc_get_tb(tb_env, tb_env->tb_offset);
438#if defined(PPC_DEBUG_TB)
439 if (loglevel != 0) {
440 fprintf(logfile, "%s: tb=0x%016lx\n", __func__, tb);
9fddaa0c
FB
441 }
442#endif
443
444 return tb & 0xFFFFFFFF;
445}
446
8a84de23 447static inline uint32_t _cpu_ppc_load_tbu (CPUState *env)
9fddaa0c
FB
448{
449 ppc_tb_t *tb_env = env->tb_env;
450 uint64_t tb;
451
a062e36c 452 tb = cpu_ppc_get_tb(tb_env, tb_env->tb_offset);
4b6d0a4c
JM
453#if defined(PPC_DEBUG_TB)
454 if (loglevel != 0) {
a496775f
JM
455 fprintf(logfile, "%s: tb=0x%016lx\n", __func__, tb);
456 }
9fddaa0c 457#endif
76a66253 458
9fddaa0c
FB
459 return tb >> 32;
460}
461
8a84de23
JM
462uint32_t cpu_ppc_load_tbu (CPUState *env)
463{
464 return _cpu_ppc_load_tbu(env);
465}
466
a062e36c
JM
467static inline void cpu_ppc_store_tb (ppc_tb_t *tb_env, int64_t *tb_offsetp,
468 uint64_t value)
9fddaa0c 469{
a062e36c 470 *tb_offsetp = muldiv64(value, ticks_per_sec, tb_env->tb_freq)
9fddaa0c 471 - qemu_get_clock(vm_clock);
4b6d0a4c
JM
472#ifdef PPC_DEBUG_TB
473 if (loglevel != 0) {
474 fprintf(logfile, "%s: tb=0x%016lx offset=%08lx\n", __func__, value,
a062e36c 475 *tb_offsetp);
a496775f 476 }
9fddaa0c
FB
477#endif
478}
479
a062e36c
JM
480void cpu_ppc_store_tbl (CPUState *env, uint32_t value)
481{
482 ppc_tb_t *tb_env = env->tb_env;
483 uint64_t tb;
484
485 tb = cpu_ppc_get_tb(tb_env, tb_env->tb_offset);
486 tb &= 0xFFFFFFFF00000000ULL;
487 cpu_ppc_store_tb(tb_env, &tb_env->tb_offset, tb | (uint64_t)value);
488}
489
8a84de23 490static inline void _cpu_ppc_store_tbu (CPUState *env, uint32_t value)
9fddaa0c
FB
491{
492 ppc_tb_t *tb_env = env->tb_env;
a062e36c 493 uint64_t tb;
9fddaa0c 494
a062e36c
JM
495 tb = cpu_ppc_get_tb(tb_env, tb_env->tb_offset);
496 tb &= 0x00000000FFFFFFFFULL;
497 cpu_ppc_store_tb(tb_env, &tb_env->tb_offset,
498 ((uint64_t)value << 32) | tb);
9fddaa0c
FB
499}
500
8a84de23
JM
501void cpu_ppc_store_tbu (CPUState *env, uint32_t value)
502{
503 _cpu_ppc_store_tbu(env, value);
504}
505
a062e36c
JM
506uint32_t cpu_ppc_load_atbl (CPUState *env)
507{
508 ppc_tb_t *tb_env = env->tb_env;
509 uint64_t tb;
510
511 tb = cpu_ppc_get_tb(tb_env, tb_env->atb_offset);
512#if defined(PPC_DEBUG_TB)
513 if (loglevel != 0) {
514 fprintf(logfile, "%s: tb=0x%016lx\n", __func__, tb);
515 }
516#endif
517
518 return tb & 0xFFFFFFFF;
519}
520
521uint32_t cpu_ppc_load_atbu (CPUState *env)
522{
523 ppc_tb_t *tb_env = env->tb_env;
524 uint64_t tb;
525
526 tb = cpu_ppc_get_tb(tb_env, tb_env->atb_offset);
527#if defined(PPC_DEBUG_TB)
528 if (loglevel != 0) {
529 fprintf(logfile, "%s: tb=0x%016lx\n", __func__, tb);
530 }
531#endif
532
533 return tb >> 32;
534}
535
536void cpu_ppc_store_atbl (CPUState *env, uint32_t value)
537{
538 ppc_tb_t *tb_env = env->tb_env;
539 uint64_t tb;
540
541 tb = cpu_ppc_get_tb(tb_env, tb_env->atb_offset);
542 tb &= 0xFFFFFFFF00000000ULL;
543 cpu_ppc_store_tb(tb_env, &tb_env->atb_offset, tb | (uint64_t)value);
544}
545
546void cpu_ppc_store_atbu (CPUState *env, uint32_t value)
9fddaa0c
FB
547{
548 ppc_tb_t *tb_env = env->tb_env;
a062e36c 549 uint64_t tb;
9fddaa0c 550
a062e36c
JM
551 tb = cpu_ppc_get_tb(tb_env, tb_env->atb_offset);
552 tb &= 0x00000000FFFFFFFFULL;
553 cpu_ppc_store_tb(tb_env, &tb_env->atb_offset,
554 ((uint64_t)value << 32) | tb);
9fddaa0c
FB
555}
556
58a7d328 557static inline uint32_t _cpu_ppc_load_decr (CPUState *env, uint64_t *next)
9fddaa0c
FB
558{
559 ppc_tb_t *tb_env = env->tb_env;
560 uint32_t decr;
4e588a4d 561 int64_t diff;
9fddaa0c 562
4e588a4d
FB
563 diff = tb_env->decr_next - qemu_get_clock(vm_clock);
564 if (diff >= 0)
565 decr = muldiv64(diff, tb_env->tb_freq, ticks_per_sec);
566 else
567 decr = -muldiv64(-diff, tb_env->tb_freq, ticks_per_sec);
4b6d0a4c
JM
568#if defined(PPC_DEBUG_TB)
569 if (loglevel != 0) {
a496775f
JM
570 fprintf(logfile, "%s: 0x%08x\n", __func__, decr);
571 }
9fddaa0c 572#endif
76a66253 573
9fddaa0c
FB
574 return decr;
575}
576
58a7d328
JM
577uint32_t cpu_ppc_load_decr (CPUState *env)
578{
579 ppc_tb_t *tb_env = env->tb_env;
580
581 return _cpu_ppc_load_decr(env, &tb_env->decr_next);
582}
583
584#if defined(TARGET_PPC64H)
585uint32_t cpu_ppc_load_hdecr (CPUState *env)
586{
587 ppc_tb_t *tb_env = env->tb_env;
588
589 return _cpu_ppc_load_decr(env, &tb_env->hdecr_next);
590}
591
592uint64_t cpu_ppc_load_purr (CPUState *env)
593{
594 ppc_tb_t *tb_env = env->tb_env;
595 uint64_t diff;
596
597 diff = qemu_get_clock(vm_clock) - tb_env->purr_start;
598
599 return tb_env->purr_load + muldiv64(diff, tb_env->tb_freq, ticks_per_sec);
600}
601#endif /* defined(TARGET_PPC64H) */
602
9fddaa0c
FB
603/* When decrementer expires,
604 * all we need to do is generate or queue a CPU exception
605 */
606static inline void cpu_ppc_decr_excp (CPUState *env)
607{
608 /* Raise it */
4b6d0a4c
JM
609#ifdef PPC_DEBUG_TB
610 if (loglevel != 0) {
a496775f
JM
611 fprintf(logfile, "raise decrementer exception\n");
612 }
9fddaa0c 613#endif
47103572 614 ppc_set_irq(env, PPC_INTERRUPT_DECR, 1);
9fddaa0c
FB
615}
616
58a7d328
JM
617static inline void cpu_ppc_hdecr_excp (CPUState *env)
618{
619 /* Raise it */
620#ifdef PPC_DEBUG_TB
621 if (loglevel != 0) {
622 fprintf(logfile, "raise decrementer exception\n");
623 }
624#endif
625 ppc_set_irq(env, PPC_INTERRUPT_HDECR, 1);
626}
627
628static void __cpu_ppc_store_decr (CPUState *env, uint64_t *nextp,
629 struct QEMUTimer *timer,
630 void (*raise_excp)(CPUState *),
631 uint32_t decr, uint32_t value,
632 int is_excp)
9fddaa0c
FB
633{
634 ppc_tb_t *tb_env = env->tb_env;
635 uint64_t now, next;
636
4b6d0a4c
JM
637#ifdef PPC_DEBUG_TB
638 if (loglevel != 0) {
a496775f
JM
639 fprintf(logfile, "%s: 0x%08x => 0x%08x\n", __func__, decr, value);
640 }
9fddaa0c
FB
641#endif
642 now = qemu_get_clock(vm_clock);
643 next = now + muldiv64(value, ticks_per_sec, tb_env->tb_freq);
644 if (is_excp)
58a7d328 645 next += *nextp - now;
9fddaa0c 646 if (next == now)
76a66253 647 next++;
58a7d328 648 *nextp = next;
9fddaa0c 649 /* Adjust timer */
58a7d328 650 qemu_mod_timer(timer, next);
9fddaa0c
FB
651 /* If we set a negative value and the decrementer was positive,
652 * raise an exception.
653 */
654 if ((value & 0x80000000) && !(decr & 0x80000000))
58a7d328
JM
655 (*raise_excp)(env);
656}
657
658
659static inline void _cpu_ppc_store_decr (CPUState *env, uint32_t decr,
660 uint32_t value, int is_excp)
661{
662 ppc_tb_t *tb_env = env->tb_env;
663
664 __cpu_ppc_store_decr(env, &tb_env->decr_next, tb_env->decr_timer,
665 &cpu_ppc_decr_excp, decr, value, is_excp);
9fddaa0c
FB
666}
667
668void cpu_ppc_store_decr (CPUState *env, uint32_t value)
669{
670 _cpu_ppc_store_decr(env, cpu_ppc_load_decr(env), value, 0);
671}
672
673static void cpu_ppc_decr_cb (void *opaque)
674{
675 _cpu_ppc_store_decr(opaque, 0x00000000, 0xFFFFFFFF, 1);
676}
677
58a7d328
JM
678#if defined(TARGET_PPC64H)
679static inline void _cpu_ppc_store_hdecr (CPUState *env, uint32_t hdecr,
680 uint32_t value, int is_excp)
681{
682 ppc_tb_t *tb_env = env->tb_env;
683
684 __cpu_ppc_store_decr(env, &tb_env->hdecr_next, tb_env->hdecr_timer,
685 &cpu_ppc_hdecr_excp, hdecr, value, is_excp);
686}
687
688void cpu_ppc_store_hdecr (CPUState *env, uint32_t value)
689{
690 _cpu_ppc_store_hdecr(env, cpu_ppc_load_hdecr(env), value, 0);
691}
692
693static void cpu_ppc_hdecr_cb (void *opaque)
694{
695 _cpu_ppc_store_hdecr(opaque, 0x00000000, 0xFFFFFFFF, 1);
696}
697
698void cpu_ppc_store_purr (CPUState *env, uint64_t value)
699{
700 ppc_tb_t *tb_env = env->tb_env;
701
702 tb_env->purr_load = value;
703 tb_env->purr_start = qemu_get_clock(vm_clock);
704}
705#endif /* defined(TARGET_PPC64H) */
706
8ecc7913
JM
707static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq)
708{
709 CPUState *env = opaque;
710 ppc_tb_t *tb_env = env->tb_env;
711
712 tb_env->tb_freq = freq;
713 /* There is a bug in Linux 2.4 kernels:
714 * if a decrementer exception is pending when it enables msr_ee at startup,
715 * it's not ready to handle it...
716 */
717 _cpu_ppc_store_decr(env, 0xFFFFFFFF, 0xFFFFFFFF, 0);
58a7d328
JM
718#if defined(TARGET_PPC64H)
719 _cpu_ppc_store_hdecr(env, 0xFFFFFFFF, 0xFFFFFFFF, 0);
720 cpu_ppc_store_purr(env, 0x0000000000000000ULL);
721#endif /* defined(TARGET_PPC64H) */
8ecc7913
JM
722}
723
9fddaa0c 724/* Set up (once) timebase frequency (in Hz) */
8ecc7913 725clk_setup_cb cpu_ppc_tb_init (CPUState *env, uint32_t freq)
9fddaa0c
FB
726{
727 ppc_tb_t *tb_env;
728
729 tb_env = qemu_mallocz(sizeof(ppc_tb_t));
730 if (tb_env == NULL)
731 return NULL;
732 env->tb_env = tb_env;
8ecc7913
JM
733 /* Create new timer */
734 tb_env->decr_timer = qemu_new_timer(vm_clock, &cpu_ppc_decr_cb, env);
58a7d328
JM
735#if defined(TARGET_PPC64H)
736 tb_env->hdecr_timer = qemu_new_timer(vm_clock, &cpu_ppc_hdecr_cb, env);
737#endif /* defined(TARGET_PPC64H) */
8ecc7913 738 cpu_ppc_set_tb_clk(env, freq);
9fddaa0c 739
8ecc7913 740 return &cpu_ppc_set_tb_clk;
9fddaa0c
FB
741}
742
76a66253 743/* Specific helpers for POWER & PowerPC 601 RTC */
8ecc7913 744clk_setup_cb cpu_ppc601_rtc_init (CPUState *env)
76a66253
JM
745{
746 return cpu_ppc_tb_init(env, 7812500);
747}
748
749void cpu_ppc601_store_rtcu (CPUState *env, uint32_t value)
8a84de23
JM
750{
751 _cpu_ppc_store_tbu(env, value);
752}
76a66253
JM
753
754uint32_t cpu_ppc601_load_rtcu (CPUState *env)
8a84de23
JM
755{
756 return _cpu_ppc_load_tbu(env);
757}
76a66253
JM
758
759void cpu_ppc601_store_rtcl (CPUState *env, uint32_t value)
760{
761 cpu_ppc_store_tbl(env, value & 0x3FFFFF80);
762}
763
764uint32_t cpu_ppc601_load_rtcl (CPUState *env)
765{
766 return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
767}
768
636aaad7 769/*****************************************************************************/
76a66253 770/* Embedded PowerPC timers */
636aaad7
JM
771
772/* PIT, FIT & WDT */
773typedef struct ppcemb_timer_t ppcemb_timer_t;
774struct ppcemb_timer_t {
775 uint64_t pit_reload; /* PIT auto-reload value */
776 uint64_t fit_next; /* Tick for next FIT interrupt */
777 struct QEMUTimer *fit_timer;
778 uint64_t wdt_next; /* Tick for next WDT interrupt */
779 struct QEMUTimer *wdt_timer;
780};
3b46e624 781
636aaad7
JM
782/* Fixed interval timer */
783static void cpu_4xx_fit_cb (void *opaque)
784{
785 CPUState *env;
786 ppc_tb_t *tb_env;
787 ppcemb_timer_t *ppcemb_timer;
788 uint64_t now, next;
789
790 env = opaque;
791 tb_env = env->tb_env;
792 ppcemb_timer = tb_env->opaque;
793 now = qemu_get_clock(vm_clock);
794 switch ((env->spr[SPR_40x_TCR] >> 24) & 0x3) {
795 case 0:
796 next = 1 << 9;
797 break;
798 case 1:
799 next = 1 << 13;
800 break;
801 case 2:
802 next = 1 << 17;
803 break;
804 case 3:
805 next = 1 << 21;
806 break;
807 default:
808 /* Cannot occur, but makes gcc happy */
809 return;
810 }
811 next = now + muldiv64(next, ticks_per_sec, tb_env->tb_freq);
812 if (next == now)
813 next++;
814 qemu_mod_timer(ppcemb_timer->fit_timer, next);
636aaad7
JM
815 env->spr[SPR_40x_TSR] |= 1 << 26;
816 if ((env->spr[SPR_40x_TCR] >> 23) & 0x1)
817 ppc_set_irq(env, PPC_INTERRUPT_FIT, 1);
4b6d0a4c
JM
818#ifdef PPC_DEBUG_TB
819 if (loglevel != 0) {
e96efcfc
JM
820 fprintf(logfile, "%s: ir %d TCR " ADDRX " TSR " ADDRX "\n", __func__,
821 (int)((env->spr[SPR_40x_TCR] >> 23) & 0x1),
636aaad7
JM
822 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
823 }
4b6d0a4c 824#endif
636aaad7
JM
825}
826
827/* Programmable interval timer */
4b6d0a4c 828static void start_stop_pit (CPUState *env, ppc_tb_t *tb_env, int is_excp)
76a66253 829{
636aaad7
JM
830 ppcemb_timer_t *ppcemb_timer;
831 uint64_t now, next;
832
636aaad7 833 ppcemb_timer = tb_env->opaque;
4b6d0a4c
JM
834 if (ppcemb_timer->pit_reload <= 1 ||
835 !((env->spr[SPR_40x_TCR] >> 26) & 0x1) ||
836 (is_excp && !((env->spr[SPR_40x_TCR] >> 22) & 0x1))) {
837 /* Stop PIT */
838#ifdef PPC_DEBUG_TB
839 if (loglevel != 0) {
840 fprintf(logfile, "%s: stop PIT\n", __func__);
841 }
842#endif
843 qemu_del_timer(tb_env->decr_timer);
844 } else {
845#ifdef PPC_DEBUG_TB
846 if (loglevel != 0) {
847 fprintf(logfile, "%s: start PIT 0x" REGX "\n",
848 __func__, ppcemb_timer->pit_reload);
849 }
850#endif
851 now = qemu_get_clock(vm_clock);
636aaad7
JM
852 next = now + muldiv64(ppcemb_timer->pit_reload,
853 ticks_per_sec, tb_env->tb_freq);
4b6d0a4c
JM
854 if (is_excp)
855 next += tb_env->decr_next - now;
636aaad7
JM
856 if (next == now)
857 next++;
858 qemu_mod_timer(tb_env->decr_timer, next);
859 tb_env->decr_next = next;
860 }
4b6d0a4c
JM
861}
862
863static void cpu_4xx_pit_cb (void *opaque)
864{
865 CPUState *env;
866 ppc_tb_t *tb_env;
867 ppcemb_timer_t *ppcemb_timer;
868
869 env = opaque;
870 tb_env = env->tb_env;
871 ppcemb_timer = tb_env->opaque;
636aaad7
JM
872 env->spr[SPR_40x_TSR] |= 1 << 27;
873 if ((env->spr[SPR_40x_TCR] >> 26) & 0x1)
874 ppc_set_irq(env, PPC_INTERRUPT_PIT, 1);
4b6d0a4c
JM
875 start_stop_pit(env, tb_env, 1);
876#ifdef PPC_DEBUG_TB
877 if (loglevel != 0) {
e96efcfc
JM
878 fprintf(logfile, "%s: ar %d ir %d TCR " ADDRX " TSR " ADDRX " "
879 "%016" PRIx64 "\n", __func__,
880 (int)((env->spr[SPR_40x_TCR] >> 22) & 0x1),
881 (int)((env->spr[SPR_40x_TCR] >> 26) & 0x1),
636aaad7
JM
882 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR],
883 ppcemb_timer->pit_reload);
884 }
4b6d0a4c 885#endif
636aaad7
JM
886}
887
888/* Watchdog timer */
889static void cpu_4xx_wdt_cb (void *opaque)
890{
891 CPUState *env;
892 ppc_tb_t *tb_env;
893 ppcemb_timer_t *ppcemb_timer;
894 uint64_t now, next;
895
896 env = opaque;
897 tb_env = env->tb_env;
898 ppcemb_timer = tb_env->opaque;
899 now = qemu_get_clock(vm_clock);
900 switch ((env->spr[SPR_40x_TCR] >> 30) & 0x3) {
901 case 0:
902 next = 1 << 17;
903 break;
904 case 1:
905 next = 1 << 21;
906 break;
907 case 2:
908 next = 1 << 25;
909 break;
910 case 3:
911 next = 1 << 29;
912 break;
913 default:
914 /* Cannot occur, but makes gcc happy */
915 return;
916 }
917 next = now + muldiv64(next, ticks_per_sec, tb_env->tb_freq);
918 if (next == now)
919 next++;
4b6d0a4c
JM
920#ifdef PPC_DEBUG_TB
921 if (loglevel != 0) {
e96efcfc 922 fprintf(logfile, "%s: TCR " ADDRX " TSR " ADDRX "\n", __func__,
636aaad7
JM
923 env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]);
924 }
4b6d0a4c 925#endif
636aaad7
JM
926 switch ((env->spr[SPR_40x_TSR] >> 30) & 0x3) {
927 case 0x0:
928 case 0x1:
929 qemu_mod_timer(ppcemb_timer->wdt_timer, next);
930 ppcemb_timer->wdt_next = next;
931 env->spr[SPR_40x_TSR] |= 1 << 31;
932 break;
933 case 0x2:
934 qemu_mod_timer(ppcemb_timer->wdt_timer, next);
935 ppcemb_timer->wdt_next = next;
936 env->spr[SPR_40x_TSR] |= 1 << 30;
937 if ((env->spr[SPR_40x_TCR] >> 27) & 0x1)
938 ppc_set_irq(env, PPC_INTERRUPT_WDT, 1);
939 break;
940 case 0x3:
941 env->spr[SPR_40x_TSR] &= ~0x30000000;
942 env->spr[SPR_40x_TSR] |= env->spr[SPR_40x_TCR] & 0x30000000;
943 switch ((env->spr[SPR_40x_TCR] >> 28) & 0x3) {
944 case 0x0:
945 /* No reset */
946 break;
947 case 0x1: /* Core reset */
8ecc7913
JM
948 ppc40x_core_reset(env);
949 break;
636aaad7 950 case 0x2: /* Chip reset */
8ecc7913
JM
951 ppc40x_chip_reset(env);
952 break;
636aaad7 953 case 0x3: /* System reset */
8ecc7913
JM
954 ppc40x_system_reset(env);
955 break;
636aaad7
JM
956 }
957 }
76a66253
JM
958}
959
960void store_40x_pit (CPUState *env, target_ulong val)
961{
636aaad7
JM
962 ppc_tb_t *tb_env;
963 ppcemb_timer_t *ppcemb_timer;
636aaad7
JM
964
965 tb_env = env->tb_env;
966 ppcemb_timer = tb_env->opaque;
4b6d0a4c
JM
967#ifdef PPC_DEBUG_TB
968 if (loglevel != 0) {
636aaad7 969 fprintf(logfile, "%s %p %p\n", __func__, tb_env, ppcemb_timer);
a496775f 970 }
4b6d0a4c 971#endif
636aaad7 972 ppcemb_timer->pit_reload = val;
4b6d0a4c 973 start_stop_pit(env, tb_env, 0);
76a66253
JM
974}
975
636aaad7 976target_ulong load_40x_pit (CPUState *env)
76a66253 977{
636aaad7 978 return cpu_ppc_load_decr(env);
76a66253
JM
979}
980
981void store_booke_tsr (CPUState *env, target_ulong val)
982{
4b6d0a4c
JM
983#ifdef PPC_DEBUG_TB
984 if (loglevel != 0) {
985 fprintf(logfile, "%s: val=" ADDRX "\n", __func__, val);
986 }
987#endif
988 env->spr[SPR_40x_TSR] &= ~(val & 0xFC000000);
989 if (val & 0x80000000)
990 ppc_set_irq(env, PPC_INTERRUPT_PIT, 0);
636aaad7
JM
991}
992
993void store_booke_tcr (CPUState *env, target_ulong val)
994{
4b6d0a4c
JM
995 ppc_tb_t *tb_env;
996
997 tb_env = env->tb_env;
998#ifdef PPC_DEBUG_TB
999 if (loglevel != 0) {
1000 fprintf(logfile, "%s: val=" ADDRX "\n", __func__, val);
1001 }
1002#endif
1003 env->spr[SPR_40x_TCR] = val & 0xFFC00000;
1004 start_stop_pit(env, tb_env, 1);
8ecc7913 1005 cpu_4xx_wdt_cb(env);
636aaad7
JM
1006}
1007
4b6d0a4c
JM
1008static void ppc_emb_set_tb_clk (void *opaque, uint32_t freq)
1009{
1010 CPUState *env = opaque;
1011 ppc_tb_t *tb_env = env->tb_env;
1012
1013#ifdef PPC_DEBUG_TB
1014 if (loglevel != 0) {
1015 fprintf(logfile, "%s set new frequency to %u\n", __func__, freq);
1016 }
1017#endif
1018 tb_env->tb_freq = freq;
1019 /* XXX: we should also update all timers */
1020}
1021
8ecc7913 1022clk_setup_cb ppc_emb_timers_init (CPUState *env, uint32_t freq)
636aaad7
JM
1023{
1024 ppc_tb_t *tb_env;
1025 ppcemb_timer_t *ppcemb_timer;
1026
8ecc7913 1027 tb_env = qemu_mallocz(sizeof(ppc_tb_t));
4b6d0a4c 1028 if (tb_env == NULL) {
8ecc7913 1029 return NULL;
4b6d0a4c 1030 }
8ecc7913 1031 env->tb_env = tb_env;
636aaad7 1032 ppcemb_timer = qemu_mallocz(sizeof(ppcemb_timer_t));
8ecc7913 1033 tb_env->tb_freq = freq;
636aaad7 1034 tb_env->opaque = ppcemb_timer;
4b6d0a4c
JM
1035#ifdef PPC_DEBUG_TB
1036 if (loglevel != 0) {
1037 fprintf(logfile, "%s %p %p %p\n", __func__, tb_env, ppcemb_timer,
1038 &ppc_emb_set_tb_clk);
8ecc7913 1039 }
4b6d0a4c 1040#endif
636aaad7
JM
1041 if (ppcemb_timer != NULL) {
1042 /* We use decr timer for PIT */
1043 tb_env->decr_timer = qemu_new_timer(vm_clock, &cpu_4xx_pit_cb, env);
1044 ppcemb_timer->fit_timer =
1045 qemu_new_timer(vm_clock, &cpu_4xx_fit_cb, env);
1046 ppcemb_timer->wdt_timer =
1047 qemu_new_timer(vm_clock, &cpu_4xx_wdt_cb, env);
1048 }
8ecc7913 1049
4b6d0a4c 1050 return &ppc_emb_set_tb_clk;
76a66253
JM
1051}
1052
2e719ba3
JM
1053/*****************************************************************************/
1054/* Embedded PowerPC Device Control Registers */
1055typedef struct ppc_dcrn_t ppc_dcrn_t;
1056struct ppc_dcrn_t {
1057 dcr_read_cb dcr_read;
1058 dcr_write_cb dcr_write;
1059 void *opaque;
1060};
1061
a750fc0b
JM
1062/* XXX: on 460, DCR addresses are 32 bits wide,
1063 * using DCRIPR to get the 22 upper bits of the DCR address
1064 */
2e719ba3
JM
1065#define DCRN_NB 1024
1066struct ppc_dcr_t {
1067 ppc_dcrn_t dcrn[DCRN_NB];
1068 int (*read_error)(int dcrn);
1069 int (*write_error)(int dcrn);
1070};
1071
1072int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, target_ulong *valp)
1073{
1074 ppc_dcrn_t *dcr;
1075
1076 if (dcrn < 0 || dcrn >= DCRN_NB)
1077 goto error;
1078 dcr = &dcr_env->dcrn[dcrn];
1079 if (dcr->dcr_read == NULL)
1080 goto error;
1081 *valp = (*dcr->dcr_read)(dcr->opaque, dcrn);
1082
1083 return 0;
1084
1085 error:
1086 if (dcr_env->read_error != NULL)
1087 return (*dcr_env->read_error)(dcrn);
1088
1089 return -1;
1090}
1091
1092int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, target_ulong val)
1093{
1094 ppc_dcrn_t *dcr;
1095
1096 if (dcrn < 0 || dcrn >= DCRN_NB)
1097 goto error;
1098 dcr = &dcr_env->dcrn[dcrn];
1099 if (dcr->dcr_write == NULL)
1100 goto error;
1101 (*dcr->dcr_write)(dcr->opaque, dcrn, val);
1102
1103 return 0;
1104
1105 error:
1106 if (dcr_env->write_error != NULL)
1107 return (*dcr_env->write_error)(dcrn);
1108
1109 return -1;
1110}
1111
1112int ppc_dcr_register (CPUState *env, int dcrn, void *opaque,
1113 dcr_read_cb dcr_read, dcr_write_cb dcr_write)
1114{
1115 ppc_dcr_t *dcr_env;
1116 ppc_dcrn_t *dcr;
1117
1118 dcr_env = env->dcr_env;
1119 if (dcr_env == NULL)
1120 return -1;
1121 if (dcrn < 0 || dcrn >= DCRN_NB)
1122 return -1;
1123 dcr = &dcr_env->dcrn[dcrn];
1124 if (dcr->opaque != NULL ||
1125 dcr->dcr_read != NULL ||
1126 dcr->dcr_write != NULL)
1127 return -1;
1128 dcr->opaque = opaque;
1129 dcr->dcr_read = dcr_read;
1130 dcr->dcr_write = dcr_write;
1131
1132 return 0;
1133}
1134
1135int ppc_dcr_init (CPUState *env, int (*read_error)(int dcrn),
1136 int (*write_error)(int dcrn))
1137{
1138 ppc_dcr_t *dcr_env;
1139
1140 dcr_env = qemu_mallocz(sizeof(ppc_dcr_t));
1141 if (dcr_env == NULL)
1142 return -1;
1143 dcr_env->read_error = read_error;
1144 dcr_env->write_error = write_error;
1145 env->dcr_env = dcr_env;
1146
1147 return 0;
1148}
1149
1150
9fddaa0c
FB
1151#if 0
1152/*****************************************************************************/
1153/* Handle system reset (for now, just stop emulation) */
1154void cpu_ppc_reset (CPUState *env)
1155{
1156 printf("Reset asked... Stop emulation\n");
1157 abort();
1158}
1159#endif
1160
64201201
FB
1161/*****************************************************************************/
1162/* Debug port */
fd0bbb12 1163void PPC_debug_write (void *opaque, uint32_t addr, uint32_t val)
64201201
FB
1164{
1165 addr &= 0xF;
1166 switch (addr) {
1167 case 0:
1168 printf("%c", val);
1169 break;
1170 case 1:
1171 printf("\n");
1172 fflush(stdout);
1173 break;
1174 case 2:
1175 printf("Set loglevel to %04x\n", val);
fd0bbb12 1176 cpu_set_log(val | 0x100);
64201201
FB
1177 break;
1178 }
1179}
1180
1181/*****************************************************************************/
1182/* NVRAM helpers */
1183void NVRAM_set_byte (m48t59_t *nvram, uint32_t addr, uint8_t value)
1184{
819385c5 1185 m48t59_write(nvram, addr, value);
64201201
FB
1186}
1187
1188uint8_t NVRAM_get_byte (m48t59_t *nvram, uint32_t addr)
1189{
819385c5 1190 return m48t59_read(nvram, addr);
64201201
FB
1191}
1192
1193void NVRAM_set_word (m48t59_t *nvram, uint32_t addr, uint16_t value)
1194{
819385c5
FB
1195 m48t59_write(nvram, addr, value >> 8);
1196 m48t59_write(nvram, addr + 1, value & 0xFF);
64201201
FB
1197}
1198
1199uint16_t NVRAM_get_word (m48t59_t *nvram, uint32_t addr)
1200{
1201 uint16_t tmp;
1202
819385c5
FB
1203 tmp = m48t59_read(nvram, addr) << 8;
1204 tmp |= m48t59_read(nvram, addr + 1);
64201201
FB
1205 return tmp;
1206}
1207
1208void NVRAM_set_lword (m48t59_t *nvram, uint32_t addr, uint32_t value)
1209{
819385c5
FB
1210 m48t59_write(nvram, addr, value >> 24);
1211 m48t59_write(nvram, addr + 1, (value >> 16) & 0xFF);
1212 m48t59_write(nvram, addr + 2, (value >> 8) & 0xFF);
1213 m48t59_write(nvram, addr + 3, value & 0xFF);
64201201
FB
1214}
1215
1216uint32_t NVRAM_get_lword (m48t59_t *nvram, uint32_t addr)
1217{
1218 uint32_t tmp;
1219
819385c5
FB
1220 tmp = m48t59_read(nvram, addr) << 24;
1221 tmp |= m48t59_read(nvram, addr + 1) << 16;
1222 tmp |= m48t59_read(nvram, addr + 2) << 8;
1223 tmp |= m48t59_read(nvram, addr + 3);
76a66253 1224
64201201
FB
1225 return tmp;
1226}
1227
1228void NVRAM_set_string (m48t59_t *nvram, uint32_t addr,
1229 const unsigned char *str, uint32_t max)
1230{
1231 int i;
1232
1233 for (i = 0; i < max && str[i] != '\0'; i++) {
819385c5 1234 m48t59_write(nvram, addr + i, str[i]);
64201201 1235 }
819385c5 1236 m48t59_write(nvram, addr + max - 1, '\0');
64201201
FB
1237}
1238
1239int NVRAM_get_string (m48t59_t *nvram, uint8_t *dst, uint16_t addr, int max)
1240{
1241 int i;
1242
1243 memset(dst, 0, max);
1244 for (i = 0; i < max; i++) {
1245 dst[i] = NVRAM_get_byte(nvram, addr + i);
1246 if (dst[i] == '\0')
1247 break;
1248 }
1249
1250 return i;
1251}
1252
1253static uint16_t NVRAM_crc_update (uint16_t prev, uint16_t value)
1254{
1255 uint16_t tmp;
1256 uint16_t pd, pd1, pd2;
1257
1258 tmp = prev >> 8;
1259 pd = prev ^ value;
1260 pd1 = pd & 0x000F;
1261 pd2 = ((pd >> 4) & 0x000F) ^ pd1;
1262 tmp ^= (pd1 << 3) | (pd1 << 8);
1263 tmp ^= pd2 | (pd2 << 7) | (pd2 << 12);
1264
1265 return tmp;
1266}
1267
1268uint16_t NVRAM_compute_crc (m48t59_t *nvram, uint32_t start, uint32_t count)
1269{
1270 uint32_t i;
1271 uint16_t crc = 0xFFFF;
1272 int odd;
1273
1274 odd = count & 1;
1275 count &= ~1;
1276 for (i = 0; i != count; i++) {
76a66253 1277 crc = NVRAM_crc_update(crc, NVRAM_get_word(nvram, start + i));
64201201
FB
1278 }
1279 if (odd) {
76a66253 1280 crc = NVRAM_crc_update(crc, NVRAM_get_byte(nvram, start + i) << 8);
64201201
FB
1281 }
1282
1283 return crc;
1284}
1285
fd0bbb12
FB
1286#define CMDLINE_ADDR 0x017ff000
1287
64201201
FB
1288int PPC_NVRAM_set_params (m48t59_t *nvram, uint16_t NVRAM_size,
1289 const unsigned char *arch,
1290 uint32_t RAM_size, int boot_device,
1291 uint32_t kernel_image, uint32_t kernel_size,
fd0bbb12 1292 const char *cmdline,
64201201 1293 uint32_t initrd_image, uint32_t initrd_size,
fd0bbb12
FB
1294 uint32_t NVRAM_image,
1295 int width, int height, int depth)
64201201
FB
1296{
1297 uint16_t crc;
1298
1299 /* Set parameters for Open Hack'Ware BIOS */
1300 NVRAM_set_string(nvram, 0x00, "QEMU_BIOS", 16);
1301 NVRAM_set_lword(nvram, 0x10, 0x00000002); /* structure v2 */
1302 NVRAM_set_word(nvram, 0x14, NVRAM_size);
1303 NVRAM_set_string(nvram, 0x20, arch, 16);
1304 NVRAM_set_lword(nvram, 0x30, RAM_size);
1305 NVRAM_set_byte(nvram, 0x34, boot_device);
1306 NVRAM_set_lword(nvram, 0x38, kernel_image);
1307 NVRAM_set_lword(nvram, 0x3C, kernel_size);
fd0bbb12
FB
1308 if (cmdline) {
1309 /* XXX: put the cmdline in NVRAM too ? */
1310 strcpy(phys_ram_base + CMDLINE_ADDR, cmdline);
1311 NVRAM_set_lword(nvram, 0x40, CMDLINE_ADDR);
1312 NVRAM_set_lword(nvram, 0x44, strlen(cmdline));
1313 } else {
1314 NVRAM_set_lword(nvram, 0x40, 0);
1315 NVRAM_set_lword(nvram, 0x44, 0);
1316 }
64201201
FB
1317 NVRAM_set_lword(nvram, 0x48, initrd_image);
1318 NVRAM_set_lword(nvram, 0x4C, initrd_size);
1319 NVRAM_set_lword(nvram, 0x50, NVRAM_image);
fd0bbb12
FB
1320
1321 NVRAM_set_word(nvram, 0x54, width);
1322 NVRAM_set_word(nvram, 0x56, height);
1323 NVRAM_set_word(nvram, 0x58, depth);
1324 crc = NVRAM_compute_crc(nvram, 0x00, 0xF8);
1325 NVRAM_set_word(nvram, 0xFC, crc);
64201201
FB
1326
1327 return 0;
a541f297 1328}