rtc.c 34 KB

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