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