Statistics
| Branch: | Revision:

root / hw / milkymist-memcard.c @ 439a97cc

History | View | Annotate | Download (7.7 kB)

1 b4e37d98 Michael Walle
/*
2 b4e37d98 Michael Walle
 *  QEMU model of the Milkymist SD Card Controller.
3 b4e37d98 Michael Walle
 *
4 b4e37d98 Michael Walle
 *  Copyright (c) 2010 Michael Walle <michael@walle.cc>
5 b4e37d98 Michael Walle
 *
6 b4e37d98 Michael Walle
 * This library is free software; you can redistribute it and/or
7 b4e37d98 Michael Walle
 * modify it under the terms of the GNU Lesser General Public
8 b4e37d98 Michael Walle
 * License as published by the Free Software Foundation; either
9 b4e37d98 Michael Walle
 * version 2 of the License, or (at your option) any later version.
10 b4e37d98 Michael Walle
 *
11 b4e37d98 Michael Walle
 * This library is distributed in the hope that it will be useful,
12 b4e37d98 Michael Walle
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 b4e37d98 Michael Walle
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 b4e37d98 Michael Walle
 * Lesser General Public License for more details.
15 b4e37d98 Michael Walle
 *
16 b4e37d98 Michael Walle
 * You should have received a copy of the GNU Lesser General Public
17 b4e37d98 Michael Walle
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 b4e37d98 Michael Walle
 *
19 b4e37d98 Michael Walle
 *
20 b4e37d98 Michael Walle
 * Specification available at:
21 b4e37d98 Michael Walle
 *   http://www.milkymist.org/socdoc/memcard.pdf
22 b4e37d98 Michael Walle
 */
