Home | History | Annotate | Line # | Download | only in dev
adb.c revision 1.21
      1 /*	$NetBSD: adb.c,v 1.21 2006/12/10 19:28:12 macallan Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 1994	Bradley A. Grantham
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Bradley A. Grantham.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: adb.c,v 1.21 2006/12/10 19:28:12 macallan Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/device.h>
     38 #include <sys/fcntl.h>
     39 #include <sys/poll.h>
     40 #include <sys/select.h>
     41 #include <sys/proc.h>
     42 #include <sys/signalvar.h>
     43 #include <sys/systm.h>
     44 
     45 #include <machine/bus.h>
     46 #include <machine/autoconf.h>
     47 
     48 #include <macppc/dev/adbvar.h>
     49 #include <macppc/dev/akbdvar.h>
     50 #include <macppc/dev/pm_direct.h>
     51 #include <macppc/dev/viareg.h>
     52 
     53 #include <dev/clock_subr.h>
     54 #include <dev/ofw/openfirm.h>
     55 
     56 #include "aed.h"
     57 #include "apm.h"
     58 
     59 /*
     60  * Function declarations.
     61  */
     62 static int	adbmatch __P((struct device *, struct cfdata *, void *));
     63 static void	adbattach __P((struct device *, struct device *, void *));
     64 static int	adbprint __P((void *, const char *));
     65 static void	adb_todr_init(void);
     66 
     67 /*
     68  * Global variables.
     69  */
     70 int     adb_polling = 0;	/* Are we polling?  (Debugger mode) */
     71 int     adb_initted = 0;	/* adb_init() has completed successfully */
     72 #ifdef ADB_DEBUG
     73 int	adb_debug = 0;		/* Output debugging messages */
     74 #endif /* ADB_DEBUG */
     75 
     76 /*
     77  * Driver definition.
     78  */
     79 CFATTACH_DECL(adb, sizeof(struct adb_softc),
     80     adbmatch, adbattach, NULL, NULL);
     81 
     82 static int
     83 adbmatch(parent, cf, aux)
     84 	struct device *parent;
     85 	struct cfdata *cf;
     86 	void *aux;
     87 {
     88 	struct confargs *ca = aux;
     89 
     90 	if (ca->ca_nreg < 8)
     91 		return 0;
     92 
     93 	if (ca->ca_nintr < 4)
     94 		return 0;
     95 
     96 	if (strcmp(ca->ca_name, "via-cuda") == 0)
     97 		return 1;
     98 
     99 	if (strcmp(ca->ca_name, "via-pmu") == 0)
    100 		return 1;
    101 
    102 	return 0;
    103 }
    104 
    105 static void
    106 adbattach(parent, self, aux)
    107 	struct device *parent, *self;
    108 	void   *aux;
    109 {
    110 	struct adb_softc *sc = (struct adb_softc *)self;
    111 	struct confargs *ca = aux;
    112 	int irq = ca->ca_intr[0];
    113 	int node;
    114 	ADBDataBlock adbdata;
    115 	struct adb_attach_args aa_args;
    116 	int totaladbs;
    117 	int adbindex, adbaddr, adb_node;
    118 
    119 	extern volatile u_char *Via1Base;
    120 
    121 	ca->ca_reg[0] += ca->ca_baseaddr;
    122 
    123 	sc->sc_regbase = mapiodev(ca->ca_reg[0], ca->ca_reg[1]);
    124 	Via1Base = sc->sc_regbase;
    125 
    126 	if (strcmp(ca->ca_name, "via-cuda") == 0)
    127 		adbHardware = ADB_HW_CUDA;
    128 	else if (strcmp(ca->ca_name, "via-pmu") == 0)
    129 		adbHardware = ADB_HW_PMU;
    130 
    131 	node = getnodebyname(OF_parent(ca->ca_node), "extint-gpio1");
    132 	if (node)
    133 		OF_getprop(node, "interrupts", &irq, 4);
    134 
    135 	printf(" irq %d: ", irq);
    136 
    137 	adb_polling = 1;
    138 	adb_node = getnodebyname(ca->ca_node, "adb");
    139 	if (adb_node)
    140 		ADBReInit();
    141 
    142 	switch (adbHardware) {
    143 	case ADB_HW_CUDA:
    144 		intr_establish(irq, IST_LEVEL, IPL_HIGH, adb_intr_cuda, sc);
    145 		break;
    146 	case ADB_HW_PMU:
    147 		intr_establish(irq, IST_LEVEL, IPL_HIGH, pm_intr, sc);
    148 		pm_init();
    149 		break;
    150 	}
    151 
    152 	adb_todr_init();
    153 
    154 #if NAPM > 0
    155 	/* Magic for signalling the apm driver to match. */
    156 	aa_args.origaddr = ADBADDR_APM;
    157 	aa_args.adbaddr = ADBADDR_APM;
    158 	aa_args.handler_id = ADBADDR_APM;
    159 
    160 	(void)config_found(self, &aa_args, NULL);
    161 #endif
    162 
    163 	/*
    164 	 * see if we're supposed to have an ADB bus
    165 	 * since some PowerBooks don't have one and their PMUs barf on ADB
    166 	 * commands we bail here if there's no adb node
    167 	 */
    168 	if (!adb_node)
    169 		return;
    170 
    171 #ifdef ADB_DEBUG
    172 	if (adb_debug)
    173 		printf("adb: done with ADBReInit\n");
    174 #endif
    175 	totaladbs = CountADBs();
    176 
    177 	printf("%d targets\n", totaladbs);
    178 
    179 #if NAED > 0
    180 	/* ADB event device for compatibility */
    181 	aa_args.origaddr = 0;
    182 	aa_args.adbaddr = 0;
    183 	aa_args.handler_id = 0;
    184 	(void)config_found(self, &aa_args, adbprint);
    185 #endif
    186 
    187 	/* for each ADB device */
    188 	for (adbindex = 1; adbindex <= totaladbs; adbindex++) {
    189 		/* Get the ADB information */
    190 		adbaddr = GetIndADB(&adbdata, adbindex);
    191 
    192 		aa_args.origaddr = adbdata.origADBAddr;
    193 		aa_args.adbaddr = adbaddr;
    194 		aa_args.handler_id = adbdata.devType;
    195 
    196 		(void)config_found(self, &aa_args, adbprint);
    197 	}
    198 
    199 	if (adbHardware == ADB_HW_CUDA)
    200 		adb_cuda_autopoll();
    201 	adb_polling = 0;
    202 
    203 }
    204 
    205 int
    206 adbprint(args, name)
    207 	void *args;
    208 	const char *name;
    209 {
    210 	struct adb_attach_args *aa_args = (struct adb_attach_args *)args;
    211 	int rv = UNCONF;
    212 
    213 	if (name) {	/* no configured device matched */
    214 		rv = UNSUPP; /* most ADB device types are unsupported */
    215 
    216 		/* print out what kind of ADB device we have found */
    217 		aprint_normal("%s addr %d: ", name, aa_args->adbaddr);
    218 		switch(aa_args->origaddr) {
    219 #ifdef DIAGNOSTIC
    220 		case 0:
    221 			aprint_normal("ADB event device");
    222 			rv = UNCONF;
    223 			break;
    224 		case ADBADDR_SECURE:
    225 			aprint_normal("security dongle (%d)",
    226 			    aa_args->handler_id);
    227 			break;
    228 #endif
    229 		case ADBADDR_MAP:
    230 			aprint_normal("mapped device (%d)",
    231 			    aa_args->handler_id);
    232 			rv = UNCONF;
    233 			break;
    234 		case ADBADDR_REL:
    235 			aprint_normal("relative positioning device (%d)",
    236 			    aa_args->handler_id);
    237 			rv = UNCONF;
    238 			break;
    239 #ifdef DIAGNOSTIC
    240 		case ADBADDR_ABS:
    241 			switch (aa_args->handler_id) {
    242 			case ADB_ARTPAD:
    243 				aprint_normal("WACOM ArtPad II");
    244 				break;
    245 			default:
    246 				aprint_normal("absolute positioning device (%d)",
    247 				    aa_args->handler_id);
    248 				break;
    249 			}
    250 			break;
    251 		case ADBADDR_DATATX:
    252 			aprint_normal("data transfer device (modem?) (%d)",
    253 			    aa_args->handler_id);
    254 			break;
    255 		case ADBADDR_MISC:
    256 			switch (aa_args->handler_id) {
    257 			case ADB_POWERKEY:
    258 				aprint_normal("Sophisticated Circuits PowerKey");
    259 				break;
    260 			default:
    261 				aprint_normal("misc. device (remote control?) (%d)",
    262 				    aa_args->handler_id);
    263 				break;
    264 			}
    265 			break;
    266 		default:
    267 			aprint_normal("unknown type device, (handler %d)",
    268 			    aa_args->handler_id);
    269 			break;
    270 #endif /* DIAGNOSTIC */
    271 		}
    272 	} else		/* a device matched and was configured */
    273                 aprint_normal(" addr %d: ", aa_args->adbaddr);
    274 
    275 	return rv;
    276 }
    277 
    278 #define DIFF19041970 2082844800
    279 
    280 static int
    281 adb_todr_get(todr_chip_handle_t tch, volatile struct timeval *tvp)
    282 {
    283 	unsigned long sec;
    284 
    285 	if (adb_read_date_time(&sec) != 0)
    286 		return EIO;
    287 	tvp->tv_sec = sec - DIFF19041970;
    288 	tvp->tv_usec = 0;
    289 	return 0;
    290 }
    291 
    292 static int
    293 adb_todr_set(todr_chip_handle_t tch, volatile struct timeval *tvp)
    294 {
    295 	unsigned long sec;
    296 
    297 	sec = tvp->tv_sec + DIFF19041970;
    298 	return adb_set_date_time(sec) ? EIO : 0;
    299 }
    300 
    301 void
    302 adb_todr_init(void)
    303 {
    304 	static struct todr_chip_handle tch = {
    305 		.todr_gettime = adb_todr_get,
    306 		.todr_settime = adb_todr_set
    307 	};
    308 
    309 	todr_attach(&tch);
    310 }
    311