Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/net/ethoc.c |
| 3 | * |
| 4 | * Copyright (C) 2007-2008 Avionic Design Development GmbH |
| 5 | * Copyright (C) 2008-2009 Avionic Design GmbH |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * Written by Thierry Reding <thierry.reding@avionic-design.de> |
| 12 | */ |
| 13 | |
| 14 | #include <linux/etherdevice.h> |
| 15 | #include <linux/crc32.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/mii.h> |
| 18 | #include <linux/phy.h> |
| 19 | #include <linux/platform_device.h> |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 20 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Jonas Bonn | e0f4258 | 2010-11-25 02:30:25 +0000 | [diff] [blame] | 22 | #include <linux/of.h> |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 23 | #include <net/ethoc.h> |
| 24 | |
Thomas Chou | 0baa080 | 2009-10-04 23:33:20 +0000 | [diff] [blame] | 25 | static int buffer_size = 0x8000; /* 32 KBytes */ |
| 26 | module_param(buffer_size, int, 0); |
| 27 | MODULE_PARM_DESC(buffer_size, "DMA buffer allocation size"); |
| 28 | |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 29 | /* register offsets */ |
| 30 | #define MODER 0x00 |
| 31 | #define INT_SOURCE 0x04 |
| 32 | #define INT_MASK 0x08 |
| 33 | #define IPGT 0x0c |
| 34 | #define IPGR1 0x10 |
| 35 | #define IPGR2 0x14 |
| 36 | #define PACKETLEN 0x18 |
| 37 | #define COLLCONF 0x1c |
| 38 | #define TX_BD_NUM 0x20 |
| 39 | #define CTRLMODER 0x24 |
| 40 | #define MIIMODER 0x28 |
| 41 | #define MIICOMMAND 0x2c |
| 42 | #define MIIADDRESS 0x30 |
| 43 | #define MIITX_DATA 0x34 |
| 44 | #define MIIRX_DATA 0x38 |
| 45 | #define MIISTATUS 0x3c |
| 46 | #define MAC_ADDR0 0x40 |
| 47 | #define MAC_ADDR1 0x44 |
| 48 | #define ETH_HASH0 0x48 |
| 49 | #define ETH_HASH1 0x4c |
| 50 | #define ETH_TXCTRL 0x50 |
| 51 | |
| 52 | /* mode register */ |
| 53 | #define MODER_RXEN (1 << 0) /* receive enable */ |
| 54 | #define MODER_TXEN (1 << 1) /* transmit enable */ |
| 55 | #define MODER_NOPRE (1 << 2) /* no preamble */ |
| 56 | #define MODER_BRO (1 << 3) /* broadcast address */ |
| 57 | #define MODER_IAM (1 << 4) /* individual address mode */ |
| 58 | #define MODER_PRO (1 << 5) /* promiscuous mode */ |
| 59 | #define MODER_IFG (1 << 6) /* interframe gap for incoming frames */ |
| 60 | #define MODER_LOOP (1 << 7) /* loopback */ |
| 61 | #define MODER_NBO (1 << 8) /* no back-off */ |
| 62 | #define MODER_EDE (1 << 9) /* excess defer enable */ |
| 63 | #define MODER_FULLD (1 << 10) /* full duplex */ |
| 64 | #define MODER_RESET (1 << 11) /* FIXME: reset (undocumented) */ |
| 65 | #define MODER_DCRC (1 << 12) /* delayed CRC enable */ |
| 66 | #define MODER_CRC (1 << 13) /* CRC enable */ |
| 67 | #define MODER_HUGE (1 << 14) /* huge packets enable */ |
| 68 | #define MODER_PAD (1 << 15) /* padding enabled */ |
| 69 | #define MODER_RSM (1 << 16) /* receive small packets */ |
| 70 | |
| 71 | /* interrupt source and mask registers */ |
| 72 | #define INT_MASK_TXF (1 << 0) /* transmit frame */ |
| 73 | #define INT_MASK_TXE (1 << 1) /* transmit error */ |
| 74 | #define INT_MASK_RXF (1 << 2) /* receive frame */ |
| 75 | #define INT_MASK_RXE (1 << 3) /* receive error */ |
| 76 | #define INT_MASK_BUSY (1 << 4) |
| 77 | #define INT_MASK_TXC (1 << 5) /* transmit control frame */ |
| 78 | #define INT_MASK_RXC (1 << 6) /* receive control frame */ |
| 79 | |
| 80 | #define INT_MASK_TX (INT_MASK_TXF | INT_MASK_TXE) |
| 81 | #define INT_MASK_RX (INT_MASK_RXF | INT_MASK_RXE) |
| 82 | |
| 83 | #define INT_MASK_ALL ( \ |
| 84 | INT_MASK_TXF | INT_MASK_TXE | \ |
| 85 | INT_MASK_RXF | INT_MASK_RXE | \ |
| 86 | INT_MASK_TXC | INT_MASK_RXC | \ |
| 87 | INT_MASK_BUSY \ |
| 88 | ) |
| 89 | |
| 90 | /* packet length register */ |
| 91 | #define PACKETLEN_MIN(min) (((min) & 0xffff) << 16) |
| 92 | #define PACKETLEN_MAX(max) (((max) & 0xffff) << 0) |
| 93 | #define PACKETLEN_MIN_MAX(min, max) (PACKETLEN_MIN(min) | \ |
| 94 | PACKETLEN_MAX(max)) |
| 95 | |
| 96 | /* transmit buffer number register */ |
| 97 | #define TX_BD_NUM_VAL(x) (((x) <= 0x80) ? (x) : 0x80) |
| 98 | |
| 99 | /* control module mode register */ |
| 100 | #define CTRLMODER_PASSALL (1 << 0) /* pass all receive frames */ |
| 101 | #define CTRLMODER_RXFLOW (1 << 1) /* receive control flow */ |
| 102 | #define CTRLMODER_TXFLOW (1 << 2) /* transmit control flow */ |
| 103 | |
| 104 | /* MII mode register */ |
| 105 | #define MIIMODER_CLKDIV(x) ((x) & 0xfe) /* needs to be an even number */ |
| 106 | #define MIIMODER_NOPRE (1 << 8) /* no preamble */ |
| 107 | |
| 108 | /* MII command register */ |
| 109 | #define MIICOMMAND_SCAN (1 << 0) /* scan status */ |
| 110 | #define MIICOMMAND_READ (1 << 1) /* read status */ |
| 111 | #define MIICOMMAND_WRITE (1 << 2) /* write control data */ |
| 112 | |
| 113 | /* MII address register */ |
| 114 | #define MIIADDRESS_FIAD(x) (((x) & 0x1f) << 0) |
| 115 | #define MIIADDRESS_RGAD(x) (((x) & 0x1f) << 8) |
| 116 | #define MIIADDRESS_ADDR(phy, reg) (MIIADDRESS_FIAD(phy) | \ |
| 117 | MIIADDRESS_RGAD(reg)) |
| 118 | |
| 119 | /* MII transmit data register */ |
| 120 | #define MIITX_DATA_VAL(x) ((x) & 0xffff) |
| 121 | |
| 122 | /* MII receive data register */ |
| 123 | #define MIIRX_DATA_VAL(x) ((x) & 0xffff) |
| 124 | |
| 125 | /* MII status register */ |
| 126 | #define MIISTATUS_LINKFAIL (1 << 0) |
| 127 | #define MIISTATUS_BUSY (1 << 1) |
| 128 | #define MIISTATUS_INVALID (1 << 2) |
| 129 | |
| 130 | /* TX buffer descriptor */ |
| 131 | #define TX_BD_CS (1 << 0) /* carrier sense lost */ |
| 132 | #define TX_BD_DF (1 << 1) /* defer indication */ |
| 133 | #define TX_BD_LC (1 << 2) /* late collision */ |
| 134 | #define TX_BD_RL (1 << 3) /* retransmission limit */ |
| 135 | #define TX_BD_RETRY_MASK (0x00f0) |
| 136 | #define TX_BD_RETRY(x) (((x) & 0x00f0) >> 4) |
| 137 | #define TX_BD_UR (1 << 8) /* transmitter underrun */ |
| 138 | #define TX_BD_CRC (1 << 11) /* TX CRC enable */ |
| 139 | #define TX_BD_PAD (1 << 12) /* pad enable for short packets */ |
| 140 | #define TX_BD_WRAP (1 << 13) |
| 141 | #define TX_BD_IRQ (1 << 14) /* interrupt request enable */ |
| 142 | #define TX_BD_READY (1 << 15) /* TX buffer ready */ |
| 143 | #define TX_BD_LEN(x) (((x) & 0xffff) << 16) |
| 144 | #define TX_BD_LEN_MASK (0xffff << 16) |
| 145 | |
| 146 | #define TX_BD_STATS (TX_BD_CS | TX_BD_DF | TX_BD_LC | \ |
| 147 | TX_BD_RL | TX_BD_RETRY_MASK | TX_BD_UR) |
| 148 | |
| 149 | /* RX buffer descriptor */ |
| 150 | #define RX_BD_LC (1 << 0) /* late collision */ |
| 151 | #define RX_BD_CRC (1 << 1) /* RX CRC error */ |
| 152 | #define RX_BD_SF (1 << 2) /* short frame */ |
| 153 | #define RX_BD_TL (1 << 3) /* too long */ |
| 154 | #define RX_BD_DN (1 << 4) /* dribble nibble */ |
| 155 | #define RX_BD_IS (1 << 5) /* invalid symbol */ |
| 156 | #define RX_BD_OR (1 << 6) /* receiver overrun */ |
| 157 | #define RX_BD_MISS (1 << 7) |
| 158 | #define RX_BD_CF (1 << 8) /* control frame */ |
| 159 | #define RX_BD_WRAP (1 << 13) |
| 160 | #define RX_BD_IRQ (1 << 14) /* interrupt request enable */ |
| 161 | #define RX_BD_EMPTY (1 << 15) |
| 162 | #define RX_BD_LEN(x) (((x) & 0xffff) << 16) |
| 163 | |
| 164 | #define RX_BD_STATS (RX_BD_LC | RX_BD_CRC | RX_BD_SF | RX_BD_TL | \ |
| 165 | RX_BD_DN | RX_BD_IS | RX_BD_OR | RX_BD_MISS) |
| 166 | |
| 167 | #define ETHOC_BUFSIZ 1536 |
| 168 | #define ETHOC_ZLEN 64 |
| 169 | #define ETHOC_BD_BASE 0x400 |
| 170 | #define ETHOC_TIMEOUT (HZ / 2) |
| 171 | #define ETHOC_MII_TIMEOUT (1 + (HZ / 5)) |
| 172 | |
| 173 | /** |
| 174 | * struct ethoc - driver-private device structure |
| 175 | * @iobase: pointer to I/O memory region |
| 176 | * @membase: pointer to buffer memory region |
Thomas Chou | 0baa080 | 2009-10-04 23:33:20 +0000 | [diff] [blame] | 177 | * @dma_alloc: dma allocated buffer size |
Thomas Chou | ee02a4e | 2010-05-23 16:44:02 +0000 | [diff] [blame] | 178 | * @io_region_size: I/O memory region size |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 179 | * @num_tx: number of send buffers |
| 180 | * @cur_tx: last send buffer written |
| 181 | * @dty_tx: last buffer actually sent |
| 182 | * @num_rx: number of receive buffers |
| 183 | * @cur_rx: current receive buffer |
Jonas Bonn | f8555ad0 | 2010-06-11 02:47:35 +0000 | [diff] [blame] | 184 | * @vma: pointer to array of virtual memory addresses for buffers |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 185 | * @netdev: pointer to network device structure |
| 186 | * @napi: NAPI structure |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 187 | * @msg_enable: device state flags |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 188 | * @lock: device lock |
| 189 | * @phy: attached PHY |
| 190 | * @mdio: MDIO bus for PHY access |
| 191 | * @phy_id: address of attached PHY |
| 192 | */ |
| 193 | struct ethoc { |
| 194 | void __iomem *iobase; |
| 195 | void __iomem *membase; |
Thomas Chou | 0baa080 | 2009-10-04 23:33:20 +0000 | [diff] [blame] | 196 | int dma_alloc; |
Thomas Chou | ee02a4e | 2010-05-23 16:44:02 +0000 | [diff] [blame] | 197 | resource_size_t io_region_size; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 198 | |
| 199 | unsigned int num_tx; |
| 200 | unsigned int cur_tx; |
| 201 | unsigned int dty_tx; |
| 202 | |
| 203 | unsigned int num_rx; |
| 204 | unsigned int cur_rx; |
| 205 | |
Jonas Bonn | f8555ad0 | 2010-06-11 02:47:35 +0000 | [diff] [blame] | 206 | void** vma; |
| 207 | |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 208 | struct net_device *netdev; |
| 209 | struct napi_struct napi; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 210 | u32 msg_enable; |
| 211 | |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 212 | spinlock_t lock; |
| 213 | |
| 214 | struct phy_device *phy; |
| 215 | struct mii_bus *mdio; |
| 216 | s8 phy_id; |
| 217 | }; |
| 218 | |
| 219 | /** |
| 220 | * struct ethoc_bd - buffer descriptor |
| 221 | * @stat: buffer statistics |
| 222 | * @addr: physical memory address |
| 223 | */ |
| 224 | struct ethoc_bd { |
| 225 | u32 stat; |
| 226 | u32 addr; |
| 227 | }; |
| 228 | |
Thomas Chou | 16dd18b | 2009-10-07 14:16:42 +0000 | [diff] [blame] | 229 | static inline u32 ethoc_read(struct ethoc *dev, loff_t offset) |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 230 | { |
| 231 | return ioread32(dev->iobase + offset); |
| 232 | } |
| 233 | |
Thomas Chou | 16dd18b | 2009-10-07 14:16:42 +0000 | [diff] [blame] | 234 | static inline void ethoc_write(struct ethoc *dev, loff_t offset, u32 data) |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 235 | { |
| 236 | iowrite32(data, dev->iobase + offset); |
| 237 | } |
| 238 | |
Thomas Chou | 16dd18b | 2009-10-07 14:16:42 +0000 | [diff] [blame] | 239 | static inline void ethoc_read_bd(struct ethoc *dev, int index, |
| 240 | struct ethoc_bd *bd) |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 241 | { |
| 242 | loff_t offset = ETHOC_BD_BASE + (index * sizeof(struct ethoc_bd)); |
| 243 | bd->stat = ethoc_read(dev, offset + 0); |
| 244 | bd->addr = ethoc_read(dev, offset + 4); |
| 245 | } |
| 246 | |
Thomas Chou | 16dd18b | 2009-10-07 14:16:42 +0000 | [diff] [blame] | 247 | static inline void ethoc_write_bd(struct ethoc *dev, int index, |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 248 | const struct ethoc_bd *bd) |
| 249 | { |
| 250 | loff_t offset = ETHOC_BD_BASE + (index * sizeof(struct ethoc_bd)); |
| 251 | ethoc_write(dev, offset + 0, bd->stat); |
| 252 | ethoc_write(dev, offset + 4, bd->addr); |
| 253 | } |
| 254 | |
Thomas Chou | 16dd18b | 2009-10-07 14:16:42 +0000 | [diff] [blame] | 255 | static inline void ethoc_enable_irq(struct ethoc *dev, u32 mask) |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 256 | { |
| 257 | u32 imask = ethoc_read(dev, INT_MASK); |
| 258 | imask |= mask; |
| 259 | ethoc_write(dev, INT_MASK, imask); |
| 260 | } |
| 261 | |
Thomas Chou | 16dd18b | 2009-10-07 14:16:42 +0000 | [diff] [blame] | 262 | static inline void ethoc_disable_irq(struct ethoc *dev, u32 mask) |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 263 | { |
| 264 | u32 imask = ethoc_read(dev, INT_MASK); |
| 265 | imask &= ~mask; |
| 266 | ethoc_write(dev, INT_MASK, imask); |
| 267 | } |
| 268 | |
Thomas Chou | 16dd18b | 2009-10-07 14:16:42 +0000 | [diff] [blame] | 269 | static inline void ethoc_ack_irq(struct ethoc *dev, u32 mask) |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 270 | { |
| 271 | ethoc_write(dev, INT_SOURCE, mask); |
| 272 | } |
| 273 | |
Thomas Chou | 16dd18b | 2009-10-07 14:16:42 +0000 | [diff] [blame] | 274 | static inline void ethoc_enable_rx_and_tx(struct ethoc *dev) |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 275 | { |
| 276 | u32 mode = ethoc_read(dev, MODER); |
| 277 | mode |= MODER_RXEN | MODER_TXEN; |
| 278 | ethoc_write(dev, MODER, mode); |
| 279 | } |
| 280 | |
Thomas Chou | 16dd18b | 2009-10-07 14:16:42 +0000 | [diff] [blame] | 281 | static inline void ethoc_disable_rx_and_tx(struct ethoc *dev) |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 282 | { |
| 283 | u32 mode = ethoc_read(dev, MODER); |
| 284 | mode &= ~(MODER_RXEN | MODER_TXEN); |
| 285 | ethoc_write(dev, MODER, mode); |
| 286 | } |
| 287 | |
David S. Miller | 5cf3e03 | 2010-07-07 18:23:19 -0700 | [diff] [blame] | 288 | static int ethoc_init_ring(struct ethoc *dev, unsigned long mem_start) |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 289 | { |
| 290 | struct ethoc_bd bd; |
| 291 | int i; |
Jonas Bonn | f8555ad0 | 2010-06-11 02:47:35 +0000 | [diff] [blame] | 292 | void* vma; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 293 | |
| 294 | dev->cur_tx = 0; |
| 295 | dev->dty_tx = 0; |
| 296 | dev->cur_rx = 0; |
| 297 | |
Jonas Bonn | ee4f56b | 2010-06-11 02:47:36 +0000 | [diff] [blame] | 298 | ethoc_write(dev, TX_BD_NUM, dev->num_tx); |
| 299 | |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 300 | /* setup transmission buffers */ |
Jonas Bonn | f8555ad0 | 2010-06-11 02:47:35 +0000 | [diff] [blame] | 301 | bd.addr = mem_start; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 302 | bd.stat = TX_BD_IRQ | TX_BD_CRC; |
Jonas Bonn | f8555ad0 | 2010-06-11 02:47:35 +0000 | [diff] [blame] | 303 | vma = dev->membase; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 304 | |
| 305 | for (i = 0; i < dev->num_tx; i++) { |
| 306 | if (i == dev->num_tx - 1) |
| 307 | bd.stat |= TX_BD_WRAP; |
| 308 | |
| 309 | ethoc_write_bd(dev, i, &bd); |
| 310 | bd.addr += ETHOC_BUFSIZ; |
Jonas Bonn | f8555ad0 | 2010-06-11 02:47:35 +0000 | [diff] [blame] | 311 | |
| 312 | dev->vma[i] = vma; |
| 313 | vma += ETHOC_BUFSIZ; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 316 | bd.stat = RX_BD_EMPTY | RX_BD_IRQ; |
| 317 | |
| 318 | for (i = 0; i < dev->num_rx; i++) { |
| 319 | if (i == dev->num_rx - 1) |
| 320 | bd.stat |= RX_BD_WRAP; |
| 321 | |
| 322 | ethoc_write_bd(dev, dev->num_tx + i, &bd); |
| 323 | bd.addr += ETHOC_BUFSIZ; |
Jonas Bonn | f8555ad0 | 2010-06-11 02:47:35 +0000 | [diff] [blame] | 324 | |
| 325 | dev->vma[dev->num_tx + i] = vma; |
| 326 | vma += ETHOC_BUFSIZ; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static int ethoc_reset(struct ethoc *dev) |
| 333 | { |
| 334 | u32 mode; |
| 335 | |
| 336 | /* TODO: reset controller? */ |
| 337 | |
| 338 | ethoc_disable_rx_and_tx(dev); |
| 339 | |
| 340 | /* TODO: setup registers */ |
| 341 | |
| 342 | /* enable FCS generation and automatic padding */ |
| 343 | mode = ethoc_read(dev, MODER); |
| 344 | mode |= MODER_CRC | MODER_PAD; |
| 345 | ethoc_write(dev, MODER, mode); |
| 346 | |
| 347 | /* set full-duplex mode */ |
| 348 | mode = ethoc_read(dev, MODER); |
| 349 | mode |= MODER_FULLD; |
| 350 | ethoc_write(dev, MODER, mode); |
| 351 | ethoc_write(dev, IPGT, 0x15); |
| 352 | |
| 353 | ethoc_ack_irq(dev, INT_MASK_ALL); |
| 354 | ethoc_enable_irq(dev, INT_MASK_ALL); |
| 355 | ethoc_enable_rx_and_tx(dev); |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | static unsigned int ethoc_update_rx_stats(struct ethoc *dev, |
| 360 | struct ethoc_bd *bd) |
| 361 | { |
| 362 | struct net_device *netdev = dev->netdev; |
| 363 | unsigned int ret = 0; |
| 364 | |
| 365 | if (bd->stat & RX_BD_TL) { |
| 366 | dev_err(&netdev->dev, "RX: frame too long\n"); |
Kulikov Vasiliy | 57616ee | 2010-07-05 02:13:31 +0000 | [diff] [blame] | 367 | netdev->stats.rx_length_errors++; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 368 | ret++; |
| 369 | } |
| 370 | |
| 371 | if (bd->stat & RX_BD_SF) { |
| 372 | dev_err(&netdev->dev, "RX: frame too short\n"); |
Kulikov Vasiliy | 57616ee | 2010-07-05 02:13:31 +0000 | [diff] [blame] | 373 | netdev->stats.rx_length_errors++; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 374 | ret++; |
| 375 | } |
| 376 | |
| 377 | if (bd->stat & RX_BD_DN) { |
| 378 | dev_err(&netdev->dev, "RX: dribble nibble\n"); |
Kulikov Vasiliy | 57616ee | 2010-07-05 02:13:31 +0000 | [diff] [blame] | 379 | netdev->stats.rx_frame_errors++; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | if (bd->stat & RX_BD_CRC) { |
| 383 | dev_err(&netdev->dev, "RX: wrong CRC\n"); |
Kulikov Vasiliy | 57616ee | 2010-07-05 02:13:31 +0000 | [diff] [blame] | 384 | netdev->stats.rx_crc_errors++; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 385 | ret++; |
| 386 | } |
| 387 | |
| 388 | if (bd->stat & RX_BD_OR) { |
| 389 | dev_err(&netdev->dev, "RX: overrun\n"); |
Kulikov Vasiliy | 57616ee | 2010-07-05 02:13:31 +0000 | [diff] [blame] | 390 | netdev->stats.rx_over_errors++; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 391 | ret++; |
| 392 | } |
| 393 | |
| 394 | if (bd->stat & RX_BD_MISS) |
Kulikov Vasiliy | 57616ee | 2010-07-05 02:13:31 +0000 | [diff] [blame] | 395 | netdev->stats.rx_missed_errors++; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 396 | |
| 397 | if (bd->stat & RX_BD_LC) { |
| 398 | dev_err(&netdev->dev, "RX: late collision\n"); |
Kulikov Vasiliy | 57616ee | 2010-07-05 02:13:31 +0000 | [diff] [blame] | 399 | netdev->stats.collisions++; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 400 | ret++; |
| 401 | } |
| 402 | |
| 403 | return ret; |
| 404 | } |
| 405 | |
| 406 | static int ethoc_rx(struct net_device *dev, int limit) |
| 407 | { |
| 408 | struct ethoc *priv = netdev_priv(dev); |
| 409 | int count; |
| 410 | |
| 411 | for (count = 0; count < limit; ++count) { |
| 412 | unsigned int entry; |
| 413 | struct ethoc_bd bd; |
| 414 | |
| 415 | entry = priv->num_tx + (priv->cur_rx % priv->num_rx); |
| 416 | ethoc_read_bd(priv, entry, &bd); |
Jonas Bonn | 20f70dd | 2010-11-25 02:30:28 +0000 | [diff] [blame^] | 417 | if (bd.stat & RX_BD_EMPTY) { |
| 418 | ethoc_ack_irq(priv, INT_MASK_RX); |
| 419 | /* If packet (interrupt) came in between checking |
| 420 | * BD_EMTPY and clearing the interrupt source, then we |
| 421 | * risk missing the packet as the RX interrupt won't |
| 422 | * trigger right away when we reenable it; hence, check |
| 423 | * BD_EMTPY here again to make sure there isn't such a |
| 424 | * packet waiting for us... |
| 425 | */ |
| 426 | ethoc_read_bd(priv, entry, &bd); |
| 427 | if (bd.stat & RX_BD_EMPTY) |
| 428 | break; |
| 429 | } |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 430 | |
| 431 | if (ethoc_update_rx_stats(priv, &bd) == 0) { |
| 432 | int size = bd.stat >> 16; |
Eric Dumazet | 89d71a6 | 2009-10-13 05:34:20 +0000 | [diff] [blame] | 433 | struct sk_buff *skb; |
Thomas Chou | 050f91d | 2009-10-04 23:33:19 +0000 | [diff] [blame] | 434 | |
| 435 | size -= 4; /* strip the CRC */ |
Eric Dumazet | 89d71a6 | 2009-10-13 05:34:20 +0000 | [diff] [blame] | 436 | skb = netdev_alloc_skb_ip_align(dev, size); |
Thomas Chou | 050f91d | 2009-10-04 23:33:19 +0000 | [diff] [blame] | 437 | |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 438 | if (likely(skb)) { |
Jonas Bonn | f8555ad0 | 2010-06-11 02:47:35 +0000 | [diff] [blame] | 439 | void *src = priv->vma[entry]; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 440 | memcpy_fromio(skb_put(skb, size), src, size); |
| 441 | skb->protocol = eth_type_trans(skb, dev); |
Kulikov Vasiliy | 57616ee | 2010-07-05 02:13:31 +0000 | [diff] [blame] | 442 | dev->stats.rx_packets++; |
| 443 | dev->stats.rx_bytes += size; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 444 | netif_receive_skb(skb); |
| 445 | } else { |
| 446 | if (net_ratelimit()) |
| 447 | dev_warn(&dev->dev, "low on memory - " |
| 448 | "packet dropped\n"); |
| 449 | |
Kulikov Vasiliy | 57616ee | 2010-07-05 02:13:31 +0000 | [diff] [blame] | 450 | dev->stats.rx_dropped++; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 451 | break; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | /* clear the buffer descriptor so it can be reused */ |
| 456 | bd.stat &= ~RX_BD_STATS; |
| 457 | bd.stat |= RX_BD_EMPTY; |
| 458 | ethoc_write_bd(priv, entry, &bd); |
| 459 | priv->cur_rx++; |
| 460 | } |
| 461 | |
| 462 | return count; |
| 463 | } |
| 464 | |
| 465 | static int ethoc_update_tx_stats(struct ethoc *dev, struct ethoc_bd *bd) |
| 466 | { |
| 467 | struct net_device *netdev = dev->netdev; |
| 468 | |
| 469 | if (bd->stat & TX_BD_LC) { |
| 470 | dev_err(&netdev->dev, "TX: late collision\n"); |
Kulikov Vasiliy | 57616ee | 2010-07-05 02:13:31 +0000 | [diff] [blame] | 471 | netdev->stats.tx_window_errors++; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | if (bd->stat & TX_BD_RL) { |
| 475 | dev_err(&netdev->dev, "TX: retransmit limit\n"); |
Kulikov Vasiliy | 57616ee | 2010-07-05 02:13:31 +0000 | [diff] [blame] | 476 | netdev->stats.tx_aborted_errors++; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | if (bd->stat & TX_BD_UR) { |
| 480 | dev_err(&netdev->dev, "TX: underrun\n"); |
Kulikov Vasiliy | 57616ee | 2010-07-05 02:13:31 +0000 | [diff] [blame] | 481 | netdev->stats.tx_fifo_errors++; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | if (bd->stat & TX_BD_CS) { |
| 485 | dev_err(&netdev->dev, "TX: carrier sense lost\n"); |
Kulikov Vasiliy | 57616ee | 2010-07-05 02:13:31 +0000 | [diff] [blame] | 486 | netdev->stats.tx_carrier_errors++; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | if (bd->stat & TX_BD_STATS) |
Kulikov Vasiliy | 57616ee | 2010-07-05 02:13:31 +0000 | [diff] [blame] | 490 | netdev->stats.tx_errors++; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 491 | |
Kulikov Vasiliy | 57616ee | 2010-07-05 02:13:31 +0000 | [diff] [blame] | 492 | netdev->stats.collisions += (bd->stat >> 4) & 0xf; |
| 493 | netdev->stats.tx_bytes += bd->stat >> 16; |
| 494 | netdev->stats.tx_packets++; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static void ethoc_tx(struct net_device *dev) |
| 499 | { |
| 500 | struct ethoc *priv = netdev_priv(dev); |
| 501 | |
| 502 | spin_lock(&priv->lock); |
| 503 | |
| 504 | while (priv->dty_tx != priv->cur_tx) { |
| 505 | unsigned int entry = priv->dty_tx % priv->num_tx; |
| 506 | struct ethoc_bd bd; |
| 507 | |
| 508 | ethoc_read_bd(priv, entry, &bd); |
| 509 | if (bd.stat & TX_BD_READY) |
| 510 | break; |
| 511 | |
| 512 | entry = (++priv->dty_tx) % priv->num_tx; |
| 513 | (void)ethoc_update_tx_stats(priv, &bd); |
| 514 | } |
| 515 | |
| 516 | if ((priv->cur_tx - priv->dty_tx) <= (priv->num_tx / 2)) |
| 517 | netif_wake_queue(dev); |
| 518 | |
| 519 | ethoc_ack_irq(priv, INT_MASK_TX); |
| 520 | spin_unlock(&priv->lock); |
| 521 | } |
| 522 | |
| 523 | static irqreturn_t ethoc_interrupt(int irq, void *dev_id) |
| 524 | { |
Kulikov Vasiliy | 57616ee | 2010-07-05 02:13:31 +0000 | [diff] [blame] | 525 | struct net_device *dev = dev_id; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 526 | struct ethoc *priv = netdev_priv(dev); |
| 527 | u32 pending; |
| 528 | |
| 529 | ethoc_disable_irq(priv, INT_MASK_ALL); |
| 530 | pending = ethoc_read(priv, INT_SOURCE); |
| 531 | if (unlikely(pending == 0)) { |
| 532 | ethoc_enable_irq(priv, INT_MASK_ALL); |
| 533 | return IRQ_NONE; |
| 534 | } |
| 535 | |
Thomas Chou | 50c54a5 | 2009-10-07 14:16:43 +0000 | [diff] [blame] | 536 | ethoc_ack_irq(priv, pending); |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 537 | |
| 538 | if (pending & INT_MASK_BUSY) { |
| 539 | dev_err(&dev->dev, "packet dropped\n"); |
Kulikov Vasiliy | 57616ee | 2010-07-05 02:13:31 +0000 | [diff] [blame] | 540 | dev->stats.rx_dropped++; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | if (pending & INT_MASK_RX) { |
| 544 | if (napi_schedule_prep(&priv->napi)) |
| 545 | __napi_schedule(&priv->napi); |
| 546 | } else { |
| 547 | ethoc_enable_irq(priv, INT_MASK_RX); |
| 548 | } |
| 549 | |
| 550 | if (pending & INT_MASK_TX) |
| 551 | ethoc_tx(dev); |
| 552 | |
| 553 | ethoc_enable_irq(priv, INT_MASK_ALL & ~INT_MASK_RX); |
| 554 | return IRQ_HANDLED; |
| 555 | } |
| 556 | |
| 557 | static int ethoc_get_mac_address(struct net_device *dev, void *addr) |
| 558 | { |
| 559 | struct ethoc *priv = netdev_priv(dev); |
| 560 | u8 *mac = (u8 *)addr; |
| 561 | u32 reg; |
| 562 | |
| 563 | reg = ethoc_read(priv, MAC_ADDR0); |
| 564 | mac[2] = (reg >> 24) & 0xff; |
| 565 | mac[3] = (reg >> 16) & 0xff; |
| 566 | mac[4] = (reg >> 8) & 0xff; |
| 567 | mac[5] = (reg >> 0) & 0xff; |
| 568 | |
| 569 | reg = ethoc_read(priv, MAC_ADDR1); |
| 570 | mac[0] = (reg >> 8) & 0xff; |
| 571 | mac[1] = (reg >> 0) & 0xff; |
| 572 | |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | static int ethoc_poll(struct napi_struct *napi, int budget) |
| 577 | { |
| 578 | struct ethoc *priv = container_of(napi, struct ethoc, napi); |
| 579 | int work_done = 0; |
| 580 | |
| 581 | work_done = ethoc_rx(priv->netdev, budget); |
| 582 | if (work_done < budget) { |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 583 | napi_complete(napi); |
Adam Edvardsson | 7438a54 | 2010-11-25 02:30:27 +0000 | [diff] [blame] | 584 | ethoc_enable_irq(priv, INT_MASK_RX); |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | return work_done; |
| 588 | } |
| 589 | |
| 590 | static int ethoc_mdio_read(struct mii_bus *bus, int phy, int reg) |
| 591 | { |
| 592 | unsigned long timeout = jiffies + ETHOC_MII_TIMEOUT; |
| 593 | struct ethoc *priv = bus->priv; |
| 594 | |
| 595 | ethoc_write(priv, MIIADDRESS, MIIADDRESS_ADDR(phy, reg)); |
| 596 | ethoc_write(priv, MIICOMMAND, MIICOMMAND_READ); |
| 597 | |
| 598 | while (time_before(jiffies, timeout)) { |
| 599 | u32 status = ethoc_read(priv, MIISTATUS); |
| 600 | if (!(status & MIISTATUS_BUSY)) { |
| 601 | u32 data = ethoc_read(priv, MIIRX_DATA); |
| 602 | /* reset MII command register */ |
| 603 | ethoc_write(priv, MIICOMMAND, 0); |
| 604 | return data; |
| 605 | } |
| 606 | |
| 607 | schedule(); |
| 608 | } |
| 609 | |
| 610 | return -EBUSY; |
| 611 | } |
| 612 | |
| 613 | static int ethoc_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val) |
| 614 | { |
| 615 | unsigned long timeout = jiffies + ETHOC_MII_TIMEOUT; |
| 616 | struct ethoc *priv = bus->priv; |
| 617 | |
| 618 | ethoc_write(priv, MIIADDRESS, MIIADDRESS_ADDR(phy, reg)); |
| 619 | ethoc_write(priv, MIITX_DATA, val); |
| 620 | ethoc_write(priv, MIICOMMAND, MIICOMMAND_WRITE); |
| 621 | |
| 622 | while (time_before(jiffies, timeout)) { |
| 623 | u32 stat = ethoc_read(priv, MIISTATUS); |
Jonas Bonn | b46773d | 2010-06-11 02:47:39 +0000 | [diff] [blame] | 624 | if (!(stat & MIISTATUS_BUSY)) { |
| 625 | /* reset MII command register */ |
| 626 | ethoc_write(priv, MIICOMMAND, 0); |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 627 | return 0; |
Jonas Bonn | b46773d | 2010-06-11 02:47:39 +0000 | [diff] [blame] | 628 | } |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 629 | |
| 630 | schedule(); |
| 631 | } |
| 632 | |
| 633 | return -EBUSY; |
| 634 | } |
| 635 | |
| 636 | static int ethoc_mdio_reset(struct mii_bus *bus) |
| 637 | { |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | static void ethoc_mdio_poll(struct net_device *dev) |
| 642 | { |
| 643 | } |
| 644 | |
Jonas Bonn | f78f09f | 2010-07-26 18:45:05 -0700 | [diff] [blame] | 645 | static int __devinit ethoc_mdio_probe(struct net_device *dev) |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 646 | { |
| 647 | struct ethoc *priv = netdev_priv(dev); |
| 648 | struct phy_device *phy; |
Jonas Bonn | 637f33b8 | 2010-06-11 02:47:37 +0000 | [diff] [blame] | 649 | int err; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 650 | |
Jonas Bonn | 637f33b8 | 2010-06-11 02:47:37 +0000 | [diff] [blame] | 651 | if (priv->phy_id != -1) { |
| 652 | phy = priv->mdio->phy_map[priv->phy_id]; |
| 653 | } else { |
| 654 | phy = phy_find_first(priv->mdio); |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | if (!phy) { |
| 658 | dev_err(&dev->dev, "no PHY found\n"); |
| 659 | return -ENXIO; |
| 660 | } |
| 661 | |
Jonas Bonn | 637f33b8 | 2010-06-11 02:47:37 +0000 | [diff] [blame] | 662 | err = phy_connect_direct(dev, phy, ethoc_mdio_poll, 0, |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 663 | PHY_INTERFACE_MODE_GMII); |
Jonas Bonn | 637f33b8 | 2010-06-11 02:47:37 +0000 | [diff] [blame] | 664 | if (err) { |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 665 | dev_err(&dev->dev, "could not attach to PHY\n"); |
Jonas Bonn | 637f33b8 | 2010-06-11 02:47:37 +0000 | [diff] [blame] | 666 | return err; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | priv->phy = phy; |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static int ethoc_open(struct net_device *dev) |
| 674 | { |
| 675 | struct ethoc *priv = netdev_priv(dev); |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 676 | int ret; |
| 677 | |
| 678 | ret = request_irq(dev->irq, ethoc_interrupt, IRQF_SHARED, |
| 679 | dev->name, dev); |
| 680 | if (ret) |
| 681 | return ret; |
| 682 | |
David S. Miller | 5cf3e03 | 2010-07-07 18:23:19 -0700 | [diff] [blame] | 683 | ethoc_init_ring(priv, dev->mem_start); |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 684 | ethoc_reset(priv); |
| 685 | |
| 686 | if (netif_queue_stopped(dev)) { |
| 687 | dev_dbg(&dev->dev, " resuming queue\n"); |
| 688 | netif_wake_queue(dev); |
| 689 | } else { |
| 690 | dev_dbg(&dev->dev, " starting queue\n"); |
| 691 | netif_start_queue(dev); |
| 692 | } |
| 693 | |
| 694 | phy_start(priv->phy); |
| 695 | napi_enable(&priv->napi); |
| 696 | |
| 697 | if (netif_msg_ifup(priv)) { |
| 698 | dev_info(&dev->dev, "I/O: %08lx Memory: %08lx-%08lx\n", |
| 699 | dev->base_addr, dev->mem_start, dev->mem_end); |
| 700 | } |
| 701 | |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | static int ethoc_stop(struct net_device *dev) |
| 706 | { |
| 707 | struct ethoc *priv = netdev_priv(dev); |
| 708 | |
| 709 | napi_disable(&priv->napi); |
| 710 | |
| 711 | if (priv->phy) |
| 712 | phy_stop(priv->phy); |
| 713 | |
| 714 | ethoc_disable_rx_and_tx(priv); |
| 715 | free_irq(dev->irq, dev); |
| 716 | |
| 717 | if (!netif_queue_stopped(dev)) |
| 718 | netif_stop_queue(dev); |
| 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | static int ethoc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 724 | { |
| 725 | struct ethoc *priv = netdev_priv(dev); |
| 726 | struct mii_ioctl_data *mdio = if_mii(ifr); |
| 727 | struct phy_device *phy = NULL; |
| 728 | |
| 729 | if (!netif_running(dev)) |
| 730 | return -EINVAL; |
| 731 | |
| 732 | if (cmd != SIOCGMIIPHY) { |
| 733 | if (mdio->phy_id >= PHY_MAX_ADDR) |
| 734 | return -ERANGE; |
| 735 | |
| 736 | phy = priv->mdio->phy_map[mdio->phy_id]; |
| 737 | if (!phy) |
| 738 | return -ENODEV; |
| 739 | } else { |
| 740 | phy = priv->phy; |
| 741 | } |
| 742 | |
Richard Cochran | 28b0411 | 2010-07-17 08:48:55 +0000 | [diff] [blame] | 743 | return phy_mii_ioctl(phy, ifr, cmd); |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | static int ethoc_config(struct net_device *dev, struct ifmap *map) |
| 747 | { |
| 748 | return -ENOSYS; |
| 749 | } |
| 750 | |
| 751 | static int ethoc_set_mac_address(struct net_device *dev, void *addr) |
| 752 | { |
| 753 | struct ethoc *priv = netdev_priv(dev); |
| 754 | u8 *mac = (u8 *)addr; |
| 755 | |
| 756 | ethoc_write(priv, MAC_ADDR0, (mac[2] << 24) | (mac[3] << 16) | |
| 757 | (mac[4] << 8) | (mac[5] << 0)); |
| 758 | ethoc_write(priv, MAC_ADDR1, (mac[0] << 8) | (mac[1] << 0)); |
| 759 | |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | static void ethoc_set_multicast_list(struct net_device *dev) |
| 764 | { |
| 765 | struct ethoc *priv = netdev_priv(dev); |
| 766 | u32 mode = ethoc_read(priv, MODER); |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 767 | struct netdev_hw_addr *ha; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 768 | u32 hash[2] = { 0, 0 }; |
| 769 | |
| 770 | /* set loopback mode if requested */ |
| 771 | if (dev->flags & IFF_LOOPBACK) |
| 772 | mode |= MODER_LOOP; |
| 773 | else |
| 774 | mode &= ~MODER_LOOP; |
| 775 | |
| 776 | /* receive broadcast frames if requested */ |
| 777 | if (dev->flags & IFF_BROADCAST) |
| 778 | mode &= ~MODER_BRO; |
| 779 | else |
| 780 | mode |= MODER_BRO; |
| 781 | |
| 782 | /* enable promiscuous mode if requested */ |
| 783 | if (dev->flags & IFF_PROMISC) |
| 784 | mode |= MODER_PRO; |
| 785 | else |
| 786 | mode &= ~MODER_PRO; |
| 787 | |
| 788 | ethoc_write(priv, MODER, mode); |
| 789 | |
| 790 | /* receive multicast frames */ |
| 791 | if (dev->flags & IFF_ALLMULTI) { |
| 792 | hash[0] = 0xffffffff; |
| 793 | hash[1] = 0xffffffff; |
| 794 | } else { |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 795 | netdev_for_each_mc_addr(ha, dev) { |
| 796 | u32 crc = ether_crc(ETH_ALEN, ha->addr); |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 797 | int bit = (crc >> 26) & 0x3f; |
| 798 | hash[bit >> 5] |= 1 << (bit & 0x1f); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | ethoc_write(priv, ETH_HASH0, hash[0]); |
| 803 | ethoc_write(priv, ETH_HASH1, hash[1]); |
| 804 | } |
| 805 | |
| 806 | static int ethoc_change_mtu(struct net_device *dev, int new_mtu) |
| 807 | { |
| 808 | return -ENOSYS; |
| 809 | } |
| 810 | |
| 811 | static void ethoc_tx_timeout(struct net_device *dev) |
| 812 | { |
| 813 | struct ethoc *priv = netdev_priv(dev); |
| 814 | u32 pending = ethoc_read(priv, INT_SOURCE); |
| 815 | if (likely(pending)) |
| 816 | ethoc_interrupt(dev->irq, dev); |
| 817 | } |
| 818 | |
Stephen Hemminger | 61357325 | 2009-08-31 19:50:58 +0000 | [diff] [blame] | 819 | static netdev_tx_t ethoc_start_xmit(struct sk_buff *skb, struct net_device *dev) |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 820 | { |
| 821 | struct ethoc *priv = netdev_priv(dev); |
| 822 | struct ethoc_bd bd; |
| 823 | unsigned int entry; |
| 824 | void *dest; |
| 825 | |
| 826 | if (unlikely(skb->len > ETHOC_BUFSIZ)) { |
Kulikov Vasiliy | 57616ee | 2010-07-05 02:13:31 +0000 | [diff] [blame] | 827 | dev->stats.tx_errors++; |
Patrick McHardy | 3790c8c | 2009-06-12 03:00:35 +0000 | [diff] [blame] | 828 | goto out; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | entry = priv->cur_tx % priv->num_tx; |
| 832 | spin_lock_irq(&priv->lock); |
| 833 | priv->cur_tx++; |
| 834 | |
| 835 | ethoc_read_bd(priv, entry, &bd); |
| 836 | if (unlikely(skb->len < ETHOC_ZLEN)) |
| 837 | bd.stat |= TX_BD_PAD; |
| 838 | else |
| 839 | bd.stat &= ~TX_BD_PAD; |
| 840 | |
Jonas Bonn | f8555ad0 | 2010-06-11 02:47:35 +0000 | [diff] [blame] | 841 | dest = priv->vma[entry]; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 842 | memcpy_toio(dest, skb->data, skb->len); |
| 843 | |
| 844 | bd.stat &= ~(TX_BD_STATS | TX_BD_LEN_MASK); |
| 845 | bd.stat |= TX_BD_LEN(skb->len); |
| 846 | ethoc_write_bd(priv, entry, &bd); |
| 847 | |
| 848 | bd.stat |= TX_BD_READY; |
| 849 | ethoc_write_bd(priv, entry, &bd); |
| 850 | |
| 851 | if (priv->cur_tx == (priv->dty_tx + priv->num_tx)) { |
| 852 | dev_dbg(&dev->dev, "stopping queue\n"); |
| 853 | netif_stop_queue(dev); |
| 854 | } |
| 855 | |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 856 | spin_unlock_irq(&priv->lock); |
Patrick McHardy | 3790c8c | 2009-06-12 03:00:35 +0000 | [diff] [blame] | 857 | out: |
| 858 | dev_kfree_skb(skb); |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 859 | return NETDEV_TX_OK; |
| 860 | } |
| 861 | |
| 862 | static const struct net_device_ops ethoc_netdev_ops = { |
| 863 | .ndo_open = ethoc_open, |
| 864 | .ndo_stop = ethoc_stop, |
| 865 | .ndo_do_ioctl = ethoc_ioctl, |
| 866 | .ndo_set_config = ethoc_config, |
| 867 | .ndo_set_mac_address = ethoc_set_mac_address, |
| 868 | .ndo_set_multicast_list = ethoc_set_multicast_list, |
| 869 | .ndo_change_mtu = ethoc_change_mtu, |
| 870 | .ndo_tx_timeout = ethoc_tx_timeout, |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 871 | .ndo_start_xmit = ethoc_start_xmit, |
| 872 | }; |
| 873 | |
| 874 | /** |
| 875 | * ethoc_probe() - initialize OpenCores ethernet MAC |
| 876 | * pdev: platform device |
| 877 | */ |
Jonas Bonn | f78f09f | 2010-07-26 18:45:05 -0700 | [diff] [blame] | 878 | static int __devinit ethoc_probe(struct platform_device *pdev) |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 879 | { |
| 880 | struct net_device *netdev = NULL; |
| 881 | struct resource *res = NULL; |
| 882 | struct resource *mmio = NULL; |
| 883 | struct resource *mem = NULL; |
| 884 | struct ethoc *priv = NULL; |
| 885 | unsigned int phy; |
Jonas Bonn | c527f81 | 2010-06-11 02:47:34 +0000 | [diff] [blame] | 886 | int num_bd; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 887 | int ret = 0; |
| 888 | |
| 889 | /* allocate networking device */ |
| 890 | netdev = alloc_etherdev(sizeof(struct ethoc)); |
| 891 | if (!netdev) { |
| 892 | dev_err(&pdev->dev, "cannot allocate network device\n"); |
| 893 | ret = -ENOMEM; |
| 894 | goto out; |
| 895 | } |
| 896 | |
| 897 | SET_NETDEV_DEV(netdev, &pdev->dev); |
| 898 | platform_set_drvdata(pdev, netdev); |
| 899 | |
| 900 | /* obtain I/O memory space */ |
| 901 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 902 | if (!res) { |
| 903 | dev_err(&pdev->dev, "cannot obtain I/O memory space\n"); |
| 904 | ret = -ENXIO; |
| 905 | goto free; |
| 906 | } |
| 907 | |
| 908 | mmio = devm_request_mem_region(&pdev->dev, res->start, |
Tobias Klauser | d864584 | 2010-01-15 01:48:22 -0800 | [diff] [blame] | 909 | resource_size(res), res->name); |
Julia Lawall | 463889e | 2009-07-27 06:13:30 +0000 | [diff] [blame] | 910 | if (!mmio) { |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 911 | dev_err(&pdev->dev, "cannot request I/O memory space\n"); |
| 912 | ret = -ENXIO; |
| 913 | goto free; |
| 914 | } |
| 915 | |
| 916 | netdev->base_addr = mmio->start; |
| 917 | |
| 918 | /* obtain buffer memory space */ |
| 919 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
Thomas Chou | 0baa080 | 2009-10-04 23:33:20 +0000 | [diff] [blame] | 920 | if (res) { |
| 921 | mem = devm_request_mem_region(&pdev->dev, res->start, |
Tobias Klauser | d864584 | 2010-01-15 01:48:22 -0800 | [diff] [blame] | 922 | resource_size(res), res->name); |
Thomas Chou | 0baa080 | 2009-10-04 23:33:20 +0000 | [diff] [blame] | 923 | if (!mem) { |
| 924 | dev_err(&pdev->dev, "cannot request memory space\n"); |
| 925 | ret = -ENXIO; |
| 926 | goto free; |
| 927 | } |
| 928 | |
| 929 | netdev->mem_start = mem->start; |
| 930 | netdev->mem_end = mem->end; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 931 | } |
| 932 | |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 933 | |
| 934 | /* obtain device IRQ number */ |
| 935 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 936 | if (!res) { |
| 937 | dev_err(&pdev->dev, "cannot obtain IRQ\n"); |
| 938 | ret = -ENXIO; |
| 939 | goto free; |
| 940 | } |
| 941 | |
| 942 | netdev->irq = res->start; |
| 943 | |
| 944 | /* setup driver-private data */ |
| 945 | priv = netdev_priv(netdev); |
| 946 | priv->netdev = netdev; |
Thomas Chou | 0baa080 | 2009-10-04 23:33:20 +0000 | [diff] [blame] | 947 | priv->dma_alloc = 0; |
Thomas Chou | ee02a4e | 2010-05-23 16:44:02 +0000 | [diff] [blame] | 948 | priv->io_region_size = mmio->end - mmio->start + 1; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 949 | |
| 950 | priv->iobase = devm_ioremap_nocache(&pdev->dev, netdev->base_addr, |
Tobias Klauser | d864584 | 2010-01-15 01:48:22 -0800 | [diff] [blame] | 951 | resource_size(mmio)); |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 952 | if (!priv->iobase) { |
| 953 | dev_err(&pdev->dev, "cannot remap I/O memory space\n"); |
| 954 | ret = -ENXIO; |
| 955 | goto error; |
| 956 | } |
| 957 | |
Thomas Chou | 0baa080 | 2009-10-04 23:33:20 +0000 | [diff] [blame] | 958 | if (netdev->mem_end) { |
| 959 | priv->membase = devm_ioremap_nocache(&pdev->dev, |
Tobias Klauser | d864584 | 2010-01-15 01:48:22 -0800 | [diff] [blame] | 960 | netdev->mem_start, resource_size(mem)); |
Thomas Chou | 0baa080 | 2009-10-04 23:33:20 +0000 | [diff] [blame] | 961 | if (!priv->membase) { |
| 962 | dev_err(&pdev->dev, "cannot remap memory space\n"); |
| 963 | ret = -ENXIO; |
| 964 | goto error; |
| 965 | } |
| 966 | } else { |
| 967 | /* Allocate buffer memory */ |
Jonas Bonn | a71fba9 | 2010-06-11 02:47:40 +0000 | [diff] [blame] | 968 | priv->membase = dmam_alloc_coherent(&pdev->dev, |
Thomas Chou | 0baa080 | 2009-10-04 23:33:20 +0000 | [diff] [blame] | 969 | buffer_size, (void *)&netdev->mem_start, |
| 970 | GFP_KERNEL); |
| 971 | if (!priv->membase) { |
| 972 | dev_err(&pdev->dev, "cannot allocate %dB buffer\n", |
| 973 | buffer_size); |
| 974 | ret = -ENOMEM; |
| 975 | goto error; |
| 976 | } |
| 977 | netdev->mem_end = netdev->mem_start + buffer_size; |
| 978 | priv->dma_alloc = buffer_size; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 979 | } |
| 980 | |
Jonas Bonn | c527f81 | 2010-06-11 02:47:34 +0000 | [diff] [blame] | 981 | /* calculate the number of TX/RX buffers, maximum 128 supported */ |
| 982 | num_bd = min_t(unsigned int, |
| 983 | 128, (netdev->mem_end - netdev->mem_start + 1) / ETHOC_BUFSIZ); |
| 984 | priv->num_tx = max(2, num_bd / 4); |
| 985 | priv->num_rx = num_bd - priv->num_tx; |
| 986 | |
Jonas Bonn | f8555ad0 | 2010-06-11 02:47:35 +0000 | [diff] [blame] | 987 | priv->vma = devm_kzalloc(&pdev->dev, num_bd*sizeof(void*), GFP_KERNEL); |
| 988 | if (!priv->vma) { |
| 989 | ret = -ENOMEM; |
| 990 | goto error; |
| 991 | } |
| 992 | |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 993 | /* Allow the platform setup code to pass in a MAC address. */ |
| 994 | if (pdev->dev.platform_data) { |
Jonas Bonn | e0f4258 | 2010-11-25 02:30:25 +0000 | [diff] [blame] | 995 | struct ethoc_platform_data *pdata = pdev->dev.platform_data; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 996 | memcpy(netdev->dev_addr, pdata->hwaddr, IFHWADDRLEN); |
| 997 | priv->phy_id = pdata->phy_id; |
Jonas Bonn | e0f4258 | 2010-11-25 02:30:25 +0000 | [diff] [blame] | 998 | } else { |
| 999 | priv->phy_id = -1; |
| 1000 | |
| 1001 | #ifdef CONFIG_OF |
| 1002 | { |
| 1003 | const uint8_t* mac; |
| 1004 | |
| 1005 | mac = of_get_property(pdev->dev.of_node, |
| 1006 | "local-mac-address", |
| 1007 | NULL); |
| 1008 | if (mac) |
| 1009 | memcpy(netdev->dev_addr, mac, IFHWADDRLEN); |
| 1010 | } |
| 1011 | #endif |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | /* Check that the given MAC address is valid. If it isn't, read the |
| 1015 | * current MAC from the controller. */ |
| 1016 | if (!is_valid_ether_addr(netdev->dev_addr)) |
| 1017 | ethoc_get_mac_address(netdev, netdev->dev_addr); |
| 1018 | |
| 1019 | /* Check the MAC again for validity, if it still isn't choose and |
| 1020 | * program a random one. */ |
| 1021 | if (!is_valid_ether_addr(netdev->dev_addr)) |
| 1022 | random_ether_addr(netdev->dev_addr); |
| 1023 | |
| 1024 | ethoc_set_mac_address(netdev, netdev->dev_addr); |
| 1025 | |
| 1026 | /* register MII bus */ |
| 1027 | priv->mdio = mdiobus_alloc(); |
| 1028 | if (!priv->mdio) { |
| 1029 | ret = -ENOMEM; |
| 1030 | goto free; |
| 1031 | } |
| 1032 | |
| 1033 | priv->mdio->name = "ethoc-mdio"; |
| 1034 | snprintf(priv->mdio->id, MII_BUS_ID_SIZE, "%s-%d", |
| 1035 | priv->mdio->name, pdev->id); |
| 1036 | priv->mdio->read = ethoc_mdio_read; |
| 1037 | priv->mdio->write = ethoc_mdio_write; |
| 1038 | priv->mdio->reset = ethoc_mdio_reset; |
| 1039 | priv->mdio->priv = priv; |
| 1040 | |
| 1041 | priv->mdio->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL); |
| 1042 | if (!priv->mdio->irq) { |
| 1043 | ret = -ENOMEM; |
| 1044 | goto free_mdio; |
| 1045 | } |
| 1046 | |
| 1047 | for (phy = 0; phy < PHY_MAX_ADDR; phy++) |
| 1048 | priv->mdio->irq[phy] = PHY_POLL; |
| 1049 | |
| 1050 | ret = mdiobus_register(priv->mdio); |
| 1051 | if (ret) { |
| 1052 | dev_err(&netdev->dev, "failed to register MDIO bus\n"); |
| 1053 | goto free_mdio; |
| 1054 | } |
| 1055 | |
| 1056 | ret = ethoc_mdio_probe(netdev); |
| 1057 | if (ret) { |
| 1058 | dev_err(&netdev->dev, "failed to probe MDIO bus\n"); |
| 1059 | goto error; |
| 1060 | } |
| 1061 | |
| 1062 | ether_setup(netdev); |
| 1063 | |
| 1064 | /* setup the net_device structure */ |
| 1065 | netdev->netdev_ops = ðoc_netdev_ops; |
| 1066 | netdev->watchdog_timeo = ETHOC_TIMEOUT; |
| 1067 | netdev->features |= 0; |
| 1068 | |
| 1069 | /* setup NAPI */ |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 1070 | netif_napi_add(netdev, &priv->napi, ethoc_poll, 64); |
| 1071 | |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 1072 | spin_lock_init(&priv->lock); |
| 1073 | |
| 1074 | ret = register_netdev(netdev); |
| 1075 | if (ret < 0) { |
| 1076 | dev_err(&netdev->dev, "failed to register interface\n"); |
Thomas Chou | ee02a4e | 2010-05-23 16:44:02 +0000 | [diff] [blame] | 1077 | goto error2; |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | goto out; |
| 1081 | |
Thomas Chou | ee02a4e | 2010-05-23 16:44:02 +0000 | [diff] [blame] | 1082 | error2: |
| 1083 | netif_napi_del(&priv->napi); |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 1084 | error: |
| 1085 | mdiobus_unregister(priv->mdio); |
| 1086 | free_mdio: |
| 1087 | kfree(priv->mdio->irq); |
| 1088 | mdiobus_free(priv->mdio); |
| 1089 | free: |
| 1090 | free_netdev(netdev); |
| 1091 | out: |
| 1092 | return ret; |
| 1093 | } |
| 1094 | |
| 1095 | /** |
| 1096 | * ethoc_remove() - shutdown OpenCores ethernet MAC |
| 1097 | * @pdev: platform device |
| 1098 | */ |
Jonas Bonn | f78f09f | 2010-07-26 18:45:05 -0700 | [diff] [blame] | 1099 | static int __devexit ethoc_remove(struct platform_device *pdev) |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 1100 | { |
| 1101 | struct net_device *netdev = platform_get_drvdata(pdev); |
| 1102 | struct ethoc *priv = netdev_priv(netdev); |
| 1103 | |
| 1104 | platform_set_drvdata(pdev, NULL); |
| 1105 | |
| 1106 | if (netdev) { |
Thomas Chou | ee02a4e | 2010-05-23 16:44:02 +0000 | [diff] [blame] | 1107 | netif_napi_del(&priv->napi); |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 1108 | phy_disconnect(priv->phy); |
| 1109 | priv->phy = NULL; |
| 1110 | |
| 1111 | if (priv->mdio) { |
| 1112 | mdiobus_unregister(priv->mdio); |
| 1113 | kfree(priv->mdio->irq); |
| 1114 | mdiobus_free(priv->mdio); |
| 1115 | } |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 1116 | unregister_netdev(netdev); |
| 1117 | free_netdev(netdev); |
| 1118 | } |
| 1119 | |
| 1120 | return 0; |
| 1121 | } |
| 1122 | |
| 1123 | #ifdef CONFIG_PM |
| 1124 | static int ethoc_suspend(struct platform_device *pdev, pm_message_t state) |
| 1125 | { |
| 1126 | return -ENOSYS; |
| 1127 | } |
| 1128 | |
| 1129 | static int ethoc_resume(struct platform_device *pdev) |
| 1130 | { |
| 1131 | return -ENOSYS; |
| 1132 | } |
| 1133 | #else |
| 1134 | # define ethoc_suspend NULL |
| 1135 | # define ethoc_resume NULL |
| 1136 | #endif |
| 1137 | |
Jonas Bonn | e0f4258 | 2010-11-25 02:30:25 +0000 | [diff] [blame] | 1138 | #ifdef CONFIG_OF |
| 1139 | static struct of_device_id ethoc_match[] = { |
| 1140 | { |
| 1141 | .compatible = "opencores,ethoc", |
| 1142 | }, |
| 1143 | {}, |
| 1144 | }; |
| 1145 | MODULE_DEVICE_TABLE(of, ethoc_match); |
| 1146 | #endif |
| 1147 | |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 1148 | static struct platform_driver ethoc_driver = { |
| 1149 | .probe = ethoc_probe, |
Jonas Bonn | f78f09f | 2010-07-26 18:45:05 -0700 | [diff] [blame] | 1150 | .remove = __devexit_p(ethoc_remove), |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 1151 | .suspend = ethoc_suspend, |
| 1152 | .resume = ethoc_resume, |
| 1153 | .driver = { |
| 1154 | .name = "ethoc", |
Jonas Bonn | e0f4258 | 2010-11-25 02:30:25 +0000 | [diff] [blame] | 1155 | .owner = THIS_MODULE, |
| 1156 | #ifdef CONFIG_OF |
| 1157 | .of_match_table = ethoc_match, |
| 1158 | #endif |
Thierry Reding | a170285 | 2009-03-27 00:12:24 -0700 | [diff] [blame] | 1159 | }, |
| 1160 | }; |
| 1161 | |
| 1162 | static int __init ethoc_init(void) |
| 1163 | { |
| 1164 | return platform_driver_register(ðoc_driver); |
| 1165 | } |
| 1166 | |
| 1167 | static void __exit ethoc_exit(void) |
| 1168 | { |
| 1169 | platform_driver_unregister(ðoc_driver); |
| 1170 | } |
| 1171 | |
| 1172 | module_init(ethoc_init); |
| 1173 | module_exit(ethoc_exit); |
| 1174 | |
| 1175 | MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>"); |
| 1176 | MODULE_DESCRIPTION("OpenCores Ethernet MAC driver"); |
| 1177 | MODULE_LICENSE("GPL v2"); |
| 1178 | |