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
/* $NetBSD: am79c930.c,v 1.16 2008/04/28 20:23:49 martin Exp $ */

/*-
 * Copyright (c) 1999 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Bill Sommerfeld
 *
 * 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.
 */

/*
 * Am79c930 chip driver.
 *
 * This is used by the awi driver to use the shared
 * memory attached to the 79c930 to communicate with the firmware running
 * in the 930's on-board 80188 core.
 *
 * The 79c930 can be mapped into just I/O space, or also have a
 * memory mapping; the mapping must be set up by the bus front-end
 * before am79c930_init is called.
 */

/*
 * operations:
 *
 * read_8, read_16, read_32, read_64, read_bytes
 * write_8, write_16, write_32, write_64, write_bytes
 * (two versions, depending on whether memory-space or i/o space is in use).
 *
 * interrupt E.C.
 * start isr
 * end isr
 */

#include <sys/cdefs.h>
#ifdef __NetBSD__
__KERNEL_RCSID(0, "$NetBSD: am79c930.c,v 1.16 2008/04/28 20:23:49 martin Exp $");
#endif
#ifdef __FreeBSD__
__FBSDID("$FreeBSD$");
#endif

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/endian.h>
#ifndef __FreeBSD__
#include <sys/device.h>
#endif

#include <sys/cpu.h>
#ifdef __FreeBSD__
#include <machine/bus_pio.h>
#include <machine/bus_memio.h>
#endif
#include <sys/bus.h>
#ifdef __NetBSD__
#include <sys/intr.h>
#endif

#ifdef __NetBSD__
#include <dev/ic/am79c930reg.h>
#include <dev/ic/am79c930var.h>
#endif
#ifdef __FreeBSD__
#include <dev/awi/am79c930reg.h>
#include <dev/awi/am79c930var.h>
#endif

#define AM930_DELAY(x) /*nothing*/

#ifndef __BUS_SPACE_HAS_STREAM_METHODS
#define bus_space_read_stream_2		bus_space_read_2
#define bus_space_read_stream_4		bus_space_read_4
#define bus_space_write_stream_2	bus_space_write_2
#define bus_space_write_stream_4 	bus_space_write_4
#endif /* __BUS_SPACE_HAS_STREAM_METHODS */

void am79c930_regdump(struct am79c930_softc *sc);

static void io_write_1(struct am79c930_softc *, u_int32_t, u_int8_t);
static void io_write_2(struct am79c930_softc *, u_int32_t, u_int16_t);
static void io_write_4(struct am79c930_softc *, u_int32_t, u_int32_t);
static void io_write_bytes(struct am79c930_softc *, u_int32_t, u_int8_t *, size_t);

static u_int8_t io_read_1(struct am79c930_softc *, u_int32_t);
static u_int16_t io_read_2(struct am79c930_softc *, u_int32_t);
static u_int32_t io_read_4(struct am79c930_softc *, u_int32_t);
static void io_read_bytes(struct am79c930_softc *, u_int32_t, u_int8_t *, size_t);

static void mem_write_1(struct am79c930_softc *, u_int32_t, u_int8_t);
static void mem_write_2(struct am79c930_softc *, u_int32_t, u_int16_t);
static void mem_write_4(struct am79c930_softc *, u_int32_t, u_int32_t);
static void mem_write_bytes(struct am79c930_softc *, u_int32_t, u_int8_t *, size_t);

static u_int8_t mem_read_1(struct am79c930_softc *, u_int32_t);
static u_int16_t mem_read_2(struct am79c930_softc *, u_int32_t);
static u_int32_t mem_read_4(struct am79c930_softc *, u_int32_t);
static void mem_read_bytes(struct am79c930_softc *, u_int32_t, u_int8_t *, size_t);

static struct am79c930_ops iospace_ops = {
	io_write_1,
	io_write_2,
	io_write_4,
	io_write_bytes,
	io_read_1,
	io_read_2,
	io_read_4,
	io_read_bytes
};

struct am79c930_ops memspace_ops = {
	mem_write_1,
	mem_write_2,
	mem_write_4,
	mem_write_bytes,
	mem_read_1,
	mem_read_2,
	mem_read_4,
	mem_read_bytes
};

