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/slab.h>
20 #include <linux/platform_device.h>
21 #include <linux/bcd.h>
24 #define DRV_VERSION "1.0"
27 u8 pad
[0x7ff8]; /* starts at 0x7ff8 */
38 #define M48T35_RTC_SET 0x80
39 #define M48T35_RTC_READ 0x40
42 struct rtc_device
*rtc
;
43 struct m48t35_rtc __iomem
*reg
;
45 unsigned long baseaddr
;
49 static int m48t35_read_time(struct device
*dev
, struct rtc_time
*tm
)
51 struct m48t35_priv
*priv
= dev_get_drvdata(dev
);
55 * Only the values that we read from the RTC are set. We leave
56 * tm_wday, tm_yday and tm_isdst untouched. Even though the
57 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
58 * by the RTC when initially set to a non-zero value.
60 spin_lock_irq(&priv
->lock
);
61 control
= readb(&priv
->reg
->control
);
62 writeb(control
| M48T35_RTC_READ
, &priv
->reg
->control
);
63 tm
->tm_sec
= readb(&priv
->reg
->sec
);
64 tm
->tm_min
= readb(&priv
->reg
->min
);
65 tm
->tm_hour
= readb(&priv
->reg
->hour
);
66 tm
->tm_mday
= readb(&priv
->reg
->date
);
67 tm
->tm_mon
= readb(&priv
->reg
->month
);
68 tm
->tm_year
= readb(&priv
->reg
->year
);
69 writeb(control
, &priv
->reg
->control
);
70 spin_unlock_irq(&priv
->lock
);
72 tm
->tm_sec
= bcd2bin(tm
->tm_sec
);
73 tm
->tm_min
= bcd2bin(tm
->tm_min
);
74 tm
->tm_hour
= bcd2bin(tm
->tm_hour
);
75 tm
->tm_mday
= bcd2bin(tm
->tm_mday
);
76 tm
->tm_mon
= bcd2bin(tm
->tm_mon
);
77 tm
->tm_year
= bcd2bin(tm
->tm_year
);
80 * Account for differences between how the RTC uses the values
81 * and how they are defined in a struct rtc_time;
84 if (tm
->tm_year
<= 69)
88 return rtc_valid_tm(tm
);
91 static int m48t35_set_time(struct device
*dev
, struct rtc_time
*tm
)
93 struct m48t35_priv
*priv
= dev_get_drvdata(dev
);
94 unsigned char mon
, day
, hrs
, min
, sec
;
98 yrs
= tm
->tm_year
+ 1900;
99 mon
= tm
->tm_mon
+ 1; /* tm_mon starts at zero */
109 if (yrs
> 255) /* They are unsigned */
125 spin_lock_irq(&priv
->lock
);
126 control
= readb(&priv
->reg
->control
);
127 writeb(control
| M48T35_RTC_SET
, &priv
->reg
->control
);
128 writeb(yrs
, &priv
->reg
->year
);
129 writeb(mon
, &priv
->reg
->month
);
130 writeb(day
, &priv
->reg
->date
);
131 writeb(hrs
, &priv
->reg
->hour
);
132 writeb(min
, &priv
->reg
->min
);
133 writeb(sec
, &priv
->reg
->sec
);
134 writeb(control
, &priv
->reg
->control
);
135 spin_unlock_irq(&priv
->lock
);
139 static const struct rtc_class_ops m48t35_ops
= {
140 .read_time
= m48t35_read_time
,
141 .set_time
= m48t35_set_time
,
144 static int __devinit
m48t35_probe(struct platform_device
*pdev
)
146 struct resource
*res
;
147 struct m48t35_priv
*priv
;
150 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
153 priv
= kzalloc(sizeof(struct m48t35_priv
), GFP_KERNEL
);
157 priv
->size
= resource_size(res
);
159 * kludge: remove the #ifndef after ioc3 resource
160 * conflicts are resolved
162 #ifndef CONFIG_SGI_IP27
163 if (!request_mem_region(res
->start
, priv
->size
, pdev
->name
)) {
168 priv
->baseaddr
= res
->start
;
169 priv
->reg
= ioremap(priv
->baseaddr
, priv
->size
);
175 spin_lock_init(&priv
->lock
);
177 platform_set_drvdata(pdev
, priv
);
179 priv
->rtc
= rtc_device_register("m48t35", &pdev
->dev
,
180 &m48t35_ops
, THIS_MODULE
);
181 if (IS_ERR(priv
->rtc
)) {
182 ret
= PTR_ERR(priv
->rtc
);
192 release_mem_region(priv
->baseaddr
, priv
->size
);
197 static int __devexit
m48t35_remove(struct platform_device
*pdev
)
199 struct m48t35_priv
*priv
= platform_get_drvdata(pdev
);
201 rtc_device_unregister(priv
->rtc
);
203 #ifndef CONFIG_SGI_IP27
204 release_mem_region(priv
->baseaddr
, priv
->size
);
210 static struct platform_driver m48t35_platform_driver
= {
212 .name
= "rtc-m48t35",
213 .owner
= THIS_MODULE
,
215 .probe
= m48t35_probe
,
216 .remove
= __devexit_p(m48t35_remove
),
219 module_platform_driver(m48t35_platform_driver
);
221 MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
222 MODULE_DESCRIPTION("M48T35 RTC driver");
223 MODULE_LICENSE("GPL");
224 MODULE_VERSION(DRV_VERSION
);
225 MODULE_ALIAS("platform:rtc-m48t35");