1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2009-2012 ADVANSEE
4 * Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
6 * Based on the Linux rtc-imxdi.c driver, which is:
7 * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
8 * Copyright 2010 Orex Computed Radiography
12 * Date & Time support for Freescale i.MX DryIce RTC
17 #include <linux/compat.h>
21 #include <asm/arch/imx-regs.h>
23 /* DryIce Register Definitions */
26 u32 dtcmr; /* Time Counter MSB Reg */
27 u32 dtclr; /* Time Counter LSB Reg */
28 u32 dcamr; /* Clock Alarm MSB Reg */
29 u32 dcalr; /* Clock Alarm LSB Reg */
30 u32 dcr; /* Control Reg */
31 u32 dsr; /* Status Reg */
32 u32 dier; /* Interrupt Enable Reg */
35 #define DCAMR_UNSET 0xFFFFFFFF /* doomsday - 1 sec */
37 #define DCR_TCE (1 << 3) /* Time Counter Enable */
39 #define DSR_WBF (1 << 10) /* Write Busy Flag */
40 #define DSR_WNF (1 << 9) /* Write Next Flag */
41 #define DSR_WCF (1 << 8) /* Write Complete Flag */
42 #define DSR_WEF (1 << 7) /* Write Error Flag */
43 #define DSR_CAF (1 << 4) /* Clock Alarm Flag */
44 #define DSR_NVF (1 << 1) /* Non-Valid Flag */
45 #define DSR_SVF (1 << 0) /* Security Violation Flag */
47 #define DIER_WNIE (1 << 9) /* Write Next Interrupt Enable */
48 #define DIER_WCIE (1 << 8) /* Write Complete Interrupt Enable */
49 #define DIER_WEIE (1 << 7) /* Write Error Interrupt Enable */
50 #define DIER_CAIE (1 << 4) /* Clock Alarm Interrupt Enable */
52 /* Driver Private Data */
55 struct imxdi_regs __iomem *regs;
59 static struct imxdi_data data;
62 * This function attempts to clear the dryice write-error flag.
64 * A dryice write error is similar to a bus fault and should not occur in
65 * normal operation. Clearing the flag requires another write, so the root
66 * cause of the problem may need to be fixed before the flag can be cleared.
68 static void clear_write_error(void)
72 puts("### Warning: RTC - Register write error!\n");
74 /* clear the write error flag */
75 __raw_writel(DSR_WEF, &data.regs->dsr);
77 /* wait for it to take effect */
78 for (cnt = 0; cnt < 1000; cnt++) {
79 if ((__raw_readl(&data.regs->dsr) & DSR_WEF) == 0)
83 puts("### Error: RTC - Cannot clear write-error flag!\n");
87 * Write a dryice register and wait until it completes.
89 * Use interrupt flags to determine when the write has completed.
91 #define DI_WRITE_WAIT(val, reg) \
93 /* do the register write */ \
94 __raw_writel((val), &data.regs->reg), \
96 di_write_wait((val), #reg) \
98 static int di_write_wait(u32 val, const char *reg)
104 /* wait for the write to finish */
105 for (cnt = 0; cnt < 100; cnt++) {
106 if ((__raw_readl(&data.regs->dsr) & (DSR_WCF | DSR_WEF)) != 0) {
113 printf("### Warning: RTC - Write-wait timeout "
114 "val = 0x%.8x reg = %s\n", val, reg);
116 /* check for write error */
117 if (__raw_readl(&data.regs->dsr) & DSR_WEF) {
126 * Initialize dryice hardware
128 static int di_init(void)
132 data.regs = (struct imxdi_regs __iomem *)IMX_DRYICE_BASE;
134 /* mask all interrupts */
135 __raw_writel(0, &data.regs->dier);
137 /* put dryice into valid state */
138 if (__raw_readl(&data.regs->dsr) & DSR_NVF) {
139 rc = DI_WRITE_WAIT(DSR_NVF | DSR_SVF, dsr);
144 /* initialize alarm */
145 rc = DI_WRITE_WAIT(DCAMR_UNSET, dcamr);
148 rc = DI_WRITE_WAIT(0, dcalr);
152 /* clear alarm flag */
153 if (__raw_readl(&data.regs->dsr) & DSR_CAF) {
154 rc = DI_WRITE_WAIT(DSR_CAF, dsr);
159 /* the timer won't count if it has never been written to */
160 if (__raw_readl(&data.regs->dtcmr) == 0) {
161 rc = DI_WRITE_WAIT(0, dtcmr);
166 /* start keeping time */
167 if (!(__raw_readl(&data.regs->dcr) & DCR_TCE)) {
168 rc = DI_WRITE_WAIT(__raw_readl(&data.regs->dcr) | DCR_TCE, dcr);
180 int rtc_get(struct rtc_time *tmp)
185 if (!data.init_done) {
191 now = __raw_readl(&data.regs->dtcmr);
198 int rtc_set(struct rtc_time *tmp)
203 if (!data.init_done) {
209 now = rtc_mktime(tmp);
210 /* zero the fractional part first */
211 rc = DI_WRITE_WAIT(0, dtclr);
213 rc = DI_WRITE_WAIT(now, dtcmr);