static void
io_write_1( struct am79c930_softc *sc, u_int32_t off, u_int8_t val)
{
	AM930_DELAY(1);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
	    ((off>>8)& 0x7f));
	AM930_DELAY(1);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_LO, (off&0xff));
	AM930_DELAY(1);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA, val);
	AM930_DELAY(1);
}

static void
io_write_2(struct am79c930_softc *sc, u_int32_t off, u_int16_t val)
{
	AM930_DELAY(1);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
	    ((off>>8)& 0x7f));
	AM930_DELAY(1);
	bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_LMA_LO, (off&0xff));
	AM930_DELAY(1);
	bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_IODPA, val & 0xff);
	AM930_DELAY(1);
	bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_IODPA, (val>>8)&0xff);
	AM930_DELAY(1);
}

static void
io_write_4(struct am79c930_softc *sc, u_int32_t off, u_int32_t val)
{
	AM930_DELAY(1);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
	    ((off>>8)& 0x7f));
	AM930_DELAY(1);
	bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_LMA_LO, (off&0xff));
	AM930_DELAY(1);
	bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_IODPA,val & 0xff);
	AM930_DELAY(1);
	bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_IODPA,(val>>8)&0xff);
	AM930_DELAY(1);
	bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_IODPA,(val>>16)&0xff);
	AM930_DELAY(1);
	bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_IODPA,(val>>24)&0xff);
	AM930_DELAY(1);
}

static void
io_write_bytes(struct am79c930_softc *sc, u_int32_t off, u_int8_t *ptr,
    size_t len)
{
	int i;

	AM930_DELAY(1);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
	    ((off>>8)& 0x7f));
	AM930_DELAY(1);
	bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_LMA_LO, (off&0xff));
	AM930_DELAY(1);
	for (i=0; i<len; i++)
		bus_space_write_1(sc->sc_iot,sc->sc_ioh,AM79C930_IODPA,ptr[i]);
}

static u_int8_t
io_read_1(struct am79c930_softc *sc, u_int32_t off)
{
	u_int8_t val;

	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
	    ((off>>8)& 0x7f));
	AM930_DELAY(1);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_LO, (off&0xff));
	AM930_DELAY(1);
	val = bus_space_read_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA);
	AM930_DELAY(1);
	return val;
}

static u_int16_t
io_read_2(struct am79c930_softc *sc, u_int32_t off)
{
	u_int16_t val;

	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
	    ((off>>8)& 0x7f));
	AM930_DELAY(1);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_LO, (off&0xff));
	AM930_DELAY(1);
	val = bus_space_read_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA);
	AM930_DELAY(1);
	val |= bus_space_read_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA) << 8;
	AM930_DELAY(1);
	return val;
}

static u_int32_t
io_read_4(struct am79c930_softc *sc, u_int32_t off)
{
	u_int32_t val;

	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
	    ((off>>8)& 0x7f));
	AM930_DELAY(1);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_LO, (off&0xff));
	AM930_DELAY(1);
	val = bus_space_read_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA);
	AM930_DELAY(1);
	val |= bus_space_read_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA) << 8;
	AM930_DELAY(1);
	val |= bus_space_read_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA) << 16;
	AM930_DELAY(1);
	val |= bus_space_read_1(sc->sc_iot, sc->sc_ioh, AM79C930_IODPA) << 24;
	AM930_DELAY(1);
	return val;
}

static void
io_read_bytes(struct am79c930_softc *sc, u_int32_t off, u_int8_t *ptr,
    size_t len)
{
	int i;

	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_HI,
	    ((off>>8)& 0x7f));
	AM930_DELAY(1);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_LMA_LO, (off&0xff));
	AM930_DELAY(1);
	for (i=0; i<len; i++)
		ptr[i] = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
		    AM79C930_IODPA);
}

static void
mem_write_1(struct am79c930_softc *sc, u_int32_t off, u_int8_t val)
{
	bus_space_write_1(sc->sc_memt, sc->sc_memh, off, val);
}

static void
mem_write_2(struct am79c930_softc *sc, u_int32_t off, u_int16_t val)
{
	bus_space_tag_t t = sc->sc_memt;
	bus_space_handle_t h = sc->sc_memh;

	/* could be unaligned */
	if ((off & 0x1) == 0)
		bus_space_write_stream_2(t, h, off,    htole16(val));
	else {
		bus_space_write_1(t, h, off,    val        & 0xff);
		bus_space_write_1(t, h, off+1, (val >>  8) & 0xff);
	}
}

