xref: /netbsd-src/sys/dev/isa/pckbc_isa.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /* $NetBSD: pckbc_isa.c,v 1.17 2005/12/11 12:22:03 christos Exp $ */
2 
3 /*
4  * Copyright (c) 1998
5  *	Matthias Drochner.  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 OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: pckbc_isa.c,v 1.17 2005/12/11 12:22:03 christos Exp $");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/proc.h>
35 #include <sys/device.h>
36 #include <sys/malloc.h>
37 #include <sys/errno.h>
38 #include <sys/queue.h>
39 #include <sys/lock.h>
40 
41 #include <machine/bus.h>
42 
43 #include <dev/isa/isareg.h>
44 #include <dev/isa/isavar.h>
45 
46 #include <dev/ic/i8042reg.h>
47 #include <dev/ic/pckbcvar.h>
48 
49 int	pckbc_isa_match(struct device *, struct cfdata *, void *);
50 void	pckbc_isa_attach(struct device *, struct device *, void *);
51 
52 struct pckbc_isa_softc {
53 	struct pckbc_softc sc_pckbc;
54 
55 	isa_chipset_tag_t sc_ic;
56 	int sc_irq[PCKBC_NSLOTS];
57 };
58 
59 CFATTACH_DECL(pckbc_isa, sizeof(struct pckbc_isa_softc),
60     pckbc_isa_match, pckbc_isa_attach, NULL, NULL);
61 
62 void	pckbc_isa_intr_establish(struct pckbc_softc *, pckbc_slot_t);
63 
64 int
65 pckbc_isa_match(parent, match, aux)
66 	struct device *parent;
67 	struct cfdata *match;
68 	void *aux;
69 {
70 	struct isa_attach_args *ia = aux;
71 	bus_space_tag_t iot = ia->ia_iot;
72 	bus_space_handle_t ioh_d, ioh_c;
73 	int res, ok = 1;
74 
75 	if (ISA_DIRECT_CONFIG(ia))
76 		return (0);
77 
78 	/* If values are hardwired to something that they can't be, punt. */
79 	if (ia->ia_nio < 1 ||
80 	    (ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT &&
81 	     ia->ia_io[0].ir_addr != IO_KBD))
82 		return (0);
83 
84 	if (ia->ia_niomem > 0 &&
85 	    (ia->ia_iomem[0].ir_addr != ISA_UNKNOWN_IOMEM))
86 		return (0);
87 
88 	if (ia->ia_nirq < 1 ||
89 	    (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ &&
90 	     ia->ia_irq[0].ir_irq != 1 /*XXX*/))
91 		return (0);
92 
93 	if (ia->ia_ndrq > 0 &&
94 	    (ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ))
95 		return (0);
96 
97 	if (pckbc_is_console(iot, IO_KBD) == 0) {
98 		struct pckbc_internal t;
99 
100 		if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d))
101 			return (0);
102 		if (bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c)) {
103 			bus_space_unmap(iot, ioh_d, 1);
104 			return (0);
105 		}
106 
107 		memset(&t, 0, sizeof(t));
108 		t.t_iot = iot;
109 		t.t_ioh_d = ioh_d;
110 		t.t_ioh_c = ioh_c;
111 
112 		/* flush KBC */
113 		(void) pckbc_poll_data1(&t, PCKBC_KBD_SLOT);
114 
115 		/* KBC selftest */
116 		if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0) {
117 			ok = 0;
118 			goto out;
119 		}
120 		res = pckbc_poll_data1(&t, PCKBC_KBD_SLOT);
121 		if (res != 0x55) {
122 #ifdef DEBUG
123 			printf("kbc selftest: %x\n", res);
124 #endif
125 			ok = 0;
126 		}
127  out:
128 		bus_space_unmap(iot, ioh_d, 1);
129 		bus_space_unmap(iot, ioh_c, 1);
130 	}
131 
132 	if (ok) {
133 		ia->ia_io[0].ir_addr = IO_KBD;
134 		ia->ia_io[0].ir_size = 5;
135 		ia->ia_nio = 1;
136 
137 		ia->ia_niomem = 0;
138 		ia->ia_nirq = 0;
139 		ia->ia_ndrq = 0;
140 	}
141 	return (ok);
142 }
143 
144 void
145 pckbc_isa_attach(parent, self, aux)
146 	struct device *parent, *self;
147 	void *aux;
148 {
149 	struct pckbc_isa_softc *isc = (void *)self;
150 	struct pckbc_softc *sc = &isc->sc_pckbc;
151 	struct isa_attach_args *ia = aux;
152 	struct pckbc_internal *t;
153 	bus_space_tag_t iot;
154 	bus_space_handle_t ioh_d, ioh_c;
155 
156 	isc->sc_ic = ia->ia_ic;
157 	iot = ia->ia_iot;
158 
159 	switch (ia->ia_nirq) {
160 	case 1:
161 		/* Both channels use the same IRQ. */
162 		isc->sc_irq[PCKBC_KBD_SLOT] =
163 		    isc->sc_irq[PCKBC_AUX_SLOT] = ia->ia_irq[0].ir_irq;
164 		break;
165 
166 	case 2:
167 		/* First IRQ is kbd, second IRQ is aux port. */
168 		isc->sc_irq[PCKBC_KBD_SLOT] = ia->ia_irq[0].ir_irq;
169 		isc->sc_irq[PCKBC_AUX_SLOT] = ia->ia_irq[1].ir_irq;
170 		break;
171 
172 	default:
173 		/* Set up IRQs for "normal" ISA. */
174 		isc->sc_irq[PCKBC_KBD_SLOT] = 1;
175 		isc->sc_irq[PCKBC_AUX_SLOT] = 12;
176 		break;
177 	}
178 
179 	sc->intr_establish = pckbc_isa_intr_establish;
180 
181 	if (pckbc_is_console(iot, IO_KBD)) {
182 		t = &pckbc_consdata;
183 		ioh_d = t->t_ioh_d;
184 		ioh_c = t->t_ioh_c;
185 		pckbc_console_attached = 1;
186 		/* t->t_cmdbyte was initialized by cnattach */
187 	} else {
188 		if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d) ||
189 		    bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c))
190 			panic("pckbc_attach: couldn't map");
191 
192 		t = malloc(sizeof(struct pckbc_internal), M_DEVBUF,
193 		    M_WAITOK|M_ZERO);
194 		t->t_iot = iot;
195 		t->t_ioh_d = ioh_d;
196 		t->t_ioh_c = ioh_c;
197 		t->t_addr = IO_KBD;
198 		t->t_cmdbyte = KC8_CPU; /* Enable ports */
199 		callout_init(&t->t_cleanup);
200 	}
201 
202 	t->t_sc = sc;
203 	sc->id = t;
204 
205 	printf("\n");
206 
207 	/* Finish off the attach. */
208 	pckbc_attach(sc);
209 }
210 
211 void
212 pckbc_isa_intr_establish(sc, slot)
213 	struct pckbc_softc *sc;
214 	pckbc_slot_t slot;
215 {
216 	struct pckbc_isa_softc *isc = (void *) sc;
217 	void *rv;
218 
219 	rv = isa_intr_establish(isc->sc_ic, isc->sc_irq[slot], IST_EDGE,
220 	    IPL_TTY, pckbcintr, sc);
221 	if (rv == NULL) {
222 		printf("%s: unable to establish interrupt for %s slot\n",
223 		    sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
224 	} else {
225 		printf("%s: using irq %d for %s slot\n", sc->sc_dv.dv_xname,
226 		    isc->sc_irq[slot], pckbc_slot_names[slot]);
227 	}
228 }
229