nsclpcsio_isa.c revision 1.23 1 1.23 ad /* $NetBSD: nsclpcsio_isa.c,v 1.23 2007/12/05 07:06:51 ad Exp $ */
2 1.1 drochner
3 1.1 drochner /*
4 1.1 drochner * Copyright (c) 2002
5 1.1 drochner * Matthias Drochner. All rights reserved.
6 1.1 drochner *
7 1.1 drochner * Redistribution and use in source and binary forms, with or without
8 1.1 drochner * modification, are permitted provided that the following conditions
9 1.1 drochner * are met:
10 1.1 drochner * 1. Redistributions of source code must retain the above copyright
11 1.1 drochner * notice, this list of conditions, and the following disclaimer.
12 1.1 drochner * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 drochner * notice, this list of conditions and the following disclaimer in the
14 1.1 drochner * documentation and/or other materials provided with the distribution.
15 1.1 drochner *
16 1.1 drochner * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 drochner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 drochner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 drochner * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 drochner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 drochner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 drochner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 drochner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 drochner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 drochner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 drochner * SUCH DAMAGE.
27 1.1 drochner */
28 1.1 drochner
29 1.21 xtraeme /*
30 1.21 xtraeme * National Semiconductor PC87366 LPC Super I/O driver.
31 1.21 xtraeme * Supported logical devices: GPIO, TMS, VLM.
32 1.21 xtraeme */
33 1.21 xtraeme
34 1.1 drochner #include <sys/cdefs.h>
35 1.23 ad __KERNEL_RCSID(0, "$NetBSD: nsclpcsio_isa.c,v 1.23 2007/12/05 07:06:51 ad Exp $");
36 1.1 drochner
37 1.1 drochner #include <sys/param.h>
38 1.1 drochner #include <sys/systm.h>
39 1.1 drochner #include <sys/device.h>
40 1.16 xtraeme #include <sys/mutex.h>
41 1.11 jmcneill #include <sys/gpio.h>
42 1.20 ad #include <sys/bus.h>
43 1.1 drochner
44 1.19 xtraeme /* Don't use gpio for now in the LKM */
45 1.19 xtraeme #ifdef _LKM
46 1.19 xtraeme #undef NGPIO
47 1.19 xtraeme #endif
48 1.19 xtraeme
49 1.1 drochner #include <dev/isa/isareg.h>
50 1.1 drochner #include <dev/isa/isavar.h>
51 1.19 xtraeme
52 1.19 xtraeme #ifndef _LKM
53 1.12 drochner #include "gpio.h"
54 1.19 xtraeme #endif
55 1.12 drochner #if NGPIO > 0
56 1.11 jmcneill #include <dev/gpio/gpiovar.h>
57 1.12 drochner #endif
58 1.1 drochner #include <dev/sysmon/sysmonvar.h>
59 1.1 drochner
60 1.21 xtraeme #define SIO_REG_SID 0x20 /* Super I/O ID */
61 1.21 xtraeme #define SIO_SID_PC87366 0xE9 /* PC87366 is identified by 0xE9.*/
62 1.21 xtraeme
63 1.21 xtraeme #define SIO_REG_SRID 0x27 /* Super I/O Revision */
64 1.21 xtraeme
65 1.21 xtraeme #define SIO_REG_LDN 0x07 /* Logical Device Number */
66 1.21 xtraeme #define SIO_LDN_FDC 0x00 /* Floppy Disk Controller (FDC) */
67 1.21 xtraeme #define SIO_LDN_PP 0x01 /* Parallel Port (PP) */
68 1.21 xtraeme #define SIO_LDN_SP2 0x02 /* Serial Port 2 with IR (SP2) */
69 1.21 xtraeme #define SIO_LDN_SP1 0x03 /* Serial Port 1 (SP1) */
70 1.21 xtraeme #define SIO_LDN_SWC 0x04 /* System Wake-Up Control (SWC) */
71 1.21 xtraeme #define SIO_LDN_KBCM 0x05 /* Mouse Controller (KBC) */
72 1.21 xtraeme #define SIO_LDN_KBCK 0x06 /* Keyboard Controller (KBC) */
73 1.21 xtraeme #define SIO_LDN_GPIO 0x07 /* General-Purpose I/O (GPIO) Ports */
74 1.21 xtraeme #define SIO_LDN_ACB 0x08 /* ACCESS.bus Interface (ACB) */
75 1.21 xtraeme #define SIO_LDN_FSCM 0x09 /* Fan Speed Control and Monitor (FSCM) */
76 1.21 xtraeme #define SIO_LDN_WDT 0x0A /* WATCHDOG Timer (WDT) */
77 1.21 xtraeme #define SIO_LDN_GMP 0x0B /* Game Port (GMP) */
78 1.21 xtraeme #define SIO_LDN_MIDI 0x0C /* Musical Instrument Digital Interface */
79 1.21 xtraeme #define SIO_LDN_VLM 0x0D /* Voltage Level Monitor (VLM) */
80 1.21 xtraeme #define SIO_LDN_TMS 0x0E /* Temperature Sensor (TMS) */
81 1.21 xtraeme
82 1.21 xtraeme #define SIO_REG_ACTIVE 0x30 /* Logical Device Activate Register */
83 1.21 xtraeme #define SIO_ACTIVE_EN 0x01 /* enabled */
84 1.21 xtraeme
85 1.21 xtraeme #define SIO_REG_IO_MSB 0x60 /* I/O Port Base, bits 15-8 */
86 1.21 xtraeme #define SIO_REG_IO_LSB 0x61 /* I/O Port Base, bits 7-0 */
87 1.21 xtraeme
88 1.21 xtraeme #define SIO_LDNUM 15 /* total number of logical devices */
89 1.21 xtraeme
90 1.21 xtraeme /* Supported logical devices description */
91 1.21 xtraeme static const struct {
92 1.21 xtraeme const char *ld_name;
93 1.21 xtraeme int ld_num;
94 1.21 xtraeme int ld_iosize;
95 1.21 xtraeme } sio_ld[] = {
96 1.21 xtraeme { "GPIO", SIO_LDN_GPIO, 16 },
97 1.21 xtraeme { "VLM", SIO_LDN_VLM, 16 },
98 1.21 xtraeme { "TMS", SIO_LDN_TMS, 16 }
99 1.21 xtraeme };
100 1.21 xtraeme
101 1.21 xtraeme /* GPIO */
102 1.21 xtraeme #define SIO_GPIO_PINSEL 0xf0
103 1.21 xtraeme #define SIO_GPIO_PINCFG 0xf1
104 1.21 xtraeme #define SIO_GPIO_PINEV 0xf2
105 1.1 drochner
106 1.11 jmcneill #define SIO_GPIO_CONF_OUTPUTEN (1 << 0)
107 1.11 jmcneill #define SIO_GPIO_CONF_PUSHPULL (1 << 1)
108 1.11 jmcneill #define SIO_GPIO_CONF_PULLUP (1 << 2)
109 1.11 jmcneill
110 1.21 xtraeme #define SIO_GPDO0 0x00
111 1.21 xtraeme #define SIO_GPDI0 0x01
112 1.21 xtraeme #define SIO_GPEVEN0 0x02
113 1.21 xtraeme #define SIO_GPEVST0 0x03
114 1.21 xtraeme #define SIO_GPDO1 0x04
115 1.21 xtraeme #define SIO_GPDI1 0x05
116 1.21 xtraeme #define SIO_GPEVEN1 0x06
117 1.21 xtraeme #define SIO_GPEVST1 0x07
118 1.21 xtraeme #define SIO_GPDO2 0x08
119 1.21 xtraeme #define SIO_GPDI2 0x09
120 1.21 xtraeme #define SIO_GPDO3 0x0a
121 1.21 xtraeme #define SIO_GPDI3 0x0b
122 1.21 xtraeme
123 1.21 xtraeme #define SIO_GPIO_NPINS 29
124 1.21 xtraeme
125 1.21 xtraeme /* TMS */
126 1.21 xtraeme #define SIO_TEVSTS 0x00 /* Temperature Event Status */
127 1.21 xtraeme #define SIO_TEVSMI 0x02 /* Temperature Event to SMI */
128 1.21 xtraeme #define SIO_TEVIRQ 0x04 /* Temperature Event to IRQ */
129 1.21 xtraeme #define SIO_TMSCFG 0x08 /* TMS Configuration */
130 1.21 xtraeme #define SIO_TMSBS 0x09 /* TMS Bank Select */
131 1.21 xtraeme #define SIO_TCHCFST 0x0a /* Temperature Channel Config and Status */
132 1.21 xtraeme #define SIO_RDCHT 0x0b /* Read Channel Temperature */
133 1.21 xtraeme #define SIO_CHTH 0x0c /* Channel Temperature High Limit */
134 1.21 xtraeme #define SIO_CHTL 0x0d /* Channel Temperature Low Limit */
135 1.21 xtraeme #define SIO_CHOTL 0x0e /* Channel Overtemperature Limit */
136 1.21 xtraeme
137 1.21 xtraeme /* VLM */
138 1.21 xtraeme #define SIO_VEVSTS0 0x00 /* Voltage Event Status 0 */
139 1.21 xtraeme #define SIO_VEVSTS1 0x01 /* Voltage Event Status 1 */
140 1.21 xtraeme #define SIO_VEVSMI0 0x02 /* Voltage Event to SMI 0 */
141 1.21 xtraeme #define SIO_VEVSMI1 0x03 /* Voltage Event to SMI 1 */
142 1.21 xtraeme #define SIO_VEVIRQ0 0x04 /* Voltage Event to IRQ 0 */
143 1.21 xtraeme #define SIO_VEVIRQ1 0x05 /* Voltage Event to IRQ 1 */
144 1.21 xtraeme #define SIO_VID 0x06 /* Voltage ID */
145 1.21 xtraeme #define SIO_VCNVR 0x07 /* Voltage Conversion Rate */
146 1.21 xtraeme #define SIO_VLMCFG 0x08 /* VLM Configuration */
147 1.21 xtraeme #define SIO_VLMBS 0x09 /* VLM Bank Select */
148 1.21 xtraeme #define SIO_VCHCFST 0x0a /* Voltage Channel Config and Status */
149 1.21 xtraeme #define SIO_RDCHV 0x0b /* Read Channel Voltage */
150 1.21 xtraeme #define SIO_CHVH 0x0c /* Channel Voltage High Limit */
151 1.21 xtraeme #define SIO_CHVL 0x0d /* Channel Voltage Low Limit */
152 1.21 xtraeme #define SIO_OTSL 0x0e /* Overtemperature Shutdown Limit */
153 1.21 xtraeme
154 1.21 xtraeme #define SIO_REG_SIOCF1 0x21
155 1.21 xtraeme #define SIO_REG_SIOCF2 0x22
156 1.21 xtraeme #define SIO_REG_SIOCF3 0x23
157 1.21 xtraeme #define SIO_REG_SIOCF4 0x24
158 1.21 xtraeme #define SIO_REG_SIOCF5 0x25
159 1.21 xtraeme #define SIO_REG_SIOCF8 0x28
160 1.21 xtraeme #define SIO_REG_SIOCFA 0x2a
161 1.21 xtraeme #define SIO_REG_SIOCFB 0x2b
162 1.21 xtraeme #define SIO_REG_SIOCFC 0x2c
163 1.21 xtraeme #define SIO_REG_SIOCFD 0x2d
164 1.21 xtraeme
165 1.21 xtraeme #define SIO_VLM_OFF 3
166 1.21 xtraeme #define SIO_NUM_SENSORS (SIO_VLM_OFF + 14)
167 1.21 xtraeme #define SIO_VREF 1235 /* 1000.0 * VREF */
168 1.21 xtraeme
169 1.1 drochner struct nsclpcsio_softc {
170 1.1 drochner struct device sc_dev;
171 1.21 xtraeme bus_space_tag_t sc_iot;
172 1.21 xtraeme bus_space_handle_t sc_ioh;
173 1.21 xtraeme
174 1.21 xtraeme bus_space_handle_t sc_ld_ioh[SIO_LDNUM];
175 1.21 xtraeme int sc_ld_en[SIO_LDNUM];
176 1.1 drochner
177 1.21 xtraeme /* TMS and VLM */
178 1.22 xtraeme struct sysmon_envsys *sc_sme;
179 1.21 xtraeme envsys_data_t sc_sensor[SIO_NUM_SENSORS];
180 1.21 xtraeme
181 1.16 xtraeme kmutex_t sc_lock;
182 1.12 drochner #if NGPIO > 0
183 1.11 jmcneill /* GPIO */
184 1.11 jmcneill struct gpio_chipset_tag sc_gpio_gc;
185 1.21 xtraeme struct gpio_pin sc_gpio_pins[SIO_GPIO_NPINS];
186 1.12 drochner #endif
187 1.1 drochner };
188 1.1 drochner
189 1.11 jmcneill #define GPIO_READ(sc, reg) \
190 1.21 xtraeme bus_space_read_1((sc)->sc_iot, \
191 1.21 xtraeme (sc)->sc_ld_ioh[SIO_LDN_GPIO], (reg))
192 1.11 jmcneill #define GPIO_WRITE(sc, reg, val) \
193 1.21 xtraeme bus_space_write_1((sc)->sc_iot, \
194 1.21 xtraeme (sc)->sc_ld_ioh[SIO_LDN_GPIO], (reg), (val))
195 1.21 xtraeme #define TMS_WRITE(sc, reg, val) \
196 1.21 xtraeme bus_space_write_1((sc)->sc_iot, \
197 1.21 xtraeme (sc)->sc_ld_ioh[SIO_LDN_TMS], (reg), (val))
198 1.21 xtraeme #define TMS_READ(sc, reg) \
199 1.21 xtraeme bus_space_read_1((sc)->sc_iot, \
200 1.21 xtraeme (sc)->sc_ld_ioh[SIO_LDN_TMS], (reg))
201 1.21 xtraeme #define VLM_WRITE(sc, reg, val) \
202 1.21 xtraeme bus_space_write_1((sc)->sc_iot, \
203 1.21 xtraeme (sc)->sc_ld_ioh[SIO_LDN_VLM], (reg), (val))
204 1.21 xtraeme #define VLM_READ(sc, reg) \
205 1.21 xtraeme bus_space_read_1((sc)->sc_iot, \
206 1.21 xtraeme (sc)->sc_ld_ioh[SIO_LDN_VLM], (reg))
207 1.21 xtraeme
208 1.21 xtraeme static int nsclpcsio_isa_match(struct device *, struct cfdata *, void *);
209 1.21 xtraeme static void nsclpcsio_isa_attach(struct device *, struct device *, void *);
210 1.21 xtraeme static int nsclpcsio_isa_detach(struct device *, int);
211 1.11 jmcneill
212 1.5 drochner CFATTACH_DECL(nsclpcsio_isa, sizeof(struct nsclpcsio_softc),
213 1.19 xtraeme nsclpcsio_isa_match, nsclpcsio_isa_attach, nsclpcsio_isa_detach, NULL);
214 1.1 drochner
215 1.21 xtraeme static uint8_t nsread(bus_space_tag_t, bus_space_handle_t, int);
216 1.21 xtraeme static void nswrite(bus_space_tag_t, bus_space_handle_t, int, uint8_t);
217 1.1 drochner static int nscheck(bus_space_tag_t, int);
218 1.1 drochner
219 1.21 xtraeme static void nsclpcsio_tms_init(struct nsclpcsio_softc *);
220 1.21 xtraeme static void nsclpcsio_vlm_init(struct nsclpcsio_softc *);
221 1.22 xtraeme static void nsclpcsio_refresh(struct sysmon_envsys *, envsys_data_t *);
222 1.1 drochner
223 1.12 drochner #if NGPIO > 0
224 1.11 jmcneill static void nsclpcsio_gpio_init(struct nsclpcsio_softc *);
225 1.11 jmcneill static void nsclpcsio_gpio_pin_select(struct nsclpcsio_softc *, int);
226 1.11 jmcneill static void nsclpcsio_gpio_pin_write(void *, int, int);
227 1.11 jmcneill static int nsclpcsio_gpio_pin_read(void *, int);
228 1.11 jmcneill static void nsclpcsio_gpio_pin_ctl(void *, int, int);
229 1.12 drochner #endif
230 1.11 jmcneill
231 1.21 xtraeme static uint8_t
232 1.21 xtraeme nsread(bus_space_tag_t iot, bus_space_handle_t ioh, int idx)
233 1.1 drochner {
234 1.1 drochner bus_space_write_1(iot, ioh, 0, idx);
235 1.21 xtraeme return bus_space_read_1(iot, ioh, 1);
236 1.1 drochner }
237 1.1 drochner
238 1.1 drochner static void
239 1.21 xtraeme nswrite(bus_space_tag_t iot, bus_space_handle_t ioh, int idx, uint8_t data)
240 1.1 drochner {
241 1.1 drochner bus_space_write_1(iot, ioh, 0, idx);
242 1.1 drochner bus_space_write_1(iot, ioh, 1, data);
243 1.1 drochner }
244 1.1 drochner
245 1.1 drochner static int
246 1.21 xtraeme nscheck(bus_space_tag_t iot, int base)
247 1.1 drochner {
248 1.1 drochner bus_space_handle_t ioh;
249 1.1 drochner int rv = 0;
250 1.1 drochner
251 1.1 drochner if (bus_space_map(iot, base, 2, 0, &ioh))
252 1.21 xtraeme return 0;
253 1.1 drochner
254 1.1 drochner /* XXX this is for PC87366 only for now */
255 1.21 xtraeme if (nsread(iot, ioh, SIO_REG_SID) == SIO_SID_PC87366)
256 1.1 drochner rv = 1;
257 1.1 drochner
258 1.1 drochner bus_space_unmap(iot, ioh, 2);
259 1.21 xtraeme return rv;
260 1.1 drochner }
261 1.1 drochner
262 1.1 drochner static int
263 1.21 xtraeme nsclpcsio_isa_match(struct device *parent, struct cfdata *match, void *aux)
264 1.1 drochner {
265 1.1 drochner struct isa_attach_args *ia = aux;
266 1.1 drochner int iobase;
267 1.1 drochner
268 1.1 drochner if (ISA_DIRECT_CONFIG(ia))
269 1.21 xtraeme return 0;
270 1.1 drochner
271 1.6 drochner if (ia->ia_nio > 0 && ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT) {
272 1.1 drochner /* XXX check for legal iobase ??? */
273 1.1 drochner if (nscheck(ia->ia_iot, ia->ia_io[0].ir_addr)) {
274 1.1 drochner iobase = ia->ia_io[0].ir_addr;
275 1.1 drochner goto found;
276 1.1 drochner }
277 1.21 xtraeme return 0;
278 1.1 drochner }
279 1.1 drochner
280 1.1 drochner /* PC87366 has two possible locations depending on wiring */
281 1.1 drochner if (nscheck(ia->ia_iot, 0x2e)) {
282 1.1 drochner iobase = 0x2e;
283 1.1 drochner goto found;
284 1.1 drochner }
285 1.1 drochner if (nscheck(ia->ia_iot, 0x4e)) {
286 1.1 drochner iobase = 0x4e;
287 1.1 drochner goto found;
288 1.1 drochner }
289 1.21 xtraeme
290 1.21 xtraeme return 0;
291 1.1 drochner
292 1.1 drochner found:
293 1.1 drochner ia->ia_nio = 1;
294 1.1 drochner ia->ia_io[0].ir_addr = iobase;
295 1.1 drochner ia->ia_io[0].ir_size = 2;
296 1.1 drochner ia->ia_niomem = 0;
297 1.1 drochner ia->ia_nirq = 0;
298 1.1 drochner ia->ia_ndrq = 0;
299 1.21 xtraeme
300 1.21 xtraeme return 1;
301 1.1 drochner }
302 1.1 drochner
303 1.1 drochner static void
304 1.21 xtraeme nsclpcsio_isa_attach(struct device *parent, struct device *self, void *aux)
305 1.1 drochner {
306 1.21 xtraeme struct nsclpcsio_softc *sc = device_private(self);
307 1.1 drochner struct isa_attach_args *ia = aux;
308 1.12 drochner #if NGPIO > 0
309 1.11 jmcneill struct gpiobus_attach_args gba;
310 1.12 drochner #endif
311 1.21 xtraeme int i, iobase;
312 1.1 drochner
313 1.23 ad mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
314 1.11 jmcneill
315 1.21 xtraeme sc->sc_iot = ia->ia_iot;
316 1.21 xtraeme iobase = ia->ia_io[0].ir_addr;
317 1.11 jmcneill
318 1.21 xtraeme if (bus_space_map(ia->ia_iot, iobase, 2, 0, &sc->sc_ioh)) {
319 1.21 xtraeme aprint_error(": can't map i/o space\n");
320 1.11 jmcneill return;
321 1.11 jmcneill }
322 1.11 jmcneill
323 1.21 xtraeme aprint_normal(": NSC PC87366 rev. 0x%d ",
324 1.21 xtraeme nsread(sc->sc_iot, sc->sc_ioh, SIO_REG_SRID));
325 1.11 jmcneill
326 1.21 xtraeme /* Configure all supported logical devices */
327 1.21 xtraeme for (i = 0; i < __arraycount(sio_ld); i++) {
328 1.21 xtraeme sc->sc_ld_en[sio_ld[i].ld_num] = 0;
329 1.21 xtraeme
330 1.21 xtraeme /* Select the device and check if it's activated */
331 1.21 xtraeme nswrite(sc->sc_iot, sc->sc_ioh, SIO_REG_LDN, sio_ld[i].ld_num);
332 1.21 xtraeme if ((nsread(sc->sc_iot, sc->sc_ioh,
333 1.21 xtraeme SIO_REG_ACTIVE) & SIO_ACTIVE_EN) == 0)
334 1.21 xtraeme continue;
335 1.21 xtraeme
336 1.21 xtraeme /* Map I/O space if necessary */
337 1.21 xtraeme if (sio_ld[i].ld_iosize != 0) {
338 1.21 xtraeme iobase = (nsread(sc->sc_iot, sc->sc_ioh,
339 1.21 xtraeme SIO_REG_IO_MSB) << 8);
340 1.21 xtraeme iobase |= nsread(sc->sc_iot, sc->sc_ioh,
341 1.21 xtraeme SIO_REG_IO_LSB);
342 1.21 xtraeme if (bus_space_map(sc->sc_iot, iobase,
343 1.21 xtraeme sio_ld[i].ld_iosize, 0,
344 1.21 xtraeme &sc->sc_ld_ioh[sio_ld[i].ld_num]))
345 1.21 xtraeme continue;
346 1.22 xtraeme }
347 1.21 xtraeme
348 1.21 xtraeme sc->sc_ld_en[sio_ld[i].ld_num] = 1;
349 1.21 xtraeme aprint_normal("%s ", sio_ld[i].ld_name);
350 1.21 xtraeme }
351 1.21 xtraeme
352 1.21 xtraeme aprint_normal("\n");
353 1.21 xtraeme
354 1.21 xtraeme #if NGPIO > 0
355 1.21 xtraeme nsclpcsio_gpio_init(sc);
356 1.21 xtraeme #endif
357 1.21 xtraeme nsclpcsio_tms_init(sc);
358 1.21 xtraeme nsclpcsio_vlm_init(sc);
359 1.1 drochner
360 1.22 xtraeme sc->sc_sme = sysmon_envsys_create();
361 1.22 xtraeme for (i = 0; i < SIO_NUM_SENSORS; i++) {
362 1.22 xtraeme if (sysmon_envsys_sensor_attach(sc->sc_sme,
363 1.22 xtraeme &sc->sc_sensor[i])) {
364 1.22 xtraeme sysmon_envsys_destroy(sc->sc_sme);
365 1.22 xtraeme return;
366 1.22 xtraeme }
367 1.22 xtraeme }
368 1.22 xtraeme
369 1.1 drochner /*
370 1.1 drochner * Hook into the System Monitor.
371 1.1 drochner */
372 1.22 xtraeme sc->sc_sme->sme_name = device_xname(&sc->sc_dev);
373 1.22 xtraeme sc->sc_sme->sme_cookie = sc;
374 1.22 xtraeme sc->sc_sme->sme_refresh = nsclpcsio_refresh;
375 1.1 drochner
376 1.22 xtraeme if (sysmon_envsys_register(sc->sc_sme)) {
377 1.21 xtraeme aprint_error("%s: unable to register with sysmon\n",
378 1.1 drochner sc->sc_dev.dv_xname);
379 1.22 xtraeme sysmon_envsys_destroy(sc->sc_sme);
380 1.22 xtraeme return;
381 1.22 xtraeme }
382 1.22 xtraeme
383 1.11 jmcneill
384 1.12 drochner #if NGPIO > 0
385 1.11 jmcneill /* attach GPIO framework */
386 1.21 xtraeme if (sc->sc_ld_en[SIO_LDN_GPIO]) {
387 1.11 jmcneill gba.gba_gc = &sc->sc_gpio_gc;
388 1.11 jmcneill gba.gba_pins = sc->sc_gpio_pins;
389 1.21 xtraeme gba.gba_npins = SIO_GPIO_NPINS;
390 1.12 drochner config_found_ia(&sc->sc_dev, "gpiobus", &gba, NULL);
391 1.11 jmcneill }
392 1.12 drochner #endif
393 1.1 drochner }
394 1.1 drochner
395 1.19 xtraeme static int
396 1.19 xtraeme nsclpcsio_isa_detach(struct device *self, int flags)
397 1.19 xtraeme {
398 1.19 xtraeme struct nsclpcsio_softc *sc = device_private(self);
399 1.19 xtraeme
400 1.22 xtraeme sysmon_envsys_unregister(sc->sc_sme);
401 1.21 xtraeme mutex_destroy(&sc->sc_lock);
402 1.21 xtraeme
403 1.19 xtraeme bus_space_unmap(sc->sc_iot, sc->sc_ioh, 2);
404 1.21 xtraeme
405 1.19 xtraeme return 0;
406 1.19 xtraeme }
407 1.19 xtraeme
408 1.1 drochner static void
409 1.21 xtraeme nsclpcsio_tms_init(struct nsclpcsio_softc *sc)
410 1.21 xtraeme {
411 1.21 xtraeme int i;
412 1.1 drochner
413 1.21 xtraeme /* Initialisation, PC87366.pdf, page 208 */
414 1.21 xtraeme TMS_WRITE(sc, 0x08, 0x00);
415 1.21 xtraeme TMS_WRITE(sc, 0x09, 0x0f);
416 1.21 xtraeme TMS_WRITE(sc, 0x0a, 0x08);
417 1.21 xtraeme TMS_WRITE(sc, 0x0b, 0x04);
418 1.21 xtraeme TMS_WRITE(sc, 0x0c, 0x35);
419 1.21 xtraeme TMS_WRITE(sc, 0x0d, 0x05);
420 1.21 xtraeme TMS_WRITE(sc, 0x0e, 0x05);
421 1.21 xtraeme
422 1.21 xtraeme TMS_WRITE(sc, SIO_TMSCFG, 0x00);
423 1.21 xtraeme
424 1.21 xtraeme for (i = 0; i < SIO_VLM_OFF; i++) {
425 1.21 xtraeme TMS_WRITE(sc, SIO_TMSBS, i);
426 1.21 xtraeme TMS_WRITE(sc, SIO_TCHCFST, 0x01);
427 1.21 xtraeme sc->sc_sensor[i].units = ENVSYS_STEMP;
428 1.21 xtraeme }
429 1.21 xtraeme
430 1.21 xtraeme #define COPYDESCR(x, y) \
431 1.21 xtraeme do { \
432 1.21 xtraeme (void)strlcpy((x), (y), sizeof(x)); \
433 1.21 xtraeme } while (/* CONSTCOND */ 0)
434 1.21 xtraeme
435 1.21 xtraeme COPYDESCR(sc->sc_sensor[0].desc, "TSENS1");
436 1.21 xtraeme COPYDESCR(sc->sc_sensor[1].desc, "TSENS2");
437 1.21 xtraeme COPYDESCR(sc->sc_sensor[2].desc, "TNSC");
438 1.21 xtraeme }
439 1.21 xtraeme
440 1.21 xtraeme static void
441 1.21 xtraeme nsclpcsio_vlm_init(struct nsclpcsio_softc *sc)
442 1.21 xtraeme {
443 1.21 xtraeme int i;
444 1.21 xtraeme char tmp[16];
445 1.11 jmcneill
446 1.21 xtraeme for (i = SIO_VLM_OFF; i < SIO_NUM_SENSORS; i++) {
447 1.21 xtraeme VLM_WRITE(sc, SIO_VLMBS, i - SIO_VLM_OFF);
448 1.21 xtraeme VLM_WRITE(sc, SIO_VCHCFST, 0x01);
449 1.21 xtraeme sc->sc_sensor[i].units = ENVSYS_SVOLTS_DC;
450 1.21 xtraeme }
451 1.21 xtraeme
452 1.21 xtraeme for (i = 0; i < 7; i++) {
453 1.21 xtraeme (void)snprintf(tmp, sizeof(tmp), "VSENS%d", i);
454 1.21 xtraeme COPYDESCR(sc->sc_sensor[i + SIO_VLM_OFF].desc, tmp);
455 1.21 xtraeme }
456 1.21 xtraeme
457 1.21 xtraeme i += SIO_VLM_OFF;
458 1.21 xtraeme COPYDESCR(sc->sc_sensor[i + 1].desc, "VSB");
459 1.21 xtraeme COPYDESCR(sc->sc_sensor[i + 2].desc, "VDD");
460 1.21 xtraeme COPYDESCR(sc->sc_sensor[i + 3].desc, "VBAT");
461 1.21 xtraeme COPYDESCR(sc->sc_sensor[i + 4].desc, "AVDD");
462 1.21 xtraeme COPYDESCR(sc->sc_sensor[i + 5].desc, "TS1");
463 1.21 xtraeme COPYDESCR(sc->sc_sensor[i + 6].desc, "TS2");
464 1.21 xtraeme COPYDESCR(sc->sc_sensor[i + 7].desc, "TS3");
465 1.21 xtraeme }
466 1.11 jmcneill
467 1.1 drochner
468 1.22 xtraeme static void
469 1.22 xtraeme nsclpcsio_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
470 1.21 xtraeme {
471 1.21 xtraeme struct nsclpcsio_softc *sc = sme->sme_cookie;
472 1.21 xtraeme uint8_t status, data;
473 1.21 xtraeme int8_t sdata = 0;
474 1.21 xtraeme int scale, rfact;
475 1.1 drochner
476 1.21 xtraeme scale = rfact = 0;
477 1.21 xtraeme status = data = 0;
478 1.1 drochner
479 1.21 xtraeme mutex_enter(&sc->sc_lock);
480 1.21 xtraeme /* TMS */
481 1.21 xtraeme if (edata->sensor < SIO_VLM_OFF && sc->sc_ld_en[SIO_LDN_TMS]) {
482 1.21 xtraeme TMS_WRITE(sc, SIO_TMSBS, edata->sensor);
483 1.21 xtraeme status = TMS_READ(sc, SIO_TCHCFST);
484 1.21 xtraeme if (!(status & 0x01))
485 1.21 xtraeme edata->state = ENVSYS_SINVALID;
486 1.21 xtraeme
487 1.21 xtraeme sdata = TMS_READ(sc, SIO_RDCHT);
488 1.21 xtraeme edata->value_cur = sdata * 1000000 + 273150000;
489 1.21 xtraeme edata->state = ENVSYS_SVALID;
490 1.21 xtraeme /* VLM */
491 1.21 xtraeme } else if (edata->sensor >= SIO_VLM_OFF &&
492 1.21 xtraeme edata->sensor < SIO_NUM_SENSORS &&
493 1.21 xtraeme sc->sc_ld_en[SIO_LDN_VLM]) {
494 1.21 xtraeme VLM_WRITE(sc, SIO_VLMBS, edata->sensor - SIO_VLM_OFF);
495 1.21 xtraeme status = VLM_READ(sc, SIO_VCHCFST);
496 1.21 xtraeme if (!(status & 0x01)) {
497 1.21 xtraeme edata->state = ENVSYS_SINVALID;
498 1.21 xtraeme } else {
499 1.21 xtraeme data = VLM_READ(sc, SIO_RDCHV);
500 1.21 xtraeme scale = 1;
501 1.21 xtraeme switch (edata->sensor - SIO_VLM_OFF) {
502 1.21 xtraeme case 7:
503 1.21 xtraeme case 8:
504 1.21 xtraeme case 10:
505 1.21 xtraeme scale = 2;
506 1.21 xtraeme break;
507 1.21 xtraeme }
508 1.21 xtraeme /* Vi = (2.450.05)*VREF *RDCHVi / 256 */
509 1.21 xtraeme rfact = 10 * scale * ((245 * SIO_VREF) >> 8);
510 1.21 xtraeme edata->value_cur = data * rfact;
511 1.21 xtraeme edata->state = ENVSYS_SVALID;
512 1.1 drochner }
513 1.1 drochner }
514 1.16 xtraeme mutex_exit(&sc->sc_lock);
515 1.1 drochner }
516 1.11 jmcneill
517 1.12 drochner #if NGPIO > 0
518 1.11 jmcneill static void
519 1.11 jmcneill nsclpcsio_gpio_pin_select(struct nsclpcsio_softc *sc, int pin)
520 1.11 jmcneill {
521 1.21 xtraeme uint8_t v;
522 1.11 jmcneill
523 1.11 jmcneill v = ((pin / 8) << 4) | (pin % 8);
524 1.11 jmcneill
525 1.21 xtraeme nswrite(sc->sc_iot, sc->sc_ioh, SIO_REG_LDN, SIO_LDN_GPIO);
526 1.21 xtraeme nswrite(sc->sc_iot, sc->sc_ioh, SIO_GPIO_PINSEL, v);
527 1.11 jmcneill }
528 1.11 jmcneill
529 1.11 jmcneill static void
530 1.11 jmcneill nsclpcsio_gpio_init(struct nsclpcsio_softc *sc)
531 1.11 jmcneill {
532 1.11 jmcneill int i;
533 1.11 jmcneill
534 1.21 xtraeme for (i = 0; i < SIO_GPIO_NPINS; i++) {
535 1.11 jmcneill sc->sc_gpio_pins[i].pin_num = i;
536 1.11 jmcneill sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
537 1.11 jmcneill GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN |
538 1.11 jmcneill GPIO_PIN_PUSHPULL | GPIO_PIN_TRISTATE |
539 1.11 jmcneill GPIO_PIN_PULLUP;
540 1.11 jmcneill /* safe defaults */
541 1.11 jmcneill sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_TRISTATE;
542 1.11 jmcneill sc->sc_gpio_pins[i].pin_state = GPIO_PIN_LOW;
543 1.11 jmcneill nsclpcsio_gpio_pin_ctl(sc, i, sc->sc_gpio_pins[i].pin_flags);
544 1.11 jmcneill nsclpcsio_gpio_pin_write(sc, i, sc->sc_gpio_pins[i].pin_state);
545 1.11 jmcneill }
546 1.11 jmcneill
547 1.11 jmcneill /* create controller tag */
548 1.11 jmcneill sc->sc_gpio_gc.gp_cookie = sc;
549 1.11 jmcneill sc->sc_gpio_gc.gp_pin_read = nsclpcsio_gpio_pin_read;
550 1.11 jmcneill sc->sc_gpio_gc.gp_pin_write = nsclpcsio_gpio_pin_write;
551 1.11 jmcneill sc->sc_gpio_gc.gp_pin_ctl = nsclpcsio_gpio_pin_ctl;
552 1.11 jmcneill }
553 1.11 jmcneill
554 1.11 jmcneill static int
555 1.11 jmcneill nsclpcsio_gpio_pin_read(void *aux, int pin)
556 1.11 jmcneill {
557 1.11 jmcneill struct nsclpcsio_softc *sc = (struct nsclpcsio_softc *)aux;
558 1.11 jmcneill int port, shift, reg;
559 1.21 xtraeme uint8_t v;
560 1.11 jmcneill
561 1.11 jmcneill port = pin / 8;
562 1.11 jmcneill shift = pin % 8;
563 1.11 jmcneill
564 1.11 jmcneill switch (port) {
565 1.21 xtraeme case 0:
566 1.21 xtraeme reg = SIO_GPDI0;
567 1.21 xtraeme break;
568 1.21 xtraeme case 1:
569 1.21 xtraeme reg = SIO_GPDI1;
570 1.21 xtraeme break;
571 1.21 xtraeme case 2:
572 1.21 xtraeme reg = SIO_GPDI2;
573 1.21 xtraeme break;
574 1.21 xtraeme case 3:
575 1.21 xtraeme reg = SIO_GPDI3;
576 1.21 xtraeme break;
577 1.21 xtraeme default:
578 1.21 xtraeme reg = SIO_GPDI0;
579 1.21 xtraeme break;
580 1.11 jmcneill }
581 1.11 jmcneill
582 1.11 jmcneill v = GPIO_READ(sc, reg);
583 1.11 jmcneill
584 1.11 jmcneill return ((v >> shift) & 0x1);
585 1.11 jmcneill }
586 1.11 jmcneill
587 1.11 jmcneill static void
588 1.11 jmcneill nsclpcsio_gpio_pin_write(void *aux, int pin, int v)
589 1.11 jmcneill {
590 1.11 jmcneill struct nsclpcsio_softc *sc = (struct nsclpcsio_softc *)aux;
591 1.11 jmcneill int port, shift, reg;
592 1.21 xtraeme uint8_t d;
593 1.11 jmcneill
594 1.11 jmcneill port = pin / 8;
595 1.11 jmcneill shift = pin % 8;
596 1.11 jmcneill
597 1.11 jmcneill switch (port) {
598 1.21 xtraeme case 0:
599 1.21 xtraeme reg = SIO_GPDO0;
600 1.21 xtraeme break;
601 1.21 xtraeme case 1:
602 1.21 xtraeme reg = SIO_GPDO1;
603 1.21 xtraeme break;
604 1.21 xtraeme case 2:
605 1.21 xtraeme reg = SIO_GPDO2;
606 1.21 xtraeme break;
607 1.21 xtraeme case 3:
608 1.21 xtraeme reg = SIO_GPDO3;
609 1.21 xtraeme break;
610 1.21 xtraeme default:
611 1.21 xtraeme reg = SIO_GPDO0;
612 1.21 xtraeme break; /* shouldn't happen */
613 1.11 jmcneill }
614 1.11 jmcneill
615 1.11 jmcneill d = GPIO_READ(sc, reg);
616 1.11 jmcneill if (v == 0)
617 1.11 jmcneill d &= ~(1 << shift);
618 1.11 jmcneill else if (v == 1)
619 1.11 jmcneill d |= (1 << shift);
620 1.11 jmcneill GPIO_WRITE(sc, reg, d);
621 1.11 jmcneill }
622 1.11 jmcneill
623 1.11 jmcneill void
624 1.11 jmcneill nsclpcsio_gpio_pin_ctl(void *aux, int pin, int flags)
625 1.11 jmcneill {
626 1.11 jmcneill struct nsclpcsio_softc *sc = (struct nsclpcsio_softc *)aux;
627 1.21 xtraeme uint8_t conf;
628 1.11 jmcneill
629 1.16 xtraeme mutex_enter(&sc->sc_lock);
630 1.11 jmcneill
631 1.21 xtraeme nswrite(sc->sc_iot, sc->sc_ioh, SIO_REG_LDN, SIO_LDN_GPIO);
632 1.11 jmcneill nsclpcsio_gpio_pin_select(sc, pin);
633 1.21 xtraeme conf = nsread(sc->sc_iot, sc->sc_ioh, SIO_GPIO_PINCFG);
634 1.11 jmcneill
635 1.11 jmcneill conf &= ~(SIO_GPIO_CONF_OUTPUTEN | SIO_GPIO_CONF_PUSHPULL |
636 1.11 jmcneill SIO_GPIO_CONF_PULLUP);
637 1.11 jmcneill if ((flags & GPIO_PIN_TRISTATE) == 0)
638 1.11 jmcneill conf |= SIO_GPIO_CONF_OUTPUTEN;
639 1.11 jmcneill if (flags & GPIO_PIN_PUSHPULL)
640 1.11 jmcneill conf |= SIO_GPIO_CONF_PUSHPULL;
641 1.11 jmcneill if (flags & GPIO_PIN_PULLUP)
642 1.11 jmcneill conf |= SIO_GPIO_CONF_PULLUP;
643 1.11 jmcneill
644 1.21 xtraeme nswrite(sc->sc_iot, sc->sc_ioh, SIO_GPIO_PINCFG, conf);
645 1.11 jmcneill
646 1.16 xtraeme mutex_exit(&sc->sc_lock);
647 1.11 jmcneill }
648 1.12 drochner #endif /* NGPIO */
649