1141586Simp/*-
2141586Simp * Copyright (c) 2005, M. Warner Losh
3141586Simp * All rights reserved.
4141586Simp * Copyright (c) 1995, David Greenman
5141586Simp * All rights reserved.
6141586Simp *
7141586Simp * Redistribution and use in source and binary forms, with or without
8141586Simp * modification, are permitted provided that the following conditions
9141586Simp * are met:
10141586Simp * 1. Redistributions of source code must retain the above copyright
11141586Simp *    notice unmodified, this list of conditions, and the following
12141586Simp *    disclaimer.
13141586Simp * 2. Redistributions in binary form must reproduce the above copyright
14141586Simp *    notice, this list of conditions and the following disclaimer in the
15141586Simp *    documentation and/or other materials provided with the distribution.
16141586Simp *
17141586Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18141586Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19141586Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20141586Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21141586Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22141586Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23141586Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24141586Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25141586Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26141586Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27141586Simp * SUCH DAMAGE.
28141586Simp */
29141586Simp
30141586Simp
31141586Simp#include <sys/cdefs.h>
32141586Simp__FBSDID("$FreeBSD$");
33141586Simp
34141586Simp#include "opt_ed.h"
35141586Simp
36141586Simp#include <sys/param.h>
37141586Simp#include <sys/systm.h>
38141586Simp#include <sys/sockio.h>
39141586Simp#include <sys/mbuf.h>
40141586Simp#include <sys/kernel.h>
41141586Simp#include <sys/socket.h>
42141586Simp#include <sys/syslog.h>
43141586Simp
44141586Simp#include <sys/bus.h>
45141586Simp
46141586Simp#include <machine/bus.h>
47141586Simp#include <sys/rman.h>
48141586Simp#include <machine/resource.h>
49141586Simp
50141586Simp#include <net/ethernet.h>
51141586Simp#include <net/if.h>
52141586Simp#include <net/if_arp.h>
53141586Simp#include <net/if_dl.h>
54141586Simp#include <net/if_mib.h>
55141586Simp#include <net/if_media.h>
56141586Simp
57141586Simp#include <net/bpf.h>
58141586Simp
59141586Simp#include <dev/ed/if_edreg.h>
60141586Simp#include <dev/ed/if_edvar.h>
61141586Simp
62141600Simpstatic int ed_probe_gwether(device_t);
63141600Simp
64141586Simp/*
65141586Simp * Probe and vendor-specific initialization routine for NE1000/2000 boards
66141586Simp */
67149840Simpint
68141586Simped_probe_Novell_generic(device_t dev, int flags)
69141586Simp{
70141586Simp	struct ed_softc *sc = device_get_softc(dev);
71141932Simp	u_int   memsize;
72141600Simp	int	error;
73141932Simp	u_char  tmp;
74141586Simp	static char test_pattern[32] = "THIS is A memory TEST pattern";
75141586Simp	char    test_buffer[32];
76141586Simp
77141586Simp	/* Reset the board */
78141586Simp	if (ED_FLAGS_GETTYPE(flags) == ED_FLAGS_GWETHER) {
79141586Simp		ed_asic_outb(sc, ED_NOVELL_RESET, 0);
80141586Simp		DELAY(200);
81141586Simp	}
82141586Simp	tmp = ed_asic_inb(sc, ED_NOVELL_RESET);
83141586Simp
84141586Simp	/*
85141586Simp	 * I don't know if this is necessary; probably cruft leftover from
86141586Simp	 * Clarkson packet driver code. Doesn't do a thing on the boards I've
87149840Simp	 * tested. -DG
88141586Simp	 */
89141586Simp	ed_asic_outb(sc, ED_NOVELL_RESET, tmp);
90141586Simp	DELAY(5000);
91141586Simp
92141586Simp	/*
93141586Simp	 * This is needed because some NE clones apparently don't reset the
94141586Simp	 * NIC properly (or the NIC chip doesn't reset fully on power-up) XXX
95141586Simp	 * - this makes the probe invasive! ...Done against my better
96141586Simp	 * judgement. -DLG
97141586Simp	 */
98141586Simp	ed_nic_outb(sc, ED_P0_CR, ED_CR_RD2 | ED_CR_STP);
99141586Simp	DELAY(5000);
100141586Simp
101141586Simp	/* Make sure that we really have an 8390 based board */
102141586Simp	if (!ed_probe_generic8390(sc))
103141586Simp		return (ENXIO);
104141586Simp
105141586Simp	sc->vendor = ED_VENDOR_NOVELL;
106141586Simp	sc->mem_shared = 0;
107141586Simp	sc->cr_proto = ED_CR_RD2;
108141586Simp
109141586Simp	/*
110141586Simp	 * Test the ability to read and write to the NIC memory. This has the
111141586Simp	 * side affect of determining if this is an NE1000 or an NE2000.
112141586Simp	 */
113141586Simp
114141586Simp	/*
115141586Simp	 * This prevents packets from being stored in the NIC memory when the
116141586Simp	 * readmem routine turns on the start bit in the CR.
117141586Simp	 */
118141586Simp	ed_nic_outb(sc, ED_P0_RCR, ED_RCR_MON);
119141586Simp
120141586Simp	/* Temporarily initialize DCR for byte operations */
121141586Simp	ed_nic_outb(sc, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
122141586Simp
123141586Simp	ed_nic_outb(sc, ED_P0_PSTART, 8192 / ED_PAGE_SIZE);
124141586Simp	ed_nic_outb(sc, ED_P0_PSTOP, 16384 / ED_PAGE_SIZE);
125141586Simp
126141586Simp	/*
127211792Simp	 * Some devices identify themselves.  Some of those devices
128211792Simp	 * can't handle being probed, so we allow forcing a mode.  If
129211792Simp	 * these flags are set, force it, otherwise probe.
130141586Simp	 */
131211792Simp	if (flags & ED_FLAGS_FORCE_8BIT_MODE) {
132211792Simp		sc->isa16bit = 0;
133141586Simp		sc->type = ED_TYPE_NE1000;
134141586Simp		sc->type_str = "NE1000";
135211792Simp	} else if (flags & ED_FLAGS_FORCE_16BIT_MODE) {
136211792Simp		sc->isa16bit = 1;
137211792Simp		sc->type = ED_TYPE_NE2000;
138211792Simp		sc->type_str = "NE2000";
139211793Simp		ed_nic_outb(sc, ED_P0_DCR, ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
140211793Simp		ed_nic_outb(sc, ED_P0_PSTART, 16384 / ED_PAGE_SIZE);
141211793Simp		ed_nic_outb(sc, ED_P0_PSTOP, 32768 / ED_PAGE_SIZE);
142141586Simp	} else {
143141586Simp		/*
144211792Simp		 * Write a test pattern in byte mode. If this fails, then there
145211792Simp		 * probably isn't any memory at 8k - which likely means that the board
146211792Simp		 * is an NE2000.
147141586Simp		 */
148211792Simp		ed_pio_writemem(sc, test_pattern, 8192, sizeof(test_pattern));
149211792Simp		ed_pio_readmem(sc, 8192, test_buffer, sizeof(test_pattern));
150211792Simp
151141586Simp		if (bcmp(test_pattern, test_buffer, sizeof(test_pattern)) == 0) {
152211792Simp			sc->type = ED_TYPE_NE1000;
153211792Simp			sc->type_str = "NE1000";
154211792Simp			sc->isa16bit = 0;
155141586Simp		} else {
156211792Simp			/* Not an NE1000 - try NE2000 */
157211792Simp			sc->isa16bit = 1;
158211792Simp			ed_nic_outb(sc, ED_P0_DCR, ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
159211792Simp			ed_nic_outb(sc, ED_P0_PSTART, 16384 / ED_PAGE_SIZE);
160211792Simp			ed_nic_outb(sc, ED_P0_PSTOP, 32768 / ED_PAGE_SIZE);
161211792Simp			/*
162211792Simp			 * Write a test pattern in word mode. If this also fails, then
163211792Simp			 * we don't know what this board is.
164211792Simp			 */
165211792Simp			ed_pio_writemem(sc, test_pattern, 16384, sizeof(test_pattern));
166211792Simp			ed_pio_readmem(sc, 16384, test_buffer, sizeof(test_pattern));
167211792Simp			if (bcmp(test_pattern, test_buffer, sizeof(test_pattern)) == 0) {
168211792Simp				sc->type = ED_TYPE_NE2000;
169211792Simp				sc->type_str = "NE2000";
170211792Simp			} else {
171211792Simp				return (ENXIO);
172211792Simp			}
173141586Simp		}
174141586Simp	}
175141681Simp	sc->chip_type = ED_CHIP_TYPE_DP8390;
176141586Simp
177141586Simp	/* 8k of memory plus an additional 8k if 16bit */
178141586Simp	memsize = 8192 + sc->isa16bit * 8192;
179141586Simp	sc->mem_size = memsize;
180141586Simp
181141586Simp	/* NIC memory doesn't start at zero on an NE board */
182141586Simp	/* The start address is tied to the bus width */
183149558Simp	sc->mem_start = 8192 + sc->isa16bit * 8192;
184141586Simp	sc->mem_end = sc->mem_start + memsize;
185141586Simp	sc->tx_page_start = memsize / ED_PAGE_SIZE;
186141586Simp
187141586Simp	if (ED_FLAGS_GETTYPE(flags) == ED_FLAGS_GWETHER) {
188141600Simp		error = ed_probe_gwether(dev);
189141600Simp		if (error)
190141600Simp			return (error);
191141586Simp	}
192141586Simp
193141586Simp	/*
194141586Simp	 * Use one xmit buffer if < 16k, two buffers otherwise (if not told
195141586Simp	 * otherwise).
196141586Simp	 */
197141586Simp	if ((memsize < 16384) || (flags & ED_FLAGS_NO_MULTI_BUFFERING))
198141586Simp		sc->txb_cnt = 1;
199141586Simp	else
200141586Simp		sc->txb_cnt = 2;
201141586Simp
202141586Simp	sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
203141586Simp	sc->rec_page_stop = sc->tx_page_start + memsize / ED_PAGE_SIZE;
204141586Simp
205141586Simp	sc->mem_ring = sc->mem_start + sc->txb_cnt * ED_PAGE_SIZE * ED_TXBUF_SIZE;
206141586Simp	/* clear any pending interrupts that might have occurred above */
207141586Simp	ed_nic_outb(sc, ED_P0_ISR, 0xff);
208141586Simp
209154924Simp	sc->sc_write_mbufs = ed_pio_write_mbufs;
210141586Simp	return (0);
211141586Simp}
212141586Simp
213141586Simpint
214141586Simped_probe_Novell(device_t dev, int port_rid, int flags)
215141586Simp{
216141586Simp	struct ed_softc *sc = device_get_softc(dev);
217141586Simp	int	error;
218141586Simp
219141586Simp	error = ed_alloc_port(dev, port_rid, ED_NOVELL_IO_PORTS);
220141586Simp	if (error)
221141586Simp		return (error);
222141586Simp
223141586Simp	sc->asic_offset = ED_NOVELL_ASIC_OFFSET;
224141586Simp	sc->nic_offset  = ED_NOVELL_NIC_OFFSET;
225141586Simp
226141586Simp	return ed_probe_Novell_generic(dev, flags);
227141586Simp}
228141600Simp
229141600Simpstatic int
230141600Simped_probe_gwether(device_t dev)
231141600Simp{
232141600Simp	int     x, i, msize = 0;
233149558Simp	bus_size_t mstart = 0;
234141600Simp	char    pbuf0[ED_PAGE_SIZE], pbuf[ED_PAGE_SIZE], tbuf[ED_PAGE_SIZE];
235141600Simp	struct ed_softc *sc = device_get_softc(dev);
236141600Simp
237141600Simp	for (i = 0; i < ED_PAGE_SIZE; i++)
238141600Simp		pbuf0[i] = 0;
239141600Simp
240141600Simp	/* Clear all the memory. */
241141600Simp	for (x = 1; x < 256; x++)
242141600Simp		ed_pio_writemem(sc, pbuf0, x * 256, ED_PAGE_SIZE);
243141600Simp
244141600Simp	/* Search for the start of RAM. */
245141600Simp	for (x = 1; x < 256; x++) {
246141600Simp		ed_pio_readmem(sc, x * 256, tbuf, ED_PAGE_SIZE);
247141600Simp		if (bcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
248141600Simp			for (i = 0; i < ED_PAGE_SIZE; i++)
249141600Simp				pbuf[i] = 255 - x;
250141600Simp			ed_pio_writemem(sc, pbuf, x * 256, ED_PAGE_SIZE);
251141600Simp			ed_pio_readmem(sc, x * 256, tbuf, ED_PAGE_SIZE);
252141600Simp			if (bcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0) {
253141600Simp				mstart = x * ED_PAGE_SIZE;
254141600Simp				msize = ED_PAGE_SIZE;
255141600Simp				break;
256141600Simp			}
257141600Simp		}
258141600Simp	}
259141600Simp	if (mstart == 0) {
260141600Simp		device_printf(dev, "Cannot find start of RAM.\n");
261141600Simp		return (ENXIO);
262141600Simp	}
263141600Simp
264141600Simp	/* Probe the size of RAM. */
265141600Simp	for (x = (mstart / ED_PAGE_SIZE) + 1; x < 256; x++) {
266141600Simp		ed_pio_readmem(sc, x * 256, tbuf, ED_PAGE_SIZE);
267141600Simp		if (bcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
268141600Simp			for (i = 0; i < ED_PAGE_SIZE; i++)
269141600Simp				pbuf[i] = 255 - x;
270141600Simp			ed_pio_writemem(sc, pbuf, x * 256, ED_PAGE_SIZE);
271141600Simp			ed_pio_readmem(sc, x * 256, tbuf, ED_PAGE_SIZE);
272141600Simp			if (bcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0)
273141600Simp				msize += ED_PAGE_SIZE;
274141600Simp			else {
275141600Simp				break;
276141600Simp			}
277141600Simp		} else {
278141600Simp			break;
279141600Simp		}
280141600Simp	}
281141600Simp
282141600Simp	if (msize == 0) {
283141600Simp		device_printf(dev,
284149558Simp		    "Cannot find any RAM, start : %d, x = %d.\n",
285149558Simp		    (int)mstart, x);
286141600Simp		return (ENXIO);
287141600Simp	}
288149558Simp	if (bootverbose)
289149558Simp		device_printf(dev,
290149558Simp		    "RAM start at %d, size : %d.\n", (int)mstart, msize);
291141600Simp
292141600Simp	sc->mem_size = msize;
293149558Simp	sc->mem_start = mstart;
294149558Simp	sc->mem_end = msize + mstart;
295141600Simp	sc->tx_page_start = mstart / ED_PAGE_SIZE;
296141600Simp	return 0;
297141600Simp}
298141932Simp
299141932Simpvoid
300141932Simped_Novell_read_mac(struct ed_softc *sc)
301141932Simp{
302141932Simp	int n;
303141932Simp	uint8_t romdata[16];
304141932Simp
305141932Simp	/*
306159538Simp	 * Most ne1000/ne2000 compatible cards have their MAC address
307159538Simp	 * located in the first few words of the address space.  This seems
308159538Simp	 * universally true for ISA and PCI implementations, but PC Card
309159538Simp	 * devices seem to have more variance.
310141932Simp	 */
311141932Simp	ed_pio_readmem(sc, 0, romdata, 16);
312141932Simp	for (n = 0; n < ETHER_ADDR_LEN; n++)
313147256Sbrooks		sc->enaddr[n] = romdata[n * (sc->isa16bit + 1)];
314141932Simp}
315