1 /* 2 * Real Time Clock interface for StrongARM SA1x00 and XScale PXA2xx 3 * 4 * Copyright (c) 2000 Nils Faerber 5 * 6 * Based on rtc.c by Paul Gortmaker 7 * 8 * Original Driver by Nils Faerber <nils@kernelconcepts.de> 9 * 10 * Modifications from: 11 * CIH <cih@coventive.com> 12 * Nicolas Pitre <nico@fluxnic.net> 13 * Andrew Christian <andrew.christian@hp.com> 14 * 15 * Converted to the RTC subsystem and Driver Model 16 * by Richard Purdie <rpurdie@rpsys.net> 17 * 18 * This program is free software; you can redistribute it and/or 19 * modify it under the terms of the GNU General Public License 20 * as published by the Free Software Foundation; either version 21 * 2 of the License, or (at your option) any later version. 22 */ 23 24 #include <linux/platform_device.h> 25 #include <linux/module.h> 26 #include <linux/rtc.h> 27 #include <linux/init.h> 28 #include <linux/fs.h> 29 #include <linux/interrupt.h> 30 #include <linux/string.h> 31 #include <linux/pm.h> 32 #include <linux/bitops.h> 33 34 #include <mach/hardware.h> 35 #include <asm/irq.h> 36 37 #ifdef CONFIG_ARCH_PXA 38 #include <mach/regs-rtc.h> 39 #endif 40 41 #define RTC_DEF_DIVIDER (32768 - 1) 42 #define RTC_DEF_TRIM 0 43 44 static const unsigned long RTC_FREQ = 1024; 45 static struct rtc_time rtc_alarm; 46 static DEFINE_SPINLOCK(sa1100_rtc_lock); 47 48 static inline int rtc_periodic_alarm(struct rtc_time *tm) 49 { 50 return (tm->tm_year == -1) || 51 ((unsigned)tm->tm_mon >= 12) || 52 ((unsigned)(tm->tm_mday - 1) >= 31) || 53 ((unsigned)tm->tm_hour > 23) || 54 ((unsigned)tm->tm_min > 59) || 55 ((unsigned)tm->tm_sec > 59); 56 } 57 58 /* 59 * Calculate the next alarm time given the requested alarm time mask 60 * and the current time. 61 */ 62 static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now, 63 struct rtc_time *alrm) 64 { 65 unsigned long next_time; 66 unsigned long now_time; 67 68 next->tm_year = now->tm_year; 69 next->tm_mon = now->tm_mon; 70 next->tm_mday = now->tm_mday; 71 next->tm_hour = alrm->tm_hour; 72 next->tm_min = alrm->tm_min; 73 next->tm_sec = alrm->tm_sec; 74 75 rtc_tm_to_time(now, &now_time); 76 rtc_tm_to_time(next, &next_time); 77 78 if (next_time < now_time) { 79 /* Advance one day */ 80 next_time += 60 * 60 * 24; 81 rtc_time_to_tm(next_time, next); 82 } 83 } 84 85 static int rtc_update_alarm(struct rtc_time *alrm) 86 { 87 struct rtc_time alarm_tm, now_tm; 88 unsigned long now, time; 89 int ret; 90 91 do { 92 now = RCNR; 93 rtc_time_to_tm(now, &now_tm); 94 rtc_next_alarm_time(&alarm_tm, &now_tm, alrm); 95 ret = rtc_tm_to_time(&alarm_tm, &time); 96 if (ret != 0) 97 break; 98 99 RTSR = RTSR & (RTSR_HZE|RTSR_ALE|RTSR_AL); 100 RTAR = time; 101 } while (now != RCNR); 102 103 return ret; 104 } 105 106 static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id) 107 { 108 struct platform_device *pdev = to_platform_device(dev_id); 109 struct rtc_device *rtc = platform_get_drvdata(pdev); 110 unsigned int rtsr; 111 unsigned long events = 0; 112 113 spin_lock(&sa1100_rtc_lock); 114 115 rtsr = RTSR; 116 /* clear interrupt sources */ 117 RTSR = 0; 118 /* Fix for a nasty initialization problem the in SA11xx RTSR register. 119 * See also the comments in sa1100_rtc_probe(). */ 120 if (rtsr & (RTSR_ALE | RTSR_HZE)) { 121 /* This is the original code, before there was the if test 122 * above. This code does not clear interrupts that were not 123 * enabled. */ 124 RTSR = (RTSR_AL | RTSR_HZ) & (rtsr >> 2); 125 } else { 126 /* For some reason, it is possible to enter this routine 127 * without interruptions enabled, it has been tested with 128 * several units (Bug in SA11xx chip?). 129 * 130 * This situation leads to an infinite "loop" of interrupt 131 * routine calling and as a result the processor seems to 132 * lock on its first call to open(). */ 133 RTSR = RTSR_AL | RTSR_HZ; 134 } 135 136 /* clear alarm interrupt if it has occurred */ 137 if (rtsr & RTSR_AL) 138 rtsr &= ~RTSR_ALE; 139 RTSR = rtsr & (RTSR_ALE | RTSR_HZE); 140 141 /* update irq data & counter */ 142 if (rtsr & RTSR_AL) 143 events |= RTC_AF | RTC_IRQF; 144 if (rtsr & RTSR_HZ) 145 events |= RTC_UF | RTC_IRQF; 146 147 rtc_update_irq(rtc, 1, events); 148 149 if (rtsr & RTSR_AL && rtc_periodic_alarm(&rtc_alarm)) 150 rtc_update_alarm(&rtc_alarm); 151 152 spin_unlock(&sa1100_rtc_lock); 153 154 return IRQ_HANDLED; 155 } 156 157 static int sa1100_rtc_open(struct device *dev) 158 { 159 int ret; 160 struct platform_device *plat_dev = to_platform_device(dev); 161 struct rtc_device *rtc = platform_get_drvdata(plat_dev); 162 163 ret = request_irq(IRQ_RTC1Hz, sa1100_rtc_interrupt, 0, "rtc 1Hz", dev); 164 if (ret) { 165 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTC1Hz); 166 goto fail_ui; 167 } 168 ret = request_irq(IRQ_RTCAlrm, sa1100_rtc_interrupt, 0, 169 "rtc Alrm", dev); 170 if (ret) { 171 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTCAlrm); 172 goto fail_ai; 173 } 174 rtc->max_user_freq = RTC_FREQ; 175 rtc_irq_set_freq(rtc, NULL, RTC_FREQ); 176 177 return 0; 178 179 fail_ai: 180 free_irq(IRQ_RTC1Hz, dev); 181 fail_ui: 182 return ret; 183 } 184 185 static void sa1100_rtc_release(struct device *dev) 186 { 187 spin_lock_irq(&sa1100_rtc_lock); 188 RTSR = 0; 189 spin_unlock_irq(&sa1100_rtc_lock); 190 191 free_irq(IRQ_RTCAlrm, dev); 192 free_irq(IRQ_RTC1Hz, dev); 193 } 194 195 static int sa1100_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) 196 { 197 spin_lock_irq(&sa1100_rtc_lock); 198 if (enabled) 199 RTSR |= RTSR_ALE; 200 else 201 RTSR &= ~RTSR_ALE; 202 spin_unlock_irq(&sa1100_rtc_lock); 203 return 0; 204 } 205 206 static int sa1100_rtc_read_time(struct device *dev, struct rtc_time *tm) 207 { 208 rtc_time_to_tm(RCNR, tm); 209 return 0; 210 } 211 212 static int sa1100_rtc_set_time(struct device *dev, struct rtc_time *tm) 213 { 214 unsigned long time; 215 int ret; 216 217 ret = rtc_tm_to_time(tm, &time); 218 if (ret == 0) 219 RCNR = time; 220 return ret; 221 } 222 223 static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) 224 { 225 u32 rtsr; 226 227 memcpy(&alrm->time, &rtc_alarm, sizeof(struct rtc_time)); 228 rtsr = RTSR; 229 alrm->enabled = (rtsr & RTSR_ALE) ? 1 : 0; 230 alrm->pending = (rtsr & RTSR_AL) ? 1 : 0; 231 return 0; 232 } 233 234 static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 235 { 236 int ret; 237 238 spin_lock_irq(&sa1100_rtc_lock); 239 ret = rtc_update_alarm(&alrm->time); 240 if (ret == 0) { 241 if (alrm->enabled) 242 RTSR |= RTSR_ALE; 243 else 244 RTSR &= ~RTSR_ALE; 245 } 246 spin_unlock_irq(&sa1100_rtc_lock); 247 248 return ret; 249 } 250 251 static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq) 252 { 253 seq_printf(seq, "trim/divider\t\t: 0x%08x\n", (u32) RTTR); 254 seq_printf(seq, "RTSR\t\t\t: 0x%08x\n", (u32)RTSR); 255 256 return 0; 257 } 258 259 static const struct rtc_class_ops sa1100_rtc_ops = { 260 .open = sa1100_rtc_open, 261 .release = sa1100_rtc_release, 262 .read_time = sa1100_rtc_read_time, 263 .set_time = sa1100_rtc_set_time, 264 .read_alarm = sa1100_rtc_read_alarm, 265 .set_alarm = sa1100_rtc_set_alarm, 266 .proc = sa1100_rtc_proc, 267 .alarm_irq_enable = sa1100_rtc_alarm_irq_enable, 268 }; 269 270 static int sa1100_rtc_probe(struct platform_device *pdev) 271 { 272 struct rtc_device *rtc; 273 274 /* 275 * According to the manual we should be able to let RTTR be zero 276 * and then a default diviser for a 32.768KHz clock is used. 277 * Apparently this doesn't work, at least for my SA1110 rev 5. 278 * If the clock divider is uninitialized then reset it to the 279 * default value to get the 1Hz clock. 280 */ 281 if (RTTR == 0) { 282 RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16); 283 dev_warn(&pdev->dev, "warning: " 284 "initializing default clock divider/trim value\n"); 285 /* The current RTC value probably doesn't make sense either */ 286 RCNR = 0; 287 } 288 289 device_init_wakeup(&pdev->dev, 1); 290 291 rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops, 292 THIS_MODULE); 293 294 if (IS_ERR(rtc)) 295 return PTR_ERR(rtc); 296 297 platform_set_drvdata(pdev, rtc); 298 299 /* Fix for a nasty initialization problem the in SA11xx RTSR register. 300 * See also the comments in sa1100_rtc_interrupt(). 301 * 302 * Sometimes bit 1 of the RTSR (RTSR_HZ) will wake up 1, which means an 303 * interrupt pending, even though interrupts were never enabled. 304 * In this case, this bit it must be reset before enabling 305 * interruptions to avoid a nonexistent interrupt to occur. 306 * 307 * In principle, the same problem would apply to bit 0, although it has 308 * never been observed to happen. 309 * 310 * This issue is addressed both here and in sa1100_rtc_interrupt(). 311 * If the issue is not addressed here, in the times when the processor 312 * wakes up with the bit set there will be one spurious interrupt. 313 * 314 * The issue is also dealt with in sa1100_rtc_interrupt() to be on the 315 * safe side, once the condition that lead to this strange 316 * initialization is unknown and could in principle happen during 317 * normal processing. 318 * 319 * Notice that clearing bit 1 and 0 is accomplished by writting ONES to 320 * the corresponding bits in RTSR. */ 321 RTSR = RTSR_AL | RTSR_HZ; 322 323 return 0; 324 } 325 326 static int sa1100_rtc_remove(struct platform_device *pdev) 327 { 328 struct rtc_device *rtc = platform_get_drvdata(pdev); 329 330 if (rtc) 331 rtc_device_unregister(rtc); 332 333 return 0; 334 } 335 336 #ifdef CONFIG_PM 337 static int sa1100_rtc_suspend(struct device *dev) 338 { 339 if (device_may_wakeup(dev)) 340 enable_irq_wake(IRQ_RTCAlrm); 341 return 0; 342 } 343 344 static int sa1100_rtc_resume(struct device *dev) 345 { 346 if (device_may_wakeup(dev)) 347 disable_irq_wake(IRQ_RTCAlrm); 348 return 0; 349 } 350 351 static const struct dev_pm_ops sa1100_rtc_pm_ops = { 352 .suspend = sa1100_rtc_suspend, 353 .resume = sa1100_rtc_resume, 354 }; 355 #endif 356 357 static struct platform_driver sa1100_rtc_driver = { 358 .probe = sa1100_rtc_probe, 359 .remove = sa1100_rtc_remove, 360 .driver = { 361 .name = "sa1100-rtc", 362 #ifdef CONFIG_PM 363 .pm = &sa1100_rtc_pm_ops, 364 #endif 365 }, 366 }; 367 368 module_platform_driver(sa1100_rtc_driver); 369 370 MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>"); 371 MODULE_DESCRIPTION("SA11x0/PXA2xx Realtime Clock Driver (RTC)"); 372 MODULE_LICENSE("GPL"); 373 MODULE_ALIAS("platform:sa1100-rtc"); 374