Statistics
| Branch: | Revision:

root / hw / ds1225y.c @ 66450b15

History | View | Annotate | Download (3.5 kB)

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