mace.c

来自「linux 内核源代码」· C语言 代码 · 共 1,024 行 · 第 1/2 页

C
1,024
字号
/* * Network device driver for the MACE ethernet controller on * Apple Powermacs.  Assumes it's under a DBDMA controller. * * Copyright (C) 1996 Paul Mackerras. */#include <linux/module.h>#include <linux/kernel.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/delay.h>#include <linux/string.h>#include <linux/timer.h>#include <linux/init.h>#include <linux/crc32.h>#include <linux/spinlock.h>#include <linux/bitrev.h>#include <asm/prom.h>#include <asm/dbdma.h>#include <asm/io.h>#include <asm/pgtable.h>#include <asm/macio.h>#include "mace.h"static int port_aaui = -1;#define N_RX_RING	8#define N_TX_RING	6#define MAX_TX_ACTIVE	1#define NCMDS_TX	1	/* dma commands per element in tx ring */#define RX_BUFLEN	(ETH_FRAME_LEN + 8)#define TX_TIMEOUT	HZ	/* 1 second *//* Chip rev needs workaround on HW & multicast addr change */#define BROKEN_ADDRCHG_REV	0x0941/* Bits in transmit DMA status */#define TX_DMA_ERR	0x80struct mace_data {    volatile struct mace __iomem *mace;    volatile struct dbdma_regs __iomem *tx_dma;    int tx_dma_intr;    volatile struct dbdma_regs __iomem *rx_dma;    int rx_dma_intr;    volatile struct dbdma_cmd *tx_cmds;	/* xmit dma command list */    volatile struct dbdma_cmd *rx_cmds;	/* recv dma command list */    struct sk_buff *rx_bufs[N_RX_RING];    int rx_fill;    int rx_empty;    struct sk_buff *tx_bufs[N_TX_RING];    int tx_fill;    int tx_empty;    unsigned char maccc;    unsigned char tx_fullup;    unsigned char tx_active;    unsigned char tx_bad_runt;    struct timer_list tx_timeout;    int timeout_active;    int port_aaui;    int chipid;    struct macio_dev *mdev;    spinlock_t lock;};/* * Number of bytes of private data per MACE: allow enough for * the rx and tx dma commands plus a branch dma command each, * and another 16 bytes to allow us to align the dma command * buffers on a 16 byte boundary. */#define PRIV_BYTES	(sizeof(struct mace_data) \	+ (N_RX_RING + NCMDS_TX * N_TX_RING + 3) * sizeof(struct dbdma_cmd))static int mace_open(struct net_device *dev);static int mace_close(struct net_device *dev);static int mace_xmit_start(struct sk_buff *skb, struct net_device *dev);static void mace_set_multicast(struct net_device *dev);static void mace_reset(struct net_device *dev);static int mace_set_address(struct net_device *dev, void *addr);static irqreturn_t mace_interrupt(int irq, void *dev_id);static irqreturn_t mace_txdma_intr(int irq, void *dev_id);static irqreturn_t mace_rxdma_intr(int irq, void *dev_id);static void mace_set_timeout(struct net_device *dev);static void mace_tx_timeout(unsigned long data);static inline void dbdma_reset(volatile struct dbdma_regs __iomem *dma);static inline void mace_clean_rings(struct mace_data *mp);static void __mace_set_address(struct net_device *dev, void *addr);/* * If we can't get a skbuff when we need it, we use this area for DMA. */static unsigned char *dummy_buf;static int __devinit mace_probe(struct macio_dev *mdev, const struct of_device_id *match){	struct device_node *mace = macio_get_of_node(mdev);	struct net_device *dev;	struct mace_data *mp;	const unsigned char *addr;	int j, rev, rc = -EBUSY;	DECLARE_MAC_BUF(mac);	if (macio_resource_count(mdev) != 3 || macio_irq_count(mdev) != 3) {		printk(KERN_ERR "can't use MACE %s: need 3 addrs and 3 irqs\n",		       mace->full_name);		return -ENODEV;	}	addr = of_get_property(mace, "mac-address", NULL);	if (addr == NULL) {		addr = of_get_property(mace, "local-mac-address", NULL);		if (addr == NULL) {			printk(KERN_ERR "Can't get mac-address for MACE %s\n",			       mace->full_name);			return -ENODEV;		}	}	/*	 * lazy allocate the driver-wide dummy buffer. (Note that we	 * never have more than one MACE in the system anyway)	 */	if (dummy_buf == NULL) {		dummy_buf = kmalloc(RX_BUFLEN+2, GFP_KERNEL);		if (dummy_buf == NULL) {			printk(KERN_ERR "MACE: couldn't allocate dummy buffer\n");			return -ENOMEM;		}	}	if (macio_request_resources(mdev, "mace")) {		printk(KERN_ERR "MACE: can't request IO resources !\n");		return -EBUSY;	}	dev = alloc_etherdev(PRIV_BYTES);	if (!dev) {		printk(KERN_ERR "MACE: can't allocate ethernet device !\n");		rc = -ENOMEM;		goto err_release;	}	SET_NETDEV_DEV(dev, &mdev->ofdev.dev);	mp = dev->priv;	mp->mdev = mdev;	macio_set_drvdata(mdev, dev);	dev->base_addr = macio_resource_start(mdev, 0);	mp->mace = ioremap(dev->base_addr, 0x1000);	if (mp->mace == NULL) {		printk(KERN_ERR "MACE: can't map IO resources !\n");		rc = -ENOMEM;		goto err_free;	}	dev->irq = macio_irq(mdev, 0);	rev = addr[0] == 0 && addr[1] == 0xA0;	for (j = 0; j < 6; ++j) {		dev->dev_addr[j] = rev ? bitrev8(addr[j]): addr[j];	}	mp->chipid = (in_8(&mp->mace->chipid_hi) << 8) |			in_8(&mp->mace->chipid_lo);	mp = (struct mace_data *) dev->priv;	mp->maccc = ENXMT | ENRCV;	mp->tx_dma = ioremap(macio_resource_start(mdev, 1), 0x1000);	if (mp->tx_dma == NULL) {		printk(KERN_ERR "MACE: can't map TX DMA resources !\n");		rc = -ENOMEM;		goto err_unmap_io;	}	mp->tx_dma_intr = macio_irq(mdev, 1);	mp->rx_dma = ioremap(macio_resource_start(mdev, 2), 0x1000);	if (mp->rx_dma == NULL) {		printk(KERN_ERR "MACE: can't map RX DMA resources !\n");		rc = -ENOMEM;		goto err_unmap_tx_dma;	}	mp->rx_dma_intr = macio_irq(mdev, 2);	mp->tx_cmds = (volatile struct dbdma_cmd *) DBDMA_ALIGN(mp + 1);	mp->rx_cmds = mp->tx_cmds + NCMDS_TX * N_TX_RING + 1;	memset((char *) mp->tx_cmds, 0,	       (NCMDS_TX*N_TX_RING + N_RX_RING + 2) * sizeof(struct dbdma_cmd));	init_timer(&mp->tx_timeout);	spin_lock_init(&mp->lock);	mp->timeout_active = 0;	if (port_aaui >= 0)		mp->port_aaui = port_aaui;	else {		/* Apple Network Server uses the AAUI port */		if (machine_is_compatible("AAPL,ShinerESB"))			mp->port_aaui = 1;		else {#ifdef CONFIG_MACE_AAUI_PORT			mp->port_aaui = 1;#else			mp->port_aaui = 0;#endif		}	}	dev->open = mace_open;	dev->stop = mace_close;	dev->hard_start_xmit = mace_xmit_start;	dev->set_multicast_list = mace_set_multicast;	dev->set_mac_address = mace_set_address;	/*	 * Most of what is below could be moved to mace_open()	 */	mace_reset(dev);	rc = request_irq(dev->irq, mace_interrupt, 0, "MACE", dev);	if (rc) {		printk(KERN_ERR "MACE: can't get irq %d\n", dev->irq);		goto err_unmap_rx_dma;	}	rc = request_irq(mp->tx_dma_intr, mace_txdma_intr, 0, "MACE-txdma", dev);	if (rc) {		printk(KERN_ERR "MACE: can't get irq %d\n", mp->tx_dma_intr);		goto err_free_irq;	}	rc = request_irq(mp->rx_dma_intr, mace_rxdma_intr, 0, "MACE-rxdma", dev);	if (rc) {		printk(KERN_ERR "MACE: can't get irq %d\n", mp->rx_dma_intr);		goto err_free_tx_irq;	}	rc = register_netdev(dev);	if (rc) {		printk(KERN_ERR "MACE: Cannot register net device, aborting.\n");		goto err_free_rx_irq;	}	printk(KERN_INFO "%s: MACE at %s, chip revision %d.%d\n",	       dev->name, print_mac(mac, dev->dev_addr),	       mp->chipid >> 8, mp->chipid & 0xff);	return 0; err_free_rx_irq:	free_irq(macio_irq(mdev, 2), dev); err_free_tx_irq:	free_irq(macio_irq(mdev, 1), dev); err_free_irq:	free_irq(macio_irq(mdev, 0), dev); err_unmap_rx_dma:	iounmap(mp->rx_dma); err_unmap_tx_dma:	iounmap(mp->tx_dma); err_unmap_io:	iounmap(mp->mace); err_free:	free_netdev(dev); err_release:	macio_release_resources(mdev);	return rc;}static int __devexit mace_remove(struct macio_dev *mdev){	struct net_device *dev = macio_get_drvdata(mdev);	struct mace_data *mp;	BUG_ON(dev == NULL);	macio_set_drvdata(mdev, NULL);	mp = dev->priv;	unregister_netdev(dev);	free_irq(dev->irq, dev);	free_irq(mp->tx_dma_intr, dev);	free_irq(mp->rx_dma_intr, dev);	iounmap(mp->rx_dma);	iounmap(mp->tx_dma);	iounmap(mp->mace);	free_netdev(dev);	macio_release_resources(mdev);	return 0;}static void dbdma_reset(volatile struct dbdma_regs __iomem *dma){    int i;    out_le32(&dma->control, (WAKE|FLUSH|PAUSE|RUN) << 16);    /*     * Yes this looks peculiar, but apparently it needs to be this     * way on some machines.     */    for (i = 200; i > 0; --i)	if (ld_le32(&dma->control) & RUN)	    udelay(1);}static void mace_reset(struct net_device *dev){    struct mace_data *mp = (struct mace_data *) dev->priv;    volatile struct mace __iomem *mb = mp->mace;    int i;    /* soft-reset the chip */    i = 200;    while (--i) {	out_8(&mb->biucc, SWRST);	if (in_8(&mb->biucc) & SWRST) {	    udelay(10);	    continue;	}	break;    }    if (!i) {	printk(KERN_ERR "mace: cannot reset chip!\n");	return;    }    out_8(&mb->imr, 0xff);	/* disable all intrs for now */    i = in_8(&mb->ir);    out_8(&mb->maccc, 0);	/* turn off tx, rx */    out_8(&mb->biucc, XMTSP_64);    out_8(&mb->utr, RTRD);    out_8(&mb->fifocc, RCVFW_32 | XMTFW_16 | XMTFWU | RCVFWU | XMTBRST);    out_8(&mb->xmtfc, AUTO_PAD_XMIT); /* auto-pad short frames */    out_8(&mb->rcvfc, 0);    /* load up the hardware address */    __mace_set_address(dev, dev->dev_addr);    /* clear the multicast filter */    if (mp->chipid == BROKEN_ADDRCHG_REV)	out_8(&mb->iac, LOGADDR);    else {	out_8(&mb->iac, ADDRCHG | LOGADDR);	while ((in_8(&mb->iac) & ADDRCHG) != 0)		;    }    for (i = 0; i < 8; ++i)	out_8(&mb->ladrf, 0);    /* done changing address */    if (mp->chipid != BROKEN_ADDRCHG_REV)	out_8(&mb->iac, 0);    if (mp->port_aaui)    	out_8(&mb->plscc, PORTSEL_AUI + ENPLSIO);    else    	out_8(&mb->plscc, PORTSEL_GPSI + ENPLSIO);}static void __mace_set_address(struct net_device *dev, void *addr){    struct mace_data *mp = (struct mace_data *) dev->priv;    volatile struct mace __iomem *mb = mp->mace;    unsigned char *p = addr;    int i;    /* load up the hardware address */    if (mp->chipid == BROKEN_ADDRCHG_REV)    	out_8(&mb->iac, PHYADDR);    else {    	out_8(&mb->iac, ADDRCHG | PHYADDR);	while ((in_8(&mb->iac) & ADDRCHG) != 0)	    ;    }    for (i = 0; i < 6; ++i)	out_8(&mb->padr, dev->dev_addr[i] = p[i]);    if (mp->chipid != BROKEN_ADDRCHG_REV)        out_8(&mb->iac, 0);}static int mace_set_address(struct net_device *dev, void *addr){    struct mace_data *mp = (struct mace_data *) dev->priv;    volatile struct mace __iomem *mb = mp->mace;    unsigned long flags;    spin_lock_irqsave(&mp->lock, flags);    __mace_set_address(dev, addr);    /* note: setting ADDRCHG clears ENRCV */    out_8(&mb->maccc, mp->maccc);    spin_unlock_irqrestore(&mp->lock, flags);    return 0;}static inline void mace_clean_rings(struct mace_data *mp){    int i;    /* free some skb's */    for (i = 0; i < N_RX_RING; ++i) {	if (mp->rx_bufs[i] != 0) {	    dev_kfree_skb(mp->rx_bufs[i]);	    mp->rx_bufs[i] = NULL;	}    }    for (i = mp->tx_empty; i != mp->tx_fill; ) {	dev_kfree_skb(mp->tx_bufs[i]);	if (++i >= N_TX_RING)	    i = 0;    }}static int mace_open(struct net_device *dev){    struct mace_data *mp = (struct mace_data *) dev->priv;    volatile struct mace __iomem *mb = mp->mace;    volatile struct dbdma_regs __iomem *rd = mp->rx_dma;    volatile struct dbdma_regs __iomem *td = mp->tx_dma;    volatile struct dbdma_cmd *cp;    int i;    struct sk_buff *skb;    unsigned char *data;    /* reset the chip */    mace_reset(dev);    /* initialize list of sk_buffs for receiving and set up recv dma */    mace_clean_rings(mp);    memset((char *)mp->rx_cmds, 0, N_RX_RING * sizeof(struct dbdma_cmd));    cp = mp->rx_cmds;    for (i = 0; i < N_RX_RING - 1; ++i) {	skb = dev_alloc_skb(RX_BUFLEN + 2);	if (skb == 0) {	    data = dummy_buf;	} else {	    skb_reserve(skb, 2);	/* so IP header lands on 4-byte bdry */	    data = skb->data;	}	mp->rx_bufs[i] = skb;	st_le16(&cp->req_count, RX_BUFLEN);	st_le16(&cp->command, INPUT_LAST + INTR_ALWAYS);	st_le32(&cp->phy_addr, virt_to_bus(data));	cp->xfer_status = 0;	++cp;    }    mp->rx_bufs[i] = NULL;    st_le16(&cp->command, DBDMA_STOP);    mp->rx_fill = i;    mp->rx_empty = 0;    /* Put a branch back to the beginning of the receive command list */    ++cp;    st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);    st_le32(&cp->cmd_dep, virt_to_bus(mp->rx_cmds));    /* start rx dma */    out_le32(&rd->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* clear run bit */    out_le32(&rd->cmdptr, virt_to_bus(mp->rx_cmds));    out_le32(&rd->control, (RUN << 16) | RUN);    /* put a branch at the end of the tx command list */    cp = mp->tx_cmds + NCMDS_TX * N_TX_RING;    st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);    st_le32(&cp->cmd_dep, virt_to_bus(mp->tx_cmds));    /* reset tx dma */    out_le32(&td->control, (RUN|PAUSE|FLUSH|WAKE) << 16);    out_le32(&td->cmdptr, virt_to_bus(mp->tx_cmds));    mp->tx_fill = 0;    mp->tx_empty = 0;    mp->tx_fullup = 0;    mp->tx_active = 0;    mp->tx_bad_runt = 0;    /* turn it on! */    out_8(&mb->maccc, mp->maccc);    /* enable all interrupts except receive interrupts */    out_8(&mb->imr, RCVINT);    return 0;}static int mace_close(struct net_device *dev){    struct mace_data *mp = (struct mace_data *) dev->priv;    volatile struct mace __iomem *mb = mp->mace;    volatile struct dbdma_regs __iomem *rd = mp->rx_dma;    volatile struct dbdma_regs __iomem *td = mp->tx_dma;    /* disable rx and tx */    out_8(&mb->maccc, 0);    out_8(&mb->imr, 0xff);		/* disable all intrs */    /* disable rx and tx dma */    st_le32(&rd->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* clear run bit */    st_le32(&td->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* clear run bit */    mace_clean_rings(mp);    return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?