hydra.c 6.6 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* New Hydra driver using generic 8390 core */
/* Based on old hydra driver by Topi Kanerva (topi@susanna.oulu.fi) */

/* This file is subject to the terms and conditions of the GNU General      */
/* Public License.  See the file COPYING in the main directory of the       */
/* Linux distribution for more details.                                     */

/* Peter De Schrijver (p2@mind.be) */
/* Oldenburg 2000 */

/* The Amiganet is a Zorro-II board made by Hydra Systems. It contains a    */
/* NS8390 NIC (network interface controller) clone, 16 or 64K on-board RAM  */
/* and 10BASE-2 (thin coax) and AUI connectors.                             */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/ioport.h>
Linus Torvalds's avatar
Linus Torvalds committed
20
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <linux/interrupt.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/init.h>

#include <asm/bitops.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/amigaints.h>
#include <asm/amigahw.h>
#include <linux/zorro.h>

#include "8390.h"

#define NE_EN0_DCFG     (0x0e*2)

#define NESM_START_PG   0x0    /* First page of TX buffer */
#define NESM_STOP_PG    0x40    /* Last page +1 of RX ring */

#define HYDRA_NIC_BASE 0xffe1
#define HYDRA_ADDRPROM 0xffc0
#define HYDRA_VERSION "v3.0alpha"

#define WORDSWAP(a)     ((((a)>>8)&0xff) | ((a)<<8))

Linus Torvalds's avatar
Linus Torvalds committed
47
static struct net_device *root_hydra_dev;
Linus Torvalds's avatar
Linus Torvalds committed
48 49

static int __init hydra_probe(void);
50
static int __init hydra_init(unsigned long board);
Linus Torvalds's avatar
Linus Torvalds committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
static int hydra_open(struct net_device *dev);
static int hydra_close(struct net_device *dev);
static void hydra_reset_8390(struct net_device *dev);
static void hydra_get_8390_hdr(struct net_device *dev,
			       struct e8390_pkt_hdr *hdr, int ring_page);
static void hydra_block_input(struct net_device *dev, int count,
			      struct sk_buff *skb, int ring_offset);
static void hydra_block_output(struct net_device *dev, int count,
			       const unsigned char *buf, int start_page);
static void __exit hydra_cleanup(void);

static int __init hydra_probe(void)
{
    struct zorro_dev *z = NULL;
    unsigned long board;
    int err = -ENODEV;

    while ((z = zorro_find_device(ZORRO_PROD_HYDRA_SYSTEMS_AMIGANET, z))) {
	board = z->resource.start;
	if (!request_mem_region(board, 0x10000, "Hydra"))
	    continue;
	if ((err = hydra_init(ZTWO_VADDR(board)))) {
	    release_mem_region(board, 0x10000);
	    return err;
	}
	err = 0;
    }

Linus Torvalds's avatar
Linus Torvalds committed
79
    if (err == -ENODEV)
Linus Torvalds's avatar
Linus Torvalds committed
80
	printk("No Hydra ethernet card found.\n");
Linus Torvalds's avatar
Linus Torvalds committed
81

Linus Torvalds's avatar
Linus Torvalds committed
82 83 84
    return err;
}

85
static int __init hydra_init(unsigned long board)
Linus Torvalds's avatar
Linus Torvalds committed
86 87 88
{
    struct net_device *dev;
    unsigned long ioaddr = board+HYDRA_NIC_BASE;
89
    const char name[] = "NE2000";
Linus Torvalds's avatar
Linus Torvalds committed
90 91 92 93 94 95 96 97
    int start_page, stop_page;
    int j;

    static u32 hydra_offsets[16] = {
	0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e,
	0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e,
    };

Linus Torvalds's avatar
Linus Torvalds committed
98
    dev = init_etherdev(NULL, 0);
Linus Torvalds's avatar
Linus Torvalds committed
99 100
    if (!dev)
	return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
101
    SET_MODULE_OWNER(dev);
Linus Torvalds's avatar
Linus Torvalds committed
102 103 104 105 106

    for(j = 0; j < ETHER_ADDR_LEN; j++)
	dev->dev_addr[j] = *((u8 *)(board + HYDRA_ADDRPROM + 2*j));

    /* We must set the 8390 for word mode. */
107
    z_writeb(0x4b, ioaddr + NE_EN0_DCFG);
Linus Torvalds's avatar
Linus Torvalds committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    start_page = NESM_START_PG;
    stop_page = NESM_STOP_PG;

    dev->base_addr = ioaddr;
    dev->irq = IRQ_AMIGA_PORTS;

    /* Install the Interrupt handler */
    if (request_irq(IRQ_AMIGA_PORTS, ei_interrupt, SA_SHIRQ, "Hydra Ethernet",
		    dev))
	return -EAGAIN;

    /* Allocate dev->priv and fill in 8390 specific dev fields. */
    if (ethdev_init(dev)) {
	printk("Unable to get memory for dev->priv.\n");
	return -ENOMEM;
    }

    printk("%s: hydra at 0x%08lx, address %02x:%02x:%02x:%02x:%02x:%02x (hydra.c " HYDRA_VERSION ")\n", dev->name, ZTWO_PADDR(board),
	dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
	dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);

    ei_status.name = name;
    ei_status.tx_start_page = start_page;
    ei_status.stop_page = stop_page;
    ei_status.word16 = 1;
    ei_status.bigendian = 1;

    ei_status.rx_start_page = start_page + TX_PAGES;

    ei_status.reset_8390 = &hydra_reset_8390;
    ei_status.block_input = &hydra_block_input;
    ei_status.block_output = &hydra_block_output;
    ei_status.get_8390_hdr = &hydra_get_8390_hdr;
    ei_status.reg_offset = hydra_offsets;
    dev->open = &hydra_open;
    dev->stop = &hydra_close;
#ifdef MODULE
    ei_status.priv = (unsigned long)root_hydra_dev;
    root_hydra_dev = dev;
#endif
    NS8390_init(dev, 0);
    return 0;
}

