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