Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/sh/boards/sh03/rtc.c -- CTP/PCI-SH03 on-chip RTC support |
| 3 | * |
| 4 | * Copyright (C) 2004 Saito.K & Jeanne(ksaito@interface.co.jp) |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/time.h> |
Matt Mackall | 4f3a36a | 2006-03-28 01:56:10 -0800 | [diff] [blame] | 12 | #include <linux/bcd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/rtc.h> |
| 14 | #include <linux/spinlock.h> |
Paul Mundt | af514ca | 2006-09-27 17:11:32 +0900 | [diff] [blame] | 15 | #include <asm/io.h> |
| 16 | #include <asm/rtc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | #define RTC_BASE 0xb0000000 |
| 19 | #define RTC_SEC1 (RTC_BASE + 0) |
| 20 | #define RTC_SEC10 (RTC_BASE + 1) |
| 21 | #define RTC_MIN1 (RTC_BASE + 2) |
| 22 | #define RTC_MIN10 (RTC_BASE + 3) |
| 23 | #define RTC_HOU1 (RTC_BASE + 4) |
| 24 | #define RTC_HOU10 (RTC_BASE + 5) |
| 25 | #define RTC_WEE1 (RTC_BASE + 6) |
| 26 | #define RTC_DAY1 (RTC_BASE + 7) |
| 27 | #define RTC_DAY10 (RTC_BASE + 8) |
| 28 | #define RTC_MON1 (RTC_BASE + 9) |
| 29 | #define RTC_MON10 (RTC_BASE + 10) |
| 30 | #define RTC_YEA1 (RTC_BASE + 11) |
| 31 | #define RTC_YEA10 (RTC_BASE + 12) |
| 32 | #define RTC_YEA100 (RTC_BASE + 13) |
| 33 | #define RTC_YEA1000 (RTC_BASE + 14) |
| 34 | #define RTC_CTL (RTC_BASE + 15) |
| 35 | #define RTC_BUSY 1 |
| 36 | #define RTC_STOP 2 |
| 37 | |
Paul Mundt | cd1408f | 2009-05-08 16:57:35 +0900 | [diff] [blame] | 38 | static DEFINE_SPINLOCK(sh03_rtc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | unsigned long get_cmos_time(void) |
| 41 | { |
| 42 | unsigned int year, mon, day, hour, min, sec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Paul Mundt | cd1408f | 2009-05-08 16:57:35 +0900 | [diff] [blame] | 44 | spin_lock(&sh03_rtc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | again: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | do { |
Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 47 | sec = (__raw_readb(RTC_SEC1) & 0xf) + (__raw_readb(RTC_SEC10) & 0x7) * 10; |
| 48 | min = (__raw_readb(RTC_MIN1) & 0xf) + (__raw_readb(RTC_MIN10) & 0xf) * 10; |
| 49 | hour = (__raw_readb(RTC_HOU1) & 0xf) + (__raw_readb(RTC_HOU10) & 0xf) * 10; |
| 50 | day = (__raw_readb(RTC_DAY1) & 0xf) + (__raw_readb(RTC_DAY10) & 0xf) * 10; |
| 51 | mon = (__raw_readb(RTC_MON1) & 0xf) + (__raw_readb(RTC_MON10) & 0xf) * 10; |
| 52 | year = (__raw_readb(RTC_YEA1) & 0xf) + (__raw_readb(RTC_YEA10) & 0xf) * 10 |
| 53 | + (__raw_readb(RTC_YEA100 ) & 0xf) * 100 |
| 54 | + (__raw_readb(RTC_YEA1000) & 0xf) * 1000; |
| 55 | } while (sec != (__raw_readb(RTC_SEC1) & 0xf) + (__raw_readb(RTC_SEC10) & 0x7) * 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | if (year == 0 || mon < 1 || mon > 12 || day > 31 || day < 1 || |
| 57 | hour > 23 || min > 59 || sec > 59) { |
| 58 | printk(KERN_ERR |
| 59 | "SH-03 RTC: invalid value, resetting to 1 Jan 2000\n"); |
| 60 | printk("year=%d, mon=%d, day=%d, hour=%d, min=%d, sec=%d\n", |
| 61 | year, mon, day, hour, min, sec); |
| 62 | |
Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 63 | __raw_writeb(0, RTC_SEC1); __raw_writeb(0, RTC_SEC10); |
| 64 | __raw_writeb(0, RTC_MIN1); __raw_writeb(0, RTC_MIN10); |
| 65 | __raw_writeb(0, RTC_HOU1); __raw_writeb(0, RTC_HOU10); |
| 66 | __raw_writeb(6, RTC_WEE1); |
| 67 | __raw_writeb(1, RTC_DAY1); __raw_writeb(0, RTC_DAY10); |
| 68 | __raw_writeb(1, RTC_MON1); __raw_writeb(0, RTC_MON10); |
| 69 | __raw_writeb(0, RTC_YEA1); __raw_writeb(0, RTC_YEA10); |
| 70 | __raw_writeb(0, RTC_YEA100); |
| 71 | __raw_writeb(2, RTC_YEA1000); |
| 72 | __raw_writeb(0, RTC_CTL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | goto again; |
| 74 | } |
| 75 | |
Paul Mundt | cd1408f | 2009-05-08 16:57:35 +0900 | [diff] [blame] | 76 | spin_unlock(&sh03_rtc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | return mktime(year, mon, day, hour, min, sec); |
| 78 | } |
| 79 | |
| 80 | void sh03_rtc_gettimeofday(struct timespec *tv) |
| 81 | { |
| 82 | |
| 83 | tv->tv_sec = get_cmos_time(); |
| 84 | tv->tv_nsec = 0; |
| 85 | } |
| 86 | |
| 87 | static int set_rtc_mmss(unsigned long nowtime) |
| 88 | { |
| 89 | int retval = 0; |
| 90 | int real_seconds, real_minutes, cmos_minutes; |
| 91 | int i; |
| 92 | |
| 93 | /* gets recalled with irq locally disabled */ |
Paul Mundt | cd1408f | 2009-05-08 16:57:35 +0900 | [diff] [blame] | 94 | spin_lock(&sh03_rtc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | for (i = 0 ; i < 1000000 ; i++) /* may take up to 1 second... */ |
Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 96 | if (!(__raw_readb(RTC_CTL) & RTC_BUSY)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | break; |
Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 98 | cmos_minutes = (__raw_readb(RTC_MIN1) & 0xf) + (__raw_readb(RTC_MIN10) & 0xf) * 10; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | real_seconds = nowtime % 60; |
| 100 | real_minutes = nowtime / 60; |
| 101 | if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1) |
| 102 | real_minutes += 30; /* correct for half hour time zone */ |
| 103 | real_minutes %= 60; |
| 104 | |
| 105 | if (abs(real_minutes - cmos_minutes) < 30) { |
Paul Mundt | 9d56dd3 | 2010-01-26 12:58:40 +0900 | [diff] [blame] | 106 | __raw_writeb(real_seconds % 10, RTC_SEC1); |
| 107 | __raw_writeb(real_seconds / 10, RTC_SEC10); |
| 108 | __raw_writeb(real_minutes % 10, RTC_MIN1); |
| 109 | __raw_writeb(real_minutes / 10, RTC_MIN10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } else { |
Stephen Hemminger | 3e5c124 | 2011-01-12 16:59:31 -0800 | [diff] [blame] | 111 | printk_once(KERN_NOTICE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | "set_rtc_mmss: can't update from %d to %d\n", |
| 113 | cmos_minutes, real_minutes); |
| 114 | retval = -1; |
| 115 | } |
Paul Mundt | cd1408f | 2009-05-08 16:57:35 +0900 | [diff] [blame] | 116 | spin_unlock(&sh03_rtc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | return retval; |
| 119 | } |
| 120 | |
| 121 | int sh03_rtc_settimeofday(const time_t secs) |
| 122 | { |
| 123 | unsigned long nowtime = secs; |
| 124 | |
| 125 | return set_rtc_mmss(nowtime); |
| 126 | } |
| 127 | |
| 128 | void sh03_time_init(void) |
| 129 | { |
Paul Mundt | af514ca | 2006-09-27 17:11:32 +0900 | [diff] [blame] | 130 | rtc_sh_get_time = sh03_rtc_gettimeofday; |
| 131 | rtc_sh_set_time = sh03_rtc_settimeofday; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |