rtc-bfin.c 13 KB

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