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>
23 #include <linux/err.h>
26 u8 pad
[0x7ff8]; /* starts at 0x7ff8 */
37 #define M48T35_RTC_SET 0x80
38 #define M48T35_RTC_READ 0x40
41 struct rtc_device
*rtc
;
42 struct m48t35_rtc __iomem
*reg
;
44 unsigned long baseaddr
;
48 static int m48t35_read_time(struct device
*dev
, struct rtc_time
*tm
)
50 struct m48t35_priv
*priv
= dev_get_drvdata(dev
);
54 * Only the values that we read from the RTC are set. We leave
55 * tm_wday, tm_yday and tm_isdst untouched. Even though the
56 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
57 * by the RTC when initially set to a non-zero value.
59 spin_lock_irq(&priv
->lock
);
60 control
= readb(&priv
->reg
->control
);
61 writeb(control
| M48T35_RTC_READ
, &priv
->reg
->control
);
62 tm
->tm_sec
= readb(&priv
->reg
->sec
);
63 tm
->tm_min
= readb(&priv
->reg
->min
);
64 tm
->tm_hour
= readb(&priv
->reg
->hour
);
65 tm
->tm_mday
= readb(&priv
->reg
->date
);
66 tm
->tm_mon
= readb(&priv
->reg
->month
);
67 tm
->tm_year
= readb(&priv
->reg
->year
);
68 writeb(control
, &priv
->reg
->control
);
69 spin_unlock_irq(&priv
->lock
);
71 tm
->tm_sec
= bcd2bin(tm
->tm_sec
);
72 tm
->tm_min
= bcd2bin(tm
->tm_min
);
73 tm
->tm_hour
= bcd2bin(tm
->tm_hour
);
74 tm
->tm_mday
= bcd2bin(tm
->tm_mday
);
75 tm
->tm_mon
= bcd2bin(tm
->tm_mon
);
76 tm
->tm_year
= bcd2bin(tm
->tm_year
);
79 * Account for differences between how the RTC uses the values
80 * and how they are defined in a struct rtc_time;
83 if (tm
->tm_year
<= 69)
90 static int m48t35_set_time(struct device
*dev
, struct rtc_time
*tm
)
92 struct m48t35_priv
*priv
= dev_get_drvdata(dev
);
93 unsigned char mon
, day
, hrs
, min
, sec
;
97 yrs
= tm
->tm_year
+ 1900;
98 mon
= tm
->tm_mon
+ 1; /* tm_mon starts at zero */
108 if (yrs
> 255) /* They are unsigned */
124 spin_lock_irq(&priv
->lock
);
125 control
= readb(&priv
->reg
->control
);
126 writeb(control
| M48T35_RTC_SET
, &priv
->reg
->control
);
127 writeb(yrs
, &priv
->reg
->year
);
128 writeb(mon
, &priv
->reg
->month
);
129 writeb(day
, &priv
->reg
->date
);
130 writeb(hrs
, &priv
->reg
->hour
);
131 writeb(min
, &priv
->reg
->min
);
132 writeb(sec
, &priv
->reg
->sec
);
133 writeb(control
, &priv
->reg
->control
);
134 spin_unlock_irq(&priv
->lock
);
138 static const struct rtc_class_ops m48t35_ops
= {
139 .read_time
= m48t35_read_time
,
140 .set_time
= m48t35_set_time
,
143 static int m48t35_probe(struct platform_device
*pdev
)
145 struct resource
*res
;
146 struct m48t35_priv
*priv
;
148 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
151 priv
= devm_kzalloc(&pdev
->dev
, sizeof(struct m48t35_priv
), GFP_KERNEL
);
155 priv
->size
= resource_size(res
);
157 * kludge: remove the #ifndef after ioc3 resource
158 * conflicts are resolved
160 #ifndef CONFIG_SGI_IP27
161 if (!devm_request_mem_region(&pdev
->dev
, res
->start
, priv
->size
,
165 priv
->baseaddr
= res
->start
;
166 priv
->reg
= devm_ioremap(&pdev
->dev
, priv
->baseaddr
, priv
->size
);
170 spin_lock_init(&priv
->lock
);
172 platform_set_drvdata(pdev
, priv
);
174 priv
->rtc
= devm_rtc_device_register(&pdev
->dev
, "m48t35",
175 &m48t35_ops
, THIS_MODULE
);
176 return PTR_ERR_OR_ZERO(priv
->rtc
);
179 static struct platform_driver m48t35_platform_driver
= {
181 .name
= "rtc-m48t35",
183 .probe
= m48t35_probe
,
186 module_platform_driver(m48t35_platform_driver
);
188 MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
189 MODULE_DESCRIPTION("M48T35 RTC driver");
190 MODULE_LICENSE("GPL");
191 MODULE_ALIAS("platform:rtc-m48t35");