macsonic.c
来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 657 行 · 第 1/2 页
C
657 行
/* * macsonic.c * * (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/types.h>#include <linux/ctype.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/module.h>#include <asm/bootinfo.h>#include <asm/system.h>#include <asm/bitops.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>#define SREGS_PAD(n) u16 n;#include "sonic.h"#define SONIC_READ(reg) \ nubus_readl(base_addr+(reg))#define SONIC_WRITE(reg,val) \ nubus_writel((val), base_addr+(reg))#define sonic_read(dev, reg) \ nubus_readl((dev)->base_addr+(reg))#define sonic_write(dev, reg, val) \ nubus_writel((val), (dev)->base_addr+(reg))static int sonic_debug;static int sonic_version_printed;static int reg_offset;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)struct net_device * __init macsonic_probe(int unit){ struct net_device *dev = alloc_etherdev(0); int err; if (!dev) return ERR_PTR(-ENOMEM); if (unit >= 0) sprintf(dev->name, "eth%d", unit); SET_MODULE_OWNER(dev); /* This will catch fatal stuff like -ENOMEM as well as success */ err = mac_onboard_sonic_probe(dev); if (err == 0) goto found; if (err != -ENODEV) goto out; err = mac_nubus_sonic_probe(dev); if (err) goto out;found: err = register_netdev(dev); if (err) goto out1; return dev;out1: kfree(dev->priv);out: free_netdev(dev); return ERR_PTR(err);}/* * For reversing the PROM address */static unsigned char nibbletab[] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15};static inline void bit_reverse_addr(unsigned char addr[6]){ int i; for(i = 0; i < 6; i++) addr[i] = ((nibbletab[addr[i] & 0xf] << 4) | nibbletab[(addr[i] >> 4) &0xf]);}int __init macsonic_init(struct net_device* dev){ struct sonic_local* lp = NULL; int i; /* Allocate the entire chunk of memory for the descriptors. Note that this cannot cross a 64K boundary. */ for (i = 0; i < 20; i++) { unsigned long desc_base, desc_top; if((lp = kmalloc(sizeof(struct sonic_local), GFP_KERNEL | GFP_DMA)) == NULL) { printk(KERN_ERR "%s: couldn't allocate descriptor buffers\n", dev->name); return -ENOMEM; } desc_base = (unsigned long) lp; desc_top = desc_base + sizeof(struct sonic_local); if ((desc_top & 0xffff) >= (desc_base & 0xffff)) break; /* Hmm. try again (FIXME: does this actually work?) */ kfree(lp); printk(KERN_DEBUG "%s: didn't get continguous chunk [%08lx - %08lx], trying again\n", dev->name, desc_base, desc_top); } if (lp == NULL) { printk(KERN_ERR "%s: tried 20 times to allocate descriptor buffers, giving up.\n", dev->name); return -ENOMEM; } dev->priv = lp;#if 0 /* this code is only here as a curiousity... mainly, where the fuck did SONIC_BUS_SCALE come from, and what was it supposed to do? the normal allocation works great for 32 bit stuffs.. */ /* Now set up the pointers to point to the appropriate places */ lp->cda = lp->sonic_desc; 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));#endif memset(lp, 0, sizeof(struct sonic_local)); lp->cda_laddr = (unsigned int)&(lp->cda); lp->tda_laddr = (unsigned int)lp->tda; lp->rra_laddr = (unsigned int)lp->rra; lp->rda_laddr = (unsigned int)lp->rda; /* FIXME, maybe we should use skbs */ if ((lp->rba = (char *) kmalloc(SONIC_NUM_RRS * SONIC_RBSIZE, GFP_KERNEL | GFP_DMA)) == NULL) { printk(KERN_ERR "%s: couldn't allocate receive buffers\n", dev->name); dev->priv = NULL; kfree(lp); return -ENOMEM; } lp->rba_laddr = (unsigned int)lp->rba; { int rs, ds; /* almost always 12*4096, but let's not take chances */ rs = ((SONIC_NUM_RRS * SONIC_RBSIZE + 4095) / 4096) * 4096; /* almost always under a page, but let's not take chances */ ds = ((sizeof(struct sonic_local) + 4095) / 4096) * 4096; kernel_set_cachemode(lp->rba, rs, IOMAP_NOCACHE_SER); kernel_set_cachemode(lp, ds, IOMAP_NOCACHE_SER); } #if 0 flush_cache_all();#endif dev->open = sonic_open; dev->stop = sonic_close; dev->hard_start_xmit = sonic_send_packet; dev->get_stats = sonic_get_stats; dev->set_multicast_list = &sonic_multicast_list; /* * clear tally counter */ sonic_write(dev, SONIC_CRCT, 0xffff); sonic_write(dev, SONIC_FAET, 0xffff); sonic_write(dev, SONIC_MPT, 0xffff); return 0;}int __init mac_onboard_sonic_ethernet_addr(struct net_device* dev){ const int prom_addr = ONBOARD_SONIC_PROM_BASE; int i; /* 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\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\x05\x02", 3)) { unsigned short val; printk(KERN_INFO "macsonic: PROM seems to be wrong, trying CAM entry 15\n"); sonic_write(dev, SONIC_CMD, SONIC_CR_RST); sonic_write(dev, SONIC_CEP, 15); val = sonic_read(dev, SONIC_CAP2); dev->dev_addr[5] = val >> 8; dev->dev_addr[4] = val & 0xff; val = sonic_read(dev, SONIC_CAP1); dev->dev_addr[3] = val >> 8; dev->dev_addr[2] = val & 0xff; val = sonic_read(dev, SONIC_CAP0); dev->dev_addr[1] = val >> 8; dev->dev_addr[0] = val & 0xff; printk(KERN_INFO "HW Address from CAM 15: "); for (i = 0; i < 6; i++) { printk("%2.2x", dev->dev_addr[i]); if (i < 5) printk(":"); } printk("\n"); } 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\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; int i; int dma_bitmode;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?