rtc.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. /*
  2. * Real Time Clock interface for Linux
  3. *
  4. * Copyright (C) 1996 Paul Gortmaker
  5. *
  6. * This driver allows use of the real time clock (built into
  7. * nearly all computers) from user space. It exports the /dev/rtc
  8. * interface supporting various ioctl() and also the
  9. * /proc/driver/rtc pseudo-file for status information.
  10. *
  11. * The ioctls can be used to set the interrupt behaviour and
  12. * generation rate from the RTC via IRQ 8. Then the /dev/rtc
  13. * interface can be used to make use of these timer interrupts,
  14. * be they interval or alarm based.
  15. *
  16. * The /dev/rtc interface will block on reads until an interrupt
  17. * has been received. If a RTC interrupt has already happened,
  18. * it will output an unsigned long and then block. The output value
  19. * contains the interrupt status in the low byte and the number of
  20. * interrupts since the last read in the remaining high bytes. The
  21. * /dev/rtc interface can also be used with the select(2) call.
  22. *
  23. * This program is free software; you can redistribute it and/or
  24. * modify it under the terms of the GNU General Public License
  25. * as published by the Free Software Foundation; either version
  26. * 2 of the License, or (at your option) any later version.
  27. *
  28. * Based on other minimal char device drivers, like Alan's
  29. * watchdog, Ted's random, etc. etc.
  30. *
  31. * 1.07 Paul Gortmaker.
  32. * 1.08 Miquel van Smoorenburg: disallow certain things on the
  33. * DEC Alpha as the CMOS clock is also used for other things.
  34. * 1.09 Nikita Schmidt: epoch support and some Alpha cleanup.
  35. * 1.09a Pete Zaitcev: Sun SPARC
  36. * 1.09b Jeff Garzik: Modularize, init cleanup
  37. * 1.09c Jeff Garzik: SMP cleanup
  38. * 1.10 Paul Barton-Davis: add support for async I/O
  39. * 1.10a Andrea Arcangeli: Alpha updates
  40. * 1.10b Andrew Morton: SMP lock fix
  41. * 1.10c Cesar Barros: SMP locking fixes and cleanup
  42. * 1.10d Paul Gortmaker: delete paranoia check in rtc_exit
  43. * 1.10e Maciej W. Rozycki: Handle DECstation's year weirdness.
  44. * 1.11 Takashi Iwai: Kernel access functions
  45. * rtc_register/rtc_unregister/rtc_control
  46. * 1.11a Daniele Bellucci: Audit create_proc_read_entry in rtc_init
  47. * 1.12 Venkatesh Pallipadi: Hooks for emulating rtc on HPET base-timer
  48. * CONFIG_HPET_EMULATE_RTC
  49. * 1.12a Maciej W. Rozycki: Handle memory-mapped chips properly.
  50. * 1.12ac Alan Cox: Allow read access to the day of week register
  51. * 1.12b David John: Remove calls to the BKL.
  52. */
  53. #define RTC_VERSION "1.12b"
  54. /*
  55. * Note that *all* calls to CMOS_READ and CMOS_WRITE are done with
  56. * interrupts disabled. Due to the index-port/data-port (0x70/0x71)
  57. * design of the RTC, we don't want two different things trying to
  58. * get to it at once. (e.g. the periodic 11 min sync from
  59. * kernel/time/ntp.c vs. this driver.)
  60. */
  61. #include <linux/interrupt.h>
  62. #include <linux/module.h>
  63. #include <linux/kernel.h>
  64. #include <linux/types.h>
  65. #include <linux/miscdevice.h>
  66. #include <linux/ioport.h>
  67. #include <linux/fcntl.h>
  68. #include <linux/mc146818rtc.h>
  69. #include <linux/init.h>
  70. #include <linux/poll.h>
  71. #include <linux/proc_fs.h>
  72. #include <linux/seq_file.h>
  73. #include <linux/spinlock.h>
  74. #include <linux/sched/signal.h>
  75. #include <linux/sysctl.h>
  76. #include <linux/wait.h>
  77. #include <linux/bcd.h>
  78. #include <linux/delay.h>
  79. #include <linux/uaccess.h>
  80. #include <linux/ratelimit.h>
  81. #include <asm/current.h>
  82. #ifdef CONFIG_X86
  83. #include <asm/hpet.h>
  84. #endif
  85. #ifdef CONFIG_SPARC32
  86. #include <linux/of.h>
  87. #include <linux/of_device.h>
  88. #include <asm/io.h>
  89. static unsigned long rtc_port;
  90. static int rtc_irq;
  91. #endif
  92. #ifdef CONFIG_HPET_EMULATE_RTC
  93. #undef RTC_IRQ
  94. #endif
  95. #ifdef RTC_IRQ
  96. static int rtc_has_irq = 1;
  97. #endif
  98. #ifndef CONFIG_HPET_EMULATE_RTC
  99. #define is_hpet_enabled() 0
  100. #define hpet_set_alarm_time(hrs, min, sec) 0
  101. #define hpet_set_periodic_freq(arg) 0
  102. #define hpet_mask_rtc_irq_bit(arg) 0
  103. #define hpet_set_rtc_irq_bit(arg) 0
  104. #define hpet_rtc_timer_init() do { } while (0)
  105. #define hpet_rtc_dropped_irq() 0
  106. #define hpet_register_irq_handler(h) ({ 0; })
  107. #define hpet_unregister_irq_handler(h) ({ 0; })
  108. #ifdef RTC_IRQ
  109. static irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
  110. {
  111. return 0;
  112. }
  113. #endif
  114. #endif
  115. /*
  116. * We sponge a minor off of the misc major. No need slurping
  117. * up another valuable major dev number for this. If you add
  118. * an ioctl, make sure you don't conflict with SPARC's RTC
  119. * ioctls.
  120. */
  121. static struct fasync_struct *rtc_async_queue;
  122. static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
  123. #ifdef RTC_IRQ
  124. static void rtc_dropped_irq(struct timer_list *unused);
  125. static DEFINE_TIMER(rtc_irq_timer, rtc_dropped_irq);
  126. #endif
  127. static ssize_t rtc_read(struct file *file, char __user *buf,
  128. size_t count, loff_t *ppos);
  129. static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  130. static void rtc_get_rtc_time(struct rtc_time *rtc_tm);
  131. #ifdef RTC_IRQ
  132. static __poll_t rtc_poll(struct file *file, poll_table *wait);
  133. #endif
  134. static void get_rtc_alm_time(struct rtc_time *alm_tm);
  135. #ifdef RTC_IRQ
  136. static void set_rtc_irq_bit_locked(unsigned char bit);
  137. static void mask_rtc_irq_bit_locked(unsigned char bit);
  138. static inline void set_rtc_irq_bit(unsigned char bit)
  139. {
  140. spin_lock_irq(&rtc_lock);
  141. set_rtc_irq_bit_locked(bit);
  142. spin_unlock_irq(&rtc_lock);
  143. }
  144. static void mask_rtc_irq_bit(unsigned char bit)
  145. {
  146. spin_lock_irq(&rtc_lock);
  147. mask_rtc_irq_bit_locked(bit);
  148. spin_unlock_irq(&rtc_lock);
  149. }
  150. #endif
  151. #ifdef CONFIG_PROC_FS
  152. static int rtc_proc_show(struct seq_file *seq, void *v);
  153. #endif
  154. /*
  155. * Bits in rtc_status. (6 bits of room for future expansion)
  156. */
  157. #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
  158. #define RTC_TIMER_ON 0x02 /* missed irq timer active */
  159. /*
  160. * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
  161. * protected by the spin lock rtc_lock. However, ioctl can still disable the
  162. * timer in rtc_status and then with del_timer after the interrupt has read
  163. * rtc_status but before mod_timer is called, which would then reenable the
  164. * timer (but you would need to have an awful timing before you'd trip on it)
  165. */
  166. static unsigned long rtc_status; /* bitmapped status byte. */
  167. static unsigned long rtc_freq; /* Current periodic IRQ rate */
  168. static unsigned long rtc_irq_data; /* our output to the world */
  169. static unsigned long rtc_max_user_freq = 64; /* > this, need CAP_SYS_RESOURCE */
  170. /*
  171. * If this driver ever becomes modularised, it will be really nice
  172. * to make the epoch retain its value across module reload...
  173. */
  174. static unsigned long epoch = 1900; /* year corresponding to 0x00 */
  175. static const unsigned char days_in_mo[] =
  176. {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  177. /*
  178. * Returns true if a clock update is in progress
  179. */
  180. static inline unsigned char rtc_is_updating(void)
  181. {
  182. unsigned long flags;
  183. unsigned char uip;
  184. spin_lock_irqsave(&rtc_lock, flags);
  185. uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
  186. spin_unlock_irqrestore(&rtc_lock, flags);
  187. return uip;
  188. }
  189. #ifdef RTC_IRQ
  190. /*
  191. * A very tiny interrupt handler. It runs with interrupts disabled,
  192. * but there is possibility of conflicting with the set_rtc_mmss()
  193. * call (the rtc irq and the timer irq can easily run at the same
  194. * time in two different CPUs). So we need to serialize
  195. * accesses to the chip with the rtc_lock spinlock that each
  196. * architecture should implement in the timer code.
  197. * (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
  198. */
  199. static irqreturn_t rtc_interrupt(int irq, void *dev_id)
  200. {
  201. /*
  202. * Can be an alarm interrupt, update complete interrupt,
  203. * or a periodic interrupt. We store the status in the
  204. * low byte and the number of interrupts received since
  205. * the last read in the remainder of rtc_irq_data.
  206. */
  207. spin_lock(&rtc_lock);
  208. rtc_irq_data += 0x100;
  209. rtc_irq_data &= ~0xff;
  210. if (is_hpet_enabled()) {
  211. /*
  212. * In this case it is HPET RTC interrupt handler
  213. * calling us, with the interrupt information
  214. * passed as arg1, instead of irq.
  215. */
  216. rtc_irq_data |= (unsigned long)irq & 0xF0;
  217. } else {
  218. rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);
  219. }
  220. if (rtc_status & RTC_TIMER_ON)
  221. mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
  222. spin_unlock(&rtc_lock);
  223. wake_up_interruptible(&rtc_wait);
  224. kill_fasync(&rtc_async_queue, SIGIO, POLL_IN);
  225. return IRQ_HANDLED;
  226. }
  227. #endif
  228. /*
  229. * sysctl-tuning infrastructure.
  230. */
  231. static struct ctl_table rtc_table[] = {
  232. {
  233. .procname = "max-user-freq",
  234. .data = &rtc_max_user_freq,
  235. .maxlen = sizeof(int),
  236. .mode = 0644,
  237. .proc_handler = proc_dointvec,
  238. },
  239. { }
  240. };
  241. static struct ctl_table rtc_root[] = {
  242. {
  243. .procname = "rtc",
  244. .mode = 0555,
  245. .child = rtc_table,
  246. },
  247. { }
  248. };
  249. static struct ctl_table dev_root[] = {
  250. {
  251. .procname = "dev",
  252. .mode = 0555,
  253. .child = rtc_root,
  254. },
  255. { }
  256. };
  257. static struct ctl_table_header *sysctl_header;
  258. static int __init init_sysctl(void)
  259. {
  260. sysctl_header = register_sysctl_table(dev_root);
  261. return 0;
  262. }
  263. static void __exit cleanup_sysctl(void)
  264. {
  265. unregister_sysctl_table(sysctl_header);
  266. }
  267. /*
  268. * Now all the various file operations that we export.
  269. */
  270. static ssize_t rtc_read(struct file *file, char __user *buf,
  271. size_t count, loff_t *ppos)
  272. {
  273. #ifndef RTC_IRQ
  274. return -EIO;
  275. #else
  276. DECLARE_WAITQUEUE(wait, current);
  277. unsigned long data;
  278. ssize_t retval;
  279. if (rtc_has_irq == 0)
  280. return -EIO;
  281. /*
  282. * Historically this function used to assume that sizeof(unsigned long)
  283. * is the same in userspace and kernelspace. This lead to problems
  284. * for configurations with multiple ABIs such a the MIPS o32 and 64
  285. * ABIs supported on the same kernel. So now we support read of both
  286. * 4 and 8 bytes and assume that's the sizeof(unsigned long) in the
  287. * userspace ABI.
  288. */
  289. if (count != sizeof(unsigned int) && count != sizeof(unsigned long))
  290. return -EINVAL;
  291. add_wait_queue(&rtc_wait, &wait);
  292. do {
  293. /* First make it right. Then make it fast. Putting this whole
  294. * block within the parentheses of a while would be too
  295. * confusing. And no, xchg() is not the answer. */
  296. __set_current_state(TASK_INTERRUPTIBLE);
  297. spin_lock_irq(&rtc_lock);
  298. data = rtc_irq_data;
  299. rtc_irq_data = 0;
  300. spin_unlock_irq(&rtc_lock);
  301. if (data != 0)
  302. break;
  303. if (file->f_flags & O_NONBLOCK) {
  304. retval = -EAGAIN;
  305. goto out;
  306. }
  307. if (signal_pending(current)) {
  308. retval = -ERESTARTSYS;
  309. goto out;
  310. }
  311. schedule();
  312. } while (1);
  313. if (count == sizeof(unsigned int)) {
  314. retval = put_user(data,
  315. (unsigned int __user *)buf) ?: sizeof(int);
  316. } else {
  317. retval = put_user(data,
  318. (unsigned long __user *)buf) ?: sizeof(long);
  319. }
  320. if (!retval)
  321. retval = count;
  322. out:
  323. __set_current_state(TASK_RUNNING);
  324. remove_wait_queue(&rtc_wait, &wait);
  325. return retval;
  326. #endif
  327. }
  328. static int rtc_do_ioctl(unsigned int cmd, unsigned long arg, int kernel)
  329. {
  330. struct rtc_time wtime;
  331. #ifdef RTC_IRQ
  332. if (rtc_has_irq == 0) {
  333. switch (cmd) {
  334. case RTC_AIE_OFF:
  335. case RTC_AIE_ON:
  336. case RTC_PIE_OFF:
  337. case RTC_PIE_ON:
  338. case RTC_UIE_OFF:
  339. case RTC_UIE_ON:
  340. case RTC_IRQP_READ:
  341. case RTC_IRQP_SET:
  342. return -EINVAL;
  343. }
  344. }
  345. #endif
  346. switch (cmd) {
  347. #ifdef RTC_IRQ
  348. case RTC_AIE_OFF: /* Mask alarm int. enab. bit */
  349. {
  350. mask_rtc_irq_bit(RTC_AIE);
  351. return 0;
  352. }
  353. case RTC_AIE_ON: /* Allow alarm interrupts. */
  354. {
  355. set_rtc_irq_bit(RTC_AIE);
  356. return 0;
  357. }
  358. case RTC_PIE_OFF: /* Mask periodic int. enab. bit */
  359. {
  360. /* can be called from isr via rtc_control() */
  361. unsigned long flags;
  362. spin_lock_irqsave(&rtc_lock, flags);
  363. mask_rtc_irq_bit_locked(RTC_PIE);
  364. if (rtc_status & RTC_TIMER_ON) {
  365. rtc_status &= ~RTC_TIMER_ON;
  366. del_timer(&rtc_irq_timer);
  367. }
  368. spin_unlock_irqrestore(&rtc_lock, flags);
  369. return 0;
  370. }
  371. case RTC_PIE_ON: /* Allow periodic ints */
  372. {
  373. /* can be called from isr via rtc_control() */
  374. unsigned long flags;
  375. /*
  376. * We don't really want Joe User enabling more
  377. * than 64Hz of interrupts on a multi-user machine.
  378. */
  379. if (!kernel && (rtc_freq > rtc_max_user_freq) &&
  380. (!capable(CAP_SYS_RESOURCE)))
  381. return -EACCES;
  382. spin_lock_irqsave(&rtc_lock, flags);
  383. if (!(rtc_status & RTC_TIMER_ON)) {
  384. mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq +
  385. 2*HZ/100);
  386. rtc_status |= RTC_TIMER_ON;
  387. }
  388. set_rtc_irq_bit_locked(RTC_PIE);
  389. spin_unlock_irqrestore(&rtc_lock, flags);
  390. return 0;
  391. }
  392. case RTC_UIE_OFF: /* Mask ints from RTC updates. */
  393. {
  394. mask_rtc_irq_bit(RTC_UIE);
  395. return 0;
  396. }
  397. case RTC_UIE_ON: /* Allow ints for RTC updates. */
  398. {
  399. set_rtc_irq_bit(RTC_UIE);
  400. return 0;
  401. }
  402. #endif
  403. case RTC_ALM_READ: /* Read the present alarm time */
  404. {
  405. /*
  406. * This returns a struct rtc_time. Reading >= 0xc0
  407. * means "don't care" or "match all". Only the tm_hour,
  408. * tm_min, and tm_sec values are filled in.
  409. */
  410. memset(&wtime, 0, sizeof(struct rtc_time));
  411. get_rtc_alm_time(&wtime);
  412. break;
  413. }
  414. case RTC_ALM_SET: /* Store a time into the alarm */
  415. {
  416. /*
  417. * This expects a struct rtc_time. Writing 0xff means
  418. * "don't care" or "match all". Only the tm_hour,
  419. * tm_min and tm_sec are used.
  420. */
  421. unsigned char hrs, min, sec;
  422. struct rtc_time alm_tm;
  423. if (copy_from_user(&alm_tm, (struct rtc_time __user *)arg,
  424. sizeof(struct rtc_time)))
  425. return -EFAULT;
  426. hrs = alm_tm.tm_hour;
  427. min = alm_tm.tm_min;
  428. sec = alm_tm.tm_sec;
  429. spin_lock_irq(&rtc_lock);
  430. if (hpet_set_alarm_time(hrs, min, sec)) {
  431. /*
  432. * Fallthru and set alarm time in CMOS too,
  433. * so that we will get proper value in RTC_ALM_READ
  434. */
  435. }
  436. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) ||
  437. RTC_ALWAYS_BCD) {
  438. if (sec < 60)
  439. sec = bin2bcd(sec);
  440. else
  441. sec = 0xff;
  442. if (min < 60)
  443. min = bin2bcd(min);
  444. else
  445. min = 0xff;
  446. if (hrs < 24)
  447. hrs = bin2bcd(hrs);
  448. else
  449. hrs = 0xff;
  450. }
  451. CMOS_WRITE(hrs, RTC_HOURS_ALARM);
  452. CMOS_WRITE(min, RTC_MINUTES_ALARM);
  453. CMOS_WRITE(sec, RTC_SECONDS_ALARM);
  454. spin_unlock_irq(&rtc_lock);
  455. return 0;
  456. }
  457. case RTC_RD_TIME: /* Read the time/date from RTC */
  458. {
  459. memset(&wtime, 0, sizeof(struct rtc_time));
  460. rtc_get_rtc_time(&wtime);
  461. break;
  462. }
  463. case RTC_SET_TIME: /* Set the RTC */
  464. {
  465. struct rtc_time rtc_tm;
  466. unsigned char mon, day, hrs, min, sec, leap_yr;
  467. unsigned char save_control, save_freq_select;
  468. unsigned int yrs;
  469. #ifdef CONFIG_MACH_DECSTATION
  470. unsigned int real_yrs;
  471. #endif
  472. if (!capable(CAP_SYS_TIME))
  473. return -EACCES;
  474. if (copy_from_user(&rtc_tm, (struct rtc_time __user *)arg,
  475. sizeof(struct rtc_time)))
  476. return -EFAULT;
  477. yrs = rtc_tm.tm_year + 1900;
  478. mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
  479. day = rtc_tm.tm_mday;
  480. hrs = rtc_tm.tm_hour;
  481. min = rtc_tm.tm_min;
  482. sec = rtc_tm.tm_sec;
  483. if (yrs < 1970)
  484. return -EINVAL;
  485. leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
  486. if ((mon > 12) || (day == 0))
  487. return -EINVAL;
  488. if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
  489. return -EINVAL;
  490. if ((hrs >= 24) || (min >= 60) || (sec >= 60))
  491. return -EINVAL;
  492. yrs -= epoch;
  493. if (yrs > 255) /* They are unsigned */
  494. return -EINVAL;
  495. spin_lock_irq(&rtc_lock);
  496. #ifdef CONFIG_MACH_DECSTATION
  497. real_yrs = yrs;
  498. yrs = 72;
  499. /*
  500. * We want to keep the year set to 73 until March
  501. * for non-leap years, so that Feb, 29th is handled
  502. * correctly.
  503. */
  504. if (!leap_yr && mon < 3) {
  505. real_yrs--;
  506. yrs = 73;
  507. }
  508. #endif
  509. /* These limits and adjustments are independent of
  510. * whether the chip is in binary mode or not.
  511. */
  512. if (yrs > 169) {
  513. spin_unlock_irq(&rtc_lock);
  514. return -EINVAL;
  515. }
  516. if (yrs >= 100)
  517. yrs -= 100;
  518. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
  519. || RTC_ALWAYS_BCD) {
  520. sec = bin2bcd(sec);
  521. min = bin2bcd(min);
  522. hrs = bin2bcd(hrs);
  523. day = bin2bcd(day);
  524. mon = bin2bcd(mon);
  525. yrs = bin2bcd(yrs);
  526. }
  527. save_control = CMOS_READ(RTC_CONTROL);
  528. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  529. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  530. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  531. #ifdef CONFIG_MACH_DECSTATION
  532. CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
  533. #endif
  534. CMOS_WRITE(yrs, RTC_YEAR);
  535. CMOS_WRITE(mon, RTC_MONTH);
  536. CMOS_WRITE(day, RTC_DAY_OF_MONTH);
  537. CMOS_WRITE(hrs, RTC_HOURS);
  538. CMOS_WRITE(min, RTC_MINUTES);
  539. CMOS_WRITE(sec, RTC_SECONDS);
  540. CMOS_WRITE(save_control, RTC_CONTROL);
  541. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  542. spin_unlock_irq(&rtc_lock);
  543. return 0;
  544. }
  545. #ifdef RTC_IRQ
  546. case RTC_IRQP_READ: /* Read the periodic IRQ rate. */
  547. {
  548. return put_user(rtc_freq, (unsigned long __user *)arg);
  549. }
  550. case RTC_IRQP_SET: /* Set periodic IRQ rate. */
  551. {
  552. int tmp = 0;
  553. unsigned char val;
  554. /* can be called from isr via rtc_control() */
  555. unsigned long flags;
  556. /*
  557. * The max we can do is 8192Hz.
  558. */
  559. if ((arg < 2) || (arg > 8192))
  560. return -EINVAL;
  561. /*
  562. * We don't really want Joe User generating more
  563. * than 64Hz of interrupts on a multi-user machine.
  564. */
  565. if (!kernel && (arg > rtc_max_user_freq) &&
  566. !capable(CAP_SYS_RESOURCE))
  567. return -EACCES;
  568. while (arg > (1<<tmp))
  569. tmp++;
  570. /*
  571. * Check that the input was really a power of 2.
  572. */
  573. if (arg != (1<<tmp))
  574. return -EINVAL;
  575. rtc_freq = arg;
  576. spin_lock_irqsave(&rtc_lock, flags);
  577. if (hpet_set_periodic_freq(arg)) {
  578. spin_unlock_irqrestore(&rtc_lock, flags);
  579. return 0;
  580. }
  581. val = CMOS_READ(RTC_FREQ_SELECT) & 0xf0;
  582. val |= (16 - tmp);
  583. CMOS_WRITE(val, RTC_FREQ_SELECT);
  584. spin_unlock_irqrestore(&rtc_lock, flags);
  585. return 0;
  586. }
  587. #endif
  588. case RTC_EPOCH_READ: /* Read the epoch. */
  589. {
  590. return put_user(epoch, (unsigned long __user *)arg);
  591. }
  592. case RTC_EPOCH_SET: /* Set the epoch. */
  593. {
  594. /*
  595. * There were no RTC clocks before 1900.
  596. */
  597. if (arg < 1900)
  598. return -EINVAL;
  599. if (!capable(CAP_SYS_TIME))
  600. return -EACCES;
  601. epoch = arg;
  602. return 0;
  603. }
  604. default:
  605. return -ENOTTY;
  606. }
  607. return copy_to_user((void __user *)arg,
  608. &wtime, sizeof wtime) ? -EFAULT : 0;
  609. }
  610. static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  611. {
  612. long ret;
  613. ret = rtc_do_ioctl(cmd, arg, 0);
  614. return ret;
  615. }
  616. /*
  617. * We enforce only one user at a time here with the open/close.
  618. * Also clear the previous interrupt data on an open, and clean
  619. * up things on a close.
  620. */
  621. static int rtc_open(struct inode *inode, struct file *file)
  622. {
  623. spin_lock_irq(&rtc_lock);
  624. if (rtc_status & RTC_IS_OPEN)
  625. goto out_busy;
  626. rtc_status |= RTC_IS_OPEN;
  627. rtc_irq_data = 0;
  628. spin_unlock_irq(&rtc_lock);
  629. return 0;
  630. out_busy:
  631. spin_unlock_irq(&rtc_lock);
  632. return -EBUSY;
  633. }
  634. static int rtc_fasync(int fd, struct file *filp, int on)
  635. {
  636. return fasync_helper(fd, filp, on, &rtc_async_queue);
  637. }
  638. static int rtc_release(struct inode *inode, struct file *file)
  639. {
  640. #ifdef RTC_IRQ
  641. unsigned char tmp;
  642. if (rtc_has_irq == 0)
  643. goto no_irq;
  644. /*
  645. * Turn off all interrupts once the device is no longer
  646. * in use, and clear the data.
  647. */
  648. spin_lock_irq(&rtc_lock);
  649. if (!hpet_mask_rtc_irq_bit(RTC_PIE | RTC_AIE | RTC_UIE)) {
  650. tmp = CMOS_READ(RTC_CONTROL);
  651. tmp &= ~RTC_PIE;
  652. tmp &= ~RTC_AIE;
  653. tmp &= ~RTC_UIE;
  654. CMOS_WRITE(tmp, RTC_CONTROL);
  655. CMOS_READ(RTC_INTR_FLAGS);
  656. }
  657. if (rtc_status & RTC_TIMER_ON) {
  658. rtc_status &= ~RTC_TIMER_ON;
  659. del_timer(&rtc_irq_timer);
  660. }
  661. spin_unlock_irq(&rtc_lock);
  662. no_irq:
  663. #endif
  664. spin_lock_irq(&rtc_lock);
  665. rtc_irq_data = 0;
  666. rtc_status &= ~RTC_IS_OPEN;
  667. spin_unlock_irq(&rtc_lock);
  668. return 0;
  669. }
  670. #ifdef RTC_IRQ
  671. static __poll_t rtc_poll(struct file *file, poll_table *wait)
  672. {
  673. unsigned long l;
  674. if (rtc_has_irq == 0)
  675. return 0;
  676. poll_wait(file, &rtc_wait, wait);
  677. spin_lock_irq(&rtc_lock);
  678. l = rtc_irq_data;
  679. spin_unlock_irq(&rtc_lock);
  680. if (l != 0)
  681. return EPOLLIN | EPOLLRDNORM;
  682. return 0;
  683. }
  684. #endif
  685. /*
  686. * The various file operations we support.
  687. */
  688. static const struct file_operations rtc_fops = {
  689. .owner = THIS_MODULE,
  690. .llseek = no_llseek,
  691. .read = rtc_read,
  692. #ifdef RTC_IRQ
  693. .poll = rtc_poll,
  694. #endif
  695. .unlocked_ioctl = rtc_ioctl,
  696. .open = rtc_open,
  697. .release = rtc_release,
  698. .fasync = rtc_fasync,
  699. };
  700. static struct miscdevice rtc_dev = {
  701. .minor = RTC_MINOR,
  702. .name = "rtc",
  703. .fops = &rtc_fops,
  704. };
  705. static resource_size_t rtc_size;
  706. static struct resource * __init rtc_request_region(resource_size_t size)
  707. {
  708. struct resource *r;
  709. if (RTC_IOMAPPED)
  710. r = request_region(RTC_PORT(0), size, "rtc");
  711. else
  712. r = request_mem_region(RTC_PORT(0), size, "rtc");
  713. if (r)
  714. rtc_size = size;
  715. return r;
  716. }
  717. static void rtc_release_region(void)
  718. {
  719. if (RTC_IOMAPPED)
  720. release_region(RTC_PORT(0), rtc_size);
  721. else
  722. release_mem_region(RTC_PORT(0), rtc_size);
  723. }
  724. static int __init rtc_init(void)
  725. {
  726. #ifdef CONFIG_PROC_FS
  727. struct proc_dir_entry *ent;
  728. #endif
  729. #if defined(__alpha__) || defined(__mips__)
  730. unsigned int year, ctrl;
  731. char *guess = NULL;
  732. #endif
  733. #ifdef CONFIG_SPARC32
  734. struct device_node *ebus_dp;
  735. struct platform_device *op;
  736. #else
  737. void *r;
  738. #ifdef RTC_IRQ
  739. irq_handler_t rtc_int_handler_ptr;
  740. #endif
  741. #endif
  742. #ifdef CONFIG_SPARC32
  743. for_each_node_by_name(ebus_dp, "ebus") {
  744. struct device_node *dp;
  745. for (dp = ebus_dp; dp; dp = dp->sibling) {
  746. if (!strcmp(dp->name, "rtc")) {
  747. op = of_find_device_by_node(dp);
  748. if (op) {
  749. rtc_port = op->resource[0].start;
  750. rtc_irq = op->irqs[0];
  751. goto found;
  752. }
  753. }
  754. }
  755. }
  756. rtc_has_irq = 0;
  757. printk(KERN_ERR "rtc_init: no PC rtc found\n");
  758. return -EIO;
  759. found:
  760. if (!rtc_irq) {
  761. rtc_has_irq = 0;
  762. goto no_irq;
  763. }
  764. /*
  765. * XXX Interrupt pin #7 in Espresso is shared between RTC and
  766. * PCI Slot 2 INTA# (and some INTx# in Slot 1).
  767. */
  768. if (request_irq(rtc_irq, rtc_interrupt, IRQF_SHARED, "rtc",
  769. (void *)&rtc_port)) {
  770. rtc_has_irq = 0;
  771. printk(KERN_ERR "rtc: cannot register IRQ %d\n", rtc_irq);
  772. return -EIO;
  773. }
  774. no_irq:
  775. #else
  776. r = rtc_request_region(RTC_IO_EXTENT);
  777. /*
  778. * If we've already requested a smaller range (for example, because
  779. * PNPBIOS or ACPI told us how the device is configured), the request
  780. * above might fail because it's too big.
  781. *
  782. * If so, request just the range we actually use.
  783. */
  784. if (!r)
  785. r = rtc_request_region(RTC_IO_EXTENT_USED);
  786. if (!r) {
  787. #ifdef RTC_IRQ
  788. rtc_has_irq = 0;
  789. #endif
  790. printk(KERN_ERR "rtc: I/O resource %lx is not free.\n",
  791. (long)(RTC_PORT(0)));
  792. return -EIO;
  793. }
  794. #ifdef RTC_IRQ
  795. if (is_hpet_enabled()) {
  796. int err;
  797. rtc_int_handler_ptr = hpet_rtc_interrupt;
  798. err = hpet_register_irq_handler(rtc_interrupt);
  799. if (err != 0) {
  800. printk(KERN_WARNING "hpet_register_irq_handler failed "
  801. "in rtc_init().");
  802. return err;
  803. }
  804. } else {
  805. rtc_int_handler_ptr = rtc_interrupt;
  806. }
  807. if (request_irq(RTC_IRQ, rtc_int_handler_ptr, 0, "rtc", NULL)) {
  808. /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
  809. rtc_has_irq = 0;
  810. printk(KERN_ERR "rtc: IRQ %d is not free.\n", RTC_IRQ);
  811. rtc_release_region();
  812. return -EIO;
  813. }
  814. hpet_rtc_timer_init();
  815. #endif
  816. #endif /* CONFIG_SPARC32 vs. others */
  817. if (misc_register(&rtc_dev)) {
  818. #ifdef RTC_IRQ
  819. free_irq(RTC_IRQ, NULL);
  820. hpet_unregister_irq_handler(rtc_interrupt);
  821. rtc_has_irq = 0;
  822. #endif
  823. rtc_release_region();
  824. return -ENODEV;
  825. }
  826. #ifdef CONFIG_PROC_FS
  827. ent = proc_create_single("driver/rtc", 0, NULL, rtc_proc_show);
  828. if (!ent)
  829. printk(KERN_WARNING "rtc: Failed to register with procfs.\n");
  830. #endif
  831. #if defined(__alpha__) || defined(__mips__)
  832. rtc_freq = HZ;
  833. /* Each operating system on an Alpha uses its own epoch.
  834. Let's try to guess which one we are using now. */
  835. if (rtc_is_updating() != 0)
  836. msleep(20);
  837. spin_lock_irq(&rtc_lock);
  838. year = CMOS_READ(RTC_YEAR);
  839. ctrl = CMOS_READ(RTC_CONTROL);
  840. spin_unlock_irq(&rtc_lock);
  841. if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  842. year = bcd2bin(year); /* This should never happen... */
  843. if (year < 20) {
  844. epoch = 2000;
  845. guess = "SRM (post-2000)";
  846. } else if (year >= 20 && year < 48) {
  847. epoch = 1980;
  848. guess = "ARC console";
  849. } else if (year >= 48 && year < 72) {
  850. epoch = 1952;
  851. guess = "Digital UNIX";
  852. #if defined(__mips__)
  853. } else if (year >= 72 && year < 74) {
  854. epoch = 2000;
  855. guess = "Digital DECstation";
  856. #else
  857. } else if (year >= 70) {
  858. epoch = 1900;
  859. guess = "Standard PC (1900)";
  860. #endif
  861. }
  862. if (guess)
  863. printk(KERN_INFO "rtc: %s epoch (%lu) detected\n",
  864. guess, epoch);
  865. #endif
  866. #ifdef RTC_IRQ
  867. if (rtc_has_irq == 0)
  868. goto no_irq2;
  869. spin_lock_irq(&rtc_lock);
  870. rtc_freq = 1024;
  871. if (!hpet_set_periodic_freq(rtc_freq)) {
  872. /*
  873. * Initialize periodic frequency to CMOS reset default,
  874. * which is 1024Hz
  875. */
  876. CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT) & 0xF0) | 0x06),
  877. RTC_FREQ_SELECT);
  878. }
  879. spin_unlock_irq(&rtc_lock);
  880. no_irq2:
  881. #endif
  882. (void) init_sysctl();
  883. printk(KERN_INFO "Real Time Clock Driver v" RTC_VERSION "\n");
  884. return 0;
  885. }
  886. static void __exit rtc_exit(void)
  887. {
  888. cleanup_sysctl();
  889. remove_proc_entry("driver/rtc", NULL);
  890. misc_deregister(&rtc_dev);
  891. #ifdef CONFIG_SPARC32
  892. if (rtc_has_irq)
  893. free_irq(rtc_irq, &rtc_port);
  894. #else
  895. rtc_release_region();
  896. #ifdef RTC_IRQ
  897. if (rtc_has_irq) {
  898. free_irq(RTC_IRQ, NULL);
  899. hpet_unregister_irq_handler(hpet_rtc_interrupt);
  900. }
  901. #endif
  902. #endif /* CONFIG_SPARC32 */
  903. }
  904. module_init(rtc_init);
  905. module_exit(rtc_exit);
  906. #ifdef RTC_IRQ
  907. /*
  908. * At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
  909. * (usually during an IDE disk interrupt, with IRQ unmasking off)
  910. * Since the interrupt handler doesn't get called, the IRQ status
  911. * byte doesn't get read, and the RTC stops generating interrupts.
  912. * A timer is set, and will call this function if/when that happens.
  913. * To get it out of this stalled state, we just read the status.
  914. * At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
  915. * (You *really* shouldn't be trying to use a non-realtime system
  916. * for something that requires a steady > 1KHz signal anyways.)
  917. */
  918. static void rtc_dropped_irq(struct timer_list *unused)
  919. {
  920. unsigned long freq;
  921. spin_lock_irq(&rtc_lock);
  922. if (hpet_rtc_dropped_irq()) {
  923. spin_unlock_irq(&rtc_lock);
  924. return;
  925. }
  926. /* Just in case someone disabled the timer from behind our back... */
  927. if (rtc_status & RTC_TIMER_ON)
  928. mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
  929. rtc_irq_data += ((rtc_freq/HZ)<<8);
  930. rtc_irq_data &= ~0xff;
  931. rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0); /* restart */
  932. freq = rtc_freq;
  933. spin_unlock_irq(&rtc_lock);
  934. printk_ratelimited(KERN_WARNING "rtc: lost some interrupts at %ldHz.\n",
  935. freq);
  936. /* Now we have new data */
  937. wake_up_interruptible(&rtc_wait);
  938. kill_fasync(&rtc_async_queue, SIGIO, POLL_IN);
  939. }
  940. #endif
  941. #ifdef CONFIG_PROC_FS
  942. /*
  943. * Info exported via "/proc/driver/rtc".
  944. */
  945. static int rtc_proc_show(struct seq_file *seq, void *v)
  946. {
  947. #define YN(bit) ((ctrl & bit) ? "yes" : "no")
  948. #define NY(bit) ((ctrl & bit) ? "no" : "yes")
  949. struct rtc_time tm;
  950. unsigned char batt, ctrl;
  951. unsigned long freq;
  952. spin_lock_irq(&rtc_lock);
  953. batt = CMOS_READ(RTC_VALID) & RTC_VRT;
  954. ctrl = CMOS_READ(RTC_CONTROL);
  955. freq = rtc_freq;
  956. spin_unlock_irq(&rtc_lock);
  957. rtc_get_rtc_time(&tm);
  958. /*
  959. * There is no way to tell if the luser has the RTC set for local
  960. * time or for Universal Standard Time (GMT). Probably local though.
  961. */
  962. seq_printf(seq,
  963. "rtc_time\t: %02d:%02d:%02d\n"
  964. "rtc_date\t: %04d-%02d-%02d\n"
  965. "rtc_epoch\t: %04lu\n",
  966. tm.tm_hour, tm.tm_min, tm.tm_sec,
  967. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
  968. get_rtc_alm_time(&tm);
  969. /*
  970. * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
  971. * match any value for that particular field. Values that are
  972. * greater than a valid time, but less than 0xc0 shouldn't appear.
  973. */
  974. seq_puts(seq, "alarm\t\t: ");
  975. if (tm.tm_hour <= 24)
  976. seq_printf(seq, "%02d:", tm.tm_hour);
  977. else
  978. seq_puts(seq, "**:");
  979. if (tm.tm_min <= 59)
  980. seq_printf(seq, "%02d:", tm.tm_min);
  981. else
  982. seq_puts(seq, "**:");
  983. if (tm.tm_sec <= 59)
  984. seq_printf(seq, "%02d\n", tm.tm_sec);
  985. else
  986. seq_puts(seq, "**\n");
  987. seq_printf(seq,
  988. "DST_enable\t: %s\n"
  989. "BCD\t\t: %s\n"
  990. "24hr\t\t: %s\n"
  991. "square_wave\t: %s\n"
  992. "alarm_IRQ\t: %s\n"
  993. "update_IRQ\t: %s\n"
  994. "periodic_IRQ\t: %s\n"
  995. "periodic_freq\t: %ld\n"
  996. "batt_status\t: %s\n",
  997. YN(RTC_DST_EN),
  998. NY(RTC_DM_BINARY),
  999. YN(RTC_24H),
  1000. YN(RTC_SQWE),
  1001. YN(RTC_AIE),
  1002. YN(RTC_UIE),
  1003. YN(RTC_PIE),
  1004. freq,
  1005. batt ? "okay" : "dead");
  1006. return 0;
  1007. #undef YN
  1008. #undef NY
  1009. }
  1010. #endif
  1011. static void rtc_get_rtc_time(struct rtc_time *rtc_tm)
  1012. {
  1013. unsigned long uip_watchdog = jiffies, flags;
  1014. unsigned char ctrl;
  1015. #ifdef CONFIG_MACH_DECSTATION
  1016. unsigned int real_year;
  1017. #endif
  1018. /*
  1019. * read RTC once any update in progress is done. The update
  1020. * can take just over 2ms. We wait 20ms. There is no need to
  1021. * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
  1022. * If you need to know *exactly* when a second has started, enable
  1023. * periodic update complete interrupts, (via ioctl) and then
  1024. * immediately read /dev/rtc which will block until you get the IRQ.
  1025. * Once the read clears, read the RTC time (again via ioctl). Easy.
  1026. */
  1027. while (rtc_is_updating() != 0 &&
  1028. time_before(jiffies, uip_watchdog + 2*HZ/100))
  1029. cpu_relax();
  1030. /*
  1031. * Only the values that we read from the RTC are set. We leave
  1032. * tm_wday, tm_yday and tm_isdst untouched. Note that while the
  1033. * RTC has RTC_DAY_OF_WEEK, we should usually ignore it, as it is
  1034. * only updated by the RTC when initially set to a non-zero value.
  1035. */
  1036. spin_lock_irqsave(&rtc_lock, flags);
  1037. rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
  1038. rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
  1039. rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
  1040. rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
  1041. rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
  1042. rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
  1043. /* Only set from 2.6.16 onwards */
  1044. rtc_tm->tm_wday = CMOS_READ(RTC_DAY_OF_WEEK);
  1045. #ifdef CONFIG_MACH_DECSTATION
  1046. real_year = CMOS_READ(RTC_DEC_YEAR);
  1047. #endif
  1048. ctrl = CMOS_READ(RTC_CONTROL);
  1049. spin_unlock_irqrestore(&rtc_lock, flags);
  1050. if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  1051. rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
  1052. rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
  1053. rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
  1054. rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
  1055. rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
  1056. rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
  1057. rtc_tm->tm_wday = bcd2bin(rtc_tm->tm_wday);
  1058. }
  1059. #ifdef CONFIG_MACH_DECSTATION
  1060. rtc_tm->tm_year += real_year - 72;
  1061. #endif
  1062. /*
  1063. * Account for differences between how the RTC uses the values
  1064. * and how they are defined in a struct rtc_time;
  1065. */
  1066. rtc_tm->tm_year += epoch - 1900;
  1067. if (rtc_tm->tm_year <= 69)
  1068. rtc_tm->tm_year += 100;
  1069. rtc_tm->tm_mon--;
  1070. }
  1071. static void get_rtc_alm_time(struct rtc_time *alm_tm)
  1072. {
  1073. unsigned char ctrl;
  1074. /*
  1075. * Only the values that we read from the RTC are set. That
  1076. * means only tm_hour, tm_min, and tm_sec.
  1077. */
  1078. spin_lock_irq(&rtc_lock);
  1079. alm_tm->tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
  1080. alm_tm->tm_min = CMOS_READ(RTC_MINUTES_ALARM);
  1081. alm_tm->tm_hour = CMOS_READ(RTC_HOURS_ALARM);
  1082. ctrl = CMOS_READ(RTC_CONTROL);
  1083. spin_unlock_irq(&rtc_lock);
  1084. if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  1085. alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
  1086. alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
  1087. alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
  1088. }
  1089. }
  1090. #ifdef RTC_IRQ
  1091. /*
  1092. * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
  1093. * Rumour has it that if you frob the interrupt enable/disable
  1094. * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
  1095. * ensure you actually start getting interrupts. Probably for
  1096. * compatibility with older/broken chipset RTC implementations.
  1097. * We also clear out any old irq data after an ioctl() that
  1098. * meddles with the interrupt enable/disable bits.
  1099. */
  1100. static void mask_rtc_irq_bit_locked(unsigned char bit)
  1101. {
  1102. unsigned char val;
  1103. if (hpet_mask_rtc_irq_bit(bit))
  1104. return;
  1105. val = CMOS_READ(RTC_CONTROL);
  1106. val &= ~bit;
  1107. CMOS_WRITE(val, RTC_CONTROL);
  1108. CMOS_READ(RTC_INTR_FLAGS);
  1109. rtc_irq_data = 0;
  1110. }
  1111. static void set_rtc_irq_bit_locked(unsigned char bit)
  1112. {
  1113. unsigned char val;
  1114. if (hpet_set_rtc_irq_bit(bit))
  1115. return;
  1116. val = CMOS_READ(RTC_CONTROL);
  1117. val |= bit;
  1118. CMOS_WRITE(val, RTC_CONTROL);
  1119. CMOS_READ(RTC_INTR_FLAGS);
  1120. rtc_irq_data = 0;
  1121. }
  1122. #endif
  1123. MODULE_AUTHOR("Paul Gortmaker");
  1124. MODULE_LICENSE("GPL");
  1125. MODULE_ALIAS_MISCDEV(RTC_MINOR);