From patchwork Wed Oct 3 13:31:54 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Stanley X-Patchwork-Id: 10624803 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7B5A3112B for ; Wed, 3 Oct 2018 13:34:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 69D2328BA6 for ; Wed, 3 Oct 2018 13:34:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5D3AA28BBE; Wed, 3 Oct 2018 13:34:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id AC5D328BA6 for ; Wed, 3 Oct 2018 13:34:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=d0RgeI3aPc4WOv8g0wwa7XhK5XLCKAMbyvTA/q0EaWE=; b=lgxtgcczeJz6iJXjRNt71FglQC MPqr+OKjbU4Ep0WtLLsS0LfIOPCLie3JihKMqsBA/E5I/EtfkwepndXkdH44PaD/L1sHPCsQuf1VS DWZIT7EpcjiPTL8Cnp+S10fTpHd+kWS74aCfSTuW3l+YnqZWvTFzkgXIS0nLNdV44m9ZDhviKoDws ezy7Wfz22Kh+qh9Aq36coD7+3CzpBtXy500sUw36lPZzQdHbvL1QeuEsgYpYfGVEebfL6E1FTrbMM N1KzLz60XfN+5G1yTWKSH0IYXeNy2rc2DVU7nQlLs6owDSmIPYXxHXm4VY+YkZWqk25NmC9vZNra1 bLu+szKA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1g7hI9-0003As-Op; Wed, 03 Oct 2018 13:34:17 +0000 Received: from mail-ed1-x543.google.com ([2a00:1450:4864:20::543]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1g7hGF-0002IX-Fq for linux-arm-kernel@lists.infradead.org; Wed, 03 Oct 2018 13:33:01 +0000 Received: by mail-ed1-x543.google.com with SMTP id z21-v6so928118edb.11 for ; Wed, 03 Oct 2018 06:32:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=JoZeYtyQ79MOQHmmKRs1FkK/Yz7O5KeZm4argFjAYY0=; b=cMXx+YCQZ5J5RqGjss7KIV7Py4RMpHo8B/X0K0UPsBHA0QKeBDc58pO46L4ItXXUmP e1GmyBq387km/a3396gVPScog0YtRoEukYIyu0P8f2xDuE9F4MAiw9gHIINlAZw66Xal RYktRokK+rI4TwfwKpDIUMVwbYY8Tjc2rSb/kJ5KanlA+fatgIX9ikuW5Jf3YWy0kL4/ jhjh5+PtOEyXbxtNW6K7dCL/3qPNeupQE4udWxEdo27DlFSaJvDvK5TK+kKDupiyjlNr X7rlwfybU/pV1eaCWn/IfDllHvfyu5rNxLSyzeW6EQ8V19ahDh82kFTz9fhpFxAMV6Tw Jd2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=JoZeYtyQ79MOQHmmKRs1FkK/Yz7O5KeZm4argFjAYY0=; b=HVERFVY+8Co4z6sZjHEczLuQZF2K37sX/70IaQbHpu6HzZz87scDo5NkICSQG8HZOR tqHouXg+FRbgl//5ZQDALbDtjM4yqpOnPwPCczzx+Ap7SVI6PYJAt+tWAVwsctBkozkU ziwY9xicOcA1Zsg17kmUvFvzKawWZyypJwL8i+YcfbMwL4bZY+1F3++l6J6ePKqkRS4I 89IROclYcKGnEzxe16+IrWsA+wFFmlKeqAhSfZn92xKVBqWU1eINsW0tWyJZFcg9GAvm qv91hpQHS6N3YwFOKS5IZkYzs+imogs7bkJphRGY+qFjY8mM7gENa99yT7O24D+SsVbZ Z/lQ== X-Gm-Message-State: ABuFfoi/j4zql9EgK0VMQu4zcSbjPSfgkZhGi9gC/SSIhZKfCKjeegRk wQhER9YqZrQ38yHZ2BA2Cgg= X-Google-Smtp-Source: ACcGV63V6goTyGOW8r3VDw46a7sNXr7JUXo8ra5L06AeiZMJ+F5MqZsRIX1qnzyY1shYmpbM1ekLeg== X-Received: by 2002:a17:906:c28f:: with SMTP id r15-v6mr1920450ejz.188.1538573527457; Wed, 03 Oct 2018 06:32:07 -0700 (PDT) Received: from aurora.jms.id.au ([213.244.178.17]) by smtp.gmail.com with ESMTPSA id l34-v6sm547766eda.54.2018.10.03.06.32.05 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 03 Oct 2018 06:32:06 -0700 (PDT) Received: by aurora.jms.id.au (sSMTP sendmail emulation); Wed, 03 Oct 2018 15:32:05 +0200 From: Joel Stanley To: Alessandro Zummo , Alexandre Belloni , linux-rtc@vger.kernel.org Subject: [PATCH 1/2] rtc: Add ASPEED RTC driver Date: Wed, 3 Oct 2018 15:31:54 +0200 Message-Id: <20181003133155.27494-2-joel@jms.id.au> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181003133155.27494-1-joel@jms.id.au> References: <20181003133155.27494-1-joel@jms.id.au> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181003_063219_554690_0ADCC5AF X-CRM114-Status: GOOD ( 21.52 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Andrew Jeffery , linux-aspeed@lists.ozlabs.org, linux-arm-kernel@lists.infradead.org, Christian Svensson MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Read and writes the time to the non-battery backed RTC in the ASPEED AST2400 and AST2500 system on chip. Signed-off-by: Joel Stanley --- drivers/rtc/Kconfig | 10 +++ drivers/rtc/Makefile | 1 + drivers/rtc/rtc-aspeed.c | 142 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 153 insertions(+) create mode 100644 drivers/rtc/rtc-aspeed.c diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index a819ef07b7ec..372944b59b69 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -1783,6 +1783,16 @@ config RTC_DRV_RTD119X If you say yes here, you get support for the RTD1295 SoC Real Time Clock. +config RTC_DRV_ASPEED + tristate "Aspeed RTC" + depends on ARCH_ASPEED || COMPILE_TEST + help + If you say yes here you get support for the ASPEED AST2400 and + AST2500 SoC real time clocks. + + This driver can also be built as a module, if so, the module + will be called "rtc-aspeed". + comment "HID Sensor RTC drivers" config RTC_DRV_HID_SENSOR_TIME diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index 290c1730fb0a..7d924577d133 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -33,6 +33,7 @@ obj-$(CONFIG_RTC_DRV_AC100) += rtc-ac100.o obj-$(CONFIG_RTC_DRV_ARMADA38X) += rtc-armada38x.o obj-$(CONFIG_RTC_DRV_AS3722) += rtc-as3722.o obj-$(CONFIG_RTC_DRV_ASM9260) += rtc-asm9260.o +obj-$(CONFIG_RTC_DRV_ASPEED) += rtc-aspeed.o obj-$(CONFIG_RTC_DRV_AT91RM9200)+= rtc-at91rm9200.o obj-$(CONFIG_RTC_DRV_AT91SAM9) += rtc-at91sam9.o obj-$(CONFIG_RTC_DRV_AU1XXX) += rtc-au1xxx.o diff --git a/drivers/rtc/rtc-aspeed.c b/drivers/rtc/rtc-aspeed.c new file mode 100644 index 000000000000..dce00e594910 --- /dev/null +++ b/drivers/rtc/rtc-aspeed.c @@ -0,0 +1,142 @@ +// SPDX-License-Identifier: GPL-2.0+ +// Copyright 2015 IBM Corp. + +#include +#include +#include +#include +#include + +struct aspeed_rtc { + struct rtc_device *rtc_dev; + void __iomem *base; + spinlock_t lock; +}; + +#define RTC_TIME 0x00 +#define RTC_YEAR 0x04 +#define RTC_CTRL 0x10 + +#define RTC_UNLOCK 0x02 +#define RTC_ENABLE 0x01 + +static int aspeed_rtc_read_time(struct device *dev, struct rtc_time *tm) +{ + struct aspeed_rtc *rtc = dev_get_drvdata(dev); + unsigned int cent, year, mon, day, hour, min, sec; + unsigned long flags; + u32 reg1, reg2; + + spin_lock_irqsave(&rtc->lock, flags); + + do { + reg2 = readl(rtc->base + RTC_YEAR); + reg1 = readl(rtc->base + RTC_TIME); + } while (reg2 != readl(rtc->base + RTC_YEAR)); + + day = (reg1 >> 24) & 0x1f; + hour = (reg1 >> 16) & 0x1f; + min = (reg1 >> 8) & 0x3f; + sec = (reg1 >> 0) & 0x3f; + cent = (reg2 >> 16) & 0x1f; + year = (reg2 >> 8) & 0x7f; + /* + * Month is 1-12 in hardware, and 0-11 in struct rtc_time, however we + * are using mktime64 which is 1-12, so no adjustment is necessary + */ + mon = (reg2 >> 0) & 0x0f; + + rtc_time64_to_tm(mktime64(cent * 100 + year, mon, day, hour, min, sec), + tm); + + spin_unlock_irqrestore(&rtc->lock, flags); + + return 0; +} + +static int aspeed_rtc_set_time(struct device *dev, struct rtc_time *tm) +{ + struct aspeed_rtc *rtc = dev_get_drvdata(dev); + unsigned long flags; + u32 reg1, reg2, ctrl; + int year, cent; + + spin_lock_irqsave(&rtc->lock, flags); + + cent = (tm->tm_year + 1900) / 100; + year = tm->tm_year % 100; + + reg1 = (tm->tm_mday << 24) | (tm->tm_hour << 16) | (tm->tm_min << 8) | + tm->tm_sec; + + /* Hardware is 1-12, convert to 0-11 */ + reg2 = ((cent & 0x1f) << 16) | ((year & 0x7f) << 8) | + ((tm->tm_mon & 0xf) + 1); + + ctrl = readl(rtc->base + RTC_CTRL); + writel(ctrl | RTC_UNLOCK, rtc->base + RTC_CTRL); + + writel(reg1, rtc->base + RTC_TIME); + writel(reg2, rtc->base + RTC_YEAR); + + writel(ctrl, rtc->base + RTC_CTRL); + + spin_unlock_irqrestore(&rtc->lock, flags); + + return 0; +} + +static const struct rtc_class_ops aspeed_rtc_ops = { + .read_time = aspeed_rtc_read_time, + .set_time = aspeed_rtc_set_time, +}; + +static int aspeed_rtc_probe(struct platform_device *pdev) +{ + struct resource *res; + struct aspeed_rtc *rtc; + + rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL); + if (!rtc) + return -ENOMEM; + + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + rtc->base = devm_ioremap_resource(&pdev->dev, res); + if (IS_ERR(rtc->base)) + return PTR_ERR(rtc->base); + + platform_set_drvdata(pdev, rtc); + + rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, pdev->name, + &aspeed_rtc_ops, THIS_MODULE); + + if (IS_ERR(rtc->rtc_dev)) + return PTR_ERR(rtc->rtc_dev); + + spin_lock_init(&rtc->lock); + + /* Enable RTC and clear the unlock bit */ + writel(RTC_ENABLE, rtc->base + RTC_CTRL); + + return 0; +} + +static const struct of_device_id aspeed_rtc_match[] = { + { .compatible = "aspeed,ast2400-rtc", }, + { .compatible = "aspeed,ast2500-rtc", }, + {} +}; +MODULE_DEVICE_TABLE(of, aspeed_rtc_match); + +static struct platform_driver aspeed_rtc_driver = { + .driver = { + .name = "aspeed-rtc", + .of_match_table = of_match_ptr(aspeed_rtc_match), + }, +}; + +module_platform_driver_probe(aspeed_rtc_driver, aspeed_rtc_probe); + +MODULE_DESCRIPTION("Aspeed RTC driver"); +MODULE_AUTHOR("Joel Stanley "); +MODULE_LICENSE("GPL");