rtc-bfin.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Blackfin On-Chip Real Time Clock Driver
  3. * Supports BF51x/BF52x/BF53[123]/BF53[467]/BF54x
  4. *
  5. * Copyright 2004-2010 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 every time 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 doesn't 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_istat_clear, rtc_ictl, bits;
  175. dev_dbg_stamp(dev);
  176. rtc_istat = bfin_read_RTC_ISTAT();
  177. rtc_ictl = bfin_read_RTC_ICTL();
  178. rtc_istat_clear = 0;
  179. bits = RTC_ISTAT_WRITE_COMPLETE;
  180. if (rtc_istat & bits) {
  181. rtc_istat_clear |= bits;
  182. write_complete = true;
  183. complete(&bfin_write_complete);
  184. }
  185. bits = (RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY);
  186. if (rtc_ictl & bits) {
  187. if (rtc_istat & bits) {
  188. rtc_istat_clear |= bits;
  189. events |= RTC_AF | RTC_IRQF;
  190. }
  191. }
  192. bits = RTC_ISTAT_SEC;
  193. if (rtc_ictl & bits) {
  194. if (rtc_istat & bits) {
  195. rtc_istat_clear |= bits;
  196. events |= RTC_UF | RTC_IRQF;
  197. }
  198. }
  199. if (events)
  200. rtc_update_irq(rtc->rtc_dev, 1, events);
  201. if (write_complete || events) {
  202. bfin_write_RTC_ISTAT(rtc_istat_clear);
  203. return IRQ_HANDLED;
  204. } else
  205. return IRQ_NONE;
  206. }
  207. static void bfin_rtc_int_set(u16 rtc_int)
  208. {
  209. bfin_write_RTC_ISTAT(rtc_int);
  210. bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() | rtc_int);
  211. }
  212. static void bfin_rtc_int_clear(u16 rtc_int)
  213. {
  214. bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() & rtc_int);
  215. }
  216. static void bfin_rtc_int_set_alarm(struct bfin_rtc *rtc)
  217. {
  218. /* Blackfin has different bits for whether the alarm is
  219. * more than 24 hours away.
  220. */
  221. bfin_rtc_int_set(rtc->rtc_alarm.tm_yday == -1 ? RTC_ISTAT_ALARM : RTC_ISTAT_ALARM_DAY);
  222. }
  223. static int bfin_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  224. {
  225. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  226. dev_dbg_stamp(dev);
  227. if (enabled)
  228. bfin_rtc_int_set_alarm(rtc);
  229. else
  230. bfin_rtc_int_clear(~(RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY));
  231. return 0;
  232. }
  233. static int bfin_rtc_read_time(struct device *dev, struct rtc_time *tm)
  234. {
  235. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  236. dev_dbg_stamp(dev);
  237. if (rtc->rtc_wrote_regs & 0x1)
  238. bfin_rtc_sync_pending(dev);
  239. rtc_bfin_to_tm(bfin_read_RTC_STAT(), tm);
  240. return 0;
  241. }
  242. static int bfin_rtc_set_time(struct device *dev, struct rtc_time *tm)
  243. {
  244. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  245. int ret;
  246. unsigned long now;
  247. dev_dbg_stamp(dev);
  248. ret = rtc_tm_to_time(tm, &now);
  249. if (ret == 0) {
  250. if (rtc->rtc_wrote_regs & 0x1)
  251. bfin_rtc_sync_pending(dev);
  252. bfin_write_RTC_STAT(rtc_time_to_bfin(now));
  253. rtc->rtc_wrote_regs = 0x1;
  254. }
  255. return ret;
  256. }
  257. static int bfin_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  258. {
  259. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  260. dev_dbg_stamp(dev);
  261. alrm->time = rtc->rtc_alarm;
  262. bfin_rtc_sync_pending(dev);
  263. alrm->enabled = !!(bfin_read_RTC_ICTL() & (RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY));
  264. return 0;
  265. }
  266. static int bfin_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  267. {
  268. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  269. unsigned long rtc_alarm;
  270. dev_dbg_stamp(dev);
  271. if (rtc_tm_to_time(&alrm->time, &rtc_alarm))
  272. return -EINVAL;
  273. rtc->rtc_alarm = alrm->time;
  274. bfin_rtc_sync_pending(dev);
  275. bfin_write_RTC_ALARM(rtc_time_to_bfin(rtc_alarm));
  276. if (alrm->enabled)
  277. bfin_rtc_int_set_alarm(rtc);
  278. return 0;
  279. }
  280. static int bfin_rtc_proc(struct device *dev, struct seq_file *seq)
  281. {
  282. #define yesno(x) ((x) ? "yes" : "no")
  283. u16 ictl = bfin_read_RTC_ICTL();
  284. dev_dbg_stamp(dev);
  285. seq_printf(seq,
  286. "alarm_IRQ\t: %s\n"
  287. "wkalarm_IRQ\t: %s\n"
  288. "seconds_IRQ\t: %s\n",
  289. yesno(ictl & RTC_ISTAT_ALARM),
  290. yesno(ictl & RTC_ISTAT_ALARM_DAY),
  291. yesno(ictl & RTC_ISTAT_SEC));
  292. return 0;
  293. #undef yesno
  294. }
  295. static struct rtc_class_ops bfin_rtc_ops = {
  296. .read_time = bfin_rtc_read_time,
  297. .set_time = bfin_rtc_set_time,
  298. .read_alarm = bfin_rtc_read_alarm,
  299. .set_alarm = bfin_rtc_set_alarm,
  300. .proc = bfin_rtc_proc,
  301. .alarm_irq_enable = bfin_rtc_alarm_irq_enable,
  302. };
  303. static int bfin_rtc_probe(struct platform_device *pdev)
  304. {
  305. struct bfin_rtc *rtc;
  306. struct device *dev = &pdev->dev;
  307. int ret;
  308. unsigned long timeout = jiffies + HZ;
  309. dev_dbg_stamp(dev);
  310. /* Allocate memory for our RTC struct */
  311. rtc = devm_kzalloc(dev, sizeof(*rtc), GFP_KERNEL);
  312. if (unlikely(!rtc))
  313. return -ENOMEM;
  314. platform_set_drvdata(pdev, rtc);
  315. device_init_wakeup(dev, 1);
  316. /* Register our RTC with the RTC framework */
  317. rtc->rtc_dev = devm_rtc_device_register(dev, pdev->name, &bfin_rtc_ops,
  318. THIS_MODULE);
  319. if (IS_ERR(rtc->rtc_dev))
  320. return PTR_ERR(rtc->rtc_dev);
  321. /* Grab the IRQ and init the hardware */
  322. ret = devm_request_irq(dev, IRQ_RTC, bfin_rtc_interrupt, 0,
  323. pdev->name, dev);
  324. if (unlikely(ret))
  325. dev_err(&pdev->dev,
  326. "unable to request IRQ; alarm won't work, "
  327. "and writes will be delayed\n");
  328. /* sometimes the bootloader touched things, but the write complete was not
  329. * enabled, so let's just do a quick timeout here since the IRQ will not fire ...
  330. */
  331. while (bfin_read_RTC_ISTAT() & RTC_ISTAT_WRITE_PENDING)
  332. if (time_after(jiffies, timeout))
  333. break;
  334. bfin_rtc_reset(dev, RTC_ISTAT_WRITE_COMPLETE);
  335. bfin_write_RTC_SWCNT(0);
  336. return 0;
  337. }
  338. static int bfin_rtc_remove(struct platform_device *pdev)
  339. {
  340. struct device *dev = &pdev->dev;
  341. bfin_rtc_reset(dev, 0);
  342. return 0;
  343. }
  344. #ifdef CONFIG_PM_SLEEP
  345. static int bfin_rtc_suspend(struct device *dev)
  346. {
  347. dev_dbg_stamp(dev);
  348. if (device_may_wakeup(dev)) {
  349. enable_irq_wake(IRQ_RTC);
  350. bfin_rtc_sync_pending(dev);
  351. } else
  352. bfin_rtc_int_clear(0);
  353. return 0;
  354. }
  355. static int bfin_rtc_resume(struct device *dev)
  356. {
  357. dev_dbg_stamp(dev);
  358. if (device_may_wakeup(dev))
  359. disable_irq_wake(IRQ_RTC);
  360. /*
  361. * Since only some of the RTC bits are maintained externally in the
  362. * Vbat domain, we need to wait for the RTC MMRs to be synced into
  363. * the core after waking up. This happens every RTC 1HZ. Once that
  364. * has happened, we can go ahead and re-enable the important write
  365. * complete interrupt event.
  366. */
  367. while (!(bfin_read_RTC_ISTAT() & RTC_ISTAT_SEC))
  368. continue;
  369. bfin_rtc_int_set(RTC_ISTAT_WRITE_COMPLETE);
  370. return 0;
  371. }
  372. #endif
  373. static SIMPLE_DEV_PM_OPS(bfin_rtc_pm_ops, bfin_rtc_suspend, bfin_rtc_resume);
  374. static struct platform_driver bfin_rtc_driver = {
  375. .driver = {
  376. .name = "rtc-bfin",
  377. .pm = &bfin_rtc_pm_ops,
  378. },
  379. .probe = bfin_rtc_probe,
  380. .remove = bfin_rtc_remove,
  381. };
  382. module_platform_driver(bfin_rtc_driver);
  383. MODULE_DESCRIPTION("Blackfin On-Chip Real Time Clock Driver");
  384. MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
  385. MODULE_LICENSE("GPL");
  386. MODULE_ALIAS("platform:rtc-bfin");