static void
mem_write_4(struct am79c930_softc *sc, u_int32_t off, u_int32_t val)
{
	bus_space_tag_t t = sc->sc_memt;
	bus_space_handle_t h = sc->sc_memh;

	/* could be unaligned */
	if ((off & 0x3) == 0)
		bus_space_write_stream_4(t, h, off,    htole32(val));
	else {
		bus_space_write_1(t, h, off,    val        & 0xff);
		bus_space_write_1(t, h, off+1, (val >>  8) & 0xff);
		bus_space_write_1(t, h, off+2, (val >> 16) & 0xff);
		bus_space_write_1(t, h, off+3, (val >> 24) & 0xff);
	}
}

static void
mem_write_bytes(struct am79c930_softc *sc, u_int32_t off, u_int8_t *ptr,
    size_t len)
{
	bus_space_write_region_1 (sc->sc_memt, sc->sc_memh, off, ptr, len);
}

static u_int8_t
mem_read_1(struct am79c930_softc *sc, u_int32_t off)
{
	return bus_space_read_1(sc->sc_memt, sc->sc_memh, off);
}

static u_int16_t
mem_read_2(struct am79c930_softc *sc, u_int32_t off)
{
	/* could be unaligned */
	if ((off & 0x1) == 0)
		return le16toh(bus_space_read_stream_2(sc->sc_memt,
		    sc->sc_memh, off));
	else
		return
		     bus_space_read_1(sc->sc_memt, sc->sc_memh, off  )       |
		    (bus_space_read_1(sc->sc_memt, sc->sc_memh, off+1) << 8);
}

static u_int32_t
mem_read_4(struct am79c930_softc *sc, u_int32_t off)
{
	/* could be unaligned */
	if ((off & 0x3) == 0)
		return le32toh(bus_space_read_stream_4(sc->sc_memt, sc->sc_memh,
		    off));
	else
		return
		     bus_space_read_1(sc->sc_memt, sc->sc_memh, off  )       |
		    (bus_space_read_1(sc->sc_memt, sc->sc_memh, off+1) << 8) |
		    (bus_space_read_1(sc->sc_memt, sc->sc_memh, off+2) <<16) |
		    (bus_space_read_1(sc->sc_memt, sc->sc_memh, off+3) <<24);
}

static void
mem_read_bytes(struct am79c930_softc *sc, u_int32_t off, u_int8_t *ptr,
    size_t len)
{
	bus_space_read_region_1 (sc->sc_memt, sc->sc_memh, off, ptr, len);
}


/*
 * Set bits in GCR.
 */

void
am79c930_gcr_setbits(struct am79c930_softc *sc, u_int8_t bits)
{
	u_int8_t gcr = bus_space_read_1 (sc->sc_iot, sc->sc_ioh, AM79C930_GCR);

	gcr |= bits;

	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_GCR, gcr);
}

/*
 * Clear bits in GCR.
 */

void
am79c930_gcr_clearbits(struct am79c930_softc *sc, u_int8_t bits)
{
	u_int8_t gcr = bus_space_read_1 (sc->sc_iot, sc->sc_ioh, AM79C930_GCR);

	gcr &= ~bits;

	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_GCR, gcr);
}

u_int8_t
am79c930_gcr_read(struct am79c930_softc *sc)
{
	return bus_space_read_1 (sc->sc_iot, sc->sc_ioh, AM79C930_GCR);
}

#if 0
void
am79c930_regdump(struct am79c930_softc *sc)
{
	u_int8_t buf[8];
	int i;

	AM930_DELAY(5);
	for (i=0; i<8; i++) {
		buf[i] = bus_space_read_1 (sc->sc_iot, sc->sc_ioh, i);
		AM930_DELAY(5);
	}
	printf("am79c930: regdump:");
	for (i=0; i<8; i++) {
		printf(" %02x", buf[i]);
	}
	printf("\n");
}
#endif

void
am79c930_chip_init(struct am79c930_softc *sc, int how)
{
	/* zero the bank select register, and leave it that way.. */
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AM79C930_BSS, 0);
	if (how)
	  	sc->sc_ops = &memspace_ops;
	else
	  	sc->sc_ops = &iospace_ops;
}