milkymist-minimac2.c 14.0 KB
Newer Older
1
/*
2
 *  QEMU model of the Milkymist minimac2 block.
3
 *
4
 *  Copyright (c) 2011 Michael Walle <michael@walle.cc>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 *
 * Specification available at:
21
 *   not available yet
22 23 24 25 26 27
 *
 */

#include "hw.h"
#include "sysbus.h"
#include "trace.h"
P
Paolo Bonzini 已提交
28
#include "net/net.h"
29
#include "qemu/error-report.h"
30
#include "qdev-addr.h"
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

#include <zlib.h>

enum {
    R_SETUP = 0,
    R_MDIO,
    R_STATE0,
    R_COUNT0,
    R_STATE1,
    R_COUNT1,
    R_TXCOUNT,
    R_MAX
};

enum {
46
    SETUP_PHY_RST = (1<<0),
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
};

enum {
    MDIO_DO  = (1<<0),
    MDIO_DI  = (1<<1),
    MDIO_OE  = (1<<2),
    MDIO_CLK = (1<<3),
};

enum {
    STATE_EMPTY   = 0,
    STATE_LOADED  = 1,
    STATE_PENDING = 2,
};

enum {
    MDIO_OP_WRITE = 1,
    MDIO_OP_READ  = 2,
};

enum mdio_state {
    MDIO_STATE_IDLE,
    MDIO_STATE_READING,
    MDIO_STATE_WRITING,
};

enum {
    R_PHY_ID1  = 2,
    R_PHY_ID2  = 3,
    R_PHY_MAX  = 32
};

79 80
#define MINIMAC2_MTU 1530
#define MINIMAC2_BUFFER_SIZE 2048
81

82
struct MilkymistMinimac2MdioState {
83 84 85 86 87 88 89 90 91
    int last_clk;
    int count;
    uint32_t data;
    uint16_t data_out;
    int state;

    uint8_t phy_addr;
    uint8_t reg_addr;
};
92
typedef struct MilkymistMinimac2MdioState MilkymistMinimac2MdioState;
93

94
struct MilkymistMinimac2State {
95 96 97 98
    SysBusDevice busdev;
    NICState *nic;
    NICConf conf;
    char *phy_model;
A
Avi Kivity 已提交
99
    hwaddr buffers_base;
100 101
    MemoryRegion buffers;
    MemoryRegion regs_region;
102 103 104 105 106 107

    qemu_irq rx_irq;
    qemu_irq tx_irq;

    uint32_t regs[R_MAX];

108
    MilkymistMinimac2MdioState mdio;
109 110

    uint16_t phy_regs[R_PHY_MAX];
111 112 113 114

    uint8_t *rx0_buf;
    uint8_t *rx1_buf;
    uint8_t *tx_buf;
115
};
116
typedef struct MilkymistMinimac2State MilkymistMinimac2State;
117 118 119 120 121

static const uint8_t preamble_sfd[] = {
        0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xd5
};

122
static void minimac2_mdio_write_reg(MilkymistMinimac2State *s,
123 124
        uint8_t phy_addr, uint8_t reg_addr, uint16_t value)
{
125
    trace_milkymist_minimac2_mdio_write(phy_addr, reg_addr, value);
126 127 128 129

    /* nop */
}

130
static uint16_t minimac2_mdio_read_reg(MilkymistMinimac2State *s,
131 132 133 134
        uint8_t phy_addr, uint8_t reg_addr)
{
    uint16_t r = s->phy_regs[reg_addr];

135
    trace_milkymist_minimac2_mdio_read(phy_addr, reg_addr, r);
136 137 138 139

    return r;
}

