rtc.c 32 KB

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