Subject: Support for HPET found on AMD8111 LPC controller
To: NetBSD amd64 <port-amd64@NetBSD.org>
From: Nicolas Joly <njoly@pasteur.fr>
List: port-amd64
Date: 12/05/2006 14:09:57
--pWyiEgJYm5f9v55/
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline


Hi,

Here follow a small patch i made in the last few days to add support
for HPET timer found on amd8111 chpiset.

This timer is available on the LPC controller with a new amdpcib
driver.

amdpcib0 at pci0 dev 7 function 0
amdpcib0: Advanced Micro Devices AMD8111 LPC Controller (rev. 0x05)
timecounter: Timecounter "amdpcib0" frequency 14318179 Hz quality 2000
amdpcib0 32-bit HPET timer

njoly@lanfeust [~]> sysctl kern.timecounter
kern.timecounter.choice = clockinterrupt(q=0, f=100 Hz) TSC(q=-100, f=1992187890 Hz) amdpm0(q=1000, f=3579545 Hz) amdpcib0(q=2000, f=14318179 Hz) i8254(q=100, f=1193182 Hz) dummy(q=-1000000, f=1000000 Hz)
kern.timecounter.hardware = amdpcib0
kern.timecounter.timestepwarnings = 0

To enable it, simply patch your sources, and add these lines to your
kernel config file:

# AMD8111 LPC controller
amdpcib* at pci? dev ? function ?
isa0 at amdpcib?

Any volunteer, to test it ?

-- 
Nicolas Joly

Biological Software and Databanks.
Institut Pasteur, Paris.

--pWyiEgJYm5f9v55/
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="netbsd-amd8111hpet.diff"

Index: sys/conf/files
===================================================================
RCS file: /cvsroot/src/sys/conf/files,v
retrieving revision 1.819
diff -u -r1.819 files
--- sys/conf/files	24 Nov 2006 22:04:25 -0000	1.819
+++ sys/conf/files	5 Dec 2006 12:52:56 -0000
@@ -958,6 +958,9 @@
 define acpipmtimer
 file	dev/ic/acpipmtimer.c	acpipmtimer
 
+define hpet
+file 	dev/ic/hpet.c		hpet
+
 # Definitions for wscons
 # device attributes: display, display with emulator, keyboard, and mouse
 #
