Statistics
| Branch: | Revision:

root / hw / mipsnet.c @ a8fbaf96

History | View | Annotate | Download (7 kB)

1 87ecb68b pbrook
#include "hw.h"
2 87ecb68b pbrook
#include "mips.h"
3 87ecb68b pbrook
#include "net.h"
4 87ecb68b pbrook
#include "isa.h"
5 f0fc6f8f ths
6 57ba97de ths
//#define DEBUG_MIPSNET_SEND
7 57ba97de ths
//#define DEBUG_MIPSNET_RECEIVE
8 f0fc6f8f ths
//#define DEBUG_MIPSNET_DATA
9 57ba97de ths
//#define DEBUG_MIPSNET_IRQ
10 f0fc6f8f ths
11 f0fc6f8f ths
/* MIPSnet register offsets */
12 f0fc6f8f ths
13 f0fc6f8f ths
#define MIPSNET_DEV_ID                0x00
14 f0fc6f8f ths
# define MIPSNET_DEV_ID_STRING        "MIPSNET0"
15 f0fc6f8f ths
#define MIPSNET_BUSY                0x08
16 f0fc6f8f ths
#define MIPSNET_RX_DATA_COUNT        0x0c
17 f0fc6f8f ths
#define MIPSNET_TX_DATA_COUNT        0x10
18 f0fc6f8f ths
#define MIPSNET_INT_CTL                0x14
19 f0fc6f8f ths
# define MIPSNET_INTCTL_TXDONE                0x00000001
20 f0fc6f8f ths
# define MIPSNET_INTCTL_RXDONE                0x00000002
21 f0fc6f8f ths
# define MIPSNET_INTCTL_TESTBIT                0x80000000
22 f0fc6f8f ths
#define MIPSNET_INTERRUPT_INFO        0x18
23 f0fc6f8f ths
#define MIPSNET_RX_DATA_BUFFER        0x1c
24 f0fc6f8f ths
#define MIPSNET_TX_DATA_BUFFER        0x20
25 f0fc6f8f ths
26 f0fc6f8f ths
#define MAX_ETH_FRAME_SIZE        1514
27 f0fc6f8f ths
28 f0fc6f8f ths
typedef struct MIPSnetState {
29 f0fc6f8f ths
    uint32_t busy;
30 f0fc6f8f ths
    uint32_t rx_count;
31 f0fc6f8f ths
    uint32_t rx_read;
32 f0fc6f8f ths
    uint32_t tx_count;
33 f0fc6f8f ths
    uint32_t tx_written;
34 f0fc6f8f ths
    uint32_t intctl;
35 f0fc6f8f ths
    uint8_t rx_buffer[MAX_ETH_FRAME_SIZE];
36 f0fc6f8f ths
    uint8_t tx_buffer[MAX_ETH_FRAME_SIZE];
37 f0fc6f8f ths
    qemu_irq irq;
38 f0fc6f8f ths
    VLANClientState *vc;
39 f0fc6f8f ths
    NICInfo *nd;
40 f0fc6f8f ths
} MIPSnetState;
41 f0fc6f8f ths
42 f0fc6f8f ths
static void mipsnet_reset(MIPSnetState *s)
43 f0fc6f8f ths
{
44 f0fc6f8f ths
    s->busy = 1;
45 f0fc6f8f ths
    s->rx_count = 0;
46 f0fc6f8f ths
    s->rx_read = 0;
47 f0fc6f8f ths
    s->tx_count = 0;
48 f0fc6f8f ths
    s->tx_written = 0;
49 f0fc6f8f ths
    s->intctl = 0;
50 f0fc6f8f ths
    memset(s->rx_buffer, 0, MAX_ETH_FRAME_SIZE);
51 f0fc6f8f ths
    memset(s->tx_buffer, 0, MAX_ETH_FRAME_SIZE);
52 f0fc6f8f ths
}
53 f0fc6f8f ths
54 f0fc6f8f ths
static void mipsnet_update_irq(MIPSnetState *s)
55 f0fc6f8f ths
{
56 f0fc6f8f ths
    int isr = !!s->intctl;
57 f0fc6f8f ths
#ifdef DEBUG_MIPSNET_IRQ
58 f0fc6f8f ths
    printf("mipsnet: Set IRQ to %d (%02x)\n", isr, s->intctl);
59 f0fc6f8f ths
#endif
60 f0fc6f8f ths
    qemu_set_irq(s->irq, isr);
61 f0fc6f8f ths
}
62 f0fc6f8f ths
63 f0fc6f8f ths
static int mipsnet_buffer_full(MIPSnetState *s)
64 f0fc6f8f ths
{
65 f0fc6f8f ths
    if (s->rx_count >= MAX_ETH_FRAME_SIZE)
66 f0fc6f8f ths
        return 1;
67 f0fc6f8f ths
    return 0;
68 f0fc6f8f ths
}
69 f0fc6f8f ths
70 f0fc6f8f ths
static int mipsnet_can_receive(void *opaque)
71 f0fc6f8f ths
{
72 f0fc6f8f ths
    MIPSnetState *s = opaque;
73 f0fc6f8f ths
74 f0fc6f8f ths
    if (s->busy)
75 f0fc6f8f ths
        return 0;
76 f0fc6f8f ths
    return !mipsnet_buffer_full(s);
77 f0fc6f8f ths
}
78 f0fc6f8f ths
79 f0fc6f8f ths
static void mipsnet_receive(void *opaque, const uint8_t *buf, int size)
80 f0fc6f8f ths
{
81 f0fc6f8f ths
    MIPSnetState *s = opaque;
82 f0fc6f8f ths
83 f0fc6f8f ths
#ifdef DEBUG_MIPSNET_RECEIVE
84 f0fc6f8f ths
    printf("mipsnet: receiving len=%d\n", size);
85 f0fc6f8f ths
#endif
86 f0fc6f8f ths
    if (!mipsnet_can_receive(opaque))
87 f0fc6f8f ths
        return;
88 f0fc6f8f ths
89 f0fc6f8f ths
    s->busy = 1;
90 f0fc6f8f ths
91 f0fc6f8f ths
    /* Just accept everything. */
92 f0fc6f8f ths
93 f0fc6f8f ths
    /* Write packet data. */
94 f0fc6f8f ths
    memcpy(s->rx_buffer, buf, size);
95 f0fc6f8f ths
96 f0fc6f8f ths
    s->rx_count = size;
97 f0fc6f8f ths
    s->rx_read = 0;
98 f0fc6f8f ths
99 f0fc6f8f ths
    /* Now we can signal we have received something. */
100 f0fc6f8f ths
    s->intctl |= MIPSNET_INTCTL_RXDONE;
101 f0fc6f8f ths
    mipsnet_update_irq(s);
102 f0fc6f8f ths
}
103 f0fc6f8f ths
104 f0fc6f8f ths
static uint32_t mipsnet_ioport_read(void *opaque, uint32_t addr)
105 f0fc6f8f ths
{
106 f0fc6f8f ths
    MIPSnetState *s = opaque;
107 f0fc6f8f ths
    int ret = 0;
108 f0fc6f8f ths
    const char *devid = MIPSNET_DEV_ID_STRING;
109 f0fc6f8f ths
110 f0fc6f8f ths
    addr &= 0x3f;
111 f0fc6f8f ths
    switch (addr) {
112 f0fc6f8f ths
    case MIPSNET_DEV_ID:
113 f0fc6f8f ths
        ret = *((uint32_t *)&devid);
114 f0fc6f8f ths
        break;
115 f0fc6f8f ths
    case MIPSNET_DEV_ID + 4:
116 f0fc6f8f ths
        ret = *((uint32_t *)(&devid + 4));
117 f0fc6f8f ths
        break;
118 f0fc6f8f ths
    case MIPSNET_BUSY:
119 f0fc6f8f ths
        ret = s->busy;
120 f0fc6f8f ths
        break;
121 f0fc6f8f ths
    case MIPSNET_RX_DATA_COUNT:
122 f0fc6f8f ths
        ret = s->rx_count;
123 f0fc6f8f ths
        break;
124 f0fc6f8f ths
    case MIPSNET_TX_DATA_COUNT:
125 f0fc6f8f ths
        ret = s->tx_count;
126 f0fc6f8f ths
        break;
127 f0fc6f8f ths
    case MIPSNET_INT_CTL:
128 f0fc6f8f ths
        ret = s->intctl;
129 f0fc6f8f ths
        s->intctl &= ~MIPSNET_INTCTL_TESTBIT;
130 f0fc6f8f ths
        break;
131 f0fc6f8f ths
    case MIPSNET_INTERRUPT_INFO:
132 f0fc6f8f ths
        /* XXX: This seems to be a per-VPE interrupt number. */
133 f0fc6f8f ths
        ret = 0;
134 f0fc6f8f ths
        break;
135 f0fc6f8f ths
    case MIPSNET_RX_DATA_BUFFER:
136 f0fc6f8f ths
        if (s->rx_count) {
137 f0fc6f8f ths
            s->rx_count--;
138 f0fc6f8f ths
            ret = s->rx_buffer[s->rx_read++];
139 f0fc6f8f ths
        }
140 f0fc6f8f ths
        break;
141 f0fc6f8f ths
    /* Reads as zero. */
142 f0fc6f8f ths
    case MIPSNET_TX_DATA_BUFFER:
143 f0fc6f8f ths
    default:
144 f0fc6f8f ths
        break;
145 f0fc6f8f ths
    }
146 f0fc6f8f ths
#ifdef DEBUG_MIPSNET_DATA
147 f0fc6f8f ths
    printf("mipsnet: read addr=0x%02x val=0x%02x\n", addr, ret);
148 f0fc6f8f ths
#endif
149 f0fc6f8f ths
    return ret;
150 f0fc6f8f ths
}
151 f0fc6f8f ths
152 f0fc6f8f ths
static void mipsnet_ioport_write(void *opaque, uint32_t addr, uint32_t val)
153 f0fc6f8f ths
{
154 f0fc6f8f ths
    MIPSnetState *s = opaque;
155 f0fc6f8f ths
156 f0fc6f8f ths
    addr &= 0x3f;
157 f0fc6f8f ths
#ifdef DEBUG_MIPSNET_DATA
158 f0fc6f8f ths
    printf("mipsnet: write addr=0x%02x val=0x%02x\n", addr, val);
159 f0fc6f8f ths
#endif
160 f0fc6f8f ths
    switch (addr) {
161 f0fc6f8f ths
    case MIPSNET_TX_DATA_COUNT:
162 f0fc6f8f ths
        s->tx_count = (val <= MAX_ETH_FRAME_SIZE) ? val : 0;
163 f0fc6f8f ths
        s->tx_written = 0;
164 f0fc6f8f ths
        break;
165 f0fc6f8f ths
    case MIPSNET_INT_CTL:
166 f0fc6f8f ths
        if (val & MIPSNET_INTCTL_TXDONE) {
167 f0fc6f8f ths
            s->intctl &= ~MIPSNET_INTCTL_TXDONE;
168 f0fc6f8f ths
        } else if (val & MIPSNET_INTCTL_RXDONE) {
169 f0fc6f8f ths
            s->intctl &= ~MIPSNET_INTCTL_RXDONE;
170 f0fc6f8f ths
        } else if (val & MIPSNET_INTCTL_TESTBIT) {
171 f0fc6f8f ths
            mipsnet_reset(s);
172 f0fc6f8f ths
            s->intctl |= MIPSNET_INTCTL_TESTBIT;
173 f0fc6f8f ths
        } else if (!val) {
174 f0fc6f8f ths
            /* ACK testbit interrupt, flag was cleared on read. */
175 f0fc6f8f ths
        }
176 f0fc6f8f ths
        s->busy = !!s->intctl;
177 f0fc6f8f ths
        mipsnet_update_irq(s);
178 f0fc6f8f ths
        break;
179 f0fc6f8f ths
    case MIPSNET_TX_DATA_BUFFER:
180 f0fc6f8f ths
        s->tx_buffer[s->tx_written++] = val;
181 f0fc6f8f ths
        if (s->tx_written == s->tx_count) {
182 f0fc6f8f ths
            /* Send buffer. */
183 f0fc6f8f ths
#ifdef DEBUG_MIPSNET_SEND
184 f0fc6f8f ths
            printf("mipsnet: sending len=%d\n", s->tx_count);
185 f0fc6f8f ths
#endif
186 f0fc6f8f ths
            qemu_send_packet(s->vc, s->tx_buffer, s->tx_count);
187 f0fc6f8f ths
            s->tx_count = s->tx_written = 0;
188 f0fc6f8f ths
            s->intctl |= MIPSNET_INTCTL_TXDONE;
189 f0fc6f8f ths
            s->busy = 1;
190 f0fc6f8f ths
            mipsnet_update_irq(s);
191 f0fc6f8f ths
        }
192 f0fc6f8f ths
        break;
193 f0fc6f8f ths
    /* Read-only registers */
194 f0fc6f8f ths
    case MIPSNET_DEV_ID:
195 f0fc6f8f ths
    case MIPSNET_BUSY:
196 f0fc6f8f ths
    case MIPSNET_RX_DATA_COUNT:
197 f0fc6f8f ths
    case MIPSNET_INTERRUPT_INFO:
198 f0fc6f8f ths
    case MIPSNET_RX_DATA_BUFFER:
199 f0fc6f8f ths
    default:
200 f0fc6f8f ths
        break;
201 f0fc6f8f ths
    }
202 f0fc6f8f ths
}
203 f0fc6f8f ths
204 f0fc6f8f ths
static void mipsnet_save(QEMUFile *f, void *opaque)
205 f0fc6f8f ths
{
206 f0fc6f8f ths
    MIPSnetState *s = opaque;
207 f0fc6f8f ths
208 f0fc6f8f ths
    qemu_put_be32s(f, &s->busy);
209 f0fc6f8f ths
    qemu_put_be32s(f, &s->rx_count);
210 f0fc6f8f ths
    qemu_put_be32s(f, &s->rx_read);
211 f0fc6f8f ths
    qemu_put_be32s(f, &s->tx_count);
212 f0fc6f8f ths
    qemu_put_be32s(f, &s->tx_written);
213 f0fc6f8f ths
    qemu_put_be32s(f, &s->intctl);
214 f0fc6f8f ths
    qemu_put_buffer(f, s->rx_buffer, MAX_ETH_FRAME_SIZE);
215 f0fc6f8f ths
    qemu_put_buffer(f, s->tx_buffer, MAX_ETH_FRAME_SIZE);
216 f0fc6f8f ths
}
217 f0fc6f8f ths
218 f0fc6f8f ths
static int mipsnet_load(QEMUFile *f, void *opaque, int version_id)
219 f0fc6f8f ths
{
220 f0fc6f8f ths
    MIPSnetState *s = opaque;
221 f0fc6f8f ths
222 f0fc6f8f ths
    if (version_id > 0)
223 f0fc6f8f ths
        return -EINVAL;
224 f0fc6f8f ths
225 f0fc6f8f ths
    qemu_get_be32s(f, &s->busy);
226 f0fc6f8f ths
    qemu_get_be32s(f, &s->rx_count);
227 f0fc6f8f ths
    qemu_get_be32s(f, &s->rx_read);
228 f0fc6f8f ths
    qemu_get_be32s(f, &s->tx_count);
229 f0fc6f8f ths
    qemu_get_be32s(f, &s->tx_written);
230 f0fc6f8f ths
    qemu_get_be32s(f, &s->intctl);
231 f0fc6f8f ths
    qemu_get_buffer(f, s->rx_buffer, MAX_ETH_FRAME_SIZE);
232 f0fc6f8f ths
    qemu_get_buffer(f, s->tx_buffer, MAX_ETH_FRAME_SIZE);
233 f0fc6f8f ths
234 f0fc6f8f ths
    return 0;
235 f0fc6f8f ths
}
236 f0fc6f8f ths
237 f0fc6f8f ths
void mipsnet_init (int base, qemu_irq irq, NICInfo *nd)
238 f0fc6f8f ths
{
239 f0fc6f8f ths
    MIPSnetState *s;
240 f0fc6f8f ths
241 f0fc6f8f ths
    s = qemu_mallocz(sizeof(MIPSnetState));
242 f0fc6f8f ths
    if (!s)
243 f0fc6f8f ths
        return;
244 f0fc6f8f ths
245 f0fc6f8f ths
    register_ioport_write(base, 36, 1, mipsnet_ioport_write, s);
246 f0fc6f8f ths
    register_ioport_read(base, 36, 1, mipsnet_ioport_read, s);
247 f0fc6f8f ths
    register_ioport_write(base, 36, 2, mipsnet_ioport_write, s);
248 f0fc6f8f ths
    register_ioport_read(base, 36, 2, mipsnet_ioport_read, s);
249 f0fc6f8f ths
    register_ioport_write(base, 36, 4, mipsnet_ioport_write, s);
250 f0fc6f8f ths
    register_ioport_read(base, 36, 4, mipsnet_ioport_read, s);
251 f0fc6f8f ths
252 f0fc6f8f ths
    s->irq = irq;
253 f0fc6f8f ths
    s->nd = nd;
254 f0fc6f8f ths
    if (nd && nd->vlan) {
255 f0fc6f8f ths
        s->vc = qemu_new_vlan_client(nd->vlan, mipsnet_receive,
256 f0fc6f8f ths
                                     mipsnet_can_receive, s);
257 f0fc6f8f ths
    } else {
258 f0fc6f8f ths
        s->vc = NULL;
259 f0fc6f8f ths
    }
260 f0fc6f8f ths
261 f0fc6f8f ths
    snprintf(s->vc->info_str, sizeof(s->vc->info_str),
262 f0fc6f8f ths
             "mipsnet macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
263 f0fc6f8f ths
              s->nd->macaddr[0],
264 f0fc6f8f ths
              s->nd->macaddr[1],
265 f0fc6f8f ths
              s->nd->macaddr[2],
266 f0fc6f8f ths
              s->nd->macaddr[3],
267 f0fc6f8f ths
              s->nd->macaddr[4],
268 f0fc6f8f ths
              s->nd->macaddr[5]);
269 f0fc6f8f ths
270 f0fc6f8f ths
    mipsnet_reset(s);
271 f0fc6f8f ths
    register_savevm("mipsnet", 0, 0, mipsnet_save, mipsnet_load, s);
272 f0fc6f8f ths
}