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