Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/*	$NetBSD: if_ne_mb.c,v 1.2 2010/04/06 15:32:36 tsutsui Exp $	*/

/*
 * Copyright (c) 2010 Izumi Tsutsui.  All rights reserved.
 *
 * 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 AUTHOR ``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 AUTHOR 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 the EtherNEC,
 * NE2000 in 8bit mode over Atari ROM cartridge slot.
 * http://hardware.atari.org/ether/
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_ne_mb.c,v 1.2 2010/04/06 15:32:36 tsutsui Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/callout.h>

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

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

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

#include <atari/atari/device.h>

#include <dev/ic/dp8390reg.h>
#include <dev/ic/dp8390var.h>
#include <dev/ic/ne2000reg.h>
#include <dev/ic/ne2000var.h>

/*
 * EtherNEC specific address configuration
 */

/* I/O read ops are through /ROM4 area (0xFA0000) */
#define AD_CART_ROM4		(AD_CART + 0x00000)
#define ETHERNEC_READ_PORT	AD_CART_ROM4

/* I/O write ops are through /ROM3 area (0xFB0000) */
#define AD_CART_ROM3		(AD_CART + 0x10000)
#define ETHERNEC_WRITE_PORT	AD_CART_ROM3

/* CPU address lines A13-A9 are connected to ISA A4-A0 */
#define ETHERNEC_PORT_STRIDE	9

/* Using A8-A1 lines to specify write data (no A0 but UDS/LDS on m68k) */
#define ETHERNEC_WR_ADDR_SHIFT	1

/* interrupt polling per HZ */
#define ETHERNEC_TICK		1

static int  ne_mb_probe(device_t, cfdata_t, void *);
static void ne_mb_attach(device_t, device_t, void *);

static void ne_mb_poll(void *);

static bus_space_tag_t ethernec_init_bus_space_tag(bus_space_tag_t);
static void    ethernec_bus_space_unimpl(void);

static int     ethernec_bus_space_peek_1(bus_space_tag_t, bus_space_handle_t,
    bus_size_t);
static uint8_t ethernec_bus_space_read_1(bus_space_tag_t, bus_space_handle_t,
    bus_size_t);
static void    ethernec_bus_space_write_1(bus_space_tag_t, bus_space_handle_t,
    bus_size_t, uint8_t);
static void    ethernec_bus_space_read_multi_1(bus_space_tag_t,
    bus_space_handle_t, bus_size_t, uint8_t *, bus_size_t);
static void    ethernec_bus_space_read_multi_2(bus_space_tag_t,
    bus_space_handle_t, bus_size_t, uint16_t *, bus_size_t);
static void    ethernec_bus_space_write_multi_1(bus_space_tag_t,
    bus_space_handle_t, bus_size_t, const uint8_t *, bus_size_t);
static void    ethernec_bus_space_write_multi_2(bus_space_tag_t,
    bus_space_handle_t, bus_size_t, const uint16_t *, bus_size_t);

struct ne_mb_softc {
	struct ne2000_softc sc_ne2000;		/* MI ne2000 softc */

	struct atari_bus_space sc_bs;
	struct callout sc_poll;
};

CFATTACH_DECL_NEW(ne_mb, sizeof(struct ne_mb_softc),
    ne_mb_probe, ne_mb_attach, NULL, NULL);

