Statistics
| Branch: | Revision:

root / hw / etraxfs_eth.c @ 57a46d05

History | View | Annotate | Download (13.4 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 a3ea5df5 edgar_igl
        VLANClientState *vc;
323 a3ea5df5 edgar_igl
        int ethregs;
324 a3ea5df5 edgar_igl
325 f6953f13 edgar_igl
        /* Two addrs in the filter.  */
326 f6953f13 edgar_igl
        uint8_t macaddr[2][6];
327 a3ea5df5 edgar_igl
        uint32_t regs[FS_ETH_MAX_REGS];
328 a3ea5df5 edgar_igl
329 a3ea5df5 edgar_igl
        struct etraxfs_dma_client *dma_out;
330 a3ea5df5 edgar_igl
        struct etraxfs_dma_client *dma_in;
331 a3ea5df5 edgar_igl
332 a3ea5df5 edgar_igl
        /* MDIO bus.  */
333 a3ea5df5 edgar_igl
        struct qemu_mdio mdio_bus;
334 c6488268 edgar_igl
        unsigned int phyaddr;
335 c6488268 edgar_igl
        int duplex_mismatch;
336 c6488268 edgar_igl
337 f6953f13 edgar_igl
        /* PHY.         */
338 a3ea5df5 edgar_igl
        struct qemu_phy phy;
339 a3ea5df5 edgar_igl
};
340 a3ea5df5 edgar_igl
341 c6488268 edgar_igl
static void eth_validate_duplex(struct fs_eth *eth)
342 c6488268 edgar_igl
{
343 c6488268 edgar_igl
        struct qemu_phy *phy;
344 c6488268 edgar_igl
        unsigned int phy_duplex;
345 c6488268 edgar_igl
        unsigned int mac_duplex;
346 c6488268 edgar_igl
        int new_mm = 0;
347 c6488268 edgar_igl
348 c6488268 edgar_igl
        phy = eth->mdio_bus.devs[eth->phyaddr];
349 c6488268 edgar_igl
        phy_duplex = !!(phy->read(phy, 18) & (1 << 11));
350 c6488268 edgar_igl
        mac_duplex = !!(eth->regs[RW_REC_CTRL] & 128);
351 c6488268 edgar_igl
352 c6488268 edgar_igl
        if (mac_duplex != phy_duplex)
353 c6488268 edgar_igl
                new_mm = 1;
354 c6488268 edgar_igl
355 c6488268 edgar_igl
        if (eth->regs[RW_GEN_CTRL] & 1) {
356 c6488268 edgar_igl
                if (new_mm != eth->duplex_mismatch) {
357 c6488268 edgar_igl
                        if (new_mm)
358 c6488268 edgar_igl
                                printf("HW: WARNING "
359 c6488268 edgar_igl
                                       "ETH duplex mismatch MAC=%d PHY=%d\n",
360 c6488268 edgar_igl
                                       mac_duplex, phy_duplex);
361 c6488268 edgar_igl
                        else
362 c6488268 edgar_igl
                                printf("HW: ETH duplex ok.\n");
363 c6488268 edgar_igl
                }
364 c6488268 edgar_igl
                eth->duplex_mismatch = new_mm;
365 c6488268 edgar_igl
        }
366 c6488268 edgar_igl
}
367 c6488268 edgar_igl
368 c227f099 Anthony Liguori
static uint32_t eth_readl (void *opaque, target_phys_addr_t addr)
369 a3ea5df5 edgar_igl
{
370 f6953f13 edgar_igl
        struct fs_eth *eth = opaque;
371 f6953f13 edgar_igl
        uint32_t r = 0;
372 a3ea5df5 edgar_igl
373 35ef81d6 edgar_igl
        addr >>= 2;
374 35ef81d6 edgar_igl
375 f6953f13 edgar_igl
        switch (addr) {
376 a3ea5df5 edgar_igl
                case R_STAT:
377 a3ea5df5 edgar_igl
                        r = eth->mdio_bus.mdio & 1;
378 a3ea5df5 edgar_igl
                        break;
379 f6953f13 edgar_igl
        default:
380 a3ea5df5 edgar_igl
                r = eth->regs[addr];
381 35ef81d6 edgar_igl
                D(printf ("%s %x\n", __func__, addr * 4));
382 f6953f13 edgar_igl
                break;
383 f6953f13 edgar_igl
        }
384 f6953f13 edgar_igl
        return r;
385 a3ea5df5 edgar_igl
}
386 a3ea5df5 edgar_igl
387 f6953f13 edgar_igl
static void eth_update_ma(struct fs_eth *eth, int ma)
388 f6953f13 edgar_igl
{
389 f6953f13 edgar_igl
        int reg;
390 f6953f13 edgar_igl
        int i = 0;
391 f6953f13 edgar_igl
392 f6953f13 edgar_igl
        ma &= 1;
393 f6953f13 edgar_igl
394 f6953f13 edgar_igl
        reg = RW_MA0_LO;
395 f6953f13 edgar_igl
        if (ma)
396 f6953f13 edgar_igl
                reg = RW_MA1_LO;
397 f6953f13 edgar_igl
398 f6953f13 edgar_igl
        eth->macaddr[ma][i++] = eth->regs[reg];
399 f6953f13 edgar_igl
        eth->macaddr[ma][i++] = eth->regs[reg] >> 8;
400 f6953f13 edgar_igl
        eth->macaddr[ma][i++] = eth->regs[reg] >> 16;
401 f6953f13 edgar_igl
        eth->macaddr[ma][i++] = eth->regs[reg] >> 24;
402 4af6e404 Edgar E. Iglesias
        eth->macaddr[ma][i++] = eth->regs[reg + 1];
403 4af6e404 Edgar E. Iglesias
        eth->macaddr[ma][i++] = eth->regs[reg + 1] >> 8;
404 f6953f13 edgar_igl
405 f6953f13 edgar_igl
        D(printf("set mac%d=%x.%x.%x.%x.%x.%x\n", ma,
406 f6953f13 edgar_igl
                 eth->macaddr[ma][0], eth->macaddr[ma][1],
407 f6953f13 edgar_igl
                 eth->macaddr[ma][2], eth->macaddr[ma][3],
408 f6953f13 edgar_igl
                 eth->macaddr[ma][4], eth->macaddr[ma][5]));
409 a3ea5df5 edgar_igl
}
410 a3ea5df5 edgar_igl
411 a3ea5df5 edgar_igl
static void
412 c227f099 Anthony Liguori
eth_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
413 a3ea5df5 edgar_igl
{
414 f6953f13 edgar_igl
        struct fs_eth *eth = opaque;
415 f6953f13 edgar_igl
416 35ef81d6 edgar_igl
        addr >>= 2;
417 f6953f13 edgar_igl
        switch (addr)
418 f6953f13 edgar_igl
        {
419 f6953f13 edgar_igl
                case RW_MA0_LO:
420 f6953f13 edgar_igl
                case RW_MA0_HI:
421 f6953f13 edgar_igl
                        eth->regs[addr] = value;
422 f6953f13 edgar_igl
                        eth_update_ma(eth, 0);
423 f6953f13 edgar_igl
                        break;
424 f6953f13 edgar_igl
                case RW_MA1_LO:
425 f6953f13 edgar_igl
                case RW_MA1_HI:
426 f6953f13 edgar_igl
                        eth->regs[addr] = value;
427 f6953f13 edgar_igl
                        eth_update_ma(eth, 1);
428 f6953f13 edgar_igl
                        break;
429 a3ea5df5 edgar_igl
430 a3ea5df5 edgar_igl
                case RW_MGM_CTRL:
431 a3ea5df5 edgar_igl
                        /* Attach an MDIO/PHY abstraction.  */
432 a3ea5df5 edgar_igl
                        if (value & 2)
433 a3ea5df5 edgar_igl
                                eth->mdio_bus.mdio = value & 1;
434 c6488268 edgar_igl
                        if (eth->mdio_bus.mdc != (value & 4)) {
435 a3ea5df5 edgar_igl
                                mdio_cycle(&eth->mdio_bus);
436 c6488268 edgar_igl
                                eth_validate_duplex(eth);
437 c6488268 edgar_igl
                        }
438 a3ea5df5 edgar_igl
                        eth->mdio_bus.mdc = !!(value & 4);
439 a3ea5df5 edgar_igl
                        break;
440 a3ea5df5 edgar_igl
441 c6488268 edgar_igl
                case RW_REC_CTRL:
442 c6488268 edgar_igl
                        eth->regs[addr] = value;
443 c6488268 edgar_igl
                        eth_validate_duplex(eth);
444 c6488268 edgar_igl
                        break;
445 c6488268 edgar_igl
446 f6953f13 edgar_igl
                default:
447 f6953f13 edgar_igl
                        eth->regs[addr] = value;
448 9bcd77d6 edgar_igl
                        D(printf ("%s %x %x\n",
449 9bcd77d6 edgar_igl
                                  __func__, addr, value));
450 f6953f13 edgar_igl
                        break;
451 f6953f13 edgar_igl
        }
452 f6953f13 edgar_igl
}
453 f6953f13 edgar_igl
454 f6953f13 edgar_igl
/* The ETRAX FS has a groupt address table (GAT) which works like a k=1 bloom
455 f6953f13 edgar_igl
   filter dropping group addresses we have not joined.        The filter has 64
456 f6953f13 edgar_igl
   bits (m). The has function is a simple nible xor of the group addr.        */
