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