2 * QEMU M48T59 and M48T08 NVRAM emulation for PPC PREP and Sparc platforms
4 * Copyright (c) 2003-2005, 2007 Jocelyn Mayer
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
27 #include "qemu-timer.h"
32 #if defined(DEBUG_NVRAM)
33 #define NVRAM_PRINTF(fmt, args...) do { printf(fmt , ##args); } while (0)
35 #define NVRAM_PRINTF(fmt, args...) do { } while (0)
39 * The M48T08 and M48T59 chips are very similar. The newer '59 has
40 * alarm and a watchdog timer and related control registers. In the
41 * PPC platform there is also a nvram lock function.
44 /* Model parameters */
45 int type
; // 8 = m48t08, 59 = m48t59
46 /* Hardware parameters */
49 target_phys_addr_t mem_base
;
55 /* Alarm & watchdog */
57 struct QEMUTimer
*alrm_timer
;
58 struct QEMUTimer
*wd_timer
;
65 /* Fake timer functions */
66 /* Generic helpers for BCD */
67 static inline uint8_t toBCD (uint8_t value
)
69 return (((value
/ 10) % 10) << 4) | (value
% 10);
72 static inline uint8_t fromBCD (uint8_t BCD
)
74 return ((BCD
>> 4) * 10) + (BCD
& 0x0F);
77 /* RTC management helpers */
78 static void get_time (m48t59_t
*NVRAM
, struct tm
*tm
)
82 t
= time(NULL
) + NVRAM
->time_offset
;
84 memcpy(tm
,localtime(&t
),sizeof(*tm
));
89 localtime_r (&t
, tm
) ;
93 static void set_time (m48t59_t
*NVRAM
, struct tm
*tm
)
97 new_time
= mktime(tm
);
99 NVRAM
->time_offset
= new_time
- now
;
102 /* Alarm management */
103 static void alarm_cb (void *opaque
)
105 struct tm tm
, tm_now
;
107 m48t59_t
*NVRAM
= opaque
;
109 qemu_set_irq(NVRAM
->IRQ
, 1);
110 if ((NVRAM
->buffer
[0x1FF5] & 0x80) == 0 &&
111 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
112 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
113 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
114 /* Repeat once a month */
115 get_time(NVRAM
, &tm_now
);
116 memcpy(&tm
, &tm_now
, sizeof(struct tm
));
118 if (tm
.tm_mon
== 13) {
122 next_time
= mktime(&tm
);
123 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
124 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
125 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
126 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
127 /* Repeat once a day */
128 next_time
= 24 * 60 * 60 + mktime(&tm_now
);
129 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
130 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
131 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
132 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
133 /* Repeat once an hour */
134 next_time
= 60 * 60 + mktime(&tm_now
);
135 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
136 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
137 (NVRAM
->buffer
[0x1FF3] & 0x80) != 0 &&
138 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
139 /* Repeat once a minute */
140 next_time
= 60 + mktime(&tm_now
);
142 /* Repeat once a second */
143 next_time
= 1 + mktime(&tm_now
);
145 qemu_mod_timer(NVRAM
->alrm_timer
, next_time
* 1000);
146 qemu_set_irq(NVRAM
->IRQ
, 0);
150 static void get_alarm (m48t59_t
*NVRAM
, struct tm
*tm
)
153 memcpy(tm
,localtime(&NVRAM
->alarm
),sizeof(*tm
));
156 gmtime_r (&NVRAM
->alarm
, tm
);
158 localtime_r (&NVRAM
->alarm
, tm
);
162 static void set_alarm (m48t59_t
*NVRAM
, struct tm
*tm
)
164 NVRAM
->alarm
= mktime(tm
);
165 if (NVRAM
->alrm_timer
!= NULL
) {
166 qemu_del_timer(NVRAM
->alrm_timer
);
167 if (NVRAM
->alarm
- time(NULL
) > 0)
168 qemu_mod_timer(NVRAM
->alrm_timer
, NVRAM
->alarm
* 1000);
172 /* Watchdog management */
173 static void watchdog_cb (void *opaque
)
175 m48t59_t
*NVRAM
= opaque
;
177 NVRAM
->buffer
[0x1FF0] |= 0x80;
178 if (NVRAM
->buffer
[0x1FF7] & 0x80) {
179 NVRAM
->buffer
[0x1FF7] = 0x00;
180 NVRAM
->buffer
[0x1FFC] &= ~0x40;
181 /* May it be a hw CPU Reset instead ? */
182 qemu_system_reset_request();
184 qemu_set_irq(NVRAM
->IRQ
, 1);
185 qemu_set_irq(NVRAM
->IRQ
, 0);
189 static void set_up_watchdog (m48t59_t
*NVRAM
, uint8_t value
)
191 uint64_t interval
; /* in 1/16 seconds */
193 NVRAM
->buffer
[0x1FF0] &= ~0x80;
194 if (NVRAM
->wd_timer
!= NULL
) {
195 qemu_del_timer(NVRAM
->wd_timer
);
197 interval
= (1 << (2 * (value
& 0x03))) * ((value
>> 2) & 0x1F);
198 qemu_mod_timer(NVRAM
->wd_timer
, ((uint64_t)time(NULL
) * 1000) +
199 ((interval
* 1000) >> 4));
204 /* Direct access to NVRAM */
205 void m48t59_write (void *opaque
, uint32_t addr
, uint32_t val
)
207 m48t59_t
*NVRAM
= opaque
;
211 if (addr
> 0x1FF8 && addr
< 0x2000)
212 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
213 if (NVRAM
->type
== 8 &&
214 (addr
>= 0x1ff0 && addr
<= 0x1ff7))
218 /* flags register : read-only */
225 tmp
= fromBCD(val
& 0x7F);
226 if (tmp
>= 0 && tmp
<= 59) {
227 get_alarm(NVRAM
, &tm
);
229 NVRAM
->buffer
[0x1FF2] = val
;
230 set_alarm(NVRAM
, &tm
);
235 tmp
= fromBCD(val
& 0x7F);
236 if (tmp
>= 0 && tmp
<= 59) {
237 get_alarm(NVRAM
, &tm
);
239 NVRAM
->buffer
[0x1FF3] = val
;
240 set_alarm(NVRAM
, &tm
);
245 tmp
= fromBCD(val
& 0x3F);
246 if (tmp
>= 0 && tmp
<= 23) {
247 get_alarm(NVRAM
, &tm
);
249 NVRAM
->buffer
[0x1FF4] = val
;
250 set_alarm(NVRAM
, &tm
);
255 tmp
= fromBCD(val
& 0x1F);
257 get_alarm(NVRAM
, &tm
);
259 NVRAM
->buffer
[0x1FF5] = val
;
260 set_alarm(NVRAM
, &tm
);
265 NVRAM
->buffer
[0x1FF6] = val
;
269 NVRAM
->buffer
[0x1FF7] = val
;
270 set_up_watchdog(NVRAM
, val
);
274 NVRAM
->buffer
[0x1FF8] = (val
& ~0xA0) | 0x90;
278 tmp
= fromBCD(val
& 0x7F);
279 if (tmp
>= 0 && tmp
<= 59) {
280 get_time(NVRAM
, &tm
);
282 set_time(NVRAM
, &tm
);
284 if ((val
& 0x80) ^ (NVRAM
->buffer
[0x1FF9] & 0x80)) {
286 NVRAM
->stop_time
= time(NULL
);
288 NVRAM
->time_offset
+= NVRAM
->stop_time
- time(NULL
);
289 NVRAM
->stop_time
= 0;
292 NVRAM
->buffer
[0x1FF9] = val
& 0x80;
296 tmp
= fromBCD(val
& 0x7F);
297 if (tmp
>= 0 && tmp
<= 59) {
298 get_time(NVRAM
, &tm
);
300 set_time(NVRAM
, &tm
);
305 tmp
= fromBCD(val
& 0x3F);
306 if (tmp
>= 0 && tmp
<= 23) {
307 get_time(NVRAM
, &tm
);
309 set_time(NVRAM
, &tm
);
313 /* day of the week / century */
314 tmp
= fromBCD(val
& 0x07);
315 get_time(NVRAM
, &tm
);
317 set_time(NVRAM
, &tm
);
318 NVRAM
->buffer
[0x1FFC] = val
& 0x40;
322 tmp
= fromBCD(val
& 0x1F);
324 get_time(NVRAM
, &tm
);
326 set_time(NVRAM
, &tm
);
331 tmp
= fromBCD(val
& 0x1F);
332 if (tmp
>= 1 && tmp
<= 12) {
333 get_time(NVRAM
, &tm
);
335 set_time(NVRAM
, &tm
);
341 if (tmp
>= 0 && tmp
<= 99) {
342 get_time(NVRAM
, &tm
);
343 if (NVRAM
->type
== 8)
344 tm
.tm_year
= fromBCD(val
) + 68; // Base year is 1968
346 tm
.tm_year
= fromBCD(val
);
347 set_time(NVRAM
, &tm
);
351 /* Check lock registers state */
352 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
354 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
357 if (addr
< NVRAM
->size
) {
358 NVRAM
->buffer
[addr
] = val
& 0xFF;
364 uint32_t m48t59_read (void *opaque
, uint32_t addr
)
366 m48t59_t
*NVRAM
= opaque
;
368 uint32_t retval
= 0xFF;
370 if (NVRAM
->type
== 8 &&
371 (addr
>= 0x1ff0 && addr
<= 0x1ff7))
397 /* A read resets the watchdog */
398 set_up_watchdog(NVRAM
, NVRAM
->buffer
[0x1FF7]);
405 get_time(NVRAM
, &tm
);
406 retval
= (NVRAM
->buffer
[0x1FF9] & 0x80) | toBCD(tm
.tm_sec
);
410 get_time(NVRAM
, &tm
);
411 retval
= toBCD(tm
.tm_min
);
415 get_time(NVRAM
, &tm
);
416 retval
= toBCD(tm
.tm_hour
);
419 /* day of the week / century */
420 get_time(NVRAM
, &tm
);
421 retval
= NVRAM
->buffer
[0x1FFC] | tm
.tm_wday
;
425 get_time(NVRAM
, &tm
);
426 retval
= toBCD(tm
.tm_mday
);
430 get_time(NVRAM
, &tm
);
431 retval
= toBCD(tm
.tm_mon
+ 1);
435 get_time(NVRAM
, &tm
);
436 if (NVRAM
->type
== 8)
437 retval
= toBCD(tm
.tm_year
- 68); // Base year is 1968
439 retval
= toBCD(tm
.tm_year
);
442 /* Check lock registers state */
443 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
445 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
448 if (addr
< NVRAM
->size
) {
449 retval
= NVRAM
->buffer
[addr
];
453 if (addr
> 0x1FF9 && addr
< 0x2000)
454 NVRAM_PRINTF("0x%08x <= 0x%08x\n", addr
, retval
);
459 void m48t59_set_addr (void *opaque
, uint32_t addr
)
461 m48t59_t
*NVRAM
= opaque
;
466 void m48t59_toggle_lock (void *opaque
, int lock
)
468 m48t59_t
*NVRAM
= opaque
;
470 NVRAM
->lock
^= 1 << lock
;
473 /* IO access to NVRAM */
474 static void NVRAM_writeb (void *opaque
, uint32_t addr
, uint32_t val
)
476 m48t59_t
*NVRAM
= opaque
;
478 addr
-= NVRAM
->io_base
;
479 NVRAM_PRINTF("0x%08x => 0x%08x\n", addr
, val
);
482 NVRAM
->addr
&= ~0x00FF;
486 NVRAM
->addr
&= ~0xFF00;
487 NVRAM
->addr
|= val
<< 8;
490 m48t59_write(NVRAM
, val
, NVRAM
->addr
);
491 NVRAM
->addr
= 0x0000;
498 static uint32_t NVRAM_readb (void *opaque
, uint32_t addr
)
500 m48t59_t
*NVRAM
= opaque
;
503 addr
-= NVRAM
->io_base
;
506 retval
= m48t59_read(NVRAM
, NVRAM
->addr
);
512 NVRAM_PRINTF("0x%08x <= 0x%08x\n", addr
, retval
);
517 static void nvram_writeb (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
519 m48t59_t
*NVRAM
= opaque
;
521 addr
-= NVRAM
->mem_base
;
522 m48t59_write(NVRAM
, addr
, value
& 0xff);
525 static void nvram_writew (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
527 m48t59_t
*NVRAM
= opaque
;
529 addr
-= NVRAM
->mem_base
;
530 m48t59_write(NVRAM
, addr
, (value
>> 8) & 0xff);
531 m48t59_write(NVRAM
, addr
+ 1, value
& 0xff);
534 static void nvram_writel (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
536 m48t59_t
*NVRAM
= opaque
;
538 addr
-= NVRAM
->mem_base
;
539 m48t59_write(NVRAM
, addr
, (value
>> 24) & 0xff);
540 m48t59_write(NVRAM
, addr
+ 1, (value
>> 16) & 0xff);
541 m48t59_write(NVRAM
, addr
+ 2, (value
>> 8) & 0xff);
542 m48t59_write(NVRAM
, addr
+ 3, value
& 0xff);
545 static uint32_t nvram_readb (void *opaque
, target_phys_addr_t addr
)
547 m48t59_t
*NVRAM
= opaque
;
550 addr
-= NVRAM
->mem_base
;
551 retval
= m48t59_read(NVRAM
, addr
);
555 static uint32_t nvram_readw (void *opaque
, target_phys_addr_t addr
)
557 m48t59_t
*NVRAM
= opaque
;
560 addr
-= NVRAM
->mem_base
;
561 retval
= m48t59_read(NVRAM
, addr
) << 8;
562 retval
|= m48t59_read(NVRAM
, addr
+ 1);
566 static uint32_t nvram_readl (void *opaque
, target_phys_addr_t addr
)
568 m48t59_t
*NVRAM
= opaque
;
571 addr
-= NVRAM
->mem_base
;
572 retval
= m48t59_read(NVRAM
, addr
) << 24;
573 retval
|= m48t59_read(NVRAM
, addr
+ 1) << 16;
574 retval
|= m48t59_read(NVRAM
, addr
+ 2) << 8;
575 retval
|= m48t59_read(NVRAM
, addr
+ 3);
579 static CPUWriteMemoryFunc
*nvram_write
[] = {
585 static CPUReadMemoryFunc
*nvram_read
[] = {
591 static void m48t59_save(QEMUFile
*f
, void *opaque
)
593 m48t59_t
*s
= opaque
;
595 qemu_put_8s(f
, &s
->lock
);
596 qemu_put_be16s(f
, &s
->addr
);
597 qemu_put_buffer(f
, s
->buffer
, s
->size
);
600 static int m48t59_load(QEMUFile
*f
, void *opaque
, int version_id
)
602 m48t59_t
*s
= opaque
;
607 qemu_get_8s(f
, &s
->lock
);
608 qemu_get_be16s(f
, &s
->addr
);
609 qemu_get_buffer(f
, s
->buffer
, s
->size
);
614 static void m48t59_reset(void *opaque
)
616 m48t59_t
*NVRAM
= opaque
;
618 if (NVRAM
->alrm_timer
!= NULL
)
619 qemu_del_timer(NVRAM
->alrm_timer
);
621 if (NVRAM
->wd_timer
!= NULL
)
622 qemu_del_timer(NVRAM
->wd_timer
);
625 /* Initialisation routine */
626 m48t59_t
*m48t59_init (qemu_irq IRQ
, target_phys_addr_t mem_base
,
627 uint32_t io_base
, uint16_t size
,
631 target_phys_addr_t save_base
;
633 s
= qemu_mallocz(sizeof(m48t59_t
));
636 s
->buffer
= qemu_mallocz(size
);
643 s
->mem_base
= mem_base
;
644 s
->io_base
= io_base
;
648 register_ioport_read(io_base
, 0x04, 1, NVRAM_readb
, s
);
649 register_ioport_write(io_base
, 0x04, 1, NVRAM_writeb
, s
);
652 s
->mem_index
= cpu_register_io_memory(0, nvram_read
, nvram_write
, s
);
653 cpu_register_physical_memory(mem_base
, 0x4000, s
->mem_index
);
656 s
->alrm_timer
= qemu_new_timer(vm_clock
, &alarm_cb
, s
);
657 s
->wd_timer
= qemu_new_timer(vm_clock
, &watchdog_cb
, s
);
661 qemu_register_reset(m48t59_reset
, s
);
662 save_base
= mem_base
? mem_base
: io_base
;
663 register_savevm("m48t59", save_base
, 1, m48t59_save
, m48t59_load
, s
);