Index: sys/dev/ic/hpet.c
===================================================================
RCS file: sys/dev/ic/hpet.c
diff -N sys/dev/ic/hpet.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ sys/dev/ic/hpet.c	5 Dec 2006 12:52:56 -0000
@@ -0,0 +1,109 @@
+/* $NetBSD$ */
+
+/*
+ * Copyright (c) 2006 Nicolas Joly
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * High Precision Event Timer.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD$");
+
+#include <sys/systm.h>
+#include <sys/device.h>
+#include <sys/malloc.h>
+
+#include <sys/time.h>
+#include <sys/timetc.h>
+
+#include <machine/bus.h>
+
+#include <dev/ic/hpetreg.h>
+
+struct hpet_tc {
+	struct timecounter	tc;
+	bus_space_tag_t		memt;
+	bus_space_handle_t	memh;
+};
+
+int     hpet_attach(struct device *, bus_space_tag_t, bus_space_handle_t);
+
+static u_int	hpet_get_timecount(struct timecounter *);
+
+int
+hpet_attach(struct device *dev, bus_space_tag_t t, bus_space_handle_t h) {
+	struct hpet_tc *ht;
+	uint32_t val;
+
+	ht = malloc(sizeof(struct hpet_tc), M_DEVBUF, M_WAITOK|M_ZERO);
+	if (ht == NULL)
+		return -1;
+
+	ht->memt = t;
+	ht->memh = h;
+
+	ht->tc.tc_name = dev->dv_xname;
+	ht->tc.tc_get_timecount = hpet_get_timecount;
+	ht->tc.tc_quality = 2000;
+
+	/* XXX Only 32-bit supported for now */
+	val = bus_space_read_4(ht->memt, ht->memh, HPET_INFO);
+	if ((val & HPET_INFO_64BIT) != 0) {
+		aprint_normal("%s: Found 64-bit HPET, will only use lowest"
+		    "32-bits\n", dev->dv_xname);
+	}
+	ht->tc.tc_counter_mask = 0xffffffff;
+
+	/* Get frequency */
+	val = bus_space_read_4(ht->memt, ht->memh, HPET_PERIOD);
+	ht->tc.tc_frequency = 1000000000000000ULL / val;
+
+	/* Enable timer */
+	val = bus_space_read_4(ht->memt, ht->memh, HPET_CONFIG);
+	if ((val & HPET_CONFIG_ENABLE) == 0) {
+		val |= HPET_CONFIG_ENABLE;
+		bus_space_write_4(ht->memt, ht->memh, HPET_CONFIG, val);
+	}
+
+	ht->tc.tc_priv = ht;
+
+	tc_init(&ht->tc);
+	aprint_normal("%s 32-bit HPET timer\n", ht->tc.tc_name);
+
+	return 0;
+}
+
+
+static u_int
+hpet_get_timecount(struct timecounter *tc) {
+	struct hpet_tc *ht = tc->tc_priv;
+
+	return bus_space_read_4(ht->memt, ht->memh, HPET_MCOUNT);
+}
+
Index: sys/dev/ic/hpetreg.h
===================================================================
RCS file: sys/dev/ic/hpetreg.h
diff -N sys/dev/ic/hpetreg.h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ sys/dev/ic/hpetreg.h	5 Dec 2006 12:52:56 -0000
@@ -0,0 +1,41 @@
+/* $NetBSD$ */
+
+/*
+ * Copyright (c) 2006 Nicolas Joly
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _DEV_IC_HPETREG_H_
+#define _DEV_IC_HPETREG_H_
+
+#define HPET_INFO	0x00
+#define HPET_INFO_64BIT		0x2000
+#define HPET_PERIOD	0x04
+#define HPET_CONFIG	0x10
+#define HPET_CONFIG_ENABLE	0x0001
+#define HPET_MCOUNT	0xf0
+
+#endif /* _DEV_IC_HPETREG_H_ */
Index: sys/arch/amd64/pci/amdpcib.c
===================================================================
RCS file: sys/arch/amd64/pci/amdpcib.c
diff -N sys/arch/amd64/pci/amdpcib.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ sys/arch/amd64/pci/amdpcib.c	5 Dec 2006 12:52:56 -0000
@@ -0,0 +1,100 @@
+/* $NetBSD$ */
+
+/*
+ * Copyright (c) 2006 Nicolas Joly
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD$");
+
+#include <sys/systm.h>
+#include <sys/device.h>
+
+#include <machine/bus.h>
+
+#include <dev/pci/pcivar.h>
+#include <dev/pci/pcireg.h>
+#include <dev/pci/pcidevs.h>
+
+
+struct amdpcib_softc {
+	struct device		sc_dev;
+
+	bus_space_tag_t		sc_memt;
+	bus_space_handle_t	sc_memh;
+};
+
+static int	amdpcib_match(struct device *, struct cfdata *, void *);
+static void	amdpcib_attach(struct device *, struct device *, void *);
+
+extern void	pcibattach(struct device *, struct device *, void *);
+extern int	hpet_attach(struct device *, bus_space_tag_t, bus_space_handle_t);
+
+CFATTACH_DECL(amdpcib, sizeof(struct amdpcib_softc), amdpcib_match,
+    amdpcib_attach, NULL, NULL);
+
+static int
+amdpcib_match(struct device *parent, struct cfdata *match, void *aux) {
+	struct pci_attach_args *pa = aux;
+
+	if ((PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD) &&
+	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC8111_LPC))
+		return 2;
+
+	return 0;
+}
+
+static void
+amdpcib_attach(struct device *parent, struct device *self, void *aux) {
+	struct amdpcib_softc *sc;
+	struct pci_attach_args *pa;
+	pcireg_t conf, addr;
+
+	sc = (struct amdpcib_softc *)self;
+	pa = (struct pci_attach_args *)aux;
+
+	pcibattach(parent, self, aux);
+
+	conf = pci_conf_read(pa->pa_pc, pa->pa_tag, 0xa0);
+	if ((conf & 1) == 0) {
+		printf("%s: HPET timer is disabled\n", sc->sc_dev.dv_xname);
+		return;
+	}
+
+	sc->sc_memt = pa->pa_memt;
+
+	addr = conf & 0xfffffc00;
+	if (bus_space_map(sc->sc_memt, addr, 1024, 0,
+	    &sc->sc_memh)) {
+		printf("%s: failed to map mem\n", sc->sc_dev.dv_xname);
+		return;
+	}
+
+#ifdef __HAVE_TIMECOUNTER
+	hpet_attach(&sc->sc_dev, sc->sc_memt, sc->sc_memh);
+#endif
+}
Index: sys/arch/amd64/conf/files.amd64
===================================================================
RCS file: /cvsroot/src/sys/arch/amd64/conf/files.amd64,v
retrieving revision 1.29
diff -u -r1.29 files.amd64
--- sys/arch/amd64/conf/files.amd64	10 Sep 2006 19:50:48 -0000	1.29
+++ sys/arch/amd64/conf/files.amd64	5 Dec 2006 12:52:56 -0000
@@ -114,7 +117,11 @@
 # PCI-ISA bridges
 device	pcib: isabus
 attach	pcib at pci
-file	arch/amd64/pci/pcib.c			pcib
+file	arch/amd64/pci/pcib.c			pcib | amdpcib
+
+device 	amdpcib: isabus, hpet
+attach 	amdpcib at pci
+file 	arch/amd64/pci/amdpcib.c		amdpcib
 
 device 	aapic
 attach 	aapic at pci

--pWyiEgJYm5f9v55/--