Home | History | Annotate | Line # | Download | only in ic
acpipmtimer.c revision 1.7
      1  1.7    cegger /* $NetBSD: acpipmtimer.c,v 1.7 2009/05/12 14:25:17 cegger Exp $ */
      2  1.3   xtraeme 
      3  1.3   xtraeme #include <sys/cdefs.h>
      4  1.7    cegger __KERNEL_RCSID(0, "$NetBSD: acpipmtimer.c,v 1.7 2009/05/12 14:25:17 cegger Exp $");
      5  1.1  drochner 
      6  1.1  drochner #include <sys/types.h>
      7  1.1  drochner 
      8  1.1  drochner #include <sys/systm.h>
      9  1.1  drochner #include <sys/device.h>
     10  1.1  drochner #include <sys/malloc.h>
     11  1.4        ad #include <sys/bus.h>
     12  1.1  drochner #include <sys/time.h>
     13  1.1  drochner #include <sys/timetc.h>
     14  1.1  drochner 
     15  1.1  drochner #include <dev/ic/acpipmtimer.h>
     16  1.1  drochner 
     17  1.1  drochner #define ACPI_PM_TIMER_FREQUENCY 3579545
     18  1.1  drochner 
     19  1.1  drochner struct hwtc {
     20  1.1  drochner 	struct timecounter tc;
     21  1.1  drochner 	bus_space_tag_t t;
     22  1.1  drochner 	bus_space_handle_t h;
     23  1.1  drochner 	bus_size_t off;
     24  1.1  drochner };
     25  1.1  drochner 
     26  1.1  drochner static u_int acpihwtimer_read_safe(struct timecounter *);
     27  1.1  drochner static u_int acpihwtimer_read_fast(struct timecounter *);
     28  1.1  drochner 
     29  1.1  drochner int
     30  1.7    cegger acpipmtimer_attach(device_t dev,
     31  1.1  drochner 		   bus_space_tag_t t, bus_space_handle_t h, bus_size_t off,
     32  1.1  drochner 		   int flags)
     33  1.1  drochner {
     34  1.1  drochner 	struct hwtc *tc;
     35  1.1  drochner 
     36  1.1  drochner 	tc = malloc(sizeof(struct hwtc), M_DEVBUF, M_WAITOK|M_ZERO);
     37  1.1  drochner 	if (!tc)
     38  1.1  drochner 		return (-1);
     39  1.1  drochner 
     40  1.6    cegger 	tc->tc.tc_name = device_xname(dev);
     41  1.1  drochner 	tc->tc.tc_frequency = ACPI_PM_TIMER_FREQUENCY;
     42  1.1  drochner 	if (flags & ACPIPMT_32BIT)
     43  1.1  drochner 		tc->tc.tc_counter_mask = 0xffffffff;
     44  1.1  drochner 	else
     45  1.1  drochner 		tc->tc.tc_counter_mask = 0x00ffffff;
     46  1.1  drochner 	if (flags & ACPIPMT_BADLATCH) {
     47  1.1  drochner 		tc->tc.tc_get_timecount = acpihwtimer_read_safe;
     48  1.1  drochner 		tc->tc.tc_quality = 900;
     49  1.1  drochner 	} else {
     50  1.1  drochner 		tc->tc.tc_get_timecount = acpihwtimer_read_fast;
     51  1.1  drochner 		tc->tc.tc_quality = 1000;
     52  1.1  drochner 	}
     53  1.1  drochner 
     54  1.1  drochner 	tc->t = t;
     55  1.1  drochner 	tc->h = h;
     56  1.1  drochner 	tc->off = off;
     57  1.1  drochner 
     58  1.1  drochner 	tc->tc.tc_priv = tc;
     59  1.1  drochner 	tc_init(&tc->tc);
     60  1.2   xtraeme 	aprint_normal("%s: %d-bit timer\n", tc->tc.tc_name,
     61  1.1  drochner 		      (flags & ACPIPMT_32BIT ? 32 : 24));
     62  1.1  drochner 	return (0);
     63  1.1  drochner }
     64  1.1  drochner 
     65  1.1  drochner #define r(h) bus_space_read_4(h->t, h->h, h->off)
     66  1.1  drochner 
     67  1.1  drochner static u_int
     68  1.1  drochner acpihwtimer_read_safe(struct timecounter *tc)
     69  1.1  drochner {
     70  1.1  drochner 	struct hwtc *h = tc->tc_priv;
     71  1.1  drochner 	uint32_t t1, t2, t3;
     72  1.1  drochner 
     73  1.1  drochner 	t2 = r(h);
     74  1.1  drochner 	t3 = r(h);
     75  1.1  drochner 	do {
     76  1.1  drochner 		t1 = t2;
     77  1.1  drochner 		t2 = t3;
     78  1.1  drochner 		t3 = r(h);
     79  1.1  drochner 	} while ((t1 > t2) || (t2 > t3));
     80  1.1  drochner 	return (t2);
     81  1.1  drochner }
     82  1.1  drochner 
     83  1.1  drochner static u_int
     84  1.1  drochner acpihwtimer_read_fast(struct timecounter *tc)
     85  1.1  drochner {
     86  1.1  drochner 	struct hwtc *h = tc->tc_priv;
     87  1.1  drochner 
     88  1.1  drochner 	return r(h);
     89  1.1  drochner }
     90