rtc-at91sam9.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * "RTT as Real Time Clock" driver for AT91SAM9 SoC family
  3. *
  4. * (C) 2007 Michel Benoit
  5. *
  6. * Based on rtc-at91rm9200.c by Rick Bronson
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/time.h>
  17. #include <linux/rtc.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/ioctl.h>
  20. #include <linux/slab.h>
  21. #include <linux/platform_data/atmel.h>
  22. #include <linux/io.h>
  23. #include <mach/at91_rtt.h>
  24. #include <mach/cpu.h>
  25. /*
  26. * This driver uses two configurable hardware resources that live in the
  27. * AT91SAM9 backup power domain (intended to be powered at all times)
  28. * to implement the Real Time Clock interfaces
  29. *
  30. * - A "Real-time Timer" (RTT) counts up in seconds from a base time.
  31. * We can't assign the counter value (CRTV) ... but we can reset it.
  32. *
  33. * - One of the "General Purpose Backup Registers" (GPBRs) holds the
  34. * base time, normally an offset from the beginning of the POSIX
  35. * epoch (1970-Jan-1 00:00:00 UTC). Some systems also include the
  36. * local timezone's offset.
  37. *
  38. * The RTC's value is the RTT counter plus that offset. The RTC's alarm
  39. * is likewise a base (ALMV) plus that offset.
  40. *
  41. * Not all RTTs will be used as RTCs; some systems have multiple RTTs to
  42. * choose from, or a "real" RTC module. All systems have multiple GPBR
  43. * registers available, likewise usable for more than "RTC" support.
  44. */
  45. /*
  46. * We store ALARM_DISABLED in ALMV to record that no alarm is set.
  47. * It's also the reset value for that field.
  48. */
  49. #define ALARM_DISABLED ((u32)~0)
  50. struct sam9_rtc {
  51. void __iomem *rtt;
  52. struct rtc_device *rtcdev;
  53. u32 imr;
  54. void __iomem *gpbr;
  55. int irq;
  56. };
  57. #define rtt_readl(rtc, field) \
  58. __raw_readl((rtc)->rtt + AT91_RTT_ ## field)
  59. #define rtt_writel(rtc, field, val) \
  60. __raw_writel((val), (rtc)->rtt + AT91_RTT_ ## field)
  61. #define gpbr_readl(rtc) \
  62. __raw_readl((rtc)->gpbr)
  63. #define gpbr_writel(rtc, val) \
  64. __raw_writel((val), (rtc)->gpbr)
  65. /*
  66. * Read current time and date in RTC
  67. */
  68. static int at91_rtc_readtime(struct device *dev, struct rtc_time *tm)
  69. {
  70. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  71. u32 secs, secs2;
  72. u32 offset;
  73. /* read current time offset */
  74. offset = gpbr_readl(rtc);
  75. if (offset == 0)
  76. return -EILSEQ;
  77. /* reread the counter to help sync the two clock domains */
  78. secs = rtt_readl(rtc, VR);
  79. secs2 = rtt_readl(rtc, VR);
  80. if (secs != secs2)
  81. secs = rtt_readl(rtc, VR);
  82. rtc_time_to_tm(offset + secs, tm);
  83. dev_dbg(dev, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "readtime",
  84. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  85. tm->tm_hour, tm->tm_min, tm->tm_sec);
  86. return 0;
  87. }
  88. /*
  89. * Set current time and date in RTC
  90. */
  91. static int at91_rtc_settime(struct device *dev, struct rtc_time *tm)
  92. {
  93. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  94. int err;
  95. u32 offset, alarm, mr;
  96. unsigned long secs;
  97. dev_dbg(dev, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "settime",
  98. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  99. tm->tm_hour, tm->tm_min, tm->tm_sec);
  100. err = rtc_tm_to_time(tm, &secs);
  101. if (err != 0)
  102. return err;
  103. mr = rtt_readl(rtc, MR);
  104. /* disable interrupts */
  105. rtt_writel(rtc, MR, mr & ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN));
  106. /* read current time offset */
  107. offset = gpbr_readl(rtc);
  108. /* store the new base time in a battery backup register */
  109. secs += 1;
  110. gpbr_writel(rtc, secs);
  111. /* adjust the alarm time for the new base */
  112. alarm = rtt_readl(rtc, AR);
  113. if (alarm != ALARM_DISABLED) {
  114. if (offset > secs) {
  115. /* time jumped backwards, increase time until alarm */
  116. alarm += (offset - secs);
  117. } else if ((alarm + offset) > secs) {
  118. /* time jumped forwards, decrease time until alarm */
  119. alarm -= (secs - offset);
  120. } else {
  121. /* time jumped past the alarm, disable alarm */
  122. alarm = ALARM_DISABLED;
  123. mr &= ~AT91_RTT_ALMIEN;
  124. }
  125. rtt_writel(rtc, AR, alarm);
  126. }
  127. /* reset the timer, and re-enable interrupts */
  128. rtt_writel(rtc, MR, mr | AT91_RTT_RTTRST);
  129. return 0;
  130. }
  131. static int at91_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
  132. {
  133. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  134. struct rtc_time *tm = &alrm->time;
  135. u32 alarm = rtt_readl(rtc, AR);
  136. u32 offset;
  137. offset = gpbr_readl(rtc);
  138. if (offset == 0)
  139. return -EILSEQ;
  140. memset(alrm, 0, sizeof(*alrm));
  141. if (alarm != ALARM_DISABLED && offset != 0) {
  142. rtc_time_to_tm(offset + alarm, tm);
  143. dev_dbg(dev, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "readalarm",
  144. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  145. tm->tm_hour, tm->tm_min, tm->tm_sec);
  146. if (rtt_readl(rtc, MR) & AT91_RTT_ALMIEN)
  147. alrm->enabled = 1;
  148. }
  149. return 0;
  150. }
  151. static int at91_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  152. {
  153. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  154. struct rtc_time *tm = &alrm->time;
  155. unsigned long secs;
  156. u32 offset;
  157. u32 mr;
  158. int err;
  159. err = rtc_tm_to_time(tm, &secs);
  160. if (err != 0)
  161. return err;
  162. offset = gpbr_readl(rtc);
  163. if (offset == 0) {
  164. /* time is not set */
  165. return -EILSEQ;
  166. }
  167. mr = rtt_readl(rtc, MR);
  168. rtt_writel(rtc, MR, mr & ~AT91_RTT_ALMIEN);
  169. /* alarm in the past? finish and leave disabled */
  170. if (secs <= offset) {
  171. rtt_writel(rtc, AR, ALARM_DISABLED);
  172. return 0;
  173. }
  174. /* else set alarm and maybe enable it */
  175. rtt_writel(rtc, AR, secs - offset);
  176. if (alrm->enabled)
  177. rtt_writel(rtc, MR, mr | AT91_RTT_ALMIEN);
  178. dev_dbg(dev, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "setalarm",
  179. tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour,
  180. tm->tm_min, tm->tm_sec);
  181. return 0;
  182. }
  183. static int at91_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  184. {
  185. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  186. u32 mr = rtt_readl(rtc, MR);
  187. dev_dbg(dev, "alarm_irq_enable: enabled=%08x, mr %08x\n", enabled, mr);
  188. if (enabled)
  189. rtt_writel(rtc, MR, mr | AT91_RTT_ALMIEN);
  190. else
  191. rtt_writel(rtc, MR, mr & ~AT91_RTT_ALMIEN);
  192. return 0;
  193. }
  194. /*
  195. * Provide additional RTC information in /proc/driver/rtc
  196. */
  197. static int at91_rtc_proc(struct device *dev, struct seq_file *seq)
  198. {
  199. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  200. u32 mr = mr = rtt_readl(rtc, MR);
  201. seq_printf(seq, "update_IRQ\t: %s\n",
  202. (mr & AT91_RTT_RTTINCIEN) ? "yes" : "no");
  203. return 0;
  204. }
  205. /*
  206. * IRQ handler for the RTC
  207. */
  208. static irqreturn_t at91_rtc_interrupt(int irq, void *_rtc)
  209. {
  210. struct sam9_rtc *rtc = _rtc;
  211. u32 sr, mr;
  212. unsigned long events = 0;
  213. /* Shared interrupt may be for another device. Note: reading
  214. * SR clears it, so we must only read it in this irq handler!
  215. */
  216. mr = rtt_readl(rtc, MR) & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
  217. sr = rtt_readl(rtc, SR) & (mr >> 16);
  218. if (!sr)
  219. return IRQ_NONE;
  220. /* alarm status */
  221. if (sr & AT91_RTT_ALMS)
  222. events |= (RTC_AF | RTC_IRQF);
  223. /* timer update/increment */
  224. if (sr & AT91_RTT_RTTINC)
  225. events |= (RTC_UF | RTC_IRQF);
  226. rtc_update_irq(rtc->rtcdev, 1, events);
  227. pr_debug("%s: num=%ld, events=0x%02lx\n", __func__,
  228. events >> 8, events & 0x000000FF);
  229. return IRQ_HANDLED;
  230. }
  231. static const struct rtc_class_ops at91_rtc_ops = {
  232. .read_time = at91_rtc_readtime,
  233. .set_time = at91_rtc_settime,
  234. .read_alarm = at91_rtc_readalarm,
  235. .set_alarm = at91_rtc_setalarm,
  236. .proc = at91_rtc_proc,
  237. .alarm_irq_enable = at91_rtc_alarm_irq_enable,
  238. };
  239. /*
  240. * Initialize and install RTC driver
  241. */
  242. static int at91_rtc_probe(struct platform_device *pdev)
  243. {
  244. struct resource *r, *r_gpbr;
  245. struct sam9_rtc *rtc;
  246. int ret, irq;
  247. u32 mr;
  248. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  249. r_gpbr = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  250. if (!r || !r_gpbr) {
  251. dev_err(&pdev->dev, "need 2 ressources\n");
  252. return -ENODEV;
  253. }
  254. irq = platform_get_irq(pdev, 0);
  255. if (irq < 0) {
  256. dev_err(&pdev->dev, "failed to get interrupt resource\n");
  257. return irq;
  258. }
  259. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  260. if (!rtc)
  261. return -ENOMEM;
  262. rtc->irq = irq;
  263. /* platform setup code should have handled this; sigh */
  264. if (!device_can_wakeup(&pdev->dev))
  265. device_init_wakeup(&pdev->dev, 1);
  266. platform_set_drvdata(pdev, rtc);
  267. rtc->rtt = devm_ioremap(&pdev->dev, r->start, resource_size(r));
  268. if (!rtc->rtt) {
  269. dev_err(&pdev->dev, "failed to map registers, aborting.\n");
  270. return -ENOMEM;
  271. }
  272. rtc->gpbr = devm_ioremap(&pdev->dev, r_gpbr->start,
  273. resource_size(r_gpbr));
  274. if (!rtc->gpbr) {
  275. dev_err(&pdev->dev, "failed to map gpbr registers, aborting.\n");
  276. return -ENOMEM;
  277. }
  278. mr = rtt_readl(rtc, MR);
  279. /* unless RTT is counting at 1 Hz, re-initialize it */
  280. if ((mr & AT91_RTT_RTPRES) != AT91_SLOW_CLOCK) {
  281. mr = AT91_RTT_RTTRST | (AT91_SLOW_CLOCK & AT91_RTT_RTPRES);
  282. gpbr_writel(rtc, 0);
  283. }
  284. /* disable all interrupts (same as on shutdown path) */
  285. mr &= ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
  286. rtt_writel(rtc, MR, mr);
  287. rtc->rtcdev = devm_rtc_device_register(&pdev->dev, pdev->name,
  288. &at91_rtc_ops, THIS_MODULE);
  289. if (IS_ERR(rtc->rtcdev))
  290. return PTR_ERR(rtc->rtcdev);
  291. /* register irq handler after we know what name we'll use */
  292. ret = devm_request_irq(&pdev->dev, rtc->irq, at91_rtc_interrupt,
  293. IRQF_SHARED, dev_name(&rtc->rtcdev->dev), rtc);
  294. if (ret) {
  295. dev_dbg(&pdev->dev, "can't share IRQ %d?\n", rtc->irq);
  296. return ret;
  297. }
  298. /* NOTE: sam9260 rev A silicon has a ROM bug which resets the
  299. * RTT on at least some reboots. If you have that chip, you must
  300. * initialize the time from some external source like a GPS, wall
  301. * clock, discrete RTC, etc
  302. */
  303. if (gpbr_readl(rtc) == 0)
  304. dev_warn(&pdev->dev, "%s: SET TIME!\n",
  305. dev_name(&rtc->rtcdev->dev));
  306. return 0;
  307. }
  308. /*
  309. * Disable and remove the RTC driver
  310. */
  311. static int at91_rtc_remove(struct platform_device *pdev)
  312. {
  313. struct sam9_rtc *rtc = platform_get_drvdata(pdev);
  314. u32 mr = rtt_readl(rtc, MR);
  315. /* disable all interrupts */
  316. rtt_writel(rtc, MR, mr & ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN));
  317. return 0;
  318. }
  319. static void at91_rtc_shutdown(struct platform_device *pdev)
  320. {
  321. struct sam9_rtc *rtc = platform_get_drvdata(pdev);
  322. u32 mr = rtt_readl(rtc, MR);
  323. rtc->imr = mr & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
  324. rtt_writel(rtc, MR, mr & ~rtc->imr);
  325. }
  326. #ifdef CONFIG_PM_SLEEP
  327. /* AT91SAM9 RTC Power management control */
  328. static int at91_rtc_suspend(struct device *dev)
  329. {
  330. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  331. u32 mr = rtt_readl(rtc, MR);
  332. /*
  333. * This IRQ is shared with DBGU and other hardware which isn't
  334. * necessarily a wakeup event source.
  335. */
  336. rtc->imr = mr & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
  337. if (rtc->imr) {
  338. if (device_may_wakeup(dev) && (mr & AT91_RTT_ALMIEN)) {
  339. enable_irq_wake(rtc->irq);
  340. /* don't let RTTINC cause wakeups */
  341. if (mr & AT91_RTT_RTTINCIEN)
  342. rtt_writel(rtc, MR, mr & ~AT91_RTT_RTTINCIEN);
  343. } else
  344. rtt_writel(rtc, MR, mr & ~rtc->imr);
  345. }
  346. return 0;
  347. }
  348. static int at91_rtc_resume(struct device *dev)
  349. {
  350. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  351. u32 mr;
  352. if (rtc->imr) {
  353. if (device_may_wakeup(dev))
  354. disable_irq_wake(rtc->irq);
  355. mr = rtt_readl(rtc, MR);
  356. rtt_writel(rtc, MR, mr | rtc->imr);
  357. }
  358. return 0;
  359. }
  360. #endif
  361. static SIMPLE_DEV_PM_OPS(at91_rtc_pm_ops, at91_rtc_suspend, at91_rtc_resume);
  362. static struct platform_driver at91_rtc_driver = {
  363. .probe = at91_rtc_probe,
  364. .remove = at91_rtc_remove,
  365. .shutdown = at91_rtc_shutdown,
  366. .driver = {
  367. .name = "rtc-at91sam9",
  368. .owner = THIS_MODULE,
  369. .pm = &at91_rtc_pm_ops,
  370. },
  371. };
  372. module_platform_driver(at91_rtc_driver);
  373. MODULE_AUTHOR("Michel Benoit");
  374. MODULE_DESCRIPTION("RTC driver for Atmel AT91SAM9x");
  375. MODULE_LICENSE("GPL");