Home | History | Annotate | Line # | Download | only in dev
dio.c revision 1.40
      1 /*	$NetBSD: dio.c,v 1.40 2021/04/24 23:36:37 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Autoconfiguration and mapping support for the DIO bus.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: dio.c,v 1.40 2021/04/24 23:36:37 thorpej Exp $");
     38 
     39 #define	_HP300_INTR_H_PRIVATE
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/device.h>
     45 
     46 #include <machine/bus.h>
     47 
     48 #include <uvm/uvm_extern.h>
     49 
     50 #include <machine/autoconf.h>
     51 #include <machine/cpu.h>
     52 #include <machine/hp300spu.h>
     53 
     54 #include <hp300/dev/dmavar.h>
     55 
     56 #include <hp300/dev/dioreg.h>
     57 #include <hp300/dev/diovar.h>
     58 
     59 #include <hp300/dev/diodevs.h>
     60 #include <hp300/dev/diodevs_data.h>
     61 
     62 #include "locators.h"
     63 #define        diocf_scode             cf_loc[DIOCF_SCODE]
     64 
     65 struct dio_softc {
     66 	device_t sc_dev;
     67 	struct bus_space_tag sc_tag;
     68 };
     69 
     70 static int	dio_scodesize(struct dio_attach_args *);
     71 static const char *dio_devinfo(struct dio_attach_args *, char *, size_t);
     72 
     73 static int	diomatch(device_t, cfdata_t, void *);
     74 static void	dioattach(device_t, device_t, void *);
     75 static int	dioprint(void *, const char *);
     76 static int	diosubmatch(device_t, cfdata_t, const int *, void *);
     77 
     78 CFATTACH_DECL_NEW(dio, sizeof(struct dio_softc),
     79     diomatch, dioattach, NULL, NULL);
     80 
     81 static int
     82 diomatch(device_t parent, cfdata_t cf, void *aux)
     83 {
     84 	static int dio_matched = 0;
     85 
     86 	/* Allow only one instance. */
     87 	if (dio_matched)
     88 		return 0;
     89 
     90 	dio_matched = 1;
     91 	return 1;
     92 }
     93 
     94 static void
     95 dioattach(device_t parent, device_t self, void *aux)
     96 {
     97 	struct dio_softc *sc = device_private(self);
     98 	struct dio_attach_args da;
     99 	bus_addr_t pa;
    100 	void *va;
    101 	bus_space_tag_t bst = &sc->sc_tag;
    102 	bus_space_handle_t bsh;
    103 	int scode, scmax, scodesize;
    104 
    105 	sc->sc_dev = self;
    106 	aprint_normal("\n");
    107 
    108 	memset(bst, 0, sizeof(struct bus_space_tag));
    109 	bst->bustype = HP300_BUS_SPACE_DIO;
    110 
    111 	scmax = DIO_SCMAX(machineid);
    112 
    113 	for (scode = 0; scode < scmax; ) {
    114 		if (DIO_INHOLE(scode)) {
    115 			scode++;
    116 			continue;
    117 		}
    118 
    119 		/*
    120 		 * Temporarily map the space corresponding to
    121 		 * the current select code unless:
    122 		 */
    123 		pa = (bus_addr_t)dio_scodetopa(scode);
    124 		if (bus_space_map(bst, pa, PAGE_SIZE, 0, &bsh)) {
    125 			aprint_error_dev(self, "can't map scode %d\n", scode);
    126 			scode++;
    127 			continue;
    128 		}
    129 		va = bus_space_vaddr(bst, bsh);
    130 
    131 		/* Check for hardware. */
    132 		if (badaddr(va)) {
    133 			bus_space_unmap(bst, bsh, PAGE_SIZE);
    134 			scode++;
    135 			continue;
    136 		}
    137 
    138 		/* Fill out attach args. */
    139 		memset(&da, 0, sizeof(da));
    140 		da.da_bst = bst;
    141 		da.da_scode = scode;
    142 
    143 		da.da_id = DIO_ID(va);
    144 		if (DIO_ISFRAMEBUFFER(da.da_id))
    145 			da.da_secid = DIO_SECID(va);
    146 		da.da_addr = pa;
    147 		da.da_size = DIO_SIZE(scode, va);
    148 		scodesize = dio_scodesize(&da);
    149 		if (DIO_ISDIO(scode))
    150 			da.da_size *= scodesize;
    151 		da.da_ipl = DIO_IPL(va);
    152 
    153 		/* No longer need the device to be mapped. */
    154 		bus_space_unmap(bst, bsh, PAGE_SIZE);
    155 
    156 		/* Attach matching device. */
    157 		config_found(self, &da, dioprint,
    158 		    CFARG_SUBMATCH, diosubmatch,
    159 		    CFARG_EOL);
    160 		scode += scodesize;
    161 	}
    162 }
    163 
    164 static int
    165 diosubmatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    166 {
    167 	struct dio_attach_args *da = aux;
    168 
    169 	if (cf->diocf_scode != DIOCF_SCODE_DEFAULT &&
    170 	    cf->diocf_scode != da->da_scode)
    171 		return 0;
    172 
    173 	return config_match(parent, cf, aux);
    174 }
    175 
    176 static int
    177 dioprint(void *aux, const char *pnp)
    178 {
    179 	struct dio_attach_args *da = aux;
    180 	char buf[128];
    181 
    182 	if (pnp)
    183 		aprint_normal("%s at %s",
    184 		    dio_devinfo(da, buf, sizeof(buf)), pnp);
    185 	aprint_normal(" scode %d ipl %d", da->da_scode, da->da_ipl);
    186 	return UNCONF;
    187 }
    188 
    189 /*
    190  * Convert a select code to a system physical address.
    191  */
    192 void *
    193 dio_scodetopa(int scode)
    194 {
    195 	u_long rval;
    196 
    197 	if (DIO_ISDIO(scode))
    198 		rval = DIO_BASE + (scode * DIO_DEVSIZE);
    199 	else if (DIO_ISDIOII(scode))
    200 		rval = DIOII_BASE + ((scode - DIOII_SCBASE) * DIOII_DEVSIZE);
    201 	else
    202 		rval = 0;
    203 
    204 	return (void *)rval;
    205 }
    206 
    207 /*
    208  * Return the select code size for this device, defaulting to 1
    209  * if we don't know what kind of device we have.
    210  */
    211 static int
    212 dio_scodesize(struct dio_attach_args *da)
    213 {
    214 	int i;
    215 
    216 	/*
    217 	 * Find the dio_devdata matchind the primary id.
    218 	 * If we're a framebuffer, we also check the secondary id.
    219 	 */
    220 	for (i = 0; i < DIO_NDEVICES; i++) {
    221 		if (da->da_id == dio_devdatas[i].dd_id) {
    222 			if (DIO_ISFRAMEBUFFER(da->da_id)) {
    223 				if (da->da_secid == dio_devdatas[i].dd_secid) {
    224 					goto foundit;
    225 				}
    226 			} else {
    227  foundit:
    228 				return dio_devdatas[i].dd_nscode;
    229 			}
    230 		}
    231 	}
    232 
    233 	/*
    234 	 * Device is unknown.  Print a warning and assume a default.
    235 	 */
    236 	aprint_error("WARNING: select code size unknown "
    237 	    "for id = 0x%x secid = 0x%x\n",
    238 	    da->da_id, da->da_secid);
    239 	return 1;
    240 }
    241 
    242 /*
    243  * Return a reasonable description of a DIO device.
    244  */
    245 static const char *
    246 dio_devinfo(struct dio_attach_args *da, char *buf, size_t buflen)
    247 {
    248 #ifdef DIOVERBOSE
    249 	int i;
    250 #endif
    251 
    252 	memset(buf, 0, buflen);
    253 
    254 #ifdef DIOVERBOSE
    255 	/*
    256 	 * Find the description matching our primary id.
    257 	 * If we're a framebuffer, we also check the secondary id.
    258 	 */
    259 	for (i = 0; i < DIO_NDEVICES; i++) {
    260 		if (da->da_id == dio_devdescs[i].dd_id) {
    261 			if (DIO_ISFRAMEBUFFER(da->da_id)) {
    262 				if (da->da_secid == dio_devdescs[i].dd_secid) {
    263 					goto foundit;
    264 				}
    265 			} else {
    266 			foundit:
    267 				snprintf(buf, buflen, "%s",
    268 				    dio_devdescs[i].dd_desc);
    269 				return buf;
    270 			}
    271 		}
    272 	}
    273 #endif /* DIOVERBOSE */
    274 
    275 	/*
    276 	 * Device is unknown.  Construct something reasonable.
    277 	 */
    278 	snprintf(buf, buflen, "device id = 0x%x secid = 0x%x",
    279 	    da->da_id, da->da_secid);
    280 	return buf;
    281 }
    282 
    283 /*
    284  * Establish an interrupt handler for a DIO device.
    285  */
    286 void *
    287 dio_intr_establish(int (*func)(void *), void *arg, int ipl, int priority)
    288 {
    289 	void *ih;
    290 
    291 	ih = intr_establish(func, arg, ipl, priority);
    292 
    293 	if (priority == IPL_BIO)
    294 		dmacomputeipl();
    295 
    296 	return ih;
    297 }
    298 
    299 /*
    300  * Remove an interrupt handler for a DIO device.
    301  */
    302 void
    303 dio_intr_disestablish(void *arg)
    304 {
    305 	struct hp300_intrhand *ih = arg;
    306 	int priority = ih->ih_priority;
    307 
    308 	intr_disestablish(arg);
    309 
    310 	if (priority == IPL_BIO)
    311 		dmacomputeipl();
    312 }
    313 
    314 /*
    315  * DIO specific bus_space(9) support functions.
    316  */
    317 static uint8_t dio_bus_space_read_oddbyte_1(bus_space_tag_t,
    318     bus_space_handle_t, bus_size_t);
    319 static void dio_bus_space_write_oddbyte_1(bus_space_tag_t,
    320     bus_space_handle_t, bus_size_t, uint8_t);
    321 
    322 static void dio_bus_space_read_multi_oddbyte_1(bus_space_tag_t,
    323     bus_space_handle_t, bus_size_t, uint8_t *, bus_size_t);
    324 static void dio_bus_space_write_multi_oddbyte_1(bus_space_tag_t,
    325     bus_space_handle_t, bus_size_t, const uint8_t *, bus_size_t);
    326 
    327 static void dio_bus_space_read_region_oddbyte_1(bus_space_tag_t,
    328     bus_space_handle_t, bus_size_t, uint8_t *, bus_size_t);
    329 static void dio_bus_space_write_region_oddbyte_1(bus_space_tag_t,
    330     bus_space_handle_t, bus_size_t, const uint8_t *, bus_size_t);
    331 
    332 static void dio_bus_space_set_multi_oddbyte_1(bus_space_tag_t,
    333     bus_space_handle_t, bus_size_t, uint8_t, bus_size_t);
    334 
    335 static void dio_bus_space_set_region_oddbyte_1(bus_space_tag_t,
    336     bus_space_handle_t, bus_size_t, uint8_t, bus_size_t);
    337 
    338 /*
    339  * dio_set_bus_space_oddbyte():
    340  *	Override bus_space functions in bus_space_tag_t
    341  *	for devices which have odd byte address space.
    342  */
    343 void
    344 dio_set_bus_space_oddbyte(bus_space_tag_t bst)
    345 {
    346 
    347 	/* XXX only 1-byte functions for now */
    348 	bst->bsr1 = dio_bus_space_read_oddbyte_1;
    349 	bst->bsw1 = dio_bus_space_write_oddbyte_1;
    350 
    351 	bst->bsrm1 = dio_bus_space_read_multi_oddbyte_1;
    352 	bst->bswm1 = dio_bus_space_write_multi_oddbyte_1;
    353 
    354 	bst->bsrr1 = dio_bus_space_read_region_oddbyte_1;
    355 	bst->bswr1 = dio_bus_space_write_region_oddbyte_1;
    356 
    357 	bst->bssm1 = dio_bus_space_set_multi_oddbyte_1;
    358 
    359 	bst->bssr1 = dio_bus_space_set_region_oddbyte_1;
    360 }
    361 
    362 static uint8_t
    363 dio_bus_space_read_oddbyte_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    364     bus_size_t offset)
    365 {
    366 
    367 	return *(volatile uint8_t *)(bsh + (offset << 1) + 1);
    368 }
    369 
    370 static void
    371 dio_bus_space_write_oddbyte_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    372     bus_size_t offset, uint8_t val)
    373 {
    374 
    375 	*(volatile uint8_t *)(bsh + (offset << 1) + 1) = val;
    376 }
    377 
    378 static void
    379 dio_bus_space_read_multi_oddbyte_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    380     bus_size_t offset, uint8_t *addr, bus_size_t len)
    381 {
    382 
    383 	__asm volatile (
    384 	"	movl	%0,%%a0		;\n"
    385 	"	movl	%1,%%a1		;\n"
    386 	"	movl	%2,%%d0		;\n"
    387 	"1:	movb	%%a0@,%%a1@+	;\n"
    388 	"	subql	#1,%%d0		;\n"
    389 	"	jne	1b"
    390 	    :
    391 	    : "r" (bsh + (offset << 1) + 1), "g" (addr), "g" (len)
    392 	    : "%a0","%a1","%d0");
    393 }
    394 
    395 static void
    396 dio_bus_space_write_multi_oddbyte_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    397     bus_size_t offset, const uint8_t *addr, bus_size_t len)
    398 {
    399 
    400 	__asm volatile (
    401 	"	movl	%0,%%a0		;\n"
    402 	"	movl	%1,%%a1		;\n"
    403 	"	movl	%2,%%d0		;\n"
    404 	"1:	movb	%%a1@+,%%a0@	;\n"
    405 	"	subql	#1,%%d0		;\n"
    406 	"	jne	1b"
    407 	    :
    408 	    : "r" (bsh + (offset << 1) + 1), "g" (addr), "g" (len)
    409 	    : "%a0","%a1","%d0");
    410 }
    411 
    412 static void
    413 dio_bus_space_read_region_oddbyte_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    414     bus_size_t offset, uint8_t *addr, bus_size_t len)
    415 {
    416 	__asm volatile (
    417 	"	movl	%0,%%a0		;\n"
    418 	"	movl	%1,%%a1		;\n"
    419 	"	movl	%2,%%d0		;\n"
    420 	"1:	movb	%%a0@,%%a1@+	;\n"
    421 	"	addql	#2,%%a0		;\n"
    422 	"	subql	#1,%%d0		;\n"
    423 	"	jne	1b"
    424 	    :
    425 	    : "r" (bsh + (offset << 1) + 1), "g" (addr), "g" (len)
    426 	    : "%a0","%a1","%d0");
    427 }
    428 
    429 static void
    430 dio_bus_space_write_region_oddbyte_1(bus_space_tag_t bst,
    431     bus_space_handle_t bsh, bus_size_t offset, const uint8_t *addr,
    432     bus_size_t len)
    433 {
    434 
    435 	__asm volatile (
    436 	"	movl	%0,%%a0		;\n"
    437 	"	movl	%1,%%a1		;\n"
    438 	"	movl	%2,%%d0		;\n"
    439 	"1:	movb	%%a1@+,%%a0@	;\n"
    440 	"	addql	#2,%%a0		;\n"
    441 	"	subql	#1,%%d0		;\n"
    442 	"	jne	1b"
    443 	    :
    444 	    : "r" (bsh + (offset << 1) + 1), "g" (addr), "g" (len)
    445 	    : "%a0","%a1","%d0");
    446 }
    447 
    448 static void
    449 dio_bus_space_set_multi_oddbyte_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    450     bus_size_t offset, uint8_t val, bus_size_t count)
    451 {
    452 	__asm volatile (
    453 	"	movl	%0,%%a0		;\n"
    454 	"	movl	%1,%%d1		;\n"
    455 	"	movl	%2,%%d0		;\n"
    456 	"1:	movb	%%d1,%%a0@	;\n"
    457 	"	subql	#1,%%d0		;\n"
    458 	"	jne	1b"
    459 	    :
    460 	    : "r" (bsh + (offset << 1) + 1), "g" (val), "g" (count)
    461 	    : "%a0","%d0","%d1");
    462 }
    463 
    464 static void
    465 dio_bus_space_set_region_oddbyte_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    466     bus_size_t offset, uint8_t val, bus_size_t count)
    467 {
    468 
    469 	__asm volatile (
    470 	"	movl	%0,%%a0		;\n"
    471 	"	movl	%1,%%d1		;\n"
    472 	"	movl	%2,%%d0		;\n"
    473 	"1:	movb	%%d1,%%a0@	;\n"
    474 	"	addql	#2,%%a0		;\n"
    475 	"	subql	#1,%%d0		;\n"
    476 	"	jne	1b"
    477 	    :
    478 	    : "r" (bsh + (offset << 1) + 1), "g" (val), "g" (count)
    479 	    : "%a0","%d0","%d1");
    480 }
    481