libertas: rename and clean up DownloadcommandToStation
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / rtc / rtc-stk17ta8.c
bloba265da7c6ff8db2908b0686bf376157a3017157f
1 /*
2 * A RTC driver for the Simtek STK17TA8
4 * By Thomas Hommel <thomas.hommel@gefanuc.com>
6 * Based on the DS1553 driver from
7 * Atsushi Nemoto <anemo@mba.ocn.ne.jp>
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.
14 #include <linux/bcd.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/delay.h>
18 #include <linux/jiffies.h>
19 #include <linux/interrupt.h>
20 #include <linux/rtc.h>
21 #include <linux/platform_device.h>
22 #include <linux/io.h>
24 #define DRV_VERSION "0.1"
26 #define RTC_REG_SIZE 0x20000
27 #define RTC_OFFSET 0x1fff0
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)
46 #define RTC_SECONDS_MASK 0x7f
47 #define RTC_DAY_MASK 0x07
48 #define RTC_CAL_MASK 0x3f
50 /* Bits in the Calibration register */
51 #define RTC_STOP 0x80
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
59 /* Bits in the Interrupts register */
60 #define RTC_INTS_AIE 0x40
62 struct rtc_plat_data {
63 struct rtc_device *rtc;
64 void __iomem *ioaddr;
65 unsigned long baseaddr;
66 unsigned long last_jiffies;
67 int irq;
68 unsigned int irqen;
69 int alrm_sec;
70 int alrm_min;
71 int alrm_hour;
72 int alrm_mday;
75 static int stk17ta8_rtc_set_time(struct device *dev, struct rtc_time *tm)
77 struct platform_device *pdev = to_platform_device(dev);
78 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
79 void __iomem *ioaddr = pdata->ioaddr;
80 u8 flags;
82 flags = readb(pdata->ioaddr + RTC_FLAGS);
83 writeb(flags | RTC_WRITE, pdata->ioaddr + RTC_FLAGS);
85 writeb(BIN2BCD(tm->tm_year % 100), ioaddr + RTC_YEAR);
86 writeb(BIN2BCD(tm->tm_mon + 1), ioaddr + RTC_MONTH);
87 writeb(BIN2BCD(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY);
88 writeb(BIN2BCD(tm->tm_mday), ioaddr + RTC_DATE);
89 writeb(BIN2BCD(tm->tm_hour), ioaddr + RTC_HOURS);
90 writeb(BIN2BCD(tm->tm_min), ioaddr + RTC_MINUTES);
91 writeb(BIN2BCD(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS);
92 writeb(BIN2BCD((tm->tm_year + 1900) / 100), ioaddr + RTC_CENTURY);
94 writeb(flags & ~RTC_WRITE, pdata->ioaddr + RTC_FLAGS);
95 return 0;
98 static int stk17ta8_rtc_read_time(struct device *dev, struct rtc_time *tm)
100 struct platform_device *pdev = to_platform_device(dev);
101 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
102 void __iomem *ioaddr = pdata->ioaddr;
103 unsigned int year, month, day, hour, minute, second, week;
104 unsigned int century;
105 u8 flags;
107 /* give enough time to update RTC in case of continuous read */
108 if (pdata->last_jiffies == jiffies)
109 msleep(1);
110 pdata->last_jiffies = jiffies;
112 flags = readb(pdata->ioaddr + RTC_FLAGS);
113 writeb(flags | RTC_READ, ioaddr + RTC_FLAGS);
114 second = readb(ioaddr + RTC_SECONDS) & RTC_SECONDS_MASK;
115 minute = readb(ioaddr + RTC_MINUTES);
116 hour = readb(ioaddr + RTC_HOURS);
117 day = readb(ioaddr + RTC_DATE);
118 week = readb(ioaddr + RTC_DAY) & RTC_DAY_MASK;
119 month = readb(ioaddr + RTC_MONTH);
120 year = readb(ioaddr + RTC_YEAR);
121 century = readb(ioaddr + RTC_CENTURY);
122 writeb(flags & ~RTC_READ, ioaddr + RTC_FLAGS);
123 tm->tm_sec = BCD2BIN(second);
124 tm->tm_min = BCD2BIN(minute);
125 tm->tm_hour = BCD2BIN(hour);
126 tm->tm_mday = BCD2BIN(day);
127 tm->tm_wday = BCD2BIN(week);
128 tm->tm_mon = BCD2BIN(month) - 1;
129 /* year is 1900 + tm->tm_year */
130 tm->tm_year = BCD2BIN(year) + BCD2BIN(century) * 100 - 1900;
132 if (rtc_valid_tm(tm) < 0) {
133 dev_err(dev, "retrieved date/time is not valid.\n");
134 rtc_time_to_tm(0, tm);
136 return 0;
139 static void stk17ta8_rtc_update_alarm(struct rtc_plat_data *pdata)
141 void __iomem *ioaddr = pdata->ioaddr;
142 unsigned long irqflags;
143 u8 flags;
145 spin_lock_irqsave(&pdata->rtc->irq_lock, irqflags);
147 flags = readb(ioaddr + RTC_FLAGS);
148 writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS);
150 writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ?
151 0x80 : BIN2BCD(pdata->alrm_mday),
152 ioaddr + RTC_DATE_ALARM);
153 writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ?
154 0x80 : BIN2BCD(pdata->alrm_hour),
155 ioaddr + RTC_HOURS_ALARM);
156 writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ?
157 0x80 : BIN2BCD(pdata->alrm_min),
158 ioaddr + RTC_MINUTES_ALARM);
159 writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ?
160 0x80 : BIN2BCD(pdata->alrm_sec),
161 ioaddr + RTC_SECONDS_ALARM);
162 writeb(pdata->irqen ? RTC_INTS_AIE : 0, ioaddr + RTC_INTERRUPTS);
163 readb(ioaddr + RTC_FLAGS); /* clear interrupts */
164 writeb(flags & ~RTC_WRITE, ioaddr + RTC_FLAGS);
165 spin_unlock_irqrestore(&pdata->rtc->irq_lock, irqflags);
168 static int stk17ta8_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
170 struct platform_device *pdev = to_platform_device(dev);
171 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
173 if (pdata->irq < 0)
174 return -EINVAL;
175 pdata->alrm_mday = alrm->time.tm_mday;
176 pdata->alrm_hour = alrm->time.tm_hour;
177 pdata->alrm_min = alrm->time.tm_min;
178 pdata->alrm_sec = alrm->time.tm_sec;
179 if (alrm->enabled)
180 pdata->irqen |= RTC_AF;
181 stk17ta8_rtc_update_alarm(pdata);
182 return 0;
185 static int stk17ta8_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
187 struct platform_device *pdev = to_platform_device(dev);
188 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
190 if (pdata->irq < 0)
191 return -EINVAL;
192 alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday;
193 alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour;
194 alrm->time.tm_min = pdata->alrm_min < 0 ? 0 : pdata->alrm_min;
195 alrm->time.tm_sec = pdata->alrm_sec < 0 ? 0 : pdata->alrm_sec;
196 alrm->enabled = (pdata->irqen & RTC_AF) ? 1 : 0;
197 return 0;
200 static irqreturn_t stk17ta8_rtc_interrupt(int irq, void *dev_id)
202 struct platform_device *pdev = dev_id;
203 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
204 void __iomem *ioaddr = pdata->ioaddr;
205 unsigned long events = RTC_IRQF;
207 /* read and clear interrupt */
208 if (!(readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF))
209 return IRQ_NONE;
210 if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80)
211 events |= RTC_UF;
212 else
213 events |= RTC_AF;
214 rtc_update_irq(pdata->rtc, 1, events);
215 return IRQ_HANDLED;
218 static void stk17ta8_rtc_release(struct device *dev)
220 struct platform_device *pdev = to_platform_device(dev);
221 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
223 if (pdata->irq >= 0) {
224 pdata->irqen = 0;
225 stk17ta8_rtc_update_alarm(pdata);
229 static int stk17ta8_rtc_ioctl(struct device *dev, unsigned int cmd,
230 unsigned long arg)
232 struct platform_device *pdev = to_platform_device(dev);
233 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
235 if (pdata->irq < 0)
236 return -ENOIOCTLCMD; /* fall back into rtc-dev's emulation */
237 switch (cmd) {
238 case RTC_AIE_OFF:
239 pdata->irqen &= ~RTC_AF;
240 stk17ta8_rtc_update_alarm(pdata);
241 break;
242 case RTC_AIE_ON:
243 pdata->irqen |= RTC_AF;
244 stk17ta8_rtc_update_alarm(pdata);
245 break;
246 default:
247 return -ENOIOCTLCMD;
249 return 0;
252 static const struct rtc_class_ops stk17ta8_rtc_ops = {
253 .read_time = stk17ta8_rtc_read_time,
254 .set_time = stk17ta8_rtc_set_time,
255 .read_alarm = stk17ta8_rtc_read_alarm,
256 .set_alarm = stk17ta8_rtc_set_alarm,
257 .release = stk17ta8_rtc_release,
258 .ioctl = stk17ta8_rtc_ioctl,
261 static ssize_t stk17ta8_nvram_read(struct kobject *kobj,
262 struct bin_attribute *attr, char *buf,
263 loff_t pos, size_t size)
265 struct platform_device *pdev =
266 to_platform_device(container_of(kobj, struct device, kobj));
267 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
268 void __iomem *ioaddr = pdata->ioaddr;
269 ssize_t count;
271 for (count = 0; size > 0 && pos < RTC_OFFSET; count++, size--)
272 *buf++ = readb(ioaddr + pos++);
273 return count;
276 static ssize_t stk17ta8_nvram_write(struct kobject *kobj,
277 struct bin_attribute *attr, char *buf,
278 loff_t pos, size_t size)
280 struct platform_device *pdev =
281 to_platform_device(container_of(kobj, struct device, kobj));
282 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
283 void __iomem *ioaddr = pdata->ioaddr;
284 ssize_t count;
286 for (count = 0; size > 0 && pos < RTC_OFFSET; count++, size--)
287 writeb(*buf++, ioaddr + pos++);
288 return count;
291 static struct bin_attribute stk17ta8_nvram_attr = {
292 .attr = {
293 .name = "nvram",
294 .mode = S_IRUGO | S_IWUSR,
295 .owner = THIS_MODULE,
297 .size = RTC_OFFSET,
298 .read = stk17ta8_nvram_read,
299 .write = stk17ta8_nvram_write,
302 static int __init stk17ta8_rtc_probe(struct platform_device *pdev)
304 struct rtc_device *rtc;
305 struct resource *res;
306 unsigned int cal;
307 unsigned int flags;
308 struct rtc_plat_data *pdata;
309 void __iomem *ioaddr = NULL;
310 int ret = 0;
312 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
313 if (!res)
314 return -ENODEV;
316 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
317 if (!pdata)
318 return -ENOMEM;
319 pdata->irq = -1;
320 if (!request_mem_region(res->start, RTC_REG_SIZE, pdev->name)) {
321 ret = -EBUSY;
322 goto out;
324 pdata->baseaddr = res->start;
325 ioaddr = ioremap(pdata->baseaddr, RTC_REG_SIZE);
326 if (!ioaddr) {
327 ret = -ENOMEM;
328 goto out;
330 pdata->ioaddr = ioaddr;
331 pdata->irq = platform_get_irq(pdev, 0);
333 /* turn RTC on if it was not on */
334 cal = readb(ioaddr + RTC_CALIBRATION);
335 if (cal & RTC_STOP) {
336 cal &= RTC_CAL_MASK;
337 flags = readb(ioaddr + RTC_FLAGS);
338 writeb(flags | RTC_WRITE, ioaddr + RTC_FLAGS);
339 writeb(cal, ioaddr + RTC_CALIBRATION);
340 writeb(flags & ~RTC_WRITE, ioaddr + RTC_FLAGS);
342 if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_PF)
343 dev_warn(&pdev->dev, "voltage-low detected.\n");
345 if (pdata->irq >= 0) {
346 writeb(0, ioaddr + RTC_INTERRUPTS);
347 if (request_irq(pdata->irq, stk17ta8_rtc_interrupt,
348 IRQF_DISABLED | IRQF_SHARED,
349 pdev->name, pdev) < 0) {
350 dev_warn(&pdev->dev, "interrupt not available.\n");
351 pdata->irq = -1;
355 rtc = rtc_device_register(pdev->name, &pdev->dev,
356 &stk17ta8_rtc_ops, THIS_MODULE);
357 if (IS_ERR(rtc)) {
358 ret = PTR_ERR(rtc);
359 goto out;
361 pdata->rtc = rtc;
362 pdata->last_jiffies = jiffies;
363 platform_set_drvdata(pdev, pdata);
364 ret = sysfs_create_bin_file(&pdev->dev.kobj, &stk17ta8_nvram_attr);
365 if (ret)
366 goto out;
367 return 0;
368 out:
369 if (pdata->rtc)
370 rtc_device_unregister(pdata->rtc);
371 if (pdata->irq >= 0)
372 free_irq(pdata->irq, pdev);
373 if (ioaddr)
374 iounmap(ioaddr);
375 if (pdata->baseaddr)
376 release_mem_region(pdata->baseaddr, RTC_REG_SIZE);
377 kfree(pdata);
378 return ret;
381 static int __devexit stk17ta8_rtc_remove(struct platform_device *pdev)
383 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
385 sysfs_remove_bin_file(&pdev->dev.kobj, &stk17ta8_nvram_attr);
386 rtc_device_unregister(pdata->rtc);
387 if (pdata->irq >= 0) {
388 writeb(0, pdata->ioaddr + RTC_INTERRUPTS);
389 free_irq(pdata->irq, pdev);
391 iounmap(pdata->ioaddr);
392 release_mem_region(pdata->baseaddr, RTC_REG_SIZE);
393 kfree(pdata);
394 return 0;
397 static struct platform_driver stk17ta8_rtc_driver = {
398 .probe = stk17ta8_rtc_probe,
399 .remove = __devexit_p(stk17ta8_rtc_remove),
400 .driver = {
401 .name = "stk17ta8",
402 .owner = THIS_MODULE,
406 static __init int stk17ta8_init(void)
408 return platform_driver_register(&stk17ta8_rtc_driver);
411 static __exit void stk17ta8_exit(void)
413 return platform_driver_unregister(&stk17ta8_rtc_driver);
416 module_init(stk17ta8_init);
417 module_exit(stk17ta8_exit);
419 MODULE_AUTHOR("Thomas Hommel <thomas.hommel@gefanuc.com>");
420 MODULE_DESCRIPTION("Simtek STK17TA8 RTC driver");
421 MODULE_LICENSE("GPL");
422 MODULE_VERSION(DRV_VERSION);