gpio_opb.c revision 1.1.10.2 1 1.1.10.2 kent /* $NetBSD: gpio_opb.c,v 1.1.10.2 2005/04/29 11:28:20 kent Exp $ */
2 1.1.10.2 kent
3 1.1.10.2 kent /*
4 1.1.10.2 kent * Copyright (c) 2004 Shigeyuki Fukushima.
5 1.1.10.2 kent * All rights reserved.
6 1.1.10.2 kent *
7 1.1.10.2 kent * Redistribution and use in source and binary forms, with or without
8 1.1.10.2 kent * modification, are permitted provided that the following conditions
9 1.1.10.2 kent * are met:
10 1.1.10.2 kent * 1. Redistributions of source code must retain the above copyright
11 1.1.10.2 kent * notice, this list of conditions and the following disclaimer.
12 1.1.10.2 kent * 2. Redistributions in binary form must reproduce the above
13 1.1.10.2 kent * copyright notice, this list of conditions and the following
14 1.1.10.2 kent * disclaimer in the documentation and/or other materials provided
15 1.1.10.2 kent * with the distribution.
16 1.1.10.2 kent * 3. The name of the author may not be used to endorse or promote
17 1.1.10.2 kent * products derived from this software without specific prior
18 1.1.10.2 kent * written permission.
19 1.1.10.2 kent *
20 1.1.10.2 kent * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
21 1.1.10.2 kent * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 1.1.10.2 kent * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1.10.2 kent * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
24 1.1.10.2 kent * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1.10.2 kent * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 1.1.10.2 kent * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1.10.2 kent * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 1.1.10.2 kent * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 1.1.10.2 kent * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 1.1.10.2 kent * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1.10.2 kent */
32 1.1.10.2 kent
33 1.1.10.2 kent #include "locators.h"
34 1.1.10.2 kent
35 1.1.10.2 kent #include <sys/param.h>
36 1.1.10.2 kent #include <sys/device.h>
37 1.1.10.2 kent #include <sys/systm.h>
38 1.1.10.2 kent
39 1.1.10.2 kent #include <machine/pio.h>
40 1.1.10.2 kent
41 1.1.10.2 kent #include <powerpc/ibm4xx/dcr405gp.h>
42 1.1.10.2 kent #include <powerpc/ibm4xx/dev/opbvar.h>
43 1.1.10.2 kent #include <powerpc/ibm4xx/dev/gpioreg.h>
44 1.1.10.2 kent #include <powerpc/ibm4xx/dev/gpiovar.h>
45 1.1.10.2 kent
46 1.1.10.2 kent struct gpio_softc {
47 1.1.10.2 kent struct device sc_dev; /* device generic */
48 1.1.10.2 kent struct gpio_controller sc_gpio; /* GPIO controller */
49 1.1.10.2 kent u_long sc_addr; /* GPIO controller address */
50 1.1.10.2 kent };
51 1.1.10.2 kent
52 1.1.10.2 kent static int gpio_print(void *, const char *);
53 1.1.10.2 kent static int gpio_search(struct device *, struct cfdata *,
54 1.1.10.2 kent const locdesc_t *ldesc, void *aux);
55 1.1.10.2 kent static int gpio_match(struct device *, struct cfdata *, void *);
56 1.1.10.2 kent static void gpio_attach(struct device *, struct device *, void *);
57 1.1.10.2 kent
58 1.1.10.2 kent static int gpio_or_read(void *, int);
59 1.1.10.2 kent static int gpio_tcr_read(void *, int);
60 1.1.10.2 kent static int gpio_odr_read(void *, int);
61 1.1.10.2 kent static int gpio_ir_read(void *, int);
62 1.1.10.2 kent static void gpio_or_write(void *arg, int addr, int bit);
63 1.1.10.2 kent static void gpio_tcr_write(void *arg, int addr, int bit);
64 1.1.10.2 kent static void gpio_odr_write(void *arg, int addr, int bit);
65 1.1.10.2 kent
66 1.1.10.2 kent static int gpio_read_bit(void *, int, int);
67 1.1.10.2 kent static void gpio_write_bit(void *, int, int, int);
68 1.1.10.2 kent static uint32_t gpio_read(void *, int);
69 1.1.10.2 kent static void gpio_write(void *, int, uint32_t);
70 1.1.10.2 kent
71 1.1.10.2 kent CFATTACH_DECL(gpio, sizeof(struct gpio_softc),
72 1.1.10.2 kent gpio_match, gpio_attach, NULL, NULL);
73 1.1.10.2 kent
74 1.1.10.2 kent static int
75 1.1.10.2 kent gpio_print(void *aux, const char *pnp)
76 1.1.10.2 kent {
77 1.1.10.2 kent struct gpio_attach_args *gaa = aux;
78 1.1.10.2 kent
79 1.1.10.2 kent aprint_normal(" addr GPIO#%d", gaa->ga_addr);
80 1.1.10.2 kent
81 1.1.10.2 kent return (UNCONF);
82 1.1.10.2 kent }
83 1.1.10.2 kent
84 1.1.10.2 kent static int
85 1.1.10.2 kent gpio_search(struct device *parent, struct cfdata *cf,
86 1.1.10.2 kent const locdesc_t *ldesc, void *aux)
87 1.1.10.2 kent {
88 1.1.10.2 kent struct gpio_softc *sc = (void *)parent;
89 1.1.10.2 kent struct gpio_attach_args gaa;
90 1.1.10.2 kent
91 1.1.10.2 kent gaa.ga_tag = &sc->sc_gpio;
92 1.1.10.2 kent gaa.ga_addr = cf->cf_loc[GPIOCF_ADDR];
93 1.1.10.2 kent
94 1.1.10.2 kent if (config_match(parent, cf, &gaa) > 0)
95 1.1.10.2 kent config_attach(parent, cf, &gaa, gpio_print);
96 1.1.10.2 kent
97 1.1.10.2 kent return (0);
98 1.1.10.2 kent }
99 1.1.10.2 kent
100 1.1.10.2 kent static int
101 1.1.10.2 kent gpio_match(struct device *parent, struct cfdata *cf, void *args)
102 1.1.10.2 kent {
103 1.1.10.2 kent struct opb_attach_args *oaa = args;
104 1.1.10.2 kent
105 1.1.10.2 kent if (strcmp(oaa->opb_name, cf->cf_name) != 0)
106 1.1.10.2 kent return (0);
107 1.1.10.2 kent
108 1.1.10.2 kent return (1);
109 1.1.10.2 kent }
110 1.1.10.2 kent
111 1.1.10.2 kent static void
112 1.1.10.2 kent gpio_attach(struct device *parent, struct device *self, void *aux)
113 1.1.10.2 kent {
114 1.1.10.2 kent struct gpio_softc *sc = (struct gpio_softc *)self;
115 1.1.10.2 kent struct opb_attach_args *oaa = aux;
116 1.1.10.2 kent
117 1.1.10.2 kent aprint_naive(": GPIO controller\n");
118 1.1.10.2 kent aprint_normal(": On-Chip GPIO controller\n");
119 1.1.10.2 kent
120 1.1.10.2 kent sc->sc_addr = oaa->opb_addr;
121 1.1.10.2 kent sc->sc_gpio.cookie = sc;
122 1.1.10.2 kent sc->sc_gpio.io_or_read = gpio_or_read;
123 1.1.10.2 kent sc->sc_gpio.io_tcr_read = gpio_tcr_read;
124 1.1.10.2 kent sc->sc_gpio.io_odr_read = gpio_odr_read;
125 1.1.10.2 kent sc->sc_gpio.io_ir_read = gpio_ir_read;
126 1.1.10.2 kent sc->sc_gpio.io_or_write = gpio_or_write;
127 1.1.10.2 kent sc->sc_gpio.io_tcr_write = gpio_tcr_write;
128 1.1.10.2 kent sc->sc_gpio.io_odr_write = gpio_odr_write;
129 1.1.10.2 kent
130 1.1.10.2 kent (void) config_search_ia(gpio_search, self, "gpio", NULL);
131 1.1.10.2 kent }
132 1.1.10.2 kent
133 1.1.10.2 kent static int
134 1.1.10.2 kent gpio_or_read(void *arg, int addr)
135 1.1.10.2 kent {
136 1.1.10.2 kent return (gpio_read_bit(arg, GPIO_OR, addr));
137 1.1.10.2 kent }
138 1.1.10.2 kent
139 1.1.10.2 kent static int
140 1.1.10.2 kent gpio_tcr_read(void *arg, int addr)
141 1.1.10.2 kent {
142 1.1.10.2 kent return (gpio_read_bit(arg, GPIO_TCR, addr));
143 1.1.10.2 kent }
144 1.1.10.2 kent
145 1.1.10.2 kent static int
146 1.1.10.2 kent gpio_odr_read(void *arg, int addr)
147 1.1.10.2 kent {
148 1.1.10.2 kent return (gpio_read_bit(arg, GPIO_ODR, addr));
149 1.1.10.2 kent }
150 1.1.10.2 kent
151 1.1.10.2 kent static int
152 1.1.10.2 kent gpio_ir_read(void *arg, int addr)
153 1.1.10.2 kent {
154 1.1.10.2 kent gpio_write_bit(arg, GPIO_ODR, addr, 0);
155 1.1.10.2 kent gpio_write_bit(arg, GPIO_TCR, addr, 0);
156 1.1.10.2 kent return (gpio_read_bit(arg, GPIO_ODR, addr));
157 1.1.10.2 kent }
158 1.1.10.2 kent
159 1.1.10.2 kent static void
160 1.1.10.2 kent gpio_or_write(void *arg, int addr, int bit)
161 1.1.10.2 kent {
162 1.1.10.2 kent gpio_write_bit(arg, GPIO_ODR, addr, 0);
163 1.1.10.2 kent gpio_write_bit(arg, GPIO_TCR, addr, 1);
164 1.1.10.2 kent gpio_write_bit(arg, GPIO_OR, addr, bit);
165 1.1.10.2 kent }
166 1.1.10.2 kent
167 1.1.10.2 kent static void
168 1.1.10.2 kent gpio_tcr_write(void *arg, int addr, int bit)
169 1.1.10.2 kent {
170 1.1.10.2 kent gpio_write_bit(arg, GPIO_TCR, addr, bit);
171 1.1.10.2 kent }
172 1.1.10.2 kent
173 1.1.10.2 kent static void
174 1.1.10.2 kent gpio_odr_write(void *arg, int addr, int bit)
175 1.1.10.2 kent {
176 1.1.10.2 kent gpio_write_bit(arg, GPIO_ODR, addr, bit);
177 1.1.10.2 kent }
178 1.1.10.2 kent
179 1.1.10.2 kent static int
180 1.1.10.2 kent gpio_read_bit(void *arg, int offset, int addr)
181 1.1.10.2 kent {
182 1.1.10.2 kent uint32_t rv = gpio_read(arg, offset);
183 1.1.10.2 kent uint32_t mask = GPIO_SBIT(addr);
184 1.1.10.2 kent
185 1.1.10.2 kent return ((rv & mask) >> GPIO_SHIFT(addr));
186 1.1.10.2 kent }
187 1.1.10.2 kent
188 1.1.10.2 kent void
189 1.1.10.2 kent gpio_write_bit(void *arg, int offset, int addr, int bit)
190 1.1.10.2 kent {
191 1.1.10.2 kent uint32_t rv = gpio_read(arg, offset);
192 1.1.10.2 kent uint32_t mask = GPIO_SBIT(addr);
193 1.1.10.2 kent
194 1.1.10.2 kent rv = rv & ~mask;
195 1.1.10.2 kent rv = rv | ((bit << GPIO_SHIFT(addr)) & mask);
196 1.1.10.2 kent gpio_write(arg, offset, rv);
197 1.1.10.2 kent }
198 1.1.10.2 kent
199 1.1.10.2 kent static uint32_t
200 1.1.10.2 kent gpio_read(void *arg, int offset)
201 1.1.10.2 kent {
202 1.1.10.2 kent struct gpio_softc *sc = arg;
203 1.1.10.2 kent uint32_t rv;
204 1.1.10.2 kent
205 1.1.10.2 kent rv = inl(sc->sc_addr + offset);
206 1.1.10.2 kent
207 1.1.10.2 kent return rv;
208 1.1.10.2 kent }
209 1.1.10.2 kent
210 1.1.10.2 kent static void
211 1.1.10.2 kent gpio_write(void *arg, int offset, uint32_t out)
212 1.1.10.2 kent {
213 1.1.10.2 kent struct gpio_softc *sc = arg;
214 1.1.10.2 kent
215 1.1.10.2 kent outl((sc->sc_addr + offset), out);
216 1.1.10.2 kent }
217