📄 e100lpslavenet.c
字号:
/* $Id: e100lpslavenet.c,v 1.4 2001/06/21 16:55:26 olof Exp $ * * e100lpslavenet.c: A network driver for the ETRAX 100LX slave controller. * * Copyright (c) 1998-2001 Axis Communications AB. * * The outline of this driver comes from skeleton.c. * * $Log: e100lpslavenet.c,v $ * Revision 1.4 2001/06/21 16:55:26 olof * Minimized par port setup time to gain bandwidth * * Revision 1.3 2001/06/21 15:49:02 olof * Removed setting of default MAC address * * Revision 1.2 2001/06/11 15:39:52 olof * Clean up and sync with ethernet.c rev 1.16. Increased reset time of slave. * * Revision 1.1 2001/06/06 08:56:26 olof * Added support for slave Etrax defined by CONFIG_ETRAX_ETHERNET_LPSLAVE * */#include <linux/config.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/delay.h>#include <linux/types.h>#include <linux/fcntl.h>#include <linux/interrupt.h>#include <linux/ptrace.h>#include <linux/ioport.h>#include <linux/in.h>#include <linux/slab.h>#include <linux/string.h>#include <linux/spinlock.h>#include <linux/errno.h>#include <linux/init.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>#include <asm/svinto.h> /* DMA and register descriptions */#include <asm/io.h> /* LED_* I/O functions */#include <asm/irq.h>#include <asm/dma.h>#include <asm/system.h>#include <asm/bitops.h>#include "e100lpslave.h"/* #define ETHDEBUG */#define D(x)/* * The name of the card. Is used for messages and in the requests for * io regions, irqs and dma channels */static const char* cardname = "Etrax 100LX ethernet slave controller";/* A default ethernet address. Highlevel SW will set the real one later */static struct sockaddr default_mac = { 0, { 0x00, 0x40, 0x8C, 0xCD, 0x00, 0x00 }};/* Information that need to be kept for each board. */struct net_local { struct net_device_stats stats; /* Tx control lock. This protects the transmit buffer ring * state along with the "tx full" state of the driver. This * means all netif_queue flow control actions are protected * by this lock as well. */ spinlock_t lock;};/* Dma descriptors etc. */#define RX_BUF_SIZE 32768#define ETHER_HEAD_LEN 14#define PAR0_ECP_IRQ_NBR 4#define RX_DESC_BUF_SIZE 256#define NBR_OF_RX_DESC (RX_BUF_SIZE / \ RX_DESC_BUF_SIZE)/* Size of slave etrax boot image */#define ETRAX_PAR_BOOT_LENGTH 784static etrax_dma_descr *myNextRxDesc; /* Points to the next descriptor to to be processed */static etrax_dma_descr *myLastRxDesc; /* The last processed descriptor */static etrax_dma_descr *myPrevRxDesc; /* The descriptor right before myNextRxDesc */static unsigned char RxBuf[RX_BUF_SIZE];static etrax_dma_descr RxDescList[NBR_OF_RX_DESC] __attribute__ ((aligned(4)));static etrax_dma_descr TxDescList[3] __attribute__ ((aligned(4))); /* host command, data, bogus ECP command */static struct sk_buff *tx_skb;/* Index to functions, as function prototypes. */static int etrax_ethernet_lpslave_init(struct net_device *dev);static int e100_open(struct net_device *dev);static int e100_set_mac_address(struct net_device *dev, void *addr);static int e100_send_packet(struct sk_buff *skb, struct net_device *dev);static void e100rx_interrupt(int irq, void *dev_id, struct pt_regs *regs);static void e100tx_interrupt(int irq, void *dev_id, struct pt_regs *regs);static void ecp_interrupt(int irq, void *dev_id, struct pt_regs *regs);static void e100_rx(struct net_device *dev);static int e100_close(struct net_device *dev);static struct net_device_stats *e100_get_stats(struct net_device *dev);static void set_multicast_list(struct net_device *dev);static void e100_hardware_send_packet(unsigned long hostcmd, char *buf, int length);static void update_rx_stats(struct net_device_stats *);static void update_tx_stats(struct net_device_stats *);static void e100_reset_tranceiver(void);static void boot_slave(unsigned char *code);#ifdef ETHDEBUGstatic void dump_parport_status(void);#endif#define tx_done(dev) (*R_DMA_CH0_CMD == 0)static unsigned long host_command;extern unsigned char e100lpslaveprog;/* * This driver uses PAR0 to recevice data from slave ETRAX and PAR1 to boot * and send data to slave ETRAX. * Used ETRAX100 DMAchannels with corresponding IRQ: * PAR0 RX : DMA3 - IRQ 19 * PAR1 TX : DMA4 - IRQ 20 * IRQ 4 is used to detect ECP commands from slave ETRAX * * NOTE! PAR0 and PAR1 shares DMA and IRQ numbers with SER2 and SER3 *//* * Check for a network adaptor of this type, and return '0' if one exists. * If dev->base_addr == 0, probe all likely locations. * If dev->base_addr == 1, always return failure. * If dev->base_addr == 2, allocate space for the device and return success * (detachable devices only). */static int __initetrax_ethernet_lpslave_init(struct net_device *dev){ int i; int anOffset = 0; printk("Etrax/100 lpslave ethernet driver v0.3, (c) 1999 Axis Communications AB\n"); dev->base_addr = 2; printk("%s initialized\n", dev->name); /* make Linux aware of the new hardware */ if (!dev) { printk(KERN_WARNING "%s: dev == NULL. Should this happen?\n", cardname); dev = init_etherdev(dev, sizeof(struct net_local)); if (!dev) panic("init_etherdev failed\n"); } /* setup generic handlers and stuff in the dev struct */ ether_setup(dev); /* make room for the local structure containing stats etc */ dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL); if (dev->priv == NULL) return -ENOMEM; memset(dev->priv, 0, sizeof(struct net_local)); /* now setup our etrax specific stuff */ dev->irq = DMA3_RX_IRQ_NBR; /* we really use DMATX as well... */ dev->dma = PAR0_RX_DMA_NBR; /* fill in our handlers so the network layer can talk to us in the future */ dev->open = e100_open; dev->hard_start_xmit = e100_send_packet; dev->stop = e100_close; dev->get_stats = e100_get_stats; dev->set_multicast_list = set_multicast_list; dev->set_mac_address = e100_set_mac_address; /* Initialise the list of Etrax DMA-descriptors */ /* Initialise receive descriptors */ for(i = 0; i < (NBR_OF_RX_DESC - 1); i++) { RxDescList[i].ctrl = 0; RxDescList[i].sw_len = RX_DESC_BUF_SIZE; RxDescList[i].next = virt_to_phys(&RxDescList[i + 1]); RxDescList[i].buf = virt_to_phys(RxBuf + anOffset); RxDescList[i].status = 0; RxDescList[i].hw_len = 0; anOffset += RX_DESC_BUF_SIZE; } RxDescList[i].ctrl = d_eol; RxDescList[i].sw_len = RX_DESC_BUF_SIZE; RxDescList[i].next = virt_to_phys(&RxDescList[0]); RxDescList[i].buf = virt_to_phys(RxBuf + anOffset); RxDescList[i].status = 0; RxDescList[i].hw_len = 0; /* Initialise initial pointers */ myNextRxDesc = &RxDescList[0]; myLastRxDesc = &RxDescList[NBR_OF_RX_DESC - 1]; myPrevRxDesc = &RxDescList[NBR_OF_RX_DESC - 1]; /* setup some TX descriptor data */ TxDescList[0].sw_len = 4; TxDescList[0].ctrl = 0; TxDescList[0].buf = virt_to_phys(&host_command); TxDescList[0].next = virt_to_phys(&TxDescList[1]); return 0;}/* set MAC address of the interface. called from the core after a * SIOCSIFADDR ioctl, and from the bootup above. */static inte100_set_mac_address(struct net_device *dev, void *p){ struct sockaddr *addr = p; int i; /* remember it */ memcpy(dev->dev_addr, addr->sa_data, dev->addr_len); /* Write it to the hardware. * Note the way the address is wrapped: * *R_NETWORK_SA_0 = a0_0 | (a0_1 << 8) | (a0_2 << 16) | (a0_3 << 24); * *R_NETWORK_SA_1 = a0_4 | (a0_5 << 8); */ tx_skb = 0; e100_hardware_send_packet(HOST_CMD_SETMAC, dev->dev_addr, 6); /* show it in the log as well */ printk("%s: changed MAC to ", dev->name); for (i = 0; i < 5; i++) printk("%02X:", dev->dev_addr[i]); printk("%02X\n", dev->dev_addr[i]); return 0;}/* * Open/initialize the board. This is called (in the current kernel) * sometime after booting when the 'ifconfig' program is run. * * This routine should set everything up anew at each open, even * registers that "should" only need to be set once at boot, so that * there is non-reboot way to recover if something goes wrong. */static inte100_open(struct net_device *dev){ unsigned long flags; /* configure the PAR0 (RX) and PAR1 (TX) ports * * perror is nAckReverse, which must be 1 at the TX side, * and 0 at the RX side * * select is XFlag, which must be 1 at both sides */#ifdef ETHDEBUG printk("Setting up PAR ports\n");#endif *R_PAR0_CONFIG = /* We do not have an external buffer, don't care */ IO_STATE(R_PAR0_CONFIG, ioe, noninv) | /* Not connected, don't care */ IO_STATE(R_PAR0_CONFIG, iseli, noninv) | /* iautofd is not inverted, noninv */ IO_STATE(R_PAR0_CONFIG, iautofd, noninv) | /* Not used in reverse direction, don't care */ IO_STATE(R_PAR0_CONFIG, istrb, noninv) | /* Not connected, don't care / IO_STATE(R_PAR0_CONFIG, iinit, noninv) | /* perror is GND and reverse wants 0, noninv */ IO_STATE(R_PAR0_CONFIG, iperr, noninv) | /* ack is not inverted, noninv */ IO_STATE(R_PAR0_CONFIG, iack, noninv) | /* busy is not inverted, noninv */ IO_STATE(R_PAR0_CONFIG, ibusy, noninv) | /* fault is not inverted, noninv */ IO_STATE(R_PAR0_CONFIG, ifault, noninv) | /* select is Vcc and we want 1, noninv */ IO_STATE(R_PAR0_CONFIG, isel, noninv) | /* We will run dma, enable */ IO_STATE(R_PAR0_CONFIG, dma, enable) | /* No run length encoding, disable */ IO_STATE(R_PAR0_CONFIG, rle_in, disable) | /* No run length encoding, disable */ IO_STATE(R_PAR0_CONFIG, rle_out, disable) | /* Enable parallel port */ IO_STATE(R_PAR0_CONFIG, enable, on) | /* Force mode regardless of pin status */ IO_STATE(R_PAR0_CONFIG, force, on) | /* We want ECP forward mode since PAR0 is RX */ IO_STATE(R_PAR0_CONFIG, mode, ecp_rev); *R_PAR1_CONFIG = /* We do not have an external buffer, don't care */ IO_STATE(R_PAR1_CONFIG, ioe, noninv) | /* Not connected, don't care */ IO_STATE(R_PAR1_CONFIG, iseli, noninv) | /* HostAck must indicate data cycle, noninv */ IO_STATE(R_PAR1_CONFIG, iautofd, noninv) | /* HostClk has no external inverter, noninv */ IO_STATE(R_PAR1_CONFIG, istrb, noninv) | /* Not connected, don't care */ IO_STATE(R_PAR1_CONFIG, iinit, noninv) | /* nAckReverse must be 1 in forward mode but is grounded, inv */ IO_STATE(R_PAR1_CONFIG, iperr, inv) | /* PeriphClk must be 1 in forward mode, noninv */ IO_STATE(R_PAR1_CONFIG, iack, noninv) | /* PeriphAck has no external inverter, noninv */ IO_STATE(R_PAR1_CONFIG, ibusy, noninv) | /* nPerihpRequest has no external inverter, noniv */ IO_STATE(R_PAR1_CONFIG, ifault, noninv) | /* Select is VCC and we want 1, noninv */ IO_STATE(R_PAR1_CONFIG, isel, noninv) | /* No EPP mode, disable */ IO_STATE(R_PAR1_CONFIG, ext_mode, disable) | /* We will run dma, enable */ IO_STATE(R_PAR1_CONFIG, dma, enable) | /* No run length encoding, disable */ IO_STATE(R_PAR1_CONFIG, rle_in, disable) | /* No run length encoding, disable */ IO_STATE(R_PAR1_CONFIG, rle_out, disable) | /* Enable parallel port */ IO_STATE(R_PAR1_CONFIG, enable, on) | /* Force mode regardless of pin status */ IO_STATE(R_PAR1_CONFIG, force, on) | /* We want ECP forward mode since PAR1 is TX */ IO_STATE(R_PAR1_CONFIG, mode, ecp_fwd); /* Setup time of value * 160 + 20 ns == 20 ns below */ *R_PAR1_DELAY = IO_FIELD(R_PAR1_DELAY, setup, 0); *R_PAR1_CTRL = 0; while ((((*R_PAR1_STATUS)&0xE000) >> 13) != 5); /* Wait for ECP_FWD mode */#ifdef ETHDEBUG dump_parport_status();#endif /* make sure ECP irq is acked when we enable it below */ (void)*R_PAR0_STATUS_DATA; (void)*R_PAR1_STATUS_DATA; /* Reset and wait for the DMA channels */ RESET_DMA(4); /* PAR1_TX_DMA_NBR */ RESET_DMA(3); /* PAR0_RX_DMA_NBR */ WAIT_DMA(4); WAIT_DMA(3); /* boot the slave Etrax, by sending code on PAR1. * do this before we start up the IRQ handlers and stuff, * beacuse we simply poll for completion in boot_slave. */ boot_slave(&e100lpslaveprog); /* allocate the irq corresponding to the receiving DMA */ if (request_irq(DMA3_RX_IRQ_NBR, e100rx_interrupt, 0, cardname, (void *)dev)) { printk("Failed to allocate DMA3_RX_IRQ_NBR\n"); goto grace_exit; } /* allocate the irq corresponding to the transmitting DMA */ if (request_irq(DMA4_TX_IRQ_NBR, e100tx_interrupt, 0, cardname, (void *)dev)) { printk("Failed to allocate DMA4_TX_IRQ_NBR\n"); goto grace_exit; } /* allocate the irq used for detecting ECP commands on the RX port (PAR0) */ if (request_irq(PAR0_ECP_IRQ_NBR, ecp_interrupt, 0, cardname, (void *)dev)) { printk("Failed to allocate PAR0_ECP_IRQ_NBR\n"); grace_exit: free_irq(PAR0_ECP_IRQ_NBR, (void *)dev); free_irq(DMA4_TX_IRQ_NBR, (void *)dev); free_irq(DMA3_RX_IRQ_NBR, (void *)dev); return -EAGAIN; }#if 0 /* We are not allocating DMA since DMA4 is reserved for 'cascading' * and will always fail with the current dma.c */ /* * Always allocate the DMA channels after the IRQ, * and clean up on failure. */ if(request_dma(PAR0_RX_DMA_NBR, cardname)) { printk("Failed to allocate PAR0_RX_DMA_NBR\n"); goto grace_exit; } if(request_dma(PAR1_TX_DMA_NBR, cardname)) { printk("Failed to allocate PAR1_TX_DMA_NBR\n"); grace_exit: /* this will cause some 'trying to free free irq' but what the heck... */ free_dma(PAR1_TX_DMA_NBR); free_dma(PAR0_RX_DMA_NBR); free_irq(PAR0_ECP_IRQ_NBR, (void *)dev); free_irq(DMA4_TX_IRQ_NBR, (void *)dev); free_irq(DMA3_RX_IRQ_NBR, (void *)dev); return -EAGAIN; }#endif #ifdef ETHDEBUG printk("Par port IRQ and DMA allocated\n");#endif save_flags(flags); cli(); /* enable the irq's for PAR0/1 DMA */ *R_IRQ_MASK2_SET = IO_STATE(R_IRQ_MASK2_SET, dma3_eop, set) | IO_STATE(R_IRQ_MASK2_SET, dma4_descr, set); *R_IRQ_MASK0_SET = IO_STATE(R_IRQ_MASK0_SET, par0_ecp_cmd, set); tx_skb = 0; /* make sure the irqs are cleared */ *R_DMA_CH3_CLR_INTR = IO_STATE(R_DMA_CH3_CLR_INTR, clr_eop, do); *R_DMA_CH4_CLR_INTR = IO_STATE(R_DMA_CH4_CLR_INTR, clr_descr, do); /* Write the MAC address to the slave HW */ udelay(5000); e100_hardware_send_packet(HOST_CMD_SETMAC, dev->dev_addr, 6); /* make sure the rec and transmit error counters are cleared */ (void)*R_REC_COUNTERS; /* dummy read */ (void)*R_TR_COUNTERS; /* dummy read */ /* start the receiving DMA channel so we can receive packets from now on */ *R_DMA_CH3_FIRST = virt_to_phys(myNextRxDesc); *R_DMA_CH3_CMD = IO_STATE(R_DMA_CH3_CMD, cmd, start); restore_flags(flags); /* We are now ready to accept transmit requeusts from * the queueing layer of the networking.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -