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