root / hw / etraxfs_eth.c @ a0a7573b
History | View | Annotate | Download (14.1 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 | d949396e | Edgar E. Iglesias | #include "sysbus.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 | d949396e | Edgar E. Iglesias | SysBusDevice busdev; |
323 | 163bf3a5 | Mark McLoughlin | NICState *nic; |
324 | 163bf3a5 | Mark McLoughlin | NICConf conf; |
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 | d949396e | Edgar E. Iglesias | union {
|
332 | d949396e | Edgar E. Iglesias | void *vdma_out;
|
333 | d949396e | Edgar E. Iglesias | struct etraxfs_dma_client *dma_out;
|
334 | d949396e | Edgar E. Iglesias | }; |
335 | d949396e | Edgar E. Iglesias | union {
|
336 | d949396e | Edgar E. Iglesias | void *vdma_in;
|
337 | d949396e | Edgar E. Iglesias | struct etraxfs_dma_client *dma_in;
|
338 | d949396e | Edgar E. Iglesias | }; |
339 | a3ea5df5 | edgar_igl | |
340 | a3ea5df5 | edgar_igl | /* MDIO bus. */
|
341 | a3ea5df5 | edgar_igl | struct qemu_mdio mdio_bus;
|
342 | c6488268 | edgar_igl | unsigned int phyaddr; |
343 | c6488268 | edgar_igl | int duplex_mismatch;
|
344 | c6488268 | edgar_igl | |
345 | f6953f13 | edgar_igl | /* PHY. */
|
346 | a3ea5df5 | edgar_igl | struct qemu_phy phy;
|
347 | a3ea5df5 | edgar_igl | }; |
348 | a3ea5df5 | edgar_igl | |
349 | c6488268 | edgar_igl | static void eth_validate_duplex(struct fs_eth *eth) |
350 | c6488268 | edgar_igl | { |
351 | c6488268 | edgar_igl | struct qemu_phy *phy;
|
352 | c6488268 | edgar_igl | unsigned int phy_duplex; |
353 | c6488268 | edgar_igl | unsigned int mac_duplex; |
354 | c6488268 | edgar_igl | int new_mm = 0; |
355 | c6488268 | edgar_igl | |
356 | c6488268 | edgar_igl | phy = eth->mdio_bus.devs[eth->phyaddr]; |
357 | c6488268 | edgar_igl | phy_duplex = !!(phy->read(phy, 18) & (1 << 11)); |
358 | c6488268 | edgar_igl | mac_duplex = !!(eth->regs[RW_REC_CTRL] & 128);
|
359 | c6488268 | edgar_igl | |
360 | c6488268 | edgar_igl | if (mac_duplex != phy_duplex)
|
361 | c6488268 | edgar_igl | new_mm = 1;
|
362 | c6488268 | edgar_igl | |
363 | c6488268 | edgar_igl | if (eth->regs[RW_GEN_CTRL] & 1) { |
364 | c6488268 | edgar_igl | if (new_mm != eth->duplex_mismatch) {
|
365 | c6488268 | edgar_igl | if (new_mm)
|
366 | c6488268 | edgar_igl | printf("HW: WARNING "
|
367 | c6488268 | edgar_igl | "ETH duplex mismatch MAC=%d PHY=%d\n",
|
368 | c6488268 | edgar_igl | mac_duplex, phy_duplex); |
369 | c6488268 | edgar_igl | else
|
370 | c6488268 | edgar_igl | printf("HW: ETH duplex ok.\n");
|
371 | c6488268 | edgar_igl | } |
372 | c6488268 | edgar_igl | eth->duplex_mismatch = new_mm; |
373 | c6488268 | edgar_igl | } |
374 | c6488268 | edgar_igl | } |
375 | c6488268 | edgar_igl | |
376 | c227f099 | Anthony Liguori | static uint32_t eth_readl (void *opaque, target_phys_addr_t addr) |
377 | a3ea5df5 | edgar_igl | { |
378 | f6953f13 | edgar_igl | struct fs_eth *eth = opaque;
|
379 | f6953f13 | edgar_igl | uint32_t r = 0;
|
380 | a3ea5df5 | edgar_igl | |
381 | 35ef81d6 | edgar_igl | addr >>= 2;
|
382 | 35ef81d6 | edgar_igl | |
383 | f6953f13 | edgar_igl | switch (addr) {
|
384 | a3ea5df5 | edgar_igl | case R_STAT:
|
385 | a3ea5df5 | edgar_igl | r = eth->mdio_bus.mdio & 1;
|
386 | a3ea5df5 | edgar_igl | break;
|
387 | f6953f13 | edgar_igl | default:
|
388 | a3ea5df5 | edgar_igl | r = eth->regs[addr]; |
389 | 35ef81d6 | edgar_igl | D(printf ("%s %x\n", __func__, addr * 4)); |
390 | f6953f13 | edgar_igl | break;
|
391 | f6953f13 | edgar_igl | } |
392 | f6953f13 | edgar_igl | return r;
|
393 | a3ea5df5 | edgar_igl | } |
394 | a3ea5df5 | edgar_igl | |
395 | f6953f13 | edgar_igl | static void eth_update_ma(struct fs_eth *eth, int ma) |
396 | f6953f13 | edgar_igl | { |
397 | f6953f13 | edgar_igl | int reg;
|
398 | f6953f13 | edgar_igl | int i = 0; |
399 | f6953f13 | edgar_igl | |
400 | f6953f13 | edgar_igl | ma &= 1;
|
401 | f6953f13 | edgar_igl | |
402 | f6953f13 | edgar_igl | reg = RW_MA0_LO; |
403 | f6953f13 | edgar_igl | if (ma)
|
404 | f6953f13 | edgar_igl | reg = RW_MA1_LO; |
405 | f6953f13 | edgar_igl | |
406 | f6953f13 | edgar_igl | eth->macaddr[ma][i++] = eth->regs[reg]; |
407 | f6953f13 | edgar_igl | eth->macaddr[ma][i++] = eth->regs[reg] >> 8;
|
408 | f6953f13 | edgar_igl | eth->macaddr[ma][i++] = eth->regs[reg] >> 16;
|
409 | f6953f13 | edgar_igl | eth->macaddr[ma][i++] = eth->regs[reg] >> 24;
|
410 | 4af6e404 | Edgar E. Iglesias | eth->macaddr[ma][i++] = eth->regs[reg + 1];
|
411 | 0d84be5b | Blue Swirl | eth->macaddr[ma][i] = eth->regs[reg + 1] >> 8; |
412 | f6953f13 | edgar_igl | |
413 | f6953f13 | edgar_igl | D(printf("set mac%d=%x.%x.%x.%x.%x.%x\n", ma,
|
414 | f6953f13 | edgar_igl | eth->macaddr[ma][0], eth->macaddr[ma][1], |
415 | f6953f13 | edgar_igl | eth->macaddr[ma][2], eth->macaddr[ma][3], |
416 | f6953f13 | edgar_igl | eth->macaddr[ma][4], eth->macaddr[ma][5])); |
417 | a3ea5df5 | edgar_igl | } |
418 | a3ea5df5 | edgar_igl | |
419 | a3ea5df5 | edgar_igl | static void |
420 | c227f099 | Anthony Liguori | eth_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
|
421 | a3ea5df5 | edgar_igl | { |
422 | f6953f13 | edgar_igl | struct fs_eth *eth = opaque;
|
423 | f6953f13 | edgar_igl | |
424 | 35ef81d6 | edgar_igl | addr >>= 2;
|
425 | f6953f13 | edgar_igl | switch (addr)
|
426 | f6953f13 | edgar_igl | { |
427 | f6953f13 | edgar_igl | case RW_MA0_LO:
|
428 | f6953f13 | edgar_igl | case RW_MA0_HI:
|
429 | f6953f13 | edgar_igl | eth->regs[addr] = value; |
430 | f6953f13 | edgar_igl | eth_update_ma(eth, 0);
|
431 | f6953f13 | edgar_igl | break;
|
432 | f6953f13 | edgar_igl | case RW_MA1_LO:
|
433 | f6953f13 | edgar_igl | case RW_MA1_HI:
|
434 | f6953f13 | edgar_igl | eth->regs[addr] = value; |
435 | f6953f13 | edgar_igl | eth_update_ma(eth, 1);
|
436 | f6953f13 | edgar_igl | break;
|
437 | a3ea5df5 | edgar_igl | |
438 | a3ea5df5 | edgar_igl | case RW_MGM_CTRL:
|
439 | a3ea5df5 | edgar_igl | /* Attach an MDIO/PHY abstraction. */
|
440 | a3ea5df5 | edgar_igl | if (value & 2) |
441 | a3ea5df5 | edgar_igl | eth->mdio_bus.mdio = value & 1;
|
442 | c6488268 | edgar_igl | if (eth->mdio_bus.mdc != (value & 4)) { |
443 | a3ea5df5 | edgar_igl | mdio_cycle(ð->mdio_bus); |
444 | c6488268 | edgar_igl | eth_validate_duplex(eth); |
445 | c6488268 | edgar_igl | } |
446 | a3ea5df5 | edgar_igl | eth->mdio_bus.mdc = !!(value & 4);
|
447 | cdcf9153 | Edgar E. Iglesias | eth->regs[addr] = value; |
448 | a3ea5df5 | edgar_igl | break;
|
449 | a3ea5df5 | edgar_igl | |
450 | c6488268 | edgar_igl | case RW_REC_CTRL:
|
451 | c6488268 | edgar_igl | eth->regs[addr] = value; |
452 | c6488268 | edgar_igl | eth_validate_duplex(eth); |
453 | c6488268 | edgar_igl | break;
|
454 | c6488268 | edgar_igl | |
455 | f6953f13 | edgar_igl | default:
|
456 | f6953f13 | edgar_igl | eth->regs[addr] = value; |
457 | 9bcd77d6 | edgar_igl | D(printf ("%s %x %x\n",
|
458 | 9bcd77d6 | edgar_igl | __func__, addr, value)); |
459 | f6953f13 | edgar_igl | break;
|
460 | f6953f13 | edgar_igl | } |
461 | f6953f13 | edgar_igl | } |
462 | f6953f13 | edgar_igl | |
463 | f6953f13 | edgar_igl | /* The ETRAX FS has a groupt address table (GAT) which works like a k=1 bloom
|
464 | f6953f13 | edgar_igl | filter dropping group addresses we have not joined. The filter has 64
|
465 | f6953f13 | edgar_igl | bits (m). The has function is a simple nible xor of the group addr. */
|
466 | f6953f13 | edgar_igl | static int eth_match_groupaddr(struct fs_eth *eth, const unsigned char *sa) |
467 | f6953f13 | edgar_igl | { |
468 | f6953f13 | edgar_igl | unsigned int hsh; |
469 | f6953f13 | edgar_igl | int m_individual = eth->regs[RW_REC_CTRL] & 4; |
470 | f6953f13 | edgar_igl | int match;
|
471 | f6953f13 | edgar_igl | |
472 | f6953f13 | edgar_igl | /* First bit on the wire of a MAC address signals multicast or
|
473 | f6953f13 | edgar_igl | physical address. */
|
474 | 9e84a470 | Blue Swirl | if (!m_individual && !(sa[0] & 1)) |
475 | f6953f13 | edgar_igl | return 0; |
476 | f6953f13 | edgar_igl | |
477 | f6953f13 | edgar_igl | /* Calculate the hash index for the GA registers. */
|
478 | f6953f13 | edgar_igl | hsh = 0;
|
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 | ++sa; |
488 | f6953f13 | edgar_igl | hsh ^= (*sa) & 0x3f;
|
489 | f6953f13 | edgar_igl | hsh ^= ((*sa) >> 6) & 0x03; |
490 | f6953f13 | edgar_igl | ++sa; |
491 | f6953f13 | edgar_igl | hsh ^= ((*sa) << 2) & 0x03c; |
492 | f6953f13 | edgar_igl | hsh ^= ((*sa) >> 4) & 0xf; |
493 | f6953f13 | edgar_igl | ++sa; |
494 | f6953f13 | edgar_igl | hsh ^= ((*sa) << 4) & 0x30; |
495 | f6953f13 | edgar_igl | hsh ^= ((*sa) >> 2) & 0x3f; |
496 | f6953f13 | edgar_igl | |
497 | f6953f13 | edgar_igl | hsh &= 63;
|
498 | f6953f13 | edgar_igl | if (hsh > 31) |
499 | f6953f13 | edgar_igl | match = eth->regs[RW_GA_HI] & (1 << (hsh - 32)); |
500 | f6953f13 | edgar_igl | else
|
501 | f6953f13 | edgar_igl | match = eth->regs[RW_GA_LO] & (1 << hsh);
|
502 | f6953f13 | edgar_igl | D(printf("hsh=%x ga=%x.%x mtch=%d\n", hsh,
|
503 | f6953f13 | edgar_igl | eth->regs[RW_GA_HI], eth->regs[RW_GA_LO], match)); |
504 | f6953f13 | edgar_igl | return match;
|
505 | a3ea5df5 | edgar_igl | } |
506 | a3ea5df5 | edgar_igl | |
507 | 163bf3a5 | Mark McLoughlin | static int eth_can_receive(VLANClientState *nc) |
508 | a3ea5df5 | edgar_igl | { |
509 | aa25cf46 | edgar_igl | return 1; |
510 | a3ea5df5 | edgar_igl | } |
511 | a3ea5df5 | edgar_igl | |
512 | 163bf3a5 | Mark McLoughlin | static ssize_t eth_receive(VLANClientState *nc, const uint8_t *buf, size_t size) |
513 | a3ea5df5 | edgar_igl | { |
514 | f6953f13 | edgar_igl | unsigned char sa_bcast[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
515 | 163bf3a5 | Mark McLoughlin | struct fs_eth *eth = DO_UPCAST(NICState, nc, nc)->opaque;
|
516 | f6953f13 | edgar_igl | int use_ma0 = eth->regs[RW_REC_CTRL] & 1; |
517 | f6953f13 | edgar_igl | int use_ma1 = eth->regs[RW_REC_CTRL] & 2; |
518 | f6953f13 | edgar_igl | int r_bcast = eth->regs[RW_REC_CTRL] & 8; |
519 | f6953f13 | edgar_igl | |
520 | f6953f13 | edgar_igl | if (size < 12) |
521 | 4f1c942b | Mark McLoughlin | return -1; |
522 | f6953f13 | edgar_igl | |
523 | f6953f13 | edgar_igl | D(printf("%x.%x.%x.%x.%x.%x ma=%d %d bc=%d\n",
|
524 | f6953f13 | edgar_igl | buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], |
525 | f6953f13 | edgar_igl | use_ma0, use_ma1, r_bcast)); |
526 | f6953f13 | edgar_igl | |
527 | f6953f13 | edgar_igl | /* Does the frame get through the address filters? */
|
528 | f6953f13 | edgar_igl | if ((!use_ma0 || memcmp(buf, eth->macaddr[0], 6)) |
529 | f6953f13 | edgar_igl | && (!use_ma1 || memcmp(buf, eth->macaddr[1], 6)) |
530 | f6953f13 | edgar_igl | && (!r_bcast || memcmp(buf, sa_bcast, 6))
|
531 | f6953f13 | edgar_igl | && !eth_match_groupaddr(eth, buf)) |
532 | 4f1c942b | Mark McLoughlin | return size;
|
533 | f6953f13 | edgar_igl | |
534 | aa25cf46 | edgar_igl | /* FIXME: Find another way to pass on the fake csum. */
|
535 | aa25cf46 | edgar_igl | etraxfs_dmac_input(eth->dma_in, (void *)buf, size + 4, 1); |
536 | 4f1c942b | Mark McLoughlin | |
537 | 4f1c942b | Mark McLoughlin | return size;
|
538 | a3ea5df5 | edgar_igl | } |
539 | a3ea5df5 | edgar_igl | |
540 | a3ea5df5 | edgar_igl | static int eth_tx_push(void *opaque, unsigned char *buf, int len) |
541 | a3ea5df5 | edgar_igl | { |
542 | a3ea5df5 | edgar_igl | struct fs_eth *eth = opaque;
|
543 | a3ea5df5 | edgar_igl | |
544 | a3ea5df5 | edgar_igl | D(printf("%s buf=%p len=%d\n", __func__, buf, len));
|
545 | 163bf3a5 | Mark McLoughlin | qemu_send_packet(ð->nic->nc, buf, len); |
546 | a3ea5df5 | edgar_igl | return len;
|
547 | a3ea5df5 | edgar_igl | } |
548 | a3ea5df5 | edgar_igl | |
549 | 163bf3a5 | Mark McLoughlin | static void eth_set_link(VLANClientState *nc) |
550 | 94410b78 | edgar_igl | { |
551 | 163bf3a5 | Mark McLoughlin | struct fs_eth *eth = DO_UPCAST(NICState, nc, nc)->opaque;
|
552 | 163bf3a5 | Mark McLoughlin | D(printf("%s %d\n", __func__, nc->link_down));
|
553 | 163bf3a5 | Mark McLoughlin | eth->phy.link = !nc->link_down; |
554 | 94410b78 | edgar_igl | } |
555 | 94410b78 | edgar_igl | |
556 | d60efc6b | Blue Swirl | static CPUReadMemoryFunc * const eth_read[] = { |
557 | 35ef81d6 | edgar_igl | NULL, NULL, |
558 | 2e56350e | edgar_igl | ð_readl, |
559 | a3ea5df5 | edgar_igl | }; |
560 | a3ea5df5 | edgar_igl | |
561 | d60efc6b | Blue Swirl | static CPUWriteMemoryFunc * const eth_write[] = { |
562 | 35ef81d6 | edgar_igl | NULL, NULL, |
563 | 2e56350e | edgar_igl | ð_writel, |
564 | a3ea5df5 | edgar_igl | }; |
565 | a3ea5df5 | edgar_igl | |
566 | 163bf3a5 | Mark McLoughlin | static void eth_cleanup(VLANClientState *nc) |
567 | b946a153 | aliguori | { |
568 | 163bf3a5 | Mark McLoughlin | struct fs_eth *eth = DO_UPCAST(NICState, nc, nc)->opaque;
|
569 | b946a153 | aliguori | |
570 | b946a153 | aliguori | cpu_unregister_io_memory(eth->ethregs); |
571 | b946a153 | aliguori | |
572 | 1da005b3 | Edgar E. Iglesias | /* Disconnect the client. */
|
573 | 1da005b3 | Edgar E. Iglesias | eth->dma_out->client.push = NULL;
|
574 | 1da005b3 | Edgar E. Iglesias | eth->dma_out->client.opaque = NULL;
|
575 | 1da005b3 | Edgar E. Iglesias | eth->dma_in->client.opaque = NULL;
|
576 | 1da005b3 | Edgar E. Iglesias | eth->dma_in->client.pull = NULL;
|
577 | 7267c094 | Anthony Liguori | g_free(eth); |
578 | b946a153 | aliguori | } |
579 | b946a153 | aliguori | |
580 | 163bf3a5 | Mark McLoughlin | static NetClientInfo net_etraxfs_info = {
|
581 | 163bf3a5 | Mark McLoughlin | .type = NET_CLIENT_TYPE_NIC, |
582 | 163bf3a5 | Mark McLoughlin | .size = sizeof(NICState),
|
583 | 163bf3a5 | Mark McLoughlin | .can_receive = eth_can_receive, |
584 | 163bf3a5 | Mark McLoughlin | .receive = eth_receive, |
585 | 163bf3a5 | Mark McLoughlin | .cleanup = eth_cleanup, |
586 | 163bf3a5 | Mark McLoughlin | .link_status_changed = eth_set_link, |
587 | 163bf3a5 | Mark McLoughlin | }; |
588 | 163bf3a5 | Mark McLoughlin | |
589 | d949396e | Edgar E. Iglesias | static int fs_eth_init(SysBusDevice *dev) |
590 | a3ea5df5 | edgar_igl | { |
591 | d949396e | Edgar E. Iglesias | struct fs_eth *s = FROM_SYSBUS(typeof(*s), dev);
|
592 | d949396e | Edgar E. Iglesias | int eth_regs;
|
593 | a3ea5df5 | edgar_igl | |
594 | d949396e | Edgar E. Iglesias | if (!s->dma_out || !s->dma_in) {
|
595 | d949396e | Edgar E. Iglesias | hw_error("Unconnected ETRAX-FS Ethernet MAC.\n");
|
596 | d949396e | Edgar E. Iglesias | } |
597 | a3ea5df5 | edgar_igl | |
598 | d949396e | Edgar E. Iglesias | s->dma_out->client.push = eth_tx_push; |
599 | d949396e | Edgar E. Iglesias | s->dma_out->client.opaque = s; |
600 | d949396e | Edgar E. Iglesias | s->dma_in->client.opaque = s; |
601 | d949396e | Edgar E. Iglesias | s->dma_in->client.pull = NULL;
|
602 | a3ea5df5 | edgar_igl | |
603 | d949396e | Edgar E. Iglesias | eth_regs = cpu_register_io_memory(eth_read, eth_write, s, |
604 | d949396e | Edgar E. Iglesias | DEVICE_LITTLE_ENDIAN); |
605 | d949396e | Edgar E. Iglesias | sysbus_init_mmio(dev, 0x5c, eth_regs);
|
606 | a3ea5df5 | edgar_igl | |
607 | d949396e | Edgar E. Iglesias | qemu_macaddr_default_if_unset(&s->conf.macaddr); |
608 | d949396e | Edgar E. Iglesias | s->nic = qemu_new_nic(&net_etraxfs_info, &s->conf, |
609 | d949396e | Edgar E. Iglesias | dev->qdev.info->name, dev->qdev.id, s); |
610 | d949396e | Edgar E. Iglesias | qemu_format_nic_info_str(&s->nic->nc, s->conf.macaddr.a); |
611 | a3ea5df5 | edgar_igl | |
612 | d949396e | Edgar E. Iglesias | tdk_init(&s->phy); |
613 | d949396e | Edgar E. Iglesias | mdio_attach(&s->mdio_bus, &s->phy, s->phyaddr); |
614 | d949396e | Edgar E. Iglesias | return 0; |
615 | d949396e | Edgar E. Iglesias | } |
616 | a3ea5df5 | edgar_igl | |
617 | d949396e | Edgar E. Iglesias | static SysBusDeviceInfo etraxfs_eth_info = {
|
618 | d949396e | Edgar E. Iglesias | .init = fs_eth_init, |
619 | d949396e | Edgar E. Iglesias | .qdev.name = "etraxfs-eth",
|
620 | d949396e | Edgar E. Iglesias | .qdev.size = sizeof(struct fs_eth), |
621 | d949396e | Edgar E. Iglesias | .qdev.props = (Property[]) { |
622 | d949396e | Edgar E. Iglesias | DEFINE_PROP_UINT32("phyaddr", struct fs_eth, phyaddr, 1), |
623 | d949396e | Edgar E. Iglesias | DEFINE_PROP_PTR("dma_out", struct fs_eth, vdma_out), |
624 | d949396e | Edgar E. Iglesias | DEFINE_PROP_PTR("dma_in", struct fs_eth, vdma_in), |
625 | d949396e | Edgar E. Iglesias | DEFINE_NIC_PROPERTIES(struct fs_eth, conf),
|
626 | d949396e | Edgar E. Iglesias | DEFINE_PROP_END_OF_LIST(), |
627 | d949396e | Edgar E. Iglesias | } |
628 | d949396e | Edgar E. Iglesias | }; |
629 | 163bf3a5 | Mark McLoughlin | |
630 | d949396e | Edgar E. Iglesias | static void etraxfs_eth_register(void) |
631 | d949396e | Edgar E. Iglesias | { |
632 | d949396e | Edgar E. Iglesias | sysbus_register_withprop(&etraxfs_eth_info); |
633 | a3ea5df5 | edgar_igl | } |
634 | d949396e | Edgar E. Iglesias | |
635 | d949396e | Edgar E. Iglesias | device_init(etraxfs_eth_register) |