root / hw / ds1225y.c @ 86f25c7c
History | View | Annotate | Download (4 kB)
1 |
/*
|
---|---|
2 |
* QEMU NVRAM emulation for DS1225Y chip
|
3 |
*
|
4 |
* Copyright (c) 2007-2008 Herv? Poussineau
|
5 |
*
|
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:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be included in
|
14 |
* all copies or substantial portions of the Software.
|
15 |
*
|
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
|
22 |
* THE SOFTWARE.
|
23 |
*/
|
24 |
|
25 |
#include "hw.h" |
26 |
#include "mips.h" |
27 |
#include "nvram.h" |
28 |
|
29 |
//#define DEBUG_NVRAM
|
30 |
|
31 |
typedef struct ds1225y_t |
32 |
{ |
33 |
uint32_t chip_size; |
34 |
QEMUFile *file; |
35 |
uint8_t *contents; |
36 |
} ds1225y_t; |
37 |
|
38 |
|
39 |
static uint32_t nvram_readb (void *opaque, target_phys_addr_t addr) |
40 |
{ |
41 |
ds1225y_t *s = opaque; |
42 |
uint32_t val; |
43 |
|
44 |
val = s->contents[addr]; |
45 |
|
46 |
#ifdef DEBUG_NVRAM
|
47 |
printf("nvram: read 0x%x at " TARGET_FMT_lx "\n", val, addr); |
48 |
#endif
|
49 |
return val;
|
50 |
} |
51 |
|
52 |
static uint32_t nvram_readw (void *opaque, target_phys_addr_t addr) |
53 |
{ |
54 |
uint32_t v; |
55 |
v = nvram_readb(opaque, addr); |
56 |
v |= nvram_readb(opaque, addr + 1) << 8; |
57 |
return v;
|
58 |
} |
59 |
|
60 |
static uint32_t nvram_readl (void *opaque, target_phys_addr_t addr) |
61 |
{ |
62 |
uint32_t v; |
63 |
v = nvram_readb(opaque, addr); |
64 |
v |= nvram_readb(opaque, addr + 1) << 8; |
65 |
v |= nvram_readb(opaque, addr + 2) << 16; |
66 |
v |= nvram_readb(opaque, addr + 3) << 24; |
67 |
return v;
|
68 |
} |
69 |
|
70 |
static void nvram_writeb (void *opaque, target_phys_addr_t addr, uint32_t val) |
71 |
{ |
72 |
ds1225y_t *s = opaque; |
73 |
|
74 |
#ifdef DEBUG_NVRAM
|
75 |
printf("nvram: write 0x%x at " TARGET_FMT_lx "\n", val, addr); |
76 |
#endif
|
77 |
|
78 |
s->contents[addr] = val & 0xff;
|
79 |
if (s->file) {
|
80 |
qemu_fseek(s->file, addr, SEEK_SET); |
81 |
qemu_put_byte(s->file, (int)val);
|
82 |
qemu_fflush(s->file); |
83 |
} |
84 |
} |
85 |
|
86 |
static void nvram_writew (void *opaque, target_phys_addr_t addr, uint32_t val) |
87 |
{ |
88 |
nvram_writeb(opaque, addr, val & 0xff);
|
89 |
nvram_writeb(opaque, addr + 1, (val >> 8) & 0xff); |
90 |
} |
91 |
|
92 |
static void nvram_writel (void *opaque, target_phys_addr_t addr, uint32_t val) |
93 |
{ |
94 |
nvram_writeb(opaque, addr, val & 0xff);
|
95 |
nvram_writeb(opaque, addr + 1, (val >> 8) & 0xff); |
96 |
nvram_writeb(opaque, addr + 2, (val >> 16) & 0xff); |
97 |
nvram_writeb(opaque, addr + 3, (val >> 24) & 0xff); |
98 |
} |
99 |
|
100 |
static CPUReadMemoryFunc * const nvram_read[] = { |
101 |
&nvram_readb, |
102 |
&nvram_readw, |
103 |
&nvram_readl, |
104 |
}; |
105 |
|
106 |
static CPUWriteMemoryFunc * const nvram_write[] = { |
107 |
&nvram_writeb, |
108 |
&nvram_writew, |
109 |
&nvram_writel, |
110 |
}; |
111 |
|
112 |
/* Initialisation routine */
|
113 |
void *ds1225y_init(target_phys_addr_t mem_base, const char *filename) |
114 |
{ |
115 |
ds1225y_t *s; |
116 |
int mem_indexRW;
|
117 |
QEMUFile *file; |
118 |
|
119 |
s = qemu_mallocz(sizeof(ds1225y_t));
|
120 |
s->chip_size = 0x2000; /* Fixed for ds1225y chip: 8 KiB */ |
121 |
s->contents = qemu_mallocz(s->chip_size); |
122 |
|
123 |
/* Read current file */
|
124 |
file = qemu_fopen(filename, "rb");
|
125 |
if (file) {
|
126 |
/* Read nvram contents */
|
127 |
qemu_get_buffer(file, s->contents, s->chip_size); |
128 |
qemu_fclose(file); |
129 |
} |
130 |
s->file = qemu_fopen(filename, "wb");
|
131 |
if (s->file) {
|
132 |
/* Write back contents, as 'wb' mode cleaned the file */
|
133 |
qemu_put_buffer(s->file, s->contents, s->chip_size); |
134 |
qemu_fflush(s->file); |
135 |
} |
136 |
|
137 |
/* Read/write memory */
|
138 |
mem_indexRW = cpu_register_io_memory(nvram_read, nvram_write, s, |
139 |
DEVICE_NATIVE_ENDIAN); |
140 |
cpu_register_physical_memory(mem_base, s->chip_size, mem_indexRW); |
141 |
return s;
|
142 |
} |