xref: /openbsd-src/sys/arch/hppa/gsc/gsckbc.c (revision 0c77ed638bd446250a35aa709acff10176d4c273)
1*0c77ed63Smiod /*	$OpenBSD: gsckbc.c,v 1.22 2023/07/25 10:00:44 miod Exp $	*/
2b23d0fcaSmiod /*
3b23d0fcaSmiod  * Copyright (c) 2003, Miodrag Vallat.
4b23d0fcaSmiod  * All rights reserved.
5b23d0fcaSmiod  *
6b23d0fcaSmiod  * Redistribution and use in source and binary forms, with or without
7b23d0fcaSmiod  * modification, are permitted provided that the following conditions
8b23d0fcaSmiod  * are met:
9b23d0fcaSmiod  * 1. Redistributions of source code must retain the above copyright
10b23d0fcaSmiod  *    notice, this list of conditions and the following disclaimer.
11b23d0fcaSmiod  * 2. Redistributions in binary form must reproduce the above copyright
12b23d0fcaSmiod  *    notice, this list of conditions and the following disclaimer in the
13b23d0fcaSmiod  *    documentation and/or other materials provided with the distribution.
14b23d0fcaSmiod  *
15b23d0fcaSmiod  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16b23d0fcaSmiod  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17b23d0fcaSmiod  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18b23d0fcaSmiod  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19b23d0fcaSmiod  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20b23d0fcaSmiod  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF MIND,
21b23d0fcaSmiod  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22b23d0fcaSmiod  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23b23d0fcaSmiod  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24b23d0fcaSmiod  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25b23d0fcaSmiod  */
26b23d0fcaSmiod 
27b23d0fcaSmiod /*
28b23d0fcaSmiod  * Derived from /sys/dev/ic/pckbd.c under the following terms:
29b23d0fcaSmiod  * OpenBSD: pckbc.c,v 1.5 2002/06/09 00:58:03 nordin Exp
30b23d0fcaSmiod  * NetBSD: pckbc.c,v 1.5 2000/06/09 04:58:35 soda Exp
31b23d0fcaSmiod  */
32b23d0fcaSmiod /*
33b23d0fcaSmiod  * Copyright (c) 1998
34b23d0fcaSmiod  *	Matthias Drochner.  All rights reserved.
35b23d0fcaSmiod  *
36b23d0fcaSmiod  * Redistribution and use in source and binary forms, with or without
37b23d0fcaSmiod  * modification, are permitted provided that the following conditions
38b23d0fcaSmiod  * are met:
39b23d0fcaSmiod  * 1. Redistributions of source code must retain the above copyright
40b23d0fcaSmiod  *    notice, this list of conditions and the following disclaimer.
41b23d0fcaSmiod  * 2. Redistributions in binary form must reproduce the above copyright
42b23d0fcaSmiod  *    notice, this list of conditions and the following disclaimer in the
43b23d0fcaSmiod  *    documentation and/or other materials provided with the distribution.
44b23d0fcaSmiod  * 3. All advertising materials mentioning features or use of this software
45b23d0fcaSmiod  *    must display the following acknowledgement:
46b23d0fcaSmiod  *	This product includes software developed for the NetBSD Project
47b23d0fcaSmiod  *	by Matthias Drochner.
48b23d0fcaSmiod  * 4. The name of the author may not be used to endorse or promote products
49b23d0fcaSmiod  *    derived from this software without specific prior written permission.
50b23d0fcaSmiod  *
51b23d0fcaSmiod  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52b23d0fcaSmiod  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53b23d0fcaSmiod  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54b23d0fcaSmiod  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
55b23d0fcaSmiod  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56b23d0fcaSmiod  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57b23d0fcaSmiod  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58b23d0fcaSmiod  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59b23d0fcaSmiod  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60b23d0fcaSmiod  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61b23d0fcaSmiod  */
62b23d0fcaSmiod 
63b23d0fcaSmiod /*
64b23d0fcaSmiod  * Driver for the PS/2-like keyboard and mouse ports found on 712 and 715
65b23d0fcaSmiod  * models, among others.
66b23d0fcaSmiod  *
67b23d0fcaSmiod  * Contrary to the ``pckbc'' port set found on other arches, the
68b23d0fcaSmiod  * keyboard and mouse port are two separate entities on the snakes, and
69b23d0fcaSmiod  * they are driven by a custom chip not 8042-compatible.
70b23d0fcaSmiod  */
71b23d0fcaSmiod 
7294fe7638Sjsg #include "pckbd.h"
7394fe7638Sjsg 
74b23d0fcaSmiod #include <sys/param.h>
75b23d0fcaSmiod #include <sys/systm.h>
76b23d0fcaSmiod #include <sys/device.h>
77b23d0fcaSmiod #include <sys/kernel.h>
78b23d0fcaSmiod #include <sys/malloc.h>
79b23d0fcaSmiod #include <sys/proc.h>
80b23d0fcaSmiod 
81f307151fSmiod #include <machine/autoconf.h>
82b23d0fcaSmiod #include <machine/bus.h>
83b23d0fcaSmiod #include <machine/intr.h>
84f307151fSmiod #include <machine/iomod.h>
85b23d0fcaSmiod 
86b23d0fcaSmiod #include <hppa/dev/cpudevs.h>
87b23d0fcaSmiod #include <hppa/gsc/gscbusvar.h>
88b23d0fcaSmiod 
89b23d0fcaSmiod #include <hppa/gsc/gsckbcreg.h>
90b23d0fcaSmiod #include <dev/ic/pckbcvar.h>
91b23d0fcaSmiod 
92b23d0fcaSmiod #include <dev/pckbc/pckbdreg.h>	/* constants for probe magic */
932415bd9bSmiod #include <dev/pckbc/pckbdvar.h>
94b23d0fcaSmiod 
95b23d0fcaSmiod int	gsckbc_match(struct device *, void *, void *);
96b23d0fcaSmiod void	gsckbc_attach(struct device *, struct device *, void *);
97b23d0fcaSmiod 
98b23d0fcaSmiod struct	gsckbc_softc {
99b23d0fcaSmiod 	struct pckbc_softc sc_pckbc;
100b23d0fcaSmiod 
101b23d0fcaSmiod 	void *sc_ih;
102b23d0fcaSmiod 	int sc_type;
103b23d0fcaSmiod };
104b23d0fcaSmiod 
10578d5ff0eSmpi const struct cfattach gsckbc_ca = {
106b23d0fcaSmiod 	sizeof(struct gsckbc_softc), gsckbc_match, gsckbc_attach
107b23d0fcaSmiod };
108b23d0fcaSmiod 
109b23d0fcaSmiod struct cfdriver gsckbc_cd = {
110b23d0fcaSmiod 	NULL, "gsckbc", DV_DULL
111b23d0fcaSmiod };
112b23d0fcaSmiod 
113b23d0fcaSmiod /* descriptor for one device command */
114b23d0fcaSmiod struct pckbc_devcmd {
115b23d0fcaSmiod 	TAILQ_ENTRY(pckbc_devcmd) next;
116b23d0fcaSmiod 	int flags;
117b23d0fcaSmiod #define KBC_CMDFLAG_SYNC 1 /* give descriptor back to caller */
118b23d0fcaSmiod #define KBC_CMDFLAG_SLOW 2
119b23d0fcaSmiod 	u_char cmd[4];
120b23d0fcaSmiod 	int cmdlen, cmdidx, retries;
121b23d0fcaSmiod 	u_char response[4];
122b23d0fcaSmiod 	int status, responselen, responseidx;
123b23d0fcaSmiod };
124b23d0fcaSmiod 
125b23d0fcaSmiod /* data per slave device */
126b23d0fcaSmiod struct pckbc_slotdata {
127b23d0fcaSmiod 	int polling; /* don't read data port in interrupt handler */
128b23d0fcaSmiod 	TAILQ_HEAD(, pckbc_devcmd) cmdqueue; /* active commands */
129b23d0fcaSmiod 	TAILQ_HEAD(, pckbc_devcmd) freequeue; /* free commands */
130b23d0fcaSmiod #define NCMD 5
131b23d0fcaSmiod 	struct pckbc_devcmd cmds[NCMD];
132b23d0fcaSmiod };
133b23d0fcaSmiod 
134b23d0fcaSmiod #define CMD_IN_QUEUE(q) (TAILQ_FIRST(&(q)->cmdqueue) != NULL)
135b23d0fcaSmiod /* Force polling mode behaviour for boot -a XXX */
136b23d0fcaSmiod #define IS_POLLING(q)	((q)->polling || cold)
137b23d0fcaSmiod 
138b23d0fcaSmiod void pckbc_init_slotdata(struct pckbc_slotdata *);
139b23d0fcaSmiod int pckbc_attach_slot(struct pckbc_softc *, pckbc_slot_t);
140b23d0fcaSmiod int pckbc_submatch(struct device *, void *, void *);
141b23d0fcaSmiod int pckbcprint(void *, const char *);
142b23d0fcaSmiod 
143b23d0fcaSmiod int pckbc_wait_output(bus_space_tag_t, bus_space_handle_t);
144b23d0fcaSmiod int pckbc_send_devcmd(struct pckbc_internal *, pckbc_slot_t,
145b23d0fcaSmiod 				  u_char);
146b23d0fcaSmiod void pckbc_poll_cmd1(struct pckbc_internal *, pckbc_slot_t,
147b23d0fcaSmiod 				 struct pckbc_devcmd *);
148b23d0fcaSmiod 
149b23d0fcaSmiod void pckbc_cleanqueue(struct pckbc_slotdata *);
150b23d0fcaSmiod void pckbc_cleanup(void *);
151b23d0fcaSmiod int pckbc_cmdresponse(struct pckbc_internal *, pckbc_slot_t, u_char);
152b23d0fcaSmiod void pckbc_start(struct pckbc_internal *, pckbc_slot_t);
1531f388c19Smickey int gsckbcintr(void *);
154b23d0fcaSmiod 
155b23d0fcaSmiod const char *pckbc_slot_names[] = { "kbd", "mouse" };
156b23d0fcaSmiod 
157b23d0fcaSmiod #define KBC_DEVCMD_ACK 0xfa
158b23d0fcaSmiod #define KBC_DEVCMD_RESEND 0xfe
159b23d0fcaSmiod 
160b23d0fcaSmiod #define	KBD_DELAY	DELAY(8)
161b23d0fcaSmiod 
162b23d0fcaSmiod int
gsckbc_match(struct device * parent,void * match,void * aux)163b23d0fcaSmiod gsckbc_match(struct device *parent, void *match, void *aux)
164b23d0fcaSmiod {
165b23d0fcaSmiod 	struct gsc_attach_args *ga = aux;
166b23d0fcaSmiod 	bus_space_handle_t ioh;
167b23d0fcaSmiod 	u_int8_t rv;
168b23d0fcaSmiod 
169b23d0fcaSmiod 	if (ga->ga_type.iodc_type != HPPA_TYPE_FIO ||
170b23d0fcaSmiod 	    ga->ga_type.iodc_sv_model != HPPA_FIO_GPCIO)
171b23d0fcaSmiod 		return (0);
172b23d0fcaSmiod 
173b23d0fcaSmiod 	/* Map the i/o space. */
174b23d0fcaSmiod 	if (bus_space_map(ga->ga_ca.ca_iot, ga->ga_ca.ca_hpa,
175b23d0fcaSmiod 	    KBMAPSIZE, 0, &ioh))
176b23d0fcaSmiod 		return 0;
177b23d0fcaSmiod 
178b23d0fcaSmiod 	rv = bus_space_read_1(ga->ga_ca.ca_iot, ioh, KBIDP);
179b23d0fcaSmiod 	bus_space_unmap(ga->ga_ca.ca_iot, ioh, KBMAPSIZE);
180b23d0fcaSmiod 
181b23d0fcaSmiod 	if (rv == PCKBC_KBD_SLOT || rv == PCKBC_AUX_SLOT)
182b23d0fcaSmiod 		return (1);	/* keyboard or mouse port */
183b23d0fcaSmiod 
184b23d0fcaSmiod 	return (0);
185b23d0fcaSmiod }
186b23d0fcaSmiod 
187b23d0fcaSmiod /*
188b23d0fcaSmiod  * Attachment helper functions
189b23d0fcaSmiod  */
190b23d0fcaSmiod 
191b23d0fcaSmiod /* state machine values */
192b23d0fcaSmiod #define	PROBE_SUCCESS	0
193b23d0fcaSmiod #define	PROBE_TIMEOUT	1
194b23d0fcaSmiod #define	PROBE_RETRANS	2
195b23d0fcaSmiod #define	PROBE_NOACK	3
196b23d0fcaSmiod 
197b23d0fcaSmiod int probe_readtmo(bus_space_tag_t iot, bus_space_handle_t ioh, int *reply);
198b23d0fcaSmiod int probe_readretry(bus_space_tag_t iot, bus_space_handle_t ioh, int *reply);
199b23d0fcaSmiod int probe_sendtmo(bus_space_tag_t iot, bus_space_handle_t ioh, int cmdbyte);
200b23d0fcaSmiod int probe_sendack(bus_space_tag_t iot, bus_space_handle_t ioh, int cmdbyte);
201b23d0fcaSmiod int probe_ident(bus_space_tag_t iot, bus_space_handle_t ioh);
202b23d0fcaSmiod 
203b23d0fcaSmiod #define	PROBE_TRIES	1000
204b23d0fcaSmiod 
205b23d0fcaSmiod int
probe_readtmo(bus_space_tag_t iot,bus_space_handle_t ioh,int * reply)206b23d0fcaSmiod probe_readtmo(bus_space_tag_t iot, bus_space_handle_t ioh, int *reply)
207b23d0fcaSmiod {
208b23d0fcaSmiod 	int numtries = PROBE_TRIES;
209b23d0fcaSmiod 
210b23d0fcaSmiod 	while (numtries--) {
211b23d0fcaSmiod 		if (bus_space_read_1(iot, ioh, KBSTATP) &
212b23d0fcaSmiod 		    (KBS_DIB | KBS_TERR | KBS_PERR))
213b23d0fcaSmiod 			break;
214b23d0fcaSmiod 		DELAY(500);
215b23d0fcaSmiod 	}
216b23d0fcaSmiod 
217b23d0fcaSmiod 	if (numtries <= 0)
218b23d0fcaSmiod 		return (PROBE_TIMEOUT);
219b23d0fcaSmiod 
220b23d0fcaSmiod 	if (bus_space_read_1(iot, ioh, KBSTATP) & (KBS_PERR | KBS_TERR)) {
221b23d0fcaSmiod 		if (!(bus_space_read_1(iot, ioh, KBSTATP) & KBS_DIB)) {
22266dc5f26Smickey 			bus_space_write_1(iot, ioh, KBRESETP, 0xff);
22366dc5f26Smickey 			bus_space_write_1(iot, ioh, KBRESETP, 0x00);
22466dc5f26Smickey 			bus_space_write_1(iot, ioh, KBCMDP,
22566dc5f26Smickey 			    bus_space_read_1(iot, ioh, KBCMDP) | KBCP_ENABLE);
226b23d0fcaSmiod 			return (PROBE_TIMEOUT);
227b23d0fcaSmiod 		}
228b23d0fcaSmiod 
229b23d0fcaSmiod 		*reply = bus_space_read_1(iot, ioh, KBDATAP);
230b23d0fcaSmiod 		if (!(bus_space_read_1(iot, ioh, KBSTATP) & KBS_DIB)) {
23166dc5f26Smickey 			bus_space_write_1(iot, ioh, KBRESETP, 0xff);
23266dc5f26Smickey 			bus_space_write_1(iot, ioh, KBRESETP, 0x00);
23366dc5f26Smickey 			bus_space_write_1(iot, ioh, KBCMDP,
23466dc5f26Smickey 			    bus_space_read_1(iot, ioh, KBCMDP) | KBCP_ENABLE);
235b23d0fcaSmiod 			if (probe_sendtmo(iot, ioh, KBR_RESEND))
236b23d0fcaSmiod 				return (PROBE_TIMEOUT);
237b23d0fcaSmiod 			else
238b23d0fcaSmiod 				return (PROBE_RETRANS);
239b23d0fcaSmiod 		} else
240b23d0fcaSmiod 			return (PROBE_SUCCESS);
241b23d0fcaSmiod 	} else {
242b23d0fcaSmiod 		*reply = bus_space_read_1(iot, ioh, KBDATAP);
243b23d0fcaSmiod 		return (PROBE_SUCCESS);
244b23d0fcaSmiod 	}
245b23d0fcaSmiod }
246b23d0fcaSmiod 
247b23d0fcaSmiod int
probe_readretry(bus_space_tag_t iot,bus_space_handle_t ioh,int * reply)248b23d0fcaSmiod probe_readretry(bus_space_tag_t iot, bus_space_handle_t ioh, int *reply)
249b23d0fcaSmiod {
250b23d0fcaSmiod 	int read_status;
251b23d0fcaSmiod 	int retrans = KB_MAX_RETRANS;
252b23d0fcaSmiod 
253b23d0fcaSmiod 	do {
254b23d0fcaSmiod 		read_status = probe_readtmo(iot, ioh, reply);
255b23d0fcaSmiod 	} while ((read_status == PROBE_RETRANS) && retrans--);
256b23d0fcaSmiod 
257b23d0fcaSmiod 	return (read_status);
258b23d0fcaSmiod }
259b23d0fcaSmiod 
260b23d0fcaSmiod int
probe_sendtmo(bus_space_tag_t iot,bus_space_handle_t ioh,int cmdbyte)261b23d0fcaSmiod probe_sendtmo(bus_space_tag_t iot, bus_space_handle_t ioh, int cmdbyte)
262b23d0fcaSmiod {
263b23d0fcaSmiod 	int numtries = PROBE_TRIES;
264b23d0fcaSmiod 
265b23d0fcaSmiod 	while (numtries--) {
266b23d0fcaSmiod 		if ((bus_space_read_1(iot, ioh, KBSTATP) & KBS_OCMD) == 0)
267b23d0fcaSmiod 			break;
268b23d0fcaSmiod 		DELAY(500);
269b23d0fcaSmiod 	}
270b23d0fcaSmiod 
271f307151fSmiod 	if (numtries <= 0)
272b23d0fcaSmiod 		return (1);
273b23d0fcaSmiod 
274b23d0fcaSmiod 	bus_space_write_1(iot, ioh, KBDATAP, cmdbyte);
275b23d0fcaSmiod 	bus_space_write_1(iot, ioh, KBCMDP, KBCP_ENABLE);
276b23d0fcaSmiod 	return (0);
277b23d0fcaSmiod }
278b23d0fcaSmiod 
279b23d0fcaSmiod int
probe_sendack(bus_space_tag_t iot,bus_space_handle_t ioh,int cmdbyte)280b23d0fcaSmiod probe_sendack(bus_space_tag_t iot, bus_space_handle_t ioh, int cmdbyte)
281b23d0fcaSmiod {
282b23d0fcaSmiod 	int retranscount;
283b23d0fcaSmiod 	int reply;
284b23d0fcaSmiod 
285b23d0fcaSmiod 	for (retranscount = 0; retranscount < KB_MAX_RETRANS; retranscount++) {
286b23d0fcaSmiod 		if (probe_sendtmo(iot, ioh, cmdbyte))
287b23d0fcaSmiod 			return (PROBE_TIMEOUT);
288b23d0fcaSmiod 		if (probe_readretry(iot, ioh, &reply))
289b23d0fcaSmiod 			return (PROBE_TIMEOUT);
290b23d0fcaSmiod 
291b23d0fcaSmiod 		switch (reply) {
292b23d0fcaSmiod 		case KBR_ACK:
293b23d0fcaSmiod 			return (PROBE_SUCCESS);
294b23d0fcaSmiod 		case KBR_RESEND:
295b23d0fcaSmiod 			break;
296b23d0fcaSmiod 		default:
297b23d0fcaSmiod 			return (PROBE_NOACK);
298b23d0fcaSmiod 		}
299b23d0fcaSmiod 	}
300b23d0fcaSmiod 	return (PROBE_TIMEOUT);
301b23d0fcaSmiod 
302b23d0fcaSmiod }
303b23d0fcaSmiod 
304b23d0fcaSmiod int
probe_ident(bus_space_tag_t iot,bus_space_handle_t ioh)305b23d0fcaSmiod probe_ident(bus_space_tag_t iot, bus_space_handle_t ioh)
306b23d0fcaSmiod {
307b23d0fcaSmiod 	int status;
308b23d0fcaSmiod 
309b23d0fcaSmiod 	bus_space_write_1(iot, ioh, KBRESETP, 0);
310b23d0fcaSmiod 	bus_space_write_1(iot, ioh, KBCMDP, KBCP_ENABLE);
311b23d0fcaSmiod 	DELAY(0x20000);	/* XXX why 0x? */
312b23d0fcaSmiod 	bus_space_write_1(iot, ioh, KBCMDP, 0);
313b23d0fcaSmiod 	DELAY(20000);
314b23d0fcaSmiod 	bus_space_write_1(iot, ioh, KBRESETP, 0);
315b23d0fcaSmiod 	bus_space_write_1(iot, ioh, KBCMDP, KBCP_DIAG);
316b23d0fcaSmiod 	DELAY(20000);
317b23d0fcaSmiod 
318b23d0fcaSmiod 	status = probe_sendack(iot, ioh, KBC_DISABLE);
319b23d0fcaSmiod 	switch (status) {
320b23d0fcaSmiod 	case PROBE_TIMEOUT:
321b23d0fcaSmiod 		if (bus_space_read_1(iot, ioh, KBSTATP) & KBS_OCMD) {
322b23d0fcaSmiod 			bus_space_write_1(iot, ioh, KBRESETP, 0);
323b23d0fcaSmiod 			bus_space_write_1(iot, ioh, KBCMDP, KBCP_ENABLE);
324b23d0fcaSmiod 		}
325b23d0fcaSmiod 		return (-1);
326b23d0fcaSmiod 	case PROBE_NOACK:
327b23d0fcaSmiod 		return (-1);
328b23d0fcaSmiod 	}
329b23d0fcaSmiod 
330b23d0fcaSmiod 	if (probe_sendack(iot, ioh, KBC_ID) != PROBE_SUCCESS)
331b23d0fcaSmiod 		return (-1);
332b23d0fcaSmiod 
333b23d0fcaSmiod 	if (probe_readretry(iot, ioh, &status))
334b23d0fcaSmiod 		return (-1);
335b23d0fcaSmiod 
336b23d0fcaSmiod 	switch (status) {
337b23d0fcaSmiod 	case KBR_MOUSE_ID:
338b23d0fcaSmiod 		return PCKBC_AUX_SLOT;
339b23d0fcaSmiod 	case KBR_KBD_ID1:
340b23d0fcaSmiod 		if (probe_readretry(iot, ioh, &status))
341b23d0fcaSmiod 			return (-1);
342b23d0fcaSmiod 		if (status == KBR_KBD_ID2) {
343b23d0fcaSmiod 			if (probe_sendack(iot, ioh, KBC_ENABLE) ==
344b23d0fcaSmiod 			    PROBE_TIMEOUT) {
345b23d0fcaSmiod 				bus_space_write_1(iot, ioh, KBRESETP, 0);
346b23d0fcaSmiod 				bus_space_write_1(iot, ioh, KBCMDP,
347b23d0fcaSmiod 				    KBCP_ENABLE);
348b23d0fcaSmiod 			}
349b23d0fcaSmiod 			return PCKBC_KBD_SLOT;
350b23d0fcaSmiod 		}
351b23d0fcaSmiod 	}
352b23d0fcaSmiod 	return (-1);
353b23d0fcaSmiod }
354b23d0fcaSmiod 
355b23d0fcaSmiod void
gsckbc_attach(struct device * parent,struct device * self,void * aux)356b23d0fcaSmiod gsckbc_attach(struct device *parent, struct device *self, void *aux)
357b23d0fcaSmiod {
358b23d0fcaSmiod 	struct gsc_attach_args *ga = aux;
359b23d0fcaSmiod 	struct gsckbc_softc *gsc = (void *)self;
360b23d0fcaSmiod 	struct pckbc_softc *sc = &gsc->sc_pckbc;
361b23d0fcaSmiod 	struct pckbc_internal *t;
362b23d0fcaSmiod 	bus_space_tag_t iot;
363b23d0fcaSmiod 	bus_space_handle_t ioh;
3641f388c19Smickey 	int ident;
365b23d0fcaSmiod 
366b23d0fcaSmiod 	iot = ga->ga_ca.ca_iot;
367b23d0fcaSmiod 
368b23d0fcaSmiod 	if (bus_space_map(iot, ga->ga_ca.ca_hpa, KBMAPSIZE, 0, &ioh))
369b23d0fcaSmiod 		panic("gsckbc_attach: couldn't map port");
370b23d0fcaSmiod 
371b23d0fcaSmiod 	gsc->sc_type = bus_space_read_1(iot, ioh, KBIDP);
372b23d0fcaSmiod 
373b23d0fcaSmiod 	switch (gsc->sc_type) {
374b23d0fcaSmiod 	case PCKBC_KBD_SLOT:
375b23d0fcaSmiod 	case PCKBC_AUX_SLOT:
376b23d0fcaSmiod 		break;
377b23d0fcaSmiod 	default:
378b23d0fcaSmiod 		printf(": unknown port type %x\n", gsc->sc_type);
379b23d0fcaSmiod 		/* play nice and don't really attach. */
380b23d0fcaSmiod 		bus_space_unmap(iot, ioh, KBMAPSIZE);
381b23d0fcaSmiod 		return;
382b23d0fcaSmiod 	}
383b23d0fcaSmiod 
384bd4a265cSmiod 	gsc->sc_ih = gsc_intr_establish((struct gsc_softc *)parent,
385bd4a265cSmiod 	    ga->ga_ca.ca_irq, IPL_TTY, gsckbcintr, sc, sc->sc_dv.dv_xname);
386bd4a265cSmiod 	if (gsc->sc_ih == NULL) {
387bd4a265cSmiod 		printf(": can't establish interrupt\n");
388bd4a265cSmiod 		bus_space_unmap(iot, ioh, KBMAPSIZE);
389bd4a265cSmiod 		return;
390bd4a265cSmiod 	}
3911f388c19Smickey 
392bd4a265cSmiod 	printf("\n");
393b23d0fcaSmiod 
3947107272fSkrw 	t = malloc(sizeof(*t), M_DEVBUF, M_WAITOK | M_ZERO);
395b23d0fcaSmiod 	t->t_iot = iot;
396b23d0fcaSmiod 	/* XXX it does not make sense to only map two ports here */
397b23d0fcaSmiod 	t->t_ioh_d = t->t_ioh_c = ioh;
398b23d0fcaSmiod 	t->t_addr = ga->ga_ca.ca_hpa;
399b23d0fcaSmiod 	t->t_sc = sc;
400b23d0fcaSmiod 	timeout_set(&t->t_cleanup, pckbc_cleanup, t);
401b23d0fcaSmiod 	sc->id = t;
402b23d0fcaSmiod 
4039a68db39Smiod 	/*
404b23d0fcaSmiod 	 * Reset port and probe device, if plugged
405b23d0fcaSmiod 	 */
406b23d0fcaSmiod 	ident = probe_ident(iot, ioh);
407b23d0fcaSmiod 	if (ident != gsc->sc_type) {
408b23d0fcaSmiod 		/* don't whine for unplugged ports */
409b23d0fcaSmiod 		if (ident != -1)
410b23d0fcaSmiod 			printf("%s: expecting device type %d, got %d\n",
411b23d0fcaSmiod 			    sc->sc_dv.dv_xname, gsc->sc_type, ident);
412b23d0fcaSmiod 	} else {
4132415bd9bSmiod #if (NPCKBD > 0)
414f307151fSmiod 		if (gsc->sc_type == PCKBC_KBD_SLOT &&
415f307151fSmiod 		    ga->ga_dp.dp_mod == PAGE0->mem_kbd.pz_dp.dp_mod &&
416f307151fSmiod 		    bcmp(ga->ga_dp.dp_bc, PAGE0->mem_kbd.pz_dp.dp_bc, 6) == 0)
4176b1675bfSshadchin 			pckbd_cnattach(t);
418b23d0fcaSmiod #endif
419b23d0fcaSmiod 		pckbc_attach_slot(sc, gsc->sc_type);
420b23d0fcaSmiod 	}
421b23d0fcaSmiod }
422b23d0fcaSmiod 
423b23d0fcaSmiod /*
424b23d0fcaSmiod  * pckbc-like interfaces
425b23d0fcaSmiod  */
426b23d0fcaSmiod 
427b23d0fcaSmiod int
pckbc_wait_output(iot,ioh)428b23d0fcaSmiod pckbc_wait_output(iot, ioh)
429b23d0fcaSmiod 	bus_space_tag_t iot;
430b23d0fcaSmiod 	bus_space_handle_t ioh;
431b23d0fcaSmiod {
432b23d0fcaSmiod 	u_int i;
433b23d0fcaSmiod 
434b23d0fcaSmiod 	for (i = 100000; i; i--) {
435b23d0fcaSmiod 		if ((bus_space_read_1(iot, ioh, KBSTATP) & KBS_OCMD)) {
436b23d0fcaSmiod 			KBD_DELAY;
437b23d0fcaSmiod 		} else
438b23d0fcaSmiod 			return (1);
439b23d0fcaSmiod 	}
440b23d0fcaSmiod 	return (0);
441b23d0fcaSmiod }
442b23d0fcaSmiod 
443b23d0fcaSmiod int
pckbc_send_cmd(iot,ioh,val)444b23d0fcaSmiod pckbc_send_cmd(iot, ioh, val)
445b23d0fcaSmiod 	bus_space_tag_t iot;
446b23d0fcaSmiod 	bus_space_handle_t ioh;
447b23d0fcaSmiod 	u_char val;
448b23d0fcaSmiod {
449b23d0fcaSmiod 	if (!pckbc_wait_output(iot, ioh))
450b23d0fcaSmiod 		return (0);
451b23d0fcaSmiod 	bus_space_write_1(iot, ioh, KBOUTP, val);
452b23d0fcaSmiod 	bus_space_write_1(iot, ioh, KBCMDP, KBCP_ENABLE);
453b23d0fcaSmiod 	return (1);
454b23d0fcaSmiod }
455b23d0fcaSmiod 
456b23d0fcaSmiod /* XXX logic */
457b23d0fcaSmiod int
pckbc_poll_data1(iot,ioh,ioh_c,slot,checkaux)458ac6d5436Stobias pckbc_poll_data1(iot, ioh, ioh_c, slot, checkaux)
459b23d0fcaSmiod 	bus_space_tag_t iot;
460b23d0fcaSmiod 	bus_space_handle_t ioh, ioh_c;
461b23d0fcaSmiod 	pckbc_slot_t slot;
462ac6d5436Stobias 	int checkaux;	/* ignored on hppa */
463b23d0fcaSmiod {
464b23d0fcaSmiod 	int i;
465b23d0fcaSmiod 	u_char stat;
466b23d0fcaSmiod 
467b23d0fcaSmiod 	/* if 1 port read takes 1us (?), this polls for 100ms */
468b23d0fcaSmiod 	for (i = 100000; i; i--) {
469b23d0fcaSmiod 		stat = bus_space_read_1(iot, ioh, KBSTATP);
470b23d0fcaSmiod 		if (stat & KBS_DIB) {
471b23d0fcaSmiod 			KBD_DELAY;
472b23d0fcaSmiod 			return bus_space_read_1(iot, ioh, KBDATAP);
473b23d0fcaSmiod 		}
474b23d0fcaSmiod 	}
475b23d0fcaSmiod 	return (-1);
476b23d0fcaSmiod }
477b23d0fcaSmiod 
478b23d0fcaSmiod int
pckbc_send_devcmd(t,slot,val)479b23d0fcaSmiod pckbc_send_devcmd(t, slot, val)
480b23d0fcaSmiod 	struct pckbc_internal *t;
481b23d0fcaSmiod 	pckbc_slot_t slot;
482b23d0fcaSmiod 	u_char val;
483b23d0fcaSmiod {
484b23d0fcaSmiod 	return pckbc_send_cmd(t->t_iot, t->t_ioh_d, val);
485b23d0fcaSmiod }
486b23d0fcaSmiod 
487b23d0fcaSmiod int
pckbc_submatch(parent,match,aux)488b23d0fcaSmiod pckbc_submatch(parent, match, aux)
489b23d0fcaSmiod 	struct device *parent;
490b23d0fcaSmiod 	void *match;
491b23d0fcaSmiod 	void *aux;
492b23d0fcaSmiod {
493b23d0fcaSmiod 	struct cfdata *cf = match;
494b23d0fcaSmiod 	struct pckbc_attach_args *pa = aux;
495b23d0fcaSmiod 
496b23d0fcaSmiod 	if (cf->cf_loc[PCKBCCF_SLOT] != PCKBCCF_SLOT_DEFAULT &&
497b23d0fcaSmiod 	    cf->cf_loc[PCKBCCF_SLOT] != pa->pa_slot)
498b23d0fcaSmiod 		return (0);
499b23d0fcaSmiod 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
500b23d0fcaSmiod }
501b23d0fcaSmiod 
502b23d0fcaSmiod int
pckbc_attach_slot(sc,slot)503b23d0fcaSmiod pckbc_attach_slot(sc, slot)
504b23d0fcaSmiod 	struct pckbc_softc *sc;
505b23d0fcaSmiod 	pckbc_slot_t slot;
506b23d0fcaSmiod {
507b23d0fcaSmiod 	struct pckbc_internal *t = sc->id;
508b23d0fcaSmiod 	struct pckbc_attach_args pa;
509b23d0fcaSmiod 	int found;
510b23d0fcaSmiod 
511b23d0fcaSmiod 	pa.pa_tag = t;
512b23d0fcaSmiod 	pa.pa_slot = slot;
513b23d0fcaSmiod 	found = (config_found_sm((struct device *)sc, &pa,
514b23d0fcaSmiod 				 pckbcprint, pckbc_submatch) != NULL);
515b23d0fcaSmiod 
516b23d0fcaSmiod 	if (found && !t->t_slotdata[slot]) {
517b23d0fcaSmiod 		t->t_slotdata[slot] = malloc(sizeof(struct pckbc_slotdata),
518b23d0fcaSmiod 					     M_DEVBUF, M_NOWAIT);
519b23d0fcaSmiod 		if (t->t_slotdata[slot] == NULL)
520b23d0fcaSmiod 			return 0;
521b23d0fcaSmiod 		pckbc_init_slotdata(t->t_slotdata[slot]);
522b23d0fcaSmiod 	}
523b23d0fcaSmiod 	return (found);
524b23d0fcaSmiod }
525b23d0fcaSmiod 
526b23d0fcaSmiod int
pckbcprint(aux,pnp)527b23d0fcaSmiod pckbcprint(aux, pnp)
528b23d0fcaSmiod 	void *aux;
529b23d0fcaSmiod 	const char *pnp;
530b23d0fcaSmiod {
531b23d0fcaSmiod #if 0	/* hppa having devices for each slot, this is barely useful */
532b23d0fcaSmiod 	struct pckbc_attach_args *pa = aux;
533b23d0fcaSmiod 
534b23d0fcaSmiod 	if (!pnp)
535b23d0fcaSmiod 		printf(" (%s slot)", pckbc_slot_names[pa->pa_slot]);
536b23d0fcaSmiod #endif
537b23d0fcaSmiod 	return (QUIET);
538b23d0fcaSmiod }
539b23d0fcaSmiod 
540b23d0fcaSmiod void
pckbc_init_slotdata(q)541b23d0fcaSmiod pckbc_init_slotdata(q)
542b23d0fcaSmiod 	struct pckbc_slotdata *q;
543b23d0fcaSmiod {
544b23d0fcaSmiod 	int i;
545b23d0fcaSmiod 	TAILQ_INIT(&q->cmdqueue);
546b23d0fcaSmiod 	TAILQ_INIT(&q->freequeue);
547b23d0fcaSmiod 
548b23d0fcaSmiod 	for (i = 0; i < NCMD; i++) {
549b23d0fcaSmiod 		TAILQ_INSERT_TAIL(&q->freequeue, &(q->cmds[i]), next);
550b23d0fcaSmiod 	}
551b23d0fcaSmiod 	q->polling = 0;
552b23d0fcaSmiod }
553b23d0fcaSmiod 
554b23d0fcaSmiod void
pckbc_flush(self,slot)555b23d0fcaSmiod pckbc_flush(self, slot)
556b23d0fcaSmiod 	pckbc_tag_t self;
557b23d0fcaSmiod 	pckbc_slot_t slot;
558b23d0fcaSmiod {
559b23d0fcaSmiod 	struct pckbc_internal *t = self;
560b23d0fcaSmiod 
561ac6d5436Stobias 	pckbc_poll_data1(t->t_iot, t->t_ioh_d, t->t_ioh_d, slot, 0);
562b23d0fcaSmiod }
563b23d0fcaSmiod 
564b23d0fcaSmiod int
pckbc_poll_data(self,slot)565b23d0fcaSmiod pckbc_poll_data(self, slot)
566b23d0fcaSmiod 	pckbc_tag_t self;
567b23d0fcaSmiod 	pckbc_slot_t slot;
568b23d0fcaSmiod {
569b23d0fcaSmiod 	struct pckbc_internal *t = self;
570b23d0fcaSmiod 	struct pckbc_slotdata *q = t->t_slotdata[slot];
571b23d0fcaSmiod 	int c;
572b23d0fcaSmiod 
573ac6d5436Stobias 	c = pckbc_poll_data1(t->t_iot, t->t_ioh_d, t->t_ioh_d, slot, 0);
574b23d0fcaSmiod 	if (c != -1 && q && CMD_IN_QUEUE(q)) {
575b23d0fcaSmiod 		/* we jumped into a running command - try to
576b23d0fcaSmiod 		 deliver the response */
577b23d0fcaSmiod 		if (pckbc_cmdresponse(t, slot, c))
578b23d0fcaSmiod 			return (-1);
579b23d0fcaSmiod 	}
580b23d0fcaSmiod 	return (c);
581b23d0fcaSmiod }
582b23d0fcaSmiod 
5832415bd9bSmiod int
pckbc_xt_translation(self,table)584*0c77ed63Smiod pckbc_xt_translation(self, table)
5852415bd9bSmiod 	pckbc_tag_t self;
586*0c77ed63Smiod 	int *table;
5872415bd9bSmiod {
588f73481aeSshadchin 	/* Translation isn't supported... */
589f73481aeSshadchin 	return (-1);
5902415bd9bSmiod }
5912415bd9bSmiod 
592b23d0fcaSmiod void
pckbc_slot_enable(self,slot,on)593b23d0fcaSmiod pckbc_slot_enable(self, slot, on)
594b23d0fcaSmiod 	pckbc_tag_t self;
595b23d0fcaSmiod 	pckbc_slot_t slot;
596b23d0fcaSmiod 	int on;
597b23d0fcaSmiod {
598b23d0fcaSmiod 	/* can't enable slots here as they are different devices */
599b23d0fcaSmiod }
600b23d0fcaSmiod 
601b23d0fcaSmiod void
pckbc_set_poll(self,slot,on)602b23d0fcaSmiod pckbc_set_poll(self, slot, on)
603b23d0fcaSmiod 	pckbc_tag_t self;
604b23d0fcaSmiod 	pckbc_slot_t slot;
605b23d0fcaSmiod 	int on;
606b23d0fcaSmiod {
607b23d0fcaSmiod 	struct pckbc_internal *t = (struct pckbc_internal *)self;
608b23d0fcaSmiod 
609b23d0fcaSmiod 	t->t_slotdata[slot]->polling = on;
610b23d0fcaSmiod 
611b23d0fcaSmiod 	if (!on) {
612b23d0fcaSmiod                 int s;
613b23d0fcaSmiod 
614b23d0fcaSmiod                 /*
615b23d0fcaSmiod                  * If disabling polling on a device that's been configured,
616b23d0fcaSmiod                  * make sure there are no bytes left in the FIFO, holding up
617b23d0fcaSmiod                  * the interrupt line.  Otherwise we won't get any further
618b23d0fcaSmiod                  * interrupts.
619b23d0fcaSmiod                  */
620b23d0fcaSmiod 		if (t->t_sc) {
621b23d0fcaSmiod 			s = spltty();
6221f388c19Smickey 			gsckbcintr(t->t_sc);
623b23d0fcaSmiod 			splx(s);
624b23d0fcaSmiod 		}
625b23d0fcaSmiod 	}
626b23d0fcaSmiod }
627b23d0fcaSmiod 
628b23d0fcaSmiod /*
629b23d0fcaSmiod  * Pass command to device, poll for ACK and data.
630b23d0fcaSmiod  * to be called at spltty()
631b23d0fcaSmiod  */
632b23d0fcaSmiod void
pckbc_poll_cmd1(t,slot,cmd)633b23d0fcaSmiod pckbc_poll_cmd1(t, slot, cmd)
634b23d0fcaSmiod 	struct pckbc_internal *t;
635b23d0fcaSmiod 	pckbc_slot_t slot;
636b23d0fcaSmiod 	struct pckbc_devcmd *cmd;
637b23d0fcaSmiod {
638b23d0fcaSmiod 	bus_space_tag_t iot = t->t_iot;
639b23d0fcaSmiod 	bus_space_handle_t ioh = t->t_ioh_d;
640b23d0fcaSmiod 	int i, c = 0;
641b23d0fcaSmiod 
642b23d0fcaSmiod 	while (cmd->cmdidx < cmd->cmdlen) {
643b23d0fcaSmiod 		if (!pckbc_send_devcmd(t, slot, cmd->cmd[cmd->cmdidx])) {
644b23d0fcaSmiod 			printf("pckbc_cmd: send error\n");
645b23d0fcaSmiod 			cmd->status = EIO;
646b23d0fcaSmiod 			return;
647b23d0fcaSmiod 		}
648b23d0fcaSmiod 		for (i = 10; i; i--) { /* 1s ??? */
649ac6d5436Stobias 			c = pckbc_poll_data1(iot, ioh, ioh, slot, 0);
650b23d0fcaSmiod 			if (c != -1)
651b23d0fcaSmiod 				break;
652b23d0fcaSmiod 		}
653b23d0fcaSmiod 
654b23d0fcaSmiod 		if (c == KBC_DEVCMD_ACK) {
655b23d0fcaSmiod 			cmd->cmdidx++;
656b23d0fcaSmiod 			continue;
657b23d0fcaSmiod 		}
658b23d0fcaSmiod 		if (c == KBC_DEVCMD_RESEND) {
659b23d0fcaSmiod #ifdef PCKBCDEBUG
660b23d0fcaSmiod 			printf("pckbc_cmd: RESEND\n");
661b23d0fcaSmiod #endif
662b23d0fcaSmiod 			if (cmd->retries++ < KB_MAX_RETRANS)
663b23d0fcaSmiod 				continue;
664b23d0fcaSmiod 			else {
665b23d0fcaSmiod #ifdef PCKBCDEBUG
666b23d0fcaSmiod 				printf("pckbc: cmd failed\n");
667b23d0fcaSmiod #endif
668b23d0fcaSmiod 				cmd->status = EIO;
669b23d0fcaSmiod 				return;
670b23d0fcaSmiod 			}
671b23d0fcaSmiod 		}
672b23d0fcaSmiod 		if (c == -1) {
673b23d0fcaSmiod #ifdef PCKBCDEBUG
674b23d0fcaSmiod 			printf("pckbc_cmd: timeout\n");
675b23d0fcaSmiod #endif
676b23d0fcaSmiod 			cmd->status = EIO;
677b23d0fcaSmiod 			return;
678b23d0fcaSmiod 		}
679b23d0fcaSmiod #ifdef PCKBCDEBUG
680b23d0fcaSmiod 		printf("pckbc_cmd: lost 0x%x\n", c);
681b23d0fcaSmiod #endif
682b23d0fcaSmiod 	}
683b23d0fcaSmiod 
684b23d0fcaSmiod 	while (cmd->responseidx < cmd->responselen) {
685b23d0fcaSmiod 		if (cmd->flags & KBC_CMDFLAG_SLOW)
686b23d0fcaSmiod 			i = 100; /* 10s ??? */
687b23d0fcaSmiod 		else
688b23d0fcaSmiod 			i = 10; /* 1s ??? */
689b23d0fcaSmiod 		while (i--) {
690ac6d5436Stobias 			c = pckbc_poll_data1(iot, ioh, ioh, slot, 0);
691b23d0fcaSmiod 			if (c != -1)
692b23d0fcaSmiod 				break;
693b23d0fcaSmiod 		}
694b23d0fcaSmiod 		if (c == -1) {
695b23d0fcaSmiod #ifdef PCKBCDEBUG
696b23d0fcaSmiod 			printf("pckbc_cmd: no data\n");
697b23d0fcaSmiod #endif
698b23d0fcaSmiod 			cmd->status = ETIMEDOUT;
699b23d0fcaSmiod 			return;
700b23d0fcaSmiod 		} else
701b23d0fcaSmiod 			cmd->response[cmd->responseidx++] = c;
702b23d0fcaSmiod 	}
703b23d0fcaSmiod }
704b23d0fcaSmiod 
705b23d0fcaSmiod /* for use in autoconfiguration */
706b23d0fcaSmiod int
pckbc_poll_cmd(self,slot,cmd,len,responselen,respbuf,slow)707b23d0fcaSmiod pckbc_poll_cmd(self, slot, cmd, len, responselen, respbuf, slow)
708b23d0fcaSmiod 	pckbc_tag_t self;
709b23d0fcaSmiod 	pckbc_slot_t slot;
710b23d0fcaSmiod 	u_char *cmd;
711b23d0fcaSmiod 	int len, responselen;
712b23d0fcaSmiod 	u_char *respbuf;
713b23d0fcaSmiod 	int slow;
714b23d0fcaSmiod {
715b23d0fcaSmiod 	struct pckbc_devcmd nc;
716b23d0fcaSmiod 
717b23d0fcaSmiod 	if ((len > 4) || (responselen > 4))
718b23d0fcaSmiod 		return (EINVAL);
719b23d0fcaSmiod 
720b23d0fcaSmiod 	bzero(&nc, sizeof(nc));
721b23d0fcaSmiod 	bcopy(cmd, nc.cmd, len);
722b23d0fcaSmiod 	nc.cmdlen = len;
723b23d0fcaSmiod 	nc.responselen = responselen;
724b23d0fcaSmiod 	nc.flags = (slow ? KBC_CMDFLAG_SLOW : 0);
725b23d0fcaSmiod 
726d7153411Smiod 	pckbc_poll_cmd1(self, slot, &nc);
727b23d0fcaSmiod 
728b23d0fcaSmiod 	if (nc.status == 0 && respbuf)
729b23d0fcaSmiod 		bcopy(nc.response, respbuf, responselen);
730b23d0fcaSmiod 
731b23d0fcaSmiod 	return (nc.status);
732b23d0fcaSmiod }
733b23d0fcaSmiod 
734b23d0fcaSmiod /*
735b23d0fcaSmiod  * Clean up a command queue, throw away everything.
736b23d0fcaSmiod  */
737b23d0fcaSmiod void
pckbc_cleanqueue(q)738b23d0fcaSmiod pckbc_cleanqueue(q)
739b23d0fcaSmiod 	struct pckbc_slotdata *q;
740b23d0fcaSmiod {
741b23d0fcaSmiod 	struct pckbc_devcmd *cmd;
742b23d0fcaSmiod #ifdef PCKBCDEBUG
743b23d0fcaSmiod 	int i;
744b23d0fcaSmiod #endif
745b23d0fcaSmiod 
746b23d0fcaSmiod 	while ((cmd = TAILQ_FIRST(&q->cmdqueue))) {
747b23d0fcaSmiod 		TAILQ_REMOVE(&q->cmdqueue, cmd, next);
748b23d0fcaSmiod #ifdef PCKBCDEBUG
749b23d0fcaSmiod 		printf("pckbc_cleanqueue: removing");
750b23d0fcaSmiod 		for (i = 0; i < cmd->cmdlen; i++)
751b23d0fcaSmiod 			printf(" %02x", cmd->cmd[i]);
752b23d0fcaSmiod 		printf("\n");
753b23d0fcaSmiod #endif
754b23d0fcaSmiod 		TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
755b23d0fcaSmiod 	}
756b23d0fcaSmiod }
757b23d0fcaSmiod 
758b23d0fcaSmiod /*
759b23d0fcaSmiod  * Timeout error handler: clean queues and data port.
760b23d0fcaSmiod  * XXX could be less invasive.
761b23d0fcaSmiod  */
762b23d0fcaSmiod void
pckbc_cleanup(self)763b23d0fcaSmiod pckbc_cleanup(self)
764b23d0fcaSmiod 	void *self;
765b23d0fcaSmiod {
766b23d0fcaSmiod 	struct pckbc_internal *t = self;
767b23d0fcaSmiod 	int s;
768b23d0fcaSmiod 
769b23d0fcaSmiod 	printf("pckbc: command timeout\n");
770b23d0fcaSmiod 
771b23d0fcaSmiod 	s = spltty();
772b23d0fcaSmiod 
773b23d0fcaSmiod 	if (t->t_slotdata[PCKBC_KBD_SLOT])
774b23d0fcaSmiod 		pckbc_cleanqueue(t->t_slotdata[PCKBC_KBD_SLOT]);
775b23d0fcaSmiod 	if (t->t_slotdata[PCKBC_AUX_SLOT])
776b23d0fcaSmiod 		pckbc_cleanqueue(t->t_slotdata[PCKBC_AUX_SLOT]);
777b23d0fcaSmiod 
778b23d0fcaSmiod 	while (bus_space_read_1(t->t_iot, t->t_ioh_d, KBSTATP) & KBS_DIB) {
779b23d0fcaSmiod 		KBD_DELAY;
780b23d0fcaSmiod 		(void) bus_space_read_1(t->t_iot, t->t_ioh_d, KBDATAP);
781b23d0fcaSmiod 	}
782b23d0fcaSmiod 
783b23d0fcaSmiod 	/* reset KBC? */
784b23d0fcaSmiod 
785b23d0fcaSmiod 	splx(s);
786b23d0fcaSmiod }
787b23d0fcaSmiod 
788b23d0fcaSmiod /*
789b23d0fcaSmiod  * Pass command to device during normal operation.
790b23d0fcaSmiod  * to be called at spltty()
791b23d0fcaSmiod  */
792b23d0fcaSmiod void
pckbc_start(t,slot)793b23d0fcaSmiod pckbc_start(t, slot)
794b23d0fcaSmiod 	struct pckbc_internal *t;
795b23d0fcaSmiod 	pckbc_slot_t slot;
796b23d0fcaSmiod {
797b23d0fcaSmiod 	struct pckbc_slotdata *q = t->t_slotdata[slot];
798b23d0fcaSmiod 	struct pckbc_devcmd *cmd = TAILQ_FIRST(&q->cmdqueue);
799b23d0fcaSmiod 
800b23d0fcaSmiod 	if (IS_POLLING(q)) {
801b23d0fcaSmiod 		do {
802b23d0fcaSmiod 			pckbc_poll_cmd1(t, slot, cmd);
803b23d0fcaSmiod 			if (cmd->status)
804b23d0fcaSmiod 				printf("pckbc_start: command error\n");
805b23d0fcaSmiod 
806b23d0fcaSmiod 			TAILQ_REMOVE(&q->cmdqueue, cmd, next);
807b23d0fcaSmiod 			if (cmd->flags & KBC_CMDFLAG_SYNC)
808b23d0fcaSmiod 				wakeup(cmd);
809b23d0fcaSmiod 			else {
810b23d0fcaSmiod 				timeout_del(&t->t_cleanup);
811b23d0fcaSmiod 				TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
812b23d0fcaSmiod 			}
813b23d0fcaSmiod 			cmd = TAILQ_FIRST(&q->cmdqueue);
814b23d0fcaSmiod 		} while (cmd);
815b23d0fcaSmiod 		return;
816b23d0fcaSmiod 	}
817b23d0fcaSmiod 
818b23d0fcaSmiod 	if (!pckbc_send_devcmd(t, slot, cmd->cmd[cmd->cmdidx])) {
819b23d0fcaSmiod 		printf("pckbc_start: send error\n");
820b23d0fcaSmiod 		/* XXX what now? */
821b23d0fcaSmiod 		return;
822b23d0fcaSmiod 	}
823b23d0fcaSmiod }
824b23d0fcaSmiod 
825b23d0fcaSmiod /*
826d7153411Smiod  * Handle command responses coming in asynchronously,
827b23d0fcaSmiod  * return nonzero if valid response.
828b23d0fcaSmiod  * to be called at spltty()
829b23d0fcaSmiod  */
830b23d0fcaSmiod int
pckbc_cmdresponse(t,slot,data)831b23d0fcaSmiod pckbc_cmdresponse(t, slot, data)
832b23d0fcaSmiod 	struct pckbc_internal *t;
833b23d0fcaSmiod 	pckbc_slot_t slot;
834b23d0fcaSmiod 	u_char data;
835b23d0fcaSmiod {
836b23d0fcaSmiod 	struct pckbc_slotdata *q = t->t_slotdata[slot];
837b23d0fcaSmiod 	struct pckbc_devcmd *cmd = TAILQ_FIRST(&q->cmdqueue);
838b23d0fcaSmiod 
839b23d0fcaSmiod #ifdef DIAGNOSTIC
840b23d0fcaSmiod 	if (!cmd)
841b23d0fcaSmiod 		panic("pckbc_cmdresponse: no active command");
842b23d0fcaSmiod #endif
843b23d0fcaSmiod 	if (cmd->cmdidx < cmd->cmdlen) {
844b23d0fcaSmiod 		if (data != KBC_DEVCMD_ACK && data != KBC_DEVCMD_RESEND)
845b23d0fcaSmiod 			return (0);
846b23d0fcaSmiod 
847b23d0fcaSmiod 		if (data == KBC_DEVCMD_RESEND) {
848b23d0fcaSmiod 			if (cmd->retries++ < KB_MAX_RETRANS) {
849b23d0fcaSmiod 				/* try again last command */
850b23d0fcaSmiod 				goto restart;
851b23d0fcaSmiod 			} else {
852d7153411Smiod #ifdef PCKBCDEBUG
853b23d0fcaSmiod 				printf("pckbc: cmd failed\n");
854d7153411Smiod #endif
855b23d0fcaSmiod 				cmd->status = EIO;
856b23d0fcaSmiod 				/* dequeue */
857b23d0fcaSmiod 			}
858b23d0fcaSmiod 		} else {
859b23d0fcaSmiod 			if (++cmd->cmdidx < cmd->cmdlen)
860b23d0fcaSmiod 				goto restart;
861b23d0fcaSmiod 			if (cmd->responselen)
862b23d0fcaSmiod 				return (1);
863b23d0fcaSmiod 			/* else dequeue */
864b23d0fcaSmiod 		}
865b23d0fcaSmiod 	} else if (cmd->responseidx < cmd->responselen) {
866b23d0fcaSmiod 		cmd->response[cmd->responseidx++] = data;
867b23d0fcaSmiod 		if (cmd->responseidx < cmd->responselen)
868b23d0fcaSmiod 			return (1);
869b23d0fcaSmiod 		/* else dequeue */
870b23d0fcaSmiod 	} else
871b23d0fcaSmiod 		return (0);
872b23d0fcaSmiod 
873b23d0fcaSmiod 	/* dequeue: */
874b23d0fcaSmiod 	TAILQ_REMOVE(&q->cmdqueue, cmd, next);
875b23d0fcaSmiod 	if (cmd->flags & KBC_CMDFLAG_SYNC)
876b23d0fcaSmiod 		wakeup(cmd);
877b23d0fcaSmiod 	else {
878b23d0fcaSmiod 		timeout_del(&t->t_cleanup);
879b23d0fcaSmiod 		TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
880b23d0fcaSmiod 	}
881b23d0fcaSmiod 	if (!CMD_IN_QUEUE(q))
882b23d0fcaSmiod 		return (1);
883b23d0fcaSmiod restart:
884b23d0fcaSmiod 	pckbc_start(t, slot);
885b23d0fcaSmiod 	return (1);
886b23d0fcaSmiod }
887b23d0fcaSmiod 
888b23d0fcaSmiod /*
889b23d0fcaSmiod  * Put command into the device's command queue, return zero or errno.
890b23d0fcaSmiod  */
891b23d0fcaSmiod int
pckbc_enqueue_cmd(self,slot,cmd,len,responselen,sync,respbuf)892b23d0fcaSmiod pckbc_enqueue_cmd(self, slot, cmd, len, responselen, sync, respbuf)
893b23d0fcaSmiod 	pckbc_tag_t self;
894b23d0fcaSmiod 	pckbc_slot_t slot;
895b23d0fcaSmiod 	u_char *cmd;
896b23d0fcaSmiod 	int len, responselen, sync;
897b23d0fcaSmiod 	u_char *respbuf;
898b23d0fcaSmiod {
899b23d0fcaSmiod 	struct pckbc_internal *t = self;
900b23d0fcaSmiod 	struct pckbc_slotdata *q = t->t_slotdata[slot];
901b23d0fcaSmiod 	struct pckbc_devcmd *nc;
902b23d0fcaSmiod 	int s, isactive, res = 0;
903b23d0fcaSmiod 
904b23d0fcaSmiod 	if ((len > 4) || (responselen > 4))
905b23d0fcaSmiod 		return (EINVAL);
906b23d0fcaSmiod 	s = spltty();
907b23d0fcaSmiod 	nc = TAILQ_FIRST(&q->freequeue);
908b23d0fcaSmiod 	if (nc) {
909b23d0fcaSmiod 		TAILQ_REMOVE(&q->freequeue, nc, next);
910b23d0fcaSmiod 	}
911b23d0fcaSmiod 	splx(s);
912b23d0fcaSmiod 	if (!nc)
913b23d0fcaSmiod 		return (ENOMEM);
914b23d0fcaSmiod 
915b23d0fcaSmiod 	bzero(nc, sizeof(*nc));
916b23d0fcaSmiod 	bcopy(cmd, nc->cmd, len);
917b23d0fcaSmiod 	nc->cmdlen = len;
918b23d0fcaSmiod 	nc->responselen = responselen;
919b23d0fcaSmiod 	nc->flags = (sync ? KBC_CMDFLAG_SYNC : 0);
920b23d0fcaSmiod 
921b23d0fcaSmiod 	s = spltty();
922b23d0fcaSmiod 
923b23d0fcaSmiod 	if (IS_POLLING(q) && sync) {
924b23d0fcaSmiod 		/*
925b23d0fcaSmiod 		 * XXX We should poll until the queue is empty.
926b23d0fcaSmiod 		 * But we don't come here normally, so make
927b23d0fcaSmiod 		 * it simple and throw away everything.
928b23d0fcaSmiod 		 */
929b23d0fcaSmiod 		pckbc_cleanqueue(q);
930b23d0fcaSmiod 	}
931b23d0fcaSmiod 
932b23d0fcaSmiod 	isactive = CMD_IN_QUEUE(q);
933b23d0fcaSmiod 	TAILQ_INSERT_TAIL(&q->cmdqueue, nc, next);
934b23d0fcaSmiod 	if (!isactive)
935b23d0fcaSmiod 		pckbc_start(t, slot);
936b23d0fcaSmiod 
937b23d0fcaSmiod 	if (IS_POLLING(q))
938b23d0fcaSmiod 		res = (sync ? nc->status : 0);
939b23d0fcaSmiod 	else if (sync) {
940448a8ce5Scheloha 		if ((res = tsleep_nsec(nc, 0, "kbccmd", SEC_TO_NSEC(1)))) {
941b23d0fcaSmiod 			TAILQ_REMOVE(&q->cmdqueue, nc, next);
942b23d0fcaSmiod 			pckbc_cleanup(t);
943b23d0fcaSmiod 		} else
944b23d0fcaSmiod 			res = nc->status;
945b23d0fcaSmiod 	} else
94629e86e5eSblambert 		timeout_add_sec(&t->t_cleanup, 1);
947b23d0fcaSmiod 
948b23d0fcaSmiod 	if (sync) {
949b23d0fcaSmiod 		if (respbuf)
950b23d0fcaSmiod 			bcopy(nc->response, respbuf, responselen);
951b23d0fcaSmiod 		TAILQ_INSERT_TAIL(&q->freequeue, nc, next);
952b23d0fcaSmiod 	}
953b23d0fcaSmiod 
954b23d0fcaSmiod 	splx(s);
955b23d0fcaSmiod 
956b23d0fcaSmiod 	return (res);
957b23d0fcaSmiod }
958b23d0fcaSmiod 
959b23d0fcaSmiod void
pckbc_set_inputhandler(self,slot,func,arg,name)960b23d0fcaSmiod pckbc_set_inputhandler(self, slot, func, arg, name)
961b23d0fcaSmiod 	pckbc_tag_t self;
962b23d0fcaSmiod 	pckbc_slot_t slot;
963b23d0fcaSmiod 	pckbc_inputfcn func;
964b23d0fcaSmiod 	void *arg;
965b23d0fcaSmiod 	char *name;
966b23d0fcaSmiod {
967b23d0fcaSmiod 	struct pckbc_internal *t = (struct pckbc_internal *)self;
968b23d0fcaSmiod 	struct pckbc_softc *sc = t->t_sc;
969b23d0fcaSmiod 
970b23d0fcaSmiod 	if (slot >= PCKBC_NSLOTS)
971b23d0fcaSmiod 		panic("pckbc_set_inputhandler: bad slot %d", slot);
972b23d0fcaSmiod 
973b23d0fcaSmiod 	sc->inputhandler[slot] = func;
974b23d0fcaSmiod 	sc->inputarg[slot] = arg;
975b23d0fcaSmiod 	sc->subname[slot] = name;
976b23d0fcaSmiod }
977b23d0fcaSmiod 
978b23d0fcaSmiod int
gsckbcintr(void * v)9791f388c19Smickey gsckbcintr(void *v)
980b23d0fcaSmiod {
9811f388c19Smickey 	struct gsckbc_softc *gsc = v;
9829a68db39Smiod 	struct pckbc_softc *sc = (struct pckbc_softc *)gsc;
983b23d0fcaSmiod 	struct pckbc_internal *t = sc->id;
984b23d0fcaSmiod 	pckbc_slot_t slot;
985b23d0fcaSmiod 	struct pckbc_slotdata *q;
986b23d0fcaSmiod 	int served = 0, data;
987b23d0fcaSmiod 
988b23d0fcaSmiod 	while (bus_space_read_1(t->t_iot, t->t_ioh_d, KBSTATP) & KBS_DIB) {
989b23d0fcaSmiod 		served = 1;
990b23d0fcaSmiod 
991b23d0fcaSmiod 		slot = gsc->sc_type;
992b23d0fcaSmiod 		q = t->t_slotdata[slot];
993b23d0fcaSmiod 
994b23d0fcaSmiod 		if (!q) {
995b23d0fcaSmiod 			/* XXX do something for live insertion? */
996b23d0fcaSmiod #ifdef PCKBCDEBUG
99715dabb47Smickey 			printf("gsckbcintr: no dev for slot %d\n", slot);
998b23d0fcaSmiod #endif
999b23d0fcaSmiod 			KBD_DELAY;
1000b23d0fcaSmiod 			(void) bus_space_read_1(t->t_iot, t->t_ioh_d, KBDATAP);
1001b23d0fcaSmiod 			continue;
1002b23d0fcaSmiod 		}
1003b23d0fcaSmiod 
1004b23d0fcaSmiod 		if (IS_POLLING(q))
1005b23d0fcaSmiod 			break; /* pckbc_poll_data() will get it */
1006b23d0fcaSmiod 
1007b23d0fcaSmiod 		KBD_DELAY;
1008b23d0fcaSmiod 		data = bus_space_read_1(t->t_iot, t->t_ioh_d, KBDATAP);
1009b23d0fcaSmiod 
1010b23d0fcaSmiod 		if (CMD_IN_QUEUE(q) && pckbc_cmdresponse(t, slot, data))
1011b23d0fcaSmiod 			continue;
1012b23d0fcaSmiod 
1013b23d0fcaSmiod 		if (sc->inputhandler[slot])
1014b23d0fcaSmiod 			(*sc->inputhandler[slot])(sc->inputarg[slot], data);
1015b23d0fcaSmiod #ifdef PCKBCDEBUG
1016b23d0fcaSmiod 		else
101715dabb47Smickey 			printf("gsckbcintr: slot %d lost %d\n", slot, data);
1018b23d0fcaSmiod #endif
1019b23d0fcaSmiod 	}
1020b23d0fcaSmiod 
1021b23d0fcaSmiod 	return (served);
1022b23d0fcaSmiod }
1023