summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--share/man/man4/Makefile6
-rw-r--r--share/man/man4/mcprtc.443
-rw-r--r--sys/arch/arm64/conf/GENERIC3
-rw-r--r--sys/arch/arm64/conf/RAMDISK3
-rw-r--r--sys/dev/i2c/files.i2c7
-rw-r--r--sys/dev/i2c/mcp794xx.c274
6 files changed, 330 insertions, 6 deletions
diff --git a/share/man/man4/Makefile b/share/man/man4/Makefile
index a618f9ddb04..d3414dd8a19 100644
--- a/share/man/man4/Makefile
+++ b/share/man/man4/Makefile
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile,v 1.729 2019/09/06 08:45:37 patrick Exp $
+# $OpenBSD: Makefile,v 1.730 2019/09/06 09:38:19 patrick Exp $
MAN= aac.4 abcrtc.4 ac97.4 acphy.4 acrtc.4 \
acpi.4 acpiac.4 acpials.4 acpiasus.4 acpibat.4 \
@@ -44,8 +44,8 @@ MAN= aac.4 abcrtc.4 ac97.4 acphy.4 acrtc.4 \
ix.4 ixgb.4 ixl.4 jmb.4 jme.4 jmphy.4 \
kate.4 kcov.4 km.4 ksyms.4 kubsan.4 kue.4 lc.4 lge.4 lii.4 lisa.4 \
lm.4 lmenv.4 lmn.4 lmtemp.4 lo.4 lpt.4 lxtphy.4 luphy.4 \
- maestro.4 mainbus.4 malo.4 maxds.4 maxrtc.4 maxtmp.4 mbg.4 mcx.4 \
- midi.4 mii.4 mfi.4 \
+ maestro.4 mainbus.4 malo.4 maxds.4 maxrtc.4 maxtmp.4 mbg.4 \
+ mcprtc.4 mcx.4 midi.4 mii.4 mfi.4 \
mfii.4 mlphy.4 mobileip.4 moscom.4 mos.4 mpe.4 mpath.4 mpi.4 mpii.4 \
mpip.4 mpu.4 msk.4 mpw.4 msts.4 mtd.4 mtdphy.4 mtio.4 mue.4 \
multicast.4 mvclock.4 mvdog.4 mvgicp.4 mvgpio.4 mvicu.4 mviic.4 \
diff --git a/share/man/man4/mcprtc.4 b/share/man/man4/mcprtc.4
new file mode 100644
index 00000000000..629669ab6ed
--- /dev/null
+++ b/share/man/man4/mcprtc.4
@@ -0,0 +1,43 @@
+.\" $OpenBSD: mcprtc.4,v 1.1 2019/09/06 09:38:19 patrick Exp $
+.\"
+.\" Copyright (c) 2019 Patrick Wildt <patrick@blueri.se>
+.\"
+.\" Permission to use, copy, modify, and distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd $Mdocdate: September 6 2019 $
+.Dt MCPRTC 4
+.Os
+.Sh NAME
+.Nm mcprtc
+.Nd Microchip MCP79400 real-time clock
+.Sh SYNOPSIS
+.Cd "mcprtc* at iic? addr 0x6f"
+.Sh DESCRIPTION
+The
+.Nm
+driver provides support for the Microchip MCP79400 real-time clock chip
+and similar variants.
+.Sh SEE ALSO
+.Xr iic 4 ,
+.Xr intro 4
+.Sh HISTORY
+The
+.Nm
+driver first appeared in
+.Ox 6.6 .
+.Sh AUTHORS
+.An -nosplit
+The
+.Nm
+driver was written by
+.An Patrick Wildt Aq Mt patrick@blueri.se .
diff --git a/sys/arch/arm64/conf/GENERIC b/sys/arch/arm64/conf/GENERIC
index 3215e8b6936..49cda0b2612 100644
--- a/sys/arch/arm64/conf/GENERIC
+++ b/sys/arch/arm64/conf/GENERIC
@@ -1,4 +1,4 @@
-# $OpenBSD: GENERIC,v 1.122 2019/09/06 08:45:37 patrick Exp $
+# $OpenBSD: GENERIC,v 1.123 2019/09/06 09:38:19 patrick Exp $
#
# GENERIC machine description file
#
@@ -366,6 +366,7 @@ abcrtc* at iic? # Abracon x80x RTC
fanpwr* at iic? # FAN53555 regulator
ipmi* at iic? # IPMI (SIIF)
islrtc* at iic? # ISL1208 RTC
+mcprtc* at iic? # MCP794XX RTC
rkpmic* at iic? # RK808 PMIC
sypwr* at iic? # SY8106A regulator
fusbtc* at iic? # USB Type-C controller
diff --git a/sys/arch/arm64/conf/RAMDISK b/sys/arch/arm64/conf/RAMDISK
index b0d5085094c..49f3b7c99de 100644
--- a/sys/arch/arm64/conf/RAMDISK
+++ b/sys/arch/arm64/conf/RAMDISK
@@ -1,4 +1,4 @@
-# $OpenBSD: RAMDISK,v 1.100 2019/09/06 08:45:37 patrick Exp $
+# $OpenBSD: RAMDISK,v 1.101 2019/09/06 09:38:19 patrick Exp $
#
# GENERIC machine description file
#
@@ -272,6 +272,7 @@ uk* at scsibus?
# I2C devices
abcrtc* at iic? # Abracon x80x RTC
islrtc* at iic? # ISL1208 RTC
+mcprtc* at iic? # MCP794XX RTC
rkpmic* at iic? # RK808 PMIC
fusbtc* at iic? # USB Type-C controller
diff --git a/sys/dev/i2c/files.i2c b/sys/dev/i2c/files.i2c
index 1018a660db9..e3598dce551 100644
--- a/sys/dev/i2c/files.i2c
+++ b/sys/dev/i2c/files.i2c
@@ -1,4 +1,4 @@
-# $OpenBSD: files.i2c,v 1.63 2019/08/19 18:31:02 kettenis Exp $
+# $OpenBSD: files.i2c,v 1.64 2019/09/06 09:38:19 patrick Exp $
# $NetBSD: files.i2c,v 1.3 2003/10/20 16:24:10 briggs Exp $
define i2c {[addr = -1], [size = -1]}
@@ -233,3 +233,8 @@ file dev/i2c/abx80x.c abcrtc
# IPMI SMBus System Interface (SSIF)
attach ipmi at i2c with ipmi_i2c
file dev/i2c/ipmi_i2c.c ipmi_i2c
+
+# Microchip MCP794xx Real Time Clock
+device mcprtc
+attach mcprtc at i2c
+file dev/i2c/mcp794xx.c mcprtc
diff --git a/sys/dev/i2c/mcp794xx.c b/sys/dev/i2c/mcp794xx.c
new file mode 100644
index 00000000000..93d2d5a5a89
--- /dev/null
+++ b/sys/dev/i2c/mcp794xx.c
@@ -0,0 +1,274 @@
+/* $OpenBSD: mcp794xx.c,v 1.1 2019/09/06 09:38:19 patrick Exp $ */
+/*
+ * Copyright (c) 2018 Mark Kettenis <kettenis@openbsd.org>
+ * Copyright (c) 2018 Patrick Wildt <patrick@blueri.se>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/device.h>
+
+#include <dev/i2c/i2cvar.h>
+
+#include <dev/clock_subr.h>
+
+extern todr_chip_handle_t todr_handle;
+
+#define MCP794XX_SC 0x00
+#define MCP794XX_SC_ST (1 << 7)
+#define MCP794XX_MN 0x01
+#define MCP794XX_HR 0x02
+#define MCP794XX_HR_PM (1 << 5)
+#define MCP794XX_HR_12H (1 << 6)
+#define MCP794XX_DW 0x03
+#define MCP794XX_DW_VBATEN (1 << 3)
+#define MCP794XX_DW_PWRFAIL (1 << 4)
+#define MCP794XX_DW_OSCRUN (1 << 5)
+#define MCP794XX_DT 0x04
+#define MCP794XX_MO 0x05
+#define MCP794XX_YR 0x06
+#define MCP794XX_CR 0x07
+#define MCP794XX_CR_EXTOSC (1 << 3)
+
+#define MCP794XX_NRTC_REGS 7
+
+struct mcprtc_softc {
+ struct device sc_dev;
+ i2c_tag_t sc_tag;
+ i2c_addr_t sc_addr;
+
+ int sc_extosc;
+ struct todr_chip_handle sc_todr;
+};
+
+int mcprtc_match(struct device *, void *, void *);
+void mcprtc_attach(struct device *, struct device *, void *);
+
+struct cfattach mcprtc_ca = {
+ sizeof(struct mcprtc_softc), mcprtc_match, mcprtc_attach
+};
+
+struct cfdriver mcprtc_cd = {
+ NULL, "mcprtc", DV_DULL
+};
+
+uint8_t mcprtc_reg_read(struct mcprtc_softc *, int);
+void mcprtc_reg_write(struct mcprtc_softc *, int, uint8_t);
+int mcprtc_clock_read(struct mcprtc_softc *, struct clock_ymdhms *);
+int mcprtc_clock_write(struct mcprtc_softc *, struct clock_ymdhms *);
+int mcprtc_gettime(struct todr_chip_handle *, struct timeval *);
+int mcprtc_settime(struct todr_chip_handle *, struct timeval *);
+
+int
+mcprtc_match(struct device *parent, void *match, void *aux)
+{
+ struct i2c_attach_args *ia = aux;
+
+ if (strcmp(ia->ia_name, "microchip,mcp7940x") == 0 ||
+ strcmp(ia->ia_name, "microchip,mcp7941x") == 0)
+ return 1;
+
+ return 0;
+}
+
+void
+mcprtc_attach(struct device *parent, struct device *self, void *aux)
+{
+ struct mcprtc_softc *sc = (struct mcprtc_softc *)self;
+ struct i2c_attach_args *ia = aux;
+
+ sc->sc_tag = ia->ia_tag;
+ sc->sc_addr = ia->ia_addr;
+
+ sc->sc_todr.cookie = sc;
+ sc->sc_todr.todr_gettime = mcprtc_gettime;
+ sc->sc_todr.todr_settime = mcprtc_settime;
+
+ printf("\n");
+ todr_handle = &sc->sc_todr;
+}
+
+int
+mcprtc_gettime(struct todr_chip_handle *handle, struct timeval *tv)
+{
+ struct mcprtc_softc *sc = handle->cookie;
+ struct clock_ymdhms dt;
+ int error;
+
+ error = mcprtc_clock_read(sc, &dt);
+ if (error)
+ return error;
+
+ if (dt.dt_sec > 59 || dt.dt_min > 59 || dt.dt_hour > 23 ||
+ dt.dt_day > 31 || dt.dt_day == 0 ||
+ dt.dt_mon > 12 || dt.dt_mon == 0 ||
+ dt.dt_year < POSIX_BASE_YEAR)
+ return EINVAL;
+
+ tv->tv_sec = clock_ymdhms_to_secs(&dt);
+ tv->tv_usec = 0;
+ return 0;
+}
+
+int
+mcprtc_settime(struct todr_chip_handle *handle, struct timeval *tv)
+{
+ struct mcprtc_softc *sc = handle->cookie;
+ struct clock_ymdhms dt;
+
+ clock_secs_to_ymdhms(tv->tv_sec, &dt);
+
+ return mcprtc_clock_write(sc, &dt);
+}
+
+uint8_t
+mcprtc_reg_read(struct mcprtc_softc *sc, int reg)
+{
+ uint8_t cmd = reg;
+ uint8_t val;
+ int error;
+
+ iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
+ error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
+ &cmd, sizeof cmd, &val, sizeof val, I2C_F_POLL);
+ iic_release_bus(sc->sc_tag, I2C_F_POLL);
+
+ if (error) {
+ printf("%s: can't read register 0x%02x\n",
+ sc->sc_dev.dv_xname, reg);
+ val = 0xff;
+ }
+
+ return val;
+}
+
+void
+mcprtc_reg_write(struct mcprtc_softc *sc, int reg, uint8_t val)
+{
+ uint8_t cmd = reg;
+ int error;
+
+ iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
+ error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
+ &cmd, sizeof cmd, &val, sizeof val, I2C_F_POLL);
+ iic_release_bus(sc->sc_tag, I2C_F_POLL);
+
+ if (error) {
+ printf("%s: can't write register 0x%02x\n",
+ sc->sc_dev.dv_xname, reg);
+ }
+}
+
+int
+mcprtc_clock_read(struct mcprtc_softc *sc, struct clock_ymdhms *dt)
+{
+ uint8_t regs[MCP794XX_NRTC_REGS];
+ uint8_t cmd = MCP794XX_SC;
+ int error;
+
+ /* Don't trust the RTC if the oscillator is not running. */
+ if (!(mcprtc_reg_read(sc, MCP794XX_DW) & MCP794XX_DW_OSCRUN))
+ return EIO;
+
+ iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
+ error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
+ &cmd, sizeof(cmd), regs, MCP794XX_NRTC_REGS, I2C_F_POLL);
+ iic_release_bus(sc->sc_tag, I2C_F_POLL);
+
+ if (error) {
+ printf("%s: can't read RTC\n", sc->sc_dev.dv_xname);
+ return error;
+ }
+
+ /*
+ * Convert the MCP794XX's register values into something useable.
+ */
+ dt->dt_sec = FROMBCD(regs[0] & 0x7f);
+ dt->dt_min = FROMBCD(regs[1] & 0x7f);
+ dt->dt_hour = FROMBCD(regs[2] & 0x1f);
+ if ((regs[2] & MCP794XX_HR_12H) && (regs[2] & MCP794XX_HR_PM))
+ dt->dt_hour += 12;
+ dt->dt_wday = FROMBCD(regs[3] & 0x7);
+ dt->dt_day = FROMBCD(regs[4] & 0x3f);
+ dt->dt_mon = FROMBCD(regs[5] & 0x1f);
+ dt->dt_year = FROMBCD(regs[6]) + 2000;
+
+ return 0;
+}
+
+int
+mcprtc_clock_write(struct mcprtc_softc *sc, struct clock_ymdhms *dt)
+{
+ uint8_t regs[MCP794XX_NRTC_REGS];
+ uint8_t cmd = MCP794XX_SC;
+ uint8_t oscoff, oscbit;
+ uint8_t reg;
+ int error, i;
+
+ /*
+ * Convert our time representation into something the MCP794XX
+ * can understand.
+ */
+ regs[0] = TOBCD(dt->dt_sec);
+ regs[1] = TOBCD(dt->dt_min);
+ regs[2] = TOBCD(dt->dt_hour);
+ regs[3] = TOBCD(dt->dt_wday) | MCP794XX_DW_VBATEN;
+ regs[4] = TOBCD(dt->dt_day);
+ regs[5] = TOBCD(dt->dt_mon);
+ regs[6] = TOBCD(dt->dt_year - 2000);
+
+ /* Stop RTC. */
+ if (sc->sc_extosc) {
+ oscoff = MCP794XX_CR;
+ oscbit = MCP794XX_CR_EXTOSC;
+ } else {
+ oscoff = MCP794XX_SC;
+ oscbit = MCP794XX_SC_ST;
+ }
+
+ /* Stop RTC such that we can write to it. */
+ reg = mcprtc_reg_read(sc, oscoff);
+ reg &= ~oscbit;
+ mcprtc_reg_write(sc, oscoff, reg);
+
+ for (i = 0; i < 10; i++) {
+ reg = mcprtc_reg_read(sc, MCP794XX_DW);
+ if ((reg & MCP794XX_DW_OSCRUN) == 0)
+ break;
+ delay(10);
+ }
+ if (i == 10) {
+ error = EIO;
+ goto fail;
+ }
+
+ iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
+ error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
+ &cmd, sizeof(cmd), regs, MCP794XX_NRTC_REGS, I2C_F_POLL);
+ iic_release_bus(sc->sc_tag, I2C_F_POLL);
+
+ /* Restart RTC. */
+ reg = mcprtc_reg_read(sc, oscoff);
+ reg |= oscbit;
+ mcprtc_reg_write(sc, oscoff, reg);
+
+fail:
+ if (error) {
+ printf("%s: can't write RTC\n", sc->sc_dev.dv_xname);
+ return error;
+ }
+
+ return 0;
+}