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