Statistics
| Branch: | Revision:

root / hw / smc91c111.c @ 22ed1d34

History | View | Annotate | Download (20.3 kB)

1 5fafdf24 ths
/*
2 80337b66 bellard
 * SMSC 91C111 Ethernet interface emulation
3 80337b66 bellard
 *
4 80337b66 bellard
 * Copyright (c) 2005 CodeSourcery, LLC.
5 80337b66 bellard
 * Written by Paul Brook
6 80337b66 bellard
 *
7 80337b66 bellard
 * This code is licenced under the GPL
8 80337b66 bellard
 */
9 80337b66 bellard
10 418dcf5b Paul Brook
#include "sysbus.h"
11 87ecb68b pbrook
#include "net.h"
12 87ecb68b pbrook
#include "devices.h"
13 80337b66 bellard
/* For crc32 */
14 80337b66 bellard
#include <zlib.h>
15 80337b66 bellard
16 80337b66 bellard
/* Number of 2k memory pages available.  */
17 80337b66 bellard
#define NUM_PACKETS 4
18 80337b66 bellard
19 80337b66 bellard
typedef struct {
20 418dcf5b Paul Brook
    SysBusDevice busdev;
21 42a4260f Mark McLoughlin
    NICState *nic;
22 50132156 Gerd Hoffmann
    NICConf conf;
23 80337b66 bellard
    uint16_t tcr;
24 80337b66 bellard
    uint16_t rcr;
25 80337b66 bellard
    uint16_t cr;
26 80337b66 bellard
    uint16_t ctr;
27 80337b66 bellard
    uint16_t gpr;
28 80337b66 bellard
    uint16_t ptr;
29 80337b66 bellard
    uint16_t ercv;
30 d537cf6c pbrook
    qemu_irq irq;
31 80337b66 bellard
    int bank;
32 80337b66 bellard
    int packet_num;
33 80337b66 bellard
    int tx_alloc;
34 80337b66 bellard
    /* Bitmask of allocated packets.  */
35 80337b66 bellard
    int allocated;
36 80337b66 bellard
    int tx_fifo_len;
37 80337b66 bellard
    int tx_fifo[NUM_PACKETS];
38 80337b66 bellard
    int rx_fifo_len;
39 80337b66 bellard
    int rx_fifo[NUM_PACKETS];
40 5198cfd9 bellard
    int tx_fifo_done_len;
41 5198cfd9 bellard
    int tx_fifo_done[NUM_PACKETS];
42 80337b66 bellard
    /* Packet buffer memory.  */
43 5198cfd9 bellard
    uint8_t data[NUM_PACKETS][2048];
44 80337b66 bellard
    uint8_t int_level;
45 80337b66 bellard
    uint8_t int_mask;
46 b946a153 aliguori
    int mmio_index;
47 80337b66 bellard
} smc91c111_state;
48 80337b66 bellard
49 80337b66 bellard
#define RCR_SOFT_RST  0x8000
50 80337b66 bellard
#define RCR_STRIP_CRC 0x0200
51 80337b66 bellard
#define RCR_RXEN      0x0100
52 80337b66 bellard
53 80337b66 bellard
#define TCR_EPH_LOOP  0x2000
54 80337b66 bellard
#define TCR_NOCRC     0x0100
55 80337b66 bellard
#define TCR_PAD_EN    0x0080
56 80337b66 bellard
#define TCR_FORCOL    0x0004
57 80337b66 bellard
#define TCR_LOOP      0x0002
58 80337b66 bellard
#define TCR_TXEN      0x0001
59 80337b66 bellard
60 80337b66 bellard
#define INT_MD        0x80
61 80337b66 bellard
#define INT_ERCV      0x40
62 80337b66 bellard
#define INT_EPH       0x20
63 80337b66 bellard
#define INT_RX_OVRN   0x10
64 80337b66 bellard
#define INT_ALLOC     0x08
65 80337b66 bellard
#define INT_TX_EMPTY  0x04
66 80337b66 bellard
#define INT_TX        0x02
67 80337b66 bellard
#define INT_RCV       0x01
68 80337b66 bellard
69 80337b66 bellard
#define CTR_AUTO_RELEASE  0x0800
70 80337b66 bellard
#define CTR_RELOAD        0x0002
71 80337b66 bellard
#define CTR_STORE         0x0001
72 80337b66 bellard
73 80337b66 bellard
#define RS_ALGNERR      0x8000
74 80337b66 bellard
#define RS_BRODCAST     0x4000
75 80337b66 bellard
#define RS_BADCRC       0x2000
76 80337b66 bellard
#define RS_ODDFRAME     0x1000
77 80337b66 bellard
#define RS_TOOLONG      0x0800
78 80337b66 bellard
#define RS_TOOSHORT     0x0400
79 80337b66 bellard
#define RS_MULTICAST    0x0001
80 80337b66 bellard
81 80337b66 bellard
/* Update interrupt status.  */
82 80337b66 bellard
static void smc91c111_update(smc91c111_state *s)
83 80337b66 bellard
{
84 80337b66 bellard
    int level;
85 80337b66 bellard
86 80337b66 bellard
    if (s->tx_fifo_len == 0)
87 80337b66 bellard
        s->int_level |= INT_TX_EMPTY;
88 5198cfd9 bellard
    if (s->tx_fifo_done_len != 0)
89 5198cfd9 bellard
        s->int_level |= INT_TX;
90 80337b66 bellard
    level = (s->int_level & s->int_mask) != 0;
91 d537cf6c pbrook
    qemu_set_irq(s->irq, level);
92 80337b66 bellard
}
93 80337b66 bellard
94 80337b66 bellard
/* Try to allocate a packet.  Returns 0x80 on failure.  */
95 80337b66 bellard
static int smc91c111_allocate_packet(smc91c111_state *s)
96 80337b66 bellard
{
97 80337b66 bellard
    int i;
98 80337b66 bellard
    if (s->allocated == (1 << NUM_PACKETS) - 1) {
99 80337b66 bellard
        return 0x80;
100 80337b66 bellard
    }
101 80337b66 bellard
102 80337b66 bellard
    for (i = 0; i < NUM_PACKETS; i++) {
103 80337b66 bellard
        if ((s->allocated & (1 << i)) == 0)
104 80337b66 bellard
            break;
105 80337b66 bellard
    }
106 80337b66 bellard
    s->allocated |= 1 << i;
107 80337b66 bellard
    return i;
108 80337b66 bellard
}
109 80337b66 bellard
110 80337b66 bellard
111 80337b66 bellard
/* Process a pending TX allocate.  */
112 80337b66 bellard
static void smc91c111_tx_alloc(smc91c111_state *s)
113 80337b66 bellard
{
114 80337b66 bellard
    s->tx_alloc = smc91c111_allocate_packet(s);
115 80337b66 bellard
    if (s->tx_alloc == 0x80)
116 80337b66 bellard
        return;
117 80337b66 bellard
    s->int_level |= INT_ALLOC;
118 80337b66 bellard
    smc91c111_update(s);
119 80337b66 bellard
}
120 80337b66 bellard
121 80337b66 bellard
/* Remove and item from the RX FIFO.  */
122 80337b66 bellard
static void smc91c111_pop_rx_fifo(smc91c111_state *s)
123 80337b66 bellard
{
124 80337b66 bellard
    int i;
125 80337b66 bellard
126 80337b66 bellard
    s->rx_fifo_len--;
127 80337b66 bellard
    if (s->rx_fifo_len) {
128 80337b66 bellard
        for (i = 0; i < s->rx_fifo_len; i++)
129 80337b66 bellard
            s->rx_fifo[i] = s->rx_fifo[i + 1];
130 80337b66 bellard
        s->int_level |= INT_RCV;
131 80337b66 bellard
    } else {
132 80337b66 bellard
        s->int_level &= ~INT_RCV;
133 80337b66 bellard
    }
134 80337b66 bellard
    smc91c111_update(s);
135 80337b66 bellard
}
136 80337b66 bellard
137 5198cfd9 bellard
/* Remove an item from the TX completion FIFO.  */
138 5198cfd9 bellard
static void smc91c111_pop_tx_fifo_done(smc91c111_state *s)
139 5198cfd9 bellard
{
140 5198cfd9 bellard
    int i;
141 5198cfd9 bellard
142 5198cfd9 bellard
    if (s->tx_fifo_done_len == 0)
143 5198cfd9 bellard
        return;
144 5198cfd9 bellard
    s->tx_fifo_done_len--;
145 5198cfd9 bellard
    for (i = 0; i < s->tx_fifo_done_len; i++)
146 5198cfd9 bellard
        s->tx_fifo_done[i] = s->tx_fifo_done[i + 1];
147 5198cfd9 bellard
}
148 5198cfd9 bellard
149 80337b66 bellard
/* Release the memory allocated to a packet.  */
150 80337b66 bellard
static void smc91c111_release_packet(smc91c111_state *s, int packet)
151 80337b66 bellard
{
152 80337b66 bellard
    s->allocated &= ~(1 << packet);
153 80337b66 bellard
    if (s->tx_alloc == 0x80)
154 80337b66 bellard
        smc91c111_tx_alloc(s);
155 80337b66 bellard
}
156 80337b66 bellard
157 80337b66 bellard
/* Flush the TX FIFO.  */
158 80337b66 bellard
static void smc91c111_do_tx(smc91c111_state *s)
159 80337b66 bellard
{
160 80337b66 bellard
    int i;
161 80337b66 bellard
    int len;
162 80337b66 bellard
    int control;
163 80337b66 bellard
    int packetnum;
164 80337b66 bellard
    uint8_t *p;
165 80337b66 bellard
166 80337b66 bellard
    if ((s->tcr & TCR_TXEN) == 0)
167 80337b66 bellard
        return;
168 80337b66 bellard
    if (s->tx_fifo_len == 0)
169 80337b66 bellard
        return;
170 80337b66 bellard
    for (i = 0; i < s->tx_fifo_len; i++) {
171 80337b66 bellard
        packetnum = s->tx_fifo[i];
172 80337b66 bellard
        p = &s->data[packetnum][0];
173 80337b66 bellard
        /* Set status word.  */
174 80337b66 bellard
        *(p++) = 0x01;
175 80337b66 bellard
        *(p++) = 0x40;
176 80337b66 bellard
        len = *(p++);
177 80337b66 bellard
        len |= ((int)*(p++)) << 8;
178 80337b66 bellard
        len -= 6;
179 80337b66 bellard
        control = p[len + 1];
180 80337b66 bellard
        if (control & 0x20)
181 80337b66 bellard
            len++;
182 80337b66 bellard
        /* ??? This overwrites the data following the buffer.
183 80337b66 bellard
           Don't know what real hardware does.  */
184 80337b66 bellard
        if (len < 64 && (s->tcr & TCR_PAD_EN)) {
185 80337b66 bellard
            memset(p + len, 0, 64 - len);
186 80337b66 bellard
            len = 64;
187 80337b66 bellard
        }
188 80337b66 bellard
#if 0
189 22ed1d34 Blue Swirl
        {
190 22ed1d34 Blue Swirl
            int add_crc;
191 22ed1d34 Blue Swirl

192 22ed1d34 Blue Swirl
            /* The card is supposed to append the CRC to the frame.
193 22ed1d34 Blue Swirl
               However none of the other network traffic has the CRC
194 22ed1d34 Blue Swirl
               appended.  Suspect this is low level ethernet detail we
195 22ed1d34 Blue Swirl
               don't need to worry about.  */
196 22ed1d34 Blue Swirl
            add_crc = (control & 0x10) || (s->tcr & TCR_NOCRC) == 0;
197 22ed1d34 Blue Swirl
            if (add_crc) {
198 22ed1d34 Blue Swirl
                uint32_t crc;
199 22ed1d34 Blue Swirl

200 22ed1d34 Blue Swirl
                crc = crc32(~0, p, len);
201 22ed1d34 Blue Swirl
                memcpy(p + len, &crc, 4);
202 22ed1d34 Blue Swirl
                len += 4;
203 22ed1d34 Blue Swirl
            }
204 80337b66 bellard
        }
205 80337b66 bellard
#endif
206 80337b66 bellard
        if (s->ctr & CTR_AUTO_RELEASE)
207 5198cfd9 bellard
            /* Race?  */
208 80337b66 bellard
            smc91c111_release_packet(s, packetnum);
209 5198cfd9 bellard
        else if (s->tx_fifo_done_len < NUM_PACKETS)
210 5198cfd9 bellard
            s->tx_fifo_done[s->tx_fifo_done_len++] = packetnum;
211 42a4260f Mark McLoughlin
        qemu_send_packet(&s->nic->nc, p, len);
212 80337b66 bellard
    }
213 80337b66 bellard
    s->tx_fifo_len = 0;
214 80337b66 bellard
    smc91c111_update(s);
215 80337b66 bellard
}
216 80337b66 bellard
217 80337b66 bellard
/* Add a packet to the TX FIFO.  */
218 80337b66 bellard
static void smc91c111_queue_tx(smc91c111_state *s, int packet)
219 80337b66 bellard
{
220 80337b66 bellard
    if (s->tx_fifo_len == NUM_PACKETS)
221 80337b66 bellard
        return;
222 80337b66 bellard
    s->tx_fifo[s->tx_fifo_len++] = packet;
223 80337b66 bellard
    smc91c111_do_tx(s);
224 80337b66 bellard
}
225 80337b66 bellard
226 80337b66 bellard
static void smc91c111_reset(smc91c111_state *s)
227 80337b66 bellard
{
228 80337b66 bellard
    s->bank = 0;
229 80337b66 bellard
    s->tx_fifo_len = 0;
230 5198cfd9 bellard
    s->tx_fifo_done_len = 0;
231 80337b66 bellard
    s->rx_fifo_len = 0;
232 80337b66 bellard
    s->allocated = 0;
233 80337b66 bellard
    s->packet_num = 0;
234 80337b66 bellard
    s->tx_alloc = 0;
235 80337b66 bellard
    s->tcr = 0;
236 80337b66 bellard
    s->rcr = 0;
237 80337b66 bellard
    s->cr = 0xa0b1;
238 80337b66 bellard
    s->ctr = 0x1210;
239 80337b66 bellard
    s->ptr = 0;
240 80337b66 bellard
    s->ercv = 0x1f;
241 80337b66 bellard
    s->int_level = INT_TX_EMPTY;
242 80337b66 bellard
    s->int_mask = 0;
243 80337b66 bellard
    smc91c111_update(s);
244 80337b66 bellard
}
245 80337b66 bellard
246 80337b66 bellard
#define SET_LOW(name, val) s->name = (s->name & 0xff00) | val
247 80337b66 bellard
#define SET_HIGH(name, val) s->name = (s->name & 0xff) | (val << 8)
248 80337b66 bellard
249 c227f099 Anthony Liguori
static void smc91c111_writeb(void *opaque, target_phys_addr_t offset,
250 80337b66 bellard
                             uint32_t value)