140
static void minimac2_update_mdio(MilkymistMinimac2State *s)
141
{
142
    MilkymistMinimac2MdioState *m = &s->mdio;
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

    /* detect rising clk edge */
    if (m->last_clk == 0 && (s->regs[R_MDIO] & MDIO_CLK)) {
        /* shift data in */
        int bit = ((s->regs[R_MDIO] & MDIO_DO)
                   && (s->regs[R_MDIO] & MDIO_OE)) ? 1 : 0;
        m->data = (m->data << 1) | bit;

        /* check for sync */
        if (m->data == 0xffffffff) {
            m->count = 32;
        }

        if (m->count == 16) {
            uint8_t start = (m->data >> 14) & 0x3;
            uint8_t op = (m->data >> 12) & 0x3;
            uint8_t ta = (m->data) & 0x3;

            if (start == 1 && op == MDIO_OP_WRITE && ta == 2) {
                m->state = MDIO_STATE_WRITING;
            } else if (start == 1 && op == MDIO_OP_READ && (ta & 1) == 0) {
                m->state = MDIO_STATE_READING;
            } else {
                m->state = MDIO_STATE_IDLE;
            }

            if (m->state != MDIO_STATE_IDLE) {
                m->phy_addr = (m->data >> 7) & 0x1f;
                m->reg_addr = (m->data >> 2) & 0x1f;
            }

            if (m->state == MDIO_STATE_READING) {
175
                m->data_out = minimac2_mdio_read_reg(s, m->phy_addr,
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
                        m->reg_addr);
            }
        }

        if (m->count < 16 && m->state == MDIO_STATE_READING) {
            int bit = (m->data_out & 0x8000) ? 1 : 0;
            m->data_out <<= 1;

            if (bit) {
                s->regs[R_MDIO] |= MDIO_DI;
            } else {
                s->regs[R_MDIO] &= ~MDIO_DI;
            }
        }

        if (m->count == 0 && m->state) {
            if (m->state == MDIO_STATE_WRITING) {
                uint16_t data = m->data & 0xffff;
194
                minimac2_mdio_write_reg(s, m->phy_addr, m->reg_addr, data);
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
            }
            m->state = MDIO_STATE_IDLE;
        }
        m->count--;
    }

    m->last_clk = (s->regs[R_MDIO] & MDIO_CLK) ? 1 : 0;
}

static size_t assemble_frame(uint8_t *buf, size_t size,
        const uint8_t *payload, size_t payload_size)
{
    uint32_t crc;

    if (size < payload_size + 12) {
210
        error_report("milkymist_minimac2: received too big ethernet frame");
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
        return 0;
    }

    /* prepend preamble and sfd */
    memcpy(buf, preamble_sfd, 8);

    /* now copy the payload */
    memcpy(buf + 8, payload, payload_size);

    /* pad frame if needed */
    if (payload_size < 60) {
        memset(buf + payload_size + 8, 0, 60 - payload_size);
        payload_size = 60;
    }

    /* append fcs */
    crc = cpu_to_le32(crc32(0, buf + 8, payload_size));
    memcpy(buf + payload_size + 8, &crc, 4);

    return payload_size + 12;
}

