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