summaryrefslogtreecommitdiff
path: root/sys/dev/isa/if_ai.c
blob: e8352ae7e36326e164b052ca0701f499354066f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*	$NetBSD: if_ai.c,v 1.36 2022/07/12 02:03:57 thorpej Exp $	*/

/*-
 * Copyright (c) 1998 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Rafal K. Boni.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_ai.c,v 1.36 2022/07/12 02:03:57 thorpej Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/errno.h>
#include <sys/device.h>
#include <sys/protosw.h>
#include <sys/socket.h>

#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/if_media.h>
#include <net/if_ether.h>

#include <sys/cpu.h>
#include <sys/bus.h>
#include <sys/intr.h>

#include <dev/isa/isareg.h>
#include <dev/isa/isavar.h>

#include <dev/ic/i82586reg.h>
#include <dev/ic/i82586var.h>
#include <dev/isa/if_aireg.h>

#ifdef AI_DEBUG
#define DPRINTF(x)	printf x
#else
#define DPRINTF(x)
#endif

struct ai_softc {
	struct ie_softc sc_ie;

	bus_space_tag_t sc_regt;	/* space tag for registers */
	bus_space_handle_t sc_regh;	/* space handle for registers */

	uint8_t	card_rev;
	uint8_t	card_type;

	void		*sc_ih;		/* interrupt handle */
};

static const char *ai_names[] = {
        "StarLAN 10",
        "EN100",
        "StarLAN Fiber",
};

/* Functions required by the i82586 MI driver */
static void 	ai_reset(struct ie_softc *, int);
static void 	ai_atten(struct ie_softc *, int);

static void	ai_copyin(struct ie_softc *, void *, int, size_t);
static void	ai_copyout(struct ie_softc *, const void *, int, size_t);

static uint16_t ai_read_16(struct ie_softc *, int);
static void	ai_write_16(struct ie_softc *, int, uint16_t);
static void	ai_write_24(struct ie_softc *, int, int);

/* Local support functions */
static int 	check_ie_present(struct ie_softc*, bus_space_tag_t,
					bus_space_handle_t, bus_size_t);
static int	ai_find_mem_size(struct ai_softc*, bus_space_tag_t,
					bus_size_t);

static int	ai_match(device_t, cfdata_t, void *);
static void	ai_attach(device_t, device_t, void *);

/*
 * AT&T StarLan support routines
 */
static void
ai_reset(struct ie_softc *sc, int why)
{
	struct ai_softc *asc = (struct ai_softc *)sc;

	switch (why) {
	case CHIP_PROBE:
		/* Reset to chip to see if it responds */
		bus_space_write_1(asc->sc_regt, asc->sc_regh, AI_RESET, 0);
		DELAY(100);
		break;

	case CARD_RESET:
		/*
		 * This takes around 10sec, and we can get
		 * by quite well w/out it...
		 */
		break;
	}
}

static void
ai_atten(struct ie_softc *sc, int why)
{
	struct ai_softc *asc = (struct ai_softc *)sc;

	bus_space_write_1(asc->sc_regt, asc->sc_regh, AI_ATTN, 0);
}

static void
ai_copyin(struct ie_softc *sc, void *dst, int offset, size_t size)
{
	int dribble;
	uint8_t *bptr = dst;

	if (offset % 2) {
		*bptr = bus_space_read_1(sc->bt, sc->bh, offset);
		offset++; bptr++; size--;
	}

	dribble = size % 2;
	bus_space_read_region_2(sc->bt, sc->bh, offset, (uint16_t *)bptr,
	    size >> 1);

	if (dribble) {
		bptr += size - 1;
		offset += size - 1;
		*bptr = bus_space_read_1(sc->bt, sc->bh, offset);
	}
}

static void
ai_copyout(struct ie_softc *sc, const void *src, int offset, size_t size)
{
	int dribble;
	const uint8_t *bptr = src;

	if (offset % 2) {
		bus_space_write_1(sc->bt, sc->bh, offset, *bptr);
		offset++; bptr++; size--;
	}

	dribble = size % 2;
	bus_space_write_region_2(sc->bt, sc->bh, offset,
	    (const uint16_t *)bptr, size >> 1);
	if (dribble) {
		bptr += size - 1;
		offset += size - 1;
		bus_space_write_1(sc->bt, sc->bh, offset, *bptr);
	}
}

static uint16_t
ai_read_16(struct ie_softc *sc, int offset)
{

        return bus_space_read_2(sc->bt, sc->bh, offset);
}

static void
ai_write_16(struct ie_softc *sc, int offset, uint16_t value)
{

        bus_space_write_2(sc->bt, sc->bh, offset, value);
}

static void
ai_write_24(struct ie_softc *sc, int offset, int addr)
{

        bus_space_write_4(sc->bt, sc->bh, offset, addr +
	    (u_long)sc->sc_maddr - (u_long)sc->sc_iobase);
}

