Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 1 | /* |
| 2 | * SuperH On-Chip RTC Support |
| 3 | * |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 4 | * Copyright (C) 2006, 2007 Paul Mundt |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 5 | * Copyright (C) 2006 Jamie Lenehan |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 6 | * |
| 7 | * Based on the old arch/sh/kernel/cpu/rtc.c by: |
| 8 | * |
| 9 | * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org> |
| 10 | * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka |
| 11 | * |
| 12 | * This file is subject to the terms and conditions of the GNU General Public |
| 13 | * License. See the file "COPYING" in the main directory of this archive |
| 14 | * for more details. |
| 15 | */ |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/bcd.h> |
| 19 | #include <linux/rtc.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/seq_file.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/spinlock.h> |
Jamie Lenehan | 31ccb08 | 2006-12-07 17:23:50 +0900 | [diff] [blame] | 25 | #include <linux/io.h> |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 26 | #include <asm/rtc.h> |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 27 | |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 28 | #define DRV_NAME "sh-rtc" |
Paul Mundt | 9a519f6 | 2007-11-08 14:44:44 +0900 | [diff] [blame^] | 29 | #define DRV_VERSION "0.1.4" |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 30 | |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 31 | #ifdef CONFIG_CPU_SH3 |
| 32 | #define rtc_reg_size sizeof(u16) |
| 33 | #define RTC_BIT_INVERTED 0 /* No bug on SH7708, SH7709A */ |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 34 | #define RTC_DEF_CAPABILITIES 0UL |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 35 | #elif defined(CONFIG_CPU_SH4) |
| 36 | #define rtc_reg_size sizeof(u32) |
| 37 | #define RTC_BIT_INVERTED 0x40 /* bug on SH7750, SH7750S */ |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 38 | #define RTC_DEF_CAPABILITIES RTC_CAP_4_DIGIT_YEAR |
Paul Mundt | 9a519f6 | 2007-11-08 14:44:44 +0900 | [diff] [blame^] | 39 | #elif defined(CONFIG_CPU_SH5) |
| 40 | #define rtc_reg_size sizeof(u32) |
| 41 | #define RTC_BIT_INVERTED 0 /* The SH-5 RTC is surprisingly sane! */ |
| 42 | #define RTC_DEF_CAPABILITIES RTC_CAP_4_DIGIT_YEAR |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 43 | #endif |
| 44 | |
| 45 | #define RTC_REG(r) ((r) * rtc_reg_size) |
| 46 | |
Jamie Lenehan | 31ccb08 | 2006-12-07 17:23:50 +0900 | [diff] [blame] | 47 | #define R64CNT RTC_REG(0) |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 48 | |
| 49 | #define RSECCNT RTC_REG(1) /* RTC sec */ |
| 50 | #define RMINCNT RTC_REG(2) /* RTC min */ |
| 51 | #define RHRCNT RTC_REG(3) /* RTC hour */ |
| 52 | #define RWKCNT RTC_REG(4) /* RTC week */ |
| 53 | #define RDAYCNT RTC_REG(5) /* RTC day */ |
| 54 | #define RMONCNT RTC_REG(6) /* RTC month */ |
| 55 | #define RYRCNT RTC_REG(7) /* RTC year */ |
| 56 | #define RSECAR RTC_REG(8) /* ALARM sec */ |
| 57 | #define RMINAR RTC_REG(9) /* ALARM min */ |
| 58 | #define RHRAR RTC_REG(10) /* ALARM hour */ |
| 59 | #define RWKAR RTC_REG(11) /* ALARM week */ |
| 60 | #define RDAYAR RTC_REG(12) /* ALARM day */ |
| 61 | #define RMONAR RTC_REG(13) /* ALARM month */ |
| 62 | #define RCR1 RTC_REG(14) /* Control */ |
| 63 | #define RCR2 RTC_REG(15) /* Control */ |
| 64 | |
| 65 | /* ALARM Bits - or with BCD encoded value */ |
| 66 | #define AR_ENB 0x80 /* Enable for alarm cmp */ |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 67 | |
| 68 | /* RCR1 Bits */ |
| 69 | #define RCR1_CF 0x80 /* Carry Flag */ |
| 70 | #define RCR1_CIE 0x10 /* Carry Interrupt Enable */ |
| 71 | #define RCR1_AIE 0x08 /* Alarm Interrupt Enable */ |
| 72 | #define RCR1_AF 0x01 /* Alarm Flag */ |
| 73 | |
| 74 | /* RCR2 Bits */ |
| 75 | #define RCR2_PEF 0x80 /* PEriodic interrupt Flag */ |
| 76 | #define RCR2_PESMASK 0x70 /* Periodic interrupt Set */ |
| 77 | #define RCR2_RTCEN 0x08 /* ENable RTC */ |
| 78 | #define RCR2_ADJ 0x04 /* ADJustment (30-second) */ |
| 79 | #define RCR2_RESET 0x02 /* Reset bit */ |
| 80 | #define RCR2_START 0x01 /* Start bit */ |
| 81 | |
| 82 | struct sh_rtc { |
| 83 | void __iomem *regbase; |
| 84 | unsigned long regsize; |
| 85 | struct resource *res; |
| 86 | unsigned int alarm_irq, periodic_irq, carry_irq; |
| 87 | struct rtc_device *rtc_dev; |
| 88 | spinlock_t lock; |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 89 | int rearm_aie; |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 90 | unsigned long capabilities; /* See asm-sh/rtc.h for cap bits */ |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 91 | }; |
| 92 | |
Jamie Lenehan | 31ccb08 | 2006-12-07 17:23:50 +0900 | [diff] [blame] | 93 | static irqreturn_t sh_rtc_interrupt(int irq, void *dev_id) |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 94 | { |
Jamie Lenehan | 31ccb08 | 2006-12-07 17:23:50 +0900 | [diff] [blame] | 95 | struct platform_device *pdev = to_platform_device(dev_id); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 96 | struct sh_rtc *rtc = platform_get_drvdata(pdev); |
| 97 | unsigned int tmp, events = 0; |
| 98 | |
| 99 | spin_lock(&rtc->lock); |
| 100 | |
| 101 | tmp = readb(rtc->regbase + RCR1); |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 102 | tmp &= ~RCR1_CF; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 103 | |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 104 | if (rtc->rearm_aie) { |
| 105 | if (tmp & RCR1_AF) |
| 106 | tmp &= ~RCR1_AF; /* try to clear AF again */ |
| 107 | else { |
| 108 | tmp |= RCR1_AIE; /* AF has cleared, rearm IRQ */ |
| 109 | rtc->rearm_aie = 0; |
| 110 | } |
| 111 | } |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 112 | |
| 113 | writeb(tmp, rtc->regbase + RCR1); |
| 114 | |
Paul Mundt | a361a68 | 2007-05-09 12:37:53 +0900 | [diff] [blame] | 115 | rtc_update_irq(rtc->rtc_dev, 1, events); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 116 | |
| 117 | spin_unlock(&rtc->lock); |
| 118 | |
| 119 | return IRQ_HANDLED; |
| 120 | } |
| 121 | |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 122 | static irqreturn_t sh_rtc_alarm(int irq, void *dev_id) |
| 123 | { |
| 124 | struct platform_device *pdev = to_platform_device(dev_id); |
| 125 | struct sh_rtc *rtc = platform_get_drvdata(pdev); |
| 126 | unsigned int tmp, events = 0; |
| 127 | |
| 128 | spin_lock(&rtc->lock); |
| 129 | |
| 130 | tmp = readb(rtc->regbase + RCR1); |
| 131 | |
| 132 | /* |
| 133 | * If AF is set then the alarm has triggered. If we clear AF while |
| 134 | * the alarm time still matches the RTC time then AF will |
| 135 | * immediately be set again, and if AIE is enabled then the alarm |
| 136 | * interrupt will immediately be retrigger. So we clear AIE here |
| 137 | * and use rtc->rearm_aie so that the carry interrupt will keep |
| 138 | * trying to clear AF and once it stays cleared it'll re-enable |
| 139 | * AIE. |
| 140 | */ |
| 141 | if (tmp & RCR1_AF) { |
| 142 | events |= RTC_AF | RTC_IRQF; |
| 143 | |
| 144 | tmp &= ~(RCR1_AF|RCR1_AIE); |
| 145 | |
| 146 | writeb(tmp, rtc->regbase + RCR1); |
| 147 | |
| 148 | rtc->rearm_aie = 1; |
| 149 | |
Paul Mundt | a361a68 | 2007-05-09 12:37:53 +0900 | [diff] [blame] | 150 | rtc_update_irq(rtc->rtc_dev, 1, events); |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | spin_unlock(&rtc->lock); |
| 154 | return IRQ_HANDLED; |
| 155 | } |
| 156 | |
Jamie Lenehan | 31ccb08 | 2006-12-07 17:23:50 +0900 | [diff] [blame] | 157 | static irqreturn_t sh_rtc_periodic(int irq, void *dev_id) |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 158 | { |
Jamie Lenehan | 31ccb08 | 2006-12-07 17:23:50 +0900 | [diff] [blame] | 159 | struct platform_device *pdev = to_platform_device(dev_id); |
| 160 | struct sh_rtc *rtc = platform_get_drvdata(pdev); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 161 | |
| 162 | spin_lock(&rtc->lock); |
| 163 | |
Paul Mundt | a361a68 | 2007-05-09 12:37:53 +0900 | [diff] [blame] | 164 | rtc_update_irq(rtc->rtc_dev, 1, RTC_PF | RTC_IRQF); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 165 | |
| 166 | spin_unlock(&rtc->lock); |
| 167 | |
| 168 | return IRQ_HANDLED; |
| 169 | } |
| 170 | |
| 171 | static inline void sh_rtc_setpie(struct device *dev, unsigned int enable) |
| 172 | { |
| 173 | struct sh_rtc *rtc = dev_get_drvdata(dev); |
| 174 | unsigned int tmp; |
| 175 | |
| 176 | spin_lock_irq(&rtc->lock); |
| 177 | |
| 178 | tmp = readb(rtc->regbase + RCR2); |
| 179 | |
| 180 | if (enable) { |
| 181 | tmp &= ~RCR2_PESMASK; |
| 182 | tmp |= RCR2_PEF | (2 << 4); |
| 183 | } else |
| 184 | tmp &= ~(RCR2_PESMASK | RCR2_PEF); |
| 185 | |
| 186 | writeb(tmp, rtc->regbase + RCR2); |
| 187 | |
| 188 | spin_unlock_irq(&rtc->lock); |
| 189 | } |
| 190 | |
| 191 | static inline void sh_rtc_setaie(struct device *dev, unsigned int enable) |
| 192 | { |
| 193 | struct sh_rtc *rtc = dev_get_drvdata(dev); |
| 194 | unsigned int tmp; |
| 195 | |
| 196 | spin_lock_irq(&rtc->lock); |
| 197 | |
| 198 | tmp = readb(rtc->regbase + RCR1); |
| 199 | |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 200 | if (!enable) { |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 201 | tmp &= ~RCR1_AIE; |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 202 | rtc->rearm_aie = 0; |
| 203 | } else if (rtc->rearm_aie == 0) |
| 204 | tmp |= RCR1_AIE; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 205 | |
| 206 | writeb(tmp, rtc->regbase + RCR1); |
| 207 | |
| 208 | spin_unlock_irq(&rtc->lock); |
| 209 | } |
| 210 | |
| 211 | static int sh_rtc_open(struct device *dev) |
| 212 | { |
| 213 | struct sh_rtc *rtc = dev_get_drvdata(dev); |
| 214 | unsigned int tmp; |
| 215 | int ret; |
| 216 | |
| 217 | tmp = readb(rtc->regbase + RCR1); |
| 218 | tmp &= ~RCR1_CF; |
| 219 | tmp |= RCR1_CIE; |
| 220 | writeb(tmp, rtc->regbase + RCR1); |
| 221 | |
Paul Mundt | 35f3c51 | 2006-10-06 15:31:16 +0900 | [diff] [blame] | 222 | ret = request_irq(rtc->periodic_irq, sh_rtc_periodic, IRQF_DISABLED, |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 223 | "sh-rtc period", dev); |
| 224 | if (unlikely(ret)) { |
| 225 | dev_err(dev, "request period IRQ failed with %d, IRQ %d\n", |
| 226 | ret, rtc->periodic_irq); |
| 227 | return ret; |
| 228 | } |
| 229 | |
Paul Mundt | 35f3c51 | 2006-10-06 15:31:16 +0900 | [diff] [blame] | 230 | ret = request_irq(rtc->carry_irq, sh_rtc_interrupt, IRQF_DISABLED, |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 231 | "sh-rtc carry", dev); |
| 232 | if (unlikely(ret)) { |
| 233 | dev_err(dev, "request carry IRQ failed with %d, IRQ %d\n", |
| 234 | ret, rtc->carry_irq); |
| 235 | free_irq(rtc->periodic_irq, dev); |
| 236 | goto err_bad_carry; |
| 237 | } |
| 238 | |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 239 | ret = request_irq(rtc->alarm_irq, sh_rtc_alarm, IRQF_DISABLED, |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 240 | "sh-rtc alarm", dev); |
| 241 | if (unlikely(ret)) { |
| 242 | dev_err(dev, "request alarm IRQ failed with %d, IRQ %d\n", |
| 243 | ret, rtc->alarm_irq); |
| 244 | goto err_bad_alarm; |
| 245 | } |
| 246 | |
| 247 | return 0; |
| 248 | |
| 249 | err_bad_alarm: |
| 250 | free_irq(rtc->carry_irq, dev); |
| 251 | err_bad_carry: |
| 252 | free_irq(rtc->periodic_irq, dev); |
| 253 | |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | static void sh_rtc_release(struct device *dev) |
| 258 | { |
| 259 | struct sh_rtc *rtc = dev_get_drvdata(dev); |
| 260 | |
| 261 | sh_rtc_setpie(dev, 0); |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 262 | sh_rtc_setaie(dev, 0); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 263 | |
| 264 | free_irq(rtc->periodic_irq, dev); |
| 265 | free_irq(rtc->carry_irq, dev); |
| 266 | free_irq(rtc->alarm_irq, dev); |
| 267 | } |
| 268 | |
| 269 | static int sh_rtc_proc(struct device *dev, struct seq_file *seq) |
| 270 | { |
| 271 | struct sh_rtc *rtc = dev_get_drvdata(dev); |
| 272 | unsigned int tmp; |
| 273 | |
| 274 | tmp = readb(rtc->regbase + RCR1); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 275 | seq_printf(seq, "carry_IRQ\t: %s\n", |
| 276 | (tmp & RCR1_CIE) ? "yes" : "no"); |
| 277 | |
| 278 | tmp = readb(rtc->regbase + RCR2); |
| 279 | seq_printf(seq, "periodic_IRQ\t: %s\n", |
| 280 | (tmp & RCR2_PEF) ? "yes" : "no"); |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static int sh_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) |
| 286 | { |
| 287 | unsigned int ret = -ENOIOCTLCMD; |
| 288 | |
| 289 | switch (cmd) { |
| 290 | case RTC_PIE_OFF: |
| 291 | case RTC_PIE_ON: |
| 292 | sh_rtc_setpie(dev, cmd == RTC_PIE_ON); |
| 293 | ret = 0; |
| 294 | break; |
| 295 | case RTC_AIE_OFF: |
| 296 | case RTC_AIE_ON: |
| 297 | sh_rtc_setaie(dev, cmd == RTC_AIE_ON); |
| 298 | ret = 0; |
| 299 | break; |
| 300 | } |
| 301 | |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | static int sh_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 306 | { |
| 307 | struct platform_device *pdev = to_platform_device(dev); |
| 308 | struct sh_rtc *rtc = platform_get_drvdata(pdev); |
| 309 | unsigned int sec128, sec2, yr, yr100, cf_bit; |
| 310 | |
| 311 | do { |
| 312 | unsigned int tmp; |
| 313 | |
| 314 | spin_lock_irq(&rtc->lock); |
| 315 | |
| 316 | tmp = readb(rtc->regbase + RCR1); |
| 317 | tmp &= ~RCR1_CF; /* Clear CF-bit */ |
| 318 | tmp |= RCR1_CIE; |
| 319 | writeb(tmp, rtc->regbase + RCR1); |
| 320 | |
| 321 | sec128 = readb(rtc->regbase + R64CNT); |
| 322 | |
| 323 | tm->tm_sec = BCD2BIN(readb(rtc->regbase + RSECCNT)); |
| 324 | tm->tm_min = BCD2BIN(readb(rtc->regbase + RMINCNT)); |
| 325 | tm->tm_hour = BCD2BIN(readb(rtc->regbase + RHRCNT)); |
| 326 | tm->tm_wday = BCD2BIN(readb(rtc->regbase + RWKCNT)); |
| 327 | tm->tm_mday = BCD2BIN(readb(rtc->regbase + RDAYCNT)); |
Jamie Lenehan | a161479 | 2006-12-08 14:49:30 +0900 | [diff] [blame] | 328 | tm->tm_mon = BCD2BIN(readb(rtc->regbase + RMONCNT)) - 1; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 329 | |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 330 | if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) { |
| 331 | yr = readw(rtc->regbase + RYRCNT); |
| 332 | yr100 = BCD2BIN(yr >> 8); |
| 333 | yr &= 0xff; |
| 334 | } else { |
| 335 | yr = readb(rtc->regbase + RYRCNT); |
| 336 | yr100 = BCD2BIN((yr == 0x99) ? 0x19 : 0x20); |
| 337 | } |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 338 | |
| 339 | tm->tm_year = (yr100 * 100 + BCD2BIN(yr)) - 1900; |
| 340 | |
| 341 | sec2 = readb(rtc->regbase + R64CNT); |
| 342 | cf_bit = readb(rtc->regbase + RCR1) & RCR1_CF; |
| 343 | |
| 344 | spin_unlock_irq(&rtc->lock); |
| 345 | } while (cf_bit != 0 || ((sec128 ^ sec2) & RTC_BIT_INVERTED) != 0); |
| 346 | |
| 347 | #if RTC_BIT_INVERTED != 0 |
| 348 | if ((sec128 & RTC_BIT_INVERTED)) |
| 349 | tm->tm_sec--; |
| 350 | #endif |
| 351 | |
Paul Mundt | 435c55d | 2007-05-08 11:56:27 +0900 | [diff] [blame] | 352 | dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, " |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 353 | "mday=%d, mon=%d, year=%d, wday=%d\n", |
| 354 | __FUNCTION__, |
| 355 | tm->tm_sec, tm->tm_min, tm->tm_hour, |
Jamie Lenehan | a161479 | 2006-12-08 14:49:30 +0900 | [diff] [blame] | 356 | tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 357 | |
Paul Mundt | 0ac554b | 2007-11-07 20:13:24 +0900 | [diff] [blame] | 358 | if (rtc_valid_tm(tm) < 0) { |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 359 | dev_err(dev, "invalid date\n"); |
Paul Mundt | 0ac554b | 2007-11-07 20:13:24 +0900 | [diff] [blame] | 360 | rtc_time_to_tm(0, tm); |
| 361 | } |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | static int sh_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 367 | { |
| 368 | struct platform_device *pdev = to_platform_device(dev); |
| 369 | struct sh_rtc *rtc = platform_get_drvdata(pdev); |
| 370 | unsigned int tmp; |
| 371 | int year; |
| 372 | |
| 373 | spin_lock_irq(&rtc->lock); |
| 374 | |
| 375 | /* Reset pre-scaler & stop RTC */ |
| 376 | tmp = readb(rtc->regbase + RCR2); |
| 377 | tmp |= RCR2_RESET; |
Markus Brunner | 699bc66 | 2007-07-26 17:31:28 +0900 | [diff] [blame] | 378 | tmp &= ~RCR2_START; |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 379 | writeb(tmp, rtc->regbase + RCR2); |
| 380 | |
| 381 | writeb(BIN2BCD(tm->tm_sec), rtc->regbase + RSECCNT); |
| 382 | writeb(BIN2BCD(tm->tm_min), rtc->regbase + RMINCNT); |
| 383 | writeb(BIN2BCD(tm->tm_hour), rtc->regbase + RHRCNT); |
| 384 | writeb(BIN2BCD(tm->tm_wday), rtc->regbase + RWKCNT); |
| 385 | writeb(BIN2BCD(tm->tm_mday), rtc->regbase + RDAYCNT); |
Jamie Lenehan | a161479 | 2006-12-08 14:49:30 +0900 | [diff] [blame] | 386 | writeb(BIN2BCD(tm->tm_mon + 1), rtc->regbase + RMONCNT); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 387 | |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 388 | if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) { |
| 389 | year = (BIN2BCD((tm->tm_year + 1900) / 100) << 8) | |
| 390 | BIN2BCD(tm->tm_year % 100); |
| 391 | writew(year, rtc->regbase + RYRCNT); |
| 392 | } else { |
| 393 | year = tm->tm_year % 100; |
| 394 | writeb(BIN2BCD(year), rtc->regbase + RYRCNT); |
| 395 | } |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 396 | |
| 397 | /* Start RTC */ |
| 398 | tmp = readb(rtc->regbase + RCR2); |
| 399 | tmp &= ~RCR2_RESET; |
| 400 | tmp |= RCR2_RTCEN | RCR2_START; |
| 401 | writeb(tmp, rtc->regbase + RCR2); |
| 402 | |
| 403 | spin_unlock_irq(&rtc->lock); |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 408 | static inline int sh_rtc_read_alarm_value(struct sh_rtc *rtc, int reg_off) |
| 409 | { |
| 410 | unsigned int byte; |
| 411 | int value = 0xff; /* return 0xff for ignored values */ |
| 412 | |
| 413 | byte = readb(rtc->regbase + reg_off); |
| 414 | if (byte & AR_ENB) { |
| 415 | byte &= ~AR_ENB; /* strip the enable bit */ |
| 416 | value = BCD2BIN(byte); |
| 417 | } |
| 418 | |
| 419 | return value; |
| 420 | } |
| 421 | |
| 422 | static int sh_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) |
| 423 | { |
| 424 | struct platform_device *pdev = to_platform_device(dev); |
| 425 | struct sh_rtc *rtc = platform_get_drvdata(pdev); |
| 426 | struct rtc_time* tm = &wkalrm->time; |
| 427 | |
| 428 | spin_lock_irq(&rtc->lock); |
| 429 | |
| 430 | tm->tm_sec = sh_rtc_read_alarm_value(rtc, RSECAR); |
| 431 | tm->tm_min = sh_rtc_read_alarm_value(rtc, RMINAR); |
| 432 | tm->tm_hour = sh_rtc_read_alarm_value(rtc, RHRAR); |
| 433 | tm->tm_wday = sh_rtc_read_alarm_value(rtc, RWKAR); |
| 434 | tm->tm_mday = sh_rtc_read_alarm_value(rtc, RDAYAR); |
| 435 | tm->tm_mon = sh_rtc_read_alarm_value(rtc, RMONAR); |
| 436 | if (tm->tm_mon > 0) |
| 437 | tm->tm_mon -= 1; /* RTC is 1-12, tm_mon is 0-11 */ |
| 438 | tm->tm_year = 0xffff; |
| 439 | |
David Brownell | 0d103e9 | 2007-01-10 23:15:32 -0800 | [diff] [blame] | 440 | wkalrm->enabled = (readb(rtc->regbase + RCR1) & RCR1_AIE) ? 1 : 0; |
| 441 | |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 442 | spin_unlock_irq(&rtc->lock); |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | static inline void sh_rtc_write_alarm_value(struct sh_rtc *rtc, |
| 448 | int value, int reg_off) |
| 449 | { |
| 450 | /* < 0 for a value that is ignored */ |
| 451 | if (value < 0) |
| 452 | writeb(0, rtc->regbase + reg_off); |
| 453 | else |
| 454 | writeb(BIN2BCD(value) | AR_ENB, rtc->regbase + reg_off); |
| 455 | } |
| 456 | |
| 457 | static int sh_rtc_check_alarm(struct rtc_time* tm) |
| 458 | { |
| 459 | /* |
| 460 | * The original rtc says anything > 0xc0 is "don't care" or "match |
| 461 | * all" - most users use 0xff but rtc-dev uses -1 for the same thing. |
| 462 | * The original rtc doesn't support years - some things use -1 and |
| 463 | * some 0xffff. We use -1 to make out tests easier. |
| 464 | */ |
| 465 | if (tm->tm_year == 0xffff) |
| 466 | tm->tm_year = -1; |
| 467 | if (tm->tm_mon >= 0xff) |
| 468 | tm->tm_mon = -1; |
| 469 | if (tm->tm_mday >= 0xff) |
| 470 | tm->tm_mday = -1; |
| 471 | if (tm->tm_wday >= 0xff) |
| 472 | tm->tm_wday = -1; |
| 473 | if (tm->tm_hour >= 0xff) |
| 474 | tm->tm_hour = -1; |
| 475 | if (tm->tm_min >= 0xff) |
| 476 | tm->tm_min = -1; |
| 477 | if (tm->tm_sec >= 0xff) |
| 478 | tm->tm_sec = -1; |
| 479 | |
| 480 | if (tm->tm_year > 9999 || |
| 481 | tm->tm_mon >= 12 || |
| 482 | tm->tm_mday == 0 || tm->tm_mday >= 32 || |
| 483 | tm->tm_wday >= 7 || |
| 484 | tm->tm_hour >= 24 || |
| 485 | tm->tm_min >= 60 || |
| 486 | tm->tm_sec >= 60) |
| 487 | return -EINVAL; |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | static int sh_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) |
| 493 | { |
| 494 | struct platform_device *pdev = to_platform_device(dev); |
| 495 | struct sh_rtc *rtc = platform_get_drvdata(pdev); |
| 496 | unsigned int rcr1; |
| 497 | struct rtc_time *tm = &wkalrm->time; |
| 498 | int mon, err; |
| 499 | |
| 500 | err = sh_rtc_check_alarm(tm); |
| 501 | if (unlikely(err < 0)) |
| 502 | return err; |
| 503 | |
| 504 | spin_lock_irq(&rtc->lock); |
| 505 | |
Jamie Lenehan | 15c945c | 2007-01-22 20:40:41 -0800 | [diff] [blame] | 506 | /* disable alarm interrupt and clear the alarm flag */ |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 507 | rcr1 = readb(rtc->regbase + RCR1); |
Jamie Lenehan | 15c945c | 2007-01-22 20:40:41 -0800 | [diff] [blame] | 508 | rcr1 &= ~(RCR1_AF|RCR1_AIE); |
| 509 | writeb(rcr1, rtc->regbase + RCR1); |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 510 | |
| 511 | rtc->rearm_aie = 0; |
| 512 | |
| 513 | /* set alarm time */ |
| 514 | sh_rtc_write_alarm_value(rtc, tm->tm_sec, RSECAR); |
| 515 | sh_rtc_write_alarm_value(rtc, tm->tm_min, RMINAR); |
| 516 | sh_rtc_write_alarm_value(rtc, tm->tm_hour, RHRAR); |
| 517 | sh_rtc_write_alarm_value(rtc, tm->tm_wday, RWKAR); |
| 518 | sh_rtc_write_alarm_value(rtc, tm->tm_mday, RDAYAR); |
| 519 | mon = tm->tm_mon; |
| 520 | if (mon >= 0) |
| 521 | mon += 1; |
| 522 | sh_rtc_write_alarm_value(rtc, mon, RMONAR); |
| 523 | |
Jamie Lenehan | 15c945c | 2007-01-22 20:40:41 -0800 | [diff] [blame] | 524 | if (wkalrm->enabled) { |
| 525 | rcr1 |= RCR1_AIE; |
| 526 | writeb(rcr1, rtc->regbase + RCR1); |
| 527 | } |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 528 | |
| 529 | spin_unlock_irq(&rtc->lock); |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 534 | static struct rtc_class_ops sh_rtc_ops = { |
| 535 | .open = sh_rtc_open, |
| 536 | .release = sh_rtc_release, |
| 537 | .ioctl = sh_rtc_ioctl, |
| 538 | .read_time = sh_rtc_read_time, |
| 539 | .set_time = sh_rtc_set_time, |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 540 | .read_alarm = sh_rtc_read_alarm, |
| 541 | .set_alarm = sh_rtc_set_alarm, |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 542 | .proc = sh_rtc_proc, |
| 543 | }; |
| 544 | |
| 545 | static int __devinit sh_rtc_probe(struct platform_device *pdev) |
| 546 | { |
| 547 | struct sh_rtc *rtc; |
| 548 | struct resource *res; |
| 549 | int ret = -ENOENT; |
| 550 | |
| 551 | rtc = kzalloc(sizeof(struct sh_rtc), GFP_KERNEL); |
| 552 | if (unlikely(!rtc)) |
| 553 | return -ENOMEM; |
| 554 | |
| 555 | spin_lock_init(&rtc->lock); |
| 556 | |
| 557 | rtc->periodic_irq = platform_get_irq(pdev, 0); |
| 558 | if (unlikely(rtc->periodic_irq < 0)) { |
| 559 | dev_err(&pdev->dev, "No IRQ for period\n"); |
| 560 | goto err_badres; |
| 561 | } |
| 562 | |
| 563 | rtc->carry_irq = platform_get_irq(pdev, 1); |
| 564 | if (unlikely(rtc->carry_irq < 0)) { |
| 565 | dev_err(&pdev->dev, "No IRQ for carry\n"); |
| 566 | goto err_badres; |
| 567 | } |
| 568 | |
| 569 | rtc->alarm_irq = platform_get_irq(pdev, 2); |
| 570 | if (unlikely(rtc->alarm_irq < 0)) { |
| 571 | dev_err(&pdev->dev, "No IRQ for alarm\n"); |
| 572 | goto err_badres; |
| 573 | } |
| 574 | |
| 575 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
| 576 | if (unlikely(res == NULL)) { |
| 577 | dev_err(&pdev->dev, "No IO resource\n"); |
| 578 | goto err_badres; |
| 579 | } |
| 580 | |
| 581 | rtc->regsize = res->end - res->start + 1; |
| 582 | |
| 583 | rtc->res = request_mem_region(res->start, rtc->regsize, pdev->name); |
| 584 | if (unlikely(!rtc->res)) { |
| 585 | ret = -EBUSY; |
| 586 | goto err_badres; |
| 587 | } |
| 588 | |
| 589 | rtc->regbase = (void __iomem *)rtc->res->start; |
| 590 | if (unlikely(!rtc->regbase)) { |
| 591 | ret = -EINVAL; |
| 592 | goto err_badmap; |
| 593 | } |
| 594 | |
| 595 | rtc->rtc_dev = rtc_device_register("sh", &pdev->dev, |
| 596 | &sh_rtc_ops, THIS_MODULE); |
Paul Mundt | 29dd0da | 2007-11-07 14:58:09 +0900 | [diff] [blame] | 597 | if (IS_ERR(rtc->rtc_dev)) { |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 598 | ret = PTR_ERR(rtc->rtc_dev); |
| 599 | goto err_badmap; |
| 600 | } |
| 601 | |
Paul Mundt | ad89f87 | 2007-08-03 14:19:58 +0900 | [diff] [blame] | 602 | rtc->capabilities = RTC_DEF_CAPABILITIES; |
| 603 | if (pdev->dev.platform_data) { |
| 604 | struct sh_rtc_platform_info *pinfo = pdev->dev.platform_data; |
| 605 | |
| 606 | /* |
| 607 | * Some CPUs have special capabilities in addition to the |
| 608 | * default set. Add those in here. |
| 609 | */ |
| 610 | rtc->capabilities |= pinfo->capabilities; |
| 611 | } |
| 612 | |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 613 | platform_set_drvdata(pdev, rtc); |
| 614 | |
| 615 | return 0; |
| 616 | |
| 617 | err_badmap: |
| 618 | release_resource(rtc->res); |
| 619 | err_badres: |
| 620 | kfree(rtc); |
| 621 | |
| 622 | return ret; |
| 623 | } |
| 624 | |
| 625 | static int __devexit sh_rtc_remove(struct platform_device *pdev) |
| 626 | { |
| 627 | struct sh_rtc *rtc = platform_get_drvdata(pdev); |
| 628 | |
| 629 | if (likely(rtc->rtc_dev)) |
| 630 | rtc_device_unregister(rtc->rtc_dev); |
| 631 | |
| 632 | sh_rtc_setpie(&pdev->dev, 0); |
| 633 | sh_rtc_setaie(&pdev->dev, 0); |
| 634 | |
| 635 | release_resource(rtc->res); |
| 636 | |
| 637 | platform_set_drvdata(pdev, NULL); |
| 638 | |
| 639 | kfree(rtc); |
| 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | static struct platform_driver sh_rtc_platform_driver = { |
| 644 | .driver = { |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 645 | .name = DRV_NAME, |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 646 | .owner = THIS_MODULE, |
| 647 | }, |
| 648 | .probe = sh_rtc_probe, |
| 649 | .remove = __devexit_p(sh_rtc_remove), |
| 650 | }; |
| 651 | |
| 652 | static int __init sh_rtc_init(void) |
| 653 | { |
| 654 | return platform_driver_register(&sh_rtc_platform_driver); |
| 655 | } |
| 656 | |
| 657 | static void __exit sh_rtc_exit(void) |
| 658 | { |
| 659 | platform_driver_unregister(&sh_rtc_platform_driver); |
| 660 | } |
| 661 | |
| 662 | module_init(sh_rtc_init); |
| 663 | module_exit(sh_rtc_exit); |
| 664 | |
| 665 | MODULE_DESCRIPTION("SuperH on-chip RTC driver"); |
Jamie Lenehan | 1b73e6a | 2006-12-08 15:26:15 +0900 | [diff] [blame] | 666 | MODULE_VERSION(DRV_VERSION); |
| 667 | MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, Jamie Lenehan <lenehan@twibble.org>"); |
Paul Mundt | 317a610 | 2006-09-27 17:13:19 +0900 | [diff] [blame] | 668 | MODULE_LICENSE("GPL"); |