Home | History | Annotate | Line # | Download | only in dev
dio.c revision 1.39.44.1
      1 /*	$NetBSD: dio.c,v 1.39.44.1 2021/03/22 02:00:56 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.39.44.1 2021/03/22 02:00:56 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_IATTR, "dio",
    160 		    CFARG_EOL);
    161 		scode += scodesize;
    162 	}
    163 }
    164 
    165 static int
    166 diosubmatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    167 {
    168 	struct dio_attach_args *da = aux;
    169 
    170 	if (cf->diocf_scode != DIOCF_SCODE_DEFAULT &&
    171 	    cf->diocf_scode != da->da_scode)
    172 		return 0;
    173 
    174 	return config_match(parent, cf, aux);
    175 }
    176 
    177 static int
    178 dioprint(void *aux, const char *pnp)
    179 {
    180 	struct dio_attach_args *da = aux;
    181 	char buf[128];
    182 
    183 	if (pnp)
    184 		aprint_normal("%s at %s",
    185 		    dio_devinfo(da, buf, sizeof(buf)), pnp);
    186 	aprint_normal(" scode %d ipl %d", da->da_scode, da->da_ipl);
    187 	return UNCONF;
    188 }
    189 
    190 /*
    191  * Convert a select code to a system physical address.
    192  */
    193 void *
    194 dio_scodetopa(int scode)
    195 {
    196 	u_long rval;
    197 
    198 	if (DIO_ISDIO(scode))
    199 		rval = DIO_BASE + (scode * DIO_DEVSIZE);
    200 	else if (DIO_ISDIOII(scode))
    201 		rval = DIOII_BASE + ((scode - DIOII_SCBASE) * DIOII_DEVSIZE);
    202 	else
    203 		rval = 0;
    204 
    205 	return (void *)rval;
    206 }
    207 
    208 /*
    209  * Return the select code size for this device, defaulting to 1
    210  * if we don't know what kind of device we have.
    211  */
    212 static int
    213 dio_scodesize(struct dio_attach_args *da)
    214 {
    215 	int i;
    216 
    217 	/*
    218 	 * Find the dio_devdata matchind the primary id.
    219 	 * If we're a framebuffer, we also check the secondary id.
    220 	 */
    221 	for (i = 0; i < DIO_NDEVICES; i++) {
    222 		if (da->da_id == dio_devdatas[i].dd_id) {
    223 			if (DIO_ISFRAMEBUFFER(da->da_id)) {
    224 				if (da->da_secid == dio_devdatas[i].dd_secid) {
    225 					goto foundit;
    226 				}
    227 			} else {
    228  foundit:
    229 				return dio_devdatas[i].dd_nscode;
    230 			}
    231 		}
    232 	}
    233 
    234 	/*
    235 	 * Device is unknown.  Print a warning and assume a default.
    236 	 */
    237 	aprint_error("WARNING: select code size unknown "
    238 	    "for id = 0x%x secid = 0x%x\n",
    239 	    da->da_id, da->da_secid);
    240 	return 1;
    241 }
    242 
    243 /*
    244  * Return a reasonable description of a DIO device.
    245  */
    246 static const char *
    247 dio_devinfo(struct dio_attach_args *da, char *buf, size_t buflen)
    248 {
    249 #ifdef DIOVERBOSE
    250 	int i;
    251 #endif
    252 
    253 	memset(buf, 0, buflen);
    254 
    255 #ifdef DIOVERBOSE
    256 	/*
    257 	 * Find the description matching our primary id.
    258 	 * If we're a framebuffer, we also check the secondary id.
    259 	 */
    260 	for (i = 0; i < DIO_NDEVICES; i++) {
    261 		if (da->da_id == dio_devdescs[i].dd_id) {
    262 			if (DIO_ISFRAMEBUFFER(da->da_id)) {
    263 				if (da->da_secid == dio_devdescs[i].dd_secid) {
    264 					goto foundit;
    265 				}
    266 			} else {
    267 			foundit:
    268 				snprintf(buf, buflen, "%s",
    269 				    dio_devdescs[i].dd_desc);
    270 				return buf;
    271 			}
    272 		}
    273 	}
    274 #endif /* DIOVERBOSE */
    275 
    276 	/*
    277 	 * Device is unknown.  Construct something reasonable.
    278 	 */
    279 	snprintf(buf, buflen, "device id = 0x%x secid = 0x%x",
    280 	    da->da_id, da->da_secid);
    281 	return buf;
    282 }
    283 
    284 /*
    285  * Establish an interrupt handler for a DIO device.
    286  */
    287 void *
    288 dio_intr_establish(int (*func)(void *), void *arg, int ipl, int priority)
    289 {
    290 	void *ih;
    291 
    292 	ih = intr_establish(func, arg, ipl, priority);
    293 
    294 	if (priority == IPL_BIO)
    295 		dmacomputeipl();
    296 
    297 	return ih;
    298 }
    299 
    300 /*
    301  * Remove an interrupt handler for a DIO device.
    302  */
    303 void
    304 dio_intr_disestablish(void *arg)
    305 {
    306 	struct hp300_intrhand *ih = arg;
    307 	int priority = ih->ih_priority;
    308 
    309 	intr_disestablish(arg);
    310 
    311 	if (priority == IPL_BIO)
    312 		dmacomputeipl();
    313 }
    314 
    315 /*
    316  * DIO specific bus_space(9) support functions.
    317  */
    318 static uint8_t dio_bus_space_read_oddbyte_1(bus_space_tag_t,
    319     bus_space_handle_t, bus_size_t);
    320 static void dio_bus_space_write_oddbyte_1(bus_space_tag_t,
    321     bus_space_handle_t, bus_size_t, uint8_t);
    322 
    323 static void dio_bus_space_read_multi_oddbyte_1(bus_space_tag_t,
    324     bus_space_handle_t, bus_size_t, uint8_t *, bus_size_t);
    325 static void dio_bus_space_write_multi_oddbyte_1(bus_space_tag_t,
    326     bus_space_handle_t, bus_size_t, const uint8_t *, bus_size_t);
    327 
    328 static void dio_bus_space_read_region_oddbyte_1(bus_space_tag_t,
    329     bus_space_handle_t, bus_size_t, uint8_t *, bus_size_t);
    330 static void dio_bus_space_write_region_oddbyte_1(bus_space_tag_t,
    331     bus_space_handle_t, bus_size_t, const uint8_t *, bus_size_t);
    332 
    333 static void dio_bus_space_set_multi_oddbyte_1(bus_space_tag_t,
    334     bus_space_handle_t, bus_size_t, uint8_t, bus_size_t);
    335 
    336 static void dio_bus_space_set_region_oddbyte_1(bus_space_tag_t,
    337     bus_space_handle_t, bus_size_t, uint8_t, bus_size_t);
    338 
    339 /*
    340  * dio_set_bus_space_oddbyte():
    341  *	Override bus_space functions in bus_space_tag_t
    342  *	for devices which have odd byte address space.
    343  */
    344 void
    345 dio_set_bus_space_oddbyte(bus_space_tag_t bst)
    346 {
    347 
    348 	/* XXX only 1-byte functions for now */
    349 	bst->bsr1 = dio_bus_space_read_oddbyte_1;
    350 	bst->bsw1 = dio_bus_space_write_oddbyte_1;
    351 
    352 	bst->bsrm1 = dio_bus_space_read_multi_oddbyte_1;
    353 	bst->bswm1 = dio_bus_space_write_multi_oddbyte_1;
    354 
    355 	bst->bsrr1 = dio_bus_space_read_region_oddbyte_1;
    356 	bst->bswr1 = dio_bus_space_write_region_oddbyte_1;
    357 
    358 	bst->bssm1 = dio_bus_space_set_multi_oddbyte_1;
    359 
    360 	bst->bssr1 = dio_bus_space_set_region_oddbyte_1;
    361 }
    362 
    363 static uint8_t
    364 dio_bus_space_read_oddbyte_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    365     bus_size_t offset)
    366 {
    367 
    368 	return *(volatile uint8_t *)(bsh + (offset << 1) + 1);
    369 }
    370 
    371 static void
    372 dio_bus_space_write_oddbyte_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    373     bus_size_t offset, uint8_t val)
    374 {
    375 
    376 	*(volatile uint8_t *)(bsh + (offset << 1) + 1) = val;
    377 }
    378 
    379 static void
    380 dio_bus_space_read_multi_oddbyte_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    381     bus_size_t offset, uint8_t *addr, bus_size_t len)
    382 {
    383 
    384 	__asm volatile (
    385 	"	movl	%0,%%a0		;\n"
    386 	"	movl	%1,%%a1		;\n"
    387 	"	movl	%2,%%d0		;\n"
    388 	"1:	movb	%%a0@,%%a1@+	;\n"
    389 	"	subql	#1,%%d0		;\n"
    390 	"	jne	1b"
    391 	    :
    392 	    : "r" (bsh + (offset << 1) + 1), "g" (addr), "g" (len)
    393 	    : "%a0","%a1","%d0");
    394 }
    395 
    396 static void
    397 dio_bus_space_write_multi_oddbyte_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    398     bus_size_t offset, const uint8_t *addr, bus_size_t len)
    399 {
    400 
    401 	__asm volatile (
    402 	"	movl	%0,%%a0		;\n"
    403 	"	movl	%1,%%a1		;\n"
    404 	"	movl	%2,%%d0		;\n"
    405 	"1:	movb	%%a1@+,%%a0@	;\n"
    406 	"	subql	#1,%%d0		;\n"
    407 	"	jne	1b"
    408 	    :
    409 	    : "r" (bsh + (offset << 1) + 1), "g" (addr), "g" (len)
    410 	    : "%a0","%a1","%d0");
    411 }
    412 
    413 static void
    414 dio_bus_space_read_region_oddbyte_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    415     bus_size_t offset, uint8_t *addr, bus_size_t len)
    416 {
    417 	__asm volatile (
    418 	"	movl	%0,%%a0		;\n"
    419 	"	movl	%1,%%a1		;\n"
    420 	"	movl	%2,%%d0		;\n"
    421 	"1:	movb	%%a0@,%%a1@+	;\n"
    422 	"	addql	#2,%%a0		;\n"
    423 	"	subql	#1,%%d0		;\n"
    424 	"	jne	1b"
    425 	    :
    426 	    : "r" (bsh + (offset << 1) + 1), "g" (addr), "g" (len)
    427 	    : "%a0","%a1","%d0");
    428 }
    429 
    430 static void
    431 dio_bus_space_write_region_oddbyte_1(bus_space_tag_t bst,
    432     bus_space_handle_t bsh, bus_size_t offset, const uint8_t *addr,
    433     bus_size_t len)
    434 {
    435 
    436 	__asm volatile (
    437 	"	movl	%0,%%a0		;\n"
    438 	"	movl	%1,%%a1		;\n"
    439 	"	movl	%2,%%d0		;\n"
    440 	"1:	movb	%%a1@+,%%a0@	;\n"
    441 	"	addql	#2,%%a0		;\n"
    442 	"	subql	#1,%%d0		;\n"
    443 	"	jne	1b"
    444 	    :
    445 	    : "r" (bsh + (offset << 1) + 1), "g" (addr), "g" (len)
    446 	    : "%a0","%a1","%d0");
    447 }
    448 
    449 static void
    450 dio_bus_space_set_multi_oddbyte_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    451     bus_size_t offset, uint8_t val, bus_size_t count)
    452 {
    453 	__asm volatile (
    454 	"	movl	%0,%%a0		;\n"
    455 	"	movl	%1,%%d1		;\n"
    456 	"	movl	%2,%%d0		;\n"
    457 	"1:	movb	%%d1,%%a0@	;\n"
    458 	"	subql	#1,%%d0		;\n"
    459 	"	jne	1b"
    460 	    :
    461 	    : "r" (bsh + (offset << 1) + 1), "g" (val), "g" (count)
    462 	    : "%a0","%d0","%d1");
    463 }
    464 
    465 static void
    466 dio_bus_space_set_region_oddbyte_1(bus_space_tag_t bst, bus_space_handle_t bsh,
    467     bus_size_t offset, uint8_t val, bus_size_t count)
    468 {
    469 
    470 	__asm volatile (
    471 	"	movl	%0,%%a0		;\n"
    472 	"	movl	%1,%%d1		;\n"
    473 	"	movl	%2,%%d0		;\n"
    474 	"1:	movb	%%d1,%%a0@	;\n"
    475 	"	addql	#2,%%a0		;\n"
    476 	"	subql	#1,%%d0		;\n"
    477 	"	jne	1b"
    478 	    :
    479 	    : "r" (bsh + (offset << 1) + 1), "g" (val), "g" (count)
    480 	    : "%a0","%d0","%d1");
    481 }
    482