Skip to content

Commit 3ed345c

Browse files
Wolfram Sangalexandrebelloni
authored andcommitted
rtc: rzn1: reduce register access
This RTC has special 32bit registers which return multiple of the same 8bit registers at once. Use these to minimize register access. Also, do the to/from BCD conversions right away, so 'tm' always contains values as described in time.h. Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com> Acked-by: Miquel Raynal <miquel.raynal@bootlin.com> Link: https://lore.kernel.org/r/20241122101448.4374-3-wsa+renesas@sang-engineering.com Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
1 parent 692f983 commit 3ed345c

File tree

1 file changed

+33
-42
lines changed

1 file changed

+33
-42
lines changed

drivers/rtc/rtc-rzn1.c

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@
3535
#define RZN1_RTC_CTL2_WUST BIT(5)
3636
#define RZN1_RTC_CTL2_STOPPED (RZN1_RTC_CTL2_WAIT | RZN1_RTC_CTL2_WST)
3737

38-
#define RZN1_RTC_SEC 0x14
39-
#define RZN1_RTC_MIN 0x18
40-
#define RZN1_RTC_HOUR 0x1c
41-
#define RZN1_RTC_WEEK 0x20
42-
#define RZN1_RTC_DAY 0x24
43-
#define RZN1_RTC_MONTH 0x28
44-
#define RZN1_RTC_YEAR 0x2c
38+
#define RZN1_RTC_TIME 0x30
39+
#define RZN1_RTC_TIME_MIN_SHIFT 8
40+
#define RZN1_RTC_TIME_HOUR_SHIFT 16
41+
#define RZN1_RTC_CAL 0x34
42+
#define RZN1_RTC_CAL_DAY_SHIFT 8
43+
#define RZN1_RTC_CAL_MON_SHIFT 16
44+
#define RZN1_RTC_CAL_YEAR_SHIFT 24
4545

4646
#define RZN1_RTC_SUBU 0x38
4747
#define RZN1_RTC_SUBU_DEV BIT(7)
@@ -52,12 +52,8 @@
5252
#define RZN1_RTC_ALW 0x48
5353

5454
#define RZN1_RTC_SECC 0x4c
55-
#define RZN1_RTC_MINC 0x50
56-
#define RZN1_RTC_HOURC 0x54
57-
#define RZN1_RTC_WEEKC 0x58
58-
#define RZN1_RTC_DAYC 0x5c
59-
#define RZN1_RTC_MONTHC 0x60
60-
#define RZN1_RTC_YEARC 0x64
55+
#define RZN1_RTC_TIMEC 0x68
56+
#define RZN1_RTC_CALC 0x6c
6157

