xref: /freebsd/sys/powerpc/powermac/pswitch.c (revision 42249ef2)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 2002 Benno Rice.
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 Benno Rice ``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 TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #include "opt_ddb.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kdb.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/malloc.h>
38 #include <sys/bus.h>
39 #include <machine/bus.h>
40 #include <sys/rman.h>
41 
42 #include <machine/resource.h>
43 
44 #include <dev/ofw/openfirm.h>
45 
46 #include <powerpc/powermac/maciovar.h>
47 
48 struct pswitch_softc {
49 	int	sc_irqrid;
50 	struct	resource *sc_irq;
51 	void	*sc_ih;
52 };
53 
54 static int	pswitch_probe(device_t);
55 static int	pswitch_attach(device_t);
56 
57 static int	pswitch_intr(void *);
58 
59 static device_method_t pswitch_methods[] = {
60 	/* Device interface */
61 	DEVMETHOD(device_probe,		pswitch_probe),
62 	DEVMETHOD(device_attach,	pswitch_attach),
63 
64 	{ 0, 0 }
65 };
66 
67 static driver_t pswitch_driver = {
68 	"pswitch",
69 	pswitch_methods,
70 	sizeof(struct pswitch_softc)
71 };
72 
73 static devclass_t pswitch_devclass;
74 
75 DRIVER_MODULE(pswitch, macio, pswitch_driver, pswitch_devclass, 0, 0);
76 
77 static int
78 pswitch_probe(device_t dev)
79 {
80 	char	*type = macio_get_devtype(dev);
81 
82 	if (strcmp(type, "gpio") != 0)
83 		return (ENXIO);
84 
85 	device_set_desc(dev, "GPIO Programmer's Switch");
86 	return (0);
87 }
88 
89 static int
90 pswitch_attach(device_t dev)
91 {
92 	struct		pswitch_softc *sc;
93 	phandle_t	node, child;
94 	char		type[32];
95 	u_int		irq[2];
96 
97 	sc = device_get_softc(dev);
98 	node = macio_get_node(dev);
99 
100 	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
101 		if (OF_getprop(child, "device_type", type, 32) == -1)
102 			continue;
103 
104 		if (strcmp(type, "programmer-switch") == 0)
105 			break;
106 	}
107 
108 	if (child == 0) {
109 		device_printf(dev, "could not find correct node\n");
110 		return (ENXIO);
111 	}
112 
113 	if (OF_getprop(child, "interrupts", irq, sizeof(irq)) == -1) {
114 		device_printf(dev, "could not get interrupt\n");
115 		return (ENXIO);
116 	}
117 
118 	sc->sc_irqrid = 0;
119 	sc->sc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->sc_irqrid,
120 	    irq[0], irq[0], 1, RF_ACTIVE);
121 	if (sc->sc_irq == NULL) {
122 		device_printf(dev, "could not allocate interrupt\n");
123 		return (ENXIO);
124 	}
125 
126 	if (bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC,
127 	    pswitch_intr, NULL, dev, &sc->sc_ih) != 0) {
128 		device_printf(dev, "could not setup interrupt\n");
129 		bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid,
130 		    sc->sc_irq);
131 		return (ENXIO);
132 	}
133 
134 	return (0);
135 }
136 
137 static int
138 pswitch_intr(void *arg)
139 {
140 	device_t	dev;
141 
142 	dev = (device_t)arg;
143 
144 	kdb_enter(KDB_WHY_POWERPC, device_get_nameunit(dev));
145 	return (FILTER_HANDLED);
146 }
147