rtc-ds1302.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Dallas DS1302 RTC Support
  3. *
  4. * Copyright (C) 2002 David McCullough
  5. * Copyright (C) 2003 - 2007 Paul Mundt
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License version 2. See the file "COPYING" in the main directory of
  9. * this archive for more details.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/rtc.h>
  16. #include <linux/io.h>
  17. #include <linux/bcd.h>
  18. #include <asm/rtc.h>
  19. #define DRV_NAME "rtc-ds1302"
  20. #define DRV_VERSION "0.1.1"
  21. #define RTC_CMD_READ 0x81 /* Read command */
  22. #define RTC_CMD_WRITE 0x80 /* Write command */
  23. #define RTC_ADDR_RAM0 0x20 /* Address of RAM0 */
  24. #define RTC_ADDR_TCR 0x08 /* Address of trickle charge register */
  25. #define RTC_ADDR_YEAR 0x06 /* Address of year register */
  26. #define RTC_ADDR_DAY 0x05 /* Address of day of week register */
  27. #define RTC_ADDR_MON 0x04 /* Address of month register */
  28. #define RTC_ADDR_DATE 0x03 /* Address of day of month register */
  29. #define RTC_ADDR_HOUR 0x02 /* Address of hour register */
  30. #define RTC_ADDR_MIN 0x01 /* Address of minute register */
  31. #define RTC_ADDR_SEC 0x00 /* Address of second register */
  32. #define RTC_RESET 0x1000
  33. #define RTC_IODATA 0x0800
  34. #define RTC_SCLK 0x0400
  35. #ifdef CONFIG_SH_SECUREEDGE5410
  36. #include <mach/snapgear.h>
  37. #define set_dp(x) SECUREEDGE_WRITE_IOPORT(x, 0x1c00)
  38. #define get_dp() SECUREEDGE_READ_IOPORT()
  39. #else
  40. #error "Add support for your platform"
  41. #endif
  42. static void ds1302_sendbits(unsigned int val)
  43. {
  44. int i;
  45. for (i = 8; (i); i--, val >>= 1) {
  46. set_dp((get_dp() & ~RTC_IODATA) | ((val & 0x1) ?
  47. RTC_IODATA : 0));
  48. set_dp(get_dp() | RTC_SCLK); /* clock high */
  49. set_dp(get_dp() & ~RTC_SCLK); /* clock low */
  50. }
  51. }
  52. static unsigned int ds1302_recvbits(void)
  53. {
  54. unsigned int val;
  55. int i;
  56. for (i = 0, val = 0; (i < 8); i++) {
  57. val |= (((get_dp() & RTC_IODATA) ? 1 : 0) << i);
  58. set_dp(get_dp() | RTC_SCLK); /* clock high */
  59. set_dp(get_dp() & ~RTC_SCLK); /* clock low */
  60. }
  61. return val;
  62. }
  63. static unsigned int ds1302_readbyte(unsigned int addr)
  64. {
  65. unsigned int val;
  66. set_dp(get_dp() & ~(RTC_RESET | RTC_IODATA | RTC_SCLK));
  67. set_dp(get_dp() | RTC_RESET);
  68. ds1302_sendbits(((addr & 0x3f) << 1) | RTC_CMD_READ);
  69. val = ds1302_recvbits();
  70. set_dp(get_dp() & ~RTC_RESET);
  71. return val;
  72. }
  73. static void ds1302_writebyte(unsigned int addr, unsigned int val)
  74. {
  75. set_dp(get_dp() & ~(RTC_RESET | RTC_IODATA | RTC_SCLK));
  76. set_dp(get_dp() | RTC_RESET);
  77. ds1302_sendbits(((addr & 0x3f) << 1) | RTC_CMD_WRITE);
  78. ds1302_sendbits(val);
  79. set_dp(get_dp() & ~RTC_RESET);
  80. }
  81. static int ds1302_rtc_read_time(struct device *dev, struct rtc_time *tm)
  82. {
  83. tm->tm_sec = bcd2bin(ds1302_readbyte(RTC_ADDR_SEC));
  84. tm->tm_min = bcd2bin(ds1302_readbyte(RTC_ADDR_MIN));
  85. tm->tm_hour = bcd2bin(ds1302_readbyte(RTC_ADDR_HOUR));
  86. tm->tm_wday = bcd2bin(ds1302_readbyte(RTC_ADDR_DAY));
  87. tm->tm_mday = bcd2bin(ds1302_readbyte(RTC_ADDR_DATE));
  88. tm->tm_mon = bcd2bin(ds1302_readbyte(RTC_ADDR_MON)) - 1;
  89. tm->tm_year = bcd2bin(ds1302_readbyte(RTC_ADDR_YEAR));
  90. if (tm->tm_year < 70)
  91. tm->tm_year += 100;
  92. dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
  93. "mday=%d, mon=%d, year=%d, wday=%d\n",
  94. __func__,
  95. tm->tm_sec, tm->tm_min, tm->tm_hour,
  96. tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday);
  97. return rtc_valid_tm(tm);
  98. }
  99. static int ds1302_rtc_set_time(struct device *dev, struct rtc_time *tm)
  100. {
  101. /* Stop RTC */
  102. ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) | 0x80);
  103. ds1302_writebyte(RTC_ADDR_SEC, bin2bcd(tm->tm_sec));
  104. ds1302_writebyte(RTC_ADDR_MIN, bin2bcd(tm->tm_min));
  105. ds1302_writebyte(RTC_ADDR_HOUR, bin2bcd(tm->tm_hour));
  106. ds1302_writebyte(RTC_ADDR_DAY, bin2bcd(tm->tm_wday));
  107. ds1302_writebyte(RTC_ADDR_DATE, bin2bcd(tm->tm_mday));
  108. ds1302_writebyte(RTC_ADDR_MON, bin2bcd(tm->tm_mon + 1));
  109. ds1302_writebyte(RTC_ADDR_YEAR, bin2bcd(tm->tm_year % 100));
  110. /* Start RTC */
  111. ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) & ~0x80);
  112. return 0;
  113. }
  114. static int ds1302_rtc_ioctl(struct device *dev, unsigned int cmd,
  115. unsigned long arg)
  116. {
  117. switch (cmd) {
  118. #ifdef RTC_SET_CHARGE
  119. case RTC_SET_CHARGE:
  120. {
  121. int tcs_val;
  122. if (copy_from_user(&tcs_val, (int __user *)arg, sizeof(int)))
  123. return -EFAULT;
  124. ds1302_writebyte(RTC_ADDR_TCR, (0xa0 | tcs_val * 0xf));
  125. return 0;
  126. }
  127. #endif
  128. }
  129. return -ENOIOCTLCMD;
  130. }
  131. static struct rtc_class_ops ds1302_rtc_ops = {
  132. .read_time = ds1302_rtc_read_time,
  133. .set_time = ds1302_rtc_set_time,
  134. .ioctl = ds1302_rtc_ioctl,
  135. };
  136. static int __init ds1302_rtc_probe(struct platform_device *pdev)
  137. {
  138. struct rtc_device *rtc;
  139. /* Reset */
  140. set_dp(get_dp() & ~(RTC_RESET | RTC_IODATA | RTC_SCLK));
  141. /* Write a magic value to the DS1302 RAM, and see if it sticks. */
  142. ds1302_writebyte(RTC_ADDR_RAM0, 0x42);
  143. if (ds1302_readbyte(RTC_ADDR_RAM0) != 0x42)
  144. return -ENODEV;
  145. rtc = rtc_device_register("ds1302", &pdev->dev,
  146. &ds1302_rtc_ops, THIS_MODULE);
  147. if (IS_ERR(rtc))
  148. return PTR_ERR(rtc);
  149. platform_set_drvdata(pdev, rtc);
  150. return 0;
  151. }
  152. static int __devexit ds1302_rtc_remove(struct platform_device *pdev)
  153. {
  154. struct rtc_device *rtc = platform_get_drvdata(pdev);
  155. rtc_device_unregister(rtc);
  156. platform_set_drvdata(pdev, NULL);
  157. return 0;
  158. }
  159. static struct platform_driver ds1302_platform_driver = {
  160. .driver = {
  161. .name = DRV_NAME,
  162. .owner = THIS_MODULE,
  163. },
  164. .remove = __devexit_p(ds1302_rtc_remove),
  165. };
  166. static int __init ds1302_rtc_init(void)
  167. {
  168. return platform_driver_probe(&ds1302_platform_driver, ds1302_rtc_probe);
  169. }
  170. static void __exit ds1302_rtc_exit(void)
  171. {
  172. platform_driver_unregister(&ds1302_platform_driver);
  173. }
  174. module_init(ds1302_rtc_init);
  175. module_exit(ds1302_rtc_exit);
  176. MODULE_DESCRIPTION("Dallas DS1302 RTC driver");
  177. MODULE_VERSION(DRV_VERSION);
  178. MODULE_AUTHOR("Paul Mundt, David McCullough");
  179. MODULE_LICENSE("GPL v2");