]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/i386/geode.c
ena: Upgrade ena-com to freebsd v2.7.0
[FreeBSD/FreeBSD.git] / sys / i386 / i386 / geode.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003-2004 Poul-Henning Kamp
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/timetc.h>
32 #include <sys/bus.h>
33 #include <sys/eventhandler.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/watchdog.h>
37 #include <dev/pci/pcireg.h>
38 #include <dev/pci/pcivar.h>
39 #include <dev/led/led.h>
40 #include <machine/pc/bios.h>
41
42 static struct bios_oem bios_soekris = {
43     { 0xf0000, 0xf1000 },
44     {
45         { "Soekris", 0, 8 },            /* Soekris Engineering. */
46         { "net4", 0, 8 },               /* net45xx */
47         { "comBIOS", 0, 54 },           /* comBIOS ver. 1.26a  20040819 ... */
48         { NULL, 0, 0 },
49     }
50 };
51
52 static struct bios_oem bios_soekris_55 = {
53     { 0xf0000, 0xf1000 },
54     {
55         { "Soekris", 0, 8 },            /* Soekris Engineering. */
56         { "net5", 0, 8 },               /* net5xxx */
57         { "comBIOS", 0, 54 },           /* comBIOS ver. 1.26a  20040819 ... */
58         { NULL, 0, 0 },
59     }
60 };
61
62 static struct bios_oem bios_pcengines = {
63     { 0xf9000, 0xfa000 },
64     {
65         { "PC Engines WRAP", 0, 28 },   /* PC Engines WRAP.1C v1.03 */
66         { "tinyBIOS", 0, 28 },          /* tinyBIOS V1.4a (C)1997-2003 */
67         { NULL, 0, 0 },
68     }
69 };
70
71 static struct bios_oem bios_pcengines_55 = {
72     { 0xf9000, 0xfa000 },
73     {
74         { "PC Engines ALIX", 0, 28 },   /* PC Engines ALIX */
75         { "tinyBIOS", 0, 28 },          /* tinyBIOS V1.4a (C)1997-2005 */
76         { NULL, 0, 0 },
77     }
78 };
79
80 static struct bios_oem bios_advantech = {
81     { 0xfe000, 0xff000 },
82     {
83         { "**** PCM-582", 5, 33 },      /* PCM-5823 BIOS V1.12 ... */
84         { "GXm-Cx5530", -11, 35 },      /* 06/07/2002-GXm-Cx5530... */
85         { NULL, 0, 0 },
86     }
87 };
88
89 static unsigned cba;
90 static unsigned gpio;
91 static unsigned geode_counter;
92
93 static struct cdev *led1, *led2, *led3;
94 static int      led1b, led2b, led3b;
95
96 static void
97 led_func(void *ptr, int onoff)
98 {
99         uint32_t u;
100         int bit;
101
102         bit = *(int *)ptr;
103         if (bit < 0) {
104                 bit = -bit;
105                 onoff = !onoff;
106         }
107
108         u = inl(gpio + 4);
109         if (onoff)
110                 u |= 1 << bit;
111         else
112                 u &= ~(1 << bit);
113         outl(gpio, u);
114 }
115
116 static void
117 cs5536_led_func(void *ptr, int onoff)
118 {
119         int bit;
120         uint16_t a;
121
122         bit = *(int *)ptr;
123         if (bit < 0) {
124                 bit = -bit;
125                 onoff = !onoff;
126         }
127
128         a = rdmsr(0x5140000c);
129         if (bit >= 16) {
130                 a += 0x80;
131                 bit -= 16;
132         }
133
134         if (onoff)
135                 outl(a, 1 << bit);
136         else
137                 outl(a, 1 << (bit + 16));
138 }
139
140 static unsigned
141 geode_get_timecount(struct timecounter *tc)
142 {
143         return (inl(geode_counter));
144 }
145
146 static struct timecounter geode_timecounter = {
147         geode_get_timecount,
148         NULL,
149         0xffffffff,
150         27000000,
151         "Geode",
152         1000
153 };
154
155 static uint64_t
156 geode_cputicks(void)
157 {
158         unsigned c;
159         static unsigned last;
160         static uint64_t offset;
161
162         c = inl(geode_counter);
163         if (c < last)
164                 offset += (1LL << 32);
165         last = c;
166         return (offset | c);
167 }
168
169 /*
170  * The GEODE watchdog runs from a 32kHz frequency.  One period of that is
171  * 31250 nanoseconds which we round down to 2^14 nanoseconds.  The watchdog
172  * consists of a power-of-two prescaler and a 16 bit counter, so the math
173  * is quite simple.  The max timeout is 14 + 16 + 13 = 2^43 nsec ~= 2h26m.
174  */
175 static void
176 geode_watchdog(void *foo __unused, u_int cmd, int *error)
177 {
178         u_int u, p, r;
179
180         u = cmd & WD_INTERVAL;
181         if (u >= 14 && u <= 43) {
182                 u -= 14;
183                 if (u > 16) {
184                         p = u - 16;
185                         u -= p;
186                 } else {
187                         p = 0;
188                 }
189                 if (u == 16)
190                         u = (1 << u) - 1;
191                 else
192                         u = 1 << u;
193                 r = inw(cba + 2) & 0xff00;
194                 outw(cba + 2, p | 0xf0 | r);
195                 outw(cba, u);
196                 *error = 0;
197         } else {
198                 outw(cba, 0);
199         }
200 }
201
202 /*
203  * We run MFGPT0 off the 32kHz frequency and prescale by 16384 giving a
204  * period of half a second.
205  * Range becomes 2^30 (= 1 sec) to 2^44 (almost 5 hours)
206  */
207 static void
208 cs5536_watchdog(void *foo __unused, u_int cmd, int *error)
209 {
210         u_int u, p, s;
211         uint16_t a;
212         uint32_t m;
213
214         a = rdmsr(0x5140000d);
215
216         u = cmd & WD_INTERVAL;
217         if (u >= 30 && u <= 44) {
218                 p = 1 << (u - 29);
219
220                 /* Set up MFGPT0, 32khz, prescaler 16k, C2 event */
221                 outw(a + 6, 0x030e);
222                 /* set comparator 2 */
223                 outw(a + 2, p);
224                 /* reset counter */
225                 outw(a + 4, 0);
226                 /* Arm reset mechanism */
227                 m = rdmsr(0x51400029);
228                 m |= (1 << 24);
229                 wrmsr(0x51400029, m);
230                 /* Start counter */
231                 outw(a + 6, 0x8000);
232
233                 *error = 0;
234         } else {
235                 /* 
236                  * MFGPT_SETUP is write-once
237                  * Check if the counter has been setup
238                  */
239                 s = inw(a + 6);
240                 if (s & (1 << 12)) {
241                         /* Stop and reset counter */
242                         outw(a + 6, 0);
243                         outw(a + 4, 0);
244                 }
245         }
246 }
247
248 /*
249  * The Advantech PCM-582x watchdog expects 0x1 at I/O port 0x0443
250  * every 1.6 secs +/- 30%. Writing 0x0 disables the watchdog
251  * NB: reading the I/O port enables the timer as well
252  */
253 static void
254 advantech_watchdog(void *foo __unused, u_int cmd, int *error)
255 {
256         u_int u;
257
258         u = cmd & WD_INTERVAL;
259         if (u > 0 && u <= WD_TO_1SEC) {
260                 outb(0x0443, 1);
261                 *error = 0;
262         } else {
263                 outb(0x0443, 0);
264         }
265 }
266
267 static int
268 geode_probe(device_t self)
269 {
270 #define BIOS_OEM_MAXLEN 80
271         static u_char bios_oem[BIOS_OEM_MAXLEN] = "\0";
272
273         switch (pci_get_devid(self)) {
274         case 0x0515100b:
275                 if (geode_counter == 0) {
276                         /*
277                          * The address of the CBA is written to this register
278                          * by the bios, see p161 in data sheet.
279                          */
280                         cba = pci_read_config(self, 0x64, 4);
281                         if (bootverbose)
282                                 printf("Geode CBA@ 0x%x\n", cba);
283                         geode_counter = cba + 0x08;
284                         outl(cba + 0x0d, 2);
285                         if (bootverbose)
286                                 printf("Geode rev: %02x %02x\n",
287                                         inb(cba + 0x3c), inb(cba + 0x3d));
288                         tc_init(&geode_timecounter);
289                         EVENTHANDLER_REGISTER(watchdog_list, geode_watchdog,
290                             NULL, 0);
291                         set_cputicker(geode_cputicks, 27000000, false);
292                 }
293                 break;
294         case 0x0510100b:
295                 gpio = pci_read_config(self, PCIR_BAR(0), 4);
296                 gpio &= ~0x1f;
297                 if (bootverbose)
298                         printf("Geode GPIO@ = %x\n", gpio);
299                 if (bios_oem_strings(&bios_soekris,
300                     bios_oem, sizeof bios_oem) > 0 ) {
301                         led1b = 20;
302                         led1 = led_create(led_func, &led1b, "error");
303                 } else if (bios_oem_strings(&bios_pcengines,
304                     bios_oem, sizeof bios_oem) > 0 ) {
305                         led1b = -2;
306                         led2b = -3;
307                         led3b = -18;
308                         led1 = led_create(led_func, &led1b, "led1");
309                         led2 = led_create(led_func, &led2b, "led2");
310                         led3 = led_create(led_func, &led3b, "led3");
311                         /*
312                         * Turn on first LED so we don't make
313                         * people think their box just died.
314                         */
315                         led_func(&led1b, 1);
316                 }
317                 if (*bios_oem)
318                         printf("Geode %s\n", bios_oem);
319                 break;
320         case 0x01011078:
321                 if (bios_oem_strings(&bios_advantech,
322                     bios_oem, sizeof bios_oem) > 0 ) {
323                         printf("Geode %s\n", bios_oem);
324                         EVENTHANDLER_REGISTER(watchdog_list, advantech_watchdog,
325                             NULL, 0);
326                 }
327                 break;
328         case 0x20801022:
329                 if (bios_oem_strings(&bios_soekris_55,
330                     bios_oem, sizeof bios_oem) > 0 ) {
331                         led1b = 6;
332                         led1 = led_create(cs5536_led_func, &led1b, "error");
333                 } else if (bios_oem_strings(&bios_pcengines_55,
334                     bios_oem, sizeof bios_oem) > 0 ) {
335                         led1b = -6;
336                         led2b = -25;
337                         led3b = -27;
338                         led1 = led_create(cs5536_led_func, &led1b, "led1");
339                         led2 = led_create(cs5536_led_func, &led2b, "led2");
340                         led3 = led_create(cs5536_led_func, &led3b, "led3");
341                         /*
342                         * Turn on first LED so we don't make
343                         * people think their box just died.
344                         */
345                         cs5536_led_func(&led1b, 1);
346                 }
347                 if (*bios_oem)
348                         printf("Geode LX: %s\n", bios_oem);
349                 if (bootverbose)
350                         printf("MFGPT bar: %jx\n", rdmsr(0x5140000d));
351                 EVENTHANDLER_REGISTER(watchdog_list, cs5536_watchdog, NULL, 0);
352                 break;
353         }
354         return (ENXIO);
355 }
356
357 static int
358 geode_attach(device_t self)
359 {
360
361         return(ENODEV);
362 }
363
364 static device_method_t geode_methods[] = {
365         /* Device interface */
366         DEVMETHOD(device_probe,         geode_probe),
367         DEVMETHOD(device_attach,        geode_attach),
368         DEVMETHOD(device_suspend,       bus_generic_suspend),
369         DEVMETHOD(device_resume,        bus_generic_resume),
370         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
371         {0, 0}
372 };
373
374 static driver_t geode_driver = {
375         "geode",
376         geode_methods,
377         0,
378 };
379
380 DRIVER_MODULE(geode, pci, geode_driver, 0, 0);