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