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