ds1302.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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/system.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 DEFINE_MUTEX(rtc_mutex);
  31. static const char ds1302_name[] = "ds1302";
  32. /* Send 8 bits. */
  33. static void
  34. out_byte_rtc(unsigned int reg_addr, unsigned char x)
  35. {
  36. //RST H
  37. outw(0x0001,(unsigned long)PLD_RTCRSTODT);
  38. //write data
  39. outw(((x<<8)|(reg_addr&0xff)),(unsigned long)PLD_RTCWRDATA);
  40. //WE
  41. outw(0x0002,(unsigned long)PLD_RTCCR);
  42. //wait
  43. while(inw((unsigned long)PLD_RTCCR));
  44. //RST L
  45. outw(0x0000,(unsigned long)PLD_RTCRSTODT);
  46. }
  47. static unsigned char
  48. in_byte_rtc(unsigned int reg_addr)
  49. {
  50. unsigned char retval;
  51. //RST H
  52. outw(0x0001,(unsigned long)PLD_RTCRSTODT);
  53. //write data
  54. outw((reg_addr&0xff),(unsigned long)PLD_RTCRDDATA);
  55. //RE
  56. outw(0x0001,(unsigned long)PLD_RTCCR);
  57. //wait
  58. while(inw((unsigned long)PLD_RTCCR));
  59. //read data
  60. retval=(inw((unsigned long)PLD_RTCRDDATA) & 0xff00)>>8;
  61. //RST L
  62. outw(0x0000,(unsigned long)PLD_RTCRSTODT);
  63. return retval;
  64. }
  65. /* Enable writing. */
  66. static void
  67. ds1302_wenable(void)
  68. {
  69. out_byte_rtc(0x8e,0x00);
  70. }
  71. /* Disable writing. */
  72. static void
  73. ds1302_wdisable(void)
  74. {
  75. out_byte_rtc(0x8e,0x80);
  76. }
  77. /* Read a byte from the selected register in the DS1302. */
  78. unsigned char
  79. ds1302_readreg(int reg)
  80. {
  81. unsigned char x;
  82. x=in_byte_rtc((0x81 | (reg << 1))); /* read register */
  83. return x;
  84. }
  85. /* Write a byte to the selected register. */
  86. void
  87. ds1302_writereg(int reg, unsigned char val)
  88. {
  89. ds1302_wenable();
  90. out_byte_rtc((0x80 | (reg << 1)),val);
  91. ds1302_wdisable();
  92. }
  93. void
  94. get_rtc_time(struct rtc_time *rtc_tm)
  95. {
  96. unsigned long flags;
  97. local_irq_save(flags);
  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. rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
  106. rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
  107. rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
  108. rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
  109. rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
  110. rtc_tm->tm_year = bcd2bin(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 long rtc_ioctl(struct file *file, unsigned int cmd, 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. mutex_lock(&rtc_mutex);
  131. get_rtc_time(&rtc_tm);
  132. mutex_unlock(&rtc_mutex);
  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. sec = bin2bcd(sec);
  166. min = bin2bcd(min);
  167. hrs = bin2bcd(hrs);
  168. day = bin2bcd(day);
  169. mon = bin2bcd(mon);
  170. yrs = bin2bcd(yrs);
  171. mutex_lock(&rtc_mutex);
  172. local_irq_save(flags);
  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. mutex_unlock(&rtc_mutex);
  181. /* Notice that at this point, the RTC is updated but
  182. * the kernel is still running with the old time.
  183. * You need to set that separately with settimeofday
  184. * or adjtimex.
  185. */
  186. return 0;
  187. }
  188. case RTC_SET_CHARGE: /* set the RTC TRICKLE CHARGE register */
  189. {
  190. int tcs_val;
  191. if (!capable(CAP_SYS_TIME))
  192. return -EPERM;
  193. if(copy_from_user(&tcs_val, (int*)arg, sizeof(int)))
  194. return -EFAULT;
  195. mutex_lock(&rtc_mutex);
  196. tcs_val = RTC_TCR_PATTERN | (tcs_val & 0x0F);
  197. ds1302_writereg(RTC_TRICKLECHARGER, tcs_val);
  198. mutex_unlock(&rtc_mutex);
  199. return 0;
  200. }
  201. default:
  202. return -EINVAL;
  203. }
  204. }
  205. int
  206. get_rtc_status(char *buf)
  207. {
  208. char *p;
  209. struct rtc_time tm;
  210. p = buf;
  211. get_rtc_time(&tm);
  212. /*
  213. * There is no way to tell if the luser has the RTC set for local
  214. * time or for Universal Standard Time (GMT). Probably local though.
  215. */
  216. p += sprintf(p,
  217. "rtc_time\t: %02d:%02d:%02d\n"
  218. "rtc_date\t: %04d-%02d-%02d\n",
  219. tm.tm_hour, tm.tm_min, tm.tm_sec,
  220. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
  221. return p - buf;
  222. }
  223. /* The various file operations we support. */
  224. static const struct file_operations rtc_fops = {
  225. .owner = THIS_MODULE,
  226. .unlocked_ioctl = rtc_ioctl,
  227. .llseek = noop_llseek,
  228. };
  229. /* Probe for the chip by writing something to its RAM and try reading it back. */
  230. #define MAGIC_PATTERN 0x42
  231. static int __init
  232. ds1302_probe(void)
  233. {
  234. int retval, res, baur;
  235. baur=(boot_cpu_data.bus_clock/(2*1000*1000));
  236. printk("%s: Set PLD_RTCBAUR = %d\n", ds1302_name,baur);
  237. outw(0x0000,(unsigned long)PLD_RTCCR);
  238. outw(0x0000,(unsigned long)PLD_RTCRSTODT);
  239. outw(baur,(unsigned long)PLD_RTCBAUR);
  240. /* Try to talk to timekeeper. */
  241. ds1302_wenable();
  242. /* write RAM byte 0 */
  243. /* write something magic */
  244. out_byte_rtc(0xc0,MAGIC_PATTERN);
  245. /* read RAM byte 0 */
  246. if((res = in_byte_rtc(0xc1)) == MAGIC_PATTERN) {
  247. char buf[100];
  248. ds1302_wdisable();
  249. printk("%s: RTC found.\n", ds1302_name);
  250. get_rtc_status(buf);
  251. printk(buf);
  252. retval = 1;
  253. } else {
  254. printk("%s: RTC not found.\n", ds1302_name);
  255. retval = 0;
  256. }
  257. return retval;
  258. }
  259. /* Just probe for the RTC and register the device to handle the ioctl needed. */
  260. int __init
  261. ds1302_init(void)
  262. {
  263. if (!ds1302_probe()) {
  264. return -1;
  265. }
  266. return 0;
  267. }
  268. static int __init ds1302_register(void)
  269. {
  270. ds1302_init();
  271. if (register_chrdev(RTC_MAJOR_NR, ds1302_name, &rtc_fops)) {
  272. printk(KERN_INFO "%s: unable to get major %d for rtc\n",
  273. ds1302_name, RTC_MAJOR_NR);
  274. return -1;
  275. }
  276. return 0;
  277. }
  278. module_init(ds1302_register);