wbsio.c revision 1.22 1 /* $NetBSD: wbsio.c,v 1.22 2018/02/20 01:53:39 pgoyette 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 sc->sc_smw_valid = false;
288 wbsio_rescan(self, "wbsio", NULL);
289
290 #if NGPIO > 0
291
292 wbsio_rescan(self, "gpiobus", NULL);
293 #endif
294 }
295
296 int
297 wbsio_detach(device_t self, int flags)
298 {
299 struct wbsio_softc *sc = device_private(self);
300 int rc;
301
302 if ((rc = wbsio_wdog_detach(self)) != 0)
303 return rc;
304
305 if ((rc = config_detach_children(self, flags)) != 0)
306 return rc;
307 bus_space_unmap(sc->sc_iot, sc->sc_ioh, WBSIO_IOSIZE);
308 pmf_device_deregister(self);
309
310 #if NGPIO > 0
311 if (sc->sc_gpio_dev) {
312 bus_space_unmap(sc->sc_iot, sc->sc_gpio_ioh,
313 WBSIO_GPIO_IOSIZE);
314 }
315
316 if (sc->sc_gpio_rt) {
317 mutex_destroy(&sc->sc_gpio_lock);
318 }
319 #endif
320
321 mutex_destroy(&sc->sc_conf_lock);
322 return 0;
323 }
324
325 int
326 wbsio_rescan(device_t self, const char *ifattr, const int *locators)
327 {
328
329 #if NGPIO > 0
330 if (ifattr_match(ifattr, "gpiobus")) {
331 config_search_loc(wbsio_gpio_search, self,
332 ifattr, locators, NULL);
333 return 0;
334 }
335 #endif
336 config_search_loc(wbsio_search, self, ifattr, locators, NULL);
337
338 wbsio_wdog_attach(self);
339
340 return 0;
341 }
342
343 void
344 wbsio_childdet(device_t self, device_t child)
345 {
346 struct wbsio_softc *sc = device_private(self);
347
348 if (sc->sc_lm_dev == child)
349 sc->sc_lm_dev = NULL;
350 }
351
352 static int
353 wbsio_search(device_t parent, cfdata_t cf, const int *slocs, void *aux)
354 {
355 struct wbsio_softc *sc = device_private(parent);
356 const struct wbsio_product *product;
357 uint16_t iobase;
358 uint16_t devid;
359 uint8_t reg0, reg1, rev;
360
361 /* Enter configuration mode */
362 wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
363
364 /* Select HM logical device */
365 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_HM);
366
367 /*
368 * The address should be 8-byte aligned, but it seems some
369 * BIOSes ignore this. They get away with it, because
370 * Apparently the hardware simply ignores the lower three
371 * bits. We do the same here.
372 */
373 reg0 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_HM_ADDR_LSB);
374 reg1 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_HM_ADDR_MSB);
375
376 /* Escape from configuration mode */
377 wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
378
379 iobase = (reg1 << 8) | (reg0 & ~0x7);
380
381 if (iobase == 0)
382 return -1;
383
384 /* Enter configuration mode */
385 wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
386 /* Read device ID and revision */
387 devid = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_ID);
388 rev = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_REV);
389 /* Escape from configuration mode */
390 wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
391
392 if ((product = wbsio_lookup(devid, rev)) == NULL) {
393 aprint_error_dev(parent, "%s: Unknown device.\n", __func__);
394 return -1;
395 }
396 if (product->idis12bits)
397 devid = (devid << 4) | (rev >> 4);
398
399 sc->sc_ia.ia_nio = 1;
400 sc->sc_ia.ia_io = &sc->sc_io;
401 sc->sc_ia.ia_io[0].ir_addr = iobase;
402 sc->sc_ia.ia_io[0].ir_size = 8;
403 sc->sc_ia.ia_niomem = 0;
404 sc->sc_ia.ia_nirq = 0;
405 sc->sc_ia.ia_ndrq = 0;
406 /* Store device-id to ia_aux */
407 sc->sc_ia.ia_aux = (void *)(uintptr_t)devid;
408 sc->sc_lm_dev = config_attach(parent, cf, &sc->sc_ia, wbsio_print);
409
410 return 0;
411 }
412
413 int
414 wbsio_print(void *aux, const char *pnp)
415 {
416 struct isa_attach_args *ia = aux;
417
418 if (pnp)
419 aprint_normal("%s", pnp);
420 if (ia->ia_io[0].ir_size)
421 aprint_normal(" port 0x%x", ia->ia_io[0].ir_addr);
422 if (ia->ia_io[0].ir_size > 1)
423 aprint_normal("-0x%x", ia->ia_io[0].ir_addr +
424 ia->ia_io[0].ir_size - 1);
425 return (UNCONF);
426 }
427
428 static bool
429 wbsio_suspend(device_t self, const pmf_qual_t *qual)
430 {
431 struct wbsio_softc *sc = device_private(self);
432
433 if (sc->sc_smw_valid) {
434 if ((sc->sc_smw.smw_mode & WDOG_MODE_MASK)
435 != WDOG_MODE_DISARMED)
436 return false;
437 }
438
439 return true;
440 }
441
442 #if NGPIO > 0
443 static int
444 wbsio_gpio_search(device_t parent, cfdata_t cf, const int *slocs, void *aux)
445 {
446 struct wbsio_softc *sc = device_private(parent);
447 const struct wbsio_product *product;
448 struct gpiobus_attach_args gba;
449 uint16_t devid;
450 uint8_t rev;
451 int i;
452
453 /* Enter configuration mode */
454 wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
455 /* Read device ID and revision */
456 devid = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_ID);
457 rev = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_REV);
458 /* Escape from configuration mode */
459 wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
460
461 if ((product = wbsio_lookup(devid, rev)) == NULL) {
462 aprint_error_dev(parent, "%s: Unknown device.\n", __func__);
463 return -1;
464 }
465
466 sc->sc_gpio_rt = false;
467
468 switch (product->id) {
469 case WBSIO_ID_NCT6779D:
470 wbsio_gpio_enable_nct6779d(parent);
471 sc->sc_gpio_rt = true;
472 break;
473 default:
474 aprint_error_dev(parent, "%s's GPIO is not supported yet\n",
475 product->str);
476 return -1;
477 }
478
479 if (sc->sc_gpio_rt) {
480 if (wbsio_gpio_rt_init(sc) != 0) {
481 sc->sc_gpio_rt = false;
482 return -1;
483 }
484 sc->sc_gpio_gc.gp_cookie = sc;
485 sc->sc_gpio_gc.gp_pin_read = wbsio_gpio_rt_pin_read;
486 sc->sc_gpio_gc.gp_pin_write = wbsio_gpio_rt_pin_write;
487 sc->sc_gpio_gc.gp_pin_ctl = wbsio_gpio_rt_pin_ctl;
488 } else {
489 aprint_error_dev(parent,
490 "GPIO indirect access is not supported\n");
491 return -1;
492 }
493
494 for (i = 0; i < WBSIO_GPIO_NPINS; i++) {
495 sc->sc_gpio_pins[i].pin_num = i;
496 sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
497 GPIO_PIN_OUTPUT | GPIO_PIN_INVIN | GPIO_PIN_INVOUT;
498
499 /* safe defaults */
500 sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_INPUT;
501 sc->sc_gpio_pins[i].pin_state = GPIO_PIN_LOW;
502 sc->sc_gpio_gc.gp_pin_ctl(sc, i, sc->sc_gpio_pins[i].pin_flags);
503 sc->sc_gpio_gc.gp_pin_write(sc, i, sc->sc_gpio_pins[i].pin_state);
504 }
505
506 switch (product->id) {
507 case WBSIO_ID_NCT6779D:
508 wbsio_gpio_pinconfig_nct6779d(parent);
509 break;
510 }
511
512 gba.gba_gc = &sc->sc_gpio_gc;
513 gba.gba_pins = sc->sc_gpio_pins;
514 gba.gba_npins = WBSIO_GPIO_NPINS;
515
516 sc->sc_gpio_dev = config_attach(parent, cf, &gba, gpiobus_print);
517
518 return 0;
519 }
520
521 static int
522 wbsio_gpio_rt_init(struct wbsio_softc *sc)
523 {
524 uint16_t iobase;
525 uint8_t reg0, reg1;
526
527 /* Enter configuration mode */
528 wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
529
530 /* Get GPIO Register Table address */
531 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_GPIO0);
532 reg0 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_GPIO_ADDR_LSB);
533 reg1 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_GPIO_ADDR_MSB);
534 iobase = (reg1 << 8) | (reg0 & ~0x7);
535
536 /* Escape from configuration mode */
537 wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
538
539 if (bus_space_map(sc->sc_iot, iobase, WBSIO_GPIO_IOSIZE,
540 0, &sc->sc_gpio_ioh)) {
541 aprint_error_dev(sc->sc_dev,
542 "can't map gpio to i/o space\n");
543 return -1;
544 }
545
546 aprint_normal_dev(sc->sc_dev, "GPIO: port 0x%x-0x%x\n",
547 iobase, iobase + WBSIO_GPIO_IOSIZE);
548
549 mutex_init(&sc->sc_gpio_lock, MUTEX_DEFAULT, IPL_NONE);
550
551 return 0;
552 }
553
554 static int
555 wbsio_gpio_rt_read(struct wbsio_softc *sc, int port, int reg)
556 {
557 int v;
558
559 mutex_enter(&sc->sc_gpio_lock);
560
561 bus_space_write_1(sc->sc_iot, sc->sc_gpio_ioh,
562 WBSIO_GPIO_GSR, port);
563 v = bus_space_read_1(sc->sc_iot, sc->sc_gpio_ioh, reg);
564
565 mutex_exit(&sc->sc_gpio_lock);
566
567 return v;
568 }
569
570 static void
571 wbsio_gpio_rt_write(struct wbsio_softc *sc, int port, int reg, int value)
572 {
573
574 mutex_enter(&sc->sc_gpio_lock);
575
576 bus_space_write_1(sc->sc_iot, sc->sc_gpio_ioh,
577 WBSIO_GPIO_GSR, port);
578 bus_space_write_1(sc->sc_iot, sc->sc_gpio_ioh,
579 reg, value);
580
581 mutex_exit(&sc->sc_gpio_lock);
582 }
583
584 static int
585 wbsio_gpio_rt_pin_read(void *aux, int pin)
586 {
587 struct wbsio_softc *sc = (struct wbsio_softc *)aux;
588 int port, shift, data;
589
590 port = (pin >> 3) & 0x07;
591 shift = pin & 0x07;
592
593 data = wbsio_gpio_rt_read(sc, port, WBSIO_GPIO_DAT);
594
595 return ((data >> shift) & 0x01);
596 }
597
598 static void
599 wbsio_gpio_rt_pin_write(void *aux, int pin, int v)
600 {
601 struct wbsio_softc *sc = (struct wbsio_softc *)aux;
602 int port, shift, data;
603
604 port = (pin >> 3) & 0x07;
605 shift = pin & 0x07;
606
607 data = wbsio_gpio_rt_read(sc, port, WBSIO_GPIO_DAT);
608
609 if (v == 0)
610 data &= ~(1 << shift);
611 else if (v == 1)
612 data |= (1 << shift);
613
614 wbsio_gpio_rt_write(sc, port, WBSIO_GPIO_DAT, data);
615 }
616
617 static void
618 wbsio_gpio_rt_pin_ctl(void *aux, int pin, int flags)
619 {
620 struct wbsio_softc *sc = (struct wbsio_softc *)aux;
621 uint8_t ior, inv;
622 int port, shift;
623
624 port = (pin >> 3) & 0x07;
625 shift = pin & 0x07;
626
627 ior = wbsio_gpio_rt_read(sc, port, WBSIO_GPIO_IOR);
628 inv = wbsio_gpio_rt_read(sc, port, WBSIO_GPIO_INV);
629
630 if (flags & GPIO_PIN_INPUT) {
631 ior |= (1 << shift);
632 inv &= ~(1 << shift);
633 } else if (flags & GPIO_PIN_OUTPUT) {
634 ior &= ~(1 << shift);
635 inv &= ~(1 << shift);
636 } else if (flags & GPIO_PIN_INVIN) {
637 ior |= (1 << shift);
638 inv |= (1 << shift);
639 } else if (flags & GPIO_PIN_INVOUT) {
640 ior &= ~(1 << shift);
641 inv |= (1 << shift);
642 }
643
644 wbsio_gpio_rt_write(sc, port, WBSIO_GPIO_IOR, ior);
645 wbsio_gpio_rt_write(sc, port, WBSIO_GPIO_INV, inv);
646 }
647
648 static void
649 wbsio_gpio_enable_nct6779d(device_t parent)
650 {
651 struct wbsio_softc *sc = device_private(parent);
652 uint8_t reg, conf;
653
654 /* Enter configuration mode */
655 wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
656
657 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_GPIO0);
658 reg = WBSIO_GPIO_CONF;
659 conf = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, reg);
660 /* Activate Register Table access */
661 conf |= WBSIO_GPIO_BASEADDR;
662
663 conf |= WBSIO_GPIO0_ENABLE;
664 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, reg, conf);
665
666 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_GPIO1);
667 reg = WBSIO_GPIO_CONF;
668 conf = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, reg);
669 conf |= WBSIO_GPIO1_ENABLE;
670 conf |= WBSIO_GPIO2_ENABLE;
671 conf |= WBSIO_GPIO3_ENABLE;
672 conf |= WBSIO_GPIO4_ENABLE;
673 conf |= WBSIO_GPIO5_ENABLE;
674 conf |= WBSIO_GPIO6_ENABLE;
675 conf |= WBSIO_GPIO7_ENABLE;
676 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, reg, conf);
677
678 /* Escape from configuration mode */
679 wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
680 }
681
682 static void
683 wbsio_gpio_pinconfig_nct6779d(device_t parent)
684 {
685 struct wbsio_softc *sc = device_private(parent);
686 uint8_t sfr, mfs0, mfs1, mfs2, mfs3;
687 uint8_t mfs4, mfs5, mfs6, gopt2, hm_conf;
688
689 /* Enter configuration mode */
690 wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
691
692 /* Strapping Function Result */
693 sfr = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_SFR);
694 sfr &= ~(WBSIO_SFR_LPT | WBSIO_SFR_DSW | WBSIO_SFR_AMDPWR);
695
696 /* Read current configuration */
697 mfs0 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_MFS0);
698 mfs1 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_MFS1);
699 mfs2 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_MFS2);
700 mfs3 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_MFS3);
701 mfs4 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_MFS4);
702 mfs5 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_MFS5);
703 mfs6 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_MFS6);
704 gopt2 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_GOPT2);
705
706 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_HM);
707 hm_conf = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_HM_CONF);
708
709 /* GPIO0 pin configs */
710 mfs2 |= WBSIO_NCT6779D_MFS2_GP00;
711 mfs2 |= WBSIO_NCT6779D_MFS2_GP01;
712 mfs2 |= WBSIO_NCT6779D_MFS2_GP02;
713 mfs2 &= ~WBSIO_NCT6779D_MFS2_GP03_MASK;
714 mfs2 |= WBSIO_NCT6779D_MFS2_GP04;
715 mfs2 |= WBSIO_NCT6779D_MFS2_GP05;
716 mfs2 |= WBSIO_NCT6779D_MFS2_GP06;
717 mfs3 &= ~WBSIO_NCT6779D_MFS3_GP07_MASK;
718
719 /* GPIO1 pin configs */
720 mfs4 |= WBSIO_NCT6779D_MFS4_GP10_GP17;
721
722 /* GPIO2 pin configs */
723 mfs4 |= WBSIO_NCT6779D_MFS4_GP20_GP21;
724 mfs4 |= WBSIO_NCT6779D_MFS4_GP22_GP23;
725 mfs1 &= ~WBSIO_NCT6779D_MFS1_GP24_MASK;
726 gopt2 &= ~WBSIO_NCT6779D_GOPT2_GP24_MASK;
727 mfs4 &= ~WBSIO_NCT6779D_MFS4_GP25_MASK;
728 gopt2 &= ~WBSIO_NCT6779D_GOPT2_GP25_MASK;
729 mfs6 |= WBSIO_NCT6779D_MFS6_GP26;
730 mfs6 &= ~WBSIO_NCT6779D_MFS6_GP27_MASK;
731
732 /* GPIO3 pin configs */
733 mfs0 &= ~WBSIO_NCT6779D_MFS0_GP30_MASK;
734 mfs0 |= WBSIO_NCT6779D_MFS0_GP30;
735 mfs1 &= ~WBSIO_NCT6779D_MFS1_GP31_MASK;
736 mfs0 |= WBSIO_NCT6779D_MFS0_GP31;
737 mfs1 &= ~WBSIO_NCT6779D_MFS1_GP32_MASK;
738 mfs0 |= WBSIO_NCT6779D_MFS0_GP32;
739 mfs6 &= ~WBSIO_NCT6779D_MFS6_GP33_MASK;
740 mfs6 |= WBSIO_NCT6779D_MFS6_GP33;
741 /* GP34, 35 and 36 are enabled by LPT_EN=0 */
742 /* GP37 is not existed */
743
744 /* GPIO4 pin configs */
745 mfs1 |= WBSIO_NCT6779D_MFS1_GP40;
746 /* GP41 to GP46 requires LPT_EN=0 */
747 mfs0 &= ~WBSIO_NCT6779D_MFS0_GP41_MASK;
748 mfs0 |= WBSIO_NCT6779D_MFS0_GP41;
749 mfs1 |= WBSIO_NCT6779D_MFS1_GP42;
750 mfs1 |= WBSIO_NCT6779D_MFS1_GP42;
751 gopt2 |= WBSIO_NCT6779D_GOPT2_GP43;
752 mfs1 &= ~WBSIO_NCT6779D_MFS1_GP44_GP45_MASK;
753 gopt2 &= ~WBSIO_NCT6779D_GOPT2_GP46_MASK;
754 mfs1 |= WBSIO_NCT6779D_MFS1_GP47;
755
756 /* GPIO5 pin configs */
757 /* GP50 to GP55 requires DSW_EN=0 */
758 hm_conf &= ~WBSIO_NCT6779D_HM_GP50_MASK;
759 /* GP51 is enabled by DSW_EN=0 */
760 hm_conf &= ~WBSIO_NCT6779D_HM_GP52_MASK;
761 /* GP53 and GP54 are enabled by DSW_EN=0 */
762 hm_conf &= ~WBSIO_NCT6779D_HM_GP55_MASK;
763 /* GP56 and GP57 are enabled by AMDPWR_EN=0 */
764
765 /* GPIO6 pin configs are shared with GP43 */
766
767 /* GPIO7 pin configs */
768 /* GP70 to GP73 are enabled by TEST_MODE_EN */
769 mfs5 |= WBSIO_NCT6779D_MFS5_GP74;
770 mfs5 |= WBSIO_NCT6779D_MFS5_GP75;
771 mfs5 |= WBSIO_NCT6779D_MFS5_GP76;
772 /* GP77 is not existed */
773
774 /* Write all pin configs */
775 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_SFR, sfr);
776 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_MFS0, mfs0);
777 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_MFS1, mfs1);
778 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_MFS2, mfs2);
779 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_MFS3, mfs3);
780 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_MFS4, mfs4);
781 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_MFS5, mfs5);
782 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_MFS6, mfs6);
783 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_GOPT2, gopt2);
784
785 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_HM);
786 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_HM_CONF, hm_conf);
787
788 /* Escape from configuration mode */
789 wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
790 }
791
792 #endif /* NGPIO > 0 */
793
794 static void
795 wbsio_wdog_attach(device_t self)
796 {
797 struct wbsio_softc *sc = device_private(self);
798 const struct wbsio_product *product;
799 uint8_t gpio, mode;
800 uint16_t devid;
801 uint8_t rev;
802
803 if (sc->sc_smw_valid)
804 return; /* watchdog already attached */
805
806 wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
807 devid = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_ID);
808 rev = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_REV);
809 wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
810
811 if ((product = wbsio_lookup(devid, rev)) == NULL) {
812 return;
813 }
814
815 switch (product->id) {
816 case WBSIO_ID_NCT6779D:
817 break;
818 default:
819 /* WDT is not supoorted */
820 return;
821 }
822
823 wbsio_wdog_setcounter(sc, WBSIO_WDT_CNTR_STOP);
824
825 wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
826 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_GPIO0);
827
828 gpio = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_GPIO_CONF);
829 mode = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_WDT_MODE);
830
831 gpio |= WBSIO_GPIO0_WDT1;
832
833 mode &= ~WBSIO_WDT_MODE_FASTER;
834 mode &= ~WBSIO_WDT_MODE_MINUTES;
835 mode &= ~WBSIO_WDT_MODE_KBCRST;
836 mode &= ~WBSIO_WDT_MODE_LEVEL;
837
838 /* initialize WDT mode */
839 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_WDT_MODE, mode);
840 /* Activate WDT1 function */
841 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_GPIO_CONF, gpio);
842
843 wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
844
845 sc->sc_smw.smw_name = device_xname(self);
846 sc->sc_smw.smw_cookie = sc;
847 sc->sc_smw.smw_setmode = wbsio_wdog_setmode;
848 sc->sc_smw.smw_tickle = wbsio_wdog_tickle;
849 sc->sc_smw.smw_period = WBSIO_WDT_CNTR_MAX;
850
851 if (sysmon_wdog_register(&sc->sc_smw))
852 aprint_error_dev(self, "couldn't register with sysmon\n");
853 else
854 sc->sc_smw_valid = true;
855 }
856
857 static int
858 wbsio_wdog_detach(device_t self)
859 {
860 struct wbsio_softc *sc = device_private(self);
861 int error;
862
863 error = 0;
864
865 if (sc->sc_smw_valid) {
866 if ((sc->sc_smw.smw_mode & WDOG_MODE_MASK)
867 != WDOG_MODE_DISARMED)
868 return EBUSY;
869
870 error = sysmon_wdog_unregister(&sc->sc_smw);
871 }
872
873 if (!error)
874 sc->sc_smw_valid = false;
875
876 return error;
877 }
878
879 static int
880 wbsio_wdog_setmode(struct sysmon_wdog *smw)
881 {
882
883 switch(smw->smw_mode & WDOG_MODE_MASK) {
884 case WDOG_MODE_DISARMED:
885 wbsio_wdog_setcounter(smw->smw_cookie, WBSIO_WDT_CNTR_STOP);
886 wbsio_wdog_clear_timeout(smw->smw_cookie);
887 break;
888 default:
889 if (smw->smw_period > WBSIO_WDT_CNTR_MAX
890 || smw->smw_period == 0)
891 return EINVAL;
892
893 wbsio_wdog_setcounter(smw->smw_cookie, smw->smw_period);
894 }
895
896 return 0;
897 }
898
899 static void
900 wbsio_wdog_setcounter(struct wbsio_softc *sc, uint8_t period)
901 {
902
903 KASSERT(!mutex_owned(&sc->sc_conf_lock));
904
905 wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
906
907 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_GPIO0);
908 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_WDT_CNTR, period);
909
910 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_GPIO0);
911
912
913 wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
914 }
915
916 static void
917 wbsio_wdog_clear_timeout(struct wbsio_softc *sc)
918 {
919 uint8_t st;
920
921 KASSERT(!mutex_owned(&sc->sc_conf_lock));
922
923 wbsio_conf_enable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
924
925 st = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_WDT_STAT);
926 st &= ~WBSIO_WDT_STAT_TIMEOUT;
927 wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_WDT_STAT, st);
928
929 wbsio_conf_disable(&sc->sc_conf_lock, sc->sc_iot, sc->sc_ioh);
930 }
931
932 static int
933 wbsio_wdog_tickle(struct sysmon_wdog *smw)
934 {
935
936 wbsio_wdog_setcounter(smw->smw_cookie, smw->smw_period);
937
938 return 0;
939 }
940
941
942 MODULE(MODULE_CLASS_DRIVER, wbsio, "sysmon_wdog");
943
944 #ifdef _MODULE
945 #include "ioconf.c"
946 #endif
947
948 static int
949 wbsio_modcmd(modcmd_t cmd, void *opaque)
950 {
951 switch (cmd) {
952 case MODULE_CMD_INIT:
953 #ifdef _MODULE
954 return config_init_component(cfdriver_ioconf_wbsio,
955 cfattach_ioconf_wbsio, cfdata_ioconf_wbsio);
956 #else
957 return 0;
958 #endif
959 case MODULE_CMD_FINI:
960 #ifdef _MODULE
961 return config_fini_component(cfdriver_ioconf_wbsio,
962 cfattach_ioconf_wbsio, cfdata_ioconf_wbsio);
963 #else
964 return 0;
965 #endif
966 default:
967 return ENOTTY;
968 }
969 }
970