xref: /netbsd-src/sys/arch/evbarm/nslu2/nslu2_buttons.c (revision a2b8c7fb0742c36745ea1fd35822da4277b62383)
1*a2b8c7fbSmsaitoh /*	$NetBSD: nslu2_buttons.c,v 1.4 2012/10/14 14:20:58 msaitoh Exp $	*/
26f616773Sscw 
36f616773Sscw /*-
46f616773Sscw  * Copyright (c) 2006 The NetBSD Foundation, Inc.
56f616773Sscw  * All rights reserved.
66f616773Sscw  *
76f616773Sscw  * This code is derived from software contributed to The NetBSD Foundation
86f616773Sscw  * by Steve C. Woodford.
96f616773Sscw  *
106f616773Sscw  * Redistribution and use in source and binary forms, with or without
116f616773Sscw  * modification, are permitted provided that the following conditions
126f616773Sscw  * are met:
136f616773Sscw  * 1. Redistributions of source code must retain the above copyright
146f616773Sscw  *    notice, this list of conditions and the following disclaimer.
156f616773Sscw  * 2. Redistributions in binary form must reproduce the above copyright
166f616773Sscw  *    notice, this list of conditions and the following disclaimer in the
176f616773Sscw  *    documentation and/or other materials provided with the distribution.
186f616773Sscw  *
196f616773Sscw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
206f616773Sscw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
216f616773Sscw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
226f616773Sscw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
236f616773Sscw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
246f616773Sscw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
256f616773Sscw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
266f616773Sscw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
276f616773Sscw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
286f616773Sscw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
296f616773Sscw  * POSSIBILITY OF SUCH DAMAGE.
306f616773Sscw  */
316f616773Sscw 
326f616773Sscw #include <sys/cdefs.h>
33*a2b8c7fbSmsaitoh __KERNEL_RCSID(0, "$NetBSD: nslu2_buttons.c,v 1.4 2012/10/14 14:20:58 msaitoh Exp $");
346f616773Sscw 
356f616773Sscw #include <sys/param.h>
366f616773Sscw #include <sys/systm.h>
376f616773Sscw #include <sys/device.h>
386f616773Sscw 
396f616773Sscw #include <dev/sysmon/sysmonvar.h>
406f616773Sscw #include <dev/sysmon/sysmon_taskq.h>
416f616773Sscw 
426f616773Sscw #include <arm/xscale/ixp425reg.h>
436f616773Sscw #include <arm/xscale/ixp425var.h>
446f616773Sscw 
456f616773Sscw #include <evbarm/nslu2/nslu2reg.h>
466f616773Sscw 
476f616773Sscw struct slugbutt_softc {
48*a2b8c7fbSmsaitoh 	device_t sc_dev;
496f616773Sscw 	struct sysmon_pswitch sc_smpwr;
506f616773Sscw 	struct sysmon_pswitch sc_smrst;
516f616773Sscw };
526f616773Sscw 
536f616773Sscw static int slugbutt_attached;
546f616773Sscw 
55c9e50b3cSscw #define	SLUGBUTT_PWR_BIT	(1u << GPIO_BUTTON_PWR)
56c9e50b3cSscw #define	SLUGBUTT_RST_BIT	(1u << GPIO_BUTTON_RST)
57c9e50b3cSscw 
586f616773Sscw static void
power_event(void * arg)596f616773Sscw power_event(void *arg)
606f616773Sscw {
616f616773Sscw 	struct slugbutt_softc *sc = arg;
626f616773Sscw 
636f616773Sscw 	sysmon_pswitch_event(&sc->sc_smpwr, PSWITCH_EVENT_PRESSED);
646f616773Sscw }
656f616773Sscw 
666f616773Sscw static int
power_intr(void * arg)676f616773Sscw power_intr(void *arg)
686f616773Sscw {
696f616773Sscw 	struct slugbutt_softc *sc = arg;
706f616773Sscw 	int rv;
716f616773Sscw 
72c9e50b3cSscw 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPISR, SLUGBUTT_PWR_BIT);
736f616773Sscw 
746f616773Sscw 	rv = sysmon_task_queue_sched(0, power_event, sc);
756f616773Sscw 	if (rv) {
766f616773Sscw 		printf("%s: WARNING: unable to queue power button "
77*a2b8c7fbSmsaitoh 		    "callback: %d\n", device_xname(sc->sc_dev), rv);
786f616773Sscw 	}
796f616773Sscw 
806f616773Sscw 	return (1);
816f616773Sscw }
826f616773Sscw 
836f616773Sscw static void
reset_event(void * arg)846f616773Sscw reset_event(void *arg)
856f616773Sscw {
866f616773Sscw 	struct slugbutt_softc *sc = arg;
876f616773Sscw 
886f616773Sscw 	sysmon_pswitch_event(&sc->sc_smrst, PSWITCH_EVENT_PRESSED);
896f616773Sscw }
906f616773Sscw 
916f616773Sscw static int
reset_intr(void * arg)926f616773Sscw reset_intr(void *arg)
936f616773Sscw {
946f616773Sscw 	struct slugbutt_softc *sc = arg;
956f616773Sscw 	int rv;
966f616773Sscw 
97c9e50b3cSscw 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPISR, SLUGBUTT_RST_BIT);
986f616773Sscw 
996f616773Sscw 	rv = sysmon_task_queue_sched(0, reset_event, sc);
1006f616773Sscw 	if (rv) {
1016f616773Sscw 		printf("%s: WARNING: unable to queue reset button "
102*a2b8c7fbSmsaitoh 		    "callback: %d\n", device_xname(sc->sc_dev), rv);
1036f616773Sscw 	}
1046f616773Sscw 
1056f616773Sscw 	return (1);
1066f616773Sscw }
1076f616773Sscw 
1086f616773Sscw static void
slugbutt_deferred(device_t self)109*a2b8c7fbSmsaitoh slugbutt_deferred(device_t self)
1106f616773Sscw {
111*a2b8c7fbSmsaitoh 	struct slugbutt_softc *sc = device_private(self);
1126f616773Sscw 	struct ixp425_softc *ixsc = ixp425_softc;
1136f616773Sscw 	uint32_t reg;
1146f616773Sscw 
115*a2b8c7fbSmsaitoh 	sc->sc_dev = self;
116*a2b8c7fbSmsaitoh 
1176f616773Sscw 	/* Configure the GPIO pins as inputs */
1186f616773Sscw 	reg = GPIO_CONF_READ_4(ixsc, IXP425_GPIO_GPOER);
119c9e50b3cSscw 	reg |= SLUGBUTT_PWR_BIT | SLUGBUTT_RST_BIT;
1206f616773Sscw 	GPIO_CONF_WRITE_4(ixsc, IXP425_GPIO_GPOER, reg);
1216f616773Sscw 
1226f616773Sscw 	/* Configure the input type: Falling edge */
1236f616773Sscw 	reg = GPIO_CONF_READ_4(ixsc, GPIO_TYPE_REG(GPIO_BUTTON_PWR));
1246f616773Sscw 	reg &= ~GPIO_TYPE(GPIO_BUTTON_PWR, GPIO_TYPE_MASK);
1256f616773Sscw 	reg |= GPIO_TYPE(GPIO_BUTTON_PWR, GPIO_TYPE_EDG_FALLING);
1266f616773Sscw 	GPIO_CONF_WRITE_4(ixsc, GPIO_TYPE_REG(GPIO_BUTTON_PWR), reg);
1276f616773Sscw 
1286f616773Sscw 	reg = GPIO_CONF_READ_4(ixsc, GPIO_TYPE_REG(GPIO_BUTTON_RST));
1296f616773Sscw 	reg &= ~GPIO_TYPE(GPIO_BUTTON_RST, GPIO_TYPE_MASK);
1306f616773Sscw 	reg |= GPIO_TYPE(GPIO_BUTTON_RST, GPIO_TYPE_EDG_FALLING);
1316f616773Sscw 	GPIO_CONF_WRITE_4(ixsc, GPIO_TYPE_REG(GPIO_BUTTON_RST), reg);
1326f616773Sscw 
1336f616773Sscw 	/* Clear any existing interrupt */
134c9e50b3cSscw 	GPIO_CONF_WRITE_4(ixsc, IXP425_GPIO_GPISR, SLUGBUTT_PWR_BIT |
135c9e50b3cSscw 	    SLUGBUTT_RST_BIT);
1366f616773Sscw 
1376f616773Sscw 	sysmon_task_queue_init();
1386f616773Sscw 
139*a2b8c7fbSmsaitoh 	sc->sc_smpwr.smpsw_name = device_xname(sc->sc_dev);
1406f616773Sscw 	sc->sc_smpwr.smpsw_type = PSWITCH_TYPE_POWER;
1416f616773Sscw 
1426f616773Sscw 	if (sysmon_pswitch_register(&sc->sc_smpwr) != 0) {
1436f616773Sscw 		printf("%s: unable to register power button with sysmon\n",
144*a2b8c7fbSmsaitoh 		    device_xname(sc->sc_dev));
1456f616773Sscw 		return;
1466f616773Sscw 	}
1476f616773Sscw 
148*a2b8c7fbSmsaitoh 	sc->sc_smrst.smpsw_name = device_xname(sc->sc_dev);
1496f616773Sscw 	sc->sc_smrst.smpsw_type = PSWITCH_TYPE_RESET;
1506f616773Sscw 
1516f616773Sscw 	if (sysmon_pswitch_register(&sc->sc_smrst) != 0) {
1526f616773Sscw 		printf("%s: unable to register reset button with sysmon\n",
153*a2b8c7fbSmsaitoh 		    device_xname(sc->sc_dev));
1546f616773Sscw 		return;
1556f616773Sscw 	}
1566f616773Sscw 
1576f616773Sscw 	/* Hook the interrupts */
1586f616773Sscw 	ixp425_intr_establish(BUTTON_PWR_INT, IPL_TTY, power_intr, sc);
1596f616773Sscw 	ixp425_intr_establish(BUTTON_RST_INT, IPL_TTY, reset_intr, sc);
1606f616773Sscw }
1616f616773Sscw 
1626f616773Sscw 
1636f616773Sscw static int
slugbutt_match(device_t parent,cfdata_t cf,void * aux)164*a2b8c7fbSmsaitoh slugbutt_match(device_t parent, cfdata_t cf, void *aux)
1656f616773Sscw {
1666f616773Sscw 
1676f616773Sscw 	return (slugbutt_attached == 0);
1686f616773Sscw }
1696f616773Sscw 
1706f616773Sscw static void
slugbutt_attach(device_t parent,device_t self,void * aux)171*a2b8c7fbSmsaitoh slugbutt_attach(device_t parent, device_t self, void *aux)
1726f616773Sscw {
1736f616773Sscw 
1746f616773Sscw 	slugbutt_attached = 1;
1756f616773Sscw 
1766f616773Sscw 	aprint_normal(": Power and Reset buttons\n");
1776f616773Sscw 
1786f616773Sscw 	/* Defer, to ensure ixp425_softc has been initialised */
1796f616773Sscw 	config_interrupts(self, slugbutt_deferred);
1806f616773Sscw }
1816f616773Sscw 
182*a2b8c7fbSmsaitoh CFATTACH_DECL_NEW(slugbutt, sizeof(struct slugbutt_softc),
1836f616773Sscw     slugbutt_match, slugbutt_attach, NULL, NULL);
184