Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1 | /* |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 2 | * Cadence MACB/GEM Ethernet Controller driver |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2004-2006 Atmel Corporation |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 12 | #include <linux/clk.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/moduleparam.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/types.h> |
Nicolas Ferre | 909a858 | 2012-11-19 06:00:21 +0000 | [diff] [blame] | 17 | #include <linux/circ_buf.h> |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 18 | #include <linux/slab.h> |
| 19 | #include <linux/init.h> |
Joachim Eastwood | 2dbfdbb9 | 2012-11-11 13:56:27 +0000 | [diff] [blame] | 20 | #include <linux/gpio.h> |
Alexey Dobriyan | a6b7a40 | 2011-06-06 10:43:46 +0000 | [diff] [blame] | 21 | #include <linux/interrupt.h> |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 22 | #include <linux/netdevice.h> |
| 23 | #include <linux/etherdevice.h> |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 24 | #include <linux/dma-mapping.h> |
Jamie Iles | 84e0cdb | 2011-03-08 20:17:06 +0000 | [diff] [blame] | 25 | #include <linux/platform_data/macb.h> |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 26 | #include <linux/platform_device.h> |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 27 | #include <linux/phy.h> |
Olof Johansson | b17471f | 2011-12-20 13:13:07 -0800 | [diff] [blame] | 28 | #include <linux/of.h> |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 29 | #include <linux/of_device.h> |
Boris BREZILLON | 148cbb5 | 2013-08-22 17:57:28 +0200 | [diff] [blame] | 30 | #include <linux/of_mdio.h> |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 31 | #include <linux/of_net.h> |
Jean-Christophe PLAGNIOL-VILLARD | 8ef29f8a | 2012-10-31 06:04:59 +0000 | [diff] [blame] | 32 | #include <linux/pinctrl/consumer.h> |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 33 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 34 | #include "macb.h" |
| 35 | |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 36 | #define MACB_RX_BUFFER_SIZE 128 |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 37 | #define RX_BUFFER_MULTIPLE 64 /* bytes */ |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 38 | #define RX_RING_SIZE 512 /* must be power of 2 */ |
| 39 | #define RX_RING_BYTES (sizeof(struct macb_dma_desc) * RX_RING_SIZE) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 40 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 41 | #define TX_RING_SIZE 128 /* must be power of 2 */ |
| 42 | #define TX_RING_BYTES (sizeof(struct macb_dma_desc) * TX_RING_SIZE) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 43 | |
Nicolas Ferre | 909a858 | 2012-11-19 06:00:21 +0000 | [diff] [blame] | 44 | /* level of occupied TX descriptors under which we wake up TX process */ |
| 45 | #define MACB_TX_WAKEUP_THRESH (3 * TX_RING_SIZE / 4) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 46 | |
| 47 | #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \ |
| 48 | | MACB_BIT(ISR_ROVR)) |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 49 | #define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \ |
| 50 | | MACB_BIT(ISR_RLE) \ |
| 51 | | MACB_BIT(TXERR)) |
| 52 | #define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP)) |
| 53 | |
| 54 | /* |
| 55 | * Graceful stop timeouts in us. We should allow up to |
| 56 | * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions) |
| 57 | */ |
| 58 | #define MACB_HALT_TIMEOUT 1230 |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 59 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 60 | /* Ring buffer accessors */ |
| 61 | static unsigned int macb_tx_ring_wrap(unsigned int index) |
| 62 | { |
| 63 | return index & (TX_RING_SIZE - 1); |
| 64 | } |
| 65 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 66 | static struct macb_dma_desc *macb_tx_desc(struct macb *bp, unsigned int index) |
| 67 | { |
| 68 | return &bp->tx_ring[macb_tx_ring_wrap(index)]; |
| 69 | } |
| 70 | |
| 71 | static struct macb_tx_skb *macb_tx_skb(struct macb *bp, unsigned int index) |
| 72 | { |
| 73 | return &bp->tx_skb[macb_tx_ring_wrap(index)]; |
| 74 | } |
| 75 | |
| 76 | static dma_addr_t macb_tx_dma(struct macb *bp, unsigned int index) |
| 77 | { |
| 78 | dma_addr_t offset; |
| 79 | |
| 80 | offset = macb_tx_ring_wrap(index) * sizeof(struct macb_dma_desc); |
| 81 | |
| 82 | return bp->tx_ring_dma + offset; |
| 83 | } |
| 84 | |
| 85 | static unsigned int macb_rx_ring_wrap(unsigned int index) |
| 86 | { |
| 87 | return index & (RX_RING_SIZE - 1); |
| 88 | } |
| 89 | |
| 90 | static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index) |
| 91 | { |
| 92 | return &bp->rx_ring[macb_rx_ring_wrap(index)]; |
| 93 | } |
| 94 | |
| 95 | static void *macb_rx_buffer(struct macb *bp, unsigned int index) |
| 96 | { |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 97 | return bp->rx_buffers + bp->rx_buffer_size * macb_rx_ring_wrap(index); |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Joachim Eastwood | 314bccc | 2012-11-07 08:14:52 +0000 | [diff] [blame] | 100 | void macb_set_hwaddr(struct macb *bp) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 101 | { |
| 102 | u32 bottom; |
| 103 | u16 top; |
| 104 | |
| 105 | bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr)); |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 106 | macb_or_gem_writel(bp, SA1B, bottom); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 107 | top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4))); |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 108 | macb_or_gem_writel(bp, SA1T, top); |
Joachim Eastwood | 3629a6c | 2012-11-11 13:56:28 +0000 | [diff] [blame] | 109 | |
| 110 | /* Clear unused address register sets */ |
| 111 | macb_or_gem_writel(bp, SA2B, 0); |
| 112 | macb_or_gem_writel(bp, SA2T, 0); |
| 113 | macb_or_gem_writel(bp, SA3B, 0); |
| 114 | macb_or_gem_writel(bp, SA3T, 0); |
| 115 | macb_or_gem_writel(bp, SA4B, 0); |
| 116 | macb_or_gem_writel(bp, SA4T, 0); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 117 | } |
Joachim Eastwood | 314bccc | 2012-11-07 08:14:52 +0000 | [diff] [blame] | 118 | EXPORT_SYMBOL_GPL(macb_set_hwaddr); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 119 | |
Joachim Eastwood | 314bccc | 2012-11-07 08:14:52 +0000 | [diff] [blame] | 120 | void macb_get_hwaddr(struct macb *bp) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 121 | { |
Joachim Eastwood | d25e78a | 2012-11-07 08:14:51 +0000 | [diff] [blame] | 122 | struct macb_platform_data *pdata; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 123 | u32 bottom; |
| 124 | u16 top; |
| 125 | u8 addr[6]; |
Joachim Eastwood | 17b8bb3 | 2012-11-07 08:14:50 +0000 | [diff] [blame] | 126 | int i; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 127 | |
Joachim Eastwood | d25e78a | 2012-11-07 08:14:51 +0000 | [diff] [blame] | 128 | pdata = bp->pdev->dev.platform_data; |
| 129 | |
Joachim Eastwood | 17b8bb3 | 2012-11-07 08:14:50 +0000 | [diff] [blame] | 130 | /* Check all 4 address register for vaild address */ |
| 131 | for (i = 0; i < 4; i++) { |
| 132 | bottom = macb_or_gem_readl(bp, SA1B + i * 8); |
| 133 | top = macb_or_gem_readl(bp, SA1T + i * 8); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 134 | |
Joachim Eastwood | d25e78a | 2012-11-07 08:14:51 +0000 | [diff] [blame] | 135 | if (pdata && pdata->rev_eth_addr) { |
| 136 | addr[5] = bottom & 0xff; |
| 137 | addr[4] = (bottom >> 8) & 0xff; |
| 138 | addr[3] = (bottom >> 16) & 0xff; |
| 139 | addr[2] = (bottom >> 24) & 0xff; |
| 140 | addr[1] = top & 0xff; |
| 141 | addr[0] = (top & 0xff00) >> 8; |
| 142 | } else { |
| 143 | addr[0] = bottom & 0xff; |
| 144 | addr[1] = (bottom >> 8) & 0xff; |
| 145 | addr[2] = (bottom >> 16) & 0xff; |
| 146 | addr[3] = (bottom >> 24) & 0xff; |
| 147 | addr[4] = top & 0xff; |
| 148 | addr[5] = (top >> 8) & 0xff; |
| 149 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 150 | |
Joachim Eastwood | 17b8bb3 | 2012-11-07 08:14:50 +0000 | [diff] [blame] | 151 | if (is_valid_ether_addr(addr)) { |
| 152 | memcpy(bp->dev->dev_addr, addr, sizeof(addr)); |
| 153 | return; |
| 154 | } |
Sven Schnelle | d1d5741 | 2008-06-09 16:33:57 -0700 | [diff] [blame] | 155 | } |
Joachim Eastwood | 17b8bb3 | 2012-11-07 08:14:50 +0000 | [diff] [blame] | 156 | |
| 157 | netdev_info(bp->dev, "invalid hw address, using random\n"); |
| 158 | eth_hw_addr_random(bp->dev); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 159 | } |
Joachim Eastwood | 314bccc | 2012-11-07 08:14:52 +0000 | [diff] [blame] | 160 | EXPORT_SYMBOL_GPL(macb_get_hwaddr); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 161 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 162 | static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 163 | { |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 164 | struct macb *bp = bus->priv; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 165 | int value; |
| 166 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 167 | macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF) |
| 168 | | MACB_BF(RW, MACB_MAN_READ) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 169 | | MACB_BF(PHYA, mii_id) |
| 170 | | MACB_BF(REGA, regnum) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 171 | | MACB_BF(CODE, MACB_MAN_CODE))); |
| 172 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 173 | /* wait for end of transfer */ |
| 174 | while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR))) |
| 175 | cpu_relax(); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 176 | |
| 177 | value = MACB_BFEXT(DATA, macb_readl(bp, MAN)); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 178 | |
| 179 | return value; |
| 180 | } |
| 181 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 182 | static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum, |
| 183 | u16 value) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 184 | { |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 185 | struct macb *bp = bus->priv; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 186 | |
| 187 | macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF) |
| 188 | | MACB_BF(RW, MACB_MAN_WRITE) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 189 | | MACB_BF(PHYA, mii_id) |
| 190 | | MACB_BF(REGA, regnum) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 191 | | MACB_BF(CODE, MACB_MAN_CODE) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 192 | | MACB_BF(DATA, value))); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 193 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 194 | /* wait for end of transfer */ |
| 195 | while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR))) |
| 196 | cpu_relax(); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 197 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 201 | static int macb_mdio_reset(struct mii_bus *bus) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 202 | { |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 203 | return 0; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 204 | } |
| 205 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 206 | static void macb_handle_link_change(struct net_device *dev) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 207 | { |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 208 | struct macb *bp = netdev_priv(dev); |
| 209 | struct phy_device *phydev = bp->phy_dev; |
| 210 | unsigned long flags; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 211 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 212 | int status_change = 0; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 213 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 214 | spin_lock_irqsave(&bp->lock, flags); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 215 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 216 | if (phydev->link) { |
| 217 | if ((bp->speed != phydev->speed) || |
| 218 | (bp->duplex != phydev->duplex)) { |
| 219 | u32 reg; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 220 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 221 | reg = macb_readl(bp, NCFGR); |
| 222 | reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD)); |
Patrice Vilchez | 140b755 | 2012-10-31 06:04:50 +0000 | [diff] [blame] | 223 | if (macb_is_gem(bp)) |
| 224 | reg &= ~GEM_BIT(GBE); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 225 | |
| 226 | if (phydev->duplex) |
| 227 | reg |= MACB_BIT(FD); |
Atsushi Nemoto | 179956f | 2008-02-21 22:50:54 +0900 | [diff] [blame] | 228 | if (phydev->speed == SPEED_100) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 229 | reg |= MACB_BIT(SPD); |
Patrice Vilchez | 140b755 | 2012-10-31 06:04:50 +0000 | [diff] [blame] | 230 | if (phydev->speed == SPEED_1000) |
| 231 | reg |= GEM_BIT(GBE); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 232 | |
Patrice Vilchez | 140b755 | 2012-10-31 06:04:50 +0000 | [diff] [blame] | 233 | macb_or_gem_writel(bp, NCFGR, reg); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 234 | |
| 235 | bp->speed = phydev->speed; |
| 236 | bp->duplex = phydev->duplex; |
| 237 | status_change = 1; |
| 238 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 239 | } |
| 240 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 241 | if (phydev->link != bp->link) { |
Anton Vorontsov | c8f1568 | 2008-07-22 15:41:24 -0700 | [diff] [blame] | 242 | if (!phydev->link) { |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 243 | bp->speed = 0; |
| 244 | bp->duplex = -1; |
| 245 | } |
| 246 | bp->link = phydev->link; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 247 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 248 | status_change = 1; |
| 249 | } |
| 250 | |
| 251 | spin_unlock_irqrestore(&bp->lock, flags); |
| 252 | |
| 253 | if (status_change) { |
Nicolas Ferre | 03fc472 | 2012-07-03 23:14:13 +0000 | [diff] [blame] | 254 | if (phydev->link) { |
| 255 | netif_carrier_on(dev); |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 256 | netdev_info(dev, "link up (%d/%s)\n", |
| 257 | phydev->speed, |
| 258 | phydev->duplex == DUPLEX_FULL ? |
| 259 | "Full" : "Half"); |
Nicolas Ferre | 03fc472 | 2012-07-03 23:14:13 +0000 | [diff] [blame] | 260 | } else { |
| 261 | netif_carrier_off(dev); |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 262 | netdev_info(dev, "link down\n"); |
Nicolas Ferre | 03fc472 | 2012-07-03 23:14:13 +0000 | [diff] [blame] | 263 | } |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
| 267 | /* based on au1000_eth. c*/ |
| 268 | static int macb_mii_probe(struct net_device *dev) |
| 269 | { |
| 270 | struct macb *bp = netdev_priv(dev); |
Joachim Eastwood | 2dbfdbb9 | 2012-11-11 13:56:27 +0000 | [diff] [blame] | 271 | struct macb_platform_data *pdata; |
Jiri Pirko | 7455a76 | 2010-02-08 05:12:08 +0000 | [diff] [blame] | 272 | struct phy_device *phydev; |
Joachim Eastwood | 2dbfdbb9 | 2012-11-11 13:56:27 +0000 | [diff] [blame] | 273 | int phy_irq; |
Jiri Pirko | 7455a76 | 2010-02-08 05:12:08 +0000 | [diff] [blame] | 274 | int ret; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 275 | |
Jiri Pirko | 7455a76 | 2010-02-08 05:12:08 +0000 | [diff] [blame] | 276 | phydev = phy_find_first(bp->mii_bus); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 277 | if (!phydev) { |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 278 | netdev_err(dev, "no PHY found\n"); |
Boris BREZILLON | 7daa78e | 2013-08-27 14:36:14 +0200 | [diff] [blame^] | 279 | return -ENXIO; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 280 | } |
| 281 | |
Joachim Eastwood | 2dbfdbb9 | 2012-11-11 13:56:27 +0000 | [diff] [blame] | 282 | pdata = dev_get_platdata(&bp->pdev->dev); |
| 283 | if (pdata && gpio_is_valid(pdata->phy_irq_pin)) { |
| 284 | ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin, "phy int"); |
| 285 | if (!ret) { |
| 286 | phy_irq = gpio_to_irq(pdata->phy_irq_pin); |
| 287 | phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq; |
| 288 | } |
| 289 | } |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 290 | |
| 291 | /* attach the mac to the phy */ |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 292 | ret = phy_connect_direct(dev, phydev, &macb_handle_link_change, |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 293 | bp->phy_interface); |
Jiri Pirko | 7455a76 | 2010-02-08 05:12:08 +0000 | [diff] [blame] | 294 | if (ret) { |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 295 | netdev_err(dev, "Could not attach to PHY\n"); |
Jiri Pirko | 7455a76 | 2010-02-08 05:12:08 +0000 | [diff] [blame] | 296 | return ret; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 297 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 298 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 299 | /* mask with MAC supported features */ |
Patrice Vilchez | 140b755 | 2012-10-31 06:04:50 +0000 | [diff] [blame] | 300 | if (macb_is_gem(bp)) |
| 301 | phydev->supported &= PHY_GBIT_FEATURES; |
| 302 | else |
| 303 | phydev->supported &= PHY_BASIC_FEATURES; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 304 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 305 | phydev->advertising = phydev->supported; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 306 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 307 | bp->link = 0; |
| 308 | bp->speed = 0; |
| 309 | bp->duplex = -1; |
| 310 | bp->phy_dev = phydev; |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
Joachim Eastwood | 0005f54 | 2012-10-18 11:01:12 +0000 | [diff] [blame] | 315 | int macb_mii_init(struct macb *bp) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 316 | { |
Jamie Iles | 84e0cdb | 2011-03-08 20:17:06 +0000 | [diff] [blame] | 317 | struct macb_platform_data *pdata; |
Boris BREZILLON | 148cbb5 | 2013-08-22 17:57:28 +0200 | [diff] [blame] | 318 | struct device_node *np; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 319 | int err = -ENXIO, i; |
| 320 | |
Uwe Kleine-Koenig | 3dbda77 | 2009-07-23 08:31:31 +0200 | [diff] [blame] | 321 | /* Enable management port */ |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 322 | macb_writel(bp, NCR, MACB_BIT(MPE)); |
| 323 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 324 | bp->mii_bus = mdiobus_alloc(); |
| 325 | if (bp->mii_bus == NULL) { |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 326 | err = -ENOMEM; |
| 327 | goto err_out; |
| 328 | } |
| 329 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 330 | bp->mii_bus->name = "MACB_mii_bus"; |
| 331 | bp->mii_bus->read = &macb_mdio_read; |
| 332 | bp->mii_bus->write = &macb_mdio_write; |
| 333 | bp->mii_bus->reset = &macb_mdio_reset; |
Florian Fainelli | 98d5e57 | 2012-01-09 23:59:11 +0000 | [diff] [blame] | 334 | snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x", |
| 335 | bp->pdev->name, bp->pdev->id); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 336 | bp->mii_bus->priv = bp; |
| 337 | bp->mii_bus->parent = &bp->dev->dev; |
| 338 | pdata = bp->pdev->dev.platform_data; |
| 339 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 340 | bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL); |
| 341 | if (!bp->mii_bus->irq) { |
| 342 | err = -ENOMEM; |
| 343 | goto err_out_free_mdiobus; |
| 344 | } |
| 345 | |
Jamie Iles | 9152394 | 2011-02-28 04:05:25 +0000 | [diff] [blame] | 346 | dev_set_drvdata(&bp->dev->dev, bp->mii_bus); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 347 | |
Boris BREZILLON | 148cbb5 | 2013-08-22 17:57:28 +0200 | [diff] [blame] | 348 | np = bp->pdev->dev.of_node; |
| 349 | if (np) { |
| 350 | /* try dt phy registration */ |
| 351 | err = of_mdiobus_register(bp->mii_bus, np); |
| 352 | |
| 353 | /* fallback to standard phy registration if no phy were |
| 354 | found during dt phy registration */ |
| 355 | if (!err && !phy_find_first(bp->mii_bus)) { |
| 356 | for (i = 0; i < PHY_MAX_ADDR; i++) { |
| 357 | struct phy_device *phydev; |
| 358 | |
| 359 | phydev = mdiobus_scan(bp->mii_bus, i); |
| 360 | if (IS_ERR(phydev)) { |
| 361 | err = PTR_ERR(phydev); |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | if (err) |
| 367 | goto err_out_unregister_bus; |
| 368 | } |
| 369 | } else { |
| 370 | for (i = 0; i < PHY_MAX_ADDR; i++) |
| 371 | bp->mii_bus->irq[i] = PHY_POLL; |
| 372 | |
| 373 | if (pdata) |
| 374 | bp->mii_bus->phy_mask = pdata->phy_mask; |
| 375 | |
| 376 | err = mdiobus_register(bp->mii_bus); |
| 377 | } |
| 378 | |
| 379 | if (err) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 380 | goto err_out_free_mdio_irq; |
| 381 | |
Boris BREZILLON | 7daa78e | 2013-08-27 14:36:14 +0200 | [diff] [blame^] | 382 | err = macb_mii_probe(bp->dev); |
| 383 | if (err) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 384 | goto err_out_unregister_bus; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 385 | |
| 386 | return 0; |
| 387 | |
| 388 | err_out_unregister_bus: |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 389 | mdiobus_unregister(bp->mii_bus); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 390 | err_out_free_mdio_irq: |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 391 | kfree(bp->mii_bus->irq); |
| 392 | err_out_free_mdiobus: |
| 393 | mdiobus_free(bp->mii_bus); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 394 | err_out: |
| 395 | return err; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 396 | } |
Joachim Eastwood | 0005f54 | 2012-10-18 11:01:12 +0000 | [diff] [blame] | 397 | EXPORT_SYMBOL_GPL(macb_mii_init); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 398 | |
| 399 | static void macb_update_stats(struct macb *bp) |
| 400 | { |
| 401 | u32 __iomem *reg = bp->regs + MACB_PFR; |
Jamie Iles | a494ed8 | 2011-03-09 16:26:35 +0000 | [diff] [blame] | 402 | u32 *p = &bp->hw_stats.macb.rx_pause_frames; |
| 403 | u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 404 | |
| 405 | WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4); |
| 406 | |
| 407 | for(; p < end; p++, reg++) |
Haavard Skinnemoen | 0f0d84e | 2006-12-08 14:38:30 +0100 | [diff] [blame] | 408 | *p += __raw_readl(reg); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 409 | } |
| 410 | |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 411 | static int macb_halt_tx(struct macb *bp) |
| 412 | { |
| 413 | unsigned long halt_time, timeout; |
| 414 | u32 status; |
| 415 | |
| 416 | macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT)); |
| 417 | |
| 418 | timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT); |
| 419 | do { |
| 420 | halt_time = jiffies; |
| 421 | status = macb_readl(bp, TSR); |
| 422 | if (!(status & MACB_BIT(TGO))) |
| 423 | return 0; |
| 424 | |
| 425 | usleep_range(10, 250); |
| 426 | } while (time_before(halt_time, timeout)); |
| 427 | |
| 428 | return -ETIMEDOUT; |
| 429 | } |
| 430 | |
| 431 | static void macb_tx_error_task(struct work_struct *work) |
| 432 | { |
| 433 | struct macb *bp = container_of(work, struct macb, tx_error_task); |
| 434 | struct macb_tx_skb *tx_skb; |
| 435 | struct sk_buff *skb; |
| 436 | unsigned int tail; |
| 437 | |
| 438 | netdev_vdbg(bp->dev, "macb_tx_error_task: t = %u, h = %u\n", |
| 439 | bp->tx_tail, bp->tx_head); |
| 440 | |
| 441 | /* Make sure nobody is trying to queue up new packets */ |
| 442 | netif_stop_queue(bp->dev); |
| 443 | |
| 444 | /* |
| 445 | * Stop transmission now |
| 446 | * (in case we have just queued new packets) |
| 447 | */ |
| 448 | if (macb_halt_tx(bp)) |
| 449 | /* Just complain for now, reinitializing TX path can be good */ |
| 450 | netdev_err(bp->dev, "BUG: halt tx timed out\n"); |
| 451 | |
| 452 | /* No need for the lock here as nobody will interrupt us anymore */ |
| 453 | |
| 454 | /* |
| 455 | * Treat frames in TX queue including the ones that caused the error. |
| 456 | * Free transmit buffers in upper layer. |
| 457 | */ |
| 458 | for (tail = bp->tx_tail; tail != bp->tx_head; tail++) { |
| 459 | struct macb_dma_desc *desc; |
| 460 | u32 ctrl; |
| 461 | |
| 462 | desc = macb_tx_desc(bp, tail); |
| 463 | ctrl = desc->ctrl; |
| 464 | tx_skb = macb_tx_skb(bp, tail); |
| 465 | skb = tx_skb->skb; |
| 466 | |
| 467 | if (ctrl & MACB_BIT(TX_USED)) { |
| 468 | netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n", |
| 469 | macb_tx_ring_wrap(tail), skb->data); |
| 470 | bp->stats.tx_packets++; |
| 471 | bp->stats.tx_bytes += skb->len; |
| 472 | } else { |
| 473 | /* |
| 474 | * "Buffers exhausted mid-frame" errors may only happen |
| 475 | * if the driver is buggy, so complain loudly about those. |
| 476 | * Statistics are updated by hardware. |
| 477 | */ |
| 478 | if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED)) |
| 479 | netdev_err(bp->dev, |
| 480 | "BUG: TX buffers exhausted mid-frame\n"); |
| 481 | |
| 482 | desc->ctrl = ctrl | MACB_BIT(TX_USED); |
| 483 | } |
| 484 | |
| 485 | dma_unmap_single(&bp->pdev->dev, tx_skb->mapping, skb->len, |
| 486 | DMA_TO_DEVICE); |
| 487 | tx_skb->skb = NULL; |
| 488 | dev_kfree_skb(skb); |
| 489 | } |
| 490 | |
| 491 | /* Make descriptor updates visible to hardware */ |
| 492 | wmb(); |
| 493 | |
| 494 | /* Reinitialize the TX desc queue */ |
| 495 | macb_writel(bp, TBQP, bp->tx_ring_dma); |
| 496 | /* Make TX ring reflect state of hardware */ |
| 497 | bp->tx_head = bp->tx_tail = 0; |
| 498 | |
| 499 | /* Now we are ready to start transmission again */ |
| 500 | netif_wake_queue(bp->dev); |
| 501 | |
| 502 | /* Housework before enabling TX IRQ */ |
| 503 | macb_writel(bp, TSR, macb_readl(bp, TSR)); |
| 504 | macb_writel(bp, IER, MACB_TX_INT_FLAGS); |
| 505 | } |
| 506 | |
| 507 | static void macb_tx_interrupt(struct macb *bp) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 508 | { |
| 509 | unsigned int tail; |
| 510 | unsigned int head; |
| 511 | u32 status; |
| 512 | |
| 513 | status = macb_readl(bp, TSR); |
| 514 | macb_writel(bp, TSR, status); |
| 515 | |
Nicolas Ferre | 581df9e | 2013-05-14 03:00:16 +0000 | [diff] [blame] | 516 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
| 517 | macb_writel(bp, ISR, MACB_BIT(TCOMP)); |
Steffen Trumtrar | 749a2b6 | 2013-03-27 23:07:05 +0000 | [diff] [blame] | 518 | |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 519 | netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n", |
| 520 | (unsigned long)status); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 521 | |
| 522 | head = bp->tx_head; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 523 | for (tail = bp->tx_tail; tail != head; tail++) { |
| 524 | struct macb_tx_skb *tx_skb; |
| 525 | struct sk_buff *skb; |
| 526 | struct macb_dma_desc *desc; |
| 527 | u32 ctrl; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 528 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 529 | desc = macb_tx_desc(bp, tail); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 530 | |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 531 | /* Make hw descriptor updates visible to CPU */ |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 532 | rmb(); |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 533 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 534 | ctrl = desc->ctrl; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 535 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 536 | if (!(ctrl & MACB_BIT(TX_USED))) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 537 | break; |
| 538 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 539 | tx_skb = macb_tx_skb(bp, tail); |
| 540 | skb = tx_skb->skb; |
| 541 | |
Havard Skinnemoen | a268adb | 2012-10-31 06:04:52 +0000 | [diff] [blame] | 542 | netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n", |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 543 | macb_tx_ring_wrap(tail), skb->data); |
| 544 | dma_unmap_single(&bp->pdev->dev, tx_skb->mapping, skb->len, |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 545 | DMA_TO_DEVICE); |
| 546 | bp->stats.tx_packets++; |
| 547 | bp->stats.tx_bytes += skb->len; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 548 | tx_skb->skb = NULL; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 549 | dev_kfree_skb_irq(skb); |
| 550 | } |
| 551 | |
| 552 | bp->tx_tail = tail; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 553 | if (netif_queue_stopped(bp->dev) |
Nicolas Ferre | 909a858 | 2012-11-19 06:00:21 +0000 | [diff] [blame] | 554 | && CIRC_CNT(bp->tx_head, bp->tx_tail, |
| 555 | TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 556 | netif_wake_queue(bp->dev); |
| 557 | } |
| 558 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 559 | static void gem_rx_refill(struct macb *bp) |
| 560 | { |
| 561 | unsigned int entry; |
| 562 | struct sk_buff *skb; |
| 563 | struct macb_dma_desc *desc; |
| 564 | dma_addr_t paddr; |
| 565 | |
| 566 | while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail, RX_RING_SIZE) > 0) { |
| 567 | u32 addr, ctrl; |
| 568 | |
| 569 | entry = macb_rx_ring_wrap(bp->rx_prepared_head); |
| 570 | desc = &bp->rx_ring[entry]; |
| 571 | |
| 572 | /* Make hw descriptor updates visible to CPU */ |
| 573 | rmb(); |
| 574 | |
| 575 | addr = desc->addr; |
| 576 | ctrl = desc->ctrl; |
| 577 | bp->rx_prepared_head++; |
| 578 | |
| 579 | if ((addr & MACB_BIT(RX_USED))) |
| 580 | continue; |
| 581 | |
| 582 | if (bp->rx_skbuff[entry] == NULL) { |
| 583 | /* allocate sk_buff for this free entry in ring */ |
| 584 | skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size); |
| 585 | if (unlikely(skb == NULL)) { |
| 586 | netdev_err(bp->dev, |
| 587 | "Unable to allocate sk_buff\n"); |
| 588 | break; |
| 589 | } |
| 590 | bp->rx_skbuff[entry] = skb; |
| 591 | |
| 592 | /* now fill corresponding descriptor entry */ |
| 593 | paddr = dma_map_single(&bp->pdev->dev, skb->data, |
| 594 | bp->rx_buffer_size, DMA_FROM_DEVICE); |
| 595 | |
| 596 | if (entry == RX_RING_SIZE - 1) |
| 597 | paddr |= MACB_BIT(RX_WRAP); |
| 598 | bp->rx_ring[entry].addr = paddr; |
| 599 | bp->rx_ring[entry].ctrl = 0; |
| 600 | |
| 601 | /* properly align Ethernet header */ |
| 602 | skb_reserve(skb, NET_IP_ALIGN); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | /* Make descriptor updates visible to hardware */ |
| 607 | wmb(); |
| 608 | |
| 609 | netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n", |
| 610 | bp->rx_prepared_head, bp->rx_tail); |
| 611 | } |
| 612 | |
| 613 | /* Mark DMA descriptors from begin up to and not including end as unused */ |
| 614 | static void discard_partial_frame(struct macb *bp, unsigned int begin, |
| 615 | unsigned int end) |
| 616 | { |
| 617 | unsigned int frag; |
| 618 | |
| 619 | for (frag = begin; frag != end; frag++) { |
| 620 | struct macb_dma_desc *desc = macb_rx_desc(bp, frag); |
| 621 | desc->addr &= ~MACB_BIT(RX_USED); |
| 622 | } |
| 623 | |
| 624 | /* Make descriptor updates visible to hardware */ |
| 625 | wmb(); |
| 626 | |
| 627 | /* |
| 628 | * When this happens, the hardware stats registers for |
| 629 | * whatever caused this is updated, so we don't have to record |
| 630 | * anything. |
| 631 | */ |
| 632 | } |
| 633 | |
| 634 | static int gem_rx(struct macb *bp, int budget) |
| 635 | { |
| 636 | unsigned int len; |
| 637 | unsigned int entry; |
| 638 | struct sk_buff *skb; |
| 639 | struct macb_dma_desc *desc; |
| 640 | int count = 0; |
| 641 | |
| 642 | while (count < budget) { |
| 643 | u32 addr, ctrl; |
| 644 | |
| 645 | entry = macb_rx_ring_wrap(bp->rx_tail); |
| 646 | desc = &bp->rx_ring[entry]; |
| 647 | |
| 648 | /* Make hw descriptor updates visible to CPU */ |
| 649 | rmb(); |
| 650 | |
| 651 | addr = desc->addr; |
| 652 | ctrl = desc->ctrl; |
| 653 | |
| 654 | if (!(addr & MACB_BIT(RX_USED))) |
| 655 | break; |
| 656 | |
| 657 | desc->addr &= ~MACB_BIT(RX_USED); |
| 658 | bp->rx_tail++; |
| 659 | count++; |
| 660 | |
| 661 | if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) { |
| 662 | netdev_err(bp->dev, |
| 663 | "not whole frame pointed by descriptor\n"); |
| 664 | bp->stats.rx_dropped++; |
| 665 | break; |
| 666 | } |
| 667 | skb = bp->rx_skbuff[entry]; |
| 668 | if (unlikely(!skb)) { |
| 669 | netdev_err(bp->dev, |
| 670 | "inconsistent Rx descriptor chain\n"); |
| 671 | bp->stats.rx_dropped++; |
| 672 | break; |
| 673 | } |
| 674 | /* now everything is ready for receiving packet */ |
| 675 | bp->rx_skbuff[entry] = NULL; |
| 676 | len = MACB_BFEXT(RX_FRMLEN, ctrl); |
| 677 | |
| 678 | netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len); |
| 679 | |
| 680 | skb_put(skb, len); |
| 681 | addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, addr)); |
| 682 | dma_unmap_single(&bp->pdev->dev, addr, |
| 683 | len, DMA_FROM_DEVICE); |
| 684 | |
| 685 | skb->protocol = eth_type_trans(skb, bp->dev); |
| 686 | skb_checksum_none_assert(skb); |
| 687 | |
| 688 | bp->stats.rx_packets++; |
| 689 | bp->stats.rx_bytes += skb->len; |
| 690 | |
| 691 | #if defined(DEBUG) && defined(VERBOSE_DEBUG) |
| 692 | netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n", |
| 693 | skb->len, skb->csum); |
| 694 | print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1, |
| 695 | skb->mac_header, 16, true); |
| 696 | print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1, |
| 697 | skb->data, 32, true); |
| 698 | #endif |
| 699 | |
| 700 | netif_receive_skb(skb); |
| 701 | } |
| 702 | |
| 703 | gem_rx_refill(bp); |
| 704 | |
| 705 | return count; |
| 706 | } |
| 707 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 708 | static int macb_rx_frame(struct macb *bp, unsigned int first_frag, |
| 709 | unsigned int last_frag) |
| 710 | { |
| 711 | unsigned int len; |
| 712 | unsigned int frag; |
Havard Skinnemoen | 29bc2e1 | 2012-10-31 06:04:58 +0000 | [diff] [blame] | 713 | unsigned int offset; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 714 | struct sk_buff *skb; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 715 | struct macb_dma_desc *desc; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 716 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 717 | desc = macb_rx_desc(bp, last_frag); |
| 718 | len = MACB_BFEXT(RX_FRMLEN, desc->ctrl); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 719 | |
Havard Skinnemoen | a268adb | 2012-10-31 06:04:52 +0000 | [diff] [blame] | 720 | netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n", |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 721 | macb_rx_ring_wrap(first_frag), |
| 722 | macb_rx_ring_wrap(last_frag), len); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 723 | |
Havard Skinnemoen | 29bc2e1 | 2012-10-31 06:04:58 +0000 | [diff] [blame] | 724 | /* |
| 725 | * The ethernet header starts NET_IP_ALIGN bytes into the |
| 726 | * first buffer. Since the header is 14 bytes, this makes the |
| 727 | * payload word-aligned. |
| 728 | * |
| 729 | * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy |
| 730 | * the two padding bytes into the skb so that we avoid hitting |
| 731 | * the slowpath in memcpy(), and pull them off afterwards. |
| 732 | */ |
| 733 | skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 734 | if (!skb) { |
| 735 | bp->stats.rx_dropped++; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 736 | for (frag = first_frag; ; frag++) { |
| 737 | desc = macb_rx_desc(bp, frag); |
| 738 | desc->addr &= ~MACB_BIT(RX_USED); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 739 | if (frag == last_frag) |
| 740 | break; |
| 741 | } |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 742 | |
| 743 | /* Make descriptor updates visible to hardware */ |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 744 | wmb(); |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 745 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 746 | return 1; |
| 747 | } |
| 748 | |
Havard Skinnemoen | 29bc2e1 | 2012-10-31 06:04:58 +0000 | [diff] [blame] | 749 | offset = 0; |
| 750 | len += NET_IP_ALIGN; |
Eric Dumazet | bc8acf2 | 2010-09-02 13:07:41 -0700 | [diff] [blame] | 751 | skb_checksum_none_assert(skb); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 752 | skb_put(skb, len); |
| 753 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 754 | for (frag = first_frag; ; frag++) { |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 755 | unsigned int frag_len = bp->rx_buffer_size; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 756 | |
| 757 | if (offset + frag_len > len) { |
| 758 | BUG_ON(frag != last_frag); |
| 759 | frag_len = len - offset; |
| 760 | } |
Arnaldo Carvalho de Melo | 27d7ff4 | 2007-03-31 11:55:19 -0300 | [diff] [blame] | 761 | skb_copy_to_linear_data_offset(skb, offset, |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 762 | macb_rx_buffer(bp, frag), frag_len); |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 763 | offset += bp->rx_buffer_size; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 764 | desc = macb_rx_desc(bp, frag); |
| 765 | desc->addr &= ~MACB_BIT(RX_USED); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 766 | |
| 767 | if (frag == last_frag) |
| 768 | break; |
| 769 | } |
| 770 | |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 771 | /* Make descriptor updates visible to hardware */ |
| 772 | wmb(); |
| 773 | |
Havard Skinnemoen | 29bc2e1 | 2012-10-31 06:04:58 +0000 | [diff] [blame] | 774 | __skb_pull(skb, NET_IP_ALIGN); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 775 | skb->protocol = eth_type_trans(skb, bp->dev); |
| 776 | |
| 777 | bp->stats.rx_packets++; |
Havard Skinnemoen | 29bc2e1 | 2012-10-31 06:04:58 +0000 | [diff] [blame] | 778 | bp->stats.rx_bytes += skb->len; |
Havard Skinnemoen | a268adb | 2012-10-31 06:04:52 +0000 | [diff] [blame] | 779 | netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n", |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 780 | skb->len, skb->csum); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 781 | netif_receive_skb(skb); |
| 782 | |
| 783 | return 0; |
| 784 | } |
| 785 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 786 | static int macb_rx(struct macb *bp, int budget) |
| 787 | { |
| 788 | int received = 0; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 789 | unsigned int tail; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 790 | int first_frag = -1; |
| 791 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 792 | for (tail = bp->rx_tail; budget > 0; tail++) { |
| 793 | struct macb_dma_desc *desc = macb_rx_desc(bp, tail); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 794 | u32 addr, ctrl; |
| 795 | |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 796 | /* Make hw descriptor updates visible to CPU */ |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 797 | rmb(); |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 798 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 799 | addr = desc->addr; |
| 800 | ctrl = desc->ctrl; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 801 | |
| 802 | if (!(addr & MACB_BIT(RX_USED))) |
| 803 | break; |
| 804 | |
| 805 | if (ctrl & MACB_BIT(RX_SOF)) { |
| 806 | if (first_frag != -1) |
| 807 | discard_partial_frame(bp, first_frag, tail); |
| 808 | first_frag = tail; |
| 809 | } |
| 810 | |
| 811 | if (ctrl & MACB_BIT(RX_EOF)) { |
| 812 | int dropped; |
| 813 | BUG_ON(first_frag == -1); |
| 814 | |
| 815 | dropped = macb_rx_frame(bp, first_frag, tail); |
| 816 | first_frag = -1; |
| 817 | if (!dropped) { |
| 818 | received++; |
| 819 | budget--; |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | if (first_frag != -1) |
| 825 | bp->rx_tail = first_frag; |
| 826 | else |
| 827 | bp->rx_tail = tail; |
| 828 | |
| 829 | return received; |
| 830 | } |
| 831 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 832 | static int macb_poll(struct napi_struct *napi, int budget) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 833 | { |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 834 | struct macb *bp = container_of(napi, struct macb, napi); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 835 | int work_done; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 836 | u32 status; |
| 837 | |
| 838 | status = macb_readl(bp, RSR); |
| 839 | macb_writel(bp, RSR, status); |
| 840 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 841 | work_done = 0; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 842 | |
Havard Skinnemoen | a268adb | 2012-10-31 06:04:52 +0000 | [diff] [blame] | 843 | netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n", |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 844 | (unsigned long)status, budget); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 845 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 846 | work_done = bp->macbgem_ops.mog_rx(bp, budget); |
Joshua Hoke | b336369 | 2010-10-25 01:44:22 +0000 | [diff] [blame] | 847 | if (work_done < budget) { |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 848 | napi_complete(napi); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 849 | |
Joshua Hoke | b336369 | 2010-10-25 01:44:22 +0000 | [diff] [blame] | 850 | /* |
| 851 | * We've done what we can to clean the buffers. Make sure we |
| 852 | * get notified when new packets arrive. |
| 853 | */ |
| 854 | macb_writel(bp, IER, MACB_RX_INT_FLAGS); |
Nicolas Ferre | 8770e91 | 2013-02-12 11:08:48 +0100 | [diff] [blame] | 855 | |
| 856 | /* Packets received while interrupts were disabled */ |
| 857 | status = macb_readl(bp, RSR); |
| 858 | if (unlikely(status)) |
| 859 | napi_reschedule(napi); |
Joshua Hoke | b336369 | 2010-10-25 01:44:22 +0000 | [diff] [blame] | 860 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 861 | |
| 862 | /* TODO: Handle errors */ |
| 863 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 864 | return work_done; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | static irqreturn_t macb_interrupt(int irq, void *dev_id) |
| 868 | { |
| 869 | struct net_device *dev = dev_id; |
| 870 | struct macb *bp = netdev_priv(dev); |
| 871 | u32 status; |
| 872 | |
| 873 | status = macb_readl(bp, ISR); |
| 874 | |
| 875 | if (unlikely(!status)) |
| 876 | return IRQ_NONE; |
| 877 | |
| 878 | spin_lock(&bp->lock); |
| 879 | |
| 880 | while (status) { |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 881 | /* close possible race with dev_close */ |
| 882 | if (unlikely(!netif_running(dev))) { |
Joachim Eastwood | 95ebcea | 2012-10-22 08:45:31 +0000 | [diff] [blame] | 883 | macb_writel(bp, IDR, -1); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 884 | break; |
| 885 | } |
| 886 | |
Havard Skinnemoen | a268adb | 2012-10-31 06:04:52 +0000 | [diff] [blame] | 887 | netdev_vdbg(bp->dev, "isr = 0x%08lx\n", (unsigned long)status); |
| 888 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 889 | if (status & MACB_RX_INT_FLAGS) { |
Joshua Hoke | b336369 | 2010-10-25 01:44:22 +0000 | [diff] [blame] | 890 | /* |
| 891 | * There's no point taking any more interrupts |
| 892 | * until we have processed the buffers. The |
| 893 | * scheduling call may fail if the poll routine |
| 894 | * is already scheduled, so disable interrupts |
| 895 | * now. |
| 896 | */ |
| 897 | macb_writel(bp, IDR, MACB_RX_INT_FLAGS); |
Nicolas Ferre | 581df9e | 2013-05-14 03:00:16 +0000 | [diff] [blame] | 898 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
| 899 | macb_writel(bp, ISR, MACB_BIT(RCOMP)); |
Joshua Hoke | b336369 | 2010-10-25 01:44:22 +0000 | [diff] [blame] | 900 | |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 901 | if (napi_schedule_prep(&bp->napi)) { |
Havard Skinnemoen | a268adb | 2012-10-31 06:04:52 +0000 | [diff] [blame] | 902 | netdev_vdbg(bp->dev, "scheduling RX softirq\n"); |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 903 | __napi_schedule(&bp->napi); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 904 | } |
| 905 | } |
| 906 | |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 907 | if (unlikely(status & (MACB_TX_ERR_FLAGS))) { |
| 908 | macb_writel(bp, IDR, MACB_TX_INT_FLAGS); |
| 909 | schedule_work(&bp->tx_error_task); |
| 910 | break; |
| 911 | } |
| 912 | |
| 913 | if (status & MACB_BIT(TCOMP)) |
| 914 | macb_tx_interrupt(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 915 | |
| 916 | /* |
| 917 | * Link change detection isn't possible with RMII, so we'll |
| 918 | * add that if/when we get our hands on a full-blown MII PHY. |
| 919 | */ |
| 920 | |
Alexander Stein | b19f7f7 | 2011-04-13 05:03:24 +0000 | [diff] [blame] | 921 | if (status & MACB_BIT(ISR_ROVR)) { |
| 922 | /* We missed at least one packet */ |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 923 | if (macb_is_gem(bp)) |
| 924 | bp->hw_stats.gem.rx_overruns++; |
| 925 | else |
| 926 | bp->hw_stats.macb.rx_overruns++; |
Alexander Stein | b19f7f7 | 2011-04-13 05:03:24 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 929 | if (status & MACB_BIT(HRESP)) { |
| 930 | /* |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 931 | * TODO: Reset the hardware, and maybe move the |
| 932 | * netdev_err to a lower-priority context as well |
| 933 | * (work queue?) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 934 | */ |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 935 | netdev_err(dev, "DMA bus error: HRESP not OK\n"); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | status = macb_readl(bp, ISR); |
| 939 | } |
| 940 | |
| 941 | spin_unlock(&bp->lock); |
| 942 | |
| 943 | return IRQ_HANDLED; |
| 944 | } |
| 945 | |
Thomas Petazzoni | 6e8cf5c | 2009-05-04 11:08:41 -0700 | [diff] [blame] | 946 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 947 | /* |
| 948 | * Polling receive - used by netconsole and other diagnostic tools |
| 949 | * to allow network i/o with interrupts disabled. |
| 950 | */ |
| 951 | static void macb_poll_controller(struct net_device *dev) |
| 952 | { |
| 953 | unsigned long flags; |
| 954 | |
| 955 | local_irq_save(flags); |
| 956 | macb_interrupt(dev->irq, dev); |
| 957 | local_irq_restore(flags); |
| 958 | } |
| 959 | #endif |
| 960 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 961 | static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 962 | { |
| 963 | struct macb *bp = netdev_priv(dev); |
| 964 | dma_addr_t mapping; |
| 965 | unsigned int len, entry; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 966 | struct macb_dma_desc *desc; |
| 967 | struct macb_tx_skb *tx_skb; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 968 | u32 ctrl; |
Dongdong Deng | 4871953 | 2009-08-23 19:49:07 -0700 | [diff] [blame] | 969 | unsigned long flags; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 970 | |
Havard Skinnemoen | a268adb | 2012-10-31 06:04:52 +0000 | [diff] [blame] | 971 | #if defined(DEBUG) && defined(VERBOSE_DEBUG) |
| 972 | netdev_vdbg(bp->dev, |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 973 | "start_xmit: len %u head %p data %p tail %p end %p\n", |
| 974 | skb->len, skb->head, skb->data, |
| 975 | skb_tail_pointer(skb), skb_end_pointer(skb)); |
| 976 | print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1, |
| 977 | skb->data, 16, true); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 978 | #endif |
| 979 | |
| 980 | len = skb->len; |
Dongdong Deng | 4871953 | 2009-08-23 19:49:07 -0700 | [diff] [blame] | 981 | spin_lock_irqsave(&bp->lock, flags); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 982 | |
| 983 | /* This is a hard error, log it. */ |
Nicolas Ferre | 909a858 | 2012-11-19 06:00:21 +0000 | [diff] [blame] | 984 | if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < 1) { |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 985 | netif_stop_queue(dev); |
Dongdong Deng | 4871953 | 2009-08-23 19:49:07 -0700 | [diff] [blame] | 986 | spin_unlock_irqrestore(&bp->lock, flags); |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 987 | netdev_err(bp->dev, "BUG! Tx Ring full when queue awake!\n"); |
| 988 | netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n", |
| 989 | bp->tx_head, bp->tx_tail); |
Patrick McHardy | 5b54814 | 2009-06-12 06:22:29 +0000 | [diff] [blame] | 990 | return NETDEV_TX_BUSY; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 991 | } |
| 992 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 993 | entry = macb_tx_ring_wrap(bp->tx_head); |
| 994 | bp->tx_head++; |
Havard Skinnemoen | a268adb | 2012-10-31 06:04:52 +0000 | [diff] [blame] | 995 | netdev_vdbg(bp->dev, "Allocated ring entry %u\n", entry); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 996 | mapping = dma_map_single(&bp->pdev->dev, skb->data, |
| 997 | len, DMA_TO_DEVICE); |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 998 | |
| 999 | tx_skb = &bp->tx_skb[entry]; |
| 1000 | tx_skb->skb = skb; |
| 1001 | tx_skb->mapping = mapping; |
Havard Skinnemoen | a268adb | 2012-10-31 06:04:52 +0000 | [diff] [blame] | 1002 | netdev_vdbg(bp->dev, "Mapped skb data %p to DMA addr %08lx\n", |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 1003 | skb->data, (unsigned long)mapping); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1004 | |
| 1005 | ctrl = MACB_BF(TX_FRMLEN, len); |
| 1006 | ctrl |= MACB_BIT(TX_LAST); |
| 1007 | if (entry == (TX_RING_SIZE - 1)) |
| 1008 | ctrl |= MACB_BIT(TX_WRAP); |
| 1009 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 1010 | desc = &bp->tx_ring[entry]; |
| 1011 | desc->addr = mapping; |
| 1012 | desc->ctrl = ctrl; |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 1013 | |
| 1014 | /* Make newly initialized descriptor visible to hardware */ |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1015 | wmb(); |
| 1016 | |
Richard Cochran | e072092 | 2011-06-19 21:51:28 +0000 | [diff] [blame] | 1017 | skb_tx_timestamp(skb); |
| 1018 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1019 | macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART)); |
| 1020 | |
Nicolas Ferre | 909a858 | 2012-11-19 06:00:21 +0000 | [diff] [blame] | 1021 | if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < 1) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1022 | netif_stop_queue(dev); |
| 1023 | |
Dongdong Deng | 4871953 | 2009-08-23 19:49:07 -0700 | [diff] [blame] | 1024 | spin_unlock_irqrestore(&bp->lock, flags); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1025 | |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 1026 | return NETDEV_TX_OK; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1027 | } |
| 1028 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1029 | static void macb_init_rx_buffer_size(struct macb *bp, size_t size) |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1030 | { |
| 1031 | if (!macb_is_gem(bp)) { |
| 1032 | bp->rx_buffer_size = MACB_RX_BUFFER_SIZE; |
| 1033 | } else { |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1034 | bp->rx_buffer_size = size; |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1035 | |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1036 | if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) { |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1037 | netdev_dbg(bp->dev, |
| 1038 | "RX buffer must be multiple of %d bytes, expanding\n", |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1039 | RX_BUFFER_MULTIPLE); |
| 1040 | bp->rx_buffer_size = |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1041 | roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE); |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1042 | } |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1043 | } |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1044 | |
| 1045 | netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n", |
| 1046 | bp->dev->mtu, bp->rx_buffer_size); |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1049 | static void gem_free_rx_buffers(struct macb *bp) |
| 1050 | { |
| 1051 | struct sk_buff *skb; |
| 1052 | struct macb_dma_desc *desc; |
| 1053 | dma_addr_t addr; |
| 1054 | int i; |
| 1055 | |
| 1056 | if (!bp->rx_skbuff) |
| 1057 | return; |
| 1058 | |
| 1059 | for (i = 0; i < RX_RING_SIZE; i++) { |
| 1060 | skb = bp->rx_skbuff[i]; |
| 1061 | |
| 1062 | if (skb == NULL) |
| 1063 | continue; |
| 1064 | |
| 1065 | desc = &bp->rx_ring[i]; |
| 1066 | addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr)); |
| 1067 | dma_unmap_single(&bp->pdev->dev, addr, skb->len, |
| 1068 | DMA_FROM_DEVICE); |
| 1069 | dev_kfree_skb_any(skb); |
| 1070 | skb = NULL; |
| 1071 | } |
| 1072 | |
| 1073 | kfree(bp->rx_skbuff); |
| 1074 | bp->rx_skbuff = NULL; |
| 1075 | } |
| 1076 | |
| 1077 | static void macb_free_rx_buffers(struct macb *bp) |
| 1078 | { |
| 1079 | if (bp->rx_buffers) { |
| 1080 | dma_free_coherent(&bp->pdev->dev, |
| 1081 | RX_RING_SIZE * bp->rx_buffer_size, |
| 1082 | bp->rx_buffers, bp->rx_buffers_dma); |
| 1083 | bp->rx_buffers = NULL; |
| 1084 | } |
| 1085 | } |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1086 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1087 | static void macb_free_consistent(struct macb *bp) |
| 1088 | { |
| 1089 | if (bp->tx_skb) { |
| 1090 | kfree(bp->tx_skb); |
| 1091 | bp->tx_skb = NULL; |
| 1092 | } |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1093 | bp->macbgem_ops.mog_free_rx_buffers(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1094 | if (bp->rx_ring) { |
| 1095 | dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES, |
| 1096 | bp->rx_ring, bp->rx_ring_dma); |
| 1097 | bp->rx_ring = NULL; |
| 1098 | } |
| 1099 | if (bp->tx_ring) { |
| 1100 | dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES, |
| 1101 | bp->tx_ring, bp->tx_ring_dma); |
| 1102 | bp->tx_ring = NULL; |
| 1103 | } |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | static int gem_alloc_rx_buffers(struct macb *bp) |
| 1107 | { |
| 1108 | int size; |
| 1109 | |
| 1110 | size = RX_RING_SIZE * sizeof(struct sk_buff *); |
| 1111 | bp->rx_skbuff = kzalloc(size, GFP_KERNEL); |
| 1112 | if (!bp->rx_skbuff) |
| 1113 | return -ENOMEM; |
| 1114 | else |
| 1115 | netdev_dbg(bp->dev, |
| 1116 | "Allocated %d RX struct sk_buff entries at %p\n", |
| 1117 | RX_RING_SIZE, bp->rx_skbuff); |
| 1118 | return 0; |
| 1119 | } |
| 1120 | |
| 1121 | static int macb_alloc_rx_buffers(struct macb *bp) |
| 1122 | { |
| 1123 | int size; |
| 1124 | |
| 1125 | size = RX_RING_SIZE * bp->rx_buffer_size; |
| 1126 | bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size, |
| 1127 | &bp->rx_buffers_dma, GFP_KERNEL); |
| 1128 | if (!bp->rx_buffers) |
| 1129 | return -ENOMEM; |
| 1130 | else |
| 1131 | netdev_dbg(bp->dev, |
| 1132 | "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n", |
| 1133 | size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers); |
| 1134 | return 0; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | static int macb_alloc_consistent(struct macb *bp) |
| 1138 | { |
| 1139 | int size; |
| 1140 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 1141 | size = TX_RING_SIZE * sizeof(struct macb_tx_skb); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1142 | bp->tx_skb = kmalloc(size, GFP_KERNEL); |
| 1143 | if (!bp->tx_skb) |
| 1144 | goto out_err; |
| 1145 | |
| 1146 | size = RX_RING_BYTES; |
| 1147 | bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size, |
| 1148 | &bp->rx_ring_dma, GFP_KERNEL); |
| 1149 | if (!bp->rx_ring) |
| 1150 | goto out_err; |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 1151 | netdev_dbg(bp->dev, |
| 1152 | "Allocated RX ring of %d bytes at %08lx (mapped %p)\n", |
| 1153 | size, (unsigned long)bp->rx_ring_dma, bp->rx_ring); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1154 | |
| 1155 | size = TX_RING_BYTES; |
| 1156 | bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size, |
| 1157 | &bp->tx_ring_dma, GFP_KERNEL); |
| 1158 | if (!bp->tx_ring) |
| 1159 | goto out_err; |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 1160 | netdev_dbg(bp->dev, |
| 1161 | "Allocated TX ring of %d bytes at %08lx (mapped %p)\n", |
| 1162 | size, (unsigned long)bp->tx_ring_dma, bp->tx_ring); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1163 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1164 | if (bp->macbgem_ops.mog_alloc_rx_buffers(bp)) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1165 | goto out_err; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1166 | |
| 1167 | return 0; |
| 1168 | |
| 1169 | out_err: |
| 1170 | macb_free_consistent(bp); |
| 1171 | return -ENOMEM; |
| 1172 | } |
| 1173 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1174 | static void gem_init_rings(struct macb *bp) |
| 1175 | { |
| 1176 | int i; |
| 1177 | |
| 1178 | for (i = 0; i < TX_RING_SIZE; i++) { |
| 1179 | bp->tx_ring[i].addr = 0; |
| 1180 | bp->tx_ring[i].ctrl = MACB_BIT(TX_USED); |
| 1181 | } |
| 1182 | bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP); |
| 1183 | |
| 1184 | bp->rx_tail = bp->rx_prepared_head = bp->tx_head = bp->tx_tail = 0; |
| 1185 | |
| 1186 | gem_rx_refill(bp); |
| 1187 | } |
| 1188 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1189 | static void macb_init_rings(struct macb *bp) |
| 1190 | { |
| 1191 | int i; |
| 1192 | dma_addr_t addr; |
| 1193 | |
| 1194 | addr = bp->rx_buffers_dma; |
| 1195 | for (i = 0; i < RX_RING_SIZE; i++) { |
| 1196 | bp->rx_ring[i].addr = addr; |
| 1197 | bp->rx_ring[i].ctrl = 0; |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1198 | addr += bp->rx_buffer_size; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1199 | } |
| 1200 | bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP); |
| 1201 | |
| 1202 | for (i = 0; i < TX_RING_SIZE; i++) { |
| 1203 | bp->tx_ring[i].addr = 0; |
| 1204 | bp->tx_ring[i].ctrl = MACB_BIT(TX_USED); |
| 1205 | } |
| 1206 | bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP); |
| 1207 | |
| 1208 | bp->rx_tail = bp->tx_head = bp->tx_tail = 0; |
| 1209 | } |
| 1210 | |
| 1211 | static void macb_reset_hw(struct macb *bp) |
| 1212 | { |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1213 | /* |
| 1214 | * Disable RX and TX (XXX: Should we halt the transmission |
| 1215 | * more gracefully?) |
| 1216 | */ |
| 1217 | macb_writel(bp, NCR, 0); |
| 1218 | |
| 1219 | /* Clear the stats registers (XXX: Update stats first?) */ |
| 1220 | macb_writel(bp, NCR, MACB_BIT(CLRSTAT)); |
| 1221 | |
| 1222 | /* Clear all status flags */ |
Joachim Eastwood | 95ebcea | 2012-10-22 08:45:31 +0000 | [diff] [blame] | 1223 | macb_writel(bp, TSR, -1); |
| 1224 | macb_writel(bp, RSR, -1); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1225 | |
| 1226 | /* Disable all interrupts */ |
Joachim Eastwood | 95ebcea | 2012-10-22 08:45:31 +0000 | [diff] [blame] | 1227 | macb_writel(bp, IDR, -1); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1228 | macb_readl(bp, ISR); |
| 1229 | } |
| 1230 | |
Jamie Iles | 70c9f3d | 2011-03-09 16:22:54 +0000 | [diff] [blame] | 1231 | static u32 gem_mdc_clk_div(struct macb *bp) |
| 1232 | { |
| 1233 | u32 config; |
| 1234 | unsigned long pclk_hz = clk_get_rate(bp->pclk); |
| 1235 | |
| 1236 | if (pclk_hz <= 20000000) |
| 1237 | config = GEM_BF(CLK, GEM_CLK_DIV8); |
| 1238 | else if (pclk_hz <= 40000000) |
| 1239 | config = GEM_BF(CLK, GEM_CLK_DIV16); |
| 1240 | else if (pclk_hz <= 80000000) |
| 1241 | config = GEM_BF(CLK, GEM_CLK_DIV32); |
| 1242 | else if (pclk_hz <= 120000000) |
| 1243 | config = GEM_BF(CLK, GEM_CLK_DIV48); |
| 1244 | else if (pclk_hz <= 160000000) |
| 1245 | config = GEM_BF(CLK, GEM_CLK_DIV64); |
| 1246 | else |
| 1247 | config = GEM_BF(CLK, GEM_CLK_DIV96); |
| 1248 | |
| 1249 | return config; |
| 1250 | } |
| 1251 | |
| 1252 | static u32 macb_mdc_clk_div(struct macb *bp) |
| 1253 | { |
| 1254 | u32 config; |
| 1255 | unsigned long pclk_hz; |
| 1256 | |
| 1257 | if (macb_is_gem(bp)) |
| 1258 | return gem_mdc_clk_div(bp); |
| 1259 | |
| 1260 | pclk_hz = clk_get_rate(bp->pclk); |
| 1261 | if (pclk_hz <= 20000000) |
| 1262 | config = MACB_BF(CLK, MACB_CLK_DIV8); |
| 1263 | else if (pclk_hz <= 40000000) |
| 1264 | config = MACB_BF(CLK, MACB_CLK_DIV16); |
| 1265 | else if (pclk_hz <= 80000000) |
| 1266 | config = MACB_BF(CLK, MACB_CLK_DIV32); |
| 1267 | else |
| 1268 | config = MACB_BF(CLK, MACB_CLK_DIV64); |
| 1269 | |
| 1270 | return config; |
| 1271 | } |
| 1272 | |
Jamie Iles | 757a03c | 2011-03-09 16:29:59 +0000 | [diff] [blame] | 1273 | /* |
| 1274 | * Get the DMA bus width field of the network configuration register that we |
| 1275 | * should program. We find the width from decoding the design configuration |
| 1276 | * register to find the maximum supported data bus width. |
| 1277 | */ |
| 1278 | static u32 macb_dbw(struct macb *bp) |
| 1279 | { |
| 1280 | if (!macb_is_gem(bp)) |
| 1281 | return 0; |
| 1282 | |
| 1283 | switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) { |
| 1284 | case 4: |
| 1285 | return GEM_BF(DBW, GEM_DBW128); |
| 1286 | case 2: |
| 1287 | return GEM_BF(DBW, GEM_DBW64); |
| 1288 | case 1: |
| 1289 | default: |
| 1290 | return GEM_BF(DBW, GEM_DBW32); |
| 1291 | } |
| 1292 | } |
| 1293 | |
Jamie Iles | 0116da4 | 2011-03-14 17:38:30 +0000 | [diff] [blame] | 1294 | /* |
Nicolas Ferre | b3e3bd71 | 2012-11-23 03:49:01 +0000 | [diff] [blame] | 1295 | * Configure the receive DMA engine |
| 1296 | * - use the correct receive buffer size |
| 1297 | * - set the possibility to use INCR16 bursts |
| 1298 | * (if not supported by FIFO, it will fallback to default) |
| 1299 | * - set both rx/tx packet buffers to full memory size |
| 1300 | * These are configurable parameters for GEM. |
Jamie Iles | 0116da4 | 2011-03-14 17:38:30 +0000 | [diff] [blame] | 1301 | */ |
| 1302 | static void macb_configure_dma(struct macb *bp) |
| 1303 | { |
| 1304 | u32 dmacfg; |
| 1305 | |
| 1306 | if (macb_is_gem(bp)) { |
| 1307 | dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L); |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1308 | dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE); |
Nicolas Ferre | b3e3bd71 | 2012-11-23 03:49:01 +0000 | [diff] [blame] | 1309 | dmacfg |= GEM_BF(FBLDO, 16); |
| 1310 | dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L); |
Steffen Trumtrar | a1ae385 | 2013-03-27 23:07:06 +0000 | [diff] [blame] | 1311 | dmacfg &= ~GEM_BIT(ENDIA); |
Jamie Iles | 0116da4 | 2011-03-14 17:38:30 +0000 | [diff] [blame] | 1312 | gem_writel(bp, DMACFG, dmacfg); |
| 1313 | } |
| 1314 | } |
| 1315 | |
Nicolas Ferre | 581df9e | 2013-05-14 03:00:16 +0000 | [diff] [blame] | 1316 | /* |
| 1317 | * Configure peripheral capacities according to integration options used |
| 1318 | */ |
| 1319 | static void macb_configure_caps(struct macb *bp) |
| 1320 | { |
| 1321 | if (macb_is_gem(bp)) { |
Jongsung Kim | 01276ed | 2013-07-09 17:36:00 +0900 | [diff] [blame] | 1322 | if (GEM_BFEXT(IRQCOR, gem_readl(bp, DCFG1)) == 0) |
Nicolas Ferre | 581df9e | 2013-05-14 03:00:16 +0000 | [diff] [blame] | 1323 | bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE; |
| 1324 | } |
| 1325 | } |
| 1326 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1327 | static void macb_init_hw(struct macb *bp) |
| 1328 | { |
| 1329 | u32 config; |
| 1330 | |
| 1331 | macb_reset_hw(bp); |
Joachim Eastwood | 314bccc | 2012-11-07 08:14:52 +0000 | [diff] [blame] | 1332 | macb_set_hwaddr(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1333 | |
Jamie Iles | 70c9f3d | 2011-03-09 16:22:54 +0000 | [diff] [blame] | 1334 | config = macb_mdc_clk_div(bp); |
Havard Skinnemoen | 29bc2e1 | 2012-10-31 06:04:58 +0000 | [diff] [blame] | 1335 | config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */ |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1336 | config |= MACB_BIT(PAE); /* PAuse Enable */ |
| 1337 | config |= MACB_BIT(DRFCS); /* Discard Rx FCS */ |
Peter Korsgaard | 8dd4bd0 | 2010-04-07 21:53:41 -0700 | [diff] [blame] | 1338 | config |= MACB_BIT(BIG); /* Receive oversized frames */ |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1339 | if (bp->dev->flags & IFF_PROMISC) |
| 1340 | config |= MACB_BIT(CAF); /* Copy All Frames */ |
| 1341 | if (!(bp->dev->flags & IFF_BROADCAST)) |
| 1342 | config |= MACB_BIT(NBC); /* No BroadCast */ |
Jamie Iles | 757a03c | 2011-03-09 16:29:59 +0000 | [diff] [blame] | 1343 | config |= macb_dbw(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1344 | macb_writel(bp, NCFGR, config); |
Vitalii Demianets | 26cdfb4 | 2012-11-02 07:09:24 +0000 | [diff] [blame] | 1345 | bp->speed = SPEED_10; |
| 1346 | bp->duplex = DUPLEX_HALF; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1347 | |
Jamie Iles | 0116da4 | 2011-03-14 17:38:30 +0000 | [diff] [blame] | 1348 | macb_configure_dma(bp); |
Nicolas Ferre | 581df9e | 2013-05-14 03:00:16 +0000 | [diff] [blame] | 1349 | macb_configure_caps(bp); |
Jamie Iles | 0116da4 | 2011-03-14 17:38:30 +0000 | [diff] [blame] | 1350 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1351 | /* Initialize TX and RX buffers */ |
| 1352 | macb_writel(bp, RBQP, bp->rx_ring_dma); |
| 1353 | macb_writel(bp, TBQP, bp->tx_ring_dma); |
| 1354 | |
| 1355 | /* Enable TX and RX */ |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1356 | macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE)); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1357 | |
| 1358 | /* Enable interrupts */ |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1359 | macb_writel(bp, IER, (MACB_RX_INT_FLAGS |
| 1360 | | MACB_TX_INT_FLAGS |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1361 | | MACB_BIT(HRESP))); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1362 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1363 | } |
| 1364 | |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1365 | /* |
| 1366 | * The hash address register is 64 bits long and takes up two |
| 1367 | * locations in the memory map. The least significant bits are stored |
| 1368 | * in EMAC_HSL and the most significant bits in EMAC_HSH. |
| 1369 | * |
| 1370 | * The unicast hash enable and the multicast hash enable bits in the |
| 1371 | * network configuration register enable the reception of hash matched |
| 1372 | * frames. The destination address is reduced to a 6 bit index into |
| 1373 | * the 64 bit hash register using the following hash function. The |
| 1374 | * hash function is an exclusive or of every sixth bit of the |
| 1375 | * destination address. |
| 1376 | * |
| 1377 | * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47] |
| 1378 | * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46] |
| 1379 | * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45] |
| 1380 | * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44] |
| 1381 | * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43] |
| 1382 | * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42] |
| 1383 | * |
| 1384 | * da[0] represents the least significant bit of the first byte |
| 1385 | * received, that is, the multicast/unicast indicator, and da[47] |
| 1386 | * represents the most significant bit of the last byte received. If |
| 1387 | * the hash index, hi[n], points to a bit that is set in the hash |
| 1388 | * register then the frame will be matched according to whether the |
| 1389 | * frame is multicast or unicast. A multicast match will be signalled |
| 1390 | * if the multicast hash enable bit is set, da[0] is 1 and the hash |
| 1391 | * index points to a bit set in the hash register. A unicast match |
| 1392 | * will be signalled if the unicast hash enable bit is set, da[0] is 0 |
| 1393 | * and the hash index points to a bit set in the hash register. To |
| 1394 | * receive all multicast frames, the hash register should be set with |
| 1395 | * all ones and the multicast hash enable bit should be set in the |
| 1396 | * network configuration register. |
| 1397 | */ |
| 1398 | |
| 1399 | static inline int hash_bit_value(int bitnr, __u8 *addr) |
| 1400 | { |
| 1401 | if (addr[bitnr / 8] & (1 << (bitnr % 8))) |
| 1402 | return 1; |
| 1403 | return 0; |
| 1404 | } |
| 1405 | |
| 1406 | /* |
| 1407 | * Return the hash index value for the specified address. |
| 1408 | */ |
| 1409 | static int hash_get_index(__u8 *addr) |
| 1410 | { |
| 1411 | int i, j, bitval; |
| 1412 | int hash_index = 0; |
| 1413 | |
| 1414 | for (j = 0; j < 6; j++) { |
| 1415 | for (i = 0, bitval = 0; i < 8; i++) |
| 1416 | bitval ^= hash_bit_value(i*6 + j, addr); |
| 1417 | |
| 1418 | hash_index |= (bitval << j); |
| 1419 | } |
| 1420 | |
| 1421 | return hash_index; |
| 1422 | } |
| 1423 | |
| 1424 | /* |
| 1425 | * Add multicast addresses to the internal multicast-hash table. |
| 1426 | */ |
| 1427 | static void macb_sethashtable(struct net_device *dev) |
| 1428 | { |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 1429 | struct netdev_hw_addr *ha; |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1430 | unsigned long mc_filter[2]; |
Jiri Pirko | f9dcbcc | 2010-02-23 09:19:49 +0000 | [diff] [blame] | 1431 | unsigned int bitnr; |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1432 | struct macb *bp = netdev_priv(dev); |
| 1433 | |
| 1434 | mc_filter[0] = mc_filter[1] = 0; |
| 1435 | |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 1436 | netdev_for_each_mc_addr(ha, dev) { |
| 1437 | bitnr = hash_get_index(ha->addr); |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1438 | mc_filter[bitnr >> 5] |= 1 << (bitnr & 31); |
| 1439 | } |
| 1440 | |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1441 | macb_or_gem_writel(bp, HRB, mc_filter[0]); |
| 1442 | macb_or_gem_writel(bp, HRT, mc_filter[1]); |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | /* |
| 1446 | * Enable/Disable promiscuous and multicast modes. |
| 1447 | */ |
Joachim Eastwood | e0da1f1 | 2012-10-18 11:01:15 +0000 | [diff] [blame] | 1448 | void macb_set_rx_mode(struct net_device *dev) |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1449 | { |
| 1450 | unsigned long cfg; |
| 1451 | struct macb *bp = netdev_priv(dev); |
| 1452 | |
| 1453 | cfg = macb_readl(bp, NCFGR); |
| 1454 | |
| 1455 | if (dev->flags & IFF_PROMISC) |
| 1456 | /* Enable promiscuous mode */ |
| 1457 | cfg |= MACB_BIT(CAF); |
| 1458 | else if (dev->flags & (~IFF_PROMISC)) |
| 1459 | /* Disable promiscuous mode */ |
| 1460 | cfg &= ~MACB_BIT(CAF); |
| 1461 | |
| 1462 | if (dev->flags & IFF_ALLMULTI) { |
| 1463 | /* Enable all multicast mode */ |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1464 | macb_or_gem_writel(bp, HRB, -1); |
| 1465 | macb_or_gem_writel(bp, HRT, -1); |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1466 | cfg |= MACB_BIT(NCFGR_MTI); |
Jiri Pirko | 4cd24ea | 2010-02-08 04:30:35 +0000 | [diff] [blame] | 1467 | } else if (!netdev_mc_empty(dev)) { |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1468 | /* Enable specific multicasts */ |
| 1469 | macb_sethashtable(dev); |
| 1470 | cfg |= MACB_BIT(NCFGR_MTI); |
| 1471 | } else if (dev->flags & (~IFF_ALLMULTI)) { |
| 1472 | /* Disable all multicast mode */ |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1473 | macb_or_gem_writel(bp, HRB, 0); |
| 1474 | macb_or_gem_writel(bp, HRT, 0); |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1475 | cfg &= ~MACB_BIT(NCFGR_MTI); |
| 1476 | } |
| 1477 | |
| 1478 | macb_writel(bp, NCFGR, cfg); |
| 1479 | } |
Joachim Eastwood | e0da1f1 | 2012-10-18 11:01:15 +0000 | [diff] [blame] | 1480 | EXPORT_SYMBOL_GPL(macb_set_rx_mode); |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1481 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1482 | static int macb_open(struct net_device *dev) |
| 1483 | { |
| 1484 | struct macb *bp = netdev_priv(dev); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1485 | size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1486 | int err; |
| 1487 | |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 1488 | netdev_dbg(bp->dev, "open\n"); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1489 | |
Nicolas Ferre | 03fc472 | 2012-07-03 23:14:13 +0000 | [diff] [blame] | 1490 | /* carrier starts down */ |
| 1491 | netif_carrier_off(dev); |
| 1492 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1493 | /* if the phy is not yet register, retry later*/ |
| 1494 | if (!bp->phy_dev) |
| 1495 | return -EAGAIN; |
| 1496 | |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1497 | /* RX buffers initialization */ |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1498 | macb_init_rx_buffer_size(bp, bufsz); |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1499 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1500 | err = macb_alloc_consistent(bp); |
| 1501 | if (err) { |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 1502 | netdev_err(dev, "Unable to allocate DMA memory (error %d)\n", |
| 1503 | err); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1504 | return err; |
| 1505 | } |
| 1506 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1507 | napi_enable(&bp->napi); |
| 1508 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1509 | bp->macbgem_ops.mog_init_rings(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1510 | macb_init_hw(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1511 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1512 | /* schedule a link state check */ |
| 1513 | phy_start(bp->phy_dev); |
| 1514 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1515 | netif_start_queue(dev); |
| 1516 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1517 | return 0; |
| 1518 | } |
| 1519 | |
| 1520 | static int macb_close(struct net_device *dev) |
| 1521 | { |
| 1522 | struct macb *bp = netdev_priv(dev); |
| 1523 | unsigned long flags; |
| 1524 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1525 | netif_stop_queue(dev); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1526 | napi_disable(&bp->napi); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1527 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1528 | if (bp->phy_dev) |
| 1529 | phy_stop(bp->phy_dev); |
| 1530 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1531 | spin_lock_irqsave(&bp->lock, flags); |
| 1532 | macb_reset_hw(bp); |
| 1533 | netif_carrier_off(dev); |
| 1534 | spin_unlock_irqrestore(&bp->lock, flags); |
| 1535 | |
| 1536 | macb_free_consistent(bp); |
| 1537 | |
| 1538 | return 0; |
| 1539 | } |
| 1540 | |
Jamie Iles | a494ed8 | 2011-03-09 16:26:35 +0000 | [diff] [blame] | 1541 | static void gem_update_stats(struct macb *bp) |
| 1542 | { |
| 1543 | u32 __iomem *reg = bp->regs + GEM_OTX; |
| 1544 | u32 *p = &bp->hw_stats.gem.tx_octets_31_0; |
| 1545 | u32 *end = &bp->hw_stats.gem.rx_udp_checksum_errors + 1; |
| 1546 | |
| 1547 | for (; p < end; p++, reg++) |
| 1548 | *p += __raw_readl(reg); |
| 1549 | } |
| 1550 | |
| 1551 | static struct net_device_stats *gem_get_stats(struct macb *bp) |
| 1552 | { |
| 1553 | struct gem_stats *hwstat = &bp->hw_stats.gem; |
| 1554 | struct net_device_stats *nstat = &bp->stats; |
| 1555 | |
| 1556 | gem_update_stats(bp); |
| 1557 | |
| 1558 | nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors + |
| 1559 | hwstat->rx_alignment_errors + |
| 1560 | hwstat->rx_resource_errors + |
| 1561 | hwstat->rx_overruns + |
| 1562 | hwstat->rx_oversize_frames + |
| 1563 | hwstat->rx_jabbers + |
| 1564 | hwstat->rx_undersized_frames + |
| 1565 | hwstat->rx_length_field_frame_errors); |
| 1566 | nstat->tx_errors = (hwstat->tx_late_collisions + |
| 1567 | hwstat->tx_excessive_collisions + |
| 1568 | hwstat->tx_underrun + |
| 1569 | hwstat->tx_carrier_sense_errors); |
| 1570 | nstat->multicast = hwstat->rx_multicast_frames; |
| 1571 | nstat->collisions = (hwstat->tx_single_collision_frames + |
| 1572 | hwstat->tx_multiple_collision_frames + |
| 1573 | hwstat->tx_excessive_collisions); |
| 1574 | nstat->rx_length_errors = (hwstat->rx_oversize_frames + |
| 1575 | hwstat->rx_jabbers + |
| 1576 | hwstat->rx_undersized_frames + |
| 1577 | hwstat->rx_length_field_frame_errors); |
| 1578 | nstat->rx_over_errors = hwstat->rx_resource_errors; |
| 1579 | nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors; |
| 1580 | nstat->rx_frame_errors = hwstat->rx_alignment_errors; |
| 1581 | nstat->rx_fifo_errors = hwstat->rx_overruns; |
| 1582 | nstat->tx_aborted_errors = hwstat->tx_excessive_collisions; |
| 1583 | nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors; |
| 1584 | nstat->tx_fifo_errors = hwstat->tx_underrun; |
| 1585 | |
| 1586 | return nstat; |
| 1587 | } |
| 1588 | |
Joachim Eastwood | 2ea32ee | 2012-11-07 08:14:54 +0000 | [diff] [blame] | 1589 | struct net_device_stats *macb_get_stats(struct net_device *dev) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1590 | { |
| 1591 | struct macb *bp = netdev_priv(dev); |
| 1592 | struct net_device_stats *nstat = &bp->stats; |
Jamie Iles | a494ed8 | 2011-03-09 16:26:35 +0000 | [diff] [blame] | 1593 | struct macb_stats *hwstat = &bp->hw_stats.macb; |
| 1594 | |
| 1595 | if (macb_is_gem(bp)) |
| 1596 | return gem_get_stats(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1597 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1598 | /* read stats from hardware */ |
| 1599 | macb_update_stats(bp); |
| 1600 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1601 | /* Convert HW stats into netdevice stats */ |
| 1602 | nstat->rx_errors = (hwstat->rx_fcs_errors + |
| 1603 | hwstat->rx_align_errors + |
| 1604 | hwstat->rx_resource_errors + |
| 1605 | hwstat->rx_overruns + |
| 1606 | hwstat->rx_oversize_pkts + |
| 1607 | hwstat->rx_jabbers + |
| 1608 | hwstat->rx_undersize_pkts + |
| 1609 | hwstat->sqe_test_errors + |
| 1610 | hwstat->rx_length_mismatch); |
| 1611 | nstat->tx_errors = (hwstat->tx_late_cols + |
| 1612 | hwstat->tx_excessive_cols + |
| 1613 | hwstat->tx_underruns + |
| 1614 | hwstat->tx_carrier_errors); |
| 1615 | nstat->collisions = (hwstat->tx_single_cols + |
| 1616 | hwstat->tx_multiple_cols + |
| 1617 | hwstat->tx_excessive_cols); |
| 1618 | nstat->rx_length_errors = (hwstat->rx_oversize_pkts + |
| 1619 | hwstat->rx_jabbers + |
| 1620 | hwstat->rx_undersize_pkts + |
| 1621 | hwstat->rx_length_mismatch); |
Alexander Stein | b19f7f7 | 2011-04-13 05:03:24 +0000 | [diff] [blame] | 1622 | nstat->rx_over_errors = hwstat->rx_resource_errors + |
| 1623 | hwstat->rx_overruns; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1624 | nstat->rx_crc_errors = hwstat->rx_fcs_errors; |
| 1625 | nstat->rx_frame_errors = hwstat->rx_align_errors; |
| 1626 | nstat->rx_fifo_errors = hwstat->rx_overruns; |
| 1627 | /* XXX: What does "missed" mean? */ |
| 1628 | nstat->tx_aborted_errors = hwstat->tx_excessive_cols; |
| 1629 | nstat->tx_carrier_errors = hwstat->tx_carrier_errors; |
| 1630 | nstat->tx_fifo_errors = hwstat->tx_underruns; |
| 1631 | /* Don't know about heartbeat or window errors... */ |
| 1632 | |
| 1633 | return nstat; |
| 1634 | } |
Joachim Eastwood | 2ea32ee | 2012-11-07 08:14:54 +0000 | [diff] [blame] | 1635 | EXPORT_SYMBOL_GPL(macb_get_stats); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1636 | |
| 1637 | static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 1638 | { |
| 1639 | struct macb *bp = netdev_priv(dev); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1640 | struct phy_device *phydev = bp->phy_dev; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1641 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1642 | if (!phydev) |
| 1643 | return -ENODEV; |
| 1644 | |
| 1645 | return phy_ethtool_gset(phydev, cmd); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 1649 | { |
| 1650 | struct macb *bp = netdev_priv(dev); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1651 | struct phy_device *phydev = bp->phy_dev; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1652 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1653 | if (!phydev) |
| 1654 | return -ENODEV; |
| 1655 | |
| 1656 | return phy_ethtool_sset(phydev, cmd); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1657 | } |
| 1658 | |
Nicolas Ferre | d1d1b53 | 2012-10-31 06:04:56 +0000 | [diff] [blame] | 1659 | static int macb_get_regs_len(struct net_device *netdev) |
| 1660 | { |
| 1661 | return MACB_GREGS_NBR * sizeof(u32); |
| 1662 | } |
| 1663 | |
| 1664 | static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs, |
| 1665 | void *p) |
| 1666 | { |
| 1667 | struct macb *bp = netdev_priv(dev); |
| 1668 | unsigned int tail, head; |
| 1669 | u32 *regs_buff = p; |
| 1670 | |
| 1671 | regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1)) |
| 1672 | | MACB_GREGS_VERSION; |
| 1673 | |
| 1674 | tail = macb_tx_ring_wrap(bp->tx_tail); |
| 1675 | head = macb_tx_ring_wrap(bp->tx_head); |
| 1676 | |
| 1677 | regs_buff[0] = macb_readl(bp, NCR); |
| 1678 | regs_buff[1] = macb_or_gem_readl(bp, NCFGR); |
| 1679 | regs_buff[2] = macb_readl(bp, NSR); |
| 1680 | regs_buff[3] = macb_readl(bp, TSR); |
| 1681 | regs_buff[4] = macb_readl(bp, RBQP); |
| 1682 | regs_buff[5] = macb_readl(bp, TBQP); |
| 1683 | regs_buff[6] = macb_readl(bp, RSR); |
| 1684 | regs_buff[7] = macb_readl(bp, IMR); |
| 1685 | |
| 1686 | regs_buff[8] = tail; |
| 1687 | regs_buff[9] = head; |
| 1688 | regs_buff[10] = macb_tx_dma(bp, tail); |
| 1689 | regs_buff[11] = macb_tx_dma(bp, head); |
| 1690 | |
| 1691 | if (macb_is_gem(bp)) { |
| 1692 | regs_buff[12] = gem_readl(bp, USRIO); |
| 1693 | regs_buff[13] = gem_readl(bp, DMACFG); |
| 1694 | } |
| 1695 | } |
| 1696 | |
Joachim Eastwood | 0005f54 | 2012-10-18 11:01:12 +0000 | [diff] [blame] | 1697 | const struct ethtool_ops macb_ethtool_ops = { |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1698 | .get_settings = macb_get_settings, |
| 1699 | .set_settings = macb_set_settings, |
Nicolas Ferre | d1d1b53 | 2012-10-31 06:04:56 +0000 | [diff] [blame] | 1700 | .get_regs_len = macb_get_regs_len, |
| 1701 | .get_regs = macb_get_regs, |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1702 | .get_link = ethtool_op_get_link, |
Richard Cochran | 17f393e | 2012-04-03 22:59:31 +0000 | [diff] [blame] | 1703 | .get_ts_info = ethtool_op_get_ts_info, |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1704 | }; |
Joachim Eastwood | 0005f54 | 2012-10-18 11:01:12 +0000 | [diff] [blame] | 1705 | EXPORT_SYMBOL_GPL(macb_ethtool_ops); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1706 | |
Joachim Eastwood | 0005f54 | 2012-10-18 11:01:12 +0000 | [diff] [blame] | 1707 | int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1708 | { |
| 1709 | struct macb *bp = netdev_priv(dev); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1710 | struct phy_device *phydev = bp->phy_dev; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1711 | |
| 1712 | if (!netif_running(dev)) |
| 1713 | return -EINVAL; |
| 1714 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1715 | if (!phydev) |
| 1716 | return -ENODEV; |
| 1717 | |
Richard Cochran | 28b0411 | 2010-07-17 08:48:55 +0000 | [diff] [blame] | 1718 | return phy_mii_ioctl(phydev, rq, cmd); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1719 | } |
Joachim Eastwood | 0005f54 | 2012-10-18 11:01:12 +0000 | [diff] [blame] | 1720 | EXPORT_SYMBOL_GPL(macb_ioctl); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1721 | |
Alexander Beregalov | 5f1fa99 | 2009-04-11 07:42:26 +0000 | [diff] [blame] | 1722 | static const struct net_device_ops macb_netdev_ops = { |
| 1723 | .ndo_open = macb_open, |
| 1724 | .ndo_stop = macb_close, |
| 1725 | .ndo_start_xmit = macb_start_xmit, |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame] | 1726 | .ndo_set_rx_mode = macb_set_rx_mode, |
Alexander Beregalov | 5f1fa99 | 2009-04-11 07:42:26 +0000 | [diff] [blame] | 1727 | .ndo_get_stats = macb_get_stats, |
| 1728 | .ndo_do_ioctl = macb_ioctl, |
| 1729 | .ndo_validate_addr = eth_validate_addr, |
| 1730 | .ndo_change_mtu = eth_change_mtu, |
| 1731 | .ndo_set_mac_address = eth_mac_addr, |
Thomas Petazzoni | 6e8cf5c | 2009-05-04 11:08:41 -0700 | [diff] [blame] | 1732 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1733 | .ndo_poll_controller = macb_poll_controller, |
| 1734 | #endif |
Alexander Beregalov | 5f1fa99 | 2009-04-11 07:42:26 +0000 | [diff] [blame] | 1735 | }; |
| 1736 | |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 1737 | #if defined(CONFIG_OF) |
| 1738 | static const struct of_device_id macb_dt_ids[] = { |
| 1739 | { .compatible = "cdns,at32ap7000-macb" }, |
| 1740 | { .compatible = "cdns,at91sam9260-macb" }, |
| 1741 | { .compatible = "cdns,macb" }, |
| 1742 | { .compatible = "cdns,pc302-gem" }, |
| 1743 | { .compatible = "cdns,gem" }, |
| 1744 | { /* sentinel */ } |
| 1745 | }; |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 1746 | MODULE_DEVICE_TABLE(of, macb_dt_ids); |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 1747 | #endif |
| 1748 | |
Haavard Skinnemoen | 06c3fd6 | 2008-01-31 13:10:22 +0100 | [diff] [blame] | 1749 | static int __init macb_probe(struct platform_device *pdev) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1750 | { |
Jamie Iles | 84e0cdb | 2011-03-08 20:17:06 +0000 | [diff] [blame] | 1751 | struct macb_platform_data *pdata; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1752 | struct resource *regs; |
| 1753 | struct net_device *dev; |
| 1754 | struct macb *bp; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1755 | struct phy_device *phydev; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1756 | u32 config; |
| 1757 | int err = -ENXIO; |
Jean-Christophe PLAGNIOL-VILLARD | 8ef29f8a | 2012-10-31 06:04:59 +0000 | [diff] [blame] | 1758 | struct pinctrl *pinctrl; |
Guenter Roeck | 5090704 | 2013-04-02 09:35:09 +0000 | [diff] [blame] | 1759 | const char *mac; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1760 | |
| 1761 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1762 | if (!regs) { |
| 1763 | dev_err(&pdev->dev, "no mmio resource defined\n"); |
| 1764 | goto err_out; |
| 1765 | } |
| 1766 | |
Jean-Christophe PLAGNIOL-VILLARD | 8ef29f8a | 2012-10-31 06:04:59 +0000 | [diff] [blame] | 1767 | pinctrl = devm_pinctrl_get_select_default(&pdev->dev); |
| 1768 | if (IS_ERR(pinctrl)) { |
| 1769 | err = PTR_ERR(pinctrl); |
| 1770 | if (err == -EPROBE_DEFER) |
| 1771 | goto err_out; |
| 1772 | |
| 1773 | dev_warn(&pdev->dev, "No pinctrl provided\n"); |
| 1774 | } |
| 1775 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1776 | err = -ENOMEM; |
| 1777 | dev = alloc_etherdev(sizeof(*bp)); |
Joe Perches | 41de8d4 | 2012-01-29 13:47:52 +0000 | [diff] [blame] | 1778 | if (!dev) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1779 | goto err_out; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1780 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1781 | SET_NETDEV_DEV(dev, &pdev->dev); |
| 1782 | |
| 1783 | /* TODO: Actually, we have some interesting features... */ |
| 1784 | dev->features |= 0; |
| 1785 | |
| 1786 | bp = netdev_priv(dev); |
| 1787 | bp->pdev = pdev; |
| 1788 | bp->dev = dev; |
| 1789 | |
| 1790 | spin_lock_init(&bp->lock); |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1791 | INIT_WORK(&bp->tx_error_task, macb_tx_error_task); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1792 | |
Jamie Iles | 461845d | 2011-03-08 20:19:23 +0000 | [diff] [blame] | 1793 | bp->pclk = clk_get(&pdev->dev, "pclk"); |
Andrew Victor | 0cc8674 | 2007-02-07 16:40:44 +0100 | [diff] [blame] | 1794 | if (IS_ERR(bp->pclk)) { |
| 1795 | dev_err(&pdev->dev, "failed to get macb_clk\n"); |
| 1796 | goto err_out_free_dev; |
| 1797 | } |
Steffen Trumtrar | ace5801 | 2013-03-27 23:07:07 +0000 | [diff] [blame] | 1798 | clk_prepare_enable(bp->pclk); |
Jamie Iles | 461845d | 2011-03-08 20:19:23 +0000 | [diff] [blame] | 1799 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1800 | bp->hclk = clk_get(&pdev->dev, "hclk"); |
| 1801 | if (IS_ERR(bp->hclk)) { |
| 1802 | dev_err(&pdev->dev, "failed to get hclk\n"); |
| 1803 | goto err_out_put_pclk; |
| 1804 | } |
Steffen Trumtrar | ace5801 | 2013-03-27 23:07:07 +0000 | [diff] [blame] | 1805 | clk_prepare_enable(bp->hclk); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1806 | |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 1807 | bp->regs = ioremap(regs->start, resource_size(regs)); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1808 | if (!bp->regs) { |
| 1809 | dev_err(&pdev->dev, "failed to map registers, aborting.\n"); |
| 1810 | err = -ENOMEM; |
| 1811 | goto err_out_disable_clocks; |
| 1812 | } |
| 1813 | |
| 1814 | dev->irq = platform_get_irq(pdev, 0); |
Javier Martinez Canillas | ab392d2 | 2011-03-28 16:27:31 +0000 | [diff] [blame] | 1815 | err = request_irq(dev->irq, macb_interrupt, 0, dev->name, dev); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1816 | if (err) { |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 1817 | dev_err(&pdev->dev, "Unable to request IRQ %d (error %d)\n", |
| 1818 | dev->irq, err); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1819 | goto err_out_iounmap; |
| 1820 | } |
| 1821 | |
Alexander Beregalov | 5f1fa99 | 2009-04-11 07:42:26 +0000 | [diff] [blame] | 1822 | dev->netdev_ops = &macb_netdev_ops; |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1823 | netif_napi_add(dev, &bp->napi, macb_poll, 64); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1824 | dev->ethtool_ops = &macb_ethtool_ops; |
| 1825 | |
| 1826 | dev->base_addr = regs->start; |
| 1827 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1828 | /* setup appropriated routines according to adapter type */ |
| 1829 | if (macb_is_gem(bp)) { |
| 1830 | bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers; |
| 1831 | bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers; |
| 1832 | bp->macbgem_ops.mog_init_rings = gem_init_rings; |
| 1833 | bp->macbgem_ops.mog_rx = gem_rx; |
| 1834 | } else { |
| 1835 | bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers; |
| 1836 | bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers; |
| 1837 | bp->macbgem_ops.mog_init_rings = macb_init_rings; |
| 1838 | bp->macbgem_ops.mog_rx = macb_rx; |
| 1839 | } |
| 1840 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1841 | /* Set MII management clock divider */ |
Jamie Iles | 70c9f3d | 2011-03-09 16:22:54 +0000 | [diff] [blame] | 1842 | config = macb_mdc_clk_div(bp); |
Jamie Iles | 757a03c | 2011-03-09 16:29:59 +0000 | [diff] [blame] | 1843 | config |= macb_dbw(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1844 | macb_writel(bp, NCFGR, config); |
| 1845 | |
Guenter Roeck | 5090704 | 2013-04-02 09:35:09 +0000 | [diff] [blame] | 1846 | mac = of_get_mac_address(pdev->dev.of_node); |
| 1847 | if (mac) |
| 1848 | memcpy(bp->dev->dev_addr, mac, ETH_ALEN); |
| 1849 | else |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 1850 | macb_get_hwaddr(bp); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1851 | |
Guenter Roeck | 5090704 | 2013-04-02 09:35:09 +0000 | [diff] [blame] | 1852 | err = of_get_phy_mode(pdev->dev.of_node); |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 1853 | if (err < 0) { |
| 1854 | pdata = pdev->dev.platform_data; |
| 1855 | if (pdata && pdata->is_rmii) |
| 1856 | bp->phy_interface = PHY_INTERFACE_MODE_RMII; |
| 1857 | else |
| 1858 | bp->phy_interface = PHY_INTERFACE_MODE_MII; |
| 1859 | } else { |
| 1860 | bp->phy_interface = err; |
| 1861 | } |
| 1862 | |
Patrice Vilchez | 140b755 | 2012-10-31 06:04:50 +0000 | [diff] [blame] | 1863 | if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII) |
| 1864 | macb_or_gem_writel(bp, USRIO, GEM_BIT(RGMII)); |
| 1865 | else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII) |
Andrew Victor | 0cc8674 | 2007-02-07 16:40:44 +0100 | [diff] [blame] | 1866 | #if defined(CONFIG_ARCH_AT91) |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1867 | macb_or_gem_writel(bp, USRIO, (MACB_BIT(RMII) | |
| 1868 | MACB_BIT(CLKEN))); |
Andrew Victor | 0cc8674 | 2007-02-07 16:40:44 +0100 | [diff] [blame] | 1869 | #else |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1870 | macb_or_gem_writel(bp, USRIO, 0); |
Andrew Victor | 0cc8674 | 2007-02-07 16:40:44 +0100 | [diff] [blame] | 1871 | #endif |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1872 | else |
Andrew Victor | 0cc8674 | 2007-02-07 16:40:44 +0100 | [diff] [blame] | 1873 | #if defined(CONFIG_ARCH_AT91) |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1874 | macb_or_gem_writel(bp, USRIO, MACB_BIT(CLKEN)); |
Andrew Victor | 0cc8674 | 2007-02-07 16:40:44 +0100 | [diff] [blame] | 1875 | #else |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1876 | macb_or_gem_writel(bp, USRIO, MACB_BIT(MII)); |
Andrew Victor | 0cc8674 | 2007-02-07 16:40:44 +0100 | [diff] [blame] | 1877 | #endif |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1878 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1879 | err = register_netdev(dev); |
| 1880 | if (err) { |
| 1881 | dev_err(&pdev->dev, "Cannot register net device, aborting.\n"); |
| 1882 | goto err_out_free_irq; |
| 1883 | } |
| 1884 | |
Nicolas Ferre | 72ca820 | 2013-04-14 22:04:33 +0000 | [diff] [blame] | 1885 | err = macb_mii_init(bp); |
| 1886 | if (err) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1887 | goto err_out_unregister_netdev; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1888 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1889 | platform_set_drvdata(pdev, dev); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1890 | |
Nicolas Ferre | 03fc472 | 2012-07-03 23:14:13 +0000 | [diff] [blame] | 1891 | netif_carrier_off(dev); |
| 1892 | |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1893 | netdev_info(dev, "Cadence %s at 0x%08lx irq %d (%pM)\n", |
| 1894 | macb_is_gem(bp) ? "GEM" : "MACB", dev->base_addr, |
| 1895 | dev->irq, dev->dev_addr); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1896 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1897 | phydev = bp->phy_dev; |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 1898 | netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n", |
| 1899 | phydev->drv->name, dev_name(&phydev->dev), phydev->irq); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1900 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1901 | return 0; |
| 1902 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1903 | err_out_unregister_netdev: |
| 1904 | unregister_netdev(dev); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1905 | err_out_free_irq: |
| 1906 | free_irq(dev->irq, dev); |
| 1907 | err_out_iounmap: |
| 1908 | iounmap(bp->regs); |
| 1909 | err_out_disable_clocks: |
Steffen Trumtrar | ace5801 | 2013-03-27 23:07:07 +0000 | [diff] [blame] | 1910 | clk_disable_unprepare(bp->hclk); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1911 | clk_put(bp->hclk); |
Steffen Trumtrar | ace5801 | 2013-03-27 23:07:07 +0000 | [diff] [blame] | 1912 | clk_disable_unprepare(bp->pclk); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1913 | err_out_put_pclk: |
| 1914 | clk_put(bp->pclk); |
| 1915 | err_out_free_dev: |
| 1916 | free_netdev(dev); |
| 1917 | err_out: |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1918 | return err; |
| 1919 | } |
| 1920 | |
Haavard Skinnemoen | 06c3fd6 | 2008-01-31 13:10:22 +0100 | [diff] [blame] | 1921 | static int __exit macb_remove(struct platform_device *pdev) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1922 | { |
| 1923 | struct net_device *dev; |
| 1924 | struct macb *bp; |
| 1925 | |
| 1926 | dev = platform_get_drvdata(pdev); |
| 1927 | |
| 1928 | if (dev) { |
| 1929 | bp = netdev_priv(dev); |
Atsushi Nemoto | 84b7901 | 2008-04-10 23:30:07 +0900 | [diff] [blame] | 1930 | if (bp->phy_dev) |
| 1931 | phy_disconnect(bp->phy_dev); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 1932 | mdiobus_unregister(bp->mii_bus); |
| 1933 | kfree(bp->mii_bus->irq); |
| 1934 | mdiobus_free(bp->mii_bus); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1935 | unregister_netdev(dev); |
| 1936 | free_irq(dev->irq, dev); |
| 1937 | iounmap(bp->regs); |
Steffen Trumtrar | ace5801 | 2013-03-27 23:07:07 +0000 | [diff] [blame] | 1938 | clk_disable_unprepare(bp->hclk); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1939 | clk_put(bp->hclk); |
Steffen Trumtrar | ace5801 | 2013-03-27 23:07:07 +0000 | [diff] [blame] | 1940 | clk_disable_unprepare(bp->pclk); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1941 | clk_put(bp->pclk); |
| 1942 | free_netdev(dev); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | return 0; |
| 1946 | } |
| 1947 | |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 1948 | #ifdef CONFIG_PM |
| 1949 | static int macb_suspend(struct platform_device *pdev, pm_message_t state) |
| 1950 | { |
| 1951 | struct net_device *netdev = platform_get_drvdata(pdev); |
| 1952 | struct macb *bp = netdev_priv(netdev); |
| 1953 | |
Nicolas Ferre | 03fc472 | 2012-07-03 23:14:13 +0000 | [diff] [blame] | 1954 | netif_carrier_off(netdev); |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 1955 | netif_device_detach(netdev); |
| 1956 | |
Steffen Trumtrar | ace5801 | 2013-03-27 23:07:07 +0000 | [diff] [blame] | 1957 | clk_disable_unprepare(bp->hclk); |
| 1958 | clk_disable_unprepare(bp->pclk); |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 1959 | |
| 1960 | return 0; |
| 1961 | } |
| 1962 | |
| 1963 | static int macb_resume(struct platform_device *pdev) |
| 1964 | { |
| 1965 | struct net_device *netdev = platform_get_drvdata(pdev); |
| 1966 | struct macb *bp = netdev_priv(netdev); |
| 1967 | |
Steffen Trumtrar | ace5801 | 2013-03-27 23:07:07 +0000 | [diff] [blame] | 1968 | clk_prepare_enable(bp->pclk); |
| 1969 | clk_prepare_enable(bp->hclk); |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 1970 | |
| 1971 | netif_device_attach(netdev); |
| 1972 | |
| 1973 | return 0; |
| 1974 | } |
| 1975 | #else |
| 1976 | #define macb_suspend NULL |
| 1977 | #define macb_resume NULL |
| 1978 | #endif |
| 1979 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1980 | static struct platform_driver macb_driver = { |
Haavard Skinnemoen | 06c3fd6 | 2008-01-31 13:10:22 +0100 | [diff] [blame] | 1981 | .remove = __exit_p(macb_remove), |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 1982 | .suspend = macb_suspend, |
| 1983 | .resume = macb_resume, |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1984 | .driver = { |
| 1985 | .name = "macb", |
Kay Sievers | 72abb46 | 2008-04-18 13:50:44 -0700 | [diff] [blame] | 1986 | .owner = THIS_MODULE, |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 1987 | .of_match_table = of_match_ptr(macb_dt_ids), |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1988 | }, |
| 1989 | }; |
| 1990 | |
Jingoo Han | b543a8d | 2013-03-04 16:43:18 +0000 | [diff] [blame] | 1991 | module_platform_driver_probe(macb_driver, macb_probe); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1992 | |
| 1993 | MODULE_LICENSE("GPL"); |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1994 | MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver"); |
Jean Delvare | e05503e | 2011-05-18 16:49:24 +0200 | [diff] [blame] | 1995 | MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); |
Kay Sievers | 72abb46 | 2008-04-18 13:50:44 -0700 | [diff] [blame] | 1996 | MODULE_ALIAS("platform:macb"); |