Statistics
| Branch: | Revision:

root / hw / milkymist-minimac2.c @ ae2ebad7

History | View | Annotate | Download (13.6 kB)

1 07424544 Michael Walle
/*
2 57aa265d Michael Walle
 *  QEMU model of the Milkymist minimac2 block.
3 07424544 Michael Walle
 *
4 57aa265d Michael Walle
 *  Copyright (c) 2011 Michael Walle <michael@walle.cc>
5 07424544 Michael Walle
 *
6 07424544 Michael Walle
 * This library is free software; you can redistribute it and/or
7 07424544 Michael Walle
 * modify it under the terms of the GNU Lesser General Public
8 07424544 Michael Walle
 * License as published by the Free Software Foundation; either
9 07424544 Michael Walle
 * version 2 of the License, or (at your option) any later version.
10 07424544 Michael Walle
 *
11 07424544 Michael Walle
 * This library is distributed in the hope that it will be useful,
12 07424544 Michael Walle
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 07424544 Michael Walle
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 07424544 Michael Walle
 * Lesser General Public License for more details.
15 07424544 Michael Walle
 *
16 07424544 Michael Walle
 * You should have received a copy of the GNU Lesser General Public
17 07424544 Michael Walle
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 07424544 Michael Walle
 *
19 07424544 Michael Walle
 *
20 07424544 Michael Walle
 * Specification available at:
21 57aa265d Michael Walle
 *   not available yet
22 07424544 Michael Walle
 *
23 07424544 Michael Walle
 */
24 07424544 Michael Walle
25 07424544 Michael Walle
#include "hw.h"
26 07424544 Michael Walle
#include "sysbus.h"
27 07424544 Michael Walle
#include "trace.h"
28 07424544 Michael Walle
#include "net.h"
29 07424544 Michael Walle
#include "qemu-error.h"
30 57aa265d Michael Walle
#include "qdev-addr.h"
31 07424544 Michael Walle
32 07424544 Michael Walle
#include <zlib.h>
33 07424544 Michael Walle
34 07424544 Michael Walle
enum {
35 07424544 Michael Walle
    R_SETUP = 0,
36 07424544 Michael Walle
    R_MDIO,
37 07424544 Michael Walle
    R_STATE0,
38 07424544 Michael Walle
    R_COUNT0,
39 07424544 Michael Walle
    R_STATE1,
40 07424544 Michael Walle
    R_COUNT1,
41 07424544 Michael Walle
    R_TXCOUNT,
42 07424544 Michael Walle
    R_MAX
43 07424544 Michael Walle
};
44 07424544 Michael Walle
45 07424544 Michael Walle
enum {
46 57aa265d Michael Walle
    SETUP_PHY_RST = (1<<0),
47 07424544 Michael Walle
};
48 07424544 Michael Walle
49 07424544 Michael Walle
enum {
50 07424544 Michael Walle
    MDIO_DO  = (1<<0),
51 07424544 Michael Walle
    MDIO_DI  = (1<<1),
52 07424544 Michael Walle
    MDIO_OE  = (1<<2),
53 07424544 Michael Walle
    MDIO_CLK = (1<<3),
54 07424544 Michael Walle
};
55 07424544 Michael Walle
56 07424544 Michael Walle
enum {
57 07424544 Michael Walle
    STATE_EMPTY   = 0,
58 07424544 Michael Walle
    STATE_LOADED  = 1,
59 07424544 Michael Walle
    STATE_PENDING = 2,
60 07424544 Michael Walle
};
61 07424544 Michael Walle
62 07424544 Michael Walle
enum {
63 07424544 Michael Walle
    MDIO_OP_WRITE = 1,
64 07424544 Michael Walle
    MDIO_OP_READ  = 2,
65 07424544 Michael Walle
};
66 07424544 Michael Walle
67 07424544 Michael Walle
enum mdio_state {
68 07424544 Michael Walle
    MDIO_STATE_IDLE,
69 07424544 Michael Walle
    MDIO_STATE_READING,
70 07424544 Michael Walle
    MDIO_STATE_WRITING,
71 07424544 Michael Walle
};
72 07424544 Michael Walle
73 07424544 Michael Walle
enum {
74 07424544 Michael Walle
    R_PHY_ID1  = 2,
75 07424544 Michael Walle
    R_PHY_ID2  = 3,
76 07424544 Michael Walle
    R_PHY_MAX  = 32
77 07424544 Michael Walle
};
78 07424544 Michael Walle
79 57aa265d Michael Walle
#define MINIMAC2_MTU 1530
80 57aa265d Michael Walle
#define MINIMAC2_BUFFER_SIZE 2048
81 07424544 Michael Walle
82 57aa265d Michael Walle
struct MilkymistMinimac2MdioState {
83 07424544 Michael Walle
    int last_clk;
84 07424544 Michael Walle
    int count;
85 07424544 Michael Walle
    uint32_t data;
86 07424544 Michael Walle
    uint16_t data_out;
87 07424544 Michael Walle
    int state;
88 07424544 Michael Walle
89 07424544 Michael Walle
    uint8_t phy_addr;
90 07424544 Michael Walle
    uint8_t reg_addr;
91 07424544 Michael Walle
};
92 57aa265d Michael Walle
typedef struct MilkymistMinimac2MdioState MilkymistMinimac2MdioState;
93 07424544 Michael Walle
94 57aa265d Michael Walle
struct MilkymistMinimac2State {
95 07424544 Michael Walle
    SysBusDevice busdev;
96 07424544 Michael Walle
    NICState *nic;
97 07424544 Michael Walle
    NICConf conf;
98 07424544 Michael Walle
    char *phy_model;
99 57aa265d Michael Walle
    target_phys_addr_t buffers_base;
100 07424544 Michael Walle
101 07424544 Michael Walle
    qemu_irq rx_irq;
102 07424544 Michael Walle
    qemu_irq tx_irq;
103 07424544 Michael Walle
104 07424544 Michael Walle
    uint32_t regs[R_MAX];
105 07424544 Michael Walle
106 57aa265d Michael Walle
    MilkymistMinimac2MdioState mdio;
107 07424544 Michael Walle
108 07424544 Michael Walle
    uint16_t phy_regs[R_PHY_MAX];
109 57aa265d Michael Walle
110 57aa265d Michael Walle
    uint8_t *rx0_buf;
111 57aa265d Michael Walle
    uint8_t *rx1_buf;
112 57aa265d Michael Walle
    uint8_t *tx_buf;
113 07424544 Michael Walle
};
114 57aa265d Michael Walle
typedef struct MilkymistMinimac2State MilkymistMinimac2State;
115 07424544 Michael Walle
116 07424544 Michael Walle
static const uint8_t preamble_sfd[] = {
117 07424544 Michael Walle
        0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xd5
118 07424544 Michael Walle
};
119 07424544 Michael Walle
120 57aa265d Michael Walle
static void minimac2_mdio_write_reg(MilkymistMinimac2State *s,
121 07424544 Michael Walle
        uint8_t phy_addr, uint8_t reg_addr, uint16_t value)
