rtc-vr41xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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/fs.h>
  21. #include <linux/init.h>
  22. #include <linux/ioport.h>
  23. #include <linux/irq.h>
  24. #include <linux/module.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/rtc.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/types.h>
  29. #include <asm/div64.h>
  30. #include <asm/io.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/vr41xx/vr41xx.h>
  33. MODULE_AUTHOR("Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>");
  34. MODULE_DESCRIPTION("NEC VR4100 series RTC driver");
  35. MODULE_LICENSE("GPL");
  36. #define RTC1_TYPE1_START 0x0b0000c0UL
  37. #define RTC1_TYPE1_END 0x0b0000dfUL
  38. #define RTC2_TYPE1_START 0x0b0001c0UL
  39. #define RTC2_TYPE1_END 0x0b0001dfUL
  40. #define RTC1_TYPE2_START 0x0f000100UL
  41. #define RTC1_TYPE2_END 0x0f00011fUL
  42. #define RTC2_TYPE2_START 0x0f000120UL
  43. #define RTC2_TYPE2_END 0x0f00013fUL
  44. #define RTC1_SIZE 0x20
  45. #define RTC2_SIZE 0x20
  46. /* RTC 1 registers */
  47. #define ETIMELREG 0x00
  48. #define ETIMEMREG 0x02
  49. #define ETIMEHREG 0x04
  50. /* RFU */
  51. #define ECMPLREG 0x08
  52. #define ECMPMREG 0x0a
  53. #define ECMPHREG 0x0c
  54. /* RFU */
  55. #define RTCL1LREG 0x10
  56. #define RTCL1HREG 0x12
  57. #define RTCL1CNTLREG 0x14
  58. #define RTCL1CNTHREG 0x16
  59. #define RTCL2LREG 0x18
  60. #define RTCL2HREG 0x1a
  61. #define RTCL2CNTLREG 0x1c
  62. #define RTCL2CNTHREG 0x1e
  63. /* RTC 2 registers */
  64. #define TCLKLREG 0x00
  65. #define TCLKHREG 0x02
  66. #define TCLKCNTLREG 0x04
  67. #define TCLKCNTHREG 0x06
  68. /* RFU */
  69. #define RTCINTREG 0x1e
  70. #define TCLOCK_INT 0x08
  71. #define RTCLONG2_INT 0x04
  72. #define RTCLONG1_INT 0x02
  73. #define ELAPSEDTIME_INT 0x01
  74. #define RTC_FREQUENCY 32768
  75. #define MAX_PERIODIC_RATE 6553
  76. #define MAX_USER_PERIODIC_RATE 64
  77. static void __iomem *rtc1_base;
  78. static void __iomem *rtc2_base;
  79. #define rtc1_read(offset) readw(rtc1_base + (offset))
  80. #define rtc1_write(offset, value) writew((value), rtc1_base + (offset))
  81. #define rtc2_read(offset) readw(rtc2_base + (offset))
  82. #define rtc2_write(offset, value) writew((value), rtc2_base + (offset))
  83. static unsigned long epoch = 1970; /* Jan 1 1970 00:00:00 */
  84. static spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
  85. static char rtc_name[] = "RTC";
  86. static unsigned long periodic_frequency;
  87. static unsigned long periodic_count;
  88. struct resource rtc_resource[2] = {
  89. { .name = rtc_name,
  90. .flags = IORESOURCE_MEM, },
  91. { .name = rtc_name,
  92. .flags = IORESOURCE_MEM, },
  93. };
  94. static inline unsigned long read_elapsed_second(void)
  95. {
  96. unsigned long first_low, first_mid, first_high;
  97. unsigned long second_low, second_mid, second_high;
  98. do {
  99. first_low = rtc1_read(ETIMELREG);
  100. first_mid = rtc1_read(ETIMEMREG);
  101. first_high = rtc1_read(ETIMEHREG);
  102. second_low = rtc1_read(ETIMELREG);
  103. second_mid = rtc1_read(ETIMEMREG);
  104. second_high = rtc1_read(ETIMEHREG);
  105. } while (first_low != second_low || first_mid != second_mid ||
  106. first_high != second_high);
  107. return (first_high << 17) | (first_mid << 1) | (first_low >> 15);
  108. }
  109. static inline void write_elapsed_second(unsigned long sec)
  110. {
  111. spin_lock_irq(&rtc_lock);
  112. rtc1_write(ETIMELREG, (uint16_t)(sec << 15));
  113. rtc1_write(ETIMEMREG, (uint16_t)(sec >> 1));
  114. rtc1_write(ETIMEHREG, (uint16_t)(sec >> 17));
  115. spin_unlock_irq(&rtc_lock);
  116. }
  117. static void vr41xx_rtc_release(struct device *dev)
  118. {
  119. spin_lock_irq(&rtc_lock);
  120. rtc1_write(ECMPLREG, 0);
  121. rtc1_write(ECMPMREG, 0);
  122. rtc1_write(ECMPHREG, 0);
  123. rtc1_write(RTCL1LREG, 0);
  124. rtc1_write(RTCL1HREG, 0);
  125. spin_unlock_irq(&rtc_lock);
  126. disable_irq(ELAPSEDTIME_IRQ);
  127. disable_irq(RTCLONG1_IRQ);
  128. }
  129. static int vr41xx_rtc_read_time(struct device *dev, struct rtc_time *time)
  130. {
  131. unsigned long epoch_sec, elapsed_sec;
  132. epoch_sec = mktime(epoch, 1, 1, 0, 0, 0);
  133. elapsed_sec = read_elapsed_second();
  134. rtc_time_to_tm(epoch_sec + elapsed_sec, time);
  135. return 0;
  136. }
  137. static int vr41xx_rtc_set_time(struct device *dev, struct rtc_time *time)
  138. {
  139. unsigned long epoch_sec, current_sec;
  140. epoch_sec = mktime(epoch, 1, 1, 0, 0, 0);
  141. current_sec = mktime(time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
  142. time->tm_hour, time->tm_min, time->tm_sec);
  143. write_elapsed_second(current_sec - epoch_sec);
  144. return 0;
  145. }
  146. static int vr41xx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  147. {
  148. unsigned long low, mid, high;
  149. struct rtc_time *time = &wkalrm->time;
  150. spin_lock_irq(&rtc_lock);
  151. low = rtc1_read(ECMPLREG);
  152. mid = rtc1_read(ECMPMREG);
  153. high = rtc1_read(ECMPHREG);
  154. spin_unlock_irq(&rtc_lock);
  155. rtc_time_to_tm((high << 17) | (mid << 1) | (low >> 15), time);
  156. return 0;
  157. }
  158. static int vr41xx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  159. {
  160. unsigned long alarm_sec;
  161. struct rtc_time *time = &wkalrm->time;
  162. alarm_sec = mktime(time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
  163. time->tm_hour, time->tm_min, time->tm_sec);
  164. spin_lock_irq(&rtc_lock);
  165. rtc1_write(ECMPLREG, (uint16_t)(alarm_sec << 15));
  166. rtc1_write(ECMPMREG, (uint16_t)(alarm_sec >> 1));
  167. rtc1_write(ECMPHREG, (uint16_t)(alarm_sec >> 17));
  168. spin_unlock_irq(&rtc_lock);
  169. return 0;
  170. }
  171. static int vr41xx_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  172. {
  173. unsigned long count;
  174. switch (cmd) {
  175. case RTC_AIE_ON:
  176. enable_irq(ELAPSEDTIME_IRQ);
  177. break;
  178. case RTC_AIE_OFF:
  179. disable_irq(ELAPSEDTIME_IRQ);
  180. break;
  181. case RTC_PIE_ON:
  182. enable_irq(RTCLONG1_IRQ);
  183. break;
  184. case RTC_PIE_OFF:
  185. disable_irq(RTCLONG1_IRQ);
  186. break;
  187. case RTC_IRQP_READ:
  188. return put_user(periodic_frequency, (unsigned long __user *)arg);
  189. break;
  190. case RTC_IRQP_SET:
  191. if (arg > MAX_PERIODIC_RATE)
  192. return -EINVAL;
  193. if (arg > MAX_USER_PERIODIC_RATE && capable(CAP_SYS_RESOURCE) == 0)
  194. return -EACCES;
  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. if (capable(CAP_SYS_TIME) == 0)
  211. return -EACCES;
  212. epoch = arg;
  213. break;
  214. default:
  215. return -EINVAL;
  216. }
  217. return 0;
  218. }
  219. static irqreturn_t elapsedtime_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  220. {
  221. struct platform_device *pdev = (struct platform_device *)dev_id;
  222. struct rtc_device *rtc = platform_get_drvdata(pdev);
  223. rtc2_write(RTCINTREG, ELAPSEDTIME_INT);
  224. rtc_update_irq(&rtc->class_dev, 1, RTC_AF);
  225. return IRQ_HANDLED;
  226. }
  227. static irqreturn_t rtclong1_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  228. {
  229. struct platform_device *pdev = (struct platform_device *)dev_id;
  230. struct rtc_device *rtc = platform_get_drvdata(pdev);
  231. unsigned long count = periodic_count;
  232. rtc2_write(RTCINTREG, RTCLONG1_INT);
  233. rtc1_write(RTCL1LREG, count);
  234. rtc1_write(RTCL1HREG, count >> 16);
  235. rtc_update_irq(&rtc->class_dev, 1, RTC_PF);
  236. return IRQ_HANDLED;
  237. }
  238. static struct rtc_class_ops vr41xx_rtc_ops = {
  239. .release = vr41xx_rtc_release,
  240. .ioctl = vr41xx_rtc_ioctl,
  241. .read_time = vr41xx_rtc_read_time,
  242. .set_time = vr41xx_rtc_set_time,
  243. .read_alarm = vr41xx_rtc_read_alarm,
  244. .set_alarm = vr41xx_rtc_set_alarm,
  245. };
  246. static int __devinit rtc_probe(struct platform_device *pdev)
  247. {
  248. struct rtc_device *rtc;
  249. unsigned int irq;
  250. int retval;
  251. if (pdev->num_resources != 2)
  252. return -EBUSY;
  253. rtc1_base = ioremap(pdev->resource[0].start, RTC1_SIZE);
  254. if (rtc1_base == NULL)
  255. return -EBUSY;
  256. rtc2_base = ioremap(pdev->resource[1].start, RTC2_SIZE);
  257. if (rtc2_base == NULL) {
  258. iounmap(rtc1_base);
  259. rtc1_base = NULL;
  260. return -EBUSY;
  261. }
  262. rtc = rtc_device_register(rtc_name, &pdev->dev, &vr41xx_rtc_ops, THIS_MODULE);
  263. if (IS_ERR(rtc)) {
  264. iounmap(rtc1_base);
  265. iounmap(rtc2_base);
  266. rtc1_base = NULL;
  267. rtc2_base = NULL;
  268. return PTR_ERR(rtc);
  269. }
  270. spin_lock_irq(&rtc_lock);
  271. rtc1_write(ECMPLREG, 0);
  272. rtc1_write(ECMPMREG, 0);
  273. rtc1_write(ECMPHREG, 0);
  274. rtc1_write(RTCL1LREG, 0);
  275. rtc1_write(RTCL1HREG, 0);
  276. spin_unlock_irq(&rtc_lock);
  277. irq = ELAPSEDTIME_IRQ;
  278. retval = request_irq(irq, elapsedtime_interrupt, SA_INTERRUPT,
  279. "elapsed_time", pdev);
  280. if (retval == 0) {
  281. irq = RTCLONG1_IRQ;
  282. retval = request_irq(irq, rtclong1_interrupt, SA_INTERRUPT,
  283. "rtclong1", pdev);
  284. }
  285. if (retval < 0) {
  286. printk(KERN_ERR "rtc: IRQ%d is busy\n", irq);
  287. rtc_device_unregister(rtc);
  288. if (irq == RTCLONG1_IRQ)
  289. free_irq(ELAPSEDTIME_IRQ, NULL);
  290. iounmap(rtc1_base);
  291. iounmap(rtc2_base);
  292. rtc1_base = NULL;
  293. rtc2_base = NULL;
  294. return retval;
  295. }
  296. platform_set_drvdata(pdev, rtc);
  297. disable_irq(ELAPSEDTIME_IRQ);
  298. disable_irq(RTCLONG1_IRQ);
  299. printk(KERN_INFO "rtc: Real Time Clock of NEC VR4100 series\n");
  300. return 0;
  301. }
  302. static int __devexit rtc_remove(struct platform_device *pdev)
  303. {
  304. struct rtc_device *rtc;
  305. rtc = platform_get_drvdata(pdev);
  306. if (rtc != NULL)
  307. rtc_device_unregister(rtc);
  308. platform_set_drvdata(pdev, NULL);
  309. free_irq(ELAPSEDTIME_IRQ, NULL);
  310. free_irq(RTCLONG1_IRQ, NULL);
  311. if (rtc1_base != NULL)
  312. iounmap(rtc1_base);
  313. if (rtc2_base != NULL)
  314. iounmap(rtc2_base);
  315. return 0;
  316. }
  317. static struct platform_device *rtc_platform_device;
  318. static struct platform_driver rtc_platform_driver = {
  319. .probe = rtc_probe,
  320. .remove = __devexit_p(rtc_remove),
  321. .driver = {
  322. .name = rtc_name,
  323. .owner = THIS_MODULE,
  324. },
  325. };
  326. static int __init vr41xx_rtc_init(void)
  327. {
  328. int retval;
  329. switch (current_cpu_data.cputype) {
  330. case CPU_VR4111:
  331. case CPU_VR4121:
  332. rtc_resource[0].start = RTC1_TYPE1_START;
  333. rtc_resource[0].end = RTC1_TYPE1_END;
  334. rtc_resource[1].start = RTC2_TYPE1_START;
  335. rtc_resource[1].end = RTC2_TYPE1_END;
  336. break;
  337. case CPU_VR4122:
  338. case CPU_VR4131:
  339. case CPU_VR4133:
  340. rtc_resource[0].start = RTC1_TYPE2_START;
  341. rtc_resource[0].end = RTC1_TYPE2_END;
  342. rtc_resource[1].start = RTC2_TYPE2_START;
  343. rtc_resource[1].end = RTC2_TYPE2_END;
  344. break;
  345. default:
  346. return -ENODEV;
  347. break;
  348. }
  349. rtc_platform_device = platform_device_alloc("RTC", -1);
  350. if (rtc_platform_device == NULL)
  351. return -ENOMEM;
  352. retval = platform_device_add_resources(rtc_platform_device,
  353. rtc_resource, ARRAY_SIZE(rtc_resource));
  354. if (retval == 0)
  355. retval = platform_device_add(rtc_platform_device);
  356. if (retval < 0) {
  357. platform_device_put(rtc_platform_device);
  358. return retval;
  359. }
  360. retval = platform_driver_register(&rtc_platform_driver);
  361. if (retval < 0)
  362. platform_device_unregister(rtc_platform_device);
  363. return retval;
  364. }
  365. static void __exit vr41xx_rtc_exit(void)
  366. {
  367. platform_driver_unregister(&rtc_platform_driver);
  368. platform_device_unregister(rtc_platform_device);
  369. }
  370. module_init(vr41xx_rtc_init);
  371. module_exit(vr41xx_rtc_exit);