Thomas Gleixner | b886d83 | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2008-2009 Nuvoton technology corporation. |
| 4 | * |
| 5 | * Wan ZongShun <mcuos.com@gmail.com> |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 12 | #include <linux/rtc.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <linux/bcd.h> |
| 16 | |
| 17 | /* RTC Control Registers */ |
| 18 | #define REG_RTC_INIR 0x00 |
| 19 | #define REG_RTC_AER 0x04 |
| 20 | #define REG_RTC_FCR 0x08 |
| 21 | #define REG_RTC_TLR 0x0C |
| 22 | #define REG_RTC_CLR 0x10 |
| 23 | #define REG_RTC_TSSR 0x14 |
| 24 | #define REG_RTC_DWR 0x18 |
| 25 | #define REG_RTC_TAR 0x1C |
| 26 | #define REG_RTC_CAR 0x20 |
| 27 | #define REG_RTC_LIR 0x24 |
| 28 | #define REG_RTC_RIER 0x28 |
| 29 | #define REG_RTC_RIIR 0x2C |
| 30 | #define REG_RTC_TTR 0x30 |
| 31 | |
| 32 | #define RTCSET 0x01 |
| 33 | #define AERRWENB 0x10000 |
| 34 | #define INIRRESET 0xa5eb1357 |
| 35 | #define AERPOWERON 0xA965 |
| 36 | #define AERPOWEROFF 0x0000 |
| 37 | #define LEAPYEAR 0x0001 |
| 38 | #define TICKENB 0x80 |
| 39 | #define TICKINTENB 0x0002 |
| 40 | #define ALARMINTENB 0x0001 |
| 41 | #define MODE24 0x0001 |
| 42 | |
| 43 | struct nuc900_rtc { |
| 44 | int irq_num; |
| 45 | void __iomem *rtc_reg; |
| 46 | struct rtc_device *rtcdev; |
| 47 | }; |
| 48 | |
| 49 | struct nuc900_bcd_time { |
| 50 | int bcd_sec; |
| 51 | int bcd_min; |
| 52 | int bcd_hour; |
| 53 | int bcd_mday; |
| 54 | int bcd_mon; |
| 55 | int bcd_year; |
| 56 | }; |
| 57 | |
| 58 | static irqreturn_t nuc900_rtc_interrupt(int irq, void *_rtc) |
| 59 | { |
| 60 | struct nuc900_rtc *rtc = _rtc; |
| 61 | unsigned long events = 0, rtc_irq; |
| 62 | |
| 63 | rtc_irq = __raw_readl(rtc->rtc_reg + REG_RTC_RIIR); |
| 64 | |
| 65 | if (rtc_irq & ALARMINTENB) { |
| 66 | rtc_irq &= ~ALARMINTENB; |
| 67 | __raw_writel(rtc_irq, rtc->rtc_reg + REG_RTC_RIIR); |
| 68 | events |= RTC_AF | RTC_IRQF; |
| 69 | } |
| 70 | |
| 71 | if (rtc_irq & TICKINTENB) { |
| 72 | rtc_irq &= ~TICKINTENB; |
| 73 | __raw_writel(rtc_irq, rtc->rtc_reg + REG_RTC_RIIR); |
| 74 | events |= RTC_UF | RTC_IRQF; |
| 75 | } |
| 76 | |
| 77 | rtc_update_irq(rtc->rtcdev, 1, events); |
| 78 | |
| 79 | return IRQ_HANDLED; |
| 80 | } |
| 81 | |
| 82 | static int *check_rtc_access_enable(struct nuc900_rtc *nuc900_rtc) |
| 83 | { |
Wan ZongShun | 2f11e57 | 2010-08-10 18:02:20 -0700 | [diff] [blame] | 84 | unsigned int timeout = 0x1000; |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 85 | __raw_writel(INIRRESET, nuc900_rtc->rtc_reg + REG_RTC_INIR); |
| 86 | |
| 87 | mdelay(10); |
| 88 | |
| 89 | __raw_writel(AERPOWERON, nuc900_rtc->rtc_reg + REG_RTC_AER); |
| 90 | |
Wan ZongShun | 0a89b55 | 2010-08-10 18:02:05 -0700 | [diff] [blame] | 91 | while (!(__raw_readl(nuc900_rtc->rtc_reg + REG_RTC_AER) & AERRWENB) |
Dan Carpenter | d0a67c3 | 2017-06-23 11:29:00 +0300 | [diff] [blame] | 92 | && --timeout) |
Wan ZongShun | 0a89b55 | 2010-08-10 18:02:05 -0700 | [diff] [blame] | 93 | mdelay(1); |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 94 | |
Wan ZongShun | 0a89b55 | 2010-08-10 18:02:05 -0700 | [diff] [blame] | 95 | if (!timeout) |
| 96 | return ERR_PTR(-EPERM); |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 97 | |
Jingoo Han | 0ebbf43 | 2013-09-11 14:24:22 -0700 | [diff] [blame] | 98 | return NULL; |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Alexandre Belloni | b6cb398 | 2018-02-21 11:27:10 +0100 | [diff] [blame] | 101 | static void nuc900_rtc_bcd2bin(unsigned int timereg, |
| 102 | unsigned int calreg, struct rtc_time *tm) |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 103 | { |
| 104 | tm->tm_mday = bcd2bin(calreg >> 0); |
| 105 | tm->tm_mon = bcd2bin(calreg >> 8); |
| 106 | tm->tm_year = bcd2bin(calreg >> 16) + 100; |
| 107 | |
| 108 | tm->tm_sec = bcd2bin(timereg >> 0); |
| 109 | tm->tm_min = bcd2bin(timereg >> 8); |
| 110 | tm->tm_hour = bcd2bin(timereg >> 16); |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Wan ZongShun | 70d2a0b | 2010-08-10 18:02:07 -0700 | [diff] [blame] | 113 | static void nuc900_rtc_bin2bcd(struct device *dev, struct rtc_time *settm, |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 114 | struct nuc900_bcd_time *gettm) |
| 115 | { |
| 116 | gettm->bcd_mday = bin2bcd(settm->tm_mday) << 0; |
| 117 | gettm->bcd_mon = bin2bcd(settm->tm_mon) << 8; |
Wan ZongShun | 70d2a0b | 2010-08-10 18:02:07 -0700 | [diff] [blame] | 118 | |
| 119 | if (settm->tm_year < 100) { |
| 120 | dev_warn(dev, "The year will be between 1970-1999, right?\n"); |
| 121 | gettm->bcd_year = bin2bcd(settm->tm_year) << 16; |
| 122 | } else { |
| 123 | gettm->bcd_year = bin2bcd(settm->tm_year - 100) << 16; |
| 124 | } |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 125 | |
| 126 | gettm->bcd_sec = bin2bcd(settm->tm_sec) << 0; |
| 127 | gettm->bcd_min = bin2bcd(settm->tm_min) << 8; |
| 128 | gettm->bcd_hour = bin2bcd(settm->tm_hour) << 16; |
| 129 | } |
| 130 | |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 131 | static int nuc900_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 132 | { |
| 133 | struct nuc900_rtc *rtc = dev_get_drvdata(dev); |
| 134 | |
| 135 | if (enabled) |
| 136 | __raw_writel(__raw_readl(rtc->rtc_reg + REG_RTC_RIER)| |
| 137 | (ALARMINTENB), rtc->rtc_reg + REG_RTC_RIER); |
| 138 | else |
| 139 | __raw_writel(__raw_readl(rtc->rtc_reg + REG_RTC_RIER)& |
| 140 | (~ALARMINTENB), rtc->rtc_reg + REG_RTC_RIER); |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int nuc900_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 146 | { |
| 147 | struct nuc900_rtc *rtc = dev_get_drvdata(dev); |
| 148 | unsigned int timeval, clrval; |
| 149 | |
| 150 | timeval = __raw_readl(rtc->rtc_reg + REG_RTC_TLR); |
| 151 | clrval = __raw_readl(rtc->rtc_reg + REG_RTC_CLR); |
| 152 | |
Alexandre Belloni | b6cb398 | 2018-02-21 11:27:10 +0100 | [diff] [blame] | 153 | nuc900_rtc_bcd2bin(timeval, clrval, tm); |
| 154 | |
| 155 | return 0; |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static int nuc900_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 159 | { |
| 160 | struct nuc900_rtc *rtc = dev_get_drvdata(dev); |
| 161 | struct nuc900_bcd_time gettm; |
| 162 | unsigned long val; |
| 163 | int *err; |
| 164 | |
Wan ZongShun | 70d2a0b | 2010-08-10 18:02:07 -0700 | [diff] [blame] | 165 | nuc900_rtc_bin2bcd(dev, tm, &gettm); |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 166 | |
| 167 | err = check_rtc_access_enable(rtc); |
| 168 | if (IS_ERR(err)) |
| 169 | return PTR_ERR(err); |
| 170 | |
| 171 | val = gettm.bcd_mday | gettm.bcd_mon | gettm.bcd_year; |
| 172 | __raw_writel(val, rtc->rtc_reg + REG_RTC_CLR); |
| 173 | |
| 174 | val = gettm.bcd_sec | gettm.bcd_min | gettm.bcd_hour; |
| 175 | __raw_writel(val, rtc->rtc_reg + REG_RTC_TLR); |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int nuc900_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 181 | { |
| 182 | struct nuc900_rtc *rtc = dev_get_drvdata(dev); |
| 183 | unsigned int timeval, carval; |
| 184 | |
| 185 | timeval = __raw_readl(rtc->rtc_reg + REG_RTC_TAR); |
| 186 | carval = __raw_readl(rtc->rtc_reg + REG_RTC_CAR); |
| 187 | |
Alexandre Belloni | b6cb398 | 2018-02-21 11:27:10 +0100 | [diff] [blame] | 188 | nuc900_rtc_bcd2bin(timeval, carval, &alrm->time); |
| 189 | |
| 190 | return rtc_valid_tm(&alrm->time); |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static int nuc900_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 194 | { |
| 195 | struct nuc900_rtc *rtc = dev_get_drvdata(dev); |
| 196 | struct nuc900_bcd_time tm; |
| 197 | unsigned long val; |
| 198 | int *err; |
| 199 | |
Wan ZongShun | 70d2a0b | 2010-08-10 18:02:07 -0700 | [diff] [blame] | 200 | nuc900_rtc_bin2bcd(dev, &alrm->time, &tm); |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 201 | |
| 202 | err = check_rtc_access_enable(rtc); |
| 203 | if (IS_ERR(err)) |
| 204 | return PTR_ERR(err); |
| 205 | |
| 206 | val = tm.bcd_mday | tm.bcd_mon | tm.bcd_year; |
| 207 | __raw_writel(val, rtc->rtc_reg + REG_RTC_CAR); |
| 208 | |
| 209 | val = tm.bcd_sec | tm.bcd_min | tm.bcd_hour; |
| 210 | __raw_writel(val, rtc->rtc_reg + REG_RTC_TAR); |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
Julia Lawall | 34c7b3a | 2016-08-31 10:05:25 +0200 | [diff] [blame] | 215 | static const struct rtc_class_ops nuc900_rtc_ops = { |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 216 | .read_time = nuc900_rtc_read_time, |
| 217 | .set_time = nuc900_rtc_set_time, |
| 218 | .read_alarm = nuc900_rtc_read_alarm, |
| 219 | .set_alarm = nuc900_rtc_set_alarm, |
| 220 | .alarm_irq_enable = nuc900_alarm_irq_enable, |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 221 | }; |
| 222 | |
Jingoo Han | e7a6c21 | 2013-04-29 16:18:25 -0700 | [diff] [blame] | 223 | static int __init nuc900_rtc_probe(struct platform_device *pdev) |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 224 | { |
| 225 | struct resource *res; |
| 226 | struct nuc900_rtc *nuc900_rtc; |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 227 | |
Jingoo Han | a63794f | 2013-04-29 16:20:46 -0700 | [diff] [blame] | 228 | nuc900_rtc = devm_kzalloc(&pdev->dev, sizeof(struct nuc900_rtc), |
| 229 | GFP_KERNEL); |
Jingoo Han | ee56719 | 2014-04-03 14:49:42 -0700 | [diff] [blame] | 230 | if (!nuc900_rtc) |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 231 | return -ENOMEM; |
Jingoo Han | ee56719 | 2014-04-03 14:49:42 -0700 | [diff] [blame] | 232 | |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 233 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Jingoo Han | a63794f | 2013-04-29 16:20:46 -0700 | [diff] [blame] | 234 | nuc900_rtc->rtc_reg = devm_ioremap_resource(&pdev->dev, res); |
| 235 | if (IS_ERR(nuc900_rtc->rtc_reg)) |
| 236 | return PTR_ERR(nuc900_rtc->rtc_reg); |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 237 | |
Wan ZongShun | 23e53be | 2010-08-10 18:02:19 -0700 | [diff] [blame] | 238 | platform_set_drvdata(pdev, nuc900_rtc); |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 239 | |
Jingoo Han | a63794f | 2013-04-29 16:20:46 -0700 | [diff] [blame] | 240 | nuc900_rtc->rtcdev = devm_rtc_device_register(&pdev->dev, pdev->name, |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 241 | &nuc900_rtc_ops, THIS_MODULE); |
| 242 | if (IS_ERR(nuc900_rtc->rtcdev)) { |
Paul Bolle | 426d310 | 2010-08-07 12:30:03 +0200 | [diff] [blame] | 243 | dev_err(&pdev->dev, "rtc device register failed\n"); |
Jingoo Han | a63794f | 2013-04-29 16:20:46 -0700 | [diff] [blame] | 244 | return PTR_ERR(nuc900_rtc->rtcdev); |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 247 | __raw_writel(__raw_readl(nuc900_rtc->rtc_reg + REG_RTC_TSSR) | MODE24, |
| 248 | nuc900_rtc->rtc_reg + REG_RTC_TSSR); |
| 249 | |
Wan ZongShun | 23e53be | 2010-08-10 18:02:19 -0700 | [diff] [blame] | 250 | nuc900_rtc->irq_num = platform_get_irq(pdev, 0); |
Jingoo Han | a63794f | 2013-04-29 16:20:46 -0700 | [diff] [blame] | 251 | if (devm_request_irq(&pdev->dev, nuc900_rtc->irq_num, |
| 252 | nuc900_rtc_interrupt, 0, "nuc900rtc", nuc900_rtc)) { |
Wan ZongShun | 23e53be | 2010-08-10 18:02:19 -0700 | [diff] [blame] | 253 | dev_err(&pdev->dev, "NUC900 RTC request irq failed\n"); |
Jingoo Han | a63794f | 2013-04-29 16:20:46 -0700 | [diff] [blame] | 254 | return -EBUSY; |
Wan ZongShun | 23e53be | 2010-08-10 18:02:19 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 257 | return 0; |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 260 | static struct platform_driver nuc900_rtc_driver = { |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 261 | .driver = { |
| 262 | .name = "nuc900-rtc", |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 263 | }, |
| 264 | }; |
| 265 | |
Jingoo Han | af5e7db | 2013-04-29 16:18:45 -0700 | [diff] [blame] | 266 | module_platform_driver_probe(nuc900_rtc_driver, nuc900_rtc_probe); |
Wan ZongShun | afd49a7e | 2009-12-15 16:46:17 -0800 | [diff] [blame] | 267 | |
| 268 | MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>"); |
| 269 | MODULE_DESCRIPTION("nuc910/nuc920 RTC driver"); |
| 270 | MODULE_LICENSE("GPL"); |
| 271 | MODULE_ALIAS("platform:nuc900-rtc"); |