static int
ne_mb_probe(device_t parent, cfdata_t cf, void *aux)
{
	static bool ne_matched = false;
	struct atari_bus_space bs;
	bus_space_tag_t iot, asict;
	bus_space_handle_t ioh, iowh, asich;
	int netype, rv;

	rv = 0;

	if (!atari_realconfig)
		goto out;
	if (strcmp("ne", aux) || ne_matched)
		goto out;

	iot = ethernec_init_bus_space_tag(&bs);

	/* map I/O space for read ops */
	if (bus_space_map(iot, ETHERNEC_READ_PORT,
	    NE2000_NPORTS << ETHERNEC_PORT_STRIDE, 0, &ioh) != 0)
		goto out;

	/* map I/O space for write ops */
	if (bus_space_map(iot, ETHERNEC_WRITE_PORT,
	    NE2000_NPORTS << ETHERNEC_PORT_STRIDE, 0, &iowh) != 0)
		goto out1;

	/* XXX abuse stride for offset of write ports from read ones */
	iot->stride =
	    (vaddr_t)bus_space_vaddr(iot, iowh) -
	    (vaddr_t)bus_space_vaddr(iot, ioh);

	/* check if register regions are vaild */
	if (bus_space_peek_1(iot, ioh, 0) == 0)
		goto out2;

	asict = iot;
	if (bus_space_subregion(iot, ioh,
	    NE2000_ASIC_OFFSET << ETHERNEC_PORT_STRIDE,
	    NE2000_ASIC_NPORTS << ETHERNEC_PORT_STRIDE, &asich))
		goto out2;

	/* Look for an NE2000 compatible card */
	netype = ne2000_detect(iot, ioh, asict, asich);
	switch (netype) {
	/* XXX should we reject non RTL8019 variants? */
	case NE2000_TYPE_NE1000:
	case NE2000_TYPE_NE2000:
	case NE2000_TYPE_RTL8019:
		ne_matched = true;
		rv = 1;
		break;
	default:
		break;
	}

 out2:
	bus_space_unmap(iot, iowh, NE2000_NPORTS << ETHERNEC_PORT_STRIDE);
 out1:
	bus_space_unmap(iot, ioh, NE2000_NPORTS << ETHERNEC_PORT_STRIDE);
 out:
	return rv;
}

static void
ne_mb_attach(device_t parent, device_t self, void *aux)
{
	struct ne_mb_softc *sc = device_private(self);
	struct ne2000_softc *nsc = &sc->sc_ne2000;
	struct dp8390_softc *dsc = &nsc->sc_dp8390;
	bus_space_tag_t iot, asict;
	bus_space_handle_t ioh, iowh, asich;
	const char *typestr;
	int netype;

	dsc->sc_dev = self;
	aprint_normal(": EtherNEC on Atari ROM cartridge slot\n");

	iot = ethernec_init_bus_space_tag(&sc->sc_bs);

	/* map I/O space for read ops */
	if (bus_space_map(iot, ETHERNEC_READ_PORT,
	    NE2000_NPORTS << ETHERNEC_PORT_STRIDE, 0, &ioh) != 0)
		goto out;

	/* map I/O space for write ops */
	if (bus_space_map(iot, ETHERNEC_WRITE_PORT,
	    NE2000_NPORTS << ETHERNEC_PORT_STRIDE, 0, &iowh) != 0)
		goto out1;

	/* XXX abuse stride */
	iot->stride =
	    (vaddr_t)bus_space_vaddr(iot, iowh) -
	    (vaddr_t)bus_space_vaddr(iot, ioh);

	asict = iot;
	if (bus_space_subregion(iot, ioh,
	    NE2000_ASIC_OFFSET << ETHERNEC_PORT_STRIDE,
	    NE2000_ASIC_NPORTS << ETHERNEC_PORT_STRIDE, &asich))
		goto out2;

	dsc->sc_regt = iot;
	dsc->sc_regh = ioh;

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

	/* EtherNEC uses 8-bit data bus */
	nsc->sc_quirk = NE2000_QUIRK_8BIT;

	/*
	 * detect it again, so we can print some information about
	 * the interface.
	 * XXX: Should we accept only RTL8019?
	 */
	netype = ne2000_detect(iot, ioh, asict, asich);
	switch (netype) {
	case NE2000_TYPE_NE1000:
		typestr = "NE1000";
		break;

	case NE2000_TYPE_NE2000:
		typestr = "NE2000";
		break;

	case NE2000_TYPE_RTL8019:
		typestr = "NE2000 (RTL8019)";
		break;

	default:
		aprint_error_dev(self, "where did the card go?!\n");
		goto out2;
	}

	aprint_normal_dev(self, "%s Ethernet (8-bit)\n", typestr);

	/* this interface is always enabled */
	dsc->sc_enabled = 1;

	/* call MI ne2000 attach function */
	ne2000_attach(nsc, NULL);

	/* emulate interrupts by callout(9) */
	aprint_normal_dev(self, "using %d Hz polling\n", hz / ETHERNEC_TICK);
	callout_init(&sc->sc_poll, 0);
	callout_reset(&sc->sc_poll, ETHERNEC_TICK, ne_mb_poll, sc);

	return;

 out2:
	bus_space_unmap(iot, iowh, NE2000_NPORTS << ETHERNEC_PORT_STRIDE);
 out1:
	bus_space_unmap(iot, ioh, NE2000_NPORTS << ETHERNEC_PORT_STRIDE);
 out:
	return;
}

