xref: /netbsd-src/sys/arch/macppc/dev/adb.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: adb.c,v 1.27 2009/03/14 21:04:11 dsl 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.27 2009/03/14 21:04:11 dsl 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 #include <machine/pio.h>
48 
49 #include <macppc/dev/adbvar.h>
50 #include <macppc/dev/akbdvar.h>
51 #include <macppc/dev/pm_direct.h>
52 #include <macppc/dev/viareg.h>
53 
54 #include <dev/clock_subr.h>
55 #include <dev/ofw/openfirm.h>
56 
57 #include "aed.h"
58 #include "apm.h"
59 
60 /*
61  * Function declarations.
62  */
63 static int	adbmatch(struct device *, struct cfdata *, void *);
64 static void	adbattach(struct device *, struct device *, void *);
65 static int	adbprint(void *, const char *);
66 static void	adb_todr_init(void);
67 
68 /*
69  * Global variables.
70  */
71 int     adb_polling = 0;	/* Are we polling?  (Debugger mode) */
72 int     adb_initted = 0;	/* adb_init() has completed successfully */
73 #ifdef ADB_DEBUG
74 int	adb_debug = 0;		/* Output debugging messages */
75 #endif /* ADB_DEBUG */
76 
77 /*
78  * Driver definition.
79  */
80 CFATTACH_DECL(adb, sizeof(struct adb_softc),
81     adbmatch, adbattach, NULL, NULL);
82 
83 static int
84 adbmatch(struct device *parent, struct cfdata *cf, void *aux)
85 {
86 	struct confargs *ca = aux;
87 
88 	if (ca->ca_nreg < 8)
89 		return 0;
90 
91 	if (ca->ca_nintr < 4)
92 		return 0;
93 
94 	if (strcmp(ca->ca_name, "via-cuda") == 0)
95 		return 1;
96 
97 	if (strcmp(ca->ca_name, "via-pmu") == 0)
98 		return 1;
99 
100 	return 0;
101 }
102 
103 static void
104 adbattach(struct device *parent, struct device *self, void *aux)
105 {
106 	struct adb_softc *sc = (struct adb_softc *)self;
107 	struct confargs *ca = aux;
108 	int irq = ca->ca_intr[0];
109 	int node;
110 	ADBDataBlock adbdata;
111 	struct adb_attach_args aa_args;
112 	int totaladbs;
113 	int adbindex, adbaddr, adb_node;
114 
115 	extern volatile u_char *Via1Base;
116 
117 	ca->ca_reg[0] += ca->ca_baseaddr;
118 
119 	sc->sc_regbase = mapiodev(ca->ca_reg[0], ca->ca_reg[1]);
120 	Via1Base = sc->sc_regbase;
121 
122 	if (strcmp(ca->ca_name, "via-cuda") == 0)
123 		adbHardware = ADB_HW_CUDA;
124 	else if (strcmp(ca->ca_name, "via-pmu") == 0)
125 		adbHardware = ADB_HW_PMU;
126 
127 	node = of_getnode_byname(OF_parent(ca->ca_node), "extint-gpio1");
128 	if (node)
129 		OF_getprop(node, "interrupts", &irq, 4);
130 
131 	printf(" irq %d: ", irq);
132 
133 	adb_polling = 1;
134 	adb_node = of_getnode_byname(ca->ca_node, "adb");
135 	if (adb_node)
136 		ADBReInit();
137 
138 	switch (adbHardware) {
139 	case ADB_HW_CUDA:
140 		intr_establish(irq, IST_LEVEL, IPL_TTY, adb_intr_cuda, sc);
141 		break;
142 	case ADB_HW_PMU:
143 		intr_establish(irq, IST_LEVEL, IPL_TTY, pm_intr, sc);
144 		pm_init();
145 		break;
146 	}
147 
148 	adb_todr_init();
149 
150 #if NAPM > 0
151 	/* Magic for signalling the apm driver to match. */
152 	aa_args.origaddr = ADBADDR_APM;
153 	aa_args.adbaddr = ADBADDR_APM;
154 	aa_args.handler_id = ADBADDR_APM;
155 
156 	(void)config_found(self, &aa_args, NULL);
157 #endif
158 
159 	/*
160 	 * see if we're supposed to have an ADB bus
161 	 * since some PowerBooks don't have one and their PMUs barf on ADB
162 	 * commands we bail here if there's no adb node
163 	 */
164 	if (!adb_node)
165 		return;
166 
167 #ifdef ADB_DEBUG
168 	if (adb_debug)
169 		printf("adb: done with ADBReInit\n");
170 #endif
171 	totaladbs = CountADBs();
172 
173 	printf("%d targets\n", totaladbs);
174 
175 #if NAED > 0
176 	/* ADB event device for compatibility */
177 	aa_args.origaddr = 0;
178 	aa_args.adbaddr = 0;
179 	aa_args.handler_id = 0;
180 	(void)config_found(self, &aa_args, adbprint);
181 #endif
182 
183 	/* for each ADB device */
184 	for (adbindex = 1; adbindex <= totaladbs; adbindex++) {
185 		/* Get the ADB information */
186 		adbaddr = GetIndADB(&adbdata, adbindex);
187 
188 		aa_args.origaddr = adbdata.origADBAddr;
189 		aa_args.adbaddr = adbaddr;
190 		aa_args.handler_id = adbdata.devType;
191 
192 		(void)config_found(self, &aa_args, adbprint);
193 	}
194 
195 	if (adbHardware == ADB_HW_CUDA)
196 		adb_cuda_autopoll();
197 	adb_polling = 0;
198 
199 }
200 
201 int
202 adbprint(void *args, const char *name)
203 {
204 	struct adb_attach_args *aa_args = (struct adb_attach_args *)args;
205 	int rv = UNCONF;
206 
207 	if (name) {	/* no configured device matched */
208 		rv = UNSUPP; /* most ADB device types are unsupported */
209 
210 		/* print out what kind of ADB device we have found */
211 		aprint_normal("%s addr %d: ", name, aa_args->adbaddr);
212 		switch(aa_args->origaddr) {
213 #ifdef DIAGNOSTIC
214 		case 0:
215 			aprint_normal("ADB event device");
216 			rv = UNCONF;
217 			break;
218 		case ADBADDR_SECURE:
219 			aprint_normal("security dongle (%d)",
220 			    aa_args->handler_id);
221 			break;
222 #endif
223 		case ADBADDR_MAP:
224 			aprint_normal("mapped device (%d)",
225 			    aa_args->handler_id);
226 			rv = UNCONF;
227 			break;
228 		case ADBADDR_REL:
229 			aprint_normal("relative positioning device (%d)",
230 			    aa_args->handler_id);
231 			rv = UNCONF;
232 			break;
233 #ifdef DIAGNOSTIC
234 		case ADBADDR_ABS:
235 			switch (aa_args->handler_id) {
236 			case ADB_ARTPAD:
237 				aprint_normal("WACOM ArtPad II");
238 				break;
239 			default:
240 				aprint_normal("absolute positioning device (%d)",
241 				    aa_args->handler_id);
242 				break;
243 			}
244 			break;
245 		case ADBADDR_DATATX:
246 			aprint_normal("data transfer device (modem?) (%d)",
247 			    aa_args->handler_id);
248 			break;
249 		case ADBADDR_MISC:
250 			switch (aa_args->handler_id) {
251 			case ADB_POWERKEY:
252 				aprint_normal("Sophisticated Circuits PowerKey");
253 				break;
254 			default:
255 				aprint_normal("misc. device (remote control?) (%d)",
256 				    aa_args->handler_id);
257 				break;
258 			}
259 			break;
260 		default:
261 			aprint_normal("unknown type device, (handler %d)",
262 			    aa_args->handler_id);
263 			break;
264 #endif /* DIAGNOSTIC */
265 		}
266 	} else		/* a device matched and was configured */
267                 aprint_normal(" addr %d: ", aa_args->adbaddr);
268 
269 	return rv;
270 }
271 
272 #define DIFF19041970 2082844800
273 
274 static int
275 adb_todr_get(todr_chip_handle_t tch, volatile struct timeval *tvp)
276 {
277 	unsigned long sec;
278 
279 	if (adb_read_date_time(&sec) != 0)
280 		return EIO;
281 	tvp->tv_sec = sec - DIFF19041970;
282 	tvp->tv_usec = 0;
283 	return 0;
284 }
285 
286 static int
287 adb_todr_set(todr_chip_handle_t tch, volatile struct timeval *tvp)
288 {
289 	unsigned long sec;
290 
291 	sec = tvp->tv_sec + DIFF19041970;
292 	return adb_set_date_time(sec) ? EIO : 0;
293 }
294 
295 void
296 adb_todr_init(void)
297 {
298 	static struct todr_chip_handle tch = {
299 		.todr_gettime = adb_todr_get,
300 		.todr_settime = adb_todr_set
301 	};
302 
303 	todr_attach(&tch);
304 }
305