23 b4e37d98 Michael Walle
24 b4e37d98 Michael Walle
#include "hw.h"
25 b4e37d98 Michael Walle
#include "sysbus.h"
26 b4e37d98 Michael Walle
#include "sysemu.h"
27 b4e37d98 Michael Walle
#include "trace.h"
28 b4e37d98 Michael Walle
#include "qemu-error.h"
29 b4e37d98 Michael Walle
#include "blockdev.h"
30 b4e37d98 Michael Walle
#include "sd.h"
31 b4e37d98 Michael Walle
32 b4e37d98 Michael Walle
enum {
33 b4e37d98 Michael Walle
    ENABLE_CMD_TX   = (1<<0),
34 b4e37d98 Michael Walle
    ENABLE_CMD_RX   = (1<<1),
35 b4e37d98 Michael Walle
    ENABLE_DAT_TX   = (1<<2),
36 b4e37d98 Michael Walle
    ENABLE_DAT_RX   = (1<<3),
37 b4e37d98 Michael Walle
};
38 b4e37d98 Michael Walle
39 b4e37d98 Michael Walle
enum {
40 b4e37d98 Michael Walle
    PENDING_CMD_TX   = (1<<0),
41 b4e37d98 Michael Walle
    PENDING_CMD_RX   = (1<<1),
42 b4e37d98 Michael Walle
    PENDING_DAT_TX   = (1<<2),
43 b4e37d98 Michael Walle
    PENDING_DAT_RX   = (1<<3),
44 b4e37d98 Michael Walle
};
45 b4e37d98 Michael Walle
46 b4e37d98 Michael Walle
enum {
47 b4e37d98 Michael Walle
    START_CMD_TX    = (1<<0),
48 b4e37d98 Michael Walle
    START_DAT_RX    = (1<<1),
49 b4e37d98 Michael Walle
};
50 b4e37d98 Michael Walle
51 b4e37d98 Michael Walle
enum {
52 b4e37d98 Michael Walle
    R_CLK2XDIV = 0,
53 b4e37d98 Michael Walle
    R_ENABLE,
54 b4e37d98 Michael Walle
    R_PENDING,
55 b4e37d98 Michael Walle
    R_START,
56 b4e37d98 Michael Walle
    R_CMD,
57 b4e37d98 Michael Walle
    R_DAT,
58 b4e37d98 Michael Walle
    R_MAX
59 b4e37d98 Michael Walle
};
60 b4e37d98 Michael Walle
61 b4e37d98 Michael Walle
struct MilkymistMemcardState {
62 b4e37d98 Michael Walle
    SysBusDevice busdev;
63 b4e37d98 Michael Walle
    SDState *card;
64 b4e37d98 Michael Walle
65 b4e37d98 Michael Walle
    int command_write_ptr;
66 b4e37d98 Michael Walle
    int response_read_ptr;
67 b4e37d98 Michael Walle
    int response_len;
68 b4e37d98 Michael Walle
    int ignore_next_cmd;
69 b4e37d98 Michael Walle
    int enabled;
70 b4e37d98 Michael Walle
    uint8_t command[6];
71 b4e37d98 Michael Walle
    uint8_t response[17];
72 b4e37d98 Michael Walle
    uint32_t regs[R_MAX];
73 b4e37d98 Michael Walle
};
74 b4e37d98 Michael Walle
typedef struct MilkymistMemcardState MilkymistMemcardState;
75 b4e37d98 Michael Walle
76 b4e37d98 Michael Walle
static void update_pending_bits(MilkymistMemcardState *s)
77 b4e37d98 Michael Walle
{
78 b4e37d98 Michael Walle
    /* transmits are instantaneous, thus tx pending bits are never set */
79 b4e37d98 Michael Walle
    s->regs[R_PENDING] = 0;
80 b4e37d98 Michael Walle
    /* if rx is enabled the corresponding pending bits are always set */
81 b4e37d98 Michael Walle
    if (s->regs[R_ENABLE] & ENABLE_CMD_RX) {
82 b4e37d98 Michael Walle
        s->regs[R_PENDING] |= PENDING_CMD_RX;
83 b4e37d98 Michael Walle
    }
84 b4e37d98 Michael Walle
    if (s->regs[R_ENABLE] & ENABLE_DAT_RX) {
85 b4e37d98 Michael Walle
        s->regs[R_PENDING] |= PENDING_DAT_RX;
86 b4e37d98 Michael Walle
    }
87 b4e37d98 Michael Walle
}
88 b4e37d98 Michael Walle
89 b4e37d98 Michael Walle
static void memcard_sd_command(MilkymistMemcardState *s)
90 b4e37d98 Michael Walle
{
91 b4e37d98 Michael Walle
    SDRequest req;
92 b4e37d98 Michael Walle
93 b4e37d98 Michael Walle
    req.cmd = s->command[0] & 0x3f;
94 b4e37d98 Michael Walle
    req.arg = (s->command[1] << 24) | (s->command[2] << 16)
95 b4e37d98 Michael Walle
              | (s->command[3] << 8) | s->command[4];
96 b4e37d98 Michael Walle
    req.crc = s->command[5];
97 b4e37d98 Michael Walle
98 b4e37d98 Michael Walle
    s->response[0] = req.cmd;
99 b4e37d98 Michael Walle
    s->response_len = sd_do_command(s->card, &req, s->response+1);
100 b4e37d98 Michael Walle
    s->response_read_ptr = 0;
101 b4e37d98 Michael Walle
102 b4e37d98 Michael Walle
    if (s->response_len == 16) {
103 b4e37d98 Michael Walle
        /* R2 response */
104 b4e37d98 Michael Walle
        s->response[0] = 0x3f;
105 b4e37d98 Michael Walle
        s->response_len += 1;
106 b4e37d98 Michael Walle
    } else if (s->response_len == 4) {
107 b4e37d98 Michael Walle
        /* no crc calculation, insert dummy byte */
108 b4e37d98 Michael Walle
        s->response[5] = 0;
109 b4e37d98 Michael Walle
        s->response_len += 2;
110 b4e37d98 Michael Walle
    }
111 b4e37d98 Michael Walle
112 b4e37d98 Michael Walle
    if (req.cmd == 0) {
113 b4e37d98 Michael Walle
        /* next write is a dummy byte to clock the initialization of the sd
114 b4e37d98 Michael Walle
         * card */
115 b4e37d98 Michael Walle
        s->ignore_next_cmd = 1;
116 b4e37d98 Michael Walle
    }
117 b4e37d98 Michael Walle
}
118 b4e37d98 Michael Walle
119 b4e37d98 Michael Walle
static uint32_t memcard_read(void *opaque, target_phys_addr_t addr)
120 b4e37d98 Michael Walle
{
121 b4e37d98 Michael Walle
    MilkymistMemcardState *s = opaque;
122 b4e37d98 Michael Walle
    uint32_t r = 0;
123 b4e37d98 Michael Walle
124 b4e37d98 Michael Walle
    addr >>= 2;
125 b4e37d98 Michael Walle
    switch (addr) {
126 b4e37d98 Michael Walle
    case R_CMD:
127 b4e37d98 Michael Walle
        if (!s->enabled) {
128 b4e37d98 Michael Walle
            r = 0xff;
129 b4e37d98 Michael Walle
        } else {
130 b4e37d98 Michael Walle
            r = s->response[s->response_read_ptr++];
131 b4e37d98 Michael Walle
            if (s->response_read_ptr > s->response_len) {
132 b4e37d98 Michael Walle
                error_report("milkymist_memcard: "
133 b4e37d98 Michael Walle
                        "read more cmd bytes than available. Clipping.");
134 b4e37d98 Michael Walle
                s->response_read_ptr = 0;
135 b4e37d98 Michael Walle
            }
136 b4e37d98 Michael Walle
        }
137 b4e37d98 Michael Walle
        break;
138 b4e37d98 Michael Walle
    case R_DAT:
139 b4e37d98 Michael Walle
        if (!s->enabled) {
140 b4e37d98 Michael Walle
            r = 0xffffffff;
141 b4e37d98 Michael Walle
        } else {
142 b4e37d98 Michael Walle
            r = 0;
143 b4e37d98 Michael Walle
            r |= sd_read_data(s->card) << 24;
144 b4e37d98 Michael Walle
            r |= sd_read_data(s->card) << 16;
145 b4e37d98 Michael Walle
            r |= sd_read_data(s->card) << 8;
146 b4e37d98 Michael Walle
            r |= sd_read_data(s->card);
147 b4e37d98 Michael Walle
        }
148 b4e37d98 Michael Walle
        break;
149 b4e37d98 Michael Walle
    case R_CLK2XDIV:
150 b4e37d98 Michael Walle
    case R_ENABLE:
151 b4e37d98 Michael Walle
    case R_PENDING:
152 b4e37d98 Michael Walle
    case R_START:
153 b4e37d98 Michael Walle
        r = s->regs[addr];
154 b4e37d98 Michael Walle
        break;
155 b4e37d98 Michael Walle
156 b4e37d98 Michael Walle
    default:
157 b4e37d98 Michael Walle
        error_report("milkymist_memcard: read access to unkown register 0x"
158 b4e37d98 Michael Walle
                TARGET_FMT_plx, addr << 2);
159 b4e37d98 Michael Walle
        break;
160 b4e37d98 Michael Walle
    }
161 b4e37d98 Michael Walle
162 b4e37d98 Michael Walle
    trace_milkymist_memcard_memory_read(addr << 2, r);
163 b4e37d98 Michael Walle
164 b4e37d98 Michael Walle
    return r;
165 b4e37d98 Michael Walle
}
166 b4e37d98 Michael Walle
167 b4e37d98 Michael Walle
static void memcard_write(void *opaque, target_phys_addr_t addr, uint32_t value)
168 b4e37d98 Michael Walle
{
169 b4e37d98 Michael Walle
    MilkymistMemcardState *s = opaque;
170 b4e37d98 Michael Walle
171 b4e37d98 Michael Walle
    trace_milkymist_memcard_memory_write(addr, value);
172 b4e37d98 Michael Walle
173 b4e37d98 Michael Walle
    addr >>= 2;
174 b4e37d98 Michael Walle
    switch (addr) {
175 b4e37d98 Michael Walle
    case R_PENDING:
176 b4e37d98 Michael Walle
        /* clear rx pending bits */
177 b4e37d98 Michael Walle
        s->regs[R_PENDING] &= ~(value & (PENDING_CMD_RX | PENDING_DAT_RX));
178 b4e37d98 Michael Walle
        update_pending_bits(s);
179 b4e37d98 Michael Walle
        break;
180 b4e37d98 Michael Walle
    case R_CMD:
181 b4e37d98 Michael Walle
        if (!s->enabled) {
182 b4e37d98 Michael Walle
            break;
183 b4e37d98 Michael Walle
        }
184 b4e37d98 Michael Walle
        if (s->ignore_next_cmd) {
185 b4e37d98 Michael Walle
            s->ignore_next_cmd = 0;
186 b4e37d98 Michael Walle
            break;
187 b4e37d98 Michael Walle
        }
188 b4e37d98 Michael Walle
        s->command[s->command_write_ptr] = value & 0xff;
189 b4e37d98 Michael Walle
        s->command_write_ptr = (s->command_write_ptr + 1) % 6;
190 b4e37d98 Michael Walle
        if (s->command_write_ptr == 0) {
191 b4e37d98 Michael Walle
            memcard_sd_command(s);
192 b4e37d98 Michael Walle
        }
193 b4e37d98 Michael Walle
        break;
194 b4e37d98 Michael Walle
    case R_DAT:
195 b4e37d98 Michael Walle
        if (!s->enabled) {
196 b4e37d98 Michael Walle
            break;
197 b4e37d98 Michael Walle
        }
198 b4e37d98 Michael Walle
        sd_write_data(s->card, (value >> 24) & 0xff);
199 b4e37d98 Michael Walle
        sd_write_data(s->card, (value >> 16) & 0xff);
200 b4e37d98 Michael Walle
        sd_write_data(s->card, (value >> 8) & 0xff);
201 b4e37d98 Michael Walle
        sd_write_data(s->card, value & 0xff);
202 b4e37d98 Michael Walle
        break;
203 b4e37d98 Michael Walle
    case R_ENABLE:
204 b4e37d98 Michael Walle
        s->regs[addr] = value;
205 b4e37d98 Michael Walle
        update_pending_bits(s);
206 b4e37d98 Michael Walle
        break;
207 b4e37d98 Michael Walle
    case R_CLK2XDIV:
208 b4e37d98 Michael Walle
    case R_START:
209 b4e37d98 Michael Walle
        s->regs[addr] = value;
210 b4e37d98 Michael Walle
        break;
211 b4e37d98 Michael Walle
212 b4e37d98 Michael Walle
    default:
213 b4e37d98 Michael Walle
        error_report("milkymist_memcard: write access to unkown register 0x"
214 b4e37d98 Michael Walle
                TARGET_FMT_plx, addr << 2);
215 b4e37d98 Michael Walle
        break;
216 b4e37d98 Michael Walle
    }
217 b4e37d98 Michael Walle
}
218 b4e37d98 Michael Walle
219 b4e37d98 Michael Walle
static CPUReadMemoryFunc * const memcard_read_fn[] = {
220 b4e37d98 Michael Walle
    NULL,
221 b4e37d98 Michael Walle
    NULL,
222 b4e37d98 Michael Walle
    &memcard_read,
223 b4e37d98 Michael Walle
};
224 b4e37d98 Michael Walle
225 b4e37d98 Michael Walle
static CPUWriteMemoryFunc * const memcard_write_fn[] = {
226 b4e37d98 Michael Walle
    NULL,
227 b4e37d98 Michael Walle
    NULL,
228 b4e37d98 Michael Walle
    &memcard_write,
229 b4e37d98 Michael Walle
};
230 b4e37d98 Michael Walle
231 b4e37d98 Michael Walle
static void milkymist_memcard_reset(DeviceState *d)
232 b4e37d98 Michael Walle
{
233 b4e37d98 Michael Walle
    MilkymistMemcardState *s =
234 b4e37d98 Michael Walle
            container_of(d, MilkymistMemcardState, busdev.qdev);
235 b4e37d98 Michael Walle
    int i;
236 b4e37d98 Michael Walle
237 b4e37d98 Michael Walle
    s->command_write_ptr = 0;
238 b4e37d98 Michael Walle
    s->response_read_ptr = 0;
239 b4e37d98 Michael Walle
    s->response_len = 0;
240 b4e37d98 Michael Walle
241 b4e37d98 Michael Walle
    for (i = 0; i < R_MAX; i++) {
242 b4e37d98 Michael Walle
        s->regs[i] = 0;
243 b4e37d98 Michael Walle
    }
244 b4e37d98 Michael Walle
}
245 b4e37d98 Michael Walle
246 b4e37d98 Michael Walle
static int milkymist_memcard_init(SysBusDevice *dev)
247 b4e37d98 Michael Walle
{
248 b4e37d98 Michael Walle
    MilkymistMemcardState *s = FROM_SYSBUS(typeof(*s), dev);
249 b4e37d98 Michael Walle
    DriveInfo *dinfo;
250 b4e37d98 Michael Walle
    int memcard_regs;
251 b4e37d98 Michael Walle
252 b4e37d98 Michael Walle
    dinfo = drive_get_next(IF_SD);
253 b4e37d98 Michael Walle
    s->card = sd_init(dinfo ? dinfo->bdrv : NULL, 0);
254 b4e37d98 Michael Walle
    s->enabled = dinfo ? bdrv_is_inserted(dinfo->bdrv) : 0;
255 b4e37d98 Michael Walle
256 b4e37d98 Michael Walle
    memcard_regs = cpu_register_io_memory(memcard_read_fn, memcard_write_fn, s,
257 b4e37d98 Michael Walle
            DEVICE_NATIVE_ENDIAN);
258 b4e37d98 Michael Walle
    sysbus_init_mmio(dev, R_MAX * 4, memcard_regs);
259 b4e37d98 Michael Walle
260 b4e37d98 Michael Walle
    return 0;
261 b4e37d98 Michael Walle
}
262 b4e37d98 Michael Walle
263 b4e37d98 Michael Walle
static const VMStateDescription vmstate_milkymist_memcard = {
264 b4e37d98 Michael Walle
    .name = "milkymist-memcard",
265 b4e37d98 Michael Walle
    .version_id = 1,
266 b4e37d98 Michael Walle
    .minimum_version_id = 1,
267 b4e37d98 Michael Walle
    .minimum_version_id_old = 1,
268 b4e37d98 Michael Walle
    .fields      = (VMStateField[]) {
269 b4e37d98 Michael Walle
        VMSTATE_INT32(command_write_ptr, MilkymistMemcardState),
270 b4e37d98 Michael Walle
        VMSTATE_INT32(response_read_ptr, MilkymistMemcardState),
271 b4e37d98 Michael Walle
        VMSTATE_INT32(response_len, MilkymistMemcardState),
272 b4e37d98 Michael Walle
        VMSTATE_INT32(ignore_next_cmd, MilkymistMemcardState),
273 b4e37d98 Michael Walle
        VMSTATE_INT32(enabled, MilkymistMemcardState),
274 b4e37d98 Michael Walle
        VMSTATE_UINT8_ARRAY(command, MilkymistMemcardState, 6),
275 b4e37d98 Michael Walle
        VMSTATE_UINT8_ARRAY(response, MilkymistMemcardState, 17),
276 b4e37d98 Michael Walle
        VMSTATE_UINT32_ARRAY(regs, MilkymistMemcardState, R_MAX),
277 b4e37d98 Michael Walle
        VMSTATE_END_OF_LIST()
278 b4e37d98 Michael Walle
    }
279 b4e37d98 Michael Walle
};
280 b4e37d98 Michael Walle
281 b4e37d98 Michael Walle
static SysBusDeviceInfo milkymist_memcard_info = {
282 b4e37d98 Michael Walle
    .init = milkymist_memcard_init,
283 b4e37d98 Michael Walle
    .qdev.name  = "milkymist-memcard",
284 b4e37d98 Michael Walle
    .qdev.size  = sizeof(MilkymistMemcardState),
285 b4e37d98 Michael Walle
    .qdev.vmsd  = &vmstate_milkymist_memcard,
286 b4e37d98 Michael Walle
    .qdev.reset = milkymist_memcard_reset,
287 b4e37d98 Michael Walle
};
288 b4e37d98 Michael Walle
289 b4e37d98 Michael Walle
static void milkymist_memcard_register(void)
290 b4e37d98 Michael Walle
{
291 b4e37d98 Michael Walle
    sysbus_register_withprop(&milkymist_memcard_info);
292 b4e37d98 Michael Walle
}
293 b4e37d98 Michael Walle
294 b4e37d98 Michael Walle
device_init(milkymist_memcard_register)