251 80337b66 bellard
{
252 80337b66 bellard
    smc91c111_state *s = (smc91c111_state *)opaque;
253 80337b66 bellard
254 3b4b86aa Lars Munch
    offset = offset & 0xf;
255 80337b66 bellard
    if (offset == 14) {
256 80337b66 bellard
        s->bank = value;
257 80337b66 bellard
        return;
258 80337b66 bellard
    }
259 80337b66 bellard
    if (offset == 15)
260 80337b66 bellard
        return;
261 80337b66 bellard
    switch (s->bank) {
262 80337b66 bellard
    case 0:
263 80337b66 bellard
        switch (offset) {
264 80337b66 bellard
        case 0: /* TCR */
265 80337b66 bellard
            SET_LOW(tcr, value);
266 80337b66 bellard
            return;
267 80337b66 bellard
        case 1:
268 80337b66 bellard
            SET_HIGH(tcr, value);
269 80337b66 bellard
            return;
270 80337b66 bellard
        case 4: /* RCR */
271 80337b66 bellard
            SET_LOW(rcr, value);
272 80337b66 bellard
            return;
273 80337b66 bellard
        case 5:
274 80337b66 bellard
            SET_HIGH(rcr, value);
275 80337b66 bellard
            if (s->rcr & RCR_SOFT_RST)
276 80337b66 bellard
                smc91c111_reset(s);
277 80337b66 bellard
            return;
278 80337b66 bellard
        case 10: case 11: /* RPCR */
279 80337b66 bellard
            /* Ignored */
280 80337b66 bellard
            return;
281 14da5616 Lars Munch
        case 12: case 13: /* Reserved */
282 14da5616 Lars Munch
            return;
283 80337b66 bellard
        }
284 80337b66 bellard
        break;
285 80337b66 bellard
286 80337b66 bellard
    case 1:
287 80337b66 bellard
        switch (offset) {
288 80337b66 bellard
        case 0: /* CONFIG */
289 80337b66 bellard
            SET_LOW(cr, value);
290 80337b66 bellard
            return;
291 80337b66 bellard
        case 1:
292 80337b66 bellard
            SET_HIGH(cr,value);
293 80337b66 bellard
            return;
294 80337b66 bellard
        case 2: case 3: /* BASE */
295 80337b66 bellard
        case 4: case 5: case 6: case 7: case 8: case 9: /* IA */
296 80337b66 bellard
            /* Not implemented.  */
297 80337b66 bellard
            return;
298 80337b66 bellard
        case 10: /* Genral Purpose */
299 80337b66 bellard
            SET_LOW(gpr, value);
300 80337b66 bellard
            return;
301 80337b66 bellard
        case 11:
302 80337b66 bellard
            SET_HIGH(gpr, value);
303 80337b66 bellard
            return;
304 80337b66 bellard
        case 12: /* Control */
305 80337b66 bellard
            if (value & 1)
306 80337b66 bellard
                fprintf(stderr, "smc91c111:EEPROM store not implemented\n");
307 80337b66 bellard
            if (value & 2)
308 80337b66 bellard
                fprintf(stderr, "smc91c111:EEPROM reload not implemented\n");
309 80337b66 bellard
            value &= ~3;
310 80337b66 bellard
            SET_LOW(ctr, value);
311 80337b66 bellard
            return;
312 80337b66 bellard
        case 13:
313 80337b66 bellard
            SET_HIGH(ctr, value);
314 80337b66 bellard
            return;
315 80337b66 bellard
        }
316 80337b66 bellard
        break;
317 80337b66 bellard
318 80337b66 bellard
    case 2:
319 80337b66 bellard
        switch (offset) {
320 80337b66 bellard
        case 0: /* MMU Command */
321 80337b66 bellard
            switch (value >> 5) {
322 80337b66 bellard
            case 0: /* no-op */
323 80337b66 bellard
                break;
324 80337b66 bellard
            case 1: /* Allocate for TX.  */
325 80337b66 bellard
                s->tx_alloc = 0x80;
326 80337b66 bellard
                s->int_level &= ~INT_ALLOC;
327 80337b66 bellard
                smc91c111_update(s);
328 80337b66 bellard
                smc91c111_tx_alloc(s);
329 80337b66 bellard
                break;
330 80337b66 bellard
            case 2: /* Reset MMU.  */
331 80337b66 bellard
                s->allocated = 0;
332 80337b66 bellard
                s->tx_fifo_len = 0;
333 5198cfd9 bellard
                s->tx_fifo_done_len = 0;
334 80337b66 bellard
                s->rx_fifo_len = 0;
335 80337b66 bellard
                s->tx_alloc = 0;
336 80337b66 bellard
                break;
337 80337b66 bellard
            case 3: /* Remove from RX FIFO.  */
338 80337b66 bellard
                smc91c111_pop_rx_fifo(s);
339 80337b66 bellard
                break;
340 80337b66 bellard
            case 4: /* Remove from RX FIFO and release.  */
341 80337b66 bellard
                if (s->rx_fifo_len > 0) {
342 80337b66 bellard
                    smc91c111_release_packet(s, s->rx_fifo[0]);
343 80337b66 bellard
                }
344 80337b66 bellard
                smc91c111_pop_rx_fifo(s);
345 80337b66 bellard
                break;
346 80337b66 bellard
            case 5: /* Release.  */
347 80337b66 bellard
                smc91c111_release_packet(s, s->packet_num);
348 80337b66 bellard
                break;
349 80337b66 bellard
            case 6: /* Add to TX FIFO.  */
350 80337b66 bellard
                smc91c111_queue_tx(s, s->packet_num);
351 80337b66 bellard
                break;
352 80337b66 bellard
            case 7: /* Reset TX FIFO.  */
353 80337b66 bellard
                s->tx_fifo_len = 0;
354 5198cfd9 bellard
                s->tx_fifo_done_len = 0;
355 80337b66 bellard
                break;
356 80337b66 bellard
            }
357 80337b66 bellard
            return;
358 80337b66 bellard
        case 1:
359 80337b66 bellard
            /* Ignore.  */
360 80337b66 bellard
            return;
361 80337b66 bellard
        case 2: /* Packet Number Register */
362 80337b66 bellard
            s->packet_num = value;
363 80337b66 bellard
            return;
364 80337b66 bellard
        case 3: case 4: case 5:
365 80337b66 bellard
            /* Should be readonly, but linux writes to them anyway. Ignore.  */
366 80337b66 bellard
            return;
367 80337b66 bellard
        case 6: /* Pointer */
368 80337b66 bellard
            SET_LOW(ptr, value);
369 80337b66 bellard
            return;
370 80337b66 bellard
        case 7:
371 80337b66 bellard
            SET_HIGH(ptr, value);
372 80337b66 bellard
            return;
373 80337b66 bellard
        case 8: case 9: case 10: case 11: /* Data */
374 80337b66 bellard
            {
375 80337b66 bellard
                int p;
376 80337b66 bellard
                int n;
377 80337b66 bellard
378 80337b66 bellard
                if (s->ptr & 0x8000)
379 80337b66 bellard
                    n = s->rx_fifo[0];
380 80337b66 bellard
                else
381 80337b66 bellard
                    n = s->packet_num;
382 80337b66 bellard
                p = s->ptr & 0x07ff;
383 80337b66 bellard
                if (s->ptr & 0x4000) {
384 80337b66 bellard
                    s->ptr = (s->ptr & 0xf800) | ((s->ptr + 1) & 0x7ff);
385 80337b66 bellard
                } else {
386 80337b66 bellard
                    p += (offset & 3);
387 80337b66 bellard
                }
388 80337b66 bellard
                s->data[n][p] = value;
389 80337b66 bellard
            }
390 80337b66 bellard
            return;
391 80337b66 bellard
        case 12: /* Interrupt ACK.  */
392 80337b66 bellard
            s->int_level &= ~(value & 0xd6);
393 5198cfd9 bellard
            if (value & INT_TX)
394 5198cfd9 bellard
                smc91c111_pop_tx_fifo_done(s);
395 80337b66 bellard
            smc91c111_update(s);
396 80337b66 bellard
            return;
397 80337b66 bellard
        case 13: /* Interrupt mask.  */
398 80337b66 bellard
            s->int_mask = value;
399 80337b66 bellard
            smc91c111_update(s);
400 80337b66 bellard
            return;
401 80337b66 bellard
        }
402 80337b66 bellard
        break;;
403 80337b66 bellard
404 80337b66 bellard
    case 3:
405 80337b66 bellard
        switch (offset) {
406 80337b66 bellard
        case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
407 80337b66 bellard
            /* Multicast table.  */
408 80337b66 bellard
            /* Not implemented.  */
409 80337b66 bellard
            return;
410 80337b66 bellard
        case 8: case 9: /* Management Interface.  */
411 80337b66 bellard
            /* Not implemented.  */
412 80337b66 bellard
            return;
413 80337b66 bellard
        case 12: /* Early receive.  */
414 80337b66 bellard
            s->ercv = value & 0x1f;
415 80337b66 bellard
        case 13:
416 80337b66 bellard
            /* Ignore.  */
417 80337b66 bellard
            return;
418 80337b66 bellard
        }
419 80337b66 bellard
        break;
420 80337b66 bellard
    }
421 2ac71179 Paul Brook
    hw_error("smc91c111_write: Bad reg %d:%x\n", s->bank, (int)offset);
422 80337b66 bellard
}
423 80337b66 bellard
424 c227f099 Anthony Liguori
static uint32_t smc91c111_readb(void *opaque, target_phys_addr_t offset)
425 80337b66 bellard
{
426 80337b66 bellard
    smc91c111_state *s = (smc91c111_state *)opaque;
427 80337b66 bellard
428 3b4b86aa Lars Munch
    offset = offset & 0xf;
429 80337b66 bellard
    if (offset == 14) {
430 80337b66 bellard
        return s->bank;
431 80337b66 bellard
    }
432 80337b66 bellard
    if (offset == 15)
433 80337b66 bellard
        return 0x33;
434 80337b66 bellard
    switch (s->bank) {
435 80337b66 bellard
    case 0:
436 80337b66 bellard
        switch (offset) {
437 80337b66 bellard
        case 0: /* TCR */
438 80337b66 bellard
            return s->tcr & 0xff;
439 80337b66 bellard
        case 1:
440 80337b66 bellard
            return s->tcr >> 8;
441 80337b66 bellard
        case 2: /* EPH Status */
442 80337b66 bellard
            return 0;
443 80337b66 bellard
        case 3:
444 80337b66 bellard
            return 0x40;
445 80337b66 bellard
        case 4: /* RCR */
446 80337b66 bellard
            return s->rcr & 0xff;
447 80337b66 bellard
        case 5:
448 80337b66 bellard
            return s->rcr >> 8;
449 80337b66 bellard
        case 6: /* Counter */
450 80337b66 bellard
        case 7:
451 80337b66 bellard
            /* Not implemented.  */
452 80337b66 bellard
            return 0;
453 687fa640 ths
        case 8: /* Memory size.  */
454 687fa640 ths
            return NUM_PACKETS;
455 687fa640 ths
        case 9: /* Free memory available.  */
456 80337b66 bellard
            {
457 80337b66 bellard
                int i;
458 80337b66 bellard
                int n;
459 80337b66 bellard
                n = 0;
460 80337b66 bellard
                for (i = 0; i < NUM_PACKETS; i++) {
461 80337b66 bellard
                    if (s->allocated & (1 << i))
462 80337b66 bellard
                        n++;
463 80337b66 bellard
                }
464 80337b66 bellard
                return n;
465 80337b66 bellard
            }
466 80337b66 bellard
        case 10: case 11: /* RPCR */
467 80337b66 bellard
            /* Not implemented.  */
468 80337b66 bellard
            return 0;
469 14da5616 Lars Munch
        case 12: case 13: /* Reserved */
470 14da5616 Lars Munch
            return 0;
471 80337b66 bellard
        }
472 80337b66 bellard
        break;
473 80337b66 bellard
474 80337b66 bellard
    case 1:
475 80337b66 bellard
        switch (offset) {
476 80337b66 bellard
        case 0: /* CONFIG */
477 80337b66 bellard
            return s->cr & 0xff;
478 80337b66 bellard
        case 1:
479 80337b66 bellard
            return s->cr >> 8;
480 80337b66 bellard
        case 2: case 3: /* BASE */
481 80337b66 bellard
            /* Not implemented.  */
482 80337b66 bellard
            return 0;
483 80337b66 bellard
        case 4: case 5: case 6: case 7: case 8: case 9: /* IA */
484 50132156 Gerd Hoffmann
            return s->conf.macaddr.a[offset - 4];
485 80337b66 bellard
        case 10: /* General Purpose */
486 80337b66 bellard
            return s->gpr & 0xff;
487 80337b66 bellard
        case 11:
488 80337b66 bellard
            return s->gpr >> 8;
489 80337b66 bellard
        case 12: /* Control */
490 80337b66 bellard
            return s->ctr & 0xff;
491 80337b66 bellard
        case 13:
492 80337b66 bellard
            return s->ctr >> 8;
493 80337b66 bellard
        }
494 80337b66 bellard
        break;
495 80337b66 bellard
496 80337b66 bellard
    case 2:
497 80337b66 bellard
        switch (offset) {
498 80337b66 bellard
        case 0: case 1: /* MMUCR Busy bit.  */
499 80337b66 bellard
            return 0;
500 80337b66 bellard
        case 2: /* Packet Number.  */
501 80337b66 bellard
            return s->packet_num;
502 80337b66 bellard
        case 3: /* Allocation Result.  */
503 80337b66 bellard
            return s->tx_alloc;
504 80337b66 bellard
        case 4: /* TX FIFO */
505 5198cfd9 bellard
            if (s->tx_fifo_done_len == 0)
506 80337b66 bellard
                return 0x80;
507 80337b66 bellard
            else
508 5198cfd9 bellard
                return s->tx_fifo_done[0];
509 80337b66 bellard
        case 5: /* RX FIFO */
510 80337b66 bellard
            if (s->rx_fifo_len == 0)
511 80337b66 bellard
                return 0x80;
512 80337b66 bellard
            else
513 80337b66 bellard
                return s->rx_fifo[0];
514 80337b66 bellard
        case 6: /* Pointer */
515 80337b66 bellard
            return s->ptr & 0xff;
516 80337b66 bellard
        case 7:
517 80337b66 bellard
            return (s->ptr >> 8) & 0xf7;
518 80337b66 bellard
        case 8: case 9: case 10: case 11: /* Data */
519 80337b66 bellard
            {
520 80337b66 bellard
                int p;
521 80337b66 bellard
                int n;
522 80337b66 bellard
523 80337b66 bellard
                if (s->ptr & 0x8000)
524 80337b66 bellard
                    n = s->rx_fifo[0];
525 80337b66 bellard
                else
526 80337b66 bellard
                    n = s->packet_num;
527 80337b66 bellard
                p = s->ptr & 0x07ff;
528 80337b66 bellard
                if (s->ptr & 0x4000) {
529 80337b66 bellard
                    s->ptr = (s->ptr & 0xf800) | ((s->ptr + 1) & 0x07ff);
530 80337b66 bellard
                } else {
531 80337b66 bellard
                    p += (offset & 3);
532 80337b66 bellard
                }
533 80337b66 bellard
                return s->data[n][p];
534 80337b66 bellard
            }
535 80337b66 bellard
        case 12: /* Interrupt status.  */
536 80337b66 bellard
            return s->int_level;
537 80337b66 bellard
        case 13: /* Interrupt mask.  */
538 80337b66 bellard
            return s->int_mask;
539 80337b66 bellard
        }
540 80337b66 bellard
        break;
541 80337b66 bellard
542 80337b66 bellard
    case 3:
543 80337b66 bellard
        switch (offset) {
544 80337b66 bellard
        case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
545 80337b66 bellard
            /* Multicast table.  */
546 80337b66 bellard
            /* Not implemented.  */
547 80337b66 bellard
            return 0;
548 80337b66 bellard
        case 8: /* Management Interface.  */
549 80337b66 bellard
            /* Not implemented.  */
550 80337b66 bellard
            return 0x30;
551 80337b66 bellard
        case 9:
552 80337b66 bellard
            return 0x33;
553 80337b66 bellard
        case 10: /* Revision.  */
554 80337b66 bellard
            return 0x91;
555 80337b66 bellard
        case 11:
556 80337b66 bellard
            return 0x33;
557 80337b66 bellard
        case 12:
558 80337b66 bellard
            return s->ercv;
559 80337b66 bellard
        case 13:
560 80337b66 bellard
            return 0;
561 80337b66 bellard
        }
562 80337b66 bellard
        break;
563 80337b66 bellard
    }
564 2ac71179 Paul Brook
    hw_error("smc91c111_read: Bad reg %d:%x\n", s->bank, (int)offset);
565 80337b66 bellard
    return 0;
566 80337b66 bellard
}
567 80337b66 bellard
568 c227f099 Anthony Liguori
static void smc91c111_writew(void *opaque, target_phys_addr_t offset,
569 80337b66 bellard
                             uint32_t value)
