nsclpcsio_isa.c revision 1.21 1 1.21 xtraeme /* $NetBSD: nsclpcsio_isa.c,v 1.21 2007/11/09 01:09:47 xtraeme 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.21 xtraeme __KERNEL_RCSID(0, "$NetBSD: nsclpcsio_isa.c,v 1.21 2007/11/09 01:09:47 xtraeme 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.1 drochner struct sysmon_envsys sc_sysmon;
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.21 xtraeme static int nsclpcsio_gtredata(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.16 xtraeme mutex_init(&sc->sc_lock, MUTEX_DRIVER, 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.21 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 for (i = 0; i < SIO_NUM_SENSORS; i++) {
355 1.21 xtraeme sc->sc_sensor[i].sensor = i;
356 1.21 xtraeme sc->sc_sensor[i].state = ENVSYS_SVALID;
357 1.1 drochner }
358 1.1 drochner
359 1.21 xtraeme #if NGPIO > 0
360 1.21 xtraeme nsclpcsio_gpio_init(sc);
361 1.21 xtraeme #endif
362 1.21 xtraeme nsclpcsio_tms_init(sc);
363 1.21 xtraeme nsclpcsio_vlm_init(sc);
364 1.1 drochner
365 1.1 drochner /*
366 1.1 drochner * Hook into the System Monitor.
367 1.1 drochner */
368 1.21 xtraeme sc->sc_sysmon.sme_name = device_xname(&sc->sc_dev);
369 1.21 xtraeme sc->sc_sysmon.sme_sensor_data = sc->sc_sensor;
370 1.1 drochner sc->sc_sysmon.sme_cookie = sc;
371 1.21 xtraeme sc->sc_sysmon.sme_gtredata = nsclpcsio_gtredata;
372 1.21 xtraeme sc->sc_sysmon.sme_nsensors = SIO_NUM_SENSORS;
373 1.1 drochner
374 1.1 drochner if (sysmon_envsys_register(&sc->sc_sysmon))
375 1.21 xtraeme aprint_error("%s: unable to register with sysmon\n",
376 1.1 drochner sc->sc_dev.dv_xname);
377 1.11 jmcneill
378 1.12 drochner #if NGPIO > 0
379 1.11 jmcneill /* attach GPIO framework */
380 1.21 xtraeme if (sc->sc_ld_en[SIO_LDN_GPIO]) {
381 1.11 jmcneill gba.gba_gc = &sc->sc_gpio_gc;
382 1.11 jmcneill gba.gba_pins = sc->sc_gpio_pins;
383 1.21 xtraeme gba.gba_npins = SIO_GPIO_NPINS;
384 1.12 drochner config_found_ia(&sc->sc_dev, "gpiobus", &gba, NULL);
385 1.11 jmcneill }
386 1.12 drochner #endif
387 1.1 drochner }
388 1.1 drochner
389 1.19 xtraeme static int
390 1.19 xtraeme nsclpcsio_isa_detach(struct device *self, int flags)
391 1.19 xtraeme {
392 1.19 xtraeme struct nsclpcsio_softc *sc = device_private(self);
393 1.19 xtraeme
394 1.19 xtraeme sysmon_envsys_unregister(&sc->sc_sysmon);
395 1.21 xtraeme mutex_destroy(&sc->sc_lock);
396 1.21 xtraeme
397 1.19 xtraeme bus_space_unmap(sc->sc_iot, sc->sc_ioh, 2);
398 1.21 xtraeme
399 1.19 xtraeme return 0;
400 1.19 xtraeme }
401 1.19 xtraeme
402 1.1 drochner static void
403 1.21 xtraeme nsclpcsio_tms_init(struct nsclpcsio_softc *sc)
404 1.21 xtraeme {
405 1.21 xtraeme int i;
406 1.1 drochner
407 1.21 xtraeme /* Initialisation, PC87366.pdf, page 208 */
408 1.21 xtraeme TMS_WRITE(sc, 0x08, 0x00);
409 1.21 xtraeme TMS_WRITE(sc, 0x09, 0x0f);
410 1.21 xtraeme TMS_WRITE(sc, 0x0a, 0x08);
411 1.21 xtraeme TMS_WRITE(sc, 0x0b, 0x04);
412 1.21 xtraeme TMS_WRITE(sc, 0x0c, 0x35);
413 1.21 xtraeme TMS_WRITE(sc, 0x0d, 0x05);
414 1.21 xtraeme TMS_WRITE(sc, 0x0e, 0x05);
415 1.21 xtraeme
416 1.21 xtraeme TMS_WRITE(sc, SIO_TMSCFG, 0x00);
417 1.21 xtraeme
418 1.21 xtraeme for (i = 0; i < SIO_VLM_OFF; i++) {
419 1.21 xtraeme TMS_WRITE(sc, SIO_TMSBS, i);
420 1.21 xtraeme TMS_WRITE(sc, SIO_TCHCFST, 0x01);
421 1.21 xtraeme sc->sc_sensor[i].units = ENVSYS_STEMP;
422 1.21 xtraeme }
423 1.21 xtraeme
424 1.21 xtraeme #define COPYDESCR(x, y) \
425 1.21 xtraeme do { \
426 1.21 xtraeme (void)strlcpy((x), (y), sizeof(x)); \
427 1.21 xtraeme } while (/* CONSTCOND */ 0)
428 1.21 xtraeme
429 1.21 xtraeme COPYDESCR(sc->sc_sensor[0].desc, "TSENS1");
430 1.21 xtraeme COPYDESCR(sc->sc_sensor[1].desc, "TSENS2");
431 1.21 xtraeme COPYDESCR(sc->sc_sensor[2].desc, "TNSC");
432 1.21 xtraeme }
433 1.21 xtraeme
434 1.21 xtraeme static void
435 1.21 xtraeme nsclpcsio_vlm_init(struct nsclpcsio_softc *sc)
436 1.21 xtraeme {
437 1.21 xtraeme int i;
438 1.21 xtraeme char tmp[16];
439 1.11 jmcneill
440 1.21 xtraeme for (i = SIO_VLM_OFF; i < SIO_NUM_SENSORS; i++) {
441 1.21 xtraeme VLM_WRITE(sc, SIO_VLMBS, i - SIO_VLM_OFF);
442 1.21 xtraeme VLM_WRITE(sc, SIO_VCHCFST, 0x01);
443 1.21 xtraeme sc->sc_sensor[i].units = ENVSYS_SVOLTS_DC;
444 1.21 xtraeme }
445 1.21 xtraeme
446 1.21 xtraeme for (i = 0; i < 7; i++) {
447 1.21 xtraeme (void)snprintf(tmp, sizeof(tmp), "VSENS%d", i);
448 1.21 xtraeme COPYDESCR(sc->sc_sensor[i + SIO_VLM_OFF].desc, tmp);
449 1.21 xtraeme }
450 1.21 xtraeme
451 1.21 xtraeme i += SIO_VLM_OFF;
452 1.21 xtraeme COPYDESCR(sc->sc_sensor[i + 1].desc, "VSB");
453 1.21 xtraeme COPYDESCR(sc->sc_sensor[i + 2].desc, "VDD");
454 1.21 xtraeme COPYDESCR(sc->sc_sensor[i + 3].desc, "VBAT");
455 1.21 xtraeme COPYDESCR(sc->sc_sensor[i + 4].desc, "AVDD");
456 1.21 xtraeme COPYDESCR(sc->sc_sensor[i + 5].desc, "TS1");
457 1.21 xtraeme COPYDESCR(sc->sc_sensor[i + 6].desc, "TS2");
458 1.21 xtraeme COPYDESCR(sc->sc_sensor[i + 7].desc, "TS3");
459 1.21 xtraeme }
460 1.11 jmcneill
461 1.1 drochner
462 1.21 xtraeme static int
463 1.21 xtraeme nsclpcsio_gtredata(struct sysmon_envsys *sme, envsys_data_t *edata)
464 1.21 xtraeme {
465 1.21 xtraeme struct nsclpcsio_softc *sc = sme->sme_cookie;
466 1.21 xtraeme uint8_t status, data;
467 1.21 xtraeme int8_t sdata = 0;
468 1.21 xtraeme int scale, rfact;
469 1.1 drochner
470 1.21 xtraeme scale = rfact = 0;
471 1.21 xtraeme status = data = 0;
472 1.1 drochner
473 1.21 xtraeme mutex_enter(&sc->sc_lock);
474 1.21 xtraeme /* TMS */
475 1.21 xtraeme if (edata->sensor < SIO_VLM_OFF && sc->sc_ld_en[SIO_LDN_TMS]) {
476 1.21 xtraeme TMS_WRITE(sc, SIO_TMSBS, edata->sensor);
477 1.21 xtraeme status = TMS_READ(sc, SIO_TCHCFST);
478 1.21 xtraeme if (!(status & 0x01))
479 1.21 xtraeme edata->state = ENVSYS_SINVALID;
480 1.21 xtraeme
481 1.21 xtraeme sdata = TMS_READ(sc, SIO_RDCHT);
482 1.21 xtraeme edata->value_cur = sdata * 1000000 + 273150000;
483 1.21 xtraeme edata->state = ENVSYS_SVALID;
484 1.21 xtraeme /* VLM */
485 1.21 xtraeme } else if (edata->sensor >= SIO_VLM_OFF &&
486 1.21 xtraeme edata->sensor < SIO_NUM_SENSORS &&
487 1.21 xtraeme sc->sc_ld_en[SIO_LDN_VLM]) {
488 1.21 xtraeme VLM_WRITE(sc, SIO_VLMBS, edata->sensor - SIO_VLM_OFF);
489 1.21 xtraeme status = VLM_READ(sc, SIO_VCHCFST);
490 1.21 xtraeme if (!(status & 0x01)) {
491 1.21 xtraeme edata->state = ENVSYS_SINVALID;
492 1.21 xtraeme } else {
493 1.21 xtraeme data = VLM_READ(sc, SIO_RDCHV);
494 1.21 xtraeme scale = 1;
495 1.21 xtraeme switch (edata->sensor - SIO_VLM_OFF) {
496 1.21 xtraeme case 7:
497 1.21 xtraeme case 8:
498 1.21 xtraeme case 10:
499 1.21 xtraeme scale = 2;
500 1.21 xtraeme break;
501 1.21 xtraeme }
502 1.21 xtraeme /* Vi = (2.450.05)*VREF *RDCHVi / 256 */
503 1.21 xtraeme rfact = 10 * scale * ((245 * SIO_VREF) >> 8);
504 1.21 xtraeme edata->value_cur = data * rfact;
505 1.21 xtraeme edata->state = ENVSYS_SVALID;
506 1.1 drochner }
507 1.1 drochner }
508 1.16 xtraeme mutex_exit(&sc->sc_lock);
509 1.11 jmcneill
510 1.21 xtraeme return 0;
511 1.1 drochner }
512 1.11 jmcneill
513 1.12 drochner #if NGPIO > 0
514 1.11 jmcneill static void
515 1.11 jmcneill nsclpcsio_gpio_pin_select(struct nsclpcsio_softc *sc, int pin)
516 1.11 jmcneill {
517 1.21 xtraeme uint8_t v;
518 1.11 jmcneill
519 1.11 jmcneill v = ((pin / 8) << 4) | (pin % 8);
520 1.11 jmcneill
521 1.21 xtraeme nswrite(sc->sc_iot, sc->sc_ioh, SIO_REG_LDN, SIO_LDN_GPIO);
522 1.21 xtraeme nswrite(sc->sc_iot, sc->sc_ioh, SIO_GPIO_PINSEL, v);
523 1.11 jmcneill }
524 1.11 jmcneill
525 1.11 jmcneill static void
526 1.11 jmcneill nsclpcsio_gpio_init(struct nsclpcsio_softc *sc)
527 1.11 jmcneill {
528 1.11 jmcneill int i;
529 1.11 jmcneill
530 1.21 xtraeme for (i = 0; i < SIO_GPIO_NPINS; i++) {
531 1.11 jmcneill sc->sc_gpio_pins[i].pin_num = i;
532 1.11 jmcneill sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
533 1.11 jmcneill GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN |
534 1.11 jmcneill GPIO_PIN_PUSHPULL | GPIO_PIN_TRISTATE |
535 1.11 jmcneill GPIO_PIN_PULLUP;
536 1.11 jmcneill /* safe defaults */
537 1.11 jmcneill sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_TRISTATE;
538 1.11 jmcneill sc->sc_gpio_pins[i].pin_state = GPIO_PIN_LOW;
539 1.11 jmcneill nsclpcsio_gpio_pin_ctl(sc, i, sc->sc_gpio_pins[i].pin_flags);
540 1.11 jmcneill nsclpcsio_gpio_pin_write(sc, i, sc->sc_gpio_pins[i].pin_state);
541 1.11 jmcneill }
542 1.11 jmcneill
543 1.11 jmcneill /* create controller tag */
544 1.11 jmcneill sc->sc_gpio_gc.gp_cookie = sc;
545 1.11 jmcneill sc->sc_gpio_gc.gp_pin_read = nsclpcsio_gpio_pin_read;
546 1.11 jmcneill sc->sc_gpio_gc.gp_pin_write = nsclpcsio_gpio_pin_write;
547 1.11 jmcneill sc->sc_gpio_gc.gp_pin_ctl = nsclpcsio_gpio_pin_ctl;
548 1.11 jmcneill }
549 1.11 jmcneill
550 1.11 jmcneill static int
551 1.11 jmcneill nsclpcsio_gpio_pin_read(void *aux, int pin)
552 1.11 jmcneill {
553 1.11 jmcneill struct nsclpcsio_softc *sc = (struct nsclpcsio_softc *)aux;
554 1.11 jmcneill int port, shift, reg;
555 1.21 xtraeme uint8_t v;
556 1.11 jmcneill
557 1.11 jmcneill port = pin / 8;
558 1.11 jmcneill shift = pin % 8;
559 1.11 jmcneill
560 1.11 jmcneill switch (port) {
561 1.21 xtraeme case 0:
562 1.21 xtraeme reg = SIO_GPDI0;
563 1.21 xtraeme break;
564 1.21 xtraeme case 1:
565 1.21 xtraeme reg = SIO_GPDI1;
566 1.21 xtraeme break;
567 1.21 xtraeme case 2:
568 1.21 xtraeme reg = SIO_GPDI2;
569 1.21 xtraeme break;
570 1.21 xtraeme case 3:
571 1.21 xtraeme reg = SIO_GPDI3;
572 1.21 xtraeme break;
573 1.21 xtraeme default:
574 1.21 xtraeme reg = SIO_GPDI0;
575 1.21 xtraeme break;
576 1.11 jmcneill }
577 1.11 jmcneill
578 1.11 jmcneill v = GPIO_READ(sc, reg);
579 1.11 jmcneill
580 1.11 jmcneill return ((v >> shift) & 0x1);
581 1.11 jmcneill }
582 1.11 jmcneill
583 1.11 jmcneill static void
584 1.11 jmcneill nsclpcsio_gpio_pin_write(void *aux, int pin, int v)
585 1.11 jmcneill {
586 1.11 jmcneill struct nsclpcsio_softc *sc = (struct nsclpcsio_softc *)aux;
587 1.11 jmcneill int port, shift, reg;
588 1.21 xtraeme uint8_t d;
589 1.11 jmcneill
590 1.11 jmcneill port = pin / 8;
591 1.11 jmcneill shift = pin % 8;
592 1.11 jmcneill
593 1.11 jmcneill switch (port) {
594 1.21 xtraeme case 0:
595 1.21 xtraeme reg = SIO_GPDO0;
596 1.21 xtraeme break;
597 1.21 xtraeme case 1:
598 1.21 xtraeme reg = SIO_GPDO1;
599 1.21 xtraeme break;
600 1.21 xtraeme case 2:
601 1.21 xtraeme reg = SIO_GPDO2;
602 1.21 xtraeme break;
603 1.21 xtraeme case 3:
604 1.21 xtraeme reg = SIO_GPDO3;
605 1.21 xtraeme break;
606 1.21 xtraeme default:
607 1.21 xtraeme reg = SIO_GPDO0;
608 1.21 xtraeme break; /* shouldn't happen */
609 1.11 jmcneill }
610 1.11 jmcneill
611 1.11 jmcneill d = GPIO_READ(sc, reg);
612 1.11 jmcneill if (v == 0)
613 1.11 jmcneill d &= ~(1 << shift);
614 1.11 jmcneill else if (v == 1)
615 1.11 jmcneill d |= (1 << shift);
616 1.11 jmcneill GPIO_WRITE(sc, reg, d);
617 1.11 jmcneill }
618 1.11 jmcneill
619 1.11 jmcneill void
620 1.11 jmcneill nsclpcsio_gpio_pin_ctl(void *aux, int pin, int flags)
621 1.11 jmcneill {
622 1.11 jmcneill struct nsclpcsio_softc *sc = (struct nsclpcsio_softc *)aux;
623 1.21 xtraeme uint8_t conf;
624 1.11 jmcneill
625 1.16 xtraeme mutex_enter(&sc->sc_lock);
626 1.11 jmcneill
627 1.21 xtraeme nswrite(sc->sc_iot, sc->sc_ioh, SIO_REG_LDN, SIO_LDN_GPIO);
628 1.11 jmcneill nsclpcsio_gpio_pin_select(sc, pin);
629 1.21 xtraeme conf = nsread(sc->sc_iot, sc->sc_ioh, SIO_GPIO_PINCFG);
630 1.11 jmcneill
631 1.11 jmcneill conf &= ~(SIO_GPIO_CONF_OUTPUTEN | SIO_GPIO_CONF_PUSHPULL |
632 1.11 jmcneill SIO_GPIO_CONF_PULLUP);
633 1.11 jmcneill if ((flags & GPIO_PIN_TRISTATE) == 0)
634 1.11 jmcneill conf |= SIO_GPIO_CONF_OUTPUTEN;
635 1.11 jmcneill if (flags & GPIO_PIN_PUSHPULL)
636 1.11 jmcneill conf |= SIO_GPIO_CONF_PUSHPULL;
637 1.11 jmcneill if (flags & GPIO_PIN_PULLUP)
638 1.11 jmcneill conf |= SIO_GPIO_CONF_PULLUP;
639 1.11 jmcneill
640 1.21 xtraeme nswrite(sc->sc_iot, sc->sc_ioh, SIO_GPIO_PINCFG, conf);
641 1.11 jmcneill
642 1.16 xtraeme mutex_exit(&sc->sc_lock);
643 1.11 jmcneill }
644 1.12 drochner #endif /* NGPIO */
645