2 * QEMU model of the Xilinx Ethernet Lite MAC.
4 * Copyright (c) 2009 Edgar E. Iglesias.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 #define R_TX_LEN0 (0x07f4 / 4)
32 #define R_TX_GIE0 (0x07f8 / 4)
33 #define R_TX_CTRL0 (0x07fc / 4)
34 #define R_TX_BUF1 (0x0800 / 4)
35 #define R_TX_LEN1 (0x0ff4 / 4)
36 #define R_TX_CTRL1 (0x0ffc / 4)
38 #define R_RX_BUF0 (0x1000 / 4)
39 #define R_RX_CTRL0 (0x17fc / 4)
40 #define R_RX_BUF1 (0x1800 / 4)
41 #define R_RX_CTRL1 (0x1ffc / 4)
42 #define R_MAX (0x2000 / 4)
44 #define GIE_GIE 0x80000000
56 unsigned int c_tx_pingpong
;
57 unsigned int c_rx_pingpong
;
65 static inline void eth_pulse_irq(struct xlx_ethlite
*s
)
67 /* Only the first gie reg is active. */
68 if (s
->regs
[R_TX_GIE0
] & GIE_GIE
) {
69 qemu_irq_pulse(s
->irq
);
73 static uint32_t eth_readl (void *opaque
, target_phys_addr_t addr
)
75 struct xlx_ethlite
*s
= opaque
;
90 D(qemu_log("%s %x=%x\n", __func__
, addr
* 4, r
));
93 /* Rx packet data is endian fixed at the way into the rx rams. This
94 * speeds things up because the ethlite MAC does not have a len
95 * register. That means the CPU will issue MMIO reads for the entire
96 * 2k rx buffer even for small packets.
106 eth_writel (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
108 struct xlx_ethlite
*s
= opaque
;
109 unsigned int base
= 0;
116 if (addr
== R_TX_CTRL1
)
119 D(qemu_log("%s addr=%x val=%x\n", __func__
, addr
* 4, value
));
120 if ((value
& (CTRL_P
| CTRL_S
)) == CTRL_S
) {
121 qemu_send_packet(s
->vc
,
122 (void *) &s
->regs
[base
],
123 s
->regs
[base
+ R_TX_LEN0
]);
124 D(qemu_log("eth_tx %d\n", s
->regs
[base
+ R_TX_LEN0
]));
125 if (s
->regs
[base
+ R_TX_CTRL0
] & CTRL_I
)
127 } else if ((value
& (CTRL_P
| CTRL_S
)) == (CTRL_P
| CTRL_S
)) {
128 memcpy(&s
->macaddr
[0], &s
->regs
[base
], 6);
129 if (s
->regs
[base
+ R_TX_CTRL0
] & CTRL_I
)
133 /* We are fast and get ready pretty much immediately so
134 we actually never flip the S nor P bits to one. */
135 s
->regs
[addr
] = value
& ~(CTRL_P
| CTRL_S
);
138 /* Keep these native. */
144 D(qemu_log("%s addr=%x val=%x\n", __func__
, addr
* 4, value
));
145 s
->regs
[addr
] = value
;
148 /* Packet data, make sure it stays BE. */
150 s
->regs
[addr
] = cpu_to_be32(value
);
155 static CPUReadMemoryFunc
*eth_read
[] = {
156 NULL
, NULL
, ð_readl
,
159 static CPUWriteMemoryFunc
*eth_write
[] = {
160 NULL
, NULL
, ð_writel
,
163 static int eth_can_rx(VLANClientState
*vc
)
165 struct xlx_ethlite
*s
= vc
->opaque
;
167 r
= !(s
->regs
[R_RX_CTRL0
] & CTRL_S
);
168 qemu_log("%s %d\n", __func__
, r
);
172 static ssize_t
eth_rx(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
174 struct xlx_ethlite
*s
= vc
->opaque
;
175 unsigned int rxbase
= s
->rxbuf
* (0x800 / 4);
179 if (!(buf
[0] & 0x80) && memcmp(&s
->macaddr
[0], buf
, 6))
182 if (s
->regs
[rxbase
+ R_RX_CTRL0
] & CTRL_S
) {
183 D(qemu_log("ethlite lost packet %x\n", s
->regs
[R_RX_CTRL0
]));
187 D(qemu_log("%s %d rxbase=%x\n", __func__
, size
, rxbase
));
188 memcpy(&s
->regs
[rxbase
+ R_RX_BUF0
], buf
, size
);
190 /* Bring it into host endianess. */
191 for (i
= 0; i
< ((size
+ 3) / 4); i
++) {
192 uint32_t d
= s
->regs
[rxbase
+ R_RX_BUF0
+ i
];
193 s
->regs
[rxbase
+ R_RX_BUF0
+ i
] = be32_to_cpu(d
);
196 s
->regs
[rxbase
+ R_RX_CTRL0
] |= CTRL_S
;
197 if (s
->regs
[rxbase
+ R_RX_CTRL0
] & CTRL_I
)
200 /* If c_rx_pingpong was set flip buffers. */
201 s
->rxbuf
^= s
->c_rx_pingpong
;
205 static void eth_cleanup(VLANClientState
*vc
)
207 struct xlx_ethlite
*s
= vc
->opaque
;
211 static void xilinx_ethlite_init(SysBusDevice
*dev
)
213 struct xlx_ethlite
*s
= FROM_SYSBUS(typeof (*s
), dev
);
216 sysbus_init_irq(dev
, &s
->irq
);
217 s
->c_tx_pingpong
= qdev_get_prop_int(&dev
->qdev
, "txpingpong", 1);
218 s
->c_rx_pingpong
= qdev_get_prop_int(&dev
->qdev
, "rxpingpong", 1);
221 regs
= cpu_register_io_memory(0, eth_read
, eth_write
, s
);
222 sysbus_init_mmio(dev
, R_MAX
* 4, regs
);
224 qdev_get_macaddr(&dev
->qdev
, s
->macaddr
);
225 s
->vc
= qdev_get_vlan_client(&dev
->qdev
,
226 eth_can_rx
, eth_rx
, NULL
, eth_cleanup
, s
);
229 static void xilinx_ethlite_register(void)
231 sysbus_register_dev("xilinx,ethlite", sizeof (struct xlx_ethlite
),
232 xilinx_ethlite_init
);
235 device_init(xilinx_ethlite_register
)