2009-01-06 22:42:22 +00:00
|
|
|
/*
|
|
|
|
* TX4939 internal RTC driver
|
|
|
|
* Based on RBTX49xx patch from CELF patch archive.
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU General Public
|
|
|
|
* License. See the file "COPYING" in the main directory of this archive
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* (C) Copyright TOSHIBA CORPORATION 2005-2007
|
|
|
|
*/
|
|
|
|
#include <linux/rtc.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/interrupt.h>
|
2011-05-27 13:57:25 +00:00
|
|
|
#include <linux/module.h>
|
2009-01-06 22:42:22 +00:00
|
|
|
#include <linux/io.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/gfp.h>
|
2009-01-06 22:42:22 +00:00
|
|
|
#include <asm/txx9/tx4939.h>
|
|
|
|
|
|
|
|
struct tx4939rtc_plat_data {
|
|
|
|
struct rtc_device *rtc;
|
|
|
|
struct tx4939_rtc_reg __iomem *rtcreg;
|
2009-12-16 00:46:00 +00:00
|
|
|
spinlock_t lock;
|
2009-01-06 22:42:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct tx4939rtc_plat_data *get_tx4939rtc_plat_data(struct device *dev)
|
|
|
|
{
|
|
|
|
return platform_get_drvdata(to_platform_device(dev));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tx4939_rtc_cmd(struct tx4939_rtc_reg __iomem *rtcreg, int cmd)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
__raw_writel(cmd, &rtcreg->ctl);
|
|
|
|
/* This might take 30us (next 32.768KHz clock) */
|
|
|
|
while (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_BUSY) {
|
|
|
|
/* timeout on approx. 100us (@ GBUS200MHz) */
|
|
|
|
if (i++ > 200 * 100)
|
|
|
|
return -EBUSY;
|
|
|
|
cpu_relax();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tx4939_rtc_set_mmss(struct device *dev, unsigned long secs)
|
|
|
|
{
|
|
|
|
struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
|
|
|
|
struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
|
|
|
|
int i, ret;
|
|
|
|
unsigned char buf[6];
|
|
|
|
|
|
|
|
buf[0] = 0;
|
|
|
|
buf[1] = 0;
|
|
|
|
buf[2] = secs;
|
|
|
|
buf[3] = secs >> 8;
|
|
|
|
buf[4] = secs >> 16;
|
|
|
|
buf[5] = secs >> 24;
|
2009-12-16 00:46:00 +00:00
|
|
|
spin_lock_irq(&pdata->lock);
|
2009-01-06 22:42:22 +00:00
|
|
|
__raw_writel(0, &rtcreg->adr);
|
|
|
|
for (i = 0; i < 6; i++)
|
|
|
|
__raw_writel(buf[i], &rtcreg->dat);
|
|
|
|
ret = tx4939_rtc_cmd(rtcreg,
|
|
|
|
TX4939_RTCCTL_COMMAND_SETTIME |
|
|
|
|
(__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
|
2009-12-16 00:46:00 +00:00
|
|
|
spin_unlock_irq(&pdata->lock);
|
2009-01-06 22:42:22 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tx4939_rtc_read_time(struct device *dev, struct rtc_time *tm)
|
|
|
|
{
|
|
|
|
struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
|
|
|
|
struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
|
|
|
|
int i, ret;
|
|
|
|
unsigned long sec;
|
|
|
|
unsigned char buf[6];
|
|
|
|
|
2009-12-16 00:46:00 +00:00
|
|
|
spin_lock_irq(&pdata->lock);
|
2009-01-06 22:42:22 +00:00
|
|
|
ret = tx4939_rtc_cmd(rtcreg,
|
|
|
|
TX4939_RTCCTL_COMMAND_GETTIME |
|
|
|
|
(__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
|
|
|
|
if (ret) {
|
2009-12-16 00:46:00 +00:00
|
|
|
spin_unlock_irq(&pdata->lock);
|
2009-01-06 22:42:22 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
__raw_writel(2, &rtcreg->adr);
|
|
|
|
for (i = 2; i < 6; i++)
|
|
|
|
buf[i] = __raw_readl(&rtcreg->dat);
|
2009-12-16 00:46:00 +00:00
|
|
|
spin_unlock_irq(&pdata->lock);
|
2009-01-06 22:42:22 +00:00
|
|
|
sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2];
|
|
|
|
rtc_time_to_tm(sec, tm);
|
|
|
|
return rtc_valid_tm(tm);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tx4939_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
|
|
|
|
{
|
|
|
|
struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
|
|
|
|
struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
|
|
|
|
int i, ret;
|
|
|
|
unsigned long sec;
|
|
|
|
unsigned char buf[6];
|
|
|
|
|
|
|
|
if (alrm->time.tm_sec < 0 ||
|
|
|
|
alrm->time.tm_min < 0 ||
|
|
|
|
alrm->time.tm_hour < 0 ||
|
|
|
|
alrm->time.tm_mday < 0 ||
|
|
|
|
alrm->time.tm_mon < 0 ||
|
|
|
|
alrm->time.tm_year < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
rtc_tm_to_time(&alrm->time, &sec);
|
|
|
|
buf[0] = 0;
|
|
|
|
buf[1] = 0;
|
|
|
|
buf[2] = sec;
|
|
|
|
buf[3] = sec >> 8;
|
|
|
|
buf[4] = sec >> 16;
|
|
|
|
buf[5] = sec >> 24;
|
2009-12-16 00:46:00 +00:00
|
|
|
spin_lock_irq(&pdata->lock);
|
2009-01-06 22:42:22 +00:00
|
|
|
__raw_writel(0, &rtcreg->adr);
|
|
|
|
for (i = 0; i < 6; i++)
|
|
|
|
__raw_writel(buf[i], &rtcreg->dat);
|
|
|
|
ret = tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_SETALARM |
|
|
|
|
(alrm->enabled ? TX4939_RTCCTL_ALME : 0));
|
2009-12-16 00:46:00 +00:00
|
|
|
spin_unlock_irq(&pdata->lock);
|
2009-01-06 22:42:22 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tx4939_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
|
|
|
|
{
|
|
|
|
struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
|
|
|
|
struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
|
|
|
|
int i, ret;
|
|
|
|
unsigned long sec;
|
|
|
|
unsigned char buf[6];
|
|
|
|
u32 ctl;
|
|
|
|
|
2009-12-16 00:46:00 +00:00
|
|
|
spin_lock_irq(&pdata->lock);
|
2009-01-06 22:42:22 +00:00
|
|
|
ret = tx4939_rtc_cmd(rtcreg,
|
|
|
|
TX4939_RTCCTL_COMMAND_GETALARM |
|
|
|
|
(__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
|
|
|
|
if (ret) {
|
2009-12-16 00:46:00 +00:00
|
|
|
spin_unlock_irq(&pdata->lock);
|
2009-01-06 22:42:22 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
__raw_writel(2, &rtcreg->adr);
|
|
|
|
for (i = 2; i < 6; i++)
|
|
|
|
buf[i] = __raw_readl(&rtcreg->dat);
|
|
|
|
ctl = __raw_readl(&rtcreg->ctl);
|
|
|
|
alrm->enabled = (ctl & TX4939_RTCCTL_ALME) ? 1 : 0;
|
|
|
|
alrm->pending = (ctl & TX4939_RTCCTL_ALMD) ? 1 : 0;
|
2009-12-16 00:46:00 +00:00
|
|
|
spin_unlock_irq(&pdata->lock);
|
2009-01-06 22:42:22 +00:00
|
|
|
sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2];
|
|
|
|
rtc_time_to_tm(sec, &alrm->time);
|
|
|
|
return rtc_valid_tm(&alrm->time);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tx4939_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
|
|
|
|
{
|
|
|
|
struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
|
|
|
|
|
2009-12-16 00:46:00 +00:00
|
|
|
spin_lock_irq(&pdata->lock);
|
2009-01-06 22:42:22 +00:00
|
|
|
tx4939_rtc_cmd(pdata->rtcreg,
|
|
|
|
TX4939_RTCCTL_COMMAND_NOP |
|
|
|
|
(enabled ? TX4939_RTCCTL_ALME : 0));
|
2009-12-16 00:46:00 +00:00
|
|
|
spin_unlock_irq(&pdata->lock);
|
2009-01-06 22:42:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t tx4939_rtc_interrupt(int irq, void *dev_id)
|
|
|
|
{
|
|
|
|
struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev_id);
|
|
|
|
struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
|
|
|
|
unsigned long events = RTC_IRQF;
|
|
|
|
|
2009-12-16 00:46:00 +00:00
|
|
|
spin_lock(&pdata->lock);
|
2009-01-06 22:42:22 +00:00
|
|
|
if (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALMD) {
|
|
|
|
events |= RTC_AF;
|
|
|
|
tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_NOP);
|
|
|
|
}
|
2009-12-16 00:46:00 +00:00
|
|
|
spin_unlock(&pdata->lock);
|
2014-04-03 21:50:20 +00:00
|
|
|
rtc_update_irq(pdata->rtc, 1, events);
|
|
|
|
|
2009-01-06 22:42:22 +00:00
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct rtc_class_ops tx4939_rtc_ops = {
|
|
|
|
.read_time = tx4939_rtc_read_time,
|
|
|
|
.read_alarm = tx4939_rtc_read_alarm,
|
|
|
|
.set_alarm = tx4939_rtc_set_alarm,
|
|
|
|
.set_mmss = tx4939_rtc_set_mmss,
|
|
|
|
.alarm_irq_enable = tx4939_rtc_alarm_irq_enable,
|
|
|
|
};
|
|
|
|
|
2010-05-13 01:28:57 +00:00
|
|
|
static ssize_t tx4939_rtc_nvram_read(struct file *filp, struct kobject *kobj,
|
2009-01-06 22:42:22 +00:00
|
|
|
struct bin_attribute *bin_attr,
|
|
|
|
char *buf, loff_t pos, size_t size)
|
|
|
|
{
|
|
|
|
struct device *dev = container_of(kobj, struct device, kobj);
|
|
|
|
struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
|
|
|
|
struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
|
|
|
|
ssize_t count;
|
|
|
|
|
2009-12-16 00:46:00 +00:00
|
|
|
spin_lock_irq(&pdata->lock);
|
2015-07-26 21:48:36 +00:00
|
|
|
for (count = 0; count < size; count++) {
|
2009-01-06 22:42:22 +00:00
|
|
|
__raw_writel(pos++, &rtcreg->adr);
|
|
|
|
*buf++ = __raw_readl(&rtcreg->dat);
|
|
|
|
}
|
2009-12-16 00:46:00 +00:00
|
|
|
spin_unlock_irq(&pdata->lock);
|
2009-01-06 22:42:22 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2010-05-13 01:28:57 +00:00
|
|
|
static ssize_t tx4939_rtc_nvram_write(struct file *filp, struct kobject *kobj,
|
2009-01-06 22:42:22 +00:00
|
|
|
struct bin_attribute *bin_attr,
|
|
|
|
char *buf, loff_t pos, size_t size)
|
|
|
|
{
|
|
|
|
struct device *dev = container_of(kobj, struct device, kobj);
|
|
|
|
struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
|
|
|
|
struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
|
|
|
|
ssize_t count;
|
|
|
|
|
2009-12-16 00:46:00 +00:00
|
|
|
spin_lock_irq(&pdata->lock);
|
2015-07-26 21:48:36 +00:00
|
|
|
for (count = 0; count < size; count++) {
|
2009-01-06 22:42:22 +00:00
|
|
|
__raw_writel(pos++, &rtcreg->adr);
|
|
|
|
__raw_writel(*buf++, &rtcreg->dat);
|
|
|
|
}
|
2009-12-16 00:46:00 +00:00
|
|
|
spin_unlock_irq(&pdata->lock);
|
2009-01-06 22:42:22 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct bin_attribute tx4939_rtc_nvram_attr = {
|
|
|
|
.attr = {
|
|
|
|
.name = "nvram",
|
|
|
|
.mode = S_IRUGO | S_IWUSR,
|
|
|
|
},
|
|
|
|
.size = TX4939_RTC_REG_RAMSIZE,
|
|
|
|
.read = tx4939_rtc_nvram_read,
|
|
|
|
.write = tx4939_rtc_nvram_write,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init tx4939_rtc_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct rtc_device *rtc;
|
|
|
|
struct tx4939rtc_plat_data *pdata;
|
|
|
|
struct resource *res;
|
|
|
|
int irq, ret;
|
|
|
|
|
|
|
|
irq = platform_get_irq(pdev, 0);
|
|
|
|
if (irq < 0)
|
|
|
|
return -ENODEV;
|
|
|
|
pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
|
|
|
|
if (!pdata)
|
|
|
|
return -ENOMEM;
|
|
|
|
platform_set_drvdata(pdev, pdata);
|
|
|
|
|
2013-09-11 21:24:27 +00:00
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
|
|
|
pdata->rtcreg = devm_ioremap_resource(&pdev->dev, res);
|
|
|
|
if (IS_ERR(pdata->rtcreg))
|
|
|
|
return PTR_ERR(pdata->rtcreg);
|
2009-01-06 22:42:22 +00:00
|
|
|
|
2009-12-16 00:46:00 +00:00
|
|
|
spin_lock_init(&pdata->lock);
|
2009-01-06 22:42:22 +00:00
|
|
|
tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP);
|
|
|
|
if (devm_request_irq(&pdev->dev, irq, tx4939_rtc_interrupt,
|
2012-03-23 22:02:34 +00:00
|
|
|
0, pdev->name, &pdev->dev) < 0)
|
2009-01-06 22:42:22 +00:00
|
|
|
return -EBUSY;
|
2013-04-29 23:19:18 +00:00
|
|
|
rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
|
2009-01-06 22:42:22 +00:00
|
|
|
&tx4939_rtc_ops, THIS_MODULE);
|
|
|
|
if (IS_ERR(rtc))
|
|
|
|
return PTR_ERR(rtc);
|
|
|
|
pdata->rtc = rtc;
|
|
|
|
ret = sysfs_create_bin_file(&pdev->dev.kobj, &tx4939_rtc_nvram_attr);
|
2013-04-29 23:19:18 +00:00
|
|
|
|
2009-01-06 22:42:22 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __exit tx4939_rtc_remove(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct tx4939rtc_plat_data *pdata = platform_get_drvdata(pdev);
|
|
|
|
|
|
|
|
sysfs_remove_bin_file(&pdev->dev.kobj, &tx4939_rtc_nvram_attr);
|
2009-12-16 00:46:00 +00:00
|
|
|
spin_lock_irq(&pdata->lock);
|
|
|
|
tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP);
|
|
|
|
spin_unlock_irq(&pdata->lock);
|
2009-01-06 22:42:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver tx4939_rtc_driver = {
|
|
|
|
.remove = __exit_p(tx4939_rtc_remove),
|
|
|
|
.driver = {
|
|
|
|
.name = "tx4939rtc",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2013-04-29 23:18:54 +00:00
|
|
|
module_platform_driver_probe(tx4939_rtc_driver, tx4939_rtc_probe);
|
2009-01-06 22:42:22 +00:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
|
|
|
|
MODULE_DESCRIPTION("TX4939 internal RTC driver");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_ALIAS("platform:tx4939rtc");
|