]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/rtc/rtc-m48t86.c
rtc: m48t86: verify that the RTC is actually present
[karo-tx-linux.git] / drivers / rtc / rtc-m48t86.c
1 /*
2  * ST M48T86 / Dallas DS12887 RTC driver
3  * Copyright (c) 2006 Tower Technologies
4  *
5  * Author: Alessandro Zummo <a.zummo@towertech.it>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This drivers only supports the clock running in BCD and 24H mode.
12  * If it will be ever adapted to binary and 12H mode, care must be taken
13  * to not introduce bugs.
14  */
15
16 #include <linux/module.h>
17 #include <linux/rtc.h>
18 #include <linux/platform_device.h>
19 #include <linux/platform_data/rtc-m48t86.h>
20 #include <linux/bcd.h>
21 #include <linux/io.h>
22
23 #define M48T86_SEC              0x00
24 #define M48T86_SECALRM          0x01
25 #define M48T86_MIN              0x02
26 #define M48T86_MINALRM          0x03
27 #define M48T86_HOUR             0x04
28 #define M48T86_HOURALRM         0x05
29 #define M48T86_DOW              0x06 /* 1 = sunday */
30 #define M48T86_DOM              0x07
31 #define M48T86_MONTH            0x08 /* 1 - 12 */
32 #define M48T86_YEAR             0x09 /* 0 - 99 */
33 #define M48T86_A                0x0a
34 #define M48T86_B                0x0b
35 #define M48T86_B_SET            BIT(7)
36 #define M48T86_B_DM             BIT(2)
37 #define M48T86_B_H24            BIT(1)
38 #define M48T86_C                0x0c
39 #define M48T86_D                0x0d
40 #define M48T86_D_VRT            BIT(7)
41 #define M48T86_NVRAM(x)         (0x0e + (x))
42 #define M48T86_NVRAM_LEN        114
43
44 struct m48t86_rtc_info {
45         void __iomem *index_reg;
46         void __iomem *data_reg;
47         struct rtc_device *rtc;
48         struct m48t86_ops *ops;
49 };
50
51 static unsigned char m48t86_readb(struct device *dev, unsigned long addr)
52 {
53         struct m48t86_rtc_info *info = dev_get_drvdata(dev);
54         unsigned char value;
55
56         if (info->ops) {
57                 value = info->ops->readbyte(addr);
58         } else {
59                 writeb(addr, info->index_reg);
60                 value = readb(info->data_reg);
61         }
62         return value;
63 }
64
65 static void m48t86_writeb(struct device *dev,
66                           unsigned char value, unsigned long addr)
67 {
68         struct m48t86_rtc_info *info = dev_get_drvdata(dev);
69
70         if (info->ops) {
71                 info->ops->writebyte(value, addr);
72         } else {
73                 writeb(addr, info->index_reg);
74                 writeb(value, info->data_reg);
75         }
76 }
77
78 static int m48t86_rtc_read_time(struct device *dev, struct rtc_time *tm)
79 {
80         unsigned char reg;
81
82         reg = m48t86_readb(dev, M48T86_B);
83
84         if (reg & M48T86_B_DM) {
85                 /* data (binary) mode */
86                 tm->tm_sec      = m48t86_readb(dev, M48T86_SEC);
87                 tm->tm_min      = m48t86_readb(dev, M48T86_MIN);
88                 tm->tm_hour     = m48t86_readb(dev, M48T86_HOUR) & 0x3f;
89                 tm->tm_mday     = m48t86_readb(dev, M48T86_DOM);
90                 /* tm_mon is 0-11 */
91                 tm->tm_mon      = m48t86_readb(dev, M48T86_MONTH) - 1;
92                 tm->tm_year     = m48t86_readb(dev, M48T86_YEAR) + 100;
93                 tm->tm_wday     = m48t86_readb(dev, M48T86_DOW);
94         } else {
95                 /* bcd mode */
96                 tm->tm_sec      = bcd2bin(m48t86_readb(dev, M48T86_SEC));
97                 tm->tm_min      = bcd2bin(m48t86_readb(dev, M48T86_MIN));
98                 tm->tm_hour     = bcd2bin(m48t86_readb(dev, M48T86_HOUR) &
99                                           0x3f);
100                 tm->tm_mday     = bcd2bin(m48t86_readb(dev, M48T86_DOM));
101                 /* tm_mon is 0-11 */
102                 tm->tm_mon      = bcd2bin(m48t86_readb(dev, M48T86_MONTH)) - 1;
103                 tm->tm_year     = bcd2bin(m48t86_readb(dev, M48T86_YEAR)) + 100;
104                 tm->tm_wday     = bcd2bin(m48t86_readb(dev, M48T86_DOW));
105         }
106
107         /* correct the hour if the clock is in 12h mode */
108         if (!(reg & M48T86_B_H24))
109                 if (m48t86_readb(dev, M48T86_HOUR) & 0x80)
110                         tm->tm_hour += 12;
111
112         return rtc_valid_tm(tm);
113 }
114
115 static int m48t86_rtc_set_time(struct device *dev, struct rtc_time *tm)
116 {
117         unsigned char reg;
118
119         reg = m48t86_readb(dev, M48T86_B);
120
121         /* update flag and 24h mode */
122         reg |= M48T86_B_SET | M48T86_B_H24;
123         m48t86_writeb(dev, reg, M48T86_B);
124
125         if (reg & M48T86_B_DM) {
126                 /* data (binary) mode */
127                 m48t86_writeb(dev, tm->tm_sec, M48T86_SEC);
128                 m48t86_writeb(dev, tm->tm_min, M48T86_MIN);
129                 m48t86_writeb(dev, tm->tm_hour, M48T86_HOUR);
130                 m48t86_writeb(dev, tm->tm_mday, M48T86_DOM);
131                 m48t86_writeb(dev, tm->tm_mon + 1, M48T86_MONTH);
132                 m48t86_writeb(dev, tm->tm_year % 100, M48T86_YEAR);
133                 m48t86_writeb(dev, tm->tm_wday, M48T86_DOW);
134         } else {
135                 /* bcd mode */
136                 m48t86_writeb(dev, bin2bcd(tm->tm_sec), M48T86_SEC);
137                 m48t86_writeb(dev, bin2bcd(tm->tm_min), M48T86_MIN);
138                 m48t86_writeb(dev, bin2bcd(tm->tm_hour), M48T86_HOUR);
139                 m48t86_writeb(dev, bin2bcd(tm->tm_mday), M48T86_DOM);
140                 m48t86_writeb(dev, bin2bcd(tm->tm_mon + 1), M48T86_MONTH);
141                 m48t86_writeb(dev, bin2bcd(tm->tm_year % 100), M48T86_YEAR);
142                 m48t86_writeb(dev, bin2bcd(tm->tm_wday), M48T86_DOW);
143         }
144
145         /* update ended */
146         reg &= ~M48T86_B_SET;
147         m48t86_writeb(dev, reg, M48T86_B);
148
149         return 0;
150 }
151
152 static int m48t86_rtc_proc(struct device *dev, struct seq_file *seq)
153 {
154         unsigned char reg;
155
156         reg = m48t86_readb(dev, M48T86_B);
157
158         seq_printf(seq, "mode\t\t: %s\n",
159                    (reg & M48T86_B_DM) ? "binary" : "bcd");
160
161         reg = m48t86_readb(dev, M48T86_D);
162
163         seq_printf(seq, "battery\t\t: %s\n",
164                    (reg & M48T86_D_VRT) ? "ok" : "exhausted");
165
166         return 0;
167 }
168
169 static const struct rtc_class_ops m48t86_rtc_ops = {
170         .read_time      = m48t86_rtc_read_time,
171         .set_time       = m48t86_rtc_set_time,
172         .proc           = m48t86_rtc_proc,
173 };
174
175 static ssize_t m48t86_nvram_read(struct file *filp, struct kobject *kobj,
176                                  struct bin_attribute *attr,
177                                  char *buf, loff_t off, size_t count)
178 {
179         struct device *dev = kobj_to_dev(kobj);
180         unsigned int i;
181
182         for (i = 0; i < count; i++)
183                 buf[i] = m48t86_readb(dev, M48T86_NVRAM(off + i));
184
185         return count;
186 }
187
188 static ssize_t m48t86_nvram_write(struct file *filp, struct kobject *kobj,
189                                   struct bin_attribute *attr,
190                                   char *buf, loff_t off, size_t count)
191 {
192         struct device *dev = kobj_to_dev(kobj);
193         unsigned int i;
194
195         for (i = 0; i < count; i++)
196                 m48t86_writeb(dev, buf[i], M48T86_NVRAM(off + i));
197
198         return count;
199 }
200
201 static BIN_ATTR(nvram, 0644, m48t86_nvram_read, m48t86_nvram_write,
202                 M48T86_NVRAM_LEN);
203
204 /*
205  * The RTC is an optional feature at purchase time on some Technologic Systems
206  * boards. Verify that it actually exists by checking if the last two bytes
207  * of the NVRAM can be changed.
208  *
209  * This is based on the method used in their rtc7800.c example.
210  */
211 static bool m48t86_verify_chip(struct platform_device *pdev)
212 {
213         unsigned int offset0 = M48T86_NVRAM(M48T86_NVRAM_LEN - 2);
214         unsigned int offset1 = M48T86_NVRAM(M48T86_NVRAM_LEN - 1);
215         unsigned char tmp0, tmp1;
216
217         tmp0 = m48t86_readb(&pdev->dev, offset0);
218         tmp1 = m48t86_readb(&pdev->dev, offset1);
219
220         m48t86_writeb(&pdev->dev, 0x00, offset0);
221         m48t86_writeb(&pdev->dev, 0x55, offset1);
222         if (m48t86_readb(&pdev->dev, offset1) == 0x55) {
223                 m48t86_writeb(&pdev->dev, 0xaa, offset1);
224                 if (m48t86_readb(&pdev->dev, offset1) == 0xaa &&
225                     m48t86_readb(&pdev->dev, offset0) == 0x00) {
226                         m48t86_writeb(&pdev->dev, tmp0, offset0);
227                         m48t86_writeb(&pdev->dev, tmp1, offset1);
228
229                         return true;
230                 }
231         }
232         return false;
233 }
234
235 static int m48t86_rtc_probe(struct platform_device *pdev)
236 {
237         struct m48t86_rtc_info *info;
238         unsigned char reg;
239
240         info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
241         if (!info)
242                 return -ENOMEM;
243
244         info->ops = dev_get_platdata(&pdev->dev);
245         if (!info->ops) {
246                 struct resource *res;
247
248                 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
249                 if (!res)
250                         return -ENODEV;
251                 info->index_reg = devm_ioremap_resource(&pdev->dev, res);
252                 if (IS_ERR(info->index_reg))
253                         return PTR_ERR(info->index_reg);
254
255                 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
256                 if (!res)
257                         return -ENODEV;
258                 info->data_reg = devm_ioremap_resource(&pdev->dev, res);
259                 if (IS_ERR(info->data_reg))
260                         return PTR_ERR(info->data_reg);
261         }
262
263         dev_set_drvdata(&pdev->dev, info);
264
265         if (!m48t86_verify_chip(pdev)) {
266                 dev_info(&pdev->dev, "RTC not present\n");
267                 return -ENODEV;
268         }
269
270         info->rtc = devm_rtc_device_register(&pdev->dev, "m48t86",
271                                              &m48t86_rtc_ops, THIS_MODULE);
272         if (IS_ERR(info->rtc))
273                 return PTR_ERR(info->rtc);
274
275         /* read battery status */
276         reg = m48t86_readb(&pdev->dev, M48T86_D);
277         dev_info(&pdev->dev, "battery %s\n",
278                  (reg & M48T86_D_VRT) ? "ok" : "exhausted");
279
280         if (device_create_bin_file(&pdev->dev, &bin_attr_nvram))
281                 dev_err(&pdev->dev, "failed to create nvram sysfs entry\n");
282
283         return 0;
284 }
285
286 static int m48t86_rtc_remove(struct platform_device *pdev)
287 {
288         device_remove_bin_file(&pdev->dev, &bin_attr_nvram);
289         return 0;
290 }
291
292 static struct platform_driver m48t86_rtc_platform_driver = {
293         .driver         = {
294                 .name   = "rtc-m48t86",
295         },
296         .probe          = m48t86_rtc_probe,
297         .remove         = m48t86_rtc_remove,
298 };
299
300 module_platform_driver(m48t86_rtc_platform_driver);
301
302 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
303 MODULE_DESCRIPTION("M48T86 RTC driver");
304 MODULE_LICENSE("GPL");
305 MODULE_ALIAS("platform:rtc-m48t86");