rk_gpio.c revision 1.2.2.2 1 1.2.2.2 pgoyette /* $NetBSD: rk_gpio.c,v 1.2.2.2 2018/06/25 07:25:39 pgoyette Exp $ */
2 1.2.2.2 pgoyette
3 1.2.2.2 pgoyette /*-
4 1.2.2.2 pgoyette * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
5 1.2.2.2 pgoyette * All rights reserved.
6 1.2.2.2 pgoyette *
7 1.2.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
8 1.2.2.2 pgoyette * modification, are permitted provided that the following conditions
9 1.2.2.2 pgoyette * are met:
10 1.2.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
11 1.2.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
12 1.2.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
14 1.2.2.2 pgoyette * documentation and/or other materials provided with the distribution.
15 1.2.2.2 pgoyette *
16 1.2.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.2.2.2 pgoyette * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.2.2.2 pgoyette * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.2.2.2 pgoyette * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.2.2.2 pgoyette * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.2.2.2 pgoyette * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.2.2.2 pgoyette * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.2.2.2 pgoyette * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.2.2.2 pgoyette * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.2.2.2 pgoyette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.2.2.2 pgoyette * SUCH DAMAGE.
27 1.2.2.2 pgoyette */
28 1.2.2.2 pgoyette
29 1.2.2.2 pgoyette #include <sys/cdefs.h>
30 1.2.2.2 pgoyette __KERNEL_RCSID(0, "$NetBSD: rk_gpio.c,v 1.2.2.2 2018/06/25 07:25:39 pgoyette Exp $");
31 1.2.2.2 pgoyette
32 1.2.2.2 pgoyette #include <sys/param.h>
33 1.2.2.2 pgoyette #include <sys/bus.h>
34 1.2.2.2 pgoyette #include <sys/device.h>
35 1.2.2.2 pgoyette #include <sys/intr.h>
36 1.2.2.2 pgoyette #include <sys/systm.h>
37 1.2.2.2 pgoyette #include <sys/mutex.h>
38 1.2.2.2 pgoyette #include <sys/kmem.h>
39 1.2.2.2 pgoyette #include <sys/gpio.h>
40 1.2.2.2 pgoyette #include <sys/bitops.h>
41 1.2.2.2 pgoyette #include <sys/lwp.h>
42 1.2.2.2 pgoyette
43 1.2.2.2 pgoyette #include <dev/fdt/fdtvar.h>
44 1.2.2.2 pgoyette #include <dev/gpio/gpiovar.h>
45 1.2.2.2 pgoyette
46 1.2.2.2 pgoyette #define GPIO_SWPORTA_DR_REG 0x0000
47 1.2.2.2 pgoyette #define GPIO_SWPORTA_DDR_REG 0x0004
48 1.2.2.2 pgoyette #define GPIO_INTEN_REG 0x0030
49 1.2.2.2 pgoyette #define GPIO_INTMASK_REG 0x0034
50 1.2.2.2 pgoyette #define GPIO_INTTYPE_LEVEL_REG 0x0038
51 1.2.2.2 pgoyette #define GPIO_INT_POLARITY_REG 0x003c
52 1.2.2.2 pgoyette #define GPIO_INT_STATUS_REG 0x0040
53 1.2.2.2 pgoyette #define GPIO_INT_RAWSTATUS_REG 0x0044
54 1.2.2.2 pgoyette #define GPIO_DEBOUNCE_REG 0x0048
55 1.2.2.2 pgoyette #define GPIO_PORTA_EOI_REG 0x004c
56 1.2.2.2 pgoyette #define GPIO_EXT_PORTA_REG 0x0050
57 1.2.2.2 pgoyette #define GPIO_LS_SYNC_REG 0x0060
58 1.2.2.2 pgoyette
59 1.2.2.2 pgoyette static const char * const compatible[] = {
60 1.2.2.2 pgoyette "rockchip,gpio-bank",
61 1.2.2.2 pgoyette NULL
62 1.2.2.2 pgoyette };
63 1.2.2.2 pgoyette
64 1.2.2.2 pgoyette struct rk_gpio_softc {
65 1.2.2.2 pgoyette device_t sc_dev;
66 1.2.2.2 pgoyette bus_space_tag_t sc_bst;
67 1.2.2.2 pgoyette bus_space_handle_t sc_bsh;
68 1.2.2.2 pgoyette kmutex_t sc_lock;
69 1.2.2.2 pgoyette
70 1.2.2.2 pgoyette struct gpio_chipset_tag sc_gp;
71 1.2.2.2 pgoyette gpio_pin_t sc_pins[32];
72 1.2.2.2 pgoyette device_t sc_gpiodev;
73 1.2.2.2 pgoyette };
74 1.2.2.2 pgoyette
75 1.2.2.2 pgoyette struct rk_gpio_pin {
76 1.2.2.2 pgoyette struct rk_gpio_softc *pin_sc;
77 1.2.2.2 pgoyette u_int pin_nr;
78 1.2.2.2 pgoyette int pin_flags;
79 1.2.2.2 pgoyette bool pin_actlo;
80 1.2.2.2 pgoyette };
81 1.2.2.2 pgoyette
82 1.2.2.2 pgoyette #define RD4(sc, reg) \
83 1.2.2.2 pgoyette bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
84 1.2.2.2 pgoyette #define WR4(sc, reg, val) \
85 1.2.2.2 pgoyette bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
86 1.2.2.2 pgoyette
87 1.2.2.2 pgoyette static int rk_gpio_match(device_t, cfdata_t, void *);
88 1.2.2.2 pgoyette static void rk_gpio_attach(device_t, device_t, void *);
89 1.2.2.2 pgoyette
90 1.2.2.2 pgoyette CFATTACH_DECL_NEW(rk_gpio, sizeof(struct rk_gpio_softc),
91 1.2.2.2 pgoyette rk_gpio_match, rk_gpio_attach, NULL, NULL);
92 1.2.2.2 pgoyette
93 1.2.2.2 pgoyette static int
94 1.2.2.2 pgoyette rk_gpio_ctl(struct rk_gpio_softc *sc, u_int pin, int flags)
95 1.2.2.2 pgoyette {
96 1.2.2.2 pgoyette uint32_t ddr;
97 1.2.2.2 pgoyette
98 1.2.2.2 pgoyette KASSERT(mutex_owned(&sc->sc_lock));
99 1.2.2.2 pgoyette
100 1.2.2.2 pgoyette ddr = RD4(sc, GPIO_SWPORTA_DDR_REG);
101 1.2.2.2 pgoyette if (flags & GPIO_PIN_INPUT)
102 1.2.2.2 pgoyette ddr &= ~__BIT(pin);
103 1.2.2.2 pgoyette else if (flags & GPIO_PIN_OUTPUT)
104 1.2.2.2 pgoyette ddr |= __BIT(pin);
105 1.2.2.2 pgoyette WR4(sc, GPIO_SWPORTA_DDR_REG, ddr);
106 1.2.2.2 pgoyette
107 1.2.2.2 pgoyette return 0;
108 1.2.2.2 pgoyette }
109 1.2.2.2 pgoyette
110 1.2.2.2 pgoyette static void *
111 1.2.2.2 pgoyette rk_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
112 1.2.2.2 pgoyette {
113 1.2.2.2 pgoyette struct rk_gpio_softc * const sc = device_private(dev);
114 1.2.2.2 pgoyette struct rk_gpio_pin *gpin;
115 1.2.2.2 pgoyette const u_int *gpio = data;
116 1.2.2.2 pgoyette int error;
117 1.2.2.2 pgoyette
118 1.2.2.2 pgoyette if (len != 12)
119 1.2.2.2 pgoyette return NULL;
120 1.2.2.2 pgoyette
121 1.2.2.2 pgoyette const uint8_t pin = be32toh(gpio[1]) & 0xff;
122 1.2.2.2 pgoyette const bool actlo = be32toh(gpio[2]) & 1;
123 1.2.2.2 pgoyette
124 1.2.2.2 pgoyette if (pin >= __arraycount(sc->sc_pins))
125 1.2.2.2 pgoyette return NULL;
126 1.2.2.2 pgoyette
127 1.2.2.2 pgoyette mutex_enter(&sc->sc_lock);
128 1.2.2.2 pgoyette error = rk_gpio_ctl(sc, pin, flags);
129 1.2.2.2 pgoyette mutex_exit(&sc->sc_lock);
130 1.2.2.2 pgoyette
131 1.2.2.2 pgoyette if (error != 0)
132 1.2.2.2 pgoyette return NULL;
133 1.2.2.2 pgoyette
134 1.2.2.2 pgoyette gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
135 1.2.2.2 pgoyette gpin->pin_sc = sc;
136 1.2.2.2 pgoyette gpin->pin_nr = pin;
137 1.2.2.2 pgoyette gpin->pin_flags = flags;
138 1.2.2.2 pgoyette gpin->pin_actlo = actlo;
139 1.2.2.2 pgoyette
140 1.2.2.2 pgoyette return gpin;
141 1.2.2.2 pgoyette }
142 1.2.2.2 pgoyette
143 1.2.2.2 pgoyette static void
144 1.2.2.2 pgoyette rk_gpio_release(device_t dev, void *priv)
145 1.2.2.2 pgoyette {
146 1.2.2.2 pgoyette struct rk_gpio_softc * const sc = device_private(dev);
147 1.2.2.2 pgoyette struct rk_gpio_pin *pin = priv;
148 1.2.2.2 pgoyette
149 1.2.2.2 pgoyette mutex_enter(&sc->sc_lock);
150 1.2.2.2 pgoyette rk_gpio_ctl(pin->pin_sc, pin->pin_nr, GPIO_PIN_INPUT);
151 1.2.2.2 pgoyette mutex_exit(&sc->sc_lock);
152 1.2.2.2 pgoyette
153 1.2.2.2 pgoyette kmem_free(pin, sizeof(*pin));
154 1.2.2.2 pgoyette }
155 1.2.2.2 pgoyette
156 1.2.2.2 pgoyette static int
157 1.2.2.2 pgoyette rk_gpio_read(device_t dev, void *priv, bool raw)
158 1.2.2.2 pgoyette {
159 1.2.2.2 pgoyette struct rk_gpio_softc * const sc = device_private(dev);
160 1.2.2.2 pgoyette struct rk_gpio_pin *pin = priv;
161 1.2.2.2 pgoyette uint32_t data;
162 1.2.2.2 pgoyette int val;
163 1.2.2.2 pgoyette
164 1.2.2.2 pgoyette KASSERT(sc == pin->pin_sc);
165 1.2.2.2 pgoyette
166 1.2.2.2 pgoyette const uint32_t data_mask = __BIT(pin->pin_nr);
167 1.2.2.2 pgoyette
168 1.2.2.2 pgoyette /* No lock required for reads */
169 1.2.2.2 pgoyette data = RD4(sc, GPIO_EXT_PORTA_REG);
170 1.2.2.2 pgoyette val = __SHIFTOUT(data, data_mask);
171 1.2.2.2 pgoyette if (!raw && pin->pin_actlo)
172 1.2.2.2 pgoyette val = !val;
173 1.2.2.2 pgoyette
174 1.2.2.2 pgoyette return val;
175 1.2.2.2 pgoyette }
176 1.2.2.2 pgoyette
177 1.2.2.2 pgoyette static void
178 1.2.2.2 pgoyette rk_gpio_write(device_t dev, void *priv, int val, bool raw)
179 1.2.2.2 pgoyette {
180 1.2.2.2 pgoyette struct rk_gpio_softc * const sc = device_private(dev);
181 1.2.2.2 pgoyette struct rk_gpio_pin *pin = priv;
182 1.2.2.2 pgoyette uint32_t data;
183 1.2.2.2 pgoyette
184 1.2.2.2 pgoyette KASSERT(sc == pin->pin_sc);
185 1.2.2.2 pgoyette
186 1.2.2.2 pgoyette const uint32_t data_mask = __BIT(pin->pin_nr);
187 1.2.2.2 pgoyette
188 1.2.2.2 pgoyette if (!raw && pin->pin_actlo)
189 1.2.2.2 pgoyette val = !val;
190 1.2.2.2 pgoyette
191 1.2.2.2 pgoyette mutex_enter(&sc->sc_lock);
192 1.2.2.2 pgoyette data = RD4(sc, GPIO_SWPORTA_DR_REG);
193 1.2.2.2 pgoyette if (val)
194 1.2.2.2 pgoyette data |= data_mask;
195 1.2.2.2 pgoyette else
196 1.2.2.2 pgoyette data &= ~data_mask;
197 1.2.2.2 pgoyette WR4(sc, GPIO_SWPORTA_DR_REG, data);
198 1.2.2.2 pgoyette mutex_exit(&sc->sc_lock);
199 1.2.2.2 pgoyette }
200 1.2.2.2 pgoyette
201 1.2.2.2 pgoyette static struct fdtbus_gpio_controller_func rk_gpio_funcs = {
202 1.2.2.2 pgoyette .acquire = rk_gpio_acquire,
203 1.2.2.2 pgoyette .release = rk_gpio_release,
204 1.2.2.2 pgoyette .read = rk_gpio_read,
205 1.2.2.2 pgoyette .write = rk_gpio_write,
206 1.2.2.2 pgoyette };
207 1.2.2.2 pgoyette
208 1.2.2.2 pgoyette static int
209 1.2.2.2 pgoyette rk_gpio_pin_read(void *priv, int pin)
210 1.2.2.2 pgoyette {
211 1.2.2.2 pgoyette struct rk_gpio_softc * const sc = priv;
212 1.2.2.2 pgoyette uint32_t data;
213 1.2.2.2 pgoyette int val;
214 1.2.2.2 pgoyette
215 1.2.2.2 pgoyette KASSERT(pin < __arraycount(sc->sc_pins));
216 1.2.2.2 pgoyette
217 1.2.2.2 pgoyette const uint32_t data_mask = __BIT(pin);
218 1.2.2.2 pgoyette
219 1.2.2.2 pgoyette /* No lock required for reads */
220 1.2.2.2 pgoyette data = RD4(sc, GPIO_EXT_PORTA_REG);
221 1.2.2.2 pgoyette val = __SHIFTOUT(data, data_mask);
222 1.2.2.2 pgoyette
223 1.2.2.2 pgoyette return val;
224 1.2.2.2 pgoyette }
225 1.2.2.2 pgoyette
226 1.2.2.2 pgoyette static void
227 1.2.2.2 pgoyette rk_gpio_pin_write(void *priv, int pin, int val)
228 1.2.2.2 pgoyette {
229 1.2.2.2 pgoyette struct rk_gpio_softc * const sc = priv;
230 1.2.2.2 pgoyette uint32_t data;
231 1.2.2.2 pgoyette
232 1.2.2.2 pgoyette KASSERT(pin < __arraycount(sc->sc_pins));
233 1.2.2.2 pgoyette
234 1.2.2.2 pgoyette const uint32_t data_mask = __BIT(pin);
235 1.2.2.2 pgoyette
236 1.2.2.2 pgoyette mutex_enter(&sc->sc_lock);
237 1.2.2.2 pgoyette data = RD4(sc, GPIO_SWPORTA_DR_REG);
238 1.2.2.2 pgoyette if (val)
239 1.2.2.2 pgoyette data |= data_mask;
240 1.2.2.2 pgoyette else
241 1.2.2.2 pgoyette data &= ~data_mask;
242 1.2.2.2 pgoyette WR4(sc, GPIO_SWPORTA_DR_REG, data);
243 1.2.2.2 pgoyette mutex_exit(&sc->sc_lock);
244 1.2.2.2 pgoyette }
245 1.2.2.2 pgoyette
246 1.2.2.2 pgoyette static void
247 1.2.2.2 pgoyette rk_gpio_pin_ctl(void *priv, int pin, int flags)
248 1.2.2.2 pgoyette {
249 1.2.2.2 pgoyette struct rk_gpio_softc * const sc = priv;
250 1.2.2.2 pgoyette
251 1.2.2.2 pgoyette KASSERT(pin < __arraycount(sc->sc_pins));
252 1.2.2.2 pgoyette
253 1.2.2.2 pgoyette mutex_enter(&sc->sc_lock);
254 1.2.2.2 pgoyette rk_gpio_ctl(sc, pin, flags);
255 1.2.2.2 pgoyette mutex_exit(&sc->sc_lock);
256 1.2.2.2 pgoyette }
257 1.2.2.2 pgoyette
258 1.2.2.2 pgoyette static void
259 1.2.2.2 pgoyette rk_gpio_attach_ports(struct rk_gpio_softc *sc)
260 1.2.2.2 pgoyette {
261 1.2.2.2 pgoyette struct gpio_chipset_tag *gp = &sc->sc_gp;
262 1.2.2.2 pgoyette struct gpiobus_attach_args gba;
263 1.2.2.2 pgoyette u_int pin;
264 1.2.2.2 pgoyette
265 1.2.2.2 pgoyette gp->gp_cookie = sc;
266 1.2.2.2 pgoyette gp->gp_pin_read = rk_gpio_pin_read;
267 1.2.2.2 pgoyette gp->gp_pin_write = rk_gpio_pin_write;
268 1.2.2.2 pgoyette gp->gp_pin_ctl = rk_gpio_pin_ctl;
269 1.2.2.2 pgoyette
270 1.2.2.2 pgoyette for (pin = 0; pin < __arraycount(sc->sc_pins); pin++) {
271 1.2.2.2 pgoyette sc->sc_pins[pin].pin_num = pin;
272 1.2.2.2 pgoyette sc->sc_pins[pin].pin_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
273 1.2.2.2 pgoyette sc->sc_pins[pin].pin_state = rk_gpio_pin_read(sc, pin);
274 1.2.2.2 pgoyette }
275 1.2.2.2 pgoyette
276 1.2.2.2 pgoyette memset(&gba, 0, sizeof(gba));
277 1.2.2.2 pgoyette gba.gba_gc = gp;
278 1.2.2.2 pgoyette gba.gba_pins = sc->sc_pins;
279 1.2.2.2 pgoyette gba.gba_npins = __arraycount(sc->sc_pins);
280 1.2.2.2 pgoyette sc->sc_gpiodev = config_found_ia(sc->sc_dev, "gpiobus", &gba, NULL);
281 1.2.2.2 pgoyette }
282 1.2.2.2 pgoyette
283 1.2.2.2 pgoyette static int
284 1.2.2.2 pgoyette rk_gpio_match(device_t parent, cfdata_t cf, void *aux)
285 1.2.2.2 pgoyette {
286 1.2.2.2 pgoyette struct fdt_attach_args * const faa = aux;
287 1.2.2.2 pgoyette
288 1.2.2.2 pgoyette return of_match_compatible(faa->faa_phandle, compatible);
289 1.2.2.2 pgoyette }
290 1.2.2.2 pgoyette
291 1.2.2.2 pgoyette static void
292 1.2.2.2 pgoyette rk_gpio_attach(device_t parent, device_t self, void *aux)
293 1.2.2.2 pgoyette {
294 1.2.2.2 pgoyette struct rk_gpio_softc * const sc = device_private(self);
295 1.2.2.2 pgoyette struct fdt_attach_args * const faa = aux;
296 1.2.2.2 pgoyette const int phandle = faa->faa_phandle;
297 1.2.2.2 pgoyette struct clk *clk;
298 1.2.2.2 pgoyette bus_addr_t addr;
299 1.2.2.2 pgoyette bus_size_t size;
300 1.2.2.2 pgoyette
301 1.2.2.2 pgoyette if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
302 1.2.2.2 pgoyette aprint_error(": couldn't get registers\n");
303 1.2.2.2 pgoyette return;
304 1.2.2.2 pgoyette }
305 1.2.2.2 pgoyette
306 1.2.2.2 pgoyette if ((clk = fdtbus_clock_get_index(phandle, 0)) == NULL || clk_enable(clk) != 0) {
307 1.2.2.2 pgoyette aprint_error(": couldn't enable clock\n");
308 1.2.2.2 pgoyette return;
309 1.2.2.2 pgoyette }
310 1.2.2.2 pgoyette
311 1.2.2.2 pgoyette sc->sc_dev = self;
312 1.2.2.2 pgoyette sc->sc_bst = faa->faa_bst;
313 1.2.2.2 pgoyette if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
314 1.2.2.2 pgoyette aprint_error(": couldn't map registers\n");
315 1.2.2.2 pgoyette return;
316 1.2.2.2 pgoyette }
317 1.2.2.2 pgoyette mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
318 1.2.2.2 pgoyette
319 1.2.2.2 pgoyette aprint_naive("\n");
320 1.2.2.2 pgoyette aprint_normal(": GPIO (%s)\n", fdtbus_get_string(phandle, "name"));
321 1.2.2.2 pgoyette
322 1.2.2.2 pgoyette fdtbus_register_gpio_controller(self, phandle, &rk_gpio_funcs);
323 1.2.2.2 pgoyette
324 1.2.2.2 pgoyette rk_gpio_attach_ports(sc);
325 1.2.2.2 pgoyette }
326