rtc-bfin.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * Blackfin On-Chip Real Time Clock Driver
  3. * Supports BF52[257]/BF53[123]/BF53[467]/BF54[24789]
  4. *
  5. * Copyright 2004-2008 Analog Devices Inc.
  6. *
  7. * Enter bugs at http://blackfin.uclinux.org/
  8. *
  9. * Licensed under the GPL-2 or later.
  10. */
  11. /* The biggest issue we deal with in this driver is that register writes are
  12. * synced to the RTC frequency of 1Hz. So if you write to a register and
  13. * attempt to write again before the first write has completed, the new write
  14. * is simply discarded. This can easily be troublesome if userspace disables
  15. * one event (say periodic) and then right after enables an event (say alarm).
  16. * Since all events are maintained in the same interrupt mask register, if
  17. * we wrote to it to disable the first event and then wrote to it again to
  18. * enable the second event, that second event would not be enabled as the
  19. * write would be discarded and things quickly fall apart.
  20. *
  21. * To keep this delay from significantly degrading performance (we, in theory,
  22. * would have to sleep for up to 1 second everytime we wanted to write a
  23. * register), we only check the write pending status before we start to issue
  24. * a new write. We bank on the idea that it doesnt matter when the sync
  25. * happens so long as we don't attempt another write before it does. The only
  26. * time userspace would take this penalty is when they try and do multiple
  27. * operations right after another ... but in this case, they need to take the
  28. * sync penalty, so we should be OK.
  29. *
  30. * Also note that the RTC_ISTAT register does not suffer this penalty; its
  31. * writes to clear status registers complete immediately.
  32. */
  33. /* It may seem odd that there is no SWCNT code in here (which would be exposed
  34. * via the periodic interrupt event, or PIE). Since the Blackfin RTC peripheral
  35. * runs in units of seconds (N/HZ) but the Linux framework runs in units of HZ
  36. * (2^N HZ), there is no point in keeping code that only provides 1 HZ PIEs.
  37. * The same exact behavior can be accomplished by using the update interrupt
  38. * event (UIE). Maybe down the line the RTC peripheral will suck less in which
  39. * case we can re-introduce PIE support.
  40. */
  41. #include <linux/bcd.h>
  42. #include <linux/completion.h>
  43. #include <linux/delay.h>
  44. #include <linux/init.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/kernel.h>
  47. #include <linux/module.h>
  48. #include <linux/platform_device.h>
  49. #include <linux/rtc.h>
  50. #include <linux/seq_file.h>
  51. #include <asm/blackfin.h>
  52. #define dev_dbg_stamp(dev) dev_dbg(dev, "%s:%i: here i am\n", __func__, __LINE__)
  53. struct bfin_rtc {
  54. struct rtc_device *rtc_dev;
  55. struct rtc_time rtc_alarm;
  56. u16 rtc_wrote_regs;
  57. };
  58. /* Bit values for the ISTAT / ICTL registers */
  59. #define RTC_ISTAT_WRITE_COMPLETE 0x8000
  60. #define RTC_ISTAT_WRITE_PENDING 0x4000
  61. #define RTC_ISTAT_ALARM_DAY 0x0040
  62. #define RTC_ISTAT_24HR 0x0020
  63. #define RTC_ISTAT_HOUR 0x0010
  64. #define RTC_ISTAT_MIN 0x0008
  65. #define RTC_ISTAT_SEC 0x0004
  66. #define RTC_ISTAT_ALARM 0x0002
  67. #define RTC_ISTAT_STOPWATCH 0x0001
  68. /* Shift values for RTC_STAT register */
  69. #define DAY_BITS_OFF 17
  70. #define HOUR_BITS_OFF 12
  71. #define MIN_BITS_OFF 6
  72. #define SEC_BITS_OFF 0
  73. /* Some helper functions to convert between the common RTC notion of time
  74. * and the internal Blackfin notion that is encoded in 32bits.
  75. */
  76. static inline u32 rtc_time_to_bfin(unsigned long now)
  77. {
  78. u32 sec = (now % 60);
  79. u32 min = (now % (60 * 60)) / 60;
  80. u32 hour = (now % (60 * 60 * 24)) / (60 * 60);
  81. u32 days = (now / (60 * 60 * 24));
  82. return (sec << SEC_BITS_OFF) +
  83. (min << MIN_BITS_OFF) +
  84. (hour << HOUR_BITS_OFF) +
  85. (days << DAY_BITS_OFF);
  86. }
  87. static inline unsigned long rtc_bfin_to_time(u32 rtc_bfin)
  88. {
  89. return (((rtc_bfin >> SEC_BITS_OFF) & 0x003F)) +
  90. (((rtc_bfin >> MIN_BITS_OFF) & 0x003F) * 60) +
  91. (((rtc_bfin >> HOUR_BITS_OFF) & 0x001F) * 60 * 60) +
  92. (((rtc_bfin >> DAY_BITS_OFF) & 0x7FFF) * 60 * 60 * 24);
  93. }
  94. static inline void rtc_bfin_to_tm(u32 rtc_bfin, struct rtc_time *tm)
  95. {
  96. rtc_time_to_tm(rtc_bfin_to_time(rtc_bfin), tm);
  97. }
  98. /**
  99. * bfin_rtc_sync_pending - make sure pending writes have complete
  100. *
  101. * Wait for the previous write to a RTC register to complete.
  102. * Unfortunately, we can't sleep here as that introduces a race condition when
  103. * turning on interrupt events. Consider this:
  104. * - process sets alarm
  105. * - process enables alarm
  106. * - process sleeps while waiting for rtc write to sync
  107. * - interrupt fires while process is sleeping
  108. * - interrupt acks the event by writing to ISTAT
  109. * - interrupt sets the WRITE PENDING bit
  110. * - interrupt handler finishes
  111. * - process wakes up, sees WRITE PENDING bit set, goes to sleep
  112. * - interrupt fires while process is sleeping
  113. * If anyone can point out the obvious solution here, i'm listening :). This
  114. * shouldn't be an issue on an SMP or preempt system as this function should
  115. * only be called with the rtc lock held.
  116. *
  117. * Other options:
  118. * - disable PREN so the sync happens at 32.768kHZ ... but this changes the
  119. * inc rate for all RTC registers from 1HZ to 32.768kHZ ...
  120. * - use the write complete IRQ
  121. */
  122. /*
  123. static void bfin_rtc_sync_pending_polled(void)
  124. {
  125. while (!(bfin_read_RTC_ISTAT() & RTC_ISTAT_WRITE_COMPLETE))
  126. if (!(bfin_read_RTC_ISTAT() & RTC_ISTAT_WRITE_PENDING))
  127. break;
  128. bfin_write_RTC_ISTAT(RTC_ISTAT_WRITE_COMPLETE);
  129. }
  130. */
  131. static DECLARE_COMPLETION(bfin_write_complete);
  132. static void bfin_rtc_sync_pending(struct device *dev)
  133. {
  134. dev_dbg_stamp(dev);
  135. while (bfin_read_RTC_ISTAT() & RTC_ISTAT_WRITE_PENDING)
  136. wait_for_completion_timeout(&bfin_write_complete, HZ * 5);
  137. dev_dbg_stamp(dev);
  138. }
  139. /**
  140. * bfin_rtc_reset - set RTC to sane/known state
  141. *
  142. * Initialize the RTC. Enable pre-scaler to scale RTC clock
  143. * to 1Hz and clear interrupt/status registers.
  144. */
  145. static void bfin_rtc_reset(struct device *dev, u16 rtc_ictl)
  146. {
  147. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  148. dev_dbg_stamp(dev);
  149. bfin_rtc_sync_pending(dev);
  150. bfin_write_RTC_PREN(0x1);
  151. bfin_write_RTC_ICTL(rtc_ictl);
  152. bfin_write_RTC_ALARM(0);
  153. bfin_write_RTC_ISTAT(0xFFFF);
  154. rtc->rtc_wrote_regs = 0;
  155. }
  156. /**
  157. * bfin_rtc_interrupt - handle interrupt from RTC
  158. *
  159. * Since we handle all RTC events here, we have to make sure the requested
  160. * interrupt is enabled (in RTC_ICTL) as the event status register (RTC_ISTAT)
  161. * always gets updated regardless of the interrupt being enabled. So when one
  162. * even we care about (e.g. stopwatch) goes off, we don't want to turn around
  163. * and say that other events have happened as well (e.g. second). We do not
  164. * have to worry about pending writes to the RTC_ICTL register as interrupts
  165. * only fire if they are enabled in the RTC_ICTL register.
  166. */
  167. static irqreturn_t bfin_rtc_interrupt(int irq, void *dev_id)
  168. {
  169. struct device *dev = dev_id;
  170. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  171. unsigned long events = 0;
  172. bool write_complete = false;
  173. u16 rtc_istat, rtc_ictl;
  174. dev_dbg_stamp(dev);
  175. rtc_istat = bfin_read_RTC_ISTAT();
  176. rtc_ictl = bfin_read_RTC_ICTL();
  177. if (rtc_istat & RTC_ISTAT_WRITE_COMPLETE) {
  178. bfin_write_RTC_ISTAT(RTC_ISTAT_WRITE_COMPLETE);
  179. write_complete = true;
  180. complete(&bfin_write_complete);
  181. }
  182. if (rtc_ictl & (RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY)) {
  183. if (rtc_istat & (RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY)) {
  184. bfin_write_RTC_ISTAT(RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY);
  185. events |= RTC_AF | RTC_IRQF;
  186. }
  187. }
  188. if (rtc_ictl & RTC_ISTAT_SEC) {
  189. if (rtc_istat & RTC_ISTAT_SEC) {
  190. bfin_write_RTC_ISTAT(RTC_ISTAT_SEC);
  191. events |= RTC_UF | RTC_IRQF;
  192. }
  193. }
  194. if (events)
  195. rtc_update_irq(rtc->rtc_dev, 1, events);
  196. if (write_complete || events)
  197. return IRQ_HANDLED;
  198. else
  199. return IRQ_NONE;
  200. }
  201. static int bfin_rtc_open(struct device *dev)
  202. {
  203. int ret;
  204. dev_dbg_stamp(dev);
  205. ret = request_irq(IRQ_RTC, bfin_rtc_interrupt, IRQF_SHARED, to_platform_device(dev)->name, dev);
  206. if (!ret)
  207. bfin_rtc_reset(dev, RTC_ISTAT_WRITE_COMPLETE);
  208. return ret;
  209. }
  210. static void bfin_rtc_release(struct device *dev)
  211. {
  212. dev_dbg_stamp(dev);
  213. bfin_rtc_reset(dev, 0);
  214. free_irq(IRQ_RTC, dev);
  215. }
  216. static void bfin_rtc_int_set(u16 rtc_int)
  217. {
  218. bfin_write_RTC_ISTAT(rtc_int);
  219. bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() | rtc_int);
  220. }
  221. static void bfin_rtc_int_clear(u16 rtc_int)
  222. {
  223. bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() & rtc_int);
  224. }
  225. static void bfin_rtc_int_set_alarm(struct bfin_rtc *rtc)
  226. {
  227. /* Blackfin has different bits for whether the alarm is
  228. * more than 24 hours away.
  229. */
  230. bfin_rtc_int_set(rtc->rtc_alarm.tm_yday == -1 ? RTC_ISTAT_ALARM : RTC_ISTAT_ALARM_DAY);
  231. }
  232. static int bfin_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  233. {
  234. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  235. int ret = 0;
  236. dev_dbg_stamp(dev);
  237. bfin_rtc_sync_pending(dev);
  238. switch (cmd) {
  239. case RTC_UIE_ON:
  240. dev_dbg_stamp(dev);
  241. bfin_rtc_int_set(RTC_ISTAT_SEC);
  242. break;
  243. case RTC_UIE_OFF:
  244. dev_dbg_stamp(dev);
  245. bfin_rtc_int_clear(~RTC_ISTAT_SEC);
  246. break;
  247. case RTC_AIE_ON:
  248. dev_dbg_stamp(dev);
  249. bfin_rtc_int_set_alarm(rtc);
  250. break;
  251. case RTC_AIE_OFF:
  252. dev_dbg_stamp(dev);
  253. bfin_rtc_int_clear(~(RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY));
  254. break;
  255. default:
  256. dev_dbg_stamp(dev);
  257. ret = -ENOIOCTLCMD;
  258. }
  259. return ret;
  260. }
  261. static int bfin_rtc_read_time(struct device *dev, struct rtc_time *tm)
  262. {
  263. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  264. dev_dbg_stamp(dev);
  265. if (rtc->rtc_wrote_regs & 0x1)
  266. bfin_rtc_sync_pending(dev);
  267. rtc_bfin_to_tm(bfin_read_RTC_STAT(), tm);
  268. return 0;
  269. }
  270. static int bfin_rtc_set_time(struct device *dev, struct rtc_time *tm)
  271. {
  272. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  273. int ret;
  274. unsigned long now;
  275. dev_dbg_stamp(dev);
  276. ret = rtc_tm_to_time(tm, &now);
  277. if (ret == 0) {
  278. if (rtc->rtc_wrote_regs & 0x1)
  279. bfin_rtc_sync_pending(dev);
  280. bfin_write_RTC_STAT(rtc_time_to_bfin(now));
  281. rtc->rtc_wrote_regs = 0x1;
  282. }
  283. return ret;
  284. }
  285. static int bfin_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  286. {
  287. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  288. dev_dbg_stamp(dev);
  289. alrm->time = rtc->rtc_alarm;
  290. bfin_rtc_sync_pending(dev);
  291. alrm->enabled = !!(bfin_read_RTC_ICTL() & (RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY));
  292. return 0;
  293. }
  294. static int bfin_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  295. {
  296. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  297. unsigned long rtc_alarm;
  298. dev_dbg_stamp(dev);
  299. if (rtc_tm_to_time(&alrm->time, &rtc_alarm))
  300. return -EINVAL;
  301. rtc->rtc_alarm = alrm->time;
  302. bfin_rtc_sync_pending(dev);
  303. bfin_write_RTC_ALARM(rtc_time_to_bfin(rtc_alarm));
  304. if (alrm->enabled)
  305. bfin_rtc_int_set_alarm(rtc);
  306. return 0;
  307. }
  308. static int bfin_rtc_proc(struct device *dev, struct seq_file *seq)
  309. {
  310. #define yesno(x) ((x) ? "yes" : "no")
  311. u16 ictl = bfin_read_RTC_ICTL();
  312. dev_dbg_stamp(dev);
  313. seq_printf(seq,
  314. "alarm_IRQ\t: %s\n"
  315. "wkalarm_IRQ\t: %s\n"
  316. "seconds_IRQ\t: %s\n",
  317. yesno(ictl & RTC_ISTAT_ALARM),
  318. yesno(ictl & RTC_ISTAT_ALARM_DAY),
  319. yesno(ictl & RTC_ISTAT_SEC));
  320. return 0;
  321. #undef yesno
  322. }
  323. static struct rtc_class_ops bfin_rtc_ops = {
  324. .open = bfin_rtc_open,
  325. .release = bfin_rtc_release,
  326. .ioctl = bfin_rtc_ioctl,
  327. .read_time = bfin_rtc_read_time,
  328. .set_time = bfin_rtc_set_time,
  329. .read_alarm = bfin_rtc_read_alarm,
  330. .set_alarm = bfin_rtc_set_alarm,
  331. .proc = bfin_rtc_proc,
  332. };
  333. static int __devinit bfin_rtc_probe(struct platform_device *pdev)
  334. {
  335. struct bfin_rtc *rtc;
  336. int ret = 0;
  337. dev_dbg_stamp(&pdev->dev);
  338. rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
  339. if (unlikely(!rtc))
  340. return -ENOMEM;
  341. rtc->rtc_dev = rtc_device_register(pdev->name, &pdev->dev, &bfin_rtc_ops, THIS_MODULE);
  342. if (IS_ERR(rtc)) {
  343. ret = PTR_ERR(rtc->rtc_dev);
  344. goto err;
  345. }
  346. /* see comment at top of file about stopwatch/PIE */
  347. bfin_write_RTC_SWCNT(0);
  348. platform_set_drvdata(pdev, rtc);
  349. device_init_wakeup(&pdev->dev, 1);
  350. return 0;
  351. err:
  352. kfree(rtc);
  353. return ret;
  354. }
  355. static int __devexit bfin_rtc_remove(struct platform_device *pdev)
  356. {
  357. struct bfin_rtc *rtc = platform_get_drvdata(pdev);
  358. rtc_device_unregister(rtc->rtc_dev);
  359. platform_set_drvdata(pdev, NULL);
  360. kfree(rtc);
  361. return 0;
  362. }
  363. #ifdef CONFIG_PM
  364. static int bfin_rtc_suspend(struct platform_device *pdev, pm_message_t state)
  365. {
  366. if (device_may_wakeup(&pdev->dev)) {
  367. enable_irq_wake(IRQ_RTC);
  368. bfin_rtc_sync_pending(&pdev->dev);
  369. } else
  370. bfin_rtc_int_clear(-1);
  371. return 0;
  372. }
  373. static int bfin_rtc_resume(struct platform_device *pdev)
  374. {
  375. if (device_may_wakeup(&pdev->dev))
  376. disable_irq_wake(IRQ_RTC);
  377. else
  378. bfin_write_RTC_ISTAT(-1);
  379. return 0;
  380. }
  381. #else
  382. # define bfin_rtc_suspend NULL
  383. # define bfin_rtc_resume NULL
  384. #endif
  385. static struct platform_driver bfin_rtc_driver = {
  386. .driver = {
  387. .name = "rtc-bfin",
  388. .owner = THIS_MODULE,
  389. },
  390. .probe = bfin_rtc_probe,
  391. .remove = __devexit_p(bfin_rtc_remove),
  392. .suspend = bfin_rtc_suspend,
  393. .resume = bfin_rtc_resume,
  394. };
  395. static int __init bfin_rtc_init(void)
  396. {
  397. return platform_driver_register(&bfin_rtc_driver);
  398. }
  399. static void __exit bfin_rtc_exit(void)
  400. {
  401. platform_driver_unregister(&bfin_rtc_driver);
  402. }
  403. module_init(bfin_rtc_init);
  404. module_exit(bfin_rtc_exit);
  405. MODULE_DESCRIPTION("Blackfin On-Chip Real Time Clock Driver");
  406. MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
  407. MODULE_LICENSE("GPL");
  408. MODULE_ALIAS("platform:rtc-bfin");