1/*	$NetBSD: nslu2_buttons.c,v 1.3 2008/04/28 20:23:17 martin Exp $	*/
2
3/*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Steve C. Woodford.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: nslu2_buttons.c,v 1.3 2008/04/28 20:23:17 martin Exp $");
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/device.h>
38
39#include <dev/sysmon/sysmonvar.h>
40#include <dev/sysmon/sysmon_taskq.h>
41
42#include <arm/xscale/ixp425reg.h>
43#include <arm/xscale/ixp425var.h>
44
45#include <evbarm/nslu2/nslu2reg.h>
46
47struct slugbutt_softc {
48	device_t sc_dev;
49	struct sysmon_pswitch sc_smpwr;
50	struct sysmon_pswitch sc_smrst;
51};
52
53static int slugbutt_attached;
54
55#define	SLUGBUTT_PWR_BIT	(1u << GPIO_BUTTON_PWR)
56#define	SLUGBUTT_RST_BIT	(1u << GPIO_BUTTON_RST)
57
58static void
59power_event(void *arg)
60{
61	struct slugbutt_softc *sc = arg;
62
63	sysmon_pswitch_event(&sc->sc_smpwr, PSWITCH_EVENT_PRESSED);
64}
65
66static int
67power_intr(void *arg)
68{
69	struct slugbutt_softc *sc = arg;
70	int rv;
71
72	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPISR, SLUGBUTT_PWR_BIT);
73
74	rv = sysmon_task_queue_sched(0, power_event, sc);
75	if (rv) {
76		printf("%s: WARNING: unable to queue power button "
77		    "callback: %d\n", device_xname(sc->sc_dev), rv);
78	}
79
80	return (1);
81}
82
83static void
84reset_event(void *arg)
85{
86	struct slugbutt_softc *sc = arg;
87
88	sysmon_pswitch_event(&sc->sc_smrst, PSWITCH_EVENT_PRESSED);
89}
90
91static int
92reset_intr(void *arg)
93{
94	struct slugbutt_softc *sc = arg;
95	int rv;
96
97	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPISR, SLUGBUTT_RST_BIT);
98
99	rv = sysmon_task_queue_sched(0, reset_event, sc);
100	if (rv) {
101		printf("%s: WARNING: unable to queue reset button "
102		    "callback: %d\n", device_xname(sc->sc_dev), rv);
103	}
104
105	return (1);
106}
107
108static void
109slugbutt_deferred(device_t self)
110{
111	struct slugbutt_softc *sc = device_private(self);
112	struct ixp425_softc *ixsc = ixp425_softc;
113	uint32_t reg;
114
115	sc->sc_dev = self;
116
117	/* Configure the GPIO pins as inputs */
118	reg = GPIO_CONF_READ_4(ixsc, IXP425_GPIO_GPOER);
119	reg |= SLUGBUTT_PWR_BIT | SLUGBUTT_RST_BIT;
120	GPIO_CONF_WRITE_4(ixsc, IXP425_GPIO_GPOER, reg);
121
122	/* Configure the input type: Falling edge */
123	reg = GPIO_CONF_READ_4(ixsc, GPIO_TYPE_REG(GPIO_BUTTON_PWR));
124	reg &= ~GPIO_TYPE(GPIO_BUTTON_PWR, GPIO_TYPE_MASK);
125	reg |= GPIO_TYPE(GPIO_BUTTON_PWR, GPIO_TYPE_EDG_FALLING);
126	GPIO_CONF_WRITE_4(ixsc, GPIO_TYPE_REG(GPIO_BUTTON_PWR), reg);
127
128	reg = GPIO_CONF_READ_4(ixsc, GPIO_TYPE_REG(GPIO_BUTTON_RST));
129	reg &= ~GPIO_TYPE(GPIO_BUTTON_RST, GPIO_TYPE_MASK);
130	reg |= GPIO_TYPE(GPIO_BUTTON_RST, GPIO_TYPE_EDG_FALLING);
131	GPIO_CONF_WRITE_4(ixsc, GPIO_TYPE_REG(GPIO_BUTTON_RST), reg);
132
133	/* Clear any existing interrupt */
134	GPIO_CONF_WRITE_4(ixsc, IXP425_GPIO_GPISR, SLUGBUTT_PWR_BIT |
135	    SLUGBUTT_RST_BIT);
136
137	sysmon_task_queue_init();
138
139	sc->sc_smpwr.smpsw_name = device_xname(sc->sc_dev);
140	sc->sc_smpwr.smpsw_type = PSWITCH_TYPE_POWER;
141
142	if (sysmon_pswitch_register(&sc->sc_smpwr) != 0) {
143		printf("%s: unable to register power button with sysmon\n",
144		    device_xname(sc->sc_dev));
145		return;
146	}
147
148	sc->sc_smrst.smpsw_name = device_xname(sc->sc_dev);
149	sc->sc_smrst.smpsw_type = PSWITCH_TYPE_RESET;
150
151	if (sysmon_pswitch_register(&sc->sc_smrst) != 0) {
152		printf("%s: unable to register reset button with sysmon\n",
153		    device_xname(sc->sc_dev));
154		return;
155	}
156
157	/* Hook the interrupts */
158	ixp425_intr_establish(BUTTON_PWR_INT, IPL_TTY, power_intr, sc);
159	ixp425_intr_establish(BUTTON_RST_INT, IPL_TTY, reset_intr, sc);
160}
161
162
163static int
164slugbutt_match(device_t parent, cfdata_t cf, void *aux)
165{
166
167	return (slugbutt_attached == 0);
168}
169
170static void
171slugbutt_attach(device_t parent, device_t self, void *aux)
172{
173
174	slugbutt_attached = 1;
175
176	aprint_normal(": Power and Reset buttons\n");
177
178	/* Defer, to ensure ixp425_softc has been initialised */
179	config_interrupts(self, slugbutt_deferred);
180}
181
182CFATTACH_DECL_NEW(slugbutt, sizeof(struct slugbutt_softc),
183    slugbutt_match, slugbutt_attach, NULL, NULL);
184