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