ds1302.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. local_irq_disable();
  97. rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
  98. rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
  99. rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
  100. rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
  101. rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
  102. rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
  103. local_irq_restore(flags);
  104. BCD_TO_BIN(rtc_tm->tm_sec);
  105. BCD_TO_BIN(rtc_tm->tm_min);
  106. BCD_TO_BIN(rtc_tm->tm_hour);
  107. BCD_TO_BIN(rtc_tm->tm_mday);
  108. BCD_TO_BIN(rtc_tm->tm_mon);
  109. BCD_TO_BIN(rtc_tm->tm_year);
  110. /*
  111. * Account for differences between how the RTC uses the values
  112. * and how they are defined in a struct rtc_time;
  113. */
  114. if (rtc_tm->tm_year <= 69)
  115. rtc_tm->tm_year += 100;
  116. rtc_tm->tm_mon--;
  117. }
  118. static unsigned char days_in_mo[] =
  119. {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  120. /* ioctl that supports RTC_RD_TIME and RTC_SET_TIME (read and set time/date). */
  121. static int
  122. rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  123. unsigned long arg)
  124. {
  125. unsigned long flags;
  126. switch(cmd) {
  127. case RTC_RD_TIME: /* read the time/date from RTC */
  128. {
  129. struct rtc_time rtc_tm;
  130. memset(&rtc_tm, 0, sizeof (struct rtc_time));
  131. get_rtc_time(&rtc_tm);
  132. if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time)))
  133. return -EFAULT;
  134. return 0;
  135. }
  136. case RTC_SET_TIME: /* set the RTC */
  137. {
  138. struct rtc_time rtc_tm;
  139. unsigned char mon, day, hrs, min, sec, leap_yr;
  140. unsigned int yrs;
  141. if (!capable(CAP_SYS_TIME))
  142. return -EPERM;
  143. if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time)))
  144. return -EFAULT;
  145. yrs = rtc_tm.tm_year + 1900;
  146. mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
  147. day = rtc_tm.tm_mday;
  148. hrs = rtc_tm.tm_hour;
  149. min = rtc_tm.tm_min;
  150. sec = rtc_tm.tm_sec;
  151. if ((yrs < 1970) || (yrs > 2069))
  152. return -EINVAL;
  153. leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
  154. if ((mon > 12) || (day == 0))
  155. return -EINVAL;
  156. if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
  157. return -EINVAL;
  158. if ((hrs >= 24) || (min >= 60) || (sec >= 60))
  159. return -EINVAL;
  160. if (yrs >= 2000)
  161. yrs -= 2000; /* RTC (0, 1, ... 69) */
  162. else
  163. yrs -= 1900; /* RTC (70, 71, ... 99) */
  164. BIN_TO_BCD(sec);
  165. BIN_TO_BCD(min);
  166. BIN_TO_BCD(hrs);
  167. BIN_TO_BCD(day);
  168. BIN_TO_BCD(mon);
  169. BIN_TO_BCD(yrs);
  170. local_irq_save(flags);
  171. local_irq_disable();
  172. CMOS_WRITE(yrs, RTC_YEAR);
  173. CMOS_WRITE(mon, RTC_MONTH);
  174. CMOS_WRITE(day, RTC_DAY_OF_MONTH);
  175. CMOS_WRITE(hrs, RTC_HOURS);
  176. CMOS_WRITE(min, RTC_MINUTES);
  177. CMOS_WRITE(sec, RTC_SECONDS);
  178. local_irq_restore(flags);
  179. /* Notice that at this point, the RTC is updated but
  180. * the kernel is still running with the old time.
  181. * You need to set that separately with settimeofday
  182. * or adjtimex.
  183. */
  184. return 0;
  185. }
  186. case RTC_SET_CHARGE: /* set the RTC TRICKLE CHARGE register */
  187. {
  188. int tcs_val;
  189. if (!capable(CAP_SYS_TIME))
  190. return -EPERM;
  191. if(copy_from_user(&tcs_val, (int*)arg, sizeof(int)))
  192. return -EFAULT;
  193. tcs_val = RTC_TCR_PATTERN | (tcs_val & 0x0F);
  194. ds1302_writereg(RTC_TRICKLECHARGER, tcs_val);
  195. return 0;
  196. }
  197. default:
  198. return -EINVAL;
  199. }
  200. }
  201. int
  202. get_rtc_status(char *buf)
  203. {
  204. char *p;
  205. struct rtc_time tm;
  206. p = buf;
  207. get_rtc_time(&tm);
  208. /*
  209. * There is no way to tell if the luser has the RTC set for local
  210. * time or for Universal Standard Time (GMT). Probably local though.
  211. */
  212. p += sprintf(p,
  213. "rtc_time\t: %02d:%02d:%02d\n"
  214. "rtc_date\t: %04d-%02d-%02d\n",
  215. tm.tm_hour, tm.tm_min, tm.tm_sec,
  216. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
  217. return p - buf;
  218. }
  219. /* The various file operations we support. */
  220. static const struct file_operations rtc_fops = {
  221. .owner = THIS_MODULE,
  222. .ioctl = rtc_ioctl,
  223. };
  224. /* Probe for the chip by writing something to its RAM and try reading it back. */
  225. #define MAGIC_PATTERN 0x42
  226. static int __init
  227. ds1302_probe(void)
  228. {
  229. int retval, res, baur;
  230. baur=(boot_cpu_data.bus_clock/(2*1000*1000));
  231. printk("%s: Set PLD_RTCBAUR = %d\n", ds1302_name,baur);
  232. outw(0x0000,(unsigned long)PLD_RTCCR);
  233. outw(0x0000,(unsigned long)PLD_RTCRSTODT);
  234. outw(baur,(unsigned long)PLD_RTCBAUR);
  235. /* Try to talk to timekeeper. */
  236. ds1302_wenable();
  237. /* write RAM byte 0 */
  238. /* write something magic */
  239. out_byte_rtc(0xc0,MAGIC_PATTERN);
  240. /* read RAM byte 0 */
  241. if((res = in_byte_rtc(0xc1)) == MAGIC_PATTERN) {
  242. char buf[100];
  243. ds1302_wdisable();
  244. printk("%s: RTC found.\n", ds1302_name);
  245. get_rtc_status(buf);
  246. printk(buf);
  247. retval = 1;
  248. } else {
  249. printk("%s: RTC not found.\n", ds1302_name);
  250. retval = 0;
  251. }
  252. return retval;
  253. }
  254. /* Just probe for the RTC and register the device to handle the ioctl needed. */
  255. int __init
  256. ds1302_init(void)
  257. {
  258. if (!ds1302_probe()) {
  259. return -1;
  260. }
  261. return 0;
  262. }
  263. static int __init ds1302_register(void)
  264. {
  265. ds1302_init();
  266. if (register_chrdev(RTC_MAJOR_NR, ds1302_name, &rtc_fops)) {
  267. printk(KERN_INFO "%s: unable to get major %d for rtc\n",
  268. ds1302_name, RTC_MAJOR_NR);
  269. return -1;
  270. }
  271. return 0;
  272. }
  273. module_init(ds1302_register);