elan520.c revision 1.14 1 1.14 thorpej /* $NetBSD: elan520.c,v 1.14 2006/02/19 14:59:23 thorpej 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.14 thorpej __KERNEL_RCSID(0, "$NetBSD: elan520.c,v 1.14 2006/02/19 14:59:23 thorpej Exp $");
51 1.1 thorpej
52 1.1 thorpej #include <sys/param.h>
53 1.1 thorpej #include <sys/systm.h>
54 1.1 thorpej #include <sys/device.h>
55 1.1 thorpej #include <sys/wdog.h>
56 1.9 riz #include <sys/gpio.h>
57 1.1 thorpej
58 1.5 thorpej #include <uvm/uvm_extern.h>
59 1.5 thorpej
60 1.1 thorpej #include <machine/bus.h>
61 1.1 thorpej
62 1.1 thorpej #include <dev/pci/pcivar.h>
63 1.1 thorpej
64 1.1 thorpej #include <dev/pci/pcidevs.h>
65 1.1 thorpej
66 1.10 drochner #include "gpio.h"
67 1.10 drochner #if NGPIO > 0
68 1.9 riz #include <dev/gpio/gpiovar.h>
69 1.10 drochner #endif
70 1.9 riz
71 1.1 thorpej #include <arch/i386/pci/elan520reg.h>
72 1.1 thorpej
73 1.1 thorpej #include <dev/sysmon/sysmonvar.h>
74 1.1 thorpej
75 1.1 thorpej struct elansc_softc {
76 1.1 thorpej struct device sc_dev;
77 1.1 thorpej bus_space_tag_t sc_memt;
78 1.1 thorpej bus_space_handle_t sc_memh;
79 1.1 thorpej int sc_echobug;
80 1.1 thorpej
81 1.1 thorpej struct sysmon_wdog sc_smw;
82 1.11 riz #if NGPIO > 0
83 1.9 riz /* GPIO interface */
84 1.9 riz struct gpio_chipset_tag sc_gpio_gc;
85 1.9 riz gpio_pin_t sc_gpio_pins[ELANSC_PIO_NPINS];
86 1.11 riz #endif
87 1.1 thorpej };
88 1.1 thorpej
89 1.10 drochner #if NGPIO > 0
90 1.9 riz static int elansc_gpio_pin_read(void *, int);
91 1.9 riz static void elansc_gpio_pin_write(void *, int, int);
92 1.9 riz static void elansc_gpio_pin_ctl(void *, int, int);
93 1.10 drochner #endif
94 1.9 riz
95 1.1 thorpej static void
96 1.1 thorpej elansc_wdogctl_write(struct elansc_softc *sc, uint16_t val)
97 1.1 thorpej {
98 1.1 thorpej int s;
99 1.6 christos uint8_t echo_mode = 0; /* XXX: gcc */
100 1.1 thorpej
101 1.1 thorpej s = splhigh();
102 1.1 thorpej
103 1.1 thorpej /* Switch off GP bus echo mode if we need to. */
104 1.1 thorpej if (sc->sc_echobug) {
105 1.1 thorpej echo_mode = bus_space_read_1(sc->sc_memt, sc->sc_memh,
106 1.1 thorpej MMCR_GPECHO);
107 1.1 thorpej bus_space_write_1(sc->sc_memt, sc->sc_memh,
108 1.1 thorpej MMCR_GPECHO, echo_mode & ~GPECHO_GP_ECHO_ENB);
109 1.1 thorpej }
110 1.1 thorpej
111 1.1 thorpej /* Unlock the register. */
112 1.1 thorpej bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
113 1.1 thorpej WDTMRCTL_UNLOCK1);
114 1.1 thorpej bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
115 1.1 thorpej WDTMRCTL_UNLOCK2);
116 1.1 thorpej
117 1.1 thorpej /* Write the value. */
118 1.1 thorpej bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL, val);
119 1.1 thorpej
120 1.1 thorpej /* Switch GP bus echo mode back. */
121 1.1 thorpej if (sc->sc_echobug)
122 1.1 thorpej bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_GPECHO,
123 1.1 thorpej echo_mode);
124 1.1 thorpej
125 1.1 thorpej splx(s);
126 1.1 thorpej }
127 1.1 thorpej
128 1.1 thorpej static void
129 1.1 thorpej elansc_wdogctl_reset(struct elansc_softc *sc)
130 1.1 thorpej {
131 1.1 thorpej int s;
132 1.7 christos uint8_t echo_mode = 0/* XXX: gcc */;
133 1.1 thorpej
134 1.1 thorpej s = splhigh();
135 1.1 thorpej
136 1.1 thorpej /* Switch off GP bus echo mode if we need to. */
137 1.1 thorpej if (sc->sc_echobug) {
138 1.1 thorpej echo_mode = bus_space_read_1(sc->sc_memt, sc->sc_memh,
139 1.1 thorpej MMCR_GPECHO);
140 1.1 thorpej bus_space_write_1(sc->sc_memt, sc->sc_memh,
141 1.1 thorpej MMCR_GPECHO, echo_mode & ~GPECHO_GP_ECHO_ENB);
142 1.1 thorpej }
143 1.1 thorpej
144 1.1 thorpej /* Reset the watchdog. */
145 1.1 thorpej bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
146 1.1 thorpej WDTMRCTL_RESET1);
147 1.1 thorpej bus_space_write_2(sc->sc_memt, sc->sc_memh, MMCR_WDTMRCTL,
148 1.1 thorpej WDTMRCTL_RESET2);
149 1.1 thorpej
150 1.1 thorpej /* Switch GP bus echo mode back. */
151 1.1 thorpej if (sc->sc_echobug)
152 1.1 thorpej bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_GPECHO,
153 1.1 thorpej echo_mode);
154 1.1 thorpej
155 1.1 thorpej splx(s);
156 1.1 thorpej }
157 1.1 thorpej
158 1.1 thorpej static const struct {
159 1.1 thorpej int period; /* whole seconds */
160 1.1 thorpej uint16_t exp; /* exponent select */
161 1.1 thorpej } elansc_wdog_periods[] = {
162 1.1 thorpej { 1, WDTMRCTL_EXP_SEL25 },
163 1.1 thorpej { 2, WDTMRCTL_EXP_SEL26 },
164 1.1 thorpej { 4, WDTMRCTL_EXP_SEL27 },
165 1.1 thorpej { 8, WDTMRCTL_EXP_SEL28 },
166 1.1 thorpej { 16, WDTMRCTL_EXP_SEL29 },
167 1.1 thorpej { 32, WDTMRCTL_EXP_SEL30 },
168 1.1 thorpej { 0, 0 },
169 1.1 thorpej };
170 1.1 thorpej
171 1.1 thorpej static int
172 1.1 thorpej elansc_wdog_setmode(struct sysmon_wdog *smw)
173 1.1 thorpej {
174 1.1 thorpej struct elansc_softc *sc = smw->smw_cookie;
175 1.1 thorpej int i;
176 1.7 christos uint16_t exp_sel = 0; /* XXX: gcc */
177 1.1 thorpej
178 1.1 thorpej if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
179 1.1 thorpej elansc_wdogctl_write(sc,
180 1.1 thorpej WDTMRCTL_WRST_ENB | WDTMRCTL_EXP_SEL30);
181 1.1 thorpej } else {
182 1.1 thorpej if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
183 1.1 thorpej smw->smw_period = 32;
184 1.1 thorpej exp_sel = WDTMRCTL_EXP_SEL30;
185 1.1 thorpej } else {
186 1.1 thorpej for (i = 0; elansc_wdog_periods[i].period != 0; i++) {
187 1.1 thorpej if (elansc_wdog_periods[i].period ==
188 1.1 thorpej smw->smw_period) {
189 1.1 thorpej exp_sel = elansc_wdog_periods[i].exp;
190 1.1 thorpej break;
191 1.1 thorpej }
192 1.1 thorpej }
193 1.1 thorpej if (elansc_wdog_periods[i].period == 0)
194 1.1 thorpej return (EINVAL);
195 1.1 thorpej }
196 1.1 thorpej elansc_wdogctl_write(sc, WDTMRCTL_ENB |
197 1.1 thorpej WDTMRCTL_WRST_ENB | exp_sel);
198 1.1 thorpej elansc_wdogctl_reset(sc);
199 1.1 thorpej }
200 1.1 thorpej return (0);
201 1.1 thorpej }
202 1.1 thorpej
203 1.1 thorpej static int
204 1.1 thorpej elansc_wdog_tickle(struct sysmon_wdog *smw)
205 1.1 thorpej {
206 1.1 thorpej struct elansc_softc *sc = smw->smw_cookie;
207 1.1 thorpej
208 1.1 thorpej elansc_wdogctl_reset(sc);
209 1.1 thorpej return (0);
210 1.1 thorpej }
211 1.1 thorpej
212 1.1 thorpej static int
213 1.1 thorpej elansc_match(struct device *parent, struct cfdata *match, void *aux)
214 1.1 thorpej {
215 1.1 thorpej struct pci_attach_args *pa = aux;
216 1.1 thorpej
217 1.1 thorpej if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD &&
218 1.1 thorpej PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_SC520_SC)
219 1.1 thorpej return (10); /* beat pchb */
220 1.1 thorpej
221 1.1 thorpej return (0);
222 1.1 thorpej }
223 1.1 thorpej
224 1.1 thorpej static const char *elansc_speeds[] = {
225 1.1 thorpej "(reserved 00)",
226 1.1 thorpej "100MHz",
227 1.1 thorpej "133MHz",
228 1.1 thorpej "(reserved 11)",
229 1.1 thorpej };
230 1.1 thorpej
231 1.1 thorpej static void
232 1.1 thorpej elansc_attach(struct device *parent, struct device *self, void *aux)
233 1.1 thorpej {
234 1.1 thorpej struct elansc_softc *sc = (void *) self;
235 1.1 thorpej struct pci_attach_args *pa = aux;
236 1.1 thorpej uint16_t rev;
237 1.1 thorpej uint8_t ressta, cpuctl;
238 1.10 drochner #if NGPIO > 0
239 1.10 drochner struct gpiobus_attach_args gba;
240 1.9 riz int pin;
241 1.9 riz int reg, shift;
242 1.9 riz uint16_t data;
243 1.10 drochner #endif
244 1.1 thorpej
245 1.14 thorpej aprint_naive(": System Controller\n");
246 1.14 thorpej aprint_normal(": AMD Elan SC520 System Controller\n");
247 1.1 thorpej
248 1.1 thorpej sc->sc_memt = pa->pa_memt;
249 1.5 thorpej if (bus_space_map(sc->sc_memt, MMCR_BASE_ADDR, PAGE_SIZE, 0,
250 1.1 thorpej &sc->sc_memh) != 0) {
251 1.14 thorpej aprint_error("%s: unable to map registers\n",
252 1.14 thorpej sc->sc_dev.dv_xname);
253 1.1 thorpej return;
254 1.1 thorpej }
255 1.1 thorpej
256 1.1 thorpej rev = bus_space_read_2(sc->sc_memt, sc->sc_memh, MMCR_REVID);
257 1.1 thorpej cpuctl = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_CPUCTL);
258 1.1 thorpej
259 1.14 thorpej aprint_normal("%s: product %d stepping %d.%d, CPU clock %s\n",
260 1.1 thorpej sc->sc_dev.dv_xname,
261 1.1 thorpej (rev & REVID_PRODID) >> REVID_PRODID_SHIFT,
262 1.1 thorpej (rev & REVID_MAJSTEP) >> REVID_MAJSTEP_SHIFT,
263 1.1 thorpej (rev & REVID_MINSTEP),
264 1.1 thorpej elansc_speeds[cpuctl & CPUCTL_CPU_CLK_SPD_MASK]);
265 1.1 thorpej
266 1.1 thorpej /*
267 1.1 thorpej * SC520 rev A1 has a bug that affects the watchdog timer. If
268 1.1 thorpej * the GP bus echo mode is enabled, writing to the watchdog control
269 1.1 thorpej * register is blocked.
270 1.1 thorpej *
271 1.1 thorpej * The BIOS in some systems (e.g. the Soekris net4501) enables
272 1.1 thorpej * GP bus echo for various reasons, so we need to switch it off
273 1.1 thorpej * when we talk to the watchdog timer.
274 1.1 thorpej *
275 1.1 thorpej * XXX The step 1.1 (B1?) in my Soekris net4501 also has this
276 1.1 thorpej * XXX problem, so we'll just enable it for all Elan SC520s
277 1.8 keihan * XXX for now. --thorpej (at) NetBSD.org
278 1.1 thorpej */
279 1.1 thorpej if (1 || rev == ((PRODID_ELAN_SC520 << REVID_PRODID_SHIFT) |
280 1.1 thorpej (0 << REVID_MAJSTEP_SHIFT) | (1)))
281 1.1 thorpej sc->sc_echobug = 1;
282 1.1 thorpej
283 1.1 thorpej /*
284 1.1 thorpej * Determine cause of the last reset, and issue a warning if it
285 1.1 thorpej * was due to watchdog expiry.
286 1.1 thorpej */
287 1.1 thorpej ressta = bus_space_read_1(sc->sc_memt, sc->sc_memh, MMCR_RESSTA);
288 1.1 thorpej if (ressta & RESSTA_WDT_RST_DET)
289 1.14 thorpej aprint_error(
290 1.14 thorpej "%s: WARNING: LAST RESET DUE TO WATCHDOG EXPIRATION!\n",
291 1.1 thorpej sc->sc_dev.dv_xname);
292 1.1 thorpej bus_space_write_1(sc->sc_memt, sc->sc_memh, MMCR_RESSTA, ressta);
293 1.1 thorpej
294 1.1 thorpej /*
295 1.1 thorpej * Hook up the watchdog timer.
296 1.1 thorpej */
297 1.1 thorpej sc->sc_smw.smw_name = sc->sc_dev.dv_xname;
298 1.1 thorpej sc->sc_smw.smw_cookie = sc;
299 1.1 thorpej sc->sc_smw.smw_setmode = elansc_wdog_setmode;
300 1.1 thorpej sc->sc_smw.smw_tickle = elansc_wdog_tickle;
301 1.1 thorpej sc->sc_smw.smw_period = 32; /* actually 32.54 */
302 1.1 thorpej if (sysmon_wdog_register(&sc->sc_smw) != 0)
303 1.14 thorpej aprint_error("%s: unable to register watchdog with sysmon\n",
304 1.1 thorpej sc->sc_dev.dv_xname);
305 1.1 thorpej
306 1.1 thorpej /* Set up the watchdog registers with some defaults. */
307 1.1 thorpej elansc_wdogctl_write(sc, WDTMRCTL_WRST_ENB | WDTMRCTL_EXP_SEL30);
308 1.1 thorpej
309 1.1 thorpej /* ...and clear it. */
310 1.1 thorpej elansc_wdogctl_reset(sc);
311 1.9 riz
312 1.10 drochner #if NGPIO > 0
313 1.9 riz /* Initialize GPIO pins array */
314 1.9 riz for (pin = 0; pin < ELANSC_PIO_NPINS; pin++) {
315 1.9 riz sc->sc_gpio_pins[pin].pin_num = pin;
316 1.9 riz sc->sc_gpio_pins[pin].pin_caps = GPIO_PIN_INPUT |
317 1.9 riz GPIO_PIN_OUTPUT;
318 1.9 riz
319 1.9 riz /* Read initial state */
320 1.9 riz reg = (pin < 16 ? MMCR_PIODIR15_0 : MMCR_PIODIR31_16);
321 1.9 riz shift = pin % 16;
322 1.9 riz data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
323 1.9 riz if ((data & (1 << shift)) == 0)
324 1.9 riz sc->sc_gpio_pins[pin].pin_flags = GPIO_PIN_INPUT;
325 1.9 riz else
326 1.9 riz sc->sc_gpio_pins[pin].pin_flags = GPIO_PIN_OUTPUT;
327 1.9 riz if (elansc_gpio_pin_read(sc, pin) == 0)
328 1.9 riz sc->sc_gpio_pins[pin].pin_state = GPIO_PIN_LOW;
329 1.9 riz else
330 1.9 riz sc->sc_gpio_pins[pin].pin_state = GPIO_PIN_HIGH;
331 1.9 riz }
332 1.9 riz
333 1.9 riz /* Create controller tag */
334 1.9 riz sc->sc_gpio_gc.gp_cookie = sc;
335 1.9 riz sc->sc_gpio_gc.gp_pin_read = elansc_gpio_pin_read;
336 1.9 riz sc->sc_gpio_gc.gp_pin_write = elansc_gpio_pin_write;
337 1.9 riz sc->sc_gpio_gc.gp_pin_ctl = elansc_gpio_pin_ctl;
338 1.9 riz
339 1.9 riz gba.gba_gc = &sc->sc_gpio_gc;
340 1.9 riz gba.gba_pins = sc->sc_gpio_pins;
341 1.9 riz gba.gba_npins = ELANSC_PIO_NPINS;
342 1.9 riz
343 1.9 riz /* Attach GPIO framework */
344 1.10 drochner config_found_ia(&sc->sc_dev, "gpiobus", &gba, gpiobus_print);
345 1.10 drochner #endif /* NGPIO */
346 1.1 thorpej }
347 1.1 thorpej
348 1.4 thorpej CFATTACH_DECL(elansc, sizeof(struct elansc_softc),
349 1.4 thorpej elansc_match, elansc_attach, NULL, NULL);
350 1.9 riz
351 1.10 drochner #if NGPIO > 0
352 1.9 riz static int
353 1.9 riz elansc_gpio_pin_read(void *arg, int pin)
354 1.9 riz {
355 1.9 riz struct elansc_softc *sc = arg;
356 1.9 riz int reg, shift;
357 1.13 perry uint16_t data;
358 1.9 riz
359 1.9 riz reg = (pin < 16 ? MMCR_PIODATA15_0 : MMCR_PIODATA31_16);
360 1.9 riz shift = pin % 16;
361 1.9 riz data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
362 1.9 riz
363 1.9 riz return ((data >> shift) & 0x1);
364 1.9 riz }
365 1.9 riz
366 1.9 riz static void
367 1.9 riz elansc_gpio_pin_write(void *arg, int pin, int value)
368 1.9 riz {
369 1.9 riz struct elansc_softc *sc = arg;
370 1.9 riz int reg, shift;
371 1.13 perry uint16_t data;
372 1.9 riz
373 1.9 riz reg = (pin < 16 ? MMCR_PIODATA15_0 : MMCR_PIODATA31_16);
374 1.9 riz shift = pin % 16;
375 1.9 riz data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
376 1.9 riz if (value == 0)
377 1.9 riz data &= ~(1 << shift);
378 1.9 riz else if (value == 1)
379 1.9 riz data |= (1 << shift);
380 1.9 riz
381 1.9 riz bus_space_write_2(sc->sc_memt, sc->sc_memh, reg, data);
382 1.9 riz }
383 1.9 riz
384 1.9 riz static void
385 1.9 riz elansc_gpio_pin_ctl(void *arg, int pin, int flags)
386 1.9 riz {
387 1.9 riz struct elansc_softc *sc = arg;
388 1.9 riz int reg, shift;
389 1.13 perry uint16_t data;
390 1.9 riz
391 1.9 riz reg = (pin < 16 ? MMCR_PIODIR15_0 : MMCR_PIODIR31_16);
392 1.9 riz shift = pin % 16;
393 1.9 riz data = bus_space_read_2(sc->sc_memt, sc->sc_memh, reg);
394 1.9 riz if (flags & GPIO_PIN_INPUT)
395 1.9 riz data &= ~(1 << shift);
396 1.9 riz if (flags & GPIO_PIN_OUTPUT)
397 1.9 riz data |= (1 << shift);
398 1.9 riz
399 1.9 riz bus_space_write_2(sc->sc_memt, sc->sc_memh, reg, data);
400 1.9 riz }
401 1.10 drochner #endif /* NGPIO */
402