ppc/pnv: Drop PnvChipClass::type
[qemu/ar7.git] / hw / net / milkymist-minimac2.c
blob86b6d28033476e3d204d2edc73bb4f596b218089
1 /*
2 * QEMU model of the Milkymist minimac2 block.
4 * Copyright (c) 2011 Michael Walle <michael@walle.cc>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Specification available at:
21 * not available yet
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "cpu.h" /* FIXME: why does this use TARGET_PAGE_ALIGN? */
28 #include "hw/irq.h"
29 #include "hw/qdev-properties.h"
30 #include "hw/sysbus.h"
31 #include "migration/vmstate.h"
32 #include "trace.h"
33 #include "net/net.h"
34 #include "qemu/log.h"
35 #include "qemu/module.h"
36 #include "qemu/error-report.h"
38 #include <zlib.h>
40 enum {
41 R_SETUP = 0,
42 R_MDIO,
43 R_STATE0,
44 R_COUNT0,
45 R_STATE1,
46 R_COUNT1,
47 R_TXCOUNT,
48 R_MAX
51 enum {
52 SETUP_PHY_RST = (1<<0),
55 enum {
56 MDIO_DO = (1<<0),
57 MDIO_DI = (1<<1),
58 MDIO_OE = (1<<2),
59 MDIO_CLK = (1<<3),
62 enum {
63 STATE_EMPTY = 0,
64 STATE_LOADED = 1,
65 STATE_PENDING = 2,
68 enum {
69 MDIO_OP_WRITE = 1,
70 MDIO_OP_READ = 2,
73 enum mdio_state {
74 MDIO_STATE_IDLE,
75 MDIO_STATE_READING,
76 MDIO_STATE_WRITING,
79 enum {
80 R_PHY_ID1 = 2,
81 R_PHY_ID2 = 3,
82 R_PHY_MAX = 32
85 #define MINIMAC2_MTU 1530
86 #define MINIMAC2_BUFFER_SIZE 2048
88 struct MilkymistMinimac2MdioState {
89 int last_clk;
90 int count;
91 uint32_t data;
92 uint16_t data_out;
93 int state;
95 uint8_t phy_addr;
96 uint8_t reg_addr;
98 typedef struct MilkymistMinimac2MdioState MilkymistMinimac2MdioState;
100 #define TYPE_MILKYMIST_MINIMAC2 "milkymist-minimac2"
101 #define MILKYMIST_MINIMAC2(obj) \
102 OBJECT_CHECK(MilkymistMinimac2State, (obj), TYPE_MILKYMIST_MINIMAC2)
104 struct MilkymistMinimac2State {
105 SysBusDevice parent_obj;
107 NICState *nic;
108 NICConf conf;
109 char *phy_model;
110 MemoryRegion buffers;
111 MemoryRegion regs_region;
113 qemu_irq rx_irq;
114 qemu_irq tx_irq;
116 uint32_t regs[R_MAX];
118 MilkymistMinimac2MdioState mdio;
120 uint16_t phy_regs[R_PHY_MAX];
122 uint8_t *rx0_buf;
123 uint8_t *rx1_buf;
124 uint8_t *tx_buf;
126 typedef struct MilkymistMinimac2State MilkymistMinimac2State;
128 static const uint8_t preamble_sfd[] = {
129 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xd5
132 static void minimac2_mdio_write_reg(MilkymistMinimac2State *s,
133 uint8_t phy_addr, uint8_t reg_addr, uint16_t value)
135 trace_milkymist_minimac2_mdio_write(phy_addr, reg_addr, value);
137 /* nop */
140 static uint16_t minimac2_mdio_read_reg(MilkymistMinimac2State *s,
141 uint8_t phy_addr, uint8_t reg_addr)
143 uint16_t r = s->phy_regs[reg_addr];
145 trace_milkymist_minimac2_mdio_read(phy_addr, reg_addr, r);
147 return r;
150 static void minimac2_update_mdio(MilkymistMinimac2State *s)
152 MilkymistMinimac2MdioState *m = &s->mdio;
154 /* detect rising clk edge */
155 if (m->last_clk == 0 && (s->regs[R_MDIO] & MDIO_CLK)) {
156 /* shift data in */
157 int bit = ((s->regs[R_MDIO] & MDIO_DO)
158 && (s->regs[R_MDIO] & MDIO_OE)) ? 1 : 0;
159 m->data = (m->data << 1) | bit;
161 /* check for sync */
162 if (m->data == 0xffffffff) {
163 m->count = 32;
166 if (m->count == 16) {
167 uint8_t start = (m->data >> 14) & 0x3;
168 uint8_t op = (m->data >> 12) & 0x3;
169 uint8_t ta = (m->data) & 0x3;
171 if (start == 1 && op == MDIO_OP_WRITE && ta == 2) {
172 m->state = MDIO_STATE_WRITING;
173 } else if (start == 1 && op == MDIO_OP_READ && (ta & 1) == 0) {
174 m->state = MDIO_STATE_READING;
175 } else {
176 m->state = MDIO_STATE_IDLE;
179 if (m->state != MDIO_STATE_IDLE) {
180 m->phy_addr = (m->data >> 7) & 0x1f;
181 m->reg_addr = (m->data >> 2) & 0x1f;
184 if (m->state == MDIO_STATE_READING) {
185 m->data_out = minimac2_mdio_read_reg(s, m->phy_addr,
186 m->reg_addr);
190 if (m->count < 16 && m->state == MDIO_STATE_READING) {
191 int bit = (m->data_out & 0x8000) ? 1 : 0;
192 m->data_out <<= 1;
194 if (bit) {
195 s->regs[R_MDIO] |= MDIO_DI;
196 } else {
197 s->regs[R_MDIO] &= ~MDIO_DI;
201 if (m->count == 0 && m->state) {
202 if (m->state == MDIO_STATE_WRITING) {
203 uint16_t data = m->data & 0xffff;
204 minimac2_mdio_write_reg(s, m->phy_addr, m->reg_addr, data);
206 m->state = MDIO_STATE_IDLE;
208 m->count--;
211 m->last_clk = (s->regs[R_MDIO] & MDIO_CLK) ? 1 : 0;
214 static size_t assemble_frame(uint8_t *buf, size_t size,
215 const uint8_t *payload, size_t payload_size)
217 uint32_t crc;
219 if (size < payload_size + 12) {
220 qemu_log_mask(LOG_GUEST_ERROR, "milkymist_minimac2: frame too big "
221 "(%zd bytes)\n", payload_size);
222 return 0;
225 /* prepend preamble and sfd */
226 memcpy(buf, preamble_sfd, 8);
228 /* now copy the payload */
229 memcpy(buf + 8, payload, payload_size);
231 /* pad frame if needed */
232 if (payload_size < 60) {
233 memset(buf + payload_size + 8, 0, 60 - payload_size);
234 payload_size = 60;
237 /* append fcs */
238 crc = cpu_to_le32(crc32(0, buf + 8, payload_size));
239 memcpy(buf + payload_size + 8, &crc, 4);
241 return payload_size + 12;
244 static void minimac2_tx(MilkymistMinimac2State *s)
246 uint32_t txcount = s->regs[R_TXCOUNT];
247 uint8_t *buf = s->tx_buf;
249 if (txcount < 64) {
250 error_report("milkymist_minimac2: ethernet frame too small (%u < %u)",
251 txcount, 64);
252 goto err;
255 if (txcount > MINIMAC2_MTU) {
256 error_report("milkymist_minimac2: MTU exceeded (%u > %u)",
257 txcount, MINIMAC2_MTU);
258 goto err;
261 if (memcmp(buf, preamble_sfd, 8) != 0) {
262 error_report("milkymist_minimac2: frame doesn't contain the preamble "
263 "and/or the SFD (%02x %02x %02x %02x %02x %02x %02x %02x)",
264 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
265 goto err;
268 trace_milkymist_minimac2_tx_frame(txcount - 12);
270 /* send packet, skipping preamble and sfd */
271 qemu_send_packet_raw(qemu_get_queue(s->nic), buf + 8, txcount - 12);
273 s->regs[R_TXCOUNT] = 0;
275 err:
276 trace_milkymist_minimac2_pulse_irq_tx();
277 qemu_irq_pulse(s->tx_irq);
280 static void update_rx_interrupt(MilkymistMinimac2State *s)
282 if (s->regs[R_STATE0] == STATE_PENDING
283 || s->regs[R_STATE1] == STATE_PENDING) {
284 trace_milkymist_minimac2_raise_irq_rx();
285 qemu_irq_raise(s->rx_irq);
286 } else {
287 trace_milkymist_minimac2_lower_irq_rx();
288 qemu_irq_lower(s->rx_irq);
292 static ssize_t minimac2_rx(NetClientState *nc, const uint8_t *buf, size_t size)
294 MilkymistMinimac2State *s = qemu_get_nic_opaque(nc);
296 uint32_t r_count;
297 uint32_t r_state;
298 uint8_t *rx_buf;
300 size_t frame_size;
302 trace_milkymist_minimac2_rx_frame(buf, size);
304 /* choose appropriate slot */
305 if (s->regs[R_STATE0] == STATE_LOADED) {
306 r_count = R_COUNT0;
307 r_state = R_STATE0;
308 rx_buf = s->rx0_buf;
309 } else if (s->regs[R_STATE1] == STATE_LOADED) {
310 r_count = R_COUNT1;
311 r_state = R_STATE1;
312 rx_buf = s->rx1_buf;
313 } else {
314 return 0;
317 /* assemble frame */
318 frame_size = assemble_frame(rx_buf, MINIMAC2_BUFFER_SIZE, buf, size);
320 if (frame_size == 0) {
321 return size;
324 trace_milkymist_minimac2_rx_transfer(rx_buf, frame_size);
326 /* update slot */
327 s->regs[r_count] = frame_size;
328 s->regs[r_state] = STATE_PENDING;
330 update_rx_interrupt(s);
332 return size;
335 static uint64_t
336 minimac2_read(void *opaque, hwaddr addr, unsigned size)
338 MilkymistMinimac2State *s = opaque;
339 uint32_t r = 0;
341 addr >>= 2;
342 switch (addr) {
343 case R_SETUP:
344 case R_MDIO:
345 case R_STATE0:
346 case R_COUNT0:
347 case R_STATE1:
348 case R_COUNT1:
349 case R_TXCOUNT:
350 r = s->regs[addr];
351 break;
353 default:
354 qemu_log_mask(LOG_GUEST_ERROR,
355 "milkymist_minimac2_rd%d: 0x%" HWADDR_PRIx "\n",
356 size, addr << 2);
357 break;
360 trace_milkymist_minimac2_memory_read(addr << 2, r);
362 return r;
365 static int minimac2_can_rx(MilkymistMinimac2State *s)
367 if (s->regs[R_STATE0] == STATE_LOADED) {
368 return 1;
370 if (s->regs[R_STATE1] == STATE_LOADED) {
371 return 1;
374 return 0;
377 static void
378 minimac2_write(void *opaque, hwaddr addr, uint64_t value,
379 unsigned size)
381 MilkymistMinimac2State *s = opaque;
383 trace_milkymist_minimac2_memory_write(addr, value);
385 addr >>= 2;
386 switch (addr) {
387 case R_MDIO:
389 /* MDIO_DI is read only */
390 int mdio_di = (s->regs[R_MDIO] & MDIO_DI);
391 s->regs[R_MDIO] = value;
392 if (mdio_di) {
393 s->regs[R_MDIO] |= mdio_di;
394 } else {
395 s->regs[R_MDIO] &= ~mdio_di;
398 minimac2_update_mdio(s);
399 } break;
400 case R_TXCOUNT:
401 s->regs[addr] = value;
402 if (value > 0) {
403 minimac2_tx(s);
405 break;
406 case R_STATE0:
407 case R_STATE1:
408 s->regs[addr] = value;
409 update_rx_interrupt(s);
410 if (minimac2_can_rx(s)) {
411 qemu_flush_queued_packets(qemu_get_queue(s->nic));
413 break;
414 case R_SETUP:
415 case R_COUNT0:
416 case R_COUNT1:
417 s->regs[addr] = value;
418 break;
420 default:
421 qemu_log_mask(LOG_GUEST_ERROR,
422 "milkymist_minimac2_wr%d: 0x%" HWADDR_PRIx
423 " = 0x%" PRIx64 "\n",
424 size, addr << 2, value);
425 break;
429 static const MemoryRegionOps minimac2_ops = {
430 .read = minimac2_read,
431 .write = minimac2_write,
432 .valid = {
433 .min_access_size = 4,
434 .max_access_size = 4,
436 .endianness = DEVICE_NATIVE_ENDIAN,
439 static void milkymist_minimac2_reset(DeviceState *d)
441 MilkymistMinimac2State *s = MILKYMIST_MINIMAC2(d);
442 int i;
444 for (i = 0; i < R_MAX; i++) {
445 s->regs[i] = 0;
447 for (i = 0; i < R_PHY_MAX; i++) {
448 s->phy_regs[i] = 0;
451 /* defaults */
452 s->phy_regs[R_PHY_ID1] = 0x0022; /* Micrel KSZ8001L */
453 s->phy_regs[R_PHY_ID2] = 0x161a;
456 static NetClientInfo net_milkymist_minimac2_info = {
457 .type = NET_CLIENT_DRIVER_NIC,
458 .size = sizeof(NICState),
459 .receive = minimac2_rx,
462 static void milkymist_minimac2_realize(DeviceState *dev, Error **errp)
464 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
465 MilkymistMinimac2State *s = MILKYMIST_MINIMAC2(dev);
466 size_t buffers_size = TARGET_PAGE_ALIGN(3 * MINIMAC2_BUFFER_SIZE);
468 sysbus_init_irq(sbd, &s->rx_irq);
469 sysbus_init_irq(sbd, &s->tx_irq);
471 memory_region_init_io(&s->regs_region, OBJECT(dev), &minimac2_ops, s,
472 "milkymist-minimac2", R_MAX * 4);
473 sysbus_init_mmio(sbd, &s->regs_region);
475 /* register buffers memory */
476 memory_region_init_ram_nomigrate(&s->buffers, OBJECT(dev), "milkymist-minimac2.buffers",
477 buffers_size, &error_fatal);
478 vmstate_register_ram_global(&s->buffers);
479 s->rx0_buf = memory_region_get_ram_ptr(&s->buffers);
480 s->rx1_buf = s->rx0_buf + MINIMAC2_BUFFER_SIZE;
481 s->tx_buf = s->rx1_buf + MINIMAC2_BUFFER_SIZE;
483 sysbus_init_mmio(sbd, &s->buffers);
485 qemu_macaddr_default_if_unset(&s->conf.macaddr);
486 s->nic = qemu_new_nic(&net_milkymist_minimac2_info, &s->conf,
487 object_get_typename(OBJECT(dev)), dev->id, s);
488 qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
491 static const VMStateDescription vmstate_milkymist_minimac2_mdio = {
492 .name = "milkymist-minimac2-mdio",
493 .version_id = 1,
494 .minimum_version_id = 1,
495 .fields = (VMStateField[]) {
496 VMSTATE_INT32(last_clk, MilkymistMinimac2MdioState),
497 VMSTATE_INT32(count, MilkymistMinimac2MdioState),
498 VMSTATE_UINT32(data, MilkymistMinimac2MdioState),
499 VMSTATE_UINT16(data_out, MilkymistMinimac2MdioState),
500 VMSTATE_INT32(state, MilkymistMinimac2MdioState),
501 VMSTATE_UINT8(phy_addr, MilkymistMinimac2MdioState),
502 VMSTATE_UINT8(reg_addr, MilkymistMinimac2MdioState),
503 VMSTATE_END_OF_LIST()
507 static const VMStateDescription vmstate_milkymist_minimac2 = {
508 .name = "milkymist-minimac2",
509 .version_id = 1,
510 .minimum_version_id = 1,
511 .fields = (VMStateField[]) {
512 VMSTATE_UINT32_ARRAY(regs, MilkymistMinimac2State, R_MAX),
513 VMSTATE_UINT16_ARRAY(phy_regs, MilkymistMinimac2State, R_PHY_MAX),
514 VMSTATE_STRUCT(mdio, MilkymistMinimac2State, 0,
515 vmstate_milkymist_minimac2_mdio, MilkymistMinimac2MdioState),
516 VMSTATE_END_OF_LIST()
520 static Property milkymist_minimac2_properties[] = {
521 DEFINE_NIC_PROPERTIES(MilkymistMinimac2State, conf),
522 DEFINE_PROP_STRING("phy_model", MilkymistMinimac2State, phy_model),
523 DEFINE_PROP_END_OF_LIST(),
526 static void milkymist_minimac2_class_init(ObjectClass *klass, void *data)
528 DeviceClass *dc = DEVICE_CLASS(klass);
530 dc->realize = milkymist_minimac2_realize;
531 dc->reset = milkymist_minimac2_reset;
532 dc->vmsd = &vmstate_milkymist_minimac2;
533 dc->props = milkymist_minimac2_properties;
536 static const TypeInfo milkymist_minimac2_info = {
537 .name = TYPE_MILKYMIST_MINIMAC2,
538 .parent = TYPE_SYS_BUS_DEVICE,
539 .instance_size = sizeof(MilkymistMinimac2State),
540 .class_init = milkymist_minimac2_class_init,
543 static void milkymist_minimac2_register_types(void)
545 type_register_static(&milkymist_minimac2_info);
548 type_init(milkymist_minimac2_register_types)