wbsio.c revision 1.19 1 /* $NetBSD: wbsio.c,v 1.19 2017/12/13 00:29:02 knakahara 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/cdefs.h>
24 #include <sys/param.h>
25 #include <sys/device.h>
26 #include <sys/kernel.h>
27 #include <sys/module.h>
28 #include <sys/systm.h>
29 #include <sys/mutex.h>
30 #include <sys/gpio.h>
31
32 #include <sys/bus.h>
33
34 #include <dev/isa/isareg.h>
35 #include <dev/isa/isavar.h>
36 #include <dev/isa/wbsioreg.h>
37 #include <dev/sysmon/sysmonvar.h>
38
39 /* Don't use gpio for now in the module */
40 #if !defined(_MODULE) && defined(WBSIO_GPIO)
41 #include "gpio.h"
42 #endif
43
44 #if NGPIO > 0
45 #include <dev/gpio/gpiovar.h>
46 #endif
47
48 struct wbsio_softc {
49 device_t sc_dev;
50 device_t sc_lm_dev;
51 #if NGPIO > 0
52 device_t sc_gpio_dev;
53 #endif
54
55 bus_space_tag_t sc_iot;
56 bus_space_handle_t sc_ioh;
57 kmutex_t sc_conf_lock;
58
59 struct isa_attach_args sc_ia;
60 struct isa_io sc_io;
61
62 #if NGPIO > 0
63 bus_space_handle_t sc_gpio_ioh;
64 kmutex_t sc_gpio_lock;
65 struct gpio_chipset_tag sc_gpio_gc;
66 struct gpio_pin sc_gpio_pins[WBSIO_GPIO_NPINS];
67 bool sc_gpio_rt;
68 #endif
69
70 struct sysmon_wdog sc_smw;
71 bool sc_smw_valid;
72 };
73
74 static const struct wbsio_product {
75 uint16_t id;
76 bool idis12bits;
77 const char *str;
78 } wbsio_products[] = {
79 { WBSIO_ID_W83627HF, false, "W83627HF" },
80 { WBSIO_ID_W83697HF, false, "W83697HF" },
81 { WBSIO_ID_W83637HF, false, "W83637HF" },
82 { WBSIO_ID_W83627THF, false, "W83627THF" },
83 { WBSIO_ID_W83687THF, false, "W83687THF" },
84 { WBSIO_ID_W83627DHG, true, "W83627DHG" },
85 { WBSIO_ID_W83627DHGP, true, "W83627DHG-P" },
86 { WBSIO_ID_W83627EHF, true, "W83627EHF" },
87 { WBSIO_ID_W83627SF, true, "W83627SF" },
88 { WBSIO_ID_W83627UHG, true, "W83627UHG" },
89 { WBSIO_ID_W83667HG, true, "W83667HG" },
90 { WBSIO_ID_W83667HGB, true, "W83667HGB" },
91 { WBSIO_ID_W83697UG, true, "W83697UG" },
92 { WBSIO_ID_NCT6775F, true, "NCT6775F" },
93 { WBSIO_ID_NCT6776F, true, "NCT6776F" },
94 { WBSIO_ID_NCT5104D, true, "NCT5104D or 610[246]D" },
95 { WBSIO_ID_NCT6779D, true, "NCT6779D" },
96 { WBSIO_ID_NCT6791D, true, "NCT6791D" },
97 { WBSIO_ID_NCT6792D, true, "NCT6792D" },
98 { WBSIO_ID_NCT6793D, true, "NCT6793D" },
99 { WBSIO_ID_NCT6795D, true, "NCT6795D" },
100 };
101
102 static const struct wbsio_product *wbsio_lookup(uint8_t id, uint8_t rev);
103 static int wbsio_match(device_t, cfdata_t, void *);
104 static void wbsio_attach(device_t, device_t, void *);
105 static int wbsio_detach(device_t, int);
106 static int wbsio_rescan(device_t, const char *, const int *);
107 static void wbsio_childdet(device_t, device_t);
108 static int wbsio_print(void *, const char *);
109 static int wbsio_search(device_t, cfdata_t, const int *, void *);
110 static bool wbsio_suspend(device_t, const pmf_qual_t *);
111 #if NGPIO > 0
112 static int wbsio_gpio_search(device_t, cfdata_t, const int *, void *);
113 static int wbsio_gpio_rt_init(struct wbsio_softc *);
114 static int wbsio_gpio_rt_read(struct wbsio_softc *, int, int);
115 static void wbsio_gpio_rt_write(struct wbsio_softc *, int, int, int);
116 static int wbsio_gpio_rt_pin_read(void *, int);
117 static void wbsio_gpio_rt_pin_write(void *, int, int);
118 static void wbsio_gpio_rt_pin_ctl(void *, int, int);
119 static void wbsio_gpio_enable_nct6779d(device_t);
120 static void wbsio_gpio_pinconfig_nct6779d(device_t);
121 #endif
122 static void wbsio_wdog_attach(device_t);
123 static int wbsio_wdog_detach(device_t);
124 static int wbsio_wdog_setmode(struct sysmon_wdog *);
125 static int wbsio_wdog_tickle(struct sysmon_wdog *);
126 static void wbsio_wdog_setcounter(struct wbsio_softc *, uint8_t);
127 static void wbsio_wdog_clear_timeout(struct wbsio_softc *);
128
129 CFATTACH_DECL2_NEW(wbsio, sizeof(struct wbsio_softc),
130 wbsio_match, wbsio_attach, wbsio_detach, NULL,
131 wbsio_rescan, wbsio_childdet);
132
133 static __inline void
134 wbsio_conf_enable(kmutex_t *lock, bus_space_tag_t iot, bus_space_handle_t ioh)
135 {
136 if (lock)
137 mutex_enter(lock);
138
139 bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_EN_MAGIC);
140 bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_EN_MAGIC);
141 }
142
143 static __inline void
144 wbsio_conf_disable(kmutex_t *lock, bus_space_tag_t iot, bus_space_handle_t ioh)
145 {
146
147 bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_DS_MAGIC);
148
149 if (lock)
150 mutex_exit(lock);
151 }
152
153 static __inline uint8_t
154 wbsio_conf_read(bus_space_tag_t iot, bus_space_handle_t ioh, uint8_t index)
155 {
156 bus_space_write_1(iot, ioh, WBSIO_INDEX, index);
157 return (bus_space_read_1(iot, ioh, WBSIO_DATA));
158 }
159
160 static __inline void
161 wbsio_conf_write(bus_space_tag_t iot, bus_space_handle_t ioh, uint8_t index,
162 uint8_t data)
163 {
164 bus_space_write_1(iot, ioh, WBSIO_INDEX, index);
165 bus_space_write_1(iot, ioh, WBSIO_DATA, data);
166 }
167
168 static const struct wbsio_product *
169 wbsio_lookup(uint8_t id, uint8_t rev)
170 {
171 uint16_t wid = ((uint16_t)id << 4) | (rev >> 4);
172 int i;
173
174 for (i = 0; i < __arraycount(wbsio_products); i++) {
175 if (wbsio_products[i].idis12bits) {
176 if (wbsio_products[i].id == wid)
177 return &wbsio_products[i];
178 } else {
179 if (wbsio_products[i].id == id)
180 return &wbsio_products[i];
181 }
182 }
183
184 /* Not found */
185 return NULL;
186 }
187
188 int
189 wbsio_match(device_t parent, cfdata_t match, void *aux)
190 {
191 struct isa_attach_args *ia = aux;
192 const struct wbsio_product *product;
193 bus_space_tag_t iot;
194 bus_space_handle_t ioh;
195 uint8_t id, rev;
196
197 /* Must supply an address */
198 if (ia->ia_nio < 1)
199 return 0;
200
201 if (ISA_DIRECT_CONFIG(ia))
202 return 0;
203
204 if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
205 return 0;
206
207 /* Match by device ID */
208 iot = ia->ia_iot;
209 if (bus_space_map(iot, ia->ia_io[0].ir_addr, WBSIO_IOSIZE, 0, &ioh))
210 return 0;
211 wbsio_conf_enable(NULL, iot, ioh);
212 id = wbsio_conf_read(iot, ioh, WBSIO_ID);
213 rev = wbsio_conf_read(iot, ioh, WBSIO_REV);
214 aprint_debug("wbsio_probe: id 0x%02x, rev 0x%02x\n", id, rev);
215 wbsio_conf_disable(NULL, iot, ioh);
216 bus_space_unmap(iot, ioh, WBSIO_IOSIZE);
217
218 if ((product = wbsio_lookup(id, rev)) == NULL)
219 return 0;
220
221 ia->ia_nio = 1;
222 ia->ia_io[0].ir_size = WBSIO_IOSIZE;
223 ia->ia_niomem = 0;
224 ia->ia_nirq = 0;
225 ia->ia_ndrq = 0;
226 return 1;
227 }
228
229 void
230 wbsio_attach(device_t parent, device_t self, void *aux)
231 {
232 struct wbsio_softc *sc = device_private(self);
233 struct isa_attach_args *ia = aux;
234 const struct wbsio_product *product;
235 const char *desc;
236 const char *vendor;
237 uint8_t id, rev;
238
239 sc->sc_dev = self;
240
241 sc->sc_ia = *ia;
242
243 /* Map ISA I/O space */
244 sc->sc_iot = ia->ia_iot;
245 if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr,
246 WBSIO_IOSIZE, 0, &sc->sc_ioh)) {
247 aprint_error(": can't map i/o space\n");
248 return;
249 }
250
251 mutex_init(&sc->sc_conf_lock, MUTEX_DEFAULT, IPL_NONE);
252
253 /* Enter configuration mode */
254 wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
255
256 /* Read device ID */
257 id = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_ID);
258 /* Read device revision */
259 rev = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_REV);
260
261 /* Escape from configuration mode */
262 wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
263
264 if ((product = wbsio_lookup(id, rev)) == NULL) {
265 aprint_error_dev(self, "Unknown device. Failed to attach\n");
266 return;
267 }
268 if (product->idis12bits)
269 rev &= 0x0f; /* Revision is low 4bits */
270
271 desc = product->str;
272 if (desc[0] == 'W')
273 vendor = "Winbond";
274 else
275 vendor = "Nuvoton";
276 aprint_naive("\n");
277 aprint_normal(": %s LPC Super I/O %s rev ", vendor, desc);
278 if (product->idis12bits) {
279 /* Revision filed is 4bit only */
280 aprint_normal("%c\n", 'A' + rev);
281 } else
282 aprint_normal("0x%02x\n", rev);
283
284 if (!pmf_device_register(self, wbsio_suspend, NULL))
285 aprint_error_dev(self, "couldn't establish power handler\n");
286
287 wbsio_wdog_attach(self);
288
289 wbsio_rescan(self, "wbsio", NULL);
290
291 #if NGPIO > 0
292
293 wbsio_rescan(self, "gpiobus", NULL);
294 #endif
295 }
296
297 int
298 wbsio_detach(device_t self, int flags)
299 {
300 struct wbsio_softc *sc = device_private(self);
301 int rc;
302
303 if ((rc = wbsio_wdog_detach(self)) != 0)
304 return rc;
305
306 if ((rc = config_detach_children(self, flags)) != 0)
307 return rc;
308 bus_space_unmap(sc->sc_iot, sc->sc_ioh, WBSIO_IOSIZE);
309 pmf_device_deregister(self);
310
311 #if NGPIO > 0
312 if (sc->sc_gpio_dev) {
313 bus_space_unmap(sc->sc_iot, sc->sc_gpio_ioh,
314 WBSIO_GPIO_IOSIZE);
315 }
316
317 if (sc->sc_gpio_rt) {
318 mutex_destroy(&sc->sc_gpio_lock);
319 }
320 #endif
321
322 mutex_destroy(&sc->sc_conf_lock);
323 return 0;
324 }
325
326 int
327 wbsio_rescan(device_t self, const char *ifattr, const int *locators)
328 {
329
330 #if NGPIO > 0
331 if (ifattr_match(ifattr, "gpiobus")) {
332 config_search_loc(wbsio_gpio_search, self,
333 ifattr, locators, NULL);
334 return 0;
335 }
336 #endif
337 config_search_loc(wbsio_search, self, ifattr, locators, NULL);
338
339 return 0;
340 }
341
342 void
343 wbsio_childdet(device_t self, device_t child)
344 {
345 struct wbsio_softc *sc = device_private(self);
346
347 if (sc->sc_lm_dev == child)
348 sc->sc_lm_dev = NULL;
349 }
350
351 static int
352 wbsio_search(device_t parent, cfdata_t cf, const int *slocs, void *aux)
353 {
354 struct wbsio_softc *sc = device_private(parent);
355 const struct wbsio_product *product;
356 uint16_t iobase;
357 uint16_t devid;
358 uint8_t reg0, reg1, rev;
359
360 /* Enter configuration mode */
361 wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
362
363 /* Select HM logical device */
364 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_HM);
365
366 /*
367 * The address should be 8-byte aligned, but it seems some
368 * BIOSes ignore this. They get away with it, because
369 * Apparently the hardware simply ignores the lower three
370 * bits. We do the same here.
371 */
372 reg0 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_HM_ADDR_LSB);
373 reg1 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_HM_ADDR_MSB);
374
375 /* Escape from configuration mode */
376 wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
377
378 iobase = (reg1 << 8) | (reg0 & ~0x7);
379
380 if (iobase == 0)
381 return -1;
382
383 /* Enter configuration mode */
384 wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
385 /* Read device ID and revision */
386 devid = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_ID);
387 rev = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_REV);
388 /* Escape from configuration mode */
389 wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
390
391 if ((product = wbsio_lookup(devid, rev)) == NULL) {
392 aprint_error_dev(parent, "%s: Unknown device.\n", __func__);
393 return -1;
394 }
395 if (product->idis12bits)
396 devid = (devid << 4) | (rev >> 4);
397
398 sc->sc_ia.ia_nio = 1;
399 sc->sc_ia.ia_io = &sc->sc_io;
400 sc->sc_ia.ia_io[0].ir_addr = iobase;
401 sc->sc_ia.ia_io[0].ir_size = 8;
402 sc->sc_ia.ia_niomem = 0;
403 sc->sc_ia.ia_nirq = 0;
404 sc->sc_ia.ia_ndrq = 0;
405 /* Store device-id to ia_aux */
406 sc->sc_ia.ia_aux = (void *)(uintptr_t)devid;
407 sc->sc_lm_dev = config_attach(parent, cf, &sc->sc_ia, wbsio_print);
408
409 return 0;
410 }
411
412 int
413 wbsio_print(void *aux, const char *pnp)
414 {
415 struct isa_attach_args *ia = aux;
416
417 if (pnp)
418 aprint_normal("%s", pnp);
419 if (ia->ia_io[0].ir_size)
420 aprint_normal(" port 0x%x", ia->ia_io[0].ir_addr);
421 if (ia->ia_io[0].ir_size > 1)
422 aprint_normal("-0x%x", ia->ia_io[0].ir_addr +
423 ia->ia_io[0].ir_size - 1);
424 return (UNCONF);
425 }
426
427 static bool
428 wbsio_suspend(device_t self, const pmf_qual_t *qual)
429 {
430 struct wbsio_softc *sc = device_private(self);
431
432 if (sc->sc_smw_valid) {
433 if ((sc->sc_smw.smw_mode & WDOG_MODE_MASK)
434 != WDOG_MODE_DISARMED)
435 return false;
436 }
437
438 return true;
439 }
440
441 #if NGPIO > 0
442 static int
443 wbsio_gpio_search(device_t parent, cfdata_t cf, const int *slocs, void *aux)
444 {
445 struct wbsio_softc *sc = device_private(parent);
446 const struct wbsio_product *product;
447 struct gpiobus_attach_args gba;
448 uint16_t devid;
449 uint8_t rev;
450 int i;
451
452 /* Enter configuration mode */
453 wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
454 /* Read device ID and revision */
455 devid = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_ID);
456 rev = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_REV);
457 /* Escape from configuration mode */
458 wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
459
460 if ((product = wbsio_lookup(devid, rev)) == NULL) {
461 aprint_error_dev(parent, "%s: Unknown device.\n", __func__);
462 return -1;
463 }
464
465 sc->sc_gpio_rt = false;
466
467 switch (product->id) {
468 case WBSIO_ID_NCT6779D:
469 wbsio_gpio_enable_nct6779d(parent);
470 sc->sc_gpio_rt = true;
471 break;
472 default:
473 aprint_error_dev(parent, "GPIO is not supported\n");
474 return -1;
475 }
476
477 if (sc->sc_gpio_rt) {
478 if (wbsio_gpio_rt_init(sc) != 0) {
479 sc->sc_gpio_rt = false;
480 return -1;
481 }
482 sc->sc_gpio_gc.gp_cookie = sc;
483 sc->sc_gpio_gc.gp_pin_read = wbsio_gpio_rt_pin_read;
484 sc->sc_gpio_gc.gp_pin_write = wbsio_gpio_rt_pin_write;
485 sc->sc_gpio_gc.gp_pin_ctl = wbsio_gpio_rt_pin_ctl;
486 } else {
487 aprint_error_dev(parent,
488 "GPIO indirect access is not supported\n");
489 return -1;
490 }
491
492 for (i = 0; i < WBSIO_GPIO_NPINS; i++) {
493 sc->sc_gpio_pins[i].pin_num = i;
494 sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
495 GPIO_PIN_OUTPUT | GPIO_PIN_INVIN | GPIO_PIN_INVOUT;
496
497 /* safe defaults */
498 sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_INPUT;
499 sc->sc_gpio_pins[i].pin_state = GPIO_PIN_LOW;
500 sc->sc_gpio_gc.gp_pin_ctl(sc, i, sc->sc_gpio_pins[i].pin_flags);
501 sc->sc_gpio_gc.gp_pin_write(sc, i, sc->sc_gpio_pins[i].pin_state);
502 }
503
504 switch (product->id) {
505 case WBSIO_ID_NCT6779D:
506 wbsio_gpio_pinconfig_nct6779d(parent);
507 break;
508 }
509
510 gba.gba_gc = &sc->sc_gpio_gc;
511 gba.gba_pins = sc->sc_gpio_pins;
512 gba.gba_npins = WBSIO_GPIO_NPINS;
513
514 sc->sc_gpio_dev = config_attach(parent, cf, &gba, gpiobus_print);
515
516 return 0;
517 }
518
519 static int
520 wbsio_gpio_rt_init(struct wbsio_softc *sc)
521 {
522 uint16_t iobase;
523 uint8_t reg0, reg1;
524
525 /* Enter configuration mode */
526 wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
527
528 /* Get GPIO Register Table address */
529 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_GPIO0);
530 reg0 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_GPIO_ADDR_LSB);
531 reg1 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_GPIO_ADDR_MSB);
532 iobase = (reg1 << 8) | (reg0 & ~0x7);
533
534 /* Escape from configuration mode */
535 wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
536
537 if (bus_space_map(sc->sc_iot, iobase, WBSIO_GPIO_IOSIZE,
538 0, &sc->sc_gpio_ioh)) {
539 aprint_error_dev(sc->sc_dev,
540 "can't map gpio to i/o space\n");
541 return -1;
542 }
543
544 aprint_normal_dev(sc->sc_dev, "GPIO: port 0x%x-0x%x\n",
545 iobase, iobase + WBSIO_GPIO_IOSIZE);
546
547 mutex_init(&sc->sc_gpio_lock, MUTEX_DEFAULT, IPL_NONE);
548
549 return 0;
550 }
551
552 static int
553 wbsio_gpio_rt_read(struct wbsio_softc *sc, int port, int reg)
554 {
555 int v;
556
557 mutex_enter(&sc->sc_gpio_lock);
558
559 bus_space_write_1(sc->sc_iot, sc->sc_gpio_ioh,
560 WBSIO_GPIO_GSR, port);
561 v = bus_space_read_1(sc->sc_iot, sc->sc_gpio_ioh, reg);
562
563 mutex_exit(&sc->sc_gpio_lock);
564
565 return v;
566 }
567
568 static void
569 wbsio_gpio_rt_write(struct wbsio_softc *sc, int port, int reg, int value)
570 {
571
572 mutex_enter(&sc->sc_gpio_lock);
573
574 bus_space_write_1(sc->sc_iot, sc->sc_gpio_ioh,
575 WBSIO_GPIO_GSR, port);
576 bus_space_write_1(sc->sc_iot, sc->sc_gpio_ioh,
577 reg, value);
578
579 mutex_exit(&sc->sc_gpio_lock);
580 }
581
582 static int
583 wbsio_gpio_rt_pin_read(void *aux, int pin)
584 {
585 struct wbsio_softc *sc = (struct wbsio_softc *)aux;
586 int port, shift, data;
587
588 port = (pin >> 3) & 0x07;
589 shift = pin & 0x07;
590
591 data = wbsio_gpio_rt_read(sc, port, WBSIO_GPIO_DAT);
592
593 return ((data >> shift) & 0x01);
594 }
595
596 static void
597 wbsio_gpio_rt_pin_write(void *aux, int pin, int v)
598 {
599 struct wbsio_softc *sc = (struct wbsio_softc *)aux;
600 int port, shift, data;
601
602 port = (pin >> 3) & 0x07;
603 shift = pin & 0x07;
604
605 data = wbsio_gpio_rt_read(sc, port, WBSIO_GPIO_DAT);
606
607 if (v == 0)
608 data &= ~(1 << shift);
609 else if (v == 1)
610 data |= (1 << shift);
611
612 wbsio_gpio_rt_write(sc, port, WBSIO_GPIO_DAT, data);
613 }
614
615 static void
616 wbsio_gpio_rt_pin_ctl(void *aux, int pin, int flags)
617 {
618 struct wbsio_softc *sc = (struct wbsio_softc *)aux;
619 uint8_t ior, inv;
620 int port, shift;
621
622 port = (pin >> 3) & 0x07;
623 shift = pin & 0x07;
624
625 ior = wbsio_gpio_rt_read(sc, port, WBSIO_GPIO_IOR);
626 inv = wbsio_gpio_rt_read(sc, port, WBSIO_GPIO_INV);
627
628 if (flags & GPIO_PIN_INPUT) {
629 ior |= (1 << shift);
630 inv &= ~(1 << shift);
631 } else if (flags & GPIO_PIN_OUTPUT) {
632 ior &= ~(1 << shift);
633 inv &= ~(1 << shift);
634 } else if (flags & GPIO_PIN_INVIN) {
635 ior |= (1 << shift);
636 inv |= (1 << shift);
637 } else if (flags & GPIO_PIN_INVOUT) {
638 ior &= ~(1 << shift);
639 inv |= (1 << shift);
640 }
641
642 wbsio_gpio_rt_write(sc, port, WBSIO_GPIO_IOR, ior);
643 wbsio_gpio_rt_write(sc, port, WBSIO_GPIO_INV, inv);
644 }
645
646 static void
647 wbsio_gpio_enable_nct6779d(device_t parent)
648 {
649 struct wbsio_softc *sc = device_private(parent);
650 uint8_t reg, conf;
651
652 /* Enter configuration mode */
653 wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
654
655 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_GPIO0);
656 reg = WBSIO_GPIO_CONF;
657 conf = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, reg);
658 /* Activate Register Table access */
659 conf |= WBSIO_GPIO_BASEADDR;
660
661 conf |= WBSIO_GPIO0_ENABLE;
662 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, reg, conf);
663
664 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_GPIO1);
665 reg = WBSIO_GPIO_CONF;
666 conf = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, reg);
667 conf |= WBSIO_GPIO1_ENABLE;
668 conf |= WBSIO_GPIO2_ENABLE;
669 conf |= WBSIO_GPIO3_ENABLE;
670 conf |= WBSIO_GPIO4_ENABLE;
671 conf |= WBSIO_GPIO5_ENABLE;
672 conf |= WBSIO_GPIO6_ENABLE;
673 conf |= WBSIO_GPIO7_ENABLE;
674 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, reg, conf);
675
676 /* Escape from configuration mode */
677 wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
678 }
679
680 static void
681 wbsio_gpio_pinconfig_nct6779d(device_t parent)
682 {
683 struct wbsio_softc *sc = device_private(parent);
684 uint8_t sfr, mfs0, mfs1, mfs2, mfs3;
685 uint8_t mfs4, mfs5, mfs6, gopt2, hm_conf;
686
687 /* Enter configuration mode */
688 wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
689
690 /* Strapping Function Result */
691 sfr = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_SFR);
692 sfr &= ~(WBSIO_SFR_LPT | WBSIO_SFR_DSW | WBSIO_SFR_AMDPWR);
693
694 /* Read current configuration */
695 mfs0 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_MFS0);
696 mfs1 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_MFS1);
697 mfs2 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_MFS2);
698 mfs3 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_MFS3);
699 mfs4 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_MFS4);
700 mfs5 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_MFS5);
701 mfs6 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_MFS6);
702 gopt2 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_GOPT2);
703
704 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_HM);
705 hm_conf = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_HM_CONF);
706
707 /* GPIO0 pin configs */
708 mfs2 |= WBSIO_NCT6779D_MFS2_GP00;
709 mfs2 |= WBSIO_NCT6779D_MFS2_GP01;
710 mfs2 |= WBSIO_NCT6779D_MFS2_GP02;
711 mfs2 &= ~WBSIO_NCT6779D_MFS2_GP03_MASK;
712 mfs2 |= WBSIO_NCT6779D_MFS2_GP04;
713 mfs2 |= WBSIO_NCT6779D_MFS2_GP05;
714 mfs2 |= WBSIO_NCT6779D_MFS2_GP06;
715 mfs3 &= ~WBSIO_NCT6779D_MFS3_GP07_MASK;
716
717 /* GPIO1 pin configs */
718 mfs4 |= WBSIO_NCT6779D_MFS4_GP10_GP17;
719
720 /* GPIO2 pin configs */
721 mfs4 |= WBSIO_NCT6779D_MFS4_GP20_GP21;
722 mfs4 |= WBSIO_NCT6779D_MFS4_GP22_GP23;
723 mfs1 &= ~WBSIO_NCT6779D_MFS1_GP24_MASK;
724 gopt2 &= ~WBSIO_NCT6779D_GOPT2_GP24_MASK;
725 mfs4 &= ~WBSIO_NCT6779D_MFS4_GP25_MASK;
726 gopt2 &= ~WBSIO_NCT6779D_GOPT2_GP25_MASK;
727 mfs6 |= WBSIO_NCT6779D_MFS6_GP26;
728 mfs6 &= ~WBSIO_NCT6779D_MFS6_GP27_MASK;
729
730 /* GPIO3 pin configs */
731 mfs0 &= ~WBSIO_NCT6779D_MFS0_GP30_MASK;
732 mfs0 |= WBSIO_NCT6779D_MFS0_GP30;
733 mfs1 &= ~WBSIO_NCT6779D_MFS1_GP31_MASK;
734 mfs0 |= WBSIO_NCT6779D_MFS0_GP31;
735 mfs1 &= ~WBSIO_NCT6779D_MFS1_GP32_MASK;
736 mfs0 |= WBSIO_NCT6779D_MFS0_GP32;
737 mfs6 &= ~WBSIO_NCT6779D_MFS6_GP33_MASK;
738 mfs6 |= WBSIO_NCT6779D_MFS6_GP33;
739 /* GP34, 35 and 36 are enabled by LPT_EN=0 */
740 /* GP37 is not existed */
741
742 /* GPIO4 pin configs */
743 mfs1 |= WBSIO_NCT6779D_MFS1_GP40;
744 /* GP41 to GP46 requires LPT_EN=0 */
745 mfs0 &= ~WBSIO_NCT6779D_MFS0_GP41_MASK;
746 mfs0 |= WBSIO_NCT6779D_MFS0_GP41;
747 mfs1 |= WBSIO_NCT6779D_MFS1_GP42;
748 mfs1 |= WBSIO_NCT6779D_MFS1_GP42;
749 gopt2 |= WBSIO_NCT6779D_GOPT2_GP43;
750 mfs1 &= ~WBSIO_NCT6779D_MFS1_GP44_GP45_MASK;
751 gopt2 &= ~WBSIO_NCT6779D_GOPT2_GP46_MASK;
752 mfs1 |= WBSIO_NCT6779D_MFS1_GP47;
753
754 /* GPIO5 pin configs */
755 /* GP50 to GP55 requires DSW_EN=0 */
756 hm_conf &= ~WBSIO_NCT6779D_HM_GP50_MASK;
757 /* GP51 is enabled by DSW_EN=0 */
758 hm_conf &= ~WBSIO_NCT6779D_HM_GP52_MASK;
759 /* GP53 and GP54 are enabled by DSW_EN=0 */
760 hm_conf &= ~WBSIO_NCT6779D_HM_GP55_MASK;
761 /* GP56 and GP57 are enabled by AMDPWR_EN=0 */
762
763 /* GPIO6 pin configs are shared with GP43 */
764
765 /* GPIO7 pin configs */
766 /* GP70 to GP73 are enabled by TEST_MODE_EN */
767 mfs5 |= WBSIO_NCT6779D_MFS5_GP74;
768 mfs5 |= WBSIO_NCT6779D_MFS5_GP75;
769 mfs5 |= WBSIO_NCT6779D_MFS5_GP76;
770 /* GP77 is not existed */
771
772 /* Write all pin configs */
773 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_SFR, sfr);
774 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_MFS0, mfs0);
775 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_MFS1, mfs1);
776 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_MFS2, mfs2);
777 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_MFS3, mfs3);
778 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_MFS4, mfs4);
779 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_MFS5, mfs5);
780 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_MFS6, mfs6);
781 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_GOPT2, gopt2);
782
783 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_HM);
784 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_HM_CONF, hm_conf);
785
786 /* Escape from configuration mode */
787 wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
788 }
789
790 #endif /* NGPIO > 0 */
791
792 static void
793 wbsio_wdog_attach(device_t self)
794 {
795 struct wbsio_softc *sc = device_private(self);
796 const struct wbsio_product *product;
797 uint8_t gpio, mode;
798 uint16_t devid;
799 uint8_t rev;
800
801 sc->sc_smw_valid = false;
802
803 wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
804 devid = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_ID);
805 rev = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_REV);
806 wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
807
808 if ((product = wbsio_lookup(devid, rev)) == NULL) {
809 return;
810 }
811
812 switch (product->id) {
813 case WBSIO_ID_NCT6779D:
814 break;
815 default:
816 /* WDT is not supoorted */
817 return;
818 }
819
820 wbsio_wdog_setcounter(sc, WBSIO_WDT_CNTR_STOP);
821
822 wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
823 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_GPIO0);
824
825 gpio = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_GPIO_CONF);
826 mode = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_WDT_MODE);
827
828 gpio |= WBSIO_GPIO0_WDT1;
829
830 mode &= ~WBSIO_WDT_MODE_FASTER;
831 mode &= ~WBSIO_WDT_MODE_MINUTES;
832 mode &= ~WBSIO_WDT_MODE_KBCRST;
833 mode &= ~WBSIO_WDT_MODE_LEVEL;
834
835 /* initialize WDT mode */
836 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_WDT_MODE, mode);
837 /* Activate WDT1 function */
838 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_GPIO_CONF, gpio);
839
840 wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
841
842 sc->sc_smw.smw_name = device_xname(self);
843 sc->sc_smw.smw_cookie = sc;
844 sc->sc_smw.smw_setmode = wbsio_wdog_setmode;
845 sc->sc_smw.smw_tickle = wbsio_wdog_tickle;
846 sc->sc_smw.smw_period = WBSIO_WDT_CNTR_MAX;
847
848 if (sysmon_wdog_register(&sc->sc_smw))
849 aprint_error_dev(self, "couldn't register with sysmon\n");
850 else
851 sc->sc_smw_valid = true;
852 }
853
854 static int
855 wbsio_wdog_detach(device_t self)
856 {
857 struct wbsio_softc *sc = device_private(self);
858 int error;
859
860 error = 0;
861
862 if (sc->sc_smw_valid) {
863 if ((sc->sc_smw.smw_mode & WDOG_MODE_MASK)
864 != WDOG_MODE_DISARMED)
865 return EBUSY;
866
867 error = sysmon_wdog_unregister(&sc->sc_smw);
868 }
869
870 if (!error)
871 sc->sc_smw_valid = false;
872
873 return error;
874 }
875
876 static int
877 wbsio_wdog_setmode(struct sysmon_wdog *smw)
878 {
879
880 switch(smw->smw_mode & WDOG_MODE_MASK) {
881 case WDOG_MODE_DISARMED:
882 wbsio_wdog_setcounter(smw->smw_cookie, WBSIO_WDT_CNTR_STOP);
883 wbsio_wdog_clear_timeout(smw->smw_cookie);
884 break;
885 default:
886 if (smw->smw_period > WBSIO_WDT_CNTR_MAX
887 || smw->smw_period == 0)
888 return EINVAL;
889
890 wbsio_wdog_setcounter(smw->smw_cookie, smw->smw_period);
891 }
892
893 return 0;
894 }
895
896 static void
897 wbsio_wdog_setcounter(struct wbsio_softc *sc, uint8_t period)
898 {
899
900 KASSERT(!mutex_owned(&sc->sc_conf_lock));
901
902 wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
903
904 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_GPIO0);
905 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_WDT_CNTR, period);
906
907 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_GPIO0);
908
909
910 wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
911 }
912
913 static void
914 wbsio_wdog_clear_timeout(struct wbsio_softc *sc)
915 {
916 uint8_t st;
917
918 KASSERT(!mutex_owned(&sc->sc_conf_lock));
919
920 wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
921
922 st = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_WDT_STAT);
923 st &= ~WBSIO_WDT_STAT_TIMEOUT;
924 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_WDT_STAT, st);
925
926 wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
927 }
928
929 static int
930 wbsio_wdog_tickle(struct sysmon_wdog *smw)
931 {
932
933 wbsio_wdog_setcounter(smw->smw_cookie, smw->smw_period);
934
935 return 0;
936 }
937
938
939 MODULE(MODULE_CLASS_DRIVER, wbsio, NULL);
940
941 #ifdef _MODULE
942 #include "ioconf.c"
943 #endif
944
945 static int
946 wbsio_modcmd(modcmd_t cmd, void *opaque)
947 {
948 switch (cmd) {
949 case MODULE_CMD_INIT:
950 #ifdef _MODULE
951 return config_init_component(cfdriver_ioconf_wbsio,
952 cfattach_ioconf_wbsio, cfdata_ioconf_wbsio);
953 #else
954 return 0;
955 #endif
956 case MODULE_CMD_FINI:
957 #ifdef _MODULE
958 return config_fini_component(cfdriver_ioconf_wbsio,
959 cfattach_ioconf_wbsio, cfdata_ioconf_wbsio);
960 #else
961 return 0;
962 #endif
963 default:
964 return ENOTTY;
965 }
966 }
967