Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/net/am79c961.c |
| 3 | * |
| 4 | * by Russell King <rmk@arm.linux.org.uk> 1995-2001. |
| 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 | * Derived from various things including skeleton.c |
| 11 | * |
| 12 | * This is a special driver for the am79c961A Lance chip used in the |
| 13 | * Intel (formally Digital Equipment Corp) EBSA110 platform. Please |
| 14 | * note that this can not be built as a module (it doesn't make sense). |
| 15 | */ |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/ioport.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/slab.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/netdevice.h> |
| 24 | #include <linux/etherdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/delay.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/crc32.h> |
| 28 | #include <linux/bitops.h> |
Russell King | 37bb30e | 2005-10-31 17:14:57 +0000 | [diff] [blame] | 29 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 31 | #include <mach/hardware.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <asm/io.h> |
Russell King | 37bb30e | 2005-10-31 17:14:57 +0000 | [diff] [blame] | 33 | #include <asm/system.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | #define TX_BUFFERS 15 |
| 36 | #define RX_BUFFERS 25 |
| 37 | |
| 38 | #include "am79c961a.h" |
| 39 | |
| 40 | static irqreturn_t |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 41 | am79c961_interrupt (int irq, void *dev_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | static unsigned int net_debug = NET_DEBUG; |
| 44 | |
| 45 | static const char version[] = |
| 46 | "am79c961 ethernet driver (C) 1995-2001 Russell King v0.04\n"; |
| 47 | |
| 48 | /* --------------------------------------------------------------------------- */ |
| 49 | |
| 50 | #ifdef __arm__ |
| 51 | static void write_rreg(u_long base, u_int reg, u_int val) |
| 52 | { |
| 53 | __asm__( |
| 54 | "str%?h %1, [%2] @ NET_RAP\n\t" |
| 55 | "str%?h %0, [%2, #-4] @ NET_RDP" |
| 56 | : |
| 57 | : "r" (val), "r" (reg), "r" (ISAIO_BASE + 0x0464)); |
| 58 | } |
| 59 | |
| 60 | static inline unsigned short read_rreg(u_long base_addr, u_int reg) |
| 61 | { |
| 62 | unsigned short v; |
| 63 | __asm__( |
| 64 | "str%?h %1, [%2] @ NET_RAP\n\t" |
| 65 | "ldr%?h %0, [%2, #-4] @ NET_RDP" |
| 66 | : "=r" (v) |
| 67 | : "r" (reg), "r" (ISAIO_BASE + 0x0464)); |
| 68 | return v; |
| 69 | } |
| 70 | |
| 71 | static inline void write_ireg(u_long base, u_int reg, u_int val) |
| 72 | { |
| 73 | __asm__( |
| 74 | "str%?h %1, [%2] @ NET_RAP\n\t" |
| 75 | "str%?h %0, [%2, #8] @ NET_IDP" |
| 76 | : |
| 77 | : "r" (val), "r" (reg), "r" (ISAIO_BASE + 0x0464)); |
| 78 | } |
| 79 | |
| 80 | static inline unsigned short read_ireg(u_long base_addr, u_int reg) |
| 81 | { |
| 82 | u_short v; |
| 83 | __asm__( |
| 84 | "str%?h %1, [%2] @ NAT_RAP\n\t" |
Russell King | 0e3a64e | 2005-10-02 18:02:25 +0100 | [diff] [blame] | 85 | "ldr%?h %0, [%2, #8] @ NET_IDP\n\t" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | : "=r" (v) |
| 87 | : "r" (reg), "r" (ISAIO_BASE + 0x0464)); |
| 88 | return v; |
| 89 | } |
| 90 | |
| 91 | #define am_writeword(dev,off,val) __raw_writew(val, ISAMEM_BASE + ((off) << 1)) |
| 92 | #define am_readword(dev,off) __raw_readw(ISAMEM_BASE + ((off) << 1)) |
| 93 | |
| 94 | static inline void |
| 95 | am_writebuffer(struct net_device *dev, u_int offset, unsigned char *buf, unsigned int length) |
| 96 | { |
| 97 | offset = ISAMEM_BASE + (offset << 1); |
| 98 | length = (length + 1) & ~1; |
| 99 | if ((int)buf & 2) { |
| 100 | __asm__ __volatile__("str%?h %2, [%0], #4" |
| 101 | : "=&r" (offset) : "0" (offset), "r" (buf[0] | (buf[1] << 8))); |
| 102 | buf += 2; |
| 103 | length -= 2; |
| 104 | } |
| 105 | while (length > 8) { |
| 106 | unsigned int tmp, tmp2; |
| 107 | __asm__ __volatile__( |
| 108 | "ldm%?ia %1!, {%2, %3}\n\t" |
| 109 | "str%?h %2, [%0], #4\n\t" |
| 110 | "mov%? %2, %2, lsr #16\n\t" |
| 111 | "str%?h %2, [%0], #4\n\t" |
| 112 | "str%?h %3, [%0], #4\n\t" |
| 113 | "mov%? %3, %3, lsr #16\n\t" |
| 114 | "str%?h %3, [%0], #4" |
| 115 | : "=&r" (offset), "=&r" (buf), "=r" (tmp), "=r" (tmp2) |
| 116 | : "0" (offset), "1" (buf)); |
| 117 | length -= 8; |
| 118 | } |
| 119 | while (length > 0) { |
| 120 | __asm__ __volatile__("str%?h %2, [%0], #4" |
| 121 | : "=&r" (offset) : "0" (offset), "r" (buf[0] | (buf[1] << 8))); |
| 122 | buf += 2; |
| 123 | length -= 2; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | static inline void |
| 128 | am_readbuffer(struct net_device *dev, u_int offset, unsigned char *buf, unsigned int length) |
| 129 | { |
| 130 | offset = ISAMEM_BASE + (offset << 1); |
| 131 | length = (length + 1) & ~1; |
| 132 | if ((int)buf & 2) { |
| 133 | unsigned int tmp; |
| 134 | __asm__ __volatile__( |
| 135 | "ldr%?h %2, [%0], #4\n\t" |
| 136 | "str%?b %2, [%1], #1\n\t" |
| 137 | "mov%? %2, %2, lsr #8\n\t" |
| 138 | "str%?b %2, [%1], #1" |
| 139 | : "=&r" (offset), "=&r" (buf), "=r" (tmp): "0" (offset), "1" (buf)); |
| 140 | length -= 2; |
| 141 | } |
| 142 | while (length > 8) { |
| 143 | unsigned int tmp, tmp2, tmp3; |
| 144 | __asm__ __volatile__( |
| 145 | "ldr%?h %2, [%0], #4\n\t" |
| 146 | "ldr%?h %3, [%0], #4\n\t" |
| 147 | "orr%? %2, %2, %3, lsl #16\n\t" |
| 148 | "ldr%?h %3, [%0], #4\n\t" |
| 149 | "ldr%?h %4, [%0], #4\n\t" |
| 150 | "orr%? %3, %3, %4, lsl #16\n\t" |
| 151 | "stm%?ia %1!, {%2, %3}" |
| 152 | : "=&r" (offset), "=&r" (buf), "=r" (tmp), "=r" (tmp2), "=r" (tmp3) |
| 153 | : "0" (offset), "1" (buf)); |
| 154 | length -= 8; |
| 155 | } |
| 156 | while (length > 0) { |
| 157 | unsigned int tmp; |
| 158 | __asm__ __volatile__( |
| 159 | "ldr%?h %2, [%0], #4\n\t" |
| 160 | "str%?b %2, [%1], #1\n\t" |
| 161 | "mov%? %2, %2, lsr #8\n\t" |
| 162 | "str%?b %2, [%1], #1" |
| 163 | : "=&r" (offset), "=&r" (buf), "=r" (tmp) : "0" (offset), "1" (buf)); |
| 164 | length -= 2; |
| 165 | } |
| 166 | } |
| 167 | #else |
| 168 | #error Not compatible |
| 169 | #endif |
| 170 | |
| 171 | static int |
| 172 | am79c961_ramtest(struct net_device *dev, unsigned int val) |
| 173 | { |
| 174 | unsigned char *buffer = kmalloc (65536, GFP_KERNEL); |
| 175 | int i, error = 0, errorcount = 0; |
| 176 | |
| 177 | if (!buffer) |
| 178 | return 0; |
| 179 | memset (buffer, val, 65536); |
| 180 | am_writebuffer(dev, 0, buffer, 65536); |
| 181 | memset (buffer, val ^ 255, 65536); |
| 182 | am_readbuffer(dev, 0, buffer, 65536); |
| 183 | for (i = 0; i < 65536; i++) { |
| 184 | if (buffer[i] != val && !error) { |
| 185 | printk ("%s: buffer error (%02X %02X) %05X - ", dev->name, val, buffer[i], i); |
| 186 | error = 1; |
| 187 | errorcount ++; |
| 188 | } else if (error && buffer[i] == val) { |
| 189 | printk ("%05X\n", i); |
| 190 | error = 0; |
| 191 | } |
| 192 | } |
| 193 | if (error) |
| 194 | printk ("10000\n"); |
| 195 | kfree (buffer); |
| 196 | return errorcount; |
| 197 | } |
| 198 | |
| 199 | static void |
| 200 | am79c961_init_for_open(struct net_device *dev) |
| 201 | { |
| 202 | struct dev_priv *priv = netdev_priv(dev); |
| 203 | unsigned long flags; |
| 204 | unsigned char *p; |
| 205 | u_int hdr_addr, first_free_addr; |
| 206 | int i; |
| 207 | |
| 208 | /* |
| 209 | * Stop the chip. |
| 210 | */ |
Uwe Kleine-König | fb22d72 | 2009-01-22 23:29:42 +0100 | [diff] [blame] | 211 | spin_lock_irqsave(&priv->chip_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | write_rreg (dev->base_addr, CSR0, CSR0_BABL|CSR0_CERR|CSR0_MISS|CSR0_MERR|CSR0_TINT|CSR0_RINT|CSR0_STOP); |
Uwe Kleine-König | fb22d72 | 2009-01-22 23:29:42 +0100 | [diff] [blame] | 213 | spin_unlock_irqrestore(&priv->chip_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 215 | write_ireg (dev->base_addr, 5, 0x00a0); /* Receive address LED */ |
| 216 | write_ireg (dev->base_addr, 6, 0x0081); /* Collision LED */ |
| 217 | write_ireg (dev->base_addr, 7, 0x0090); /* XMIT LED */ |
| 218 | write_ireg (dev->base_addr, 2, 0x0000); /* MODE register selects media */ |
| 219 | |
| 220 | for (i = LADRL; i <= LADRH; i++) |
| 221 | write_rreg (dev->base_addr, i, 0); |
| 222 | |
| 223 | for (i = PADRL, p = dev->dev_addr; i <= PADRH; i++, p += 2) |
| 224 | write_rreg (dev->base_addr, i, p[0] | (p[1] << 8)); |
| 225 | |
| 226 | i = MODE_PORT_10BT; |
| 227 | if (dev->flags & IFF_PROMISC) |
| 228 | i |= MODE_PROMISC; |
| 229 | |
| 230 | write_rreg (dev->base_addr, MODE, i); |
| 231 | write_rreg (dev->base_addr, POLLINT, 0); |
| 232 | write_rreg (dev->base_addr, SIZERXR, -RX_BUFFERS); |
| 233 | write_rreg (dev->base_addr, SIZETXR, -TX_BUFFERS); |
| 234 | |
| 235 | first_free_addr = RX_BUFFERS * 8 + TX_BUFFERS * 8 + 16; |
| 236 | hdr_addr = 0; |
| 237 | |
| 238 | priv->rxhead = 0; |
| 239 | priv->rxtail = 0; |
| 240 | priv->rxhdr = hdr_addr; |
| 241 | |
| 242 | for (i = 0; i < RX_BUFFERS; i++) { |
| 243 | priv->rxbuffer[i] = first_free_addr; |
| 244 | am_writeword (dev, hdr_addr, first_free_addr); |
| 245 | am_writeword (dev, hdr_addr + 2, RMD_OWN); |
| 246 | am_writeword (dev, hdr_addr + 4, (-1600)); |
| 247 | am_writeword (dev, hdr_addr + 6, 0); |
| 248 | first_free_addr += 1600; |
| 249 | hdr_addr += 8; |
| 250 | } |
| 251 | priv->txhead = 0; |
| 252 | priv->txtail = 0; |
| 253 | priv->txhdr = hdr_addr; |
| 254 | for (i = 0; i < TX_BUFFERS; i++) { |
| 255 | priv->txbuffer[i] = first_free_addr; |
| 256 | am_writeword (dev, hdr_addr, first_free_addr); |
| 257 | am_writeword (dev, hdr_addr + 2, TMD_STP|TMD_ENP); |
| 258 | am_writeword (dev, hdr_addr + 4, 0xf000); |
| 259 | am_writeword (dev, hdr_addr + 6, 0); |
| 260 | first_free_addr += 1600; |
| 261 | hdr_addr += 8; |
| 262 | } |
| 263 | |
| 264 | write_rreg (dev->base_addr, BASERXL, priv->rxhdr); |
| 265 | write_rreg (dev->base_addr, BASERXH, 0); |
| 266 | write_rreg (dev->base_addr, BASETXL, priv->txhdr); |
| 267 | write_rreg (dev->base_addr, BASERXH, 0); |
| 268 | write_rreg (dev->base_addr, CSR0, CSR0_STOP); |
| 269 | write_rreg (dev->base_addr, CSR3, CSR3_IDONM|CSR3_BABLM|CSR3_DXSUFLO); |
| 270 | write_rreg (dev->base_addr, CSR4, CSR4_APAD_XMIT|CSR4_MFCOM|CSR4_RCVCCOM|CSR4_TXSTRTM|CSR4_JABM); |
| 271 | write_rreg (dev->base_addr, CSR0, CSR0_IENA|CSR0_STRT); |
| 272 | } |
| 273 | |
| 274 | static void am79c961_timer(unsigned long data) |
| 275 | { |
| 276 | struct net_device *dev = (struct net_device *)data; |
| 277 | struct dev_priv *priv = netdev_priv(dev); |
| 278 | unsigned int lnkstat, carrier; |
| 279 | |
| 280 | lnkstat = read_ireg(dev->base_addr, ISALED0) & ISALED0_LNKST; |
| 281 | carrier = netif_carrier_ok(dev); |
| 282 | |
Russell King | 37bb30e | 2005-10-31 17:14:57 +0000 | [diff] [blame] | 283 | if (lnkstat && !carrier) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | netif_carrier_on(dev); |
Russell King | 37bb30e | 2005-10-31 17:14:57 +0000 | [diff] [blame] | 285 | printk("%s: link up\n", dev->name); |
| 286 | } else if (!lnkstat && carrier) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | netif_carrier_off(dev); |
Russell King | 37bb30e | 2005-10-31 17:14:57 +0000 | [diff] [blame] | 288 | printk("%s: link down\n", dev->name); |
| 289 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Russell King | 487fd4e | 2005-10-02 18:12:03 +0100 | [diff] [blame] | 291 | mod_timer(&priv->timer, jiffies + msecs_to_jiffies(500)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Open/initialize the board. |
| 296 | */ |
| 297 | static int |
| 298 | am79c961_open(struct net_device *dev) |
| 299 | { |
| 300 | struct dev_priv *priv = netdev_priv(dev); |
| 301 | int ret; |
| 302 | |
| 303 | memset (&priv->stats, 0, sizeof (priv->stats)); |
| 304 | |
| 305 | ret = request_irq(dev->irq, am79c961_interrupt, 0, dev->name, dev); |
| 306 | if (ret) |
| 307 | return ret; |
| 308 | |
| 309 | am79c961_init_for_open(dev); |
| 310 | |
| 311 | netif_carrier_off(dev); |
| 312 | |
| 313 | priv->timer.expires = jiffies; |
| 314 | add_timer(&priv->timer); |
| 315 | |
| 316 | netif_start_queue(dev); |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * The inverse routine to am79c961_open(). |
| 323 | */ |
| 324 | static int |
| 325 | am79c961_close(struct net_device *dev) |
| 326 | { |
| 327 | struct dev_priv *priv = netdev_priv(dev); |
| 328 | unsigned long flags; |
| 329 | |
| 330 | del_timer_sync(&priv->timer); |
| 331 | |
| 332 | netif_stop_queue(dev); |
| 333 | netif_carrier_off(dev); |
| 334 | |
Uwe Kleine-König | fb22d72 | 2009-01-22 23:29:42 +0100 | [diff] [blame] | 335 | spin_lock_irqsave(&priv->chip_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | write_rreg (dev->base_addr, CSR0, CSR0_STOP); |
| 337 | write_rreg (dev->base_addr, CSR3, CSR3_MASKALL); |
Uwe Kleine-König | fb22d72 | 2009-01-22 23:29:42 +0100 | [diff] [blame] | 338 | spin_unlock_irqrestore(&priv->chip_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
| 340 | free_irq (dev->irq, dev); |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * Get the current statistics. |
| 347 | */ |
| 348 | static struct net_device_stats *am79c961_getstats (struct net_device *dev) |
| 349 | { |
| 350 | struct dev_priv *priv = netdev_priv(dev); |
| 351 | return &priv->stats; |
| 352 | } |
| 353 | |
| 354 | static void am79c961_mc_hash(struct dev_mc_list *dmi, unsigned short *hash) |
| 355 | { |
| 356 | if (dmi->dmi_addrlen == ETH_ALEN && dmi->dmi_addr[0] & 0x01) { |
| 357 | int idx, bit; |
| 358 | u32 crc; |
| 359 | |
| 360 | crc = ether_crc_le(ETH_ALEN, dmi->dmi_addr); |
| 361 | |
| 362 | idx = crc >> 30; |
| 363 | bit = (crc >> 26) & 15; |
| 364 | |
| 365 | hash[idx] |= 1 << bit; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * Set or clear promiscuous/multicast mode filter for this adapter. |
| 371 | */ |
| 372 | static void am79c961_setmulticastlist (struct net_device *dev) |
| 373 | { |
| 374 | struct dev_priv *priv = netdev_priv(dev); |
| 375 | unsigned long flags; |
| 376 | unsigned short multi_hash[4], mode; |
| 377 | int i, stopped; |
| 378 | |
| 379 | mode = MODE_PORT_10BT; |
| 380 | |
| 381 | if (dev->flags & IFF_PROMISC) { |
| 382 | mode |= MODE_PROMISC; |
| 383 | } else if (dev->flags & IFF_ALLMULTI) { |
| 384 | memset(multi_hash, 0xff, sizeof(multi_hash)); |
| 385 | } else { |
| 386 | struct dev_mc_list *dmi; |
| 387 | |
| 388 | memset(multi_hash, 0x00, sizeof(multi_hash)); |
| 389 | |
| 390 | for (dmi = dev->mc_list; dmi; dmi = dmi->next) |
| 391 | am79c961_mc_hash(dmi, multi_hash); |
| 392 | } |
| 393 | |
Uwe Kleine-König | fb22d72 | 2009-01-22 23:29:42 +0100 | [diff] [blame] | 394 | spin_lock_irqsave(&priv->chip_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
| 396 | stopped = read_rreg(dev->base_addr, CSR0) & CSR0_STOP; |
| 397 | |
| 398 | if (!stopped) { |
| 399 | /* |
| 400 | * Put the chip into suspend mode |
| 401 | */ |
| 402 | write_rreg(dev->base_addr, CTRL1, CTRL1_SPND); |
| 403 | |
| 404 | /* |
| 405 | * Spin waiting for chip to report suspend mode |
| 406 | */ |
| 407 | while ((read_rreg(dev->base_addr, CTRL1) & CTRL1_SPND) == 0) { |
Uwe Kleine-König | fb22d72 | 2009-01-22 23:29:42 +0100 | [diff] [blame] | 408 | spin_unlock_irqrestore(&priv->chip_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | nop(); |
Uwe Kleine-König | fb22d72 | 2009-01-22 23:29:42 +0100 | [diff] [blame] | 410 | spin_lock_irqsave(&priv->chip_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
| 414 | /* |
| 415 | * Update the multicast hash table |
| 416 | */ |
Denis Cheng | ff8ac60 | 2007-09-02 18:30:18 +0800 | [diff] [blame] | 417 | for (i = 0; i < ARRAY_SIZE(multi_hash); i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | write_rreg(dev->base_addr, i + LADRL, multi_hash[i]); |
| 419 | |
| 420 | /* |
| 421 | * Write the mode register |
| 422 | */ |
| 423 | write_rreg(dev->base_addr, MODE, mode); |
| 424 | |
| 425 | if (!stopped) { |
| 426 | /* |
| 427 | * Put the chip back into running mode |
| 428 | */ |
| 429 | write_rreg(dev->base_addr, CTRL1, 0); |
| 430 | } |
| 431 | |
Uwe Kleine-König | fb22d72 | 2009-01-22 23:29:42 +0100 | [diff] [blame] | 432 | spin_unlock_irqrestore(&priv->chip_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | static void am79c961_timeout(struct net_device *dev) |
| 436 | { |
| 437 | printk(KERN_WARNING "%s: transmit timed out, network cable problem?\n", |
| 438 | dev->name); |
| 439 | |
| 440 | /* |
| 441 | * ought to do some setup of the tx side here |
| 442 | */ |
| 443 | |
| 444 | netif_wake_queue(dev); |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * Transmit a packet |
| 449 | */ |
| 450 | static int |
| 451 | am79c961_sendpacket(struct sk_buff *skb, struct net_device *dev) |
| 452 | { |
| 453 | struct dev_priv *priv = netdev_priv(dev); |
| 454 | unsigned int hdraddr, bufaddr; |
| 455 | unsigned int head; |
| 456 | unsigned long flags; |
| 457 | |
| 458 | head = priv->txhead; |
| 459 | hdraddr = priv->txhdr + (head << 3); |
| 460 | bufaddr = priv->txbuffer[head]; |
| 461 | head += 1; |
| 462 | if (head >= TX_BUFFERS) |
| 463 | head = 0; |
| 464 | |
| 465 | am_writebuffer (dev, bufaddr, skb->data, skb->len); |
| 466 | am_writeword (dev, hdraddr + 4, -skb->len); |
| 467 | am_writeword (dev, hdraddr + 2, TMD_OWN|TMD_STP|TMD_ENP); |
| 468 | priv->txhead = head; |
| 469 | |
Uwe Kleine-König | fb22d72 | 2009-01-22 23:29:42 +0100 | [diff] [blame] | 470 | spin_lock_irqsave(&priv->chip_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | write_rreg (dev->base_addr, CSR0, CSR0_TDMD|CSR0_IENA); |
| 472 | dev->trans_start = jiffies; |
Uwe Kleine-König | fb22d72 | 2009-01-22 23:29:42 +0100 | [diff] [blame] | 473 | spin_unlock_irqrestore(&priv->chip_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
| 475 | /* |
| 476 | * If the next packet is owned by the ethernet device, |
| 477 | * then the tx ring is full and we can't add another |
| 478 | * packet. |
| 479 | */ |
| 480 | if (am_readword(dev, priv->txhdr + (priv->txhead << 3) + 2) & TMD_OWN) |
| 481 | netif_stop_queue(dev); |
| 482 | |
| 483 | dev_kfree_skb(skb); |
| 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * If we have a good packet(s), get it/them out of the buffers. |
| 490 | */ |
| 491 | static void |
| 492 | am79c961_rx(struct net_device *dev, struct dev_priv *priv) |
| 493 | { |
| 494 | do { |
| 495 | struct sk_buff *skb; |
| 496 | u_int hdraddr; |
| 497 | u_int pktaddr; |
| 498 | u_int status; |
| 499 | int len; |
| 500 | |
| 501 | hdraddr = priv->rxhdr + (priv->rxtail << 3); |
| 502 | pktaddr = priv->rxbuffer[priv->rxtail]; |
| 503 | |
| 504 | status = am_readword (dev, hdraddr + 2); |
| 505 | if (status & RMD_OWN) /* do we own it? */ |
| 506 | break; |
| 507 | |
| 508 | priv->rxtail ++; |
| 509 | if (priv->rxtail >= RX_BUFFERS) |
| 510 | priv->rxtail = 0; |
| 511 | |
| 512 | if ((status & (RMD_ERR|RMD_STP|RMD_ENP)) != (RMD_STP|RMD_ENP)) { |
| 513 | am_writeword (dev, hdraddr + 2, RMD_OWN); |
| 514 | priv->stats.rx_errors ++; |
| 515 | if (status & RMD_ERR) { |
| 516 | if (status & RMD_FRAM) |
| 517 | priv->stats.rx_frame_errors ++; |
| 518 | if (status & RMD_CRC) |
| 519 | priv->stats.rx_crc_errors ++; |
| 520 | } else if (status & RMD_STP) |
| 521 | priv->stats.rx_length_errors ++; |
| 522 | continue; |
| 523 | } |
| 524 | |
| 525 | len = am_readword(dev, hdraddr + 6); |
| 526 | skb = dev_alloc_skb(len + 2); |
| 527 | |
| 528 | if (skb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | skb_reserve(skb, 2); |
| 530 | |
| 531 | am_readbuffer(dev, pktaddr, skb_put(skb, len), len); |
| 532 | am_writeword(dev, hdraddr + 2, RMD_OWN); |
| 533 | skb->protocol = eth_type_trans(skb, dev); |
| 534 | netif_rx(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | priv->stats.rx_bytes += len; |
| 536 | priv->stats.rx_packets ++; |
| 537 | } else { |
| 538 | am_writeword (dev, hdraddr + 2, RMD_OWN); |
| 539 | printk (KERN_WARNING "%s: memory squeeze, dropping packet.\n", dev->name); |
| 540 | priv->stats.rx_dropped ++; |
| 541 | break; |
| 542 | } |
| 543 | } while (1); |
| 544 | } |
| 545 | |
| 546 | /* |
| 547 | * Update stats for the transmitted packet |
| 548 | */ |
| 549 | static void |
| 550 | am79c961_tx(struct net_device *dev, struct dev_priv *priv) |
| 551 | { |
| 552 | do { |
| 553 | short len; |
| 554 | u_int hdraddr; |
| 555 | u_int status; |
| 556 | |
| 557 | hdraddr = priv->txhdr + (priv->txtail << 3); |
| 558 | status = am_readword (dev, hdraddr + 2); |
| 559 | if (status & TMD_OWN) |
| 560 | break; |
| 561 | |
| 562 | priv->txtail ++; |
| 563 | if (priv->txtail >= TX_BUFFERS) |
| 564 | priv->txtail = 0; |
| 565 | |
| 566 | if (status & TMD_ERR) { |
| 567 | u_int status2; |
| 568 | |
| 569 | priv->stats.tx_errors ++; |
| 570 | |
| 571 | status2 = am_readword (dev, hdraddr + 6); |
| 572 | |
| 573 | /* |
| 574 | * Clear the error byte |
| 575 | */ |
| 576 | am_writeword (dev, hdraddr + 6, 0); |
| 577 | |
| 578 | if (status2 & TST_RTRY) |
| 579 | priv->stats.collisions += 16; |
| 580 | if (status2 & TST_LCOL) |
| 581 | priv->stats.tx_window_errors ++; |
| 582 | if (status2 & TST_LCAR) |
| 583 | priv->stats.tx_carrier_errors ++; |
| 584 | if (status2 & TST_UFLO) |
| 585 | priv->stats.tx_fifo_errors ++; |
| 586 | continue; |
| 587 | } |
| 588 | priv->stats.tx_packets ++; |
| 589 | len = am_readword (dev, hdraddr + 4); |
| 590 | priv->stats.tx_bytes += -len; |
| 591 | } while (priv->txtail != priv->txhead); |
| 592 | |
| 593 | netif_wake_queue(dev); |
| 594 | } |
| 595 | |
| 596 | static irqreturn_t |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 597 | am79c961_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | { |
| 599 | struct net_device *dev = (struct net_device *)dev_id; |
| 600 | struct dev_priv *priv = netdev_priv(dev); |
| 601 | u_int status, n = 100; |
| 602 | int handled = 0; |
| 603 | |
| 604 | do { |
| 605 | status = read_rreg(dev->base_addr, CSR0); |
| 606 | write_rreg(dev->base_addr, CSR0, status & |
| 607 | (CSR0_IENA|CSR0_TINT|CSR0_RINT| |
| 608 | CSR0_MERR|CSR0_MISS|CSR0_CERR|CSR0_BABL)); |
| 609 | |
| 610 | if (status & CSR0_RINT) { |
| 611 | handled = 1; |
| 612 | am79c961_rx(dev, priv); |
| 613 | } |
| 614 | if (status & CSR0_TINT) { |
| 615 | handled = 1; |
| 616 | am79c961_tx(dev, priv); |
| 617 | } |
| 618 | if (status & CSR0_MISS) { |
| 619 | handled = 1; |
| 620 | priv->stats.rx_dropped ++; |
| 621 | } |
| 622 | if (status & CSR0_CERR) { |
| 623 | handled = 1; |
| 624 | mod_timer(&priv->timer, jiffies); |
| 625 | } |
| 626 | } while (--n && status & (CSR0_RINT | CSR0_TINT)); |
| 627 | |
| 628 | return IRQ_RETVAL(handled); |
| 629 | } |
| 630 | |
| 631 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 632 | static void am79c961_poll_controller(struct net_device *dev) |
| 633 | { |
| 634 | unsigned long flags; |
| 635 | local_irq_save(flags); |
Yoann Padioleau | 0da2f0f | 2007-07-06 02:39:56 -0700 | [diff] [blame] | 636 | am79c961_interrupt(dev->irq, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | local_irq_restore(flags); |
| 638 | } |
| 639 | #endif |
| 640 | |
| 641 | /* |
| 642 | * Initialise the chip. Note that we always expect |
| 643 | * to be entered with interrupts enabled. |
| 644 | */ |
| 645 | static int |
| 646 | am79c961_hw_init(struct net_device *dev) |
| 647 | { |
| 648 | struct dev_priv *priv = netdev_priv(dev); |
| 649 | |
| 650 | spin_lock_irq(&priv->chip_lock); |
| 651 | write_rreg (dev->base_addr, CSR0, CSR0_STOP); |
| 652 | write_rreg (dev->base_addr, CSR3, CSR3_MASKALL); |
| 653 | spin_unlock_irq(&priv->chip_lock); |
| 654 | |
| 655 | am79c961_ramtest(dev, 0x66); |
| 656 | am79c961_ramtest(dev, 0x99); |
| 657 | |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | static void __init am79c961_banner(void) |
| 662 | { |
| 663 | static unsigned version_printed; |
| 664 | |
| 665 | if (net_debug && version_printed++ == 0) |
| 666 | printk(KERN_INFO "%s", version); |
| 667 | } |
| 668 | |
Russell King | 50465d0 | 2006-01-09 13:59:36 +0000 | [diff] [blame] | 669 | static int __init am79c961_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | { |
Russell King | 37bb30e | 2005-10-31 17:14:57 +0000 | [diff] [blame] | 671 | struct resource *res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | struct net_device *dev; |
| 673 | struct dev_priv *priv; |
| 674 | int i, ret; |
| 675 | |
Russell King | 37bb30e | 2005-10-31 17:14:57 +0000 | [diff] [blame] | 676 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
| 677 | if (!res) |
| 678 | return -ENODEV; |
| 679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | dev = alloc_etherdev(sizeof(struct dev_priv)); |
| 681 | ret = -ENOMEM; |
| 682 | if (!dev) |
| 683 | goto out; |
| 684 | |
Russell King | 37bb30e | 2005-10-31 17:14:57 +0000 | [diff] [blame] | 685 | SET_NETDEV_DEV(dev, &pdev->dev); |
| 686 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | priv = netdev_priv(dev); |
| 688 | |
| 689 | /* |
| 690 | * Fixed address and IRQ lines here. |
| 691 | * The PNP initialisation should have been |
| 692 | * done by the ether bootp loader. |
| 693 | */ |
Russell King | 37bb30e | 2005-10-31 17:14:57 +0000 | [diff] [blame] | 694 | dev->base_addr = res->start; |
Roel Kluin | 6278367 | 2008-04-23 23:44:03 +0200 | [diff] [blame] | 695 | ret = platform_get_irq(pdev, 0); |
| 696 | |
| 697 | if (ret < 0) { |
| 698 | ret = -ENODEV; |
| 699 | goto nodev; |
| 700 | } |
| 701 | dev->irq = ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | |
David Vrabel | 4894473 | 2006-01-19 17:56:29 +0000 | [diff] [blame] | 703 | ret = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | if (!request_region(dev->base_addr, 0x18, dev->name)) |
| 705 | goto nodev; |
| 706 | |
| 707 | /* |
| 708 | * Reset the device. |
| 709 | */ |
| 710 | inb(dev->base_addr + NET_RESET); |
| 711 | udelay(5); |
| 712 | |
| 713 | /* |
| 714 | * Check the manufacturer part of the |
| 715 | * ether address. |
| 716 | */ |
| 717 | if (inb(dev->base_addr) != 0x08 || |
| 718 | inb(dev->base_addr + 2) != 0x00 || |
| 719 | inb(dev->base_addr + 4) != 0x2b) |
| 720 | goto release; |
| 721 | |
Russell King | 487fd4e | 2005-10-02 18:12:03 +0100 | [diff] [blame] | 722 | for (i = 0; i < 6; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | dev->dev_addr[i] = inb(dev->base_addr + i * 2) & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
Russell King | 37bb30e | 2005-10-31 17:14:57 +0000 | [diff] [blame] | 725 | am79c961_banner(); |
| 726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | spin_lock_init(&priv->chip_lock); |
| 728 | init_timer(&priv->timer); |
| 729 | priv->timer.data = (unsigned long)dev; |
| 730 | priv->timer.function = am79c961_timer; |
| 731 | |
| 732 | if (am79c961_hw_init(dev)) |
| 733 | goto release; |
| 734 | |
| 735 | dev->open = am79c961_open; |
| 736 | dev->stop = am79c961_close; |
| 737 | dev->hard_start_xmit = am79c961_sendpacket; |
| 738 | dev->get_stats = am79c961_getstats; |
| 739 | dev->set_multicast_list = am79c961_setmulticastlist; |
| 740 | dev->tx_timeout = am79c961_timeout; |
| 741 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 742 | dev->poll_controller = am79c961_poll_controller; |
| 743 | #endif |
| 744 | |
| 745 | ret = register_netdev(dev); |
Russell King | 487fd4e | 2005-10-02 18:12:03 +0100 | [diff] [blame] | 746 | if (ret == 0) { |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 747 | printk(KERN_INFO "%s: ether address %pM\n", |
| 748 | dev->name, dev->dev_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | return 0; |
Russell King | 487fd4e | 2005-10-02 18:12:03 +0100 | [diff] [blame] | 750 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | |
| 752 | release: |
| 753 | release_region(dev->base_addr, 0x18); |
| 754 | nodev: |
| 755 | free_netdev(dev); |
| 756 | out: |
| 757 | return ret; |
| 758 | } |
| 759 | |
Russell King | 50465d0 | 2006-01-09 13:59:36 +0000 | [diff] [blame] | 760 | static struct platform_driver am79c961_driver = { |
Russell King | 37bb30e | 2005-10-31 17:14:57 +0000 | [diff] [blame] | 761 | .probe = am79c961_probe, |
Russell King | 50465d0 | 2006-01-09 13:59:36 +0000 | [diff] [blame] | 762 | .driver = { |
| 763 | .name = "am79c961", |
| 764 | }, |
Russell King | 37bb30e | 2005-10-31 17:14:57 +0000 | [diff] [blame] | 765 | }; |
| 766 | |
| 767 | static int __init am79c961_init(void) |
| 768 | { |
Russell King | 50465d0 | 2006-01-09 13:59:36 +0000 | [diff] [blame] | 769 | return platform_driver_register(&am79c961_driver); |
Russell King | 37bb30e | 2005-10-31 17:14:57 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | __initcall(am79c961_init); |