rtc.c 34 KB

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