int
ai_match(device_t parent, cfdata_t cf, void *aux)
{
	int rv = 0;
	uint8_t val, type;
	bus_size_t memsize;
	bus_space_tag_t iot;
	bus_space_handle_t ioh;
	struct isa_attach_args * const ia = aux;
	struct ai_softc asc;

	if (ia->ia_nio < 1)
		return 0;
	if (ia->ia_niomem < 1)
		return 0;
	if (ia->ia_nirq < 1)
		return 0;

	if (ISA_DIRECT_CONFIG(ia))
		return 0;

	/* Punt if wildcarded port, IRQ or memory address */
	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT ||
	    ia->ia_iomem[0].ir_addr == ISA_UNKNOWN_IOMEM ||
	    ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ) {
		DPRINTF((
		 "ai_match: wildcarded IRQ, IOAddr, or memAddr, skipping\n"));
		return 0;
	}

	iot = ia->ia_iot;

	/*
	 * This probe is horribly bad, but I have no info on this card other
	 * than the former driver, and it was just as bad!
	 */
	if (bus_space_map(iot, ia->ia_io[0].ir_addr,
			  AI_IOSIZE, 0, &ioh) != 0) {

		DPRINTF(("ai_match: cannot map %d IO ports @ 0x%x\n",
			 AI_IOSIZE, ia->ia_io[0].ir_addr));
		return 0;
	}

	val = bus_space_read_1(iot, ioh, AI_REVISION);

	type = SL_BOARD(val);
	if (type != SL10_BOARD && type != EN100_BOARD &&
	    type != SLFIBER_BOARD) {
		DPRINTF(("ai_match: unknown board code 0x%02x @ 0x%x\n",
			 type, ia->ia_io[0].ir_addr));
		goto out;
	}

	/*
	 * Fill in just about enough of our local `ai_softc' for
	 * ai_find_mem_size() to do its job.
	 */
	memset(&asc, 0, sizeof asc);
	asc.sc_regt = iot;
	asc.sc_regh = ioh;

	if ((memsize = ai_find_mem_size(&asc, ia->ia_memt,
	     ia->ia_iomem[0].ir_addr)) == 0) {
		DPRINTF(("ai_match: cannot size memory of board @ 0x%x\n",
			 ia->ia_io[0].ir_addr));
		goto out;
	}

	if (ia->ia_iomem[0].ir_size != 0 &&
	    ia->ia_iomem[0].ir_size != memsize) {
		DPRINTF((
		   "ai_match: memsize of board @ 0x%x doesn't match config\n",
		   ia->ia_io[0].ir_addr));
		goto out;
	}

	rv = 1;

	ia->ia_nio = 1;
	ia->ia_io[0].ir_size = AI_IOSIZE;

	ia->ia_niomem = 1;
	ia->ia_iomem[0].ir_size = memsize;

	ia->ia_nirq = 1;

	ia->ia_ndrq = 0;

	DPRINTF(("ai_match: found board @ 0x%x\n", ia->ia_io[0].ir_addr));

out:
	bus_space_unmap(iot, ioh, AI_IOSIZE);
	return rv;
}

