xref: /freebsd/sys/powerpc/powermac/pswitch.c (revision f0adf7f5cdd241db2f2c817683191a6ef64a4e95)
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$
26  */
27 
28 #include "opt_ddb.h"
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/bus.h>
35 #include <machine/bus.h>
36 #include <sys/rman.h>
37 
38 #include <machine/resource.h>
39 
40 #include <dev/ofw/openfirm.h>
41 
42 #include <powerpc/powermac/maciovar.h>
43 
44 struct pswitch_softc {
45 	int	sc_irqrid;
46 	struct	resource *sc_irq;
47 	void	*sc_ih;
48 };
49 
50 static int	pswitch_probe(device_t);
51 static int	pswitch_attach(device_t);
52 
53 static void	pswitch_intr(void *);
54 
55 static device_method_t pswitch_methods[] = {
56 	/* Device interface */
57 	DEVMETHOD(device_probe,		pswitch_probe),
58 	DEVMETHOD(device_attach,	pswitch_attach),
59 
60 	{ 0, 0 }
61 };
62 
63 static driver_t pswitch_driver = {
64 	"pswitch",
65 	pswitch_methods,
66 	sizeof(struct pswitch_softc)
67 };
68 
69 static devclass_t pswitch_devclass;
70 
71 DRIVER_MODULE(pswitch, macio, pswitch_driver, pswitch_devclass, 0, 0);
72 
73 static int
74 pswitch_probe(device_t dev)
75 {
76 	char	*type = macio_get_devtype(dev);
77 
78 	if (strcmp(type, "gpio") != 0)
79 		return (ENXIO);
80 
81 	device_set_desc(dev, "GPIO Programmer's Switch");
82 	return (0);
83 }
84 
85 static int
86 pswitch_attach(device_t dev)
87 {
88 	struct		pswitch_softc *sc;
89 	phandle_t	node, child;
90 	char		type[32];
91 	u_int		irq[2];
92 
93 	sc = device_get_softc(dev);
94 	node = macio_get_node(dev);
95 
96 	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
97 		if (OF_getprop(child, "device_type", type, 32) == -1)
98 			continue;
99 
100 		if (strcmp(type, "programmer-switch") == 0)
101 			break;
102 	}
103 
104 	if (child == 0) {
105 		device_printf(dev, "could not find correct node\n");
106 		return (ENXIO);
107 	}
108 
109 	if (OF_getprop(child, "interrupts", irq, sizeof(irq)) == -1) {
110 		device_printf(dev, "could not get interrupt\n");
111 		return (ENXIO);
112 	}
113 
114 	sc->sc_irqrid = 0;
115 	sc->sc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->sc_irqrid,
116 	    irq[0], irq[0], 1, RF_ACTIVE);
117 	if (sc->sc_irq == NULL) {
118 		device_printf(dev, "could not allocate interrupt\n");
119 		return (ENXIO);
120 	}
121 
122 	if (bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC | INTR_FAST,
123 	    pswitch_intr, dev, &sc->sc_ih) != 0) {
124 		device_printf(dev, "could not setup interrupt\n");
125 		bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid,
126 		    sc->sc_irq);
127 		return (ENXIO);
128 	}
129 
130 	return (0);
131 }
132 
133 static void
134 pswitch_intr(void *arg)
135 {
136 	device_t	dev;
137 
138 	dev = (device_t)arg;
139 
140 #ifdef DDB
141 	Debugger(device_get_nameunit(dev));
142 #else
143 	device_printf(dev, "close, but no debugger\n");
144 #endif
145 }
146