Statistics
| Branch: | Revision:

root / hw / xilinx_ethlite.c @ 0b8b716d

History | View | Annotate | Download (6.8 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
        default:
94 d48751ed Edgar E. Iglesias
            r = tswap32(s->regs[addr]);
95 b43848a1 Edgar E. Iglesias
            break;
96 b43848a1 Edgar E. Iglesias
    }
97 b43848a1 Edgar E. Iglesias
    return r;
98 b43848a1 Edgar E. Iglesias
}
99 b43848a1 Edgar E. Iglesias
100 b43848a1 Edgar E. Iglesias
static void
101 c227f099 Anthony Liguori
eth_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
102 b43848a1 Edgar E. Iglesias
{
103 b43848a1 Edgar E. Iglesias
    struct xlx_ethlite *s = opaque;
104 b43848a1 Edgar E. Iglesias
    unsigned int base = 0;
105 b43848a1 Edgar E. Iglesias
106 b43848a1 Edgar E. Iglesias
    addr >>= 2;
107 b43848a1 Edgar E. Iglesias
    switch (addr) 
108 b43848a1 Edgar E. Iglesias
    {
109 b43848a1 Edgar E. Iglesias
        case R_TX_CTRL0:
110 b43848a1 Edgar E. Iglesias
        case R_TX_CTRL1:
111 b43848a1 Edgar E. Iglesias
            if (addr == R_TX_CTRL1)
112 b43848a1 Edgar E. Iglesias
                base = 0x800 / 4;
113 b43848a1 Edgar E. Iglesias
114 b43848a1 Edgar E. Iglesias
            D(qemu_log("%s addr=%x val=%x\n", __func__, addr * 4, value));
115 b43848a1 Edgar E. Iglesias
            if ((value & (CTRL_P | CTRL_S)) == CTRL_S) {
116 d7539ab4 Mark McLoughlin
                qemu_send_packet(&s->nic->nc,
117 b43848a1 Edgar E. Iglesias
                                 (void *) &s->regs[base],
118 b43848a1 Edgar E. Iglesias
                                 s->regs[base + R_TX_LEN0]);
119 b43848a1 Edgar E. Iglesias
                D(qemu_log("eth_tx %d\n", s->regs[base + R_TX_LEN0]));
120 b43848a1 Edgar E. Iglesias
                if (s->regs[base + R_TX_CTRL0] & CTRL_I)
121 b43848a1 Edgar E. Iglesias
                    eth_pulse_irq(s);
122 b43848a1 Edgar E. Iglesias
            } else if ((value & (CTRL_P | CTRL_S)) == (CTRL_P | CTRL_S)) {
123 17d1ae3c Gerd Hoffmann
                memcpy(&s->conf.macaddr.a[0], &s->regs[base], 6);
124 b43848a1 Edgar E. Iglesias
                if (s->regs[base + R_TX_CTRL0] & CTRL_I)
125 b43848a1 Edgar E. Iglesias
                    eth_pulse_irq(s);
126 b43848a1 Edgar E. Iglesias
            }
127 b43848a1 Edgar E. Iglesias
128 b43848a1 Edgar E. Iglesias
            /* We are fast and get ready pretty much immediately so
129 b43848a1 Edgar E. Iglesias
               we actually never flip the S nor P bits to one.  */
130 b43848a1 Edgar E. Iglesias
            s->regs[addr] = value & ~(CTRL_P | CTRL_S);
131 b43848a1 Edgar E. Iglesias
            break;
132 b43848a1 Edgar E. Iglesias
133 b43848a1 Edgar E. Iglesias
        /* Keep these native.  */
134 b43848a1 Edgar E. Iglesias
        case R_TX_LEN0:
135 b43848a1 Edgar E. Iglesias
        case R_TX_LEN1:
136 b43848a1 Edgar E. Iglesias
        case R_TX_GIE0:
137 b43848a1 Edgar E. Iglesias
        case R_RX_CTRL0:
138 b43848a1 Edgar E. Iglesias
        case R_RX_CTRL1:
139 b43848a1 Edgar E. Iglesias
            D(qemu_log("%s addr=%x val=%x\n", __func__, addr * 4, value));
140 b43848a1 Edgar E. Iglesias
            s->regs[addr] = value;
141 b43848a1 Edgar E. Iglesias
            break;
142 b43848a1 Edgar E. Iglesias
143 b43848a1 Edgar E. Iglesias
        default:
144 d48751ed Edgar E. Iglesias
            s->regs[addr] = tswap32(value);
145 b43848a1 Edgar E. Iglesias
            break;
146 b43848a1 Edgar E. Iglesias
    }
147 b43848a1 Edgar E. Iglesias
}
148 b43848a1 Edgar E. Iglesias
149 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const eth_read[] = {
150 b43848a1 Edgar E. Iglesias
    NULL, NULL, &eth_readl,
151 b43848a1 Edgar E. Iglesias
};
152 b43848a1 Edgar E. Iglesias
153 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const eth_write[] = {
154 b43848a1 Edgar E. Iglesias
    NULL, NULL, &eth_writel,
155 b43848a1 Edgar E. Iglesias
};
156 b43848a1 Edgar E. Iglesias
157 d7539ab4 Mark McLoughlin
static int eth_can_rx(VLANClientState *nc)
158 b43848a1 Edgar E. Iglesias
{
159 d7539ab4 Mark McLoughlin
    struct xlx_ethlite *s = DO_UPCAST(NICState, nc, nc)->opaque;
160 b43848a1 Edgar E. Iglesias
    int r;
161 b43848a1 Edgar E. Iglesias
    r = !(s->regs[R_RX_CTRL0] & CTRL_S);
162 b43848a1 Edgar E. Iglesias
    return r;
163 b43848a1 Edgar E. Iglesias
}
164 b43848a1 Edgar E. Iglesias
165 d7539ab4 Mark McLoughlin
static ssize_t eth_rx(VLANClientState *nc, const uint8_t *buf, size_t size)
166 b43848a1 Edgar E. Iglesias
{
167 d7539ab4 Mark McLoughlin
    struct xlx_ethlite *s = DO_UPCAST(NICState, nc, nc)->opaque;
168 b43848a1 Edgar E. Iglesias
    unsigned int rxbase = s->rxbuf * (0x800 / 4);
169 b43848a1 Edgar E. Iglesias
170 b43848a1 Edgar E. Iglesias
    /* DA filter.  */
171 17d1ae3c Gerd Hoffmann
    if (!(buf[0] & 0x80) && memcmp(&s->conf.macaddr.a[0], buf, 6))
172 df12c1f5 Jan Kiszka
        return size;
173 b43848a1 Edgar E. Iglesias
174 b43848a1 Edgar E. Iglesias
    if (s->regs[rxbase + R_RX_CTRL0] & CTRL_S) {
175 b43848a1 Edgar E. Iglesias
        D(qemu_log("ethlite lost packet %x\n", s->regs[R_RX_CTRL0]));
176 df12c1f5 Jan Kiszka
        return -1;
177 b43848a1 Edgar E. Iglesias
    }
178 b43848a1 Edgar E. Iglesias
179 b43848a1 Edgar E. Iglesias
    D(qemu_log("%s %d rxbase=%x\n", __func__, size, rxbase));
180 b43848a1 Edgar E. Iglesias
    memcpy(&s->regs[rxbase + R_RX_BUF0], buf, size);
181 b43848a1 Edgar E. Iglesias
182 b43848a1 Edgar E. Iglesias
    s->regs[rxbase + R_RX_CTRL0] |= CTRL_S;
183 b43848a1 Edgar E. Iglesias
    if (s->regs[rxbase + R_RX_CTRL0] & CTRL_I)
184 b43848a1 Edgar E. Iglesias
        eth_pulse_irq(s);
185 b43848a1 Edgar E. Iglesias
186 b43848a1 Edgar E. Iglesias
    /* If c_rx_pingpong was set flip buffers.  */
187 b43848a1 Edgar E. Iglesias
    s->rxbuf ^= s->c_rx_pingpong;
188 df12c1f5 Jan Kiszka
    return size;
189 b43848a1 Edgar E. Iglesias
}
190 b43848a1 Edgar E. Iglesias
191 d7539ab4 Mark McLoughlin
static void eth_cleanup(VLANClientState *nc)
192 b43848a1 Edgar E. Iglesias
{
193 d7539ab4 Mark McLoughlin
    struct xlx_ethlite *s = DO_UPCAST(NICState, nc, nc)->opaque;
194 17d1ae3c Gerd Hoffmann
195 d7539ab4 Mark McLoughlin
    s->nic = NULL;
196 b43848a1 Edgar E. Iglesias
}
197 b43848a1 Edgar E. Iglesias
198 d7539ab4 Mark McLoughlin
static NetClientInfo net_xilinx_ethlite_info = {
199 d7539ab4 Mark McLoughlin
    .type = NET_CLIENT_TYPE_NIC,
200 d7539ab4 Mark McLoughlin
    .size = sizeof(NICState),
201 d7539ab4 Mark McLoughlin
    .can_receive = eth_can_rx,
202 d7539ab4 Mark McLoughlin
    .receive = eth_rx,
203 d7539ab4 Mark McLoughlin
    .cleanup = eth_cleanup,
204 d7539ab4 Mark McLoughlin
};
205 d7539ab4 Mark McLoughlin
206 81a322d4 Gerd Hoffmann
static int xilinx_ethlite_init(SysBusDevice *dev)
207 b43848a1 Edgar E. Iglesias
{
208 b43848a1 Edgar E. Iglesias
    struct xlx_ethlite *s = FROM_SYSBUS(typeof (*s), dev);
209 b43848a1 Edgar E. Iglesias
    int regs;
210 b43848a1 Edgar E. Iglesias
211 b43848a1 Edgar E. Iglesias
    sysbus_init_irq(dev, &s->irq);
212 b43848a1 Edgar E. Iglesias
    s->rxbuf = 0;
213 b43848a1 Edgar E. Iglesias
214 2507c12a Alexander Graf
    regs = cpu_register_io_memory(eth_read, eth_write, s, DEVICE_NATIVE_ENDIAN);
215 b43848a1 Edgar E. Iglesias
    sysbus_init_mmio(dev, R_MAX * 4, regs);
216 b43848a1 Edgar E. Iglesias
217 17d1ae3c Gerd Hoffmann
    qemu_macaddr_default_if_unset(&s->conf.macaddr);
218 d7539ab4 Mark McLoughlin
    s->nic = qemu_new_nic(&net_xilinx_ethlite_info, &s->conf,
219 d7539ab4 Mark McLoughlin
                          dev->qdev.info->name, dev->qdev.id, s);
220 d7539ab4 Mark McLoughlin
    qemu_format_nic_info_str(&s->nic->nc, s->conf.macaddr.a);
221 81a322d4 Gerd Hoffmann
    return 0;
222 b43848a1 Edgar E. Iglesias
}
223 b43848a1 Edgar E. Iglesias
224 ee6847d1 Gerd Hoffmann
static SysBusDeviceInfo xilinx_ethlite_info = {
225 ee6847d1 Gerd Hoffmann
    .init = xilinx_ethlite_init,
226 ee6847d1 Gerd Hoffmann
    .qdev.name  = "xilinx,ethlite",
227 ee6847d1 Gerd Hoffmann
    .qdev.size  = sizeof(struct xlx_ethlite),
228 ee6847d1 Gerd Hoffmann
    .qdev.props = (Property[]) {
229 05f02579 Gerd Hoffmann
        DEFINE_PROP_UINT32("txpingpong", struct xlx_ethlite, c_tx_pingpong, 1),
230 05f02579 Gerd Hoffmann
        DEFINE_PROP_UINT32("rxpingpong", struct xlx_ethlite, c_rx_pingpong, 1),
231 17d1ae3c Gerd Hoffmann
        DEFINE_NIC_PROPERTIES(struct xlx_ethlite, conf),
232 05f02579 Gerd Hoffmann
        DEFINE_PROP_END_OF_LIST(),
233 ee6847d1 Gerd Hoffmann
    }
234 ee6847d1 Gerd Hoffmann
};
235 ee6847d1 Gerd Hoffmann
236 b43848a1 Edgar E. Iglesias
static void xilinx_ethlite_register(void)
237 b43848a1 Edgar E. Iglesias
{
238 ee6847d1 Gerd Hoffmann
    sysbus_register_withprop(&xilinx_ethlite_info);
239 b43848a1 Edgar E. Iglesias
}
240 b43848a1 Edgar E. Iglesias
241 b43848a1 Edgar E. Iglesias
device_init(xilinx_ethlite_register)