blob: 6974933c67bb96d4e7e5675d861ed593e1b5cd31 [file] [log] [blame]
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001/*
Jamie Ilesf75ba502011-11-08 10:12:32 +00002 * Cadence MACB/GEM Ethernet Controller driver
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003 *
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 Ilesc220f8c2011-03-08 20:27:08 +000011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010012#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 Ferre909a8582012-11-19 06:00:21 +000017#include <linux/circ_buf.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010018#include <linux/slab.h>
19#include <linux/init.h>
Soren Brinkmann60fe7162013-12-10 16:07:21 -080020#include <linux/io.h>
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +000021#include <linux/gpio.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010022#include <linux/gpio/consumer.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000023#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010024#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010026#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000027#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010028#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020029#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080030#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010031#include <linux/of_device.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010032#include <linux/of_gpio.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020033#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010034#include <linux/of_net.h>
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000035#include <linux/ip.h>
36#include <linux/udp.h>
37#include <linux/tcp.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010038#include "macb.h"
39
Nicolas Ferre1b447912013-06-04 21:57:11 +000040#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000041#define RX_BUFFER_MULTIPLE 64 /* bytes */
Zach Brown8441bb32016-10-19 09:56:58 -050042
Zach Brownb410d132016-10-19 09:56:57 -050043#define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050044#define MIN_RX_RING_SIZE 64
45#define MAX_RX_RING_SIZE 8192
Rafal Ozieblodc97a892017-01-27 15:08:20 +000046#define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050047 * (bp)->rx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010048
Zach Brownb410d132016-10-19 09:56:57 -050049#define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050050#define MIN_TX_RING_SIZE 64
51#define MAX_TX_RING_SIZE 4096
Rafal Ozieblodc97a892017-01-27 15:08:20 +000052#define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050053 * (bp)->tx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010054
Nicolas Ferre909a8582012-11-19 06:00:21 +000055/* level of occupied TX descriptors under which we wake up TX process */
Zach Brownb410d132016-10-19 09:56:57 -050056#define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010057
58#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
59 | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000060#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 Ozieblo1629dd42016-11-16 10:02:34 +000065/* 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 Pitchena4c35ed32014-07-24 13:50:59 +020069
Jarod Wilson44770e12016-10-17 15:54:17 -040070#define GEM_MTU_MIN_SIZE ETH_MIN_MTU
David S. Millerf9c45ae2017-07-03 06:31:05 -070071#define MACB_NETIF_LSO NETIF_F_TSO
Harini Katakama5898ea2015-05-06 22:27:18 +053072
Sergio Prado3e2a5e12016-02-09 12:07:16 -020073#define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
74#define MACB_WOL_ENABLED (0x1 << 1)
75
Moritz Fischer64ec42f2016-03-29 19:11:12 -070076/* Graceful stop timeouts in us. We should allow up to
Nicolas Ferree86cd532012-10-31 06:04:57 +000077 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
78 */
79#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010080
Rafal Ozieblodc97a892017-01-27 15:08:20 +000081/* DMA buffer descriptor might be different size
Rafal Ozieblo7b429612017-06-29 07:12:51 +010082 * depends on hardware configuration:
83 *
84 * 1. dma address width 32 bits:
85 * word 1: 32 bit address of Data Buffer
86 * word 2: control
87 *
88 * 2. dma address width 64 bits:
89 * word 1: 32 bit address of Data Buffer
90 * word 2: control
91 * word 3: upper 32 bit address of Data Buffer
92 * word 4: unused
93 *
94 * 3. dma address width 32 bits with hardware timestamping:
95 * word 1: 32 bit address of Data Buffer
96 * word 2: control
97 * word 3: timestamp word 1
98 * word 4: timestamp word 2
99 *
100 * 4. dma address width 64 bits with hardware timestamping:
101 * word 1: 32 bit address of Data Buffer
102 * word 2: control
103 * word 3: upper 32 bit address of Data Buffer
104 * word 4: unused
105 * word 5: timestamp word 1
106 * word 6: timestamp word 2
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000107 */
108static unsigned int macb_dma_desc_get_size(struct macb *bp)
109{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100110#ifdef MACB_EXT_DESC
111 unsigned int desc_size;
112
113 switch (bp->hw_dma_cap) {
114 case HW_DMA_CAP_64B:
115 desc_size = sizeof(struct macb_dma_desc)
116 + sizeof(struct macb_dma_desc_64);
117 break;
118 case HW_DMA_CAP_PTP:
119 desc_size = sizeof(struct macb_dma_desc)
120 + sizeof(struct macb_dma_desc_ptp);
121 break;
122 case HW_DMA_CAP_64B_PTP:
123 desc_size = sizeof(struct macb_dma_desc)
124 + sizeof(struct macb_dma_desc_64)
125 + sizeof(struct macb_dma_desc_ptp);
126 break;
127 default:
128 desc_size = sizeof(struct macb_dma_desc);
129 }
130 return desc_size;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000131#endif
132 return sizeof(struct macb_dma_desc);
133}
134
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100135static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000136{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100137#ifdef MACB_EXT_DESC
138 switch (bp->hw_dma_cap) {
139 case HW_DMA_CAP_64B:
140 case HW_DMA_CAP_PTP:
141 desc_idx <<= 1;
142 break;
143 case HW_DMA_CAP_64B_PTP:
144 desc_idx *= 3;
145 break;
146 default:
147 break;
148 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000149#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100150 return desc_idx;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000151}
152
153#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
154static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
155{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100156 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
157 return (struct macb_dma_desc_64 *)((void *)desc + sizeof(struct macb_dma_desc));
158 return NULL;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000159}
160#endif
161
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000162/* Ring buffer accessors */
Zach Brownb410d132016-10-19 09:56:57 -0500163static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000164{
Zach Brownb410d132016-10-19 09:56:57 -0500165 return index & (bp->tx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000166}
167
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100168static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
169 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000170{
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000171 index = macb_tx_ring_wrap(queue->bp, index);
172 index = macb_adj_dma_desc_idx(queue->bp, index);
173 return &queue->tx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000174}
175
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100176static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
177 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000178{
Zach Brownb410d132016-10-19 09:56:57 -0500179 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000180}
181
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100182static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000183{
184 dma_addr_t offset;
185
Zach Brownb410d132016-10-19 09:56:57 -0500186 offset = macb_tx_ring_wrap(queue->bp, index) *
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000187 macb_dma_desc_get_size(queue->bp);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000188
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100189 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000190}
191
Zach Brownb410d132016-10-19 09:56:57 -0500192static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000193{
Zach Brownb410d132016-10-19 09:56:57 -0500194 return index & (bp->rx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000195}
196
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000197static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000198{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000199 index = macb_rx_ring_wrap(queue->bp, index);
200 index = macb_adj_dma_desc_idx(queue->bp, index);
201 return &queue->rx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000202}
203
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000204static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000205{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000206 return queue->rx_buffers + queue->bp->rx_buffer_size *
207 macb_rx_ring_wrap(queue->bp, index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000208}
209
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300210/* I/O accessors */
211static u32 hw_readl_native(struct macb *bp, int offset)
212{
213 return __raw_readl(bp->regs + offset);
214}
215
216static void hw_writel_native(struct macb *bp, int offset, u32 value)
217{
218 __raw_writel(value, bp->regs + offset);
219}
220
221static u32 hw_readl(struct macb *bp, int offset)
222{
223 return readl_relaxed(bp->regs + offset);
224}
225
226static void hw_writel(struct macb *bp, int offset, u32 value)
227{
228 writel_relaxed(value, bp->regs + offset);
229}
230
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700231/* Find the CPU endianness by using the loopback bit of NCR register. When the
Moritz Fischer88023be2016-03-29 19:11:15 -0700232 * CPU is in big endian we need to program swapped mode for management
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300233 * descriptor access.
234 */
235static bool hw_is_native_io(void __iomem *addr)
236{
237 u32 value = MACB_BIT(LLB);
238
239 __raw_writel(value, addr + MACB_NCR);
240 value = __raw_readl(addr + MACB_NCR);
241
242 /* Write 0 back to disable everything */
243 __raw_writel(0, addr + MACB_NCR);
244
245 return value == MACB_BIT(LLB);
246}
247
248static bool hw_is_gem(void __iomem *addr, bool native_io)
249{
250 u32 id;
251
252 if (native_io)
253 id = __raw_readl(addr + MACB_MID);
254 else
255 id = readl_relaxed(addr + MACB_MID);
256
257 return MACB_BFEXT(IDNUM, id) >= 0x2;
258}
259
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100260static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100261{
262 u32 bottom;
263 u16 top;
264
265 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000266 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100267 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000268 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000269
270 /* Clear unused address register sets */
271 macb_or_gem_writel(bp, SA2B, 0);
272 macb_or_gem_writel(bp, SA2T, 0);
273 macb_or_gem_writel(bp, SA3B, 0);
274 macb_or_gem_writel(bp, SA3T, 0);
275 macb_or_gem_writel(bp, SA4B, 0);
276 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100277}
278
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100279static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100280{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000281 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100282 u32 bottom;
283 u16 top;
284 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000285 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100286
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900287 pdata = dev_get_platdata(&bp->pdev->dev);
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000288
Moritz Fischeraa50b552016-03-29 19:11:13 -0700289 /* Check all 4 address register for valid address */
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000290 for (i = 0; i < 4; i++) {
291 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
292 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100293
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000294 if (pdata && pdata->rev_eth_addr) {
295 addr[5] = bottom & 0xff;
296 addr[4] = (bottom >> 8) & 0xff;
297 addr[3] = (bottom >> 16) & 0xff;
298 addr[2] = (bottom >> 24) & 0xff;
299 addr[1] = top & 0xff;
300 addr[0] = (top & 0xff00) >> 8;
301 } else {
302 addr[0] = bottom & 0xff;
303 addr[1] = (bottom >> 8) & 0xff;
304 addr[2] = (bottom >> 16) & 0xff;
305 addr[3] = (bottom >> 24) & 0xff;
306 addr[4] = top & 0xff;
307 addr[5] = (top >> 8) & 0xff;
308 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100309
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000310 if (is_valid_ether_addr(addr)) {
311 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
312 return;
313 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700314 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000315
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300316 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000317 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100318}
319
frederic RODO6c36a702007-07-12 19:07:24 +0200320static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100321{
frederic RODO6c36a702007-07-12 19:07:24 +0200322 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100323 int value;
324
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100325 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
326 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200327 | MACB_BF(PHYA, mii_id)
328 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100329 | MACB_BF(CODE, MACB_MAN_CODE)));
330
frederic RODO6c36a702007-07-12 19:07:24 +0200331 /* wait for end of transfer */
332 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
333 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100334
335 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100336
337 return value;
338}
339
frederic RODO6c36a702007-07-12 19:07:24 +0200340static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
341 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100342{
frederic RODO6c36a702007-07-12 19:07:24 +0200343 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100344
345 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
346 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200347 | MACB_BF(PHYA, mii_id)
348 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100349 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200350 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100351
frederic RODO6c36a702007-07-12 19:07:24 +0200352 /* wait for end of transfer */
353 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
354 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100355
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100356 return 0;
357}
358
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800359/**
360 * macb_set_tx_clk() - Set a clock to a new frequency
361 * @clk Pointer to the clock to change
362 * @rate New frequency in Hz
363 * @dev Pointer to the struct net_device
364 */
365static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
366{
367 long ferr, rate, rate_rounded;
368
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100369 if (!clk)
370 return;
371
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800372 switch (speed) {
373 case SPEED_10:
374 rate = 2500000;
375 break;
376 case SPEED_100:
377 rate = 25000000;
378 break;
379 case SPEED_1000:
380 rate = 125000000;
381 break;
382 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800383 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800384 }
385
386 rate_rounded = clk_round_rate(clk, rate);
387 if (rate_rounded < 0)
388 return;
389
390 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
391 * is not satisfied.
392 */
393 ferr = abs(rate_rounded - rate);
394 ferr = DIV_ROUND_UP(ferr, rate / 100000);
395 if (ferr > 5)
396 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700397 rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800398
399 if (clk_set_rate(clk, rate_rounded))
400 netdev_err(dev, "adjusting tx_clk failed.\n");
401}
402
frederic RODO6c36a702007-07-12 19:07:24 +0200403static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100404{
frederic RODO6c36a702007-07-12 19:07:24 +0200405 struct macb *bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +0200406 struct phy_device *phydev = dev->phydev;
frederic RODO6c36a702007-07-12 19:07:24 +0200407 unsigned long flags;
frederic RODO6c36a702007-07-12 19:07:24 +0200408 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100409
frederic RODO6c36a702007-07-12 19:07:24 +0200410 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100411
frederic RODO6c36a702007-07-12 19:07:24 +0200412 if (phydev->link) {
413 if ((bp->speed != phydev->speed) ||
414 (bp->duplex != phydev->duplex)) {
415 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100416
frederic RODO6c36a702007-07-12 19:07:24 +0200417 reg = macb_readl(bp, NCFGR);
418 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000419 if (macb_is_gem(bp))
420 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200421
422 if (phydev->duplex)
423 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900424 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200425 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200426 if (phydev->speed == SPEED_1000 &&
427 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000428 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200429
Patrice Vilchez140b7552012-10-31 06:04:50 +0000430 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200431
432 bp->speed = phydev->speed;
433 bp->duplex = phydev->duplex;
434 status_change = 1;
435 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100436 }
437
frederic RODO6c36a702007-07-12 19:07:24 +0200438 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700439 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200440 bp->speed = 0;
441 bp->duplex = -1;
442 }
443 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100444
frederic RODO6c36a702007-07-12 19:07:24 +0200445 status_change = 1;
446 }
447
448 spin_unlock_irqrestore(&bp->lock, flags);
449
450 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000451 if (phydev->link) {
Jaeden Amero2c29b232015-03-12 18:07:54 -0500452 /* Update the TX clock rate if and only if the link is
453 * up and there has been a link change.
454 */
455 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
456
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000457 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000458 netdev_info(dev, "link up (%d/%s)\n",
459 phydev->speed,
460 phydev->duplex == DUPLEX_FULL ?
461 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000462 } else {
463 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000464 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000465 }
frederic RODO6c36a702007-07-12 19:07:24 +0200466 }
467}
468
469/* based on au1000_eth. c*/
470static int macb_mii_probe(struct net_device *dev)
471{
472 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000473 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000474 struct phy_device *phydev;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000475 int phy_irq;
Jiri Pirko7455a762010-02-08 05:12:08 +0000476 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200477
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200478 if (bp->phy_node) {
479 phydev = of_phy_connect(dev, bp->phy_node,
480 &macb_handle_link_change, 0,
481 bp->phy_interface);
482 if (!phydev)
483 return -ENODEV;
484 } else {
485 phydev = phy_find_first(bp->mii_bus);
486 if (!phydev) {
487 netdev_err(dev, "no PHY found\n");
488 return -ENXIO;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000489 }
frederic RODO6c36a702007-07-12 19:07:24 +0200490
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200491 pdata = dev_get_platdata(&bp->pdev->dev);
492 if (pdata) {
493 if (gpio_is_valid(pdata->phy_irq_pin)) {
494 ret = devm_gpio_request(&bp->pdev->dev,
495 pdata->phy_irq_pin, "phy int");
496 if (!ret) {
497 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
498 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
499 }
500 } else {
501 phydev->irq = PHY_POLL;
502 }
503 }
504
505 /* attach the mac to the phy */
506 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
507 bp->phy_interface);
508 if (ret) {
509 netdev_err(dev, "Could not attach to PHY\n");
510 return ret;
511 }
frederic RODO6c36a702007-07-12 19:07:24 +0200512 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100513
frederic RODO6c36a702007-07-12 19:07:24 +0200514 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200515 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000516 phydev->supported &= PHY_GBIT_FEATURES;
517 else
518 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100519
Nathan Sullivan222ca8e2015-05-22 09:22:10 -0500520 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
521 phydev->supported &= ~SUPPORTED_1000baseT_Half;
522
frederic RODO6c36a702007-07-12 19:07:24 +0200523 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100524
frederic RODO6c36a702007-07-12 19:07:24 +0200525 bp->link = 0;
526 bp->speed = 0;
527 bp->duplex = -1;
frederic RODO6c36a702007-07-12 19:07:24 +0200528
529 return 0;
530}
531
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100532static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200533{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000534 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200535 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200536 int err = -ENXIO, i;
537
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200538 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200539 macb_writel(bp, NCR, MACB_BIT(MPE));
540
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700541 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700542 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200543 err = -ENOMEM;
544 goto err_out;
545 }
546
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700547 bp->mii_bus->name = "MACB_mii_bus";
548 bp->mii_bus->read = &macb_mdio_read;
549 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000550 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700551 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700552 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700553 bp->mii_bus->parent = &bp->pdev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900554 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700555
Jamie Iles91523942011-02-28 04:05:25 +0000556 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200557
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200558 np = bp->pdev->dev.of_node;
559 if (np) {
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200560 if (of_phy_is_fixed_link(np)) {
561 if (of_phy_register_fixed_link(np) < 0) {
562 dev_err(&bp->pdev->dev,
563 "broken fixed-link specification\n");
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200564 goto err_out_unregister_bus;
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200565 }
566 bp->phy_node = of_node_get(np);
567
568 err = mdiobus_register(bp->mii_bus);
569 } else {
570 /* try dt phy registration */
571 err = of_mdiobus_register(bp->mii_bus, np);
572
573 /* fallback to standard phy registration if no phy were
574 * found during dt phy registration
575 */
576 if (!err && !phy_find_first(bp->mii_bus)) {
577 for (i = 0; i < PHY_MAX_ADDR; i++) {
578 struct phy_device *phydev;
579
580 phydev = mdiobus_scan(bp->mii_bus, i);
581 if (IS_ERR(phydev) &&
582 PTR_ERR(phydev) != -ENODEV) {
583 err = PTR_ERR(phydev);
584 break;
585 }
586 }
587
588 if (err)
589 goto err_out_unregister_bus;
590 }
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200591 }
592 } else {
Bartosz Folta83a77e92016-12-14 06:39:15 +0000593 for (i = 0; i < PHY_MAX_ADDR; i++)
594 bp->mii_bus->irq[i] = PHY_POLL;
595
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200596 if (pdata)
597 bp->mii_bus->phy_mask = pdata->phy_mask;
598
599 err = mdiobus_register(bp->mii_bus);
600 }
601
602 if (err)
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100603 goto err_out_free_mdiobus;
frederic RODO6c36a702007-07-12 19:07:24 +0200604
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200605 err = macb_mii_probe(bp->dev);
606 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200607 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200608
609 return 0;
610
611err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700612 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700613err_out_free_mdiobus:
Michael Grzeschik66ee6a02017-11-08 09:56:35 +0100614 of_node_put(bp->phy_node);
Michael Grzeschik9ce98142017-11-08 09:56:34 +0100615 if (np && of_phy_is_fixed_link(np))
616 of_phy_deregister_fixed_link(np);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700617 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200618err_out:
619 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100620}
621
622static void macb_update_stats(struct macb *bp)
623{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000624 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
625 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300626 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100627
628 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
629
Moritz Fischer96ec6312016-03-29 19:11:11 -0700630 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700631 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100632}
633
Nicolas Ferree86cd532012-10-31 06:04:57 +0000634static int macb_halt_tx(struct macb *bp)
635{
636 unsigned long halt_time, timeout;
637 u32 status;
638
639 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
640
641 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
642 do {
643 halt_time = jiffies;
644 status = macb_readl(bp, TSR);
645 if (!(status & MACB_BIT(TGO)))
646 return 0;
647
648 usleep_range(10, 250);
649 } while (time_before(halt_time, timeout));
650
651 return -ETIMEDOUT;
652}
653
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200654static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
655{
656 if (tx_skb->mapping) {
657 if (tx_skb->mapped_as_page)
658 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
659 tx_skb->size, DMA_TO_DEVICE);
660 else
661 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
662 tx_skb->size, DMA_TO_DEVICE);
663 tx_skb->mapping = 0;
664 }
665
666 if (tx_skb->skb) {
667 dev_kfree_skb_any(tx_skb->skb);
668 tx_skb->skb = NULL;
669 }
670}
671
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000672static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
Harini Katakamfff80192016-08-09 13:15:53 +0530673{
Harini Katakamfff80192016-08-09 13:15:53 +0530674#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000675 struct macb_dma_desc_64 *desc_64;
676
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100677 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000678 desc_64 = macb_64b_desc(bp, desc);
679 desc_64->addrh = upper_32_bits(addr);
680 }
Harini Katakamfff80192016-08-09 13:15:53 +0530681#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000682 desc->addr = lower_32_bits(addr);
683}
684
685static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
686{
687 dma_addr_t addr = 0;
688#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
689 struct macb_dma_desc_64 *desc_64;
690
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100691 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000692 desc_64 = macb_64b_desc(bp, desc);
693 addr = ((u64)(desc_64->addrh) << 32);
694 }
695#endif
696 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
697 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +0530698}
699
Nicolas Ferree86cd532012-10-31 06:04:57 +0000700static void macb_tx_error_task(struct work_struct *work)
701{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100702 struct macb_queue *queue = container_of(work, struct macb_queue,
703 tx_error_task);
704 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000705 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100706 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000707 struct sk_buff *skb;
708 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100709 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000710
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100711 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
712 (unsigned int)(queue - bp->queues),
713 queue->tx_tail, queue->tx_head);
714
715 /* Prevent the queue IRQ handlers from running: each of them may call
716 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
717 * As explained below, we have to halt the transmission before updating
718 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
719 * network engine about the macb/gem being halted.
720 */
721 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000722
723 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100724 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000725
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700726 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000727 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100728 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000729 */
730 if (macb_halt_tx(bp))
731 /* Just complain for now, reinitializing TX path can be good */
732 netdev_err(bp->dev, "BUG: halt tx timed out\n");
733
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700734 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000735 * Free transmit buffers in upper layer.
736 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100737 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
738 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000739
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100740 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000741 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100742 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000743 skb = tx_skb->skb;
744
745 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200746 /* skb is set for the last buffer of the frame */
747 while (!skb) {
748 macb_tx_unmap(bp, tx_skb);
749 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100750 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200751 skb = tx_skb->skb;
752 }
753
754 /* ctrl still refers to the first buffer descriptor
755 * since it's the only one written back by the hardware
756 */
757 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
758 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500759 macb_tx_ring_wrap(bp, tail),
760 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200761 bp->dev->stats.tx_packets++;
762 bp->dev->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200763 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000764 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700765 /* "Buffers exhausted mid-frame" errors may only happen
766 * if the driver is buggy, so complain loudly about
767 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000768 */
769 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
770 netdev_err(bp->dev,
771 "BUG: TX buffers exhausted mid-frame\n");
772
773 desc->ctrl = ctrl | MACB_BIT(TX_USED);
774 }
775
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200776 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000777 }
778
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100779 /* Set end of TX queue */
780 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000781 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100782 desc->ctrl = MACB_BIT(TX_USED);
783
Nicolas Ferree86cd532012-10-31 06:04:57 +0000784 /* Make descriptor updates visible to hardware */
785 wmb();
786
787 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000788 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530789#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100790 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000791 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530792#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000793 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100794 queue->tx_head = 0;
795 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000796
797 /* Housework before enabling TX IRQ */
798 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100799 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
800
801 /* Now we are ready to start transmission again */
802 netif_tx_start_all_queues(bp->dev);
803 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
804
805 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000806}
807
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100808static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100809{
810 unsigned int tail;
811 unsigned int head;
812 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100813 struct macb *bp = queue->bp;
814 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100815
816 status = macb_readl(bp, TSR);
817 macb_writel(bp, TSR, status);
818
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000819 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100820 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000821
Nicolas Ferree86cd532012-10-31 06:04:57 +0000822 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700823 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100824
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100825 head = queue->tx_head;
826 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000827 struct macb_tx_skb *tx_skb;
828 struct sk_buff *skb;
829 struct macb_dma_desc *desc;
830 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100831
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100832 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100833
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000834 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100835 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000836
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000837 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100838
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200839 /* TX_USED bit is only set by hardware on the very first buffer
840 * descriptor of the transmitted frame.
841 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000842 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100843 break;
844
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200845 /* Process all buffers of the current transmitted frame */
846 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100847 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200848 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000849
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200850 /* First, update TX stats if needed */
851 if (skb) {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +0100852 if (gem_ptp_do_txstamp(queue, skb, desc) == 0) {
853 /* skb now belongs to timestamp buffer
854 * and will be removed later
855 */
856 tx_skb->skb = NULL;
857 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200858 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500859 macb_tx_ring_wrap(bp, tail),
860 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200861 bp->dev->stats.tx_packets++;
862 bp->dev->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200863 }
864
865 /* Now we can safely release resources */
866 macb_tx_unmap(bp, tx_skb);
867
868 /* skb is set only for the last buffer of the frame.
869 * WARNING: at this point skb has been freed by
870 * macb_tx_unmap().
871 */
872 if (skb)
873 break;
874 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100875 }
876
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100877 queue->tx_tail = tail;
878 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
879 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500880 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100881 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100882}
883
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000884static void gem_rx_refill(struct macb_queue *queue)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000885{
886 unsigned int entry;
887 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000888 dma_addr_t paddr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000889 struct macb *bp = queue->bp;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000890 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000891
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000892 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
893 bp->rx_ring_size) > 0) {
894 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000895
896 /* Make hw descriptor updates visible to CPU */
897 rmb();
898
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000899 queue->rx_prepared_head++;
900 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000901
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000902 if (!queue->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000903 /* allocate sk_buff for this free entry in ring */
904 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -0700905 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000906 netdev_err(bp->dev,
907 "Unable to allocate sk_buff\n");
908 break;
909 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000910
911 /* now fill corresponding descriptor entry */
912 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700913 bp->rx_buffer_size,
914 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800915 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
916 dev_kfree_skb(skb);
917 break;
918 }
919
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000920 queue->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000921
Zach Brownb410d132016-10-19 09:56:57 -0500922 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000923 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000924 macb_set_addr(bp, desc, paddr);
925 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000926
927 /* properly align Ethernet header */
928 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530929 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000930 desc->addr &= ~MACB_BIT(RX_USED);
931 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000932 }
933 }
934
935 /* Make descriptor updates visible to hardware */
936 wmb();
937
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000938 netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
939 queue, queue->rx_prepared_head, queue->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000940}
941
942/* Mark DMA descriptors from begin up to and not including end as unused */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000943static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
Nicolas Ferre4df95132013-06-04 21:57:12 +0000944 unsigned int end)
945{
946 unsigned int frag;
947
948 for (frag = begin; frag != end; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000949 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700950
Nicolas Ferre4df95132013-06-04 21:57:12 +0000951 desc->addr &= ~MACB_BIT(RX_USED);
952 }
953
954 /* Make descriptor updates visible to hardware */
955 wmb();
956
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700957 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +0000958 * whatever caused this is updated, so we don't have to record
959 * anything.
960 */
961}
962
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000963static int gem_rx(struct macb_queue *queue, int budget)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000964{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000965 struct macb *bp = queue->bp;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000966 unsigned int len;
967 unsigned int entry;
968 struct sk_buff *skb;
969 struct macb_dma_desc *desc;
970 int count = 0;
971
972 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +0530973 u32 ctrl;
974 dma_addr_t addr;
975 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000976
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000977 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
978 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000979
980 /* Make hw descriptor updates visible to CPU */
981 rmb();
982
Harini Katakamfff80192016-08-09 13:15:53 +0530983 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000984 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000985 ctrl = desc->ctrl;
986
Harini Katakamfff80192016-08-09 13:15:53 +0530987 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000988 break;
989
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000990 queue->rx_tail++;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000991 count++;
992
993 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
994 netdev_err(bp->dev,
995 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200996 bp->dev->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000997 break;
998 }
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000999 skb = queue->rx_skbuff[entry];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001000 if (unlikely(!skb)) {
1001 netdev_err(bp->dev,
1002 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001003 bp->dev->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001004 break;
1005 }
1006 /* now everything is ready for receiving packet */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001007 queue->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301008 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001009
1010 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1011
1012 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001013 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -08001014 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001015
1016 skb->protocol = eth_type_trans(skb, bp->dev);
1017 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001018 if (bp->dev->features & NETIF_F_RXCSUM &&
1019 !(bp->dev->flags & IFF_PROMISC) &&
1020 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1021 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001022
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001023 bp->dev->stats.rx_packets++;
1024 bp->dev->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001025
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001026 gem_ptp_do_rxstamp(bp, skb, desc);
1027
Nicolas Ferre4df95132013-06-04 21:57:12 +00001028#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1029 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1030 skb->len, skb->csum);
1031 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +01001032 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001033 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1034 skb->data, 32, true);
1035#endif
1036
1037 netif_receive_skb(skb);
1038 }
1039
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001040 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001041
1042 return count;
1043}
1044
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001045static int macb_rx_frame(struct macb_queue *queue, unsigned int first_frag,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001046 unsigned int last_frag)
1047{
1048 unsigned int len;
1049 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001050 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001051 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001052 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001053 struct macb *bp = queue->bp;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001054
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001055 desc = macb_rx_desc(queue, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301056 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001057
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001058 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -05001059 macb_rx_ring_wrap(bp, first_frag),
1060 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001061
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001062 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001063 * first buffer. Since the header is 14 bytes, this makes the
1064 * payload word-aligned.
1065 *
1066 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1067 * the two padding bytes into the skb so that we avoid hitting
1068 * the slowpath in memcpy(), and pull them off afterwards.
1069 */
1070 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001071 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001072 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001073 for (frag = first_frag; ; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001074 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001075 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001076 if (frag == last_frag)
1077 break;
1078 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001079
1080 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001081 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001082
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001083 return 1;
1084 }
1085
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001086 offset = 0;
1087 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001088 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001089 skb_put(skb, len);
1090
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001091 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001092 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001093
1094 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001095 if (unlikely(frag != last_frag)) {
1096 dev_kfree_skb_any(skb);
1097 return -1;
1098 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001099 frag_len = len - offset;
1100 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001101 skb_copy_to_linear_data_offset(skb, offset,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001102 macb_rx_buffer(queue, frag),
Moritz Fischeraa50b552016-03-29 19:11:13 -07001103 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001104 offset += bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001105 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001106 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001107
1108 if (frag == last_frag)
1109 break;
1110 }
1111
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001112 /* Make descriptor updates visible to hardware */
1113 wmb();
1114
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001115 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001116 skb->protocol = eth_type_trans(skb, bp->dev);
1117
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001118 bp->dev->stats.rx_packets++;
1119 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001120 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001121 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001122 netif_receive_skb(skb);
1123
1124 return 0;
1125}
1126
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001127static inline void macb_init_rx_ring(struct macb_queue *queue)
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001128{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001129 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001130 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001131 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001132 int i;
1133
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001134 addr = queue->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001135 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001136 desc = macb_rx_desc(queue, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001137 macb_set_addr(bp, desc, addr);
1138 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001139 addr += bp->rx_buffer_size;
1140 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001141 desc->addr |= MACB_BIT(RX_WRAP);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001142 queue->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001143}
1144
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001145static int macb_rx(struct macb_queue *queue, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001146{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001147 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001148 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001149 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001150 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001151 int first_frag = -1;
1152
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001153 for (tail = queue->rx_tail; budget > 0; tail++) {
1154 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001155 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001156
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001157 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001158 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001159
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001160 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001161
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001162 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001163 break;
1164
1165 if (ctrl & MACB_BIT(RX_SOF)) {
1166 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001167 discard_partial_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001168 first_frag = tail;
1169 }
1170
1171 if (ctrl & MACB_BIT(RX_EOF)) {
1172 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001173
1174 if (unlikely(first_frag == -1)) {
1175 reset_rx_queue = true;
1176 continue;
1177 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001178
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001179 dropped = macb_rx_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001180 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001181 if (unlikely(dropped < 0)) {
1182 reset_rx_queue = true;
1183 continue;
1184 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001185 if (!dropped) {
1186 received++;
1187 budget--;
1188 }
1189 }
1190 }
1191
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001192 if (unlikely(reset_rx_queue)) {
1193 unsigned long flags;
1194 u32 ctrl;
1195
1196 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1197
1198 spin_lock_irqsave(&bp->lock, flags);
1199
1200 ctrl = macb_readl(bp, NCR);
1201 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1202
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001203 macb_init_rx_ring(queue);
1204 queue_writel(queue, RBQP, queue->rx_ring_dma);
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001205
1206 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1207
1208 spin_unlock_irqrestore(&bp->lock, flags);
1209 return received;
1210 }
1211
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001212 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001213 queue->rx_tail = first_frag;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001214 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001215 queue->rx_tail = tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001216
1217 return received;
1218}
1219
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001220static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001221{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001222 struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
1223 struct macb *bp = queue->bp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001224 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001225 u32 status;
1226
1227 status = macb_readl(bp, RSR);
1228 macb_writel(bp, RSR, status);
1229
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001230 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001231 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001232
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001233 work_done = bp->macbgem_ops.mog_rx(queue, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001234 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001235 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001236
Nicolas Ferre8770e912013-02-12 11:08:48 +01001237 /* Packets received while interrupts were disabled */
1238 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001239 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001240 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001241 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001242 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001243 } else {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001244 queue_writel(queue, IER, MACB_RX_INT_FLAGS);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001245 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001246 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001247
1248 /* TODO: Handle errors */
1249
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001250 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001251}
1252
1253static irqreturn_t macb_interrupt(int irq, void *dev_id)
1254{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001255 struct macb_queue *queue = dev_id;
1256 struct macb *bp = queue->bp;
1257 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001258 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001259
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001260 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001261
1262 if (unlikely(!status))
1263 return IRQ_NONE;
1264
1265 spin_lock(&bp->lock);
1266
1267 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001268 /* close possible race with dev_close */
1269 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001270 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001271 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1272 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001273 break;
1274 }
1275
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001276 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1277 (unsigned int)(queue - bp->queues),
1278 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001279
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001280 if (status & MACB_RX_INT_FLAGS) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001281 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001282 * until we have processed the buffers. The
1283 * scheduling call may fail if the poll routine
1284 * is already scheduled, so disable interrupts
1285 * now.
1286 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001287 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001288 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001289 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001290
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001291 if (napi_schedule_prep(&queue->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001292 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001293 __napi_schedule(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001294 }
1295 }
1296
Nicolas Ferree86cd532012-10-31 06:04:57 +00001297 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001298 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1299 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001300
1301 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001302 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001303
Nicolas Ferree86cd532012-10-31 06:04:57 +00001304 break;
1305 }
1306
1307 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001308 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001309
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001310 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001311 * add that if/when we get our hands on a full-blown MII PHY.
1312 */
1313
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001314 /* There is a hardware issue under heavy load where DMA can
1315 * stop, this causes endless "used buffer descriptor read"
1316 * interrupts but it can be cleared by re-enabling RX. See
1317 * the at91 manual, section 41.3.1 or the Zynq manual
1318 * section 16.7.4 for details.
1319 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001320 if (status & MACB_BIT(RXUBR)) {
1321 ctrl = macb_readl(bp, NCR);
1322 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001323 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001324 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1325
1326 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001327 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001328 }
1329
Alexander Steinb19f7f72011-04-13 05:03:24 +00001330 if (status & MACB_BIT(ISR_ROVR)) {
1331 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001332 if (macb_is_gem(bp))
1333 bp->hw_stats.gem.rx_overruns++;
1334 else
1335 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001336
1337 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001338 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001339 }
1340
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001341 if (status & MACB_BIT(HRESP)) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001342 /* TODO: Reset the hardware, and maybe move the
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001343 * netdev_err to a lower-priority context as well
1344 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001345 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001346 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001347
1348 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001349 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001350 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001351 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001352 }
1353
1354 spin_unlock(&bp->lock);
1355
1356 return IRQ_HANDLED;
1357}
1358
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001359#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001360/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001361 * to allow network i/o with interrupts disabled.
1362 */
1363static void macb_poll_controller(struct net_device *dev)
1364{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001365 struct macb *bp = netdev_priv(dev);
1366 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001367 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001368 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001369
1370 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001371 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1372 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001373 local_irq_restore(flags);
1374}
1375#endif
1376
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001377static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001378 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001379 struct sk_buff *skb,
1380 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001381{
1382 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001383 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001384 struct macb_tx_skb *tx_skb = NULL;
1385 struct macb_dma_desc *desc;
1386 unsigned int offset, size, count = 0;
1387 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001388 unsigned int eof = 1, mss_mfs = 0;
1389 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1390
1391 /* LSO */
1392 if (skb_shinfo(skb)->gso_size != 0) {
1393 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1394 /* UDP - UFO */
1395 lso_ctrl = MACB_LSO_UFO_ENABLE;
1396 else
1397 /* TCP - TSO */
1398 lso_ctrl = MACB_LSO_TSO_ENABLE;
1399 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001400
1401 /* First, map non-paged data */
1402 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001403
1404 /* first buffer length */
1405 size = hdrlen;
1406
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001407 offset = 0;
1408 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001409 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001410 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001411
1412 mapping = dma_map_single(&bp->pdev->dev,
1413 skb->data + offset,
1414 size, DMA_TO_DEVICE);
1415 if (dma_mapping_error(&bp->pdev->dev, mapping))
1416 goto dma_error;
1417
1418 /* Save info to properly release resources */
1419 tx_skb->skb = NULL;
1420 tx_skb->mapping = mapping;
1421 tx_skb->size = size;
1422 tx_skb->mapped_as_page = false;
1423
1424 len -= size;
1425 offset += size;
1426 count++;
1427 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001428
1429 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001430 }
1431
1432 /* Then, map paged data from fragments */
1433 for (f = 0; f < nr_frags; f++) {
1434 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1435
1436 len = skb_frag_size(frag);
1437 offset = 0;
1438 while (len) {
1439 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001440 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001441 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001442
1443 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1444 offset, size, DMA_TO_DEVICE);
1445 if (dma_mapping_error(&bp->pdev->dev, mapping))
1446 goto dma_error;
1447
1448 /* Save info to properly release resources */
1449 tx_skb->skb = NULL;
1450 tx_skb->mapping = mapping;
1451 tx_skb->size = size;
1452 tx_skb->mapped_as_page = true;
1453
1454 len -= size;
1455 offset += size;
1456 count++;
1457 tx_head++;
1458 }
1459 }
1460
1461 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001462 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001463 netdev_err(bp->dev, "BUG! empty skb!\n");
1464 return 0;
1465 }
1466
1467 /* This is the last buffer of the frame: save socket buffer */
1468 tx_skb->skb = skb;
1469
1470 /* Update TX ring: update buffer descriptors in reverse order
1471 * to avoid race condition
1472 */
1473
1474 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1475 * to set the end of TX queue
1476 */
1477 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001478 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001479 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001480 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001481 desc->ctrl = ctrl;
1482
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001483 if (lso_ctrl) {
1484 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1485 /* include header and FCS in value given to h/w */
1486 mss_mfs = skb_shinfo(skb)->gso_size +
1487 skb_transport_offset(skb) +
1488 ETH_FCS_LEN;
1489 else /* TSO */ {
1490 mss_mfs = skb_shinfo(skb)->gso_size;
1491 /* TCP Sequence Number Source Select
1492 * can be set only for TSO
1493 */
1494 seq_ctrl = 0;
1495 }
1496 }
1497
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001498 do {
1499 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001500 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001501 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001502 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001503
1504 ctrl = (u32)tx_skb->size;
1505 if (eof) {
1506 ctrl |= MACB_BIT(TX_LAST);
1507 eof = 0;
1508 }
Zach Brownb410d132016-10-19 09:56:57 -05001509 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001510 ctrl |= MACB_BIT(TX_WRAP);
1511
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001512 /* First descriptor is header descriptor */
1513 if (i == queue->tx_head) {
1514 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1515 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
1516 } else
1517 /* Only set MSS/MFS on payload descriptors
1518 * (second or later descriptor)
1519 */
1520 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1521
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001522 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001523 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001524 /* desc->addr must be visible to hardware before clearing
1525 * 'TX_USED' bit in desc->ctrl.
1526 */
1527 wmb();
1528 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001529 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001530
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001531 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001532
1533 return count;
1534
1535dma_error:
1536 netdev_err(bp->dev, "TX DMA map failed\n");
1537
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001538 for (i = queue->tx_head; i != tx_head; i++) {
1539 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001540
1541 macb_tx_unmap(bp, tx_skb);
1542 }
1543
1544 return 0;
1545}
1546
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001547static netdev_features_t macb_features_check(struct sk_buff *skb,
1548 struct net_device *dev,
1549 netdev_features_t features)
1550{
1551 unsigned int nr_frags, f;
1552 unsigned int hdrlen;
1553
1554 /* Validate LSO compatibility */
1555
1556 /* there is only one buffer */
1557 if (!skb_is_nonlinear(skb))
1558 return features;
1559
1560 /* length of header */
1561 hdrlen = skb_transport_offset(skb);
1562 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1563 hdrlen += tcp_hdrlen(skb);
1564
1565 /* For LSO:
1566 * When software supplies two or more payload buffers all payload buffers
1567 * apart from the last must be a multiple of 8 bytes in size.
1568 */
1569 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1570 return features & ~MACB_NETIF_LSO;
1571
1572 nr_frags = skb_shinfo(skb)->nr_frags;
1573 /* No need to check last fragment */
1574 nr_frags--;
1575 for (f = 0; f < nr_frags; f++) {
1576 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1577
1578 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1579 return features & ~MACB_NETIF_LSO;
1580 }
1581 return features;
1582}
1583
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001584static inline int macb_clear_csum(struct sk_buff *skb)
1585{
1586 /* no change for packets without checksum offloading */
1587 if (skb->ip_summed != CHECKSUM_PARTIAL)
1588 return 0;
1589
1590 /* make sure we can modify the header */
1591 if (unlikely(skb_cow_head(skb, 0)))
1592 return -1;
1593
1594 /* initialize checksum field
1595 * This is required - at least for Zynq, which otherwise calculates
1596 * wrong UDP header checksums for UDP packets with UDP data len <=2
1597 */
1598 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1599 return 0;
1600}
1601
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001602static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1603{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001604 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001605 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001606 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001607 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001608 unsigned int desc_cnt, nr_frags, frag_size, f;
1609 unsigned int hdrlen;
1610 bool is_lso, is_udp = 0;
1611
1612 is_lso = (skb_shinfo(skb)->gso_size != 0);
1613
1614 if (is_lso) {
1615 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1616
1617 /* length of headers */
1618 if (is_udp)
1619 /* only queue eth + ip headers separately for UDP */
1620 hdrlen = skb_transport_offset(skb);
1621 else
1622 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1623 if (skb_headlen(skb) < hdrlen) {
1624 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1625 /* if this is required, would need to copy to single buffer */
1626 return NETDEV_TX_BUSY;
1627 }
1628 } else
1629 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001630
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001631#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1632 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001633 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1634 queue_index, skb->len, skb->head, skb->data,
1635 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001636 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1637 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001638#endif
1639
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001640 /* Count how many TX buffer descriptors are needed to send this
1641 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001642 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001643 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001644 if (is_lso && (skb_headlen(skb) > hdrlen))
1645 /* extra header descriptor if also payload in first buffer */
1646 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1647 else
1648 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001649 nr_frags = skb_shinfo(skb)->nr_frags;
1650 for (f = 0; f < nr_frags; f++) {
1651 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001652 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001653 }
1654
Dongdong Deng48719532009-08-23 19:49:07 -07001655 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001656
1657 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001658 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001659 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001660 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001661 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001662 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001663 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001664 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001665 }
1666
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001667 if (macb_clear_csum(skb)) {
1668 dev_kfree_skb_any(skb);
Wei Yongjuna7c22bd2016-09-10 11:17:57 +00001669 goto unlock;
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001670 }
1671
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001672 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001673 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001674 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001675 goto unlock;
1676 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001677
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001678 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001679 wmb();
Richard Cochrane0720922011-06-19 21:51:28 +00001680 skb_tx_timestamp(skb);
1681
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001682 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1683
Zach Brownb410d132016-10-19 09:56:57 -05001684 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001685 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001686
Soren Brinkmann92030902014-03-04 08:46:39 -08001687unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001688 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001689
Patrick McHardy6ed10652009-06-23 06:03:08 +00001690 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001691}
1692
Nicolas Ferre4df95132013-06-04 21:57:12 +00001693static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001694{
1695 if (!macb_is_gem(bp)) {
1696 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1697 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001698 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001699
Nicolas Ferre1b447912013-06-04 21:57:11 +00001700 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001701 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001702 "RX buffer must be multiple of %d bytes, expanding\n",
1703 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001704 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001705 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001706 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001707 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001708
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001709 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00001710 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001711}
1712
Nicolas Ferre4df95132013-06-04 21:57:12 +00001713static void gem_free_rx_buffers(struct macb *bp)
1714{
1715 struct sk_buff *skb;
1716 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001717 struct macb_queue *queue;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001718 dma_addr_t addr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001719 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001720 int i;
1721
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001722 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1723 if (!queue->rx_skbuff)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001724 continue;
1725
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001726 for (i = 0; i < bp->rx_ring_size; i++) {
1727 skb = queue->rx_skbuff[i];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001728
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001729 if (!skb)
1730 continue;
1731
1732 desc = macb_rx_desc(queue, i);
1733 addr = macb_get_addr(bp, desc);
1734
1735 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
1736 DMA_FROM_DEVICE);
1737 dev_kfree_skb_any(skb);
1738 skb = NULL;
1739 }
1740
1741 kfree(queue->rx_skbuff);
1742 queue->rx_skbuff = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001743 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001744}
1745
1746static void macb_free_rx_buffers(struct macb *bp)
1747{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001748 struct macb_queue *queue = &bp->queues[0];
1749
1750 if (queue->rx_buffers) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001751 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05001752 bp->rx_ring_size * bp->rx_buffer_size,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001753 queue->rx_buffers, queue->rx_buffers_dma);
1754 queue->rx_buffers = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001755 }
1756}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001757
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001758static void macb_free_consistent(struct macb *bp)
1759{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001760 struct macb_queue *queue;
1761 unsigned int q;
1762
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001763 queue = &bp->queues[0];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001764 bp->macbgem_ops.mog_free_rx_buffers(bp);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001765 if (queue->rx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001766 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES(bp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001767 queue->rx_ring, queue->rx_ring_dma);
1768 queue->rx_ring = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001769 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001770
1771 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1772 kfree(queue->tx_skb);
1773 queue->tx_skb = NULL;
1774 if (queue->tx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001775 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES(bp),
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001776 queue->tx_ring, queue->tx_ring_dma);
1777 queue->tx_ring = NULL;
1778 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001779 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001780}
1781
1782static int gem_alloc_rx_buffers(struct macb *bp)
1783{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001784 struct macb_queue *queue;
1785 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001786 int size;
1787
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001788 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1789 size = bp->rx_ring_size * sizeof(struct sk_buff *);
1790 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
1791 if (!queue->rx_skbuff)
1792 return -ENOMEM;
1793 else
1794 netdev_dbg(bp->dev,
1795 "Allocated %d RX struct sk_buff entries at %p\n",
1796 bp->rx_ring_size, queue->rx_skbuff);
1797 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001798 return 0;
1799}
1800
1801static int macb_alloc_rx_buffers(struct macb *bp)
1802{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001803 struct macb_queue *queue = &bp->queues[0];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001804 int size;
1805
Zach Brownb410d132016-10-19 09:56:57 -05001806 size = bp->rx_ring_size * bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001807 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1808 &queue->rx_buffers_dma, GFP_KERNEL);
1809 if (!queue->rx_buffers)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001810 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001811
1812 netdev_dbg(bp->dev,
1813 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001814 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001815 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001816}
1817
1818static int macb_alloc_consistent(struct macb *bp)
1819{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001820 struct macb_queue *queue;
1821 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001822 int size;
1823
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001824 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001825 size = TX_RING_BYTES(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001826 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1827 &queue->tx_ring_dma,
1828 GFP_KERNEL);
1829 if (!queue->tx_ring)
1830 goto out_err;
1831 netdev_dbg(bp->dev,
1832 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1833 q, size, (unsigned long)queue->tx_ring_dma,
1834 queue->tx_ring);
1835
Zach Brownb410d132016-10-19 09:56:57 -05001836 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001837 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1838 if (!queue->tx_skb)
1839 goto out_err;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001840
1841 size = RX_RING_BYTES(bp);
1842 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1843 &queue->rx_ring_dma, GFP_KERNEL);
1844 if (!queue->rx_ring)
1845 goto out_err;
1846 netdev_dbg(bp->dev,
1847 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1848 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001849 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001850 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001851 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001852
1853 return 0;
1854
1855out_err:
1856 macb_free_consistent(bp);
1857 return -ENOMEM;
1858}
1859
Nicolas Ferre4df95132013-06-04 21:57:12 +00001860static void gem_init_rings(struct macb *bp)
1861{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001862 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001863 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001864 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001865 int i;
1866
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001867 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001868 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001869 desc = macb_tx_desc(queue, i);
1870 macb_set_addr(bp, desc, 0);
1871 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001872 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001873 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001874 queue->tx_head = 0;
1875 queue->tx_tail = 0;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001876
1877 queue->rx_tail = 0;
1878 queue->rx_prepared_head = 0;
1879
1880 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001881 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001882
Nicolas Ferre4df95132013-06-04 21:57:12 +00001883}
1884
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001885static void macb_init_rings(struct macb *bp)
1886{
1887 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001888 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001889
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001890 macb_init_rx_ring(&bp->queues[0]);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001891
Zach Brownb410d132016-10-19 09:56:57 -05001892 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001893 desc = macb_tx_desc(&bp->queues[0], i);
1894 macb_set_addr(bp, desc, 0);
1895 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001896 }
Ben Shelton21d35152015-04-22 17:28:54 -05001897 bp->queues[0].tx_head = 0;
1898 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001899 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001900}
1901
1902static void macb_reset_hw(struct macb *bp)
1903{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001904 struct macb_queue *queue;
1905 unsigned int q;
1906
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001907 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001908 * more gracefully?)
1909 */
1910 macb_writel(bp, NCR, 0);
1911
1912 /* Clear the stats registers (XXX: Update stats first?) */
1913 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1914
1915 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001916 macb_writel(bp, TSR, -1);
1917 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001918
1919 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001920 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1921 queue_writel(queue, IDR, -1);
1922 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06001923 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1924 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001925 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001926}
1927
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001928static u32 gem_mdc_clk_div(struct macb *bp)
1929{
1930 u32 config;
1931 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1932
1933 if (pclk_hz <= 20000000)
1934 config = GEM_BF(CLK, GEM_CLK_DIV8);
1935 else if (pclk_hz <= 40000000)
1936 config = GEM_BF(CLK, GEM_CLK_DIV16);
1937 else if (pclk_hz <= 80000000)
1938 config = GEM_BF(CLK, GEM_CLK_DIV32);
1939 else if (pclk_hz <= 120000000)
1940 config = GEM_BF(CLK, GEM_CLK_DIV48);
1941 else if (pclk_hz <= 160000000)
1942 config = GEM_BF(CLK, GEM_CLK_DIV64);
1943 else
1944 config = GEM_BF(CLK, GEM_CLK_DIV96);
1945
1946 return config;
1947}
1948
1949static u32 macb_mdc_clk_div(struct macb *bp)
1950{
1951 u32 config;
1952 unsigned long pclk_hz;
1953
1954 if (macb_is_gem(bp))
1955 return gem_mdc_clk_div(bp);
1956
1957 pclk_hz = clk_get_rate(bp->pclk);
1958 if (pclk_hz <= 20000000)
1959 config = MACB_BF(CLK, MACB_CLK_DIV8);
1960 else if (pclk_hz <= 40000000)
1961 config = MACB_BF(CLK, MACB_CLK_DIV16);
1962 else if (pclk_hz <= 80000000)
1963 config = MACB_BF(CLK, MACB_CLK_DIV32);
1964 else
1965 config = MACB_BF(CLK, MACB_CLK_DIV64);
1966
1967 return config;
1968}
1969
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001970/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00001971 * should program. We find the width from decoding the design configuration
1972 * register to find the maximum supported data bus width.
1973 */
1974static u32 macb_dbw(struct macb *bp)
1975{
1976 if (!macb_is_gem(bp))
1977 return 0;
1978
1979 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1980 case 4:
1981 return GEM_BF(DBW, GEM_DBW128);
1982 case 2:
1983 return GEM_BF(DBW, GEM_DBW64);
1984 case 1:
1985 default:
1986 return GEM_BF(DBW, GEM_DBW32);
1987 }
1988}
1989
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001990/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001991 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001992 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001993 * (if not supported by FIFO, it will fallback to default)
1994 * - set both rx/tx packet buffers to full memory size
1995 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001996 */
1997static void macb_configure_dma(struct macb *bp)
1998{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001999 struct macb_queue *queue;
2000 u32 buffer_size;
2001 unsigned int q;
Jamie Iles0116da42011-03-14 17:38:30 +00002002 u32 dmacfg;
2003
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002004 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
Jamie Iles0116da42011-03-14 17:38:30 +00002005 if (macb_is_gem(bp)) {
2006 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002007 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2008 if (q)
2009 queue_writel(queue, RBQS, buffer_size);
2010 else
2011 dmacfg |= GEM_BF(RXBS, buffer_size);
2012 }
Nicolas Ferree1755872014-07-24 13:50:58 +02002013 if (bp->dma_burst_length)
2014 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002015 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05302016 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05302017
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002018 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05302019 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2020 else
2021 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2022
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002023 if (bp->dev->features & NETIF_F_HW_CSUM)
2024 dmacfg |= GEM_BIT(TXCOEN);
2025 else
2026 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05302027
2028#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002029 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002030 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302031#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002032#ifdef CONFIG_MACB_USE_HWSTAMP
2033 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2034 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2035#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002036 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2037 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00002038 gem_writel(bp, DMACFG, dmacfg);
2039 }
2040}
2041
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002042static void macb_init_hw(struct macb *bp)
2043{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002044 struct macb_queue *queue;
2045 unsigned int q;
2046
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002047 u32 config;
2048
2049 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00002050 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002051
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002052 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302053 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2054 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00002055 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002056 config |= MACB_BIT(PAE); /* PAuse Enable */
2057 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03002058 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302059 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2060 else
2061 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002062 if (bp->dev->flags & IFF_PROMISC)
2063 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002064 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2065 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002066 if (!(bp->dev->flags & IFF_BROADCAST))
2067 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00002068 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002069 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03002070 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302071 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00002072 bp->speed = SPEED_10;
2073 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302074 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03002075 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302076 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002077
Jamie Iles0116da42011-03-14 17:38:30 +00002078 macb_configure_dma(bp);
2079
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002080 /* Initialize TX and RX buffers */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002081 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002082 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
2083#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2084 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2085 queue_writel(queue, RBQPH, upper_32_bits(queue->rx_ring_dma));
2086#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002087 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302088#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002089 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002090 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302091#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002092
2093 /* Enable interrupts */
2094 queue_writel(queue, IER,
2095 MACB_RX_INT_FLAGS |
2096 MACB_TX_INT_FLAGS |
2097 MACB_BIT(HRESP));
2098 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002099
2100 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02002101 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002102}
2103
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002104/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002105 * locations in the memory map. The least significant bits are stored
2106 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2107 *
2108 * The unicast hash enable and the multicast hash enable bits in the
2109 * network configuration register enable the reception of hash matched
2110 * frames. The destination address is reduced to a 6 bit index into
2111 * the 64 bit hash register using the following hash function. The
2112 * hash function is an exclusive or of every sixth bit of the
2113 * destination address.
2114 *
2115 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2116 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2117 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2118 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2119 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2120 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2121 *
2122 * da[0] represents the least significant bit of the first byte
2123 * received, that is, the multicast/unicast indicator, and da[47]
2124 * represents the most significant bit of the last byte received. If
2125 * the hash index, hi[n], points to a bit that is set in the hash
2126 * register then the frame will be matched according to whether the
2127 * frame is multicast or unicast. A multicast match will be signalled
2128 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2129 * index points to a bit set in the hash register. A unicast match
2130 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2131 * and the hash index points to a bit set in the hash register. To
2132 * receive all multicast frames, the hash register should be set with
2133 * all ones and the multicast hash enable bit should be set in the
2134 * network configuration register.
2135 */
2136
2137static inline int hash_bit_value(int bitnr, __u8 *addr)
2138{
2139 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2140 return 1;
2141 return 0;
2142}
2143
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002144/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002145static int hash_get_index(__u8 *addr)
2146{
2147 int i, j, bitval;
2148 int hash_index = 0;
2149
2150 for (j = 0; j < 6; j++) {
2151 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002152 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002153
2154 hash_index |= (bitval << j);
2155 }
2156
2157 return hash_index;
2158}
2159
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002160/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002161static void macb_sethashtable(struct net_device *dev)
2162{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002163 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002164 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002165 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002166 struct macb *bp = netdev_priv(dev);
2167
Moritz Fischeraa50b552016-03-29 19:11:13 -07002168 mc_filter[0] = 0;
2169 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002170
Jiri Pirko22bedad32010-04-01 21:22:57 +00002171 netdev_for_each_mc_addr(ha, dev) {
2172 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002173 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2174 }
2175
Jamie Ilesf75ba502011-11-08 10:12:32 +00002176 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2177 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002178}
2179
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002180/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002181static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002182{
2183 unsigned long cfg;
2184 struct macb *bp = netdev_priv(dev);
2185
2186 cfg = macb_readl(bp, NCFGR);
2187
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002188 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002189 /* Enable promiscuous mode */
2190 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002191
2192 /* Disable RX checksum offload */
2193 if (macb_is_gem(bp))
2194 cfg &= ~GEM_BIT(RXCOEN);
2195 } else {
2196 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002197 cfg &= ~MACB_BIT(CAF);
2198
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002199 /* Enable RX checksum offload only if requested */
2200 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2201 cfg |= GEM_BIT(RXCOEN);
2202 }
2203
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002204 if (dev->flags & IFF_ALLMULTI) {
2205 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002206 macb_or_gem_writel(bp, HRB, -1);
2207 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002208 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002209 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002210 /* Enable specific multicasts */
2211 macb_sethashtable(dev);
2212 cfg |= MACB_BIT(NCFGR_MTI);
2213 } else if (dev->flags & (~IFF_ALLMULTI)) {
2214 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002215 macb_or_gem_writel(bp, HRB, 0);
2216 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002217 cfg &= ~MACB_BIT(NCFGR_MTI);
2218 }
2219
2220 macb_writel(bp, NCFGR, cfg);
2221}
2222
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002223static int macb_open(struct net_device *dev)
2224{
2225 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002226 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002227 struct macb_queue *queue;
2228 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002229 int err;
2230
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002231 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002232
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002233 /* carrier starts down */
2234 netif_carrier_off(dev);
2235
frederic RODO6c36a702007-07-12 19:07:24 +02002236 /* if the phy is not yet register, retry later*/
Philippe Reynes0a912812016-06-22 00:32:35 +02002237 if (!dev->phydev)
frederic RODO6c36a702007-07-12 19:07:24 +02002238 return -EAGAIN;
2239
Nicolas Ferre1b447912013-06-04 21:57:11 +00002240 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002241 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002242
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002243 err = macb_alloc_consistent(bp);
2244 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002245 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2246 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002247 return err;
2248 }
2249
Nicolas Ferre4df95132013-06-04 21:57:12 +00002250 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002251 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002252
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002253 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2254 napi_enable(&queue->napi);
2255
frederic RODO6c36a702007-07-12 19:07:24 +02002256 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002257 phy_start(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002258
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002259 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002260
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002261 if (bp->ptp_info)
2262 bp->ptp_info->ptp_init(dev);
2263
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002264 return 0;
2265}
2266
2267static int macb_close(struct net_device *dev)
2268{
2269 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002270 struct macb_queue *queue;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002271 unsigned long flags;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002272 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002273
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002274 netif_tx_stop_all_queues(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002275
2276 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2277 napi_disable(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002278
Philippe Reynes0a912812016-06-22 00:32:35 +02002279 if (dev->phydev)
2280 phy_stop(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002281
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002282 spin_lock_irqsave(&bp->lock, flags);
2283 macb_reset_hw(bp);
2284 netif_carrier_off(dev);
2285 spin_unlock_irqrestore(&bp->lock, flags);
2286
2287 macb_free_consistent(bp);
2288
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002289 if (bp->ptp_info)
2290 bp->ptp_info->ptp_remove(dev);
2291
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002292 return 0;
2293}
2294
Harini Katakama5898ea2015-05-06 22:27:18 +05302295static int macb_change_mtu(struct net_device *dev, int new_mtu)
2296{
Harini Katakama5898ea2015-05-06 22:27:18 +05302297 if (netif_running(dev))
2298 return -EBUSY;
2299
Harini Katakama5898ea2015-05-06 22:27:18 +05302300 dev->mtu = new_mtu;
2301
2302 return 0;
2303}
2304
Jamie Ilesa494ed82011-03-09 16:26:35 +00002305static void gem_update_stats(struct macb *bp)
2306{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002307 unsigned int i;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002308 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002309
Xander Huff3ff13f12015-01-13 16:15:51 -06002310 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2311 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002312 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002313
2314 bp->ethtool_stats[i] += val;
2315 *p += val;
2316
2317 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2318 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002319 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002320 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002321 *(++p) += val;
2322 }
2323 }
Jamie Ilesa494ed82011-03-09 16:26:35 +00002324}
2325
2326static struct net_device_stats *gem_get_stats(struct macb *bp)
2327{
2328 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002329 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002330
2331 gem_update_stats(bp);
2332
2333 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2334 hwstat->rx_alignment_errors +
2335 hwstat->rx_resource_errors +
2336 hwstat->rx_overruns +
2337 hwstat->rx_oversize_frames +
2338 hwstat->rx_jabbers +
2339 hwstat->rx_undersized_frames +
2340 hwstat->rx_length_field_frame_errors);
2341 nstat->tx_errors = (hwstat->tx_late_collisions +
2342 hwstat->tx_excessive_collisions +
2343 hwstat->tx_underrun +
2344 hwstat->tx_carrier_sense_errors);
2345 nstat->multicast = hwstat->rx_multicast_frames;
2346 nstat->collisions = (hwstat->tx_single_collision_frames +
2347 hwstat->tx_multiple_collision_frames +
2348 hwstat->tx_excessive_collisions);
2349 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2350 hwstat->rx_jabbers +
2351 hwstat->rx_undersized_frames +
2352 hwstat->rx_length_field_frame_errors);
2353 nstat->rx_over_errors = hwstat->rx_resource_errors;
2354 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2355 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2356 nstat->rx_fifo_errors = hwstat->rx_overruns;
2357 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2358 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2359 nstat->tx_fifo_errors = hwstat->tx_underrun;
2360
2361 return nstat;
2362}
2363
Xander Huff3ff13f12015-01-13 16:15:51 -06002364static void gem_get_ethtool_stats(struct net_device *dev,
2365 struct ethtool_stats *stats, u64 *data)
2366{
2367 struct macb *bp;
2368
2369 bp = netdev_priv(dev);
2370 gem_update_stats(bp);
Xander Huff2fa45e22015-01-15 15:55:19 -06002371 memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
Xander Huff3ff13f12015-01-13 16:15:51 -06002372}
2373
2374static int gem_get_sset_count(struct net_device *dev, int sset)
2375{
2376 switch (sset) {
2377 case ETH_SS_STATS:
2378 return GEM_STATS_LEN;
2379 default:
2380 return -EOPNOTSUPP;
2381 }
2382}
2383
2384static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2385{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002386 unsigned int i;
Xander Huff3ff13f12015-01-13 16:15:51 -06002387
2388 switch (sset) {
2389 case ETH_SS_STATS:
2390 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2391 memcpy(p, gem_statistics[i].stat_string,
2392 ETH_GSTRING_LEN);
2393 break;
2394 }
2395}
2396
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002397static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002398{
2399 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002400 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002401 struct macb_stats *hwstat = &bp->hw_stats.macb;
2402
2403 if (macb_is_gem(bp))
2404 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002405
frederic RODO6c36a702007-07-12 19:07:24 +02002406 /* read stats from hardware */
2407 macb_update_stats(bp);
2408
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002409 /* Convert HW stats into netdevice stats */
2410 nstat->rx_errors = (hwstat->rx_fcs_errors +
2411 hwstat->rx_align_errors +
2412 hwstat->rx_resource_errors +
2413 hwstat->rx_overruns +
2414 hwstat->rx_oversize_pkts +
2415 hwstat->rx_jabbers +
2416 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002417 hwstat->rx_length_mismatch);
2418 nstat->tx_errors = (hwstat->tx_late_cols +
2419 hwstat->tx_excessive_cols +
2420 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002421 hwstat->tx_carrier_errors +
2422 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002423 nstat->collisions = (hwstat->tx_single_cols +
2424 hwstat->tx_multiple_cols +
2425 hwstat->tx_excessive_cols);
2426 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2427 hwstat->rx_jabbers +
2428 hwstat->rx_undersize_pkts +
2429 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002430 nstat->rx_over_errors = hwstat->rx_resource_errors +
2431 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002432 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2433 nstat->rx_frame_errors = hwstat->rx_align_errors;
2434 nstat->rx_fifo_errors = hwstat->rx_overruns;
2435 /* XXX: What does "missed" mean? */
2436 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2437 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2438 nstat->tx_fifo_errors = hwstat->tx_underruns;
2439 /* Don't know about heartbeat or window errors... */
2440
2441 return nstat;
2442}
2443
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002444static int macb_get_regs_len(struct net_device *netdev)
2445{
2446 return MACB_GREGS_NBR * sizeof(u32);
2447}
2448
2449static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2450 void *p)
2451{
2452 struct macb *bp = netdev_priv(dev);
2453 unsigned int tail, head;
2454 u32 *regs_buff = p;
2455
2456 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2457 | MACB_GREGS_VERSION;
2458
Zach Brownb410d132016-10-19 09:56:57 -05002459 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2460 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002461
2462 regs_buff[0] = macb_readl(bp, NCR);
2463 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2464 regs_buff[2] = macb_readl(bp, NSR);
2465 regs_buff[3] = macb_readl(bp, TSR);
2466 regs_buff[4] = macb_readl(bp, RBQP);
2467 regs_buff[5] = macb_readl(bp, TBQP);
2468 regs_buff[6] = macb_readl(bp, RSR);
2469 regs_buff[7] = macb_readl(bp, IMR);
2470
2471 regs_buff[8] = tail;
2472 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002473 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2474 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002475
Neil Armstrongce721a72016-01-05 14:39:16 +01002476 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2477 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002478 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002479 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002480}
2481
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002482static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2483{
2484 struct macb *bp = netdev_priv(netdev);
2485
2486 wol->supported = 0;
2487 wol->wolopts = 0;
2488
2489 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2490 wol->supported = WAKE_MAGIC;
2491
2492 if (bp->wol & MACB_WOL_ENABLED)
2493 wol->wolopts |= WAKE_MAGIC;
2494 }
2495}
2496
2497static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2498{
2499 struct macb *bp = netdev_priv(netdev);
2500
2501 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2502 (wol->wolopts & ~WAKE_MAGIC))
2503 return -EOPNOTSUPP;
2504
2505 if (wol->wolopts & WAKE_MAGIC)
2506 bp->wol |= MACB_WOL_ENABLED;
2507 else
2508 bp->wol &= ~MACB_WOL_ENABLED;
2509
2510 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2511
2512 return 0;
2513}
2514
Zach Brown8441bb32016-10-19 09:56:58 -05002515static void macb_get_ringparam(struct net_device *netdev,
2516 struct ethtool_ringparam *ring)
2517{
2518 struct macb *bp = netdev_priv(netdev);
2519
2520 ring->rx_max_pending = MAX_RX_RING_SIZE;
2521 ring->tx_max_pending = MAX_TX_RING_SIZE;
2522
2523 ring->rx_pending = bp->rx_ring_size;
2524 ring->tx_pending = bp->tx_ring_size;
2525}
2526
2527static int macb_set_ringparam(struct net_device *netdev,
2528 struct ethtool_ringparam *ring)
2529{
2530 struct macb *bp = netdev_priv(netdev);
2531 u32 new_rx_size, new_tx_size;
2532 unsigned int reset = 0;
2533
2534 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2535 return -EINVAL;
2536
2537 new_rx_size = clamp_t(u32, ring->rx_pending,
2538 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2539 new_rx_size = roundup_pow_of_two(new_rx_size);
2540
2541 new_tx_size = clamp_t(u32, ring->tx_pending,
2542 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2543 new_tx_size = roundup_pow_of_two(new_tx_size);
2544
2545 if ((new_tx_size == bp->tx_ring_size) &&
2546 (new_rx_size == bp->rx_ring_size)) {
2547 /* nothing to do */
2548 return 0;
2549 }
2550
2551 if (netif_running(bp->dev)) {
2552 reset = 1;
2553 macb_close(bp->dev);
2554 }
2555
2556 bp->rx_ring_size = new_rx_size;
2557 bp->tx_ring_size = new_tx_size;
2558
2559 if (reset)
2560 macb_open(bp->dev);
2561
2562 return 0;
2563}
2564
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002565#ifdef CONFIG_MACB_USE_HWSTAMP
2566static unsigned int gem_get_tsu_rate(struct macb *bp)
2567{
2568 struct clk *tsu_clk;
2569 unsigned int tsu_rate;
2570
2571 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
2572 if (!IS_ERR(tsu_clk))
2573 tsu_rate = clk_get_rate(tsu_clk);
2574 /* try pclk instead */
2575 else if (!IS_ERR(bp->pclk)) {
2576 tsu_clk = bp->pclk;
2577 tsu_rate = clk_get_rate(tsu_clk);
2578 } else
2579 return -ENOTSUPP;
2580 return tsu_rate;
2581}
2582
2583static s32 gem_get_ptp_max_adj(void)
2584{
2585 return 64000000;
2586}
2587
2588static int gem_get_ts_info(struct net_device *dev,
2589 struct ethtool_ts_info *info)
2590{
2591 struct macb *bp = netdev_priv(dev);
2592
2593 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
2594 ethtool_op_get_ts_info(dev, info);
2595 return 0;
2596 }
2597
2598 info->so_timestamping =
2599 SOF_TIMESTAMPING_TX_SOFTWARE |
2600 SOF_TIMESTAMPING_RX_SOFTWARE |
2601 SOF_TIMESTAMPING_SOFTWARE |
2602 SOF_TIMESTAMPING_TX_HARDWARE |
2603 SOF_TIMESTAMPING_RX_HARDWARE |
2604 SOF_TIMESTAMPING_RAW_HARDWARE;
2605 info->tx_types =
2606 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
2607 (1 << HWTSTAMP_TX_OFF) |
2608 (1 << HWTSTAMP_TX_ON);
2609 info->rx_filters =
2610 (1 << HWTSTAMP_FILTER_NONE) |
2611 (1 << HWTSTAMP_FILTER_ALL);
2612
2613 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
2614
2615 return 0;
2616}
2617
2618static struct macb_ptp_info gem_ptp_info = {
2619 .ptp_init = gem_ptp_init,
2620 .ptp_remove = gem_ptp_remove,
2621 .get_ptp_max_adj = gem_get_ptp_max_adj,
2622 .get_tsu_rate = gem_get_tsu_rate,
2623 .get_ts_info = gem_get_ts_info,
2624 .get_hwtst = gem_get_hwtst,
2625 .set_hwtst = gem_set_hwtst,
2626};
2627#endif
2628
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002629static int macb_get_ts_info(struct net_device *netdev,
2630 struct ethtool_ts_info *info)
2631{
2632 struct macb *bp = netdev_priv(netdev);
2633
2634 if (bp->ptp_info)
2635 return bp->ptp_info->get_ts_info(netdev, info);
2636
2637 return ethtool_op_get_ts_info(netdev, info);
2638}
2639
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002640static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002641 .get_regs_len = macb_get_regs_len,
2642 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002643 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00002644 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002645 .get_wol = macb_get_wol,
2646 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02002647 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2648 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05002649 .get_ringparam = macb_get_ringparam,
2650 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06002651};
Xander Huff8cd5a562015-01-15 15:55:20 -06002652
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00002653static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06002654 .get_regs_len = macb_get_regs_len,
2655 .get_regs = macb_get_regs,
2656 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002657 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06002658 .get_ethtool_stats = gem_get_ethtool_stats,
2659 .get_strings = gem_get_ethtool_strings,
2660 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02002661 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2662 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05002663 .get_ringparam = macb_get_ringparam,
2664 .set_ringparam = macb_set_ringparam,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002665};
2666
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002667static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002668{
Philippe Reynes0a912812016-06-22 00:32:35 +02002669 struct phy_device *phydev = dev->phydev;
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002670 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002671
2672 if (!netif_running(dev))
2673 return -EINVAL;
2674
frederic RODO6c36a702007-07-12 19:07:24 +02002675 if (!phydev)
2676 return -ENODEV;
2677
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002678 if (!bp->ptp_info)
2679 return phy_mii_ioctl(phydev, rq, cmd);
2680
2681 switch (cmd) {
2682 case SIOCSHWTSTAMP:
2683 return bp->ptp_info->set_hwtst(dev, rq, cmd);
2684 case SIOCGHWTSTAMP:
2685 return bp->ptp_info->get_hwtst(dev, rq);
2686 default:
2687 return phy_mii_ioctl(phydev, rq, cmd);
2688 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002689}
2690
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002691static int macb_set_features(struct net_device *netdev,
2692 netdev_features_t features)
2693{
2694 struct macb *bp = netdev_priv(netdev);
2695 netdev_features_t changed = features ^ netdev->features;
2696
2697 /* TX checksum offload */
2698 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2699 u32 dmacfg;
2700
2701 dmacfg = gem_readl(bp, DMACFG);
2702 if (features & NETIF_F_HW_CSUM)
2703 dmacfg |= GEM_BIT(TXCOEN);
2704 else
2705 dmacfg &= ~GEM_BIT(TXCOEN);
2706 gem_writel(bp, DMACFG, dmacfg);
2707 }
2708
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002709 /* RX checksum offload */
2710 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2711 u32 netcfg;
2712
2713 netcfg = gem_readl(bp, NCFGR);
2714 if (features & NETIF_F_RXCSUM &&
2715 !(netdev->flags & IFF_PROMISC))
2716 netcfg |= GEM_BIT(RXCOEN);
2717 else
2718 netcfg &= ~GEM_BIT(RXCOEN);
2719 gem_writel(bp, NCFGR, netcfg);
2720 }
2721
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002722 return 0;
2723}
2724
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002725static const struct net_device_ops macb_netdev_ops = {
2726 .ndo_open = macb_open,
2727 .ndo_stop = macb_close,
2728 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002729 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002730 .ndo_get_stats = macb_get_stats,
2731 .ndo_do_ioctl = macb_ioctl,
2732 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05302733 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002734 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07002735#ifdef CONFIG_NET_POLL_CONTROLLER
2736 .ndo_poll_controller = macb_poll_controller,
2737#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002738 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002739 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002740};
2741
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002742/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02002743 * and integration options used
2744 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002745static void macb_configure_caps(struct macb *bp,
2746 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02002747{
2748 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02002749
Nicolas Ferref6970502015-03-31 15:02:01 +02002750 if (dt_conf)
2751 bp->caps = dt_conf->caps;
2752
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002753 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02002754 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2755
Nicolas Ferree1755872014-07-24 13:50:58 +02002756 dcfg = gem_readl(bp, DCFG1);
2757 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2758 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2759 dcfg = gem_readl(bp, DCFG2);
2760 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2761 bp->caps |= MACB_CAPS_FIFO_MODE;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002762#ifdef CONFIG_MACB_USE_HWSTAMP
2763 if (gem_has_ptp(bp)) {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002764 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
2765 pr_err("GEM doesn't support hardware ptp.\n");
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002766 else {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002767 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002768 bp->ptp_info = &gem_ptp_info;
2769 }
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002770 }
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002771#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002772 }
2773
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03002774 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02002775}
2776
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002777static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002778 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002779 unsigned int *queue_mask,
2780 unsigned int *num_queues)
2781{
2782 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002783
2784 *queue_mask = 0x1;
2785 *num_queues = 1;
2786
Nicolas Ferreda120112015-03-31 15:02:00 +02002787 /* is it macb or gem ?
2788 *
2789 * We need to read directly from the hardware here because
2790 * we are early in the probe process and don't have the
2791 * MACB_CAPS_MACB_IS_GEM flag positioned
2792 */
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002793 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002794 return;
2795
2796 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05302797 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
2798
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002799 *queue_mask |= 0x1;
2800
2801 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
2802 if (*queue_mask & (1 << hw_q))
2803 (*num_queues)++;
2804}
2805
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002806static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302807 struct clk **hclk, struct clk **tx_clk,
2808 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002809{
Bartosz Folta83a77e92016-12-14 06:39:15 +00002810 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002811 int err;
2812
Bartosz Folta83a77e92016-12-14 06:39:15 +00002813 pdata = dev_get_platdata(&pdev->dev);
2814 if (pdata) {
2815 *pclk = pdata->pclk;
2816 *hclk = pdata->hclk;
2817 } else {
2818 *pclk = devm_clk_get(&pdev->dev, "pclk");
2819 *hclk = devm_clk_get(&pdev->dev, "hclk");
2820 }
2821
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002822 if (IS_ERR(*pclk)) {
2823 err = PTR_ERR(*pclk);
2824 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
2825 return err;
2826 }
2827
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002828 if (IS_ERR(*hclk)) {
2829 err = PTR_ERR(*hclk);
2830 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
2831 return err;
2832 }
2833
2834 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
2835 if (IS_ERR(*tx_clk))
2836 *tx_clk = NULL;
2837
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302838 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
2839 if (IS_ERR(*rx_clk))
2840 *rx_clk = NULL;
2841
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002842 err = clk_prepare_enable(*pclk);
2843 if (err) {
2844 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2845 return err;
2846 }
2847
2848 err = clk_prepare_enable(*hclk);
2849 if (err) {
2850 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
2851 goto err_disable_pclk;
2852 }
2853
2854 err = clk_prepare_enable(*tx_clk);
2855 if (err) {
2856 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2857 goto err_disable_hclk;
2858 }
2859
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302860 err = clk_prepare_enable(*rx_clk);
2861 if (err) {
2862 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
2863 goto err_disable_txclk;
2864 }
2865
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002866 return 0;
2867
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302868err_disable_txclk:
2869 clk_disable_unprepare(*tx_clk);
2870
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002871err_disable_hclk:
2872 clk_disable_unprepare(*hclk);
2873
2874err_disable_pclk:
2875 clk_disable_unprepare(*pclk);
2876
2877 return err;
2878}
2879
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002880static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002881{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002882 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002883 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002884 struct macb *bp = netdev_priv(dev);
2885 struct macb_queue *queue;
2886 int err;
2887 u32 val;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002888
Zach Brownb410d132016-10-19 09:56:57 -05002889 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
2890 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
2891
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002892 /* set the queue register mapping once for all: queue0 has a special
2893 * register mapping but we don't want to test the queue index then
2894 * compute the corresponding register offset at run time.
2895 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002896 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002897 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002898 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00002899
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002900 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002901 queue->bp = bp;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002902 netif_napi_add(dev, &queue->napi, macb_poll, 64);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002903 if (hw_q) {
2904 queue->ISR = GEM_ISR(hw_q - 1);
2905 queue->IER = GEM_IER(hw_q - 1);
2906 queue->IDR = GEM_IDR(hw_q - 1);
2907 queue->IMR = GEM_IMR(hw_q - 1);
2908 queue->TBQP = GEM_TBQP(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002909 queue->RBQP = GEM_RBQP(hw_q - 1);
2910 queue->RBQS = GEM_RBQS(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05302911#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002912 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002913 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002914 queue->RBQPH = GEM_RBQPH(hw_q - 1);
2915 }
Harini Katakamfff80192016-08-09 13:15:53 +05302916#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002917 } else {
2918 /* queue0 uses legacy registers */
2919 queue->ISR = MACB_ISR;
2920 queue->IER = MACB_IER;
2921 queue->IDR = MACB_IDR;
2922 queue->IMR = MACB_IMR;
2923 queue->TBQP = MACB_TBQP;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002924 queue->RBQP = MACB_RBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05302925#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002926 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002927 queue->TBQPH = MACB_TBQPH;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002928 queue->RBQPH = MACB_RBQPH;
2929 }
Harini Katakamfff80192016-08-09 13:15:53 +05302930#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002931 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002932
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002933 /* get irq: here we use the linux queue index, not the hardware
2934 * queue index. the queue irq definitions in the device tree
2935 * must remove the optional gaps that could exist in the
2936 * hardware queue mask.
2937 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002938 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002939 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01002940 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002941 if (err) {
2942 dev_err(&pdev->dev,
2943 "Unable to request IRQ %d (error %d)\n",
2944 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002945 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002946 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002947
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002948 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002949 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002950 }
2951
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002952 dev->netdev_ops = &macb_netdev_ops;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002953
Nicolas Ferre4df95132013-06-04 21:57:12 +00002954 /* setup appropriated routines according to adapter type */
2955 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002956 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002957 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
2958 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
2959 bp->macbgem_ops.mog_init_rings = gem_init_rings;
2960 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002961 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002962 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002963 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002964 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
2965 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
2966 bp->macbgem_ops.mog_init_rings = macb_init_rings;
2967 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002968 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002969 }
2970
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002971 /* Set features */
2972 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002973
2974 /* Check LSO capability */
2975 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
2976 dev->hw_features |= MACB_NETIF_LSO;
2977
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002978 /* Checksum offload is only available on gem with packet buffer */
2979 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002980 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002981 if (bp->caps & MACB_CAPS_SG_DISABLED)
2982 dev->hw_features &= ~NETIF_F_SG;
2983 dev->features = dev->hw_features;
2984
Neil Armstrongce721a72016-01-05 14:39:16 +01002985 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
2986 val = 0;
2987 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
2988 val = GEM_BIT(RGMII);
2989 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002990 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002991 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002992 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002993 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002994
Neil Armstrongce721a72016-01-05 14:39:16 +01002995 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
2996 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002997
Neil Armstrongce721a72016-01-05 14:39:16 +01002998 macb_or_gem_writel(bp, USRIO, val);
2999 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003000
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003001 /* Set MII management clock divider */
3002 val = macb_mdc_clk_div(bp);
3003 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05303004 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3005 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003006 macb_writel(bp, NCFGR, val);
3007
3008 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003009}
3010
3011#if defined(CONFIG_OF)
3012/* 1518 rounded up */
3013#define AT91ETHER_MAX_RBUFF_SZ 0x600
3014/* max number of receive buffers */
3015#define AT91ETHER_MAX_RX_DESCR 9
3016
3017/* Initialize and start the Receiver and Transmit subsystems */
3018static int at91ether_start(struct net_device *dev)
3019{
3020 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003021 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003022 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003023 dma_addr_t addr;
3024 u32 ctl;
3025 int i;
3026
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003027 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003028 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003029 macb_dma_desc_get_size(lp)),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003030 &q->rx_ring_dma, GFP_KERNEL);
3031 if (!q->rx_ring)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003032 return -ENOMEM;
3033
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003034 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003035 AT91ETHER_MAX_RX_DESCR *
3036 AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003037 &q->rx_buffers_dma, GFP_KERNEL);
3038 if (!q->rx_buffers) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003039 dma_free_coherent(&lp->pdev->dev,
3040 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003041 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003042 q->rx_ring, q->rx_ring_dma);
3043 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003044 return -ENOMEM;
3045 }
3046
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003047 addr = q->rx_buffers_dma;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003048 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003049 desc = macb_rx_desc(q, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003050 macb_set_addr(lp, desc, addr);
3051 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003052 addr += AT91ETHER_MAX_RBUFF_SZ;
3053 }
3054
3055 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003056 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003057
3058 /* Reset buffer index */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003059 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003060
3061 /* Program address of descriptor list in Rx Buffer Queue register */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003062 macb_writel(lp, RBQP, q->rx_ring_dma);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003063
3064 /* Enable Receive and Transmit */
3065 ctl = macb_readl(lp, NCR);
3066 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
3067
3068 return 0;
3069}
3070
3071/* Open the ethernet interface */
3072static int at91ether_open(struct net_device *dev)
3073{
3074 struct macb *lp = netdev_priv(dev);
3075 u32 ctl;
3076 int ret;
3077
3078 /* Clear internal statistics */
3079 ctl = macb_readl(lp, NCR);
3080 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
3081
3082 macb_set_hwaddr(lp);
3083
3084 ret = at91ether_start(dev);
3085 if (ret)
3086 return ret;
3087
3088 /* Enable MAC interrupts */
3089 macb_writel(lp, IER, MACB_BIT(RCOMP) |
3090 MACB_BIT(RXUBR) |
3091 MACB_BIT(ISR_TUND) |
3092 MACB_BIT(ISR_RLE) |
3093 MACB_BIT(TCOMP) |
3094 MACB_BIT(ISR_ROVR) |
3095 MACB_BIT(HRESP));
3096
3097 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02003098 phy_start(dev->phydev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003099
3100 netif_start_queue(dev);
3101
3102 return 0;
3103}
3104
3105/* Close the interface */
3106static int at91ether_close(struct net_device *dev)
3107{
3108 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003109 struct macb_queue *q = &lp->queues[0];
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003110 u32 ctl;
3111
3112 /* Disable Receiver and Transmitter */
3113 ctl = macb_readl(lp, NCR);
3114 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
3115
3116 /* Disable MAC interrupts */
3117 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
3118 MACB_BIT(RXUBR) |
3119 MACB_BIT(ISR_TUND) |
3120 MACB_BIT(ISR_RLE) |
3121 MACB_BIT(TCOMP) |
3122 MACB_BIT(ISR_ROVR) |
3123 MACB_BIT(HRESP));
3124
3125 netif_stop_queue(dev);
3126
3127 dma_free_coherent(&lp->pdev->dev,
3128 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003129 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003130 q->rx_ring, q->rx_ring_dma);
3131 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003132
3133 dma_free_coherent(&lp->pdev->dev,
3134 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003135 q->rx_buffers, q->rx_buffers_dma);
3136 q->rx_buffers = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003137
3138 return 0;
3139}
3140
3141/* Transmit packet */
3142static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
3143{
3144 struct macb *lp = netdev_priv(dev);
3145
3146 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
3147 netif_stop_queue(dev);
3148
3149 /* Store packet information (to free when Tx completed) */
3150 lp->skb = skb;
3151 lp->skb_length = skb->len;
3152 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
3153 DMA_TO_DEVICE);
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03003154 if (dma_mapping_error(NULL, lp->skb_physaddr)) {
3155 dev_kfree_skb_any(skb);
3156 dev->stats.tx_dropped++;
3157 netdev_err(dev, "%s: DMA mapping error\n", __func__);
3158 return NETDEV_TX_OK;
3159 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003160
3161 /* Set address of the data in the Transmit Address register */
3162 macb_writel(lp, TAR, lp->skb_physaddr);
3163 /* Set length of the packet in the Transmit Control register */
3164 macb_writel(lp, TCR, skb->len);
3165
3166 } else {
3167 netdev_err(dev, "%s called, but device is busy!\n", __func__);
3168 return NETDEV_TX_BUSY;
3169 }
3170
3171 return NETDEV_TX_OK;
3172}
3173
3174/* Extract received frame from buffer descriptors and sent to upper layers.
3175 * (Called from interrupt context)
3176 */
3177static void at91ether_rx(struct net_device *dev)
3178{
3179 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003180 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003181 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003182 unsigned char *p_recv;
3183 struct sk_buff *skb;
3184 unsigned int pktlen;
3185
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003186 desc = macb_rx_desc(q, q->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003187 while (desc->addr & MACB_BIT(RX_USED)) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003188 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003189 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003190 skb = netdev_alloc_skb(dev, pktlen + 2);
3191 if (skb) {
3192 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02003193 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003194
3195 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003196 dev->stats.rx_packets++;
3197 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003198 netif_rx(skb);
3199 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003200 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003201 }
3202
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003203 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003204 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003205
3206 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003207 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003208
3209 /* wrap after last buffer */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003210 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3211 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003212 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003213 q->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003214
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003215 desc = macb_rx_desc(q, q->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003216 }
3217}
3218
3219/* MAC interrupt handler */
3220static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3221{
3222 struct net_device *dev = dev_id;
3223 struct macb *lp = netdev_priv(dev);
3224 u32 intstatus, ctl;
3225
3226 /* MAC Interrupt Status register indicates what interrupts are pending.
3227 * It is automatically cleared once read.
3228 */
3229 intstatus = macb_readl(lp, ISR);
3230
3231 /* Receive complete */
3232 if (intstatus & MACB_BIT(RCOMP))
3233 at91ether_rx(dev);
3234
3235 /* Transmit complete */
3236 if (intstatus & MACB_BIT(TCOMP)) {
3237 /* The TCOM bit is set even if the transmission failed */
3238 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003239 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003240
3241 if (lp->skb) {
3242 dev_kfree_skb_irq(lp->skb);
3243 lp->skb = NULL;
3244 dma_unmap_single(NULL, lp->skb_physaddr,
3245 lp->skb_length, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003246 dev->stats.tx_packets++;
3247 dev->stats.tx_bytes += lp->skb_length;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003248 }
3249 netif_wake_queue(dev);
3250 }
3251
3252 /* Work-around for EMAC Errata section 41.3.1 */
3253 if (intstatus & MACB_BIT(RXUBR)) {
3254 ctl = macb_readl(lp, NCR);
3255 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08003256 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003257 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3258 }
3259
3260 if (intstatus & MACB_BIT(ISR_ROVR))
3261 netdev_err(dev, "ROVR error\n");
3262
3263 return IRQ_HANDLED;
3264}
3265
3266#ifdef CONFIG_NET_POLL_CONTROLLER
3267static void at91ether_poll_controller(struct net_device *dev)
3268{
3269 unsigned long flags;
3270
3271 local_irq_save(flags);
3272 at91ether_interrupt(dev->irq, dev);
3273 local_irq_restore(flags);
3274}
3275#endif
3276
3277static const struct net_device_ops at91ether_netdev_ops = {
3278 .ndo_open = at91ether_open,
3279 .ndo_stop = at91ether_close,
3280 .ndo_start_xmit = at91ether_start_xmit,
3281 .ndo_get_stats = macb_get_stats,
3282 .ndo_set_rx_mode = macb_set_rx_mode,
3283 .ndo_set_mac_address = eth_mac_addr,
3284 .ndo_do_ioctl = macb_ioctl,
3285 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003286#ifdef CONFIG_NET_POLL_CONTROLLER
3287 .ndo_poll_controller = at91ether_poll_controller,
3288#endif
3289};
3290
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003291static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303292 struct clk **hclk, struct clk **tx_clk,
3293 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003294{
3295 int err;
3296
3297 *hclk = NULL;
3298 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303299 *rx_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003300
3301 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
3302 if (IS_ERR(*pclk))
3303 return PTR_ERR(*pclk);
3304
3305 err = clk_prepare_enable(*pclk);
3306 if (err) {
3307 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3308 return err;
3309 }
3310
3311 return 0;
3312}
3313
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003314static int at91ether_init(struct platform_device *pdev)
3315{
3316 struct net_device *dev = platform_get_drvdata(pdev);
3317 struct macb *bp = netdev_priv(dev);
3318 int err;
3319 u32 reg;
3320
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003321 dev->netdev_ops = &at91ether_netdev_ops;
3322 dev->ethtool_ops = &macb_ethtool_ops;
3323
3324 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3325 0, dev->name, dev);
3326 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003327 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003328
3329 macb_writel(bp, NCR, 0);
3330
3331 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
3332 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
3333 reg |= MACB_BIT(RM9200_RMII);
3334
3335 macb_writel(bp, NCFGR, reg);
3336
3337 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003338}
3339
David S. Miller3cef5c52015-03-09 23:38:02 -04003340static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003341 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003342 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003343 .init = macb_init,
3344};
3345
David S. Miller3cef5c52015-03-09 23:38:02 -04003346static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003347 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
3348 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003349 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003350 .init = macb_init,
3351};
3352
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003353static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003354 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003355 .dma_burst_length = 16,
3356 .clk_init = macb_clk_init,
3357 .init = macb_init,
3358};
3359
David S. Miller3cef5c52015-03-09 23:38:02 -04003360static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003361 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
vishnuvardhan233a1582017-07-05 17:36:16 +02003362 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003363 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003364 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003365 .init = macb_init,
vishnuvardhan233a1582017-07-05 17:36:16 +02003366 .jumbo_max_len = 10240,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003367};
3368
David S. Miller3cef5c52015-03-09 23:38:02 -04003369static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003370 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003371 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003372 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003373 .init = macb_init,
3374};
3375
David S. Miller3cef5c52015-03-09 23:38:02 -04003376static const struct macb_config emac_config = {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003377 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003378 .init = at91ether_init,
3379};
3380
Neil Armstronge611b5b2016-01-05 14:39:17 +01003381static const struct macb_config np4_config = {
3382 .caps = MACB_CAPS_USRIO_DISABLED,
3383 .clk_init = macb_clk_init,
3384 .init = macb_init,
3385};
David S. Miller36583eb2015-05-23 01:22:35 -04003386
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303387static const struct macb_config zynqmp_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003388 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
3389 MACB_CAPS_JUMBO |
3390 MACB_CAPS_GEM_HAS_PTP,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303391 .dma_burst_length = 16,
3392 .clk_init = macb_clk_init,
3393 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303394 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303395};
3396
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003397static const struct macb_config zynq_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05303398 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003399 .dma_burst_length = 16,
3400 .clk_init = macb_clk_init,
3401 .init = macb_init,
3402};
3403
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003404static const struct of_device_id macb_dt_ids[] = {
3405 { .compatible = "cdns,at32ap7000-macb" },
3406 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
3407 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01003408 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003409 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
3410 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003411 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003412 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
3413 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
3414 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
3415 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303416 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003417 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003418 { /* sentinel */ }
3419};
3420MODULE_DEVICE_TABLE(of, macb_dt_ids);
3421#endif /* CONFIG_OF */
3422
Bartosz Folta83a77e92016-12-14 06:39:15 +00003423static const struct macb_config default_gem_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003424 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
3425 MACB_CAPS_JUMBO |
3426 MACB_CAPS_GEM_HAS_PTP,
Bartosz Folta83a77e92016-12-14 06:39:15 +00003427 .dma_burst_length = 16,
3428 .clk_init = macb_clk_init,
3429 .init = macb_init,
3430 .jumbo_max_len = 10240,
3431};
3432
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003433static int macb_probe(struct platform_device *pdev)
3434{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003435 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003436 int (*clk_init)(struct platform_device *, struct clk **,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303437 struct clk **, struct clk **, struct clk **)
Bartosz Folta83a77e92016-12-14 06:39:15 +00003438 = macb_config->clk_init;
3439 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003440 struct device_node *np = pdev->dev.of_node;
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003441 struct device_node *phy_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303442 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003443 unsigned int queue_mask, num_queues;
3444 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003445 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003446 struct phy_device *phydev;
3447 struct net_device *dev;
3448 struct resource *regs;
3449 void __iomem *mem;
3450 const char *mac;
3451 struct macb *bp;
3452 int err;
3453
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003454 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3455 mem = devm_ioremap_resource(&pdev->dev, regs);
3456 if (IS_ERR(mem))
3457 return PTR_ERR(mem);
3458
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003459 if (np) {
3460 const struct of_device_id *match;
3461
3462 match = of_match_node(macb_dt_ids, np);
3463 if (match && match->data) {
3464 macb_config = match->data;
3465 clk_init = macb_config->clk_init;
3466 init = macb_config->init;
3467 }
3468 }
3469
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303470 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003471 if (err)
3472 return err;
3473
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003474 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003475
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003476 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003477 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003478 if (!dev) {
3479 err = -ENOMEM;
3480 goto err_disable_clocks;
3481 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003482
3483 dev->base_addr = regs->start;
3484
3485 SET_NETDEV_DEV(dev, &pdev->dev);
3486
3487 bp = netdev_priv(dev);
3488 bp->pdev = pdev;
3489 bp->dev = dev;
3490 bp->regs = mem;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003491 bp->native_io = native_io;
3492 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07003493 bp->macb_reg_readl = hw_readl_native;
3494 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003495 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07003496 bp->macb_reg_readl = hw_readl;
3497 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003498 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003499 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003500 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003501 if (macb_config)
3502 bp->dma_burst_length = macb_config->dma_burst_length;
3503 bp->pclk = pclk;
3504 bp->hclk = hclk;
3505 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303506 bp->rx_clk = rx_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03003507 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303508 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303509
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003510 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02003511 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003512 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
3513 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
3514
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003515 spin_lock_init(&bp->lock);
3516
Nicolas Ferread783472015-03-31 15:02:02 +02003517 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02003518 macb_configure_caps(bp, macb_config);
3519
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003520#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3521 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
3522 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
3523 bp->hw_dma_cap |= HW_DMA_CAP_64B;
3524 }
3525#endif
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003526 platform_set_drvdata(pdev, dev);
3527
3528 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003529 if (dev->irq < 0) {
3530 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00003531 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003532 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003533
Jarod Wilson44770e12016-10-17 15:54:17 -04003534 /* MTU range: 68 - 1500 or 10240 */
3535 dev->min_mtu = GEM_MTU_MIN_SIZE;
3536 if (bp->caps & MACB_CAPS_JUMBO)
3537 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
3538 else
3539 dev->max_mtu = ETH_DATA_LEN;
3540
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003541 mac = of_get_mac_address(np);
Guenter Roeck50907042013-04-02 09:35:09 +00003542 if (mac)
Moritz Fischereefb52d2016-03-29 19:11:14 -07003543 ether_addr_copy(bp->dev->dev_addr, mac);
Guenter Roeck50907042013-04-02 09:35:09 +00003544 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003545 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02003546
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003547 /* Power up the PHY if there is a GPIO reset */
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003548 phy_node = of_get_next_available_child(np, NULL);
3549 if (phy_node) {
3550 int gpio = of_get_named_gpio(phy_node, "reset-gpios", 0);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003551
Charles Keepax0e3e7992016-03-28 13:47:42 +01003552 if (gpio_is_valid(gpio)) {
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003553 bp->reset_gpio = gpio_to_desc(gpio);
Charles Keepax0e3e7992016-03-28 13:47:42 +01003554 gpiod_direction_output(bp->reset_gpio, 1);
3555 }
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003556 }
3557 of_node_put(phy_node);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003558
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003559 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003560 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09003561 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003562 if (pdata && pdata->is_rmii)
3563 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
3564 else
3565 bp->phy_interface = PHY_INTERFACE_MODE_MII;
3566 } else {
3567 bp->phy_interface = err;
3568 }
3569
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003570 /* IP specific init */
3571 err = init(pdev);
3572 if (err)
3573 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003574
Florian Fainellicf669662016-05-02 18:38:45 -07003575 err = macb_mii_init(bp);
3576 if (err)
3577 goto err_out_free_netdev;
3578
Philippe Reynes0a912812016-06-22 00:32:35 +02003579 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07003580
3581 netif_carrier_off(dev);
3582
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003583 err = register_netdev(dev);
3584 if (err) {
3585 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07003586 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003587 }
3588
Florian Fainellicf669662016-05-02 18:38:45 -07003589 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003590
Bo Shen58798232014-09-13 01:57:49 +02003591 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
3592 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
3593 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003594
3595 return 0;
3596
Florian Fainellicf669662016-05-02 18:38:45 -07003597err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02003598 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07003599 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik66ee6a02017-11-08 09:56:35 +01003600 of_node_put(bp->phy_node);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01003601 if (np && of_phy_is_fixed_link(np))
3602 of_phy_deregister_fixed_link(np);
Florian Fainellicf669662016-05-02 18:38:45 -07003603 mdiobus_free(bp->mii_bus);
3604
3605 /* Shutdown the PHY if there is a GPIO reset */
3606 if (bp->reset_gpio)
3607 gpiod_set_value(bp->reset_gpio, 0);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003608
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003609err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003610 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003611
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003612err_disable_clocks:
3613 clk_disable_unprepare(tx_clk);
3614 clk_disable_unprepare(hclk);
3615 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303616 clk_disable_unprepare(rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003617
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003618 return err;
3619}
3620
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003621static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003622{
3623 struct net_device *dev;
3624 struct macb *bp;
Michael Grzeschik9ce98142017-11-08 09:56:34 +01003625 struct device_node *np = pdev->dev.of_node;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003626
3627 dev = platform_get_drvdata(pdev);
3628
3629 if (dev) {
3630 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02003631 if (dev->phydev)
3632 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003633 mdiobus_unregister(bp->mii_bus);
Michael Grzeschik9ce98142017-11-08 09:56:34 +01003634 if (np && of_phy_is_fixed_link(np))
3635 of_phy_deregister_fixed_link(np);
Nathan Sullivanfa6114d2016-10-07 10:13:22 -05003636 dev->phydev = NULL;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003637 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003638
3639 /* Shutdown the PHY if there is a GPIO reset */
Charles Keepax0e3e7992016-03-28 13:47:42 +01003640 if (bp->reset_gpio)
3641 gpiod_set_value(bp->reset_gpio, 0);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003642
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003643 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01003644 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003645 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003646 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303647 clk_disable_unprepare(bp->rx_clk);
Michael Grzeschikdacdbb42017-06-23 16:54:10 +02003648 of_node_put(bp->phy_node);
Cyrille Pitchene965be72014-12-15 15:13:31 +01003649 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003650 }
3651
3652 return 0;
3653}
3654
Michal Simekd23823d2015-01-23 09:36:03 +01003655static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003656{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003657 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003658 struct net_device *netdev = platform_get_drvdata(pdev);
3659 struct macb *bp = netdev_priv(netdev);
3660
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003661 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003662 netif_device_detach(netdev);
3663
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003664 if (bp->wol & MACB_WOL_ENABLED) {
3665 macb_writel(bp, IER, MACB_BIT(WOL));
3666 macb_writel(bp, WOL, MACB_BIT(MAG));
3667 enable_irq_wake(bp->queues[0].irq);
3668 } else {
3669 clk_disable_unprepare(bp->tx_clk);
3670 clk_disable_unprepare(bp->hclk);
3671 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303672 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003673 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003674
3675 return 0;
3676}
3677
Michal Simekd23823d2015-01-23 09:36:03 +01003678static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003679{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003680 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003681 struct net_device *netdev = platform_get_drvdata(pdev);
3682 struct macb *bp = netdev_priv(netdev);
3683
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003684 if (bp->wol & MACB_WOL_ENABLED) {
3685 macb_writel(bp, IDR, MACB_BIT(WOL));
3686 macb_writel(bp, WOL, 0);
3687 disable_irq_wake(bp->queues[0].irq);
3688 } else {
3689 clk_prepare_enable(bp->pclk);
3690 clk_prepare_enable(bp->hclk);
3691 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303692 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003693 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003694
3695 netif_device_attach(netdev);
3696
3697 return 0;
3698}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003699
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003700static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
3701
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003702static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003703 .probe = macb_probe,
3704 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003705 .driver = {
3706 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003707 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003708 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003709 },
3710};
3711
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003712module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003713
3714MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00003715MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02003716MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07003717MODULE_ALIAS("platform:macb");