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