Statistics
| Branch: | Revision:

root / hw / etraxfs_eth.c @ f3e3aa8c

History | View | Annotate | Download (13 kB)

1 a3ea5df5 edgar_igl
/*
2 a3ea5df5 edgar_igl
 * QEMU ETRAX Ethernet Controller.
3 a3ea5df5 edgar_igl
 *
4 a3ea5df5 edgar_igl
 * Copyright (c) 2008 Edgar E. Iglesias, Axis Communications AB.
5 a3ea5df5 edgar_igl
 *
6 a3ea5df5 edgar_igl
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 a3ea5df5 edgar_igl
 * of this software and associated documentation files (the "Software"), to deal
8 a3ea5df5 edgar_igl
 * in the Software without restriction, including without limitation the rights
9 a3ea5df5 edgar_igl
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 a3ea5df5 edgar_igl
 * copies of the Software, and to permit persons to whom the Software is
11 a3ea5df5 edgar_igl
 * furnished to do so, subject to the following conditions:
12 a3ea5df5 edgar_igl
 *
13 a3ea5df5 edgar_igl
 * The above copyright notice and this permission notice shall be included in
14 a3ea5df5 edgar_igl
 * all copies or substantial portions of the Software.
15 a3ea5df5 edgar_igl
 *
16 a3ea5df5 edgar_igl
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 a3ea5df5 edgar_igl
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 a3ea5df5 edgar_igl
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 a3ea5df5 edgar_igl
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 a3ea5df5 edgar_igl
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 a3ea5df5 edgar_igl
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 a3ea5df5 edgar_igl
 * THE SOFTWARE.
23 a3ea5df5 edgar_igl
 */
24 a3ea5df5 edgar_igl
25 a3ea5df5 edgar_igl
#include <stdio.h>
26 a3ea5df5 edgar_igl
#include "hw.h"
27 a3ea5df5 edgar_igl
#include "net.h"
28 cc53adbc edgar_igl
#include "etraxfs.h"
29 a3ea5df5 edgar_igl
30 a3ea5df5 edgar_igl
#define D(x)
31 a3ea5df5 edgar_igl
32 c6488268 edgar_igl
/* Advertisement control register. */
33 c6488268 edgar_igl
#define ADVERTISE_10HALF        0x0020  /* Try for 10mbps half-duplex  */
34 c6488268 edgar_igl
#define ADVERTISE_10FULL        0x0040  /* Try for 10mbps full-duplex  */
35 c6488268 edgar_igl
#define ADVERTISE_100HALF       0x0080  /* Try for 100mbps half-duplex */
36 c6488268 edgar_igl
#define ADVERTISE_100FULL       0x0100  /* Try for 100mbps full-duplex */
37 c6488268 edgar_igl
38 2e56350e edgar_igl
/* 
39 2e56350e edgar_igl
 * The MDIO extensions in the TDK PHY model were reversed engineered from the 
40 2e56350e edgar_igl
 * linux driver (PHYID and Diagnostics reg).
41 2e56350e edgar_igl
 * TODO: Add friendly names for the register nums.
42 2e56350e edgar_igl
 */
