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