2 * QEMU M48T59 and M48T08 NVRAM emulation for PPC PREP and Sparc platforms
4 * Copyright (c) 2003-2005 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
29 #if defined(DEBUG_NVRAM)
30 #define NVRAM_PRINTF(fmt, args...) do { printf(fmt , ##args); } while (0)
32 #define NVRAM_PRINTF(fmt, args...) do { } while (0)
36 * The M48T08 and M48T59 chips are very similar. The newer '59 has
37 * alarm and a watchdog timer and related control registers. In the
38 * PPC platform there is also a nvram lock function.
41 /* Model parameters */
42 int type
; // 8 = m48t08, 59 = m48t59
43 /* Hardware parameters */
52 /* Alarm & watchdog */
54 struct QEMUTimer
*alrm_timer
;
55 struct QEMUTimer
*wd_timer
;
62 /* Fake timer functions */
63 /* Generic helpers for BCD */
64 static inline uint8_t toBCD (uint8_t value
)
66 return (((value
/ 10) % 10) << 4) | (value
% 10);
69 static inline uint8_t fromBCD (uint8_t BCD
)
71 return ((BCD
>> 4) * 10) + (BCD
& 0x0F);
74 /* RTC management helpers */
75 static void get_time (m48t59_t
*NVRAM
, struct tm
*tm
)
79 t
= time(NULL
) + NVRAM
->time_offset
;
81 memcpy(tm
,localtime(&t
),sizeof(*tm
));
83 localtime_r (&t
, tm
) ;
87 static void set_time (m48t59_t
*NVRAM
, struct tm
*tm
)
91 new_time
= mktime(tm
);
93 NVRAM
->time_offset
= new_time
- now
;
96 /* Alarm management */
97 static void alarm_cb (void *opaque
)
101 m48t59_t
*NVRAM
= opaque
;
103 pic_set_irq(NVRAM
->IRQ
, 1);
104 if ((NVRAM
->buffer
[0x1FF5] & 0x80) == 0 &&
105 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
106 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
107 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
108 /* Repeat once a month */
109 get_time(NVRAM
, &tm_now
);
110 memcpy(&tm
, &tm_now
, sizeof(struct tm
));
112 if (tm
.tm_mon
== 13) {
116 next_time
= mktime(&tm
);
117 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
118 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
119 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
120 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
121 /* Repeat once a day */
122 next_time
= 24 * 60 * 60 + mktime(&tm_now
);
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 an hour */
128 next_time
= 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 a minute */
134 next_time
= 60 + mktime(&tm_now
);
136 /* Repeat once a second */
137 next_time
= 1 + mktime(&tm_now
);
139 qemu_mod_timer(NVRAM
->alrm_timer
, next_time
* 1000);
140 pic_set_irq(NVRAM
->IRQ
, 0);
144 static void get_alarm (m48t59_t
*NVRAM
, struct tm
*tm
)
147 memcpy(tm
,localtime(&NVRAM
->alarm
),sizeof(*tm
));
149 localtime_r (&NVRAM
->alarm
, tm
);
153 static void set_alarm (m48t59_t
*NVRAM
, struct tm
*tm
)
155 NVRAM
->alarm
= mktime(tm
);
156 if (NVRAM
->alrm_timer
!= NULL
) {
157 qemu_del_timer(NVRAM
->alrm_timer
);
158 NVRAM
->alrm_timer
= NULL
;
160 if (NVRAM
->alarm
- time(NULL
) > 0)
161 qemu_mod_timer(NVRAM
->alrm_timer
, NVRAM
->alarm
* 1000);
164 /* Watchdog management */
165 static void watchdog_cb (void *opaque
)
167 m48t59_t
*NVRAM
= opaque
;
169 NVRAM
->buffer
[0x1FF0] |= 0x80;
170 if (NVRAM
->buffer
[0x1FF7] & 0x80) {
171 NVRAM
->buffer
[0x1FF7] = 0x00;
172 NVRAM
->buffer
[0x1FFC] &= ~0x40;
173 /* May it be a hw CPU Reset instead ? */
174 qemu_system_reset_request();
176 pic_set_irq(NVRAM
->IRQ
, 1);
177 pic_set_irq(NVRAM
->IRQ
, 0);
181 static void set_up_watchdog (m48t59_t
*NVRAM
, uint8_t value
)
183 uint64_t interval
; /* in 1/16 seconds */
185 if (NVRAM
->wd_timer
!= NULL
) {
186 qemu_del_timer(NVRAM
->wd_timer
);
187 NVRAM
->wd_timer
= NULL
;
189 NVRAM
->buffer
[0x1FF0] &= ~0x80;
191 interval
= (1 << (2 * (value
& 0x03))) * ((value
>> 2) & 0x1F);
192 qemu_mod_timer(NVRAM
->wd_timer
, ((uint64_t)time(NULL
) * 1000) +
193 ((interval
* 1000) >> 4));
197 /* Direct access to NVRAM */
198 void m48t59_write (m48t59_t
*NVRAM
, uint32_t addr
, uint32_t val
)
203 if (addr
> 0x1FF8 && addr
< 0x2000)
204 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
205 if (NVRAM
->type
== 8 &&
206 (addr
>= 0x1ff0 && addr
<= 0x1ff7))
210 /* flags register : read-only */
217 tmp
= fromBCD(val
& 0x7F);
218 if (tmp
>= 0 && tmp
<= 59) {
219 get_alarm(NVRAM
, &tm
);
221 NVRAM
->buffer
[0x1FF2] = val
;
222 set_alarm(NVRAM
, &tm
);
227 tmp
= fromBCD(val
& 0x7F);
228 if (tmp
>= 0 && tmp
<= 59) {
229 get_alarm(NVRAM
, &tm
);
231 NVRAM
->buffer
[0x1FF3] = val
;
232 set_alarm(NVRAM
, &tm
);
237 tmp
= fromBCD(val
& 0x3F);
238 if (tmp
>= 0 && tmp
<= 23) {
239 get_alarm(NVRAM
, &tm
);
241 NVRAM
->buffer
[0x1FF4] = val
;
242 set_alarm(NVRAM
, &tm
);
247 tmp
= fromBCD(val
& 0x1F);
249 get_alarm(NVRAM
, &tm
);
251 NVRAM
->buffer
[0x1FF5] = val
;
252 set_alarm(NVRAM
, &tm
);
257 NVRAM
->buffer
[0x1FF6] = val
;
261 NVRAM
->buffer
[0x1FF7] = val
;
262 set_up_watchdog(NVRAM
, val
);
266 NVRAM
->buffer
[0x1FF8] = (val
& ~0xA0) | 0x90;
270 tmp
= fromBCD(val
& 0x7F);
271 if (tmp
>= 0 && tmp
<= 59) {
272 get_time(NVRAM
, &tm
);
274 set_time(NVRAM
, &tm
);
276 if ((val
& 0x80) ^ (NVRAM
->buffer
[0x1FF9] & 0x80)) {
278 NVRAM
->stop_time
= time(NULL
);
280 NVRAM
->time_offset
+= NVRAM
->stop_time
- time(NULL
);
281 NVRAM
->stop_time
= 0;
284 NVRAM
->buffer
[0x1FF9] = val
& 0x80;
288 tmp
= fromBCD(val
& 0x7F);
289 if (tmp
>= 0 && tmp
<= 59) {
290 get_time(NVRAM
, &tm
);
292 set_time(NVRAM
, &tm
);
297 tmp
= fromBCD(val
& 0x3F);
298 if (tmp
>= 0 && tmp
<= 23) {
299 get_time(NVRAM
, &tm
);
301 set_time(NVRAM
, &tm
);
305 /* day of the week / century */
306 tmp
= fromBCD(val
& 0x07);
307 get_time(NVRAM
, &tm
);
309 set_time(NVRAM
, &tm
);
310 NVRAM
->buffer
[0x1FFC] = val
& 0x40;
314 tmp
= fromBCD(val
& 0x1F);
316 get_time(NVRAM
, &tm
);
318 set_time(NVRAM
, &tm
);
323 tmp
= fromBCD(val
& 0x1F);
324 if (tmp
>= 1 && tmp
<= 12) {
325 get_time(NVRAM
, &tm
);
327 set_time(NVRAM
, &tm
);
333 if (tmp
>= 0 && tmp
<= 99) {
334 get_time(NVRAM
, &tm
);
335 tm
.tm_year
= fromBCD(val
);
336 set_time(NVRAM
, &tm
);
340 /* Check lock registers state */
341 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
343 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
346 if (addr
< NVRAM
->size
) {
347 NVRAM
->buffer
[addr
] = val
& 0xFF;
353 uint32_t m48t59_read (m48t59_t
*NVRAM
, uint32_t addr
)
356 uint32_t retval
= 0xFF;
358 if (NVRAM
->type
== 8 &&
359 (addr
>= 0x1ff0 && addr
<= 0x1ff7))
385 /* A read resets the watchdog */
386 set_up_watchdog(NVRAM
, NVRAM
->buffer
[0x1FF7]);
393 get_time(NVRAM
, &tm
);
394 retval
= (NVRAM
->buffer
[0x1FF9] & 0x80) | toBCD(tm
.tm_sec
);
398 get_time(NVRAM
, &tm
);
399 retval
= toBCD(tm
.tm_min
);
403 get_time(NVRAM
, &tm
);
404 retval
= toBCD(tm
.tm_hour
);
407 /* day of the week / century */
408 get_time(NVRAM
, &tm
);
409 retval
= NVRAM
->buffer
[0x1FFC] | tm
.tm_wday
;
413 get_time(NVRAM
, &tm
);
414 retval
= toBCD(tm
.tm_mday
);
418 get_time(NVRAM
, &tm
);
419 retval
= toBCD(tm
.tm_mon
+ 1);
423 get_time(NVRAM
, &tm
);
424 retval
= toBCD(tm
.tm_year
);
427 /* Check lock registers state */
428 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
430 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
433 if (addr
< NVRAM
->size
) {
434 retval
= NVRAM
->buffer
[addr
];
438 if (addr
> 0x1FF9 && addr
< 0x2000)
439 NVRAM_PRINTF("0x%08x <= 0x%08x\n", addr
, retval
);
444 void m48t59_set_addr (m48t59_t
*NVRAM
, uint32_t addr
)
449 void m48t59_toggle_lock (m48t59_t
*NVRAM
, int lock
)
451 NVRAM
->lock
^= 1 << lock
;
454 /* IO access to NVRAM */
455 static void NVRAM_writeb (void *opaque
, uint32_t addr
, uint32_t val
)
457 m48t59_t
*NVRAM
= opaque
;
459 addr
-= NVRAM
->io_base
;
460 NVRAM_PRINTF("0x%08x => 0x%08x\n", addr
, val
);
463 NVRAM
->addr
&= ~0x00FF;
467 NVRAM
->addr
&= ~0xFF00;
468 NVRAM
->addr
|= val
<< 8;
471 m48t59_write(NVRAM
, val
, NVRAM
->addr
);
472 NVRAM
->addr
= 0x0000;
479 static uint32_t NVRAM_readb (void *opaque
, uint32_t addr
)
481 m48t59_t
*NVRAM
= opaque
;
484 addr
-= NVRAM
->io_base
;
487 retval
= m48t59_read(NVRAM
, NVRAM
->addr
);
493 NVRAM_PRINTF("0x%08x <= 0x%08x\n", addr
, retval
);
498 static void nvram_writeb (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
500 m48t59_t
*NVRAM
= opaque
;
502 addr
-= NVRAM
->mem_base
;
503 m48t59_write(NVRAM
, addr
, value
& 0xff);
506 static void nvram_writew (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
508 m48t59_t
*NVRAM
= opaque
;
510 addr
-= NVRAM
->mem_base
;
511 m48t59_write(NVRAM
, addr
, (value
>> 8) & 0xff);
512 m48t59_write(NVRAM
, addr
+ 1, value
& 0xff);
515 static void nvram_writel (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
517 m48t59_t
*NVRAM
= opaque
;
519 addr
-= NVRAM
->mem_base
;
520 m48t59_write(NVRAM
, addr
, (value
>> 24) & 0xff);
521 m48t59_write(NVRAM
, addr
+ 1, (value
>> 16) & 0xff);
522 m48t59_write(NVRAM
, addr
+ 2, (value
>> 8) & 0xff);
523 m48t59_write(NVRAM
, addr
+ 3, value
& 0xff);
526 static uint32_t nvram_readb (void *opaque
, target_phys_addr_t addr
)
528 m48t59_t
*NVRAM
= opaque
;
531 addr
-= NVRAM
->mem_base
;
532 retval
= m48t59_read(NVRAM
, addr
);
536 static uint32_t nvram_readw (void *opaque
, target_phys_addr_t addr
)
538 m48t59_t
*NVRAM
= opaque
;
541 addr
-= NVRAM
->mem_base
;
542 retval
= m48t59_read(NVRAM
, addr
) << 8;
543 retval
|= m48t59_read(NVRAM
, addr
+ 1);
547 static uint32_t nvram_readl (void *opaque
, target_phys_addr_t addr
)
549 m48t59_t
*NVRAM
= opaque
;
552 addr
-= NVRAM
->mem_base
;
553 retval
= m48t59_read(NVRAM
, addr
) << 24;
554 retval
|= m48t59_read(NVRAM
, addr
+ 1) << 16;
555 retval
|= m48t59_read(NVRAM
, addr
+ 2) << 8;
556 retval
|= m48t59_read(NVRAM
, addr
+ 3);
560 static CPUWriteMemoryFunc
*nvram_write
[] = {
566 static CPUReadMemoryFunc
*nvram_read
[] = {
572 /* Initialisation routine */
573 m48t59_t
*m48t59_init (int IRQ
, target_ulong mem_base
,
574 uint32_t io_base
, uint16_t size
,
579 s
= qemu_mallocz(sizeof(m48t59_t
));
582 s
->buffer
= qemu_mallocz(size
);
589 s
->mem_base
= mem_base
;
590 s
->io_base
= io_base
;
594 register_ioport_read(io_base
, 0x04, 1, NVRAM_readb
, s
);
595 register_ioport_write(io_base
, 0x04, 1, NVRAM_writeb
, s
);
598 s
->mem_index
= cpu_register_io_memory(0, nvram_read
, nvram_write
, s
);
599 cpu_register_physical_memory(mem_base
, 0x4000, s
->mem_index
);
602 s
->alrm_timer
= qemu_new_timer(vm_clock
, &alarm_cb
, s
);
603 s
->wd_timer
= qemu_new_timer(vm_clock
, &watchdog_cb
, s
);