wbsio.c revision 1.15 1 1.15 msaitoh /* $NetBSD: wbsio.c,v 1.15 2017/08/18 04:07:51 msaitoh Exp $ */
2 1.11 msaitoh /* $OpenBSD: wbsio.c,v 1.10 2015/03/14 03:38:47 jsg Exp $ */
3 1.1 cnst /*
4 1.1 cnst * Copyright (c) 2008 Mark Kettenis <kettenis (at) openbsd.org>
5 1.1 cnst *
6 1.1 cnst * Permission to use, copy, modify, and distribute this software for any
7 1.1 cnst * purpose with or without fee is hereby granted, provided that the above
8 1.1 cnst * copyright notice and this permission notice appear in all copies.
9 1.1 cnst *
10 1.1 cnst * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1 cnst * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1 cnst * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1 cnst * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1 cnst * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 1.1 cnst * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 1.1 cnst * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1 cnst */
18 1.1 cnst
19 1.1 cnst /*
20 1.1 cnst * Winbond LPC Super I/O driver.
21 1.1 cnst */
22 1.1 cnst
23 1.1 cnst #include <sys/param.h>
24 1.1 cnst #include <sys/device.h>
25 1.1 cnst #include <sys/kernel.h>
26 1.6 jakllsch #include <sys/module.h>
27 1.1 cnst #include <sys/systm.h>
28 1.1 cnst
29 1.3 dyoung #include <sys/bus.h>
30 1.1 cnst
31 1.1 cnst #include <dev/isa/isareg.h>
32 1.1 cnst #include <dev/isa/isavar.h>
33 1.11 msaitoh #include <dev/isa/wbsioreg.h>
34 1.1 cnst
35 1.1 cnst struct wbsio_softc {
36 1.9 jakllsch device_t sc_dev;
37 1.9 jakllsch device_t sc_lm_dev;
38 1.1 cnst
39 1.1 cnst bus_space_tag_t sc_iot;
40 1.1 cnst bus_space_handle_t sc_ioh;
41 1.9 jakllsch
42 1.9 jakllsch struct isa_attach_args sc_ia;
43 1.9 jakllsch struct isa_io sc_io;
44 1.1 cnst };
45 1.1 cnst
46 1.15 msaitoh static const struct wbsio_product {
47 1.15 msaitoh uint16_t id;
48 1.15 msaitoh bool idis12bits;
49 1.15 msaitoh const char *str;
50 1.15 msaitoh } wbsio_products[] = {
51 1.15 msaitoh { WBSIO_ID_W83627HF, false, "W83627HF" },
52 1.15 msaitoh { WBSIO_ID_W83697HF, false, "W83697HF" },
53 1.15 msaitoh { WBSIO_ID_W83637HF, false, "W83637HF" },
54 1.15 msaitoh { WBSIO_ID_W83627THF, false, "W83627THF" },
55 1.15 msaitoh { WBSIO_ID_W83687THF, false, "W83687THF" },
56 1.15 msaitoh { WBSIO_ID_W83627DHG, true, "W83627DHG" },
57 1.15 msaitoh { WBSIO_ID_W83627DHGP, true, "W83627DHG-P" },
58 1.15 msaitoh { WBSIO_ID_W83627EHF, true, "W83627EHF" },
59 1.15 msaitoh { WBSIO_ID_W83627SF, true, "W83627SF" },
60 1.15 msaitoh { WBSIO_ID_W83627UHG, true, "W83627UHG" },
61 1.15 msaitoh { WBSIO_ID_W83667HG, true, "W83667HG" },
62 1.15 msaitoh { WBSIO_ID_W83667HGB, true, "W83667HGB" },
63 1.15 msaitoh { WBSIO_ID_W83697UG, true, "W83697UG" },
64 1.15 msaitoh { WBSIO_ID_NCT6775F, true, "NCT6775F" },
65 1.15 msaitoh { WBSIO_ID_NCT6776F, true, "NCT6776F" },
66 1.15 msaitoh { WBSIO_ID_NCT5104D, true, "NCT5104D or 610[246]D" },
67 1.15 msaitoh { WBSIO_ID_NCT6779D, true, "NCT6779D" },
68 1.15 msaitoh { WBSIO_ID_NCT6791D, true, "NCT6791D" },
69 1.15 msaitoh { WBSIO_ID_NCT6792D, true, "NCT6792D" },
70 1.15 msaitoh { WBSIO_ID_NCT6793D, true, "NCT6793D" },
71 1.15 msaitoh { WBSIO_ID_NCT6795D, true, "NCT6795D" },
72 1.15 msaitoh };
73 1.15 msaitoh
74 1.15 msaitoh static const struct wbsio_product *wbsio_lookup(uint8_t id, uint8_t rev);
75 1.15 msaitoh static int wbsio_match(device_t, cfdata_t, void *);
76 1.13 msaitoh static void wbsio_attach(device_t, device_t, void *);
77 1.13 msaitoh static int wbsio_detach(device_t, int);
78 1.13 msaitoh static int wbsio_rescan(device_t, const char *, const int *);
79 1.13 msaitoh static void wbsio_childdet(device_t, device_t);
80 1.13 msaitoh static int wbsio_print(void *, const char *);
81 1.13 msaitoh static int wbsio_search(device_t, cfdata_t, const int *, void *);
82 1.9 jakllsch
83 1.5 jakllsch CFATTACH_DECL2_NEW(wbsio, sizeof(struct wbsio_softc),
84 1.15 msaitoh wbsio_match, wbsio_attach, wbsio_detach, NULL,
85 1.9 jakllsch wbsio_rescan, wbsio_childdet);
86 1.1 cnst
87 1.1 cnst static __inline void
88 1.1 cnst wbsio_conf_enable(bus_space_tag_t iot, bus_space_handle_t ioh)
89 1.1 cnst {
90 1.1 cnst bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_EN_MAGIC);
91 1.1 cnst bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_EN_MAGIC);
92 1.1 cnst }
93 1.1 cnst
94 1.1 cnst static __inline void
95 1.1 cnst wbsio_conf_disable(bus_space_tag_t iot, bus_space_handle_t ioh)
96 1.1 cnst {
97 1.1 cnst bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_DS_MAGIC);
98 1.1 cnst }
99 1.1 cnst
100 1.9 jakllsch static __inline uint8_t
101 1.9 jakllsch wbsio_conf_read(bus_space_tag_t iot, bus_space_handle_t ioh, uint8_t index)
102 1.1 cnst {
103 1.1 cnst bus_space_write_1(iot, ioh, WBSIO_INDEX, index);
104 1.1 cnst return (bus_space_read_1(iot, ioh, WBSIO_DATA));
105 1.1 cnst }
106 1.1 cnst
107 1.1 cnst static __inline void
108 1.9 jakllsch wbsio_conf_write(bus_space_tag_t iot, bus_space_handle_t ioh, uint8_t index,
109 1.9 jakllsch uint8_t data)
110 1.1 cnst {
111 1.1 cnst bus_space_write_1(iot, ioh, WBSIO_INDEX, index);
112 1.1 cnst bus_space_write_1(iot, ioh, WBSIO_DATA, data);
113 1.1 cnst }
114 1.1 cnst
115 1.15 msaitoh static const struct wbsio_product *
116 1.15 msaitoh wbsio_lookup(uint8_t id, uint8_t rev)
117 1.15 msaitoh {
118 1.15 msaitoh uint16_t wid = ((uint16_t)id << 4) | (rev >> 4);
119 1.15 msaitoh int i;
120 1.15 msaitoh
121 1.15 msaitoh for (i = 0; i < __arraycount(wbsio_products); i++) {
122 1.15 msaitoh if (wbsio_products[i].idis12bits) {
123 1.15 msaitoh if (wbsio_products[i].id == wid)
124 1.15 msaitoh return &wbsio_products[i];
125 1.15 msaitoh } else {
126 1.15 msaitoh if (wbsio_products[i].id == id)
127 1.15 msaitoh return &wbsio_products[i];
128 1.15 msaitoh }
129 1.15 msaitoh }
130 1.15 msaitoh
131 1.15 msaitoh /* Not found */
132 1.15 msaitoh return NULL;
133 1.15 msaitoh }
134 1.15 msaitoh
135 1.1 cnst int
136 1.15 msaitoh wbsio_match(device_t parent, cfdata_t match, void *aux)
137 1.1 cnst {
138 1.1 cnst struct isa_attach_args *ia = aux;
139 1.15 msaitoh const struct wbsio_product *product;
140 1.1 cnst bus_space_tag_t iot;
141 1.1 cnst bus_space_handle_t ioh;
142 1.15 msaitoh uint8_t id, rev;
143 1.1 cnst
144 1.1 cnst /* Must supply an address */
145 1.1 cnst if (ia->ia_nio < 1)
146 1.1 cnst return 0;
147 1.1 cnst
148 1.1 cnst if (ISA_DIRECT_CONFIG(ia))
149 1.1 cnst return 0;
150 1.1 cnst
151 1.1 cnst if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
152 1.1 cnst return 0;
153 1.1 cnst
154 1.1 cnst /* Match by device ID */
155 1.1 cnst iot = ia->ia_iot;
156 1.1 cnst if (bus_space_map(iot, ia->ia_io[0].ir_addr, WBSIO_IOSIZE, 0, &ioh))
157 1.1 cnst return 0;
158 1.1 cnst wbsio_conf_enable(iot, ioh);
159 1.15 msaitoh id = wbsio_conf_read(iot, ioh, WBSIO_ID);
160 1.15 msaitoh rev = wbsio_conf_read(iot, ioh, WBSIO_REV);
161 1.15 msaitoh aprint_debug("wbsio_probe: id 0x%02x, rev 0x%02x\n", id, rev);
162 1.1 cnst wbsio_conf_disable(iot, ioh);
163 1.1 cnst bus_space_unmap(iot, ioh, WBSIO_IOSIZE);
164 1.1 cnst
165 1.15 msaitoh if ((product = wbsio_lookup(id, rev)) == NULL)
166 1.15 msaitoh return 0;
167 1.15 msaitoh
168 1.15 msaitoh ia->ia_nio = 1;
169 1.15 msaitoh ia->ia_io[0].ir_size = WBSIO_IOSIZE;
170 1.15 msaitoh ia->ia_niomem = 0;
171 1.15 msaitoh ia->ia_nirq = 0;
172 1.15 msaitoh ia->ia_ndrq = 0;
173 1.15 msaitoh return 1;
174 1.1 cnst }
175 1.1 cnst
176 1.1 cnst void
177 1.1 cnst wbsio_attach(device_t parent, device_t self, void *aux)
178 1.1 cnst {
179 1.8 jakllsch struct wbsio_softc *sc = device_private(self);
180 1.1 cnst struct isa_attach_args *ia = aux;
181 1.15 msaitoh const struct wbsio_product *product;
182 1.15 msaitoh const char *desc;
183 1.15 msaitoh const char *vendor;
184 1.15 msaitoh uint8_t id, rev;
185 1.9 jakllsch
186 1.9 jakllsch sc->sc_dev = self;
187 1.9 jakllsch
188 1.9 jakllsch sc->sc_ia = *ia;
189 1.1 cnst
190 1.1 cnst /* Map ISA I/O space */
191 1.1 cnst sc->sc_iot = ia->ia_iot;
192 1.1 cnst if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr,
193 1.1 cnst WBSIO_IOSIZE, 0, &sc->sc_ioh)) {
194 1.1 cnst aprint_error(": can't map i/o space\n");
195 1.1 cnst return;
196 1.1 cnst }
197 1.1 cnst
198 1.1 cnst /* Enter configuration mode */
199 1.1 cnst wbsio_conf_enable(sc->sc_iot, sc->sc_ioh);
200 1.1 cnst
201 1.1 cnst /* Read device ID */
202 1.15 msaitoh id = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_ID);
203 1.1 cnst /* Read device revision */
204 1.15 msaitoh rev = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_REV);
205 1.1 cnst
206 1.1 cnst /* Escape from configuration mode */
207 1.1 cnst wbsio_conf_disable(sc->sc_iot, sc->sc_ioh);
208 1.1 cnst
209 1.15 msaitoh if ((product = wbsio_lookup(id, rev)) == NULL) {
210 1.15 msaitoh aprint_error_dev(self, "Unknown device. Failed to attach\n");
211 1.15 msaitoh return;
212 1.15 msaitoh }
213 1.15 msaitoh if (product->idis12bits)
214 1.15 msaitoh rev &= 0x0f; /* Revision is low 4bits */
215 1.15 msaitoh
216 1.15 msaitoh desc = product->str;
217 1.15 msaitoh if (desc[0] == 'W')
218 1.15 msaitoh vendor = "Winbond";
219 1.15 msaitoh else
220 1.15 msaitoh vendor = "Nuvoton";
221 1.15 msaitoh aprint_naive("\n");
222 1.15 msaitoh aprint_normal(": %s LPC Super I/O %s rev ", vendor, desc);
223 1.15 msaitoh if (product->idis12bits) {
224 1.15 msaitoh /* Revision filed is 4bit only */
225 1.15 msaitoh aprint_normal("%c\n", 'A' + rev);
226 1.15 msaitoh } else
227 1.15 msaitoh aprint_normal("0x%02x\n", rev);
228 1.15 msaitoh
229 1.4 jakllsch if (!pmf_device_register(self, NULL, NULL))
230 1.4 jakllsch aprint_error_dev(self, "couldn't establish power handler\n");
231 1.9 jakllsch wbsio_rescan(self, "wbsio", NULL);
232 1.1 cnst }
233 1.1 cnst
234 1.1 cnst int
235 1.5 jakllsch wbsio_detach(device_t self, int flags)
236 1.5 jakllsch {
237 1.8 jakllsch struct wbsio_softc *sc = device_private(self);
238 1.5 jakllsch int rc;
239 1.5 jakllsch
240 1.5 jakllsch if ((rc = config_detach_children(self, flags)) != 0)
241 1.5 jakllsch return rc;
242 1.5 jakllsch bus_space_unmap(sc->sc_iot, sc->sc_ioh, WBSIO_IOSIZE);
243 1.5 jakllsch pmf_device_deregister(self);
244 1.5 jakllsch return 0;
245 1.5 jakllsch }
246 1.5 jakllsch
247 1.9 jakllsch int
248 1.9 jakllsch wbsio_rescan(device_t self, const char *ifattr, const int *locators)
249 1.9 jakllsch {
250 1.9 jakllsch
251 1.9 jakllsch config_search_loc(wbsio_search, self, ifattr, locators, NULL);
252 1.9 jakllsch
253 1.9 jakllsch return 0;
254 1.9 jakllsch }
255 1.9 jakllsch
256 1.5 jakllsch void
257 1.5 jakllsch wbsio_childdet(device_t self, device_t child)
258 1.5 jakllsch {
259 1.9 jakllsch struct wbsio_softc *sc = device_private(self);
260 1.9 jakllsch
261 1.9 jakllsch if (sc->sc_lm_dev == child)
262 1.9 jakllsch sc->sc_lm_dev = NULL;
263 1.9 jakllsch }
264 1.9 jakllsch
265 1.9 jakllsch static int
266 1.9 jakllsch wbsio_search(device_t parent, cfdata_t cf, const int *slocs, void *aux)
267 1.9 jakllsch {
268 1.9 jakllsch struct wbsio_softc *sc = device_private(parent);
269 1.15 msaitoh const struct wbsio_product *product;
270 1.9 jakllsch uint16_t iobase;
271 1.15 msaitoh uint16_t devid;
272 1.15 msaitoh uint8_t reg0, reg1, rev;
273 1.9 jakllsch
274 1.9 jakllsch /* Enter configuration mode */
275 1.9 jakllsch wbsio_conf_enable(sc->sc_iot, sc->sc_ioh);
276 1.9 jakllsch
277 1.9 jakllsch /* Select HM logical device */
278 1.9 jakllsch wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_HM);
279 1.9 jakllsch
280 1.9 jakllsch /*
281 1.9 jakllsch * The address should be 8-byte aligned, but it seems some
282 1.9 jakllsch * BIOSes ignore this. They get away with it, because
283 1.9 jakllsch * Apparently the hardware simply ignores the lower three
284 1.9 jakllsch * bits. We do the same here.
285 1.9 jakllsch */
286 1.9 jakllsch reg0 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_HM_ADDR_LSB);
287 1.9 jakllsch reg1 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_HM_ADDR_MSB);
288 1.9 jakllsch
289 1.9 jakllsch /* Escape from configuration mode */
290 1.9 jakllsch wbsio_conf_disable(sc->sc_iot, sc->sc_ioh);
291 1.9 jakllsch
292 1.9 jakllsch iobase = (reg1 << 8) | (reg0 & ~0x7);
293 1.9 jakllsch
294 1.9 jakllsch if (iobase == 0)
295 1.9 jakllsch return -1;
296 1.9 jakllsch
297 1.10 pgoyette /* Enter configuration mode */
298 1.10 pgoyette wbsio_conf_enable(sc->sc_iot, sc->sc_ioh);
299 1.15 msaitoh /* Read device ID and revision */
300 1.10 pgoyette devid = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_ID);
301 1.15 msaitoh rev = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_REV);
302 1.10 pgoyette /* Escape from configuration mode */
303 1.10 pgoyette wbsio_conf_disable(sc->sc_iot, sc->sc_ioh);
304 1.10 pgoyette
305 1.15 msaitoh if ((product = wbsio_lookup(devid, rev)) == NULL) {
306 1.15 msaitoh aprint_error_dev(parent, "%s: Unknown device.\n", __func__);
307 1.15 msaitoh return -1;
308 1.15 msaitoh }
309 1.15 msaitoh if (product->idis12bits)
310 1.15 msaitoh devid = (devid << 4) | (rev >> 4);
311 1.15 msaitoh
312 1.9 jakllsch sc->sc_ia.ia_nio = 1;
313 1.9 jakllsch sc->sc_ia.ia_io = &sc->sc_io;
314 1.9 jakllsch sc->sc_ia.ia_io[0].ir_addr = iobase;
315 1.9 jakllsch sc->sc_ia.ia_io[0].ir_size = 8;
316 1.9 jakllsch sc->sc_ia.ia_niomem = 0;
317 1.9 jakllsch sc->sc_ia.ia_nirq = 0;
318 1.9 jakllsch sc->sc_ia.ia_ndrq = 0;
319 1.10 pgoyette /* Store device-id to ia_aux */
320 1.10 pgoyette sc->sc_ia.ia_aux = (void *)(uintptr_t)devid;
321 1.9 jakllsch sc->sc_lm_dev = config_attach(parent, cf, &sc->sc_ia, wbsio_print);
322 1.9 jakllsch
323 1.9 jakllsch return 0;
324 1.5 jakllsch }
325 1.5 jakllsch
326 1.5 jakllsch int
327 1.1 cnst wbsio_print(void *aux, const char *pnp)
328 1.1 cnst {
329 1.1 cnst struct isa_attach_args *ia = aux;
330 1.1 cnst
331 1.1 cnst if (pnp)
332 1.1 cnst aprint_normal("%s", pnp);
333 1.1 cnst if (ia->ia_io[0].ir_size)
334 1.1 cnst aprint_normal(" port 0x%x", ia->ia_io[0].ir_addr);
335 1.1 cnst if (ia->ia_io[0].ir_size > 1)
336 1.1 cnst aprint_normal("-0x%x", ia->ia_io[0].ir_addr +
337 1.1 cnst ia->ia_io[0].ir_size - 1);
338 1.1 cnst return (UNCONF);
339 1.1 cnst }
340 1.6 jakllsch
341 1.7 jakllsch MODULE(MODULE_CLASS_DRIVER, wbsio, NULL);
342 1.6 jakllsch
343 1.6 jakllsch #ifdef _MODULE
344 1.6 jakllsch #include "ioconf.c"
345 1.6 jakllsch #endif
346 1.6 jakllsch
347 1.6 jakllsch static int
348 1.6 jakllsch wbsio_modcmd(modcmd_t cmd, void *opaque)
349 1.6 jakllsch {
350 1.6 jakllsch switch (cmd) {
351 1.6 jakllsch case MODULE_CMD_INIT:
352 1.6 jakllsch #ifdef _MODULE
353 1.6 jakllsch return config_init_component(cfdriver_ioconf_wbsio,
354 1.6 jakllsch cfattach_ioconf_wbsio, cfdata_ioconf_wbsio);
355 1.6 jakllsch #else
356 1.6 jakllsch return 0;
357 1.6 jakllsch #endif
358 1.6 jakllsch case MODULE_CMD_FINI:
359 1.6 jakllsch #ifdef _MODULE
360 1.6 jakllsch return config_fini_component(cfdriver_ioconf_wbsio,
361 1.6 jakllsch cfattach_ioconf_wbsio, cfdata_ioconf_wbsio);
362 1.6 jakllsch #else
363 1.6 jakllsch return 0;
364 1.6 jakllsch #endif
365 1.6 jakllsch default:
366 1.6 jakllsch return ENOTTY;
367 1.6 jakllsch }
368 1.6 jakllsch }
369