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