457 f6953f13 edgar_igl
static int eth_match_groupaddr(struct fs_eth *eth, const unsigned char *sa)
458 f6953f13 edgar_igl
{
459 f6953f13 edgar_igl
        unsigned int hsh;
460 f6953f13 edgar_igl
        int m_individual = eth->regs[RW_REC_CTRL] & 4;
461 f6953f13 edgar_igl
        int match;
462 f6953f13 edgar_igl
463 f6953f13 edgar_igl
        /* First bit on the wire of a MAC address signals multicast or
464 f6953f13 edgar_igl
           physical address.  */
465 f6953f13 edgar_igl
        if (!m_individual && !sa[0] & 1)
466 f6953f13 edgar_igl
                return 0;
467 f6953f13 edgar_igl
468 f6953f13 edgar_igl
        /* Calculate the hash index for the GA registers. */
469 f6953f13 edgar_igl
        hsh = 0;
470 f6953f13 edgar_igl
        hsh ^= (*sa) & 0x3f;
471 f6953f13 edgar_igl
        hsh ^= ((*sa) >> 6) & 0x03;
472 f6953f13 edgar_igl
        ++sa;
473 f6953f13 edgar_igl
        hsh ^= ((*sa) << 2) & 0x03c;
474 f6953f13 edgar_igl
        hsh ^= ((*sa) >> 4) & 0xf;
475 f6953f13 edgar_igl
        ++sa;
476 f6953f13 edgar_igl
        hsh ^= ((*sa) << 4) & 0x30;
477 f6953f13 edgar_igl
        hsh ^= ((*sa) >> 2) & 0x3f;
478 f6953f13 edgar_igl
        ++sa;
479 f6953f13 edgar_igl
        hsh ^= (*sa) & 0x3f;
480 f6953f13 edgar_igl
        hsh ^= ((*sa) >> 6) & 0x03;
481 f6953f13 edgar_igl
        ++sa;
482 f6953f13 edgar_igl
        hsh ^= ((*sa) << 2) & 0x03c;
483 f6953f13 edgar_igl
        hsh ^= ((*sa) >> 4) & 0xf;
484 f6953f13 edgar_igl
        ++sa;
485 f6953f13 edgar_igl
        hsh ^= ((*sa) << 4) & 0x30;
486 f6953f13 edgar_igl
        hsh ^= ((*sa) >> 2) & 0x3f;
487 f6953f13 edgar_igl
488 f6953f13 edgar_igl
        hsh &= 63;
489 f6953f13 edgar_igl
        if (hsh > 31)
490 f6953f13 edgar_igl
                match = eth->regs[RW_GA_HI] & (1 << (hsh - 32));
491 f6953f13 edgar_igl
        else
492 f6953f13 edgar_igl
                match = eth->regs[RW_GA_LO] & (1 << hsh);
493 f6953f13 edgar_igl
        D(printf("hsh=%x ga=%x.%x mtch=%d\n", hsh,
494 f6953f13 edgar_igl
                 eth->regs[RW_GA_HI], eth->regs[RW_GA_LO], match));
495 f6953f13 edgar_igl
        return match;
496 a3ea5df5 edgar_igl
}
497 a3ea5df5 edgar_igl
498 e3f5ec2b Mark McLoughlin
static int eth_can_receive(VLANClientState *vc)
499 a3ea5df5 edgar_igl
{
500 aa25cf46 edgar_igl
        return 1;
501 a3ea5df5 edgar_igl
}
502 a3ea5df5 edgar_igl
503 4f1c942b Mark McLoughlin
static ssize_t eth_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
504 a3ea5df5 edgar_igl
{
505 f6953f13 edgar_igl
        unsigned char sa_bcast[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
506 e3f5ec2b Mark McLoughlin
        struct fs_eth *eth = vc->opaque;
507 f6953f13 edgar_igl
        int use_ma0 = eth->regs[RW_REC_CTRL] & 1;
508 f6953f13 edgar_igl
        int use_ma1 = eth->regs[RW_REC_CTRL] & 2;
509 f6953f13 edgar_igl
        int r_bcast = eth->regs[RW_REC_CTRL] & 8;
510 f6953f13 edgar_igl
511 f6953f13 edgar_igl
        if (size < 12)
512 4f1c942b Mark McLoughlin
                return -1;
513 f6953f13 edgar_igl
514 f6953f13 edgar_igl
        D(printf("%x.%x.%x.%x.%x.%x ma=%d %d bc=%d\n",
515 f6953f13 edgar_igl
                 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
516 f6953f13 edgar_igl
                 use_ma0, use_ma1, r_bcast));
517 f6953f13 edgar_igl
               
518 f6953f13 edgar_igl
        /* Does the frame get through the address filters?  */
519 f6953f13 edgar_igl
        if ((!use_ma0 || memcmp(buf, eth->macaddr[0], 6))
520 f6953f13 edgar_igl
            && (!use_ma1 || memcmp(buf, eth->macaddr[1], 6))
521 f6953f13 edgar_igl
            && (!r_bcast || memcmp(buf, sa_bcast, 6))
522 f6953f13 edgar_igl
            && !eth_match_groupaddr(eth, buf))
523 4f1c942b Mark McLoughlin
                return size;
524 f6953f13 edgar_igl
525 aa25cf46 edgar_igl
        /* FIXME: Find another way to pass on the fake csum.  */
526 aa25cf46 edgar_igl
        etraxfs_dmac_input(eth->dma_in, (void *)buf, size + 4, 1);
527 4f1c942b Mark McLoughlin
528 4f1c942b Mark McLoughlin
        return size;
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 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const eth_read[] = {
548 35ef81d6 edgar_igl
        NULL, NULL,
549 2e56350e edgar_igl
        &eth_readl,
550 a3ea5df5 edgar_igl
};
551 a3ea5df5 edgar_igl
552 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const eth_write[] = {
553 35ef81d6 edgar_igl
        NULL, NULL,
554 2e56350e edgar_igl
        &eth_writel,
555 a3ea5df5 edgar_igl
};
556 a3ea5df5 edgar_igl
557 b946a153 aliguori
static void eth_cleanup(VLANClientState *vc)
558 b946a153 aliguori
{
559 b946a153 aliguori
        struct fs_eth *eth = vc->opaque;
560 b946a153 aliguori
561 b946a153 aliguori
        cpu_unregister_io_memory(eth->ethregs);
562 b946a153 aliguori
563 b946a153 aliguori
        qemu_free(eth->dma_out);
564 b946a153 aliguori
        qemu_free(eth);
565 b946a153 aliguori
}
566 b946a153 aliguori
567 c227f099 Anthony Liguori
void *etraxfs_eth_init(NICInfo *nd, target_phys_addr_t base, int phyaddr)
568 a3ea5df5 edgar_igl
{
569 a3ea5df5 edgar_igl
        struct etraxfs_dma_client *dma = NULL;        
570 a3ea5df5 edgar_igl
        struct fs_eth *eth = NULL;
571 a3ea5df5 edgar_igl
572 0ae18cee aliguori
        qemu_check_nic_model(nd, "fseth");
573 0ae18cee aliguori
574 a3ea5df5 edgar_igl
        dma = qemu_mallocz(sizeof *dma * 2);
575 a3ea5df5 edgar_igl
        eth = qemu_mallocz(sizeof *eth);
576 a3ea5df5 edgar_igl
577 a3ea5df5 edgar_igl
        dma[0].client.push = eth_tx_push;
578 a3ea5df5 edgar_igl
        dma[0].client.opaque = eth;
579 a3ea5df5 edgar_igl
        dma[1].client.opaque = eth;
580 aa25cf46 edgar_igl
        dma[1].client.pull = NULL;
581 a3ea5df5 edgar_igl
582 a3ea5df5 edgar_igl
        eth->dma_out = dma;
583 a3ea5df5 edgar_igl
        eth->dma_in = dma + 1;
584 a3ea5df5 edgar_igl
585 a3ea5df5 edgar_igl
        /* Connect the phy.  */
586 94410b78 edgar_igl
        eth->phyaddr = phyaddr & 0x1f;
587 a3ea5df5 edgar_igl
        tdk_init(&eth->phy);
588 c6488268 edgar_igl
        mdio_attach(&eth->mdio_bus, &eth->phy, eth->phyaddr);
589 a3ea5df5 edgar_igl
590 1eed09cb Avi Kivity
        eth->ethregs = cpu_register_io_memory(eth_read, eth_write, eth);
591 a3ea5df5 edgar_igl
        cpu_register_physical_memory (base, 0x5c, eth->ethregs);
592 a3ea5df5 edgar_igl
593 bb6e6364 Mark McLoughlin
        eth->vc = nd->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_NIC,
594 bb6e6364 Mark McLoughlin
                                                nd->vlan, nd->netdev,
595 283c7c63 Mark McLoughlin
                                                nd->model, nd->name,
596 ae50b274 Mark McLoughlin
                                                eth_can_receive, eth_receive,
597 70783b9c Mark McLoughlin
                                                NULL, NULL, eth_cleanup, eth);
598 94410b78 edgar_igl
        eth->vc->opaque = eth;
599 94410b78 edgar_igl
        eth->vc->link_status_changed = eth_set_link;
600 a3ea5df5 edgar_igl
601 a3ea5df5 edgar_igl
        return dma;
602 a3ea5df5 edgar_igl
}