rtc-bfin.c 13 KB

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