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 | 2dbfdbb9 | 2012-11-11 13:56:27 +0000 | [diff] [blame] | 21 | #include <linux/gpio.h> |
Gregory CLEMENT | 270c499 | 2015-12-17 10:51:04 +0100 | [diff] [blame] | 22 | #include <linux/gpio/consumer.h> |
Alexey Dobriyan | a6b7a40 | 2011-06-06 10:43:46 +0000 | [diff] [blame] | 23 | #include <linux/interrupt.h> |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 24 | #include <linux/netdevice.h> |
| 25 | #include <linux/etherdevice.h> |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 26 | #include <linux/dma-mapping.h> |
Jamie Iles | 84e0cdb | 2011-03-08 20:17:06 +0000 | [diff] [blame] | 27 | #include <linux/platform_data/macb.h> |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 28 | #include <linux/platform_device.h> |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 29 | #include <linux/phy.h> |
Olof Johansson | b17471f | 2011-12-20 13:13:07 -0800 | [diff] [blame] | 30 | #include <linux/of.h> |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 31 | #include <linux/of_device.h> |
Gregory CLEMENT | 270c499 | 2015-12-17 10:51:04 +0100 | [diff] [blame] | 32 | #include <linux/of_gpio.h> |
Boris BREZILLON | 148cbb5 | 2013-08-22 17:57:28 +0200 | [diff] [blame] | 33 | #include <linux/of_mdio.h> |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 34 | #include <linux/of_net.h> |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 35 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 36 | #include "macb.h" |
| 37 | |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 38 | #define MACB_RX_BUFFER_SIZE 128 |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 39 | #define RX_BUFFER_MULTIPLE 64 /* bytes */ |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 40 | #define RX_RING_SIZE 512 /* must be power of 2 */ |
| 41 | #define RX_RING_BYTES (sizeof(struct macb_dma_desc) * RX_RING_SIZE) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 42 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 43 | #define TX_RING_SIZE 128 /* must be power of 2 */ |
| 44 | #define TX_RING_BYTES (sizeof(struct macb_dma_desc) * TX_RING_SIZE) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 45 | |
Nicolas Ferre | 909a858 | 2012-11-19 06:00:21 +0000 | [diff] [blame] | 46 | /* level of occupied TX descriptors under which we wake up TX process */ |
| 47 | #define MACB_TX_WAKEUP_THRESH (3 * TX_RING_SIZE / 4) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 48 | |
| 49 | #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \ |
| 50 | | MACB_BIT(ISR_ROVR)) |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 51 | #define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \ |
| 52 | | MACB_BIT(ISR_RLE) \ |
| 53 | | MACB_BIT(TXERR)) |
| 54 | #define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP)) |
| 55 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 56 | #define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1)) |
| 57 | #define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1)) |
| 58 | |
Harini Katakam | a5898ea | 2015-05-06 22:27:18 +0530 | [diff] [blame] | 59 | #define GEM_MTU_MIN_SIZE 68 |
| 60 | |
Sergio Prado | 3e2a5e1 | 2016-02-09 12:07:16 -0200 | [diff] [blame] | 61 | #define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0) |
| 62 | #define MACB_WOL_ENABLED (0x1 << 1) |
| 63 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 64 | /* Graceful stop timeouts in us. We should allow up to |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 65 | * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions) |
| 66 | */ |
| 67 | #define MACB_HALT_TIMEOUT 1230 |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 68 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 69 | /* Ring buffer accessors */ |
| 70 | static unsigned int macb_tx_ring_wrap(unsigned int index) |
| 71 | { |
| 72 | return index & (TX_RING_SIZE - 1); |
| 73 | } |
| 74 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 75 | static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue, |
| 76 | unsigned int index) |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 77 | { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 78 | return &queue->tx_ring[macb_tx_ring_wrap(index)]; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 81 | static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue, |
| 82 | unsigned int index) |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 83 | { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 84 | return &queue->tx_skb[macb_tx_ring_wrap(index)]; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 87 | static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index) |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 88 | { |
| 89 | dma_addr_t offset; |
| 90 | |
| 91 | offset = macb_tx_ring_wrap(index) * sizeof(struct macb_dma_desc); |
| 92 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 93 | return queue->tx_ring_dma + offset; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static unsigned int macb_rx_ring_wrap(unsigned int index) |
| 97 | { |
| 98 | return index & (RX_RING_SIZE - 1); |
| 99 | } |
| 100 | |
| 101 | static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index) |
| 102 | { |
| 103 | return &bp->rx_ring[macb_rx_ring_wrap(index)]; |
| 104 | } |
| 105 | |
| 106 | static void *macb_rx_buffer(struct macb *bp, unsigned int index) |
| 107 | { |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 108 | 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] | 109 | } |
| 110 | |
Andy Shevchenko | f2ce8a9 | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 111 | /* I/O accessors */ |
| 112 | static u32 hw_readl_native(struct macb *bp, int offset) |
| 113 | { |
| 114 | return __raw_readl(bp->regs + offset); |
| 115 | } |
| 116 | |
| 117 | static void hw_writel_native(struct macb *bp, int offset, u32 value) |
| 118 | { |
| 119 | __raw_writel(value, bp->regs + offset); |
| 120 | } |
| 121 | |
| 122 | static u32 hw_readl(struct macb *bp, int offset) |
| 123 | { |
| 124 | return readl_relaxed(bp->regs + offset); |
| 125 | } |
| 126 | |
| 127 | static void hw_writel(struct macb *bp, int offset, u32 value) |
| 128 | { |
| 129 | writel_relaxed(value, bp->regs + offset); |
| 130 | } |
| 131 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 132 | /* Find the CPU endianness by using the loopback bit of NCR register. When the |
Moritz Fischer | 88023be | 2016-03-29 19:11:15 -0700 | [diff] [blame] | 133 | * CPU is in big endian we need to program swapped mode for management |
Andy Shevchenko | f2ce8a9 | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 134 | * descriptor access. |
| 135 | */ |
| 136 | static bool hw_is_native_io(void __iomem *addr) |
| 137 | { |
| 138 | u32 value = MACB_BIT(LLB); |
| 139 | |
| 140 | __raw_writel(value, addr + MACB_NCR); |
| 141 | value = __raw_readl(addr + MACB_NCR); |
| 142 | |
| 143 | /* Write 0 back to disable everything */ |
| 144 | __raw_writel(0, addr + MACB_NCR); |
| 145 | |
| 146 | return value == MACB_BIT(LLB); |
| 147 | } |
| 148 | |
| 149 | static bool hw_is_gem(void __iomem *addr, bool native_io) |
| 150 | { |
| 151 | u32 id; |
| 152 | |
| 153 | if (native_io) |
| 154 | id = __raw_readl(addr + MACB_MID); |
| 155 | else |
| 156 | id = readl_relaxed(addr + MACB_MID); |
| 157 | |
| 158 | return MACB_BFEXT(IDNUM, id) >= 0x2; |
| 159 | } |
| 160 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 161 | static void macb_set_hwaddr(struct macb *bp) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 162 | { |
| 163 | u32 bottom; |
| 164 | u16 top; |
| 165 | |
| 166 | bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr)); |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 167 | macb_or_gem_writel(bp, SA1B, bottom); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 168 | top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4))); |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 169 | macb_or_gem_writel(bp, SA1T, top); |
Joachim Eastwood | 3629a6c | 2012-11-11 13:56:28 +0000 | [diff] [blame] | 170 | |
| 171 | /* Clear unused address register sets */ |
| 172 | macb_or_gem_writel(bp, SA2B, 0); |
| 173 | macb_or_gem_writel(bp, SA2T, 0); |
| 174 | macb_or_gem_writel(bp, SA3B, 0); |
| 175 | macb_or_gem_writel(bp, SA3T, 0); |
| 176 | macb_or_gem_writel(bp, SA4B, 0); |
| 177 | macb_or_gem_writel(bp, SA4T, 0); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 180 | static void macb_get_hwaddr(struct macb *bp) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 181 | { |
Joachim Eastwood | d25e78a | 2012-11-07 08:14:51 +0000 | [diff] [blame] | 182 | struct macb_platform_data *pdata; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 183 | u32 bottom; |
| 184 | u16 top; |
| 185 | u8 addr[6]; |
Joachim Eastwood | 17b8bb3 | 2012-11-07 08:14:50 +0000 | [diff] [blame] | 186 | int i; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 187 | |
Jingoo Han | c607a0d | 2013-08-30 14:12:21 +0900 | [diff] [blame] | 188 | pdata = dev_get_platdata(&bp->pdev->dev); |
Joachim Eastwood | d25e78a | 2012-11-07 08:14:51 +0000 | [diff] [blame] | 189 | |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 190 | /* Check all 4 address register for valid address */ |
Joachim Eastwood | 17b8bb3 | 2012-11-07 08:14:50 +0000 | [diff] [blame] | 191 | for (i = 0; i < 4; i++) { |
| 192 | bottom = macb_or_gem_readl(bp, SA1B + i * 8); |
| 193 | top = macb_or_gem_readl(bp, SA1T + i * 8); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 194 | |
Joachim Eastwood | d25e78a | 2012-11-07 08:14:51 +0000 | [diff] [blame] | 195 | if (pdata && pdata->rev_eth_addr) { |
| 196 | addr[5] = bottom & 0xff; |
| 197 | addr[4] = (bottom >> 8) & 0xff; |
| 198 | addr[3] = (bottom >> 16) & 0xff; |
| 199 | addr[2] = (bottom >> 24) & 0xff; |
| 200 | addr[1] = top & 0xff; |
| 201 | addr[0] = (top & 0xff00) >> 8; |
| 202 | } else { |
| 203 | addr[0] = bottom & 0xff; |
| 204 | addr[1] = (bottom >> 8) & 0xff; |
| 205 | addr[2] = (bottom >> 16) & 0xff; |
| 206 | addr[3] = (bottom >> 24) & 0xff; |
| 207 | addr[4] = top & 0xff; |
| 208 | addr[5] = (top >> 8) & 0xff; |
| 209 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 210 | |
Joachim Eastwood | 17b8bb3 | 2012-11-07 08:14:50 +0000 | [diff] [blame] | 211 | if (is_valid_ether_addr(addr)) { |
| 212 | memcpy(bp->dev->dev_addr, addr, sizeof(addr)); |
| 213 | return; |
| 214 | } |
Sven Schnelle | d1d5741 | 2008-06-09 16:33:57 -0700 | [diff] [blame] | 215 | } |
Joachim Eastwood | 17b8bb3 | 2012-11-07 08:14:50 +0000 | [diff] [blame] | 216 | |
Andy Shevchenko | a35919e | 2015-07-24 21:24:01 +0300 | [diff] [blame] | 217 | dev_info(&bp->pdev->dev, "invalid hw address, using random\n"); |
Joachim Eastwood | 17b8bb3 | 2012-11-07 08:14:50 +0000 | [diff] [blame] | 218 | eth_hw_addr_random(bp->dev); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 219 | } |
| 220 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 221 | 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] | 222 | { |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 223 | struct macb *bp = bus->priv; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 224 | int value; |
| 225 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 226 | macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF) |
| 227 | | MACB_BF(RW, MACB_MAN_READ) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 228 | | MACB_BF(PHYA, mii_id) |
| 229 | | MACB_BF(REGA, regnum) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 230 | | MACB_BF(CODE, MACB_MAN_CODE))); |
| 231 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 232 | /* wait for end of transfer */ |
| 233 | while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR))) |
| 234 | cpu_relax(); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 235 | |
| 236 | value = MACB_BFEXT(DATA, macb_readl(bp, MAN)); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 237 | |
| 238 | return value; |
| 239 | } |
| 240 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 241 | static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum, |
| 242 | u16 value) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 243 | { |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 244 | struct macb *bp = bus->priv; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 245 | |
| 246 | macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF) |
| 247 | | MACB_BF(RW, MACB_MAN_WRITE) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 248 | | MACB_BF(PHYA, mii_id) |
| 249 | | MACB_BF(REGA, regnum) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 250 | | MACB_BF(CODE, MACB_MAN_CODE) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 251 | | MACB_BF(DATA, value))); |
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 | /* wait for end of transfer */ |
| 254 | while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR))) |
| 255 | cpu_relax(); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 256 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
Soren Brinkmann | e1824df | 2013-12-10 16:07:23 -0800 | [diff] [blame] | 260 | /** |
| 261 | * macb_set_tx_clk() - Set a clock to a new frequency |
| 262 | * @clk Pointer to the clock to change |
| 263 | * @rate New frequency in Hz |
| 264 | * @dev Pointer to the struct net_device |
| 265 | */ |
| 266 | static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev) |
| 267 | { |
| 268 | long ferr, rate, rate_rounded; |
| 269 | |
Cyrille Pitchen | 93b31f4 | 2015-03-07 07:23:31 +0100 | [diff] [blame] | 270 | if (!clk) |
| 271 | return; |
| 272 | |
Soren Brinkmann | e1824df | 2013-12-10 16:07:23 -0800 | [diff] [blame] | 273 | switch (speed) { |
| 274 | case SPEED_10: |
| 275 | rate = 2500000; |
| 276 | break; |
| 277 | case SPEED_100: |
| 278 | rate = 25000000; |
| 279 | break; |
| 280 | case SPEED_1000: |
| 281 | rate = 125000000; |
| 282 | break; |
| 283 | default: |
Soren Brinkmann | 9319e47 | 2013-12-10 20:57:57 -0800 | [diff] [blame] | 284 | return; |
Soren Brinkmann | e1824df | 2013-12-10 16:07:23 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | rate_rounded = clk_round_rate(clk, rate); |
| 288 | if (rate_rounded < 0) |
| 289 | return; |
| 290 | |
| 291 | /* RGMII allows 50 ppm frequency error. Test and warn if this limit |
| 292 | * is not satisfied. |
| 293 | */ |
| 294 | ferr = abs(rate_rounded - rate); |
| 295 | ferr = DIV_ROUND_UP(ferr, rate / 100000); |
| 296 | if (ferr > 5) |
| 297 | netdev_warn(dev, "unable to generate target frequency: %ld Hz\n", |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 298 | rate); |
Soren Brinkmann | e1824df | 2013-12-10 16:07:23 -0800 | [diff] [blame] | 299 | |
| 300 | if (clk_set_rate(clk, rate_rounded)) |
| 301 | netdev_err(dev, "adjusting tx_clk failed.\n"); |
| 302 | } |
| 303 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 304 | static void macb_handle_link_change(struct net_device *dev) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 305 | { |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 306 | struct macb *bp = netdev_priv(dev); |
Philippe Reynes | 0a91281 | 2016-06-22 00:32:35 +0200 | [diff] [blame] | 307 | struct phy_device *phydev = dev->phydev; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 308 | unsigned long flags; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 309 | int status_change = 0; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 310 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 311 | spin_lock_irqsave(&bp->lock, flags); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 312 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 313 | if (phydev->link) { |
| 314 | if ((bp->speed != phydev->speed) || |
| 315 | (bp->duplex != phydev->duplex)) { |
| 316 | u32 reg; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 317 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 318 | reg = macb_readl(bp, NCFGR); |
| 319 | reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD)); |
Patrice Vilchez | 140b755 | 2012-10-31 06:04:50 +0000 | [diff] [blame] | 320 | if (macb_is_gem(bp)) |
| 321 | reg &= ~GEM_BIT(GBE); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 322 | |
| 323 | if (phydev->duplex) |
| 324 | reg |= MACB_BIT(FD); |
Atsushi Nemoto | 179956f | 2008-02-21 22:50:54 +0900 | [diff] [blame] | 325 | if (phydev->speed == SPEED_100) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 326 | reg |= MACB_BIT(SPD); |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 327 | if (phydev->speed == SPEED_1000 && |
| 328 | bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE) |
Patrice Vilchez | 140b755 | 2012-10-31 06:04:50 +0000 | [diff] [blame] | 329 | reg |= GEM_BIT(GBE); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 330 | |
Patrice Vilchez | 140b755 | 2012-10-31 06:04:50 +0000 | [diff] [blame] | 331 | macb_or_gem_writel(bp, NCFGR, reg); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 332 | |
| 333 | bp->speed = phydev->speed; |
| 334 | bp->duplex = phydev->duplex; |
| 335 | status_change = 1; |
| 336 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 337 | } |
| 338 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 339 | if (phydev->link != bp->link) { |
Anton Vorontsov | c8f1568 | 2008-07-22 15:41:24 -0700 | [diff] [blame] | 340 | if (!phydev->link) { |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 341 | bp->speed = 0; |
| 342 | bp->duplex = -1; |
| 343 | } |
| 344 | bp->link = phydev->link; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 345 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 346 | status_change = 1; |
| 347 | } |
| 348 | |
| 349 | spin_unlock_irqrestore(&bp->lock, flags); |
| 350 | |
| 351 | if (status_change) { |
Nicolas Ferre | 03fc472 | 2012-07-03 23:14:13 +0000 | [diff] [blame] | 352 | if (phydev->link) { |
Jaeden Amero | 2c29b23 | 2015-03-12 18:07:54 -0500 | [diff] [blame] | 353 | /* Update the TX clock rate if and only if the link is |
| 354 | * up and there has been a link change. |
| 355 | */ |
| 356 | macb_set_tx_clk(bp->tx_clk, phydev->speed, dev); |
| 357 | |
Nicolas Ferre | 03fc472 | 2012-07-03 23:14:13 +0000 | [diff] [blame] | 358 | netif_carrier_on(dev); |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 359 | netdev_info(dev, "link up (%d/%s)\n", |
| 360 | phydev->speed, |
| 361 | phydev->duplex == DUPLEX_FULL ? |
| 362 | "Full" : "Half"); |
Nicolas Ferre | 03fc472 | 2012-07-03 23:14:13 +0000 | [diff] [blame] | 363 | } else { |
| 364 | netif_carrier_off(dev); |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 365 | netdev_info(dev, "link down\n"); |
Nicolas Ferre | 03fc472 | 2012-07-03 23:14:13 +0000 | [diff] [blame] | 366 | } |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | /* based on au1000_eth. c*/ |
| 371 | static int macb_mii_probe(struct net_device *dev) |
| 372 | { |
| 373 | struct macb *bp = netdev_priv(dev); |
Joachim Eastwood | 2dbfdbb9 | 2012-11-11 13:56:27 +0000 | [diff] [blame] | 374 | struct macb_platform_data *pdata; |
Jiri Pirko | 7455a76 | 2010-02-08 05:12:08 +0000 | [diff] [blame] | 375 | struct phy_device *phydev; |
Joachim Eastwood | 2dbfdbb9 | 2012-11-11 13:56:27 +0000 | [diff] [blame] | 376 | int phy_irq; |
Jiri Pirko | 7455a76 | 2010-02-08 05:12:08 +0000 | [diff] [blame] | 377 | int ret; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 378 | |
Jiri Pirko | 7455a76 | 2010-02-08 05:12:08 +0000 | [diff] [blame] | 379 | phydev = phy_find_first(bp->mii_bus); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 380 | if (!phydev) { |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 381 | netdev_err(dev, "no PHY found\n"); |
Boris BREZILLON | 7daa78e | 2013-08-27 14:36:14 +0200 | [diff] [blame] | 382 | return -ENXIO; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Joachim Eastwood | 2dbfdbb9 | 2012-11-11 13:56:27 +0000 | [diff] [blame] | 385 | pdata = dev_get_platdata(&bp->pdev->dev); |
| 386 | if (pdata && gpio_is_valid(pdata->phy_irq_pin)) { |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 387 | ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin, |
| 388 | "phy int"); |
Joachim Eastwood | 2dbfdbb9 | 2012-11-11 13:56:27 +0000 | [diff] [blame] | 389 | if (!ret) { |
| 390 | phy_irq = gpio_to_irq(pdata->phy_irq_pin); |
| 391 | phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq; |
| 392 | } |
| 393 | } |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 394 | |
| 395 | /* attach the mac to the phy */ |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 396 | ret = phy_connect_direct(dev, phydev, &macb_handle_link_change, |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 397 | bp->phy_interface); |
Jiri Pirko | 7455a76 | 2010-02-08 05:12:08 +0000 | [diff] [blame] | 398 | if (ret) { |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 399 | netdev_err(dev, "Could not attach to PHY\n"); |
Jiri Pirko | 7455a76 | 2010-02-08 05:12:08 +0000 | [diff] [blame] | 400 | return ret; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 401 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 402 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 403 | /* mask with MAC supported features */ |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 404 | if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE) |
Patrice Vilchez | 140b755 | 2012-10-31 06:04:50 +0000 | [diff] [blame] | 405 | phydev->supported &= PHY_GBIT_FEATURES; |
| 406 | else |
| 407 | phydev->supported &= PHY_BASIC_FEATURES; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 408 | |
Nathan Sullivan | 222ca8e | 2015-05-22 09:22:10 -0500 | [diff] [blame] | 409 | if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF) |
| 410 | phydev->supported &= ~SUPPORTED_1000baseT_Half; |
| 411 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 412 | phydev->advertising = phydev->supported; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 413 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 414 | bp->link = 0; |
| 415 | bp->speed = 0; |
| 416 | bp->duplex = -1; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 421 | static int macb_mii_init(struct macb *bp) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 422 | { |
Jamie Iles | 84e0cdb | 2011-03-08 20:17:06 +0000 | [diff] [blame] | 423 | struct macb_platform_data *pdata; |
Boris BREZILLON | 148cbb5 | 2013-08-22 17:57:28 +0200 | [diff] [blame] | 424 | struct device_node *np; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 425 | int err = -ENXIO, i; |
| 426 | |
Uwe Kleine-Koenig | 3dbda77 | 2009-07-23 08:31:31 +0200 | [diff] [blame] | 427 | /* Enable management port */ |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 428 | macb_writel(bp, NCR, MACB_BIT(MPE)); |
| 429 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 430 | bp->mii_bus = mdiobus_alloc(); |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 431 | if (!bp->mii_bus) { |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 432 | err = -ENOMEM; |
| 433 | goto err_out; |
| 434 | } |
| 435 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 436 | bp->mii_bus->name = "MACB_mii_bus"; |
| 437 | bp->mii_bus->read = &macb_mdio_read; |
| 438 | bp->mii_bus->write = &macb_mdio_write; |
Florian Fainelli | 98d5e57 | 2012-01-09 23:59:11 +0000 | [diff] [blame] | 439 | snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x", |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 440 | bp->pdev->name, bp->pdev->id); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 441 | bp->mii_bus->priv = bp; |
Florian Fainelli | cf66966 | 2016-05-02 18:38:45 -0700 | [diff] [blame] | 442 | bp->mii_bus->parent = &bp->pdev->dev; |
Jingoo Han | c607a0d | 2013-08-30 14:12:21 +0900 | [diff] [blame] | 443 | pdata = dev_get_platdata(&bp->pdev->dev); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 444 | |
Jamie Iles | 9152394 | 2011-02-28 04:05:25 +0000 | [diff] [blame] | 445 | dev_set_drvdata(&bp->dev->dev, bp->mii_bus); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 446 | |
Boris BREZILLON | 148cbb5 | 2013-08-22 17:57:28 +0200 | [diff] [blame] | 447 | np = bp->pdev->dev.of_node; |
| 448 | if (np) { |
| 449 | /* try dt phy registration */ |
| 450 | err = of_mdiobus_register(bp->mii_bus, np); |
| 451 | |
| 452 | /* fallback to standard phy registration if no phy were |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 453 | * found during dt phy registration |
| 454 | */ |
Boris BREZILLON | 148cbb5 | 2013-08-22 17:57:28 +0200 | [diff] [blame] | 455 | if (!err && !phy_find_first(bp->mii_bus)) { |
| 456 | for (i = 0; i < PHY_MAX_ADDR; i++) { |
| 457 | struct phy_device *phydev; |
| 458 | |
| 459 | phydev = mdiobus_scan(bp->mii_bus, i); |
Sergei Shtylyov | ce24c2b | 2016-05-01 01:47:36 +0300 | [diff] [blame] | 460 | if (IS_ERR(phydev) && |
| 461 | PTR_ERR(phydev) != -ENODEV) { |
Boris BREZILLON | 148cbb5 | 2013-08-22 17:57:28 +0200 | [diff] [blame] | 462 | err = PTR_ERR(phydev); |
| 463 | break; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | if (err) |
| 468 | goto err_out_unregister_bus; |
| 469 | } |
| 470 | } else { |
Boris BREZILLON | 148cbb5 | 2013-08-22 17:57:28 +0200 | [diff] [blame] | 471 | if (pdata) |
| 472 | bp->mii_bus->phy_mask = pdata->phy_mask; |
| 473 | |
| 474 | err = mdiobus_register(bp->mii_bus); |
| 475 | } |
| 476 | |
| 477 | if (err) |
Andrew Lunn | e7f4dc3 | 2016-01-06 20:11:15 +0100 | [diff] [blame] | 478 | goto err_out_free_mdiobus; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 479 | |
Boris BREZILLON | 7daa78e | 2013-08-27 14:36:14 +0200 | [diff] [blame] | 480 | err = macb_mii_probe(bp->dev); |
| 481 | if (err) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 482 | goto err_out_unregister_bus; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 483 | |
| 484 | return 0; |
| 485 | |
| 486 | err_out_unregister_bus: |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 487 | mdiobus_unregister(bp->mii_bus); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 488 | err_out_free_mdiobus: |
| 489 | mdiobus_free(bp->mii_bus); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 490 | err_out: |
| 491 | return err; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | static void macb_update_stats(struct macb *bp) |
| 495 | { |
Jamie Iles | a494ed8 | 2011-03-09 16:26:35 +0000 | [diff] [blame] | 496 | u32 *p = &bp->hw_stats.macb.rx_pause_frames; |
| 497 | u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1; |
Andy Shevchenko | f2ce8a9 | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 498 | int offset = MACB_PFR; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 499 | |
| 500 | WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4); |
| 501 | |
Moritz Fischer | 96ec631 | 2016-03-29 19:11:11 -0700 | [diff] [blame] | 502 | for (; p < end; p++, offset += 4) |
David S. Miller | 7a6e070 | 2015-07-27 14:24:48 -0700 | [diff] [blame] | 503 | *p += bp->macb_reg_readl(bp, offset); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 504 | } |
| 505 | |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 506 | static int macb_halt_tx(struct macb *bp) |
| 507 | { |
| 508 | unsigned long halt_time, timeout; |
| 509 | u32 status; |
| 510 | |
| 511 | macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT)); |
| 512 | |
| 513 | timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT); |
| 514 | do { |
| 515 | halt_time = jiffies; |
| 516 | status = macb_readl(bp, TSR); |
| 517 | if (!(status & MACB_BIT(TGO))) |
| 518 | return 0; |
| 519 | |
| 520 | usleep_range(10, 250); |
| 521 | } while (time_before(halt_time, timeout)); |
| 522 | |
| 523 | return -ETIMEDOUT; |
| 524 | } |
| 525 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 526 | static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb) |
| 527 | { |
| 528 | if (tx_skb->mapping) { |
| 529 | if (tx_skb->mapped_as_page) |
| 530 | dma_unmap_page(&bp->pdev->dev, tx_skb->mapping, |
| 531 | tx_skb->size, DMA_TO_DEVICE); |
| 532 | else |
| 533 | dma_unmap_single(&bp->pdev->dev, tx_skb->mapping, |
| 534 | tx_skb->size, DMA_TO_DEVICE); |
| 535 | tx_skb->mapping = 0; |
| 536 | } |
| 537 | |
| 538 | if (tx_skb->skb) { |
| 539 | dev_kfree_skb_any(tx_skb->skb); |
| 540 | tx_skb->skb = NULL; |
| 541 | } |
| 542 | } |
| 543 | |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 544 | static void macb_tx_error_task(struct work_struct *work) |
| 545 | { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 546 | struct macb_queue *queue = container_of(work, struct macb_queue, |
| 547 | tx_error_task); |
| 548 | struct macb *bp = queue->bp; |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 549 | struct macb_tx_skb *tx_skb; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 550 | struct macb_dma_desc *desc; |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 551 | struct sk_buff *skb; |
| 552 | unsigned int tail; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 553 | unsigned long flags; |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 554 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 555 | netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n", |
| 556 | (unsigned int)(queue - bp->queues), |
| 557 | queue->tx_tail, queue->tx_head); |
| 558 | |
| 559 | /* Prevent the queue IRQ handlers from running: each of them may call |
| 560 | * macb_tx_interrupt(), which in turn may call netif_wake_subqueue(). |
| 561 | * As explained below, we have to halt the transmission before updating |
| 562 | * TBQP registers so we call netif_tx_stop_all_queues() to notify the |
| 563 | * network engine about the macb/gem being halted. |
| 564 | */ |
| 565 | spin_lock_irqsave(&bp->lock, flags); |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 566 | |
| 567 | /* Make sure nobody is trying to queue up new packets */ |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 568 | netif_tx_stop_all_queues(bp->dev); |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 569 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 570 | /* Stop transmission now |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 571 | * (in case we have just queued new packets) |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 572 | * macb/gem must be halted to write TBQP register |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 573 | */ |
| 574 | if (macb_halt_tx(bp)) |
| 575 | /* Just complain for now, reinitializing TX path can be good */ |
| 576 | netdev_err(bp->dev, "BUG: halt tx timed out\n"); |
| 577 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 578 | /* Treat frames in TX queue including the ones that caused the error. |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 579 | * Free transmit buffers in upper layer. |
| 580 | */ |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 581 | for (tail = queue->tx_tail; tail != queue->tx_head; tail++) { |
| 582 | u32 ctrl; |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 583 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 584 | desc = macb_tx_desc(queue, tail); |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 585 | ctrl = desc->ctrl; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 586 | tx_skb = macb_tx_skb(queue, tail); |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 587 | skb = tx_skb->skb; |
| 588 | |
| 589 | if (ctrl & MACB_BIT(TX_USED)) { |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 590 | /* skb is set for the last buffer of the frame */ |
| 591 | while (!skb) { |
| 592 | macb_tx_unmap(bp, tx_skb); |
| 593 | tail++; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 594 | tx_skb = macb_tx_skb(queue, tail); |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 595 | skb = tx_skb->skb; |
| 596 | } |
| 597 | |
| 598 | /* ctrl still refers to the first buffer descriptor |
| 599 | * since it's the only one written back by the hardware |
| 600 | */ |
| 601 | if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) { |
| 602 | netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n", |
| 603 | macb_tx_ring_wrap(tail), skb->data); |
| 604 | bp->stats.tx_packets++; |
| 605 | bp->stats.tx_bytes += skb->len; |
| 606 | } |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 607 | } else { |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 608 | /* "Buffers exhausted mid-frame" errors may only happen |
| 609 | * if the driver is buggy, so complain loudly about |
| 610 | * those. Statistics are updated by hardware. |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 611 | */ |
| 612 | if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED)) |
| 613 | netdev_err(bp->dev, |
| 614 | "BUG: TX buffers exhausted mid-frame\n"); |
| 615 | |
| 616 | desc->ctrl = ctrl | MACB_BIT(TX_USED); |
| 617 | } |
| 618 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 619 | macb_tx_unmap(bp, tx_skb); |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 622 | /* Set end of TX queue */ |
| 623 | desc = macb_tx_desc(queue, 0); |
| 624 | desc->addr = 0; |
| 625 | desc->ctrl = MACB_BIT(TX_USED); |
| 626 | |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 627 | /* Make descriptor updates visible to hardware */ |
| 628 | wmb(); |
| 629 | |
| 630 | /* Reinitialize the TX desc queue */ |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 631 | queue_writel(queue, TBQP, queue->tx_ring_dma); |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 632 | /* Make TX ring reflect state of hardware */ |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 633 | queue->tx_head = 0; |
| 634 | queue->tx_tail = 0; |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 635 | |
| 636 | /* Housework before enabling TX IRQ */ |
| 637 | macb_writel(bp, TSR, macb_readl(bp, TSR)); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 638 | queue_writel(queue, IER, MACB_TX_INT_FLAGS); |
| 639 | |
| 640 | /* Now we are ready to start transmission again */ |
| 641 | netif_tx_start_all_queues(bp->dev); |
| 642 | macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART)); |
| 643 | |
| 644 | spin_unlock_irqrestore(&bp->lock, flags); |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 647 | static void macb_tx_interrupt(struct macb_queue *queue) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 648 | { |
| 649 | unsigned int tail; |
| 650 | unsigned int head; |
| 651 | u32 status; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 652 | struct macb *bp = queue->bp; |
| 653 | u16 queue_index = queue - bp->queues; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 654 | |
| 655 | status = macb_readl(bp, TSR); |
| 656 | macb_writel(bp, TSR, status); |
| 657 | |
Nicolas Ferre | 581df9e | 2013-05-14 03:00:16 +0000 | [diff] [blame] | 658 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 659 | queue_writel(queue, ISR, MACB_BIT(TCOMP)); |
Steffen Trumtrar | 749a2b6 | 2013-03-27 23:07:05 +0000 | [diff] [blame] | 660 | |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 661 | netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n", |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 662 | (unsigned long)status); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 663 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 664 | head = queue->tx_head; |
| 665 | for (tail = queue->tx_tail; tail != head; tail++) { |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 666 | struct macb_tx_skb *tx_skb; |
| 667 | struct sk_buff *skb; |
| 668 | struct macb_dma_desc *desc; |
| 669 | u32 ctrl; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 670 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 671 | desc = macb_tx_desc(queue, tail); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 672 | |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 673 | /* Make hw descriptor updates visible to CPU */ |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 674 | rmb(); |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 675 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 676 | ctrl = desc->ctrl; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 677 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 678 | /* TX_USED bit is only set by hardware on the very first buffer |
| 679 | * descriptor of the transmitted frame. |
| 680 | */ |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 681 | if (!(ctrl & MACB_BIT(TX_USED))) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 682 | break; |
| 683 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 684 | /* Process all buffers of the current transmitted frame */ |
| 685 | for (;; tail++) { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 686 | tx_skb = macb_tx_skb(queue, tail); |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 687 | skb = tx_skb->skb; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 688 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 689 | /* First, update TX stats if needed */ |
| 690 | if (skb) { |
| 691 | netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n", |
| 692 | macb_tx_ring_wrap(tail), skb->data); |
| 693 | bp->stats.tx_packets++; |
| 694 | bp->stats.tx_bytes += skb->len; |
| 695 | } |
| 696 | |
| 697 | /* Now we can safely release resources */ |
| 698 | macb_tx_unmap(bp, tx_skb); |
| 699 | |
| 700 | /* skb is set only for the last buffer of the frame. |
| 701 | * WARNING: at this point skb has been freed by |
| 702 | * macb_tx_unmap(). |
| 703 | */ |
| 704 | if (skb) |
| 705 | break; |
| 706 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 707 | } |
| 708 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 709 | queue->tx_tail = tail; |
| 710 | if (__netif_subqueue_stopped(bp->dev, queue_index) && |
| 711 | CIRC_CNT(queue->tx_head, queue->tx_tail, |
| 712 | TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH) |
| 713 | netif_wake_subqueue(bp->dev, queue_index); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 714 | } |
| 715 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 716 | static void gem_rx_refill(struct macb *bp) |
| 717 | { |
| 718 | unsigned int entry; |
| 719 | struct sk_buff *skb; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 720 | dma_addr_t paddr; |
| 721 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 722 | while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail, |
| 723 | RX_RING_SIZE) > 0) { |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 724 | entry = macb_rx_ring_wrap(bp->rx_prepared_head); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 725 | |
| 726 | /* Make hw descriptor updates visible to CPU */ |
| 727 | rmb(); |
| 728 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 729 | bp->rx_prepared_head++; |
| 730 | |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 731 | if (!bp->rx_skbuff[entry]) { |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 732 | /* allocate sk_buff for this free entry in ring */ |
| 733 | skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size); |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 734 | if (unlikely(!skb)) { |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 735 | netdev_err(bp->dev, |
| 736 | "Unable to allocate sk_buff\n"); |
| 737 | break; |
| 738 | } |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 739 | |
| 740 | /* now fill corresponding descriptor entry */ |
| 741 | paddr = dma_map_single(&bp->pdev->dev, skb->data, |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 742 | bp->rx_buffer_size, |
| 743 | DMA_FROM_DEVICE); |
Soren Brinkmann | 9203090 | 2014-03-04 08:46:39 -0800 | [diff] [blame] | 744 | if (dma_mapping_error(&bp->pdev->dev, paddr)) { |
| 745 | dev_kfree_skb(skb); |
| 746 | break; |
| 747 | } |
| 748 | |
| 749 | bp->rx_skbuff[entry] = skb; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 750 | |
| 751 | if (entry == RX_RING_SIZE - 1) |
| 752 | paddr |= MACB_BIT(RX_WRAP); |
| 753 | bp->rx_ring[entry].addr = paddr; |
| 754 | bp->rx_ring[entry].ctrl = 0; |
| 755 | |
| 756 | /* properly align Ethernet header */ |
| 757 | skb_reserve(skb, NET_IP_ALIGN); |
Punnaiah Choudary Kalluri | d4c216c | 2015-04-29 08:34:46 +0530 | [diff] [blame] | 758 | } else { |
| 759 | bp->rx_ring[entry].addr &= ~MACB_BIT(RX_USED); |
| 760 | bp->rx_ring[entry].ctrl = 0; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | |
| 764 | /* Make descriptor updates visible to hardware */ |
| 765 | wmb(); |
| 766 | |
| 767 | netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n", |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 768 | bp->rx_prepared_head, bp->rx_tail); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | /* Mark DMA descriptors from begin up to and not including end as unused */ |
| 772 | static void discard_partial_frame(struct macb *bp, unsigned int begin, |
| 773 | unsigned int end) |
| 774 | { |
| 775 | unsigned int frag; |
| 776 | |
| 777 | for (frag = begin; frag != end; frag++) { |
| 778 | struct macb_dma_desc *desc = macb_rx_desc(bp, frag); |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 779 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 780 | desc->addr &= ~MACB_BIT(RX_USED); |
| 781 | } |
| 782 | |
| 783 | /* Make descriptor updates visible to hardware */ |
| 784 | wmb(); |
| 785 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 786 | /* When this happens, the hardware stats registers for |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 787 | * whatever caused this is updated, so we don't have to record |
| 788 | * anything. |
| 789 | */ |
| 790 | } |
| 791 | |
| 792 | static int gem_rx(struct macb *bp, int budget) |
| 793 | { |
| 794 | unsigned int len; |
| 795 | unsigned int entry; |
| 796 | struct sk_buff *skb; |
| 797 | struct macb_dma_desc *desc; |
| 798 | int count = 0; |
| 799 | |
| 800 | while (count < budget) { |
| 801 | u32 addr, ctrl; |
| 802 | |
| 803 | entry = macb_rx_ring_wrap(bp->rx_tail); |
| 804 | desc = &bp->rx_ring[entry]; |
| 805 | |
| 806 | /* Make hw descriptor updates visible to CPU */ |
| 807 | rmb(); |
| 808 | |
| 809 | addr = desc->addr; |
| 810 | ctrl = desc->ctrl; |
| 811 | |
| 812 | if (!(addr & MACB_BIT(RX_USED))) |
| 813 | break; |
| 814 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 815 | bp->rx_tail++; |
| 816 | count++; |
| 817 | |
| 818 | if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) { |
| 819 | netdev_err(bp->dev, |
| 820 | "not whole frame pointed by descriptor\n"); |
| 821 | bp->stats.rx_dropped++; |
| 822 | break; |
| 823 | } |
| 824 | skb = bp->rx_skbuff[entry]; |
| 825 | if (unlikely(!skb)) { |
| 826 | netdev_err(bp->dev, |
| 827 | "inconsistent Rx descriptor chain\n"); |
| 828 | bp->stats.rx_dropped++; |
| 829 | break; |
| 830 | } |
| 831 | /* now everything is ready for receiving packet */ |
| 832 | bp->rx_skbuff[entry] = NULL; |
Harini Katakam | 98b5a0f4 | 2015-05-06 22:27:17 +0530 | [diff] [blame] | 833 | len = ctrl & bp->rx_frm_len_mask; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 834 | |
| 835 | netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len); |
| 836 | |
| 837 | skb_put(skb, len); |
| 838 | addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, addr)); |
| 839 | dma_unmap_single(&bp->pdev->dev, addr, |
Soren Brinkmann | 48330e08 | 2014-03-04 08:46:40 -0800 | [diff] [blame] | 840 | bp->rx_buffer_size, DMA_FROM_DEVICE); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 841 | |
| 842 | skb->protocol = eth_type_trans(skb, bp->dev); |
| 843 | skb_checksum_none_assert(skb); |
Cyrille Pitchen | 924ec53 | 2014-07-24 13:51:01 +0200 | [diff] [blame] | 844 | if (bp->dev->features & NETIF_F_RXCSUM && |
| 845 | !(bp->dev->flags & IFF_PROMISC) && |
| 846 | GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK) |
| 847 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 848 | |
| 849 | bp->stats.rx_packets++; |
| 850 | bp->stats.rx_bytes += skb->len; |
| 851 | |
| 852 | #if defined(DEBUG) && defined(VERBOSE_DEBUG) |
| 853 | netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n", |
| 854 | skb->len, skb->csum); |
| 855 | print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1, |
Cyrille Pitchen | 51f8301 | 2014-12-11 11:15:54 +0100 | [diff] [blame] | 856 | skb_mac_header(skb), 16, true); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 857 | print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1, |
| 858 | skb->data, 32, true); |
| 859 | #endif |
| 860 | |
| 861 | netif_receive_skb(skb); |
| 862 | } |
| 863 | |
| 864 | gem_rx_refill(bp); |
| 865 | |
| 866 | return count; |
| 867 | } |
| 868 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 869 | static int macb_rx_frame(struct macb *bp, unsigned int first_frag, |
| 870 | unsigned int last_frag) |
| 871 | { |
| 872 | unsigned int len; |
| 873 | unsigned int frag; |
Havard Skinnemoen | 29bc2e1 | 2012-10-31 06:04:58 +0000 | [diff] [blame] | 874 | unsigned int offset; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 875 | struct sk_buff *skb; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 876 | struct macb_dma_desc *desc; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 877 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 878 | desc = macb_rx_desc(bp, last_frag); |
Harini Katakam | 98b5a0f4 | 2015-05-06 22:27:17 +0530 | [diff] [blame] | 879 | len = desc->ctrl & bp->rx_frm_len_mask; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 880 | |
Havard Skinnemoen | a268adb | 2012-10-31 06:04:52 +0000 | [diff] [blame] | 881 | netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n", |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 882 | macb_rx_ring_wrap(first_frag), |
| 883 | macb_rx_ring_wrap(last_frag), len); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 884 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 885 | /* The ethernet header starts NET_IP_ALIGN bytes into the |
Havard Skinnemoen | 29bc2e1 | 2012-10-31 06:04:58 +0000 | [diff] [blame] | 886 | * first buffer. Since the header is 14 bytes, this makes the |
| 887 | * payload word-aligned. |
| 888 | * |
| 889 | * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy |
| 890 | * the two padding bytes into the skb so that we avoid hitting |
| 891 | * the slowpath in memcpy(), and pull them off afterwards. |
| 892 | */ |
| 893 | skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 894 | if (!skb) { |
| 895 | bp->stats.rx_dropped++; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 896 | for (frag = first_frag; ; frag++) { |
| 897 | desc = macb_rx_desc(bp, frag); |
| 898 | desc->addr &= ~MACB_BIT(RX_USED); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 899 | if (frag == last_frag) |
| 900 | break; |
| 901 | } |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 902 | |
| 903 | /* Make descriptor updates visible to hardware */ |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 904 | wmb(); |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 905 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 906 | return 1; |
| 907 | } |
| 908 | |
Havard Skinnemoen | 29bc2e1 | 2012-10-31 06:04:58 +0000 | [diff] [blame] | 909 | offset = 0; |
| 910 | len += NET_IP_ALIGN; |
Eric Dumazet | bc8acf2 | 2010-09-02 13:07:41 -0700 | [diff] [blame] | 911 | skb_checksum_none_assert(skb); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 912 | skb_put(skb, len); |
| 913 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 914 | for (frag = first_frag; ; frag++) { |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 915 | unsigned int frag_len = bp->rx_buffer_size; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 916 | |
| 917 | if (offset + frag_len > len) { |
Cyrille Pitchen | 9ba723b | 2016-03-25 10:37:34 +0100 | [diff] [blame] | 918 | if (unlikely(frag != last_frag)) { |
| 919 | dev_kfree_skb_any(skb); |
| 920 | return -1; |
| 921 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 922 | frag_len = len - offset; |
| 923 | } |
Arnaldo Carvalho de Melo | 27d7ff4 | 2007-03-31 11:55:19 -0300 | [diff] [blame] | 924 | skb_copy_to_linear_data_offset(skb, offset, |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 925 | macb_rx_buffer(bp, frag), |
| 926 | frag_len); |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 927 | offset += bp->rx_buffer_size; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 928 | desc = macb_rx_desc(bp, frag); |
| 929 | desc->addr &= ~MACB_BIT(RX_USED); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 930 | |
| 931 | if (frag == last_frag) |
| 932 | break; |
| 933 | } |
| 934 | |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 935 | /* Make descriptor updates visible to hardware */ |
| 936 | wmb(); |
| 937 | |
Havard Skinnemoen | 29bc2e1 | 2012-10-31 06:04:58 +0000 | [diff] [blame] | 938 | __skb_pull(skb, NET_IP_ALIGN); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 939 | skb->protocol = eth_type_trans(skb, bp->dev); |
| 940 | |
| 941 | bp->stats.rx_packets++; |
Havard Skinnemoen | 29bc2e1 | 2012-10-31 06:04:58 +0000 | [diff] [blame] | 942 | bp->stats.rx_bytes += skb->len; |
Havard Skinnemoen | a268adb | 2012-10-31 06:04:52 +0000 | [diff] [blame] | 943 | netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n", |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 944 | skb->len, skb->csum); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 945 | netif_receive_skb(skb); |
| 946 | |
| 947 | return 0; |
| 948 | } |
| 949 | |
Cyrille Pitchen | 9ba723b | 2016-03-25 10:37:34 +0100 | [diff] [blame] | 950 | static inline void macb_init_rx_ring(struct macb *bp) |
| 951 | { |
| 952 | dma_addr_t addr; |
| 953 | int i; |
| 954 | |
| 955 | addr = bp->rx_buffers_dma; |
| 956 | for (i = 0; i < RX_RING_SIZE; i++) { |
| 957 | bp->rx_ring[i].addr = addr; |
| 958 | bp->rx_ring[i].ctrl = 0; |
| 959 | addr += bp->rx_buffer_size; |
| 960 | } |
| 961 | bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP); |
| 962 | } |
| 963 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 964 | static int macb_rx(struct macb *bp, int budget) |
| 965 | { |
Cyrille Pitchen | 9ba723b | 2016-03-25 10:37:34 +0100 | [diff] [blame] | 966 | bool reset_rx_queue = false; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 967 | int received = 0; |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 968 | unsigned int tail; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 969 | int first_frag = -1; |
| 970 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 971 | for (tail = bp->rx_tail; budget > 0; tail++) { |
| 972 | struct macb_dma_desc *desc = macb_rx_desc(bp, tail); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 973 | u32 addr, ctrl; |
| 974 | |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 975 | /* Make hw descriptor updates visible to CPU */ |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 976 | rmb(); |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 977 | |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 978 | addr = desc->addr; |
| 979 | ctrl = desc->ctrl; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 980 | |
| 981 | if (!(addr & MACB_BIT(RX_USED))) |
| 982 | break; |
| 983 | |
| 984 | if (ctrl & MACB_BIT(RX_SOF)) { |
| 985 | if (first_frag != -1) |
| 986 | discard_partial_frame(bp, first_frag, tail); |
| 987 | first_frag = tail; |
| 988 | } |
| 989 | |
| 990 | if (ctrl & MACB_BIT(RX_EOF)) { |
| 991 | int dropped; |
Cyrille Pitchen | 9ba723b | 2016-03-25 10:37:34 +0100 | [diff] [blame] | 992 | |
| 993 | if (unlikely(first_frag == -1)) { |
| 994 | reset_rx_queue = true; |
| 995 | continue; |
| 996 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 997 | |
| 998 | dropped = macb_rx_frame(bp, first_frag, tail); |
| 999 | first_frag = -1; |
Cyrille Pitchen | 9ba723b | 2016-03-25 10:37:34 +0100 | [diff] [blame] | 1000 | if (unlikely(dropped < 0)) { |
| 1001 | reset_rx_queue = true; |
| 1002 | continue; |
| 1003 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1004 | if (!dropped) { |
| 1005 | received++; |
| 1006 | budget--; |
| 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 | |
Cyrille Pitchen | 9ba723b | 2016-03-25 10:37:34 +0100 | [diff] [blame] | 1011 | if (unlikely(reset_rx_queue)) { |
| 1012 | unsigned long flags; |
| 1013 | u32 ctrl; |
| 1014 | |
| 1015 | netdev_err(bp->dev, "RX queue corruption: reset it\n"); |
| 1016 | |
| 1017 | spin_lock_irqsave(&bp->lock, flags); |
| 1018 | |
| 1019 | ctrl = macb_readl(bp, NCR); |
| 1020 | macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE)); |
| 1021 | |
| 1022 | macb_init_rx_ring(bp); |
| 1023 | macb_writel(bp, RBQP, bp->rx_ring_dma); |
| 1024 | |
| 1025 | macb_writel(bp, NCR, ctrl | MACB_BIT(RE)); |
| 1026 | |
| 1027 | spin_unlock_irqrestore(&bp->lock, flags); |
| 1028 | return received; |
| 1029 | } |
| 1030 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1031 | if (first_frag != -1) |
| 1032 | bp->rx_tail = first_frag; |
| 1033 | else |
| 1034 | bp->rx_tail = tail; |
| 1035 | |
| 1036 | return received; |
| 1037 | } |
| 1038 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1039 | static int macb_poll(struct napi_struct *napi, int budget) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1040 | { |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1041 | struct macb *bp = container_of(napi, struct macb, napi); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1042 | int work_done; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1043 | u32 status; |
| 1044 | |
| 1045 | status = macb_readl(bp, RSR); |
| 1046 | macb_writel(bp, RSR, status); |
| 1047 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1048 | work_done = 0; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1049 | |
Havard Skinnemoen | a268adb | 2012-10-31 06:04:52 +0000 | [diff] [blame] | 1050 | netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n", |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 1051 | (unsigned long)status, budget); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1052 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1053 | work_done = bp->macbgem_ops.mog_rx(bp, budget); |
Joshua Hoke | b336369 | 2010-10-25 01:44:22 +0000 | [diff] [blame] | 1054 | if (work_done < budget) { |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 1055 | napi_complete(napi); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1056 | |
Nicolas Ferre | 8770e91 | 2013-02-12 11:08:48 +0100 | [diff] [blame] | 1057 | /* Packets received while interrupts were disabled */ |
| 1058 | status = macb_readl(bp, RSR); |
Soren Brinkmann | 504ad98 | 2014-05-04 15:43:01 -0700 | [diff] [blame] | 1059 | if (status) { |
Soren Brinkmann | 02f7a34 | 2014-05-04 15:43:00 -0700 | [diff] [blame] | 1060 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
| 1061 | macb_writel(bp, ISR, MACB_BIT(RCOMP)); |
Nicolas Ferre | 8770e91 | 2013-02-12 11:08:48 +0100 | [diff] [blame] | 1062 | napi_reschedule(napi); |
Soren Brinkmann | 02f7a34 | 2014-05-04 15:43:00 -0700 | [diff] [blame] | 1063 | } else { |
| 1064 | macb_writel(bp, IER, MACB_RX_INT_FLAGS); |
| 1065 | } |
Joshua Hoke | b336369 | 2010-10-25 01:44:22 +0000 | [diff] [blame] | 1066 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1067 | |
| 1068 | /* TODO: Handle errors */ |
| 1069 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1070 | return work_done; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | static irqreturn_t macb_interrupt(int irq, void *dev_id) |
| 1074 | { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1075 | struct macb_queue *queue = dev_id; |
| 1076 | struct macb *bp = queue->bp; |
| 1077 | struct net_device *dev = bp->dev; |
Nathan Sullivan | bfbb92c | 2015-05-05 15:00:25 -0500 | [diff] [blame] | 1078 | u32 status, ctrl; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1079 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1080 | status = queue_readl(queue, ISR); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1081 | |
| 1082 | if (unlikely(!status)) |
| 1083 | return IRQ_NONE; |
| 1084 | |
| 1085 | spin_lock(&bp->lock); |
| 1086 | |
| 1087 | while (status) { |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1088 | /* close possible race with dev_close */ |
| 1089 | if (unlikely(!netif_running(dev))) { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1090 | queue_writel(queue, IDR, -1); |
Nathan Sullivan | 2446837 | 2016-01-14 13:27:27 -0600 | [diff] [blame] | 1091 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
| 1092 | queue_writel(queue, ISR, -1); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1093 | break; |
| 1094 | } |
| 1095 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1096 | netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n", |
| 1097 | (unsigned int)(queue - bp->queues), |
| 1098 | (unsigned long)status); |
Havard Skinnemoen | a268adb | 2012-10-31 06:04:52 +0000 | [diff] [blame] | 1099 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1100 | if (status & MACB_RX_INT_FLAGS) { |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1101 | /* There's no point taking any more interrupts |
Joshua Hoke | b336369 | 2010-10-25 01:44:22 +0000 | [diff] [blame] | 1102 | * until we have processed the buffers. The |
| 1103 | * scheduling call may fail if the poll routine |
| 1104 | * is already scheduled, so disable interrupts |
| 1105 | * now. |
| 1106 | */ |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1107 | queue_writel(queue, IDR, MACB_RX_INT_FLAGS); |
Nicolas Ferre | 581df9e | 2013-05-14 03:00:16 +0000 | [diff] [blame] | 1108 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1109 | queue_writel(queue, ISR, MACB_BIT(RCOMP)); |
Joshua Hoke | b336369 | 2010-10-25 01:44:22 +0000 | [diff] [blame] | 1110 | |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 1111 | if (napi_schedule_prep(&bp->napi)) { |
Havard Skinnemoen | a268adb | 2012-10-31 06:04:52 +0000 | [diff] [blame] | 1112 | netdev_vdbg(bp->dev, "scheduling RX softirq\n"); |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 1113 | __napi_schedule(&bp->napi); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1117 | if (unlikely(status & (MACB_TX_ERR_FLAGS))) { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1118 | queue_writel(queue, IDR, MACB_TX_INT_FLAGS); |
| 1119 | schedule_work(&queue->tx_error_task); |
Soren Brinkmann | 6a027b7 | 2014-05-04 15:42:59 -0700 | [diff] [blame] | 1120 | |
| 1121 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1122 | queue_writel(queue, ISR, MACB_TX_ERR_FLAGS); |
Soren Brinkmann | 6a027b7 | 2014-05-04 15:42:59 -0700 | [diff] [blame] | 1123 | |
Nicolas Ferre | e86cd53 | 2012-10-31 06:04:57 +0000 | [diff] [blame] | 1124 | break; |
| 1125 | } |
| 1126 | |
| 1127 | if (status & MACB_BIT(TCOMP)) |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1128 | macb_tx_interrupt(queue); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1129 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1130 | /* Link change detection isn't possible with RMII, so we'll |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1131 | * add that if/when we get our hands on a full-blown MII PHY. |
| 1132 | */ |
| 1133 | |
Nathan Sullivan | 86b5e7d | 2015-05-13 17:01:36 -0500 | [diff] [blame] | 1134 | /* There is a hardware issue under heavy load where DMA can |
| 1135 | * stop, this causes endless "used buffer descriptor read" |
| 1136 | * interrupts but it can be cleared by re-enabling RX. See |
| 1137 | * the at91 manual, section 41.3.1 or the Zynq manual |
| 1138 | * section 16.7.4 for details. |
| 1139 | */ |
Nathan Sullivan | bfbb92c | 2015-05-05 15:00:25 -0500 | [diff] [blame] | 1140 | if (status & MACB_BIT(RXUBR)) { |
| 1141 | ctrl = macb_readl(bp, NCR); |
| 1142 | macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE)); |
| 1143 | macb_writel(bp, NCR, ctrl | MACB_BIT(RE)); |
| 1144 | |
| 1145 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
Cyrille Pitchen | ba50499 | 2016-03-24 15:40:04 +0100 | [diff] [blame] | 1146 | queue_writel(queue, ISR, MACB_BIT(RXUBR)); |
Nathan Sullivan | bfbb92c | 2015-05-05 15:00:25 -0500 | [diff] [blame] | 1147 | } |
| 1148 | |
Alexander Stein | b19f7f7 | 2011-04-13 05:03:24 +0000 | [diff] [blame] | 1149 | if (status & MACB_BIT(ISR_ROVR)) { |
| 1150 | /* We missed at least one packet */ |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1151 | if (macb_is_gem(bp)) |
| 1152 | bp->hw_stats.gem.rx_overruns++; |
| 1153 | else |
| 1154 | bp->hw_stats.macb.rx_overruns++; |
Soren Brinkmann | 6a027b7 | 2014-05-04 15:42:59 -0700 | [diff] [blame] | 1155 | |
| 1156 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1157 | queue_writel(queue, ISR, MACB_BIT(ISR_ROVR)); |
Alexander Stein | b19f7f7 | 2011-04-13 05:03:24 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1160 | if (status & MACB_BIT(HRESP)) { |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1161 | /* TODO: Reset the hardware, and maybe move the |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 1162 | * netdev_err to a lower-priority context as well |
| 1163 | * (work queue?) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1164 | */ |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 1165 | netdev_err(dev, "DMA bus error: HRESP not OK\n"); |
Soren Brinkmann | 6a027b7 | 2014-05-04 15:42:59 -0700 | [diff] [blame] | 1166 | |
| 1167 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1168 | queue_writel(queue, ISR, MACB_BIT(HRESP)); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1169 | } |
| 1170 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1171 | status = queue_readl(queue, ISR); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | spin_unlock(&bp->lock); |
| 1175 | |
| 1176 | return IRQ_HANDLED; |
| 1177 | } |
| 1178 | |
Thomas Petazzoni | 6e8cf5c | 2009-05-04 11:08:41 -0700 | [diff] [blame] | 1179 | #ifdef CONFIG_NET_POLL_CONTROLLER |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1180 | /* Polling receive - used by netconsole and other diagnostic tools |
Thomas Petazzoni | 6e8cf5c | 2009-05-04 11:08:41 -0700 | [diff] [blame] | 1181 | * to allow network i/o with interrupts disabled. |
| 1182 | */ |
| 1183 | static void macb_poll_controller(struct net_device *dev) |
| 1184 | { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1185 | struct macb *bp = netdev_priv(dev); |
| 1186 | struct macb_queue *queue; |
Thomas Petazzoni | 6e8cf5c | 2009-05-04 11:08:41 -0700 | [diff] [blame] | 1187 | unsigned long flags; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1188 | unsigned int q; |
Thomas Petazzoni | 6e8cf5c | 2009-05-04 11:08:41 -0700 | [diff] [blame] | 1189 | |
| 1190 | local_irq_save(flags); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1191 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) |
| 1192 | macb_interrupt(dev->irq, queue); |
Thomas Petazzoni | 6e8cf5c | 2009-05-04 11:08:41 -0700 | [diff] [blame] | 1193 | local_irq_restore(flags); |
| 1194 | } |
| 1195 | #endif |
| 1196 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1197 | static unsigned int macb_tx_map(struct macb *bp, |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1198 | struct macb_queue *queue, |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1199 | struct sk_buff *skb) |
| 1200 | { |
| 1201 | dma_addr_t mapping; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1202 | unsigned int len, entry, i, tx_head = queue->tx_head; |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1203 | struct macb_tx_skb *tx_skb = NULL; |
| 1204 | struct macb_dma_desc *desc; |
| 1205 | unsigned int offset, size, count = 0; |
| 1206 | unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags; |
| 1207 | unsigned int eof = 1; |
| 1208 | u32 ctrl; |
| 1209 | |
| 1210 | /* First, map non-paged data */ |
| 1211 | len = skb_headlen(skb); |
| 1212 | offset = 0; |
| 1213 | while (len) { |
| 1214 | size = min(len, bp->max_tx_length); |
| 1215 | entry = macb_tx_ring_wrap(tx_head); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1216 | tx_skb = &queue->tx_skb[entry]; |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1217 | |
| 1218 | mapping = dma_map_single(&bp->pdev->dev, |
| 1219 | skb->data + offset, |
| 1220 | size, DMA_TO_DEVICE); |
| 1221 | if (dma_mapping_error(&bp->pdev->dev, mapping)) |
| 1222 | goto dma_error; |
| 1223 | |
| 1224 | /* Save info to properly release resources */ |
| 1225 | tx_skb->skb = NULL; |
| 1226 | tx_skb->mapping = mapping; |
| 1227 | tx_skb->size = size; |
| 1228 | tx_skb->mapped_as_page = false; |
| 1229 | |
| 1230 | len -= size; |
| 1231 | offset += size; |
| 1232 | count++; |
| 1233 | tx_head++; |
| 1234 | } |
| 1235 | |
| 1236 | /* Then, map paged data from fragments */ |
| 1237 | for (f = 0; f < nr_frags; f++) { |
| 1238 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[f]; |
| 1239 | |
| 1240 | len = skb_frag_size(frag); |
| 1241 | offset = 0; |
| 1242 | while (len) { |
| 1243 | size = min(len, bp->max_tx_length); |
| 1244 | entry = macb_tx_ring_wrap(tx_head); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1245 | tx_skb = &queue->tx_skb[entry]; |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1246 | |
| 1247 | mapping = skb_frag_dma_map(&bp->pdev->dev, frag, |
| 1248 | offset, size, DMA_TO_DEVICE); |
| 1249 | if (dma_mapping_error(&bp->pdev->dev, mapping)) |
| 1250 | goto dma_error; |
| 1251 | |
| 1252 | /* Save info to properly release resources */ |
| 1253 | tx_skb->skb = NULL; |
| 1254 | tx_skb->mapping = mapping; |
| 1255 | tx_skb->size = size; |
| 1256 | tx_skb->mapped_as_page = true; |
| 1257 | |
| 1258 | len -= size; |
| 1259 | offset += size; |
| 1260 | count++; |
| 1261 | tx_head++; |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | /* Should never happen */ |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 1266 | if (unlikely(!tx_skb)) { |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1267 | netdev_err(bp->dev, "BUG! empty skb!\n"); |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
| 1271 | /* This is the last buffer of the frame: save socket buffer */ |
| 1272 | tx_skb->skb = skb; |
| 1273 | |
| 1274 | /* Update TX ring: update buffer descriptors in reverse order |
| 1275 | * to avoid race condition |
| 1276 | */ |
| 1277 | |
| 1278 | /* Set 'TX_USED' bit in buffer descriptor at tx_head position |
| 1279 | * to set the end of TX queue |
| 1280 | */ |
| 1281 | i = tx_head; |
| 1282 | entry = macb_tx_ring_wrap(i); |
| 1283 | ctrl = MACB_BIT(TX_USED); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1284 | desc = &queue->tx_ring[entry]; |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1285 | desc->ctrl = ctrl; |
| 1286 | |
| 1287 | do { |
| 1288 | i--; |
| 1289 | entry = macb_tx_ring_wrap(i); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1290 | tx_skb = &queue->tx_skb[entry]; |
| 1291 | desc = &queue->tx_ring[entry]; |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1292 | |
| 1293 | ctrl = (u32)tx_skb->size; |
| 1294 | if (eof) { |
| 1295 | ctrl |= MACB_BIT(TX_LAST); |
| 1296 | eof = 0; |
| 1297 | } |
| 1298 | if (unlikely(entry == (TX_RING_SIZE - 1))) |
| 1299 | ctrl |= MACB_BIT(TX_WRAP); |
| 1300 | |
| 1301 | /* Set TX buffer descriptor */ |
| 1302 | desc->addr = tx_skb->mapping; |
| 1303 | /* desc->addr must be visible to hardware before clearing |
| 1304 | * 'TX_USED' bit in desc->ctrl. |
| 1305 | */ |
| 1306 | wmb(); |
| 1307 | desc->ctrl = ctrl; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1308 | } while (i != queue->tx_head); |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1309 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1310 | queue->tx_head = tx_head; |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1311 | |
| 1312 | return count; |
| 1313 | |
| 1314 | dma_error: |
| 1315 | netdev_err(bp->dev, "TX DMA map failed\n"); |
| 1316 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1317 | for (i = queue->tx_head; i != tx_head; i++) { |
| 1318 | tx_skb = macb_tx_skb(queue, i); |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1319 | |
| 1320 | macb_tx_unmap(bp, tx_skb); |
| 1321 | } |
| 1322 | |
| 1323 | return 0; |
| 1324 | } |
| 1325 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1326 | static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 1327 | { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1328 | u16 queue_index = skb_get_queue_mapping(skb); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1329 | struct macb *bp = netdev_priv(dev); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1330 | struct macb_queue *queue = &bp->queues[queue_index]; |
Dongdong Deng | 4871953 | 2009-08-23 19:49:07 -0700 | [diff] [blame] | 1331 | unsigned long flags; |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1332 | unsigned int count, nr_frags, frag_size, f; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1333 | |
Havard Skinnemoen | a268adb | 2012-10-31 06:04:52 +0000 | [diff] [blame] | 1334 | #if defined(DEBUG) && defined(VERBOSE_DEBUG) |
| 1335 | netdev_vdbg(bp->dev, |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 1336 | "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n", |
| 1337 | queue_index, skb->len, skb->head, skb->data, |
| 1338 | skb_tail_pointer(skb), skb_end_pointer(skb)); |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 1339 | print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1, |
| 1340 | skb->data, 16, true); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1341 | #endif |
| 1342 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1343 | /* Count how many TX buffer descriptors are needed to send this |
| 1344 | * socket buffer: skb fragments of jumbo frames may need to be |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 1345 | * split into many buffer descriptors. |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1346 | */ |
Andy Shevchenko | 94b295e | 2015-07-24 21:24:03 +0300 | [diff] [blame] | 1347 | count = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length); |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1348 | nr_frags = skb_shinfo(skb)->nr_frags; |
| 1349 | for (f = 0; f < nr_frags; f++) { |
| 1350 | frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]); |
Andy Shevchenko | 94b295e | 2015-07-24 21:24:03 +0300 | [diff] [blame] | 1351 | count += DIV_ROUND_UP(frag_size, bp->max_tx_length); |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1352 | } |
| 1353 | |
Dongdong Deng | 4871953 | 2009-08-23 19:49:07 -0700 | [diff] [blame] | 1354 | spin_lock_irqsave(&bp->lock, flags); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1355 | |
| 1356 | /* This is a hard error, log it. */ |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1357 | if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < count) { |
| 1358 | netif_stop_subqueue(dev, queue_index); |
Dongdong Deng | 4871953 | 2009-08-23 19:49:07 -0700 | [diff] [blame] | 1359 | spin_unlock_irqrestore(&bp->lock, flags); |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 1360 | netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n", |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1361 | queue->tx_head, queue->tx_tail); |
Patrick McHardy | 5b54814 | 2009-06-12 06:22:29 +0000 | [diff] [blame] | 1362 | return NETDEV_TX_BUSY; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1363 | } |
| 1364 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 1365 | /* Map socket buffer for DMA transfer */ |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1366 | if (!macb_tx_map(bp, queue, skb)) { |
Eric W. Biederman | c88b5b6 | 2014-03-15 16:08:27 -0700 | [diff] [blame] | 1367 | dev_kfree_skb_any(skb); |
Soren Brinkmann | 9203090 | 2014-03-04 08:46:39 -0800 | [diff] [blame] | 1368 | goto unlock; |
| 1369 | } |
Havard Skinnemoen | 55054a1 | 2012-10-31 06:04:55 +0000 | [diff] [blame] | 1370 | |
Havard Skinnemoen | 03dbe05 | 2012-10-31 06:04:51 +0000 | [diff] [blame] | 1371 | /* Make newly initialized descriptor visible to hardware */ |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1372 | wmb(); |
| 1373 | |
Richard Cochran | e072092 | 2011-06-19 21:51:28 +0000 | [diff] [blame] | 1374 | skb_tx_timestamp(skb); |
| 1375 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1376 | macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART)); |
| 1377 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1378 | if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < 1) |
| 1379 | netif_stop_subqueue(dev, queue_index); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1380 | |
Soren Brinkmann | 9203090 | 2014-03-04 08:46:39 -0800 | [diff] [blame] | 1381 | unlock: |
Dongdong Deng | 4871953 | 2009-08-23 19:49:07 -0700 | [diff] [blame] | 1382 | spin_unlock_irqrestore(&bp->lock, flags); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1383 | |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 1384 | return NETDEV_TX_OK; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1385 | } |
| 1386 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1387 | 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] | 1388 | { |
| 1389 | if (!macb_is_gem(bp)) { |
| 1390 | bp->rx_buffer_size = MACB_RX_BUFFER_SIZE; |
| 1391 | } else { |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1392 | bp->rx_buffer_size = size; |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1393 | |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1394 | if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) { |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1395 | netdev_dbg(bp->dev, |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 1396 | "RX buffer must be multiple of %d bytes, expanding\n", |
| 1397 | RX_BUFFER_MULTIPLE); |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1398 | bp->rx_buffer_size = |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1399 | roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE); |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1400 | } |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1401 | } |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1402 | |
| 1403 | netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n", |
| 1404 | bp->dev->mtu, bp->rx_buffer_size); |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1407 | static void gem_free_rx_buffers(struct macb *bp) |
| 1408 | { |
| 1409 | struct sk_buff *skb; |
| 1410 | struct macb_dma_desc *desc; |
| 1411 | dma_addr_t addr; |
| 1412 | int i; |
| 1413 | |
| 1414 | if (!bp->rx_skbuff) |
| 1415 | return; |
| 1416 | |
| 1417 | for (i = 0; i < RX_RING_SIZE; i++) { |
| 1418 | skb = bp->rx_skbuff[i]; |
| 1419 | |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 1420 | if (!skb) |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1421 | continue; |
| 1422 | |
| 1423 | desc = &bp->rx_ring[i]; |
| 1424 | addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr)); |
Soren Brinkmann | ccd6d0a | 2014-05-04 15:42:58 -0700 | [diff] [blame] | 1425 | dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size, |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1426 | DMA_FROM_DEVICE); |
| 1427 | dev_kfree_skb_any(skb); |
| 1428 | skb = NULL; |
| 1429 | } |
| 1430 | |
| 1431 | kfree(bp->rx_skbuff); |
| 1432 | bp->rx_skbuff = NULL; |
| 1433 | } |
| 1434 | |
| 1435 | static void macb_free_rx_buffers(struct macb *bp) |
| 1436 | { |
| 1437 | if (bp->rx_buffers) { |
| 1438 | dma_free_coherent(&bp->pdev->dev, |
| 1439 | RX_RING_SIZE * bp->rx_buffer_size, |
| 1440 | bp->rx_buffers, bp->rx_buffers_dma); |
| 1441 | bp->rx_buffers = NULL; |
| 1442 | } |
| 1443 | } |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1444 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1445 | static void macb_free_consistent(struct macb *bp) |
| 1446 | { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1447 | struct macb_queue *queue; |
| 1448 | unsigned int q; |
| 1449 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1450 | bp->macbgem_ops.mog_free_rx_buffers(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1451 | if (bp->rx_ring) { |
| 1452 | dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES, |
| 1453 | bp->rx_ring, bp->rx_ring_dma); |
| 1454 | bp->rx_ring = NULL; |
| 1455 | } |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1456 | |
| 1457 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { |
| 1458 | kfree(queue->tx_skb); |
| 1459 | queue->tx_skb = NULL; |
| 1460 | if (queue->tx_ring) { |
| 1461 | dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES, |
| 1462 | queue->tx_ring, queue->tx_ring_dma); |
| 1463 | queue->tx_ring = NULL; |
| 1464 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1465 | } |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | static int gem_alloc_rx_buffers(struct macb *bp) |
| 1469 | { |
| 1470 | int size; |
| 1471 | |
| 1472 | size = RX_RING_SIZE * sizeof(struct sk_buff *); |
| 1473 | bp->rx_skbuff = kzalloc(size, GFP_KERNEL); |
| 1474 | if (!bp->rx_skbuff) |
| 1475 | return -ENOMEM; |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1476 | |
| 1477 | netdev_dbg(bp->dev, |
| 1478 | "Allocated %d RX struct sk_buff entries at %p\n", |
| 1479 | RX_RING_SIZE, bp->rx_skbuff); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1480 | return 0; |
| 1481 | } |
| 1482 | |
| 1483 | static int macb_alloc_rx_buffers(struct macb *bp) |
| 1484 | { |
| 1485 | int size; |
| 1486 | |
| 1487 | size = RX_RING_SIZE * bp->rx_buffer_size; |
| 1488 | bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size, |
| 1489 | &bp->rx_buffers_dma, GFP_KERNEL); |
| 1490 | if (!bp->rx_buffers) |
| 1491 | return -ENOMEM; |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1492 | |
| 1493 | netdev_dbg(bp->dev, |
| 1494 | "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n", |
| 1495 | size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1496 | return 0; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | static int macb_alloc_consistent(struct macb *bp) |
| 1500 | { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1501 | struct macb_queue *queue; |
| 1502 | unsigned int q; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1503 | int size; |
| 1504 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1505 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { |
| 1506 | size = TX_RING_BYTES; |
| 1507 | queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size, |
| 1508 | &queue->tx_ring_dma, |
| 1509 | GFP_KERNEL); |
| 1510 | if (!queue->tx_ring) |
| 1511 | goto out_err; |
| 1512 | netdev_dbg(bp->dev, |
| 1513 | "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n", |
| 1514 | q, size, (unsigned long)queue->tx_ring_dma, |
| 1515 | queue->tx_ring); |
| 1516 | |
| 1517 | size = TX_RING_SIZE * sizeof(struct macb_tx_skb); |
| 1518 | queue->tx_skb = kmalloc(size, GFP_KERNEL); |
| 1519 | if (!queue->tx_skb) |
| 1520 | goto out_err; |
| 1521 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1522 | |
| 1523 | size = RX_RING_BYTES; |
| 1524 | bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size, |
| 1525 | &bp->rx_ring_dma, GFP_KERNEL); |
| 1526 | if (!bp->rx_ring) |
| 1527 | goto out_err; |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 1528 | netdev_dbg(bp->dev, |
| 1529 | "Allocated RX ring of %d bytes at %08lx (mapped %p)\n", |
| 1530 | size, (unsigned long)bp->rx_ring_dma, bp->rx_ring); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1531 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1532 | if (bp->macbgem_ops.mog_alloc_rx_buffers(bp)) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1533 | goto out_err; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1534 | |
| 1535 | return 0; |
| 1536 | |
| 1537 | out_err: |
| 1538 | macb_free_consistent(bp); |
| 1539 | return -ENOMEM; |
| 1540 | } |
| 1541 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1542 | static void gem_init_rings(struct macb *bp) |
| 1543 | { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1544 | struct macb_queue *queue; |
| 1545 | unsigned int q; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1546 | int i; |
| 1547 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1548 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { |
| 1549 | for (i = 0; i < TX_RING_SIZE; i++) { |
| 1550 | queue->tx_ring[i].addr = 0; |
| 1551 | queue->tx_ring[i].ctrl = MACB_BIT(TX_USED); |
| 1552 | } |
| 1553 | queue->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP); |
| 1554 | queue->tx_head = 0; |
| 1555 | queue->tx_tail = 0; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1556 | } |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1557 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1558 | bp->rx_tail = 0; |
| 1559 | bp->rx_prepared_head = 0; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1560 | |
| 1561 | gem_rx_refill(bp); |
| 1562 | } |
| 1563 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1564 | static void macb_init_rings(struct macb *bp) |
| 1565 | { |
| 1566 | int i; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1567 | |
Cyrille Pitchen | 9ba723b | 2016-03-25 10:37:34 +0100 | [diff] [blame] | 1568 | macb_init_rx_ring(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1569 | |
| 1570 | for (i = 0; i < TX_RING_SIZE; i++) { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1571 | bp->queues[0].tx_ring[i].addr = 0; |
| 1572 | bp->queues[0].tx_ring[i].ctrl = MACB_BIT(TX_USED); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1573 | } |
Ben Shelton | 21d3515 | 2015-04-22 17:28:54 -0500 | [diff] [blame] | 1574 | bp->queues[0].tx_head = 0; |
| 1575 | bp->queues[0].tx_tail = 0; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1576 | bp->queues[0].tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1577 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1578 | bp->rx_tail = 0; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | static void macb_reset_hw(struct macb *bp) |
| 1582 | { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1583 | struct macb_queue *queue; |
| 1584 | unsigned int q; |
| 1585 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1586 | /* Disable RX and TX (XXX: Should we halt the transmission |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1587 | * more gracefully?) |
| 1588 | */ |
| 1589 | macb_writel(bp, NCR, 0); |
| 1590 | |
| 1591 | /* Clear the stats registers (XXX: Update stats first?) */ |
| 1592 | macb_writel(bp, NCR, MACB_BIT(CLRSTAT)); |
| 1593 | |
| 1594 | /* Clear all status flags */ |
Joachim Eastwood | 95ebcea | 2012-10-22 08:45:31 +0000 | [diff] [blame] | 1595 | macb_writel(bp, TSR, -1); |
| 1596 | macb_writel(bp, RSR, -1); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1597 | |
| 1598 | /* Disable all interrupts */ |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1599 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { |
| 1600 | queue_writel(queue, IDR, -1); |
| 1601 | queue_readl(queue, ISR); |
Nathan Sullivan | 2446837 | 2016-01-14 13:27:27 -0600 | [diff] [blame] | 1602 | if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE) |
| 1603 | queue_writel(queue, ISR, -1); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1604 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1605 | } |
| 1606 | |
Jamie Iles | 70c9f3d | 2011-03-09 16:22:54 +0000 | [diff] [blame] | 1607 | static u32 gem_mdc_clk_div(struct macb *bp) |
| 1608 | { |
| 1609 | u32 config; |
| 1610 | unsigned long pclk_hz = clk_get_rate(bp->pclk); |
| 1611 | |
| 1612 | if (pclk_hz <= 20000000) |
| 1613 | config = GEM_BF(CLK, GEM_CLK_DIV8); |
| 1614 | else if (pclk_hz <= 40000000) |
| 1615 | config = GEM_BF(CLK, GEM_CLK_DIV16); |
| 1616 | else if (pclk_hz <= 80000000) |
| 1617 | config = GEM_BF(CLK, GEM_CLK_DIV32); |
| 1618 | else if (pclk_hz <= 120000000) |
| 1619 | config = GEM_BF(CLK, GEM_CLK_DIV48); |
| 1620 | else if (pclk_hz <= 160000000) |
| 1621 | config = GEM_BF(CLK, GEM_CLK_DIV64); |
| 1622 | else |
| 1623 | config = GEM_BF(CLK, GEM_CLK_DIV96); |
| 1624 | |
| 1625 | return config; |
| 1626 | } |
| 1627 | |
| 1628 | static u32 macb_mdc_clk_div(struct macb *bp) |
| 1629 | { |
| 1630 | u32 config; |
| 1631 | unsigned long pclk_hz; |
| 1632 | |
| 1633 | if (macb_is_gem(bp)) |
| 1634 | return gem_mdc_clk_div(bp); |
| 1635 | |
| 1636 | pclk_hz = clk_get_rate(bp->pclk); |
| 1637 | if (pclk_hz <= 20000000) |
| 1638 | config = MACB_BF(CLK, MACB_CLK_DIV8); |
| 1639 | else if (pclk_hz <= 40000000) |
| 1640 | config = MACB_BF(CLK, MACB_CLK_DIV16); |
| 1641 | else if (pclk_hz <= 80000000) |
| 1642 | config = MACB_BF(CLK, MACB_CLK_DIV32); |
| 1643 | else |
| 1644 | config = MACB_BF(CLK, MACB_CLK_DIV64); |
| 1645 | |
| 1646 | return config; |
| 1647 | } |
| 1648 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1649 | /* Get the DMA bus width field of the network configuration register that we |
Jamie Iles | 757a03c | 2011-03-09 16:29:59 +0000 | [diff] [blame] | 1650 | * should program. We find the width from decoding the design configuration |
| 1651 | * register to find the maximum supported data bus width. |
| 1652 | */ |
| 1653 | static u32 macb_dbw(struct macb *bp) |
| 1654 | { |
| 1655 | if (!macb_is_gem(bp)) |
| 1656 | return 0; |
| 1657 | |
| 1658 | switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) { |
| 1659 | case 4: |
| 1660 | return GEM_BF(DBW, GEM_DBW128); |
| 1661 | case 2: |
| 1662 | return GEM_BF(DBW, GEM_DBW64); |
| 1663 | case 1: |
| 1664 | default: |
| 1665 | return GEM_BF(DBW, GEM_DBW32); |
| 1666 | } |
| 1667 | } |
| 1668 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1669 | /* Configure the receive DMA engine |
Nicolas Ferre | b3e3bd71 | 2012-11-23 03:49:01 +0000 | [diff] [blame] | 1670 | * - use the correct receive buffer size |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 1671 | * - set best burst length for DMA operations |
Nicolas Ferre | b3e3bd71 | 2012-11-23 03:49:01 +0000 | [diff] [blame] | 1672 | * (if not supported by FIFO, it will fallback to default) |
| 1673 | * - set both rx/tx packet buffers to full memory size |
| 1674 | * These are configurable parameters for GEM. |
Jamie Iles | 0116da4 | 2011-03-14 17:38:30 +0000 | [diff] [blame] | 1675 | */ |
| 1676 | static void macb_configure_dma(struct macb *bp) |
| 1677 | { |
| 1678 | u32 dmacfg; |
| 1679 | |
| 1680 | if (macb_is_gem(bp)) { |
| 1681 | dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L); |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1682 | dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE); |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 1683 | if (bp->dma_burst_length) |
| 1684 | dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg); |
Nicolas Ferre | b3e3bd71 | 2012-11-23 03:49:01 +0000 | [diff] [blame] | 1685 | dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L); |
Arun Chandran | a50dad3 | 2015-02-18 16:59:35 +0530 | [diff] [blame] | 1686 | dmacfg &= ~GEM_BIT(ENDIA_PKT); |
Arun Chandran | 62f6924 | 2015-03-01 11:38:02 +0530 | [diff] [blame] | 1687 | |
Andy Shevchenko | f2ce8a9 | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 1688 | if (bp->native_io) |
Arun Chandran | 62f6924 | 2015-03-01 11:38:02 +0530 | [diff] [blame] | 1689 | dmacfg &= ~GEM_BIT(ENDIA_DESC); |
| 1690 | else |
| 1691 | dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */ |
| 1692 | |
Cyrille Pitchen | 85ff3d8 | 2014-07-24 13:51:00 +0200 | [diff] [blame] | 1693 | if (bp->dev->features & NETIF_F_HW_CSUM) |
| 1694 | dmacfg |= GEM_BIT(TXCOEN); |
| 1695 | else |
| 1696 | dmacfg &= ~GEM_BIT(TXCOEN); |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 1697 | netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n", |
| 1698 | dmacfg); |
Jamie Iles | 0116da4 | 2011-03-14 17:38:30 +0000 | [diff] [blame] | 1699 | gem_writel(bp, DMACFG, dmacfg); |
| 1700 | } |
| 1701 | } |
| 1702 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1703 | static void macb_init_hw(struct macb *bp) |
| 1704 | { |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1705 | struct macb_queue *queue; |
| 1706 | unsigned int q; |
| 1707 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1708 | u32 config; |
| 1709 | |
| 1710 | macb_reset_hw(bp); |
Joachim Eastwood | 314bccc | 2012-11-07 08:14:52 +0000 | [diff] [blame] | 1711 | macb_set_hwaddr(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1712 | |
Jamie Iles | 70c9f3d | 2011-03-09 16:22:54 +0000 | [diff] [blame] | 1713 | config = macb_mdc_clk_div(bp); |
Punnaiah Choudary Kalluri | 022be25 | 2015-11-18 09:03:50 +0530 | [diff] [blame] | 1714 | if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII) |
| 1715 | config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL); |
Havard Skinnemoen | 29bc2e1 | 2012-10-31 06:04:58 +0000 | [diff] [blame] | 1716 | config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */ |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1717 | config |= MACB_BIT(PAE); /* PAuse Enable */ |
| 1718 | config |= MACB_BIT(DRFCS); /* Discard Rx FCS */ |
Dan Carpenter | a104a6b | 2015-05-12 21:15:24 +0300 | [diff] [blame] | 1719 | if (bp->caps & MACB_CAPS_JUMBO) |
Harini Katakam | 98b5a0f4 | 2015-05-06 22:27:17 +0530 | [diff] [blame] | 1720 | config |= MACB_BIT(JFRAME); /* Enable jumbo frames */ |
| 1721 | else |
| 1722 | config |= MACB_BIT(BIG); /* Receive oversized frames */ |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1723 | if (bp->dev->flags & IFF_PROMISC) |
| 1724 | config |= MACB_BIT(CAF); /* Copy All Frames */ |
Cyrille Pitchen | 924ec53 | 2014-07-24 13:51:01 +0200 | [diff] [blame] | 1725 | else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM) |
| 1726 | config |= GEM_BIT(RXCOEN); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1727 | if (!(bp->dev->flags & IFF_BROADCAST)) |
| 1728 | config |= MACB_BIT(NBC); /* No BroadCast */ |
Jamie Iles | 757a03c | 2011-03-09 16:29:59 +0000 | [diff] [blame] | 1729 | config |= macb_dbw(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1730 | macb_writel(bp, NCFGR, config); |
Dan Carpenter | a104a6b | 2015-05-12 21:15:24 +0300 | [diff] [blame] | 1731 | if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len) |
Harini Katakam | 98b5a0f4 | 2015-05-06 22:27:17 +0530 | [diff] [blame] | 1732 | gem_writel(bp, JML, bp->jumbo_max_len); |
Vitalii Demianets | 26cdfb4 | 2012-11-02 07:09:24 +0000 | [diff] [blame] | 1733 | bp->speed = SPEED_10; |
| 1734 | bp->duplex = DUPLEX_HALF; |
Harini Katakam | 98b5a0f4 | 2015-05-06 22:27:17 +0530 | [diff] [blame] | 1735 | bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK; |
Dan Carpenter | a104a6b | 2015-05-12 21:15:24 +0300 | [diff] [blame] | 1736 | if (bp->caps & MACB_CAPS_JUMBO) |
Harini Katakam | 98b5a0f4 | 2015-05-06 22:27:17 +0530 | [diff] [blame] | 1737 | bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1738 | |
Jamie Iles | 0116da4 | 2011-03-14 17:38:30 +0000 | [diff] [blame] | 1739 | macb_configure_dma(bp); |
| 1740 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1741 | /* Initialize TX and RX buffers */ |
| 1742 | macb_writel(bp, RBQP, bp->rx_ring_dma); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1743 | for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) { |
| 1744 | queue_writel(queue, TBQP, queue->tx_ring_dma); |
| 1745 | |
| 1746 | /* Enable interrupts */ |
| 1747 | queue_writel(queue, IER, |
| 1748 | MACB_RX_INT_FLAGS | |
| 1749 | MACB_TX_INT_FLAGS | |
| 1750 | MACB_BIT(HRESP)); |
| 1751 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1752 | |
| 1753 | /* Enable TX and RX */ |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1754 | 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] | 1755 | } |
| 1756 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1757 | /* The hash address register is 64 bits long and takes up two |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1758 | * locations in the memory map. The least significant bits are stored |
| 1759 | * in EMAC_HSL and the most significant bits in EMAC_HSH. |
| 1760 | * |
| 1761 | * The unicast hash enable and the multicast hash enable bits in the |
| 1762 | * network configuration register enable the reception of hash matched |
| 1763 | * frames. The destination address is reduced to a 6 bit index into |
| 1764 | * the 64 bit hash register using the following hash function. The |
| 1765 | * hash function is an exclusive or of every sixth bit of the |
| 1766 | * destination address. |
| 1767 | * |
| 1768 | * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47] |
| 1769 | * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46] |
| 1770 | * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45] |
| 1771 | * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44] |
| 1772 | * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43] |
| 1773 | * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42] |
| 1774 | * |
| 1775 | * da[0] represents the least significant bit of the first byte |
| 1776 | * received, that is, the multicast/unicast indicator, and da[47] |
| 1777 | * represents the most significant bit of the last byte received. If |
| 1778 | * the hash index, hi[n], points to a bit that is set in the hash |
| 1779 | * register then the frame will be matched according to whether the |
| 1780 | * frame is multicast or unicast. A multicast match will be signalled |
| 1781 | * if the multicast hash enable bit is set, da[0] is 1 and the hash |
| 1782 | * index points to a bit set in the hash register. A unicast match |
| 1783 | * will be signalled if the unicast hash enable bit is set, da[0] is 0 |
| 1784 | * and the hash index points to a bit set in the hash register. To |
| 1785 | * receive all multicast frames, the hash register should be set with |
| 1786 | * all ones and the multicast hash enable bit should be set in the |
| 1787 | * network configuration register. |
| 1788 | */ |
| 1789 | |
| 1790 | static inline int hash_bit_value(int bitnr, __u8 *addr) |
| 1791 | { |
| 1792 | if (addr[bitnr / 8] & (1 << (bitnr % 8))) |
| 1793 | return 1; |
| 1794 | return 0; |
| 1795 | } |
| 1796 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1797 | /* Return the hash index value for the specified address. */ |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1798 | static int hash_get_index(__u8 *addr) |
| 1799 | { |
| 1800 | int i, j, bitval; |
| 1801 | int hash_index = 0; |
| 1802 | |
| 1803 | for (j = 0; j < 6; j++) { |
| 1804 | for (i = 0, bitval = 0; i < 8; i++) |
Xander Huff | 2fa45e2 | 2015-01-15 15:55:19 -0600 | [diff] [blame] | 1805 | bitval ^= hash_bit_value(i * 6 + j, addr); |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1806 | |
| 1807 | hash_index |= (bitval << j); |
| 1808 | } |
| 1809 | |
| 1810 | return hash_index; |
| 1811 | } |
| 1812 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1813 | /* Add multicast addresses to the internal multicast-hash table. */ |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1814 | static void macb_sethashtable(struct net_device *dev) |
| 1815 | { |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 1816 | struct netdev_hw_addr *ha; |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1817 | unsigned long mc_filter[2]; |
Jiri Pirko | f9dcbcc | 2010-02-23 09:19:49 +0000 | [diff] [blame] | 1818 | unsigned int bitnr; |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1819 | struct macb *bp = netdev_priv(dev); |
| 1820 | |
Moritz Fischer | aa50b55 | 2016-03-29 19:11:13 -0700 | [diff] [blame] | 1821 | mc_filter[0] = 0; |
| 1822 | mc_filter[1] = 0; |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1823 | |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 1824 | netdev_for_each_mc_addr(ha, dev) { |
| 1825 | bitnr = hash_get_index(ha->addr); |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1826 | mc_filter[bitnr >> 5] |= 1 << (bitnr & 31); |
| 1827 | } |
| 1828 | |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1829 | macb_or_gem_writel(bp, HRB, mc_filter[0]); |
| 1830 | macb_or_gem_writel(bp, HRT, mc_filter[1]); |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1831 | } |
| 1832 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 1833 | /* Enable/Disable promiscuous and multicast modes. */ |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 1834 | static void macb_set_rx_mode(struct net_device *dev) |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1835 | { |
| 1836 | unsigned long cfg; |
| 1837 | struct macb *bp = netdev_priv(dev); |
| 1838 | |
| 1839 | cfg = macb_readl(bp, NCFGR); |
| 1840 | |
Cyrille Pitchen | 924ec53 | 2014-07-24 13:51:01 +0200 | [diff] [blame] | 1841 | if (dev->flags & IFF_PROMISC) { |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1842 | /* Enable promiscuous mode */ |
| 1843 | cfg |= MACB_BIT(CAF); |
Cyrille Pitchen | 924ec53 | 2014-07-24 13:51:01 +0200 | [diff] [blame] | 1844 | |
| 1845 | /* Disable RX checksum offload */ |
| 1846 | if (macb_is_gem(bp)) |
| 1847 | cfg &= ~GEM_BIT(RXCOEN); |
| 1848 | } else { |
| 1849 | /* Disable promiscuous mode */ |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1850 | cfg &= ~MACB_BIT(CAF); |
| 1851 | |
Cyrille Pitchen | 924ec53 | 2014-07-24 13:51:01 +0200 | [diff] [blame] | 1852 | /* Enable RX checksum offload only if requested */ |
| 1853 | if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM) |
| 1854 | cfg |= GEM_BIT(RXCOEN); |
| 1855 | } |
| 1856 | |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1857 | if (dev->flags & IFF_ALLMULTI) { |
| 1858 | /* Enable all multicast mode */ |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1859 | macb_or_gem_writel(bp, HRB, -1); |
| 1860 | macb_or_gem_writel(bp, HRT, -1); |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1861 | cfg |= MACB_BIT(NCFGR_MTI); |
Jiri Pirko | 4cd24ea | 2010-02-08 04:30:35 +0000 | [diff] [blame] | 1862 | } else if (!netdev_mc_empty(dev)) { |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1863 | /* Enable specific multicasts */ |
| 1864 | macb_sethashtable(dev); |
| 1865 | cfg |= MACB_BIT(NCFGR_MTI); |
| 1866 | } else if (dev->flags & (~IFF_ALLMULTI)) { |
| 1867 | /* Disable all multicast mode */ |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1868 | macb_or_gem_writel(bp, HRB, 0); |
| 1869 | macb_or_gem_writel(bp, HRT, 0); |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1870 | cfg &= ~MACB_BIT(NCFGR_MTI); |
| 1871 | } |
| 1872 | |
| 1873 | macb_writel(bp, NCFGR, cfg); |
| 1874 | } |
| 1875 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1876 | static int macb_open(struct net_device *dev) |
| 1877 | { |
| 1878 | struct macb *bp = netdev_priv(dev); |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1879 | 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] | 1880 | int err; |
| 1881 | |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 1882 | netdev_dbg(bp->dev, "open\n"); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1883 | |
Nicolas Ferre | 03fc472 | 2012-07-03 23:14:13 +0000 | [diff] [blame] | 1884 | /* carrier starts down */ |
| 1885 | netif_carrier_off(dev); |
| 1886 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1887 | /* if the phy is not yet register, retry later*/ |
Philippe Reynes | 0a91281 | 2016-06-22 00:32:35 +0200 | [diff] [blame] | 1888 | if (!dev->phydev) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1889 | return -EAGAIN; |
| 1890 | |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1891 | /* RX buffers initialization */ |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1892 | macb_init_rx_buffer_size(bp, bufsz); |
Nicolas Ferre | 1b44791 | 2013-06-04 21:57:11 +0000 | [diff] [blame] | 1893 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1894 | err = macb_alloc_consistent(bp); |
| 1895 | if (err) { |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 1896 | netdev_err(dev, "Unable to allocate DMA memory (error %d)\n", |
| 1897 | err); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1898 | return err; |
| 1899 | } |
| 1900 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1901 | napi_enable(&bp->napi); |
| 1902 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 1903 | bp->macbgem_ops.mog_init_rings(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1904 | macb_init_hw(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1905 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1906 | /* schedule a link state check */ |
Philippe Reynes | 0a91281 | 2016-06-22 00:32:35 +0200 | [diff] [blame] | 1907 | phy_start(dev->phydev); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1908 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1909 | netif_tx_start_all_queues(dev); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1910 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1911 | return 0; |
| 1912 | } |
| 1913 | |
| 1914 | static int macb_close(struct net_device *dev) |
| 1915 | { |
| 1916 | struct macb *bp = netdev_priv(dev); |
| 1917 | unsigned long flags; |
| 1918 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 1919 | netif_tx_stop_all_queues(dev); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1920 | napi_disable(&bp->napi); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1921 | |
Philippe Reynes | 0a91281 | 2016-06-22 00:32:35 +0200 | [diff] [blame] | 1922 | if (dev->phydev) |
| 1923 | phy_stop(dev->phydev); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1924 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1925 | spin_lock_irqsave(&bp->lock, flags); |
| 1926 | macb_reset_hw(bp); |
| 1927 | netif_carrier_off(dev); |
| 1928 | spin_unlock_irqrestore(&bp->lock, flags); |
| 1929 | |
| 1930 | macb_free_consistent(bp); |
| 1931 | |
| 1932 | return 0; |
| 1933 | } |
| 1934 | |
Harini Katakam | a5898ea | 2015-05-06 22:27:18 +0530 | [diff] [blame] | 1935 | static int macb_change_mtu(struct net_device *dev, int new_mtu) |
| 1936 | { |
| 1937 | struct macb *bp = netdev_priv(dev); |
| 1938 | u32 max_mtu; |
| 1939 | |
| 1940 | if (netif_running(dev)) |
| 1941 | return -EBUSY; |
| 1942 | |
| 1943 | max_mtu = ETH_DATA_LEN; |
Dan Carpenter | a104a6b | 2015-05-12 21:15:24 +0300 | [diff] [blame] | 1944 | if (bp->caps & MACB_CAPS_JUMBO) |
Harini Katakam | a5898ea | 2015-05-06 22:27:18 +0530 | [diff] [blame] | 1945 | max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN; |
| 1946 | |
| 1947 | if ((new_mtu > max_mtu) || (new_mtu < GEM_MTU_MIN_SIZE)) |
| 1948 | return -EINVAL; |
| 1949 | |
| 1950 | dev->mtu = new_mtu; |
| 1951 | |
| 1952 | return 0; |
| 1953 | } |
| 1954 | |
Jamie Iles | a494ed8 | 2011-03-09 16:26:35 +0000 | [diff] [blame] | 1955 | static void gem_update_stats(struct macb *bp) |
| 1956 | { |
Andy Shevchenko | 8bcbf82 | 2015-07-24 21:24:02 +0300 | [diff] [blame] | 1957 | unsigned int i; |
Jamie Iles | a494ed8 | 2011-03-09 16:26:35 +0000 | [diff] [blame] | 1958 | u32 *p = &bp->hw_stats.gem.tx_octets_31_0; |
Jamie Iles | a494ed8 | 2011-03-09 16:26:35 +0000 | [diff] [blame] | 1959 | |
Xander Huff | 3ff13f1 | 2015-01-13 16:15:51 -0600 | [diff] [blame] | 1960 | for (i = 0; i < GEM_STATS_LEN; ++i, ++p) { |
| 1961 | u32 offset = gem_statistics[i].offset; |
David S. Miller | 7a6e070 | 2015-07-27 14:24:48 -0700 | [diff] [blame] | 1962 | u64 val = bp->macb_reg_readl(bp, offset); |
Xander Huff | 3ff13f1 | 2015-01-13 16:15:51 -0600 | [diff] [blame] | 1963 | |
| 1964 | bp->ethtool_stats[i] += val; |
| 1965 | *p += val; |
| 1966 | |
| 1967 | if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) { |
| 1968 | /* Add GEM_OCTTXH, GEM_OCTRXH */ |
David S. Miller | 7a6e070 | 2015-07-27 14:24:48 -0700 | [diff] [blame] | 1969 | val = bp->macb_reg_readl(bp, offset + 4); |
Xander Huff | 2fa45e2 | 2015-01-15 15:55:19 -0600 | [diff] [blame] | 1970 | bp->ethtool_stats[i] += ((u64)val) << 32; |
Xander Huff | 3ff13f1 | 2015-01-13 16:15:51 -0600 | [diff] [blame] | 1971 | *(++p) += val; |
| 1972 | } |
| 1973 | } |
Jamie Iles | a494ed8 | 2011-03-09 16:26:35 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | static struct net_device_stats *gem_get_stats(struct macb *bp) |
| 1977 | { |
| 1978 | struct gem_stats *hwstat = &bp->hw_stats.gem; |
| 1979 | struct net_device_stats *nstat = &bp->stats; |
| 1980 | |
| 1981 | gem_update_stats(bp); |
| 1982 | |
| 1983 | nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors + |
| 1984 | hwstat->rx_alignment_errors + |
| 1985 | hwstat->rx_resource_errors + |
| 1986 | hwstat->rx_overruns + |
| 1987 | hwstat->rx_oversize_frames + |
| 1988 | hwstat->rx_jabbers + |
| 1989 | hwstat->rx_undersized_frames + |
| 1990 | hwstat->rx_length_field_frame_errors); |
| 1991 | nstat->tx_errors = (hwstat->tx_late_collisions + |
| 1992 | hwstat->tx_excessive_collisions + |
| 1993 | hwstat->tx_underrun + |
| 1994 | hwstat->tx_carrier_sense_errors); |
| 1995 | nstat->multicast = hwstat->rx_multicast_frames; |
| 1996 | nstat->collisions = (hwstat->tx_single_collision_frames + |
| 1997 | hwstat->tx_multiple_collision_frames + |
| 1998 | hwstat->tx_excessive_collisions); |
| 1999 | nstat->rx_length_errors = (hwstat->rx_oversize_frames + |
| 2000 | hwstat->rx_jabbers + |
| 2001 | hwstat->rx_undersized_frames + |
| 2002 | hwstat->rx_length_field_frame_errors); |
| 2003 | nstat->rx_over_errors = hwstat->rx_resource_errors; |
| 2004 | nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors; |
| 2005 | nstat->rx_frame_errors = hwstat->rx_alignment_errors; |
| 2006 | nstat->rx_fifo_errors = hwstat->rx_overruns; |
| 2007 | nstat->tx_aborted_errors = hwstat->tx_excessive_collisions; |
| 2008 | nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors; |
| 2009 | nstat->tx_fifo_errors = hwstat->tx_underrun; |
| 2010 | |
| 2011 | return nstat; |
| 2012 | } |
| 2013 | |
Xander Huff | 3ff13f1 | 2015-01-13 16:15:51 -0600 | [diff] [blame] | 2014 | static void gem_get_ethtool_stats(struct net_device *dev, |
| 2015 | struct ethtool_stats *stats, u64 *data) |
| 2016 | { |
| 2017 | struct macb *bp; |
| 2018 | |
| 2019 | bp = netdev_priv(dev); |
| 2020 | gem_update_stats(bp); |
Xander Huff | 2fa45e2 | 2015-01-15 15:55:19 -0600 | [diff] [blame] | 2021 | memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN); |
Xander Huff | 3ff13f1 | 2015-01-13 16:15:51 -0600 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | static int gem_get_sset_count(struct net_device *dev, int sset) |
| 2025 | { |
| 2026 | switch (sset) { |
| 2027 | case ETH_SS_STATS: |
| 2028 | return GEM_STATS_LEN; |
| 2029 | default: |
| 2030 | return -EOPNOTSUPP; |
| 2031 | } |
| 2032 | } |
| 2033 | |
| 2034 | static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p) |
| 2035 | { |
Andy Shevchenko | 8bcbf82 | 2015-07-24 21:24:02 +0300 | [diff] [blame] | 2036 | unsigned int i; |
Xander Huff | 3ff13f1 | 2015-01-13 16:15:51 -0600 | [diff] [blame] | 2037 | |
| 2038 | switch (sset) { |
| 2039 | case ETH_SS_STATS: |
| 2040 | for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN) |
| 2041 | memcpy(p, gem_statistics[i].stat_string, |
| 2042 | ETH_GSTRING_LEN); |
| 2043 | break; |
| 2044 | } |
| 2045 | } |
| 2046 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2047 | static struct net_device_stats *macb_get_stats(struct net_device *dev) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2048 | { |
| 2049 | struct macb *bp = netdev_priv(dev); |
| 2050 | struct net_device_stats *nstat = &bp->stats; |
Jamie Iles | a494ed8 | 2011-03-09 16:26:35 +0000 | [diff] [blame] | 2051 | struct macb_stats *hwstat = &bp->hw_stats.macb; |
| 2052 | |
| 2053 | if (macb_is_gem(bp)) |
| 2054 | return gem_get_stats(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2055 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 2056 | /* read stats from hardware */ |
| 2057 | macb_update_stats(bp); |
| 2058 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2059 | /* Convert HW stats into netdevice stats */ |
| 2060 | nstat->rx_errors = (hwstat->rx_fcs_errors + |
| 2061 | hwstat->rx_align_errors + |
| 2062 | hwstat->rx_resource_errors + |
| 2063 | hwstat->rx_overruns + |
| 2064 | hwstat->rx_oversize_pkts + |
| 2065 | hwstat->rx_jabbers + |
| 2066 | hwstat->rx_undersize_pkts + |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2067 | hwstat->rx_length_mismatch); |
| 2068 | nstat->tx_errors = (hwstat->tx_late_cols + |
| 2069 | hwstat->tx_excessive_cols + |
| 2070 | hwstat->tx_underruns + |
Wolfgang Steinwender | 716723c | 2015-04-10 11:42:56 +0200 | [diff] [blame] | 2071 | hwstat->tx_carrier_errors + |
| 2072 | hwstat->sqe_test_errors); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2073 | nstat->collisions = (hwstat->tx_single_cols + |
| 2074 | hwstat->tx_multiple_cols + |
| 2075 | hwstat->tx_excessive_cols); |
| 2076 | nstat->rx_length_errors = (hwstat->rx_oversize_pkts + |
| 2077 | hwstat->rx_jabbers + |
| 2078 | hwstat->rx_undersize_pkts + |
| 2079 | hwstat->rx_length_mismatch); |
Alexander Stein | b19f7f7 | 2011-04-13 05:03:24 +0000 | [diff] [blame] | 2080 | nstat->rx_over_errors = hwstat->rx_resource_errors + |
| 2081 | hwstat->rx_overruns; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2082 | nstat->rx_crc_errors = hwstat->rx_fcs_errors; |
| 2083 | nstat->rx_frame_errors = hwstat->rx_align_errors; |
| 2084 | nstat->rx_fifo_errors = hwstat->rx_overruns; |
| 2085 | /* XXX: What does "missed" mean? */ |
| 2086 | nstat->tx_aborted_errors = hwstat->tx_excessive_cols; |
| 2087 | nstat->tx_carrier_errors = hwstat->tx_carrier_errors; |
| 2088 | nstat->tx_fifo_errors = hwstat->tx_underruns; |
| 2089 | /* Don't know about heartbeat or window errors... */ |
| 2090 | |
| 2091 | return nstat; |
| 2092 | } |
| 2093 | |
Nicolas Ferre | d1d1b53 | 2012-10-31 06:04:56 +0000 | [diff] [blame] | 2094 | static int macb_get_regs_len(struct net_device *netdev) |
| 2095 | { |
| 2096 | return MACB_GREGS_NBR * sizeof(u32); |
| 2097 | } |
| 2098 | |
| 2099 | static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs, |
| 2100 | void *p) |
| 2101 | { |
| 2102 | struct macb *bp = netdev_priv(dev); |
| 2103 | unsigned int tail, head; |
| 2104 | u32 *regs_buff = p; |
| 2105 | |
| 2106 | regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1)) |
| 2107 | | MACB_GREGS_VERSION; |
| 2108 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2109 | tail = macb_tx_ring_wrap(bp->queues[0].tx_tail); |
| 2110 | head = macb_tx_ring_wrap(bp->queues[0].tx_head); |
Nicolas Ferre | d1d1b53 | 2012-10-31 06:04:56 +0000 | [diff] [blame] | 2111 | |
| 2112 | regs_buff[0] = macb_readl(bp, NCR); |
| 2113 | regs_buff[1] = macb_or_gem_readl(bp, NCFGR); |
| 2114 | regs_buff[2] = macb_readl(bp, NSR); |
| 2115 | regs_buff[3] = macb_readl(bp, TSR); |
| 2116 | regs_buff[4] = macb_readl(bp, RBQP); |
| 2117 | regs_buff[5] = macb_readl(bp, TBQP); |
| 2118 | regs_buff[6] = macb_readl(bp, RSR); |
| 2119 | regs_buff[7] = macb_readl(bp, IMR); |
| 2120 | |
| 2121 | regs_buff[8] = tail; |
| 2122 | regs_buff[9] = head; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2123 | regs_buff[10] = macb_tx_dma(&bp->queues[0], tail); |
| 2124 | regs_buff[11] = macb_tx_dma(&bp->queues[0], head); |
Nicolas Ferre | d1d1b53 | 2012-10-31 06:04:56 +0000 | [diff] [blame] | 2125 | |
Neil Armstrong | ce721a7 | 2016-01-05 14:39:16 +0100 | [diff] [blame] | 2126 | if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) |
| 2127 | regs_buff[12] = macb_or_gem_readl(bp, USRIO); |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 2128 | if (macb_is_gem(bp)) |
Nicolas Ferre | d1d1b53 | 2012-10-31 06:04:56 +0000 | [diff] [blame] | 2129 | regs_buff[13] = gem_readl(bp, DMACFG); |
Nicolas Ferre | d1d1b53 | 2012-10-31 06:04:56 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
Sergio Prado | 3e2a5e1 | 2016-02-09 12:07:16 -0200 | [diff] [blame] | 2132 | static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol) |
| 2133 | { |
| 2134 | struct macb *bp = netdev_priv(netdev); |
| 2135 | |
| 2136 | wol->supported = 0; |
| 2137 | wol->wolopts = 0; |
| 2138 | |
| 2139 | if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) { |
| 2140 | wol->supported = WAKE_MAGIC; |
| 2141 | |
| 2142 | if (bp->wol & MACB_WOL_ENABLED) |
| 2143 | wol->wolopts |= WAKE_MAGIC; |
| 2144 | } |
| 2145 | } |
| 2146 | |
| 2147 | static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol) |
| 2148 | { |
| 2149 | struct macb *bp = netdev_priv(netdev); |
| 2150 | |
| 2151 | if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) || |
| 2152 | (wol->wolopts & ~WAKE_MAGIC)) |
| 2153 | return -EOPNOTSUPP; |
| 2154 | |
| 2155 | if (wol->wolopts & WAKE_MAGIC) |
| 2156 | bp->wol |= MACB_WOL_ENABLED; |
| 2157 | else |
| 2158 | bp->wol &= ~MACB_WOL_ENABLED; |
| 2159 | |
| 2160 | device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED); |
| 2161 | |
| 2162 | return 0; |
| 2163 | } |
| 2164 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2165 | static const struct ethtool_ops macb_ethtool_ops = { |
Nicolas Ferre | d1d1b53 | 2012-10-31 06:04:56 +0000 | [diff] [blame] | 2166 | .get_regs_len = macb_get_regs_len, |
| 2167 | .get_regs = macb_get_regs, |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2168 | .get_link = ethtool_op_get_link, |
Richard Cochran | 17f393e | 2012-04-03 22:59:31 +0000 | [diff] [blame] | 2169 | .get_ts_info = ethtool_op_get_ts_info, |
Sergio Prado | 3e2a5e1 | 2016-02-09 12:07:16 -0200 | [diff] [blame] | 2170 | .get_wol = macb_get_wol, |
| 2171 | .set_wol = macb_set_wol, |
Philippe Reynes | 176275a | 2016-06-22 00:32:36 +0200 | [diff] [blame^] | 2172 | .get_link_ksettings = phy_ethtool_get_link_ksettings, |
| 2173 | .set_link_ksettings = phy_ethtool_set_link_ksettings, |
Xander Huff | 8cd5a56 | 2015-01-15 15:55:20 -0600 | [diff] [blame] | 2174 | }; |
Xander Huff | 8cd5a56 | 2015-01-15 15:55:20 -0600 | [diff] [blame] | 2175 | |
Lad, Prabhakar | 8093b1c | 2015-02-05 16:21:07 +0000 | [diff] [blame] | 2176 | static const struct ethtool_ops gem_ethtool_ops = { |
Xander Huff | 8cd5a56 | 2015-01-15 15:55:20 -0600 | [diff] [blame] | 2177 | .get_regs_len = macb_get_regs_len, |
| 2178 | .get_regs = macb_get_regs, |
| 2179 | .get_link = ethtool_op_get_link, |
| 2180 | .get_ts_info = ethtool_op_get_ts_info, |
Xander Huff | 3ff13f1 | 2015-01-13 16:15:51 -0600 | [diff] [blame] | 2181 | .get_ethtool_stats = gem_get_ethtool_stats, |
| 2182 | .get_strings = gem_get_ethtool_strings, |
| 2183 | .get_sset_count = gem_get_sset_count, |
Philippe Reynes | 176275a | 2016-06-22 00:32:36 +0200 | [diff] [blame^] | 2184 | .get_link_ksettings = phy_ethtool_get_link_ksettings, |
| 2185 | .set_link_ksettings = phy_ethtool_set_link_ksettings, |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2186 | }; |
| 2187 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2188 | static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2189 | { |
Philippe Reynes | 0a91281 | 2016-06-22 00:32:35 +0200 | [diff] [blame] | 2190 | struct phy_device *phydev = dev->phydev; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2191 | |
| 2192 | if (!netif_running(dev)) |
| 2193 | return -EINVAL; |
| 2194 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 2195 | if (!phydev) |
| 2196 | return -ENODEV; |
| 2197 | |
Richard Cochran | 28b0411 | 2010-07-17 08:48:55 +0000 | [diff] [blame] | 2198 | return phy_mii_ioctl(phydev, rq, cmd); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2199 | } |
| 2200 | |
Cyrille Pitchen | 85ff3d8 | 2014-07-24 13:51:00 +0200 | [diff] [blame] | 2201 | static int macb_set_features(struct net_device *netdev, |
| 2202 | netdev_features_t features) |
| 2203 | { |
| 2204 | struct macb *bp = netdev_priv(netdev); |
| 2205 | netdev_features_t changed = features ^ netdev->features; |
| 2206 | |
| 2207 | /* TX checksum offload */ |
| 2208 | if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) { |
| 2209 | u32 dmacfg; |
| 2210 | |
| 2211 | dmacfg = gem_readl(bp, DMACFG); |
| 2212 | if (features & NETIF_F_HW_CSUM) |
| 2213 | dmacfg |= GEM_BIT(TXCOEN); |
| 2214 | else |
| 2215 | dmacfg &= ~GEM_BIT(TXCOEN); |
| 2216 | gem_writel(bp, DMACFG, dmacfg); |
| 2217 | } |
| 2218 | |
Cyrille Pitchen | 924ec53 | 2014-07-24 13:51:01 +0200 | [diff] [blame] | 2219 | /* RX checksum offload */ |
| 2220 | if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) { |
| 2221 | u32 netcfg; |
| 2222 | |
| 2223 | netcfg = gem_readl(bp, NCFGR); |
| 2224 | if (features & NETIF_F_RXCSUM && |
| 2225 | !(netdev->flags & IFF_PROMISC)) |
| 2226 | netcfg |= GEM_BIT(RXCOEN); |
| 2227 | else |
| 2228 | netcfg &= ~GEM_BIT(RXCOEN); |
| 2229 | gem_writel(bp, NCFGR, netcfg); |
| 2230 | } |
| 2231 | |
Cyrille Pitchen | 85ff3d8 | 2014-07-24 13:51:00 +0200 | [diff] [blame] | 2232 | return 0; |
| 2233 | } |
| 2234 | |
Alexander Beregalov | 5f1fa99 | 2009-04-11 07:42:26 +0000 | [diff] [blame] | 2235 | static const struct net_device_ops macb_netdev_ops = { |
| 2236 | .ndo_open = macb_open, |
| 2237 | .ndo_stop = macb_close, |
| 2238 | .ndo_start_xmit = macb_start_xmit, |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame] | 2239 | .ndo_set_rx_mode = macb_set_rx_mode, |
Alexander Beregalov | 5f1fa99 | 2009-04-11 07:42:26 +0000 | [diff] [blame] | 2240 | .ndo_get_stats = macb_get_stats, |
| 2241 | .ndo_do_ioctl = macb_ioctl, |
| 2242 | .ndo_validate_addr = eth_validate_addr, |
Harini Katakam | a5898ea | 2015-05-06 22:27:18 +0530 | [diff] [blame] | 2243 | .ndo_change_mtu = macb_change_mtu, |
Alexander Beregalov | 5f1fa99 | 2009-04-11 07:42:26 +0000 | [diff] [blame] | 2244 | .ndo_set_mac_address = eth_mac_addr, |
Thomas Petazzoni | 6e8cf5c | 2009-05-04 11:08:41 -0700 | [diff] [blame] | 2245 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 2246 | .ndo_poll_controller = macb_poll_controller, |
| 2247 | #endif |
Cyrille Pitchen | 85ff3d8 | 2014-07-24 13:51:00 +0200 | [diff] [blame] | 2248 | .ndo_set_features = macb_set_features, |
Alexander Beregalov | 5f1fa99 | 2009-04-11 07:42:26 +0000 | [diff] [blame] | 2249 | }; |
| 2250 | |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 2251 | /* Configure peripheral capabilities according to device tree |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 2252 | * and integration options used |
| 2253 | */ |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 2254 | static void macb_configure_caps(struct macb *bp, |
| 2255 | const struct macb_config *dt_conf) |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 2256 | { |
| 2257 | u32 dcfg; |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 2258 | |
Nicolas Ferre | f697050 | 2015-03-31 15:02:01 +0200 | [diff] [blame] | 2259 | if (dt_conf) |
| 2260 | bp->caps = dt_conf->caps; |
| 2261 | |
Andy Shevchenko | f2ce8a9 | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 2262 | if (hw_is_gem(bp->regs, bp->native_io)) { |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 2263 | bp->caps |= MACB_CAPS_MACB_IS_GEM; |
| 2264 | |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 2265 | dcfg = gem_readl(bp, DCFG1); |
| 2266 | if (GEM_BFEXT(IRQCOR, dcfg) == 0) |
| 2267 | bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE; |
| 2268 | dcfg = gem_readl(bp, DCFG2); |
| 2269 | if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0) |
| 2270 | bp->caps |= MACB_CAPS_FIFO_MODE; |
| 2271 | } |
| 2272 | |
Andy Shevchenko | a35919e | 2015-07-24 21:24:01 +0300 | [diff] [blame] | 2273 | dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps); |
Nicolas Ferre | e175587 | 2014-07-24 13:50:58 +0200 | [diff] [blame] | 2274 | } |
| 2275 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2276 | static void macb_probe_queues(void __iomem *mem, |
Andy Shevchenko | f2ce8a9 | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 2277 | bool native_io, |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2278 | unsigned int *queue_mask, |
| 2279 | unsigned int *num_queues) |
| 2280 | { |
| 2281 | unsigned int hw_q; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2282 | |
| 2283 | *queue_mask = 0x1; |
| 2284 | *num_queues = 1; |
| 2285 | |
Nicolas Ferre | da12011 | 2015-03-31 15:02:00 +0200 | [diff] [blame] | 2286 | /* is it macb or gem ? |
| 2287 | * |
| 2288 | * We need to read directly from the hardware here because |
| 2289 | * we are early in the probe process and don't have the |
| 2290 | * MACB_CAPS_MACB_IS_GEM flag positioned |
| 2291 | */ |
Andy Shevchenko | f2ce8a9 | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 2292 | if (!hw_is_gem(mem, native_io)) |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2293 | return; |
| 2294 | |
| 2295 | /* bit 0 is never set but queue 0 always exists */ |
Arun Chandran | a50dad3 | 2015-02-18 16:59:35 +0530 | [diff] [blame] | 2296 | *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff; |
| 2297 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2298 | *queue_mask |= 0x1; |
| 2299 | |
| 2300 | for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q) |
| 2301 | if (*queue_mask & (1 << hw_q)) |
| 2302 | (*num_queues)++; |
| 2303 | } |
| 2304 | |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 2305 | static int macb_clk_init(struct platform_device *pdev, struct clk **pclk, |
| 2306 | struct clk **hclk, struct clk **tx_clk) |
| 2307 | { |
| 2308 | int err; |
| 2309 | |
| 2310 | *pclk = devm_clk_get(&pdev->dev, "pclk"); |
| 2311 | if (IS_ERR(*pclk)) { |
| 2312 | err = PTR_ERR(*pclk); |
| 2313 | dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err); |
| 2314 | return err; |
| 2315 | } |
| 2316 | |
| 2317 | *hclk = devm_clk_get(&pdev->dev, "hclk"); |
| 2318 | if (IS_ERR(*hclk)) { |
| 2319 | err = PTR_ERR(*hclk); |
| 2320 | dev_err(&pdev->dev, "failed to get hclk (%u)\n", err); |
| 2321 | return err; |
| 2322 | } |
| 2323 | |
| 2324 | *tx_clk = devm_clk_get(&pdev->dev, "tx_clk"); |
| 2325 | if (IS_ERR(*tx_clk)) |
| 2326 | *tx_clk = NULL; |
| 2327 | |
| 2328 | err = clk_prepare_enable(*pclk); |
| 2329 | if (err) { |
| 2330 | dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err); |
| 2331 | return err; |
| 2332 | } |
| 2333 | |
| 2334 | err = clk_prepare_enable(*hclk); |
| 2335 | if (err) { |
| 2336 | dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err); |
| 2337 | goto err_disable_pclk; |
| 2338 | } |
| 2339 | |
| 2340 | err = clk_prepare_enable(*tx_clk); |
| 2341 | if (err) { |
| 2342 | dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err); |
| 2343 | goto err_disable_hclk; |
| 2344 | } |
| 2345 | |
| 2346 | return 0; |
| 2347 | |
| 2348 | err_disable_hclk: |
| 2349 | clk_disable_unprepare(*hclk); |
| 2350 | |
| 2351 | err_disable_pclk: |
| 2352 | clk_disable_unprepare(*pclk); |
| 2353 | |
| 2354 | return err; |
| 2355 | } |
| 2356 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2357 | static int macb_init(struct platform_device *pdev) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2358 | { |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2359 | struct net_device *dev = platform_get_drvdata(pdev); |
Nicolas Ferre | bfa0914 | 2015-03-31 15:01:59 +0200 | [diff] [blame] | 2360 | unsigned int hw_q, q; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2361 | struct macb *bp = netdev_priv(dev); |
| 2362 | struct macb_queue *queue; |
| 2363 | int err; |
| 2364 | u32 val; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2365 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2366 | /* set the queue register mapping once for all: queue0 has a special |
| 2367 | * register mapping but we don't want to test the queue index then |
| 2368 | * compute the corresponding register offset at run time. |
| 2369 | */ |
Cyrille Pitchen | cf250de | 2014-12-15 15:13:32 +0100 | [diff] [blame] | 2370 | for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) { |
Nicolas Ferre | bfa0914 | 2015-03-31 15:01:59 +0200 | [diff] [blame] | 2371 | if (!(bp->queue_mask & (1 << hw_q))) |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2372 | continue; |
Jamie Iles | 461845d | 2011-03-08 20:19:23 +0000 | [diff] [blame] | 2373 | |
Cyrille Pitchen | cf250de | 2014-12-15 15:13:32 +0100 | [diff] [blame] | 2374 | queue = &bp->queues[q]; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2375 | queue->bp = bp; |
| 2376 | if (hw_q) { |
| 2377 | queue->ISR = GEM_ISR(hw_q - 1); |
| 2378 | queue->IER = GEM_IER(hw_q - 1); |
| 2379 | queue->IDR = GEM_IDR(hw_q - 1); |
| 2380 | queue->IMR = GEM_IMR(hw_q - 1); |
| 2381 | queue->TBQP = GEM_TBQP(hw_q - 1); |
| 2382 | } else { |
| 2383 | /* queue0 uses legacy registers */ |
| 2384 | queue->ISR = MACB_ISR; |
| 2385 | queue->IER = MACB_IER; |
| 2386 | queue->IDR = MACB_IDR; |
| 2387 | queue->IMR = MACB_IMR; |
| 2388 | queue->TBQP = MACB_TBQP; |
Soren Brinkmann | e1824df | 2013-12-10 16:07:23 -0800 | [diff] [blame] | 2389 | } |
Soren Brinkmann | e1824df | 2013-12-10 16:07:23 -0800 | [diff] [blame] | 2390 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2391 | /* get irq: here we use the linux queue index, not the hardware |
| 2392 | * queue index. the queue irq definitions in the device tree |
| 2393 | * must remove the optional gaps that could exist in the |
| 2394 | * hardware queue mask. |
| 2395 | */ |
Cyrille Pitchen | cf250de | 2014-12-15 15:13:32 +0100 | [diff] [blame] | 2396 | queue->irq = platform_get_irq(pdev, q); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2397 | err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt, |
Punnaiah Choudary Kalluri | 2048823 | 2015-03-06 18:29:12 +0100 | [diff] [blame] | 2398 | IRQF_SHARED, dev->name, queue); |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2399 | if (err) { |
| 2400 | dev_err(&pdev->dev, |
| 2401 | "Unable to request IRQ %d (error %d)\n", |
| 2402 | queue->irq, err); |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 2403 | return err; |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2404 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2405 | |
Cyrille Pitchen | 02c958d | 2014-12-12 13:26:44 +0100 | [diff] [blame] | 2406 | INIT_WORK(&queue->tx_error_task, macb_tx_error_task); |
Cyrille Pitchen | cf250de | 2014-12-15 15:13:32 +0100 | [diff] [blame] | 2407 | q++; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2408 | } |
| 2409 | |
Alexander Beregalov | 5f1fa99 | 2009-04-11 07:42:26 +0000 | [diff] [blame] | 2410 | dev->netdev_ops = &macb_netdev_ops; |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 2411 | netif_napi_add(dev, &bp->napi, macb_poll, 64); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2412 | |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2413 | /* setup appropriated routines according to adapter type */ |
| 2414 | if (macb_is_gem(bp)) { |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 2415 | bp->max_tx_length = GEM_MAX_TX_LEN; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2416 | bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers; |
| 2417 | bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers; |
| 2418 | bp->macbgem_ops.mog_init_rings = gem_init_rings; |
| 2419 | bp->macbgem_ops.mog_rx = gem_rx; |
Xander Huff | 8cd5a56 | 2015-01-15 15:55:20 -0600 | [diff] [blame] | 2420 | dev->ethtool_ops = &gem_ethtool_ops; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2421 | } else { |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 2422 | bp->max_tx_length = MACB_MAX_TX_LEN; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2423 | bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers; |
| 2424 | bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers; |
| 2425 | bp->macbgem_ops.mog_init_rings = macb_init_rings; |
| 2426 | bp->macbgem_ops.mog_rx = macb_rx; |
Xander Huff | 8cd5a56 | 2015-01-15 15:55:20 -0600 | [diff] [blame] | 2427 | dev->ethtool_ops = &macb_ethtool_ops; |
Nicolas Ferre | 4df9513 | 2013-06-04 21:57:12 +0000 | [diff] [blame] | 2428 | } |
| 2429 | |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 2430 | /* Set features */ |
| 2431 | dev->hw_features = NETIF_F_SG; |
Cyrille Pitchen | 85ff3d8 | 2014-07-24 13:51:00 +0200 | [diff] [blame] | 2432 | /* Checksum offload is only available on gem with packet buffer */ |
| 2433 | if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE)) |
Cyrille Pitchen | 924ec53 | 2014-07-24 13:51:01 +0200 | [diff] [blame] | 2434 | dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM; |
Cyrille Pitchen | a4c35ed3 | 2014-07-24 13:50:59 +0200 | [diff] [blame] | 2435 | if (bp->caps & MACB_CAPS_SG_DISABLED) |
| 2436 | dev->hw_features &= ~NETIF_F_SG; |
| 2437 | dev->features = dev->hw_features; |
| 2438 | |
Neil Armstrong | ce721a7 | 2016-01-05 14:39:16 +0100 | [diff] [blame] | 2439 | if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) { |
| 2440 | val = 0; |
| 2441 | if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII) |
| 2442 | val = GEM_BIT(RGMII); |
| 2443 | else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII && |
Nicolas Ferre | 6bdaa5e | 2016-03-10 16:44:32 +0100 | [diff] [blame] | 2444 | (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII)) |
Neil Armstrong | ce721a7 | 2016-01-05 14:39:16 +0100 | [diff] [blame] | 2445 | val = MACB_BIT(RMII); |
Nicolas Ferre | 6bdaa5e | 2016-03-10 16:44:32 +0100 | [diff] [blame] | 2446 | else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII)) |
Neil Armstrong | ce721a7 | 2016-01-05 14:39:16 +0100 | [diff] [blame] | 2447 | val = MACB_BIT(MII); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2448 | |
Neil Armstrong | ce721a7 | 2016-01-05 14:39:16 +0100 | [diff] [blame] | 2449 | if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN) |
| 2450 | val |= MACB_BIT(CLKEN); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2451 | |
Neil Armstrong | ce721a7 | 2016-01-05 14:39:16 +0100 | [diff] [blame] | 2452 | macb_or_gem_writel(bp, USRIO, val); |
| 2453 | } |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2454 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2455 | /* Set MII management clock divider */ |
| 2456 | val = macb_mdc_clk_div(bp); |
| 2457 | val |= macb_dbw(bp); |
Punnaiah Choudary Kalluri | 022be25 | 2015-11-18 09:03:50 +0530 | [diff] [blame] | 2458 | if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII) |
| 2459 | val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2460 | macb_writel(bp, NCFGR, val); |
| 2461 | |
| 2462 | return 0; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2463 | } |
| 2464 | |
| 2465 | #if defined(CONFIG_OF) |
| 2466 | /* 1518 rounded up */ |
| 2467 | #define AT91ETHER_MAX_RBUFF_SZ 0x600 |
| 2468 | /* max number of receive buffers */ |
| 2469 | #define AT91ETHER_MAX_RX_DESCR 9 |
| 2470 | |
| 2471 | /* Initialize and start the Receiver and Transmit subsystems */ |
| 2472 | static int at91ether_start(struct net_device *dev) |
| 2473 | { |
| 2474 | struct macb *lp = netdev_priv(dev); |
| 2475 | dma_addr_t addr; |
| 2476 | u32 ctl; |
| 2477 | int i; |
| 2478 | |
| 2479 | lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev, |
| 2480 | (AT91ETHER_MAX_RX_DESCR * |
| 2481 | sizeof(struct macb_dma_desc)), |
| 2482 | &lp->rx_ring_dma, GFP_KERNEL); |
| 2483 | if (!lp->rx_ring) |
| 2484 | return -ENOMEM; |
| 2485 | |
| 2486 | lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev, |
| 2487 | AT91ETHER_MAX_RX_DESCR * |
| 2488 | AT91ETHER_MAX_RBUFF_SZ, |
| 2489 | &lp->rx_buffers_dma, GFP_KERNEL); |
| 2490 | if (!lp->rx_buffers) { |
| 2491 | dma_free_coherent(&lp->pdev->dev, |
| 2492 | AT91ETHER_MAX_RX_DESCR * |
| 2493 | sizeof(struct macb_dma_desc), |
| 2494 | lp->rx_ring, lp->rx_ring_dma); |
| 2495 | lp->rx_ring = NULL; |
| 2496 | return -ENOMEM; |
| 2497 | } |
| 2498 | |
| 2499 | addr = lp->rx_buffers_dma; |
| 2500 | for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) { |
| 2501 | lp->rx_ring[i].addr = addr; |
| 2502 | lp->rx_ring[i].ctrl = 0; |
| 2503 | addr += AT91ETHER_MAX_RBUFF_SZ; |
| 2504 | } |
| 2505 | |
| 2506 | /* Set the Wrap bit on the last descriptor */ |
| 2507 | lp->rx_ring[AT91ETHER_MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP); |
| 2508 | |
| 2509 | /* Reset buffer index */ |
| 2510 | lp->rx_tail = 0; |
| 2511 | |
| 2512 | /* Program address of descriptor list in Rx Buffer Queue register */ |
| 2513 | macb_writel(lp, RBQP, lp->rx_ring_dma); |
| 2514 | |
| 2515 | /* Enable Receive and Transmit */ |
| 2516 | ctl = macb_readl(lp, NCR); |
| 2517 | macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE)); |
| 2518 | |
| 2519 | return 0; |
| 2520 | } |
| 2521 | |
| 2522 | /* Open the ethernet interface */ |
| 2523 | static int at91ether_open(struct net_device *dev) |
| 2524 | { |
| 2525 | struct macb *lp = netdev_priv(dev); |
| 2526 | u32 ctl; |
| 2527 | int ret; |
| 2528 | |
| 2529 | /* Clear internal statistics */ |
| 2530 | ctl = macb_readl(lp, NCR); |
| 2531 | macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT)); |
| 2532 | |
| 2533 | macb_set_hwaddr(lp); |
| 2534 | |
| 2535 | ret = at91ether_start(dev); |
| 2536 | if (ret) |
| 2537 | return ret; |
| 2538 | |
| 2539 | /* Enable MAC interrupts */ |
| 2540 | macb_writel(lp, IER, MACB_BIT(RCOMP) | |
| 2541 | MACB_BIT(RXUBR) | |
| 2542 | MACB_BIT(ISR_TUND) | |
| 2543 | MACB_BIT(ISR_RLE) | |
| 2544 | MACB_BIT(TCOMP) | |
| 2545 | MACB_BIT(ISR_ROVR) | |
| 2546 | MACB_BIT(HRESP)); |
| 2547 | |
| 2548 | /* schedule a link state check */ |
Philippe Reynes | 0a91281 | 2016-06-22 00:32:35 +0200 | [diff] [blame] | 2549 | phy_start(dev->phydev); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2550 | |
| 2551 | netif_start_queue(dev); |
| 2552 | |
| 2553 | return 0; |
| 2554 | } |
| 2555 | |
| 2556 | /* Close the interface */ |
| 2557 | static int at91ether_close(struct net_device *dev) |
| 2558 | { |
| 2559 | struct macb *lp = netdev_priv(dev); |
| 2560 | u32 ctl; |
| 2561 | |
| 2562 | /* Disable Receiver and Transmitter */ |
| 2563 | ctl = macb_readl(lp, NCR); |
| 2564 | macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE))); |
| 2565 | |
| 2566 | /* Disable MAC interrupts */ |
| 2567 | macb_writel(lp, IDR, MACB_BIT(RCOMP) | |
| 2568 | MACB_BIT(RXUBR) | |
| 2569 | MACB_BIT(ISR_TUND) | |
| 2570 | MACB_BIT(ISR_RLE) | |
| 2571 | MACB_BIT(TCOMP) | |
| 2572 | MACB_BIT(ISR_ROVR) | |
| 2573 | MACB_BIT(HRESP)); |
| 2574 | |
| 2575 | netif_stop_queue(dev); |
| 2576 | |
| 2577 | dma_free_coherent(&lp->pdev->dev, |
| 2578 | AT91ETHER_MAX_RX_DESCR * |
| 2579 | sizeof(struct macb_dma_desc), |
| 2580 | lp->rx_ring, lp->rx_ring_dma); |
| 2581 | lp->rx_ring = NULL; |
| 2582 | |
| 2583 | dma_free_coherent(&lp->pdev->dev, |
| 2584 | AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ, |
| 2585 | lp->rx_buffers, lp->rx_buffers_dma); |
| 2586 | lp->rx_buffers = NULL; |
| 2587 | |
| 2588 | return 0; |
| 2589 | } |
| 2590 | |
| 2591 | /* Transmit packet */ |
| 2592 | static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 2593 | { |
| 2594 | struct macb *lp = netdev_priv(dev); |
| 2595 | |
| 2596 | if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) { |
| 2597 | netif_stop_queue(dev); |
| 2598 | |
| 2599 | /* Store packet information (to free when Tx completed) */ |
| 2600 | lp->skb = skb; |
| 2601 | lp->skb_length = skb->len; |
| 2602 | lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len, |
| 2603 | DMA_TO_DEVICE); |
| 2604 | |
| 2605 | /* Set address of the data in the Transmit Address register */ |
| 2606 | macb_writel(lp, TAR, lp->skb_physaddr); |
| 2607 | /* Set length of the packet in the Transmit Control register */ |
| 2608 | macb_writel(lp, TCR, skb->len); |
| 2609 | |
| 2610 | } else { |
| 2611 | netdev_err(dev, "%s called, but device is busy!\n", __func__); |
| 2612 | return NETDEV_TX_BUSY; |
| 2613 | } |
| 2614 | |
| 2615 | return NETDEV_TX_OK; |
| 2616 | } |
| 2617 | |
| 2618 | /* Extract received frame from buffer descriptors and sent to upper layers. |
| 2619 | * (Called from interrupt context) |
| 2620 | */ |
| 2621 | static void at91ether_rx(struct net_device *dev) |
| 2622 | { |
| 2623 | struct macb *lp = netdev_priv(dev); |
| 2624 | unsigned char *p_recv; |
| 2625 | struct sk_buff *skb; |
| 2626 | unsigned int pktlen; |
| 2627 | |
| 2628 | while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) { |
| 2629 | p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ; |
| 2630 | pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl); |
| 2631 | skb = netdev_alloc_skb(dev, pktlen + 2); |
| 2632 | if (skb) { |
| 2633 | skb_reserve(skb, 2); |
| 2634 | memcpy(skb_put(skb, pktlen), p_recv, pktlen); |
| 2635 | |
| 2636 | skb->protocol = eth_type_trans(skb, dev); |
| 2637 | lp->stats.rx_packets++; |
| 2638 | lp->stats.rx_bytes += pktlen; |
| 2639 | netif_rx(skb); |
| 2640 | } else { |
| 2641 | lp->stats.rx_dropped++; |
| 2642 | } |
| 2643 | |
| 2644 | if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH)) |
| 2645 | lp->stats.multicast++; |
| 2646 | |
| 2647 | /* reset ownership bit */ |
| 2648 | lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED); |
| 2649 | |
| 2650 | /* wrap after last buffer */ |
| 2651 | if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1) |
| 2652 | lp->rx_tail = 0; |
| 2653 | else |
| 2654 | lp->rx_tail++; |
| 2655 | } |
| 2656 | } |
| 2657 | |
| 2658 | /* MAC interrupt handler */ |
| 2659 | static irqreturn_t at91ether_interrupt(int irq, void *dev_id) |
| 2660 | { |
| 2661 | struct net_device *dev = dev_id; |
| 2662 | struct macb *lp = netdev_priv(dev); |
| 2663 | u32 intstatus, ctl; |
| 2664 | |
| 2665 | /* MAC Interrupt Status register indicates what interrupts are pending. |
| 2666 | * It is automatically cleared once read. |
| 2667 | */ |
| 2668 | intstatus = macb_readl(lp, ISR); |
| 2669 | |
| 2670 | /* Receive complete */ |
| 2671 | if (intstatus & MACB_BIT(RCOMP)) |
| 2672 | at91ether_rx(dev); |
| 2673 | |
| 2674 | /* Transmit complete */ |
| 2675 | if (intstatus & MACB_BIT(TCOMP)) { |
| 2676 | /* The TCOM bit is set even if the transmission failed */ |
| 2677 | if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE))) |
| 2678 | lp->stats.tx_errors++; |
| 2679 | |
| 2680 | if (lp->skb) { |
| 2681 | dev_kfree_skb_irq(lp->skb); |
| 2682 | lp->skb = NULL; |
| 2683 | dma_unmap_single(NULL, lp->skb_physaddr, |
| 2684 | lp->skb_length, DMA_TO_DEVICE); |
| 2685 | lp->stats.tx_packets++; |
| 2686 | lp->stats.tx_bytes += lp->skb_length; |
| 2687 | } |
| 2688 | netif_wake_queue(dev); |
| 2689 | } |
| 2690 | |
| 2691 | /* Work-around for EMAC Errata section 41.3.1 */ |
| 2692 | if (intstatus & MACB_BIT(RXUBR)) { |
| 2693 | ctl = macb_readl(lp, NCR); |
| 2694 | macb_writel(lp, NCR, ctl & ~MACB_BIT(RE)); |
| 2695 | macb_writel(lp, NCR, ctl | MACB_BIT(RE)); |
| 2696 | } |
| 2697 | |
| 2698 | if (intstatus & MACB_BIT(ISR_ROVR)) |
| 2699 | netdev_err(dev, "ROVR error\n"); |
| 2700 | |
| 2701 | return IRQ_HANDLED; |
| 2702 | } |
| 2703 | |
| 2704 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 2705 | static void at91ether_poll_controller(struct net_device *dev) |
| 2706 | { |
| 2707 | unsigned long flags; |
| 2708 | |
| 2709 | local_irq_save(flags); |
| 2710 | at91ether_interrupt(dev->irq, dev); |
| 2711 | local_irq_restore(flags); |
| 2712 | } |
| 2713 | #endif |
| 2714 | |
| 2715 | static const struct net_device_ops at91ether_netdev_ops = { |
| 2716 | .ndo_open = at91ether_open, |
| 2717 | .ndo_stop = at91ether_close, |
| 2718 | .ndo_start_xmit = at91ether_start_xmit, |
| 2719 | .ndo_get_stats = macb_get_stats, |
| 2720 | .ndo_set_rx_mode = macb_set_rx_mode, |
| 2721 | .ndo_set_mac_address = eth_mac_addr, |
| 2722 | .ndo_do_ioctl = macb_ioctl, |
| 2723 | .ndo_validate_addr = eth_validate_addr, |
| 2724 | .ndo_change_mtu = eth_change_mtu, |
| 2725 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 2726 | .ndo_poll_controller = at91ether_poll_controller, |
| 2727 | #endif |
| 2728 | }; |
| 2729 | |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 2730 | static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk, |
| 2731 | struct clk **hclk, struct clk **tx_clk) |
| 2732 | { |
| 2733 | int err; |
| 2734 | |
| 2735 | *hclk = NULL; |
| 2736 | *tx_clk = NULL; |
| 2737 | |
| 2738 | *pclk = devm_clk_get(&pdev->dev, "ether_clk"); |
| 2739 | if (IS_ERR(*pclk)) |
| 2740 | return PTR_ERR(*pclk); |
| 2741 | |
| 2742 | err = clk_prepare_enable(*pclk); |
| 2743 | if (err) { |
| 2744 | dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err); |
| 2745 | return err; |
| 2746 | } |
| 2747 | |
| 2748 | return 0; |
| 2749 | } |
| 2750 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2751 | static int at91ether_init(struct platform_device *pdev) |
| 2752 | { |
| 2753 | struct net_device *dev = platform_get_drvdata(pdev); |
| 2754 | struct macb *bp = netdev_priv(dev); |
| 2755 | int err; |
| 2756 | u32 reg; |
| 2757 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2758 | dev->netdev_ops = &at91ether_netdev_ops; |
| 2759 | dev->ethtool_ops = &macb_ethtool_ops; |
| 2760 | |
| 2761 | err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt, |
| 2762 | 0, dev->name, dev); |
| 2763 | if (err) |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 2764 | return err; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2765 | |
| 2766 | macb_writel(bp, NCR, 0); |
| 2767 | |
| 2768 | reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG); |
| 2769 | if (bp->phy_interface == PHY_INTERFACE_MODE_RMII) |
| 2770 | reg |= MACB_BIT(RM9200_RMII); |
| 2771 | |
| 2772 | macb_writel(bp, NCFGR, reg); |
| 2773 | |
| 2774 | return 0; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2775 | } |
| 2776 | |
David S. Miller | 3cef5c5 | 2015-03-09 23:38:02 -0400 | [diff] [blame] | 2777 | static const struct macb_config at91sam9260_config = { |
Nicolas Ferre | 6bdaa5e | 2016-03-10 16:44:32 +0100 | [diff] [blame] | 2778 | .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII, |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 2779 | .clk_init = macb_clk_init, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2780 | .init = macb_init, |
| 2781 | }; |
| 2782 | |
David S. Miller | 3cef5c5 | 2015-03-09 23:38:02 -0400 | [diff] [blame] | 2783 | static const struct macb_config pc302gem_config = { |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2784 | .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE, |
| 2785 | .dma_burst_length = 16, |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 2786 | .clk_init = macb_clk_init, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2787 | .init = macb_init, |
| 2788 | }; |
| 2789 | |
Cyrille Pitchen | 5c8fe71 | 2015-06-18 16:27:23 +0200 | [diff] [blame] | 2790 | static const struct macb_config sama5d2_config = { |
Nicolas Ferre | 6bdaa5e | 2016-03-10 16:44:32 +0100 | [diff] [blame] | 2791 | .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII, |
Cyrille Pitchen | 5c8fe71 | 2015-06-18 16:27:23 +0200 | [diff] [blame] | 2792 | .dma_burst_length = 16, |
| 2793 | .clk_init = macb_clk_init, |
| 2794 | .init = macb_init, |
| 2795 | }; |
| 2796 | |
David S. Miller | 3cef5c5 | 2015-03-09 23:38:02 -0400 | [diff] [blame] | 2797 | static const struct macb_config sama5d3_config = { |
Nicolas Ferre | 6bdaa5e | 2016-03-10 16:44:32 +0100 | [diff] [blame] | 2798 | .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE |
| 2799 | | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2800 | .dma_burst_length = 16, |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 2801 | .clk_init = macb_clk_init, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2802 | .init = macb_init, |
| 2803 | }; |
| 2804 | |
David S. Miller | 3cef5c5 | 2015-03-09 23:38:02 -0400 | [diff] [blame] | 2805 | static const struct macb_config sama5d4_config = { |
Nicolas Ferre | 6bdaa5e | 2016-03-10 16:44:32 +0100 | [diff] [blame] | 2806 | .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2807 | .dma_burst_length = 4, |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 2808 | .clk_init = macb_clk_init, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2809 | .init = macb_init, |
| 2810 | }; |
| 2811 | |
David S. Miller | 3cef5c5 | 2015-03-09 23:38:02 -0400 | [diff] [blame] | 2812 | static const struct macb_config emac_config = { |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 2813 | .clk_init = at91ether_clk_init, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2814 | .init = at91ether_init, |
| 2815 | }; |
| 2816 | |
Neil Armstrong | e611b5b | 2016-01-05 14:39:17 +0100 | [diff] [blame] | 2817 | static const struct macb_config np4_config = { |
| 2818 | .caps = MACB_CAPS_USRIO_DISABLED, |
| 2819 | .clk_init = macb_clk_init, |
| 2820 | .init = macb_init, |
| 2821 | }; |
David S. Miller | 36583eb | 2015-05-23 01:22:35 -0400 | [diff] [blame] | 2822 | |
Harini Katakam | 7b61f9c | 2015-05-06 22:27:16 +0530 | [diff] [blame] | 2823 | static const struct macb_config zynqmp_config = { |
Punnaiah Choudary Kalluri | 7baaa90 | 2015-07-06 10:02:53 +0530 | [diff] [blame] | 2824 | .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO, |
Harini Katakam | 7b61f9c | 2015-05-06 22:27:16 +0530 | [diff] [blame] | 2825 | .dma_burst_length = 16, |
| 2826 | .clk_init = macb_clk_init, |
| 2827 | .init = macb_init, |
Harini Katakam | 98b5a0f4 | 2015-05-06 22:27:17 +0530 | [diff] [blame] | 2828 | .jumbo_max_len = 10240, |
Harini Katakam | 7b61f9c | 2015-05-06 22:27:16 +0530 | [diff] [blame] | 2829 | }; |
| 2830 | |
Nathan Sullivan | 222ca8e | 2015-05-22 09:22:10 -0500 | [diff] [blame] | 2831 | static const struct macb_config zynq_config = { |
Punnaiah Choudary Kalluri | 7baaa90 | 2015-07-06 10:02:53 +0530 | [diff] [blame] | 2832 | .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF, |
Nathan Sullivan | 222ca8e | 2015-05-22 09:22:10 -0500 | [diff] [blame] | 2833 | .dma_burst_length = 16, |
| 2834 | .clk_init = macb_clk_init, |
| 2835 | .init = macb_init, |
| 2836 | }; |
| 2837 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2838 | static const struct of_device_id macb_dt_ids[] = { |
| 2839 | { .compatible = "cdns,at32ap7000-macb" }, |
| 2840 | { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config }, |
| 2841 | { .compatible = "cdns,macb" }, |
Neil Armstrong | e611b5b | 2016-01-05 14:39:17 +0100 | [diff] [blame] | 2842 | { .compatible = "cdns,np4-macb", .data = &np4_config }, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2843 | { .compatible = "cdns,pc302-gem", .data = &pc302gem_config }, |
| 2844 | { .compatible = "cdns,gem", .data = &pc302gem_config }, |
Cyrille Pitchen | 5c8fe71 | 2015-06-18 16:27:23 +0200 | [diff] [blame] | 2845 | { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config }, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2846 | { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config }, |
| 2847 | { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config }, |
| 2848 | { .compatible = "cdns,at91rm9200-emac", .data = &emac_config }, |
| 2849 | { .compatible = "cdns,emac", .data = &emac_config }, |
Harini Katakam | 7b61f9c | 2015-05-06 22:27:16 +0530 | [diff] [blame] | 2850 | { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config}, |
Nathan Sullivan | 222ca8e | 2015-05-22 09:22:10 -0500 | [diff] [blame] | 2851 | { .compatible = "cdns,zynq-gem", .data = &zynq_config }, |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2852 | { /* sentinel */ } |
| 2853 | }; |
| 2854 | MODULE_DEVICE_TABLE(of, macb_dt_ids); |
| 2855 | #endif /* CONFIG_OF */ |
| 2856 | |
| 2857 | static int macb_probe(struct platform_device *pdev) |
| 2858 | { |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 2859 | int (*clk_init)(struct platform_device *, struct clk **, |
| 2860 | struct clk **, struct clk **) |
| 2861 | = macb_clk_init; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2862 | int (*init)(struct platform_device *) = macb_init; |
| 2863 | struct device_node *np = pdev->dev.of_node; |
Gregory CLEMENT | 270c499 | 2015-12-17 10:51:04 +0100 | [diff] [blame] | 2864 | struct device_node *phy_node; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2865 | const struct macb_config *macb_config = NULL; |
Sudip Mukherjee | 36df745 | 2016-01-25 11:43:09 +0530 | [diff] [blame] | 2866 | struct clk *pclk, *hclk = NULL, *tx_clk = NULL; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2867 | unsigned int queue_mask, num_queues; |
| 2868 | struct macb_platform_data *pdata; |
Andy Shevchenko | f2ce8a9 | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 2869 | bool native_io; |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2870 | struct phy_device *phydev; |
| 2871 | struct net_device *dev; |
| 2872 | struct resource *regs; |
| 2873 | void __iomem *mem; |
| 2874 | const char *mac; |
| 2875 | struct macb *bp; |
| 2876 | int err; |
| 2877 | |
Andy Shevchenko | f2ce8a9 | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 2878 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2879 | mem = devm_ioremap_resource(&pdev->dev, regs); |
| 2880 | if (IS_ERR(mem)) |
| 2881 | return PTR_ERR(mem); |
| 2882 | |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 2883 | if (np) { |
| 2884 | const struct of_device_id *match; |
| 2885 | |
| 2886 | match = of_match_node(macb_dt_ids, np); |
| 2887 | if (match && match->data) { |
| 2888 | macb_config = match->data; |
| 2889 | clk_init = macb_config->clk_init; |
| 2890 | init = macb_config->init; |
| 2891 | } |
| 2892 | } |
| 2893 | |
| 2894 | err = clk_init(pdev, &pclk, &hclk, &tx_clk); |
| 2895 | if (err) |
| 2896 | return err; |
| 2897 | |
Andy Shevchenko | f2ce8a9 | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 2898 | native_io = hw_is_native_io(mem); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2899 | |
Andy Shevchenko | f2ce8a9 | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 2900 | macb_probe_queues(mem, native_io, &queue_mask, &num_queues); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2901 | dev = alloc_etherdev_mq(sizeof(*bp), num_queues); |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 2902 | if (!dev) { |
| 2903 | err = -ENOMEM; |
| 2904 | goto err_disable_clocks; |
| 2905 | } |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2906 | |
| 2907 | dev->base_addr = regs->start; |
| 2908 | |
| 2909 | SET_NETDEV_DEV(dev, &pdev->dev); |
| 2910 | |
| 2911 | bp = netdev_priv(dev); |
| 2912 | bp->pdev = pdev; |
| 2913 | bp->dev = dev; |
| 2914 | bp->regs = mem; |
Andy Shevchenko | f2ce8a9 | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 2915 | bp->native_io = native_io; |
| 2916 | if (native_io) { |
David S. Miller | 7a6e070 | 2015-07-27 14:24:48 -0700 | [diff] [blame] | 2917 | bp->macb_reg_readl = hw_readl_native; |
| 2918 | bp->macb_reg_writel = hw_writel_native; |
Andy Shevchenko | f2ce8a9 | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 2919 | } else { |
David S. Miller | 7a6e070 | 2015-07-27 14:24:48 -0700 | [diff] [blame] | 2920 | bp->macb_reg_readl = hw_readl; |
| 2921 | bp->macb_reg_writel = hw_writel; |
Andy Shevchenko | f2ce8a9 | 2015-07-24 21:23:59 +0300 | [diff] [blame] | 2922 | } |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2923 | bp->num_queues = num_queues; |
Nicolas Ferre | bfa0914 | 2015-03-31 15:01:59 +0200 | [diff] [blame] | 2924 | bp->queue_mask = queue_mask; |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 2925 | if (macb_config) |
| 2926 | bp->dma_burst_length = macb_config->dma_burst_length; |
| 2927 | bp->pclk = pclk; |
| 2928 | bp->hclk = hclk; |
| 2929 | bp->tx_clk = tx_clk; |
Andy Shevchenko | f36dbe6 | 2015-07-24 21:24:00 +0300 | [diff] [blame] | 2930 | if (macb_config) |
Harini Katakam | 98b5a0f4 | 2015-05-06 22:27:17 +0530 | [diff] [blame] | 2931 | bp->jumbo_max_len = macb_config->jumbo_max_len; |
Harini Katakam | 98b5a0f4 | 2015-05-06 22:27:17 +0530 | [diff] [blame] | 2932 | |
Sergio Prado | 3e2a5e1 | 2016-02-09 12:07:16 -0200 | [diff] [blame] | 2933 | bp->wol = 0; |
Sergio Prado | 7c4a1d0 | 2016-02-16 21:10:45 -0200 | [diff] [blame] | 2934 | if (of_get_property(np, "magic-packet", NULL)) |
Sergio Prado | 3e2a5e1 | 2016-02-09 12:07:16 -0200 | [diff] [blame] | 2935 | bp->wol |= MACB_WOL_HAS_MAGIC_PACKET; |
| 2936 | device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET); |
| 2937 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2938 | spin_lock_init(&bp->lock); |
| 2939 | |
Nicolas Ferre | ad78347 | 2015-03-31 15:02:02 +0200 | [diff] [blame] | 2940 | /* setup capabilities */ |
Nicolas Ferre | f697050 | 2015-03-31 15:02:01 +0200 | [diff] [blame] | 2941 | macb_configure_caps(bp, macb_config); |
| 2942 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2943 | platform_set_drvdata(pdev, dev); |
| 2944 | |
| 2945 | dev->irq = platform_get_irq(pdev, 0); |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 2946 | if (dev->irq < 0) { |
| 2947 | err = dev->irq; |
| 2948 | goto err_disable_clocks; |
| 2949 | } |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2950 | |
| 2951 | mac = of_get_mac_address(np); |
Guenter Roeck | 5090704 | 2013-04-02 09:35:09 +0000 | [diff] [blame] | 2952 | if (mac) |
Moritz Fischer | eefb52d | 2016-03-29 19:11:14 -0700 | [diff] [blame] | 2953 | ether_addr_copy(bp->dev->dev_addr, mac); |
Guenter Roeck | 5090704 | 2013-04-02 09:35:09 +0000 | [diff] [blame] | 2954 | else |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 2955 | macb_get_hwaddr(bp); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 2956 | |
Gregory CLEMENT | 5833e05 | 2015-12-11 11:34:53 +0100 | [diff] [blame] | 2957 | /* Power up the PHY if there is a GPIO reset */ |
Gregory CLEMENT | 270c499 | 2015-12-17 10:51:04 +0100 | [diff] [blame] | 2958 | phy_node = of_get_next_available_child(np, NULL); |
| 2959 | if (phy_node) { |
| 2960 | int gpio = of_get_named_gpio(phy_node, "reset-gpios", 0); |
Moritz Fischer | 64ec42f | 2016-03-29 19:11:12 -0700 | [diff] [blame] | 2961 | |
Charles Keepax | 0e3e799 | 2016-03-28 13:47:42 +0100 | [diff] [blame] | 2962 | if (gpio_is_valid(gpio)) { |
Gregory CLEMENT | 270c499 | 2015-12-17 10:51:04 +0100 | [diff] [blame] | 2963 | bp->reset_gpio = gpio_to_desc(gpio); |
Charles Keepax | 0e3e799 | 2016-03-28 13:47:42 +0100 | [diff] [blame] | 2964 | gpiod_direction_output(bp->reset_gpio, 1); |
| 2965 | } |
Gregory CLEMENT | 270c499 | 2015-12-17 10:51:04 +0100 | [diff] [blame] | 2966 | } |
| 2967 | of_node_put(phy_node); |
Gregory CLEMENT | 5833e05 | 2015-12-11 11:34:53 +0100 | [diff] [blame] | 2968 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2969 | err = of_get_phy_mode(np); |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 2970 | if (err < 0) { |
Jingoo Han | c607a0d | 2013-08-30 14:12:21 +0900 | [diff] [blame] | 2971 | pdata = dev_get_platdata(&pdev->dev); |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 2972 | if (pdata && pdata->is_rmii) |
| 2973 | bp->phy_interface = PHY_INTERFACE_MODE_RMII; |
| 2974 | else |
| 2975 | bp->phy_interface = PHY_INTERFACE_MODE_MII; |
| 2976 | } else { |
| 2977 | bp->phy_interface = err; |
| 2978 | } |
| 2979 | |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 2980 | /* IP specific init */ |
| 2981 | err = init(pdev); |
| 2982 | if (err) |
| 2983 | goto err_out_free_netdev; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2984 | |
Florian Fainelli | cf66966 | 2016-05-02 18:38:45 -0700 | [diff] [blame] | 2985 | err = macb_mii_init(bp); |
| 2986 | if (err) |
| 2987 | goto err_out_free_netdev; |
| 2988 | |
Philippe Reynes | 0a91281 | 2016-06-22 00:32:35 +0200 | [diff] [blame] | 2989 | phydev = dev->phydev; |
Florian Fainelli | cf66966 | 2016-05-02 18:38:45 -0700 | [diff] [blame] | 2990 | |
| 2991 | netif_carrier_off(dev); |
| 2992 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2993 | err = register_netdev(dev); |
| 2994 | if (err) { |
| 2995 | dev_err(&pdev->dev, "Cannot register net device, aborting.\n"); |
Florian Fainelli | cf66966 | 2016-05-02 18:38:45 -0700 | [diff] [blame] | 2996 | goto err_out_unregister_mdio; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 2997 | } |
| 2998 | |
Florian Fainelli | cf66966 | 2016-05-02 18:38:45 -0700 | [diff] [blame] | 2999 | phy_attached_info(phydev); |
Nicolas Ferre | 03fc472 | 2012-07-03 23:14:13 +0000 | [diff] [blame] | 3000 | |
Bo Shen | 5879823 | 2014-09-13 01:57:49 +0200 | [diff] [blame] | 3001 | netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n", |
| 3002 | macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID), |
| 3003 | dev->base_addr, dev->irq, dev->dev_addr); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3004 | |
| 3005 | return 0; |
| 3006 | |
Florian Fainelli | cf66966 | 2016-05-02 18:38:45 -0700 | [diff] [blame] | 3007 | err_out_unregister_mdio: |
Philippe Reynes | 0a91281 | 2016-06-22 00:32:35 +0200 | [diff] [blame] | 3008 | phy_disconnect(dev->phydev); |
Florian Fainelli | cf66966 | 2016-05-02 18:38:45 -0700 | [diff] [blame] | 3009 | mdiobus_unregister(bp->mii_bus); |
| 3010 | mdiobus_free(bp->mii_bus); |
| 3011 | |
| 3012 | /* Shutdown the PHY if there is a GPIO reset */ |
| 3013 | if (bp->reset_gpio) |
| 3014 | gpiod_set_value(bp->reset_gpio, 0); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 3015 | |
Cyrille Pitchen | cf250de | 2014-12-15 15:13:32 +0100 | [diff] [blame] | 3016 | err_out_free_netdev: |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3017 | free_netdev(dev); |
Cyrille Pitchen | 421d9df | 2015-03-07 07:23:32 +0100 | [diff] [blame] | 3018 | |
Nicolas Ferre | c69618b | 2015-03-31 15:02:03 +0200 | [diff] [blame] | 3019 | err_disable_clocks: |
| 3020 | clk_disable_unprepare(tx_clk); |
| 3021 | clk_disable_unprepare(hclk); |
| 3022 | clk_disable_unprepare(pclk); |
| 3023 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3024 | return err; |
| 3025 | } |
| 3026 | |
Nicolae Rosia | 9e86d766 | 2015-01-22 17:31:05 +0000 | [diff] [blame] | 3027 | static int macb_remove(struct platform_device *pdev) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3028 | { |
| 3029 | struct net_device *dev; |
| 3030 | struct macb *bp; |
| 3031 | |
| 3032 | dev = platform_get_drvdata(pdev); |
| 3033 | |
| 3034 | if (dev) { |
| 3035 | bp = netdev_priv(dev); |
Philippe Reynes | 0a91281 | 2016-06-22 00:32:35 +0200 | [diff] [blame] | 3036 | if (dev->phydev) |
| 3037 | phy_disconnect(dev->phydev); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 3038 | mdiobus_unregister(bp->mii_bus); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 3039 | mdiobus_free(bp->mii_bus); |
Gregory CLEMENT | 5833e05 | 2015-12-11 11:34:53 +0100 | [diff] [blame] | 3040 | |
| 3041 | /* Shutdown the PHY if there is a GPIO reset */ |
Charles Keepax | 0e3e799 | 2016-03-28 13:47:42 +0100 | [diff] [blame] | 3042 | if (bp->reset_gpio) |
| 3043 | gpiod_set_value(bp->reset_gpio, 0); |
Gregory CLEMENT | 5833e05 | 2015-12-11 11:34:53 +0100 | [diff] [blame] | 3044 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3045 | unregister_netdev(dev); |
Cyrille Pitchen | 93b31f4 | 2015-03-07 07:23:31 +0100 | [diff] [blame] | 3046 | clk_disable_unprepare(bp->tx_clk); |
Steffen Trumtrar | ace5801 | 2013-03-27 23:07:07 +0000 | [diff] [blame] | 3047 | clk_disable_unprepare(bp->hclk); |
Steffen Trumtrar | ace5801 | 2013-03-27 23:07:07 +0000 | [diff] [blame] | 3048 | clk_disable_unprepare(bp->pclk); |
Cyrille Pitchen | e965be7 | 2014-12-15 15:13:31 +0100 | [diff] [blame] | 3049 | free_netdev(dev); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3050 | } |
| 3051 | |
| 3052 | return 0; |
| 3053 | } |
| 3054 | |
Michal Simek | d23823d | 2015-01-23 09:36:03 +0100 | [diff] [blame] | 3055 | static int __maybe_unused macb_suspend(struct device *dev) |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 3056 | { |
Soren Brinkmann | 0dfc3e1 | 2013-12-10 16:07:19 -0800 | [diff] [blame] | 3057 | struct platform_device *pdev = to_platform_device(dev); |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 3058 | struct net_device *netdev = platform_get_drvdata(pdev); |
| 3059 | struct macb *bp = netdev_priv(netdev); |
| 3060 | |
Nicolas Ferre | 03fc472 | 2012-07-03 23:14:13 +0000 | [diff] [blame] | 3061 | netif_carrier_off(netdev); |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 3062 | netif_device_detach(netdev); |
| 3063 | |
Sergio Prado | 3e2a5e1 | 2016-02-09 12:07:16 -0200 | [diff] [blame] | 3064 | if (bp->wol & MACB_WOL_ENABLED) { |
| 3065 | macb_writel(bp, IER, MACB_BIT(WOL)); |
| 3066 | macb_writel(bp, WOL, MACB_BIT(MAG)); |
| 3067 | enable_irq_wake(bp->queues[0].irq); |
| 3068 | } else { |
| 3069 | clk_disable_unprepare(bp->tx_clk); |
| 3070 | clk_disable_unprepare(bp->hclk); |
| 3071 | clk_disable_unprepare(bp->pclk); |
| 3072 | } |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 3073 | |
| 3074 | return 0; |
| 3075 | } |
| 3076 | |
Michal Simek | d23823d | 2015-01-23 09:36:03 +0100 | [diff] [blame] | 3077 | static int __maybe_unused macb_resume(struct device *dev) |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 3078 | { |
Soren Brinkmann | 0dfc3e1 | 2013-12-10 16:07:19 -0800 | [diff] [blame] | 3079 | struct platform_device *pdev = to_platform_device(dev); |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 3080 | struct net_device *netdev = platform_get_drvdata(pdev); |
| 3081 | struct macb *bp = netdev_priv(netdev); |
| 3082 | |
Sergio Prado | 3e2a5e1 | 2016-02-09 12:07:16 -0200 | [diff] [blame] | 3083 | if (bp->wol & MACB_WOL_ENABLED) { |
| 3084 | macb_writel(bp, IDR, MACB_BIT(WOL)); |
| 3085 | macb_writel(bp, WOL, 0); |
| 3086 | disable_irq_wake(bp->queues[0].irq); |
| 3087 | } else { |
| 3088 | clk_prepare_enable(bp->pclk); |
| 3089 | clk_prepare_enable(bp->hclk); |
| 3090 | clk_prepare_enable(bp->tx_clk); |
| 3091 | } |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 3092 | |
| 3093 | netif_device_attach(netdev); |
| 3094 | |
| 3095 | return 0; |
| 3096 | } |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 3097 | |
Soren Brinkmann | 0dfc3e1 | 2013-12-10 16:07:19 -0800 | [diff] [blame] | 3098 | static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume); |
| 3099 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3100 | static struct platform_driver macb_driver = { |
Nicolae Rosia | 9e86d766 | 2015-01-22 17:31:05 +0000 | [diff] [blame] | 3101 | .probe = macb_probe, |
| 3102 | .remove = macb_remove, |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3103 | .driver = { |
| 3104 | .name = "macb", |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 3105 | .of_match_table = of_match_ptr(macb_dt_ids), |
Soren Brinkmann | 0dfc3e1 | 2013-12-10 16:07:19 -0800 | [diff] [blame] | 3106 | .pm = &macb_pm_ops, |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3107 | }, |
| 3108 | }; |
| 3109 | |
Nicolae Rosia | 9e86d766 | 2015-01-22 17:31:05 +0000 | [diff] [blame] | 3110 | module_platform_driver(macb_driver); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 3111 | |
| 3112 | MODULE_LICENSE("GPL"); |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 3113 | MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver"); |
Jean Delvare | e05503e | 2011-05-18 16:49:24 +0200 | [diff] [blame] | 3114 | MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); |
Kay Sievers | 72abb46 | 2008-04-18 13:50:44 -0700 | [diff] [blame] | 3115 | MODULE_ALIAS("platform:macb"); |