btn_obio.c revision 1.1
1/*	$NetBSD: btn_obio.c,v 1.1 2006/04/16 02:22:33 nonaka Exp $	*/
2
3/*-
4 * Copyright (c) 2005, 2006 NONAKA Kimihiro
5 * 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: btn_obio.c,v 1.1 2006/04/16 02:22:33 nonaka Exp $");
31
32#include <sys/types.h>
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/device.h>
36#include <sys/malloc.h>
37#include <sys/kernel.h>
38#include <sys/conf.h>
39#include <sys/ioctl.h>
40#include <sys/callout.h>
41
42#include <arm/xscale/i80321var.h>
43
44#include <machine/bus.h>
45#include <machine/intr.h>
46
47#include <dev/sysmon/sysmonvar.h>
48#include <dev/sysmon/sysmon_taskq.h>
49
50#include <evbarm/hdl_g/hdlgreg.h>
51#include <evbarm/hdl_g/obiovar.h>
52
53struct btn_obio_softc {
54	struct device		sc_dev;
55	bus_space_tag_t		sc_iot;
56	bus_space_handle_t	sc_ioh;
57	void			*sc_ih;
58
59	struct sysmon_pswitch	sc_smpsw[2];
60
61	int			sc_mask;
62};
63
64static int btn_obio_match(struct device *, struct cfdata *, void *);
65static void btn_obio_attach(struct device *, struct device *, void *);
66
67static int btn_intr(void *aux);
68static void btn_sysmon_pressed_event(void *arg);
69
70CFATTACH_DECL(btn_obio, sizeof(struct btn_obio_softc),
71    btn_obio_match, btn_obio_attach, NULL, NULL);
72
73static int
74btn_obio_match(struct device *parent, struct cfdata *cfp, void *aux)
75{
76
77	/* We take it on faith that the device is there. */
78	return 1;
79}
80
81static void
82btn_obio_attach(struct device *parent, struct device *self, void *aux)
83{
84	struct obio_attach_args *oba = aux;
85	struct btn_obio_softc *sc = (void *)self;
86	int error;
87
88	sc->sc_iot = oba->oba_st;
89	error = bus_space_map(sc->sc_iot, oba->oba_addr, 1, 0, &sc->sc_ioh);
90	if (error) {
91		aprint_error(": failed to map registers: %d\n", error);
92		return;
93	}
94
95	sysmon_power_settype("landisk");
96	sysmon_task_queue_init();
97
98	/* power switch */
99	sc->sc_smpsw[0].smpsw_name = device_xname(&sc->sc_dev);
100	sc->sc_smpsw[0].smpsw_type = PSWITCH_TYPE_POWER;
101	if (sysmon_pswitch_register(&sc->sc_smpsw[0]) != 0) {
102		aprint_error(": unable to register power button with sysmon\n");
103		return;
104	}
105	sc->sc_mask |= BTNSTAT_POWER;
106	hdlg_enable_pldintr(INTEN_PWRSW);
107
108	/* reset button */
109	sc->sc_smpsw[1].smpsw_name = device_xname(&sc->sc_dev);
110	sc->sc_smpsw[1].smpsw_type = PSWITCH_TYPE_RESET;
111	if (sysmon_pswitch_register(&sc->sc_smpsw[1]) != 0) {
112		aprint_error(": unable to register reset button with sysmon\n");
113		return;
114	}
115	sc->sc_mask |= BTNSTAT_RESET;
116
117	sc->sc_ih = i80321_intr_establish(oba->oba_irq, IPL_TTY, btn_intr, sc);
118	if (sc->sc_ih == NULL) {
119		aprint_error(": couldn't establish intr handler");
120		return;
121	}
122	hdlg_enable_pldintr(INTEN_BUTTON);
123
124	aprint_normal("\n");
125}
126
127static int
128btn_intr(void *arg)
129{
130	struct btn_obio_softc *sc = (void *)arg;
131	int status;
132	int rv;
133
134	status = (int8_t)bus_space_read_1(sc->sc_iot, sc->sc_ioh, 0);
135	if (status == -1) {
136		return 0;
137	}
138
139	rv = 0;
140	status = ~status;
141	if (status & BTNSTAT_POWER) {
142		if (sc->sc_mask & BTNSTAT_POWER) {
143			hdlg_disable_pldintr(INTEN_PWRSW|INTEN_BUTTON);
144			i80321_intr_disestablish(sc->sc_ih);
145			sc->sc_ih = NULL;
146			sysmon_task_queue_sched(0, btn_sysmon_pressed_event,
147			    &sc->sc_smpsw[0]);
148		} else {
149			aprint_error("%s: power button pressed\n",
150			    device_xname(&sc->sc_dev));
151		}
152		rv = 1;
153	} else if (status & BTNSTAT_RESET) {
154		if (sc->sc_mask & BTNSTAT_RESET) {
155			hdlg_disable_pldintr(INTEN_PWRSW|INTEN_BUTTON);
156			i80321_intr_disestablish(sc->sc_ih);
157			sc->sc_ih = NULL;
158			sysmon_task_queue_sched(0, btn_sysmon_pressed_event,
159			    &sc->sc_smpsw[1]);
160		} else {
161			aprint_error("%s: reset button pressed\n",
162			    device_xname(&sc->sc_dev));
163		}
164		rv = 1;
165	}
166
167	return rv;
168}
169
170static void
171btn_sysmon_pressed_event(void *arg)
172{
173	struct sysmon_pswitch *smpsw = (void *)arg;
174
175	sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
176}
177