1 /* 2 * rtc-st-lpc.c - ST's LPC RTC, powered by the Low Power Timer 3 * 4 * Copyright (C) 2014 STMicroelectronics Limited 5 * 6 * Author: David Paris <david.paris@st.com> for STMicroelectronics 7 * Lee Jones <lee.jones@linaro.org> for STMicroelectronics 8 * 9 * Based on the original driver written by Stuart Menefy. 10 * 11 * This program is free software; you can redistribute it and/or 12 * modify it under the terms of the GNU General Public Licence 13 * as published by the Free Software Foundation; either version 14 * 2 of the Licence, or (at your option) any later version. 15 */ 16 17 #include <linux/clk.h> 18 #include <linux/delay.h> 19 #include <linux/init.h> 20 #include <linux/io.h> 21 #include <linux/irq.h> 22 #include <linux/kernel.h> 23 #include <linux/module.h> 24 #include <linux/of.h> 25 #include <linux/of_irq.h> 26 #include <linux/platform_device.h> 27 #include <linux/rtc.h> 28 29 #include <dt-bindings/mfd/st-lpc.h> 30 31 /* Low Power Timer */ 32 #define LPC_LPT_LSB_OFF 0x400 33 #define LPC_LPT_MSB_OFF 0x404 34 #define LPC_LPT_START_OFF 0x408 35 36 /* Low Power Alarm */ 37 #define LPC_LPA_LSB_OFF 0x410 38 #define LPC_LPA_MSB_OFF 0x414 39 #define LPC_LPA_START_OFF 0x418 40 41 /* LPC as WDT */ 42 #define LPC_WDT_OFF 0x510 43 #define LPC_WDT_FLAG_OFF 0x514 44 45 struct st_rtc { 46 struct rtc_device *rtc_dev; 47 struct rtc_wkalrm alarm; 48 struct resource *res; 49 struct clk *clk; 50 unsigned long clkrate; 51 void __iomem *ioaddr; 52 bool irq_enabled:1; 53 spinlock_t lock; 54 short irq; 55 }; 56 57 static void st_rtc_set_hw_alarm(struct st_rtc *rtc, 58 unsigned long msb, unsigned long lsb) 59 { 60 unsigned long flags; 61 62 spin_lock_irqsave(&rtc->lock, flags); 63 64 writel_relaxed(1, rtc->ioaddr + LPC_WDT_OFF); 65 66 writel_relaxed(msb, rtc->ioaddr + LPC_LPA_MSB_OFF); 67 writel_relaxed(lsb, rtc->ioaddr + LPC_LPA_LSB_OFF); 68 writel_relaxed(1, rtc->ioaddr + LPC_LPA_START_OFF); 69 70 writel_relaxed(0, rtc->ioaddr + LPC_WDT_OFF); 71 72 spin_unlock_irqrestore(&rtc->lock, flags); 73 } 74 75 static irqreturn_t st_rtc_handler(int this_irq, void *data) 76 { 77 struct st_rtc *rtc = (struct st_rtc *)data; 78 79 rtc_update_irq(rtc->rtc_dev, 1, RTC_AF); 80 81 return IRQ_HANDLED; 82 } 83 84 static int st_rtc_read_time(struct device *dev, struct rtc_time *tm) 85 { 86 struct st_rtc *rtc = dev_get_drvdata(dev); 87 unsigned long lpt_lsb, lpt_msb; 88 unsigned long long lpt; 89 unsigned long flags; 90 91 spin_lock_irqsave(&rtc->lock, flags); 92 93 do { 94 lpt_msb = readl_relaxed(rtc->ioaddr + LPC_LPT_MSB_OFF); 95 lpt_lsb = readl_relaxed(rtc->ioaddr + LPC_LPT_LSB_OFF); 96 } while (readl_relaxed(rtc->ioaddr + LPC_LPT_MSB_OFF) != lpt_msb); 97 98 spin_unlock_irqrestore(&rtc->lock, flags); 99 100 lpt = ((unsigned long long)lpt_msb << 32) | lpt_lsb; 101 do_div(lpt, rtc->clkrate); 102 rtc_time64_to_tm(lpt, tm); 103 104 return 0; 105 } 106 107 static int st_rtc_set_time(struct device *dev, struct rtc_time *tm) 108 { 109 struct st_rtc *rtc = dev_get_drvdata(dev); 110 unsigned long long lpt, secs; 111 unsigned long flags; 112 113 secs = rtc_tm_to_time64(tm); 114 115 lpt = (unsigned long long)secs * rtc->clkrate; 116 117 spin_lock_irqsave(&rtc->lock, flags); 118 119 writel_relaxed(lpt >> 32, rtc->ioaddr + LPC_LPT_MSB_OFF); 120 writel_relaxed(lpt, rtc->ioaddr + LPC_LPT_LSB_OFF); 121 writel_relaxed(1, rtc->ioaddr + LPC_LPT_START_OFF); 122 123 spin_unlock_irqrestore(&rtc->lock, flags); 124 125 return 0; 126 } 127 128 static int st_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) 129 { 130 struct st_rtc *rtc = dev_get_drvdata(dev); 131 unsigned long flags; 132 133 spin_lock_irqsave(&rtc->lock, flags); 134 135 memcpy(wkalrm, &rtc->alarm, sizeof(struct rtc_wkalrm)); 136 137 spin_unlock_irqrestore(&rtc->lock, flags); 138 139 return 0; 140 } 141 142 static int st_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) 143 { 144 struct st_rtc *rtc = dev_get_drvdata(dev); 145 146 if (enabled && !rtc->irq_enabled) { 147 enable_irq(rtc->irq); 148 rtc->irq_enabled = true; 149 } else if (!enabled && rtc->irq_enabled) { 150 disable_irq(rtc->irq); 151 rtc->irq_enabled = false; 152 } 153 154 return 0; 155 } 156 157 static int st_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *t) 158 { 159 struct st_rtc *rtc = dev_get_drvdata(dev); 160 struct rtc_time now; 161 unsigned long long now_secs; 162 unsigned long long alarm_secs; 163 unsigned long long lpa; 164 165 st_rtc_read_time(dev, &now); 166 now_secs = rtc_tm_to_time64(&now); 167 alarm_secs = rtc_tm_to_time64(&t->time); 168 169 /* Invalid alarm time */ 170 if (now_secs > alarm_secs) 171 return -EINVAL; 172 173 memcpy(&rtc->alarm, t, sizeof(struct rtc_wkalrm)); 174 175 /* Now many secs to fire */ 176 alarm_secs -= now_secs; 177 lpa = (unsigned long long)alarm_secs * rtc->clkrate; 178 179 st_rtc_set_hw_alarm(rtc, lpa >> 32, lpa); 180 st_rtc_alarm_irq_enable(dev, t->enabled); 181 182 return 0; 183 } 184 185 static struct rtc_class_ops st_rtc_ops = { 186 .read_time = st_rtc_read_time, 187 .set_time = st_rtc_set_time, 188 .read_alarm = st_rtc_read_alarm, 189 .set_alarm = st_rtc_set_alarm, 190 .alarm_irq_enable = st_rtc_alarm_irq_enable, 191 }; 192 193 static int st_rtc_probe(struct platform_device *pdev) 194 { 195 struct device_node *np = pdev->dev.of_node; 196 struct st_rtc *rtc; 197 struct resource *res; 198 uint32_t mode; 199 int ret = 0; 200 201 ret = of_property_read_u32(np, "st,lpc-mode", &mode); 202 if (ret) { 203 dev_err(&pdev->dev, "An LPC mode must be provided\n"); 204 return -EINVAL; 205 } 206 207 /* LPC can either run as a Clocksource or in RTC or WDT mode */ 208 if (mode != ST_LPC_MODE_RTC) 209 return -ENODEV; 210 211 rtc = devm_kzalloc(&pdev->dev, sizeof(struct st_rtc), GFP_KERNEL); 212 if (!rtc) 213 return -ENOMEM; 214 215 spin_lock_init(&rtc->lock); 216 217 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 218 rtc->ioaddr = devm_ioremap_resource(&pdev->dev, res); 219 if (IS_ERR(rtc->ioaddr)) 220 return PTR_ERR(rtc->ioaddr); 221 222 rtc->irq = irq_of_parse_and_map(np, 0); 223 if (!rtc->irq) { 224 dev_err(&pdev->dev, "IRQ missing or invalid\n"); 225 return -EINVAL; 226 } 227 228 ret = devm_request_irq(&pdev->dev, rtc->irq, st_rtc_handler, 0, 229 pdev->name, rtc); 230 if (ret) { 231 dev_err(&pdev->dev, "Failed to request irq %i\n", rtc->irq); 232 return ret; 233 } 234 235 enable_irq_wake(rtc->irq); 236 disable_irq(rtc->irq); 237 238 rtc->clk = clk_get(&pdev->dev, NULL); 239 if (IS_ERR(rtc->clk)) { 240 dev_err(&pdev->dev, "Unable to request clock\n"); 241 return PTR_ERR(rtc->clk); 242 } 243 244 clk_prepare_enable(rtc->clk); 245 246 rtc->clkrate = clk_get_rate(rtc->clk); 247 if (!rtc->clkrate) { 248 dev_err(&pdev->dev, "Unable to fetch clock rate\n"); 249 return -EINVAL; 250 } 251 252 device_set_wakeup_capable(&pdev->dev, 1); 253 254 platform_set_drvdata(pdev, rtc); 255 256 rtc->rtc_dev = rtc_device_register("st-lpc-rtc", &pdev->dev, 257 &st_rtc_ops, THIS_MODULE); 258 if (IS_ERR(rtc->rtc_dev)) { 259 clk_disable_unprepare(rtc->clk); 260 return PTR_ERR(rtc->rtc_dev); 261 } 262 263 return 0; 264 } 265 266 static int st_rtc_remove(struct platform_device *pdev) 267 { 268 struct st_rtc *rtc = platform_get_drvdata(pdev); 269 270 if (likely(rtc->rtc_dev)) 271 rtc_device_unregister(rtc->rtc_dev); 272 273 return 0; 274 } 275 276 #ifdef CONFIG_PM_SLEEP 277 static int st_rtc_suspend(struct device *dev) 278 { 279 struct st_rtc *rtc = dev_get_drvdata(dev); 280 281 if (device_may_wakeup(dev)) 282 return 0; 283 284 writel_relaxed(1, rtc->ioaddr + LPC_WDT_OFF); 285 writel_relaxed(0, rtc->ioaddr + LPC_LPA_START_OFF); 286 writel_relaxed(0, rtc->ioaddr + LPC_WDT_OFF); 287 288 return 0; 289 } 290 291 static int st_rtc_resume(struct device *dev) 292 { 293 struct st_rtc *rtc = dev_get_drvdata(dev); 294 295 rtc_alarm_irq_enable(rtc->rtc_dev, 0); 296 297 /* 298 * clean 'rtc->alarm' to allow a new 299 * .set_alarm to the upper RTC layer 300 */ 301 memset(&rtc->alarm, 0, sizeof(struct rtc_wkalrm)); 302 303 writel_relaxed(0, rtc->ioaddr + LPC_LPA_MSB_OFF); 304 writel_relaxed(0, rtc->ioaddr + LPC_LPA_LSB_OFF); 305 writel_relaxed(1, rtc->ioaddr + LPC_WDT_OFF); 306 writel_relaxed(1, rtc->ioaddr + LPC_LPA_START_OFF); 307 writel_relaxed(0, rtc->ioaddr + LPC_WDT_OFF); 308 309 return 0; 310 } 311 #endif 312 313 static SIMPLE_DEV_PM_OPS(st_rtc_pm_ops, st_rtc_suspend, st_rtc_resume); 314 315 static const struct of_device_id st_rtc_match[] = { 316 { .compatible = "st,stih407-lpc" }, 317 {} 318 }; 319 MODULE_DEVICE_TABLE(of, st_rtc_match); 320 321 static struct platform_driver st_rtc_platform_driver = { 322 .driver = { 323 .name = "st-lpc-rtc", 324 .pm = &st_rtc_pm_ops, 325 .of_match_table = st_rtc_match, 326 }, 327 .probe = st_rtc_probe, 328 .remove = st_rtc_remove, 329 }; 330 331 module_platform_driver(st_rtc_platform_driver); 332 333 MODULE_DESCRIPTION("STMicroelectronics LPC RTC driver"); 334 MODULE_AUTHOR("David Paris <david.paris@st.com>"); 335 MODULE_LICENSE("GPL"); 336