2 * Driver for the SGS-Thomson M48T35 Timekeeper RAM chip
4 * Copyright (C) 2000 Silicon Graphics, Inc.
5 * Written by Ulf Carlsson (ulfc@engr.sgi.com)
7 * Copyright (C) 2008 Thomas Bogendoerfer
9 * Based on code written by Paul Gortmaker.
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
17 #include <linux/module.h>
18 #include <linux/rtc.h>
19 #include <linux/platform_device.h>
20 #include <linux/bcd.h>
22 #define DRV_VERSION "1.0"
25 u8 pad[0x7ff8]; /* starts at 0x7ff8 */
36 #define M48T35_RTC_SET 0x80
37 #define M48T35_RTC_READ 0x40
40 struct rtc_device *rtc;
41 struct m48t35_rtc __iomem *reg;
43 unsigned long baseaddr;
47 static int m48t35_read_time(struct device *dev, struct rtc_time *tm)
49 struct m48t35_priv *priv = dev_get_drvdata(dev);
53 * Only the values that we read from the RTC are set. We leave
54 * tm_wday, tm_yday and tm_isdst untouched. Even though the
55 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
56 * by the RTC when initially set to a non-zero value.
58 spin_lock_irq(&priv->lock);
59 control = readb(&priv->reg->control);
60 writeb(control | M48T35_RTC_READ, &priv->reg->control);
61 tm->tm_sec = readb(&priv->reg->sec);
62 tm->tm_min = readb(&priv->reg->min);
63 tm->tm_hour = readb(&priv->reg->hour);
64 tm->tm_mday = readb(&priv->reg->date);
65 tm->tm_mon = readb(&priv->reg->month);
66 tm->tm_year = readb(&priv->reg->year);
67 writeb(control, &priv->reg->control);
68 spin_unlock_irq(&priv->lock);
70 tm->tm_sec = bcd2bin(tm->tm_sec);
71 tm->tm_min = bcd2bin(tm->tm_min);
72 tm->tm_hour = bcd2bin(tm->tm_hour);
73 tm->tm_mday = bcd2bin(tm->tm_mday);
74 tm->tm_mon = bcd2bin(tm->tm_mon);
75 tm->tm_year = bcd2bin(tm->tm_year);
78 * Account for differences between how the RTC uses the values
79 * and how they are defined in a struct rtc_time;
82 if (tm->tm_year <= 69)
86 return rtc_valid_tm(tm);
89 static int m48t35_set_time(struct device *dev, struct rtc_time *tm)
91 struct m48t35_priv *priv = dev_get_drvdata(dev);
92 unsigned char mon, day, hrs, min, sec;
96 yrs = tm->tm_year + 1900;
97 mon = tm->tm_mon + 1; /* tm_mon starts at zero */
107 if (yrs > 255) /* They are unsigned */
123 spin_lock_irq(&priv->lock);
124 control = readb(&priv->reg->control);
125 writeb(control | M48T35_RTC_SET, &priv->reg->control);
126 writeb(yrs, &priv->reg->year);
127 writeb(mon, &priv->reg->month);
128 writeb(day, &priv->reg->date);
129 writeb(hrs, &priv->reg->hour);
130 writeb(min, &priv->reg->min);
131 writeb(sec, &priv->reg->sec);
132 writeb(control, &priv->reg->control);
133 spin_unlock_irq(&priv->lock);
137 static const struct rtc_class_ops m48t35_ops = {
138 .read_time = m48t35_read_time,
139 .set_time = m48t35_set_time,
142 static int __devinit m48t35_probe(struct platform_device *pdev)
144 struct rtc_device *rtc;
145 struct resource *res;
146 struct m48t35_priv *priv;
149 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
152 priv = kzalloc(sizeof(struct m48t35_priv), GFP_KERNEL);
156 priv->size = res->end - res->start + 1;
158 * kludge: remove the #ifndef after ioc3 resource
159 * conflicts are resolved
161 #ifndef CONFIG_SGI_IP27
162 if (!request_mem_region(res->start, priv->size, pdev->name)) {
167 priv->baseaddr = res->start;
168 priv->reg = ioremap(priv->baseaddr, priv->size);
173 spin_lock_init(&priv->lock);
174 rtc = rtc_device_register("m48t35", &pdev->dev,
175 &m48t35_ops, THIS_MODULE);
181 platform_set_drvdata(pdev, priv);
186 rtc_device_unregister(priv->rtc);
190 release_mem_region(priv->baseaddr, priv->size);
195 static int __devexit m48t35_remove(struct platform_device *pdev)
197 struct m48t35_priv *priv = platform_get_drvdata(pdev);
199 rtc_device_unregister(priv->rtc);
201 #ifndef CONFIG_SGI_IP27
202 release_mem_region(priv->baseaddr, priv->size);
208 static struct platform_driver m48t35_platform_driver = {
210 .name = "rtc-m48t35",
211 .owner = THIS_MODULE,
213 .probe = m48t35_probe,
214 .remove = __devexit_p(m48t35_remove),
217 static int __init m48t35_init(void)
219 return platform_driver_register(&m48t35_platform_driver);
222 static void __exit m48t35_exit(void)
224 platform_driver_unregister(&m48t35_platform_driver);
227 MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
228 MODULE_DESCRIPTION("M48T35 RTC driver");
229 MODULE_LICENSE("GPL");
230 MODULE_VERSION(DRV_VERSION);
231 MODULE_ALIAS("platform:rtc-m48t35");
233 module_init(m48t35_init);
234 module_exit(m48t35_exit);