2
* QEMU NVRAM emulation for DS1225Y chip
4
* Copyright (c) 2007 Herv� Poussineau
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:
13
* The above copyright notice and this permission notice shall be included in
14
* all copies or substantial portions of the Software.
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
38
target_phys_addr_t mem_base;
42
nvram_open_mode open_mode;
45
static int ds1225y_set_to_mode(ds1225y_t *NVRAM, nvram_open_mode mode, const char *filemode)
47
if (NVRAM->open_mode != mode)
50
qemu_fclose(NVRAM->file);
51
NVRAM->file = qemu_fopen(NVRAM->filename, filemode);
52
NVRAM->open_mode = mode;
54
return (NVRAM->file != NULL);
57
static uint32_t nvram_readb (void *opaque, target_phys_addr_t addr)
59
ds1225y_t *NVRAM = opaque;
62
pos = addr - NVRAM->mem_base;
63
if (addr >= NVRAM->capacity)
64
addr -= NVRAM->capacity;
66
if (!ds1225y_set_to_mode(NVRAM, readmode, "rb"))
68
qemu_fseek(NVRAM->file, pos, SEEK_SET);
69
return (uint32_t)qemu_get_byte(NVRAM->file);
72
static void nvram_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
74
ds1225y_t *NVRAM = opaque;
77
pos = addr - NVRAM->mem_base;
78
if (ds1225y_set_to_mode(NVRAM, writemode, "wb"))
80
qemu_fseek(NVRAM->file, pos, SEEK_SET);
81
qemu_put_byte(NVRAM->file, (int)value);
85
static CPUReadMemoryFunc *nvram_read[] = {
91
static CPUWriteMemoryFunc *nvram_write[] = {
97
static CPUWriteMemoryFunc *nvram_none[] = {
103
/* Initialisation routine */
104
ds1225y_t *ds1225y_init(target_phys_addr_t mem_base, const char *filename)
107
int mem_index1, mem_index2;
109
s = qemu_mallocz(sizeof(ds1225y_t));
112
s->mem_base = mem_base;
113
s->capacity = 0x2000; /* Fixed for ds1225y chip: 8K */
114
s->filename = filename;
116
/* Read/write memory */
117
mem_index1 = cpu_register_io_memory(0, nvram_read, nvram_write, s);
118
cpu_register_physical_memory(mem_base, s->capacity, mem_index1);
119
/* Read-only memory */
120
mem_index2 = cpu_register_io_memory(0, nvram_read, nvram_none, s);
121
cpu_register_physical_memory(mem_base + s->capacity, s->capacity, mem_index2);
2
* QEMU NVRAM emulation for DS1225Y chip
4
* Copyright (c) 2007-2008 Herv� Poussineau
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:
13
* The above copyright notice and this permission notice shall be included in
14
* all copies or substantial portions of the Software.
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
31
typedef struct ds1225y_t
40
static uint32_t nvram_readb (void *opaque, target_phys_addr_t addr)
42
ds1225y_t *s = opaque;
45
val = s->contents[addr];
48
printf("nvram: read 0x%x at " TARGET_FMT_lx "\n", val, addr);
53
static uint32_t nvram_readw (void *opaque, target_phys_addr_t addr)
56
v = nvram_readb(opaque, addr);
57
v |= nvram_readb(opaque, addr + 1) << 8;
61
static uint32_t nvram_readl (void *opaque, target_phys_addr_t addr)
64
v = nvram_readb(opaque, addr);
65
v |= nvram_readb(opaque, addr + 1) << 8;
66
v |= nvram_readb(opaque, addr + 2) << 16;
67
v |= nvram_readb(opaque, addr + 3) << 24;
71
static void nvram_writeb (void *opaque, target_phys_addr_t addr, uint32_t val)
73
ds1225y_t *s = opaque;
76
printf("nvram: write 0x%x at " TARGET_FMT_lx "\n", val, addr);
79
s->contents[addr] = val & 0xff;
81
qemu_fseek(s->file, addr, SEEK_SET);
82
qemu_put_byte(s->file, (int)val);
87
static void nvram_writew (void *opaque, target_phys_addr_t addr, uint32_t val)
89
nvram_writeb(opaque, addr, val & 0xff);
90
nvram_writeb(opaque, addr + 1, (val >> 8) & 0xff);
93
static void nvram_writel (void *opaque, target_phys_addr_t addr, uint32_t val)
95
nvram_writeb(opaque, addr, val & 0xff);
96
nvram_writeb(opaque, addr + 1, (val >> 8) & 0xff);
97
nvram_writeb(opaque, addr + 2, (val >> 16) & 0xff);
98
nvram_writeb(opaque, addr + 3, (val >> 24) & 0xff);
101
static void nvram_writeb_protected (void *opaque, target_phys_addr_t addr, uint32_t val)
103
ds1225y_t *s = opaque;
105
if (s->protection != 7) {
107
printf("nvram: prevent write of 0x%x at " TARGET_FMT_lx "\n", val, addr);
112
nvram_writeb(opaque, addr, val);
115
static void nvram_writew_protected (void *opaque, target_phys_addr_t addr, uint32_t val)
117
nvram_writeb_protected(opaque, addr, val & 0xff);
118
nvram_writeb_protected(opaque, addr + 1, (val >> 8) & 0xff);
121
static void nvram_writel_protected (void *opaque, target_phys_addr_t addr, uint32_t val)
123
nvram_writeb_protected(opaque, addr, val & 0xff);
124
nvram_writeb_protected(opaque, addr + 1, (val >> 8) & 0xff);
125
nvram_writeb_protected(opaque, addr + 2, (val >> 16) & 0xff);
126
nvram_writeb_protected(opaque, addr + 3, (val >> 24) & 0xff);
129
static CPUReadMemoryFunc *nvram_read[] = {
135
static CPUWriteMemoryFunc *nvram_write[] = {
141
static CPUWriteMemoryFunc *nvram_write_protected[] = {
142
&nvram_writeb_protected,
143
&nvram_writew_protected,
144
&nvram_writel_protected,
147
/* Initialisation routine */
148
void *ds1225y_init(target_phys_addr_t mem_base, const char *filename)
151
int mem_indexRW, mem_indexRP;
154
s = qemu_mallocz(sizeof(ds1225y_t));
155
s->chip_size = 0x2000; /* Fixed for ds1225y chip: 8 KiB */
156
s->contents = qemu_mallocz(s->chip_size);
159
/* Read current file */
160
file = qemu_fopen(filename, "rb");
162
/* Read nvram contents */
163
qemu_get_buffer(file, s->contents, s->chip_size);
166
s->file = qemu_fopen(filename, "wb");
168
/* Write back contents, as 'wb' mode cleaned the file */
169
qemu_put_buffer(s->file, s->contents, s->chip_size);
170
qemu_fflush(s->file);
173
/* Read/write memory */
174
mem_indexRW = cpu_register_io_memory(0, nvram_read, nvram_write, s);
175
cpu_register_physical_memory(mem_base, s->chip_size, mem_indexRW);
176
/* Read/write protected memory */
177
mem_indexRP = cpu_register_io_memory(0, nvram_read, nvram_write_protected, s);
178
cpu_register_physical_memory(mem_base + s->chip_size, s->chip_size, mem_indexRP);