xref: /netbsd-src/sys/dev/isa/smsc.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: smsc.c,v 1.6 2007/11/17 08:30:35 kefren Exp $ */
2 
3 /*-
4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Brett Lymn.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * This is a driver for the Standard Microsystems Corp (SMSC)
41  * LPC47B397 "super i/o" chip.  This driver only handles the environment
42  * monitoring capabilities of the chip, the other functions will be
43  * probed/matched as "normal" PC hardware devices (serial ports, fdc, so on).
44  * SMSC has not deigned to release a datasheet for this particular chip
45  * (though they do for others they make) so this driver was written from
46  * information contained in the comment block for the Linux driver.
47  */
48 
49 #include <sys/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: smsc.c,v 1.6 2007/11/17 08:30:35 kefren Exp $");
51 
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/proc.h>
57 #include <sys/device.h>
58 #include <sys/conf.h>
59 #include <sys/time.h>
60 
61 #include <sys/bus.h>
62 
63 #include <dev/isa/isareg.h>
64 #include <dev/isa/isavar.h>
65 
66 #include <dev/sysmon/sysmonvar.h>
67 
68 #include <dev/isa/smscvar.h>
69 
70 #include <sys/intr.h>
71 
72 #if defined(LMDEBUG)
73 #define DPRINTF(x)	do { printf x; } while (0)
74 #else
75 #define DPRINTF(x)
76 #endif
77 
78 static int	smsc_match(struct device *, struct cfdata *, void *);
79 static void	smsc_attach(struct device *, struct device *, void *);
80 static int 	smsc_detach(struct device *, int);
81 static uint8_t	smsc_readreg(struct smsc_softc *, int);
82 /*static void smsc_writereg(struct smsc_softc *, int, int);*/
83 
84 static void 	smsc_refresh(struct sysmon_envsys *, envsys_data_t *);
85 
86 CFATTACH_DECL(smsc, sizeof(struct smsc_softc),
87     smsc_match, smsc_attach, smsc_detach, NULL);
88 
89 /*
90  * Probe for the SMSC Super I/O chip
91  */
92 static int
93 smsc_match(struct device *parent, struct cfdata *match, void *aux)
94 {
95 	bus_space_handle_t ioh;
96 	struct isa_attach_args *ia = aux;
97 	int rv;
98 	uint8_t cr;
99 
100 	/* Must supply an address */
101 	if (ia->ia_nio < 1)
102 		return 0;
103 
104 	if (ISA_DIRECT_CONFIG(ia))
105 		return 0;
106 
107 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
108 		return 0;
109 
110 	if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr, 2, 0, &ioh))
111 		return 0;
112 
113 	/* To get the device ID we must enter config mode... */
114 	bus_space_write_1(ia->ia_iot, ioh, SMSC_ADDR, SMSC_CONFIG_START);
115 
116 	/* Then select the device id register */
117 	bus_space_write_1(ia->ia_iot, ioh, SMSC_ADDR, SMSC_DEVICE_ID);
118 
119 	/* Finally, read the id from the chip */
120 	cr = bus_space_read_1(ia->ia_iot, ioh, SMSC_DATA);
121 
122 	/* Exit config mode, apparently this is important to do */
123 	bus_space_write_1(ia->ia_iot, ioh, SMSC_ADDR, SMSC_CONFIG_END);
124 
125 	if (cr == SMSC_ID)
126 		rv = 1;
127 	else
128 		rv = 0;
129 
130 	DPRINTF(("smsc: rv = %d, cr = %x\n", rv, cr));
131 
132 	bus_space_unmap(ia->ia_iot, ioh, 2);
133 
134 	if (rv) {
135 		ia->ia_nio = 1;
136 		ia->ia_io[0].ir_size = 2;
137 
138 		ia->ia_niomem = 0;
139 		ia->ia_nirq = 0;
140 		ia->ia_ndrq = 0;
141 	}
142 
143 	return rv;
144 }
145 
146 /*
147  * Get the base address for the monitoring registers and set up the
148  * env sysmon framework.
149  */
150 static void
151 smsc_attach(struct device *parent, struct device *self, void *aux)
152 {
153 	struct smsc_softc *sc = device_private(self);
154 	struct isa_attach_args *ia = aux;
155 	bus_space_handle_t ioh;
156 	uint8_t rev, msb, lsb;
157 	unsigned address;
158 	char label[8];
159 	int i;
160 
161 	sc->sc_iot = ia->ia_iot;
162 
163 	/* To attach we need to find the actual i/o register space,
164 	   map the base registers in first. */
165 	if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr, 2, 0,
166 	    &ioh)) {
167 		aprint_error(": can't map base i/o space\n");
168 		return;
169 	}
170 
171 	/* Enter config mode. */
172 	bus_space_write_1(ia->ia_iot, ioh, SMSC_ADDR, SMSC_CONFIG_START);
173 
174 	/* While we have the base registers mapped, grab the chip revision */
175 	bus_space_write_1(ia->ia_iot, ioh, SMSC_ADDR, SMSC_DEVICE_REVISION);
176 	rev = bus_space_read_1(ia->ia_iot, ioh, SMSC_DATA);
177 
178 	/* Select the correct logical device */
179 	bus_space_write_1(ia->ia_iot, ioh, SMSC_ADDR, SMSC_LOGICAL_DEV_SEL);
180 	bus_space_write_1(ia->ia_iot, ioh, SMSC_DATA, SMSC_LOGICAL_DEVICE);
181 
182 	/* Read the base address for the registers. */
183 	bus_space_write_1(ia->ia_iot, ioh, SMSC_ADDR, SMSC_IO_BASE_MSB);
184 	msb = bus_space_read_1(ia->ia_iot, ioh, SMSC_DATA);
185 	bus_space_write_1(ia->ia_iot, ioh, SMSC_ADDR, SMSC_IO_BASE_LSB);
186 	lsb = bus_space_read_1(ia->ia_iot, ioh, SMSC_DATA);
187 	address = (msb << 8) | lsb;
188 
189 	/* Exit config mode */
190 	bus_space_write_1(ia->ia_iot, ioh, SMSC_ADDR, SMSC_CONFIG_END);
191 
192 	bus_space_unmap(ia->ia_iot, ioh, 2);
193 
194 	/* Map the i/o space for the registers. */
195 	if (bus_space_map(ia->ia_iot, address, 2, 0, &sc->sc_ioh)) {
196 		aprint_error(": can't map register i/o space\n");
197 		return;
198 	}
199 
200 	sc->sc_sme = sysmon_envsys_create();
201 
202 	for (i = 0; i < 4; i++) {
203 		sprintf(label, "Temp-%d", i);
204 		strlcpy(sc->sc_sensor[i].desc, label,
205 		    sizeof(sc->sc_sensor[i].desc));
206 		sc->sc_sensor[i].units = ENVSYS_STEMP;
207 		switch (i) {
208 		case 0:
209 			sc->sc_regs[i] = SMSC_TEMP1;
210 			break;
211 
212 		case 1:
213 			sc->sc_regs[i] = SMSC_TEMP2;
214 			break;
215 
216 		case 2:
217 			sc->sc_regs[i] = SMSC_TEMP3;
218 			break;
219 
220 		case 3:
221 			sc->sc_regs[i] = SMSC_TEMP4;
222 			break;
223 		}
224 		if (sysmon_envsys_sensor_attach(sc->sc_sme,
225 						&sc->sc_sensor[i])) {
226 			sysmon_envsys_destroy(sc->sc_sme);
227 			return;
228 		}
229 
230 	}
231 
232 	for (i = 4; i < SMSC_MAX_SENSORS; i++) {
233 		sprintf(label, "Fan-%d", i - 3);
234 		strlcpy(sc->sc_sensor[i].desc, label,
235 		    sizeof(sc->sc_sensor[i].desc));
236 		sc->sc_sensor[i].units = ENVSYS_SFANRPM;
237 		switch (i) {
238 		case 4:
239 			sc->sc_regs[i] = SMSC_FAN1_LSB;
240 			break;
241 
242 		case 5:
243 			sc->sc_regs[i] = SMSC_FAN2_LSB;
244 			break;
245 
246 		case 6:
247 			sc->sc_regs[i] = SMSC_FAN3_LSB;
248 			break;
249 
250 		case 7:
251 			sc->sc_regs[i] = SMSC_FAN4_LSB;
252 			break;
253 
254 		default:
255 			aprint_error(": more fans than expected");
256 			break;
257 		}
258 		if (sysmon_envsys_sensor_attach(sc->sc_sme,
259 						&sc->sc_sensor[i])) {
260 			sysmon_envsys_destroy(sc->sc_sme);
261 			return;
262 		}
263 	}
264 
265 	sc->sc_sme->sme_name = sc->sc_dev.dv_xname;
266 	sc->sc_sme->sme_cookie = sc;
267 	sc->sc_sme->sme_refresh = smsc_refresh;
268 
269 	if ((i = sysmon_envsys_register(sc->sc_sme)) != 0) {
270 		aprint_error("%s: unable to register with sysmon (%d)\n",
271 		    sc->sc_dev.dv_xname, i);
272 		sysmon_envsys_destroy(sc->sc_sme);
273 	}
274 
275 	aprint_normal(": monitor registers at 0x%04x  (rev. %u)\n",
276 	       address, rev);
277 }
278 
279 static int
280 smsc_detach(struct device *self, int flags)
281 {
282 	struct smsc_softc *sc = device_private(self);
283 
284 	sysmon_envsys_unregister(sc->sc_sme);
285 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, 2);
286 	return 0;
287 }
288 
289 /*
290  * Read the value of the given register
291  */
292 static uint8_t
293 smsc_readreg(struct smsc_softc *sc, int reg)
294 {
295 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, SMSC_ADDR, reg);
296 	return bus_space_read_1(sc->sc_iot, sc->sc_ioh, SMSC_DATA);
297 }
298 
299 
300 /*
301  * Write the given value to the given register - here just for completeness
302  * it is unused in the current code.
303 
304 static void
305 smsc_writereg (struct smsc_softc *sc, int reg, int val)
306 {
307 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, SMSC_ADDR, reg);
308 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, SMSC_DATA, val);
309 }
310 */
311 
312 /* convert temperature read from the chip to micro kelvin */
313 static inline int
314 smsc_temp2muk(uint8_t t)
315 {
316         int temp=t;
317 
318         return temp * 1000000 + 273150000U;
319 }
320 
321 /*
322  * convert register value read from chip into rpm using:
323  *
324  * RPM = 60/(Count * 11.111us)
325  *
326  * 1/1.1111us = 90kHz
327  *
328  */
329 static inline int
330 smsc_reg2rpm(unsigned int r)
331 {
332 	unsigned long rpm;
333 
334         if (r == 0x0)
335                 return 0;
336 
337 	rpm = (90000 * 60) / ((unsigned long) r);
338         return (int) rpm;
339 }
340 
341 /* min and max temperatures in uK */
342 #define SMSC_MIN_TEMP_UK ((-127 * 1000000) + 273150000)
343 #define SMSC_MAX_TEMP_UK ((127 * 1000000) + 273150000)
344 
345 /*
346  * Get the data for the requested sensor, update the sysmon structure
347  * with the retrieved value.
348  */
349 static void
350 smsc_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
351 {
352 	struct smsc_softc *sc = sme->sme_cookie;
353 	int reg;
354 	unsigned int rpm;
355 	uint8_t msb, lsb;
356 
357 	reg = sc->sc_regs[edata->sensor];
358 
359 	switch (edata->units) {
360 	case ENVSYS_STEMP:
361 		edata->value_cur = smsc_temp2muk(smsc_readreg(sc, reg));
362 		break;
363 
364 	case ENVSYS_SFANRPM:
365 		/* reading lsb first locks msb... */
366 		lsb = smsc_readreg(sc, reg);
367 		msb = smsc_readreg(sc, reg + 1); /* XXX note explicit
368 						    assumption that msb is
369 						    the next register along */
370 		rpm = (msb << 8) | lsb;
371 		edata->value_cur = smsc_reg2rpm(rpm);
372 		break;
373 	}
374 	edata->state = ENVSYS_SVALID;
375 }
376