elan520.c revision 1.8.16.5 1 1.8.16.5 yamt /* $NetBSD: elan520.c,v 1.8.16.5 2008/03/17 09:14:20 yamt Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.1 thorpej * must display the following acknowledgement:
20 1.1 thorpej * This product includes software developed by the NetBSD
21 1.1 thorpej * Foundation, Inc. and its contributors.
22 1.1 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 thorpej * contributors may be used to endorse or promote products derived
24 1.1 thorpej * from this software without specific prior written permission.
25 1.1 thorpej *
26 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.1 thorpej */
38 1.1 thorpej
39 1.1 thorpej /*
40 1.1 thorpej * Device driver for the AMD Elan SC520 System Controller. This attaches
41 1.1 thorpej * where the "pchb" driver might normally attach, and provides support for
42 1.1 thorpej * extra features on the SC520, such as the watchdog timer and GPIO.
43 1.1 thorpej *
44 1.1 thorpej * Information about the GP bus echo bug work-around is from code posted
45 1.1 thorpej * to the "soekris-tech" mailing list by Jasper Wallace.
46 1.1 thorpej */
47 1.1 thorpej
48 1.1 thorpej #include <sys/cdefs.h>
49 1.1 thorpej
50 1.8.16.5 yamt __KERNEL_RCSID(0, "$NetBSD: elan520.c,v 1.8.16.5 2008/03/17 09:14:20 yamt Exp $");
51 1.1 thorpej
52 1.1 thorpej #include <sys/param.h>
53 1.1 thorpej #include <sys/systm.h>
54 1.8.16.3 yamt #include <sys/time.h>
55 1.1 thorpej #include <sys/device.h>
56 1.8.16.1 yamt #include <sys/gpio.h>
57 1.8.16.3 yamt #include <sys/mutex.h>
58 1.8.16.3 yamt #include <sys/wdog.h>
59 1.1 thorpej
60 1.5 thorpej #include <uvm/uvm_extern.h>
61 1.5 thorpej
62 1.1 thorpej #include <machine/bus.h>
63 1.1 thorpej
64 1.1 thorpej #include <dev/pci/pcivar.h>
65 1.1 thorpej
66 1.1 thorpej #include <dev/pci/pcidevs.h>
67 1.1 thorpej
68 1.8.16.1 yamt #include "gpio.h"
69 1.8.16.1 yamt #if NGPIO > 0
70 1.8.16.1 yamt #include <dev/gpio/gpiovar.h>
71 1.8.16.1 yamt #endif
72 1.8.16.1 yamt
73 1.1 thorpej #include <arch/i386/pci/elan520reg.h>
74 1.1 thorpej
75 1.1 thorpej #include <dev/sysmon/sysmonvar.h>
76 1.1 thorpej
77 1.8.16.3 yamt #define ELAN_IRQ 1
78 1.8.16.4 yamt #define PG0_PROT_SIZE PAGE_SIZE
79 1.8.16.3 yamt
80 1.1 thorpej struct elansc_softc {
81 1.1 thorpej struct device sc_dev;
82 1.8.16.3 yamt device_t sc_par;
83 1.8.16.3 yamt device_t sc_pex;
84 1.8.16.3 yamt
85 1.8.16.3 yamt pci_chipset_tag_t sc_pc;
86 1.8.16.3 yamt pcitag_t sc_tag;
87 1.1 thorpej bus_space_tag_t sc_memt;
88 1.1 thorpej bus_space_handle_t sc_memh;
89 1.1 thorpej int sc_echobug;
90 1.1 thorpej
91 1.8.16.3 yamt kmutex_t sc_mtx;
92 1.8.16.3 yamt
93 1.1 thorpej struct sysmon_wdog sc_smw;
94 1.8.16.3 yamt void *sc_eih;
95 1.8.16.3 yamt void *sc_pih;
96 1.8.16.3 yamt void *sc_sh;
97 1.8.16.3 yamt uint8_t sc_mpicmode;
98 1.8.16.3 yamt uint8_t sc_picicr;
99 1.8.16.4 yamt int sc_pg0par;
100 1.8.16.3 yamt int sc_textpar;
101 1.8.16.1 yamt #if NGPIO > 0
102 1.8.16.1 yamt /* GPIO interface */
103 1.8.16.1 yamt struct gpio_chipset_tag sc_gpio_gc;
104 1.8.16.1 yamt gpio_pin_t sc_gpio_pins[ELANSC_PIO_NPINS];
105 1.8.16.1 yamt #endif
106 1.1 thorpej };
107 1.1 thorpej
108 1.8.16.3 yamt int elansc_wpvnmi = 1;
109 1.8.16.3 yamt int elansc_pcinmi = 1;
110 1.8.16.4 yamt int elansc_do_protect_pg0 = 1;
111 1.8.16.3 yamt
112 1.8.16.1 yamt #if NGPIO > 0
113 1.8.16.1 yamt static int elansc_gpio_pin_read(void *, int);
114 1.8.16.1 yamt static void elansc_gpio_pin_write(void *, int, int);
115 1.8.16.1 yamt static void elansc_gpio_pin_ctl(void *, int, int);
116 1.8.16.1 yamt #endif
117 1.8.16.1 yamt
118 1.8.16.3 yamt static void elansc_print_par(device_t, int, uint32_t);
119 1.8.16.3 yamt static void elanpex_intr_establish(device_t, struct elansc_softc *);
120 1.8.16.3 yamt static void elanpar_intr_establish(device_t, struct elansc_softc *);
121 1.8.16.3 yamt static void elanpex_intr_disestablish(struct elansc_softc *);
122 1.8.16.3 yamt static void elanpar_intr_disestablish(struct elansc_softc *);
123 1.8.16.5 yamt static bool elanpar_shutdown(device_t, int);
124 1.8.16.5 yamt static bool elanpex_shutdown(device_t, int);
125 1.8.16.3 yamt
126 1.8.16.3 yamt static void
127 1.8.16.3 yamt elansc_childdetached(device_t self, device_t child)
128 1.8.16.3 yamt {
129 1.8.16.3 yamt struct elansc_softc *sc = device_private(self);
130 1.8.16.3 yamt
131 1.8.16.3 yamt if (child == sc->sc_par)
132 1.8.16.3 yamt sc->sc_par = NULL;
133 1.8.16.3 yamt if (child == sc->sc_pex)
134 1.8.16.3 yamt sc->sc_pex = NULL;
135 1.8.16.3 yamt /* elansc does not presently keep a pointer to
136 1.8.16.3 yamt * the gpio, so there is nothing to do if it is detached.
137 1.8.16.3 yamt */
138 1.8.16.3 yamt }
139 1.8.16.3 yamt
140 1.1 thorpej static void
141 1.1 thorpej elansc_wdogctl_write(struct elansc_softc *sc, uint16_t val)
142 1.1 thorpej {
143 1.6 christos uint8_t echo_mode = 0; /* XXX: gcc */
144 1.1 thorpej
145 1.8.16.3 yamt KASSERT(mutex_owned(&sc->sc_mtx));
146 1.1 thorpej
147 1.1 thorpej /* Switch off GP bus echo mode if we need to. */
148 1.1 thorpej if (sc->sc_echobug) {
149 1.1 thorpej echo_mode = bus_space_read_1(sc->sc_memt, sc->sc_memh,
150 1.1 thorpej MMCR_GPECHO);
151 1.1 thorpej bus_space_write_1(sc->sc_memt, sc->sc_memh,
152 1.1 thorpej MMCR_GPECHO, echo_mode & ~GPECHO_GP_ECHO_ENB);
153 1.1 thorpej }
154 1.1 thorpej
155 1.1 thorpej /* Unlock the register. */
156 1.1 thorpej bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
157 1.1 thorpej WDTMRCTL_UNLOCK1);
158 1.1 thorpej bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
159 1.1 thorpej WDTMRCTL_UNLOCK2);
160 1.1 thorpej
161 1.1 thorpej /* Write the value. */
162 1.1 thorpej bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL, val);
163 1.1 thorpej
164 1.1 thorpej /* Switch GP bus echo mode back. */
165 1.1 thorpej if (sc->sc_echobug)
166 1.1 thorpej bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_GPECHO,
167 1.1 thorpej echo_mode);
168 1.1 thorpej }
169 1.1 thorpej
170 1.1 thorpej static void
171 1.1 thorpej elansc_wdogctl_reset(struct elansc_softc *sc)
172 1.1 thorpej {
173 1.7 christos uint8_t echo_mode = 0/* XXX: gcc */;
174 1.1 thorpej
175 1.8.16.3 yamt KASSERT(mutex_owned(&sc->sc_mtx));
176 1.1 thorpej
177 1.1 thorpej /* Switch off GP bus echo mode if we need to. */
178 1.1 thorpej if (sc->sc_echobug) {
179 1.1 thorpej echo_mode = bus_space_read_1(sc->sc_memt, sc->sc_memh,
180 1.1 thorpej MMCR_GPECHO);
181 1.1 thorpej bus_space_write_1(sc->sc_memt, sc->sc_memh,
182 1.1 thorpej MMCR_GPECHO, echo_mode & ~GPECHO_GP_ECHO_ENB);
183 1.1 thorpej }
184 1.1 thorpej
185 1.1 thorpej /* Reset the watchdog. */
186 1.1 thorpej bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
187 1.1 thorpej WDTMRCTL_RESET1);
188 1.1 thorpej bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
189 1.1 thorpej WDTMRCTL_RESET2);
190 1.1 thorpej
191 1.1 thorpej /* Switch GP bus echo mode back. */
192 1.1 thorpej if (sc->sc_echobug)
193 1.1 thorpej bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_GPECHO,
194 1.1 thorpej echo_mode);
195 1.1 thorpej }
196 1.1 thorpej
197 1.1 thorpej static const struct {
198 1.1 thorpej int period; /* whole seconds */
199 1.1 thorpej uint16_t exp; /* exponent select */
200 1.1 thorpej } elansc_wdog_periods[] = {
201 1.1 thorpej { 1, WDTMRCTL_EXP_SEL25 },
202 1.1 thorpej { 2, WDTMRCTL_EXP_SEL26 },
203 1.1 thorpej { 4, WDTMRCTL_EXP_SEL27 },
204 1.1 thorpej { 8, WDTMRCTL_EXP_SEL28 },
205 1.1 thorpej { 16, WDTMRCTL_EXP_SEL29 },
206 1.1 thorpej { 32, WDTMRCTL_EXP_SEL30 },
207 1.1 thorpej { 0, 0 },
208 1.1 thorpej };
209 1.1 thorpej
210 1.1 thorpej static int
211 1.8.16.3 yamt elansc_wdog_arm(struct elansc_softc *sc)
212 1.1 thorpej {
213 1.8.16.3 yamt struct sysmon_wdog *smw = &sc->sc_smw;
214 1.1 thorpej int i;
215 1.7 christos uint16_t exp_sel = 0; /* XXX: gcc */
216 1.1 thorpej
217 1.8.16.3 yamt KASSERT(mutex_owned(&sc->sc_mtx));
218 1.8.16.3 yamt
219 1.8.16.3 yamt if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
220 1.8.16.3 yamt smw->smw_period = 32;
221 1.8.16.3 yamt exp_sel = WDTMRCTL_EXP_SEL30;
222 1.1 thorpej } else {
223 1.8.16.3 yamt for (i = 0; elansc_wdog_periods[i].period != 0; i++) {
224 1.8.16.3 yamt if (elansc_wdog_periods[i].period ==
225 1.8.16.3 yamt smw->smw_period) {
226 1.8.16.3 yamt exp_sel = elansc_wdog_periods[i].exp;
227 1.8.16.3 yamt break;
228 1.1 thorpej }
229 1.1 thorpej }
230 1.8.16.3 yamt if (elansc_wdog_periods[i].period == 0)
231 1.8.16.3 yamt return EINVAL;
232 1.1 thorpej }
233 1.8.16.3 yamt elansc_wdogctl_write(sc, WDTMRCTL_ENB |
234 1.8.16.3 yamt WDTMRCTL_WRST_ENB | exp_sel);
235 1.8.16.3 yamt elansc_wdogctl_reset(sc);
236 1.8.16.3 yamt return 0;
237 1.8.16.3 yamt }
238 1.8.16.3 yamt
239 1.8.16.3 yamt static int
240 1.8.16.3 yamt elansc_wdog_setmode(struct sysmon_wdog *smw)
241 1.8.16.3 yamt {
242 1.8.16.3 yamt struct elansc_softc *sc = smw->smw_cookie;
243 1.8.16.3 yamt int rc = 0;
244 1.8.16.3 yamt
245 1.8.16.3 yamt mutex_enter(&sc->sc_mtx);
246 1.8.16.3 yamt
247 1.8.16.3 yamt if (!device_is_active(&sc->sc_dev))
248 1.8.16.3 yamt rc = EBUSY;
249 1.8.16.3 yamt else if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
250 1.8.16.3 yamt elansc_wdogctl_write(sc,
251 1.8.16.3 yamt WDTMRCTL_WRST_ENB | WDTMRCTL_EXP_SEL30);
252 1.8.16.3 yamt } else
253 1.8.16.3 yamt rc = elansc_wdog_arm(sc);
254 1.8.16.3 yamt
255 1.8.16.3 yamt mutex_exit(&sc->sc_mtx);
256 1.8.16.3 yamt return rc;
257 1.1 thorpej }
258 1.1 thorpej
259 1.1 thorpej static int
260 1.1 thorpej elansc_wdog_tickle(struct sysmon_wdog *smw)
261 1.1 thorpej {
262 1.1 thorpej struct elansc_softc *sc = smw->smw_cookie;
263 1.1 thorpej
264 1.8.16.3 yamt mutex_enter(&sc->sc_mtx);
265 1.1 thorpej elansc_wdogctl_reset(sc);
266 1.8.16.3 yamt mutex_exit(&sc->sc_mtx);
267 1.8.16.3 yamt return 0;
268 1.1 thorpej }
269 1.1 thorpej
270 1.1 thorpej static int
271 1.8.16.3 yamt elansc_match(device_t parent, struct cfdata *match, void *aux)
272 1.1 thorpej {
273 1.1 thorpej struct pci_attach_args *pa = aux;
274 1.1 thorpej
275 1.1 thorpej if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD &&
276 1.1 thorpej PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_SC520_SC)
277 1.1 thorpej return (10); /* beat pchb */
278 1.1 thorpej
279 1.1 thorpej return (0);
280 1.1 thorpej }
281 1.1 thorpej
282 1.1 thorpej static const char *elansc_speeds[] = {
283 1.1 thorpej "(reserved 00)",
284 1.1 thorpej "100MHz",
285 1.1 thorpej "133MHz",
286 1.1 thorpej "(reserved 11)",
287 1.1 thorpej };
288 1.1 thorpej
289 1.8.16.3 yamt static int
290 1.8.16.3 yamt elanpar_intr(void *arg)
291 1.8.16.3 yamt {
292 1.8.16.3 yamt struct elansc_softc *sc = arg;
293 1.8.16.3 yamt uint16_t wpvsta;
294 1.8.16.3 yamt unsigned win;
295 1.8.16.3 yamt uint32_t par;
296 1.8.16.3 yamt const char *wpvstr;
297 1.8.16.3 yamt
298 1.8.16.3 yamt wpvsta = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_WPVSTA);
299 1.8.16.3 yamt
300 1.8.16.3 yamt if ((wpvsta & MMCR_WPVSTA_WPV_STA) == 0)
301 1.8.16.3 yamt return 0;
302 1.8.16.3 yamt
303 1.8.16.3 yamt win = __SHIFTOUT(wpvsta, MMCR_WPVSTA_WPV_WINDOW);
304 1.8.16.3 yamt
305 1.8.16.3 yamt par = bus_space_read_4(sc->sc_memt, sc->sc_memh, MMCR_PAR(win));
306 1.8.16.3 yamt
307 1.8.16.3 yamt bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WPVSTA,
308 1.8.16.3 yamt MMCR_WPVSTA_WPV_STA);
309 1.8.16.3 yamt
310 1.8.16.3 yamt switch (wpvsta & MMCR_WPVSTA_WPV_MSTR) {
311 1.8.16.3 yamt case MMCR_WPVSTA_WPV_MSTR_CPU:
312 1.8.16.3 yamt wpvstr = "cpu";
313 1.8.16.3 yamt break;
314 1.8.16.3 yamt case MMCR_WPVSTA_WPV_MSTR_PCI:
315 1.8.16.3 yamt wpvstr = "pci";
316 1.8.16.3 yamt break;
317 1.8.16.3 yamt case MMCR_WPVSTA_WPV_MSTR_GP:
318 1.8.16.3 yamt wpvstr = "gp";
319 1.8.16.3 yamt break;
320 1.8.16.3 yamt default:
321 1.8.16.3 yamt wpvstr = "unknown";
322 1.8.16.3 yamt break;
323 1.8.16.3 yamt }
324 1.8.16.3 yamt aprint_error_dev(sc->sc_par,
325 1.8.16.3 yamt "%s violated write-protect window %u\n", wpvstr, win);
326 1.8.16.3 yamt elansc_print_par(sc->sc_par, win, par);
327 1.8.16.3 yamt return 0;
328 1.8.16.3 yamt }
329 1.8.16.3 yamt
330 1.8.16.3 yamt static int
331 1.8.16.3 yamt elanpex_intr(void *arg)
332 1.8.16.3 yamt {
333 1.8.16.3 yamt static struct {
334 1.8.16.3 yamt const char *string;
335 1.8.16.3 yamt bool nonfatal;
336 1.8.16.3 yamt } cmd[16] = {
337 1.8.16.3 yamt [0] = {.string = "not latched"}
338 1.8.16.3 yamt , [1] = {.string = "special cycle"}
339 1.8.16.3 yamt , [2] = {.string = "i/o read"}
340 1.8.16.3 yamt , [3] = {.string = "i/o write"}
341 1.8.16.3 yamt , [4] = {.string = "4"}
342 1.8.16.3 yamt , [5] = {.string = "5"}
343 1.8.16.3 yamt , [6] = {.string = "memory rd"}
344 1.8.16.3 yamt , [7] = {.string = "memory wr"}
345 1.8.16.3 yamt , [8] = {.string = "8"}
346 1.8.16.3 yamt , [9] = {.string = "9"}
347 1.8.16.3 yamt , [10] = {.string = "cfg rd", .nonfatal = true}
348 1.8.16.3 yamt , [11] = {.string = "cfg wr"}
349 1.8.16.3 yamt , [12] = {.string = "memory rd mul"}
350 1.8.16.3 yamt , [13] = {.string = "dual-address cycle"}
351 1.8.16.3 yamt , [14] = {.string = "memory rd line"}
352 1.8.16.3 yamt , [15] = {.string = "memory wr & inv"}
353 1.8.16.3 yamt };
354 1.8.16.3 yamt
355 1.8.16.3 yamt static const struct {
356 1.8.16.3 yamt uint16_t bit;
357 1.8.16.3 yamt const char *msg;
358 1.8.16.3 yamt } mmsg[] = {
359 1.8.16.3 yamt {MMCR_HBMSTIRQSTA_M_RTRTO_IRQ_STA, "retry timeout"}
360 1.8.16.3 yamt , {MMCR_HBMSTIRQSTA_M_TABRT_IRQ_STA, "target abort"}
361 1.8.16.3 yamt , {MMCR_HBMSTIRQSTA_M_MABRT_IRQ_STA, "abort"}
362 1.8.16.3 yamt , {MMCR_HBMSTIRQSTA_M_SERR_IRQ_STA, "system error"}
363 1.8.16.3 yamt , {MMCR_HBMSTIRQSTA_M_RPER_IRQ_STA, "received parity error"}
364 1.8.16.3 yamt , {MMCR_HBMSTIRQSTA_M_DPER_IRQ_STA, "detected parity error"}
365 1.8.16.3 yamt }, tmsg[] = {
366 1.8.16.3 yamt {MMCR_HBTGTIRQSTA_T_DLYTO_IRQ_STA, "delayed txn timeout"}
367 1.8.16.3 yamt , {MMCR_HBTGTIRQSTA_T_APER_IRQ_STA, "address parity"}
368 1.8.16.3 yamt , {MMCR_HBTGTIRQSTA_T_DPER_IRQ_STA, "data parity"}
369 1.8.16.3 yamt };
370 1.8.16.3 yamt uint8_t pciarbsta;
371 1.8.16.3 yamt uint16_t mstcmd, mstirq, tgtid, tgtirq;
372 1.8.16.3 yamt uint32_t mstaddr;
373 1.8.16.3 yamt uint16_t mstack = 0, tgtack = 0;
374 1.8.16.3 yamt int fatal = 0, i, handled = 0;
375 1.8.16.3 yamt struct elansc_softc *sc = arg;
376 1.8.16.3 yamt
377 1.8.16.3 yamt pciarbsta = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_PCIARBSTA);
378 1.8.16.3 yamt mstirq = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBMSTIRQSTA);
379 1.8.16.3 yamt mstaddr = bus_space_read_4(sc->sc_memt, sc->sc_memh, MMCR_MSTINTADD);
380 1.8.16.3 yamt tgtirq = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBTGTIRQSTA);
381 1.8.16.3 yamt
382 1.8.16.3 yamt if ((pciarbsta & MMCR_PCIARBSTA_GNT_TO_STA) != 0) {
383 1.8.16.3 yamt aprint_error_dev(sc->sc_pex,
384 1.8.16.3 yamt "grant time-out, GNT%" __PRIuBITS "# asserted\n",
385 1.8.16.3 yamt __SHIFTOUT(pciarbsta, MMCR_PCIARBSTA_GNT_TO_ID));
386 1.8.16.3 yamt bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_PCIARBSTA,
387 1.8.16.3 yamt MMCR_PCIARBSTA_GNT_TO_STA);
388 1.8.16.3 yamt handled = true;
389 1.8.16.3 yamt }
390 1.8.16.3 yamt
391 1.8.16.3 yamt mstcmd = __SHIFTOUT(mstirq, MMCR_HBMSTIRQSTA_M_CMD_IRQ_ID);
392 1.8.16.3 yamt
393 1.8.16.3 yamt for (i = 0; i < __arraycount(mmsg); i++) {
394 1.8.16.3 yamt if ((mstirq & mmsg[i].bit) == 0)
395 1.8.16.3 yamt continue;
396 1.8.16.3 yamt aprint_error_dev(sc->sc_pex,
397 1.8.16.3 yamt "%s %08" PRIx32 " master %s\n",
398 1.8.16.3 yamt cmd[mstcmd].string, mstaddr, mmsg[i].msg);
399 1.8.16.3 yamt
400 1.8.16.3 yamt mstack |= mmsg[i].bit;
401 1.8.16.3 yamt if (!cmd[mstcmd].nonfatal)
402 1.8.16.3 yamt fatal = true;
403 1.8.16.3 yamt }
404 1.8.16.3 yamt
405 1.8.16.3 yamt tgtid = __SHIFTOUT(tgtirq, MMCR_HBTGTIRQSTA_T_IRQ_ID);
406 1.8.16.3 yamt
407 1.8.16.3 yamt for (i = 0; i < __arraycount(tmsg); i++) {
408 1.8.16.3 yamt if ((tgtirq & tmsg[i].bit) == 0)
409 1.8.16.3 yamt continue;
410 1.8.16.3 yamt aprint_error_dev(sc->sc_pex, "%1x target %s\n", tgtid,
411 1.8.16.3 yamt tmsg[i].msg);
412 1.8.16.3 yamt tgtack |= tmsg[i].bit;
413 1.8.16.3 yamt }
414 1.8.16.3 yamt
415 1.8.16.3 yamt /* acknowledge interrupts */
416 1.8.16.3 yamt if (tgtack != 0) {
417 1.8.16.3 yamt handled = true;
418 1.8.16.3 yamt bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_HBTGTIRQSTA,
419 1.8.16.3 yamt tgtack);
420 1.8.16.3 yamt }
421 1.8.16.3 yamt if (mstack != 0) {
422 1.8.16.3 yamt handled = true;
423 1.8.16.3 yamt bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_HBMSTIRQSTA,
424 1.8.16.3 yamt mstack);
425 1.8.16.3 yamt }
426 1.8.16.3 yamt return fatal ? 0 : (handled ? 1 : 0);
427 1.8.16.3 yamt }
428 1.8.16.3 yamt
429 1.8.16.3 yamt #define elansc_print_1(__dev, __sc, __reg) \
430 1.8.16.3 yamt do { \
431 1.8.16.3 yamt aprint_debug_dev(__dev, \
432 1.8.16.3 yamt "%s: %s %02" PRIx8 "\n", __func__, #__reg, \
433 1.8.16.3 yamt bus_space_read_1((__sc)->sc_memt, (__sc)->sc_memh, __reg)); \
434 1.8.16.3 yamt } while (/*CONSTCOND*/0)
435 1.8.16.3 yamt
436 1.8.16.3 yamt static void
437 1.8.16.3 yamt elansc_print_par(device_t dev, int i, uint32_t par)
438 1.8.16.3 yamt {
439 1.8.16.3 yamt uint32_t addr, sz, unit;
440 1.8.16.3 yamt const char *tgtstr;
441 1.8.16.3 yamt
442 1.8.16.3 yamt switch (par & MMCR_PAR_TARGET) {
443 1.8.16.3 yamt default:
444 1.8.16.3 yamt case MMCR_PAR_TARGET_OFF:
445 1.8.16.3 yamt tgtstr = "off";
446 1.8.16.3 yamt break;
447 1.8.16.3 yamt case MMCR_PAR_TARGET_GPIO:
448 1.8.16.3 yamt tgtstr = "gpio";
449 1.8.16.3 yamt break;
450 1.8.16.3 yamt case MMCR_PAR_TARGET_GPMEM:
451 1.8.16.3 yamt tgtstr = "gpmem";
452 1.8.16.3 yamt break;
453 1.8.16.3 yamt case MMCR_PAR_TARGET_PCI:
454 1.8.16.3 yamt tgtstr = "pci";
455 1.8.16.3 yamt break;
456 1.8.16.3 yamt case MMCR_PAR_TARGET_BOOTCS:
457 1.8.16.3 yamt tgtstr = "bootcs";
458 1.8.16.3 yamt break;
459 1.8.16.3 yamt case MMCR_PAR_TARGET_ROMCS1:
460 1.8.16.3 yamt tgtstr = "romcs1";
461 1.8.16.3 yamt break;
462 1.8.16.3 yamt case MMCR_PAR_TARGET_ROMCS2:
463 1.8.16.3 yamt tgtstr = "romcs2";
464 1.8.16.3 yamt break;
465 1.8.16.3 yamt case MMCR_PAR_TARGET_SDRAM:
466 1.8.16.3 yamt tgtstr = "sdram";
467 1.8.16.3 yamt break;
468 1.8.16.3 yamt }
469 1.8.16.3 yamt if ((par & MMCR_PAR_TARGET) == MMCR_PAR_TARGET_GPIO) {
470 1.8.16.3 yamt unit = 1;
471 1.8.16.3 yamt sz = __SHIFTOUT(par, MMCR_PAR_IO_SZ);
472 1.8.16.3 yamt addr = __SHIFTOUT(par, MMCR_PAR_IO_ST_ADR);
473 1.8.16.3 yamt } else if ((par & MMCR_PAR_PG_SZ) != 0) {
474 1.8.16.3 yamt unit = 64 * 1024;
475 1.8.16.3 yamt sz = __SHIFTOUT(par, MMCR_PAR_64KB_SZ);
476 1.8.16.3 yamt addr = __SHIFTOUT(par, MMCR_PAR_64KB_ST_ADR);
477 1.8.16.3 yamt } else {
478 1.8.16.3 yamt unit = 4 * 1024;
479 1.8.16.3 yamt sz = __SHIFTOUT(par, MMCR_PAR_4KB_SZ);
480 1.8.16.3 yamt addr = __SHIFTOUT(par, MMCR_PAR_4KB_ST_ADR);
481 1.8.16.3 yamt }
482 1.8.16.3 yamt
483 1.8.16.3 yamt aprint_debug_dev(dev,
484 1.8.16.3 yamt "PAR[%d] %08" PRIx32 " tgt %s attr %1" __PRIxBITS
485 1.8.16.3 yamt " start %08" PRIx32 " size %" PRIu32 "\n",
486 1.8.16.3 yamt i, par, tgtstr, __SHIFTOUT(par, MMCR_PAR_ATTR),
487 1.8.16.3 yamt addr * unit, (sz + 1) * unit);
488 1.8.16.3 yamt }
489 1.8.16.3 yamt
490 1.8.16.3 yamt static void
491 1.8.16.3 yamt elansc_print_all_par(device_t dev,
492 1.8.16.3 yamt bus_space_tag_t memt, bus_space_handle_t memh)
493 1.8.16.3 yamt {
494 1.8.16.3 yamt int i;
495 1.8.16.3 yamt uint32_t par;
496 1.8.16.3 yamt
497 1.8.16.3 yamt for (i = 0; i < 16; i++) {
498 1.8.16.3 yamt par = bus_space_read_4(memt, memh, MMCR_PAR(i));
499 1.8.16.3 yamt elansc_print_par(dev, i, par);
500 1.8.16.3 yamt }
501 1.8.16.3 yamt }
502 1.8.16.3 yamt
503 1.8.16.3 yamt static int
504 1.8.16.3 yamt elansc_alloc_par(bus_space_tag_t memt, bus_space_handle_t memh)
505 1.8.16.3 yamt {
506 1.8.16.3 yamt int i;
507 1.8.16.3 yamt uint32_t par;
508 1.8.16.3 yamt
509 1.8.16.3 yamt for (i = 0; i < 16; i++) {
510 1.8.16.3 yamt
511 1.8.16.3 yamt par = bus_space_read_4(memt, memh, MMCR_PAR(i));
512 1.8.16.3 yamt
513 1.8.16.3 yamt if ((par & MMCR_PAR_TARGET) == MMCR_PAR_TARGET_OFF)
514 1.8.16.3 yamt break;
515 1.8.16.3 yamt }
516 1.8.16.3 yamt if (i == 16)
517 1.8.16.3 yamt return -1;
518 1.8.16.3 yamt return i;
519 1.8.16.3 yamt }
520 1.8.16.3 yamt
521 1.8.16.3 yamt static void
522 1.8.16.3 yamt elansc_disable_par(bus_space_tag_t memt, bus_space_handle_t memh, int idx)
523 1.8.16.3 yamt {
524 1.8.16.3 yamt uint32_t par;
525 1.8.16.3 yamt par = bus_space_read_4(memt, memh, MMCR_PAR(idx));
526 1.8.16.3 yamt par &= ~MMCR_PAR_TARGET;
527 1.8.16.3 yamt par |= MMCR_PAR_TARGET_OFF;
528 1.8.16.3 yamt bus_space_write_4(memt, memh, MMCR_PAR(idx), par);
529 1.8.16.3 yamt }
530 1.8.16.3 yamt
531 1.8.16.3 yamt static int
532 1.8.16.3 yamt elansc_protect_text(device_t self, struct elansc_softc *sc)
533 1.8.16.3 yamt {
534 1.8.16.3 yamt int i;
535 1.8.16.3 yamt uint32_t par;
536 1.8.16.3 yamt uint32_t protsize, unprotsize;
537 1.8.16.3 yamt const uint32_t sfkb = 64 * 1024;
538 1.8.16.3 yamt paddr_t start_pa, end_pa;
539 1.8.16.3 yamt extern char kernel_text, etext;
540 1.8.16.3 yamt bus_space_tag_t memt;
541 1.8.16.3 yamt bus_space_handle_t memh;
542 1.8.16.3 yamt
543 1.8.16.3 yamt memt = sc->sc_memt;
544 1.8.16.3 yamt memh = sc->sc_memh;
545 1.8.16.3 yamt
546 1.8.16.3 yamt if (!pmap_extract(pmap_kernel(), (vaddr_t)&kernel_text, &start_pa) ||
547 1.8.16.3 yamt !pmap_extract(pmap_kernel(), (vaddr_t)&etext, &end_pa))
548 1.8.16.3 yamt return -1;
549 1.8.16.3 yamt
550 1.8.16.3 yamt if (&etext - &kernel_text != end_pa - start_pa) {
551 1.8.16.3 yamt aprint_error_dev(self, "kernel text may not be contiguous\n");
552 1.8.16.3 yamt return -1;
553 1.8.16.3 yamt }
554 1.8.16.3 yamt
555 1.8.16.3 yamt if ((i = elansc_alloc_par(memt, memh)) == -1) {
556 1.8.16.3 yamt aprint_error_dev(self, "cannot allocate PAR\n");
557 1.8.16.3 yamt return -1;
558 1.8.16.3 yamt }
559 1.8.16.3 yamt
560 1.8.16.3 yamt par = bus_space_read_4(memt, memh, MMCR_PAR(i));
561 1.8.16.3 yamt
562 1.8.16.3 yamt aprint_debug_dev(self,
563 1.8.16.3 yamt "protect kernel text at physical addresses %p - %p\n",
564 1.8.16.3 yamt (void *)start_pa, (void *)end_pa);
565 1.8.16.3 yamt
566 1.8.16.3 yamt unprotsize = sfkb - start_pa % sfkb;
567 1.8.16.3 yamt start_pa += unprotsize;
568 1.8.16.3 yamt unprotsize += end_pa % sfkb;
569 1.8.16.3 yamt end_pa -= end_pa % sfkb;
570 1.8.16.3 yamt
571 1.8.16.3 yamt aprint_debug_dev(self,
572 1.8.16.3 yamt "actually protect kernel text at physical addresses %p - %p\n",
573 1.8.16.3 yamt (void *)start_pa, (void *)end_pa);
574 1.8.16.3 yamt
575 1.8.16.3 yamt aprint_verbose_dev(self,
576 1.8.16.3 yamt "%" PRIu32 " bytes of kernel text are unprotected\n", unprotsize);
577 1.8.16.3 yamt
578 1.8.16.3 yamt protsize = end_pa - start_pa;
579 1.8.16.3 yamt
580 1.8.16.3 yamt /* clear PG_SZ, attribute, target, size, address. */
581 1.8.16.3 yamt par = MMCR_PAR_TARGET_SDRAM | MMCR_PAR_ATTR_NOWRITE | MMCR_PAR_PG_SZ;
582 1.8.16.3 yamt par |= __SHIFTIN(protsize / sfkb - 1, MMCR_PAR_64KB_SZ);
583 1.8.16.3 yamt par |= __SHIFTIN(start_pa / sfkb, MMCR_PAR_64KB_ST_ADR);
584 1.8.16.3 yamt bus_space_write_4(memt, memh, MMCR_PAR(i), par);
585 1.8.16.3 yamt return i;
586 1.8.16.3 yamt }
587 1.8.16.3 yamt
588 1.8.16.3 yamt static int
589 1.8.16.4 yamt elansc_protect_pg0(device_t self, struct elansc_softc *sc)
590 1.8.16.3 yamt {
591 1.8.16.3 yamt int i;
592 1.8.16.3 yamt uint32_t par;
593 1.8.16.4 yamt const paddr_t pg0_paddr = 0;
594 1.8.16.3 yamt bus_space_tag_t memt;
595 1.8.16.3 yamt bus_space_handle_t memh;
596 1.8.16.3 yamt
597 1.8.16.3 yamt memt = sc->sc_memt;
598 1.8.16.3 yamt memh = sc->sc_memh;
599 1.8.16.3 yamt
600 1.8.16.4 yamt if (elansc_do_protect_pg0 == 0)
601 1.8.16.3 yamt return -1;
602 1.8.16.3 yamt
603 1.8.16.3 yamt if ((i = elansc_alloc_par(memt, memh)) == -1)
604 1.8.16.3 yamt return -1;
605 1.8.16.3 yamt
606 1.8.16.3 yamt par = bus_space_read_4(memt, memh, MMCR_PAR(i));
607 1.8.16.3 yamt
608 1.8.16.4 yamt aprint_debug_dev(self, "protect page 0\n");
609 1.8.16.3 yamt
610 1.8.16.3 yamt /* clear PG_SZ, attribute, target, size, address. */
611 1.8.16.3 yamt par = MMCR_PAR_TARGET_SDRAM | MMCR_PAR_ATTR_NOWRITE;
612 1.8.16.4 yamt par |= __SHIFTIN(PG0_PROT_SIZE / PAGE_SIZE - 1, MMCR_PAR_4KB_SZ);
613 1.8.16.4 yamt par |= __SHIFTIN(pg0_paddr / PAGE_SIZE, MMCR_PAR_4KB_ST_ADR);
614 1.8.16.3 yamt bus_space_write_4(memt, memh, MMCR_PAR(i), par);
615 1.8.16.3 yamt return i;
616 1.8.16.3 yamt }
617 1.8.16.3 yamt
618 1.8.16.3 yamt static void
619 1.8.16.3 yamt elanpex_intr_ack(bus_space_tag_t memt, bus_space_handle_t memh)
620 1.8.16.3 yamt {
621 1.8.16.3 yamt bus_space_write_1(memt, memh, MMCR_PCIARBSTA,
622 1.8.16.3 yamt MMCR_PCIARBSTA_GNT_TO_STA);
623 1.8.16.3 yamt bus_space_write_2(memt, memh, MMCR_HBTGTIRQSTA, MMCR_TGTIRQ_ACT);
624 1.8.16.3 yamt bus_space_write_2(memt, memh, MMCR_HBMSTIRQSTA, MMCR_MSTIRQ_ACT);
625 1.8.16.3 yamt }
626 1.8.16.3 yamt
627 1.8.16.3 yamt static bool
628 1.8.16.5 yamt elansc_suspend(device_t dev PMF_FN_ARGS)
629 1.8.16.3 yamt {
630 1.8.16.3 yamt bool rc;
631 1.8.16.3 yamt struct elansc_softc *sc = device_private(dev);
632 1.8.16.3 yamt
633 1.8.16.3 yamt mutex_enter(&sc->sc_mtx);
634 1.8.16.3 yamt rc = ((sc->sc_smw.smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED);
635 1.8.16.3 yamt mutex_exit(&sc->sc_mtx);
636 1.8.16.3 yamt if (!rc)
637 1.8.16.3 yamt aprint_debug_dev(dev, "watchdog enabled, suspend forbidden");
638 1.8.16.3 yamt return rc;
639 1.8.16.3 yamt }
640 1.8.16.3 yamt
641 1.8.16.3 yamt static bool
642 1.8.16.5 yamt elansc_resume(device_t dev PMF_FN_ARGS)
643 1.8.16.3 yamt {
644 1.8.16.3 yamt struct elansc_softc *sc = device_private(dev);
645 1.8.16.3 yamt
646 1.8.16.3 yamt mutex_enter(&sc->sc_mtx);
647 1.8.16.3 yamt /* Set up the watchdog registers with some defaults. */
648 1.8.16.3 yamt elansc_wdogctl_write(sc, WDTMRCTL_WRST_ENB | WDTMRCTL_EXP_SEL30);
649 1.8.16.3 yamt
650 1.8.16.3 yamt /* ...and clear it. */
651 1.8.16.3 yamt elansc_wdogctl_reset(sc);
652 1.8.16.3 yamt mutex_exit(&sc->sc_mtx);
653 1.8.16.3 yamt
654 1.8.16.3 yamt return true;
655 1.8.16.3 yamt }
656 1.8.16.3 yamt
657 1.8.16.3 yamt static int
658 1.8.16.3 yamt elansc_detach(device_t self, int flags)
659 1.8.16.3 yamt {
660 1.8.16.3 yamt int rc;
661 1.8.16.3 yamt struct elansc_softc *sc = device_private(self);
662 1.8.16.3 yamt
663 1.8.16.3 yamt if ((rc = config_detach_children(self, flags)) != 0)
664 1.8.16.3 yamt return rc;
665 1.8.16.3 yamt
666 1.8.16.3 yamt pmf_device_deregister(self);
667 1.8.16.3 yamt
668 1.8.16.3 yamt if ((rc = sysmon_wdog_unregister(&sc->sc_smw)) != 0) {
669 1.8.16.3 yamt if (rc == ERESTART)
670 1.8.16.3 yamt rc = EINTR;
671 1.8.16.3 yamt return rc;
672 1.8.16.3 yamt }
673 1.8.16.3 yamt
674 1.8.16.3 yamt mutex_enter(&sc->sc_mtx);
675 1.8.16.3 yamt
676 1.8.16.3 yamt /* Set up the watchdog registers with some defaults. */
677 1.8.16.3 yamt elansc_wdogctl_write(sc, WDTMRCTL_WRST_ENB | WDTMRCTL_EXP_SEL30);
678 1.8.16.3 yamt
679 1.8.16.3 yamt /* ...and clear it. */
680 1.8.16.3 yamt elansc_wdogctl_reset(sc);
681 1.8.16.3 yamt
682 1.8.16.3 yamt bus_space_unmap(sc->sc_memt, sc->sc_memh, PAGE_SIZE);
683 1.8.16.3 yamt
684 1.8.16.3 yamt mutex_exit(&sc->sc_mtx);
685 1.8.16.3 yamt mutex_destroy(&sc->sc_mtx);
686 1.8.16.3 yamt return 0;
687 1.8.16.3 yamt }
688 1.8.16.3 yamt
689 1.8.16.3 yamt static void *
690 1.8.16.3 yamt elansc_intr_establish(device_t dev, int (*handler)(void *), void *arg)
691 1.8.16.3 yamt {
692 1.8.16.3 yamt struct pic *pic;
693 1.8.16.3 yamt void *ih;
694 1.8.16.3 yamt
695 1.8.16.3 yamt if ((pic = intr_findpic(ELAN_IRQ)) == NULL) {
696 1.8.16.3 yamt aprint_error_dev(dev, "PIC for irq %d not found\n",
697 1.8.16.3 yamt ELAN_IRQ);
698 1.8.16.3 yamt return NULL;
699 1.8.16.3 yamt } else if ((ih = intr_establish(ELAN_IRQ, pic, ELAN_IRQ,
700 1.8.16.3 yamt IST_LEVEL, IPL_HIGH, handler, arg)) == NULL) {
701 1.8.16.3 yamt aprint_error_dev(dev,
702 1.8.16.3 yamt "could not establish interrupt\n");
703 1.8.16.3 yamt return NULL;
704 1.8.16.3 yamt }
705 1.8.16.3 yamt aprint_verbose_dev(dev, "interrupting at irq %d\n", ELAN_IRQ);
706 1.8.16.3 yamt return ih;
707 1.8.16.3 yamt }
708 1.8.16.3 yamt
709 1.8.16.3 yamt static bool
710 1.8.16.5 yamt elanpex_resume(device_t self PMF_FN_ARGS)
711 1.8.16.3 yamt {
712 1.8.16.3 yamt struct elansc_softc *sc = device_private(device_parent(self));
713 1.8.16.3 yamt
714 1.8.16.3 yamt elanpex_intr_establish(self, sc);
715 1.8.16.3 yamt return sc->sc_eih != NULL;
716 1.8.16.3 yamt }
717 1.8.16.3 yamt
718 1.8.16.3 yamt static bool
719 1.8.16.5 yamt elanpex_suspend(device_t self PMF_FN_ARGS)
720 1.8.16.3 yamt {
721 1.8.16.3 yamt struct elansc_softc *sc = device_private(device_parent(self));
722 1.8.16.3 yamt
723 1.8.16.3 yamt elanpex_intr_disestablish(sc);
724 1.8.16.3 yamt
725 1.8.16.3 yamt return true;
726 1.8.16.3 yamt }
727 1.8.16.3 yamt
728 1.8.16.3 yamt static bool
729 1.8.16.5 yamt elanpar_resume(device_t self PMF_FN_ARGS)
730 1.8.16.3 yamt {
731 1.8.16.3 yamt struct elansc_softc *sc = device_private(device_parent(self));
732 1.8.16.3 yamt
733 1.8.16.3 yamt elanpar_intr_establish(self, sc);
734 1.8.16.3 yamt return sc->sc_pih != NULL;
735 1.8.16.3 yamt }
736 1.8.16.3 yamt
737 1.8.16.3 yamt static bool
738 1.8.16.5 yamt elanpar_suspend(device_t self PMF_FN_ARGS)
739 1.8.16.3 yamt {
740 1.8.16.3 yamt struct elansc_softc *sc = device_private(device_parent(self));
741 1.8.16.3 yamt
742 1.8.16.5 yamt elanpar_intr_disestablish(sc);
743 1.8.16.3 yamt
744 1.8.16.3 yamt return true;
745 1.8.16.3 yamt }
746 1.8.16.3 yamt
747 1.1 thorpej static void
748 1.8.16.3 yamt elanpex_intr_establish(device_t self, struct elansc_softc *sc)
749 1.1 thorpej {
750 1.8.16.3 yamt uint8_t sysarbctl;
751 1.8.16.3 yamt uint16_t pcihostmap, mstirq, tgtirq;
752 1.8.16.3 yamt
753 1.8.16.3 yamt pcihostmap = bus_space_read_2(sc->sc_memt, sc->sc_memh,
754 1.8.16.3 yamt MMCR_PCIHOSTMAP);
755 1.8.16.3 yamt /* Priority P2 (Master PIC IR1) */
756 1.8.16.3 yamt pcihostmap &= ~MMCR_PCIHOSTMAP_PCI_IRQ_MAP;
757 1.8.16.3 yamt pcihostmap |= __SHIFTIN(__BIT(ELAN_IRQ), MMCR_PCIHOSTMAP_PCI_IRQ_MAP);
758 1.8.16.3 yamt if (elansc_pcinmi)
759 1.8.16.3 yamt pcihostmap |= MMCR_PCIHOSTMAP_PCI_NMI_ENB;
760 1.8.16.3 yamt bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_PCIHOSTMAP,
761 1.8.16.3 yamt pcihostmap);
762 1.8.16.3 yamt
763 1.8.16.3 yamt elanpex_intr_ack(sc->sc_memt, sc->sc_memh);
764 1.8.16.3 yamt
765 1.8.16.3 yamt sysarbctl = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_SYSARBCTL);
766 1.8.16.3 yamt mstirq = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBMSTIRQCTL);
767 1.8.16.3 yamt tgtirq = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBTGTIRQCTL);
768 1.8.16.3 yamt
769 1.8.16.3 yamt sysarbctl |= MMCR_SYSARBCTL_GNT_TO_INT_ENB;
770 1.8.16.3 yamt
771 1.8.16.3 yamt mstirq |= MMCR_HBMSTIRQCTL_M_RTRTO_IRQ_ENB;
772 1.8.16.3 yamt mstirq |= MMCR_HBMSTIRQCTL_M_TABRT_IRQ_ENB;
773 1.8.16.3 yamt mstirq |= MMCR_HBMSTIRQCTL_M_MABRT_IRQ_ENB;
774 1.8.16.3 yamt mstirq |= MMCR_HBMSTIRQCTL_M_SERR_IRQ_ENB;
775 1.8.16.3 yamt mstirq |= MMCR_HBMSTIRQCTL_M_RPER_IRQ_ENB;
776 1.8.16.3 yamt mstirq |= MMCR_HBMSTIRQCTL_M_DPER_IRQ_ENB;
777 1.8.16.3 yamt
778 1.8.16.3 yamt tgtirq |= MMCR_HBTGTIRQCTL_T_DLYTO_IRQ_ENB;
779 1.8.16.3 yamt tgtirq |= MMCR_HBTGTIRQCTL_T_APER_IRQ_ENB;
780 1.8.16.3 yamt tgtirq |= MMCR_HBTGTIRQCTL_T_DPER_IRQ_ENB;
781 1.8.16.3 yamt
782 1.8.16.3 yamt if (elansc_pcinmi) {
783 1.8.16.3 yamt sc->sc_eih = nmi_establish(elanpex_intr, sc);
784 1.8.16.3 yamt
785 1.8.16.3 yamt mstirq |= MMCR_HBMSTIRQCTL_M_RTRTO_IRQ_SEL;
786 1.8.16.3 yamt mstirq |= MMCR_HBMSTIRQCTL_M_TABRT_IRQ_SEL;
787 1.8.16.3 yamt mstirq |= MMCR_HBMSTIRQCTL_M_MABRT_IRQ_SEL;
788 1.8.16.3 yamt mstirq |= MMCR_HBMSTIRQCTL_M_SERR_IRQ_SEL;
789 1.8.16.3 yamt mstirq |= MMCR_HBMSTIRQCTL_M_RPER_IRQ_SEL;
790 1.8.16.3 yamt mstirq |= MMCR_HBMSTIRQCTL_M_DPER_IRQ_SEL;
791 1.8.16.3 yamt
792 1.8.16.3 yamt tgtirq |= MMCR_HBTGTIRQCTL_T_DLYTO_IRQ_SEL;
793 1.8.16.3 yamt tgtirq |= MMCR_HBTGTIRQCTL_T_APER_IRQ_SEL;
794 1.8.16.3 yamt tgtirq |= MMCR_HBTGTIRQCTL_T_DPER_IRQ_SEL;
795 1.8.16.3 yamt } else
796 1.8.16.3 yamt sc->sc_eih = elansc_intr_establish(self, elanpex_intr, sc);
797 1.8.16.3 yamt
798 1.8.16.3 yamt bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_SYSARBCTL, sysarbctl);
799 1.8.16.3 yamt bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_HBMSTIRQCTL, mstirq);
800 1.8.16.3 yamt bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_HBTGTIRQCTL, tgtirq);
801 1.8.16.3 yamt }
802 1.8.16.3 yamt
803 1.8.16.3 yamt static void
804 1.8.16.3 yamt elanpex_attach(device_t parent, device_t self, void *aux)
805 1.8.16.3 yamt {
806 1.8.16.3 yamt struct elansc_softc *sc = device_private(parent);
807 1.8.16.3 yamt
808 1.8.16.3 yamt aprint_naive(": PCI Exceptions\n");
809 1.8.16.3 yamt aprint_normal(": AMD Elan SC520 PCI Exceptions\n");
810 1.8.16.3 yamt
811 1.8.16.3 yamt elanpex_intr_establish(self, sc);
812 1.8.16.3 yamt
813 1.8.16.3 yamt aprint_debug_dev(self, "HBMSTIRQCTL %04x\n",
814 1.8.16.3 yamt bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBMSTIRQCTL));
815 1.8.16.3 yamt
816 1.8.16.3 yamt aprint_debug_dev(self, "HBTGTIRQCTL %04x\n",
817 1.8.16.3 yamt bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBTGTIRQCTL));
818 1.8.16.3 yamt
819 1.8.16.3 yamt aprint_debug_dev(self, "PCIHOSTMAP %04x\n",
820 1.8.16.3 yamt bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_PCIHOSTMAP));
821 1.8.16.3 yamt
822 1.8.16.3 yamt pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG,
823 1.8.16.3 yamt pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG) |
824 1.8.16.3 yamt PCI_COMMAND_PARITY_ENABLE|PCI_COMMAND_SERR_ENABLE);
825 1.8.16.3 yamt
826 1.8.16.5 yamt if (!pmf_device_register1(self, elanpex_suspend, elanpex_resume,
827 1.8.16.5 yamt elanpex_shutdown))
828 1.8.16.3 yamt aprint_error_dev(self, "could not establish power hooks\n");
829 1.8.16.3 yamt }
830 1.8.16.3 yamt
831 1.8.16.5 yamt static bool
832 1.8.16.5 yamt elanpex_shutdown(device_t self, int flags)
833 1.8.16.3 yamt {
834 1.8.16.5 yamt struct elansc_softc *sc = device_private(device_parent(self));
835 1.8.16.3 yamt uint8_t sysarbctl;
836 1.8.16.3 yamt uint16_t pcihostmap, mstirq, tgtirq;
837 1.8.16.3 yamt
838 1.8.16.3 yamt sysarbctl = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_SYSARBCTL);
839 1.8.16.3 yamt sysarbctl &= ~MMCR_SYSARBCTL_GNT_TO_INT_ENB;
840 1.8.16.3 yamt bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_SYSARBCTL, sysarbctl);
841 1.8.16.3 yamt
842 1.8.16.3 yamt mstirq = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBMSTIRQCTL);
843 1.8.16.3 yamt mstirq &= ~MMCR_HBMSTIRQCTL_M_RTRTO_IRQ_ENB;
844 1.8.16.3 yamt mstirq &= ~MMCR_HBMSTIRQCTL_M_TABRT_IRQ_ENB;
845 1.8.16.3 yamt mstirq &= ~MMCR_HBMSTIRQCTL_M_MABRT_IRQ_ENB;
846 1.8.16.3 yamt mstirq &= ~MMCR_HBMSTIRQCTL_M_SERR_IRQ_ENB;
847 1.8.16.3 yamt mstirq &= ~MMCR_HBMSTIRQCTL_M_RPER_IRQ_ENB;
848 1.8.16.3 yamt mstirq &= ~MMCR_HBMSTIRQCTL_M_DPER_IRQ_ENB;
849 1.8.16.3 yamt bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_HBMSTIRQCTL, mstirq);
850 1.8.16.3 yamt
851 1.8.16.3 yamt tgtirq = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_HBTGTIRQCTL);
852 1.8.16.3 yamt tgtirq &= ~MMCR_HBTGTIRQCTL_T_DLYTO_IRQ_ENB;
853 1.8.16.3 yamt tgtirq &= ~MMCR_HBTGTIRQCTL_T_APER_IRQ_ENB;
854 1.8.16.3 yamt tgtirq &= ~MMCR_HBTGTIRQCTL_T_DPER_IRQ_ENB;
855 1.8.16.3 yamt bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_HBTGTIRQCTL, tgtirq);
856 1.8.16.3 yamt
857 1.8.16.3 yamt pcihostmap = bus_space_read_2(sc->sc_memt, sc->sc_memh,
858 1.8.16.3 yamt MMCR_PCIHOSTMAP);
859 1.8.16.3 yamt /* Priority P2 (Master PIC IR1) */
860 1.8.16.3 yamt pcihostmap &= ~MMCR_PCIHOSTMAP_PCI_IRQ_MAP;
861 1.8.16.3 yamt bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_PCIHOSTMAP,
862 1.8.16.3 yamt pcihostmap);
863 1.8.16.3 yamt
864 1.8.16.5 yamt return true;
865 1.8.16.5 yamt }
866 1.8.16.5 yamt
867 1.8.16.5 yamt static void
868 1.8.16.5 yamt elanpex_intr_disestablish(struct elansc_softc *sc)
869 1.8.16.5 yamt {
870 1.8.16.5 yamt elanpex_shutdown(sc->sc_pex, 0);
871 1.8.16.5 yamt
872 1.8.16.3 yamt if (elansc_pcinmi)
873 1.8.16.3 yamt nmi_disestablish(sc->sc_eih);
874 1.8.16.3 yamt else
875 1.8.16.3 yamt intr_disestablish(sc->sc_eih);
876 1.8.16.3 yamt sc->sc_eih = NULL;
877 1.8.16.3 yamt
878 1.8.16.3 yamt }
879 1.8.16.3 yamt
880 1.8.16.3 yamt static int
881 1.8.16.3 yamt elanpex_detach(device_t self, int flags)
882 1.8.16.3 yamt {
883 1.8.16.3 yamt struct elansc_softc *sc = device_private(device_parent(self));
884 1.8.16.3 yamt
885 1.8.16.3 yamt pmf_device_deregister(self);
886 1.8.16.3 yamt elanpex_intr_disestablish(sc);
887 1.8.16.3 yamt
888 1.8.16.3 yamt return 0;
889 1.8.16.3 yamt }
890 1.8.16.3 yamt
891 1.8.16.3 yamt static void
892 1.8.16.3 yamt elanpar_intr_establish(device_t self, struct elansc_softc *sc)
893 1.8.16.3 yamt {
894 1.8.16.3 yamt uint8_t adddecctl, wpvmap;
895 1.8.16.3 yamt
896 1.8.16.3 yamt wpvmap = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_WPVMAP);
897 1.8.16.3 yamt wpvmap &= ~MMCR_WPVMAP_INT_MAP;
898 1.8.16.3 yamt if (elansc_wpvnmi)
899 1.8.16.3 yamt wpvmap |= MMCR_WPVMAP_INT_NMI;
900 1.8.16.3 yamt else
901 1.8.16.3 yamt wpvmap |= __SHIFTIN(__BIT(ELAN_IRQ), MMCR_WPVMAP_INT_MAP);
902 1.8.16.3 yamt bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_WPVMAP, wpvmap);
903 1.8.16.3 yamt
904 1.8.16.3 yamt /* clear interrupt status */
905 1.8.16.3 yamt bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WPVSTA,
906 1.8.16.3 yamt MMCR_WPVSTA_WPV_STA);
907 1.8.16.3 yamt
908 1.8.16.3 yamt /* establish interrupt */
909 1.8.16.3 yamt if (elansc_wpvnmi)
910 1.8.16.3 yamt sc->sc_pih = nmi_establish(elanpar_intr, sc);
911 1.8.16.3 yamt else
912 1.8.16.3 yamt sc->sc_pih = elansc_intr_establish(self, elanpar_intr, sc);
913 1.8.16.3 yamt
914 1.8.16.3 yamt adddecctl = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_ADDDECCTL);
915 1.8.16.3 yamt adddecctl |= MMCR_ADDDECCTL_WPV_INT_ENB;
916 1.8.16.3 yamt bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_ADDDECCTL, adddecctl);
917 1.8.16.3 yamt }
918 1.8.16.3 yamt
919 1.8.16.5 yamt static bool
920 1.8.16.5 yamt elanpar_shutdown(device_t self, int flags)
921 1.8.16.5 yamt {
922 1.8.16.5 yamt struct elansc_softc *sc = device_private(device_parent(self));
923 1.8.16.5 yamt
924 1.8.16.5 yamt if (sc->sc_textpar != -1) {
925 1.8.16.5 yamt elansc_disable_par(sc->sc_memt, sc->sc_memh, sc->sc_textpar);
926 1.8.16.5 yamt sc->sc_textpar = -1;
927 1.8.16.5 yamt }
928 1.8.16.5 yamt if (sc->sc_pg0par != -1) {
929 1.8.16.5 yamt elansc_disable_par(sc->sc_memt, sc->sc_memh, sc->sc_pg0par);
930 1.8.16.5 yamt sc->sc_pg0par = -1;
931 1.8.16.5 yamt }
932 1.8.16.5 yamt return true;
933 1.8.16.5 yamt }
934 1.8.16.5 yamt
935 1.8.16.3 yamt static void
936 1.8.16.3 yamt elanpar_attach(device_t parent, device_t self, void *aux)
937 1.8.16.3 yamt {
938 1.8.16.3 yamt struct elansc_softc *sc = device_private(parent);
939 1.8.16.3 yamt
940 1.8.16.3 yamt aprint_naive(": Programmable Address Regions\n");
941 1.8.16.3 yamt aprint_normal(": AMD Elan SC520 Programmable Address Regions\n");
942 1.8.16.3 yamt
943 1.8.16.3 yamt elansc_print_1(self, sc, MMCR_WPVMAP);
944 1.8.16.3 yamt elansc_print_all_par(self, sc->sc_memt, sc->sc_memh);
945 1.8.16.3 yamt
946 1.8.16.4 yamt sc->sc_pg0par = elansc_protect_pg0(self, sc);
947 1.8.16.3 yamt sc->sc_textpar = elansc_protect_text(self, sc);
948 1.8.16.3 yamt
949 1.8.16.3 yamt elanpar_intr_establish(self, sc);
950 1.8.16.3 yamt
951 1.8.16.3 yamt elansc_print_1(self, sc, MMCR_ADDDECCTL);
952 1.8.16.3 yamt
953 1.8.16.5 yamt if (!pmf_device_register1(self, elanpar_suspend, elanpar_resume,
954 1.8.16.5 yamt elanpar_shutdown))
955 1.8.16.3 yamt aprint_error_dev(self, "could not establish power hooks\n");
956 1.8.16.3 yamt }
957 1.8.16.3 yamt
958 1.8.16.3 yamt static void
959 1.8.16.3 yamt elanpar_intr_disestablish(struct elansc_softc *sc)
960 1.8.16.3 yamt {
961 1.8.16.3 yamt uint8_t adddecctl, wpvmap;
962 1.8.16.3 yamt
963 1.8.16.3 yamt /* disable interrupt, acknowledge it, disestablish our
964 1.8.16.3 yamt * handler, unmap it
965 1.8.16.3 yamt */
966 1.8.16.3 yamt adddecctl = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_ADDDECCTL);
967 1.8.16.3 yamt adddecctl &= ~MMCR_ADDDECCTL_WPV_INT_ENB;
968 1.8.16.3 yamt bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_ADDDECCTL, adddecctl);
969 1.8.16.3 yamt
970 1.8.16.3 yamt bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WPVSTA,
971 1.8.16.3 yamt MMCR_WPVSTA_WPV_STA);
972 1.8.16.3 yamt
973 1.8.16.3 yamt if (elansc_wpvnmi)
974 1.8.16.3 yamt nmi_disestablish(sc->sc_pih);
975 1.8.16.3 yamt else
976 1.8.16.3 yamt intr_disestablish(sc->sc_pih);
977 1.8.16.3 yamt sc->sc_pih = NULL;
978 1.8.16.3 yamt
979 1.8.16.3 yamt wpvmap = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_WPVMAP);
980 1.8.16.3 yamt wpvmap &= ~MMCR_WPVMAP_INT_MAP;
981 1.8.16.3 yamt bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_WPVMAP, wpvmap);
982 1.8.16.3 yamt }
983 1.8.16.3 yamt
984 1.8.16.3 yamt static int
985 1.8.16.3 yamt elanpar_detach(device_t self, int flags)
986 1.8.16.3 yamt {
987 1.8.16.3 yamt struct elansc_softc *sc = device_private(device_parent(self));
988 1.8.16.3 yamt
989 1.8.16.3 yamt pmf_device_deregister(self);
990 1.8.16.3 yamt
991 1.8.16.5 yamt elanpar_shutdown(self, 0);
992 1.8.16.3 yamt
993 1.8.16.3 yamt elanpar_intr_disestablish(sc);
994 1.8.16.3 yamt
995 1.8.16.3 yamt return 0;
996 1.8.16.3 yamt }
997 1.8.16.3 yamt
998 1.8.16.3 yamt static void
999 1.8.16.3 yamt elansc_attach(device_t parent, device_t self, void *aux)
1000 1.8.16.3 yamt {
1001 1.8.16.3 yamt struct elansc_softc *sc = device_private(self);
1002 1.1 thorpej struct pci_attach_args *pa = aux;
1003 1.1 thorpej uint16_t rev;
1004 1.8.16.3 yamt uint8_t cpuctl, picicr, ressta;
1005 1.8.16.1 yamt #if NGPIO > 0
1006 1.8.16.1 yamt struct gpiobus_attach_args gba;
1007 1.8.16.3 yamt int pin, reg, shift;
1008 1.8.16.1 yamt uint16_t data;
1009 1.8.16.1 yamt #endif
1010 1.8.16.3 yamt sc->sc_pc = pa->pa_pc;
1011 1.8.16.3 yamt sc->sc_tag = pa->pa_tag;
1012 1.1 thorpej
1013 1.8.16.1 yamt aprint_naive(": System Controller\n");
1014 1.8.16.1 yamt aprint_normal(": AMD Elan SC520 System Controller\n");
1015 1.1 thorpej
1016 1.1 thorpej sc->sc_memt = pa->pa_memt;
1017 1.5 thorpej if (bus_space_map(sc->sc_memt, MMCR_BASE_ADDR, PAGE_SIZE, 0,
1018 1.1 thorpej &sc->sc_memh) != 0) {
1019 1.8.16.3 yamt aprint_error_dev(&sc->sc_dev, "unable to map registers\n");
1020 1.1 thorpej return;
1021 1.1 thorpej }
1022 1.1 thorpej
1023 1.8.16.3 yamt mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_HIGH);
1024 1.8.16.3 yamt
1025 1.1 thorpej rev = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_REVID);
1026 1.1 thorpej cpuctl = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_CPUCTL);
1027 1.1 thorpej
1028 1.8.16.3 yamt aprint_normal_dev(&sc->sc_dev,
1029 1.8.16.3 yamt "product %d stepping %d.%d, CPU clock %s\n",
1030 1.1 thorpej (rev & REVID_PRODID) >> REVID_PRODID_SHIFT,
1031 1.1 thorpej (rev & REVID_MAJSTEP) >> REVID_MAJSTEP_SHIFT,
1032 1.1 thorpej (rev & REVID_MINSTEP),
1033 1.1 thorpej elansc_speeds[cpuctl & CPUCTL_CPU_CLK_SPD_MASK]);
1034 1.1 thorpej
1035 1.1 thorpej /*
1036 1.1 thorpej * SC520 rev A1 has a bug that affects the watchdog timer. If
1037 1.1 thorpej * the GP bus echo mode is enabled, writing to the watchdog control
1038 1.1 thorpej * register is blocked.
1039 1.1 thorpej *
1040 1.1 thorpej * The BIOS in some systems (e.g. the Soekris net4501) enables
1041 1.1 thorpej * GP bus echo for various reasons, so we need to switch it off
1042 1.1 thorpej * when we talk to the watchdog timer.
1043 1.1 thorpej *
1044 1.1 thorpej * XXX The step 1.1 (B1?) in my Soekris net4501 also has this
1045 1.1 thorpej * XXX problem, so we'll just enable it for all Elan SC520s
1046 1.8 keihan * XXX for now. --thorpej (at) NetBSD.org
1047 1.1 thorpej */
1048 1.1 thorpej if (1 || rev == ((PRODID_ELAN_SC520 << REVID_PRODID_SHIFT) |
1049 1.1 thorpej (0 << REVID_MAJSTEP_SHIFT) | (1)))
1050 1.1 thorpej sc->sc_echobug = 1;
1051 1.1 thorpej
1052 1.1 thorpej /*
1053 1.1 thorpej * Determine cause of the last reset, and issue a warning if it
1054 1.1 thorpej * was due to watchdog expiry.
1055 1.1 thorpej */
1056 1.1 thorpej ressta = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_RESSTA);
1057 1.1 thorpej if (ressta & RESSTA_WDT_RST_DET)
1058 1.8.16.3 yamt aprint_error_dev(&sc->sc_dev,
1059 1.8.16.3 yamt "WARNING: LAST RESET DUE TO WATCHDOG EXPIRATION!\n");
1060 1.1 thorpej bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_RESSTA, ressta);
1061 1.1 thorpej
1062 1.8.16.3 yamt elansc_print_1(self, sc, MMCR_MPICMODE);
1063 1.8.16.3 yamt elansc_print_1(self, sc, MMCR_SL1PICMODE);
1064 1.8.16.3 yamt elansc_print_1(self, sc, MMCR_SL2PICMODE);
1065 1.8.16.3 yamt elansc_print_1(self, sc, MMCR_PICICR);
1066 1.8.16.3 yamt
1067 1.8.16.3 yamt sc->sc_mpicmode = bus_space_read_1(sc->sc_memt, sc->sc_memh,
1068 1.8.16.3 yamt MMCR_MPICMODE);
1069 1.8.16.3 yamt bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_MPICMODE,
1070 1.8.16.3 yamt sc->sc_mpicmode | __BIT(ELAN_IRQ));
1071 1.8.16.3 yamt
1072 1.8.16.3 yamt sc->sc_picicr = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_PICICR);
1073 1.8.16.3 yamt picicr = sc->sc_picicr;
1074 1.8.16.3 yamt if (elansc_pcinmi || elansc_wpvnmi)
1075 1.8.16.3 yamt picicr |= MMCR_PICICR_NMI_ENB;
1076 1.8.16.3 yamt #if 0
1077 1.8.16.3 yamt /* PC/AT compatibility */
1078 1.8.16.3 yamt picicr |= MMCR_PICICR_S1_GINT_MODE|MMCR_PICICR_M_GINT_MODE;
1079 1.8.16.3 yamt #endif
1080 1.8.16.3 yamt bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_PICICR, picicr);
1081 1.1 thorpej
1082 1.8.16.3 yamt elansc_print_1(self, sc, MMCR_PICICR);
1083 1.8.16.3 yamt elansc_print_1(self, sc, MMCR_MPICMODE);
1084 1.8.16.3 yamt
1085 1.8.16.3 yamt mutex_enter(&sc->sc_mtx);
1086 1.1 thorpej /* Set up the watchdog registers with some defaults. */
1087 1.1 thorpej elansc_wdogctl_write(sc, WDTMRCTL_WRST_ENB | WDTMRCTL_EXP_SEL30);
1088 1.1 thorpej
1089 1.1 thorpej /* ...and clear it. */
1090 1.1 thorpej elansc_wdogctl_reset(sc);
1091 1.8.16.3 yamt mutex_exit(&sc->sc_mtx);
1092 1.8.16.3 yamt
1093 1.8.16.3 yamt if (!pmf_device_register(self, elansc_suspend, elansc_resume))
1094 1.8.16.3 yamt aprint_error_dev(self, "could not establish power hooks\n");
1095 1.8.16.1 yamt
1096 1.8.16.1 yamt #if NGPIO > 0
1097 1.8.16.1 yamt /* Initialize GPIO pins array */
1098 1.8.16.1 yamt for (pin = 0; pin < ELANSC_PIO_NPINS; pin++) {
1099 1.8.16.1 yamt sc->sc_gpio_pins[pin].pin_num = pin;
1100 1.8.16.1 yamt sc->sc_gpio_pins[pin].pin_caps = GPIO_PIN_INPUT |
1101 1.8.16.1 yamt GPIO_PIN_OUTPUT;
1102 1.8.16.1 yamt
1103 1.8.16.1 yamt /* Read initial state */
1104 1.8.16.1 yamt reg = (pin < 16 ? MMCR_PIODIR15_0 : MMCR_PIODIR31_16);
1105 1.8.16.1 yamt shift = pin % 16;
1106 1.8.16.1 yamt data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
1107 1.8.16.1 yamt if ((data & (1 << shift)) == 0)
1108 1.8.16.1 yamt sc->sc_gpio_pins[pin].pin_flags = GPIO_PIN_INPUT;
1109 1.8.16.1 yamt else
1110 1.8.16.1 yamt sc->sc_gpio_pins[pin].pin_flags = GPIO_PIN_OUTPUT;
1111 1.8.16.1 yamt if (elansc_gpio_pin_read(sc, pin) == 0)
1112 1.8.16.1 yamt sc->sc_gpio_pins[pin].pin_state = GPIO_PIN_LOW;
1113 1.8.16.1 yamt else
1114 1.8.16.1 yamt sc->sc_gpio_pins[pin].pin_state = GPIO_PIN_HIGH;
1115 1.8.16.1 yamt }
1116 1.8.16.1 yamt
1117 1.8.16.1 yamt /* Create controller tag */
1118 1.8.16.1 yamt sc->sc_gpio_gc.gp_cookie = sc;
1119 1.8.16.1 yamt sc->sc_gpio_gc.gp_pin_read = elansc_gpio_pin_read;
1120 1.8.16.1 yamt sc->sc_gpio_gc.gp_pin_write = elansc_gpio_pin_write;
1121 1.8.16.1 yamt sc->sc_gpio_gc.gp_pin_ctl = elansc_gpio_pin_ctl;
1122 1.8.16.1 yamt
1123 1.8.16.1 yamt gba.gba_gc = &sc->sc_gpio_gc;
1124 1.8.16.1 yamt gba.gba_pins = sc->sc_gpio_pins;
1125 1.8.16.1 yamt gba.gba_npins = ELANSC_PIO_NPINS;
1126 1.8.16.1 yamt
1127 1.8.16.3 yamt sc->sc_par = config_found_ia(&sc->sc_dev, "elanparbus", NULL, NULL);
1128 1.8.16.3 yamt sc->sc_pex = config_found_ia(&sc->sc_dev, "elanpexbus", NULL, NULL);
1129 1.8.16.1 yamt /* Attach GPIO framework */
1130 1.8.16.1 yamt config_found_ia(&sc->sc_dev, "gpiobus", &gba, gpiobus_print);
1131 1.8.16.1 yamt #endif /* NGPIO */
1132 1.8.16.3 yamt
1133 1.8.16.3 yamt /*
1134 1.8.16.3 yamt * Hook up the watchdog timer.
1135 1.8.16.3 yamt */
1136 1.8.16.3 yamt sc->sc_smw.smw_name = device_xname(&sc->sc_dev);
1137 1.8.16.3 yamt sc->sc_smw.smw_cookie = sc;
1138 1.8.16.3 yamt sc->sc_smw.smw_setmode = elansc_wdog_setmode;
1139 1.8.16.3 yamt sc->sc_smw.smw_tickle = elansc_wdog_tickle;
1140 1.8.16.3 yamt sc->sc_smw.smw_period = 32; /* actually 32.54 */
1141 1.8.16.3 yamt if (sysmon_wdog_register(&sc->sc_smw) != 0) {
1142 1.8.16.3 yamt aprint_error_dev(&sc->sc_dev,
1143 1.8.16.3 yamt "unable to register watchdog with sysmon\n");
1144 1.8.16.3 yamt }
1145 1.8.16.3 yamt }
1146 1.8.16.3 yamt
1147 1.8.16.3 yamt static int
1148 1.8.16.3 yamt elanpex_match(device_t parent, struct cfdata *match, void *aux)
1149 1.8.16.3 yamt {
1150 1.8.16.3 yamt struct elansc_softc *sc = device_private(parent);
1151 1.8.16.3 yamt
1152 1.8.16.3 yamt return sc->sc_pex == NULL;
1153 1.1 thorpej }
1154 1.1 thorpej
1155 1.8.16.3 yamt static int
1156 1.8.16.3 yamt elanpar_match(device_t parent, struct cfdata *match, void *aux)
1157 1.8.16.3 yamt {
1158 1.8.16.3 yamt struct elansc_softc *sc = device_private(parent);
1159 1.8.16.3 yamt
1160 1.8.16.3 yamt return sc->sc_par == NULL;
1161 1.8.16.3 yamt }
1162 1.8.16.3 yamt
1163 1.8.16.3 yamt CFATTACH_DECL_NEW(elanpar, sizeof(struct device),
1164 1.8.16.3 yamt elanpar_match, elanpar_attach, elanpar_detach, NULL);
1165 1.8.16.3 yamt
1166 1.8.16.3 yamt CFATTACH_DECL_NEW(elanpex, sizeof(struct device),
1167 1.8.16.3 yamt elanpex_match, elanpex_attach, elanpex_detach, NULL);
1168 1.8.16.3 yamt
1169 1.8.16.3 yamt CFATTACH_DECL2(elansc, sizeof(struct elansc_softc),
1170 1.8.16.3 yamt elansc_match, elansc_attach, elansc_detach, NULL, NULL,
1171 1.8.16.3 yamt elansc_childdetached);
1172 1.8.16.1 yamt
1173 1.8.16.1 yamt #if NGPIO > 0
1174 1.8.16.1 yamt static int
1175 1.8.16.1 yamt elansc_gpio_pin_read(void *arg, int pin)
1176 1.8.16.1 yamt {
1177 1.8.16.1 yamt struct elansc_softc *sc = arg;
1178 1.8.16.1 yamt int reg, shift;
1179 1.8.16.1 yamt uint16_t data;
1180 1.8.16.1 yamt
1181 1.8.16.1 yamt reg = (pin < 16 ? MMCR_PIODATA15_0 : MMCR_PIODATA31_16);
1182 1.8.16.1 yamt shift = pin % 16;
1183 1.8.16.3 yamt
1184 1.8.16.3 yamt mutex_enter(&sc->sc_mtx);
1185 1.8.16.1 yamt data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
1186 1.8.16.3 yamt mutex_exit(&sc->sc_mtx);
1187 1.8.16.1 yamt
1188 1.8.16.1 yamt return ((data >> shift) & 0x1);
1189 1.8.16.1 yamt }
1190 1.8.16.1 yamt
1191 1.8.16.1 yamt static void
1192 1.8.16.1 yamt elansc_gpio_pin_write(void *arg, int pin, int value)
1193 1.8.16.1 yamt {
1194 1.8.16.1 yamt struct elansc_softc *sc = arg;
1195 1.8.16.1 yamt int reg, shift;
1196 1.8.16.1 yamt uint16_t data;
1197 1.8.16.1 yamt
1198 1.8.16.1 yamt reg = (pin < 16 ? MMCR_PIODATA15_0 : MMCR_PIODATA31_16);
1199 1.8.16.1 yamt shift = pin % 16;
1200 1.8.16.3 yamt
1201 1.8.16.3 yamt mutex_enter(&sc->sc_mtx);
1202 1.8.16.1 yamt data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
1203 1.8.16.1 yamt if (value == 0)
1204 1.8.16.1 yamt data &= ~(1 << shift);
1205 1.8.16.1 yamt else if (value == 1)
1206 1.8.16.1 yamt data |= (1 << shift);
1207 1.8.16.1 yamt
1208 1.8.16.1 yamt bus_space_write_2(sc->sc_memt, sc->sc_memh, reg, data);
1209 1.8.16.3 yamt mutex_exit(&sc->sc_mtx);
1210 1.8.16.1 yamt }
1211 1.8.16.1 yamt
1212 1.8.16.1 yamt static void
1213 1.8.16.1 yamt elansc_gpio_pin_ctl(void *arg, int pin, int flags)
1214 1.8.16.1 yamt {
1215 1.8.16.1 yamt struct elansc_softc *sc = arg;
1216 1.8.16.1 yamt int reg, shift;
1217 1.8.16.1 yamt uint16_t data;
1218 1.8.16.1 yamt
1219 1.8.16.1 yamt reg = (pin < 16 ? MMCR_PIODIR15_0 : MMCR_PIODIR31_16);
1220 1.8.16.1 yamt shift = pin % 16;
1221 1.8.16.3 yamt mutex_enter(&sc->sc_mtx);
1222 1.8.16.1 yamt data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
1223 1.8.16.1 yamt if (flags & GPIO_PIN_INPUT)
1224 1.8.16.1 yamt data &= ~(1 << shift);
1225 1.8.16.1 yamt if (flags & GPIO_PIN_OUTPUT)
1226 1.8.16.1 yamt data |= (1 << shift);
1227 1.8.16.1 yamt
1228 1.8.16.1 yamt bus_space_write_2(sc->sc_memt, sc->sc_memh, reg, data);
1229 1.8.16.3 yamt mutex_exit(&sc->sc_mtx);
1230 1.8.16.1 yamt }
1231 1.8.16.1 yamt #endif /* NGPIO */
1232