Initial import of binutils 2.22 on the new vendor branch
[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 "opt_cpu.h"
54 #include "use_gpio.h"
55
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/systimer.h>
59 #include <sys/bus.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>
68 #include <sys/wdog.h>
69
70 #define AMD5536_TIMER_FREQ      3579545
71
72 #define AMD5536_REV             0x51400017
73 #define AMD5536_REV_MASK        0xff
74 #define AMD5536_TMC             0x51400050
75
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
140
141 /* GPIO */
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 */
154
155
156 struct cs5536_softc {
157         bus_space_tag_t         sc_iot;
158         bus_space_handle_t      sc_ioh;
159
160 #if NGPIO > 0
161         /* GPIO interface */
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];
166 #endif
167 };
168
169 static struct cs5536_softc cs5536_sc;
170
171 #if NGPIO > 0
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);
175 #endif
176 static sysclock_t cs5536_get_timecount(void);
177
178 static struct bios_oem bios_soekris_55 = {
179     { 0xf0000, 0xf1000 },
180     {
181         { "Soekris", 0, 8 },            /* Soekris Engineering. */
182         { "net5", 0, 8 },               /* net5xxx */
183         { "comBIOS", 0, 54 },           /* comBIOS ver. 1.26a  20040819 ... */
184         { NULL, 0, 0 },
185     }
186 };
187
188 static struct bios_oem bios_pcengines_55 = {
189     { 0xf9000, 0xfa000 },
190     {
191         { "PC Engines ALIX", 0, 28 },   /* PC Engines ALIX */
192         { "tinyBIOS", 0, 28 },          /* tinyBIOS V1.4a (C)1997-2005 */
193         { NULL, 0, 0 },
194     }
195 };
196
197 #if NGPIO > 0
198 int
199 cs5536_gpio_pin_read(void *arg, int pin)
200 {
201         uint32_t data;
202         uint16_t port;
203         int     reg, off = 0;
204
205         port = rdmsr(MSR_LBAR_GPIO);
206
207         reg = AMD5536_GPIO_IN_EN;
208         if (pin > 15) {
209                 pin &= 0x0f;
210                 off = AMD5536_GPIOH_OFFSET;
211         }
212         reg += off;
213         data = bus_space_read_4(cs5536_sc.sc_gpio_iot, cs5536_sc.sc_gpio_ioh, reg);
214
215         if (data & (1 << pin))
216                 reg = AMD5536_GPIO_READ_BACK + off;
217         else
218                 reg = AMD5536_GPIO_OUT_VAL + off;
219
220         data = bus_space_read_4(cs5536_sc.sc_gpio_iot, cs5536_sc.sc_gpio_ioh, reg);
221
222         return data & 1 << pin ? GPIO_PIN_HIGH : GPIO_PIN_LOW;
223 }
224
225 void
226 cs5536_gpio_pin_write(void *arg, int pin, int value)
227 {
228         uint32_t data;
229         uint16_t port;
230         int     reg;
231
232         port = rdmsr(MSR_LBAR_GPIO);
233
234         reg = AMD5536_GPIO_OUT_VAL;
235         if (pin > 15) {
236                 pin &= 0x0f;
237                 reg += AMD5536_GPIOH_OFFSET;
238         }
239
240         if (value == 1)
241                 data = 1 << pin;
242         else
243                 data = 1 << (pin + 16);
244
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);
248 }
249
250 void
251 cs5536_gpio_pin_ctl(void *arg, int pin, int flags)
252 {
253         int n, reg[7], val[7], nreg = 0, off = 0;
254
255         if (pin > 15) {
256                 pin &= 0x0f;
257                 off = AMD5536_GPIOH_OFFSET;
258         }
259
260         reg[nreg] = AMD5536_GPIO_IN_EN + off;
261         if (flags & GPIO_PIN_INPUT)
262                 val[nreg++] = 1 << pin;
263         else
264                 val[nreg++] = 1 << (pin + 16);
265
266         reg[nreg] = AMD5536_GPIO_OUT_EN + off;
267         if (flags & GPIO_PIN_OUTPUT)
268                 val[nreg++] = 1 << pin;
269         else
270                 val[nreg++] = 1 << (pin + 16);
271
272         reg[nreg] = AMD5536_GPIO_OD_EN + off;
273         if (flags & GPIO_PIN_OPENDRAIN)
274                 val[nreg++] = 1 << pin;
275         else
276                 val[nreg++] = 1 << (pin + 16);
277
278         reg[nreg] = AMD5536_GPIO_PU_EN + off;
279         if (flags & GPIO_PIN_PULLUP)
280                 val[nreg++] = 1 << pin;
281         else
282                 val[nreg++] = 1 << (pin + 16);
283
284         reg[nreg] = AMD5536_GPIO_PD_EN + off;
285         if (flags & GPIO_PIN_PULLDOWN)
286                 val[nreg++] = 1 << pin;
287         else
288                 val[nreg++] = 1 << (pin + 16);
289
290         reg[nreg] = AMD5536_GPIO_IN_INVRT_EN + off;
291         if (flags & GPIO_PIN_INVIN)
292                 val[nreg++] = 1 << pin;
293         else
294                 val[nreg++] = 1 << (pin + 16);
295
296         reg[nreg] = AMD5536_GPIO_OUT_INVRT_EN + off;
297         if (flags & GPIO_PIN_INVOUT)
298                 val[nreg++] = 1 << pin;
299         else
300                 val[nreg++] = 1 << (pin + 16);
301
302         /* set flags */
303         for (n = 0; n < nreg; n++)
304                 bus_space_write_4(cs5536_sc.sc_gpio_iot, cs5536_sc.sc_gpio_ioh, reg[n],
305                     val[n]);
306 }
307 #endif /* NGPIO */
308
309 static void
310 cs5536_cputimer_construct(struct cputimer *timer, sysclock_t oldclock)
311 {
312         timer->base = 0;
313         timer->base = oldclock - cs5536_get_timecount();
314 }
315
316 static struct cputimer cs5536_timer = {
317         SLIST_ENTRY_INITIALIZER,
318         "CS5536",
319         CPUTIMER_PRI_CS5536,
320         CPUTIMER_GEODE,
321         cs5536_get_timecount,
322         cputimer_default_fromhz,
323         cputimer_default_fromus,
324         cs5536_cputimer_construct,
325         cputimer_default_destruct,
326         AMD5536_TIMER_FREQ,
327         0, 0, 0
328 };
329
330 static sysclock_t
331 cs5536_get_timecount(void)
332 {
333         return (cs5536_timer.base + rdmsr(AMD5536_TMC));
334 }
335
336 #ifdef WATCHDOG_ENABLE
337 static int
338 cs5536_watchdog(void *unused, int period)
339 {
340         if (period > 0xffff)
341                 period = 0xffff;
342
343         bus_space_write_2(cs5536_sc.sc_iot, cs5536_sc.sc_ioh, AMD5536_MFGPT0_SETUP,
344                     AMD5536_MFGPT_CNT_EN | AMD5536_MFGPT_CMP2);
345         /* reset counter */
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);
349
350         if (period) {
351                 wrmsr(AMD5536_MFGPT_NR,
352                     rdmsr(AMD5536_MFGPT_NR) | AMD5536_MFGPT0_C2_RSTEN);
353         } else {
354                 wrmsr(AMD5536_MFGPT_NR,
355                     rdmsr(AMD5536_MFGPT_NR) & ~AMD5536_MFGPT0_C2_RSTEN);
356         }
357
358         return period;
359 }
360
361 static struct watchdog  cs5536_wdog = {
362         .name           =       "AMD CS5536",
363         .wdog_fn        =       cs5536_watchdog,
364         .arg            =       NULL,
365         .period_max     =       0xffff,
366 };
367 #endif /* WATCHDOG_ENABLE */
368
369 static int
370 cs5536_probe(device_t self)
371 {
372         static int probed = 0;
373
374         if (probed)
375                 return ENXIO;
376
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, ...) */
382                 probed = 1;
383                 return 0;
384         }
385
386         return ENXIO;
387 }
388
389 static int
390 cs5536_attach(device_t self)
391 {
392 #define BIOS_OEM_MAXLEN 80
393         static u_char bios_oem[BIOS_OEM_MAXLEN] = "\0";
394         static int attached = 0;
395 #if NGPIO > 0
396         int i;
397 #endif
398 #if 0 /* Watchdog stuff */
399         EVENTHANDLER_REGISTER(watchdog_list, cs5536_watchdog, NULL, 0);
400 #endif
401         if (attached)
402                 return ENODEV;
403
404         attached = 1;
405         kprintf("AMD CS5536: rev %d, 32-bit %uHz timer\n",
406                 (int)rdmsr(AMD5536_REV) & AMD5536_REV_MASK,
407                 cs5536_timer.freq);
408
409         /* enable timer */
410         cputimer_register(&cs5536_timer);
411         cputimer_select(&cs5536_timer, 0);
412
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);
416
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 */
424
425 #if NGPIO > 0
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;
435
436                 /* read initial state */
437                 cs5536_sc.sc_gpio_pins[i].pin_state =
438                     cs5536_gpio_pin_read(&cs5536_sc, i);
439         }
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);
448 #endif
449         if (bios_oem_strings(&bios_soekris_55,
450             bios_oem, sizeof bios_oem) > 0 ) {
451 #if NGPIO > 0
452                 /* Attach led to pin 6 */
453                 gpio_consumer_attach("led", "error", &cs5536_sc.sc_gpio_gc,
454                 6, 1);
455 #endif
456         } else if (bios_oem_strings(&bios_pcengines_55,
457             bios_oem, sizeof bios_oem) > 0 ) {
458 #if NGPIO > 0
459                 /* Attach leds */
460                 gpio_consumer_attach("led", "led1", &cs5536_sc.sc_gpio_gc,
461                 6, 1);
462                 gpio_consumer_attach("led", "led2", &cs5536_sc.sc_gpio_gc,
463                 25, 1);
464                 gpio_consumer_attach("led", "led3", &cs5536_sc.sc_gpio_gc,
465                 27, 1);
466 #endif
467 #if 0
468                 /*
469                 * Turn on first LED so we don't make
470                 * people think their box just died.
471                 */
472                 cs5536_led_func(&led1b, 1);
473 #endif
474         }
475         if (*bios_oem)
476                 kprintf("Geode LX: %s\n", bios_oem);
477         else
478                 kprintf("Geode LX: Unknown OEM bios\n");
479
480         if (bootverbose)
481                 kprintf("MFGPT bar: %jx\n", rdmsr(MSR_LBAR_MFGPT));
482
483         return 0;
484 }
485
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),
493         {0, 0}
494 };
495
496 static driver_t cs5536_driver = {
497         "cs5536",
498         cs5536_methods,
499         0,
500 };
501
502 static devclass_t cs5536_devclass;
503
504 DRIVER_MODULE(cs5536, pci, cs5536_driver, cs5536_devclass, NULL, NULL);