root / hw / m48t59.c @ a541f297
History | View | Annotate | Download (12 kB)
1 |
/*
|
---|---|
2 |
* QEMU M48T59 NVRAM emulation for PPC PREP platform
|
3 |
*
|
4 |
* Copyright (c) 2003-2004 Jocelyn Mayer
|
5 |
*
|
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 |
*/
|
24 |
|
25 |
#include <stdlib.h> |
26 |
#include <stdio.h> /* needed by vl.h */ |
27 |
#include <stdint.h> |
28 |
#include <string.h> |
29 |
#include <time.h> |
30 |
|
31 |
#include "vl.h" |
32 |
|
33 |
//#define NVRAM_DEBUG
|
34 |
|
35 |
#if defined(NVRAM_DEBUG)
|
36 |
#define NVRAM_PRINTF(fmt, args...) do { printf(fmt , ##args); } while (0) |
37 |
#else
|
38 |
#define NVRAM_PRINTF(fmt, args...) do { } while (0) |
39 |
#endif
|
40 |
|
41 |
typedef struct m48t59_t { |
42 |
/* Hardware parameters */
|
43 |
int IRQ;
|
44 |
uint32_t io_base; |
45 |
uint16_t size; |
46 |
/* RTC management */
|
47 |
time_t time_offset; |
48 |
time_t stop_time; |
49 |
/* Alarm & watchdog */
|
50 |
time_t alarm; |
51 |
struct QEMUTimer *alrm_timer;
|
52 |
struct QEMUTimer *wd_timer;
|
53 |
/* NVRAM storage */
|
54 |
uint16_t addr; |
55 |
uint8_t *buffer; |
56 |
} m48t59_t; |
57 |
|
58 |
static m48t59_t *NVRAMs;
|
59 |
static int nb_NVRAMs; |
60 |
|
61 |
/* Fake timer functions */
|
62 |
/* Generic helpers for BCD */
|
63 |
static inline uint8_t toBCD (uint8_t value) |
64 |
{ |
65 |
return (((value / 10) % 10) << 4) | (value % 10); |
66 |
} |
67 |
|
68 |
static inline uint8_t fromBCD (uint8_t BCD) |
69 |
{ |
70 |
return ((BCD >> 4) * 10) + (BCD & 0x0F); |
71 |
} |
72 |
|
73 |
/* RTC management helpers */
|
74 |
static void get_time (m48t59_t *NVRAM, struct tm *tm) |
75 |
{ |
76 |
time_t t; |
77 |
|
78 |
t = time(NULL) + NVRAM->time_offset;
|
79 |
localtime_r(&t, tm); |
80 |
} |
81 |
|
82 |
static void set_time (m48t59_t *NVRAM, struct tm *tm) |
83 |
{ |
84 |
time_t now, new_time; |
85 |
|
86 |
new_time = mktime(tm); |
87 |
now = time(NULL);
|
88 |
NVRAM->time_offset = new_time - now; |
89 |
} |
90 |
|
91 |
/* Alarm management */
|
92 |
static void alarm_cb (void *opaque) |
93 |
{ |
94 |
struct tm tm, tm_now;
|
95 |
uint64_t next_time; |
96 |
m48t59_t *NVRAM = opaque; |
97 |
|
98 |
pic_set_irq(NVRAM->IRQ, 1);
|
99 |
if ((NVRAM->buffer[0x1FF5] & 0x80) == 0 && |
100 |
(NVRAM->buffer[0x1FF4] & 0x80) == 0 && |
101 |
(NVRAM->buffer[0x1FF3] & 0x80) == 0 && |
102 |
(NVRAM->buffer[0x1FF2] & 0x80) == 0) { |
103 |
/* Repeat once a month */
|
104 |
get_time(NVRAM, &tm_now); |
105 |
memcpy(&tm, &tm_now, sizeof(struct tm)); |
106 |
tm.tm_mon++; |
107 |
if (tm.tm_mon == 13) { |
108 |
tm.tm_mon = 1;
|
109 |
tm.tm_year++; |
110 |
} |
111 |
next_time = mktime(&tm); |
112 |
} else if ((NVRAM->buffer[0x1FF5] & 0x80) != 0 && |
113 |
(NVRAM->buffer[0x1FF4] & 0x80) == 0 && |
114 |
(NVRAM->buffer[0x1FF3] & 0x80) == 0 && |
115 |
(NVRAM->buffer[0x1FF2] & 0x80) == 0) { |
116 |
/* Repeat once a day */
|
117 |
next_time = 24 * 60 * 60 + mktime(&tm_now); |
118 |
} else if ((NVRAM->buffer[0x1FF5] & 0x80) != 0 && |
119 |
(NVRAM->buffer[0x1FF4] & 0x80) != 0 && |
120 |
(NVRAM->buffer[0x1FF3] & 0x80) == 0 && |
121 |
(NVRAM->buffer[0x1FF2] & 0x80) == 0) { |
122 |
/* Repeat once an hour */
|
123 |
next_time = 60 * 60 + mktime(&tm_now); |
124 |
} else if ((NVRAM->buffer[0x1FF5] & 0x80) != 0 && |
125 |
(NVRAM->buffer[0x1FF4] & 0x80) != 0 && |
126 |
(NVRAM->buffer[0x1FF3] & 0x80) != 0 && |
127 |
(NVRAM->buffer[0x1FF2] & 0x80) == 0) { |
128 |
/* Repeat once a minute */
|
129 |
next_time = 60 + mktime(&tm_now);
|
130 |
} else {
|
131 |
/* Repeat once a second */
|
132 |
next_time = 1 + mktime(&tm_now);
|
133 |
} |
134 |
qemu_mod_timer(NVRAM->alrm_timer, next_time * 1000);
|
135 |
pic_set_irq(NVRAM->IRQ, 0);
|
136 |
} |
137 |
|
138 |
|
139 |
static void get_alarm (m48t59_t *NVRAM, struct tm *tm) |
140 |
{ |
141 |
localtime_r(&NVRAM->alarm, tm); |
142 |
} |
143 |
|
144 |
static void set_alarm (m48t59_t *NVRAM, struct tm *tm) |
145 |
{ |
146 |
NVRAM->alarm = mktime(tm); |
147 |
if (NVRAM->alrm_timer != NULL) { |
148 |
qemu_del_timer(NVRAM->alrm_timer); |
149 |
NVRAM->alrm_timer = NULL;
|
150 |
} |
151 |
if (NVRAM->alarm - time(NULL) > 0) |
152 |
qemu_mod_timer(NVRAM->alrm_timer, NVRAM->alarm * 1000);
|
153 |
} |
154 |
|
155 |
/* Watchdog management */
|
156 |
static void watchdog_cb (void *opaque) |
157 |
{ |
158 |
m48t59_t *NVRAM = opaque; |
159 |
|
160 |
NVRAM->buffer[0x1FF0] |= 0x80; |
161 |
if (NVRAM->buffer[0x1FF7] & 0x80) { |
162 |
NVRAM->buffer[0x1FF7] = 0x00; |
163 |
NVRAM->buffer[0x1FFC] &= ~0x40; |
164 |
// reset_CPU();
|
165 |
} else {
|
166 |
pic_set_irq(NVRAM->IRQ, 1);
|
167 |
pic_set_irq(NVRAM->IRQ, 0);
|
168 |
} |
169 |
} |
170 |
|
171 |
static void set_up_watchdog (m48t59_t *NVRAM, uint8_t value) |
172 |
{ |
173 |
uint64_t interval; /* in 1/16 seconds */
|
174 |
|
175 |
if (NVRAM->wd_timer != NULL) { |
176 |
qemu_del_timer(NVRAM->wd_timer); |
177 |
NVRAM->wd_timer = NULL;
|
178 |
} |
179 |
NVRAM->buffer[0x1FF0] &= ~0x80; |
180 |
if (value != 0) { |
181 |
interval = (1 << (2 * (value & 0x03))) * ((value >> 2) & 0x1F); |
182 |
qemu_mod_timer(NVRAM->wd_timer, ((uint64_t)time(NULL) * 1000) + |
183 |
((interval * 1000) >> 4)); |
184 |
} |
185 |
} |
186 |
|
187 |
/* Direct access to NVRAM */
|
188 |
void m48t59_write (void *opaque, uint32_t val) |
189 |
{ |
190 |
m48t59_t *NVRAM = opaque; |
191 |
struct tm tm;
|
192 |
int tmp;
|
193 |
|
194 |
if (NVRAM->addr > 0x1FF8 && NVRAM->addr < 0x2000) |
195 |
NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__, NVRAM->addr, val);
|
196 |
switch (NVRAM->addr) {
|
197 |
case 0x1FF0: |
198 |
/* flags register : read-only */
|
199 |
break;
|
200 |
case 0x1FF1: |
201 |
/* unused */
|
202 |
break;
|
203 |
case 0x1FF2: |
204 |
/* alarm seconds */
|
205 |
tmp = fromBCD(val & 0x7F);
|
206 |
if (tmp >= 0 && tmp <= 59) { |
207 |
get_alarm(NVRAM, &tm); |
208 |
tm.tm_sec = tmp; |
209 |
NVRAM->buffer[0x1FF2] = val;
|
210 |
set_alarm(NVRAM, &tm); |
211 |
} |
212 |
break;
|
213 |
case 0x1FF3: |
214 |
/* alarm minutes */
|
215 |
tmp = fromBCD(val & 0x7F);
|
216 |
if (tmp >= 0 && tmp <= 59) { |
217 |
get_alarm(NVRAM, &tm); |
218 |
tm.tm_min = tmp; |
219 |
NVRAM->buffer[0x1FF3] = val;
|
220 |
set_alarm(NVRAM, &tm); |
221 |
} |
222 |
break;
|
223 |
case 0x1FF4: |
224 |
/* alarm hours */
|
225 |
tmp = fromBCD(val & 0x3F);
|
226 |
if (tmp >= 0 && tmp <= 23) { |
227 |
get_alarm(NVRAM, &tm); |
228 |
tm.tm_hour = tmp; |
229 |
NVRAM->buffer[0x1FF4] = val;
|
230 |
set_alarm(NVRAM, &tm); |
231 |
} |
232 |
break;
|
233 |
case 0x1FF5: |
234 |
/* alarm date */
|
235 |
tmp = fromBCD(val & 0x1F);
|
236 |
if (tmp != 0) { |
237 |
get_alarm(NVRAM, &tm); |
238 |
tm.tm_mday = tmp; |
239 |
NVRAM->buffer[0x1FF5] = val;
|
240 |
set_alarm(NVRAM, &tm); |
241 |
} |
242 |
break;
|
243 |
case 0x1FF6: |
244 |
/* interrupts */
|
245 |
NVRAM->buffer[0x1FF6] = val;
|
246 |
break;
|
247 |
case 0x1FF7: |
248 |
/* watchdog */
|
249 |
NVRAM->buffer[0x1FF7] = val;
|
250 |
set_up_watchdog(NVRAM, val); |
251 |
break;
|
252 |
case 0x1FF8: |
253 |
/* control */
|
254 |
NVRAM->buffer[0x1FF8] = (val & ~0xA0) | 0x90; |
255 |
break;
|
256 |
case 0x1FF9: |
257 |
/* seconds (BCD) */
|
258 |
tmp = fromBCD(val & 0x7F);
|
259 |
if (tmp >= 0 && tmp <= 59) { |
260 |
get_time(NVRAM, &tm); |
261 |
tm.tm_sec = tmp; |
262 |
set_time(NVRAM, &tm); |
263 |
} |
264 |
if ((val & 0x80) ^ (NVRAM->buffer[0x1FF9] & 0x80)) { |
265 |
if (val & 0x80) { |
266 |
NVRAM->stop_time = time(NULL);
|
267 |
} else {
|
268 |
NVRAM->time_offset += NVRAM->stop_time - time(NULL);
|
269 |
NVRAM->stop_time = 0;
|
270 |
} |
271 |
} |
272 |
NVRAM->buffer[0x1FF9] = val & 0x80; |
273 |
break;
|
274 |
case 0x1FFA: |
275 |
/* minutes (BCD) */
|
276 |
tmp = fromBCD(val & 0x7F);
|
277 |
if (tmp >= 0 && tmp <= 59) { |
278 |
get_time(NVRAM, &tm); |
279 |
tm.tm_min = tmp; |
280 |
set_time(NVRAM, &tm); |
281 |
} |
282 |
break;
|
283 |
case 0x1FFB: |
284 |
/* hours (BCD) */
|
285 |
tmp = fromBCD(val & 0x3F);
|
286 |
if (tmp >= 0 && tmp <= 23) { |
287 |
get_time(NVRAM, &tm); |
288 |
tm.tm_hour = tmp; |
289 |
set_time(NVRAM, &tm); |
290 |
} |
291 |
break;
|
292 |
case 0x1FFC: |
293 |
/* day of the week / century */
|
294 |
tmp = fromBCD(val & 0x07);
|
295 |
get_time(NVRAM, &tm); |
296 |
tm.tm_wday = tmp; |
297 |
set_time(NVRAM, &tm); |
298 |
NVRAM->buffer[0x1FFC] = val & 0x40; |
299 |
break;
|
300 |
case 0x1FFD: |
301 |
/* date */
|
302 |
tmp = fromBCD(val & 0x1F);
|
303 |
if (tmp != 0) { |
304 |
get_time(NVRAM, &tm); |
305 |
tm.tm_mday = tmp; |
306 |
set_time(NVRAM, &tm); |
307 |
} |
308 |
break;
|
309 |
case 0x1FFE: |
310 |
/* month */
|
311 |
tmp = fromBCD(val & 0x1F);
|
312 |
if (tmp >= 1 && tmp <= 12) { |
313 |
get_time(NVRAM, &tm); |
314 |
tm.tm_mon = tmp - 1;
|
315 |
set_time(NVRAM, &tm); |
316 |
} |
317 |
break;
|
318 |
case 0x1FFF: |
319 |
/* year */
|
320 |
tmp = fromBCD(val); |
321 |
if (tmp >= 0 && tmp <= 99) { |
322 |
get_time(NVRAM, &tm); |
323 |
tm.tm_year = fromBCD(val); |
324 |
set_time(NVRAM, &tm); |
325 |
} |
326 |
break;
|
327 |
default:
|
328 |
if (NVRAM->addr < 0x1FF0 || |
329 |
(NVRAM->addr > 0x1FFF && NVRAM->addr < NVRAM->size)) {
|
330 |
NVRAM->buffer[NVRAM->addr] = val & 0xFF;
|
331 |
} |
332 |
break;
|
333 |
} |
334 |
} |
335 |
|
336 |
uint32_t m48t59_read (void *opaque)
|
337 |
{ |
338 |
m48t59_t *NVRAM = opaque; |
339 |
struct tm tm;
|
340 |
uint32_t retval = 0xFF;
|
341 |
|
342 |
switch (NVRAM->addr) {
|
343 |
case 0x1FF0: |
344 |
/* flags register */
|
345 |
goto do_read;
|
346 |
case 0x1FF1: |
347 |
/* unused */
|
348 |
retval = 0;
|
349 |
break;
|
350 |
case 0x1FF2: |
351 |
/* alarm seconds */
|
352 |
goto do_read;
|
353 |
case 0x1FF3: |
354 |
/* alarm minutes */
|
355 |
goto do_read;
|
356 |
case 0x1FF4: |
357 |
/* alarm hours */
|
358 |
goto do_read;
|
359 |
case 0x1FF5: |
360 |
/* alarm date */
|
361 |
goto do_read;
|
362 |
case 0x1FF6: |
363 |
/* interrupts */
|
364 |
goto do_read;
|
365 |
case 0x1FF7: |
366 |
/* A read resets the watchdog */
|
367 |
set_up_watchdog(NVRAM, NVRAM->buffer[0x1FF7]);
|
368 |
goto do_read;
|
369 |
case 0x1FF8: |
370 |
/* control */
|
371 |
goto do_read;
|
372 |
case 0x1FF9: |
373 |
/* seconds (BCD) */
|
374 |
get_time(NVRAM, &tm); |
375 |
retval = (NVRAM->buffer[0x1FF9] & 0x80) | toBCD(tm.tm_sec); |
376 |
break;
|
377 |
case 0x1FFA: |
378 |
/* minutes (BCD) */
|
379 |
get_time(NVRAM, &tm); |
380 |
retval = toBCD(tm.tm_min); |
381 |
break;
|
382 |
case 0x1FFB: |
383 |
/* hours (BCD) */
|
384 |
get_time(NVRAM, &tm); |
385 |
retval = toBCD(tm.tm_hour); |
386 |
break;
|
387 |
case 0x1FFC: |
388 |
/* day of the week / century */
|
389 |
get_time(NVRAM, &tm); |
390 |
retval = NVRAM->buffer[0x1FFC] | tm.tm_wday;
|
391 |
break;
|
392 |
case 0x1FFD: |
393 |
/* date */
|
394 |
get_time(NVRAM, &tm); |
395 |
retval = toBCD(tm.tm_mday); |
396 |
break;
|
397 |
case 0x1FFE: |
398 |
/* month */
|
399 |
get_time(NVRAM, &tm); |
400 |
retval = toBCD(tm.tm_mon + 1);
|
401 |
break;
|
402 |
case 0x1FFF: |
403 |
/* year */
|
404 |
get_time(NVRAM, &tm); |
405 |
retval = toBCD(tm.tm_year); |
406 |
break;
|
407 |
default:
|
408 |
if (NVRAM->addr < 0x1FF0 || |
409 |
(NVRAM->addr > 0x1FFF && NVRAM->addr < NVRAM->size)) {
|
410 |
do_read:
|
411 |
retval = NVRAM->buffer[NVRAM->addr]; |
412 |
} |
413 |
break;
|
414 |
} |
415 |
if (NVRAM->addr > 0x1FF9 && NVRAM->addr < 0x2000) |
416 |
NVRAM_PRINTF("0x%08x <= 0x%08x\n", NVRAM->addr, retval);
|
417 |
|
418 |
return retval;
|
419 |
} |
420 |
|
421 |
void m48t59_set_addr (void *opaque, uint32_t addr) |
422 |
{ |
423 |
m48t59_t *NVRAM = opaque; |
424 |
|
425 |
NVRAM->addr = addr; |
426 |
} |
427 |
|
428 |
/* IO access to NVRAM */
|
429 |
static void NVRAM_writeb (void *opaque, uint32_t addr, uint32_t val) |
430 |
{ |
431 |
m48t59_t *NVRAM = opaque; |
432 |
|
433 |
addr -= NVRAM->io_base; |
434 |
switch (addr) {
|
435 |
case 0: |
436 |
NVRAM->addr &= ~0x00FF;
|
437 |
NVRAM->addr |= val; |
438 |
break;
|
439 |
case 1: |
440 |
NVRAM->addr &= ~0xFF00;
|
441 |
NVRAM->addr |= val << 8;
|
442 |
break;
|
443 |
case 3: |
444 |
m48t59_write(NVRAM, val); |
445 |
NVRAM->addr = 0x0000;
|
446 |
break;
|
447 |
default:
|
448 |
break;
|
449 |
} |
450 |
} |
451 |
|
452 |
static uint32_t NVRAM_readb (void *opaque, uint32_t addr) |
453 |
{ |
454 |
m48t59_t *NVRAM = opaque; |
455 |
|
456 |
if (addr == NVRAM->io_base + 3) |
457 |
return m48t59_read(NVRAM);
|
458 |
|
459 |
return 0xFF; |
460 |
} |
461 |
|
462 |
/* Initialisation routine */
|
463 |
void *m48t59_init (int IRQ, uint32_t io_base, uint16_t size) |
464 |
{ |
465 |
m48t59_t *tmp; |
466 |
|
467 |
tmp = realloc(NVRAMs, (nb_NVRAMs + 1) * sizeof(m48t59_t)); |
468 |
if (tmp == NULL) |
469 |
return NULL; |
470 |
NVRAMs = tmp; |
471 |
tmp[nb_NVRAMs].buffer = malloc(size); |
472 |
if (tmp[nb_NVRAMs].buffer == NULL) |
473 |
return NULL; |
474 |
memset(tmp[nb_NVRAMs].buffer, 0, size);
|
475 |
tmp[nb_NVRAMs].IRQ = IRQ; |
476 |
tmp[nb_NVRAMs].size = size; |
477 |
tmp[nb_NVRAMs].io_base = io_base; |
478 |
tmp[nb_NVRAMs].addr = 0;
|
479 |
register_ioport_read(io_base, 0x04, 1, NVRAM_readb, &NVRAMs[nb_NVRAMs]); |
480 |
register_ioport_write(io_base, 0x04, 1, NVRAM_writeb, &NVRAMs[nb_NVRAMs]); |
481 |
tmp[nb_NVRAMs].alrm_timer = qemu_new_timer(vm_clock, &alarm_cb, |
482 |
&tmp[nb_NVRAMs]); |
483 |
tmp[nb_NVRAMs].wd_timer = qemu_new_timer(vm_clock, &watchdog_cb, |
484 |
&tmp[nb_NVRAMs]); |
485 |
return &NVRAMs[nb_NVRAMs++];
|
486 |
} |