122 07424544 Michael Walle
{
123 57aa265d Michael Walle
    trace_milkymist_minimac2_mdio_write(phy_addr, reg_addr, value);
124 07424544 Michael Walle
125 07424544 Michael Walle
    /* nop */
126 07424544 Michael Walle
}
127 07424544 Michael Walle
128 57aa265d Michael Walle
static uint16_t minimac2_mdio_read_reg(MilkymistMinimac2State *s,
129 07424544 Michael Walle
        uint8_t phy_addr, uint8_t reg_addr)
130 07424544 Michael Walle
{
131 07424544 Michael Walle
    uint16_t r = s->phy_regs[reg_addr];
132 07424544 Michael Walle
133 57aa265d Michael Walle
    trace_milkymist_minimac2_mdio_read(phy_addr, reg_addr, r);
134 07424544 Michael Walle
135 07424544 Michael Walle
    return r;
136 07424544 Michael Walle
}
137 07424544 Michael Walle
138 57aa265d Michael Walle
static void minimac2_update_mdio(MilkymistMinimac2State *s)
139 07424544 Michael Walle
{
140 57aa265d Michael Walle
    MilkymistMinimac2MdioState *m = &s->mdio;
141 07424544 Michael Walle
142 07424544 Michael Walle
    /* detect rising clk edge */
143 07424544 Michael Walle
    if (m->last_clk == 0 && (s->regs[R_MDIO] & MDIO_CLK)) {
144 07424544 Michael Walle
        /* shift data in */
145 07424544 Michael Walle
        int bit = ((s->regs[R_MDIO] & MDIO_DO)
146 07424544 Michael Walle
                   && (s->regs[R_MDIO] & MDIO_OE)) ? 1 : 0;
147 07424544 Michael Walle
        m->data = (m->data << 1) | bit;
148 07424544 Michael Walle
149 07424544 Michael Walle
        /* check for sync */
150 07424544 Michael Walle
        if (m->data == 0xffffffff) {
151 07424544 Michael Walle
            m->count = 32;
152 07424544 Michael Walle
        }
153 07424544 Michael Walle
154 07424544 Michael Walle
        if (m->count == 16) {
155 07424544 Michael Walle
            uint8_t start = (m->data >> 14) & 0x3;
156 07424544 Michael Walle
            uint8_t op = (m->data >> 12) & 0x3;
157 07424544 Michael Walle
            uint8_t ta = (m->data) & 0x3;
158 07424544 Michael Walle
159 07424544 Michael Walle
            if (start == 1 && op == MDIO_OP_WRITE && ta == 2) {
160 07424544 Michael Walle
                m->state = MDIO_STATE_WRITING;
161 07424544 Michael Walle
            } else if (start == 1 && op == MDIO_OP_READ && (ta & 1) == 0) {
162 07424544 Michael Walle
                m->state = MDIO_STATE_READING;
163 07424544 Michael Walle
            } else {
164 07424544 Michael Walle
                m->state = MDIO_STATE_IDLE;
165 07424544 Michael Walle
            }
166 07424544 Michael Walle
167 07424544 Michael Walle
            if (m->state != MDIO_STATE_IDLE) {
168 07424544 Michael Walle
                m->phy_addr = (m->data >> 7) & 0x1f;
169 07424544 Michael Walle
                m->reg_addr = (m->data >> 2) & 0x1f;
170 07424544 Michael Walle
            }
171 07424544 Michael Walle
172 07424544 Michael Walle
            if (m->state == MDIO_STATE_READING) {
173 57aa265d Michael Walle
                m->data_out = minimac2_mdio_read_reg(s, m->phy_addr,
174 07424544 Michael Walle
                        m->reg_addr);
175 07424544 Michael Walle
            }
176 07424544 Michael Walle
        }
177 07424544 Michael Walle
178 07424544 Michael Walle
        if (m->count < 16 && m->state == MDIO_STATE_READING) {
179 07424544 Michael Walle
            int bit = (m->data_out & 0x8000) ? 1 : 0;
180 07424544 Michael Walle
            m->data_out <<= 1;
181 07424544 Michael Walle
182 07424544 Michael Walle
            if (bit) {
183 07424544 Michael Walle
                s->regs[R_MDIO] |= MDIO_DI;
184 07424544 Michael Walle
            } else {
185 07424544 Michael Walle
                s->regs[R_MDIO] &= ~MDIO_DI;
186 07424544 Michael Walle
            }
187 07424544 Michael Walle
        }
188 07424544 Michael Walle
189 07424544 Michael Walle
        if (m->count == 0 && m->state) {
190 07424544 Michael Walle
            if (m->state == MDIO_STATE_WRITING) {
191 07424544 Michael Walle
                uint16_t data = m->data & 0xffff;
192 57aa265d Michael Walle
                minimac2_mdio_write_reg(s, m->phy_addr, m->reg_addr, data);
193 07424544 Michael Walle
            }
194 07424544 Michael Walle
            m->state = MDIO_STATE_IDLE;
195 07424544 Michael Walle
        }
196 07424544 Michael Walle
        m->count--;
197 07424544 Michael Walle
    }
198 07424544 Michael Walle
199 07424544 Michael Walle
    m->last_clk = (s->regs[R_MDIO] & MDIO_CLK) ? 1 : 0;
200 07424544 Michael Walle
}
201 07424544 Michael Walle
202 07424544 Michael Walle
static size_t assemble_frame(uint8_t *buf, size_t size,
203 07424544 Michael Walle
        const uint8_t *payload, size_t payload_size)
