rtc-at91rm9200.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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/ioctl.h>
  28. #include <linux/completion.h>
  29. #include <asm/uaccess.h>
  30. #include <mach/at91_rtc.h>
  31. #define at91_rtc_read(field) \
  32. __raw_readl(at91_rtc_regs + field)
  33. #define at91_rtc_write(field, val) \
  34. __raw_writel((val), at91_rtc_regs + field)
  35. #define AT91_RTC_EPOCH 1900UL /* just like arch/arm/common/rtctime.c */
  36. static DECLARE_COMPLETION(at91_rtc_updated);
  37. static unsigned int at91_alarm_year = AT91_RTC_EPOCH;
  38. static void __iomem *at91_rtc_regs;
  39. static int irq;
  40. /*
  41. * Decode time/date into rtc_time structure
  42. */
  43. static void at91_rtc_decodetime(unsigned int timereg, unsigned int calreg,
  44. struct rtc_time *tm)
  45. {
  46. unsigned int time, date;
  47. /* must read twice in case it changes */
  48. do {
  49. time = at91_rtc_read(timereg);
  50. date = at91_rtc_read(calreg);
  51. } while ((time != at91_rtc_read(timereg)) ||
  52. (date != at91_rtc_read(calreg)));
  53. tm->tm_sec = bcd2bin((time & AT91_RTC_SEC) >> 0);
  54. tm->tm_min = bcd2bin((time & AT91_RTC_MIN) >> 8);
  55. tm->tm_hour = bcd2bin((time & AT91_RTC_HOUR) >> 16);
  56. /*
  57. * The Calendar Alarm register does not have a field for
  58. * the year - so these will return an invalid value. When an
  59. * alarm is set, at91_alarm_year will store the current year.
  60. */
  61. tm->tm_year = bcd2bin(date & AT91_RTC_CENT) * 100; /* century */
  62. tm->tm_year += bcd2bin((date & AT91_RTC_YEAR) >> 8); /* year */
  63. tm->tm_wday = bcd2bin((date & AT91_RTC_DAY) >> 21) - 1; /* day of the week [0-6], Sunday=0 */
  64. tm->tm_mon = bcd2bin((date & AT91_RTC_MONTH) >> 16) - 1;
  65. tm->tm_mday = bcd2bin((date & AT91_RTC_DATE) >> 24);
  66. }
  67. /*
  68. * Read current time and date in RTC
  69. */
  70. static int at91_rtc_readtime(struct device *dev, struct rtc_time *tm)
  71. {
  72. at91_rtc_decodetime(AT91_RTC_TIMR, AT91_RTC_CALR, tm);
  73. tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
  74. tm->tm_year = tm->tm_year - 1900;
  75. pr_debug("%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __func__,
  76. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  77. tm->tm_hour, tm->tm_min, tm->tm_sec);
  78. return 0;
  79. }
  80. /*
  81. * Set current time and date in RTC
  82. */
  83. static int at91_rtc_settime(struct device *dev, struct rtc_time *tm)
  84. {
  85. unsigned long cr;
  86. pr_debug("%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __func__,
  87. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  88. tm->tm_hour, tm->tm_min, tm->tm_sec);
  89. /* Stop Time/Calendar from counting */
  90. cr = at91_rtc_read(AT91_RTC_CR);
  91. at91_rtc_write(AT91_RTC_CR, cr | AT91_RTC_UPDCAL | AT91_RTC_UPDTIM);
  92. at91_rtc_write(AT91_RTC_IER, AT91_RTC_ACKUPD);
  93. wait_for_completion(&at91_rtc_updated); /* wait for ACKUPD interrupt */
  94. at91_rtc_write(AT91_RTC_IDR, AT91_RTC_ACKUPD);
  95. at91_rtc_write(AT91_RTC_TIMR,
  96. bin2bcd(tm->tm_sec) << 0
  97. | bin2bcd(tm->tm_min) << 8
  98. | bin2bcd(tm->tm_hour) << 16);
  99. at91_rtc_write(AT91_RTC_CALR,
  100. bin2bcd((tm->tm_year + 1900) / 100) /* century */
  101. | bin2bcd(tm->tm_year % 100) << 8 /* year */
  102. | bin2bcd(tm->tm_mon + 1) << 16 /* tm_mon starts at zero */
  103. | bin2bcd(tm->tm_wday + 1) << 21 /* day of the week [0-6], Sunday=0 */
  104. | bin2bcd(tm->tm_mday) << 24);
  105. /* Restart Time/Calendar */
  106. cr = at91_rtc_read(AT91_RTC_CR);
  107. at91_rtc_write(AT91_RTC_CR, cr & ~(AT91_RTC_UPDCAL | AT91_RTC_UPDTIM));
  108. return 0;
  109. }
  110. /*
  111. * Read alarm time and date in RTC
  112. */
  113. static int at91_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
  114. {
  115. struct rtc_time *tm = &alrm->time;
  116. at91_rtc_decodetime(AT91_RTC_TIMALR, AT91_RTC_CALALR, tm);
  117. tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
  118. tm->tm_year = at91_alarm_year - 1900;
  119. alrm->enabled = (at91_rtc_read(AT91_RTC_IMR) & AT91_RTC_ALARM)
  120. ? 1 : 0;
  121. pr_debug("%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __func__,
  122. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  123. tm->tm_hour, tm->tm_min, tm->tm_sec);
  124. return 0;
  125. }
  126. /*
  127. * Set alarm time and date in RTC
  128. */
  129. static int at91_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  130. {
  131. struct rtc_time tm;
  132. at91_rtc_decodetime(AT91_RTC_TIMR, AT91_RTC_CALR, &tm);
  133. at91_alarm_year = tm.tm_year;
  134. tm.tm_mon = alrm->time.tm_mon;
  135. tm.tm_mday = alrm->time.tm_mday;
  136. tm.tm_hour = alrm->time.tm_hour;
  137. tm.tm_min = alrm->time.tm_min;
  138. tm.tm_sec = alrm->time.tm_sec;
  139. at91_rtc_write(AT91_RTC_IDR, AT91_RTC_ALARM);
  140. at91_rtc_write(AT91_RTC_TIMALR,
  141. bin2bcd(tm.tm_sec) << 0
  142. | bin2bcd(tm.tm_min) << 8
  143. | bin2bcd(tm.tm_hour) << 16
  144. | AT91_RTC_HOUREN | AT91_RTC_MINEN | AT91_RTC_SECEN);
  145. at91_rtc_write(AT91_RTC_CALALR,
  146. bin2bcd(tm.tm_mon + 1) << 16 /* tm_mon starts at zero */
  147. | bin2bcd(tm.tm_mday) << 24
  148. | AT91_RTC_DATEEN | AT91_RTC_MTHEN);
  149. if (alrm->enabled) {
  150. at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_ALARM);
  151. at91_rtc_write(AT91_RTC_IER, AT91_RTC_ALARM);
  152. }
  153. pr_debug("%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __func__,
  154. at91_alarm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour,
  155. tm.tm_min, tm.tm_sec);
  156. return 0;
  157. }
  158. static int at91_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  159. {
  160. pr_debug("%s(): cmd=%08x\n", __func__, enabled);
  161. if (enabled) {
  162. at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_ALARM);
  163. at91_rtc_write(AT91_RTC_IER, AT91_RTC_ALARM);
  164. } else
  165. at91_rtc_write(AT91_RTC_IDR, AT91_RTC_ALARM);
  166. return 0;
  167. }
  168. /*
  169. * Provide additional RTC information in /proc/driver/rtc
  170. */
  171. static int at91_rtc_proc(struct device *dev, struct seq_file *seq)
  172. {
  173. unsigned long imr = at91_rtc_read(AT91_RTC_IMR);
  174. seq_printf(seq, "update_IRQ\t: %s\n",
  175. (imr & AT91_RTC_ACKUPD) ? "yes" : "no");
  176. seq_printf(seq, "periodic_IRQ\t: %s\n",
  177. (imr & AT91_RTC_SECEV) ? "yes" : "no");
  178. return 0;
  179. }
  180. /*
  181. * IRQ handler for the RTC
  182. */
  183. static irqreturn_t at91_rtc_interrupt(int irq, void *dev_id)
  184. {
  185. struct platform_device *pdev = dev_id;
  186. struct rtc_device *rtc = platform_get_drvdata(pdev);
  187. unsigned int rtsr;
  188. unsigned long events = 0;
  189. rtsr = at91_rtc_read(AT91_RTC_SR) & at91_rtc_read(AT91_RTC_IMR);
  190. if (rtsr) { /* this interrupt is shared! Is it ours? */
  191. if (rtsr & AT91_RTC_ALARM)
  192. events |= (RTC_AF | RTC_IRQF);
  193. if (rtsr & AT91_RTC_SECEV)
  194. events |= (RTC_UF | RTC_IRQF);
  195. if (rtsr & AT91_RTC_ACKUPD)
  196. complete(&at91_rtc_updated);
  197. at91_rtc_write(AT91_RTC_SCCR, rtsr); /* clear status reg */
  198. rtc_update_irq(rtc, 1, events);
  199. pr_debug("%s(): num=%ld, events=0x%02lx\n", __func__,
  200. events >> 8, events & 0x000000FF);
  201. return IRQ_HANDLED;
  202. }
  203. return IRQ_NONE; /* not handled */
  204. }
  205. static const struct rtc_class_ops at91_rtc_ops = {
  206. .read_time = at91_rtc_readtime,
  207. .set_time = at91_rtc_settime,
  208. .read_alarm = at91_rtc_readalarm,
  209. .set_alarm = at91_rtc_setalarm,
  210. .proc = at91_rtc_proc,
  211. .alarm_irq_enable = at91_rtc_alarm_irq_enable,
  212. };
  213. /*
  214. * Initialize and install RTC driver
  215. */
  216. static int __init at91_rtc_probe(struct platform_device *pdev)
  217. {
  218. struct rtc_device *rtc;
  219. struct resource *regs;
  220. int ret = 0;
  221. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  222. if (!regs) {
  223. dev_err(&pdev->dev, "no mmio resource defined\n");
  224. return -ENXIO;
  225. }
  226. irq = platform_get_irq(pdev, 0);
  227. if (irq < 0) {
  228. dev_err(&pdev->dev, "no irq resource defined\n");
  229. return -ENXIO;
  230. }
  231. at91_rtc_regs = ioremap(regs->start, resource_size(regs));
  232. if (!at91_rtc_regs) {
  233. dev_err(&pdev->dev, "failed to map registers, aborting.\n");
  234. return -ENOMEM;
  235. }
  236. at91_rtc_write(AT91_RTC_CR, 0);
  237. at91_rtc_write(AT91_RTC_MR, 0); /* 24 hour mode */
  238. /* Disable all interrupts */
  239. at91_rtc_write(AT91_RTC_IDR, AT91_RTC_ACKUPD | AT91_RTC_ALARM |
  240. AT91_RTC_SECEV | AT91_RTC_TIMEV |
  241. AT91_RTC_CALEV);
  242. ret = request_irq(irq, at91_rtc_interrupt,
  243. IRQF_SHARED,
  244. "at91_rtc", pdev);
  245. if (ret) {
  246. printk(KERN_ERR "at91_rtc: IRQ %d already in use.\n",
  247. irq);
  248. return ret;
  249. }
  250. /* cpu init code should really have flagged this device as
  251. * being wake-capable; if it didn't, do that here.
  252. */
  253. if (!device_can_wakeup(&pdev->dev))
  254. device_init_wakeup(&pdev->dev, 1);
  255. rtc = rtc_device_register(pdev->name, &pdev->dev,
  256. &at91_rtc_ops, THIS_MODULE);
  257. if (IS_ERR(rtc)) {
  258. free_irq(irq, pdev);
  259. return PTR_ERR(rtc);
  260. }
  261. platform_set_drvdata(pdev, rtc);
  262. printk(KERN_INFO "AT91 Real Time Clock driver.\n");
  263. return 0;
  264. }
  265. /*
  266. * Disable and remove the RTC driver
  267. */
  268. static int __exit at91_rtc_remove(struct platform_device *pdev)
  269. {
  270. struct rtc_device *rtc = platform_get_drvdata(pdev);
  271. /* Disable all interrupts */
  272. at91_rtc_write(AT91_RTC_IDR, AT91_RTC_ACKUPD | AT91_RTC_ALARM |
  273. AT91_RTC_SECEV | AT91_RTC_TIMEV |
  274. AT91_RTC_CALEV);
  275. free_irq(irq, pdev);
  276. rtc_device_unregister(rtc);
  277. platform_set_drvdata(pdev, NULL);
  278. return 0;
  279. }
  280. #ifdef CONFIG_PM
  281. /* AT91RM9200 RTC Power management control */
  282. static u32 at91_rtc_imr;
  283. static int at91_rtc_suspend(struct device *dev)
  284. {
  285. /* this IRQ is shared with DBGU and other hardware which isn't
  286. * necessarily doing PM like we are...
  287. */
  288. at91_rtc_imr = at91_rtc_read(AT91_RTC_IMR)
  289. & (AT91_RTC_ALARM|AT91_RTC_SECEV);
  290. if (at91_rtc_imr) {
  291. if (device_may_wakeup(dev))
  292. enable_irq_wake(irq);
  293. else
  294. at91_rtc_write(AT91_RTC_IDR, at91_rtc_imr);
  295. }
  296. return 0;
  297. }
  298. static int at91_rtc_resume(struct device *dev)
  299. {
  300. if (at91_rtc_imr) {
  301. if (device_may_wakeup(dev))
  302. disable_irq_wake(irq);
  303. else
  304. at91_rtc_write(AT91_RTC_IER, at91_rtc_imr);
  305. }
  306. return 0;
  307. }
  308. static const struct dev_pm_ops at91_rtc_pm = {
  309. .suspend = at91_rtc_suspend,
  310. .resume = at91_rtc_resume,
  311. };
  312. #define at91_rtc_pm_ptr &at91_rtc_pm
  313. #else
  314. #define at91_rtc_pm_ptr NULL
  315. #endif
  316. static struct platform_driver at91_rtc_driver = {
  317. .remove = __exit_p(at91_rtc_remove),
  318. .driver = {
  319. .name = "at91_rtc",
  320. .owner = THIS_MODULE,
  321. .pm = at91_rtc_pm_ptr,
  322. },
  323. };
  324. static int __init at91_rtc_init(void)
  325. {
  326. return platform_driver_probe(&at91_rtc_driver, at91_rtc_probe);
  327. }
  328. static void __exit at91_rtc_exit(void)
  329. {
  330. platform_driver_unregister(&at91_rtc_driver);
  331. }
  332. module_init(at91_rtc_init);
  333. module_exit(at91_rtc_exit);
  334. MODULE_AUTHOR("Rick Bronson");
  335. MODULE_DESCRIPTION("RTC driver for Atmel AT91RM9200");
  336. MODULE_LICENSE("GPL");
  337. MODULE_ALIAS("platform:at91_rtc");