xref: /netbsd/sys/dev/isa/pckbc_isa.c (revision c4a72b64)
1 /* $NetBSD: pckbc_isa.c,v 1.11 2002/10/04 03:40:29 soren 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  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed for the NetBSD Project
18  *	by Matthias Drochner.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: pckbc_isa.c,v 1.11 2002/10/04 03:40:29 soren Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/device.h>
42 #include <sys/malloc.h>
43 #include <sys/errno.h>
44 #include <sys/queue.h>
45 #include <sys/lock.h>
46 
47 #include <machine/bus.h>
48 
49 #include <dev/isa/isareg.h>
50 #include <dev/isa/isavar.h>
51 
52 #include <dev/ic/i8042reg.h>
53 #include <dev/ic/pckbcvar.h>
54 
55 int	pckbc_isa_match __P((struct device *, struct cfdata *, void *));
56 void	pckbc_isa_attach __P((struct device *, struct device *, void *));
57 
58 struct pckbc_isa_softc {
59 	struct pckbc_softc sc_pckbc;
60 
61 	isa_chipset_tag_t sc_ic;
62 	int sc_irq[PCKBC_NSLOTS];
63 };
64 
65 CFATTACH_DECL(pckbc_isa, sizeof(struct pckbc_isa_softc),
66     pckbc_isa_match, pckbc_isa_attach, NULL, NULL);
67 
68 void	pckbc_isa_intr_establish __P((struct pckbc_softc *, pckbc_slot_t));
69 
70 int
71 pckbc_isa_match(parent, match, aux)
72 	struct device *parent;
73 	struct cfdata *match;
74 	void *aux;
75 {
76 	struct isa_attach_args *ia = aux;
77 	bus_space_tag_t iot = ia->ia_iot;
78 	bus_space_handle_t ioh_d, ioh_c;
79 	int res, ok = 1;
80 
81 	if (ISA_DIRECT_CONFIG(ia))
82 		return (0);
83 
84 	/* If values are hardwired to something that they can't be, punt. */
85 	if (ia->ia_nio < 1 ||
86 	    (ia->ia_io[0].ir_addr != ISACF_PORT_DEFAULT &&
87 	     ia->ia_io[0].ir_addr != IO_KBD))
88 		return (0);
89 
90 	if (ia->ia_niomem > 0 &&
91 	    (ia->ia_iomem[0].ir_addr != ISACF_IOMEM_DEFAULT))
92 		return (0);
93 
94 	if (ia->ia_nirq < 1 ||
95 	    (ia->ia_irq[0].ir_irq != ISACF_IRQ_DEFAULT &&
96 	     ia->ia_irq[0].ir_irq != 1 /*XXX*/))
97 		return (0);
98 
99 	if (ia->ia_ndrq > 0 &&
100 	    (ia->ia_drq[0].ir_drq != ISACF_DRQ_DEFAULT))
101 		return (0);
102 
103 	if (pckbc_is_console(iot, IO_KBD) == 0) {
104 		struct pckbc_internal t;
105 
106 		if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d))
107 			return (0);
108 		if (bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c)) {
109 			bus_space_unmap(iot, ioh_d, 1);
110 			return (0);
111 		}
112 
113 		memset(&t, 0, sizeof(t));
114 		t.t_iot = iot;
115 		t.t_ioh_d = ioh_d;
116 		t.t_ioh_c = ioh_c;
117 
118 		/* flush KBC */
119 		(void) pckbc_poll_data1(&t, PCKBC_KBD_SLOT, 0);
120 
121 		/* KBC selftest */
122 		if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0) {
123 			ok = 0;
124 			goto out;
125 		}
126 		res = pckbc_poll_data1(&t, PCKBC_KBD_SLOT, 0);
127 		if (res != 0x55) {
128 #ifdef DEBUG
129 			printf("kbc selftest: %x\n", res);
130 #endif
131 			ok = 0;
132 		}
133  out:
134 		bus_space_unmap(iot, ioh_d, 1);
135 		bus_space_unmap(iot, ioh_c, 1);
136 	}
137 
138 	if (ok) {
139 		ia->ia_io[0].ir_addr = IO_KBD;
140 		ia->ia_io[0].ir_size = 5;
141 		ia->ia_nio = 1;
142 
143 		ia->ia_niomem = 0;
144 		ia->ia_nirq = 0;
145 		ia->ia_ndrq = 0;
146 	}
147 	return (ok);
148 }
149 
150 void
151 pckbc_isa_attach(parent, self, aux)
152 	struct device *parent, *self;
153 	void *aux;
154 {
155 	struct pckbc_isa_softc *isc = (void *)self;
156 	struct pckbc_softc *sc = &isc->sc_pckbc;
157 	struct isa_attach_args *ia = aux;
158 	struct pckbc_internal *t;
159 	bus_space_tag_t iot;
160 	bus_space_handle_t ioh_d, ioh_c;
161 
162 	isc->sc_ic = ia->ia_ic;
163 	iot = ia->ia_iot;
164 
165 	switch (ia->ia_nirq) {
166 	case 1:
167 		/* Both channels use the same IRQ. */
168 		isc->sc_irq[PCKBC_KBD_SLOT] =
169 		    isc->sc_irq[PCKBC_AUX_SLOT] = ia->ia_irq[0].ir_irq;
170 		break;
171 
172 	case 2:
173 		/* First IRQ is kbd, second IRQ is aux port. */
174 		isc->sc_irq[PCKBC_KBD_SLOT] = ia->ia_irq[0].ir_irq;
175 		isc->sc_irq[PCKBC_AUX_SLOT] = ia->ia_irq[1].ir_irq;
176 		break;
177 
178 	default:
179 		/* Set up IRQs for "normal" ISA. */
180 		isc->sc_irq[PCKBC_KBD_SLOT] = 1;
181 		isc->sc_irq[PCKBC_AUX_SLOT] = 12;
182 		break;
183 	}
184 
185 	sc->intr_establish = pckbc_isa_intr_establish;
186 
187 	if (pckbc_is_console(iot, IO_KBD)) {
188 		t = &pckbc_consdata;
189 		ioh_d = t->t_ioh_d;
190 		ioh_c = t->t_ioh_c;
191 		pckbc_console_attached = 1;
192 		/* t->t_cmdbyte was initialized by cnattach */
193 	} else {
194 		if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d) ||
195 		    bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c))
196 			panic("pckbc_attach: couldn't map");
197 
198 		t = malloc(sizeof(struct pckbc_internal), M_DEVBUF,
199 		    M_WAITOK|M_ZERO);
200 		t->t_iot = iot;
201 		t->t_ioh_d = ioh_d;
202 		t->t_ioh_c = ioh_c;
203 		t->t_addr = IO_KBD;
204 		t->t_cmdbyte = KC8_CPU; /* Enable ports */
205 		callout_init(&t->t_cleanup);
206 	}
207 
208 	t->t_sc = sc;
209 	sc->id = t;
210 
211 	printf("\n");
212 
213 	/* Finish off the attach. */
214 	pckbc_attach(sc);
215 }
216 
217 void
218 pckbc_isa_intr_establish(sc, slot)
219 	struct pckbc_softc *sc;
220 	pckbc_slot_t slot;
221 {
222 	struct pckbc_isa_softc *isc = (void *) sc;
223 	void *rv;
224 
225 	rv = isa_intr_establish(isc->sc_ic, isc->sc_irq[slot], IST_EDGE,
226 	    IPL_TTY, pckbcintr, sc);
227 	if (rv == NULL) {
228 		printf("%s: unable to establish interrupt for %s slot\n",
229 		    sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
230 	} else {
231 		printf("%s: using irq %d for %s slot\n", sc->sc_dv.dv_xname,
232 		    isc->sc_irq[slot], pckbc_slot_names[slot]);
233 	}
234 }
235