macsonic.c
来自「linux 内核源代码」· C语言 代码 · 共 666 行 · 第 1/2 页
C
666 行
/* * macsonic.c * * (C) 2005 Finn Thain * * Converted to DMA API, converted to unified driver model, made it work as * a module again, and from the mac68k project, introduced more 32-bit cards * and dhd's support for 16-bit cards. * * (C) 1998 Alan Cox * * Debugging Andreas Ehliar, Michael Schmitz * * Based on code * (C) 1996 by Thomas Bogendoerfer (tsbogend@bigbug.franken.de) * * This driver is based on work from Andreas Busse, but most of * the code is rewritten. * * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de) * * A driver for the Mac onboard Sonic ethernet chip. * * 98/12/21 MSch: judged from tests on Q800, it's basically working, * but eating up both receive and transmit resources * and duplicating packets. Needs more testing. * * 99/01/03 MSch: upgraded to version 0.92 of the core driver, fixed. * * 00/10/31 sammy@oh.verio.com: Updated driver for 2.4 kernels, fixed problems * on centris. */#include <linux/kernel.h>#include <linux/module.h>#include <linux/types.h>#include <linux/fcntl.h>#include <linux/interrupt.h>#include <linux/init.h>#include <linux/ioport.h>#include <linux/in.h>#include <linux/slab.h>#include <linux/string.h>#include <linux/delay.h>#include <linux/nubus.h>#include <linux/errno.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>#include <linux/platform_device.h>#include <linux/dma-mapping.h>#include <linux/bitrev.h>#include <asm/bootinfo.h>#include <asm/system.h>#include <asm/pgtable.h>#include <asm/io.h>#include <asm/hwtest.h>#include <asm/dma.h>#include <asm/macintosh.h>#include <asm/macints.h>#include <asm/mac_via.h>static char mac_sonic_string[] = "macsonic";static struct platform_device *mac_sonic_device;#include "sonic.h"/* These should basically be bus-size and endian independent (since the SONIC is at least smart enough that it uses the same endianness as the host, unlike certain less enlightened Macintosh NICs) */#define SONIC_READ(reg) (nubus_readw(dev->base_addr + (reg * 4) \ + lp->reg_offset))#define SONIC_WRITE(reg,val) (nubus_writew(val, dev->base_addr + (reg * 4) \ + lp->reg_offset))/* use 0 for production, 1 for verification, >1 for debug */#ifdef SONIC_DEBUGstatic unsigned int sonic_debug = SONIC_DEBUG;#elsestatic unsigned int sonic_debug = 1;#endifstatic int sonic_version_printed;extern int mac_onboard_sonic_probe(struct net_device* dev);extern int mac_nubus_sonic_probe(struct net_device* dev);/* For onboard SONIC */#define ONBOARD_SONIC_REGISTERS 0x50F0A000#define ONBOARD_SONIC_PROM_BASE 0x50f08000enum macsonic_type { MACSONIC_DUODOCK, MACSONIC_APPLE, MACSONIC_APPLE16, MACSONIC_DAYNA, MACSONIC_DAYNALINK};/* For the built-in SONIC in the Duo Dock */#define DUODOCK_SONIC_REGISTERS 0xe10000#define DUODOCK_SONIC_PROM_BASE 0xe12000/* For Apple-style NuBus SONIC */#define APPLE_SONIC_REGISTERS 0#define APPLE_SONIC_PROM_BASE 0x40000/* Daynalink LC SONIC */#define DAYNALINK_PROM_BASE 0x400000/* For Dayna-style NuBus SONIC (haven't seen one yet) */#define DAYNA_SONIC_REGISTERS 0x180000/* This is what OpenBSD says. However, this is definitely in NuBus ROM space so we should be able to get it by walking the NuBus resource directories */#define DAYNA_SONIC_MAC_ADDR 0xffe004#define SONIC_READ_PROM(addr) nubus_readb(prom_addr+addr)/* * For reversing the PROM address */static inline void bit_reverse_addr(unsigned char addr[6]){ int i; for(i = 0; i < 6; i++) addr[i] = bitrev8(addr[i]);}static irqreturn_t macsonic_interrupt(int irq, void *dev_id){ irqreturn_t result; unsigned long flags; local_irq_save(flags); result = sonic_interrupt(irq, dev_id); local_irq_restore(flags); return result;}static int macsonic_open(struct net_device* dev){ if (request_irq(dev->irq, &sonic_interrupt, IRQ_FLG_FAST, "sonic", dev)) { printk(KERN_ERR "%s: unable to get IRQ %d.\n", dev->name, dev->irq); return -EAGAIN; } /* Under the A/UX interrupt scheme, the onboard SONIC interrupt comes * in at priority level 3. However, we sometimes get the level 2 inter- * rupt as well, which must prevent re-entrance of the sonic handler. */ if (dev->irq == IRQ_AUTO_3) if (request_irq(IRQ_NUBUS_9, &macsonic_interrupt, IRQ_FLG_FAST, "sonic", dev)) { printk(KERN_ERR "%s: unable to get IRQ %d.\n", dev->name, IRQ_NUBUS_9); free_irq(dev->irq, dev); return -EAGAIN; } return sonic_open(dev);}static int macsonic_close(struct net_device* dev){ int err; err = sonic_close(dev); free_irq(dev->irq, dev); if (dev->irq == IRQ_AUTO_3) free_irq(IRQ_NUBUS_9, dev); return err;}int __init macsonic_init(struct net_device* dev){ struct sonic_local* lp = netdev_priv(dev); /* Allocate the entire chunk of memory for the descriptors. Note that this cannot cross a 64K boundary. */ if ((lp->descriptors = dma_alloc_coherent(lp->device, SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode), &lp->descriptors_laddr, GFP_KERNEL)) == NULL) { printk(KERN_ERR "%s: couldn't alloc DMA memory for descriptors.\n", lp->device->bus_id); return -ENOMEM; } /* Now set up the pointers to point to the appropriate places */ lp->cda = lp->descriptors; lp->tda = lp->cda + (SIZEOF_SONIC_CDA * SONIC_BUS_SCALE(lp->dma_bitmode)); lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS * SONIC_BUS_SCALE(lp->dma_bitmode)); lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS * SONIC_BUS_SCALE(lp->dma_bitmode)); lp->cda_laddr = lp->descriptors_laddr; lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA * SONIC_BUS_SCALE(lp->dma_bitmode)); lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS * SONIC_BUS_SCALE(lp->dma_bitmode)); lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS * SONIC_BUS_SCALE(lp->dma_bitmode)); dev->open = macsonic_open; dev->stop = macsonic_close; dev->hard_start_xmit = sonic_send_packet; dev->get_stats = sonic_get_stats; dev->set_multicast_list = &sonic_multicast_list; dev->tx_timeout = sonic_tx_timeout; dev->watchdog_timeo = TX_TIMEOUT; /* * clear tally counter */ SONIC_WRITE(SONIC_CRCT, 0xffff); SONIC_WRITE(SONIC_FAET, 0xffff); SONIC_WRITE(SONIC_MPT, 0xffff); return 0;}int __init mac_onboard_sonic_ethernet_addr(struct net_device* dev){ struct sonic_local *lp = netdev_priv(dev); const int prom_addr = ONBOARD_SONIC_PROM_BASE; int i; DECLARE_MAC_BUF(mac); /* On NuBus boards we can sometimes look in the ROM resources. No such luck for comm-slot/onboard. */ for(i = 0; i < 6; i++) dev->dev_addr[i] = SONIC_READ_PROM(i); /* Most of the time, the address is bit-reversed. The NetBSD source has a rather long and detailed historical account of why this is so. */ if (memcmp(dev->dev_addr, "\x08\x00\x07", 3) && memcmp(dev->dev_addr, "\x00\xA0\x40", 3) && memcmp(dev->dev_addr, "\x00\x80\x19", 3) && memcmp(dev->dev_addr, "\x00\x05\x02", 3)) bit_reverse_addr(dev->dev_addr); else return 0; /* If we still have what seems to be a bogus address, we'll look in the CAM. The top entry should be ours. */ /* Danger! This only works if MacOS has already initialized the card... */ if (memcmp(dev->dev_addr, "\x08\x00\x07", 3) && memcmp(dev->dev_addr, "\x00\xA0\x40", 3) && memcmp(dev->dev_addr, "\x00\x80\x19", 3) && memcmp(dev->dev_addr, "\x00\x05\x02", 3)) { unsigned short val; printk(KERN_INFO "macsonic: PROM seems to be wrong, trying CAM entry 15\n"); SONIC_WRITE(SONIC_CMD, SONIC_CR_RST); SONIC_WRITE(SONIC_CEP, 15); val = SONIC_READ(SONIC_CAP2); dev->dev_addr[5] = val >> 8; dev->dev_addr[4] = val & 0xff; val = SONIC_READ(SONIC_CAP1); dev->dev_addr[3] = val >> 8; dev->dev_addr[2] = val & 0xff; val = SONIC_READ(SONIC_CAP0); dev->dev_addr[1] = val >> 8; dev->dev_addr[0] = val & 0xff; printk(KERN_INFO "HW Address from CAM 15: %s\n", print_mac(mac, dev->dev_addr)); } else return 0; if (memcmp(dev->dev_addr, "\x08\x00\x07", 3) && memcmp(dev->dev_addr, "\x00\xA0\x40", 3) && memcmp(dev->dev_addr, "\x00\x80\x19", 3) && memcmp(dev->dev_addr, "\x00\x05\x02", 3)) { /* * Still nonsense ... messed up someplace! */ printk(KERN_ERR "macsonic: ERROR (INVALID MAC)\n"); return -EIO; } else return 0;}int __init mac_onboard_sonic_probe(struct net_device* dev){ /* Bwahahaha */ static int once_is_more_than_enough; struct sonic_local* lp = netdev_priv(dev); int sr; int commslot = 0; if (once_is_more_than_enough) return -ENODEV; once_is_more_than_enough = 1; if (!MACH_IS_MAC) return -ENODEV; if (macintosh_config->ether_type != MAC_ETHER_SONIC) return -ENODEV; printk(KERN_INFO "Checking for internal Macintosh ethernet (SONIC).. "); /* Bogus probing, on the models which may or may not have Ethernet (BTW, the Ethernet *is* always at the same address, and nothing else lives there, at least if Apple's documentation is to be believed) */ if (macintosh_config->ident == MAC_MODEL_Q630 || macintosh_config->ident == MAC_MODEL_P588 || macintosh_config->ident == MAC_MODEL_P575 || macintosh_config->ident == MAC_MODEL_C610) { unsigned long flags; int card_present; local_irq_save(flags); card_present = hwreg_present((void*)ONBOARD_SONIC_REGISTERS); local_irq_restore(flags); if (!card_present) { printk("none.\n"); return -ENODEV; } commslot = 1; } printk("yes\n"); /* Danger! My arms are flailing wildly! You *must* set lp->reg_offset * and dev->base_addr before using SONIC_READ() or SONIC_WRITE() */ dev->base_addr = ONBOARD_SONIC_REGISTERS;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?