1/*-
2 * SPDX-License-Identifier: BSD-4-Clause
3 *
4 * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. 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 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *      This product includes software developed by Herb Peyerl.
18 * 4. The name of Herb Peyerl may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD$");
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/kernel.h>
39#include <sys/socket.h>
40#include <sys/module.h>
41#include <sys/bus.h>
42
43#include <machine/bus.h>
44#include <machine/resource.h>
45#include <sys/rman.h>
46
47#include <net/if.h>
48#include <net/if_arp.h>
49#include <net/if_media.h>
50
51#include <isa/isavar.h>
52
53#include <dev/ep/if_epreg.h>
54#include <dev/ep/if_epvar.h>
55
56#ifdef __i386__
57#include <dev/ep/elink.h>
58#endif
59
60#ifdef __i386__
61static uint16_t get_eeprom_data(int, int);
62static void ep_isa_identify(driver_t *, device_t);
63#endif
64
65static int ep_isa_probe(device_t);
66static int ep_isa_attach(device_t);
67static int ep_eeprom_cksum(struct ep_softc *);
68
69struct isa_ident {
70	uint32_t id;
71	char *name;
72};
73const char *ep_isa_match_id(uint32_t, struct isa_ident *);
74
75#define ISA_ID_3C509_XXX   0x0506d509
76#define ISA_ID_3C509_TP    0x506d5090
77#define ISA_ID_3C509_BNC   0x506d5091
78#define ISA_ID_3C509_COMBO 0x506d5094
79#define ISA_ID_3C509_TPO   0x506d5095
80#define ISA_ID_3C509_TPC   0x506d5098
81
82#ifdef __i386__
83static struct isa_ident ep_isa_devs[] = {
84	{ISA_ID_3C509_TP, "3Com 3C509-TP EtherLink III"},
85	{ISA_ID_3C509_BNC, "3Com 3C509-BNC EtherLink III"},
86	{ISA_ID_3C509_COMBO, "3Com 3C509-Combo EtherLink III"},
87	{ISA_ID_3C509_TPO, "3Com 3C509-TPO EtherLink III"},
88	{ISA_ID_3C509_TPC, "3Com 3C509-TPC EtherLink III"},
89	{0, NULL},
90};
91#endif
92
93static struct isa_pnp_id ep_ids[] = {
94	{0x90506d50, "3Com 3C509B-TP EtherLink III (PnP)"},	/* TCM5090 */
95	{0x91506d50, "3Com 3C509B-BNC EtherLink III (PnP)"},	/* TCM5091 */
96	{0x94506d50, "3Com 3C509B-Combo EtherLink III (PnP)"},	/* TCM5094 */
97	{0x95506d50, "3Com 3C509B-TPO EtherLink III (PnP)"},	/* TCM5095 */
98	{0x98506d50, "3Com 3C509B-TPC EtherLink III (PnP)"},	/* TCM5098 */
99	{0xf780d041, NULL},	/* PNP80f7 */
100	{0, NULL},
101};
102
103/*
104 * We get eeprom data from the id_port given an offset into the eeprom.
105 * Basically; after the ID_sequence is sent to all of the cards; they enter
106 * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
107 * the eeprom data.  We then read the port 16 times and with every read; the
108 * cards check for contention (ie: if one card writes a 0 bit and another
109 * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
110 * compares the data on the bus; if there is a difference then that card goes
111 * into ID_WAIT state again). In the meantime; one bit of data is returned in
112 * the AX register which is conveniently returned to us by inb().  Hence; we
113 * read 16 times getting one bit of data with each read.
114 */
115#ifdef __i386__
116static uint16_t
117get_eeprom_data(int id_port, int offset)
118{
119	int i;
120	uint16_t data = 0;
121
122	outb(id_port, EEPROM_CMD_RD | offset);
123	DELAY(BIT_DELAY_MULTIPLE * 1000);
124	for (i = 0; i < 16; i++) {
125		DELAY(50);
126		data = (data << 1) | (inw(id_port) & 1);
127	}
128	return (data);
129}
130#endif
131
132const char *
133ep_isa_match_id(uint32_t id, struct isa_ident *isa_devs)
134{
135	struct isa_ident *i = isa_devs;
136
137	while (i->name != NULL) {
138		if (id == i->id)
139			return (i->name);
140		i++;
141	}
142	/*
143	 * If we see a card that is likely to be a 3c509
144	 * return something so that it will work; be annoying
145	 * so that the user will tell us about it though.
146	 */
147	if ((id >> 4) == ISA_ID_3C509_XXX)
148		return ("Unknown 3c509; notify maintainer!");
149	return (NULL);
150}
151
152#ifdef __i386__
153static void
154ep_isa_identify(driver_t * driver, device_t parent)
155{
156	int tag = EP_LAST_TAG;
157	int found = 0;
158	int i;
159	int j;
160	const char *desc;
161	uint16_t data;
162	uint32_t irq;
163	uint32_t ioport;
164	uint32_t isa_id;
165	device_t child;
166
167	outb(ELINK_ID_PORT, 0);
168	outb(ELINK_ID_PORT, 0);
169	elink_idseq(ELINK_509_POLY);
170	elink_reset();
171
172	DELAY(DELAY_MULTIPLE * 10000);
173
174	for (i = 0; i < EP_MAX_BOARDS; i++) {
175
176		outb(ELINK_ID_PORT, 0);
177		outb(ELINK_ID_PORT, 0);
178		elink_idseq(ELINK_509_POLY);
179		DELAY(400);
180
181		/*
182		 * For the first probe, clear all board's tag registers.
183		 * Otherwise kill off already-found boards. -- linux 3c509.c
184		 */
185		if (i == 0)
186			outb(ELINK_ID_PORT, 0xd0);
187		else
188			outb(ELINK_ID_PORT, 0xd8);
189
190		/* Get out of loop if we're out of cards. */
191		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
192		if (data != MFG_ID)
193			break;
194		/* resolve contention using the Ethernet address */
195		for (j = 0; j < 3; j++)
196			(void)get_eeprom_data(ELINK_ID_PORT, j);
197
198		/*
199		 * Construct an 'isa_id' in 'EISA' format.
200		 */
201		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
202		isa_id = (htons(data) << 16);
203		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_PROD_ID);
204		isa_id |= htons(data);
205
206		/* Find known ISA boards */
207		desc = ep_isa_match_id(isa_id, ep_isa_devs);
208		if (!desc) {
209			if (bootverbose)
210				device_printf(parent,
211				    "unknown ID 0x%08x\n", isa_id);
212			continue;
213		}
214		/* Retreive IRQ */
215		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_RESOURCE_CFG);
216		irq = (data >> 12);
217
218		/* Retreive IOPORT */
219		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_ADDR_CFG);
220		ioport = (((data & ADDR_CFG_MASK) << 4) + 0x200);
221
222		if ((data & ADDR_CFG_MASK) == ADDR_CFG_EISA) {
223			device_printf(parent,
224			    "<%s> at port 0x%03x in EISA mode, ignoring!\n",
225			    desc, ioport);
226			/*
227			 * Set the adaptor tag so that the next card can be
228			 * found.
229			 */
230			outb(ELINK_ID_PORT, tag--);
231			continue;
232		}
233		/* Test for an adapter with PnP support. */
234		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_CAP);
235		if (data == CAP_ISA) {
236			data = get_eeprom_data(ELINK_ID_PORT,
237			    EEPROM_INT_CONFIG_1);
238			if (data & ICW1_IAS_PNP) {
239				if (bootverbose)
240					device_printf(parent,
241					    "<%s> at 0x%03x "
242					    "in PnP mode!\n",
243					    desc, ioport);
244				/*
245				 * Set the adaptor tag so that the next card
246				 * can be found.
247				 */
248				outb(ELINK_ID_PORT, tag--);
249				continue;
250			}
251		}
252		/* Set the adaptor tag so that the next card can be found. */
253		outb(ELINK_ID_PORT, tag--);
254
255		/* Activate the adaptor at the EEPROM location. */
256		outb(ELINK_ID_PORT, ACTIVATE_ADAPTER_TO_CONFIG);
257
258		/* Test for an adapter in TEST mode. */
259		outw(ioport + EP_COMMAND, WINDOW_SELECT | 0);
260		data = inw(ioport + EP_W0_EEPROM_COMMAND);
261		if (data & EEPROM_TST_MODE) {
262			device_printf(parent,
263			    "<%s> at port 0x%03x in TEST mode!"
264			    "  Erase pencil mark.\n",
265			    desc, ioport);
266			continue;
267		}
268		child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "ep", -1);
269		device_set_desc_copy(child, desc);
270		device_set_driver(child, driver);
271		bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
272		bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, EP_IOSIZE);
273
274		if (bootverbose)
275			device_printf(parent,
276			    "<%s>"
277			    " at port 0x%03x-0x%03x irq %d\n",
278			    desc, ioport, ioport + EP_IOSIZE, irq);
279		found++;
280	}
281}
282#endif
283
284static int
285ep_isa_probe(device_t dev)
286{
287	int error = 0;
288
289	/* Check isapnp ids */
290	error = ISA_PNP_PROBE(device_get_parent(dev), dev, ep_ids);
291
292	/* If the card had a PnP ID that didn't match any we know about */
293	if (error == ENXIO)
294		return (error);
295
296	/* If we had some other problem. */
297	if (!(error == 0 || error == ENOENT))
298		return (error);
299
300	/* If we have the resources we need then we're good to go. */
301	if ((bus_get_resource_start(dev, SYS_RES_IOPORT, 0) != 0) &&
302	    (bus_get_resource_start(dev, SYS_RES_IRQ, 0) != 0))
303		return (0);
304
305	return (ENXIO);
306}
307
308static int
309ep_isa_attach(device_t dev)
310{
311	struct ep_softc *sc = device_get_softc(dev);
312	int error = 0;
313
314	if ((error = ep_alloc(dev)))
315		goto bad;
316	ep_get_media(sc);
317
318	GO_WINDOW(sc, 0);
319	SET_IRQ(sc, rman_get_start(sc->irq));
320
321	if ((error = ep_attach(sc)))
322		goto bad;
323	error = ep_eeprom_cksum(sc);
324	if (error) {
325		device_printf(sc->dev, "Invalid EEPROM checksum!\n");
326		goto bad;
327	}
328	if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
329	    NULL, ep_intr, sc, &sc->ep_intrhand))) {
330		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
331		goto bad;
332	}
333	return (0);
334bad:
335	ep_free(dev);
336	return (error);
337}
338
339static int
340ep_eeprom_cksum(struct ep_softc *sc)
341{
342	int i;
343	int error;
344	uint16_t val;
345	uint16_t cksum;
346	uint8_t cksum_high = 0;
347	uint8_t cksum_low = 0;
348
349	error = ep_get_e(sc, 0x0f, &val);
350	if (error)
351		return (ENXIO);
352	cksum = val;
353
354	for (i = 0; i < 0x0f; i++) {
355		error = ep_get_e(sc, i, &val);
356		if (error)
357			return (ENXIO);
358		switch (i) {
359		case 0x08:
360		case 0x09:
361		case 0x0d:
362			cksum_low ^= (uint8_t) (val & 0x00ff) ^
363			    (uint8_t)((val & 0xff00) >> 8);
364			break;
365		default:
366			cksum_high ^= (uint8_t) (val & 0x00ff) ^
367			    (uint8_t)((val & 0xff00) >> 8);
368			break;
369		}
370	}
371	return (cksum != ((uint16_t)cksum_low | (uint16_t)(cksum_high << 8)));
372}
373
374static device_method_t ep_isa_methods[] = {
375	/* Device interface */
376#ifdef __i386__
377	DEVMETHOD(device_identify, ep_isa_identify),
378#endif
379	DEVMETHOD(device_probe, ep_isa_probe),
380	DEVMETHOD(device_attach, ep_isa_attach),
381	DEVMETHOD(device_detach, ep_detach),
382
383	DEVMETHOD_END
384};
385
386static driver_t ep_isa_driver = {
387	"ep",
388	ep_isa_methods,
389	sizeof(struct ep_softc),
390};
391
392extern devclass_t ep_devclass;
393
394DRIVER_MODULE(ep, isa, ep_isa_driver, ep_devclass, 0, 0);
395#ifdef __i386__
396MODULE_DEPEND(ep, elink, 1, 1, 1);
397#endif
398ISA_PNP_INFO(ep_ids);
399