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