2 * SPDX-License-Identifier: Beerware
4 * ----------------------------------------------------------------------------
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
9 * ----------------------------------------------------------------------------
12 * The AMD Elan sc520 is a system-on-chip gadget which is used in embedded
13 * kind of things, see www.soekris.com for instance, and it has a few quirks
14 * we need to deal with.
15 * Unfortunately we cannot identify the gadget by CPUID output because it
16 * depends on strapping options and only the stepping field may be useful
17 * and those are undocumented from AMDs side.
19 * So instead we recognize the on-chip host-PCI bridge and call back from
20 * sys/i386/pci/pci_bus.c to here if we find it.
23 * The Elan has three general purpose counters, and when two of these
24 * are used just right they can hardware timestamp external events with
25 * approx 125 nsec resolution and +/- 125 nsec precision.
27 * Connect the signal to TMR1IN and a GPIO pin, and configure the GPIO pin
28 * with a 'P' in sysctl machdep.elan_gpio_config.
30 * The rising edge of the signal will start timer 1 counting up from
31 * zero, and when the timecounter polls for PPS, both counter 1 & 2 is
32 * read, as well as the GPIO bit. If a rising edge has happened, the
33 * contents of timer 1 which is how long time ago the edge happened,
34 * is subtracted from timer 2 to give us a "true time stamp".
36 * Echoing the PPS signal on any GPIO pin is supported (set it to 'e'
37 * or 'E' (inverted) in the sysctl) The echo signal should only be
38 * used as a visual indication, not for calibration since it suffers
39 * from 1/hz (or more) jitter which the timestamps are compensated for.
43 #include <sys/cdefs.h>
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
49 #include <sys/eventhandler.h>
50 #include <sys/sysctl.h>
51 #include <sys/syslog.h>
52 #include <sys/timetc.h>
56 #include <sys/mutex.h>
57 #include <sys/malloc.h>
58 #include <sys/sysctl.h>
59 #include <sys/timepps.h>
60 #include <sys/watchdog.h>
62 #include <dev/led/led.h>
63 #include <machine/md_var.h>
64 #include <machine/elan_mmcr.h>
65 #include <machine/pc/bios.h>
70 static char gpio_config[33];
72 static volatile uint16_t *mmcrptr;
73 volatile struct elan_mmcr *elan_mmcr;
76 static struct pps_state elan_pps;
77 static volatile uint16_t *pps_ap[3];
78 static u_int pps_a, pps_d;
79 static u_int echo_a, echo_d;
80 #endif /* CPU_ELAN_PPS */
84 static struct bios_oem bios_soekris = {
87 { "Soekris", 0, 8 }, /* Soekris Engineering. */
88 { "net4", 0, 8 }, /* net45xx */
89 { "comBIOS", 0, 54 }, /* comBIOS ver. 1.26a 20040819 ... */
96 static u_int led_cookie[32];
97 static struct cdev *led_dev[32];
100 gpio_led(void *cookie, int state)
113 sysctl_machdep_elan_gpio_config(SYSCTL_HANDLER_ARGS)
121 error = SYSCTL_OUT(req, gpio_config, 33);
122 if (error != 0 || req->newptr == NULL)
124 if (req->newlen != 32)
126 error = SYSCTL_IN(req, buf, 32);
129 /* Disallow any disabled pins and count pps and echo */
131 for (i = 0; i < 32; i++) {
132 if (gpio_config[i] == '-' && buf[i] == '.')
133 buf[i] = gpio_config[i];
134 if (gpio_config[i] == '-' && buf[i] != '-')
141 if (buf[i] == 'e' || buf[i] == 'E') {
146 if (buf[i] != 'L' && buf[i] != 'l'
148 && buf[i] != 'P' && buf[i] != 'E' && buf[i] != 'e'
149 #endif /* CPU_ELAN_PPS */
150 && buf[i] != '.' && buf[i] != '-')
159 for (i = 0; i < 32; i++) {
170 if (buf[i] != 'l' && buf[i] != 'L' && led_dev[i] != NULL) {
171 led_destroy(led_dev[i]);
173 mmcrptr[(0xc2a + v) / 2] &= ~u;
180 pps_ap[0] = &mmcrptr[pps_a / 2];
181 pps_ap[1] = &elan_mmcr->GPTMR2CNT;
182 pps_ap[2] = &elan_mmcr->GPTMR1CNT;
183 mmcrptr[(0xc2a + v) / 2] &= ~u;
184 gpio_config[i] = buf[i];
193 mmcrptr[(0xc2a + v) / 2] |= u;
194 gpio_config[i] = buf[i];
196 #endif /* CPU_ELAN_PPS */
200 led_cookie[i] = (0xc34 + v) | (u << 16);
202 led_cookie[i] = (0xc38 + v) | (u << 16);
205 sprintf(tmp, "gpio%d", i);
206 mmcrptr[(0xc2a + v) / 2] |= u;
207 gpio_config[i] = buf[i];
209 led_create(gpio_led, &led_cookie[i], tmp);
212 gpio_config[i] = buf[i];
222 SYSCTL_OID(_machdep, OID_AUTO, elan_gpio_config,
223 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
224 sysctl_machdep_elan_gpio_config, "A",
225 "Elan CPU GPIO pin config");
229 elan_poll_pps(struct timecounter *tc)
237 * Grab the HW state as quickly and compactly as we can. Disable
238 * interrupts to avoid measuring our interrupt service time on
239 * hw with quality clock sources.
241 saveintr = intr_disable();
242 x = *pps_ap[0]; /* state, must be first, see below */
243 y = *pps_ap[1]; /* timer2 */
244 z = *pps_ap[2]; /* timer1 */
245 intr_restore(saveintr);
248 * Order is important here. We need to check the state of the GPIO
249 * pin first, in order to avoid reading timer 1 right before the
250 * state change. Technically pps_a may be zero in which case we
251 * harmlessly read the REVID register and the contents of pps_d is
257 /* If state did not change or we don't have a GPIO pin, return */
258 if (i == state || pps_a == 0)
263 /* If the state is "low", flip the echo GPIO and return. */
266 mmcrptr[(echo_a ^ 0xc) / 2] = echo_d;
271 * Subtract timer1 from timer2 to compensate for time from the
272 * edge until we read the counters.
276 pps_capture(&elan_pps);
277 elan_pps.capcount = u;
278 pps_event(&elan_pps, PPS_CAPTUREASSERT);
280 /* Twiddle echo bit */
282 mmcrptr[echo_a / 2] = echo_d;
284 #endif /* CPU_ELAN_PPS */
287 elan_get_timecount(struct timecounter *tc)
290 /* Read timer2, end of story */
291 return (elan_mmcr->GPTMR2CNT);
295 * The Elan CPU can be run from a number of clock frequencies, this
296 * allows you to override the default 33.3 MHZ.
298 #ifndef CPU_ELAN_XTAL
299 #define CPU_ELAN_XTAL 33333333
302 static struct timecounter elan_timecounter = {
312 sysctl_machdep_elan_freq(SYSCTL_HANDLER_ARGS)
317 f = elan_timecounter.tc_frequency * 4;
318 error = sysctl_handle_int(oidp, &f, 0, req);
319 if (error == 0 && req->newptr != NULL)
320 elan_timecounter.tc_frequency = (f + 3) / 4;
324 SYSCTL_PROC(_machdep, OID_AUTO, elan_freq,
325 CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, sizeof (u_int),
326 sysctl_machdep_elan_freq, "IU",
330 * Positively identifying the Elan can only be done through the PCI id of
331 * the host-bridge, this function is called from i386/pci/pci_bus.c.
334 init_AMD_Elan_sc520(void)
339 mmcrptr = pmap_mapdev(0xfffef000, 0x1000);
340 elan_mmcr = (volatile struct elan_mmcr *)mmcrptr;
343 * The i8254 is driven with a nonstandard frequency which is
345 * f = 32768 * 45 * 25 / 31 = 1189161.29...
346 * We use the sysctl to get the i8254 (timecounter etc) into whack.
350 i = kernel_sysctlbyname(&thread0, "machdep.i8254_freq",
351 NULL, 0, &new, sizeof new, NULL, 0);
352 if (bootverbose || 1)
353 printf("sysctl machdep.i8254_freq=%d returns %d\n", new, i);
355 /* Start GP timer #2 and use it as timecounter, hz permitting */
356 elan_mmcr->GPTMR2MAXCMPA = 0;
357 elan_mmcr->GPTMR2CTL = 0xc001;
360 /* Set up GP timer #1 as pps counter */
361 elan_mmcr->CSPFS &= ~0x10;
362 elan_mmcr->GPTMR1CTL = 0x8000 | 0x4000 | 0x10 | 0x1;
363 elan_mmcr->GPTMR1MAXCMPA = 0x0;
364 elan_mmcr->GPTMR1MAXCMPB = 0x0;
365 elan_pps.ppscap |= PPS_CAPTUREASSERT;
368 tc_init(&elan_timecounter);
372 elan_watchdog(void *foo __unused, u_int spec, int *error)
377 u = spec & WD_INTERVAL;
378 if (u > 0 && u <= 35) {
384 * There is a bug in some silicon which prevents us from
385 * writing to the WDTMRCTL register if the GP echo mode is
386 * enabled. GP echo mode on the other hand is desirable
387 * for other reasons. Save and restore the GP echo mode
388 * around our hardware tom-foolery.
390 w = elan_mmcr->GPECHO;
391 elan_mmcr->GPECHO = 0;
393 /* Clear the ENB bit */
394 elan_mmcr->WDTMRCTL = 0x3333;
395 elan_mmcr->WDTMRCTL = 0xcccc;
396 elan_mmcr->WDTMRCTL = 0;
399 elan_mmcr->WDTMRCTL = 0x3333;
400 elan_mmcr->WDTMRCTL = 0xcccc;
401 elan_mmcr->WDTMRCTL = v;
404 /* Just reset timer */
405 elan_mmcr->WDTMRCTL = 0xaaaa;
406 elan_mmcr->WDTMRCTL = 0x5555;
408 elan_mmcr->GPECHO = w;
411 w = elan_mmcr->GPECHO;
412 elan_mmcr->GPECHO = 0;
413 elan_mmcr->WDTMRCTL = 0x3333;
414 elan_mmcr->WDTMRCTL = 0xcccc;
415 elan_mmcr->WDTMRCTL = 0x4080;
416 elan_mmcr->WDTMRCTL = w; /* XXX What does this statement do? */
417 elan_mmcr->GPECHO = w;
423 elan_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
424 int nprot, vm_memattr_t *memattr)
427 if (offset >= 0x1000)
433 elan_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *tdr)
441 error = pps_ioctl(cmd, arg, &elan_pps);
443 * We only want to incur the overhead of the PPS polling if we
444 * are actually asked to timestamp.
446 if (elan_pps.ppsparam.mode & PPS_CAPTUREASSERT) {
447 elan_timecounter.tc_poll_pps = elan_poll_pps;
449 elan_timecounter.tc_poll_pps = NULL;
451 if (error != ENOIOCTL)
458 static struct cdevsw elan_cdevsw = {
459 .d_version = D_VERSION,
460 .d_flags = D_NEEDGIANT,
461 .d_ioctl = elan_ioctl,
471 #define BIOS_OEM_MAXLEN 72
472 static u_char bios_oem[BIOS_OEM_MAXLEN] = "\0";
473 #endif /* CPU_SOEKRIS */
475 /* If no elan found, just return */
479 printf("Elan-mmcr driver: MMCR at %p.%s\n",
488 make_dev(&elan_cdevsw, 0,
489 UID_ROOT, GID_WHEEL, 0600, "elan-mmcr");
492 if ( bios_oem_strings(&bios_soekris, bios_oem, BIOS_OEM_MAXLEN) > 0 )
493 printf("Elan-mmcr %s\n", bios_oem);
495 /* Create the error LED on GPIO9 */
496 led_cookie[9] = 0x02000c34;
497 led_dev[9] = led_create(gpio_led, &led_cookie[9], "error");
499 /* Disable the unavailable GPIO pins */
500 strcpy(gpio_config, "-----....--..--------..---------");
501 #else /* !CPU_SOEKRIS */
502 /* We don't know which pins are available so enable them all */
503 strcpy(gpio_config, "................................");
504 #endif /* CPU_SOEKRIS */
506 EVENTHANDLER_REGISTER(watchdog_list, elan_watchdog, NULL, 0);
509 SYSINIT(elan, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, elan_drvinit, NULL);