rtc.c 33 KB

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