static int hydra_open(struct net_device *dev)
{
    ei_open(dev);
    return 0;
}

static int hydra_close(struct net_device *dev)
{
    if (ei_debug > 1)
	printk("%s: Shutting down ethercard.\n", dev->name);
    ei_close(dev);
    return 0;
}

static void hydra_reset_8390(struct net_device *dev)
{
    printk("Hydra hw reset not there\n");
}

static void hydra_get_8390_hdr(struct net_device *dev,
			       struct e8390_pkt_hdr *hdr, int ring_page)
{
    int nic_base = dev->base_addr;
    short *ptrs;
    unsigned long hdr_start= (nic_base-HYDRA_NIC_BASE) +
			     ((ring_page - NESM_START_PG)<<8);
    ptrs = (short *)hdr;

180
    *(ptrs++) = z_readw(hdr_start);
Linus Torvalds's avatar
Linus Torvalds committed
181 182
    *((short *)hdr) = WORDSWAP(*((short *)hdr));
    hdr_start += 2;
183
    *(ptrs++) = z_readw(hdr_start);
Linus Torvalds's avatar
Linus Torvalds committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    *((short *)hdr+1) = WORDSWAP(*((short *)hdr+1));
}

static void hydra_block_input(struct net_device *dev, int count,
			      struct sk_buff *skb, int ring_offset)
{
    unsigned long nic_base = dev->base_addr;
    unsigned long mem_base = nic_base - HYDRA_NIC_BASE;
    unsigned long xfer_start = mem_base + ring_offset - (NESM_START_PG<<8);

    if (count&1)
	count++;

    if (xfer_start+count >  mem_base + (NESM_STOP_PG<<8)) {
	int semi_count = (mem_base + (NESM_STOP_PG<<8)) - xfer_start;

200
	z_memcpy_fromio(skb->data,xfer_start,semi_count);
Linus Torvalds's avatar
Linus Torvalds committed
201
	count -= semi_count;
202
	z_memcpy_fromio(skb->data+semi_count, mem_base, count);
Linus Torvalds's avatar
Linus Torvalds committed
203
    } else
204
	z_memcpy_fromio(skb->data, xfer_start,count);
Linus Torvalds's avatar
Linus Torvalds committed
205 206 207 208 209 210 211 212 213 214 215 216

}

static void hydra_block_output(struct net_device *dev, int count,
			       const unsigned char *buf, int start_page)
{
    unsigned long nic_base = dev->base_addr;
    unsigned long mem_base = nic_base - HYDRA_NIC_BASE;

    if (count&1)
	count++;

217
    z_memcpy_toio(mem_base+((start_page - NESM_START_PG)<<8), buf, count);
Linus Torvalds's avatar
Linus Torvalds committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
}

static void __exit hydra_cleanup(void)
{
    struct net_device *dev, *next;

    while ((dev = root_hydra_dev)) {
	next = (struct net_device *)(ei_status.priv);
	unregister_netdev(dev);
	free_irq(IRQ_AMIGA_PORTS, dev);
	release_mem_region(ZTWO_PADDR(dev->base_addr)-HYDRA_NIC_BASE, 0x10000);
	kfree(dev);
	root_hydra_dev = next;
    }
}

module_init(hydra_probe);
module_exit(hydra_cleanup);
Linus Torvalds's avatar
Linus Torvalds committed
236
MODULE_LICENSE("GPL");