gcscpcib.c revision 1.5 1 1.5 thorpej /* $NetBSD: gcscpcib.c,v 1.5 2021/08/07 16:19:12 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.5 thorpej __KERNEL_RCSID(0, "$NetBSD: gcscpcib.c,v 1.5 2021/08/07 16:19:12 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.4 thorpej config_found(self, &gba, gpiobus_print,
194 1.5 thorpej CFARGS(.iattr = "gpiobus"));
195 1.1 bouyer #endif
196 1.1 bouyer
197 1.1 bouyer /* Register Watchdog timer to SMW */
198 1.1 bouyer if (wdt) {
199 1.1 bouyer if (sysmon_wdog_register(&sc->sc_smw) != 0)
200 1.1 bouyer aprint_error_dev(self,
201 1.1 bouyer "cannot register wdog with sysmon\n");
202 1.1 bouyer }
203 1.1 bouyer }
204 1.1 bouyer
205 1.1 bouyer static u_int
206 1.1 bouyer gcscpcib_get_timecount(struct timecounter *tc)
207 1.1 bouyer {
208 1.1 bouyer return gcsc_rdmsr(AMD553X_TMC);
209 1.1 bouyer }
210 1.1 bouyer
211 1.1 bouyer /* Watchdog timer support functions */
212 1.1 bouyer static int
213 1.1 bouyer gscspcib_scan_mfgpt(struct gcscpcib_softc *sc)
214 1.1 bouyer {
215 1.1 bouyer int i;
216 1.1 bouyer
217 1.1 bouyer #ifdef AMD553X_WDT_FORCEUSEMFGPT
218 1.1 bouyer if (AMD553X_WDT_FORCEUSEMFGPT >= AMD553X_MFGPT_MAX)
219 1.1 bouyer return 0;
220 1.1 bouyer sc->sc_wdt_mfgpt = AMD553X_WDT_FORCEUSEMFGPT;
221 1.1 bouyer return 1;
222 1.1 bouyer #endif /* AMD553X_WDT_FORCEUSEMFGPT */
223 1.1 bouyer
224 1.1 bouyer for (i = 0; i < AMD553X_MFGPT_MAX; i++){
225 1.1 bouyer if (bus_space_read_2(sc->sc_iot, sc->sc_ioh,
226 1.1 bouyer AMD553X_MFGPTX_SETUP(i)) == 0) {
227 1.1 bouyer /* found unused MFGPT, use it. */
228 1.1 bouyer sc->sc_wdt_mfgpt = i;
229 1.1 bouyer return 1;
230 1.1 bouyer }
231 1.1 bouyer }
232 1.1 bouyer /* no MFGPT for WDT found */
233 1.1 bouyer return 0;
234 1.1 bouyer }
235 1.1 bouyer
236 1.1 bouyer
237 1.1 bouyer static void
238 1.1 bouyer gscspcib_wdog_update(struct gcscpcib_softc *sc, uint16_t count)
239 1.1 bouyer {
240 1.1 bouyer #ifdef GCSCPCIB_DEBUG
241 1.1 bouyer uint16_t cnt;
242 1.1 bouyer cnt = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
243 1.1 bouyer AMD553X_MFGPTX_CNT(sc->sc_wdt_mfgpt));
244 1.1 bouyer #endif
245 1.1 bouyer if (count > AMD553X_WDT_COUNTMAX)
246 1.1 bouyer count = AMD553X_WDT_COUNTMAX;
247 1.1 bouyer /*
248 1.1 bouyer * CS553X databook recommend following sequence to re-initialize
249 1.1 bouyer * the counter and compare value. (See p165 on CS5536 databook)
250 1.1 bouyer * 1: suspend counter: clear counter enable bit to 0
251 1.1 bouyer * 2: reset (and NMI, if need) enable bit in MSRs
252 1.1 bouyer * 3: update counter & clear event flags
253 1.1 bouyer * 4: resume (2) operation
254 1.1 bouyer * 5: re-enable counter
255 1.1 bouyer */
256 1.1 bouyer bus_space_write_2(sc->sc_iot, sc->sc_ioh,
257 1.1 bouyer AMD553X_MFGPTX_SETUP(sc->sc_wdt_mfgpt), 0);
258 1.1 bouyer AMD553X_MFGPTx_NR_DISABLE(sc->sc_wdt_mfgpt, AMD553X_MFGPT0_C2_RSTEN);
259 1.1 bouyer bus_space_write_2(sc->sc_iot, sc->sc_ioh,
260 1.1 bouyer AMD553X_MFGPTX_CNT(sc->sc_wdt_mfgpt), count);
261 1.1 bouyer bus_space_write_2(sc->sc_iot, sc->sc_ioh,
262 1.1 bouyer AMD553X_MFGPTX_SETUP(sc->sc_wdt_mfgpt),
263 1.1 bouyer AMD553X_MFGPT_CMP1 | AMD553X_MFGPT_CMP2);
264 1.1 bouyer AMD553X_MFGPTx_NR_ENABLE(sc->sc_wdt_mfgpt, AMD553X_MFGPT0_C2_RSTEN);
265 1.1 bouyer bus_space_write_2(sc->sc_iot, sc->sc_ioh,
266 1.1 bouyer AMD553X_MFGPTX_SETUP(sc->sc_wdt_mfgpt),
267 1.1 bouyer AMD553X_MFGPT_CNT_EN | AMD553X_MFGPT_CMP2);
268 1.1 bouyer
269 1.1 bouyer DPRINTF(("%s: MFGPT%d_CNT= %d -> %d (expect: %d), MFGPT_NR=%#.8x\n",
270 1.1 bouyer __func__, sc->sc_wdt_mfgpt, cnt,
271 1.1 bouyer bus_space_read_2(sc->sc_iot, sc->sc_ioh,
272 1.1 bouyer AMD553X_MFGPTX_CNT(sc->sc_wdt_mfgpt)), count,
273 1.1 bouyer (uint32_t)(gcsc_rdmsr(AMD553X_MFGPT_NR))));
274 1.1 bouyer }
275 1.1 bouyer
276 1.1 bouyer static void
277 1.1 bouyer gcscpcib_wdog_disable(struct gcscpcib_softc *sc)
278 1.1 bouyer {
279 1.1 bouyer /*
280 1.1 bouyer * stop counter and reset counter value
281 1.1 bouyer * Note: as the MFGPTx_SETUP is write once register, the prescaler
282 1.1 bouyer * setting, clock select and compare mode are kept till reset.
283 1.1 bouyer */
284 1.1 bouyer gscspcib_wdog_update(sc, 0);
285 1.1 bouyer bus_space_write_2(sc->sc_iot, sc->sc_ioh,
286 1.1 bouyer AMD553X_MFGPTX_SETUP(sc->sc_wdt_mfgpt), 0);
287 1.1 bouyer
288 1.1 bouyer /* disable watchdog action */
289 1.1 bouyer DPRINTF(("%s: disable watchdog action\n", __func__));
290 1.1 bouyer AMD553X_MFGPTx_NR_DISABLE(sc->sc_wdt_mfgpt, AMD553X_MFGPT0_C2_RSTEN);
291 1.1 bouyer }
292 1.1 bouyer
293 1.1 bouyer static void
294 1.1 bouyer gcscpcib_wdog_enable(struct gcscpcib_softc *sc)
295 1.1 bouyer {
296 1.1 bouyer int period = sc->sc_smw.smw_period;
297 1.1 bouyer
298 1.1 bouyer /* clear recent event flag and counter value, and start counter */
299 1.1 bouyer gcscpcib_wdog_reset(sc);
300 1.1 bouyer /* set watchdog timer limit, counter tick is 0.5sec */
301 1.1 bouyer bus_space_write_2(sc->sc_iot, sc->sc_ioh,
302 1.1 bouyer AMD553X_MFGPTX_CMP2(sc->sc_wdt_mfgpt),
303 1.1 bouyer period * AMD553X_WDT_TICK);
304 1.1 bouyer
305 1.1 bouyer /* enable watchdog action */
306 1.2 bouyer DPRINTF(("%s: enable watchdog action. (MFGPT0_CMP2= %d)", __func__,
307 1.1 bouyer bus_space_read_2(sc->sc_iot, sc->sc_ioh,
308 1.1 bouyer AMD553X_MFGPTX_CMP2(sc->sc_wdt_mfgpt))));
309 1.1 bouyer AMD553X_MFGPTx_NR_ENABLE(sc->sc_wdt_mfgpt, AMD553X_MFGPT0_C2_RSTEN);
310 1.2 bouyer DPRINTF((" AMD553X_MFGPT_NR 0x%" PRIx64 "\n", gcsc_rdmsr(AMD553X_MFGPT_NR)));
311 1.1 bouyer }
312 1.1 bouyer
313 1.1 bouyer static int
314 1.1 bouyer gcscpcib_wdog_setmode(struct sysmon_wdog *smw)
315 1.1 bouyer {
316 1.1 bouyer struct gcscpcib_softc *sc = smw->smw_cookie;
317 1.1 bouyer
318 1.1 bouyer if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
319 1.1 bouyer gcscpcib_wdog_disable(sc);
320 1.1 bouyer return 0;
321 1.1 bouyer }
322 1.1 bouyer
323 1.1 bouyer if (smw->smw_period == WDOG_PERIOD_DEFAULT)
324 1.1 bouyer smw->smw_period = 32;
325 1.1 bouyer else if (smw->smw_period > AMD553X_WDT_COUNTMAX) /* too big */
326 1.1 bouyer return EINVAL;
327 1.1 bouyer
328 1.1 bouyer gcscpcib_wdog_enable(sc);
329 1.1 bouyer
330 1.1 bouyer return 0;
331 1.1 bouyer }
332 1.1 bouyer
333 1.1 bouyer static void
334 1.1 bouyer gcscpcib_wdog_reset(struct gcscpcib_softc *sc)
335 1.1 bouyer {
336 1.1 bouyer /* reset counter value */
337 1.1 bouyer gscspcib_wdog_update(sc, 0);
338 1.1 bouyer /* start counter & clear recent event of CMP2 */
339 1.1 bouyer bus_space_write_2(sc->sc_iot, sc->sc_ioh,
340 1.1 bouyer AMD553X_MFGPTX_SETUP(sc->sc_wdt_mfgpt),
341 1.1 bouyer AMD553X_MFGPT_CNT_EN | AMD553X_MFGPT_CMP2);
342 1.1 bouyer }
343 1.1 bouyer
344 1.1 bouyer static int
345 1.1 bouyer gcscpcib_wdog_tickle(struct sysmon_wdog *smw)
346 1.1 bouyer {
347 1.1 bouyer struct gcscpcib_softc *sc = smw->smw_cookie;
348 1.1 bouyer
349 1.1 bouyer DPRINTF(("%s: update watchdog timer\n", __func__));
350 1.1 bouyer gcscpcib_wdog_reset(sc);
351 1.1 bouyer return 0;
352 1.1 bouyer }
353 1.1 bouyer
354 1.1 bouyer #if NGPIO > 0
355 1.1 bouyer /* GPIO support functions */
356 1.1 bouyer static int
357 1.1 bouyer gcscpcib_gpio_pin_read(void *arg, int pin)
358 1.1 bouyer {
359 1.1 bouyer struct gcscpcib_softc *sc = arg;
360 1.1 bouyer uint32_t data;
361 1.1 bouyer int reg;
362 1.1 bouyer
363 1.1 bouyer reg = AMD553X_GPIO_OUT_VAL;
364 1.1 bouyer if (pin > 15) {
365 1.1 bouyer pin &= 0x0f;
366 1.1 bouyer reg += AMD553X_GPIOH_OFFSET;
367 1.1 bouyer }
368 1.1 bouyer data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg);
369 1.1 bouyer
370 1.1 bouyer return data & 1 << pin ? GPIO_PIN_HIGH : GPIO_PIN_LOW;
371 1.1 bouyer }
372 1.1 bouyer
373 1.1 bouyer static void
374 1.1 bouyer gcscpcib_gpio_pin_write(void *arg, int pin, int value)
375 1.1 bouyer {
376 1.1 bouyer struct gcscpcib_softc *sc = arg;
377 1.1 bouyer uint32_t data;
378 1.1 bouyer int reg;
379 1.1 bouyer
380 1.1 bouyer reg = AMD553X_GPIO_OUT_VAL;
381 1.1 bouyer if (pin > 15) {
382 1.1 bouyer pin &= 0x0f;
383 1.1 bouyer reg += AMD553X_GPIOH_OFFSET;
384 1.1 bouyer }
385 1.1 bouyer if (value == 1)
386 1.1 bouyer data = 1 << pin;
387 1.1 bouyer else
388 1.1 bouyer data = 1 << (pin + 16);
389 1.1 bouyer
390 1.1 bouyer bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg, data);
391 1.1 bouyer }
392 1.1 bouyer
393 1.1 bouyer static void
394 1.1 bouyer gcscpcib_gpio_pin_ctl(void *arg, int pin, int flags)
395 1.1 bouyer {
396 1.1 bouyer struct gcscpcib_softc *sc = arg;
397 1.1 bouyer int n, reg[7], val[7], nreg = 0, off = 0;
398 1.1 bouyer
399 1.1 bouyer if (pin > 15) {
400 1.1 bouyer pin &= 0x0f;
401 1.1 bouyer off = AMD553X_GPIOH_OFFSET;
402 1.1 bouyer }
403 1.1 bouyer
404 1.1 bouyer reg[nreg] = AMD553X_GPIO_IN_EN + off;
405 1.1 bouyer if (flags & GPIO_PIN_INPUT)
406 1.1 bouyer val[nreg++] = 1 << pin;
407 1.1 bouyer else
408 1.1 bouyer val[nreg++] = 1 << (pin + 16);
409 1.1 bouyer
410 1.1 bouyer reg[nreg] = AMD553X_GPIO_OUT_EN + off;
411 1.1 bouyer if (flags & GPIO_PIN_OUTPUT)
412 1.1 bouyer val[nreg++] = 1 << pin;
413 1.1 bouyer else
414 1.1 bouyer val[nreg++] = 1 << (pin + 16);
415 1.1 bouyer
416 1.1 bouyer reg[nreg] = AMD553X_GPIO_OD_EN + off;
417 1.1 bouyer if (flags & GPIO_PIN_OPENDRAIN)
418 1.1 bouyer val[nreg++] = 1 << pin;
419 1.1 bouyer else
420 1.1 bouyer val[nreg++] = 1 << (pin + 16);
421 1.1 bouyer
422 1.1 bouyer reg[nreg] = AMD553X_GPIO_PU_EN + off;
423 1.1 bouyer if (flags & GPIO_PIN_PULLUP)
424 1.1 bouyer val[nreg++] = 1 << pin;
425 1.1 bouyer else
426 1.1 bouyer val[nreg++] = 1 << (pin + 16);
427 1.1 bouyer
428 1.1 bouyer reg[nreg] = AMD553X_GPIO_PD_EN + off;
429 1.1 bouyer if (flags & GPIO_PIN_PULLDOWN)
430 1.1 bouyer val[nreg++] = 1 << pin;
431 1.1 bouyer else
432 1.1 bouyer val[nreg++] = 1 << (pin + 16);
433 1.1 bouyer
434 1.1 bouyer reg[nreg] = AMD553X_GPIO_IN_INVRT_EN + off;
435 1.1 bouyer if (flags & GPIO_PIN_INVIN)
436 1.1 bouyer val[nreg++] = 1 << pin;
437 1.1 bouyer else
438 1.1 bouyer val[nreg++] = 1 << (pin + 16);
439 1.1 bouyer
440 1.1 bouyer reg[nreg] = AMD553X_GPIO_OUT_INVRT_EN + off;
441 1.1 bouyer if (flags & GPIO_PIN_INVOUT)
442 1.1 bouyer val[nreg++] = 1 << pin;
443 1.1 bouyer else
444 1.1 bouyer val[nreg++] = 1 << (pin + 16);
445 1.1 bouyer
446 1.1 bouyer /* set flags */
447 1.1 bouyer for (n = 0; n < nreg; n++)
448 1.1 bouyer bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg[n],
449 1.1 bouyer val[n]);
450 1.1 bouyer }
451 1.1 bouyer #endif /* NGPIO > 0 */
452 1.1 bouyer
453