"Das U-Boot" Source Tree

rtc: add support for DS3232 device

DS3232 is an i2c RTC with 236 bytes of battery-backed SRAM.

Add an RTC driver for DS3232 device, which provides time and
date support. Also read and write functions are provided,
which can be used to access the SRAM memory.

Signed-off-by: Nandor Han <nandor.han@vaisala.com>

authored by

Han Nandor and committed by
Tom Rini
0b326fc2 c82abaa5

+298
+15
doc/device-tree-bindings/rtc/ds3232.txt
··· 1 + DS3232 Real-Time Clock with SRAM 2 + 3 + The RTC driver provides time and date functionality. Also read and write 4 + functions are provided that can be used to access the SRAM memory. 5 + 6 + Required properties: 7 + - compatible : should contain "dallas,ds3232" 8 + - reg : the I2C RTC address 9 + 10 + Example: 11 + 12 + rtc@68 { 13 + compatible = "dallas,ds3232"; 14 + reg = <0x68>; 15 + };
+8
drivers/rtc/Kconfig
··· 55 55 Support for Dallas Semiconductor (now Maxim) DS1307 and DS1338/9 and 56 56 compatible Real Time Clock devices. 57 57 58 + config RTC_DS3232 59 + bool "Enable DS3232 driver" 60 + depends on DM_RTC 61 + depends on DM_I2C 62 + help 63 + Support for Dallas Semiconductor (now Maxim) DS3232 compatible 64 + Real Time Clock devices. 65 + 58 66 config RTC_ISL1208 59 67 bool "Enable ISL1208 driver" 60 68 depends on DM_RTC
+1
drivers/rtc/Makefile
··· 21 21 obj-$(CONFIG_RTC_DS164x) += ds164x.o 22 22 obj-$(CONFIG_RTC_DS174x) += ds174x.o 23 23 obj-$(CONFIG_RTC_DS3231) += ds3231.o 24 + obj-$(CONFIG_RTC_DS3232) += ds3232.o 24 25 obj-$(CONFIG_RTC_FTRTC010) += ftrtc010.o 25 26 obj-$(CONFIG_SANDBOX) += i2c_rtc_emul.o 26 27 obj-$(CONFIG_RTC_IMXDI) += imxdi.o
+274
drivers/rtc/ds3232.c
··· 1 + // SPDX-License-Identifier: GPL-2.0+ 2 + /* 3 + * (C) Copyright 2019, Vaisala Oyj 4 + */ 5 + 6 + #include <common.h> 7 + #include <command.h> 8 + #include <dm.h> 9 + #include <i2c.h> 10 + #include <rtc.h> 11 + 12 + /* 13 + * RTC register addresses 14 + */ 15 + #define RTC_SEC_REG_ADDR 0x00 16 + #define RTC_MIN_REG_ADDR 0x01 17 + #define RTC_HR_REG_ADDR 0x02 18 + #define RTC_DAY_REG_ADDR 0x03 19 + #define RTC_DATE_REG_ADDR 0x04 20 + #define RTC_MON_REG_ADDR 0x05 21 + #define RTC_YR_REG_ADDR 0x06 22 + #define RTC_CTL_REG_ADDR 0x0e 23 + #define RTC_STAT_REG_ADDR 0x0f 24 + #define RTC_TEST_REG_ADDR 0x13 25 + 26 + /* 27 + * RTC control register bits 28 + */ 29 + #define RTC_CTL_BIT_A1IE BIT(0) /* Alarm 1 interrupt enable */ 30 + #define RTC_CTL_BIT_A2IE BIT(1) /* Alarm 2 interrupt enable */ 31 + #define RTC_CTL_BIT_INTCN BIT(2) /* Interrupt control */ 32 + #define RTC_CTL_BIT_DOSC BIT(7) /* Disable Oscillator */ 33 + 34 + /* 35 + * RTC status register bits 36 + */ 37 + #define RTC_STAT_BIT_A1F BIT(0) /* Alarm 1 flag */ 38 + #define RTC_STAT_BIT_A2F BIT(1) /* Alarm 2 flag */ 39 + #define RTC_STAT_BIT_EN32KHZ BIT(3) /* Enable 32KHz Output */ 40 + #define RTC_STAT_BIT_BB32KHZ BIT(6) /* Battery backed 32KHz Output */ 41 + #define RTC_STAT_BIT_OSF BIT(7) /* Oscillator stop flag */ 42 + 43 + /* 44 + * RTC test register bits 45 + */ 46 + #define RTC_TEST_BIT_SWRST BIT(7) /* Software reset */ 47 + 48 + #define RTC_DATE_TIME_REG_SIZE 7 49 + #define RTC_SRAM_START 0x14 50 + #define RTC_SRAM_END 0xFF 51 + #define RTC_SRAM_SIZE 236 52 + 53 + struct ds3232_priv_data { 54 + u8 max_register; 55 + u8 sram_start; 56 + int sram_size; 57 + }; 58 + 59 + static int ds3232_rtc_read8(struct udevice *dev, unsigned int reg) 60 + { 61 + int ret; 62 + u8 buf; 63 + struct ds3232_priv_data *priv_data; 64 + 65 + priv_data = dev_get_priv(dev); 66 + if (!priv_data) 67 + return -EINVAL; 68 + 69 + if (reg > priv_data->max_register) 70 + return -EINVAL; 71 + 72 + ret = dm_i2c_read(dev, reg, &buf, sizeof(buf)); 73 + if (ret < 0) 74 + return ret; 75 + 76 + return buf; 77 + } 78 + 79 + static int ds3232_rtc_write8(struct udevice *dev, unsigned int reg, int val) 80 + { 81 + u8 buf = (u8)val; 82 + struct ds3232_priv_data *priv_data; 83 + 84 + priv_data = dev_get_priv(dev); 85 + if (!priv_data) 86 + return -EINVAL; 87 + 88 + if (reg > priv_data->max_register) 89 + return -EINVAL; 90 + 91 + return dm_i2c_write(dev, reg, &buf, sizeof(buf)); 92 + } 93 + 94 + static int reset_sram(struct udevice *dev) 95 + { 96 + int ret, sram_end, reg; 97 + struct ds3232_priv_data *priv_data; 98 + 99 + priv_data = dev_get_priv(dev); 100 + if (!priv_data) 101 + return -EINVAL; 102 + 103 + sram_end = priv_data->sram_start + priv_data->sram_size; 104 + 105 + for (reg = priv_data->sram_start; reg < sram_end; reg++) { 106 + ret = ds3232_rtc_write8(dev, reg, 0x00); 107 + if (ret < 0) 108 + return ret; 109 + } 110 + 111 + return 0; 112 + } 113 + 114 + static int verify_osc(struct udevice *dev) 115 + { 116 + int ret, rtc_status; 117 + 118 + ret = ds3232_rtc_read8(dev, RTC_STAT_REG_ADDR); 119 + if (ret < 0) 120 + return ret; 121 + 122 + rtc_status = ret; 123 + 124 + if (rtc_status & RTC_STAT_BIT_OSF) { 125 + dev_warn(dev, 126 + "oscillator discontinuity flagged, time unreliable\n"); 127 + /* 128 + * In case OSC was off we cannot trust the SRAM data anymore. 129 + * Reset it to 0x00. 130 + */ 131 + ret = reset_sram(dev); 132 + if (ret < 0) 133 + return ret; 134 + } 135 + 136 + return 0; 137 + } 138 + 139 + static int ds3232_rtc_set(struct udevice *dev, const struct rtc_time *tm) 140 + { 141 + u8 buf[RTC_DATE_TIME_REG_SIZE]; 142 + u8 is_century; 143 + 144 + if (tm->tm_year < 1900 || tm->tm_year > 2099) 145 + dev_warn(dev, "WARNING: year should be between 1900 and 2099!\n"); 146 + 147 + is_century = (tm->tm_year >= 2000) ? 0x80 : 0; 148 + 149 + buf[RTC_SEC_REG_ADDR] = bin2bcd(tm->tm_sec); 150 + buf[RTC_MIN_REG_ADDR] = bin2bcd(tm->tm_min); 151 + buf[RTC_HR_REG_ADDR] = bin2bcd(tm->tm_hour); 152 + buf[RTC_DAY_REG_ADDR] = bin2bcd(tm->tm_wday + 1); 153 + buf[RTC_DATE_REG_ADDR] = bin2bcd(tm->tm_mday); 154 + buf[RTC_MON_REG_ADDR] = bin2bcd(tm->tm_mon) | is_century; 155 + buf[RTC_YR_REG_ADDR] = bin2bcd(tm->tm_year % 100); 156 + 157 + return dm_i2c_write(dev, 0, buf, sizeof(buf)); 158 + } 159 + 160 + static int ds3232_rtc_get(struct udevice *dev, struct rtc_time *tm) 161 + { 162 + int ret; 163 + u8 buf[RTC_DATE_TIME_REG_SIZE]; 164 + u8 is_twelve_hr; 165 + u8 is_pm; 166 + u8 is_century; 167 + 168 + ret = verify_osc(dev); 169 + if (ret < 0) 170 + return ret; 171 + 172 + ret = dm_i2c_read(dev, 0, buf, sizeof(buf)); 173 + if (ret < 0) 174 + return ret; 175 + 176 + /* Extract additional information for AM/PM and century */ 177 + is_twelve_hr = buf[RTC_HR_REG_ADDR] & 0x40; 178 + is_pm = buf[RTC_HR_REG_ADDR] & 0x20; 179 + is_century = buf[RTC_MON_REG_ADDR] & 0x80; 180 + 181 + tm->tm_sec = bcd2bin(buf[RTC_SEC_REG_ADDR] & 0x7F); 182 + tm->tm_min = bcd2bin(buf[RTC_MIN_REG_ADDR] & 0x7F); 183 + 184 + if (is_twelve_hr) 185 + tm->tm_hour = bcd2bin(buf[RTC_HR_REG_ADDR] & 0x1F) 186 + + (is_pm ? 12 : 0); 187 + else 188 + tm->tm_hour = bcd2bin(buf[RTC_HR_REG_ADDR]); 189 + 190 + tm->tm_wday = bcd2bin((buf[RTC_DAY_REG_ADDR] & 0x07) - 1); 191 + tm->tm_mday = bcd2bin(buf[RTC_DATE_REG_ADDR] & 0x3F); 192 + tm->tm_mon = bcd2bin((buf[RTC_MON_REG_ADDR] & 0x7F)); 193 + tm->tm_year = bcd2bin(buf[RTC_YR_REG_ADDR]) 194 + + (is_century ? 2000 : 1900); 195 + tm->tm_yday = 0; 196 + tm->tm_isdst = 0; 197 + 198 + return 0; 199 + } 200 + 201 + static int ds3232_rtc_reset(struct udevice *dev) 202 + { 203 + int ret; 204 + 205 + ret = reset_sram(dev); 206 + if (ret < 0) 207 + return ret; 208 + 209 + /* 210 + * From datasheet 211 + * (https://datasheets.maximintegrated.com/en/ds/DS3232M.pdf): 212 + * 213 + * The device reset occurs during the normal acknowledge time slot 214 + * following the receipt of the data byte carrying that 215 + * SWRST instruction a NACK occurs due to the resetting action. 216 + * 217 + * Therefore we don't verify the result of I2C write operation since it 218 + * will fail due the NACK. 219 + */ 220 + ds3232_rtc_write8(dev, RTC_TEST_REG_ADDR, RTC_TEST_BIT_SWRST); 221 + 222 + return 0; 223 + } 224 + 225 + static int ds3232_probe(struct udevice *dev) 226 + { 227 + int rtc_status; 228 + int ret; 229 + struct ds3232_priv_data *priv_data; 230 + 231 + priv_data = dev_get_priv(dev); 232 + if (!priv_data) 233 + return -EINVAL; 234 + 235 + priv_data->sram_start = RTC_SRAM_START; 236 + priv_data->max_register = RTC_SRAM_END; 237 + priv_data->sram_size = RTC_SRAM_SIZE; 238 + 239 + ret = ds3232_rtc_read8(dev, RTC_STAT_REG_ADDR); 240 + if (ret < 0) 241 + return ret; 242 + 243 + rtc_status = ret; 244 + 245 + ret = verify_osc(dev); 246 + if (ret < 0) 247 + return ret; 248 + 249 + rtc_status &= ~(RTC_STAT_BIT_OSF | RTC_STAT_BIT_A1F | RTC_STAT_BIT_A2F); 250 + 251 + return ds3232_rtc_write8(dev, RTC_STAT_REG_ADDR, rtc_status); 252 + } 253 + 254 + static const struct rtc_ops ds3232_rtc_ops = { 255 + .get = ds3232_rtc_get, 256 + .set = ds3232_rtc_set, 257 + .reset = ds3232_rtc_reset, 258 + .read8 = ds3232_rtc_read8, 259 + .write8 = ds3232_rtc_write8 260 + }; 261 + 262 + static const struct udevice_id ds3232_rtc_ids[] = { 263 + { .compatible = "dallas,ds3232" }, 264 + { } 265 + }; 266 + 267 + U_BOOT_DRIVER(rtc_ds3232) = { 268 + .name = "rtc-ds3232", 269 + .id = UCLASS_RTC, 270 + .probe = ds3232_probe, 271 + .of_match = ds3232_rtc_ids, 272 + .ops = &ds3232_rtc_ops, 273 + .priv_auto_alloc_size = sizeof(struct ds3232_priv_data), 274 + };