rtc-ds1302.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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/time.h>
  16. #include <linux/rtc.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/io.h>
  19. #include <linux/bcd.h>
  20. #include <asm/rtc.h>
  21. #define DRV_NAME "rtc-ds1302"
  22. #define DRV_VERSION "0.1.0"
  23. #define RTC_CMD_READ 0x81 /* Read command */
  24. #define RTC_CMD_WRITE 0x80 /* Write command */
  25. #define RTC_ADDR_RAM0 0x20 /* Address of RAM0 */
  26. #define RTC_ADDR_TCR 0x08 /* Address of trickle charge register */
  27. #define RTC_ADDR_YEAR 0x06 /* Address of year register */
  28. #define RTC_ADDR_DAY 0x05 /* Address of day of week register */
  29. #define RTC_ADDR_MON 0x04 /* Address of month register */
  30. #define RTC_ADDR_DATE 0x03 /* Address of day of month register */
  31. #define RTC_ADDR_HOUR 0x02 /* Address of hour register */
  32. #define RTC_ADDR_MIN 0x01 /* Address of minute register */
  33. #define RTC_ADDR_SEC 0x00 /* Address of second register */
  34. #define RTC_RESET 0x1000
  35. #define RTC_IODATA 0x0800
  36. #define RTC_SCLK 0x0400
  37. #ifdef CONFIG_SH_SECUREEDGE5410
  38. #include <mach/snapgear.h>
  39. #define set_dp(x) SECUREEDGE_WRITE_IOPORT(x, 0x1c00)
  40. #define get_dp() SECUREEDGE_READ_IOPORT()
  41. #else
  42. #error "Add support for your platform"
  43. #endif
  44. struct ds1302_rtc {
  45. struct rtc_device *rtc_dev;
  46. spinlock_t lock;
  47. };
  48. static void ds1302_sendbits(unsigned int val)
  49. {
  50. int i;
  51. for (i = 8; (i); i--, val >>= 1) {
  52. set_dp((get_dp() & ~RTC_IODATA) | ((val & 0x1) ?
  53. RTC_IODATA : 0));
  54. set_dp(get_dp() | RTC_SCLK); /* clock high */
  55. set_dp(get_dp() & ~RTC_SCLK); /* clock low */
  56. }
  57. }
  58. static unsigned int ds1302_recvbits(void)
  59. {
  60. unsigned int val;
  61. int i;
  62. for (i = 0, val = 0; (i < 8); i++) {
  63. val |= (((get_dp() & RTC_IODATA) ? 1 : 0) << i);
  64. set_dp(get_dp() | RTC_SCLK); /* clock high */
  65. set_dp(get_dp() & ~RTC_SCLK); /* clock low */
  66. }
  67. return val;
  68. }
  69. static unsigned int ds1302_readbyte(unsigned int addr)
  70. {
  71. unsigned int val;
  72. set_dp(get_dp() & ~(RTC_RESET | RTC_IODATA | RTC_SCLK));
  73. set_dp(get_dp() | RTC_RESET);
  74. ds1302_sendbits(((addr & 0x3f) << 1) | RTC_CMD_READ);
  75. val = ds1302_recvbits();
  76. set_dp(get_dp() & ~RTC_RESET);
  77. return val;
  78. }
  79. static void ds1302_writebyte(unsigned int addr, unsigned int val)
  80. {
  81. set_dp(get_dp() & ~(RTC_RESET | RTC_IODATA | RTC_SCLK));
  82. set_dp(get_dp() | RTC_RESET);
  83. ds1302_sendbits(((addr & 0x3f) << 1) | RTC_CMD_WRITE);
  84. ds1302_sendbits(val);
  85. set_dp(get_dp() & ~RTC_RESET);
  86. }
  87. static int ds1302_rtc_read_time(struct device *dev, struct rtc_time *tm)
  88. {
  89. struct ds1302_rtc *rtc = dev_get_drvdata(dev);
  90. spin_lock_irq(&rtc->lock);
  91. tm->tm_sec = bcd2bin(ds1302_readbyte(RTC_ADDR_SEC));
  92. tm->tm_min = bcd2bin(ds1302_readbyte(RTC_ADDR_MIN));
  93. tm->tm_hour = bcd2bin(ds1302_readbyte(RTC_ADDR_HOUR));
  94. tm->tm_wday = bcd2bin(ds1302_readbyte(RTC_ADDR_DAY));
  95. tm->tm_mday = bcd2bin(ds1302_readbyte(RTC_ADDR_DATE));
  96. tm->tm_mon = bcd2bin(ds1302_readbyte(RTC_ADDR_MON)) - 1;
  97. tm->tm_year = bcd2bin(ds1302_readbyte(RTC_ADDR_YEAR));
  98. if (tm->tm_year < 70)
  99. tm->tm_year += 100;
  100. spin_unlock_irq(&rtc->lock);
  101. dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
  102. "mday=%d, mon=%d, year=%d, wday=%d\n",
  103. __func__,
  104. tm->tm_sec, tm->tm_min, tm->tm_hour,
  105. tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday);
  106. if (rtc_valid_tm(tm) < 0)
  107. dev_err(dev, "invalid date\n");
  108. return 0;
  109. }
  110. static int ds1302_rtc_set_time(struct device *dev, struct rtc_time *tm)
  111. {
  112. struct ds1302_rtc *rtc = dev_get_drvdata(dev);
  113. spin_lock_irq(&rtc->lock);
  114. /* Stop RTC */
  115. ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) | 0x80);
  116. ds1302_writebyte(RTC_ADDR_SEC, bin2bcd(tm->tm_sec));
  117. ds1302_writebyte(RTC_ADDR_MIN, bin2bcd(tm->tm_min));
  118. ds1302_writebyte(RTC_ADDR_HOUR, bin2bcd(tm->tm_hour));
  119. ds1302_writebyte(RTC_ADDR_DAY, bin2bcd(tm->tm_wday));
  120. ds1302_writebyte(RTC_ADDR_DATE, bin2bcd(tm->tm_mday));
  121. ds1302_writebyte(RTC_ADDR_MON, bin2bcd(tm->tm_mon + 1));
  122. ds1302_writebyte(RTC_ADDR_YEAR, bin2bcd(tm->tm_year % 100));
  123. /* Start RTC */
  124. ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) & ~0x80);
  125. spin_unlock_irq(&rtc->lock);
  126. return 0;
  127. }
  128. static int ds1302_rtc_ioctl(struct device *dev, unsigned int cmd,
  129. unsigned long arg)
  130. {
  131. switch (cmd) {
  132. #ifdef RTC_SET_CHARGE
  133. case RTC_SET_CHARGE:
  134. {
  135. struct ds1302_rtc *rtc = dev_get_drvdata(dev);
  136. int tcs_val;
  137. if (copy_from_user(&tcs_val, (int __user *)arg, sizeof(int)))
  138. return -EFAULT;
  139. spin_lock_irq(&rtc->lock);
  140. ds1302_writebyte(RTC_ADDR_TCR, (0xa0 | tcs_val * 0xf));
  141. spin_unlock_irq(&rtc->lock);
  142. return 0;
  143. }
  144. #endif
  145. }
  146. return -ENOIOCTLCMD;
  147. }
  148. static struct rtc_class_ops ds1302_rtc_ops = {
  149. .read_time = ds1302_rtc_read_time,
  150. .set_time = ds1302_rtc_set_time,
  151. .ioctl = ds1302_rtc_ioctl,
  152. };
  153. static int __devinit ds1302_rtc_probe(struct platform_device *pdev)
  154. {
  155. struct ds1302_rtc *rtc;
  156. int ret;
  157. /* Reset */
  158. set_dp(get_dp() & ~(RTC_RESET | RTC_IODATA | RTC_SCLK));
  159. /* Write a magic value to the DS1302 RAM, and see if it sticks. */
  160. ds1302_writebyte(RTC_ADDR_RAM0, 0x42);
  161. if (ds1302_readbyte(RTC_ADDR_RAM0) != 0x42)
  162. return -ENODEV;
  163. rtc = kzalloc(sizeof(struct ds1302_rtc), GFP_KERNEL);
  164. if (unlikely(!rtc))
  165. return -ENOMEM;
  166. spin_lock_init(&rtc->lock);
  167. rtc->rtc_dev = rtc_device_register("ds1302", &pdev->dev,
  168. &ds1302_rtc_ops, THIS_MODULE);
  169. if (IS_ERR(rtc->rtc_dev)) {
  170. ret = PTR_ERR(rtc->rtc_dev);
  171. goto out;
  172. }
  173. platform_set_drvdata(pdev, rtc);
  174. return 0;
  175. out:
  176. kfree(rtc);
  177. return ret;
  178. }
  179. static int __devexit ds1302_rtc_remove(struct platform_device *pdev)
  180. {
  181. struct ds1302_rtc *rtc = platform_get_drvdata(pdev);
  182. if (likely(rtc->rtc_dev))
  183. rtc_device_unregister(rtc->rtc_dev);
  184. platform_set_drvdata(pdev, NULL);
  185. kfree(rtc);
  186. return 0;
  187. }
  188. static struct platform_driver ds1302_platform_driver = {
  189. .driver = {
  190. .name = DRV_NAME,
  191. .owner = THIS_MODULE,
  192. },
  193. .probe = ds1302_rtc_probe,
  194. .remove = __devexit_p(ds1302_rtc_remove),
  195. };
  196. static int __init ds1302_rtc_init(void)
  197. {
  198. return platform_driver_register(&ds1302_platform_driver);
  199. }
  200. static void __exit ds1302_rtc_exit(void)
  201. {
  202. platform_driver_unregister(&ds1302_platform_driver);
  203. }
  204. module_init(ds1302_rtc_init);
  205. module_exit(ds1302_rtc_exit);
  206. MODULE_DESCRIPTION("Dallas DS1302 RTC driver");
  207. MODULE_VERSION(DRV_VERSION);
  208. MODULE_AUTHOR("Paul Mundt, David McCullough");
  209. MODULE_LICENSE("GPL v2");