rtc-at91rm9200.c 13 KB

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