rtc.c 32 KB

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