Thomas Bogendoerfer | d1dbd82e | 2008-10-14 17:17:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the SGS-Thomson M48T35 Timekeeper RAM chip |
| 3 | * |
| 4 | * Copyright (C) 2000 Silicon Graphics, Inc. |
| 5 | * Written by Ulf Carlsson (ulfc@engr.sgi.com) |
| 6 | * |
| 7 | * Copyright (C) 2008 Thomas Bogendoerfer |
| 8 | * |
| 9 | * Based on code written by Paul Gortmaker. |
| 10 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/rtc.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/bcd.h> |
Geert Uytterhoeven | d7a6119 | 2008-10-16 09:28:47 +0200 | [diff] [blame] | 21 | #include <linux/io.h> |
Thomas Bogendoerfer | d1dbd82e | 2008-10-14 17:17:32 +0200 | [diff] [blame] | 22 | |
| 23 | #define DRV_VERSION "1.0" |
| 24 | |
| 25 | struct m48t35_rtc { |
| 26 | u8 pad[0x7ff8]; /* starts at 0x7ff8 */ |
| 27 | u8 control; |
| 28 | u8 sec; |
| 29 | u8 min; |
| 30 | u8 hour; |
| 31 | u8 day; |
| 32 | u8 date; |
| 33 | u8 month; |
| 34 | u8 year; |
| 35 | }; |
| 36 | |
| 37 | #define M48T35_RTC_SET 0x80 |
| 38 | #define M48T35_RTC_READ 0x40 |
| 39 | |
| 40 | struct m48t35_priv { |
| 41 | struct rtc_device *rtc; |
| 42 | struct m48t35_rtc __iomem *reg; |
| 43 | size_t size; |
| 44 | unsigned long baseaddr; |
| 45 | spinlock_t lock; |
| 46 | }; |
| 47 | |
| 48 | static int m48t35_read_time(struct device *dev, struct rtc_time *tm) |
| 49 | { |
| 50 | struct m48t35_priv *priv = dev_get_drvdata(dev); |
| 51 | u8 control; |
| 52 | |
| 53 | /* |
| 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. |
| 58 | */ |
| 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); |
| 70 | |
| 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); |
| 77 | |
| 78 | /* |
| 79 | * Account for differences between how the RTC uses the values |
| 80 | * and how they are defined in a struct rtc_time; |
| 81 | */ |
| 82 | tm->tm_year += 70; |
| 83 | if (tm->tm_year <= 69) |
| 84 | tm->tm_year += 100; |
| 85 | |
| 86 | tm->tm_mon--; |
| 87 | return rtc_valid_tm(tm); |
| 88 | } |
| 89 | |
| 90 | static int m48t35_set_time(struct device *dev, struct rtc_time *tm) |
| 91 | { |
| 92 | struct m48t35_priv *priv = dev_get_drvdata(dev); |
| 93 | unsigned char mon, day, hrs, min, sec; |
| 94 | unsigned int yrs; |
| 95 | u8 control; |
| 96 | |
| 97 | yrs = tm->tm_year + 1900; |
| 98 | mon = tm->tm_mon + 1; /* tm_mon starts at zero */ |
| 99 | day = tm->tm_mday; |
| 100 | hrs = tm->tm_hour; |
| 101 | min = tm->tm_min; |
| 102 | sec = tm->tm_sec; |
| 103 | |
| 104 | if (yrs < 1970) |
| 105 | return -EINVAL; |
| 106 | |
| 107 | yrs -= 1970; |
| 108 | if (yrs > 255) /* They are unsigned */ |
| 109 | return -EINVAL; |
| 110 | |
| 111 | if (yrs > 169) |
| 112 | return -EINVAL; |
| 113 | |
| 114 | if (yrs >= 100) |
| 115 | yrs -= 100; |
| 116 | |
| 117 | sec = bin2bcd(sec); |
| 118 | min = bin2bcd(min); |
| 119 | hrs = bin2bcd(hrs); |
| 120 | day = bin2bcd(day); |
| 121 | mon = bin2bcd(mon); |
| 122 | yrs = bin2bcd(yrs); |
| 123 | |
| 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); |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static const struct rtc_class_ops m48t35_ops = { |
| 139 | .read_time = m48t35_read_time, |
| 140 | .set_time = m48t35_set_time, |
| 141 | }; |
| 142 | |
| 143 | static int __devinit m48t35_probe(struct platform_device *pdev) |
| 144 | { |
Thomas Bogendoerfer | d1dbd82e | 2008-10-14 17:17:32 +0200 | [diff] [blame] | 145 | struct resource *res; |
| 146 | struct m48t35_priv *priv; |
| 147 | int ret = 0; |
| 148 | |
| 149 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 150 | if (!res) |
| 151 | return -ENODEV; |
| 152 | priv = kzalloc(sizeof(struct m48t35_priv), GFP_KERNEL); |
| 153 | if (!priv) |
| 154 | return -ENOMEM; |
| 155 | |
| 156 | priv->size = res->end - res->start + 1; |
| 157 | /* |
| 158 | * kludge: remove the #ifndef after ioc3 resource |
| 159 | * conflicts are resolved |
| 160 | */ |
| 161 | #ifndef CONFIG_SGI_IP27 |
| 162 | if (!request_mem_region(res->start, priv->size, pdev->name)) { |
| 163 | ret = -EBUSY; |
| 164 | goto out; |
| 165 | } |
| 166 | #endif |
| 167 | priv->baseaddr = res->start; |
| 168 | priv->reg = ioremap(priv->baseaddr, priv->size); |
| 169 | if (!priv->reg) { |
| 170 | ret = -ENOMEM; |
| 171 | goto out; |
| 172 | } |
Alessandro Zummo | b74d2ca | 2009-12-15 16:45:53 -0800 | [diff] [blame] | 173 | |
Thomas Bogendoerfer | d1dbd82e | 2008-10-14 17:17:32 +0200 | [diff] [blame] | 174 | spin_lock_init(&priv->lock); |
Alessandro Zummo | b74d2ca | 2009-12-15 16:45:53 -0800 | [diff] [blame] | 175 | |
| 176 | platform_set_drvdata(pdev, priv); |
| 177 | |
| 178 | priv->rtc = rtc_device_register("m48t35", &pdev->dev, |
Thomas Bogendoerfer | d1dbd82e | 2008-10-14 17:17:32 +0200 | [diff] [blame] | 179 | &m48t35_ops, THIS_MODULE); |
Alessandro Zummo | b74d2ca | 2009-12-15 16:45:53 -0800 | [diff] [blame] | 180 | if (IS_ERR(priv->rtc)) { |
| 181 | ret = PTR_ERR(priv->rtc); |
Thomas Bogendoerfer | d1dbd82e | 2008-10-14 17:17:32 +0200 | [diff] [blame] | 182 | goto out; |
| 183 | } |
Alessandro Zummo | b74d2ca | 2009-12-15 16:45:53 -0800 | [diff] [blame] | 184 | |
Thomas Bogendoerfer | d1dbd82e | 2008-10-14 17:17:32 +0200 | [diff] [blame] | 185 | return 0; |
| 186 | |
| 187 | out: |
Thomas Bogendoerfer | d1dbd82e | 2008-10-14 17:17:32 +0200 | [diff] [blame] | 188 | if (priv->reg) |
| 189 | iounmap(priv->reg); |
| 190 | if (priv->baseaddr) |
| 191 | release_mem_region(priv->baseaddr, priv->size); |
| 192 | kfree(priv); |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | static int __devexit m48t35_remove(struct platform_device *pdev) |
| 197 | { |
| 198 | struct m48t35_priv *priv = platform_get_drvdata(pdev); |
| 199 | |
| 200 | rtc_device_unregister(priv->rtc); |
| 201 | iounmap(priv->reg); |
| 202 | #ifndef CONFIG_SGI_IP27 |
| 203 | release_mem_region(priv->baseaddr, priv->size); |
| 204 | #endif |
| 205 | kfree(priv); |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static struct platform_driver m48t35_platform_driver = { |
| 210 | .driver = { |
| 211 | .name = "rtc-m48t35", |
| 212 | .owner = THIS_MODULE, |
| 213 | }, |
| 214 | .probe = m48t35_probe, |
| 215 | .remove = __devexit_p(m48t35_remove), |
| 216 | }; |
| 217 | |
| 218 | static int __init m48t35_init(void) |
| 219 | { |
| 220 | return platform_driver_register(&m48t35_platform_driver); |
| 221 | } |
| 222 | |
| 223 | static void __exit m48t35_exit(void) |
| 224 | { |
| 225 | platform_driver_unregister(&m48t35_platform_driver); |
| 226 | } |
| 227 | |
| 228 | MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>"); |
| 229 | MODULE_DESCRIPTION("M48T35 RTC driver"); |
| 230 | MODULE_LICENSE("GPL"); |
| 231 | MODULE_VERSION(DRV_VERSION); |
| 232 | MODULE_ALIAS("platform:rtc-m48t35"); |
| 233 | |
| 234 | module_init(m48t35_init); |
| 235 | module_exit(m48t35_exit); |