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