nsclpcsio_isa.c revision 1.8.4.4 1 1.8.4.4 yamt /* $NetBSD: nsclpcsio_isa.c,v 1.8.4.4 2007/09/03 14:35:42 yamt Exp $ */
2 1.1 drochner
3 1.1 drochner /*
4 1.1 drochner * Copyright (c) 2002
5 1.1 drochner * Matthias Drochner. All rights reserved.
6 1.1 drochner *
7 1.1 drochner * Redistribution and use in source and binary forms, with or without
8 1.1 drochner * modification, are permitted provided that the following conditions
9 1.1 drochner * are met:
10 1.1 drochner * 1. Redistributions of source code must retain the above copyright
11 1.1 drochner * notice, this list of conditions, and the following disclaimer.
12 1.1 drochner * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 drochner * notice, this list of conditions and the following disclaimer in the
14 1.1 drochner * documentation and/or other materials provided with the distribution.
15 1.1 drochner *
16 1.1 drochner * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 drochner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 drochner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 drochner * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 drochner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 drochner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 drochner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 drochner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 drochner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 drochner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 drochner * SUCH DAMAGE.
27 1.1 drochner */
28 1.1 drochner
29 1.1 drochner #include <sys/cdefs.h>
30 1.8.4.4 yamt __KERNEL_RCSID(0, "$NetBSD: nsclpcsio_isa.c,v 1.8.4.4 2007/09/03 14:35:42 yamt Exp $");
31 1.1 drochner
32 1.1 drochner #include <sys/param.h>
33 1.1 drochner #include <sys/systm.h>
34 1.1 drochner #include <sys/device.h>
35 1.8.4.3 yamt #include <sys/mutex.h>
36 1.8.4.1 yamt #include <sys/gpio.h>
37 1.1 drochner #include <machine/bus.h>
38 1.1 drochner
39 1.1 drochner #include <dev/isa/isareg.h>
40 1.1 drochner #include <dev/isa/isavar.h>
41 1.8.4.1 yamt #include "gpio.h"
42 1.8.4.1 yamt #if NGPIO > 0
43 1.8.4.1 yamt #include <dev/gpio/gpiovar.h>
44 1.8.4.1 yamt #endif
45 1.1 drochner #include <dev/sysmon/sysmonvar.h>
46 1.1 drochner
47 1.7 perry static int nsclpcsio_isa_match(struct device *, struct cfdata *, void *);
48 1.7 perry static void nsclpcsio_isa_attach(struct device *, struct device *, void *);
49 1.1 drochner
50 1.8.4.1 yamt #define GPIO_NPINS 29
51 1.8.4.1 yamt #define SIO_GPIO_CONF_OUTPUTEN (1 << 0)
52 1.8.4.1 yamt #define SIO_GPIO_CONF_PUSHPULL (1 << 1)
53 1.8.4.1 yamt #define SIO_GPIO_CONF_PULLUP (1 << 2)
54 1.8.4.1 yamt
55 1.1 drochner struct nsclpcsio_softc {
56 1.1 drochner struct device sc_dev;
57 1.8.4.1 yamt bus_space_tag_t sc_iot, sc_gpio_iot, sc_tms_iot;
58 1.8.4.1 yamt bus_space_handle_t sc_ioh, sc_gpio_ioh, sc_tms_ioh;
59 1.1 drochner
60 1.8.4.4 yamt envsys_data_t sc_data[3];
61 1.1 drochner struct sysmon_envsys sc_sysmon;
62 1.8.4.3 yamt kmutex_t sc_lock;
63 1.8.4.1 yamt
64 1.8.4.1 yamt #if NGPIO > 0
65 1.8.4.1 yamt /* GPIO */
66 1.8.4.1 yamt struct gpio_chipset_tag sc_gpio_gc;
67 1.8.4.1 yamt struct gpio_pin sc_gpio_pins[GPIO_NPINS];
68 1.8.4.1 yamt #endif
69 1.1 drochner };
70 1.1 drochner
71 1.8.4.1 yamt #define GPIO_READ(sc, reg) \
72 1.8.4.1 yamt bus_space_read_1((sc)->sc_gpio_iot, \
73 1.8.4.1 yamt (sc)->sc_gpio_ioh, (reg))
74 1.8.4.1 yamt #define GPIO_WRITE(sc, reg, val) \
75 1.8.4.1 yamt bus_space_write_1((sc)->sc_gpio_iot, \
76 1.8.4.1 yamt (sc)->sc_gpio_ioh, (reg), (val))
77 1.8.4.1 yamt
78 1.5 drochner CFATTACH_DECL(nsclpcsio_isa, sizeof(struct nsclpcsio_softc),
79 1.4 thorpej nsclpcsio_isa_match, nsclpcsio_isa_attach, NULL, NULL);
80 1.1 drochner
81 1.1 drochner static u_int8_t nsread(bus_space_tag_t, bus_space_handle_t, int);
82 1.1 drochner static void nswrite(bus_space_tag_t, bus_space_handle_t, int, u_int8_t);
83 1.1 drochner static int nscheck(bus_space_tag_t, int);
84 1.1 drochner
85 1.1 drochner static void tms_update(struct nsclpcsio_softc *, int);
86 1.8.4.4 yamt static int tms_gtredata(struct sysmon_envsys *, envsys_data_t *);
87 1.1 drochner
88 1.8.4.1 yamt #if NGPIO > 0
89 1.8.4.1 yamt static void nsclpcsio_gpio_init(struct nsclpcsio_softc *);
90 1.8.4.1 yamt static void nsclpcsio_gpio_pin_select(struct nsclpcsio_softc *, int);
91 1.8.4.1 yamt static void nsclpcsio_gpio_pin_write(void *, int, int);
92 1.8.4.1 yamt static int nsclpcsio_gpio_pin_read(void *, int);
93 1.8.4.1 yamt static void nsclpcsio_gpio_pin_ctl(void *, int, int);
94 1.8.4.1 yamt #endif
95 1.8.4.1 yamt
96 1.1 drochner static u_int8_t
97 1.1 drochner nsread(iot, ioh, idx)
98 1.1 drochner bus_space_tag_t iot;
99 1.1 drochner bus_space_handle_t ioh;
100 1.1 drochner int idx;
101 1.1 drochner {
102 1.1 drochner
103 1.1 drochner bus_space_write_1(iot, ioh, 0, idx);
104 1.1 drochner return (bus_space_read_1(iot, ioh, 1));
105 1.1 drochner }
106 1.1 drochner
107 1.1 drochner static void
108 1.1 drochner nswrite(iot, ioh, idx, data)
109 1.1 drochner bus_space_tag_t iot;
110 1.1 drochner bus_space_handle_t ioh;
111 1.1 drochner int idx;
112 1.1 drochner u_int8_t data;
113 1.1 drochner {
114 1.1 drochner
115 1.1 drochner bus_space_write_1(iot, ioh, 0, idx);
116 1.1 drochner bus_space_write_1(iot, ioh, 1, data);
117 1.1 drochner }
118 1.1 drochner
119 1.1 drochner static int
120 1.1 drochner nscheck(iot, base)
121 1.1 drochner bus_space_tag_t iot;
122 1.1 drochner int base;
123 1.1 drochner {
124 1.1 drochner bus_space_handle_t ioh;
125 1.1 drochner int rv = 0;
126 1.1 drochner
127 1.1 drochner if (bus_space_map(iot, base, 2, 0, &ioh))
128 1.1 drochner return (0);
129 1.1 drochner
130 1.1 drochner /* XXX this is for PC87366 only for now */
131 1.1 drochner if (nsread(iot, ioh, 0x20) == 0xe9)
132 1.1 drochner rv = 1;
133 1.1 drochner
134 1.1 drochner bus_space_unmap(iot, ioh, 2);
135 1.1 drochner return (rv);
136 1.1 drochner }
137 1.1 drochner
138 1.1 drochner static int
139 1.8.4.2 yamt nsclpcsio_isa_match(struct device *parent,
140 1.8.4.2 yamt struct cfdata *match, void *aux)
141 1.1 drochner {
142 1.1 drochner struct isa_attach_args *ia = aux;
143 1.1 drochner int iobase;
144 1.1 drochner
145 1.1 drochner if (ISA_DIRECT_CONFIG(ia))
146 1.1 drochner return (0);
147 1.1 drochner
148 1.6 drochner if (ia->ia_nio > 0 && ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT) {
149 1.1 drochner /* XXX check for legal iobase ??? */
150 1.1 drochner if (nscheck(ia->ia_iot, ia->ia_io[0].ir_addr)) {
151 1.1 drochner iobase = ia->ia_io[0].ir_addr;
152 1.1 drochner goto found;
153 1.1 drochner }
154 1.5 drochner return (0);
155 1.1 drochner }
156 1.1 drochner
157 1.1 drochner /* PC87366 has two possible locations depending on wiring */
158 1.1 drochner if (nscheck(ia->ia_iot, 0x2e)) {
159 1.1 drochner iobase = 0x2e;
160 1.1 drochner goto found;
161 1.1 drochner }
162 1.1 drochner if (nscheck(ia->ia_iot, 0x4e)) {
163 1.1 drochner iobase = 0x4e;
164 1.1 drochner goto found;
165 1.1 drochner }
166 1.1 drochner return (0);
167 1.1 drochner
168 1.1 drochner found:
169 1.1 drochner ia->ia_nio = 1;
170 1.1 drochner ia->ia_io[0].ir_addr = iobase;
171 1.1 drochner ia->ia_io[0].ir_size = 2;
172 1.1 drochner ia->ia_niomem = 0;
173 1.1 drochner ia->ia_nirq = 0;
174 1.1 drochner ia->ia_ndrq = 0;
175 1.1 drochner return (1);
176 1.1 drochner }
177 1.1 drochner
178 1.1 drochner static void
179 1.8.4.2 yamt nsclpcsio_isa_attach(struct device *parent, struct device *self,
180 1.8.4.2 yamt void *aux)
181 1.1 drochner {
182 1.1 drochner struct nsclpcsio_softc *sc = (void *)self;
183 1.1 drochner struct isa_attach_args *ia = aux;
184 1.8.4.1 yamt #if NGPIO > 0
185 1.8.4.1 yamt struct gpiobus_attach_args gba;
186 1.8.4.1 yamt #endif
187 1.1 drochner bus_space_tag_t iot;
188 1.1 drochner bus_space_handle_t ioh;
189 1.1 drochner u_int8_t val;
190 1.8.4.1 yamt int tms_iobase, gpio_iobase = 0;
191 1.1 drochner int i;
192 1.1 drochner
193 1.1 drochner sc->sc_iot = iot = ia->ia_iot;
194 1.1 drochner if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr, 2, 0, &ioh)) {
195 1.1 drochner printf(": can't map i/o space\n");
196 1.1 drochner return;
197 1.1 drochner }
198 1.1 drochner sc->sc_ioh = ioh;
199 1.1 drochner printf(": NSC PC87366 rev. %d\n", nsread(iot, ioh, 0x27));
200 1.1 drochner
201 1.8.4.3 yamt mutex_init(&sc->sc_lock, MUTEX_DRIVER, IPL_NONE);
202 1.8.4.1 yamt
203 1.8.4.1 yamt nswrite(iot, ioh, 0x07, 0x07); /* select gpio */
204 1.8.4.1 yamt
205 1.8.4.1 yamt val = nsread(iot, ioh, 0x30); /* control register */
206 1.8.4.1 yamt if (!(val & 1)) {
207 1.8.4.1 yamt printf("%s: GPIO disabled\n", sc->sc_dev.dv_xname);
208 1.8.4.1 yamt } else {
209 1.8.4.1 yamt gpio_iobase = (nsread(iot, ioh, 0x60) << 8) |
210 1.8.4.1 yamt nsread(iot, ioh, 0x61);
211 1.8.4.1 yamt sc->sc_gpio_iot = iot;
212 1.8.4.1 yamt if (bus_space_map(iot, gpio_iobase, 0x2c, 0,
213 1.8.4.1 yamt &sc->sc_gpio_ioh)) {
214 1.8.4.1 yamt printf("%s: can't map GPIO i/o space\n",
215 1.8.4.1 yamt sc->sc_dev.dv_xname);
216 1.8.4.1 yamt return;
217 1.8.4.1 yamt }
218 1.8.4.1 yamt printf("%s: GPIO at 0x%x\n", sc->sc_dev.dv_xname, gpio_iobase);
219 1.8.4.1 yamt
220 1.8.4.1 yamt #if NGPIO > 0
221 1.8.4.1 yamt nsclpcsio_gpio_init(sc);
222 1.8.4.1 yamt #endif
223 1.8.4.1 yamt }
224 1.8.4.1 yamt
225 1.1 drochner nswrite(iot, ioh, 0x07, 0x0e); /* select tms */
226 1.1 drochner
227 1.1 drochner val = nsread(iot, ioh, 0x30); /* control register */
228 1.1 drochner if (!(val & 1)) {
229 1.1 drochner printf("%s: TMS disabled\n", sc->sc_dev.dv_xname);
230 1.1 drochner return;
231 1.1 drochner }
232 1.1 drochner
233 1.1 drochner tms_iobase = (nsread(iot, ioh, 0x60) << 8) | nsread(iot, ioh, 0x61);
234 1.1 drochner sc->sc_tms_iot = iot;
235 1.1 drochner if (bus_space_map(iot, tms_iobase, 16, 0, &sc->sc_tms_ioh)) {
236 1.1 drochner printf("%s: can't map TMS i/o space\n", sc->sc_dev.dv_xname);
237 1.1 drochner return;
238 1.1 drochner }
239 1.1 drochner printf("%s: TMS at 0x%x\n", sc->sc_dev.dv_xname, tms_iobase);
240 1.1 drochner
241 1.1 drochner if (bus_space_read_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x08) & 1) {
242 1.1 drochner printf("%s: TMS in standby mode\n", sc->sc_dev.dv_xname);
243 1.8.4.1 yamt
244 1.8.4.1 yamt /* Wake up the TMS and enable all temperature sensors. */
245 1.8.4.1 yamt bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x08, 0x00);
246 1.8.4.1 yamt bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x09, 0x00);
247 1.8.4.1 yamt bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x0a, 0x01);
248 1.8.4.1 yamt bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x09, 0x01);
249 1.8.4.1 yamt bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x0a, 0x01);
250 1.8.4.1 yamt bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x09, 0x02);
251 1.8.4.1 yamt bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x0a, 0x01);
252 1.8.4.1 yamt
253 1.8.4.1 yamt if (!(bus_space_read_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x08)
254 1.8.4.1 yamt & 1)) {
255 1.8.4.1 yamt printf("%s: TMS awoken\n", sc->sc_dev.dv_xname);
256 1.8.4.1 yamt } else {
257 1.8.4.1 yamt return;
258 1.8.4.1 yamt }
259 1.1 drochner }
260 1.1 drochner
261 1.1 drochner /* Initialize sensor meta data */
262 1.1 drochner for (i = 0; i < 3; i++) {
263 1.8.4.4 yamt sc->sc_data[i].sensor = i;
264 1.8.4.4 yamt sc->sc_data[i].units =ENVSYS_STEMP;
265 1.1 drochner }
266 1.8.4.4 yamt strcpy(sc->sc_data[0].desc, "TSENS1");
267 1.8.4.4 yamt strcpy(sc->sc_data[1].desc, "TSENS2");
268 1.8.4.4 yamt strcpy(sc->sc_data[2].desc, "TNSC");
269 1.1 drochner
270 1.1 drochner /* Get initial set of sensor values. */
271 1.1 drochner for (i = 0; i < 3; i++)
272 1.1 drochner tms_update(sc, i);
273 1.1 drochner
274 1.1 drochner /*
275 1.1 drochner * Hook into the System Monitor.
276 1.1 drochner */
277 1.8.4.4 yamt sc->sc_sysmon.sme_name = sc->sc_dev.dv_xname;
278 1.1 drochner sc->sc_sysmon.sme_sensor_data = sc->sc_data;
279 1.1 drochner sc->sc_sysmon.sme_cookie = sc;
280 1.1 drochner sc->sc_sysmon.sme_gtredata = tms_gtredata;
281 1.1 drochner sc->sc_sysmon.sme_nsensors = 3;
282 1.1 drochner
283 1.1 drochner if (sysmon_envsys_register(&sc->sc_sysmon))
284 1.1 drochner printf("%s: unable to register with sysmon\n",
285 1.1 drochner sc->sc_dev.dv_xname);
286 1.8.4.1 yamt
287 1.8.4.1 yamt #if NGPIO > 0
288 1.8.4.1 yamt /* attach GPIO framework */
289 1.8.4.1 yamt if (gpio_iobase != 0) {
290 1.8.4.1 yamt gba.gba_gc = &sc->sc_gpio_gc;
291 1.8.4.1 yamt gba.gba_pins = sc->sc_gpio_pins;
292 1.8.4.1 yamt gba.gba_npins = GPIO_NPINS;
293 1.8.4.1 yamt config_found_ia(&sc->sc_dev, "gpiobus", &gba, NULL);
294 1.8.4.1 yamt }
295 1.8.4.1 yamt #endif
296 1.8.4.1 yamt return;
297 1.1 drochner }
298 1.1 drochner
299 1.1 drochner static void
300 1.1 drochner tms_update(sc, chan)
301 1.1 drochner struct nsclpcsio_softc *sc;
302 1.1 drochner int chan;
303 1.1 drochner {
304 1.1 drochner bus_space_tag_t iot = sc->sc_tms_iot;
305 1.1 drochner bus_space_handle_t ioh = sc->sc_tms_ioh;
306 1.1 drochner u_int8_t status;
307 1.1 drochner int8_t temp, ctemp; /* signed!! */
308 1.1 drochner
309 1.8.4.3 yamt mutex_enter(&sc->sc_lock);
310 1.8.4.1 yamt
311 1.8.4.1 yamt nswrite(iot, ioh, 0x07, 0x0e); /* select tms */
312 1.8.4.1 yamt
313 1.1 drochner bus_space_write_1(iot, ioh, 0x09, chan); /* select */
314 1.1 drochner
315 1.1 drochner status = bus_space_read_1(iot, ioh, 0x0a); /* config/status */
316 1.1 drochner if (status & 0x01) {
317 1.1 drochner /* enabled */
318 1.8.4.4 yamt sc->sc_data[chan].state = ENVSYS_SVALID;
319 1.8.4.4 yamt } else {
320 1.8.4.4 yamt sc->sc_data[chan].state = ENVSYS_SINVALID;
321 1.8.4.3 yamt mutex_exit(&sc->sc_lock);
322 1.1 drochner return;
323 1.1 drochner }
324 1.1 drochner
325 1.1 drochner /*
326 1.1 drochner * If the channel is enabled, it is considered valid.
327 1.1 drochner * An "open circuit" might be temporary.
328 1.1 drochner */
329 1.8.4.4 yamt #if 0
330 1.8.4.4 yamt sc->sc_data[chan].state = ENVSYS_SVALID;
331 1.1 drochner if (status & 0x40) {
332 1.1 drochner /*
333 1.1 drochner * open circuit
334 1.1 drochner * XXX should have a warning for it
335 1.1 drochner */
336 1.1 drochner sc->sc_data[chan].warnflags = ENVSYS_WARN_OK; /* XXX */
337 1.8.4.3 yamt mutex_exit(&sc->sc_lock);
338 1.1 drochner return;
339 1.1 drochner }
340 1.8.4.4 yamt #endif
341 1.1 drochner
342 1.1 drochner /* get current temperature in signed degree celsius */
343 1.1 drochner temp = bus_space_read_1(iot, ioh, 0x0b);
344 1.8.4.4 yamt sc->sc_data[chan].value_cur = (int)temp * 1000000 + 273150000;
345 1.8.4.4 yamt sc->sc_data[chan].state = ENVSYS_SVALID;
346 1.1 drochner
347 1.1 drochner if (status & 0x0e) { /* any temperature warning? */
348 1.1 drochner /*
349 1.1 drochner * XXX the chip documentation is a bit fuzzy - it doesn't state
350 1.1 drochner * that the hardware OTS output depends on the "overtemp"
351 1.1 drochner * warning bit.
352 1.1 drochner * It seems the output gets cleared if the warning bit is reset.
353 1.1 drochner * This sucks.
354 1.1 drochner * The hardware might do something useful with output pins, eg
355 1.1 drochner * throttling the CPU, so we must do the comparision in
356 1.1 drochner * software, and only reset the bits if the reason is gone.
357 1.1 drochner */
358 1.1 drochner if (status & 0x02) { /* low limit */
359 1.8.4.4 yamt sc->sc_data[chan].state = ENVSYS_SWARNUNDER;
360 1.1 drochner /* read low limit */
361 1.1 drochner ctemp = bus_space_read_1(iot, ioh, 0x0d);
362 1.1 drochner if (temp <= ctemp) /* still valid, don't reset */
363 1.1 drochner status &= ~0x02;
364 1.1 drochner }
365 1.1 drochner if (status & 0x04) { /* high limit */
366 1.8.4.4 yamt sc->sc_data[chan].state = ENVSYS_SWARNOVER;
367 1.1 drochner /* read high limit */
368 1.1 drochner ctemp = bus_space_read_1(iot, ioh, 0x0c);
369 1.1 drochner if (temp >= ctemp) /* still valid, don't reset */
370 1.1 drochner status &= ~0x04;
371 1.1 drochner }
372 1.1 drochner if (status & 0x08) { /* overtemperature */
373 1.8.4.4 yamt sc->sc_data[chan].state = ENVSYS_SCRITOVER;
374 1.1 drochner /* read overtemperature limit */
375 1.1 drochner ctemp = bus_space_read_1(iot, ioh, 0x0e);
376 1.1 drochner if (temp >= ctemp) /* still valid, don't reset */
377 1.1 drochner status &= ~0x08;
378 1.1 drochner }
379 1.1 drochner
380 1.1 drochner /* clear outdated warnings */
381 1.1 drochner if (status & 0x0e)
382 1.1 drochner bus_space_write_1(iot, ioh, 0x0a, status);
383 1.1 drochner }
384 1.8.4.1 yamt
385 1.8.4.3 yamt mutex_exit(&sc->sc_lock);
386 1.8.4.1 yamt
387 1.8.4.1 yamt return;
388 1.1 drochner }
389 1.1 drochner
390 1.1 drochner static int
391 1.8.4.4 yamt tms_gtredata(struct sysmon_envsys *sme, envsys_data_t *data)
392 1.1 drochner {
393 1.1 drochner struct nsclpcsio_softc *sc = sme->sme_cookie;
394 1.1 drochner
395 1.1 drochner tms_update(sc, data->sensor);
396 1.1 drochner return (0);
397 1.1 drochner }
398 1.8.4.1 yamt
399 1.8.4.1 yamt #if NGPIO > 0
400 1.8.4.1 yamt static void
401 1.8.4.1 yamt nsclpcsio_gpio_pin_select(struct nsclpcsio_softc *sc, int pin)
402 1.8.4.1 yamt {
403 1.8.4.1 yamt u_int8_t v;
404 1.8.4.1 yamt bus_space_tag_t iot = sc->sc_iot;
405 1.8.4.1 yamt bus_space_handle_t ioh = sc->sc_ioh;
406 1.8.4.1 yamt
407 1.8.4.1 yamt v = ((pin / 8) << 4) | (pin % 8);
408 1.8.4.1 yamt
409 1.8.4.1 yamt nswrite(iot, ioh, 0x07, 0x07); /* select gpio */
410 1.8.4.1 yamt nswrite(iot, ioh, 0xf0, v);
411 1.8.4.1 yamt
412 1.8.4.1 yamt return;
413 1.8.4.1 yamt }
414 1.8.4.1 yamt
415 1.8.4.1 yamt static void
416 1.8.4.1 yamt nsclpcsio_gpio_init(struct nsclpcsio_softc *sc)
417 1.8.4.1 yamt {
418 1.8.4.1 yamt int i;
419 1.8.4.1 yamt
420 1.8.4.1 yamt for (i = 0; i < GPIO_NPINS; i++) {
421 1.8.4.1 yamt sc->sc_gpio_pins[i].pin_num = i;
422 1.8.4.1 yamt sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
423 1.8.4.1 yamt GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN |
424 1.8.4.1 yamt GPIO_PIN_PUSHPULL | GPIO_PIN_TRISTATE |
425 1.8.4.1 yamt GPIO_PIN_PULLUP;
426 1.8.4.1 yamt /* safe defaults */
427 1.8.4.1 yamt sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_TRISTATE;
428 1.8.4.1 yamt sc->sc_gpio_pins[i].pin_state = GPIO_PIN_LOW;
429 1.8.4.1 yamt nsclpcsio_gpio_pin_ctl(sc, i, sc->sc_gpio_pins[i].pin_flags);
430 1.8.4.1 yamt nsclpcsio_gpio_pin_write(sc, i, sc->sc_gpio_pins[i].pin_state);
431 1.8.4.1 yamt }
432 1.8.4.1 yamt
433 1.8.4.1 yamt /* create controller tag */
434 1.8.4.1 yamt sc->sc_gpio_gc.gp_cookie = sc;
435 1.8.4.1 yamt sc->sc_gpio_gc.gp_pin_read = nsclpcsio_gpio_pin_read;
436 1.8.4.1 yamt sc->sc_gpio_gc.gp_pin_write = nsclpcsio_gpio_pin_write;
437 1.8.4.1 yamt sc->sc_gpio_gc.gp_pin_ctl = nsclpcsio_gpio_pin_ctl;
438 1.8.4.1 yamt }
439 1.8.4.1 yamt
440 1.8.4.1 yamt static int
441 1.8.4.1 yamt nsclpcsio_gpio_pin_read(void *aux, int pin)
442 1.8.4.1 yamt {
443 1.8.4.1 yamt struct nsclpcsio_softc *sc = (struct nsclpcsio_softc *)aux;
444 1.8.4.1 yamt int port, shift, reg;
445 1.8.4.1 yamt u_int8_t v;
446 1.8.4.1 yamt
447 1.8.4.1 yamt reg = 0x00;
448 1.8.4.1 yamt port = pin / 8;
449 1.8.4.1 yamt shift = pin % 8;
450 1.8.4.1 yamt
451 1.8.4.1 yamt switch (port) {
452 1.8.4.1 yamt case 0: reg = 0x00; break;
453 1.8.4.1 yamt case 1: reg = 0x04; break;
454 1.8.4.1 yamt case 2: reg = 0x08; break;
455 1.8.4.1 yamt case 3: reg = 0x0a; break;
456 1.8.4.1 yamt }
457 1.8.4.1 yamt
458 1.8.4.1 yamt v = GPIO_READ(sc, reg);
459 1.8.4.1 yamt
460 1.8.4.1 yamt return ((v >> shift) & 0x1);
461 1.8.4.1 yamt }
462 1.8.4.1 yamt
463 1.8.4.1 yamt static void
464 1.8.4.1 yamt nsclpcsio_gpio_pin_write(void *aux, int pin, int v)
465 1.8.4.1 yamt {
466 1.8.4.1 yamt struct nsclpcsio_softc *sc = (struct nsclpcsio_softc *)aux;
467 1.8.4.1 yamt int port, shift, reg;
468 1.8.4.1 yamt u_int8_t d;
469 1.8.4.1 yamt
470 1.8.4.1 yamt port = pin / 8;
471 1.8.4.1 yamt shift = pin % 8;
472 1.8.4.1 yamt
473 1.8.4.1 yamt switch (port) {
474 1.8.4.1 yamt case 0: reg = 0x00; break;
475 1.8.4.1 yamt case 1: reg = 0x04; break;
476 1.8.4.1 yamt case 2: reg = 0x08; break;
477 1.8.4.1 yamt case 3: reg = 0x0a; break;
478 1.8.4.1 yamt default: reg = 0x00; break; /* shouldn't happen */
479 1.8.4.1 yamt }
480 1.8.4.1 yamt
481 1.8.4.1 yamt d = GPIO_READ(sc, reg);
482 1.8.4.1 yamt if (v == 0)
483 1.8.4.1 yamt d &= ~(1 << shift);
484 1.8.4.1 yamt else if (v == 1)
485 1.8.4.1 yamt d |= (1 << shift);
486 1.8.4.1 yamt GPIO_WRITE(sc, reg, d);
487 1.8.4.1 yamt
488 1.8.4.1 yamt return;
489 1.8.4.1 yamt }
490 1.8.4.1 yamt
491 1.8.4.1 yamt void
492 1.8.4.1 yamt nsclpcsio_gpio_pin_ctl(void *aux, int pin, int flags)
493 1.8.4.1 yamt {
494 1.8.4.1 yamt struct nsclpcsio_softc *sc = (struct nsclpcsio_softc *)aux;
495 1.8.4.1 yamt u_int8_t conf;
496 1.8.4.1 yamt
497 1.8.4.3 yamt mutex_enter(&sc->sc_lock);
498 1.8.4.1 yamt
499 1.8.4.1 yamt nswrite(sc->sc_iot, sc->sc_ioh, 0x07, 0x07); /* select gpio */
500 1.8.4.1 yamt nsclpcsio_gpio_pin_select(sc, pin);
501 1.8.4.1 yamt conf = nsread(sc->sc_iot, sc->sc_ioh, 0xf1);
502 1.8.4.1 yamt
503 1.8.4.1 yamt conf &= ~(SIO_GPIO_CONF_OUTPUTEN | SIO_GPIO_CONF_PUSHPULL |
504 1.8.4.1 yamt SIO_GPIO_CONF_PULLUP);
505 1.8.4.1 yamt if ((flags & GPIO_PIN_TRISTATE) == 0)
506 1.8.4.1 yamt conf |= SIO_GPIO_CONF_OUTPUTEN;
507 1.8.4.1 yamt if (flags & GPIO_PIN_PUSHPULL)
508 1.8.4.1 yamt conf |= SIO_GPIO_CONF_PUSHPULL;
509 1.8.4.1 yamt if (flags & GPIO_PIN_PULLUP)
510 1.8.4.1 yamt conf |= SIO_GPIO_CONF_PULLUP;
511 1.8.4.1 yamt
512 1.8.4.1 yamt nswrite(sc->sc_iot, sc->sc_ioh, 0xf1, conf);
513 1.8.4.1 yamt
514 1.8.4.3 yamt mutex_exit(&sc->sc_lock);
515 1.8.4.1 yamt
516 1.8.4.1 yamt return;
517 1.8.4.1 yamt }
518 1.8.4.1 yamt #endif /* NGPIO */
519