| 1 | |
| 2 | |
| 3 | * Driver for the RTC in Marvell SoCs.
|
| 4 |
|
| 5 |
|
| 6 | #include <linux/init.h>
|
| 7 | #include <linux/kernel.h>
|
| 8 | #include <linux/rtc.h>
|
| 9 | #include <linux/bcd.h>
|
| 10 | #include <linux/bitops.h>
|
| 11 | #include <linux/io.h>
|
| 12 | #include <linux/platform_device.h>
|
| 13 | #include <linux/of.h>
|
| 14 | #include <linux/delay.h>
|
| 15 | #include <linux/clk.h>
|
| 16 | #include <linux/gfp.h>
|
| 17 | #include <linux/module.h>
|
| 18 |
|
| 19 |
|
| 20 | #define RTC_TIME_REG_OFFS 0
|
| 21 | #define RTC_SECONDS_OFFS 0
|
| 22 | #define RTC_MINUTES_OFFS 8
|
| 23 | #define RTC_HOURS_OFFS 16
|
| 24 | #define RTC_WDAY_OFFS 24
|
| 25 | #define RTC_HOURS_12H_MODE BIT(22)
|
| 26 |
|
| 27 | #define RTC_DATE_REG_OFFS 4
|
| 28 | #define RTC_MDAY_OFFS 0
|
| 29 | #define RTC_MONTH_OFFS 8
|
| 30 | #define RTC_YEAR_OFFS 16
|
| 31 |
|
| 32 | #define RTC_ALARM_TIME_REG_OFFS 8
|
| 33 | #define RTC_ALARM_DATE_REG_OFFS 0xc
|
| 34 | #define RTC_ALARM_VALID BIT(7)
|
| 35 |
|
| 36 | #define RTC_ALARM_INTERRUPT_MASK_REG_OFFS 0x10
|
| 37 | #define RTC_ALARM_INTERRUPT_CASUE_REG_OFFS 0x14
|
| 38 |
|
| 39 | struct rtc_plat_data {
|
| 40 | struct rtc_device *rtc;
|
| 41 | void __iomem *ioaddr;
|
| 42 | int irq;
|
| 43 | struct clk *clk;
|
| 44 | };
|
| 45 |
|
| 46 | static int mv_rtc_set_time(struct device *dev, struct rtc_time *tm)
|
| 47 | {
|
| 48 | struct rtc_plat_data *pdata = dev_get_drvdata(dev);
|
| 49 | void __iomem *ioaddr = pdata->ioaddr;
|
| 50 | u32 rtc_reg;
|
| 51 |
|
| 52 | rtc_reg = (bin2bcd(tm->tm_sec) << RTC_SECONDS_OFFS) |
|
| 53 | (bin2bcd(tm->tm_min) << RTC_MINUTES_OFFS) |
|
| 54 | (bin2bcd(tm->tm_hour) << RTC_HOURS_OFFS) |
|
| 55 | (bin2bcd(tm->tm_wday) << RTC_WDAY_OFFS);
|
| 56 | writel(rtc_reg, ioaddr + RTC_TIME_REG_OFFS);
|
| 57 |
|
| 58 | rtc_reg = (bin2bcd(tm->tm_mday) << RTC_MDAY_OFFS) |
|
| 59 | (bin2bcd(tm->tm_mon + 1) << RTC_MONTH_OFFS) |
|
| 60 | (bin2bcd(tm->tm_year - 100) << RTC_YEAR_OFFS);
|
| 61 | writel(rtc_reg, ioaddr + RTC_DATE_REG_OFFS);
|
| 62 |
|
| 63 | return 0;
|
| 64 | }
|
| 65 |
|
| 66 | static int mv_rtc_read_time(struct device *dev, struct rtc_time *tm)
|
| 67 | {
|
| 68 | struct rtc_plat_data *pdata = dev_get_drvdata(dev);
|
| 69 | void __iomem *ioaddr = pdata->ioaddr;
|
| 70 | u32 rtc_time, rtc_date;
|
| 71 | unsigned int year, month, day, hour, minute, second, wday;
|
| 72 |
|
| 73 | rtc_time = readl(ioaddr + RTC_TIME_REG_OFFS);
|
| 74 | rtc_date = readl(ioaddr + RTC_DATE_REG_OFFS);
|
| 75 |
|
| 76 | second = rtc_time & 0x7f;
|
| 77 | minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
|
| 78 | hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f;
|
| 79 | wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
|
| 80 |
|
| 81 | day = rtc_date & 0x3f;
|
| 82 | month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
|
| 83 | year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
|
| 84 |
|
| 85 | tm->tm_sec = bcd2bin(second);
|
| 86 | tm->tm_min = bcd2bin(minute);
|
| 87 | tm->tm_hour = bcd2bin(hour);
|
| 88 | tm->tm_mday = bcd2bin(day);
|
| 89 | tm->tm_wday = bcd2bin(wday);
|
| 90 | tm->tm_mon = bcd2bin(month) - 1;
|
| 91 |
|
| 92 | tm->tm_year = bcd2bin(year) + 100;
|
| 93 |
|
| 94 | return 0;
|
| 95 | }
|
| 96 |
|
| 97 | static int mv_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
|
| 98 | {
|
| 99 | struct rtc_plat_data *pdata = dev_get_drvdata(dev);
|
| 100 | void __iomem *ioaddr = pdata->ioaddr;
|
| 101 | u32 rtc_time, rtc_date;
|
| 102 | unsigned int year, month, day, hour, minute, second, wday;
|
| 103 |
|
| 104 | rtc_time = readl(ioaddr + RTC_ALARM_TIME_REG_OFFS);
|
| 105 | rtc_date = readl(ioaddr + RTC_ALARM_DATE_REG_OFFS);
|
| 106 |
|
| 107 | second = rtc_time & 0x7f;
|
| 108 | minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
|
| 109 | hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f;
|
| 110 | wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
|
| 111 |
|
| 112 | day = rtc_date & 0x3f;
|
| 113 | month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
|
| 114 | year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
|
| 115 |
|
| 116 | alm->time.tm_sec = bcd2bin(second);
|
| 117 | alm->time.tm_min = bcd2bin(minute);
|
| 118 | alm->time.tm_hour = bcd2bin(hour);
|
| 119 | alm->time.tm_mday = bcd2bin(day);
|
| 120 | alm->time.tm_wday = bcd2bin(wday);
|
| 121 | alm->time.tm_mon = bcd2bin(month) - 1;
|
| 122 |
|
| 123 | alm->time.tm_year = bcd2bin(year) + 100;
|
| 124 |
|
| 125 | alm->enabled = !!readl(ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
|
| 126 |
|
| 127 | return rtc_valid_tm(&alm->time);
|
| 128 | }
|
| 129 |
|
| 130 | static int mv_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
|
| 131 | {
|
| 132 | struct rtc_plat_data *pdata = dev_get_drvdata(dev);
|
| 133 | void __iomem *ioaddr = pdata->ioaddr;
|
| 134 | u32 rtc_reg = 0;
|
| 135 |
|
| 136 | if (alm->time.tm_sec >= 0)
|
| 137 | rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_sec))
|
| 138 | << RTC_SECONDS_OFFS;
|
| 139 | if (alm->time.tm_min >= 0)
|
| 140 | rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_min))
|
| 141 | << RTC_MINUTES_OFFS;
|
| 142 | if (alm->time.tm_hour >= 0)
|
| 143 | rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_hour))
|
| 144 | << RTC_HOURS_OFFS;
|
| 145 |
|
| 146 | writel(rtc_reg, ioaddr + RTC_ALARM_TIME_REG_OFFS);
|
| 147 |
|
| 148 | if (alm->time.tm_mday >= 0)
|
| 149 | rtc_reg = (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mday))
|
| 150 | << RTC_MDAY_OFFS;
|
| 151 | else
|
| 152 | rtc_reg = 0;
|
| 153 |
|
| 154 | if (alm->time.tm_mon >= 0)
|
| 155 | rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mon + 1))
|
| 156 | << RTC_MONTH_OFFS;
|
| 157 |
|
| 158 | if (alm->time.tm_year >= 0)
|
| 159 | rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_year - 100))
|
| 160 | << RTC_YEAR_OFFS;
|
| 161 |
|
| 162 | writel(rtc_reg, ioaddr + RTC_ALARM_DATE_REG_OFFS);
|
| 163 | writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
|
| 164 | writel(alm->enabled ? 1 : 0,
|
| 165 | ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
|
| 166 |
|
| 167 | return 0;
|
| 168 | }
|
| 169 |
|
| 170 | static int mv_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
|
| 171 | {
|
| 172 | struct rtc_plat_data *pdata = dev_get_drvdata(dev);
|
| 173 | void __iomem *ioaddr = pdata->ioaddr;
|
| 174 |
|
| 175 | if (pdata->irq < 0)
|
| 176 | return -EINVAL;
|
| 177 |
|
| 178 | if (enabled)
|
| 179 | writel(1, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
|
| 180 | else
|
| 181 | writel(0, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
|
| 182 | return 0;
|
| 183 | }
|
| 184 |
|
| 185 | static irqreturn_t mv_rtc_interrupt(int irq, void *data)
|
| 186 | {
|
| 187 | struct rtc_plat_data *pdata = data;
|
| 188 | void __iomem *ioaddr = pdata->ioaddr;
|
| 189 |
|
| 190 |
|
| 191 | if (!readl(ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS))
|
| 192 | return IRQ_NONE;
|
| 193 |
|
| 194 |
|
| 195 | writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
|
| 196 | rtc_update_irq(pdata->rtc, 1, RTC_IRQF | RTC_AF);
|
| 197 | return IRQ_HANDLED;
|
| 198 | }
|
| 199 |
|
| 200 | static const struct rtc_class_ops mv_rtc_ops = {
|
| 201 | .read_time = mv_rtc_read_time,
|
| 202 | .set_time = mv_rtc_set_time,
|
| 203 | .read_alarm = mv_rtc_read_alarm,
|
| 204 | .set_alarm = mv_rtc_set_alarm,
|
| 205 | .alarm_irq_enable = mv_rtc_alarm_irq_enable,
|
| 206 | };
|
| 207 |
|
| 208 | static int __init mv_rtc_probe(struct platform_device *pdev)
|
| 209 | {
|
| 210 | struct rtc_plat_data *pdata;
|
| 211 | u32 rtc_time;
|
| 212 | int ret = 0;
|
| 213 |
|
| 214 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
|
| 215 | if (!pdata)
|
| 216 | return -ENOMEM;
|
| 217 |
|
| 218 | pdata->ioaddr = devm_platform_ioremap_resource(pdev, 0);
|
| 219 | if (IS_ERR(pdata->ioaddr))
|
| 220 | return PTR_ERR(pdata->ioaddr);
|
| 221 |
|
| 222 | pdata->clk = devm_clk_get_optional_prepared(&pdev->dev, NULL);
|
| 223 | if (IS_ERR(pdata->clk))
|
| 224 | return PTR_ERR(pdata->clk);
|
| 225 |
|
| 226 |
|
| 227 | rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
|
| 228 | if (rtc_time & RTC_HOURS_12H_MODE) {
|
| 229 | dev_err(&pdev->dev, "12 Hour mode is enabled but not supported.\n");
|
| 230 | return -EINVAL;
|
| 231 | }
|
| 232 |
|
| 233 |
|
| 234 | if (rtc_time == 0x01000000) {
|
| 235 | ssleep(1);
|
| 236 | rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
|
| 237 | if (rtc_time == 0x01000000) {
|
| 238 | dev_err(&pdev->dev, "internal RTC not ticking\n");
|
| 239 | return -ENODEV;
|
| 240 | }
|
| 241 | }
|
| 242 |
|
| 243 | pdata->irq = platform_get_irq(pdev, 0);
|
| 244 |
|
| 245 | platform_set_drvdata(pdev, pdata);
|
| 246 |
|
| 247 | pdata->rtc = devm_rtc_allocate_device(&pdev->dev);
|
| 248 | if (IS_ERR(pdata->rtc)) {
|
| 249 | return PTR_ERR(pdata->rtc);
|
| 250 | }
|
| 251 |
|
| 252 | if (pdata->irq >= 0) {
|
| 253 | writel(0, pdata->ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
|
| 254 | if (devm_request_irq(&pdev->dev, pdata->irq, mv_rtc_interrupt,
|
| 255 | IRQF_SHARED,
|
| 256 | pdev->name, pdata) < 0) {
|
| 257 | dev_warn(&pdev->dev, "interrupt not available.\n");
|
| 258 | pdata->irq = -1;
|
| 259 | }
|
| 260 | }
|
| 261 |
|
| 262 | if (pdata->irq >= 0)
|
| 263 | devm_device_init_wakeup(&pdev->dev);
|
| 264 | else
|
| 265 | clear_bit(RTC_FEATURE_ALARM, pdata->rtc->features);
|
| 266 |
|
| 267 | pdata->rtc->ops = &mv_rtc_ops;
|
| 268 | pdata->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
|
| 269 | pdata->rtc->range_max = RTC_TIMESTAMP_END_2099;
|
| 270 |
|
| 271 | ret = devm_rtc_register_device(pdata->rtc);
|
| 272 | if (!ret)
|
| 273 | return 0;
|
| 274 |
|
| 275 | return ret;
|
| 276 | }
|
| 277 |
|
| 278 | #ifdef CONFIG_OF
|
| 279 | static const struct of_device_id rtc_mv_of_match_table[] = {
|
| 280 | { .compatible = "marvell,orion-rtc", },
|
| 281 | {}
|
| 282 | };
|
| 283 | MODULE_DEVICE_TABLE(of, rtc_mv_of_match_table);
|
| 284 | #endif
|
| 285 |
|
| 286 | #ifdef CONFIG_PM_SLEEP
|
| 287 | static int mv_rtc_suspend(struct device *dev)
|
| 288 | {
|
| 289 | struct rtc_plat_data *pdata = dev_get_drvdata(dev);
|
| 290 |
|
| 291 | if (device_may_wakeup(dev) && pdata->irq >= 0)
|
| 292 | enable_irq_wake(pdata->irq);
|
| 293 | return 0;
|
| 294 | }
|
| 295 |
|
| 296 | static int mv_rtc_resume(struct device *dev)
|
| 297 | {
|
| 298 | struct rtc_plat_data *pdata = dev_get_drvdata(dev);
|
| 299 |
|
| 300 | if (device_may_wakeup(dev) && pdata->irq >= 0)
|
| 301 | disable_irq_wake(pdata->irq);
|
| 302 | return 0;
|
| 303 | }
|
| 304 | #endif
|
| 305 |
|
| 306 | static SIMPLE_DEV_PM_OPS(mv_rtc_pm_ops, mv_rtc_suspend, mv_rtc_resume);
|
| 307 |
|
| 308 | static struct platform_driver mv_rtc_driver = {
|
| 309 | .driver = {
|
| 310 | .name = "rtc-mv",
|
| 311 | .of_match_table = of_match_ptr(rtc_mv_of_match_table),
|
| 312 | .pm = &mv_rtc_pm_ops,
|
| 313 | },
|
| 314 | };
|
| 315 |
|
| 316 | module_platform_driver_probe(mv_rtc_driver, mv_rtc_probe);
|
| 317 |
|
| 318 | MODULE_AUTHOR("Saeed Bishara <[email protected]>");
|
| 319 | MODULE_DESCRIPTION("Marvell RTC driver");
|
| 320 | MODULE_LICENSE("GPL");
|
| 321 | MODULE_ALIAS("platform:rtc-mv");
|