Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2008 |
| 4 | * Gururaja Hebbar gururajakr@sanyo.co.in |
| 5 | * |
| 6 | * reference linux-2.6.20.6/drivers/rtc/rtc-pl031.c |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <command.h> |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 11 | #include <dm.h> |
| 12 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 14 | #include <rtc.h> |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 15 | #include <asm/io.h> |
| 16 | #include <asm/types.h> |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * Register definitions |
| 20 | */ |
| 21 | #define RTC_DR 0x00 /* Data read register */ |
| 22 | #define RTC_MR 0x04 /* Match register */ |
| 23 | #define RTC_LR 0x08 /* Data load register */ |
| 24 | #define RTC_CR 0x0c /* Control register */ |
| 25 | #define RTC_IMSC 0x10 /* Interrupt mask and set register */ |
| 26 | #define RTC_RIS 0x14 /* Raw interrupt status register */ |
| 27 | #define RTC_MIS 0x18 /* Masked interrupt status register */ |
| 28 | #define RTC_ICR 0x1c /* Interrupt clear register */ |
| 29 | |
| 30 | #define RTC_CR_START (1 << 0) |
| 31 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 32 | struct pl031_plat { |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 33 | phys_addr_t base; |
| 34 | }; |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 35 | |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 36 | static inline u32 pl031_read_reg(struct udevice *dev, int reg) |
| 37 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 38 | struct pl031_plat *pdata = dev_get_plat(dev); |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 39 | |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 40 | return readl(pdata->base + reg); |
| 41 | } |
| 42 | |
| 43 | static inline u32 pl031_write_reg(struct udevice *dev, int reg, u32 value) |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 44 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 45 | struct pl031_plat *pdata = dev_get_plat(dev); |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 46 | |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 47 | return writel(value, pdata->base + reg); |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /* |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 51 | * Probe RTC device |
| 52 | */ |
| 53 | static int pl031_probe(struct udevice *dev) |
| 54 | { |
| 55 | /* Enable RTC Start in Control register*/ |
| 56 | pl031_write_reg(dev, RTC_CR, RTC_CR_START); |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Get the current time from the RTC |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 63 | */ |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 64 | static int pl031_get(struct udevice *dev, struct rtc_time *tm) |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 65 | { |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 66 | unsigned long tim; |
| 67 | |
| 68 | if (!tm) |
| 69 | return -EINVAL; |
| 70 | |
| 71 | tim = pl031_read_reg(dev, RTC_DR); |
| 72 | |
| 73 | rtc_to_tm(tim, tm); |
| 74 | |
| 75 | debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 76 | tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday, |
| 77 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 78 | |
| 79 | return 0; |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Set the RTC |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 84 | */ |
| 85 | static int pl031_set(struct udevice *dev, const struct rtc_time *tm) |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 86 | { |
| 87 | unsigned long tim; |
| 88 | |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 89 | if (!tm) |
| 90 | return -EINVAL; |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 91 | |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 92 | debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
| 93 | tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday, |
| 94 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 95 | |
| 96 | /* Calculate number of seconds this incoming time represents */ |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 97 | tim = rtc_mktime(tm); |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 98 | |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 99 | pl031_write_reg(dev, RTC_LR, tim); |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 100 | |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 101 | return 0; |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 105 | * Reset the RTC. We set the date back to 1970-01-01. |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 106 | */ |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 107 | static int pl031_reset(struct udevice *dev) |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 108 | { |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 109 | pl031_write_reg(dev, RTC_LR, 0); |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 110 | |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 111 | return 0; |
| 112 | } |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 113 | |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 114 | static const struct rtc_ops pl031_ops = { |
| 115 | .get = pl031_get, |
| 116 | .set = pl031_set, |
| 117 | .reset = pl031_reset, |
| 118 | }; |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 119 | |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 120 | static const struct udevice_id pl031_ids[] = { |
| 121 | { .compatible = "arm,pl031" }, |
| 122 | { } |
| 123 | }; |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 124 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 125 | static int pl031_of_to_plat(struct udevice *dev) |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 126 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 127 | struct pl031_plat *pdata = dev_get_plat(dev); |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 128 | |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 129 | pdata->base = dev_read_addr(dev); |
Gururaja Hebbar K R | 76873a6 | 2008-08-25 11:30:29 +0200 | [diff] [blame] | 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 134 | U_BOOT_DRIVER(rtc_pl031) = { |
| 135 | .name = "rtc-pl031", |
| 136 | .id = UCLASS_RTC, |
| 137 | .of_match = pl031_ids, |
| 138 | .probe = pl031_probe, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 139 | .of_to_plat = pl031_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 140 | .plat_auto = sizeof(struct pl031_plat), |
AKASHI Takahiro | cbba155 | 2018-09-14 17:06:53 +0900 | [diff] [blame] | 141 | .ops = &pl031_ops, |
| 142 | }; |