Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 1 | /* |
| 2 | * EP93xx ethernet network device driver |
| 3 | * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org> |
| 4 | * Dedicated to Marija Kulikova. |
| 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 as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
| 11 | |
H Hartley Sweeten | df2f7ec | 2010-01-08 00:53:03 -0800 | [diff] [blame^] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__ |
| 13 | |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 14 | #include <linux/dma-mapping.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/netdevice.h> |
| 18 | #include <linux/mii.h> |
| 19 | #include <linux/etherdevice.h> |
| 20 | #include <linux/ethtool.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/moduleparam.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/delay.h> |
H Hartley Sweeten | df2f7ec | 2010-01-08 00:53:03 -0800 | [diff] [blame^] | 25 | #include <linux/io.h> |
| 26 | |
| 27 | #include <mach/hardware.h> |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 28 | |
| 29 | #define DRV_MODULE_NAME "ep93xx-eth" |
| 30 | #define DRV_MODULE_VERSION "0.1" |
| 31 | |
| 32 | #define RX_QUEUE_ENTRIES 64 |
| 33 | #define TX_QUEUE_ENTRIES 8 |
| 34 | |
| 35 | #define MAX_PKT_SIZE 2044 |
| 36 | #define PKT_BUF_SIZE 2048 |
| 37 | |
| 38 | #define REG_RXCTL 0x0000 |
| 39 | #define REG_RXCTL_DEFAULT 0x00073800 |
| 40 | #define REG_TXCTL 0x0004 |
| 41 | #define REG_TXCTL_ENABLE 0x00000001 |
| 42 | #define REG_MIICMD 0x0010 |
| 43 | #define REG_MIICMD_READ 0x00008000 |
| 44 | #define REG_MIICMD_WRITE 0x00004000 |
| 45 | #define REG_MIIDATA 0x0014 |
| 46 | #define REG_MIISTS 0x0018 |
| 47 | #define REG_MIISTS_BUSY 0x00000001 |
| 48 | #define REG_SELFCTL 0x0020 |
| 49 | #define REG_SELFCTL_RESET 0x00000001 |
| 50 | #define REG_INTEN 0x0024 |
| 51 | #define REG_INTEN_TX 0x00000008 |
| 52 | #define REG_INTEN_RX 0x00000007 |
| 53 | #define REG_INTSTSP 0x0028 |
| 54 | #define REG_INTSTS_TX 0x00000008 |
| 55 | #define REG_INTSTS_RX 0x00000004 |
| 56 | #define REG_INTSTSC 0x002c |
| 57 | #define REG_AFP 0x004c |
| 58 | #define REG_INDAD0 0x0050 |
| 59 | #define REG_INDAD1 0x0051 |
| 60 | #define REG_INDAD2 0x0052 |
| 61 | #define REG_INDAD3 0x0053 |
| 62 | #define REG_INDAD4 0x0054 |
| 63 | #define REG_INDAD5 0x0055 |
| 64 | #define REG_GIINTMSK 0x0064 |
| 65 | #define REG_GIINTMSK_ENABLE 0x00008000 |
| 66 | #define REG_BMCTL 0x0080 |
| 67 | #define REG_BMCTL_ENABLE_TX 0x00000100 |
| 68 | #define REG_BMCTL_ENABLE_RX 0x00000001 |
| 69 | #define REG_BMSTS 0x0084 |
| 70 | #define REG_BMSTS_RX_ACTIVE 0x00000008 |
| 71 | #define REG_RXDQBADD 0x0090 |
| 72 | #define REG_RXDQBLEN 0x0094 |
| 73 | #define REG_RXDCURADD 0x0098 |
| 74 | #define REG_RXDENQ 0x009c |
| 75 | #define REG_RXSTSQBADD 0x00a0 |
| 76 | #define REG_RXSTSQBLEN 0x00a4 |
| 77 | #define REG_RXSTSQCURADD 0x00a8 |
| 78 | #define REG_RXSTSENQ 0x00ac |
| 79 | #define REG_TXDQBADD 0x00b0 |
| 80 | #define REG_TXDQBLEN 0x00b4 |
| 81 | #define REG_TXDQCURADD 0x00b8 |
| 82 | #define REG_TXDENQ 0x00bc |
| 83 | #define REG_TXSTSQBADD 0x00c0 |
| 84 | #define REG_TXSTSQBLEN 0x00c4 |
| 85 | #define REG_TXSTSQCURADD 0x00c8 |
| 86 | #define REG_MAXFRMLEN 0x00e8 |
| 87 | |
| 88 | struct ep93xx_rdesc |
| 89 | { |
| 90 | u32 buf_addr; |
| 91 | u32 rdesc1; |
| 92 | }; |
| 93 | |
| 94 | #define RDESC1_NSOF 0x80000000 |
| 95 | #define RDESC1_BUFFER_INDEX 0x7fff0000 |
| 96 | #define RDESC1_BUFFER_LENGTH 0x0000ffff |
| 97 | |
| 98 | struct ep93xx_rstat |
| 99 | { |
| 100 | u32 rstat0; |
| 101 | u32 rstat1; |
| 102 | }; |
| 103 | |
| 104 | #define RSTAT0_RFP 0x80000000 |
| 105 | #define RSTAT0_RWE 0x40000000 |
| 106 | #define RSTAT0_EOF 0x20000000 |
| 107 | #define RSTAT0_EOB 0x10000000 |
| 108 | #define RSTAT0_AM 0x00c00000 |
| 109 | #define RSTAT0_RX_ERR 0x00200000 |
| 110 | #define RSTAT0_OE 0x00100000 |
| 111 | #define RSTAT0_FE 0x00080000 |
| 112 | #define RSTAT0_RUNT 0x00040000 |
| 113 | #define RSTAT0_EDATA 0x00020000 |
| 114 | #define RSTAT0_CRCE 0x00010000 |
| 115 | #define RSTAT0_CRCI 0x00008000 |
| 116 | #define RSTAT0_HTI 0x00003f00 |
| 117 | #define RSTAT1_RFP 0x80000000 |
| 118 | #define RSTAT1_BUFFER_INDEX 0x7fff0000 |
| 119 | #define RSTAT1_FRAME_LENGTH 0x0000ffff |
| 120 | |
| 121 | struct ep93xx_tdesc |
| 122 | { |
| 123 | u32 buf_addr; |
| 124 | u32 tdesc1; |
| 125 | }; |
| 126 | |
| 127 | #define TDESC1_EOF 0x80000000 |
| 128 | #define TDESC1_BUFFER_INDEX 0x7fff0000 |
| 129 | #define TDESC1_BUFFER_ABORT 0x00008000 |
| 130 | #define TDESC1_BUFFER_LENGTH 0x00000fff |
| 131 | |
| 132 | struct ep93xx_tstat |
| 133 | { |
| 134 | u32 tstat0; |
| 135 | }; |
| 136 | |
| 137 | #define TSTAT0_TXFP 0x80000000 |
| 138 | #define TSTAT0_TXWE 0x40000000 |
| 139 | #define TSTAT0_FA 0x20000000 |
| 140 | #define TSTAT0_LCRS 0x10000000 |
| 141 | #define TSTAT0_OW 0x04000000 |
| 142 | #define TSTAT0_TXU 0x02000000 |
| 143 | #define TSTAT0_ECOLL 0x01000000 |
| 144 | #define TSTAT0_NCOLL 0x001f0000 |
| 145 | #define TSTAT0_BUFFER_INDEX 0x00007fff |
| 146 | |
| 147 | struct ep93xx_descs |
| 148 | { |
| 149 | struct ep93xx_rdesc rdesc[RX_QUEUE_ENTRIES]; |
| 150 | struct ep93xx_tdesc tdesc[TX_QUEUE_ENTRIES]; |
| 151 | struct ep93xx_rstat rstat[RX_QUEUE_ENTRIES]; |
| 152 | struct ep93xx_tstat tstat[TX_QUEUE_ENTRIES]; |
| 153 | }; |
| 154 | |
| 155 | struct ep93xx_priv |
| 156 | { |
| 157 | struct resource *res; |
Hartley Sweeten | 5e075cb | 2009-04-16 17:56:10 +0100 | [diff] [blame] | 158 | void __iomem *base_addr; |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 159 | int irq; |
| 160 | |
| 161 | struct ep93xx_descs *descs; |
| 162 | dma_addr_t descs_dma_addr; |
| 163 | |
| 164 | void *rx_buf[RX_QUEUE_ENTRIES]; |
| 165 | void *tx_buf[TX_QUEUE_ENTRIES]; |
| 166 | |
| 167 | spinlock_t rx_lock; |
| 168 | unsigned int rx_pointer; |
| 169 | unsigned int tx_clean_pointer; |
| 170 | unsigned int tx_pointer; |
| 171 | spinlock_t tx_pending_lock; |
| 172 | unsigned int tx_pending; |
| 173 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 174 | struct net_device *dev; |
| 175 | struct napi_struct napi; |
| 176 | |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 177 | struct net_device_stats stats; |
| 178 | |
| 179 | struct mii_if_info mii; |
| 180 | u8 mdc_divisor; |
| 181 | }; |
| 182 | |
| 183 | #define rdb(ep, off) __raw_readb((ep)->base_addr + (off)) |
| 184 | #define rdw(ep, off) __raw_readw((ep)->base_addr + (off)) |
| 185 | #define rdl(ep, off) __raw_readl((ep)->base_addr + (off)) |
| 186 | #define wrb(ep, off, val) __raw_writeb((val), (ep)->base_addr + (off)) |
| 187 | #define wrw(ep, off, val) __raw_writew((val), (ep)->base_addr + (off)) |
| 188 | #define wrl(ep, off, val) __raw_writel((val), (ep)->base_addr + (off)) |
| 189 | |
H Hartley Sweeten | df2f7ec | 2010-01-08 00:53:03 -0800 | [diff] [blame^] | 190 | static int ep93xx_mdio_read(struct net_device *dev, int phy_id, int reg) |
| 191 | { |
| 192 | struct ep93xx_priv *ep = netdev_priv(dev); |
| 193 | int data; |
| 194 | int i; |
| 195 | |
| 196 | wrl(ep, REG_MIICMD, REG_MIICMD_READ | (phy_id << 5) | reg); |
| 197 | |
| 198 | for (i = 0; i < 10; i++) { |
| 199 | if ((rdl(ep, REG_MIISTS) & REG_MIISTS_BUSY) == 0) |
| 200 | break; |
| 201 | msleep(1); |
| 202 | } |
| 203 | |
| 204 | if (i == 10) { |
| 205 | pr_info("mdio read timed out\n"); |
| 206 | data = 0xffff; |
| 207 | } else { |
| 208 | data = rdl(ep, REG_MIIDATA); |
| 209 | } |
| 210 | |
| 211 | return data; |
| 212 | } |
| 213 | |
| 214 | static void ep93xx_mdio_write(struct net_device *dev, int phy_id, int reg, int data) |
| 215 | { |
| 216 | struct ep93xx_priv *ep = netdev_priv(dev); |
| 217 | int i; |
| 218 | |
| 219 | wrl(ep, REG_MIIDATA, data); |
| 220 | wrl(ep, REG_MIICMD, REG_MIICMD_WRITE | (phy_id << 5) | reg); |
| 221 | |
| 222 | for (i = 0; i < 10; i++) { |
| 223 | if ((rdl(ep, REG_MIISTS) & REG_MIISTS_BUSY) == 0) |
| 224 | break; |
| 225 | msleep(1); |
| 226 | } |
| 227 | |
| 228 | if (i == 10) |
| 229 | pr_info("mdio write timed out\n"); |
| 230 | } |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 231 | |
| 232 | static struct net_device_stats *ep93xx_get_stats(struct net_device *dev) |
| 233 | { |
| 234 | struct ep93xx_priv *ep = netdev_priv(dev); |
| 235 | return &(ep->stats); |
| 236 | } |
| 237 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 238 | static int ep93xx_rx(struct net_device *dev, int processed, int budget) |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 239 | { |
| 240 | struct ep93xx_priv *ep = netdev_priv(dev); |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 241 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 242 | while (processed < budget) { |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 243 | int entry; |
| 244 | struct ep93xx_rstat *rstat; |
| 245 | u32 rstat0; |
| 246 | u32 rstat1; |
| 247 | int length; |
| 248 | struct sk_buff *skb; |
| 249 | |
| 250 | entry = ep->rx_pointer; |
| 251 | rstat = ep->descs->rstat + entry; |
Lennert Buytenhek | 2d38cab | 2006-10-30 19:52:31 +0100 | [diff] [blame] | 252 | |
| 253 | rstat0 = rstat->rstat0; |
| 254 | rstat1 = rstat->rstat1; |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 255 | if (!(rstat0 & RSTAT0_RFP) || !(rstat1 & RSTAT1_RFP)) |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 256 | break; |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 257 | |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 258 | rstat->rstat0 = 0; |
| 259 | rstat->rstat1 = 0; |
| 260 | |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 261 | if (!(rstat0 & RSTAT0_EOF)) |
H Hartley Sweeten | df2f7ec | 2010-01-08 00:53:03 -0800 | [diff] [blame^] | 262 | pr_crit("not end-of-frame %.8x %.8x\n", rstat0, rstat1); |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 263 | if (!(rstat0 & RSTAT0_EOB)) |
H Hartley Sweeten | df2f7ec | 2010-01-08 00:53:03 -0800 | [diff] [blame^] | 264 | pr_crit("not end-of-buffer %.8x %.8x\n", rstat0, rstat1); |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 265 | if ((rstat1 & RSTAT1_BUFFER_INDEX) >> 16 != entry) |
H Hartley Sweeten | df2f7ec | 2010-01-08 00:53:03 -0800 | [diff] [blame^] | 266 | pr_crit("entry mismatch %.8x %.8x\n", rstat0, rstat1); |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 267 | |
| 268 | if (!(rstat0 & RSTAT0_RWE)) { |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 269 | ep->stats.rx_errors++; |
| 270 | if (rstat0 & RSTAT0_OE) |
| 271 | ep->stats.rx_fifo_errors++; |
| 272 | if (rstat0 & RSTAT0_FE) |
| 273 | ep->stats.rx_frame_errors++; |
| 274 | if (rstat0 & (RSTAT0_RUNT | RSTAT0_EDATA)) |
| 275 | ep->stats.rx_length_errors++; |
| 276 | if (rstat0 & RSTAT0_CRCE) |
| 277 | ep->stats.rx_crc_errors++; |
| 278 | goto err; |
| 279 | } |
| 280 | |
| 281 | length = rstat1 & RSTAT1_FRAME_LENGTH; |
| 282 | if (length > MAX_PKT_SIZE) { |
H Hartley Sweeten | df2f7ec | 2010-01-08 00:53:03 -0800 | [diff] [blame^] | 283 | pr_notice("invalid length %.8x %.8x\n", rstat0, rstat1); |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 284 | goto err; |
| 285 | } |
| 286 | |
| 287 | /* Strip FCS. */ |
| 288 | if (rstat0 & RSTAT0_CRCI) |
| 289 | length -= 4; |
| 290 | |
| 291 | skb = dev_alloc_skb(length + 2); |
| 292 | if (likely(skb != NULL)) { |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 293 | skb_reserve(skb, 2); |
FUJITA Tomonori | 5d23a1d | 2009-05-27 15:10:42 +0000 | [diff] [blame] | 294 | dma_sync_single_for_cpu(NULL, ep->descs->rdesc[entry].buf_addr, |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 295 | length, DMA_FROM_DEVICE); |
David S. Miller | 8c7b7fa | 2007-07-10 22:08:12 -0700 | [diff] [blame] | 296 | skb_copy_to_linear_data(skb, ep->rx_buf[entry], length); |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 297 | skb_put(skb, length); |
| 298 | skb->protocol = eth_type_trans(skb, dev); |
| 299 | |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 300 | netif_receive_skb(skb); |
| 301 | |
| 302 | ep->stats.rx_packets++; |
| 303 | ep->stats.rx_bytes += length; |
| 304 | } else { |
| 305 | ep->stats.rx_dropped++; |
| 306 | } |
| 307 | |
| 308 | err: |
| 309 | ep->rx_pointer = (entry + 1) & (RX_QUEUE_ENTRIES - 1); |
| 310 | processed++; |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | if (processed) { |
| 314 | wrw(ep, REG_RXDENQ, processed); |
| 315 | wrw(ep, REG_RXSTSENQ, processed); |
| 316 | } |
| 317 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 318 | return processed; |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | static int ep93xx_have_more_rx(struct ep93xx_priv *ep) |
| 322 | { |
Lennert Buytenhek | 2d38cab | 2006-10-30 19:52:31 +0100 | [diff] [blame] | 323 | struct ep93xx_rstat *rstat = ep->descs->rstat + ep->rx_pointer; |
| 324 | return !!((rstat->rstat0 & RSTAT0_RFP) && (rstat->rstat1 & RSTAT1_RFP)); |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 325 | } |
| 326 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 327 | static int ep93xx_poll(struct napi_struct *napi, int budget) |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 328 | { |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 329 | struct ep93xx_priv *ep = container_of(napi, struct ep93xx_priv, napi); |
| 330 | struct net_device *dev = ep->dev; |
| 331 | int rx = 0; |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 332 | |
| 333 | poll_some_more: |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 334 | rx = ep93xx_rx(dev, rx, budget); |
| 335 | if (rx < budget) { |
| 336 | int more = 0; |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 337 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 338 | spin_lock_irq(&ep->rx_lock); |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 339 | __napi_complete(napi); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 340 | wrl(ep, REG_INTEN, REG_INTEN_TX | REG_INTEN_RX); |
| 341 | if (ep93xx_have_more_rx(ep)) { |
| 342 | wrl(ep, REG_INTEN, REG_INTEN_TX); |
| 343 | wrl(ep, REG_INTSTSP, REG_INTSTS_RX); |
| 344 | more = 1; |
| 345 | } |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 346 | spin_unlock_irq(&ep->rx_lock); |
| 347 | |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 348 | if (more && napi_reschedule(napi)) |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 349 | goto poll_some_more; |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 350 | } |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 351 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 352 | return rx; |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | static int ep93xx_xmit(struct sk_buff *skb, struct net_device *dev) |
| 356 | { |
| 357 | struct ep93xx_priv *ep = netdev_priv(dev); |
| 358 | int entry; |
| 359 | |
Lennert Buytenhek | 79c356f | 2006-10-30 19:52:54 +0100 | [diff] [blame] | 360 | if (unlikely(skb->len > MAX_PKT_SIZE)) { |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 361 | ep->stats.tx_dropped++; |
| 362 | dev_kfree_skb(skb); |
| 363 | return NETDEV_TX_OK; |
| 364 | } |
| 365 | |
| 366 | entry = ep->tx_pointer; |
| 367 | ep->tx_pointer = (ep->tx_pointer + 1) & (TX_QUEUE_ENTRIES - 1); |
| 368 | |
| 369 | ep->descs->tdesc[entry].tdesc1 = |
| 370 | TDESC1_EOF | (entry << 16) | (skb->len & 0xfff); |
| 371 | skb_copy_and_csum_dev(skb, ep->tx_buf[entry]); |
FUJITA Tomonori | 5d23a1d | 2009-05-27 15:10:42 +0000 | [diff] [blame] | 372 | dma_sync_single_for_cpu(NULL, ep->descs->tdesc[entry].buf_addr, |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 373 | skb->len, DMA_TO_DEVICE); |
| 374 | dev_kfree_skb(skb); |
| 375 | |
| 376 | dev->trans_start = jiffies; |
| 377 | |
| 378 | spin_lock_irq(&ep->tx_pending_lock); |
| 379 | ep->tx_pending++; |
| 380 | if (ep->tx_pending == TX_QUEUE_ENTRIES) |
| 381 | netif_stop_queue(dev); |
| 382 | spin_unlock_irq(&ep->tx_pending_lock); |
| 383 | |
| 384 | wrl(ep, REG_TXDENQ, 1); |
| 385 | |
| 386 | return NETDEV_TX_OK; |
| 387 | } |
| 388 | |
| 389 | static void ep93xx_tx_complete(struct net_device *dev) |
| 390 | { |
| 391 | struct ep93xx_priv *ep = netdev_priv(dev); |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 392 | int wake; |
| 393 | |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 394 | wake = 0; |
| 395 | |
| 396 | spin_lock(&ep->tx_pending_lock); |
| 397 | while (1) { |
| 398 | int entry; |
| 399 | struct ep93xx_tstat *tstat; |
| 400 | u32 tstat0; |
| 401 | |
| 402 | entry = ep->tx_clean_pointer; |
| 403 | tstat = ep->descs->tstat + entry; |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 404 | |
| 405 | tstat0 = tstat->tstat0; |
Lennert Buytenhek | 2d38cab | 2006-10-30 19:52:31 +0100 | [diff] [blame] | 406 | if (!(tstat0 & TSTAT0_TXFP)) |
| 407 | break; |
| 408 | |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 409 | tstat->tstat0 = 0; |
| 410 | |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 411 | if (tstat0 & TSTAT0_FA) |
H Hartley Sweeten | df2f7ec | 2010-01-08 00:53:03 -0800 | [diff] [blame^] | 412 | pr_crit("frame aborted %.8x\n", tstat0); |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 413 | if ((tstat0 & TSTAT0_BUFFER_INDEX) != entry) |
H Hartley Sweeten | df2f7ec | 2010-01-08 00:53:03 -0800 | [diff] [blame^] | 414 | pr_crit("entry mismatch %.8x\n", tstat0); |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 415 | |
| 416 | if (tstat0 & TSTAT0_TXWE) { |
| 417 | int length = ep->descs->tdesc[entry].tdesc1 & 0xfff; |
| 418 | |
| 419 | ep->stats.tx_packets++; |
| 420 | ep->stats.tx_bytes += length; |
| 421 | } else { |
| 422 | ep->stats.tx_errors++; |
| 423 | } |
| 424 | |
| 425 | if (tstat0 & TSTAT0_OW) |
| 426 | ep->stats.tx_window_errors++; |
| 427 | if (tstat0 & TSTAT0_TXU) |
| 428 | ep->stats.tx_fifo_errors++; |
| 429 | ep->stats.collisions += (tstat0 >> 16) & 0x1f; |
| 430 | |
| 431 | ep->tx_clean_pointer = (entry + 1) & (TX_QUEUE_ENTRIES - 1); |
| 432 | if (ep->tx_pending == TX_QUEUE_ENTRIES) |
| 433 | wake = 1; |
| 434 | ep->tx_pending--; |
| 435 | } |
| 436 | spin_unlock(&ep->tx_pending_lock); |
| 437 | |
| 438 | if (wake) |
| 439 | netif_wake_queue(dev); |
| 440 | } |
| 441 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 442 | static irqreturn_t ep93xx_irq(int irq, void *dev_id) |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 443 | { |
| 444 | struct net_device *dev = dev_id; |
| 445 | struct ep93xx_priv *ep = netdev_priv(dev); |
| 446 | u32 status; |
| 447 | |
| 448 | status = rdl(ep, REG_INTSTSC); |
| 449 | if (status == 0) |
| 450 | return IRQ_NONE; |
| 451 | |
| 452 | if (status & REG_INTSTS_RX) { |
| 453 | spin_lock(&ep->rx_lock); |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 454 | if (likely(napi_schedule_prep(&ep->napi))) { |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 455 | wrl(ep, REG_INTEN, REG_INTEN_TX); |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 456 | __napi_schedule(&ep->napi); |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 457 | } |
| 458 | spin_unlock(&ep->rx_lock); |
| 459 | } |
| 460 | |
| 461 | if (status & REG_INTSTS_TX) |
| 462 | ep93xx_tx_complete(dev); |
| 463 | |
| 464 | return IRQ_HANDLED; |
| 465 | } |
| 466 | |
| 467 | static void ep93xx_free_buffers(struct ep93xx_priv *ep) |
| 468 | { |
| 469 | int i; |
| 470 | |
| 471 | for (i = 0; i < RX_QUEUE_ENTRIES; i += 2) { |
| 472 | dma_addr_t d; |
| 473 | |
| 474 | d = ep->descs->rdesc[i].buf_addr; |
| 475 | if (d) |
| 476 | dma_unmap_single(NULL, d, PAGE_SIZE, DMA_FROM_DEVICE); |
| 477 | |
| 478 | if (ep->rx_buf[i] != NULL) |
| 479 | free_page((unsigned long)ep->rx_buf[i]); |
| 480 | } |
| 481 | |
| 482 | for (i = 0; i < TX_QUEUE_ENTRIES; i += 2) { |
| 483 | dma_addr_t d; |
| 484 | |
| 485 | d = ep->descs->tdesc[i].buf_addr; |
| 486 | if (d) |
| 487 | dma_unmap_single(NULL, d, PAGE_SIZE, DMA_TO_DEVICE); |
| 488 | |
| 489 | if (ep->tx_buf[i] != NULL) |
| 490 | free_page((unsigned long)ep->tx_buf[i]); |
| 491 | } |
| 492 | |
| 493 | dma_free_coherent(NULL, sizeof(struct ep93xx_descs), ep->descs, |
| 494 | ep->descs_dma_addr); |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * The hardware enforces a sub-2K maximum packet size, so we put |
| 499 | * two buffers on every hardware page. |
| 500 | */ |
| 501 | static int ep93xx_alloc_buffers(struct ep93xx_priv *ep) |
| 502 | { |
| 503 | int i; |
| 504 | |
| 505 | ep->descs = dma_alloc_coherent(NULL, sizeof(struct ep93xx_descs), |
| 506 | &ep->descs_dma_addr, GFP_KERNEL | GFP_DMA); |
| 507 | if (ep->descs == NULL) |
| 508 | return 1; |
| 509 | |
| 510 | for (i = 0; i < RX_QUEUE_ENTRIES; i += 2) { |
| 511 | void *page; |
| 512 | dma_addr_t d; |
| 513 | |
| 514 | page = (void *)__get_free_page(GFP_KERNEL | GFP_DMA); |
| 515 | if (page == NULL) |
| 516 | goto err; |
| 517 | |
| 518 | d = dma_map_single(NULL, page, PAGE_SIZE, DMA_FROM_DEVICE); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 519 | if (dma_mapping_error(NULL, d)) { |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 520 | free_page((unsigned long)page); |
| 521 | goto err; |
| 522 | } |
| 523 | |
| 524 | ep->rx_buf[i] = page; |
| 525 | ep->descs->rdesc[i].buf_addr = d; |
| 526 | ep->descs->rdesc[i].rdesc1 = (i << 16) | PKT_BUF_SIZE; |
| 527 | |
| 528 | ep->rx_buf[i + 1] = page + PKT_BUF_SIZE; |
| 529 | ep->descs->rdesc[i + 1].buf_addr = d + PKT_BUF_SIZE; |
| 530 | ep->descs->rdesc[i + 1].rdesc1 = ((i + 1) << 16) | PKT_BUF_SIZE; |
| 531 | } |
| 532 | |
| 533 | for (i = 0; i < TX_QUEUE_ENTRIES; i += 2) { |
| 534 | void *page; |
| 535 | dma_addr_t d; |
| 536 | |
| 537 | page = (void *)__get_free_page(GFP_KERNEL | GFP_DMA); |
| 538 | if (page == NULL) |
| 539 | goto err; |
| 540 | |
| 541 | d = dma_map_single(NULL, page, PAGE_SIZE, DMA_TO_DEVICE); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 542 | if (dma_mapping_error(NULL, d)) { |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 543 | free_page((unsigned long)page); |
| 544 | goto err; |
| 545 | } |
| 546 | |
| 547 | ep->tx_buf[i] = page; |
| 548 | ep->descs->tdesc[i].buf_addr = d; |
| 549 | |
| 550 | ep->tx_buf[i + 1] = page + PKT_BUF_SIZE; |
| 551 | ep->descs->tdesc[i + 1].buf_addr = d + PKT_BUF_SIZE; |
| 552 | } |
| 553 | |
| 554 | return 0; |
| 555 | |
| 556 | err: |
| 557 | ep93xx_free_buffers(ep); |
| 558 | return 1; |
| 559 | } |
| 560 | |
| 561 | static int ep93xx_start_hw(struct net_device *dev) |
| 562 | { |
| 563 | struct ep93xx_priv *ep = netdev_priv(dev); |
| 564 | unsigned long addr; |
| 565 | int i; |
| 566 | |
| 567 | wrl(ep, REG_SELFCTL, REG_SELFCTL_RESET); |
| 568 | for (i = 0; i < 10; i++) { |
| 569 | if ((rdl(ep, REG_SELFCTL) & REG_SELFCTL_RESET) == 0) |
| 570 | break; |
| 571 | msleep(1); |
| 572 | } |
| 573 | |
| 574 | if (i == 10) { |
H Hartley Sweeten | df2f7ec | 2010-01-08 00:53:03 -0800 | [diff] [blame^] | 575 | pr_crit("hw failed to reset\n"); |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 576 | return 1; |
| 577 | } |
| 578 | |
| 579 | wrl(ep, REG_SELFCTL, ((ep->mdc_divisor - 1) << 9)); |
| 580 | |
| 581 | /* Does the PHY support preamble suppress? */ |
| 582 | if ((ep93xx_mdio_read(dev, ep->mii.phy_id, MII_BMSR) & 0x0040) != 0) |
| 583 | wrl(ep, REG_SELFCTL, ((ep->mdc_divisor - 1) << 9) | (1 << 8)); |
| 584 | |
| 585 | /* Receive descriptor ring. */ |
| 586 | addr = ep->descs_dma_addr + offsetof(struct ep93xx_descs, rdesc); |
| 587 | wrl(ep, REG_RXDQBADD, addr); |
| 588 | wrl(ep, REG_RXDCURADD, addr); |
| 589 | wrw(ep, REG_RXDQBLEN, RX_QUEUE_ENTRIES * sizeof(struct ep93xx_rdesc)); |
| 590 | |
| 591 | /* Receive status ring. */ |
| 592 | addr = ep->descs_dma_addr + offsetof(struct ep93xx_descs, rstat); |
| 593 | wrl(ep, REG_RXSTSQBADD, addr); |
| 594 | wrl(ep, REG_RXSTSQCURADD, addr); |
| 595 | wrw(ep, REG_RXSTSQBLEN, RX_QUEUE_ENTRIES * sizeof(struct ep93xx_rstat)); |
| 596 | |
| 597 | /* Transmit descriptor ring. */ |
| 598 | addr = ep->descs_dma_addr + offsetof(struct ep93xx_descs, tdesc); |
| 599 | wrl(ep, REG_TXDQBADD, addr); |
| 600 | wrl(ep, REG_TXDQCURADD, addr); |
| 601 | wrw(ep, REG_TXDQBLEN, TX_QUEUE_ENTRIES * sizeof(struct ep93xx_tdesc)); |
| 602 | |
| 603 | /* Transmit status ring. */ |
| 604 | addr = ep->descs_dma_addr + offsetof(struct ep93xx_descs, tstat); |
| 605 | wrl(ep, REG_TXSTSQBADD, addr); |
| 606 | wrl(ep, REG_TXSTSQCURADD, addr); |
| 607 | wrw(ep, REG_TXSTSQBLEN, TX_QUEUE_ENTRIES * sizeof(struct ep93xx_tstat)); |
| 608 | |
| 609 | wrl(ep, REG_BMCTL, REG_BMCTL_ENABLE_TX | REG_BMCTL_ENABLE_RX); |
| 610 | wrl(ep, REG_INTEN, REG_INTEN_TX | REG_INTEN_RX); |
| 611 | wrl(ep, REG_GIINTMSK, 0); |
| 612 | |
| 613 | for (i = 0; i < 10; i++) { |
| 614 | if ((rdl(ep, REG_BMSTS) & REG_BMSTS_RX_ACTIVE) != 0) |
| 615 | break; |
| 616 | msleep(1); |
| 617 | } |
| 618 | |
| 619 | if (i == 10) { |
H Hartley Sweeten | df2f7ec | 2010-01-08 00:53:03 -0800 | [diff] [blame^] | 620 | pr_crit("hw failed to start\n"); |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 621 | return 1; |
| 622 | } |
| 623 | |
| 624 | wrl(ep, REG_RXDENQ, RX_QUEUE_ENTRIES); |
| 625 | wrl(ep, REG_RXSTSENQ, RX_QUEUE_ENTRIES); |
| 626 | |
| 627 | wrb(ep, REG_INDAD0, dev->dev_addr[0]); |
| 628 | wrb(ep, REG_INDAD1, dev->dev_addr[1]); |
| 629 | wrb(ep, REG_INDAD2, dev->dev_addr[2]); |
| 630 | wrb(ep, REG_INDAD3, dev->dev_addr[3]); |
| 631 | wrb(ep, REG_INDAD4, dev->dev_addr[4]); |
| 632 | wrb(ep, REG_INDAD5, dev->dev_addr[5]); |
| 633 | wrl(ep, REG_AFP, 0); |
| 634 | |
| 635 | wrl(ep, REG_MAXFRMLEN, (MAX_PKT_SIZE << 16) | MAX_PKT_SIZE); |
| 636 | |
| 637 | wrl(ep, REG_RXCTL, REG_RXCTL_DEFAULT); |
| 638 | wrl(ep, REG_TXCTL, REG_TXCTL_ENABLE); |
| 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | static void ep93xx_stop_hw(struct net_device *dev) |
| 644 | { |
| 645 | struct ep93xx_priv *ep = netdev_priv(dev); |
| 646 | int i; |
| 647 | |
| 648 | wrl(ep, REG_SELFCTL, REG_SELFCTL_RESET); |
| 649 | for (i = 0; i < 10; i++) { |
| 650 | if ((rdl(ep, REG_SELFCTL) & REG_SELFCTL_RESET) == 0) |
| 651 | break; |
| 652 | msleep(1); |
| 653 | } |
| 654 | |
| 655 | if (i == 10) |
H Hartley Sweeten | df2f7ec | 2010-01-08 00:53:03 -0800 | [diff] [blame^] | 656 | pr_crit("hw failed to reset\n"); |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | static int ep93xx_open(struct net_device *dev) |
| 660 | { |
| 661 | struct ep93xx_priv *ep = netdev_priv(dev); |
| 662 | int err; |
| 663 | |
| 664 | if (ep93xx_alloc_buffers(ep)) |
| 665 | return -ENOMEM; |
| 666 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 667 | napi_enable(&ep->napi); |
| 668 | |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 669 | if (ep93xx_start_hw(dev)) { |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 670 | napi_disable(&ep->napi); |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 671 | ep93xx_free_buffers(ep); |
| 672 | return -EIO; |
| 673 | } |
| 674 | |
| 675 | spin_lock_init(&ep->rx_lock); |
| 676 | ep->rx_pointer = 0; |
| 677 | ep->tx_clean_pointer = 0; |
| 678 | ep->tx_pointer = 0; |
| 679 | spin_lock_init(&ep->tx_pending_lock); |
| 680 | ep->tx_pending = 0; |
| 681 | |
| 682 | err = request_irq(ep->irq, ep93xx_irq, IRQF_SHARED, dev->name, dev); |
| 683 | if (err) { |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 684 | napi_disable(&ep->napi); |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 685 | ep93xx_stop_hw(dev); |
| 686 | ep93xx_free_buffers(ep); |
| 687 | return err; |
| 688 | } |
| 689 | |
| 690 | wrl(ep, REG_GIINTMSK, REG_GIINTMSK_ENABLE); |
| 691 | |
| 692 | netif_start_queue(dev); |
| 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | static int ep93xx_close(struct net_device *dev) |
| 698 | { |
| 699 | struct ep93xx_priv *ep = netdev_priv(dev); |
| 700 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 701 | napi_disable(&ep->napi); |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 702 | netif_stop_queue(dev); |
| 703 | |
| 704 | wrl(ep, REG_GIINTMSK, 0); |
| 705 | free_irq(ep->irq, dev); |
| 706 | ep93xx_stop_hw(dev); |
| 707 | ep93xx_free_buffers(ep); |
| 708 | |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | static int ep93xx_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 713 | { |
| 714 | struct ep93xx_priv *ep = netdev_priv(dev); |
| 715 | struct mii_ioctl_data *data = if_mii(ifr); |
| 716 | |
| 717 | return generic_mii_ioctl(&ep->mii, data, cmd, NULL); |
| 718 | } |
| 719 | |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 720 | static void ep93xx_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) |
| 721 | { |
| 722 | strcpy(info->driver, DRV_MODULE_NAME); |
| 723 | strcpy(info->version, DRV_MODULE_VERSION); |
| 724 | } |
| 725 | |
| 726 | static int ep93xx_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 727 | { |
| 728 | struct ep93xx_priv *ep = netdev_priv(dev); |
| 729 | return mii_ethtool_gset(&ep->mii, cmd); |
| 730 | } |
| 731 | |
| 732 | static int ep93xx_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 733 | { |
| 734 | struct ep93xx_priv *ep = netdev_priv(dev); |
| 735 | return mii_ethtool_sset(&ep->mii, cmd); |
| 736 | } |
| 737 | |
| 738 | static int ep93xx_nway_reset(struct net_device *dev) |
| 739 | { |
| 740 | struct ep93xx_priv *ep = netdev_priv(dev); |
| 741 | return mii_nway_restart(&ep->mii); |
| 742 | } |
| 743 | |
| 744 | static u32 ep93xx_get_link(struct net_device *dev) |
| 745 | { |
| 746 | struct ep93xx_priv *ep = netdev_priv(dev); |
| 747 | return mii_link_ok(&ep->mii); |
| 748 | } |
| 749 | |
Stephen Hemminger | 0fc0b73 | 2009-09-02 01:03:33 -0700 | [diff] [blame] | 750 | static const struct ethtool_ops ep93xx_ethtool_ops = { |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 751 | .get_drvinfo = ep93xx_get_drvinfo, |
| 752 | .get_settings = ep93xx_get_settings, |
| 753 | .set_settings = ep93xx_set_settings, |
| 754 | .nway_reset = ep93xx_nway_reset, |
| 755 | .get_link = ep93xx_get_link, |
| 756 | }; |
| 757 | |
Alexander Beregalov | 9aa7b30 | 2009-04-09 17:28:06 +0000 | [diff] [blame] | 758 | static const struct net_device_ops ep93xx_netdev_ops = { |
| 759 | .ndo_open = ep93xx_open, |
| 760 | .ndo_stop = ep93xx_close, |
| 761 | .ndo_start_xmit = ep93xx_xmit, |
| 762 | .ndo_get_stats = ep93xx_get_stats, |
| 763 | .ndo_do_ioctl = ep93xx_ioctl, |
| 764 | .ndo_validate_addr = eth_validate_addr, |
| 765 | .ndo_change_mtu = eth_change_mtu, |
| 766 | .ndo_set_mac_address = eth_mac_addr, |
| 767 | }; |
| 768 | |
| 769 | static struct net_device *ep93xx_dev_alloc(struct ep93xx_eth_data *data) |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 770 | { |
| 771 | struct net_device *dev; |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 772 | |
| 773 | dev = alloc_etherdev(sizeof(struct ep93xx_priv)); |
| 774 | if (dev == NULL) |
| 775 | return NULL; |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 776 | |
| 777 | memcpy(dev->dev_addr, data->dev_addr, ETH_ALEN); |
| 778 | |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 779 | dev->ethtool_ops = &ep93xx_ethtool_ops; |
Alexander Beregalov | 9aa7b30 | 2009-04-09 17:28:06 +0000 | [diff] [blame] | 780 | dev->netdev_ops = &ep93xx_netdev_ops; |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 781 | |
| 782 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 783 | |
| 784 | return dev; |
| 785 | } |
| 786 | |
| 787 | |
| 788 | static int ep93xx_eth_remove(struct platform_device *pdev) |
| 789 | { |
| 790 | struct net_device *dev; |
| 791 | struct ep93xx_priv *ep; |
| 792 | |
| 793 | dev = platform_get_drvdata(pdev); |
| 794 | if (dev == NULL) |
| 795 | return 0; |
| 796 | platform_set_drvdata(pdev, NULL); |
| 797 | |
| 798 | ep = netdev_priv(dev); |
| 799 | |
| 800 | /* @@@ Force down. */ |
| 801 | unregister_netdev(dev); |
| 802 | ep93xx_free_buffers(ep); |
| 803 | |
| 804 | if (ep->base_addr != NULL) |
| 805 | iounmap(ep->base_addr); |
| 806 | |
| 807 | if (ep->res != NULL) { |
| 808 | release_resource(ep->res); |
| 809 | kfree(ep->res); |
| 810 | } |
| 811 | |
| 812 | free_netdev(dev); |
| 813 | |
| 814 | return 0; |
| 815 | } |
| 816 | |
| 817 | static int ep93xx_eth_probe(struct platform_device *pdev) |
| 818 | { |
| 819 | struct ep93xx_eth_data *data; |
| 820 | struct net_device *dev; |
| 821 | struct ep93xx_priv *ep; |
H Hartley Sweeten | df2f7ec | 2010-01-08 00:53:03 -0800 | [diff] [blame^] | 822 | struct resource *mem; |
| 823 | int irq; |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 824 | int err; |
| 825 | |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 826 | if (pdev == NULL) |
| 827 | return -ENODEV; |
Yan Burman | ebf5112 | 2006-12-19 13:08:48 -0800 | [diff] [blame] | 828 | data = pdev->dev.platform_data; |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 829 | |
H Hartley Sweeten | df2f7ec | 2010-01-08 00:53:03 -0800 | [diff] [blame^] | 830 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 831 | irq = platform_get_irq(pdev, 0); |
| 832 | if (!mem || irq < 0) |
| 833 | return -ENXIO; |
| 834 | |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 835 | dev = ep93xx_dev_alloc(data); |
| 836 | if (dev == NULL) { |
| 837 | err = -ENOMEM; |
| 838 | goto err_out; |
| 839 | } |
| 840 | ep = netdev_priv(dev); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 841 | ep->dev = dev; |
| 842 | netif_napi_add(dev, &ep->napi, ep93xx_poll, 64); |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 843 | |
| 844 | platform_set_drvdata(pdev, dev); |
| 845 | |
H Hartley Sweeten | df2f7ec | 2010-01-08 00:53:03 -0800 | [diff] [blame^] | 846 | ep->res = request_mem_region(mem->start, resource_size(mem), |
| 847 | dev_name(&pdev->dev)); |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 848 | if (ep->res == NULL) { |
| 849 | dev_err(&pdev->dev, "Could not reserve memory region\n"); |
| 850 | err = -ENOMEM; |
| 851 | goto err_out; |
| 852 | } |
| 853 | |
H Hartley Sweeten | df2f7ec | 2010-01-08 00:53:03 -0800 | [diff] [blame^] | 854 | ep->base_addr = ioremap(mem->start, resource_size(mem)); |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 855 | if (ep->base_addr == NULL) { |
| 856 | dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n"); |
| 857 | err = -EIO; |
| 858 | goto err_out; |
| 859 | } |
H Hartley Sweeten | df2f7ec | 2010-01-08 00:53:03 -0800 | [diff] [blame^] | 860 | ep->irq = irq; |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 861 | |
| 862 | ep->mii.phy_id = data->phy_id; |
| 863 | ep->mii.phy_id_mask = 0x1f; |
| 864 | ep->mii.reg_num_mask = 0x1f; |
| 865 | ep->mii.dev = dev; |
| 866 | ep->mii.mdio_read = ep93xx_mdio_read; |
| 867 | ep->mii.mdio_write = ep93xx_mdio_write; |
| 868 | ep->mdc_divisor = 40; /* Max HCLK 100 MHz, min MDIO clk 2.5 MHz. */ |
| 869 | |
Florian Fainelli | 3c91c7a | 2009-11-28 13:57:00 +0000 | [diff] [blame] | 870 | if (is_zero_ether_addr(dev->dev_addr)) |
| 871 | random_ether_addr(dev->dev_addr); |
| 872 | |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 873 | err = register_netdev(dev); |
| 874 | if (err) { |
| 875 | dev_err(&pdev->dev, "Failed to register netdev\n"); |
| 876 | goto err_out; |
| 877 | } |
| 878 | |
H Hartley Sweeten | df2f7ec | 2010-01-08 00:53:03 -0800 | [diff] [blame^] | 879 | printk(KERN_INFO "%s: ep93xx on-chip ethernet, IRQ %d, %pM\n", |
| 880 | dev->name, ep->irq, dev->dev_addr); |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 881 | |
| 882 | return 0; |
| 883 | |
| 884 | err_out: |
| 885 | ep93xx_eth_remove(pdev); |
| 886 | return err; |
| 887 | } |
| 888 | |
| 889 | |
| 890 | static struct platform_driver ep93xx_eth_driver = { |
| 891 | .probe = ep93xx_eth_probe, |
| 892 | .remove = ep93xx_eth_remove, |
| 893 | .driver = { |
| 894 | .name = "ep93xx-eth", |
Kay Sievers | 72abb46 | 2008-04-18 13:50:44 -0700 | [diff] [blame] | 895 | .owner = THIS_MODULE, |
Lennert Buytenhek | 1d22e05 | 2006-09-22 02:28:13 +0200 | [diff] [blame] | 896 | }, |
| 897 | }; |
| 898 | |
| 899 | static int __init ep93xx_eth_init_module(void) |
| 900 | { |
| 901 | printk(KERN_INFO DRV_MODULE_NAME " version " DRV_MODULE_VERSION " loading\n"); |
| 902 | return platform_driver_register(&ep93xx_eth_driver); |
| 903 | } |
| 904 | |
| 905 | static void __exit ep93xx_eth_cleanup_module(void) |
| 906 | { |
| 907 | platform_driver_unregister(&ep93xx_eth_driver); |
| 908 | } |
| 909 | |
| 910 | module_init(ep93xx_eth_init_module); |
| 911 | module_exit(ep93xx_eth_cleanup_module); |
| 912 | MODULE_LICENSE("GPL"); |
Kay Sievers | 72abb46 | 2008-04-18 13:50:44 -0700 | [diff] [blame] | 913 | MODULE_ALIAS("platform:ep93xx-eth"); |