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