if_ie.c revision 1.5 1 /* $NetBSD: if_ie.c,v 1.5 2000/07/24 18:39:51 jdolecek Exp $ */
2
3 /*
4 * Copyright (c) 1995 Theo de Raadt
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed under OpenBSD by
17 * Theo de Raadt for Willowglen Singapore.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37
38 #include <netinet/in.h>
39 #include <netinet/in_systm.h>
40
41 #include <net/if.h>
42 #include <net/if_ether.h>
43
44 #include <lib/libkern/libkern.h>
45 #include <lib/libsa/stand.h>
46 #include <lib/libsa/net.h>
47
48 #define NTXBUF 1
49 #define NRXBUF 16
50 #define IE_RBUF_SIZE ETHER_MAX_LEN
51
52 #include <machine/prom.h>
53
54 #include "libsa.h"
55 #include "netif.h"
56 #include "config.h"
57 #include "dev_net.h"
58
59 #include "i82586.h"
60 #include "if_iereg.h"
61
62 int ie_debug = 0;
63
64 void ie_stop __P((struct netif *));
65 void ie_end __P((struct netif *));
66 void ie_error __P((struct netif *, char *, volatile struct iereg *));
67 int ie_get __P((struct iodesc *, void *, size_t, time_t));
68 void ie_init __P((struct iodesc *, void *));
69 int ie_match __P((struct netif *, void *));
70 int ie_poll __P((struct iodesc *, void *, int));
71 int ie_probe __P((struct netif *, void *));
72 int ie_put __P((struct iodesc *, void *, size_t));
73 void ie_reset __P((struct netif *, u_char *));
74 void ieack __P((volatile struct iereg *, struct iemem *));
75
76 struct netif_stats ie_stats;
77
78 struct netif_dif ie0_dif = {
79 0, /* unit */
80 1, /* nsel */
81 &ie_stats,
82 0,
83 0,
84 };
85
86 struct netif_driver ie_driver = {
87 "ie", /* netif_bname */
88 ie_match, /* match */
89 ie_probe, /* probe */
90 ie_init, /* init */
91 ie_get, /* get */
92 ie_put, /* put */
93 ie_end, /* end */
94 &ie0_dif, /* netif_ifs */
95 1, /* netif_nifs */
96 };
97
98 struct ie_configuration {
99 u_int phys_addr;
100 int used;
101 } ie_config[] = {
102 { INTEL_REG_ADDR, 0 }
103 };
104
105 int nie_config = sizeof(ie_config) / (sizeof(ie_config[0]));
106
107 struct {
108 struct iereg *sc_reg; /* IE registers */
109 struct iemem *sc_mem; /* RAM */
110 } ie_softc;
111
112 int
113 ie_match(nif, machdep_hint)
114 struct netif *nif;
115 void *machdep_hint;
116 {
117 char *name;
118 int i, val = 0;
119
120 if (bugargs.cputyp == CPU_147)
121 return (0);
122 name = machdep_hint;
123 if (name && !bcmp(ie_driver.netif_bname, name, 2))
124 val += 10;
125 for (i = 0; i < nie_config; i++) {
126 if (ie_config[i].used)
127 continue;
128 if (ie_debug)
129 printf("ie%d: ie_match --> %d\n", i, val + 1);
130 ie_config[i].used++;
131 return (val + 1);
132 }
133 if (ie_debug)
134 printf("ie%d: ie_match --> 0\n", i);
135 return (0);
136 }
137
138 int
139 ie_probe(nif, machdep_hint)
140 struct netif *nif;
141 void *machdep_hint;
142 {
143
144 /* the set unit is the current unit */
145 if (ie_debug)
146 printf("ie%d: ie_probe called\n", nif->nif_unit);
147
148 if (bugargs.cputyp != CPU_147)
149 return (0);
150 return (1);
151 }
152
153 void
154 ie_error(nif, str, ier)
155 struct netif *nif;
156 char *str;
157 volatile struct iereg *ier;
158 {
159 panic("ie%d: unknown error\n", nif->nif_unit);
160 }
161
162 void
163 ieack(ier, iem)
164 volatile struct iereg *ier;
165 struct iemem *iem;
166 {
167 /* ack the `interrupt' */
168 iem->im_scb.ie_command = iem->im_scb.ie_status & IE_ST_WHENCE;
169 ier->ie_attention = 1; /* chan attention! */
170 while (iem->im_scb.ie_command)
171 ;
172 }
173
174 void
175 ie_reset(nif, myea)
176 struct netif *nif;
177 u_char *myea;
178 {
179 volatile struct iereg *ier = ie_softc.sc_reg;
180 struct iemem *iem = ie_softc.sc_mem;
181 int timo = 10000, i;
182 volatile int t;
183 u_int a;
184
185 if (ie_debug)
186 printf("ie%d: ie_reset called\n", nif->nif_unit);
187
188 /*printf("ier %x iem %x\n", ier, iem);*/
189
190 *(u_char *)0xfff4202a = 0x40;
191
192 bzero(iem, sizeof(*iem));
193 iem->im_scp.scp_sysbus = 0;
194 iem->im_scp.scp_iscp_low = (int) &iem->im_iscp & 0xffff;
195 iem->im_scp.scp_iscp_high = (int) &iem->im_iscp >> 16;
196
197 iem->im_iscp.iscp_scboffset = (int) &iem->im_scb - (int) iem;
198 iem->im_iscp.iscp_busy = 1;
199 iem->im_iscp.iscp_base_low = (int) iem & 0xffff;
200 iem->im_iscp.iscp_base_high = (int) iem >> 16;
201
202 /*
203 * completely and utterly unlike what i expected, the
204 * "write" order is:
205 * 1st: d15-d0 -> high address
206 * 2nd: d31-d16 -> low address
207 */
208
209 /* reset chip */
210 a = IE_PORT_RESET;
211 ier->ie_porthigh = a & 0xffff;
212 t = 0;
213 t = 1;
214 ier->ie_portlow = a >> 16;
215 for (t = timo; t--;)
216 ;
217
218 /* set new SCP pointer */
219 a = (int) &iem->im_scp | IE_PORT_NEWSCP;
220 ier->ie_porthigh = a & 0xffff;
221 t = 0;
222 t = 1;
223 ier->ie_portlow = a >> 16;
224 for (t = timo; t--;)
225 ;
226
227 ier->ie_attention = 1; /* chan attention! */
228 for (t = timo * 10; t--;)
229 ;
230
231 /* send CONFIGURE command */
232 iem->im_scb.ie_command = IE_CU_START;
233 iem->im_scb.ie_command_list = (int) &iem->im_cc - (int) iem;
234 iem->im_cc.com.ie_cmd_status = 0;
235 iem->im_cc.com.ie_cmd_cmd = IE_CMD_CONFIG | IE_CMD_LAST;
236 iem->im_cc.com.ie_cmd_link = 0xffff;
237 iem->im_cc.ie_config_count = 0x0c;
238 iem->im_cc.ie_fifo = 8;
239 iem->im_cc.ie_save_bad = 0x40;
240 iem->im_cc.ie_addr_len = 0x2e;
241 iem->im_cc.ie_priority = 0;
242 iem->im_cc.ie_ifs = 0x60;
243 iem->im_cc.ie_slot_low = 0;
244 iem->im_cc.ie_slot_high = 0xf2;
245 iem->im_cc.ie_promisc = 0;
246 iem->im_cc.ie_crs_cdt = 0;
247 iem->im_cc.ie_min_len = 64;
248 iem->im_cc.ie_junk = 0xff;
249
250 ier->ie_attention = 1; /* chan attention! */
251 for (t = timo * 10; t--;)
252 ;
253
254 ieack(ier, iem);
255
256 /*printf("ic %x\n", &iem->im_ic);*/
257 /* send IASETUP command */
258 iem->im_scb.ie_command = IE_CU_START;
259 iem->im_scb.ie_command_list = (int) &iem->im_ic - (int) iem;
260 iem->im_ic.com.ie_cmd_status = 0;
261 iem->im_ic.com.ie_cmd_cmd = IE_CMD_IASETUP | IE_CMD_LAST;
262 iem->im_ic.com.ie_cmd_link = 0xffff;
263 bcopy(myea, (void *)&iem->im_ic.ie_address, sizeof iem->im_ic.ie_address);
264
265 ier->ie_attention = 1; /* chan attention! */
266 for (t = timo * 10; t--;)
267 ;
268
269 ieack(ier, iem);
270
271 /* setup buffers */
272
273 for (i = 0; i < NRXBUF; i++) {
274 iem->im_rfd[i].ie_fd_next = (int) &iem->im_rfd[(i+1) % NRXBUF] -
275 (int) iem;
276 iem->im_rbd[i].ie_rbd_next = (int) &iem->im_rbd[(i+1) % NRXBUF] -
277 (int) iem;
278 a = (int) &iem->im_rxbuf[i * IE_RBUF_SIZE];
279 iem->im_rbd[i].ie_rbd_buffer_low = a & 0xffff;
280 iem->im_rbd[i].ie_rbd_buffer_high = a >> 16;
281 iem->im_rbd[i].ie_rbd_length = IE_RBUF_SIZE;
282 }
283 iem->im_rfd[NRXBUF-1].ie_fd_last |= IE_FD_LAST;
284 iem->im_rbd[NRXBUF-1].ie_rbd_length |= IE_RBD_LAST;
285 iem->im_rfd[0].ie_fd_buf_desc = (int) &iem->im_rbd[0] - (int) iem;
286
287 /*printf("rfd[0] %x rbd[0] %x buf[0] %x\n", &iem->im_rfd, &iem->im_rbd,
288 &iem->im_rxbuf);*/
289
290 /* send receiver start command */
291 iem->im_scb.ie_command = IE_RU_START;
292 iem->im_scb.ie_command_list = 0;
293 iem->im_scb.ie_recv_list = (int) &iem->im_rfd[0] - (int) iem;
294 ier->ie_attention = 1; /* chan attention! */
295 while (iem->im_scb.ie_command)
296 ;
297
298 ieack(ier, iem);
299 }
300
301 int
302 ie_poll(desc, pkt, len)
303 struct iodesc *desc;
304 void *pkt;
305 int len;
306 {
307 volatile struct iereg *ier = ie_softc.sc_reg;
308 struct iemem *iem = ie_softc.sc_mem;
309 static int slot;
310 int length = 0;
311 u_short status;
312
313 asm(".word 0xf518\n");
314 status = iem->im_rfd[slot].ie_fd_status;
315 if (status & IE_FD_BUSY)
316 return (0);
317
318 /* printf("slot %d: %x\n", slot, status); */
319 if ((status & (IE_FD_COMPLETE | IE_FD_OK)) == (IE_FD_COMPLETE | IE_FD_OK)) {
320 if (status & IE_FD_OK) {
321 length = iem->im_rbd[slot].ie_rbd_actual & 0x3fff;
322 if (length > len)
323 length = len;
324 bcopy((void *)&iem->im_rxbuf[slot * IE_RBUF_SIZE],
325 pkt, length);
326
327 iem->im_rfd[slot].ie_fd_status = 0;
328 iem->im_rfd[slot].ie_fd_last |= IE_FD_LAST;
329 iem->im_rfd[(slot+NRXBUF-1)%NRXBUF].ie_fd_last &=
330 ~IE_FD_LAST;
331 iem->im_rbd[slot].ie_rbd_actual = 0;
332 iem->im_rbd[slot].ie_rbd_length |= IE_RBD_LAST;
333 iem->im_rbd[(slot+NRXBUF-1)%NRXBUF].ie_rbd_length &=
334 ~IE_RBD_LAST;
335 /*printf("S%d\n", slot);*/
336
337 } else {
338 printf("shit\n");
339 }
340 slot++;
341 /* should move descriptor onto end of queue... */
342 }
343 if ((iem->im_scb.ie_status & IE_RU_READY) == 0) {
344 printf("RR\n");
345
346 for (slot = 0; slot < NRXBUF; slot++) {
347 iem->im_rbd[slot].ie_rbd_length &= ~IE_RBD_LAST;
348 iem->im_rfd[slot].ie_fd_last &= ~IE_FD_LAST;
349 }
350 iem->im_rbd[NRXBUF-1].ie_rbd_length |= IE_RBD_LAST;
351 iem->im_rfd[NRXBUF-1].ie_fd_last |= IE_FD_LAST;
352
353 iem->im_rfd[0].ie_fd_buf_desc = (int)&iem->im_rbd[0] - (int)iem;
354
355 iem->im_scb.ie_command = IE_RU_START;
356 iem->im_scb.ie_command_list = 0;
357 iem->im_scb.ie_recv_list = (int)&iem->im_rfd[0] - (int)iem;
358 ier->ie_attention = 1; /* chan attention! */
359 while (iem->im_scb.ie_command)
360 ;
361 slot = 0;
362 }
363 slot = slot % NRXBUF;
364 return (length);
365 }
366
367 int
368 ie_put(desc, pkt, len)
369 struct iodesc *desc;
370 void *pkt;
371 size_t len;
372 {
373 volatile struct iereg *ier = ie_softc.sc_reg;
374 struct iemem *iem = ie_softc.sc_mem;
375 u_char *p = pkt;
376 u_int a;
377 int xx = 0;
378
379 /* send transmit command */
380
381 while (iem->im_scb.ie_command)
382 ;
383
384 /* copy data */
385 bcopy(p, (void *)&iem->im_txbuf[xx], len);
386
387 len = MAX(len, ETHER_MIN_LEN);
388
389 /* build transmit descriptor */
390 iem->im_xd[xx].ie_xmit_flags = len | IE_XMIT_LAST;
391 iem->im_xd[xx].ie_xmit_next = 0xffff;
392 a = (int) &iem->im_txbuf[xx];
393 iem->im_xd[xx].ie_xmit_buf_low = a & 0xffff;
394 iem->im_xd[xx].ie_xmit_buf_high = a >> 16;
395
396 /* transmit command */
397 iem->im_xc[xx].com.ie_cmd_status = 0;
398 iem->im_xc[xx].com.ie_cmd_cmd = IE_CMD_XMIT | IE_CMD_LAST;
399 iem->im_xc[xx].com.ie_cmd_link = 0xffff;
400 iem->im_xc[xx].ie_xmit_desc = (int) &iem->im_xd[xx] - (int) iem;
401 iem->im_xc[xx].ie_xmit_length = len;
402 bcopy(p, (void *)&iem->im_xc[xx].ie_xmit_addr,
403 sizeof iem->im_xc[xx].ie_xmit_addr);
404
405 iem->im_scb.ie_command = IE_CU_START;
406 iem->im_scb.ie_command_list = (int) &iem->im_xc[xx] - (int) iem;
407
408 ier->ie_attention = 1; /* chan attention! */
409
410 if (ie_debug) {
411 printf("ie%d: send %d to %x:%x:%x:%x:%x:%x\n",
412 desc->io_netif->nif_unit, len,
413 p[0], p[1], p[2], p[3], p[4], p[5]);
414 }
415 return (len);
416 }
417
418 int
419 ie_get(desc, pkt, len, timeout)
420 struct iodesc *desc;
421 void *pkt;
422 size_t len;
423 time_t timeout;
424 {
425 time_t t;
426 int cc;
427
428 t = getsecs();
429 cc = 0;
430 while (((getsecs() - t) < timeout) && !cc) {
431 cc = ie_poll(desc, pkt, len);
432 }
433 return (cc);
434 }
435 /*
436 * init ie device. return 0 on failure, 1 if ok.
437 */
438 void
439 ie_init(desc, machdep_hint)
440 struct iodesc *desc;
441 void *machdep_hint;
442 {
443 struct netif *nif = desc->io_netif;
444
445 if (ie_debug)
446 printf("ie%d: ie_init called\n", desc->io_netif->nif_unit);
447 machdep_common_ether(desc->myea);
448 bzero(&ie_softc, sizeof(ie_softc));
449 ie_softc.sc_reg =
450 (struct iereg *) ie_config[desc->io_netif->nif_unit].phys_addr;
451 ie_softc.sc_mem = (struct iemem *) 0x3e0000;
452 ie_reset(desc->io_netif, desc->myea);
453 printf("device: %s%d attached to %s\n", nif->nif_driver->netif_bname,
454 nif->nif_unit, ether_sprintf(desc->myea));
455 }
456
457 void
458 ie_stop(nif)
459 struct netif *nif;
460 {
461 volatile struct iereg *ier = ie_softc.sc_reg;
462 struct iemem *iem = ie_softc.sc_mem;
463 int timo = 10000;
464 volatile int t;
465 u_int a;
466
467 iem->im_iscp.iscp_busy = 1;
468 /* reset chip */
469 a = IE_PORT_RESET;
470 ier->ie_porthigh = a & 0xffff;
471 t = 0;
472 t = 1;
473 ier->ie_portlow = a >> 16;
474 for (t = timo; t--;)
475 ;
476
477 /* reset chip again */
478 a = IE_PORT_RESET;
479 ier->ie_porthigh = a & 0xffff;
480 t = 0;
481 t = 1;
482 ier->ie_portlow = a >> 16;
483 for (t = timo; t--;)
484 ;
485
486 /*printf("status %x busy %x\n", iem->im_scb.ie_status,
487 iem->im_iscp.iscp_busy);*/
488 }
489
490 void
491 ie_end(nif)
492 struct netif *nif;
493 {
494 if (ie_debug)
495 printf("ie%d: ie_end called\n", nif->nif_unit);
496
497 ie_stop(nif);
498
499 /* *(u_char *) 0xfff42002 = 0; */
500 }
501