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>
19 #include <linux/delay.h>
22 #include <asm/arch/imx-regs.h>
24 /* DryIce Register Definitions */
27 u32 dtcmr; /* Time Counter MSB Reg */
28 u32 dtclr; /* Time Counter LSB Reg */
29 u32 dcamr; /* Clock Alarm MSB Reg */
30 u32 dcalr; /* Clock Alarm LSB Reg */
31 u32 dcr; /* Control Reg */
32 u32 dsr; /* Status Reg */
33 u32 dier; /* Interrupt Enable Reg */
36 #define DCAMR_UNSET 0xFFFFFFFF /* doomsday - 1 sec */
38 #define DCR_TCE (1 << 3) /* Time Counter Enable */
40 #define DSR_WBF (1 << 10) /* Write Busy Flag */
41 #define DSR_WNF (1 << 9) /* Write Next Flag */
42 #define DSR_WCF (1 << 8) /* Write Complete Flag */
43 #define DSR_WEF (1 << 7) /* Write Error Flag */
44 #define DSR_CAF (1 << 4) /* Clock Alarm Flag */
45 #define DSR_NVF (1 << 1) /* Non-Valid Flag */
46 #define DSR_SVF (1 << 0) /* Security Violation Flag */
48 #define DIER_WNIE (1 << 9) /* Write Next Interrupt Enable */
49 #define DIER_WCIE (1 << 8) /* Write Complete Interrupt Enable */
50 #define DIER_WEIE (1 << 7) /* Write Error Interrupt Enable */
51 #define DIER_CAIE (1 << 4) /* Clock Alarm Interrupt Enable */
53 /* Driver Private Data */
56 struct imxdi_regs __iomem *regs;
60 static struct imxdi_data data;
63 * This function attempts to clear the dryice write-error flag.
65 * A dryice write error is similar to a bus fault and should not occur in
66 * normal operation. Clearing the flag requires another write, so the root
67 * cause of the problem may need to be fixed before the flag can be cleared.
69 static void clear_write_error(void)
73 puts("### Warning: RTC - Register write error!\n");
75 /* clear the write error flag */
76 __raw_writel(DSR_WEF, &data.regs->dsr);
78 /* wait for it to take effect */
79 for (cnt = 0; cnt < 1000; cnt++) {
80 if ((__raw_readl(&data.regs->dsr) & DSR_WEF) == 0)
84 puts("### Error: RTC - Cannot clear write-error flag!\n");
88 * Write a dryice register and wait until it completes.
90 * Use interrupt flags to determine when the write has completed.
92 #define DI_WRITE_WAIT(val, reg) \
94 /* do the register write */ \
95 __raw_writel((val), &data.regs->reg), \
97 di_write_wait((val), #reg) \
99 static int di_write_wait(u32 val, const char *reg)
105 /* wait for the write to finish */
106 for (cnt = 0; cnt < 100; cnt++) {
107 if ((__raw_readl(&data.regs->dsr) & (DSR_WCF | DSR_WEF)) != 0) {
114 printf("### Warning: RTC - Write-wait timeout "
115 "val = 0x%.8x reg = %s\n", val, reg);
117 /* check for write error */
118 if (__raw_readl(&data.regs->dsr) & DSR_WEF) {
127 * Initialize dryice hardware
129 static int di_init(void)
133 data.regs = (struct imxdi_regs __iomem *)IMX_DRYICE_BASE;
135 /* mask all interrupts */
136 __raw_writel(0, &data.regs->dier);
138 /* put dryice into valid state */
139 if (__raw_readl(&data.regs->dsr) & DSR_NVF) {
140 rc = DI_WRITE_WAIT(DSR_NVF | DSR_SVF, dsr);
145 /* initialize alarm */
146 rc = DI_WRITE_WAIT(DCAMR_UNSET, dcamr);
149 rc = DI_WRITE_WAIT(0, dcalr);
153 /* clear alarm flag */
154 if (__raw_readl(&data.regs->dsr) & DSR_CAF) {
155 rc = DI_WRITE_WAIT(DSR_CAF, dsr);
160 /* the timer won't count if it has never been written to */
161 if (__raw_readl(&data.regs->dtcmr) == 0) {
162 rc = DI_WRITE_WAIT(0, dtcmr);
167 /* start keeping time */
168 if (!(__raw_readl(&data.regs->dcr) & DCR_TCE)) {
169 rc = DI_WRITE_WAIT(__raw_readl(&data.regs->dcr) | DCR_TCE, dcr);
181 int rtc_get(struct rtc_time *tmp)
186 if (!data.init_done) {
192 now = __raw_readl(&data.regs->dtcmr);
199 int rtc_set(struct rtc_time *tmp)
204 if (!data.init_done) {
210 now = rtc_mktime(tmp);
211 /* zero the fractional part first */
212 rc = DI_WRITE_WAIT(0, dtclr);
214 rc = DI_WRITE_WAIT(now, dtcmr);