From nobody Wed Nov 24 06:41:58 2021 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id D8A6B18ABE97; Wed, 24 Nov 2021 06:42:00 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4HzWdM0J9rz4kpw; Wed, 24 Nov 2021 06:41:58 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id AC1982558A; Wed, 24 Nov 2021 06:41:58 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 1AO6fwKh037228; Wed, 24 Nov 2021 06:41:58 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1AO6fwHc037227; Wed, 24 Nov 2021 06:41:58 GMT (envelope-from git) Date: Wed, 24 Nov 2021 06:41:58 GMT Message-Id: <202111240641.1AO6fwHc037227@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Wojciech Macek Subject: git: 26d65475676f - main - Pcf85063: Add RTC driver List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: wma X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 26d65475676f5743eee9929f6b1c3da424450c4c Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1637736119; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=N0RxKVSMrCPkh5B6mbk2J9pz1I6w8BDYzvUzIhuWz1s=; b=BnE8xuT9lxWBnBOTHS9Ox05OKZyBGuMtlsxTdF8Fro5TlpVO82VNxpU18mzqFpBa0g0CDC FcRmidD6Lv+dgdQqwZ+3dkAdwBLaQLN6XVoKEUVGsSZjeuVLNnYZvWf+r3+cKDutFWrv2j w7nvQR/9Hx1kO9uydEtfnkD/YO7j7V7WFQ+4NJy+lis4JK5ScyaSmMTyapULBuuaoXdew0 vM3SL3A6C8A0HGS5uXHyYJxt6C2aofunIDOXIk9egrZ9OUW65Gc+BZXFmYGjNjhVdpns8Z GK89jWCQlo1m5j/U/mQqMTLGo4gw2ujebdYLVr1Pf+23TlmpJfwZpoOStU7t0A== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1637736119; a=rsa-sha256; cv=none; b=ao1JzJxeaH9VQW4dkWodH8ugQt2mCRPYrO1Jw/BdR1qJ6kVIjFSdf9tH8wWG/sOYYf0NDE UIaJUPjLmDiMgaXBB/HsJa7bKgq8auBs+/dewGODhLY8FEOWNbzHnoT42LuzUTwxGSr3RX HPTx6PIK+af19TH30fPBIiq4UIm5ABDs34J00+Ircm5rgRsTZ3sWUbl135QvO0fRCCeHY/ 1xQB/h0fbj0iq7a4zraWgamXQym/EkpajV0dimUE0NHvV7NCWYHRlIr+zegOprOGE2m8vt KicHJITnBEEJkeyeEjteNllK4Odu4NkN+fLehU3yzUZPxYa/G2WEH11XLXR7hQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by wma: URL: https://cgit.FreeBSD.org/src/commit/?id=26d65475676f5743eee9929f6b1c3da424450c4c commit 26d65475676f5743eee9929f6b1c3da424450c4c Author: Hubert Mazur AuthorDate: 2021-10-08 08:42:30 +0000 Commit: Wojciech Macek CommitDate: 2021-11-24 06:40:38 +0000 Pcf85063: Add RTC driver Add driver for pcf85063 real time clock. Register set and get time methods. Parse data obtained from bus according to specification and fill kernel structures. Obtained from: Semihalf Sponsored by: Alstom Group Differential revision: https://reviews.freebsd.org/D32817 --- sys/arm64/conf/std.nxp | 1 + sys/conf/files | 1 + sys/dev/iicbus/rtc/pcf85063.c | 268 ++++++++++++++++++++++++++++++++++++++++++ sys/modules/i2c/Makefile | 4 +- 4 files changed, 272 insertions(+), 2 deletions(-) diff --git a/sys/arm64/conf/std.nxp b/sys/arm64/conf/std.nxp index 75e57a6b2386..ded5611c3da0 100644 --- a/sys/arm64/conf/std.nxp +++ b/sys/arm64/conf/std.nxp @@ -9,6 +9,7 @@ options SOC_NXP_LS device pca9547 # NPX I2C bus multiplexer device pcf8563 # NXP Real-time clock/calendar device tca6408 # NXP I2C gpio expander +device pcf85063 # NXP Real-time clock # Serial (COM) ports device uart_ns8250 # ns8250-type UART driver diff --git a/sys/conf/files b/sys/conf/files index 56ac8d60db31..f25508f1fded 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -1866,6 +1866,7 @@ dev/iicbus/ofw_iicbus.c optional fdt iicbus dev/iicbus/pcf8574.c optional pcf8574 dev/iicbus/pcf8591.c optional pcf8591 dev/iicbus/rtc8583.c optional rtc8583 +dev/iicbus/rtc/pcf85063.c optional pcf85063 iicbus fdt dev/iicbus/rtc/rx8803.c optional rx8803 iicbus fdt dev/iicbus/s35390a.c optional s35390a dev/iicbus/sy8106a.c optional sy8106a ext_resources fdt diff --git a/sys/dev/iicbus/rtc/pcf85063.c b/sys/dev/iicbus/rtc/pcf85063.c new file mode 100644 index 000000000000..f3038ac32340 --- /dev/null +++ b/sys/dev/iicbus/rtc/pcf85063.c @@ -0,0 +1,268 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2021 Alstom Group. + * Copyright (c) 2021 Semihalf. + * + * 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 AUTHOR ``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 AUTHOR 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 +__FBSDID("$FreeBSD$"); + +#include "opt_platform.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#include "clock_if.h" +#include "iicbus_if.h" + +#define BIT(x) (1 << (x)) + +#define PCF85063_CTRL1_REG 0x0 +#define PCF85063_TIME_REG 0x4 + +#define PCF85063_CTRL1_TIME_FORMAT BIT(1) +#define PCF85063_CTRL1_RTC_CLK_STOP BIT(5) +#define PCF85063_TIME_REG_OSC_STOP BIT(7) + +#define PCF85063_HALF_OF_SEC_NS 500000000 + +struct pcf85063_time { + uint8_t sec; + uint8_t min; + uint8_t hour; + uint8_t day; + uint8_t dow; + uint8_t mon; + uint8_t year; +}; + +static int pcf85063_attach(device_t dev); +static int pcf85063_detach(device_t dev); +static int pcf85063_probe(device_t dev); + +static int pcf85063_get_time(device_t dev, struct timespec *ts); +static int pcf85063_set_time(device_t dev, struct timespec *ts); + +static int pcf85063_check_status(device_t dev); + +static struct ofw_compat_data pcf85063_compat_data[] = { + { "nxp,pcf85063", 1}, + { NULL, 0} +}; + +static int +pcf85063_check_status(device_t dev) +{ + uint8_t flags; + int error; + + error = iicdev_readfrom(dev, PCF85063_TIME_REG, &flags, 1, IIC_WAIT); + if (error != 0) + return (error); + + if (flags & PCF85063_TIME_REG_OSC_STOP) { + device_printf(dev, + "Low voltage flag set, date is incorrect.\n"); + return (ENXIO); + } + + return (0); +} + +static int +pcf85063_attach(device_t dev) +{ + + clock_register_flags(dev, 1000000, 0); + clock_schedule(dev, 1); + + return (0); +} + +static int +pcf85063_detach(device_t dev) +{ + + clock_unregister(dev); + + return (0); +} + +static int +pcf85063_get_time(device_t dev, struct timespec *ts) +{ + struct pcf85063_time data; + struct bcd_clocktime bcd; + uint8_t control_reg; + int error; + + error = pcf85063_check_status(dev); + if (error != 0) + return (error); + + /* read hour format (12/24 hour mode) */ + error = iicdev_readfrom(dev, PCF85063_CTRL1_REG, &control_reg, + sizeof(uint8_t), IIC_WAIT); + if (error != 0) + return (error); + + /* read current date and time */ + error = iicdev_readfrom(dev, PCF85063_TIME_REG, &data, + sizeof(struct pcf85063_time), IIC_WAIT); + if (error != 0) + return (error); + + bcd.nsec = 0; + bcd.sec = data.sec & 0x7F; + bcd.min = data.min & 0x7F; + bcd.ispm = data.hour & 0x20; + + if (control_reg & PCF85063_CTRL1_TIME_FORMAT) + /* 12 hour mode */ + bcd.hour = data.hour & 0x1F; + else + /* 24 hour mode */ + bcd.hour = data.hour & 0x3F; + + bcd.dow = (data.dow & 0x7) + 1; + bcd.day = data.day & 0x3F; + bcd.mon = data.mon & 0x1F; + bcd.year = data.year; + + clock_dbgprint_bcd(dev, CLOCK_DBG_READ, &bcd); + error = clock_bcd_to_ts(&bcd, ts, true); + + return (error); +} + +static int +pcf85063_set_time(device_t dev, struct timespec *ts) +{ + uint8_t time_reg, ctrl_reg; + struct pcf85063_time data; + struct bcd_clocktime bcd; + int error; + + ts->tv_sec -= utc_offset(); + clock_ts_to_bcd(ts, &bcd, true); + clock_dbgprint_bcd(dev, CLOCK_DBG_WRITE, &bcd); + + data.sec = bcd.sec; + data.min = bcd.min; + data.hour = bcd.hour; + data.dow = bcd.dow - 1; + data.day = bcd.day; + data.mon = bcd.mon; + data.year = bcd.year; + + if (ts->tv_nsec > PCF85063_HALF_OF_SEC_NS) + data.sec++; + + /* disable clock */ + error = iicdev_readfrom(dev, PCF85063_CTRL1_REG, &ctrl_reg, + sizeof(uint8_t), IIC_WAIT); + if (error != 0) + return (error); + + ctrl_reg |= PCF85063_CTRL1_RTC_CLK_STOP; + + error = iicdev_writeto(dev, PCF85063_CTRL1_REG, &ctrl_reg, + sizeof(uint8_t), IIC_WAIT); + if (error != 0) + return (error); + + /* clock is disabled now, write time and date */ + error = iicdev_writeto(dev, PCF85063_TIME_REG, &data, + sizeof(struct pcf85063_time), IIC_WAIT); + if (error != 0) + return (error); + + /* restart clock */ + ctrl_reg &= ~PCF85063_CTRL1_RTC_CLK_STOP; + error = iicdev_writeto(dev, PCF85063_CTRL1_REG, &ctrl_reg, + sizeof(uint8_t), IIC_WAIT); + if (error != 0) + return (error); + + /* reset low voltage flag */ + error = iicdev_readfrom(dev, PCF85063_TIME_REG, &time_reg, + sizeof(uint8_t), IIC_WAIT); + if (error != 0) + return (error); + + time_reg &= ~PCF85063_TIME_REG_OSC_STOP; + + error = iicdev_writeto(dev, PCF85063_TIME_REG, &time_reg, + sizeof(uint8_t), IIC_WAIT); + + return (error); +} + +static int +pcf85063_probe(device_t dev) +{ + + if (!ofw_bus_status_okay(dev)) + return (ENXIO); + + if (!ofw_bus_search_compatible(dev, pcf85063_compat_data)->ocd_data) + return (ENXIO); + + device_set_desc(dev, "NXP pcf85063 Real Time Clock"); + + return (BUS_PROBE_GENERIC); +} + +static device_method_t pcf85063_methods [] = { + DEVMETHOD(device_attach, pcf85063_attach), + DEVMETHOD(device_detach, pcf85063_detach), + DEVMETHOD(device_probe, pcf85063_probe), + + DEVMETHOD(clock_gettime, pcf85063_get_time), + DEVMETHOD(clock_settime, pcf85063_set_time), + + DEVMETHOD_END +}; + +static driver_t pcf85063_driver = { + "pcf85063", + pcf85063_methods, + 0 +}; + +static devclass_t pcf85063_devclass; + +DRIVER_MODULE(pcf85063, iicbus, pcf85063_driver, pcf85063_devclass, NULL, NULL); +IICBUS_FDT_PNP_INFO(pcf85063_compat_data); diff --git a/sys/modules/i2c/Makefile b/sys/modules/i2c/Makefile index 9fea714975f7..31c348a63dbc 100644 --- a/sys/modules/i2c/Makefile +++ b/sys/modules/i2c/Makefile @@ -29,8 +29,8 @@ SUBDIR = \ smbus \ .if !empty(OPT_FDT) -SUBDIR += tca6416 \ - rx8803 +SUBDIR += rx8803 \ + tca6416 .endif .if ${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "amd64" || \