xref: /netbsd-src/sys/arch/macppc/dev/adb.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: adb.c,v 1.29 2009/12/12 14:44:09 tsutsui 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.29 2009/12/12 14:44:09 tsutsui 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 <machine/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(struct device *, struct cfdata *, void *);
59 static void	adbattach(struct device *, struct device *, 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(adb, sizeof(struct adb_softc),
76     adbmatch, adbattach, NULL, NULL);
77 
78 static int
79 adbmatch(struct device *parent, struct cfdata *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(struct device *parent, struct device *self, void *aux)
100 {
101 	struct adb_softc *sc = (struct adb_softc *)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 u_char *Via1Base;
111 
112 	ca->ca_reg[0] += ca->ca_baseaddr;
113 
114 	sc->sc_regbase = mapiodev(ca->ca_reg[0], ca->ca_reg[1]);
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(irq, IST_LEVEL, IPL_TTY, adb_intr_cuda, sc);
136 		break;
137 	case ADB_HW_PMU:
138 		intr_establish(irq, IST_LEVEL, IPL_TTY, pm_intr, sc);
139 		pm_init();
140 		break;
141 	}
142 
143 	adb_todr_init();
144 
145 #if NAPM > 0
146 	/* Magic for signalling the apm driver to match. */
147 	aa_args.origaddr = ADBADDR_APM;
148 	aa_args.adbaddr = ADBADDR_APM;
149 	aa_args.handler_id = ADBADDR_APM;
150 
151 	(void)config_found(self, &aa_args, NULL);
152 #endif
153 
154 	/*
155 	 * see if we're supposed to have an ADB bus
156 	 * since some PowerBooks don't have one and their PMUs barf on ADB
157 	 * commands we bail here if there's no adb node
158 	 */
159 	if (!adb_node)
160 		return;
161 
162 #ifdef ADB_DEBUG
163 	if (adb_debug)
164 		printf("adb: done with ADBReInit\n");
165 #endif
166 	totaladbs = CountADBs();
167 
168 	printf("%d targets\n", totaladbs);
169 
170 #if NAED > 0
171 	/* ADB event device for compatibility */
172 	aa_args.origaddr = 0;
173 	aa_args.adbaddr = 0;
174 	aa_args.handler_id = 0;
175 	(void)config_found(self, &aa_args, adbprint);
176 #endif
177 
178 	/* for each ADB device */
179 	for (adbindex = 1; adbindex <= totaladbs; adbindex++) {
180 		/* Get the ADB information */
181 		adbaddr = GetIndADB(&adbdata, adbindex);
182 
183 		aa_args.origaddr = adbdata.origADBAddr;
184 		aa_args.adbaddr = adbaddr;
185 		aa_args.handler_id = adbdata.devType;
186 
187 		(void)config_found(self, &aa_args, adbprint);
188 	}
189 
190 	if (adbHardware == ADB_HW_CUDA)
191 		adb_cuda_autopoll();
192 	adb_polling = 0;
193 
194 }
195 
196 int
197 adbprint(void *args, const char *name)
198 {
199 	struct adb_attach_args *aa_args = (struct adb_attach_args *)args;
200 	int rv = UNCONF;
201 
202 	if (name) {	/* no configured device matched */
203 		rv = UNSUPP; /* most ADB device types are unsupported */
204 
205 		/* print out what kind of ADB device we have found */
206 		aprint_normal("%s addr %d: ", name, aa_args->adbaddr);
207 		switch(aa_args->origaddr) {
208 #ifdef DIAGNOSTIC
209 		case 0:
210 			aprint_normal("ADB event device");
211 			rv = UNCONF;
212 			break;
213 		case ADBADDR_SECURE:
214 			aprint_normal("security dongle (%d)",
215 			    aa_args->handler_id);
216 			break;
217 #endif
218 		case ADBADDR_MAP:
219 			aprint_normal("mapped device (%d)",
220 			    aa_args->handler_id);
221 			rv = UNCONF;
222 			break;
223 		case ADBADDR_REL:
224 			aprint_normal("relative positioning device (%d)",
225 			    aa_args->handler_id);
226 			rv = UNCONF;
227 			break;
228 #ifdef DIAGNOSTIC
229 		case ADBADDR_ABS:
230 			switch (aa_args->handler_id) {
231 			case ADB_ARTPAD:
232 				aprint_normal("WACOM ArtPad II");
233 				break;
234 			default:
235 				aprint_normal("absolute positioning device (%d)",
236 				    aa_args->handler_id);
237 				break;
238 			}
239 			break;
240 		case ADBADDR_DATATX:
241 			aprint_normal("data transfer device (modem?) (%d)",
242 			    aa_args->handler_id);
243 			break;
244 		case ADBADDR_MISC:
245 			switch (aa_args->handler_id) {
246 			case ADB_POWERKEY:
247 				aprint_normal("Sophisticated Circuits PowerKey");
248 				break;
249 			default:
250 				aprint_normal("misc. device (remote control?) (%d)",
251 				    aa_args->handler_id);
252 				break;
253 			}
254 			break;
255 		default:
256 			aprint_normal("unknown type device, (handler %d)",
257 			    aa_args->handler_id);
258 			break;
259 #endif /* DIAGNOSTIC */
260 		}
261 	} else		/* a device matched and was configured */
262                 aprint_normal(" addr %d: ", aa_args->adbaddr);
263 
264 	return rv;
265 }
266 
267 #define DIFF19041970 2082844800
268 
269 static int
270 adb_todr_get(todr_chip_handle_t tch, struct timeval *tvp)
271 {
272 	unsigned long sec;
273 
274 	if (adb_read_date_time(&sec) != 0)
275 		return EIO;
276 	tvp->tv_sec = sec - DIFF19041970;
277 	tvp->tv_usec = 0;
278 	return 0;
279 }
280 
281 static int
282 adb_todr_set(todr_chip_handle_t tch, struct timeval *tvp)
283 {
284 	unsigned long sec;
285 
286 	sec = tvp->tv_sec + DIFF19041970;
287 	return adb_set_date_time(sec) ? EIO : 0;
288 }
289 
290 void
291 adb_todr_init(void)
292 {
293 	static struct todr_chip_handle tch = {
294 		.todr_gettime = adb_todr_get,
295 		.todr_settime = adb_todr_set
296 	};
297 
298 	todr_attach(&tch);
299 }
300