rtc-bfin.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 <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 void bfin_rtc_int_set(u16 rtc_int)
  202. {
  203. bfin_write_RTC_ISTAT(rtc_int);
  204. bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() | rtc_int);
  205. }
  206. static void bfin_rtc_int_clear(u16 rtc_int)
  207. {
  208. bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() & rtc_int);
  209. }
  210. static void bfin_rtc_int_set_alarm(struct bfin_rtc *rtc)
  211. {
  212. /* Blackfin has different bits for whether the alarm is
  213. * more than 24 hours away.
  214. */
  215. bfin_rtc_int_set(rtc->rtc_alarm.tm_yday == -1 ? RTC_ISTAT_ALARM : RTC_ISTAT_ALARM_DAY);
  216. }
  217. static int bfin_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  218. {
  219. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  220. int ret = 0;
  221. dev_dbg_stamp(dev);
  222. bfin_rtc_sync_pending(dev);
  223. switch (cmd) {
  224. case RTC_UIE_ON:
  225. dev_dbg_stamp(dev);
  226. bfin_rtc_int_set(RTC_ISTAT_SEC);
  227. break;
  228. case RTC_UIE_OFF:
  229. dev_dbg_stamp(dev);
  230. bfin_rtc_int_clear(~RTC_ISTAT_SEC);
  231. break;
  232. case RTC_AIE_ON:
  233. dev_dbg_stamp(dev);
  234. bfin_rtc_int_set_alarm(rtc);
  235. break;
  236. case RTC_AIE_OFF:
  237. dev_dbg_stamp(dev);
  238. bfin_rtc_int_clear(~(RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY));
  239. break;
  240. default:
  241. dev_dbg_stamp(dev);
  242. ret = -ENOIOCTLCMD;
  243. }
  244. return ret;
  245. }
  246. static int bfin_rtc_read_time(struct device *dev, struct rtc_time *tm)
  247. {
  248. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  249. dev_dbg_stamp(dev);
  250. if (rtc->rtc_wrote_regs & 0x1)
  251. bfin_rtc_sync_pending(dev);
  252. rtc_bfin_to_tm(bfin_read_RTC_STAT(), tm);
  253. return 0;
  254. }
  255. static int bfin_rtc_set_time(struct device *dev, struct rtc_time *tm)
  256. {
  257. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  258. int ret;
  259. unsigned long now;
  260. dev_dbg_stamp(dev);
  261. ret = rtc_tm_to_time(tm, &now);
  262. if (ret == 0) {
  263. if (rtc->rtc_wrote_regs & 0x1)
  264. bfin_rtc_sync_pending(dev);
  265. bfin_write_RTC_STAT(rtc_time_to_bfin(now));
  266. rtc->rtc_wrote_regs = 0x1;
  267. }
  268. return ret;
  269. }
  270. static int bfin_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  271. {
  272. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  273. dev_dbg_stamp(dev);
  274. alrm->time = rtc->rtc_alarm;
  275. bfin_rtc_sync_pending(dev);
  276. alrm->enabled = !!(bfin_read_RTC_ICTL() & (RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY));
  277. return 0;
  278. }
  279. static int bfin_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  280. {
  281. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  282. unsigned long rtc_alarm;
  283. dev_dbg_stamp(dev);
  284. if (rtc_tm_to_time(&alrm->time, &rtc_alarm))
  285. return -EINVAL;
  286. rtc->rtc_alarm = alrm->time;
  287. bfin_rtc_sync_pending(dev);
  288. bfin_write_RTC_ALARM(rtc_time_to_bfin(rtc_alarm));
  289. if (alrm->enabled)
  290. bfin_rtc_int_set_alarm(rtc);
  291. return 0;
  292. }
  293. static int bfin_rtc_proc(struct device *dev, struct seq_file *seq)
  294. {
  295. #define yesno(x) ((x) ? "yes" : "no")
  296. u16 ictl = bfin_read_RTC_ICTL();
  297. dev_dbg_stamp(dev);
  298. seq_printf(seq,
  299. "alarm_IRQ\t: %s\n"
  300. "wkalarm_IRQ\t: %s\n"
  301. "seconds_IRQ\t: %s\n",
  302. yesno(ictl & RTC_ISTAT_ALARM),
  303. yesno(ictl & RTC_ISTAT_ALARM_DAY),
  304. yesno(ictl & RTC_ISTAT_SEC));
  305. return 0;
  306. #undef yesno
  307. }
  308. static struct rtc_class_ops bfin_rtc_ops = {
  309. .ioctl = bfin_rtc_ioctl,
  310. .read_time = bfin_rtc_read_time,
  311. .set_time = bfin_rtc_set_time,
  312. .read_alarm = bfin_rtc_read_alarm,
  313. .set_alarm = bfin_rtc_set_alarm,
  314. .proc = bfin_rtc_proc,
  315. };
  316. static int __devinit bfin_rtc_probe(struct platform_device *pdev)
  317. {
  318. struct bfin_rtc *rtc;
  319. struct device *dev = &pdev->dev;
  320. int ret = 0;
  321. unsigned long timeout = jiffies + HZ;
  322. dev_dbg_stamp(dev);
  323. /* Allocate memory for our RTC struct */
  324. rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
  325. if (unlikely(!rtc))
  326. return -ENOMEM;
  327. platform_set_drvdata(pdev, rtc);
  328. device_init_wakeup(dev, 1);
  329. /* Register our RTC with the RTC framework */
  330. rtc->rtc_dev = rtc_device_register(pdev->name, dev, &bfin_rtc_ops,
  331. THIS_MODULE);
  332. if (unlikely(IS_ERR(rtc->rtc_dev))) {
  333. ret = PTR_ERR(rtc->rtc_dev);
  334. goto err;
  335. }
  336. /* Grab the IRQ and init the hardware */
  337. ret = request_irq(IRQ_RTC, bfin_rtc_interrupt, IRQF_SHARED, pdev->name, dev);
  338. if (unlikely(ret))
  339. goto err_reg;
  340. /* sometimes the bootloader touched things, but the write complete was not
  341. * enabled, so let's just do a quick timeout here since the IRQ will not fire ...
  342. */
  343. while (bfin_read_RTC_ISTAT() & RTC_ISTAT_WRITE_PENDING)
  344. if (time_after(jiffies, timeout))
  345. break;
  346. bfin_rtc_reset(dev, RTC_ISTAT_WRITE_COMPLETE);
  347. bfin_write_RTC_SWCNT(0);
  348. return 0;
  349. err_reg:
  350. rtc_device_unregister(rtc->rtc_dev);
  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. struct device *dev = &pdev->dev;
  359. bfin_rtc_reset(dev, 0);
  360. free_irq(IRQ_RTC, dev);
  361. rtc_device_unregister(rtc->rtc_dev);
  362. platform_set_drvdata(pdev, NULL);
  363. kfree(rtc);
  364. return 0;
  365. }
  366. #ifdef CONFIG_PM
  367. static int bfin_rtc_suspend(struct platform_device *pdev, pm_message_t state)
  368. {
  369. if (device_may_wakeup(&pdev->dev)) {
  370. enable_irq_wake(IRQ_RTC);
  371. bfin_rtc_sync_pending(&pdev->dev);
  372. } else
  373. bfin_rtc_int_clear(-1);
  374. return 0;
  375. }
  376. static int bfin_rtc_resume(struct platform_device *pdev)
  377. {
  378. if (device_may_wakeup(&pdev->dev))
  379. disable_irq_wake(IRQ_RTC);
  380. else
  381. bfin_write_RTC_ISTAT(-1);
  382. return 0;
  383. }
  384. #else
  385. # define bfin_rtc_suspend NULL
  386. # define bfin_rtc_resume NULL
  387. #endif
  388. static struct platform_driver bfin_rtc_driver = {
  389. .driver = {
  390. .name = "rtc-bfin",
  391. .owner = THIS_MODULE,
  392. },
  393. .probe = bfin_rtc_probe,
  394. .remove = __devexit_p(bfin_rtc_remove),
  395. .suspend = bfin_rtc_suspend,
  396. .resume = bfin_rtc_resume,
  397. };
  398. static int __init bfin_rtc_init(void)
  399. {
  400. return platform_driver_register(&bfin_rtc_driver);
  401. }
  402. static void __exit bfin_rtc_exit(void)
  403. {
  404. platform_driver_unregister(&bfin_rtc_driver);
  405. }
  406. module_init(bfin_rtc_init);
  407. module_exit(bfin_rtc_exit);
  408. MODULE_DESCRIPTION("Blackfin On-Chip Real Time Clock Driver");
  409. MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
  410. MODULE_LICENSE("GPL");
  411. MODULE_ALIAS("platform:rtc-bfin");