rtc.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  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 irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id) {return 0;}
  107. #else
  108. extern irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id);
  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. #ifdef CONFIG_PROC_FS
  147. static int rtc_proc_open(struct inode *inode, struct file *file);
  148. #endif
  149. /*
  150. * Bits in rtc_status. (6 bits of room for future expansion)
  151. */
  152. #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
  153. #define RTC_TIMER_ON 0x02 /* missed irq timer active */
  154. /*
  155. * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
  156. * protected by the big kernel lock. However, ioctl can still disable the timer
  157. * in rtc_status and then with del_timer after the interrupt has read
  158. * rtc_status but before mod_timer is called, which would then reenable the
  159. * timer (but you would need to have an awful timing before you'd trip on it)
  160. */
  161. static unsigned long rtc_status = 0; /* bitmapped status byte. */
  162. static unsigned long rtc_freq = 0; /* Current periodic IRQ rate */
  163. static unsigned long rtc_irq_data = 0; /* our output to the world */
  164. static unsigned long rtc_max_user_freq = 64; /* > this, need CAP_SYS_RESOURCE */
  165. #ifdef RTC_IRQ
  166. /*
  167. * rtc_task_lock nests inside rtc_lock.
  168. */
  169. static DEFINE_SPINLOCK(rtc_task_lock);
  170. static rtc_task_t *rtc_callback = NULL;
  171. #endif
  172. /*
  173. * If this driver ever becomes modularised, it will be really nice
  174. * to make the epoch retain its value across module reload...
  175. */
  176. static unsigned long epoch = 1900; /* year corresponding to 0x00 */
  177. static const unsigned char days_in_mo[] =
  178. {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  179. /*
  180. * Returns true if a clock update is in progress
  181. */
  182. static inline unsigned char rtc_is_updating(void)
  183. {
  184. unsigned long flags;
  185. unsigned char uip;
  186. spin_lock_irqsave(&rtc_lock, flags);
  187. uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
  188. spin_unlock_irqrestore(&rtc_lock, flags);
  189. return uip;
  190. }
  191. #ifdef RTC_IRQ
  192. /*
  193. * A very tiny interrupt handler. It runs with IRQF_DISABLED set,
  194. * but there is possibility of conflicting with the set_rtc_mmss()
  195. * call (the rtc irq and the timer irq can easily run at the same
  196. * time in two different CPUs). So we need to serialize
  197. * accesses to the chip with the rtc_lock spinlock that each
  198. * architecture should implement in the timer code.
  199. * (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
  200. */
  201. irqreturn_t rtc_interrupt(int irq, void *dev_id)
  202. {
  203. /*
  204. * Can be an alarm interrupt, update complete interrupt,
  205. * or a periodic interrupt. We store the status in the
  206. * low byte and the number of interrupts received since
  207. * the last read in the remainder of rtc_irq_data.
  208. */
  209. spin_lock (&rtc_lock);
  210. rtc_irq_data += 0x100;
  211. rtc_irq_data &= ~0xff;
  212. if (is_hpet_enabled()) {
  213. /*
  214. * In this case it is HPET RTC interrupt handler
  215. * calling us, with the interrupt information
  216. * passed as arg1, instead of irq.
  217. */
  218. rtc_irq_data |= (unsigned long)irq & 0xF0;
  219. } else {
  220. rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);
  221. }
  222. if (rtc_status & RTC_TIMER_ON)
  223. mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
  224. spin_unlock (&rtc_lock);
  225. /* Now do the rest of the actions */
  226. spin_lock(&rtc_task_lock);
  227. if (rtc_callback)
  228. rtc_callback->func(rtc_callback->private_data);
  229. spin_unlock(&rtc_task_lock);
  230. wake_up_interruptible(&rtc_wait);
  231. kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
  232. return IRQ_HANDLED;
  233. }
  234. #endif
  235. /*
  236. * sysctl-tuning infrastructure.
  237. */
  238. static ctl_table rtc_table[] = {
  239. {
  240. .ctl_name = 1,
  241. .procname = "max-user-freq",
  242. .data = &rtc_max_user_freq,
  243. .maxlen = sizeof(int),
  244. .mode = 0644,
  245. .proc_handler = &proc_dointvec,
  246. },
  247. { .ctl_name = 0 }
  248. };
  249. static ctl_table rtc_root[] = {
  250. {
  251. .ctl_name = 1,
  252. .procname = "rtc",
  253. .maxlen = 0,
  254. .mode = 0555,
  255. .child = rtc_table,
  256. },
  257. { .ctl_name = 0 }
  258. };
  259. static ctl_table dev_root[] = {
  260. {
  261. .ctl_name = CTL_DEV,
  262. .procname = "dev",
  263. .maxlen = 0,
  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, 0);
  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. 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. rtc_irq_timer.expires = jiffies + HZ/rtc_freq + 2*HZ/100;
  392. add_timer(&rtc_irq_timer);
  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 __sparc__
  808. struct linux_ebus *ebus;
  809. struct linux_ebus_device *edev;
  810. #ifdef __sparc_v9__
  811. struct sparc_isa_bridge *isa_br;
  812. struct sparc_isa_device *isa_dev;
  813. #endif
  814. #else
  815. void *r;
  816. #ifdef RTC_IRQ
  817. irq_handler_t rtc_int_handler_ptr;
  818. #endif
  819. #endif
  820. #ifdef __sparc__
  821. for_each_ebus(ebus) {
  822. for_each_ebusdev(edev, ebus) {
  823. if(strcmp(edev->prom_node->name, "rtc") == 0) {
  824. rtc_port = edev->resource[0].start;
  825. rtc_irq = edev->irqs[0];
  826. goto found;
  827. }
  828. }
  829. }
  830. #ifdef __sparc_v9__
  831. for_each_isa(isa_br) {
  832. for_each_isadev(isa_dev, isa_br) {
  833. if (strcmp(isa_dev->prom_node->name, "rtc") == 0) {
  834. rtc_port = isa_dev->resource.start;
  835. rtc_irq = isa_dev->irq;
  836. goto found;
  837. }
  838. }
  839. }
  840. #endif
  841. rtc_has_irq = 0;
  842. printk(KERN_ERR "rtc_init: no PC rtc found\n");
  843. return -EIO;
  844. found:
  845. if (rtc_irq == PCI_IRQ_NONE) {
  846. rtc_has_irq = 0;
  847. goto no_irq;
  848. }
  849. /*
  850. * XXX Interrupt pin #7 in Espresso is shared between RTC and
  851. * PCI Slot 2 INTA# (and some INTx# in Slot 1).
  852. */
  853. if (request_irq(rtc_irq, rtc_interrupt, IRQF_SHARED, "rtc", (void *)&rtc_port)) {
  854. rtc_has_irq = 0;
  855. printk(KERN_ERR "rtc: cannot register IRQ %d\n", rtc_irq);
  856. return -EIO;
  857. }
  858. no_irq:
  859. #else
  860. if (RTC_IOMAPPED)
  861. r = request_region(RTC_PORT(0), RTC_IO_EXTENT, "rtc");
  862. else
  863. r = request_mem_region(RTC_PORT(0), RTC_IO_EXTENT, "rtc");
  864. if (!r) {
  865. #ifdef RTC_IRQ
  866. rtc_has_irq = 0;
  867. #endif
  868. printk(KERN_ERR "rtc: I/O resource %lx is not free.\n",
  869. (long)(RTC_PORT(0)));
  870. return -EIO;
  871. }
  872. #ifdef RTC_IRQ
  873. if (is_hpet_enabled()) {
  874. rtc_int_handler_ptr = hpet_rtc_interrupt;
  875. } else {
  876. rtc_int_handler_ptr = rtc_interrupt;
  877. }
  878. if(request_irq(RTC_IRQ, rtc_int_handler_ptr, IRQF_DISABLED, "rtc", NULL)) {
  879. /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
  880. rtc_has_irq = 0;
  881. printk(KERN_ERR "rtc: IRQ %d is not free.\n", RTC_IRQ);
  882. if (RTC_IOMAPPED)
  883. release_region(RTC_PORT(0), RTC_IO_EXTENT);
  884. else
  885. release_mem_region(RTC_PORT(0), RTC_IO_EXTENT);
  886. return -EIO;
  887. }
  888. hpet_rtc_timer_init();
  889. #endif
  890. #endif /* __sparc__ vs. others */
  891. if (misc_register(&rtc_dev)) {
  892. #ifdef RTC_IRQ
  893. free_irq(RTC_IRQ, NULL);
  894. rtc_has_irq = 0;
  895. #endif
  896. release_region(RTC_PORT(0), RTC_IO_EXTENT);
  897. return -ENODEV;
  898. }
  899. #ifdef CONFIG_PROC_FS
  900. ent = create_proc_entry("driver/rtc", 0, NULL);
  901. if (ent)
  902. ent->proc_fops = &rtc_proc_fops;
  903. else
  904. printk(KERN_WARNING "rtc: Failed to register with procfs.\n");
  905. #endif
  906. #if defined(__alpha__) || defined(__mips__)
  907. rtc_freq = HZ;
  908. /* Each operating system on an Alpha uses its own epoch.
  909. Let's try to guess which one we are using now. */
  910. if (rtc_is_updating() != 0)
  911. msleep(20);
  912. spin_lock_irq(&rtc_lock);
  913. year = CMOS_READ(RTC_YEAR);
  914. ctrl = CMOS_READ(RTC_CONTROL);
  915. spin_unlock_irq(&rtc_lock);
  916. if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  917. BCD_TO_BIN(year); /* This should never happen... */
  918. if (year < 20) {
  919. epoch = 2000;
  920. guess = "SRM (post-2000)";
  921. } else if (year >= 20 && year < 48) {
  922. epoch = 1980;
  923. guess = "ARC console";
  924. } else if (year >= 48 && year < 72) {
  925. epoch = 1952;
  926. guess = "Digital UNIX";
  927. #if defined(__mips__)
  928. } else if (year >= 72 && year < 74) {
  929. epoch = 2000;
  930. guess = "Digital DECstation";
  931. #else
  932. } else if (year >= 70) {
  933. epoch = 1900;
  934. guess = "Standard PC (1900)";
  935. #endif
  936. }
  937. if (guess)
  938. printk(KERN_INFO "rtc: %s epoch (%lu) detected\n", guess, epoch);
  939. #endif
  940. #ifdef RTC_IRQ
  941. if (rtc_has_irq == 0)
  942. goto no_irq2;
  943. init_timer(&rtc_irq_timer);
  944. rtc_irq_timer.function = rtc_dropped_irq;
  945. spin_lock_irq(&rtc_lock);
  946. rtc_freq = 1024;
  947. if (!hpet_set_periodic_freq(rtc_freq)) {
  948. /* Initialize periodic freq. to CMOS reset default, which is 1024Hz */
  949. CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT) & 0xF0) | 0x06), RTC_FREQ_SELECT);
  950. }
  951. spin_unlock_irq(&rtc_lock);
  952. no_irq2:
  953. #endif
  954. (void) init_sysctl();
  955. printk(KERN_INFO "Real Time Clock Driver v" RTC_VERSION "\n");
  956. return 0;
  957. }
  958. static void __exit rtc_exit (void)
  959. {
  960. cleanup_sysctl();
  961. remove_proc_entry ("driver/rtc", NULL);
  962. misc_deregister(&rtc_dev);
  963. #ifdef __sparc__
  964. if (rtc_has_irq)
  965. free_irq (rtc_irq, &rtc_port);
  966. #else
  967. if (RTC_IOMAPPED)
  968. release_region(RTC_PORT(0), RTC_IO_EXTENT);
  969. else
  970. release_mem_region(RTC_PORT(0), RTC_IO_EXTENT);
  971. #ifdef RTC_IRQ
  972. if (rtc_has_irq)
  973. free_irq (RTC_IRQ, NULL);
  974. #endif
  975. #endif /* __sparc__ */
  976. }
  977. module_init(rtc_init);
  978. module_exit(rtc_exit);
  979. #ifdef RTC_IRQ
  980. /*
  981. * At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
  982. * (usually during an IDE disk interrupt, with IRQ unmasking off)
  983. * Since the interrupt handler doesn't get called, the IRQ status
  984. * byte doesn't get read, and the RTC stops generating interrupts.
  985. * A timer is set, and will call this function if/when that happens.
  986. * To get it out of this stalled state, we just read the status.
  987. * At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
  988. * (You *really* shouldn't be trying to use a non-realtime system
  989. * for something that requires a steady > 1KHz signal anyways.)
  990. */
  991. static void rtc_dropped_irq(unsigned long data)
  992. {
  993. unsigned long freq;
  994. spin_lock_irq (&rtc_lock);
  995. if (hpet_rtc_dropped_irq()) {
  996. spin_unlock_irq(&rtc_lock);
  997. return;
  998. }
  999. /* Just in case someone disabled the timer from behind our back... */
  1000. if (rtc_status & RTC_TIMER_ON)
  1001. mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
  1002. rtc_irq_data += ((rtc_freq/HZ)<<8);
  1003. rtc_irq_data &= ~0xff;
  1004. rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0); /* restart */
  1005. freq = rtc_freq;
  1006. spin_unlock_irq(&rtc_lock);
  1007. printk(KERN_WARNING "rtc: lost some interrupts at %ldHz.\n", freq);
  1008. /* Now we have new data */
  1009. wake_up_interruptible(&rtc_wait);
  1010. kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
  1011. }
  1012. #endif
  1013. #ifdef CONFIG_PROC_FS
  1014. /*
  1015. * Info exported via "/proc/driver/rtc".
  1016. */
  1017. static int rtc_proc_show(struct seq_file *seq, void *v)
  1018. {
  1019. #define YN(bit) ((ctrl & bit) ? "yes" : "no")
  1020. #define NY(bit) ((ctrl & bit) ? "no" : "yes")
  1021. struct rtc_time tm;
  1022. unsigned char batt, ctrl;
  1023. unsigned long freq;
  1024. spin_lock_irq(&rtc_lock);
  1025. batt = CMOS_READ(RTC_VALID) & RTC_VRT;
  1026. ctrl = CMOS_READ(RTC_CONTROL);
  1027. freq = rtc_freq;
  1028. spin_unlock_irq(&rtc_lock);
  1029. rtc_get_rtc_time(&tm);
  1030. /*
  1031. * There is no way to tell if the luser has the RTC set for local
  1032. * time or for Universal Standard Time (GMT). Probably local though.
  1033. */
  1034. seq_printf(seq,
  1035. "rtc_time\t: %02d:%02d:%02d\n"
  1036. "rtc_date\t: %04d-%02d-%02d\n"
  1037. "rtc_epoch\t: %04lu\n",
  1038. tm.tm_hour, tm.tm_min, tm.tm_sec,
  1039. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
  1040. get_rtc_alm_time(&tm);
  1041. /*
  1042. * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
  1043. * match any value for that particular field. Values that are
  1044. * greater than a valid time, but less than 0xc0 shouldn't appear.
  1045. */
  1046. seq_puts(seq, "alarm\t\t: ");
  1047. if (tm.tm_hour <= 24)
  1048. seq_printf(seq, "%02d:", tm.tm_hour);
  1049. else
  1050. seq_puts(seq, "**:");
  1051. if (tm.tm_min <= 59)
  1052. seq_printf(seq, "%02d:", tm.tm_min);
  1053. else
  1054. seq_puts(seq, "**:");
  1055. if (tm.tm_sec <= 59)
  1056. seq_printf(seq, "%02d\n", tm.tm_sec);
  1057. else
  1058. seq_puts(seq, "**\n");
  1059. seq_printf(seq,
  1060. "DST_enable\t: %s\n"
  1061. "BCD\t\t: %s\n"
  1062. "24hr\t\t: %s\n"
  1063. "square_wave\t: %s\n"
  1064. "alarm_IRQ\t: %s\n"
  1065. "update_IRQ\t: %s\n"
  1066. "periodic_IRQ\t: %s\n"
  1067. "periodic_freq\t: %ld\n"
  1068. "batt_status\t: %s\n",
  1069. YN(RTC_DST_EN),
  1070. NY(RTC_DM_BINARY),
  1071. YN(RTC_24H),
  1072. YN(RTC_SQWE),
  1073. YN(RTC_AIE),
  1074. YN(RTC_UIE),
  1075. YN(RTC_PIE),
  1076. freq,
  1077. batt ? "okay" : "dead");
  1078. return 0;
  1079. #undef YN
  1080. #undef NY
  1081. }
  1082. static int rtc_proc_open(struct inode *inode, struct file *file)
  1083. {
  1084. return single_open(file, rtc_proc_show, NULL);
  1085. }
  1086. #endif
  1087. void rtc_get_rtc_time(struct rtc_time *rtc_tm)
  1088. {
  1089. unsigned long uip_watchdog = jiffies, flags;
  1090. unsigned char ctrl;
  1091. #ifdef CONFIG_MACH_DECSTATION
  1092. unsigned int real_year;
  1093. #endif
  1094. /*
  1095. * read RTC once any update in progress is done. The update
  1096. * can take just over 2ms. We wait 20ms. There is no need to
  1097. * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
  1098. * If you need to know *exactly* when a second has started, enable
  1099. * periodic update complete interrupts, (via ioctl) and then
  1100. * immediately read /dev/rtc which will block until you get the IRQ.
  1101. * Once the read clears, read the RTC time (again via ioctl). Easy.
  1102. */
  1103. while (rtc_is_updating() != 0 && jiffies - uip_watchdog < 2*HZ/100)
  1104. cpu_relax();
  1105. /*
  1106. * Only the values that we read from the RTC are set. We leave
  1107. * tm_wday, tm_yday and tm_isdst untouched. Note that while the
  1108. * RTC has RTC_DAY_OF_WEEK, we should usually ignore it, as it is
  1109. * only updated by the RTC when initially set to a non-zero value.
  1110. */
  1111. spin_lock_irqsave(&rtc_lock, flags);
  1112. rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
  1113. rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
  1114. rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
  1115. rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
  1116. rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
  1117. rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
  1118. /* Only set from 2.6.16 onwards */
  1119. rtc_tm->tm_wday = CMOS_READ(RTC_DAY_OF_WEEK);
  1120. #ifdef CONFIG_MACH_DECSTATION
  1121. real_year = CMOS_READ(RTC_DEC_YEAR);
  1122. #endif
  1123. ctrl = CMOS_READ(RTC_CONTROL);
  1124. spin_unlock_irqrestore(&rtc_lock, flags);
  1125. if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  1126. {
  1127. BCD_TO_BIN(rtc_tm->tm_sec);
  1128. BCD_TO_BIN(rtc_tm->tm_min);
  1129. BCD_TO_BIN(rtc_tm->tm_hour);
  1130. BCD_TO_BIN(rtc_tm->tm_mday);
  1131. BCD_TO_BIN(rtc_tm->tm_mon);
  1132. BCD_TO_BIN(rtc_tm->tm_year);
  1133. BCD_TO_BIN(rtc_tm->tm_wday);
  1134. }
  1135. #ifdef CONFIG_MACH_DECSTATION
  1136. rtc_tm->tm_year += real_year - 72;
  1137. #endif
  1138. /*
  1139. * Account for differences between how the RTC uses the values
  1140. * and how they are defined in a struct rtc_time;
  1141. */
  1142. if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
  1143. rtc_tm->tm_year += 100;
  1144. rtc_tm->tm_mon--;
  1145. }
  1146. static void get_rtc_alm_time(struct rtc_time *alm_tm)
  1147. {
  1148. unsigned char ctrl;
  1149. /*
  1150. * Only the values that we read from the RTC are set. That
  1151. * means only tm_hour, tm_min, and tm_sec.
  1152. */
  1153. spin_lock_irq(&rtc_lock);
  1154. alm_tm->tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
  1155. alm_tm->tm_min = CMOS_READ(RTC_MINUTES_ALARM);
  1156. alm_tm->tm_hour = CMOS_READ(RTC_HOURS_ALARM);
  1157. ctrl = CMOS_READ(RTC_CONTROL);
  1158. spin_unlock_irq(&rtc_lock);
  1159. if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  1160. {
  1161. BCD_TO_BIN(alm_tm->tm_sec);
  1162. BCD_TO_BIN(alm_tm->tm_min);
  1163. BCD_TO_BIN(alm_tm->tm_hour);
  1164. }
  1165. }
  1166. #ifdef RTC_IRQ
  1167. /*
  1168. * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
  1169. * Rumour has it that if you frob the interrupt enable/disable
  1170. * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
  1171. * ensure you actually start getting interrupts. Probably for
  1172. * compatibility with older/broken chipset RTC implementations.
  1173. * We also clear out any old irq data after an ioctl() that
  1174. * meddles with the interrupt enable/disable bits.
  1175. */
  1176. static void mask_rtc_irq_bit_locked(unsigned char bit)
  1177. {
  1178. unsigned char val;
  1179. if (hpet_mask_rtc_irq_bit(bit))
  1180. return;
  1181. val = CMOS_READ(RTC_CONTROL);
  1182. val &= ~bit;
  1183. CMOS_WRITE(val, RTC_CONTROL);
  1184. CMOS_READ(RTC_INTR_FLAGS);
  1185. rtc_irq_data = 0;
  1186. }
  1187. static void set_rtc_irq_bit_locked(unsigned char bit)
  1188. {
  1189. unsigned char val;
  1190. if (hpet_set_rtc_irq_bit(bit))
  1191. return;
  1192. val = CMOS_READ(RTC_CONTROL);
  1193. val |= bit;
  1194. CMOS_WRITE(val, RTC_CONTROL);
  1195. CMOS_READ(RTC_INTR_FLAGS);
  1196. rtc_irq_data = 0;
  1197. }
  1198. #endif
  1199. MODULE_AUTHOR("Paul Gortmaker");
  1200. MODULE_LICENSE("GPL");
  1201. MODULE_ALIAS_MISCDEV(RTC_MINOR);