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;
118 static struct vm_device *demultiplex_timer_interrupt(uint_t period_us)
129 static uchar_t add_to(uchar_t *left, uchar_t *right, uchar_t bcd)
134 struct bcd_num *bl = (struct bcd_num *)left;
135 struct bcd_num *br = (struct bcd_num *)right;
139 carry = bl->bot / 0xa;
141 bl->top += carry + br->top;
142 carry = bl->top / 0xa;
158 static uchar_t days_in_month(struct vm_device *dev, uchar_t month, uchar_t bcd)
160 // This completely ignores Julian / Gregorian stuff right now
212 static void update_time(struct vm_device *dev, uint_t period_us)
214 struct nvram_internal *data = (struct nvram_internal *) (dev->private_data);
215 struct rtc_stata *stata = (struct rtc_stata *) &((data->mem_state[NVRAM_REG_STAT_A]));
216 struct rtc_statb *statb = (struct rtc_statb *) &((data->mem_state[NVRAM_REG_STAT_B]));
217 struct rtc_statc *statc = (struct rtc_statc *) &((data->mem_state[NVRAM_REG_STAT_C]));
218 //struct rtc_statd *statd = (struct rtc_statd *) &((data->mem_state[NVRAM_REG_STAT_D]));
219 uchar_t *sec = (uchar_t *) &(data->mem_state[NVRAM_REG_SEC]);
220 uchar_t *min = (uchar_t *) &(data->mem_state[NVRAM_REG_MIN]);
221 uchar_t *hour = (uchar_t *) &(data->mem_state[NVRAM_REG_HOUR]);
222 uchar_t *weekday = (uchar_t *) &(data->mem_state[NVRAM_REG_WEEK_DAY]);
223 uchar_t *monthday = (uchar_t *) &(data->mem_state[NVRAM_REG_MONTH_DAY]);
224 uchar_t *month = (uchar_t *) &(data->mem_state[NVRAM_REG_MONTH]);
225 uchar_t *year = (uchar_t *) &(data->mem_state[NVRAM_REG_YEAR]);
226 uchar_t *cent = (uchar_t *) &(data->mem_state[NVRAM_REG_IBM_CENTURY_BYTE]);
227 uchar_t *seca = (uchar_t *) &(data->mem_state[NVRAM_REG_SEC_ALARM]);
228 uchar_t *mina = (uchar_t *) &(data->mem_state[NVRAM_REG_MIN_ALARM]);
229 uchar_t *houra = (uchar_t *) &(data->mem_state[NVRAM_REG_HOUR_ALARM]);
232 uchar_t bcd = (statb->dm==1);
235 uint_t periodic_period;
237 //PrintDebug("nvram: sizeof(struct rtc_stata)=%d\n", sizeof(struct rtc_stata));
240 //PrintDebug("nvram: update_time\n",statb->pi);
242 // We will set these flags on exit
243 statc->irq=statc->pf=statc->af=statc->uf=0;
245 // We will reset us after one second
246 data->us += period_us;
247 // We will reset pus after one periodic_period
248 data->pus += period_us;
250 if (data->us > 1000000) {
252 //PrintDebug("nvram: adding 1 to seconds=0x%x (bcd=%d)...", *sec, bcd);
253 carry=add_to(sec,&carry,bcd);
254 //PrintDebug("got 0x%x with carry=%d\n",*sec,carry);
255 if (carry) { PrintDebug("nvram: somehow managed to get a carry in second update\n"); }
256 if ((bcd && *sec==0x60) || (!bcd && *sec==60)) {
259 carry=add_to(min,&carry,bcd);
260 if (carry) { PrintDebug("nvram: somehow managed to get a carry in minute update\n"); }
261 if ((bcd && *min==0x60) || (!bcd && *min==60)) {
267 uchar_t temp = bcd ? 0x12 : 12;
268 add_to(&hour24,&temp, bcd);
272 carry=add_to(&hour24,&carry,bcd);
273 if (carry) { PrintDebug("nvram: somehow managed to get a carry in hour update\n"); }
274 if ((bcd && hour24==0x24) || (!bcd && hour24==24)) {
284 if ((bcd && hour24<0x12) || (!bcd && hour24<12)) {
288 *hour = (hour24-12) | 0x80;
291 struct bcd_num *n = (struct bcd_num *) hour;
301 // now see if we need to carry into the days and further
304 add_to(weekday, &carry, bcd);
305 *weekday%=0x7; // same regardless of bcd
306 if (!(*monthday==days_in_month(dev,*month,bcd))) {
307 add_to(monthday, &carry, bcd);
311 add_to(month,&carry,bcd);
312 if ((bcd && *month==0x13) || (!bcd && *month==13)) {
313 *month=1; // same for both
315 carry=add_to(year,&carry,bcd);
316 if ((bcd && carry) || (!bcd && *year==100)) {
319 add_to(cent,&carry,bcd);
327 // OK, now check for the alarm, if it is set to interrupt
329 if (*sec==*seca && *min==*mina && *hour==*houra) {
331 PrintDebug("nvram: interrupt on alarm\n");
337 periodic_period = 1000000/(65536/(0x1 << stata->rate));
338 if (data->pus >= periodic_period) {
340 data->pus -= periodic_period;
341 PrintDebug("nvram: interrupt on periodic\n");
347 PrintDebug("nvram: interrupt on update\n");
350 statc->irq= statc->pf || statc->af || statc->uf;
352 //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);
354 // Interrupt associated VM, if needed
356 PrintDebug("nvram: injecting interrupt\n");
357 dev->vm->vm_ops.raise_irq(dev->vm, NVRAM_RTC_IRQ);
364 void deliver_timer_interrupt_to_vmm(uint_t period_us)
366 struct vm_device *dev = demultiplex_timer_interrupt(period_us);
369 update_time(dev,period_us);
375 static int set_nvram_defaults(struct vm_device *dev)
377 struct nvram_internal * nvram_state = (struct nvram_internal*) dev->private_data;
383 // 2 1.44 MB floppy drives
386 nvram_state->mem_state[NVRAM_REG_FLOPPY_TYPE]= 0x44;
388 nvram_state->mem_state[NVRAM_REG_FLOPPY_TYPE] = 0x00;
392 // For old boot sequence style, do floppy first
394 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_OLD]= 0x10;
397 // For new boot sequence style, do floppy, cd, then hd
398 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_FIRST]= 0x31;
399 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_SECOND]= 0x20;
402 // For new boot sequence style, do cd, hd, floppy
403 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_FIRST]= 0x23;
404 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_SECOND]= 0x10;
407 // Set equipment byte to note 2 floppies, vga display, keyboard,math,floppy
408 nvram_state->mem_state[NVRAM_REG_EQUIPMENT_BYTE]= 0x4f;
409 //nvram_state->mem_state[NVRAM_REG_EQUIPMENT_BYTE] = 0xf;
411 // Set conventional memory to 640K
412 nvram_state->mem_state[NVRAM_REG_BASE_MEMORY_HIGH]= 0x02;
413 nvram_state->mem_state[NVRAM_REG_BASE_MEMORY_LOW]= 0x80;
415 // Set extended memory to 15 MB
416 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_HIGH]= 0x3C;
417 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_LOW]= 0x00;
418 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_2ND_HIGH]= 0x3C;
419 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_2ND_LOW]= 0x00;
421 // Set the extended memory beyond 16 MB to 128-16 MB
422 nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_HIGH] = 0x7;
423 nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_LOW] = 0x00;
425 //nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_HIGH]= 0x00;
426 //nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_LOW]= 0x00;
429 // This is the harddisk type.... Set accordingly...
430 nvram_state->mem_state[NVRAM_IBM_HD_DATA] = 0x20;
432 // Set the shutdown status gently
434 nvram_state->mem_state[NVRAM_REG_SHUTDOWN_STATUS] = 0x0;
438 // 00100110 = no update in progress, base=32768 Hz, rate = 1024 Hz
439 nvram_state->mem_state[NVRAM_REG_STAT_A] = 0x26;
442 // 00000100 = not setting, no interrupts, blocked rect signal, bcd mode, 24 hour, normal time
443 nvram_state->mem_state[NVRAM_REG_STAT_B] = 0x06;
447 // No IRQ requested, result not do to any source
448 nvram_state->mem_state[NVRAM_REG_STAT_C] = 0x00;
452 nvram_state->mem_state[NVRAM_REG_STAT_D] = 0x80;
455 // january 1, 2008, 00:00:00
456 nvram_state->mem_state[NVRAM_REG_MONTH] = 0x1;
457 nvram_state->mem_state[NVRAM_REG_MONTH_DAY] = 0x1;
458 nvram_state->mem_state[NVRAM_REG_WEEK_DAY] = 0x1;
459 nvram_state->mem_state[NVRAM_REG_YEAR] = 0x08;
461 nvram_state->us=nvram_state->pus=0;
468 int nvram_reset_device(struct vm_device * dev)
470 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
472 PrintDebug("nvram: reset device\n");
476 data->dev_state = NVRAM_READY;
488 int nvram_start_device(struct vm_device *dev)
490 PrintDebug("nvram: start device\n");
495 int nvram_stop_device(struct vm_device *dev)
497 PrintDebug("nvram: stop device\n");
504 int nvram_write_reg_port(ushort_t port,
507 struct vm_device * dev)
509 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
511 memcpy(&(data->thereg), src, 1);
512 PrintDebug("Writing To NVRAM reg: 0x%x\n", data->thereg);
518 int nvram_read_data_port(ushort_t port,
521 struct vm_device * dev)
523 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
527 memcpy(dst, &(data->mem_state[data->thereg]), 1);
529 PrintDebug("nvram_read_data_port(0x%x)=0x%x\n", data->thereg, data->mem_state[data->thereg]);
532 if (data->thereg==NVRAM_REG_STAT_A) {
533 data->mem_state[data->thereg] ^= 0x80; // toggle Update in progess
540 int nvram_write_data_port(ushort_t port,
543 struct vm_device * dev)
545 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
547 memcpy(&(data->mem_state[data->thereg]), src, 1);
549 PrintDebug("nvram_write_data_port(0x%x)=0x%x\n", data->thereg, data->mem_state[data->thereg]);
556 int nvram_init_device(struct vm_device * dev) {
558 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
560 PrintDebug("nvram: init_device\n");
562 memset(data->mem_state, 0, NVRAM_REG_MAX);
564 // Would read state here
565 set_nvram_defaults(dev);
567 nvram_reset_device(dev);
570 dev_hook_io(dev, NVRAM_REG_PORT, NULL, &nvram_write_reg_port);
571 dev_hook_io(dev, NVRAM_DATA_PORT, &nvram_read_data_port, &nvram_write_data_port);
576 int nvram_deinit_device(struct vm_device *dev)
580 dev_unhook_io(dev, NVRAM_REG_PORT);
581 dev_unhook_io(dev, NVRAM_DATA_PORT);
583 nvram_reset_device(dev);
591 static struct vm_device_ops dev_ops = {
592 .init = nvram_init_device,
593 .deinit = nvram_deinit_device,
594 .reset = nvram_reset_device,
595 .start = nvram_start_device,
596 .stop = nvram_stop_device,
602 struct vm_device *create_nvram() {
603 struct nvram_internal * nvram_state = (struct nvram_internal *)V3_Malloc(sizeof(struct nvram_internal)+1000);
605 PrintDebug("nvram: internal at %x\n",nvram_state);
607 struct vm_device *device = create_device("NVRAM", &dev_ops, nvram_state);
610 PrintDebug("nvram: warning! overwriting thedev\n");