| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | *  linux/arch/arm/mach-integrator/time.c | 
|  | 3 | * | 
|  | 4 | *  Copyright (C) 2000-2001 Deep Blue Solutions Ltd. | 
|  | 5 | * | 
|  | 6 | * This program is free software; you can redistribute it and/or modify | 
|  | 7 | * it under the terms of the GNU General Public License version 2 as | 
|  | 8 | * published by the Free Software Foundation. | 
|  | 9 | */ | 
|  | 10 | #include <linux/module.h> | 
|  | 11 | #include <linux/kernel.h> | 
|  | 12 | #include <linux/time.h> | 
|  | 13 | #include <linux/mc146818rtc.h> | 
|  | 14 | #include <linux/interrupt.h> | 
|  | 15 | #include <linux/init.h> | 
|  | 16 | #include <linux/device.h> | 
| Russell King | a62c80e | 2006-01-07 13:52:45 +0000 | [diff] [blame] | 17 | #include <linux/amba/bus.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/hardware.h> | 
|  | 20 | #include <asm/io.h> | 
|  | 21 | #include <asm/uaccess.h> | 
|  | 22 | #include <asm/rtc.h> | 
|  | 23 |  | 
|  | 24 | #include <asm/mach/time.h> | 
|  | 25 |  | 
|  | 26 | #define RTC_DR		(0) | 
|  | 27 | #define RTC_MR		(4) | 
|  | 28 | #define RTC_STAT	(8) | 
|  | 29 | #define RTC_EOI		(8) | 
|  | 30 | #define RTC_LR		(12) | 
|  | 31 | #define RTC_CR		(16) | 
|  | 32 | #define RTC_CR_MIE	(1 << 0) | 
|  | 33 |  | 
|  | 34 | extern int (*set_rtc)(void); | 
|  | 35 | static void __iomem *rtc_base; | 
|  | 36 |  | 
|  | 37 | static int integrator_set_rtc(void) | 
|  | 38 | { | 
|  | 39 | __raw_writel(xtime.tv_sec, rtc_base + RTC_LR); | 
|  | 40 | return 1; | 
|  | 41 | } | 
|  | 42 |  | 
| Richard Purdie | 4079c39 | 2006-03-27 01:16:36 -0800 | [diff] [blame] | 43 | static int integrator_rtc_read_alarm(struct rtc_wkalrm *alrm) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { | 
|  | 45 | rtc_time_to_tm(readl(rtc_base + RTC_MR), &alrm->time); | 
| Russell King | d5aa207 | 2005-04-30 12:19:28 +0100 | [diff] [blame] | 46 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | } | 
|  | 48 |  | 
| Richard Purdie | 4079c39 | 2006-03-27 01:16:36 -0800 | [diff] [blame] | 49 | static inline int integrator_rtc_set_alarm(struct rtc_wkalrm *alrm) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { | 
|  | 51 | unsigned long time; | 
|  | 52 | int ret; | 
|  | 53 |  | 
| Russell King | d5aa207 | 2005-04-30 12:19:28 +0100 | [diff] [blame] | 54 | /* | 
|  | 55 | * At the moment, we can only deal with non-wildcarded alarm times. | 
|  | 56 | */ | 
|  | 57 | ret = rtc_valid_tm(&alrm->time); | 
|  | 58 | if (ret == 0) | 
|  | 59 | ret = rtc_tm_to_time(&alrm->time, &time); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | if (ret == 0) | 
|  | 61 | writel(time, rtc_base + RTC_MR); | 
|  | 62 | return ret; | 
|  | 63 | } | 
|  | 64 |  | 
| Richard Purdie | 4079c39 | 2006-03-27 01:16:36 -0800 | [diff] [blame] | 65 | static int integrator_rtc_read_time(struct rtc_time *tm) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { | 
|  | 67 | rtc_time_to_tm(readl(rtc_base + RTC_DR), tm); | 
| Russell King | d5aa207 | 2005-04-30 12:19:28 +0100 | [diff] [blame] | 68 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } | 
|  | 70 |  | 
|  | 71 | /* | 
|  | 72 | * Set the RTC time.  Unfortunately, we can't accurately set | 
|  | 73 | * the point at which the counter updates. | 
|  | 74 | * | 
|  | 75 | * Also, since RTC_LR is transferred to RTC_CR on next rising | 
|  | 76 | * edge of the 1Hz clock, we must write the time one second | 
|  | 77 | * in advance. | 
|  | 78 | */ | 
| Richard Purdie | 4079c39 | 2006-03-27 01:16:36 -0800 | [diff] [blame] | 79 | static inline int integrator_rtc_set_time(struct rtc_time *tm) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { | 
|  | 81 | unsigned long time; | 
|  | 82 | int ret; | 
|  | 83 |  | 
|  | 84 | ret = rtc_tm_to_time(tm, &time); | 
|  | 85 | if (ret == 0) | 
|  | 86 | writel(time + 1, rtc_base + RTC_LR); | 
|  | 87 |  | 
|  | 88 | return ret; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | static struct rtc_ops rtc_ops = { | 
|  | 92 | .owner		= THIS_MODULE, | 
| Richard Purdie | 4079c39 | 2006-03-27 01:16:36 -0800 | [diff] [blame] | 93 | .read_time	= integrator_rtc_read_time, | 
|  | 94 | .set_time	= integrator_rtc_set_time, | 
|  | 95 | .read_alarm	= integrator_rtc_read_alarm, | 
|  | 96 | .set_alarm	= integrator_rtc_set_alarm, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | }; | 
|  | 98 |  | 
| Linus Torvalds | 0cd61b6 | 2006-10-06 10:53:39 -0700 | [diff] [blame] | 99 | static irqreturn_t arm_rtc_interrupt(int irq, void *dev_id) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { | 
|  | 101 | writel(0, rtc_base + RTC_EOI); | 
|  | 102 | return IRQ_HANDLED; | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | static int rtc_probe(struct amba_device *dev, void *id) | 
|  | 106 | { | 
|  | 107 | int ret; | 
|  | 108 |  | 
|  | 109 | if (rtc_base) | 
|  | 110 | return -EBUSY; | 
|  | 111 |  | 
|  | 112 | ret = amba_request_regions(dev, NULL); | 
|  | 113 | if (ret) | 
|  | 114 | goto out; | 
|  | 115 |  | 
|  | 116 | rtc_base = ioremap(dev->res.start, SZ_4K); | 
|  | 117 | if (!rtc_base) { | 
|  | 118 | ret = -ENOMEM; | 
|  | 119 | goto res_out; | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | __raw_writel(0, rtc_base + RTC_CR); | 
|  | 123 | __raw_writel(0, rtc_base + RTC_EOI); | 
|  | 124 |  | 
|  | 125 | xtime.tv_sec = __raw_readl(rtc_base + RTC_DR); | 
|  | 126 |  | 
| Thomas Gleixner | 52e405e | 2006-07-03 02:20:05 +0200 | [diff] [blame] | 127 | ret = request_irq(dev->irq[0], arm_rtc_interrupt, IRQF_DISABLED, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | "rtc-pl030", dev); | 
|  | 129 | if (ret) | 
|  | 130 | goto map_out; | 
|  | 131 |  | 
|  | 132 | ret = register_rtc(&rtc_ops); | 
|  | 133 | if (ret) | 
|  | 134 | goto irq_out; | 
|  | 135 |  | 
|  | 136 | set_rtc = integrator_set_rtc; | 
|  | 137 | return 0; | 
|  | 138 |  | 
|  | 139 | irq_out: | 
|  | 140 | free_irq(dev->irq[0], dev); | 
|  | 141 | map_out: | 
|  | 142 | iounmap(rtc_base); | 
|  | 143 | rtc_base = NULL; | 
|  | 144 | res_out: | 
|  | 145 | amba_release_regions(dev); | 
|  | 146 | out: | 
|  | 147 | return ret; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | static int rtc_remove(struct amba_device *dev) | 
|  | 151 | { | 
|  | 152 | set_rtc = NULL; | 
|  | 153 |  | 
|  | 154 | writel(0, rtc_base + RTC_CR); | 
|  | 155 |  | 
|  | 156 | free_irq(dev->irq[0], dev); | 
|  | 157 | unregister_rtc(&rtc_ops); | 
|  | 158 |  | 
|  | 159 | iounmap(rtc_base); | 
|  | 160 | rtc_base = NULL; | 
|  | 161 | amba_release_regions(dev); | 
|  | 162 |  | 
|  | 163 | return 0; | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | static struct timespec rtc_delta; | 
|  | 167 |  | 
|  | 168 | static int rtc_suspend(struct amba_device *dev, pm_message_t state) | 
|  | 169 | { | 
|  | 170 | struct timespec rtc; | 
|  | 171 |  | 
|  | 172 | rtc.tv_sec = readl(rtc_base + RTC_DR); | 
|  | 173 | rtc.tv_nsec = 0; | 
|  | 174 | save_time_delta(&rtc_delta, &rtc); | 
|  | 175 |  | 
|  | 176 | return 0; | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | static int rtc_resume(struct amba_device *dev) | 
|  | 180 | { | 
|  | 181 | struct timespec rtc; | 
|  | 182 |  | 
|  | 183 | rtc.tv_sec = readl(rtc_base + RTC_DR); | 
|  | 184 | rtc.tv_nsec = 0; | 
|  | 185 | restore_time_delta(&rtc_delta, &rtc); | 
|  | 186 |  | 
|  | 187 | return 0; | 
|  | 188 | } | 
|  | 189 |  | 
|  | 190 | static struct amba_id rtc_ids[] = { | 
|  | 191 | { | 
|  | 192 | .id	= 0x00041030, | 
|  | 193 | .mask	= 0x000fffff, | 
|  | 194 | }, | 
|  | 195 | { 0, 0 }, | 
|  | 196 | }; | 
|  | 197 |  | 
|  | 198 | static struct amba_driver rtc_driver = { | 
|  | 199 | .drv		= { | 
|  | 200 | .name	= "rtc-pl030", | 
|  | 201 | }, | 
|  | 202 | .probe		= rtc_probe, | 
|  | 203 | .remove		= rtc_remove, | 
|  | 204 | .suspend	= rtc_suspend, | 
|  | 205 | .resume		= rtc_resume, | 
|  | 206 | .id_table	= rtc_ids, | 
|  | 207 | }; | 
|  | 208 |  | 
|  | 209 | static int __init integrator_rtc_init(void) | 
|  | 210 | { | 
|  | 211 | return amba_driver_register(&rtc_driver); | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | static void __exit integrator_rtc_exit(void) | 
|  | 215 | { | 
|  | 216 | amba_driver_unregister(&rtc_driver); | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | module_init(integrator_rtc_init); | 
|  | 220 | module_exit(integrator_rtc_exit); |