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