version /usr/lib/crontab too
[minix3.git] / drivers / dp8390 / ne2000.c
blob4f8d698ffc595486c4985d5833f103f3dd6ee9bb
1 /*
2 ne2000.c
4 Driver for the ne2000 ethernet cards. This file contains only the ne2000
5 specific code, the rest is in dp8390.c
7 Created: March 15, 1994 by Philip Homburg <philip@f-mnx.phicoh.com>
8 */
10 #include "../drivers.h"
12 #include <net/gen/ether.h>
13 #include <net/gen/eth_io.h>
14 #if __minix_vmd
15 #include "config.h"
16 #endif
18 #include "local.h"
19 #include "dp8390.h"
20 #include "ne2000.h"
22 #if ENABLE_NE2000
24 #define N 100
26 #define MILLIS_TO_TICKS(m) (((m)*HZ/1000)+1)
28 _PROTOTYPE( typedef int (*testf_t), (dpeth_t *dep, int pos, u8_t *pat) );
30 u8_t pat0[]= { 0x00, 0x00, 0x00, 0x00 };
31 u8_t pat1[]= { 0xFF, 0xFF, 0xFF, 0xFF };
32 u8_t pat2[]= { 0xA5, 0x5A, 0x69, 0x96 };
33 u8_t pat3[]= { 0x96, 0x69, 0x5A, 0xA5 };
35 _PROTOTYPE( static int test_8, (dpeth_t *dep, int pos, u8_t *pat) );
36 _PROTOTYPE( static int test_16, (dpeth_t *dep, int pos, u8_t *pat) );
37 _PROTOTYPE( static void ne_stop, (dpeth_t *dep) );
38 _PROTOTYPE( static void milli_delay, (unsigned long millis) );
40 /*===========================================================================*
41 * ne_probe *
42 *===========================================================================*/
43 int ne_probe(dep)
44 dpeth_t *dep;
46 int byte;
47 int i;
48 int loc1, loc2;
49 testf_t f;
51 dep->de_dp8390_port= dep->de_base_port + NE_DP8390;
53 /* We probe for an ne1000 or an ne2000 by testing whether the
54 * on board is reachable through the dp8390. Note that the
55 * ne1000 is an 8bit card and has a memory region distict from
56 * the 16bit ne2000
59 for (dep->de_16bit= 0; dep->de_16bit < 2; dep->de_16bit++)
61 /* Reset the ethernet card */
62 byte= inb_ne(dep, NE_RESET);
63 milli_delay(2);
64 outb_ne(dep, NE_RESET, byte);
65 milli_delay(2);
67 /* Reset the dp8390 */
68 outb_reg0(dep, DP_CR, CR_STP | CR_DM_ABORT);
69 for (i= 0; i < 0x1000 && ((inb_reg0(dep, DP_ISR) & ISR_RST) == 0); i++)
70 ; /* Do nothing */
72 /* Check if the dp8390 is really there */
73 if ((inb_reg0(dep, DP_CR) & (CR_STP|CR_DM_ABORT)) !=
74 (CR_STP|CR_DM_ABORT))
76 return 0;
79 /* Disable the receiver and init TCR and DCR. */
80 outb_reg0(dep, DP_RCR, RCR_MON);
81 outb_reg0(dep, DP_TCR, TCR_NORMAL);
82 if (dep->de_16bit)
84 outb_reg0(dep, DP_DCR, DCR_WORDWIDE | DCR_8BYTES |
85 DCR_BMS);
87 else
89 outb_reg0(dep, DP_DCR, DCR_BYTEWIDE | DCR_8BYTES |
90 DCR_BMS);
93 if (dep->de_16bit)
95 loc1= NE2000_START;
96 loc2= NE2000_START + NE2000_SIZE - 4;
97 f= test_16;
99 else
101 loc1= NE1000_START;
102 loc2= NE1000_START + NE1000_SIZE - 4;
103 f= test_8;
105 if (f(dep, loc1, pat0) && f(dep, loc1, pat1) &&
106 f(dep, loc1, pat2) && f(dep, loc1, pat3) &&
107 f(dep, loc2, pat0) && f(dep, loc2, pat1) &&
108 f(dep, loc2, pat2) && f(dep, loc2, pat3))
110 /* We don't need a memory segment */
111 dep->de_linmem= 0;
112 if (!dep->de_pci)
113 dep->de_initf= ne_init;
114 dep->de_stopf= ne_stop;
115 dep->de_prog_IO= 1;
116 return 1;
119 return 0;
122 /*===========================================================================*
123 * ne_init *
124 *===========================================================================*/
125 void ne_init(dep)
126 dpeth_t *dep;
128 int i;
129 int word, sendq_nr;
131 /* Setup a transfer to get the ethernet address. */
132 if (dep->de_16bit)
133 outb_reg0(dep, DP_RBCR0, 6*2);
134 else
135 outb_reg0(dep, DP_RBCR0, 6);
136 outb_reg0(dep, DP_RBCR1, 0);
137 outb_reg0(dep, DP_RSAR0, 0);
138 outb_reg0(dep, DP_RSAR1, 0);
139 outb_reg0(dep, DP_CR, CR_DM_RR | CR_PS_P0 | CR_STA);
141 for (i= 0; i<6; i++)
143 if (dep->de_16bit)
145 word= inw_ne(dep, NE_DATA);
146 dep->de_address.ea_addr[i]= word;
148 else
150 dep->de_address.ea_addr[i] = inb_ne(dep, NE_DATA);
153 dep->de_data_port= dep->de_base_port + NE_DATA;
154 if (dep->de_16bit)
156 dep->de_ramsize= NE2000_SIZE;
157 dep->de_offset_page= NE2000_START / DP_PAGESIZE;
159 else
161 dep->de_ramsize= NE1000_SIZE;
162 dep->de_offset_page= NE1000_START / DP_PAGESIZE;
165 /* Allocate one send buffer (1.5KB) per 8KB of on board memory. */
166 sendq_nr= dep->de_ramsize / 0x2000;
167 if (sendq_nr < 1)
168 sendq_nr= 1;
169 else if (sendq_nr > SENDQ_NR)
170 sendq_nr= SENDQ_NR;
171 dep->de_sendq_nr= sendq_nr;
172 for (i= 0; i<sendq_nr; i++)
174 dep->de_sendq[i].sq_sendpage= dep->de_offset_page +
175 i*SENDQ_PAGES;
178 dep->de_startpage= dep->de_offset_page + i*SENDQ_PAGES;
179 dep->de_stoppage= dep->de_offset_page + dep->de_ramsize / DP_PAGESIZE;
181 /* Can't override the default IRQ. */
182 dep->de_irq &= ~DEI_DEFAULT;
184 if (!debug)
186 printf("%s: NE%d000 at %X:%d\n",
187 dep->de_name, dep->de_16bit ? 2 : 1,
188 dep->de_base_port, dep->de_irq);
190 else
192 printf("%s: Novell NE%d000 ethernet card at I/O address "
193 "0x%X, memory size 0x%X, irq %d\n",
194 dep->de_name, dep->de_16bit ? 2 : 1,
195 dep->de_base_port, dep->de_ramsize, dep->de_irq);
199 /*===========================================================================*
200 * test_8 *
201 *===========================================================================*/
202 static int test_8(dep, pos, pat)
203 dpeth_t *dep;
204 int pos;
205 u8_t *pat;
207 u8_t buf[4];
208 int i;
209 int r;
211 outb_reg0(dep, DP_ISR, 0xFF);
213 /* Setup a transfer to put the pattern. */
214 outb_reg0(dep, DP_RBCR0, 4);
215 outb_reg0(dep, DP_RBCR1, 0);
216 outb_reg0(dep, DP_RSAR0, pos & 0xFF);
217 outb_reg0(dep, DP_RSAR1, pos >> 8);
218 outb_reg0(dep, DP_CR, CR_DM_RW | CR_PS_P0 | CR_STA);
220 for (i= 0; i<4; i++)
221 outb_ne(dep, NE_DATA, pat[i]);
223 for (i= 0; i<N; i++)
225 if (inb_reg0(dep, DP_ISR) & ISR_RDC)
226 break;
228 if (i == N)
230 if (debug)
232 printf("%s: NE1000 remote DMA test failed\n",
233 dep->de_name);
235 return 0;
238 outb_reg0(dep, DP_RBCR0, 4);
239 outb_reg0(dep, DP_RBCR1, 0);
240 outb_reg0(dep, DP_RSAR0, pos & 0xFF);
241 outb_reg0(dep, DP_RSAR1, pos >> 8);
242 outb_reg0(dep, DP_CR, CR_DM_RR | CR_PS_P0 | CR_STA);
244 for (i= 0; i<4; i++)
245 buf[i]= inb_ne(dep, NE_DATA);
247 r= (memcmp(buf, pat, 4) == 0);
248 return r;
251 /*===========================================================================*
252 * test_16 *
253 *===========================================================================*/
254 static int test_16(dep, pos, pat)
255 dpeth_t *dep;
256 int pos;
257 u8_t *pat;
259 u8_t buf[4];
260 int i;
261 int r;
263 outb_reg0(dep, DP_ISR, 0xFF);
265 /* Setup a transfer to put the pattern. */
266 outb_reg0(dep, DP_RBCR0, 4);
267 outb_reg0(dep, DP_RBCR1, 0);
268 outb_reg0(dep, DP_RSAR0, pos & 0xFF);
269 outb_reg0(dep, DP_RSAR1, pos >> 8);
270 outb_reg0(dep, DP_CR, CR_DM_RW | CR_PS_P0 | CR_STA);
272 for (i= 0; i<4; i += 2)
274 outw_ne(dep, NE_DATA, *(u16_t *)(pat+i));
277 for (i= 0; i<N; i++)
279 if (inb_reg0(dep, DP_ISR) & ISR_RDC)
280 break;
282 if (i == N)
284 if (debug)
286 printf("%s: NE2000 remote DMA test failed\n",
287 dep->de_name);
289 return 0;
292 outb_reg0(dep, DP_RBCR0, 4);
293 outb_reg0(dep, DP_RBCR1, 0);
294 outb_reg0(dep, DP_RSAR0, pos & 0xFF);
295 outb_reg0(dep, DP_RSAR1, pos >> 8);
296 outb_reg0(dep, DP_CR, CR_DM_RR | CR_PS_P0 | CR_STA);
298 for (i= 0; i<4; i += 2)
300 *(u16_t *)(buf+i)= inw_ne(dep, NE_DATA);
303 r= (memcmp(buf, pat, 4) == 0);
304 return r;
307 /*===========================================================================*
308 * ne_stop *
309 *===========================================================================*/
310 static void ne_stop(dep)
311 dpeth_t *dep;
313 int byte;
315 /* Reset the ethernet card */
316 byte= inb_ne(dep, NE_RESET);
317 milli_delay(2);
318 outb_ne(dep, NE_RESET, byte);
321 static void milli_delay(unsigned long millis)
323 tickdelay(MILLIS_TO_TICKS(millis));
326 #endif /* ENABLE_NE2000 */
329 * $PchId: ne2000.c,v 1.10 2004/08/03 12:03:00 philip Exp $