43 a3ea5df5 edgar_igl
struct qemu_phy
44 a3ea5df5 edgar_igl
{
45 a3ea5df5 edgar_igl
        uint32_t regs[32];
46 a3ea5df5 edgar_igl
47 94410b78 edgar_igl
        int link;
48 94410b78 edgar_igl
49 a3ea5df5 edgar_igl
        unsigned int (*read)(struct qemu_phy *phy, unsigned int req);
50 2e56350e edgar_igl
        void (*write)(struct qemu_phy *phy, unsigned int req, 
51 2e56350e edgar_igl
                      unsigned int data);
52 a3ea5df5 edgar_igl
};
53 a3ea5df5 edgar_igl
54 a3ea5df5 edgar_igl
static unsigned int tdk_read(struct qemu_phy *phy, unsigned int req)
55 a3ea5df5 edgar_igl
{
56 a3ea5df5 edgar_igl
        int regnum;
57 a3ea5df5 edgar_igl
        unsigned r = 0;
58 a3ea5df5 edgar_igl
59 a3ea5df5 edgar_igl
        regnum = req & 0x1f;
60 a3ea5df5 edgar_igl
61 a3ea5df5 edgar_igl
        switch (regnum) {
62 a3ea5df5 edgar_igl
                case 1:
63 94410b78 edgar_igl
                        if (!phy->link)
64 94410b78 edgar_igl
                                break;
65 f6953f13 edgar_igl
                        /* MR1.         */
66 a3ea5df5 edgar_igl
                        /* Speeds and modes.  */
67 a3ea5df5 edgar_igl
                        r |= (1 << 13) | (1 << 14);
68 a3ea5df5 edgar_igl
                        r |= (1 << 11) | (1 << 12);
69 a3ea5df5 edgar_igl
                        r |= (1 << 5); /* Autoneg complete.  */
70 f6953f13 edgar_igl
                        r |= (1 << 3); /* Autoneg able.         */
71 94410b78 edgar_igl
                        r |= (1 << 2); /* link.         */
72 a3ea5df5 edgar_igl
                        break;
73 2e56350e edgar_igl
                case 5:
74 2e56350e edgar_igl
                        /* Link partner ability.
75 2e56350e edgar_igl
                           We are kind; always agree with whatever best mode
76 2e56350e edgar_igl
                           the guest advertises.  */
77 2e56350e edgar_igl
                        r = 1 << 14; /* Success.  */
78 2e56350e edgar_igl
                        /* Copy advertised modes.  */
79 2e56350e edgar_igl
                        r |= phy->regs[4] & (15 << 5);
80 2e56350e edgar_igl
                        /* Autoneg support.  */
81 2e56350e edgar_igl
                        r |= 1;
82 2e56350e edgar_igl
                        break;
83 2e56350e edgar_igl
                case 18:
84 2e56350e edgar_igl
                {
85 2e56350e edgar_igl
                        /* Diagnostics reg.  */
86 2e56350e edgar_igl
                        int duplex = 0;
87 2e56350e edgar_igl
                        int speed_100 = 0;
88 2e56350e edgar_igl
89 94410b78 edgar_igl
                        if (!phy->link)
90 94410b78 edgar_igl
                                break;
91 94410b78 edgar_igl
92 2e56350e edgar_igl
                        /* Are we advertising 100 half or 100 duplex ? */
93 c6488268 edgar_igl
                        speed_100 = !!(phy->regs[4] & ADVERTISE_100HALF);
94 c6488268 edgar_igl
                        speed_100 |= !!(phy->regs[4] & ADVERTISE_100FULL);
95 c6488268 edgar_igl
96 2e56350e edgar_igl
                        /* Are we advertising 10 duplex or 100 duplex ? */
97 c6488268 edgar_igl
                        duplex = !!(phy->regs[4] & ADVERTISE_100FULL);
98 c6488268 edgar_igl
                        duplex |= !!(phy->regs[4] & ADVERTISE_10FULL);
99 2e56350e edgar_igl
                        r = (speed_100 << 10) | (duplex << 11);
100 2e56350e edgar_igl
                }
101 2e56350e edgar_igl
                break;
102 2e56350e edgar_igl
103 a3ea5df5 edgar_igl
                default:
104 a3ea5df5 edgar_igl
                        r = phy->regs[regnum];
105 a3ea5df5 edgar_igl
                        break;
106 a3ea5df5 edgar_igl
        }
107 2e56350e edgar_igl
        D(printf("\n%s %x = reg[%d]\n", __func__, r, regnum));
108 a3ea5df5 edgar_igl
        return r;
109 a3ea5df5 edgar_igl
}
110 a3ea5df5 edgar_igl
111 a3ea5df5 edgar_igl
static void 
112 a3ea5df5 edgar_igl
tdk_write(struct qemu_phy *phy, unsigned int req, unsigned int data)
113 a3ea5df5 edgar_igl
{
114 a3ea5df5 edgar_igl
        int regnum;
115 a3ea5df5 edgar_igl
116 a3ea5df5 edgar_igl
        regnum = req & 0x1f;
117 a3ea5df5 edgar_igl
        D(printf("%s reg[%d] = %x\n", __func__, regnum, data));
118 a3ea5df5 edgar_igl
        switch (regnum) {
119 a3ea5df5 edgar_igl
                default:
120 a3ea5df5 edgar_igl
                        phy->regs[regnum] = data;
121 a3ea5df5 edgar_igl
                        break;
122 a3ea5df5 edgar_igl
        }
123 a3ea5df5 edgar_igl
}
124 a3ea5df5 edgar_igl
125 a3ea5df5 edgar_igl
static void 
126 a3ea5df5 edgar_igl
tdk_init(struct qemu_phy *phy)
127 a3ea5df5 edgar_igl
{
128 2e56350e edgar_igl
        phy->regs[0] = 0x3100;
129 2e56350e edgar_igl
        /* PHY Id.  */
130 2e56350e edgar_igl
        phy->regs[2] = 0x0300;
131 2e56350e edgar_igl
        phy->regs[3] = 0xe400;
132 2e56350e edgar_igl
        /* Autonegotiation advertisement reg.  */
133 2e56350e edgar_igl
        phy->regs[4] = 0x01E1;
134 94410b78 edgar_igl
        phy->link = 1;
135 2e56350e edgar_igl
136 a3ea5df5 edgar_igl
        phy->read = tdk_read;
137 a3ea5df5 edgar_igl
        phy->write = tdk_write;
138 a3ea5df5 edgar_igl
}
139 a3ea5df5 edgar_igl
140 a3ea5df5 edgar_igl
struct qemu_mdio
141 a3ea5df5 edgar_igl
{
142 f6953f13 edgar_igl
        /* bus.         */
143 a3ea5df5 edgar_igl
        int mdc;
144 a3ea5df5 edgar_igl
        int mdio;
145 a3ea5df5 edgar_igl
146 a3ea5df5 edgar_igl
        /* decoder.  */
147 a3ea5df5 edgar_igl
        enum {
148 a3ea5df5 edgar_igl
                PREAMBLE,
149 a3ea5df5 edgar_igl
                SOF,
150 a3ea5df5 edgar_igl
                OPC,
151 a3ea5df5 edgar_igl
                ADDR,
152 a3ea5df5 edgar_igl
                REQ,
153 a3ea5df5 edgar_igl
                TURNAROUND,
154 a3ea5df5 edgar_igl
                DATA
155 a3ea5df5 edgar_igl
        } state;
156 a3ea5df5 edgar_igl
        unsigned int drive;
157 a3ea5df5 edgar_igl
158 a3ea5df5 edgar_igl
        unsigned int cnt;
159 a3ea5df5 edgar_igl
        unsigned int addr;
160 a3ea5df5 edgar_igl
        unsigned int opc;
161 a3ea5df5 edgar_igl
        unsigned int req;
162 a3ea5df5 edgar_igl
        unsigned int data;
163 a3ea5df5 edgar_igl
164 a3ea5df5 edgar_igl
        struct qemu_phy *devs[32];
165 a3ea5df5 edgar_igl
};
166 a3ea5df5 edgar_igl
167 a3ea5df5 edgar_igl
static void 
168 a3ea5df5 edgar_igl
mdio_attach(struct qemu_mdio *bus, struct qemu_phy *phy, unsigned int addr)
169 a3ea5df5 edgar_igl
{
170 a3ea5df5 edgar_igl
        bus->devs[addr & 0x1f] = phy;
171 a3ea5df5 edgar_igl
}
172 a3ea5df5 edgar_igl
173 d297f464 edgar_igl
#ifdef USE_THIS_DEAD_CODE
174 a3ea5df5 edgar_igl
static void 
175 a3ea5df5 edgar_igl
mdio_detach(struct qemu_mdio *bus, struct qemu_phy *phy, unsigned int addr)
176 a3ea5df5 edgar_igl
{
177 a3ea5df5 edgar_igl
        bus->devs[addr & 0x1f] = NULL;        
178 a3ea5df5 edgar_igl
}
179 d297f464 edgar_igl
#endif
180 a3ea5df5 edgar_igl
181 a3ea5df5 edgar_igl
static void mdio_read_req(struct qemu_mdio *bus)
182 a3ea5df5 edgar_igl
{
183 a3ea5df5 edgar_igl
        struct qemu_phy *phy;
184 a3ea5df5 edgar_igl
185 a3ea5df5 edgar_igl
        phy = bus->devs[bus->addr];
186 a3ea5df5 edgar_igl
        if (phy && phy->read)
187 a3ea5df5 edgar_igl
                bus->data = phy->read(phy, bus->req);
188 a3ea5df5 edgar_igl
        else 
189 a3ea5df5 edgar_igl
                bus->data = 0xffff;
190 a3ea5df5 edgar_igl
}
191 a3ea5df5 edgar_igl
192 a3ea5df5 edgar_igl
static void mdio_write_req(struct qemu_mdio *bus)
193 a3ea5df5 edgar_igl
{
194 a3ea5df5 edgar_igl
        struct qemu_phy *phy;
195 a3ea5df5 edgar_igl
196 a3ea5df5 edgar_igl
        phy = bus->devs[bus->addr];
197 a3ea5df5 edgar_igl
        if (phy && phy->write)
198 a3ea5df5 edgar_igl
                phy->write(phy, bus->req, bus->data);
199 a3ea5df5 edgar_igl
}
200 a3ea5df5 edgar_igl
201 a3ea5df5 edgar_igl
static void mdio_cycle(struct qemu_mdio *bus)
202 a3ea5df5 edgar_igl
{
203 a3ea5df5 edgar_igl
        bus->cnt++;
204 a3ea5df5 edgar_igl
205 a3ea5df5 edgar_igl
        D(printf("mdc=%d mdio=%d state=%d cnt=%d drv=%d\n",
206 a3ea5df5 edgar_igl
                bus->mdc, bus->mdio, bus->state, bus->cnt, bus->drive));
207 a3ea5df5 edgar_igl
#if 0
208 a3ea5df5 edgar_igl
        if (bus->mdc)
209 a3ea5df5 edgar_igl
                printf("%d", bus->mdio);
210 a3ea5df5 edgar_igl
#endif
211 a3ea5df5 edgar_igl
        switch (bus->state)
212 a3ea5df5 edgar_igl
        {
213 a3ea5df5 edgar_igl
                case PREAMBLE:
214 a3ea5df5 edgar_igl
                        if (bus->mdc) {
215 a3ea5df5 edgar_igl
                                if (bus->cnt >= (32 * 2) && !bus->mdio) {
216 a3ea5df5 edgar_igl
                                        bus->cnt = 0;
217 a3ea5df5 edgar_igl
                                        bus->state = SOF;
218 a3ea5df5 edgar_igl
                                        bus->data = 0;
219 a3ea5df5 edgar_igl
                                }
220 a3ea5df5 edgar_igl
                        }
221 a3ea5df5 edgar_igl
                        break;
222 a3ea5df5 edgar_igl
                case SOF:
223 a3ea5df5 edgar_igl
                        if (bus->mdc) {
224 a3ea5df5 edgar_igl
                                if (bus->mdio != 1)
225 a3ea5df5 edgar_igl
                                        printf("WARNING: no SOF\n");
226 a3ea5df5 edgar_igl
                                if (bus->cnt == 1*2) {
227 a3ea5df5 edgar_igl
                                        bus->cnt = 0;
228 a3ea5df5 edgar_igl
                                        bus->opc = 0;
229 a3ea5df5 edgar_igl
                                        bus->state = OPC;
230 a3ea5df5 edgar_igl
                                }
231 a3ea5df5 edgar_igl
                        }
232 a3ea5df5 edgar_igl
                        break;
233 a3ea5df5 edgar_igl
                case OPC:
234 a3ea5df5 edgar_igl
                        if (bus->mdc) {
235 a3ea5df5 edgar_igl
                                bus->opc <<= 1;
236 a3ea5df5 edgar_igl
                                bus->opc |= bus->mdio & 1;
237 a3ea5df5 edgar_igl
                                if (bus->cnt == 2*2) {
238 a3ea5df5 edgar_igl
                                        bus->cnt = 0;
239 a3ea5df5 edgar_igl
                                        bus->addr = 0;
240 a3ea5df5 edgar_igl
                                        bus->state = ADDR;
241 a3ea5df5 edgar_igl
                                }
242 a3ea5df5 edgar_igl
                        }
243 a3ea5df5 edgar_igl
                        break;
244 a3ea5df5 edgar_igl
                case ADDR:
245 a3ea5df5 edgar_igl
                        if (bus->mdc) {
246 a3ea5df5 edgar_igl
                                bus->addr <<= 1;
247 a3ea5df5 edgar_igl
                                bus->addr |= bus->mdio & 1;
248 a3ea5df5 edgar_igl
249 a3ea5df5 edgar_igl
                                if (bus->cnt == 5*2) {
250 a3ea5df5 edgar_igl
                                        bus->cnt = 0;
251 a3ea5df5 edgar_igl
                                        bus->req = 0;
252 a3ea5df5 edgar_igl
                                        bus->state = REQ;
253 a3ea5df5 edgar_igl
                                }
254 a3ea5df5 edgar_igl
                        }
255 a3ea5df5 edgar_igl
                        break;
256 a3ea5df5 edgar_igl
                case REQ:
257 a3ea5df5 edgar_igl
                        if (bus->mdc) {
258 a3ea5df5 edgar_igl
                                bus->req <<= 1;
259 a3ea5df5 edgar_igl
                                bus->req |= bus->mdio & 1;
260 a3ea5df5 edgar_igl
                                if (bus->cnt == 5*2) {
261 a3ea5df5 edgar_igl
                                        bus->cnt = 0;
262 a3ea5df5 edgar_igl
                                        bus->state = TURNAROUND;
263 a3ea5df5 edgar_igl
                                }
264 a3ea5df5 edgar_igl
                        }
265 a3ea5df5 edgar_igl
                        break;
266 a3ea5df5 edgar_igl
                case TURNAROUND:
267 a3ea5df5 edgar_igl
                        if (bus->mdc && bus->cnt == 2*2) {
268 a3ea5df5 edgar_igl
                                bus->mdio = 0;
269 a3ea5df5 edgar_igl
                                bus->cnt = 0;
270 a3ea5df5 edgar_igl
271 a3ea5df5 edgar_igl
                                if (bus->opc == 2) {
272 a3ea5df5 edgar_igl
                                        bus->drive = 1;
273 a3ea5df5 edgar_igl
                                        mdio_read_req(bus);
274 a3ea5df5 edgar_igl
                                        bus->mdio = bus->data & 1;
275 a3ea5df5 edgar_igl
                                }
276 a3ea5df5 edgar_igl
                                bus->state = DATA;
277 a3ea5df5 edgar_igl
                        }
278 a3ea5df5 edgar_igl
                        break;
279 a3ea5df5 edgar_igl
                case DATA:                        
280 a3ea5df5 edgar_igl
                        if (!bus->mdc) {
281 a3ea5df5 edgar_igl
                                if (bus->drive) {
282 2e56350e edgar_igl
                                        bus->mdio = !!(bus->data & (1 << 15));
283 2e56350e edgar_igl
                                        bus->data <<= 1;
284 a3ea5df5 edgar_igl
                                }
285 a3ea5df5 edgar_igl
                        } else {
286 a3ea5df5 edgar_igl
                                if (!bus->drive) {
287 a3ea5df5 edgar_igl
                                        bus->data <<= 1;
288 a3ea5df5 edgar_igl
                                        bus->data |= bus->mdio;
289 a3ea5df5 edgar_igl
                                }
290 a3ea5df5 edgar_igl
                                if (bus->cnt == 16 * 2) {
291 a3ea5df5 edgar_igl
                                        bus->cnt = 0;
292 a3ea5df5 edgar_igl
                                        bus->state = PREAMBLE;
293 2e56350e edgar_igl
                                        if (!bus->drive)
294 2e56350e edgar_igl
                                                mdio_write_req(bus);
295 2e56350e edgar_igl
                                        bus->drive = 0;
296 a3ea5df5 edgar_igl
                                }
297 a3ea5df5 edgar_igl
                        }
298 a3ea5df5 edgar_igl
                        break;
299 a3ea5df5 edgar_igl
                default:
300 a3ea5df5 edgar_igl
                        break;
301 a3ea5df5 edgar_igl
        }
302 a3ea5df5 edgar_igl
}
303 a3ea5df5 edgar_igl
304 2e56350e edgar_igl
/* ETRAX-FS Ethernet MAC block starts here.  */
305 2e56350e edgar_igl
306 f6953f13 edgar_igl
#define RW_MA0_LO          0x00
307 35ef81d6 edgar_igl
#define RW_MA0_HI          0x01
308 35ef81d6 edgar_igl
#define RW_MA1_LO          0x02
309 35ef81d6 edgar_igl
#define RW_MA1_HI          0x03
310 35ef81d6 edgar_igl
#define RW_GA_LO          0x04
311 35ef81d6 edgar_igl
#define RW_GA_HI          0x05
312 35ef81d6 edgar_igl
#define RW_GEN_CTRL          0x06
313 35ef81d6 edgar_igl
#define RW_REC_CTRL          0x07
314 35ef81d6 edgar_igl
#define RW_TR_CTRL          0x08
315 35ef81d6 edgar_igl
#define RW_CLR_ERR          0x09
316 35ef81d6 edgar_igl
#define RW_MGM_CTRL          0x0a
317 35ef81d6 edgar_igl
#define R_STAT                  0x0b
318 35ef81d6 edgar_igl
#define FS_ETH_MAX_REGS          0x17
319 a3ea5df5 edgar_igl
320 a3ea5df5 edgar_igl
struct fs_eth
321 a3ea5df5 edgar_igl
{
322 f6953f13 edgar_igl
        CPUState *env;
323 a3ea5df5 edgar_igl
        qemu_irq *irq;
324 a3ea5df5 edgar_igl
        VLANClientState *vc;
325 a3ea5df5 edgar_igl
        int ethregs;
326 a3ea5df5 edgar_igl
327 f6953f13 edgar_igl
        /* Two addrs in the filter.  */
328 f6953f13 edgar_igl
        uint8_t macaddr[2][6];
329 a3ea5df5 edgar_igl
        uint32_t regs[FS_ETH_MAX_REGS];
330 a3ea5df5 edgar_igl
331 a3ea5df5 edgar_igl
        struct etraxfs_dma_client *dma_out;
332 a3ea5df5 edgar_igl
        struct etraxfs_dma_client *dma_in;
333 a3ea5df5 edgar_igl
334 a3ea5df5 edgar_igl
        /* MDIO bus.  */
335 a3ea5df5 edgar_igl
        struct qemu_mdio mdio_bus;
336 c6488268 edgar_igl
        unsigned int phyaddr;
337 c6488268 edgar_igl
        int duplex_mismatch;
338 c6488268 edgar_igl
339 f6953f13 edgar_igl
        /* PHY.         */
340 a3ea5df5 edgar_igl
        struct qemu_phy phy;
341 a3ea5df5 edgar_igl
};
342 a3ea5df5 edgar_igl
343 c6488268 edgar_igl
static void eth_validate_duplex(struct fs_eth *eth)
344 c6488268 edgar_igl
{
345 c6488268 edgar_igl
        struct qemu_phy *phy;
346 c6488268 edgar_igl
        unsigned int phy_duplex;
347 c6488268 edgar_igl
        unsigned int mac_duplex;
348 c6488268 edgar_igl
        int new_mm = 0;
349 c6488268 edgar_igl
350 c6488268 edgar_igl
        phy = eth->mdio_bus.devs[eth->phyaddr];
351 c6488268 edgar_igl
        phy_duplex = !!(phy->read(phy, 18) & (1 << 11));
352 c6488268 edgar_igl
        mac_duplex = !!(eth->regs[RW_REC_CTRL] & 128);
353 c6488268 edgar_igl
354 c6488268 edgar_igl
        if (mac_duplex != phy_duplex)
355 c6488268 edgar_igl
                new_mm = 1;
356 c6488268 edgar_igl
357 c6488268 edgar_igl
        if (eth->regs[RW_GEN_CTRL] & 1) {
358 c6488268 edgar_igl
                if (new_mm != eth->duplex_mismatch) {
359 c6488268 edgar_igl
                        if (new_mm)
360 c6488268 edgar_igl
                                printf("HW: WARNING "
361 c6488268 edgar_igl
                                       "ETH duplex mismatch MAC=%d PHY=%d\n",
362 c6488268 edgar_igl
                                       mac_duplex, phy_duplex);
363 c6488268 edgar_igl
                        else
364 c6488268 edgar_igl
                                printf("HW: ETH duplex ok.\n");
365 c6488268 edgar_igl
                }
366 c6488268 edgar_igl
                eth->duplex_mismatch = new_mm;
367 c6488268 edgar_igl
        }
368 c6488268 edgar_igl
}
369 c6488268 edgar_igl
370 a3ea5df5 edgar_igl
static uint32_t eth_readl (void *opaque, target_phys_addr_t addr)
371 a3ea5df5 edgar_igl
{
372 f6953f13 edgar_igl
        struct fs_eth *eth = opaque;
373 f6953f13 edgar_igl
        uint32_t r = 0;
374 a3ea5df5 edgar_igl
375 35ef81d6 edgar_igl
        addr >>= 2;
376 35ef81d6 edgar_igl
377 f6953f13 edgar_igl
        switch (addr) {
378 a3ea5df5 edgar_igl
                case R_STAT:
379 a3ea5df5 edgar_igl
                        r = eth->mdio_bus.mdio & 1;
380 a3ea5df5 edgar_igl
                        break;
381 f6953f13 edgar_igl
        default:
382 a3ea5df5 edgar_igl
                r = eth->regs[addr];
383 35ef81d6 edgar_igl
                D(printf ("%s %x\n", __func__, addr * 4));
384 f6953f13 edgar_igl
                break;
385 f6953f13 edgar_igl
        }
386 f6953f13 edgar_igl
        return r;
387 a3ea5df5 edgar_igl
}
388 a3ea5df5 edgar_igl
389 f6953f13 edgar_igl
static void eth_update_ma(struct fs_eth *eth, int ma)
390 f6953f13 edgar_igl
{
391 f6953f13 edgar_igl
        int reg;
392 f6953f13 edgar_igl
        int i = 0;
393 f6953f13 edgar_igl
394 f6953f13 edgar_igl
        ma &= 1;
395 f6953f13 edgar_igl
396 f6953f13 edgar_igl
        reg = RW_MA0_LO;
397 f6953f13 edgar_igl
        if (ma)
398 f6953f13 edgar_igl
                reg = RW_MA1_LO;
399 f6953f13 edgar_igl
400 f6953f13 edgar_igl
        eth->macaddr[ma][i++] = eth->regs[reg];
401 f6953f13 edgar_igl
        eth->macaddr[ma][i++] = eth->regs[reg] >> 8;
402 f6953f13 edgar_igl
        eth->macaddr[ma][i++] = eth->regs[reg] >> 16;
403 f6953f13 edgar_igl
        eth->macaddr[ma][i++] = eth->regs[reg] >> 24;
404 f6953f13 edgar_igl
        eth->macaddr[ma][i++] = eth->regs[reg + 4];
405 f6953f13 edgar_igl
        eth->macaddr[ma][i++] = eth->regs[reg + 4] >> 8;
406 f6953f13 edgar_igl
407 f6953f13 edgar_igl
        D(printf("set mac%d=%x.%x.%x.%x.%x.%x\n", ma,
408 f6953f13 edgar_igl
                 eth->macaddr[ma][0], eth->macaddr[ma][1],
409 f6953f13 edgar_igl
                 eth->macaddr[ma][2], eth->macaddr[ma][3],
410 f6953f13 edgar_igl
                 eth->macaddr[ma][4], eth->macaddr[ma][5]));
411 a3ea5df5 edgar_igl
}
412 a3ea5df5 edgar_igl
413 a3ea5df5 edgar_igl
static void
414 a3ea5df5 edgar_igl
eth_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
415 a3ea5df5 edgar_igl
{
416 f6953f13 edgar_igl
        struct fs_eth *eth = opaque;
417 f6953f13 edgar_igl
418 35ef81d6 edgar_igl
        addr >>= 2;
419 f6953f13 edgar_igl
        switch (addr)
420 f6953f13 edgar_igl
        {
421 f6953f13 edgar_igl
                case RW_MA0_LO:
422 f6953f13 edgar_igl
                case RW_MA0_HI:
423 f6953f13 edgar_igl
                        eth->regs[addr] = value;
424 f6953f13 edgar_igl
                        eth_update_ma(eth, 0);
425 f6953f13 edgar_igl
                        break;
426 f6953f13 edgar_igl
                case RW_MA1_LO:
427 f6953f13 edgar_igl
                case RW_MA1_HI:
428 f6953f13 edgar_igl
                        eth->regs[addr] = value;
429 f6953f13 edgar_igl
                        eth_update_ma(eth, 1);
430 f6953f13 edgar_igl
                        break;
431 a3ea5df5 edgar_igl
432 a3ea5df5 edgar_igl
                case RW_MGM_CTRL:
433 a3ea5df5 edgar_igl
                        /* Attach an MDIO/PHY abstraction.  */
434 a3ea5df5 edgar_igl
                        if (value & 2)
435 a3ea5df5 edgar_igl
                                eth->mdio_bus.mdio = value & 1;
436 c6488268 edgar_igl
                        if (eth->mdio_bus.mdc != (value & 4)) {
437 a3ea5df5 edgar_igl
                                mdio_cycle(&eth->mdio_bus);
438 c6488268 edgar_igl
                                eth_validate_duplex(eth);
439 c6488268 edgar_igl
                        }
440 a3ea5df5 edgar_igl
                        eth->mdio_bus.mdc = !!(value & 4);
441 a3ea5df5 edgar_igl
                        break;
442 a3ea5df5 edgar_igl
443 c6488268 edgar_igl
                case RW_REC_CTRL:
444 c6488268 edgar_igl
                        eth->regs[addr] = value;
445 c6488268 edgar_igl
                        eth_validate_duplex(eth);
446 c6488268 edgar_igl
                        break;
447 c6488268 edgar_igl
448 f6953f13 edgar_igl
                default:
449 f6953f13 edgar_igl
                        eth->regs[addr] = value;
450 9bcd77d6 edgar_igl
                        D(printf ("%s %x %x\n",
451 9bcd77d6 edgar_igl
                                  __func__, addr, value));
452 f6953f13 edgar_igl
                        break;
453 f6953f13 edgar_igl
        }
454 f6953f13 edgar_igl
}
455 f6953f13 edgar_igl
456 f6953f13 edgar_igl
/* The ETRAX FS has a groupt address table (GAT) which works like a k=1 bloom
457 f6953f13 edgar_igl
   filter dropping group addresses we have not joined.        The filter has 64
458 f6953f13 edgar_igl
   bits (m). The has function is a simple nible xor of the group addr.        */
