ds1286.c 14 KB

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