linux/include/asm-generic/rtc.h
<<
>>
Prefs
   1/* 
   2 * include/asm-generic/rtc.h
   3 *
   4 * Author: Tom Rini <trini@mvista.com>
   5 *
   6 * Based on:
   7 * drivers/char/rtc.c
   8 *
   9 * Please read the COPYING file for all license details.
  10 */
  11
  12#ifndef __ASM_RTC_H__
  13#define __ASM_RTC_H__
  14
  15#ifdef __KERNEL__
  16
  17#include <linux/mc146818rtc.h>
  18#include <linux/rtc.h>
  19#include <linux/bcd.h>
  20
  21#define RTC_PIE 0x40            /* periodic interrupt enable */
  22#define RTC_AIE 0x20            /* alarm interrupt enable */
  23#define RTC_UIE 0x10            /* update-finished interrupt enable */
  24
  25/* some dummy definitions */
  26#define RTC_BATT_BAD 0x100      /* battery bad */
  27#define RTC_SQWE 0x08           /* enable square-wave output */
  28#define RTC_DM_BINARY 0x04      /* all time/date values are BCD if clear */
  29#define RTC_24H 0x02            /* 24 hour mode - else hours bit 7 means pm */
  30#define RTC_DST_EN 0x01         /* auto switch DST - works f. USA only */
  31
  32/*
  33 * Returns true if a clock update is in progress
  34 */
  35static inline unsigned char rtc_is_updating(void)
  36{
  37        unsigned char uip;
  38
  39        spin_lock_irq(&rtc_lock);
  40        uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
  41        spin_unlock_irq(&rtc_lock);
  42        return uip;
  43}
  44
  45static inline unsigned int get_rtc_time(struct rtc_time *time)
  46{
  47        unsigned long uip_watchdog = jiffies;
  48        unsigned char ctrl;
  49#ifdef CONFIG_MACH_DECSTATION
  50        unsigned int real_year;
  51#endif
  52
  53        /*
  54         * read RTC once any update in progress is done. The update
  55         * can take just over 2ms. We wait 10 to 20ms. There is no need to
  56         * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
  57         * If you need to know *exactly* when a second has started, enable
  58         * periodic update complete interrupts, (via ioctl) and then 
  59         * immediately read /dev/rtc which will block until you get the IRQ.
  60         * Once the read clears, read the RTC time (again via ioctl). Easy.
  61         */
  62
  63        if (rtc_is_updating() != 0)
  64                while (jiffies - uip_watchdog < 2*HZ/100) {
  65                        barrier();
  66                        cpu_relax();
  67                }
  68
  69        /*
  70         * Only the values that we read from the RTC are set. We leave
  71         * tm_wday, tm_yday and tm_isdst untouched. Even though the
  72         * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
  73         * by the RTC when initially set to a non-zero value.
  74         */
  75        spin_lock_irq(&rtc_lock);
  76        time->tm_sec = CMOS_READ(RTC_SECONDS);
  77        time->tm_min = CMOS_READ(RTC_MINUTES);
  78        time->tm_hour = CMOS_READ(RTC_HOURS);
  79        time->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
  80        time->tm_mon = CMOS_READ(RTC_MONTH);
  81        time->tm_year = CMOS_READ(RTC_YEAR);
  82#ifdef CONFIG_MACH_DECSTATION
  83        real_year = CMOS_READ(RTC_DEC_YEAR);
  84#endif
  85        ctrl = CMOS_READ(RTC_CONTROL);
  86        spin_unlock_irq(&rtc_lock);
  87
  88        if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  89        {
  90                BCD_TO_BIN(time->tm_sec);
  91                BCD_TO_BIN(time->tm_min);
  92                BCD_TO_BIN(time->tm_hour);
  93                BCD_TO_BIN(time->tm_mday);
  94                BCD_TO_BIN(time->tm_mon);
  95                BCD_TO_BIN(time->tm_year);
  96        }
  97
  98#ifdef CONFIG_MACH_DECSTATION
  99        time->tm_year += real_year - 72;
 100#endif
 101
 102        /*
 103         * Account for differences between how the RTC uses the values
 104         * and how they are defined in a struct rtc_time;
 105         */
 106        if (time->tm_year <= 69)
 107                time->tm_year += 100;
 108
 109        time->tm_mon--;
 110
 111        return RTC_24H;
 112}
 113
 114/* Set the current date and time in the real time clock. */
 115static inline int set_rtc_time(struct rtc_time *time)
 116{
 117        unsigned long flags;
 118        unsigned char mon, day, hrs, min, sec;
 119        unsigned char save_control, save_freq_select;
 120        unsigned int yrs;
 121#ifdef CONFIG_MACH_DECSTATION
 122        unsigned int real_yrs, leap_yr;
 123#endif
 124
 125        yrs = time->tm_year;
 126        mon = time->tm_mon + 1;   /* tm_mon starts at zero */
 127        day = time->tm_mday;
 128        hrs = time->tm_hour;
 129        min = time->tm_min;
 130        sec = time->tm_sec;
 131
 132        if (yrs > 255)  /* They are unsigned */
 133                return -EINVAL;
 134
 135        spin_lock_irqsave(&rtc_lock, flags);
 136#ifdef CONFIG_MACH_DECSTATION
 137        real_yrs = yrs;
 138        leap_yr = ((!((yrs + 1900) % 4) && ((yrs + 1900) % 100)) ||
 139                        !((yrs + 1900) % 400));
 140        yrs = 72;
 141
 142        /*
 143         * We want to keep the year set to 73 until March
 144         * for non-leap years, so that Feb, 29th is handled
 145         * correctly.
 146         */
 147        if (!leap_yr && mon < 3) {
 148                real_yrs--;
 149                yrs = 73;
 150        }
 151#endif
 152        /* These limits and adjustments are independent of
 153         * whether the chip is in binary mode or not.
 154         */
 155        if (yrs > 169) {
 156                spin_unlock_irqrestore(&rtc_lock, flags);
 157                return -EINVAL;
 158        }
 159
 160        if (yrs >= 100)
 161                yrs -= 100;
 162
 163        if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
 164            || RTC_ALWAYS_BCD) {
 165                BIN_TO_BCD(sec);
 166                BIN_TO_BCD(min);
 167                BIN_TO_BCD(hrs);
 168                BIN_TO_BCD(day);
 169                BIN_TO_BCD(mon);
 170                BIN_TO_BCD(yrs);
 171        }
 172
 173        save_control = CMOS_READ(RTC_CONTROL);
 174        CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
 175        save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
 176        CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
 177
 178#ifdef CONFIG_MACH_DECSTATION
 179        CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
 180#endif
 181        CMOS_WRITE(yrs, RTC_YEAR);
 182        CMOS_WRITE(mon, RTC_MONTH);
 183        CMOS_WRITE(day, RTC_DAY_OF_MONTH);
 184        CMOS_WRITE(hrs, RTC_HOURS);
 185        CMOS_WRITE(min, RTC_MINUTES);
 186        CMOS_WRITE(sec, RTC_SECONDS);
 187
 188        CMOS_WRITE(save_control, RTC_CONTROL);
 189        CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
 190
 191        spin_unlock_irqrestore(&rtc_lock, flags);
 192
 193        return 0;
 194}
 195
 196static inline unsigned int get_rtc_ss(void)
 197{
 198        struct rtc_time h;
 199
 200        get_rtc_time(&h);
 201        return h.tm_sec;
 202}
 203
 204static inline int get_rtc_pll(struct rtc_pll_info *pll)
 205{
 206        return -EINVAL;
 207}
 208static inline int set_rtc_pll(struct rtc_pll_info *pll)
 209{
 210        return -EINVAL;
 211}
 212
 213#endif /* __KERNEL__ */
 214#endif /* __ASM_RTC_H__ */
 215