rtc-at91sam9.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 <mach/board.h>
  21. #include <mach/at91_rtt.h>
  22. #include <mach/cpu.h>
  23. /*
  24. * This driver uses two configurable hardware resources that live in the
  25. * AT91SAM9 backup power domain (intended to be powered at all times)
  26. * to implement the Real Time Clock interfaces
  27. *
  28. * - A "Real-time Timer" (RTT) counts up in seconds from a base time.
  29. * We can't assign the counter value (CRTV) ... but we can reset it.
  30. *
  31. * - One of the "General Purpose Backup Registers" (GPBRs) holds the
  32. * base time, normally an offset from the beginning of the POSIX
  33. * epoch (1970-Jan-1 00:00:00 UTC). Some systems also include the
  34. * local timezone's offset.
  35. *
  36. * The RTC's value is the RTT counter plus that offset. The RTC's alarm
  37. * is likewise a base (ALMV) plus that offset.
  38. *
  39. * Not all RTTs will be used as RTCs; some systems have multiple RTTs to
  40. * choose from, or a "real" RTC module. All systems have multiple GPBR
  41. * registers available, likewise usable for more than "RTC" support.
  42. */
  43. /*
  44. * We store ALARM_DISABLED in ALMV to record that no alarm is set.
  45. * It's also the reset value for that field.
  46. */
  47. #define ALARM_DISABLED ((u32)~0)
  48. struct sam9_rtc {
  49. void __iomem *rtt;
  50. struct rtc_device *rtcdev;
  51. u32 imr;
  52. };
  53. #define rtt_readl(rtc, field) \
  54. __raw_readl((rtc)->rtt + AT91_RTT_ ## field)
  55. #define rtt_writel(rtc, field, val) \
  56. __raw_writel((val), (rtc)->rtt + AT91_RTT_ ## field)
  57. #define gpbr_readl(rtc) \
  58. at91_sys_read(AT91_GPBR + 4 * CONFIG_RTC_DRV_AT91SAM9_GPBR)
  59. #define gpbr_writel(rtc, val) \
  60. at91_sys_write(AT91_GPBR + 4 * CONFIG_RTC_DRV_AT91SAM9_GPBR, (val))
  61. /*
  62. * Read current time and date in RTC
  63. */
  64. static int at91_rtc_readtime(struct device *dev, struct rtc_time *tm)
  65. {
  66. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  67. u32 secs, secs2;
  68. u32 offset;
  69. /* read current time offset */
  70. offset = gpbr_readl(rtc);
  71. if (offset == 0)
  72. return -EILSEQ;
  73. /* reread the counter to help sync the two clock domains */
  74. secs = rtt_readl(rtc, VR);
  75. secs2 = rtt_readl(rtc, VR);
  76. if (secs != secs2)
  77. secs = rtt_readl(rtc, VR);
  78. rtc_time_to_tm(offset + secs, tm);
  79. dev_dbg(dev, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "readtime",
  80. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  81. tm->tm_hour, tm->tm_min, tm->tm_sec);
  82. return 0;
  83. }
  84. /*
  85. * Set current time and date in RTC
  86. */
  87. static int at91_rtc_settime(struct device *dev, struct rtc_time *tm)
  88. {
  89. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  90. int err;
  91. u32 offset, alarm, mr;
  92. unsigned long secs;
  93. dev_dbg(dev, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "settime",
  94. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  95. tm->tm_hour, tm->tm_min, tm->tm_sec);
  96. err = rtc_tm_to_time(tm, &secs);
  97. if (err != 0)
  98. return err;
  99. mr = rtt_readl(rtc, MR);
  100. /* disable interrupts */
  101. rtt_writel(rtc, MR, mr & ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN));
  102. /* read current time offset */
  103. offset = gpbr_readl(rtc);
  104. /* store the new base time in a battery backup register */
  105. secs += 1;
  106. gpbr_writel(rtc, secs);
  107. /* adjust the alarm time for the new base */
  108. alarm = rtt_readl(rtc, AR);
  109. if (alarm != ALARM_DISABLED) {
  110. if (offset > secs) {
  111. /* time jumped backwards, increase time until alarm */
  112. alarm += (offset - secs);
  113. } else if ((alarm + offset) > secs) {
  114. /* time jumped forwards, decrease time until alarm */
  115. alarm -= (secs - offset);
  116. } else {
  117. /* time jumped past the alarm, disable alarm */
  118. alarm = ALARM_DISABLED;
  119. mr &= ~AT91_RTT_ALMIEN;
  120. }
  121. rtt_writel(rtc, AR, alarm);
  122. }
  123. /* reset the timer, and re-enable interrupts */
  124. rtt_writel(rtc, MR, mr | AT91_RTT_RTTRST);
  125. return 0;
  126. }
  127. static int at91_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
  128. {
  129. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  130. struct rtc_time *tm = &alrm->time;
  131. u32 alarm = rtt_readl(rtc, AR);
  132. u32 offset;
  133. offset = gpbr_readl(rtc);
  134. if (offset == 0)
  135. return -EILSEQ;
  136. memset(alrm, 0, sizeof(alrm));
  137. if (alarm != ALARM_DISABLED && offset != 0) {
  138. rtc_time_to_tm(offset + alarm, tm);
  139. dev_dbg(dev, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "readalarm",
  140. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  141. tm->tm_hour, tm->tm_min, tm->tm_sec);
  142. if (rtt_readl(rtc, MR) & AT91_RTT_ALMIEN)
  143. alrm->enabled = 1;
  144. }
  145. return 0;
  146. }
  147. static int at91_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  148. {
  149. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  150. struct rtc_time *tm = &alrm->time;
  151. unsigned long secs;
  152. u32 offset;
  153. u32 mr;
  154. int err;
  155. err = rtc_tm_to_time(tm, &secs);
  156. if (err != 0)
  157. return err;
  158. offset = gpbr_readl(rtc);
  159. if (offset == 0) {
  160. /* time is not set */
  161. return -EILSEQ;
  162. }
  163. mr = rtt_readl(rtc, MR);
  164. rtt_writel(rtc, MR, mr & ~AT91_RTT_ALMIEN);
  165. /* alarm in the past? finish and leave disabled */
  166. if (secs <= offset) {
  167. rtt_writel(rtc, AR, ALARM_DISABLED);
  168. return 0;
  169. }
  170. /* else set alarm and maybe enable it */
  171. rtt_writel(rtc, AR, secs - offset);
  172. if (alrm->enabled)
  173. rtt_writel(rtc, MR, mr | AT91_RTT_ALMIEN);
  174. dev_dbg(dev, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "setalarm",
  175. tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour,
  176. tm->tm_min, tm->tm_sec);
  177. return 0;
  178. }
  179. /*
  180. * Handle commands from user-space
  181. */
  182. static int at91_rtc_ioctl(struct device *dev, unsigned int cmd,
  183. unsigned long arg)
  184. {
  185. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  186. int ret = 0;
  187. u32 mr = rtt_readl(rtc, MR);
  188. dev_dbg(dev, "ioctl: cmd=%08x, arg=%08lx, mr %08x\n", cmd, arg, mr);
  189. switch (cmd) {
  190. case RTC_AIE_OFF: /* alarm off */
  191. rtt_writel(rtc, MR, mr & ~AT91_RTT_ALMIEN);
  192. break;
  193. case RTC_AIE_ON: /* alarm on */
  194. rtt_writel(rtc, MR, mr | AT91_RTT_ALMIEN);
  195. break;
  196. case RTC_UIE_OFF: /* update off */
  197. rtt_writel(rtc, MR, mr & ~AT91_RTT_RTTINCIEN);
  198. break;
  199. case RTC_UIE_ON: /* update on */
  200. rtt_writel(rtc, MR, mr | AT91_RTT_RTTINCIEN);
  201. break;
  202. default:
  203. ret = -ENOIOCTLCMD;
  204. break;
  205. }
  206. return ret;
  207. }
  208. /*
  209. * Provide additional RTC information in /proc/driver/rtc
  210. */
  211. static int at91_rtc_proc(struct device *dev, struct seq_file *seq)
  212. {
  213. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  214. u32 mr = mr = rtt_readl(rtc, MR);
  215. seq_printf(seq, "update_IRQ\t: %s\n",
  216. (mr & AT91_RTT_RTTINCIEN) ? "yes" : "no");
  217. return 0;
  218. }
  219. /*
  220. * IRQ handler for the RTC
  221. */
  222. static irqreturn_t at91_rtc_interrupt(int irq, void *_rtc)
  223. {
  224. struct sam9_rtc *rtc = _rtc;
  225. u32 sr, mr;
  226. unsigned long events = 0;
  227. /* Shared interrupt may be for another device. Note: reading
  228. * SR clears it, so we must only read it in this irq handler!
  229. */
  230. mr = rtt_readl(rtc, MR) & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
  231. sr = rtt_readl(rtc, SR) & (mr >> 16);
  232. if (!sr)
  233. return IRQ_NONE;
  234. /* alarm status */
  235. if (sr & AT91_RTT_ALMS)
  236. events |= (RTC_AF | RTC_IRQF);
  237. /* timer update/increment */
  238. if (sr & AT91_RTT_RTTINC)
  239. events |= (RTC_UF | RTC_IRQF);
  240. rtc_update_irq(rtc->rtcdev, 1, events);
  241. pr_debug("%s: num=%ld, events=0x%02lx\n", __func__,
  242. events >> 8, events & 0x000000FF);
  243. return IRQ_HANDLED;
  244. }
  245. static const struct rtc_class_ops at91_rtc_ops = {
  246. .ioctl = at91_rtc_ioctl,
  247. .read_time = at91_rtc_readtime,
  248. .set_time = at91_rtc_settime,
  249. .read_alarm = at91_rtc_readalarm,
  250. .set_alarm = at91_rtc_setalarm,
  251. .proc = at91_rtc_proc,
  252. };
  253. /*
  254. * Initialize and install RTC driver
  255. */
  256. static int __init at91_rtc_probe(struct platform_device *pdev)
  257. {
  258. struct resource *r;
  259. struct sam9_rtc *rtc;
  260. int ret;
  261. u32 mr;
  262. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  263. if (!r)
  264. return -ENODEV;
  265. rtc = kzalloc(sizeof *rtc, GFP_KERNEL);
  266. if (!rtc)
  267. return -ENOMEM;
  268. /* platform setup code should have handled this; sigh */
  269. if (!device_can_wakeup(&pdev->dev))
  270. device_init_wakeup(&pdev->dev, 1);
  271. platform_set_drvdata(pdev, rtc);
  272. rtc->rtt = (void __force __iomem *) (AT91_VA_BASE_SYS - AT91_BASE_SYS);
  273. rtc->rtt += r->start;
  274. mr = rtt_readl(rtc, MR);
  275. /* unless RTT is counting at 1 Hz, re-initialize it */
  276. if ((mr & AT91_RTT_RTPRES) != AT91_SLOW_CLOCK) {
  277. mr = AT91_RTT_RTTRST | (AT91_SLOW_CLOCK & AT91_RTT_RTPRES);
  278. gpbr_writel(rtc, 0);
  279. }
  280. /* disable all interrupts (same as on shutdown path) */
  281. mr &= ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
  282. rtt_writel(rtc, MR, mr);
  283. rtc->rtcdev = rtc_device_register(pdev->name, &pdev->dev,
  284. &at91_rtc_ops, THIS_MODULE);
  285. if (IS_ERR(rtc->rtcdev)) {
  286. ret = PTR_ERR(rtc->rtcdev);
  287. goto fail;
  288. }
  289. /* register irq handler after we know what name we'll use */
  290. ret = request_irq(AT91_ID_SYS, at91_rtc_interrupt,
  291. IRQF_DISABLED | IRQF_SHARED,
  292. dev_name(&rtc->rtcdev->dev), rtc);
  293. if (ret) {
  294. dev_dbg(&pdev->dev, "can't share IRQ %d?\n", AT91_ID_SYS);
  295. rtc_device_unregister(rtc->rtcdev);
  296. goto fail;
  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. fail:
  308. platform_set_drvdata(pdev, NULL);
  309. kfree(rtc);
  310. return ret;
  311. }
  312. /*
  313. * Disable and remove the RTC driver
  314. */
  315. static int __exit at91_rtc_remove(struct platform_device *pdev)
  316. {
  317. struct sam9_rtc *rtc = platform_get_drvdata(pdev);
  318. u32 mr = rtt_readl(rtc, MR);
  319. /* disable all interrupts */
  320. rtt_writel(rtc, MR, mr & ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN));
  321. free_irq(AT91_ID_SYS, rtc);
  322. rtc_device_unregister(rtc->rtcdev);
  323. platform_set_drvdata(pdev, NULL);
  324. kfree(rtc);
  325. return 0;
  326. }
  327. static void at91_rtc_shutdown(struct platform_device *pdev)
  328. {
  329. struct sam9_rtc *rtc = platform_get_drvdata(pdev);
  330. u32 mr = rtt_readl(rtc, MR);
  331. rtc->imr = mr & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
  332. rtt_writel(rtc, MR, mr & ~rtc->imr);
  333. }
  334. #ifdef CONFIG_PM
  335. /* AT91SAM9 RTC Power management control */
  336. static int at91_rtc_suspend(struct platform_device *pdev,
  337. pm_message_t state)
  338. {
  339. struct sam9_rtc *rtc = platform_get_drvdata(pdev);
  340. u32 mr = rtt_readl(rtc, MR);
  341. /*
  342. * This IRQ is shared with DBGU and other hardware which isn't
  343. * necessarily a wakeup event source.
  344. */
  345. rtc->imr = mr & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
  346. if (rtc->imr) {
  347. if (device_may_wakeup(&pdev->dev) && (mr & AT91_RTT_ALMIEN)) {
  348. enable_irq_wake(AT91_ID_SYS);
  349. /* don't let RTTINC cause wakeups */
  350. if (mr & AT91_RTT_RTTINCIEN)
  351. rtt_writel(rtc, MR, mr & ~AT91_RTT_RTTINCIEN);
  352. } else
  353. rtt_writel(rtc, MR, mr & ~rtc->imr);
  354. }
  355. return 0;
  356. }
  357. static int at91_rtc_resume(struct platform_device *pdev)
  358. {
  359. struct sam9_rtc *rtc = platform_get_drvdata(pdev);
  360. u32 mr;
  361. if (rtc->imr) {
  362. if (device_may_wakeup(&pdev->dev))
  363. disable_irq_wake(AT91_ID_SYS);
  364. mr = rtt_readl(rtc, MR);
  365. rtt_writel(rtc, MR, mr | rtc->imr);
  366. }
  367. return 0;
  368. }
  369. #else
  370. #define at91_rtc_suspend NULL
  371. #define at91_rtc_resume NULL
  372. #endif
  373. static struct platform_driver at91_rtc_driver = {
  374. .driver.name = "rtc-at91sam9",
  375. .driver.owner = THIS_MODULE,
  376. .remove = __exit_p(at91_rtc_remove),
  377. .shutdown = at91_rtc_shutdown,
  378. .suspend = at91_rtc_suspend,
  379. .resume = at91_rtc_resume,
  380. };
  381. /* Chips can have more than one RTT module, and they can be used for more
  382. * than just RTCs. So we can't just register as "the" RTT driver.
  383. *
  384. * A normal approach in such cases is to create a library to allocate and
  385. * free the modules. Here we just use bus_find_device() as like such a
  386. * library, binding directly ... no runtime "library" footprint is needed.
  387. */
  388. static int __init at91_rtc_match(struct device *dev, void *v)
  389. {
  390. struct platform_device *pdev = to_platform_device(dev);
  391. int ret;
  392. /* continue searching if this isn't the RTT we need */
  393. if (strcmp("at91_rtt", pdev->name) != 0
  394. || pdev->id != CONFIG_RTC_DRV_AT91SAM9_RTT)
  395. goto fail;
  396. /* else we found it ... but fail unless we can bind to the RTC driver */
  397. if (dev->driver) {
  398. dev_dbg(dev, "busy, can't use as RTC!\n");
  399. goto fail;
  400. }
  401. dev->driver = &at91_rtc_driver.driver;
  402. if (device_attach(dev) == 0) {
  403. dev_dbg(dev, "can't attach RTC!\n");
  404. goto fail;
  405. }
  406. ret = at91_rtc_probe(pdev);
  407. if (ret == 0)
  408. return true;
  409. dev_dbg(dev, "RTC probe err %d!\n", ret);
  410. fail:
  411. return false;
  412. }
  413. static int __init at91_rtc_init(void)
  414. {
  415. int status;
  416. struct device *rtc;
  417. status = platform_driver_register(&at91_rtc_driver);
  418. if (status)
  419. return status;
  420. rtc = bus_find_device(&platform_bus_type, NULL,
  421. NULL, at91_rtc_match);
  422. if (!rtc)
  423. platform_driver_unregister(&at91_rtc_driver);
  424. return rtc ? 0 : -ENODEV;
  425. }
  426. module_init(at91_rtc_init);
  427. static void __exit at91_rtc_exit(void)
  428. {
  429. platform_driver_unregister(&at91_rtc_driver);
  430. }
  431. module_exit(at91_rtc_exit);
  432. MODULE_AUTHOR("Michel Benoit");
  433. MODULE_DESCRIPTION("RTC driver for Atmel AT91SAM9x");
  434. MODULE_LICENSE("GPL");