1 /* 2 * A RTC driver for the Simtek STK17TA8 3 * 4 * By Thomas Hommel <thomas.hommel@ge.com> 5 * 6 * Based on the DS1553 driver from 7 * Atsushi Nemoto <anemo@mba.ocn.ne.jp> 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License version 2 as 11 * published by the Free Software Foundation. 12 */ 13 14 #include <linux/bcd.h> 15 #include <linux/init.h> 16 #include <linux/kernel.h> 17 #include <linux/gfp.h> 18 #include <linux/delay.h> 19 #include <linux/jiffies.h> 20 #include <linux/interrupt.h> 21 #include <linux/rtc.h> 22 #include <linux/platform_device.h> 23 #include <linux/io.h> 24 #include <linux/module.h> 25 26 #define RTC_REG_SIZE 0x20000 27 #define RTC_OFFSET 0x1fff0 28 29 #define RTC_FLAGS (RTC_OFFSET + 0) 30 #define RTC_CENTURY (RTC_OFFSET + 1) 31 #define RTC_SECONDS_ALARM (RTC_OFFSET + 2) 32 #define RTC_MINUTES_ALARM (RTC_OFFSET + 3) 33 #define RTC_HOURS_ALARM (RTC_OFFSET + 4) 34 #define RTC_DATE_ALARM (RTC_OFFSET + 5) 35 #define RTC_INTERRUPTS (RTC_OFFSET + 6) 36 #define RTC_WATCHDOG (RTC_OFFSET + 7) 37 #define RTC_CALIBRATION (RTC_OFFSET + 8) 38 #define RTC_SECONDS (RTC_OFFSET + 9) 39 #define RTC_MINUTES (RTC_OFFSET + 10) 40 #define RTC_HOURS (RTC_OFFSET + 11) 41 #define RTC_DAY (RTC_OFFSET + 12) 42 #define RTC_DATE (RTC_OFFSET + 13) 43 #define RTC_MONTH (RTC_OFFSET + 14) 44 #define RTC_YEAR (RTC_OFFSET + 15) 45 46 #define RTC_SECONDS_MASK 0x7f 47 #define RTC_DAY_MASK 0x07 48 #define RTC_CAL_MASK 0x3f 49 50 /* Bits in the Calibration register */ 51 #define RTC_STOP 0x80 52 53 /* Bits in the Flags register */ 54 #define RTC_FLAGS_AF 0x40 55 #define RTC_FLAGS_PF 0x20 56 #define RTC_WRITE 0x02 57 #define RTC_READ 0x01 58 59 /* Bits in the Interrupts register */ 60 #define RTC_INTS_AIE 0x40 61 62 struct rtc_plat_data { 63 struct rtc_device *rtc; 64 void __iomem *ioaddr; 65 unsigned long last_jiffies; 66 int irq; 67 unsigned int irqen; 68 int alrm_sec; 69 int alrm_min; 70 int alrm_hour; 71 int alrm_mday; 72 spinlock_t lock; 73 }; 74 75 static int stk17ta8_rtc_set_time(struct device *dev, struct rtc_time *tm) 76 { 77 struct platform_device *pdev = to_platform_device(dev); 78 struct rtc_plat_data *pdata = platform_get_drvdata(pdev); 79 void __iomem *ioaddr = pdata->ioaddr; 80 u8 flags; 81 82 flags = readb(pdata->ioaddr + RTC_FLAGS); 83 writeb(flags | RTC_WRITE, pdata->ioaddr + RTC_FLAGS); 84 85 writeb(bin2bcd(tm->tm_year % 100), ioaddr + RTC_YEAR); 86 writeb(bin2bcd(tm->tm_mon + 1), ioaddr + RTC_MONTH); 87 writeb(bin2bcd(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY); 88 writeb(bin2bcd(tm->tm_mday), ioaddr + RTC_DATE); 89 writeb(bin2bcd(tm->tm_hour), ioaddr + RTC_HOURS); 90 writeb(bin2bcd(tm->tm_min), ioaddr + RTC_MINUTES); 91 writeb(bin2bcd(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS); 92 writeb(bin2bcd((tm->tm_year + 1900) / 100), ioaddr + RTC_CENTURY); 93 94 writeb(flags & ~RTC_WRITE, pdata->ioaddr + RTC_FLAGS); 95 return 0; 96 } 97 98 static int stk17ta8_rtc_read_time(struct device *dev, struct rtc_time *tm) 99 { 100 struct platform_device *pdev = to_platform_device(dev); 101 struct rtc_plat_data *pdata = platform_get_drvdata(pdev); 102 void __iomem *ioaddr = pdata->ioaddr; 103 unsigned int year, month, day, hour, minute, second, week; 104 unsigned int century; 105 u8 flags; 106 107 /* give enough time to update RTC in case of continuous read */ 108 if (pdata->last_jiffies == jiffies) 109 msleep(1); 110 pdata->last_jiffies = jiffies; 111 112 flags = readb(pdata->ioaddr + RTC_FLAGS); 113 writeb(flags | RTC_READ, ioaddr + RTC_FLAGS); 114 second = readb(ioaddr + RTC_SECONDS) & RTC_SECONDS_MASK; 115 minute = readb(ioaddr + RTC_MINUTES); 116 hour = readb(ioaddr + RTC_HOURS); 117 day = readb(ioaddr + RTC_DATE); 118 week = readb(ioaddr + RTC_DAY) & RTC_DAY_MASK; 119 month = readb(ioaddr + RTC_MONTH); 120 year = readb(ioaddr + RTC_YEAR); 121 century = readb(ioaddr + RTC_CENTURY); 122 writeb(flags & ~RTC_READ, ioaddr + RTC_FLAGS); 123 tm->tm_sec = bcd2bin(second); 124 tm->tm_min = bcd2bin(minute); 125 tm->tm_hour = bcd2bin(hour); 126 tm->tm_mday = bcd2bin(day); 127 tm->tm_wday = bcd2bin(week); 128 tm->tm_mon = bcd2bin(month) - 1; 129 /* year is 1900 + tm->tm_year */ 130 tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900; 131 132 return 0; 133 } 134 135 static void stk17ta8_rtc_update_alarm(struct rtc_plat_data *pdata) 136 { 137 void __iomem *ioaddr = pdata->ioaddr; 138 unsigned long irqflags; 139 u8 flags; 140 141 spin_lock_irqsave(&pdata->lock, irqflags); 142 143 flags = readb(ioaddr + RTC_FLAGS); 144 writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS); 145 146 writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ? 147 0x80 : bin2bcd(pdata->alrm_mday), 148 ioaddr + RTC_DATE_ALARM); 149 writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ? 150 0x80 : bin2bcd(pdata->alrm_hour), 151 ioaddr + RTC_HOURS_ALARM); 152 writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ? 153 0x80 : bin2bcd(pdata->alrm_min), 154 ioaddr + RTC_MINUTES_ALARM); 155 writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ? 156 0x80 : bin2bcd(pdata->alrm_sec), 157 ioaddr + RTC_SECONDS_ALARM); 158 writeb(pdata->irqen ? RTC_INTS_AIE : 0, ioaddr + RTC_INTERRUPTS); 159 readb(ioaddr + RTC_FLAGS); /* clear interrupts */ 160 writeb(flags & ~RTC_WRITE, ioaddr + RTC_FLAGS); 161 spin_unlock_irqrestore(&pdata->lock, irqflags); 162 } 163 164 static int stk17ta8_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 165 { 166 struct platform_device *pdev = to_platform_device(dev); 167 struct rtc_plat_data *pdata = platform_get_drvdata(pdev); 168 169 if (pdata->irq <= 0) 170 return -EINVAL; 171 pdata->alrm_mday = alrm->time.tm_mday; 172 pdata->alrm_hour = alrm->time.tm_hour; 173 pdata->alrm_min = alrm->time.tm_min; 174 pdata->alrm_sec = alrm->time.tm_sec; 175 if (alrm->enabled) 176 pdata->irqen |= RTC_AF; 177 stk17ta8_rtc_update_alarm(pdata); 178 return 0; 179 } 180 181 static int stk17ta8_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) 182 { 183 struct platform_device *pdev = to_platform_device(dev); 184 struct rtc_plat_data *pdata = platform_get_drvdata(pdev); 185 186 if (pdata->irq <= 0) 187 return -EINVAL; 188 alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday; 189 alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour; 190 alrm->time.tm_min = pdata->alrm_min < 0 ? 0 : pdata->alrm_min; 191 alrm->time.tm_sec = pdata->alrm_sec < 0 ? 0 : pdata->alrm_sec; 192 alrm->enabled = (pdata->irqen & RTC_AF) ? 1 : 0; 193 return 0; 194 } 195 196 static irqreturn_t stk17ta8_rtc_interrupt(int irq, void *dev_id) 197 { 198 struct platform_device *pdev = dev_id; 199 struct rtc_plat_data *pdata = platform_get_drvdata(pdev); 200 void __iomem *ioaddr = pdata->ioaddr; 201 unsigned long events = 0; 202 203 spin_lock(&pdata->lock); 204 /* read and clear interrupt */ 205 if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF) { 206 events = RTC_IRQF; 207 if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80) 208 events |= RTC_UF; 209 else 210 events |= RTC_AF; 211 rtc_update_irq(pdata->rtc, 1, events); 212 } 213 spin_unlock(&pdata->lock); 214 return events ? IRQ_HANDLED : IRQ_NONE; 215 } 216 217 static int stk17ta8_rtc_alarm_irq_enable(struct device *dev, 218 unsigned int enabled) 219 { 220 struct platform_device *pdev = to_platform_device(dev); 221 struct rtc_plat_data *pdata = platform_get_drvdata(pdev); 222 223 if (pdata->irq <= 0) 224 return -EINVAL; 225 if (enabled) 226 pdata->irqen |= RTC_AF; 227 else 228 pdata->irqen &= ~RTC_AF; 229 stk17ta8_rtc_update_alarm(pdata); 230 return 0; 231 } 232 233 static const struct rtc_class_ops stk17ta8_rtc_ops = { 234 .read_time = stk17ta8_rtc_read_time, 235 .set_time = stk17ta8_rtc_set_time, 236 .read_alarm = stk17ta8_rtc_read_alarm, 237 .set_alarm = stk17ta8_rtc_set_alarm, 238 .alarm_irq_enable = stk17ta8_rtc_alarm_irq_enable, 239 }; 240 241 static int stk17ta8_nvram_read(void *priv, unsigned int pos, void *val, 242 size_t bytes) 243 { 244 struct rtc_plat_data *pdata = priv; 245 void __iomem *ioaddr = pdata->ioaddr; 246 u8 *buf = val; 247 248 for (; bytes; bytes--) 249 *buf++ = readb(ioaddr + pos++); 250 return 0; 251 } 252 253 static int stk17ta8_nvram_write(void *priv, unsigned int pos, void *val, 254 size_t bytes) 255 { 256 struct rtc_plat_data *pdata = priv; 257 void __iomem *ioaddr = pdata->ioaddr; 258 u8 *buf = val; 259 260 for (; bytes; bytes--) 261 writeb(*buf++, ioaddr + pos++); 262 return 0; 263 } 264 265 static int stk17ta8_rtc_probe(struct platform_device *pdev) 266 { 267 struct resource *res; 268 unsigned int cal; 269 unsigned int flags; 270 struct rtc_plat_data *pdata; 271 void __iomem *ioaddr; 272 int ret = 0; 273 struct nvmem_config nvmem_cfg = { 274 .name = "stk17ta8_nvram", 275 .word_size = 1, 276 .stride = 1, 277 .size = RTC_OFFSET, 278 .reg_read = stk17ta8_nvram_read, 279 .reg_write = stk17ta8_nvram_write, 280 }; 281 282 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 283 if (!pdata) 284 return -ENOMEM; 285 286 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 287 ioaddr = devm_ioremap_resource(&pdev->dev, res); 288 if (IS_ERR(ioaddr)) 289 return PTR_ERR(ioaddr); 290 pdata->ioaddr = ioaddr; 291 pdata->irq = platform_get_irq(pdev, 0); 292 293 /* turn RTC on if it was not on */ 294 cal = readb(ioaddr + RTC_CALIBRATION); 295 if (cal & RTC_STOP) { 296 cal &= RTC_CAL_MASK; 297 flags = readb(ioaddr + RTC_FLAGS); 298 writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS); 299 writeb(cal, ioaddr + RTC_CALIBRATION); 300 writeb(flags & ~RTC_WRITE, ioaddr + RTC_FLAGS); 301 } 302 if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_PF) 303 dev_warn(&pdev->dev, "voltage-low detected.\n"); 304 305 spin_lock_init(&pdata->lock); 306 pdata->last_jiffies = jiffies; 307 platform_set_drvdata(pdev, pdata); 308 if (pdata->irq > 0) { 309 writeb(0, ioaddr + RTC_INTERRUPTS); 310 if (devm_request_irq(&pdev->dev, pdata->irq, 311 stk17ta8_rtc_interrupt, 312 IRQF_SHARED, 313 pdev->name, pdev) < 0) { 314 dev_warn(&pdev->dev, "interrupt not available.\n"); 315 pdata->irq = 0; 316 } 317 } 318 319 pdata->rtc = devm_rtc_allocate_device(&pdev->dev); 320 if (IS_ERR(pdata->rtc)) 321 return PTR_ERR(pdata->rtc); 322 323 pdata->rtc->ops = &stk17ta8_rtc_ops; 324 pdata->rtc->nvram_old_abi = true; 325 326 nvmem_cfg.priv = pdata; 327 ret = rtc_nvmem_register(pdata->rtc, &nvmem_cfg); 328 if (ret) 329 return ret; 330 331 return rtc_register_device(pdata->rtc); 332 } 333 334 /* work with hotplug and coldplug */ 335 MODULE_ALIAS("platform:stk17ta8"); 336 337 static struct platform_driver stk17ta8_rtc_driver = { 338 .probe = stk17ta8_rtc_probe, 339 .driver = { 340 .name = "stk17ta8", 341 }, 342 }; 343 344 module_platform_driver(stk17ta8_rtc_driver); 345 346 MODULE_AUTHOR("Thomas Hommel <thomas.hommel@ge.com>"); 347 MODULE_DESCRIPTION("Simtek STK17TA8 RTC driver"); 348 MODULE_LICENSE("GPL"); 349