2009-05-20 18:13:24 +00:00
|
|
|
/*
|
|
|
|
* QEMU model of the Xilinx Ethernet Lite MAC.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009 Edgar E. Iglesias.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sysbus.h"
|
|
|
|
#include "hw.h"
|
|
|
|
#include "net.h"
|
|
|
|
|
|
|
|
#define D(x)
|
|
|
|
#define R_TX_BUF0 0
|
|
|
|
#define R_TX_LEN0 (0x07f4 / 4)
|
|
|
|
#define R_TX_GIE0 (0x07f8 / 4)
|
|
|
|
#define R_TX_CTRL0 (0x07fc / 4)
|
|
|
|
#define R_TX_BUF1 (0x0800 / 4)
|
|
|
|
#define R_TX_LEN1 (0x0ff4 / 4)
|
|
|
|
#define R_TX_CTRL1 (0x0ffc / 4)
|
|
|
|
|
|
|
|
#define R_RX_BUF0 (0x1000 / 4)
|
|
|
|
#define R_RX_CTRL0 (0x17fc / 4)
|
|
|
|
#define R_RX_BUF1 (0x1800 / 4)
|
|
|
|
#define R_RX_CTRL1 (0x1ffc / 4)
|
|
|
|
#define R_MAX (0x2000 / 4)
|
|
|
|
|
|
|
|
#define GIE_GIE 0x80000000
|
|
|
|
|
|
|
|
#define CTRL_I 0x8
|
|
|
|
#define CTRL_P 0x2
|
|
|
|
#define CTRL_S 0x1
|
|
|
|
|
|
|
|
struct xlx_ethlite
|
|
|
|
{
|
|
|
|
SysBusDevice busdev;
|
2011-08-25 22:13:47 +00:00
|
|
|
MemoryRegion mmio;
|
2009-05-20 18:13:24 +00:00
|
|
|
qemu_irq irq;
|
2009-11-25 18:49:26 +00:00
|
|
|
NICState *nic;
|
2009-10-21 13:25:38 +00:00
|
|
|
NICConf conf;
|
2009-05-20 18:13:24 +00:00
|
|
|
|
2009-07-15 11:43:31 +00:00
|
|
|
uint32_t c_tx_pingpong;
|
|
|
|
uint32_t c_rx_pingpong;
|
2009-05-20 18:13:24 +00:00
|
|
|
unsigned int txbuf;
|
|
|
|
unsigned int rxbuf;
|
|
|
|
|
|
|
|
uint32_t regs[R_MAX];
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline void eth_pulse_irq(struct xlx_ethlite *s)
|
|
|
|
{
|
|
|
|
/* Only the first gie reg is active. */
|
|
|
|
if (s->regs[R_TX_GIE0] & GIE_GIE) {
|
|
|
|
qemu_irq_pulse(s->irq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-25 22:13:47 +00:00
|
|
|
static uint64_t
|
|
|
|
eth_read(void *opaque, target_phys_addr_t addr, unsigned int size)
|
2009-05-20 18:13:24 +00:00
|
|
|
{
|
|
|
|
struct xlx_ethlite *s = opaque;
|
|
|
|
uint32_t r = 0;
|
|
|
|
|
|
|
|
addr >>= 2;
|
|
|
|
|
|
|
|
switch (addr)
|
|
|
|
{
|
|
|
|
case R_TX_GIE0:
|
|
|
|
case R_TX_LEN0:
|
|
|
|
case R_TX_LEN1:
|
|
|
|
case R_TX_CTRL1:
|
|
|
|
case R_TX_CTRL0:
|
|
|
|
case R_RX_CTRL1:
|
|
|
|
case R_RX_CTRL0:
|
|
|
|
r = s->regs[addr];
|
|
|
|
D(qemu_log("%s %x=%x\n", __func__, addr * 4, r));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-03-10 08:16:52 +00:00
|
|
|
r = tswap32(s->regs[addr]);
|
2009-05-20 18:13:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-08-25 22:13:47 +00:00
|
|
|
eth_write(void *opaque, target_phys_addr_t addr,
|
|
|
|
uint64_t val64, unsigned int size)
|
2009-05-20 18:13:24 +00:00
|
|
|
{
|
|
|
|
struct xlx_ethlite *s = opaque;
|
|
|
|
unsigned int base = 0;
|
2011-08-25 22:13:47 +00:00
|
|
|
uint32_t value = val64;
|
2009-05-20 18:13:24 +00:00
|
|
|
|
|
|
|
addr >>= 2;
|
|
|
|
switch (addr)
|
|
|
|
{
|
|
|
|
case R_TX_CTRL0:
|
|
|
|
case R_TX_CTRL1:
|
|
|
|
if (addr == R_TX_CTRL1)
|
|
|
|
base = 0x800 / 4;
|
|
|
|
|
|
|
|
D(qemu_log("%s addr=%x val=%x\n", __func__, addr * 4, value));
|
|
|
|
if ((value & (CTRL_P | CTRL_S)) == CTRL_S) {
|
2009-11-25 18:49:26 +00:00
|
|
|
qemu_send_packet(&s->nic->nc,
|
2009-05-20 18:13:24 +00:00
|
|
|
(void *) &s->regs[base],
|
|
|
|
s->regs[base + R_TX_LEN0]);
|
|
|
|
D(qemu_log("eth_tx %d\n", s->regs[base + R_TX_LEN0]));
|
|
|
|
if (s->regs[base + R_TX_CTRL0] & CTRL_I)
|
|
|
|
eth_pulse_irq(s);
|
|
|
|
} else if ((value & (CTRL_P | CTRL_S)) == (CTRL_P | CTRL_S)) {
|
2009-10-21 13:25:38 +00:00
|
|
|
memcpy(&s->conf.macaddr.a[0], &s->regs[base], 6);
|
2009-05-20 18:13:24 +00:00
|
|
|
if (s->regs[base + R_TX_CTRL0] & CTRL_I)
|
|
|
|
eth_pulse_irq(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are fast and get ready pretty much immediately so
|
|
|
|
we actually never flip the S nor P bits to one. */
|
|
|
|
s->regs[addr] = value & ~(CTRL_P | CTRL_S);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Keep these native. */
|
|
|
|
case R_TX_LEN0:
|
|
|
|
case R_TX_LEN1:
|
|
|
|
case R_TX_GIE0:
|
|
|
|
case R_RX_CTRL0:
|
|
|
|
case R_RX_CTRL1:
|
|
|
|
D(qemu_log("%s addr=%x val=%x\n", __func__, addr * 4, value));
|
|
|
|
s->regs[addr] = value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-03-10 08:16:52 +00:00
|
|
|
s->regs[addr] = tswap32(value);
|
2009-05-20 18:13:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-25 22:13:47 +00:00
|
|
|
static const MemoryRegionOps eth_ops = {
|
|
|
|
.read = eth_read,
|
|
|
|
.write = eth_write,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
|
|
|
.valid = {
|
|
|
|
.min_access_size = 4,
|
|
|
|
.max_access_size = 4
|
|
|
|
}
|
2009-05-20 18:13:24 +00:00
|
|
|
};
|
|
|
|
|
2009-11-25 18:49:26 +00:00
|
|
|
static int eth_can_rx(VLANClientState *nc)
|
2009-05-20 18:13:24 +00:00
|
|
|
{
|
2009-11-25 18:49:26 +00:00
|
|
|
struct xlx_ethlite *s = DO_UPCAST(NICState, nc, nc)->opaque;
|
2009-05-20 18:13:24 +00:00
|
|
|
int r;
|
|
|
|
r = !(s->regs[R_RX_CTRL0] & CTRL_S);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2009-11-25 18:49:26 +00:00
|
|
|
static ssize_t eth_rx(VLANClientState *nc, const uint8_t *buf, size_t size)
|
2009-05-20 18:13:24 +00:00
|
|
|
{
|
2009-11-25 18:49:26 +00:00
|
|
|
struct xlx_ethlite *s = DO_UPCAST(NICState, nc, nc)->opaque;
|
2009-05-20 18:13:24 +00:00
|
|
|
unsigned int rxbase = s->rxbuf * (0x800 / 4);
|
|
|
|
|
|
|
|
/* DA filter. */
|
2009-10-21 13:25:38 +00:00
|
|
|
if (!(buf[0] & 0x80) && memcmp(&s->conf.macaddr.a[0], buf, 6))
|
2009-06-11 09:42:26 +00:00
|
|
|
return size;
|
2009-05-20 18:13:24 +00:00
|
|
|
|
|
|
|
if (s->regs[rxbase + R_RX_CTRL0] & CTRL_S) {
|
|
|
|
D(qemu_log("ethlite lost packet %x\n", s->regs[R_RX_CTRL0]));
|
2009-06-11 09:42:26 +00:00
|
|
|
return -1;
|
2009-05-20 18:13:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
D(qemu_log("%s %d rxbase=%x\n", __func__, size, rxbase));
|
|
|
|
memcpy(&s->regs[rxbase + R_RX_BUF0], buf, size);
|
|
|
|
|
|
|
|
s->regs[rxbase + R_RX_CTRL0] |= CTRL_S;
|
|
|
|
if (s->regs[rxbase + R_RX_CTRL0] & CTRL_I)
|
|
|
|
eth_pulse_irq(s);
|
|
|
|
|
|
|
|
/* If c_rx_pingpong was set flip buffers. */
|
|
|
|
s->rxbuf ^= s->c_rx_pingpong;
|
2009-06-11 09:42:26 +00:00
|
|
|
return size;
|
2009-05-20 18:13:24 +00:00
|
|
|
}
|
|
|
|
|
2009-11-25 18:49:26 +00:00
|
|
|
static void eth_cleanup(VLANClientState *nc)
|
2009-05-20 18:13:24 +00:00
|
|
|
{
|
2009-11-25 18:49:26 +00:00
|
|
|
struct xlx_ethlite *s = DO_UPCAST(NICState, nc, nc)->opaque;
|
2009-10-21 13:25:38 +00:00
|
|
|
|
2009-11-25 18:49:26 +00:00
|
|
|
s->nic = NULL;
|
2009-05-20 18:13:24 +00:00
|
|
|
}
|
|
|
|
|
2009-11-25 18:49:26 +00:00
|
|
|
static NetClientInfo net_xilinx_ethlite_info = {
|
|
|
|
.type = NET_CLIENT_TYPE_NIC,
|
|
|
|
.size = sizeof(NICState),
|
|
|
|
.can_receive = eth_can_rx,
|
|
|
|
.receive = eth_rx,
|
|
|
|
.cleanup = eth_cleanup,
|
|
|
|
};
|
|
|
|
|
2009-08-14 08:36:05 +00:00
|
|
|
static int xilinx_ethlite_init(SysBusDevice *dev)
|
2009-05-20 18:13:24 +00:00
|
|
|
{
|
|
|
|
struct xlx_ethlite *s = FROM_SYSBUS(typeof (*s), dev);
|
|
|
|
|
|
|
|
sysbus_init_irq(dev, &s->irq);
|
|
|
|
s->rxbuf = 0;
|
|
|
|
|
2011-08-25 22:13:47 +00:00
|
|
|
memory_region_init_io(&s->mmio, ð_ops, s, "xilinx-ethlite", R_MAX * 4);
|
2011-11-27 09:38:10 +00:00
|
|
|
sysbus_init_mmio(dev, &s->mmio);
|
2009-05-20 18:13:24 +00:00
|
|
|
|
2009-10-21 13:25:38 +00:00
|
|
|
qemu_macaddr_default_if_unset(&s->conf.macaddr);
|
2009-11-25 18:49:26 +00:00
|
|
|
s->nic = qemu_new_nic(&net_xilinx_ethlite_info, &s->conf,
|
2011-12-04 17:17:51 +00:00
|
|
|
object_get_typename(OBJECT(dev)), dev->qdev.id, s);
|
2009-11-25 18:49:26 +00:00
|
|
|
qemu_format_nic_info_str(&s->nic->nc, s->conf.macaddr.a);
|
2009-08-14 08:36:05 +00:00
|
|
|
return 0;
|
2009-05-20 18:13:24 +00:00
|
|
|
}
|
|
|
|
|
2012-01-24 19:12:29 +00:00
|
|
|
static Property xilinx_ethlite_properties[] = {
|
|
|
|
DEFINE_PROP_UINT32("txpingpong", struct xlx_ethlite, c_tx_pingpong, 1),
|
|
|
|
DEFINE_PROP_UINT32("rxpingpong", struct xlx_ethlite, c_rx_pingpong, 1),
|
|
|
|
DEFINE_NIC_PROPERTIES(struct xlx_ethlite, conf),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void xilinx_ethlite_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 03:34:16 +00:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2012-01-24 19:12:29 +00:00
|
|
|
SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
|
|
|
|
|
|
|
|
k->init = xilinx_ethlite_init;
|
2011-12-08 03:34:16 +00:00
|
|
|
dc->props = xilinx_ethlite_properties;
|
2012-01-24 19:12:29 +00:00
|
|
|
}
|
|
|
|
|
2011-12-08 03:34:16 +00:00
|
|
|
static TypeInfo xilinx_ethlite_info = {
|
|
|
|
.name = "xilinx,ethlite",
|
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
|
|
|
.instance_size = sizeof(struct xlx_ethlite),
|
|
|
|
.class_init = xilinx_ethlite_class_init,
|
2009-07-15 11:43:31 +00:00
|
|
|
};
|
|
|
|
|
2009-05-20 18:13:24 +00:00
|
|
|
static void xilinx_ethlite_register(void)
|
|
|
|
{
|
2011-12-08 03:34:16 +00:00
|
|
|
type_register_static(&xilinx_ethlite_info);
|
2009-05-20 18:13:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
device_init(xilinx_ethlite_register)
|