No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / mips / adm5120 / dev / admgpio.c
blob8c93c0cc42f1d0a0d9fb67055c43b45497f7d856
1 /* $NetBSD: admgpio.c,v 1.1 2007/03/20 08:52:01 dyoung Exp $ */
3 /*-
4 * Copyright (c) 2007 David Young. All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
19 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
27 * OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: admgpio.c,v 1.1 2007/03/20 08:52:01 dyoung Exp $");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/device.h>
36 #include <sys/gpio.h>
37 #include <sys/kernel.h> /* for hz */
39 #include <machine/bus.h>
41 #include <dev/gpio/gpiovar.h>
43 #include <mips/adm5120/include/adm5120reg.h>
44 #include <mips/adm5120/include/adm5120var.h>
45 #include <mips/adm5120/include/adm5120_mainbusvar.h>
47 static inline uint32_t
48 admgpio_read(struct mainbus_softc *sc)
50 return bus_space_read_4(sc->sc_obiot, sc->sc_gpioh, ADM5120_GPIO0);
53 static inline void
54 admgpio_write(struct mainbus_softc *sc, uint32_t val)
56 bus_space_write_4(sc->sc_obiot, sc->sc_gpioh, ADM5120_GPIO0, val);
59 static void
60 admgpio_pin_ctl(void *cookie, int pin, int flags)
62 struct mainbus_softc *sc = cookie;
63 uint32_t gpio0, mask;
65 KASSERT(flags == GPIO_PIN_INPUT || flags == GPIO_PIN_OUTPUT);
67 mask = __SHIFTIN(1 << pin, ADM5120_GPIO0_OE);
68 gpio0 = admgpio_read(sc);
70 if (flags == GPIO_PIN_OUTPUT)
71 admgpio_write(sc, gpio0 | mask);
72 else
73 admgpio_write(sc, gpio0 & ~mask);
76 static int
77 admgpio_pin_read(void *cookie, int pin)
79 struct mainbus_softc *sc = cookie;
80 uint32_t gpio0, mask;
82 KASSERT(pin >= 0 && pin < 8);
84 if (sc->sc_pins[pin].pin_flags == GPIO_PIN_INPUT)
85 mask = __SHIFTIN(1 << pin, ADM5120_GPIO0_IV);
86 else
87 mask = __SHIFTIN(1 << pin, ADM5120_GPIO0_OV);
89 gpio0 = admgpio_read(sc);
91 return ((gpio0 & mask) != 0) ? GPIO_PIN_HIGH : GPIO_PIN_LOW;
94 static void
95 admgpio_pin_write(void *cookie, int pin, int value)
97 struct mainbus_softc *sc = cookie;
98 uint32_t gpio0, mask;
100 KASSERT(pin >= 0 && pin < 8);
102 mask = __SHIFTIN(1 << pin, ADM5120_GPIO0_OV);
103 gpio0 = admgpio_read(sc);
105 admgpio_write(sc,
106 (value == GPIO_PIN_HIGH) ? (gpio0 | mask) : (gpio0 & ~mask));
109 device_t
110 admgpio_attach(struct mainbus_softc *sc)
112 int pin;
113 uint32_t oe;
114 struct gpiobus_attach_args gba;
116 oe = __SHIFTOUT(admgpio_read(sc), ADM5120_GPIO0_OE);
118 for (pin = 0; pin < __arraycount(sc->sc_pins); pin++) {
119 sc->sc_pins[pin].pin_num = pin;
120 sc->sc_pins[pin].pin_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
122 if ((oe & (1 << pin)) != 0)
123 sc->sc_pins[pin].pin_flags = GPIO_PIN_OUTPUT;
124 else
125 sc->sc_pins[pin].pin_flags = GPIO_PIN_INPUT;
127 sc->sc_pins[pin].pin_state = admgpio_pin_read(sc, pin);
130 sc->sc_gp.gp_cookie = sc;
131 sc->sc_gp.gp_pin_read = admgpio_pin_read;
132 sc->sc_gp.gp_pin_write = admgpio_pin_write;
133 sc->sc_gp.gp_pin_ctl = admgpio_pin_ctl;
135 gba.gba_gc = &sc->sc_gp;
136 gba.gba_pins = &sc->sc_pins[0];
137 gba.gba_npins = __arraycount(sc->sc_pins);
139 /* Attach GPIO framework */
140 return config_found_ia(&sc->sc_dev, "gpiobus", &gba, gpiobus_print);