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