1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the SGS-Thomson M48T35 Timekeeper RAM chip
5 * Copyright (C) 2000 Silicon Graphics, Inc.
8 * Copyright (C) 2008 Thomas Bogendoerfer
10 * Based on code written by Paul Gortmaker.
13 #include <linux/module.h>
14 #include <linux/rtc.h>
15 #include <linux/slab.h>
16 #include <linux/platform_device.h>
17 #include <linux/bcd.h>
19 #include <linux/err.h>
22 u8 pad[0x7ff8]; /* starts at 0x7ff8 */
33 #define M48T35_RTC_SET 0x80
34 #define M48T35_RTC_READ 0x40
37 struct rtc_device *rtc;
38 struct m48t35_rtc __iomem *reg;
40 unsigned long baseaddr;
44 static int m48t35_read_time(struct device *dev, struct rtc_time *tm)
46 struct m48t35_priv *priv = dev_get_drvdata(dev);
50 * Only the values that we read from the RTC are set. We leave
51 * tm_wday, tm_yday and tm_isdst untouched. Even though the
52 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
53 * by the RTC when initially set to a non-zero value.
55 spin_lock_irq(&priv->lock);
56 control = readb(&priv->reg->control);
57 writeb(control | M48T35_RTC_READ, &priv->reg->control);
58 tm->tm_sec = readb(&priv->reg->sec);
59 tm->tm_min = readb(&priv->reg->min);
60 tm->tm_hour = readb(&priv->reg->hour);
61 tm->tm_mday = readb(&priv->reg->date);
62 tm->tm_mon = readb(&priv->reg->month);
63 tm->tm_year = readb(&priv->reg->year);
64 writeb(control, &priv->reg->control);
65 spin_unlock_irq(&priv->lock);
67 tm->tm_sec = bcd2bin(tm->tm_sec);
68 tm->tm_min = bcd2bin(tm->tm_min);
69 tm->tm_hour = bcd2bin(tm->tm_hour);
70 tm->tm_mday = bcd2bin(tm->tm_mday);
71 tm->tm_mon = bcd2bin(tm->tm_mon);
72 tm->tm_year = bcd2bin(tm->tm_year);
75 * Account for differences between how the RTC uses the values
76 * and how they are defined in a struct rtc_time;
79 if (tm->tm_year <= 69)
86 static int m48t35_set_time(struct device *dev, struct rtc_time *tm)
88 struct m48t35_priv *priv = dev_get_drvdata(dev);
89 unsigned char mon, day, hrs, min, sec;
93 yrs = tm->tm_year + 1900;
94 mon = tm->tm_mon + 1; /* tm_mon starts at zero */
104 if (yrs > 255) /* They are unsigned */
120 spin_lock_irq(&priv->lock);
121 control = readb(&priv->reg->control);
122 writeb(control | M48T35_RTC_SET, &priv->reg->control);
123 writeb(yrs, &priv->reg->year);
124 writeb(mon, &priv->reg->month);
125 writeb(day, &priv->reg->date);
126 writeb(hrs, &priv->reg->hour);
127 writeb(min, &priv->reg->min);
128 writeb(sec, &priv->reg->sec);
129 writeb(control, &priv->reg->control);
130 spin_unlock_irq(&priv->lock);
134 static const struct rtc_class_ops m48t35_ops = {
135 .read_time = m48t35_read_time,
136 .set_time = m48t35_set_time,
139 static int m48t35_probe(struct platform_device *pdev)
141 struct resource *res;
142 struct m48t35_priv *priv;
144 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
147 priv = devm_kzalloc(&pdev->dev, sizeof(struct m48t35_priv), GFP_KERNEL);
151 priv->size = resource_size(res);
153 * kludge: remove the #ifndef after ioc3 resource
154 * conflicts are resolved
156 #ifndef CONFIG_SGI_IP27
157 if (!devm_request_mem_region(&pdev->dev, res->start, priv->size,
161 priv->baseaddr = res->start;
162 priv->reg = devm_ioremap(&pdev->dev, priv->baseaddr, priv->size);
166 spin_lock_init(&priv->lock);
168 platform_set_drvdata(pdev, priv);
170 priv->rtc = devm_rtc_device_register(&pdev->dev, "m48t35",
171 &m48t35_ops, THIS_MODULE);
172 return PTR_ERR_OR_ZERO(priv->rtc);
175 static struct platform_driver m48t35_platform_driver = {
177 .name = "rtc-m48t35",
179 .probe = m48t35_probe,
182 module_platform_driver(m48t35_platform_driver);
185 MODULE_DESCRIPTION("M48T35 RTC driver");
186 MODULE_LICENSE("GPL");
187 MODULE_ALIAS("platform:rtc-m48t35");