xref: /openbsd/sys/dev/isa/pckbc_isa.c (revision 91f110e0)
1 /*	$OpenBSD: pckbc_isa.c,v 1.14 2013/12/06 21:03:03 deraadt Exp $	*/
2 /*	$NetBSD: pckbc_isa.c,v 1.2 2000/03/23 07:01:35 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1998
6  *	Matthias Drochner.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/device.h>
33 #include <sys/malloc.h>
34 
35 #include <machine/bus.h>
36 
37 #include <dev/isa/isareg.h>
38 #include <dev/isa/isavar.h>
39 
40 #include <dev/ic/i8042reg.h>
41 #include <dev/ic/pckbcvar.h>
42 
43 int	pckbc_isa_match(struct device *, void *, void *);
44 void	pckbc_isa_attach(struct device *, struct device *, void *);
45 int	pckbc_isa_activate(struct device *, int);
46 
47 struct pckbc_isa_softc {
48 	struct pckbc_softc sc_pckbc;
49 
50 	isa_chipset_tag_t sc_ic;
51 	int sc_irq[PCKBC_NSLOTS];
52 };
53 
54 struct cfattach pckbc_isa_ca = {
55 	sizeof(struct pckbc_isa_softc), pckbc_isa_match, pckbc_isa_attach,
56 	NULL, pckbc_isa_activate
57 };
58 
59 void	pckbc_isa_intr_establish(struct pckbc_softc *, pckbc_slot_t);
60 
61 int
62 pckbc_isa_match(struct device *parent, void *match, void *aux)
63 {
64 	struct isa_attach_args *ia = aux;
65 	bus_space_tag_t iot = ia->ia_iot;
66 	bus_space_handle_t ioh_d, ioh_c;
67 	int res;
68 
69 	/* If values are hardwired to something that they can't be, punt. */
70 	if ((ia->ia_iobase != IOBASEUNK && ia->ia_iobase != IO_KBD) ||
71 	    ia->ia_maddr != MADDRUNK ||
72 	    (ia->ia_irq != IRQUNK && ia->ia_irq != 1 /* XXX */) ||
73 	    ia->ia_drq != DRQUNK)
74 		return (0);
75 
76 	if (pckbc_is_console(iot, IO_KBD) == 0) {
77 		if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d))
78 			return (0);
79 		if (bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c))
80 			goto fail;
81 
82 		/* flush KBC */
83 		(void) pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0);
84 
85 		/* KBC selftest */
86 		if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0)
87 			goto fail2;
88 		res = pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0);
89 		if (res != 0x55) {
90 			printf("kbc selftest: %x\n", res);
91 			goto fail2;
92 		}
93 		bus_space_unmap(iot, ioh_c, 1);
94 		bus_space_unmap(iot, ioh_d, 1);
95 	}
96 
97 	ia->ia_iobase = IO_KBD;
98 	ia->ia_iosize = 5;
99 	ia->ia_msize = 0x0;
100 
101 	return (1);
102 
103 fail2:
104 	bus_space_unmap(iot, ioh_c, 1);
105 fail:
106 	bus_space_unmap(iot, ioh_d, 1);
107 	return (0);
108 }
109 
110 int
111 pckbc_isa_activate(struct device *self, int act)
112 {
113 	struct pckbc_isa_softc *isc = (struct pckbc_isa_softc *)self;
114 	int rv = 0;
115 
116 	switch (act) {
117 	case DVACT_SUSPEND:
118 		rv = config_activate_children(self, act);
119 		pckbc_stop(&isc->sc_pckbc);
120 		break;
121 	case DVACT_RESUME:
122 		pckbc_reset(&isc->sc_pckbc);
123 		rv = config_activate_children(self, act);
124 		break;
125 	default:
126 		rv = config_activate_children(self, act);
127 		break;
128 	}
129 	return (rv);
130 }
131 
132 void
133 pckbc_isa_attach(struct device *parent, struct device *self, void *aux)
134 {
135 	struct pckbc_isa_softc *isc = (void *)self;
136 	struct pckbc_softc *sc = &isc->sc_pckbc;
137 	struct cfdata *cf = self->dv_cfdata;
138 	struct isa_attach_args *ia = aux;
139 	struct pckbc_internal *t;
140 	bus_space_tag_t iot;
141 	bus_space_handle_t ioh_d, ioh_c;
142 
143 	isc->sc_ic = ia->ia_ic;
144 	iot = ia->ia_iot;
145 
146 	/*
147 	 * Set up IRQs for "normal" ISA.
148 	 */
149 	isc->sc_irq[PCKBC_KBD_SLOT] = 1;
150 	isc->sc_irq[PCKBC_AUX_SLOT] = 12;
151 
152 	sc->intr_establish = pckbc_isa_intr_establish;
153 
154 	if (pckbc_is_console(iot, IO_KBD)) {
155 		t = &pckbc_consdata;
156 		pckbc_console_attached = 1;
157 		/* t->t_cmdbyte was initialized by cnattach */
158 	} else {
159 		if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d) ||
160 		    bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c))
161 			panic("pckbc_attach: couldn't map");
162 
163 		t = malloc(sizeof(*t), M_DEVBUF, M_WAITOK | M_ZERO);
164 		t->t_iot = iot;
165 		t->t_ioh_d = ioh_d;
166 		t->t_ioh_c = ioh_c;
167 		t->t_addr = IO_KBD;
168 		t->t_cmdbyte = KC8_CPU; /* Enable ports */
169 	}
170 
171 	t->t_sc = sc;
172 	sc->id = t;
173 
174 	printf("\n");
175 
176 	/* Finish off the attach. */
177 	pckbc_attach(sc, cf->cf_flags);
178 }
179 
180 void
181 pckbc_isa_intr_establish(struct pckbc_softc *sc, pckbc_slot_t slot)
182 {
183 	struct pckbc_isa_softc *isc = (void *)sc;
184 	void *rv;
185 
186 	rv = isa_intr_establish(isc->sc_ic, isc->sc_irq[slot], IST_EDGE,
187 	    IPL_TTY, pckbcintr, sc, sc->sc_dv.dv_xname);
188 	if (rv == NULL) {
189 		printf("%s: unable to establish interrupt for %s slot\n",
190 		    sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
191 	} else {
192 		printf("%s: using irq %d for %s slot\n", sc->sc_dv.dv_xname,
193 		    isc->sc_irq[slot], pckbc_slot_names[slot]);
194 	}
195 }
196