rtc.c 32 KB

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