xref: /netbsd/sys/dev/isa/pckbc_isa.c (revision bf9ec67e)
1 /* $NetBSD: pckbc_isa.c,v 1.7 2002/01/12 16:21:07 tsutsui 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.7 2002/01/12 16:21:07 tsutsui 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 struct cfattach pckbc_isa_ca = {
66 	sizeof(struct pckbc_isa_softc), pckbc_isa_match, pckbc_isa_attach,
67 };
68 
69 void	pckbc_isa_intr_establish __P((struct pckbc_softc *, pckbc_slot_t));
70 
71 int
72 pckbc_isa_match(parent, match, aux)
73 	struct device *parent;
74 	struct cfdata *match;
75 	void *aux;
76 {
77 	struct isa_attach_args *ia = aux;
78 	bus_space_tag_t iot = ia->ia_iot;
79 	bus_space_handle_t ioh_d, ioh_c;
80 	int res, ok = 1;
81 
82 	if (ISA_DIRECT_CONFIG(ia))
83 		return (0);
84 
85 	/* If values are hardwired to something that they can't be, punt. */
86 	if (ia->ia_nio < 1 ||
87 	    (ia->ia_io[0].ir_addr != ISACF_PORT_DEFAULT &&
88 	     ia->ia_io[0].ir_addr != IO_KBD))
89 		return (0);
90 
91 	if (ia->ia_niomem > 0 &&
92 	    (ia->ia_iomem[0].ir_addr != ISACF_IOMEM_DEFAULT))
93 		return (0);
94 
95 	if (ia->ia_nirq < 1 ||
96 	    (ia->ia_irq[0].ir_irq != ISACF_IRQ_DEFAULT &&
97 	     ia->ia_irq[0].ir_irq != 1 /*XXX*/))
98 		return (0);
99 
100 	if (ia->ia_ndrq > 0 &&
101 	    (ia->ia_drq[0].ir_drq != ISACF_DRQ_DEFAULT))
102 		return (0);
103 
104 	if (pckbc_is_console(iot, IO_KBD) == 0) {
105 		struct pckbc_internal t;
106 
107 		if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d))
108 			return (0);
109 		if (bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c)) {
110 			bus_space_unmap(iot, ioh_d, 1);
111 			return (0);
112 		}
113 
114 		memset(&t, 0, sizeof(t));
115 		t.t_iot = iot;
116 		t.t_ioh_d = ioh_d;
117 		t.t_ioh_c = ioh_c;
118 
119 		/* flush KBC */
120 		(void) pckbc_poll_data1(&t, PCKBC_KBD_SLOT, 0);
121 
122 		/* KBC selftest */
123 		if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0) {
124 			ok = 0;
125 			goto out;
126 		}
127 		res = pckbc_poll_data1(&t, PCKBC_KBD_SLOT, 0);
128 		if (res != 0x55) {
129 			printf("kbc selftest: %x\n", res);
130 			ok = 0;
131 		}
132  out:
133 		bus_space_unmap(iot, ioh_d, 1);
134 		bus_space_unmap(iot, ioh_c, 1);
135 	}
136 
137 	if (ok) {
138 		ia->ia_io[0].ir_addr = IO_KBD;
139 		ia->ia_io[0].ir_size = 5;
140 		ia->ia_nio = 1;
141 
142 		ia->ia_niomem = 0;
143 		ia->ia_nirq = 0;
144 		ia->ia_ndrq = 0;
145 	}
146 	return (ok);
147 }
148 
149 void
150 pckbc_isa_attach(parent, self, aux)
151 	struct device *parent, *self;
152 	void *aux;
153 {
154 	struct pckbc_isa_softc *isc = (void *)self;
155 	struct pckbc_softc *sc = &isc->sc_pckbc;
156 	struct isa_attach_args *ia = aux;
157 	struct pckbc_internal *t;
158 	bus_space_tag_t iot;
159 	bus_space_handle_t ioh_d, ioh_c;
160 
161 	isc->sc_ic = ia->ia_ic;
162 	iot = ia->ia_iot;
163 
164 	switch (ia->ia_nirq) {
165 	case 1:
166 		/* Both channels use the same IRQ. */
167 		isc->sc_irq[PCKBC_KBD_SLOT] =
168 		    isc->sc_irq[PCKBC_AUX_SLOT] = ia->ia_irq[0].ir_irq;
169 		break;
170 
171 	case 2:
172 		/* First IRQ is kbd, second IRQ is aux port. */
173 		isc->sc_irq[PCKBC_KBD_SLOT] = ia->ia_irq[0].ir_irq;
174 		isc->sc_irq[PCKBC_AUX_SLOT] = ia->ia_irq[1].ir_irq;
175 		break;
176 
177 	default:
178 		/* Set up IRQs for "normal" ISA. */
179 		isc->sc_irq[PCKBC_KBD_SLOT] = 1;
180 		isc->sc_irq[PCKBC_AUX_SLOT] = 12;
181 		break;
182 	}
183 
184 	sc->intr_establish = pckbc_isa_intr_establish;
185 
186 	if (pckbc_is_console(iot, IO_KBD)) {
187 		t = &pckbc_consdata;
188 		ioh_d = t->t_ioh_d;
189 		ioh_c = t->t_ioh_c;
190 		pckbc_console_attached = 1;
191 		/* t->t_cmdbyte was initialized by cnattach */
192 	} else {
193 		if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d) ||
194 		    bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c))
195 			panic("pckbc_attach: couldn't map");
196 
197 		t = malloc(sizeof(struct pckbc_internal), M_DEVBUF,
198 		    M_WAITOK|M_ZERO);
199 		t->t_iot = iot;
200 		t->t_ioh_d = ioh_d;
201 		t->t_ioh_c = ioh_c;
202 		t->t_addr = IO_KBD;
203 		t->t_cmdbyte = KC8_CPU; /* Enable ports */
204 		callout_init(&t->t_cleanup);
205 	}
206 
207 	t->t_sc = sc;
208 	sc->id = t;
209 
210 	printf("\n");
211 
212 	/* Finish off the attach. */
213 	pckbc_attach(sc);
214 }
215 
216 void
217 pckbc_isa_intr_establish(sc, slot)
218 	struct pckbc_softc *sc;
219 	pckbc_slot_t slot;
220 {
221 	struct pckbc_isa_softc *isc = (void *) sc;
222 	void *rv;
223 
224 	rv = isa_intr_establish(isc->sc_ic, isc->sc_irq[slot], IST_EDGE,
225 	    IPL_TTY, pckbcintr, sc);
226 	if (rv == NULL) {
227 		printf("%s: unable to establish interrupt for %s slot\n",
228 		    sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
229 	} else {
230 		printf("%s: using irq %d for %s slot\n", sc->sc_dv.dv_xname,
231 		    isc->sc_irq[slot], pckbc_slot_names[slot]);
232 	}
233 }
234