1/*	$OpenBSD: pckbc_isa.c,v 1.19 2015/08/18 06:54:00 stsp 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/device.h>
32#include <sys/malloc.h>
33
34#include <machine/bus.h>
35
36#include <dev/isa/isareg.h>
37#include <dev/isa/isavar.h>
38
39#include <dev/ic/i8042reg.h>
40#include <dev/ic/pckbcvar.h>
41
42int	pckbc_isa_match(struct device *, void *, void *);
43void	pckbc_isa_attach(struct device *, struct device *, void *);
44int	pckbc_isa_activate(struct device *, int);
45
46const struct cfattach pckbc_isa_ca = {
47	sizeof(struct pckbc_softc), pckbc_isa_match, pckbc_isa_attach,
48	NULL, pckbc_isa_activate
49};
50
51int
52pckbc_isa_match(struct device *parent, void *match, void *aux)
53{
54	struct isa_attach_args *ia = aux;
55	bus_space_tag_t iot = ia->ia_iot;
56	bus_space_handle_t ioh_d, ioh_c;
57	int res;
58
59	/* If values are hardwired to something that they can't be, punt. */
60	if ((ia->ia_iobase != IOBASEUNK && ia->ia_iobase != IO_KBD) ||
61	    ia->ia_maddr != MADDRUNK ||
62	    (ia->ia_irq != IRQUNK && ia->ia_irq != 1 /* XXX */) ||
63	    ia->ia_drq != DRQUNK)
64		return (0);
65
66	if (pckbc_is_console(iot, IO_KBD) == 0) {
67		if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d))
68			return (0);
69		if (bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c))
70			goto fail;
71
72		/* flush KBC */
73		(void) pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0);
74
75		/* KBC selftest */
76		if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0)
77			goto fail2;
78		res = pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0);
79		if (res != 0x55) {
80			printf("kbc selftest: %x\n", res);
81			goto fail2;
82		}
83		bus_space_unmap(iot, ioh_c, 1);
84		bus_space_unmap(iot, ioh_d, 1);
85	}
86
87	ia->ia_iobase = IO_KBD;
88	ia->ia_iosize = 5;
89	ia->ia_msize = 0x0;
90	ia->ipa_nirq = PCKBC_NSLOTS;
91	ia->ipa_irq[PCKBC_KBD_SLOT].num = 1;
92	ia->ipa_irq[PCKBC_AUX_SLOT].num = 12;
93
94	return (1);
95
96fail2:
97	bus_space_unmap(iot, ioh_c, 1);
98fail:
99	bus_space_unmap(iot, ioh_d, 1);
100	return (0);
101}
102
103int
104pckbc_isa_activate(struct device *self, int act)
105{
106	struct pckbc_softc *sc = (struct pckbc_softc *)self;
107	int rv = 0;
108
109	switch (act) {
110	case DVACT_SUSPEND:
111		rv = config_activate_children(self, act);
112		pckbc_stop(sc);
113		break;
114	case DVACT_RESUME:
115		pckbc_reset(sc);
116		rv = config_activate_children(self, act);
117		break;
118	default:
119		rv = config_activate_children(self, act);
120		break;
121	}
122	return (rv);
123}
124
125void
126pckbc_isa_attach(struct device *parent, struct device *self, void *aux)
127{
128	struct pckbc_softc *sc = (struct pckbc_softc *)self;
129	struct cfdata *cf = self->dv_cfdata;
130	struct isa_attach_args *ia = aux;
131	struct pckbc_internal *t;
132	bus_space_tag_t iot;
133	bus_space_handle_t ioh_d, ioh_c;
134	void *rv;
135	int slot;
136
137	iot = ia->ia_iot;
138
139	printf("\n");
140
141	for (slot = 0; slot < PCKBC_NSLOTS; slot++) {
142		rv = isa_intr_establish(ia->ia_ic, ia->ipa_irq[slot].num,
143		    IST_EDGE, IPL_TTY, pckbcintr, sc, sc->sc_dv.dv_xname);
144		if (rv == NULL) {
145			printf("%s: unable to establish interrupt for irq %d\n",
146			    sc->sc_dv.dv_xname, ia->ipa_irq[slot].num);
147			/* XXX fail attach? */
148		}
149	}
150
151	if (pckbc_is_console(iot, IO_KBD)) {
152		t = &pckbc_consdata;
153		pckbc_console_attached = 1;
154		/* t->t_cmdbyte was initialized by cnattach */
155	} else {
156		if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d) ||
157		    bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c))
158			panic("pckbc_attach: couldn't map");
159
160		t = malloc(sizeof(*t), M_DEVBUF, M_WAITOK | M_ZERO);
161		t->t_iot = iot;
162		t->t_ioh_d = ioh_d;
163		t->t_ioh_c = ioh_c;
164		t->t_addr = IO_KBD;
165		t->t_cmdbyte = KC8_CPU; /* Enable ports */
166	}
167
168	t->t_sc = sc;
169	sc->id = t;
170
171	/* Finish off the attach. */
172	pckbc_attach(sc, cf->cf_flags);
173}
174