570 80337b66 bellard
{
571 80337b66 bellard
    smc91c111_writeb(opaque, offset, value & 0xff);
572 80337b66 bellard
    smc91c111_writeb(opaque, offset + 1, value >> 8);
573 80337b66 bellard
}
574 80337b66 bellard
575 c227f099 Anthony Liguori
static void smc91c111_writel(void *opaque, target_phys_addr_t offset,
576 80337b66 bellard
                             uint32_t value)
577 80337b66 bellard
{
578 80337b66 bellard
    /* 32-bit writes to offset 0xc only actually write to the bank select
579 80337b66 bellard
       register (offset 0xe)  */
580 8da3ff18 pbrook
    if (offset != 0xc)
581 80337b66 bellard
        smc91c111_writew(opaque, offset, value & 0xffff);
582 80337b66 bellard
    smc91c111_writew(opaque, offset + 2, value >> 16);
583 80337b66 bellard
}
584 80337b66 bellard
585 c227f099 Anthony Liguori
static uint32_t smc91c111_readw(void *opaque, target_phys_addr_t offset)
586 80337b66 bellard
{
587 80337b66 bellard
    uint32_t val;
588 80337b66 bellard
    val = smc91c111_readb(opaque, offset);
589 80337b66 bellard
    val |= smc91c111_readb(opaque, offset + 1) << 8;
590 80337b66 bellard
    return val;
591 80337b66 bellard
}
592 80337b66 bellard
593 c227f099 Anthony Liguori
static uint32_t smc91c111_readl(void *opaque, target_phys_addr_t offset)
594 80337b66 bellard
{
595 80337b66 bellard
    uint32_t val;
596 80337b66 bellard
    val = smc91c111_readw(opaque, offset);
597 80337b66 bellard
    val |= smc91c111_readw(opaque, offset + 2) << 16;
598 80337b66 bellard
    return val;
599 80337b66 bellard
}
600 80337b66 bellard
601 42a4260f Mark McLoughlin
static int smc91c111_can_receive(VLANClientState *nc)
602 d861b05e pbrook
{
603 42a4260f Mark McLoughlin
    smc91c111_state *s = DO_UPCAST(NICState, nc, nc)->opaque;
604 d861b05e pbrook
605 d861b05e pbrook
    if ((s->rcr & RCR_RXEN) == 0 || (s->rcr & RCR_SOFT_RST))
606 d861b05e pbrook
        return 1;
607 d861b05e pbrook
    if (s->allocated == (1 << NUM_PACKETS) - 1)
608 d861b05e pbrook
        return 0;
609 d861b05e pbrook
    return 1;
610 d861b05e pbrook
}
611 d861b05e pbrook
612 42a4260f Mark McLoughlin
static ssize_t smc91c111_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
613 80337b66 bellard
{
614 42a4260f Mark McLoughlin
    smc91c111_state *s = DO_UPCAST(NICState, nc, nc)->opaque;
615 80337b66 bellard
    int status;
616 80337b66 bellard
    int packetsize;
617 80337b66 bellard
    uint32_t crc;
618 80337b66 bellard
    int packetnum;
619 80337b66 bellard
    uint8_t *p;
620 80337b66 bellard
621 80337b66 bellard
    if ((s->rcr & RCR_RXEN) == 0 || (s->rcr & RCR_SOFT_RST))
622 4f1c942b Mark McLoughlin
        return -1;
623 9f083493 ths
    /* Short packets are padded with zeros.  Receiving a packet
624 80337b66 bellard
       < 64 bytes long is considered an error condition.  */
625 80337b66 bellard
    if (size < 64)
626 80337b66 bellard
        packetsize = 64;
627 80337b66 bellard
    else
628 80337b66 bellard
        packetsize = (size & ~1);
629 80337b66 bellard
    packetsize += 6;
630 80337b66 bellard
    crc = (s->rcr & RCR_STRIP_CRC) == 0;
631 80337b66 bellard
    if (crc)
632 80337b66 bellard
        packetsize += 4;
633 80337b66 bellard
    /* TODO: Flag overrun and receive errors.  */
634 80337b66 bellard
    if (packetsize > 2048)
635 4f1c942b Mark McLoughlin
        return -1;
636 80337b66 bellard
    packetnum = smc91c111_allocate_packet(s);
637 80337b66 bellard
    if (packetnum == 0x80)
638 4f1c942b Mark McLoughlin
        return -1;
639 80337b66 bellard
    s->rx_fifo[s->rx_fifo_len++] = packetnum;
640 80337b66 bellard
641 80337b66 bellard
    p = &s->data[packetnum][0];
642 80337b66 bellard
    /* ??? Multicast packets?  */
643 80337b66 bellard
    status = 0;
644 80337b66 bellard
    if (size > 1518)
645 80337b66 bellard
        status |= RS_TOOLONG;
646 80337b66 bellard
    if (size & 1)
647 80337b66 bellard
        status |= RS_ODDFRAME;
648 80337b66 bellard
    *(p++) = status & 0xff;
649 80337b66 bellard
    *(p++) = status >> 8;
650 80337b66 bellard
    *(p++) = packetsize & 0xff;
651 80337b66 bellard
    *(p++) = packetsize >> 8;
652 80337b66 bellard
    memcpy(p, buf, size & ~1);
653 80337b66 bellard
    p += (size & ~1);
654 80337b66 bellard
    /* Pad short packets.  */
655 80337b66 bellard
    if (size < 64) {
656 80337b66 bellard
        int pad;
657 3b46e624 ths
658 80337b66 bellard
        if (size & 1)
659 80337b66 bellard
            *(p++) = buf[size - 1];
660 80337b66 bellard
        pad = 64 - size;
661 80337b66 bellard
        memset(p, 0, pad);
662 80337b66 bellard
        p += pad;
663 80337b66 bellard
        size = 64;
664 80337b66 bellard
    }
665 80337b66 bellard
    /* It's not clear if the CRC should go before or after the last byte in
666 80337b66 bellard
       odd sized packets.  Linux disables the CRC, so that's no help.
667 80337b66 bellard
       The pictures in the documentation show the CRC aligned on a 16-bit
668 80337b66 bellard
       boundary before the last odd byte, so that's what we do.  */
669 80337b66 bellard
    if (crc) {
670 80337b66 bellard
        crc = crc32(~0, buf, size);
671 80337b66 bellard
        *(p++) = crc & 0xff; crc >>= 8;
672 80337b66 bellard
        *(p++) = crc & 0xff; crc >>= 8;
673 80337b66 bellard
        *(p++) = crc & 0xff; crc >>= 8;
674 22ed1d34 Blue Swirl
        *(p++) = crc & 0xff;
675 80337b66 bellard
    }
676 80337b66 bellard
    if (size & 1) {
677 80337b66 bellard
        *(p++) = buf[size - 1];
678 22ed1d34 Blue Swirl
        *p = 0x60;
679 80337b66 bellard
    } else {
680 80337b66 bellard
        *(p++) = 0;
681 22ed1d34 Blue Swirl
        *p = 0x40;
682 80337b66 bellard
    }
683 80337b66 bellard
    /* TODO: Raise early RX interrupt?  */
684 80337b66 bellard
    s->int_level |= INT_RCV;
685 80337b66 bellard
    smc91c111_update(s);
686 4f1c942b Mark McLoughlin
687 4f1c942b Mark McLoughlin
    return size;
688 80337b66 bellard
}
689 80337b66 bellard
690 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const smc91c111_readfn[] = {
691 80337b66 bellard
    smc91c111_readb,
692 80337b66 bellard
    smc91c111_readw,
693 80337b66 bellard
    smc91c111_readl
694 80337b66 bellard
};
695 80337b66 bellard
696 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const smc91c111_writefn[] = {
697 80337b66 bellard
    smc91c111_writeb,
698 80337b66 bellard
    smc91c111_writew,
699 80337b66 bellard
    smc91c111_writel
700 80337b66 bellard
};
701 80337b66 bellard
702 42a4260f Mark McLoughlin
static void smc91c111_cleanup(VLANClientState *nc)
703 b946a153 aliguori
{
704 42a4260f Mark McLoughlin
    smc91c111_state *s = DO_UPCAST(NICState, nc, nc)->opaque;
705 b946a153 aliguori
706 42a4260f Mark McLoughlin
    s->nic = NULL;
707 b946a153 aliguori
}
708 b946a153 aliguori
709 42a4260f Mark McLoughlin
static NetClientInfo net_smc91c111_info = {
710 42a4260f Mark McLoughlin
    .type = NET_CLIENT_TYPE_NIC,
711 42a4260f Mark McLoughlin
    .size = sizeof(NICState),
712 42a4260f Mark McLoughlin
    .can_receive = smc91c111_can_receive,
713 42a4260f Mark McLoughlin
    .receive = smc91c111_receive,
714 42a4260f Mark McLoughlin
    .cleanup = smc91c111_cleanup,
715 42a4260f Mark McLoughlin
};
716 42a4260f Mark McLoughlin
717 81a322d4 Gerd Hoffmann
static int smc91c111_init1(SysBusDevice *dev)
718 80337b66 bellard
{
719 418dcf5b Paul Brook
    smc91c111_state *s = FROM_SYSBUS(smc91c111_state, dev);
720 0ae18cee aliguori
721 1eed09cb Avi Kivity
    s->mmio_index = cpu_register_io_memory(smc91c111_readfn,
722 b946a153 aliguori
                                           smc91c111_writefn, s);
723 418dcf5b Paul Brook
    sysbus_init_mmio(dev, 16, s->mmio_index);
724 418dcf5b Paul Brook
    sysbus_init_irq(dev, &s->irq);
725 50132156 Gerd Hoffmann
    qemu_macaddr_default_if_unset(&s->conf.macaddr);
726 80337b66 bellard
727 80337b66 bellard
    smc91c111_reset(s);
728 80337b66 bellard
729 42a4260f Mark McLoughlin
    s->nic = qemu_new_nic(&net_smc91c111_info, &s->conf,
730 42a4260f Mark McLoughlin
                          dev->qdev.info->name, dev->qdev.id, s);
731 42a4260f Mark McLoughlin
    qemu_format_nic_info_str(&s->nic->nc, s->conf.macaddr.a);
732 80337b66 bellard
    /* ??? Save/restore.  */
733 81a322d4 Gerd Hoffmann
    return 0;
734 80337b66 bellard
}
735 418dcf5b Paul Brook
736 50132156 Gerd Hoffmann
static SysBusDeviceInfo smc91c111_info = {
737 50132156 Gerd Hoffmann
    .init = smc91c111_init1,
738 50132156 Gerd Hoffmann
    .qdev.name  = "smc91c111",
739 50132156 Gerd Hoffmann
    .qdev.size  = sizeof(smc91c111_state),
740 50132156 Gerd Hoffmann
    .qdev.props = (Property[]) {
741 50132156 Gerd Hoffmann
        DEFINE_NIC_PROPERTIES(smc91c111_state, conf),
742 50132156 Gerd Hoffmann
        DEFINE_PROP_END_OF_LIST(),
743 50132156 Gerd Hoffmann
    }
744 50132156 Gerd Hoffmann
};
745 50132156 Gerd Hoffmann
746 418dcf5b Paul Brook
static void smc91c111_register_devices(void)
747 418dcf5b Paul Brook
{
748 50132156 Gerd Hoffmann
    sysbus_register_withprop(&smc91c111_info);
749 418dcf5b Paul Brook
}
750 418dcf5b Paul Brook
751 418dcf5b Paul Brook
/* Legacy helper function.  Should go away when machine config files are
752 418dcf5b Paul Brook
   implemented.  */
753 418dcf5b Paul Brook
void smc91c111_init(NICInfo *nd, uint32_t base, qemu_irq irq)
754 418dcf5b Paul Brook
{
755 418dcf5b Paul Brook
    DeviceState *dev;
756 418dcf5b Paul Brook
    SysBusDevice *s;
757 418dcf5b Paul Brook
758 418dcf5b Paul Brook
    qemu_check_nic_model(nd, "smc91c111");
759 418dcf5b Paul Brook
    dev = qdev_create(NULL, "smc91c111");
760 50132156 Gerd Hoffmann
    qdev_set_nic_properties(dev, nd);
761 e23a1b33 Markus Armbruster
    qdev_init_nofail(dev);
762 418dcf5b Paul Brook
    s = sysbus_from_qdev(dev);
763 418dcf5b Paul Brook
    sysbus_mmio_map(s, 0, base);
764 418dcf5b Paul Brook
    sysbus_connect_irq(s, 0, irq);
765 418dcf5b Paul Brook
}
766 418dcf5b Paul Brook
767 418dcf5b Paul Brook
device_init(smc91c111_register_devices)