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