Module Name:    src
Committed By:   macallan
Date:           Fri Oct 12 21:44:32 UTC 2018

Added Files:
        src/sys/arch/sparc64/dev: tadpmu.c tadpmureg.h tadpmuvar.h

Log Message:
experimental code to talk to the PMU found in Tadpole Viper laptops


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 src/sys/arch/sparc64/dev/tadpmu.c \
    src/sys/arch/sparc64/dev/tadpmureg.h src/sys/arch/sparc64/dev/tadpmuvar.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Added files:

Index: src/sys/arch/sparc64/dev/tadpmu.c
diff -u /dev/null src/sys/arch/sparc64/dev/tadpmu.c:1.1
--- /dev/null	Fri Oct 12 21:44:32 2018
+++ src/sys/arch/sparc64/dev/tadpmu.c	Fri Oct 12 21:44:32 2018
@@ -0,0 +1,261 @@
+/*/* $NetBSD: tadpmu.c,v 1.1 2018/10/12 21:44:32 macallan Exp $ */
+
+/*-
+ * Copyright (c) 2018 Michael Lorenz <[email protected]>
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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.
+ */
+
+/* a driver for the PMU found in Tadpole Wiper and possibly SPARCle laptops */
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/device.h>
+#include <sys/malloc.h>
+#include <sys/bus.h>
+#include <sys/intr.h>
+
+#include <dev/sysmon/sysmonvar.h>
+
+#include <sparc64/dev/tadpmureg.h>
+#include <sparc64/dev/tadpmuvar.h>
+
+static bus_space_tag_t tadpmu_iot;
+static bus_space_handle_t tadpmu_hcmd;
+static bus_space_handle_t tadpmu_hdata;
+static struct sysmon_envsys *tadpmu_sme;
+static envsys_data_t tadpmu_sensors[5];
+
+static inline void
+tadpmu_cmd(uint8_t d)
+{
+	bus_space_write_1(tadpmu_iot, tadpmu_hcmd, 0, d);
+}
+
+static inline void
+tadpmu_wdata(uint8_t d)
+{
+	bus_space_write_1(tadpmu_iot, tadpmu_hdata, 0, d);
+}
+
+static inline uint8_t
+tadpmu_status(void)
+{
+	return bus_space_read_1(tadpmu_iot, tadpmu_hcmd, 0);
+}
+
+static inline uint8_t
+tadpmu_data(void)
+{
+	return bus_space_read_1(tadpmu_iot, tadpmu_hdata, 0);
+}
+
+static void
+tadpmu_flush(void)
+{
+	volatile uint8_t junk, d;
+	int bail = 0;
+
+	d = tadpmu_status();
+	while (d & STATUS_HAVE_DATA) {
+		junk = tadpmu_data();
+		__USE(junk);
+		delay(10);
+		bail++;
+		if (bail > 100) {
+			printf("%s: timeout waiting for data out to clear %2x\n",
+			    __func__, d);
+			break;
+		}
+		d = tadpmu_status();
+	}
+	bail = 0;
+	d = tadpmu_status();
+	while (d & STATUS_SEND_DATA) {
+		bus_space_write_1(tadpmu_iot, tadpmu_hdata, 0, 0);
+		delay(10);
+		bail++;
+		if (bail > 100) {
+			printf("%s: timeout waiting for data in to clear %02x\n",
+			    __func__, d);
+			break;
+		}
+		d = tadpmu_status();
+	}
+}
+
+static void
+tadpmu_send_cmd(uint8_t cmd)
+{
+	int bail = 0;
+	uint8_t d;
+
+	tadpmu_cmd(cmd);
+
+	d = tadpmu_status();
+	while ((d & STATUS_CMD_IN_PROGRESS) == 0) {
+		delay(10);
+		bail++;
+		if (bail > 100) {
+			printf("%s: timeout waiting for command to start\n",
+			    __func__);
+			break;
+		}
+		d = tadpmu_status();
+	}
+}
+
+static uint8_t
+tadpmu_recv(void)
+{
+	int bail = 0;
+	uint8_t d;
+
+	d = tadpmu_status();	
+	while ((d & STATUS_HAVE_DATA) == 0) {
+		delay(10);
+		bail++;
+		if (bail > 1000) {
+			printf("%s: timeout waiting for data %02x\n", __func__, d);
+			break;
+		}
+		d = tadpmu_status();
+	}
+	return bus_space_read_1(tadpmu_iot, tadpmu_hdata, 0);
+}
+
+static void
+tadpmu_send(uint8_t v)
+{
+	int bail = 0;
+	uint8_t d;
+
+	d = tadpmu_status();	
+	while ((d & STATUS_SEND_DATA) == 0) {
+		delay(10);
+		bail++;
+		if (bail > 1000) {
+			printf("%s: timeout waiting for PMU ready %02x\n", __func__, d);
+			break;
+		}
+		d = tadpmu_status();
+	}
+
+	tadpmu_wdata(v);
+
+	while ((d & STATUS_SEND_DATA) != 0) {
+		delay(10);
+		bail++;
+		if (bail > 1000) {
+			printf("%s: timeout waiting for accept data %02x\n", __func__, d);
+			break;
+		}
+		d = tadpmu_status();
+	}
+}
+
+static void
+tadpmu_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
+{
+	int res;
+	if (edata->private > 0) {
+		tadpmu_flush();
+		tadpmu_send_cmd(edata->private);
+		res = tadpmu_recv();
+		if (edata->units == ENVSYS_STEMP) {
+			edata->value_cur = res * 1000000 + 273150000;
+		} else {
+			edata->value_cur = res;
+		}
+		edata->state = ENVSYS_SVALID;
+	} else {
+		edata->state = ENVSYS_SINVALID;
+	}
+}
+
+int 
+tadpmu_init(bus_space_tag_t t, bus_space_handle_t hcmd, bus_space_handle_t hdata)
+{
+	int ver;
+
+	tadpmu_iot = t;
+	tadpmu_hcmd = hcmd;
+	tadpmu_hdata = hdata;
+
+	tadpmu_flush();
+	delay(1000);
+
+	tadpmu_send_cmd(CMD_READ_VERSION);
+	ver = tadpmu_recv();
+	printf("Tadpole PMU Version 1.%d\n", ver);	
+
+	tadpmu_send_cmd(CMD_SET_OPMODE);
+	tadpmu_send(0x75);
+
+	tadpmu_send_cmd(CMD_READ_SYSTEMP);
+	ver = tadpmu_recv();
+	printf("Temperature %d\n", ver);	
+
+	tadpmu_send_cmd(CMD_READ_VBATT);
+	ver = tadpmu_recv();
+	printf("Battery voltage %d\n", ver);	
+
+	tadpmu_send_cmd(CMD_READ_GENSTAT);
+	ver = tadpmu_recv();
+	printf("status %02x\n", ver);
+
+	tadpmu_sme = sysmon_envsys_create();
+	tadpmu_sme->sme_name = "tadpmu";
+	tadpmu_sme->sme_cookie = NULL;
+	tadpmu_sme->sme_refresh = tadpmu_sensors_refresh;
+
+	tadpmu_sensors[0].units = ENVSYS_STEMP;
+	tadpmu_sensors[0].private = CMD_READ_SYSTEMP;
+	strcpy(tadpmu_sensors[0].desc, "systemp");
+	sysmon_envsys_sensor_attach(tadpmu_sme, &tadpmu_sensors[0]);
+#ifdef TADPMU_DEBUG
+	tadpmu_sensors[1].units = ENVSYS_INTEGER;
+	tadpmu_sensors[1].private = 0x17;
+	strcpy(tadpmu_sensors[1].desc, "reg 17");
+	sysmon_envsys_sensor_attach(tadpmu_sme, &tadpmu_sensors[1]);
+	tadpmu_sensors[2].units = ENVSYS_INTEGER;
+	tadpmu_sensors[2].private = 0x18;
+	strcpy(tadpmu_sensors[2].desc, "reg 18");
+	sysmon_envsys_sensor_attach(tadpmu_sme, &tadpmu_sensors[2]);
+	tadpmu_sensors[3].units = ENVSYS_INTEGER;
+	tadpmu_sensors[3].private = CMD_READ_GENSTAT;
+	strcpy(tadpmu_sensors[3].desc, "genstat");
+	sysmon_envsys_sensor_attach(tadpmu_sme, &tadpmu_sensors[3]);
+#if 0
+	tadpmu_sensors[4].units = ENVSYS_INTEGER;
+	tadpmu_sensors[4].private = CMD_READ_VBATT;
+	strcpy(tadpmu_sensors[4].desc, "Vbatt");
+	sysmon_envsys_sensor_attach(tadpmu_sme, &tadpmu_sensors[4]);
+#endif
+#endif
+	sysmon_envsys_register(tadpmu_sme);
+
+	return 0;
+}
+
Index: src/sys/arch/sparc64/dev/tadpmureg.h
diff -u /dev/null src/sys/arch/sparc64/dev/tadpmureg.h:1.1
--- /dev/null	Fri Oct 12 21:44:32 2018
+++ src/sys/arch/sparc64/dev/tadpmureg.h	Fri Oct 12 21:44:32 2018
@@ -0,0 +1,85 @@
+/* $NetBSD: tadpmureg.h,v 1.1 2018/10/12 21:44:32 macallan Exp $ */
+
+/*-
+ * Copyright (c) 2018 Michael Lorenz <[email protected]>
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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.
+ */
+
+/* registers and commands for the PMU found in Tadpole Viper laptops */
+
+#ifndef TADPMUREG_H
+#define TADPMUREG_H
+
+/* all values were found by looking at OF methods on the tadpmu node */
+
+/* these registers live in the pckbc's address space */
+#define TADPMU_CMD	0x6
+#define TADPMU_STATUS	0x6
+#define TADPMU_DATA	0x2
+
+#define STATUS_HAVE_DATA	0x01
+#define STATUS_CMD_IN_PROGRESS	0x02
+#define STATUS_SEND_DATA	0x08
+
+#define CMD_SET_OPMODE		0x41	/* not sure what exactly this does... */
+#define		OPMODE_UNIX	0x75	/* other than toggling the UNIX mode  */
+#define		OPMODE_OF	0x67	/* bit in the GENSTAT register        */
+#define CMD_SET_BACKLIGHT	0x43	/* apparently 0 - 1f */
+#define CMD_SET_CPUSPEED	0x44	/* in 10MHz, so 120 == 1.2GHz */
+#define CMD_SET_FANSPEED	0x46	/* ??? */
+#define CMD_SET_VOLUME		0x48	/* beeper volume */
+
+#define CMD_READ_GENSTAT	0x10
+#define CMD_READ_BACKLIGHT	0x11
+#define CMD_READ_SYSTEMP	0x12	/* temperature */
+#define CMD_READ_VOLUME		0x13
+#define CMD_READ_VBATT		0x14
+#define CMD_READ_VERSION	0x15
+#define CMD_READ_CPUSPEED	0x16
+/* 0x17 returns a byte, always 0 */
+/* 0x18 returns a byte, always(?) 0x77 */ 
+#define CMD_READ_GENSTAT2	0x19
+#define CMD_READ_FANSPEED	0x50	/* takes a parameter, returns nothing? */
+
+/* these are according to the ROM methods
+   #define GENSTAT_DC_PRESENT		0x01
+   #define GENSTAT_DC_ENABLE		0x02
+   #define GENSTAT_BATTERY_PRESENT	0x04
+   #define GENSTAT_BATTERY_CHARGING	0x08
+   #define GENSTAT_LID_CLOSED		0x10
+   #define GENSTAT_UNIX_MODE		0x20
+   #define GENSTAT_SPREADSPECTRUM	0x40
+*/
+
+/* these are according to experiment */
+#define GENSTAT_UNIX_MODE		0x01
+   #define GENSTAT_DC_PRESENT		0x08	/* guess */
+   #define GENSTAT_DC_ENABLE		0x18	/* guess */
+#define GENSTAT_LID_CLOSED		0x80
+
+#define GENSTAT2_MUTE		0x02
+
+
+
+#endif /* TADPMUREG_H */
\ No newline at end of file
Index: src/sys/arch/sparc64/dev/tadpmuvar.h
diff -u /dev/null src/sys/arch/sparc64/dev/tadpmuvar.h:1.1
--- /dev/null	Fri Oct 12 21:44:32 2018
+++ src/sys/arch/sparc64/dev/tadpmuvar.h	Fri Oct 12 21:44:32 2018
@@ -0,0 +1,36 @@
+/* $NetBSD: tadpmuvar.h,v 1.1 2018/10/12 21:44:32 macallan Exp $ */
+
+/*-
+ * Copyright (c) 2018 Michael Lorenz <[email protected]>
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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.
+ */
+
+/* functions to talk to the PMU found in Tadpole Viper laptops */
+
+#ifndef TADPMUVAR_H
+#define TADPMUVAR_H
+
+int tadpmu_init(bus_space_tag_t, bus_space_handle_t, bus_space_handle_t);
+ 
+#endif /* TADPMUVAR_H */
\ No newline at end of file

Reply via email to