rtc-vr41xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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/irq.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. static void __iomem *rtc1_base;
  77. static void __iomem *rtc2_base;
  78. #define rtc1_read(offset) readw(rtc1_base + (offset))
  79. #define rtc1_write(offset, value) writew((value), rtc1_base + (offset))
  80. #define rtc2_read(offset) readw(rtc2_base + (offset))
  81. #define rtc2_write(offset, value) writew((value), rtc2_base + (offset))
  82. static unsigned long epoch = 1970; /* Jan 1 1970 00:00:00 */
  83. static DEFINE_SPINLOCK(rtc_lock);
  84. static char rtc_name[] = "RTC";
  85. static unsigned long periodic_frequency;
  86. static unsigned long periodic_count;
  87. struct resource rtc_resource[2] = {
  88. { .name = rtc_name,
  89. .flags = IORESOURCE_MEM, },
  90. { .name = rtc_name,
  91. .flags = IORESOURCE_MEM, },
  92. };
  93. static inline unsigned long read_elapsed_second(void)
  94. {
  95. unsigned long first_low, first_mid, first_high;
  96. unsigned long second_low, second_mid, second_high;
  97. do {
  98. first_low = rtc1_read(ETIMELREG);
  99. first_mid = rtc1_read(ETIMEMREG);
  100. first_high = rtc1_read(ETIMEHREG);
  101. second_low = rtc1_read(ETIMELREG);
  102. second_mid = rtc1_read(ETIMEMREG);
  103. second_high = rtc1_read(ETIMEHREG);
  104. } while (first_low != second_low || first_mid != second_mid ||
  105. first_high != second_high);
  106. return (first_high << 17) | (first_mid << 1) | (first_low >> 15);
  107. }
  108. static inline void write_elapsed_second(unsigned long sec)
  109. {
  110. spin_lock_irq(&rtc_lock);
  111. rtc1_write(ETIMELREG, (uint16_t)(sec << 15));
  112. rtc1_write(ETIMEMREG, (uint16_t)(sec >> 1));
  113. rtc1_write(ETIMEHREG, (uint16_t)(sec >> 17));
  114. spin_unlock_irq(&rtc_lock);
  115. }
  116. static void vr41xx_rtc_release(struct device *dev)
  117. {
  118. spin_lock_irq(&rtc_lock);
  119. rtc1_write(ECMPLREG, 0);
  120. rtc1_write(ECMPMREG, 0);
  121. rtc1_write(ECMPHREG, 0);
  122. rtc1_write(RTCL1LREG, 0);
  123. rtc1_write(RTCL1HREG, 0);
  124. spin_unlock_irq(&rtc_lock);
  125. disable_irq(ELAPSEDTIME_IRQ);
  126. disable_irq(RTCLONG1_IRQ);
  127. }
  128. static int vr41xx_rtc_read_time(struct device *dev, struct rtc_time *time)
  129. {
  130. unsigned long epoch_sec, elapsed_sec;
  131. epoch_sec = mktime(epoch, 1, 1, 0, 0, 0);
  132. elapsed_sec = read_elapsed_second();
  133. rtc_time_to_tm(epoch_sec + elapsed_sec, time);
  134. return 0;
  135. }
  136. static int vr41xx_rtc_set_time(struct device *dev, struct rtc_time *time)
  137. {
  138. unsigned long epoch_sec, current_sec;
  139. epoch_sec = mktime(epoch, 1, 1, 0, 0, 0);
  140. current_sec = mktime(time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
  141. time->tm_hour, time->tm_min, time->tm_sec);
  142. write_elapsed_second(current_sec - epoch_sec);
  143. return 0;
  144. }
  145. static int vr41xx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  146. {
  147. unsigned long low, mid, high;
  148. struct rtc_time *time = &wkalrm->time;
  149. spin_lock_irq(&rtc_lock);
  150. low = rtc1_read(ECMPLREG);
  151. mid = rtc1_read(ECMPMREG);
  152. high = rtc1_read(ECMPHREG);
  153. spin_unlock_irq(&rtc_lock);
  154. rtc_time_to_tm((high << 17) | (mid << 1) | (low >> 15), time);
  155. return 0;
  156. }
  157. static int vr41xx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  158. {
  159. unsigned long alarm_sec;
  160. struct rtc_time *time = &wkalrm->time;
  161. alarm_sec = mktime(time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
  162. time->tm_hour, time->tm_min, time->tm_sec);
  163. spin_lock_irq(&rtc_lock);
  164. rtc1_write(ECMPLREG, (uint16_t)(alarm_sec << 15));
  165. rtc1_write(ECMPMREG, (uint16_t)(alarm_sec >> 1));
  166. rtc1_write(ECMPHREG, (uint16_t)(alarm_sec >> 17));
  167. spin_unlock_irq(&rtc_lock);
  168. return 0;
  169. }
  170. static int vr41xx_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  171. {
  172. unsigned long count;
  173. switch (cmd) {
  174. case RTC_AIE_ON:
  175. enable_irq(ELAPSEDTIME_IRQ);
  176. break;
  177. case RTC_AIE_OFF:
  178. disable_irq(ELAPSEDTIME_IRQ);
  179. break;
  180. case RTC_PIE_ON:
  181. enable_irq(RTCLONG1_IRQ);
  182. break;
  183. case RTC_PIE_OFF:
  184. disable_irq(RTCLONG1_IRQ);
  185. break;
  186. case RTC_IRQP_READ:
  187. return put_user(periodic_frequency, (unsigned long __user *)arg);
  188. break;
  189. case RTC_IRQP_SET:
  190. if (arg > MAX_PERIODIC_RATE)
  191. return -EINVAL;
  192. periodic_frequency = arg;
  193. count = RTC_FREQUENCY;
  194. do_div(count, arg);
  195. periodic_count = count;
  196. spin_lock_irq(&rtc_lock);
  197. rtc1_write(RTCL1LREG, count);
  198. rtc1_write(RTCL1HREG, count >> 16);
  199. spin_unlock_irq(&rtc_lock);
  200. break;
  201. case RTC_EPOCH_READ:
  202. return put_user(epoch, (unsigned long __user *)arg);
  203. case RTC_EPOCH_SET:
  204. /* Doesn't support before 1900 */
  205. if (arg < 1900)
  206. return -EINVAL;
  207. epoch = arg;
  208. break;
  209. default:
  210. return -ENOIOCTLCMD;
  211. }
  212. return 0;
  213. }
  214. static irqreturn_t elapsedtime_interrupt(int irq, void *dev_id)
  215. {
  216. struct platform_device *pdev = (struct platform_device *)dev_id;
  217. struct rtc_device *rtc = platform_get_drvdata(pdev);
  218. rtc2_write(RTCINTREG, ELAPSEDTIME_INT);
  219. rtc_update_irq(&rtc->class_dev, 1, RTC_AF);
  220. return IRQ_HANDLED;
  221. }
  222. static irqreturn_t rtclong1_interrupt(int irq, void *dev_id)
  223. {
  224. struct platform_device *pdev = (struct platform_device *)dev_id;
  225. struct rtc_device *rtc = platform_get_drvdata(pdev);
  226. unsigned long count = periodic_count;
  227. rtc2_write(RTCINTREG, RTCLONG1_INT);
  228. rtc1_write(RTCL1LREG, count);
  229. rtc1_write(RTCL1HREG, count >> 16);
  230. rtc_update_irq(&rtc->class_dev, 1, RTC_PF);
  231. return IRQ_HANDLED;
  232. }
  233. static const struct rtc_class_ops vr41xx_rtc_ops = {
  234. .release = vr41xx_rtc_release,
  235. .ioctl = vr41xx_rtc_ioctl,
  236. .read_time = vr41xx_rtc_read_time,
  237. .set_time = vr41xx_rtc_set_time,
  238. .read_alarm = vr41xx_rtc_read_alarm,
  239. .set_alarm = vr41xx_rtc_set_alarm,
  240. };
  241. static int __devinit rtc_probe(struct platform_device *pdev)
  242. {
  243. struct rtc_device *rtc;
  244. unsigned int irq;
  245. int retval;
  246. if (pdev->num_resources != 2)
  247. return -EBUSY;
  248. rtc1_base = ioremap(pdev->resource[0].start, RTC1_SIZE);
  249. if (rtc1_base == NULL)
  250. return -EBUSY;
  251. rtc2_base = ioremap(pdev->resource[1].start, RTC2_SIZE);
  252. if (rtc2_base == NULL) {
  253. iounmap(rtc1_base);
  254. rtc1_base = NULL;
  255. return -EBUSY;
  256. }
  257. rtc = rtc_device_register(rtc_name, &pdev->dev, &vr41xx_rtc_ops, THIS_MODULE);
  258. if (IS_ERR(rtc)) {
  259. iounmap(rtc1_base);
  260. iounmap(rtc2_base);
  261. rtc1_base = NULL;
  262. rtc2_base = NULL;
  263. return PTR_ERR(rtc);
  264. }
  265. spin_lock_irq(&rtc_lock);
  266. rtc1_write(ECMPLREG, 0);
  267. rtc1_write(ECMPMREG, 0);
  268. rtc1_write(ECMPHREG, 0);
  269. rtc1_write(RTCL1LREG, 0);
  270. rtc1_write(RTCL1HREG, 0);
  271. spin_unlock_irq(&rtc_lock);
  272. irq = ELAPSEDTIME_IRQ;
  273. retval = request_irq(irq, elapsedtime_interrupt, IRQF_DISABLED,
  274. "elapsed_time", pdev);
  275. if (retval == 0) {
  276. irq = RTCLONG1_IRQ;
  277. retval = request_irq(irq, rtclong1_interrupt, IRQF_DISABLED,
  278. "rtclong1", pdev);
  279. }
  280. if (retval < 0) {
  281. printk(KERN_ERR "rtc: IRQ%d is busy\n", irq);
  282. rtc_device_unregister(rtc);
  283. if (irq == RTCLONG1_IRQ)
  284. free_irq(ELAPSEDTIME_IRQ, NULL);
  285. iounmap(rtc1_base);
  286. iounmap(rtc2_base);
  287. rtc1_base = NULL;
  288. rtc2_base = NULL;
  289. return retval;
  290. }
  291. platform_set_drvdata(pdev, rtc);
  292. disable_irq(ELAPSEDTIME_IRQ);
  293. disable_irq(RTCLONG1_IRQ);
  294. printk(KERN_INFO "rtc: Real Time Clock of NEC VR4100 series\n");
  295. return 0;
  296. }
  297. static int __devexit rtc_remove(struct platform_device *pdev)
  298. {
  299. struct rtc_device *rtc;
  300. rtc = platform_get_drvdata(pdev);
  301. if (rtc != NULL)
  302. rtc_device_unregister(rtc);
  303. platform_set_drvdata(pdev, NULL);
  304. free_irq(ELAPSEDTIME_IRQ, NULL);
  305. free_irq(RTCLONG1_IRQ, NULL);
  306. if (rtc1_base != NULL)
  307. iounmap(rtc1_base);
  308. if (rtc2_base != NULL)
  309. iounmap(rtc2_base);
  310. return 0;
  311. }
  312. static struct platform_device *rtc_platform_device;
  313. static struct platform_driver rtc_platform_driver = {
  314. .probe = rtc_probe,
  315. .remove = __devexit_p(rtc_remove),
  316. .driver = {
  317. .name = rtc_name,
  318. .owner = THIS_MODULE,
  319. },
  320. };
  321. static int __init vr41xx_rtc_init(void)
  322. {
  323. int retval;
  324. switch (current_cpu_data.cputype) {
  325. case CPU_VR4111:
  326. case CPU_VR4121:
  327. rtc_resource[0].start = RTC1_TYPE1_START;
  328. rtc_resource[0].end = RTC1_TYPE1_END;
  329. rtc_resource[1].start = RTC2_TYPE1_START;
  330. rtc_resource[1].end = RTC2_TYPE1_END;
  331. break;
  332. case CPU_VR4122:
  333. case CPU_VR4131:
  334. case CPU_VR4133:
  335. rtc_resource[0].start = RTC1_TYPE2_START;
  336. rtc_resource[0].end = RTC1_TYPE2_END;
  337. rtc_resource[1].start = RTC2_TYPE2_START;
  338. rtc_resource[1].end = RTC2_TYPE2_END;
  339. break;
  340. default:
  341. return -ENODEV;
  342. break;
  343. }
  344. rtc_platform_device = platform_device_alloc("RTC", -1);
  345. if (rtc_platform_device == NULL)
  346. return -ENOMEM;
  347. retval = platform_device_add_resources(rtc_platform_device,
  348. rtc_resource, ARRAY_SIZE(rtc_resource));
  349. if (retval == 0)
  350. retval = platform_device_add(rtc_platform_device);
  351. if (retval < 0) {
  352. platform_device_put(rtc_platform_device);
  353. return retval;
  354. }
  355. retval = platform_driver_register(&rtc_platform_driver);
  356. if (retval < 0)
  357. platform_device_unregister(rtc_platform_device);
  358. return retval;
  359. }
  360. static void __exit vr41xx_rtc_exit(void)
  361. {
  362. platform_driver_unregister(&rtc_platform_driver);
  363. platform_device_unregister(rtc_platform_device);
  364. }
  365. module_init(vr41xx_rtc_init);
  366. module_exit(vr41xx_rtc_exit);