rtc-vr41xx.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * Driver for NEC VR4100 series Real Time Clock unit.
  3. *
  4. * Copyright (C) 2003-2006 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/err.h>
  21. #include <linux/fs.h>
  22. #include <linux/init.h>
  23. #include <linux/ioport.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/module.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/rtc.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/types.h>
  30. #include <asm/div64.h>
  31. #include <asm/io.h>
  32. #include <asm/uaccess.h>
  33. MODULE_AUTHOR("Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>");
  34. MODULE_DESCRIPTION("NEC VR4100 series RTC driver");
  35. MODULE_LICENSE("GPL");
  36. /* RTC 1 registers */
  37. #define ETIMELREG 0x00
  38. #define ETIMEMREG 0x02
  39. #define ETIMEHREG 0x04
  40. /* RFU */
  41. #define ECMPLREG 0x08
  42. #define ECMPMREG 0x0a
  43. #define ECMPHREG 0x0c
  44. /* RFU */
  45. #define RTCL1LREG 0x10
  46. #define RTCL1HREG 0x12
  47. #define RTCL1CNTLREG 0x14
  48. #define RTCL1CNTHREG 0x16
  49. #define RTCL2LREG 0x18
  50. #define RTCL2HREG 0x1a
  51. #define RTCL2CNTLREG 0x1c
  52. #define RTCL2CNTHREG 0x1e
  53. /* RTC 2 registers */
  54. #define TCLKLREG 0x00
  55. #define TCLKHREG 0x02
  56. #define TCLKCNTLREG 0x04
  57. #define TCLKCNTHREG 0x06
  58. /* RFU */
  59. #define RTCINTREG 0x1e
  60. #define TCLOCK_INT 0x08
  61. #define RTCLONG2_INT 0x04
  62. #define RTCLONG1_INT 0x02
  63. #define ELAPSEDTIME_INT 0x01
  64. #define RTC_FREQUENCY 32768
  65. #define MAX_PERIODIC_RATE 6553
  66. static void __iomem *rtc1_base;
  67. static void __iomem *rtc2_base;
  68. #define rtc1_read(offset) readw(rtc1_base + (offset))
  69. #define rtc1_write(offset, value) writew((value), rtc1_base + (offset))
  70. #define rtc2_read(offset) readw(rtc2_base + (offset))
  71. #define rtc2_write(offset, value) writew((value), rtc2_base + (offset))
  72. static unsigned long epoch = 1970; /* Jan 1 1970 00:00:00 */
  73. static DEFINE_SPINLOCK(rtc_lock);
  74. static char rtc_name[] = "RTC";
  75. static unsigned long periodic_frequency;
  76. static unsigned long periodic_count;
  77. static unsigned int alarm_enabled;
  78. static int aie_irq = -1;
  79. static int pie_irq = -1;
  80. static inline unsigned long read_elapsed_second(void)
  81. {
  82. unsigned long first_low, first_mid, first_high;
  83. unsigned long second_low, second_mid, second_high;
  84. do {
  85. first_low = rtc1_read(ETIMELREG);
  86. first_mid = rtc1_read(ETIMEMREG);
  87. first_high = rtc1_read(ETIMEHREG);
  88. second_low = rtc1_read(ETIMELREG);
  89. second_mid = rtc1_read(ETIMEMREG);
  90. second_high = rtc1_read(ETIMEHREG);
  91. } while (first_low != second_low || first_mid != second_mid ||
  92. first_high != second_high);
  93. return (first_high << 17) | (first_mid << 1) | (first_low >> 15);
  94. }
  95. static inline void write_elapsed_second(unsigned long sec)
  96. {
  97. spin_lock_irq(&rtc_lock);
  98. rtc1_write(ETIMELREG, (uint16_t)(sec << 15));
  99. rtc1_write(ETIMEMREG, (uint16_t)(sec >> 1));
  100. rtc1_write(ETIMEHREG, (uint16_t)(sec >> 17));
  101. spin_unlock_irq(&rtc_lock);
  102. }
  103. static void vr41xx_rtc_release(struct device *dev)
  104. {
  105. spin_lock_irq(&rtc_lock);
  106. rtc1_write(ECMPLREG, 0);
  107. rtc1_write(ECMPMREG, 0);
  108. rtc1_write(ECMPHREG, 0);
  109. rtc1_write(RTCL1LREG, 0);
  110. rtc1_write(RTCL1HREG, 0);
  111. spin_unlock_irq(&rtc_lock);
  112. disable_irq(aie_irq);
  113. disable_irq(pie_irq);
  114. }
  115. static int vr41xx_rtc_read_time(struct device *dev, struct rtc_time *time)
  116. {
  117. unsigned long epoch_sec, elapsed_sec;
  118. epoch_sec = mktime(epoch, 1, 1, 0, 0, 0);
  119. elapsed_sec = read_elapsed_second();
  120. rtc_time_to_tm(epoch_sec + elapsed_sec, time);
  121. return 0;
  122. }
  123. static int vr41xx_rtc_set_time(struct device *dev, struct rtc_time *time)
  124. {
  125. unsigned long epoch_sec, current_sec;
  126. epoch_sec = mktime(epoch, 1, 1, 0, 0, 0);
  127. current_sec = mktime(time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
  128. time->tm_hour, time->tm_min, time->tm_sec);
  129. write_elapsed_second(current_sec - epoch_sec);
  130. return 0;
  131. }
  132. static int vr41xx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  133. {
  134. unsigned long low, mid, high;
  135. struct rtc_time *time = &wkalrm->time;
  136. spin_lock_irq(&rtc_lock);
  137. low = rtc1_read(ECMPLREG);
  138. mid = rtc1_read(ECMPMREG);
  139. high = rtc1_read(ECMPHREG);
  140. wkalrm->enabled = alarm_enabled;
  141. spin_unlock_irq(&rtc_lock);
  142. rtc_time_to_tm((high << 17) | (mid << 1) | (low >> 15), time);
  143. return 0;
  144. }
  145. static int vr41xx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  146. {
  147. unsigned long alarm_sec;
  148. struct rtc_time *time = &wkalrm->time;
  149. alarm_sec = mktime(time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
  150. time->tm_hour, time->tm_min, time->tm_sec);
  151. spin_lock_irq(&rtc_lock);
  152. if (alarm_enabled)
  153. disable_irq(aie_irq);
  154. rtc1_write(ECMPLREG, (uint16_t)(alarm_sec << 15));
  155. rtc1_write(ECMPMREG, (uint16_t)(alarm_sec >> 1));
  156. rtc1_write(ECMPHREG, (uint16_t)(alarm_sec >> 17));
  157. if (wkalrm->enabled)
  158. enable_irq(aie_irq);
  159. alarm_enabled = wkalrm->enabled;
  160. spin_unlock_irq(&rtc_lock);
  161. return 0;
  162. }
  163. static int vr41xx_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  164. {
  165. unsigned long count;
  166. switch (cmd) {
  167. case RTC_AIE_ON:
  168. spin_lock_irq(&rtc_lock);
  169. if (!alarm_enabled) {
  170. enable_irq(aie_irq);
  171. alarm_enabled = 1;
  172. }
  173. spin_unlock_irq(&rtc_lock);
  174. break;
  175. case RTC_AIE_OFF:
  176. spin_lock_irq(&rtc_lock);
  177. if (alarm_enabled) {
  178. disable_irq(aie_irq);
  179. alarm_enabled = 0;
  180. }
  181. spin_unlock_irq(&rtc_lock);
  182. break;
  183. case RTC_PIE_ON:
  184. enable_irq(pie_irq);
  185. break;
  186. case RTC_PIE_OFF:
  187. disable_irq(pie_irq);
  188. break;
  189. case RTC_IRQP_READ:
  190. return put_user(periodic_frequency, (unsigned long __user *)arg);
  191. break;
  192. case RTC_IRQP_SET:
  193. if (arg > MAX_PERIODIC_RATE)
  194. return -EINVAL;
  195. periodic_frequency = arg;
  196. count = RTC_FREQUENCY;
  197. do_div(count, arg);
  198. periodic_count = count;
  199. spin_lock_irq(&rtc_lock);
  200. rtc1_write(RTCL1LREG, count);
  201. rtc1_write(RTCL1HREG, count >> 16);
  202. spin_unlock_irq(&rtc_lock);
  203. break;
  204. case RTC_EPOCH_READ:
  205. return put_user(epoch, (unsigned long __user *)arg);
  206. case RTC_EPOCH_SET:
  207. /* Doesn't support before 1900 */
  208. if (arg < 1900)
  209. return -EINVAL;
  210. epoch = arg;
  211. break;
  212. default:
  213. return -ENOIOCTLCMD;
  214. }
  215. return 0;
  216. }
  217. static irqreturn_t elapsedtime_interrupt(int irq, void *dev_id)
  218. {
  219. struct platform_device *pdev = (struct platform_device *)dev_id;
  220. struct rtc_device *rtc = platform_get_drvdata(pdev);
  221. rtc2_write(RTCINTREG, ELAPSEDTIME_INT);
  222. rtc_update_irq(rtc, 1, RTC_AF);
  223. return IRQ_HANDLED;
  224. }
  225. static irqreturn_t rtclong1_interrupt(int irq, void *dev_id)
  226. {
  227. struct platform_device *pdev = (struct platform_device *)dev_id;
  228. struct rtc_device *rtc = platform_get_drvdata(pdev);
  229. unsigned long count = periodic_count;
  230. rtc2_write(RTCINTREG, RTCLONG1_INT);
  231. rtc1_write(RTCL1LREG, count);
  232. rtc1_write(RTCL1HREG, count >> 16);
  233. rtc_update_irq(rtc, 1, RTC_PF);
  234. return IRQ_HANDLED;
  235. }
  236. static const struct rtc_class_ops vr41xx_rtc_ops = {
  237. .release = vr41xx_rtc_release,
  238. .ioctl = vr41xx_rtc_ioctl,
  239. .read_time = vr41xx_rtc_read_time,
  240. .set_time = vr41xx_rtc_set_time,
  241. .read_alarm = vr41xx_rtc_read_alarm,
  242. .set_alarm = vr41xx_rtc_set_alarm,
  243. };
  244. static int __devinit rtc_probe(struct platform_device *pdev)
  245. {
  246. struct resource *res;
  247. struct rtc_device *rtc;
  248. int retval;
  249. if (pdev->num_resources != 4)
  250. return -EBUSY;
  251. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  252. if (!res)
  253. return -EBUSY;
  254. rtc1_base = ioremap(res->start, res->end - res->start + 1);
  255. if (!rtc1_base)
  256. return -EBUSY;
  257. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  258. if (!res) {
  259. retval = -EBUSY;
  260. goto err_rtc1_iounmap;
  261. }
  262. rtc2_base = ioremap(res->start, res->end - res->start + 1);
  263. if (!rtc2_base) {
  264. retval = -EBUSY;
  265. goto err_rtc1_iounmap;
  266. }
  267. rtc = rtc_device_register(rtc_name, &pdev->dev, &vr41xx_rtc_ops, THIS_MODULE);
  268. if (IS_ERR(rtc)) {
  269. retval = PTR_ERR(rtc);
  270. goto err_iounmap_all;
  271. }
  272. spin_lock_irq(&rtc_lock);
  273. rtc1_write(ECMPLREG, 0);
  274. rtc1_write(ECMPMREG, 0);
  275. rtc1_write(ECMPHREG, 0);
  276. rtc1_write(RTCL1LREG, 0);
  277. rtc1_write(RTCL1HREG, 0);
  278. spin_unlock_irq(&rtc_lock);
  279. aie_irq = platform_get_irq(pdev, 0);
  280. if (aie_irq < 0 || aie_irq >= NR_IRQS) {
  281. retval = -EBUSY;
  282. goto err_device_unregister;
  283. }
  284. retval = request_irq(aie_irq, elapsedtime_interrupt, IRQF_DISABLED,
  285. "elapsed_time", pdev);
  286. if (retval < 0)
  287. goto err_device_unregister;
  288. pie_irq = platform_get_irq(pdev, 1);
  289. if (pie_irq < 0 || pie_irq >= NR_IRQS)
  290. goto err_free_irq;
  291. retval = request_irq(pie_irq, rtclong1_interrupt, IRQF_DISABLED,
  292. "rtclong1", pdev);
  293. if (retval < 0)
  294. goto err_free_irq;
  295. platform_set_drvdata(pdev, rtc);
  296. disable_irq(aie_irq);
  297. disable_irq(pie_irq);
  298. printk(KERN_INFO "rtc: Real Time Clock of NEC VR4100 series\n");
  299. return 0;
  300. err_free_irq:
  301. free_irq(aie_irq, pdev);
  302. err_device_unregister:
  303. rtc_device_unregister(rtc);
  304. err_iounmap_all:
  305. iounmap(rtc2_base);
  306. rtc2_base = NULL;
  307. err_rtc1_iounmap:
  308. iounmap(rtc1_base);
  309. rtc1_base = NULL;
  310. return retval;
  311. }
  312. static int __devexit rtc_remove(struct platform_device *pdev)
  313. {
  314. struct rtc_device *rtc;
  315. rtc = platform_get_drvdata(pdev);
  316. if (rtc)
  317. rtc_device_unregister(rtc);
  318. platform_set_drvdata(pdev, NULL);
  319. free_irq(aie_irq, pdev);
  320. free_irq(pie_irq, pdev);
  321. if (rtc1_base)
  322. iounmap(rtc1_base);
  323. if (rtc2_base)
  324. iounmap(rtc2_base);
  325. return 0;
  326. }
  327. static struct platform_driver rtc_platform_driver = {
  328. .probe = rtc_probe,
  329. .remove = __devexit_p(rtc_remove),
  330. .driver = {
  331. .name = rtc_name,
  332. .owner = THIS_MODULE,
  333. },
  334. };
  335. static int __init vr41xx_rtc_init(void)
  336. {
  337. return platform_driver_register(&rtc_platform_driver);
  338. }
  339. static void __exit vr41xx_rtc_exit(void)
  340. {
  341. platform_driver_unregister(&rtc_platform_driver);
  342. }
  343. module_init(vr41xx_rtc_init);
  344. module_exit(vr41xx_rtc_exit);