rtc-at91rm9200.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * Real Time Clock interface for Linux on Atmel AT91RM9200
  3. *
  4. * Copyright (C) 2002 Rick Bronson
  5. *
  6. * Converted to RTC class model by Andrew Victor
  7. *
  8. * Ported to Linux 2.6 by Steven Scholz
  9. * Based on s3c2410-rtc.c Simtec Electronics
  10. *
  11. * Based on sa1100-rtc.c by Nils Faerber
  12. * Based on rtc.c by Paul Gortmaker
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. *
  19. */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/time.h>
  24. #include <linux/rtc.h>
  25. #include <linux/bcd.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/ioctl.h>
  29. #include <linux/completion.h>
  30. #include <linux/io.h>
  31. #include <linux/of.h>
  32. #include <linux/of_device.h>
  33. #include <linux/suspend.h>
  34. #include <linux/uaccess.h>
  35. #include "rtc-at91rm9200.h"
  36. #define at91_rtc_read(field) \
  37. readl_relaxed(at91_rtc_regs + field)
  38. #define at91_rtc_write(field, val) \
  39. writel_relaxed((val), at91_rtc_regs + field)
  40. #define AT91_RTC_EPOCH 1900UL /* just like arch/arm/common/rtctime.c */
  41. struct at91_rtc_config {
  42. bool use_shadow_imr;
  43. };
  44. static const struct at91_rtc_config *at91_rtc_config;
  45. static DECLARE_COMPLETION(at91_rtc_updated);
  46. static DECLARE_COMPLETION(at91_rtc_upd_rdy);
  47. static unsigned int at91_alarm_year = AT91_RTC_EPOCH;
  48. static void __iomem *at91_rtc_regs;
  49. static int irq;
  50. static DEFINE_SPINLOCK(at91_rtc_lock);
  51. static u32 at91_rtc_shadow_imr;
  52. static bool suspended;
  53. static DEFINE_SPINLOCK(suspended_lock);
  54. static unsigned long cached_events;
  55. static u32 at91_rtc_imr;
  56. static void at91_rtc_write_ier(u32 mask)
  57. {
  58. unsigned long flags;
  59. spin_lock_irqsave(&at91_rtc_lock, flags);
  60. at91_rtc_shadow_imr |= mask;
  61. at91_rtc_write(AT91_RTC_IER, mask);
  62. spin_unlock_irqrestore(&at91_rtc_lock, flags);
  63. }
  64. static void at91_rtc_write_idr(u32 mask)
  65. {
  66. unsigned long flags;
  67. spin_lock_irqsave(&at91_rtc_lock, flags);
  68. at91_rtc_write(AT91_RTC_IDR, mask);
  69. /*
  70. * Register read back (of any RTC-register) needed to make sure
  71. * IDR-register write has reached the peripheral before updating
  72. * shadow mask.
  73. *
  74. * Note that there is still a possibility that the mask is updated
  75. * before interrupts have actually been disabled in hardware. The only
  76. * way to be certain would be to poll the IMR-register, which is is
  77. * the very register we are trying to emulate. The register read back
  78. * is a reasonable heuristic.
  79. */
  80. at91_rtc_read(AT91_RTC_SR);
  81. at91_rtc_shadow_imr &= ~mask;
  82. spin_unlock_irqrestore(&at91_rtc_lock, flags);
  83. }
  84. static u32 at91_rtc_read_imr(void)
  85. {
  86. unsigned long flags;
  87. u32 mask;
  88. if (at91_rtc_config->use_shadow_imr) {
  89. spin_lock_irqsave(&at91_rtc_lock, flags);
  90. mask = at91_rtc_shadow_imr;
  91. spin_unlock_irqrestore(&at91_rtc_lock, flags);
  92. } else {
  93. mask = at91_rtc_read(AT91_RTC_IMR);
  94. }
  95. return mask;
  96. }
  97. /*
  98. * Decode time/date into rtc_time structure
  99. */
  100. static void at91_rtc_decodetime(unsigned int timereg, unsigned int calreg,
  101. struct rtc_time *tm)
  102. {
  103. unsigned int time, date;
  104. /* must read twice in case it changes */
  105. do {
  106. time = at91_rtc_read(timereg);
  107. date = at91_rtc_read(calreg);
  108. } while ((time != at91_rtc_read(timereg)) ||
  109. (date != at91_rtc_read(calreg)));
  110. tm->tm_sec = bcd2bin((time & AT91_RTC_SEC) >> 0);
  111. tm->tm_min = bcd2bin((time & AT91_RTC_MIN) >> 8);
  112. tm->tm_hour = bcd2bin((time & AT91_RTC_HOUR) >> 16);
  113. /*
  114. * The Calendar Alarm register does not have a field for
  115. * the year - so these will return an invalid value. When an
  116. * alarm is set, at91_alarm_year will store the current year.
  117. */
  118. tm->tm_year = bcd2bin(date & AT91_RTC_CENT) * 100; /* century */
  119. tm->tm_year += bcd2bin((date & AT91_RTC_YEAR) >> 8); /* year */
  120. tm->tm_wday = bcd2bin((date & AT91_RTC_DAY) >> 21) - 1; /* day of the week [0-6], Sunday=0 */
  121. tm->tm_mon = bcd2bin((date & AT91_RTC_MONTH) >> 16) - 1;
  122. tm->tm_mday = bcd2bin((date & AT91_RTC_DATE) >> 24);
  123. }
  124. /*
  125. * Read current time and date in RTC
  126. */
  127. static int at91_rtc_readtime(struct device *dev, struct rtc_time *tm)
  128. {
  129. at91_rtc_decodetime(AT91_RTC_TIMR, AT91_RTC_CALR, tm);
  130. tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
  131. tm->tm_year = tm->tm_year - 1900;
  132. dev_dbg(dev, "%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __func__,
  133. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  134. tm->tm_hour, tm->tm_min, tm->tm_sec);
  135. return 0;
  136. }
  137. /*
  138. * Set current time and date in RTC
  139. */
  140. static int at91_rtc_settime(struct device *dev, struct rtc_time *tm)
  141. {
  142. unsigned long cr;
  143. dev_dbg(dev, "%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __func__,
  144. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  145. tm->tm_hour, tm->tm_min, tm->tm_sec);
  146. wait_for_completion(&at91_rtc_upd_rdy);
  147. /* Stop Time/Calendar from counting */
  148. cr = at91_rtc_read(AT91_RTC_CR);
  149. at91_rtc_write(AT91_RTC_CR, cr | AT91_RTC_UPDCAL | AT91_RTC_UPDTIM);
  150. at91_rtc_write_ier(AT91_RTC_ACKUPD);
  151. wait_for_completion(&at91_rtc_updated); /* wait for ACKUPD interrupt */
  152. at91_rtc_write_idr(AT91_RTC_ACKUPD);
  153. at91_rtc_write(AT91_RTC_TIMR,
  154. bin2bcd(tm->tm_sec) << 0
  155. | bin2bcd(tm->tm_min) << 8
  156. | bin2bcd(tm->tm_hour) << 16);
  157. at91_rtc_write(AT91_RTC_CALR,
  158. bin2bcd((tm->tm_year + 1900) / 100) /* century */
  159. | bin2bcd(tm->tm_year % 100) << 8 /* year */
  160. | bin2bcd(tm->tm_mon + 1) << 16 /* tm_mon starts at zero */
  161. | bin2bcd(tm->tm_wday + 1) << 21 /* day of the week [0-6], Sunday=0 */
  162. | bin2bcd(tm->tm_mday) << 24);
  163. /* Restart Time/Calendar */
  164. cr = at91_rtc_read(AT91_RTC_CR);
  165. at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_SECEV);
  166. at91_rtc_write(AT91_RTC_CR, cr & ~(AT91_RTC_UPDCAL | AT91_RTC_UPDTIM));
  167. at91_rtc_write_ier(AT91_RTC_SECEV);
  168. return 0;
  169. }
  170. /*
  171. * Read alarm time and date in RTC
  172. */
  173. static int at91_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
  174. {
  175. struct rtc_time *tm = &alrm->time;
  176. at91_rtc_decodetime(AT91_RTC_TIMALR, AT91_RTC_CALALR, tm);
  177. tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
  178. tm->tm_year = at91_alarm_year - 1900;
  179. alrm->enabled = (at91_rtc_read_imr() & AT91_RTC_ALARM)
  180. ? 1 : 0;
  181. dev_dbg(dev, "%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __func__,
  182. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  183. tm->tm_hour, tm->tm_min, tm->tm_sec);
  184. return 0;
  185. }
  186. /*
  187. * Set alarm time and date in RTC
  188. */
  189. static int at91_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  190. {
  191. struct rtc_time tm;
  192. at91_rtc_decodetime(AT91_RTC_TIMR, AT91_RTC_CALR, &tm);
  193. at91_alarm_year = tm.tm_year;
  194. tm.tm_mon = alrm->time.tm_mon;
  195. tm.tm_mday = alrm->time.tm_mday;
  196. tm.tm_hour = alrm->time.tm_hour;
  197. tm.tm_min = alrm->time.tm_min;
  198. tm.tm_sec = alrm->time.tm_sec;
  199. at91_rtc_write_idr(AT91_RTC_ALARM);
  200. at91_rtc_write(AT91_RTC_TIMALR,
  201. bin2bcd(tm.tm_sec) << 0
  202. | bin2bcd(tm.tm_min) << 8
  203. | bin2bcd(tm.tm_hour) << 16
  204. | AT91_RTC_HOUREN | AT91_RTC_MINEN | AT91_RTC_SECEN);
  205. at91_rtc_write(AT91_RTC_CALALR,
  206. bin2bcd(tm.tm_mon + 1) << 16 /* tm_mon starts at zero */
  207. | bin2bcd(tm.tm_mday) << 24
  208. | AT91_RTC_DATEEN | AT91_RTC_MTHEN);
  209. if (alrm->enabled) {
  210. at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_ALARM);
  211. at91_rtc_write_ier(AT91_RTC_ALARM);
  212. }
  213. dev_dbg(dev, "%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __func__,
  214. at91_alarm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour,
  215. tm.tm_min, tm.tm_sec);
  216. return 0;
  217. }
  218. static int at91_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  219. {
  220. dev_dbg(dev, "%s(): cmd=%08x\n", __func__, enabled);
  221. if (enabled) {
  222. at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_ALARM);
  223. at91_rtc_write_ier(AT91_RTC_ALARM);
  224. } else
  225. at91_rtc_write_idr(AT91_RTC_ALARM);
  226. return 0;
  227. }
  228. /*
  229. * Provide additional RTC information in /proc/driver/rtc
  230. */
  231. static int at91_rtc_proc(struct device *dev, struct seq_file *seq)
  232. {
  233. unsigned long imr = at91_rtc_read_imr();
  234. seq_printf(seq, "update_IRQ\t: %s\n",
  235. (imr & AT91_RTC_ACKUPD) ? "yes" : "no");
  236. seq_printf(seq, "periodic_IRQ\t: %s\n",
  237. (imr & AT91_RTC_SECEV) ? "yes" : "no");
  238. return 0;
  239. }
  240. /*
  241. * IRQ handler for the RTC
  242. */
  243. static irqreturn_t at91_rtc_interrupt(int irq, void *dev_id)
  244. {
  245. struct platform_device *pdev = dev_id;
  246. struct rtc_device *rtc = platform_get_drvdata(pdev);
  247. unsigned int rtsr;
  248. unsigned long events = 0;
  249. int ret = IRQ_NONE;
  250. spin_lock(&suspended_lock);
  251. rtsr = at91_rtc_read(AT91_RTC_SR) & at91_rtc_read_imr();
  252. if (rtsr) { /* this interrupt is shared! Is it ours? */
  253. if (rtsr & AT91_RTC_ALARM)
  254. events |= (RTC_AF | RTC_IRQF);
  255. if (rtsr & AT91_RTC_SECEV) {
  256. complete(&at91_rtc_upd_rdy);
  257. at91_rtc_write_idr(AT91_RTC_SECEV);
  258. }
  259. if (rtsr & AT91_RTC_ACKUPD)
  260. complete(&at91_rtc_updated);
  261. at91_rtc_write(AT91_RTC_SCCR, rtsr); /* clear status reg */
  262. if (!suspended) {
  263. rtc_update_irq(rtc, 1, events);
  264. dev_dbg(&pdev->dev, "%s(): num=%ld, events=0x%02lx\n",
  265. __func__, events >> 8, events & 0x000000FF);
  266. } else {
  267. cached_events |= events;
  268. at91_rtc_write_idr(at91_rtc_imr);
  269. pm_system_wakeup();
  270. }
  271. ret = IRQ_HANDLED;
  272. }
  273. spin_unlock(&suspended_lock);
  274. return ret;
  275. }
  276. static const struct at91_rtc_config at91rm9200_config = {
  277. };
  278. static const struct at91_rtc_config at91sam9x5_config = {
  279. .use_shadow_imr = true,
  280. };
  281. #ifdef CONFIG_OF
  282. static const struct of_device_id at91_rtc_dt_ids[] = {
  283. {
  284. .compatible = "atmel,at91rm9200-rtc",
  285. .data = &at91rm9200_config,
  286. }, {
  287. .compatible = "atmel,at91sam9x5-rtc",
  288. .data = &at91sam9x5_config,
  289. }, {
  290. /* sentinel */
  291. }
  292. };
  293. MODULE_DEVICE_TABLE(of, at91_rtc_dt_ids);
  294. #endif
  295. static const struct at91_rtc_config *
  296. at91_rtc_get_config(struct platform_device *pdev)
  297. {
  298. const struct of_device_id *match;
  299. if (pdev->dev.of_node) {
  300. match = of_match_node(at91_rtc_dt_ids, pdev->dev.of_node);
  301. if (!match)
  302. return NULL;
  303. return (const struct at91_rtc_config *)match->data;
  304. }
  305. return &at91rm9200_config;
  306. }
  307. static const struct rtc_class_ops at91_rtc_ops = {
  308. .read_time = at91_rtc_readtime,
  309. .set_time = at91_rtc_settime,
  310. .read_alarm = at91_rtc_readalarm,
  311. .set_alarm = at91_rtc_setalarm,
  312. .proc = at91_rtc_proc,
  313. .alarm_irq_enable = at91_rtc_alarm_irq_enable,
  314. };
  315. /*
  316. * Initialize and install RTC driver
  317. */
  318. static int __init at91_rtc_probe(struct platform_device *pdev)
  319. {
  320. struct rtc_device *rtc;
  321. struct resource *regs;
  322. int ret = 0;
  323. at91_rtc_config = at91_rtc_get_config(pdev);
  324. if (!at91_rtc_config)
  325. return -ENODEV;
  326. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  327. if (!regs) {
  328. dev_err(&pdev->dev, "no mmio resource defined\n");
  329. return -ENXIO;
  330. }
  331. irq = platform_get_irq(pdev, 0);
  332. if (irq < 0) {
  333. dev_err(&pdev->dev, "no irq resource defined\n");
  334. return -ENXIO;
  335. }
  336. at91_rtc_regs = devm_ioremap(&pdev->dev, regs->start,
  337. resource_size(regs));
  338. if (!at91_rtc_regs) {
  339. dev_err(&pdev->dev, "failed to map registers, aborting.\n");
  340. return -ENOMEM;
  341. }
  342. at91_rtc_write(AT91_RTC_CR, 0);
  343. at91_rtc_write(AT91_RTC_MR, 0); /* 24 hour mode */
  344. /* Disable all interrupts */
  345. at91_rtc_write_idr(AT91_RTC_ACKUPD | AT91_RTC_ALARM |
  346. AT91_RTC_SECEV | AT91_RTC_TIMEV |
  347. AT91_RTC_CALEV);
  348. ret = devm_request_irq(&pdev->dev, irq, at91_rtc_interrupt,
  349. IRQF_SHARED | IRQF_COND_SUSPEND,
  350. "at91_rtc", pdev);
  351. if (ret) {
  352. dev_err(&pdev->dev, "IRQ %d already in use.\n", irq);
  353. return ret;
  354. }
  355. /* cpu init code should really have flagged this device as
  356. * being wake-capable; if it didn't, do that here.
  357. */
  358. if (!device_can_wakeup(&pdev->dev))
  359. device_init_wakeup(&pdev->dev, 1);
  360. rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  361. &at91_rtc_ops, THIS_MODULE);
  362. if (IS_ERR(rtc))
  363. return PTR_ERR(rtc);
  364. platform_set_drvdata(pdev, rtc);
  365. /* enable SECEV interrupt in order to initialize at91_rtc_upd_rdy
  366. * completion.
  367. */
  368. at91_rtc_write_ier(AT91_RTC_SECEV);
  369. dev_info(&pdev->dev, "AT91 Real Time Clock driver.\n");
  370. return 0;
  371. }
  372. /*
  373. * Disable and remove the RTC driver
  374. */
  375. static int __exit at91_rtc_remove(struct platform_device *pdev)
  376. {
  377. /* Disable all interrupts */
  378. at91_rtc_write_idr(AT91_RTC_ACKUPD | AT91_RTC_ALARM |
  379. AT91_RTC_SECEV | AT91_RTC_TIMEV |
  380. AT91_RTC_CALEV);
  381. return 0;
  382. }
  383. static void at91_rtc_shutdown(struct platform_device *pdev)
  384. {
  385. /* Disable all interrupts */
  386. at91_rtc_write(AT91_RTC_IDR, AT91_RTC_ACKUPD | AT91_RTC_ALARM |
  387. AT91_RTC_SECEV | AT91_RTC_TIMEV |
  388. AT91_RTC_CALEV);
  389. }
  390. #ifdef CONFIG_PM_SLEEP
  391. /* AT91RM9200 RTC Power management control */
  392. static int at91_rtc_suspend(struct device *dev)
  393. {
  394. /* this IRQ is shared with DBGU and other hardware which isn't
  395. * necessarily doing PM like we are...
  396. */
  397. at91_rtc_imr = at91_rtc_read_imr()
  398. & (AT91_RTC_ALARM|AT91_RTC_SECEV);
  399. if (at91_rtc_imr) {
  400. if (device_may_wakeup(dev)) {
  401. unsigned long flags;
  402. enable_irq_wake(irq);
  403. spin_lock_irqsave(&suspended_lock, flags);
  404. suspended = true;
  405. spin_unlock_irqrestore(&suspended_lock, flags);
  406. } else {
  407. at91_rtc_write_idr(at91_rtc_imr);
  408. }
  409. }
  410. return 0;
  411. }
  412. static int at91_rtc_resume(struct device *dev)
  413. {
  414. struct rtc_device *rtc = dev_get_drvdata(dev);
  415. if (at91_rtc_imr) {
  416. if (device_may_wakeup(dev)) {
  417. unsigned long flags;
  418. spin_lock_irqsave(&suspended_lock, flags);
  419. if (cached_events) {
  420. rtc_update_irq(rtc, 1, cached_events);
  421. cached_events = 0;
  422. }
  423. suspended = false;
  424. spin_unlock_irqrestore(&suspended_lock, flags);
  425. disable_irq_wake(irq);
  426. }
  427. at91_rtc_write_ier(at91_rtc_imr);
  428. }
  429. return 0;
  430. }
  431. #endif
  432. static SIMPLE_DEV_PM_OPS(at91_rtc_pm_ops, at91_rtc_suspend, at91_rtc_resume);
  433. static struct platform_driver at91_rtc_driver = {
  434. .remove = __exit_p(at91_rtc_remove),
  435. .shutdown = at91_rtc_shutdown,
  436. .driver = {
  437. .name = "at91_rtc",
  438. .pm = &at91_rtc_pm_ops,
  439. .of_match_table = of_match_ptr(at91_rtc_dt_ids),
  440. },
  441. };
  442. module_platform_driver_probe(at91_rtc_driver, at91_rtc_probe);
  443. MODULE_AUTHOR("Rick Bronson");
  444. MODULE_DESCRIPTION("RTC driver for Atmel AT91RM9200");
  445. MODULE_LICENSE("GPL");
  446. MODULE_ALIAS("platform:at91_rtc");