nsclpcsio_isa.c revision 1.9 1 /* $NetBSD: nsclpcsio_isa.c,v 1.9 2005/07/29 11:05:27 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.9 2005/07/29 11:05:27 drochner Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <machine/bus.h>
36
37 #include <dev/isa/isareg.h>
38 #include <dev/isa/isavar.h>
39 #include <dev/sysmon/sysmonvar.h>
40
41 static int nsclpcsio_isa_match(struct device *, struct cfdata *, void *);
42 static void nsclpcsio_isa_attach(struct device *, struct device *, void *);
43
44 struct nsclpcsio_softc {
45 struct device sc_dev;
46 bus_space_tag_t sc_iot, sc_tms_iot;
47 bus_space_handle_t sc_ioh, sc_tms_ioh;
48
49 struct envsys_tre_data sc_data[3];
50 struct envsys_basic_info sc_info[3];
51 struct sysmon_envsys sc_sysmon;
52 };
53
54 CFATTACH_DECL(nsclpcsio_isa, sizeof(struct nsclpcsio_softc),
55 nsclpcsio_isa_match, nsclpcsio_isa_attach, NULL, NULL);
56
57 static const struct envsys_range tms_ranges[] = {
58 { 0, 2, ENVSYS_STEMP },
59 };
60
61 static u_int8_t nsread(bus_space_tag_t, bus_space_handle_t, int);
62 static void nswrite(bus_space_tag_t, bus_space_handle_t, int, u_int8_t);
63 static int nscheck(bus_space_tag_t, int);
64
65 static void tms_update(struct nsclpcsio_softc *, int);
66 static int tms_gtredata(struct sysmon_envsys *, struct envsys_tre_data *);
67 static int tms_streinfo(struct sysmon_envsys *, struct envsys_basic_info *);
68
69 static u_int8_t
70 nsread(iot, ioh, idx)
71 bus_space_tag_t iot;
72 bus_space_handle_t ioh;
73 int idx;
74 {
75
76 bus_space_write_1(iot, ioh, 0, idx);
77 return (bus_space_read_1(iot, ioh, 1));
78 }
79
80 static void
81 nswrite(iot, ioh, idx, data)
82 bus_space_tag_t iot;
83 bus_space_handle_t ioh;
84 int idx;
85 u_int8_t data;
86 {
87
88 bus_space_write_1(iot, ioh, 0, idx);
89 bus_space_write_1(iot, ioh, 1, data);
90 }
91
92 static int
93 nscheck(iot, base)
94 bus_space_tag_t iot;
95 int base;
96 {
97 bus_space_handle_t ioh;
98 int rv = 0;
99
100 if (bus_space_map(iot, base, 2, 0, &ioh))
101 return (0);
102
103 /* XXX this is for PC87366 only for now */
104 if (nsread(iot, ioh, 0x20) == 0xe9)
105 rv = 1;
106
107 bus_space_unmap(iot, ioh, 2);
108 return (rv);
109 }
110
111 static int
112 nsclpcsio_isa_match(parent, match, aux)
113 struct device *parent;
114 struct cfdata *match;
115 void *aux;
116 {
117 struct isa_attach_args *ia = aux;
118 int iobase;
119
120 if (ISA_DIRECT_CONFIG(ia))
121 return (0);
122
123 if (ia->ia_nio > 0 && ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT) {
124 /* XXX check for legal iobase ??? */
125 if (nscheck(ia->ia_iot, ia->ia_io[0].ir_addr)) {
126 iobase = ia->ia_io[0].ir_addr;
127 goto found;
128 }
129 return (0);
130 }
131
132 /* PC87366 has two possible locations depending on wiring */
133 if (nscheck(ia->ia_iot, 0x2e)) {
134 iobase = 0x2e;
135 goto found;
136 }
137 if (nscheck(ia->ia_iot, 0x4e)) {
138 iobase = 0x4e;
139 goto found;
140 }
141 return (0);
142
143 found:
144 ia->ia_nio = 1;
145 ia->ia_io[0].ir_addr = iobase;
146 ia->ia_io[0].ir_size = 2;
147 ia->ia_niomem = 0;
148 ia->ia_nirq = 0;
149 ia->ia_ndrq = 0;
150 return (1);
151 }
152
153 static void
154 nsclpcsio_isa_attach(parent, self, aux)
155 struct device *parent, *self;
156 void *aux;
157 {
158 struct nsclpcsio_softc *sc = (void *)self;
159 struct isa_attach_args *ia = aux;
160 bus_space_tag_t iot;
161 bus_space_handle_t ioh;
162 u_int8_t val;
163 int tms_iobase;
164 int i;
165
166 sc->sc_iot = iot = ia->ia_iot;
167 if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr, 2, 0, &ioh)) {
168 printf(": can't map i/o space\n");
169 return;
170 }
171 sc->sc_ioh = ioh;
172 printf(": NSC PC87366 rev. %d\n", nsread(iot, ioh, 0x27));
173
174 nswrite(iot, ioh, 0x07, 0x0e); /* select tms */
175
176 val = nsread(iot, ioh, 0x30); /* control register */
177 if (!(val & 1)) {
178 printf("%s: TMS disabled\n", sc->sc_dev.dv_xname);
179 return;
180 }
181
182 tms_iobase = (nsread(iot, ioh, 0x60) << 8) | nsread(iot, ioh, 0x61);
183 sc->sc_tms_iot = iot;
184 if (bus_space_map(iot, tms_iobase, 16, 0, &sc->sc_tms_ioh)) {
185 printf("%s: can't map TMS i/o space\n", sc->sc_dev.dv_xname);
186 return;
187 }
188 printf("%s: TMS at 0x%x\n", sc->sc_dev.dv_xname, tms_iobase);
189
190 if (bus_space_read_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x08) & 1) {
191 printf("%s: TMS in standby mode\n", sc->sc_dev.dv_xname);
192 /* Wake up the TMS and enable all temperature sensors. */
193 bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x08, 0x00);
194 bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x09, 0x00);
195 bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x0a, 0x01);
196 bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x09, 0x01);
197 bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x0a, 0x01);
198 bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x09, 0x02);
199 bus_space_write_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x0a, 0x01);
200 if (!(bus_space_read_1(sc->sc_tms_iot, sc->sc_tms_ioh, 0x08) & 1)) {
201 printf("%s: TMS awoken\n", sc->sc_dev.dv_xname);
202 } else {
203 return;
204 }
205 return;
206 }
207
208 /* Initialize sensor meta data */
209 for (i = 0; i < 3; i++) {
210 sc->sc_data[i].sensor = sc->sc_info[i].sensor = i;
211 sc->sc_data[i].units = sc->sc_info[i].units = ENVSYS_STEMP;
212 }
213 strcpy(sc->sc_info[0].desc, "TSENS1");
214 strcpy(sc->sc_info[1].desc, "TSENS2");
215 strcpy(sc->sc_info[2].desc, "TNSC");
216
217 /* Get initial set of sensor values. */
218 for (i = 0; i < 3; i++)
219 tms_update(sc, i);
220
221 /*
222 * Hook into the System Monitor.
223 */
224 sc->sc_sysmon.sme_ranges = tms_ranges;
225 sc->sc_sysmon.sme_sensor_info = sc->sc_info;
226 sc->sc_sysmon.sme_sensor_data = sc->sc_data;
227 sc->sc_sysmon.sme_cookie = sc;
228
229 sc->sc_sysmon.sme_gtredata = tms_gtredata;
230 sc->sc_sysmon.sme_streinfo = tms_streinfo;
231
232 sc->sc_sysmon.sme_nsensors = 3;
233 sc->sc_sysmon.sme_envsys_version = 1000;
234
235 if (sysmon_envsys_register(&sc->sc_sysmon))
236 printf("%s: unable to register with sysmon\n",
237 sc->sc_dev.dv_xname);
238 }
239
240 static void
241 tms_update(sc, chan)
242 struct nsclpcsio_softc *sc;
243 int chan;
244 {
245 bus_space_tag_t iot = sc->sc_tms_iot;
246 bus_space_handle_t ioh = sc->sc_tms_ioh;
247 u_int8_t status;
248 int8_t temp, ctemp; /* signed!! */
249
250 bus_space_write_1(iot, ioh, 0x09, chan); /* select */
251
252 status = bus_space_read_1(iot, ioh, 0x0a); /* config/status */
253 if (status & 0x01) {
254 /* enabled */
255 sc->sc_info[chan].validflags = ENVSYS_FVALID;
256 }else {
257 sc->sc_info[chan].validflags = 0;
258 return;
259 }
260
261 /*
262 * If the channel is enabled, it is considered valid.
263 * An "open circuit" might be temporary.
264 */
265 sc->sc_data[chan].validflags = ENVSYS_FVALID;
266 if (status & 0x40) {
267 /*
268 * open circuit
269 * XXX should have a warning for it
270 */
271 sc->sc_data[chan].warnflags = ENVSYS_WARN_OK; /* XXX */
272 return;
273 }
274
275 /* get current temperature in signed degree celsius */
276 temp = bus_space_read_1(iot, ioh, 0x0b);
277 sc->sc_data[chan].cur.data_us = (int)temp * 1000000 + 273150000;
278 sc->sc_data[chan].validflags |= ENVSYS_FCURVALID;
279
280 if (status & 0x0e) { /* any temperature warning? */
281 /*
282 * XXX the chip documentation is a bit fuzzy - it doesn't state
283 * that the hardware OTS output depends on the "overtemp"
284 * warning bit.
285 * It seems the output gets cleared if the warning bit is reset.
286 * This sucks.
287 * The hardware might do something useful with output pins, eg
288 * throttling the CPU, so we must do the comparision in
289 * software, and only reset the bits if the reason is gone.
290 */
291 if (status & 0x02) { /* low limit */
292 sc->sc_data[chan].warnflags = ENVSYS_WARN_UNDER;
293 /* read low limit */
294 ctemp = bus_space_read_1(iot, ioh, 0x0d);
295 if (temp <= ctemp) /* still valid, don't reset */
296 status &= ~0x02;
297 }
298 if (status & 0x04) { /* high limit */
299 sc->sc_data[chan].warnflags = ENVSYS_WARN_OVER;
300 /* read high limit */
301 ctemp = bus_space_read_1(iot, ioh, 0x0c);
302 if (temp >= ctemp) /* still valid, don't reset */
303 status &= ~0x04;
304 }
305 if (status & 0x08) { /* overtemperature */
306 sc->sc_data[chan].warnflags = ENVSYS_WARN_CRITOVER;
307 /* read overtemperature limit */
308 ctemp = bus_space_read_1(iot, ioh, 0x0e);
309 if (temp >= ctemp) /* still valid, don't reset */
310 status &= ~0x08;
311 }
312
313 /* clear outdated warnings */
314 if (status & 0x0e)
315 bus_space_write_1(iot, ioh, 0x0a, status);
316 }
317 }
318
319 static int
320 tms_gtredata(sme, data)
321 struct sysmon_envsys *sme;
322 struct envsys_tre_data *data;
323 {
324 struct nsclpcsio_softc *sc = sme->sme_cookie;
325
326 tms_update(sc, data->sensor);
327
328 *data = sc->sc_data[data->sensor];
329 return (0);
330 }
331
332 static int
333 tms_streinfo(sme, info)
334 struct sysmon_envsys *sme;
335 struct envsys_basic_info *info;
336 {
337 #if 0
338 struct nsclpcsio_softc *sc = sme->sme_cookie;
339 #endif
340 /* XXX Not implemented */
341 info->validflags = 0;
342
343 return (0);
344 }
345