1 /* (c) 2008, Peter Dinda <pdinda@northwestern.edu> */
2 /* (c) 2008, The V3VEE Project <http://www.v3vee.org> */
4 #include <devices/nvram.h>
5 #include <palacios/vmm.h>
6 #include <palacios/vmm_types.h>
11 #define PrintDebug(fmt, args...)
15 #define NVRAM_REG_PORT 0x70
16 #define NVRAM_DATA_PORT 0x71
18 #define NVRAM_RTC_IRQ 0x8
21 typedef enum {NVRAM_READY, NVRAM_REG_POSTED} nvram_state_t;
24 #define NVRAM_REG_MAX 256
27 // These are borrowed from Bochs, which borrowed from
28 // Ralf Brown's interupt list, and extended
29 #define NVRAM_REG_SEC 0x00
30 #define NVRAM_REG_SEC_ALARM 0x01
31 #define NVRAM_REG_MIN 0x02
32 #define NVRAM_REG_MIN_ALARM 0x03
33 #define NVRAM_REG_HOUR 0x04
34 #define NVRAM_REG_HOUR_ALARM 0x05
35 #define NVRAM_REG_WEEK_DAY 0x06
36 #define NVRAM_REG_MONTH_DAY 0x07
37 #define NVRAM_REG_MONTH 0x08
38 #define NVRAM_REG_YEAR 0x09
39 #define NVRAM_REG_STAT_A 0x0a
40 #define NVRAM_REG_STAT_B 0x0b
41 #define NVRAM_REG_STAT_C 0x0c
42 #define NVRAM_REG_STAT_D 0x0d
43 #define NVRAM_REG_DIAGNOSTIC_STATUS 0x0e
44 #define NVRAM_REG_SHUTDOWN_STATUS 0x0f
46 #define NVRAM_IBM_HD_DATA 0x12
48 #define NVRAM_REG_FLOPPY_TYPE 0x10
49 #define NVRAM_REG_EQUIPMENT_BYTE 0x14
51 #define NVRAM_REG_BASE_MEMORY_HIGH 0x16
52 #define NVRAM_REG_BASE_MEMORY_LOW 0x15
54 #define NVRAM_REG_EXT_MEMORY_HIGH 0x18
55 #define NVRAM_REG_EXT_MEMORY_LOW 0x17
57 #define NVRAM_REG_EXT_MEMORY_2ND_HIGH 0x31
58 #define NVRAM_REG_EXT_MEMORY_2ND_LOW 0x30
60 #define NVRAM_REG_BOOTSEQ_OLD 0x2d
62 #define NVRAM_REG_AMI_BIG_MEMORY_HIGH 0x35
63 #define NVRAM_REG_AMI_BIG_MEMORY_LOW 0x34
66 #define NVRAM_REG_CSUM_HIGH 0x2e
67 #define NVRAM_REG_CSUM_LOW 0x2f
68 #define NVRAM_REG_IBM_CENTURY_BYTE 0x32
69 #define NVRAM_REG_IBM_PS2_CENTURY_BYTE 0x37
71 #define NVRAM_REG_BOOTSEQ_NEW_FIRST 0x3D
72 #define NVRAM_REG_BOOTSEQ_NEW_SECOND 0x38
75 struct nvram_internal {
76 nvram_state_t dev_state;
78 uchar_t mem_state[NVRAM_REG_MAX];
80 uint_t us; //microseconds - for clock update - zeroed every second
81 uint_t pus; //microseconds - for periodic interrupt - cleared every period
86 uint_t rate: 4; // clock rate = 65536Hz / 2 rate (0110=1024 Hz)
87 uint_t basis: 3; // time base, 010 = 32,768 Hz
88 uint_t uip: 1; // 1=update in progress
89 } __attribute__((__packed__)) __attribute__((__aligned__ (1))) ;
92 uint_t sum: 1; // 1=summer (daylight savings)
93 uint_t h24: 1; // 1=24h clock
94 uint_t dm: 1; // 1=date/time is in bcd, 0=binary
95 uint_t rec: 1; // 1=rectangular signal
96 uint_t ui: 1; // 1=update interrupt
97 uint_t ai: 1; // 1=alarm interrupt
98 uint_t pi: 1; // 1=periodic interrupt
99 uint_t set: 1; // 1=blocked update
100 } __attribute__((__packed__)) __attribute__((__aligned__ (1))) ;
103 uint_t res: 4; // reserved
104 uint_t uf: 1; // 1=source of interrupt is update
105 uint_t af: 1; // 1=source of interrupt is alarm interrupt
106 uint_t pf: 1; // 1=source of interrupt is periodic interrupt
107 uint_t irq: 1; // 1=interrupt requested
108 } __attribute__((__packed__)) __attribute__((__aligned__ (1))) ;
111 uint_t res: 7; // reserved
112 uint_t val: 1; // 1=cmos ram data is OK
113 } __attribute__((__packed__)) __attribute__((__aligned__ (1))) ;
118 struct vm_device * thedev = NULL;
120 static struct vm_device * demultiplex_timer_interrupt(uint_t period_us)
131 static uchar_t add_to(uchar_t * left, uchar_t * right, uchar_t bcd)
136 struct bcd_num * bl = (struct bcd_num *)left;
137 struct bcd_num * br = (struct bcd_num *)right;
141 carry = bl->bot / 0xa;
144 bl->top += carry + br->top;
145 carry = bl->top / 0xa;
163 static uchar_t days_in_month(struct vm_device *dev, uchar_t month, uchar_t bcd)
165 // This completely ignores Julian / Gregorian stuff right now
222 static void update_time(struct vm_device *dev, uint_t period_us)
224 struct nvram_internal * data = (struct nvram_internal *) (dev->private_data);
225 struct rtc_stata * stata = (struct rtc_stata *) &((data->mem_state[NVRAM_REG_STAT_A]));
226 struct rtc_statb * statb = (struct rtc_statb *) &((data->mem_state[NVRAM_REG_STAT_B]));
227 struct rtc_statc * statc = (struct rtc_statc *) &((data->mem_state[NVRAM_REG_STAT_C]));
228 //struct rtc_statd *statd = (struct rtc_statd *) &((data->mem_state[NVRAM_REG_STAT_D]));
229 uchar_t * sec = (uchar_t *) &(data->mem_state[NVRAM_REG_SEC]);
230 uchar_t * min = (uchar_t *) &(data->mem_state[NVRAM_REG_MIN]);
231 uchar_t * hour = (uchar_t *) &(data->mem_state[NVRAM_REG_HOUR]);
232 uchar_t * weekday = (uchar_t *) &(data->mem_state[NVRAM_REG_WEEK_DAY]);
233 uchar_t * monthday = (uchar_t *) &(data->mem_state[NVRAM_REG_MONTH_DAY]);
234 uchar_t * month = (uchar_t *) &(data->mem_state[NVRAM_REG_MONTH]);
235 uchar_t * year = (uchar_t *) &(data->mem_state[NVRAM_REG_YEAR]);
236 uchar_t * cent = (uchar_t *) &(data->mem_state[NVRAM_REG_IBM_CENTURY_BYTE]);
237 uchar_t * seca = (uchar_t *) &(data->mem_state[NVRAM_REG_SEC_ALARM]);
238 uchar_t * mina = (uchar_t *) &(data->mem_state[NVRAM_REG_MIN_ALARM]);
239 uchar_t * houra = (uchar_t *) &(data->mem_state[NVRAM_REG_HOUR_ALARM]);
242 uchar_t bcd = (statb->dm == 1);
245 uint_t periodic_period;
247 //PrintDebug("nvram: sizeof(struct rtc_stata)=%d\n", sizeof(struct rtc_stata));
250 //PrintDebug("nvram: update_time\n",statb->pi);
252 // We will set these flags on exit
258 // We will reset us after one second
259 data->us += period_us;
260 // We will reset pus after one periodic_period
261 data->pus += period_us;
263 if (data->us > 1000000) {
265 carry = add_to(sec, &carry, bcd);
268 PrintDebug("nvram: somehow managed to get a carry in second update\n");
271 if ( (bcd && (*sec == 0x60)) ||
272 ((!bcd) && (*sec == 60))) {
277 carry = add_to(min, &carry, bcd);
279 PrintDebug("nvram: somehow managed to get a carry in minute update\n");
282 if ( (bcd && (*min == 0x60)) ||
283 ((!bcd) && (*min == 60))) {
292 uchar_t temp = ((bcd) ? 0x12 : 12);
293 add_to(&hour24, &temp, bcd);
298 carry = add_to(&hour24, &carry, bcd);
300 PrintDebug("nvram: somehow managed to get a carry in hour update\n");
303 if ( (bcd && (hour24 == 0x24)) ||
304 ((!bcd) && (hour24 == 24))) {
316 if ( (bcd && (hour24 < 0x12)) ||
317 ((!bcd) && (hour24 < 12))) {
323 *hour = (hour24 - 12) | 0x80;
326 struct bcd_num * n = (struct bcd_num *)hour;
339 // now see if we need to carry into the days and further
342 add_to(weekday, &carry, bcd);
344 *weekday %= 0x7; // same regardless of bcd
346 if ((*monthday) != days_in_month(dev, *month, bcd)) {
347 add_to(monthday, &carry, bcd);
352 add_to(month, &carry, bcd);
354 if ( (bcd && (*month == 0x13)) ||
355 ((!bcd) && (*month == 13))) {
356 *month = 1; // same for both
359 carry = add_to(year, &carry, bcd);
361 if ( (bcd && carry) ||
362 ((!bcd) && (*year == 100))) {
365 add_to(cent, &carry, bcd);
375 // OK, now check for the alarm, if it is set to interrupt
377 if ((*sec == *seca) && (*min == *mina) && (*hour == *houra)) {
379 PrintDebug("nvram: interrupt on alarm\n");
385 periodic_period = 1000000 / (65536 / (0x1 << stata->rate));
386 if (data->pus >= periodic_period) {
388 data->pus -= periodic_period;
389 PrintDebug("nvram: interrupt on periodic\n");
395 PrintDebug("nvram: interrupt on update\n");
398 statc->irq = (statc->pf || statc->af || statc->uf);
400 //PrintDebug("nvram: time is now: YMDHMS: 0x%x:0x%x:0x%x:0x%x:0x%x,0x%x bcd=%d\n", *year, *month, *monthday, *hour, *min, *sec,bcd);
402 // Interrupt associated VM, if needed
404 PrintDebug("nvram: injecting interrupt\n");
405 dev->vm->vm_ops.raise_irq(dev->vm, NVRAM_RTC_IRQ);
412 void deliver_timer_interrupt_to_vmm(uint_t period_us)
414 struct vm_device * dev = demultiplex_timer_interrupt(period_us);
417 update_time(dev, period_us);
423 static int set_nvram_defaults(struct vm_device * dev)
425 struct nvram_internal * nvram_state = (struct nvram_internal *)dev->private_data;
429 // 2 1.44 MB floppy drives
432 nvram_state->mem_state[NVRAM_REG_FLOPPY_TYPE] = 0x44;
434 nvram_state->mem_state[NVRAM_REG_FLOPPY_TYPE] = 0x00;
438 // For old boot sequence style, do floppy first
440 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_OLD] = 0x10;
443 // For new boot sequence style, do floppy, cd, then hd
444 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_FIRST] = 0x31;
445 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_SECOND] = 0x20;
448 // For new boot sequence style, do cd, hd, floppy
449 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_FIRST] = 0x23;
450 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_SECOND] = 0x10;
453 // Set equipment byte to note 2 floppies, vga display, keyboard,math,floppy
454 nvram_state->mem_state[NVRAM_REG_EQUIPMENT_BYTE] = 0x4f;
455 //nvram_state->mem_state[NVRAM_REG_EQUIPMENT_BYTE] = 0xf;
457 // Set conventional memory to 640K
458 nvram_state->mem_state[NVRAM_REG_BASE_MEMORY_HIGH] = 0x02;
459 nvram_state->mem_state[NVRAM_REG_BASE_MEMORY_LOW] = 0x80;
461 // Set extended memory to 15 MB
462 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_HIGH] = 0x3C;
463 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_LOW] = 0x00;
464 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_2ND_HIGH]= 0x3C;
465 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_2ND_LOW]= 0x00;
467 // Set the extended memory beyond 16 MB to 128-16 MB
468 nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_HIGH] = 0x7;
469 nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_LOW] = 0x00;
471 //nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_HIGH]= 0x00;
472 //nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_LOW]= 0x00;
475 // This is the harddisk type.... Set accordingly...
476 nvram_state->mem_state[NVRAM_IBM_HD_DATA] = 0x20;
478 // Set the shutdown status gently
480 nvram_state->mem_state[NVRAM_REG_SHUTDOWN_STATUS] = 0x0;
484 // 00100110 = no update in progress, base=32768 Hz, rate = 1024 Hz
485 nvram_state->mem_state[NVRAM_REG_STAT_A] = 0x26;
488 // 00000100 = not setting, no interrupts, blocked rect signal, bcd mode, 24 hour, normal time
489 nvram_state->mem_state[NVRAM_REG_STAT_B] = 0x06;
493 // No IRQ requested, result not do to any source
494 nvram_state->mem_state[NVRAM_REG_STAT_C] = 0x00;
498 nvram_state->mem_state[NVRAM_REG_STAT_D] = 0x80;
501 // january 1, 2008, 00:00:00
502 nvram_state->mem_state[NVRAM_REG_MONTH] = 0x1;
503 nvram_state->mem_state[NVRAM_REG_MONTH_DAY] = 0x1;
504 nvram_state->mem_state[NVRAM_REG_WEEK_DAY] = 0x1;
505 nvram_state->mem_state[NVRAM_REG_YEAR] = 0x08;
508 nvram_state->pus = 0;
515 int nvram_reset_device(struct vm_device * dev)
517 struct nvram_internal * data = (struct nvram_internal *) dev->private_data;
519 PrintDebug("nvram: reset device\n");
522 data->dev_state = NVRAM_READY;
532 int nvram_start_device(struct vm_device *dev)
534 PrintDebug("nvram: start device\n");
539 int nvram_stop_device(struct vm_device *dev)
541 PrintDebug("nvram: stop device\n");
548 int nvram_write_reg_port(ushort_t port,
551 struct vm_device * dev)
553 struct nvram_internal * data = (struct nvram_internal *)dev->private_data;
555 memcpy(&(data->thereg), src, 1);
556 PrintDebug("Writing To NVRAM reg: 0x%x\n", data->thereg);
562 int nvram_read_data_port(ushort_t port,
565 struct vm_device * dev)
567 struct nvram_internal * data = (struct nvram_internal *) dev->private_data;
571 memcpy(dst, &(data->mem_state[data->thereg]), 1);
573 PrintDebug("nvram_read_data_port(0x%x)=0x%x\n", data->thereg, data->mem_state[data->thereg]);
576 if (data->thereg == NVRAM_REG_STAT_A) {
577 data->mem_state[data->thereg] ^= 0x80; // toggle Update in progess
584 int nvram_write_data_port(ushort_t port,
587 struct vm_device * dev)
589 struct nvram_internal * data = (struct nvram_internal *)dev->private_data;
591 memcpy(&(data->mem_state[data->thereg]), src, 1);
593 PrintDebug("nvram_write_data_port(0x%x)=0x%x\n", data->thereg, data->mem_state[data->thereg]);
600 int nvram_init_device(struct vm_device * dev) {
602 struct nvram_internal * data = (struct nvram_internal *) dev->private_data;
604 PrintDebug("nvram: init_device\n");
606 memset(data->mem_state, 0, NVRAM_REG_MAX);
608 // Would read state here
609 set_nvram_defaults(dev);
611 nvram_reset_device(dev);
614 dev_hook_io(dev, NVRAM_REG_PORT, NULL, &nvram_write_reg_port);
615 dev_hook_io(dev, NVRAM_DATA_PORT, &nvram_read_data_port, &nvram_write_data_port);
620 int nvram_deinit_device(struct vm_device *dev)
624 dev_unhook_io(dev, NVRAM_REG_PORT);
625 dev_unhook_io(dev, NVRAM_DATA_PORT);
627 nvram_reset_device(dev);
635 static struct vm_device_ops dev_ops = {
636 .init = nvram_init_device,
637 .deinit = nvram_deinit_device,
638 .reset = nvram_reset_device,
639 .start = nvram_start_device,
640 .stop = nvram_stop_device,
646 struct vm_device * create_nvram() {
647 struct nvram_internal * nvram_state = (struct nvram_internal *)V3_Malloc(sizeof(struct nvram_internal) + 1000);
649 PrintDebug("nvram: internal at %x\n",nvram_state);
651 struct vm_device * device = create_device("NVRAM", &dev_ops, nvram_state);
653 if (thedev != NULL) {
654 PrintDebug("nvram: warning! overwriting thedev\n");