119c72cacSJeff Kirsher /* 219c72cacSJeff Kirsher * Driver for the IDT RC32434 (Korina) on-chip ethernet controller. 319c72cacSJeff Kirsher * 419c72cacSJeff Kirsher * Copyright 2004 IDT Inc. (rischelp@idt.com) 519c72cacSJeff Kirsher * Copyright 2006 Felix Fietkau <nbd@openwrt.org> 619c72cacSJeff Kirsher * Copyright 2008 Florian Fainelli <florian@openwrt.org> 719c72cacSJeff Kirsher * 819c72cacSJeff Kirsher * This program is free software; you can redistribute it and/or modify it 919c72cacSJeff Kirsher * under the terms of the GNU General Public License as published by the 1019c72cacSJeff Kirsher * Free Software Foundation; either version 2 of the License, or (at your 1119c72cacSJeff Kirsher * option) any later version. 1219c72cacSJeff Kirsher * 1319c72cacSJeff Kirsher * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 1419c72cacSJeff Kirsher * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 1519c72cacSJeff Kirsher * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 1619c72cacSJeff Kirsher * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1719c72cacSJeff Kirsher * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1819c72cacSJeff Kirsher * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 1919c72cacSJeff Kirsher * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 2019c72cacSJeff Kirsher * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2119c72cacSJeff Kirsher * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2219c72cacSJeff Kirsher * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2319c72cacSJeff Kirsher * 2419c72cacSJeff Kirsher * You should have received a copy of the GNU General Public License along 2519c72cacSJeff Kirsher * with this program; if not, write to the Free Software Foundation, Inc., 2619c72cacSJeff Kirsher * 675 Mass Ave, Cambridge, MA 02139, USA. 2719c72cacSJeff Kirsher * 2819c72cacSJeff Kirsher * Writing to a DMA status register: 2919c72cacSJeff Kirsher * 3019c72cacSJeff Kirsher * When writing to the status register, you should mask the bit you have 3119c72cacSJeff Kirsher * been testing the status register with. Both Tx and Rx DMA registers 3219c72cacSJeff Kirsher * should stick to this procedure. 3319c72cacSJeff Kirsher */ 3419c72cacSJeff Kirsher 3519c72cacSJeff Kirsher #include <linux/module.h> 3619c72cacSJeff Kirsher #include <linux/kernel.h> 3719c72cacSJeff Kirsher #include <linux/moduleparam.h> 3819c72cacSJeff Kirsher #include <linux/sched.h> 3919c72cacSJeff Kirsher #include <linux/ctype.h> 4019c72cacSJeff Kirsher #include <linux/types.h> 4119c72cacSJeff Kirsher #include <linux/interrupt.h> 4219c72cacSJeff Kirsher #include <linux/ioport.h> 4319c72cacSJeff Kirsher #include <linux/in.h> 4419c72cacSJeff Kirsher #include <linux/slab.h> 4519c72cacSJeff Kirsher #include <linux/string.h> 4619c72cacSJeff Kirsher #include <linux/delay.h> 4719c72cacSJeff Kirsher #include <linux/netdevice.h> 4819c72cacSJeff Kirsher #include <linux/etherdevice.h> 4919c72cacSJeff Kirsher #include <linux/skbuff.h> 5019c72cacSJeff Kirsher #include <linux/errno.h> 5119c72cacSJeff Kirsher #include <linux/platform_device.h> 5219c72cacSJeff Kirsher #include <linux/mii.h> 5319c72cacSJeff Kirsher #include <linux/ethtool.h> 5419c72cacSJeff Kirsher #include <linux/crc32.h> 5519c72cacSJeff Kirsher 5619c72cacSJeff Kirsher #include <asm/bootinfo.h> 5719c72cacSJeff Kirsher #include <asm/bitops.h> 5819c72cacSJeff Kirsher #include <asm/pgtable.h> 5919c72cacSJeff Kirsher #include <asm/io.h> 6019c72cacSJeff Kirsher #include <asm/dma.h> 6119c72cacSJeff Kirsher 6219c72cacSJeff Kirsher #include <asm/mach-rc32434/rb.h> 6319c72cacSJeff Kirsher #include <asm/mach-rc32434/rc32434.h> 6419c72cacSJeff Kirsher #include <asm/mach-rc32434/eth.h> 6519c72cacSJeff Kirsher #include <asm/mach-rc32434/dma_v.h> 6619c72cacSJeff Kirsher 6719c72cacSJeff Kirsher #define DRV_NAME "korina" 6819c72cacSJeff Kirsher #define DRV_VERSION "0.10" 6919c72cacSJeff Kirsher #define DRV_RELDATE "04Mar2008" 7019c72cacSJeff Kirsher 7119c72cacSJeff Kirsher #define STATION_ADDRESS_HIGH(dev) (((dev)->dev_addr[0] << 8) | \ 7219c72cacSJeff Kirsher ((dev)->dev_addr[1])) 7319c72cacSJeff Kirsher #define STATION_ADDRESS_LOW(dev) (((dev)->dev_addr[2] << 24) | \ 7419c72cacSJeff Kirsher ((dev)->dev_addr[3] << 16) | \ 7519c72cacSJeff Kirsher ((dev)->dev_addr[4] << 8) | \ 7619c72cacSJeff Kirsher ((dev)->dev_addr[5])) 7719c72cacSJeff Kirsher 7819c72cacSJeff Kirsher #define MII_CLOCK 1250000 /* no more than 2.5MHz */ 7919c72cacSJeff Kirsher 8019c72cacSJeff Kirsher /* the following must be powers of two */ 8119c72cacSJeff Kirsher #define KORINA_NUM_RDS 64 /* number of receive descriptors */ 8219c72cacSJeff Kirsher #define KORINA_NUM_TDS 64 /* number of transmit descriptors */ 8319c72cacSJeff Kirsher 8419c72cacSJeff Kirsher /* KORINA_RBSIZE is the hardware's default maximum receive 8519c72cacSJeff Kirsher * frame size in bytes. Having this hardcoded means that there 8619c72cacSJeff Kirsher * is no support for MTU sizes greater than 1500. */ 8719c72cacSJeff Kirsher #define KORINA_RBSIZE 1536 /* size of one resource buffer = Ether MTU */ 8819c72cacSJeff Kirsher #define KORINA_RDS_MASK (KORINA_NUM_RDS - 1) 8919c72cacSJeff Kirsher #define KORINA_TDS_MASK (KORINA_NUM_TDS - 1) 9019c72cacSJeff Kirsher #define RD_RING_SIZE (KORINA_NUM_RDS * sizeof(struct dma_desc)) 9119c72cacSJeff Kirsher #define TD_RING_SIZE (KORINA_NUM_TDS * sizeof(struct dma_desc)) 9219c72cacSJeff Kirsher 9319c72cacSJeff Kirsher #define TX_TIMEOUT (6000 * HZ / 1000) 9419c72cacSJeff Kirsher 9519c72cacSJeff Kirsher enum chain_status { desc_filled, desc_empty }; 9619c72cacSJeff Kirsher #define IS_DMA_FINISHED(X) (((X) & (DMA_DESC_FINI)) != 0) 9719c72cacSJeff Kirsher #define IS_DMA_DONE(X) (((X) & (DMA_DESC_DONE)) != 0) 9819c72cacSJeff Kirsher #define RCVPKT_LENGTH(X) (((X) & ETH_RX_LEN) >> ETH_RX_LEN_BIT) 9919c72cacSJeff Kirsher 10019c72cacSJeff Kirsher /* Information that need to be kept for each board. */ 10119c72cacSJeff Kirsher struct korina_private { 10219c72cacSJeff Kirsher struct eth_regs *eth_regs; 10319c72cacSJeff Kirsher struct dma_reg *rx_dma_regs; 10419c72cacSJeff Kirsher struct dma_reg *tx_dma_regs; 10519c72cacSJeff Kirsher struct dma_desc *td_ring; /* transmit descriptor ring */ 10619c72cacSJeff Kirsher struct dma_desc *rd_ring; /* receive descriptor ring */ 10719c72cacSJeff Kirsher 10819c72cacSJeff Kirsher struct sk_buff *tx_skb[KORINA_NUM_TDS]; 10919c72cacSJeff Kirsher struct sk_buff *rx_skb[KORINA_NUM_RDS]; 11019c72cacSJeff Kirsher 11119c72cacSJeff Kirsher int rx_next_done; 11219c72cacSJeff Kirsher int rx_chain_head; 11319c72cacSJeff Kirsher int rx_chain_tail; 11419c72cacSJeff Kirsher enum chain_status rx_chain_status; 11519c72cacSJeff Kirsher 11619c72cacSJeff Kirsher int tx_next_done; 11719c72cacSJeff Kirsher int tx_chain_head; 11819c72cacSJeff Kirsher int tx_chain_tail; 11919c72cacSJeff Kirsher enum chain_status tx_chain_status; 12019c72cacSJeff Kirsher int tx_count; 12119c72cacSJeff Kirsher int tx_full; 12219c72cacSJeff Kirsher 12319c72cacSJeff Kirsher int rx_irq; 12419c72cacSJeff Kirsher int tx_irq; 12519c72cacSJeff Kirsher int ovr_irq; 12619c72cacSJeff Kirsher int und_irq; 12719c72cacSJeff Kirsher 12819c72cacSJeff Kirsher spinlock_t lock; /* NIC xmit lock */ 12919c72cacSJeff Kirsher 13019c72cacSJeff Kirsher int dma_halt_cnt; 13119c72cacSJeff Kirsher int dma_run_cnt; 13219c72cacSJeff Kirsher struct napi_struct napi; 13319c72cacSJeff Kirsher struct timer_list media_check_timer; 13419c72cacSJeff Kirsher struct mii_if_info mii_if; 13519c72cacSJeff Kirsher struct work_struct restart_task; 13619c72cacSJeff Kirsher struct net_device *dev; 13719c72cacSJeff Kirsher int phy_addr; 13819c72cacSJeff Kirsher }; 13919c72cacSJeff Kirsher 14019c72cacSJeff Kirsher extern unsigned int idt_cpu_freq; 14119c72cacSJeff Kirsher 14219c72cacSJeff Kirsher static inline void korina_start_dma(struct dma_reg *ch, u32 dma_addr) 14319c72cacSJeff Kirsher { 14419c72cacSJeff Kirsher writel(0, &ch->dmandptr); 14519c72cacSJeff Kirsher writel(dma_addr, &ch->dmadptr); 14619c72cacSJeff Kirsher } 14719c72cacSJeff Kirsher 14819c72cacSJeff Kirsher static inline void korina_abort_dma(struct net_device *dev, 14919c72cacSJeff Kirsher struct dma_reg *ch) 15019c72cacSJeff Kirsher { 15119c72cacSJeff Kirsher if (readl(&ch->dmac) & DMA_CHAN_RUN_BIT) { 15219c72cacSJeff Kirsher writel(0x10, &ch->dmac); 15319c72cacSJeff Kirsher 15419c72cacSJeff Kirsher while (!(readl(&ch->dmas) & DMA_STAT_HALT)) 155860e9538SFlorian Westphal netif_trans_update(dev); 15619c72cacSJeff Kirsher 15719c72cacSJeff Kirsher writel(0, &ch->dmas); 15819c72cacSJeff Kirsher } 15919c72cacSJeff Kirsher 16019c72cacSJeff Kirsher writel(0, &ch->dmadptr); 16119c72cacSJeff Kirsher writel(0, &ch->dmandptr); 16219c72cacSJeff Kirsher } 16319c72cacSJeff Kirsher 16419c72cacSJeff Kirsher static inline void korina_chain_dma(struct dma_reg *ch, u32 dma_addr) 16519c72cacSJeff Kirsher { 16619c72cacSJeff Kirsher writel(dma_addr, &ch->dmandptr); 16719c72cacSJeff Kirsher } 16819c72cacSJeff Kirsher 16919c72cacSJeff Kirsher static void korina_abort_tx(struct net_device *dev) 17019c72cacSJeff Kirsher { 17119c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 17219c72cacSJeff Kirsher 17319c72cacSJeff Kirsher korina_abort_dma(dev, lp->tx_dma_regs); 17419c72cacSJeff Kirsher } 17519c72cacSJeff Kirsher 17619c72cacSJeff Kirsher static void korina_abort_rx(struct net_device *dev) 17719c72cacSJeff Kirsher { 17819c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 17919c72cacSJeff Kirsher 18019c72cacSJeff Kirsher korina_abort_dma(dev, lp->rx_dma_regs); 18119c72cacSJeff Kirsher } 18219c72cacSJeff Kirsher 18319c72cacSJeff Kirsher static void korina_start_rx(struct korina_private *lp, 18419c72cacSJeff Kirsher struct dma_desc *rd) 18519c72cacSJeff Kirsher { 18619c72cacSJeff Kirsher korina_start_dma(lp->rx_dma_regs, CPHYSADDR(rd)); 18719c72cacSJeff Kirsher } 18819c72cacSJeff Kirsher 18919c72cacSJeff Kirsher static void korina_chain_rx(struct korina_private *lp, 19019c72cacSJeff Kirsher struct dma_desc *rd) 19119c72cacSJeff Kirsher { 19219c72cacSJeff Kirsher korina_chain_dma(lp->rx_dma_regs, CPHYSADDR(rd)); 19319c72cacSJeff Kirsher } 19419c72cacSJeff Kirsher 19519c72cacSJeff Kirsher /* transmit packet */ 19619c72cacSJeff Kirsher static int korina_send_packet(struct sk_buff *skb, struct net_device *dev) 19719c72cacSJeff Kirsher { 19819c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 19919c72cacSJeff Kirsher unsigned long flags; 20019c72cacSJeff Kirsher u32 length; 20119c72cacSJeff Kirsher u32 chain_prev, chain_next; 20219c72cacSJeff Kirsher struct dma_desc *td; 20319c72cacSJeff Kirsher 20419c72cacSJeff Kirsher spin_lock_irqsave(&lp->lock, flags); 20519c72cacSJeff Kirsher 20619c72cacSJeff Kirsher td = &lp->td_ring[lp->tx_chain_tail]; 20719c72cacSJeff Kirsher 20819c72cacSJeff Kirsher /* stop queue when full, drop pkts if queue already full */ 20919c72cacSJeff Kirsher if (lp->tx_count >= (KORINA_NUM_TDS - 2)) { 21019c72cacSJeff Kirsher lp->tx_full = 1; 21119c72cacSJeff Kirsher 21219c72cacSJeff Kirsher if (lp->tx_count == (KORINA_NUM_TDS - 2)) 21319c72cacSJeff Kirsher netif_stop_queue(dev); 21419c72cacSJeff Kirsher else { 21519c72cacSJeff Kirsher dev->stats.tx_dropped++; 21619c72cacSJeff Kirsher dev_kfree_skb_any(skb); 21719c72cacSJeff Kirsher spin_unlock_irqrestore(&lp->lock, flags); 21819c72cacSJeff Kirsher 21919c72cacSJeff Kirsher return NETDEV_TX_BUSY; 22019c72cacSJeff Kirsher } 22119c72cacSJeff Kirsher } 22219c72cacSJeff Kirsher 22319c72cacSJeff Kirsher lp->tx_count++; 22419c72cacSJeff Kirsher 22519c72cacSJeff Kirsher lp->tx_skb[lp->tx_chain_tail] = skb; 22619c72cacSJeff Kirsher 22719c72cacSJeff Kirsher length = skb->len; 22819c72cacSJeff Kirsher dma_cache_wback((u32)skb->data, skb->len); 22919c72cacSJeff Kirsher 23019c72cacSJeff Kirsher /* Setup the transmit descriptor. */ 23119c72cacSJeff Kirsher dma_cache_inv((u32) td, sizeof(*td)); 23219c72cacSJeff Kirsher td->ca = CPHYSADDR(skb->data); 23319c72cacSJeff Kirsher chain_prev = (lp->tx_chain_tail - 1) & KORINA_TDS_MASK; 23419c72cacSJeff Kirsher chain_next = (lp->tx_chain_tail + 1) & KORINA_TDS_MASK; 23519c72cacSJeff Kirsher 23619c72cacSJeff Kirsher if (readl(&(lp->tx_dma_regs->dmandptr)) == 0) { 23719c72cacSJeff Kirsher if (lp->tx_chain_status == desc_empty) { 23819c72cacSJeff Kirsher /* Update tail */ 23919c72cacSJeff Kirsher td->control = DMA_COUNT(length) | 24019c72cacSJeff Kirsher DMA_DESC_COF | DMA_DESC_IOF; 24119c72cacSJeff Kirsher /* Move tail */ 24219c72cacSJeff Kirsher lp->tx_chain_tail = chain_next; 24319c72cacSJeff Kirsher /* Write to NDPTR */ 24419c72cacSJeff Kirsher writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]), 24519c72cacSJeff Kirsher &lp->tx_dma_regs->dmandptr); 24619c72cacSJeff Kirsher /* Move head to tail */ 24719c72cacSJeff Kirsher lp->tx_chain_head = lp->tx_chain_tail; 24819c72cacSJeff Kirsher } else { 24919c72cacSJeff Kirsher /* Update tail */ 25019c72cacSJeff Kirsher td->control = DMA_COUNT(length) | 25119c72cacSJeff Kirsher DMA_DESC_COF | DMA_DESC_IOF; 25219c72cacSJeff Kirsher /* Link to prev */ 25319c72cacSJeff Kirsher lp->td_ring[chain_prev].control &= 25419c72cacSJeff Kirsher ~DMA_DESC_COF; 25519c72cacSJeff Kirsher /* Link to prev */ 25619c72cacSJeff Kirsher lp->td_ring[chain_prev].link = CPHYSADDR(td); 25719c72cacSJeff Kirsher /* Move tail */ 25819c72cacSJeff Kirsher lp->tx_chain_tail = chain_next; 25919c72cacSJeff Kirsher /* Write to NDPTR */ 26019c72cacSJeff Kirsher writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]), 26119c72cacSJeff Kirsher &(lp->tx_dma_regs->dmandptr)); 26219c72cacSJeff Kirsher /* Move head to tail */ 26319c72cacSJeff Kirsher lp->tx_chain_head = lp->tx_chain_tail; 26419c72cacSJeff Kirsher lp->tx_chain_status = desc_empty; 26519c72cacSJeff Kirsher } 26619c72cacSJeff Kirsher } else { 26719c72cacSJeff Kirsher if (lp->tx_chain_status == desc_empty) { 26819c72cacSJeff Kirsher /* Update tail */ 26919c72cacSJeff Kirsher td->control = DMA_COUNT(length) | 27019c72cacSJeff Kirsher DMA_DESC_COF | DMA_DESC_IOF; 27119c72cacSJeff Kirsher /* Move tail */ 27219c72cacSJeff Kirsher lp->tx_chain_tail = chain_next; 27319c72cacSJeff Kirsher lp->tx_chain_status = desc_filled; 27419c72cacSJeff Kirsher } else { 27519c72cacSJeff Kirsher /* Update tail */ 27619c72cacSJeff Kirsher td->control = DMA_COUNT(length) | 27719c72cacSJeff Kirsher DMA_DESC_COF | DMA_DESC_IOF; 27819c72cacSJeff Kirsher lp->td_ring[chain_prev].control &= 27919c72cacSJeff Kirsher ~DMA_DESC_COF; 28019c72cacSJeff Kirsher lp->td_ring[chain_prev].link = CPHYSADDR(td); 28119c72cacSJeff Kirsher lp->tx_chain_tail = chain_next; 28219c72cacSJeff Kirsher } 28319c72cacSJeff Kirsher } 28419c72cacSJeff Kirsher dma_cache_wback((u32) td, sizeof(*td)); 28519c72cacSJeff Kirsher 286860e9538SFlorian Westphal netif_trans_update(dev); 28719c72cacSJeff Kirsher spin_unlock_irqrestore(&lp->lock, flags); 28819c72cacSJeff Kirsher 28919c72cacSJeff Kirsher return NETDEV_TX_OK; 29019c72cacSJeff Kirsher } 29119c72cacSJeff Kirsher 29219c72cacSJeff Kirsher static int mdio_read(struct net_device *dev, int mii_id, int reg) 29319c72cacSJeff Kirsher { 29419c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 29519c72cacSJeff Kirsher int ret; 29619c72cacSJeff Kirsher 29719c72cacSJeff Kirsher mii_id = ((lp->rx_irq == 0x2c ? 1 : 0) << 8); 29819c72cacSJeff Kirsher 29919c72cacSJeff Kirsher writel(0, &lp->eth_regs->miimcfg); 30019c72cacSJeff Kirsher writel(0, &lp->eth_regs->miimcmd); 30119c72cacSJeff Kirsher writel(mii_id | reg, &lp->eth_regs->miimaddr); 30219c72cacSJeff Kirsher writel(ETH_MII_CMD_SCN, &lp->eth_regs->miimcmd); 30319c72cacSJeff Kirsher 30419c72cacSJeff Kirsher ret = (int)(readl(&lp->eth_regs->miimrdd)); 30519c72cacSJeff Kirsher return ret; 30619c72cacSJeff Kirsher } 30719c72cacSJeff Kirsher 30819c72cacSJeff Kirsher static void mdio_write(struct net_device *dev, int mii_id, int reg, int val) 30919c72cacSJeff Kirsher { 31019c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 31119c72cacSJeff Kirsher 31219c72cacSJeff Kirsher mii_id = ((lp->rx_irq == 0x2c ? 1 : 0) << 8); 31319c72cacSJeff Kirsher 31419c72cacSJeff Kirsher writel(0, &lp->eth_regs->miimcfg); 31519c72cacSJeff Kirsher writel(1, &lp->eth_regs->miimcmd); 31619c72cacSJeff Kirsher writel(mii_id | reg, &lp->eth_regs->miimaddr); 31719c72cacSJeff Kirsher writel(ETH_MII_CMD_SCN, &lp->eth_regs->miimcmd); 31819c72cacSJeff Kirsher writel(val, &lp->eth_regs->miimwtd); 31919c72cacSJeff Kirsher } 32019c72cacSJeff Kirsher 32119c72cacSJeff Kirsher /* Ethernet Rx DMA interrupt */ 32219c72cacSJeff Kirsher static irqreturn_t korina_rx_dma_interrupt(int irq, void *dev_id) 32319c72cacSJeff Kirsher { 32419c72cacSJeff Kirsher struct net_device *dev = dev_id; 32519c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 32619c72cacSJeff Kirsher u32 dmas, dmasm; 32719c72cacSJeff Kirsher irqreturn_t retval; 32819c72cacSJeff Kirsher 32919c72cacSJeff Kirsher dmas = readl(&lp->rx_dma_regs->dmas); 33019c72cacSJeff Kirsher if (dmas & (DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR)) { 33119c72cacSJeff Kirsher dmasm = readl(&lp->rx_dma_regs->dmasm); 33219c72cacSJeff Kirsher writel(dmasm | (DMA_STAT_DONE | 33319c72cacSJeff Kirsher DMA_STAT_HALT | DMA_STAT_ERR), 33419c72cacSJeff Kirsher &lp->rx_dma_regs->dmasm); 33519c72cacSJeff Kirsher 33619c72cacSJeff Kirsher napi_schedule(&lp->napi); 33719c72cacSJeff Kirsher 33819c72cacSJeff Kirsher if (dmas & DMA_STAT_ERR) 33919c72cacSJeff Kirsher printk(KERN_ERR "%s: DMA error\n", dev->name); 34019c72cacSJeff Kirsher 34119c72cacSJeff Kirsher retval = IRQ_HANDLED; 34219c72cacSJeff Kirsher } else 34319c72cacSJeff Kirsher retval = IRQ_NONE; 34419c72cacSJeff Kirsher 34519c72cacSJeff Kirsher return retval; 34619c72cacSJeff Kirsher } 34719c72cacSJeff Kirsher 34819c72cacSJeff Kirsher static int korina_rx(struct net_device *dev, int limit) 34919c72cacSJeff Kirsher { 35019c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 35119c72cacSJeff Kirsher struct dma_desc *rd = &lp->rd_ring[lp->rx_next_done]; 35219c72cacSJeff Kirsher struct sk_buff *skb, *skb_new; 35319c72cacSJeff Kirsher u8 *pkt_buf; 35419c72cacSJeff Kirsher u32 devcs, pkt_len, dmas; 35519c72cacSJeff Kirsher int count; 35619c72cacSJeff Kirsher 35719c72cacSJeff Kirsher dma_cache_inv((u32)rd, sizeof(*rd)); 35819c72cacSJeff Kirsher 35919c72cacSJeff Kirsher for (count = 0; count < limit; count++) { 36019c72cacSJeff Kirsher skb = lp->rx_skb[lp->rx_next_done]; 36119c72cacSJeff Kirsher skb_new = NULL; 36219c72cacSJeff Kirsher 36319c72cacSJeff Kirsher devcs = rd->devcs; 36419c72cacSJeff Kirsher 36519c72cacSJeff Kirsher if ((KORINA_RBSIZE - (u32)DMA_COUNT(rd->control)) == 0) 36619c72cacSJeff Kirsher break; 36719c72cacSJeff Kirsher 36819c72cacSJeff Kirsher /* Update statistics counters */ 36919c72cacSJeff Kirsher if (devcs & ETH_RX_CRC) 37019c72cacSJeff Kirsher dev->stats.rx_crc_errors++; 37119c72cacSJeff Kirsher if (devcs & ETH_RX_LOR) 37219c72cacSJeff Kirsher dev->stats.rx_length_errors++; 37319c72cacSJeff Kirsher if (devcs & ETH_RX_LE) 37419c72cacSJeff Kirsher dev->stats.rx_length_errors++; 37519c72cacSJeff Kirsher if (devcs & ETH_RX_OVR) 37619c72cacSJeff Kirsher dev->stats.rx_fifo_errors++; 37719c72cacSJeff Kirsher if (devcs & ETH_RX_CV) 37819c72cacSJeff Kirsher dev->stats.rx_frame_errors++; 37919c72cacSJeff Kirsher if (devcs & ETH_RX_CES) 38019c72cacSJeff Kirsher dev->stats.rx_length_errors++; 38119c72cacSJeff Kirsher if (devcs & ETH_RX_MP) 38219c72cacSJeff Kirsher dev->stats.multicast++; 38319c72cacSJeff Kirsher 38419c72cacSJeff Kirsher if ((devcs & ETH_RX_LD) != ETH_RX_LD) { 38519c72cacSJeff Kirsher /* check that this is a whole packet 38619c72cacSJeff Kirsher * WARNING: DMA_FD bit incorrectly set 38719c72cacSJeff Kirsher * in Rc32434 (errata ref #077) */ 38819c72cacSJeff Kirsher dev->stats.rx_errors++; 38919c72cacSJeff Kirsher dev->stats.rx_dropped++; 39019c72cacSJeff Kirsher } else if ((devcs & ETH_RX_ROK)) { 39119c72cacSJeff Kirsher pkt_len = RCVPKT_LENGTH(devcs); 39219c72cacSJeff Kirsher 39319c72cacSJeff Kirsher /* must be the (first and) last 39419c72cacSJeff Kirsher * descriptor then */ 39519c72cacSJeff Kirsher pkt_buf = (u8 *)lp->rx_skb[lp->rx_next_done]->data; 39619c72cacSJeff Kirsher 39719c72cacSJeff Kirsher /* invalidate the cache */ 39819c72cacSJeff Kirsher dma_cache_inv((unsigned long)pkt_buf, pkt_len - 4); 39919c72cacSJeff Kirsher 40019c72cacSJeff Kirsher /* Malloc up new buffer. */ 40119c72cacSJeff Kirsher skb_new = netdev_alloc_skb_ip_align(dev, KORINA_RBSIZE); 40219c72cacSJeff Kirsher 40319c72cacSJeff Kirsher if (!skb_new) 40419c72cacSJeff Kirsher break; 40519c72cacSJeff Kirsher /* Do not count the CRC */ 40619c72cacSJeff Kirsher skb_put(skb, pkt_len - 4); 40719c72cacSJeff Kirsher skb->protocol = eth_type_trans(skb, dev); 40819c72cacSJeff Kirsher 40919c72cacSJeff Kirsher /* Pass the packet to upper layers */ 41019c72cacSJeff Kirsher netif_receive_skb(skb); 41119c72cacSJeff Kirsher dev->stats.rx_packets++; 41219c72cacSJeff Kirsher dev->stats.rx_bytes += pkt_len; 41319c72cacSJeff Kirsher 41419c72cacSJeff Kirsher /* Update the mcast stats */ 41519c72cacSJeff Kirsher if (devcs & ETH_RX_MP) 41619c72cacSJeff Kirsher dev->stats.multicast++; 41719c72cacSJeff Kirsher 41819c72cacSJeff Kirsher lp->rx_skb[lp->rx_next_done] = skb_new; 41919c72cacSJeff Kirsher } 42019c72cacSJeff Kirsher 42119c72cacSJeff Kirsher rd->devcs = 0; 42219c72cacSJeff Kirsher 42319c72cacSJeff Kirsher /* Restore descriptor's curr_addr */ 42419c72cacSJeff Kirsher if (skb_new) 42519c72cacSJeff Kirsher rd->ca = CPHYSADDR(skb_new->data); 42619c72cacSJeff Kirsher else 42719c72cacSJeff Kirsher rd->ca = CPHYSADDR(skb->data); 42819c72cacSJeff Kirsher 42919c72cacSJeff Kirsher rd->control = DMA_COUNT(KORINA_RBSIZE) | 43019c72cacSJeff Kirsher DMA_DESC_COD | DMA_DESC_IOD; 43119c72cacSJeff Kirsher lp->rd_ring[(lp->rx_next_done - 1) & 43219c72cacSJeff Kirsher KORINA_RDS_MASK].control &= 43319c72cacSJeff Kirsher ~DMA_DESC_COD; 43419c72cacSJeff Kirsher 43519c72cacSJeff Kirsher lp->rx_next_done = (lp->rx_next_done + 1) & KORINA_RDS_MASK; 43619c72cacSJeff Kirsher dma_cache_wback((u32)rd, sizeof(*rd)); 43719c72cacSJeff Kirsher rd = &lp->rd_ring[lp->rx_next_done]; 43819c72cacSJeff Kirsher writel(~DMA_STAT_DONE, &lp->rx_dma_regs->dmas); 43919c72cacSJeff Kirsher } 44019c72cacSJeff Kirsher 44119c72cacSJeff Kirsher dmas = readl(&lp->rx_dma_regs->dmas); 44219c72cacSJeff Kirsher 44319c72cacSJeff Kirsher if (dmas & DMA_STAT_HALT) { 44419c72cacSJeff Kirsher writel(~(DMA_STAT_HALT | DMA_STAT_ERR), 44519c72cacSJeff Kirsher &lp->rx_dma_regs->dmas); 44619c72cacSJeff Kirsher 44719c72cacSJeff Kirsher lp->dma_halt_cnt++; 44819c72cacSJeff Kirsher rd->devcs = 0; 44919c72cacSJeff Kirsher skb = lp->rx_skb[lp->rx_next_done]; 45019c72cacSJeff Kirsher rd->ca = CPHYSADDR(skb->data); 45119c72cacSJeff Kirsher dma_cache_wback((u32)rd, sizeof(*rd)); 45219c72cacSJeff Kirsher korina_chain_rx(lp, rd); 45319c72cacSJeff Kirsher } 45419c72cacSJeff Kirsher 45519c72cacSJeff Kirsher return count; 45619c72cacSJeff Kirsher } 45719c72cacSJeff Kirsher 45819c72cacSJeff Kirsher static int korina_poll(struct napi_struct *napi, int budget) 45919c72cacSJeff Kirsher { 46019c72cacSJeff Kirsher struct korina_private *lp = 46119c72cacSJeff Kirsher container_of(napi, struct korina_private, napi); 46219c72cacSJeff Kirsher struct net_device *dev = lp->dev; 46319c72cacSJeff Kirsher int work_done; 46419c72cacSJeff Kirsher 46519c72cacSJeff Kirsher work_done = korina_rx(dev, budget); 46619c72cacSJeff Kirsher if (work_done < budget) { 4676ad20165SEric Dumazet napi_complete_done(napi, work_done); 46819c72cacSJeff Kirsher 46919c72cacSJeff Kirsher writel(readl(&lp->rx_dma_regs->dmasm) & 47019c72cacSJeff Kirsher ~(DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR), 47119c72cacSJeff Kirsher &lp->rx_dma_regs->dmasm); 47219c72cacSJeff Kirsher } 47319c72cacSJeff Kirsher return work_done; 47419c72cacSJeff Kirsher } 47519c72cacSJeff Kirsher 47619c72cacSJeff Kirsher /* 47719c72cacSJeff Kirsher * Set or clear the multicast filter for this adaptor. 47819c72cacSJeff Kirsher */ 47919c72cacSJeff Kirsher static void korina_multicast_list(struct net_device *dev) 48019c72cacSJeff Kirsher { 48119c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 48219c72cacSJeff Kirsher unsigned long flags; 48319c72cacSJeff Kirsher struct netdev_hw_addr *ha; 48419c72cacSJeff Kirsher u32 recognise = ETH_ARC_AB; /* always accept broadcasts */ 48519c72cacSJeff Kirsher 48619c72cacSJeff Kirsher /* Set promiscuous mode */ 48719c72cacSJeff Kirsher if (dev->flags & IFF_PROMISC) 48819c72cacSJeff Kirsher recognise |= ETH_ARC_PRO; 48919c72cacSJeff Kirsher 49019c72cacSJeff Kirsher else if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 4)) 49119c72cacSJeff Kirsher /* All multicast and broadcast */ 49219c72cacSJeff Kirsher recognise |= ETH_ARC_AM; 49319c72cacSJeff Kirsher 49419c72cacSJeff Kirsher /* Build the hash table */ 49519c72cacSJeff Kirsher if (netdev_mc_count(dev) > 4) { 496e998fd41SEmilio López u16 hash_table[4] = { 0 }; 49719c72cacSJeff Kirsher u32 crc; 49819c72cacSJeff Kirsher 49919c72cacSJeff Kirsher netdev_for_each_mc_addr(ha, dev) { 50019c72cacSJeff Kirsher crc = ether_crc_le(6, ha->addr); 50119c72cacSJeff Kirsher crc >>= 26; 50219c72cacSJeff Kirsher hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf)); 50319c72cacSJeff Kirsher } 50419c72cacSJeff Kirsher /* Accept filtered multicast */ 50519c72cacSJeff Kirsher recognise |= ETH_ARC_AFM; 50619c72cacSJeff Kirsher 50719c72cacSJeff Kirsher /* Fill the MAC hash tables with their values */ 50819c72cacSJeff Kirsher writel((u32)(hash_table[1] << 16 | hash_table[0]), 50919c72cacSJeff Kirsher &lp->eth_regs->ethhash0); 51019c72cacSJeff Kirsher writel((u32)(hash_table[3] << 16 | hash_table[2]), 51119c72cacSJeff Kirsher &lp->eth_regs->ethhash1); 51219c72cacSJeff Kirsher } 51319c72cacSJeff Kirsher 51419c72cacSJeff Kirsher spin_lock_irqsave(&lp->lock, flags); 51519c72cacSJeff Kirsher writel(recognise, &lp->eth_regs->etharc); 51619c72cacSJeff Kirsher spin_unlock_irqrestore(&lp->lock, flags); 51719c72cacSJeff Kirsher } 51819c72cacSJeff Kirsher 51919c72cacSJeff Kirsher static void korina_tx(struct net_device *dev) 52019c72cacSJeff Kirsher { 52119c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 52219c72cacSJeff Kirsher struct dma_desc *td = &lp->td_ring[lp->tx_next_done]; 52319c72cacSJeff Kirsher u32 devcs; 52419c72cacSJeff Kirsher u32 dmas; 52519c72cacSJeff Kirsher 52619c72cacSJeff Kirsher spin_lock(&lp->lock); 52719c72cacSJeff Kirsher 52819c72cacSJeff Kirsher /* Process all desc that are done */ 52919c72cacSJeff Kirsher while (IS_DMA_FINISHED(td->control)) { 53019c72cacSJeff Kirsher if (lp->tx_full == 1) { 53119c72cacSJeff Kirsher netif_wake_queue(dev); 53219c72cacSJeff Kirsher lp->tx_full = 0; 53319c72cacSJeff Kirsher } 53419c72cacSJeff Kirsher 53519c72cacSJeff Kirsher devcs = lp->td_ring[lp->tx_next_done].devcs; 53619c72cacSJeff Kirsher if ((devcs & (ETH_TX_FD | ETH_TX_LD)) != 53719c72cacSJeff Kirsher (ETH_TX_FD | ETH_TX_LD)) { 53819c72cacSJeff Kirsher dev->stats.tx_errors++; 53919c72cacSJeff Kirsher dev->stats.tx_dropped++; 54019c72cacSJeff Kirsher 54119c72cacSJeff Kirsher /* Should never happen */ 54219c72cacSJeff Kirsher printk(KERN_ERR "%s: split tx ignored\n", 54319c72cacSJeff Kirsher dev->name); 54419c72cacSJeff Kirsher } else if (devcs & ETH_TX_TOK) { 54519c72cacSJeff Kirsher dev->stats.tx_packets++; 54619c72cacSJeff Kirsher dev->stats.tx_bytes += 54719c72cacSJeff Kirsher lp->tx_skb[lp->tx_next_done]->len; 54819c72cacSJeff Kirsher } else { 54919c72cacSJeff Kirsher dev->stats.tx_errors++; 55019c72cacSJeff Kirsher dev->stats.tx_dropped++; 55119c72cacSJeff Kirsher 55219c72cacSJeff Kirsher /* Underflow */ 55319c72cacSJeff Kirsher if (devcs & ETH_TX_UND) 55419c72cacSJeff Kirsher dev->stats.tx_fifo_errors++; 55519c72cacSJeff Kirsher 55619c72cacSJeff Kirsher /* Oversized frame */ 55719c72cacSJeff Kirsher if (devcs & ETH_TX_OF) 55819c72cacSJeff Kirsher dev->stats.tx_aborted_errors++; 55919c72cacSJeff Kirsher 56019c72cacSJeff Kirsher /* Excessive deferrals */ 56119c72cacSJeff Kirsher if (devcs & ETH_TX_ED) 56219c72cacSJeff Kirsher dev->stats.tx_carrier_errors++; 56319c72cacSJeff Kirsher 56419c72cacSJeff Kirsher /* Collisions: medium busy */ 56519c72cacSJeff Kirsher if (devcs & ETH_TX_EC) 56619c72cacSJeff Kirsher dev->stats.collisions++; 56719c72cacSJeff Kirsher 56819c72cacSJeff Kirsher /* Late collision */ 56919c72cacSJeff Kirsher if (devcs & ETH_TX_LC) 57019c72cacSJeff Kirsher dev->stats.tx_window_errors++; 57119c72cacSJeff Kirsher } 57219c72cacSJeff Kirsher 57319c72cacSJeff Kirsher /* We must always free the original skb */ 57419c72cacSJeff Kirsher if (lp->tx_skb[lp->tx_next_done]) { 57519c72cacSJeff Kirsher dev_kfree_skb_any(lp->tx_skb[lp->tx_next_done]); 57619c72cacSJeff Kirsher lp->tx_skb[lp->tx_next_done] = NULL; 57719c72cacSJeff Kirsher } 57819c72cacSJeff Kirsher 57919c72cacSJeff Kirsher lp->td_ring[lp->tx_next_done].control = DMA_DESC_IOF; 58019c72cacSJeff Kirsher lp->td_ring[lp->tx_next_done].devcs = ETH_TX_FD | ETH_TX_LD; 58119c72cacSJeff Kirsher lp->td_ring[lp->tx_next_done].link = 0; 58219c72cacSJeff Kirsher lp->td_ring[lp->tx_next_done].ca = 0; 58319c72cacSJeff Kirsher lp->tx_count--; 58419c72cacSJeff Kirsher 58519c72cacSJeff Kirsher /* Go on to next transmission */ 58619c72cacSJeff Kirsher lp->tx_next_done = (lp->tx_next_done + 1) & KORINA_TDS_MASK; 58719c72cacSJeff Kirsher td = &lp->td_ring[lp->tx_next_done]; 58819c72cacSJeff Kirsher 58919c72cacSJeff Kirsher } 59019c72cacSJeff Kirsher 59119c72cacSJeff Kirsher /* Clear the DMA status register */ 59219c72cacSJeff Kirsher dmas = readl(&lp->tx_dma_regs->dmas); 59319c72cacSJeff Kirsher writel(~dmas, &lp->tx_dma_regs->dmas); 59419c72cacSJeff Kirsher 59519c72cacSJeff Kirsher writel(readl(&lp->tx_dma_regs->dmasm) & 59619c72cacSJeff Kirsher ~(DMA_STAT_FINI | DMA_STAT_ERR), 59719c72cacSJeff Kirsher &lp->tx_dma_regs->dmasm); 59819c72cacSJeff Kirsher 59919c72cacSJeff Kirsher spin_unlock(&lp->lock); 60019c72cacSJeff Kirsher } 60119c72cacSJeff Kirsher 60219c72cacSJeff Kirsher static irqreturn_t 60319c72cacSJeff Kirsher korina_tx_dma_interrupt(int irq, void *dev_id) 60419c72cacSJeff Kirsher { 60519c72cacSJeff Kirsher struct net_device *dev = dev_id; 60619c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 60719c72cacSJeff Kirsher u32 dmas, dmasm; 60819c72cacSJeff Kirsher irqreturn_t retval; 60919c72cacSJeff Kirsher 61019c72cacSJeff Kirsher dmas = readl(&lp->tx_dma_regs->dmas); 61119c72cacSJeff Kirsher 61219c72cacSJeff Kirsher if (dmas & (DMA_STAT_FINI | DMA_STAT_ERR)) { 61319c72cacSJeff Kirsher dmasm = readl(&lp->tx_dma_regs->dmasm); 61419c72cacSJeff Kirsher writel(dmasm | (DMA_STAT_FINI | DMA_STAT_ERR), 61519c72cacSJeff Kirsher &lp->tx_dma_regs->dmasm); 61619c72cacSJeff Kirsher 61719c72cacSJeff Kirsher korina_tx(dev); 61819c72cacSJeff Kirsher 61919c72cacSJeff Kirsher if (lp->tx_chain_status == desc_filled && 62019c72cacSJeff Kirsher (readl(&(lp->tx_dma_regs->dmandptr)) == 0)) { 62119c72cacSJeff Kirsher writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]), 62219c72cacSJeff Kirsher &(lp->tx_dma_regs->dmandptr)); 62319c72cacSJeff Kirsher lp->tx_chain_status = desc_empty; 62419c72cacSJeff Kirsher lp->tx_chain_head = lp->tx_chain_tail; 625860e9538SFlorian Westphal netif_trans_update(dev); 62619c72cacSJeff Kirsher } 62719c72cacSJeff Kirsher if (dmas & DMA_STAT_ERR) 62819c72cacSJeff Kirsher printk(KERN_ERR "%s: DMA error\n", dev->name); 62919c72cacSJeff Kirsher 63019c72cacSJeff Kirsher retval = IRQ_HANDLED; 63119c72cacSJeff Kirsher } else 63219c72cacSJeff Kirsher retval = IRQ_NONE; 63319c72cacSJeff Kirsher 63419c72cacSJeff Kirsher return retval; 63519c72cacSJeff Kirsher } 63619c72cacSJeff Kirsher 63719c72cacSJeff Kirsher 63819c72cacSJeff Kirsher static void korina_check_media(struct net_device *dev, unsigned int init_media) 63919c72cacSJeff Kirsher { 64019c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 64119c72cacSJeff Kirsher 64219c72cacSJeff Kirsher mii_check_media(&lp->mii_if, 0, init_media); 64319c72cacSJeff Kirsher 64419c72cacSJeff Kirsher if (lp->mii_if.full_duplex) 64519c72cacSJeff Kirsher writel(readl(&lp->eth_regs->ethmac2) | ETH_MAC2_FD, 64619c72cacSJeff Kirsher &lp->eth_regs->ethmac2); 64719c72cacSJeff Kirsher else 64819c72cacSJeff Kirsher writel(readl(&lp->eth_regs->ethmac2) & ~ETH_MAC2_FD, 64919c72cacSJeff Kirsher &lp->eth_regs->ethmac2); 65019c72cacSJeff Kirsher } 65119c72cacSJeff Kirsher 65219c72cacSJeff Kirsher static void korina_poll_media(unsigned long data) 65319c72cacSJeff Kirsher { 65419c72cacSJeff Kirsher struct net_device *dev = (struct net_device *) data; 65519c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 65619c72cacSJeff Kirsher 65719c72cacSJeff Kirsher korina_check_media(dev, 0); 65819c72cacSJeff Kirsher mod_timer(&lp->media_check_timer, jiffies + HZ); 65919c72cacSJeff Kirsher } 66019c72cacSJeff Kirsher 66119c72cacSJeff Kirsher static void korina_set_carrier(struct mii_if_info *mii) 66219c72cacSJeff Kirsher { 66319c72cacSJeff Kirsher if (mii->force_media) { 66419c72cacSJeff Kirsher /* autoneg is off: Link is always assumed to be up */ 66519c72cacSJeff Kirsher if (!netif_carrier_ok(mii->dev)) 66619c72cacSJeff Kirsher netif_carrier_on(mii->dev); 66719c72cacSJeff Kirsher } else /* Let MMI library update carrier status */ 66819c72cacSJeff Kirsher korina_check_media(mii->dev, 0); 66919c72cacSJeff Kirsher } 67019c72cacSJeff Kirsher 67119c72cacSJeff Kirsher static int korina_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) 67219c72cacSJeff Kirsher { 67319c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 67419c72cacSJeff Kirsher struct mii_ioctl_data *data = if_mii(rq); 67519c72cacSJeff Kirsher int rc; 67619c72cacSJeff Kirsher 67719c72cacSJeff Kirsher if (!netif_running(dev)) 67819c72cacSJeff Kirsher return -EINVAL; 67919c72cacSJeff Kirsher spin_lock_irq(&lp->lock); 68019c72cacSJeff Kirsher rc = generic_mii_ioctl(&lp->mii_if, data, cmd, NULL); 68119c72cacSJeff Kirsher spin_unlock_irq(&lp->lock); 68219c72cacSJeff Kirsher korina_set_carrier(&lp->mii_if); 68319c72cacSJeff Kirsher 68419c72cacSJeff Kirsher return rc; 68519c72cacSJeff Kirsher } 68619c72cacSJeff Kirsher 68719c72cacSJeff Kirsher /* ethtool helpers */ 68819c72cacSJeff Kirsher static void netdev_get_drvinfo(struct net_device *dev, 68919c72cacSJeff Kirsher struct ethtool_drvinfo *info) 69019c72cacSJeff Kirsher { 69119c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 69219c72cacSJeff Kirsher 6937826d43fSJiri Pirko strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); 6947826d43fSJiri Pirko strlcpy(info->version, DRV_VERSION, sizeof(info->version)); 6957826d43fSJiri Pirko strlcpy(info->bus_info, lp->dev->name, sizeof(info->bus_info)); 69619c72cacSJeff Kirsher } 69719c72cacSJeff Kirsher 698af473688SPhilippe Reynes static int netdev_get_link_ksettings(struct net_device *dev, 699af473688SPhilippe Reynes struct ethtool_link_ksettings *cmd) 70019c72cacSJeff Kirsher { 70119c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 70219c72cacSJeff Kirsher 70319c72cacSJeff Kirsher spin_lock_irq(&lp->lock); 704*82c01a84Syuval.shaia@oracle.com mii_ethtool_get_link_ksettings(&lp->mii_if, cmd); 70519c72cacSJeff Kirsher spin_unlock_irq(&lp->lock); 70619c72cacSJeff Kirsher 707*82c01a84Syuval.shaia@oracle.com return 0; 70819c72cacSJeff Kirsher } 70919c72cacSJeff Kirsher 710af473688SPhilippe Reynes static int netdev_set_link_ksettings(struct net_device *dev, 711af473688SPhilippe Reynes const struct ethtool_link_ksettings *cmd) 71219c72cacSJeff Kirsher { 71319c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 71419c72cacSJeff Kirsher int rc; 71519c72cacSJeff Kirsher 71619c72cacSJeff Kirsher spin_lock_irq(&lp->lock); 717af473688SPhilippe Reynes rc = mii_ethtool_set_link_ksettings(&lp->mii_if, cmd); 71819c72cacSJeff Kirsher spin_unlock_irq(&lp->lock); 71919c72cacSJeff Kirsher korina_set_carrier(&lp->mii_if); 72019c72cacSJeff Kirsher 72119c72cacSJeff Kirsher return rc; 72219c72cacSJeff Kirsher } 72319c72cacSJeff Kirsher 72419c72cacSJeff Kirsher static u32 netdev_get_link(struct net_device *dev) 72519c72cacSJeff Kirsher { 72619c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 72719c72cacSJeff Kirsher 72819c72cacSJeff Kirsher return mii_link_ok(&lp->mii_if); 72919c72cacSJeff Kirsher } 73019c72cacSJeff Kirsher 73119c72cacSJeff Kirsher static const struct ethtool_ops netdev_ethtool_ops = { 73219c72cacSJeff Kirsher .get_drvinfo = netdev_get_drvinfo, 73319c72cacSJeff Kirsher .get_link = netdev_get_link, 734af473688SPhilippe Reynes .get_link_ksettings = netdev_get_link_ksettings, 735af473688SPhilippe Reynes .set_link_ksettings = netdev_set_link_ksettings, 73619c72cacSJeff Kirsher }; 73719c72cacSJeff Kirsher 73819c72cacSJeff Kirsher static int korina_alloc_ring(struct net_device *dev) 73919c72cacSJeff Kirsher { 74019c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 74119c72cacSJeff Kirsher struct sk_buff *skb; 74219c72cacSJeff Kirsher int i; 74319c72cacSJeff Kirsher 74419c72cacSJeff Kirsher /* Initialize the transmit descriptors */ 74519c72cacSJeff Kirsher for (i = 0; i < KORINA_NUM_TDS; i++) { 74619c72cacSJeff Kirsher lp->td_ring[i].control = DMA_DESC_IOF; 74719c72cacSJeff Kirsher lp->td_ring[i].devcs = ETH_TX_FD | ETH_TX_LD; 74819c72cacSJeff Kirsher lp->td_ring[i].ca = 0; 74919c72cacSJeff Kirsher lp->td_ring[i].link = 0; 75019c72cacSJeff Kirsher } 75119c72cacSJeff Kirsher lp->tx_next_done = lp->tx_chain_head = lp->tx_chain_tail = 75219c72cacSJeff Kirsher lp->tx_full = lp->tx_count = 0; 75319c72cacSJeff Kirsher lp->tx_chain_status = desc_empty; 75419c72cacSJeff Kirsher 75519c72cacSJeff Kirsher /* Initialize the receive descriptors */ 75619c72cacSJeff Kirsher for (i = 0; i < KORINA_NUM_RDS; i++) { 75719c72cacSJeff Kirsher skb = netdev_alloc_skb_ip_align(dev, KORINA_RBSIZE); 75819c72cacSJeff Kirsher if (!skb) 75919c72cacSJeff Kirsher return -ENOMEM; 76019c72cacSJeff Kirsher lp->rx_skb[i] = skb; 76119c72cacSJeff Kirsher lp->rd_ring[i].control = DMA_DESC_IOD | 76219c72cacSJeff Kirsher DMA_COUNT(KORINA_RBSIZE); 76319c72cacSJeff Kirsher lp->rd_ring[i].devcs = 0; 76419c72cacSJeff Kirsher lp->rd_ring[i].ca = CPHYSADDR(skb->data); 76519c72cacSJeff Kirsher lp->rd_ring[i].link = CPHYSADDR(&lp->rd_ring[i+1]); 76619c72cacSJeff Kirsher } 76719c72cacSJeff Kirsher 76819c72cacSJeff Kirsher /* loop back receive descriptors, so the last 76919c72cacSJeff Kirsher * descriptor points to the first one */ 77019c72cacSJeff Kirsher lp->rd_ring[i - 1].link = CPHYSADDR(&lp->rd_ring[0]); 77119c72cacSJeff Kirsher lp->rd_ring[i - 1].control |= DMA_DESC_COD; 77219c72cacSJeff Kirsher 77319c72cacSJeff Kirsher lp->rx_next_done = 0; 77419c72cacSJeff Kirsher lp->rx_chain_head = 0; 77519c72cacSJeff Kirsher lp->rx_chain_tail = 0; 77619c72cacSJeff Kirsher lp->rx_chain_status = desc_empty; 77719c72cacSJeff Kirsher 77819c72cacSJeff Kirsher return 0; 77919c72cacSJeff Kirsher } 78019c72cacSJeff Kirsher 78119c72cacSJeff Kirsher static void korina_free_ring(struct net_device *dev) 78219c72cacSJeff Kirsher { 78319c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 78419c72cacSJeff Kirsher int i; 78519c72cacSJeff Kirsher 78619c72cacSJeff Kirsher for (i = 0; i < KORINA_NUM_RDS; i++) { 78719c72cacSJeff Kirsher lp->rd_ring[i].control = 0; 78819c72cacSJeff Kirsher if (lp->rx_skb[i]) 78919c72cacSJeff Kirsher dev_kfree_skb_any(lp->rx_skb[i]); 79019c72cacSJeff Kirsher lp->rx_skb[i] = NULL; 79119c72cacSJeff Kirsher } 79219c72cacSJeff Kirsher 79319c72cacSJeff Kirsher for (i = 0; i < KORINA_NUM_TDS; i++) { 79419c72cacSJeff Kirsher lp->td_ring[i].control = 0; 79519c72cacSJeff Kirsher if (lp->tx_skb[i]) 79619c72cacSJeff Kirsher dev_kfree_skb_any(lp->tx_skb[i]); 79719c72cacSJeff Kirsher lp->tx_skb[i] = NULL; 79819c72cacSJeff Kirsher } 79919c72cacSJeff Kirsher } 80019c72cacSJeff Kirsher 80119c72cacSJeff Kirsher /* 80219c72cacSJeff Kirsher * Initialize the RC32434 ethernet controller. 80319c72cacSJeff Kirsher */ 80419c72cacSJeff Kirsher static int korina_init(struct net_device *dev) 80519c72cacSJeff Kirsher { 80619c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 80719c72cacSJeff Kirsher 80819c72cacSJeff Kirsher /* Disable DMA */ 80919c72cacSJeff Kirsher korina_abort_tx(dev); 81019c72cacSJeff Kirsher korina_abort_rx(dev); 81119c72cacSJeff Kirsher 81219c72cacSJeff Kirsher /* reset ethernet logic */ 81319c72cacSJeff Kirsher writel(0, &lp->eth_regs->ethintfc); 81419c72cacSJeff Kirsher while ((readl(&lp->eth_regs->ethintfc) & ETH_INT_FC_RIP)) 815860e9538SFlorian Westphal netif_trans_update(dev); 81619c72cacSJeff Kirsher 81719c72cacSJeff Kirsher /* Enable Ethernet Interface */ 81819c72cacSJeff Kirsher writel(ETH_INT_FC_EN, &lp->eth_regs->ethintfc); 81919c72cacSJeff Kirsher 82019c72cacSJeff Kirsher /* Allocate rings */ 82119c72cacSJeff Kirsher if (korina_alloc_ring(dev)) { 82219c72cacSJeff Kirsher printk(KERN_ERR "%s: descriptor allocation failed\n", dev->name); 82319c72cacSJeff Kirsher korina_free_ring(dev); 82419c72cacSJeff Kirsher return -ENOMEM; 82519c72cacSJeff Kirsher } 82619c72cacSJeff Kirsher 82719c72cacSJeff Kirsher writel(0, &lp->rx_dma_regs->dmas); 82819c72cacSJeff Kirsher /* Start Rx DMA */ 82919c72cacSJeff Kirsher korina_start_rx(lp, &lp->rd_ring[0]); 83019c72cacSJeff Kirsher 83119c72cacSJeff Kirsher writel(readl(&lp->tx_dma_regs->dmasm) & 83219c72cacSJeff Kirsher ~(DMA_STAT_FINI | DMA_STAT_ERR), 83319c72cacSJeff Kirsher &lp->tx_dma_regs->dmasm); 83419c72cacSJeff Kirsher writel(readl(&lp->rx_dma_regs->dmasm) & 83519c72cacSJeff Kirsher ~(DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR), 83619c72cacSJeff Kirsher &lp->rx_dma_regs->dmasm); 83719c72cacSJeff Kirsher 83819c72cacSJeff Kirsher /* Accept only packets destined for this Ethernet device address */ 83919c72cacSJeff Kirsher writel(ETH_ARC_AB, &lp->eth_regs->etharc); 84019c72cacSJeff Kirsher 84119c72cacSJeff Kirsher /* Set all Ether station address registers to their initial values */ 84219c72cacSJeff Kirsher writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal0); 84319c72cacSJeff Kirsher writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah0); 84419c72cacSJeff Kirsher 84519c72cacSJeff Kirsher writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal1); 84619c72cacSJeff Kirsher writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah1); 84719c72cacSJeff Kirsher 84819c72cacSJeff Kirsher writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal2); 84919c72cacSJeff Kirsher writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah2); 85019c72cacSJeff Kirsher 85119c72cacSJeff Kirsher writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal3); 85219c72cacSJeff Kirsher writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah3); 85319c72cacSJeff Kirsher 85419c72cacSJeff Kirsher 85519c72cacSJeff Kirsher /* Frame Length Checking, Pad Enable, CRC Enable, Full Duplex set */ 85619c72cacSJeff Kirsher writel(ETH_MAC2_PE | ETH_MAC2_CEN | ETH_MAC2_FD, 85719c72cacSJeff Kirsher &lp->eth_regs->ethmac2); 85819c72cacSJeff Kirsher 85919c72cacSJeff Kirsher /* Back to back inter-packet-gap */ 86019c72cacSJeff Kirsher writel(0x15, &lp->eth_regs->ethipgt); 86119c72cacSJeff Kirsher /* Non - Back to back inter-packet-gap */ 86219c72cacSJeff Kirsher writel(0x12, &lp->eth_regs->ethipgr); 86319c72cacSJeff Kirsher 86419c72cacSJeff Kirsher /* Management Clock Prescaler Divisor 86519c72cacSJeff Kirsher * Clock independent setting */ 86619c72cacSJeff Kirsher writel(((idt_cpu_freq) / MII_CLOCK + 1) & ~1, 86719c72cacSJeff Kirsher &lp->eth_regs->ethmcp); 86819c72cacSJeff Kirsher 86919c72cacSJeff Kirsher /* don't transmit until fifo contains 48b */ 87019c72cacSJeff Kirsher writel(48, &lp->eth_regs->ethfifott); 87119c72cacSJeff Kirsher 87219c72cacSJeff Kirsher writel(ETH_MAC1_RE, &lp->eth_regs->ethmac1); 87319c72cacSJeff Kirsher 87419c72cacSJeff Kirsher napi_enable(&lp->napi); 87519c72cacSJeff Kirsher netif_start_queue(dev); 87619c72cacSJeff Kirsher 87719c72cacSJeff Kirsher return 0; 87819c72cacSJeff Kirsher } 87919c72cacSJeff Kirsher 88019c72cacSJeff Kirsher /* 88119c72cacSJeff Kirsher * Restart the RC32434 ethernet controller. 88219c72cacSJeff Kirsher */ 88319c72cacSJeff Kirsher static void korina_restart_task(struct work_struct *work) 88419c72cacSJeff Kirsher { 88519c72cacSJeff Kirsher struct korina_private *lp = container_of(work, 88619c72cacSJeff Kirsher struct korina_private, restart_task); 88719c72cacSJeff Kirsher struct net_device *dev = lp->dev; 88819c72cacSJeff Kirsher 88919c72cacSJeff Kirsher /* 89019c72cacSJeff Kirsher * Disable interrupts 89119c72cacSJeff Kirsher */ 89219c72cacSJeff Kirsher disable_irq(lp->rx_irq); 89319c72cacSJeff Kirsher disable_irq(lp->tx_irq); 89419c72cacSJeff Kirsher disable_irq(lp->ovr_irq); 89519c72cacSJeff Kirsher disable_irq(lp->und_irq); 89619c72cacSJeff Kirsher 89719c72cacSJeff Kirsher writel(readl(&lp->tx_dma_regs->dmasm) | 89819c72cacSJeff Kirsher DMA_STAT_FINI | DMA_STAT_ERR, 89919c72cacSJeff Kirsher &lp->tx_dma_regs->dmasm); 90019c72cacSJeff Kirsher writel(readl(&lp->rx_dma_regs->dmasm) | 90119c72cacSJeff Kirsher DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR, 90219c72cacSJeff Kirsher &lp->rx_dma_regs->dmasm); 90319c72cacSJeff Kirsher 90419c72cacSJeff Kirsher napi_disable(&lp->napi); 90519c72cacSJeff Kirsher 906e6afb1adSFlorian Fainelli korina_free_ring(dev); 907e6afb1adSFlorian Fainelli 90819c72cacSJeff Kirsher if (korina_init(dev) < 0) { 90919c72cacSJeff Kirsher printk(KERN_ERR "%s: cannot restart device\n", dev->name); 91019c72cacSJeff Kirsher return; 91119c72cacSJeff Kirsher } 91219c72cacSJeff Kirsher korina_multicast_list(dev); 91319c72cacSJeff Kirsher 91419c72cacSJeff Kirsher enable_irq(lp->und_irq); 91519c72cacSJeff Kirsher enable_irq(lp->ovr_irq); 91619c72cacSJeff Kirsher enable_irq(lp->tx_irq); 91719c72cacSJeff Kirsher enable_irq(lp->rx_irq); 91819c72cacSJeff Kirsher } 91919c72cacSJeff Kirsher 92019c72cacSJeff Kirsher static void korina_clear_and_restart(struct net_device *dev, u32 value) 92119c72cacSJeff Kirsher { 92219c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 92319c72cacSJeff Kirsher 92419c72cacSJeff Kirsher netif_stop_queue(dev); 92519c72cacSJeff Kirsher writel(value, &lp->eth_regs->ethintfc); 92619c72cacSJeff Kirsher schedule_work(&lp->restart_task); 92719c72cacSJeff Kirsher } 92819c72cacSJeff Kirsher 92919c72cacSJeff Kirsher /* Ethernet Tx Underflow interrupt */ 93019c72cacSJeff Kirsher static irqreturn_t korina_und_interrupt(int irq, void *dev_id) 93119c72cacSJeff Kirsher { 93219c72cacSJeff Kirsher struct net_device *dev = dev_id; 93319c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 93419c72cacSJeff Kirsher unsigned int und; 93519c72cacSJeff Kirsher 93619c72cacSJeff Kirsher spin_lock(&lp->lock); 93719c72cacSJeff Kirsher 93819c72cacSJeff Kirsher und = readl(&lp->eth_regs->ethintfc); 93919c72cacSJeff Kirsher 94019c72cacSJeff Kirsher if (und & ETH_INT_FC_UND) 94119c72cacSJeff Kirsher korina_clear_and_restart(dev, und & ~ETH_INT_FC_UND); 94219c72cacSJeff Kirsher 94319c72cacSJeff Kirsher spin_unlock(&lp->lock); 94419c72cacSJeff Kirsher 94519c72cacSJeff Kirsher return IRQ_HANDLED; 94619c72cacSJeff Kirsher } 94719c72cacSJeff Kirsher 94819c72cacSJeff Kirsher static void korina_tx_timeout(struct net_device *dev) 94919c72cacSJeff Kirsher { 95019c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 95119c72cacSJeff Kirsher 95219c72cacSJeff Kirsher schedule_work(&lp->restart_task); 95319c72cacSJeff Kirsher } 95419c72cacSJeff Kirsher 95519c72cacSJeff Kirsher /* Ethernet Rx Overflow interrupt */ 95619c72cacSJeff Kirsher static irqreturn_t 95719c72cacSJeff Kirsher korina_ovr_interrupt(int irq, void *dev_id) 95819c72cacSJeff Kirsher { 95919c72cacSJeff Kirsher struct net_device *dev = dev_id; 96019c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 96119c72cacSJeff Kirsher unsigned int ovr; 96219c72cacSJeff Kirsher 96319c72cacSJeff Kirsher spin_lock(&lp->lock); 96419c72cacSJeff Kirsher ovr = readl(&lp->eth_regs->ethintfc); 96519c72cacSJeff Kirsher 96619c72cacSJeff Kirsher if (ovr & ETH_INT_FC_OVR) 96719c72cacSJeff Kirsher korina_clear_and_restart(dev, ovr & ~ETH_INT_FC_OVR); 96819c72cacSJeff Kirsher 96919c72cacSJeff Kirsher spin_unlock(&lp->lock); 97019c72cacSJeff Kirsher 97119c72cacSJeff Kirsher return IRQ_HANDLED; 97219c72cacSJeff Kirsher } 97319c72cacSJeff Kirsher 97419c72cacSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER 97519c72cacSJeff Kirsher static void korina_poll_controller(struct net_device *dev) 97619c72cacSJeff Kirsher { 97719c72cacSJeff Kirsher disable_irq(dev->irq); 97819c72cacSJeff Kirsher korina_tx_dma_interrupt(dev->irq, dev); 97919c72cacSJeff Kirsher enable_irq(dev->irq); 98019c72cacSJeff Kirsher } 98119c72cacSJeff Kirsher #endif 98219c72cacSJeff Kirsher 98319c72cacSJeff Kirsher static int korina_open(struct net_device *dev) 98419c72cacSJeff Kirsher { 98519c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 98619c72cacSJeff Kirsher int ret; 98719c72cacSJeff Kirsher 98819c72cacSJeff Kirsher /* Initialize */ 98919c72cacSJeff Kirsher ret = korina_init(dev); 99019c72cacSJeff Kirsher if (ret < 0) { 99119c72cacSJeff Kirsher printk(KERN_ERR "%s: cannot open device\n", dev->name); 99219c72cacSJeff Kirsher goto out; 99319c72cacSJeff Kirsher } 99419c72cacSJeff Kirsher 99519c72cacSJeff Kirsher /* Install the interrupt handler 99619c72cacSJeff Kirsher * that handles the Done Finished 99719c72cacSJeff Kirsher * Ovr and Und Events */ 99819c72cacSJeff Kirsher ret = request_irq(lp->rx_irq, korina_rx_dma_interrupt, 9992414fe16SMichael Opdenacker 0, "Korina ethernet Rx", dev); 100019c72cacSJeff Kirsher if (ret < 0) { 100119c72cacSJeff Kirsher printk(KERN_ERR "%s: unable to get Rx DMA IRQ %d\n", 100219c72cacSJeff Kirsher dev->name, lp->rx_irq); 100319c72cacSJeff Kirsher goto err_release; 100419c72cacSJeff Kirsher } 100519c72cacSJeff Kirsher ret = request_irq(lp->tx_irq, korina_tx_dma_interrupt, 10062414fe16SMichael Opdenacker 0, "Korina ethernet Tx", dev); 100719c72cacSJeff Kirsher if (ret < 0) { 100819c72cacSJeff Kirsher printk(KERN_ERR "%s: unable to get Tx DMA IRQ %d\n", 100919c72cacSJeff Kirsher dev->name, lp->tx_irq); 101019c72cacSJeff Kirsher goto err_free_rx_irq; 101119c72cacSJeff Kirsher } 101219c72cacSJeff Kirsher 101319c72cacSJeff Kirsher /* Install handler for overrun error. */ 101419c72cacSJeff Kirsher ret = request_irq(lp->ovr_irq, korina_ovr_interrupt, 10152414fe16SMichael Opdenacker 0, "Ethernet Overflow", dev); 101619c72cacSJeff Kirsher if (ret < 0) { 101719c72cacSJeff Kirsher printk(KERN_ERR "%s: unable to get OVR IRQ %d\n", 101819c72cacSJeff Kirsher dev->name, lp->ovr_irq); 101919c72cacSJeff Kirsher goto err_free_tx_irq; 102019c72cacSJeff Kirsher } 102119c72cacSJeff Kirsher 102219c72cacSJeff Kirsher /* Install handler for underflow error. */ 102319c72cacSJeff Kirsher ret = request_irq(lp->und_irq, korina_und_interrupt, 10242414fe16SMichael Opdenacker 0, "Ethernet Underflow", dev); 102519c72cacSJeff Kirsher if (ret < 0) { 102619c72cacSJeff Kirsher printk(KERN_ERR "%s: unable to get UND IRQ %d\n", 102719c72cacSJeff Kirsher dev->name, lp->und_irq); 102819c72cacSJeff Kirsher goto err_free_ovr_irq; 102919c72cacSJeff Kirsher } 103019c72cacSJeff Kirsher mod_timer(&lp->media_check_timer, jiffies + 1); 103119c72cacSJeff Kirsher out: 103219c72cacSJeff Kirsher return ret; 103319c72cacSJeff Kirsher 103419c72cacSJeff Kirsher err_free_ovr_irq: 103519c72cacSJeff Kirsher free_irq(lp->ovr_irq, dev); 103619c72cacSJeff Kirsher err_free_tx_irq: 103719c72cacSJeff Kirsher free_irq(lp->tx_irq, dev); 103819c72cacSJeff Kirsher err_free_rx_irq: 103919c72cacSJeff Kirsher free_irq(lp->rx_irq, dev); 104019c72cacSJeff Kirsher err_release: 104119c72cacSJeff Kirsher korina_free_ring(dev); 104219c72cacSJeff Kirsher goto out; 104319c72cacSJeff Kirsher } 104419c72cacSJeff Kirsher 104519c72cacSJeff Kirsher static int korina_close(struct net_device *dev) 104619c72cacSJeff Kirsher { 104719c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(dev); 104819c72cacSJeff Kirsher u32 tmp; 104919c72cacSJeff Kirsher 105019c72cacSJeff Kirsher del_timer(&lp->media_check_timer); 105119c72cacSJeff Kirsher 105219c72cacSJeff Kirsher /* Disable interrupts */ 105319c72cacSJeff Kirsher disable_irq(lp->rx_irq); 105419c72cacSJeff Kirsher disable_irq(lp->tx_irq); 105519c72cacSJeff Kirsher disable_irq(lp->ovr_irq); 105619c72cacSJeff Kirsher disable_irq(lp->und_irq); 105719c72cacSJeff Kirsher 105819c72cacSJeff Kirsher korina_abort_tx(dev); 105919c72cacSJeff Kirsher tmp = readl(&lp->tx_dma_regs->dmasm); 106019c72cacSJeff Kirsher tmp = tmp | DMA_STAT_FINI | DMA_STAT_ERR; 106119c72cacSJeff Kirsher writel(tmp, &lp->tx_dma_regs->dmasm); 106219c72cacSJeff Kirsher 106319c72cacSJeff Kirsher korina_abort_rx(dev); 106419c72cacSJeff Kirsher tmp = readl(&lp->rx_dma_regs->dmasm); 106519c72cacSJeff Kirsher tmp = tmp | DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR; 106619c72cacSJeff Kirsher writel(tmp, &lp->rx_dma_regs->dmasm); 106719c72cacSJeff Kirsher 106819c72cacSJeff Kirsher napi_disable(&lp->napi); 106919c72cacSJeff Kirsher 107019c72cacSJeff Kirsher cancel_work_sync(&lp->restart_task); 107119c72cacSJeff Kirsher 1072e6afb1adSFlorian Fainelli korina_free_ring(dev); 1073e6afb1adSFlorian Fainelli 107419c72cacSJeff Kirsher free_irq(lp->rx_irq, dev); 107519c72cacSJeff Kirsher free_irq(lp->tx_irq, dev); 107619c72cacSJeff Kirsher free_irq(lp->ovr_irq, dev); 107719c72cacSJeff Kirsher free_irq(lp->und_irq, dev); 107819c72cacSJeff Kirsher 107919c72cacSJeff Kirsher return 0; 108019c72cacSJeff Kirsher } 108119c72cacSJeff Kirsher 108219c72cacSJeff Kirsher static const struct net_device_ops korina_netdev_ops = { 108319c72cacSJeff Kirsher .ndo_open = korina_open, 108419c72cacSJeff Kirsher .ndo_stop = korina_close, 108519c72cacSJeff Kirsher .ndo_start_xmit = korina_send_packet, 1086afc4b13dSJiri Pirko .ndo_set_rx_mode = korina_multicast_list, 108719c72cacSJeff Kirsher .ndo_tx_timeout = korina_tx_timeout, 108819c72cacSJeff Kirsher .ndo_do_ioctl = korina_ioctl, 108919c72cacSJeff Kirsher .ndo_validate_addr = eth_validate_addr, 109019c72cacSJeff Kirsher .ndo_set_mac_address = eth_mac_addr, 109119c72cacSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER 109219c72cacSJeff Kirsher .ndo_poll_controller = korina_poll_controller, 109319c72cacSJeff Kirsher #endif 109419c72cacSJeff Kirsher }; 109519c72cacSJeff Kirsher 109619c72cacSJeff Kirsher static int korina_probe(struct platform_device *pdev) 109719c72cacSJeff Kirsher { 109819c72cacSJeff Kirsher struct korina_device *bif = platform_get_drvdata(pdev); 109919c72cacSJeff Kirsher struct korina_private *lp; 110019c72cacSJeff Kirsher struct net_device *dev; 110119c72cacSJeff Kirsher struct resource *r; 110219c72cacSJeff Kirsher int rc; 110319c72cacSJeff Kirsher 110419c72cacSJeff Kirsher dev = alloc_etherdev(sizeof(struct korina_private)); 110541de8d4cSJoe Perches if (!dev) 110619c72cacSJeff Kirsher return -ENOMEM; 110741de8d4cSJoe Perches 110819c72cacSJeff Kirsher SET_NETDEV_DEV(dev, &pdev->dev); 110919c72cacSJeff Kirsher lp = netdev_priv(dev); 111019c72cacSJeff Kirsher 111119c72cacSJeff Kirsher bif->dev = dev; 1112d458cdf7SJoe Perches memcpy(dev->dev_addr, bif->mac, ETH_ALEN); 111319c72cacSJeff Kirsher 111419c72cacSJeff Kirsher lp->rx_irq = platform_get_irq_byname(pdev, "korina_rx"); 111519c72cacSJeff Kirsher lp->tx_irq = platform_get_irq_byname(pdev, "korina_tx"); 111619c72cacSJeff Kirsher lp->ovr_irq = platform_get_irq_byname(pdev, "korina_ovr"); 111719c72cacSJeff Kirsher lp->und_irq = platform_get_irq_byname(pdev, "korina_und"); 111819c72cacSJeff Kirsher 111919c72cacSJeff Kirsher r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_regs"); 112019c72cacSJeff Kirsher dev->base_addr = r->start; 112119c72cacSJeff Kirsher lp->eth_regs = ioremap_nocache(r->start, resource_size(r)); 112219c72cacSJeff Kirsher if (!lp->eth_regs) { 112319c72cacSJeff Kirsher printk(KERN_ERR DRV_NAME ": cannot remap registers\n"); 112419c72cacSJeff Kirsher rc = -ENXIO; 112519c72cacSJeff Kirsher goto probe_err_out; 112619c72cacSJeff Kirsher } 112719c72cacSJeff Kirsher 112819c72cacSJeff Kirsher r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_dma_rx"); 112919c72cacSJeff Kirsher lp->rx_dma_regs = ioremap_nocache(r->start, resource_size(r)); 113019c72cacSJeff Kirsher if (!lp->rx_dma_regs) { 113119c72cacSJeff Kirsher printk(KERN_ERR DRV_NAME ": cannot remap Rx DMA registers\n"); 113219c72cacSJeff Kirsher rc = -ENXIO; 113319c72cacSJeff Kirsher goto probe_err_dma_rx; 113419c72cacSJeff Kirsher } 113519c72cacSJeff Kirsher 113619c72cacSJeff Kirsher r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_dma_tx"); 113719c72cacSJeff Kirsher lp->tx_dma_regs = ioremap_nocache(r->start, resource_size(r)); 113819c72cacSJeff Kirsher if (!lp->tx_dma_regs) { 113919c72cacSJeff Kirsher printk(KERN_ERR DRV_NAME ": cannot remap Tx DMA registers\n"); 114019c72cacSJeff Kirsher rc = -ENXIO; 114119c72cacSJeff Kirsher goto probe_err_dma_tx; 114219c72cacSJeff Kirsher } 114319c72cacSJeff Kirsher 114419c72cacSJeff Kirsher lp->td_ring = kmalloc(TD_RING_SIZE + RD_RING_SIZE, GFP_KERNEL); 114519c72cacSJeff Kirsher if (!lp->td_ring) { 114619c72cacSJeff Kirsher rc = -ENXIO; 114719c72cacSJeff Kirsher goto probe_err_td_ring; 114819c72cacSJeff Kirsher } 114919c72cacSJeff Kirsher 115019c72cacSJeff Kirsher dma_cache_inv((unsigned long)(lp->td_ring), 115119c72cacSJeff Kirsher TD_RING_SIZE + RD_RING_SIZE); 115219c72cacSJeff Kirsher 115319c72cacSJeff Kirsher /* now convert TD_RING pointer to KSEG1 */ 115419c72cacSJeff Kirsher lp->td_ring = (struct dma_desc *)KSEG1ADDR(lp->td_ring); 115519c72cacSJeff Kirsher lp->rd_ring = &lp->td_ring[KORINA_NUM_TDS]; 115619c72cacSJeff Kirsher 115719c72cacSJeff Kirsher spin_lock_init(&lp->lock); 115819c72cacSJeff Kirsher /* just use the rx dma irq */ 115919c72cacSJeff Kirsher dev->irq = lp->rx_irq; 116019c72cacSJeff Kirsher lp->dev = dev; 116119c72cacSJeff Kirsher 116219c72cacSJeff Kirsher dev->netdev_ops = &korina_netdev_ops; 116319c72cacSJeff Kirsher dev->ethtool_ops = &netdev_ethtool_ops; 116419c72cacSJeff Kirsher dev->watchdog_timeo = TX_TIMEOUT; 116519c72cacSJeff Kirsher netif_napi_add(dev, &lp->napi, korina_poll, 64); 116619c72cacSJeff Kirsher 116719c72cacSJeff Kirsher lp->phy_addr = (((lp->rx_irq == 0x2c? 1:0) << 8) | 0x05); 116819c72cacSJeff Kirsher lp->mii_if.dev = dev; 116919c72cacSJeff Kirsher lp->mii_if.mdio_read = mdio_read; 117019c72cacSJeff Kirsher lp->mii_if.mdio_write = mdio_write; 117119c72cacSJeff Kirsher lp->mii_if.phy_id = lp->phy_addr; 117219c72cacSJeff Kirsher lp->mii_if.phy_id_mask = 0x1f; 117319c72cacSJeff Kirsher lp->mii_if.reg_num_mask = 0x1f; 117419c72cacSJeff Kirsher 117519c72cacSJeff Kirsher rc = register_netdev(dev); 117619c72cacSJeff Kirsher if (rc < 0) { 117719c72cacSJeff Kirsher printk(KERN_ERR DRV_NAME 117819c72cacSJeff Kirsher ": cannot register net device: %d\n", rc); 117919c72cacSJeff Kirsher goto probe_err_register; 118019c72cacSJeff Kirsher } 118119c72cacSJeff Kirsher setup_timer(&lp->media_check_timer, korina_poll_media, (unsigned long) dev); 118219c72cacSJeff Kirsher 118319c72cacSJeff Kirsher INIT_WORK(&lp->restart_task, korina_restart_task); 118419c72cacSJeff Kirsher 118519c72cacSJeff Kirsher printk(KERN_INFO "%s: " DRV_NAME "-" DRV_VERSION " " DRV_RELDATE "\n", 118619c72cacSJeff Kirsher dev->name); 118719c72cacSJeff Kirsher out: 118819c72cacSJeff Kirsher return rc; 118919c72cacSJeff Kirsher 119019c72cacSJeff Kirsher probe_err_register: 119119c72cacSJeff Kirsher kfree(lp->td_ring); 119219c72cacSJeff Kirsher probe_err_td_ring: 119319c72cacSJeff Kirsher iounmap(lp->tx_dma_regs); 119419c72cacSJeff Kirsher probe_err_dma_tx: 119519c72cacSJeff Kirsher iounmap(lp->rx_dma_regs); 119619c72cacSJeff Kirsher probe_err_dma_rx: 119719c72cacSJeff Kirsher iounmap(lp->eth_regs); 119819c72cacSJeff Kirsher probe_err_out: 119919c72cacSJeff Kirsher free_netdev(dev); 120019c72cacSJeff Kirsher goto out; 120119c72cacSJeff Kirsher } 120219c72cacSJeff Kirsher 120319c72cacSJeff Kirsher static int korina_remove(struct platform_device *pdev) 120419c72cacSJeff Kirsher { 120519c72cacSJeff Kirsher struct korina_device *bif = platform_get_drvdata(pdev); 120619c72cacSJeff Kirsher struct korina_private *lp = netdev_priv(bif->dev); 120719c72cacSJeff Kirsher 120819c72cacSJeff Kirsher iounmap(lp->eth_regs); 120919c72cacSJeff Kirsher iounmap(lp->rx_dma_regs); 121019c72cacSJeff Kirsher iounmap(lp->tx_dma_regs); 121119c72cacSJeff Kirsher 121219c72cacSJeff Kirsher unregister_netdev(bif->dev); 121319c72cacSJeff Kirsher free_netdev(bif->dev); 121419c72cacSJeff Kirsher 121519c72cacSJeff Kirsher return 0; 121619c72cacSJeff Kirsher } 121719c72cacSJeff Kirsher 121819c72cacSJeff Kirsher static struct platform_driver korina_driver = { 121919c72cacSJeff Kirsher .driver.name = "korina", 122019c72cacSJeff Kirsher .probe = korina_probe, 122119c72cacSJeff Kirsher .remove = korina_remove, 122219c72cacSJeff Kirsher }; 122319c72cacSJeff Kirsher 1224db62f684SAxel Lin module_platform_driver(korina_driver); 122519c72cacSJeff Kirsher 122619c72cacSJeff Kirsher MODULE_AUTHOR("Philip Rischel <rischelp@idt.com>"); 122719c72cacSJeff Kirsher MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>"); 122819c72cacSJeff Kirsher MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>"); 122919c72cacSJeff Kirsher MODULE_DESCRIPTION("IDT RC32434 (Korina) Ethernet driver"); 123019c72cacSJeff Kirsher MODULE_LICENSE("GPL"); 1231