📄 omap-rtc.c
字号:
/* * TI OMAP Real Time Clock interface for Linux * * Copyright (C) 2003 MontaVista Software, Inc. * Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com> * * Initially based on linux-2.4.20/drivers/char/rtc.c * Copyright (C) 1996 Paul Gortmaker * * This driver allows use of the real time clock (built into * nearly all computers) from user space. It exports the /dev/rtc * interface supporting various ioctl() and also the * /proc/driver/rtc pseudo-file for status information. * * The ioctls can be used to set the interrupt behaviour from the * RTC via IRQs. Then the /dev/rtc interface can be used to make * use of RTC interrupts, be they time update or alarm based. * * The /dev/rtc interface will block on reads until an interrupt * has been received. If a RTC interrupt has already happened, * it will output an unsigned long and then block. The output value * contains the interrupt status in the low byte and the number of * interrupts since the last read in the remaining high bytes. The * /dev/rtc interface can also be used with the select(2) call. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. * * Based on other minimal char device drivers, like Alan's * watchdog, Ted's random, etc. etc. * * Change Log : * v1.0 <gdavis@mvista.com> Initial version based on rtc.c v1.10e * <ramakrishnan@india.ti.com> Added support for 2.6 kernel, * - changed the return value of the interrupt handler */#define RTC_VERSION "1.0"/* * Note that *all* calls to CMOS_READ and CMOS_WRITE are done with * interrupts disabled. * REVISIT: Elaborate on OMAP1510 TRM 15uS BUSY access rule. */#include <linux/config.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/types.h>#include <linux/miscdevice.h>#include <linux/ioport.h>#include <linux/fcntl.h>#include <linux/init.h>#include <linux/poll.h>#include <linux/proc_fs.h>#include <linux/spinlock.h>#include <linux/rtc.h>#include <linux/interrupt.h>#include <linux/rtc.h>#include <asm/io.h>#include <asm/uaccess.h>#include <asm/system.h>#include <asm/hardware.h>#include <asm/irq.h>#include "omap-rtc.h"extern spinlock_t rtc_lock;/* OMAP RTC register access macros: */#define CMOS_READ(addr) omap_readb(addr)#define CMOS_WRITE(val, addr) omap_writeb(val, addr)/* Local BCD/BIN conversion macros: */#ifdef BCD_TO_BIN#undef BCD_TO_BIN#endif#define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10) #ifdef BIN_TO_BCD#undef BIN_TO_BCD#endif#define BIN_TO_BCD(val) ((val)=(((val)/10)<<4) + (val)%10)/* * We sponge a minor off of the misc major. No need slurping * up another valuable major dev number for this. If you add * an ioctl, make sure you don't conflict with SPARC's RTC * ioctls. */static struct fasync_struct *rtc_async_queue;static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);static ssize_t rtc_read(struct file *file, char *buf, size_t count, loff_t *ppos);static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);static unsigned int rtc_poll(struct file *file, poll_table *wait);static void get_rtc_time (struct rtc_time *rtc_tm);static void get_rtc_alm_time (struct rtc_time *alm_tm);static void set_rtc_irq_bit(unsigned char bit);static void mask_rtc_irq_bit(unsigned char bit);static inline unsigned char rtc_is_updating(void);static int rtc_read_proc(char *page, char **start, off_t off, int count, int *eof, void *data);/* * Bits in rtc_status. (7 bits of room for future expansion) */#define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use *//* * REVISIT: fix this comment: * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is * protected by the big kernel lock. */static unsigned long rtc_status = 0; /* bitmapped status byte. */static unsigned long rtc_irq_data = 0; /* our output to the world *//* * If this driver ever becomes modularised, it will be really nice * to make the epoch retain its value across module reload... */static unsigned long epoch = 1900; /* year corresponding to 0x00 */static const unsigned char days_in_mo[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};/* * A very tiny interrupt handler. It runs with SA_INTERRUPT set. */static irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs){ /* * Either an alarm interrupt or update complete interrupt. * We store the status in the low byte and the number of * interrupts received since the last read in the remainder * of rtc_irq_data. */ spin_lock (&rtc_lock); rtc_irq_data += 0x100; rtc_irq_data &= ~0xff; rtc_irq_data |= CMOS_READ(OMAP_RTC_STATUS_REG); if (rtc_irq_data & OMAP_RTC_STATUS_ALARM) CMOS_WRITE(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG); spin_unlock (&rtc_lock); /* Now do the rest of the actions */ wake_up_interruptible(&rtc_wait); kill_fasync (&rtc_async_queue, SIGIO, POLL_IN); return IRQ_HANDLED;}/* * Now all the various file operations that we export. */static ssize_t rtc_read(struct file *file, char *buf, size_t count, loff_t *ppos){ DECLARE_WAITQUEUE(wait, current); unsigned long data; ssize_t retval; if (count < sizeof(unsigned long)) return -EINVAL; add_wait_queue(&rtc_wait, &wait); set_current_state(TASK_INTERRUPTIBLE); for (;;) { spin_lock_irq (&rtc_lock); data = rtc_irq_data; if (data != 0) { rtc_irq_data = 0; break; } spin_unlock_irq (&rtc_lock); if (file->f_flags & O_NONBLOCK) { retval = -EAGAIN; goto out; } if (signal_pending(current)) { retval = -ERESTARTSYS; goto out; } schedule(); } spin_unlock_irq (&rtc_lock); retval = put_user(data, (unsigned long *)buf); if (!retval) retval = sizeof(unsigned long); out: set_current_state(TASK_RUNNING); remove_wait_queue(&rtc_wait, &wait); return retval;}static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ struct rtc_time wtime; switch (cmd) { case RTC_AIE_OFF: /* Mask alarm int. enab. bit */ { mask_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_ALARM); return 0; } case RTC_AIE_ON: /* Allow alarm interrupts. */ { set_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_ALARM); return 0; } case RTC_UIE_OFF: /* Mask ints from RTC updates. */ { mask_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_TIMER); return 0; } case RTC_UIE_ON: /* Allow ints for RTC updates. */ { set_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_TIMER); return 0; } case RTC_ALM_READ: /* Read the present alarm time */ { /* * This returns a struct rtc_time. Reading >= 0xc0 * means "don't care" or "match all". Only the tm_hour, * tm_min, and tm_sec values are filled in. */ memset(&wtime, 0, sizeof(struct rtc_time)); get_rtc_alm_time(&wtime); break; } case RTC_ALM_SET: /* Store a time into the alarm */ { struct rtc_time alm_tm; unsigned char mon, day, hrs, min, sec, leap_yr; unsigned int yrs; if (copy_from_user(&alm_tm, (struct rtc_time*)arg, sizeof(struct rtc_time))) return -EFAULT; yrs = alm_tm.tm_year + 1900; mon = alm_tm.tm_mon + 1; day = alm_tm.tm_mday; hrs = alm_tm.tm_hour; min = alm_tm.tm_min; sec = alm_tm.tm_sec; if (yrs < 1970) return -EINVAL; leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400)); if ((mon > 12) || (day == 0)) return -EINVAL; if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr))) return -EINVAL; if ((hrs >= 24) || (min >= 60) || (sec >= 60)) return -EINVAL; if ((yrs -= epoch) > 255) /* They are unsigned */ return -EINVAL; if (yrs > 169) { return -EINVAL; } if (yrs >= 100) yrs -= 100; BIN_TO_BCD(sec); BIN_TO_BCD(min); BIN_TO_BCD(hrs); BIN_TO_BCD(day); BIN_TO_BCD(mon); BIN_TO_BCD(yrs); spin_lock_irq(&rtc_lock); CMOS_WRITE(yrs, OMAP_RTC_ALARM_YEARS_REG); CMOS_WRITE(mon, OMAP_RTC_ALARM_MONTHS_REG); CMOS_WRITE(day, OMAP_RTC_ALARM_DAYS_REG); CMOS_WRITE(hrs, OMAP_RTC_ALARM_HOURS_REG); CMOS_WRITE(min, OMAP_RTC_ALARM_MINUTES_REG); CMOS_WRITE(sec, OMAP_RTC_ALARM_SECONDS_REG); spin_unlock_irq(&rtc_lock); return 0; } case RTC_RD_TIME: /* Read the time/date from RTC */ { memset(&wtime, 0, sizeof(struct rtc_time)); get_rtc_time(&wtime); break; } case RTC_SET_TIME: /* Set the RTC */ { struct rtc_time rtc_tm; unsigned char mon, day, hrs, min, sec, leap_yr; unsigned char save_control; unsigned int yrs; if (!capable(CAP_SYS_TIME)) return -EACCES; if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time))) return -EFAULT; yrs = rtc_tm.tm_year + 1900; mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */ day = rtc_tm.tm_mday; hrs = rtc_tm.tm_hour; min = rtc_tm.tm_min; sec = rtc_tm.tm_sec; if (yrs < 1970) return -EINVAL; leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400)); if ((mon > 12) || (day == 0)) return -EINVAL; if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr))) return -EINVAL; if ((hrs >= 24) || (min >= 60) || (sec >= 60)) return -EINVAL; if ((yrs -= epoch) > 255) /* They are unsigned */ return -EINVAL; if (yrs > 169) { return -EINVAL; } if (yrs >= 100) yrs -= 100; BIN_TO_BCD(sec); BIN_TO_BCD(min); BIN_TO_BCD(hrs); BIN_TO_BCD(day); BIN_TO_BCD(mon); BIN_TO_BCD(yrs); spin_lock_irq(&rtc_lock); save_control = CMOS_READ(OMAP_RTC_CTRL_REG); CMOS_WRITE((save_control & ~OMAP_RTC_CTRL_STOP), OMAP_RTC_CTRL_REG); CMOS_WRITE(yrs, OMAP_RTC_YEARS_REG); CMOS_WRITE(mon, OMAP_RTC_MONTHS_REG); CMOS_WRITE(day, OMAP_RTC_DAYS_REG); CMOS_WRITE(hrs, OMAP_RTC_HOURS_REG); CMOS_WRITE(min, OMAP_RTC_MINUTES_REG); CMOS_WRITE(sec, OMAP_RTC_SECONDS_REG); CMOS_WRITE((save_control | OMAP_RTC_CTRL_STOP), OMAP_RTC_CTRL_REG); spin_unlock_irq(&rtc_lock); return 0; } case RTC_EPOCH_READ: /* Read the epoch. */ {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -