2 * QEMU ETRAX Ethernet Controller.
4 * Copyright (c) 2008 Edgar E. Iglesias, Axis Communications AB.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 #include "etraxfs_dma.h"
34 * The MDIO extensions in the TDK PHY model were reversed engineered from the
35 * linux driver (PHYID and Diagnostics reg).
36 * TODO: Add friendly names for the register nums.
42 unsigned int (*read
)(struct qemu_phy
*phy
, unsigned int req
);
43 void (*write
)(struct qemu_phy
*phy
, unsigned int req
,
47 static unsigned int tdk_read(struct qemu_phy
*phy
, unsigned int req
)
57 /* Speeds and modes. */
58 r
|= (1 << 13) | (1 << 14);
59 r
|= (1 << 11) | (1 << 12);
60 r
|= (1 << 5); /* Autoneg complete. */
61 r
|= (1 << 3); /* Autoneg able. */
62 r
|= (1 << 2); /* Link. */
65 /* Link partner ability.
66 We are kind; always agree with whatever best mode
67 the guest advertises. */
68 r
= 1 << 14; /* Success. */
69 /* Copy advertised modes. */
70 r
|= phy
->regs
[4] & (15 << 5);
71 /* Autoneg support. */
76 /* Diagnostics reg. */
80 /* Are we advertising 100 half or 100 duplex ? */
81 speed_100
= !!(phy
->regs
[4] & 0x180);
82 /* Are we advertising 10 duplex or 100 duplex ? */
83 duplex
= !!(phy
->regs
[4] & 0x180);
84 r
= (speed_100
<< 10) | (duplex
<< 11);
89 r
= phy
->regs
[regnum
];
92 D(printf("\n%s %x = reg[%d]\n", __func__
, r
, regnum
));
97 tdk_write(struct qemu_phy
*phy
, unsigned int req
, unsigned int data
)
102 D(printf("%s reg[%d] = %x\n", __func__
, regnum
, data
));
105 phy
->regs
[regnum
] = data
;
111 tdk_init(struct qemu_phy
*phy
)
113 phy
->regs
[0] = 0x3100;
115 phy
->regs
[2] = 0x0300;
116 phy
->regs
[3] = 0xe400;
117 /* Autonegotiation advertisement reg. */
118 phy
->regs
[4] = 0x01E1;
120 phy
->read
= tdk_read
;
121 phy
->write
= tdk_write
;
148 struct qemu_phy
*devs
[32];
152 mdio_attach(struct qemu_mdio
*bus
, struct qemu_phy
*phy
, unsigned int addr
)
154 bus
->devs
[addr
& 0x1f] = phy
;
157 #ifdef USE_THIS_DEAD_CODE
159 mdio_detach(struct qemu_mdio
*bus
, struct qemu_phy
*phy
, unsigned int addr
)
161 bus
->devs
[addr
& 0x1f] = NULL
;
165 static void mdio_read_req(struct qemu_mdio
*bus
)
167 struct qemu_phy
*phy
;
169 phy
= bus
->devs
[bus
->addr
];
170 if (phy
&& phy
->read
)
171 bus
->data
= phy
->read(phy
, bus
->req
);
176 static void mdio_write_req(struct qemu_mdio
*bus
)
178 struct qemu_phy
*phy
;
180 phy
= bus
->devs
[bus
->addr
];
181 if (phy
&& phy
->write
)
182 phy
->write(phy
, bus
->req
, bus
->data
);
185 static void mdio_cycle(struct qemu_mdio
*bus
)
189 D(printf("mdc=%d mdio=%d state=%d cnt=%d drv=%d\n",
190 bus
->mdc
, bus
->mdio
, bus
->state
, bus
->cnt
, bus
->drive
));
193 printf("%d", bus
->mdio
);
199 if (bus
->cnt
>= (32 * 2) && !bus
->mdio
) {
209 printf("WARNING: no SOF\n");
210 if (bus
->cnt
== 1*2) {
220 bus
->opc
|= bus
->mdio
& 1;
221 if (bus
->cnt
== 2*2) {
231 bus
->addr
|= bus
->mdio
& 1;
233 if (bus
->cnt
== 5*2) {
243 bus
->req
|= bus
->mdio
& 1;
244 if (bus
->cnt
== 5*2) {
246 bus
->state
= TURNAROUND
;
251 if (bus
->mdc
&& bus
->cnt
== 2*2) {
258 bus
->mdio
= bus
->data
& 1;
266 bus
->mdio
= !!(bus
->data
& (1 << 15));
272 bus
->data
|= bus
->mdio
;
274 if (bus
->cnt
== 16 * 2) {
276 bus
->state
= PREAMBLE
;
288 /* ETRAX-FS Ethernet MAC block starts here. */
290 #define RW_MA0_LO 0x00
291 #define RW_MA0_HI 0x04
292 #define RW_MA1_LO 0x08
293 #define RW_MA1_HI 0x0c
294 #define RW_GA_LO 0x10
295 #define RW_GA_HI 0x14
296 #define RW_GEN_CTRL 0x18
297 #define RW_REC_CTRL 0x1c
298 #define RW_TR_CTRL 0x20
299 #define RW_CLR_ERR 0x24
300 #define RW_MGM_CTRL 0x28
302 #define FS_ETH_MAX_REGS 0x5c
308 target_phys_addr_t base
;
312 /* Two addrs in the filter. */
313 uint8_t macaddr
[2][6];
314 uint32_t regs
[FS_ETH_MAX_REGS
];
316 unsigned char rx_fifo
[1536];
320 struct etraxfs_dma_client
*dma_out
;
321 struct etraxfs_dma_client
*dma_in
;
324 struct qemu_mdio mdio_bus
;
329 static uint32_t eth_rinvalid (void *opaque
, target_phys_addr_t addr
)
331 struct fs_eth
*eth
= opaque
;
332 CPUState
*env
= eth
->env
;
333 cpu_abort(env
, "Unsupported short access. reg=" TARGET_FMT_plx
"\n",
338 static uint32_t eth_readl (void *opaque
, target_phys_addr_t addr
)
340 struct fs_eth
*eth
= opaque
;
343 /* Make addr relative to this instances base. */
347 /* Attach an MDIO/PHY abstraction. */
348 r
= eth
->mdio_bus
.mdio
& 1;
352 D(printf ("%s %x\n", __func__
, addr
));
359 eth_winvalid (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
361 struct fs_eth
*eth
= opaque
;
362 CPUState
*env
= eth
->env
;
363 cpu_abort(env
, "Unsupported short access. reg=" TARGET_FMT_plx
"\n",
367 static void eth_update_ma(struct fs_eth
*eth
, int ma
)
378 eth
->macaddr
[ma
][i
++] = eth
->regs
[reg
];
379 eth
->macaddr
[ma
][i
++] = eth
->regs
[reg
] >> 8;
380 eth
->macaddr
[ma
][i
++] = eth
->regs
[reg
] >> 16;
381 eth
->macaddr
[ma
][i
++] = eth
->regs
[reg
] >> 24;
382 eth
->macaddr
[ma
][i
++] = eth
->regs
[reg
+ 4];
383 eth
->macaddr
[ma
][i
++] = eth
->regs
[reg
+ 4] >> 8;
385 D(printf("set mac%d=%x.%x.%x.%x.%x.%x\n", ma
,
386 eth
->macaddr
[ma
][0], eth
->macaddr
[ma
][1],
387 eth
->macaddr
[ma
][2], eth
->macaddr
[ma
][3],
388 eth
->macaddr
[ma
][4], eth
->macaddr
[ma
][5]));
392 eth_writel (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
394 struct fs_eth
*eth
= opaque
;
396 /* Make addr relative to this instances base. */
401 eth
->regs
[addr
] = value
;
402 eth_update_ma(eth
, 0);
405 eth
->regs
[addr
] = value
;
406 eth_update_ma(eth
, 0);
409 eth
->regs
[addr
] = value
;
410 eth_update_ma(eth
, 1);
413 eth
->regs
[addr
] = value
;
414 eth_update_ma(eth
, 1);
418 /* Attach an MDIO/PHY abstraction. */
420 eth
->mdio_bus
.mdio
= value
& 1;
421 if (eth
->mdio_bus
.mdc
!= (value
& 4))
422 mdio_cycle(ð
->mdio_bus
);
423 eth
->mdio_bus
.mdc
= !!(value
& 4);
427 eth
->regs
[addr
] = value
;
428 D(printf ("%s %x %x\n",
429 __func__
, addr
, value
));
434 /* The ETRAX FS has a groupt address table (GAT) which works like a k=1 bloom
435 filter dropping group addresses we have not joined. The filter has 64
436 bits (m). The has function is a simple nible xor of the group addr. */
437 static int eth_match_groupaddr(struct fs_eth
*eth
, const unsigned char *sa
)
440 int m_individual
= eth
->regs
[RW_REC_CTRL
] & 4;
443 /* First bit on the wire of a MAC address signals multicast or
445 if (!m_individual
&& !sa
[0] & 1)
448 /* Calculate the hash index for the GA registers. */
451 hsh
^= ((*sa
) >> 6) & 0x03;
453 hsh
^= ((*sa
) << 2) & 0x03c;
454 hsh
^= ((*sa
) >> 4) & 0xf;
456 hsh
^= ((*sa
) << 4) & 0x30;
457 hsh
^= ((*sa
) >> 2) & 0x3f;
460 hsh
^= ((*sa
) >> 6) & 0x03;
462 hsh
^= ((*sa
) << 2) & 0x03c;
463 hsh
^= ((*sa
) >> 4) & 0xf;
465 hsh
^= ((*sa
) << 4) & 0x30;
466 hsh
^= ((*sa
) >> 2) & 0x3f;
470 match
= eth
->regs
[RW_GA_HI
] & (1 << (hsh
- 32));
472 match
= eth
->regs
[RW_GA_LO
] & (1 << hsh
);
473 D(printf("hsh=%x ga=%x.%x mtch=%d\n", hsh
,
474 eth
->regs
[RW_GA_HI
], eth
->regs
[RW_GA_LO
], match
));
478 static int eth_can_receive(void *opaque
)
480 struct fs_eth
*eth
= opaque
;
483 r
= eth
->rx_fifo_len
== 0;
485 /* TODO: signal fifo overrun. */
486 printf("PACKET LOSS!\n");
491 static void eth_receive(void *opaque
, const uint8_t *buf
, int size
)
493 unsigned char sa_bcast
[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
494 struct fs_eth
*eth
= opaque
;
495 int use_ma0
= eth
->regs
[RW_REC_CTRL
] & 1;
496 int use_ma1
= eth
->regs
[RW_REC_CTRL
] & 2;
497 int r_bcast
= eth
->regs
[RW_REC_CTRL
] & 8;
502 D(printf("%x.%x.%x.%x.%x.%x ma=%d %d bc=%d\n",
503 buf
[0], buf
[1], buf
[2], buf
[3], buf
[4], buf
[5],
504 use_ma0
, use_ma1
, r_bcast
));
506 /* Does the frame get through the address filters? */
507 if ((!use_ma0
|| memcmp(buf
, eth
->macaddr
[0], 6))
508 && (!use_ma1
|| memcmp(buf
, eth
->macaddr
[1], 6))
509 && (!r_bcast
|| memcmp(buf
, sa_bcast
, 6))
510 && !eth_match_groupaddr(eth
, buf
))
513 if (size
> sizeof(eth
->rx_fifo
)) {
514 /* TODO: signal error. */
515 } else if (eth
->rx_fifo_len
) {
518 memcpy(eth
->rx_fifo
, buf
, size
);
519 /* +4, HW passes the CRC to sw. */
520 eth
->rx_fifo_len
= size
+ 4;
521 eth
->rx_fifo_pos
= 0;
525 static void eth_rx_pull(void *opaque
)
527 struct fs_eth
*eth
= opaque
;
529 if (eth
->rx_fifo_len
) {
530 D(printf("%s %d\n", __func__
, eth
->rx_fifo_len
));
534 for (i
= 0; i
< 32; i
++)
535 printf("%2.2x", eth
->rx_fifo
[i
]);
539 len
= etraxfs_dmac_input(eth
->dma_in
,
540 eth
->rx_fifo
+ eth
->rx_fifo_pos
,
541 eth
->rx_fifo_len
, 1);
542 eth
->rx_fifo_len
-= len
;
543 eth
->rx_fifo_pos
+= len
;
547 static int eth_tx_push(void *opaque
, unsigned char *buf
, int len
)
549 struct fs_eth
*eth
= opaque
;
551 D(printf("%s buf=%p len=%d\n", __func__
, buf
, len
));
552 qemu_send_packet(eth
->vc
, buf
, len
);
556 static CPUReadMemoryFunc
*eth_read
[] = {
562 static CPUWriteMemoryFunc
*eth_write
[] = {
568 void *etraxfs_eth_init(NICInfo
*nd
, CPUState
*env
,
569 qemu_irq
*irq
, target_phys_addr_t base
)
571 struct etraxfs_dma_client
*dma
= NULL
;
572 struct fs_eth
*eth
= NULL
;
574 dma
= qemu_mallocz(sizeof *dma
* 2);
578 eth
= qemu_mallocz(sizeof *eth
);
582 dma
[0].client
.push
= eth_tx_push
;
583 dma
[0].client
.opaque
= eth
;
584 dma
[1].client
.opaque
= eth
;
585 dma
[1].client
.pull
= eth_rx_pull
;
591 eth
->dma_in
= dma
+ 1;
593 /* Connect the phy. */
595 mdio_attach(ð
->mdio_bus
, ð
->phy
, 0x1);
597 eth
->ethregs
= cpu_register_io_memory(0, eth_read
, eth_write
, eth
);
598 cpu_register_physical_memory (base
, 0x5c, eth
->ethregs
);
600 eth
->vc
= qemu_new_vlan_client(nd
->vlan
,
601 eth_receive
, eth_can_receive
, eth
);