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