1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2021 Alstom Group. 5 * Copyright (c) 2021 Semihalf. 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 ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include "opt_platform.h" 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/bus.h> 36 #include <sys/clock.h> 37 #include <sys/kernel.h> 38 #include <sys/lock.h> 39 #include <sys/module.h> 40 41 #include <dev/ofw/ofw_bus.h> 42 #include <dev/ofw/ofw_bus_subr.h> 43 44 #include <dev/iicbus/iiconf.h> 45 #include <dev/iicbus/iicbus.h> 46 47 #include "clock_if.h" 48 #include "iicbus_if.h" 49 50 #define BIT(x) (1 << (x)) 51 52 #define PCF85063_CTRL1_REG 0x0 53 #define PCF85063_TIME_REG 0x4 54 55 #define PCF85063_CTRL1_TIME_FORMAT BIT(1) 56 #define PCF85063_CTRL1_RTC_CLK_STOP BIT(5) 57 #define PCF85063_TIME_REG_OSC_STOP BIT(7) 58 59 #define PCF85063_HALF_OF_SEC_NS 500000000 60 61 struct pcf85063_time { 62 uint8_t sec; 63 uint8_t min; 64 uint8_t hour; 65 uint8_t day; 66 uint8_t dow; 67 uint8_t mon; 68 uint8_t year; 69 }; 70 71 static int pcf85063_attach(device_t dev); 72 static int pcf85063_detach(device_t dev); 73 static int pcf85063_probe(device_t dev); 74 75 static int pcf85063_get_time(device_t dev, struct timespec *ts); 76 static int pcf85063_set_time(device_t dev, struct timespec *ts); 77 78 static int pcf85063_check_status(device_t dev); 79 80 static struct ofw_compat_data pcf85063_compat_data[] = { 81 { "nxp,pcf85063", 1}, 82 { NULL, 0} 83 }; 84 85 static int 86 pcf85063_check_status(device_t dev) 87 { 88 uint8_t flags; 89 int error; 90 91 error = iicdev_readfrom(dev, PCF85063_TIME_REG, &flags, 1, IIC_WAIT); 92 if (error != 0) 93 return (error); 94 95 if (flags & PCF85063_TIME_REG_OSC_STOP) { 96 device_printf(dev, 97 "Low voltage flag set, date is incorrect.\n"); 98 return (ENXIO); 99 } 100 101 return (0); 102 } 103 104 static int 105 pcf85063_attach(device_t dev) 106 { 107 108 clock_register_flags(dev, 1000000, 0); 109 clock_schedule(dev, 1); 110 111 return (0); 112 } 113 114 static int 115 pcf85063_detach(device_t dev) 116 { 117 118 clock_unregister(dev); 119 120 return (0); 121 } 122 123 static int 124 pcf85063_get_time(device_t dev, struct timespec *ts) 125 { 126 struct pcf85063_time data; 127 struct bcd_clocktime bcd; 128 uint8_t control_reg; 129 int error; 130 131 error = pcf85063_check_status(dev); 132 if (error != 0) 133 return (error); 134 135 /* read hour format (12/24 hour mode) */ 136 error = iicdev_readfrom(dev, PCF85063_CTRL1_REG, &control_reg, 137 sizeof(uint8_t), IIC_WAIT); 138 if (error != 0) 139 return (error); 140 141 /* read current date and time */ 142 error = iicdev_readfrom(dev, PCF85063_TIME_REG, &data, 143 sizeof(struct pcf85063_time), IIC_WAIT); 144 if (error != 0) 145 return (error); 146 147 bcd.nsec = 0; 148 bcd.sec = data.sec & 0x7F; 149 bcd.min = data.min & 0x7F; 150 bcd.ispm = data.hour & 0x20; 151 152 if (control_reg & PCF85063_CTRL1_TIME_FORMAT) 153 /* 12 hour mode */ 154 bcd.hour = data.hour & 0x1F; 155 else 156 /* 24 hour mode */ 157 bcd.hour = data.hour & 0x3F; 158 159 bcd.dow = (data.dow & 0x7) + 1; 160 bcd.day = data.day & 0x3F; 161 bcd.mon = data.mon & 0x1F; 162 bcd.year = data.year; 163 164 clock_dbgprint_bcd(dev, CLOCK_DBG_READ, &bcd); 165 error = clock_bcd_to_ts(&bcd, ts, true); 166 167 return (error); 168 } 169 170 static int 171 pcf85063_set_time(device_t dev, struct timespec *ts) 172 { 173 uint8_t time_reg, ctrl_reg; 174 struct pcf85063_time data; 175 struct bcd_clocktime bcd; 176 int error; 177 178 ts->tv_sec -= utc_offset(); 179 clock_ts_to_bcd(ts, &bcd, true); 180 clock_dbgprint_bcd(dev, CLOCK_DBG_WRITE, &bcd); 181 182 data.sec = bcd.sec; 183 data.min = bcd.min; 184 data.hour = bcd.hour; 185 data.dow = bcd.dow - 1; 186 data.day = bcd.day; 187 data.mon = bcd.mon; 188 data.year = bcd.year; 189 190 if (ts->tv_nsec > PCF85063_HALF_OF_SEC_NS) 191 data.sec++; 192 193 /* disable clock */ 194 error = iicdev_readfrom(dev, PCF85063_CTRL1_REG, &ctrl_reg, 195 sizeof(uint8_t), IIC_WAIT); 196 if (error != 0) 197 return (error); 198 199 ctrl_reg |= PCF85063_CTRL1_RTC_CLK_STOP; 200 201 error = iicdev_writeto(dev, PCF85063_CTRL1_REG, &ctrl_reg, 202 sizeof(uint8_t), IIC_WAIT); 203 if (error != 0) 204 return (error); 205 206 /* clock is disabled now, write time and date */ 207 error = iicdev_writeto(dev, PCF85063_TIME_REG, &data, 208 sizeof(struct pcf85063_time), IIC_WAIT); 209 if (error != 0) 210 return (error); 211 212 /* restart clock */ 213 ctrl_reg &= ~PCF85063_CTRL1_RTC_CLK_STOP; 214 error = iicdev_writeto(dev, PCF85063_CTRL1_REG, &ctrl_reg, 215 sizeof(uint8_t), IIC_WAIT); 216 if (error != 0) 217 return (error); 218 219 /* reset low voltage flag */ 220 error = iicdev_readfrom(dev, PCF85063_TIME_REG, &time_reg, 221 sizeof(uint8_t), IIC_WAIT); 222 if (error != 0) 223 return (error); 224 225 time_reg &= ~PCF85063_TIME_REG_OSC_STOP; 226 227 error = iicdev_writeto(dev, PCF85063_TIME_REG, &time_reg, 228 sizeof(uint8_t), IIC_WAIT); 229 230 return (error); 231 } 232 233 static int 234 pcf85063_probe(device_t dev) 235 { 236 237 if (!ofw_bus_status_okay(dev)) 238 return (ENXIO); 239 240 if (!ofw_bus_search_compatible(dev, pcf85063_compat_data)->ocd_data) 241 return (ENXIO); 242 243 device_set_desc(dev, "NXP pcf85063 Real Time Clock"); 244 245 return (BUS_PROBE_GENERIC); 246 } 247 248 static device_method_t pcf85063_methods [] = { 249 DEVMETHOD(device_attach, pcf85063_attach), 250 DEVMETHOD(device_detach, pcf85063_detach), 251 DEVMETHOD(device_probe, pcf85063_probe), 252 253 DEVMETHOD(clock_gettime, pcf85063_get_time), 254 DEVMETHOD(clock_settime, pcf85063_set_time), 255 256 DEVMETHOD_END 257 }; 258 259 static driver_t pcf85063_driver = { 260 "pcf85063", 261 pcf85063_methods, 262 0 263 }; 264 265 static devclass_t pcf85063_devclass; 266 267 DRIVER_MODULE(pcf85063, iicbus, pcf85063_driver, pcf85063_devclass, NULL, NULL); 268 IICBUS_FDT_PNP_INFO(pcf85063_compat_data); 269