xref: /freebsd/sys/dev/superio/superio.c (revision 6683132d54bd6d589889e43dabdc53d35e38a028)
1 /*
2  * Copyright (c) 2016 Andriy Gapon
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/proc.h>
40 #include <sys/rman.h>
41 #include <sys/time.h>
42 
43 #include <machine/bus.h>
44 #include <machine/resource.h>
45 #include <machine/stdarg.h>
46 
47 #include <isa/isavar.h>
48 
49 #include <dev/superio/superio.h>
50 
51 #include "isa_if.h"
52 
53 
54 typedef void (*sio_conf_enter_f)(struct resource*, uint16_t);
55 typedef void (*sio_conf_exit_f)(struct resource*, uint16_t);
56 
57 struct sio_conf_methods {
58 	sio_conf_enter_f	enter;
59 	sio_conf_exit_f		exit;
60 	superio_vendor_t	vendor;
61 };
62 
63 struct sio_device {
64 	uint8_t			ldn;
65 	superio_dev_type_t	type;
66 };
67 
68 struct superio_devinfo {
69 	STAILQ_ENTRY(superio_devinfo) link;
70 	struct resource_list	resources;
71 	device_t		dev;
72 	uint8_t			ldn;
73 	superio_dev_type_t	type;
74 	uint16_t		iobase;
75 	uint16_t		iobase2;
76 	uint8_t			irq;
77 	uint8_t			dma;
78 };
79 
80 struct siosc {
81 	struct mtx			conf_lock;
82 	STAILQ_HEAD(, superio_devinfo)	devlist;
83 	struct resource*		io_res;
84 	int				io_rid;
85 	uint16_t			io_port;
86 	const struct sio_conf_methods	*methods;
87 	const struct sio_device		*known_devices;
88 	superio_vendor_t		vendor;
89 	uint16_t			devid;
90 	uint8_t				revid;
91 	uint8_t				current_ldn;
92 	uint8_t				ldn_reg;
93 	uint8_t				enable_reg;
94 };
95 
96 #define NUMPORTS	2
97 
98 static uint8_t
99 sio_read(struct resource* res, uint8_t reg)
100 {
101 	bus_write_1(res, 0, reg);
102 	return (bus_read_1(res, 1));
103 }
104 
105 /* Read a word from two one-byte registers, big endian. */
106 static uint16_t
107 sio_readw(struct resource* res, uint8_t reg)
108 {
109 	uint16_t v;
110 
111 	v = sio_read(res, reg);
112 	v <<= 8;
113 	v |= sio_read(res, reg + 1);
114 	return (v);
115 }
116 
117 static void
118 sio_write(struct resource* res, uint8_t reg, uint8_t val)
119 {
120 	bus_write_1(res, 0, reg);
121 	bus_write_1(res, 1, val);
122 }
123 
124 static void
125 sio_ldn_select(struct siosc *sc, uint8_t ldn)
126 {
127 	mtx_assert(&sc->conf_lock, MA_OWNED);
128 	if (ldn == sc->current_ldn)
129 		return;
130 	sio_write(sc->io_res, sc->ldn_reg, ldn);
131 	sc->current_ldn = ldn;
132 }
133 
134 static uint8_t
135 sio_ldn_read(struct siosc *sc, uint8_t ldn, uint8_t reg)
136 {
137 	mtx_assert(&sc->conf_lock, MA_OWNED);
138 	if (reg >= sc->enable_reg) {
139 		sio_ldn_select(sc, ldn);
140 		KASSERT(sc->current_ldn == ldn, ("sio_ldn_select failed"));
141 	}
142 	return (sio_read(sc->io_res, reg));
143 }
144 
145 static uint16_t
146 sio_ldn_readw(struct siosc *sc, uint8_t ldn, uint8_t reg)
147 {
148 	mtx_assert(&sc->conf_lock, MA_OWNED);
149 	if (reg >= sc->enable_reg) {
150 		sio_ldn_select(sc, ldn);
151 		KASSERT(sc->current_ldn == ldn, ("sio_ldn_select failed"));
152 	}
153 	return (sio_readw(sc->io_res, reg));
154 }
155 
156 static void
157 sio_ldn_write(struct siosc *sc, uint8_t ldn, uint8_t reg, uint8_t val)
158 {
159 	mtx_assert(&sc->conf_lock, MA_OWNED);
160 	if (reg <= sc->ldn_reg) {
161 		printf("ignored attempt to write special register 0x%x\n", reg);
162 		return;
163 	}
164 	sio_ldn_select(sc, ldn);
165 	KASSERT(sc->current_ldn == ldn, ("sio_ldn_select failed"));
166 	sio_write(sc->io_res, reg, val);
167 }
168 
169 static void
170 sio_conf_enter(struct siosc *sc)
171 {
172 	mtx_lock(&sc->conf_lock);
173 	sc->methods->enter(sc->io_res, sc->io_port);
174 }
175 
176 static void
177 sio_conf_exit(struct siosc *sc)
178 {
179 	sc->methods->exit(sc->io_res, sc->io_port);
180 	mtx_unlock(&sc->conf_lock);
181 }
182 
183 static void
184 ite_conf_enter(struct resource* res, uint16_t port)
185 {
186 	bus_write_1(res, 0, 0x87);
187 	bus_write_1(res, 0, 0x01);
188 	bus_write_1(res, 0, 0x55);
189 	bus_write_1(res, 0, port == 0x2e ? 0x55 : 0xaa);
190 }
191 
192 static void
193 ite_conf_exit(struct resource* res, uint16_t port)
194 {
195 	sio_write(res, 0x02, 0x02);
196 }
197 
198 static const struct sio_conf_methods ite_conf_methods = {
199 	.enter = ite_conf_enter,
200 	.exit = ite_conf_exit,
201 	.vendor = SUPERIO_VENDOR_ITE
202 };
203 
204 static void
205 nvt_conf_enter(struct resource* res, uint16_t port)
206 {
207 	bus_write_1(res, 0, 0x87);
208 	bus_write_1(res, 0, 0x87);
209 }
210 
211 static void
212 nvt_conf_exit(struct resource* res, uint16_t port)
213 {
214 	bus_write_1(res, 0, 0xaa);
215 }
216 
217 static const struct sio_conf_methods nvt_conf_methods = {
218 	.enter = nvt_conf_enter,
219 	.exit = nvt_conf_exit,
220 	.vendor = SUPERIO_VENDOR_NUVOTON
221 };
222 
223 static const struct sio_conf_methods * const methods_table[] = {
224 	&ite_conf_methods,
225 	&nvt_conf_methods,
226 	NULL
227 };
228 
229 static const uint16_t ports_table[] = {
230 	0x2e, 0x4e, 0
231 };
232 
233 const struct sio_device ite_devices[] = {
234 	{ .ldn = 4, .type = SUPERIO_DEV_HWM },
235 	{ .ldn = 7, .type = SUPERIO_DEV_WDT },
236 	{ .type = SUPERIO_DEV_NONE },
237 };
238 
239 const struct sio_device nvt_devices[] = {
240 	{ .ldn = 8, .type = SUPERIO_DEV_WDT },
241 	{ .type = SUPERIO_DEV_NONE },
242 };
243 
244 const struct sio_device nct5104_devices[] = {
245 	{ .ldn = 7, .type = SUPERIO_DEV_GPIO },
246 	{ .ldn = 8, .type = SUPERIO_DEV_WDT },
247 	{ .ldn = 15, .type = SUPERIO_DEV_GPIO },
248 	{ .type = SUPERIO_DEV_NONE },
249 };
250 
251 static const struct {
252 	superio_vendor_t	vendor;
253 	uint16_t		devid;
254 	uint16_t		mask;
255 	const char		*descr;
256 	const struct sio_device	*devices;
257 } superio_table[] = {
258 	{
259 		.vendor = SUPERIO_VENDOR_ITE, .devid = 0x8712,
260 		.devices = ite_devices,
261 	},
262 	{
263 		.vendor = SUPERIO_VENDOR_ITE, .devid = 0x8716,
264 		.devices = ite_devices,
265 	},
266 	{
267 		.vendor = SUPERIO_VENDOR_ITE, .devid = 0x8718,
268 		.devices = ite_devices,
269 	},
270 	{
271 		.vendor = SUPERIO_VENDOR_ITE, .devid = 0x8720,
272 		.devices = ite_devices,
273 	},
274 	{
275 		.vendor = SUPERIO_VENDOR_ITE, .devid = 0x8721,
276 		.devices = ite_devices,
277 	},
278 	{
279 		.vendor = SUPERIO_VENDOR_ITE, .devid = 0x8726,
280 		.devices = ite_devices,
281 	},
282 	{
283 		.vendor = SUPERIO_VENDOR_ITE, .devid = 0x8728,
284 		.devices = ite_devices,
285 	},
286 	{
287 		.vendor = SUPERIO_VENDOR_ITE, .devid = 0x8771,
288 		.devices = ite_devices,
289 	},
290 	{
291 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0x1061, .mask = 0x00,
292 		.descr	= "Nuvoton NCT5104D/NCT6102D/NCT6106D (rev. A)",
293 		.devices = nct5104_devices,
294 	},
295 	{
296 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0x5200, .mask = 0xff,
297 		.descr = "Winbond 83627HF/F/HG/G",
298 		.devices = nvt_devices,
299 	},
300 	{
301 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0x5900, .mask = 0xff,
302 		.descr = "Winbond 83627S",
303 		.devices = nvt_devices,
304 	},
305 	{
306 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0x6000, .mask = 0xff,
307 		.descr = "Winbond 83697HF",
308 		.devices = nvt_devices,
309 	},
310 	{
311 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0x6800, .mask = 0xff,
312 		.descr = "Winbond 83697UG",
313 		.devices = nvt_devices,
314 	},
315 	{
316 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0x7000, .mask = 0xff,
317 		.descr = "Winbond 83637HF",
318 		.devices = nvt_devices,
319 	},
320 	{
321 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0x8200, .mask = 0xff,
322 		.descr = "Winbond 83627THF",
323 		.devices = nvt_devices,
324 	},
325 	{
326 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0x8500, .mask = 0xff,
327 		.descr = "Winbond 83687THF",
328 		.devices = nvt_devices,
329 	},
330 	{
331 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0x8800, .mask = 0xff,
332 		.descr = "Winbond 83627EHF",
333 		.devices = nvt_devices,
334 	},
335 	{
336 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xa000, .mask = 0xff,
337 		.descr = "Winbond 83627DHG",
338 		.devices = nvt_devices,
339 	},
340 	{
341 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xa200, .mask = 0xff,
342 		.descr = "Winbond 83627UHG",
343 		.devices = nvt_devices,
344 	},
345 	{
346 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xa500, .mask = 0xff,
347 		.descr = "Winbond 83667HG",
348 		.devices = nvt_devices,
349 	},
350 	{
351 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xb000, .mask = 0xff,
352 		.descr = "Winbond 83627DHG-P",
353 		.devices = nvt_devices,
354 	},
355 	{
356 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xb300, .mask = 0xff,
357 		.descr = "Winbond 83667HG-B",
358 		.devices = nvt_devices,
359 	},
360 	{
361 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xb400, .mask = 0xff,
362 		.descr = "Nuvoton NCT6775",
363 		.devices = nvt_devices,
364 	},
365 	{
366 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xc300, .mask = 0xff,
367 		.descr = "Nuvoton NCT6776",
368 		.devices = nvt_devices,
369 	},
370 	{
371 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xc400, .mask = 0xff,
372 		.descr = "Nuvoton NCT5104D/NCT6102D/NCT6106D (rev. B+)",
373 		.devices = nct5104_devices,
374 	},
375 	{
376 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xc500, .mask = 0xff,
377 		.descr = "Nuvoton NCT6779",
378 		.devices = nvt_devices,
379 	},
380 	{
381 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xc800, .mask = 0xff,
382 		.descr = "Nuvoton NCT6791",
383 		.devices = nvt_devices,
384 	},
385 	{
386 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xc900, .mask = 0xff,
387 		.descr = "Nuvoton NCT6792",
388 		.devices = nvt_devices,
389 	},
390 	{
391 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xd100, .mask = 0xff,
392 		.descr = "Nuvoton NCT6793",
393 		.devices = nvt_devices,
394 	},
395 	{
396 		.vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xd300, .mask = 0xff,
397 		.descr = "Nuvoton NCT6795",
398 		.devices = nvt_devices,
399 	},
400 	{ 0, 0 }
401 };
402 
403 static const char *
404 devtype_to_str(superio_dev_type_t type)
405 {
406 	switch (type) {
407 	case SUPERIO_DEV_NONE:
408 		return ("invalid");
409 	case SUPERIO_DEV_HWM:
410 		return ("HWM");
411 	case SUPERIO_DEV_WDT:
412 		return ("WDT");
413 	case SUPERIO_DEV_GPIO:
414 		return ("GPIO");
415 	case SUPERIO_DEV_MAX:
416 		return ("invalid");
417 	}
418 }
419 
420 static int
421 superio_detect(device_t dev, bool claim, struct siosc *sc)
422 {
423 	struct resource *res;
424 	rman_res_t port;
425 	rman_res_t count;
426 	uint16_t devid;
427 	uint8_t revid;
428 	int error;
429 	int rid;
430 	int i, m;
431 
432 	error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &port, &count);
433 	if (error != 0)
434 		return (error);
435 	if (port > UINT16_MAX || count < NUMPORTS) {
436 		device_printf(dev, "unexpected I/O range size\n");
437 		return (ENXIO);
438 	}
439 
440 	/*
441 	 * Make a temporary resource reservation for hardware probing.
442 	 * If we can't get the resources we need then
443 	 * we need to abort.  Possibly this indicates
444 	 * the resources were used by another device
445 	 * in which case the probe would have failed anyhow.
446 	 */
447 	rid = 0;
448 	res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
449 	if (res == NULL) {
450 		if (claim)
451 			device_printf(dev, "failed to allocate I/O resource\n");
452 		return (ENXIO);
453 	}
454 
455 	for (m = 0; methods_table[m] != NULL; m++) {
456 		methods_table[m]->enter(res, port);
457 		if (methods_table[m]->vendor == SUPERIO_VENDOR_ITE) {
458 			devid = sio_readw(res, 0x20);
459 			revid = sio_read(res, 0x22);
460 		} else if (methods_table[m]->vendor == SUPERIO_VENDOR_NUVOTON) {
461 			devid = sio_read(res, 0x20);
462 			revid = sio_read(res, 0x21);
463 			devid = (devid << 8) | revid;
464 		} else {
465 			continue;
466 		}
467 		methods_table[m]->exit(res, port);
468 		for (i = 0; superio_table[i].vendor != 0; i++) {
469 			uint16_t mask;
470 
471 			mask = superio_table[i].mask;
472 			if (superio_table[i].vendor !=
473 			    methods_table[m]->vendor)
474 				continue;
475 			if ((superio_table[i].devid & ~mask) != (devid & ~mask))
476 				continue;
477 			break;
478 		}
479 
480 		/* Found a matching SuperIO entry. */
481 		if (superio_table[i].vendor != 0)
482 			break;
483 	}
484 
485 	if (methods_table[m] == NULL)
486 		error = ENXIO;
487 	else
488 		error = 0;
489 	if (!claim || error != 0) {
490 		bus_release_resource(dev, SYS_RES_IOPORT, rid, res);
491 		return (error);
492 	}
493 
494 	sc->methods = methods_table[m];
495 	sc->vendor = sc->methods->vendor;
496 	sc->known_devices = superio_table[i].devices;
497 	sc->io_res = res;
498 	sc->io_rid = rid;
499 	sc->io_port = port;
500 	sc->devid = devid;
501 	sc->revid = revid;
502 
503 	KASSERT(sc->vendor == SUPERIO_VENDOR_ITE ||
504 	    sc->vendor == SUPERIO_VENDOR_NUVOTON,
505 	    ("Only ITE and Nuvoton SuperIO-s are supported"));
506 	sc->ldn_reg = 0x07;
507 	sc->enable_reg = 0x30;
508 	sc->current_ldn = 0xff;	/* no device should have this */
509 
510 	if (superio_table[i].descr != NULL) {
511 		device_set_desc(dev, superio_table[i].descr);
512 	} else if (sc->vendor == SUPERIO_VENDOR_ITE) {
513 		char descr[64];
514 
515 		snprintf(descr, sizeof(descr),
516 		    "ITE IT%4x SuperIO (revision 0x%02x)",
517 		    sc->devid, sc->revid);
518 		device_set_desc_copy(dev, descr);
519 	}
520 	return (0);
521 }
522 
523 static void
524 superio_identify(driver_t *driver, device_t parent)
525 {
526 	device_t	child;
527 	int i;
528 
529 	/*
530 	 * Don't create child devices if any already exist.
531 	 * Those could be created via isa hints or if this
532 	 * driver is loaded, unloaded and then loaded again.
533 	 */
534 	if (device_find_child(parent, "superio", -1)) {
535 		if (bootverbose)
536 			printf("superio: device(s) already created\n");
537 		return;
538 	}
539 
540 	/*
541 	 * Create a child for each candidate port.
542 	 * It would be nice if we could somehow clean up those
543 	 * that this driver fails to probe.
544 	 */
545 	for (i = 0; ports_table[i] != 0; i++) {
546 		child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE,
547 		    "superio", -1);
548 		if (child == NULL) {
549 			device_printf(parent, "failed to add superio child\n");
550 			continue;
551 		}
552 		bus_set_resource(child, SYS_RES_IOPORT,	0, ports_table[i], 2);
553 		if (superio_detect(child, false, NULL) != 0)
554 			device_delete_child(parent, child);
555 	}
556 }
557 
558 static int
559 superio_probe(device_t dev)
560 {
561 	struct siosc *sc;
562 	int error;
563 
564 	/* Make sure we do not claim some ISA PNP device. */
565 	if (isa_get_logicalid(dev) != 0)
566 		return (ENXIO);
567 
568 	/*
569 	 * XXX We can populate the softc now only because we return
570 	 * BUS_PROBE_SPECIFIC
571 	 */
572 	sc = device_get_softc(dev);
573 	error = superio_detect(dev, true, sc);
574 	if (error != 0)
575 		return (error);
576 	return (BUS_PROBE_SPECIFIC);
577 }
578 
579 static void
580 superio_add_known_child(device_t dev, superio_dev_type_t type, uint8_t ldn)
581 {
582 	struct siosc *sc = device_get_softc(dev);
583 	struct superio_devinfo *dinfo;
584 	device_t child;
585 
586 	child = BUS_ADD_CHILD(dev, 0, NULL, -1);
587 	if (child == NULL) {
588 		device_printf(dev, "failed to add child for ldn %d, type %s\n",
589 		    ldn, devtype_to_str(type));
590 		return;
591 	}
592 	dinfo = device_get_ivars(child);
593 	dinfo->ldn = ldn;
594 	dinfo->type = type;
595 	sio_conf_enter(sc);
596 	dinfo->iobase = sio_ldn_readw(sc, ldn, 0x60);
597 	dinfo->iobase2 = sio_ldn_readw(sc, ldn, 0x62);
598 	dinfo->irq = sio_ldn_readw(sc, ldn, 0x70);
599 	dinfo->dma = sio_ldn_readw(sc, ldn, 0x74);
600 	sio_conf_exit(sc);
601 	STAILQ_INSERT_TAIL(&sc->devlist, dinfo, link);
602 }
603 
604 static int
605 superio_attach(device_t dev)
606 {
607 	struct siosc *sc = device_get_softc(dev);
608 	int i;
609 
610 	mtx_init(&sc->conf_lock, device_get_nameunit(dev), "superio", MTX_DEF);
611 	STAILQ_INIT(&sc->devlist);
612 
613 	for (i = 0; sc->known_devices[i].type != SUPERIO_DEV_NONE; i++) {
614 		superio_add_known_child(dev, sc->known_devices[i].type,
615 		    sc->known_devices[i].ldn);
616 	}
617 
618 	bus_generic_probe(dev);
619 	bus_generic_attach(dev);
620 	return (0);
621 }
622 
623 static int
624 superio_detach(device_t dev)
625 {
626 	struct siosc *sc = device_get_softc(dev);
627 	int error;
628 
629 	error = bus_generic_detach(dev);
630 	if (error != 0)
631 		return (error);
632 	device_delete_children(dev);
633 	bus_release_resource(dev, SYS_RES_IOPORT, sc->io_rid, sc->io_res);
634 	mtx_destroy(&sc->conf_lock);
635 	return (0);
636 }
637 
638 static device_t
639 superio_add_child(device_t dev, u_int order, const char *name, int unit)
640 {
641 	struct superio_devinfo *dinfo;
642 	device_t child;
643 
644 	child = device_add_child_ordered(dev, order, name, unit);
645 	if (child == NULL)
646 		return (NULL);
647 	dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
648 	if (dinfo == NULL) {
649 		device_delete_child(dev, child);
650 		return (NULL);
651 	}
652 	dinfo->ldn = 0xff;
653 	dinfo->type = SUPERIO_DEV_NONE;
654 	dinfo->dev = child;
655 	resource_list_init(&dinfo->resources);
656 	device_set_ivars(child, dinfo);
657 	return (child);
658 }
659 
660 static int
661 superio_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
662 {
663 	struct superio_devinfo *dinfo;
664 
665 	dinfo = device_get_ivars(child);
666 	switch (which) {
667 	case SUPERIO_IVAR_LDN:
668 		*result = dinfo->ldn;
669 		break;
670 	case SUPERIO_IVAR_TYPE:
671 		*result = dinfo->type;
672 		break;
673 	case SUPERIO_IVAR_IOBASE:
674 		*result = dinfo->iobase;
675 		break;
676 	case SUPERIO_IVAR_IOBASE2:
677 		*result = dinfo->iobase2;
678 		break;
679 	case SUPERIO_IVAR_IRQ:
680 		*result = dinfo->irq;
681 		break;
682 	case SUPERIO_IVAR_DMA:
683 		*result = dinfo->dma;
684 		break;
685 	default:
686 		return (ENOENT);
687 	}
688 	return (0);
689 }
690 
691 static int
692 superio_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
693 {
694 
695 	switch (which) {
696 	case SUPERIO_IVAR_LDN:
697 	case SUPERIO_IVAR_TYPE:
698 	case SUPERIO_IVAR_IOBASE:
699 	case SUPERIO_IVAR_IOBASE2:
700 	case SUPERIO_IVAR_IRQ:
701 	case SUPERIO_IVAR_DMA:
702 		return (EINVAL);
703 	default:
704 		return (ENOENT);
705 	}
706 }
707 
708 static struct resource_list *
709 superio_get_resource_list(device_t dev, device_t child)
710 {
711 	struct superio_devinfo *dinfo = device_get_ivars(child);
712 
713 	return (&dinfo->resources);
714 }
715 
716 static int
717 superio_printf(struct superio_devinfo *dinfo, const char *fmt, ...)
718 {
719 	va_list ap;
720 	int retval;
721 
722 	retval = printf("superio:%s@ldn%0x2x: ",
723 	    devtype_to_str(dinfo->type), dinfo->ldn);
724 	va_start(ap, fmt);
725 	retval += vprintf(fmt, ap);
726 	va_end(ap);
727 	return (retval);
728 }
729 
730 static void
731 superio_child_detached(device_t dev, device_t child)
732 {
733 	struct superio_devinfo *dinfo;
734 	struct resource_list *rl;
735 
736 	dinfo = device_get_ivars(child);
737 	rl = &dinfo->resources;
738 
739 	if (resource_list_release_active(rl, dev, child, SYS_RES_IRQ) != 0)
740 		superio_printf(dinfo, "Device leaked IRQ resources\n");
741 	if (resource_list_release_active(rl, dev, child, SYS_RES_MEMORY) != 0)
742 		superio_printf(dinfo, "Device leaked memory resources\n");
743 	if (resource_list_release_active(rl, dev, child, SYS_RES_IOPORT) != 0)
744 		superio_printf(dinfo, "Device leaked I/O resources\n");
745 }
746 
747 static int
748 superio_child_location_str(device_t parent, device_t child, char *buf,
749     size_t buflen)
750 {
751 	uint8_t ldn;
752 
753 	ldn = superio_get_ldn(child);
754 	snprintf(buf, buflen, "ldn=0x%02x", ldn);
755 	return (0);
756 }
757 
758 static int
759 superio_child_pnp_str(device_t parent, device_t child, char *buf,
760     size_t buflen)
761 {
762 	superio_dev_type_t type;
763 
764 	type = superio_get_type(child);
765 	snprintf(buf, buflen, "type=%s", devtype_to_str(type));
766 	return (0);
767 }
768 
769 static int
770 superio_print_child(device_t parent, device_t child)
771 {
772 	superio_dev_type_t type;
773 	uint8_t ldn;
774 	int retval;
775 
776 	ldn = superio_get_ldn(child);
777 	type = superio_get_type(child);
778 
779 	retval = bus_print_child_header(parent, child);
780 	retval += printf(" at %s ldn 0x%02x", devtype_to_str(type), ldn);
781 	retval += bus_print_child_footer(parent, child);
782 
783 	return (retval);
784 }
785 
786 superio_vendor_t
787 superio_vendor(device_t dev)
788 {
789 	device_t sio_dev = device_get_parent(dev);
790 	struct siosc *sc = device_get_softc(sio_dev);
791 
792 	return (sc->vendor);
793 }
794 
795 uint16_t
796 superio_devid(device_t dev)
797 {
798 	device_t sio_dev = device_get_parent(dev);
799 	struct siosc *sc = device_get_softc(sio_dev);
800 
801 	return (sc->devid);
802 }
803 
804 uint8_t
805 superio_revid(device_t dev)
806 {
807 	device_t sio_dev = device_get_parent(dev);
808 	struct siosc *sc = device_get_softc(sio_dev);
809 
810 	return (sc->revid);
811 }
812 
813 uint8_t
814 superio_read(device_t dev, uint8_t reg)
815 {
816 	device_t sio_dev = device_get_parent(dev);
817 	struct siosc *sc = device_get_softc(sio_dev);
818 	struct superio_devinfo *dinfo = device_get_ivars(dev);
819 	uint8_t v;
820 
821 	sio_conf_enter(sc);
822 	v = sio_ldn_read(sc, dinfo->ldn, reg);
823 	sio_conf_exit(sc);
824 	return (v);
825 }
826 
827 void
828 superio_write(device_t dev, uint8_t reg, uint8_t val)
829 {
830 	device_t sio_dev = device_get_parent(dev);
831 	struct siosc *sc = device_get_softc(sio_dev);
832 	struct superio_devinfo *dinfo = device_get_ivars(dev);
833 
834 	sio_conf_enter(sc);
835 	sio_ldn_write(sc, dinfo->ldn, reg, val);
836 	sio_conf_exit(sc);
837 }
838 
839 bool
840 superio_dev_enabled(device_t dev, uint8_t mask)
841 {
842 	device_t sio_dev = device_get_parent(dev);
843 	struct siosc *sc = device_get_softc(sio_dev);
844 	struct superio_devinfo *dinfo = device_get_ivars(dev);
845 	uint8_t v;
846 
847 	/* GPIO device is always active in ITE chips. */
848 	if (sc->vendor == SUPERIO_VENDOR_ITE && dinfo->ldn == 7)
849 		return (true);
850 
851 	v = superio_read(dev, sc->enable_reg);
852 	return ((v & mask) != 0);
853 }
854 
855 void
856 superio_dev_enable(device_t dev, uint8_t mask)
857 {
858 	device_t sio_dev = device_get_parent(dev);
859 	struct siosc *sc = device_get_softc(sio_dev);
860 	struct superio_devinfo *dinfo = device_get_ivars(dev);
861 	uint8_t v;
862 
863 	/* GPIO device is always active in ITE chips. */
864 	if (sc->vendor == SUPERIO_VENDOR_ITE && dinfo->ldn == 7)
865 		return;
866 
867 	sio_conf_enter(sc);
868 	v = sio_ldn_read(sc, dinfo->ldn, sc->enable_reg);
869 	v |= mask;
870 	sio_ldn_write(sc, dinfo->ldn, sc->enable_reg, v);
871 	sio_conf_exit(sc);
872 }
873 
874 void
875 superio_dev_disable(device_t dev, uint8_t mask)
876 {
877 	device_t sio_dev = device_get_parent(dev);
878 	struct siosc *sc = device_get_softc(sio_dev);
879 	struct superio_devinfo *dinfo = device_get_ivars(dev);
880 	uint8_t v;
881 
882 	/* GPIO device is always active in ITE chips. */
883 	if (sc->vendor == SUPERIO_VENDOR_ITE && dinfo->ldn == 7)
884 		return;
885 
886 	sio_conf_enter(sc);
887 	v = sio_ldn_read(sc, dinfo->ldn, sc->enable_reg);
888 	v &= ~mask;
889 	sio_ldn_write(sc, dinfo->ldn, sc->enable_reg, v);
890 	sio_conf_exit(sc);
891 }
892 
893 device_t
894 superio_find_dev(device_t superio, superio_dev_type_t type, int ldn)
895 {
896 	struct siosc *sc = device_get_softc(superio);
897 	struct superio_devinfo *dinfo;
898 
899 	if (ldn < -1 || ldn > UINT8_MAX)
900 		return (NULL);		/* ERANGE */
901 	if (type == SUPERIO_DEV_NONE && ldn == -1)
902 		return (NULL);		/* EINVAL */
903 
904 	STAILQ_FOREACH(dinfo, &sc->devlist, link) {
905 		if (ldn != -1 && dinfo->ldn != ldn)
906 			continue;
907 		if (type != SUPERIO_DEV_NONE && dinfo->type != type)
908 			continue;
909 		return (dinfo->dev);
910 	}
911 	return (NULL);
912 }
913 
914 static devclass_t superio_devclass;
915 
916 static device_method_t superio_methods[] = {
917 	DEVMETHOD(device_identify,	superio_identify),
918 	DEVMETHOD(device_probe,		superio_probe),
919 	DEVMETHOD(device_attach,	superio_attach),
920 	DEVMETHOD(device_detach,	superio_detach),
921 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
922 	DEVMETHOD(device_suspend,	bus_generic_suspend),
923 	DEVMETHOD(device_resume,	bus_generic_resume),
924 
925 	DEVMETHOD(bus_add_child,	superio_add_child),
926 	DEVMETHOD(bus_child_detached,	superio_child_detached),
927 	DEVMETHOD(bus_child_location_str, superio_child_location_str),
928 	DEVMETHOD(bus_child_pnpinfo_str, superio_child_pnp_str),
929 	DEVMETHOD(bus_print_child,	superio_print_child),
930 	DEVMETHOD(bus_read_ivar,	superio_read_ivar),
931 	DEVMETHOD(bus_write_ivar,	superio_write_ivar),
932 	DEVMETHOD(bus_get_resource_list, superio_get_resource_list),
933 	DEVMETHOD(bus_alloc_resource,	bus_generic_rl_alloc_resource),
934 	DEVMETHOD(bus_release_resource,	bus_generic_rl_release_resource),
935 	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
936 	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
937 	DEVMETHOD(bus_delete_resource,	bus_generic_rl_delete_resource),
938 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
939 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
940 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
941 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
942 
943 	DEVMETHOD_END
944 };
945 
946 static driver_t superio_driver = {
947 	"superio",
948 	superio_methods,
949 	sizeof(struct siosc)
950 };
951 
952 DRIVER_MODULE(superio, isa, superio_driver, superio_devclass, 0, 0);
953 MODULE_VERSION(superio, 1);
954