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