ds1302.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*!***************************************************************************
  2. *!
  3. *! FILE NAME : ds1302.c
  4. *!
  5. *! DESCRIPTION: Implements an interface for the DS1302 RTC
  6. *!
  7. *! Functions exported: ds1302_readreg, ds1302_writereg, ds1302_init, get_rtc_status
  8. *!
  9. *! ---------------------------------------------------------------------------
  10. *!
  11. *! (C) Copyright 1999, 2000, 2001 Axis Communications AB, LUND, SWEDEN
  12. *!
  13. *!***************************************************************************/
  14. #include <linux/config.h>
  15. #include <linux/fs.h>
  16. #include <linux/init.h>
  17. #include <linux/mm.h>
  18. #include <linux/module.h>
  19. #include <linux/miscdevice.h>
  20. #include <linux/delay.h>
  21. #include <linux/bcd.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/system.h>
  24. #include <asm/io.h>
  25. #include <asm/rtc.h>
  26. #if defined(CONFIG_M32R)
  27. #include <asm/m32r.h>
  28. #endif
  29. #define RTC_MAJOR_NR 121 /* local major, change later */
  30. static const char ds1302_name[] = "ds1302";
  31. /* Send 8 bits. */
  32. static void
  33. out_byte_rtc(unsigned int reg_addr, unsigned char x)
  34. {
  35. //RST H
  36. outw(0x0001,(unsigned long)PLD_RTCRSTODT);
  37. //write data
  38. outw(((x<<8)|(reg_addr&0xff)),(unsigned long)PLD_RTCWRDATA);
  39. //WE
  40. outw(0x0002,(unsigned long)PLD_RTCCR);
  41. //wait
  42. while(inw((unsigned long)PLD_RTCCR));
  43. //RST L
  44. outw(0x0000,(unsigned long)PLD_RTCRSTODT);
  45. }
  46. static unsigned char
  47. in_byte_rtc(unsigned int reg_addr)
  48. {
  49. unsigned char retval;
  50. //RST H
  51. outw(0x0001,(unsigned long)PLD_RTCRSTODT);
  52. //write data
  53. outw((reg_addr&0xff),(unsigned long)PLD_RTCRDDATA);
  54. //RE
  55. outw(0x0001,(unsigned long)PLD_RTCCR);
  56. //wait
  57. while(inw((unsigned long)PLD_RTCCR));
  58. //read data
  59. retval=(inw((unsigned long)PLD_RTCRDDATA) & 0xff00)>>8;
  60. //RST L
  61. outw(0x0000,(unsigned long)PLD_RTCRSTODT);
  62. return retval;
  63. }
  64. /* Enable writing. */
  65. static void
  66. ds1302_wenable(void)
  67. {
  68. out_byte_rtc(0x8e,0x00);
  69. }
  70. /* Disable writing. */
  71. static void
  72. ds1302_wdisable(void)
  73. {
  74. out_byte_rtc(0x8e,0x80);
  75. }
  76. /* Read a byte from the selected register in the DS1302. */
  77. unsigned char
  78. ds1302_readreg(int reg)
  79. {
  80. unsigned char x;
  81. x=in_byte_rtc((0x81 | (reg << 1))); /* read register */
  82. return x;
  83. }
  84. /* Write a byte to the selected register. */
  85. void
  86. ds1302_writereg(int reg, unsigned char val)
  87. {
  88. ds1302_wenable();
  89. out_byte_rtc((0x80 | (reg << 1)),val);
  90. ds1302_wdisable();
  91. }
  92. void
  93. get_rtc_time(struct rtc_time *rtc_tm)
  94. {
  95. unsigned long flags;
  96. local_irq_save(flags);
  97. local_irq_disable();
  98. rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
  99. rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
  100. rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
  101. rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
  102. rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
  103. rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
  104. local_irq_restore(flags);
  105. BCD_TO_BIN(rtc_tm->tm_sec);
  106. BCD_TO_BIN(rtc_tm->tm_min);
  107. BCD_TO_BIN(rtc_tm->tm_hour);
  108. BCD_TO_BIN(rtc_tm->tm_mday);
  109. BCD_TO_BIN(rtc_tm->tm_mon);
  110. BCD_TO_BIN(rtc_tm->tm_year);
  111. /*
  112. * Account for differences between how the RTC uses the values
  113. * and how they are defined in a struct rtc_time;
  114. */
  115. if (rtc_tm->tm_year <= 69)
  116. rtc_tm->tm_year += 100;
  117. rtc_tm->tm_mon--;
  118. }
  119. static unsigned char days_in_mo[] =
  120. {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  121. /* ioctl that supports RTC_RD_TIME and RTC_SET_TIME (read and set time/date). */
  122. static int
  123. rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  124. unsigned long arg)
  125. {
  126. unsigned long flags;
  127. switch(cmd) {
  128. case RTC_RD_TIME: /* read the time/date from RTC */
  129. {
  130. struct rtc_time rtc_tm;
  131. memset(&rtc_tm, 0, sizeof (struct rtc_time));
  132. get_rtc_time(&rtc_tm);
  133. if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time)))
  134. return -EFAULT;
  135. return 0;
  136. }
  137. case RTC_SET_TIME: /* set the RTC */
  138. {
  139. struct rtc_time rtc_tm;
  140. unsigned char mon, day, hrs, min, sec, leap_yr;
  141. unsigned int yrs;
  142. if (!capable(CAP_SYS_TIME))
  143. return -EPERM;
  144. if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time)))
  145. return -EFAULT;
  146. yrs = rtc_tm.tm_year + 1900;
  147. mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
  148. day = rtc_tm.tm_mday;
  149. hrs = rtc_tm.tm_hour;
  150. min = rtc_tm.tm_min;
  151. sec = rtc_tm.tm_sec;
  152. if ((yrs < 1970) || (yrs > 2069))
  153. return -EINVAL;
  154. leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
  155. if ((mon > 12) || (day == 0))
  156. return -EINVAL;
  157. if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
  158. return -EINVAL;
  159. if ((hrs >= 24) || (min >= 60) || (sec >= 60))
  160. return -EINVAL;
  161. if (yrs >= 2000)
  162. yrs -= 2000; /* RTC (0, 1, ... 69) */
  163. else
  164. yrs -= 1900; /* RTC (70, 71, ... 99) */
  165. BIN_TO_BCD(sec);
  166. BIN_TO_BCD(min);
  167. BIN_TO_BCD(hrs);
  168. BIN_TO_BCD(day);
  169. BIN_TO_BCD(mon);
  170. BIN_TO_BCD(yrs);
  171. local_irq_save(flags);
  172. local_irq_disable();
  173. CMOS_WRITE(yrs, RTC_YEAR);
  174. CMOS_WRITE(mon, RTC_MONTH);
  175. CMOS_WRITE(day, RTC_DAY_OF_MONTH);
  176. CMOS_WRITE(hrs, RTC_HOURS);
  177. CMOS_WRITE(min, RTC_MINUTES);
  178. CMOS_WRITE(sec, RTC_SECONDS);
  179. local_irq_restore(flags);
  180. /* Notice that at this point, the RTC is updated but
  181. * the kernel is still running with the old time.
  182. * You need to set that separately with settimeofday
  183. * or adjtimex.
  184. */
  185. return 0;
  186. }
  187. case RTC_SET_CHARGE: /* set the RTC TRICKLE CHARGE register */
  188. {
  189. int tcs_val;
  190. if (!capable(CAP_SYS_TIME))
  191. return -EPERM;
  192. if(copy_from_user(&tcs_val, (int*)arg, sizeof(int)))
  193. return -EFAULT;
  194. tcs_val = RTC_TCR_PATTERN | (tcs_val & 0x0F);
  195. ds1302_writereg(RTC_TRICKLECHARGER, tcs_val);
  196. return 0;
  197. }
  198. default:
  199. return -EINVAL;
  200. }
  201. }
  202. int
  203. get_rtc_status(char *buf)
  204. {
  205. char *p;
  206. struct rtc_time tm;
  207. p = buf;
  208. get_rtc_time(&tm);
  209. /*
  210. * There is no way to tell if the luser has the RTC set for local
  211. * time or for Universal Standard Time (GMT). Probably local though.
  212. */
  213. p += sprintf(p,
  214. "rtc_time\t: %02d:%02d:%02d\n"
  215. "rtc_date\t: %04d-%02d-%02d\n",
  216. tm.tm_hour, tm.tm_min, tm.tm_sec,
  217. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
  218. return p - buf;
  219. }
  220. /* The various file operations we support. */
  221. static struct file_operations rtc_fops = {
  222. .owner = THIS_MODULE,
  223. .ioctl = rtc_ioctl,
  224. };
  225. /* Probe for the chip by writing something to its RAM and try reading it back. */
  226. #define MAGIC_PATTERN 0x42
  227. static int __init
  228. ds1302_probe(void)
  229. {
  230. int retval, res, baur;
  231. baur=(boot_cpu_data.bus_clock/(2*1000*1000));
  232. printk("%s: Set PLD_RTCBAUR = %d\n", ds1302_name,baur);
  233. outw(0x0000,(unsigned long)PLD_RTCCR);
  234. outw(0x0000,(unsigned long)PLD_RTCRSTODT);
  235. outw(baur,(unsigned long)PLD_RTCBAUR);
  236. /* Try to talk to timekeeper. */
  237. ds1302_wenable();
  238. /* write RAM byte 0 */
  239. /* write something magic */
  240. out_byte_rtc(0xc0,MAGIC_PATTERN);
  241. /* read RAM byte 0 */
  242. if((res = in_byte_rtc(0xc1)) == MAGIC_PATTERN) {
  243. char buf[100];
  244. ds1302_wdisable();
  245. printk("%s: RTC found.\n", ds1302_name);
  246. get_rtc_status(buf);
  247. printk(buf);
  248. retval = 1;
  249. } else {
  250. printk("%s: RTC not found.\n", ds1302_name);
  251. retval = 0;
  252. }
  253. return retval;
  254. }
  255. /* Just probe for the RTC and register the device to handle the ioctl needed. */
  256. int __init
  257. ds1302_init(void)
  258. {
  259. if (!ds1302_probe()) {
  260. return -1;
  261. }
  262. return 0;
  263. }
  264. static int __init ds1302_register(void)
  265. {
  266. ds1302_init();
  267. if (register_chrdev(RTC_MAJOR_NR, ds1302_name, &rtc_fops)) {
  268. printk(KERN_INFO "%s: unable to get major %d for rtc\n",
  269. ds1302_name, RTC_MAJOR_NR);
  270. return -1;
  271. }
  272. return 0;
  273. }
  274. module_init(ds1302_register);