From patchwork Mon Mar 25 13:31:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Stanley X-Patchwork-Id: 10869139 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 92C12139A for ; Mon, 25 Mar 2019 13:32:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7870928635 for ; Mon, 25 Mar 2019 13:32:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6B460286EB; Mon, 25 Mar 2019 13:32:53 +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=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED 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 D49D228635 for ; Mon, 25 Mar 2019 13:32:52 +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:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version: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=CT4K71o46D1/GtoGogJd2R+wARAStUluAejFLbQ8njE=; b=QyMEuxK3jZbA0Z iyDbKJxQR8VL5IqJ/5Smji+CIviiJRABpYqJwUMJxMLPMDgtwJy3UN6d+rpH1nl9tSDUIHnCDd5EO qTFIN7OAGzHxaJnG2MgLytQs8F27DLbnU0tBqrN2ipC1HbnO+BEEEPniSZWJAvEPg3J0N4pNuUSN+ vAaTeURHruCfx7VbAK5Xl0+dOgzcSjRAsr7BZYJanhHx1DKIb34Wd490aav8AoyEGIbhvXczr/tyC PNZGK+2Teo2JFdjotoDFcQCaSXbWzYFAvrIIFEyY/8gULvK6Jlq4Wxt0X1S7j8aT6BuwFOY2JVCcV jIxQCymJ8wof0bhrcFRw==; 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 1h8PiX-0004xq-Vj; Mon, 25 Mar 2019 13:32:45 +0000 Received: from mail-pf1-x444.google.com ([2607:f8b0:4864:20::444]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1h8PiJ-0004j9-A4 for linux-arm-kernel@lists.infradead.org; Mon, 25 Mar 2019 13:32:40 +0000 Received: by mail-pf1-x444.google.com with SMTP id 188so996257pfd.8 for ; Mon, 25 Mar 2019 06:32:31 -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 :mime-version:content-transfer-encoding; bh=ZNMynFsYBw4bJOkRxzZKAehBFlMsAcqfPwZnt4LHr6Y=; b=Tkn4zj8xOHgJLuaEbb8YRTPlsm7DpDp0dRUqUw2uFQaqihw44lfAPUcbw8Q++btstd kAqqVEP5d9cEEOyEOdeBQreb4sUh6xCU7Od2IZbnlpCMKgcL0xHDE1GDT22WnhRBfuJy cMzpnJjo8GtlrejLgOwfvvrC37HDuuaDZY23FM7t1A2GByWfzM1dnnzW0hubcIn+jMEJ EsVFPahDyTRsMaCLKPoF1U/hVnIykOjjDwuzjEfbrOeeAWYm3dKiUuY/Dd9qDcTpqZWn UWhexXpFKA3bCwKv7Ah50TE0W/NpYWjBgvYxgt9+cK+0jB1JbtRlYVYsrN8eItUyDZe/ P8RQ== 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:mime-version:content-transfer-encoding; bh=ZNMynFsYBw4bJOkRxzZKAehBFlMsAcqfPwZnt4LHr6Y=; b=V/6ZKSTrqMAZqC2bQKNrH2lTAzZuk5RWxfR7ZZf1BqQeGUG9mcdtVUYeFRPU83P3cq RlN+438Xrj0/IBe1roSQCAGTUpk1GY32X7FTEEV2Cg4tw88GGG8YVspAQkBeWHp1bV9A zAi9INYXZMyhyvMfo3eEd5CBrMotp4/BCTVWO5tqngD8hsZswMkFHjk6Dy/zbtHT7R+U V2MYCI2ajfdlKQpzwKxydRHO1/4iHtkil9w3sn7JBRnACFgXWBlgvmGnL8fFJq4TSV5b CQicZMFhu9HQA34mgArQXBzfH9nKRvY+tPN7FUmnluMSMtF7qtiBYOkB/UTV67Y8KvL5 tz2g== X-Gm-Message-State: APjAAAUXx21yD923fPbZN8ZT6mzsD/SH2tACGqBPDWgIWV8ZsNlQtVZS tVZneCKUNHm4dFullPibJnI= X-Google-Smtp-Source: APXvYqxWmgl5NSWVyfiMFj1K6VYnIt5doHiSXnXRCtJB3R+x+8vSDkZ72cgv4gSxLujBuHtn6Nzr4w== X-Received: by 2002:a63:28c:: with SMTP id 134mr18182510pgc.278.1553520750567; Mon, 25 Mar 2019 06:32:30 -0700 (PDT) Received: from voyager.jms.id.au ([45.124.203.18]) by smtp.gmail.com with ESMTPSA id i64sm23498107pfb.112.2019.03.25.06.32.25 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 25 Mar 2019 06:32:29 -0700 (PDT) Received: by voyager.jms.id.au (sSMTP sendmail emulation); Tue, 26 Mar 2019 00:02:22 +1030 From: Joel Stanley To: Alexandre Belloni , Alessandro Zummo , Rob Herring , Mark Rutland Subject: [PATCH v2 2/2] rtc: Add ASPEED RTC driver Date: Tue, 26 Mar 2019 00:01:59 +1030 Message-Id: <20190325133159.10653-3-joel@jms.id.au> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190325133159.10653-1-joel@jms.id.au> References: <20190325133159.10653-1-joel@jms.id.au> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190325_063232_036713_2E6E6D41 X-CRM114-Status: GOOD ( 20.97 ) 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-rtc@vger.kernel.org, linux-aspeed@lists.ozlabs.org, linux-arm-kernel@lists.infradead.org, devicetree@vger.kernel.org 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 BMC system on chip families. Signed-off-by: Joel Stanley --- v2: Address review from Alexandre - Use devm_rtc_allocate_device - Fill in range_min - Direcly fill in tm struct - Return EINVAL when RTC is not enabled v1: https://lore.kernel.org/linux-arm-kernel/20181003133155.27494-2-joel@jms.id.au/ Signed-off-by: Joel Stanley --- drivers/rtc/Kconfig | 10 +++ drivers/rtc/Makefile | 1 + drivers/rtc/rtc-aspeed.c | 152 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 drivers/rtc/rtc-aspeed.c diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index dc0d66e80038..2d0482bc468b 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -1841,6 +1841,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 fe3962496685..9d997faa2c26 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -34,6 +34,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..9ddb3121b30d --- /dev/null +++ b/drivers/rtc/rtc-aspeed.c @@ -0,0 +1,152 @@ +// 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 BIT(1) +#define RTC_ENABLE BIT(0) + +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; + unsigned long flags; + u32 reg1, reg2; + + spin_lock_irqsave(&rtc->lock, flags); + + if (!(readl(rtc->base + RTC_CTRL) & RTC_ENABLE)) { + spin_unlock_irqrestore(&rtc->lock, flags); + dev_dbg(dev, "%s failing as rtc disabled\n", __func__); + return -EINVAL; + } + + do { + reg2 = readl(rtc->base + RTC_YEAR); + reg1 = readl(rtc->base + RTC_TIME); + } while (reg2 != readl(rtc->base + RTC_YEAR)); + + tm->tm_mday = (reg1 >> 24) & 0x1f; + tm->tm_hour = (reg1 >> 16) & 0x1f; + tm->tm_min = (reg1 >> 8) & 0x3f; + tm->tm_sec = (reg1 >> 0) & 0x3f; + + cent = (reg2 >> 16) & 0x1f; + year = (reg2 >> 8) & 0x7f; + tm->tm_mon = ((reg2 >> 0) & 0x0f) - 1; + tm->tm_year = year + (cent * 100) - 1900; + + dev_dbg(dev, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", __func__, + 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday, + tm->tm_hour, tm->tm_min, tm->tm_sec); + + 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; + + 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; + + reg2 = ((cent & 0x1f) << 16) | ((year & 0x7f) << 8) | + ((tm->tm_mon + 1) & 0xf); + + spin_lock_irqsave(&rtc->lock, flags); + + 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); + + /* Re-lock and ensure enable is set now that a time is programmed */ + writel(ctrl | RTC_ENABLE, 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 aspeed_rtc *rtc; + struct resource *res; + int ret; + + 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); + + rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev); + if (IS_ERR(rtc->rtc_dev)) + return PTR_ERR(rtc->rtc_dev); + + platform_set_drvdata(pdev, rtc); + + rtc->rtc_dev->ops = &aspeed_rtc_ops; + rtc->rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_1900; + rtc->rtc_dev->range_max = 38814989399LL; /* 3199-12-31 23:59:59 */ + + ret = rtc_register_device(rtc->rtc_dev); + if (ret) + return ret; + + spin_lock_init(&rtc->lock); + + return 0; +} + +static const struct of_device_id aspeed_rtc_match[] = { + { .compatible = "aspeed,ast2400-rtc", }, + { .compatible = "aspeed,ast2500-rtc", }, + { .compatible = "aspeed,ast2600-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");