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