ds1302.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 <linux/mutex.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/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 DEFINE_MUTEX(rtc_mutex);
  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. 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. rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
  105. rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
  106. rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
  107. rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
  108. rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
  109. rtc_tm->tm_year = bcd2bin(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 long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  122. {
  123. unsigned long flags;
  124. switch(cmd) {
  125. case RTC_RD_TIME: /* read the time/date from RTC */
  126. {
  127. struct rtc_time rtc_tm;
  128. memset(&rtc_tm, 0, sizeof (struct rtc_time));
  129. mutex_lock(&rtc_mutex);
  130. get_rtc_time(&rtc_tm);
  131. mutex_unlock(&rtc_mutex);
  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. sec = bin2bcd(sec);
  165. min = bin2bcd(min);
  166. hrs = bin2bcd(hrs);
  167. day = bin2bcd(day);
  168. mon = bin2bcd(mon);
  169. yrs = bin2bcd(yrs);
  170. mutex_lock(&rtc_mutex);
  171. local_irq_save(flags);
  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. mutex_unlock(&rtc_mutex);
  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. mutex_lock(&rtc_mutex);
  195. tcs_val = RTC_TCR_PATTERN | (tcs_val & 0x0F);
  196. ds1302_writereg(RTC_TRICKLECHARGER, tcs_val);
  197. mutex_unlock(&rtc_mutex);
  198. return 0;
  199. }
  200. default:
  201. return -EINVAL;
  202. }
  203. }
  204. int
  205. get_rtc_status(char *buf)
  206. {
  207. char *p;
  208. struct rtc_time tm;
  209. p = buf;
  210. get_rtc_time(&tm);
  211. /*
  212. * There is no way to tell if the luser has the RTC set for local
  213. * time or for Universal Standard Time (GMT). Probably local though.
  214. */
  215. p += sprintf(p,
  216. "rtc_time\t: %02d:%02d:%02d\n"
  217. "rtc_date\t: %04d-%02d-%02d\n",
  218. tm.tm_hour, tm.tm_min, tm.tm_sec,
  219. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
  220. return p - buf;
  221. }
  222. /* The various file operations we support. */
  223. static const struct file_operations rtc_fops = {
  224. .owner = THIS_MODULE,
  225. .unlocked_ioctl = rtc_ioctl,
  226. .llseek = noop_llseek,
  227. };
  228. /* Probe for the chip by writing something to its RAM and try reading it back. */
  229. #define MAGIC_PATTERN 0x42
  230. static int __init
  231. ds1302_probe(void)
  232. {
  233. int retval, res, baur;
  234. baur=(boot_cpu_data.bus_clock/(2*1000*1000));
  235. printk("%s: Set PLD_RTCBAUR = %d\n", ds1302_name,baur);
  236. outw(0x0000,(unsigned long)PLD_RTCCR);
  237. outw(0x0000,(unsigned long)PLD_RTCRSTODT);
  238. outw(baur,(unsigned long)PLD_RTCBAUR);
  239. /* Try to talk to timekeeper. */
  240. ds1302_wenable();
  241. /* write RAM byte 0 */
  242. /* write something magic */
  243. out_byte_rtc(0xc0,MAGIC_PATTERN);
  244. /* read RAM byte 0 */
  245. if((res = in_byte_rtc(0xc1)) == MAGIC_PATTERN) {
  246. char buf[100];
  247. ds1302_wdisable();
  248. printk("%s: RTC found.\n", ds1302_name);
  249. get_rtc_status(buf);
  250. printk(buf);
  251. retval = 1;
  252. } else {
  253. printk("%s: RTC not found.\n", ds1302_name);
  254. retval = 0;
  255. }
  256. return retval;
  257. }
  258. /* Just probe for the RTC and register the device to handle the ioctl needed. */
  259. int __init
  260. ds1302_init(void)
  261. {
  262. if (!ds1302_probe()) {
  263. return -1;
  264. }
  265. return 0;
  266. }
  267. static int __init ds1302_register(void)
  268. {
  269. ds1302_init();
  270. if (register_chrdev(RTC_MAJOR_NR, ds1302_name, &rtc_fops)) {
  271. printk(KERN_INFO "%s: unable to get major %d for rtc\n",
  272. ds1302_name, RTC_MAJOR_NR);
  273. return -1;
  274. }
  275. return 0;
  276. }
  277. module_init(ds1302_register);