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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*	$NetBSD: dio.c,v 1.39 2014/03/26 17:55:26 christos Exp $	*/

/*-
 * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jason R. Thorpe.
 *
 * 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.
 */

/*
 * Autoconfiguration and mapping support for the DIO bus.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: dio.c,v 1.39 2014/03/26 17:55:26 christos Exp $");

#define	_HP300_INTR_H_PRIVATE

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

#include <machine/bus.h>

#include <uvm/uvm_extern.h>

#include <machine/autoconf.h>
#include <machine/cpu.h>
#include <machine/hp300spu.h>

#include <hp300/dev/dmavar.h>

#include <hp300/dev/dioreg.h>
#include <hp300/dev/diovar.h>

#include <hp300/dev/diodevs.h>
#include <hp300/dev/diodevs_data.h>

#include "locators.h"
#define        diocf_scode             cf_loc[DIOCF_SCODE]

struct dio_softc {
	device_t sc_dev;
	struct bus_space_tag sc_tag;
};

static int	dio_scodesize(struct dio_attach_args *);
static const char *dio_devinfo(struct dio_attach_args *, char *, size_t);

static int	diomatch(device_t, cfdata_t, void *);
static void	dioattach(device_t, device_t, void *);
static int	dioprint(void *, const char *);
static int	diosubmatch(device_t, cfdata_t, const int *, void *);

CFATTACH_DECL_NEW(dio, sizeof(struct dio_softc),
    diomatch, dioattach, NULL, NULL);

static int
diomatch(device_t parent, cfdata_t cf, void *aux)
{
	static int dio_matched = 0;

	/* Allow only one instance. */
	if (dio_matched)
		return 0;

	dio_matched = 1;
	return 1;
}

static void
dioattach(device_t parent, device_t self, void *aux)
{
	struct dio_softc *sc = device_private(self);
	struct dio_attach_args da;
	bus_addr_t pa;
	void *va;
	bus_space_tag_t bst = &sc->sc_tag;
	bus_space_handle_t bsh;
	int scode, scmax, scodesize;

	sc->sc_dev = self;
	aprint_normal("\n");

	memset(bst, 0, sizeof(struct bus_space_tag));
	bst->bustype = HP300_BUS_SPACE_DIO;

	scmax = DIO_SCMAX(machineid);

	for (scode = 0; scode < scmax; ) {
		if (DIO_INHOLE(scode)) {
			scode++;
			continue;
		}

		/*
		 * Temporarily map the space corresponding to
		 * the current select code unless:
		 */
		pa = (bus_addr_t)dio_scodetopa(scode);
		if (bus_space_map(bst, pa, PAGE_SIZE, 0, &bsh)) {
			aprint_error_dev(self, "can't map scode %d\n", scode);
			scode++;
			continue;
		}
		va = bus_space_vaddr(bst, bsh);

		/* Check for hardware. */
		if (badaddr(va)) {
			bus_space_unmap(bst, bsh, PAGE_SIZE);
			scode++;
			continue;
		}

		/* Fill out attach args. */
		memset(&da, 0, sizeof(da));
		da.da_bst = bst;
		da.da_scode = scode;

		da.da_id = DIO_ID(va);
		if (DIO_ISFRAMEBUFFER(da.da_id))
			da.da_secid = DIO_SECID(va);
		da.da_addr = pa;
		da.da_size = DIO_SIZE(scode, va);
		scodesize = dio_scodesize(&da);
		if (DIO_ISDIO(scode))
			da.da_size *= scodesize;
		da.da_ipl = DIO_IPL(va);

		/* No longer need the device to be mapped. */
		bus_space_unmap(bst, bsh, PAGE_SIZE);

		/* Attach matching device. */
		config_found_sm_loc(self, "dio", NULL, &da, dioprint,
		    diosubmatch);
		scode += scodesize;
	}
}

static int
diosubmatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
{
	struct dio_attach_args *da = aux;

	if (cf->diocf_scode != DIOCF_SCODE_DEFAULT &&
	    cf->diocf_scode != da->da_scode)
		return 0;

	return config_match(parent, cf, aux);
}

static int
dioprint(void *aux, const char *pnp)
{
	struct dio_attach_args *da = aux;
	char buf[128];

	if (pnp)
		aprint_normal("%s at %s",
		    dio_devinfo(da, buf, sizeof(buf)), pnp);
	aprint_normal(" scode %d ipl %d", da->da_scode, da->da_ipl);
	return UNCONF;
}

/*
 * Convert a select code to a system physical address.
 */
