summaryrefslogtreecommitdiff
path: root/sys/arch/atari/vme/if_we_vme.c
blob: a945728c0696ed643c40896a426a055fe74d9498 (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
/*	$NetBSD: if_we_vme.c,v 1.4 2014/10/18 08:33:25 snj Exp $	*/

/*-
 * Copyright (c) 1997, 1998, 2010 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
 * NASA Ames Research Center.
 *
 * 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.
 */

/*
 * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
 * adapters.
 *
 * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
 *
 * Copyright (C) 1993, David Greenman.  This software may be used, modified,
 * copied, distributed, and sold, in both source and binary form provided that
 * the above copyright and these terms are retained.  Under no circumstances is
 * the author responsible for the proper functioning of this software, nor does
 * the author assume any responsibility for damages incurred with its use.
 */

/*
 * Device driver for the SMC Elite Ultra (8216) with SMC_TT VME-ISA bridge.
 * Based on:
 *	NetBSD: if_we_isa.c,v 1.20 2008/04/28 20:23:52 martin Exp
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_we_vme.c,v 1.4 2014/10/18 08:33:25 snj Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/socket.h>
#include <sys/mbuf.h>
#include <sys/syslog.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/bus.h>
#include <sys/intr.h>

#include <machine/cpu.h>
#include <machine/iomap.h>
#include <machine/scu.h>

#include <atari/vme/vmevar.h>

#include <dev/ic/dp8390reg.h>
#include <dev/ic/dp8390var.h>
#include <dev/ic/wereg.h>
#include <dev/ic/wevar.h>

/* #define WE_DEBUG */
#ifdef WE_DEBUG
#define DPRINTF(x)	printf x
#else
#define DPRINTF(x)	/**/
#endif

/* VME space mapped by SMC_TT VME-ISA bridge */
#define SMCTT_MEM_BASE	0xFE000000	/* base for shared memory space */
#define SMCTT_IOE_BASE	0xFE200000	/* base for I/O ports at even address */
#define SMCTT_IOO_BASE	0xFE300000	/* base for I/O ports at odd address */

#define SMCTT_IO_OFFSET	(SMCTT_IOO_BASE - SMCTT_IOE_BASE)

/* default SMC8216 settings for SMC_TT specified by a jumper switch at No.2 */
#define SMCTT_MEM_ADDR	0xD0000
#define SMCTT_IO_ADDR	0x280

/* SMC_TT uses IRQ4 on VME, IRQ3 on ISA, and interrupt vector 0xAA */
#define SMCTT_VME_IRQ	4
#define SMCTT_ISA_IRQ	3
#define SMCTT_VECTOR	0xAA

static int we_vme_probe(device_t, cfdata_t , void *);
static void we_vme_attach(device_t, device_t, void *);

static uint8_t smctt_bus_space_read_1(bus_space_tag_t, bus_space_handle_t,
    bus_size_t);
static void    smctt_bus_space_write_1(bus_space_tag_t, bus_space_handle_t,
    bus_size_t, uint8_t);
static int     smctt_bus_space_peek_1(bus_space_tag_t, bus_space_handle_t,
    bus_size_t);

struct we_vme_softc {
	struct we_softc	sc_we;
	struct atari_bus_space sc_bs;
};

CFATTACH_DECL_NEW(we_vme, sizeof(struct we_vme_softc),
    we_vme_probe, we_vme_attach, NULL, NULL);

static const int we_790_irq[] = {
	-1, 9, 3, 5, 7, 10, 11, 15,
};

static int
we_vme_probe(device_t parent, cfdata_t cf, void *aux)
{
	struct vme_attach_args *va = aux;
	struct atari_bus_space t;
	bus_space_tag_t asict, memt;
	bus_space_handle_t asich, asich1, memh;
	bus_size_t memsize = 0;
	bool asich_valid, asich1_valid, memh_valid;
	int i, rv;
	uint8_t sum, reg, type, hwr;

	rv = 0;
	asich_valid = false;
	asich1_valid = false;
	memh_valid = false;

	if (va->va_iobase != IOBASEUNK &&
	    va->va_iobase != SMCTT_IOE_BASE + SMCTT_IO_ADDR)
		return 0;
	if (va->va_maddr != IOBASEUNK &&
	    va->va_maddr != SMCTT_MEM_BASE + SMCTT_MEM_ADDR)
		return 0;
	if (va->va_irq != IRQUNK &&
	    va->va_irq != SMCTT_VME_IRQ)
		return 0;

	/* SMC_TT has a bit weird I/O address mappings */
	asict = beb_alloc_bus_space_tag(&t);
	/* XXX setup only simple byte functions used in MI we(4) driver */
	asict->abs_r_1 = smctt_bus_space_read_1;
	asict->abs_w_1 = smctt_bus_space_write_1;
	asict->abs_p_1 = smctt_bus_space_peek_1;

	/*
	 * Only 16 bit accesses are allowed for memory space on SMC_TT,
	 * but MI we(4) uses them on 16 bit mode.
	 */
	memt = va->va_memt;

	/* Attempt to map the device. */
	if (bus_space_map(asict, SMCTT_IOE_BASE + SMCTT_IO_ADDR, WE_NPORTS,
	    0, &asich) != 0) {
		DPRINTF(("%s: failed to map even I/O space", __func__));
		goto out;
	}
	asich_valid = true;

	if (bus_space_map(asict, SMCTT_IOO_BASE + SMCTT_IO_ADDR, WE_NPORTS,
	    0, &asich1) != 0) {
		DPRINTF(("%s: failed to map odd I/O space", __func__));
		goto out;
	}
	asich1_valid = true;

	/* XXX abuse stride for offset of odd ports from even ones */
	asict->stride =
	    (vaddr_t)bus_space_vaddr(asict, asich1) -
	    (vaddr_t)bus_space_vaddr(asict, asich);

	/* check if register regions are valid */
	if (bus_space_peek_1(asict, asich, WE_PROM + 0) == 0 ||
	    bus_space_peek_1(asict, asich, WE_PROM + 1) == 0)
		goto out;

	/*
	 * Attempt to do a checksum over the station address PROM.
	 * If it fails, it's probably not an SMC_TT board.
	 */
	DPRINTF(("%s: WE_PROM: ", __func__));
	sum = 0;
	for (i = 0; i < 8; i++) {
		reg = bus_space_read_1(asict, asich, WE_PROM + i);
		DPRINTF(("%02x ", reg));
		sum += reg;
	}
	DPRINTF(("\n"));
	DPRINTF(("%s: WE_ROM_SUM: 0x%02x\n", __func__, sum));

	if (sum != WE_ROM_CHECKSUM_TOTAL)
		goto out;

	/*
	 * Reset the card to force it into a known state.
	 */
	bus_space_write_1(asict, asich, WE_MSR, WE_MSR_RST);
	delay(100);

	bus_space_write_1(asict, asich, WE_MSR,
	    bus_space_read_1(asict, asich, WE_MSR) & ~WE_MSR_RST);

	/* Wait in case the card is reading its EEPROM. */
	delay(5000);

	/*
	 * Check card type.
	 */
	type = bus_space_read_1(asict, asich, WE_CARD_ID);
	/* Assume SMT_TT has only 8216 */
	if (type != WE_TYPE_SMC8216C && type != WE_TYPE_SMC8216T)
		goto out;

	hwr = bus_space_read_1(asict, asich, WE790_HWR);
	bus_space_write_1(asict, asich, WE790_HWR, hwr | WE790_HWR_SWH);
	switch (bus_space_read_1(asict, asich, WE790_RAR) & WE790_RAR_SZ64) {
	case WE790_RAR_SZ64:
		memsize = 65536;
		break;
	case WE790_RAR_SZ32:
		memsize = 32768;
		break;
	case WE790_RAR_SZ16:
		memsize = 16384;
		break;
	case WE790_RAR_SZ8:
		memsize = 8192;
		break;
	default:
		memsize = 16384;
		break;
	}
	bus_space_write_1(asict, asich, WE790_HWR, hwr);

	/* Attempt to map the memory space. */
	if (bus_space_map(memt, SMCTT_MEM_BASE + SMCTT_MEM_ADDR, memsize,
	    0, &memh) != 0) {
		DPRINTF(("%s: failed to map shared memory", __func__));
		goto out;
	}
	memh_valid = true;

	/* check if memory region is valid */
	if (bus_space_peek_2(memt, memh, 0) == 0)
		goto out;

	/*
	 * Check the assigned interrupt number from the card.
	 */

	/* Assemble together the encoded interrupt number. */
	hwr = bus_space_read_1(asict, asich, WE790_HWR);
	bus_space_write_1(asict, asich, WE790_HWR, hwr | WE790_HWR_SWH);

	reg = bus_space_read_1(asict, asich, WE790_GCR);
	i = ((reg & WE790_GCR_IR2) >> 4) |
	    ((reg & (WE790_GCR_IR1|WE790_GCR_IR0)) >> 2);
	bus_space_write_1(asict, asich, WE790_HWR, hwr & ~WE790_HWR_SWH);

	if (we_790_irq[i] != SMCTT_ISA_IRQ) {
		DPRINTF(("%s: wrong IRQ (%d); check jumper settings\n",
		    __func__, we_790_irq[i]));
		goto out;
	}

	/* So, we say we've found it! */
	va->va_iobase = SMCTT_IOE_BASE + SMCTT_IO_ADDR;
	va->va_iosize = WE_NPORTS;
	va->va_maddr = SMCTT_MEM_BASE + SMCTT_MEM_ADDR;
	va->va_msize = memsize;
	va->va_irq = SMCTT_VME_IRQ;

	rv = 1;

 out:
	if (asich_valid)
		bus_space_unmap(asict, asich, WE_NPORTS);
	if (asich1_valid)
		bus_space_unmap(asict, asich1, WE_NPORTS);
	if (memh_valid)
		bus_space_unmap(memt, memh, memsize);
	return rv;
}

void
we_vme_attach(device_t parent, device_t self, void *aux)
{
	struct we_vme_softc *wvsc = device_private(self);
	struct we_softc *wsc = &wvsc->sc_we;
	struct dp8390_softc *sc = &wsc->sc_dp8390;
	struct vme_attach_args *va = aux;
	bus_space_tag_t nict, asict, memt;
	bus_space_handle_t nich, asich, asich1, memh;
	const char *typestr;

	aprint_normal("\n");

	sc->sc_dev = self;

	/* See comments in the above probe function */
	asict = beb_alloc_bus_space_tag(&wvsc->sc_bs);
	asict->abs_r_1 = smctt_bus_space_read_1;
	asict->abs_w_1 = smctt_bus_space_write_1;
	nict = asict;

	memt = va->va_memt;

	/* Map the device. */
	if (bus_space_map(asict, va->va_iobase, WE_NPORTS, 0, &asich) != 0) {
		aprint_error_dev(self, "can't map even I/O space\n");
		return;
	}
	if (bus_space_map(asict, va->va_iobase + SMCTT_IO_OFFSET, WE_NPORTS,
	    0, &asich1) != 0) {
		aprint_error_dev(self, "can't map odd I/O space\n");
		goto out;
	}
	asict->stride =
	    (vaddr_t)bus_space_vaddr(asict, asich1) -
	    (vaddr_t)bus_space_vaddr(asict, asich);

	if (bus_space_subregion(asict, asich, WE_NIC_OFFSET, WE_NIC_NPORTS,
	    &nich) != 0) {
		aprint_error_dev(self, "can't subregion I/O space\n");
		goto out1;
	}

	/* Map memory space. */
	if (bus_space_map(memt, va->va_maddr, va->va_msize, 0, &memh) != 0) {
		aprint_error_dev(self, "can't map shared memory\n");
		goto out1;
	}

	wsc->sc_asict = asict;
	wsc->sc_asich = asich;

	sc->sc_regt = nict;
	sc->sc_regh = nich;

	sc->sc_buft = memt;
	sc->sc_bufh = memh;

	wsc->sc_maddr = va->va_maddr & 0xfffff;
	sc->mem_size = va->va_msize;

	/* Interface is always enabled. */
	sc->sc_enabled = 1;

	/* SMC_TT assumes SMC8216 */
	sc->is790 = 1;

	/* SMC_TT supports only 16 bit access for shared memory */
	wsc->sc_flags |= WE_16BIT_ENABLE;

	/* Appeal the Atari spirit :-) */
	typestr = "SMC8216 with SMC_TT VME-ISA bridge";

	if (we_config(self, wsc, typestr) != 0)
		goto out2;

	/*
	 * Enable the configured interrupt.
	 */
	bus_space_write_1(asict, asich, WE790_ICR,
	    bus_space_read_1(asict, asich, WE790_ICR) | WE790_ICR_EIL);

	/* Establish interrupt handler. */
	wsc->sc_ih = intr_establish(SMCTT_VECTOR - 64, USER_VEC, 0,
	    (hw_ifun_t)dp8390_intr, sc);
	if (wsc->sc_ih == NULL) {
		aprint_error_dev(self, "can't establish interrupt\n");
		goto out2;
	}
	/*
	 * Unmask the VME interrupt we're on.
	 */
	if ((machineid & ATARI_TT) != 0)
		SCU->vme_mask |= 1 << va->va_irq;

	return;

 out2:
		bus_space_unmap(memt, memh, va->va_msize);
 out1:
		bus_space_unmap(asict, asich1, WE_NPORTS);
 out:
		bus_space_unmap(asict, asich, WE_NPORTS);
}

static uint8_t
smctt_bus_space_read_1(bus_space_tag_t bt, bus_space_handle_t bh,
    bus_size_t reg)
{
	uint8_t rv;

	if ((reg & 0x01) != 0) {
		/* odd address space */
		rv = *(volatile uint8_t *)(bh + bt->stride + (reg & ~0x01));
	} else {
		/* even address space */
		rv = *(volatile uint8_t *)(bh + reg);
	}

	return rv;
}

static void
smctt_bus_space_write_1(bus_space_tag_t bt, bus_space_handle_t bh,
    bus_size_t reg, uint8_t val)
{

	if ((reg & 0x01) != 0) {
		/* odd address space */
		*(volatile uint8_t *)(bh + bt->stride + (reg & ~0x01)) = val;
	} else {
		/* even address space */
		*(volatile uint8_t *)(bh + reg) = val;
	}
}

static int
smctt_bus_space_peek_1(bus_space_tag_t bt, bus_space_handle_t bh,
    bus_size_t reg)
{
	uint8_t *va;

	if ((reg & 0x01) != 0) {
		/* odd address space */
		va = (uint8_t *)(bh + bt->stride + (reg & ~0x01));
	} else {
		/* even address space */
		va = (uint8_t *)(bh + reg);
	}

	return !badbaddr(va, sizeof(uint8_t));
}