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