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> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/init.h> |
Alexey Dobriyan | a6b7a40 | 2011-06-06 10:43:46 +0000 | [diff] [blame] | 19 | #include <linux/interrupt.h> |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 20 | #include <linux/netdevice.h> |
| 21 | #include <linux/etherdevice.h> |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 22 | #include <linux/dma-mapping.h> |
Jamie Iles | 84e0cdb | 2011-03-08 20:17:06 +0000 | [diff] [blame] | 23 | #include <linux/platform_data/macb.h> |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 24 | #include <linux/platform_device.h> |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 25 | #include <linux/phy.h> |
Olof Johansson | b17471f | 2011-12-20 13:13:07 -0800 | [diff] [blame] | 26 | #include <linux/of.h> |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 27 | #include <linux/of_device.h> |
| 28 | #include <linux/of_net.h> |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 29 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 30 | #include "macb.h" |
| 31 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 32 | #define RX_BUFFER_SIZE 128 |
| 33 | #define RX_RING_SIZE 512 |
| 34 | #define RX_RING_BYTES (sizeof(struct dma_desc) * RX_RING_SIZE) |
| 35 | |
| 36 | /* Make the IP header word-aligned (the ethernet header is 14 bytes) */ |
| 37 | #define RX_OFFSET 2 |
| 38 | |
| 39 | #define TX_RING_SIZE 128 |
| 40 | #define DEF_TX_RING_PENDING (TX_RING_SIZE - 1) |
| 41 | #define TX_RING_BYTES (sizeof(struct dma_desc) * TX_RING_SIZE) |
| 42 | |
| 43 | #define TX_RING_GAP(bp) \ |
| 44 | (TX_RING_SIZE - (bp)->tx_pending) |
| 45 | #define TX_BUFFS_AVAIL(bp) \ |
| 46 | (((bp)->tx_tail <= (bp)->tx_head) ? \ |
| 47 | (bp)->tx_tail + (bp)->tx_pending - (bp)->tx_head : \ |
| 48 | (bp)->tx_tail - (bp)->tx_head - TX_RING_GAP(bp)) |
| 49 | #define NEXT_TX(n) (((n) + 1) & (TX_RING_SIZE - 1)) |
| 50 | |
| 51 | #define NEXT_RX(n) (((n) + 1) & (RX_RING_SIZE - 1)) |
| 52 | |
| 53 | /* minimum number of free TX descriptors before waking up TX process */ |
| 54 | #define MACB_TX_WAKEUP_THRESH (TX_RING_SIZE / 4) |
| 55 | |
| 56 | #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \ |
| 57 | | MACB_BIT(ISR_ROVR)) |
| 58 | |
| 59 | static void __macb_set_hwaddr(struct macb *bp) |
| 60 | { |
| 61 | u32 bottom; |
| 62 | u16 top; |
| 63 | |
| 64 | bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr)); |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 65 | macb_or_gem_writel(bp, SA1B, bottom); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 66 | top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4))); |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 67 | macb_or_gem_writel(bp, SA1T, top); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static void __init macb_get_hwaddr(struct macb *bp) |
| 71 | { |
| 72 | u32 bottom; |
| 73 | u16 top; |
| 74 | u8 addr[6]; |
| 75 | |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 76 | bottom = macb_or_gem_readl(bp, SA1B); |
| 77 | top = macb_or_gem_readl(bp, SA1T); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 78 | |
| 79 | addr[0] = bottom & 0xff; |
| 80 | addr[1] = (bottom >> 8) & 0xff; |
| 81 | addr[2] = (bottom >> 16) & 0xff; |
| 82 | addr[3] = (bottom >> 24) & 0xff; |
| 83 | addr[4] = top & 0xff; |
| 84 | addr[5] = (top >> 8) & 0xff; |
| 85 | |
Sven Schnelle | d1d5741 | 2008-06-09 16:33:57 -0700 | [diff] [blame] | 86 | if (is_valid_ether_addr(addr)) { |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 87 | memcpy(bp->dev->dev_addr, addr, sizeof(addr)); |
Sven Schnelle | d1d5741 | 2008-06-09 16:33:57 -0700 | [diff] [blame] | 88 | } else { |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 89 | netdev_info(bp->dev, "invalid hw address, using random\n"); |
Danny Kukawka | f2cedb6 | 2012-02-15 06:45:39 +0000 | [diff] [blame] | 90 | eth_hw_addr_random(bp->dev); |
Sven Schnelle | d1d5741 | 2008-06-09 16:33:57 -0700 | [diff] [blame] | 91 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 92 | } |
| 93 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 94 | 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] | 95 | { |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 96 | struct macb *bp = bus->priv; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 97 | int value; |
| 98 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 99 | macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF) |
| 100 | | MACB_BF(RW, MACB_MAN_READ) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 101 | | MACB_BF(PHYA, mii_id) |
| 102 | | MACB_BF(REGA, regnum) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 103 | | MACB_BF(CODE, MACB_MAN_CODE))); |
| 104 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 105 | /* wait for end of transfer */ |
| 106 | while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR))) |
| 107 | cpu_relax(); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 108 | |
| 109 | value = MACB_BFEXT(DATA, macb_readl(bp, MAN)); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 110 | |
| 111 | return value; |
| 112 | } |
| 113 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 114 | static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum, |
| 115 | u16 value) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 116 | { |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 117 | struct macb *bp = bus->priv; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 118 | |
| 119 | macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF) |
| 120 | | MACB_BF(RW, MACB_MAN_WRITE) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 121 | | MACB_BF(PHYA, mii_id) |
| 122 | | MACB_BF(REGA, regnum) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 123 | | MACB_BF(CODE, MACB_MAN_CODE) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 124 | | MACB_BF(DATA, value))); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 125 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 126 | /* wait for end of transfer */ |
| 127 | while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR))) |
| 128 | cpu_relax(); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 129 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 133 | static int macb_mdio_reset(struct mii_bus *bus) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 134 | { |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 135 | return 0; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 136 | } |
| 137 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 138 | static void macb_handle_link_change(struct net_device *dev) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 139 | { |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 140 | struct macb *bp = netdev_priv(dev); |
| 141 | struct phy_device *phydev = bp->phy_dev; |
| 142 | unsigned long flags; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 143 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 144 | int status_change = 0; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 145 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 146 | spin_lock_irqsave(&bp->lock, flags); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 147 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 148 | if (phydev->link) { |
| 149 | if ((bp->speed != phydev->speed) || |
| 150 | (bp->duplex != phydev->duplex)) { |
| 151 | u32 reg; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 152 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 153 | reg = macb_readl(bp, NCFGR); |
| 154 | reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD)); |
Patrice Vilchez | 140b755 | 2012-10-31 06:04:50 +0000 | [diff] [blame^] | 155 | if (macb_is_gem(bp)) |
| 156 | reg &= ~GEM_BIT(GBE); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 157 | |
| 158 | if (phydev->duplex) |
| 159 | reg |= MACB_BIT(FD); |
Atsushi Nemoto | 179956f | 2008-02-21 22:50:54 +0900 | [diff] [blame] | 160 | if (phydev->speed == SPEED_100) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 161 | reg |= MACB_BIT(SPD); |
Patrice Vilchez | 140b755 | 2012-10-31 06:04:50 +0000 | [diff] [blame^] | 162 | if (phydev->speed == SPEED_1000) |
| 163 | reg |= GEM_BIT(GBE); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 164 | |
Patrice Vilchez | 140b755 | 2012-10-31 06:04:50 +0000 | [diff] [blame^] | 165 | macb_or_gem_writel(bp, NCFGR, reg); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 166 | |
| 167 | bp->speed = phydev->speed; |
| 168 | bp->duplex = phydev->duplex; |
| 169 | status_change = 1; |
| 170 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 171 | } |
| 172 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 173 | if (phydev->link != bp->link) { |
Anton Vorontsov | c8f1568 | 2008-07-22 15:41:24 -0700 | [diff] [blame] | 174 | if (!phydev->link) { |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 175 | bp->speed = 0; |
| 176 | bp->duplex = -1; |
| 177 | } |
| 178 | bp->link = phydev->link; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 179 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 180 | status_change = 1; |
| 181 | } |
| 182 | |
| 183 | spin_unlock_irqrestore(&bp->lock, flags); |
| 184 | |
| 185 | if (status_change) { |
Nicolas Ferre | 03fc472 | 2012-07-03 23:14:13 +0000 | [diff] [blame] | 186 | if (phydev->link) { |
| 187 | netif_carrier_on(dev); |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 188 | netdev_info(dev, "link up (%d/%s)\n", |
| 189 | phydev->speed, |
| 190 | phydev->duplex == DUPLEX_FULL ? |
| 191 | "Full" : "Half"); |
Nicolas Ferre | 03fc472 | 2012-07-03 23:14:13 +0000 | [diff] [blame] | 192 | } else { |
| 193 | netif_carrier_off(dev); |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 194 | netdev_info(dev, "link down\n"); |
Nicolas Ferre | 03fc472 | 2012-07-03 23:14:13 +0000 | [diff] [blame] | 195 | } |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | /* based on au1000_eth. c*/ |
| 200 | static int macb_mii_probe(struct net_device *dev) |
| 201 | { |
| 202 | struct macb *bp = netdev_priv(dev); |
Jiri Pirko | 7455a76 | 2010-02-08 05:12:08 +0000 | [diff] [blame] | 203 | struct phy_device *phydev; |
Jiri Pirko | 7455a76 | 2010-02-08 05:12:08 +0000 | [diff] [blame] | 204 | int ret; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 205 | |
Jiri Pirko | 7455a76 | 2010-02-08 05:12:08 +0000 | [diff] [blame] | 206 | phydev = phy_find_first(bp->mii_bus); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 207 | if (!phydev) { |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 208 | netdev_err(dev, "no PHY found\n"); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 209 | return -1; |
| 210 | } |
| 211 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 212 | /* TODO : add pin_irq */ |
| 213 | |
| 214 | /* attach the mac to the phy */ |
Jiri Pirko | 7455a76 | 2010-02-08 05:12:08 +0000 | [diff] [blame] | 215 | ret = phy_connect_direct(dev, phydev, &macb_handle_link_change, 0, |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 216 | bp->phy_interface); |
Jiri Pirko | 7455a76 | 2010-02-08 05:12:08 +0000 | [diff] [blame] | 217 | if (ret) { |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 218 | netdev_err(dev, "Could not attach to PHY\n"); |
Jiri Pirko | 7455a76 | 2010-02-08 05:12:08 +0000 | [diff] [blame] | 219 | return ret; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 220 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 221 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 222 | /* mask with MAC supported features */ |
Patrice Vilchez | 140b755 | 2012-10-31 06:04:50 +0000 | [diff] [blame^] | 223 | if (macb_is_gem(bp)) |
| 224 | phydev->supported &= PHY_GBIT_FEATURES; |
| 225 | else |
| 226 | phydev->supported &= PHY_BASIC_FEATURES; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 227 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 228 | phydev->advertising = phydev->supported; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 229 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 230 | bp->link = 0; |
| 231 | bp->speed = 0; |
| 232 | bp->duplex = -1; |
| 233 | bp->phy_dev = phydev; |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
Joachim Eastwood | 0005f54 | 2012-10-18 11:01:12 +0000 | [diff] [blame] | 238 | int macb_mii_init(struct macb *bp) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 239 | { |
Jamie Iles | 84e0cdb | 2011-03-08 20:17:06 +0000 | [diff] [blame] | 240 | struct macb_platform_data *pdata; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 241 | int err = -ENXIO, i; |
| 242 | |
Uwe Kleine-Koenig | 3dbda77 | 2009-07-23 08:31:31 +0200 | [diff] [blame] | 243 | /* Enable management port */ |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 244 | macb_writel(bp, NCR, MACB_BIT(MPE)); |
| 245 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 246 | bp->mii_bus = mdiobus_alloc(); |
| 247 | if (bp->mii_bus == NULL) { |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 248 | err = -ENOMEM; |
| 249 | goto err_out; |
| 250 | } |
| 251 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 252 | bp->mii_bus->name = "MACB_mii_bus"; |
| 253 | bp->mii_bus->read = &macb_mdio_read; |
| 254 | bp->mii_bus->write = &macb_mdio_write; |
| 255 | bp->mii_bus->reset = &macb_mdio_reset; |
Florian Fainelli | 98d5e57 | 2012-01-09 23:59:11 +0000 | [diff] [blame] | 256 | snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x", |
| 257 | bp->pdev->name, bp->pdev->id); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 258 | bp->mii_bus->priv = bp; |
| 259 | bp->mii_bus->parent = &bp->dev->dev; |
| 260 | pdata = bp->pdev->dev.platform_data; |
| 261 | |
| 262 | if (pdata) |
| 263 | bp->mii_bus->phy_mask = pdata->phy_mask; |
| 264 | |
| 265 | bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL); |
| 266 | if (!bp->mii_bus->irq) { |
| 267 | err = -ENOMEM; |
| 268 | goto err_out_free_mdiobus; |
| 269 | } |
| 270 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 271 | for (i = 0; i < PHY_MAX_ADDR; i++) |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 272 | bp->mii_bus->irq[i] = PHY_POLL; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 273 | |
Jamie Iles | 9152394 | 2011-02-28 04:05:25 +0000 | [diff] [blame] | 274 | dev_set_drvdata(&bp->dev->dev, bp->mii_bus); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 275 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 276 | if (mdiobus_register(bp->mii_bus)) |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 277 | goto err_out_free_mdio_irq; |
| 278 | |
| 279 | if (macb_mii_probe(bp->dev) != 0) { |
| 280 | goto err_out_unregister_bus; |
| 281 | } |
| 282 | |
| 283 | return 0; |
| 284 | |
| 285 | err_out_unregister_bus: |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 286 | mdiobus_unregister(bp->mii_bus); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 287 | err_out_free_mdio_irq: |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 288 | kfree(bp->mii_bus->irq); |
| 289 | err_out_free_mdiobus: |
| 290 | mdiobus_free(bp->mii_bus); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 291 | err_out: |
| 292 | return err; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 293 | } |
Joachim Eastwood | 0005f54 | 2012-10-18 11:01:12 +0000 | [diff] [blame] | 294 | EXPORT_SYMBOL_GPL(macb_mii_init); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 295 | |
| 296 | static void macb_update_stats(struct macb *bp) |
| 297 | { |
| 298 | u32 __iomem *reg = bp->regs + MACB_PFR; |
Jamie Iles | a494ed8 | 2011-03-09 16:26:35 +0000 | [diff] [blame] | 299 | u32 *p = &bp->hw_stats.macb.rx_pause_frames; |
| 300 | u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 301 | |
| 302 | WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4); |
| 303 | |
| 304 | for(; p < end; p++, reg++) |
Haavard Skinnemoen | 0f0d84e | 2006-12-08 14:38:30 +0100 | [diff] [blame] | 305 | *p += __raw_readl(reg); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 306 | } |
| 307 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 308 | static void macb_tx(struct macb *bp) |
| 309 | { |
| 310 | unsigned int tail; |
| 311 | unsigned int head; |
| 312 | u32 status; |
| 313 | |
| 314 | status = macb_readl(bp, TSR); |
| 315 | macb_writel(bp, TSR, status); |
| 316 | |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 317 | netdev_dbg(bp->dev, "macb_tx status = %02lx\n", (unsigned long)status); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 318 | |
Erik Waling | ee33c58 | 2009-04-15 23:32:10 +0000 | [diff] [blame] | 319 | if (status & (MACB_BIT(UND) | MACB_BIT(TSR_RLE))) { |
Gregory CLEMENT | bdcba151 | 2007-12-19 18:23:44 +0100 | [diff] [blame] | 320 | int i; |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 321 | netdev_err(bp->dev, "TX %s, resetting buffers\n", |
| 322 | status & MACB_BIT(UND) ? |
| 323 | "underrun" : "retry limit exceeded"); |
Gregory CLEMENT | bdcba151 | 2007-12-19 18:23:44 +0100 | [diff] [blame] | 324 | |
Richard Röjfors | 39eddb4 | 2009-01-18 21:57:35 -0800 | [diff] [blame] | 325 | /* Transfer ongoing, disable transmitter, to avoid confusion */ |
| 326 | if (status & MACB_BIT(TGO)) |
| 327 | macb_writel(bp, NCR, macb_readl(bp, NCR) & ~MACB_BIT(TE)); |
| 328 | |
Gregory CLEMENT | bdcba151 | 2007-12-19 18:23:44 +0100 | [diff] [blame] | 329 | head = bp->tx_head; |
| 330 | |
| 331 | /*Mark all the buffer as used to avoid sending a lost buffer*/ |
| 332 | for (i = 0; i < TX_RING_SIZE; i++) |
| 333 | bp->tx_ring[i].ctrl = MACB_BIT(TX_USED); |
| 334 | |
Tord Andersson | d3e6145 | 2011-08-03 22:11:47 +0000 | [diff] [blame] | 335 | /* Add wrap bit */ |
| 336 | bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP); |
| 337 | |
Gregory CLEMENT | bdcba151 | 2007-12-19 18:23:44 +0100 | [diff] [blame] | 338 | /* free transmit buffer in upper layer*/ |
| 339 | for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) { |
| 340 | struct ring_info *rp = &bp->tx_skb[tail]; |
| 341 | struct sk_buff *skb = rp->skb; |
| 342 | |
| 343 | BUG_ON(skb == NULL); |
| 344 | |
| 345 | rmb(); |
| 346 | |
| 347 | dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len, |
| 348 | DMA_TO_DEVICE); |
| 349 | rp->skb = NULL; |
| 350 | dev_kfree_skb_irq(skb); |
| 351 | } |
| 352 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 353 | bp->tx_head = bp->tx_tail = 0; |
Richard Röjfors | 39eddb4 | 2009-01-18 21:57:35 -0800 | [diff] [blame] | 354 | |
| 355 | /* Enable the transmitter again */ |
| 356 | if (status & MACB_BIT(TGO)) |
| 357 | macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TE)); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | if (!(status & MACB_BIT(COMP))) |
| 361 | /* |
| 362 | * This may happen when a buffer becomes complete |
| 363 | * between reading the ISR and scanning the |
| 364 | * descriptors. Nothing to worry about. |
| 365 | */ |
| 366 | return; |
| 367 | |
| 368 | head = bp->tx_head; |
| 369 | for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) { |
| 370 | struct ring_info *rp = &bp->tx_skb[tail]; |
| 371 | struct sk_buff *skb = rp->skb; |
| 372 | u32 bufstat; |
| 373 | |
| 374 | BUG_ON(skb == NULL); |
| 375 | |
| 376 | rmb(); |
| 377 | bufstat = bp->tx_ring[tail].ctrl; |
| 378 | |
| 379 | if (!(bufstat & MACB_BIT(TX_USED))) |
| 380 | break; |
| 381 | |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 382 | netdev_dbg(bp->dev, "skb %u (data %p) TX complete\n", |
| 383 | tail, skb->data); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 384 | dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len, |
| 385 | DMA_TO_DEVICE); |
| 386 | bp->stats.tx_packets++; |
| 387 | bp->stats.tx_bytes += skb->len; |
| 388 | rp->skb = NULL; |
| 389 | dev_kfree_skb_irq(skb); |
| 390 | } |
| 391 | |
| 392 | bp->tx_tail = tail; |
| 393 | if (netif_queue_stopped(bp->dev) && |
| 394 | TX_BUFFS_AVAIL(bp) > MACB_TX_WAKEUP_THRESH) |
| 395 | netif_wake_queue(bp->dev); |
| 396 | } |
| 397 | |
| 398 | static int macb_rx_frame(struct macb *bp, unsigned int first_frag, |
| 399 | unsigned int last_frag) |
| 400 | { |
| 401 | unsigned int len; |
| 402 | unsigned int frag; |
| 403 | unsigned int offset = 0; |
| 404 | struct sk_buff *skb; |
| 405 | |
| 406 | len = MACB_BFEXT(RX_FRMLEN, bp->rx_ring[last_frag].ctrl); |
| 407 | |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 408 | netdev_dbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n", |
| 409 | first_frag, last_frag, len); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 410 | |
Pradeep A Dalvi | 21a4e46 | 2012-02-05 02:50:10 +0000 | [diff] [blame] | 411 | skb = netdev_alloc_skb(bp->dev, len + RX_OFFSET); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 412 | if (!skb) { |
| 413 | bp->stats.rx_dropped++; |
| 414 | for (frag = first_frag; ; frag = NEXT_RX(frag)) { |
| 415 | bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED); |
| 416 | if (frag == last_frag) |
| 417 | break; |
| 418 | } |
| 419 | wmb(); |
| 420 | return 1; |
| 421 | } |
| 422 | |
| 423 | skb_reserve(skb, RX_OFFSET); |
Eric Dumazet | bc8acf2 | 2010-09-02 13:07:41 -0700 | [diff] [blame] | 424 | skb_checksum_none_assert(skb); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 425 | skb_put(skb, len); |
| 426 | |
| 427 | for (frag = first_frag; ; frag = NEXT_RX(frag)) { |
| 428 | unsigned int frag_len = RX_BUFFER_SIZE; |
| 429 | |
| 430 | if (offset + frag_len > len) { |
| 431 | BUG_ON(frag != last_frag); |
| 432 | frag_len = len - offset; |
| 433 | } |
Arnaldo Carvalho de Melo | 27d7ff4 | 2007-03-31 11:55:19 -0300 | [diff] [blame] | 434 | skb_copy_to_linear_data_offset(skb, offset, |
| 435 | (bp->rx_buffers + |
| 436 | (RX_BUFFER_SIZE * frag)), |
| 437 | frag_len); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 438 | offset += RX_BUFFER_SIZE; |
| 439 | bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED); |
| 440 | wmb(); |
| 441 | |
| 442 | if (frag == last_frag) |
| 443 | break; |
| 444 | } |
| 445 | |
| 446 | skb->protocol = eth_type_trans(skb, bp->dev); |
| 447 | |
| 448 | bp->stats.rx_packets++; |
| 449 | bp->stats.rx_bytes += len; |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 450 | netdev_dbg(bp->dev, "received skb of length %u, csum: %08x\n", |
| 451 | skb->len, skb->csum); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 452 | netif_receive_skb(skb); |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | /* Mark DMA descriptors from begin up to and not including end as unused */ |
| 458 | static void discard_partial_frame(struct macb *bp, unsigned int begin, |
| 459 | unsigned int end) |
| 460 | { |
| 461 | unsigned int frag; |
| 462 | |
| 463 | for (frag = begin; frag != end; frag = NEXT_RX(frag)) |
| 464 | bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED); |
| 465 | wmb(); |
| 466 | |
| 467 | /* |
| 468 | * When this happens, the hardware stats registers for |
| 469 | * whatever caused this is updated, so we don't have to record |
| 470 | * anything. |
| 471 | */ |
| 472 | } |
| 473 | |
| 474 | static int macb_rx(struct macb *bp, int budget) |
| 475 | { |
| 476 | int received = 0; |
| 477 | unsigned int tail = bp->rx_tail; |
| 478 | int first_frag = -1; |
| 479 | |
| 480 | for (; budget > 0; tail = NEXT_RX(tail)) { |
| 481 | u32 addr, ctrl; |
| 482 | |
| 483 | rmb(); |
| 484 | addr = bp->rx_ring[tail].addr; |
| 485 | ctrl = bp->rx_ring[tail].ctrl; |
| 486 | |
| 487 | if (!(addr & MACB_BIT(RX_USED))) |
| 488 | break; |
| 489 | |
| 490 | if (ctrl & MACB_BIT(RX_SOF)) { |
| 491 | if (first_frag != -1) |
| 492 | discard_partial_frame(bp, first_frag, tail); |
| 493 | first_frag = tail; |
| 494 | } |
| 495 | |
| 496 | if (ctrl & MACB_BIT(RX_EOF)) { |
| 497 | int dropped; |
| 498 | BUG_ON(first_frag == -1); |
| 499 | |
| 500 | dropped = macb_rx_frame(bp, first_frag, tail); |
| 501 | first_frag = -1; |
| 502 | if (!dropped) { |
| 503 | received++; |
| 504 | budget--; |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | if (first_frag != -1) |
| 510 | bp->rx_tail = first_frag; |
| 511 | else |
| 512 | bp->rx_tail = tail; |
| 513 | |
| 514 | return received; |
| 515 | } |
| 516 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 517 | static int macb_poll(struct napi_struct *napi, int budget) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 518 | { |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 519 | struct macb *bp = container_of(napi, struct macb, napi); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 520 | int work_done; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 521 | u32 status; |
| 522 | |
| 523 | status = macb_readl(bp, RSR); |
| 524 | macb_writel(bp, RSR, status); |
| 525 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 526 | work_done = 0; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 527 | |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 528 | netdev_dbg(bp->dev, "poll: status = %08lx, budget = %d\n", |
| 529 | (unsigned long)status, budget); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 530 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 531 | work_done = macb_rx(bp, budget); |
Joshua Hoke | b336369 | 2010-10-25 01:44:22 +0000 | [diff] [blame] | 532 | if (work_done < budget) { |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 533 | napi_complete(napi); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 534 | |
Joshua Hoke | b336369 | 2010-10-25 01:44:22 +0000 | [diff] [blame] | 535 | /* |
| 536 | * We've done what we can to clean the buffers. Make sure we |
| 537 | * get notified when new packets arrive. |
| 538 | */ |
| 539 | macb_writel(bp, IER, MACB_RX_INT_FLAGS); |
| 540 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 541 | |
| 542 | /* TODO: Handle errors */ |
| 543 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 544 | return work_done; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | static irqreturn_t macb_interrupt(int irq, void *dev_id) |
| 548 | { |
| 549 | struct net_device *dev = dev_id; |
| 550 | struct macb *bp = netdev_priv(dev); |
| 551 | u32 status; |
| 552 | |
| 553 | status = macb_readl(bp, ISR); |
| 554 | |
| 555 | if (unlikely(!status)) |
| 556 | return IRQ_NONE; |
| 557 | |
| 558 | spin_lock(&bp->lock); |
| 559 | |
| 560 | while (status) { |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 561 | /* close possible race with dev_close */ |
| 562 | if (unlikely(!netif_running(dev))) { |
Joachim Eastwood | 95ebcea | 2012-10-22 08:45:31 +0000 | [diff] [blame] | 563 | macb_writel(bp, IDR, -1); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 564 | break; |
| 565 | } |
| 566 | |
| 567 | if (status & MACB_RX_INT_FLAGS) { |
Joshua Hoke | b336369 | 2010-10-25 01:44:22 +0000 | [diff] [blame] | 568 | /* |
| 569 | * There's no point taking any more interrupts |
| 570 | * until we have processed the buffers. The |
| 571 | * scheduling call may fail if the poll routine |
| 572 | * is already scheduled, so disable interrupts |
| 573 | * now. |
| 574 | */ |
| 575 | macb_writel(bp, IDR, MACB_RX_INT_FLAGS); |
| 576 | |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 577 | if (napi_schedule_prep(&bp->napi)) { |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 578 | netdev_dbg(bp->dev, "scheduling RX softirq\n"); |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 579 | __napi_schedule(&bp->napi); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
Erik Waling | ee33c58 | 2009-04-15 23:32:10 +0000 | [diff] [blame] | 583 | if (status & (MACB_BIT(TCOMP) | MACB_BIT(ISR_TUND) | |
| 584 | MACB_BIT(ISR_RLE))) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 585 | macb_tx(bp); |
| 586 | |
| 587 | /* |
| 588 | * Link change detection isn't possible with RMII, so we'll |
| 589 | * add that if/when we get our hands on a full-blown MII PHY. |
| 590 | */ |
| 591 | |
Alexander Stein | b19f7f7 | 2011-04-13 05:03:24 +0000 | [diff] [blame] | 592 | if (status & MACB_BIT(ISR_ROVR)) { |
| 593 | /* We missed at least one packet */ |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 594 | if (macb_is_gem(bp)) |
| 595 | bp->hw_stats.gem.rx_overruns++; |
| 596 | else |
| 597 | bp->hw_stats.macb.rx_overruns++; |
Alexander Stein | b19f7f7 | 2011-04-13 05:03:24 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 600 | if (status & MACB_BIT(HRESP)) { |
| 601 | /* |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 602 | * TODO: Reset the hardware, and maybe move the |
| 603 | * netdev_err to a lower-priority context as well |
| 604 | * (work queue?) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 605 | */ |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 606 | netdev_err(dev, "DMA bus error: HRESP not OK\n"); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | status = macb_readl(bp, ISR); |
| 610 | } |
| 611 | |
| 612 | spin_unlock(&bp->lock); |
| 613 | |
| 614 | return IRQ_HANDLED; |
| 615 | } |
| 616 | |
Thomas Petazzoni | 6e8cf5c | 2009-05-04 11:08:41 -0700 | [diff] [blame] | 617 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 618 | /* |
| 619 | * Polling receive - used by netconsole and other diagnostic tools |
| 620 | * to allow network i/o with interrupts disabled. |
| 621 | */ |
| 622 | static void macb_poll_controller(struct net_device *dev) |
| 623 | { |
| 624 | unsigned long flags; |
| 625 | |
| 626 | local_irq_save(flags); |
| 627 | macb_interrupt(dev->irq, dev); |
| 628 | local_irq_restore(flags); |
| 629 | } |
| 630 | #endif |
| 631 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 632 | static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 633 | { |
| 634 | struct macb *bp = netdev_priv(dev); |
| 635 | dma_addr_t mapping; |
| 636 | unsigned int len, entry; |
| 637 | u32 ctrl; |
Dongdong Deng | 4871953 | 2009-08-23 19:49:07 -0700 | [diff] [blame] | 638 | unsigned long flags; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 639 | |
| 640 | #ifdef DEBUG |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 641 | netdev_dbg(bp->dev, |
| 642 | "start_xmit: len %u head %p data %p tail %p end %p\n", |
| 643 | skb->len, skb->head, skb->data, |
| 644 | skb_tail_pointer(skb), skb_end_pointer(skb)); |
| 645 | print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1, |
| 646 | skb->data, 16, true); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 647 | #endif |
| 648 | |
| 649 | len = skb->len; |
Dongdong Deng | 4871953 | 2009-08-23 19:49:07 -0700 | [diff] [blame] | 650 | spin_lock_irqsave(&bp->lock, flags); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 651 | |
| 652 | /* This is a hard error, log it. */ |
| 653 | if (TX_BUFFS_AVAIL(bp) < 1) { |
| 654 | netif_stop_queue(dev); |
Dongdong Deng | 4871953 | 2009-08-23 19:49:07 -0700 | [diff] [blame] | 655 | spin_unlock_irqrestore(&bp->lock, flags); |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 656 | netdev_err(bp->dev, "BUG! Tx Ring full when queue awake!\n"); |
| 657 | netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n", |
| 658 | bp->tx_head, bp->tx_tail); |
Patrick McHardy | 5b54814 | 2009-06-12 06:22:29 +0000 | [diff] [blame] | 659 | return NETDEV_TX_BUSY; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | entry = bp->tx_head; |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 663 | netdev_dbg(bp->dev, "Allocated ring entry %u\n", entry); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 664 | mapping = dma_map_single(&bp->pdev->dev, skb->data, |
| 665 | len, DMA_TO_DEVICE); |
| 666 | bp->tx_skb[entry].skb = skb; |
| 667 | bp->tx_skb[entry].mapping = mapping; |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 668 | netdev_dbg(bp->dev, "Mapped skb data %p to DMA addr %08lx\n", |
| 669 | skb->data, (unsigned long)mapping); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 670 | |
| 671 | ctrl = MACB_BF(TX_FRMLEN, len); |
| 672 | ctrl |= MACB_BIT(TX_LAST); |
| 673 | if (entry == (TX_RING_SIZE - 1)) |
| 674 | ctrl |= MACB_BIT(TX_WRAP); |
| 675 | |
| 676 | bp->tx_ring[entry].addr = mapping; |
| 677 | bp->tx_ring[entry].ctrl = ctrl; |
| 678 | wmb(); |
| 679 | |
| 680 | entry = NEXT_TX(entry); |
| 681 | bp->tx_head = entry; |
| 682 | |
Richard Cochran | e072092 | 2011-06-19 21:51:28 +0000 | [diff] [blame] | 683 | skb_tx_timestamp(skb); |
| 684 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 685 | macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART)); |
| 686 | |
| 687 | if (TX_BUFFS_AVAIL(bp) < 1) |
| 688 | netif_stop_queue(dev); |
| 689 | |
Dongdong Deng | 4871953 | 2009-08-23 19:49:07 -0700 | [diff] [blame] | 690 | spin_unlock_irqrestore(&bp->lock, flags); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 691 | |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 692 | return NETDEV_TX_OK; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | static void macb_free_consistent(struct macb *bp) |
| 696 | { |
| 697 | if (bp->tx_skb) { |
| 698 | kfree(bp->tx_skb); |
| 699 | bp->tx_skb = NULL; |
| 700 | } |
| 701 | if (bp->rx_ring) { |
| 702 | dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES, |
| 703 | bp->rx_ring, bp->rx_ring_dma); |
| 704 | bp->rx_ring = NULL; |
| 705 | } |
| 706 | if (bp->tx_ring) { |
| 707 | dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES, |
| 708 | bp->tx_ring, bp->tx_ring_dma); |
| 709 | bp->tx_ring = NULL; |
| 710 | } |
| 711 | if (bp->rx_buffers) { |
| 712 | dma_free_coherent(&bp->pdev->dev, |
| 713 | RX_RING_SIZE * RX_BUFFER_SIZE, |
| 714 | bp->rx_buffers, bp->rx_buffers_dma); |
| 715 | bp->rx_buffers = NULL; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | static int macb_alloc_consistent(struct macb *bp) |
| 720 | { |
| 721 | int size; |
| 722 | |
| 723 | size = TX_RING_SIZE * sizeof(struct ring_info); |
| 724 | bp->tx_skb = kmalloc(size, GFP_KERNEL); |
| 725 | if (!bp->tx_skb) |
| 726 | goto out_err; |
| 727 | |
| 728 | size = RX_RING_BYTES; |
| 729 | bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size, |
| 730 | &bp->rx_ring_dma, GFP_KERNEL); |
| 731 | if (!bp->rx_ring) |
| 732 | goto out_err; |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 733 | netdev_dbg(bp->dev, |
| 734 | "Allocated RX ring of %d bytes at %08lx (mapped %p)\n", |
| 735 | size, (unsigned long)bp->rx_ring_dma, bp->rx_ring); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 736 | |
| 737 | size = TX_RING_BYTES; |
| 738 | bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size, |
| 739 | &bp->tx_ring_dma, GFP_KERNEL); |
| 740 | if (!bp->tx_ring) |
| 741 | goto out_err; |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 742 | netdev_dbg(bp->dev, |
| 743 | "Allocated TX ring of %d bytes at %08lx (mapped %p)\n", |
| 744 | size, (unsigned long)bp->tx_ring_dma, bp->tx_ring); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 745 | |
| 746 | size = RX_RING_SIZE * RX_BUFFER_SIZE; |
| 747 | bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size, |
| 748 | &bp->rx_buffers_dma, GFP_KERNEL); |
| 749 | if (!bp->rx_buffers) |
| 750 | goto out_err; |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 751 | netdev_dbg(bp->dev, |
| 752 | "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n", |
| 753 | size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 754 | |
| 755 | return 0; |
| 756 | |
| 757 | out_err: |
| 758 | macb_free_consistent(bp); |
| 759 | return -ENOMEM; |
| 760 | } |
| 761 | |
| 762 | static void macb_init_rings(struct macb *bp) |
| 763 | { |
| 764 | int i; |
| 765 | dma_addr_t addr; |
| 766 | |
| 767 | addr = bp->rx_buffers_dma; |
| 768 | for (i = 0; i < RX_RING_SIZE; i++) { |
| 769 | bp->rx_ring[i].addr = addr; |
| 770 | bp->rx_ring[i].ctrl = 0; |
| 771 | addr += RX_BUFFER_SIZE; |
| 772 | } |
| 773 | bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP); |
| 774 | |
| 775 | for (i = 0; i < TX_RING_SIZE; i++) { |
| 776 | bp->tx_ring[i].addr = 0; |
| 777 | bp->tx_ring[i].ctrl = MACB_BIT(TX_USED); |
| 778 | } |
| 779 | bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP); |
| 780 | |
| 781 | bp->rx_tail = bp->tx_head = bp->tx_tail = 0; |
| 782 | } |
| 783 | |
| 784 | static void macb_reset_hw(struct macb *bp) |
| 785 | { |
| 786 | /* Make sure we have the write buffer for ourselves */ |
| 787 | wmb(); |
| 788 | |
| 789 | /* |
| 790 | * Disable RX and TX (XXX: Should we halt the transmission |
| 791 | * more gracefully?) |
| 792 | */ |
| 793 | macb_writel(bp, NCR, 0); |
| 794 | |
| 795 | /* Clear the stats registers (XXX: Update stats first?) */ |
| 796 | macb_writel(bp, NCR, MACB_BIT(CLRSTAT)); |
| 797 | |
| 798 | /* Clear all status flags */ |
Joachim Eastwood | 95ebcea | 2012-10-22 08:45:31 +0000 | [diff] [blame] | 799 | macb_writel(bp, TSR, -1); |
| 800 | macb_writel(bp, RSR, -1); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 801 | |
| 802 | /* Disable all interrupts */ |
Joachim Eastwood | 95ebcea | 2012-10-22 08:45:31 +0000 | [diff] [blame] | 803 | macb_writel(bp, IDR, -1); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 804 | macb_readl(bp, ISR); |
| 805 | } |
| 806 | |
Jamie Iles | 70c9f3d | 2011-03-09 16:22:54 +0000 | [diff] [blame] | 807 | static u32 gem_mdc_clk_div(struct macb *bp) |
| 808 | { |
| 809 | u32 config; |
| 810 | unsigned long pclk_hz = clk_get_rate(bp->pclk); |
| 811 | |
| 812 | if (pclk_hz <= 20000000) |
| 813 | config = GEM_BF(CLK, GEM_CLK_DIV8); |
| 814 | else if (pclk_hz <= 40000000) |
| 815 | config = GEM_BF(CLK, GEM_CLK_DIV16); |
| 816 | else if (pclk_hz <= 80000000) |
| 817 | config = GEM_BF(CLK, GEM_CLK_DIV32); |
| 818 | else if (pclk_hz <= 120000000) |
| 819 | config = GEM_BF(CLK, GEM_CLK_DIV48); |
| 820 | else if (pclk_hz <= 160000000) |
| 821 | config = GEM_BF(CLK, GEM_CLK_DIV64); |
| 822 | else |
| 823 | config = GEM_BF(CLK, GEM_CLK_DIV96); |
| 824 | |
| 825 | return config; |
| 826 | } |
| 827 | |
| 828 | static u32 macb_mdc_clk_div(struct macb *bp) |
| 829 | { |
| 830 | u32 config; |
| 831 | unsigned long pclk_hz; |
| 832 | |
| 833 | if (macb_is_gem(bp)) |
| 834 | return gem_mdc_clk_div(bp); |
| 835 | |
| 836 | pclk_hz = clk_get_rate(bp->pclk); |
| 837 | if (pclk_hz <= 20000000) |
| 838 | config = MACB_BF(CLK, MACB_CLK_DIV8); |
| 839 | else if (pclk_hz <= 40000000) |
| 840 | config = MACB_BF(CLK, MACB_CLK_DIV16); |
| 841 | else if (pclk_hz <= 80000000) |
| 842 | config = MACB_BF(CLK, MACB_CLK_DIV32); |
| 843 | else |
| 844 | config = MACB_BF(CLK, MACB_CLK_DIV64); |
| 845 | |
| 846 | return config; |
| 847 | } |
| 848 | |
Jamie Iles | 757a03c | 2011-03-09 16:29:59 +0000 | [diff] [blame] | 849 | /* |
| 850 | * Get the DMA bus width field of the network configuration register that we |
| 851 | * should program. We find the width from decoding the design configuration |
| 852 | * register to find the maximum supported data bus width. |
| 853 | */ |
| 854 | static u32 macb_dbw(struct macb *bp) |
| 855 | { |
| 856 | if (!macb_is_gem(bp)) |
| 857 | return 0; |
| 858 | |
| 859 | switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) { |
| 860 | case 4: |
| 861 | return GEM_BF(DBW, GEM_DBW128); |
| 862 | case 2: |
| 863 | return GEM_BF(DBW, GEM_DBW64); |
| 864 | case 1: |
| 865 | default: |
| 866 | return GEM_BF(DBW, GEM_DBW32); |
| 867 | } |
| 868 | } |
| 869 | |
Jamie Iles | 0116da4 | 2011-03-14 17:38:30 +0000 | [diff] [blame] | 870 | /* |
| 871 | * Configure the receive DMA engine to use the correct receive buffer size. |
| 872 | * This is a configurable parameter for GEM. |
| 873 | */ |
| 874 | static void macb_configure_dma(struct macb *bp) |
| 875 | { |
| 876 | u32 dmacfg; |
| 877 | |
| 878 | if (macb_is_gem(bp)) { |
| 879 | dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L); |
| 880 | dmacfg |= GEM_BF(RXBS, RX_BUFFER_SIZE / 64); |
| 881 | gem_writel(bp, DMACFG, dmacfg); |
| 882 | } |
| 883 | } |
| 884 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 885 | static void macb_init_hw(struct macb *bp) |
| 886 | { |
| 887 | u32 config; |
| 888 | |
| 889 | macb_reset_hw(bp); |
| 890 | __macb_set_hwaddr(bp); |
| 891 | |
Jamie Iles | 70c9f3d | 2011-03-09 16:22:54 +0000 | [diff] [blame] | 892 | config = macb_mdc_clk_div(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 893 | config |= MACB_BIT(PAE); /* PAuse Enable */ |
| 894 | config |= MACB_BIT(DRFCS); /* Discard Rx FCS */ |
Peter Korsgaard | 8dd4bd0 | 2010-04-07 21:53:41 -0700 | [diff] [blame] | 895 | config |= MACB_BIT(BIG); /* Receive oversized frames */ |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 896 | if (bp->dev->flags & IFF_PROMISC) |
| 897 | config |= MACB_BIT(CAF); /* Copy All Frames */ |
| 898 | if (!(bp->dev->flags & IFF_BROADCAST)) |
| 899 | config |= MACB_BIT(NBC); /* No BroadCast */ |
Jamie Iles | 757a03c | 2011-03-09 16:29:59 +0000 | [diff] [blame] | 900 | config |= macb_dbw(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 901 | macb_writel(bp, NCFGR, config); |
| 902 | |
Jamie Iles | 0116da4 | 2011-03-14 17:38:30 +0000 | [diff] [blame] | 903 | macb_configure_dma(bp); |
| 904 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 905 | /* Initialize TX and RX buffers */ |
| 906 | macb_writel(bp, RBQP, bp->rx_ring_dma); |
| 907 | macb_writel(bp, TBQP, bp->tx_ring_dma); |
| 908 | |
| 909 | /* Enable TX and RX */ |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 910 | 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] | 911 | |
| 912 | /* Enable interrupts */ |
| 913 | macb_writel(bp, IER, (MACB_BIT(RCOMP) |
| 914 | | MACB_BIT(RXUBR) |
| 915 | | MACB_BIT(ISR_TUND) |
| 916 | | MACB_BIT(ISR_RLE) |
| 917 | | MACB_BIT(TXERR) |
| 918 | | MACB_BIT(TCOMP) |
| 919 | | MACB_BIT(ISR_ROVR) |
| 920 | | MACB_BIT(HRESP))); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 921 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 922 | } |
| 923 | |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 924 | /* |
| 925 | * The hash address register is 64 bits long and takes up two |
| 926 | * locations in the memory map. The least significant bits are stored |
| 927 | * in EMAC_HSL and the most significant bits in EMAC_HSH. |
| 928 | * |
| 929 | * The unicast hash enable and the multicast hash enable bits in the |
| 930 | * network configuration register enable the reception of hash matched |
| 931 | * frames. The destination address is reduced to a 6 bit index into |
| 932 | * the 64 bit hash register using the following hash function. The |
| 933 | * hash function is an exclusive or of every sixth bit of the |
| 934 | * destination address. |
| 935 | * |
| 936 | * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47] |
| 937 | * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46] |
| 938 | * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45] |
| 939 | * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44] |
| 940 | * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43] |
| 941 | * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42] |
| 942 | * |
| 943 | * da[0] represents the least significant bit of the first byte |
| 944 | * received, that is, the multicast/unicast indicator, and da[47] |
| 945 | * represents the most significant bit of the last byte received. If |
| 946 | * the hash index, hi[n], points to a bit that is set in the hash |
| 947 | * register then the frame will be matched according to whether the |
| 948 | * frame is multicast or unicast. A multicast match will be signalled |
| 949 | * if the multicast hash enable bit is set, da[0] is 1 and the hash |
| 950 | * index points to a bit set in the hash register. A unicast match |
| 951 | * will be signalled if the unicast hash enable bit is set, da[0] is 0 |
| 952 | * and the hash index points to a bit set in the hash register. To |
| 953 | * receive all multicast frames, the hash register should be set with |
| 954 | * all ones and the multicast hash enable bit should be set in the |
| 955 | * network configuration register. |
| 956 | */ |
| 957 | |
| 958 | static inline int hash_bit_value(int bitnr, __u8 *addr) |
| 959 | { |
| 960 | if (addr[bitnr / 8] & (1 << (bitnr % 8))) |
| 961 | return 1; |
| 962 | return 0; |
| 963 | } |
| 964 | |
| 965 | /* |
| 966 | * Return the hash index value for the specified address. |
| 967 | */ |
| 968 | static int hash_get_index(__u8 *addr) |
| 969 | { |
| 970 | int i, j, bitval; |
| 971 | int hash_index = 0; |
| 972 | |
| 973 | for (j = 0; j < 6; j++) { |
| 974 | for (i = 0, bitval = 0; i < 8; i++) |
| 975 | bitval ^= hash_bit_value(i*6 + j, addr); |
| 976 | |
| 977 | hash_index |= (bitval << j); |
| 978 | } |
| 979 | |
| 980 | return hash_index; |
| 981 | } |
| 982 | |
| 983 | /* |
| 984 | * Add multicast addresses to the internal multicast-hash table. |
| 985 | */ |
| 986 | static void macb_sethashtable(struct net_device *dev) |
| 987 | { |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 988 | struct netdev_hw_addr *ha; |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 989 | unsigned long mc_filter[2]; |
Jiri Pirko | f9dcbcc | 2010-02-23 09:19:49 +0000 | [diff] [blame] | 990 | unsigned int bitnr; |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 991 | struct macb *bp = netdev_priv(dev); |
| 992 | |
| 993 | mc_filter[0] = mc_filter[1] = 0; |
| 994 | |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 995 | netdev_for_each_mc_addr(ha, dev) { |
| 996 | bitnr = hash_get_index(ha->addr); |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 997 | mc_filter[bitnr >> 5] |= 1 << (bitnr & 31); |
| 998 | } |
| 999 | |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1000 | macb_or_gem_writel(bp, HRB, mc_filter[0]); |
| 1001 | macb_or_gem_writel(bp, HRT, mc_filter[1]); |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | * Enable/Disable promiscuous and multicast modes. |
| 1006 | */ |
Joachim Eastwood | e0da1f1 | 2012-10-18 11:01:15 +0000 | [diff] [blame] | 1007 | void macb_set_rx_mode(struct net_device *dev) |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1008 | { |
| 1009 | unsigned long cfg; |
| 1010 | struct macb *bp = netdev_priv(dev); |
| 1011 | |
| 1012 | cfg = macb_readl(bp, NCFGR); |
| 1013 | |
| 1014 | if (dev->flags & IFF_PROMISC) |
| 1015 | /* Enable promiscuous mode */ |
| 1016 | cfg |= MACB_BIT(CAF); |
| 1017 | else if (dev->flags & (~IFF_PROMISC)) |
| 1018 | /* Disable promiscuous mode */ |
| 1019 | cfg &= ~MACB_BIT(CAF); |
| 1020 | |
| 1021 | if (dev->flags & IFF_ALLMULTI) { |
| 1022 | /* Enable all multicast mode */ |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1023 | macb_or_gem_writel(bp, HRB, -1); |
| 1024 | macb_or_gem_writel(bp, HRT, -1); |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1025 | cfg |= MACB_BIT(NCFGR_MTI); |
Jiri Pirko | 4cd24ea | 2010-02-08 04:30:35 +0000 | [diff] [blame] | 1026 | } else if (!netdev_mc_empty(dev)) { |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1027 | /* Enable specific multicasts */ |
| 1028 | macb_sethashtable(dev); |
| 1029 | cfg |= MACB_BIT(NCFGR_MTI); |
| 1030 | } else if (dev->flags & (~IFF_ALLMULTI)) { |
| 1031 | /* Disable all multicast mode */ |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1032 | macb_or_gem_writel(bp, HRB, 0); |
| 1033 | macb_or_gem_writel(bp, HRT, 0); |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1034 | cfg &= ~MACB_BIT(NCFGR_MTI); |
| 1035 | } |
| 1036 | |
| 1037 | macb_writel(bp, NCFGR, cfg); |
| 1038 | } |
Joachim Eastwood | e0da1f1 | 2012-10-18 11:01:15 +0000 | [diff] [blame] | 1039 | EXPORT_SYMBOL_GPL(macb_set_rx_mode); |
Patrice Vilchez | 446ebd0 | 2007-07-12 19:07:25 +0200 | [diff] [blame] | 1040 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1041 | static int macb_open(struct net_device *dev) |
| 1042 | { |
| 1043 | struct macb *bp = netdev_priv(dev); |
| 1044 | int err; |
| 1045 | |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 1046 | netdev_dbg(bp->dev, "open\n"); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1047 | |
Nicolas Ferre | 03fc472 | 2012-07-03 23:14:13 +0000 | [diff] [blame] | 1048 | /* carrier starts down */ |
| 1049 | netif_carrier_off(dev); |
| 1050 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1051 | /* if the phy is not yet register, retry later*/ |
| 1052 | if (!bp->phy_dev) |
| 1053 | return -EAGAIN; |
| 1054 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1055 | if (!is_valid_ether_addr(dev->dev_addr)) |
| 1056 | return -EADDRNOTAVAIL; |
| 1057 | |
| 1058 | err = macb_alloc_consistent(bp); |
| 1059 | if (err) { |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 1060 | netdev_err(dev, "Unable to allocate DMA memory (error %d)\n", |
| 1061 | err); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1062 | return err; |
| 1063 | } |
| 1064 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1065 | napi_enable(&bp->napi); |
| 1066 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1067 | macb_init_rings(bp); |
| 1068 | macb_init_hw(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1069 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1070 | /* schedule a link state check */ |
| 1071 | phy_start(bp->phy_dev); |
| 1072 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1073 | netif_start_queue(dev); |
| 1074 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1075 | return 0; |
| 1076 | } |
| 1077 | |
| 1078 | static int macb_close(struct net_device *dev) |
| 1079 | { |
| 1080 | struct macb *bp = netdev_priv(dev); |
| 1081 | unsigned long flags; |
| 1082 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1083 | netif_stop_queue(dev); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1084 | napi_disable(&bp->napi); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1085 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1086 | if (bp->phy_dev) |
| 1087 | phy_stop(bp->phy_dev); |
| 1088 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1089 | spin_lock_irqsave(&bp->lock, flags); |
| 1090 | macb_reset_hw(bp); |
| 1091 | netif_carrier_off(dev); |
| 1092 | spin_unlock_irqrestore(&bp->lock, flags); |
| 1093 | |
| 1094 | macb_free_consistent(bp); |
| 1095 | |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
Jamie Iles | a494ed8 | 2011-03-09 16:26:35 +0000 | [diff] [blame] | 1099 | static void gem_update_stats(struct macb *bp) |
| 1100 | { |
| 1101 | u32 __iomem *reg = bp->regs + GEM_OTX; |
| 1102 | u32 *p = &bp->hw_stats.gem.tx_octets_31_0; |
| 1103 | u32 *end = &bp->hw_stats.gem.rx_udp_checksum_errors + 1; |
| 1104 | |
| 1105 | for (; p < end; p++, reg++) |
| 1106 | *p += __raw_readl(reg); |
| 1107 | } |
| 1108 | |
| 1109 | static struct net_device_stats *gem_get_stats(struct macb *bp) |
| 1110 | { |
| 1111 | struct gem_stats *hwstat = &bp->hw_stats.gem; |
| 1112 | struct net_device_stats *nstat = &bp->stats; |
| 1113 | |
| 1114 | gem_update_stats(bp); |
| 1115 | |
| 1116 | nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors + |
| 1117 | hwstat->rx_alignment_errors + |
| 1118 | hwstat->rx_resource_errors + |
| 1119 | hwstat->rx_overruns + |
| 1120 | hwstat->rx_oversize_frames + |
| 1121 | hwstat->rx_jabbers + |
| 1122 | hwstat->rx_undersized_frames + |
| 1123 | hwstat->rx_length_field_frame_errors); |
| 1124 | nstat->tx_errors = (hwstat->tx_late_collisions + |
| 1125 | hwstat->tx_excessive_collisions + |
| 1126 | hwstat->tx_underrun + |
| 1127 | hwstat->tx_carrier_sense_errors); |
| 1128 | nstat->multicast = hwstat->rx_multicast_frames; |
| 1129 | nstat->collisions = (hwstat->tx_single_collision_frames + |
| 1130 | hwstat->tx_multiple_collision_frames + |
| 1131 | hwstat->tx_excessive_collisions); |
| 1132 | nstat->rx_length_errors = (hwstat->rx_oversize_frames + |
| 1133 | hwstat->rx_jabbers + |
| 1134 | hwstat->rx_undersized_frames + |
| 1135 | hwstat->rx_length_field_frame_errors); |
| 1136 | nstat->rx_over_errors = hwstat->rx_resource_errors; |
| 1137 | nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors; |
| 1138 | nstat->rx_frame_errors = hwstat->rx_alignment_errors; |
| 1139 | nstat->rx_fifo_errors = hwstat->rx_overruns; |
| 1140 | nstat->tx_aborted_errors = hwstat->tx_excessive_collisions; |
| 1141 | nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors; |
| 1142 | nstat->tx_fifo_errors = hwstat->tx_underrun; |
| 1143 | |
| 1144 | return nstat; |
| 1145 | } |
| 1146 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1147 | static struct net_device_stats *macb_get_stats(struct net_device *dev) |
| 1148 | { |
| 1149 | struct macb *bp = netdev_priv(dev); |
| 1150 | struct net_device_stats *nstat = &bp->stats; |
Jamie Iles | a494ed8 | 2011-03-09 16:26:35 +0000 | [diff] [blame] | 1151 | struct macb_stats *hwstat = &bp->hw_stats.macb; |
| 1152 | |
| 1153 | if (macb_is_gem(bp)) |
| 1154 | return gem_get_stats(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1155 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1156 | /* read stats from hardware */ |
| 1157 | macb_update_stats(bp); |
| 1158 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1159 | /* Convert HW stats into netdevice stats */ |
| 1160 | nstat->rx_errors = (hwstat->rx_fcs_errors + |
| 1161 | hwstat->rx_align_errors + |
| 1162 | hwstat->rx_resource_errors + |
| 1163 | hwstat->rx_overruns + |
| 1164 | hwstat->rx_oversize_pkts + |
| 1165 | hwstat->rx_jabbers + |
| 1166 | hwstat->rx_undersize_pkts + |
| 1167 | hwstat->sqe_test_errors + |
| 1168 | hwstat->rx_length_mismatch); |
| 1169 | nstat->tx_errors = (hwstat->tx_late_cols + |
| 1170 | hwstat->tx_excessive_cols + |
| 1171 | hwstat->tx_underruns + |
| 1172 | hwstat->tx_carrier_errors); |
| 1173 | nstat->collisions = (hwstat->tx_single_cols + |
| 1174 | hwstat->tx_multiple_cols + |
| 1175 | hwstat->tx_excessive_cols); |
| 1176 | nstat->rx_length_errors = (hwstat->rx_oversize_pkts + |
| 1177 | hwstat->rx_jabbers + |
| 1178 | hwstat->rx_undersize_pkts + |
| 1179 | hwstat->rx_length_mismatch); |
Alexander Stein | b19f7f7 | 2011-04-13 05:03:24 +0000 | [diff] [blame] | 1180 | nstat->rx_over_errors = hwstat->rx_resource_errors + |
| 1181 | hwstat->rx_overruns; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1182 | nstat->rx_crc_errors = hwstat->rx_fcs_errors; |
| 1183 | nstat->rx_frame_errors = hwstat->rx_align_errors; |
| 1184 | nstat->rx_fifo_errors = hwstat->rx_overruns; |
| 1185 | /* XXX: What does "missed" mean? */ |
| 1186 | nstat->tx_aborted_errors = hwstat->tx_excessive_cols; |
| 1187 | nstat->tx_carrier_errors = hwstat->tx_carrier_errors; |
| 1188 | nstat->tx_fifo_errors = hwstat->tx_underruns; |
| 1189 | /* Don't know about heartbeat or window errors... */ |
| 1190 | |
| 1191 | return nstat; |
| 1192 | } |
| 1193 | |
| 1194 | static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 1195 | { |
| 1196 | struct macb *bp = netdev_priv(dev); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1197 | struct phy_device *phydev = bp->phy_dev; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1198 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1199 | if (!phydev) |
| 1200 | return -ENODEV; |
| 1201 | |
| 1202 | return phy_ethtool_gset(phydev, cmd); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 1206 | { |
| 1207 | struct macb *bp = netdev_priv(dev); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1208 | struct phy_device *phydev = bp->phy_dev; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1209 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1210 | if (!phydev) |
| 1211 | return -ENODEV; |
| 1212 | |
| 1213 | return phy_ethtool_sset(phydev, cmd); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1214 | } |
| 1215 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1216 | static void macb_get_drvinfo(struct net_device *dev, |
| 1217 | struct ethtool_drvinfo *info) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1218 | { |
| 1219 | struct macb *bp = netdev_priv(dev); |
| 1220 | |
| 1221 | strcpy(info->driver, bp->pdev->dev.driver->name); |
| 1222 | strcpy(info->version, "$Revision: 1.14 $"); |
Kay Sievers | db1d7bf | 2009-01-26 21:12:58 -0800 | [diff] [blame] | 1223 | strcpy(info->bus_info, dev_name(&bp->pdev->dev)); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1224 | } |
| 1225 | |
Joachim Eastwood | 0005f54 | 2012-10-18 11:01:12 +0000 | [diff] [blame] | 1226 | const struct ethtool_ops macb_ethtool_ops = { |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1227 | .get_settings = macb_get_settings, |
| 1228 | .set_settings = macb_set_settings, |
| 1229 | .get_drvinfo = macb_get_drvinfo, |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1230 | .get_link = ethtool_op_get_link, |
Richard Cochran | 17f393e | 2012-04-03 22:59:31 +0000 | [diff] [blame] | 1231 | .get_ts_info = ethtool_op_get_ts_info, |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1232 | }; |
Joachim Eastwood | 0005f54 | 2012-10-18 11:01:12 +0000 | [diff] [blame] | 1233 | EXPORT_SYMBOL_GPL(macb_ethtool_ops); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1234 | |
Joachim Eastwood | 0005f54 | 2012-10-18 11:01:12 +0000 | [diff] [blame] | 1235 | int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1236 | { |
| 1237 | struct macb *bp = netdev_priv(dev); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1238 | struct phy_device *phydev = bp->phy_dev; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1239 | |
| 1240 | if (!netif_running(dev)) |
| 1241 | return -EINVAL; |
| 1242 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1243 | if (!phydev) |
| 1244 | return -ENODEV; |
| 1245 | |
Richard Cochran | 28b0411 | 2010-07-17 08:48:55 +0000 | [diff] [blame] | 1246 | return phy_mii_ioctl(phydev, rq, cmd); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1247 | } |
Joachim Eastwood | 0005f54 | 2012-10-18 11:01:12 +0000 | [diff] [blame] | 1248 | EXPORT_SYMBOL_GPL(macb_ioctl); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1249 | |
Alexander Beregalov | 5f1fa99 | 2009-04-11 07:42:26 +0000 | [diff] [blame] | 1250 | static const struct net_device_ops macb_netdev_ops = { |
| 1251 | .ndo_open = macb_open, |
| 1252 | .ndo_stop = macb_close, |
| 1253 | .ndo_start_xmit = macb_start_xmit, |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame] | 1254 | .ndo_set_rx_mode = macb_set_rx_mode, |
Alexander Beregalov | 5f1fa99 | 2009-04-11 07:42:26 +0000 | [diff] [blame] | 1255 | .ndo_get_stats = macb_get_stats, |
| 1256 | .ndo_do_ioctl = macb_ioctl, |
| 1257 | .ndo_validate_addr = eth_validate_addr, |
| 1258 | .ndo_change_mtu = eth_change_mtu, |
| 1259 | .ndo_set_mac_address = eth_mac_addr, |
Thomas Petazzoni | 6e8cf5c | 2009-05-04 11:08:41 -0700 | [diff] [blame] | 1260 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1261 | .ndo_poll_controller = macb_poll_controller, |
| 1262 | #endif |
Alexander Beregalov | 5f1fa99 | 2009-04-11 07:42:26 +0000 | [diff] [blame] | 1263 | }; |
| 1264 | |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 1265 | #if defined(CONFIG_OF) |
| 1266 | static const struct of_device_id macb_dt_ids[] = { |
| 1267 | { .compatible = "cdns,at32ap7000-macb" }, |
| 1268 | { .compatible = "cdns,at91sam9260-macb" }, |
| 1269 | { .compatible = "cdns,macb" }, |
| 1270 | { .compatible = "cdns,pc302-gem" }, |
| 1271 | { .compatible = "cdns,gem" }, |
| 1272 | { /* sentinel */ } |
| 1273 | }; |
| 1274 | |
| 1275 | MODULE_DEVICE_TABLE(of, macb_dt_ids); |
| 1276 | |
| 1277 | static int __devinit macb_get_phy_mode_dt(struct platform_device *pdev) |
| 1278 | { |
| 1279 | struct device_node *np = pdev->dev.of_node; |
| 1280 | |
| 1281 | if (np) |
| 1282 | return of_get_phy_mode(np); |
| 1283 | |
| 1284 | return -ENODEV; |
| 1285 | } |
| 1286 | |
| 1287 | static int __devinit macb_get_hwaddr_dt(struct macb *bp) |
| 1288 | { |
| 1289 | struct device_node *np = bp->pdev->dev.of_node; |
| 1290 | if (np) { |
| 1291 | const char *mac = of_get_mac_address(np); |
| 1292 | if (mac) { |
| 1293 | memcpy(bp->dev->dev_addr, mac, ETH_ALEN); |
| 1294 | return 0; |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | return -ENODEV; |
| 1299 | } |
| 1300 | #else |
| 1301 | static int __devinit macb_get_phy_mode_dt(struct platform_device *pdev) |
| 1302 | { |
| 1303 | return -ENODEV; |
| 1304 | } |
| 1305 | static int __devinit macb_get_hwaddr_dt(struct macb *bp) |
| 1306 | { |
| 1307 | return -ENODEV; |
| 1308 | } |
| 1309 | #endif |
| 1310 | |
Haavard Skinnemoen | 06c3fd6 | 2008-01-31 13:10:22 +0100 | [diff] [blame] | 1311 | static int __init macb_probe(struct platform_device *pdev) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1312 | { |
Jamie Iles | 84e0cdb | 2011-03-08 20:17:06 +0000 | [diff] [blame] | 1313 | struct macb_platform_data *pdata; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1314 | struct resource *regs; |
| 1315 | struct net_device *dev; |
| 1316 | struct macb *bp; |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1317 | struct phy_device *phydev; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1318 | u32 config; |
| 1319 | int err = -ENXIO; |
| 1320 | |
| 1321 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1322 | if (!regs) { |
| 1323 | dev_err(&pdev->dev, "no mmio resource defined\n"); |
| 1324 | goto err_out; |
| 1325 | } |
| 1326 | |
| 1327 | err = -ENOMEM; |
| 1328 | dev = alloc_etherdev(sizeof(*bp)); |
Joe Perches | 41de8d4 | 2012-01-29 13:47:52 +0000 | [diff] [blame] | 1329 | if (!dev) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1330 | goto err_out; |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1331 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1332 | SET_NETDEV_DEV(dev, &pdev->dev); |
| 1333 | |
| 1334 | /* TODO: Actually, we have some interesting features... */ |
| 1335 | dev->features |= 0; |
| 1336 | |
| 1337 | bp = netdev_priv(dev); |
| 1338 | bp->pdev = pdev; |
| 1339 | bp->dev = dev; |
| 1340 | |
| 1341 | spin_lock_init(&bp->lock); |
| 1342 | |
Jamie Iles | 461845d | 2011-03-08 20:19:23 +0000 | [diff] [blame] | 1343 | bp->pclk = clk_get(&pdev->dev, "pclk"); |
Andrew Victor | 0cc8674 | 2007-02-07 16:40:44 +0100 | [diff] [blame] | 1344 | if (IS_ERR(bp->pclk)) { |
| 1345 | dev_err(&pdev->dev, "failed to get macb_clk\n"); |
| 1346 | goto err_out_free_dev; |
| 1347 | } |
| 1348 | clk_enable(bp->pclk); |
Jamie Iles | 461845d | 2011-03-08 20:19:23 +0000 | [diff] [blame] | 1349 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1350 | bp->hclk = clk_get(&pdev->dev, "hclk"); |
| 1351 | if (IS_ERR(bp->hclk)) { |
| 1352 | dev_err(&pdev->dev, "failed to get hclk\n"); |
| 1353 | goto err_out_put_pclk; |
| 1354 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1355 | clk_enable(bp->hclk); |
| 1356 | |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 1357 | bp->regs = ioremap(regs->start, resource_size(regs)); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1358 | if (!bp->regs) { |
| 1359 | dev_err(&pdev->dev, "failed to map registers, aborting.\n"); |
| 1360 | err = -ENOMEM; |
| 1361 | goto err_out_disable_clocks; |
| 1362 | } |
| 1363 | |
| 1364 | dev->irq = platform_get_irq(pdev, 0); |
Javier Martinez Canillas | ab392d2 | 2011-03-28 16:27:31 +0000 | [diff] [blame] | 1365 | err = request_irq(dev->irq, macb_interrupt, 0, dev->name, dev); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1366 | if (err) { |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 1367 | dev_err(&pdev->dev, "Unable to request IRQ %d (error %d)\n", |
| 1368 | dev->irq, err); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1369 | goto err_out_iounmap; |
| 1370 | } |
| 1371 | |
Alexander Beregalov | 5f1fa99 | 2009-04-11 07:42:26 +0000 | [diff] [blame] | 1372 | dev->netdev_ops = &macb_netdev_ops; |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1373 | netif_napi_add(dev, &bp->napi, macb_poll, 64); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1374 | dev->ethtool_ops = &macb_ethtool_ops; |
| 1375 | |
| 1376 | dev->base_addr = regs->start; |
| 1377 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1378 | /* Set MII management clock divider */ |
Jamie Iles | 70c9f3d | 2011-03-09 16:22:54 +0000 | [diff] [blame] | 1379 | config = macb_mdc_clk_div(bp); |
Jamie Iles | 757a03c | 2011-03-09 16:29:59 +0000 | [diff] [blame] | 1380 | config |= macb_dbw(bp); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1381 | macb_writel(bp, NCFGR, config); |
| 1382 | |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 1383 | err = macb_get_hwaddr_dt(bp); |
| 1384 | if (err < 0) |
| 1385 | macb_get_hwaddr(bp); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1386 | |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 1387 | err = macb_get_phy_mode_dt(pdev); |
| 1388 | if (err < 0) { |
| 1389 | pdata = pdev->dev.platform_data; |
| 1390 | if (pdata && pdata->is_rmii) |
| 1391 | bp->phy_interface = PHY_INTERFACE_MODE_RMII; |
| 1392 | else |
| 1393 | bp->phy_interface = PHY_INTERFACE_MODE_MII; |
| 1394 | } else { |
| 1395 | bp->phy_interface = err; |
| 1396 | } |
| 1397 | |
Patrice Vilchez | 140b755 | 2012-10-31 06:04:50 +0000 | [diff] [blame^] | 1398 | if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII) |
| 1399 | macb_or_gem_writel(bp, USRIO, GEM_BIT(RGMII)); |
| 1400 | else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII) |
Andrew Victor | 0cc8674 | 2007-02-07 16:40:44 +0100 | [diff] [blame] | 1401 | #if defined(CONFIG_ARCH_AT91) |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1402 | macb_or_gem_writel(bp, USRIO, (MACB_BIT(RMII) | |
| 1403 | MACB_BIT(CLKEN))); |
Andrew Victor | 0cc8674 | 2007-02-07 16:40:44 +0100 | [diff] [blame] | 1404 | #else |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1405 | macb_or_gem_writel(bp, USRIO, 0); |
Andrew Victor | 0cc8674 | 2007-02-07 16:40:44 +0100 | [diff] [blame] | 1406 | #endif |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1407 | else |
Andrew Victor | 0cc8674 | 2007-02-07 16:40:44 +0100 | [diff] [blame] | 1408 | #if defined(CONFIG_ARCH_AT91) |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1409 | macb_or_gem_writel(bp, USRIO, MACB_BIT(CLKEN)); |
Andrew Victor | 0cc8674 | 2007-02-07 16:40:44 +0100 | [diff] [blame] | 1410 | #else |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1411 | macb_or_gem_writel(bp, USRIO, MACB_BIT(MII)); |
Andrew Victor | 0cc8674 | 2007-02-07 16:40:44 +0100 | [diff] [blame] | 1412 | #endif |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1413 | |
| 1414 | bp->tx_pending = DEF_TX_RING_PENDING; |
| 1415 | |
| 1416 | err = register_netdev(dev); |
| 1417 | if (err) { |
| 1418 | dev_err(&pdev->dev, "Cannot register net device, aborting.\n"); |
| 1419 | goto err_out_free_irq; |
| 1420 | } |
| 1421 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1422 | if (macb_mii_init(bp) != 0) { |
| 1423 | goto err_out_unregister_netdev; |
| 1424 | } |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1425 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1426 | platform_set_drvdata(pdev, dev); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1427 | |
Nicolas Ferre | 03fc472 | 2012-07-03 23:14:13 +0000 | [diff] [blame] | 1428 | netif_carrier_off(dev); |
| 1429 | |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1430 | netdev_info(dev, "Cadence %s at 0x%08lx irq %d (%pM)\n", |
| 1431 | macb_is_gem(bp) ? "GEM" : "MACB", dev->base_addr, |
| 1432 | dev->irq, dev->dev_addr); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1433 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1434 | phydev = bp->phy_dev; |
Jamie Iles | c220f8c | 2011-03-08 20:27:08 +0000 | [diff] [blame] | 1435 | netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n", |
| 1436 | phydev->drv->name, dev_name(&phydev->dev), phydev->irq); |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1437 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1438 | return 0; |
| 1439 | |
frederic RODO | 6c36a70 | 2007-07-12 19:07:24 +0200 | [diff] [blame] | 1440 | err_out_unregister_netdev: |
| 1441 | unregister_netdev(dev); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1442 | err_out_free_irq: |
| 1443 | free_irq(dev->irq, dev); |
| 1444 | err_out_iounmap: |
| 1445 | iounmap(bp->regs); |
| 1446 | err_out_disable_clocks: |
| 1447 | clk_disable(bp->hclk); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1448 | clk_put(bp->hclk); |
Andrew Victor | 0cc8674 | 2007-02-07 16:40:44 +0100 | [diff] [blame] | 1449 | clk_disable(bp->pclk); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1450 | err_out_put_pclk: |
| 1451 | clk_put(bp->pclk); |
| 1452 | err_out_free_dev: |
| 1453 | free_netdev(dev); |
| 1454 | err_out: |
| 1455 | platform_set_drvdata(pdev, NULL); |
| 1456 | return err; |
| 1457 | } |
| 1458 | |
Haavard Skinnemoen | 06c3fd6 | 2008-01-31 13:10:22 +0100 | [diff] [blame] | 1459 | static int __exit macb_remove(struct platform_device *pdev) |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1460 | { |
| 1461 | struct net_device *dev; |
| 1462 | struct macb *bp; |
| 1463 | |
| 1464 | dev = platform_get_drvdata(pdev); |
| 1465 | |
| 1466 | if (dev) { |
| 1467 | bp = netdev_priv(dev); |
Atsushi Nemoto | 84b7901 | 2008-04-10 23:30:07 +0900 | [diff] [blame] | 1468 | if (bp->phy_dev) |
| 1469 | phy_disconnect(bp->phy_dev); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 1470 | mdiobus_unregister(bp->mii_bus); |
| 1471 | kfree(bp->mii_bus->irq); |
| 1472 | mdiobus_free(bp->mii_bus); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1473 | unregister_netdev(dev); |
| 1474 | free_irq(dev->irq, dev); |
| 1475 | iounmap(bp->regs); |
| 1476 | clk_disable(bp->hclk); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1477 | clk_put(bp->hclk); |
Andrew Victor | 0cc8674 | 2007-02-07 16:40:44 +0100 | [diff] [blame] | 1478 | clk_disable(bp->pclk); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1479 | clk_put(bp->pclk); |
| 1480 | free_netdev(dev); |
| 1481 | platform_set_drvdata(pdev, NULL); |
| 1482 | } |
| 1483 | |
| 1484 | return 0; |
| 1485 | } |
| 1486 | |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 1487 | #ifdef CONFIG_PM |
| 1488 | static int macb_suspend(struct platform_device *pdev, pm_message_t state) |
| 1489 | { |
| 1490 | struct net_device *netdev = platform_get_drvdata(pdev); |
| 1491 | struct macb *bp = netdev_priv(netdev); |
| 1492 | |
Nicolas Ferre | 03fc472 | 2012-07-03 23:14:13 +0000 | [diff] [blame] | 1493 | netif_carrier_off(netdev); |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 1494 | netif_device_detach(netdev); |
| 1495 | |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 1496 | clk_disable(bp->hclk); |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 1497 | clk_disable(bp->pclk); |
| 1498 | |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
| 1502 | static int macb_resume(struct platform_device *pdev) |
| 1503 | { |
| 1504 | struct net_device *netdev = platform_get_drvdata(pdev); |
| 1505 | struct macb *bp = netdev_priv(netdev); |
| 1506 | |
| 1507 | clk_enable(bp->pclk); |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 1508 | clk_enable(bp->hclk); |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 1509 | |
| 1510 | netif_device_attach(netdev); |
| 1511 | |
| 1512 | return 0; |
| 1513 | } |
| 1514 | #else |
| 1515 | #define macb_suspend NULL |
| 1516 | #define macb_resume NULL |
| 1517 | #endif |
| 1518 | |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1519 | static struct platform_driver macb_driver = { |
Haavard Skinnemoen | 06c3fd6 | 2008-01-31 13:10:22 +0100 | [diff] [blame] | 1520 | .remove = __exit_p(macb_remove), |
Haavard Skinnemoen | c1f598f | 2008-03-04 13:39:29 +0100 | [diff] [blame] | 1521 | .suspend = macb_suspend, |
| 1522 | .resume = macb_resume, |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1523 | .driver = { |
| 1524 | .name = "macb", |
Kay Sievers | 72abb46 | 2008-04-18 13:50:44 -0700 | [diff] [blame] | 1525 | .owner = THIS_MODULE, |
Jean-Christophe PLAGNIOL-VILLARD | fb97a84 | 2011-11-18 15:29:25 +0100 | [diff] [blame] | 1526 | .of_match_table = of_match_ptr(macb_dt_ids), |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1527 | }, |
| 1528 | }; |
| 1529 | |
| 1530 | static int __init macb_init(void) |
| 1531 | { |
Haavard Skinnemoen | 06c3fd6 | 2008-01-31 13:10:22 +0100 | [diff] [blame] | 1532 | return platform_driver_probe(&macb_driver, macb_probe); |
Haavard Skinnemoen | 89e5785 | 2006-11-09 14:51:17 +0100 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | static void __exit macb_exit(void) |
| 1536 | { |
| 1537 | platform_driver_unregister(&macb_driver); |
| 1538 | } |
| 1539 | |
| 1540 | module_init(macb_init); |
| 1541 | module_exit(macb_exit); |
| 1542 | |
| 1543 | MODULE_LICENSE("GPL"); |
Jamie Iles | f75ba50 | 2011-11-08 10:12:32 +0000 | [diff] [blame] | 1544 | MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver"); |
Jean Delvare | e05503e | 2011-05-18 16:49:24 +0200 | [diff] [blame] | 1545 | MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); |
Kay Sievers | 72abb46 | 2008-04-18 13:50:44 -0700 | [diff] [blame] | 1546 | MODULE_ALIAS("platform:macb"); |