Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Amiga Linux/68k A2065 Ethernet Driver |
| 3 | * |
| 4 | * (C) Copyright 1995-2003 by Geert Uytterhoeven <geert@linux-m68k.org> |
| 5 | * |
| 6 | * Fixes and tips by: |
| 7 | * - Janos Farkas (CHEXUM@sparta.banki.hu) |
| 8 | * - Jes Degn Soerensen (jds@kom.auc.dk) |
| 9 | * - Matt Domsch (Matt_Domsch@dell.com) |
| 10 | * |
| 11 | * ---------------------------------------------------------------------------- |
| 12 | * |
| 13 | * This program is based on |
| 14 | * |
| 15 | * ariadne.?: Amiga Linux/68k Ariadne Ethernet Driver |
| 16 | * (C) Copyright 1995 by Geert Uytterhoeven, |
| 17 | * Peter De Schrijver |
| 18 | * |
| 19 | * lance.c: An AMD LANCE ethernet driver for linux. |
| 20 | * Written 1993-94 by Donald Becker. |
| 21 | * |
| 22 | * Am79C960: PCnet(tm)-ISA Single-Chip Ethernet Controller |
| 23 | * Advanced Micro Devices |
| 24 | * Publication #16907, Rev. B, Amendment/0, May 1994 |
| 25 | * |
| 26 | * ---------------------------------------------------------------------------- |
| 27 | * |
| 28 | * This file is subject to the terms and conditions of the GNU General Public |
| 29 | * License. See the file COPYING in the main directory of the Linux |
| 30 | * distribution for more details. |
| 31 | * |
| 32 | * ---------------------------------------------------------------------------- |
| 33 | * |
| 34 | * The A2065 is a Zorro-II board made by Commodore/Ameristar. It contains: |
| 35 | * |
| 36 | * - an Am7990 Local Area Network Controller for Ethernet (LANCE) with |
| 37 | * both 10BASE-2 (thin coax) and AUI (DB-15) connectors |
| 38 | */ |
| 39 | |
| 40 | #include <linux/errno.h> |
| 41 | #include <linux/netdevice.h> |
| 42 | #include <linux/etherdevice.h> |
| 43 | #include <linux/module.h> |
| 44 | #include <linux/stddef.h> |
| 45 | #include <linux/kernel.h> |
| 46 | #include <linux/interrupt.h> |
| 47 | #include <linux/ioport.h> |
| 48 | #include <linux/skbuff.h> |
| 49 | #include <linux/slab.h> |
| 50 | #include <linux/string.h> |
| 51 | #include <linux/config.h> |
| 52 | #include <linux/init.h> |
| 53 | #include <linux/crc32.h> |
| 54 | #include <linux/zorro.h> |
| 55 | #include <linux/bitops.h> |
| 56 | |
| 57 | #include <asm/irq.h> |
| 58 | #include <asm/amigaints.h> |
| 59 | #include <asm/amigahw.h> |
| 60 | |
| 61 | #include "a2065.h" |
| 62 | |
| 63 | |
| 64 | /* |
| 65 | * Transmit/Receive Ring Definitions |
| 66 | */ |
| 67 | |
| 68 | #define LANCE_LOG_TX_BUFFERS (2) |
| 69 | #define LANCE_LOG_RX_BUFFERS (4) |
| 70 | |
| 71 | #define TX_RING_SIZE (1<<LANCE_LOG_TX_BUFFERS) |
| 72 | #define RX_RING_SIZE (1<<LANCE_LOG_RX_BUFFERS) |
| 73 | |
| 74 | #define TX_RING_MOD_MASK (TX_RING_SIZE-1) |
| 75 | #define RX_RING_MOD_MASK (RX_RING_SIZE-1) |
| 76 | |
| 77 | #define PKT_BUF_SIZE (1544) |
| 78 | #define RX_BUFF_SIZE PKT_BUF_SIZE |
| 79 | #define TX_BUFF_SIZE PKT_BUF_SIZE |
| 80 | |
| 81 | |
| 82 | /* |
| 83 | * Layout of the Lance's RAM Buffer |
| 84 | */ |
| 85 | |
| 86 | |
| 87 | struct lance_init_block { |
| 88 | unsigned short mode; /* Pre-set mode (reg. 15) */ |
| 89 | unsigned char phys_addr[6]; /* Physical ethernet address */ |
| 90 | unsigned filter[2]; /* Multicast filter. */ |
| 91 | |
| 92 | /* Receive and transmit ring base, along with extra bits. */ |
| 93 | unsigned short rx_ptr; /* receive descriptor addr */ |
| 94 | unsigned short rx_len; /* receive len and high addr */ |
| 95 | unsigned short tx_ptr; /* transmit descriptor addr */ |
| 96 | unsigned short tx_len; /* transmit len and high addr */ |
| 97 | |
| 98 | /* The Tx and Rx ring entries must aligned on 8-byte boundaries. */ |
| 99 | struct lance_rx_desc brx_ring[RX_RING_SIZE]; |
| 100 | struct lance_tx_desc btx_ring[TX_RING_SIZE]; |
| 101 | |
| 102 | char rx_buf [RX_RING_SIZE][RX_BUFF_SIZE]; |
| 103 | char tx_buf [TX_RING_SIZE][TX_BUFF_SIZE]; |
| 104 | }; |
| 105 | |
| 106 | |
| 107 | /* |
| 108 | * Private Device Data |
| 109 | */ |
| 110 | |
| 111 | struct lance_private { |
| 112 | char *name; |
| 113 | volatile struct lance_regs *ll; |
| 114 | volatile struct lance_init_block *init_block; /* Hosts view */ |
| 115 | volatile struct lance_init_block *lance_init_block; /* Lance view */ |
| 116 | |
| 117 | int rx_new, tx_new; |
| 118 | int rx_old, tx_old; |
| 119 | |
| 120 | int lance_log_rx_bufs, lance_log_tx_bufs; |
| 121 | int rx_ring_mod_mask, tx_ring_mod_mask; |
| 122 | |
| 123 | struct net_device_stats stats; |
| 124 | int tpe; /* cable-selection is TPE */ |
| 125 | int auto_select; /* cable-selection by carrier */ |
| 126 | unsigned short busmaster_regval; |
| 127 | |
| 128 | #ifdef CONFIG_SUNLANCE |
| 129 | struct Linux_SBus_DMA *ledma; /* if set this points to ledma and arch=4m */ |
| 130 | int burst_sizes; /* ledma SBus burst sizes */ |
| 131 | #endif |
| 132 | struct timer_list multicast_timer; |
| 133 | }; |
| 134 | |
| 135 | #define TX_BUFFS_AVAIL ((lp->tx_old<=lp->tx_new)?\ |
| 136 | lp->tx_old+lp->tx_ring_mod_mask-lp->tx_new:\ |
| 137 | lp->tx_old - lp->tx_new-1) |
| 138 | |
| 139 | |
| 140 | #define LANCE_ADDR(x) ((int)(x) & ~0xff000000) |
| 141 | |
| 142 | /* Load the CSR registers */ |
| 143 | static void load_csrs (struct lance_private *lp) |
| 144 | { |
| 145 | volatile struct lance_regs *ll = lp->ll; |
| 146 | volatile struct lance_init_block *aib = lp->lance_init_block; |
| 147 | int leptr; |
| 148 | |
| 149 | leptr = LANCE_ADDR (aib); |
| 150 | |
| 151 | ll->rap = LE_CSR1; |
| 152 | ll->rdp = (leptr & 0xFFFF); |
| 153 | ll->rap = LE_CSR2; |
| 154 | ll->rdp = leptr >> 16; |
| 155 | ll->rap = LE_CSR3; |
| 156 | ll->rdp = lp->busmaster_regval; |
| 157 | |
| 158 | /* Point back to csr0 */ |
| 159 | ll->rap = LE_CSR0; |
| 160 | } |
| 161 | |
| 162 | #define ZERO 0 |
| 163 | |
| 164 | /* Setup the Lance Rx and Tx rings */ |
| 165 | static void lance_init_ring (struct net_device *dev) |
| 166 | { |
| 167 | struct lance_private *lp = netdev_priv(dev); |
| 168 | volatile struct lance_init_block *ib = lp->init_block; |
| 169 | volatile struct lance_init_block *aib; /* for LANCE_ADDR computations */ |
| 170 | int leptr; |
| 171 | int i; |
| 172 | |
| 173 | aib = lp->lance_init_block; |
| 174 | |
| 175 | /* Lock out other processes while setting up hardware */ |
| 176 | netif_stop_queue(dev); |
| 177 | lp->rx_new = lp->tx_new = 0; |
| 178 | lp->rx_old = lp->tx_old = 0; |
| 179 | |
| 180 | ib->mode = 0; |
| 181 | |
| 182 | /* Copy the ethernet address to the lance init block |
| 183 | * Note that on the sparc you need to swap the ethernet address. |
| 184 | */ |
| 185 | ib->phys_addr [0] = dev->dev_addr [1]; |
| 186 | ib->phys_addr [1] = dev->dev_addr [0]; |
| 187 | ib->phys_addr [2] = dev->dev_addr [3]; |
| 188 | ib->phys_addr [3] = dev->dev_addr [2]; |
| 189 | ib->phys_addr [4] = dev->dev_addr [5]; |
| 190 | ib->phys_addr [5] = dev->dev_addr [4]; |
| 191 | |
| 192 | if (ZERO) |
| 193 | printk(KERN_DEBUG "TX rings:\n"); |
| 194 | |
| 195 | /* Setup the Tx ring entries */ |
| 196 | for (i = 0; i <= (1<<lp->lance_log_tx_bufs); i++) { |
| 197 | leptr = LANCE_ADDR(&aib->tx_buf[i][0]); |
| 198 | ib->btx_ring [i].tmd0 = leptr; |
| 199 | ib->btx_ring [i].tmd1_hadr = leptr >> 16; |
| 200 | ib->btx_ring [i].tmd1_bits = 0; |
| 201 | ib->btx_ring [i].length = 0xf000; /* The ones required by tmd2 */ |
| 202 | ib->btx_ring [i].misc = 0; |
| 203 | if (i < 3 && ZERO) |
| 204 | printk(KERN_DEBUG "%d: 0x%8.8x\n", i, leptr); |
| 205 | } |
| 206 | |
| 207 | /* Setup the Rx ring entries */ |
| 208 | if (ZERO) |
| 209 | printk(KERN_DEBUG "RX rings:\n"); |
| 210 | for (i = 0; i < (1<<lp->lance_log_rx_bufs); i++) { |
| 211 | leptr = LANCE_ADDR(&aib->rx_buf[i][0]); |
| 212 | |
| 213 | ib->brx_ring [i].rmd0 = leptr; |
| 214 | ib->brx_ring [i].rmd1_hadr = leptr >> 16; |
| 215 | ib->brx_ring [i].rmd1_bits = LE_R1_OWN; |
| 216 | ib->brx_ring [i].length = -RX_BUFF_SIZE | 0xf000; |
| 217 | ib->brx_ring [i].mblength = 0; |
| 218 | if (i < 3 && ZERO) |
| 219 | printk(KERN_DEBUG "%d: 0x%8.8x\n", i, leptr); |
| 220 | } |
| 221 | |
| 222 | /* Setup the initialization block */ |
| 223 | |
| 224 | /* Setup rx descriptor pointer */ |
| 225 | leptr = LANCE_ADDR(&aib->brx_ring); |
| 226 | ib->rx_len = (lp->lance_log_rx_bufs << 13) | (leptr >> 16); |
| 227 | ib->rx_ptr = leptr; |
| 228 | if (ZERO) |
| 229 | printk(KERN_DEBUG "RX ptr: %8.8x\n", leptr); |
| 230 | |
| 231 | /* Setup tx descriptor pointer */ |
| 232 | leptr = LANCE_ADDR(&aib->btx_ring); |
| 233 | ib->tx_len = (lp->lance_log_tx_bufs << 13) | (leptr >> 16); |
| 234 | ib->tx_ptr = leptr; |
| 235 | if (ZERO) |
| 236 | printk(KERN_DEBUG "TX ptr: %8.8x\n", leptr); |
| 237 | |
| 238 | /* Clear the multicast filter */ |
| 239 | ib->filter [0] = 0; |
| 240 | ib->filter [1] = 0; |
| 241 | } |
| 242 | |
| 243 | static int init_restart_lance (struct lance_private *lp) |
| 244 | { |
| 245 | volatile struct lance_regs *ll = lp->ll; |
| 246 | int i; |
| 247 | |
| 248 | ll->rap = LE_CSR0; |
| 249 | ll->rdp = LE_C0_INIT; |
| 250 | |
| 251 | /* Wait for the lance to complete initialization */ |
| 252 | for (i = 0; (i < 100) && !(ll->rdp & (LE_C0_ERR | LE_C0_IDON)); i++) |
| 253 | barrier(); |
| 254 | if ((i == 100) || (ll->rdp & LE_C0_ERR)) { |
| 255 | printk(KERN_ERR "LANCE unopened after %d ticks, csr0=%4.4x.\n", |
| 256 | i, ll->rdp); |
| 257 | return -EIO; |
| 258 | } |
| 259 | |
| 260 | /* Clear IDON by writing a "1", enable interrupts and start lance */ |
| 261 | ll->rdp = LE_C0_IDON; |
| 262 | ll->rdp = LE_C0_INEA | LE_C0_STRT; |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static int lance_rx (struct net_device *dev) |
| 268 | { |
| 269 | struct lance_private *lp = netdev_priv(dev); |
| 270 | volatile struct lance_init_block *ib = lp->init_block; |
| 271 | volatile struct lance_regs *ll = lp->ll; |
| 272 | volatile struct lance_rx_desc *rd; |
| 273 | unsigned char bits; |
| 274 | int len = 0; /* XXX shut up gcc warnings */ |
| 275 | struct sk_buff *skb = 0; /* XXX shut up gcc warnings */ |
| 276 | |
| 277 | #ifdef TEST_HITS |
| 278 | int i; |
| 279 | printk(KERN_DEBUG "["); |
| 280 | for (i = 0; i < RX_RING_SIZE; i++) { |
| 281 | if (i == lp->rx_new) |
| 282 | printk ("%s", |
| 283 | ib->brx_ring [i].rmd1_bits & LE_R1_OWN ? "_" : "X"); |
| 284 | else |
| 285 | printk ("%s", |
| 286 | ib->brx_ring [i].rmd1_bits & LE_R1_OWN ? "." : "1"); |
| 287 | } |
| 288 | printk ("]\n"); |
| 289 | #endif |
| 290 | |
| 291 | ll->rdp = LE_C0_RINT|LE_C0_INEA; |
| 292 | for (rd = &ib->brx_ring [lp->rx_new]; |
| 293 | !((bits = rd->rmd1_bits) & LE_R1_OWN); |
| 294 | rd = &ib->brx_ring [lp->rx_new]) { |
| 295 | |
| 296 | /* We got an incomplete frame? */ |
| 297 | if ((bits & LE_R1_POK) != LE_R1_POK) { |
| 298 | lp->stats.rx_over_errors++; |
| 299 | lp->stats.rx_errors++; |
| 300 | continue; |
| 301 | } else if (bits & LE_R1_ERR) { |
| 302 | /* Count only the end frame as a rx error, |
| 303 | * not the beginning |
| 304 | */ |
| 305 | if (bits & LE_R1_BUF) lp->stats.rx_fifo_errors++; |
| 306 | if (bits & LE_R1_CRC) lp->stats.rx_crc_errors++; |
| 307 | if (bits & LE_R1_OFL) lp->stats.rx_over_errors++; |
| 308 | if (bits & LE_R1_FRA) lp->stats.rx_frame_errors++; |
| 309 | if (bits & LE_R1_EOP) lp->stats.rx_errors++; |
| 310 | } else { |
| 311 | len = (rd->mblength & 0xfff) - 4; |
| 312 | skb = dev_alloc_skb (len+2); |
| 313 | |
| 314 | if (skb == 0) { |
| 315 | printk(KERN_WARNING "%s: Memory squeeze, " |
| 316 | "deferring packet.\n", dev->name); |
| 317 | lp->stats.rx_dropped++; |
| 318 | rd->mblength = 0; |
| 319 | rd->rmd1_bits = LE_R1_OWN; |
| 320 | lp->rx_new = (lp->rx_new + 1) & lp->rx_ring_mod_mask; |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | skb->dev = dev; |
| 325 | skb_reserve (skb, 2); /* 16 byte align */ |
| 326 | skb_put (skb, len); /* make room */ |
| 327 | eth_copy_and_sum(skb, |
| 328 | (unsigned char *)&(ib->rx_buf [lp->rx_new][0]), |
| 329 | len, 0); |
| 330 | skb->protocol = eth_type_trans (skb, dev); |
| 331 | netif_rx (skb); |
| 332 | dev->last_rx = jiffies; |
| 333 | lp->stats.rx_packets++; |
| 334 | lp->stats.rx_bytes += len; |
| 335 | } |
| 336 | |
| 337 | /* Return the packet to the pool */ |
| 338 | rd->mblength = 0; |
| 339 | rd->rmd1_bits = LE_R1_OWN; |
| 340 | lp->rx_new = (lp->rx_new + 1) & lp->rx_ring_mod_mask; |
| 341 | } |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static int lance_tx (struct net_device *dev) |
| 346 | { |
| 347 | struct lance_private *lp = netdev_priv(dev); |
| 348 | volatile struct lance_init_block *ib = lp->init_block; |
| 349 | volatile struct lance_regs *ll = lp->ll; |
| 350 | volatile struct lance_tx_desc *td; |
| 351 | int i, j; |
| 352 | int status; |
| 353 | |
| 354 | /* csr0 is 2f3 */ |
| 355 | ll->rdp = LE_C0_TINT | LE_C0_INEA; |
| 356 | /* csr0 is 73 */ |
| 357 | |
| 358 | j = lp->tx_old; |
| 359 | for (i = j; i != lp->tx_new; i = j) { |
| 360 | td = &ib->btx_ring [i]; |
| 361 | |
| 362 | /* If we hit a packet not owned by us, stop */ |
| 363 | if (td->tmd1_bits & LE_T1_OWN) |
| 364 | break; |
| 365 | |
| 366 | if (td->tmd1_bits & LE_T1_ERR) { |
| 367 | status = td->misc; |
| 368 | |
| 369 | lp->stats.tx_errors++; |
| 370 | if (status & LE_T3_RTY) lp->stats.tx_aborted_errors++; |
| 371 | if (status & LE_T3_LCOL) lp->stats.tx_window_errors++; |
| 372 | |
| 373 | if (status & LE_T3_CLOS) { |
| 374 | lp->stats.tx_carrier_errors++; |
| 375 | if (lp->auto_select) { |
| 376 | lp->tpe = 1 - lp->tpe; |
| 377 | printk(KERN_ERR "%s: Carrier Lost, " |
| 378 | "trying %s\n", dev->name, |
| 379 | lp->tpe?"TPE":"AUI"); |
| 380 | /* Stop the lance */ |
| 381 | ll->rap = LE_CSR0; |
| 382 | ll->rdp = LE_C0_STOP; |
| 383 | lance_init_ring (dev); |
| 384 | load_csrs (lp); |
| 385 | init_restart_lance (lp); |
| 386 | return 0; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /* buffer errors and underflows turn off the transmitter */ |
| 391 | /* Restart the adapter */ |
| 392 | if (status & (LE_T3_BUF|LE_T3_UFL)) { |
| 393 | lp->stats.tx_fifo_errors++; |
| 394 | |
| 395 | printk(KERN_ERR "%s: Tx: ERR_BUF|ERR_UFL, " |
| 396 | "restarting\n", dev->name); |
| 397 | /* Stop the lance */ |
| 398 | ll->rap = LE_CSR0; |
| 399 | ll->rdp = LE_C0_STOP; |
| 400 | lance_init_ring (dev); |
| 401 | load_csrs (lp); |
| 402 | init_restart_lance (lp); |
| 403 | return 0; |
| 404 | } |
| 405 | } else if ((td->tmd1_bits & LE_T1_POK) == LE_T1_POK) { |
| 406 | /* |
| 407 | * So we don't count the packet more than once. |
| 408 | */ |
| 409 | td->tmd1_bits &= ~(LE_T1_POK); |
| 410 | |
| 411 | /* One collision before packet was sent. */ |
| 412 | if (td->tmd1_bits & LE_T1_EONE) |
| 413 | lp->stats.collisions++; |
| 414 | |
| 415 | /* More than one collision, be optimistic. */ |
| 416 | if (td->tmd1_bits & LE_T1_EMORE) |
| 417 | lp->stats.collisions += 2; |
| 418 | |
| 419 | lp->stats.tx_packets++; |
| 420 | } |
| 421 | |
| 422 | j = (j + 1) & lp->tx_ring_mod_mask; |
| 423 | } |
| 424 | lp->tx_old = j; |
| 425 | ll->rdp = LE_C0_TINT | LE_C0_INEA; |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | static irqreturn_t |
| 430 | lance_interrupt (int irq, void *dev_id, struct pt_regs *regs) |
| 431 | { |
| 432 | struct net_device *dev; |
| 433 | struct lance_private *lp; |
| 434 | volatile struct lance_regs *ll; |
| 435 | int csr0; |
| 436 | |
| 437 | dev = (struct net_device *) dev_id; |
| 438 | |
| 439 | lp = netdev_priv(dev); |
| 440 | ll = lp->ll; |
| 441 | |
| 442 | ll->rap = LE_CSR0; /* LANCE Controller Status */ |
| 443 | csr0 = ll->rdp; |
| 444 | |
| 445 | if (!(csr0 & LE_C0_INTR)) /* Check if any interrupt has */ |
| 446 | return IRQ_NONE; /* been generated by the Lance. */ |
| 447 | |
| 448 | /* Acknowledge all the interrupt sources ASAP */ |
| 449 | ll->rdp = csr0 & ~(LE_C0_INEA|LE_C0_TDMD|LE_C0_STOP|LE_C0_STRT| |
| 450 | LE_C0_INIT); |
| 451 | |
| 452 | if ((csr0 & LE_C0_ERR)) { |
| 453 | /* Clear the error condition */ |
| 454 | ll->rdp = LE_C0_BABL|LE_C0_ERR|LE_C0_MISS|LE_C0_INEA; |
| 455 | } |
| 456 | |
| 457 | if (csr0 & LE_C0_RINT) |
| 458 | lance_rx (dev); |
| 459 | |
| 460 | if (csr0 & LE_C0_TINT) |
| 461 | lance_tx (dev); |
| 462 | |
| 463 | /* Log misc errors. */ |
| 464 | if (csr0 & LE_C0_BABL) |
| 465 | lp->stats.tx_errors++; /* Tx babble. */ |
| 466 | if (csr0 & LE_C0_MISS) |
| 467 | lp->stats.rx_errors++; /* Missed a Rx frame. */ |
| 468 | if (csr0 & LE_C0_MERR) { |
| 469 | printk(KERN_ERR "%s: Bus master arbitration failure, status " |
| 470 | "%4.4x.\n", dev->name, csr0); |
| 471 | /* Restart the chip. */ |
| 472 | ll->rdp = LE_C0_STRT; |
| 473 | } |
| 474 | |
| 475 | if (netif_queue_stopped(dev) && TX_BUFFS_AVAIL > 0) |
| 476 | netif_wake_queue(dev); |
| 477 | |
| 478 | ll->rap = LE_CSR0; |
| 479 | ll->rdp = LE_C0_BABL|LE_C0_CERR|LE_C0_MISS|LE_C0_MERR| |
| 480 | LE_C0_IDON|LE_C0_INEA; |
| 481 | return IRQ_HANDLED; |
| 482 | } |
| 483 | |
| 484 | struct net_device *last_dev = 0; |
| 485 | |
| 486 | static int lance_open (struct net_device *dev) |
| 487 | { |
| 488 | struct lance_private *lp = netdev_priv(dev); |
| 489 | volatile struct lance_regs *ll = lp->ll; |
| 490 | int ret; |
| 491 | |
| 492 | last_dev = dev; |
| 493 | |
| 494 | /* Stop the Lance */ |
| 495 | ll->rap = LE_CSR0; |
| 496 | ll->rdp = LE_C0_STOP; |
| 497 | |
| 498 | /* Install the Interrupt handler */ |
| 499 | ret = request_irq(IRQ_AMIGA_PORTS, lance_interrupt, SA_SHIRQ, |
| 500 | dev->name, dev); |
| 501 | if (ret) return ret; |
| 502 | |
| 503 | load_csrs (lp); |
| 504 | lance_init_ring (dev); |
| 505 | |
| 506 | netif_start_queue(dev); |
| 507 | |
| 508 | return init_restart_lance (lp); |
| 509 | } |
| 510 | |
| 511 | static int lance_close (struct net_device *dev) |
| 512 | { |
| 513 | struct lance_private *lp = netdev_priv(dev); |
| 514 | volatile struct lance_regs *ll = lp->ll; |
| 515 | |
| 516 | netif_stop_queue(dev); |
| 517 | del_timer_sync(&lp->multicast_timer); |
| 518 | |
| 519 | /* Stop the card */ |
| 520 | ll->rap = LE_CSR0; |
| 521 | ll->rdp = LE_C0_STOP; |
| 522 | |
| 523 | free_irq(IRQ_AMIGA_PORTS, dev); |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | static inline int lance_reset (struct net_device *dev) |
| 528 | { |
| 529 | struct lance_private *lp = netdev_priv(dev); |
| 530 | volatile struct lance_regs *ll = lp->ll; |
| 531 | int status; |
| 532 | |
| 533 | /* Stop the lance */ |
| 534 | ll->rap = LE_CSR0; |
| 535 | ll->rdp = LE_C0_STOP; |
| 536 | |
| 537 | load_csrs (lp); |
| 538 | |
| 539 | lance_init_ring (dev); |
| 540 | dev->trans_start = jiffies; |
| 541 | netif_start_queue(dev); |
| 542 | |
| 543 | status = init_restart_lance (lp); |
| 544 | #ifdef DEBUG_DRIVER |
| 545 | printk(KERN_DEBUG "Lance restart=%d\n", status); |
| 546 | #endif |
| 547 | return status; |
| 548 | } |
| 549 | |
| 550 | static void lance_tx_timeout(struct net_device *dev) |
| 551 | { |
| 552 | struct lance_private *lp = netdev_priv(dev); |
| 553 | volatile struct lance_regs *ll = lp->ll; |
| 554 | |
| 555 | printk(KERN_ERR "%s: transmit timed out, status %04x, reset\n", |
| 556 | dev->name, ll->rdp); |
| 557 | lance_reset(dev); |
| 558 | netif_wake_queue(dev); |
| 559 | } |
| 560 | |
| 561 | static int lance_start_xmit (struct sk_buff *skb, struct net_device *dev) |
| 562 | { |
| 563 | struct lance_private *lp = netdev_priv(dev); |
| 564 | volatile struct lance_regs *ll = lp->ll; |
| 565 | volatile struct lance_init_block *ib = lp->init_block; |
| 566 | int entry, skblen, len; |
| 567 | int status = 0; |
| 568 | static int outs; |
| 569 | unsigned long flags; |
| 570 | |
| 571 | skblen = skb->len; |
| 572 | len = skblen; |
| 573 | |
| 574 | if (len < ETH_ZLEN) { |
| 575 | len = ETH_ZLEN; |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame^] | 576 | if (skb_padto(skb, ETH_ZLEN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | local_irq_save(flags); |
| 581 | |
| 582 | if (!TX_BUFFS_AVAIL){ |
| 583 | local_irq_restore(flags); |
| 584 | return -1; |
| 585 | } |
| 586 | |
| 587 | #ifdef DEBUG_DRIVER |
| 588 | /* dump the packet */ |
| 589 | { |
| 590 | int i; |
| 591 | |
| 592 | for (i = 0; i < 64; i++) { |
| 593 | if ((i % 16) == 0) |
| 594 | printk("\n" KERN_DEBUG); |
| 595 | printk ("%2.2x ", skb->data [i]); |
| 596 | } |
| 597 | printk("\n"); |
| 598 | } |
| 599 | #endif |
| 600 | entry = lp->tx_new & lp->tx_ring_mod_mask; |
| 601 | ib->btx_ring [entry].length = (-len) | 0xf000; |
| 602 | ib->btx_ring [entry].misc = 0; |
| 603 | |
| 604 | memcpy ((char *)&ib->tx_buf [entry][0], skb->data, skblen); |
| 605 | |
| 606 | /* Clear the slack of the packet, do I need this? */ |
| 607 | if (len != skblen) |
| 608 | memset ((char *) &ib->tx_buf [entry][skblen], 0, len - skblen); |
| 609 | |
| 610 | /* Now, give the packet to the lance */ |
| 611 | ib->btx_ring [entry].tmd1_bits = (LE_T1_POK|LE_T1_OWN); |
| 612 | lp->tx_new = (lp->tx_new+1) & lp->tx_ring_mod_mask; |
| 613 | |
| 614 | outs++; |
| 615 | |
| 616 | if (TX_BUFFS_AVAIL <= 0) |
| 617 | netif_stop_queue(dev); |
| 618 | |
| 619 | /* Kick the lance: transmit now */ |
| 620 | ll->rdp = LE_C0_INEA | LE_C0_TDMD; |
| 621 | dev->trans_start = jiffies; |
| 622 | dev_kfree_skb (skb); |
| 623 | |
| 624 | local_irq_restore(flags); |
| 625 | |
| 626 | return status; |
| 627 | } |
| 628 | |
| 629 | static struct net_device_stats *lance_get_stats (struct net_device *dev) |
| 630 | { |
| 631 | struct lance_private *lp = netdev_priv(dev); |
| 632 | |
| 633 | return &lp->stats; |
| 634 | } |
| 635 | |
| 636 | /* taken from the depca driver */ |
| 637 | static void lance_load_multicast (struct net_device *dev) |
| 638 | { |
| 639 | struct lance_private *lp = netdev_priv(dev); |
| 640 | volatile struct lance_init_block *ib = lp->init_block; |
| 641 | volatile u16 *mcast_table = (u16 *)&ib->filter; |
| 642 | struct dev_mc_list *dmi=dev->mc_list; |
| 643 | char *addrs; |
| 644 | int i; |
| 645 | u32 crc; |
| 646 | |
| 647 | /* set all multicast bits */ |
| 648 | if (dev->flags & IFF_ALLMULTI){ |
| 649 | ib->filter [0] = 0xffffffff; |
| 650 | ib->filter [1] = 0xffffffff; |
| 651 | return; |
| 652 | } |
| 653 | /* clear the multicast filter */ |
| 654 | ib->filter [0] = 0; |
| 655 | ib->filter [1] = 0; |
| 656 | |
| 657 | /* Add addresses */ |
| 658 | for (i = 0; i < dev->mc_count; i++){ |
| 659 | addrs = dmi->dmi_addr; |
| 660 | dmi = dmi->next; |
| 661 | |
| 662 | /* multicast address? */ |
| 663 | if (!(*addrs & 1)) |
| 664 | continue; |
| 665 | |
| 666 | crc = ether_crc_le(6, addrs); |
| 667 | crc = crc >> 26; |
| 668 | mcast_table [crc >> 4] |= 1 << (crc & 0xf); |
| 669 | } |
| 670 | return; |
| 671 | } |
| 672 | |
| 673 | static void lance_set_multicast (struct net_device *dev) |
| 674 | { |
| 675 | struct lance_private *lp = netdev_priv(dev); |
| 676 | volatile struct lance_init_block *ib = lp->init_block; |
| 677 | volatile struct lance_regs *ll = lp->ll; |
| 678 | |
| 679 | if (!netif_running(dev)) |
| 680 | return; |
| 681 | |
| 682 | if (lp->tx_old != lp->tx_new) { |
| 683 | mod_timer(&lp->multicast_timer, jiffies + 4); |
| 684 | netif_wake_queue(dev); |
| 685 | return; |
| 686 | } |
| 687 | |
| 688 | netif_stop_queue(dev); |
| 689 | |
| 690 | ll->rap = LE_CSR0; |
| 691 | ll->rdp = LE_C0_STOP; |
| 692 | lance_init_ring (dev); |
| 693 | |
| 694 | if (dev->flags & IFF_PROMISC) { |
| 695 | ib->mode |= LE_MO_PROM; |
| 696 | } else { |
| 697 | ib->mode &= ~LE_MO_PROM; |
| 698 | lance_load_multicast (dev); |
| 699 | } |
| 700 | load_csrs (lp); |
| 701 | init_restart_lance (lp); |
| 702 | netif_wake_queue(dev); |
| 703 | } |
| 704 | |
| 705 | static int __devinit a2065_init_one(struct zorro_dev *z, |
| 706 | const struct zorro_device_id *ent); |
| 707 | static void __devexit a2065_remove_one(struct zorro_dev *z); |
| 708 | |
| 709 | |
| 710 | static struct zorro_device_id a2065_zorro_tbl[] __devinitdata = { |
| 711 | { ZORRO_PROD_CBM_A2065_1 }, |
| 712 | { ZORRO_PROD_CBM_A2065_2 }, |
| 713 | { ZORRO_PROD_AMERISTAR_A2065 }, |
| 714 | { 0 } |
| 715 | }; |
| 716 | |
| 717 | static struct zorro_driver a2065_driver = { |
| 718 | .name = "a2065", |
| 719 | .id_table = a2065_zorro_tbl, |
| 720 | .probe = a2065_init_one, |
| 721 | .remove = __devexit_p(a2065_remove_one), |
| 722 | }; |
| 723 | |
| 724 | static int __devinit a2065_init_one(struct zorro_dev *z, |
| 725 | const struct zorro_device_id *ent) |
| 726 | { |
| 727 | struct net_device *dev; |
| 728 | struct lance_private *priv; |
| 729 | unsigned long board, base_addr, mem_start; |
| 730 | struct resource *r1, *r2; |
| 731 | int err; |
| 732 | |
| 733 | board = z->resource.start; |
| 734 | base_addr = board+A2065_LANCE; |
| 735 | mem_start = board+A2065_RAM; |
| 736 | |
| 737 | r1 = request_mem_region(base_addr, sizeof(struct lance_regs), |
| 738 | "Am7990"); |
| 739 | if (!r1) |
| 740 | return -EBUSY; |
| 741 | r2 = request_mem_region(mem_start, A2065_RAM_SIZE, "RAM"); |
| 742 | if (!r2) { |
| 743 | release_resource(r1); |
| 744 | return -EBUSY; |
| 745 | } |
| 746 | |
| 747 | dev = alloc_etherdev(sizeof(struct lance_private)); |
| 748 | if (dev == NULL) { |
| 749 | release_resource(r1); |
| 750 | release_resource(r2); |
| 751 | return -ENOMEM; |
| 752 | } |
| 753 | |
| 754 | SET_MODULE_OWNER(dev); |
| 755 | priv = netdev_priv(dev); |
| 756 | |
| 757 | r1->name = dev->name; |
| 758 | r2->name = dev->name; |
| 759 | |
| 760 | dev->dev_addr[0] = 0x00; |
| 761 | if (z->id != ZORRO_PROD_AMERISTAR_A2065) { /* Commodore */ |
| 762 | dev->dev_addr[1] = 0x80; |
| 763 | dev->dev_addr[2] = 0x10; |
| 764 | } else { /* Ameristar */ |
| 765 | dev->dev_addr[1] = 0x00; |
| 766 | dev->dev_addr[2] = 0x9f; |
| 767 | } |
| 768 | dev->dev_addr[3] = (z->rom.er_SerialNumber>>16) & 0xff; |
| 769 | dev->dev_addr[4] = (z->rom.er_SerialNumber>>8) & 0xff; |
| 770 | dev->dev_addr[5] = z->rom.er_SerialNumber & 0xff; |
| 771 | dev->base_addr = ZTWO_VADDR(base_addr); |
| 772 | dev->mem_start = ZTWO_VADDR(mem_start); |
| 773 | dev->mem_end = dev->mem_start+A2065_RAM_SIZE; |
| 774 | |
| 775 | priv->ll = (volatile struct lance_regs *)dev->base_addr; |
| 776 | priv->init_block = (struct lance_init_block *)dev->mem_start; |
| 777 | priv->lance_init_block = (struct lance_init_block *)A2065_RAM; |
| 778 | priv->auto_select = 0; |
| 779 | priv->busmaster_regval = LE_C3_BSWP; |
| 780 | |
| 781 | priv->lance_log_rx_bufs = LANCE_LOG_RX_BUFFERS; |
| 782 | priv->lance_log_tx_bufs = LANCE_LOG_TX_BUFFERS; |
| 783 | priv->rx_ring_mod_mask = RX_RING_MOD_MASK; |
| 784 | priv->tx_ring_mod_mask = TX_RING_MOD_MASK; |
| 785 | |
| 786 | dev->open = &lance_open; |
| 787 | dev->stop = &lance_close; |
| 788 | dev->hard_start_xmit = &lance_start_xmit; |
| 789 | dev->tx_timeout = &lance_tx_timeout; |
| 790 | dev->watchdog_timeo = 5*HZ; |
| 791 | dev->get_stats = &lance_get_stats; |
| 792 | dev->set_multicast_list = &lance_set_multicast; |
| 793 | dev->dma = 0; |
| 794 | |
| 795 | init_timer(&priv->multicast_timer); |
| 796 | priv->multicast_timer.data = (unsigned long) dev; |
| 797 | priv->multicast_timer.function = |
| 798 | (void (*)(unsigned long)) &lance_set_multicast; |
| 799 | |
| 800 | err = register_netdev(dev); |
| 801 | if (err) { |
| 802 | release_resource(r1); |
| 803 | release_resource(r2); |
| 804 | free_netdev(dev); |
| 805 | return err; |
| 806 | } |
| 807 | zorro_set_drvdata(z, dev); |
| 808 | |
| 809 | printk(KERN_INFO "%s: A2065 at 0x%08lx, Ethernet Address " |
| 810 | "%02x:%02x:%02x:%02x:%02x:%02x\n", dev->name, board, |
| 811 | dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2], |
| 812 | dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]); |
| 813 | |
| 814 | return 0; |
| 815 | } |
| 816 | |
| 817 | |
| 818 | static void __devexit a2065_remove_one(struct zorro_dev *z) |
| 819 | { |
| 820 | struct net_device *dev = zorro_get_drvdata(z); |
| 821 | |
| 822 | unregister_netdev(dev); |
| 823 | release_mem_region(ZTWO_PADDR(dev->base_addr), |
| 824 | sizeof(struct lance_regs)); |
| 825 | release_mem_region(ZTWO_PADDR(dev->mem_start), A2065_RAM_SIZE); |
| 826 | free_netdev(dev); |
| 827 | } |
| 828 | |
| 829 | static int __init a2065_init_module(void) |
| 830 | { |
Bjorn Helgaas | 33d8675 | 2006-03-25 03:07:20 -0800 | [diff] [blame] | 831 | return zorro_register_driver(&a2065_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | static void __exit a2065_cleanup_module(void) |
| 835 | { |
| 836 | zorro_unregister_driver(&a2065_driver); |
| 837 | } |
| 838 | |
| 839 | module_init(a2065_init_module); |
| 840 | module_exit(a2065_cleanup_module); |
| 841 | |
| 842 | MODULE_LICENSE("GPL"); |