]>
git.proxmox.com Git - qemu.git/blob - hw/m48t08.c
2 * QEMU M48T08 NVRAM emulation for Sparc platform
4 * Copyright (c) 2003-2004 Jocelyn Mayer
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
29 #if defined(DEBUG_NVRAM)
30 #define NVRAM_PRINTF(fmt, args...) do { printf(fmt , ##args); } while (0)
32 #define NVRAM_PRINTF(fmt, args...) do { } while (0)
35 #define NVRAM_MAX_MEM 0xfff0
38 /* Hardware parameters */
51 /* Fake timer functions */
52 /* Generic helpers for BCD */
53 static inline uint8_t toBCD (uint8_t value
)
55 return (((value
/ 10) % 10) << 4) | (value
% 10);
58 static inline uint8_t fromBCD (uint8_t BCD
)
60 return ((BCD
>> 4) * 10) + (BCD
& 0x0F);
63 /* RTC management helpers */
64 static void get_time (m48t08_t
*NVRAM
, struct tm
*tm
)
68 t
= time(NULL
) + NVRAM
->time_offset
;
70 memcpy(tm
,localtime(&t
),sizeof(*tm
));
72 localtime_r (&t
, tm
) ;
76 static void set_time (m48t08_t
*NVRAM
, struct tm
*tm
)
80 new_time
= mktime(tm
);
82 NVRAM
->time_offset
= new_time
- now
;
85 /* Direct access to NVRAM */
86 void m48t08_write (m48t08_t
*NVRAM
, uint32_t val
)
91 if (NVRAM
->addr
> NVRAM_MAX_MEM
&& NVRAM
->addr
< 0x2000)
92 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, NVRAM
->addr
, val
);
93 switch (NVRAM
->addr
) {
96 NVRAM
->buffer
[0x1FF8] = (val
& ~0xA0) | 0x90;
100 tmp
= fromBCD(val
& 0x7F);
101 if (tmp
>= 0 && tmp
<= 59) {
102 get_time(NVRAM
, &tm
);
104 set_time(NVRAM
, &tm
);
106 if ((val
& 0x80) ^ (NVRAM
->buffer
[0x1FF9] & 0x80)) {
108 NVRAM
->stop_time
= time(NULL
);
110 NVRAM
->time_offset
+= NVRAM
->stop_time
- time(NULL
);
111 NVRAM
->stop_time
= 0;
114 NVRAM
->buffer
[0x1FF9] = val
& 0x80;
118 tmp
= fromBCD(val
& 0x7F);
119 if (tmp
>= 0 && tmp
<= 59) {
120 get_time(NVRAM
, &tm
);
122 set_time(NVRAM
, &tm
);
127 tmp
= fromBCD(val
& 0x3F);
128 if (tmp
>= 0 && tmp
<= 23) {
129 get_time(NVRAM
, &tm
);
131 set_time(NVRAM
, &tm
);
135 /* day of the week / century */
136 tmp
= fromBCD(val
& 0x07);
137 get_time(NVRAM
, &tm
);
139 set_time(NVRAM
, &tm
);
140 NVRAM
->buffer
[0x1FFC] = val
& 0x40;
144 tmp
= fromBCD(val
& 0x1F);
146 get_time(NVRAM
, &tm
);
148 set_time(NVRAM
, &tm
);
153 tmp
= fromBCD(val
& 0x1F);
154 if (tmp
>= 1 && tmp
<= 12) {
155 get_time(NVRAM
, &tm
);
157 set_time(NVRAM
, &tm
);
163 if (tmp
>= 0 && tmp
<= 99) {
164 get_time(NVRAM
, &tm
);
165 tm
.tm_year
= fromBCD(val
);
166 set_time(NVRAM
, &tm
);
170 /* Check lock registers state */
171 if (NVRAM
->addr
>= 0x20 && NVRAM
->addr
<= 0x2F && (NVRAM
->lock
& 1))
173 if (NVRAM
->addr
>= 0x30 && NVRAM
->addr
<= 0x3F && (NVRAM
->lock
& 2))
175 if (NVRAM
->addr
< NVRAM_MAX_MEM
||
176 (NVRAM
->addr
> 0x1FFF && NVRAM
->addr
< NVRAM
->size
)) {
177 NVRAM
->buffer
[NVRAM
->addr
] = val
& 0xFF;
183 uint32_t m48t08_read (m48t08_t
*NVRAM
)
186 uint32_t retval
= 0xFF;
188 switch (NVRAM
->addr
) {
194 get_time(NVRAM
, &tm
);
195 retval
= (NVRAM
->buffer
[0x1FF9] & 0x80) | toBCD(tm
.tm_sec
);
199 get_time(NVRAM
, &tm
);
200 retval
= toBCD(tm
.tm_min
);
204 get_time(NVRAM
, &tm
);
205 retval
= toBCD(tm
.tm_hour
);
208 /* day of the week / century */
209 get_time(NVRAM
, &tm
);
210 retval
= NVRAM
->buffer
[0x1FFC] | tm
.tm_wday
;
214 get_time(NVRAM
, &tm
);
215 retval
= toBCD(tm
.tm_mday
);
219 get_time(NVRAM
, &tm
);
220 retval
= toBCD(tm
.tm_mon
+ 1);
224 get_time(NVRAM
, &tm
);
225 retval
= toBCD(tm
.tm_year
);
228 /* Check lock registers state */
229 if (NVRAM
->addr
>= 0x20 && NVRAM
->addr
<= 0x2F && (NVRAM
->lock
& 1))
231 if (NVRAM
->addr
>= 0x30 && NVRAM
->addr
<= 0x3F && (NVRAM
->lock
& 2))
233 if (NVRAM
->addr
< NVRAM_MAX_MEM
||
234 (NVRAM
->addr
> 0x1FFF && NVRAM
->addr
< NVRAM
->size
)) {
236 retval
= NVRAM
->buffer
[NVRAM
->addr
];
240 if (NVRAM
->addr
> NVRAM_MAX_MEM
+ 1 && NVRAM
->addr
< 0x2000)
241 NVRAM_PRINTF("0x%08x <= 0x%08x\n", NVRAM
->addr
, retval
);
246 void m48t08_set_addr (m48t08_t
*NVRAM
, uint32_t addr
)
251 void m48t08_toggle_lock (m48t08_t
*NVRAM
, int lock
)
253 NVRAM
->lock
^= 1 << lock
;
256 static void nvram_writeb (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
258 m48t08_t
*NVRAM
= opaque
;
260 addr
-= NVRAM
->mem_base
;
261 if (addr
< NVRAM_MAX_MEM
)
262 NVRAM
->buffer
[addr
] = value
;
265 static void nvram_writew (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
267 m48t08_t
*NVRAM
= opaque
;
269 addr
-= NVRAM
->mem_base
;
270 if (addr
< NVRAM_MAX_MEM
) {
271 NVRAM
->buffer
[addr
] = value
>> 8;
272 NVRAM
->buffer
[addr
+ 1] = value
;
276 static void nvram_writel (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
278 m48t08_t
*NVRAM
= opaque
;
280 addr
-= NVRAM
->mem_base
;
281 if (addr
< NVRAM_MAX_MEM
) {
282 NVRAM
->buffer
[addr
] = value
>> 24;
283 NVRAM
->buffer
[addr
+ 1] = value
>> 16;
284 NVRAM
->buffer
[addr
+ 2] = value
>> 8;
285 NVRAM
->buffer
[addr
+ 3] = value
;
289 static uint32_t nvram_readb (void *opaque
, target_phys_addr_t addr
)
291 m48t08_t
*NVRAM
= opaque
;
294 addr
-= NVRAM
->mem_base
;
295 if (addr
< NVRAM_MAX_MEM
)
296 retval
= NVRAM
->buffer
[addr
];
301 static uint32_t nvram_readw (void *opaque
, target_phys_addr_t addr
)
303 m48t08_t
*NVRAM
= opaque
;
306 addr
-= NVRAM
->mem_base
;
307 if (addr
< NVRAM_MAX_MEM
) {
308 retval
= NVRAM
->buffer
[addr
] << 8;
309 retval
|= NVRAM
->buffer
[addr
+ 1];
315 static uint32_t nvram_readl (void *opaque
, target_phys_addr_t addr
)
317 m48t08_t
*NVRAM
= opaque
;
320 addr
-= NVRAM
->mem_base
;
321 if (addr
< NVRAM_MAX_MEM
) {
322 retval
= NVRAM
->buffer
[addr
] << 24;
323 retval
|= NVRAM
->buffer
[addr
+ 1] << 16;
324 retval
|= NVRAM
->buffer
[addr
+ 2] << 8;
325 retval
|= NVRAM
->buffer
[addr
+ 3];
331 static CPUWriteMemoryFunc
*nvram_write
[] = {
337 static CPUReadMemoryFunc
*nvram_read
[] = {
343 /* Initialisation routine */
344 m48t08_t
*m48t08_init(uint32_t mem_base
, uint16_t size
, uint8_t *macaddr
)
348 unsigned char tmp
= 0;
350 s
= qemu_mallocz(sizeof(m48t08_t
));
353 s
->buffer
= qemu_mallocz(size
);
359 s
->mem_base
= mem_base
;
362 s
->mem_index
= cpu_register_io_memory(0, nvram_read
, nvram_write
, s
);
363 cpu_register_physical_memory(mem_base
, 0x4000, s
->mem_index
);
368 s
->buffer
[i
++] = 0x01;
369 s
->buffer
[i
++] = 0x80; /* Sun4m OBP */
370 memcpy(&s
->buffer
[i
], macaddr
, 6);
372 /* Calculate checksum */
373 for (i
= 0x1fd8; i
< 0x1fe7; i
++) {
376 s
->buffer
[0x1fe7] = tmp
;
383 unsigned char id_format
; /* Format identifier (always 0x01) */
384 unsigned char id_machtype
; /* Machine type */
385 unsigned char id_ethaddr
[6]; /* Hardware ethernet address */
386 long id_date
; /* Date of manufacture */
387 unsigned int id_sernum
:24; /* Unique serial number */
388 unsigned char id_cksum
; /* Checksum - xor of the data bytes */
389 unsigned char reserved
[16];