233
static void minimac2_tx(MilkymistMinimac2State *s)
234 235
{
    uint32_t txcount = s->regs[R_TXCOUNT];
236
    uint8_t *buf = s->tx_buf;
237 238

    if (txcount < 64) {
239
        error_report("milkymist_minimac2: ethernet frame too small (%u < %u)",
240
                txcount, 64);
241
        goto err;
242 243
    }

244
    if (txcount > MINIMAC2_MTU) {
245
        error_report("milkymist_minimac2: MTU exceeded (%u > %u)",
246 247
                txcount, MINIMAC2_MTU);
        goto err;
248 249 250
    }

    if (memcmp(buf, preamble_sfd, 8) != 0) {
251
        error_report("milkymist_minimac2: frame doesn't contain the preamble "
252
                "and/or the SFD (%02x %02x %02x %02x %02x %02x %02x %02x)",
253
                buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
254
        goto err;
255 256
    }

257
    trace_milkymist_minimac2_tx_frame(txcount - 12);
258 259

    /* send packet, skipping preamble and sfd */
J
Jason Wang 已提交
260
    qemu_send_packet_raw(qemu_get_queue(s->nic), buf + 8, txcount - 12);
261 262 263

    s->regs[R_TXCOUNT] = 0;

264 265
err:
    trace_milkymist_minimac2_pulse_irq_tx();
266 267 268
    qemu_irq_pulse(s->tx_irq);
}

269 270 271 272 273 274 275 276 277 278 279 280
static void update_rx_interrupt(MilkymistMinimac2State *s)
{
    if (s->regs[R_STATE0] == STATE_PENDING
            || s->regs[R_STATE1] == STATE_PENDING) {
        trace_milkymist_minimac2_raise_irq_rx();
        qemu_irq_raise(s->rx_irq);
    } else {
        trace_milkymist_minimac2_lower_irq_rx();
        qemu_irq_lower(s->rx_irq);
    }
}

281
static ssize_t minimac2_rx(NetClientState *nc, const uint8_t *buf, size_t size)
282
{
283
    MilkymistMinimac2State *s = DO_UPCAST(NICState, nc, nc)->opaque;
284 285 286

    uint32_t r_count;
    uint32_t r_state;
287
    uint8_t *rx_buf;
288 289 290

    size_t frame_size;

291
    trace_milkymist_minimac2_rx_frame(buf, size);
292 293 294 295 296

    /* choose appropriate slot */
    if (s->regs[R_STATE0] == STATE_LOADED) {
        r_count = R_COUNT0;
        r_state = R_STATE0;
297
        rx_buf = s->rx0_buf;
298 299 300
    } else if (s->regs[R_STATE1] == STATE_LOADED) {
        r_count = R_COUNT1;
        r_state = R_STATE1;
301
        rx_buf = s->rx1_buf;
302
    } else {
303
        trace_milkymist_minimac2_drop_rx_frame(buf);
304 305 306 307
        return size;
    }

    /* assemble frame */
308
    frame_size = assemble_frame(rx_buf, MINIMAC2_BUFFER_SIZE, buf, size);
309 310 311 312 313

    if (frame_size == 0) {
        return size;
    }

314
    trace_milkymist_minimac2_rx_transfer(rx_buf, frame_size);
315 316 317 318 319

    /* update slot */
    s->regs[r_count] = frame_size;
    s->regs[r_state] = STATE_PENDING;

320
    update_rx_interrupt(s);
321 322 323 324

    return size;
}

325
static uint64_t
A
Avi Kivity 已提交
326
minimac2_read(void *opaque, hwaddr addr, unsigned size)
327
{
328
    MilkymistMinimac2State *s = opaque;
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    uint32_t r = 0;

    addr >>= 2;
    switch (addr) {
    case R_SETUP:
    case R_MDIO:
    case R_STATE0:
    case R_COUNT0:
    case R_STATE1:
    case R_COUNT1:
    case R_TXCOUNT:
        r = s->regs[addr];
        break;

    default:
344
        error_report("milkymist_minimac2: read access to unknown register 0x"
345 346 347 348
                TARGET_FMT_plx, addr << 2);
        break;
    }

349
    trace_milkymist_minimac2_memory_read(addr << 2, r);
350 351 352 353 354

    return r;
}

static void
A
Avi Kivity 已提交
355
minimac2_write(void *opaque, hwaddr addr, uint64_t value,
356
               unsigned size)
357
{
358
    MilkymistMinimac2State *s = opaque;
359

360
    trace_milkymist_minimac2_memory_read(addr, value);
361 362 363 364 365 366 367 368 369 370 371 372 373 374

    addr >>= 2;
    switch (addr) {
    case R_MDIO:
    {
        /* MDIO_DI is read only */
        int mdio_di = (s->regs[R_MDIO] & MDIO_DI);
        s->regs[R_MDIO] = value;
        if (mdio_di) {
            s->regs[R_MDIO] |= mdio_di;
        } else {
            s->regs[R_MDIO] &= ~mdio_di;
        }

375
        minimac2_update_mdio(s);
376 377 378 379
    } break;
    case R_TXCOUNT:
        s->regs[addr] = value;
        if (value > 0) {
380
            minimac2_tx(s);
381 382 383 384
        }
        break;
    case R_STATE0:
    case R_STATE1:
385 386 387 388 389
        s->regs[addr] = value;
        update_rx_interrupt(s);
        break;
    case R_SETUP:
    case R_COUNT0:
390 391 392 393 394
    case R_COUNT1:
        s->regs[addr] = value;
        break;

    default:
395
        error_report("milkymist_minimac2: write access to unknown register 0x"
396 397 398 399 400
                TARGET_FMT_plx, addr << 2);
        break;
    }
}

401 402 403 404 405 406 407 408
static const MemoryRegionOps minimac2_ops = {
    .read = minimac2_read,
    .write = minimac2_write,
    .valid = {
        .min_access_size = 4,
        .max_access_size = 4,
    },
    .endianness = DEVICE_NATIVE_ENDIAN,
409 410
};

411
static int minimac2_can_rx(NetClientState *nc)
412
{
413
    MilkymistMinimac2State *s = DO_UPCAST(NICState, nc, nc)->opaque;
414 415 416 417 418 419 420 421 422 423 424

    if (s->regs[R_STATE0] == STATE_LOADED) {
        return 1;
    }
    if (s->regs[R_STATE1] == STATE_LOADED) {
        return 1;
    }

    return 0;
}

425
static void minimac2_cleanup(NetClientState *nc)
426
{
427
    MilkymistMinimac2State *s = DO_UPCAST(NICState, nc, nc)->opaque;
428 429 430 431

    s->nic = NULL;
}

432
static void milkymist_minimac2_reset(DeviceState *d)
433
{
434 435
    MilkymistMinimac2State *s =
            container_of(d, MilkymistMinimac2State, busdev.qdev);
436 437 438 439 440 441 442 443 444 445 446 447 448 449
    int i;

    for (i = 0; i < R_MAX; i++) {
        s->regs[i] = 0;
    }
    for (i = 0; i < R_PHY_MAX; i++) {
        s->phy_regs[i] = 0;
    }

    /* defaults */
    s->phy_regs[R_PHY_ID1] = 0x0022; /* Micrel KSZ8001L */
    s->phy_regs[R_PHY_ID2] = 0x161a;
}

450
static NetClientInfo net_milkymist_minimac2_info = {
451
    .type = NET_CLIENT_OPTIONS_KIND_NIC,
452
    .size = sizeof(NICState),
453 454 455
    .can_receive = minimac2_can_rx,
    .receive = minimac2_rx,
    .cleanup = minimac2_cleanup,
456 457
};

458
static int milkymist_minimac2_init(SysBusDevice *dev)
459
{
460 461
    MilkymistMinimac2State *s = FROM_SYSBUS(typeof(*s), dev);
    size_t buffers_size = TARGET_PAGE_ALIGN(3 * MINIMAC2_BUFFER_SIZE);
462 463 464 465

    sysbus_init_irq(dev, &s->rx_irq);
    sysbus_init_irq(dev, &s->tx_irq);

466
    memory_region_init_io(&s->regs_region, &minimac2_ops, s,
467
                          "milkymist-minimac2", R_MAX * 4);
468
    sysbus_init_mmio(dev, &s->regs_region);
469

470
    /* register buffers memory */
471
    memory_region_init_ram(&s->buffers, "milkymist-minimac2.buffers",
472
                           buffers_size);
473
    vmstate_register_ram_global(&s->buffers);
474
    s->rx0_buf = memory_region_get_ram_ptr(&s->buffers);
475 476 477
    s->rx1_buf = s->rx0_buf + MINIMAC2_BUFFER_SIZE;
    s->tx_buf = s->rx1_buf + MINIMAC2_BUFFER_SIZE;

478
    sysbus_add_memory(dev, s->buffers_base, &s->buffers);
479

480
    qemu_macaddr_default_if_unset(&s->conf.macaddr);
481
    s->nic = qemu_new_nic(&net_milkymist_minimac2_info, &s->conf,
482
                          object_get_typename(OBJECT(dev)), dev->qdev.id, s);
J
Jason Wang 已提交
483
    qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
484 485 486 487

    return 0;
}

488 489
static const VMStateDescription vmstate_milkymist_minimac2_mdio = {
    .name = "milkymist-minimac2-mdio",
490 491 492 493
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .fields      = (VMStateField[]) {
494 495 496 497 498 499 500
        VMSTATE_INT32(last_clk, MilkymistMinimac2MdioState),
        VMSTATE_INT32(count, MilkymistMinimac2MdioState),
        VMSTATE_UINT32(data, MilkymistMinimac2MdioState),
        VMSTATE_UINT16(data_out, MilkymistMinimac2MdioState),
        VMSTATE_INT32(state, MilkymistMinimac2MdioState),
        VMSTATE_UINT8(phy_addr, MilkymistMinimac2MdioState),
        VMSTATE_UINT8(reg_addr, MilkymistMinimac2MdioState),
501 502 503 504
        VMSTATE_END_OF_LIST()
    }
};

505 506
static const VMStateDescription vmstate_milkymist_minimac2 = {
    .name = "milkymist-minimac2",
507 508 509 510
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .fields      = (VMStateField[]) {
511 512 513 514
        VMSTATE_UINT32_ARRAY(regs, MilkymistMinimac2State, R_MAX),
        VMSTATE_UINT16_ARRAY(phy_regs, MilkymistMinimac2State, R_PHY_MAX),
        VMSTATE_STRUCT(mdio, MilkymistMinimac2State, 0,
                vmstate_milkymist_minimac2_mdio, MilkymistMinimac2MdioState),
515 516 517 518
        VMSTATE_END_OF_LIST()
    }
};

519 520 521 522 523 524 525 526 527 528
static Property milkymist_minimac2_properties[] = {
    DEFINE_PROP_TADDR("buffers_base", MilkymistMinimac2State,
    buffers_base, 0),
    DEFINE_NIC_PROPERTIES(MilkymistMinimac2State, conf),
    DEFINE_PROP_STRING("phy_model", MilkymistMinimac2State, phy_model),
    DEFINE_PROP_END_OF_LIST(),
};

static void milkymist_minimac2_class_init(ObjectClass *klass, void *data)
{
529
    DeviceClass *dc = DEVICE_CLASS(klass);
530 531 532
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);

    k->init = milkymist_minimac2_init;
533 534 535
    dc->reset = milkymist_minimac2_reset;
    dc->vmsd = &vmstate_milkymist_minimac2;
    dc->props = milkymist_minimac2_properties;
536 537
}

538
static const TypeInfo milkymist_minimac2_info = {
539 540 541 542
    .name          = "milkymist-minimac2",
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(MilkymistMinimac2State),
    .class_init    = milkymist_minimac2_class_init,
543 544
};

A
Andreas Färber 已提交
545
static void milkymist_minimac2_register_types(void)
546
{
547
    type_register_static(&milkymist_minimac2_info);
548 549
}

A
Andreas Färber 已提交
550
type_init(milkymist_minimac2_register_types)