xref: /linux/drivers/net/ethernet/8390/zorro8390.c (revision a8d70602b186f3c347e62c59a418be802b71886d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Amiga Linux/m68k and Linux/PPC Zorro NS8390 Ethernet Driver
4  *
5  *  (C) Copyright 1998-2000 by some Elitist 680x0 Users(TM)
6  *
7  *  ---------------------------------------------------------------------------
8  *
9  *  This program is based on all the other NE2000 drivers for Linux
10  *
11  *  ---------------------------------------------------------------------------
12  *
13  *  The Ariadne II and X-Surf are Zorro-II boards containing Realtek RTL8019AS
14  *  Ethernet Controllers.
15  */
16 
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/zorro.h>
27 #include <linux/jiffies.h>
28 
29 #include <asm/irq.h>
30 #include <asm/amigaints.h>
31 #include <asm/amigahw.h>
32 
33 #define EI_SHIFT(x)		(ei_local->reg_offset[x])
34 #define ei_inb(port)		in_8(port)
35 #define ei_outb(val, port)	out_8(port, val)
36 #define ei_inb_p(port)		in_8(port)
37 #define ei_outb_p(val, port)	out_8(port, val)
38 
39 static const char version[] =
40 	"8390.c:v1.10cvs 9/23/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
41 
42 #include "lib8390.c"
43 
44 #define DRV_NAME	"zorro8390"
45 
46 #define NE_BASE		(dev->base_addr)
47 #define NE_CMD		(0x00 * 2)
48 #define NE_DATAPORT	(0x10 * 2)	/* NatSemi-defined port window offset */
49 #define NE_RESET	(0x1f * 2)	/* Issue a read to reset,
50 					 * a write to clear. */
51 #define NE_IO_EXTENT	(0x20 * 2)
52 
53 #define NE_EN0_ISR	(0x07 * 2)
54 #define NE_EN0_DCFG	(0x0e * 2)
55 
56 #define NE_EN0_RSARLO	(0x08 * 2)
57 #define NE_EN0_RSARHI	(0x09 * 2)
58 #define NE_EN0_RCNTLO	(0x0a * 2)
59 #define NE_EN0_RXCR	(0x0c * 2)
60 #define NE_EN0_TXCR	(0x0d * 2)
61 #define NE_EN0_RCNTHI	(0x0b * 2)
62 #define NE_EN0_IMR	(0x0f * 2)
63 
64 #define NESM_START_PG	0x40	/* First page of TX buffer */
65 #define NESM_STOP_PG	0x80	/* Last page +1 of RX ring */
66 
67 #define WORDSWAP(a)	((((a) >> 8) & 0xff) | ((a) << 8))
68 
69 static struct card_info {
70 	zorro_id id;
71 	const char *name;
72 	unsigned int offset;
73 } cards[] = {
74 	{ ZORRO_PROD_VILLAGE_TRONIC_ARIADNE2, "Ariadne II", 0x0600 },
75 	{ ZORRO_PROD_INDIVIDUAL_COMPUTERS_X_SURF, "X-Surf", 0x8600 },
76 };
77 
78 /* Hard reset the card.  This used to pause for the same period that a
79  * 8390 reset command required, but that shouldn't be necessary.
80  */
81 static void zorro8390_reset_8390(struct net_device *dev)
82 {
83 	unsigned long reset_start_time = jiffies;
84 	struct ei_device *ei_local = netdev_priv(dev);
85 
86 	netif_dbg(ei_local, hw, dev, "resetting - t=%ld...\n", jiffies);
87 
88 	z_writeb(z_readb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
89 
90 	ei_status.txing = 0;
91 	ei_status.dmaing = 0;
92 
93 	/* This check _should_not_ be necessary, omit eventually. */
94 	while ((z_readb(NE_BASE + NE_EN0_ISR) & ENISR_RESET) == 0)
95 		if (time_after(jiffies, reset_start_time + 2 * HZ / 100)) {
96 			netdev_warn(dev, "%s: did not complete\n", __func__);
97 			break;
98 		}
99 	z_writeb(ENISR_RESET, NE_BASE + NE_EN0_ISR);	/* Ack intr */
100 }
101 
102 /* Grab the 8390 specific header. Similar to the block_input routine, but
103  * we don't need to be concerned with ring wrap as the header will be at
104  * the start of a page, so we optimize accordingly.
105  */
106 static void zorro8390_get_8390_hdr(struct net_device *dev,
107 				   struct e8390_pkt_hdr *hdr, int ring_page)
108 {
109 	int nic_base = dev->base_addr;
110 	int cnt;
111 	short *ptrs;
112 
113 	/* This *shouldn't* happen.
114 	 * If it does, it's the last thing you'll see
115 	 */
116 	if (ei_status.dmaing) {
117 		netdev_warn(dev,
118 			    "%s: DMAing conflict [DMAstat:%d][irqlock:%d]\n",
119 			    __func__, ei_status.dmaing, ei_status.irqlock);
120 		return;
121 	}
122 
123 	ei_status.dmaing |= 0x01;
124 	z_writeb(E8390_NODMA + E8390_PAGE0 + E8390_START, nic_base + NE_CMD);
125 	z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR);
126 	z_writeb(sizeof(struct e8390_pkt_hdr), nic_base + NE_EN0_RCNTLO);
127 	z_writeb(0, nic_base + NE_EN0_RCNTHI);
128 	z_writeb(0, nic_base + NE_EN0_RSARLO);		/* On page boundary */
129 	z_writeb(ring_page, nic_base + NE_EN0_RSARHI);
130 	z_writeb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
131 
132 	ptrs = (short *)hdr;
133 	for (cnt = 0; cnt < sizeof(struct e8390_pkt_hdr) >> 1; cnt++)
134 		*ptrs++ = z_readw(NE_BASE + NE_DATAPORT);
135 
136 	z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR);	/* Ack intr */
137 
138 	hdr->count = WORDSWAP(hdr->count);
139 
140 	ei_status.dmaing &= ~0x01;
141 }
142 
143 /* Block input and output, similar to the Crynwr packet driver.
144  * If you are porting to a new ethercard, look at the packet driver source
145  * for hints. The NEx000 doesn't share the on-board packet memory --
146  * you have to put the packet out through the "remote DMA" dataport
147  * using z_writeb.
148  */
149 static void zorro8390_block_input(struct net_device *dev, int count,
150 				  struct sk_buff *skb, int ring_offset)
151 {
152 	int nic_base = dev->base_addr;
153 	char *buf = skb->data;
154 	short *ptrs;
155 	int cnt;
156 
157 	/* This *shouldn't* happen.
158 	 * If it does, it's the last thing you'll see
159 	 */
160 	if (ei_status.dmaing) {
161 		netdev_err(dev, "%s: DMAing conflict [DMAstat:%d][irqlock:%d]\n",
162 			   __func__, ei_status.dmaing, ei_status.irqlock);
163 		return;
164 	}
165 	ei_status.dmaing |= 0x01;
166 	z_writeb(E8390_NODMA + E8390_PAGE0 + E8390_START, nic_base + NE_CMD);
167 	z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR);
168 	z_writeb(count & 0xff, nic_base + NE_EN0_RCNTLO);
169 	z_writeb(count >> 8, nic_base + NE_EN0_RCNTHI);
170 	z_writeb(ring_offset & 0xff, nic_base + NE_EN0_RSARLO);
171 	z_writeb(ring_offset >> 8, nic_base + NE_EN0_RSARHI);
172 	z_writeb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
173 	ptrs = (short *)buf;
174 	for (cnt = 0; cnt < count >> 1; cnt++)
175 		*ptrs++ = z_readw(NE_BASE + NE_DATAPORT);
176 	if (count & 0x01)
177 		buf[count - 1] = z_readb(NE_BASE + NE_DATAPORT);
178 
179 	z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR);	/* Ack intr */
180 	ei_status.dmaing &= ~0x01;
181 }
182 
183 static void zorro8390_block_output(struct net_device *dev, int count,
184 				   const unsigned char *buf,
185 				   const int start_page)
186 {
187 	int nic_base = NE_BASE;
188 	unsigned long dma_start;
189 	short *ptrs;
190 	int cnt;
191 
192 	/* Round the count up for word writes.  Do we need to do this?
193 	 * What effect will an odd byte count have on the 8390?
194 	 * I should check someday.
195 	 */
196 	if (count & 0x01)
197 		count++;
198 
199 	/* This *shouldn't* happen.
200 	 * If it does, it's the last thing you'll see
201 	 */
202 	if (ei_status.dmaing) {
203 		netdev_err(dev, "%s: DMAing conflict [DMAstat:%d][irqlock:%d]\n",
204 			   __func__, ei_status.dmaing, ei_status.irqlock);
205 		return;
206 	}
207 	ei_status.dmaing |= 0x01;
208 	/* We should already be in page 0, but to be safe... */
209 	z_writeb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
210 
211 	z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR);
212 
213 	/* Now the normal output. */
214 	z_writeb(count & 0xff, nic_base + NE_EN0_RCNTLO);
215 	z_writeb(count >> 8,   nic_base + NE_EN0_RCNTHI);
216 	z_writeb(0x00, nic_base + NE_EN0_RSARLO);
217 	z_writeb(start_page, nic_base + NE_EN0_RSARHI);
218 
219 	z_writeb(E8390_RWRITE + E8390_START, nic_base + NE_CMD);
220 	ptrs = (short *)buf;
221 	for (cnt = 0; cnt < count >> 1; cnt++)
222 		z_writew(*ptrs++, NE_BASE + NE_DATAPORT);
223 
224 	dma_start = jiffies;
225 
226 	while ((z_readb(NE_BASE + NE_EN0_ISR) & ENISR_RDC) == 0)
227 		if (time_after(jiffies, dma_start + 2 * HZ / 100)) {
228 					/* 20ms */
229 			netdev_warn(dev, "timeout waiting for Tx RDC\n");
230 			zorro8390_reset_8390(dev);
231 			__NS8390_init(dev, 1);
232 			break;
233 		}
234 
235 	z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR);	/* Ack intr */
236 	ei_status.dmaing &= ~0x01;
237 }
238 
239 static int zorro8390_open(struct net_device *dev)
240 {
241 	__ei_open(dev);
242 	return 0;
243 }
244 
245 static int zorro8390_close(struct net_device *dev)
246 {
247 	struct ei_device *ei_local = netdev_priv(dev);
248 
249 	netif_dbg(ei_local, ifdown, dev, "Shutting down ethercard\n");
250 	__ei_close(dev);
251 	return 0;
252 }
253 
254 static void zorro8390_remove_one(struct zorro_dev *z)
255 {
256 	struct net_device *dev = zorro_get_drvdata(z);
257 
258 	unregister_netdev(dev);
259 	free_irq(IRQ_AMIGA_PORTS, dev);
260 	release_mem_region(ZTWO_PADDR(dev->base_addr), NE_IO_EXTENT * 2);
261 	free_netdev(dev);
262 }
263 
264 static struct zorro_device_id zorro8390_zorro_tbl[] = {
265 	{ ZORRO_PROD_VILLAGE_TRONIC_ARIADNE2, },
266 	{ ZORRO_PROD_INDIVIDUAL_COMPUTERS_X_SURF, },
267 	{ 0 }
268 };
269 MODULE_DEVICE_TABLE(zorro, zorro8390_zorro_tbl);
270 
271 static const struct net_device_ops zorro8390_netdev_ops = {
272 	.ndo_open		= zorro8390_open,
273 	.ndo_stop		= zorro8390_close,
274 	.ndo_start_xmit		= __ei_start_xmit,
275 	.ndo_tx_timeout		= __ei_tx_timeout,
276 	.ndo_get_stats		= __ei_get_stats,
277 	.ndo_set_rx_mode	= __ei_set_multicast_list,
278 	.ndo_validate_addr	= eth_validate_addr,
279 	.ndo_set_mac_address	= eth_mac_addr,
280 #ifdef CONFIG_NET_POLL_CONTROLLER
281 	.ndo_poll_controller	= __ei_poll,
282 #endif
283 };
284 
285 static int zorro8390_init(struct net_device *dev, unsigned long board,
286 			  const char *name, void __iomem *ioaddr)
287 {
288 	int i;
289 	int err;
290 	unsigned char SA_prom[32];
291 	int start_page, stop_page;
292 	static u32 zorro8390_offsets[16] = {
293 		0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e,
294 		0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e,
295 	};
296 
297 	/* Reset card. Who knows what dain-bramaged state it was left in. */
298 	{
299 		unsigned long reset_start_time = jiffies;
300 
301 		z_writeb(z_readb(ioaddr + NE_RESET), ioaddr + NE_RESET);
302 
303 		while ((z_readb(ioaddr + NE_EN0_ISR) & ENISR_RESET) == 0)
304 			if (time_after(jiffies,
305 				       reset_start_time + 2 * HZ / 100)) {
306 				netdev_warn(dev, "not found (no reset ack)\n");
307 				return -ENODEV;
308 			}
309 
310 		z_writeb(0xff, ioaddr + NE_EN0_ISR);	/* Ack all intr. */
311 	}
312 
313 	/* Read the 16 bytes of station address PROM.
314 	 * We must first initialize registers,
315 	 * similar to NS8390_init(eifdev, 0).
316 	 * We can't reliably read the SAPROM address without this.
317 	 * (I learned the hard way!).
318 	 */
319 	{
320 		static const struct {
321 			u32 value;
322 			u32 offset;
323 		} program_seq[] = {
324 			{E8390_NODMA + E8390_PAGE0 + E8390_STOP, NE_CMD},
325 						/* Select page 0 */
326 			{0x48,	NE_EN0_DCFG},	/* 0x48: Set byte-wide access */
327 			{0x00,	NE_EN0_RCNTLO},	/* Clear the count regs */
328 			{0x00,	NE_EN0_RCNTHI},
329 			{0x00,	NE_EN0_IMR},	/* Mask completion irq */
330 			{0xFF,	NE_EN0_ISR},
331 			{E8390_RXOFF, NE_EN0_RXCR}, /* 0x20 Set to monitor */
332 			{E8390_TXOFF, NE_EN0_TXCR}, /* 0x02 and loopback mode */
333 			{32,	NE_EN0_RCNTLO},
334 			{0x00,	NE_EN0_RCNTHI},
335 			{0x00,	NE_EN0_RSARLO},	/* DMA starting at 0x0000 */
336 			{0x00,	NE_EN0_RSARHI},
337 			{E8390_RREAD + E8390_START, NE_CMD},
338 		};
339 		for (i = 0; i < ARRAY_SIZE(program_seq); i++)
340 			z_writeb(program_seq[i].value,
341 				 ioaddr + program_seq[i].offset);
342 	}
343 	for (i = 0; i < 16; i++) {
344 		SA_prom[i] = z_readb(ioaddr + NE_DATAPORT);
345 		(void)z_readb(ioaddr + NE_DATAPORT);
346 	}
347 
348 	/* We must set the 8390 for word mode. */
349 	z_writeb(0x49, ioaddr + NE_EN0_DCFG);
350 	start_page = NESM_START_PG;
351 	stop_page = NESM_STOP_PG;
352 
353 	dev->base_addr = (unsigned long)ioaddr;
354 	dev->irq = IRQ_AMIGA_PORTS;
355 
356 	/* Install the Interrupt handler */
357 	i = request_irq(IRQ_AMIGA_PORTS, __ei_interrupt,
358 			IRQF_SHARED, DRV_NAME, dev);
359 	if (i)
360 		return i;
361 
362 	eth_hw_addr_set(dev, SA_prom);
363 
364 	pr_debug("Found ethernet address: %pM\n", dev->dev_addr);
365 
366 	ei_status.name = name;
367 	ei_status.tx_start_page = start_page;
368 	ei_status.stop_page = stop_page;
369 	ei_status.word16 = 1;
370 
371 	ei_status.rx_start_page = start_page + TX_PAGES;
372 
373 	ei_status.reset_8390 = zorro8390_reset_8390;
374 	ei_status.block_input = zorro8390_block_input;
375 	ei_status.block_output = zorro8390_block_output;
376 	ei_status.get_8390_hdr = zorro8390_get_8390_hdr;
377 	ei_status.reg_offset = zorro8390_offsets;
378 
379 	dev->netdev_ops = &zorro8390_netdev_ops;
380 	__NS8390_init(dev, 0);
381 
382 	err = register_netdev(dev);
383 	if (err) {
384 		free_irq(IRQ_AMIGA_PORTS, dev);
385 		return err;
386 	}
387 
388 	netdev_info(dev, "%s at 0x%08lx, Ethernet Address %pM\n",
389 		    name, board, dev->dev_addr);
390 
391 	return 0;
392 }
393 
394 static int zorro8390_init_one(struct zorro_dev *z,
395 			      const struct zorro_device_id *ent)
396 {
397 	struct net_device *dev;
398 	unsigned long board, ioaddr;
399 	int err, i;
400 
401 	for (i = ARRAY_SIZE(cards) - 1; i >= 0; i--)
402 		if (z->id == cards[i].id)
403 			break;
404 	if (i < 0)
405 		return -ENODEV;
406 
407 	board = z->resource.start;
408 	ioaddr = board + cards[i].offset;
409 	dev = ____alloc_ei_netdev(0);
410 	if (!dev)
411 		return -ENOMEM;
412 	if (!request_mem_region(ioaddr, NE_IO_EXTENT * 2, DRV_NAME)) {
413 		free_netdev(dev);
414 		return -EBUSY;
415 	}
416 	err = zorro8390_init(dev, board, cards[i].name, ZTWO_VADDR(ioaddr));
417 	if (err) {
418 		release_mem_region(ioaddr, NE_IO_EXTENT * 2);
419 		free_netdev(dev);
420 		return err;
421 	}
422 	zorro_set_drvdata(z, dev);
423 	return 0;
424 }
425 
426 static struct zorro_driver zorro8390_driver = {
427 	.name		= "zorro8390",
428 	.id_table	= zorro8390_zorro_tbl,
429 	.probe		= zorro8390_init_one,
430 	.remove		= zorro8390_remove_one,
431 };
432 
433 static int __init zorro8390_init_module(void)
434 {
435 	return zorro_register_driver(&zorro8390_driver);
436 }
437 
438 static void __exit zorro8390_cleanup_module(void)
439 {
440 	zorro_unregister_driver(&zorro8390_driver);
441 }
442 
443 module_init(zorro8390_init_module);
444 module_exit(zorro8390_cleanup_module);
445 
446 MODULE_LICENSE("GPL");
447