1*d4488377SClaudiu Beznea // SPDX-License-Identifier: GPL-2.0 2*d4488377SClaudiu Beznea /* 3*d4488377SClaudiu Beznea * On-Chip RTC Support available on RZ/G3S SoC 4*d4488377SClaudiu Beznea * 5*d4488377SClaudiu Beznea * Copyright (C) 2024 Renesas Electronics Corp. 6*d4488377SClaudiu Beznea */ 7*d4488377SClaudiu Beznea #include <linux/bcd.h> 8*d4488377SClaudiu Beznea #include <linux/cleanup.h> 9*d4488377SClaudiu Beznea #include <linux/clk.h> 10*d4488377SClaudiu Beznea #include <linux/completion.h> 11*d4488377SClaudiu Beznea #include <linux/delay.h> 12*d4488377SClaudiu Beznea #include <linux/iopoll.h> 13*d4488377SClaudiu Beznea #include <linux/interrupt.h> 14*d4488377SClaudiu Beznea #include <linux/jiffies.h> 15*d4488377SClaudiu Beznea #include <linux/of.h> 16*d4488377SClaudiu Beznea #include <linux/platform_device.h> 17*d4488377SClaudiu Beznea #include <linux/pm_runtime.h> 18*d4488377SClaudiu Beznea #include <linux/reset.h> 19*d4488377SClaudiu Beznea #include <linux/rtc.h> 20*d4488377SClaudiu Beznea 21*d4488377SClaudiu Beznea /* Counter registers. */ 22*d4488377SClaudiu Beznea #define RTCA3_RSECCNT 0x2 23*d4488377SClaudiu Beznea #define RTCA3_RSECCNT_SEC GENMASK(6, 0) 24*d4488377SClaudiu Beznea #define RTCA3_RMINCNT 0x4 25*d4488377SClaudiu Beznea #define RTCA3_RMINCNT_MIN GENMASK(6, 0) 26*d4488377SClaudiu Beznea #define RTCA3_RHRCNT 0x6 27*d4488377SClaudiu Beznea #define RTCA3_RHRCNT_HR GENMASK(5, 0) 28*d4488377SClaudiu Beznea #define RTCA3_RHRCNT_PM BIT(6) 29*d4488377SClaudiu Beznea #define RTCA3_RWKCNT 0x8 30*d4488377SClaudiu Beznea #define RTCA3_RWKCNT_WK GENMASK(2, 0) 31*d4488377SClaudiu Beznea #define RTCA3_RDAYCNT 0xa 32*d4488377SClaudiu Beznea #define RTCA3_RDAYCNT_DAY GENMASK(5, 0) 33*d4488377SClaudiu Beznea #define RTCA3_RMONCNT 0xc 34*d4488377SClaudiu Beznea #define RTCA3_RMONCNT_MONTH GENMASK(4, 0) 35*d4488377SClaudiu Beznea #define RTCA3_RYRCNT 0xe 36*d4488377SClaudiu Beznea #define RTCA3_RYRCNT_YEAR GENMASK(7, 0) 37*d4488377SClaudiu Beznea 38*d4488377SClaudiu Beznea /* Alarm registers. */ 39*d4488377SClaudiu Beznea #define RTCA3_RSECAR 0x10 40*d4488377SClaudiu Beznea #define RTCA3_RSECAR_SEC GENMASK(6, 0) 41*d4488377SClaudiu Beznea #define RTCA3_RMINAR 0x12 42*d4488377SClaudiu Beznea #define RTCA3_RMINAR_MIN GENMASK(6, 0) 43*d4488377SClaudiu Beznea #define RTCA3_RHRAR 0x14 44*d4488377SClaudiu Beznea #define RTCA3_RHRAR_HR GENMASK(5, 0) 45*d4488377SClaudiu Beznea #define RTCA3_RHRAR_PM BIT(6) 46*d4488377SClaudiu Beznea #define RTCA3_RWKAR 0x16 47*d4488377SClaudiu Beznea #define RTCA3_RWKAR_DAYW GENMASK(2, 0) 48*d4488377SClaudiu Beznea #define RTCA3_RDAYAR 0x18 49*d4488377SClaudiu Beznea #define RTCA3_RDAYAR_DATE GENMASK(5, 0) 50*d4488377SClaudiu Beznea #define RTCA3_RMONAR 0x1a 51*d4488377SClaudiu Beznea #define RTCA3_RMONAR_MON GENMASK(4, 0) 52*d4488377SClaudiu Beznea #define RTCA3_RYRAR 0x1c 53*d4488377SClaudiu Beznea #define RTCA3_RYRAR_YR GENMASK(7, 0) 54*d4488377SClaudiu Beznea #define RTCA3_RYRAREN 0x1e 55*d4488377SClaudiu Beznea 56*d4488377SClaudiu Beznea /* Alarm enable bit (for all alarm registers). */ 57*d4488377SClaudiu Beznea #define RTCA3_AR_ENB BIT(7) 58*d4488377SClaudiu Beznea 59*d4488377SClaudiu Beznea /* Control registers. */ 60*d4488377SClaudiu Beznea #define RTCA3_RCR1 0x22 61*d4488377SClaudiu Beznea #define RTCA3_RCR1_AIE BIT(0) 62*d4488377SClaudiu Beznea #define RTCA3_RCR1_CIE BIT(1) 63*d4488377SClaudiu Beznea #define RTCA3_RCR1_PIE BIT(2) 64*d4488377SClaudiu Beznea #define RTCA3_RCR1_PES GENMASK(7, 4) 65*d4488377SClaudiu Beznea #define RTCA3_RCR1_PES_1_64_SEC 0x8 66*d4488377SClaudiu Beznea #define RTCA3_RCR2 0x24 67*d4488377SClaudiu Beznea #define RTCA3_RCR2_START BIT(0) 68*d4488377SClaudiu Beznea #define RTCA3_RCR2_RESET BIT(1) 69*d4488377SClaudiu Beznea #define RTCA3_RCR2_AADJE BIT(4) 70*d4488377SClaudiu Beznea #define RTCA3_RCR2_ADJP BIT(5) 71*d4488377SClaudiu Beznea #define RTCA3_RCR2_HR24 BIT(6) 72*d4488377SClaudiu Beznea #define RTCA3_RCR2_CNTMD BIT(7) 73*d4488377SClaudiu Beznea #define RTCA3_RSR 0x20 74*d4488377SClaudiu Beznea #define RTCA3_RSR_AF BIT(0) 75*d4488377SClaudiu Beznea #define RTCA3_RSR_CF BIT(1) 76*d4488377SClaudiu Beznea #define RTCA3_RSR_PF BIT(2) 77*d4488377SClaudiu Beznea #define RTCA3_RADJ 0x2e 78*d4488377SClaudiu Beznea #define RTCA3_RADJ_ADJ GENMASK(5, 0) 79*d4488377SClaudiu Beznea #define RTCA3_RADJ_ADJ_MAX 0x3f 80*d4488377SClaudiu Beznea #define RTCA3_RADJ_PMADJ GENMASK(7, 6) 81*d4488377SClaudiu Beznea #define RTCA3_RADJ_PMADJ_NONE 0 82*d4488377SClaudiu Beznea #define RTCA3_RADJ_PMADJ_ADD 1 83*d4488377SClaudiu Beznea #define RTCA3_RADJ_PMADJ_SUB 2 84*d4488377SClaudiu Beznea 85*d4488377SClaudiu Beznea /* Polling operation timeouts. */ 86*d4488377SClaudiu Beznea #define RTCA3_DEFAULT_TIMEOUT_US 150 87*d4488377SClaudiu Beznea #define RTCA3_IRQSET_TIMEOUT_US 5000 88*d4488377SClaudiu Beznea #define RTCA3_START_TIMEOUT_US 150000 89*d4488377SClaudiu Beznea #define RTCA3_RESET_TIMEOUT_US 200000 90*d4488377SClaudiu Beznea 91*d4488377SClaudiu Beznea /** 92*d4488377SClaudiu Beznea * enum rtca3_alrm_set_step - RTCA3 alarm set steps 93*d4488377SClaudiu Beznea * @RTCA3_ALRM_SSTEP_DONE: alarm setup done step 94*d4488377SClaudiu Beznea * @RTCA3_ALRM_SSTEP_IRQ: two 1/64 periodic IRQs were generated step 95*d4488377SClaudiu Beznea * @RTCA3_ALRM_SSTEP_INIT: alarm setup initialization step 96*d4488377SClaudiu Beznea */ 97*d4488377SClaudiu Beznea enum rtca3_alrm_set_step { 98*d4488377SClaudiu Beznea RTCA3_ALRM_SSTEP_DONE = 0, 99*d4488377SClaudiu Beznea RTCA3_ALRM_SSTEP_IRQ = 1, 100*d4488377SClaudiu Beznea RTCA3_ALRM_SSTEP_INIT = 3, 101*d4488377SClaudiu Beznea }; 102*d4488377SClaudiu Beznea 103*d4488377SClaudiu Beznea /** 104*d4488377SClaudiu Beznea * struct rtca3_ppb_per_cycle - PPB per cycle 105*d4488377SClaudiu Beznea * @ten_sec: PPB per cycle in 10 seconds adjutment mode 106*d4488377SClaudiu Beznea * @sixty_sec: PPB per cycle in 60 seconds adjustment mode 107*d4488377SClaudiu Beznea */ 108*d4488377SClaudiu Beznea struct rtca3_ppb_per_cycle { 109*d4488377SClaudiu Beznea int ten_sec; 110*d4488377SClaudiu Beznea int sixty_sec; 111*d4488377SClaudiu Beznea }; 112*d4488377SClaudiu Beznea 113*d4488377SClaudiu Beznea /** 114*d4488377SClaudiu Beznea * struct rtca3_priv - RTCA3 private data structure 115*d4488377SClaudiu Beznea * @base: base address 116*d4488377SClaudiu Beznea * @rtc_dev: RTC device 117*d4488377SClaudiu Beznea * @rstc: reset control 118*d4488377SClaudiu Beznea * @set_alarm_completion: alarm setup completion 119*d4488377SClaudiu Beznea * @alrm_sstep: alarm setup step (see enum rtca3_alrm_set_step) 120*d4488377SClaudiu Beznea * @lock: device lock 121*d4488377SClaudiu Beznea * @ppb: ppb per cycle for each the available adjustment modes 122*d4488377SClaudiu Beznea * @wakeup_irq: wakeup IRQ 123*d4488377SClaudiu Beznea */ 124*d4488377SClaudiu Beznea struct rtca3_priv { 125*d4488377SClaudiu Beznea void __iomem *base; 126*d4488377SClaudiu Beznea struct rtc_device *rtc_dev; 127*d4488377SClaudiu Beznea struct reset_control *rstc; 128*d4488377SClaudiu Beznea struct completion set_alarm_completion; 129*d4488377SClaudiu Beznea atomic_t alrm_sstep; 130*d4488377SClaudiu Beznea spinlock_t lock; 131*d4488377SClaudiu Beznea struct rtca3_ppb_per_cycle ppb; 132*d4488377SClaudiu Beznea int wakeup_irq; 133*d4488377SClaudiu Beznea }; 134*d4488377SClaudiu Beznea 135*d4488377SClaudiu Beznea static void rtca3_byte_update_bits(struct rtca3_priv *priv, u8 off, u8 mask, u8 val) 136*d4488377SClaudiu Beznea { 137*d4488377SClaudiu Beznea u8 tmp; 138*d4488377SClaudiu Beznea 139*d4488377SClaudiu Beznea tmp = readb(priv->base + off); 140*d4488377SClaudiu Beznea tmp &= ~mask; 141*d4488377SClaudiu Beznea tmp |= (val & mask); 142*d4488377SClaudiu Beznea writeb(tmp, priv->base + off); 143*d4488377SClaudiu Beznea } 144*d4488377SClaudiu Beznea 145*d4488377SClaudiu Beznea static u8 rtca3_alarm_handler_helper(struct rtca3_priv *priv) 146*d4488377SClaudiu Beznea { 147*d4488377SClaudiu Beznea u8 val, pending; 148*d4488377SClaudiu Beznea 149*d4488377SClaudiu Beznea val = readb(priv->base + RTCA3_RSR); 150*d4488377SClaudiu Beznea pending = val & RTCA3_RSR_AF; 151*d4488377SClaudiu Beznea writeb(val & ~pending, priv->base + RTCA3_RSR); 152*d4488377SClaudiu Beznea 153*d4488377SClaudiu Beznea if (pending) 154*d4488377SClaudiu Beznea rtc_update_irq(priv->rtc_dev, 1, RTC_AF | RTC_IRQF); 155*d4488377SClaudiu Beznea 156*d4488377SClaudiu Beznea return pending; 157*d4488377SClaudiu Beznea } 158*d4488377SClaudiu Beznea 159*d4488377SClaudiu Beznea static irqreturn_t rtca3_alarm_handler(int irq, void *dev_id) 160*d4488377SClaudiu Beznea { 161*d4488377SClaudiu Beznea struct rtca3_priv *priv = dev_id; 162*d4488377SClaudiu Beznea u8 pending; 163*d4488377SClaudiu Beznea 164*d4488377SClaudiu Beznea guard(spinlock)(&priv->lock); 165*d4488377SClaudiu Beznea 166*d4488377SClaudiu Beznea pending = rtca3_alarm_handler_helper(priv); 167*d4488377SClaudiu Beznea 168*d4488377SClaudiu Beznea return IRQ_RETVAL(pending); 169*d4488377SClaudiu Beznea } 170*d4488377SClaudiu Beznea 171*d4488377SClaudiu Beznea static irqreturn_t rtca3_periodic_handler(int irq, void *dev_id) 172*d4488377SClaudiu Beznea { 173*d4488377SClaudiu Beznea struct rtca3_priv *priv = dev_id; 174*d4488377SClaudiu Beznea u8 val, pending; 175*d4488377SClaudiu Beznea 176*d4488377SClaudiu Beznea guard(spinlock)(&priv->lock); 177*d4488377SClaudiu Beznea 178*d4488377SClaudiu Beznea val = readb(priv->base + RTCA3_RSR); 179*d4488377SClaudiu Beznea pending = val & RTCA3_RSR_PF; 180*d4488377SClaudiu Beznea 181*d4488377SClaudiu Beznea if (pending) { 182*d4488377SClaudiu Beznea writeb(val & ~pending, priv->base + RTCA3_RSR); 183*d4488377SClaudiu Beznea 184*d4488377SClaudiu Beznea if (atomic_read(&priv->alrm_sstep) > RTCA3_ALRM_SSTEP_IRQ) { 185*d4488377SClaudiu Beznea /* Alarm setup in progress. */ 186*d4488377SClaudiu Beznea atomic_dec(&priv->alrm_sstep); 187*d4488377SClaudiu Beznea 188*d4488377SClaudiu Beznea if (atomic_read(&priv->alrm_sstep) == RTCA3_ALRM_SSTEP_IRQ) { 189*d4488377SClaudiu Beznea /* 190*d4488377SClaudiu Beznea * We got 2 * 1/64 periodic interrupts. Disable 191*d4488377SClaudiu Beznea * interrupt and let alarm setup continue. 192*d4488377SClaudiu Beznea */ 193*d4488377SClaudiu Beznea rtca3_byte_update_bits(priv, RTCA3_RCR1, 194*d4488377SClaudiu Beznea RTCA3_RCR1_PIE, 0); 195*d4488377SClaudiu Beznea readb_poll_timeout_atomic(priv->base + RTCA3_RCR1, val, 196*d4488377SClaudiu Beznea !(val & RTCA3_RCR1_PIE), 197*d4488377SClaudiu Beznea 10, RTCA3_DEFAULT_TIMEOUT_US); 198*d4488377SClaudiu Beznea complete(&priv->set_alarm_completion); 199*d4488377SClaudiu Beznea } 200*d4488377SClaudiu Beznea } 201*d4488377SClaudiu Beznea } 202*d4488377SClaudiu Beznea 203*d4488377SClaudiu Beznea return IRQ_RETVAL(pending); 204*d4488377SClaudiu Beznea } 205*d4488377SClaudiu Beznea 206*d4488377SClaudiu Beznea static void rtca3_prepare_cntalrm_regs_for_read(struct rtca3_priv *priv, bool cnt) 207*d4488377SClaudiu Beznea { 208*d4488377SClaudiu Beznea /* Offset b/w time and alarm registers. */ 209*d4488377SClaudiu Beznea u8 offset = cnt ? 0 : 0xe; 210*d4488377SClaudiu Beznea 211*d4488377SClaudiu Beznea /* 212*d4488377SClaudiu Beznea * According to HW manual (section 22.6.4. Notes on writing to and 213*d4488377SClaudiu Beznea * reading from registers) after writing to count registers, alarm 214*d4488377SClaudiu Beznea * registers, year alarm enable register, bits RCR2.AADJE, AADJP, 215*d4488377SClaudiu Beznea * and HR24 register, we need to do 3 empty reads before being 216*d4488377SClaudiu Beznea * able to fetch the registers content. 217*d4488377SClaudiu Beznea */ 218*d4488377SClaudiu Beznea for (u8 i = 0; i < 3; i++) { 219*d4488377SClaudiu Beznea readb(priv->base + RTCA3_RSECCNT + offset); 220*d4488377SClaudiu Beznea readb(priv->base + RTCA3_RMINCNT + offset); 221*d4488377SClaudiu Beznea readb(priv->base + RTCA3_RHRCNT + offset); 222*d4488377SClaudiu Beznea readb(priv->base + RTCA3_RWKCNT + offset); 223*d4488377SClaudiu Beznea readb(priv->base + RTCA3_RDAYCNT + offset); 224*d4488377SClaudiu Beznea readw(priv->base + RTCA3_RYRCNT + offset); 225*d4488377SClaudiu Beznea if (!cnt) 226*d4488377SClaudiu Beznea readb(priv->base + RTCA3_RYRAREN); 227*d4488377SClaudiu Beznea } 228*d4488377SClaudiu Beznea } 229*d4488377SClaudiu Beznea 230*d4488377SClaudiu Beznea static int rtca3_read_time(struct device *dev, struct rtc_time *tm) 231*d4488377SClaudiu Beznea { 232*d4488377SClaudiu Beznea struct rtca3_priv *priv = dev_get_drvdata(dev); 233*d4488377SClaudiu Beznea u8 sec, min, hour, wday, mday, month, tmp; 234*d4488377SClaudiu Beznea u8 trials = 0; 235*d4488377SClaudiu Beznea u32 year100; 236*d4488377SClaudiu Beznea u16 year; 237*d4488377SClaudiu Beznea 238*d4488377SClaudiu Beznea guard(spinlock_irqsave)(&priv->lock); 239*d4488377SClaudiu Beznea 240*d4488377SClaudiu Beznea tmp = readb(priv->base + RTCA3_RCR2); 241*d4488377SClaudiu Beznea if (!(tmp & RTCA3_RCR2_START)) 242*d4488377SClaudiu Beznea return -EINVAL; 243*d4488377SClaudiu Beznea 244*d4488377SClaudiu Beznea do { 245*d4488377SClaudiu Beznea /* Clear carry interrupt. */ 246*d4488377SClaudiu Beznea rtca3_byte_update_bits(priv, RTCA3_RSR, RTCA3_RSR_CF, 0); 247*d4488377SClaudiu Beznea 248*d4488377SClaudiu Beznea /* Read counters. */ 249*d4488377SClaudiu Beznea sec = readb(priv->base + RTCA3_RSECCNT); 250*d4488377SClaudiu Beznea min = readb(priv->base + RTCA3_RMINCNT); 251*d4488377SClaudiu Beznea hour = readb(priv->base + RTCA3_RHRCNT); 252*d4488377SClaudiu Beznea wday = readb(priv->base + RTCA3_RWKCNT); 253*d4488377SClaudiu Beznea mday = readb(priv->base + RTCA3_RDAYCNT); 254*d4488377SClaudiu Beznea month = readb(priv->base + RTCA3_RMONCNT); 255*d4488377SClaudiu Beznea year = readw(priv->base + RTCA3_RYRCNT); 256*d4488377SClaudiu Beznea 257*d4488377SClaudiu Beznea tmp = readb(priv->base + RTCA3_RSR); 258*d4488377SClaudiu Beznea 259*d4488377SClaudiu Beznea /* 260*d4488377SClaudiu Beznea * We cannot generate carries due to reading 64Hz counter as 261*d4488377SClaudiu Beznea * the driver doesn't implement carry, thus, carries will be 262*d4488377SClaudiu Beznea * generated once per seconds. Add a timeout of 5 trials here 263*d4488377SClaudiu Beznea * to avoid infinite loop, if any. 264*d4488377SClaudiu Beznea */ 265*d4488377SClaudiu Beznea } while ((tmp & RTCA3_RSR_CF) && ++trials < 5); 266*d4488377SClaudiu Beznea 267*d4488377SClaudiu Beznea if (trials >= 5) 268*d4488377SClaudiu Beznea return -ETIMEDOUT; 269*d4488377SClaudiu Beznea 270*d4488377SClaudiu Beznea tm->tm_sec = bcd2bin(FIELD_GET(RTCA3_RSECCNT_SEC, sec)); 271*d4488377SClaudiu Beznea tm->tm_min = bcd2bin(FIELD_GET(RTCA3_RMINCNT_MIN, min)); 272*d4488377SClaudiu Beznea tm->tm_hour = bcd2bin(FIELD_GET(RTCA3_RHRCNT_HR, hour)); 273*d4488377SClaudiu Beznea tm->tm_wday = bcd2bin(FIELD_GET(RTCA3_RWKCNT_WK, wday)); 274*d4488377SClaudiu Beznea tm->tm_mday = bcd2bin(FIELD_GET(RTCA3_RDAYCNT_DAY, mday)); 275*d4488377SClaudiu Beznea tm->tm_mon = bcd2bin(FIELD_GET(RTCA3_RMONCNT_MONTH, month)) - 1; 276*d4488377SClaudiu Beznea year = FIELD_GET(RTCA3_RYRCNT_YEAR, year); 277*d4488377SClaudiu Beznea year100 = bcd2bin((year == 0x99) ? 0x19 : 0x20); 278*d4488377SClaudiu Beznea tm->tm_year = (year100 * 100 + bcd2bin(year)) - 1900; 279*d4488377SClaudiu Beznea 280*d4488377SClaudiu Beznea return 0; 281*d4488377SClaudiu Beznea } 282*d4488377SClaudiu Beznea 283*d4488377SClaudiu Beznea static int rtca3_set_time(struct device *dev, struct rtc_time *tm) 284*d4488377SClaudiu Beznea { 285*d4488377SClaudiu Beznea struct rtca3_priv *priv = dev_get_drvdata(dev); 286*d4488377SClaudiu Beznea u8 rcr2, tmp; 287*d4488377SClaudiu Beznea int ret; 288*d4488377SClaudiu Beznea 289*d4488377SClaudiu Beznea guard(spinlock_irqsave)(&priv->lock); 290*d4488377SClaudiu Beznea 291*d4488377SClaudiu Beznea /* Stop the RTC. */ 292*d4488377SClaudiu Beznea rcr2 = readb(priv->base + RTCA3_RCR2); 293*d4488377SClaudiu Beznea writeb(rcr2 & ~RTCA3_RCR2_START, priv->base + RTCA3_RCR2); 294*d4488377SClaudiu Beznea ret = readb_poll_timeout_atomic(priv->base + RTCA3_RCR2, tmp, 295*d4488377SClaudiu Beznea !(tmp & RTCA3_RCR2_START), 296*d4488377SClaudiu Beznea 10, RTCA3_DEFAULT_TIMEOUT_US); 297*d4488377SClaudiu Beznea if (ret) 298*d4488377SClaudiu Beznea return ret; 299*d4488377SClaudiu Beznea 300*d4488377SClaudiu Beznea /* Update time. */ 301*d4488377SClaudiu Beznea writeb(bin2bcd(tm->tm_sec), priv->base + RTCA3_RSECCNT); 302*d4488377SClaudiu Beznea writeb(bin2bcd(tm->tm_min), priv->base + RTCA3_RMINCNT); 303*d4488377SClaudiu Beznea writeb(bin2bcd(tm->tm_hour), priv->base + RTCA3_RHRCNT); 304*d4488377SClaudiu Beznea writeb(bin2bcd(tm->tm_wday), priv->base + RTCA3_RWKCNT); 305*d4488377SClaudiu Beznea writeb(bin2bcd(tm->tm_mday), priv->base + RTCA3_RDAYCNT); 306*d4488377SClaudiu Beznea writeb(bin2bcd(tm->tm_mon + 1), priv->base + RTCA3_RMONCNT); 307*d4488377SClaudiu Beznea writew(bin2bcd(tm->tm_year % 100), priv->base + RTCA3_RYRCNT); 308*d4488377SClaudiu Beznea 309*d4488377SClaudiu Beznea /* Make sure we can read back the counters. */ 310*d4488377SClaudiu Beznea rtca3_prepare_cntalrm_regs_for_read(priv, true); 311*d4488377SClaudiu Beznea 312*d4488377SClaudiu Beznea /* Start RTC. */ 313*d4488377SClaudiu Beznea writeb(rcr2 | RTCA3_RCR2_START, priv->base + RTCA3_RCR2); 314*d4488377SClaudiu Beznea return readb_poll_timeout_atomic(priv->base + RTCA3_RCR2, tmp, 315*d4488377SClaudiu Beznea (tmp & RTCA3_RCR2_START), 316*d4488377SClaudiu Beznea 10, RTCA3_DEFAULT_TIMEOUT_US); 317*d4488377SClaudiu Beznea } 318*d4488377SClaudiu Beznea 319*d4488377SClaudiu Beznea static int rtca3_alarm_irq_set_helper(struct rtca3_priv *priv, 320*d4488377SClaudiu Beznea u8 interrupts, 321*d4488377SClaudiu Beznea unsigned int enabled) 322*d4488377SClaudiu Beznea { 323*d4488377SClaudiu Beznea u8 tmp, val; 324*d4488377SClaudiu Beznea 325*d4488377SClaudiu Beznea if (enabled) { 326*d4488377SClaudiu Beznea /* 327*d4488377SClaudiu Beznea * AIE, CIE, PIE bit indexes in RSR corresponds with 328*d4488377SClaudiu Beznea * those on RCR1. Same interrupts mask can be used. 329*d4488377SClaudiu Beznea */ 330*d4488377SClaudiu Beznea rtca3_byte_update_bits(priv, RTCA3_RSR, interrupts, 0); 331*d4488377SClaudiu Beznea val = interrupts; 332*d4488377SClaudiu Beznea } else { 333*d4488377SClaudiu Beznea val = 0; 334*d4488377SClaudiu Beznea } 335*d4488377SClaudiu Beznea 336*d4488377SClaudiu Beznea rtca3_byte_update_bits(priv, RTCA3_RCR1, interrupts, val); 337*d4488377SClaudiu Beznea return readb_poll_timeout_atomic(priv->base + RTCA3_RCR1, tmp, 338*d4488377SClaudiu Beznea ((tmp & interrupts) == val), 339*d4488377SClaudiu Beznea 10, RTCA3_IRQSET_TIMEOUT_US); 340*d4488377SClaudiu Beznea } 341*d4488377SClaudiu Beznea 342*d4488377SClaudiu Beznea static int rtca3_alarm_irq_enable(struct device *dev, unsigned int enabled) 343*d4488377SClaudiu Beznea { 344*d4488377SClaudiu Beznea struct rtca3_priv *priv = dev_get_drvdata(dev); 345*d4488377SClaudiu Beznea 346*d4488377SClaudiu Beznea guard(spinlock_irqsave)(&priv->lock); 347*d4488377SClaudiu Beznea 348*d4488377SClaudiu Beznea return rtca3_alarm_irq_set_helper(priv, RTCA3_RCR1_AIE, enabled); 349*d4488377SClaudiu Beznea } 350*d4488377SClaudiu Beznea 351*d4488377SClaudiu Beznea static int rtca3_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) 352*d4488377SClaudiu Beznea { 353*d4488377SClaudiu Beznea struct rtca3_priv *priv = dev_get_drvdata(dev); 354*d4488377SClaudiu Beznea u8 sec, min, hour, wday, mday, month; 355*d4488377SClaudiu Beznea struct rtc_time *tm = &wkalrm->time; 356*d4488377SClaudiu Beznea u32 year100; 357*d4488377SClaudiu Beznea u16 year; 358*d4488377SClaudiu Beznea 359*d4488377SClaudiu Beznea guard(spinlock_irqsave)(&priv->lock); 360*d4488377SClaudiu Beznea 361*d4488377SClaudiu Beznea sec = readb(priv->base + RTCA3_RSECAR); 362*d4488377SClaudiu Beznea min = readb(priv->base + RTCA3_RMINAR); 363*d4488377SClaudiu Beznea hour = readb(priv->base + RTCA3_RHRAR); 364*d4488377SClaudiu Beznea wday = readb(priv->base + RTCA3_RWKAR); 365*d4488377SClaudiu Beznea mday = readb(priv->base + RTCA3_RDAYAR); 366*d4488377SClaudiu Beznea month = readb(priv->base + RTCA3_RMONAR); 367*d4488377SClaudiu Beznea year = readw(priv->base + RTCA3_RYRAR); 368*d4488377SClaudiu Beznea 369*d4488377SClaudiu Beznea tm->tm_sec = bcd2bin(FIELD_GET(RTCA3_RSECAR_SEC, sec)); 370*d4488377SClaudiu Beznea tm->tm_min = bcd2bin(FIELD_GET(RTCA3_RMINAR_MIN, min)); 371*d4488377SClaudiu Beznea tm->tm_hour = bcd2bin(FIELD_GET(RTCA3_RHRAR_HR, hour)); 372*d4488377SClaudiu Beznea tm->tm_wday = bcd2bin(FIELD_GET(RTCA3_RWKAR_DAYW, wday)); 373*d4488377SClaudiu Beznea tm->tm_mday = bcd2bin(FIELD_GET(RTCA3_RDAYAR_DATE, mday)); 374*d4488377SClaudiu Beznea tm->tm_mon = bcd2bin(FIELD_GET(RTCA3_RMONAR_MON, month)) - 1; 375*d4488377SClaudiu Beznea year = FIELD_GET(RTCA3_RYRAR_YR, year); 376*d4488377SClaudiu Beznea year100 = bcd2bin((year == 0x99) ? 0x19 : 0x20); 377*d4488377SClaudiu Beznea tm->tm_year = (year100 * 100 + bcd2bin(year)) - 1900; 378*d4488377SClaudiu Beznea 379*d4488377SClaudiu Beznea wkalrm->enabled = !!(readb(priv->base + RTCA3_RCR1) & RTCA3_RCR1_AIE); 380*d4488377SClaudiu Beznea 381*d4488377SClaudiu Beznea return 0; 382*d4488377SClaudiu Beznea } 383*d4488377SClaudiu Beznea 384*d4488377SClaudiu Beznea static int rtca3_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) 385*d4488377SClaudiu Beznea { 386*d4488377SClaudiu Beznea struct rtca3_priv *priv = dev_get_drvdata(dev); 387*d4488377SClaudiu Beznea struct rtc_time *tm = &wkalrm->time; 388*d4488377SClaudiu Beznea u8 rcr1, tmp; 389*d4488377SClaudiu Beznea int ret; 390*d4488377SClaudiu Beznea 391*d4488377SClaudiu Beznea scoped_guard(spinlock_irqsave, &priv->lock) { 392*d4488377SClaudiu Beznea tmp = readb(priv->base + RTCA3_RCR2); 393*d4488377SClaudiu Beznea if (!(tmp & RTCA3_RCR2_START)) 394*d4488377SClaudiu Beznea return -EPERM; 395*d4488377SClaudiu Beznea 396*d4488377SClaudiu Beznea /* Disable AIE to prevent false interrupts. */ 397*d4488377SClaudiu Beznea rcr1 = readb(priv->base + RTCA3_RCR1); 398*d4488377SClaudiu Beznea rcr1 &= ~RTCA3_RCR1_AIE; 399*d4488377SClaudiu Beznea writeb(rcr1, priv->base + RTCA3_RCR1); 400*d4488377SClaudiu Beznea ret = readb_poll_timeout_atomic(priv->base + RTCA3_RCR1, tmp, 401*d4488377SClaudiu Beznea !(tmp & RTCA3_RCR1_AIE), 402*d4488377SClaudiu Beznea 10, RTCA3_DEFAULT_TIMEOUT_US); 403*d4488377SClaudiu Beznea if (ret) 404*d4488377SClaudiu Beznea return ret; 405*d4488377SClaudiu Beznea 406*d4488377SClaudiu Beznea /* Set the time and enable the alarm. */ 407*d4488377SClaudiu Beznea writeb(RTCA3_AR_ENB | bin2bcd(tm->tm_sec), priv->base + RTCA3_RSECAR); 408*d4488377SClaudiu Beznea writeb(RTCA3_AR_ENB | bin2bcd(tm->tm_min), priv->base + RTCA3_RMINAR); 409*d4488377SClaudiu Beznea writeb(RTCA3_AR_ENB | bin2bcd(tm->tm_hour), priv->base + RTCA3_RHRAR); 410*d4488377SClaudiu Beznea writeb(RTCA3_AR_ENB | bin2bcd(tm->tm_wday), priv->base + RTCA3_RWKAR); 411*d4488377SClaudiu Beznea writeb(RTCA3_AR_ENB | bin2bcd(tm->tm_mday), priv->base + RTCA3_RDAYAR); 412*d4488377SClaudiu Beznea writeb(RTCA3_AR_ENB | bin2bcd(tm->tm_mon + 1), priv->base + RTCA3_RMONAR); 413*d4488377SClaudiu Beznea 414*d4488377SClaudiu Beznea writew(bin2bcd(tm->tm_year % 100), priv->base + RTCA3_RYRAR); 415*d4488377SClaudiu Beznea writeb(RTCA3_AR_ENB, priv->base + RTCA3_RYRAREN); 416*d4488377SClaudiu Beznea 417*d4488377SClaudiu Beznea /* Make sure we can read back the counters. */ 418*d4488377SClaudiu Beznea rtca3_prepare_cntalrm_regs_for_read(priv, false); 419*d4488377SClaudiu Beznea 420*d4488377SClaudiu Beznea /* Need to wait for 2 * 1/64 periodic interrupts to be generated. */ 421*d4488377SClaudiu Beznea atomic_set(&priv->alrm_sstep, RTCA3_ALRM_SSTEP_INIT); 422*d4488377SClaudiu Beznea reinit_completion(&priv->set_alarm_completion); 423*d4488377SClaudiu Beznea 424*d4488377SClaudiu Beznea /* Enable periodic interrupt. */ 425*d4488377SClaudiu Beznea rcr1 |= RTCA3_RCR1_PIE; 426*d4488377SClaudiu Beznea writeb(rcr1, priv->base + RTCA3_RCR1); 427*d4488377SClaudiu Beznea ret = readb_poll_timeout_atomic(priv->base + RTCA3_RCR1, tmp, 428*d4488377SClaudiu Beznea (tmp & RTCA3_RCR1_PIE), 429*d4488377SClaudiu Beznea 10, RTCA3_IRQSET_TIMEOUT_US); 430*d4488377SClaudiu Beznea } 431*d4488377SClaudiu Beznea 432*d4488377SClaudiu Beznea if (ret) 433*d4488377SClaudiu Beznea goto setup_failed; 434*d4488377SClaudiu Beznea 435*d4488377SClaudiu Beznea /* Wait for the 2 * 1/64 periodic interrupts. */ 436*d4488377SClaudiu Beznea ret = wait_for_completion_interruptible_timeout(&priv->set_alarm_completion, 437*d4488377SClaudiu Beznea msecs_to_jiffies(500)); 438*d4488377SClaudiu Beznea if (ret <= 0) { 439*d4488377SClaudiu Beznea ret = -ETIMEDOUT; 440*d4488377SClaudiu Beznea goto setup_failed; 441*d4488377SClaudiu Beznea } 442*d4488377SClaudiu Beznea 443*d4488377SClaudiu Beznea scoped_guard(spinlock_irqsave, &priv->lock) { 444*d4488377SClaudiu Beznea ret = rtca3_alarm_irq_set_helper(priv, RTCA3_RCR1_AIE, wkalrm->enabled); 445*d4488377SClaudiu Beznea atomic_set(&priv->alrm_sstep, RTCA3_ALRM_SSTEP_DONE); 446*d4488377SClaudiu Beznea } 447*d4488377SClaudiu Beznea 448*d4488377SClaudiu Beznea return ret; 449*d4488377SClaudiu Beznea 450*d4488377SClaudiu Beznea setup_failed: 451*d4488377SClaudiu Beznea scoped_guard(spinlock_irqsave, &priv->lock) { 452*d4488377SClaudiu Beznea /* 453*d4488377SClaudiu Beznea * Disable PIE to avoid interrupt storm in case HW needed more than 454*d4488377SClaudiu Beznea * specified timeout for setup. 455*d4488377SClaudiu Beznea */ 456*d4488377SClaudiu Beznea writeb(rcr1 & ~RTCA3_RCR1_PIE, priv->base + RTCA3_RCR1); 457*d4488377SClaudiu Beznea readb_poll_timeout_atomic(priv->base + RTCA3_RCR1, tmp, !(tmp & ~RTCA3_RCR1_PIE), 458*d4488377SClaudiu Beznea 10, RTCA3_DEFAULT_TIMEOUT_US); 459*d4488377SClaudiu Beznea atomic_set(&priv->alrm_sstep, RTCA3_ALRM_SSTEP_DONE); 460*d4488377SClaudiu Beznea } 461*d4488377SClaudiu Beznea 462*d4488377SClaudiu Beznea return ret; 463*d4488377SClaudiu Beznea } 464*d4488377SClaudiu Beznea 465*d4488377SClaudiu Beznea static int rtca3_read_offset(struct device *dev, long *offset) 466*d4488377SClaudiu Beznea { 467*d4488377SClaudiu Beznea struct rtca3_priv *priv = dev_get_drvdata(dev); 468*d4488377SClaudiu Beznea u8 val, radj, cycles; 469*d4488377SClaudiu Beznea u32 ppb_per_cycle; 470*d4488377SClaudiu Beznea 471*d4488377SClaudiu Beznea scoped_guard(spinlock_irqsave, &priv->lock) { 472*d4488377SClaudiu Beznea radj = readb(priv->base + RTCA3_RADJ); 473*d4488377SClaudiu Beznea val = readb(priv->base + RTCA3_RCR2); 474*d4488377SClaudiu Beznea } 475*d4488377SClaudiu Beznea 476*d4488377SClaudiu Beznea cycles = FIELD_GET(RTCA3_RADJ_ADJ, radj); 477*d4488377SClaudiu Beznea 478*d4488377SClaudiu Beznea if (!cycles) { 479*d4488377SClaudiu Beznea *offset = 0; 480*d4488377SClaudiu Beznea return 0; 481*d4488377SClaudiu Beznea } 482*d4488377SClaudiu Beznea 483*d4488377SClaudiu Beznea if (val & RTCA3_RCR2_ADJP) 484*d4488377SClaudiu Beznea ppb_per_cycle = priv->ppb.ten_sec; 485*d4488377SClaudiu Beznea else 486*d4488377SClaudiu Beznea ppb_per_cycle = priv->ppb.sixty_sec; 487*d4488377SClaudiu Beznea 488*d4488377SClaudiu Beznea *offset = cycles * ppb_per_cycle; 489*d4488377SClaudiu Beznea val = FIELD_GET(RTCA3_RADJ_PMADJ, radj); 490*d4488377SClaudiu Beznea if (val == RTCA3_RADJ_PMADJ_SUB) 491*d4488377SClaudiu Beznea *offset = -(*offset); 492*d4488377SClaudiu Beznea 493*d4488377SClaudiu Beznea return 0; 494*d4488377SClaudiu Beznea } 495*d4488377SClaudiu Beznea 496*d4488377SClaudiu Beznea static int rtca3_set_offset(struct device *dev, long offset) 497*d4488377SClaudiu Beznea { 498*d4488377SClaudiu Beznea struct rtca3_priv *priv = dev_get_drvdata(dev); 499*d4488377SClaudiu Beznea int cycles, cycles10, cycles60; 500*d4488377SClaudiu Beznea u8 radj, adjp, tmp; 501*d4488377SClaudiu Beznea int ret; 502*d4488377SClaudiu Beznea 503*d4488377SClaudiu Beznea /* 504*d4488377SClaudiu Beznea * Automatic time error adjustment could be set at intervals of 10 505*d4488377SClaudiu Beznea * or 60 seconds. 506*d4488377SClaudiu Beznea */ 507*d4488377SClaudiu Beznea cycles10 = DIV_ROUND_CLOSEST(offset, priv->ppb.ten_sec); 508*d4488377SClaudiu Beznea cycles60 = DIV_ROUND_CLOSEST(offset, priv->ppb.sixty_sec); 509*d4488377SClaudiu Beznea 510*d4488377SClaudiu Beznea /* We can set b/w 1 and 63 clock cycles. */ 511*d4488377SClaudiu Beznea if (cycles60 >= -RTCA3_RADJ_ADJ_MAX && 512*d4488377SClaudiu Beznea cycles60 <= RTCA3_RADJ_ADJ_MAX) { 513*d4488377SClaudiu Beznea cycles = cycles60; 514*d4488377SClaudiu Beznea adjp = 0; 515*d4488377SClaudiu Beznea } else if (cycles10 >= -RTCA3_RADJ_ADJ_MAX && 516*d4488377SClaudiu Beznea cycles10 <= RTCA3_RADJ_ADJ_MAX) { 517*d4488377SClaudiu Beznea cycles = cycles10; 518*d4488377SClaudiu Beznea adjp = RTCA3_RCR2_ADJP; 519*d4488377SClaudiu Beznea } else { 520*d4488377SClaudiu Beznea return -ERANGE; 521*d4488377SClaudiu Beznea } 522*d4488377SClaudiu Beznea 523*d4488377SClaudiu Beznea radj = FIELD_PREP(RTCA3_RADJ_ADJ, abs(cycles)); 524*d4488377SClaudiu Beznea if (!cycles) 525*d4488377SClaudiu Beznea radj |= FIELD_PREP(RTCA3_RADJ_PMADJ, RTCA3_RADJ_PMADJ_NONE); 526*d4488377SClaudiu Beznea else if (cycles > 0) 527*d4488377SClaudiu Beznea radj |= FIELD_PREP(RTCA3_RADJ_PMADJ, RTCA3_RADJ_PMADJ_ADD); 528*d4488377SClaudiu Beznea else 529*d4488377SClaudiu Beznea radj |= FIELD_PREP(RTCA3_RADJ_PMADJ, RTCA3_RADJ_PMADJ_SUB); 530*d4488377SClaudiu Beznea 531*d4488377SClaudiu Beznea guard(spinlock_irqsave)(&priv->lock); 532*d4488377SClaudiu Beznea 533*d4488377SClaudiu Beznea tmp = readb(priv->base + RTCA3_RCR2); 534*d4488377SClaudiu Beznea 535*d4488377SClaudiu Beznea if ((tmp & RTCA3_RCR2_ADJP) != adjp) { 536*d4488377SClaudiu Beznea /* RADJ.PMADJ need to be set to zero before setting RCR2.ADJP. */ 537*d4488377SClaudiu Beznea writeb(0, priv->base + RTCA3_RADJ); 538*d4488377SClaudiu Beznea ret = readb_poll_timeout_atomic(priv->base + RTCA3_RADJ, tmp, !tmp, 539*d4488377SClaudiu Beznea 10, RTCA3_DEFAULT_TIMEOUT_US); 540*d4488377SClaudiu Beznea if (ret) 541*d4488377SClaudiu Beznea return ret; 542*d4488377SClaudiu Beznea 543*d4488377SClaudiu Beznea rtca3_byte_update_bits(priv, RTCA3_RCR2, RTCA3_RCR2_ADJP, adjp); 544*d4488377SClaudiu Beznea ret = readb_poll_timeout_atomic(priv->base + RTCA3_RCR2, tmp, 545*d4488377SClaudiu Beznea ((tmp & RTCA3_RCR2_ADJP) == adjp), 546*d4488377SClaudiu Beznea 10, RTCA3_DEFAULT_TIMEOUT_US); 547*d4488377SClaudiu Beznea if (ret) 548*d4488377SClaudiu Beznea return ret; 549*d4488377SClaudiu Beznea } 550*d4488377SClaudiu Beznea 551*d4488377SClaudiu Beznea writeb(radj, priv->base + RTCA3_RADJ); 552*d4488377SClaudiu Beznea return readb_poll_timeout_atomic(priv->base + RTCA3_RADJ, tmp, (tmp == radj), 553*d4488377SClaudiu Beznea 10, RTCA3_DEFAULT_TIMEOUT_US); 554*d4488377SClaudiu Beznea } 555*d4488377SClaudiu Beznea 556*d4488377SClaudiu Beznea static const struct rtc_class_ops rtca3_ops = { 557*d4488377SClaudiu Beznea .read_time = rtca3_read_time, 558*d4488377SClaudiu Beznea .set_time = rtca3_set_time, 559*d4488377SClaudiu Beznea .read_alarm = rtca3_read_alarm, 560*d4488377SClaudiu Beznea .set_alarm = rtca3_set_alarm, 561*d4488377SClaudiu Beznea .alarm_irq_enable = rtca3_alarm_irq_enable, 562*d4488377SClaudiu Beznea .set_offset = rtca3_set_offset, 563*d4488377SClaudiu Beznea .read_offset = rtca3_read_offset, 564*d4488377SClaudiu Beznea }; 565*d4488377SClaudiu Beznea 566*d4488377SClaudiu Beznea static int rtca3_initial_setup(struct clk *clk, struct rtca3_priv *priv) 567*d4488377SClaudiu Beznea { 568*d4488377SClaudiu Beznea unsigned long osc32k_rate; 569*d4488377SClaudiu Beznea u8 val, tmp, mask; 570*d4488377SClaudiu Beznea u32 sleep_us; 571*d4488377SClaudiu Beznea int ret; 572*d4488377SClaudiu Beznea 573*d4488377SClaudiu Beznea osc32k_rate = clk_get_rate(clk); 574*d4488377SClaudiu Beznea if (!osc32k_rate) 575*d4488377SClaudiu Beznea return -EINVAL; 576*d4488377SClaudiu Beznea 577*d4488377SClaudiu Beznea sleep_us = DIV_ROUND_UP_ULL(1000000ULL, osc32k_rate) * 6; 578*d4488377SClaudiu Beznea 579*d4488377SClaudiu Beznea priv->ppb.ten_sec = DIV_ROUND_CLOSEST_ULL(1000000000ULL, (osc32k_rate * 10)); 580*d4488377SClaudiu Beznea priv->ppb.sixty_sec = DIV_ROUND_CLOSEST_ULL(1000000000ULL, (osc32k_rate * 60)); 581*d4488377SClaudiu Beznea 582*d4488377SClaudiu Beznea /* 583*d4488377SClaudiu Beznea * According to HW manual (section 22.4.2. Clock and count mode setting procedure) 584*d4488377SClaudiu Beznea * we need to wait at least 6 cycles of the 32KHz clock after clock was enabled. 585*d4488377SClaudiu Beznea */ 586*d4488377SClaudiu Beznea usleep_range(sleep_us, sleep_us + 10); 587*d4488377SClaudiu Beznea 588*d4488377SClaudiu Beznea /* Disable all interrupts. */ 589*d4488377SClaudiu Beznea mask = RTCA3_RCR1_AIE | RTCA3_RCR1_CIE | RTCA3_RCR1_PIE; 590*d4488377SClaudiu Beznea ret = rtca3_alarm_irq_set_helper(priv, mask, 0); 591*d4488377SClaudiu Beznea if (ret) 592*d4488377SClaudiu Beznea return ret; 593*d4488377SClaudiu Beznea 594*d4488377SClaudiu Beznea mask = RTCA3_RCR2_START | RTCA3_RCR2_HR24; 595*d4488377SClaudiu Beznea val = readb(priv->base + RTCA3_RCR2); 596*d4488377SClaudiu Beznea /* Nothing to do if already started in 24 hours and calendar count mode. */ 597*d4488377SClaudiu Beznea if ((val & mask) == mask) 598*d4488377SClaudiu Beznea return 0; 599*d4488377SClaudiu Beznea 600*d4488377SClaudiu Beznea /* Reconfigure the RTC in 24 hours and calendar count mode. */ 601*d4488377SClaudiu Beznea mask = RTCA3_RCR2_START | RTCA3_RCR2_CNTMD; 602*d4488377SClaudiu Beznea writeb(0, priv->base + RTCA3_RCR2); 603*d4488377SClaudiu Beznea ret = readb_poll_timeout(priv->base + RTCA3_RCR2, tmp, !(tmp & mask), 604*d4488377SClaudiu Beznea 10, RTCA3_DEFAULT_TIMEOUT_US); 605*d4488377SClaudiu Beznea if (ret) 606*d4488377SClaudiu Beznea return ret; 607*d4488377SClaudiu Beznea 608*d4488377SClaudiu Beznea /* 609*d4488377SClaudiu Beznea * Set 24 hours mode. According to HW manual (section 22.3.19. RTC Control 610*d4488377SClaudiu Beznea * Register 2) this needs to be done separate from stop operation. 611*d4488377SClaudiu Beznea */ 612*d4488377SClaudiu Beznea mask = RTCA3_RCR2_HR24; 613*d4488377SClaudiu Beznea val = RTCA3_RCR2_HR24; 614*d4488377SClaudiu Beznea writeb(val, priv->base + RTCA3_RCR2); 615*d4488377SClaudiu Beznea ret = readb_poll_timeout(priv->base + RTCA3_RCR2, tmp, (tmp & mask), 616*d4488377SClaudiu Beznea 10, RTCA3_DEFAULT_TIMEOUT_US); 617*d4488377SClaudiu Beznea if (ret) 618*d4488377SClaudiu Beznea return ret; 619*d4488377SClaudiu Beznea 620*d4488377SClaudiu Beznea /* Execute reset. */ 621*d4488377SClaudiu Beznea mask = RTCA3_RCR2_RESET; 622*d4488377SClaudiu Beznea writeb(val | RTCA3_RCR2_RESET, priv->base + RTCA3_RCR2); 623*d4488377SClaudiu Beznea ret = readb_poll_timeout(priv->base + RTCA3_RCR2, tmp, !(tmp & mask), 624*d4488377SClaudiu Beznea 10, RTCA3_RESET_TIMEOUT_US); 625*d4488377SClaudiu Beznea if (ret) 626*d4488377SClaudiu Beznea return ret; 627*d4488377SClaudiu Beznea 628*d4488377SClaudiu Beznea /* 629*d4488377SClaudiu Beznea * According to HW manual (section 22.6.3. Notes on writing to and reading 630*d4488377SClaudiu Beznea * from registers) after reset we need to wait 6 clock cycles before 631*d4488377SClaudiu Beznea * writing to RTC registers. 632*d4488377SClaudiu Beznea */ 633*d4488377SClaudiu Beznea usleep_range(sleep_us, sleep_us + 10); 634*d4488377SClaudiu Beznea 635*d4488377SClaudiu Beznea /* Set no adjustment. */ 636*d4488377SClaudiu Beznea writeb(0, priv->base + RTCA3_RADJ); 637*d4488377SClaudiu Beznea ret = readb_poll_timeout(priv->base + RTCA3_RADJ, tmp, !tmp, 10, 638*d4488377SClaudiu Beznea RTCA3_DEFAULT_TIMEOUT_US); 639*d4488377SClaudiu Beznea 640*d4488377SClaudiu Beznea /* Start the RTC and enable automatic time error adjustment. */ 641*d4488377SClaudiu Beznea mask = RTCA3_RCR2_START | RTCA3_RCR2_AADJE; 642*d4488377SClaudiu Beznea val |= RTCA3_RCR2_START | RTCA3_RCR2_AADJE; 643*d4488377SClaudiu Beznea writeb(val, priv->base + RTCA3_RCR2); 644*d4488377SClaudiu Beznea ret = readb_poll_timeout(priv->base + RTCA3_RCR2, tmp, ((tmp & mask) == mask), 645*d4488377SClaudiu Beznea 10, RTCA3_START_TIMEOUT_US); 646*d4488377SClaudiu Beznea if (ret) 647*d4488377SClaudiu Beznea return ret; 648*d4488377SClaudiu Beznea 649*d4488377SClaudiu Beznea /* 650*d4488377SClaudiu Beznea * According to HW manual (section 22.6.4. Notes on writing to and reading 651*d4488377SClaudiu Beznea * from registers) we need to wait 1/128 seconds while the clock is operating 652*d4488377SClaudiu Beznea * (RCR2.START bit = 1) to be able to read the counters after a return from 653*d4488377SClaudiu Beznea * reset. 654*d4488377SClaudiu Beznea */ 655*d4488377SClaudiu Beznea usleep_range(8000, 9000); 656*d4488377SClaudiu Beznea 657*d4488377SClaudiu Beznea /* Set period interrupt to 1/64 seconds. It is necessary for alarm setup. */ 658*d4488377SClaudiu Beznea val = FIELD_PREP(RTCA3_RCR1_PES, RTCA3_RCR1_PES_1_64_SEC); 659*d4488377SClaudiu Beznea rtca3_byte_update_bits(priv, RTCA3_RCR1, RTCA3_RCR1_PES, val); 660*d4488377SClaudiu Beznea return readb_poll_timeout(priv->base + RTCA3_RCR1, tmp, ((tmp & RTCA3_RCR1_PES) == val), 661*d4488377SClaudiu Beznea 10, RTCA3_DEFAULT_TIMEOUT_US); 662*d4488377SClaudiu Beznea } 663*d4488377SClaudiu Beznea 664*d4488377SClaudiu Beznea static int rtca3_request_irqs(struct platform_device *pdev, struct rtca3_priv *priv) 665*d4488377SClaudiu Beznea { 666*d4488377SClaudiu Beznea struct device *dev = &pdev->dev; 667*d4488377SClaudiu Beznea int ret, irq; 668*d4488377SClaudiu Beznea 669*d4488377SClaudiu Beznea irq = platform_get_irq_byname(pdev, "alarm"); 670*d4488377SClaudiu Beznea if (irq < 0) 671*d4488377SClaudiu Beznea return dev_err_probe(dev, irq, "Failed to get alarm IRQ!\n"); 672*d4488377SClaudiu Beznea 673*d4488377SClaudiu Beznea ret = devm_request_irq(dev, irq, rtca3_alarm_handler, 0, "rtca3-alarm", priv); 674*d4488377SClaudiu Beznea if (ret) 675*d4488377SClaudiu Beznea return dev_err_probe(dev, ret, "Failed to request alarm IRQ!\n"); 676*d4488377SClaudiu Beznea priv->wakeup_irq = irq; 677*d4488377SClaudiu Beznea 678*d4488377SClaudiu Beznea irq = platform_get_irq_byname(pdev, "period"); 679*d4488377SClaudiu Beznea if (irq < 0) 680*d4488377SClaudiu Beznea return dev_err_probe(dev, irq, "Failed to get period IRQ!\n"); 681*d4488377SClaudiu Beznea 682*d4488377SClaudiu Beznea ret = devm_request_irq(dev, irq, rtca3_periodic_handler, 0, "rtca3-period", priv); 683*d4488377SClaudiu Beznea if (ret) 684*d4488377SClaudiu Beznea return dev_err_probe(dev, ret, "Failed to request period IRQ!\n"); 685*d4488377SClaudiu Beznea 686*d4488377SClaudiu Beznea /* 687*d4488377SClaudiu Beznea * Driver doesn't implement carry handler. Just get the IRQ here 688*d4488377SClaudiu Beznea * for backward compatibility, in case carry support will be added later. 689*d4488377SClaudiu Beznea */ 690*d4488377SClaudiu Beznea irq = platform_get_irq_byname(pdev, "carry"); 691*d4488377SClaudiu Beznea if (irq < 0) 692*d4488377SClaudiu Beznea return dev_err_probe(dev, irq, "Failed to get carry IRQ!\n"); 693*d4488377SClaudiu Beznea 694*d4488377SClaudiu Beznea return 0; 695*d4488377SClaudiu Beznea } 696*d4488377SClaudiu Beznea 697*d4488377SClaudiu Beznea static void rtca3_action(void *data) 698*d4488377SClaudiu Beznea { 699*d4488377SClaudiu Beznea struct device *dev = data; 700*d4488377SClaudiu Beznea struct rtca3_priv *priv = dev_get_drvdata(dev); 701*d4488377SClaudiu Beznea int ret; 702*d4488377SClaudiu Beznea 703*d4488377SClaudiu Beznea ret = reset_control_assert(priv->rstc); 704*d4488377SClaudiu Beznea if (ret) 705*d4488377SClaudiu Beznea dev_err(dev, "Failed to de-assert reset!"); 706*d4488377SClaudiu Beznea 707*d4488377SClaudiu Beznea ret = pm_runtime_put_sync(dev); 708*d4488377SClaudiu Beznea if (ret < 0) 709*d4488377SClaudiu Beznea dev_err(dev, "Failed to runtime suspend!"); 710*d4488377SClaudiu Beznea } 711*d4488377SClaudiu Beznea 712*d4488377SClaudiu Beznea static int rtca3_probe(struct platform_device *pdev) 713*d4488377SClaudiu Beznea { 714*d4488377SClaudiu Beznea struct device *dev = &pdev->dev; 715*d4488377SClaudiu Beznea struct rtca3_priv *priv; 716*d4488377SClaudiu Beznea struct clk *clk; 717*d4488377SClaudiu Beznea int ret; 718*d4488377SClaudiu Beznea 719*d4488377SClaudiu Beznea priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 720*d4488377SClaudiu Beznea if (!priv) 721*d4488377SClaudiu Beznea return -ENOMEM; 722*d4488377SClaudiu Beznea 723*d4488377SClaudiu Beznea priv->base = devm_platform_ioremap_resource(pdev, 0); 724*d4488377SClaudiu Beznea if (IS_ERR(priv->base)) 725*d4488377SClaudiu Beznea return PTR_ERR(priv->base); 726*d4488377SClaudiu Beznea 727*d4488377SClaudiu Beznea ret = devm_pm_runtime_enable(dev); 728*d4488377SClaudiu Beznea if (ret) 729*d4488377SClaudiu Beznea return ret; 730*d4488377SClaudiu Beznea 731*d4488377SClaudiu Beznea priv->rstc = devm_reset_control_get_shared(dev, NULL); 732*d4488377SClaudiu Beznea if (IS_ERR(priv->rstc)) 733*d4488377SClaudiu Beznea return PTR_ERR(priv->rstc); 734*d4488377SClaudiu Beznea 735*d4488377SClaudiu Beznea ret = pm_runtime_resume_and_get(dev); 736*d4488377SClaudiu Beznea if (ret) 737*d4488377SClaudiu Beznea return ret; 738*d4488377SClaudiu Beznea 739*d4488377SClaudiu Beznea ret = reset_control_deassert(priv->rstc); 740*d4488377SClaudiu Beznea if (ret) { 741*d4488377SClaudiu Beznea pm_runtime_put_sync(dev); 742*d4488377SClaudiu Beznea return ret; 743*d4488377SClaudiu Beznea } 744*d4488377SClaudiu Beznea 745*d4488377SClaudiu Beznea dev_set_drvdata(dev, priv); 746*d4488377SClaudiu Beznea ret = devm_add_action_or_reset(dev, rtca3_action, dev); 747*d4488377SClaudiu Beznea if (ret) 748*d4488377SClaudiu Beznea return ret; 749*d4488377SClaudiu Beznea 750*d4488377SClaudiu Beznea /* 751*d4488377SClaudiu Beznea * This must be an always-on clock to keep the RTC running even after 752*d4488377SClaudiu Beznea * driver is unbinded. 753*d4488377SClaudiu Beznea */ 754*d4488377SClaudiu Beznea clk = devm_clk_get_enabled(dev, "counter"); 755*d4488377SClaudiu Beznea if (IS_ERR(clk)) 756*d4488377SClaudiu Beznea return PTR_ERR(clk); 757*d4488377SClaudiu Beznea 758*d4488377SClaudiu Beznea spin_lock_init(&priv->lock); 759*d4488377SClaudiu Beznea atomic_set(&priv->alrm_sstep, RTCA3_ALRM_SSTEP_DONE); 760*d4488377SClaudiu Beznea init_completion(&priv->set_alarm_completion); 761*d4488377SClaudiu Beznea 762*d4488377SClaudiu Beznea ret = rtca3_initial_setup(clk, priv); 763*d4488377SClaudiu Beznea if (ret) 764*d4488377SClaudiu Beznea return dev_err_probe(dev, ret, "Failed to setup the RTC!\n"); 765*d4488377SClaudiu Beznea 766*d4488377SClaudiu Beznea ret = rtca3_request_irqs(pdev, priv); 767*d4488377SClaudiu Beznea if (ret) 768*d4488377SClaudiu Beznea return ret; 769*d4488377SClaudiu Beznea 770*d4488377SClaudiu Beznea device_init_wakeup(&pdev->dev, 1); 771*d4488377SClaudiu Beznea 772*d4488377SClaudiu Beznea priv->rtc_dev = devm_rtc_allocate_device(&pdev->dev); 773*d4488377SClaudiu Beznea if (IS_ERR(priv->rtc_dev)) 774*d4488377SClaudiu Beznea return PTR_ERR(priv->rtc_dev); 775*d4488377SClaudiu Beznea 776*d4488377SClaudiu Beznea priv->rtc_dev->ops = &rtca3_ops; 777*d4488377SClaudiu Beznea priv->rtc_dev->max_user_freq = 256; 778*d4488377SClaudiu Beznea priv->rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_2000; 779*d4488377SClaudiu Beznea priv->rtc_dev->range_max = RTC_TIMESTAMP_END_2099; 780*d4488377SClaudiu Beznea 781*d4488377SClaudiu Beznea return devm_rtc_register_device(priv->rtc_dev); 782*d4488377SClaudiu Beznea } 783*d4488377SClaudiu Beznea 784*d4488377SClaudiu Beznea static void rtca3_remove(struct platform_device *pdev) 785*d4488377SClaudiu Beznea { 786*d4488377SClaudiu Beznea struct rtca3_priv *priv = platform_get_drvdata(pdev); 787*d4488377SClaudiu Beznea 788*d4488377SClaudiu Beznea guard(spinlock_irqsave)(&priv->lock); 789*d4488377SClaudiu Beznea 790*d4488377SClaudiu Beznea /* 791*d4488377SClaudiu Beznea * Disable alarm, periodic interrupts. The RTC device cannot 792*d4488377SClaudiu Beznea * power up the system. 793*d4488377SClaudiu Beznea */ 794*d4488377SClaudiu Beznea rtca3_alarm_irq_set_helper(priv, RTCA3_RCR1_AIE | RTCA3_RCR1_PIE, 0); 795*d4488377SClaudiu Beznea } 796*d4488377SClaudiu Beznea 797*d4488377SClaudiu Beznea static int rtca3_suspend(struct device *dev) 798*d4488377SClaudiu Beznea { 799*d4488377SClaudiu Beznea struct rtca3_priv *priv = dev_get_drvdata(dev); 800*d4488377SClaudiu Beznea 801*d4488377SClaudiu Beznea if (!device_may_wakeup(dev)) 802*d4488377SClaudiu Beznea return 0; 803*d4488377SClaudiu Beznea 804*d4488377SClaudiu Beznea /* Alarm setup in progress. */ 805*d4488377SClaudiu Beznea if (atomic_read(&priv->alrm_sstep) != RTCA3_ALRM_SSTEP_DONE) 806*d4488377SClaudiu Beznea return -EBUSY; 807*d4488377SClaudiu Beznea 808*d4488377SClaudiu Beznea enable_irq_wake(priv->wakeup_irq); 809*d4488377SClaudiu Beznea 810*d4488377SClaudiu Beznea return 0; 811*d4488377SClaudiu Beznea } 812*d4488377SClaudiu Beznea 813*d4488377SClaudiu Beznea static int rtca3_clean_alarm(struct rtca3_priv *priv) 814*d4488377SClaudiu Beznea { 815*d4488377SClaudiu Beznea struct rtc_device *rtc_dev = priv->rtc_dev; 816*d4488377SClaudiu Beznea time64_t alarm_time, now; 817*d4488377SClaudiu Beznea struct rtc_wkalrm alarm; 818*d4488377SClaudiu Beznea struct rtc_time tm; 819*d4488377SClaudiu Beznea u8 pending; 820*d4488377SClaudiu Beznea int ret; 821*d4488377SClaudiu Beznea 822*d4488377SClaudiu Beznea ret = rtc_read_alarm(rtc_dev, &alarm); 823*d4488377SClaudiu Beznea if (ret) 824*d4488377SClaudiu Beznea return ret; 825*d4488377SClaudiu Beznea 826*d4488377SClaudiu Beznea if (!alarm.enabled) 827*d4488377SClaudiu Beznea return 0; 828*d4488377SClaudiu Beznea 829*d4488377SClaudiu Beznea ret = rtc_read_time(rtc_dev, &tm); 830*d4488377SClaudiu Beznea if (ret) 831*d4488377SClaudiu Beznea return ret; 832*d4488377SClaudiu Beznea 833*d4488377SClaudiu Beznea alarm_time = rtc_tm_to_time64(&alarm.time); 834*d4488377SClaudiu Beznea now = rtc_tm_to_time64(&tm); 835*d4488377SClaudiu Beznea if (alarm_time >= now) 836*d4488377SClaudiu Beznea return 0; 837*d4488377SClaudiu Beznea 838*d4488377SClaudiu Beznea /* 839*d4488377SClaudiu Beznea * Heuristically, it has been determined that when returning from deep 840*d4488377SClaudiu Beznea * sleep state the RTCA3_RSR.AF is zero even though the alarm expired. 841*d4488377SClaudiu Beznea * Call again the rtc_update_irq() if alarm helper detects this. 842*d4488377SClaudiu Beznea */ 843*d4488377SClaudiu Beznea 844*d4488377SClaudiu Beznea guard(spinlock_irqsave)(&priv->lock); 845*d4488377SClaudiu Beznea 846*d4488377SClaudiu Beznea pending = rtca3_alarm_handler_helper(priv); 847*d4488377SClaudiu Beznea if (!pending) 848*d4488377SClaudiu Beznea rtc_update_irq(priv->rtc_dev, 1, RTC_AF | RTC_IRQF); 849*d4488377SClaudiu Beznea 850*d4488377SClaudiu Beznea return 0; 851*d4488377SClaudiu Beznea } 852*d4488377SClaudiu Beznea 853*d4488377SClaudiu Beznea static int rtca3_resume(struct device *dev) 854*d4488377SClaudiu Beznea { 855*d4488377SClaudiu Beznea struct rtca3_priv *priv = dev_get_drvdata(dev); 856*d4488377SClaudiu Beznea 857*d4488377SClaudiu Beznea if (!device_may_wakeup(dev)) 858*d4488377SClaudiu Beznea return 0; 859*d4488377SClaudiu Beznea 860*d4488377SClaudiu Beznea disable_irq_wake(priv->wakeup_irq); 861*d4488377SClaudiu Beznea 862*d4488377SClaudiu Beznea /* 863*d4488377SClaudiu Beznea * According to the HW manual (section 22.6.4 Notes on writing to 864*d4488377SClaudiu Beznea * and reading from registers) we need to wait 1/128 seconds while 865*d4488377SClaudiu Beznea * RCR2.START = 1 to be able to read the counters after a return from low 866*d4488377SClaudiu Beznea * power consumption state. 867*d4488377SClaudiu Beznea */ 868*d4488377SClaudiu Beznea mdelay(8); 869*d4488377SClaudiu Beznea 870*d4488377SClaudiu Beznea /* 871*d4488377SClaudiu Beznea * The alarm cannot wake the system from deep sleep states. In case 872*d4488377SClaudiu Beznea * we return from deep sleep states and the alarm expired we need 873*d4488377SClaudiu Beznea * to disable it to avoid failures when setting another alarm. 874*d4488377SClaudiu Beznea */ 875*d4488377SClaudiu Beznea return rtca3_clean_alarm(priv); 876*d4488377SClaudiu Beznea } 877*d4488377SClaudiu Beznea 878*d4488377SClaudiu Beznea static DEFINE_SIMPLE_DEV_PM_OPS(rtca3_pm_ops, rtca3_suspend, rtca3_resume); 879*d4488377SClaudiu Beznea 880*d4488377SClaudiu Beznea static const struct of_device_id rtca3_of_match[] = { 881*d4488377SClaudiu Beznea { .compatible = "renesas,rz-rtca3", }, 882*d4488377SClaudiu Beznea { /* sentinel */ } 883*d4488377SClaudiu Beznea }; 884*d4488377SClaudiu Beznea MODULE_DEVICE_TABLE(of, rtca3_of_match); 885*d4488377SClaudiu Beznea 886*d4488377SClaudiu Beznea static struct platform_driver rtca3_platform_driver = { 887*d4488377SClaudiu Beznea .driver = { 888*d4488377SClaudiu Beznea .name = "rtc-rtca3", 889*d4488377SClaudiu Beznea .pm = pm_ptr(&rtca3_pm_ops), 890*d4488377SClaudiu Beznea .of_match_table = rtca3_of_match, 891*d4488377SClaudiu Beznea }, 892*d4488377SClaudiu Beznea .probe = rtca3_probe, 893*d4488377SClaudiu Beznea .remove = rtca3_remove, 894*d4488377SClaudiu Beznea }; 895*d4488377SClaudiu Beznea module_platform_driver(rtca3_platform_driver); 896*d4488377SClaudiu Beznea 897*d4488377SClaudiu Beznea MODULE_DESCRIPTION("Renesas RTCA-3 RTC driver"); 898*d4488377SClaudiu Beznea MODULE_AUTHOR("Claudiu Beznea <claudiu.beznea.uj@bp.renesas.com>"); 899*d4488377SClaudiu Beznea MODULE_LICENSE("GPL"); 900