xref: /minix3/minix/drivers/net/dpeth/ne.c (revision f7df02e7476731c31f12548e38bcadbaf0233f6a)
1433d6423SLionel Sambuc /*
2433d6423SLionel Sambuc **  File:	ne.c	Jun. 08, 2000
3433d6423SLionel Sambuc **
4433d6423SLionel Sambuc **  Driver for the NE*000 ethernet cards and derivates.
5433d6423SLionel Sambuc **  This file contains only the ne specific code,
6433d6423SLionel Sambuc **  the rest is in 8390.c   Code specific for ISA bus only
7433d6423SLionel Sambuc **
8433d6423SLionel Sambuc **  Created:	March 15, 1994 by Philip Homburg <philip@cs.vu.nl>
9433d6423SLionel Sambuc **  PchId: 	ne2000.c,v 1.4 1996/01/19 23:30:34 philip Exp
10433d6423SLionel Sambuc **
11433d6423SLionel Sambuc **  Modified:	Jun. 08, 2000 by Giovanni Falzoni <gfalzoni@inwind.it>
12433d6423SLionel Sambuc **  		Adapted to interface new main network task.
13433d6423SLionel Sambuc */
14433d6423SLionel Sambuc 
15433d6423SLionel Sambuc #include <minix/drivers.h>
1691c4db25SDavid van Moolenbroek #include <minix/netdriver.h>
17433d6423SLionel Sambuc #include "dp.h"
18433d6423SLionel Sambuc 
19433d6423SLionel Sambuc #if (ENABLE_NE2000 == 1)
20433d6423SLionel Sambuc 
21433d6423SLionel Sambuc #include "8390.h"
22433d6423SLionel Sambuc #include "ne.h"
23433d6423SLionel Sambuc 
24433d6423SLionel Sambuc /*
2591c4db25SDavid van Moolenbroek **  Name:	ne_reset
26433d6423SLionel Sambuc **  Function:	Resets the board and checks if reset cycle completes
27433d6423SLionel Sambuc */
ne_reset(dpeth_t * dep)28433d6423SLionel Sambuc static int ne_reset(dpeth_t * dep)
29433d6423SLionel Sambuc {
30433d6423SLionel Sambuc   int count = 0;
31433d6423SLionel Sambuc 
32433d6423SLionel Sambuc   /* Reset the ethernet card */
33433d6423SLionel Sambuc   outb_ne(dep, NE_RESET, inb_ne(dep, NE_RESET));
34433d6423SLionel Sambuc   do {
35433d6423SLionel Sambuc 	if (++count > 10) return FALSE;	/* 20 mSecs. timeout */
3691c4db25SDavid van Moolenbroek 	micro_delay(2000);
37433d6423SLionel Sambuc   } while ((inb_ne(dep, DP_ISR) & ISR_RST) == 0);
38433d6423SLionel Sambuc   return TRUE;
39433d6423SLionel Sambuc }
40433d6423SLionel Sambuc 
41433d6423SLionel Sambuc /*
4291c4db25SDavid van Moolenbroek **  Name:	ne_close
43433d6423SLionel Sambuc **  Function:	Stops the board by resetting it and masking interrupts.
44433d6423SLionel Sambuc */
ne_close(dpeth_t * dep)45433d6423SLionel Sambuc static void ne_close(dpeth_t * dep)
46433d6423SLionel Sambuc {
47433d6423SLionel Sambuc 
48433d6423SLionel Sambuc   (void)ne_reset(dep);
49433d6423SLionel Sambuc   outb_ne(dep, DP_ISR, 0xFF);
50433d6423SLionel Sambuc   sys_irqdisable(&dep->de_hook);
51433d6423SLionel Sambuc }
52433d6423SLionel Sambuc 
53433d6423SLionel Sambuc /*
5491c4db25SDavid van Moolenbroek **  Name:	ne_init
55433d6423SLionel Sambuc **  Function:	Initialize the board making it ready to work.
56433d6423SLionel Sambuc */
ne_init(dpeth_t * dep)57433d6423SLionel Sambuc static void ne_init(dpeth_t * dep)
58433d6423SLionel Sambuc {
59*f7df02e7SDavid van Moolenbroek   unsigned int ix;
60433d6423SLionel Sambuc 
61433d6423SLionel Sambuc   dep->de_data_port = dep->de_base_port + NE_DATA;
62433d6423SLionel Sambuc   if (dep->de_16bit) {
63433d6423SLionel Sambuc 	dep->de_ramsize = NE2000_SIZE;
64433d6423SLionel Sambuc 	dep->de_offset_page = NE2000_START / DP_PAGESIZE;
65433d6423SLionel Sambuc   } else {
66433d6423SLionel Sambuc 	dep->de_ramsize = NE1000_SIZE;
67433d6423SLionel Sambuc 	dep->de_offset_page = NE1000_START / DP_PAGESIZE;
68433d6423SLionel Sambuc   }
69433d6423SLionel Sambuc 
70433d6423SLionel Sambuc   /* Allocates two send buffers from onboard RAM */
71433d6423SLionel Sambuc   dep->de_sendq_nr = SENDQ_NR;
72433d6423SLionel Sambuc   for (ix = 0; ix < SENDQ_NR; ix += 1) {
73433d6423SLionel Sambuc 	dep->de_sendq[ix].sq_sendpage = dep->de_offset_page + ix * SENDQ_PAGES;
74433d6423SLionel Sambuc   }
75433d6423SLionel Sambuc 
76433d6423SLionel Sambuc   /* Remaining onboard RAM allocated for receiving */
77433d6423SLionel Sambuc   dep->de_startpage = dep->de_offset_page + ix * SENDQ_PAGES;
78433d6423SLionel Sambuc   dep->de_stoppage = dep->de_offset_page + dep->de_ramsize / DP_PAGESIZE;
79433d6423SLionel Sambuc 
80433d6423SLionel Sambuc   /* Can't override the default IRQ. */
81433d6423SLionel Sambuc   dep->de_irq &= NOT(DEI_DEFAULT);
82433d6423SLionel Sambuc 
83433d6423SLionel Sambuc   ns_init(dep);			/* Initialize DP controller */
84433d6423SLionel Sambuc 
85433d6423SLionel Sambuc   printf("%s: NE%d000 (%dkB RAM) at %X:%d - ",
86*f7df02e7SDavid van Moolenbroek          netdriver_name(),
87433d6423SLionel Sambuc          dep->de_16bit ? 2 : 1,
88433d6423SLionel Sambuc          dep->de_ramsize / 1024,
89433d6423SLionel Sambuc          dep->de_base_port, dep->de_irq);
90433d6423SLionel Sambuc   for (ix = 0; ix < SA_ADDR_LEN; ix += 1)
91*f7df02e7SDavid van Moolenbroek 	printf("%02X%c", dep->de_address.na_addr[ix],
9291c4db25SDavid van Moolenbroek 	    ix < SA_ADDR_LEN - 1 ? ':' : '\n');
93433d6423SLionel Sambuc }
94433d6423SLionel Sambuc 
95433d6423SLionel Sambuc /*
9691c4db25SDavid van Moolenbroek **  Name:	ne_probe
97433d6423SLionel Sambuc **  Function:	Probe for the presence of a NE*000 card by testing
98433d6423SLionel Sambuc **  		whether the board is reachable through the dp8390.
99433d6423SLionel Sambuc **  		Note that the NE1000 is an 8bit card and has a memory
100433d6423SLionel Sambuc **  		region distict from the 16bit NE2000.
101433d6423SLionel Sambuc */
ne_probe(dpeth_t * dep)102433d6423SLionel Sambuc int ne_probe(dpeth_t * dep)
103433d6423SLionel Sambuc {
104*f7df02e7SDavid van Moolenbroek   unsigned int ix, wd, loc1, loc2;
105433d6423SLionel Sambuc   char EPROM[32];
106433d6423SLionel Sambuc   static const struct {
107433d6423SLionel Sambuc 	unsigned char offset;
108433d6423SLionel Sambuc 	unsigned char value;
109433d6423SLionel Sambuc   } InitSeq[] =
110433d6423SLionel Sambuc   {
111433d6423SLionel Sambuc 	{	/* Selects page 0. */
112433d6423SLionel Sambuc 		DP_CR, (CR_NO_DMA | CR_PS_P0 | CR_STP),
113433d6423SLionel Sambuc 	},
114433d6423SLionel Sambuc 	{	/* Set byte-wide access and 8 bytes burst mode. */
115433d6423SLionel Sambuc 		DP_DCR, (DCR_8BYTES | DCR_BMS),
116433d6423SLionel Sambuc 	},
117433d6423SLionel Sambuc 	{	/* Clears the count registers. */
118433d6423SLionel Sambuc 		DP_RBCR0, 0x00, }, {	DP_RBCR1, 0x00,
119433d6423SLionel Sambuc 	},
120433d6423SLionel Sambuc 	{	/* Mask completion irq. */
121433d6423SLionel Sambuc 		DP_IMR, 0x00, }, {	DP_ISR, 0xFF,
122433d6423SLionel Sambuc 	},
123433d6423SLionel Sambuc 	{	/* Set receiver to monitor */
124433d6423SLionel Sambuc 		DP_RCR, RCR_MON,
125433d6423SLionel Sambuc 	},
126433d6423SLionel Sambuc 	{	/* and transmitter to loopback mode. */
127433d6423SLionel Sambuc 		DP_TCR, TCR_INTERNAL,
128433d6423SLionel Sambuc 	},
129433d6423SLionel Sambuc 	{	/* Transmit 32 bytes */
130433d6423SLionel Sambuc 		DP_RBCR0, 32, }, { DP_RBCR1, 0,
131433d6423SLionel Sambuc 	},
132433d6423SLionel Sambuc 	{	/* DMA starting at 0x0000. */
133433d6423SLionel Sambuc 		DP_RSAR0, 0x00, }, { DP_RSAR1, 0x00,
134433d6423SLionel Sambuc 	},
135433d6423SLionel Sambuc 	{			/* Start board (reads) */
136433d6423SLionel Sambuc 		DP_CR, (CR_PS_P0 | CR_DM_RR | CR_STA),
137433d6423SLionel Sambuc 	},
138433d6423SLionel Sambuc   };
139433d6423SLionel Sambuc 
140433d6423SLionel Sambuc   dep->de_dp8390_port = dep->de_base_port + NE_DP8390;
141433d6423SLionel Sambuc 
142433d6423SLionel Sambuc   if ((loc1 = inb_ne(dep, NE_DP8390)) == 0xFF) return FALSE;
143433d6423SLionel Sambuc 
144433d6423SLionel Sambuc   /* Check if the dp8390 is really there */
145433d6423SLionel Sambuc   outb_ne(dep, DP_CR, CR_STP | CR_NO_DMA | CR_PS_P1);
146433d6423SLionel Sambuc   loc2 = inb_ne(dep, DP_MAR5);	/* Saves one byte of the address */
147433d6423SLionel Sambuc   outb_ne(dep, DP_MAR5, 0xFF);	/* Write 0xFF to it (same offset as DP_CNTR0) */
148433d6423SLionel Sambuc   outb_ne(dep, DP_CR, CR_NO_DMA | CR_PS_P0);	/* Back to page 0 */
149433d6423SLionel Sambuc   inb_ne(dep, DP_CNTR0);	/* Reading counter resets it */
150433d6423SLionel Sambuc   if (inb_ne(dep, DP_CNTR0) != 0) {
151433d6423SLionel Sambuc 	outb_ne(dep, NE_DP8390, loc1);	/* Try to restore modified bytes */
152433d6423SLionel Sambuc 	outb_ne(dep, DP_TCR, loc2);
153433d6423SLionel Sambuc 	return FALSE;
154433d6423SLionel Sambuc   }
155433d6423SLionel Sambuc 
156433d6423SLionel Sambuc   /* Try to reset the board */
157433d6423SLionel Sambuc   if (ne_reset(dep) == FALSE) return FALSE;
158433d6423SLionel Sambuc 
159433d6423SLionel Sambuc   /* Checks whether the board is 8/16bits and a real NE*000 or clone */
160433d6423SLionel Sambuc   for (ix = 0; ix < sizeof(InitSeq)/sizeof(InitSeq[0]); ix += 1) {
161433d6423SLionel Sambuc 	outb_ne(dep, InitSeq[ix].offset, InitSeq[ix].value);
162433d6423SLionel Sambuc   }
163433d6423SLionel Sambuc   for (ix = 0, wd = 1; ix < 32; ix += 2) {
164433d6423SLionel Sambuc 	EPROM[ix + 0] = inb_ne(dep, NE_DATA);
165433d6423SLionel Sambuc 	EPROM[ix + 1] = inb_ne(dep, NE_DATA);
166433d6423SLionel Sambuc 	/* NE2000s and clones read same value for even and odd addresses */
167433d6423SLionel Sambuc 	if (EPROM[ix + 0] != EPROM[ix + 1]) wd = 0;
168433d6423SLionel Sambuc   }
169433d6423SLionel Sambuc   if (wd == 1) {	/* Normalize EPROM contents for NE2000 */
170433d6423SLionel Sambuc 	for (ix = 0; ix < 16; ix += 1) EPROM[ix] = EPROM[ix * 2];
171433d6423SLionel Sambuc   }
172433d6423SLionel Sambuc   /* Real NE*000 and good clones have '0x57' at locations 14 and 15 */
173433d6423SLionel Sambuc   if (EPROM[14] != 0x57 || EPROM[15] != 0x57) return FALSE;
174433d6423SLionel Sambuc 
175433d6423SLionel Sambuc   /* Setup the ethernet address. */
176433d6423SLionel Sambuc   for (ix = 0; ix < SA_ADDR_LEN; ix += 1) {
177*f7df02e7SDavid van Moolenbroek 	dep->de_address.na_addr[ix] = EPROM[ix];
178433d6423SLionel Sambuc   }
179433d6423SLionel Sambuc   dep->de_16bit = wd;
180433d6423SLionel Sambuc   dep->de_linmem = 0;		/* Uses Programmed I/O only */
181433d6423SLionel Sambuc   dep->de_prog_IO = 1;
182433d6423SLionel Sambuc   dep->de_initf = ne_init;
183433d6423SLionel Sambuc   dep->de_stopf = ne_close;
184433d6423SLionel Sambuc   return TRUE;
185433d6423SLionel Sambuc }
186433d6423SLionel Sambuc 
187433d6423SLionel Sambuc #endif				/* ENABLE_NE2000 */
188433d6423SLionel Sambuc 
189433d6423SLionel Sambuc /** ne.c **/
190