![Arnd Bergmann](/assets/img/avatar_default.png)
Drivers should not really include stuff from asm-generic directly, and the PC-style cmos rtc driver does this in order to reuse the mc146818 implementation of get_rtc_time/set_rtc_time rather than the architecture specific one for the architecture it gets built for. To make it more obvious what is going on, this moves and renames the two functions into include/linux/mc146818rtc.h, which holds the other mc146818 specific code. Ideally it would be in a .c file, but that would require extra infrastructure as the functions are called by multiple drivers with conflicting dependencies. With this change, the asm-generic/rtc.h header also becomes much more generic, so it can be reused more easily across any architecture that still relies on the genrtc driver. The only caller of the internal __get_rtc_time/__set_rtc_time functions is in arch/alpha/kernel/rtc.c, and we just change those over to the new naming. Signed-off-by: Arnd Bergmann <arnd@arndb.de> Acked-by: Geert Uytterhoeven <geert@linux-m68k.org> Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
318 lines
9.1 KiB
C
318 lines
9.1 KiB
C
/* mc146818rtc.h - register definitions for the Real-Time-Clock / CMOS RAM
|
|
* Copyright Torsten Duwe <duwe@informatik.uni-erlangen.de> 1993
|
|
* derived from Data Sheet, Copyright Motorola 1984 (!).
|
|
* It was written to be part of the Linux operating system.
|
|
*/
|
|
/* permission is hereby granted to copy, modify and redistribute this code
|
|
* in terms of the GNU Library General Public License, Version 2 or later,
|
|
* at your option.
|
|
*/
|
|
|
|
#ifndef _MC146818RTC_H
|
|
#define _MC146818RTC_H
|
|
|
|
#include <asm/io.h>
|
|
#include <linux/rtc.h> /* get the user-level API */
|
|
#include <asm/mc146818rtc.h> /* register access macros */
|
|
#include <linux/bcd.h>
|
|
#include <linux/delay.h>
|
|
|
|
#ifdef CONFIG_ACPI
|
|
#include <linux/acpi.h>
|
|
#endif
|
|
|
|
#ifdef __KERNEL__
|
|
#include <linux/spinlock.h> /* spinlock_t */
|
|
extern spinlock_t rtc_lock; /* serialize CMOS RAM access */
|
|
|
|
/* Some RTCs extend the mc146818 register set to support alarms of more
|
|
* than 24 hours in the future; or dates that include a century code.
|
|
* This platform_data structure can pass this information to the driver.
|
|
*
|
|
* Also, some platforms need suspend()/resume() hooks to kick in special
|
|
* handling of wake alarms, e.g. activating ACPI BIOS hooks or setting up
|
|
* a separate wakeup alarm used by some almost-clone chips.
|
|
*/
|
|
struct cmos_rtc_board_info {
|
|
void (*wake_on)(struct device *dev);
|
|
void (*wake_off)(struct device *dev);
|
|
|
|
u32 flags;
|
|
#define CMOS_RTC_FLAGS_NOFREQ (1 << 0)
|
|
int address_space;
|
|
|
|
u8 rtc_day_alarm; /* zero, or register index */
|
|
u8 rtc_mon_alarm; /* zero, or register index */
|
|
u8 rtc_century; /* zero, or register index */
|
|
};
|
|
#endif
|
|
|
|
/**********************************************************************
|
|
* register summary
|
|
**********************************************************************/
|
|
#define RTC_SECONDS 0
|
|
#define RTC_SECONDS_ALARM 1
|
|
#define RTC_MINUTES 2
|
|
#define RTC_MINUTES_ALARM 3
|
|
#define RTC_HOURS 4
|
|
#define RTC_HOURS_ALARM 5
|
|
/* RTC_*_alarm is always true if 2 MSBs are set */
|
|
# define RTC_ALARM_DONT_CARE 0xC0
|
|
|
|
#define RTC_DAY_OF_WEEK 6
|
|
#define RTC_DAY_OF_MONTH 7
|
|
#define RTC_MONTH 8
|
|
#define RTC_YEAR 9
|
|
|
|
/* control registers - Moto names
|
|
*/
|
|
#define RTC_REG_A 10
|
|
#define RTC_REG_B 11
|
|
#define RTC_REG_C 12
|
|
#define RTC_REG_D 13
|
|
|
|
/**********************************************************************
|
|
* register details
|
|
**********************************************************************/
|
|
#define RTC_FREQ_SELECT RTC_REG_A
|
|
|
|
/* update-in-progress - set to "1" 244 microsecs before RTC goes off the bus,
|
|
* reset after update (may take 1.984ms @ 32768Hz RefClock) is complete,
|
|
* totalling to a max high interval of 2.228 ms.
|
|
*/
|
|
# define RTC_UIP 0x80
|
|
# define RTC_DIV_CTL 0x70
|
|
/* divider control: refclock values 4.194 / 1.049 MHz / 32.768 kHz */
|
|
# define RTC_REF_CLCK_4MHZ 0x00
|
|
# define RTC_REF_CLCK_1MHZ 0x10
|
|
# define RTC_REF_CLCK_32KHZ 0x20
|
|
/* 2 values for divider stage reset, others for "testing purposes only" */
|
|
# define RTC_DIV_RESET1 0x60
|
|
# define RTC_DIV_RESET2 0x70
|
|
/* Periodic intr. / Square wave rate select. 0=none, 1=32.8kHz,... 15=2Hz */
|
|
# define RTC_RATE_SELECT 0x0F
|
|
|
|
/**********************************************************************/
|
|
#define RTC_CONTROL RTC_REG_B
|
|
# define RTC_SET 0x80 /* disable updates for clock setting */
|
|
# define RTC_PIE 0x40 /* periodic interrupt enable */
|
|
# define RTC_AIE 0x20 /* alarm interrupt enable */
|
|
# define RTC_UIE 0x10 /* update-finished interrupt enable */
|
|
# define RTC_SQWE 0x08 /* enable square-wave output */
|
|
# define RTC_DM_BINARY 0x04 /* all time/date values are BCD if clear */
|
|
# define RTC_24H 0x02 /* 24 hour mode - else hours bit 7 means pm */
|
|
# define RTC_DST_EN 0x01 /* auto switch DST - works f. USA only */
|
|
|
|
/**********************************************************************/
|
|
#define RTC_INTR_FLAGS RTC_REG_C
|
|
/* caution - cleared by read */
|
|
# define RTC_IRQF 0x80 /* any of the following 3 is active */
|
|
# define RTC_PF 0x40
|
|
# define RTC_AF 0x20
|
|
# define RTC_UF 0x10
|
|
|
|
/**********************************************************************/
|
|
#define RTC_VALID RTC_REG_D
|
|
# define RTC_VRT 0x80 /* valid RAM and time */
|
|
/**********************************************************************/
|
|
|
|
#ifndef ARCH_RTC_LOCATION /* Override by <asm/mc146818rtc.h>? */
|
|
|
|
#define RTC_IO_EXTENT 0x8
|
|
#define RTC_IO_EXTENT_USED 0x2
|
|
#define RTC_IOMAPPED 1 /* Default to I/O mapping. */
|
|
|
|
#else
|
|
#define RTC_IO_EXTENT_USED RTC_IO_EXTENT
|
|
#endif /* ARCH_RTC_LOCATION */
|
|
|
|
/*
|
|
* Returns true if a clock update is in progress
|
|
*/
|
|
static inline unsigned char mc146818_is_updating(void)
|
|
{
|
|
unsigned char uip;
|
|
unsigned long flags;
|
|
|
|
spin_lock_irqsave(&rtc_lock, flags);
|
|
uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
|
|
spin_unlock_irqrestore(&rtc_lock, flags);
|
|
return uip;
|
|
}
|
|
|
|
static inline unsigned int mc146818_get_time(struct rtc_time *time)
|
|
{
|
|
unsigned char ctrl;
|
|
unsigned long flags;
|
|
unsigned char century = 0;
|
|
|
|
#ifdef CONFIG_MACH_DECSTATION
|
|
unsigned int real_year;
|
|
#endif
|
|
|
|
/*
|
|
* read RTC once any update in progress is done. The update
|
|
* can take just over 2ms. We wait 20ms. There is no need to
|
|
* to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
|
|
* If you need to know *exactly* when a second has started, enable
|
|
* periodic update complete interrupts, (via ioctl) and then
|
|
* immediately read /dev/rtc which will block until you get the IRQ.
|
|
* Once the read clears, read the RTC time (again via ioctl). Easy.
|
|
*/
|
|
if (mc146818_is_updating())
|
|
mdelay(20);
|
|
|
|
/*
|
|
* Only the values that we read from the RTC are set. We leave
|
|
* tm_wday, tm_yday and tm_isdst untouched. Even though the
|
|
* RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
|
|
* by the RTC when initially set to a non-zero value.
|
|
*/
|
|
spin_lock_irqsave(&rtc_lock, flags);
|
|
time->tm_sec = CMOS_READ(RTC_SECONDS);
|
|
time->tm_min = CMOS_READ(RTC_MINUTES);
|
|
time->tm_hour = CMOS_READ(RTC_HOURS);
|
|
time->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
|
|
time->tm_mon = CMOS_READ(RTC_MONTH);
|
|
time->tm_year = CMOS_READ(RTC_YEAR);
|
|
#ifdef CONFIG_MACH_DECSTATION
|
|
real_year = CMOS_READ(RTC_DEC_YEAR);
|
|
#endif
|
|
#ifdef CONFIG_ACPI
|
|
if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
|
|
acpi_gbl_FADT.century)
|
|
century = CMOS_READ(acpi_gbl_FADT.century);
|
|
#endif
|
|
ctrl = CMOS_READ(RTC_CONTROL);
|
|
spin_unlock_irqrestore(&rtc_lock, flags);
|
|
|
|
if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
|
|
{
|
|
time->tm_sec = bcd2bin(time->tm_sec);
|
|
time->tm_min = bcd2bin(time->tm_min);
|
|
time->tm_hour = bcd2bin(time->tm_hour);
|
|
time->tm_mday = bcd2bin(time->tm_mday);
|
|
time->tm_mon = bcd2bin(time->tm_mon);
|
|
time->tm_year = bcd2bin(time->tm_year);
|
|
century = bcd2bin(century);
|
|
}
|
|
|
|
#ifdef CONFIG_MACH_DECSTATION
|
|
time->tm_year += real_year - 72;
|
|
#endif
|
|
|
|
if (century)
|
|
time->tm_year += (century - 19) * 100;
|
|
|
|
/*
|
|
* Account for differences between how the RTC uses the values
|
|
* and how they are defined in a struct rtc_time;
|
|
*/
|
|
if (time->tm_year <= 69)
|
|
time->tm_year += 100;
|
|
|
|
time->tm_mon--;
|
|
|
|
return RTC_24H;
|
|
}
|
|
|
|
/* Set the current date and time in the real time clock. */
|
|
static inline int mc146818_set_time(struct rtc_time *time)
|
|
{
|
|
unsigned long flags;
|
|
unsigned char mon, day, hrs, min, sec;
|
|
unsigned char save_control, save_freq_select;
|
|
unsigned int yrs;
|
|
#ifdef CONFIG_MACH_DECSTATION
|
|
unsigned int real_yrs, leap_yr;
|
|
#endif
|
|
unsigned char century = 0;
|
|
|
|
yrs = time->tm_year;
|
|
mon = time->tm_mon + 1; /* tm_mon starts at zero */
|
|
day = time->tm_mday;
|
|
hrs = time->tm_hour;
|
|
min = time->tm_min;
|
|
sec = time->tm_sec;
|
|
|
|
if (yrs > 255) /* They are unsigned */
|
|
return -EINVAL;
|
|
|
|
spin_lock_irqsave(&rtc_lock, flags);
|
|
#ifdef CONFIG_MACH_DECSTATION
|
|
real_yrs = yrs;
|
|
leap_yr = ((!((yrs + 1900) % 4) && ((yrs + 1900) % 100)) ||
|
|
!((yrs + 1900) % 400));
|
|
yrs = 72;
|
|
|
|
/*
|
|
* We want to keep the year set to 73 until March
|
|
* for non-leap years, so that Feb, 29th is handled
|
|
* correctly.
|
|
*/
|
|
if (!leap_yr && mon < 3) {
|
|
real_yrs--;
|
|
yrs = 73;
|
|
}
|
|
#endif
|
|
|
|
#ifdef CONFIG_ACPI
|
|
if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
|
|
acpi_gbl_FADT.century) {
|
|
century = (yrs + 1900) / 100;
|
|
yrs %= 100;
|
|
}
|
|
#endif
|
|
|
|
/* These limits and adjustments are independent of
|
|
* whether the chip is in binary mode or not.
|
|
*/
|
|
if (yrs > 169) {
|
|
spin_unlock_irqrestore(&rtc_lock, flags);
|
|
return -EINVAL;
|
|
}
|
|
|
|
if (yrs >= 100)
|
|
yrs -= 100;
|
|
|
|
if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
|
|
|| RTC_ALWAYS_BCD) {
|
|
sec = bin2bcd(sec);
|
|
min = bin2bcd(min);
|
|
hrs = bin2bcd(hrs);
|
|
day = bin2bcd(day);
|
|
mon = bin2bcd(mon);
|
|
yrs = bin2bcd(yrs);
|
|
century = bin2bcd(century);
|
|
}
|
|
|
|
save_control = CMOS_READ(RTC_CONTROL);
|
|
CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
|
|
save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
|
|
CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
|
|
|
|
#ifdef CONFIG_MACH_DECSTATION
|
|
CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
|
|
#endif
|
|
CMOS_WRITE(yrs, RTC_YEAR);
|
|
CMOS_WRITE(mon, RTC_MONTH);
|
|
CMOS_WRITE(day, RTC_DAY_OF_MONTH);
|
|
CMOS_WRITE(hrs, RTC_HOURS);
|
|
CMOS_WRITE(min, RTC_MINUTES);
|
|
CMOS_WRITE(sec, RTC_SECONDS);
|
|
#ifdef CONFIG_ACPI
|
|
if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
|
|
acpi_gbl_FADT.century)
|
|
CMOS_WRITE(century, acpi_gbl_FADT.century);
|
|
#endif
|
|
|
|
CMOS_WRITE(save_control, RTC_CONTROL);
|
|
CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
|
|
|
|
spin_unlock_irqrestore(&rtc_lock, flags);
|
|
|
|
return 0;
|
|
}
|
|
|
|
#endif /* _MC146818RTC_H */
|