rtc.c 33 KB

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