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