1*f5b064eeSdyoung /* $NetBSD: geodecntr.c,v 1.9 2011/07/01 17:37:26 dyoung Exp $ */
209b51ec9Skardel
309b51ec9Skardel /*-
409b51ec9Skardel * Copyright (c) 2006 The NetBSD Foundation, Inc.
509b51ec9Skardel * All rights reserved.
609b51ec9Skardel *
709b51ec9Skardel * This code is derived from software contributed to The NetBSD Foundation
809b51ec9Skardel * by Frank Kardel inspired from the patches to FreeBSD by Poul Henning Kamp
909b51ec9Skardel *
1009b51ec9Skardel * Redistribution and use in source and binary forms, with or without
1109b51ec9Skardel * modification, are permitted provided that the following conditions
1209b51ec9Skardel * are met:
1309b51ec9Skardel * 1. Redistributions of source code must retain the above copyright
1409b51ec9Skardel * notice, this list of conditions and the following disclaimer.
1509b51ec9Skardel * 2. Redistributions in binary form must reproduce the above copyright
1609b51ec9Skardel * notice, this list of conditions and the following disclaimer in the
1709b51ec9Skardel * documentation and/or other materials provided with the distribution.
1809b51ec9Skardel *
1909b51ec9Skardel * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2009b51ec9Skardel * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2109b51ec9Skardel * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2209b51ec9Skardel * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2309b51ec9Skardel * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2409b51ec9Skardel * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2509b51ec9Skardel * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2609b51ec9Skardel * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2709b51ec9Skardel * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2809b51ec9Skardel * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2909b51ec9Skardel * POSSIBILITY OF SUCH DAMAGE.
3009b51ec9Skardel */
3109b51ec9Skardel
3209b51ec9Skardel #include <sys/cdefs.h>
33*f5b064eeSdyoung __KERNEL_RCSID(0, "$NetBSD: geodecntr.c,v 1.9 2011/07/01 17:37:26 dyoung Exp $");
3409b51ec9Skardel
3509b51ec9Skardel #include <sys/param.h>
3609b51ec9Skardel #include <sys/systm.h>
3709b51ec9Skardel #include <sys/kernel.h>
3809b51ec9Skardel #include <sys/device.h>
3909b51ec9Skardel #include <machine/cpu.h>
4009b51ec9Skardel #include <sys/timetc.h>
41*f5b064eeSdyoung #include <sys/bus.h>
4209b51ec9Skardel #include <arch/i386/pci/geodevar.h>
4309b51ec9Skardel #include <arch/i386/pci/geodereg.h>
4409b51ec9Skardel
4509b51ec9Skardel struct geodecntr_softc {
4609b51ec9Skardel struct geode_gcb_softc *sc_gcb_dev;
4709b51ec9Skardel struct timecounter sc_tc;
4809b51ec9Skardel };
4909b51ec9Skardel
5009b51ec9Skardel static unsigned geode_get_timecount(struct timecounter *);
5109b51ec9Skardel
5209b51ec9Skardel static int attached = 0;
5309b51ec9Skardel
5409b51ec9Skardel static int
geodecntr_match(device_t parent,cfdata_t match,void * aux)55ed38b748Sxtraeme geodecntr_match(device_t parent, cfdata_t match, void *aux)
5609b51ec9Skardel {
5709b51ec9Skardel return !attached;
5809b51ec9Skardel }
5909b51ec9Skardel
6009b51ec9Skardel /*
6109b51ec9Skardel * attach time counter
6209b51ec9Skardel */
6309b51ec9Skardel static void
geodecntr_attach(device_t parent,device_t self,void * aux)648d9f58eaSdyoung geodecntr_attach(device_t parent, device_t self, void *aux)
6509b51ec9Skardel {
668d9f58eaSdyoung struct geodecntr_softc *sc = device_private(self);
6709b51ec9Skardel
68a6a2bdb8Sjmcneill aprint_naive("\n");
69a6a2bdb8Sjmcneill aprint_normal(": AMD Geode SC1100 27MHz Counter\n");
7009b51ec9Skardel
718d9f58eaSdyoung sc->sc_gcb_dev = device_private(parent);
7209b51ec9Skardel
7309b51ec9Skardel /*
7409b51ec9Skardel * select 27MHz, no powerdown, no interrupt
7509b51ec9Skardel */
7609b51ec9Skardel bus_space_write_1(sc->sc_gcb_dev->sc_iot, sc->sc_gcb_dev->sc_ioh,
7709b51ec9Skardel SC1100_GCB_TMCNFG_B, SC1100_TMCNFG_TMCLKSEL);
7809b51ec9Skardel
7909b51ec9Skardel memset(&sc->sc_tc, 0, sizeof(sc->sc_tc));
8009b51ec9Skardel
8109b51ec9Skardel sc->sc_tc.tc_get_timecount = geode_get_timecount;
8209b51ec9Skardel sc->sc_tc.tc_poll_pps = NULL;
8309b51ec9Skardel sc->sc_tc.tc_counter_mask = 0xffffffff;
8409b51ec9Skardel sc->sc_tc.tc_frequency = 27000000;
8509b51ec9Skardel sc->sc_tc.tc_name = "geodecounter";
8609b51ec9Skardel sc->sc_tc.tc_priv = sc;
8709b51ec9Skardel sc->sc_tc.tc_quality = 1000;
8809b51ec9Skardel
8909b51ec9Skardel tc_init(&sc->sc_tc);
9009b51ec9Skardel
9109b51ec9Skardel attached = 1;
9209b51ec9Skardel }
9309b51ec9Skardel
948d9f58eaSdyoung static int
geodecntr_detach(device_t self,int flags)958d9f58eaSdyoung geodecntr_detach(device_t self, int flags)
968d9f58eaSdyoung {
978d9f58eaSdyoung struct geodecntr_softc *sc = device_private(self);
988d9f58eaSdyoung
998d9f58eaSdyoung attached = 0;
1008d9f58eaSdyoung return tc_detach(&sc->sc_tc);
1018d9f58eaSdyoung }
1028d9f58eaSdyoung
10309b51ec9Skardel /*
10409b51ec9Skardel * read counter
10509b51ec9Skardel */
geode_get_timecount(struct timecounter * tc)10609b51ec9Skardel static unsigned geode_get_timecount(struct timecounter *tc)
10709b51ec9Skardel {
10809b51ec9Skardel struct geodecntr_softc *sc = (struct geodecntr_softc *)tc->tc_priv;
10909b51ec9Skardel
1108d9f58eaSdyoung return bus_space_read_4(sc->sc_gcb_dev->sc_iot, sc->sc_gcb_dev->sc_ioh,
1118d9f58eaSdyoung SC1100_GCB_TMVALUE_L);
11209b51ec9Skardel }
11309b51ec9Skardel
114ed38b748Sxtraeme CFATTACH_DECL_NEW(geodecntr, sizeof(struct geodecntr_softc),
1158d9f58eaSdyoung geodecntr_match, geodecntr_attach, geodecntr_detach, NULL);
11609b51ec9Skardel
117