kernel/wdog: Compile in kern_wdog.c by default.
[dragonfly.git] / sys / platform / pc32 / i386 / cs5536.c
1 /*
2  * Copyright (c) 2007 Marc Balmer <mbalmer@openbsd.org>
3  * Copyright (c) 2007 Michael Shalayeff
4  * All rights reserved.
5  *
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.
9  *
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.
17  *
18  *
19  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
20  *
21  * This code is derived from software contributed to The DragonFly Project
22  * by Alex Hornung <ahornung@gmail.com>
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions
26  * are met:
27  *
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
33  *    distribution.
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.
37  *
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
49  * SUCH DAMAGE.
50  *
51  */
52
53 #include "use_gpio.h"
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/systimer.h>
58 #include <sys/bus.h>
59 #include <sys/kernel.h>
60 #include <sys/module.h>
61 #include <bus/pci/pcireg.h>
62 #include <bus/pci/pcivar.h>
63 #include <bus/pci/pcidevs.h>
64 #include <bus/pci/pcib_private.h>
65 #include <dev/misc/gpio/gpio.h>
66 #include <machine/pc/bios.h>
67 #include <sys/wdog.h>
68
69 #define AMD5536_TIMER_FREQ      3579545
70
71 #define AMD5536_REV             0x51400017
72 #define AMD5536_REV_MASK        0xff
73 #define AMD5536_TMC             0x51400050
74
75 /* Multi-Functional General Purpose Timer */
76 #define MSR_LBAR_MFGPT          0x5140000d
77 #define AMD5536_MFGPT0_CMP1     0x00000000
78 #define AMD5536_MFGPT0_CMP2     0x00000002
79 #define AMD5536_MFGPT0_CNT      0x00000004
80 #define AMD5536_MFGPT0_SETUP    0x00000006
81 #define AMD5536_MFGPT_DIV_MASK  0x000f  /* div = 1 << mask */
82 #define AMD5536_MFGPT_CLKSEL    0x0010
83 #define AMD5536_MFGPT_REV_EN    0x0020
84 #define AMD5536_MFGPT_CMP1DIS   0x0000
85 #define AMD5536_MFGPT_CMP1EQ    0x0040
86 #define AMD5536_MFGPT_CMP1GE    0x0080
87 #define AMD5536_MFGPT_CMP1EV    0x00c0
88 #define AMD5536_MFGPT_CMP2DIS   0x0000
89 #define AMD5536_MFGPT_CMP2EQ    0x0100
90 #define AMD5536_MFGPT_CMP2GE    0x0200
91 #define AMD5536_MFGPT_CMP2EV    0x0300
92 #define AMD5536_MFGPT_STOP_EN   0x0800
93 #define AMD5536_MFGPT_SET       0x1000
94 #define AMD5536_MFGPT_CMP1      0x2000
95 #define AMD5536_MFGPT_CMP2      0x4000
96 #define AMD5536_MFGPT_CNT_EN    0x8000
97 #define AMD5536_MFGPT_IRQ       0x51400028
98 #define AMD5536_MFGPT0_C1_IRQM  0x00000001
99 #define AMD5536_MFGPT1_C1_IRQM  0x00000002
100 #define AMD5536_MFGPT2_C1_IRQM  0x00000004
101 #define AMD5536_MFGPT3_C1_IRQM  0x00000008
102 #define AMD5536_MFGPT4_C1_IRQM  0x00000010
103 #define AMD5536_MFGPT5_C1_IRQM  0x00000020
104 #define AMD5536_MFGPT6_C1_IRQM  0x00000040
105 #define AMD5536_MFGPT7_C1_IRQM  0x00000080
106 #define AMD5536_MFGPT0_C2_IRQM  0x00000100
107 #define AMD5536_MFGPT1_C2_IRQM  0x00000200
108 #define AMD5536_MFGPT2_C2_IRQM  0x00000400
109 #define AMD5536_MFGPT3_C2_IRQM  0x00000800
110 #define AMD5536_MFGPT4_C2_IRQM  0x00001000
111 #define AMD5536_MFGPT5_C2_IRQM  0x00002000
112 #define AMD5536_MFGPT6_C2_IRQM  0x00004000
113 #define AMD5536_MFGPT7_C2_IRQM  0x00008000
114 #define AMD5536_MFGPT_NR        0x51400029
115 #define AMD5536_MFGPT0_C1_NMIM  0x00000001
116 #define AMD5536_MFGPT1_C1_NMIM  0x00000002
117 #define AMD5536_MFGPT2_C1_NMIM  0x00000004
118 #define AMD5536_MFGPT3_C1_NMIM  0x00000008
119 #define AMD5536_MFGPT4_C1_NMIM  0x00000010
120 #define AMD5536_MFGPT5_C1_NMIM  0x00000020
121 #define AMD5536_MFGPT6_C1_NMIM  0x00000040
122 #define AMD5536_MFGPT7_C1_NMIM  0x00000080
123 #define AMD5536_MFGPT0_C2_NMIM  0x00000100
124 #define AMD5536_MFGPT1_C2_NMIM  0x00000200
125 #define AMD5536_MFGPT2_C2_NMIM  0x00000400
126 #define AMD5536_MFGPT3_C2_NMIM  0x00000800
127 #define AMD5536_MFGPT4_C2_NMIM  0x00001000
128 #define AMD5536_MFGPT5_C2_NMIM  0x00002000
129 #define AMD5536_MFGPT6_C2_NMIM  0x00004000
130 #define AMD5536_MFGPT7_C2_NMIM  0x00008000
131 #define AMD5536_NMI_LEG         0x00010000
132 #define AMD5536_MFGPT0_C2_RSTEN 0x01000000
133 #define AMD5536_MFGPT1_C2_RSTEN 0x02000000
134 #define AMD5536_MFGPT2_C2_RSTEN 0x04000000
135 #define AMD5536_MFGPT3_C2_RSTEN 0x08000000
136 #define AMD5536_MFGPT4_C2_RSTEN 0x10000000
137 #define AMD5536_MFGPT5_C2_RSTEN 0x20000000
138 #define AMD5536_MFGPT_SETUP     0x5140002b
139
140 /* GPIO */
141 #define MSR_LBAR_GPIO           0x5140000c
142 #define AMD5536_GPIO_NPINS      32
143 #define AMD5536_GPIOH_OFFSET    0x80    /* high bank register offset */
144 #define AMD5536_GPIO_OUT_VAL    0x00    /* output value */
145 #define AMD5536_GPIO_OUT_EN     0x04    /* output enable */
146 #define AMD5536_GPIO_OD_EN      0x08    /* open-drain enable */
147 #define AMD5536_GPIO_OUT_INVRT_EN 0x0c  /* invert output */
148 #define AMD5536_GPIO_PU_EN      0x18    /* pull-up enable */
149 #define AMD5536_GPIO_PD_EN      0x1c    /* pull-down enable */
150 #define AMD5536_GPIO_IN_EN      0x20    /* input enable */
151 #define AMD5536_GPIO_IN_INVRT_EN 0x24   /* invert input */
152 #define AMD5536_GPIO_READ_BACK  0x30    /* read back value */
153
154
155 struct cs5536_softc {
156         bus_space_tag_t         sc_iot;
157         bus_space_handle_t      sc_ioh;
158
159 #if NGPIO > 0
160         /* GPIO interface */
161         bus_space_tag_t         sc_gpio_iot;
162         bus_space_handle_t      sc_gpio_ioh;
163         struct gpio             sc_gpio_gc;
164         gpio_pin_t              sc_gpio_pins[AMD5536_GPIO_NPINS];
165 #endif
166 };
167
168 static struct cs5536_softc cs5536_sc;
169
170 #if NGPIO > 0
171 int cs5536_gpio_pin_read(void *arg, int pin);
172 void cs5536_gpio_pin_write(void *arg, int pin, int value);
173 void cs5536_gpio_pin_ctl(void *arg, int pin, int flags);
174 #endif
175 static sysclock_t cs5536_get_timecount(void);
176
177 static struct bios_oem bios_soekris_55 = {
178     { 0xf0000, 0xf1000 },
179     {
180         { "Soekris", 0, 8 },            /* Soekris Engineering. */
181         { "net5", 0, 8 },               /* net5xxx */
182         { "comBIOS", 0, 54 },           /* comBIOS ver. 1.26a  20040819 ... */
183         { NULL, 0, 0 },
184     }
185 };
186
187 static struct bios_oem bios_pcengines_55 = {
188     { 0xf9000, 0xfa000 },
189     {
190         { "PC Engines ALIX", 0, 28 },   /* PC Engines ALIX */
191         { "tinyBIOS", 0, 28 },          /* tinyBIOS V1.4a (C)1997-2005 */
192         { NULL, 0, 0 },
193     }
194 };
195
196 #if NGPIO > 0
197 int
198 cs5536_gpio_pin_read(void *arg, int pin)
199 {
200         uint32_t data;
201         uint16_t port;
202         int     reg, off = 0;
203
204         port = rdmsr(MSR_LBAR_GPIO);
205
206         reg = AMD5536_GPIO_IN_EN;
207         if (pin > 15) {
208                 pin &= 0x0f;
209                 off = AMD5536_GPIOH_OFFSET;
210         }
211         reg += off;
212         data = bus_space_read_4(cs5536_sc.sc_gpio_iot, cs5536_sc.sc_gpio_ioh, reg);
213
214         if (data & (1 << pin))
215                 reg = AMD5536_GPIO_READ_BACK + off;
216         else
217                 reg = AMD5536_GPIO_OUT_VAL + off;
218
219         data = bus_space_read_4(cs5536_sc.sc_gpio_iot, cs5536_sc.sc_gpio_ioh, reg);
220
221         return data & 1 << pin ? GPIO_PIN_HIGH : GPIO_PIN_LOW;
222 }
223
224 void
225 cs5536_gpio_pin_write(void *arg, int pin, int value)
226 {
227         uint32_t data;
228         uint16_t port;
229         int     reg;
230
231         port = rdmsr(MSR_LBAR_GPIO);
232
233         reg = AMD5536_GPIO_OUT_VAL;
234         if (pin > 15) {
235                 pin &= 0x0f;
236                 reg += AMD5536_GPIOH_OFFSET;
237         }
238
239         if (value == 1)
240                 data = 1 << pin;
241         else
242                 data = 1 << (pin + 16);
243
244         bus_space_write_4(cs5536_sc.sc_gpio_iot, cs5536_sc.sc_gpio_ioh, reg, data);
245         //write_data_4(port, reg, data);
246         //outl(port + reg, data);
247 }
248
249 void
250 cs5536_gpio_pin_ctl(void *arg, int pin, int flags)
251 {
252         int n, reg[7], val[7], nreg = 0, off = 0;
253
254         if (pin > 15) {
255                 pin &= 0x0f;
256                 off = AMD5536_GPIOH_OFFSET;
257         }
258
259         reg[nreg] = AMD5536_GPIO_IN_EN + off;
260         if (flags & GPIO_PIN_INPUT)
261                 val[nreg++] = 1 << pin;
262         else
263                 val[nreg++] = 1 << (pin + 16);
264
265         reg[nreg] = AMD5536_GPIO_OUT_EN + off;
266         if (flags & GPIO_PIN_OUTPUT)
267                 val[nreg++] = 1 << pin;
268         else
269                 val[nreg++] = 1 << (pin + 16);
270
271         reg[nreg] = AMD5536_GPIO_OD_EN + off;
272         if (flags & GPIO_PIN_OPENDRAIN)
273                 val[nreg++] = 1 << pin;
274         else
275                 val[nreg++] = 1 << (pin + 16);
276
277         reg[nreg] = AMD5536_GPIO_PU_EN + off;
278         if (flags & GPIO_PIN_PULLUP)
279                 val[nreg++] = 1 << pin;
280         else
281                 val[nreg++] = 1 << (pin + 16);
282
283         reg[nreg] = AMD5536_GPIO_PD_EN + off;
284         if (flags & GPIO_PIN_PULLDOWN)
285                 val[nreg++] = 1 << pin;
286         else
287                 val[nreg++] = 1 << (pin + 16);
288
289         reg[nreg] = AMD5536_GPIO_IN_INVRT_EN + off;
290         if (flags & GPIO_PIN_INVIN)
291                 val[nreg++] = 1 << pin;
292         else
293                 val[nreg++] = 1 << (pin + 16);
294
295         reg[nreg] = AMD5536_GPIO_OUT_INVRT_EN + off;
296         if (flags & GPIO_PIN_INVOUT)
297                 val[nreg++] = 1 << pin;
298         else
299                 val[nreg++] = 1 << (pin + 16);
300
301         /* set flags */
302         for (n = 0; n < nreg; n++)
303                 bus_space_write_4(cs5536_sc.sc_gpio_iot, cs5536_sc.sc_gpio_ioh, reg[n],
304                     val[n]);
305 }
306 #endif /* NGPIO */
307
308 static void
309 cs5536_cputimer_construct(struct cputimer *timer, sysclock_t oldclock)
310 {
311         timer->base = 0;
312         timer->base = oldclock - cs5536_get_timecount();
313 }
314
315 static struct cputimer cs5536_timer = {
316         SLIST_ENTRY_INITIALIZER,
317         "CS5536",
318         CPUTIMER_PRI_CS5536,
319         CPUTIMER_GEODE,
320         cs5536_get_timecount,
321         cputimer_default_fromhz,
322         cputimer_default_fromus,
323         cs5536_cputimer_construct,
324         cputimer_default_destruct,
325         AMD5536_TIMER_FREQ,
326         0, 0, 0
327 };
328
329 static sysclock_t
330 cs5536_get_timecount(void)
331 {
332         return (cs5536_timer.base + rdmsr(AMD5536_TMC));
333 }
334
335 static int
336 cs5536_watchdog(void *unused, int period)
337 {
338         if (period > 0xffff)
339                 period = 0xffff;
340
341         bus_space_write_2(cs5536_sc.sc_iot, cs5536_sc.sc_ioh, AMD5536_MFGPT0_SETUP,
342                     AMD5536_MFGPT_CNT_EN | AMD5536_MFGPT_CMP2);
343         /* reset counter */
344         bus_space_write_2(cs5536_sc.sc_iot, cs5536_sc.sc_ioh, AMD5536_MFGPT0_CNT, 0);
345         /* set comparator 2 */
346         bus_space_write_2(cs5536_sc.sc_iot, cs5536_sc.sc_ioh, AMD5536_MFGPT0_CMP2, period);
347
348         if (period) {
349                 wrmsr(AMD5536_MFGPT_NR,
350                     rdmsr(AMD5536_MFGPT_NR) | AMD5536_MFGPT0_C2_RSTEN);
351         } else {
352                 wrmsr(AMD5536_MFGPT_NR,
353                     rdmsr(AMD5536_MFGPT_NR) & ~AMD5536_MFGPT0_C2_RSTEN);
354         }
355
356         return period;
357 }
358
359 static struct watchdog  cs5536_wdog = {
360         .name           =       "AMD CS5536",
361         .wdog_fn        =       cs5536_watchdog,
362         .arg            =       NULL,
363         .period_max     =       0xffff,
364 };
365
366 static int
367 cs5536_probe(device_t self)
368 {
369         static int probed = 0;
370
371         if (probed)
372                 return ENXIO;
373
374         if (pci_get_vendor(self) == PCI_VENDOR_AMD &&
375                 (pci_get_device(self) == PCI_PRODUCT_AMD_CS5536_PCIB ||
376                 /* XXX: OpenBSD doesn't attach to this one, but free does, though no counter */
377                 pci_get_device(self) == PCI_PRODUCT_AMD_GEODE_LX_PCHB)) {
378                 /* device_set_desc(self, ...) */
379                 probed = 1;
380                 return 0;
381         }
382
383         return ENXIO;
384 }
385
386 static int
387 cs5536_attach(device_t self)
388 {
389 #define BIOS_OEM_MAXLEN 80
390         static u_char bios_oem[BIOS_OEM_MAXLEN] = "\0";
391         static int attached = 0;
392 #if NGPIO > 0
393         int i;
394 #endif
395 #if 0 /* Watchdog stuff */
396         EVENTHANDLER_REGISTER(watchdog_list, cs5536_watchdog, NULL, 0);
397 #endif
398         if (attached)
399                 return ENODEV;
400
401         attached = 1;
402         kprintf("AMD CS5536: rev %d, 32-bit %uHz timer\n",
403                 (int)rdmsr(AMD5536_REV) & AMD5536_REV_MASK,
404                 cs5536_timer.freq);
405
406         /* enable timer */
407         cputimer_register(&cs5536_timer);
408         cputimer_select(&cs5536_timer, 0);
409
410         /* bus_space_map(sc->sc_iot, wa & 0xffff, 64, 0, &sc->sc_ioh)) */
411         cs5536_sc.sc_iot = I386_BUS_SPACE_IO;
412         cs5536_sc.sc_ioh = rdmsr(MSR_LBAR_MFGPT);
413
414         /* enable watchdog and configure */
415         bus_space_write_2(cs5536_sc.sc_iot, cs5536_sc.sc_ioh, AMD5536_MFGPT0_SETUP,
416                 AMD5536_MFGPT_CNT_EN | AMD5536_MFGPT_CMP2EV |
417                 AMD5536_MFGPT_CMP2 | AMD5536_MFGPT_DIV_MASK);
418         wdog_register(&cs5536_wdog);
419
420 #if NGPIO > 0
421         /* bus_space_map(sc->sc_gpio_iot, ga & 0xffff, 0xff, 0,... */
422         cs5536_sc.sc_gpio_iot = I386_BUS_SPACE_IO;
423         cs5536_sc.sc_gpio_ioh = rdmsr(MSR_LBAR_GPIO);
424         for (i = 0; i < AMD5536_GPIO_NPINS; i++) {
425                 cs5536_sc.sc_gpio_pins[i].pin_num = i;
426                 cs5536_sc.sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
427                     GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN |
428                         GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN |
429                         GPIO_PIN_INVIN | GPIO_PIN_INVOUT;
430
431                 /* read initial state */
432                 cs5536_sc.sc_gpio_pins[i].pin_state =
433                     cs5536_gpio_pin_read(&cs5536_sc, i);
434         }
435         cs5536_sc.sc_gpio_gc.driver_name = "cs5536";
436         cs5536_sc.sc_gpio_gc.arg = &cs5536_sc;
437         cs5536_sc.sc_gpio_gc.pin_read = cs5536_gpio_pin_read;
438         cs5536_sc.sc_gpio_gc.pin_write = cs5536_gpio_pin_write;
439         cs5536_sc.sc_gpio_gc.pin_ctl = cs5536_gpio_pin_ctl;
440         cs5536_sc.sc_gpio_gc.pins = cs5536_sc.sc_gpio_pins;
441         cs5536_sc.sc_gpio_gc.npins = AMD5536_GPIO_NPINS;
442         gpio_register(&cs5536_sc.sc_gpio_gc);
443 #endif
444         if (bios_oem_strings(&bios_soekris_55,
445             bios_oem, sizeof bios_oem) > 0 ) {
446 #if NGPIO > 0
447                 /* Attach led to pin 6 */
448                 gpio_consumer_attach("led", "error", &cs5536_sc.sc_gpio_gc,
449                 6, 1);
450 #endif
451         } else if (bios_oem_strings(&bios_pcengines_55,
452             bios_oem, sizeof bios_oem) > 0 ) {
453 #if NGPIO > 0
454                 /* Attach leds */
455                 gpio_consumer_attach("led", "led1", &cs5536_sc.sc_gpio_gc,
456                 6, 1);
457                 gpio_consumer_attach("led", "led2", &cs5536_sc.sc_gpio_gc,
458                 25, 1);
459                 gpio_consumer_attach("led", "led3", &cs5536_sc.sc_gpio_gc,
460                 27, 1);
461 #endif
462 #if 0
463                 /*
464                 * Turn on first LED so we don't make
465                 * people think their box just died.
466                 */
467                 cs5536_led_func(&led1b, 1);
468 #endif
469         }
470         if (*bios_oem)
471                 kprintf("Geode LX: %s\n", bios_oem);
472         else
473                 kprintf("Geode LX: Unknown OEM bios\n");
474
475         if (bootverbose)
476                 kprintf("MFGPT bar: %jx\n", rdmsr(MSR_LBAR_MFGPT));
477
478         return 0;
479 }
480
481 static device_method_t cs5536_methods[] = {
482         /* Device interface */
483         DEVMETHOD(device_probe,         cs5536_probe),
484         DEVMETHOD(device_attach,        cs5536_attach),
485         DEVMETHOD(device_suspend,       bus_generic_suspend),
486         DEVMETHOD(device_resume,        bus_generic_resume),
487         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
488         {0, 0}
489 };
490
491 static driver_t cs5536_driver = {
492         "cs5536",
493         cs5536_methods,
494         0,
495 };
496
497 static devclass_t cs5536_devclass;
498
499 DRIVER_MODULE(cs5536, pci, cs5536_driver, cs5536_devclass, NULL, NULL);