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