pswitch.c revision 331722
1/*-
2 * Copyright (C) 2002 Benno Rice.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * $FreeBSD: stable/11/sys/powerpc/powermac/pswitch.c 331722 2018-03-29 02:50:57Z eadler $
26 */
27
28#include "opt_ddb.h"
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/kdb.h>
33#include <sys/kernel.h>
34#include <sys/module.h>
35#include <sys/malloc.h>
36#include <sys/bus.h>
37#include <machine/bus.h>
38#include <sys/rman.h>
39
40#include <machine/resource.h>
41
42#include <dev/ofw/openfirm.h>
43
44#include <powerpc/powermac/maciovar.h>
45
46struct pswitch_softc {
47	int	sc_irqrid;
48	struct	resource *sc_irq;
49	void	*sc_ih;
50};
51
52static int	pswitch_probe(device_t);
53static int	pswitch_attach(device_t);
54
55static int	pswitch_intr(void *);
56
57static device_method_t pswitch_methods[] = {
58	/* Device interface */
59	DEVMETHOD(device_probe,		pswitch_probe),
60	DEVMETHOD(device_attach,	pswitch_attach),
61
62	{ 0, 0 }
63};
64
65static driver_t pswitch_driver = {
66	"pswitch",
67	pswitch_methods,
68	sizeof(struct pswitch_softc)
69};
70
71static devclass_t pswitch_devclass;
72
73DRIVER_MODULE(pswitch, macio, pswitch_driver, pswitch_devclass, 0, 0);
74
75static int
76pswitch_probe(device_t dev)
77{
78	char	*type = macio_get_devtype(dev);
79
80	if (strcmp(type, "gpio") != 0)
81		return (ENXIO);
82
83	device_set_desc(dev, "GPIO Programmer's Switch");
84	return (0);
85}
86
87static int
88pswitch_attach(device_t dev)
89{
90	struct		pswitch_softc *sc;
91	phandle_t	node, child;
92	char		type[32];
93	u_int		irq[2];
94
95	sc = device_get_softc(dev);
96	node = macio_get_node(dev);
97
98	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
99		if (OF_getprop(child, "device_type", type, 32) == -1)
100			continue;
101
102		if (strcmp(type, "programmer-switch") == 0)
103			break;
104	}
105
106	if (child == 0) {
107		device_printf(dev, "could not find correct node\n");
108		return (ENXIO);
109	}
110
111	if (OF_getprop(child, "interrupts", irq, sizeof(irq)) == -1) {
112		device_printf(dev, "could not get interrupt\n");
113		return (ENXIO);
114	}
115
116	sc->sc_irqrid = 0;
117	sc->sc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->sc_irqrid,
118	    irq[0], irq[0], 1, RF_ACTIVE);
119	if (sc->sc_irq == NULL) {
120		device_printf(dev, "could not allocate interrupt\n");
121		return (ENXIO);
122	}
123
124	if (bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC,
125	    pswitch_intr, NULL, dev, &sc->sc_ih) != 0) {
126		device_printf(dev, "could not setup interrupt\n");
127		bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid,
128		    sc->sc_irq);
129		return (ENXIO);
130	}
131
132	return (0);
133}
134
135static int
136pswitch_intr(void *arg)
137{
138	device_t	dev;
139
140	dev = (device_t)arg;
141
142	kdb_enter(KDB_WHY_POWERPC, device_get_nameunit(dev));
143	return (FILTER_HANDLED);
144}
145