mirror of https://github.com/OpenIPC/firmware.git
680 lines
19 KiB
Diff
680 lines
19 KiB
Diff
diff -drupN a/drivers/rtc/rtc-sunxi-v2.c b/drivers/rtc/rtc-sunxi-v2.c
|
|
--- a/drivers/rtc/rtc-sunxi-v2.c 1970-01-01 03:00:00.000000000 +0300
|
|
+++ b/drivers/rtc/rtc-sunxi-v2.c 2022-06-12 05:28:14.000000000 +0300
|
|
@@ -0,0 +1,675 @@
|
|
+/*
|
|
+ * An RTC driver for Sunxi Platform of Allwinner SoC
|
|
+ *
|
|
+ * Copyright (c) 2013, Carlo Caione <carlo.caione@gmail.com>
|
|
+ *
|
|
+ * 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.
|
|
+ */
|
|
+
|
|
+#include <linux/delay.h>
|
|
+#include <linux/err.h>
|
|
+#include <linux/fs.h>
|
|
+#include <linux/init.h>
|
|
+#include <linux/interrupt.h>
|
|
+#include <linux/io.h>
|
|
+#include <linux/kernel.h>
|
|
+#include <linux/module.h>
|
|
+#include <linux/of.h>
|
|
+#include <linux/of_address.h>
|
|
+#include <linux/of_device.h>
|
|
+#include <linux/platform_device.h>
|
|
+#include <linux/pm_runtime.h>
|
|
+#include <linux/rtc.h>
|
|
+#include <linux/types.h>
|
|
+#include <linux/reboot.h>
|
|
+
|
|
+#include "rtc-sunxi-v2.h"
|
|
+
|
|
+#define DAYS_PER_YEAR 365
|
|
+#define DAYS_PER_LEAP_YEAR 366
|
|
+
|
|
+static void __iomem *global_pgregbase;
|
|
+static void __iomem *global_pbase;
|
|
+
|
|
+static struct sunxi_rtc_data_year data_year_param[] = {
|
|
+ [0] = {
|
|
+ .min = 2010,
|
|
+ .max = 2073,
|
|
+ .mask = 0x3f,
|
|
+ .yshift = 16,
|
|
+ .leap_shift = 22,
|
|
+ }
|
|
+};
|
|
+
|
|
+static inline int rtc_write(unsigned int reg, unsigned int val)
|
|
+{
|
|
+ unsigned int reg_val = 0, check = 0;
|
|
+
|
|
+ reg_val = readl(global_pbase + SUNXI_RTC_SPI_CFG_REG);
|
|
+ if ((reg_val & (1 << SUNXI_RTC_SPI_OP_BUSY)) == 1) {
|
|
+ pr_err("RTC access failed(%d).(Rtc spi interface is busy.)\n",
|
|
+ __LINE__);
|
|
+ return -EBUSY;
|
|
+ }
|
|
+ reg_val = 0;
|
|
+ reg_val |= (val << SUNXI_RTC_REG_ACCESS_WRITE_VAL);
|
|
+ reg_val |= (reg << SUNXI_RTC_REG_ACCESS_ADDR);
|
|
+ reg_val |= (1 << SUNXI_RTC_SPI_CFG_BUSY);
|
|
+ reg_val |= (1 << SUNXI_RTC_REG_ACCESS_WAY);
|
|
+ writel(reg_val, global_pbase + SUNXI_RTC_SPI_CFG_REG);
|
|
+ msleep(200);
|
|
+
|
|
+ do {
|
|
+ if (check++ > 10) {
|
|
+ pr_err("RTC access failed(%d).(Rtc spi interface write failed.)\n",
|
|
+ __LINE__);
|
|
+ break;
|
|
+ }
|
|
+ msleep(200);
|
|
+ reg_val = readl(global_pbase + SUNXI_RTC_SPI_CFG_REG);
|
|
+ } while ((reg_val & (1 << SUNXI_RTC_SPI_OP_BUSY)) == 1);
|
|
+
|
|
+ if ((reg_val & (1 << SUNXI_RTC_SPI_OP_BUSY)) == 1)
|
|
+ return -EBUSY;
|
|
+ else
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+static inline int rtc_read(unsigned int reg)
|
|
+{
|
|
+ unsigned int reg_val = 0, check = 0;
|
|
+
|
|
+ reg_val = readl(global_pbase + SUNXI_RTC_SPI_CFG_REG);
|
|
+
|
|
+ if ((reg_val & (1 << SUNXI_RTC_SPI_OP_BUSY)) == 1) {
|
|
+ pr_err("RTC access failed(%d).(Rtc spi interface is busy.)\n",
|
|
+ __LINE__);
|
|
+ return -EBUSY;
|
|
+ }
|
|
+
|
|
+ reg_val = 0;
|
|
+ reg_val |= (reg << SUNXI_RTC_REG_ACCESS_ADDR);
|
|
+ reg_val |= (0 << SUNXI_RTC_REG_ACCESS_WAY);
|
|
+ reg_val |= (1 << SUNXI_RTC_SPI_CFG_BUSY);
|
|
+ writel(reg_val, global_pbase + SUNXI_RTC_SPI_CFG_REG);
|
|
+ msleep(200);
|
|
+
|
|
+ do {
|
|
+ if (check++ > 10) {
|
|
+ pr_err("RTC access failed(%d).(Rtc spi interface read failed.)\n",
|
|
+ __LINE__);
|
|
+ break;
|
|
+ }
|
|
+ msleep(200);
|
|
+ reg_val = readl(global_pbase + SUNXI_RTC_SPI_CFG_REG);
|
|
+ } while ((reg_val & (1 << SUNXI_RTC_SPI_OP_BUSY)) == 1);
|
|
+
|
|
+ if ((reg_val & (1 << SUNXI_RTC_SPI_OP_BUSY)) == 1)
|
|
+ return -EBUSY;
|
|
+ else
|
|
+ return (reg_val & (0xFF << SUNXI_RTC_REG_ACCESS_READ_VAL));
|
|
+}
|
|
+
|
|
+static time64_t sunxi_rtc_time64_offset(unsigned int cur)
|
|
+{
|
|
+ struct rtc_time rtc_tm;
|
|
+
|
|
+ rtc_tm.tm_sec = 0;
|
|
+ rtc_tm.tm_min = 0;
|
|
+ rtc_tm.tm_hour = 0;
|
|
+ rtc_tm.tm_mon = 0;
|
|
+ rtc_tm.tm_mday = 1;
|
|
+ rtc_tm.tm_year = cur - 1900;
|
|
+
|
|
+ return rtc_tm_to_time64(&rtc_tm);
|
|
+}
|
|
+
|
|
+#ifdef CONFIG_RTC_SHUTDOWN_ALARM
|
|
+static int alarm_in_booting;
|
|
+module_param_named(alarm_in_booting, alarm_in_booting, int, 0644);
|
|
+
|
|
+static void sunxi_rtc_alarm_in_boot(struct sunxi_rtc_dev *rtc)
|
|
+{
|
|
+ unsigned int irq_en, en, int_ctrl, int_stat;
|
|
+
|
|
+ /*
|
|
+ * when alarm irq occur at boot0~rtc_driver.probe() process in shutdown
|
|
+ * charger mode, /charger in userspace must know this irq through sysfs
|
|
+ * node 'alarm_in_booting' to reboot and startup system.
|
|
+ */
|
|
+
|
|
+ int_ctrl = rtc_read(SUNXI_ALM0_ENABLE_REG);
|
|
+ int_stat = rtc_read(SUNXI_ALM0_IRQ_STA);
|
|
+
|
|
+ en = int_ctrl & SUNXI_ALM0_ENABLE;
|
|
+ irq_en = (int_ctrl & SUNXI_ALM0_IRQ_ENABLE) >> 1;
|
|
+
|
|
+ if (int_stat && irq_en && en)
|
|
+ alarm_in_booting = 1;
|
|
+}
|
|
+#endif
|
|
+
|
|
+static irqreturn_t sunxi_rtc_alarmirq(int irq, void *id)
|
|
+{
|
|
+ struct sunxi_rtc_dev *chip = (struct sunxi_rtc_dev *) id;
|
|
+ unsigned int val;
|
|
+
|
|
+ val = rtc_read(SUNXI_ALM0_IRQ_STA);
|
|
+
|
|
+ if (val & (1 << SUNXI_ALM0_IRQ_PEND)) {
|
|
+ val |= SUNXI_ALM0_IRQ_PEND;
|
|
+ rtc_write(SUNXI_ALM0_IRQ_STA, val);
|
|
+
|
|
+ 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)
|
|
+{
|
|
+ unsigned int alrm_val = 0;
|
|
+
|
|
+ if (to) {
|
|
+ alrm_val = rtc_read(SUNXI_ALM0_ENABLE_REG);
|
|
+ alrm_val |= SUNXI_ALM0_IRQ_ENABLE|SUNXI_ALM0_ENABLE;
|
|
+ } else {
|
|
+ rtc_write(SUNXI_ALM0_IRQ_STA, 1);
|
|
+ }
|
|
+
|
|
+ rtc_write(SUNXI_ALM0_ENABLE_REG, alrm_val);
|
|
+}
|
|
+
|
|
+static int sunxi_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm);
|
|
+
|
|
+static int sunxi_rtc_getalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
|
|
+{
|
|
+ int ret;
|
|
+ struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
|
|
+ struct rtc_time *alrm_tm = &wkalrm->time;
|
|
+ struct rtc_time hw_alrm_tm;
|
|
+ unsigned int alrm_en, hw_alarm_days = 0;
|
|
+ unsigned long alarm_seconds = 0, cur_seconds = 0;
|
|
+ time64_t seconds = 0;
|
|
+
|
|
+ hw_alarm_days = (rtc_read(SUNXI_ALMCFG_DH) << 8) |
|
|
+ rtc_read(SUNXI_ALMCFG_DL);
|
|
+
|
|
+ seconds = hw_alarm_days * 24 * 3600
|
|
+ + sunxi_rtc_time64_offset(chip->data_year->min);
|
|
+ rtc_time64_to_tm(seconds, &hw_alrm_tm);
|
|
+
|
|
+ hw_alrm_tm.tm_hour = rtc_read(SUNXI_ALMCFG_HH);
|
|
+ hw_alrm_tm.tm_min = rtc_read(SUNXI_ALMCFG_MM);
|
|
+ hw_alrm_tm.tm_sec = rtc_read(SUNXI_ALMCFG_SS);
|
|
+
|
|
+ ret = sunxi_rtc_gettime(dev, alrm_tm);
|
|
+ if (ret)
|
|
+ return -EINVAL;
|
|
+ rtc_time_to_tm(cur_seconds, alrm_tm);
|
|
+ rtc_time_to_tm(alarm_seconds, &hw_alrm_tm);
|
|
+ hw_alrm_tm.tm_mon -= 1;
|
|
+
|
|
+ dev_dbg(dev, "alarm: %04d-%02d-%02d %02d:%02d:%02d\n",
|
|
+ hw_alrm_tm.tm_year + 1900, hw_alrm_tm.tm_mon + 1,
|
|
+ hw_alrm_tm.tm_mday, hw_alrm_tm.tm_hour,
|
|
+ hw_alrm_tm.tm_min, hw_alrm_tm.tm_sec);
|
|
+
|
|
+ if (cur_seconds > alarm_seconds) {
|
|
+ /* alarm is disabled. */
|
|
+ wkalrm->enabled = 0;
|
|
+ alrm_tm->tm_mon = -1;
|
|
+ alrm_tm->tm_mday = -1;
|
|
+ alrm_tm->tm_year = -1;
|
|
+ alrm_tm->tm_hour = -1;
|
|
+ alrm_tm->tm_min = -1;
|
|
+ alrm_tm->tm_sec = -1;
|
|
+ return 0;
|
|
+ } else
|
|
+ memcpy(alrm_tm, &hw_alrm_tm, sizeof(hw_alrm_tm));
|
|
+
|
|
+ alrm_en = rtc_read(SUNXI_ALM0_ENABLE_REG);
|
|
+ if (alrm_en & SUNXI_ALM0_ENABLE)
|
|
+ wkalrm->enabled = 1;
|
|
+
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+static int sunxi_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
|
|
+{
|
|
+ unsigned int days = 0;
|
|
+ struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
|
|
+ time64_t seconds = 0;
|
|
+ struct rtc_time rtc_days;
|
|
+
|
|
+ /* lock rtc real time */
|
|
+ rtc_write(SUNXI_RTC_TIMER_READ_CFG,
|
|
+ SUNXI_RTC_TIMER_READ_LOCK_BYPASS|SUNXI_RTC_TIMER_READ_LOCK_ENABLE);
|
|
+
|
|
+ /* read rtc time */
|
|
+ rtc_tm->tm_sec = rtc_read(SUNXI_RTC_SS_RD);
|
|
+ rtc_tm->tm_min = rtc_read(SUNXI_RTC_MM_RD);
|
|
+ rtc_tm->tm_hour = rtc_read(SUNXI_RTC_HH_RD);
|
|
+ days = rtc_read(SUNXI_RTC_DL_RD)|(rtc_read(SUNXI_RTC_DH_RD) << 8);
|
|
+ seconds = days * 24 * 3600 +
|
|
+ sunxi_rtc_time64_offset(chip->data_year->min);
|
|
+ rtc_time64_to_tm(seconds, &rtc_days);
|
|
+ rtc_tm->tm_year = rtc_days.tm_year;
|
|
+ rtc_tm->tm_mon = rtc_days.tm_mon - 1;
|
|
+ rtc_tm->tm_mday = rtc_days.tm_mday;
|
|
+
|
|
+ dev_dbg(dev, "Read hardware RTC time %04d-%02d-%02d %02d:%02d:%02d\n",
|
|
+ rtc_tm->tm_year + 1900, rtc_tm->tm_mon + 1, rtc_tm->tm_mday,
|
|
+ rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
|
|
+
|
|
+ return rtc_valid_tm(rtc_tm);
|
|
+}
|
|
+
|
|
+static int sunxi_rtc_setalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
|
|
+{
|
|
+
|
|
+ int ret;
|
|
+ time64_t diff;
|
|
+ unsigned int diff_year = 0, date = 0, year = 0;
|
|
+ struct rtc_time tm_now;
|
|
+
|
|
+ struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
|
|
+ struct rtc_time *alrm_tm = &wkalrm->time;
|
|
+
|
|
+ dev_dbg(dev, "Write hardware RTC time %04d-%02d-%02d %02d:%02d:%02d\n",
|
|
+ alrm_tm->tm_year + 1900, alrm_tm->tm_mon + 1, alrm_tm->tm_mday,
|
|
+ alrm_tm->tm_hour, alrm_tm->tm_min, alrm_tm->tm_sec);
|
|
+
|
|
+ ret = sunxi_rtc_gettime(dev, &tm_now);
|
|
+ if (ret < 0) {
|
|
+ dev_err(dev, "Error in getting time\n");
|
|
+ return -EINVAL;
|
|
+ }
|
|
+
|
|
+ diff = rtc_tm_sub(alrm_tm, &tm_now);
|
|
+ if (diff <= 0) {
|
|
+ dev_err(dev, "Date to set in the past\n");
|
|
+ return -EINVAL;
|
|
+ }
|
|
+
|
|
+ if (diff > 255 * SEC_IN_DAY) {
|
|
+ dev_err(dev, "Day must be in the range 0 - 255\n");
|
|
+ return -EINVAL;
|
|
+ }
|
|
+
|
|
+ alrm_tm->tm_year -= SUNXI_YEAR_OFF(chip->data_year);
|
|
+ alrm_tm->tm_mon += 1;
|
|
+
|
|
+ year = alrm_tm->tm_year + 1900;
|
|
+
|
|
+ for (diff_year = chip->data_year->min; diff_year < year; diff_year++) {
|
|
+ if (is_leap_year(diff_year))
|
|
+ date += DAYS_PER_LEAP_YEAR;
|
|
+ else
|
|
+ date += DAYS_PER_YEAR;
|
|
+ }
|
|
+ date += rtc_year_days(alrm_tm->tm_mday, alrm_tm->tm_mon, year);
|
|
+
|
|
+ sunxi_rtc_setaie(0, chip);
|
|
+
|
|
+ rtc_write(SUNXI_ALMCFG_DL, date & 0xFF);
|
|
+ rtc_write(SUNXI_ALMCFG_DH, date >> 8);
|
|
+ rtc_write(SUNXI_ALMCFG_HH, alrm_tm->tm_hour);
|
|
+ rtc_write(SUNXI_ALMCFG_MM, alrm_tm->tm_min);
|
|
+ rtc_write(SUNXI_ALMCFG_SS, alrm_tm->tm_sec);
|
|
+
|
|
+ sunxi_rtc_setaie(wkalrm->enabled, chip);
|
|
+
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+static int sunxi_rtc_settime(struct device *dev, struct rtc_time *rtc_tm)
|
|
+{
|
|
+ struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
|
|
+ unsigned int days = 0, year = 0, diff_year = 0;
|
|
+
|
|
+ /*
|
|
+ * the input rtc_tm->tm_year is the offset relative to 1900. We use
|
|
+ * the SUNXI_YEAR_OFF macro to rebase it with respect to the min year
|
|
+ * allowed by the hardware
|
|
+ */
|
|
+
|
|
+ year = rtc_tm->tm_year + 1900;
|
|
+ if (rtc_valid_tm(rtc_tm) || year < chip->data_year->min
|
|
+ || year > chip->data_year->max) {
|
|
+ dev_err(dev, "rtc only supports year in range %d - %d\n",
|
|
+ chip->data_year->min, chip->data_year->max);
|
|
+ return -EINVAL;
|
|
+ }
|
|
+
|
|
+ rtc_tm->tm_year -= SUNXI_YEAR_OFF(chip->data_year);
|
|
+ rtc_tm->tm_mon += 1;
|
|
+
|
|
+ dev_dbg(dev, "Will set hardware RTC time %04d-%02d-%02d %02d:%02d:%02d\n",
|
|
+ rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
|
|
+ rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
|
|
+
|
|
+ for (diff_year = chip->data_year->min; diff_year < year; diff_year++) {
|
|
+ if (is_leap_year(diff_year))
|
|
+ days += DAYS_PER_LEAP_YEAR;
|
|
+ else
|
|
+ days += DAYS_PER_YEAR;
|
|
+ }
|
|
+ days += rtc_year_days(rtc_tm->tm_mday, rtc_tm->tm_mon, year);
|
|
+
|
|
+ /* set rtc time */
|
|
+ rtc_write(SUNXI_RTC_HH_CFG, rtc_tm->tm_hour);
|
|
+ rtc_write(SUNXI_RTC_MM_CFG, rtc_tm->tm_min);
|
|
+ rtc_write(SUNXI_RTC_SS_CFG, rtc_tm->tm_sec);
|
|
+ rtc_write(SUNXI_RTC_DH_CFG, days >> 8);
|
|
+ rtc_write(SUNXI_RTC_DL_CFG, days & 0xFF);
|
|
+
|
|
+ /* lock rtc real time */
|
|
+ rtc_write(SUNXI_RTC_TIMER_READ_CFG,
|
|
+ SUNXI_RTC_TIMER_READ_LOCK_BYPASS|SUNXI_RTC_TIMER_VAL_CFG_ENABLE);
|
|
+ msleep(400);
|
|
+
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+static int sunxi_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
|
|
+{
|
|
+ struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
|
|
+
|
|
+ if (!enabled)
|
|
+ sunxi_rtc_setaie(enabled, chip);
|
|
+
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+static const struct rtc_class_ops sunxi_rtc_ops = {
|
|
+ .read_time = sunxi_rtc_gettime,
|
|
+ .set_time = sunxi_rtc_settime,
|
|
+ .read_alarm = sunxi_rtc_getalarm,
|
|
+ .set_alarm = sunxi_rtc_setalarm,
|
|
+ .alarm_irq_enable = sunxi_rtc_alarm_irq_enable
|
|
+};
|
|
+
|
|
+static const struct of_device_id sunxi_rtc_dt_ids[] = {
|
|
+ {.compatible = "allwinner,sunxi-rtc", .data = &data_year_param[0]},
|
|
+ { /* sentinel */ },
|
|
+};
|
|
+MODULE_DEVICE_TABLE(of, sunxi_rtc_dt_ids);
|
|
+
|
|
+static ssize_t sunxi_rtc_min_year_show(struct device *dev,
|
|
+ struct device_attribute *attr, char *buf)
|
|
+{
|
|
+ struct platform_device *pdev = container_of(dev, struct platform_device, dev);
|
|
+ struct sunxi_rtc_dev *rtc_dev = platform_get_drvdata(pdev);
|
|
+
|
|
+ return snprintf(buf, PAGE_SIZE, "%u\n", rtc_dev->data_year->min);
|
|
+}
|
|
+static struct device_attribute sunxi_rtc_min_year_attr =
|
|
+ __ATTR(min_year, 0444, sunxi_rtc_min_year_show, NULL);
|
|
+
|
|
+static ssize_t sunxi_rtc_max_year_show(struct device *dev,
|
|
+ struct device_attribute *attr, char *buf)
|
|
+{
|
|
+ struct platform_device *pdev = container_of(dev, struct platform_device, dev);
|
|
+ struct sunxi_rtc_dev *rtc_dev = platform_get_drvdata(pdev);
|
|
+
|
|
+ return snprintf(buf, PAGE_SIZE, "%u\n", rtc_dev->data_year->max);
|
|
+}
|
|
+static struct device_attribute sunxi_rtc_max_year_attr =
|
|
+ __ATTR(max_year, 0444, sunxi_rtc_max_year_show, NULL);
|
|
+
|
|
+#ifdef CONFIG_SUNXI_BOOTUP_EXTEND
|
|
+static int sunxi_reboot_callback(struct notifier_block *this,
|
|
+ unsigned long code, void *data)
|
|
+{
|
|
+ unsigned int rtc_flag = 0;
|
|
+
|
|
+ if (data == NULL)
|
|
+ return NOTIFY_DONE;
|
|
+
|
|
+ pr_info("sunxi rtc reboot, arg %s\n", (char *)data);
|
|
+
|
|
+ if (!strncmp(data, "debug", sizeof("debug"))) {
|
|
+ rtc_flag = SUNXI_DEBUG_MODE_FLAG;
|
|
+ } else if (!strncmp(data, "efex", sizeof("efex"))) {
|
|
+ rtc_flag = SUNXI_EFEX_CMD_FLAG;
|
|
+ } else if (!strncmp(data, "boot-resignature",
|
|
+ sizeof("boot-resignature"))) {
|
|
+ rtc_flag = SUNXI_BOOT_RESIGNATURE_FLAG;
|
|
+ } else if (!strncmp(data, "recovery", sizeof("recovery"))
|
|
+ || !strncmp(data, "boot-recovery", sizeof("boot-recovery"))) {
|
|
+ rtc_flag = SUNXI_BOOT_RECOVERY_FLAG;
|
|
+ } else if (!strncmp(data, "sysrecovery", sizeof("sysrecovery"))) {
|
|
+ rtc_flag = SUNXI_SYS_RECOVERY_FLAG;
|
|
+ } else if (!strncmp(data, "bootloader", sizeof("bootloader"))) {
|
|
+ rtc_flag = SUNXI_FASTBOOT_FLAG;
|
|
+ } else if (!strncmp(data, "usb-recovery", sizeof("usb-recovery"))) {
|
|
+ rtc_flag = SUNXI_USB_RECOVERY_FLAG;
|
|
+ } else {
|
|
+ pr_warn("unknown reboot arg %s", (char *)data);
|
|
+ return NOTIFY_DONE;
|
|
+ }
|
|
+
|
|
+ /*write the data to reg*/
|
|
+ writel(rtc_flag, global_pgregbase);
|
|
+ return NOTIFY_DONE;
|
|
+}
|
|
+
|
|
+
|
|
+static struct notifier_block sunxi_reboot_notifier = {
|
|
+ .notifier_call = sunxi_reboot_callback,
|
|
+};
|
|
+#endif
|
|
+
|
|
+
|
|
+static int sunxi_rtc_probe(struct platform_device *pdev)
|
|
+{
|
|
+ int ret;
|
|
+ unsigned int reg_val;
|
|
+ struct resource *res;
|
|
+ struct sunxi_rtc_dev *chip;
|
|
+ const struct of_device_id *of_id;
|
|
+
|
|
+#ifdef CONFIG_SUNXI_BOOTUP_EXTEND
|
|
+ unsigned int gpr_offset = 0, gpr_len = 0, gpr_num = 0;
|
|
+#endif
|
|
+ global_pgregbase = NULL;
|
|
+
|
|
+ of_id = of_match_device(sunxi_rtc_dt_ids, &pdev->dev);
|
|
+ if (!of_id) {
|
|
+ dev_err(&pdev->dev, "Unable to setup RTC data\n");
|
|
+ return -ENODEV;
|
|
+ }
|
|
+
|
|
+ chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
|
|
+ if (!chip)
|
|
+ return -ENOMEM;
|
|
+
|
|
+ platform_set_drvdata(pdev, chip);
|
|
+ chip->dev = &pdev->dev;
|
|
+
|
|
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
|
+ chip->base = devm_ioremap_resource(&pdev->dev, res);
|
|
+ if (IS_ERR(chip->base))
|
|
+ return PTR_ERR(chip->base);
|
|
+ global_pbase = chip->base;
|
|
+
|
|
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
|
|
+ chip->prcm_base = devm_ioremap_resource(&pdev->dev, res);
|
|
+ if (IS_ERR(chip->base))
|
|
+ return PTR_ERR(chip->base);
|
|
+
|
|
+ /* Enable the clock/module so that we can access the registers */
|
|
+ pm_runtime_enable(&pdev->dev);
|
|
+ pm_runtime_get_sync(&pdev->dev);
|
|
+
|
|
+ chip->data_year = (struct sunxi_rtc_data_year *) of_id->data;
|
|
+
|
|
+ /* set prcm. enable rtc apb bus clk.*/
|
|
+ reg_val = readl(chip->prcm_base + SUNXI_RTC_BGR_REG);
|
|
+ reg_val |= SUNXI_RTC_GATING_ENABLE;
|
|
+ writel(reg_val, chip->prcm_base + SUNXI_RTC_BGR_REG);
|
|
+
|
|
+ /* set rtc spi clk.*/
|
|
+ reg_val = readl(chip->base + SUNXI_SPI_CLK_CFG_REG);
|
|
+ reg_val &= ~(0x1f << SUNXI_RTC_SPI_CLK_DIV);
|
|
+ reg_val |= (0x13 << SUNXI_RTC_SPI_CLK_DIV);
|
|
+ reg_val |= SUNXI_RTC_SPI_CLK_ENABLE;
|
|
+ writel(reg_val, chip->base + SUNXI_SPI_CLK_CFG_REG);
|
|
+
|
|
+
|
|
+#ifdef CONFIG_RTC_SHUTDOWN_ALARM
|
|
+ sunxi_rtc_alarm_in_boot(chip);
|
|
+#else
|
|
+ /* disable alarm, not generate irq pending, wakeup output */
|
|
+ rtc_write(SUNXI_ALM0_ENABLE_REG, 0);
|
|
+
|
|
+ /* clear alarm irq pending */
|
|
+ rtc_write(SUNXI_ALM0_IRQ_STA, 0);
|
|
+#endif
|
|
+ /*
|
|
+ * select RTC clock source(external 32k),disable auto switch func.
|
|
+ */
|
|
+ reg_val = rtc_read(SUNXI_LOSC_CTRL);
|
|
+ reg_val |= (1 << SUNXI_RTC_CLK_SRC);
|
|
+ reg_val |= SUNXI_EXT_LOSC_ENABLE;
|
|
+ reg_val &= ~SUNXI_LOSC_AUTO_SWT_PEND_ENABLE;
|
|
+ reg_val &= ~SUNXI_LOSC_AUTO_SWT_ENABLE;
|
|
+ reg_val &= ~SUNXI_RC16M_OUT_ENABLE;
|
|
+ rtc_write(SUNXI_LOSC_CTRL, reg_val);
|
|
+
|
|
+ device_init_wakeup(&pdev->dev, 1);
|
|
+
|
|
+ chip->rtc = devm_rtc_device_register(&pdev->dev, "sunxi-rtc",
|
|
+ &sunxi_rtc_ops, THIS_MODULE);
|
|
+ if (IS_ERR(chip->rtc)) {
|
|
+ dev_err(&pdev->dev, "unable to register device\n");
|
|
+ goto fail;
|
|
+ }
|
|
+
|
|
+ chip->irq = platform_get_irq(pdev, 0);
|
|
+ if (chip->irq < 0) {
|
|
+ dev_err(&pdev->dev, "No IRQ resource\n");
|
|
+ goto fail;
|
|
+ }
|
|
+ ret = devm_request_irq(&pdev->dev, chip->irq, sunxi_rtc_alarmirq,
|
|
+ 0, dev_name(&pdev->dev), chip);
|
|
+ if (ret) {
|
|
+ dev_err(&pdev->dev, "Could not request IRQ\n");
|
|
+ goto fail;
|
|
+ }
|
|
+
|
|
+ dev_info(&pdev->dev, "RTC enabled\n");
|
|
+
|
|
+ device_create_file(&pdev->dev, &sunxi_rtc_min_year_attr);
|
|
+ device_create_file(&pdev->dev, &sunxi_rtc_max_year_attr);
|
|
+
|
|
+#ifdef CONFIG_SUNXI_BOOTUP_EXTEND
|
|
+ ret = of_property_read_u32(pdev->dev.of_node,
|
|
+ "gpr_offset", &gpr_offset);
|
|
+ if (ret) {
|
|
+ dev_err(&pdev->dev, "Could not get Gpr offset\n");
|
|
+ goto fail_3;
|
|
+ }
|
|
+
|
|
+ ret = of_property_read_u32(pdev->dev.of_node,
|
|
+ "gpr_len", &gpr_len);
|
|
+ if (ret) {
|
|
+ dev_err(&pdev->dev, "Could not get Gpr len\n");
|
|
+ goto fail_3;
|
|
+ }
|
|
+
|
|
+ ret = of_property_read_u32(pdev->dev.of_node,
|
|
+ "gpr_cur_pos", &gpr_num);
|
|
+ if (ret) {
|
|
+ dev_err(&pdev->dev, "Could not get Gpr reboot cur pos");
|
|
+ goto fail_3;
|
|
+ } else {
|
|
+
|
|
+ if (gpr_num >= gpr_len) {
|
|
+ dev_err(&pdev->dev,
|
|
+ "gpr_cur_pos is out of range!\n");
|
|
+ goto fail_3;
|
|
+ }
|
|
+ /*
|
|
+ * This notification function is for monitoring reboot command
|
|
+ * when the system has been started, the reboot parameter is
|
|
+ * stored in the RTC General Purpose register.
|
|
+ *
|
|
+ * gpr_offset: General Purpose register's offset
|
|
+ * gpr_len: The number of General Purpose registers
|
|
+ * gpr_cur_pos: which to store the parameter in
|
|
+ * General Purpose register
|
|
+ */
|
|
+ ret = register_reboot_notifier(&sunxi_reboot_notifier);
|
|
+ if (ret) {
|
|
+ dev_err(&pdev->dev,
|
|
+ "register reboot notifier error %d\n", ret);
|
|
+ goto fail_3;
|
|
+ }
|
|
+ global_pgregbase = chip->base + gpr_offset + 0x4 * gpr_num;
|
|
+ }
|
|
+
|
|
+ return 0;
|
|
+
|
|
+fail_3:
|
|
+ device_remove_file(&pdev->dev, &sunxi_rtc_min_year_attr);
|
|
+ device_remove_file(&pdev->dev, &sunxi_rtc_max_year_attr);
|
|
+ devm_free_irq(&pdev->dev, chip->irq, chip);
|
|
+#else
|
|
+ return 0;
|
|
+#endif
|
|
+
|
|
+fail:
|
|
+ pm_runtime_put_sync(&pdev->dev);
|
|
+ pm_runtime_disable(&pdev->dev);
|
|
+ return -EIO;
|
|
+}
|
|
+
|
|
+static int sunxi_rtc_remove(struct platform_device *pdev)
|
|
+{
|
|
+ struct sunxi_rtc_dev *chip = platform_get_drvdata(pdev);
|
|
+#ifdef CONFIG_SUNXI_BOOTUP_EXTEND
|
|
+ unregister_reboot_notifier(&sunxi_reboot_notifier);
|
|
+#endif
|
|
+ device_remove_file(&pdev->dev, &sunxi_rtc_min_year_attr);
|
|
+ device_remove_file(&pdev->dev, &sunxi_rtc_max_year_attr);
|
|
+
|
|
+ devm_rtc_device_unregister(chip->dev, chip->rtc);
|
|
+
|
|
+ /* Disable the clock/module */
|
|
+ pm_runtime_put_sync(&pdev->dev);
|
|
+ pm_runtime_disable(&pdev->dev);
|
|
+
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+static void sunxi_rtc_shutdown(struct platform_device *pdev)
|
|
+{
|
|
+}
|
|
+
|
|
+static struct platform_driver sunxi_rtc_driver = {
|
|
+ .probe = sunxi_rtc_probe,
|
|
+ .remove = sunxi_rtc_remove,
|
|
+ .shutdown = sunxi_rtc_shutdown,
|
|
+ .driver = {
|
|
+ .name = "sunxi-rtc",
|
|
+ .of_match_table = sunxi_rtc_dt_ids,
|
|
+ },
|
|
+};
|
|
+
|
|
+module_platform_driver(sunxi_rtc_driver);
|
|
+
|
|
+MODULE_DESCRIPTION("sunxi RTC driver V2");
|
|
+MODULE_AUTHOR("Carlo Caione <carlo.caione@gmail.com>");
|
|
+MODULE_LICENSE("GPL");
|