1 /* $NetBSD: btn_obio.c,v 1.4 2012/10/27 17:17:47 chs 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.4 2012/10/27 17:17:47 chs 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 device_t 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(device_t, cfdata_t, void *); 64 static void btn_obio_attach(device_t, device_t, void *); 65 66 static int btn_intr(void *aux); 67 static void btn_sysmon_pressed_event(void *arg); 68 69 CFATTACH_DECL_NEW(btn_obio, sizeof(struct btn_obio_softc), 70 btn_obio_match, btn_obio_attach, NULL, NULL); 71 72 static int 73 btn_obio_match(device_t parent, cfdata_t cf, 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(device_t parent, device_t self, void *aux) 82 { 83 struct obio_attach_args *oba = aux; 84 struct btn_obio_softc *sc = device_private(self); 85 int error; 86 87 sc->sc_dev = self; 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(self); 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(self); 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 127 static int 128 btn_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 170 static void 171 btn_sysmon_pressed_event(void *arg) 172 { 173 struct sysmon_pswitch *smpsw = (void *)arg; 174 175 sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED); 176 } 177