rtc.c 34 KB

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