Home | History | Annotate | Line # | Download | only in sboot
le_poll.c revision 1.1
      1 /*	$NetBSD: le_poll.c,v 1.1 1995/07/25 23:12:31 chuck Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993 Adam Glass
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Adam Glass.
     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 Adam Glass ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY 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 "sboot.h"
     35 #include "if_lereg.h"
     36 
     37 struct {
     38 	struct	lereg1 *sc_r1;	/* LANCE registers */
     39 	struct	lereg2 *sc_r2;	/* RAM */
     40 	int next_rmd;
     41 	int next_tmd;
     42 } le_softc;
     43 
     44 void le_error(str, ler1)
     45      char *str;
     46      struct lereg1 *ler1;
     47 {
     48     /* ler1->ler1_rap = LE_CSRO done in caller */
     49     if (ler1->ler1_rdp & LE_C0_BABL) {
     50 	printf("le0: been babbling, found by '%s'\n", str);
     51 	callrom();
     52     }
     53     if (ler1->ler1_rdp & LE_C0_CERR) {
     54 	ler1->ler1_rdp = LE_C0_CERR;
     55     }
     56     if (ler1->ler1_rdp & LE_C0_MISS) {
     57 	ler1->ler1_rdp = LE_C0_MISS;
     58     }
     59     if (ler1->ler1_rdp & LE_C0_MERR) {
     60 	printf("le0: memory error in '%s'\n", str);
     61 	callrom();
     62     }
     63 
     64 }
     65 
     66 void le_reset(myea)
     67      u_char *myea;
     68 {
     69     struct lereg1 *ler1 = le_softc.sc_r1;
     70     struct lereg2 *ler2 = le_softc.sc_r2;
     71     unsigned int a;
     72     int timo = 100000, stat, i;
     73 
     74     ler1->ler1_rap = LE_CSR0;
     75     ler1->ler1_rdp = LE_C0_STOP;	/* do nothing until we are finished */
     76 
     77     bzero(ler2, sizeof(*ler2));
     78 
     79     ler2->ler2_mode = LE_MODE_NORMAL;
     80     ler2->ler2_padr[0] = myea[1];
     81     ler2->ler2_padr[1] = myea[0];
     82     ler2->ler2_padr[2] = myea[3];
     83     ler2->ler2_padr[3] = myea[2];
     84     ler2->ler2_padr[4] = myea[5];
     85     ler2->ler2_padr[5] = myea[4];
     86 
     87 
     88     ler2->ler2_ladrf0 = 0;
     89     ler2->ler2_ladrf1 = 0;
     90 
     91     a = (u_int)ler2->ler2_rmd;
     92     ler2->ler2_rlen =  LE_RLEN | (a >> 16);
     93     ler2->ler2_rdra = a & LE_ADDR_LOW_MASK;
     94 
     95     a = (u_int)ler2->ler2_tmd;
     96     ler2->ler2_tlen = LE_TLEN | (a >> 16);
     97     ler2->ler2_tdra = a & LE_ADDR_LOW_MASK;
     98 
     99     ler1->ler1_rap = LE_CSR1;
    100     a = (u_int)ler2;
    101     ler1->ler1_rdp = a & LE_ADDR_LOW_MASK;
    102     ler1->ler1_rap = LE_CSR2;
    103     ler1->ler1_rdp = a >> 16;
    104 
    105     for (i = 0; i < LERBUF; i++) {
    106 	a = (u_int)&ler2->ler2_rbuf[i];
    107 	ler2->ler2_rmd[i].rmd0 = a & LE_ADDR_LOW_MASK;
    108 	ler2->ler2_rmd[i].rmd1_bits = LE_R1_OWN;
    109 	ler2->ler2_rmd[i].rmd1_hadr = a >> 16;
    110 	ler2->ler2_rmd[i].rmd2 = -LEMTU;
    111 	ler2->ler2_rmd[i].rmd3 = 0;
    112     }
    113     for (i = 0; i < LETBUF; i++) {
    114 	a = (u_int)&ler2->ler2_tbuf[i];
    115 	ler2->ler2_tmd[i].tmd0 = a & LE_ADDR_LOW_MASK;
    116 	ler2->ler2_tmd[i].tmd1_bits = 0;
    117 	ler2->ler2_tmd[i].tmd1_hadr = a >> 16;
    118 	ler2->ler2_tmd[i].tmd2 = 0;
    119 	ler2->ler2_tmd[i].tmd3 = 0;
    120     }
    121 
    122     ler1->ler1_rap = LE_CSR3;
    123     ler1->ler1_rdp = LE_C3_BSWP;
    124 
    125     ler1->ler1_rap = LE_CSR0;
    126     ler1->ler1_rdp = LE_C0_INIT;
    127     do {
    128 	if (--timo == 0) {
    129 	    printf("le0: init timeout, stat = 0x%x\n", stat);
    130 	    break;
    131 	}
    132 	stat = ler1->ler1_rdp;
    133     } while ((stat & LE_C0_IDON) == 0);
    134 
    135     ler1->ler1_rdp = LE_C0_IDON;
    136     le_softc.next_rmd = 0;
    137     le_softc.next_tmd = 0;
    138     ler1->ler1_rap = LE_CSR0;
    139     ler1->ler1_rdp = LE_C0_STRT;
    140 }
    141 
    142 int le_poll(pkt, len)
    143      void *pkt;
    144      int len;
    145 {
    146     struct lereg1 *ler1 = le_softc.sc_r1;
    147     struct lereg2 *ler2 = le_softc.sc_r2;
    148     unsigned int a;
    149     int length;
    150     struct lermd *rmd;
    151 
    152     ler1->ler1_rap = LE_CSR0;
    153     if ((ler1->ler1_rdp & LE_C0_RINT) != 0)
    154       ler1->ler1_rdp = LE_C0_RINT;
    155     rmd = &ler2->ler2_rmd[le_softc.next_rmd];
    156     if (rmd->rmd1_bits & LE_R1_OWN) {
    157 	return(0);
    158     }
    159     if (ler1->ler1_rdp & LE_C0_ERR)
    160 	le_error("le_poll", ler1);
    161     if (rmd->rmd1_bits & LE_R1_ERR) {
    162 	printf("le0_poll: rmd status 0x%x\n", rmd->rmd1_bits);
    163 	length = 0;
    164 	goto cleanup;
    165     }
    166     if ((rmd->rmd1_bits & (LE_R1_STP|LE_R1_ENP)) != (LE_R1_STP|LE_R1_ENP)) {
    167 	printf("le_poll: chained packet\n");
    168 	callrom();
    169     }
    170 
    171     length = rmd->rmd3;
    172     if (length >= LEMTU) {
    173 	length = 0;
    174 	printf("csr0 when bad things happen: %x\n", ler1->ler1_rdp);
    175 	callrom();
    176 	goto cleanup;
    177     }
    178     if (!length) goto cleanup;
    179     length -= 4;
    180     if (length > 0)
    181 	bcopy((char *)&ler2->ler2_rbuf[le_softc.next_rmd], pkt, length);
    182 
    183  cleanup:
    184     a = (u_int)&ler2->ler2_rbuf[le_softc.next_rmd];
    185     rmd->rmd0 = a & LE_ADDR_LOW_MASK;
    186     rmd->rmd1_hadr = a >> 16;
    187     rmd->rmd2 = -LEMTU;
    188     le_softc.next_rmd =
    189 	(le_softc.next_rmd == (LERBUF - 1)) ? 0 : (le_softc.next_rmd + 1);
    190     rmd->rmd1_bits = LE_R1_OWN;
    191     return length;
    192 }
    193 
    194 int le_put(pkt, len)
    195      u_char *pkt;
    196      size_t len;
    197 {
    198     struct lereg1 *ler1 = le_softc.sc_r1;
    199     struct lereg2 *ler2 = le_softc.sc_r2;
    200     struct letmd *tmd;
    201     int timo = 100000, stat, i;
    202     unsigned int a;
    203 
    204     ler1->ler1_rap = LE_CSR0;
    205     if (ler1->ler1_rdp & LE_C0_ERR)
    206 	le_error("le_put(way before xmit)", ler1);
    207     tmd = &ler2->ler2_tmd[le_softc.next_tmd];
    208     while(tmd->tmd1_bits & LE_T1_OWN) {
    209 	printf("le0: output buffer busy\n");
    210     }
    211     bcopy(pkt, (char *)ler2->ler2_tbuf[le_softc.next_tmd], len);
    212     if (len < 64)
    213 	tmd->tmd2 = -64;
    214     else
    215 	tmd->tmd2 = -len;
    216     tmd->tmd3 = 0;
    217     if (ler1->ler1_rdp & LE_C0_ERR)
    218 	le_error("le_put(before xmit)", ler1);
    219     tmd->tmd1_bits = LE_T1_STP | LE_T1_ENP | LE_T1_OWN;
    220     a = (u_int)&ler2->ler2_tbuf[le_softc.next_tmd];
    221     tmd->tmd0 = a & LE_ADDR_LOW_MASK;
    222     tmd->tmd1_hadr = a >> 16;
    223     ler1->ler1_rdp = LE_C0_TDMD;
    224     if (ler1->ler1_rdp & LE_C0_ERR)
    225 	le_error("le_put(after xmit)", ler1);
    226     do {
    227 	if (--timo == 0) {
    228 	    printf("le0: transmit timeout, stat = 0x%x\n",
    229 		   stat);
    230 	    if (ler1->ler1_rdp & LE_C0_ERR)
    231 		le_error("le_put(timeout)", ler1);
    232 	    break;
    233 	}
    234 	stat = ler1->ler1_rdp;
    235     } while ((stat & LE_C0_TINT) == 0);
    236     ler1->ler1_rdp = LE_C0_TINT;
    237     if (ler1->ler1_rdp & LE_C0_ERR) {
    238 	if ((ler1->ler1_rdp & (LE_C0_BABL|LE_C0_CERR|LE_C0_MISS|LE_C0_MERR)) !=
    239 		LE_C0_CERR)
    240 	  printf("le_put: xmit error, buf %d\n", le_softc.next_tmd);
    241 	le_error("le_put(xmit error)", ler1);
    242     }
    243     le_softc.next_tmd = 0;
    244 /*	(le_softc.next_tmd == (LETBUF - 1)) ? 0 : le_softc.next_tmd + 1;*/
    245     if (tmd->tmd1_bits & LE_T1_ERR) {
    246 	printf("le0: transmit error, error = 0x%x\n",
    247 	       tmd->tmd3);
    248 	return -1;
    249     }
    250     return len;
    251 }
    252 
    253 int le_get(pkt, len, timeout)
    254      u_char *pkt;
    255      size_t len;
    256      u_long timeout;
    257 {
    258     int cc;
    259     int now, then;
    260     int stopat = time() + timeout;
    261     then = 0;
    262 
    263     cc = 0;
    264     while ((now = time()) < stopat && !cc) {
    265 	cc = le_poll(pkt, len);
    266         if (then != now) {
    267 #ifdef LE_DEBUG
    268           printf("%d  \r", stopat - now);
    269 #endif
    270           then = now;
    271         }
    272         if (cc && (pkt[0] != myea[0] || pkt[1] != myea[1] ||
    273                    pkt[2] != myea[2] || pkt[3] != myea[3] ||
    274                    pkt[4] != myea[4] || pkt[5] != myea[5])) {
    275           cc = 0; /* ignore broadcast / multicast */
    276 #ifdef LE_DEBUG
    277           printf("reject (%d sec left)\n", stopat - now);
    278 #endif
    279         }
    280     }
    281 #ifdef LE_DEBUG
    282     printf("\n");
    283 #endif
    284     return cc;
    285 }
    286 
    287 void le_init()
    288 {
    289     caddr_t addr;
    290     int *ea = (int *) LANCE_ADDR;
    291     u_long *eram = (u_long *) ERAM_ADDR;
    292     u_long e = *ea;
    293     if (( e & 0x2fffff00 ) == 0x2fffff00) {
    294       printf("ERROR: ethernet address not set!  Use LSAD.\n");
    295       callrom();
    296     }
    297     myea[0] = 0x08;
    298     myea[1] = 0x00;
    299     myea[2] = 0x3e;
    300     e = e >> 8;
    301     myea[5] = e & 0xff;
    302     e = e >> 8;
    303     myea[4] = e & 0xff;
    304     e = e >> 8;
    305     myea[3] = e;
    306     printf("le0: ethernet address: %x:%x:%x:%x:%x:%x\n",
    307       myea[0], myea[1], myea[2], myea[3], myea[4], myea[5]);
    308     bzero(&le_softc, sizeof(le_softc));
    309     le_softc.sc_r1 = (struct lereg1 *) LANCE_REG_ADDR;
    310     le_softc.sc_r2 = (struct lereg2 *)(*eram - (1024*1024));
    311     le_reset(myea);
    312 }
    313 
    314 void le_end()
    315 {
    316     struct lereg1 *ler1 = le_softc.sc_r1;
    317 
    318     ler1->ler1_rap = LE_CSR0;
    319     ler1->ler1_rdp = LE_C0_STOP;
    320 }
    321