ds1286.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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 long 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 long 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 long 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 long 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. sec = alm_tm.tm_sec;
  166. if (hrs >= 24)
  167. hrs = 0xff;
  168. if (min >= 60)
  169. min = 0xff;
  170. if (sec != 0)
  171. return -EINVAL;
  172. min = BIN2BCD(min);
  173. min = BIN2BCD(hrs);
  174. spin_lock(&ds1286_lock);
  175. rtc_write(hrs, RTC_HOURS_ALARM);
  176. rtc_write(min, RTC_MINUTES_ALARM);
  177. spin_unlock(&ds1286_lock);
  178. return 0;
  179. }
  180. case RTC_RD_TIME: /* Read the time/date from RTC */
  181. {
  182. memset(&wtime, 0, sizeof(wtime));
  183. ds1286_get_time(&wtime);
  184. break;
  185. }
  186. case RTC_SET_TIME: /* Set the RTC */
  187. {
  188. struct rtc_time rtc_tm;
  189. if (!capable(CAP_SYS_TIME))
  190. return -EACCES;
  191. if (copy_from_user(&rtc_tm, (struct rtc_time*)arg,
  192. sizeof(struct rtc_time)))
  193. return -EFAULT;
  194. return ds1286_set_time(&rtc_tm);
  195. }
  196. default:
  197. return -EINVAL;
  198. }
  199. return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
  200. }
  201. /*
  202. * We enforce only one user at a time here with the open/close.
  203. * Also clear the previous interrupt data on an open, and clean
  204. * up things on a close.
  205. */
  206. static int ds1286_open(struct inode *inode, struct file *file)
  207. {
  208. spin_lock_irq(&ds1286_lock);
  209. if (ds1286_status & RTC_IS_OPEN)
  210. goto out_busy;
  211. ds1286_status |= RTC_IS_OPEN;
  212. spin_unlock_irq(&ds1286_lock);
  213. return 0;
  214. out_busy:
  215. spin_lock_irq(&ds1286_lock);
  216. return -EBUSY;
  217. }
  218. static int ds1286_release(struct inode *inode, struct file *file)
  219. {
  220. ds1286_status &= ~RTC_IS_OPEN;
  221. return 0;
  222. }
  223. static unsigned int ds1286_poll(struct file *file, poll_table *wait)
  224. {
  225. poll_wait(file, &ds1286_wait, wait);
  226. return 0;
  227. }
  228. /*
  229. * The various file operations we support.
  230. */
  231. static const struct file_operations ds1286_fops = {
  232. .llseek = no_llseek,
  233. .read = ds1286_read,
  234. .poll = ds1286_poll,
  235. .ioctl = ds1286_ioctl,
  236. .open = ds1286_open,
  237. .release = ds1286_release,
  238. };
  239. static struct miscdevice ds1286_dev=
  240. {
  241. .minor = RTC_MINOR,
  242. .name = "rtc",
  243. .fops = &ds1286_fops,
  244. };
  245. static int __init ds1286_init(void)
  246. {
  247. int err;
  248. printk(KERN_INFO "DS1286 Real Time Clock Driver v%s\n", DS1286_VERSION);
  249. err = misc_register(&ds1286_dev);
  250. if (err)
  251. goto out;
  252. if (!create_proc_read_entry("driver/rtc", 0, 0, ds1286_read_proc, NULL)) {
  253. err = -ENOMEM;
  254. goto out_deregister;
  255. }
  256. return 0;
  257. out_deregister:
  258. misc_deregister(&ds1286_dev);
  259. out:
  260. return err;
  261. }
  262. static void __exit ds1286_exit(void)
  263. {
  264. remove_proc_entry("driver/rtc", NULL);
  265. misc_deregister(&ds1286_dev);
  266. }
  267. static char *days[] = {
  268. "***", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  269. };
  270. /*
  271. * Info exported via "/proc/rtc".
  272. */
  273. static int ds1286_proc_output(char *buf)
  274. {
  275. char *p, *s;
  276. struct rtc_time tm;
  277. unsigned char hundredth, month, cmd, amode;
  278. p = buf;
  279. ds1286_get_time(&tm);
  280. hundredth = rtc_read(RTC_HUNDREDTH_SECOND);
  281. BCD_TO_BIN(hundredth);
  282. p += sprintf(p,
  283. "rtc_time\t: %02d:%02d:%02d.%02d\n"
  284. "rtc_date\t: %04d-%02d-%02d\n",
  285. tm.tm_hour, tm.tm_min, tm.tm_sec, hundredth,
  286. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
  287. /*
  288. * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
  289. * match any value for that particular field. Values that are
  290. * greater than a valid time, but less than 0xc0 shouldn't appear.
  291. */
  292. ds1286_get_alm_time(&tm);
  293. p += sprintf(p, "alarm\t\t: %s ", days[tm.tm_wday]);
  294. if (tm.tm_hour <= 24)
  295. p += sprintf(p, "%02d:", tm.tm_hour);
  296. else
  297. p += sprintf(p, "**:");
  298. if (tm.tm_min <= 59)
  299. p += sprintf(p, "%02d\n", tm.tm_min);
  300. else
  301. p += sprintf(p, "**\n");
  302. month = rtc_read(RTC_MONTH);
  303. p += sprintf(p,
  304. "oscillator\t: %s\n"
  305. "square_wave\t: %s\n",
  306. (month & RTC_EOSC) ? "disabled" : "enabled",
  307. (month & RTC_ESQW) ? "disabled" : "enabled");
  308. amode = ((rtc_read(RTC_MINUTES_ALARM) & 0x80) >> 5) |
  309. ((rtc_read(RTC_HOURS_ALARM) & 0x80) >> 6) |
  310. ((rtc_read(RTC_DAY_ALARM) & 0x80) >> 7);
  311. if (amode == 7) s = "each minute";
  312. else if (amode == 3) s = "minutes match";
  313. else if (amode == 1) s = "hours and minutes match";
  314. else if (amode == 0) s = "days, hours and minutes match";
  315. else s = "invalid";
  316. p += sprintf(p, "alarm_mode\t: %s\n", s);
  317. cmd = rtc_read(RTC_CMD);
  318. p += sprintf(p,
  319. "alarm_enable\t: %s\n"
  320. "wdog_alarm\t: %s\n"
  321. "alarm_mask\t: %s\n"
  322. "wdog_alarm_mask\t: %s\n"
  323. "interrupt_mode\t: %s\n"
  324. "INTB_mode\t: %s_active\n"
  325. "interrupt_pins\t: %s\n",
  326. (cmd & RTC_TDF) ? "yes" : "no",
  327. (cmd & RTC_WAF) ? "yes" : "no",
  328. (cmd & RTC_TDM) ? "disabled" : "enabled",
  329. (cmd & RTC_WAM) ? "disabled" : "enabled",
  330. (cmd & RTC_PU_LVL) ? "pulse" : "level",
  331. (cmd & RTC_IBH_LO) ? "low" : "high",
  332. (cmd & RTC_IPSW) ? "unswapped" : "swapped");
  333. return p - buf;
  334. }
  335. static int ds1286_read_proc(char *page, char **start, off_t off,
  336. int count, int *eof, void *data)
  337. {
  338. int len = ds1286_proc_output (page);
  339. if (len <= off+count) *eof = 1;
  340. *start = page + off;
  341. len -= off;
  342. if (len>count)
  343. len = count;
  344. if (len<0)
  345. len = 0;
  346. return len;
  347. }
  348. /*
  349. * Returns true if a clock update is in progress
  350. */
  351. static inline unsigned char ds1286_is_updating(void)
  352. {
  353. return rtc_read(RTC_CMD) & RTC_TE;
  354. }
  355. static void ds1286_get_time(struct rtc_time *rtc_tm)
  356. {
  357. unsigned char save_control;
  358. unsigned long flags;
  359. unsigned long uip_watchdog = jiffies;
  360. /*
  361. * read RTC once any update in progress is done. The update
  362. * can take just over 2ms. We wait 10 to 20ms. There is no need to
  363. * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
  364. * If you need to know *exactly* when a second has started, enable
  365. * periodic update complete interrupts, (via ioctl) and then
  366. * immediately read /dev/rtc which will block until you get the IRQ.
  367. * Once the read clears, read the RTC time (again via ioctl). Easy.
  368. */
  369. if (ds1286_is_updating() != 0)
  370. while (jiffies - uip_watchdog < 2*HZ/100)
  371. barrier();
  372. /*
  373. * Only the values that we read from the RTC are set. We leave
  374. * tm_wday, tm_yday and tm_isdst untouched. Even though the
  375. * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
  376. * by the RTC when initially set to a non-zero value.
  377. */
  378. spin_lock_irqsave(&ds1286_lock, flags);
  379. save_control = rtc_read(RTC_CMD);
  380. rtc_write((save_control|RTC_TE), RTC_CMD);
  381. rtc_tm->tm_sec = rtc_read(RTC_SECONDS);
  382. rtc_tm->tm_min = rtc_read(RTC_MINUTES);
  383. rtc_tm->tm_hour = rtc_read(RTC_HOURS) & 0x3f;
  384. rtc_tm->tm_mday = rtc_read(RTC_DATE);
  385. rtc_tm->tm_mon = rtc_read(RTC_MONTH) & 0x1f;
  386. rtc_tm->tm_year = rtc_read(RTC_YEAR);
  387. rtc_write(save_control, RTC_CMD);
  388. spin_unlock_irqrestore(&ds1286_lock, flags);
  389. BCD_TO_BIN(rtc_tm->tm_sec);
  390. BCD_TO_BIN(rtc_tm->tm_min);
  391. BCD_TO_BIN(rtc_tm->tm_hour);
  392. BCD_TO_BIN(rtc_tm->tm_mday);
  393. BCD_TO_BIN(rtc_tm->tm_mon);
  394. BCD_TO_BIN(rtc_tm->tm_year);
  395. /*
  396. * Account for differences between how the RTC uses the values
  397. * and how they are defined in a struct rtc_time;
  398. */
  399. if (rtc_tm->tm_year < 45)
  400. rtc_tm->tm_year += 30;
  401. if ((rtc_tm->tm_year += 40) < 70)
  402. rtc_tm->tm_year += 100;
  403. rtc_tm->tm_mon--;
  404. }
  405. static int ds1286_set_time(struct rtc_time *rtc_tm)
  406. {
  407. unsigned char mon, day, hrs, min, sec, leap_yr;
  408. unsigned char save_control;
  409. unsigned int yrs;
  410. unsigned long flags;
  411. yrs = rtc_tm->tm_year + 1900;
  412. mon = rtc_tm->tm_mon + 1; /* tm_mon starts at zero */
  413. day = rtc_tm->tm_mday;
  414. hrs = rtc_tm->tm_hour;
  415. min = rtc_tm->tm_min;
  416. sec = rtc_tm->tm_sec;
  417. if (yrs < 1970)
  418. return -EINVAL;
  419. leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
  420. if ((mon > 12) || (day == 0))
  421. return -EINVAL;
  422. if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
  423. return -EINVAL;
  424. if ((hrs >= 24) || (min >= 60) || (sec >= 60))
  425. return -EINVAL;
  426. if ((yrs -= 1940) > 255) /* They are unsigned */
  427. return -EINVAL;
  428. if (yrs >= 100)
  429. yrs -= 100;
  430. BIN_TO_BCD(sec);
  431. BIN_TO_BCD(min);
  432. BIN_TO_BCD(hrs);
  433. BIN_TO_BCD(day);
  434. BIN_TO_BCD(mon);
  435. BIN_TO_BCD(yrs);
  436. spin_lock_irqsave(&ds1286_lock, flags);
  437. save_control = rtc_read(RTC_CMD);
  438. rtc_write((save_control|RTC_TE), RTC_CMD);
  439. rtc_write(yrs, RTC_YEAR);
  440. rtc_write(mon, RTC_MONTH);
  441. rtc_write(day, RTC_DATE);
  442. rtc_write(hrs, RTC_HOURS);
  443. rtc_write(min, RTC_MINUTES);
  444. rtc_write(sec, RTC_SECONDS);
  445. rtc_write(0, RTC_HUNDREDTH_SECOND);
  446. rtc_write(save_control, RTC_CMD);
  447. spin_unlock_irqrestore(&ds1286_lock, flags);
  448. return 0;
  449. }
  450. static void ds1286_get_alm_time(struct rtc_time *alm_tm)
  451. {
  452. unsigned char cmd;
  453. unsigned long flags;
  454. /*
  455. * Only the values that we read from the RTC are set. That
  456. * means only tm_wday, tm_hour, tm_min.
  457. */
  458. spin_lock_irqsave(&ds1286_lock, flags);
  459. alm_tm->tm_min = rtc_read(RTC_MINUTES_ALARM) & 0x7f;
  460. alm_tm->tm_hour = rtc_read(RTC_HOURS_ALARM) & 0x1f;
  461. alm_tm->tm_wday = rtc_read(RTC_DAY_ALARM) & 0x07;
  462. cmd = rtc_read(RTC_CMD);
  463. spin_unlock_irqrestore(&ds1286_lock, flags);
  464. BCD_TO_BIN(alm_tm->tm_min);
  465. BCD_TO_BIN(alm_tm->tm_hour);
  466. alm_tm->tm_sec = 0;
  467. }
  468. module_init(ds1286_init);
  469. module_exit(ds1286_exit);
  470. MODULE_AUTHOR("Ralf Baechle");
  471. MODULE_LICENSE("GPL");
  472. MODULE_ALIAS_MISCDEV(RTC_MINOR);