2 * Copyright (c) 2007 Marc Balmer <mbalmer@openbsd.org>
3 * Copyright (c) 2007 Michael Shalayeff
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
15 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
21 * This code is derived from software contributed to The DragonFly Project
22 * by Alex Hornung <ahornung@gmail.com>
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
28 * 1. Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in
32 * the documentation and/or other materials provided with the
34 * 3. Neither the name of The DragonFly Project nor the names of its
35 * contributors may be used to endorse or promote products derived
36 * from this software without specific, prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
41 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
42 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
43 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
44 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
46 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/systimer.h>
60 #include <sys/kernel.h>
61 #include <sys/module.h>
62 #include <bus/pci/pcireg.h>
63 #include <bus/pci/pcivar.h>
64 #include <bus/pci/pcidevs.h>
65 #include <bus/pci/pcib_private.h>
66 #include <dev/misc/gpio/gpio.h>
67 #include <machine/pc/bios.h>
70 #define AMD5536_TIMER_FREQ 3579545
72 #define AMD5536_REV 0x51400017
73 #define AMD5536_REV_MASK 0xff
74 #define AMD5536_TMC 0x51400050
76 /* Multi-Functional General Purpose Timer */
77 #define MSR_LBAR_MFGPT 0x5140000d
78 #define AMD5536_MFGPT0_CMP1 0x00000000
79 #define AMD5536_MFGPT0_CMP2 0x00000002
80 #define AMD5536_MFGPT0_CNT 0x00000004
81 #define AMD5536_MFGPT0_SETUP 0x00000006
82 #define AMD5536_MFGPT_DIV_MASK 0x000f /* div = 1 << mask */
83 #define AMD5536_MFGPT_CLKSEL 0x0010
84 #define AMD5536_MFGPT_REV_EN 0x0020
85 #define AMD5536_MFGPT_CMP1DIS 0x0000
86 #define AMD5536_MFGPT_CMP1EQ 0x0040
87 #define AMD5536_MFGPT_CMP1GE 0x0080
88 #define AMD5536_MFGPT_CMP1EV 0x00c0
89 #define AMD5536_MFGPT_CMP2DIS 0x0000
90 #define AMD5536_MFGPT_CMP2EQ 0x0100
91 #define AMD5536_MFGPT_CMP2GE 0x0200
92 #define AMD5536_MFGPT_CMP2EV 0x0300
93 #define AMD5536_MFGPT_STOP_EN 0x0800
94 #define AMD5536_MFGPT_SET 0x1000
95 #define AMD5536_MFGPT_CMP1 0x2000
96 #define AMD5536_MFGPT_CMP2 0x4000
97 #define AMD5536_MFGPT_CNT_EN 0x8000
98 #define AMD5536_MFGPT_IRQ 0x51400028
99 #define AMD5536_MFGPT0_C1_IRQM 0x00000001
100 #define AMD5536_MFGPT1_C1_IRQM 0x00000002
101 #define AMD5536_MFGPT2_C1_IRQM 0x00000004
102 #define AMD5536_MFGPT3_C1_IRQM 0x00000008
103 #define AMD5536_MFGPT4_C1_IRQM 0x00000010
104 #define AMD5536_MFGPT5_C1_IRQM 0x00000020
105 #define AMD5536_MFGPT6_C1_IRQM 0x00000040
106 #define AMD5536_MFGPT7_C1_IRQM 0x00000080
107 #define AMD5536_MFGPT0_C2_IRQM 0x00000100
108 #define AMD5536_MFGPT1_C2_IRQM 0x00000200
109 #define AMD5536_MFGPT2_C2_IRQM 0x00000400
110 #define AMD5536_MFGPT3_C2_IRQM 0x00000800
111 #define AMD5536_MFGPT4_C2_IRQM 0x00001000
112 #define AMD5536_MFGPT5_C2_IRQM 0x00002000
113 #define AMD5536_MFGPT6_C2_IRQM 0x00004000
114 #define AMD5536_MFGPT7_C2_IRQM 0x00008000
115 #define AMD5536_MFGPT_NR 0x51400029
116 #define AMD5536_MFGPT0_C1_NMIM 0x00000001
117 #define AMD5536_MFGPT1_C1_NMIM 0x00000002
118 #define AMD5536_MFGPT2_C1_NMIM 0x00000004
119 #define AMD5536_MFGPT3_C1_NMIM 0x00000008
120 #define AMD5536_MFGPT4_C1_NMIM 0x00000010
121 #define AMD5536_MFGPT5_C1_NMIM 0x00000020
122 #define AMD5536_MFGPT6_C1_NMIM 0x00000040
123 #define AMD5536_MFGPT7_C1_NMIM 0x00000080
124 #define AMD5536_MFGPT0_C2_NMIM 0x00000100
125 #define AMD5536_MFGPT1_C2_NMIM 0x00000200
126 #define AMD5536_MFGPT2_C2_NMIM 0x00000400
127 #define AMD5536_MFGPT3_C2_NMIM 0x00000800
128 #define AMD5536_MFGPT4_C2_NMIM 0x00001000
129 #define AMD5536_MFGPT5_C2_NMIM 0x00002000
130 #define AMD5536_MFGPT6_C2_NMIM 0x00004000
131 #define AMD5536_MFGPT7_C2_NMIM 0x00008000
132 #define AMD5536_NMI_LEG 0x00010000
133 #define AMD5536_MFGPT0_C2_RSTEN 0x01000000
134 #define AMD5536_MFGPT1_C2_RSTEN 0x02000000
135 #define AMD5536_MFGPT2_C2_RSTEN 0x04000000
136 #define AMD5536_MFGPT3_C2_RSTEN 0x08000000
137 #define AMD5536_MFGPT4_C2_RSTEN 0x10000000
138 #define AMD5536_MFGPT5_C2_RSTEN 0x20000000
139 #define AMD5536_MFGPT_SETUP 0x5140002b
142 #define MSR_LBAR_GPIO 0x5140000c
143 #define AMD5536_GPIO_NPINS 32
144 #define AMD5536_GPIOH_OFFSET 0x80 /* high bank register offset */
145 #define AMD5536_GPIO_OUT_VAL 0x00 /* output value */
146 #define AMD5536_GPIO_OUT_EN 0x04 /* output enable */
147 #define AMD5536_GPIO_OD_EN 0x08 /* open-drain enable */
148 #define AMD5536_GPIO_OUT_INVRT_EN 0x0c /* invert output */
149 #define AMD5536_GPIO_PU_EN 0x18 /* pull-up enable */
150 #define AMD5536_GPIO_PD_EN 0x1c /* pull-down enable */
151 #define AMD5536_GPIO_IN_EN 0x20 /* input enable */
152 #define AMD5536_GPIO_IN_INVRT_EN 0x24 /* invert input */
153 #define AMD5536_GPIO_READ_BACK 0x30 /* read back value */
156 struct cs5536_softc {
157 bus_space_tag_t sc_iot;
158 bus_space_handle_t sc_ioh;
162 bus_space_tag_t sc_gpio_iot;
163 bus_space_handle_t sc_gpio_ioh;
164 struct gpio sc_gpio_gc;
165 gpio_pin_t sc_gpio_pins[AMD5536_GPIO_NPINS];
169 static struct cs5536_softc cs5536_sc;
172 int cs5536_gpio_pin_read(void *arg, int pin);
173 void cs5536_gpio_pin_write(void *arg, int pin, int value);
174 void cs5536_gpio_pin_ctl(void *arg, int pin, int flags);
176 static sysclock_t cs5536_get_timecount(void);
178 static struct bios_oem bios_soekris_55 = {
179 { 0xf0000, 0xf1000 },
181 { "Soekris", 0, 8 }, /* Soekris Engineering. */
182 { "net5", 0, 8 }, /* net5xxx */
183 { "comBIOS", 0, 54 }, /* comBIOS ver. 1.26a 20040819 ... */
188 static struct bios_oem bios_pcengines_55 = {
189 { 0xf9000, 0xfa000 },
191 { "PC Engines ALIX", 0, 28 }, /* PC Engines ALIX */
192 { "tinyBIOS", 0, 28 }, /* tinyBIOS V1.4a (C)1997-2005 */
199 cs5536_gpio_pin_read(void *arg, int pin)
205 port = rdmsr(MSR_LBAR_GPIO);
207 reg = AMD5536_GPIO_IN_EN;
210 off = AMD5536_GPIOH_OFFSET;
213 data = bus_space_read_4(cs5536_sc.sc_gpio_iot, cs5536_sc.sc_gpio_ioh, reg);
215 if (data & (1 << pin))
216 reg = AMD5536_GPIO_READ_BACK + off;
218 reg = AMD5536_GPIO_OUT_VAL + off;
220 data = bus_space_read_4(cs5536_sc.sc_gpio_iot, cs5536_sc.sc_gpio_ioh, reg);
222 return data & 1 << pin ? GPIO_PIN_HIGH : GPIO_PIN_LOW;
226 cs5536_gpio_pin_write(void *arg, int pin, int value)
232 port = rdmsr(MSR_LBAR_GPIO);
234 reg = AMD5536_GPIO_OUT_VAL;
237 reg += AMD5536_GPIOH_OFFSET;
243 data = 1 << (pin + 16);
245 bus_space_write_4(cs5536_sc.sc_gpio_iot, cs5536_sc.sc_gpio_ioh, reg, data);
246 //write_data_4(port, reg, data);
247 //outl(port + reg, data);
251 cs5536_gpio_pin_ctl(void *arg, int pin, int flags)
253 int n, reg[7], val[7], nreg = 0, off = 0;
257 off = AMD5536_GPIOH_OFFSET;
260 reg[nreg] = AMD5536_GPIO_IN_EN + off;
261 if (flags & GPIO_PIN_INPUT)
262 val[nreg++] = 1 << pin;
264 val[nreg++] = 1 << (pin + 16);
266 reg[nreg] = AMD5536_GPIO_OUT_EN + off;
267 if (flags & GPIO_PIN_OUTPUT)
268 val[nreg++] = 1 << pin;
270 val[nreg++] = 1 << (pin + 16);
272 reg[nreg] = AMD5536_GPIO_OD_EN + off;
273 if (flags & GPIO_PIN_OPENDRAIN)
274 val[nreg++] = 1 << pin;
276 val[nreg++] = 1 << (pin + 16);
278 reg[nreg] = AMD5536_GPIO_PU_EN + off;
279 if (flags & GPIO_PIN_PULLUP)
280 val[nreg++] = 1 << pin;
282 val[nreg++] = 1 << (pin + 16);
284 reg[nreg] = AMD5536_GPIO_PD_EN + off;
285 if (flags & GPIO_PIN_PULLDOWN)
286 val[nreg++] = 1 << pin;
288 val[nreg++] = 1 << (pin + 16);
290 reg[nreg] = AMD5536_GPIO_IN_INVRT_EN + off;
291 if (flags & GPIO_PIN_INVIN)
292 val[nreg++] = 1 << pin;
294 val[nreg++] = 1 << (pin + 16);
296 reg[nreg] = AMD5536_GPIO_OUT_INVRT_EN + off;
297 if (flags & GPIO_PIN_INVOUT)
298 val[nreg++] = 1 << pin;
300 val[nreg++] = 1 << (pin + 16);
303 for (n = 0; n < nreg; n++)
304 bus_space_write_4(cs5536_sc.sc_gpio_iot, cs5536_sc.sc_gpio_ioh, reg[n],
310 cs5536_cputimer_construct(struct cputimer *timer, sysclock_t oldclock)
313 timer->base = oldclock - cs5536_get_timecount();
316 static struct cputimer cs5536_timer = {
317 SLIST_ENTRY_INITIALIZER,
321 cs5536_get_timecount,
322 cputimer_default_fromhz,
323 cputimer_default_fromus,
324 cs5536_cputimer_construct,
325 cputimer_default_destruct,
331 cs5536_get_timecount(void)
333 return (cs5536_timer.base + rdmsr(AMD5536_TMC));
336 #ifdef WATCHDOG_ENABLE
338 cs5536_watchdog(void *unused, int period)
343 bus_space_write_2(cs5536_sc.sc_iot, cs5536_sc.sc_ioh, AMD5536_MFGPT0_SETUP,
344 AMD5536_MFGPT_CNT_EN | AMD5536_MFGPT_CMP2);
346 bus_space_write_2(cs5536_sc.sc_iot, cs5536_sc.sc_ioh, AMD5536_MFGPT0_CNT, 0);
347 /* set comparator 2 */
348 bus_space_write_2(cs5536_sc.sc_iot, cs5536_sc.sc_ioh, AMD5536_MFGPT0_CMP2, period);
351 wrmsr(AMD5536_MFGPT_NR,
352 rdmsr(AMD5536_MFGPT_NR) | AMD5536_MFGPT0_C2_RSTEN);
354 wrmsr(AMD5536_MFGPT_NR,
355 rdmsr(AMD5536_MFGPT_NR) & ~AMD5536_MFGPT0_C2_RSTEN);
361 static struct watchdog cs5536_wdog = {
362 .name = "AMD CS5536",
363 .wdog_fn = cs5536_watchdog,
365 .period_max = 0xffff,
367 #endif /* WATCHDOG_ENABLE */
370 cs5536_probe(device_t self)
372 static int probed = 0;
377 if (pci_get_vendor(self) == PCI_VENDOR_AMD &&
378 (pci_get_device(self) == PCI_PRODUCT_AMD_CS5536_PCIB ||
379 /* XXX: OpenBSD doesn't attach to this one, but free does, though no counter */
380 pci_get_device(self) == PCI_PRODUCT_AMD_GEODE_LX_PCHB)) {
381 /* device_set_desc(self, ...) */
390 cs5536_attach(device_t self)
392 #define BIOS_OEM_MAXLEN 80
393 static u_char bios_oem[BIOS_OEM_MAXLEN] = "\0";
394 static int attached = 0;
398 #if 0 /* Watchdog stuff */
399 EVENTHANDLER_REGISTER(watchdog_list, cs5536_watchdog, NULL, 0);
405 kprintf("AMD CS5536: rev %d, 32-bit %uHz timer\n",
406 (int)rdmsr(AMD5536_REV) & AMD5536_REV_MASK,
410 cputimer_register(&cs5536_timer);
411 cputimer_select(&cs5536_timer, 0);
413 /* bus_space_map(sc->sc_iot, wa & 0xffff, 64, 0, &sc->sc_ioh)) */
414 cs5536_sc.sc_iot = I386_BUS_SPACE_IO;
415 cs5536_sc.sc_ioh = rdmsr(MSR_LBAR_MFGPT);
417 #ifdef WATCHDOG_ENABLE
418 /* enable watchdog and configure */
419 bus_space_write_2(cs5536_sc.sc_iot, cs5536_sc.sc_ioh, AMD5536_MFGPT0_SETUP,
420 AMD5536_MFGPT_CNT_EN | AMD5536_MFGPT_CMP2EV |
421 AMD5536_MFGPT_CMP2 | AMD5536_MFGPT_DIV_MASK);
422 wdog_register(&cs5536_wdog);
423 #endif /* WATCHDOG_ENABLE */
426 /* bus_space_map(sc->sc_gpio_iot, ga & 0xffff, 0xff, 0,... */
427 cs5536_sc.sc_gpio_iot = I386_BUS_SPACE_IO;
428 cs5536_sc.sc_gpio_ioh = rdmsr(MSR_LBAR_GPIO);
429 for (i = 0; i < AMD5536_GPIO_NPINS; i++) {
430 cs5536_sc.sc_gpio_pins[i].pin_num = i;
431 cs5536_sc.sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
432 GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN |
433 GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN |
434 GPIO_PIN_INVIN | GPIO_PIN_INVOUT;
436 /* read initial state */
437 cs5536_sc.sc_gpio_pins[i].pin_state =
438 cs5536_gpio_pin_read(&cs5536_sc, i);
440 cs5536_sc.sc_gpio_gc.driver_name = "cs5536";
441 cs5536_sc.sc_gpio_gc.arg = &cs5536_sc;
442 cs5536_sc.sc_gpio_gc.pin_read = cs5536_gpio_pin_read;
443 cs5536_sc.sc_gpio_gc.pin_write = cs5536_gpio_pin_write;
444 cs5536_sc.sc_gpio_gc.pin_ctl = cs5536_gpio_pin_ctl;
445 cs5536_sc.sc_gpio_gc.pins = cs5536_sc.sc_gpio_pins;
446 cs5536_sc.sc_gpio_gc.npins = AMD5536_GPIO_NPINS;
447 gpio_register(&cs5536_sc.sc_gpio_gc);
449 if (bios_oem_strings(&bios_soekris_55,
450 bios_oem, sizeof bios_oem) > 0 ) {
452 /* Attach led to pin 6 */
453 gpio_consumer_attach("led", "error", &cs5536_sc.sc_gpio_gc,
456 } else if (bios_oem_strings(&bios_pcengines_55,
457 bios_oem, sizeof bios_oem) > 0 ) {
460 gpio_consumer_attach("led", "led1", &cs5536_sc.sc_gpio_gc,
462 gpio_consumer_attach("led", "led2", &cs5536_sc.sc_gpio_gc,
464 gpio_consumer_attach("led", "led3", &cs5536_sc.sc_gpio_gc,
469 * Turn on first LED so we don't make
470 * people think their box just died.
472 cs5536_led_func(&led1b, 1);
476 kprintf("Geode LX: %s\n", bios_oem);
478 kprintf("Geode LX: Unknown OEM bios\n");
481 kprintf("MFGPT bar: %jx\n", rdmsr(MSR_LBAR_MFGPT));
486 static device_method_t cs5536_methods[] = {
487 /* Device interface */
488 DEVMETHOD(device_probe, cs5536_probe),
489 DEVMETHOD(device_attach, cs5536_attach),
490 DEVMETHOD(device_suspend, bus_generic_suspend),
491 DEVMETHOD(device_resume, bus_generic_resume),
492 DEVMETHOD(device_shutdown, bus_generic_shutdown),
496 static driver_t cs5536_driver = {
502 static devclass_t cs5536_devclass;
504 DRIVER_MODULE(cs5536, pci, cs5536_driver, cs5536_devclass, NULL, NULL);