rtc.c 32 KB

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