rtc-bfin.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Blackfin On-Chip Real Time Clock Driver
  3. * Supports BF53[123]/BF53[467]/BF54[2489]
  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 encoded 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. * Other options:
  108. * - disable PREN so the sync happens at 32.768kHZ ... but this changes the
  109. * inc rate for all RTC registers from 1HZ to 32.768kHZ ...
  110. * - use the write complete IRQ
  111. */
  112. static void rtc_bfin_sync_pending(void)
  113. {
  114. stampit();
  115. while (!(bfin_read_RTC_ISTAT() & RTC_ISTAT_WRITE_COMPLETE)) {
  116. if (!(bfin_read_RTC_ISTAT() & RTC_ISTAT_WRITE_PENDING))
  117. break;
  118. }
  119. bfin_write_RTC_ISTAT(RTC_ISTAT_WRITE_COMPLETE);
  120. }
  121. static void rtc_bfin_reset(struct bfin_rtc *rtc)
  122. {
  123. /* Initialize the RTC. Enable pre-scaler to scale RTC clock
  124. * to 1Hz and clear interrupt/status registers. */
  125. spin_lock_irq(&rtc->lock);
  126. rtc_bfin_sync_pending();
  127. bfin_write_RTC_PREN(0x1);
  128. bfin_write_RTC_ICTL(0);
  129. bfin_write_RTC_SWCNT(0);
  130. bfin_write_RTC_ALARM(0);
  131. bfin_write_RTC_ISTAT(0xFFFF);
  132. spin_unlock_irq(&rtc->lock);
  133. }
  134. static irqreturn_t bfin_rtc_interrupt(int irq, void *dev_id)
  135. {
  136. struct platform_device *pdev = to_platform_device(dev_id);
  137. struct bfin_rtc *rtc = platform_get_drvdata(pdev);
  138. unsigned long events = 0;
  139. u16 rtc_istat;
  140. stampit();
  141. spin_lock_irq(&rtc->lock);
  142. rtc_istat = bfin_read_RTC_ISTAT();
  143. if (rtc_istat & (RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY)) {
  144. bfin_write_RTC_ISTAT(RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY);
  145. events |= RTC_AF | RTC_IRQF;
  146. }
  147. if (rtc_istat & RTC_ISTAT_STOPWATCH) {
  148. bfin_write_RTC_ISTAT(RTC_ISTAT_STOPWATCH);
  149. events |= RTC_PF | RTC_IRQF;
  150. bfin_write_RTC_SWCNT(rtc->rtc_dev->irq_freq);
  151. }
  152. if (rtc_istat & RTC_ISTAT_SEC) {
  153. bfin_write_RTC_ISTAT(RTC_ISTAT_SEC);
  154. events |= RTC_UF | RTC_IRQF;
  155. }
  156. rtc_update_irq(rtc->rtc_dev, 1, events);
  157. spin_unlock_irq(&rtc->lock);
  158. return IRQ_HANDLED;
  159. }
  160. static int bfin_rtc_open(struct device *dev)
  161. {
  162. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  163. int ret;
  164. stampit();
  165. ret = request_irq(IRQ_RTC, bfin_rtc_interrupt, IRQF_DISABLED, "rtc-bfin", dev);
  166. if (unlikely(ret)) {
  167. dev_err(dev, "request RTC IRQ failed with %d\n", ret);
  168. return ret;
  169. }
  170. rtc_bfin_reset(rtc);
  171. return ret;
  172. }
  173. static void bfin_rtc_release(struct device *dev)
  174. {
  175. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  176. stampit();
  177. rtc_bfin_reset(rtc);
  178. free_irq(IRQ_RTC, dev);
  179. }
  180. static int bfin_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  181. {
  182. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  183. stampit();
  184. switch (cmd) {
  185. case RTC_PIE_ON:
  186. stampit();
  187. spin_lock_irq(&rtc->lock);
  188. rtc_bfin_sync_pending();
  189. bfin_write_RTC_ISTAT(RTC_ISTAT_STOPWATCH);
  190. bfin_write_RTC_SWCNT(rtc->rtc_dev->irq_freq);
  191. bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() | RTC_ISTAT_STOPWATCH);
  192. spin_unlock_irq(&rtc->lock);
  193. return 0;
  194. case RTC_PIE_OFF:
  195. stampit();
  196. spin_lock_irq(&rtc->lock);
  197. rtc_bfin_sync_pending();
  198. bfin_write_RTC_SWCNT(0);
  199. bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() & ~RTC_ISTAT_STOPWATCH);
  200. spin_unlock_irq(&rtc->lock);
  201. return 0;
  202. case RTC_UIE_ON:
  203. stampit();
  204. spin_lock_irq(&rtc->lock);
  205. rtc_bfin_sync_pending();
  206. bfin_write_RTC_ISTAT(RTC_ISTAT_SEC);
  207. bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() | RTC_ISTAT_SEC);
  208. spin_unlock_irq(&rtc->lock);
  209. return 0;
  210. case RTC_UIE_OFF:
  211. stampit();
  212. spin_lock_irq(&rtc->lock);
  213. rtc_bfin_sync_pending();
  214. bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() & ~RTC_ISTAT_SEC);
  215. spin_unlock_irq(&rtc->lock);
  216. return 0;
  217. case RTC_AIE_ON: {
  218. unsigned long rtc_alarm;
  219. u16 which_alarm;
  220. int ret = 0;
  221. stampit();
  222. spin_lock_irq(&rtc->lock);
  223. rtc_bfin_sync_pending();
  224. if (rtc->rtc_alarm.tm_yday == -1) {
  225. struct rtc_time now;
  226. rtc_bfin_to_tm(bfin_read_RTC_STAT(), &now);
  227. now.tm_sec = rtc->rtc_alarm.tm_sec;
  228. now.tm_min = rtc->rtc_alarm.tm_min;
  229. now.tm_hour = rtc->rtc_alarm.tm_hour;
  230. ret = rtc_tm_to_time(&now, &rtc_alarm);
  231. which_alarm = RTC_ISTAT_ALARM;
  232. } else {
  233. ret = rtc_tm_to_time(&rtc->rtc_alarm, &rtc_alarm);
  234. which_alarm = RTC_ISTAT_ALARM_DAY;
  235. }
  236. if (ret == 0) {
  237. bfin_write_RTC_ISTAT(which_alarm);
  238. bfin_write_RTC_ALARM(rtc_time_to_bfin(rtc_alarm));
  239. bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() | which_alarm);
  240. }
  241. spin_unlock_irq(&rtc->lock);
  242. return ret;
  243. }
  244. case RTC_AIE_OFF:
  245. stampit();
  246. spin_lock_irq(&rtc->lock);
  247. rtc_bfin_sync_pending();
  248. bfin_write_RTC_ICTL(bfin_read_RTC_ICTL() & ~(RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY));
  249. spin_unlock_irq(&rtc->lock);
  250. return 0;
  251. }
  252. return -ENOIOCTLCMD;
  253. }
  254. static int bfin_rtc_read_time(struct device *dev, struct rtc_time *tm)
  255. {
  256. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  257. stampit();
  258. spin_lock_irq(&rtc->lock);
  259. rtc_bfin_sync_pending();
  260. rtc_bfin_to_tm(bfin_read_RTC_STAT(), tm);
  261. spin_unlock_irq(&rtc->lock);
  262. return 0;
  263. }
  264. static int bfin_rtc_set_time(struct device *dev, struct rtc_time *tm)
  265. {
  266. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  267. int ret;
  268. unsigned long now;
  269. stampit();
  270. spin_lock_irq(&rtc->lock);
  271. ret = rtc_tm_to_time(tm, &now);
  272. if (ret == 0) {
  273. rtc_bfin_sync_pending();
  274. bfin_write_RTC_STAT(rtc_time_to_bfin(now));
  275. }
  276. spin_unlock_irq(&rtc->lock);
  277. return ret;
  278. }
  279. static int bfin_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  280. {
  281. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  282. stampit();
  283. memcpy(&alrm->time, &rtc->rtc_alarm, sizeof(struct rtc_time));
  284. alrm->pending = !!(bfin_read_RTC_ICTL() & (RTC_ISTAT_ALARM | RTC_ISTAT_ALARM_DAY));
  285. return 0;
  286. }
  287. static int bfin_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  288. {
  289. struct bfin_rtc *rtc = dev_get_drvdata(dev);
  290. stampit();
  291. memcpy(&rtc->rtc_alarm, &alrm->time, sizeof(struct rtc_time));
  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. stampit();
  299. seq_printf(seq, "alarm_IRQ\t: %s\n", yesno(ictl & RTC_ISTAT_ALARM));
  300. seq_printf(seq, "wkalarm_IRQ\t: %s\n", yesno(ictl & RTC_ISTAT_ALARM_DAY));
  301. seq_printf(seq, "seconds_IRQ\t: %s\n", yesno(ictl & RTC_ISTAT_SEC));
  302. seq_printf(seq, "periodic_IRQ\t: %s\n", yesno(ictl & RTC_ISTAT_STOPWATCH));
  303. #ifdef DEBUG
  304. seq_printf(seq, "RTC_STAT\t: 0x%08X\n", bfin_read_RTC_STAT());
  305. seq_printf(seq, "RTC_ICTL\t: 0x%04X\n", bfin_read_RTC_ICTL());
  306. seq_printf(seq, "RTC_ISTAT\t: 0x%04X\n", bfin_read_RTC_ISTAT());
  307. seq_printf(seq, "RTC_SWCNT\t: 0x%04X\n", bfin_read_RTC_SWCNT());
  308. seq_printf(seq, "RTC_ALARM\t: 0x%08X\n", bfin_read_RTC_ALARM());
  309. seq_printf(seq, "RTC_PREN\t: 0x%04X\n", bfin_read_RTC_PREN());
  310. #endif
  311. return 0;
  312. }
  313. /**
  314. * bfin_irq_set_freq - make sure hardware supports requested freq
  315. * @dev: pointer to RTC device structure
  316. * @freq: requested frequency rate
  317. *
  318. * The Blackfin RTC can only generate periodic events at 1 per
  319. * second (1 Hz), so reject any attempt at changing it.
  320. */
  321. static int bfin_irq_set_freq(struct device *dev, int freq)
  322. {
  323. stampit();
  324. return -ENOTTY;
  325. }
  326. static struct rtc_class_ops bfin_rtc_ops = {
  327. .open = bfin_rtc_open,
  328. .release = bfin_rtc_release,
  329. .ioctl = bfin_rtc_ioctl,
  330. .read_time = bfin_rtc_read_time,
  331. .set_time = bfin_rtc_set_time,
  332. .read_alarm = bfin_rtc_read_alarm,
  333. .set_alarm = bfin_rtc_set_alarm,
  334. .proc = bfin_rtc_proc,
  335. .irq_set_freq = bfin_irq_set_freq,
  336. };
  337. static int __devinit bfin_rtc_probe(struct platform_device *pdev)
  338. {
  339. struct bfin_rtc *rtc;
  340. int ret = 0;
  341. stampit();
  342. rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
  343. if (unlikely(!rtc))
  344. return -ENOMEM;
  345. spin_lock_init(&rtc->lock);
  346. rtc->rtc_dev = rtc_device_register(pdev->name, &pdev->dev, &bfin_rtc_ops, THIS_MODULE);
  347. if (unlikely(IS_ERR(rtc))) {
  348. ret = PTR_ERR(rtc->rtc_dev);
  349. goto err;
  350. }
  351. rtc->rtc_dev->irq_freq = 1;
  352. platform_set_drvdata(pdev, rtc);
  353. return 0;
  354. err:
  355. kfree(rtc);
  356. return ret;
  357. }
  358. static int __devexit bfin_rtc_remove(struct platform_device *pdev)
  359. {
  360. struct bfin_rtc *rtc = platform_get_drvdata(pdev);
  361. rtc_device_unregister(rtc->rtc_dev);
  362. platform_set_drvdata(pdev, NULL);
  363. kfree(rtc);
  364. return 0;
  365. }
  366. static struct platform_driver bfin_rtc_driver = {
  367. .driver = {
  368. .name = "rtc-bfin",
  369. .owner = THIS_MODULE,
  370. },
  371. .probe = bfin_rtc_probe,
  372. .remove = __devexit_p(bfin_rtc_remove),
  373. };
  374. static int __init bfin_rtc_init(void)
  375. {
  376. stampit();
  377. return platform_driver_register(&bfin_rtc_driver);
  378. }
  379. static void __exit bfin_rtc_exit(void)
  380. {
  381. platform_driver_unregister(&bfin_rtc_driver);
  382. }
  383. module_init(bfin_rtc_init);
  384. module_exit(bfin_rtc_exit);
  385. MODULE_DESCRIPTION("Blackfin On-Chip Real Time Clock Driver");
  386. MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
  387. MODULE_LICENSE("GPL");