2 * QEMU M48T59 NVRAM emulation for PPC PREP platform
4 * Copyright (c) 2003-2004 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 /* Hardware parameters */
45 /* Alarm & watchdog */
47 struct QEMUTimer *alrm_timer;
48 struct QEMUTimer *wd_timer;
55 /* Fake timer functions */
56 /* Generic helpers for BCD */
57 static inline uint8_t toBCD (uint8_t value)
59 return (((value / 10) % 10) << 4) | (value % 10);
62 static inline uint8_t fromBCD (uint8_t BCD)
64 return ((BCD >> 4) * 10) + (BCD & 0x0F);
67 /* RTC management helpers */
68 static void get_time (m48t59_t *NVRAM, struct tm *tm)
72 t = time(NULL) + NVRAM->time_offset;
74 memcpy(tm,localtime(&t),sizeof(*tm));
76 localtime_r (&t, tm) ;
80 static void set_time (m48t59_t *NVRAM, struct tm *tm)
84 new_time = mktime(tm);
86 NVRAM->time_offset = new_time - now;
89 /* Alarm management */
90 static void alarm_cb (void *opaque)
94 m48t59_t *NVRAM = opaque;
96 pic_set_irq(NVRAM->IRQ, 1);
97 if ((NVRAM->buffer[0x1FF5] & 0x80) == 0 &&
98 (NVRAM->buffer[0x1FF4] & 0x80) == 0 &&
99 (NVRAM->buffer[0x1FF3] & 0x80) == 0 &&
100 (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
101 /* Repeat once a month */
102 get_time(NVRAM, &tm_now);
103 memcpy(&tm, &tm_now, sizeof(struct tm));
105 if (tm.tm_mon == 13) {
109 next_time = mktime(&tm);
110 } else 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 day */
115 next_time = 24 * 60 * 60 + mktime(&tm_now);
116 } else if ((NVRAM->buffer[0x1FF5] & 0x80) != 0 &&
117 (NVRAM->buffer[0x1FF4] & 0x80) != 0 &&
118 (NVRAM->buffer[0x1FF3] & 0x80) == 0 &&
119 (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
120 /* Repeat once an hour */
121 next_time = 60 * 60 + mktime(&tm_now);
122 } else if ((NVRAM->buffer[0x1FF5] & 0x80) != 0 &&
123 (NVRAM->buffer[0x1FF4] & 0x80) != 0 &&
124 (NVRAM->buffer[0x1FF3] & 0x80) != 0 &&
125 (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
126 /* Repeat once a minute */
127 next_time = 60 + mktime(&tm_now);
129 /* Repeat once a second */
130 next_time = 1 + mktime(&tm_now);
132 qemu_mod_timer(NVRAM->alrm_timer, next_time * 1000);
133 pic_set_irq(NVRAM->IRQ, 0);
137 static void get_alarm (m48t59_t *NVRAM, struct tm *tm)
140 memcpy(tm,localtime(&NVRAM->alarm),sizeof(*tm));
142 localtime_r (&NVRAM->alarm, tm);
146 static void set_alarm (m48t59_t *NVRAM, struct tm *tm)
148 NVRAM->alarm = mktime(tm);
149 if (NVRAM->alrm_timer != NULL) {
150 qemu_del_timer(NVRAM->alrm_timer);
151 NVRAM->alrm_timer = NULL;
153 if (NVRAM->alarm - time(NULL) > 0)
154 qemu_mod_timer(NVRAM->alrm_timer, NVRAM->alarm * 1000);
157 /* Watchdog management */
158 static void watchdog_cb (void *opaque)
160 m48t59_t *NVRAM = opaque;
162 NVRAM->buffer[0x1FF0] |= 0x80;
163 if (NVRAM->buffer[0x1FF7] & 0x80) {
164 NVRAM->buffer[0x1FF7] = 0x00;
165 NVRAM->buffer[0x1FFC] &= ~0x40;
166 /* May it be a hw CPU Reset instead ? */
167 qemu_system_reset_request();
169 pic_set_irq(NVRAM->IRQ, 1);
170 pic_set_irq(NVRAM->IRQ, 0);
174 static void set_up_watchdog (m48t59_t *NVRAM, uint8_t value)
176 uint64_t interval; /* in 1/16 seconds */
178 if (NVRAM->wd_timer != NULL) {
179 qemu_del_timer(NVRAM->wd_timer);
180 NVRAM->wd_timer = NULL;
182 NVRAM->buffer[0x1FF0] &= ~0x80;
184 interval = (1 << (2 * (value & 0x03))) * ((value >> 2) & 0x1F);
185 qemu_mod_timer(NVRAM->wd_timer, ((uint64_t)time(NULL) * 1000) +
186 ((interval * 1000) >> 4));
190 /* Direct access to NVRAM */
191 void m48t59_write (m48t59_t *NVRAM, uint32_t val)
196 if (NVRAM->addr > 0x1FF8 && NVRAM->addr < 0x2000)
197 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__, NVRAM->addr, val);
198 switch (NVRAM->addr) {
200 /* flags register : read-only */
207 tmp = fromBCD(val & 0x7F);
208 if (tmp >= 0 && tmp <= 59) {
209 get_alarm(NVRAM, &tm);
211 NVRAM->buffer[0x1FF2] = val;
212 set_alarm(NVRAM, &tm);
217 tmp = fromBCD(val & 0x7F);
218 if (tmp >= 0 && tmp <= 59) {
219 get_alarm(NVRAM, &tm);
221 NVRAM->buffer[0x1FF3] = val;
222 set_alarm(NVRAM, &tm);
227 tmp = fromBCD(val & 0x3F);
228 if (tmp >= 0 && tmp <= 23) {
229 get_alarm(NVRAM, &tm);
231 NVRAM->buffer[0x1FF4] = val;
232 set_alarm(NVRAM, &tm);
237 tmp = fromBCD(val & 0x1F);
239 get_alarm(NVRAM, &tm);
241 NVRAM->buffer[0x1FF5] = val;
242 set_alarm(NVRAM, &tm);
247 NVRAM->buffer[0x1FF6] = val;
251 NVRAM->buffer[0x1FF7] = val;
252 set_up_watchdog(NVRAM, val);
256 NVRAM->buffer[0x1FF8] = (val & ~0xA0) | 0x90;
260 tmp = fromBCD(val & 0x7F);
261 if (tmp >= 0 && tmp <= 59) {
262 get_time(NVRAM, &tm);
264 set_time(NVRAM, &tm);
266 if ((val & 0x80) ^ (NVRAM->buffer[0x1FF9] & 0x80)) {
268 NVRAM->stop_time = time(NULL);
270 NVRAM->time_offset += NVRAM->stop_time - time(NULL);
271 NVRAM->stop_time = 0;
274 NVRAM->buffer[0x1FF9] = val & 0x80;
278 tmp = fromBCD(val & 0x7F);
279 if (tmp >= 0 && tmp <= 59) {
280 get_time(NVRAM, &tm);
282 set_time(NVRAM, &tm);
287 tmp = fromBCD(val & 0x3F);
288 if (tmp >= 0 && tmp <= 23) {
289 get_time(NVRAM, &tm);
291 set_time(NVRAM, &tm);
295 /* day of the week / century */
296 tmp = fromBCD(val & 0x07);
297 get_time(NVRAM, &tm);
299 set_time(NVRAM, &tm);
300 NVRAM->buffer[0x1FFC] = val & 0x40;
304 tmp = fromBCD(val & 0x1F);
306 get_time(NVRAM, &tm);
308 set_time(NVRAM, &tm);
313 tmp = fromBCD(val & 0x1F);
314 if (tmp >= 1 && tmp <= 12) {
315 get_time(NVRAM, &tm);
317 set_time(NVRAM, &tm);
323 if (tmp >= 0 && tmp <= 99) {
324 get_time(NVRAM, &tm);
325 tm.tm_year = fromBCD(val);
326 set_time(NVRAM, &tm);
330 /* Check lock registers state */
331 if (NVRAM->addr >= 0x20 && NVRAM->addr <= 0x2F && (NVRAM->lock & 1))
333 if (NVRAM->addr >= 0x30 && NVRAM->addr <= 0x3F && (NVRAM->lock & 2))
335 if (NVRAM->addr < 0x1FF0 ||
336 (NVRAM->addr > 0x1FFF && NVRAM->addr < NVRAM->size)) {
337 NVRAM->buffer[NVRAM->addr] = val & 0xFF;
343 uint32_t m48t59_read (m48t59_t *NVRAM)
346 uint32_t retval = 0xFF;
348 switch (NVRAM->addr) {
372 /* A read resets the watchdog */
373 set_up_watchdog(NVRAM, NVRAM->buffer[0x1FF7]);
380 get_time(NVRAM, &tm);
381 retval = (NVRAM->buffer[0x1FF9] & 0x80) | toBCD(tm.tm_sec);
385 get_time(NVRAM, &tm);
386 retval = toBCD(tm.tm_min);
390 get_time(NVRAM, &tm);
391 retval = toBCD(tm.tm_hour);
394 /* day of the week / century */
395 get_time(NVRAM, &tm);
396 retval = NVRAM->buffer[0x1FFC] | tm.tm_wday;
400 get_time(NVRAM, &tm);
401 retval = toBCD(tm.tm_mday);
405 get_time(NVRAM, &tm);
406 retval = toBCD(tm.tm_mon + 1);
410 get_time(NVRAM, &tm);
411 retval = toBCD(tm.tm_year);
414 /* Check lock registers state */
415 if (NVRAM->addr >= 0x20 && NVRAM->addr <= 0x2F && (NVRAM->lock & 1))
417 if (NVRAM->addr >= 0x30 && NVRAM->addr <= 0x3F && (NVRAM->lock & 2))
419 if (NVRAM->addr < 0x1FF0 ||
420 (NVRAM->addr > 0x1FFF && NVRAM->addr < NVRAM->size)) {
422 retval = NVRAM->buffer[NVRAM->addr];
426 if (NVRAM->addr > 0x1FF9 && NVRAM->addr < 0x2000)
427 NVRAM_PRINTF("0x%08x <= 0x%08x\n", NVRAM->addr, retval);
432 void m48t59_set_addr (m48t59_t *NVRAM, uint32_t addr)
437 void m48t59_toggle_lock (m48t59_t *NVRAM, int lock)
439 NVRAM->lock ^= 1 << lock;
442 /* IO access to NVRAM */
443 static void NVRAM_writeb (void *opaque, uint32_t addr, uint32_t val)
445 m48t59_t *NVRAM = opaque;
447 addr -= NVRAM->io_base;
448 NVRAM_PRINTF("0x%08x => 0x%08x\n", addr, val);
451 NVRAM->addr &= ~0x00FF;
455 NVRAM->addr &= ~0xFF00;
456 NVRAM->addr |= val << 8;
459 m48t59_write(NVRAM, val);
460 NVRAM->addr = 0x0000;
467 static uint32_t NVRAM_readb (void *opaque, uint32_t addr)
469 m48t59_t *NVRAM = opaque;
472 addr -= NVRAM->io_base;
475 retval = m48t59_read(NVRAM);
481 NVRAM_PRINTF("0x%08x <= 0x%08x\n", addr, retval);
486 static void nvram_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
488 m48t59_t *NVRAM = opaque;
490 addr -= NVRAM->mem_base;
492 NVRAM->buffer[addr] = value;
495 static void nvram_writew (void *opaque, target_phys_addr_t addr, uint32_t value)
497 m48t59_t *NVRAM = opaque;
499 addr -= NVRAM->mem_base;
501 NVRAM->buffer[addr] = value >> 8;
502 NVRAM->buffer[addr + 1] = value;
506 static void nvram_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
508 m48t59_t *NVRAM = opaque;
510 addr -= NVRAM->mem_base;
512 NVRAM->buffer[addr] = value >> 24;
513 NVRAM->buffer[addr + 1] = value >> 16;
514 NVRAM->buffer[addr + 2] = value >> 8;
515 NVRAM->buffer[addr + 3] = value;
519 static uint32_t nvram_readb (void *opaque, target_phys_addr_t addr)
521 m48t59_t *NVRAM = opaque;
524 addr -= NVRAM->mem_base;
526 retval = NVRAM->buffer[addr];
531 static uint32_t nvram_readw (void *opaque, target_phys_addr_t addr)
533 m48t59_t *NVRAM = opaque;
536 addr -= NVRAM->mem_base;
538 retval = NVRAM->buffer[addr] << 8;
539 retval |= NVRAM->buffer[addr + 1];
545 static uint32_t nvram_readl (void *opaque, target_phys_addr_t addr)
547 m48t59_t *NVRAM = opaque;
550 addr -= NVRAM->mem_base;
552 retval = NVRAM->buffer[addr] << 24;
553 retval |= NVRAM->buffer[addr + 1] << 16;
554 retval |= NVRAM->buffer[addr + 2] << 8;
555 retval |= NVRAM->buffer[addr + 3];
561 static CPUWriteMemoryFunc *nvram_write[] = {
567 static CPUReadMemoryFunc *nvram_read[] = {
572 /* Initialisation routine */
573 m48t59_t *m48t59_init (int IRQ, uint32_t mem_base,
574 uint32_t io_base, uint16_t size)
578 s = qemu_mallocz(sizeof(m48t59_t));
581 s->buffer = qemu_mallocz(size);
588 s->mem_base = mem_base;
589 s->io_base = io_base;
591 register_ioport_read(io_base, 0x04, 1, NVRAM_readb, s);
592 register_ioport_write(io_base, 0x04, 1, NVRAM_writeb, s);
594 s->mem_index = cpu_register_io_memory(0, nvram_read, nvram_write, s);
595 cpu_register_physical_memory(mem_base, 0x4000, s->mem_index);
597 s->alrm_timer = qemu_new_timer(vm_clock, &alarm_cb, s);
598 s->wd_timer = qemu_new_timer(vm_clock, &watchdog_cb, s);