Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / arm / iomd / iomdkbc.c
blob359b1a4caf1adc6d533b26609818c831fbec9fe5
1 /* $NetBSD: iomdkbc.c,v 1.2 2005/12/11 12:16:47 christos Exp $ */
3 /*-
4 * Copyright (c) 2004 Ben Harris
5 * All rights reserved.
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. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: iomdkbc.c,v 1.2 2005/12/11 12:16:47 christos Exp $");
33 #include <sys/param.h>
34 #include <sys/device.h>
35 #include <sys/malloc.h>
36 #include <sys/systm.h>
38 #include <dev/pckbport/pckbportvar.h>
40 #include <machine/bus.h>
41 #include <machine/intr.h>
43 #include <arch/arm/iomd/iomdreg.h>
44 #include <arch/arm/iomd/iomdvar.h>
45 #include <arch/arm/iomd/iomdkbcvar.h>
47 /* XXX belongs in iomdreg.h */
48 #define IOMDKBC_TXE 0x80
49 #define IOMDKBC_TXB 0x40
50 #define IOMDKBC_RXF 0x20
51 #define IOMDKBC_RXB 0x10
52 #define IOMDKBC_ENA 0x08
53 #define IOMDKBC_RXP 0x04
54 #define IOMDKBC_DATA 0x02
55 #define IOMDKBC_CLK 0x01
57 #define IOMDKBC_DR 0x00
58 #define IOMDKBC_CR 0x01
60 #define KBC_DEVCMD_ACK 0xfa
61 #define KBC_DEVCMD_RESEND 0xfe
63 struct iomdkbc_softc {
64 struct device sc_dev;
65 struct iomdkbc_internal *sc_id;
68 struct iomdkbc_internal {
69 struct iomdkbc_softc *t_sc;
70 struct pckbport_tag *t_pt;
72 int t_haveport[PCKBPORT_NSLOTS];
73 bus_space_tag_t t_iot;
74 bus_space_handle_t t_ioh[PCKBPORT_NSLOTS];
76 void *t_rxih[PCKBPORT_NSLOTS];
77 int t_rxirq[PCKBPORT_NSLOTS];
80 static int iomdkbc_match(struct device *, struct cfdata *, void *);
81 static void iomdkbc_attach(struct device *, struct device *, void *);
83 static int iomdkbc_xt_translation(void *, pckbport_slot_t, int);
84 static int iomdkbc_send_devcmd(void *, pckbport_slot_t, u_char);
85 static int iomdkbc_poll_data1(void *, pckbport_slot_t);
86 static void iomdkbc_slot_enable(void *, pckbport_slot_t, int);
87 static void iomdkbc_intr_establish(void *, pckbport_slot_t);
88 static void iomdkbc_set_poll(void *, pckbport_slot_t, int);
90 static int iomdkbc_intr(void *);
92 CFATTACH_DECL(iomdkbc, sizeof(struct iomdkbc_softc),
93 iomdkbc_match, iomdkbc_attach, NULL, NULL);
95 static struct pckbport_accessops const iomdkbc_ops = {
96 iomdkbc_xt_translation,
97 iomdkbc_send_devcmd,
98 iomdkbc_poll_data1,
99 iomdkbc_slot_enable,
100 iomdkbc_intr_establish,
101 iomdkbc_set_poll
104 static struct iomdkbc_internal iomdkbc_cntag;
106 static int
107 iomdkbc_match(struct device *parent, struct cfdata *cf, void *aux)
109 struct kbd_attach_args *ka = aux;
110 struct opms_attach_args *pa = aux;
112 if (strcmp(ka->ka_name, "kbd") == 0 ||
113 strcmp(pa->pa_name, "opms") == 0)
114 return 1;
115 return 0;
118 static void
119 iomdkbc_attach(struct device *parent, struct device *self, void *aux)
121 struct kbd_attach_args *ka = aux;
122 struct opms_attach_args *pa = aux;
123 struct iomdkbc_softc *sc = (struct iomdkbc_softc *)self;
124 struct iomdkbc_internal *t;
126 printf("\n");
128 t = NULL;
129 if (strcmp(ka->ka_name, "kbd") == 0) {
130 /* XXX not really right, but good enough. */
131 if (iomdkbc_cntag.t_haveport[PCKBPORT_KBD_SLOT]) {
132 /* Have an iomdkbc as console. Assume it's this one.*/
133 t = &iomdkbc_cntag;
134 } else {
135 t = malloc(sizeof(struct iomdkbc_internal), M_DEVBUF,
136 M_NOWAIT | M_ZERO);
137 if (t == NULL) {
138 aprint_error(": no memory");
139 return;
141 t->t_haveport[PCKBPORT_KBD_SLOT] = 1;
142 t->t_iot = ka->ka_iot;
143 t->t_ioh[PCKBPORT_KBD_SLOT] = ka->ka_ioh;
145 t->t_rxih[PCKBPORT_KBD_SLOT] = intr_claim(ka->ka_rxirq,
146 IPL_TTY, sc->sc_dev.dv_xname, iomdkbc_intr, t);
147 t->t_rxirq[PCKBPORT_KBD_SLOT] = ka->ka_rxirq;
148 disable_irq(t->t_rxirq[PCKBPORT_KBD_SLOT]);
149 sc->sc_id = t;
150 t->t_sc = sc;
151 t->t_pt = pckbport_attach(t, &iomdkbc_ops);
152 pckbport_attach_slot(&sc->sc_dev, t->t_pt, PCKBPORT_KBD_SLOT);
155 if (strcmp(pa->pa_name, "opms") == 0) {
156 if (t == NULL) {
157 t = malloc(sizeof(struct iomdkbc_internal), M_DEVBUF,
158 M_NOWAIT | M_ZERO);
159 if (t == NULL) {
160 aprint_error(": no memory");
161 return;
164 t->t_haveport[PCKBPORT_AUX_SLOT] = 1;
165 t->t_iot = pa->pa_iot;
166 t->t_ioh[PCKBPORT_AUX_SLOT] = pa->pa_ioh;
167 t->t_rxih[PCKBPORT_AUX_SLOT] = intr_claim(pa->pa_irq,
168 IPL_TTY, sc->sc_dev.dv_xname, iomdkbc_intr, t);
169 t->t_rxirq[PCKBPORT_AUX_SLOT] = pa->pa_irq;
170 disable_irq(t->t_rxirq[PCKBPORT_AUX_SLOT]);
171 sc->sc_id = t;
172 t->t_sc = sc;
173 if (t->t_pt == NULL)
174 t->t_pt = pckbport_attach(t, &iomdkbc_ops);
175 pckbport_attach_slot(&sc->sc_dev, t->t_pt, PCKBPORT_AUX_SLOT);
179 static int
180 iomdkbc_send_devcmd(void *cookie, pckbport_slot_t slot, u_char cmd)
182 struct iomdkbc_internal *t = cookie;
183 bus_space_tag_t iot = t->t_iot;
184 bus_space_handle_t ioh = t->t_ioh[slot];
185 int timeout;
187 timeout = 10000;
188 while ((bus_space_read_1(iot, ioh, IOMDKBC_CR) &
189 IOMDKBC_TXE) == 0) {
190 DELAY(10);
191 if (--timeout == 0) return 0;
194 bus_space_write_1(iot, ioh, IOMDKBC_DR, cmd);
195 return 1;
198 static int
199 iomdkbc_poll_data1(void *cookie, pckbport_slot_t slot)
201 struct iomdkbc_internal *t = cookie;
202 bus_space_tag_t iot = t->t_iot;
203 bus_space_handle_t ioh = t->t_ioh[slot];
204 int timeout;
206 timeout = 10000;
207 while ((bus_space_read_1(iot, ioh, IOMDKBC_CR) &
208 IOMDKBC_RXF) == 0) {
209 DELAY(10);
210 if (--timeout == 0) return -1;
213 return bus_space_read_1(iot, ioh, IOMDKBC_DR);
217 * switch scancode translation on / off
218 * return nonzero on success
220 static int
221 iomdkbc_xt_translation(void *cookie, pckbport_slot_t slot, int on)
224 if (on)
225 return 0; /* Can't do XT translation */
226 else
227 return 1;
230 static void
231 iomdkbc_slot_enable(void *cookie, pckbport_slot_t slot, int on)
233 struct iomdkbc_internal *t = cookie;
234 bus_space_tag_t iot = t->t_iot;
235 bus_space_handle_t ioh = t->t_ioh[slot];
237 bus_space_write_1(iot, ioh, IOMDKBC_CR, on ? IOMDKBC_ENA : 0);
241 static void
242 iomdkbc_intr_establish(void *cookie, pckbport_slot_t slot)
244 struct iomdkbc_internal *t = cookie;
246 enable_irq(t->t_rxirq[slot]);
249 static void
250 iomdkbc_set_poll(void *cookie, pckbport_slot_t slot, int on)
252 struct iomdkbc_internal *t = cookie;
254 if (on)
255 disable_irq(t->t_rxirq[slot]);
256 else
257 enable_irq(t->t_rxirq[slot]);
260 static int
261 iomdkbc_intr(void *self)
263 struct iomdkbc_internal *t = self;
264 bus_space_tag_t iot = t->t_iot;
265 bus_space_handle_t ioh;
266 unsigned i;
267 int stat, ret;
269 ret = 0;
270 for (i = 0; i < PCKBPORT_NSLOTS; i++)
271 if (t->t_haveport[i]) {
272 ioh = t->t_ioh[i];
273 stat = bus_space_read_1(iot, ioh, IOMDKBC_CR);
274 if ((stat & IOMDKBC_RXF) == 0)
275 continue;
276 pckbportintr(t->t_pt, i,
277 bus_space_read_1(iot, ioh, IOMDKBC_DR));
278 ret = 1;
281 return ret;
285 iomdkbc_cnattach(bus_space_tag_t iot, bus_addr_t addr, int slot)
287 struct iomdkbc_internal *t = &iomdkbc_cntag;
289 t->t_iot = iot;
290 bus_space_map(iot, addr, 2, 0, &t->t_ioh[slot]);
291 t->t_haveport[slot] = 1;
292 iomdkbc_slot_enable(t, slot, 1);
293 return pckbport_cnattach(t, &iomdkbc_ops, slot);