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