rtc.c 33 KB

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