6258
struct rzn1_rtc {
6359
struct rtc_device *rtcdev;
@@ -66,13 +62,18 @@ struct rzn1_rtc {
6662

6763
static void rzn1_rtc_get_time_snapshot(struct rzn1_rtc *rtc, struct rtc_time *tm)
6864
{
69-
tm->tm_sec = readl(rtc->base + RZN1_RTC_SECC);
70-
tm->tm_min = readl(rtc->base + RZN1_RTC_MINC);
71-
tm->tm_hour = readl(rtc->base + RZN1_RTC_HOURC);
72-
tm->tm_wday = readl(rtc->base + RZN1_RTC_WEEKC);
73-
tm->tm_mday = readl(rtc->base + RZN1_RTC_DAYC);
74-
tm->tm_mon = readl(rtc->base + RZN1_RTC_MONTHC);
75-
tm->tm_year = readl(rtc->base + RZN1_RTC_YEARC);
65+
u32 val;
66+
67+
val = readl(rtc->base + RZN1_RTC_TIMEC);
68+
tm->tm_sec = bcd2bin(val);
69+
tm->tm_min = bcd2bin(val >> RZN1_RTC_TIME_MIN_SHIFT);
70+
tm->tm_hour = bcd2bin(val >> RZN1_RTC_TIME_HOUR_SHIFT);
71+
72+
val = readl(rtc->base + RZN1_RTC_CALC);
73+
tm->tm_wday = val & 0x0f;
74+
tm->tm_mday = bcd2bin(val >> RZN1_RTC_CAL_DAY_SHIFT);
75+
tm->tm_mon = bcd2bin(val >> RZN1_RTC_CAL_MON_SHIFT) - 1;
76+
tm->tm_year = bcd2bin(val >> RZN1_RTC_CAL_YEAR_SHIFT) + 100;
7677
}
7778

7879
static int rzn1_rtc_read_time(struct device *dev, struct rtc_time *tm)
@@ -90,16 +91,9 @@ static int rzn1_rtc_read_time(struct device *dev, struct rtc_time *tm)
9091

9192
rzn1_rtc_get_time_snapshot(rtc, tm);
9293
secs = readl(rtc->base + RZN1_RTC_SECC);
93-
if (tm->tm_sec != secs)
94+
if (tm->tm_sec != bcd2bin(secs))
9495
rzn1_rtc_get_time_snapshot(rtc, tm);
9596

96-
tm->tm_sec = bcd2bin(tm->tm_sec);
97-
tm->tm_min = bcd2bin(tm->tm_min);
98-
tm->tm_hour = bcd2bin(tm->tm_hour);
99-
tm->tm_mday = bcd2bin(tm->tm_mday);
100-
tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
101-
tm->tm_year = bcd2bin(tm->tm_year) + 100;
102-
10397
return 0;
10498
}
10599

@@ -109,13 +103,6 @@ static int rzn1_rtc_set_time(struct device *dev, struct rtc_time *tm)
109103
u32 val;
110104
int ret;
111105

112-
tm->tm_sec = bin2bcd(tm->tm_sec);
113-
tm->tm_min = bin2bcd(tm->tm_min);
114-
tm->tm_hour = bin2bcd(tm->tm_hour);
115-
tm->tm_mday = bin2bcd(tm->tm_mday);
116-
tm->tm_mon = bin2bcd(tm->tm_mon + 1);
117-
tm->tm_year = bin2bcd(tm->tm_year - 100);
118-
119106
val = readl(rtc->base + RZN1_RTC_CTL2);
120107
if (!(val & RZN1_RTC_CTL2_STOPPED)) {
121108
/* Hold the counter if it was counting up */
@@ -129,13 +116,17 @@ static int rzn1_rtc_set_time(struct device *dev, struct rtc_time *tm)
129116
return ret;
130117
}
131118

132-
writel(tm->tm_sec, rtc->base + RZN1_RTC_SEC);
133-
writel(tm->tm_min, rtc->base + RZN1_RTC_MIN);
134-
writel(tm->tm_hour, rtc->base + RZN1_RTC_HOUR);
135-
writel(tm->tm_wday, rtc->base + RZN1_RTC_WEEK);
136-
writel(tm->tm_mday, rtc->base + RZN1_RTC_DAY);
137-
writel(tm->tm_mon, rtc->base + RZN1_RTC_MONTH);
138-
writel(tm->tm_year, rtc->base + RZN1_RTC_YEAR);
119+
val = bin2bcd(tm->tm_sec);
120+
val |= bin2bcd(tm->tm_min) << RZN1_RTC_TIME_MIN_SHIFT;
121+
val |= bin2bcd(tm->tm_hour) << RZN1_RTC_TIME_HOUR_SHIFT;
122+
writel(val, rtc->base + RZN1_RTC_TIME);
123+
124+
val = tm->tm_wday;
125+
val |= bin2bcd(tm->tm_mday) << RZN1_RTC_CAL_DAY_SHIFT;
126+
val |= bin2bcd(tm->tm_mon + 1) << RZN1_RTC_CAL_MON_SHIFT;
127+
val |= bin2bcd(tm->tm_year - 100) << RZN1_RTC_CAL_YEAR_SHIFT;
128+
writel(val, rtc->base + RZN1_RTC_CAL);
129+
139130
writel(0, rtc->base + RZN1_RTC_CTL2);
140131

141132
return 0;

0 commit comments

Comments
 (0)