204 07424544 Michael Walle
{
205 07424544 Michael Walle
    uint32_t crc;
206 07424544 Michael Walle
207 07424544 Michael Walle
    if (size < payload_size + 12) {
208 57aa265d Michael Walle
        error_report("milkymist_minimac2: received too big ethernet frame");
209 07424544 Michael Walle
        return 0;
210 07424544 Michael Walle
    }
211 07424544 Michael Walle
212 07424544 Michael Walle
    /* prepend preamble and sfd */
213 07424544 Michael Walle
    memcpy(buf, preamble_sfd, 8);
214 07424544 Michael Walle
215 07424544 Michael Walle
    /* now copy the payload */
216 07424544 Michael Walle
    memcpy(buf + 8, payload, payload_size);
217 07424544 Michael Walle
218 07424544 Michael Walle
    /* pad frame if needed */
219 07424544 Michael Walle
    if (payload_size < 60) {
220 07424544 Michael Walle
        memset(buf + payload_size + 8, 0, 60 - payload_size);
221 07424544 Michael Walle
        payload_size = 60;
222 07424544 Michael Walle
    }
223 07424544 Michael Walle
224 07424544 Michael Walle
    /* append fcs */
225 07424544 Michael Walle
    crc = cpu_to_le32(crc32(0, buf + 8, payload_size));
226 07424544 Michael Walle
    memcpy(buf + payload_size + 8, &crc, 4);
227 07424544 Michael Walle
228 07424544 Michael Walle
    return payload_size + 12;
229 07424544 Michael Walle
}
230 07424544 Michael Walle
231 57aa265d Michael Walle
static void minimac2_tx(MilkymistMinimac2State *s)
232 07424544 Michael Walle
{
233 07424544 Michael Walle
    uint32_t txcount = s->regs[R_TXCOUNT];
234 57aa265d Michael Walle
    uint8_t *buf = s->tx_buf;
235 07424544 Michael Walle
236 07424544 Michael Walle
    if (txcount < 64) {
237 57aa265d Michael Walle
        error_report("milkymist_minimac2: ethernet frame too small (%u < %u)\n",
238 07424544 Michael Walle
                txcount, 64);
239 57aa265d Michael Walle
        goto err;
240 07424544 Michael Walle
    }
241 07424544 Michael Walle
242 57aa265d Michael Walle
    if (txcount > MINIMAC2_MTU) {
243 57aa265d Michael Walle
        error_report("milkymist_minimac2: MTU exceeded (%u > %u)\n",
244 57aa265d Michael Walle
                txcount, MINIMAC2_MTU);
245 57aa265d Michael Walle
        goto err;
246 07424544 Michael Walle
    }
247 07424544 Michael Walle
248 07424544 Michael Walle
    if (memcmp(buf, preamble_sfd, 8) != 0) {
249 57aa265d Michael Walle
        error_report("milkymist_minimac2: frame doesn't contain the preamble "
250 07424544 Michael Walle
                "and/or the SFD (%02x %02x %02x %02x %02x %02x %02x %02x)\n",
251 07424544 Michael Walle
                buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
252 57aa265d Michael Walle
        goto err;
253 07424544 Michael Walle
    }
254 07424544 Michael Walle
255 57aa265d Michael Walle
    trace_milkymist_minimac2_tx_frame(txcount - 12);
256 07424544 Michael Walle
257 07424544 Michael Walle
    /* send packet, skipping preamble and sfd */
258 07424544 Michael Walle
    qemu_send_packet_raw(&s->nic->nc, buf + 8, txcount - 12);
259 07424544 Michael Walle
260 07424544 Michael Walle
    s->regs[R_TXCOUNT] = 0;
261 07424544 Michael Walle
262 57aa265d Michael Walle
err:
263 57aa265d Michael Walle
    trace_milkymist_minimac2_pulse_irq_tx();
264 07424544 Michael Walle
    qemu_irq_pulse(s->tx_irq);
265 07424544 Michael Walle
}
266 07424544 Michael Walle
267 57aa265d Michael Walle
static void update_rx_interrupt(MilkymistMinimac2State *s)
268 57aa265d Michael Walle
{
269 57aa265d Michael Walle
    if (s->regs[R_STATE0] == STATE_PENDING
270 57aa265d Michael Walle
            || s->regs[R_STATE1] == STATE_PENDING) {
271 57aa265d Michael Walle
        trace_milkymist_minimac2_raise_irq_rx();
272 57aa265d Michael Walle
        qemu_irq_raise(s->rx_irq);
273 57aa265d Michael Walle
    } else {
274 57aa265d Michael Walle
        trace_milkymist_minimac2_lower_irq_rx();
275 57aa265d Michael Walle
        qemu_irq_lower(s->rx_irq);
276 57aa265d Michael Walle
    }
277 57aa265d Michael Walle
}
278 57aa265d Michael Walle
279 57aa265d Michael Walle
static ssize_t minimac2_rx(VLANClientState *nc, const uint8_t *buf, size_t size)
280 07424544 Michael Walle
{
281 57aa265d Michael Walle
    MilkymistMinimac2State *s = DO_UPCAST(NICState, nc, nc)->opaque;
282 07424544 Michael Walle
283 07424544 Michael Walle
    uint32_t r_count;
284 07424544 Michael Walle
    uint32_t r_state;
285 57aa265d Michael Walle
    uint8_t *rx_buf;
286 07424544 Michael Walle
287 07424544 Michael Walle
    size_t frame_size;
288 07424544 Michael Walle
289 57aa265d Michael Walle
    trace_milkymist_minimac2_rx_frame(buf, size);
290 07424544 Michael Walle
291 07424544 Michael Walle
    /* choose appropriate slot */
292 07424544 Michael Walle
    if (s->regs[R_STATE0] == STATE_LOADED) {
293 07424544 Michael Walle
        r_count = R_COUNT0;
294 07424544 Michael Walle
        r_state = R_STATE0;
295 57aa265d Michael Walle
        rx_buf = s->rx0_buf;
296 07424544 Michael Walle
    } else if (s->regs[R_STATE1] == STATE_LOADED) {
297 07424544 Michael Walle
        r_count = R_COUNT1;
298 07424544 Michael Walle
        r_state = R_STATE1;
299 57aa265d Michael Walle
        rx_buf = s->rx1_buf;
300 07424544 Michael Walle
    } else {
301 57aa265d Michael Walle
        trace_milkymist_minimac2_drop_rx_frame(buf);
302 07424544 Michael Walle
        return size;
303 07424544 Michael Walle
    }
304 07424544 Michael Walle
305 07424544 Michael Walle
    /* assemble frame */
306 57aa265d Michael Walle
    frame_size = assemble_frame(rx_buf, MINIMAC2_BUFFER_SIZE, buf, size);
307 07424544 Michael Walle
308 07424544 Michael Walle
    if (frame_size == 0) {
309 07424544 Michael Walle
        return size;
310 07424544 Michael Walle
    }
311 07424544 Michael Walle
312 57aa265d Michael Walle
    trace_milkymist_minimac2_rx_transfer(rx_buf, frame_size);
313 07424544 Michael Walle
314 07424544 Michael Walle
    /* update slot */
315 07424544 Michael Walle
    s->regs[r_count] = frame_size;
316 07424544 Michael Walle
    s->regs[r_state] = STATE_PENDING;
317 07424544 Michael Walle
318 57aa265d Michael Walle
    update_rx_interrupt(s);
319 07424544 Michael Walle
320 07424544 Michael Walle
    return size;
321 07424544 Michael Walle
}
322 07424544 Michael Walle
323 07424544 Michael Walle
static uint32_t
324 57aa265d Michael Walle
minimac2_read(void *opaque, target_phys_addr_t addr)
325 07424544 Michael Walle
{
326 57aa265d Michael Walle
    MilkymistMinimac2State *s = opaque;
327 07424544 Michael Walle
    uint32_t r = 0;
328 07424544 Michael Walle
329 07424544 Michael Walle
    addr >>= 2;
330 07424544 Michael Walle
    switch (addr) {
331 07424544 Michael Walle
    case R_SETUP:
332 07424544 Michael Walle
    case R_MDIO:
333 07424544 Michael Walle
    case R_STATE0:
334 07424544 Michael Walle
    case R_COUNT0:
335 07424544 Michael Walle
    case R_STATE1:
336 07424544 Michael Walle
    case R_COUNT1:
337 07424544 Michael Walle
    case R_TXCOUNT:
338 07424544 Michael Walle
        r = s->regs[addr];
339 07424544 Michael Walle
        break;
340 07424544 Michael Walle
341 07424544 Michael Walle
    default:
342 57aa265d Michael Walle
        error_report("milkymist_minimac2: read access to unknown register 0x"
343 07424544 Michael Walle
                TARGET_FMT_plx, addr << 2);
344 07424544 Michael Walle
        break;
345 07424544 Michael Walle
    }
346 07424544 Michael Walle
347 57aa265d Michael Walle
    trace_milkymist_minimac2_memory_read(addr << 2, r);
348 07424544 Michael Walle
349 07424544 Michael Walle
    return r;
350 07424544 Michael Walle
}
351 07424544 Michael Walle
352 07424544 Michael Walle
static void
353 57aa265d Michael Walle
minimac2_write(void *opaque, target_phys_addr_t addr, uint32_t value)
354 07424544 Michael Walle
{
355 57aa265d Michael Walle
    MilkymistMinimac2State *s = opaque;
356 07424544 Michael Walle
357 57aa265d Michael Walle
    trace_milkymist_minimac2_memory_read(addr, value);
358 07424544 Michael Walle
359 07424544 Michael Walle
    addr >>= 2;
360 07424544 Michael Walle
    switch (addr) {
361 07424544 Michael Walle
    case R_MDIO:
362 07424544 Michael Walle
    {
363 07424544 Michael Walle
        /* MDIO_DI is read only */
364 07424544 Michael Walle
        int mdio_di = (s->regs[R_MDIO] & MDIO_DI);
365 07424544 Michael Walle
        s->regs[R_MDIO] = value;
366 07424544 Michael Walle
        if (mdio_di) {
367 07424544 Michael Walle
            s->regs[R_MDIO] |= mdio_di;
368 07424544 Michael Walle
        } else {
369 07424544 Michael Walle
            s->regs[R_MDIO] &= ~mdio_di;
370 07424544 Michael Walle
        }
371 07424544 Michael Walle
372 57aa265d Michael Walle
        minimac2_update_mdio(s);
373 07424544 Michael Walle
    } break;
374 07424544 Michael Walle
    case R_TXCOUNT:
375 07424544 Michael Walle
        s->regs[addr] = value;
376 07424544 Michael Walle
        if (value > 0) {
377 57aa265d Michael Walle
            minimac2_tx(s);
378 07424544 Michael Walle
        }
379 07424544 Michael Walle
        break;
380 07424544 Michael Walle
    case R_STATE0:
381 07424544 Michael Walle
    case R_STATE1:
382 57aa265d Michael Walle
        s->regs[addr] = value;
383 57aa265d Michael Walle
        update_rx_interrupt(s);
384 57aa265d Michael Walle
        break;
385 57aa265d Michael Walle
    case R_SETUP:
386 57aa265d Michael Walle
    case R_COUNT0:
387 07424544 Michael Walle
    case R_COUNT1:
388 07424544 Michael Walle
        s->regs[addr] = value;
389 07424544 Michael Walle
        break;
390 07424544 Michael Walle
391 07424544 Michael Walle
    default:
392 57aa265d Michael Walle
        error_report("milkymist_minimac2: write access to unknown register 0x"
393 07424544 Michael Walle
                TARGET_FMT_plx, addr << 2);
394 07424544 Michael Walle
        break;
395 07424544 Michael Walle
    }
396 07424544 Michael Walle
}
397 07424544 Michael Walle
398 57aa265d Michael Walle
static CPUReadMemoryFunc * const minimac2_read_fn[] = {
399 07424544 Michael Walle
    NULL,
400 07424544 Michael Walle
    NULL,
401 57aa265d Michael Walle
    &minimac2_read,
402 07424544 Michael Walle
};
403 07424544 Michael Walle
404 57aa265d Michael Walle
static CPUWriteMemoryFunc * const minimac2_write_fn[] = {
405 07424544 Michael Walle
    NULL,
406 07424544 Michael Walle
    NULL,
407 57aa265d Michael Walle
    &minimac2_write,
408 07424544 Michael Walle
};
409 07424544 Michael Walle
410 57aa265d Michael Walle
static int minimac2_can_rx(VLANClientState *nc)
411 07424544 Michael Walle
{
412 57aa265d Michael Walle
    MilkymistMinimac2State *s = DO_UPCAST(NICState, nc, nc)->opaque;
413 07424544 Michael Walle
414 07424544 Michael Walle
    if (s->regs[R_STATE0] == STATE_LOADED) {
415 07424544 Michael Walle
        return 1;
416 07424544 Michael Walle
    }
417 07424544 Michael Walle
    if (s->regs[R_STATE1] == STATE_LOADED) {
418 07424544 Michael Walle
        return 1;
419 07424544 Michael Walle
    }
420 07424544 Michael Walle
421 07424544 Michael Walle
    return 0;
422 07424544 Michael Walle
}
423 07424544 Michael Walle
424 57aa265d Michael Walle
static void minimac2_cleanup(VLANClientState *nc)
425 07424544 Michael Walle
{
426 57aa265d Michael Walle
    MilkymistMinimac2State *s = DO_UPCAST(NICState, nc, nc)->opaque;
427 07424544 Michael Walle
428 07424544 Michael Walle
    s->nic = NULL;
429 07424544 Michael Walle
}
430 07424544 Michael Walle
431 57aa265d Michael Walle
static void milkymist_minimac2_reset(DeviceState *d)
432 07424544 Michael Walle
{
433 57aa265d Michael Walle
    MilkymistMinimac2State *s =
434 57aa265d Michael Walle
            container_of(d, MilkymistMinimac2State, busdev.qdev);
435 07424544 Michael Walle
    int i;
436 07424544 Michael Walle
437 07424544 Michael Walle
    for (i = 0; i < R_MAX; i++) {
438 07424544 Michael Walle
        s->regs[i] = 0;
439 07424544 Michael Walle
    }
440 07424544 Michael Walle
    for (i = 0; i < R_PHY_MAX; i++) {
441 07424544 Michael Walle
        s->phy_regs[i] = 0;
442 07424544 Michael Walle
    }
443 07424544 Michael Walle
444 07424544 Michael Walle
    /* defaults */
445 07424544 Michael Walle
    s->phy_regs[R_PHY_ID1] = 0x0022; /* Micrel KSZ8001L */
446 07424544 Michael Walle
    s->phy_regs[R_PHY_ID2] = 0x161a;
447 07424544 Michael Walle
}
448 07424544 Michael Walle
449 57aa265d Michael Walle
static NetClientInfo net_milkymist_minimac2_info = {
450 07424544 Michael Walle
    .type = NET_CLIENT_TYPE_NIC,
451 07424544 Michael Walle
    .size = sizeof(NICState),
452 57aa265d Michael Walle
    .can_receive = minimac2_can_rx,
453 57aa265d Michael Walle
    .receive = minimac2_rx,
454 57aa265d Michael Walle
    .cleanup = minimac2_cleanup,
455 07424544 Michael Walle
};
456 07424544 Michael Walle
457 57aa265d Michael Walle
static int milkymist_minimac2_init(SysBusDevice *dev)
458 07424544 Michael Walle
{
459 57aa265d Michael Walle
    MilkymistMinimac2State *s = FROM_SYSBUS(typeof(*s), dev);
460 07424544 Michael Walle
    int regs;
461 57aa265d Michael Walle
    ram_addr_t buffers;
462 57aa265d Michael Walle
    size_t buffers_size = TARGET_PAGE_ALIGN(3 * MINIMAC2_BUFFER_SIZE);
463 07424544 Michael Walle
464 07424544 Michael Walle
    sysbus_init_irq(dev, &s->rx_irq);
465 07424544 Michael Walle
    sysbus_init_irq(dev, &s->tx_irq);
466 07424544 Michael Walle
467 57aa265d Michael Walle
    regs = cpu_register_io_memory(minimac2_read_fn, minimac2_write_fn, s,
468 07424544 Michael Walle
            DEVICE_NATIVE_ENDIAN);
469 07424544 Michael Walle
    sysbus_init_mmio(dev, R_MAX * 4, regs);
470 07424544 Michael Walle
471 57aa265d Michael Walle
    /* register buffers memory */
472 57aa265d Michael Walle
    buffers = qemu_ram_alloc(NULL, "milkymist_minimac2.buffers", buffers_size);
473 57aa265d Michael Walle
    s->rx0_buf = qemu_get_ram_ptr(buffers);
474 57aa265d Michael Walle
    s->rx1_buf = s->rx0_buf + MINIMAC2_BUFFER_SIZE;
475 57aa265d Michael Walle
    s->tx_buf = s->rx1_buf + MINIMAC2_BUFFER_SIZE;
476 57aa265d Michael Walle
477 57aa265d Michael Walle
    cpu_register_physical_memory(s->buffers_base, buffers_size,
478 57aa265d Michael Walle
            buffers | IO_MEM_RAM);
479 57aa265d Michael Walle
480 07424544 Michael Walle
    qemu_macaddr_default_if_unset(&s->conf.macaddr);
481 57aa265d Michael Walle
    s->nic = qemu_new_nic(&net_milkymist_minimac2_info, &s->conf,
482 07424544 Michael Walle
                          dev->qdev.info->name, dev->qdev.id, s);
483 07424544 Michael Walle
    qemu_format_nic_info_str(&s->nic->nc, s->conf.macaddr.a);
484 07424544 Michael Walle
485 07424544 Michael Walle
    return 0;
486 07424544 Michael Walle
}
487 07424544 Michael Walle
488 57aa265d Michael Walle
static const VMStateDescription vmstate_milkymist_minimac2_mdio = {
489 57aa265d Michael Walle
    .name = "milkymist-minimac2-mdio",
490 07424544 Michael Walle
    .version_id = 1,
491 07424544 Michael Walle
    .minimum_version_id = 1,
492 07424544 Michael Walle
    .minimum_version_id_old = 1,
493 07424544 Michael Walle
    .fields      = (VMStateField[]) {
494 57aa265d Michael Walle
        VMSTATE_INT32(last_clk, MilkymistMinimac2MdioState),
495 57aa265d Michael Walle
        VMSTATE_INT32(count, MilkymistMinimac2MdioState),
496 57aa265d Michael Walle
        VMSTATE_UINT32(data, MilkymistMinimac2MdioState),
497 57aa265d Michael Walle
        VMSTATE_UINT16(data_out, MilkymistMinimac2MdioState),
498 57aa265d Michael Walle
        VMSTATE_INT32(state, MilkymistMinimac2MdioState),
499 57aa265d Michael Walle
        VMSTATE_UINT8(phy_addr, MilkymistMinimac2MdioState),
500 57aa265d Michael Walle
        VMSTATE_UINT8(reg_addr, MilkymistMinimac2MdioState),
501 07424544 Michael Walle
        VMSTATE_END_OF_LIST()
502 07424544 Michael Walle
    }
503 07424544 Michael Walle
};
504 07424544 Michael Walle
505 57aa265d Michael Walle
static const VMStateDescription vmstate_milkymist_minimac2 = {
506 57aa265d Michael Walle
    .name = "milkymist-minimac2",
507 07424544 Michael Walle
    .version_id = 1,
508 07424544 Michael Walle
    .minimum_version_id = 1,
509 07424544 Michael Walle
    .minimum_version_id_old = 1,
510 07424544 Michael Walle
    .fields      = (VMStateField[]) {
511 57aa265d Michael Walle
        VMSTATE_UINT32_ARRAY(regs, MilkymistMinimac2State, R_MAX),
512 57aa265d Michael Walle
        VMSTATE_UINT16_ARRAY(phy_regs, MilkymistMinimac2State, R_PHY_MAX),
513 57aa265d Michael Walle
        VMSTATE_STRUCT(mdio, MilkymistMinimac2State, 0,
514 57aa265d Michael Walle
                vmstate_milkymist_minimac2_mdio, MilkymistMinimac2MdioState),
515 07424544 Michael Walle
        VMSTATE_END_OF_LIST()
516 07424544 Michael Walle
    }
517 07424544 Michael Walle
};
518 07424544 Michael Walle
519 57aa265d Michael Walle
static SysBusDeviceInfo milkymist_minimac2_info = {
520 57aa265d Michael Walle
    .init = milkymist_minimac2_init,
521 57aa265d Michael Walle
    .qdev.name  = "milkymist-minimac2",
522 57aa265d Michael Walle
    .qdev.size  = sizeof(MilkymistMinimac2State),
523 57aa265d Michael Walle
    .qdev.vmsd  = &vmstate_milkymist_minimac2,
524 57aa265d Michael Walle
    .qdev.reset = milkymist_minimac2_reset,
525 07424544 Michael Walle
    .qdev.props = (Property[]) {
526 57aa265d Michael Walle
        DEFINE_PROP_TADDR("buffers_base", MilkymistMinimac2State,
527 57aa265d Michael Walle
                buffers_base, 0),
528 57aa265d Michael Walle
        DEFINE_NIC_PROPERTIES(MilkymistMinimac2State, conf),
529 57aa265d Michael Walle
        DEFINE_PROP_STRING("phy_model", MilkymistMinimac2State, phy_model),
530 07424544 Michael Walle
        DEFINE_PROP_END_OF_LIST(),
531 07424544 Michael Walle
    }
532 07424544 Michael Walle
};
533 07424544 Michael Walle
534 57aa265d Michael Walle
static void milkymist_minimac2_register(void)
535 07424544 Michael Walle
{
536 57aa265d Michael Walle
    sysbus_register_withprop(&milkymist_minimac2_info);
537 07424544 Michael Walle
}
538 07424544 Michael Walle
539 57aa265d Michael Walle
device_init(milkymist_minimac2_register)