xref: /netbsd-src/sys/dev/ic/gcscpcib.c (revision 2685996b0ecfa62e9cdfc698150a0a79d02d2e7f)
1 /* $NetBSD: gcscpcib.c,v 1.4 2021/04/24 23:36:55 thorpej Exp $ */
2 /* $OpenBSD: gcscpcib.c,v 1.6 2007/11/17 17:02:47 mbalmer Exp $	*/
3 
4 /*
5  * Copyright (c) 2008 Yojiro UO <yuo@nui.org>
6  * Copyright (c) 2007 Marc Balmer <mbalmer@openbsd.org>
7  * Copyright (c) 2007 Michael Shalayeff
8  * All rights reserved.
9  *
10  * Permission to use, copy, modify, and distribute this software for any
11  * purpose with or without fee is hereby granted, provided that the above
12  * copyright notice and this permission notice appear in all copies.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
19  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
20  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22 
23 /*
24  * AMD CS5535/CS5536 series LPC bridge also containing timer, watchdog and GPIO.
25  */
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: gcscpcib.c,v 1.4 2021/04/24 23:36:55 thorpej Exp $");
28 
29 #include "gpio.h"
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/device.h>
34 #include <sys/gpio.h>
35 #include <sys/sysctl.h>
36 #include <sys/timetc.h>
37 #include <sys/wdog.h>
38 
39 #include <sys/bus.h>
40 
41 #include <dev/gpio/gpiovar.h>
42 
43 #include <dev/sysmon/sysmonvar.h>
44 
45 #include <dev/ic/gcscpcibreg.h>
46 #include <dev/ic/gcscpcibvar.h>
47 
48 /* define if you need to select MFGPT for watchdog manually (0-5). */
49 /* #define AMD553X_WDT_FORCEUSEMFGPT 	0 */
50 /* select precision of watchdog timer (default value = 0.25sec (4Hz tick) */
51 #define	AMD553X_MFGPT_PRESCALE	AMD553X_MFGPT_DIV_8K /* 32K/8K = 4Hz */
52 
53 /* #define GCSCPCIB_DEBUG */
54 #ifdef GCSCPCIB_DEBUG
55 #define DPRINTF(x)	printf x
56 #else
57 #define DPRINTF(x)
58 #endif
59 
60 /* 1 bit replace (not support multiple bit)*/
61 #define AMD553X_MFGPTx_NR_DISABLE(x, bit) \
62 	( gcsc_wrmsr(AMD553X_MFGPT_NR, gcsc_rdmsr(AMD553X_MFGPT_NR) & ~((bit) << (x))) )
63 #define AMD553X_MFGPTx_NR_ENABLE(x, bit) \
64 	( gcsc_wrmsr(AMD553X_MFGPT_NR, gcsc_rdmsr(AMD553X_MFGPT_NR) | ((bit) << (x))) )
65 
66 /* caliculate watchdog timer setting */
67 #define	AMD553X_WDT_TICK (1<<(AMD553X_MFGPT_DIV_32K - AMD553X_MFGPT_PRESCALE))
68 #define AMD553X_WDT_COUNTMAX	(0xffff / AMD553X_WDT_TICK)
69 
70 static u_int	gcscpcib_get_timecount(struct timecounter *tc);
71 static int	gscspcib_scan_mfgpt(struct gcscpcib_softc *sc);
72 static void 	gscspcib_wdog_update(struct gcscpcib_softc *, uint16_t);
73 static int	gcscpcib_wdog_setmode(struct sysmon_wdog *smw);
74 static int	gcscpcib_wdog_tickle(struct sysmon_wdog *smw);
75 static void	gcscpcib_wdog_enable(struct gcscpcib_softc *sc);
76 static void	gcscpcib_wdog_disable(struct gcscpcib_softc *sc);
77 static void	gcscpcib_wdog_reset(struct gcscpcib_softc *sc);
78 
79 #if NGPIO > 0
80 static int	gcscpcib_gpio_pin_read(void *, int);
81 static void	gcscpcib_gpio_pin_write(void *, int, int);
82 static void	gcscpcib_gpio_pin_ctl(void *, int, int);
83 #endif
84 
85 void
86 gcscpcib_attach(device_t self, struct gcscpcib_softc *sc,
87     bus_space_tag_t iot, int flags)
88 {
89 	struct timecounter *tc = &sc->sc_timecounter;
90 	bus_addr_t wdtbase;
91 	int wdt = 0, gpio = 0;
92 #if NGPIO > 0
93 	struct gpiobus_attach_args gba;
94 	bus_addr_t gpiobase;
95 	int i;
96 #endif
97 
98 	sc->sc_iot = iot;
99 #if NGPIO > 0
100 	sc->sc_gpio_iot = iot;
101 #endif
102 
103 	/* Attach the CS553[56] timer */
104 	tc->tc_get_timecount = gcscpcib_get_timecount;
105 	tc->tc_counter_mask = 0xffffffff;
106 	tc->tc_frequency = 3579545;
107 	tc->tc_name = device_xname(self);
108 	tc->tc_quality = 1000;
109 	tc->tc_priv = sc;
110 	tc_init(tc);
111 
112 	if (flags & GCSCATTACH_NO_WDT)
113 		goto gpio;
114 
115 	/* Attach the watchdog timer */
116 	wdtbase = gcsc_rdmsr(MSR_LBAR_MFGPT) & 0xffff;
117 	if (bus_space_map(sc->sc_iot, wdtbase, 64, 0, &sc->sc_ioh)) {
118 		aprint_error_dev(self, "can't map memory space for WDT\n");
119 	} else {
120 		/* select a MFGPT timer for watchdog counter */
121 		if (!gscspcib_scan_mfgpt(sc)) {
122 			aprint_error_dev(self, "can't alloc an MFGPT for WDT\n");
123 			goto gpio;
124 		}
125 		/*
126 		 * Note: MFGPGx_SETUP register is write once register
127  		 * except CNT_EN and CMP[12]EV bit.
128 		 */
129 		bus_space_write_2(sc->sc_iot, sc->sc_ioh,
130 			AMD553X_MFGPTX_SETUP(sc->sc_wdt_mfgpt),
131 		    	AMD553X_MFGPT_CMP2EV | AMD553X_MFGPT_CMP2 |
132 		    	AMD553X_MFGPT_PRESCALE);
133 
134 		/* disable watchdog action */
135 		AMD553X_MFGPTx_NR_DISABLE(sc->sc_wdt_mfgpt,
136 			AMD553X_MFGPT0_C2_NMIM);
137 		AMD553X_MFGPTx_NR_DISABLE(sc->sc_wdt_mfgpt,
138 			AMD553X_MFGPT0_C2_RSTEN);
139 
140 		sc->sc_smw.smw_name = device_xname(self);
141 		sc->sc_smw.smw_cookie = sc;
142 		sc->sc_smw.smw_setmode = gcscpcib_wdog_setmode;
143 		sc->sc_smw.smw_tickle = gcscpcib_wdog_tickle;
144 		sc->sc_smw.smw_period = 32;
145 		aprint_normal_dev(self, "Watchdog Timer via MFGPT%d",
146 			 sc->sc_wdt_mfgpt);
147 		wdt = 1;
148 	}
149 
150 gpio:
151 #if NGPIO > 0
152 	/* map GPIO I/O space */
153 	gpiobase = gcsc_rdmsr(MSR_LBAR_GPIO) & 0xffff;
154 	if (!bus_space_map(sc->sc_gpio_iot, gpiobase, 0xff, 0,
155 	    &sc->sc_gpio_ioh)) {
156 		if (wdt)
157 			aprint_normal(", GPIO");
158 		else
159 			aprint_normal_dev(self, "GPIO");
160 
161 		/* initialize pin array */
162 		for (i = 0; i < AMD553X_GPIO_NPINS; i++) {
163 			sc->sc_gpio_pins[i].pin_num = i;
164 			sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
165 			    GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN |
166 			    GPIO_PIN_PUSHPULL | GPIO_PIN_TRISTATE |
167 			    GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN |
168 			    GPIO_PIN_INVIN | GPIO_PIN_INVOUT;
169 
170 			/* read initial state */
171 			sc->sc_gpio_pins[i].pin_state =
172 			    gcscpcib_gpio_pin_read(sc, i);
173 		}
174 
175 		/* create controller tag */
176 		sc->sc_gpio_gc.gp_cookie = sc;
177 		sc->sc_gpio_gc.gp_pin_read = gcscpcib_gpio_pin_read;
178 		sc->sc_gpio_gc.gp_pin_write = gcscpcib_gpio_pin_write;
179 		sc->sc_gpio_gc.gp_pin_ctl = gcscpcib_gpio_pin_ctl;
180 
181 		gba.gba_gc = &sc->sc_gpio_gc;
182 		gba.gba_pins = sc->sc_gpio_pins;
183 		gba.gba_npins = AMD553X_GPIO_NPINS;
184 		gpio = 1;
185 	}
186 #endif
187 	if (wdt || gpio)
188 		aprint_normal("\n");
189 
190 #if NGPIO > 0
191 	/* Attach GPIO framework */
192 	if (gpio)
193                 config_found(self, &gba, gpiobus_print,
194 		    CFARG_IATTR, "gpiobus",
195 		    CFARG_EOL);
196 #endif
197 
198 	/* Register Watchdog timer to SMW */
199 	if (wdt) {
200 		if (sysmon_wdog_register(&sc->sc_smw) != 0)
201 			aprint_error_dev(self,
202 			    "cannot register wdog with sysmon\n");
203 	}
204 }
205 
206 static u_int
207 gcscpcib_get_timecount(struct timecounter *tc)
208 {
209         return gcsc_rdmsr(AMD553X_TMC);
210 }
211 
212 /* Watchdog timer support functions */
213 static int
214 gscspcib_scan_mfgpt(struct gcscpcib_softc *sc)
215 {
216 	int i;
217 
218 #ifdef AMD553X_WDT_FORCEUSEMFGPT
219 	if (AMD553X_WDT_FORCEUSEMFGPT >= AMD553X_MFGPT_MAX)
220 		return 0;
221 	sc->sc_wdt_mfgpt = AMD553X_WDT_FORCEUSEMFGPT;
222 	return 1;
223 #endif /* AMD553X_WDT_FORCEUSEMFGPT */
224 
225 	for (i = 0; i < AMD553X_MFGPT_MAX; i++){
226 		if (bus_space_read_2(sc->sc_iot, sc->sc_ioh,
227 		    AMD553X_MFGPTX_SETUP(i)) == 0) {
228 			/* found unused MFGPT, use it. */
229 			sc->sc_wdt_mfgpt = i;
230 			return 1;
231 		}
232 	}
233 	/* no MFGPT for WDT found */
234 	return 0;
235 }
236 
237 
238 static void
239 gscspcib_wdog_update(struct gcscpcib_softc *sc, uint16_t count)
240 {
241 #ifdef GCSCPCIB_DEBUG
242 	uint16_t cnt;
243 	cnt = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
244 		AMD553X_MFGPTX_CNT(sc->sc_wdt_mfgpt));
245 #endif
246 	if (count > AMD553X_WDT_COUNTMAX)
247 		count = AMD553X_WDT_COUNTMAX;
248 	/*
249 	 * CS553X databook recommend following sequence to re-initialize
250 	 * the counter and compare value. (See p165 on CS5536 databook)
251 	 * 1: suspend counter: clear counter enable bit to 0
252 	 * 2: reset (and NMI, if need) enable bit in MSRs
253 	 * 3: update counter & clear event flags
254 	 * 4: resume (2) operation
255 	 * 5: re-enable counter
256 	 */
257 	bus_space_write_2(sc->sc_iot, sc->sc_ioh,
258 		AMD553X_MFGPTX_SETUP(sc->sc_wdt_mfgpt), 0);
259 	AMD553X_MFGPTx_NR_DISABLE(sc->sc_wdt_mfgpt, AMD553X_MFGPT0_C2_RSTEN);
260 	bus_space_write_2(sc->sc_iot, sc->sc_ioh,
261 		AMD553X_MFGPTX_CNT(sc->sc_wdt_mfgpt), count);
262 	bus_space_write_2(sc->sc_iot, sc->sc_ioh,
263 		AMD553X_MFGPTX_SETUP(sc->sc_wdt_mfgpt),
264 			AMD553X_MFGPT_CMP1 | AMD553X_MFGPT_CMP2);
265 	AMD553X_MFGPTx_NR_ENABLE(sc->sc_wdt_mfgpt, AMD553X_MFGPT0_C2_RSTEN);
266 	bus_space_write_2(sc->sc_iot, sc->sc_ioh,
267 		AMD553X_MFGPTX_SETUP(sc->sc_wdt_mfgpt),
268 	    	AMD553X_MFGPT_CNT_EN | AMD553X_MFGPT_CMP2);
269 
270 	DPRINTF(("%s: MFGPT%d_CNT= %d -> %d (expect: %d), MFGPT_NR=%#.8x\n",
271 		__func__, sc->sc_wdt_mfgpt, cnt,
272 		bus_space_read_2(sc->sc_iot, sc->sc_ioh,
273 			 AMD553X_MFGPTX_CNT(sc->sc_wdt_mfgpt)), count,
274 		(uint32_t)(gcsc_rdmsr(AMD553X_MFGPT_NR))));
275 }
276 
277 static void
278 gcscpcib_wdog_disable(struct gcscpcib_softc *sc)
279 {
280 	/*
281 	 * stop counter and reset counter value
282 	 * Note: as the MFGPTx_SETUP is write once register, the prescaler
283 	 * setting, clock select and compare mode are kept till reset.
284 	 */
285 	gscspcib_wdog_update(sc, 0);
286 	bus_space_write_2(sc->sc_iot, sc->sc_ioh,
287 		AMD553X_MFGPTX_SETUP(sc->sc_wdt_mfgpt), 0);
288 
289 	/* disable watchdog action */
290 	DPRINTF(("%s: disable watchdog action\n", __func__));
291 	AMD553X_MFGPTx_NR_DISABLE(sc->sc_wdt_mfgpt, AMD553X_MFGPT0_C2_RSTEN);
292 }
293 
294 static void
295 gcscpcib_wdog_enable(struct gcscpcib_softc *sc)
296 {
297 	int period = sc->sc_smw.smw_period;
298 
299 	/* clear recent event flag and counter value, and start counter */
300 	gcscpcib_wdog_reset(sc);
301 	/* set watchdog timer limit, counter tick is 0.5sec */
302 	bus_space_write_2(sc->sc_iot, sc->sc_ioh,
303 		AMD553X_MFGPTX_CMP2(sc->sc_wdt_mfgpt),
304 			period * AMD553X_WDT_TICK);
305 
306 	/* enable watchdog action */
307 	DPRINTF(("%s: enable watchdog action. (MFGPT0_CMP2= %d)", __func__,
308 	        bus_space_read_2(sc->sc_iot, sc->sc_ioh,
309 			 AMD553X_MFGPTX_CMP2(sc->sc_wdt_mfgpt))));
310 	AMD553X_MFGPTx_NR_ENABLE(sc->sc_wdt_mfgpt, AMD553X_MFGPT0_C2_RSTEN);
311 	DPRINTF((" AMD553X_MFGPT_NR 0x%" PRIx64 "\n", gcsc_rdmsr(AMD553X_MFGPT_NR)));
312 }
313 
314 static int
315 gcscpcib_wdog_setmode(struct sysmon_wdog *smw)
316 {
317 	struct gcscpcib_softc *sc = smw->smw_cookie;
318 
319 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
320 		gcscpcib_wdog_disable(sc);
321 		return 0;
322 	}
323 
324 	if (smw->smw_period == WDOG_PERIOD_DEFAULT)
325 		smw->smw_period = 32;
326 	else if (smw->smw_period > AMD553X_WDT_COUNTMAX) /* too big */
327 		return EINVAL;
328 
329 	gcscpcib_wdog_enable(sc);
330 
331 	return 0;
332 }
333 
334 static void
335 gcscpcib_wdog_reset(struct gcscpcib_softc *sc)
336 {
337 	/* reset counter value */
338 	gscspcib_wdog_update(sc, 0);
339 	/* start counter & clear recent event of CMP2 */
340 	bus_space_write_2(sc->sc_iot, sc->sc_ioh,
341 		AMD553X_MFGPTX_SETUP(sc->sc_wdt_mfgpt),
342 	   	AMD553X_MFGPT_CNT_EN | AMD553X_MFGPT_CMP2);
343 }
344 
345 static int
346 gcscpcib_wdog_tickle(struct sysmon_wdog *smw)
347 {
348 	struct gcscpcib_softc *sc = smw->smw_cookie;
349 
350 	DPRINTF(("%s: update watchdog timer\n", __func__));
351 	gcscpcib_wdog_reset(sc);
352 	return 0;
353 }
354 
355 #if NGPIO > 0
356 /* GPIO support functions */
357 static int
358 gcscpcib_gpio_pin_read(void *arg, int pin)
359 {
360 	struct gcscpcib_softc *sc = arg;
361 	uint32_t data;
362 	int reg;
363 
364 	reg = AMD553X_GPIO_OUT_VAL;
365 	if (pin > 15) {
366 		pin &= 0x0f;
367 		reg += AMD553X_GPIOH_OFFSET;
368 	}
369 	data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg);
370 
371 	return data & 1 << pin ? GPIO_PIN_HIGH : GPIO_PIN_LOW;
372 }
373 
374 static void
375 gcscpcib_gpio_pin_write(void *arg, int pin, int value)
376 {
377 	struct gcscpcib_softc *sc = arg;
378 	uint32_t data;
379 	int reg;
380 
381 	reg = AMD553X_GPIO_OUT_VAL;
382 	if (pin > 15) {
383 		pin &= 0x0f;
384 		reg += AMD553X_GPIOH_OFFSET;
385 	}
386 	if (value == 1)
387 		data = 1 << pin;
388 	else
389 		data = 1 << (pin + 16);
390 
391 	bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg, data);
392 }
393 
394 static void
395 gcscpcib_gpio_pin_ctl(void *arg, int pin, int flags)
396 {
397 	struct gcscpcib_softc *sc = arg;
398 	int n, reg[7], val[7], nreg = 0, off = 0;
399 
400 	if (pin > 15) {
401 		pin &= 0x0f;
402 		off = AMD553X_GPIOH_OFFSET;
403 	}
404 
405 	reg[nreg] = AMD553X_GPIO_IN_EN + off;
406 	if (flags & GPIO_PIN_INPUT)
407 		val[nreg++] = 1 << pin;
408 	else
409 		val[nreg++] = 1 << (pin + 16);
410 
411 	reg[nreg] = AMD553X_GPIO_OUT_EN + off;
412 	if (flags & GPIO_PIN_OUTPUT)
413 		val[nreg++] = 1 << pin;
414 	else
415 		val[nreg++] = 1 << (pin + 16);
416 
417 	reg[nreg] = AMD553X_GPIO_OD_EN + off;
418 	if (flags & GPIO_PIN_OPENDRAIN)
419 		val[nreg++] = 1 << pin;
420 	else
421 		val[nreg++] = 1 << (pin + 16);
422 
423 	reg[nreg] = AMD553X_GPIO_PU_EN + off;
424 	if (flags & GPIO_PIN_PULLUP)
425 		val[nreg++] = 1 << pin;
426 	else
427 		val[nreg++] = 1 << (pin + 16);
428 
429 	reg[nreg] = AMD553X_GPIO_PD_EN + off;
430 	if (flags & GPIO_PIN_PULLDOWN)
431 		val[nreg++] = 1 << pin;
432 	else
433 		val[nreg++] = 1 << (pin + 16);
434 
435 	reg[nreg] = AMD553X_GPIO_IN_INVRT_EN + off;
436 	if (flags & GPIO_PIN_INVIN)
437 		val[nreg++] = 1 << pin;
438 	else
439 		val[nreg++] = 1 << (pin + 16);
440 
441 	reg[nreg] = AMD553X_GPIO_OUT_INVRT_EN + off;
442 	if (flags & GPIO_PIN_INVOUT)
443 		val[nreg++] = 1 << pin;
444 	else
445 		val[nreg++] = 1 << (pin + 16);
446 
447 	/* set flags */
448 	for (n = 0; n < nreg; n++)
449 		bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg[n],
450 		    val[n]);
451 }
452 #endif /* NGPIO > 0 */
453 
454