static void
ne_mb_poll(void *arg)
{
	struct ne_mb_softc *sc;
	struct ne2000_softc *nsc;
	struct dp8390_softc *dsc;
	int s;

	sc = arg;
	nsc = &sc->sc_ne2000;
	dsc = &nsc->sc_dp8390;

	s = splnet();
	(void)dp8390_intr(dsc);
	splx(s);

	callout_schedule(&sc->sc_poll, ETHERNEC_TICK);
}

/*
 * bus_space(9) functions for EtherNEC
 *
 *  XXX: should these belong to an independent cartridge slot bus?
 */
static bus_space_tag_t
ethernec_init_bus_space_tag(bus_space_tag_t en_t)
{

	if (en_t == NULL)
		return NULL;

	memset(en_t, 0, sizeof(*en_t));

	/* XXX: implement functions used by MI ne2000 and dp8390 only */
	en_t->abs_p_1   = ethernec_bus_space_peek_1;
	en_t->abs_p_2   = (void *)ethernec_bus_space_unimpl;
	en_t->abs_p_4   = (void *)ethernec_bus_space_unimpl;
	en_t->abs_p_8   = (void *)ethernec_bus_space_unimpl;
	en_t->abs_r_1   = ethernec_bus_space_read_1;
	en_t->abs_r_2   = (void *)ethernec_bus_space_unimpl;
	en_t->abs_r_4   = (void *)ethernec_bus_space_unimpl;
	en_t->abs_r_8   = (void *)ethernec_bus_space_unimpl;
	en_t->abs_rs_1  = ethernec_bus_space_read_1;
	en_t->abs_rs_2  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_rs_4  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_rs_8  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_rm_1  = ethernec_bus_space_read_multi_1;
	en_t->abs_rm_2  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_rm_4  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_rm_8  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_rms_1 = ethernec_bus_space_read_multi_1;
	en_t->abs_rms_2 = ethernec_bus_space_read_multi_2;	/* XXX dummy */
	en_t->abs_rms_4 = (void *)ethernec_bus_space_unimpl;
	en_t->abs_rms_8 = (void *)ethernec_bus_space_unimpl;
	en_t->abs_rr_1  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_rr_2  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_rr_4  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_rr_8  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_rrs_1 = (void *)ethernec_bus_space_unimpl;
	en_t->abs_rrs_2 = (void *)ethernec_bus_space_unimpl;
	en_t->abs_rrs_4 = (void *)ethernec_bus_space_unimpl;
	en_t->abs_rrs_8 = (void *)ethernec_bus_space_unimpl;
	en_t->abs_w_1   = ethernec_bus_space_write_1;
	en_t->abs_w_2   = (void *)ethernec_bus_space_unimpl;
	en_t->abs_w_4   = (void *)ethernec_bus_space_unimpl;
	en_t->abs_w_8   = (void *)ethernec_bus_space_unimpl;
	en_t->abs_ws_1  = ethernec_bus_space_write_1;
	en_t->abs_ws_2  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_ws_4  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_ws_8  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_wm_1  = ethernec_bus_space_write_multi_1;
	en_t->abs_wm_2  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_wm_4  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_wm_8  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_wms_1 = ethernec_bus_space_write_multi_1;
	en_t->abs_wms_2 = ethernec_bus_space_write_multi_2;	/* XXX dummy */
	en_t->abs_wms_4 = (void *)ethernec_bus_space_unimpl;
	en_t->abs_wms_8 = (void *)ethernec_bus_space_unimpl;
	en_t->abs_wr_1  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_wr_2  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_wr_4  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_wr_8  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_wrs_1 = (void *)ethernec_bus_space_unimpl;
	en_t->abs_wrs_2 = (void *)ethernec_bus_space_unimpl;
	en_t->abs_wrs_4 = (void *)ethernec_bus_space_unimpl;
	en_t->abs_wrs_8 = (void *)ethernec_bus_space_unimpl;
	en_t->abs_sm_1  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_sm_2  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_sm_4  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_sm_8  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_sr_1  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_sr_2  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_sr_4  = (void *)ethernec_bus_space_unimpl;
	en_t->abs_sr_8  = (void *)ethernec_bus_space_unimpl;

	return en_t;
}

