From patchwork Fri Jan 9 00:56:45 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: dann frazier X-Patchwork-Id: 17450 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from mail-gx0-f56.google.com (mail-gx0-f56.google.com [209.85.217.56]) by ozlabs.org (Postfix) with ESMTP id 21F45DDED5 for ; Fri, 9 Jan 2009 11:56:55 +1100 (EST) Received: by gxk16 with SMTP id 16so11813311gxk.1 for ; Thu, 08 Jan 2009 16:56:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=beta; h=domainkey-signature:received:received:x-sender:x-apparently-to :received:received:received-spf:authentication-results:received :received:received:x-virus-scanned:received:received:received:date :from:to:cc:subject:message-id:mime-version:content-type:user-agent :reply-to:sender:precedence:x-google-loop:mailing-list:list-id :list-post:list-help:list-unsubscribe:x-beenthere-env:x-beenthere; bh=MLPrrBCYuW/cs+aagMXQWFB8pA5T1f8YDGx8PfLRPFg=; b=tIu9rpOO28rV8eNXsRke2CzrCR0ou3DgtXLA0DAgZINlMKzHSGCKn9LwXhADm+/aOr tzCFJD2Q7PbFwpbbqOBzgxOkn/gHCYqd7XKxs2bLjnN+1rCKU6ZF5Yik+4SrfvtXEAn2 ug728PfVzhvwwDOg991XwConIdAAZyoWU6KF0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlegroups.com; s=beta; h=x-sender:x-apparently-to:received-spf:authentication-results :x-virus-scanned:date:from:to:cc:subject:message-id:mime-version :content-type:user-agent:reply-to:sender:precedence:x-google-loop :mailing-list:list-id:list-post:list-help:list-unsubscribe :x-beenthere-env:x-beenthere; b=zKvSgGuAHxcw16AYyUPHTDREb6Vsn5i5IFMkLo1NrNY8aLyaecRIufWKmnTQ64dz+C X48rLahHFOI+Mx/EVdclSqyOExfGgxuBsDnQ0+4dsWXdpgWyZNP5Ubew/H1lVzVvW39G Bahg5JYqi+zXdjYap/5+OptHNkJw/sQUl3lL4= Received: by 10.90.28.12 with SMTP id b12mr2658579agb.15.1231462610223; Thu, 08 Jan 2009 16:56:50 -0800 (PST) Received: by 10.176.15.39 with SMTP id 39gr1784yqo.0; Thu, 08 Jan 2009 16:56:50 -0800 (PST) X-Sender: dannf@ldl.fc.hp.com X-Apparently-To: rtc-linux@googlegroups.com Received: by 10.90.90.4 with SMTP id n4mr1305792agb.3.1231462609599; Thu, 08 Jan 2009 16:56:49 -0800 (PST) Received: from g5t0008.atlanta.hp.com (g5t0008.atlanta.hp.com [15.192.0.45]) by mx.google.com with ESMTP id 22si15096830yxr.10.2009.01.08.16.56.49; Thu, 08 Jan 2009 16:56:49 -0800 (PST) Received-SPF: pass (google.com: domain of dannf@ldl.fc.hp.com designates 15.192.0.45 as permitted sender) client-ip=15.192.0.45; Authentication-Results: mx.google.com; spf=pass (google.com: domain of dannf@ldl.fc.hp.com designates 15.192.0.45 as permitted sender) smtp.mail=dannf@ldl.fc.hp.com Received: from g1t0038.austin.hp.com (g1t0038.austin.hp.com [16.236.32.44]) by g5t0008.atlanta.hp.com (Postfix) with ESMTP id D3B682406D; Fri, 9 Jan 2009 00:56:48 +0000 (UTC) Received: from ldl.fc.hp.com (ldl.fc.hp.com [15.11.146.30]) by g1t0038.austin.hp.com (Postfix) with ESMTP id 84DFA30008; Fri, 9 Jan 2009 00:56:48 +0000 (UTC) Received: from localhost (ldl.fc.hp.com [127.0.0.1]) by ldl.fc.hp.com (Postfix) with ESMTP id 1CCC739C011; Thu, 8 Jan 2009 17:56:48 -0700 (MST) X-Virus-Scanned: Debian amavisd-new at ldl.fc.hp.com Received: from ldl.fc.hp.com ([127.0.0.1]) by localhost (ldl.fc.hp.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id NCSrdddMo4f8; Thu, 8 Jan 2009 17:56:45 -0700 (MST) Received: from krebs (krebs.dannf [10.8.0.7]) by ldl.fc.hp.com (Postfix) with ESMTP id 7141039C008; Thu, 8 Jan 2009 17:56:45 -0700 (MST) Received: by krebs (Postfix, from userid 20800) id 57D6C3B6562; Thu, 8 Jan 2009 17:56:45 -0700 (MST) Date: Thu, 8 Jan 2009 17:56:45 -0700 From: dann frazier To: rtc-linux@googlegroups.com, linux-ia64@vger.kernel.org Cc: linux-kernel@vger.kernel.org Subject: [rtc-linux] [PATCH] add rtc platform driver for EFI Message-ID: <20090109005644.GH32403@ldl.fc.hp.com> Mime-Version: 1.0 User-Agent: Mutt/1.5.18 (2008-05-17) Reply-To: rtc-linux@googlegroups.com Sender: rtc-linux@googlegroups.com Precedence: bulk X-Google-Loop: groups Mailing-List: list rtc-linux@googlegroups.com; contact rtc-linux+owner@googlegroups.com List-Id: List-Post: List-Help: List-Unsubscribe: , X-BeenThere-Env: rtc-linux@googlegroups.com X-BeenThere: rtc-linux@googlegroups.com Munge Stephane Eranian's efirtc.c code into an rtc platform driver Signed-off-by: dann frazier --- arch/ia64/kernel/time.c | 18 +++ drivers/rtc/Kconfig | 10 ++ drivers/rtc/Makefile | 1 + drivers/rtc/rtc-efi.c | 294 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 323 insertions(+), 0 deletions(-) create mode 100644 drivers/rtc/rtc-efi.c diff --git a/arch/ia64/kernel/time.c b/arch/ia64/kernel/time.c index f0ebb34..9ed5ba0 100644 --- a/arch/ia64/kernel/time.c +++ b/arch/ia64/kernel/time.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -405,6 +406,23 @@ static struct irqaction timer_irqaction = { .name = "timer" }; +struct platform_device rtc_efi_dev = { + .name = "rtc-efi", + .id = -1, +}; + +static int __init rtc_init(void) +{ + int ret; + ret = platform_device_register(&rtc_efi_dev); + if (ret < 0) + printk(KERN_ERR "unable to register rtc device...\n"); + + /* not necessarily an error */ + return 0; +} +module_init(rtc_init); + void __init time_init (void) { diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 165a818..37ce842 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -433,6 +433,16 @@ config RTC_DRV_DS1742 This driver can also be built as a module. If so, the module will be called rtc-ds1742. +config RTC_DRV_EFI + tristate "EFI RTC" + depends on IA64 + help + If you say yes here you will get support for the EFI + Real Time Clock. + + This driver can also be built as a module. If so, the module + will be called rtc-efi. + config RTC_DRV_STK17TA8 tristate "Simtek STK17TA8" depends on RTC_CLASS diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index 6e79c91..29db401 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -34,6 +34,7 @@ obj-$(CONFIG_RTC_DRV_DS1553) += rtc-ds1553.o obj-$(CONFIG_RTC_DRV_DS1672) += rtc-ds1672.o obj-$(CONFIG_RTC_DRV_DS1742) += rtc-ds1742.o obj-$(CONFIG_RTC_DRV_DS3234) += rtc-ds3234.o +obj-$(CONFIG_RTC_DRV_EFI) += rtc-efi.o obj-$(CONFIG_RTC_DRV_EP93XX) += rtc-ep93xx.o obj-$(CONFIG_RTC_DRV_FM3130) += rtc-fm3130.o obj-$(CONFIG_RTC_DRV_ISL1208) += rtc-isl1208.o diff --git a/drivers/rtc/rtc-efi.c b/drivers/rtc/rtc-efi.c new file mode 100644 index 0000000..2386da0 --- /dev/null +++ b/drivers/rtc/rtc-efi.c @@ -0,0 +1,294 @@ +/* + * rtc-efi: RTC Class Driver for EFI-based systems + * + * Copyright (C) 2009 Hewlett-Packard Development Company, L.P. + * + * Author: dann frazier + * Based on efirtc.c by Stephane Eranian + * + * 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. + * + */ + +#include +#include +#include +#include + +#include + +#define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT) +/* + * EFI Epoch is 1/1/1998 + */ +#define EFI_RTC_EPOCH 1998 + +#define is_leap(year) \ + ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0)) + +struct efi_rtc { + struct rtc_device *rtc; + spinlock_t lock; +}; + +static const unsigned short int __mon_yday[2][13] = +{ + /* Normal years. */ + { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, + /* Leap years. */ + { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } +}; + +/* + * returns day of the year [0-365] + */ +static inline int +compute_yday(efi_time_t *eft) +{ + /* efi_time_t.month is in the [1-12] so, we need -1 */ + return __mon_yday[is_leap(eft->year)][eft->month-1] + eft->day - 1; +} +/* + * returns day of the week [0-6] 0=Sunday + * + * Don't try to provide a year that's before 1998, please ! + */ +static int +compute_wday(efi_time_t *eft) +{ + int y; + int ndays = 0; + + if (eft->year < 1998) { + printk(KERN_ERR "efirtc: EFI year < 1998, invalid date\n"); + return -1; + } + + for (y = EFI_RTC_EPOCH; y < eft->year; y++) + ndays += 365 + (is_leap(y) ? 1 : 0); + + ndays += compute_yday(eft); + + /* + * 4=1/1/1998 was a Thursday + */ + return (ndays + 4) % 7; +} + +static void +convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft) +{ + + eft->year = wtime->tm_year + 1900; + eft->month = wtime->tm_mon + 1; + eft->day = wtime->tm_mday; + eft->hour = wtime->tm_hour; + eft->minute = wtime->tm_min; + eft->second = wtime->tm_sec; + eft->nanosecond = 0; + eft->daylight = wtime->tm_isdst ? EFI_ISDST : 0; + eft->timezone = EFI_UNSPECIFIED_TIMEZONE; +} + +static void +convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime) +{ + memset(wtime, 0, sizeof(*wtime)); + wtime->tm_sec = eft->second; + wtime->tm_min = eft->minute; + wtime->tm_hour = eft->hour; + wtime->tm_mday = eft->day; + wtime->tm_mon = eft->month - 1; + wtime->tm_year = eft->year - 1900; + + /* day of the week [0-6], Sunday=0 */ + wtime->tm_wday = compute_wday(eft); + + /* day in the year [1-365]*/ + wtime->tm_yday = compute_yday(eft); + + + switch (eft->daylight & EFI_ISDST) { + case EFI_ISDST: + wtime->tm_isdst = 1; + break; + case EFI_TIME_ADJUST_DAYLIGHT: + wtime->tm_isdst = 0; + break; + default: + wtime->tm_isdst = -1; + } +} + +static int efi_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) +{ + struct efi_rtc *p = dev_get_drvdata(dev); + efi_time_t eft; + efi_status_t status; + unsigned long flags; + + lock_kernel(); + + spin_lock_irqsave(&p->lock, flags); + /* + * As of EFI v1.10, this call always returns an unsupported status + */ + status = efi.get_wakeup_time((efi_bool_t *)&wkalrm->enabled, + (efi_bool_t *)&wkalrm->pending, &eft); + spin_unlock_irqrestore(&p->lock, flags); + + unlock_kernel(); + + if (status != EFI_SUCCESS) + return -EINVAL; + + convert_from_efi_time(&eft, &wkalrm->time); + + return 0; +} + +static int efi_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) +{ + struct efi_rtc *p = dev_get_drvdata(dev); + efi_time_t eft; + efi_status_t status; + unsigned long flags; + + convert_to_efi_time(&wkalrm->time, &eft); + + lock_kernel(); + + spin_lock_irqsave(&p->lock, flags); + /* + * XXX Fixme: + * As of EFI 0.92 with the firmware I have on my + * machine this call does not seem to work quite + * right + * + * As of v1.10, this call always returns an unsupported status + */ + status = efi.set_wakeup_time((efi_bool_t)wkalrm->enabled, &eft); + spin_unlock_irqrestore(&p->lock, flags); + + unlock_kernel(); + + printk(KERN_WARNING "write status is %d\n", (int)status); + + return status == EFI_SUCCESS ? 0 : -EINVAL; +} + +static int efi_read_time(struct device *dev, struct rtc_time *tm) +{ + struct efi_rtc *p = dev_get_drvdata(dev); + unsigned long flags; + efi_status_t status; + efi_time_t eft; + efi_time_cap_t cap; + + lock_kernel(); + + spin_lock_irqsave(&p->lock, flags); + status = efi.get_time(&eft, &cap); + spin_unlock_irqrestore(&p->lock, flags); + + unlock_kernel(); + + if (status != EFI_SUCCESS) { + /* should never happen */ + printk(KERN_ERR "efitime: can't read time\n"); + return -EINVAL; + } + + convert_from_efi_time(&eft, tm); + + return 0; +} + +static int efi_set_time(struct device *dev, struct rtc_time *tm) +{ + struct efi_rtc *p = dev_get_drvdata(dev); + unsigned long flags; + efi_status_t status; + efi_time_t eft; + + convert_to_efi_time(tm, &eft); + + lock_kernel(); + + spin_lock_irqsave(&p->lock, flags); + status = efi.set_time(&eft); + spin_unlock_irqrestore(&p->lock, flags); + + unlock_kernel(); + + return status == EFI_SUCCESS ? 0 : -EINVAL; +} + +static const struct rtc_class_ops efi_rtc_ops = { + .read_time = efi_read_time, + .set_time = efi_set_time, + .read_alarm = efi_read_alarm, + .set_alarm = efi_set_alarm, +}; + +static int __devinit efi_rtc_probe(struct platform_device *dev) +{ + struct efi_rtc *p; + + p = kzalloc(sizeof (*p), GFP_KERNEL); + if (!p) + return -ENOMEM; + + spin_lock_init(&p->lock); + + p->rtc = rtc_device_register("rtc-efi", &dev->dev, &efi_rtc_ops, + THIS_MODULE); + if (IS_ERR(p->rtc)) { + int err = PTR_ERR(p->rtc); + kfree(p); + return err; + } + + platform_set_drvdata(dev, p); + + return 0; +} + +static int __devexit efi_rtc_remove(struct platform_device *dev) +{ + struct efi_rtc *p = platform_get_drvdata(dev); + + rtc_device_unregister(p->rtc); + kfree(p); + + return 0; +} + +static struct platform_driver efi_rtc_driver = { + .driver = { + .name = "rtc-efi", + .owner = THIS_MODULE, + }, + .probe = efi_rtc_probe, + .remove = __devexit_p(efi_rtc_remove), +}; + +static int __init efi_rtc_init(void) +{ + return platform_driver_register(&efi_rtc_driver); +} + +static void __exit efi_rtc_exit(void) +{ + platform_driver_unregister(&efi_rtc_driver); +} + +module_init(efi_rtc_init); +module_exit(efi_rtc_exit); + +MODULE_AUTHOR("dann frazier "); +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("EFI RTC driver");