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 if (NVRAM
->type
== 8)
336 tm
.tm_year
= fromBCD(val
) + 68; // Base year is 1968
338 tm
.tm_year
= fromBCD(val
);
339 set_time(NVRAM
, &tm
);
343 /* Check lock registers state */
344 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
346 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
349 if (addr
< NVRAM
->size
) {
350 NVRAM
->buffer
[addr
] = val
& 0xFF;
356 uint32_t m48t59_read (m48t59_t
*NVRAM
, uint32_t addr
)
359 uint32_t retval
= 0xFF;
361 if (NVRAM
->type
== 8 &&
362 (addr
>= 0x1ff0 && addr
<= 0x1ff7))
388 /* A read resets the watchdog */
389 set_up_watchdog(NVRAM
, NVRAM
->buffer
[0x1FF7]);
396 get_time(NVRAM
, &tm
);
397 retval
= (NVRAM
->buffer
[0x1FF9] & 0x80) | toBCD(tm
.tm_sec
);
401 get_time(NVRAM
, &tm
);
402 retval
= toBCD(tm
.tm_min
);
406 get_time(NVRAM
, &tm
);
407 retval
= toBCD(tm
.tm_hour
);
410 /* day of the week / century */
411 get_time(NVRAM
, &tm
);
412 retval
= NVRAM
->buffer
[0x1FFC] | tm
.tm_wday
;
416 get_time(NVRAM
, &tm
);
417 retval
= toBCD(tm
.tm_mday
);
421 get_time(NVRAM
, &tm
);
422 retval
= toBCD(tm
.tm_mon
+ 1);
426 get_time(NVRAM
, &tm
);
427 if (NVRAM
->type
== 8)
428 retval
= toBCD(tm
.tm_year
- 68); // Base year is 1968
430 retval
= toBCD(tm
.tm_year
);
433 /* Check lock registers state */
434 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
436 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
439 if (addr
< NVRAM
->size
) {
440 retval
= NVRAM
->buffer
[addr
];
444 if (addr
> 0x1FF9 && addr
< 0x2000)
445 NVRAM_PRINTF("0x%08x <= 0x%08x\n", addr
, retval
);
450 void m48t59_set_addr (m48t59_t
*NVRAM
, uint32_t addr
)
455 void m48t59_toggle_lock (m48t59_t
*NVRAM
, int lock
)
457 NVRAM
->lock
^= 1 << lock
;
460 /* IO access to NVRAM */
461 static void NVRAM_writeb (void *opaque
, uint32_t addr
, uint32_t val
)
463 m48t59_t
*NVRAM
= opaque
;
465 addr
-= NVRAM
->io_base
;
466 NVRAM_PRINTF("0x%08x => 0x%08x\n", addr
, val
);
469 NVRAM
->addr
&= ~0x00FF;
473 NVRAM
->addr
&= ~0xFF00;
474 NVRAM
->addr
|= val
<< 8;
477 m48t59_write(NVRAM
, val
, NVRAM
->addr
);
478 NVRAM
->addr
= 0x0000;
485 static uint32_t NVRAM_readb (void *opaque
, uint32_t addr
)
487 m48t59_t
*NVRAM
= opaque
;
490 addr
-= NVRAM
->io_base
;
493 retval
= m48t59_read(NVRAM
, NVRAM
->addr
);
499 NVRAM_PRINTF("0x%08x <= 0x%08x\n", addr
, retval
);
504 static void nvram_writeb (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
506 m48t59_t
*NVRAM
= opaque
;
508 addr
-= NVRAM
->mem_base
;
509 m48t59_write(NVRAM
, addr
, value
& 0xff);
512 static void nvram_writew (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
514 m48t59_t
*NVRAM
= opaque
;
516 addr
-= NVRAM
->mem_base
;
517 m48t59_write(NVRAM
, addr
, (value
>> 8) & 0xff);
518 m48t59_write(NVRAM
, addr
+ 1, value
& 0xff);
521 static void nvram_writel (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
523 m48t59_t
*NVRAM
= opaque
;
525 addr
-= NVRAM
->mem_base
;
526 m48t59_write(NVRAM
, addr
, (value
>> 24) & 0xff);
527 m48t59_write(NVRAM
, addr
+ 1, (value
>> 16) & 0xff);
528 m48t59_write(NVRAM
, addr
+ 2, (value
>> 8) & 0xff);
529 m48t59_write(NVRAM
, addr
+ 3, value
& 0xff);
532 static uint32_t nvram_readb (void *opaque
, target_phys_addr_t addr
)
534 m48t59_t
*NVRAM
= opaque
;
537 addr
-= NVRAM
->mem_base
;
538 retval
= m48t59_read(NVRAM
, addr
);
542 static uint32_t nvram_readw (void *opaque
, target_phys_addr_t addr
)
544 m48t59_t
*NVRAM
= opaque
;
547 addr
-= NVRAM
->mem_base
;
548 retval
= m48t59_read(NVRAM
, addr
) << 8;
549 retval
|= m48t59_read(NVRAM
, addr
+ 1);
553 static uint32_t nvram_readl (void *opaque
, target_phys_addr_t addr
)
555 m48t59_t
*NVRAM
= opaque
;
558 addr
-= NVRAM
->mem_base
;
559 retval
= m48t59_read(NVRAM
, addr
) << 24;
560 retval
|= m48t59_read(NVRAM
, addr
+ 1) << 16;
561 retval
|= m48t59_read(NVRAM
, addr
+ 2) << 8;
562 retval
|= m48t59_read(NVRAM
, addr
+ 3);
566 static CPUWriteMemoryFunc
*nvram_write
[] = {
572 static CPUReadMemoryFunc
*nvram_read
[] = {
578 /* Initialisation routine */
579 m48t59_t
*m48t59_init (int IRQ
, target_ulong mem_base
,
580 uint32_t io_base
, uint16_t size
,
585 s
= qemu_mallocz(sizeof(m48t59_t
));
588 s
->buffer
= qemu_mallocz(size
);
595 s
->mem_base
= mem_base
;
596 s
->io_base
= io_base
;
600 register_ioport_read(io_base
, 0x04, 1, NVRAM_readb
, s
);
601 register_ioport_write(io_base
, 0x04, 1, NVRAM_writeb
, s
);
604 s
->mem_index
= cpu_register_io_memory(0, nvram_read
, nvram_write
, s
);
605 cpu_register_physical_memory(mem_base
, 0x4000, s
->mem_index
);
608 s
->alrm_timer
= qemu_new_timer(vm_clock
, &alarm_cb
, s
);
609 s
->wd_timer
= qemu_new_timer(vm_clock
, &watchdog_cb
, s
);