blob: 45f12dcd37160986e014345018f065f9cc75c6fb [file] [log] [blame]
Saeed Bisharadefb4512009-01-06 14:42:24 -08001/*
2 * Driver for the RTC in Marvell SoCs.
3 *
4 * This file is licensed under the terms of the GNU General Public
5 * License version 2. This program is licensed "as is" without any
6 * warranty of any kind, whether express or implied.
7 */
8
9#include <linux/init.h>
10#include <linux/kernel.h>
11#include <linux/rtc.h>
12#include <linux/bcd.h>
13#include <linux/io.h>
14#include <linux/platform_device.h>
15
16
17#define RTC_TIME_REG_OFFS 0
18#define RTC_SECONDS_OFFS 0
19#define RTC_MINUTES_OFFS 8
20#define RTC_HOURS_OFFS 16
21#define RTC_WDAY_OFFS 24
22#define RTC_HOURS_12H_MODE (1 << 22) /* 12 hours mode */
23
24#define RTC_DATE_REG_OFFS 4
25#define RTC_MDAY_OFFS 0
26#define RTC_MONTH_OFFS 8
27#define RTC_YEAR_OFFS 16
28
29
30struct rtc_plat_data {
31 struct rtc_device *rtc;
32 void __iomem *ioaddr;
33};
34
35static int mv_rtc_set_time(struct device *dev, struct rtc_time *tm)
36{
37 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
38 void __iomem *ioaddr = pdata->ioaddr;
39 u32 rtc_reg;
40
41 rtc_reg = (bin2bcd(tm->tm_sec) << RTC_SECONDS_OFFS) |
42 (bin2bcd(tm->tm_min) << RTC_MINUTES_OFFS) |
43 (bin2bcd(tm->tm_hour) << RTC_HOURS_OFFS) |
44 (bin2bcd(tm->tm_wday) << RTC_WDAY_OFFS);
45 writel(rtc_reg, ioaddr + RTC_TIME_REG_OFFS);
46
47 rtc_reg = (bin2bcd(tm->tm_mday) << RTC_MDAY_OFFS) |
48 (bin2bcd(tm->tm_mon + 1) << RTC_MONTH_OFFS) |
49 (bin2bcd(tm->tm_year % 100) << RTC_YEAR_OFFS);
50 writel(rtc_reg, ioaddr + RTC_DATE_REG_OFFS);
51
52 return 0;
53}
54
55static int mv_rtc_read_time(struct device *dev, struct rtc_time *tm)
56{
57 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
58 void __iomem *ioaddr = pdata->ioaddr;
59 u32 rtc_time, rtc_date;
60 unsigned int year, month, day, hour, minute, second, wday;
61
62 rtc_time = readl(ioaddr + RTC_TIME_REG_OFFS);
63 rtc_date = readl(ioaddr + RTC_DATE_REG_OFFS);
64
65 second = rtc_time & 0x7f;
66 minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
67 hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hours mode */
68 wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
69
70 day = rtc_date & 0x3f;
71 month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
72 year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
73
74 tm->tm_sec = bcd2bin(second);
75 tm->tm_min = bcd2bin(minute);
76 tm->tm_hour = bcd2bin(hour);
77 tm->tm_mday = bcd2bin(day);
78 tm->tm_wday = bcd2bin(wday);
79 tm->tm_mon = bcd2bin(month) - 1;
80 /* hw counts from year 2000, but tm_year is relative to 1900 */
81 tm->tm_year = bcd2bin(year) + 100;
82
83 return rtc_valid_tm(tm);
84}
85
86static const struct rtc_class_ops mv_rtc_ops = {
87 .read_time = mv_rtc_read_time,
88 .set_time = mv_rtc_set_time,
89};
90
91static int __init mv_rtc_probe(struct platform_device *pdev)
92{
93 struct resource *res;
94 struct rtc_plat_data *pdata;
95 resource_size_t size;
96 u32 rtc_time;
97
98 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
99 if (!res)
100 return -ENODEV;
101
102 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
103 if (!pdata)
104 return -ENOMEM;
105
106 size = resource_size(res);
107 if (!devm_request_mem_region(&pdev->dev, res->start, size,
108 pdev->name))
109 return -EBUSY;
110
111 pdata->ioaddr = devm_ioremap(&pdev->dev, res->start, size);
112 if (!pdata->ioaddr)
113 return -ENOMEM;
114
115 /* make sure the 24 hours mode is enabled */
116 rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
117 if (rtc_time & RTC_HOURS_12H_MODE) {
118 dev_err(&pdev->dev, "24 Hours mode not supported.\n");
119 return -EINVAL;
120 }
121
122 platform_set_drvdata(pdev, pdata);
123 pdata->rtc = rtc_device_register(pdev->name, &pdev->dev,
124 &mv_rtc_ops, THIS_MODULE);
125 if (IS_ERR(pdata->rtc))
126 return PTR_ERR(pdata->rtc);
127
128 return 0;
129}
130
131static int __exit mv_rtc_remove(struct platform_device *pdev)
132{
133 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
134
135 rtc_device_unregister(pdata->rtc);
136 return 0;
137}
138
139static struct platform_driver mv_rtc_driver = {
140 .remove = __exit_p(mv_rtc_remove),
141 .driver = {
142 .name = "rtc-mv",
143 .owner = THIS_MODULE,
144 },
145};
146
147static __init int mv_init(void)
148{
149 return platform_driver_probe(&mv_rtc_driver, mv_rtc_probe);
150}
151
152static __exit void mv_exit(void)
153{
154 platform_driver_unregister(&mv_rtc_driver);
155}
156
157module_init(mv_init);
158module_exit(mv_exit);
159
160MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
161MODULE_DESCRIPTION("Marvell RTC driver");
162MODULE_LICENSE("GPL");
163MODULE_ALIAS("platform:rtc-mv");