459 f6953f13 edgar_igl
static int eth_match_groupaddr(struct fs_eth *eth, const unsigned char *sa)
460 f6953f13 edgar_igl
{
461 f6953f13 edgar_igl
        unsigned int hsh;
462 f6953f13 edgar_igl
        int m_individual = eth->regs[RW_REC_CTRL] & 4;
463 f6953f13 edgar_igl
        int match;
464 f6953f13 edgar_igl
465 f6953f13 edgar_igl
        /* First bit on the wire of a MAC address signals multicast or
466 f6953f13 edgar_igl
           physical address.  */
467 f6953f13 edgar_igl
        if (!m_individual && !sa[0] & 1)
468 f6953f13 edgar_igl
                return 0;
469 f6953f13 edgar_igl
470 f6953f13 edgar_igl
        /* Calculate the hash index for the GA registers. */
471 f6953f13 edgar_igl
        hsh = 0;
472 f6953f13 edgar_igl
        hsh ^= (*sa) & 0x3f;
473 f6953f13 edgar_igl
        hsh ^= ((*sa) >> 6) & 0x03;
474 f6953f13 edgar_igl
        ++sa;
475 f6953f13 edgar_igl
        hsh ^= ((*sa) << 2) & 0x03c;
476 f6953f13 edgar_igl
        hsh ^= ((*sa) >> 4) & 0xf;
477 f6953f13 edgar_igl
        ++sa;
478 f6953f13 edgar_igl
        hsh ^= ((*sa) << 4) & 0x30;
479 f6953f13 edgar_igl
        hsh ^= ((*sa) >> 2) & 0x3f;
480 f6953f13 edgar_igl
        ++sa;
481 f6953f13 edgar_igl
        hsh ^= (*sa) & 0x3f;
482 f6953f13 edgar_igl
        hsh ^= ((*sa) >> 6) & 0x03;
483 f6953f13 edgar_igl
        ++sa;
484 f6953f13 edgar_igl
        hsh ^= ((*sa) << 2) & 0x03c;
485 f6953f13 edgar_igl
        hsh ^= ((*sa) >> 4) & 0xf;
486 f6953f13 edgar_igl
        ++sa;
487 f6953f13 edgar_igl
        hsh ^= ((*sa) << 4) & 0x30;
488 f6953f13 edgar_igl
        hsh ^= ((*sa) >> 2) & 0x3f;
489 f6953f13 edgar_igl
490 f6953f13 edgar_igl
        hsh &= 63;
491 f6953f13 edgar_igl
        if (hsh > 31)
492 f6953f13 edgar_igl
                match = eth->regs[RW_GA_HI] & (1 << (hsh - 32));
493 f6953f13 edgar_igl
        else
494 f6953f13 edgar_igl
                match = eth->regs[RW_GA_LO] & (1 << hsh);
495 f6953f13 edgar_igl
        D(printf("hsh=%x ga=%x.%x mtch=%d\n", hsh,
496 f6953f13 edgar_igl
                 eth->regs[RW_GA_HI], eth->regs[RW_GA_LO], match));
497 f6953f13 edgar_igl
        return match;
498 a3ea5df5 edgar_igl
}
499 a3ea5df5 edgar_igl
500 a3ea5df5 edgar_igl
static int eth_can_receive(void *opaque)
501 a3ea5df5 edgar_igl
{
502 aa25cf46 edgar_igl
        return 1;
503 a3ea5df5 edgar_igl
}
504 a3ea5df5 edgar_igl
505 a3ea5df5 edgar_igl
static void eth_receive(void *opaque, const uint8_t *buf, int size)
506 a3ea5df5 edgar_igl
{
507 f6953f13 edgar_igl
        unsigned char sa_bcast[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
508 a3ea5df5 edgar_igl
        struct fs_eth *eth = opaque;
509 f6953f13 edgar_igl
        int use_ma0 = eth->regs[RW_REC_CTRL] & 1;
510 f6953f13 edgar_igl
        int use_ma1 = eth->regs[RW_REC_CTRL] & 2;
511 f6953f13 edgar_igl
        int r_bcast = eth->regs[RW_REC_CTRL] & 8;
512 f6953f13 edgar_igl
513 f6953f13 edgar_igl
        if (size < 12)
514 f6953f13 edgar_igl
                return;
515 f6953f13 edgar_igl
516 f6953f13 edgar_igl
        D(printf("%x.%x.%x.%x.%x.%x ma=%d %d bc=%d\n",
517 f6953f13 edgar_igl
                 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
518 f6953f13 edgar_igl
                 use_ma0, use_ma1, r_bcast));
519 f6953f13 edgar_igl
               
520 f6953f13 edgar_igl
        /* Does the frame get through the address filters?  */
521 f6953f13 edgar_igl
        if ((!use_ma0 || memcmp(buf, eth->macaddr[0], 6))
522 f6953f13 edgar_igl
            && (!use_ma1 || memcmp(buf, eth->macaddr[1], 6))
523 f6953f13 edgar_igl
            && (!r_bcast || memcmp(buf, sa_bcast, 6))
524 f6953f13 edgar_igl
            && !eth_match_groupaddr(eth, buf))
525 f6953f13 edgar_igl
                return;
526 f6953f13 edgar_igl
527 aa25cf46 edgar_igl
        /* FIXME: Find another way to pass on the fake csum.  */
528 aa25cf46 edgar_igl
        etraxfs_dmac_input(eth->dma_in, (void *)buf, size + 4, 1);
529 a3ea5df5 edgar_igl
}
530 a3ea5df5 edgar_igl
531 a3ea5df5 edgar_igl
static int eth_tx_push(void *opaque, unsigned char *buf, int len)
532 a3ea5df5 edgar_igl
{
533 a3ea5df5 edgar_igl
        struct fs_eth *eth = opaque;
534 a3ea5df5 edgar_igl
535 a3ea5df5 edgar_igl
        D(printf("%s buf=%p len=%d\n", __func__, buf, len));
536 a3ea5df5 edgar_igl
        qemu_send_packet(eth->vc, buf, len);
537 a3ea5df5 edgar_igl
        return len;
538 a3ea5df5 edgar_igl
}
539 a3ea5df5 edgar_igl
540 94410b78 edgar_igl
static void eth_set_link(VLANClientState *vc)
541 94410b78 edgar_igl
{
542 94410b78 edgar_igl
        struct fs_eth *eth = vc->opaque;
543 94410b78 edgar_igl
        D(printf("%s %d\n", __func__, vc->link_down));
544 94410b78 edgar_igl
        eth->phy.link = !vc->link_down;
545 94410b78 edgar_igl
}
546 94410b78 edgar_igl
547 a3ea5df5 edgar_igl
static CPUReadMemoryFunc *eth_read[] = {
548 35ef81d6 edgar_igl
        NULL, NULL,
549 2e56350e edgar_igl
        &eth_readl,
550 a3ea5df5 edgar_igl
};
551 a3ea5df5 edgar_igl
552 a3ea5df5 edgar_igl
static CPUWriteMemoryFunc *eth_write[] = {
553 35ef81d6 edgar_igl
        NULL, NULL,
554 2e56350e edgar_igl
        &eth_writel,
555 a3ea5df5 edgar_igl
};
556 a3ea5df5 edgar_igl
557 a3ea5df5 edgar_igl
void *etraxfs_eth_init(NICInfo *nd, CPUState *env, 
558 94410b78 edgar_igl
                       qemu_irq *irq, target_phys_addr_t base, int phyaddr)
559 a3ea5df5 edgar_igl
{
560 a3ea5df5 edgar_igl
        struct etraxfs_dma_client *dma = NULL;        
561 a3ea5df5 edgar_igl
        struct fs_eth *eth = NULL;
562 a3ea5df5 edgar_igl
563 0ae18cee aliguori
        qemu_check_nic_model(nd, "fseth");
564 0ae18cee aliguori
565 a3ea5df5 edgar_igl
        dma = qemu_mallocz(sizeof *dma * 2);
566 a3ea5df5 edgar_igl
567 a3ea5df5 edgar_igl
        eth = qemu_mallocz(sizeof *eth);
568 a3ea5df5 edgar_igl
569 a3ea5df5 edgar_igl
        dma[0].client.push = eth_tx_push;
570 a3ea5df5 edgar_igl
        dma[0].client.opaque = eth;
571 a3ea5df5 edgar_igl
        dma[1].client.opaque = eth;
572 aa25cf46 edgar_igl
        dma[1].client.pull = NULL;
573 a3ea5df5 edgar_igl
574 a3ea5df5 edgar_igl
        eth->env = env;
575 a3ea5df5 edgar_igl
        eth->irq = irq;
576 a3ea5df5 edgar_igl
        eth->dma_out = dma;
577 a3ea5df5 edgar_igl
        eth->dma_in = dma + 1;
578 a3ea5df5 edgar_igl
579 a3ea5df5 edgar_igl
        /* Connect the phy.  */
580 94410b78 edgar_igl
        eth->phyaddr = phyaddr & 0x1f;
581 a3ea5df5 edgar_igl
        tdk_init(&eth->phy);
582 c6488268 edgar_igl
        mdio_attach(&eth->mdio_bus, &eth->phy, eth->phyaddr);
583 a3ea5df5 edgar_igl
584 a3ea5df5 edgar_igl
        eth->ethregs = cpu_register_io_memory(0, eth_read, eth_write, eth);
585 a3ea5df5 edgar_igl
        cpu_register_physical_memory (base, 0x5c, eth->ethregs);
586 a3ea5df5 edgar_igl
587 7a9f6e4a aliguori
        eth->vc = qemu_new_vlan_client(nd->vlan, nd->model, nd->name,
588 a3ea5df5 edgar_igl
                                       eth_receive, eth_can_receive, eth);
589 94410b78 edgar_igl
        eth->vc->opaque = eth;
590 94410b78 edgar_igl
        eth->vc->link_status_changed = eth_set_link;
591 a3ea5df5 edgar_igl
592 a3ea5df5 edgar_igl
        return dma;
593 a3ea5df5 edgar_igl
}