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:
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qom/object.h"
28 #include "cpu.h" /* FIXME: why does this use TARGET_PAGE_ALIGN? */
30 #include "hw/qdev-properties.h"
31 #include "hw/sysbus.h"
32 #include "migration/vmstate.h"
36 #include "qemu/module.h"
37 #include "qemu/error-report.h"
53 SETUP_PHY_RST
= (1<<0),
86 #define MINIMAC2_MTU 1530
87 #define MINIMAC2_BUFFER_SIZE 2048
89 struct MilkymistMinimac2MdioState
{
99 typedef struct MilkymistMinimac2MdioState MilkymistMinimac2MdioState
;
101 #define TYPE_MILKYMIST_MINIMAC2 "milkymist-minimac2"
102 OBJECT_DECLARE_SIMPLE_TYPE(MilkymistMinimac2State
, MILKYMIST_MINIMAC2
)
104 struct MilkymistMinimac2State
{
105 SysBusDevice parent_obj
;
110 MemoryRegion buffers
;
111 MemoryRegion regs_region
;
116 uint32_t regs
[R_MAX
];
118 MilkymistMinimac2MdioState mdio
;
120 uint16_t phy_regs
[R_PHY_MAX
];
127 static const uint8_t preamble_sfd
[] = {
128 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xd5
131 static void minimac2_mdio_write_reg(MilkymistMinimac2State
*s
,
132 uint8_t phy_addr
, uint8_t reg_addr
, uint16_t value
)
134 trace_milkymist_minimac2_mdio_write(phy_addr
, reg_addr
, value
);
139 static uint16_t minimac2_mdio_read_reg(MilkymistMinimac2State
*s
,
140 uint8_t phy_addr
, uint8_t reg_addr
)
142 uint16_t r
= s
->phy_regs
[reg_addr
];
144 trace_milkymist_minimac2_mdio_read(phy_addr
, reg_addr
, r
);
149 static void minimac2_update_mdio(MilkymistMinimac2State
*s
)
151 MilkymistMinimac2MdioState
*m
= &s
->mdio
;
153 /* detect rising clk edge */
154 if (m
->last_clk
== 0 && (s
->regs
[R_MDIO
] & MDIO_CLK
)) {
156 int bit
= ((s
->regs
[R_MDIO
] & MDIO_DO
)
157 && (s
->regs
[R_MDIO
] & MDIO_OE
)) ? 1 : 0;
158 m
->data
= (m
->data
<< 1) | bit
;
161 if (m
->data
== 0xffffffff) {
165 if (m
->count
== 16) {
166 uint8_t start
= (m
->data
>> 14) & 0x3;
167 uint8_t op
= (m
->data
>> 12) & 0x3;
168 uint8_t ta
= (m
->data
) & 0x3;
170 if (start
== 1 && op
== MDIO_OP_WRITE
&& ta
== 2) {
171 m
->state
= MDIO_STATE_WRITING
;
172 } else if (start
== 1 && op
== MDIO_OP_READ
&& (ta
& 1) == 0) {
173 m
->state
= MDIO_STATE_READING
;
175 m
->state
= MDIO_STATE_IDLE
;
178 if (m
->state
!= MDIO_STATE_IDLE
) {
179 m
->phy_addr
= (m
->data
>> 7) & 0x1f;
180 m
->reg_addr
= (m
->data
>> 2) & 0x1f;
183 if (m
->state
== MDIO_STATE_READING
) {
184 m
->data_out
= minimac2_mdio_read_reg(s
, m
->phy_addr
,
189 if (m
->count
< 16 && m
->state
== MDIO_STATE_READING
) {
190 int bit
= (m
->data_out
& 0x8000) ? 1 : 0;
194 s
->regs
[R_MDIO
] |= MDIO_DI
;
196 s
->regs
[R_MDIO
] &= ~MDIO_DI
;
200 if (m
->count
== 0 && m
->state
) {
201 if (m
->state
== MDIO_STATE_WRITING
) {
202 uint16_t data
= m
->data
& 0xffff;
203 minimac2_mdio_write_reg(s
, m
->phy_addr
, m
->reg_addr
, data
);
205 m
->state
= MDIO_STATE_IDLE
;
210 m
->last_clk
= (s
->regs
[R_MDIO
] & MDIO_CLK
) ? 1 : 0;
213 static size_t assemble_frame(uint8_t *buf
, size_t size
,
214 const uint8_t *payload
, size_t payload_size
)
218 if (size
< payload_size
+ 12) {
219 qemu_log_mask(LOG_GUEST_ERROR
, "milkymist_minimac2: frame too big "
220 "(%zd bytes)\n", payload_size
);
224 /* prepend preamble and sfd */
225 memcpy(buf
, preamble_sfd
, 8);
227 /* now copy the payload */
228 memcpy(buf
+ 8, payload
, payload_size
);
230 /* pad frame if needed */
231 if (payload_size
< 60) {
232 memset(buf
+ payload_size
+ 8, 0, 60 - payload_size
);
237 crc
= cpu_to_le32(crc32(0, buf
+ 8, payload_size
));
238 memcpy(buf
+ payload_size
+ 8, &crc
, 4);
240 return payload_size
+ 12;
243 static void minimac2_tx(MilkymistMinimac2State
*s
)
245 uint32_t txcount
= s
->regs
[R_TXCOUNT
];
246 uint8_t *buf
= s
->tx_buf
;
249 error_report("milkymist_minimac2: ethernet frame too small (%u < %u)",
254 if (txcount
> MINIMAC2_MTU
) {
255 error_report("milkymist_minimac2: MTU exceeded (%u > %u)",
256 txcount
, MINIMAC2_MTU
);
260 if (memcmp(buf
, preamble_sfd
, 8) != 0) {
261 error_report("milkymist_minimac2: frame doesn't contain the preamble "
262 "and/or the SFD (%02x %02x %02x %02x %02x %02x %02x %02x)",
263 buf
[0], buf
[1], buf
[2], buf
[3], buf
[4], buf
[5], buf
[6], buf
[7]);
267 trace_milkymist_minimac2_tx_frame(txcount
- 12);
269 /* send packet, skipping preamble and sfd */
270 qemu_send_packet_raw(qemu_get_queue(s
->nic
), buf
+ 8, txcount
- 12);
272 s
->regs
[R_TXCOUNT
] = 0;
275 trace_milkymist_minimac2_pulse_irq_tx();
276 qemu_irq_pulse(s
->tx_irq
);
279 static void update_rx_interrupt(MilkymistMinimac2State
*s
)
281 if (s
->regs
[R_STATE0
] == STATE_PENDING
282 || s
->regs
[R_STATE1
] == STATE_PENDING
) {
283 trace_milkymist_minimac2_raise_irq_rx();
284 qemu_irq_raise(s
->rx_irq
);
286 trace_milkymist_minimac2_lower_irq_rx();
287 qemu_irq_lower(s
->rx_irq
);
291 static ssize_t
minimac2_rx(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
293 MilkymistMinimac2State
*s
= qemu_get_nic_opaque(nc
);
301 trace_milkymist_minimac2_rx_frame(buf
, size
);
303 /* choose appropriate slot */
304 if (s
->regs
[R_STATE0
] == STATE_LOADED
) {
308 } else if (s
->regs
[R_STATE1
] == STATE_LOADED
) {
317 frame_size
= assemble_frame(rx_buf
, MINIMAC2_BUFFER_SIZE
, buf
, size
);
319 if (frame_size
== 0) {
323 trace_milkymist_minimac2_rx_transfer(rx_buf
, frame_size
);
326 s
->regs
[r_count
] = frame_size
;
327 s
->regs
[r_state
] = STATE_PENDING
;
329 update_rx_interrupt(s
);
335 minimac2_read(void *opaque
, hwaddr addr
, unsigned size
)
337 MilkymistMinimac2State
*s
= opaque
;
353 qemu_log_mask(LOG_GUEST_ERROR
,
354 "milkymist_minimac2_rd%d: 0x%" HWADDR_PRIx
"\n",
359 trace_milkymist_minimac2_memory_read(addr
<< 2, r
);
364 static int minimac2_can_rx(MilkymistMinimac2State
*s
)
366 if (s
->regs
[R_STATE0
] == STATE_LOADED
) {
369 if (s
->regs
[R_STATE1
] == STATE_LOADED
) {
377 minimac2_write(void *opaque
, hwaddr addr
, uint64_t value
,
380 MilkymistMinimac2State
*s
= opaque
;
382 trace_milkymist_minimac2_memory_write(addr
, value
);
388 /* MDIO_DI is read only */
389 int mdio_di
= (s
->regs
[R_MDIO
] & MDIO_DI
);
390 s
->regs
[R_MDIO
] = value
;
392 s
->regs
[R_MDIO
] |= mdio_di
;
394 s
->regs
[R_MDIO
] &= ~mdio_di
;
397 minimac2_update_mdio(s
);
400 s
->regs
[addr
] = value
;
407 s
->regs
[addr
] = value
;
408 update_rx_interrupt(s
);
409 if (minimac2_can_rx(s
)) {
410 qemu_flush_queued_packets(qemu_get_queue(s
->nic
));
416 s
->regs
[addr
] = value
;
420 qemu_log_mask(LOG_GUEST_ERROR
,
421 "milkymist_minimac2_wr%d: 0x%" HWADDR_PRIx
422 " = 0x%" PRIx64
"\n",
423 size
, addr
<< 2, value
);
428 static const MemoryRegionOps minimac2_ops
= {
429 .read
= minimac2_read
,
430 .write
= minimac2_write
,
432 .min_access_size
= 4,
433 .max_access_size
= 4,
435 .endianness
= DEVICE_NATIVE_ENDIAN
,
438 static void milkymist_minimac2_reset(DeviceState
*d
)
440 MilkymistMinimac2State
*s
= MILKYMIST_MINIMAC2(d
);
443 for (i
= 0; i
< R_MAX
; i
++) {
446 for (i
= 0; i
< R_PHY_MAX
; i
++) {
451 s
->phy_regs
[R_PHY_ID1
] = 0x0022; /* Micrel KSZ8001L */
452 s
->phy_regs
[R_PHY_ID2
] = 0x161a;
455 static NetClientInfo net_milkymist_minimac2_info
= {
456 .type
= NET_CLIENT_DRIVER_NIC
,
457 .size
= sizeof(NICState
),
458 .receive
= minimac2_rx
,
461 static void milkymist_minimac2_realize(DeviceState
*dev
, Error
**errp
)
463 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
464 MilkymistMinimac2State
*s
= MILKYMIST_MINIMAC2(dev
);
465 size_t buffers_size
= TARGET_PAGE_ALIGN(3 * MINIMAC2_BUFFER_SIZE
);
467 sysbus_init_irq(sbd
, &s
->rx_irq
);
468 sysbus_init_irq(sbd
, &s
->tx_irq
);
470 memory_region_init_io(&s
->regs_region
, OBJECT(dev
), &minimac2_ops
, s
,
471 "milkymist-minimac2", R_MAX
* 4);
472 sysbus_init_mmio(sbd
, &s
->regs_region
);
474 /* register buffers memory */
475 memory_region_init_ram_nomigrate(&s
->buffers
, OBJECT(dev
), "milkymist-minimac2.buffers",
476 buffers_size
, &error_fatal
);
477 vmstate_register_ram_global(&s
->buffers
);
478 s
->rx0_buf
= memory_region_get_ram_ptr(&s
->buffers
);
479 s
->rx1_buf
= s
->rx0_buf
+ MINIMAC2_BUFFER_SIZE
;
480 s
->tx_buf
= s
->rx1_buf
+ MINIMAC2_BUFFER_SIZE
;
482 sysbus_init_mmio(sbd
, &s
->buffers
);
484 qemu_macaddr_default_if_unset(&s
->conf
.macaddr
);
485 s
->nic
= qemu_new_nic(&net_milkymist_minimac2_info
, &s
->conf
,
486 object_get_typename(OBJECT(dev
)), dev
->id
, s
);
487 qemu_format_nic_info_str(qemu_get_queue(s
->nic
), s
->conf
.macaddr
.a
);
490 static const VMStateDescription vmstate_milkymist_minimac2_mdio
= {
491 .name
= "milkymist-minimac2-mdio",
493 .minimum_version_id
= 1,
494 .fields
= (VMStateField
[]) {
495 VMSTATE_INT32(last_clk
, MilkymistMinimac2MdioState
),
496 VMSTATE_INT32(count
, MilkymistMinimac2MdioState
),
497 VMSTATE_UINT32(data
, MilkymistMinimac2MdioState
),
498 VMSTATE_UINT16(data_out
, MilkymistMinimac2MdioState
),
499 VMSTATE_INT32(state
, MilkymistMinimac2MdioState
),
500 VMSTATE_UINT8(phy_addr
, MilkymistMinimac2MdioState
),
501 VMSTATE_UINT8(reg_addr
, MilkymistMinimac2MdioState
),
502 VMSTATE_END_OF_LIST()
506 static const VMStateDescription vmstate_milkymist_minimac2
= {
507 .name
= "milkymist-minimac2",
509 .minimum_version_id
= 1,
510 .fields
= (VMStateField
[]) {
511 VMSTATE_UINT32_ARRAY(regs
, MilkymistMinimac2State
, R_MAX
),
512 VMSTATE_UINT16_ARRAY(phy_regs
, MilkymistMinimac2State
, R_PHY_MAX
),
513 VMSTATE_STRUCT(mdio
, MilkymistMinimac2State
, 0,
514 vmstate_milkymist_minimac2_mdio
, MilkymistMinimac2MdioState
),
515 VMSTATE_END_OF_LIST()
519 static Property milkymist_minimac2_properties
[] = {
520 DEFINE_NIC_PROPERTIES(MilkymistMinimac2State
, conf
),
521 DEFINE_PROP_STRING("phy_model", MilkymistMinimac2State
, phy_model
),
522 DEFINE_PROP_END_OF_LIST(),
525 static void milkymist_minimac2_class_init(ObjectClass
*klass
, void *data
)
527 DeviceClass
*dc
= DEVICE_CLASS(klass
);
529 dc
->realize
= milkymist_minimac2_realize
;
530 dc
->reset
= milkymist_minimac2_reset
;
531 dc
->vmsd
= &vmstate_milkymist_minimac2
;
532 device_class_set_props(dc
, milkymist_minimac2_properties
);
535 static const TypeInfo milkymist_minimac2_info
= {
536 .name
= TYPE_MILKYMIST_MINIMAC2
,
537 .parent
= TYPE_SYS_BUS_DEVICE
,
538 .instance_size
= sizeof(MilkymistMinimac2State
),
539 .class_init
= milkymist_minimac2_class_init
,
542 static void milkymist_minimac2_register_types(void)
544 type_register_static(&milkymist_minimac2_info
);
547 type_init(milkymist_minimac2_register_types
)