Home | History | Annotate | Line # | Download | only in dev
dio.c revision 1.22
      1  1.22   thorpej /*	$NetBSD: dio.c,v 1.22 2003/04/01 20:41:36 thorpej Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*-
      4  1.11   thorpej  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
      5   1.1   thorpej  * All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1   thorpej  * by Jason R. Thorpe.
      9   1.1   thorpej  *
     10   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     11   1.1   thorpej  * modification, are permitted provided that the following conditions
     12   1.1   thorpej  * are met:
     13   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     14   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     15   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     18   1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     19   1.1   thorpej  *    must display the following acknowledgement:
     20   1.1   thorpej  *        This product includes software developed by the NetBSD
     21   1.1   thorpej  *        Foundation, Inc. and its contributors.
     22   1.1   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1   thorpej  *    contributors may be used to endorse or promote products derived
     24   1.1   thorpej  *    from this software without specific prior written permission.
     25   1.1   thorpej  *
     26   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.10       jtc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.10       jtc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1   thorpej  */
     38   1.1   thorpej 
     39   1.1   thorpej /*
     40   1.1   thorpej  * Autoconfiguration and mapping support for the DIO bus.
     41   1.1   thorpej  */
     42  1.16  gmcgarry 
     43  1.16  gmcgarry #include <sys/cdefs.h>
     44  1.22   thorpej __KERNEL_RCSID(0, "$NetBSD: dio.c,v 1.22 2003/04/01 20:41:36 thorpej Exp $");
     45   1.1   thorpej 
     46   1.7   thorpej #define	_HP300_INTR_H_PRIVATE
     47   1.7   thorpej 
     48   1.1   thorpej #include <sys/param.h>
     49   1.1   thorpej #include <sys/systm.h>
     50   1.1   thorpej #include <sys/kernel.h>
     51   1.1   thorpej #include <sys/device.h>
     52   1.1   thorpej 
     53  1.22   thorpej #include <uvm/uvm_extern.h>
     54  1.22   thorpej 
     55   1.1   thorpej #include <machine/autoconf.h>
     56   1.1   thorpej #include <machine/cpu.h>
     57   1.6   thorpej #include <machine/hp300spu.h>
     58   1.1   thorpej 
     59   1.7   thorpej #include <hp300/dev/dmavar.h>
     60   1.7   thorpej 
     61   1.1   thorpej #include <hp300/dev/dioreg.h>
     62   1.1   thorpej #include <hp300/dev/diovar.h>
     63   1.1   thorpej 
     64   1.1   thorpej #include <hp300/dev/diodevs.h>
     65   1.1   thorpej #include <hp300/dev/diodevs_data.h>
     66   1.1   thorpej 
     67   1.1   thorpej extern	caddr_t internalhpib;
     68   1.1   thorpej 
     69   1.1   thorpej int	dio_scodesize __P((struct dio_attach_args *));
     70   1.1   thorpej char	*dio_devinfo __P((struct dio_attach_args *, char *, size_t));
     71   1.1   thorpej 
     72   1.1   thorpej int	diomatch __P((struct device *, struct cfdata *, void *));
     73   1.1   thorpej void	dioattach __P((struct device *, struct device *, void *));
     74   1.1   thorpej int	dioprint __P((void *, const char *));
     75   1.1   thorpej int	diosubmatch __P((struct device *, struct cfdata *, void *));
     76   1.1   thorpej 
     77  1.19   thorpej CFATTACH_DECL(dio, sizeof(struct device),
     78  1.19   thorpej     diomatch, dioattach, NULL, NULL);
     79   1.1   thorpej 
     80   1.1   thorpej int
     81   1.1   thorpej diomatch(parent, match, aux)
     82   1.1   thorpej 	struct device *parent;
     83   1.1   thorpej 	struct cfdata *match;
     84   1.1   thorpej 	void *aux;
     85   1.1   thorpej {
     86   1.1   thorpej 	static int dio_matched = 0;
     87   1.1   thorpej 
     88   1.1   thorpej 	/* Allow only one instance. */
     89   1.1   thorpej 	if (dio_matched)
     90   1.1   thorpej 		return (0);
     91   1.1   thorpej 
     92   1.1   thorpej 	dio_matched = 1;
     93   1.1   thorpej 	return (1);
     94   1.1   thorpej }
     95   1.1   thorpej 
     96   1.1   thorpej void
     97   1.1   thorpej dioattach(parent, self, aux)
     98   1.1   thorpej 	struct device *parent, *self;
     99   1.1   thorpej 	void *aux;
    100   1.1   thorpej {
    101   1.1   thorpej 	struct dio_attach_args da;
    102   1.1   thorpej 	caddr_t pa, va;
    103   1.1   thorpej 	int scode, scmax, didmap, scodesize;
    104   1.1   thorpej 
    105  1.20  gmcgarry 	printf("\n");
    106  1.20  gmcgarry 
    107   1.1   thorpej 	scmax = DIO_SCMAX(machineid);
    108   1.1   thorpej 
    109   1.1   thorpej 	for (scode = 0; scode < scmax; ) {
    110   1.1   thorpej 		if (DIO_INHOLE(scode)) {
    111   1.1   thorpej 			scode++;
    112   1.1   thorpej 			continue;
    113   1.1   thorpej 		}
    114   1.1   thorpej 
    115   1.1   thorpej 		didmap = 0;
    116   1.1   thorpej 
    117   1.1   thorpej 		/*
    118   1.1   thorpej 		 * Temporarily map the space corresponding to
    119   1.1   thorpej 		 * the current select code unless:
    120   1.1   thorpej 		 *	- this is the internal hpib select code,
    121   1.1   thorpej 		 */
    122   1.1   thorpej 		pa = dio_scodetopa(scode);
    123  1.15  gmcgarry 		if ((scode == 7) && internalhpib)
    124   1.1   thorpej 			va = internalhpib = (caddr_t)IIOV(pa);
    125   1.1   thorpej 		else {
    126  1.22   thorpej 			va = iomap(pa, PAGE_SIZE);
    127   1.1   thorpej 			if (va == NULL) {
    128   1.4    scottr 				printf("%s: can't map scode %d\n",
    129   1.4    scottr 				    self->dv_xname, scode);
    130   1.1   thorpej 				scode++;
    131   1.1   thorpej 				continue;
    132   1.1   thorpej 			}
    133   1.1   thorpej 			didmap = 1;
    134   1.1   thorpej 		}
    135   1.1   thorpej 
    136   1.1   thorpej 		/* Check for hardware. */
    137   1.1   thorpej 		if (badaddr(va)) {
    138   1.1   thorpej 			if (didmap)
    139  1.22   thorpej 				iounmap(va, PAGE_SIZE);
    140   1.1   thorpej 			scode++;
    141   1.1   thorpej 			continue;
    142   1.1   thorpej 		}
    143   1.1   thorpej 
    144   1.1   thorpej 		/* Fill out attach args. */
    145  1.14  gmcgarry 		memset(&da, 0, sizeof(da));
    146  1.11   thorpej 		da.da_bst = HP300_BUS_SPACE_DIO;
    147   1.1   thorpej 		da.da_scode = scode;
    148  1.13   thorpej 		/*
    149  1.13   thorpej 		 * Internal HP-IB doesn't always return a device ID,
    150  1.13   thorpej 		 * so we rely on the sysflags.
    151  1.13   thorpej 		 */
    152  1.13   thorpej 		if ((scode == 7) && internalhpib)
    153  1.13   thorpej 			da.da_id = DIO_DEVICE_ID_IHPIB;
    154  1.13   thorpej 		else
    155  1.13   thorpej 			da.da_id = DIO_ID(va);
    156   1.1   thorpej 
    157   1.1   thorpej 		if (DIO_ISFRAMEBUFFER(da.da_id))
    158   1.1   thorpej 			da.da_secid = DIO_SECID(va);
    159   1.1   thorpej 
    160   1.1   thorpej 		da.da_size = DIO_SIZE(scode, va);
    161   1.1   thorpej 		scodesize = dio_scodesize(&da);
    162   1.1   thorpej 		if (DIO_ISDIO(scode))
    163   1.1   thorpej 			da.da_size *= scodesize;
    164   1.1   thorpej 
    165   1.1   thorpej 		/* No longer need the device to be mapped. */
    166   1.1   thorpej 		if (didmap)
    167  1.22   thorpej 			iounmap(va, PAGE_SIZE);
    168   1.1   thorpej 
    169   1.1   thorpej 		/* Attach matching device. */
    170   1.1   thorpej 		config_found_sm(self, &da, dioprint, diosubmatch);
    171   1.1   thorpej 		scode += scodesize;
    172   1.1   thorpej 	}
    173   1.1   thorpej }
    174   1.1   thorpej 
    175   1.1   thorpej int
    176   1.1   thorpej diosubmatch(parent, cf, aux)
    177   1.1   thorpej 	struct device *parent;
    178   1.1   thorpej 	struct cfdata *cf;
    179   1.1   thorpej 	void *aux;
    180   1.1   thorpej {
    181   1.1   thorpej 	struct dio_attach_args *da = aux;
    182   1.1   thorpej 
    183   1.8       jtk 	if (cf->diocf_scode != DIOCF_SCODE_DEFAULT &&
    184   1.1   thorpej 	    cf->diocf_scode != da->da_scode)
    185   1.1   thorpej 		return (0);
    186   1.1   thorpej 
    187  1.17   thorpej 	return (config_match(parent, cf, aux));
    188   1.1   thorpej }
    189   1.1   thorpej 
    190   1.1   thorpej int
    191   1.1   thorpej dioprint(aux, pnp)
    192   1.1   thorpej 	void *aux;
    193   1.1   thorpej 	const char *pnp;
    194   1.1   thorpej {
    195   1.1   thorpej 	struct dio_attach_args *da = aux;
    196   1.1   thorpej 	char buf[128];
    197   1.1   thorpej 
    198   1.1   thorpej 	if (pnp)
    199  1.21   thorpej 		aprint_normal("%s at %s",
    200  1.21   thorpej 		    dio_devinfo(da, buf, sizeof(buf)), pnp);
    201  1.21   thorpej 	aprint_normal(" scode %d", da->da_scode);
    202   1.1   thorpej 	return (UNCONF);
    203   1.1   thorpej }
    204   1.1   thorpej 
    205   1.1   thorpej /*
    206   1.1   thorpej  * Convert a select code to a system physical address.
    207   1.1   thorpej  */
    208   1.1   thorpej void *
    209   1.1   thorpej dio_scodetopa(scode)
    210   1.1   thorpej 	int scode;
    211   1.1   thorpej {
    212   1.1   thorpej 	u_long rval;
    213   1.1   thorpej 
    214   1.1   thorpej 	if (scode == 7 && internalhpib)
    215   1.1   thorpej 		rval = DIO_IHPIBADDR;
    216   1.1   thorpej 	else if (DIO_ISDIO(scode))
    217   1.1   thorpej 		rval = DIO_BASE + (scode * DIO_DEVSIZE);
    218   1.1   thorpej 	else if (DIO_ISDIOII(scode))
    219   1.2   thorpej 		rval = DIOII_BASE + ((scode - DIOII_SCBASE) * DIOII_DEVSIZE);
    220   1.1   thorpej 	else
    221   1.1   thorpej 		rval = 0;
    222   1.1   thorpej 
    223   1.1   thorpej 	return ((void *)rval);
    224   1.1   thorpej }
    225   1.1   thorpej 
    226   1.1   thorpej /*
    227   1.1   thorpej  * Return the select code size for this device, defaulting to 1
    228   1.1   thorpej  * if we don't know what kind of device we have.
    229   1.1   thorpej  */
    230   1.1   thorpej int
    231   1.1   thorpej dio_scodesize(da)
    232   1.1   thorpej 	struct dio_attach_args *da;
    233   1.1   thorpej {
    234   1.1   thorpej 	int i;
    235   1.1   thorpej 
    236   1.1   thorpej 	/*
    237   1.1   thorpej 	 * Deal with lame internal HP-IB controllers which don't have
    238   1.1   thorpej 	 * consistent/reliable device ids.
    239   1.1   thorpej 	 */
    240   1.1   thorpej 	if (da->da_scode == 7 && internalhpib)
    241   1.1   thorpej 		return (1);
    242   1.1   thorpej 
    243   1.1   thorpej 	/*
    244   1.1   thorpej 	 * Find the dio_devdata matchind the primary id.
    245   1.1   thorpej 	 * If we're a framebuffer, we also check the secondary id.
    246   1.1   thorpej 	 */
    247   1.1   thorpej 	for (i = 0; i < DIO_NDEVICES; i++) {
    248   1.1   thorpej 		if (da->da_id == dio_devdatas[i].dd_id) {
    249   1.1   thorpej 			if (DIO_ISFRAMEBUFFER(da->da_id)) {
    250   1.1   thorpej 				if (da->da_secid == dio_devdatas[i].dd_secid) {
    251   1.1   thorpej 					goto foundit;
    252   1.1   thorpej 				}
    253   1.1   thorpej 			} else {
    254   1.1   thorpej 			foundit:
    255   1.1   thorpej 				return (dio_devdatas[i].dd_nscode);
    256   1.1   thorpej 			}
    257   1.1   thorpej 		}
    258   1.1   thorpej 	}
    259   1.1   thorpej 
    260   1.1   thorpej 	/*
    261   1.1   thorpej 	 * Device is unknown.  Print a warning and assume a default.
    262   1.1   thorpej 	 */
    263   1.1   thorpej 	printf("WARNING: select code size unknown for id = 0x%x secid = 0x%x\n",
    264   1.1   thorpej 	    da->da_id, da->da_secid);
    265   1.1   thorpej 	return (1);
    266   1.1   thorpej }
    267   1.1   thorpej 
    268   1.1   thorpej /*
    269   1.1   thorpej  * Return a reasonable description of a DIO device.
    270   1.1   thorpej  */
    271   1.1   thorpej char *
    272   1.1   thorpej dio_devinfo(da, buf, buflen)
    273   1.1   thorpej 	struct dio_attach_args *da;
    274   1.1   thorpej 	char *buf;
    275   1.1   thorpej 	size_t buflen;
    276   1.1   thorpej {
    277   1.5   thorpej #ifdef DIOVERBOSE
    278   1.1   thorpej 	int i;
    279   1.5   thorpej #endif
    280   1.1   thorpej 
    281  1.14  gmcgarry 	memset(buf, 0, buflen);
    282   1.1   thorpej 
    283   1.1   thorpej 	/*
    284   1.1   thorpej 	 * Deal with lame internal HP-IB controllers which don't have
    285   1.1   thorpej 	 * consistent/reliable device ids.
    286   1.1   thorpej 	 */
    287   1.1   thorpej 	if (da->da_scode == 7 && internalhpib) {
    288   1.1   thorpej 		sprintf(buf, DIO_DEVICE_DESC_IHPIB);
    289   1.1   thorpej 		return (buf);
    290   1.1   thorpej 	}
    291   1.1   thorpej 
    292   1.1   thorpej #ifdef DIOVERBOSE
    293   1.1   thorpej 	/*
    294   1.1   thorpej 	 * Find the description matching our primary id.
    295   1.1   thorpej 	 * If we're a framebuffer, we also check the secondary id.
    296   1.1   thorpej 	 */
    297   1.1   thorpej 	for (i = 0; i < DIO_NDEVICES; i++) {
    298   1.1   thorpej 		if (da->da_id == dio_devdescs[i].dd_id) {
    299   1.1   thorpej 			if (DIO_ISFRAMEBUFFER(da->da_id)) {
    300   1.1   thorpej 				if (da->da_secid == dio_devdescs[i].dd_secid) {
    301   1.1   thorpej 					goto foundit;
    302   1.1   thorpej 				}
    303   1.1   thorpej 			} else {
    304   1.1   thorpej 			foundit:
    305   1.1   thorpej 				sprintf(buf, "%s", dio_devdescs[i].dd_desc);
    306   1.1   thorpej 				return (buf);
    307   1.1   thorpej 			}
    308   1.1   thorpej 		}
    309   1.1   thorpej 	}
    310   1.1   thorpej #endif /* DIOVERBOSE */
    311   1.1   thorpej 
    312   1.1   thorpej 	/*
    313   1.1   thorpej 	 * Device is unknown.  Construct something reasonable.
    314   1.1   thorpej 	 */
    315   1.1   thorpej 	sprintf(buf, "device id = 0x%x secid = 0x%x",
    316   1.1   thorpej 	    da->da_id, da->da_secid);
    317   1.1   thorpej 	return (buf);
    318   1.7   thorpej }
    319   1.7   thorpej 
    320   1.7   thorpej /*
    321   1.7   thorpej  * Establish an interrupt handler for a DIO device.
    322   1.7   thorpej  */
    323   1.7   thorpej void *
    324   1.7   thorpej dio_intr_establish(func, arg, ipl, priority)
    325   1.7   thorpej 	int (*func) __P((void *));
    326   1.7   thorpej 	void *arg;
    327   1.7   thorpej 	int ipl;
    328   1.7   thorpej 	int priority;
    329   1.7   thorpej {
    330   1.7   thorpej 	void *ih;
    331   1.7   thorpej 
    332   1.7   thorpej 	ih = intr_establish(func, arg, ipl, priority);
    333   1.7   thorpej 
    334   1.7   thorpej 	if (priority == IPL_BIO)
    335   1.7   thorpej 		dmacomputeipl();
    336   1.7   thorpej 
    337   1.7   thorpej 	return (ih);
    338   1.7   thorpej }
    339   1.7   thorpej 
    340   1.7   thorpej /*
    341   1.7   thorpej  * Remove an interrupt handler for a DIO device.
    342   1.7   thorpej  */
    343   1.7   thorpej void
    344   1.7   thorpej dio_intr_disestablish(arg)
    345   1.7   thorpej 	void *arg;
    346   1.7   thorpej {
    347  1.14  gmcgarry 	struct hp300_intrhand *ih = arg;
    348  1.14  gmcgarry 	int priority = ih->ih_priority;
    349   1.7   thorpej 
    350   1.7   thorpej 	intr_disestablish(arg);
    351   1.7   thorpej 
    352   1.7   thorpej 	if (priority == IPL_BIO)
    353   1.7   thorpej 		dmacomputeipl();
    354   1.1   thorpej }
    355