1 #include <devices/nvram.h>
2 #include <palacios/vmm.h>
3 #include <palacios/vmm_types.h>
8 #define PrintDebug(fmt, args...)
15 #define NVRAM_REG_PORT 0x70
16 #define NVRAM_DATA_PORT 0x71
20 typedef enum {NVRAM_READY, NVRAM_REG_POSTED} nvram_state_t;
23 #define NVRAM_REG_MAX 256
26 // These are borrowed from Bochs, which borrowed from
27 // Ralf Brown's interupt list, and extended
28 #define NVRAM_REG_SEC 0x00
29 #define NVRAM_REG_SEC_ALARM 0x01
30 #define NVRAM_REG_MIN 0x02
31 #define NVRAM_REG_MIN_ALARM 0x03
32 #define NVRAM_REG_HOUR 0x04
33 #define NVRAM_REG_HOUR_ALARM 0x05
34 #define NVRAM_REG_WEEK_DAY 0x06
35 #define NVRAM_REG_MONTH_DAY 0x07
36 #define NVRAM_REG_MONTH 0x08
37 #define NVRAM_REG_YEAR 0x09
38 #define NVRAM_REG_STAT_A 0x0a
39 #define NVRAM_REG_STAT_B 0x0b
40 #define NVRAM_REG_STAT_C 0x0c
41 #define NVRAM_REG_STAT_D 0x0d
42 #define NVRAM_REG_DIAGNOSTIC_STATUS 0x0e
43 #define NVRAM_REG_SHUTDOWN_STATUS 0x0f
45 #define NVRAM_IBM_HD_DATA 0x12
47 #define NVRAM_REG_FLOPPY_TYPE 0x10
48 #define NVRAM_REG_EQUIPMENT_BYTE 0x14
50 #define NVRAM_REG_BASE_MEMORY_HIGH 0x16
51 #define NVRAM_REG_BASE_MEMORY_LOW 0x15
53 #define NVRAM_REG_EXT_MEMORY_HIGH 0x18
54 #define NVRAM_REG_EXT_MEMORY_LOW 0x17
56 #define NVRAM_REG_EXT_MEMORY_2ND_HIGH 0x31
57 #define NVRAM_REG_EXT_MEMORY_2ND_LOW 0x30
59 #define NVRAM_REG_BOOTSEQ_OLD 0x2d
61 #define NVRAM_REG_AMI_BIG_MEMORY_HIGH 0x35
62 #define NVRAM_REG_AMI_BIG_MEMORY_LOW 0x34
65 #define NVRAM_REG_CSUM_HIGH 0x2e
66 #define NVRAM_REG_CSUM_LOW 0x2f
67 #define NVRAM_REG_IBM_CENTURY_BYTE 0x32
68 #define NVRAM_REG_IBM_PS2_CENTURY_BYTE 0x37
70 #define NVRAM_REG_BOOTSEQ_NEW_FIRST 0x3D
71 #define NVRAM_REG_BOOTSEQ_NEW_SECOND 0x38
74 struct nvram_internal {
75 nvram_state_t dev_state;
77 uchar_t mem_state[NVRAM_REG_MAX];
82 static int set_nvram_defaults(struct vm_device *dev)
84 struct nvram_internal * nvram_state = (struct nvram_internal*) dev->private_data;
87 // 2 1.44 MB floppy drives
90 nvram_state->mem_state[NVRAM_REG_FLOPPY_TYPE]= 0x44;
92 nvram_state->mem_state[NVRAM_REG_FLOPPY_TYPE] = 0x00;
96 // For old boot sequence style, do floppy first
98 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_OLD]= 0x10;
101 // For new boot sequence style, do floppy, cd, then hd
102 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_FIRST]= 0x31;
103 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_SECOND]= 0x20;
106 // For new boot sequence style, do cd, hd, floppy
107 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_FIRST]= 0x23;
108 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_SECOND]= 0x10;
111 // Set equipment byte to note 2 floppies, vga display, keyboard,math,floppy
112 nvram_state->mem_state[NVRAM_REG_EQUIPMENT_BYTE]= 0x4f;
113 //nvram_state->mem_state[NVRAM_REG_EQUIPMENT_BYTE] = 0xf;
115 // Set conventional memory to 640K
116 nvram_state->mem_state[NVRAM_REG_BASE_MEMORY_HIGH]= 0x02;
117 nvram_state->mem_state[NVRAM_REG_BASE_MEMORY_LOW]= 0x80;
119 // Set extended memory to 15 MB
120 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_HIGH]= 0x3C;
121 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_LOW]= 0x00;
122 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_2ND_HIGH]= 0x3C;
123 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_2ND_LOW]= 0x00;
125 // Set the extended memory beyond 16 MB to 128-16 MB
126 nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_HIGH] = 0x7;
127 nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_LOW] = 0x00;
129 //nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_HIGH]= 0x00;
130 //nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_LOW]= 0x00;
133 // This is the harddisk type.... Set accordingly...
134 nvram_state->mem_state[NVRAM_IBM_HD_DATA] = 0x20;
136 // Set the shutdown status gently
138 nvram_state->mem_state[NVRAM_REG_SHUTDOWN_STATUS] = 0x0;
142 // time update in progress, default timebase (32KHz, default interrupt rate 1KHz)
144 nvram_state->mem_state[NVRAM_REG_STAT_A] = 0xa6;
147 // time updates, default timebase (32KHz, default interrupt rate 1KHz)
149 //nvram_state->mem_state[NVRAM_REG_STAT_B] = 0xa6;
158 int nvram_reset_device(struct vm_device * dev)
160 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
162 PrintDebug("nvram: reset device\n");
166 data->dev_state = NVRAM_READY;
178 int nvram_start_device(struct vm_device *dev)
180 PrintDebug("nvram: start device\n");
185 int nvram_stop_device(struct vm_device *dev)
187 PrintDebug("nvram: stop device\n");
194 int nvram_write_reg_port(ushort_t port,
197 struct vm_device * dev)
199 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
201 memcpy(&(data->thereg), src, 1);
202 PrintDebug("Writing To NVRAM reg: 0x%x\n", data->thereg);
208 int nvram_read_data_port(ushort_t port,
211 struct vm_device * dev)
213 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
217 memcpy(dst, &(data->mem_state[data->thereg]), 1);
219 PrintDebug("nvram_read_data_port(0x%x)=0x%x\n", data->thereg, data->mem_state[data->thereg]);
222 if (data->thereg==NVRAM_REG_STAT_A) {
223 data->mem_state[data->thereg] ^= 0x80; // toggle Update in progess
230 int nvram_write_data_port(ushort_t port,
233 struct vm_device * dev)
235 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
237 memcpy(&(data->mem_state[data->thereg]), src, 1);
239 PrintDebug("nvram_write_data_port(0x%x)=0x%x\n", data->thereg, data->mem_state[data->thereg]);
246 int nvram_init_device(struct vm_device * dev) {
248 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
250 PrintDebug("nvram: init_device\n");
252 memset(data->mem_state, 0, NVRAM_REG_MAX);
254 // Would read state here
255 set_nvram_defaults(dev);
257 nvram_reset_device(dev);
260 dev_hook_io(dev, NVRAM_REG_PORT, NULL, &nvram_write_reg_port);
261 dev_hook_io(dev, NVRAM_DATA_PORT, &nvram_read_data_port, &nvram_write_data_port);
266 int nvram_deinit_device(struct vm_device *dev)
270 dev_unhook_io(dev, NVRAM_REG_PORT);
271 dev_unhook_io(dev, NVRAM_DATA_PORT);
273 nvram_reset_device(dev);
281 static struct vm_device_ops dev_ops = {
282 .init = nvram_init_device,
283 .deinit = nvram_deinit_device,
284 .reset = nvram_reset_device,
285 .start = nvram_start_device,
286 .stop = nvram_stop_device,
292 struct vm_device *create_nvram() {
293 struct nvram_internal * nvram_state = (struct nvram_internal *)V3_Malloc(sizeof(struct nvram_internal)+1000);
295 PrintDebug("internal at %x\n",nvram_state);
297 struct vm_device *device = create_device("NVRAM", &dev_ops, nvram_state);