]> git.proxmox.com Git - qemu.git/blame - hw/m48t59.c
update VERSION for 1.1.2
[qemu.git] / hw / m48t59.c
CommitLineData
a541f297 1/*
819385c5 2 * QEMU M48T59 and M48T08 NVRAM emulation for PPC PREP and Sparc platforms
5fafdf24 3 *
3ccacc4a 4 * Copyright (c) 2003-2005, 2007 Jocelyn Mayer
5fafdf24 5 *
a541f297
FB
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 */
87ecb68b
PB
24#include "hw.h"
25#include "nvram.h"
87ecb68b
PB
26#include "qemu-timer.h"
27#include "sysemu.h"
d27cf0ae 28#include "sysbus.h"
f80237d4 29#include "isa.h"
a541f297 30
13ab5daa 31//#define DEBUG_NVRAM
a541f297 32
13ab5daa 33#if defined(DEBUG_NVRAM)
001faf32 34#define NVRAM_PRINTF(fmt, ...) do { printf(fmt , ## __VA_ARGS__); } while (0)
a541f297 35#else
001faf32 36#define NVRAM_PRINTF(fmt, ...) do { } while (0)
a541f297
FB
37#endif
38
819385c5 39/*
4aed2c33 40 * The M48T02, M48T08 and M48T59 chips are very similar. The newer '59 has
819385c5
FB
41 * alarm and a watchdog timer and related control registers. In the
42 * PPC platform there is also a nvram lock function.
43 */
930f3fe1
BS
44
45/*
46 * Chipset docs:
47 * http://www.st.com/stonline/products/literature/ds/2410/m48t02.pdf
48 * http://www.st.com/stonline/products/literature/ds/2411/m48t08.pdf
49 * http://www.st.com/stonline/products/literature/od/7001/m48t59y.pdf
50 */
51
43a34704 52struct M48t59State {
a541f297 53 /* Hardware parameters */
d537cf6c 54 qemu_irq IRQ;
5a31cd68 55 MemoryRegion iomem;
a541f297 56 uint32_t io_base;
ee6847d1 57 uint32_t size;
a541f297
FB
58 /* RTC management */
59 time_t time_offset;
60 time_t stop_time;
61 /* Alarm & watchdog */
f6503059 62 struct tm alarm;
a541f297
FB
63 struct QEMUTimer *alrm_timer;
64 struct QEMUTimer *wd_timer;
65 /* NVRAM storage */
a541f297 66 uint8_t *buffer;
42c812b9
BS
67 /* Model parameters */
68 uint32_t type; /* 2 = m48t02, 8 = m48t08, 59 = m48t59 */
69 /* NVRAM storage */
70 uint16_t addr;
71 uint8_t lock;
c5df018e 72};
a541f297 73
f80237d4
BS
74typedef struct M48t59ISAState {
75 ISADevice busdev;
43a34704 76 M48t59State state;
9936d6e4 77 MemoryRegion io;
f80237d4
BS
78} M48t59ISAState;
79
80typedef struct M48t59SysBusState {
81 SysBusDevice busdev;
43a34704 82 M48t59State state;
f80237d4
BS
83} M48t59SysBusState;
84
a541f297 85/* Fake timer functions */
a541f297 86
a541f297
FB
87/* Alarm management */
88static void alarm_cb (void *opaque)
89{
f6503059 90 struct tm tm;
a541f297 91 uint64_t next_time;
43a34704 92 M48t59State *NVRAM = opaque;
a541f297 93
d537cf6c 94 qemu_set_irq(NVRAM->IRQ, 1);
5fafdf24 95 if ((NVRAM->buffer[0x1FF5] & 0x80) == 0 &&
a541f297
FB
96 (NVRAM->buffer[0x1FF4] & 0x80) == 0 &&
97 (NVRAM->buffer[0x1FF3] & 0x80) == 0 &&
98 (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
f6503059
AZ
99 /* Repeat once a month */
100 qemu_get_timedate(&tm, NVRAM->time_offset);
101 tm.tm_mon++;
102 if (tm.tm_mon == 13) {
103 tm.tm_mon = 1;
104 tm.tm_year++;
105 }
106 next_time = qemu_timedate_diff(&tm) - NVRAM->time_offset;
a541f297
FB
107 } else if ((NVRAM->buffer[0x1FF5] & 0x80) != 0 &&
108 (NVRAM->buffer[0x1FF4] & 0x80) == 0 &&
109 (NVRAM->buffer[0x1FF3] & 0x80) == 0 &&
110 (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
f6503059
AZ
111 /* Repeat once a day */
112 next_time = 24 * 60 * 60;
a541f297
FB
113 } else if ((NVRAM->buffer[0x1FF5] & 0x80) != 0 &&
114 (NVRAM->buffer[0x1FF4] & 0x80) != 0 &&
115 (NVRAM->buffer[0x1FF3] & 0x80) == 0 &&
116 (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
f6503059
AZ
117 /* Repeat once an hour */
118 next_time = 60 * 60;
a541f297
FB
119 } else if ((NVRAM->buffer[0x1FF5] & 0x80) != 0 &&
120 (NVRAM->buffer[0x1FF4] & 0x80) != 0 &&
121 (NVRAM->buffer[0x1FF3] & 0x80) != 0 &&
122 (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
f6503059
AZ
123 /* Repeat once a minute */
124 next_time = 60;
a541f297 125 } else {
f6503059
AZ
126 /* Repeat once a second */
127 next_time = 1;
a541f297 128 }
1d849502 129 qemu_mod_timer(NVRAM->alrm_timer, qemu_get_clock_ns(rtc_clock) +
f6503059 130 next_time * 1000);
d537cf6c 131 qemu_set_irq(NVRAM->IRQ, 0);
a541f297
FB
132}
133
43a34704 134static void set_alarm(M48t59State *NVRAM)
f6503059
AZ
135{
136 int diff;
137 if (NVRAM->alrm_timer != NULL) {
138 qemu_del_timer(NVRAM->alrm_timer);
139 diff = qemu_timedate_diff(&NVRAM->alarm) - NVRAM->time_offset;
140 if (diff > 0)
141 qemu_mod_timer(NVRAM->alrm_timer, diff * 1000);
142 }
143}
a541f297 144
f6503059 145/* RTC management helpers */
43a34704 146static inline void get_time(M48t59State *NVRAM, struct tm *tm)
a541f297 147{
f6503059 148 qemu_get_timedate(tm, NVRAM->time_offset);
a541f297
FB
149}
150
43a34704 151static void set_time(M48t59State *NVRAM, struct tm *tm)
a541f297 152{
f6503059
AZ
153 NVRAM->time_offset = qemu_timedate_diff(tm);
154 set_alarm(NVRAM);
a541f297
FB
155}
156
157/* Watchdog management */
158static void watchdog_cb (void *opaque)
159{
43a34704 160 M48t59State *NVRAM = opaque;
a541f297
FB
161
162 NVRAM->buffer[0x1FF0] |= 0x80;
163 if (NVRAM->buffer[0x1FF7] & 0x80) {
164 NVRAM->buffer[0x1FF7] = 0x00;
165 NVRAM->buffer[0x1FFC] &= ~0x40;
13ab5daa 166 /* May it be a hw CPU Reset instead ? */
d7d02e3c 167 qemu_system_reset_request();
a541f297 168 } else {
d537cf6c
PB
169 qemu_set_irq(NVRAM->IRQ, 1);
170 qemu_set_irq(NVRAM->IRQ, 0);
a541f297
FB
171 }
172}
173
43a34704 174static void set_up_watchdog(M48t59State *NVRAM, uint8_t value)
a541f297
FB
175{
176 uint64_t interval; /* in 1/16 seconds */
177
868d585a 178 NVRAM->buffer[0x1FF0] &= ~0x80;
a541f297
FB
179 if (NVRAM->wd_timer != NULL) {
180 qemu_del_timer(NVRAM->wd_timer);
868d585a
JM
181 if (value != 0) {
182 interval = (1 << (2 * (value & 0x03))) * ((value >> 2) & 0x1F);
183 qemu_mod_timer(NVRAM->wd_timer, ((uint64_t)time(NULL) * 1000) +
184 ((interval * 1000) >> 4));
185 }
a541f297
FB
186 }
187}
188
189/* Direct access to NVRAM */
897b4c6c 190void m48t59_write (void *opaque, uint32_t addr, uint32_t val)
a541f297 191{
43a34704 192 M48t59State *NVRAM = opaque;
a541f297
FB
193 struct tm tm;
194 int tmp;
195
819385c5
FB
196 if (addr > 0x1FF8 && addr < 0x2000)
197 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__, addr, val);
4aed2c33
BS
198
199 /* check for NVRAM access */
200 if ((NVRAM->type == 2 && addr < 0x7f8) ||
201 (NVRAM->type == 8 && addr < 0x1ff8) ||
202 (NVRAM->type == 59 && addr < 0x1ff0))
819385c5 203 goto do_write;
4aed2c33
BS
204
205 /* TOD access */
819385c5 206 switch (addr) {
a541f297
FB
207 case 0x1FF0:
208 /* flags register : read-only */
209 break;
210 case 0x1FF1:
211 /* unused */
212 break;
213 case 0x1FF2:
214 /* alarm seconds */
abd0c6bd 215 tmp = from_bcd(val & 0x7F);
819385c5 216 if (tmp >= 0 && tmp <= 59) {
f6503059 217 NVRAM->alarm.tm_sec = tmp;
819385c5 218 NVRAM->buffer[0x1FF2] = val;
f6503059 219 set_alarm(NVRAM);
819385c5 220 }
a541f297
FB
221 break;
222 case 0x1FF3:
223 /* alarm minutes */
abd0c6bd 224 tmp = from_bcd(val & 0x7F);
819385c5 225 if (tmp >= 0 && tmp <= 59) {
f6503059 226 NVRAM->alarm.tm_min = tmp;
819385c5 227 NVRAM->buffer[0x1FF3] = val;
f6503059 228 set_alarm(NVRAM);
819385c5 229 }
a541f297
FB
230 break;
231 case 0x1FF4:
232 /* alarm hours */
abd0c6bd 233 tmp = from_bcd(val & 0x3F);
819385c5 234 if (tmp >= 0 && tmp <= 23) {
f6503059 235 NVRAM->alarm.tm_hour = tmp;
819385c5 236 NVRAM->buffer[0x1FF4] = val;
f6503059 237 set_alarm(NVRAM);
819385c5 238 }
a541f297
FB
239 break;
240 case 0x1FF5:
241 /* alarm date */
02f5da11 242 tmp = from_bcd(val & 0x3F);
819385c5 243 if (tmp != 0) {
f6503059 244 NVRAM->alarm.tm_mday = tmp;
819385c5 245 NVRAM->buffer[0x1FF5] = val;
f6503059 246 set_alarm(NVRAM);
819385c5 247 }
a541f297
FB
248 break;
249 case 0x1FF6:
250 /* interrupts */
819385c5 251 NVRAM->buffer[0x1FF6] = val;
a541f297
FB
252 break;
253 case 0x1FF7:
254 /* watchdog */
819385c5
FB
255 NVRAM->buffer[0x1FF7] = val;
256 set_up_watchdog(NVRAM, val);
a541f297
FB
257 break;
258 case 0x1FF8:
4aed2c33 259 case 0x07F8:
a541f297 260 /* control */
4aed2c33 261 NVRAM->buffer[addr] = (val & ~0xA0) | 0x90;
a541f297
FB
262 break;
263 case 0x1FF9:
4aed2c33 264 case 0x07F9:
a541f297 265 /* seconds (BCD) */
abd0c6bd 266 tmp = from_bcd(val & 0x7F);
a541f297
FB
267 if (tmp >= 0 && tmp <= 59) {
268 get_time(NVRAM, &tm);
269 tm.tm_sec = tmp;
270 set_time(NVRAM, &tm);
271 }
f6503059 272 if ((val & 0x80) ^ (NVRAM->buffer[addr] & 0x80)) {
a541f297
FB
273 if (val & 0x80) {
274 NVRAM->stop_time = time(NULL);
275 } else {
276 NVRAM->time_offset += NVRAM->stop_time - time(NULL);
277 NVRAM->stop_time = 0;
278 }
279 }
f6503059 280 NVRAM->buffer[addr] = val & 0x80;
a541f297
FB
281 break;
282 case 0x1FFA:
4aed2c33 283 case 0x07FA:
a541f297 284 /* minutes (BCD) */
abd0c6bd 285 tmp = from_bcd(val & 0x7F);
a541f297
FB
286 if (tmp >= 0 && tmp <= 59) {
287 get_time(NVRAM, &tm);
288 tm.tm_min = tmp;
289 set_time(NVRAM, &tm);
290 }
291 break;
292 case 0x1FFB:
4aed2c33 293 case 0x07FB:
a541f297 294 /* hours (BCD) */
abd0c6bd 295 tmp = from_bcd(val & 0x3F);
a541f297
FB
296 if (tmp >= 0 && tmp <= 23) {
297 get_time(NVRAM, &tm);
298 tm.tm_hour = tmp;
299 set_time(NVRAM, &tm);
300 }
301 break;
302 case 0x1FFC:
4aed2c33 303 case 0x07FC:
a541f297 304 /* day of the week / century */
abd0c6bd 305 tmp = from_bcd(val & 0x07);
a541f297
FB
306 get_time(NVRAM, &tm);
307 tm.tm_wday = tmp;
308 set_time(NVRAM, &tm);
4aed2c33 309 NVRAM->buffer[addr] = val & 0x40;
a541f297
FB
310 break;
311 case 0x1FFD:
4aed2c33 312 case 0x07FD:
02f5da11
AT
313 /* date (BCD) */
314 tmp = from_bcd(val & 0x3F);
a541f297
FB
315 if (tmp != 0) {
316 get_time(NVRAM, &tm);
317 tm.tm_mday = tmp;
318 set_time(NVRAM, &tm);
319 }
320 break;
321 case 0x1FFE:
4aed2c33 322 case 0x07FE:
a541f297 323 /* month */
abd0c6bd 324 tmp = from_bcd(val & 0x1F);
a541f297
FB
325 if (tmp >= 1 && tmp <= 12) {
326 get_time(NVRAM, &tm);
327 tm.tm_mon = tmp - 1;
328 set_time(NVRAM, &tm);
329 }
330 break;
331 case 0x1FFF:
4aed2c33 332 case 0x07FF:
a541f297 333 /* year */
abd0c6bd 334 tmp = from_bcd(val);
a541f297
FB
335 if (tmp >= 0 && tmp <= 99) {
336 get_time(NVRAM, &tm);
180b700d 337 if (NVRAM->type == 8)
abd0c6bd 338 tm.tm_year = from_bcd(val) + 68; // Base year is 1968
180b700d 339 else
abd0c6bd 340 tm.tm_year = from_bcd(val);
a541f297
FB
341 set_time(NVRAM, &tm);
342 }
343 break;
344 default:
13ab5daa 345 /* Check lock registers state */
819385c5 346 if (addr >= 0x20 && addr <= 0x2F && (NVRAM->lock & 1))
13ab5daa 347 break;
819385c5 348 if (addr >= 0x30 && addr <= 0x3F && (NVRAM->lock & 2))
13ab5daa 349 break;
819385c5
FB
350 do_write:
351 if (addr < NVRAM->size) {
352 NVRAM->buffer[addr] = val & 0xFF;
a541f297
FB
353 }
354 break;
355 }
356}
357
897b4c6c 358uint32_t m48t59_read (void *opaque, uint32_t addr)
a541f297 359{
43a34704 360 M48t59State *NVRAM = opaque;
a541f297
FB
361 struct tm tm;
362 uint32_t retval = 0xFF;
363
4aed2c33
BS
364 /* check for NVRAM access */
365 if ((NVRAM->type == 2 && addr < 0x078f) ||
366 (NVRAM->type == 8 && addr < 0x1ff8) ||
367 (NVRAM->type == 59 && addr < 0x1ff0))
819385c5 368 goto do_read;
4aed2c33
BS
369
370 /* TOD access */
819385c5 371 switch (addr) {
a541f297
FB
372 case 0x1FF0:
373 /* flags register */
374 goto do_read;
375 case 0x1FF1:
376 /* unused */
377 retval = 0;
378 break;
379 case 0x1FF2:
380 /* alarm seconds */
381 goto do_read;
382 case 0x1FF3:
383 /* alarm minutes */
384 goto do_read;
385 case 0x1FF4:
386 /* alarm hours */
387 goto do_read;
388 case 0x1FF5:
389 /* alarm date */
390 goto do_read;
391 case 0x1FF6:
392 /* interrupts */
393 goto do_read;
394 case 0x1FF7:
395 /* A read resets the watchdog */
396 set_up_watchdog(NVRAM, NVRAM->buffer[0x1FF7]);
397 goto do_read;
398 case 0x1FF8:
4aed2c33 399 case 0x07F8:
a541f297
FB
400 /* control */
401 goto do_read;
402 case 0x1FF9:
4aed2c33 403 case 0x07F9:
a541f297
FB
404 /* seconds (BCD) */
405 get_time(NVRAM, &tm);
abd0c6bd 406 retval = (NVRAM->buffer[addr] & 0x80) | to_bcd(tm.tm_sec);
a541f297
FB
407 break;
408 case 0x1FFA:
4aed2c33 409 case 0x07FA:
a541f297
FB
410 /* minutes (BCD) */
411 get_time(NVRAM, &tm);
abd0c6bd 412 retval = to_bcd(tm.tm_min);
a541f297
FB
413 break;
414 case 0x1FFB:
4aed2c33 415 case 0x07FB:
a541f297
FB
416 /* hours (BCD) */
417 get_time(NVRAM, &tm);
abd0c6bd 418 retval = to_bcd(tm.tm_hour);
a541f297
FB
419 break;
420 case 0x1FFC:
4aed2c33 421 case 0x07FC:
a541f297
FB
422 /* day of the week / century */
423 get_time(NVRAM, &tm);
4aed2c33 424 retval = NVRAM->buffer[addr] | tm.tm_wday;
a541f297
FB
425 break;
426 case 0x1FFD:
4aed2c33 427 case 0x07FD:
a541f297
FB
428 /* date */
429 get_time(NVRAM, &tm);
abd0c6bd 430 retval = to_bcd(tm.tm_mday);
a541f297
FB
431 break;
432 case 0x1FFE:
4aed2c33 433 case 0x07FE:
a541f297
FB
434 /* month */
435 get_time(NVRAM, &tm);
abd0c6bd 436 retval = to_bcd(tm.tm_mon + 1);
a541f297
FB
437 break;
438 case 0x1FFF:
4aed2c33 439 case 0x07FF:
a541f297
FB
440 /* year */
441 get_time(NVRAM, &tm);
5fafdf24 442 if (NVRAM->type == 8)
abd0c6bd 443 retval = to_bcd(tm.tm_year - 68); // Base year is 1968
180b700d 444 else
abd0c6bd 445 retval = to_bcd(tm.tm_year);
a541f297
FB
446 break;
447 default:
13ab5daa 448 /* Check lock registers state */
819385c5 449 if (addr >= 0x20 && addr <= 0x2F && (NVRAM->lock & 1))
13ab5daa 450 break;
819385c5 451 if (addr >= 0x30 && addr <= 0x3F && (NVRAM->lock & 2))
13ab5daa 452 break;
819385c5
FB
453 do_read:
454 if (addr < NVRAM->size) {
455 retval = NVRAM->buffer[addr];
a541f297
FB
456 }
457 break;
458 }
819385c5 459 if (addr > 0x1FF9 && addr < 0x2000)
9ed1e667 460 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__, addr, retval);
a541f297
FB
461
462 return retval;
463}
464
897b4c6c 465void m48t59_set_addr (void *opaque, uint32_t addr)
a541f297 466{
43a34704 467 M48t59State *NVRAM = opaque;
897b4c6c 468
a541f297
FB
469 NVRAM->addr = addr;
470}
471
897b4c6c 472void m48t59_toggle_lock (void *opaque, int lock)
13ab5daa 473{
43a34704 474 M48t59State *NVRAM = opaque;
897b4c6c 475
13ab5daa
FB
476 NVRAM->lock ^= 1 << lock;
477}
478
a541f297
FB
479/* IO access to NVRAM */
480static void NVRAM_writeb (void *opaque, uint32_t addr, uint32_t val)
481{
43a34704 482 M48t59State *NVRAM = opaque;
a541f297 483
9ed1e667 484 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__, addr, val);
a541f297
FB
485 switch (addr) {
486 case 0:
487 NVRAM->addr &= ~0x00FF;
488 NVRAM->addr |= val;
489 break;
490 case 1:
491 NVRAM->addr &= ~0xFF00;
492 NVRAM->addr |= val << 8;
493 break;
494 case 3:
b1f88301 495 m48t59_write(NVRAM, NVRAM->addr, val);
a541f297
FB
496 NVRAM->addr = 0x0000;
497 break;
498 default:
499 break;
500 }
501}
502
503static uint32_t NVRAM_readb (void *opaque, uint32_t addr)
504{
43a34704 505 M48t59State *NVRAM = opaque;
13ab5daa 506 uint32_t retval;
a541f297 507
13ab5daa
FB
508 switch (addr) {
509 case 3:
819385c5 510 retval = m48t59_read(NVRAM, NVRAM->addr);
13ab5daa
FB
511 break;
512 default:
513 retval = -1;
514 break;
515 }
9ed1e667 516 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__, addr, retval);
a541f297 517
13ab5daa 518 return retval;
a541f297
FB
519}
520
c227f099 521static void nvram_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
e1bb04f7 522{
43a34704 523 M48t59State *NVRAM = opaque;
3b46e624 524
819385c5 525 m48t59_write(NVRAM, addr, value & 0xff);
e1bb04f7
FB
526}
527
c227f099 528static void nvram_writew (void *opaque, target_phys_addr_t addr, uint32_t value)
e1bb04f7 529{
43a34704 530 M48t59State *NVRAM = opaque;
3b46e624 531
819385c5
FB
532 m48t59_write(NVRAM, addr, (value >> 8) & 0xff);
533 m48t59_write(NVRAM, addr + 1, value & 0xff);
e1bb04f7
FB
534}
535
c227f099 536static void nvram_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
e1bb04f7 537{
43a34704 538 M48t59State *NVRAM = opaque;
3b46e624 539
819385c5
FB
540 m48t59_write(NVRAM, addr, (value >> 24) & 0xff);
541 m48t59_write(NVRAM, addr + 1, (value >> 16) & 0xff);
542 m48t59_write(NVRAM, addr + 2, (value >> 8) & 0xff);
543 m48t59_write(NVRAM, addr + 3, value & 0xff);
e1bb04f7
FB
544}
545
c227f099 546static uint32_t nvram_readb (void *opaque, target_phys_addr_t addr)
e1bb04f7 547{
43a34704 548 M48t59State *NVRAM = opaque;
819385c5 549 uint32_t retval;
3b46e624 550
819385c5 551 retval = m48t59_read(NVRAM, addr);
e1bb04f7
FB
552 return retval;
553}
554
c227f099 555static uint32_t nvram_readw (void *opaque, target_phys_addr_t addr)
e1bb04f7 556{
43a34704 557 M48t59State *NVRAM = opaque;
819385c5 558 uint32_t retval;
3b46e624 559
819385c5
FB
560 retval = m48t59_read(NVRAM, addr) << 8;
561 retval |= m48t59_read(NVRAM, addr + 1);
e1bb04f7
FB
562 return retval;
563}
564
c227f099 565static uint32_t nvram_readl (void *opaque, target_phys_addr_t addr)
e1bb04f7 566{
43a34704 567 M48t59State *NVRAM = opaque;
819385c5 568 uint32_t retval;
e1bb04f7 569
819385c5
FB
570 retval = m48t59_read(NVRAM, addr) << 24;
571 retval |= m48t59_read(NVRAM, addr + 1) << 16;
572 retval |= m48t59_read(NVRAM, addr + 2) << 8;
573 retval |= m48t59_read(NVRAM, addr + 3);
e1bb04f7
FB
574 return retval;
575}
576
5a31cd68
AK
577static const MemoryRegionOps nvram_ops = {
578 .old_mmio = {
579 .read = { nvram_readb, nvram_readw, nvram_readl, },
580 .write = { nvram_writeb, nvram_writew, nvram_writel, },
581 },
582 .endianness = DEVICE_NATIVE_ENDIAN,
e1bb04f7 583};
819385c5 584
fd484ae4
JQ
585static const VMStateDescription vmstate_m48t59 = {
586 .name = "m48t59",
587 .version_id = 1,
588 .minimum_version_id = 1,
589 .minimum_version_id_old = 1,
590 .fields = (VMStateField[]) {
591 VMSTATE_UINT8(lock, M48t59State),
592 VMSTATE_UINT16(addr, M48t59State),
593 VMSTATE_VBUFFER_UINT32(buffer, M48t59State, 0, NULL, 0, size),
594 VMSTATE_END_OF_LIST()
595 }
596};
3ccacc4a 597
43a34704 598static void m48t59_reset_common(M48t59State *NVRAM)
3ccacc4a 599{
6e6b7363
BS
600 NVRAM->addr = 0;
601 NVRAM->lock = 0;
3ccacc4a
BS
602 if (NVRAM->alrm_timer != NULL)
603 qemu_del_timer(NVRAM->alrm_timer);
604
605 if (NVRAM->wd_timer != NULL)
606 qemu_del_timer(NVRAM->wd_timer);
607}
608
285e468d
BS
609static void m48t59_reset_isa(DeviceState *d)
610{
611 M48t59ISAState *isa = container_of(d, M48t59ISAState, busdev.qdev);
43a34704 612 M48t59State *NVRAM = &isa->state;
285e468d
BS
613
614 m48t59_reset_common(NVRAM);
615}
616
617static void m48t59_reset_sysbus(DeviceState *d)
618{
619 M48t59SysBusState *sys = container_of(d, M48t59SysBusState, busdev.qdev);
43a34704 620 M48t59State *NVRAM = &sys->state;
285e468d
BS
621
622 m48t59_reset_common(NVRAM);
623}
624
9936d6e4
RH
625static const MemoryRegionPortio m48t59_portio[] = {
626 {0, 4, 1, .read = NVRAM_readb, .write = NVRAM_writeb },
627 PORTIO_END_OF_LIST(),
628};
629
630static const MemoryRegionOps m48t59_io_ops = {
631 .old_portio = m48t59_portio,
632};
633
a541f297 634/* Initialisation routine */
43a34704
BS
635M48t59State *m48t59_init(qemu_irq IRQ, target_phys_addr_t mem_base,
636 uint32_t io_base, uint16_t size, int type)
a541f297 637{
d27cf0ae
BS
638 DeviceState *dev;
639 SysBusDevice *s;
f80237d4 640 M48t59SysBusState *d;
51f9b84e 641 M48t59State *state;
d27cf0ae
BS
642
643 dev = qdev_create(NULL, "m48t59");
ee6847d1
GH
644 qdev_prop_set_uint32(dev, "type", type);
645 qdev_prop_set_uint32(dev, "size", size);
646 qdev_prop_set_uint32(dev, "io_base", io_base);
e23a1b33 647 qdev_init_nofail(dev);
d27cf0ae 648 s = sysbus_from_qdev(dev);
51f9b84e
HP
649 d = FROM_SYSBUS(M48t59SysBusState, s);
650 state = &d->state;
d27cf0ae 651 sysbus_connect_irq(s, 0, IRQ);
819385c5 652 if (io_base != 0) {
51f9b84e
HP
653 register_ioport_read(io_base, 0x04, 1, NVRAM_readb, state);
654 register_ioport_write(io_base, 0x04, 1, NVRAM_writeb, state);
819385c5 655 }
e1bb04f7 656 if (mem_base != 0) {
d27cf0ae 657 sysbus_mmio_map(s, 0, mem_base);
e1bb04f7 658 }
d27cf0ae 659
51f9b84e 660 return state;
d27cf0ae
BS
661}
662
48a18b3c
HP
663M48t59State *m48t59_init_isa(ISABus *bus, uint32_t io_base, uint16_t size,
664 int type)
d27cf0ae 665{
f80237d4
BS
666 M48t59ISAState *d;
667 ISADevice *dev;
43a34704 668 M48t59State *s;
f80237d4 669
48a18b3c 670 dev = isa_create(bus, "m48t59_isa");
f80237d4
BS
671 qdev_prop_set_uint32(&dev->qdev, "type", type);
672 qdev_prop_set_uint32(&dev->qdev, "size", size);
673 qdev_prop_set_uint32(&dev->qdev, "io_base", io_base);
e23a1b33 674 qdev_init_nofail(&dev->qdev);
f80237d4
BS
675 d = DO_UPCAST(M48t59ISAState, busdev, dev);
676 s = &d->state;
d27cf0ae 677
9936d6e4 678 memory_region_init_io(&d->io, &m48t59_io_ops, s, "m48t59", 4);
f80237d4 679 if (io_base != 0) {
9936d6e4 680 isa_register_ioport(dev, &d->io, io_base);
f80237d4 681 }
d27cf0ae 682
f80237d4
BS
683 return s;
684}
d27cf0ae 685
43a34704 686static void m48t59_init_common(M48t59State *s)
f80237d4 687{
7267c094 688 s->buffer = g_malloc0(s->size);
d27cf0ae 689 if (s->type == 59) {
1d849502 690 s->alrm_timer = qemu_new_timer_ns(rtc_clock, &alarm_cb, s);
74475455 691 s->wd_timer = qemu_new_timer_ns(vm_clock, &watchdog_cb, s);
819385c5 692 }
f6503059 693 qemu_get_timedate(&s->alarm, 0);
13ab5daa 694
fd484ae4 695 vmstate_register(NULL, -1, &vmstate_m48t59, s);
f80237d4
BS
696}
697
698static int m48t59_init_isa1(ISADevice *dev)
699{
700 M48t59ISAState *d = DO_UPCAST(M48t59ISAState, busdev, dev);
43a34704 701 M48t59State *s = &d->state;
f80237d4
BS
702
703 isa_init_irq(dev, &s->IRQ, 8);
704 m48t59_init_common(s);
705
81a322d4 706 return 0;
d27cf0ae 707}
3ccacc4a 708
f80237d4
BS
709static int m48t59_init1(SysBusDevice *dev)
710{
711 M48t59SysBusState *d = FROM_SYSBUS(M48t59SysBusState, dev);
43a34704 712 M48t59State *s = &d->state;
f80237d4
BS
713
714 sysbus_init_irq(dev, &s->IRQ);
715
5a31cd68 716 memory_region_init_io(&s->iomem, &nvram_ops, s, "m48t59.nvram", s->size);
750ecd44 717 sysbus_init_mmio(dev, &s->iomem);
f80237d4
BS
718 m48t59_init_common(s);
719
720 return 0;
721}
722
39bffca2
AL
723static Property m48t59_isa_properties[] = {
724 DEFINE_PROP_UINT32("size", M48t59ISAState, state.size, -1),
725 DEFINE_PROP_UINT32("type", M48t59ISAState, state.type, -1),
726 DEFINE_PROP_HEX32( "io_base", M48t59ISAState, state.io_base, 0),
727 DEFINE_PROP_END_OF_LIST(),
728};
729
8f04ee08
AL
730static void m48t59_init_class_isa1(ObjectClass *klass, void *data)
731{
39bffca2 732 DeviceClass *dc = DEVICE_CLASS(klass);
8f04ee08
AL
733 ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
734 ic->init = m48t59_init_isa1;
39bffca2
AL
735 dc->no_user = 1;
736 dc->reset = m48t59_reset_isa;
737 dc->props = m48t59_isa_properties;
8f04ee08
AL
738}
739
39bffca2
AL
740static TypeInfo m48t59_isa_info = {
741 .name = "m48t59_isa",
742 .parent = TYPE_ISA_DEVICE,
743 .instance_size = sizeof(M48t59ISAState),
744 .class_init = m48t59_init_class_isa1,
f80237d4
BS
745};
746
999e12bb
AL
747static Property m48t59_properties[] = {
748 DEFINE_PROP_UINT32("size", M48t59SysBusState, state.size, -1),
749 DEFINE_PROP_UINT32("type", M48t59SysBusState, state.type, -1),
750 DEFINE_PROP_HEX32( "io_base", M48t59SysBusState, state.io_base, 0),
751 DEFINE_PROP_END_OF_LIST(),
752};
753
754static void m48t59_class_init(ObjectClass *klass, void *data)
755{
39bffca2 756 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb
AL
757 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
758
759 k->init = m48t59_init1;
39bffca2
AL
760 dc->reset = m48t59_reset_sysbus;
761 dc->props = m48t59_properties;
999e12bb
AL
762}
763
39bffca2
AL
764static TypeInfo m48t59_info = {
765 .name = "m48t59",
766 .parent = TYPE_SYS_BUS_DEVICE,
767 .instance_size = sizeof(M48t59SysBusState),
768 .class_init = m48t59_class_init,
ee6847d1
GH
769};
770
83f7d43a 771static void m48t59_register_types(void)
d27cf0ae 772{
39bffca2
AL
773 type_register_static(&m48t59_info);
774 type_register_static(&m48t59_isa_info);
a541f297 775}
d27cf0ae 776
83f7d43a 777type_init(m48t59_register_types)