Home | History | Annotate | Line # | Download | only in boot
      1 /*	$NetBSD: dp8390.c,v 1.2 2012/11/01 14:46:26 isaki Exp $	*/
      2 
      3 /*
      4  * This file is derived from sys/arch/i386/stand/lib/netif/dp8390.c
      5  * NetBSD: dp8390.c,v 1.6 2008/12/14 18:46:33 christos Exp
      6  */
      7 
      8 /*
      9  * Polling driver for National Semiconductor DS8390/WD83C690 based
     10  * ethernet adapters.
     11  *
     12  * Copyright (c) 1998 Matthias Drochner.  All rights reserved.
     13  *
     14  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
     15  *
     16  * Copyright (C) 1993, David Greenman.  This software may be used, modified,
     17  * copied, distributed, and sold, in both source and binary form provided that
     18  * the above copyright and these terms are retained.  Under no circumstances is
     19  * the author responsible for the proper functioning of this software, nor does
     20  * the author assume any responsibility for damages incurred with its use.
     21  */
     22 
     23 
     24 #include <sys/types.h>
     25 
     26 #include <lib/libsa/stand.h>
     27 #include <libx68k.h>
     28 
     29 #include <dev/ic/dp8390reg.h>
     30 #include "dp8390.h"
     31 #include "ne.h"
     32 
     33 int dp8390_iobase, dp8390_membase, dp8390_memsize;
     34 #if defined(SUPPORT_WD80X3) && defined(SUPPORT_SMC_ULTRA)
     35 int dp8390_is790;
     36 #endif
     37 uint8_t dp8390_cr_proto;
     38 uint8_t dp8390_dcr_reg;
     39 
     40 #define WE_IOBASE dp8390_iobase
     41 
     42 static u_short rec_page_start;
     43 static u_short rec_page_stop;
     44 static u_short next_packet;
     45 
     46 extern u_char eth_myaddr[6];
     47 
     48 static void dp8390_read(int, char *, u_short);
     49 
     50 #define NIC_GET(reg) inb(WE_IOBASE + (reg) * 2)
     51 #define NIC_PUT(reg, val) outb(WE_IOBASE + (reg) * 2, val)
     52 
     53 static void
     54 dp8390_init(void)
     55 {
     56 	int i;
     57 
     58 	/*
     59 	 * Initialize the NIC in the exact order outlined in the NS manual.
     60 	 * This init procedure is "mandatory"...don't change what or when
     61 	 * things happen.
     62 	 */
     63 
     64 	/* Set interface for page 0, remote DMA complete, stopped. */
     65 	NIC_PUT(ED_P0_CR, dp8390_cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
     66 
     67 	if ((dp8390_dcr_reg & ED_DCR_LS)) {
     68 		NIC_PUT(ED_P0_DCR, dp8390_dcr_reg);
     69 	} else {
     70 		/*
     71 		 * Set FIFO threshold to 8, No auto-init Remote DMA, byte
     72 		 * order=80x86, byte-wide DMA xfers,
     73 		 */
     74 		NIC_PUT(ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
     75 	}
     76 
     77 	/* Clear remote byte count registers. */
     78 	NIC_PUT(ED_P0_RBCR0, 0);
     79 	NIC_PUT(ED_P0_RBCR1, 0);
     80 
     81 	/* Tell RCR to do nothing for now. */
     82 	NIC_PUT(ED_P0_RCR, ED_RCR_MON);
     83 
     84 	/* Place NIC in internal loopback mode. */
     85 	NIC_PUT(ED_P0_TCR, ED_TCR_LB0);
     86 
     87 	/* Set lower bits of byte addressable framing to 0. */
     88 	if (dp8390_is790)
     89 		NIC_PUT(0x09, 0);
     90 
     91 	/* Initialize receive buffer ring. */
     92 	NIC_PUT(ED_P0_BNRY, rec_page_start);
     93 	NIC_PUT(ED_P0_PSTART, rec_page_start);
     94 	NIC_PUT(ED_P0_PSTOP, rec_page_stop);
     95 
     96 	/*
     97 	 * Clear all interrupts.  A '1' in each bit position clears the
     98 	 * corresponding flag.
     99 	 */
    100 	NIC_PUT(ED_P0_ISR, 0xff);
    101 
    102 	/*
    103 	 * Disable all interrupts.
    104 	 */
    105 	NIC_PUT(ED_P0_IMR, 0);
    106 
    107 	/* Program command register for page 1. */
    108 	NIC_PUT(ED_P0_CR, dp8390_cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
    109 
    110 	/* Copy out our station address. */
    111 	for (i = 0; i < 6; i++)
    112 		NIC_PUT(ED_P1_PAR0 + i, eth_myaddr[i]);
    113 
    114 	/*
    115 	 * Set current page pointer to one page after the boundary pointer, as
    116 	 * recommended in the National manual.
    117 	 */
    118 	next_packet = rec_page_start + 1;
    119 	NIC_PUT(ED_P1_CURR, next_packet);
    120 
    121 	/* Program command register for page 0. */
    122 	NIC_PUT(ED_P1_CR, dp8390_cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
    123 
    124 	/* directed and broadcast */
    125 	NIC_PUT(ED_P0_RCR, ED_RCR_AB);
    126 
    127 	/* Take interface out of loopback. */
    128 	NIC_PUT(ED_P0_TCR, 0);
    129 
    130 	/* Fire up the interface. */
    131 	NIC_PUT(ED_P0_CR, dp8390_cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    132 }
    133 
    134 int
    135 dp8390_config(void)
    136 {
    137 
    138 	rec_page_start = TX_PAGE_START + ED_TXBUF_SIZE;
    139 	rec_page_stop = TX_PAGE_START + (dp8390_memsize >> ED_PAGE_SHIFT);
    140 
    141 	dp8390_init();
    142 
    143 	return 0;
    144 }
    145 
    146 void
    147 dp8390_stop(void)
    148 {
    149 	int n = 5000;
    150 
    151 	/* Stop everything on the interface, and select page 0 registers. */
    152 	NIC_PUT(ED_P0_CR, dp8390_cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
    153 
    154 	/*
    155 	 * Wait for interface to enter stopped state, but limit # of checks to
    156 	 * 'n' (about 5ms).  It shouldn't even take 5us on modern DS8390's, but
    157 	 * just in case it's an old one.
    158 	 */
    159 	while (((NIC_GET(ED_P0_ISR) & ED_ISR_RST) == 0) && --n)
    160 		continue;
    161 }
    162 
    163 int
    164 EtherSend(char *pkt, int len)
    165 {
    166 	ne2000_writemem(pkt, dp8390_membase, len);
    167 
    168 	/* Set TX buffer start page. */
    169 	NIC_PUT(ED_P0_TPSR, TX_PAGE_START);
    170 
    171 	/* Set TX length. */
    172 	NIC_PUT(ED_P0_TBCR0, len < 60 ? 60 : len);
    173 	NIC_PUT(ED_P0_TBCR1, len >> 8);
    174 
    175 	/* Set page 0, remote DMA complete, transmit packet, and *start*. */
    176 	NIC_PUT(ED_P0_CR, dp8390_cr_proto | ED_CR_PAGE_0 | ED_CR_TXP | ED_CR_STA);
    177 
    178 	return len;
    179 }
    180 
    181 static void
    182 dp8390_read(int buf, char *dest, u_short len)
    183 {
    184 	u_short tmp_amount;
    185 
    186 	/* Does copy wrap to lower addr in ring buffer? */
    187 	if (buf + len > dp8390_membase + dp8390_memsize) {
    188 		tmp_amount = dp8390_membase + dp8390_memsize - buf;
    189 
    190 		/* Copy amount up to end of NIC memory. */
    191 		ne2000_readmem(buf, dest, tmp_amount);
    192 
    193 		len -= tmp_amount;
    194 		buf = RX_BUFBASE + (rec_page_start << ED_PAGE_SHIFT);
    195 		dest += tmp_amount;
    196 	}
    197 	ne2000_readmem(buf, dest, len);
    198 }
    199 
    200 int
    201 EtherReceive(char *pkt, int maxlen)
    202 {
    203 	struct dp8390_ring packet_hdr;
    204 	int packet_ptr;
    205 	u_short len;
    206 	u_char boundary, current;
    207 #ifdef DP8390_OLDCHIPS
    208 	u_char nlen;
    209 #endif
    210 
    211 	if (!(NIC_GET(ED_P0_RSR) & ED_RSR_PRX))
    212 		return 0; /* XXX error handling */
    213 
    214 	/* Set NIC to page 1 registers to get 'current' pointer. */
    215 	NIC_PUT(ED_P0_CR, dp8390_cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
    216 
    217 	/*
    218 	 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
    219 	 * it points to where new data has been buffered.  The 'CURR' (current)
    220 	 * register points to the logical end of the ring-buffer - i.e. it
    221 	 * points to where additional new data will be added.  We loop here
    222 	 * until the logical beginning equals the logical end (or in other
    223 	 * words, until the ring-buffer is empty).
    224 	 */
    225 	current = NIC_GET(ED_P1_CURR);
    226 
    227 	/* Set NIC to page 0 registers to update boundary register. */
    228 	NIC_PUT(ED_P1_CR, dp8390_cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    229 
    230 	if (next_packet == current)
    231 		return 0;
    232 
    233 	/* Get pointer to this buffer's header structure. */
    234 	packet_ptr = RX_BUFBASE + (next_packet << ED_PAGE_SHIFT);
    235 
    236 	/*
    237 	 * The byte count includes a 4 byte header that was added by
    238 	 * the NIC.
    239 	 */
    240 	ne2000_readmem(packet_ptr, (void *)&packet_hdr, 4);
    241 
    242 	len = le16toh(packet_hdr.count);
    243 
    244 #ifdef DP8390_OLDCHIPS
    245 	/*
    246 	 * Try do deal with old, buggy chips that sometimes duplicate
    247 	 * the low byte of the length into the high byte.  We do this
    248 	 * by simply ignoring the high byte of the length and always
    249 	 * recalculating it.
    250 	 *
    251 	 * NOTE: sc->next_packet is pointing at the current packet.
    252 	 */
    253 	if (packet_hdr.next_packet >= next_packet)
    254 		nlen = (packet_hdr.next_packet - next_packet);
    255 	else
    256 		nlen = ((packet_hdr.next_packet - rec_page_start) +
    257 			(rec_page_stop - next_packet));
    258 	--nlen;
    259 	if ((len & ED_PAGE_MASK) + sizeof(packet_hdr) > ED_PAGE_SIZE)
    260 		--nlen;
    261 	len = (len & ED_PAGE_MASK) | (nlen << ED_PAGE_SHIFT);
    262 #ifdef DIAGNOSTIC
    263 	if (len != packet_hdr.count) {
    264 		printf(IFNAME ": length does not match next packet pointer\n");
    265 		printf(IFNAME ": len %04x nlen %04x start %02x "
    266 		       "first %02x curr %02x next %02x stop %02x\n",
    267 		       packet_hdr.count, len,
    268 		       rec_page_start, next_packet, current,
    269 		       packet_hdr.next_packet, rec_page_stop);
    270 	}
    271 #endif
    272 #endif
    273 
    274 	if (packet_hdr.next_packet < rec_page_start ||
    275 	    packet_hdr.next_packet >= rec_page_stop)
    276 		panic(IFNAME ": RAM corrupt");
    277 
    278 	len -= sizeof(struct dp8390_ring);
    279 	if (len <= maxlen) {
    280 		/* Go get packet. */
    281 		dp8390_read(packet_ptr + sizeof(struct dp8390_ring),
    282 			    pkt, len);
    283 	} else
    284 		len = 0;
    285 
    286 	/* Update next packet pointer. */
    287 	next_packet = packet_hdr.next_packet;
    288 
    289 	/*
    290 	 * Update NIC boundary pointer - being careful to keep it one
    291 	 * buffer behind (as recommended by NS databook).
    292 	 */
    293 	boundary = next_packet - 1;
    294 	if (boundary < rec_page_start)
    295 		boundary = rec_page_stop - 1;
    296 	NIC_PUT(ED_P0_BNRY, boundary);
    297 
    298 	return len;
    299 }
    300