rtc.c 34 KB

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