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