diff --git a/sys/arm64/conf/std.nxp b/sys/arm64/conf/std.nxp --- a/sys/arm64/conf/std.nxp +++ b/sys/arm64/conf/std.nxp @@ -9,6 +9,7 @@ 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 --- a/sys/conf/files +++ b/sys/conf/files @@ -1866,6 +1866,7 @@ 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 --- /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 --- a/sys/modules/i2c/Makefile +++ b/sys/modules/i2c/Makefile @@ -29,8 +29,8 @@ smbus \ .if !empty(OPT_FDT) -SUBDIR += tca6416 \ - rx8803 +SUBDIR += rx8803 \ + tca6416 .endif .if ${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "amd64" || \