ds1302.c 7.2 KB

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