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 "qemu-common.h"
28 #include "cpu.h" /* FIXME: why does this use TARGET_PAGE_ALIGN? */
30 #include "hw/sysbus.h"
33 #include "qemu/error-report.h"
49 SETUP_PHY_RST
= (1<<0),
82 #define MINIMAC2_MTU 1530
83 #define MINIMAC2_BUFFER_SIZE 2048
85 struct MilkymistMinimac2MdioState
{
95 typedef struct MilkymistMinimac2MdioState MilkymistMinimac2MdioState
;
97 #define TYPE_MILKYMIST_MINIMAC2 "milkymist-minimac2"
98 #define MILKYMIST_MINIMAC2(obj) \
99 OBJECT_CHECK(MilkymistMinimac2State, (obj), TYPE_MILKYMIST_MINIMAC2)
101 struct MilkymistMinimac2State
{
102 SysBusDevice parent_obj
;
107 MemoryRegion buffers
;
108 MemoryRegion regs_region
;
113 uint32_t regs
[R_MAX
];
115 MilkymistMinimac2MdioState mdio
;
117 uint16_t phy_regs
[R_PHY_MAX
];
123 typedef struct MilkymistMinimac2State MilkymistMinimac2State
;
125 static const uint8_t preamble_sfd
[] = {
126 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xd5
129 static void minimac2_mdio_write_reg(MilkymistMinimac2State
*s
,
130 uint8_t phy_addr
, uint8_t reg_addr
, uint16_t value
)
132 trace_milkymist_minimac2_mdio_write(phy_addr
, reg_addr
, value
);
137 static uint16_t minimac2_mdio_read_reg(MilkymistMinimac2State
*s
,
138 uint8_t phy_addr
, uint8_t reg_addr
)
140 uint16_t r
= s
->phy_regs
[reg_addr
];
142 trace_milkymist_minimac2_mdio_read(phy_addr
, reg_addr
, r
);
147 static void minimac2_update_mdio(MilkymistMinimac2State
*s
)
149 MilkymistMinimac2MdioState
*m
= &s
->mdio
;
151 /* detect rising clk edge */
152 if (m
->last_clk
== 0 && (s
->regs
[R_MDIO
] & MDIO_CLK
)) {
154 int bit
= ((s
->regs
[R_MDIO
] & MDIO_DO
)
155 && (s
->regs
[R_MDIO
] & MDIO_OE
)) ? 1 : 0;
156 m
->data
= (m
->data
<< 1) | bit
;
159 if (m
->data
== 0xffffffff) {
163 if (m
->count
== 16) {
164 uint8_t start
= (m
->data
>> 14) & 0x3;
165 uint8_t op
= (m
->data
>> 12) & 0x3;
166 uint8_t ta
= (m
->data
) & 0x3;
168 if (start
== 1 && op
== MDIO_OP_WRITE
&& ta
== 2) {
169 m
->state
= MDIO_STATE_WRITING
;
170 } else if (start
== 1 && op
== MDIO_OP_READ
&& (ta
& 1) == 0) {
171 m
->state
= MDIO_STATE_READING
;
173 m
->state
= MDIO_STATE_IDLE
;
176 if (m
->state
!= MDIO_STATE_IDLE
) {
177 m
->phy_addr
= (m
->data
>> 7) & 0x1f;
178 m
->reg_addr
= (m
->data
>> 2) & 0x1f;
181 if (m
->state
== MDIO_STATE_READING
) {
182 m
->data_out
= minimac2_mdio_read_reg(s
, m
->phy_addr
,
187 if (m
->count
< 16 && m
->state
== MDIO_STATE_READING
) {
188 int bit
= (m
->data_out
& 0x8000) ? 1 : 0;
192 s
->regs
[R_MDIO
] |= MDIO_DI
;
194 s
->regs
[R_MDIO
] &= ~MDIO_DI
;
198 if (m
->count
== 0 && m
->state
) {
199 if (m
->state
== MDIO_STATE_WRITING
) {
200 uint16_t data
= m
->data
& 0xffff;
201 minimac2_mdio_write_reg(s
, m
->phy_addr
, m
->reg_addr
, data
);
203 m
->state
= MDIO_STATE_IDLE
;
208 m
->last_clk
= (s
->regs
[R_MDIO
] & MDIO_CLK
) ? 1 : 0;
211 static size_t assemble_frame(uint8_t *buf
, size_t size
,
212 const uint8_t *payload
, size_t payload_size
)
216 if (size
< payload_size
+ 12) {
217 error_report("milkymist_minimac2: received too big ethernet frame");
221 /* prepend preamble and sfd */
222 memcpy(buf
, preamble_sfd
, 8);
224 /* now copy the payload */
225 memcpy(buf
+ 8, payload
, payload_size
);
227 /* pad frame if needed */
228 if (payload_size
< 60) {
229 memset(buf
+ payload_size
+ 8, 0, 60 - payload_size
);
234 crc
= cpu_to_le32(crc32(0, buf
+ 8, payload_size
));
235 memcpy(buf
+ payload_size
+ 8, &crc
, 4);
237 return payload_size
+ 12;
240 static void minimac2_tx(MilkymistMinimac2State
*s
)
242 uint32_t txcount
= s
->regs
[R_TXCOUNT
];
243 uint8_t *buf
= s
->tx_buf
;
246 error_report("milkymist_minimac2: ethernet frame too small (%u < %u)",
251 if (txcount
> MINIMAC2_MTU
) {
252 error_report("milkymist_minimac2: MTU exceeded (%u > %u)",
253 txcount
, MINIMAC2_MTU
);
257 if (memcmp(buf
, preamble_sfd
, 8) != 0) {
258 error_report("milkymist_minimac2: frame doesn't contain the preamble "
259 "and/or the SFD (%02x %02x %02x %02x %02x %02x %02x %02x)",
260 buf
[0], buf
[1], buf
[2], buf
[3], buf
[4], buf
[5], buf
[6], buf
[7]);
264 trace_milkymist_minimac2_tx_frame(txcount
- 12);
266 /* send packet, skipping preamble and sfd */
267 qemu_send_packet_raw(qemu_get_queue(s
->nic
), buf
+ 8, txcount
- 12);
269 s
->regs
[R_TXCOUNT
] = 0;
272 trace_milkymist_minimac2_pulse_irq_tx();
273 qemu_irq_pulse(s
->tx_irq
);
276 static void update_rx_interrupt(MilkymistMinimac2State
*s
)
278 if (s
->regs
[R_STATE0
] == STATE_PENDING
279 || s
->regs
[R_STATE1
] == STATE_PENDING
) {
280 trace_milkymist_minimac2_raise_irq_rx();
281 qemu_irq_raise(s
->rx_irq
);
283 trace_milkymist_minimac2_lower_irq_rx();
284 qemu_irq_lower(s
->rx_irq
);
288 static ssize_t
minimac2_rx(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
290 MilkymistMinimac2State
*s
= qemu_get_nic_opaque(nc
);
298 trace_milkymist_minimac2_rx_frame(buf
, size
);
300 /* choose appropriate slot */
301 if (s
->regs
[R_STATE0
] == STATE_LOADED
) {
305 } else if (s
->regs
[R_STATE1
] == STATE_LOADED
) {
314 frame_size
= assemble_frame(rx_buf
, MINIMAC2_BUFFER_SIZE
, buf
, size
);
316 if (frame_size
== 0) {
320 trace_milkymist_minimac2_rx_transfer(rx_buf
, frame_size
);
323 s
->regs
[r_count
] = frame_size
;
324 s
->regs
[r_state
] = STATE_PENDING
;
326 update_rx_interrupt(s
);
332 minimac2_read(void *opaque
, hwaddr addr
, unsigned size
)
334 MilkymistMinimac2State
*s
= opaque
;
350 error_report("milkymist_minimac2: read access to unknown register 0x"
351 TARGET_FMT_plx
, addr
<< 2);
355 trace_milkymist_minimac2_memory_read(addr
<< 2, r
);
360 static int minimac2_can_rx(MilkymistMinimac2State
*s
)
362 if (s
->regs
[R_STATE0
] == STATE_LOADED
) {
365 if (s
->regs
[R_STATE1
] == STATE_LOADED
) {
373 minimac2_write(void *opaque
, hwaddr addr
, uint64_t value
,
376 MilkymistMinimac2State
*s
= opaque
;
378 trace_milkymist_minimac2_memory_write(addr
, value
);
384 /* MDIO_DI is read only */
385 int mdio_di
= (s
->regs
[R_MDIO
] & MDIO_DI
);
386 s
->regs
[R_MDIO
] = value
;
388 s
->regs
[R_MDIO
] |= mdio_di
;
390 s
->regs
[R_MDIO
] &= ~mdio_di
;
393 minimac2_update_mdio(s
);
396 s
->regs
[addr
] = value
;
403 s
->regs
[addr
] = value
;
404 update_rx_interrupt(s
);
405 if (minimac2_can_rx(s
)) {
406 qemu_flush_queued_packets(qemu_get_queue(s
->nic
));
412 s
->regs
[addr
] = value
;
416 error_report("milkymist_minimac2: write access to unknown register 0x"
417 TARGET_FMT_plx
, addr
<< 2);
422 static const MemoryRegionOps minimac2_ops
= {
423 .read
= minimac2_read
,
424 .write
= minimac2_write
,
426 .min_access_size
= 4,
427 .max_access_size
= 4,
429 .endianness
= DEVICE_NATIVE_ENDIAN
,
432 static void milkymist_minimac2_reset(DeviceState
*d
)
434 MilkymistMinimac2State
*s
= MILKYMIST_MINIMAC2(d
);
437 for (i
= 0; i
< R_MAX
; i
++) {
440 for (i
= 0; i
< R_PHY_MAX
; i
++) {
445 s
->phy_regs
[R_PHY_ID1
] = 0x0022; /* Micrel KSZ8001L */
446 s
->phy_regs
[R_PHY_ID2
] = 0x161a;
449 static NetClientInfo net_milkymist_minimac2_info
= {
450 .type
= NET_CLIENT_DRIVER_NIC
,
451 .size
= sizeof(NICState
),
452 .receive
= minimac2_rx
,
455 static int milkymist_minimac2_init(SysBusDevice
*sbd
)
457 DeviceState
*dev
= DEVICE(sbd
);
458 MilkymistMinimac2State
*s
= MILKYMIST_MINIMAC2(dev
);
459 size_t buffers_size
= TARGET_PAGE_ALIGN(3 * MINIMAC2_BUFFER_SIZE
);
461 sysbus_init_irq(sbd
, &s
->rx_irq
);
462 sysbus_init_irq(sbd
, &s
->tx_irq
);
464 memory_region_init_io(&s
->regs_region
, OBJECT(dev
), &minimac2_ops
, s
,
465 "milkymist-minimac2", R_MAX
* 4);
466 sysbus_init_mmio(sbd
, &s
->regs_region
);
468 /* register buffers memory */
469 memory_region_init_ram_nomigrate(&s
->buffers
, OBJECT(dev
), "milkymist-minimac2.buffers",
470 buffers_size
, &error_fatal
);
471 vmstate_register_ram_global(&s
->buffers
);
472 s
->rx0_buf
= memory_region_get_ram_ptr(&s
->buffers
);
473 s
->rx1_buf
= s
->rx0_buf
+ MINIMAC2_BUFFER_SIZE
;
474 s
->tx_buf
= s
->rx1_buf
+ MINIMAC2_BUFFER_SIZE
;
476 sysbus_init_mmio(sbd
, &s
->buffers
);
478 qemu_macaddr_default_if_unset(&s
->conf
.macaddr
);
479 s
->nic
= qemu_new_nic(&net_milkymist_minimac2_info
, &s
->conf
,
480 object_get_typename(OBJECT(dev
)), dev
->id
, s
);
481 qemu_format_nic_info_str(qemu_get_queue(s
->nic
), s
->conf
.macaddr
.a
);
486 static const VMStateDescription vmstate_milkymist_minimac2_mdio
= {
487 .name
= "milkymist-minimac2-mdio",
489 .minimum_version_id
= 1,
490 .fields
= (VMStateField
[]) {
491 VMSTATE_INT32(last_clk
, MilkymistMinimac2MdioState
),
492 VMSTATE_INT32(count
, MilkymistMinimac2MdioState
),
493 VMSTATE_UINT32(data
, MilkymistMinimac2MdioState
),
494 VMSTATE_UINT16(data_out
, MilkymistMinimac2MdioState
),
495 VMSTATE_INT32(state
, MilkymistMinimac2MdioState
),
496 VMSTATE_UINT8(phy_addr
, MilkymistMinimac2MdioState
),
497 VMSTATE_UINT8(reg_addr
, MilkymistMinimac2MdioState
),
498 VMSTATE_END_OF_LIST()
502 static const VMStateDescription vmstate_milkymist_minimac2
= {
503 .name
= "milkymist-minimac2",
505 .minimum_version_id
= 1,
506 .fields
= (VMStateField
[]) {
507 VMSTATE_UINT32_ARRAY(regs
, MilkymistMinimac2State
, R_MAX
),
508 VMSTATE_UINT16_ARRAY(phy_regs
, MilkymistMinimac2State
, R_PHY_MAX
),
509 VMSTATE_STRUCT(mdio
, MilkymistMinimac2State
, 0,
510 vmstate_milkymist_minimac2_mdio
, MilkymistMinimac2MdioState
),
511 VMSTATE_END_OF_LIST()
515 static Property milkymist_minimac2_properties
[] = {
516 DEFINE_NIC_PROPERTIES(MilkymistMinimac2State
, conf
),
517 DEFINE_PROP_STRING("phy_model", MilkymistMinimac2State
, phy_model
),
518 DEFINE_PROP_END_OF_LIST(),
521 static void milkymist_minimac2_class_init(ObjectClass
*klass
, void *data
)
523 DeviceClass
*dc
= DEVICE_CLASS(klass
);
524 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
526 k
->init
= milkymist_minimac2_init
;
527 dc
->reset
= milkymist_minimac2_reset
;
528 dc
->vmsd
= &vmstate_milkymist_minimac2
;
529 dc
->props
= milkymist_minimac2_properties
;
532 static const TypeInfo milkymist_minimac2_info
= {
533 .name
= TYPE_MILKYMIST_MINIMAC2
,
534 .parent
= TYPE_SYS_BUS_DEVICE
,
535 .instance_size
= sizeof(MilkymistMinimac2State
),
536 .class_init
= milkymist_minimac2_class_init
,
539 static void milkymist_minimac2_register_types(void)
541 type_register_static(&milkymist_minimac2_info
);
544 type_init(milkymist_minimac2_register_types
)