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
;
104 uint32_t regs
[R_MAX
];
106 MilkymistMinimac2MdioState mdio
;
108 uint16_t phy_regs
[R_PHY_MAX
];
114 typedef struct MilkymistMinimac2State MilkymistMinimac2State
;
116 static const uint8_t preamble_sfd
[] = {
117 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xd5
120 static void minimac2_mdio_write_reg(MilkymistMinimac2State
*s
,
121 uint8_t phy_addr
, uint8_t reg_addr
, uint16_t value
)
123 trace_milkymist_minimac2_mdio_write(phy_addr
, reg_addr
, value
);
128 static uint16_t minimac2_mdio_read_reg(MilkymistMinimac2State
*s
,
129 uint8_t phy_addr
, uint8_t reg_addr
)
131 uint16_t r
= s
->phy_regs
[reg_addr
];
133 trace_milkymist_minimac2_mdio_read(phy_addr
, reg_addr
, r
);
138 static void minimac2_update_mdio(MilkymistMinimac2State
*s
)
140 MilkymistMinimac2MdioState
*m
= &s
->mdio
;
142 /* detect rising clk edge */
143 if (m
->last_clk
== 0 && (s
->regs
[R_MDIO
] & MDIO_CLK
)) {
145 int bit
= ((s
->regs
[R_MDIO
] & MDIO_DO
)
146 && (s
->regs
[R_MDIO
] & MDIO_OE
)) ? 1 : 0;
147 m
->data
= (m
->data
<< 1) | bit
;
150 if (m
->data
== 0xffffffff) {
154 if (m
->count
== 16) {
155 uint8_t start
= (m
->data
>> 14) & 0x3;
156 uint8_t op
= (m
->data
>> 12) & 0x3;
157 uint8_t ta
= (m
->data
) & 0x3;
159 if (start
== 1 && op
== MDIO_OP_WRITE
&& ta
== 2) {
160 m
->state
= MDIO_STATE_WRITING
;
161 } else if (start
== 1 && op
== MDIO_OP_READ
&& (ta
& 1) == 0) {
162 m
->state
= MDIO_STATE_READING
;
164 m
->state
= MDIO_STATE_IDLE
;
167 if (m
->state
!= MDIO_STATE_IDLE
) {
168 m
->phy_addr
= (m
->data
>> 7) & 0x1f;
169 m
->reg_addr
= (m
->data
>> 2) & 0x1f;
172 if (m
->state
== MDIO_STATE_READING
) {
173 m
->data_out
= minimac2_mdio_read_reg(s
, m
->phy_addr
,
178 if (m
->count
< 16 && m
->state
== MDIO_STATE_READING
) {
179 int bit
= (m
->data_out
& 0x8000) ? 1 : 0;
183 s
->regs
[R_MDIO
] |= MDIO_DI
;
185 s
->regs
[R_MDIO
] &= ~MDIO_DI
;
189 if (m
->count
== 0 && m
->state
) {
190 if (m
->state
== MDIO_STATE_WRITING
) {
191 uint16_t data
= m
->data
& 0xffff;
192 minimac2_mdio_write_reg(s
, m
->phy_addr
, m
->reg_addr
, data
);
194 m
->state
= MDIO_STATE_IDLE
;
199 m
->last_clk
= (s
->regs
[R_MDIO
] & MDIO_CLK
) ? 1 : 0;
202 static size_t assemble_frame(uint8_t *buf
, size_t size
,
203 const uint8_t *payload
, size_t payload_size
)
207 if (size
< payload_size
+ 12) {
208 error_report("milkymist_minimac2: received too big ethernet frame");
212 /* prepend preamble and sfd */
213 memcpy(buf
, preamble_sfd
, 8);
215 /* now copy the payload */
216 memcpy(buf
+ 8, payload
, payload_size
);
218 /* pad frame if needed */
219 if (payload_size
< 60) {
220 memset(buf
+ payload_size
+ 8, 0, 60 - payload_size
);
225 crc
= cpu_to_le32(crc32(0, buf
+ 8, payload_size
));
226 memcpy(buf
+ payload_size
+ 8, &crc
, 4);
228 return payload_size
+ 12;
231 static void minimac2_tx(MilkymistMinimac2State
*s
)
233 uint32_t txcount
= s
->regs
[R_TXCOUNT
];
234 uint8_t *buf
= s
->tx_buf
;
237 error_report("milkymist_minimac2: ethernet frame too small (%u < %u)\n",
242 if (txcount
> MINIMAC2_MTU
) {
243 error_report("milkymist_minimac2: MTU exceeded (%u > %u)\n",
244 txcount
, MINIMAC2_MTU
);
248 if (memcmp(buf
, preamble_sfd
, 8) != 0) {
249 error_report("milkymist_minimac2: frame doesn't contain the preamble "
250 "and/or the SFD (%02x %02x %02x %02x %02x %02x %02x %02x)\n",
251 buf
[0], buf
[1], buf
[2], buf
[3], buf
[4], buf
[5], buf
[6], buf
[7]);
255 trace_milkymist_minimac2_tx_frame(txcount
- 12);
257 /* send packet, skipping preamble and sfd */
258 qemu_send_packet_raw(&s
->nic
->nc
, buf
+ 8, txcount
- 12);
260 s
->regs
[R_TXCOUNT
] = 0;
263 trace_milkymist_minimac2_pulse_irq_tx();
264 qemu_irq_pulse(s
->tx_irq
);
267 static void update_rx_interrupt(MilkymistMinimac2State
*s
)
269 if (s
->regs
[R_STATE0
] == STATE_PENDING
270 || s
->regs
[R_STATE1
] == STATE_PENDING
) {
271 trace_milkymist_minimac2_raise_irq_rx();
272 qemu_irq_raise(s
->rx_irq
);
274 trace_milkymist_minimac2_lower_irq_rx();
275 qemu_irq_lower(s
->rx_irq
);
279 static ssize_t
minimac2_rx(VLANClientState
*nc
, const uint8_t *buf
, size_t size
)
281 MilkymistMinimac2State
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
289 trace_milkymist_minimac2_rx_frame(buf
, size
);
291 /* choose appropriate slot */
292 if (s
->regs
[R_STATE0
] == STATE_LOADED
) {
296 } else if (s
->regs
[R_STATE1
] == STATE_LOADED
) {
301 trace_milkymist_minimac2_drop_rx_frame(buf
);
306 frame_size
= assemble_frame(rx_buf
, MINIMAC2_BUFFER_SIZE
, buf
, size
);
308 if (frame_size
== 0) {
312 trace_milkymist_minimac2_rx_transfer(rx_buf
, frame_size
);
315 s
->regs
[r_count
] = frame_size
;
316 s
->regs
[r_state
] = STATE_PENDING
;
318 update_rx_interrupt(s
);
324 minimac2_read(void *opaque
, target_phys_addr_t addr
)
326 MilkymistMinimac2State
*s
= opaque
;
342 error_report("milkymist_minimac2: read access to unknown register 0x"
343 TARGET_FMT_plx
, addr
<< 2);
347 trace_milkymist_minimac2_memory_read(addr
<< 2, r
);
353 minimac2_write(void *opaque
, target_phys_addr_t addr
, uint32_t value
)
355 MilkymistMinimac2State
*s
= opaque
;
357 trace_milkymist_minimac2_memory_read(addr
, value
);
363 /* MDIO_DI is read only */
364 int mdio_di
= (s
->regs
[R_MDIO
] & MDIO_DI
);
365 s
->regs
[R_MDIO
] = value
;
367 s
->regs
[R_MDIO
] |= mdio_di
;
369 s
->regs
[R_MDIO
] &= ~mdio_di
;
372 minimac2_update_mdio(s
);
375 s
->regs
[addr
] = value
;
382 s
->regs
[addr
] = value
;
383 update_rx_interrupt(s
);
388 s
->regs
[addr
] = value
;
392 error_report("milkymist_minimac2: write access to unknown register 0x"
393 TARGET_FMT_plx
, addr
<< 2);
398 static CPUReadMemoryFunc
* const minimac2_read_fn
[] = {
404 static CPUWriteMemoryFunc
* const minimac2_write_fn
[] = {
410 static int minimac2_can_rx(VLANClientState
*nc
)
412 MilkymistMinimac2State
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
414 if (s
->regs
[R_STATE0
] == STATE_LOADED
) {
417 if (s
->regs
[R_STATE1
] == STATE_LOADED
) {
424 static void minimac2_cleanup(VLANClientState
*nc
)
426 MilkymistMinimac2State
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
431 static void milkymist_minimac2_reset(DeviceState
*d
)
433 MilkymistMinimac2State
*s
=
434 container_of(d
, MilkymistMinimac2State
, busdev
.qdev
);
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_TYPE_NIC
,
451 .size
= sizeof(NICState
),
452 .can_receive
= minimac2_can_rx
,
453 .receive
= minimac2_rx
,
454 .cleanup
= minimac2_cleanup
,
457 static int milkymist_minimac2_init(SysBusDevice
*dev
)
459 MilkymistMinimac2State
*s
= FROM_SYSBUS(typeof(*s
), dev
);
462 size_t buffers_size
= TARGET_PAGE_ALIGN(3 * MINIMAC2_BUFFER_SIZE
);
464 sysbus_init_irq(dev
, &s
->rx_irq
);
465 sysbus_init_irq(dev
, &s
->tx_irq
);
467 regs
= cpu_register_io_memory(minimac2_read_fn
, minimac2_write_fn
, s
,
468 DEVICE_NATIVE_ENDIAN
);
469 sysbus_init_mmio(dev
, R_MAX
* 4, regs
);
471 /* register buffers memory */
472 buffers
= qemu_ram_alloc(NULL
, "milkymist_minimac2.buffers", buffers_size
);
473 s
->rx0_buf
= qemu_get_ram_ptr(buffers
);
474 s
->rx1_buf
= s
->rx0_buf
+ MINIMAC2_BUFFER_SIZE
;
475 s
->tx_buf
= s
->rx1_buf
+ MINIMAC2_BUFFER_SIZE
;
477 cpu_register_physical_memory(s
->buffers_base
, buffers_size
,
478 buffers
| IO_MEM_RAM
);
480 qemu_macaddr_default_if_unset(&s
->conf
.macaddr
);
481 s
->nic
= qemu_new_nic(&net_milkymist_minimac2_info
, &s
->conf
,
482 dev
->qdev
.info
->name
, 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 SysBusDeviceInfo milkymist_minimac2_info
= {
520 .init
= milkymist_minimac2_init
,
521 .qdev
.name
= "milkymist-minimac2",
522 .qdev
.size
= sizeof(MilkymistMinimac2State
),
523 .qdev
.vmsd
= &vmstate_milkymist_minimac2
,
524 .qdev
.reset
= milkymist_minimac2_reset
,
525 .qdev
.props
= (Property
[]) {
526 DEFINE_PROP_TADDR("buffers_base", MilkymistMinimac2State
,
528 DEFINE_NIC_PROPERTIES(MilkymistMinimac2State
, conf
),
529 DEFINE_PROP_STRING("phy_model", MilkymistMinimac2State
, phy_model
),
530 DEFINE_PROP_END_OF_LIST(),
534 static void milkymist_minimac2_register(void)
536 sysbus_register_withprop(&milkymist_minimac2_info
);
539 device_init(milkymist_minimac2_register
)