rtc.c 32 KB

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