gpio.c revision 1.3.8.2 1 1.3.8.2 nathanw /* $NetBSD: gpio.c,v 1.3.8.2 2002/10/18 02:38:34 nathanw Exp $ */
2 1.3.8.2 nathanw
3 1.3.8.2 nathanw /*-
4 1.3.8.2 nathanw * Copyright (C) 1998 Internet Research Institute, Inc.
5 1.3.8.2 nathanw * All rights reserved.
6 1.3.8.2 nathanw *
7 1.3.8.2 nathanw * Redistribution and use in source and binary forms, with or without
8 1.3.8.2 nathanw * modification, are permitted provided that the following conditions
9 1.3.8.2 nathanw * are met:
10 1.3.8.2 nathanw * 1. Redistributions of source code must retain the above copyright
11 1.3.8.2 nathanw * notice, this list of conditions and the following disclaimer.
12 1.3.8.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
13 1.3.8.2 nathanw * notice, this list of conditions and the following disclaimer in the
14 1.3.8.2 nathanw * documentation and/or other materials provided with the distribution.
15 1.3.8.2 nathanw * 3. All advertising materials mentioning features or use of this software
16 1.3.8.2 nathanw * must display the following acknowledgement:
17 1.3.8.2 nathanw * This product includes software developed by
18 1.3.8.2 nathanw * Internet Research Institute, Inc.
19 1.3.8.2 nathanw * 4. The name of the author may not be used to endorse or promote products
20 1.3.8.2 nathanw * derived from this software without specific prior written permission.
21 1.3.8.2 nathanw *
22 1.3.8.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.3.8.2 nathanw * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.3.8.2 nathanw * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.3.8.2 nathanw * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.3.8.2 nathanw * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.3.8.2 nathanw * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.3.8.2 nathanw * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.3.8.2 nathanw * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.3.8.2 nathanw * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 1.3.8.2 nathanw * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.3.8.2 nathanw */
33 1.3.8.2 nathanw
34 1.3.8.2 nathanw #include <sys/types.h>
35 1.3.8.2 nathanw #include <sys/param.h>
36 1.3.8.2 nathanw #include <sys/systm.h>
37 1.3.8.2 nathanw #include <sys/kernel.h>
38 1.3.8.2 nathanw #include <sys/device.h>
39 1.3.8.2 nathanw #include <sys/malloc.h>
40 1.3.8.2 nathanw #include <machine/autoconf.h>
41 1.3.8.2 nathanw #include <machine/pio.h>
42 1.3.8.2 nathanw
43 1.3.8.2 nathanw #include <dev/ofw/openfirm.h>
44 1.3.8.2 nathanw
45 1.3.8.2 nathanw #include "adb.h"
46 1.3.8.2 nathanw
47 1.3.8.2 nathanw static void gpio_obio_attach (struct device *, struct device *, void *);
48 1.3.8.2 nathanw static int gpio_obio_match (struct device *, struct cfdata *, void *);
49 1.3.8.2 nathanw static int gpio_obio_print (void *aux, const char *gpio);
50 1.3.8.2 nathanw
51 1.3.8.2 nathanw static void gpio_gpio_attach (struct device *, struct device *, void *);
52 1.3.8.2 nathanw static int gpio_gpio_match (struct device *, struct cfdata *, void *);
53 1.3.8.2 nathanw static int gpio_intr (void *);
54 1.3.8.2 nathanw
55 1.3.8.2 nathanw struct gpio_softc {
56 1.3.8.2 nathanw struct device sc_dev;
57 1.3.8.2 nathanw u_int8_t *sc_port;
58 1.3.8.2 nathanw };
59 1.3.8.2 nathanw
60 1.3.8.2 nathanw CFATTACH_DECL(gpio_obio, sizeof(struct gpio_softc),
61 1.3.8.2 nathanw gpio_obio_match, gpio_obio_attach, NULL, NULL);
62 1.3.8.2 nathanw
63 1.3.8.2 nathanw CFATTACH_DECL(gpio_gpio, sizeof(struct gpio_softc),
64 1.3.8.2 nathanw gpio_gpio_match, gpio_gpio_attach, NULL, NULL);
65 1.3.8.2 nathanw
66 1.3.8.2 nathanw extern struct cfdriver gpio_cd;
67 1.3.8.2 nathanw
68 1.3.8.2 nathanw int
69 1.3.8.2 nathanw gpio_obio_match(struct device *parent, struct cfdata *cf, void *aux)
70 1.3.8.2 nathanw {
71 1.3.8.2 nathanw struct confargs *ca = aux;
72 1.3.8.2 nathanw
73 1.3.8.2 nathanw if (strcmp(ca->ca_name, "gpio") != 0)
74 1.3.8.2 nathanw return 0;
75 1.3.8.2 nathanw
76 1.3.8.2 nathanw if (ca->ca_nreg == 0)
77 1.3.8.2 nathanw return 0;
78 1.3.8.2 nathanw
79 1.3.8.2 nathanw return 1;
80 1.3.8.2 nathanw }
81 1.3.8.2 nathanw
82 1.3.8.2 nathanw void
83 1.3.8.2 nathanw gpio_obio_attach(struct device *parent, struct device *self, void *aux)
84 1.3.8.2 nathanw {
85 1.3.8.2 nathanw struct gpio_softc *sc = (struct gpio_softc *)self;
86 1.3.8.2 nathanw struct confargs *ca = aux, ca2;
87 1.3.8.2 nathanw int child;
88 1.3.8.2 nathanw int namelen;
89 1.3.8.2 nathanw int intr[6];
90 1.3.8.2 nathanw u_int reg[20];
91 1.3.8.2 nathanw char name[32];
92 1.3.8.2 nathanw
93 1.3.8.2 nathanw printf("\n");
94 1.3.8.2 nathanw
95 1.3.8.2 nathanw sc->sc_port = mapiodev(ca->ca_baseaddr + ca->ca_reg[0], ca->ca_reg[1]);
96 1.3.8.2 nathanw
97 1.3.8.2 nathanw ca2.ca_baseaddr = ca->ca_baseaddr;
98 1.3.8.2 nathanw for (child = OF_child(ca->ca_node); child; child = OF_peer(child)) {
99 1.3.8.2 nathanw namelen = OF_getprop(child, "name", name, sizeof(name));
100 1.3.8.2 nathanw if (namelen < 0)
101 1.3.8.2 nathanw continue;
102 1.3.8.2 nathanw if (namelen >= sizeof(name))
103 1.3.8.2 nathanw continue;
104 1.3.8.2 nathanw
105 1.3.8.2 nathanw name[namelen] = 0;
106 1.3.8.2 nathanw ca2.ca_name = name;
107 1.3.8.2 nathanw ca2.ca_node = child;
108 1.3.8.2 nathanw
109 1.3.8.2 nathanw ca2.ca_nreg = OF_getprop(child, "reg", reg, sizeof(reg));
110 1.3.8.2 nathanw ca2.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
111 1.3.8.2 nathanw sizeof(intr));
112 1.3.8.2 nathanw if (ca2.ca_nintr == -1)
113 1.3.8.2 nathanw ca2.ca_nintr = OF_getprop(child, "interrupts", intr,
114 1.3.8.2 nathanw sizeof(intr));
115 1.3.8.2 nathanw
116 1.3.8.2 nathanw ca2.ca_reg = reg;
117 1.3.8.2 nathanw ca2.ca_intr = intr;
118 1.3.8.2 nathanw
119 1.3.8.2 nathanw config_found(self, &ca2, gpio_obio_print);
120 1.3.8.2 nathanw }
121 1.3.8.2 nathanw }
122 1.3.8.2 nathanw
123 1.3.8.2 nathanw int
124 1.3.8.2 nathanw gpio_obio_print(void *aux, const char *gpio)
125 1.3.8.2 nathanw {
126 1.3.8.2 nathanw struct confargs *ca = aux;
127 1.3.8.2 nathanw
128 1.3.8.2 nathanw if (gpio)
129 1.3.8.2 nathanw printf("%s at %s", ca->ca_name, gpio);
130 1.3.8.2 nathanw
131 1.3.8.2 nathanw if (ca->ca_nreg > 0)
132 1.3.8.2 nathanw printf(" offset 0x%x", ca->ca_reg[0]);
133 1.3.8.2 nathanw
134 1.3.8.2 nathanw return UNCONF;
135 1.3.8.2 nathanw }
136 1.3.8.2 nathanw
137 1.3.8.2 nathanw int
138 1.3.8.2 nathanw gpio_gpio_match(struct device *parent, struct cfdata *cf, void *aux)
139 1.3.8.2 nathanw {
140 1.3.8.2 nathanw struct confargs *ca = aux;
141 1.3.8.2 nathanw
142 1.3.8.2 nathanw if (strcmp(ca->ca_name, "extint-gpio1") != 0)
143 1.3.8.2 nathanw return 0;
144 1.3.8.2 nathanw
145 1.3.8.2 nathanw if (ca->ca_nintr < 0)
146 1.3.8.2 nathanw return 0;
147 1.3.8.2 nathanw
148 1.3.8.2 nathanw return 1;
149 1.3.8.2 nathanw }
150 1.3.8.2 nathanw
151 1.3.8.2 nathanw void
152 1.3.8.2 nathanw gpio_gpio_attach(struct device *parent, struct device *self, void *aux)
153 1.3.8.2 nathanw {
154 1.3.8.2 nathanw struct gpio_softc *sc = (struct gpio_softc *)self;
155 1.3.8.2 nathanw struct confargs *ca = aux;
156 1.3.8.2 nathanw
157 1.3.8.2 nathanw
158 1.3.8.2 nathanw sc->sc_port = ((struct gpio_softc *) parent)->sc_port;
159 1.3.8.2 nathanw intr_establish(ca->ca_intr[0], IST_LEVEL, IPL_HIGH, gpio_intr, sc);
160 1.3.8.2 nathanw
161 1.3.8.2 nathanw printf(" irq %d\n", ca->ca_intr[0]);
162 1.3.8.2 nathanw }
163 1.3.8.2 nathanw
164 1.3.8.2 nathanw #if NADB > 0
165 1.3.8.2 nathanw extern int adb_intr (void *);
166 1.3.8.2 nathanw extern struct cfdriver adb_cd;
167 1.3.8.2 nathanw #endif
168 1.3.8.2 nathanw
169 1.3.8.2 nathanw int
170 1.3.8.2 nathanw gpio_intr(void *arg)
171 1.3.8.2 nathanw {
172 1.3.8.2 nathanw int rv = 0;
173 1.3.8.2 nathanw
174 1.3.8.2 nathanw #if NADB > 0
175 1.3.8.2 nathanw if (adb_cd.cd_devs[0] != NULL)
176 1.3.8.2 nathanw rv = adb_intr(adb_cd.cd_devs[0]);
177 1.3.8.2 nathanw #endif
178 1.3.8.2 nathanw
179 1.3.8.2 nathanw return rv;
180 1.3.8.2 nathanw }
181