Saeed Bishara | defb451 | 2009-01-06 14:42:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the RTC in Marvell SoCs. |
| 3 | * |
| 4 | * This file is licensed under the terms of the GNU General Public |
| 5 | * License version 2. This program is licensed "as is" without any |
| 6 | * warranty of any kind, whether express or implied. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/rtc.h> |
| 12 | #include <linux/bcd.h> |
| 13 | #include <linux/io.h> |
| 14 | #include <linux/platform_device.h> |
Nicolas Pitre | 9d1d4f9 | 2009-03-23 20:42:29 -0400 | [diff] [blame] | 15 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/gfp.h> |
Saeed Bishara | defb451 | 2009-01-06 14:42:24 -0800 | [diff] [blame] | 17 | |
| 18 | |
| 19 | #define RTC_TIME_REG_OFFS 0 |
| 20 | #define RTC_SECONDS_OFFS 0 |
| 21 | #define RTC_MINUTES_OFFS 8 |
| 22 | #define RTC_HOURS_OFFS 16 |
| 23 | #define RTC_WDAY_OFFS 24 |
| 24 | #define RTC_HOURS_12H_MODE (1 << 22) /* 12 hours mode */ |
| 25 | |
| 26 | #define RTC_DATE_REG_OFFS 4 |
| 27 | #define RTC_MDAY_OFFS 0 |
| 28 | #define RTC_MONTH_OFFS 8 |
| 29 | #define RTC_YEAR_OFFS 16 |
| 30 | |
Saeed Bishara | aeedaca | 2009-12-15 16:46:08 -0800 | [diff] [blame] | 31 | #define RTC_ALARM_TIME_REG_OFFS 8 |
| 32 | #define RTC_ALARM_DATE_REG_OFFS 0xc |
| 33 | #define RTC_ALARM_VALID (1 << 7) |
| 34 | |
| 35 | #define RTC_ALARM_INTERRUPT_MASK_REG_OFFS 0x10 |
| 36 | #define RTC_ALARM_INTERRUPT_CASUE_REG_OFFS 0x14 |
Saeed Bishara | defb451 | 2009-01-06 14:42:24 -0800 | [diff] [blame] | 37 | |
| 38 | struct rtc_plat_data { |
| 39 | struct rtc_device *rtc; |
| 40 | void __iomem *ioaddr; |
Saeed Bishara | aeedaca | 2009-12-15 16:46:08 -0800 | [diff] [blame] | 41 | int irq; |
Saeed Bishara | defb451 | 2009-01-06 14:42:24 -0800 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | static int mv_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 45 | { |
| 46 | struct rtc_plat_data *pdata = dev_get_drvdata(dev); |
| 47 | void __iomem *ioaddr = pdata->ioaddr; |
| 48 | u32 rtc_reg; |
| 49 | |
| 50 | rtc_reg = (bin2bcd(tm->tm_sec) << RTC_SECONDS_OFFS) | |
| 51 | (bin2bcd(tm->tm_min) << RTC_MINUTES_OFFS) | |
| 52 | (bin2bcd(tm->tm_hour) << RTC_HOURS_OFFS) | |
| 53 | (bin2bcd(tm->tm_wday) << RTC_WDAY_OFFS); |
| 54 | writel(rtc_reg, ioaddr + RTC_TIME_REG_OFFS); |
| 55 | |
| 56 | rtc_reg = (bin2bcd(tm->tm_mday) << RTC_MDAY_OFFS) | |
| 57 | (bin2bcd(tm->tm_mon + 1) << RTC_MONTH_OFFS) | |
| 58 | (bin2bcd(tm->tm_year % 100) << RTC_YEAR_OFFS); |
| 59 | writel(rtc_reg, ioaddr + RTC_DATE_REG_OFFS); |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static int mv_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 65 | { |
| 66 | struct rtc_plat_data *pdata = dev_get_drvdata(dev); |
| 67 | void __iomem *ioaddr = pdata->ioaddr; |
| 68 | u32 rtc_time, rtc_date; |
| 69 | unsigned int year, month, day, hour, minute, second, wday; |
| 70 | |
| 71 | rtc_time = readl(ioaddr + RTC_TIME_REG_OFFS); |
| 72 | rtc_date = readl(ioaddr + RTC_DATE_REG_OFFS); |
| 73 | |
| 74 | second = rtc_time & 0x7f; |
| 75 | minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f; |
| 76 | hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hours mode */ |
| 77 | wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7; |
| 78 | |
| 79 | day = rtc_date & 0x3f; |
| 80 | month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f; |
| 81 | year = (rtc_date >> RTC_YEAR_OFFS) & 0xff; |
| 82 | |
| 83 | tm->tm_sec = bcd2bin(second); |
| 84 | tm->tm_min = bcd2bin(minute); |
| 85 | tm->tm_hour = bcd2bin(hour); |
| 86 | tm->tm_mday = bcd2bin(day); |
| 87 | tm->tm_wday = bcd2bin(wday); |
| 88 | tm->tm_mon = bcd2bin(month) - 1; |
| 89 | /* hw counts from year 2000, but tm_year is relative to 1900 */ |
| 90 | tm->tm_year = bcd2bin(year) + 100; |
| 91 | |
| 92 | return rtc_valid_tm(tm); |
| 93 | } |
| 94 | |
Saeed Bishara | aeedaca | 2009-12-15 16:46:08 -0800 | [diff] [blame] | 95 | static int mv_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm) |
| 96 | { |
| 97 | struct rtc_plat_data *pdata = dev_get_drvdata(dev); |
| 98 | void __iomem *ioaddr = pdata->ioaddr; |
| 99 | u32 rtc_time, rtc_date; |
| 100 | unsigned int year, month, day, hour, minute, second, wday; |
| 101 | |
| 102 | rtc_time = readl(ioaddr + RTC_ALARM_TIME_REG_OFFS); |
| 103 | rtc_date = readl(ioaddr + RTC_ALARM_DATE_REG_OFFS); |
| 104 | |
| 105 | second = rtc_time & 0x7f; |
| 106 | minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f; |
| 107 | hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hours mode */ |
| 108 | wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7; |
| 109 | |
| 110 | day = rtc_date & 0x3f; |
| 111 | month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f; |
| 112 | year = (rtc_date >> RTC_YEAR_OFFS) & 0xff; |
| 113 | |
| 114 | alm->time.tm_sec = bcd2bin(second); |
| 115 | alm->time.tm_min = bcd2bin(minute); |
| 116 | alm->time.tm_hour = bcd2bin(hour); |
| 117 | alm->time.tm_mday = bcd2bin(day); |
| 118 | alm->time.tm_wday = bcd2bin(wday); |
| 119 | alm->time.tm_mon = bcd2bin(month) - 1; |
| 120 | /* hw counts from year 2000, but tm_year is relative to 1900 */ |
| 121 | alm->time.tm_year = bcd2bin(year) + 100; |
| 122 | |
| 123 | if (rtc_valid_tm(&alm->time) < 0) { |
| 124 | dev_err(dev, "retrieved alarm date/time is not valid.\n"); |
| 125 | rtc_time_to_tm(0, &alm->time); |
| 126 | } |
| 127 | |
| 128 | alm->enabled = !!readl(ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS); |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | static int mv_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm) |
| 133 | { |
| 134 | struct rtc_plat_data *pdata = dev_get_drvdata(dev); |
| 135 | void __iomem *ioaddr = pdata->ioaddr; |
| 136 | u32 rtc_reg = 0; |
| 137 | |
| 138 | if (alm->time.tm_sec >= 0) |
| 139 | rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_sec)) |
| 140 | << RTC_SECONDS_OFFS; |
| 141 | if (alm->time.tm_min >= 0) |
| 142 | rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_min)) |
| 143 | << RTC_MINUTES_OFFS; |
| 144 | if (alm->time.tm_hour >= 0) |
| 145 | rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_hour)) |
| 146 | << RTC_HOURS_OFFS; |
| 147 | |
| 148 | writel(rtc_reg, ioaddr + RTC_ALARM_TIME_REG_OFFS); |
| 149 | |
| 150 | if (alm->time.tm_mday >= 0) |
| 151 | rtc_reg = (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mday)) |
| 152 | << RTC_MDAY_OFFS; |
| 153 | else |
| 154 | rtc_reg = 0; |
| 155 | |
| 156 | if (alm->time.tm_mon >= 0) |
| 157 | rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mon + 1)) |
| 158 | << RTC_MONTH_OFFS; |
| 159 | |
| 160 | if (alm->time.tm_year >= 0) |
| 161 | rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_year % 100)) |
| 162 | << RTC_YEAR_OFFS; |
| 163 | |
| 164 | writel(rtc_reg, ioaddr + RTC_ALARM_DATE_REG_OFFS); |
| 165 | writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS); |
| 166 | writel(alm->enabled ? 1 : 0, |
| 167 | ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS); |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 172 | static int mv_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) |
Saeed Bishara | aeedaca | 2009-12-15 16:46:08 -0800 | [diff] [blame] | 173 | { |
| 174 | struct platform_device *pdev = to_platform_device(dev); |
| 175 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); |
| 176 | void __iomem *ioaddr = pdata->ioaddr; |
| 177 | |
| 178 | if (pdata->irq < 0) |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 179 | return -EINVAL; /* fall back into rtc-dev's emulation */ |
| 180 | |
| 181 | if (enabled) |
Saeed Bishara | aeedaca | 2009-12-15 16:46:08 -0800 | [diff] [blame] | 182 | writel(1, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS); |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 183 | else |
| 184 | writel(0, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS); |
Saeed Bishara | aeedaca | 2009-12-15 16:46:08 -0800 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static irqreturn_t mv_rtc_interrupt(int irq, void *data) |
| 189 | { |
| 190 | struct rtc_plat_data *pdata = data; |
| 191 | void __iomem *ioaddr = pdata->ioaddr; |
| 192 | |
| 193 | /* alarm irq? */ |
| 194 | if (!readl(ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS)) |
| 195 | return IRQ_NONE; |
| 196 | |
| 197 | /* clear interrupt */ |
| 198 | writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS); |
| 199 | rtc_update_irq(pdata->rtc, 1, RTC_IRQF | RTC_AF); |
| 200 | return IRQ_HANDLED; |
| 201 | } |
| 202 | |
Saeed Bishara | defb451 | 2009-01-06 14:42:24 -0800 | [diff] [blame] | 203 | static const struct rtc_class_ops mv_rtc_ops = { |
| 204 | .read_time = mv_rtc_read_time, |
| 205 | .set_time = mv_rtc_set_time, |
| 206 | }; |
| 207 | |
Saeed Bishara | aeedaca | 2009-12-15 16:46:08 -0800 | [diff] [blame] | 208 | static const struct rtc_class_ops mv_rtc_alarm_ops = { |
| 209 | .read_time = mv_rtc_read_time, |
| 210 | .set_time = mv_rtc_set_time, |
| 211 | .read_alarm = mv_rtc_read_alarm, |
| 212 | .set_alarm = mv_rtc_set_alarm, |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 213 | .alarm_irq_enable = mv_rtc_alarm_irq_enable, |
Saeed Bishara | aeedaca | 2009-12-15 16:46:08 -0800 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | static int __devinit mv_rtc_probe(struct platform_device *pdev) |
Saeed Bishara | defb451 | 2009-01-06 14:42:24 -0800 | [diff] [blame] | 217 | { |
| 218 | struct resource *res; |
| 219 | struct rtc_plat_data *pdata; |
| 220 | resource_size_t size; |
| 221 | u32 rtc_time; |
| 222 | |
| 223 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 224 | if (!res) |
| 225 | return -ENODEV; |
| 226 | |
| 227 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 228 | if (!pdata) |
| 229 | return -ENOMEM; |
| 230 | |
| 231 | size = resource_size(res); |
| 232 | if (!devm_request_mem_region(&pdev->dev, res->start, size, |
| 233 | pdev->name)) |
| 234 | return -EBUSY; |
| 235 | |
| 236 | pdata->ioaddr = devm_ioremap(&pdev->dev, res->start, size); |
| 237 | if (!pdata->ioaddr) |
| 238 | return -ENOMEM; |
| 239 | |
| 240 | /* make sure the 24 hours mode is enabled */ |
| 241 | rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS); |
| 242 | if (rtc_time & RTC_HOURS_12H_MODE) { |
| 243 | dev_err(&pdev->dev, "24 Hours mode not supported.\n"); |
| 244 | return -EINVAL; |
| 245 | } |
| 246 | |
Nicolas Pitre | 9d1d4f9 | 2009-03-23 20:42:29 -0400 | [diff] [blame] | 247 | /* make sure it is actually functional */ |
| 248 | if (rtc_time == 0x01000000) { |
| 249 | ssleep(1); |
| 250 | rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS); |
| 251 | if (rtc_time == 0x01000000) { |
| 252 | dev_err(&pdev->dev, "internal RTC not ticking\n"); |
| 253 | return -ENODEV; |
| 254 | } |
| 255 | } |
| 256 | |
Saeed Bishara | aeedaca | 2009-12-15 16:46:08 -0800 | [diff] [blame] | 257 | pdata->irq = platform_get_irq(pdev, 0); |
| 258 | |
Saeed Bishara | defb451 | 2009-01-06 14:42:24 -0800 | [diff] [blame] | 259 | platform_set_drvdata(pdev, pdata); |
Saeed Bishara | aeedaca | 2009-12-15 16:46:08 -0800 | [diff] [blame] | 260 | |
| 261 | if (pdata->irq >= 0) { |
| 262 | device_init_wakeup(&pdev->dev, 1); |
| 263 | pdata->rtc = rtc_device_register(pdev->name, &pdev->dev, |
| 264 | &mv_rtc_alarm_ops, |
| 265 | THIS_MODULE); |
| 266 | } else |
| 267 | pdata->rtc = rtc_device_register(pdev->name, &pdev->dev, |
| 268 | &mv_rtc_ops, THIS_MODULE); |
Saeed Bishara | defb451 | 2009-01-06 14:42:24 -0800 | [diff] [blame] | 269 | if (IS_ERR(pdata->rtc)) |
| 270 | return PTR_ERR(pdata->rtc); |
| 271 | |
Saeed Bishara | aeedaca | 2009-12-15 16:46:08 -0800 | [diff] [blame] | 272 | if (pdata->irq >= 0) { |
| 273 | writel(0, pdata->ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS); |
| 274 | if (devm_request_irq(&pdev->dev, pdata->irq, mv_rtc_interrupt, |
| 275 | IRQF_DISABLED | IRQF_SHARED, |
| 276 | pdev->name, pdata) < 0) { |
| 277 | dev_warn(&pdev->dev, "interrupt not available.\n"); |
| 278 | pdata->irq = -1; |
| 279 | } |
| 280 | } |
| 281 | |
Saeed Bishara | defb451 | 2009-01-06 14:42:24 -0800 | [diff] [blame] | 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static int __exit mv_rtc_remove(struct platform_device *pdev) |
| 286 | { |
| 287 | struct rtc_plat_data *pdata = platform_get_drvdata(pdev); |
| 288 | |
Saeed Bishara | aeedaca | 2009-12-15 16:46:08 -0800 | [diff] [blame] | 289 | if (pdata->irq >= 0) |
| 290 | device_init_wakeup(&pdev->dev, 0); |
| 291 | |
Saeed Bishara | defb451 | 2009-01-06 14:42:24 -0800 | [diff] [blame] | 292 | rtc_device_unregister(pdata->rtc); |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | static struct platform_driver mv_rtc_driver = { |
| 297 | .remove = __exit_p(mv_rtc_remove), |
| 298 | .driver = { |
| 299 | .name = "rtc-mv", |
| 300 | .owner = THIS_MODULE, |
| 301 | }, |
| 302 | }; |
| 303 | |
| 304 | static __init int mv_init(void) |
| 305 | { |
| 306 | return platform_driver_probe(&mv_rtc_driver, mv_rtc_probe); |
| 307 | } |
| 308 | |
| 309 | static __exit void mv_exit(void) |
| 310 | { |
| 311 | platform_driver_unregister(&mv_rtc_driver); |
| 312 | } |
| 313 | |
| 314 | module_init(mv_init); |
| 315 | module_exit(mv_exit); |
| 316 | |
| 317 | MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>"); |
| 318 | MODULE_DESCRIPTION("Marvell RTC driver"); |
| 319 | MODULE_LICENSE("GPL"); |
| 320 | MODULE_ALIAS("platform:rtc-mv"); |