rtc.c 33 KB

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