rtc-at91rm9200.c 12 KB

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