rtc-bfin.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * Blackfin On-Chip Real Time Clock Driver
  3. * Supports BF531/BF532/BF533/BF534/BF536/BF537
  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/module.h>
  34. #include <linux/kernel.h>
  35. #include <linux/bcd.h>
  36. #include <linux/rtc.h>
  37. #include <linux/init.h>
  38. #include <linux/platform_device.h>
  39. #include <linux/seq_file.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/delay.h>
  43. #include <asm/blackfin.h>
  44. #define stamp(fmt, args...) pr_debug("%s:%i: " fmt "\n", __FUNCTION__, __LINE__, ## args)
  45. #define stampit() stamp("here i am")
  46. struct bfin_rtc {
  47. struct rtc_device *rtc_dev;
  48. struct rtc_time rtc_alarm;
  49. spinlock_t lock;
  50. };
  51. /* Bit values for the ISTAT / ICTL registers */
  52. #define RTC_ISTAT_WRITE_COMPLETE 0x8000
  53. #define RTC_ISTAT_WRITE_PENDING 0x4000
  54. #define RTC_ISTAT_ALARM_DAY 0x0040
  55. #define RTC_ISTAT_24HR 0x0020
  56. #define RTC_ISTAT_HOUR 0x0010
  57. #define RTC_ISTAT_MIN 0x0008
  58. #define RTC_ISTAT_SEC 0x0004
  59. #define RTC_ISTAT_ALARM 0x0002
  60. #define RTC_ISTAT_STOPWATCH 0x0001
  61. /* Shift values for RTC_STAT register */
  62. #define DAY_BITS_OFF 17
  63. #define HOUR_BITS_OFF 12
  64. #define MIN_BITS_OFF 6
  65. #define SEC_BITS_OFF 0
  66. /* Some helper functions to convert between the common RTC notion of time
  67. * and the internal Blackfin notion that is stored in 32bits.
  68. */
  69. static inline u32 rtc_time_to_bfin(unsigned long now)
  70. {
  71. u32 sec = (now % 60);
  72. u32 min = (now % (60 * 60)) / 60;
  73. u32 hour = (now % (60 * 60 * 24)) / (60 * 60);
  74. u32 days = (now / (60 * 60 * 24));
  75. return (sec << SEC_BITS_OFF) +
  76. (min << MIN_BITS_OFF) +
  77. (hour << HOUR_BITS_OFF) +
  78. (days << DAY_BITS_OFF);
  79. }
  80. static inline unsigned long rtc_bfin_to_time(u32 rtc_bfin)
  81. {
  82. return (((rtc_bfin >> SEC_BITS_OFF) & 0x003F)) +
  83. (((rtc_bfin >> MIN_BITS_OFF) & 0x003F) * 60) +
  84. (((rtc_bfin >> HOUR_BITS_OFF) & 0x001F) * 60 * 60) +
  85. (((rtc_bfin >> DAY_BITS_OFF) & 0x7FFF) * 60 * 60 * 24);
  86. }
  87. static inline void rtc_bfin_to_tm(u32 rtc_bfin, struct rtc_time *tm)
  88. {
  89. rtc_time_to_tm(rtc_bfin_to_time(rtc_bfin), tm);
  90. }
  91. /* Wait for the previous write to a RTC register to complete.
  92. * Unfortunately, we can't sleep here as that introduces a race condition when
  93. * turning on interrupt events. Consider this:
  94. * - process sets alarm
  95. * - process enables alarm
  96. * - process sleeps while waiting for rtc write to sync
  97. * - interrupt fires while process is sleeping
  98. * - interrupt acks the event by writing to ISTAT
  99. * - interrupt sets the WRITE PENDING bit
  100. * - interrupt handler finishes
  101. * - process wakes up, sees WRITE PENDING bit set, goes to sleep
  102. * - interrupt fires while process is sleeping
  103. * If anyone can point out the obvious solution here, i'm listening :). This
  104. * shouldn't be an issue on an SMP or preempt system as this function should
  105. * only be called with the rtc lock held.
  106. */
  107. static void rtc_bfin_sync_pending(void)
  108. {
  109. stampit();
  110. while (!(bfin_read_RTC_ISTAT() & RTC_ISTAT_WRITE_COMPLETE)) {
  111. if (!(bfin_read_RTC_ISTAT() & RTC_ISTAT_WRITE_PENDING))
  112. break;
  113. }
  114. bfin_write_RTC_ISTAT(RTC_ISTAT_WRITE_COMPLETE);
  115. }
  116. static void rtc_bfin_reset(struct bfin_rtc *rtc)
  117. {
  118. /* Initialize the RTC. Enable pre-scaler to scale RTC clock
  119. * to 1Hz and clear interrupt/status registers. */
  120. spin_lock_irq(&rtc->lock);
  121. rtc_bfin_sync_pending();
  122. bfin_write_RTC_PREN(0x1);
  123. bfin_write_RTC_ICTL(0);
  124. bfin_write_RTC_SWCNT(0);
  125. bfin_write_RTC_ALARM(0);
  126. bfin_write_RTC_ISTAT(0xFFFF);
  127. spin_unlock_irq(&rtc->lock);
  128. }
  129. static irqreturn_t bfin_rtc_interrupt(int irq, void *dev_id)
  130. {
  131. struct platform_device *pdev = to_platform_device(dev_id);
  132. struct bfin_rtc *rtc = platform_get_drvdata(pdev);
  133. unsigned long events = 0;
  134. u16 rtc_istat;
  135. stampit();
  136. spin_lock_irq(&rtc->lock);
  137. rtc_istat = bfin_read_RTC_ISTAT();
  138. if (rtc_istat & (RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY)) {
  139. bfin_write_RTC_ISTAT(RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY);
  140. events |= RTC_AF | RTC_IRQF;
  141. }
  142. if (rtc_istat & RTC_ISTAT_STOPWATCH) {
  143. bfin_write_RTC_ISTAT(RTC_ISTAT_STOPWATCH);
  144. events |= RTC_PF | RTC_IRQF;
  145. bfin_write_RTC_SWCNT(rtc->rtc_dev->irq_freq);
  146. }
  147. if (rtc_istat & RTC_ISTAT_SEC) {
  148. bfin_write_RTC_ISTAT(RTC_ISTAT_SEC);
  149. events |= RTC_UF | RTC_IRQF;
  150. }
  151. rtc_update_irq(rtc->rtc_dev, 1, events);
  152. spin_unlock_irq(&rtc->lock);
  153. return IRQ_HANDLED;
  154. }
  155. static int bfin_rtc_open(struct device *dev)
  156. {
  157. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  158. int ret;
  159. stampit();
  160. ret = request_irq(IRQ_RTC, bfin_rtc_interrupt, IRQF_DISABLED, "rtc-bfin", dev);
  161. if (unlikely(ret)) {
  162. dev_err(dev, "request RTC IRQ failed with %d\n", ret);
  163. return ret;
  164. }
  165. rtc_bfin_reset(rtc);
  166. return ret;
  167. }
  168. static void bfin_rtc_release(struct device *dev)
  169. {
  170. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  171. stampit();
  172. rtc_bfin_reset(rtc);
  173. free_irq(IRQ_RTC, dev);
  174. }
  175. static int bfin_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  176. {
  177. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  178. stampit();
  179. switch (cmd) {
  180. case RTC_PIE_ON:
  181. stampit();
  182. spin_lock_irq(&rtc->lock);
  183. rtc_bfin_sync_pending();
  184. bfin_write_RTC_ISTAT(RTC_ISTAT_STOPWATCH);
  185. bfin_write_RTC_SWCNT(rtc->rtc_dev->irq_freq);
  186. bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() | RTC_ISTAT_STOPWATCH);
  187. spin_unlock_irq(&rtc->lock);
  188. return 0;
  189. case RTC_PIE_OFF:
  190. stampit();
  191. spin_lock_irq(&rtc->lock);
  192. rtc_bfin_sync_pending();
  193. bfin_write_RTC_SWCNT(0);
  194. bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() & ~RTC_ISTAT_STOPWATCH);
  195. spin_unlock_irq(&rtc->lock);
  196. return 0;
  197. case RTC_UIE_ON:
  198. stampit();
  199. spin_lock_irq(&rtc->lock);
  200. rtc_bfin_sync_pending();
  201. bfin_write_RTC_ISTAT(RTC_ISTAT_SEC);
  202. bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() | RTC_ISTAT_SEC);
  203. spin_unlock_irq(&rtc->lock);
  204. return 0;
  205. case RTC_UIE_OFF:
  206. stampit();
  207. spin_lock_irq(&rtc->lock);
  208. rtc_bfin_sync_pending();
  209. bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() & ~RTC_ISTAT_SEC);
  210. spin_unlock_irq(&rtc->lock);
  211. return 0;
  212. case RTC_AIE_ON: {
  213. unsigned long rtc_alarm;
  214. u16 which_alarm;
  215. int ret = 0;
  216. stampit();
  217. spin_lock_irq(&rtc->lock);
  218. rtc_bfin_sync_pending();
  219. if (rtc->rtc_alarm.tm_yday == -1) {
  220. struct rtc_time now;
  221. rtc_bfin_to_tm(bfin_read_RTC_STAT(), &now);
  222. now.tm_sec = rtc->rtc_alarm.tm_sec;
  223. now.tm_min = rtc->rtc_alarm.tm_min;
  224. now.tm_hour = rtc->rtc_alarm.tm_hour;
  225. ret = rtc_tm_to_time(&now, &rtc_alarm);
  226. which_alarm = RTC_ISTAT_ALARM;
  227. } else {
  228. ret = rtc_tm_to_time(&rtc->rtc_alarm, &rtc_alarm);
  229. which_alarm = RTC_ISTAT_ALARM_DAY;
  230. }
  231. if (ret == 0) {
  232. bfin_write_RTC_ISTAT(which_alarm);
  233. bfin_write_RTC_ALARM(rtc_time_to_bfin(rtc_alarm));
  234. bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() | which_alarm);
  235. }
  236. spin_unlock_irq(&rtc->lock);
  237. return ret;
  238. }
  239. case RTC_AIE_OFF:
  240. stampit();
  241. spin_lock_irq(&rtc->lock);
  242. rtc_bfin_sync_pending();
  243. bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() & ~(RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY));
  244. spin_unlock_irq(&rtc->lock);
  245. return 0;
  246. }
  247. return -ENOIOCTLCMD;
  248. }
  249. static int bfin_rtc_read_time(struct device *dev, struct rtc_time *tm)
  250. {
  251. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  252. stampit();
  253. spin_lock_irq(&rtc->lock);
  254. rtc_bfin_sync_pending();
  255. rtc_bfin_to_tm(bfin_read_RTC_STAT(), tm);
  256. spin_unlock_irq(&rtc->lock);
  257. return 0;
  258. }
  259. static int bfin_rtc_set_time(struct device *dev, struct rtc_time *tm)
  260. {
  261. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  262. int ret;
  263. unsigned long now;
  264. stampit();
  265. spin_lock_irq(&rtc->lock);
  266. ret = rtc_tm_to_time(tm, &now);
  267. if (ret == 0) {
  268. rtc_bfin_sync_pending();
  269. bfin_write_RTC_STAT(rtc_time_to_bfin(now));
  270. }
  271. spin_unlock_irq(&rtc->lock);
  272. return ret;
  273. }
  274. static int bfin_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  275. {
  276. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  277. stampit();
  278. memcpy(&alrm->time, &rtc->rtc_alarm, sizeof(struct rtc_time));
  279. alrm->pending = !!(bfin_read_RTC_ICTL() & (RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY));
  280. return 0;
  281. }
  282. static int bfin_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  283. {
  284. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  285. stampit();
  286. memcpy(&rtc->rtc_alarm, &alrm->time, sizeof(struct rtc_time));
  287. return 0;
  288. }
  289. static int bfin_rtc_proc(struct device *dev, struct seq_file *seq)
  290. {
  291. #define yesno(x) (x ? "yes" : "no")
  292. u16 ictl = bfin_read_RTC_ICTL();
  293. stampit();
  294. seq_printf(seq, "alarm_IRQ\t: %s\n", yesno(ictl & RTC_ISTAT_ALARM));
  295. seq_printf(seq, "wkalarm_IRQ\t: %s\n", yesno(ictl & RTC_ISTAT_ALARM_DAY));
  296. seq_printf(seq, "seconds_IRQ\t: %s\n", yesno(ictl & RTC_ISTAT_SEC));
  297. seq_printf(seq, "periodic_IRQ\t: %s\n", yesno(ictl & RTC_ISTAT_STOPWATCH));
  298. #ifdef DEBUG
  299. seq_printf(seq, "RTC_STAT\t: 0x%08X\n", bfin_read_RTC_STAT());
  300. seq_printf(seq, "RTC_ICTL\t: 0x%04X\n", bfin_read_RTC_ICTL());
  301. seq_printf(seq, "RTC_ISTAT\t: 0x%04X\n", bfin_read_RTC_ISTAT());
  302. seq_printf(seq, "RTC_SWCNT\t: 0x%04X\n", bfin_read_RTC_SWCNT());
  303. seq_printf(seq, "RTC_ALARM\t: 0x%08X\n", bfin_read_RTC_ALARM());
  304. seq_printf(seq, "RTC_PREN\t: 0x%04X\n", bfin_read_RTC_PREN());
  305. #endif
  306. return 0;
  307. }
  308. static int bfin_irq_set_freq(struct device *dev, int freq)
  309. {
  310. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  311. stampit();
  312. rtc->rtc_dev->irq_freq = freq;
  313. return 0;
  314. }
  315. static struct rtc_class_ops bfin_rtc_ops = {
  316. .open = bfin_rtc_open,
  317. .release = bfin_rtc_release,
  318. .ioctl = bfin_rtc_ioctl,
  319. .read_time = bfin_rtc_read_time,
  320. .set_time = bfin_rtc_set_time,
  321. .read_alarm = bfin_rtc_read_alarm,
  322. .set_alarm = bfin_rtc_set_alarm,
  323. .proc = bfin_rtc_proc,
  324. .irq_set_freq = bfin_irq_set_freq,
  325. };
  326. static int __devinit bfin_rtc_probe(struct platform_device *pdev)
  327. {
  328. struct bfin_rtc *rtc;
  329. int ret = 0;
  330. stampit();
  331. rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
  332. if (unlikely(!rtc))
  333. return -ENOMEM;
  334. spin_lock_init(&rtc->lock);
  335. rtc->rtc_dev = rtc_device_register(pdev->name, &pdev->dev, &bfin_rtc_ops, THIS_MODULE);
  336. if (unlikely(IS_ERR(rtc))) {
  337. ret = PTR_ERR(rtc->rtc_dev);
  338. goto err;
  339. }
  340. rtc->rtc_dev->irq_freq = 0;
  341. rtc->rtc_dev->max_user_freq = (2 << 16); /* stopwatch is an unsigned 16 bit reg */
  342. platform_set_drvdata(pdev, rtc);
  343. return 0;
  344. err:
  345. kfree(rtc);
  346. return ret;
  347. }
  348. static int __devexit bfin_rtc_remove(struct platform_device *pdev)
  349. {
  350. struct bfin_rtc *rtc = platform_get_drvdata(pdev);
  351. rtc_device_unregister(rtc->rtc_dev);
  352. platform_set_drvdata(pdev, NULL);
  353. kfree(rtc);
  354. return 0;
  355. }
  356. static struct platform_driver bfin_rtc_driver = {
  357. .driver = {
  358. .name = "rtc-bfin",
  359. .owner = THIS_MODULE,
  360. },
  361. .probe = bfin_rtc_probe,
  362. .remove = __devexit_p(bfin_rtc_remove),
  363. };
  364. static int __init bfin_rtc_init(void)
  365. {
  366. stampit();
  367. return platform_driver_register(&bfin_rtc_driver);
  368. }
  369. static void __exit bfin_rtc_exit(void)
  370. {
  371. platform_driver_unregister(&bfin_rtc_driver);
  372. }
  373. module_init(bfin_rtc_init);
  374. module_exit(bfin_rtc_exit);
  375. MODULE_DESCRIPTION("Blackfin On-Chip Real Time Clock Driver");
  376. MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
  377. MODULE_LICENSE("GPL");