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;
158 mdio_detach(struct qemu_mdio *bus, struct qemu_phy *phy, unsigned int addr)
160 bus->devs[addr & 0x1f] = NULL;
163 static void mdio_read_req(struct qemu_mdio *bus)
165 struct qemu_phy *phy;
167 phy = bus->devs[bus->addr];
168 if (phy && phy->read)
169 bus->data = phy->read(phy, bus->req);
174 static void mdio_write_req(struct qemu_mdio *bus)
176 struct qemu_phy *phy;
178 phy = bus->devs[bus->addr];
179 if (phy && phy->write)
180 phy->write(phy, bus->req, bus->data);
183 static void mdio_cycle(struct qemu_mdio *bus)
187 D(printf("mdc=%d mdio=%d state=%d cnt=%d drv=%d\n",
188 bus->mdc, bus->mdio, bus->state, bus->cnt, bus->drive));
191 printf("%d", bus->mdio);
197 if (bus->cnt >= (32 * 2) && !bus->mdio) {
207 printf("WARNING: no SOF\n");
208 if (bus->cnt == 1*2) {
218 bus->opc |= bus->mdio & 1;
219 if (bus->cnt == 2*2) {
229 bus->addr |= bus->mdio & 1;
231 if (bus->cnt == 5*2) {
241 bus->req |= bus->mdio & 1;
242 if (bus->cnt == 5*2) {
244 bus->state = TURNAROUND;
249 if (bus->mdc && bus->cnt == 2*2) {
256 bus->mdio = bus->data & 1;
264 bus->mdio = !!(bus->data & (1 << 15));
270 bus->data |= bus->mdio;
272 if (bus->cnt == 16 * 2) {
274 bus->state = PREAMBLE;
286 /* ETRAX-FS Ethernet MAC block starts here. */
289 #define RW_MGM_CTRL 0x28
290 #define FS_ETH_MAX_REGS 0x5c
296 target_phys_addr_t base;
301 uint32_t regs[FS_ETH_MAX_REGS];
303 unsigned char rx_fifo[1536];
307 struct etraxfs_dma_client *dma_out;
308 struct etraxfs_dma_client *dma_in;
311 struct qemu_mdio mdio_bus;
316 static uint32_t eth_rinvalid (void *opaque, target_phys_addr_t addr)
318 struct fs_eth *eth = opaque;
319 CPUState *env = eth->env;
320 cpu_abort(env, "Unsupported short access. reg=%x pc=%x.\n",
325 static uint32_t eth_readl (void *opaque, target_phys_addr_t addr)
327 struct fs_eth *eth = opaque;
328 D(CPUState *env = eth->env);
331 /* Make addr relative to this instances base. */
335 /* Attach an MDIO/PHY abstraction. */
336 r = eth->mdio_bus.mdio & 1;
340 D(printf ("%s %x p=%x\n", __func__, addr, env->pc));
347 eth_winvalid (void *opaque, target_phys_addr_t addr, uint32_t value)
349 struct fs_eth *eth = opaque;
350 CPUState *env = eth->env;
351 cpu_abort(env, "Unsupported short access. reg=%x pc=%x.\n",
356 eth_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
358 struct fs_eth *eth = opaque;
359 CPUState *env = eth->env;
361 /* Make addr relative to this instances base. */
366 /* Attach an MDIO/PHY abstraction. */
368 eth->mdio_bus.mdio = value & 1;
369 if (eth->mdio_bus.mdc != (value & 4))
370 mdio_cycle(ð->mdio_bus);
371 eth->mdio_bus.mdc = !!(value & 4);
375 printf ("%s %x %x pc=%x\n",
376 __func__, addr, value, env->pc);
381 static int eth_can_receive(void *opaque)
383 struct fs_eth *eth = opaque;
386 r = eth->rx_fifo_len == 0;
388 /* TODO: signal fifo overrun. */
389 printf("PACKET LOSS!\n");
394 static void eth_receive(void *opaque, const uint8_t *buf, int size)
396 struct fs_eth *eth = opaque;
397 if (size > sizeof(eth->rx_fifo)) {
398 /* TODO: signal error. */
400 memcpy(eth->rx_fifo, buf, size);
401 /* +4, HW passes the CRC to sw. */
402 eth->rx_fifo_len = size + 4;
403 eth->rx_fifo_pos = 0;
407 static void eth_rx_pull(void *opaque)
409 struct fs_eth *eth = opaque;
411 if (eth->rx_fifo_len) {
412 D(printf("%s %d\n", __func__, eth->rx_fifo_len));
416 for (i = 0; i < 32; i++)
417 printf("%2.2x", eth->rx_fifo[i]);
421 len = etraxfs_dmac_input(eth->dma_in,
422 eth->rx_fifo + eth->rx_fifo_pos,
423 eth->rx_fifo_len, 1);
424 eth->rx_fifo_len -= len;
425 eth->rx_fifo_pos += len;
429 static int eth_tx_push(void *opaque, unsigned char *buf, int len)
431 struct fs_eth *eth = opaque;
433 D(printf("%s buf=%p len=%d\n", __func__, buf, len));
434 qemu_send_packet(eth->vc, buf, len);
438 static CPUReadMemoryFunc *eth_read[] = {
444 static CPUWriteMemoryFunc *eth_write[] = {
450 void *etraxfs_eth_init(NICInfo *nd, CPUState *env,
451 qemu_irq *irq, target_phys_addr_t base)
453 struct etraxfs_dma_client *dma = NULL;
454 struct fs_eth *eth = NULL;
456 dma = qemu_mallocz(sizeof *dma * 2);
460 eth = qemu_mallocz(sizeof *eth);
464 dma[0].client.push = eth_tx_push;
465 dma[0].client.opaque = eth;
466 dma[1].client.opaque = eth;
467 dma[1].client.pull = eth_rx_pull;
473 eth->dma_in = dma + 1;
474 memcpy(eth->macaddr, nd->macaddr, 6);
476 /* Connect the phy. */
478 mdio_attach(ð->mdio_bus, ð->phy, 0x1);
480 eth->ethregs = cpu_register_io_memory(0, eth_read, eth_write, eth);
481 cpu_register_physical_memory (base, 0x5c, eth->ethregs);
483 eth->vc = qemu_new_vlan_client(nd->vlan,
484 eth_receive, eth_can_receive, eth);