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