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:
29 #include "qemu-error.h"
30 #include "qdev-addr.h"
46 SETUP_PHY_RST
= (1<<0),
79 #define MINIMAC2_MTU 1530
80 #define MINIMAC2_BUFFER_SIZE 2048
82 struct MilkymistMinimac2MdioState
{
92 typedef struct MilkymistMinimac2MdioState MilkymistMinimac2MdioState
;
94 struct MilkymistMinimac2State
{
99 target_phys_addr_t buffers_base
;
100 MemoryRegion buffers
;
101 MemoryRegion regs_region
;
106 uint32_t regs
[R_MAX
];
108 MilkymistMinimac2MdioState mdio
;
110 uint16_t phy_regs
[R_PHY_MAX
];
116 typedef struct MilkymistMinimac2State MilkymistMinimac2State
;
118 static const uint8_t preamble_sfd
[] = {
119 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xd5
122 static void minimac2_mdio_write_reg(MilkymistMinimac2State
*s
,
123 uint8_t phy_addr
, uint8_t reg_addr
, uint16_t value
)
125 trace_milkymist_minimac2_mdio_write(phy_addr
, reg_addr
, value
);
130 static uint16_t minimac2_mdio_read_reg(MilkymistMinimac2State
*s
,
131 uint8_t phy_addr
, uint8_t reg_addr
)
133 uint16_t r
= s
->phy_regs
[reg_addr
];
135 trace_milkymist_minimac2_mdio_read(phy_addr
, reg_addr
, r
);
140 static void minimac2_update_mdio(MilkymistMinimac2State
*s
)
142 MilkymistMinimac2MdioState
*m
= &s
->mdio
;
144 /* detect rising clk edge */
145 if (m
->last_clk
== 0 && (s
->regs
[R_MDIO
] & MDIO_CLK
)) {
147 int bit
= ((s
->regs
[R_MDIO
] & MDIO_DO
)
148 && (s
->regs
[R_MDIO
] & MDIO_OE
)) ? 1 : 0;
149 m
->data
= (m
->data
<< 1) | bit
;
152 if (m
->data
== 0xffffffff) {
156 if (m
->count
== 16) {
157 uint8_t start
= (m
->data
>> 14) & 0x3;
158 uint8_t op
= (m
->data
>> 12) & 0x3;
159 uint8_t ta
= (m
->data
) & 0x3;
161 if (start
== 1 && op
== MDIO_OP_WRITE
&& ta
== 2) {
162 m
->state
= MDIO_STATE_WRITING
;
163 } else if (start
== 1 && op
== MDIO_OP_READ
&& (ta
& 1) == 0) {
164 m
->state
= MDIO_STATE_READING
;
166 m
->state
= MDIO_STATE_IDLE
;
169 if (m
->state
!= MDIO_STATE_IDLE
) {
170 m
->phy_addr
= (m
->data
>> 7) & 0x1f;
171 m
->reg_addr
= (m
->data
>> 2) & 0x1f;
174 if (m
->state
== MDIO_STATE_READING
) {
175 m
->data_out
= minimac2_mdio_read_reg(s
, m
->phy_addr
,
180 if (m
->count
< 16 && m
->state
== MDIO_STATE_READING
) {
181 int bit
= (m
->data_out
& 0x8000) ? 1 : 0;
185 s
->regs
[R_MDIO
] |= MDIO_DI
;
187 s
->regs
[R_MDIO
] &= ~MDIO_DI
;
191 if (m
->count
== 0 && m
->state
) {
192 if (m
->state
== MDIO_STATE_WRITING
) {
193 uint16_t data
= m
->data
& 0xffff;
194 minimac2_mdio_write_reg(s
, m
->phy_addr
, m
->reg_addr
, data
);
196 m
->state
= MDIO_STATE_IDLE
;
201 m
->last_clk
= (s
->regs
[R_MDIO
] & MDIO_CLK
) ? 1 : 0;
204 static size_t assemble_frame(uint8_t *buf
, size_t size
,
205 const uint8_t *payload
, size_t payload_size
)
209 if (size
< payload_size
+ 12) {
210 error_report("milkymist_minimac2: received too big ethernet frame");
214 /* prepend preamble and sfd */
215 memcpy(buf
, preamble_sfd
, 8);
217 /* now copy the payload */
218 memcpy(buf
+ 8, payload
, payload_size
);
220 /* pad frame if needed */
221 if (payload_size
< 60) {
222 memset(buf
+ payload_size
+ 8, 0, 60 - payload_size
);
227 crc
= cpu_to_le32(crc32(0, buf
+ 8, payload_size
));
228 memcpy(buf
+ payload_size
+ 8, &crc
, 4);
230 return payload_size
+ 12;
233 static void minimac2_tx(MilkymistMinimac2State
*s
)
235 uint32_t txcount
= s
->regs
[R_TXCOUNT
];
236 uint8_t *buf
= s
->tx_buf
;
239 error_report("milkymist_minimac2: ethernet frame too small (%u < %u)",
244 if (txcount
> MINIMAC2_MTU
) {
245 error_report("milkymist_minimac2: MTU exceeded (%u > %u)",
246 txcount
, MINIMAC2_MTU
);
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]);
257 trace_milkymist_minimac2_tx_frame(txcount
- 12);
259 /* send packet, skipping preamble and sfd */
260 qemu_send_packet_raw(&s
->nic
->nc
, buf
+ 8, txcount
- 12);
262 s
->regs
[R_TXCOUNT
] = 0;
265 trace_milkymist_minimac2_pulse_irq_tx();
266 qemu_irq_pulse(s
->tx_irq
);
269 static void update_rx_interrupt(MilkymistMinimac2State
*s
)
271 if (s
->regs
[R_STATE0
] == STATE_PENDING
272 || s
->regs
[R_STATE1
] == STATE_PENDING
) {
273 trace_milkymist_minimac2_raise_irq_rx();
274 qemu_irq_raise(s
->rx_irq
);
276 trace_milkymist_minimac2_lower_irq_rx();
277 qemu_irq_lower(s
->rx_irq
);
281 static ssize_t
minimac2_rx(VLANClientState
*nc
, const uint8_t *buf
, size_t size
)
283 MilkymistMinimac2State
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
291 trace_milkymist_minimac2_rx_frame(buf
, size
);
293 /* choose appropriate slot */
294 if (s
->regs
[R_STATE0
] == STATE_LOADED
) {
298 } else if (s
->regs
[R_STATE1
] == STATE_LOADED
) {
303 trace_milkymist_minimac2_drop_rx_frame(buf
);
308 frame_size
= assemble_frame(rx_buf
, MINIMAC2_BUFFER_SIZE
, buf
, size
);
310 if (frame_size
== 0) {
314 trace_milkymist_minimac2_rx_transfer(rx_buf
, frame_size
);
317 s
->regs
[r_count
] = frame_size
;
318 s
->regs
[r_state
] = STATE_PENDING
;
320 update_rx_interrupt(s
);
326 minimac2_read(void *opaque
, target_phys_addr_t addr
, unsigned size
)
328 MilkymistMinimac2State
*s
= opaque
;
344 error_report("milkymist_minimac2: read access to unknown register 0x"
345 TARGET_FMT_plx
, addr
<< 2);
349 trace_milkymist_minimac2_memory_read(addr
<< 2, r
);
355 minimac2_write(void *opaque
, target_phys_addr_t addr
, uint64_t value
,
358 MilkymistMinimac2State
*s
= opaque
;
360 trace_milkymist_minimac2_memory_read(addr
, value
);
366 /* MDIO_DI is read only */
367 int mdio_di
= (s
->regs
[R_MDIO
] & MDIO_DI
);
368 s
->regs
[R_MDIO
] = value
;
370 s
->regs
[R_MDIO
] |= mdio_di
;
372 s
->regs
[R_MDIO
] &= ~mdio_di
;
375 minimac2_update_mdio(s
);
378 s
->regs
[addr
] = value
;
385 s
->regs
[addr
] = value
;
386 update_rx_interrupt(s
);
391 s
->regs
[addr
] = value
;
395 error_report("milkymist_minimac2: write access to unknown register 0x"
396 TARGET_FMT_plx
, addr
<< 2);
401 static const MemoryRegionOps minimac2_ops
= {
402 .read
= minimac2_read
,
403 .write
= minimac2_write
,
405 .min_access_size
= 4,
406 .max_access_size
= 4,
408 .endianness
= DEVICE_NATIVE_ENDIAN
,
411 static int minimac2_can_rx(VLANClientState
*nc
)
413 MilkymistMinimac2State
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
415 if (s
->regs
[R_STATE0
] == STATE_LOADED
) {
418 if (s
->regs
[R_STATE1
] == STATE_LOADED
) {
425 static void minimac2_cleanup(VLANClientState
*nc
)
427 MilkymistMinimac2State
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
432 static void milkymist_minimac2_reset(DeviceState
*d
)
434 MilkymistMinimac2State
*s
=
435 container_of(d
, MilkymistMinimac2State
, busdev
.qdev
);
438 for (i
= 0; i
< R_MAX
; i
++) {
441 for (i
= 0; i
< R_PHY_MAX
; i
++) {
446 s
->phy_regs
[R_PHY_ID1
] = 0x0022; /* Micrel KSZ8001L */
447 s
->phy_regs
[R_PHY_ID2
] = 0x161a;
450 static NetClientInfo net_milkymist_minimac2_info
= {
451 .type
= NET_CLIENT_TYPE_NIC
,
452 .size
= sizeof(NICState
),
453 .can_receive
= minimac2_can_rx
,
454 .receive
= minimac2_rx
,
455 .cleanup
= minimac2_cleanup
,
458 static int milkymist_minimac2_init(SysBusDevice
*dev
)
460 MilkymistMinimac2State
*s
= FROM_SYSBUS(typeof(*s
), dev
);
461 size_t buffers_size
= TARGET_PAGE_ALIGN(3 * MINIMAC2_BUFFER_SIZE
);
463 sysbus_init_irq(dev
, &s
->rx_irq
);
464 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
);
470 /* register buffers memory */
471 memory_region_init_ram(&s
->buffers
, "milkymist-minimac2.buffers",
473 vmstate_register_ram_global(&s
->buffers
);
474 s
->rx0_buf
= memory_region_get_ram_ptr(&s
->buffers
);
475 s
->rx1_buf
= s
->rx0_buf
+ MINIMAC2_BUFFER_SIZE
;
476 s
->tx_buf
= s
->rx1_buf
+ MINIMAC2_BUFFER_SIZE
;
478 sysbus_add_memory(dev
, s
->buffers_base
, &s
->buffers
);
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
);
483 qemu_format_nic_info_str(&s
->nic
->nc
, s
->conf
.macaddr
.a
);
488 static const VMStateDescription vmstate_milkymist_minimac2_mdio
= {
489 .name
= "milkymist-minimac2-mdio",
491 .minimum_version_id
= 1,
492 .minimum_version_id_old
= 1,
493 .fields
= (VMStateField
[]) {
494 VMSTATE_INT32(last_clk
, MilkymistMinimac2MdioState
),
495 VMSTATE_INT32(count
, MilkymistMinimac2MdioState
),
496 VMSTATE_UINT32(data
, MilkymistMinimac2MdioState
),
497 VMSTATE_UINT16(data_out
, MilkymistMinimac2MdioState
),
498 VMSTATE_INT32(state
, MilkymistMinimac2MdioState
),
499 VMSTATE_UINT8(phy_addr
, MilkymistMinimac2MdioState
),
500 VMSTATE_UINT8(reg_addr
, MilkymistMinimac2MdioState
),
501 VMSTATE_END_OF_LIST()
505 static const VMStateDescription vmstate_milkymist_minimac2
= {
506 .name
= "milkymist-minimac2",
508 .minimum_version_id
= 1,
509 .minimum_version_id_old
= 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_PROP_TADDR("buffers_base", MilkymistMinimac2State
,
522 DEFINE_NIC_PROPERTIES(MilkymistMinimac2State
, conf
),
523 DEFINE_PROP_STRING("phy_model", MilkymistMinimac2State
, phy_model
),
524 DEFINE_PROP_END_OF_LIST(),
527 static void milkymist_minimac2_class_init(ObjectClass
*klass
, void *data
)
529 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
531 k
->init
= milkymist_minimac2_init
;
534 static DeviceInfo milkymist_minimac2_info
= {
535 .name
= "milkymist-minimac2",
536 .size
= sizeof(MilkymistMinimac2State
),
537 .vmsd
= &vmstate_milkymist_minimac2
,
538 .reset
= milkymist_minimac2_reset
,
539 .props
= milkymist_minimac2_properties
,
540 .class_init
= milkymist_minimac2_class_init
,
543 static void milkymist_minimac2_register(void)
545 sysbus_register_withprop(&milkymist_minimac2_info
);
548 device_init(milkymist_minimac2_register
)