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