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:
26 #include "hw/sysbus.h"
29 #include "qemu/error-report.h"
45 SETUP_PHY_RST
= (1<<0),
78 #define MINIMAC2_MTU 1530
79 #define MINIMAC2_BUFFER_SIZE 2048
81 struct MilkymistMinimac2MdioState
{
91 typedef struct MilkymistMinimac2MdioState MilkymistMinimac2MdioState
;
93 #define TYPE_MILKYMIST_MINIMAC2 "milkymist-minimac2"
94 #define MILKYMIST_MINIMAC2(obj) \
95 OBJECT_CHECK(MilkymistMinimac2State, (obj), TYPE_MILKYMIST_MINIMAC2)
97 struct MilkymistMinimac2State
{
98 SysBusDevice parent_obj
;
103 MemoryRegion buffers
;
104 MemoryRegion regs_region
;
109 uint32_t regs
[R_MAX
];
111 MilkymistMinimac2MdioState mdio
;
113 uint16_t phy_regs
[R_PHY_MAX
];
119 typedef struct MilkymistMinimac2State MilkymistMinimac2State
;
121 static const uint8_t preamble_sfd
[] = {
122 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xd5
125 static void minimac2_mdio_write_reg(MilkymistMinimac2State
*s
,
126 uint8_t phy_addr
, uint8_t reg_addr
, uint16_t value
)
128 trace_milkymist_minimac2_mdio_write(phy_addr
, reg_addr
, value
);
133 static uint16_t minimac2_mdio_read_reg(MilkymistMinimac2State
*s
,
134 uint8_t phy_addr
, uint8_t reg_addr
)
136 uint16_t r
= s
->phy_regs
[reg_addr
];
138 trace_milkymist_minimac2_mdio_read(phy_addr
, reg_addr
, r
);
143 static void minimac2_update_mdio(MilkymistMinimac2State
*s
)
145 MilkymistMinimac2MdioState
*m
= &s
->mdio
;
147 /* detect rising clk edge */
148 if (m
->last_clk
== 0 && (s
->regs
[R_MDIO
] & MDIO_CLK
)) {
150 int bit
= ((s
->regs
[R_MDIO
] & MDIO_DO
)
151 && (s
->regs
[R_MDIO
] & MDIO_OE
)) ? 1 : 0;
152 m
->data
= (m
->data
<< 1) | bit
;
155 if (m
->data
== 0xffffffff) {
159 if (m
->count
== 16) {
160 uint8_t start
= (m
->data
>> 14) & 0x3;
161 uint8_t op
= (m
->data
>> 12) & 0x3;
162 uint8_t ta
= (m
->data
) & 0x3;
164 if (start
== 1 && op
== MDIO_OP_WRITE
&& ta
== 2) {
165 m
->state
= MDIO_STATE_WRITING
;
166 } else if (start
== 1 && op
== MDIO_OP_READ
&& (ta
& 1) == 0) {
167 m
->state
= MDIO_STATE_READING
;
169 m
->state
= MDIO_STATE_IDLE
;
172 if (m
->state
!= MDIO_STATE_IDLE
) {
173 m
->phy_addr
= (m
->data
>> 7) & 0x1f;
174 m
->reg_addr
= (m
->data
>> 2) & 0x1f;
177 if (m
->state
== MDIO_STATE_READING
) {
178 m
->data_out
= minimac2_mdio_read_reg(s
, m
->phy_addr
,
183 if (m
->count
< 16 && m
->state
== MDIO_STATE_READING
) {
184 int bit
= (m
->data_out
& 0x8000) ? 1 : 0;
188 s
->regs
[R_MDIO
] |= MDIO_DI
;
190 s
->regs
[R_MDIO
] &= ~MDIO_DI
;
194 if (m
->count
== 0 && m
->state
) {
195 if (m
->state
== MDIO_STATE_WRITING
) {
196 uint16_t data
= m
->data
& 0xffff;
197 minimac2_mdio_write_reg(s
, m
->phy_addr
, m
->reg_addr
, data
);
199 m
->state
= MDIO_STATE_IDLE
;
204 m
->last_clk
= (s
->regs
[R_MDIO
] & MDIO_CLK
) ? 1 : 0;
207 static size_t assemble_frame(uint8_t *buf
, size_t size
,
208 const uint8_t *payload
, size_t payload_size
)
212 if (size
< payload_size
+ 12) {
213 error_report("milkymist_minimac2: received too big ethernet frame");
217 /* prepend preamble and sfd */
218 memcpy(buf
, preamble_sfd
, 8);
220 /* now copy the payload */
221 memcpy(buf
+ 8, payload
, payload_size
);
223 /* pad frame if needed */
224 if (payload_size
< 60) {
225 memset(buf
+ payload_size
+ 8, 0, 60 - payload_size
);
230 crc
= cpu_to_le32(crc32(0, buf
+ 8, payload_size
));
231 memcpy(buf
+ payload_size
+ 8, &crc
, 4);
233 return payload_size
+ 12;
236 static void minimac2_tx(MilkymistMinimac2State
*s
)
238 uint32_t txcount
= s
->regs
[R_TXCOUNT
];
239 uint8_t *buf
= s
->tx_buf
;
242 error_report("milkymist_minimac2: ethernet frame too small (%u < %u)",
247 if (txcount
> MINIMAC2_MTU
) {
248 error_report("milkymist_minimac2: MTU exceeded (%u > %u)",
249 txcount
, MINIMAC2_MTU
);
253 if (memcmp(buf
, preamble_sfd
, 8) != 0) {
254 error_report("milkymist_minimac2: frame doesn't contain the preamble "
255 "and/or the SFD (%02x %02x %02x %02x %02x %02x %02x %02x)",
256 buf
[0], buf
[1], buf
[2], buf
[3], buf
[4], buf
[5], buf
[6], buf
[7]);
260 trace_milkymist_minimac2_tx_frame(txcount
- 12);
262 /* send packet, skipping preamble and sfd */
263 qemu_send_packet_raw(qemu_get_queue(s
->nic
), buf
+ 8, txcount
- 12);
265 s
->regs
[R_TXCOUNT
] = 0;
268 trace_milkymist_minimac2_pulse_irq_tx();
269 qemu_irq_pulse(s
->tx_irq
);
272 static void update_rx_interrupt(MilkymistMinimac2State
*s
)
274 if (s
->regs
[R_STATE0
] == STATE_PENDING
275 || s
->regs
[R_STATE1
] == STATE_PENDING
) {
276 trace_milkymist_minimac2_raise_irq_rx();
277 qemu_irq_raise(s
->rx_irq
);
279 trace_milkymist_minimac2_lower_irq_rx();
280 qemu_irq_lower(s
->rx_irq
);
284 static ssize_t
minimac2_rx(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
286 MilkymistMinimac2State
*s
= qemu_get_nic_opaque(nc
);
294 trace_milkymist_minimac2_rx_frame(buf
, size
);
296 /* choose appropriate slot */
297 if (s
->regs
[R_STATE0
] == STATE_LOADED
) {
301 } else if (s
->regs
[R_STATE1
] == STATE_LOADED
) {
310 frame_size
= assemble_frame(rx_buf
, MINIMAC2_BUFFER_SIZE
, buf
, size
);
312 if (frame_size
== 0) {
316 trace_milkymist_minimac2_rx_transfer(rx_buf
, frame_size
);
319 s
->regs
[r_count
] = frame_size
;
320 s
->regs
[r_state
] = STATE_PENDING
;
322 update_rx_interrupt(s
);
328 minimac2_read(void *opaque
, hwaddr addr
, unsigned size
)
330 MilkymistMinimac2State
*s
= opaque
;
346 error_report("milkymist_minimac2: read access to unknown register 0x"
347 TARGET_FMT_plx
, addr
<< 2);
351 trace_milkymist_minimac2_memory_read(addr
<< 2, r
);
356 static int minimac2_can_rx(MilkymistMinimac2State
*s
)
358 if (s
->regs
[R_STATE0
] == STATE_LOADED
) {
361 if (s
->regs
[R_STATE1
] == STATE_LOADED
) {
369 minimac2_write(void *opaque
, hwaddr addr
, uint64_t value
,
372 MilkymistMinimac2State
*s
= opaque
;
374 trace_milkymist_minimac2_memory_write(addr
, value
);
380 /* MDIO_DI is read only */
381 int mdio_di
= (s
->regs
[R_MDIO
] & MDIO_DI
);
382 s
->regs
[R_MDIO
] = value
;
384 s
->regs
[R_MDIO
] |= mdio_di
;
386 s
->regs
[R_MDIO
] &= ~mdio_di
;
389 minimac2_update_mdio(s
);
392 s
->regs
[addr
] = value
;
399 s
->regs
[addr
] = value
;
400 update_rx_interrupt(s
);
401 if (minimac2_can_rx(s
)) {
402 qemu_flush_queued_packets(qemu_get_queue(s
->nic
));
408 s
->regs
[addr
] = value
;
412 error_report("milkymist_minimac2: write access to unknown register 0x"
413 TARGET_FMT_plx
, addr
<< 2);
418 static const MemoryRegionOps minimac2_ops
= {
419 .read
= minimac2_read
,
420 .write
= minimac2_write
,
422 .min_access_size
= 4,
423 .max_access_size
= 4,
425 .endianness
= DEVICE_NATIVE_ENDIAN
,
428 static void milkymist_minimac2_reset(DeviceState
*d
)
430 MilkymistMinimac2State
*s
= MILKYMIST_MINIMAC2(d
);
433 for (i
= 0; i
< R_MAX
; i
++) {
436 for (i
= 0; i
< R_PHY_MAX
; i
++) {
441 s
->phy_regs
[R_PHY_ID1
] = 0x0022; /* Micrel KSZ8001L */
442 s
->phy_regs
[R_PHY_ID2
] = 0x161a;
445 static NetClientInfo net_milkymist_minimac2_info
= {
446 .type
= NET_CLIENT_OPTIONS_KIND_NIC
,
447 .size
= sizeof(NICState
),
448 .receive
= minimac2_rx
,
451 static int milkymist_minimac2_init(SysBusDevice
*sbd
)
453 DeviceState
*dev
= DEVICE(sbd
);
454 MilkymistMinimac2State
*s
= MILKYMIST_MINIMAC2(dev
);
455 size_t buffers_size
= TARGET_PAGE_ALIGN(3 * MINIMAC2_BUFFER_SIZE
);
457 sysbus_init_irq(sbd
, &s
->rx_irq
);
458 sysbus_init_irq(sbd
, &s
->tx_irq
);
460 memory_region_init_io(&s
->regs_region
, OBJECT(dev
), &minimac2_ops
, s
,
461 "milkymist-minimac2", R_MAX
* 4);
462 sysbus_init_mmio(sbd
, &s
->regs_region
);
464 /* register buffers memory */
465 memory_region_init_ram(&s
->buffers
, OBJECT(dev
), "milkymist-minimac2.buffers",
466 buffers_size
, &error_abort
);
467 vmstate_register_ram_global(&s
->buffers
);
468 s
->rx0_buf
= memory_region_get_ram_ptr(&s
->buffers
);
469 s
->rx1_buf
= s
->rx0_buf
+ MINIMAC2_BUFFER_SIZE
;
470 s
->tx_buf
= s
->rx1_buf
+ MINIMAC2_BUFFER_SIZE
;
472 sysbus_init_mmio(sbd
, &s
->buffers
);
474 qemu_macaddr_default_if_unset(&s
->conf
.macaddr
);
475 s
->nic
= qemu_new_nic(&net_milkymist_minimac2_info
, &s
->conf
,
476 object_get_typename(OBJECT(dev
)), dev
->id
, s
);
477 qemu_format_nic_info_str(qemu_get_queue(s
->nic
), s
->conf
.macaddr
.a
);
482 static const VMStateDescription vmstate_milkymist_minimac2_mdio
= {
483 .name
= "milkymist-minimac2-mdio",
485 .minimum_version_id
= 1,
486 .fields
= (VMStateField
[]) {
487 VMSTATE_INT32(last_clk
, MilkymistMinimac2MdioState
),
488 VMSTATE_INT32(count
, MilkymistMinimac2MdioState
),
489 VMSTATE_UINT32(data
, MilkymistMinimac2MdioState
),
490 VMSTATE_UINT16(data_out
, MilkymistMinimac2MdioState
),
491 VMSTATE_INT32(state
, MilkymistMinimac2MdioState
),
492 VMSTATE_UINT8(phy_addr
, MilkymistMinimac2MdioState
),
493 VMSTATE_UINT8(reg_addr
, MilkymistMinimac2MdioState
),
494 VMSTATE_END_OF_LIST()
498 static const VMStateDescription vmstate_milkymist_minimac2
= {
499 .name
= "milkymist-minimac2",
501 .minimum_version_id
= 1,
502 .fields
= (VMStateField
[]) {
503 VMSTATE_UINT32_ARRAY(regs
, MilkymistMinimac2State
, R_MAX
),
504 VMSTATE_UINT16_ARRAY(phy_regs
, MilkymistMinimac2State
, R_PHY_MAX
),
505 VMSTATE_STRUCT(mdio
, MilkymistMinimac2State
, 0,
506 vmstate_milkymist_minimac2_mdio
, MilkymistMinimac2MdioState
),
507 VMSTATE_END_OF_LIST()
511 static Property milkymist_minimac2_properties
[] = {
512 DEFINE_NIC_PROPERTIES(MilkymistMinimac2State
, conf
),
513 DEFINE_PROP_STRING("phy_model", MilkymistMinimac2State
, phy_model
),
514 DEFINE_PROP_END_OF_LIST(),
517 static void milkymist_minimac2_class_init(ObjectClass
*klass
, void *data
)
519 DeviceClass
*dc
= DEVICE_CLASS(klass
);
520 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
522 k
->init
= milkymist_minimac2_init
;
523 dc
->reset
= milkymist_minimac2_reset
;
524 dc
->vmsd
= &vmstate_milkymist_minimac2
;
525 dc
->props
= milkymist_minimac2_properties
;
528 static const TypeInfo milkymist_minimac2_info
= {
529 .name
= TYPE_MILKYMIST_MINIMAC2
,
530 .parent
= TYPE_SYS_BUS_DEVICE
,
531 .instance_size
= sizeof(MilkymistMinimac2State
),
532 .class_init
= milkymist_minimac2_class_init
,
535 static void milkymist_minimac2_register_types(void)
537 type_register_static(&milkymist_minimac2_info
);
540 type_init(milkymist_minimac2_register_types
)