Break up vl.h.
[qemu] / hw / ds1225y.c
1 /*\r
2  * QEMU NVRAM emulation for DS1225Y chip\r
3  * \r
4  * Copyright (c) 2007 HervĂ© Poussineau\r
5  * \r
6  * Permission is hereby granted, free of charge, to any person obtaining a copy\r
7  * of this software and associated documentation files (the "Software"), to deal\r
8  * in the Software without restriction, including without limitation the rights\r
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
10  * copies of the Software, and to permit persons to whom the Software is\r
11  * furnished to do so, subject to the following conditions:\r
12  *\r
13  * The above copyright notice and this permission notice shall be included in\r
14  * all copies or substantial portions of the Software.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL\r
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
22  * THE SOFTWARE.\r
23  */\r
24 \r
25 #include "hw.h"\r
26 #include "mips.h"\r
27 #include "nvram.h"\r
28 \r
29 typedef enum\r
30 {\r
31     none = 0,\r
32     readmode,\r
33     writemode,\r
34 } nvram_open_mode;\r
35 \r
36 struct ds1225y_t\r
37 {\r
38     target_phys_addr_t mem_base;\r
39     uint32_t capacity;\r
40     const char *filename;\r
41     QEMUFile *file;\r
42     nvram_open_mode open_mode;\r
43 };\r
44 \r
45 static int ds1225y_set_to_mode(ds1225y_t *NVRAM, nvram_open_mode mode, const char *filemode)\r
46 {\r
47     if (NVRAM->open_mode != mode)\r
48     {\r
49         if (NVRAM->file)\r
50             qemu_fclose(NVRAM->file);\r
51         NVRAM->file = qemu_fopen(NVRAM->filename, filemode);\r
52         NVRAM->open_mode = mode;\r
53     }\r
54     return (NVRAM->file != NULL);\r
55 }\r
56 \r
57 static uint32_t nvram_readb (void *opaque, target_phys_addr_t addr)\r
58 {\r
59     ds1225y_t *NVRAM = opaque;\r
60     int64_t pos;\r
61 \r
62     pos = addr - NVRAM->mem_base;\r
63     if (addr >= NVRAM->capacity)\r
64         addr -= NVRAM->capacity;\r
65 \r
66     if (!ds1225y_set_to_mode(NVRAM, readmode, "rb"))\r
67         return 0;\r
68     qemu_fseek(NVRAM->file, pos, SEEK_SET);\r
69     return (uint32_t)qemu_get_byte(NVRAM->file);\r
70 }\r
71 \r
72 static void nvram_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)\r
73 {\r
74     ds1225y_t *NVRAM = opaque;\r
75     int64_t pos;\r
76 \r
77     pos = addr - NVRAM->mem_base;\r
78     if (ds1225y_set_to_mode(NVRAM, writemode, "wb"))\r
79     {\r
80         qemu_fseek(NVRAM->file, pos, SEEK_SET);\r
81         qemu_put_byte(NVRAM->file, (int)value);\r
82     }\r
83 }\r
84 \r
85 static CPUReadMemoryFunc *nvram_read[] = {\r
86     &nvram_readb,\r
87     NULL,\r
88     NULL,\r
89 };\r
90 \r
91 static CPUWriteMemoryFunc *nvram_write[] = {\r
92     &nvram_writeb,\r
93     NULL,\r
94     NULL,\r
95 };\r
96 \r
97 static CPUWriteMemoryFunc *nvram_none[] = {\r
98     NULL,\r
99     NULL,\r
100     NULL,\r
101 };\r
102 \r
103 /* Initialisation routine */\r
104 ds1225y_t *ds1225y_init(target_phys_addr_t mem_base, const char *filename)\r
105 {\r
106     ds1225y_t *s;\r
107     int mem_index1, mem_index2;\r
108 \r
109     s = qemu_mallocz(sizeof(ds1225y_t));\r
110     if (!s)\r
111         return NULL;\r
112     s->mem_base = mem_base;\r
113     s->capacity = 0x2000; /* Fixed for ds1225y chip: 8K */\r
114     s->filename = filename;\r
115 \r
116     /* Read/write memory */\r
117     mem_index1 = cpu_register_io_memory(0, nvram_read, nvram_write, s);\r
118     cpu_register_physical_memory(mem_base, s->capacity, mem_index1);\r
119     /* Read-only memory */\r
120     mem_index2 = cpu_register_io_memory(0, nvram_read, nvram_none, s);\r
121     cpu_register_physical_memory(mem_base + s->capacity, s->capacity, mem_index2);\r
122     return s;\r
123 }\r