root / hw / xilinx_ethlite.c @ f8b6d672
History | View | Annotate | Download (7.3 kB)
1 | b43848a1 | Edgar E. Iglesias | /*
|
---|---|---|---|
2 | b43848a1 | Edgar E. Iglesias | * QEMU model of the Xilinx Ethernet Lite MAC.
|
3 | b43848a1 | Edgar E. Iglesias | *
|
4 | b43848a1 | Edgar E. Iglesias | * Copyright (c) 2009 Edgar E. Iglesias.
|
5 | b43848a1 | Edgar E. Iglesias | *
|
6 | b43848a1 | Edgar E. Iglesias | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | b43848a1 | Edgar E. Iglesias | * of this software and associated documentation files (the "Software"), to deal
|
8 | b43848a1 | Edgar E. Iglesias | * in the Software without restriction, including without limitation the rights
|
9 | b43848a1 | Edgar E. Iglesias | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | b43848a1 | Edgar E. Iglesias | * copies of the Software, and to permit persons to whom the Software is
|
11 | b43848a1 | Edgar E. Iglesias | * furnished to do so, subject to the following conditions:
|
12 | b43848a1 | Edgar E. Iglesias | *
|
13 | b43848a1 | Edgar E. Iglesias | * The above copyright notice and this permission notice shall be included in
|
14 | b43848a1 | Edgar E. Iglesias | * all copies or substantial portions of the Software.
|
15 | b43848a1 | Edgar E. Iglesias | *
|
16 | b43848a1 | Edgar E. Iglesias | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | b43848a1 | Edgar E. Iglesias | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | b43848a1 | Edgar E. Iglesias | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | b43848a1 | Edgar E. Iglesias | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | b43848a1 | Edgar E. Iglesias | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | b43848a1 | Edgar E. Iglesias | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | b43848a1 | Edgar E. Iglesias | * THE SOFTWARE.
|
23 | b43848a1 | Edgar E. Iglesias | */
|
24 | b43848a1 | Edgar E. Iglesias | |
25 | b43848a1 | Edgar E. Iglesias | #include "sysbus.h" |
26 | b43848a1 | Edgar E. Iglesias | #include "hw.h" |
27 | b43848a1 | Edgar E. Iglesias | #include "net.h" |
28 | b43848a1 | Edgar E. Iglesias | |
29 | b43848a1 | Edgar E. Iglesias | #define D(x)
|
30 | b43848a1 | Edgar E. Iglesias | #define R_TX_BUF0 0 |
31 | b43848a1 | Edgar E. Iglesias | #define R_TX_LEN0 (0x07f4 / 4) |
32 | b43848a1 | Edgar E. Iglesias | #define R_TX_GIE0 (0x07f8 / 4) |
33 | b43848a1 | Edgar E. Iglesias | #define R_TX_CTRL0 (0x07fc / 4) |
34 | b43848a1 | Edgar E. Iglesias | #define R_TX_BUF1 (0x0800 / 4) |
35 | b43848a1 | Edgar E. Iglesias | #define R_TX_LEN1 (0x0ff4 / 4) |
36 | b43848a1 | Edgar E. Iglesias | #define R_TX_CTRL1 (0x0ffc / 4) |
37 | b43848a1 | Edgar E. Iglesias | |
38 | b43848a1 | Edgar E. Iglesias | #define R_RX_BUF0 (0x1000 / 4) |
39 | b43848a1 | Edgar E. Iglesias | #define R_RX_CTRL0 (0x17fc / 4) |
40 | b43848a1 | Edgar E. Iglesias | #define R_RX_BUF1 (0x1800 / 4) |
41 | b43848a1 | Edgar E. Iglesias | #define R_RX_CTRL1 (0x1ffc / 4) |
42 | b43848a1 | Edgar E. Iglesias | #define R_MAX (0x2000 / 4) |
43 | b43848a1 | Edgar E. Iglesias | |
44 | b43848a1 | Edgar E. Iglesias | #define GIE_GIE 0x80000000 |
45 | b43848a1 | Edgar E. Iglesias | |
46 | b43848a1 | Edgar E. Iglesias | #define CTRL_I 0x8 |
47 | b43848a1 | Edgar E. Iglesias | #define CTRL_P 0x2 |
48 | b43848a1 | Edgar E. Iglesias | #define CTRL_S 0x1 |
49 | b43848a1 | Edgar E. Iglesias | |
50 | b43848a1 | Edgar E. Iglesias | struct xlx_ethlite
|
51 | b43848a1 | Edgar E. Iglesias | { |
52 | b43848a1 | Edgar E. Iglesias | SysBusDevice busdev; |
53 | b43848a1 | Edgar E. Iglesias | qemu_irq irq; |
54 | d7539ab4 | Mark McLoughlin | NICState *nic; |
55 | 17d1ae3c | Gerd Hoffmann | NICConf conf; |
56 | b43848a1 | Edgar E. Iglesias | |
57 | ee6847d1 | Gerd Hoffmann | uint32_t c_tx_pingpong; |
58 | ee6847d1 | Gerd Hoffmann | uint32_t c_rx_pingpong; |
59 | b43848a1 | Edgar E. Iglesias | unsigned int txbuf; |
60 | b43848a1 | Edgar E. Iglesias | unsigned int rxbuf; |
61 | b43848a1 | Edgar E. Iglesias | |
62 | b43848a1 | Edgar E. Iglesias | uint32_t regs[R_MAX]; |
63 | b43848a1 | Edgar E. Iglesias | }; |
64 | b43848a1 | Edgar E. Iglesias | |
65 | b43848a1 | Edgar E. Iglesias | static inline void eth_pulse_irq(struct xlx_ethlite *s) |
66 | b43848a1 | Edgar E. Iglesias | { |
67 | b43848a1 | Edgar E. Iglesias | /* Only the first gie reg is active. */
|
68 | b43848a1 | Edgar E. Iglesias | if (s->regs[R_TX_GIE0] & GIE_GIE) {
|
69 | b43848a1 | Edgar E. Iglesias | qemu_irq_pulse(s->irq); |
70 | b43848a1 | Edgar E. Iglesias | } |
71 | b43848a1 | Edgar E. Iglesias | } |
72 | b43848a1 | Edgar E. Iglesias | |
73 | c227f099 | Anthony Liguori | static uint32_t eth_readl (void *opaque, target_phys_addr_t addr) |
74 | b43848a1 | Edgar E. Iglesias | { |
75 | b43848a1 | Edgar E. Iglesias | struct xlx_ethlite *s = opaque;
|
76 | b43848a1 | Edgar E. Iglesias | uint32_t r = 0;
|
77 | b43848a1 | Edgar E. Iglesias | |
78 | b43848a1 | Edgar E. Iglesias | addr >>= 2;
|
79 | b43848a1 | Edgar E. Iglesias | |
80 | b43848a1 | Edgar E. Iglesias | switch (addr)
|
81 | b43848a1 | Edgar E. Iglesias | { |
82 | b43848a1 | Edgar E. Iglesias | case R_TX_GIE0:
|
83 | b43848a1 | Edgar E. Iglesias | case R_TX_LEN0:
|
84 | b43848a1 | Edgar E. Iglesias | case R_TX_LEN1:
|
85 | b43848a1 | Edgar E. Iglesias | case R_TX_CTRL1:
|
86 | b43848a1 | Edgar E. Iglesias | case R_TX_CTRL0:
|
87 | b43848a1 | Edgar E. Iglesias | case R_RX_CTRL1:
|
88 | b43848a1 | Edgar E. Iglesias | case R_RX_CTRL0:
|
89 | b43848a1 | Edgar E. Iglesias | r = s->regs[addr]; |
90 | b43848a1 | Edgar E. Iglesias | D(qemu_log("%s %x=%x\n", __func__, addr * 4, r)); |
91 | b43848a1 | Edgar E. Iglesias | break;
|
92 | b43848a1 | Edgar E. Iglesias | |
93 | b43848a1 | Edgar E. Iglesias | /* Rx packet data is endian fixed at the way into the rx rams. This
|
94 | b43848a1 | Edgar E. Iglesias | * speeds things up because the ethlite MAC does not have a len
|
95 | b43848a1 | Edgar E. Iglesias | * register. That means the CPU will issue MMIO reads for the entire
|
96 | b43848a1 | Edgar E. Iglesias | * 2k rx buffer even for small packets.
|
97 | b43848a1 | Edgar E. Iglesias | */
|
98 | b43848a1 | Edgar E. Iglesias | default:
|
99 | b43848a1 | Edgar E. Iglesias | r = s->regs[addr]; |
100 | b43848a1 | Edgar E. Iglesias | break;
|
101 | b43848a1 | Edgar E. Iglesias | } |
102 | b43848a1 | Edgar E. Iglesias | return r;
|
103 | b43848a1 | Edgar E. Iglesias | } |
104 | b43848a1 | Edgar E. Iglesias | |
105 | b43848a1 | Edgar E. Iglesias | static void |
106 | c227f099 | Anthony Liguori | eth_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
|
107 | b43848a1 | Edgar E. Iglesias | { |
108 | b43848a1 | Edgar E. Iglesias | struct xlx_ethlite *s = opaque;
|
109 | b43848a1 | Edgar E. Iglesias | unsigned int base = 0; |
110 | b43848a1 | Edgar E. Iglesias | |
111 | b43848a1 | Edgar E. Iglesias | addr >>= 2;
|
112 | b43848a1 | Edgar E. Iglesias | switch (addr)
|
113 | b43848a1 | Edgar E. Iglesias | { |
114 | b43848a1 | Edgar E. Iglesias | case R_TX_CTRL0:
|
115 | b43848a1 | Edgar E. Iglesias | case R_TX_CTRL1:
|
116 | b43848a1 | Edgar E. Iglesias | if (addr == R_TX_CTRL1)
|
117 | b43848a1 | Edgar E. Iglesias | base = 0x800 / 4; |
118 | b43848a1 | Edgar E. Iglesias | |
119 | b43848a1 | Edgar E. Iglesias | D(qemu_log("%s addr=%x val=%x\n", __func__, addr * 4, value)); |
120 | b43848a1 | Edgar E. Iglesias | if ((value & (CTRL_P | CTRL_S)) == CTRL_S) {
|
121 | d7539ab4 | Mark McLoughlin | qemu_send_packet(&s->nic->nc, |
122 | b43848a1 | Edgar E. Iglesias | (void *) &s->regs[base],
|
123 | b43848a1 | Edgar E. Iglesias | s->regs[base + R_TX_LEN0]); |
124 | b43848a1 | Edgar E. Iglesias | D(qemu_log("eth_tx %d\n", s->regs[base + R_TX_LEN0]));
|
125 | b43848a1 | Edgar E. Iglesias | if (s->regs[base + R_TX_CTRL0] & CTRL_I)
|
126 | b43848a1 | Edgar E. Iglesias | eth_pulse_irq(s); |
127 | b43848a1 | Edgar E. Iglesias | } else if ((value & (CTRL_P | CTRL_S)) == (CTRL_P | CTRL_S)) { |
128 | 17d1ae3c | Gerd Hoffmann | memcpy(&s->conf.macaddr.a[0], &s->regs[base], 6); |
129 | b43848a1 | Edgar E. Iglesias | if (s->regs[base + R_TX_CTRL0] & CTRL_I)
|
130 | b43848a1 | Edgar E. Iglesias | eth_pulse_irq(s); |
131 | b43848a1 | Edgar E. Iglesias | } |
132 | b43848a1 | Edgar E. Iglesias | |
133 | b43848a1 | Edgar E. Iglesias | /* We are fast and get ready pretty much immediately so
|
134 | b43848a1 | Edgar E. Iglesias | we actually never flip the S nor P bits to one. */
|
135 | b43848a1 | Edgar E. Iglesias | s->regs[addr] = value & ~(CTRL_P | CTRL_S); |
136 | b43848a1 | Edgar E. Iglesias | break;
|
137 | b43848a1 | Edgar E. Iglesias | |
138 | b43848a1 | Edgar E. Iglesias | /* Keep these native. */
|
139 | b43848a1 | Edgar E. Iglesias | case R_TX_LEN0:
|
140 | b43848a1 | Edgar E. Iglesias | case R_TX_LEN1:
|
141 | b43848a1 | Edgar E. Iglesias | case R_TX_GIE0:
|
142 | b43848a1 | Edgar E. Iglesias | case R_RX_CTRL0:
|
143 | b43848a1 | Edgar E. Iglesias | case R_RX_CTRL1:
|
144 | b43848a1 | Edgar E. Iglesias | D(qemu_log("%s addr=%x val=%x\n", __func__, addr * 4, value)); |
145 | b43848a1 | Edgar E. Iglesias | s->regs[addr] = value; |
146 | b43848a1 | Edgar E. Iglesias | break;
|
147 | b43848a1 | Edgar E. Iglesias | |
148 | b43848a1 | Edgar E. Iglesias | /* Packet data, make sure it stays BE. */
|
149 | b43848a1 | Edgar E. Iglesias | default:
|
150 | b43848a1 | Edgar E. Iglesias | s->regs[addr] = cpu_to_be32(value); |
151 | b43848a1 | Edgar E. Iglesias | break;
|
152 | b43848a1 | Edgar E. Iglesias | } |
153 | b43848a1 | Edgar E. Iglesias | } |
154 | b43848a1 | Edgar E. Iglesias | |
155 | d60efc6b | Blue Swirl | static CPUReadMemoryFunc * const eth_read[] = { |
156 | b43848a1 | Edgar E. Iglesias | NULL, NULL, ð_readl, |
157 | b43848a1 | Edgar E. Iglesias | }; |
158 | b43848a1 | Edgar E. Iglesias | |
159 | d60efc6b | Blue Swirl | static CPUWriteMemoryFunc * const eth_write[] = { |
160 | b43848a1 | Edgar E. Iglesias | NULL, NULL, ð_writel, |
161 | b43848a1 | Edgar E. Iglesias | }; |
162 | b43848a1 | Edgar E. Iglesias | |
163 | d7539ab4 | Mark McLoughlin | static int eth_can_rx(VLANClientState *nc) |
164 | b43848a1 | Edgar E. Iglesias | { |
165 | d7539ab4 | Mark McLoughlin | struct xlx_ethlite *s = DO_UPCAST(NICState, nc, nc)->opaque;
|
166 | b43848a1 | Edgar E. Iglesias | int r;
|
167 | b43848a1 | Edgar E. Iglesias | r = !(s->regs[R_RX_CTRL0] & CTRL_S); |
168 | b43848a1 | Edgar E. Iglesias | return r;
|
169 | b43848a1 | Edgar E. Iglesias | } |
170 | b43848a1 | Edgar E. Iglesias | |
171 | d7539ab4 | Mark McLoughlin | static ssize_t eth_rx(VLANClientState *nc, const uint8_t *buf, size_t size) |
172 | b43848a1 | Edgar E. Iglesias | { |
173 | d7539ab4 | Mark McLoughlin | struct xlx_ethlite *s = DO_UPCAST(NICState, nc, nc)->opaque;
|
174 | b43848a1 | Edgar E. Iglesias | unsigned int rxbase = s->rxbuf * (0x800 / 4); |
175 | b43848a1 | Edgar E. Iglesias | int i;
|
176 | b43848a1 | Edgar E. Iglesias | |
177 | b43848a1 | Edgar E. Iglesias | /* DA filter. */
|
178 | 17d1ae3c | Gerd Hoffmann | if (!(buf[0] & 0x80) && memcmp(&s->conf.macaddr.a[0], buf, 6)) |
179 | df12c1f5 | Jan Kiszka | return size;
|
180 | b43848a1 | Edgar E. Iglesias | |
181 | b43848a1 | Edgar E. Iglesias | if (s->regs[rxbase + R_RX_CTRL0] & CTRL_S) {
|
182 | b43848a1 | Edgar E. Iglesias | D(qemu_log("ethlite lost packet %x\n", s->regs[R_RX_CTRL0]));
|
183 | df12c1f5 | Jan Kiszka | return -1; |
184 | b43848a1 | Edgar E. Iglesias | } |
185 | b43848a1 | Edgar E. Iglesias | |
186 | b43848a1 | Edgar E. Iglesias | D(qemu_log("%s %d rxbase=%x\n", __func__, size, rxbase));
|
187 | b43848a1 | Edgar E. Iglesias | memcpy(&s->regs[rxbase + R_RX_BUF0], buf, size); |
188 | b43848a1 | Edgar E. Iglesias | |
189 | b43848a1 | Edgar E. Iglesias | /* Bring it into host endianess. */
|
190 | b43848a1 | Edgar E. Iglesias | for (i = 0; i < ((size + 3) / 4); i++) { |
191 | b43848a1 | Edgar E. Iglesias | uint32_t d = s->regs[rxbase + R_RX_BUF0 + i]; |
192 | b43848a1 | Edgar E. Iglesias | s->regs[rxbase + R_RX_BUF0 + i] = be32_to_cpu(d); |
193 | b43848a1 | Edgar E. Iglesias | } |
194 | b43848a1 | Edgar E. Iglesias | |
195 | b43848a1 | Edgar E. Iglesias | s->regs[rxbase + R_RX_CTRL0] |= CTRL_S; |
196 | b43848a1 | Edgar E. Iglesias | if (s->regs[rxbase + R_RX_CTRL0] & CTRL_I)
|
197 | b43848a1 | Edgar E. Iglesias | eth_pulse_irq(s); |
198 | b43848a1 | Edgar E. Iglesias | |
199 | b43848a1 | Edgar E. Iglesias | /* If c_rx_pingpong was set flip buffers. */
|
200 | b43848a1 | Edgar E. Iglesias | s->rxbuf ^= s->c_rx_pingpong; |
201 | df12c1f5 | Jan Kiszka | return size;
|
202 | b43848a1 | Edgar E. Iglesias | } |
203 | b43848a1 | Edgar E. Iglesias | |
204 | d7539ab4 | Mark McLoughlin | static void eth_cleanup(VLANClientState *nc) |
205 | b43848a1 | Edgar E. Iglesias | { |
206 | d7539ab4 | Mark McLoughlin | struct xlx_ethlite *s = DO_UPCAST(NICState, nc, nc)->opaque;
|
207 | 17d1ae3c | Gerd Hoffmann | |
208 | d7539ab4 | Mark McLoughlin | s->nic = NULL;
|
209 | b43848a1 | Edgar E. Iglesias | } |
210 | b43848a1 | Edgar E. Iglesias | |
211 | d7539ab4 | Mark McLoughlin | static NetClientInfo net_xilinx_ethlite_info = {
|
212 | d7539ab4 | Mark McLoughlin | .type = NET_CLIENT_TYPE_NIC, |
213 | d7539ab4 | Mark McLoughlin | .size = sizeof(NICState),
|
214 | d7539ab4 | Mark McLoughlin | .can_receive = eth_can_rx, |
215 | d7539ab4 | Mark McLoughlin | .receive = eth_rx, |
216 | d7539ab4 | Mark McLoughlin | .cleanup = eth_cleanup, |
217 | d7539ab4 | Mark McLoughlin | }; |
218 | d7539ab4 | Mark McLoughlin | |
219 | 81a322d4 | Gerd Hoffmann | static int xilinx_ethlite_init(SysBusDevice *dev) |
220 | b43848a1 | Edgar E. Iglesias | { |
221 | b43848a1 | Edgar E. Iglesias | struct xlx_ethlite *s = FROM_SYSBUS(typeof (*s), dev);
|
222 | b43848a1 | Edgar E. Iglesias | int regs;
|
223 | b43848a1 | Edgar E. Iglesias | |
224 | b43848a1 | Edgar E. Iglesias | sysbus_init_irq(dev, &s->irq); |
225 | b43848a1 | Edgar E. Iglesias | s->rxbuf = 0;
|
226 | b43848a1 | Edgar E. Iglesias | |
227 | 1eed09cb | Avi Kivity | regs = cpu_register_io_memory(eth_read, eth_write, s); |
228 | b43848a1 | Edgar E. Iglesias | sysbus_init_mmio(dev, R_MAX * 4, regs);
|
229 | b43848a1 | Edgar E. Iglesias | |
230 | 17d1ae3c | Gerd Hoffmann | qemu_macaddr_default_if_unset(&s->conf.macaddr); |
231 | d7539ab4 | Mark McLoughlin | s->nic = qemu_new_nic(&net_xilinx_ethlite_info, &s->conf, |
232 | d7539ab4 | Mark McLoughlin | dev->qdev.info->name, dev->qdev.id, s); |
233 | d7539ab4 | Mark McLoughlin | qemu_format_nic_info_str(&s->nic->nc, s->conf.macaddr.a); |
234 | 81a322d4 | Gerd Hoffmann | return 0; |
235 | b43848a1 | Edgar E. Iglesias | } |
236 | b43848a1 | Edgar E. Iglesias | |
237 | ee6847d1 | Gerd Hoffmann | static SysBusDeviceInfo xilinx_ethlite_info = {
|
238 | ee6847d1 | Gerd Hoffmann | .init = xilinx_ethlite_init, |
239 | ee6847d1 | Gerd Hoffmann | .qdev.name = "xilinx,ethlite",
|
240 | ee6847d1 | Gerd Hoffmann | .qdev.size = sizeof(struct xlx_ethlite), |
241 | ee6847d1 | Gerd Hoffmann | .qdev.props = (Property[]) { |
242 | 05f02579 | Gerd Hoffmann | DEFINE_PROP_UINT32("txpingpong", struct xlx_ethlite, c_tx_pingpong, 1), |
243 | 05f02579 | Gerd Hoffmann | DEFINE_PROP_UINT32("rxpingpong", struct xlx_ethlite, c_rx_pingpong, 1), |
244 | 17d1ae3c | Gerd Hoffmann | DEFINE_NIC_PROPERTIES(struct xlx_ethlite, conf),
|
245 | 05f02579 | Gerd Hoffmann | DEFINE_PROP_END_OF_LIST(), |
246 | ee6847d1 | Gerd Hoffmann | } |
247 | ee6847d1 | Gerd Hoffmann | }; |
248 | ee6847d1 | Gerd Hoffmann | |
249 | b43848a1 | Edgar E. Iglesias | static void xilinx_ethlite_register(void) |
250 | b43848a1 | Edgar E. Iglesias | { |
251 | ee6847d1 | Gerd Hoffmann | sysbus_register_withprop(&xilinx_ethlite_info); |
252 | b43848a1 | Edgar E. Iglesias | } |
253 | b43848a1 | Edgar E. Iglesias | |
254 | b43848a1 | Edgar E. Iglesias | device_init(xilinx_ethlite_register) |