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