void *
dio_scodetopa(int scode)
{
	u_long rval;

	if (DIO_ISDIO(scode))
		rval = DIO_BASE + (scode * DIO_DEVSIZE);
	else if (DIO_ISDIOII(scode))
		rval = DIOII_BASE + ((scode - DIOII_SCBASE) * DIOII_DEVSIZE);
	else
		rval = 0;

	return (void *)rval;
}

/*
 * Return the select code size for this device, defaulting to 1
 * if we don't know what kind of device we have.
 */
static int
dio_scodesize(struct dio_attach_args *da)
{
	int i;

	/*
	 * Find the dio_devdata matchind the primary id.
	 * If we're a framebuffer, we also check the secondary id.
	 */
	for (i = 0; i < DIO_NDEVICES; i++) {
		if (da->da_id == dio_devdatas[i].dd_id) {
			if (DIO_ISFRAMEBUFFER(da->da_id)) {
				if (da->da_secid == dio_devdatas[i].dd_secid) {
					goto foundit;
				}
			} else {
 foundit:
				return dio_devdatas[i].dd_nscode;
			}
		}
	}

	/*
	 * Device is unknown.  Print a warning and assume a default.
	 */
	aprint_error("WARNING: select code size unknown "
	    "for id = 0x%x secid = 0x%x\n",
	    da->da_id, da->da_secid);
	return 1;
}

/*
 * Return a reasonable description of a DIO device.
 */
static const char *
dio_devinfo(struct dio_attach_args *da, char *buf, size_t buflen)
{
#ifdef DIOVERBOSE
	int i;
#endif

	memset(buf, 0, buflen);

#ifdef DIOVERBOSE
	/*
	 * Find the description matching our primary id.
	 * If we're a framebuffer, we also check the secondary id.
	 */
	for (i = 0; i < DIO_NDEVICES; i++) {
		if (da->da_id == dio_devdescs[i].dd_id) {
			if (DIO_ISFRAMEBUFFER(da->da_id)) {
				if (da->da_secid == dio_devdescs[i].dd_secid) {
					goto foundit;
				}
			} else {
			foundit:
				snprintf(buf, buflen, "%s",
				    dio_devdescs[i].dd_desc);
				return buf;
			}
		}
	}
#endif /* DIOVERBOSE */

	/*
	 * Device is unknown.  Construct something reasonable.
	 */
	snprintf(buf, buflen, "device id = 0x%x secid = 0x%x",
	    da->da_id, da->da_secid);
	return buf;
}

/*
 * Establish an interrupt handler for a DIO device.
 */
void *
dio_intr_establish(int (*func)(void *), void *arg, int ipl, int priority)
{
	void *ih;

	ih = intr_establish(func, arg, ipl, priority);

	if (priority == IPL_BIO)
		dmacomputeipl();

	return ih;
}

/*
 * Remove an interrupt handler for a DIO device.
 */
void
dio_intr_disestablish(void *arg)
{
	struct hp300_intrhand *ih = arg;
	int priority = ih->ih_priority;

	intr_disestablish(arg);

	if (priority == IPL_BIO)
		dmacomputeipl();
}

/*
 * DIO specific bus_space(9) support functions.
 */
static uint8_t dio_bus_space_read_oddbyte_1(bus_space_tag_t,
    bus_space_handle_t, bus_size_t);
static void dio_bus_space_write_oddbyte_1(bus_space_tag_t,
    bus_space_handle_t, bus_size_t, uint8_t);

static void dio_bus_space_read_multi_oddbyte_1(bus_space_tag_t,
    bus_space_handle_t, bus_size_t, uint8_t *, bus_size_t);
static void dio_bus_space_write_multi_oddbyte_1(bus_space_tag_t,
    bus_space_handle_t, bus_size_t, const uint8_t *, bus_size_t);

static void dio_bus_space_read_region_oddbyte_1(bus_space_tag_t,
    bus_space_handle_t, bus_size_t, uint8_t *, bus_size_t);
static void dio_bus_space_write_region_oddbyte_1(bus_space_tag_t,
    bus_space_handle_t, bus_size_t, const uint8_t *, bus_size_t);

static void dio_bus_space_set_multi_oddbyte_1(bus_space_tag_t,
    bus_space_handle_t, bus_size_t, uint8_t, bus_size_t);

static void dio_bus_space_set_region_oddbyte_1(bus_space_tag_t,
    bus_space_handle_t, bus_size_t, uint8_t, bus_size_t);

/*
 * dio_set_bus_space_oddbyte():
 *	Override bus_space functions in bus_space_tag_t
 *	for devices which have odd byte address space.
 */
