1f6569884SThomas Chou /* 2f6569884SThomas Chou * Opencore 10/100 ethernet mac driver 3f6569884SThomas Chou * 4f6569884SThomas Chou * Copyright (C) 2007-2008 Avionic Design Development GmbH 5f6569884SThomas Chou * Copyright (C) 2008-2009 Avionic Design GmbH 6f6569884SThomas Chou * Thierry Reding <thierry.reding@avionic-design.de> 7f6569884SThomas Chou * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw> 8f6569884SThomas Chou * 9f6569884SThomas Chou * This program is free software; you can redistribute it and/or modify 10f6569884SThomas Chou * it under the terms of the GNU General Public License version 2 as 11f6569884SThomas Chou * published by the Free Software Foundation. 12f6569884SThomas Chou */ 13f6569884SThomas Chou 14f6569884SThomas Chou #include <common.h> 15f6569884SThomas Chou #include <command.h> 16f6569884SThomas Chou #include <malloc.h> 17f6569884SThomas Chou #include <net.h> 18f6569884SThomas Chou #include <miiphy.h> 19f6569884SThomas Chou #include <asm/io.h> 20f6569884SThomas Chou #include <asm/cache.h> 21f6569884SThomas Chou 22f6569884SThomas Chou /* register offsets */ 23f6569884SThomas Chou #define MODER 0x00 24f6569884SThomas Chou #define INT_SOURCE 0x04 25f6569884SThomas Chou #define INT_MASK 0x08 26f6569884SThomas Chou #define IPGT 0x0c 27f6569884SThomas Chou #define IPGR1 0x10 28f6569884SThomas Chou #define IPGR2 0x14 29f6569884SThomas Chou #define PACKETLEN 0x18 30f6569884SThomas Chou #define COLLCONF 0x1c 31f6569884SThomas Chou #define TX_BD_NUM 0x20 32f6569884SThomas Chou #define CTRLMODER 0x24 33f6569884SThomas Chou #define MIIMODER 0x28 34f6569884SThomas Chou #define MIICOMMAND 0x2c 35f6569884SThomas Chou #define MIIADDRESS 0x30 36f6569884SThomas Chou #define MIITX_DATA 0x34 37f6569884SThomas Chou #define MIIRX_DATA 0x38 38f6569884SThomas Chou #define MIISTATUS 0x3c 39f6569884SThomas Chou #define MAC_ADDR0 0x40 40f6569884SThomas Chou #define MAC_ADDR1 0x44 41f6569884SThomas Chou #define ETH_HASH0 0x48 42f6569884SThomas Chou #define ETH_HASH1 0x4c 43f6569884SThomas Chou #define ETH_TXCTRL 0x50 44f6569884SThomas Chou 45f6569884SThomas Chou /* mode register */ 46f6569884SThomas Chou #define MODER_RXEN (1 << 0) /* receive enable */ 47f6569884SThomas Chou #define MODER_TXEN (1 << 1) /* transmit enable */ 48f6569884SThomas Chou #define MODER_NOPRE (1 << 2) /* no preamble */ 49f6569884SThomas Chou #define MODER_BRO (1 << 3) /* broadcast address */ 50f6569884SThomas Chou #define MODER_IAM (1 << 4) /* individual address mode */ 51f6569884SThomas Chou #define MODER_PRO (1 << 5) /* promiscuous mode */ 52f6569884SThomas Chou #define MODER_IFG (1 << 6) /* interframe gap for incoming frames */ 53f6569884SThomas Chou #define MODER_LOOP (1 << 7) /* loopback */ 54f6569884SThomas Chou #define MODER_NBO (1 << 8) /* no back-off */ 55f6569884SThomas Chou #define MODER_EDE (1 << 9) /* excess defer enable */ 56f6569884SThomas Chou #define MODER_FULLD (1 << 10) /* full duplex */ 57f6569884SThomas Chou #define MODER_RESET (1 << 11) /* FIXME: reset (undocumented) */ 58f6569884SThomas Chou #define MODER_DCRC (1 << 12) /* delayed CRC enable */ 59f6569884SThomas Chou #define MODER_CRC (1 << 13) /* CRC enable */ 60f6569884SThomas Chou #define MODER_HUGE (1 << 14) /* huge packets enable */ 61f6569884SThomas Chou #define MODER_PAD (1 << 15) /* padding enabled */ 62f6569884SThomas Chou #define MODER_RSM (1 << 16) /* receive small packets */ 63f6569884SThomas Chou 64f6569884SThomas Chou /* interrupt source and mask registers */ 65f6569884SThomas Chou #define INT_MASK_TXF (1 << 0) /* transmit frame */ 66f6569884SThomas Chou #define INT_MASK_TXE (1 << 1) /* transmit error */ 67f6569884SThomas Chou #define INT_MASK_RXF (1 << 2) /* receive frame */ 68f6569884SThomas Chou #define INT_MASK_RXE (1 << 3) /* receive error */ 69f6569884SThomas Chou #define INT_MASK_BUSY (1 << 4) 70f6569884SThomas Chou #define INT_MASK_TXC (1 << 5) /* transmit control frame */ 71f6569884SThomas Chou #define INT_MASK_RXC (1 << 6) /* receive control frame */ 72f6569884SThomas Chou 73f6569884SThomas Chou #define INT_MASK_TX (INT_MASK_TXF | INT_MASK_TXE) 74f6569884SThomas Chou #define INT_MASK_RX (INT_MASK_RXF | INT_MASK_RXE) 75f6569884SThomas Chou 76f6569884SThomas Chou #define INT_MASK_ALL ( \ 77f6569884SThomas Chou INT_MASK_TXF | INT_MASK_TXE | \ 78f6569884SThomas Chou INT_MASK_RXF | INT_MASK_RXE | \ 79f6569884SThomas Chou INT_MASK_TXC | INT_MASK_RXC | \ 80f6569884SThomas Chou INT_MASK_BUSY \ 81f6569884SThomas Chou ) 82f6569884SThomas Chou 83f6569884SThomas Chou /* packet length register */ 84f6569884SThomas Chou #define PACKETLEN_MIN(min) (((min) & 0xffff) << 16) 85f6569884SThomas Chou #define PACKETLEN_MAX(max) (((max) & 0xffff) << 0) 86f6569884SThomas Chou #define PACKETLEN_MIN_MAX(min, max) (PACKETLEN_MIN(min) | \ 87f6569884SThomas Chou PACKETLEN_MAX(max)) 88f6569884SThomas Chou 89f6569884SThomas Chou /* transmit buffer number register */ 90f6569884SThomas Chou #define TX_BD_NUM_VAL(x) (((x) <= 0x80) ? (x) : 0x80) 91f6569884SThomas Chou 92f6569884SThomas Chou /* control module mode register */ 93f6569884SThomas Chou #define CTRLMODER_PASSALL (1 << 0) /* pass all receive frames */ 94f6569884SThomas Chou #define CTRLMODER_RXFLOW (1 << 1) /* receive control flow */ 95f6569884SThomas Chou #define CTRLMODER_TXFLOW (1 << 2) /* transmit control flow */ 96f6569884SThomas Chou 97f6569884SThomas Chou /* MII mode register */ 98f6569884SThomas Chou #define MIIMODER_CLKDIV(x) ((x) & 0xfe) /* needs to be an even number */ 99f6569884SThomas Chou #define MIIMODER_NOPRE (1 << 8) /* no preamble */ 100f6569884SThomas Chou 101f6569884SThomas Chou /* MII command register */ 102f6569884SThomas Chou #define MIICOMMAND_SCAN (1 << 0) /* scan status */ 103f6569884SThomas Chou #define MIICOMMAND_READ (1 << 1) /* read status */ 104f6569884SThomas Chou #define MIICOMMAND_WRITE (1 << 2) /* write control data */ 105f6569884SThomas Chou 106f6569884SThomas Chou /* MII address register */ 107f6569884SThomas Chou #define MIIADDRESS_FIAD(x) (((x) & 0x1f) << 0) 108f6569884SThomas Chou #define MIIADDRESS_RGAD(x) (((x) & 0x1f) << 8) 109f6569884SThomas Chou #define MIIADDRESS_ADDR(phy, reg) (MIIADDRESS_FIAD(phy) | \ 110f6569884SThomas Chou MIIADDRESS_RGAD(reg)) 111f6569884SThomas Chou 112f6569884SThomas Chou /* MII transmit data register */ 113f6569884SThomas Chou #define MIITX_DATA_VAL(x) ((x) & 0xffff) 114f6569884SThomas Chou 115f6569884SThomas Chou /* MII receive data register */ 116f6569884SThomas Chou #define MIIRX_DATA_VAL(x) ((x) & 0xffff) 117f6569884SThomas Chou 118f6569884SThomas Chou /* MII status register */ 119f6569884SThomas Chou #define MIISTATUS_LINKFAIL (1 << 0) 120f6569884SThomas Chou #define MIISTATUS_BUSY (1 << 1) 121f6569884SThomas Chou #define MIISTATUS_INVALID (1 << 2) 122f6569884SThomas Chou 123f6569884SThomas Chou /* TX buffer descriptor */ 124f6569884SThomas Chou #define TX_BD_CS (1 << 0) /* carrier sense lost */ 125f6569884SThomas Chou #define TX_BD_DF (1 << 1) /* defer indication */ 126f6569884SThomas Chou #define TX_BD_LC (1 << 2) /* late collision */ 127f6569884SThomas Chou #define TX_BD_RL (1 << 3) /* retransmission limit */ 128f6569884SThomas Chou #define TX_BD_RETRY_MASK (0x00f0) 129f6569884SThomas Chou #define TX_BD_RETRY(x) (((x) & 0x00f0) >> 4) 130f6569884SThomas Chou #define TX_BD_UR (1 << 8) /* transmitter underrun */ 131f6569884SThomas Chou #define TX_BD_CRC (1 << 11) /* TX CRC enable */ 132f6569884SThomas Chou #define TX_BD_PAD (1 << 12) /* pad enable */ 133f6569884SThomas Chou #define TX_BD_WRAP (1 << 13) 134f6569884SThomas Chou #define TX_BD_IRQ (1 << 14) /* interrupt request enable */ 135f6569884SThomas Chou #define TX_BD_READY (1 << 15) /* TX buffer ready */ 136f6569884SThomas Chou #define TX_BD_LEN(x) (((x) & 0xffff) << 16) 137f6569884SThomas Chou #define TX_BD_LEN_MASK (0xffff << 16) 138f6569884SThomas Chou 139f6569884SThomas Chou #define TX_BD_STATS (TX_BD_CS | TX_BD_DF | TX_BD_LC | \ 140f6569884SThomas Chou TX_BD_RL | TX_BD_RETRY_MASK | TX_BD_UR) 141f6569884SThomas Chou 142f6569884SThomas Chou /* RX buffer descriptor */ 143f6569884SThomas Chou #define RX_BD_LC (1 << 0) /* late collision */ 144f6569884SThomas Chou #define RX_BD_CRC (1 << 1) /* RX CRC error */ 145f6569884SThomas Chou #define RX_BD_SF (1 << 2) /* short frame */ 146f6569884SThomas Chou #define RX_BD_TL (1 << 3) /* too long */ 147f6569884SThomas Chou #define RX_BD_DN (1 << 4) /* dribble nibble */ 148f6569884SThomas Chou #define RX_BD_IS (1 << 5) /* invalid symbol */ 149f6569884SThomas Chou #define RX_BD_OR (1 << 6) /* receiver overrun */ 150f6569884SThomas Chou #define RX_BD_MISS (1 << 7) 151f6569884SThomas Chou #define RX_BD_CF (1 << 8) /* control frame */ 152f6569884SThomas Chou #define RX_BD_WRAP (1 << 13) 153f6569884SThomas Chou #define RX_BD_IRQ (1 << 14) /* interrupt request enable */ 154f6569884SThomas Chou #define RX_BD_EMPTY (1 << 15) 155f6569884SThomas Chou #define RX_BD_LEN(x) (((x) & 0xffff) << 16) 156f6569884SThomas Chou 157f6569884SThomas Chou #define RX_BD_STATS (RX_BD_LC | RX_BD_CRC | RX_BD_SF | RX_BD_TL | \ 158f6569884SThomas Chou RX_BD_DN | RX_BD_IS | RX_BD_OR | RX_BD_MISS) 159f6569884SThomas Chou 160f6569884SThomas Chou #define ETHOC_BUFSIZ 1536 161f6569884SThomas Chou #define ETHOC_ZLEN 64 162f6569884SThomas Chou #define ETHOC_BD_BASE 0x400 163f6569884SThomas Chou #define ETHOC_TIMEOUT (HZ / 2) 164f6569884SThomas Chou #define ETHOC_MII_TIMEOUT (1 + (HZ / 5)) 165f6569884SThomas Chou 166f6569884SThomas Chou /** 167f6569884SThomas Chou * struct ethoc - driver-private device structure 168f6569884SThomas Chou * @num_tx: number of send buffers 169f6569884SThomas Chou * @cur_tx: last send buffer written 170f6569884SThomas Chou * @dty_tx: last buffer actually sent 171f6569884SThomas Chou * @num_rx: number of receive buffers 172f6569884SThomas Chou * @cur_rx: current receive buffer 173f6569884SThomas Chou */ 174f6569884SThomas Chou struct ethoc { 175f6569884SThomas Chou u32 num_tx; 176f6569884SThomas Chou u32 cur_tx; 177f6569884SThomas Chou u32 dty_tx; 178f6569884SThomas Chou u32 num_rx; 179f6569884SThomas Chou u32 cur_rx; 180f6569884SThomas Chou }; 181f6569884SThomas Chou 182f6569884SThomas Chou /** 183f6569884SThomas Chou * struct ethoc_bd - buffer descriptor 184f6569884SThomas Chou * @stat: buffer statistics 185f6569884SThomas Chou * @addr: physical memory address 186f6569884SThomas Chou */ 187f6569884SThomas Chou struct ethoc_bd { 188f6569884SThomas Chou u32 stat; 189f6569884SThomas Chou u32 addr; 190f6569884SThomas Chou }; 191f6569884SThomas Chou 192f6569884SThomas Chou static inline u32 ethoc_read(struct eth_device *dev, loff_t offset) 193f6569884SThomas Chou { 194f6569884SThomas Chou return readl(dev->iobase + offset); 195f6569884SThomas Chou } 196f6569884SThomas Chou 197f6569884SThomas Chou static inline void ethoc_write(struct eth_device *dev, loff_t offset, u32 data) 198f6569884SThomas Chou { 199f6569884SThomas Chou writel(data, dev->iobase + offset); 200f6569884SThomas Chou } 201f6569884SThomas Chou 202f6569884SThomas Chou static inline void ethoc_read_bd(struct eth_device *dev, int index, 203f6569884SThomas Chou struct ethoc_bd *bd) 204f6569884SThomas Chou { 205f6569884SThomas Chou loff_t offset = ETHOC_BD_BASE + (index * sizeof(struct ethoc_bd)); 206f6569884SThomas Chou bd->stat = ethoc_read(dev, offset + 0); 207f6569884SThomas Chou bd->addr = ethoc_read(dev, offset + 4); 208f6569884SThomas Chou } 209f6569884SThomas Chou 210f6569884SThomas Chou static inline void ethoc_write_bd(struct eth_device *dev, int index, 211f6569884SThomas Chou const struct ethoc_bd *bd) 212f6569884SThomas Chou { 213f6569884SThomas Chou loff_t offset = ETHOC_BD_BASE + (index * sizeof(struct ethoc_bd)); 214f6569884SThomas Chou ethoc_write(dev, offset + 0, bd->stat); 215f6569884SThomas Chou ethoc_write(dev, offset + 4, bd->addr); 216f6569884SThomas Chou } 217f6569884SThomas Chou 2183ac9d6c6SThomas Chou static int ethoc_set_mac_address(struct eth_device *dev) 219f6569884SThomas Chou { 220f6569884SThomas Chou u8 *mac = dev->enetaddr; 221f6569884SThomas Chou 222f6569884SThomas Chou ethoc_write(dev, MAC_ADDR0, (mac[2] << 24) | (mac[3] << 16) | 223f6569884SThomas Chou (mac[4] << 8) | (mac[5] << 0)); 224f6569884SThomas Chou ethoc_write(dev, MAC_ADDR1, (mac[0] << 8) | (mac[1] << 0)); 2253ac9d6c6SThomas Chou return 0; 226f6569884SThomas Chou } 227f6569884SThomas Chou 228f6569884SThomas Chou static inline void ethoc_ack_irq(struct eth_device *dev, u32 mask) 229f6569884SThomas Chou { 230f6569884SThomas Chou ethoc_write(dev, INT_SOURCE, mask); 231f6569884SThomas Chou } 232f6569884SThomas Chou 233f6569884SThomas Chou static inline void ethoc_enable_rx_and_tx(struct eth_device *dev) 234f6569884SThomas Chou { 235f6569884SThomas Chou u32 mode = ethoc_read(dev, MODER); 236f6569884SThomas Chou mode |= MODER_RXEN | MODER_TXEN; 237f6569884SThomas Chou ethoc_write(dev, MODER, mode); 238f6569884SThomas Chou } 239f6569884SThomas Chou 240f6569884SThomas Chou static inline void ethoc_disable_rx_and_tx(struct eth_device *dev) 241f6569884SThomas Chou { 242f6569884SThomas Chou u32 mode = ethoc_read(dev, MODER); 243f6569884SThomas Chou mode &= ~(MODER_RXEN | MODER_TXEN); 244f6569884SThomas Chou ethoc_write(dev, MODER, mode); 245f6569884SThomas Chou } 246f6569884SThomas Chou 247f6569884SThomas Chou static int ethoc_init_ring(struct eth_device *dev) 248f6569884SThomas Chou { 249f6569884SThomas Chou struct ethoc *priv = (struct ethoc *)dev->priv; 250f6569884SThomas Chou struct ethoc_bd bd; 251f6569884SThomas Chou int i; 252f6569884SThomas Chou 253f6569884SThomas Chou priv->cur_tx = 0; 254f6569884SThomas Chou priv->dty_tx = 0; 255f6569884SThomas Chou priv->cur_rx = 0; 256f6569884SThomas Chou 257f6569884SThomas Chou /* setup transmission buffers */ 258f6569884SThomas Chou bd.stat = TX_BD_IRQ | TX_BD_CRC; 259f6569884SThomas Chou 260f6569884SThomas Chou for (i = 0; i < priv->num_tx; i++) { 261f6569884SThomas Chou if (i == priv->num_tx - 1) 262f6569884SThomas Chou bd.stat |= TX_BD_WRAP; 263f6569884SThomas Chou 264f6569884SThomas Chou ethoc_write_bd(dev, i, &bd); 265f6569884SThomas Chou } 266f6569884SThomas Chou 267f6569884SThomas Chou bd.stat = RX_BD_EMPTY | RX_BD_IRQ; 268f6569884SThomas Chou 269f6569884SThomas Chou for (i = 0; i < priv->num_rx; i++) { 270f6569884SThomas Chou bd.addr = (u32)NetRxPackets[i]; 271f6569884SThomas Chou if (i == priv->num_rx - 1) 272f6569884SThomas Chou bd.stat |= RX_BD_WRAP; 273f6569884SThomas Chou 274*83ea1308SStefan Kristiansson flush_dcache_range(bd.addr, bd.addr + PKTSIZE_ALIGN); 275f6569884SThomas Chou ethoc_write_bd(dev, priv->num_tx + i, &bd); 276f6569884SThomas Chou } 277f6569884SThomas Chou 278f6569884SThomas Chou return 0; 279f6569884SThomas Chou } 280f6569884SThomas Chou 281f6569884SThomas Chou static int ethoc_reset(struct eth_device *dev) 282f6569884SThomas Chou { 283f6569884SThomas Chou u32 mode; 284f6569884SThomas Chou 285f6569884SThomas Chou /* TODO: reset controller? */ 286f6569884SThomas Chou 287f6569884SThomas Chou ethoc_disable_rx_and_tx(dev); 288f6569884SThomas Chou 289f6569884SThomas Chou /* TODO: setup registers */ 290f6569884SThomas Chou 291f6569884SThomas Chou /* enable FCS generation and automatic padding */ 292f6569884SThomas Chou mode = ethoc_read(dev, MODER); 293f6569884SThomas Chou mode |= MODER_CRC | MODER_PAD; 294f6569884SThomas Chou ethoc_write(dev, MODER, mode); 295f6569884SThomas Chou 296f6569884SThomas Chou /* set full-duplex mode */ 297f6569884SThomas Chou mode = ethoc_read(dev, MODER); 298f6569884SThomas Chou mode |= MODER_FULLD; 299f6569884SThomas Chou ethoc_write(dev, MODER, mode); 300f6569884SThomas Chou ethoc_write(dev, IPGT, 0x15); 301f6569884SThomas Chou 302f6569884SThomas Chou ethoc_ack_irq(dev, INT_MASK_ALL); 303f6569884SThomas Chou ethoc_enable_rx_and_tx(dev); 304f6569884SThomas Chou return 0; 305f6569884SThomas Chou } 306f6569884SThomas Chou 307f6569884SThomas Chou static int ethoc_init(struct eth_device *dev, bd_t * bd) 308f6569884SThomas Chou { 309f6569884SThomas Chou struct ethoc *priv = (struct ethoc *)dev->priv; 310f6569884SThomas Chou printf("ethoc\n"); 311f6569884SThomas Chou 312f6569884SThomas Chou priv->num_tx = 1; 313f6569884SThomas Chou priv->num_rx = PKTBUFSRX; 314f6569884SThomas Chou ethoc_write(dev, TX_BD_NUM, priv->num_tx); 315f6569884SThomas Chou ethoc_init_ring(dev); 316f6569884SThomas Chou ethoc_reset(dev); 317f6569884SThomas Chou 318f6569884SThomas Chou return 0; 319f6569884SThomas Chou } 320f6569884SThomas Chou 321f6569884SThomas Chou static int ethoc_update_rx_stats(struct ethoc_bd *bd) 322f6569884SThomas Chou { 323f6569884SThomas Chou int ret = 0; 324f6569884SThomas Chou 325f6569884SThomas Chou if (bd->stat & RX_BD_TL) { 326f6569884SThomas Chou debug("ETHOC: " "RX: frame too long\n"); 327f6569884SThomas Chou ret++; 328f6569884SThomas Chou } 329f6569884SThomas Chou 330f6569884SThomas Chou if (bd->stat & RX_BD_SF) { 331f6569884SThomas Chou debug("ETHOC: " "RX: frame too short\n"); 332f6569884SThomas Chou ret++; 333f6569884SThomas Chou } 334f6569884SThomas Chou 335f6569884SThomas Chou if (bd->stat & RX_BD_DN) 336f6569884SThomas Chou debug("ETHOC: " "RX: dribble nibble\n"); 337f6569884SThomas Chou 338f6569884SThomas Chou if (bd->stat & RX_BD_CRC) { 339f6569884SThomas Chou debug("ETHOC: " "RX: wrong CRC\n"); 340f6569884SThomas Chou ret++; 341f6569884SThomas Chou } 342f6569884SThomas Chou 343f6569884SThomas Chou if (bd->stat & RX_BD_OR) { 344f6569884SThomas Chou debug("ETHOC: " "RX: overrun\n"); 345f6569884SThomas Chou ret++; 346f6569884SThomas Chou } 347f6569884SThomas Chou 348f6569884SThomas Chou if (bd->stat & RX_BD_LC) { 349f6569884SThomas Chou debug("ETHOC: " "RX: late collision\n"); 350f6569884SThomas Chou ret++; 351f6569884SThomas Chou } 352f6569884SThomas Chou 353f6569884SThomas Chou return ret; 354f6569884SThomas Chou } 355f6569884SThomas Chou 356f6569884SThomas Chou static int ethoc_rx(struct eth_device *dev, int limit) 357f6569884SThomas Chou { 358f6569884SThomas Chou struct ethoc *priv = (struct ethoc *)dev->priv; 359f6569884SThomas Chou int count; 360f6569884SThomas Chou 361f6569884SThomas Chou for (count = 0; count < limit; ++count) { 362f6569884SThomas Chou u32 entry; 363f6569884SThomas Chou struct ethoc_bd bd; 364f6569884SThomas Chou 365f6569884SThomas Chou entry = priv->num_tx + (priv->cur_rx % priv->num_rx); 366f6569884SThomas Chou ethoc_read_bd(dev, entry, &bd); 367f6569884SThomas Chou if (bd.stat & RX_BD_EMPTY) 368f6569884SThomas Chou break; 369f6569884SThomas Chou 370f6569884SThomas Chou debug("%s(): RX buffer %d, %x received\n", 371f6569884SThomas Chou __func__, priv->cur_rx, bd.stat); 372f6569884SThomas Chou if (ethoc_update_rx_stats(&bd) == 0) { 373f6569884SThomas Chou int size = bd.stat >> 16; 374f6569884SThomas Chou size -= 4; /* strip the CRC */ 375f6569884SThomas Chou NetReceive((void *)bd.addr, size); 376f6569884SThomas Chou } 377f6569884SThomas Chou 378f6569884SThomas Chou /* clear the buffer descriptor so it can be reused */ 379*83ea1308SStefan Kristiansson flush_dcache_range(bd.addr, bd.addr + PKTSIZE_ALIGN); 380f6569884SThomas Chou bd.stat &= ~RX_BD_STATS; 381f6569884SThomas Chou bd.stat |= RX_BD_EMPTY; 382f6569884SThomas Chou ethoc_write_bd(dev, entry, &bd); 383f6569884SThomas Chou priv->cur_rx++; 384f6569884SThomas Chou } 385f6569884SThomas Chou 386f6569884SThomas Chou return count; 387f6569884SThomas Chou } 388f6569884SThomas Chou 389f6569884SThomas Chou static int ethoc_update_tx_stats(struct ethoc_bd *bd) 390f6569884SThomas Chou { 391f6569884SThomas Chou if (bd->stat & TX_BD_LC) 392f6569884SThomas Chou debug("ETHOC: " "TX: late collision\n"); 393f6569884SThomas Chou 394f6569884SThomas Chou if (bd->stat & TX_BD_RL) 395f6569884SThomas Chou debug("ETHOC: " "TX: retransmit limit\n"); 396f6569884SThomas Chou 397f6569884SThomas Chou if (bd->stat & TX_BD_UR) 398f6569884SThomas Chou debug("ETHOC: " "TX: underrun\n"); 399f6569884SThomas Chou 400f6569884SThomas Chou if (bd->stat & TX_BD_CS) 401f6569884SThomas Chou debug("ETHOC: " "TX: carrier sense lost\n"); 402f6569884SThomas Chou 403f6569884SThomas Chou return 0; 404f6569884SThomas Chou } 405f6569884SThomas Chou 406f6569884SThomas Chou static void ethoc_tx(struct eth_device *dev) 407f6569884SThomas Chou { 408f6569884SThomas Chou struct ethoc *priv = (struct ethoc *)dev->priv; 409f6569884SThomas Chou u32 entry = priv->dty_tx % priv->num_tx; 410f6569884SThomas Chou struct ethoc_bd bd; 411f6569884SThomas Chou 412f6569884SThomas Chou ethoc_read_bd(dev, entry, &bd); 413f6569884SThomas Chou if ((bd.stat & TX_BD_READY) == 0) 414f6569884SThomas Chou (void)ethoc_update_tx_stats(&bd); 415f6569884SThomas Chou } 416f6569884SThomas Chou 417f6569884SThomas Chou static int ethoc_send(struct eth_device *dev, volatile void *packet, int length) 418f6569884SThomas Chou { 419f6569884SThomas Chou struct ethoc *priv = (struct ethoc *)dev->priv; 420f6569884SThomas Chou struct ethoc_bd bd; 421f6569884SThomas Chou u32 entry; 422f6569884SThomas Chou u32 pending; 423f6569884SThomas Chou int tmo; 424f6569884SThomas Chou 425f6569884SThomas Chou entry = priv->cur_tx % priv->num_tx; 426f6569884SThomas Chou ethoc_read_bd(dev, entry, &bd); 427f6569884SThomas Chou if (unlikely(length < ETHOC_ZLEN)) 428f6569884SThomas Chou bd.stat |= TX_BD_PAD; 429f6569884SThomas Chou else 430f6569884SThomas Chou bd.stat &= ~TX_BD_PAD; 431f6569884SThomas Chou bd.addr = (u32)packet; 432f6569884SThomas Chou 433*83ea1308SStefan Kristiansson flush_dcache_range(bd.addr, bd.addr + length); 434f6569884SThomas Chou bd.stat &= ~(TX_BD_STATS | TX_BD_LEN_MASK); 435f6569884SThomas Chou bd.stat |= TX_BD_LEN(length); 436f6569884SThomas Chou ethoc_write_bd(dev, entry, &bd); 437f6569884SThomas Chou 438f6569884SThomas Chou /* start transmit */ 439f6569884SThomas Chou bd.stat |= TX_BD_READY; 440f6569884SThomas Chou ethoc_write_bd(dev, entry, &bd); 441f6569884SThomas Chou 442f6569884SThomas Chou /* wait for transfer to succeed */ 443f6569884SThomas Chou tmo = get_timer(0) + 5 * CONFIG_SYS_HZ; 444f6569884SThomas Chou while (1) { 445f6569884SThomas Chou pending = ethoc_read(dev, INT_SOURCE); 446f6569884SThomas Chou ethoc_ack_irq(dev, pending & ~INT_MASK_RX); 447f6569884SThomas Chou if (pending & INT_MASK_BUSY) 448f6569884SThomas Chou debug("%s(): packet dropped\n", __func__); 449f6569884SThomas Chou 450f6569884SThomas Chou if (pending & INT_MASK_TX) { 451f6569884SThomas Chou ethoc_tx(dev); 452f6569884SThomas Chou break; 453f6569884SThomas Chou } 454f6569884SThomas Chou if (get_timer(0) >= tmo) { 455f6569884SThomas Chou debug("%s(): timed out\n", __func__); 456f6569884SThomas Chou return -1; 457f6569884SThomas Chou } 458f6569884SThomas Chou } 459f6569884SThomas Chou 460f6569884SThomas Chou debug("%s(): packet sent\n", __func__); 461f6569884SThomas Chou return 0; 462f6569884SThomas Chou } 463f6569884SThomas Chou 464f6569884SThomas Chou static void ethoc_halt(struct eth_device *dev) 465f6569884SThomas Chou { 466f6569884SThomas Chou ethoc_disable_rx_and_tx(dev); 467f6569884SThomas Chou } 468f6569884SThomas Chou 469f6569884SThomas Chou static int ethoc_recv(struct eth_device *dev) 470f6569884SThomas Chou { 471f6569884SThomas Chou u32 pending; 472f6569884SThomas Chou 473f6569884SThomas Chou pending = ethoc_read(dev, INT_SOURCE); 474f6569884SThomas Chou ethoc_ack_irq(dev, pending); 475f6569884SThomas Chou if (pending & INT_MASK_BUSY) 476f6569884SThomas Chou debug("%s(): packet dropped\n", __func__); 477f6569884SThomas Chou if (pending & INT_MASK_RX) { 478f6569884SThomas Chou debug("%s(): rx irq\n", __func__); 479f6569884SThomas Chou ethoc_rx(dev, PKTBUFSRX); 480f6569884SThomas Chou } 481f6569884SThomas Chou 482f6569884SThomas Chou return 0; 483f6569884SThomas Chou } 484f6569884SThomas Chou 485f6569884SThomas Chou int ethoc_initialize(u8 dev_num, int base_addr) 486f6569884SThomas Chou { 487f6569884SThomas Chou struct ethoc *priv; 488f6569884SThomas Chou struct eth_device *dev; 489f6569884SThomas Chou 490f6569884SThomas Chou priv = malloc(sizeof(*priv)); 491f6569884SThomas Chou if (!priv) 492f6569884SThomas Chou return 0; 493f6569884SThomas Chou dev = malloc(sizeof(*dev)); 494f6569884SThomas Chou if (!dev) { 495f6569884SThomas Chou free(priv); 496f6569884SThomas Chou return 0; 497f6569884SThomas Chou } 498f6569884SThomas Chou 499f6569884SThomas Chou memset(dev, 0, sizeof(*dev)); 500f6569884SThomas Chou dev->priv = priv; 501f6569884SThomas Chou dev->iobase = base_addr; 502f6569884SThomas Chou dev->init = ethoc_init; 503f6569884SThomas Chou dev->halt = ethoc_halt; 504f6569884SThomas Chou dev->send = ethoc_send; 505f6569884SThomas Chou dev->recv = ethoc_recv; 5063ac9d6c6SThomas Chou dev->write_hwaddr = ethoc_set_mac_address; 507f6569884SThomas Chou sprintf(dev->name, "%s-%hu", "ETHOC", dev_num); 508f6569884SThomas Chou 509f6569884SThomas Chou eth_register(dev); 510f6569884SThomas Chou return 1; 511f6569884SThomas Chou } 512