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