source: mainline/uspace/srv/hw/netif/dp8390/ne2000.c@ e0854e3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e0854e3 was e0854e3, checked in by Martin Decky <martin@…>, 14 years ago

add missing copyright headers
remove unused flags

  • Property mode set to 100644
File size: 8.1 KB
Line 
1/*
2 * Copyright (c) 2009 Lukas Mejdrech
3 * Copyright (c) 2011 Martin Decky
4 * All rights reserved.
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 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - 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 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/*
31 * This code is based upon the NE2000 driver for MINIX,
32 * distributed according to a BSD-style license.
33 *
34 * Copyright (c) 1987, 1997, 2006 Vrije Universiteit
35 * Copyright (c) 1992, 1994 Philip Homburg
36 * Copyright (c) 1996 G. Falzoni
37 *
38 */
39
40/** @addtogroup ne2k
41 * @{
42 */
43
44/** @file
45 * NE1000 and NE2000 network interface initialization and probe functions implementation.
46 */
47
48#include <stdio.h>
49#include <unistd.h>
50#include "dp8390_port.h"
51#include "dp8390.h"
52#include "ne2000.h"
53
54/** Number of bytes to transfer */
55#define N 100
56
57typedef int (*testf_t)(dpeth_t *dep, int pos, uint8_t *pat);
58
59/** Data patterns */
60uint8_t pat0[] = {0x00, 0x00, 0x00, 0x00};
61uint8_t pat1[] = {0xFF, 0xFF, 0xFF, 0xFF};
62uint8_t pat2[] = {0xA5, 0x5A, 0x69, 0x96};
63uint8_t pat3[] = {0x96, 0x69, 0x5A, 0xA5};
64
65/** Tests 8 bit NE2000 network interface.
66 * @param[in,out] dep The network interface structure.
67 * @param[in] pos The starting position.
68 * @param[in] pat The data pattern to be written.
69 * @returns True on success.
70 * @returns false otherwise.
71 */
72static int test_8(dpeth_t *dep, int pos, uint8_t *pat);
73
74/** Tests 16 bit NE2000 network interface.
75 * @param[in,out] dep The network interface structure.
76 * @param[in] pos The starting position.
77 * @param[in] pat The data pattern to be written.
78 * @returns True on success.
79 * @returns false otherwise.
80 */
81static int test_16(dpeth_t *dep, int pos, uint8_t *pat);
82
83/** Stops the NE2000 network interface.
84 * @param[in,out] dep The network interface structure.
85 */
86static void ne_stop(dpeth_t *dep);
87
88/** Initializes the NE2000 network interface.
89 * @param[in,out] dep The network interface structure.
90 */
91void ne_init(struct dpeth *dep);
92
93int ne_probe(dpeth_t *dep)
94{
95 int byte;
96 int i;
97 int loc1, loc2;
98 testf_t f;
99
100 dep->de_dp8390_port = dep->de_base_port + NE_DP8390;
101
102 /*
103 * We probe for an ne1000 or an ne2000 by testing whether the
104 * on board is reachable through the dp8390. Note that the
105 * ne1000 is an 8bit card and has a memory region distict from
106 * the 16bit ne2000
107 */
108
109 for (dep->de_16bit = 0; dep->de_16bit < 2; dep->de_16bit++) {
110 /* Reset the ethernet card */
111 byte= inb_ne(dep, NE_RESET);
112 usleep(2000);
113 outb_ne(dep, NE_RESET, byte);
114 usleep(2000);
115
116 /* Reset the dp8390 */
117 outb_reg0(dep, DP_CR, CR_STP | CR_DM_ABORT);
118 for (i = 0; i < 0x1000 && ((inb_reg0(dep, DP_ISR) & ISR_RST) == 0); i++)
119 ; /* Do nothing */
120
121 /* Check if the dp8390 is really there */
122 if ((inb_reg0(dep, DP_CR) & (CR_STP | CR_DM_ABORT)) !=
123 (CR_STP | CR_DM_ABORT))
124 return 0;
125
126 /* Disable the receiver and init TCR and DCR. */
127 outb_reg0(dep, DP_RCR, RCR_MON);
128 outb_reg0(dep, DP_TCR, TCR_NORMAL);
129 if (dep->de_16bit) {
130 outb_reg0(dep, DP_DCR, DCR_WORDWIDE | DCR_8BYTES |
131 DCR_BMS);
132 } else {
133 outb_reg0(dep, DP_DCR, DCR_BYTEWIDE | DCR_8BYTES |
134 DCR_BMS);
135 }
136
137 if (dep->de_16bit) {
138 loc1= NE2000_START;
139 loc2= NE2000_START + NE2000_SIZE - 4;
140 f= test_16;
141 } else {
142 loc1= NE1000_START;
143 loc2= NE1000_START + NE1000_SIZE - 4;
144 f= test_8;
145 }
146
147 if (f(dep, loc1, pat0) && f(dep, loc1, pat1) &&
148 f(dep, loc1, pat2) && f(dep, loc1, pat3) &&
149 f(dep, loc2, pat0) && f(dep, loc2, pat1) &&
150 f(dep, loc2, pat2) && f(dep, loc2, pat3)) {
151 dep->de_initf = ne_init;
152 dep->de_stopf = ne_stop;
153 return 1;
154 }
155 }
156
157 return 0;
158}
159
160void ne_init(dpeth_t *dep)
161{
162 int i;
163 int word, sendq_nr;
164
165 /* Setup a transfer to get the ethernet address. */
166 if (dep->de_16bit)
167 outb_reg0(dep, DP_RBCR0, 6*2);
168 else
169 outb_reg0(dep, DP_RBCR0, 6);
170
171 outb_reg0(dep, DP_RBCR1, 0);
172 outb_reg0(dep, DP_RSAR0, 0);
173 outb_reg0(dep, DP_RSAR1, 0);
174 outb_reg0(dep, DP_CR, CR_DM_RR | CR_PS_P0 | CR_STA);
175
176 for (i = 0; i < 6; i++) {
177 if (dep->de_16bit) {
178 word = inw_ne(dep, NE_DATA);
179 dep->de_address.ea_addr[i] = word;
180 } else
181 dep->de_address.ea_addr[i] = inb_ne(dep, NE_DATA);
182 }
183
184 dep->de_data_port= dep->de_base_port + NE_DATA;
185 if (dep->de_16bit) {
186 dep->de_ramsize = NE2000_SIZE;
187 dep->de_offset_page = NE2000_START / DP_PAGESIZE;
188 } else {
189 dep->de_ramsize = NE1000_SIZE;
190 dep->de_offset_page = NE1000_START / DP_PAGESIZE;
191 }
192
193 /* Allocate one send buffer (1.5KB) per 8KB of on board memory. */
194 sendq_nr = dep->de_ramsize / 0x2000;
195
196 if (sendq_nr < 1)
197 sendq_nr = 1;
198 else if (sendq_nr > SENDQ_NR)
199 sendq_nr = SENDQ_NR;
200
201 dep->de_sendq_nr = sendq_nr;
202 for (i = 0; i < sendq_nr; i++)
203 dep->de_sendq[i].sq_sendpage = dep->de_offset_page + i * SENDQ_PAGES;
204
205 dep->de_startpage = dep->de_offset_page + i * SENDQ_PAGES;
206 dep->de_stoppage = dep->de_offset_page + dep->de_ramsize / DP_PAGESIZE;
207
208 printf("%s: Novell NE%d000 ethernet card at I/O address "
209 "%#lx, memory size %#lx, irq %d\n",
210 dep->de_name, dep->de_16bit ? 2 : 1,
211 dep->de_base_port, dep->de_ramsize, dep->de_irq);
212}
213
214static int test_8(dpeth_t *dep, int pos, uint8_t *pat)
215{
216 uint8_t buf[4];
217 int i;
218
219 outb_reg0(dep, DP_ISR, 0xff);
220
221 /* Setup a transfer to put the pattern. */
222 outb_reg0(dep, DP_RBCR0, 4);
223 outb_reg0(dep, DP_RBCR1, 0);
224 outb_reg0(dep, DP_RSAR0, pos & 0xff);
225 outb_reg0(dep, DP_RSAR1, pos >> 8);
226 outb_reg0(dep, DP_CR, CR_DM_RW | CR_PS_P0 | CR_STA);
227
228 for (i = 0; i < 4; i++)
229 outb_ne(dep, NE_DATA, pat[i]);
230
231 for (i = 0; i < N; i++) {
232 if (inb_reg0(dep, DP_ISR) & ISR_RDC)
233 break;
234 }
235
236 if (i == N) {
237 printf("%s: NE1000 remote DMA test failed\n", dep->de_name);
238 return 0;
239 }
240
241 outb_reg0(dep, DP_RBCR0, 4);
242 outb_reg0(dep, DP_RBCR1, 0);
243 outb_reg0(dep, DP_RSAR0, pos & 0xff);
244 outb_reg0(dep, DP_RSAR1, pos >> 8);
245 outb_reg0(dep, DP_CR, CR_DM_RR | CR_PS_P0 | CR_STA);
246
247 for (i = 0; i < 4; i++)
248 buf[i] = inb_ne(dep, NE_DATA);
249
250 return (memcmp(buf, pat, 4) == 0);
251}
252
253static int test_16(dpeth_t *dep, int pos, uint8_t *pat)
254{
255 uint8_t buf[4];
256 int i;
257
258 outb_reg0(dep, DP_ISR, 0xff);
259
260 /* Setup a transfer to put the pattern. */
261 outb_reg0(dep, DP_RBCR0, 4);
262 outb_reg0(dep, DP_RBCR1, 0);
263 outb_reg0(dep, DP_RSAR0, pos & 0xff);
264 outb_reg0(dep, DP_RSAR1, pos >> 8);
265 outb_reg0(dep, DP_CR, CR_DM_RW | CR_PS_P0 | CR_STA);
266
267 for (i = 0; i < 4; i += 2)
268 outw_ne(dep, NE_DATA, *(uint16_t *)(pat + i));
269
270 for (i = 0; i < N; i++) {
271 if (inb_reg0(dep, DP_ISR) &ISR_RDC)
272 break;
273 }
274
275 if (i == N) {
276 printf("%s: NE2000 remote DMA test failed\n", dep->de_name);
277 return 0;
278 }
279
280 outb_reg0(dep, DP_RBCR0, 4);
281 outb_reg0(dep, DP_RBCR1, 0);
282 outb_reg0(dep, DP_RSAR0, pos & 0xff);
283 outb_reg0(dep, DP_RSAR1, pos >> 8);
284 outb_reg0(dep, DP_CR, CR_DM_RR | CR_PS_P0 | CR_STA);
285
286 for (i = 0; i < 4; i += 2)
287 *(uint16_t *)(buf + i) = inw_ne(dep, NE_DATA);
288
289 return (memcmp(buf, pat, 4) == 0);
290}
291
292static void ne_stop(dpeth_t *dep)
293{
294 /* Reset the ethernet card */
295 int byte = inb_ne(dep, NE_RESET);
296 usleep(2000);
297 outb_ne(dep, NE_RESET, byte);
298}
299
300/** @}
301 */
Note: See TracBrowser for help on using the repository browser.