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