~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/sh/boards/mach-sh03/rtc.c

Version: ~ [ linux-6.2-rc3 ] ~ [ linux-6.1.5 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.87 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.162 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.228 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.269 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.302 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 /*
  3  * linux/arch/sh/boards/sh03/rtc.c -- CTP/PCI-SH03 on-chip RTC support
  4  *
  5  *  Copyright (C) 2004  Saito.K & Jeanne(ksaito@interface.co.jp)
  6  *
  7  */
  8 
  9 #include <linux/init.h>
 10 #include <linux/kernel.h>
 11 #include <linux/sched.h>
 12 #include <linux/time.h>
 13 #include <linux/bcd.h>
 14 #include <linux/rtc.h>
 15 #include <linux/spinlock.h>
 16 #include <linux/io.h>
 17 #include <linux/rtc.h>
 18 #include <linux/platform_device.h>
 19 
 20 #define RTC_BASE        0xb0000000
 21 #define RTC_SEC1        (RTC_BASE + 0)
 22 #define RTC_SEC10       (RTC_BASE + 1)
 23 #define RTC_MIN1        (RTC_BASE + 2)
 24 #define RTC_MIN10       (RTC_BASE + 3)
 25 #define RTC_HOU1        (RTC_BASE + 4)
 26 #define RTC_HOU10       (RTC_BASE + 5)
 27 #define RTC_WEE1        (RTC_BASE + 6)
 28 #define RTC_DAY1        (RTC_BASE + 7)
 29 #define RTC_DAY10       (RTC_BASE + 8)
 30 #define RTC_MON1        (RTC_BASE + 9)
 31 #define RTC_MON10       (RTC_BASE + 10)
 32 #define RTC_YEA1        (RTC_BASE + 11)
 33 #define RTC_YEA10       (RTC_BASE + 12)
 34 #define RTC_YEA100      (RTC_BASE + 13)
 35 #define RTC_YEA1000     (RTC_BASE + 14)
 36 #define RTC_CTL         (RTC_BASE + 15)
 37 #define RTC_BUSY        1
 38 #define RTC_STOP        2
 39 
 40 static DEFINE_SPINLOCK(sh03_rtc_lock);
 41 
 42 static int sh03_rtc_gettimeofday(struct device *dev, struct rtc_time *tm)
 43 {
 44         unsigned int year, mon, day, hour, min, sec;
 45 
 46         spin_lock(&sh03_rtc_lock);
 47  again:
 48         do {
 49                 sec  = (__raw_readb(RTC_SEC1) & 0xf) + (__raw_readb(RTC_SEC10) & 0x7) * 10;
 50                 min  = (__raw_readb(RTC_MIN1) & 0xf) + (__raw_readb(RTC_MIN10) & 0xf) * 10;
 51                 hour = (__raw_readb(RTC_HOU1) & 0xf) + (__raw_readb(RTC_HOU10) & 0xf) * 10;
 52                 day  = (__raw_readb(RTC_DAY1) & 0xf) + (__raw_readb(RTC_DAY10) & 0xf) * 10;
 53                 mon  = (__raw_readb(RTC_MON1) & 0xf) + (__raw_readb(RTC_MON10) & 0xf) * 10;
 54                 year = (__raw_readb(RTC_YEA1) & 0xf) + (__raw_readb(RTC_YEA10) & 0xf) * 10
 55                      + (__raw_readb(RTC_YEA100 ) & 0xf) * 100
 56                      + (__raw_readb(RTC_YEA1000) & 0xf) * 1000;
 57         } while (sec != (__raw_readb(RTC_SEC1) & 0xf) + (__raw_readb(RTC_SEC10) & 0x7) * 10);
 58         if (year == 0 || mon < 1 || mon > 12 || day > 31 || day < 1 ||
 59             hour > 23 || min > 59 || sec > 59) {
 60                 printk(KERN_ERR
 61                        "SH-03 RTC: invalid value, resetting to 1 Jan 2000\n");
 62                 printk("year=%d, mon=%d, day=%d, hour=%d, min=%d, sec=%d\n",
 63                        year, mon, day, hour, min, sec);
 64 
 65                 __raw_writeb(0, RTC_SEC1); __raw_writeb(0, RTC_SEC10);
 66                 __raw_writeb(0, RTC_MIN1); __raw_writeb(0, RTC_MIN10);
 67                 __raw_writeb(0, RTC_HOU1); __raw_writeb(0, RTC_HOU10);
 68                 __raw_writeb(6, RTC_WEE1);
 69                 __raw_writeb(1, RTC_DAY1); __raw_writeb(0, RTC_DAY10);
 70                 __raw_writeb(1, RTC_MON1); __raw_writeb(0, RTC_MON10);
 71                 __raw_writeb(0, RTC_YEA1); __raw_writeb(0, RTC_YEA10);
 72                 __raw_writeb(0, RTC_YEA100);
 73                 __raw_writeb(2, RTC_YEA1000);
 74                 __raw_writeb(0, RTC_CTL);
 75                 goto again;
 76         }
 77 
 78         spin_unlock(&sh03_rtc_lock);
 79 
 80         tm->tm_sec  = sec;
 81         tm->tm_min  = min;
 82         tm->tm_hour = hour;
 83         tm->tm_mday = day;
 84         tm->tm_mon  = mon;
 85         tm->tm_year = year - 1900;
 86 
 87         return 0;
 88 }
 89 
 90 static int set_rtc_mmss(struct rtc_time *tm)
 91 {
 92         int retval = 0;
 93         int real_seconds, real_minutes, cmos_minutes;
 94         int i;
 95 
 96         /* gets recalled with irq locally disabled */
 97         spin_lock(&sh03_rtc_lock);
 98         for (i = 0 ; i < 1000000 ; i++) /* may take up to 1 second... */
 99                 if (!(__raw_readb(RTC_CTL) & RTC_BUSY))
100                         break;
101         cmos_minutes = (__raw_readb(RTC_MIN1) & 0xf) + (__raw_readb(RTC_MIN10) & 0xf) * 10;
102         real_seconds = tm->tm_sec;
103         real_minutes = tm->tm_min;
104         if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
105                 real_minutes += 30;             /* correct for half hour time zone */
106         real_minutes %= 60;
107 
108         if (abs(real_minutes - cmos_minutes) < 30) {
109                 __raw_writeb(real_seconds % 10, RTC_SEC1);
110                 __raw_writeb(real_seconds / 10, RTC_SEC10);
111                 __raw_writeb(real_minutes % 10, RTC_MIN1);
112                 __raw_writeb(real_minutes / 10, RTC_MIN10);
113         } else {
114                 printk_once(KERN_NOTICE
115                        "set_rtc_mmss: can't update from %d to %d\n",
116                        cmos_minutes, real_minutes);
117                 retval = -EINVAL;
118         }
119         spin_unlock(&sh03_rtc_lock);
120 
121         return retval;
122 }
123 
124 int sh03_rtc_settimeofday(struct device *dev, struct rtc_time *tm)
125 {
126         return set_rtc_mmss(tm);
127 }
128 
129 static const struct rtc_class_ops rtc_generic_ops = {
130         .read_time = sh03_rtc_gettimeofday,
131         .set_time = sh03_rtc_settimeofday,
132 };
133 
134 static int __init sh03_time_init(void)
135 {
136         struct platform_device *pdev;
137 
138         pdev = platform_device_register_data(NULL, "rtc-generic", -1,
139                                              &rtc_generic_ops,
140                                              sizeof(rtc_generic_ops));
141 
142         return PTR_ERR_OR_ZERO(pdev);
143 }
144 arch_initcall(sh03_time_init);
145 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp