1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2021 Emmanuel Vadot <manu@FreeBSD.org> 5 * Copyright (c) 2021 Peter Jeremy <peterj@FreeBSD.org> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 #include <sys/param.h> 31 #include <sys/bus.h> 32 #include <sys/systm.h> 33 #include <sys/clock.h> 34 35 #include <dev/iicbus/pmic/rockchip/rk8xx.h> 36 37 int 38 rk8xx_gettime(device_t dev, struct timespec *ts) 39 { 40 struct rk8xx_softc *sc; 41 struct bcd_clocktime bct; 42 uint8_t data[7]; 43 uint8_t ctrl; 44 int error; 45 46 sc = device_get_softc(dev); 47 48 /* Latch the RTC value into the shadow registers and set 24hr mode */ 49 error = rk8xx_read(dev, sc->rtc_regs.ctrl, &ctrl, 1); 50 if (error != 0) 51 return (error); 52 53 ctrl |= sc->rtc_regs.ctrl_readsel_mask; 54 ctrl &= ~(sc->rtc_regs.ctrl_ampm_mask | sc->rtc_regs.ctrl_gettime_mask); 55 error = rk8xx_write(dev, sc->rtc_regs.ctrl, &ctrl, 1); 56 if (error != 0) 57 return (error); 58 ctrl |= sc->rtc_regs.ctrl_gettime_mask; 59 error = rk8xx_write(dev, sc->rtc_regs.ctrl, &ctrl, 1); 60 if (error != 0) 61 return (error); 62 if (sc->type == RK809 || sc->type == RK817) { 63 /* wait one 32khz cycle for clock shadow registers to latch */ 64 DELAY(1000000 / 32000); 65 } 66 ctrl &= ~sc->rtc_regs.ctrl_gettime_mask; 67 error = rk8xx_write(dev, sc->rtc_regs.ctrl, &ctrl, 1); 68 if (error != 0) 69 return (error); 70 71 /* This works as long as sc->rtc_regs.secs = 0 */ 72 error = rk8xx_read(dev, sc->rtc_regs.secs, data, 7); 73 if (error != 0) 74 return (error); 75 76 /* 77 * If the reported year is earlier than 2019, assume the clock is unset. 78 * This is both later than the reset value for the RK805 and RK808 as 79 * well as being prior to the current time. 80 */ 81 if (data[sc->rtc_regs.years] < 0x19) 82 return (EINVAL); 83 84 memset(&bct, 0, sizeof(bct)); 85 bct.year = data[sc->rtc_regs.years]; 86 bct.mon = data[sc->rtc_regs.months] & sc->rtc_regs.months_mask; 87 bct.day = data[sc->rtc_regs.days] & sc->rtc_regs.days_mask; 88 bct.hour = data[sc->rtc_regs.hours] & sc->rtc_regs.hours_mask; 89 bct.min = data[sc->rtc_regs.minutes] & sc->rtc_regs.minutes_mask; 90 bct.sec = data[sc->rtc_regs.secs] & sc->rtc_regs.secs_mask; 91 bct.dow = data[sc->rtc_regs.weeks] & sc->rtc_regs.weeks_mask; 92 /* The day of week is reported as 1-7 with 1 = Monday */ 93 if (bct.dow == 7) 94 bct.dow = 0; 95 bct.ispm = 0; 96 if (sc->type == RK809 || sc->type == RK817) 97 bct.year += 0x2000; /* valid for 2000-2099 only */ 98 99 if (bootverbose) 100 device_printf(dev, "Read RTC: %02x-%02x-%02x %02x:%02x:%02x\n", 101 bct.year, bct.mon, bct.day, bct.hour, bct.min, bct.sec); 102 103 return (clock_bcd_to_ts(&bct, ts, false)); 104 } 105 106 int 107 rk8xx_settime(device_t dev, struct timespec *ts) 108 { 109 struct rk8xx_softc *sc; 110 struct bcd_clocktime bct; 111 uint8_t data[7]; 112 int error; 113 uint8_t ctrl; 114 115 sc = device_get_softc(dev); 116 117 clock_ts_to_bcd(ts, &bct, false); 118 119 /* This works as long as RK805_RTC_SECS = 0 */ 120 if (sc->type == RK809 || sc->type == RK817) { 121 /* valid for 2000-2099 only */ 122 if ((bct.year & 0xff00) != 0x2000) { 123 device_printf(dev, "year out of range\n"); 124 return (EINVAL); 125 } 126 bct.year &= 0x00ff; 127 } 128 data[sc->rtc_regs.years] = bct.year; 129 data[sc->rtc_regs.months] = bct.mon; 130 data[sc->rtc_regs.days] = bct.day; 131 data[sc->rtc_regs.hours] = bct.hour; 132 data[sc->rtc_regs.minutes] = bct.min; 133 data[sc->rtc_regs.secs] = bct.sec; 134 data[sc->rtc_regs.weeks] = bct.dow; 135 /* The day of week is reported as 1-7 with 1 = Monday */ 136 if (data[sc->rtc_regs.weeks] == 0) 137 data[sc->rtc_regs.weeks] = 7; 138 139 error = rk8xx_read(dev, sc->rtc_regs.ctrl, &ctrl, 1); 140 if (error != 0) 141 return (error); 142 143 ctrl |= sc->rtc_regs.ctrl_stop_mask; 144 ctrl &= ~sc->rtc_regs.ctrl_ampm_mask; 145 error = rk8xx_write(dev, sc->rtc_regs.ctrl, &ctrl, 1); 146 if (error != 0) 147 return (error); 148 149 error = rk8xx_write(dev, sc->rtc_regs.secs, data, 7); 150 ctrl &= ~sc->rtc_regs.ctrl_stop_mask; 151 rk8xx_write(dev, sc->rtc_regs.ctrl, &ctrl, 1); 152 153 return (error); 154 } 155