1 #include <devices/nvram.h>
2 #include <palacios/vmm.h>
3 #include <palacios/vmm_types.h>
8 #define PrintDebug(fmt, args...)
12 #define NVRAM_REG_PORT 0x70
13 #define NVRAM_DATA_PORT 0x71
15 #define NVRAM_RTC_IRQ 0x8
18 typedef enum {NVRAM_READY, NVRAM_REG_POSTED} nvram_state_t;
21 #define NVRAM_REG_MAX 256
24 // These are borrowed from Bochs, which borrowed from
25 // Ralf Brown's interupt list, and extended
26 #define NVRAM_REG_SEC 0x00
27 #define NVRAM_REG_SEC_ALARM 0x01
28 #define NVRAM_REG_MIN 0x02
29 #define NVRAM_REG_MIN_ALARM 0x03
30 #define NVRAM_REG_HOUR 0x04
31 #define NVRAM_REG_HOUR_ALARM 0x05
32 #define NVRAM_REG_WEEK_DAY 0x06
33 #define NVRAM_REG_MONTH_DAY 0x07
34 #define NVRAM_REG_MONTH 0x08
35 #define NVRAM_REG_YEAR 0x09
36 #define NVRAM_REG_STAT_A 0x0a
37 #define NVRAM_REG_STAT_B 0x0b
38 #define NVRAM_REG_STAT_C 0x0c
39 #define NVRAM_REG_STAT_D 0x0d
40 #define NVRAM_REG_DIAGNOSTIC_STATUS 0x0e
41 #define NVRAM_REG_SHUTDOWN_STATUS 0x0f
43 #define NVRAM_IBM_HD_DATA 0x12
45 #define NVRAM_REG_FLOPPY_TYPE 0x10
46 #define NVRAM_REG_EQUIPMENT_BYTE 0x14
48 #define NVRAM_REG_BASE_MEMORY_HIGH 0x16
49 #define NVRAM_REG_BASE_MEMORY_LOW 0x15
51 #define NVRAM_REG_EXT_MEMORY_HIGH 0x18
52 #define NVRAM_REG_EXT_MEMORY_LOW 0x17
54 #define NVRAM_REG_EXT_MEMORY_2ND_HIGH 0x31
55 #define NVRAM_REG_EXT_MEMORY_2ND_LOW 0x30
57 #define NVRAM_REG_BOOTSEQ_OLD 0x2d
59 #define NVRAM_REG_AMI_BIG_MEMORY_HIGH 0x35
60 #define NVRAM_REG_AMI_BIG_MEMORY_LOW 0x34
63 #define NVRAM_REG_CSUM_HIGH 0x2e
64 #define NVRAM_REG_CSUM_LOW 0x2f
65 #define NVRAM_REG_IBM_CENTURY_BYTE 0x32
66 #define NVRAM_REG_IBM_PS2_CENTURY_BYTE 0x37
68 #define NVRAM_REG_BOOTSEQ_NEW_FIRST 0x3D
69 #define NVRAM_REG_BOOTSEQ_NEW_SECOND 0x38
72 struct nvram_internal {
73 nvram_state_t dev_state;
75 uchar_t mem_state[NVRAM_REG_MAX];
77 uint_t us; //microseconds - for clock update - zeroed every second
78 uint_t pus; //microseconds - for periodic interrupt - cleared every period
83 uint_t rate: 4; // clock rate = 65536Hz / 2 rate (0110=1024 Hz)
84 uint_t basis: 3; // time base, 010 = 32,768 Hz
85 uint_t uip: 1; // 1=update in progress
86 } __attribute__((__packed__)) __attribute__((__aligned__ (1))) ;
89 uint_t sum: 1; // 1=summer (daylight savings)
90 uint_t h24: 1; // 1=24h clock
91 uint_t dm: 1; // 1=date/time is in bcd, 0=binary
92 uint_t rec: 1; // 1=rectangular signal
93 uint_t ui: 1; // 1=update interrupt
94 uint_t ai: 1; // 1=alarm interrupt
95 uint_t pi: 1; // 1=periodic interrupt
96 uint_t set: 1; // 1=blocked update
97 } __attribute__((__packed__)) __attribute__((__aligned__ (1))) ;
100 uint_t res: 4; // reserved
101 uint_t uf: 1; // 1=source of interrupt is update
102 uint_t af: 1; // 1=source of interrupt is alarm interrupt
103 uint_t pf: 1; // 1=source of interrupt is periodic interrupt
104 uint_t irq: 1; // 1=interrupt requested
105 } __attribute__((__packed__)) __attribute__((__aligned__ (1))) ;
108 uint_t res: 7; // reserved
109 uint_t val: 1; // 1=cmos ram data is OK
110 } __attribute__((__packed__)) __attribute__((__aligned__ (1))) ;
115 struct vm_device * thedev = NULL;
117 static struct vm_device * demultiplex_timer_interrupt(uint_t period_us)
128 static uchar_t add_to(uchar_t * left, uchar_t * right, uchar_t bcd)
133 struct bcd_num * bl = (struct bcd_num *)left;
134 struct bcd_num * br = (struct bcd_num *)right;
138 carry = bl->bot / 0xa;
141 bl->top += carry + br->top;
142 carry = bl->top / 0xa;
160 static uchar_t days_in_month(struct vm_device *dev, uchar_t month, uchar_t bcd)
162 // This completely ignores Julian / Gregorian stuff right now
219 static void update_time(struct vm_device *dev, uint_t period_us)
221 struct nvram_internal * data = (struct nvram_internal *) (dev->private_data);
222 struct rtc_stata * stata = (struct rtc_stata *) &((data->mem_state[NVRAM_REG_STAT_A]));
223 struct rtc_statb * statb = (struct rtc_statb *) &((data->mem_state[NVRAM_REG_STAT_B]));
224 struct rtc_statc * statc = (struct rtc_statc *) &((data->mem_state[NVRAM_REG_STAT_C]));
225 //struct rtc_statd *statd = (struct rtc_statd *) &((data->mem_state[NVRAM_REG_STAT_D]));
226 uchar_t * sec = (uchar_t *) &(data->mem_state[NVRAM_REG_SEC]);
227 uchar_t * min = (uchar_t *) &(data->mem_state[NVRAM_REG_MIN]);
228 uchar_t * hour = (uchar_t *) &(data->mem_state[NVRAM_REG_HOUR]);
229 uchar_t * weekday = (uchar_t *) &(data->mem_state[NVRAM_REG_WEEK_DAY]);
230 uchar_t * monthday = (uchar_t *) &(data->mem_state[NVRAM_REG_MONTH_DAY]);
231 uchar_t * month = (uchar_t *) &(data->mem_state[NVRAM_REG_MONTH]);
232 uchar_t * year = (uchar_t *) &(data->mem_state[NVRAM_REG_YEAR]);
233 uchar_t * cent = (uchar_t *) &(data->mem_state[NVRAM_REG_IBM_CENTURY_BYTE]);
234 uchar_t * seca = (uchar_t *) &(data->mem_state[NVRAM_REG_SEC_ALARM]);
235 uchar_t * mina = (uchar_t *) &(data->mem_state[NVRAM_REG_MIN_ALARM]);
236 uchar_t * houra = (uchar_t *) &(data->mem_state[NVRAM_REG_HOUR_ALARM]);
239 uchar_t bcd = (statb->dm == 1);
242 uint_t periodic_period;
244 //PrintDebug("nvram: sizeof(struct rtc_stata)=%d\n", sizeof(struct rtc_stata));
247 //PrintDebug("nvram: update_time\n",statb->pi);
249 // We will set these flags on exit
255 // We will reset us after one second
256 data->us += period_us;
257 // We will reset pus after one periodic_period
258 data->pus += period_us;
260 if (data->us > 1000000) {
262 carry = add_to(sec, &carry, bcd);
265 PrintDebug("nvram: somehow managed to get a carry in second update\n");
268 if ( (bcd && (*sec == 0x60)) ||
269 ((!bcd) && (*sec == 60))) {
274 carry = add_to(min, &carry, bcd);
276 PrintDebug("nvram: somehow managed to get a carry in minute update\n");
279 if ( (bcd && (*min == 0x60)) ||
280 ((!bcd) && (*min == 60))) {
289 uchar_t temp = ((bcd) ? 0x12 : 12);
290 add_to(&hour24, &temp, bcd);
295 carry = add_to(&hour24, &carry, bcd);
297 PrintDebug("nvram: somehow managed to get a carry in hour update\n");
300 if ( (bcd && (hour24 == 0x24)) ||
301 ((!bcd) && (hour24 == 24))) {
313 if ( (bcd && (hour24 < 0x12)) ||
314 ((!bcd) && (hour24 < 12))) {
320 *hour = (hour24 - 12) | 0x80;
323 struct bcd_num * n = (struct bcd_num *)hour;
336 // now see if we need to carry into the days and further
339 add_to(weekday, &carry, bcd);
341 *weekday %= 0x7; // same regardless of bcd
343 if ((*monthday) != days_in_month(dev, *month, bcd)) {
344 add_to(monthday, &carry, bcd);
349 add_to(month, &carry, bcd);
351 if ( (bcd && (*month == 0x13)) ||
352 ((!bcd) && (*month == 13))) {
353 *month = 1; // same for both
356 carry = add_to(year, &carry, bcd);
358 if ( (bcd && carry) ||
359 ((!bcd) && (*year == 100))) {
362 add_to(cent, &carry, bcd);
372 // OK, now check for the alarm, if it is set to interrupt
374 if ((*sec == *seca) && (*min == *mina) && (*hour == *houra)) {
376 PrintDebug("nvram: interrupt on alarm\n");
382 periodic_period = 1000000 / (65536 / (0x1 << stata->rate));
383 if (data->pus >= periodic_period) {
385 data->pus -= periodic_period;
386 PrintDebug("nvram: interrupt on periodic\n");
392 PrintDebug("nvram: interrupt on update\n");
395 statc->irq = (statc->pf || statc->af || statc->uf);
397 //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);
399 // Interrupt associated VM, if needed
401 PrintDebug("nvram: injecting interrupt\n");
402 dev->vm->vm_ops.raise_irq(dev->vm, NVRAM_RTC_IRQ);
409 void deliver_timer_interrupt_to_vmm(uint_t period_us)
411 struct vm_device * dev = demultiplex_timer_interrupt(period_us);
414 update_time(dev, period_us);
420 static int set_nvram_defaults(struct vm_device * dev)
422 struct nvram_internal * nvram_state = (struct nvram_internal *)dev->private_data;
426 // 2 1.44 MB floppy drives
429 nvram_state->mem_state[NVRAM_REG_FLOPPY_TYPE] = 0x44;
431 nvram_state->mem_state[NVRAM_REG_FLOPPY_TYPE] = 0x00;
435 // For old boot sequence style, do floppy first
437 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_OLD] = 0x10;
440 // For new boot sequence style, do floppy, cd, then hd
441 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_FIRST] = 0x31;
442 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_SECOND] = 0x20;
445 // For new boot sequence style, do cd, hd, floppy
446 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_FIRST] = 0x23;
447 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_SECOND] = 0x10;
450 // Set equipment byte to note 2 floppies, vga display, keyboard,math,floppy
451 nvram_state->mem_state[NVRAM_REG_EQUIPMENT_BYTE] = 0x4f;
452 //nvram_state->mem_state[NVRAM_REG_EQUIPMENT_BYTE] = 0xf;
454 // Set conventional memory to 640K
455 nvram_state->mem_state[NVRAM_REG_BASE_MEMORY_HIGH] = 0x02;
456 nvram_state->mem_state[NVRAM_REG_BASE_MEMORY_LOW] = 0x80;
458 // Set extended memory to 15 MB
459 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_HIGH] = 0x3C;
460 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_LOW] = 0x00;
461 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_2ND_HIGH]= 0x3C;
462 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_2ND_LOW]= 0x00;
464 // Set the extended memory beyond 16 MB to 128-16 MB
465 nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_HIGH] = 0x7;
466 nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_LOW] = 0x00;
468 //nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_HIGH]= 0x00;
469 //nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_LOW]= 0x00;
472 // This is the harddisk type.... Set accordingly...
473 nvram_state->mem_state[NVRAM_IBM_HD_DATA] = 0x20;
475 // Set the shutdown status gently
477 nvram_state->mem_state[NVRAM_REG_SHUTDOWN_STATUS] = 0x0;
481 // 00100110 = no update in progress, base=32768 Hz, rate = 1024 Hz
482 nvram_state->mem_state[NVRAM_REG_STAT_A] = 0x26;
485 // 00000100 = not setting, no interrupts, blocked rect signal, bcd mode, 24 hour, normal time
486 nvram_state->mem_state[NVRAM_REG_STAT_B] = 0x06;
490 // No IRQ requested, result not do to any source
491 nvram_state->mem_state[NVRAM_REG_STAT_C] = 0x00;
495 nvram_state->mem_state[NVRAM_REG_STAT_D] = 0x80;
498 // january 1, 2008, 00:00:00
499 nvram_state->mem_state[NVRAM_REG_MONTH] = 0x1;
500 nvram_state->mem_state[NVRAM_REG_MONTH_DAY] = 0x1;
501 nvram_state->mem_state[NVRAM_REG_WEEK_DAY] = 0x1;
502 nvram_state->mem_state[NVRAM_REG_YEAR] = 0x08;
505 nvram_state->pus = 0;
512 int nvram_reset_device(struct vm_device * dev)
514 struct nvram_internal * data = (struct nvram_internal *) dev->private_data;
516 PrintDebug("nvram: reset device\n");
519 data->dev_state = NVRAM_READY;
529 int nvram_start_device(struct vm_device *dev)
531 PrintDebug("nvram: start device\n");
536 int nvram_stop_device(struct vm_device *dev)
538 PrintDebug("nvram: stop device\n");
545 int nvram_write_reg_port(ushort_t port,
548 struct vm_device * dev)
550 struct nvram_internal * data = (struct nvram_internal *)dev->private_data;
552 memcpy(&(data->thereg), src, 1);
553 PrintDebug("Writing To NVRAM reg: 0x%x\n", data->thereg);
559 int nvram_read_data_port(ushort_t port,
562 struct vm_device * dev)
564 struct nvram_internal * data = (struct nvram_internal *) dev->private_data;
568 memcpy(dst, &(data->mem_state[data->thereg]), 1);
570 PrintDebug("nvram_read_data_port(0x%x)=0x%x\n", data->thereg, data->mem_state[data->thereg]);
573 if (data->thereg == NVRAM_REG_STAT_A) {
574 data->mem_state[data->thereg] ^= 0x80; // toggle Update in progess
581 int nvram_write_data_port(ushort_t port,
584 struct vm_device * dev)
586 struct nvram_internal * data = (struct nvram_internal *)dev->private_data;
588 memcpy(&(data->mem_state[data->thereg]), src, 1);
590 PrintDebug("nvram_write_data_port(0x%x)=0x%x\n", data->thereg, data->mem_state[data->thereg]);
597 int nvram_init_device(struct vm_device * dev) {
599 struct nvram_internal * data = (struct nvram_internal *) dev->private_data;
601 PrintDebug("nvram: init_device\n");
603 memset(data->mem_state, 0, NVRAM_REG_MAX);
605 // Would read state here
606 set_nvram_defaults(dev);
608 nvram_reset_device(dev);
611 dev_hook_io(dev, NVRAM_REG_PORT, NULL, &nvram_write_reg_port);
612 dev_hook_io(dev, NVRAM_DATA_PORT, &nvram_read_data_port, &nvram_write_data_port);
617 int nvram_deinit_device(struct vm_device *dev)
621 dev_unhook_io(dev, NVRAM_REG_PORT);
622 dev_unhook_io(dev, NVRAM_DATA_PORT);
624 nvram_reset_device(dev);
632 static struct vm_device_ops dev_ops = {
633 .init = nvram_init_device,
634 .deinit = nvram_deinit_device,
635 .reset = nvram_reset_device,
636 .start = nvram_start_device,
637 .stop = nvram_stop_device,
643 struct vm_device * create_nvram() {
644 struct nvram_internal * nvram_state = (struct nvram_internal *)V3_Malloc(sizeof(struct nvram_internal) + 1000);
646 PrintDebug("nvram: internal at %x\n",nvram_state);
648 struct vm_device * device = create_device("NVRAM", &dev_ops, nvram_state);
650 if (thedev != NULL) {
651 PrintDebug("nvram: warning! overwriting thedev\n");