Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Driver for NEC VR4100 series Real Time Clock unit. |
| 4 | * |
Yoichi Yuasa | ada8e95 | 2009-07-03 00:39:38 +0900 | [diff] [blame] | 5 | * Copyright (C) 2003-2008 Yoichi Yuasa <yuasa@linux-mips.org> |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 6 | */ |
Arnd Bergmann | 076ff65 | 2018-08-24 00:11:19 +0200 | [diff] [blame] | 7 | #include <linux/compat.h> |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 8 | #include <linux/err.h> |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 9 | #include <linux/fs.h> |
| 10 | #include <linux/init.h> |
Sachin Kamat | 4271e7f | 2013-11-12 15:10:34 -0800 | [diff] [blame] | 11 | #include <linux/io.h> |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 12 | #include <linux/ioport.h> |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 13 | #include <linux/interrupt.h> |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/rtc.h> |
| 17 | #include <linux/spinlock.h> |
| 18 | #include <linux/types.h> |
Sachin Kamat | 4271e7f | 2013-11-12 15:10:34 -0800 | [diff] [blame] | 19 | #include <linux/uaccess.h> |
Jonathan Cameron | 5d2a503 | 2009-01-06 14:42:12 -0800 | [diff] [blame] | 20 | #include <linux/log2.h> |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 21 | |
| 22 | #include <asm/div64.h> |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 23 | |
Yoichi Yuasa | ada8e95 | 2009-07-03 00:39:38 +0900 | [diff] [blame] | 24 | MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>"); |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 25 | MODULE_DESCRIPTION("NEC VR4100 series RTC driver"); |
Yoichi Yuasa | 4cad443 | 2008-07-23 21:30:48 -0700 | [diff] [blame] | 26 | MODULE_LICENSE("GPL v2"); |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 27 | |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 28 | /* RTC 1 registers */ |
| 29 | #define ETIMELREG 0x00 |
| 30 | #define ETIMEMREG 0x02 |
| 31 | #define ETIMEHREG 0x04 |
| 32 | /* RFU */ |
| 33 | #define ECMPLREG 0x08 |
| 34 | #define ECMPMREG 0x0a |
| 35 | #define ECMPHREG 0x0c |
| 36 | /* RFU */ |
| 37 | #define RTCL1LREG 0x10 |
| 38 | #define RTCL1HREG 0x12 |
| 39 | #define RTCL1CNTLREG 0x14 |
| 40 | #define RTCL1CNTHREG 0x16 |
| 41 | #define RTCL2LREG 0x18 |
| 42 | #define RTCL2HREG 0x1a |
| 43 | #define RTCL2CNTLREG 0x1c |
| 44 | #define RTCL2CNTHREG 0x1e |
| 45 | |
| 46 | /* RTC 2 registers */ |
| 47 | #define TCLKLREG 0x00 |
| 48 | #define TCLKHREG 0x02 |
| 49 | #define TCLKCNTLREG 0x04 |
| 50 | #define TCLKCNTHREG 0x06 |
| 51 | /* RFU */ |
| 52 | #define RTCINTREG 0x1e |
| 53 | #define TCLOCK_INT 0x08 |
| 54 | #define RTCLONG2_INT 0x04 |
| 55 | #define RTCLONG1_INT 0x02 |
| 56 | #define ELAPSEDTIME_INT 0x01 |
| 57 | |
| 58 | #define RTC_FREQUENCY 32768 |
| 59 | #define MAX_PERIODIC_RATE 6553 |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 60 | |
| 61 | static void __iomem *rtc1_base; |
| 62 | static void __iomem *rtc2_base; |
| 63 | |
| 64 | #define rtc1_read(offset) readw(rtc1_base + (offset)) |
| 65 | #define rtc1_write(offset, value) writew((value), rtc1_base + (offset)) |
| 66 | |
| 67 | #define rtc2_read(offset) readw(rtc2_base + (offset)) |
| 68 | #define rtc2_write(offset, value) writew((value), rtc2_base + (offset)) |
| 69 | |
Arnd Bergmann | 076ff65 | 2018-08-24 00:11:19 +0200 | [diff] [blame] | 70 | /* 32-bit compat for ioctls that nobody else uses */ |
| 71 | #define RTC_EPOCH_READ32 _IOR('p', 0x0d, __u32) |
| 72 | |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 73 | static unsigned long epoch = 1970; /* Jan 1 1970 00:00:00 */ |
| 74 | |
Ingo Molnar | 34af946 | 2006-06-27 02:53:55 -0700 | [diff] [blame] | 75 | static DEFINE_SPINLOCK(rtc_lock); |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 76 | static char rtc_name[] = "RTC"; |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 77 | static unsigned long periodic_count; |
Yoichi Yuasa | 9b5ef64 | 2007-05-08 00:33:50 -0700 | [diff] [blame] | 78 | static unsigned int alarm_enabled; |
Anton Vorontsov | 2fac667 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 79 | static int aie_irq; |
| 80 | static int pie_irq; |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 81 | |
Arnd Bergmann | 0d1c655 | 2018-04-20 18:14:24 +0200 | [diff] [blame] | 82 | static inline time64_t read_elapsed_second(void) |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 83 | { |
| 84 | |
| 85 | unsigned long first_low, first_mid, first_high; |
| 86 | |
| 87 | unsigned long second_low, second_mid, second_high; |
| 88 | |
| 89 | do { |
| 90 | first_low = rtc1_read(ETIMELREG); |
| 91 | first_mid = rtc1_read(ETIMEMREG); |
| 92 | first_high = rtc1_read(ETIMEHREG); |
| 93 | second_low = rtc1_read(ETIMELREG); |
| 94 | second_mid = rtc1_read(ETIMEMREG); |
| 95 | second_high = rtc1_read(ETIMEHREG); |
| 96 | } while (first_low != second_low || first_mid != second_mid || |
Sachin Kamat | 0218bcf | 2013-07-03 15:06:06 -0700 | [diff] [blame] | 97 | first_high != second_high); |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 98 | |
Arnd Bergmann | 0d1c655 | 2018-04-20 18:14:24 +0200 | [diff] [blame] | 99 | return ((u64)first_high << 17) | (first_mid << 1) | (first_low >> 15); |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Arnd Bergmann | 0d1c655 | 2018-04-20 18:14:24 +0200 | [diff] [blame] | 102 | static inline void write_elapsed_second(time64_t sec) |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 103 | { |
| 104 | spin_lock_irq(&rtc_lock); |
| 105 | |
| 106 | rtc1_write(ETIMELREG, (uint16_t)(sec << 15)); |
| 107 | rtc1_write(ETIMEMREG, (uint16_t)(sec >> 1)); |
| 108 | rtc1_write(ETIMEHREG, (uint16_t)(sec >> 17)); |
| 109 | |
| 110 | spin_unlock_irq(&rtc_lock); |
| 111 | } |
| 112 | |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 113 | static int vr41xx_rtc_read_time(struct device *dev, struct rtc_time *time) |
| 114 | { |
Arnd Bergmann | 0d1c655 | 2018-04-20 18:14:24 +0200 | [diff] [blame] | 115 | time64_t epoch_sec, elapsed_sec; |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 116 | |
Arnd Bergmann | 0d1c655 | 2018-04-20 18:14:24 +0200 | [diff] [blame] | 117 | epoch_sec = mktime64(epoch, 1, 1, 0, 0, 0); |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 118 | elapsed_sec = read_elapsed_second(); |
| 119 | |
Arnd Bergmann | 0d1c655 | 2018-04-20 18:14:24 +0200 | [diff] [blame] | 120 | rtc_time64_to_tm(epoch_sec + elapsed_sec, time); |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int vr41xx_rtc_set_time(struct device *dev, struct rtc_time *time) |
| 126 | { |
Arnd Bergmann | 0d1c655 | 2018-04-20 18:14:24 +0200 | [diff] [blame] | 127 | time64_t epoch_sec, current_sec; |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 128 | |
Arnd Bergmann | 0d1c655 | 2018-04-20 18:14:24 +0200 | [diff] [blame] | 129 | epoch_sec = mktime64(epoch, 1, 1, 0, 0, 0); |
Alexandre Belloni | 89e27ce | 2018-10-01 23:51:35 +0200 | [diff] [blame] | 130 | current_sec = rtc_tm_to_time64(time); |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 131 | |
| 132 | write_elapsed_second(current_sec - epoch_sec); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static int vr41xx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) |
| 138 | { |
| 139 | unsigned long low, mid, high; |
| 140 | struct rtc_time *time = &wkalrm->time; |
| 141 | |
| 142 | spin_lock_irq(&rtc_lock); |
| 143 | |
| 144 | low = rtc1_read(ECMPLREG); |
| 145 | mid = rtc1_read(ECMPMREG); |
| 146 | high = rtc1_read(ECMPHREG); |
Yoichi Yuasa | 9b5ef64 | 2007-05-08 00:33:50 -0700 | [diff] [blame] | 147 | wkalrm->enabled = alarm_enabled; |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 148 | |
| 149 | spin_unlock_irq(&rtc_lock); |
| 150 | |
Alexandre Belloni | 89e27ce | 2018-10-01 23:51:35 +0200 | [diff] [blame] | 151 | rtc_time64_to_tm((high << 17) | (mid << 1) | (low >> 15), time); |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static int vr41xx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) |
| 157 | { |
Arnd Bergmann | 0d1c655 | 2018-04-20 18:14:24 +0200 | [diff] [blame] | 158 | time64_t alarm_sec; |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 159 | |
Alexandre Belloni | 89e27ce | 2018-10-01 23:51:35 +0200 | [diff] [blame] | 160 | alarm_sec = rtc_tm_to_time64(&wkalrm->time); |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 161 | |
| 162 | spin_lock_irq(&rtc_lock); |
| 163 | |
Yoichi Yuasa | 9b5ef64 | 2007-05-08 00:33:50 -0700 | [diff] [blame] | 164 | if (alarm_enabled) |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 165 | disable_irq(aie_irq); |
Yoichi Yuasa | 9b5ef64 | 2007-05-08 00:33:50 -0700 | [diff] [blame] | 166 | |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 167 | rtc1_write(ECMPLREG, (uint16_t)(alarm_sec << 15)); |
| 168 | rtc1_write(ECMPMREG, (uint16_t)(alarm_sec >> 1)); |
| 169 | rtc1_write(ECMPHREG, (uint16_t)(alarm_sec >> 17)); |
| 170 | |
Yoichi Yuasa | 9b5ef64 | 2007-05-08 00:33:50 -0700 | [diff] [blame] | 171 | if (wkalrm->enabled) |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 172 | enable_irq(aie_irq); |
Yoichi Yuasa | 9b5ef64 | 2007-05-08 00:33:50 -0700 | [diff] [blame] | 173 | |
| 174 | alarm_enabled = wkalrm->enabled; |
| 175 | |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 176 | spin_unlock_irq(&rtc_lock); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
Yoichi Yuasa | 4cad443 | 2008-07-23 21:30:48 -0700 | [diff] [blame] | 181 | static int vr41xx_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) |
| 182 | { |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 183 | switch (cmd) { |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 184 | case RTC_EPOCH_READ: |
| 185 | return put_user(epoch, (unsigned long __user *)arg); |
Arnd Bergmann | 076ff65 | 2018-08-24 00:11:19 +0200 | [diff] [blame] | 186 | #ifdef CONFIG_64BIT |
| 187 | case RTC_EPOCH_READ32: |
| 188 | return put_user(epoch, (unsigned int __user *)arg); |
| 189 | #endif |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 190 | case RTC_EPOCH_SET: |
| 191 | /* Doesn't support before 1900 */ |
| 192 | if (arg < 1900) |
| 193 | return -EINVAL; |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 194 | epoch = arg; |
| 195 | break; |
| 196 | default: |
Alessandro Zummo | b3969e5 | 2006-05-20 15:00:29 -0700 | [diff] [blame] | 197 | return -ENOIOCTLCMD; |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 203 | static int vr41xx_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 204 | { |
| 205 | spin_lock_irq(&rtc_lock); |
| 206 | if (enabled) { |
| 207 | if (!alarm_enabled) { |
| 208 | enable_irq(aie_irq); |
| 209 | alarm_enabled = 1; |
| 210 | } |
| 211 | } else { |
| 212 | if (alarm_enabled) { |
| 213 | disable_irq(aie_irq); |
| 214 | alarm_enabled = 0; |
| 215 | } |
| 216 | } |
| 217 | spin_unlock_irq(&rtc_lock); |
| 218 | return 0; |
| 219 | } |
| 220 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 221 | static irqreturn_t elapsedtime_interrupt(int irq, void *dev_id) |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 222 | { |
| 223 | struct platform_device *pdev = (struct platform_device *)dev_id; |
| 224 | struct rtc_device *rtc = platform_get_drvdata(pdev); |
| 225 | |
| 226 | rtc2_write(RTCINTREG, ELAPSEDTIME_INT); |
| 227 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 228 | rtc_update_irq(rtc, 1, RTC_AF); |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 229 | |
| 230 | return IRQ_HANDLED; |
| 231 | } |
| 232 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 233 | static irqreturn_t rtclong1_interrupt(int irq, void *dev_id) |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 234 | { |
| 235 | struct platform_device *pdev = (struct platform_device *)dev_id; |
| 236 | struct rtc_device *rtc = platform_get_drvdata(pdev); |
| 237 | unsigned long count = periodic_count; |
| 238 | |
| 239 | rtc2_write(RTCINTREG, RTCLONG1_INT); |
| 240 | |
| 241 | rtc1_write(RTCL1LREG, count); |
| 242 | rtc1_write(RTCL1HREG, count >> 16); |
| 243 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 244 | rtc_update_irq(rtc, 1, RTC_PF); |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 245 | |
| 246 | return IRQ_HANDLED; |
| 247 | } |
| 248 | |
David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 249 | static const struct rtc_class_ops vr41xx_rtc_ops = { |
Geert Uytterhoeven | a25f4a9 | 2016-03-01 09:50:01 +0100 | [diff] [blame] | 250 | .ioctl = vr41xx_rtc_ioctl, |
| 251 | .read_time = vr41xx_rtc_read_time, |
| 252 | .set_time = vr41xx_rtc_set_time, |
| 253 | .read_alarm = vr41xx_rtc_read_alarm, |
| 254 | .set_alarm = vr41xx_rtc_set_alarm, |
| 255 | .alarm_irq_enable = vr41xx_rtc_alarm_irq_enable, |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 256 | }; |
| 257 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 258 | static int rtc_probe(struct platform_device *pdev) |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 259 | { |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 260 | struct resource *res; |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 261 | struct rtc_device *rtc; |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 262 | int retval; |
| 263 | |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 264 | if (pdev->num_resources != 4) |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 265 | return -EBUSY; |
| 266 | |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 267 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 268 | if (!res) |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 269 | return -EBUSY; |
| 270 | |
Jingoo Han | bf6ce1a | 2014-01-23 15:55:07 -0800 | [diff] [blame] | 271 | rtc1_base = devm_ioremap(&pdev->dev, res->start, resource_size(res)); |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 272 | if (!rtc1_base) |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 273 | return -EBUSY; |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 274 | |
| 275 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 276 | if (!res) { |
| 277 | retval = -EBUSY; |
| 278 | goto err_rtc1_iounmap; |
| 279 | } |
| 280 | |
Jingoo Han | bf6ce1a | 2014-01-23 15:55:07 -0800 | [diff] [blame] | 281 | rtc2_base = devm_ioremap(&pdev->dev, res->start, resource_size(res)); |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 282 | if (!rtc2_base) { |
| 283 | retval = -EBUSY; |
| 284 | goto err_rtc1_iounmap; |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Alexandre Belloni | 9a99247 | 2018-05-17 22:47:05 +0200 | [diff] [blame] | 287 | rtc = devm_rtc_allocate_device(&pdev->dev); |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 288 | if (IS_ERR(rtc)) { |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 289 | retval = PTR_ERR(rtc); |
| 290 | goto err_iounmap_all; |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Alexandre Belloni | 9a99247 | 2018-05-17 22:47:05 +0200 | [diff] [blame] | 293 | rtc->ops = &vr41xx_rtc_ops; |
| 294 | |
Alexandre Belloni | 30d7891 | 2018-05-17 22:47:06 +0200 | [diff] [blame] | 295 | /* 48-bit counter at 32.768 kHz */ |
| 296 | rtc->range_max = (1ULL << 33) - 1; |
Yoichi Yuasa | 4cad443 | 2008-07-23 21:30:48 -0700 | [diff] [blame] | 297 | rtc->max_user_freq = MAX_PERIODIC_RATE; |
| 298 | |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 299 | spin_lock_irq(&rtc_lock); |
| 300 | |
| 301 | rtc1_write(ECMPLREG, 0); |
| 302 | rtc1_write(ECMPMREG, 0); |
| 303 | rtc1_write(ECMPHREG, 0); |
| 304 | rtc1_write(RTCL1LREG, 0); |
| 305 | rtc1_write(RTCL1HREG, 0); |
| 306 | |
| 307 | spin_unlock_irq(&rtc_lock); |
| 308 | |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 309 | aie_irq = platform_get_irq(pdev, 0); |
Anton Vorontsov | 2fac667 | 2009-01-06 14:42:11 -0800 | [diff] [blame] | 310 | if (aie_irq <= 0) { |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 311 | retval = -EBUSY; |
Jingoo Han | bf6ce1a | 2014-01-23 15:55:07 -0800 | [diff] [blame] | 312 | goto err_iounmap_all; |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Jingoo Han | bf6ce1a | 2014-01-23 15:55:07 -0800 | [diff] [blame] | 315 | retval = devm_request_irq(&pdev->dev, aie_irq, elapsedtime_interrupt, 0, |
| 316 | "elapsed_time", pdev); |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 317 | if (retval < 0) |
Jingoo Han | bf6ce1a | 2014-01-23 15:55:07 -0800 | [diff] [blame] | 318 | goto err_iounmap_all; |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 319 | |
| 320 | pie_irq = platform_get_irq(pdev, 1); |
Wei Yongjun | 812f147 | 2013-07-03 15:07:04 -0700 | [diff] [blame] | 321 | if (pie_irq <= 0) { |
| 322 | retval = -EBUSY; |
Jingoo Han | bf6ce1a | 2014-01-23 15:55:07 -0800 | [diff] [blame] | 323 | goto err_iounmap_all; |
Wei Yongjun | 812f147 | 2013-07-03 15:07:04 -0700 | [diff] [blame] | 324 | } |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 325 | |
Jingoo Han | bf6ce1a | 2014-01-23 15:55:07 -0800 | [diff] [blame] | 326 | retval = devm_request_irq(&pdev->dev, pie_irq, rtclong1_interrupt, 0, |
| 327 | "rtclong1", pdev); |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 328 | if (retval < 0) |
Jingoo Han | bf6ce1a | 2014-01-23 15:55:07 -0800 | [diff] [blame] | 329 | goto err_iounmap_all; |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 330 | |
| 331 | platform_set_drvdata(pdev, rtc); |
| 332 | |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 333 | disable_irq(aie_irq); |
| 334 | disable_irq(pie_irq); |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 335 | |
Jingoo Han | d75dcd3 | 2013-02-21 16:45:30 -0800 | [diff] [blame] | 336 | dev_info(&pdev->dev, "Real Time Clock of NEC VR4100 series\n"); |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 337 | |
Bartosz Golaszewski | fdcfd85 | 2020-11-09 17:34:08 +0100 | [diff] [blame] | 338 | retval = devm_rtc_register_device(rtc); |
Alexandre Belloni | 9a99247 | 2018-05-17 22:47:05 +0200 | [diff] [blame] | 339 | if (retval) |
| 340 | goto err_iounmap_all; |
| 341 | |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 342 | return 0; |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 343 | |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 344 | err_iounmap_all: |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 345 | rtc2_base = NULL; |
| 346 | |
| 347 | err_rtc1_iounmap: |
Yoichi Yuasa | bd076509 | 2007-05-11 21:18:48 +0900 | [diff] [blame] | 348 | rtc1_base = NULL; |
| 349 | |
| 350 | return retval; |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Kay Sievers | ad28a07 | 2008-04-10 21:29:25 -0700 | [diff] [blame] | 353 | /* work with hotplug and coldplug */ |
| 354 | MODULE_ALIAS("platform:RTC"); |
| 355 | |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 356 | static struct platform_driver rtc_platform_driver = { |
| 357 | .probe = rtc_probe, |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 358 | .driver = { |
| 359 | .name = rtc_name, |
Yoichi Yuasa | 8417eb7 | 2006-04-10 22:54:47 -0700 | [diff] [blame] | 360 | }, |
| 361 | }; |
| 362 | |
Axel Lin | 0c4eae6 | 2012-01-10 15:10:48 -0800 | [diff] [blame] | 363 | module_platform_driver(rtc_platform_driver); |