ds1286.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*
  2. * DS1286 Real Time Clock interface for Linux
  3. *
  4. * Copyright (C) 1998, 1999, 2000 Ralf Baechle
  5. *
  6. * Based on code written by Paul Gortmaker.
  7. *
  8. * This driver allows use of the real time clock (built into nearly all
  9. * computers) from user space. It exports the /dev/rtc interface supporting
  10. * various ioctl() and also the /proc/rtc pseudo-file for status
  11. * information.
  12. *
  13. * The ioctls can be used to set the interrupt behaviour and generation rate
  14. * from the RTC via IRQ 8. Then the /dev/rtc interface can be used to make
  15. * use of these timer interrupts, be they interval or alarm based.
  16. *
  17. * The /dev/rtc interface will block on reads until an interrupt has been
  18. * received. If a RTC interrupt has already happened, it will output an
  19. * unsigned long and then block. The output value contains the interrupt
  20. * status in the low byte and the number of interrupts since the last read
  21. * in the remaining high bytes. The /dev/rtc interface can also be used with
  22. * the select(2) call.
  23. *
  24. * This program is free software; you can redistribute it and/or modify it
  25. * under the terms of the GNU General Public License as published by the
  26. * Free Software Foundation; either version 2 of the License, or (at your
  27. * option) any later version.
  28. */
  29. #include <linux/ds1286.h>
  30. #include <linux/types.h>
  31. #include <linux/errno.h>
  32. #include <linux/miscdevice.h>
  33. #include <linux/slab.h>
  34. #include <linux/ioport.h>
  35. #include <linux/fcntl.h>
  36. #include <linux/init.h>
  37. #include <linux/poll.h>
  38. #include <linux/rtc.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/bcd.h>
  41. #include <linux/proc_fs.h>
  42. #include <asm/uaccess.h>
  43. #include <asm/system.h>
  44. #define DS1286_VERSION "1.0"
  45. /*
  46. * We sponge a minor off of the misc major. No need slurping
  47. * up another valuable major dev number for this. If you add
  48. * an ioctl, make sure you don't conflict with SPARC's RTC
  49. * ioctls.
  50. */
  51. static DECLARE_WAIT_QUEUE_HEAD(ds1286_wait);
  52. static ssize_t ds1286_read(struct file *file, char *buf,
  53. size_t count, loff_t *ppos);
  54. static int ds1286_ioctl(struct inode *inode, struct file *file,
  55. unsigned int cmd, unsigned long arg);
  56. static unsigned int ds1286_poll(struct file *file, poll_table *wait);
  57. static void ds1286_get_alm_time (struct rtc_time *alm_tm);
  58. static void ds1286_get_time(struct rtc_time *rtc_tm);
  59. static int ds1286_set_time(struct rtc_time *rtc_tm);
  60. static inline unsigned char ds1286_is_updating(void);
  61. static DEFINE_SPINLOCK(ds1286_lock);
  62. static int ds1286_read_proc(char *page, char **start, off_t off,
  63. int count, int *eof, void *data);
  64. /*
  65. * Bits in rtc_status. (7 bits of room for future expansion)
  66. */
  67. #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
  68. #define RTC_TIMER_ON 0x02 /* missed irq timer active */
  69. static unsigned char ds1286_status; /* bitmapped status byte. */
  70. static unsigned char days_in_mo[] = {
  71. 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  72. };
  73. /*
  74. * Now all the various file operations that we export.
  75. */
  76. static ssize_t ds1286_read(struct file *file, char *buf,
  77. size_t count, loff_t *ppos)
  78. {
  79. return -EIO;
  80. }
  81. static int ds1286_ioctl(struct inode *inode, struct file *file,
  82. unsigned int cmd, unsigned long arg)
  83. {
  84. struct rtc_time wtime;
  85. switch (cmd) {
  86. case RTC_AIE_OFF: /* Mask alarm int. enab. bit */
  87. {
  88. unsigned int flags;
  89. unsigned char val;
  90. if (!capable(CAP_SYS_TIME))
  91. return -EACCES;
  92. spin_lock_irqsave(&ds1286_lock, flags);
  93. val = rtc_read(RTC_CMD);
  94. val |= RTC_TDM;
  95. rtc_write(val, RTC_CMD);
  96. spin_unlock_irqrestore(&ds1286_lock, flags);
  97. return 0;
  98. }
  99. case RTC_AIE_ON: /* Allow alarm interrupts. */
  100. {
  101. unsigned int flags;
  102. unsigned char val;
  103. if (!capable(CAP_SYS_TIME))
  104. return -EACCES;
  105. spin_lock_irqsave(&ds1286_lock, flags);
  106. val = rtc_read(RTC_CMD);
  107. val &= ~RTC_TDM;
  108. rtc_write(val, RTC_CMD);
  109. spin_unlock_irqrestore(&ds1286_lock, flags);
  110. return 0;
  111. }
  112. case RTC_WIE_OFF: /* Mask watchdog int. enab. bit */
  113. {
  114. unsigned int flags;
  115. unsigned char val;
  116. if (!capable(CAP_SYS_TIME))
  117. return -EACCES;
  118. spin_lock_irqsave(&ds1286_lock, flags);
  119. val = rtc_read(RTC_CMD);
  120. val |= RTC_WAM;
  121. rtc_write(val, RTC_CMD);
  122. spin_unlock_irqrestore(&ds1286_lock, flags);
  123. return 0;
  124. }
  125. case RTC_WIE_ON: /* Allow watchdog interrupts. */
  126. {
  127. unsigned int flags;
  128. unsigned char val;
  129. if (!capable(CAP_SYS_TIME))
  130. return -EACCES;
  131. spin_lock_irqsave(&ds1286_lock, flags);
  132. val = rtc_read(RTC_CMD);
  133. val &= ~RTC_WAM;
  134. rtc_write(val, RTC_CMD);
  135. spin_unlock_irqrestore(&ds1286_lock, flags);
  136. return 0;
  137. }
  138. case RTC_ALM_READ: /* Read the present alarm time */
  139. {
  140. /*
  141. * This returns a struct rtc_time. Reading >= 0xc0
  142. * means "don't care" or "match all". Only the tm_hour,
  143. * tm_min, and tm_sec values are filled in.
  144. */
  145. memset(&wtime, 0, sizeof(wtime));
  146. ds1286_get_alm_time(&wtime);
  147. break;
  148. }
  149. case RTC_ALM_SET: /* Store a time into the alarm */
  150. {
  151. /*
  152. * This expects a struct rtc_time. Writing 0xff means
  153. * "don't care" or "match all". Only the tm_hour,
  154. * tm_min and tm_sec are used.
  155. */
  156. unsigned char hrs, min, sec;
  157. struct rtc_time alm_tm;
  158. if (!capable(CAP_SYS_TIME))
  159. return -EACCES;
  160. if (copy_from_user(&alm_tm, (struct rtc_time*)arg,
  161. sizeof(struct rtc_time)))
  162. return -EFAULT;
  163. hrs = alm_tm.tm_hour;
  164. min = alm_tm.tm_min;
  165. if (hrs >= 24)
  166. hrs = 0xff;
  167. if (min >= 60)
  168. min = 0xff;
  169. BIN_TO_BCD(sec);
  170. BIN_TO_BCD(min);
  171. BIN_TO_BCD(hrs);
  172. spin_lock(&ds1286_lock);
  173. rtc_write(hrs, RTC_HOURS_ALARM);
  174. rtc_write(min, RTC_MINUTES_ALARM);
  175. spin_unlock(&ds1286_lock);
  176. return 0;
  177. }
  178. case RTC_RD_TIME: /* Read the time/date from RTC */
  179. {
  180. memset(&wtime, 0, sizeof(wtime));
  181. ds1286_get_time(&wtime);
  182. break;
  183. }
  184. case RTC_SET_TIME: /* Set the RTC */
  185. {
  186. struct rtc_time rtc_tm;
  187. if (!capable(CAP_SYS_TIME))
  188. return -EACCES;
  189. if (copy_from_user(&rtc_tm, (struct rtc_time*)arg,
  190. sizeof(struct rtc_time)))
  191. return -EFAULT;
  192. return ds1286_set_time(&rtc_tm);
  193. }
  194. default:
  195. return -EINVAL;
  196. }
  197. return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
  198. }
  199. /*
  200. * We enforce only one user at a time here with the open/close.
  201. * Also clear the previous interrupt data on an open, and clean
  202. * up things on a close.
  203. */
  204. static int ds1286_open(struct inode *inode, struct file *file)
  205. {
  206. spin_lock_irq(&ds1286_lock);
  207. if (ds1286_status & RTC_IS_OPEN)
  208. goto out_busy;
  209. ds1286_status |= RTC_IS_OPEN;
  210. spin_unlock_irq(&ds1286_lock);
  211. return 0;
  212. out_busy:
  213. spin_lock_irq(&ds1286_lock);
  214. return -EBUSY;
  215. }
  216. static int ds1286_release(struct inode *inode, struct file *file)
  217. {
  218. ds1286_status &= ~RTC_IS_OPEN;
  219. return 0;
  220. }
  221. static unsigned int ds1286_poll(struct file *file, poll_table *wait)
  222. {
  223. poll_wait(file, &ds1286_wait, wait);
  224. return 0;
  225. }
  226. /*
  227. * The various file operations we support.
  228. */
  229. static struct file_operations ds1286_fops = {
  230. .llseek = no_llseek,
  231. .read = ds1286_read,
  232. .poll = ds1286_poll,
  233. .ioctl = ds1286_ioctl,
  234. .open = ds1286_open,
  235. .release = ds1286_release,
  236. };
  237. static struct miscdevice ds1286_dev=
  238. {
  239. .minor = RTC_MINOR,
  240. .name = "rtc",
  241. .fops = &ds1286_fops,
  242. };
  243. static int __init ds1286_init(void)
  244. {
  245. int err;
  246. printk(KERN_INFO "DS1286 Real Time Clock Driver v%s\n", DS1286_VERSION);
  247. err = misc_register(&ds1286_dev);
  248. if (err)
  249. goto out;
  250. if (!create_proc_read_entry("driver/rtc", 0, 0, ds1286_read_proc, NULL)) {
  251. err = -ENOMEM;
  252. goto out_deregister;
  253. }
  254. return 0;
  255. out_deregister:
  256. misc_deregister(&ds1286_dev);
  257. out:
  258. return err;
  259. }
  260. static void __exit ds1286_exit(void)
  261. {
  262. remove_proc_entry("driver/rtc", NULL);
  263. misc_deregister(&ds1286_dev);
  264. }
  265. static char *days[] = {
  266. "***", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  267. };
  268. /*
  269. * Info exported via "/proc/rtc".
  270. */
  271. static int ds1286_proc_output(char *buf)
  272. {
  273. char *p, *s;
  274. struct rtc_time tm;
  275. unsigned char hundredth, month, cmd, amode;
  276. p = buf;
  277. ds1286_get_time(&tm);
  278. hundredth = rtc_read(RTC_HUNDREDTH_SECOND);
  279. BCD_TO_BIN(hundredth);
  280. p += sprintf(p,
  281. "rtc_time\t: %02d:%02d:%02d.%02d\n"
  282. "rtc_date\t: %04d-%02d-%02d\n",
  283. tm.tm_hour, tm.tm_min, tm.tm_sec, hundredth,
  284. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
  285. /*
  286. * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
  287. * match any value for that particular field. Values that are
  288. * greater than a valid time, but less than 0xc0 shouldn't appear.
  289. */
  290. ds1286_get_alm_time(&tm);
  291. p += sprintf(p, "alarm\t\t: %s ", days[tm.tm_wday]);
  292. if (tm.tm_hour <= 24)
  293. p += sprintf(p, "%02d:", tm.tm_hour);
  294. else
  295. p += sprintf(p, "**:");
  296. if (tm.tm_min <= 59)
  297. p += sprintf(p, "%02d\n", tm.tm_min);
  298. else
  299. p += sprintf(p, "**\n");
  300. month = rtc_read(RTC_MONTH);
  301. p += sprintf(p,
  302. "oscillator\t: %s\n"
  303. "square_wave\t: %s\n",
  304. (month & RTC_EOSC) ? "disabled" : "enabled",
  305. (month & RTC_ESQW) ? "disabled" : "enabled");
  306. amode = ((rtc_read(RTC_MINUTES_ALARM) & 0x80) >> 5) |
  307. ((rtc_read(RTC_HOURS_ALARM) & 0x80) >> 6) |
  308. ((rtc_read(RTC_DAY_ALARM) & 0x80) >> 7);
  309. if (amode == 7) s = "each minute";
  310. else if (amode == 3) s = "minutes match";
  311. else if (amode == 1) s = "hours and minutes match";
  312. else if (amode == 0) s = "days, hours and minutes match";
  313. else s = "invalid";
  314. p += sprintf(p, "alarm_mode\t: %s\n", s);
  315. cmd = rtc_read(RTC_CMD);
  316. p += sprintf(p,
  317. "alarm_enable\t: %s\n"
  318. "wdog_alarm\t: %s\n"
  319. "alarm_mask\t: %s\n"
  320. "wdog_alarm_mask\t: %s\n"
  321. "interrupt_mode\t: %s\n"
  322. "INTB_mode\t: %s_active\n"
  323. "interrupt_pins\t: %s\n",
  324. (cmd & RTC_TDF) ? "yes" : "no",
  325. (cmd & RTC_WAF) ? "yes" : "no",
  326. (cmd & RTC_TDM) ? "disabled" : "enabled",
  327. (cmd & RTC_WAM) ? "disabled" : "enabled",
  328. (cmd & RTC_PU_LVL) ? "pulse" : "level",
  329. (cmd & RTC_IBH_LO) ? "low" : "high",
  330. (cmd & RTC_IPSW) ? "unswapped" : "swapped");
  331. return p - buf;
  332. }
  333. static int ds1286_read_proc(char *page, char **start, off_t off,
  334. int count, int *eof, void *data)
  335. {
  336. int len = ds1286_proc_output (page);
  337. if (len <= off+count) *eof = 1;
  338. *start = page + off;
  339. len -= off;
  340. if (len>count)
  341. len = count;
  342. if (len<0)
  343. len = 0;
  344. return len;
  345. }
  346. /*
  347. * Returns true if a clock update is in progress
  348. */
  349. static inline unsigned char ds1286_is_updating(void)
  350. {
  351. return rtc_read(RTC_CMD) & RTC_TE;
  352. }
  353. static void ds1286_get_time(struct rtc_time *rtc_tm)
  354. {
  355. unsigned char save_control;
  356. unsigned int flags;
  357. unsigned long uip_watchdog = jiffies;
  358. /*
  359. * read RTC once any update in progress is done. The update
  360. * can take just over 2ms. We wait 10 to 20ms. There is no need to
  361. * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
  362. * If you need to know *exactly* when a second has started, enable
  363. * periodic update complete interrupts, (via ioctl) and then
  364. * immediately read /dev/rtc which will block until you get the IRQ.
  365. * Once the read clears, read the RTC time (again via ioctl). Easy.
  366. */
  367. if (ds1286_is_updating() != 0)
  368. while (jiffies - uip_watchdog < 2*HZ/100)
  369. barrier();
  370. /*
  371. * Only the values that we read from the RTC are set. We leave
  372. * tm_wday, tm_yday and tm_isdst untouched. Even though the
  373. * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
  374. * by the RTC when initially set to a non-zero value.
  375. */
  376. spin_lock_irqsave(&ds1286_lock, flags);
  377. save_control = rtc_read(RTC_CMD);
  378. rtc_write((save_control|RTC_TE), RTC_CMD);
  379. rtc_tm->tm_sec = rtc_read(RTC_SECONDS);
  380. rtc_tm->tm_min = rtc_read(RTC_MINUTES);
  381. rtc_tm->tm_hour = rtc_read(RTC_HOURS) & 0x3f;
  382. rtc_tm->tm_mday = rtc_read(RTC_DATE);
  383. rtc_tm->tm_mon = rtc_read(RTC_MONTH) & 0x1f;
  384. rtc_tm->tm_year = rtc_read(RTC_YEAR);
  385. rtc_write(save_control, RTC_CMD);
  386. spin_unlock_irqrestore(&ds1286_lock, flags);
  387. BCD_TO_BIN(rtc_tm->tm_sec);
  388. BCD_TO_BIN(rtc_tm->tm_min);
  389. BCD_TO_BIN(rtc_tm->tm_hour);
  390. BCD_TO_BIN(rtc_tm->tm_mday);
  391. BCD_TO_BIN(rtc_tm->tm_mon);
  392. BCD_TO_BIN(rtc_tm->tm_year);
  393. /*
  394. * Account for differences between how the RTC uses the values
  395. * and how they are defined in a struct rtc_time;
  396. */
  397. if (rtc_tm->tm_year < 45)
  398. rtc_tm->tm_year += 30;
  399. if ((rtc_tm->tm_year += 40) < 70)
  400. rtc_tm->tm_year += 100;
  401. rtc_tm->tm_mon--;
  402. }
  403. static int ds1286_set_time(struct rtc_time *rtc_tm)
  404. {
  405. unsigned char mon, day, hrs, min, sec, leap_yr;
  406. unsigned char save_control;
  407. unsigned int yrs, flags;
  408. yrs = rtc_tm->tm_year + 1900;
  409. mon = rtc_tm->tm_mon + 1; /* tm_mon starts at zero */
  410. day = rtc_tm->tm_mday;
  411. hrs = rtc_tm->tm_hour;
  412. min = rtc_tm->tm_min;
  413. sec = rtc_tm->tm_sec;
  414. if (yrs < 1970)
  415. return -EINVAL;
  416. leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
  417. if ((mon > 12) || (day == 0))
  418. return -EINVAL;
  419. if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
  420. return -EINVAL;
  421. if ((hrs >= 24) || (min >= 60) || (sec >= 60))
  422. return -EINVAL;
  423. if ((yrs -= 1940) > 255) /* They are unsigned */
  424. return -EINVAL;
  425. if (yrs >= 100)
  426. yrs -= 100;
  427. BIN_TO_BCD(sec);
  428. BIN_TO_BCD(min);
  429. BIN_TO_BCD(hrs);
  430. BIN_TO_BCD(day);
  431. BIN_TO_BCD(mon);
  432. BIN_TO_BCD(yrs);
  433. spin_lock_irqsave(&ds1286_lock, flags);
  434. save_control = rtc_read(RTC_CMD);
  435. rtc_write((save_control|RTC_TE), RTC_CMD);
  436. rtc_write(yrs, RTC_YEAR);
  437. rtc_write(mon, RTC_MONTH);
  438. rtc_write(day, RTC_DATE);
  439. rtc_write(hrs, RTC_HOURS);
  440. rtc_write(min, RTC_MINUTES);
  441. rtc_write(sec, RTC_SECONDS);
  442. rtc_write(0, RTC_HUNDREDTH_SECOND);
  443. rtc_write(save_control, RTC_CMD);
  444. spin_unlock_irqrestore(&ds1286_lock, flags);
  445. return 0;
  446. }
  447. static void ds1286_get_alm_time(struct rtc_time *alm_tm)
  448. {
  449. unsigned char cmd;
  450. unsigned int flags;
  451. /*
  452. * Only the values that we read from the RTC are set. That
  453. * means only tm_wday, tm_hour, tm_min.
  454. */
  455. spin_lock_irqsave(&ds1286_lock, flags);
  456. alm_tm->tm_min = rtc_read(RTC_MINUTES_ALARM) & 0x7f;
  457. alm_tm->tm_hour = rtc_read(RTC_HOURS_ALARM) & 0x1f;
  458. alm_tm->tm_wday = rtc_read(RTC_DAY_ALARM) & 0x07;
  459. cmd = rtc_read(RTC_CMD);
  460. spin_unlock_irqrestore(&ds1286_lock, flags);
  461. BCD_TO_BIN(alm_tm->tm_min);
  462. BCD_TO_BIN(alm_tm->tm_hour);
  463. alm_tm->tm_sec = 0;
  464. }
  465. module_init(ds1286_init);
  466. module_exit(ds1286_exit);
  467. MODULE_AUTHOR("Ralf Baechle");
  468. MODULE_LICENSE("GPL");
  469. MODULE_ALIAS_MISCDEV(RTC_MINOR);