blob: fccbecbb2c98e092287c6a863193d067439b78a6 [file] [log] [blame]
Thomas Hommel02964112007-07-21 04:37:58 -07001/*
2 * A RTC driver for the Simtek STK17TA8
3 *
Martyn Welchab4364d312010-04-14 12:08:28 +01004 * By Thomas Hommel <thomas.hommel@ge.com>
Thomas Hommel02964112007-07-21 04:37:58 -07005 *
6 * Based on the DS1553 driver from
7 * Atsushi Nemoto <anemo@mba.ocn.ne.jp>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/bcd.h>
15#include <linux/init.h>
16#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/gfp.h>
Thomas Hommel02964112007-07-21 04:37:58 -070018#include <linux/delay.h>
19#include <linux/jiffies.h>
20#include <linux/interrupt.h>
21#include <linux/rtc.h>
22#include <linux/platform_device.h>
23#include <linux/io.h>
Paul Gortmaker21138522011-05-27 09:57:25 -040024#include <linux/module.h>
Thomas Hommel02964112007-07-21 04:37:58 -070025
Thomas Hommel02964112007-07-21 04:37:58 -070026#define RTC_REG_SIZE 0x20000
27#define RTC_OFFSET 0x1fff0
28
29#define RTC_FLAGS (RTC_OFFSET + 0)
30#define RTC_CENTURY (RTC_OFFSET + 1)
31#define RTC_SECONDS_ALARM (RTC_OFFSET + 2)
32#define RTC_MINUTES_ALARM (RTC_OFFSET + 3)
33#define RTC_HOURS_ALARM (RTC_OFFSET + 4)
34#define RTC_DATE_ALARM (RTC_OFFSET + 5)
35#define RTC_INTERRUPTS (RTC_OFFSET + 6)
36#define RTC_WATCHDOG (RTC_OFFSET + 7)
37#define RTC_CALIBRATION (RTC_OFFSET + 8)
38#define RTC_SECONDS (RTC_OFFSET + 9)
39#define RTC_MINUTES (RTC_OFFSET + 10)
40#define RTC_HOURS (RTC_OFFSET + 11)
41#define RTC_DAY (RTC_OFFSET + 12)
42#define RTC_DATE (RTC_OFFSET + 13)
43#define RTC_MONTH (RTC_OFFSET + 14)
44#define RTC_YEAR (RTC_OFFSET + 15)
45
46#define RTC_SECONDS_MASK 0x7f
47#define RTC_DAY_MASK 0x07
48#define RTC_CAL_MASK 0x3f
49
50/* Bits in the Calibration register */
51#define RTC_STOP 0x80
52
53/* Bits in the Flags register */
54#define RTC_FLAGS_AF 0x40
55#define RTC_FLAGS_PF 0x20
56#define RTC_WRITE 0x02
57#define RTC_READ 0x01
58
59/* Bits in the Interrupts register */
60#define RTC_INTS_AIE 0x40
61
62struct rtc_plat_data {
63 struct rtc_device *rtc;
64 void __iomem *ioaddr;
Thomas Hommel02964112007-07-21 04:37:58 -070065 unsigned long last_jiffies;
66 int irq;
67 unsigned int irqen;
68 int alrm_sec;
69 int alrm_min;
70 int alrm_hour;
71 int alrm_mday;
Atsushi Nemoto31515202009-12-15 16:46:04 -080072 spinlock_t lock;
Thomas Hommel02964112007-07-21 04:37:58 -070073};
74
75static int stk17ta8_rtc_set_time(struct device *dev, struct rtc_time *tm)
76{
Wolfram Sang85368bb2018-04-19 16:06:14 +020077 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
Thomas Hommel02964112007-07-21 04:37:58 -070078 void __iomem *ioaddr = pdata->ioaddr;
79 u8 flags;
80
81 flags = readb(pdata->ioaddr + RTC_FLAGS);
82 writeb(flags | RTC_WRITE, pdata->ioaddr + RTC_FLAGS);
83
Adrian Bunkfe20ba72008-10-18 20:28:41 -070084 writeb(bin2bcd(tm->tm_year % 100), ioaddr + RTC_YEAR);
85 writeb(bin2bcd(tm->tm_mon + 1), ioaddr + RTC_MONTH);
86 writeb(bin2bcd(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY);
87 writeb(bin2bcd(tm->tm_mday), ioaddr + RTC_DATE);
88 writeb(bin2bcd(tm->tm_hour), ioaddr + RTC_HOURS);
89 writeb(bin2bcd(tm->tm_min), ioaddr + RTC_MINUTES);
90 writeb(bin2bcd(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS);
91 writeb(bin2bcd((tm->tm_year + 1900) / 100), ioaddr + RTC_CENTURY);
Thomas Hommel02964112007-07-21 04:37:58 -070092
93 writeb(flags & ~RTC_WRITE, pdata->ioaddr + RTC_FLAGS);
94 return 0;
95}
96
97static int stk17ta8_rtc_read_time(struct device *dev, struct rtc_time *tm)
98{
Wolfram Sang85368bb2018-04-19 16:06:14 +020099 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
Thomas Hommel02964112007-07-21 04:37:58 -0700100 void __iomem *ioaddr = pdata->ioaddr;
101 unsigned int year, month, day, hour, minute, second, week;
102 unsigned int century;
103 u8 flags;
104
105 /* give enough time to update RTC in case of continuous read */
106 if (pdata->last_jiffies == jiffies)
107 msleep(1);
108 pdata->last_jiffies = jiffies;
109
110 flags = readb(pdata->ioaddr + RTC_FLAGS);
111 writeb(flags | RTC_READ, ioaddr + RTC_FLAGS);
112 second = readb(ioaddr + RTC_SECONDS) & RTC_SECONDS_MASK;
113 minute = readb(ioaddr + RTC_MINUTES);
114 hour = readb(ioaddr + RTC_HOURS);
115 day = readb(ioaddr + RTC_DATE);
116 week = readb(ioaddr + RTC_DAY) & RTC_DAY_MASK;
117 month = readb(ioaddr + RTC_MONTH);
118 year = readb(ioaddr + RTC_YEAR);
119 century = readb(ioaddr + RTC_CENTURY);
120 writeb(flags & ~RTC_READ, ioaddr + RTC_FLAGS);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700121 tm->tm_sec = bcd2bin(second);
122 tm->tm_min = bcd2bin(minute);
123 tm->tm_hour = bcd2bin(hour);
124 tm->tm_mday = bcd2bin(day);
125 tm->tm_wday = bcd2bin(week);
126 tm->tm_mon = bcd2bin(month) - 1;
Thomas Hommel02964112007-07-21 04:37:58 -0700127 /* year is 1900 + tm->tm_year */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700128 tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900;
Thomas Hommel02964112007-07-21 04:37:58 -0700129
Thomas Hommel02964112007-07-21 04:37:58 -0700130 return 0;
131}
132
133static void stk17ta8_rtc_update_alarm(struct rtc_plat_data *pdata)
134{
135 void __iomem *ioaddr = pdata->ioaddr;
136 unsigned long irqflags;
137 u8 flags;
138
Atsushi Nemoto31515202009-12-15 16:46:04 -0800139 spin_lock_irqsave(&pdata->lock, irqflags);
Thomas Hommel02964112007-07-21 04:37:58 -0700140
141 flags = readb(ioaddr + RTC_FLAGS);
142 writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS);
143
144 writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ?
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700145 0x80 : bin2bcd(pdata->alrm_mday),
Thomas Hommel02964112007-07-21 04:37:58 -0700146 ioaddr + RTC_DATE_ALARM);
147 writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ?
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700148 0x80 : bin2bcd(pdata->alrm_hour),
Thomas Hommel02964112007-07-21 04:37:58 -0700149 ioaddr + RTC_HOURS_ALARM);
150 writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ?
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700151 0x80 : bin2bcd(pdata->alrm_min),
Thomas Hommel02964112007-07-21 04:37:58 -0700152 ioaddr + RTC_MINUTES_ALARM);
153 writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ?
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700154 0x80 : bin2bcd(pdata->alrm_sec),
Thomas Hommel02964112007-07-21 04:37:58 -0700155 ioaddr + RTC_SECONDS_ALARM);
156 writeb(pdata->irqen ? RTC_INTS_AIE : 0, ioaddr + RTC_INTERRUPTS);
157 readb(ioaddr + RTC_FLAGS); /* clear interrupts */
158 writeb(flags & ~RTC_WRITE, ioaddr + RTC_FLAGS);
Atsushi Nemoto31515202009-12-15 16:46:04 -0800159 spin_unlock_irqrestore(&pdata->lock, irqflags);
Thomas Hommel02964112007-07-21 04:37:58 -0700160}
161
162static int stk17ta8_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
163{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200164 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
Thomas Hommel02964112007-07-21 04:37:58 -0700165
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800166 if (pdata->irq <= 0)
Thomas Hommel02964112007-07-21 04:37:58 -0700167 return -EINVAL;
168 pdata->alrm_mday = alrm->time.tm_mday;
169 pdata->alrm_hour = alrm->time.tm_hour;
170 pdata->alrm_min = alrm->time.tm_min;
171 pdata->alrm_sec = alrm->time.tm_sec;
172 if (alrm->enabled)
173 pdata->irqen |= RTC_AF;
174 stk17ta8_rtc_update_alarm(pdata);
175 return 0;
176}
177
178static int stk17ta8_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
179{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200180 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
Thomas Hommel02964112007-07-21 04:37:58 -0700181
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800182 if (pdata->irq <= 0)
Thomas Hommel02964112007-07-21 04:37:58 -0700183 return -EINVAL;
184 alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday;
185 alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour;
186 alrm->time.tm_min = pdata->alrm_min < 0 ? 0 : pdata->alrm_min;
187 alrm->time.tm_sec = pdata->alrm_sec < 0 ? 0 : pdata->alrm_sec;
188 alrm->enabled = (pdata->irqen & RTC_AF) ? 1 : 0;
189 return 0;
190}
191
192static irqreturn_t stk17ta8_rtc_interrupt(int irq, void *dev_id)
193{
194 struct platform_device *pdev = dev_id;
195 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
196 void __iomem *ioaddr = pdata->ioaddr;
Atsushi Nemoto31515202009-12-15 16:46:04 -0800197 unsigned long events = 0;
Thomas Hommel02964112007-07-21 04:37:58 -0700198
Atsushi Nemoto31515202009-12-15 16:46:04 -0800199 spin_lock(&pdata->lock);
Thomas Hommel02964112007-07-21 04:37:58 -0700200 /* read and clear interrupt */
Atsushi Nemoto31515202009-12-15 16:46:04 -0800201 if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF) {
202 events = RTC_IRQF;
203 if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80)
204 events |= RTC_UF;
205 else
206 events |= RTC_AF;
Alexander Shiyan0d719152014-04-03 14:50:20 -0700207 rtc_update_irq(pdata->rtc, 1, events);
Atsushi Nemoto31515202009-12-15 16:46:04 -0800208 }
209 spin_unlock(&pdata->lock);
210 return events ? IRQ_HANDLED : IRQ_NONE;
Thomas Hommel02964112007-07-21 04:37:58 -0700211}
212
Atsushi Nemoto31515202009-12-15 16:46:04 -0800213static int stk17ta8_rtc_alarm_irq_enable(struct device *dev,
214 unsigned int enabled)
Thomas Hommel02964112007-07-21 04:37:58 -0700215{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200216 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
Thomas Hommel02964112007-07-21 04:37:58 -0700217
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800218 if (pdata->irq <= 0)
Atsushi Nemoto31515202009-12-15 16:46:04 -0800219 return -EINVAL;
220 if (enabled)
Thomas Hommel02964112007-07-21 04:37:58 -0700221 pdata->irqen |= RTC_AF;
Atsushi Nemoto31515202009-12-15 16:46:04 -0800222 else
223 pdata->irqen &= ~RTC_AF;
224 stk17ta8_rtc_update_alarm(pdata);
Thomas Hommel02964112007-07-21 04:37:58 -0700225 return 0;
226}
227
228static const struct rtc_class_ops stk17ta8_rtc_ops = {
Atsushi Nemoto31515202009-12-15 16:46:04 -0800229 .read_time = stk17ta8_rtc_read_time,
230 .set_time = stk17ta8_rtc_set_time,
231 .read_alarm = stk17ta8_rtc_read_alarm,
232 .set_alarm = stk17ta8_rtc_set_alarm,
233 .alarm_irq_enable = stk17ta8_rtc_alarm_irq_enable,
Thomas Hommel02964112007-07-21 04:37:58 -0700234};
235
Alexandre Bellonid7ca4292018-02-12 23:47:54 +0100236static int stk17ta8_nvram_read(void *priv, unsigned int pos, void *val,
237 size_t bytes)
Thomas Hommel02964112007-07-21 04:37:58 -0700238{
Alexandre Bellonid7ca4292018-02-12 23:47:54 +0100239 struct rtc_plat_data *pdata = priv;
Thomas Hommel02964112007-07-21 04:37:58 -0700240 void __iomem *ioaddr = pdata->ioaddr;
Alexandre Bellonid7ca4292018-02-12 23:47:54 +0100241 u8 *buf = val;
Thomas Hommel02964112007-07-21 04:37:58 -0700242
Alexandre Bellonid7ca4292018-02-12 23:47:54 +0100243 for (; bytes; bytes--)
Thomas Hommel02964112007-07-21 04:37:58 -0700244 *buf++ = readb(ioaddr + pos++);
Alexandre Bellonid7ca4292018-02-12 23:47:54 +0100245 return 0;
Thomas Hommel02964112007-07-21 04:37:58 -0700246}
247
Alexandre Bellonid7ca4292018-02-12 23:47:54 +0100248static int stk17ta8_nvram_write(void *priv, unsigned int pos, void *val,
249 size_t bytes)
Thomas Hommel02964112007-07-21 04:37:58 -0700250{
Alexandre Bellonid7ca4292018-02-12 23:47:54 +0100251 struct rtc_plat_data *pdata = priv;
Thomas Hommel02964112007-07-21 04:37:58 -0700252 void __iomem *ioaddr = pdata->ioaddr;
Alexandre Bellonid7ca4292018-02-12 23:47:54 +0100253 u8 *buf = val;
Thomas Hommel02964112007-07-21 04:37:58 -0700254
Alexandre Bellonid7ca4292018-02-12 23:47:54 +0100255 for (; bytes; bytes--)
Thomas Hommel02964112007-07-21 04:37:58 -0700256 writeb(*buf++, ioaddr + pos++);
Alexandre Bellonid7ca4292018-02-12 23:47:54 +0100257 return 0;
Thomas Hommel02964112007-07-21 04:37:58 -0700258}
259
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800260static int stk17ta8_rtc_probe(struct platform_device *pdev)
Thomas Hommel02964112007-07-21 04:37:58 -0700261{
Thomas Hommel02964112007-07-21 04:37:58 -0700262 struct resource *res;
263 unsigned int cal;
264 unsigned int flags;
265 struct rtc_plat_data *pdata;
Atsushi Nemoto31515202009-12-15 16:46:04 -0800266 void __iomem *ioaddr;
Thomas Hommel02964112007-07-21 04:37:58 -0700267 int ret = 0;
Alexandre Bellonid7ca4292018-02-12 23:47:54 +0100268 struct nvmem_config nvmem_cfg = {
269 .name = "stk17ta8_nvram",
270 .word_size = 1,
271 .stride = 1,
272 .size = RTC_OFFSET,
273 .reg_read = stk17ta8_nvram_read,
274 .reg_write = stk17ta8_nvram_write,
275 };
Thomas Hommel02964112007-07-21 04:37:58 -0700276
Atsushi Nemoto31515202009-12-15 16:46:04 -0800277 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
Thomas Hommel02964112007-07-21 04:37:58 -0700278 if (!pdata)
279 return -ENOMEM;
Julia Lawall7c1d69e2013-09-11 14:24:27 -0700280
281 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
282 ioaddr = devm_ioremap_resource(&pdev->dev, res);
283 if (IS_ERR(ioaddr))
284 return PTR_ERR(ioaddr);
Thomas Hommel02964112007-07-21 04:37:58 -0700285 pdata->ioaddr = ioaddr;
286 pdata->irq = platform_get_irq(pdev, 0);
287
288 /* turn RTC on if it was not on */
289 cal = readb(ioaddr + RTC_CALIBRATION);
290 if (cal & RTC_STOP) {
291 cal &= RTC_CAL_MASK;
292 flags = readb(ioaddr + RTC_FLAGS);
293 writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS);
294 writeb(cal, ioaddr + RTC_CALIBRATION);
295 writeb(flags & ~RTC_WRITE, ioaddr + RTC_FLAGS);
296 }
297 if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_PF)
298 dev_warn(&pdev->dev, "voltage-low detected.\n");
299
Atsushi Nemoto31515202009-12-15 16:46:04 -0800300 spin_lock_init(&pdata->lock);
301 pdata->last_jiffies = jiffies;
302 platform_set_drvdata(pdev, pdata);
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800303 if (pdata->irq > 0) {
Thomas Hommel02964112007-07-21 04:37:58 -0700304 writeb(0, ioaddr + RTC_INTERRUPTS);
Atsushi Nemoto31515202009-12-15 16:46:04 -0800305 if (devm_request_irq(&pdev->dev, pdata->irq,
306 stk17ta8_rtc_interrupt,
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700307 IRQF_SHARED,
Thomas Hommel02964112007-07-21 04:37:58 -0700308 pdev->name, pdev) < 0) {
309 dev_warn(&pdev->dev, "interrupt not available.\n");
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800310 pdata->irq = 0;
Thomas Hommel02964112007-07-21 04:37:58 -0700311 }
312 }
313
Alexandre Belloni91cb6772018-02-12 23:47:52 +0100314 pdata->rtc = devm_rtc_allocate_device(&pdev->dev);
Atsushi Nemoto31515202009-12-15 16:46:04 -0800315 if (IS_ERR(pdata->rtc))
316 return PTR_ERR(pdata->rtc);
Alessandro Zummob74d2ca2009-12-15 16:45:53 -0800317
Alexandre Belloni91cb6772018-02-12 23:47:52 +0100318 pdata->rtc->ops = &stk17ta8_rtc_ops;
Alexandre Bellonid7ca4292018-02-12 23:47:54 +0100319 pdata->rtc->nvram_old_abi = true;
Alexandre Belloni91cb6772018-02-12 23:47:52 +0100320
Alexandre Bellonid7ca4292018-02-12 23:47:54 +0100321 nvmem_cfg.priv = pdata;
322 ret = rtc_nvmem_register(pdata->rtc, &nvmem_cfg);
Alexandre Belloni91cb6772018-02-12 23:47:52 +0100323 if (ret)
324 return ret;
325
Alexandre Bellonid7ca4292018-02-12 23:47:54 +0100326 return rtc_register_device(pdata->rtc);
Thomas Hommel02964112007-07-21 04:37:58 -0700327}
328
Kay Sieversad28a072008-04-10 21:29:25 -0700329/* work with hotplug and coldplug */
330MODULE_ALIAS("platform:stk17ta8");
331
Thomas Hommel02964112007-07-21 04:37:58 -0700332static struct platform_driver stk17ta8_rtc_driver = {
333 .probe = stk17ta8_rtc_probe,
Thomas Hommel02964112007-07-21 04:37:58 -0700334 .driver = {
335 .name = "stk17ta8",
Thomas Hommel02964112007-07-21 04:37:58 -0700336 },
337};
338
Axel Lin0c4eae62012-01-10 15:10:48 -0800339module_platform_driver(stk17ta8_rtc_driver);
Thomas Hommel02964112007-07-21 04:37:58 -0700340
Martyn Welchab4364d312010-04-14 12:08:28 +0100341MODULE_AUTHOR("Thomas Hommel <thomas.hommel@ge.com>");
Thomas Hommel02964112007-07-21 04:37:58 -0700342MODULE_DESCRIPTION("Simtek STK17TA8 RTC driver");
343MODULE_LICENSE("GPL");