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