1 /* New Hydra driver using generic 8390 core */
2 /* Based on old hydra driver by Topi Kanerva (topi@susanna.oulu.fi) */
3 
4 /* This file is subject to the terms and conditions of the GNU General      */
5 /* Public License.  See the file COPYING in the main directory of the       */
6 /* Linux distribution for more details.                                     */
7 
8 /* Peter De Schrijver (p2@mind.be) */
9 /* Oldenburg 2000 */
10 
11 /* The Amiganet is a Zorro-II board made by Hydra Systems. It contains a    */
12 /* NS8390 NIC (network interface controller) clone, 16 or 64K on-board RAM  */
13 /* and 10BASE-2 (thin coax) and AUI connectors.                             */
14 
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/string.h>
19 #include <linux/ptrace.h>
20 #include <linux/errno.h>
21 #include <linux/ioport.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/skbuff.h>
27 #include <linux/init.h>
28 
29 #include <asm/bitops.h>
30 #include <asm/io.h>
31 #include <asm/irq.h>
32 
33 #include <asm/amigaints.h>
34 #include <asm/amigahw.h>
35 #include <linux/zorro.h>
36 
37 #include "8390.h"
38 
39 #define NE_BASE		(dev->base_addr)
40 #define NE_CMD		(0x00*2)
41 
42 #define NE_EN0_ISR      (0x07*2)
43 #define NE_EN0_DCFG     (0x0e*2)
44 
45 #define NE_EN0_RSARLO   (0x08*2)
46 #define NE_EN0_RSARHI   (0x09*2)
47 #define NE_EN0_RCNTLO   (0x0a*2)
48 #define NE_EN0_RXCR     (0x0c*2)
49 #define NE_EN0_TXCR     (0x0d*2)
50 #define NE_EN0_RCNTHI   (0x0b*2)
51 #define NE_EN0_IMR      (0x0f*2)
52 
53 #define NESM_START_PG   0x0    /* First page of TX buffer */
54 #define NESM_STOP_PG    0x40    /* Last page +1 of RX ring */
55 
56 #define HYDRA_NIC_BASE 0xffe1
57 #define HYDRA_ADDRPROM 0xffc0
58 #define HYDRA_VERSION "v3.0alpha"
59 
60 #define WORDSWAP(a)     ((((a)>>8)&0xff) | ((a)<<8))
61 
62 #ifdef MODULE
63 static struct net_device *root_hydra_dev;
64 #endif
65 
66 static int __init hydra_probe(void);
67 static int hydra_init(unsigned long board);
68 static int hydra_open(struct net_device *dev);
69 static int hydra_close(struct net_device *dev);
70 static void hydra_reset_8390(struct net_device *dev);
71 static void hydra_get_8390_hdr(struct net_device *dev,
72 			       struct e8390_pkt_hdr *hdr, int ring_page);
73 static void hydra_block_input(struct net_device *dev, int count,
74 			      struct sk_buff *skb, int ring_offset);
75 static void hydra_block_output(struct net_device *dev, int count,
76 			       const unsigned char *buf, int start_page);
77 static void __exit hydra_cleanup(void);
78 
hydra_probe(void)79 static int __init hydra_probe(void)
80 {
81     struct zorro_dev *z = NULL;
82     unsigned long board;
83     int err = -ENODEV;
84 
85     while ((z = zorro_find_device(ZORRO_PROD_HYDRA_SYSTEMS_AMIGANET, z))) {
86 	board = z->resource.start;
87 	if (!request_mem_region(board, 0x10000, "Hydra"))
88 	    continue;
89 	if ((err = hydra_init(ZTWO_VADDR(board)))) {
90 	    release_mem_region(board, 0x10000);
91 	    return err;
92 	}
93 	err = 0;
94     }
95 
96     if (err == -ENODEV)
97 	printk("No Hydra ethernet card found.\n");
98 
99     return err;
100 }
101 
hydra_init(unsigned long board)102 int __init hydra_init(unsigned long board)
103 {
104     struct net_device *dev;
105     unsigned long ioaddr = board+HYDRA_NIC_BASE;
106     const char *name = NULL;
107     int start_page, stop_page;
108     int j;
109 
110     static u32 hydra_offsets[16] = {
111 	0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e,
112 	0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e,
113     };
114 
115     dev = init_etherdev(NULL, 0);
116     if (!dev)
117 	return -ENOMEM;
118     SET_MODULE_OWNER(dev);
119 
120     for(j = 0; j < ETHER_ADDR_LEN; j++)
121 	dev->dev_addr[j] = *((u8 *)(board + HYDRA_ADDRPROM + 2*j));
122 
123     /* We must set the 8390 for word mode. */
124     z_writeb(0x4b, ioaddr + NE_EN0_DCFG);
125     start_page = NESM_START_PG;
126     stop_page = NESM_STOP_PG;
127 
128     dev->base_addr = ioaddr;
129     dev->irq = IRQ_AMIGA_PORTS;
130 
131     /* Install the Interrupt handler */
132     if (request_irq(IRQ_AMIGA_PORTS, ei_interrupt, SA_SHIRQ, "Hydra Ethernet",
133 		    dev))
134 	return -EAGAIN;
135 
136     /* Allocate dev->priv and fill in 8390 specific dev fields. */
137     if (ethdev_init(dev)) {
138 	printk("Unable to get memory for dev->priv.\n");
139 	return -ENOMEM;
140     }
141 
142     name = "NE2000";
143 
144     printk("%s: hydra at 0x%08lx, address %02x:%02x:%02x:%02x:%02x:%02x (hydra.c " HYDRA_VERSION ")\n", dev->name, ZTWO_PADDR(board),
145 	dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
146 	dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
147 
148     ei_status.name = name;
149     ei_status.tx_start_page = start_page;
150     ei_status.stop_page = stop_page;
151     ei_status.word16 = 1;
152     ei_status.bigendian = 1;
153 
154     ei_status.rx_start_page = start_page + TX_PAGES;
155 
156     ei_status.reset_8390 = &hydra_reset_8390;
157     ei_status.block_input = &hydra_block_input;
158     ei_status.block_output = &hydra_block_output;
159     ei_status.get_8390_hdr = &hydra_get_8390_hdr;
160     ei_status.reg_offset = hydra_offsets;
161     dev->open = &hydra_open;
162     dev->stop = &hydra_close;
163 #ifdef MODULE
164     ei_status.priv = (unsigned long)root_hydra_dev;
165     root_hydra_dev = dev;
166 #endif
167     NS8390_init(dev, 0);
168     return 0;
169 }
170 
hydra_open(struct net_device * dev)171 static int hydra_open(struct net_device *dev)
172 {
173     ei_open(dev);
174     return 0;
175 }
176 
hydra_close(struct net_device * dev)177 static int hydra_close(struct net_device *dev)
178 {
179     if (ei_debug > 1)
180 	printk("%s: Shutting down ethercard.\n", dev->name);
181     ei_close(dev);
182     return 0;
183 }
184 
hydra_reset_8390(struct net_device * dev)185 static void hydra_reset_8390(struct net_device *dev)
186 {
187     printk("Hydra hw reset not there\n");
188 }
189 
hydra_get_8390_hdr(struct net_device * dev,struct e8390_pkt_hdr * hdr,int ring_page)190 static void hydra_get_8390_hdr(struct net_device *dev,
191 			       struct e8390_pkt_hdr *hdr, int ring_page)
192 {
193     int nic_base = dev->base_addr;
194     short *ptrs;
195     unsigned long hdr_start= (nic_base-HYDRA_NIC_BASE) +
196 			     ((ring_page - NESM_START_PG)<<8);
197     ptrs = (short *)hdr;
198 
199     *(ptrs++) = z_readw(hdr_start);
200     *((short *)hdr) = WORDSWAP(*((short *)hdr));
201     hdr_start += 2;
202     *(ptrs++) = z_readw(hdr_start);
203     *((short *)hdr+1) = WORDSWAP(*((short *)hdr+1));
204 }
205 
hydra_block_input(struct net_device * dev,int count,struct sk_buff * skb,int ring_offset)206 static void hydra_block_input(struct net_device *dev, int count,
207 			      struct sk_buff *skb, int ring_offset)
208 {
209     unsigned long nic_base = dev->base_addr;
210     unsigned long mem_base = nic_base - HYDRA_NIC_BASE;
211     unsigned long xfer_start = mem_base + ring_offset - (NESM_START_PG<<8);
212 
213     if (count&1)
214 	count++;
215 
216     if (xfer_start+count >  mem_base + (NESM_STOP_PG<<8)) {
217 	int semi_count = (mem_base + (NESM_STOP_PG<<8)) - xfer_start;
218 
219 	z_memcpy_fromio(skb->data,xfer_start,semi_count);
220 	count -= semi_count;
221 	z_memcpy_fromio(skb->data+semi_count, mem_base, count);
222     } else
223 	z_memcpy_fromio(skb->data, xfer_start,count);
224 
225 }
226 
hydra_block_output(struct net_device * dev,int count,const unsigned char * buf,int start_page)227 static void hydra_block_output(struct net_device *dev, int count,
228 			       const unsigned char *buf, int start_page)
229 {
230     unsigned long nic_base = dev->base_addr;
231     unsigned long mem_base = nic_base - HYDRA_NIC_BASE;
232 
233     if (count&1)
234 	count++;
235 
236     z_memcpy_toio(mem_base+((start_page - NESM_START_PG)<<8), buf, count);
237 }
238 
hydra_cleanup(void)239 static void __exit hydra_cleanup(void)
240 {
241 #ifdef MODULE
242     struct net_device *dev, *next;
243 
244     while ((dev = root_hydra_dev)) {
245 	next = (struct net_device *)(ei_status.priv);
246 	unregister_netdev(dev);
247 	free_irq(IRQ_AMIGA_PORTS, dev);
248 	release_mem_region(ZTWO_PADDR(dev->base_addr)-HYDRA_NIC_BASE, 0x10000);
249 	kfree(dev);
250 	root_hydra_dev = next;
251     }
252 #endif
253 }
254 
255 module_init(hydra_probe);
256 module_exit(hydra_cleanup);
257 MODULE_LICENSE("GPL");
258