Statistics
| Branch: | Revision:

root / hw / ds1225y.c @ 9a87ce9b

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 87ecb68b pbrook
#include "hw.h"
26 87ecb68b pbrook
#include "mips.h"
27 87ecb68b pbrook
#include "nvram.h"
28 9542611a ths
29 9542611a ths
typedef enum
30 9542611a ths
{
31 9542611a ths
    none = 0,
32 9542611a ths
    readmode,
33 9542611a ths
    writemode,
34 9542611a ths
} nvram_open_mode;
35 9542611a ths
36 9542611a ths
struct ds1225y_t
37 9542611a ths
{
38 71db710f blueswir1
    target_phys_addr_t mem_base;
39 9542611a ths
    uint32_t capacity;
40 9542611a ths
    const char *filename;
41 9542611a ths
    QEMUFile *file;
42 9542611a ths
    nvram_open_mode open_mode;
43 9542611a ths
};
44 9542611a ths
45 9542611a ths
static int ds1225y_set_to_mode(ds1225y_t *NVRAM, nvram_open_mode mode, const char *filemode)
46 9542611a ths
{
47 9542611a ths
    if (NVRAM->open_mode != mode)
48 9542611a ths
    {
49 9542611a ths
        if (NVRAM->file)
50 9542611a ths
            qemu_fclose(NVRAM->file);
51 9542611a ths
        NVRAM->file = qemu_fopen(NVRAM->filename, filemode);
52 9542611a ths
        NVRAM->open_mode = mode;
53 9542611a ths
    }
54 9542611a ths
    return (NVRAM->file != NULL);
55 9542611a ths
}
56 9542611a ths
57 9542611a ths
static uint32_t nvram_readb (void *opaque, target_phys_addr_t addr)
58 9542611a ths
{
59 9542611a ths
    ds1225y_t *NVRAM = opaque;
60 9542611a ths
    int64_t pos;
61 9542611a ths
62 9542611a ths
    pos = addr - NVRAM->mem_base;
63 9542611a ths
    if (addr >= NVRAM->capacity)
64 9542611a ths
        addr -= NVRAM->capacity;
65 9542611a ths
66 9542611a ths
    if (!ds1225y_set_to_mode(NVRAM, readmode, "rb"))
67 9542611a ths
        return 0;
68 9542611a ths
    qemu_fseek(NVRAM->file, pos, SEEK_SET);
69 9542611a ths
    return (uint32_t)qemu_get_byte(NVRAM->file);
70 9542611a ths
}
71 9542611a ths
72 9542611a ths
static void nvram_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
73 9542611a ths
{
74 9542611a ths
    ds1225y_t *NVRAM = opaque;
75 9542611a ths
    int64_t pos;
76 9542611a ths
77 9542611a ths
    pos = addr - NVRAM->mem_base;
78 9542611a ths
    if (ds1225y_set_to_mode(NVRAM, writemode, "wb"))
79 9542611a ths
    {
80 9542611a ths
        qemu_fseek(NVRAM->file, pos, SEEK_SET);
81 9542611a ths
        qemu_put_byte(NVRAM->file, (int)value);
82 9542611a ths
    }
83 9542611a ths
}
84 9542611a ths
85 9542611a ths
static CPUReadMemoryFunc *nvram_read[] = {
86 9542611a ths
    &nvram_readb,
87 9542611a ths
    NULL,
88 9542611a ths
    NULL,
89 9542611a ths
};
90 9542611a ths
91 9542611a ths
static CPUWriteMemoryFunc *nvram_write[] = {
92 9542611a ths
    &nvram_writeb,
93 9542611a ths
    NULL,
94 9542611a ths
    NULL,
95 9542611a ths
};
96 9542611a ths
97 9542611a ths
static CPUWriteMemoryFunc *nvram_none[] = {
98 9542611a ths
    NULL,
99 9542611a ths
    NULL,
100 9542611a ths
    NULL,
101 9542611a ths
};
102 9542611a ths
103 9542611a ths
/* Initialisation routine */
104 71db710f blueswir1
ds1225y_t *ds1225y_init(target_phys_addr_t mem_base, const char *filename)
105 9542611a ths
{
106 9542611a ths
    ds1225y_t *s;
107 9542611a ths
    int mem_index1, mem_index2;
108 9542611a ths
109 9542611a ths
    s = qemu_mallocz(sizeof(ds1225y_t));
110 9542611a ths
    if (!s)
111 9542611a ths
        return NULL;
112 9542611a ths
    s->mem_base = mem_base;
113 9542611a ths
    s->capacity = 0x2000; /* Fixed for ds1225y chip: 8K */
114 9542611a ths
    s->filename = filename;
115 9542611a ths
116 9542611a ths
    /* Read/write memory */
117 9542611a ths
    mem_index1 = cpu_register_io_memory(0, nvram_read, nvram_write, s);
118 9542611a ths
    cpu_register_physical_memory(mem_base, s->capacity, mem_index1);
119 9542611a ths
    /* Read-only memory */
120 9542611a ths
    mem_index2 = cpu_register_io_memory(0, nvram_read, nvram_none, s);
121 9542611a ths
    cpu_register_physical_memory(mem_base + s->capacity, s->capacity, mem_index2);
122 9542611a ths
    return s;
123 9542611a ths
}