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