/* * An RTC driver for Allwinner A10/A20 * * Copyright (c) 2013, Carlo Caione * * 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. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define SUNXI_LOSC_CTRL 0x0000 #define SUNXI_LOSC_CTRL_RTC_HMS_ACC BIT(8) #define SUNXI_LOSC_CTRL_RTC_YMD_ACC BIT(7) #define SUNXI_RTC_YMD 0x0004 #define SUNXI_RTC_HMS 0x0008 #define SUNXI_ALRM_DHMS 0x000c #define SUNXI_ALRM_EN 0x0014 #define SUNXI_ALRM_EN_CNT_EN BIT(8) #define SUNXI_ALRM_IRQ_EN 0x0018 #define SUNXI_ALRM_IRQ_EN_CNT_IRQ_EN BIT(0) #define SUNXI_ALRM_IRQ_STA 0x001c #define SUNXI_ALRM_IRQ_STA_CNT_IRQ_PEND BIT(0) #define SUNXI_MASK_DH 0x0000001f #define SUNXI_MASK_SM 0x0000003f #define SUNXI_MASK_M 0x0000000f #define SUNXI_MASK_LY 0x00000001 #define SUNXI_MASK_D 0x00000ffe #define SUNXI_MASK_M 0x0000000f #define SUNXI_GET(x, mask, shift) (((x) & ((mask) << (shift))) \ >> (shift)) #define SUNXI_SET(x, mask, shift) (((x) & (mask)) << (shift)) /* * Get date values */ #define SUNXI_DATE_GET_DAY_VALUE(x) SUNXI_GET(x, SUNXI_MASK_DH, 0) #define SUNXI_DATE_GET_MON_VALUE(x) SUNXI_GET(x, SUNXI_MASK_M, 8) #define SUNXI_DATE_GET_YEAR_VALUE(x, mask) SUNXI_GET(x, mask, 16) /* * Get time values */ #define SUNXI_TIME_GET_SEC_VALUE(x) SUNXI_GET(x, SUNXI_MASK_SM, 0) #define SUNXI_TIME_GET_MIN_VALUE(x) SUNXI_GET(x, SUNXI_MASK_SM, 8) #define SUNXI_TIME_GET_HOUR_VALUE(x) SUNXI_GET(x, SUNXI_MASK_DH, 16) /* * Get alarm values */ #define SUNXI_ALRM_GET_SEC_VALUE(x) SUNXI_GET(x, SUNXI_MASK_SM, 0) #define SUNXI_ALRM_GET_MIN_VALUE(x) SUNXI_GET(x, SUNXI_MASK_SM, 8) #define SUNXI_ALRM_GET_HOUR_VALUE(x) SUNXI_GET(x, SUNXI_MASK_DH, 16) /* * Set date values */ #define SUNXI_DATE_SET_DAY_VALUE(x) SUNXI_DATE_GET_DAY_VALUE(x) #define SUNXI_DATE_SET_MON_VALUE(x) SUNXI_SET(x, SUNXI_MASK_M, 8) #define SUNXI_DATE_SET_YEAR_VALUE(x, mask) SUNXI_SET(x, mask, 16) #define SUNXI_LEAP_SET_VALUE(x, shift) SUNXI_SET(x, SUNXI_MASK_LY, shift) /* * Set time values */ #define SUNXI_TIME_SET_SEC_VALUE(x) SUNXI_TIME_GET_SEC_VALUE(x) #define SUNXI_TIME_SET_MIN_VALUE(x) SUNXI_SET(x, SUNXI_MASK_SM, 8) #define SUNXI_TIME_SET_HOUR_VALUE(x) SUNXI_SET(x, SUNXI_MASK_DH, 16) /* * Set alarm values */ #define SUNXI_ALRM_SET_SEC_VALUE(x) SUNXI_ALRM_GET_SEC_VALUE(x) #define SUNXI_ALRM_SET_MIN_VALUE(x) SUNXI_SET(x, SUNXI_MASK_SM, 8) #define SUNXI_ALRM_SET_HOUR_VALUE(x) SUNXI_SET(x, SUNXI_MASK_DH, 16) #define SUNXI_ALRM_SET_DAY_VALUE(x) SUNXI_SET(x, SUNXI_MASK_D, 21) /* * Time unit conversions */ #define SEC_IN_MIN 60 #define SEC_IN_HOUR (60 * SEC_IN_MIN) #define SEC_IN_DAY (24 * SEC_IN_HOUR) /* * The year parameter passed to the driver is usually an offset relative to * the year 1900. This macro is used to convert this offset to another one * relative to the minimum year allowed by the hardware. */ #define SUNXI_YEAR_OFF(x) ((x)->min - 1900) /* * min and max year are arbitrary set considering the limited range of the * hardware register field */ struct sunxi_rtc_data_year { unsigned int min; /* min year allowed */ unsigned int max; /* max year allowed */ unsigned int mask; /* mask for the year field */ unsigned char leap_shift; /* bit shift to get the leap year */ }; static struct sunxi_rtc_data_year data_year_param[] = { [0] = { .min = 2010, .max = 2073, .mask = 0x3f, .leap_shift = 22, }, [1] = { .min = 1970, .max = 2225, .mask = 0xff, .leap_shift = 24, }, }; struct sunxi_rtc_dev { struct rtc_device *rtc; struct device *dev; struct sunxi_rtc_data_year *data_year; void __iomem *base; int irq; }; static irqreturn_t sunxi_rtc_alarmirq(int irq, void *id) { struct sunxi_rtc_dev *chip = (struct sunxi_rtc_dev *) id; u32 val; val = readl(chip->base + SUNXI_ALRM_IRQ_STA); if (val & SUNXI_ALRM_IRQ_STA_CNT_IRQ_PEND) { val |= SUNXI_ALRM_IRQ_STA_CNT_IRQ_PEND; writel(val, chip->base + SUNXI_ALRM_IRQ_STA); rtc_update_irq(chip->rtc, 1, RTC_AF | RTC_IRQF); return IRQ_HANDLED; } return IRQ_NONE; } static void sunxi_rtc_setaie(int to, struct sunxi_rtc_dev *chip) { u32 alrm_val = 0; u32 alrm_irq_val = 0; if (to) {