18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
48c2ecf20Sopenharmony_ci * Copyright 2010 Orex Computed Radiography
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci/*
88c2ecf20Sopenharmony_ci * This driver uses the 47-bit 32 kHz counter in the Freescale DryIce block
98c2ecf20Sopenharmony_ci * to implement a Linux RTC. Times and alarms are truncated to seconds.
108c2ecf20Sopenharmony_ci * Since the RTC framework performs API locking via rtc->ops_lock the
118c2ecf20Sopenharmony_ci * only simultaneous accesses we need to deal with is updating DryIce
128c2ecf20Sopenharmony_ci * registers while servicing an alarm.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * Note that reading the DSR (DryIce Status Register) automatically clears
158c2ecf20Sopenharmony_ci * the WCF (Write Complete Flag). All DryIce writes are synchronized to the
168c2ecf20Sopenharmony_ci * LP (Low Power) domain and set the WCF upon completion. Writes to the
178c2ecf20Sopenharmony_ci * DIER (DryIce Interrupt Enable Register) are the only exception. These
188c2ecf20Sopenharmony_ci * occur at normal bus speeds and do not set WCF.  Periodic interrupts are
198c2ecf20Sopenharmony_ci * not supported by the hardware.
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include <linux/io.h>
238c2ecf20Sopenharmony_ci#include <linux/clk.h>
248c2ecf20Sopenharmony_ci#include <linux/delay.h>
258c2ecf20Sopenharmony_ci#include <linux/module.h>
268c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
278c2ecf20Sopenharmony_ci#include <linux/rtc.h>
288c2ecf20Sopenharmony_ci#include <linux/sched.h>
298c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
308c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
318c2ecf20Sopenharmony_ci#include <linux/of.h>
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/* DryIce Register Definitions */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define DTCMR     0x00           /* Time Counter MSB Reg */
368c2ecf20Sopenharmony_ci#define DTCLR     0x04           /* Time Counter LSB Reg */
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define DCAMR     0x08           /* Clock Alarm MSB Reg */
398c2ecf20Sopenharmony_ci#define DCALR     0x0c           /* Clock Alarm LSB Reg */
408c2ecf20Sopenharmony_ci#define DCAMR_UNSET  0xFFFFFFFF  /* doomsday - 1 sec */
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define DCR       0x10           /* Control Reg */
438c2ecf20Sopenharmony_ci#define DCR_TDCHL (1 << 30)      /* Tamper-detect configuration hard lock */
448c2ecf20Sopenharmony_ci#define DCR_TDCSL (1 << 29)      /* Tamper-detect configuration soft lock */
458c2ecf20Sopenharmony_ci#define DCR_KSSL  (1 << 27)      /* Key-select soft lock */
468c2ecf20Sopenharmony_ci#define DCR_MCHL  (1 << 20)      /* Monotonic-counter hard lock */
478c2ecf20Sopenharmony_ci#define DCR_MCSL  (1 << 19)      /* Monotonic-counter soft lock */
488c2ecf20Sopenharmony_ci#define DCR_TCHL  (1 << 18)      /* Timer-counter hard lock */
498c2ecf20Sopenharmony_ci#define DCR_TCSL  (1 << 17)      /* Timer-counter soft lock */
508c2ecf20Sopenharmony_ci#define DCR_FSHL  (1 << 16)      /* Failure state hard lock */
518c2ecf20Sopenharmony_ci#define DCR_TCE   (1 << 3)       /* Time Counter Enable */
528c2ecf20Sopenharmony_ci#define DCR_MCE   (1 << 2)       /* Monotonic Counter Enable */
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define DSR       0x14           /* Status Reg */
558c2ecf20Sopenharmony_ci#define DSR_WTD   (1 << 23)      /* Wire-mesh tamper detected */
568c2ecf20Sopenharmony_ci#define DSR_ETBD  (1 << 22)      /* External tamper B detected */
578c2ecf20Sopenharmony_ci#define DSR_ETAD  (1 << 21)      /* External tamper A detected */
588c2ecf20Sopenharmony_ci#define DSR_EBD   (1 << 20)      /* External boot detected */
598c2ecf20Sopenharmony_ci#define DSR_SAD   (1 << 19)      /* SCC alarm detected */
608c2ecf20Sopenharmony_ci#define DSR_TTD   (1 << 18)      /* Temperature tamper detected */
618c2ecf20Sopenharmony_ci#define DSR_CTD   (1 << 17)      /* Clock tamper detected */
628c2ecf20Sopenharmony_ci#define DSR_VTD   (1 << 16)      /* Voltage tamper detected */
638c2ecf20Sopenharmony_ci#define DSR_WBF   (1 << 10)      /* Write Busy Flag (synchronous) */
648c2ecf20Sopenharmony_ci#define DSR_WNF   (1 << 9)       /* Write Next Flag (synchronous) */
658c2ecf20Sopenharmony_ci#define DSR_WCF   (1 << 8)       /* Write Complete Flag (synchronous)*/
668c2ecf20Sopenharmony_ci#define DSR_WEF   (1 << 7)       /* Write Error Flag */
678c2ecf20Sopenharmony_ci#define DSR_CAF   (1 << 4)       /* Clock Alarm Flag */
688c2ecf20Sopenharmony_ci#define DSR_MCO   (1 << 3)       /* monotonic counter overflow */
698c2ecf20Sopenharmony_ci#define DSR_TCO   (1 << 2)       /* time counter overflow */
708c2ecf20Sopenharmony_ci#define DSR_NVF   (1 << 1)       /* Non-Valid Flag */
718c2ecf20Sopenharmony_ci#define DSR_SVF   (1 << 0)       /* Security Violation Flag */
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#define DIER      0x18           /* Interrupt Enable Reg (synchronous) */
748c2ecf20Sopenharmony_ci#define DIER_WNIE (1 << 9)       /* Write Next Interrupt Enable */
758c2ecf20Sopenharmony_ci#define DIER_WCIE (1 << 8)       /* Write Complete Interrupt Enable */
768c2ecf20Sopenharmony_ci#define DIER_WEIE (1 << 7)       /* Write Error Interrupt Enable */
778c2ecf20Sopenharmony_ci#define DIER_CAIE (1 << 4)       /* Clock Alarm Interrupt Enable */
788c2ecf20Sopenharmony_ci#define DIER_SVIE (1 << 0)       /* Security-violation Interrupt Enable */
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci#define DMCR      0x1c           /* DryIce Monotonic Counter Reg */
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define DTCR      0x28           /* DryIce Tamper Configuration Reg */
838c2ecf20Sopenharmony_ci#define DTCR_MOE  (1 << 9)       /* monotonic overflow enabled */
848c2ecf20Sopenharmony_ci#define DTCR_TOE  (1 << 8)       /* time overflow enabled */
858c2ecf20Sopenharmony_ci#define DTCR_WTE  (1 << 7)       /* wire-mesh tamper enabled */
868c2ecf20Sopenharmony_ci#define DTCR_ETBE (1 << 6)       /* external B tamper enabled */
878c2ecf20Sopenharmony_ci#define DTCR_ETAE (1 << 5)       /* external A tamper enabled */
888c2ecf20Sopenharmony_ci#define DTCR_EBE  (1 << 4)       /* external boot tamper enabled */
898c2ecf20Sopenharmony_ci#define DTCR_SAIE (1 << 3)       /* SCC enabled */
908c2ecf20Sopenharmony_ci#define DTCR_TTE  (1 << 2)       /* temperature tamper enabled */
918c2ecf20Sopenharmony_ci#define DTCR_CTE  (1 << 1)       /* clock tamper enabled */
928c2ecf20Sopenharmony_ci#define DTCR_VTE  (1 << 0)       /* voltage tamper enabled */
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci#define DGPR      0x3c           /* DryIce General Purpose Reg */
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci/**
978c2ecf20Sopenharmony_ci * struct imxdi_dev - private imxdi rtc data
988c2ecf20Sopenharmony_ci * @pdev: pointer to platform dev
998c2ecf20Sopenharmony_ci * @rtc: pointer to rtc struct
1008c2ecf20Sopenharmony_ci * @ioaddr: IO registers pointer
1018c2ecf20Sopenharmony_ci * @clk: input reference clock
1028c2ecf20Sopenharmony_ci * @dsr: copy of the DSR register
1038c2ecf20Sopenharmony_ci * @irq_lock: interrupt enable register (DIER) lock
1048c2ecf20Sopenharmony_ci * @write_wait: registers write complete queue
1058c2ecf20Sopenharmony_ci * @write_mutex: serialize registers write
1068c2ecf20Sopenharmony_ci * @work: schedule alarm work
1078c2ecf20Sopenharmony_ci */
1088c2ecf20Sopenharmony_cistruct imxdi_dev {
1098c2ecf20Sopenharmony_ci	struct platform_device *pdev;
1108c2ecf20Sopenharmony_ci	struct rtc_device *rtc;
1118c2ecf20Sopenharmony_ci	void __iomem *ioaddr;
1128c2ecf20Sopenharmony_ci	struct clk *clk;
1138c2ecf20Sopenharmony_ci	u32 dsr;
1148c2ecf20Sopenharmony_ci	spinlock_t irq_lock;
1158c2ecf20Sopenharmony_ci	wait_queue_head_t write_wait;
1168c2ecf20Sopenharmony_ci	struct mutex write_mutex;
1178c2ecf20Sopenharmony_ci	struct work_struct work;
1188c2ecf20Sopenharmony_ci};
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci/* Some background:
1218c2ecf20Sopenharmony_ci *
1228c2ecf20Sopenharmony_ci * The DryIce unit is a complex security/tamper monitor device. To be able do
1238c2ecf20Sopenharmony_ci * its job in a useful manner it runs a bigger statemachine to bring it into
1248c2ecf20Sopenharmony_ci * security/tamper failure state and once again to bring it out of this state.
1258c2ecf20Sopenharmony_ci *
1268c2ecf20Sopenharmony_ci * This unit can be in one of three states:
1278c2ecf20Sopenharmony_ci *
1288c2ecf20Sopenharmony_ci * - "NON-VALID STATE"
1298c2ecf20Sopenharmony_ci *   always after the battery power was removed
1308c2ecf20Sopenharmony_ci * - "FAILURE STATE"
1318c2ecf20Sopenharmony_ci *   if one of the enabled security events has happened
1328c2ecf20Sopenharmony_ci * - "VALID STATE"
1338c2ecf20Sopenharmony_ci *   if the unit works as expected
1348c2ecf20Sopenharmony_ci *
1358c2ecf20Sopenharmony_ci * Everything stops when the unit enters the failure state including the RTC
1368c2ecf20Sopenharmony_ci * counter (to be able to detect the time the security event happened).
1378c2ecf20Sopenharmony_ci *
1388c2ecf20Sopenharmony_ci * The following events (when enabled) let the DryIce unit enter the failure
1398c2ecf20Sopenharmony_ci * state:
1408c2ecf20Sopenharmony_ci *
1418c2ecf20Sopenharmony_ci * - wire-mesh-tamper detect
1428c2ecf20Sopenharmony_ci * - external tamper B detect
1438c2ecf20Sopenharmony_ci * - external tamper A detect
1448c2ecf20Sopenharmony_ci * - temperature tamper detect
1458c2ecf20Sopenharmony_ci * - clock tamper detect
1468c2ecf20Sopenharmony_ci * - voltage tamper detect
1478c2ecf20Sopenharmony_ci * - RTC counter overflow
1488c2ecf20Sopenharmony_ci * - monotonic counter overflow
1498c2ecf20Sopenharmony_ci * - external boot
1508c2ecf20Sopenharmony_ci *
1518c2ecf20Sopenharmony_ci * If we find the DryIce unit in "FAILURE STATE" and the TDCHL cleared, we
1528c2ecf20Sopenharmony_ci * can only detect this state. In this case the unit is completely locked and
1538c2ecf20Sopenharmony_ci * must force a second "SYSTEM POR" to bring the DryIce into the
1548c2ecf20Sopenharmony_ci * "NON-VALID STATE" + "FAILURE STATE" where a recovery is possible.
1558c2ecf20Sopenharmony_ci * If the TDCHL is set in the "FAILURE STATE" we are out of luck. In this case
1568c2ecf20Sopenharmony_ci * a battery power cycle is required.
1578c2ecf20Sopenharmony_ci *
1588c2ecf20Sopenharmony_ci * In the "NON-VALID STATE" + "FAILURE STATE" we can clear the "FAILURE STATE"
1598c2ecf20Sopenharmony_ci * and recover the DryIce unit. By clearing the "NON-VALID STATE" as the last
1608c2ecf20Sopenharmony_ci * task, we bring back this unit into life.
1618c2ecf20Sopenharmony_ci */
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci/*
1648c2ecf20Sopenharmony_ci * Do a write into the unit without interrupt support.
1658c2ecf20Sopenharmony_ci * We do not need to check the WEF here, because the only reason this kind of
1668c2ecf20Sopenharmony_ci * write error can happen is if we write to the unit twice within the 122 us
1678c2ecf20Sopenharmony_ci * interval. This cannot happen, since we are using this function only while
1688c2ecf20Sopenharmony_ci * setting up the unit.
1698c2ecf20Sopenharmony_ci */
1708c2ecf20Sopenharmony_cistatic void di_write_busy_wait(const struct imxdi_dev *imxdi, u32 val,
1718c2ecf20Sopenharmony_ci			       unsigned reg)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	/* do the register write */
1748c2ecf20Sopenharmony_ci	writel(val, imxdi->ioaddr + reg);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	/*
1778c2ecf20Sopenharmony_ci	 * now it takes four 32,768 kHz clock cycles to take
1788c2ecf20Sopenharmony_ci	 * the change into effect = 122 us
1798c2ecf20Sopenharmony_ci	 */
1808c2ecf20Sopenharmony_ci	usleep_range(130, 200);
1818c2ecf20Sopenharmony_ci}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cistatic void di_report_tamper_info(struct imxdi_dev *imxdi,  u32 dsr)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	u32 dtcr;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	dtcr = readl(imxdi->ioaddr + DTCR);
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	dev_emerg(&imxdi->pdev->dev, "DryIce tamper event detected\n");
1908c2ecf20Sopenharmony_ci	/* the following flags force a transition into the "FAILURE STATE" */
1918c2ecf20Sopenharmony_ci	if (dsr & DSR_VTD)
1928c2ecf20Sopenharmony_ci		dev_emerg(&imxdi->pdev->dev, "%sVoltage Tamper Event\n",
1938c2ecf20Sopenharmony_ci			  dtcr & DTCR_VTE ? "" : "Spurious ");
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	if (dsr & DSR_CTD)
1968c2ecf20Sopenharmony_ci		dev_emerg(&imxdi->pdev->dev, "%s32768 Hz Clock Tamper Event\n",
1978c2ecf20Sopenharmony_ci			  dtcr & DTCR_CTE ? "" : "Spurious ");
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	if (dsr & DSR_TTD)
2008c2ecf20Sopenharmony_ci		dev_emerg(&imxdi->pdev->dev, "%sTemperature Tamper Event\n",
2018c2ecf20Sopenharmony_ci			  dtcr & DTCR_TTE ? "" : "Spurious ");
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	if (dsr & DSR_SAD)
2048c2ecf20Sopenharmony_ci		dev_emerg(&imxdi->pdev->dev,
2058c2ecf20Sopenharmony_ci			  "%sSecure Controller Alarm Event\n",
2068c2ecf20Sopenharmony_ci			  dtcr & DTCR_SAIE ? "" : "Spurious ");
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	if (dsr & DSR_EBD)
2098c2ecf20Sopenharmony_ci		dev_emerg(&imxdi->pdev->dev, "%sExternal Boot Tamper Event\n",
2108c2ecf20Sopenharmony_ci			  dtcr & DTCR_EBE ? "" : "Spurious ");
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	if (dsr & DSR_ETAD)
2138c2ecf20Sopenharmony_ci		dev_emerg(&imxdi->pdev->dev, "%sExternal Tamper A Event\n",
2148c2ecf20Sopenharmony_ci			  dtcr & DTCR_ETAE ? "" : "Spurious ");
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	if (dsr & DSR_ETBD)
2178c2ecf20Sopenharmony_ci		dev_emerg(&imxdi->pdev->dev, "%sExternal Tamper B Event\n",
2188c2ecf20Sopenharmony_ci			  dtcr & DTCR_ETBE ? "" : "Spurious ");
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	if (dsr & DSR_WTD)
2218c2ecf20Sopenharmony_ci		dev_emerg(&imxdi->pdev->dev, "%sWire-mesh Tamper Event\n",
2228c2ecf20Sopenharmony_ci			  dtcr & DTCR_WTE ? "" : "Spurious ");
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	if (dsr & DSR_MCO)
2258c2ecf20Sopenharmony_ci		dev_emerg(&imxdi->pdev->dev,
2268c2ecf20Sopenharmony_ci			  "%sMonotonic-counter Overflow Event\n",
2278c2ecf20Sopenharmony_ci			  dtcr & DTCR_MOE ? "" : "Spurious ");
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	if (dsr & DSR_TCO)
2308c2ecf20Sopenharmony_ci		dev_emerg(&imxdi->pdev->dev, "%sTimer-counter Overflow Event\n",
2318c2ecf20Sopenharmony_ci			  dtcr & DTCR_TOE ? "" : "Spurious ");
2328c2ecf20Sopenharmony_ci}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cistatic void di_what_is_to_be_done(struct imxdi_dev *imxdi,
2358c2ecf20Sopenharmony_ci				  const char *power_supply)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	dev_emerg(&imxdi->pdev->dev, "Please cycle the %s power supply in order to get the DryIce/RTC unit working again\n",
2388c2ecf20Sopenharmony_ci		  power_supply);
2398c2ecf20Sopenharmony_ci}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_cistatic int di_handle_failure_state(struct imxdi_dev *imxdi, u32 dsr)
2428c2ecf20Sopenharmony_ci{
2438c2ecf20Sopenharmony_ci	u32 dcr;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	dev_dbg(&imxdi->pdev->dev, "DSR register reports: %08X\n", dsr);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	/* report the cause */
2488c2ecf20Sopenharmony_ci	di_report_tamper_info(imxdi, dsr);
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	dcr = readl(imxdi->ioaddr + DCR);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	if (dcr & DCR_FSHL) {
2538c2ecf20Sopenharmony_ci		/* we are out of luck */
2548c2ecf20Sopenharmony_ci		di_what_is_to_be_done(imxdi, "battery");
2558c2ecf20Sopenharmony_ci		return -ENODEV;
2568c2ecf20Sopenharmony_ci	}
2578c2ecf20Sopenharmony_ci	/*
2588c2ecf20Sopenharmony_ci	 * with the next SYSTEM POR we will transit from the "FAILURE STATE"
2598c2ecf20Sopenharmony_ci	 * into the "NON-VALID STATE" + "FAILURE STATE"
2608c2ecf20Sopenharmony_ci	 */
2618c2ecf20Sopenharmony_ci	di_what_is_to_be_done(imxdi, "main");
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	return -ENODEV;
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic int di_handle_valid_state(struct imxdi_dev *imxdi, u32 dsr)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	/* initialize alarm */
2698c2ecf20Sopenharmony_ci	di_write_busy_wait(imxdi, DCAMR_UNSET, DCAMR);
2708c2ecf20Sopenharmony_ci	di_write_busy_wait(imxdi, 0, DCALR);
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	/* clear alarm flag */
2738c2ecf20Sopenharmony_ci	if (dsr & DSR_CAF)
2748c2ecf20Sopenharmony_ci		di_write_busy_wait(imxdi, DSR_CAF, DSR);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	return 0;
2778c2ecf20Sopenharmony_ci}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_cistatic int di_handle_invalid_state(struct imxdi_dev *imxdi, u32 dsr)
2808c2ecf20Sopenharmony_ci{
2818c2ecf20Sopenharmony_ci	u32 dcr, sec;
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	/*
2848c2ecf20Sopenharmony_ci	 * lets disable all sources which can force the DryIce unit into
2858c2ecf20Sopenharmony_ci	 * the "FAILURE STATE" for now
2868c2ecf20Sopenharmony_ci	 */
2878c2ecf20Sopenharmony_ci	di_write_busy_wait(imxdi, 0x00000000, DTCR);
2888c2ecf20Sopenharmony_ci	/* and lets protect them at runtime from any change */
2898c2ecf20Sopenharmony_ci	di_write_busy_wait(imxdi, DCR_TDCSL, DCR);
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	sec = readl(imxdi->ioaddr + DTCMR);
2928c2ecf20Sopenharmony_ci	if (sec != 0)
2938c2ecf20Sopenharmony_ci		dev_warn(&imxdi->pdev->dev,
2948c2ecf20Sopenharmony_ci			 "The security violation has happened at %u seconds\n",
2958c2ecf20Sopenharmony_ci			 sec);
2968c2ecf20Sopenharmony_ci	/*
2978c2ecf20Sopenharmony_ci	 * the timer cannot be set/modified if
2988c2ecf20Sopenharmony_ci	 * - the TCHL or TCSL bit is set in DCR
2998c2ecf20Sopenharmony_ci	 */
3008c2ecf20Sopenharmony_ci	dcr = readl(imxdi->ioaddr + DCR);
3018c2ecf20Sopenharmony_ci	if (!(dcr & DCR_TCE)) {
3028c2ecf20Sopenharmony_ci		if (dcr & DCR_TCHL) {
3038c2ecf20Sopenharmony_ci			/* we are out of luck */
3048c2ecf20Sopenharmony_ci			di_what_is_to_be_done(imxdi, "battery");
3058c2ecf20Sopenharmony_ci			return -ENODEV;
3068c2ecf20Sopenharmony_ci		}
3078c2ecf20Sopenharmony_ci		if (dcr & DCR_TCSL) {
3088c2ecf20Sopenharmony_ci			di_what_is_to_be_done(imxdi, "main");
3098c2ecf20Sopenharmony_ci			return -ENODEV;
3108c2ecf20Sopenharmony_ci		}
3118c2ecf20Sopenharmony_ci	}
3128c2ecf20Sopenharmony_ci	/*
3138c2ecf20Sopenharmony_ci	 * - the timer counter stops/is stopped if
3148c2ecf20Sopenharmony_ci	 *   - its overflow flag is set (TCO in DSR)
3158c2ecf20Sopenharmony_ci	 *      -> clear overflow bit to make it count again
3168c2ecf20Sopenharmony_ci	 *   - NVF is set in DSR
3178c2ecf20Sopenharmony_ci	 *      -> clear non-valid bit to make it count again
3188c2ecf20Sopenharmony_ci	 *   - its TCE (DCR) is cleared
3198c2ecf20Sopenharmony_ci	 *      -> set TCE to make it count
3208c2ecf20Sopenharmony_ci	 *   - it was never set before
3218c2ecf20Sopenharmony_ci	 *      -> write a time into it (required again if the NVF was set)
3228c2ecf20Sopenharmony_ci	 */
3238c2ecf20Sopenharmony_ci	/* state handled */
3248c2ecf20Sopenharmony_ci	di_write_busy_wait(imxdi, DSR_NVF, DSR);
3258c2ecf20Sopenharmony_ci	/* clear overflow flag */
3268c2ecf20Sopenharmony_ci	di_write_busy_wait(imxdi, DSR_TCO, DSR);
3278c2ecf20Sopenharmony_ci	/* enable the counter */
3288c2ecf20Sopenharmony_ci	di_write_busy_wait(imxdi, dcr | DCR_TCE, DCR);
3298c2ecf20Sopenharmony_ci	/* set and trigger it to make it count */
3308c2ecf20Sopenharmony_ci	di_write_busy_wait(imxdi, sec, DTCMR);
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	/* now prepare for the valid state */
3338c2ecf20Sopenharmony_ci	return di_handle_valid_state(imxdi, __raw_readl(imxdi->ioaddr + DSR));
3348c2ecf20Sopenharmony_ci}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_cistatic int di_handle_invalid_and_failure_state(struct imxdi_dev *imxdi, u32 dsr)
3378c2ecf20Sopenharmony_ci{
3388c2ecf20Sopenharmony_ci	u32 dcr;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	/*
3418c2ecf20Sopenharmony_ci	 * now we must first remove the tamper sources in order to get the
3428c2ecf20Sopenharmony_ci	 * device out of the "FAILURE STATE"
3438c2ecf20Sopenharmony_ci	 * To disable any of the following sources we need to modify the DTCR
3448c2ecf20Sopenharmony_ci	 */
3458c2ecf20Sopenharmony_ci	if (dsr & (DSR_WTD | DSR_ETBD | DSR_ETAD | DSR_EBD | DSR_SAD |
3468c2ecf20Sopenharmony_ci			DSR_TTD | DSR_CTD | DSR_VTD | DSR_MCO | DSR_TCO)) {
3478c2ecf20Sopenharmony_ci		dcr = __raw_readl(imxdi->ioaddr + DCR);
3488c2ecf20Sopenharmony_ci		if (dcr & DCR_TDCHL) {
3498c2ecf20Sopenharmony_ci			/*
3508c2ecf20Sopenharmony_ci			 * the tamper register is locked. We cannot disable the
3518c2ecf20Sopenharmony_ci			 * tamper detection. The TDCHL can only be reset by a
3528c2ecf20Sopenharmony_ci			 * DRYICE POR, but we cannot force a DRYICE POR in
3538c2ecf20Sopenharmony_ci			 * software because we are still in "FAILURE STATE".
3548c2ecf20Sopenharmony_ci			 * We need a DRYICE POR via battery power cycling....
3558c2ecf20Sopenharmony_ci			 */
3568c2ecf20Sopenharmony_ci			/*
3578c2ecf20Sopenharmony_ci			 * out of luck!
3588c2ecf20Sopenharmony_ci			 * we cannot disable them without a DRYICE POR
3598c2ecf20Sopenharmony_ci			 */
3608c2ecf20Sopenharmony_ci			di_what_is_to_be_done(imxdi, "battery");
3618c2ecf20Sopenharmony_ci			return -ENODEV;
3628c2ecf20Sopenharmony_ci		}
3638c2ecf20Sopenharmony_ci		if (dcr & DCR_TDCSL) {
3648c2ecf20Sopenharmony_ci			/* a soft lock can be removed by a SYSTEM POR */
3658c2ecf20Sopenharmony_ci			di_what_is_to_be_done(imxdi, "main");
3668c2ecf20Sopenharmony_ci			return -ENODEV;
3678c2ecf20Sopenharmony_ci		}
3688c2ecf20Sopenharmony_ci	}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	/* disable all sources */
3718c2ecf20Sopenharmony_ci	di_write_busy_wait(imxdi, 0x00000000, DTCR);
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	/* clear the status bits now */
3748c2ecf20Sopenharmony_ci	di_write_busy_wait(imxdi, dsr & (DSR_WTD | DSR_ETBD | DSR_ETAD |
3758c2ecf20Sopenharmony_ci			DSR_EBD | DSR_SAD | DSR_TTD | DSR_CTD | DSR_VTD |
3768c2ecf20Sopenharmony_ci			DSR_MCO | DSR_TCO), DSR);
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	dsr = readl(imxdi->ioaddr + DSR);
3798c2ecf20Sopenharmony_ci	if ((dsr & ~(DSR_NVF | DSR_SVF | DSR_WBF | DSR_WNF |
3808c2ecf20Sopenharmony_ci			DSR_WCF | DSR_WEF)) != 0)
3818c2ecf20Sopenharmony_ci		dev_warn(&imxdi->pdev->dev,
3828c2ecf20Sopenharmony_ci			 "There are still some sources of pain in DSR: %08x!\n",
3838c2ecf20Sopenharmony_ci			 dsr & ~(DSR_NVF | DSR_SVF | DSR_WBF | DSR_WNF |
3848c2ecf20Sopenharmony_ci				 DSR_WCF | DSR_WEF));
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	/*
3878c2ecf20Sopenharmony_ci	 * now we are trying to clear the "Security-violation flag" to
3888c2ecf20Sopenharmony_ci	 * get the DryIce out of this state
3898c2ecf20Sopenharmony_ci	 */
3908c2ecf20Sopenharmony_ci	di_write_busy_wait(imxdi, DSR_SVF, DSR);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	/* success? */
3938c2ecf20Sopenharmony_ci	dsr = readl(imxdi->ioaddr + DSR);
3948c2ecf20Sopenharmony_ci	if (dsr & DSR_SVF) {
3958c2ecf20Sopenharmony_ci		dev_crit(&imxdi->pdev->dev,
3968c2ecf20Sopenharmony_ci			 "Cannot clear the security violation flag. We are ending up in an endless loop!\n");
3978c2ecf20Sopenharmony_ci		/* last resort */
3988c2ecf20Sopenharmony_ci		di_what_is_to_be_done(imxdi, "battery");
3998c2ecf20Sopenharmony_ci		return -ENODEV;
4008c2ecf20Sopenharmony_ci	}
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	/*
4038c2ecf20Sopenharmony_ci	 * now we have left the "FAILURE STATE" and ending up in the
4048c2ecf20Sopenharmony_ci	 * "NON-VALID STATE" time to recover everything
4058c2ecf20Sopenharmony_ci	 */
4068c2ecf20Sopenharmony_ci	return di_handle_invalid_state(imxdi, dsr);
4078c2ecf20Sopenharmony_ci}
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_cistatic int di_handle_state(struct imxdi_dev *imxdi)
4108c2ecf20Sopenharmony_ci{
4118c2ecf20Sopenharmony_ci	int rc;
4128c2ecf20Sopenharmony_ci	u32 dsr;
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	dsr = readl(imxdi->ioaddr + DSR);
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	switch (dsr & (DSR_NVF | DSR_SVF)) {
4178c2ecf20Sopenharmony_ci	case DSR_NVF:
4188c2ecf20Sopenharmony_ci		dev_warn(&imxdi->pdev->dev, "Invalid stated unit detected\n");
4198c2ecf20Sopenharmony_ci		rc = di_handle_invalid_state(imxdi, dsr);
4208c2ecf20Sopenharmony_ci		break;
4218c2ecf20Sopenharmony_ci	case DSR_SVF:
4228c2ecf20Sopenharmony_ci		dev_warn(&imxdi->pdev->dev, "Failure stated unit detected\n");
4238c2ecf20Sopenharmony_ci		rc = di_handle_failure_state(imxdi, dsr);
4248c2ecf20Sopenharmony_ci		break;
4258c2ecf20Sopenharmony_ci	case DSR_NVF | DSR_SVF:
4268c2ecf20Sopenharmony_ci		dev_warn(&imxdi->pdev->dev,
4278c2ecf20Sopenharmony_ci			 "Failure+Invalid stated unit detected\n");
4288c2ecf20Sopenharmony_ci		rc = di_handle_invalid_and_failure_state(imxdi, dsr);
4298c2ecf20Sopenharmony_ci		break;
4308c2ecf20Sopenharmony_ci	default:
4318c2ecf20Sopenharmony_ci		dev_notice(&imxdi->pdev->dev, "Unlocked unit detected\n");
4328c2ecf20Sopenharmony_ci		rc = di_handle_valid_state(imxdi, dsr);
4338c2ecf20Sopenharmony_ci	}
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	return rc;
4368c2ecf20Sopenharmony_ci}
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci/*
4398c2ecf20Sopenharmony_ci * enable a dryice interrupt
4408c2ecf20Sopenharmony_ci */
4418c2ecf20Sopenharmony_cistatic void di_int_enable(struct imxdi_dev *imxdi, u32 intr)
4428c2ecf20Sopenharmony_ci{
4438c2ecf20Sopenharmony_ci	unsigned long flags;
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	spin_lock_irqsave(&imxdi->irq_lock, flags);
4468c2ecf20Sopenharmony_ci	writel(readl(imxdi->ioaddr + DIER) | intr,
4478c2ecf20Sopenharmony_ci	       imxdi->ioaddr + DIER);
4488c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&imxdi->irq_lock, flags);
4498c2ecf20Sopenharmony_ci}
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci/*
4528c2ecf20Sopenharmony_ci * disable a dryice interrupt
4538c2ecf20Sopenharmony_ci */
4548c2ecf20Sopenharmony_cistatic void di_int_disable(struct imxdi_dev *imxdi, u32 intr)
4558c2ecf20Sopenharmony_ci{
4568c2ecf20Sopenharmony_ci	unsigned long flags;
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	spin_lock_irqsave(&imxdi->irq_lock, flags);
4598c2ecf20Sopenharmony_ci	writel(readl(imxdi->ioaddr + DIER) & ~intr,
4608c2ecf20Sopenharmony_ci	       imxdi->ioaddr + DIER);
4618c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&imxdi->irq_lock, flags);
4628c2ecf20Sopenharmony_ci}
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci/*
4658c2ecf20Sopenharmony_ci * This function attempts to clear the dryice write-error flag.
4668c2ecf20Sopenharmony_ci *
4678c2ecf20Sopenharmony_ci * A dryice write error is similar to a bus fault and should not occur in
4688c2ecf20Sopenharmony_ci * normal operation.  Clearing the flag requires another write, so the root
4698c2ecf20Sopenharmony_ci * cause of the problem may need to be fixed before the flag can be cleared.
4708c2ecf20Sopenharmony_ci */
4718c2ecf20Sopenharmony_cistatic void clear_write_error(struct imxdi_dev *imxdi)
4728c2ecf20Sopenharmony_ci{
4738c2ecf20Sopenharmony_ci	int cnt;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	dev_warn(&imxdi->pdev->dev, "WARNING: Register write error!\n");
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	/* clear the write error flag */
4788c2ecf20Sopenharmony_ci	writel(DSR_WEF, imxdi->ioaddr + DSR);
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	/* wait for it to take effect */
4818c2ecf20Sopenharmony_ci	for (cnt = 0; cnt < 1000; cnt++) {
4828c2ecf20Sopenharmony_ci		if ((readl(imxdi->ioaddr + DSR) & DSR_WEF) == 0)
4838c2ecf20Sopenharmony_ci			return;
4848c2ecf20Sopenharmony_ci		udelay(10);
4858c2ecf20Sopenharmony_ci	}
4868c2ecf20Sopenharmony_ci	dev_err(&imxdi->pdev->dev,
4878c2ecf20Sopenharmony_ci			"ERROR: Cannot clear write-error flag!\n");
4888c2ecf20Sopenharmony_ci}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci/*
4918c2ecf20Sopenharmony_ci * Write a dryice register and wait until it completes.
4928c2ecf20Sopenharmony_ci *
4938c2ecf20Sopenharmony_ci * This function uses interrupts to determine when the
4948c2ecf20Sopenharmony_ci * write has completed.
4958c2ecf20Sopenharmony_ci */
4968c2ecf20Sopenharmony_cistatic int di_write_wait(struct imxdi_dev *imxdi, u32 val, int reg)
4978c2ecf20Sopenharmony_ci{
4988c2ecf20Sopenharmony_ci	int ret;
4998c2ecf20Sopenharmony_ci	int rc = 0;
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	/* serialize register writes */
5028c2ecf20Sopenharmony_ci	mutex_lock(&imxdi->write_mutex);
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	/* enable the write-complete interrupt */
5058c2ecf20Sopenharmony_ci	di_int_enable(imxdi, DIER_WCIE);
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	imxdi->dsr = 0;
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	/* do the register write */
5108c2ecf20Sopenharmony_ci	writel(val, imxdi->ioaddr + reg);
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	/* wait for the write to finish */
5138c2ecf20Sopenharmony_ci	ret = wait_event_interruptible_timeout(imxdi->write_wait,
5148c2ecf20Sopenharmony_ci			imxdi->dsr & (DSR_WCF | DSR_WEF), msecs_to_jiffies(1));
5158c2ecf20Sopenharmony_ci	if (ret < 0) {
5168c2ecf20Sopenharmony_ci		rc = ret;
5178c2ecf20Sopenharmony_ci		goto out;
5188c2ecf20Sopenharmony_ci	} else if (ret == 0) {
5198c2ecf20Sopenharmony_ci		dev_warn(&imxdi->pdev->dev,
5208c2ecf20Sopenharmony_ci				"Write-wait timeout "
5218c2ecf20Sopenharmony_ci				"val = 0x%08x reg = 0x%08x\n", val, reg);
5228c2ecf20Sopenharmony_ci	}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	/* check for write error */
5258c2ecf20Sopenharmony_ci	if (imxdi->dsr & DSR_WEF) {
5268c2ecf20Sopenharmony_ci		clear_write_error(imxdi);
5278c2ecf20Sopenharmony_ci		rc = -EIO;
5288c2ecf20Sopenharmony_ci	}
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ciout:
5318c2ecf20Sopenharmony_ci	mutex_unlock(&imxdi->write_mutex);
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	return rc;
5348c2ecf20Sopenharmony_ci}
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci/*
5378c2ecf20Sopenharmony_ci * read the seconds portion of the current time from the dryice time counter
5388c2ecf20Sopenharmony_ci */
5398c2ecf20Sopenharmony_cistatic int dryice_rtc_read_time(struct device *dev, struct rtc_time *tm)
5408c2ecf20Sopenharmony_ci{
5418c2ecf20Sopenharmony_ci	struct imxdi_dev *imxdi = dev_get_drvdata(dev);
5428c2ecf20Sopenharmony_ci	unsigned long now;
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	now = readl(imxdi->ioaddr + DTCMR);
5458c2ecf20Sopenharmony_ci	rtc_time64_to_tm(now, tm);
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	return 0;
5488c2ecf20Sopenharmony_ci}
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci/*
5518c2ecf20Sopenharmony_ci * set the seconds portion of dryice time counter and clear the
5528c2ecf20Sopenharmony_ci * fractional part.
5538c2ecf20Sopenharmony_ci */
5548c2ecf20Sopenharmony_cistatic int dryice_rtc_set_time(struct device *dev, struct rtc_time *tm)
5558c2ecf20Sopenharmony_ci{
5568c2ecf20Sopenharmony_ci	struct imxdi_dev *imxdi = dev_get_drvdata(dev);
5578c2ecf20Sopenharmony_ci	u32 dcr, dsr;
5588c2ecf20Sopenharmony_ci	int rc;
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	dcr = readl(imxdi->ioaddr + DCR);
5618c2ecf20Sopenharmony_ci	dsr = readl(imxdi->ioaddr + DSR);
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	if (!(dcr & DCR_TCE) || (dsr & DSR_SVF)) {
5648c2ecf20Sopenharmony_ci		if (dcr & DCR_TCHL) {
5658c2ecf20Sopenharmony_ci			/* we are even more out of luck */
5668c2ecf20Sopenharmony_ci			di_what_is_to_be_done(imxdi, "battery");
5678c2ecf20Sopenharmony_ci			return -EPERM;
5688c2ecf20Sopenharmony_ci		}
5698c2ecf20Sopenharmony_ci		if ((dcr & DCR_TCSL) || (dsr & DSR_SVF)) {
5708c2ecf20Sopenharmony_ci			/* we are out of luck for now */
5718c2ecf20Sopenharmony_ci			di_what_is_to_be_done(imxdi, "main");
5728c2ecf20Sopenharmony_ci			return -EPERM;
5738c2ecf20Sopenharmony_ci		}
5748c2ecf20Sopenharmony_ci	}
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	/* zero the fractional part first */
5778c2ecf20Sopenharmony_ci	rc = di_write_wait(imxdi, 0, DTCLR);
5788c2ecf20Sopenharmony_ci	if (rc != 0)
5798c2ecf20Sopenharmony_ci		return rc;
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	rc = di_write_wait(imxdi, rtc_tm_to_time64(tm), DTCMR);
5828c2ecf20Sopenharmony_ci	if (rc != 0)
5838c2ecf20Sopenharmony_ci		return rc;
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	return di_write_wait(imxdi, readl(imxdi->ioaddr + DCR) | DCR_TCE, DCR);
5868c2ecf20Sopenharmony_ci}
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_cistatic int dryice_rtc_alarm_irq_enable(struct device *dev,
5898c2ecf20Sopenharmony_ci		unsigned int enabled)
5908c2ecf20Sopenharmony_ci{
5918c2ecf20Sopenharmony_ci	struct imxdi_dev *imxdi = dev_get_drvdata(dev);
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	if (enabled)
5948c2ecf20Sopenharmony_ci		di_int_enable(imxdi, DIER_CAIE);
5958c2ecf20Sopenharmony_ci	else
5968c2ecf20Sopenharmony_ci		di_int_disable(imxdi, DIER_CAIE);
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	return 0;
5998c2ecf20Sopenharmony_ci}
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci/*
6028c2ecf20Sopenharmony_ci * read the seconds portion of the alarm register.
6038c2ecf20Sopenharmony_ci * the fractional part of the alarm register is always zero.
6048c2ecf20Sopenharmony_ci */
6058c2ecf20Sopenharmony_cistatic int dryice_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
6068c2ecf20Sopenharmony_ci{
6078c2ecf20Sopenharmony_ci	struct imxdi_dev *imxdi = dev_get_drvdata(dev);
6088c2ecf20Sopenharmony_ci	u32 dcamr;
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	dcamr = readl(imxdi->ioaddr + DCAMR);
6118c2ecf20Sopenharmony_ci	rtc_time64_to_tm(dcamr, &alarm->time);
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	/* alarm is enabled if the interrupt is enabled */
6148c2ecf20Sopenharmony_ci	alarm->enabled = (readl(imxdi->ioaddr + DIER) & DIER_CAIE) != 0;
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	/* don't allow the DSR read to mess up DSR_WCF */
6178c2ecf20Sopenharmony_ci	mutex_lock(&imxdi->write_mutex);
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci	/* alarm is pending if the alarm flag is set */
6208c2ecf20Sopenharmony_ci	alarm->pending = (readl(imxdi->ioaddr + DSR) & DSR_CAF) != 0;
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	mutex_unlock(&imxdi->write_mutex);
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	return 0;
6258c2ecf20Sopenharmony_ci}
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci/*
6288c2ecf20Sopenharmony_ci * set the seconds portion of dryice alarm register
6298c2ecf20Sopenharmony_ci */
6308c2ecf20Sopenharmony_cistatic int dryice_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
6318c2ecf20Sopenharmony_ci{
6328c2ecf20Sopenharmony_ci	struct imxdi_dev *imxdi = dev_get_drvdata(dev);
6338c2ecf20Sopenharmony_ci	int rc;
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	/* write the new alarm time */
6368c2ecf20Sopenharmony_ci	rc = di_write_wait(imxdi, rtc_tm_to_time64(&alarm->time), DCAMR);
6378c2ecf20Sopenharmony_ci	if (rc)
6388c2ecf20Sopenharmony_ci		return rc;
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	if (alarm->enabled)
6418c2ecf20Sopenharmony_ci		di_int_enable(imxdi, DIER_CAIE);  /* enable alarm intr */
6428c2ecf20Sopenharmony_ci	else
6438c2ecf20Sopenharmony_ci		di_int_disable(imxdi, DIER_CAIE); /* disable alarm intr */
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	return 0;
6468c2ecf20Sopenharmony_ci}
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_cistatic const struct rtc_class_ops dryice_rtc_ops = {
6498c2ecf20Sopenharmony_ci	.read_time		= dryice_rtc_read_time,
6508c2ecf20Sopenharmony_ci	.set_time		= dryice_rtc_set_time,
6518c2ecf20Sopenharmony_ci	.alarm_irq_enable	= dryice_rtc_alarm_irq_enable,
6528c2ecf20Sopenharmony_ci	.read_alarm		= dryice_rtc_read_alarm,
6538c2ecf20Sopenharmony_ci	.set_alarm		= dryice_rtc_set_alarm,
6548c2ecf20Sopenharmony_ci};
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci/*
6578c2ecf20Sopenharmony_ci * interrupt handler for dryice "normal" and security violation interrupt
6588c2ecf20Sopenharmony_ci */
6598c2ecf20Sopenharmony_cistatic irqreturn_t dryice_irq(int irq, void *dev_id)
6608c2ecf20Sopenharmony_ci{
6618c2ecf20Sopenharmony_ci	struct imxdi_dev *imxdi = dev_id;
6628c2ecf20Sopenharmony_ci	u32 dsr, dier;
6638c2ecf20Sopenharmony_ci	irqreturn_t rc = IRQ_NONE;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	dier = readl(imxdi->ioaddr + DIER);
6668c2ecf20Sopenharmony_ci	dsr = readl(imxdi->ioaddr + DSR);
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	/* handle the security violation event */
6698c2ecf20Sopenharmony_ci	if (dier & DIER_SVIE) {
6708c2ecf20Sopenharmony_ci		if (dsr & DSR_SVF) {
6718c2ecf20Sopenharmony_ci			/*
6728c2ecf20Sopenharmony_ci			 * Disable the interrupt when this kind of event has
6738c2ecf20Sopenharmony_ci			 * happened.
6748c2ecf20Sopenharmony_ci			 * There cannot be more than one event of this type,
6758c2ecf20Sopenharmony_ci			 * because it needs a complex state change
6768c2ecf20Sopenharmony_ci			 * including a main power cycle to get again out of
6778c2ecf20Sopenharmony_ci			 * this state.
6788c2ecf20Sopenharmony_ci			 */
6798c2ecf20Sopenharmony_ci			di_int_disable(imxdi, DIER_SVIE);
6808c2ecf20Sopenharmony_ci			/* report the violation */
6818c2ecf20Sopenharmony_ci			di_report_tamper_info(imxdi, dsr);
6828c2ecf20Sopenharmony_ci			rc = IRQ_HANDLED;
6838c2ecf20Sopenharmony_ci		}
6848c2ecf20Sopenharmony_ci	}
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	/* handle write complete and write error cases */
6878c2ecf20Sopenharmony_ci	if (dier & DIER_WCIE) {
6888c2ecf20Sopenharmony_ci		/*If the write wait queue is empty then there is no pending
6898c2ecf20Sopenharmony_ci		  operations. It means the interrupt is for DryIce -Security.
6908c2ecf20Sopenharmony_ci		  IRQ must be returned as none.*/
6918c2ecf20Sopenharmony_ci		if (list_empty_careful(&imxdi->write_wait.head))
6928c2ecf20Sopenharmony_ci			return rc;
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci		/* DSR_WCF clears itself on DSR read */
6958c2ecf20Sopenharmony_ci		if (dsr & (DSR_WCF | DSR_WEF)) {
6968c2ecf20Sopenharmony_ci			/* mask the interrupt */
6978c2ecf20Sopenharmony_ci			di_int_disable(imxdi, DIER_WCIE);
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci			/* save the dsr value for the wait queue */
7008c2ecf20Sopenharmony_ci			imxdi->dsr |= dsr;
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci			wake_up_interruptible(&imxdi->write_wait);
7038c2ecf20Sopenharmony_ci			rc = IRQ_HANDLED;
7048c2ecf20Sopenharmony_ci		}
7058c2ecf20Sopenharmony_ci	}
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	/* handle the alarm case */
7088c2ecf20Sopenharmony_ci	if (dier & DIER_CAIE) {
7098c2ecf20Sopenharmony_ci		/* DSR_WCF clears itself on DSR read */
7108c2ecf20Sopenharmony_ci		if (dsr & DSR_CAF) {
7118c2ecf20Sopenharmony_ci			/* mask the interrupt */
7128c2ecf20Sopenharmony_ci			di_int_disable(imxdi, DIER_CAIE);
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci			/* finish alarm in user context */
7158c2ecf20Sopenharmony_ci			schedule_work(&imxdi->work);
7168c2ecf20Sopenharmony_ci			rc = IRQ_HANDLED;
7178c2ecf20Sopenharmony_ci		}
7188c2ecf20Sopenharmony_ci	}
7198c2ecf20Sopenharmony_ci	return rc;
7208c2ecf20Sopenharmony_ci}
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci/*
7238c2ecf20Sopenharmony_ci * post the alarm event from user context so it can sleep
7248c2ecf20Sopenharmony_ci * on the write completion.
7258c2ecf20Sopenharmony_ci */
7268c2ecf20Sopenharmony_cistatic void dryice_work(struct work_struct *work)
7278c2ecf20Sopenharmony_ci{
7288c2ecf20Sopenharmony_ci	struct imxdi_dev *imxdi = container_of(work,
7298c2ecf20Sopenharmony_ci			struct imxdi_dev, work);
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	/* dismiss the interrupt (ignore error) */
7328c2ecf20Sopenharmony_ci	di_write_wait(imxdi, DSR_CAF, DSR);
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	/* pass the alarm event to the rtc framework. */
7358c2ecf20Sopenharmony_ci	rtc_update_irq(imxdi->rtc, 1, RTC_AF | RTC_IRQF);
7368c2ecf20Sopenharmony_ci}
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci/*
7398c2ecf20Sopenharmony_ci * probe for dryice rtc device
7408c2ecf20Sopenharmony_ci */
7418c2ecf20Sopenharmony_cistatic int __init dryice_rtc_probe(struct platform_device *pdev)
7428c2ecf20Sopenharmony_ci{
7438c2ecf20Sopenharmony_ci	struct imxdi_dev *imxdi;
7448c2ecf20Sopenharmony_ci	int norm_irq, sec_irq;
7458c2ecf20Sopenharmony_ci	int rc;
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	imxdi = devm_kzalloc(&pdev->dev, sizeof(*imxdi), GFP_KERNEL);
7488c2ecf20Sopenharmony_ci	if (!imxdi)
7498c2ecf20Sopenharmony_ci		return -ENOMEM;
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	imxdi->pdev = pdev;
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci	imxdi->ioaddr = devm_platform_ioremap_resource(pdev, 0);
7548c2ecf20Sopenharmony_ci	if (IS_ERR(imxdi->ioaddr))
7558c2ecf20Sopenharmony_ci		return PTR_ERR(imxdi->ioaddr);
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	spin_lock_init(&imxdi->irq_lock);
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci	norm_irq = platform_get_irq(pdev, 0);
7608c2ecf20Sopenharmony_ci	if (norm_irq < 0)
7618c2ecf20Sopenharmony_ci		return norm_irq;
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci	/* the 2nd irq is the security violation irq
7648c2ecf20Sopenharmony_ci	 * make this optional, don't break the device tree ABI
7658c2ecf20Sopenharmony_ci	 */
7668c2ecf20Sopenharmony_ci	sec_irq = platform_get_irq(pdev, 1);
7678c2ecf20Sopenharmony_ci	if (sec_irq <= 0)
7688c2ecf20Sopenharmony_ci		sec_irq = IRQ_NOTCONNECTED;
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci	init_waitqueue_head(&imxdi->write_wait);
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci	INIT_WORK(&imxdi->work, dryice_work);
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	mutex_init(&imxdi->write_mutex);
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	imxdi->rtc = devm_rtc_allocate_device(&pdev->dev);
7778c2ecf20Sopenharmony_ci	if (IS_ERR(imxdi->rtc))
7788c2ecf20Sopenharmony_ci		return PTR_ERR(imxdi->rtc);
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	imxdi->clk = devm_clk_get(&pdev->dev, NULL);
7818c2ecf20Sopenharmony_ci	if (IS_ERR(imxdi->clk))
7828c2ecf20Sopenharmony_ci		return PTR_ERR(imxdi->clk);
7838c2ecf20Sopenharmony_ci	rc = clk_prepare_enable(imxdi->clk);
7848c2ecf20Sopenharmony_ci	if (rc)
7858c2ecf20Sopenharmony_ci		return rc;
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci	/*
7888c2ecf20Sopenharmony_ci	 * Initialize dryice hardware
7898c2ecf20Sopenharmony_ci	 */
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	/* mask all interrupts */
7928c2ecf20Sopenharmony_ci	writel(0, imxdi->ioaddr + DIER);
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	rc = di_handle_state(imxdi);
7958c2ecf20Sopenharmony_ci	if (rc != 0)
7968c2ecf20Sopenharmony_ci		goto err;
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	rc = devm_request_irq(&pdev->dev, norm_irq, dryice_irq,
7998c2ecf20Sopenharmony_ci			      IRQF_SHARED, pdev->name, imxdi);
8008c2ecf20Sopenharmony_ci	if (rc) {
8018c2ecf20Sopenharmony_ci		dev_warn(&pdev->dev, "interrupt not available.\n");
8028c2ecf20Sopenharmony_ci		goto err;
8038c2ecf20Sopenharmony_ci	}
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci	rc = devm_request_irq(&pdev->dev, sec_irq, dryice_irq,
8068c2ecf20Sopenharmony_ci			      IRQF_SHARED, pdev->name, imxdi);
8078c2ecf20Sopenharmony_ci	if (rc) {
8088c2ecf20Sopenharmony_ci		dev_warn(&pdev->dev, "security violation interrupt not available.\n");
8098c2ecf20Sopenharmony_ci		/* this is not an error, see above */
8108c2ecf20Sopenharmony_ci	}
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, imxdi);
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	imxdi->rtc->ops = &dryice_rtc_ops;
8158c2ecf20Sopenharmony_ci	imxdi->rtc->range_max = U32_MAX;
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci	rc = rtc_register_device(imxdi->rtc);
8188c2ecf20Sopenharmony_ci	if (rc)
8198c2ecf20Sopenharmony_ci		goto err;
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	return 0;
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_cierr:
8248c2ecf20Sopenharmony_ci	clk_disable_unprepare(imxdi->clk);
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci	return rc;
8278c2ecf20Sopenharmony_ci}
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_cistatic int __exit dryice_rtc_remove(struct platform_device *pdev)
8308c2ecf20Sopenharmony_ci{
8318c2ecf20Sopenharmony_ci	struct imxdi_dev *imxdi = platform_get_drvdata(pdev);
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	flush_work(&imxdi->work);
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	/* mask all interrupts */
8368c2ecf20Sopenharmony_ci	writel(0, imxdi->ioaddr + DIER);
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	clk_disable_unprepare(imxdi->clk);
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci	return 0;
8418c2ecf20Sopenharmony_ci}
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci#ifdef CONFIG_OF
8448c2ecf20Sopenharmony_cistatic const struct of_device_id dryice_dt_ids[] = {
8458c2ecf20Sopenharmony_ci	{ .compatible = "fsl,imx25-rtc" },
8468c2ecf20Sopenharmony_ci	{ /* sentinel */ }
8478c2ecf20Sopenharmony_ci};
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, dryice_dt_ids);
8508c2ecf20Sopenharmony_ci#endif
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_cistatic struct platform_driver dryice_rtc_driver = {
8538c2ecf20Sopenharmony_ci	.driver = {
8548c2ecf20Sopenharmony_ci		   .name = "imxdi_rtc",
8558c2ecf20Sopenharmony_ci		   .of_match_table = of_match_ptr(dryice_dt_ids),
8568c2ecf20Sopenharmony_ci		   },
8578c2ecf20Sopenharmony_ci	.remove = __exit_p(dryice_rtc_remove),
8588c2ecf20Sopenharmony_ci};
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_cimodule_platform_driver_probe(dryice_rtc_driver, dryice_rtc_probe);
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ciMODULE_AUTHOR("Freescale Semiconductor, Inc.");
8638c2ecf20Sopenharmony_ciMODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
8648c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("IMX DryIce Realtime Clock Driver (RTC)");
8658c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
866