ds1286.c 14 KB

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