sdk-hwV1.3/lichee/linux-4.9/drivers/rtc/rtc-sunxi-v3.c

446 lines
11 KiB
C

/*
* 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 <linux/clk.h>
#include "rtc-sunxi-v3.h"
static const struct rtc_class_ops sunxi_rtc_ops = {
.read_time = NULL,
.set_time = NULL,
.read_alarm = NULL,
.set_alarm = NULL,
.alarm_irq_enable = NULL,
};
static const struct of_device_id sunxi_rtc_dt_ids[] = {
{.compatible = "allwinner,sunxi-rtc"},
{ /* sentinel */ },
};
MODULE_DEVICE_TABLE(of, sunxi_rtc_dt_ids);
#if defined(CONFIG_SUNXI_BOOTUP_EXTEND) || defined(CONFIG_SUNXI_RTC_BOOTCOUNT)
static void __iomem *global_pgregbase;
static int rtc_flag_str2num(const char *rtc_str, unsigned int *rtc_flag_p)
{
int ret = 0;
unsigned int rtc_flag;
if (!strcmp(rtc_str, "debug")) {
rtc_flag = SUNXI_DEBUG_MODE_FLAG;
} else if (!strcmp(rtc_str, "efex")) {
rtc_flag = SUNXI_EFEX_CMD_FLAG;
} else if (!strcmp(rtc_str, "boot-resignature")) {
rtc_flag = SUNXI_BOOT_RESIGNATURE_FLAG;
} else if (!strcmp(rtc_str, "recovery")
|| !strcmp(rtc_str, "boot-recovery")) {
rtc_flag = SUNXI_BOOT_RECOVERY_FLAG;
} else if (!strcmp(rtc_str, "sysrecovery")) {
rtc_flag = SUNXI_SYS_RECOVERY_FLAG;
} else if (!strcmp(rtc_str, "bootloader")) {
rtc_flag = SUNXI_FASTBOOT_FLAG;
} else if (!strcmp(rtc_str, "usb-recovery")) {
rtc_flag = SUNXI_USB_RECOVERY_FLAG;
} else if (!strcmp(rtc_str, "uboot")) {
rtc_flag = SUNXI_UBOOT_FLAG;
} else {
rtc_flag = 0;
ret = -1;
}
*rtc_flag_p = rtc_flag;
return ret;
}
static int sunxi_reboot_callback(struct notifier_block *this,
unsigned long code, void *data)
{
unsigned int rtc_flag = 0;
int ret;
if (data == NULL)
return NOTIFY_DONE;
pr_info("sunxi rtc reboot, arg %s\n", (char *)data);
ret = rtc_flag_str2num((const char *)data, &rtc_flag);
if (ret == -1) {
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,
};
static ssize_t sunxi_rtc_flag_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
unsigned int rtc_flag = 0;
rtc_flag = readl(global_pgregbase);
return sprintf(buf, "0x%x\n", rtc_flag);
}
static ssize_t sunxi_rtc_flag_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
unsigned int rtc_flag = 0;
int ret;
char rtc_str[30] = {0};
if (size >= 30) {
pr_err("parameters too long\n");
return size;
}
snprintf(rtc_str, size, "%s", buf);
ret = rtc_flag_str2num(rtc_str, &rtc_flag);
if (ret == -1)
pr_warn("unknown arg %s", buf);
else
pr_info("store rtc flag 0x%x\n", rtc_flag);
/* write the data to reg */
writel(rtc_flag, global_pgregbase);
return size;
}
static struct device_attribute sunxi_rtc_flag_attr =
__ATTR(flag, 0664, sunxi_rtc_flag_show, sunxi_rtc_flag_store);
#ifdef CONFIG_SUNXI_RTC_BOOTCOUNT
static void __iomem *global_bootcount;
static int rtc_bootcount_str(const char *rtc_str, unsigned int *rtc_bootcount_p)
{
int ret = 0;
unsigned int rtc_bootcount;
if (!strcmp(rtc_str, "0")) {
rtc_bootcount = 0;
} else {
pr_info("usage : input \"0\" to clean bootcount\n");
return -1;
}
*rtc_bootcount_p = rtc_bootcount;
return ret;
}
static ssize_t sunxi_rtc_bootcount_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
unsigned int rtc_bootcount = 0;
rtc_bootcount = readl(global_bootcount);
return sprintf(buf, "0x%08x\n", rtc_bootcount);
}
static ssize_t sunxi_rtc_bootcount_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
unsigned int rtc_bootcount = 0;
int ret;
char rtc_str[30] = {0};
if (size >= 30) {
pr_err("parameters too long\n");
return size;
}
snprintf(rtc_str, size, "%s", buf);
ret = rtc_bootcount_str(rtc_str, &rtc_bootcount);
if (ret == -1) {
pr_warn("unknown arg %s\n", buf);
return -1;
} else {
pr_info("store rtc bootcount arg %s\n", rtc_str);
/* write the data to reg */
writel(rtc_bootcount, global_bootcount);
}
return size;
}
static struct device_attribute sunxi_rtc_bootcount_attr =
__ATTR(bootcount, 0664, sunxi_rtc_bootcount_show, sunxi_rtc_bootcount_store);
#endif
#endif
static inline int sunxi_rtc_clk_config(struct platform_device *pdev)
{
struct sunxi_rtc_dev *chip = platform_get_drvdata(pdev);
void __iomem *addr = NULL;
unsigned int data = 0;
//Use internal 24MHz-divided clock
addr = chip->base + SUNXI_LOSC_CTRL_REG;
data = readl(addr);
data &= ~BIT(14);
writel(data, addr);
data = readl(addr);
data |= (BIT(1) | LOSC_CTRL_KEY_FIELD_MAGIC);
writel(data, addr);
data = readl(addr);
data |= (BIT(0) | LOSC_CTRL_KEY_FIELD_MAGIC);
writel(data, addr);
//Enable LOSC output gating
addr = chip->base + SUNXI_LOSC_OUT_GATING_REG;
data = readl(addr);
data |= BIT(0);
writel(data, addr);
return 0;
}
static int sunxi_rtc_probe(struct platform_device *pdev)
{
struct sunxi_rtc_dev *chip;
struct resource *res;
const struct of_device_id *of_id;
struct clk *clk = NULL;
int clk_is_out = 0;
int ret = 0;
#if defined(CONFIG_SUNXI_BOOTUP_EXTEND) || defined(CONFIG_SUNXI_RTC_BOOTCOUNT)
u32 gpr_offset = 0;
u32 gpr_len = 0;
#ifdef CONFIG_SUNXI_BOOTUP_EXTEND
u32 gpr_cur_pos = 0;
global_pgregbase = NULL;
#endif
#ifdef CONFIG_SUNXI_RTC_BOOTCOUNT
u32 gpr_bootcount_pos = 0;
global_bootcount = NULL;
#endif
#endif
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);
/* 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;
//Write CCU registers, to enable internal 24MHz-calibrated clock
clk = devm_clk_get(&pdev->dev, NULL);
if (IS_ERR(clk)) {
ret = PTR_ERR(clk);
goto fail1;
}
ret = clk_prepare_enable(clk);
if (ret)
goto fail1;
of_property_read_u32(pdev->dev.of_node,
"clk32k_out", &clk_is_out);
if (clk_is_out) {
//After writing CCU registers, write RTC clock registers
sunxi_rtc_clk_config(pdev);
}
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 fail2;
}
dev_info(&pdev->dev, "RTC enabled\n");
#if defined(CONFIG_SUNXI_BOOTUP_EXTEND) || defined(CONFIG_SUNXI_RTC_BOOTCOUNT)
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 fail2;
}
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 fail2;
}
#ifdef CONFIG_SUNXI_BOOTUP_EXTEND
ret = of_property_read_u32(pdev->dev.of_node,
"gpr_cur_pos", &gpr_cur_pos);
if (ret) {
dev_err(&pdev->dev, "Could not get Gpr reboot cur pos");
goto fail2;
}
if (gpr_cur_pos >= gpr_len) {
dev_err(&pdev->dev,
"gpr_cur_pos is out of range!\n");
goto fail2;
}
global_pgregbase = chip->base + gpr_offset + 0x4 * gpr_cur_pos;
/*
* 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 fail2;
}
/*
* Export sunxi rtc flag to sysfs:
* To support "reboot efex" with RTC, we need to store a flag
* in RTC before reboot. Android's reboot can pass parameters to
* kernel but busybox's reboot can not do that.
* Here we export rtc flag to sysfs, so the flowing command can
* be used as an alternative of "reboot efex":
* echo "efex" > "/sys/devices/platform/soc/rtc/flag"; reboot
*/
ret = device_create_file(&pdev->dev, &sunxi_rtc_flag_attr);
if (ret) {
dev_err(&pdev->dev, "device_create_file failed\n");
goto fail2;
}
#endif
#ifdef CONFIG_SUNXI_RTC_BOOTCOUNT
ret = of_property_read_u32(pdev->dev.of_node,
"gpr_bootcount_pos", &gpr_bootcount_pos);
if (ret) {
dev_err(&pdev->dev, "Could not get Gpr bootcount");
goto fail2;
}
if (gpr_bootcount_pos >= gpr_len) {
dev_err(&pdev->dev,
"gpr_bootcount_pos is out of range!\n");
goto fail2;
}
global_bootcount = chip->base + gpr_offset + 0x4 * gpr_bootcount_pos;
ret = device_create_file(&pdev->dev, &sunxi_rtc_bootcount_attr);
if (ret) {
dev_err(&pdev->dev, "device_create_file failed\n");
goto fail2;
}
#endif
#endif //CONFIG_SUNXI_BOOTUP_EXTEND
return 0;
fail2:
clk_disable_unprepare(clk);
fail1:
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
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)
{
return;
}
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");
MODULE_AUTHOR("Carlo Caione <carlo.caione@gmail.com>");
MODULE_LICENSE("GPL");