1 // SPDX-License-Identifier: GPL-2.0-only
3 * ST M48T86 / Dallas DS12887 RTC driver
4 * Copyright (c) 2006 Tower Technologies
6 * Author: Alessandro Zummo <a.zummo@towertech.it>
8 * This drivers only supports the clock running in BCD and 24H mode.
9 * If it will be ever adapted to binary and 12H mode, care must be taken
10 * to not introduce bugs.
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/rtc.h>
16 #include <linux/platform_device.h>
17 #include <linux/bcd.h>
20 #define M48T86_SEC 0x00
21 #define M48T86_SECALRM 0x01
22 #define M48T86_MIN 0x02
23 #define M48T86_MINALRM 0x03
24 #define M48T86_HOUR 0x04
25 #define M48T86_HOURALRM 0x05
26 #define M48T86_DOW 0x06 /* 1 = sunday */
27 #define M48T86_DOM 0x07
28 #define M48T86_MONTH 0x08 /* 1 - 12 */
29 #define M48T86_YEAR 0x09 /* 0 - 99 */
32 #define M48T86_B_SET BIT(7)
33 #define M48T86_B_DM BIT(2)
34 #define M48T86_B_H24 BIT(1)
37 #define M48T86_D_VRT BIT(7)
38 #define M48T86_NVRAM(x) (0x0e + (x))
39 #define M48T86_NVRAM_LEN 114
41 struct m48t86_rtc_info
{
42 void __iomem
*index_reg
;
43 void __iomem
*data_reg
;
44 struct rtc_device
*rtc
;
47 static unsigned char m48t86_readb(struct device
*dev
, unsigned long addr
)
49 struct m48t86_rtc_info
*info
= dev_get_drvdata(dev
);
52 writeb(addr
, info
->index_reg
);
53 value
= readb(info
->data_reg
);
58 static void m48t86_writeb(struct device
*dev
,
59 unsigned char value
, unsigned long addr
)
61 struct m48t86_rtc_info
*info
= dev_get_drvdata(dev
);
63 writeb(addr
, info
->index_reg
);
64 writeb(value
, info
->data_reg
);
67 static int m48t86_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
71 reg
= m48t86_readb(dev
, M48T86_B
);
73 if (reg
& M48T86_B_DM
) {
74 /* data (binary) mode */
75 tm
->tm_sec
= m48t86_readb(dev
, M48T86_SEC
);
76 tm
->tm_min
= m48t86_readb(dev
, M48T86_MIN
);
77 tm
->tm_hour
= m48t86_readb(dev
, M48T86_HOUR
) & 0x3f;
78 tm
->tm_mday
= m48t86_readb(dev
, M48T86_DOM
);
80 tm
->tm_mon
= m48t86_readb(dev
, M48T86_MONTH
) - 1;
81 tm
->tm_year
= m48t86_readb(dev
, M48T86_YEAR
) + 100;
82 tm
->tm_wday
= m48t86_readb(dev
, M48T86_DOW
);
85 tm
->tm_sec
= bcd2bin(m48t86_readb(dev
, M48T86_SEC
));
86 tm
->tm_min
= bcd2bin(m48t86_readb(dev
, M48T86_MIN
));
87 tm
->tm_hour
= bcd2bin(m48t86_readb(dev
, M48T86_HOUR
) &
89 tm
->tm_mday
= bcd2bin(m48t86_readb(dev
, M48T86_DOM
));
91 tm
->tm_mon
= bcd2bin(m48t86_readb(dev
, M48T86_MONTH
)) - 1;
92 tm
->tm_year
= bcd2bin(m48t86_readb(dev
, M48T86_YEAR
)) + 100;
93 tm
->tm_wday
= bcd2bin(m48t86_readb(dev
, M48T86_DOW
));
96 /* correct the hour if the clock is in 12h mode */
97 if (!(reg
& M48T86_B_H24
))
98 if (m48t86_readb(dev
, M48T86_HOUR
) & 0x80)
104 static int m48t86_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
108 reg
= m48t86_readb(dev
, M48T86_B
);
110 /* update flag and 24h mode */
111 reg
|= M48T86_B_SET
| M48T86_B_H24
;
112 m48t86_writeb(dev
, reg
, M48T86_B
);
114 if (reg
& M48T86_B_DM
) {
115 /* data (binary) mode */
116 m48t86_writeb(dev
, tm
->tm_sec
, M48T86_SEC
);
117 m48t86_writeb(dev
, tm
->tm_min
, M48T86_MIN
);
118 m48t86_writeb(dev
, tm
->tm_hour
, M48T86_HOUR
);
119 m48t86_writeb(dev
, tm
->tm_mday
, M48T86_DOM
);
120 m48t86_writeb(dev
, tm
->tm_mon
+ 1, M48T86_MONTH
);
121 m48t86_writeb(dev
, tm
->tm_year
% 100, M48T86_YEAR
);
122 m48t86_writeb(dev
, tm
->tm_wday
, M48T86_DOW
);
125 m48t86_writeb(dev
, bin2bcd(tm
->tm_sec
), M48T86_SEC
);
126 m48t86_writeb(dev
, bin2bcd(tm
->tm_min
), M48T86_MIN
);
127 m48t86_writeb(dev
, bin2bcd(tm
->tm_hour
), M48T86_HOUR
);
128 m48t86_writeb(dev
, bin2bcd(tm
->tm_mday
), M48T86_DOM
);
129 m48t86_writeb(dev
, bin2bcd(tm
->tm_mon
+ 1), M48T86_MONTH
);
130 m48t86_writeb(dev
, bin2bcd(tm
->tm_year
% 100), M48T86_YEAR
);
131 m48t86_writeb(dev
, bin2bcd(tm
->tm_wday
), M48T86_DOW
);
135 reg
&= ~M48T86_B_SET
;
136 m48t86_writeb(dev
, reg
, M48T86_B
);
141 static int m48t86_rtc_proc(struct device
*dev
, struct seq_file
*seq
)
145 reg
= m48t86_readb(dev
, M48T86_B
);
147 seq_printf(seq
, "mode\t\t: %s\n",
148 (reg
& M48T86_B_DM
) ? "binary" : "bcd");
150 reg
= m48t86_readb(dev
, M48T86_D
);
152 seq_printf(seq
, "battery\t\t: %s\n",
153 (reg
& M48T86_D_VRT
) ? "ok" : "exhausted");
158 static const struct rtc_class_ops m48t86_rtc_ops
= {
159 .read_time
= m48t86_rtc_read_time
,
160 .set_time
= m48t86_rtc_set_time
,
161 .proc
= m48t86_rtc_proc
,
164 static int m48t86_nvram_read(void *priv
, unsigned int off
, void *buf
,
167 struct device
*dev
= priv
;
170 for (i
= 0; i
< count
; i
++)
171 ((u8
*)buf
)[i
] = m48t86_readb(dev
, M48T86_NVRAM(off
+ i
));
176 static int m48t86_nvram_write(void *priv
, unsigned int off
, void *buf
,
179 struct device
*dev
= priv
;
182 for (i
= 0; i
< count
; i
++)
183 m48t86_writeb(dev
, ((u8
*)buf
)[i
], M48T86_NVRAM(off
+ i
));
189 * The RTC is an optional feature at purchase time on some Technologic Systems
190 * boards. Verify that it actually exists by checking if the last two bytes
191 * of the NVRAM can be changed.
193 * This is based on the method used in their rtc7800.c example.
195 static bool m48t86_verify_chip(struct platform_device
*pdev
)
197 unsigned int offset0
= M48T86_NVRAM(M48T86_NVRAM_LEN
- 2);
198 unsigned int offset1
= M48T86_NVRAM(M48T86_NVRAM_LEN
- 1);
199 unsigned char tmp0
, tmp1
;
201 tmp0
= m48t86_readb(&pdev
->dev
, offset0
);
202 tmp1
= m48t86_readb(&pdev
->dev
, offset1
);
204 m48t86_writeb(&pdev
->dev
, 0x00, offset0
);
205 m48t86_writeb(&pdev
->dev
, 0x55, offset1
);
206 if (m48t86_readb(&pdev
->dev
, offset1
) == 0x55) {
207 m48t86_writeb(&pdev
->dev
, 0xaa, offset1
);
208 if (m48t86_readb(&pdev
->dev
, offset1
) == 0xaa &&
209 m48t86_readb(&pdev
->dev
, offset0
) == 0x00) {
210 m48t86_writeb(&pdev
->dev
, tmp0
, offset0
);
211 m48t86_writeb(&pdev
->dev
, tmp1
, offset1
);
219 static int m48t86_rtc_probe(struct platform_device
*pdev
)
221 struct m48t86_rtc_info
*info
;
224 struct nvmem_config m48t86_nvmem_cfg
= {
225 .name
= "m48t86_nvram",
228 .size
= M48T86_NVRAM_LEN
,
229 .reg_read
= m48t86_nvram_read
,
230 .reg_write
= m48t86_nvram_write
,
234 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
238 info
->index_reg
= devm_platform_ioremap_resource(pdev
, 0);
239 if (IS_ERR(info
->index_reg
))
240 return PTR_ERR(info
->index_reg
);
242 info
->data_reg
= devm_platform_ioremap_resource(pdev
, 1);
243 if (IS_ERR(info
->data_reg
))
244 return PTR_ERR(info
->data_reg
);
246 dev_set_drvdata(&pdev
->dev
, info
);
248 if (!m48t86_verify_chip(pdev
)) {
249 dev_info(&pdev
->dev
, "RTC not present\n");
253 info
->rtc
= devm_rtc_allocate_device(&pdev
->dev
);
254 if (IS_ERR(info
->rtc
))
255 return PTR_ERR(info
->rtc
);
257 info
->rtc
->ops
= &m48t86_rtc_ops
;
259 err
= devm_rtc_register_device(info
->rtc
);
263 devm_rtc_nvmem_register(info
->rtc
, &m48t86_nvmem_cfg
);
265 /* read battery status */
266 reg
= m48t86_readb(&pdev
->dev
, M48T86_D
);
267 dev_info(&pdev
->dev
, "battery %s\n",
268 (reg
& M48T86_D_VRT
) ? "ok" : "exhausted");
273 static const struct of_device_id m48t86_rtc_of_ids
[] = {
274 { .compatible
= "st,m48t86" },
277 MODULE_DEVICE_TABLE(of
, m48t86_rtc_of_ids
);
279 static struct platform_driver m48t86_rtc_platform_driver
= {
281 .name
= "rtc-m48t86",
282 .of_match_table
= m48t86_rtc_of_ids
,
284 .probe
= m48t86_rtc_probe
,
287 module_platform_driver(m48t86_rtc_platform_driver
);
289 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
290 MODULE_DESCRIPTION("M48T86 RTC driver");
291 MODULE_LICENSE("GPL");
292 MODULE_ALIAS("platform:rtc-m48t86");