rtc.c 32 KB

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