static void
ethernec_bus_space_unimpl(void)
{

	panic("%s: unimplemented EtherNEC bus_space(9) function called",
	    __func__);
}

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

	va = (uint8_t *)(bh + (reg << ETHERNEC_PORT_STRIDE));

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

static uint8_t
ethernec_bus_space_read_1(bus_space_tag_t bt, bus_space_handle_t bh,
    bus_size_t reg)
{
	volatile uint8_t *ba;

	ba = (volatile uint8_t *)(bh + (reg << ETHERNEC_PORT_STRIDE));

	return *ba;
}

static void
ethernec_bus_space_write_1(bus_space_tag_t bt, bus_space_handle_t bh,
    bus_size_t reg, uint8_t val)
{
	volatile uint8_t *wport;

	/*
	 * Write ops are done by read against write region (ROM3) address.
	 * 8-bit write data is specified via lower address bits.
	 */
	wport = (volatile uint8_t *)(bh +
	    bt->stride + (reg << ETHERNEC_PORT_STRIDE));
	wport += (u_int)val << ETHERNEC_WR_ADDR_SHIFT;

	(void)*wport;
}

static void
ethernec_bus_space_read_multi_1(bus_space_tag_t bt, bus_space_handle_t bh,
    bus_size_t reg, uint8_t *a, bus_size_t c)
{
	volatile uint8_t *ba;

	ba = (volatile uint8_t *)(bh + (reg << ETHERNEC_PORT_STRIDE));
	for (; c != 0; c--)
		*a++ = *ba;
}

static void
ethernec_bus_space_read_multi_2(bus_space_tag_t bt, bus_space_handle_t bh,
    bus_size_t reg, uint16_t *a, bus_size_t c)
{

	/* XXX: dummy function for probe ops in ne2000_detect() */
}

static void
ethernec_bus_space_write_multi_1(bus_space_tag_t bt, bus_space_handle_t bh,
    bus_size_t reg, const uint8_t *a, bus_size_t c)
{
	volatile uint8_t *ba, *wport;
	u_int val;

	ba = (volatile uint8_t *)(bh +
	    bt->stride + (reg << ETHERNEC_PORT_STRIDE));

	for (; c != 0; c--) {
		val = *a++;
		wport = ba + (val << ETHERNEC_WR_ADDR_SHIFT);
		(void)*wport;
	}
}

static void
ethernec_bus_space_write_multi_2(bus_space_tag_t bt, bus_space_handle_t bh,
    bus_size_t reg, const uint16_t *a, bus_size_t c)
{

	/* XXX: dummy function for probe ops in ne2000_detect() */
}