void
dio_set_bus_space_oddbyte(bus_space_tag_t bst)
{

	/* XXX only 1-byte functions for now */
	bst->bsr1 = dio_bus_space_read_oddbyte_1;
	bst->bsw1 = dio_bus_space_write_oddbyte_1;

	bst->bsrm1 = dio_bus_space_read_multi_oddbyte_1;
	bst->bswm1 = dio_bus_space_write_multi_oddbyte_1;

	bst->bsrr1 = dio_bus_space_read_region_oddbyte_1;
	bst->bswr1 = dio_bus_space_write_region_oddbyte_1;

	bst->bssm1 = dio_bus_space_set_multi_oddbyte_1;

	bst->bssr1 = dio_bus_space_set_region_oddbyte_1;
}

static uint8_t
dio_bus_space_read_oddbyte_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    bus_size_t offset)
{

	return *(volatile uint8_t *)(bsh + (offset << 1) + 1);
}

static void
dio_bus_space_write_oddbyte_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    bus_size_t offset, uint8_t val)
{

	*(volatile uint8_t *)(bsh + (offset << 1) + 1) = val;
}

static void
dio_bus_space_read_multi_oddbyte_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    bus_size_t offset, uint8_t *addr, bus_size_t len)
{

	__asm volatile (
	"	movl	%0,%%a0		;\n"
	"	movl	%1,%%a1		;\n"
	"	movl	%2,%%d0		;\n"
	"1:	movb	%%a0@,%%a1@+	;\n"
	"	subql	#1,%%d0		;\n"
	"	jne	1b"
	    :
	    : "r" (bsh + (offset << 1) + 1), "g" (addr), "g" (len)
	    : "%a0","%a1","%d0");
}

static void
dio_bus_space_write_multi_oddbyte_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    bus_size_t offset, const uint8_t *addr, bus_size_t len)
{

	__asm volatile (
	"	movl	%0,%%a0		;\n"
	"	movl	%1,%%a1		;\n"
	"	movl	%2,%%d0		;\n"
	"1:	movb	%%a1@+,%%a0@	;\n"
	"	subql	#1,%%d0		;\n"
	"	jne	1b"
	    :
	    : "r" (bsh + (offset << 1) + 1), "g" (addr), "g" (len)
	    : "%a0","%a1","%d0");
}

static void
dio_bus_space_read_region_oddbyte_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    bus_size_t offset, uint8_t *addr, bus_size_t len)
{
	__asm volatile (
	"	movl	%0,%%a0		;\n"
	"	movl	%1,%%a1		;\n"
	"	movl	%2,%%d0		;\n"
	"1:	movb	%%a0@,%%a1@+	;\n"
	"	addql	#2,%%a0		;\n"
	"	subql	#1,%%d0		;\n"
	"	jne	1b"
	    :
	    : "r" (bsh + (offset << 1) + 1), "g" (addr), "g" (len)
	    : "%a0","%a1","%d0");
}

static void
dio_bus_space_write_region_oddbyte_1(bus_space_tag_t bst,
    bus_space_handle_t bsh, bus_size_t offset, const uint8_t *addr,
    bus_size_t len)
{

	__asm volatile (
	"	movl	%0,%%a0		;\n"
	"	movl	%1,%%a1		;\n"
	"	movl	%2,%%d0		;\n"
	"1:	movb	%%a1@+,%%a0@	;\n"
	"	addql	#2,%%a0		;\n"
	"	subql	#1,%%d0		;\n"
	"	jne	1b"
	    :
	    : "r" (bsh + (offset << 1) + 1), "g" (addr), "g" (len)
	    : "%a0","%a1","%d0");
}

static void
dio_bus_space_set_multi_oddbyte_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    bus_size_t offset, uint8_t val, bus_size_t count)
{
	__asm volatile (
	"	movl	%0,%%a0		;\n"
	"	movl	%1,%%d1		;\n"
	"	movl	%2,%%d0		;\n"
	"1:	movb	%%d1,%%a0@	;\n"
	"	subql	#1,%%d0		;\n"
	"	jne	1b"
	    :
	    : "r" (bsh + (offset << 1) + 1), "g" (val), "g" (count)
	    : "%a0","%d0","%d1");
}

static void
dio_bus_space_set_region_oddbyte_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    bus_size_t offset, uint8_t val, bus_size_t count)
{

	__asm volatile (
	"	movl	%0,%%a0		;\n"
	"	movl	%1,%%d1		;\n"
	"	movl	%2,%%d0		;\n"
	"1:	movb	%%d1,%%a0@	;\n"
	"	addql	#2,%%a0		;\n"
	"	subql	#1,%%d0		;\n"
	"	jne	1b"
	    :
	    : "r" (bsh + (offset << 1) + 1), "g" (val), "g" (count)
	    : "%a0","%d0","%d1");
}