1 /* $OpenBSD: isagpio.c,v 1.5 2022/04/06 18:59:28 naddy Exp $ */
2
3 /*
4 * Copyright (c) 2006 Oleg Safiullin <form@pdp-11.org.ru>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
12 * disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/device.h>
34 #include <sys/gpio.h>
35
36 #include <machine/bus.h>
37 #include <machine/intr.h>
38
39 #include <dev/isa/isavar.h>
40
41 #include <dev/gpio/gpiovar.h>
42
43 #define ISAGPIO_IOSIZE 1
44 #define ISAGPIO_NPINS 8
45
46 struct isagpio_softc {
47 struct device sc_dev;
48
49 bus_space_tag_t sc_iot;
50 bus_space_handle_t sc_ioh;
51
52 struct gpio_chipset_tag sc_gpio_gc;
53 struct gpio_pin sc_gpio_pins[ISAGPIO_NPINS];
54 u_int8_t sc_gpio_mask;
55 };
56
57 int isagpio_match(struct device *, void *, void *);
58 void isagpio_attach(struct device *, struct device *, void *);
59 int isagpio_pin_read(void *, int);
60 void isagpio_pin_write(void *, int, int);
61 void isagpio_pin_ctl(void *, int, int);
62
63 const struct cfattach isagpio_ca = {
64 sizeof(struct isagpio_softc), isagpio_match, isagpio_attach
65 };
66
67 struct cfdriver isagpio_cd = {
68 NULL, "isagpio", DV_DULL
69 };
70
71 int
isagpio_match(struct device * parent,void * match,void * aux)72 isagpio_match(struct device *parent, void *match, void *aux)
73 {
74 struct isa_attach_args *ia = aux;
75 bus_space_handle_t ioh;
76
77 if (ia->ia_iobase == -1 || bus_space_map(ia->ia_iot, ia->ia_iobase,
78 ISAGPIO_IOSIZE, 0, &ioh) != 0)
79 return (0);
80
81 bus_space_unmap(ia->ia_iot, ioh, ISAGPIO_IOSIZE);
82 ia->ia_iosize = ISAGPIO_IOSIZE;
83 ia->ipa_nio = 1;
84 ia->ipa_nmem = 0;
85 ia->ipa_nirq = 0;
86 ia->ipa_ndrq = 0;
87
88 return (1);
89 }
90
91 void
isagpio_attach(struct device * parent,struct device * self,void * aux)92 isagpio_attach(struct device *parent, struct device *self, void *aux)
93 {
94 struct isagpio_softc *sc = (void *)self;
95 struct isa_attach_args *ia = aux;
96 struct gpiobus_attach_args gba;
97 int i;
98
99 if (bus_space_map(ia->ia_iot, ia->ia_iobase, ia->ia_iosize, 0,
100 &sc->sc_ioh) != 0) {
101 printf(": can't map i/o space\n");
102 return;
103 }
104
105 printf("\n");
106
107 sc->sc_iot = ia->ia_iot;
108 sc->sc_gpio_mask = 0;
109
110 for (i = 0; i < ISAGPIO_NPINS; i++) {
111 sc->sc_gpio_pins[i].pin_num = i;
112 sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
113 sc->sc_gpio_pins[i].pin_state = GPIO_PIN_LOW;
114 }
115
116 sc->sc_gpio_gc.gp_cookie = sc;
117 sc->sc_gpio_gc.gp_pin_read = isagpio_pin_read;
118 sc->sc_gpio_gc.gp_pin_write = isagpio_pin_write;
119 sc->sc_gpio_gc.gp_pin_ctl = isagpio_pin_ctl;
120
121 gba.gba_name = "gpio";
122 gba.gba_gc = &sc->sc_gpio_gc;
123 gba.gba_pins = sc->sc_gpio_pins;
124 gba.gba_npins = ISAGPIO_NPINS;
125
126 (void)config_found(&sc->sc_dev, &gba, gpiobus_print);
127 }
128
129 int
isagpio_pin_read(void * arg,int pin)130 isagpio_pin_read(void *arg, int pin)
131 {
132 struct isagpio_softc *sc = arg;
133 u_int8_t mask;
134
135 mask = bus_space_read_1(sc->sc_iot, sc->sc_ioh, 0);
136 return ((mask >> pin) & 0x01);
137 }
138
139 void
isagpio_pin_write(void * arg,int pin,int value)140 isagpio_pin_write(void *arg, int pin, int value)
141 {
142 struct isagpio_softc *sc = arg;
143
144 if (value == GPIO_PIN_LOW)
145 sc->sc_gpio_mask &= ~(0x01 << pin);
146 else if (value == GPIO_PIN_HIGH)
147 sc->sc_gpio_mask |= 0x01 << pin;
148 bus_space_write_1(sc->sc_iot, sc->sc_ioh, 0, sc->sc_gpio_mask);
149 }
150
151 void
isagpio_pin_ctl(void * arg,int pin,int flags)152 isagpio_pin_ctl(void *arg, int pin, int flags)
153 {
154 }
155