wbsio.c revision 1.12 1 1.12 msaitoh /* $NetBSD: wbsio.c,v 1.12 2017/07/11 10:10: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.1 cnst int wbsio_probe(device_t, cfdata_t, void *);
47 1.1 cnst void wbsio_attach(device_t, device_t, void *);
48 1.5 jakllsch int wbsio_detach(device_t, int);
49 1.9 jakllsch int wbsio_rescan(device_t, const char *, const int *);
50 1.5 jakllsch void wbsio_childdet(device_t, device_t);
51 1.1 cnst int wbsio_print(void *, const char *);
52 1.1 cnst
53 1.9 jakllsch static int wbsio_search(device_t, cfdata_t, const int *, void *);
54 1.9 jakllsch
55 1.5 jakllsch CFATTACH_DECL2_NEW(wbsio, sizeof(struct wbsio_softc),
56 1.9 jakllsch wbsio_probe, wbsio_attach, wbsio_detach, NULL,
57 1.9 jakllsch wbsio_rescan, wbsio_childdet);
58 1.1 cnst
59 1.1 cnst static __inline void
60 1.1 cnst wbsio_conf_enable(bus_space_tag_t iot, bus_space_handle_t ioh)
61 1.1 cnst {
62 1.1 cnst bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_EN_MAGIC);
63 1.1 cnst bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_EN_MAGIC);
64 1.1 cnst }
65 1.1 cnst
66 1.1 cnst static __inline void
67 1.1 cnst wbsio_conf_disable(bus_space_tag_t iot, bus_space_handle_t ioh)
68 1.1 cnst {
69 1.1 cnst bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_DS_MAGIC);
70 1.1 cnst }
71 1.1 cnst
72 1.9 jakllsch static __inline uint8_t
73 1.9 jakllsch wbsio_conf_read(bus_space_tag_t iot, bus_space_handle_t ioh, uint8_t index)
74 1.1 cnst {
75 1.1 cnst bus_space_write_1(iot, ioh, WBSIO_INDEX, index);
76 1.1 cnst return (bus_space_read_1(iot, ioh, WBSIO_DATA));
77 1.1 cnst }
78 1.1 cnst
79 1.1 cnst static __inline void
80 1.9 jakllsch wbsio_conf_write(bus_space_tag_t iot, bus_space_handle_t ioh, uint8_t index,
81 1.9 jakllsch uint8_t data)
82 1.1 cnst {
83 1.1 cnst bus_space_write_1(iot, ioh, WBSIO_INDEX, index);
84 1.1 cnst bus_space_write_1(iot, ioh, WBSIO_DATA, data);
85 1.1 cnst }
86 1.1 cnst
87 1.1 cnst int
88 1.1 cnst wbsio_probe(device_t parent, cfdata_t match, void *aux)
89 1.1 cnst {
90 1.1 cnst struct isa_attach_args *ia = aux;
91 1.1 cnst bus_space_tag_t iot;
92 1.1 cnst bus_space_handle_t ioh;
93 1.9 jakllsch uint8_t reg;
94 1.1 cnst
95 1.1 cnst /* Must supply an address */
96 1.1 cnst if (ia->ia_nio < 1)
97 1.1 cnst return 0;
98 1.1 cnst
99 1.1 cnst if (ISA_DIRECT_CONFIG(ia))
100 1.1 cnst return 0;
101 1.1 cnst
102 1.1 cnst if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
103 1.1 cnst return 0;
104 1.1 cnst
105 1.1 cnst /* Match by device ID */
106 1.1 cnst iot = ia->ia_iot;
107 1.1 cnst if (bus_space_map(iot, ia->ia_io[0].ir_addr, WBSIO_IOSIZE, 0, &ioh))
108 1.1 cnst return 0;
109 1.1 cnst wbsio_conf_enable(iot, ioh);
110 1.1 cnst reg = wbsio_conf_read(iot, ioh, WBSIO_ID);
111 1.1 cnst aprint_debug("wbsio_probe: id 0x%02x\n", reg);
112 1.1 cnst wbsio_conf_disable(iot, ioh);
113 1.1 cnst bus_space_unmap(iot, ioh, WBSIO_IOSIZE);
114 1.1 cnst switch (reg) {
115 1.1 cnst case WBSIO_ID_W83627HF:
116 1.11 msaitoh case WBSIO_ID_W83627DHG:
117 1.11 msaitoh case WBSIO_ID_W83627DHGP:
118 1.11 msaitoh case WBSIO_ID_W83627EHF:
119 1.11 msaitoh case WBSIO_ID_W83627SF:
120 1.1 cnst case WBSIO_ID_W83627THF:
121 1.11 msaitoh case WBSIO_ID_W83627UHG:
122 1.1 cnst case WBSIO_ID_W83637HF:
123 1.11 msaitoh case WBSIO_ID_W83667HG:
124 1.11 msaitoh case WBSIO_ID_W83667HGB:
125 1.11 msaitoh case WBSIO_ID_W83687THF:
126 1.1 cnst case WBSIO_ID_W83697HF:
127 1.11 msaitoh case WBSIO_ID_W83697UG:
128 1.11 msaitoh case WBSIO_ID_NCT5104D:
129 1.12 msaitoh case WBSIO_ID_NCT6775F:
130 1.10 pgoyette case WBSIO_ID_NCT6776F:
131 1.12 msaitoh case WBSIO_ID_NCT6779D:
132 1.12 msaitoh case WBSIO_ID_NCT6791D:
133 1.12 msaitoh case WBSIO_ID_NCT6792D:
134 1.1 cnst ia->ia_nio = 1;
135 1.1 cnst ia->ia_io[0].ir_size = WBSIO_IOSIZE;
136 1.1 cnst ia->ia_niomem = 0;
137 1.1 cnst ia->ia_nirq = 0;
138 1.1 cnst ia->ia_ndrq = 0;
139 1.1 cnst return 1;
140 1.1 cnst }
141 1.1 cnst
142 1.1 cnst return 0;
143 1.1 cnst }
144 1.1 cnst
145 1.1 cnst void
146 1.1 cnst wbsio_attach(device_t parent, device_t self, void *aux)
147 1.1 cnst {
148 1.8 jakllsch struct wbsio_softc *sc = device_private(self);
149 1.1 cnst struct isa_attach_args *ia = aux;
150 1.1 cnst const char *desc = NULL;
151 1.10 pgoyette const char *vendor = "Winbond";
152 1.9 jakllsch uint8_t reg;
153 1.9 jakllsch
154 1.9 jakllsch sc->sc_dev = self;
155 1.9 jakllsch
156 1.9 jakllsch sc->sc_ia = *ia;
157 1.1 cnst
158 1.1 cnst /* Map ISA I/O space */
159 1.1 cnst sc->sc_iot = ia->ia_iot;
160 1.1 cnst if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr,
161 1.1 cnst WBSIO_IOSIZE, 0, &sc->sc_ioh)) {
162 1.1 cnst aprint_error(": can't map i/o space\n");
163 1.1 cnst return;
164 1.1 cnst }
165 1.1 cnst
166 1.1 cnst /* Enter configuration mode */
167 1.1 cnst wbsio_conf_enable(sc->sc_iot, sc->sc_ioh);
168 1.1 cnst
169 1.1 cnst /* Read device ID */
170 1.1 cnst reg = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_ID);
171 1.1 cnst switch (reg) {
172 1.1 cnst case WBSIO_ID_W83627HF:
173 1.1 cnst desc = "W83627HF";
174 1.1 cnst break;
175 1.11 msaitoh case WBSIO_ID_W83627DHG:
176 1.11 msaitoh desc = "W83627DHG";
177 1.11 msaitoh break;
178 1.11 msaitoh case WBSIO_ID_W83627DHGP:
179 1.11 msaitoh printf("W83627DHG-P");
180 1.1 cnst break;
181 1.1 cnst case WBSIO_ID_W83627EHF:
182 1.1 cnst desc = "W83627EHF";
183 1.1 cnst break;
184 1.11 msaitoh case WBSIO_ID_W83627SF:
185 1.11 msaitoh desc = "W83627SF";
186 1.11 msaitoh break;
187 1.11 msaitoh case WBSIO_ID_W83627THF:
188 1.11 msaitoh desc = "W83627THF";
189 1.11 msaitoh break;
190 1.11 msaitoh case WBSIO_ID_W83627UHG:
191 1.11 msaitoh printf("W83627UHG");
192 1.1 cnst break;
193 1.1 cnst case WBSIO_ID_W83637HF:
194 1.1 cnst desc = "W83637HF";
195 1.1 cnst break;
196 1.2 cnst case WBSIO_ID_W83667HG:
197 1.2 cnst desc = "W83667HG";
198 1.2 cnst break;
199 1.11 msaitoh case WBSIO_ID_W83667HGB:
200 1.11 msaitoh desc = "W83667HGB";
201 1.11 msaitoh break;
202 1.11 msaitoh case WBSIO_ID_W83687THF:
203 1.11 msaitoh desc = "W83687THF";
204 1.11 msaitoh break;
205 1.1 cnst case WBSIO_ID_W83697HF:
206 1.1 cnst desc = "W83697HF";
207 1.1 cnst break;
208 1.11 msaitoh case WBSIO_ID_W83697UG:
209 1.11 msaitoh desc = "W83697UG";
210 1.11 msaitoh break;
211 1.11 msaitoh case WBSIO_ID_NCT5104D:
212 1.11 msaitoh vendor = "Nuvoton";
213 1.11 msaitoh printf("NCT5104D");
214 1.11 msaitoh break;
215 1.12 msaitoh case WBSIO_ID_NCT6775F:
216 1.11 msaitoh vendor = "Nuvoton";
217 1.12 msaitoh desc = "NCT6775F";
218 1.11 msaitoh break;
219 1.10 pgoyette case WBSIO_ID_NCT6776F:
220 1.10 pgoyette vendor = "Nuvoton";
221 1.10 pgoyette desc = "NCT6776F";
222 1.10 pgoyette break;
223 1.12 msaitoh case WBSIO_ID_NCT6779D:
224 1.11 msaitoh vendor = "Nuvoton";
225 1.12 msaitoh desc = "NCT6779D";
226 1.11 msaitoh break;
227 1.12 msaitoh case WBSIO_ID_NCT6791D:
228 1.11 msaitoh vendor = "Nuvoton";
229 1.12 msaitoh desc = "NCT6791D";
230 1.11 msaitoh break;
231 1.12 msaitoh case WBSIO_ID_NCT6792D:
232 1.11 msaitoh vendor = "Nuvoton";
233 1.12 msaitoh desc = "NCT6792D";
234 1.11 msaitoh break;
235 1.12 msaitoh case WBSIO_ID_NCT6793D:
236 1.11 msaitoh vendor = "Nuvoton";
237 1.12 msaitoh desc = "NCT6793D";
238 1.12 msaitoh break;
239 1.12 msaitoh case WBSIO_ID_NCT6795D:
240 1.12 msaitoh vendor = "Nuvoton";
241 1.12 msaitoh desc = "NCT6795D";
242 1.11 msaitoh break;
243 1.1 cnst }
244 1.1 cnst /* Read device revision */
245 1.1 cnst reg = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_REV);
246 1.1 cnst
247 1.1 cnst aprint_naive("\n");
248 1.10 pgoyette aprint_normal(": %s LPC Super I/O %s rev 0x%02x\n", vendor, desc, reg);
249 1.1 cnst
250 1.1 cnst /* Escape from configuration mode */
251 1.1 cnst wbsio_conf_disable(sc->sc_iot, sc->sc_ioh);
252 1.1 cnst
253 1.4 jakllsch if (!pmf_device_register(self, NULL, NULL))
254 1.4 jakllsch aprint_error_dev(self, "couldn't establish power handler\n");
255 1.9 jakllsch wbsio_rescan(self, "wbsio", NULL);
256 1.1 cnst }
257 1.1 cnst
258 1.1 cnst int
259 1.5 jakllsch wbsio_detach(device_t self, int flags)
260 1.5 jakllsch {
261 1.8 jakllsch struct wbsio_softc *sc = device_private(self);
262 1.5 jakllsch int rc;
263 1.5 jakllsch
264 1.5 jakllsch if ((rc = config_detach_children(self, flags)) != 0)
265 1.5 jakllsch return rc;
266 1.5 jakllsch bus_space_unmap(sc->sc_iot, sc->sc_ioh, WBSIO_IOSIZE);
267 1.5 jakllsch pmf_device_deregister(self);
268 1.5 jakllsch return 0;
269 1.5 jakllsch }
270 1.5 jakllsch
271 1.9 jakllsch int
272 1.9 jakllsch wbsio_rescan(device_t self, const char *ifattr, const int *locators)
273 1.9 jakllsch {
274 1.9 jakllsch
275 1.9 jakllsch config_search_loc(wbsio_search, self, ifattr, locators, NULL);
276 1.9 jakllsch
277 1.9 jakllsch return 0;
278 1.9 jakllsch }
279 1.9 jakllsch
280 1.5 jakllsch void
281 1.5 jakllsch wbsio_childdet(device_t self, device_t child)
282 1.5 jakllsch {
283 1.9 jakllsch struct wbsio_softc *sc = device_private(self);
284 1.9 jakllsch
285 1.9 jakllsch if (sc->sc_lm_dev == child)
286 1.9 jakllsch sc->sc_lm_dev = NULL;
287 1.9 jakllsch }
288 1.9 jakllsch
289 1.9 jakllsch static int
290 1.9 jakllsch wbsio_search(device_t parent, cfdata_t cf, const int *slocs, void *aux)
291 1.9 jakllsch {
292 1.9 jakllsch struct wbsio_softc *sc = device_private(parent);
293 1.9 jakllsch uint16_t iobase;
294 1.10 pgoyette uint8_t reg0, reg1, devid;
295 1.9 jakllsch
296 1.9 jakllsch /* Enter configuration mode */
297 1.9 jakllsch wbsio_conf_enable(sc->sc_iot, sc->sc_ioh);
298 1.9 jakllsch
299 1.9 jakllsch /* Select HM logical device */
300 1.9 jakllsch wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_HM);
301 1.9 jakllsch
302 1.9 jakllsch /*
303 1.9 jakllsch * The address should be 8-byte aligned, but it seems some
304 1.9 jakllsch * BIOSes ignore this. They get away with it, because
305 1.9 jakllsch * Apparently the hardware simply ignores the lower three
306 1.9 jakllsch * bits. We do the same here.
307 1.9 jakllsch */
308 1.9 jakllsch reg0 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_HM_ADDR_LSB);
309 1.9 jakllsch reg1 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_HM_ADDR_MSB);
310 1.9 jakllsch
311 1.9 jakllsch /* Escape from configuration mode */
312 1.9 jakllsch wbsio_conf_disable(sc->sc_iot, sc->sc_ioh);
313 1.9 jakllsch
314 1.9 jakllsch iobase = (reg1 << 8) | (reg0 & ~0x7);
315 1.9 jakllsch
316 1.9 jakllsch if (iobase == 0)
317 1.9 jakllsch return -1;
318 1.9 jakllsch
319 1.10 pgoyette /* Enter configuration mode */
320 1.10 pgoyette wbsio_conf_enable(sc->sc_iot, sc->sc_ioh);
321 1.10 pgoyette /* Read device ID */
322 1.10 pgoyette devid = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_ID);
323 1.10 pgoyette /* Escape from configuration mode */
324 1.10 pgoyette wbsio_conf_disable(sc->sc_iot, sc->sc_ioh);
325 1.10 pgoyette
326 1.9 jakllsch sc->sc_ia.ia_nio = 1;
327 1.9 jakllsch sc->sc_ia.ia_io = &sc->sc_io;
328 1.9 jakllsch sc->sc_ia.ia_io[0].ir_addr = iobase;
329 1.9 jakllsch sc->sc_ia.ia_io[0].ir_size = 8;
330 1.9 jakllsch sc->sc_ia.ia_niomem = 0;
331 1.9 jakllsch sc->sc_ia.ia_nirq = 0;
332 1.9 jakllsch sc->sc_ia.ia_ndrq = 0;
333 1.10 pgoyette /* Store device-id to ia_aux */
334 1.10 pgoyette sc->sc_ia.ia_aux = (void *)(uintptr_t)devid;
335 1.9 jakllsch sc->sc_lm_dev = config_attach(parent, cf, &sc->sc_ia, wbsio_print);
336 1.9 jakllsch
337 1.9 jakllsch return 0;
338 1.5 jakllsch }
339 1.5 jakllsch
340 1.5 jakllsch int
341 1.1 cnst wbsio_print(void *aux, const char *pnp)
342 1.1 cnst {
343 1.1 cnst struct isa_attach_args *ia = aux;
344 1.1 cnst
345 1.1 cnst if (pnp)
346 1.1 cnst aprint_normal("%s", pnp);
347 1.1 cnst if (ia->ia_io[0].ir_size)
348 1.1 cnst aprint_normal(" port 0x%x", ia->ia_io[0].ir_addr);
349 1.1 cnst if (ia->ia_io[0].ir_size > 1)
350 1.1 cnst aprint_normal("-0x%x", ia->ia_io[0].ir_addr +
351 1.1 cnst ia->ia_io[0].ir_size - 1);
352 1.1 cnst return (UNCONF);
353 1.1 cnst }
354 1.6 jakllsch
355 1.7 jakllsch MODULE(MODULE_CLASS_DRIVER, wbsio, NULL);
356 1.6 jakllsch
357 1.6 jakllsch #ifdef _MODULE
358 1.6 jakllsch #include "ioconf.c"
359 1.6 jakllsch #endif
360 1.6 jakllsch
361 1.6 jakllsch static int
362 1.6 jakllsch wbsio_modcmd(modcmd_t cmd, void *opaque)
363 1.6 jakllsch {
364 1.6 jakllsch switch (cmd) {
365 1.6 jakllsch case MODULE_CMD_INIT:
366 1.6 jakllsch #ifdef _MODULE
367 1.6 jakllsch return config_init_component(cfdriver_ioconf_wbsio,
368 1.6 jakllsch cfattach_ioconf_wbsio, cfdata_ioconf_wbsio);
369 1.6 jakllsch #else
370 1.6 jakllsch return 0;
371 1.6 jakllsch #endif
372 1.6 jakllsch case MODULE_CMD_FINI:
373 1.6 jakllsch #ifdef _MODULE
374 1.6 jakllsch return config_fini_component(cfdriver_ioconf_wbsio,
375 1.6 jakllsch cfattach_ioconf_wbsio, cfdata_ioconf_wbsio);
376 1.6 jakllsch #else
377 1.6 jakllsch return 0;
378 1.6 jakllsch #endif
379 1.6 jakllsch default:
380 1.6 jakllsch return ENOTTY;
381 1.6 jakllsch }
382 1.6 jakllsch }
383