rtc-vr41xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. static unsigned int alarm_enabled;
  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. wkalrm->enabled = alarm_enabled;
  155. spin_unlock_irq(&rtc_lock);
  156. rtc_time_to_tm((high << 17) | (mid << 1) | (low >> 15), time);
  157. return 0;
  158. }
  159. static int vr41xx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  160. {
  161. unsigned long alarm_sec;
  162. struct rtc_time *time = &wkalrm->time;
  163. alarm_sec = mktime(time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
  164. time->tm_hour, time->tm_min, time->tm_sec);
  165. spin_lock_irq(&rtc_lock);
  166. if (alarm_enabled)
  167. disable_irq(ELAPSEDTIME_IRQ);
  168. rtc1_write(ECMPLREG, (uint16_t)(alarm_sec << 15));
  169. rtc1_write(ECMPMREG, (uint16_t)(alarm_sec >> 1));
  170. rtc1_write(ECMPHREG, (uint16_t)(alarm_sec >> 17));
  171. if (wkalrm->enabled)
  172. enable_irq(ELAPSEDTIME_IRQ);
  173. alarm_enabled = wkalrm->enabled;
  174. spin_unlock_irq(&rtc_lock);
  175. return 0;
  176. }
  177. static int vr41xx_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  178. {
  179. unsigned long count;
  180. switch (cmd) {
  181. case RTC_AIE_ON:
  182. spin_lock_irq(&rtc_lock);
  183. if (!alarm_enabled) {
  184. enable_irq(ELAPSEDTIME_IRQ);
  185. alarm_enabled = 1;
  186. }
  187. spin_unlock_irq(&rtc_lock);
  188. break;
  189. case RTC_AIE_OFF:
  190. spin_lock_irq(&rtc_lock);
  191. if (alarm_enabled) {
  192. disable_irq(ELAPSEDTIME_IRQ);
  193. alarm_enabled = 0;
  194. }
  195. spin_unlock_irq(&rtc_lock);
  196. break;
  197. case RTC_PIE_ON:
  198. enable_irq(RTCLONG1_IRQ);
  199. break;
  200. case RTC_PIE_OFF:
  201. disable_irq(RTCLONG1_IRQ);
  202. break;
  203. case RTC_IRQP_READ:
  204. return put_user(periodic_frequency, (unsigned long __user *)arg);
  205. break;
  206. case RTC_IRQP_SET:
  207. if (arg > MAX_PERIODIC_RATE)
  208. return -EINVAL;
  209. periodic_frequency = arg;
  210. count = RTC_FREQUENCY;
  211. do_div(count, arg);
  212. periodic_count = count;
  213. spin_lock_irq(&rtc_lock);
  214. rtc1_write(RTCL1LREG, count);
  215. rtc1_write(RTCL1HREG, count >> 16);
  216. spin_unlock_irq(&rtc_lock);
  217. break;
  218. case RTC_EPOCH_READ:
  219. return put_user(epoch, (unsigned long __user *)arg);
  220. case RTC_EPOCH_SET:
  221. /* Doesn't support before 1900 */
  222. if (arg < 1900)
  223. return -EINVAL;
  224. epoch = arg;
  225. break;
  226. default:
  227. return -ENOIOCTLCMD;
  228. }
  229. return 0;
  230. }
  231. static irqreturn_t elapsedtime_interrupt(int irq, void *dev_id)
  232. {
  233. struct platform_device *pdev = (struct platform_device *)dev_id;
  234. struct rtc_device *rtc = platform_get_drvdata(pdev);
  235. rtc2_write(RTCINTREG, ELAPSEDTIME_INT);
  236. rtc_update_irq(rtc, 1, RTC_AF);
  237. return IRQ_HANDLED;
  238. }
  239. static irqreturn_t rtclong1_interrupt(int irq, void *dev_id)
  240. {
  241. struct platform_device *pdev = (struct platform_device *)dev_id;
  242. struct rtc_device *rtc = platform_get_drvdata(pdev);
  243. unsigned long count = periodic_count;
  244. rtc2_write(RTCINTREG, RTCLONG1_INT);
  245. rtc1_write(RTCL1LREG, count);
  246. rtc1_write(RTCL1HREG, count >> 16);
  247. rtc_update_irq(rtc, 1, RTC_PF);
  248. return IRQ_HANDLED;
  249. }
  250. static const struct rtc_class_ops vr41xx_rtc_ops = {
  251. .release = vr41xx_rtc_release,
  252. .ioctl = vr41xx_rtc_ioctl,
  253. .read_time = vr41xx_rtc_read_time,
  254. .set_time = vr41xx_rtc_set_time,
  255. .read_alarm = vr41xx_rtc_read_alarm,
  256. .set_alarm = vr41xx_rtc_set_alarm,
  257. };
  258. static int __devinit rtc_probe(struct platform_device *pdev)
  259. {
  260. struct rtc_device *rtc;
  261. unsigned int irq;
  262. int retval;
  263. if (pdev->num_resources != 2)
  264. return -EBUSY;
  265. rtc1_base = ioremap(pdev->resource[0].start, RTC1_SIZE);
  266. if (rtc1_base == NULL)
  267. return -EBUSY;
  268. rtc2_base = ioremap(pdev->resource[1].start, RTC2_SIZE);
  269. if (rtc2_base == NULL) {
  270. iounmap(rtc1_base);
  271. rtc1_base = NULL;
  272. return -EBUSY;
  273. }
  274. rtc = rtc_device_register(rtc_name, &pdev->dev, &vr41xx_rtc_ops, THIS_MODULE);
  275. if (IS_ERR(rtc)) {
  276. iounmap(rtc1_base);
  277. iounmap(rtc2_base);
  278. rtc1_base = NULL;
  279. rtc2_base = NULL;
  280. return PTR_ERR(rtc);
  281. }
  282. spin_lock_irq(&rtc_lock);
  283. rtc1_write(ECMPLREG, 0);
  284. rtc1_write(ECMPMREG, 0);
  285. rtc1_write(ECMPHREG, 0);
  286. rtc1_write(RTCL1LREG, 0);
  287. rtc1_write(RTCL1HREG, 0);
  288. spin_unlock_irq(&rtc_lock);
  289. irq = ELAPSEDTIME_IRQ;
  290. retval = request_irq(irq, elapsedtime_interrupt, IRQF_DISABLED,
  291. "elapsed_time", pdev);
  292. if (retval == 0) {
  293. irq = RTCLONG1_IRQ;
  294. retval = request_irq(irq, rtclong1_interrupt, IRQF_DISABLED,
  295. "rtclong1", pdev);
  296. }
  297. if (retval < 0) {
  298. printk(KERN_ERR "rtc: IRQ%d is busy\n", irq);
  299. rtc_device_unregister(rtc);
  300. if (irq == RTCLONG1_IRQ)
  301. free_irq(ELAPSEDTIME_IRQ, NULL);
  302. iounmap(rtc1_base);
  303. iounmap(rtc2_base);
  304. rtc1_base = NULL;
  305. rtc2_base = NULL;
  306. return retval;
  307. }
  308. platform_set_drvdata(pdev, rtc);
  309. disable_irq(ELAPSEDTIME_IRQ);
  310. disable_irq(RTCLONG1_IRQ);
  311. printk(KERN_INFO "rtc: Real Time Clock of NEC VR4100 series\n");
  312. return 0;
  313. }
  314. static int __devexit rtc_remove(struct platform_device *pdev)
  315. {
  316. struct rtc_device *rtc;
  317. rtc = platform_get_drvdata(pdev);
  318. if (rtc != NULL)
  319. rtc_device_unregister(rtc);
  320. platform_set_drvdata(pdev, NULL);
  321. free_irq(ELAPSEDTIME_IRQ, NULL);
  322. free_irq(RTCLONG1_IRQ, NULL);
  323. if (rtc1_base != NULL)
  324. iounmap(rtc1_base);
  325. if (rtc2_base != NULL)
  326. iounmap(rtc2_base);
  327. return 0;
  328. }
  329. static struct platform_device *rtc_platform_device;
  330. static struct platform_driver rtc_platform_driver = {
  331. .probe = rtc_probe,
  332. .remove = __devexit_p(rtc_remove),
  333. .driver = {
  334. .name = rtc_name,
  335. .owner = THIS_MODULE,
  336. },
  337. };
  338. static int __init vr41xx_rtc_init(void)
  339. {
  340. int retval;
  341. switch (current_cpu_data.cputype) {
  342. case CPU_VR4111:
  343. case CPU_VR4121:
  344. rtc_resource[0].start = RTC1_TYPE1_START;
  345. rtc_resource[0].end = RTC1_TYPE1_END;
  346. rtc_resource[1].start = RTC2_TYPE1_START;
  347. rtc_resource[1].end = RTC2_TYPE1_END;
  348. break;
  349. case CPU_VR4122:
  350. case CPU_VR4131:
  351. case CPU_VR4133:
  352. rtc_resource[0].start = RTC1_TYPE2_START;
  353. rtc_resource[0].end = RTC1_TYPE2_END;
  354. rtc_resource[1].start = RTC2_TYPE2_START;
  355. rtc_resource[1].end = RTC2_TYPE2_END;
  356. break;
  357. default:
  358. return -ENODEV;
  359. break;
  360. }
  361. rtc_platform_device = platform_device_alloc("RTC", -1);
  362. if (rtc_platform_device == NULL)
  363. return -ENOMEM;
  364. retval = platform_device_add_resources(rtc_platform_device,
  365. rtc_resource, ARRAY_SIZE(rtc_resource));
  366. if (retval == 0)
  367. retval = platform_device_add(rtc_platform_device);
  368. if (retval < 0) {
  369. platform_device_put(rtc_platform_device);
  370. return retval;
  371. }
  372. retval = platform_driver_register(&rtc_platform_driver);
  373. if (retval < 0)
  374. platform_device_unregister(rtc_platform_device);
  375. return retval;
  376. }
  377. static void __exit vr41xx_rtc_exit(void)
  378. {
  379. platform_driver_unregister(&rtc_platform_driver);
  380. platform_device_unregister(rtc_platform_device);
  381. }
  382. module_init(vr41xx_rtc_init);
  383. module_exit(vr41xx_rtc_exit);