Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * TX4939 internal RTC driver |
| 3 | * Based on RBTX49xx patch from CELF patch archive. |
| 4 | * |
| 5 | * This file is subject to the terms and conditions of the GNU General Public |
| 6 | * License. See the file "COPYING" in the main directory of this archive |
| 7 | * for more details. |
| 8 | * |
| 9 | * (C) Copyright TOSHIBA CORPORATION 2005-2007 |
| 10 | */ |
| 11 | #include <linux/rtc.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <asm/txx9/tx4939.h> |
| 16 | |
| 17 | struct tx4939rtc_plat_data { |
| 18 | struct rtc_device *rtc; |
| 19 | struct tx4939_rtc_reg __iomem *rtcreg; |
Atsushi Nemoto | af69a18 | 2009-12-15 16:46:00 -0800 | [diff] [blame] | 20 | spinlock_t lock; |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 21 | }; |
| 22 | |
| 23 | static struct tx4939rtc_plat_data *get_tx4939rtc_plat_data(struct device *dev) |
| 24 | { |
| 25 | return platform_get_drvdata(to_platform_device(dev)); |
| 26 | } |
| 27 | |
| 28 | static int tx4939_rtc_cmd(struct tx4939_rtc_reg __iomem *rtcreg, int cmd) |
| 29 | { |
| 30 | int i = 0; |
| 31 | |
| 32 | __raw_writel(cmd, &rtcreg->ctl); |
| 33 | /* This might take 30us (next 32.768KHz clock) */ |
| 34 | while (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_BUSY) { |
| 35 | /* timeout on approx. 100us (@ GBUS200MHz) */ |
| 36 | if (i++ > 200 * 100) |
| 37 | return -EBUSY; |
| 38 | cpu_relax(); |
| 39 | } |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static int tx4939_rtc_set_mmss(struct device *dev, unsigned long secs) |
| 44 | { |
| 45 | struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev); |
| 46 | struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg; |
| 47 | int i, ret; |
| 48 | unsigned char buf[6]; |
| 49 | |
| 50 | buf[0] = 0; |
| 51 | buf[1] = 0; |
| 52 | buf[2] = secs; |
| 53 | buf[3] = secs >> 8; |
| 54 | buf[4] = secs >> 16; |
| 55 | buf[5] = secs >> 24; |
Atsushi Nemoto | af69a18 | 2009-12-15 16:46:00 -0800 | [diff] [blame] | 56 | spin_lock_irq(&pdata->lock); |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 57 | __raw_writel(0, &rtcreg->adr); |
| 58 | for (i = 0; i < 6; i++) |
| 59 | __raw_writel(buf[i], &rtcreg->dat); |
| 60 | ret = tx4939_rtc_cmd(rtcreg, |
| 61 | TX4939_RTCCTL_COMMAND_SETTIME | |
| 62 | (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME)); |
Atsushi Nemoto | af69a18 | 2009-12-15 16:46:00 -0800 | [diff] [blame] | 63 | spin_unlock_irq(&pdata->lock); |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 64 | return ret; |
| 65 | } |
| 66 | |
| 67 | static int tx4939_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 68 | { |
| 69 | struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev); |
| 70 | struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg; |
| 71 | int i, ret; |
| 72 | unsigned long sec; |
| 73 | unsigned char buf[6]; |
| 74 | |
Atsushi Nemoto | af69a18 | 2009-12-15 16:46:00 -0800 | [diff] [blame] | 75 | spin_lock_irq(&pdata->lock); |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 76 | ret = tx4939_rtc_cmd(rtcreg, |
| 77 | TX4939_RTCCTL_COMMAND_GETTIME | |
| 78 | (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME)); |
| 79 | if (ret) { |
Atsushi Nemoto | af69a18 | 2009-12-15 16:46:00 -0800 | [diff] [blame] | 80 | spin_unlock_irq(&pdata->lock); |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 81 | return ret; |
| 82 | } |
| 83 | __raw_writel(2, &rtcreg->adr); |
| 84 | for (i = 2; i < 6; i++) |
| 85 | buf[i] = __raw_readl(&rtcreg->dat); |
Atsushi Nemoto | af69a18 | 2009-12-15 16:46:00 -0800 | [diff] [blame] | 86 | spin_unlock_irq(&pdata->lock); |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 87 | sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2]; |
| 88 | rtc_time_to_tm(sec, tm); |
| 89 | return rtc_valid_tm(tm); |
| 90 | } |
| 91 | |
| 92 | static int tx4939_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 93 | { |
| 94 | struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev); |
| 95 | struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg; |
| 96 | int i, ret; |
| 97 | unsigned long sec; |
| 98 | unsigned char buf[6]; |
| 99 | |
| 100 | if (alrm->time.tm_sec < 0 || |
| 101 | alrm->time.tm_min < 0 || |
| 102 | alrm->time.tm_hour < 0 || |
| 103 | alrm->time.tm_mday < 0 || |
| 104 | alrm->time.tm_mon < 0 || |
| 105 | alrm->time.tm_year < 0) |
| 106 | return -EINVAL; |
| 107 | rtc_tm_to_time(&alrm->time, &sec); |
| 108 | buf[0] = 0; |
| 109 | buf[1] = 0; |
| 110 | buf[2] = sec; |
| 111 | buf[3] = sec >> 8; |
| 112 | buf[4] = sec >> 16; |
| 113 | buf[5] = sec >> 24; |
Atsushi Nemoto | af69a18 | 2009-12-15 16:46:00 -0800 | [diff] [blame] | 114 | spin_lock_irq(&pdata->lock); |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 115 | __raw_writel(0, &rtcreg->adr); |
| 116 | for (i = 0; i < 6; i++) |
| 117 | __raw_writel(buf[i], &rtcreg->dat); |
| 118 | ret = tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_SETALARM | |
| 119 | (alrm->enabled ? TX4939_RTCCTL_ALME : 0)); |
Atsushi Nemoto | af69a18 | 2009-12-15 16:46:00 -0800 | [diff] [blame] | 120 | spin_unlock_irq(&pdata->lock); |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | static int tx4939_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 125 | { |
| 126 | struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev); |
| 127 | struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg; |
| 128 | int i, ret; |
| 129 | unsigned long sec; |
| 130 | unsigned char buf[6]; |
| 131 | u32 ctl; |
| 132 | |
Atsushi Nemoto | af69a18 | 2009-12-15 16:46:00 -0800 | [diff] [blame] | 133 | spin_lock_irq(&pdata->lock); |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 134 | ret = tx4939_rtc_cmd(rtcreg, |
| 135 | TX4939_RTCCTL_COMMAND_GETALARM | |
| 136 | (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME)); |
| 137 | if (ret) { |
Atsushi Nemoto | af69a18 | 2009-12-15 16:46:00 -0800 | [diff] [blame] | 138 | spin_unlock_irq(&pdata->lock); |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 139 | return ret; |
| 140 | } |
| 141 | __raw_writel(2, &rtcreg->adr); |
| 142 | for (i = 2; i < 6; i++) |
| 143 | buf[i] = __raw_readl(&rtcreg->dat); |
| 144 | ctl = __raw_readl(&rtcreg->ctl); |
| 145 | alrm->enabled = (ctl & TX4939_RTCCTL_ALME) ? 1 : 0; |
| 146 | alrm->pending = (ctl & TX4939_RTCCTL_ALMD) ? 1 : 0; |
Atsushi Nemoto | af69a18 | 2009-12-15 16:46:00 -0800 | [diff] [blame] | 147 | spin_unlock_irq(&pdata->lock); |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 148 | sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2]; |
| 149 | rtc_time_to_tm(sec, &alrm->time); |
| 150 | return rtc_valid_tm(&alrm->time); |
| 151 | } |
| 152 | |
| 153 | static int tx4939_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 154 | { |
| 155 | struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev); |
| 156 | |
Atsushi Nemoto | af69a18 | 2009-12-15 16:46:00 -0800 | [diff] [blame] | 157 | spin_lock_irq(&pdata->lock); |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 158 | tx4939_rtc_cmd(pdata->rtcreg, |
| 159 | TX4939_RTCCTL_COMMAND_NOP | |
| 160 | (enabled ? TX4939_RTCCTL_ALME : 0)); |
Atsushi Nemoto | af69a18 | 2009-12-15 16:46:00 -0800 | [diff] [blame] | 161 | spin_unlock_irq(&pdata->lock); |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static irqreturn_t tx4939_rtc_interrupt(int irq, void *dev_id) |
| 166 | { |
| 167 | struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev_id); |
| 168 | struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg; |
| 169 | unsigned long events = RTC_IRQF; |
| 170 | |
Atsushi Nemoto | af69a18 | 2009-12-15 16:46:00 -0800 | [diff] [blame] | 171 | spin_lock(&pdata->lock); |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 172 | if (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALMD) { |
| 173 | events |= RTC_AF; |
| 174 | tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_NOP); |
| 175 | } |
Atsushi Nemoto | af69a18 | 2009-12-15 16:46:00 -0800 | [diff] [blame] | 176 | spin_unlock(&pdata->lock); |
| 177 | if (likely(pdata->rtc)) |
| 178 | rtc_update_irq(pdata->rtc, 1, events); |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 179 | return IRQ_HANDLED; |
| 180 | } |
| 181 | |
| 182 | static const struct rtc_class_ops tx4939_rtc_ops = { |
| 183 | .read_time = tx4939_rtc_read_time, |
| 184 | .read_alarm = tx4939_rtc_read_alarm, |
| 185 | .set_alarm = tx4939_rtc_set_alarm, |
| 186 | .set_mmss = tx4939_rtc_set_mmss, |
| 187 | .alarm_irq_enable = tx4939_rtc_alarm_irq_enable, |
| 188 | }; |
| 189 | |
| 190 | static ssize_t tx4939_rtc_nvram_read(struct kobject *kobj, |
| 191 | struct bin_attribute *bin_attr, |
| 192 | char *buf, loff_t pos, size_t size) |
| 193 | { |
| 194 | struct device *dev = container_of(kobj, struct device, kobj); |
| 195 | struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev); |
| 196 | struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg; |
| 197 | ssize_t count; |
| 198 | |
Atsushi Nemoto | af69a18 | 2009-12-15 16:46:00 -0800 | [diff] [blame] | 199 | spin_lock_irq(&pdata->lock); |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 200 | for (count = 0; size > 0 && pos < TX4939_RTC_REG_RAMSIZE; |
| 201 | count++, size--) { |
| 202 | __raw_writel(pos++, &rtcreg->adr); |
| 203 | *buf++ = __raw_readl(&rtcreg->dat); |
| 204 | } |
Atsushi Nemoto | af69a18 | 2009-12-15 16:46:00 -0800 | [diff] [blame] | 205 | spin_unlock_irq(&pdata->lock); |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 206 | return count; |
| 207 | } |
| 208 | |
| 209 | static ssize_t tx4939_rtc_nvram_write(struct kobject *kobj, |
| 210 | struct bin_attribute *bin_attr, |
| 211 | char *buf, loff_t pos, size_t size) |
| 212 | { |
| 213 | struct device *dev = container_of(kobj, struct device, kobj); |
| 214 | struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev); |
| 215 | struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg; |
| 216 | ssize_t count; |
| 217 | |
Atsushi Nemoto | af69a18 | 2009-12-15 16:46:00 -0800 | [diff] [blame] | 218 | spin_lock_irq(&pdata->lock); |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 219 | for (count = 0; size > 0 && pos < TX4939_RTC_REG_RAMSIZE; |
| 220 | count++, size--) { |
| 221 | __raw_writel(pos++, &rtcreg->adr); |
| 222 | __raw_writel(*buf++, &rtcreg->dat); |
| 223 | } |
Atsushi Nemoto | af69a18 | 2009-12-15 16:46:00 -0800 | [diff] [blame] | 224 | spin_unlock_irq(&pdata->lock); |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 225 | return count; |
| 226 | } |
| 227 | |
| 228 | static struct bin_attribute tx4939_rtc_nvram_attr = { |
| 229 | .attr = { |
| 230 | .name = "nvram", |
| 231 | .mode = S_IRUGO | S_IWUSR, |
| 232 | }, |
| 233 | .size = TX4939_RTC_REG_RAMSIZE, |
| 234 | .read = tx4939_rtc_nvram_read, |
| 235 | .write = tx4939_rtc_nvram_write, |
| 236 | }; |
| 237 | |
| 238 | static int __init tx4939_rtc_probe(struct platform_device *pdev) |
| 239 | { |
| 240 | struct rtc_device *rtc; |
| 241 | struct tx4939rtc_plat_data *pdata; |
| 242 | struct resource *res; |
| 243 | int irq, ret; |
| 244 | |
| 245 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 246 | if (!res) |
| 247 | return -ENODEV; |
| 248 | irq = platform_get_irq(pdev, 0); |
| 249 | if (irq < 0) |
| 250 | return -ENODEV; |
| 251 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 252 | if (!pdata) |
| 253 | return -ENOMEM; |
| 254 | platform_set_drvdata(pdev, pdata); |
| 255 | |
| 256 | if (!devm_request_mem_region(&pdev->dev, res->start, |
| 257 | resource_size(res), pdev->name)) |
| 258 | return -EBUSY; |
| 259 | pdata->rtcreg = devm_ioremap(&pdev->dev, res->start, |
| 260 | resource_size(res)); |
| 261 | if (!pdata->rtcreg) |
| 262 | return -EBUSY; |
| 263 | |
Atsushi Nemoto | af69a18 | 2009-12-15 16:46:00 -0800 | [diff] [blame] | 264 | spin_lock_init(&pdata->lock); |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 265 | tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP); |
| 266 | if (devm_request_irq(&pdev->dev, irq, tx4939_rtc_interrupt, |
Atsushi Nemoto | 0a817f7 | 2009-06-17 16:26:12 -0700 | [diff] [blame] | 267 | IRQF_DISABLED, pdev->name, &pdev->dev) < 0) |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 268 | return -EBUSY; |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 269 | rtc = rtc_device_register(pdev->name, &pdev->dev, |
| 270 | &tx4939_rtc_ops, THIS_MODULE); |
| 271 | if (IS_ERR(rtc)) |
| 272 | return PTR_ERR(rtc); |
| 273 | pdata->rtc = rtc; |
| 274 | ret = sysfs_create_bin_file(&pdev->dev.kobj, &tx4939_rtc_nvram_attr); |
| 275 | if (ret) |
| 276 | rtc_device_unregister(rtc); |
| 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | static int __exit tx4939_rtc_remove(struct platform_device *pdev) |
| 281 | { |
| 282 | struct tx4939rtc_plat_data *pdata = platform_get_drvdata(pdev); |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 283 | |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 284 | sysfs_remove_bin_file(&pdev->dev.kobj, &tx4939_rtc_nvram_attr); |
Atsushi Nemoto | af69a18 | 2009-12-15 16:46:00 -0800 | [diff] [blame] | 285 | rtc_device_unregister(pdata->rtc); |
| 286 | spin_lock_irq(&pdata->lock); |
| 287 | tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP); |
| 288 | spin_unlock_irq(&pdata->lock); |
Atsushi Nemoto | 0e14923 | 2009-01-06 14:42:22 -0800 | [diff] [blame] | 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static struct platform_driver tx4939_rtc_driver = { |
| 293 | .remove = __exit_p(tx4939_rtc_remove), |
| 294 | .driver = { |
| 295 | .name = "tx4939rtc", |
| 296 | .owner = THIS_MODULE, |
| 297 | }, |
| 298 | }; |
| 299 | |
| 300 | static int __init tx4939rtc_init(void) |
| 301 | { |
| 302 | return platform_driver_probe(&tx4939_rtc_driver, tx4939_rtc_probe); |
| 303 | } |
| 304 | |
| 305 | static void __exit tx4939rtc_exit(void) |
| 306 | { |
| 307 | platform_driver_unregister(&tx4939_rtc_driver); |
| 308 | } |
| 309 | |
| 310 | module_init(tx4939rtc_init); |
| 311 | module_exit(tx4939rtc_exit); |
| 312 | |
| 313 | MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>"); |
| 314 | MODULE_DESCRIPTION("TX4939 internal RTC driver"); |
| 315 | MODULE_LICENSE("GPL"); |
| 316 | MODULE_ALIAS("platform:tx4939rtc"); |