nsclpcsio_isa.c revision 1.12 1 /* $NetBSD: nsclpcsio_isa.c,v 1.12 2005/10/11 15:58:38 drochner 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.12 2005/10/11 15:58:38 drochner Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/lock.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 struct simplelock 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(parent, match, aux)
146 struct device *parent;
147 struct cfdata *match;
148 void *aux;
149 {
150 struct isa_attach_args *ia = aux;
151 int iobase;
152
153 if (ISA_DIRECT_CONFIG(ia))
154 return (0);
155
156 if (ia->ia_nio > 0 && ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT) {
157 /* XXX check for legal iobase ??? */
158 if (nscheck(ia->ia_iot, ia->ia_io[0].ir_addr)) {
159 iobase = ia->ia_io[0].ir_addr;
160 goto found;
161 }
162 return (0);
163 }
164
165 /* PC87366 has two possible locations depending on wiring */
166 if (nscheck(ia->ia_iot, 0x2e)) {
167 iobase = 0x2e;
168 goto found;
169 }
170 if (nscheck(ia->ia_iot, 0x4e)) {
171 iobase = 0x4e;
172 goto found;
173 }
174 return (0);
175
176 found:
177 ia->ia_nio = 1;
178 ia->ia_io[0].ir_addr = iobase;
179 ia->ia_io[0].ir_size = 2;
180 ia->ia_niomem = 0;
181 ia->ia_nirq = 0;
182 ia->ia_ndrq = 0;
183 return (1);
184 }
185
186 static void
187 nsclpcsio_isa_attach(parent, self, aux)
188 struct device *parent, *self;
189 void *aux;
190 {
191 struct nsclpcsio_softc *sc = (void *)self;
192 struct isa_attach_args *ia = aux;
193 #if NGPIO > 0
194 struct gpiobus_attach_args gba;
195 #endif
196 bus_space_tag_t iot;
197 bus_space_handle_t ioh;
198 u_int8_t val;
199 int tms_iobase, gpio_iobase = 0;
200 int i;
201
202 sc->sc_iot = iot = ia->ia_iot;
203 if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr, 2, 0, &ioh)) {
204 printf(": can't map i/o space\n");
205 return;
206 }
207 sc->sc_ioh = ioh;
208 printf(": NSC PC87366 rev. %d\n", nsread(iot, ioh, 0x27));
209
210 simple_lock_init(&sc->sc_lock);
211
212 nswrite(iot, ioh, 0x07, 0x07); /* select gpio */
213
214 val = nsread(iot, ioh, 0x30); /* control register */
215 if (!(val & 1)) {
216 printf("%s: GPIO disabled\n", sc->sc_dev.dv_xname);
217 } else {
218 gpio_iobase = (nsread(iot, ioh, 0x60) << 8) |
219 nsread(iot, ioh, 0x61);
220 sc->sc_gpio_iot = iot;
221 if (bus_space_map(iot, gpio_iobase, 0x2c, 0,
222 &sc->sc_gpio_ioh)) {
223 printf("%s: can't map GPIO i/o space\n",
224 sc->sc_dev.dv_xname);
225 return;
226 }
227 printf("%s: GPIO at 0x%x\n", sc->sc_dev.dv_xname, gpio_iobase);
228
229 #if NGPIO > 0
230 nsclpcsio_gpio_init(sc);
231 #endif
232 }
233
234 nswrite(iot, ioh, 0x07, 0x0e); /* select tms */
235
236 val = nsread(iot, ioh, 0x30); /* control register */
237 if (!(val & 1)) {
238 printf("%s: TMS disabled\n", sc->sc_dev.dv_xname);
239 return;
240 }
241
242 tms_iobase = (nsread(iot, ioh, 0x60) << 8) | nsread(iot, ioh, 0x61);
243 sc->sc_tms_iot = iot;
244 if (bus_space_map(iot, tms_iobase, 16, 0, &sc->sc_tms_ioh)) {
245 printf("%s: can't map TMS i/o space\n", sc->sc_dev.dv_xname);
246 return;
247 }
248 printf("%s: TMS at 0x%x\n", sc->sc_dev.dv_xname, tms_iobase);
249
250 if (bus_space_read_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x08) & 1) {
251 printf("%s: TMS in standby mode\n", sc->sc_dev.dv_xname);
252
253 /* Wake up the TMS and enable all temperature sensors. */
254 bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x08, 0x00);
255 bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x09, 0x00);
256 bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x0a, 0x01);
257 bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x09, 0x01);
258 bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x0a, 0x01);
259 bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x09, 0x02);
260 bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x0a, 0x01);
261
262 if (!(bus_space_read_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x08)
263 & 1)) {
264 printf("%s: TMS awoken\n", sc->sc_dev.dv_xname);
265 } else {
266 return;
267 }
268 }
269
270 /* Initialize sensor meta data */
271 for (i = 0; i < 3; i++) {
272 sc->sc_data[i].sensor = sc->sc_info[i].sensor = i;
273 sc->sc_data[i].units = sc->sc_info[i].units = ENVSYS_STEMP;
274 }
275 strcpy(sc->sc_info[0].desc, "TSENS1");
276 strcpy(sc->sc_info[1].desc, "TSENS2");
277 strcpy(sc->sc_info[2].desc, "TNSC");
278
279 /* Get initial set of sensor values. */
280 for (i = 0; i < 3; i++)
281 tms_update(sc, i);
282
283 /*
284 * Hook into the System Monitor.
285 */
286 sc->sc_sysmon.sme_ranges = tms_ranges;
287 sc->sc_sysmon.sme_sensor_info = sc->sc_info;
288 sc->sc_sysmon.sme_sensor_data = sc->sc_data;
289 sc->sc_sysmon.sme_cookie = sc;
290
291 sc->sc_sysmon.sme_gtredata = tms_gtredata;
292 sc->sc_sysmon.sme_streinfo = tms_streinfo;
293
294 sc->sc_sysmon.sme_nsensors = 3;
295 sc->sc_sysmon.sme_envsys_version = 1000;
296
297 if (sysmon_envsys_register(&sc->sc_sysmon))
298 printf("%s: unable to register with sysmon\n",
299 sc->sc_dev.dv_xname);
300
301 #if NGPIO > 0
302 /* attach GPIO framework */
303 if (gpio_iobase != 0) {
304 gba.gba_gc = &sc->sc_gpio_gc;
305 gba.gba_pins = sc->sc_gpio_pins;
306 gba.gba_npins = GPIO_NPINS;
307 config_found_ia(&sc->sc_dev, "gpiobus", &gba, NULL);
308 }
309 #endif
310 return;
311 }
312
313 static void
314 tms_update(sc, chan)
315 struct nsclpcsio_softc *sc;
316 int chan;
317 {
318 bus_space_tag_t iot = sc->sc_tms_iot;
319 bus_space_handle_t ioh = sc->sc_tms_ioh;
320 u_int8_t status;
321 int8_t temp, ctemp; /* signed!! */
322
323 simple_lock(&sc->sc_lock);
324
325 nswrite(iot, ioh, 0x07, 0x0e); /* select tms */
326
327 bus_space_write_1(iot, ioh, 0x09, chan); /* select */
328
329 status = bus_space_read_1(iot, ioh, 0x0a); /* config/status */
330 if (status & 0x01) {
331 /* enabled */
332 sc->sc_info[chan].validflags = ENVSYS_FVALID;
333 }else {
334 sc->sc_info[chan].validflags = 0;
335 simple_unlock(&sc->sc_lock);
336 return;
337 }
338
339 /*
340 * If the channel is enabled, it is considered valid.
341 * An "open circuit" might be temporary.
342 */
343 sc->sc_data[chan].validflags = ENVSYS_FVALID;
344 if (status & 0x40) {
345 /*
346 * open circuit
347 * XXX should have a warning for it
348 */
349 sc->sc_data[chan].warnflags = ENVSYS_WARN_OK; /* XXX */
350 simple_unlock(&sc->sc_lock);
351 return;
352 }
353
354 /* get current temperature in signed degree celsius */
355 temp = bus_space_read_1(iot, ioh, 0x0b);
356 sc->sc_data[chan].cur.data_us = (int)temp * 1000000 + 273150000;
357 sc->sc_data[chan].validflags |= ENVSYS_FCURVALID;
358
359 if (status & 0x0e) { /* any temperature warning? */
360 /*
361 * XXX the chip documentation is a bit fuzzy - it doesn't state
362 * that the hardware OTS output depends on the "overtemp"
363 * warning bit.
364 * It seems the output gets cleared if the warning bit is reset.
365 * This sucks.
366 * The hardware might do something useful with output pins, eg
367 * throttling the CPU, so we must do the comparision in
368 * software, and only reset the bits if the reason is gone.
369 */
370 if (status & 0x02) { /* low limit */
371 sc->sc_data[chan].warnflags = ENVSYS_WARN_UNDER;
372 /* read low limit */
373 ctemp = bus_space_read_1(iot, ioh, 0x0d);
374 if (temp <= ctemp) /* still valid, don't reset */
375 status &= ~0x02;
376 }
377 if (status & 0x04) { /* high limit */
378 sc->sc_data[chan].warnflags = ENVSYS_WARN_OVER;
379 /* read high limit */
380 ctemp = bus_space_read_1(iot, ioh, 0x0c);
381 if (temp >= ctemp) /* still valid, don't reset */
382 status &= ~0x04;
383 }
384 if (status & 0x08) { /* overtemperature */
385 sc->sc_data[chan].warnflags = ENVSYS_WARN_CRITOVER;
386 /* read overtemperature limit */
387 ctemp = bus_space_read_1(iot, ioh, 0x0e);
388 if (temp >= ctemp) /* still valid, don't reset */
389 status &= ~0x08;
390 }
391
392 /* clear outdated warnings */
393 if (status & 0x0e)
394 bus_space_write_1(iot, ioh, 0x0a, status);
395 }
396
397 simple_unlock(&sc->sc_lock);
398
399 return;
400 }
401
402 static int
403 tms_gtredata(sme, data)
404 struct sysmon_envsys *sme;
405 struct envsys_tre_data *data;
406 {
407 struct nsclpcsio_softc *sc = sme->sme_cookie;
408
409 tms_update(sc, data->sensor);
410
411 *data = sc->sc_data[data->sensor];
412 return (0);
413 }
414
415 static int
416 tms_streinfo(sme, info)
417 struct sysmon_envsys *sme;
418 struct envsys_basic_info *info;
419 {
420 #if 0
421 struct nsclpcsio_softc *sc = sme->sme_cookie;
422 #endif
423 /* XXX Not implemented */
424 info->validflags = 0;
425
426 return (0);
427 }
428
429 #if NGPIO > 0
430 static void
431 nsclpcsio_gpio_pin_select(struct nsclpcsio_softc *sc, int pin)
432 {
433 u_int8_t v;
434 bus_space_tag_t iot = sc->sc_iot;
435 bus_space_handle_t ioh = sc->sc_ioh;
436
437 v = ((pin / 8) << 4) | (pin % 8);
438
439 nswrite(iot, ioh, 0x07, 0x07); /* select gpio */
440 nswrite(iot, ioh, 0xf0, v);
441
442 return;
443 }
444
445 static void
446 nsclpcsio_gpio_init(struct nsclpcsio_softc *sc)
447 {
448 int i;
449
450 for (i = 0; i < GPIO_NPINS; i++) {
451 sc->sc_gpio_pins[i].pin_num = i;
452 sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
453 GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN |
454 GPIO_PIN_PUSHPULL | GPIO_PIN_TRISTATE |
455 GPIO_PIN_PULLUP;
456 /* safe defaults */
457 sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_TRISTATE;
458 sc->sc_gpio_pins[i].pin_state = GPIO_PIN_LOW;
459 nsclpcsio_gpio_pin_ctl(sc, i, sc->sc_gpio_pins[i].pin_flags);
460 nsclpcsio_gpio_pin_write(sc, i, sc->sc_gpio_pins[i].pin_state);
461 }
462
463 /* create controller tag */
464 sc->sc_gpio_gc.gp_cookie = sc;
465 sc->sc_gpio_gc.gp_pin_read = nsclpcsio_gpio_pin_read;
466 sc->sc_gpio_gc.gp_pin_write = nsclpcsio_gpio_pin_write;
467 sc->sc_gpio_gc.gp_pin_ctl = nsclpcsio_gpio_pin_ctl;
468 }
469
470 static int
471 nsclpcsio_gpio_pin_read(void *aux, int pin)
472 {
473 struct nsclpcsio_softc *sc = (struct nsclpcsio_softc *)aux;
474 int port, shift, reg;
475 u_int8_t v;
476
477 reg = 0x00;
478 port = pin / 8;
479 shift = pin % 8;
480
481 switch (port) {
482 case 0: reg = 0x00; break;
483 case 1: reg = 0x04; break;
484 case 2: reg = 0x08; break;
485 case 3: reg = 0x0a; break;
486 }
487
488 v = GPIO_READ(sc, reg);
489
490 return ((v >> shift) & 0x1);
491 }
492
493 static void
494 nsclpcsio_gpio_pin_write(void *aux, int pin, int v)
495 {
496 struct nsclpcsio_softc *sc = (struct nsclpcsio_softc *)aux;
497 int port, shift, reg;
498 u_int8_t d;
499
500 port = pin / 8;
501 shift = pin % 8;
502
503 switch (port) {
504 case 0: reg = 0x00; break;
505 case 1: reg = 0x04; break;
506 case 2: reg = 0x08; break;
507 case 3: reg = 0x0a; break;
508 default: reg = 0x00; break; /* shouldn't happen */
509 }
510
511 d = GPIO_READ(sc, reg);
512 if (v == 0)
513 d &= ~(1 << shift);
514 else if (v == 1)
515 d |= (1 << shift);
516 GPIO_WRITE(sc, reg, d);
517
518 return;
519 }
520
521 void
522 nsclpcsio_gpio_pin_ctl(void *aux, int pin, int flags)
523 {
524 struct nsclpcsio_softc *sc = (struct nsclpcsio_softc *)aux;
525 u_int8_t conf;
526
527 simple_lock(&sc->sc_lock);
528
529 nswrite(sc->sc_iot, sc->sc_ioh, 0x07, 0x07); /* select gpio */
530 nsclpcsio_gpio_pin_select(sc, pin);
531 conf = nsread(sc->sc_iot, sc->sc_ioh, 0xf1);
532
533 conf &= ~(SIO_GPIO_CONF_OUTPUTEN | SIO_GPIO_CONF_PUSHPULL |
534 SIO_GPIO_CONF_PULLUP);
535 if ((flags & GPIO_PIN_TRISTATE) == 0)
536 conf |= SIO_GPIO_CONF_OUTPUTEN;
537 if (flags & GPIO_PIN_PUSHPULL)
538 conf |= SIO_GPIO_CONF_PUSHPULL;
539 if (flags & GPIO_PIN_PULLUP)
540 conf |= SIO_GPIO_CONF_PULLUP;
541
542 nswrite(sc->sc_iot, sc->sc_ioh, 0xf1, conf);
543
544 simple_unlock(&sc->sc_lock);
545
546 return;
547 }
548 #endif /* NGPIO */
549