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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 61bfc370 was 61bfc370, checked in by Martin Decky <martin@…>, 14 years ago
  • make measured_string and other network-related data types binary-safe
  • fix several network-related routines binary-safe (replace clearly suspicious use of str_lcmp() with bcmp())
  • rename spawn() to net_spawn()
  • Property mode set to 100644
File size: 7.9 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
83int ne_probe(dpeth_t *dep)
84{
85 int byte;
86 int i;
87 int loc1, loc2;
88 testf_t f;
89
90 dep->de_dp8390_port = dep->de_base_port + NE_DP8390;
91
92 /*
93 * We probe for an ne1000 or an ne2000 by testing whether the
94 * on board is reachable through the dp8390. Note that the
95 * ne1000 is an 8bit card and has a memory region distict from
96 * the 16bit ne2000
97 */
98
99 for (dep->de_16bit = 0; dep->de_16bit < 2; dep->de_16bit++) {
100 /* Reset the ethernet card */
101 byte= inb_ne(dep, NE_RESET);
102 usleep(2000);
103 outb_ne(dep, NE_RESET, byte);
104 usleep(2000);
105
106 /* Reset the dp8390 */
107 outb_reg0(dep, DP_CR, CR_STP | CR_DM_ABORT);
108 for (i = 0; i < 0x1000 && ((inb_reg0(dep, DP_ISR) & ISR_RST) == 0); i++)
109 ; /* Do nothing */
110
111 /* Check if the dp8390 is really there */
112 if ((inb_reg0(dep, DP_CR) & (CR_STP | CR_DM_ABORT)) !=
113 (CR_STP | CR_DM_ABORT))
114 return 0;
115
116 /* Disable the receiver and init TCR and DCR. */
117 outb_reg0(dep, DP_RCR, RCR_MON);
118 outb_reg0(dep, DP_TCR, TCR_NORMAL);
119 if (dep->de_16bit) {
120 outb_reg0(dep, DP_DCR, DCR_WORDWIDE | DCR_8BYTES |
121 DCR_BMS);
122 } else {
123 outb_reg0(dep, DP_DCR, DCR_BYTEWIDE | DCR_8BYTES |
124 DCR_BMS);
125 }
126
127 if (dep->de_16bit) {
128 loc1 = NE2000_START;
129 loc2 = NE2000_START + NE2000_SIZE - 4;
130 f = test_16;
131 } else {
132 loc1 = NE1000_START;
133 loc2 = NE1000_START + NE1000_SIZE - 4;
134 f = test_8;
135 }
136
137 if (f(dep, loc1, pat0) && f(dep, loc1, pat1) &&
138 f(dep, loc1, pat2) && f(dep, loc1, pat3) &&
139 f(dep, loc2, pat0) && f(dep, loc2, pat1) &&
140 f(dep, loc2, pat2) && f(dep, loc2, pat3)) {
141 return 1;
142 }
143 }
144
145 return 0;
146}
147
148/** Initializes the NE2000 network interface.
149 *
150 * @param[in,out] dep The network interface structure.
151 *
152 */
153void ne_init(dpeth_t *dep)
154{
155 int i;
156 int word, sendq_nr;
157
158 /* Setup a transfer to get the ethernet address. */
159 if (dep->de_16bit)
160 outb_reg0(dep, DP_RBCR0, 6*2);
161 else
162 outb_reg0(dep, DP_RBCR0, 6);
163
164 outb_reg0(dep, DP_RBCR1, 0);
165 outb_reg0(dep, DP_RSAR0, 0);
166 outb_reg0(dep, DP_RSAR1, 0);
167 outb_reg0(dep, DP_CR, CR_DM_RR | CR_PS_P0 | CR_STA);
168
169 for (i = 0; i < 6; i++) {
170 if (dep->de_16bit) {
171 word = inw_ne(dep, NE_DATA);
172 dep->de_address.ea_addr[i] = word;
173 } else
174 dep->de_address.ea_addr[i] = inb_ne(dep, NE_DATA);
175 }
176
177 dep->de_data_port= dep->de_base_port + NE_DATA;
178 if (dep->de_16bit) {
179 dep->de_ramsize = NE2000_SIZE;
180 dep->de_offset_page = NE2000_START / DP_PAGESIZE;
181 } else {
182 dep->de_ramsize = NE1000_SIZE;
183 dep->de_offset_page = NE1000_START / DP_PAGESIZE;
184 }
185
186 /* Allocate one send buffer (1.5KB) per 8KB of on board memory. */
187 sendq_nr = dep->de_ramsize / 0x2000;
188
189 if (sendq_nr < 1)
190 sendq_nr = 1;
191 else if (sendq_nr > SENDQ_NR)
192 sendq_nr = SENDQ_NR;
193
194 dep->de_sendq_nr = sendq_nr;
195 for (i = 0; i < sendq_nr; i++)
196 dep->de_sendq[i].sq_sendpage = dep->de_offset_page + i * SENDQ_PAGES;
197
198 dep->de_startpage = dep->de_offset_page + i * SENDQ_PAGES;
199 dep->de_stoppage = dep->de_offset_page + dep->de_ramsize / DP_PAGESIZE;
200
201 printf("Novell NE%d000 ethernet card at I/O address "
202 "%#lx, memory size %#lx, irq %d\n",
203 dep->de_16bit ? 2 : 1, dep->de_base_port, dep->de_ramsize,
204 dep->de_irq);
205}
206
207static int test_8(dpeth_t *dep, int pos, uint8_t *pat)
208{
209 uint8_t buf[4];
210 int i;
211
212 outb_reg0(dep, DP_ISR, 0xff);
213
214 /* Setup a transfer to put the pattern. */
215 outb_reg0(dep, DP_RBCR0, 4);
216 outb_reg0(dep, DP_RBCR1, 0);
217 outb_reg0(dep, DP_RSAR0, pos & 0xff);
218 outb_reg0(dep, DP_RSAR1, pos >> 8);
219 outb_reg0(dep, DP_CR, CR_DM_RW | CR_PS_P0 | CR_STA);
220
221 for (i = 0; i < 4; i++)
222 outb_ne(dep, NE_DATA, pat[i]);
223
224 for (i = 0; i < N; i++) {
225 if (inb_reg0(dep, DP_ISR) & ISR_RDC)
226 break;
227 }
228
229 if (i == N) {
230 printf("NE1000 remote DMA test failed\n");
231 return 0;
232 }
233
234 outb_reg0(dep, DP_RBCR0, 4);
235 outb_reg0(dep, DP_RBCR1, 0);
236 outb_reg0(dep, DP_RSAR0, pos & 0xff);
237 outb_reg0(dep, DP_RSAR1, pos >> 8);
238 outb_reg0(dep, DP_CR, CR_DM_RR | CR_PS_P0 | CR_STA);
239
240 for (i = 0; i < 4; i++)
241 buf[i] = inb_ne(dep, NE_DATA);
242
243 return (bcmp(buf, pat, 4) == 0);
244}
245
246static int test_16(dpeth_t *dep, int pos, uint8_t *pat)
247{
248 uint8_t buf[4];
249 int i;
250
251 outb_reg0(dep, DP_ISR, 0xff);
252
253 /* Setup a transfer to put the pattern. */
254 outb_reg0(dep, DP_RBCR0, 4);
255 outb_reg0(dep, DP_RBCR1, 0);
256 outb_reg0(dep, DP_RSAR0, pos & 0xff);
257 outb_reg0(dep, DP_RSAR1, pos >> 8);
258 outb_reg0(dep, DP_CR, CR_DM_RW | CR_PS_P0 | CR_STA);
259
260 for (i = 0; i < 4; i += 2)
261 outw_ne(dep, NE_DATA, *(uint16_t *)(pat + i));
262
263 for (i = 0; i < N; i++) {
264 if (inb_reg0(dep, DP_ISR) &ISR_RDC)
265 break;
266 }
267
268 if (i == N) {
269 printf("NE2000 remote DMA test failed\n");
270 return 0;
271 }
272
273 outb_reg0(dep, DP_RBCR0, 4);
274 outb_reg0(dep, DP_RBCR1, 0);
275 outb_reg0(dep, DP_RSAR0, pos & 0xff);
276 outb_reg0(dep, DP_RSAR1, pos >> 8);
277 outb_reg0(dep, DP_CR, CR_DM_RR | CR_PS_P0 | CR_STA);
278
279 for (i = 0; i < 4; i += 2)
280 *(uint16_t *)(buf + i) = inw_ne(dep, NE_DATA);
281
282 return (bcmp(buf, pat, 4) == 0);
283}
284
285/** Stop the NE2000 network interface.
286 *
287 * @param[in,out] dep The network interface structure.
288 *
289 */
290void ne_stop(dpeth_t *dep)
291{
292 /* Reset the ethernet card */
293 int byte = inb_ne(dep, NE_RESET);
294 usleep(2000);
295 outb_ne(dep, NE_RESET, byte);
296}
297
298/** @}
299 */
Note: See TracBrowser for help on using the repository browser.