void
ai_attach(device_t parent, device_t self, void *aux)
{
	struct ai_softc *asc = device_private(self);
	struct ie_softc *sc = &asc->sc_ie;
	struct isa_attach_args *ia = aux;
	uint8_t val = 0;
	bus_space_handle_t ioh, memh;
	uint8_t ethaddr[ETHER_ADDR_LEN];
	char name[80];

	sc->sc_dev = self;

	if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr,
			  ia->ia_io[0].ir_size, 0, &ioh) != 0) {
		DPRINTF(("\n%s: can't map i/o space 0x%x-0x%x\n",
			 device_xname(self),
		         ia->ia_io[0].ir_addr, ia->ia_io[0].ir_addr +
		         ia->ia_io[0].ir_size - 1));
		return;
	}

	if (bus_space_map(ia->ia_memt, ia->ia_iomem[0].ir_addr,
			  ia->ia_iomem[0].ir_size, 0, &memh) != 0) {
		DPRINTF(("\n%s: can't map iomem space 0x%x-0x%x\n",
			 device_xname(self),
			 ia->ia_iomem[0].ir_addr, ia->ia_iomem[0].ir_addr +
			 ia->ia_iomem[0].ir_size - 1));
		bus_space_unmap(ia->ia_iot, ioh, ia->ia_io[0].ir_size);
		return;
	}

	asc->sc_regt = ia->ia_iot;
	asc->sc_regh = ioh;

	sc->hwinit = NULL;
	sc->intrhook = NULL;
	sc->hwreset = ai_reset;
	sc->chan_attn = ai_atten;

	sc->ie_bus_barrier = NULL;

	sc->memcopyin = ai_copyin;
	sc->memcopyout = ai_copyout;
	sc->ie_bus_read16 = ai_read_16;
	sc->ie_bus_write16 = ai_write_16;
	sc->ie_bus_write24 = ai_write_24;

	sc->do_xmitnopchain = 0;

	sc->sc_mediachange = NULL;
	sc->sc_mediastatus = NULL;

	sc->bt = ia->ia_memt;
	sc->bh = memh;

	/* Map i/o space. */
	sc->sc_msize = ia->ia_iomem[0].ir_size;
	sc->sc_maddr = (void *)memh;
	sc->sc_iobase = (char *)sc->sc_maddr + sc->sc_msize - (1 << 24);

	/* Set up pointers to important on-card control structures */
	sc->iscp = 0;
	sc->scb = IE_ISCP_SZ;
	sc->scp = sc->sc_msize + IE_SCP_ADDR - (1 << 24);

	sc->buf_area = sc->scb + IE_SCB_SZ;
	sc->buf_area_sz = sc->sc_msize - IE_ISCP_SZ - IE_SCB_SZ - IE_SCP_SZ;

	/* Zero card memory */
	bus_space_set_region_1(sc->bt, sc->bh, 0, 0, sc->sc_msize);

	/* Set card to 16-bit bus mode */
	bus_space_write_1(sc->bt, sc->bh, IE_SCP_BUS_USE((u_long)sc->scp),
	    IE_SYSBUS_16BIT);

	/* Set up pointers to key structures */
	ai_write_24(sc, IE_SCP_ISCP((u_long)sc->scp), (u_long)sc->iscp);
	ai_write_16(sc, IE_ISCP_SCB((u_long)sc->iscp), (u_long)sc->scb);
	ai_write_24(sc, IE_ISCP_BASE((u_long)sc->iscp), (u_long)sc->iscp);

	/* Flush setup of pointers, check if chip answers */
	if (!i82586_proberam(sc)) {
		DPRINTF(("\n%s: can't talk to i82586!\n",
			device_xname(self)));
		bus_space_unmap(ia->ia_iot, ioh, ia->ia_io[0].ir_size);
		bus_space_unmap(ia->ia_memt, memh, ia->ia_iomem[0].ir_size);
		return;
	}

	val = bus_space_read_1(asc->sc_regt, asc->sc_regh, AI_REVISION);
	asc->card_rev = SL_REV(val);
	asc->card_type = SL_BOARD(val) - 1;
	snprintf(name, sizeof(name), "%s, rev. %d",
	    ai_names[asc->card_type], asc->card_rev);

	i82586_attach(sc, name, ethaddr, NULL, 0, 0);

	asc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
	    IST_EDGE, IPL_NET, i82586_intr, sc);
	if (asc->sc_ih == NULL) {
		DPRINTF(("\n%s: can't establish interrupt\n",
			device_xname(self)));
	}
}

/*
 * Divine the memory size of this board.
 * Better hope there's nothing important hiding just below the card...
 */
static int
ai_find_mem_size(struct ai_softc* asc, bus_space_tag_t memt, bus_size_t maddr)
{
	int size;
	bus_space_handle_t memh;
	struct ie_softc *sc = &asc->sc_ie;

	for (size = 65536; size >= 16384; size -= 16384) {
		if (bus_space_map(memt, maddr, size, 0, &memh) == 0) {
			size = check_ie_present(sc, memt, maddr, size);
			bus_space_unmap(memt, memh, size);

			if (size != 0)
				return size;
		}
	}

	return 0;
}

/*
 * Check to see if there's an 82586 out there.
 */
static int
check_ie_present(struct ie_softc* sc, bus_space_tag_t memt,
    bus_space_handle_t memh, bus_size_t size)
{

	sc->hwreset = ai_reset;
	sc->chan_attn = ai_atten;
	sc->ie_bus_read16 = ai_read_16;
	sc->ie_bus_write16 = ai_write_16;

	sc->bt = memt;
	sc->bh = memh;
	sc->sc_iobase = (char *)memh + size - (1 << 24);

	sc->scp = size + IE_SCP_ADDR - (1 << 24);
	bus_space_set_region_1(memt, memh, (u_long)sc->scp, 0, IE_SCP_SZ);

	sc->iscp = 0;
	bus_space_set_region_1(memt, memh, (u_long)sc->iscp, 0, IE_ISCP_SZ);

	sc->scb = IE_ISCP_SZ;
	bus_space_set_region_1(memt, memh, sc->scb, 0, IE_SCB_SZ);

	/* Set card to 16-bit bus mode */
	bus_space_write_1(sc->bt, sc->bh, IE_SCP_BUS_USE((u_long)sc->scp),
	    IE_SYSBUS_16BIT);

	/* Set up pointers to key structures */
	ai_write_24(sc, IE_SCP_ISCP((u_long)sc->scp), (u_long)sc->iscp);
	ai_write_16(sc, IE_ISCP_SCB((u_long)sc->iscp), (u_long)sc->scb);
	ai_write_24(sc, IE_ISCP_BASE((u_long)sc->iscp), (u_long)sc->iscp);

	/* Flush setup of pointers, check if chip answers */
	if (!i82586_proberam(sc))
		return 0;

	return size;
}

CFATTACH_DECL_NEW(ai, sizeof(struct ai_softc),
    ai_match, ai_attach, NULL, NULL);