Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Mediatek SoCs General-Purpose Timer handling.
0004  *
0005  * Copyright (C) 2014 Matthias Brugger
0006  *
0007  * Matthias Brugger <matthias.bgg@gmail.com>
0008  */
0009 
0010 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0011 
0012 #include <linux/clockchips.h>
0013 #include <linux/clocksource.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/irqreturn.h>
0016 #include <linux/sched_clock.h>
0017 #include <linux/slab.h>
0018 #include "timer-of.h"
0019 
0020 #define TIMER_CLK_EVT           (1)
0021 #define TIMER_CLK_SRC           (2)
0022 
0023 #define TIMER_SYNC_TICKS        (3)
0024 
0025 /* cpux mcusys wrapper */
0026 #define CPUX_CON_REG        0x0
0027 #define CPUX_IDX_REG        0x4
0028 
0029 /* cpux */
0030 #define CPUX_IDX_GLOBAL_CTRL    0x0
0031  #define CPUX_ENABLE        BIT(0)
0032  #define CPUX_CLK_DIV_MASK  GENMASK(10, 8)
0033  #define CPUX_CLK_DIV1      BIT(8)
0034  #define CPUX_CLK_DIV2      BIT(9)
0035  #define CPUX_CLK_DIV4      BIT(10)
0036 #define CPUX_IDX_GLOBAL_IRQ 0x30
0037 
0038 /* gpt */
0039 #define GPT_IRQ_EN_REG          0x00
0040 #define GPT_IRQ_ENABLE(val)     BIT((val) - 1)
0041 #define GPT_IRQ_ACK_REG         0x08
0042 #define GPT_IRQ_ACK(val)        BIT((val) - 1)
0043 
0044 #define GPT_CTRL_REG(val)       (0x10 * (val))
0045 #define GPT_CTRL_OP(val)        (((val) & 0x3) << 4)
0046 #define GPT_CTRL_OP_ONESHOT     (0)
0047 #define GPT_CTRL_OP_REPEAT      (1)
0048 #define GPT_CTRL_OP_FREERUN     (3)
0049 #define GPT_CTRL_CLEAR          (2)
0050 #define GPT_CTRL_ENABLE         (1)
0051 #define GPT_CTRL_DISABLE        (0)
0052 
0053 #define GPT_CLK_REG(val)        (0x04 + (0x10 * (val)))
0054 #define GPT_CLK_SRC(val)        (((val) & 0x1) << 4)
0055 #define GPT_CLK_SRC_SYS13M      (0)
0056 #define GPT_CLK_SRC_RTC32K      (1)
0057 #define GPT_CLK_DIV1            (0x0)
0058 #define GPT_CLK_DIV2            (0x1)
0059 
0060 #define GPT_CNT_REG(val)        (0x08 + (0x10 * (val)))
0061 #define GPT_CMP_REG(val)        (0x0C + (0x10 * (val)))
0062 
0063 /* system timer */
0064 #define SYST_BASE               (0x40)
0065 
0066 #define SYST_CON                (SYST_BASE + 0x0)
0067 #define SYST_VAL                (SYST_BASE + 0x4)
0068 
0069 #define SYST_CON_REG(to)        (timer_of_base(to) + SYST_CON)
0070 #define SYST_VAL_REG(to)        (timer_of_base(to) + SYST_VAL)
0071 
0072 /*
0073  * SYST_CON_EN: Clock enable. Shall be set to
0074  *   - Start timer countdown.
0075  *   - Allow timeout ticks being updated.
0076  *   - Allow changing interrupt status,like clear irq pending.
0077  *
0078  * SYST_CON_IRQ_EN: Set to enable interrupt.
0079  *
0080  * SYST_CON_IRQ_CLR: Set to clear interrupt.
0081  */
0082 #define SYST_CON_EN              BIT(0)
0083 #define SYST_CON_IRQ_EN          BIT(1)
0084 #define SYST_CON_IRQ_CLR         BIT(4)
0085 
0086 static void __iomem *gpt_sched_reg __read_mostly;
0087 
0088 static u32 mtk_cpux_readl(u32 reg_idx, struct timer_of *to)
0089 {
0090     writel(reg_idx, timer_of_base(to) + CPUX_IDX_REG);
0091     return readl(timer_of_base(to) + CPUX_CON_REG);
0092 }
0093 
0094 static void mtk_cpux_writel(u32 val, u32 reg_idx, struct timer_of *to)
0095 {
0096     writel(reg_idx, timer_of_base(to) + CPUX_IDX_REG);
0097     writel(val, timer_of_base(to) + CPUX_CON_REG);
0098 }
0099 
0100 static void mtk_cpux_set_irq(struct timer_of *to, bool enable)
0101 {
0102     const unsigned long *irq_mask = cpumask_bits(cpu_possible_mask);
0103     u32 val;
0104 
0105     val = mtk_cpux_readl(CPUX_IDX_GLOBAL_IRQ, to);
0106 
0107     if (enable)
0108         val |= *irq_mask;
0109     else
0110         val &= ~(*irq_mask);
0111 
0112     mtk_cpux_writel(val, CPUX_IDX_GLOBAL_IRQ, to);
0113 }
0114 
0115 static int mtk_cpux_clkevt_shutdown(struct clock_event_device *clkevt)
0116 {
0117     /* Clear any irq */
0118     mtk_cpux_set_irq(to_timer_of(clkevt), false);
0119 
0120     /*
0121      * Disabling CPUXGPT timer will crash the platform, especially
0122      * if Trusted Firmware is using it (usually, for sleep states),
0123      * so we only mask the IRQ and call it a day.
0124      */
0125     return 0;
0126 }
0127 
0128 static int mtk_cpux_clkevt_resume(struct clock_event_device *clkevt)
0129 {
0130     mtk_cpux_set_irq(to_timer_of(clkevt), true);
0131     return 0;
0132 }
0133 
0134 static void mtk_syst_ack_irq(struct timer_of *to)
0135 {
0136     /* Clear and disable interrupt */
0137     writel(SYST_CON_EN, SYST_CON_REG(to));
0138     writel(SYST_CON_IRQ_CLR | SYST_CON_EN, SYST_CON_REG(to));
0139 }
0140 
0141 static irqreturn_t mtk_syst_handler(int irq, void *dev_id)
0142 {
0143     struct clock_event_device *clkevt = dev_id;
0144     struct timer_of *to = to_timer_of(clkevt);
0145 
0146     mtk_syst_ack_irq(to);
0147     clkevt->event_handler(clkevt);
0148 
0149     return IRQ_HANDLED;
0150 }
0151 
0152 static int mtk_syst_clkevt_next_event(unsigned long ticks,
0153                       struct clock_event_device *clkevt)
0154 {
0155     struct timer_of *to = to_timer_of(clkevt);
0156 
0157     /* Enable clock to allow timeout tick update later */
0158     writel(SYST_CON_EN, SYST_CON_REG(to));
0159 
0160     /*
0161      * Write new timeout ticks. Timer shall start countdown
0162      * after timeout ticks are updated.
0163      */
0164     writel(ticks, SYST_VAL_REG(to));
0165 
0166     /* Enable interrupt */
0167     writel(SYST_CON_EN | SYST_CON_IRQ_EN, SYST_CON_REG(to));
0168 
0169     return 0;
0170 }
0171 
0172 static int mtk_syst_clkevt_shutdown(struct clock_event_device *clkevt)
0173 {
0174     /* Clear any irq */
0175     mtk_syst_ack_irq(to_timer_of(clkevt));
0176 
0177     /* Disable timer */
0178     writel(0, SYST_CON_REG(to_timer_of(clkevt)));
0179 
0180     return 0;
0181 }
0182 
0183 static int mtk_syst_clkevt_resume(struct clock_event_device *clkevt)
0184 {
0185     return mtk_syst_clkevt_shutdown(clkevt);
0186 }
0187 
0188 static int mtk_syst_clkevt_oneshot(struct clock_event_device *clkevt)
0189 {
0190     return 0;
0191 }
0192 
0193 static u64 notrace mtk_gpt_read_sched_clock(void)
0194 {
0195     return readl_relaxed(gpt_sched_reg);
0196 }
0197 
0198 static void mtk_gpt_clkevt_time_stop(struct timer_of *to, u8 timer)
0199 {
0200     u32 val;
0201 
0202     val = readl(timer_of_base(to) + GPT_CTRL_REG(timer));
0203     writel(val & ~GPT_CTRL_ENABLE, timer_of_base(to) +
0204            GPT_CTRL_REG(timer));
0205 }
0206 
0207 static void mtk_gpt_clkevt_time_setup(struct timer_of *to,
0208                       unsigned long delay, u8 timer)
0209 {
0210     writel(delay, timer_of_base(to) + GPT_CMP_REG(timer));
0211 }
0212 
0213 static void mtk_gpt_clkevt_time_start(struct timer_of *to,
0214                       bool periodic, u8 timer)
0215 {
0216     u32 val;
0217 
0218     /* Acknowledge interrupt */
0219     writel(GPT_IRQ_ACK(timer), timer_of_base(to) + GPT_IRQ_ACK_REG);
0220 
0221     val = readl(timer_of_base(to) + GPT_CTRL_REG(timer));
0222 
0223     /* Clear 2 bit timer operation mode field */
0224     val &= ~GPT_CTRL_OP(0x3);
0225 
0226     if (periodic)
0227         val |= GPT_CTRL_OP(GPT_CTRL_OP_REPEAT);
0228     else
0229         val |= GPT_CTRL_OP(GPT_CTRL_OP_ONESHOT);
0230 
0231     writel(val | GPT_CTRL_ENABLE | GPT_CTRL_CLEAR,
0232            timer_of_base(to) + GPT_CTRL_REG(timer));
0233 }
0234 
0235 static int mtk_gpt_clkevt_shutdown(struct clock_event_device *clk)
0236 {
0237     mtk_gpt_clkevt_time_stop(to_timer_of(clk), TIMER_CLK_EVT);
0238 
0239     return 0;
0240 }
0241 
0242 static int mtk_gpt_clkevt_set_periodic(struct clock_event_device *clk)
0243 {
0244     struct timer_of *to = to_timer_of(clk);
0245 
0246     mtk_gpt_clkevt_time_stop(to, TIMER_CLK_EVT);
0247     mtk_gpt_clkevt_time_setup(to, to->of_clk.period, TIMER_CLK_EVT);
0248     mtk_gpt_clkevt_time_start(to, true, TIMER_CLK_EVT);
0249 
0250     return 0;
0251 }
0252 
0253 static int mtk_gpt_clkevt_next_event(unsigned long event,
0254                      struct clock_event_device *clk)
0255 {
0256     struct timer_of *to = to_timer_of(clk);
0257 
0258     mtk_gpt_clkevt_time_stop(to, TIMER_CLK_EVT);
0259     mtk_gpt_clkevt_time_setup(to, event, TIMER_CLK_EVT);
0260     mtk_gpt_clkevt_time_start(to, false, TIMER_CLK_EVT);
0261 
0262     return 0;
0263 }
0264 
0265 static irqreturn_t mtk_gpt_interrupt(int irq, void *dev_id)
0266 {
0267     struct clock_event_device *clkevt = (struct clock_event_device *)dev_id;
0268     struct timer_of *to = to_timer_of(clkevt);
0269 
0270     /* Acknowledge timer0 irq */
0271     writel(GPT_IRQ_ACK(TIMER_CLK_EVT), timer_of_base(to) + GPT_IRQ_ACK_REG);
0272     clkevt->event_handler(clkevt);
0273 
0274     return IRQ_HANDLED;
0275 }
0276 
0277 static void
0278 __init mtk_gpt_setup(struct timer_of *to, u8 timer, u8 option)
0279 {
0280     writel(GPT_CTRL_CLEAR | GPT_CTRL_DISABLE,
0281            timer_of_base(to) + GPT_CTRL_REG(timer));
0282 
0283     writel(GPT_CLK_SRC(GPT_CLK_SRC_SYS13M) | GPT_CLK_DIV1,
0284            timer_of_base(to) + GPT_CLK_REG(timer));
0285 
0286     writel(0x0, timer_of_base(to) + GPT_CMP_REG(timer));
0287 
0288     writel(GPT_CTRL_OP(option) | GPT_CTRL_ENABLE,
0289            timer_of_base(to) + GPT_CTRL_REG(timer));
0290 }
0291 
0292 static void mtk_gpt_enable_irq(struct timer_of *to, u8 timer)
0293 {
0294     u32 val;
0295 
0296     /* Disable all interrupts */
0297     writel(0x0, timer_of_base(to) + GPT_IRQ_EN_REG);
0298 
0299     /* Acknowledge all spurious pending interrupts */
0300     writel(0x3f, timer_of_base(to) + GPT_IRQ_ACK_REG);
0301 
0302     val = readl(timer_of_base(to) + GPT_IRQ_EN_REG);
0303     writel(val | GPT_IRQ_ENABLE(timer),
0304            timer_of_base(to) + GPT_IRQ_EN_REG);
0305 }
0306 
0307 static void mtk_gpt_resume(struct clock_event_device *clk)
0308 {
0309     struct timer_of *to = to_timer_of(clk);
0310 
0311     mtk_gpt_enable_irq(to, TIMER_CLK_EVT);
0312 }
0313 
0314 static void mtk_gpt_suspend(struct clock_event_device *clk)
0315 {
0316     struct timer_of *to = to_timer_of(clk);
0317 
0318     /* Disable all interrupts */
0319     writel(0x0, timer_of_base(to) + GPT_IRQ_EN_REG);
0320 
0321     /*
0322      * This is called with interrupts disabled,
0323      * so we need to ack any interrupt that is pending
0324      * or for example ATF will prevent a suspend from completing.
0325      */
0326     writel(0x3f, timer_of_base(to) + GPT_IRQ_ACK_REG);
0327 }
0328 
0329 static struct timer_of to = {
0330     .flags = TIMER_OF_IRQ | TIMER_OF_BASE | TIMER_OF_CLOCK,
0331 
0332     .clkevt = {
0333         .name = "mtk-clkevt",
0334         .rating = 300,
0335         .cpumask = cpu_possible_mask,
0336     },
0337 
0338     .of_irq = {
0339         .flags = IRQF_TIMER | IRQF_IRQPOLL,
0340     },
0341 };
0342 
0343 static int __init mtk_cpux_init(struct device_node *node)
0344 {
0345     static struct timer_of to_cpux;
0346     u32 freq, val;
0347     int ret;
0348 
0349     /*
0350      * There are per-cpu interrupts for the CPUX General Purpose Timer
0351      * but since this timer feeds the AArch64 System Timer we can rely
0352      * on the CPU timer PPIs as well, so we don't declare TIMER_OF_IRQ.
0353      */
0354     to_cpux.flags = TIMER_OF_BASE | TIMER_OF_CLOCK;
0355     to_cpux.clkevt.name = "mtk-cpuxgpt";
0356     to_cpux.clkevt.rating = 10;
0357     to_cpux.clkevt.cpumask = cpu_possible_mask;
0358     to_cpux.clkevt.set_state_shutdown = mtk_cpux_clkevt_shutdown;
0359     to_cpux.clkevt.tick_resume = mtk_cpux_clkevt_resume;
0360 
0361     /* If this fails, bad things are about to happen... */
0362     ret = timer_of_init(node, &to_cpux);
0363     if (ret) {
0364         WARN(1, "Cannot start CPUX timers.\n");
0365         return ret;
0366     }
0367 
0368     /*
0369      * Check if we're given a clock with the right frequency for this
0370      * timer, otherwise warn but keep going with the setup anyway, as
0371      * that makes it possible to still boot the kernel, even though
0372      * it may not work correctly (random lockups, etc).
0373      * The reason behind this is that having an early UART may not be
0374      * possible for everyone and this gives a chance to retrieve kmsg
0375      * for eventual debugging even on consumer devices.
0376      */
0377     freq = timer_of_rate(&to_cpux);
0378     if (freq > 13000000)
0379         WARN(1, "Requested unsupported timer frequency %u\n", freq);
0380 
0381     /* Clock input is 26MHz, set DIV2 to achieve 13MHz clock */
0382     val = mtk_cpux_readl(CPUX_IDX_GLOBAL_CTRL, &to_cpux);
0383     val &= ~CPUX_CLK_DIV_MASK;
0384     val |= CPUX_CLK_DIV2;
0385     mtk_cpux_writel(val, CPUX_IDX_GLOBAL_CTRL, &to_cpux);
0386 
0387     /* Enable all CPUXGPT timers */
0388     val = mtk_cpux_readl(CPUX_IDX_GLOBAL_CTRL, &to_cpux);
0389     mtk_cpux_writel(val | CPUX_ENABLE, CPUX_IDX_GLOBAL_CTRL, &to_cpux);
0390 
0391     clockevents_config_and_register(&to_cpux.clkevt, timer_of_rate(&to_cpux),
0392                     TIMER_SYNC_TICKS, 0xffffffff);
0393 
0394     return 0;
0395 }
0396 
0397 static int __init mtk_syst_init(struct device_node *node)
0398 {
0399     int ret;
0400 
0401     to.clkevt.features = CLOCK_EVT_FEAT_DYNIRQ | CLOCK_EVT_FEAT_ONESHOT;
0402     to.clkevt.set_state_shutdown = mtk_syst_clkevt_shutdown;
0403     to.clkevt.set_state_oneshot = mtk_syst_clkevt_oneshot;
0404     to.clkevt.tick_resume = mtk_syst_clkevt_resume;
0405     to.clkevt.set_next_event = mtk_syst_clkevt_next_event;
0406     to.of_irq.handler = mtk_syst_handler;
0407 
0408     ret = timer_of_init(node, &to);
0409     if (ret)
0410         return ret;
0411 
0412     clockevents_config_and_register(&to.clkevt, timer_of_rate(&to),
0413                     TIMER_SYNC_TICKS, 0xffffffff);
0414 
0415     return 0;
0416 }
0417 
0418 static int __init mtk_gpt_init(struct device_node *node)
0419 {
0420     int ret;
0421 
0422     to.clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
0423     to.clkevt.set_state_shutdown = mtk_gpt_clkevt_shutdown;
0424     to.clkevt.set_state_periodic = mtk_gpt_clkevt_set_periodic;
0425     to.clkevt.set_state_oneshot = mtk_gpt_clkevt_shutdown;
0426     to.clkevt.tick_resume = mtk_gpt_clkevt_shutdown;
0427     to.clkevt.set_next_event = mtk_gpt_clkevt_next_event;
0428     to.clkevt.suspend = mtk_gpt_suspend;
0429     to.clkevt.resume = mtk_gpt_resume;
0430     to.of_irq.handler = mtk_gpt_interrupt;
0431 
0432     ret = timer_of_init(node, &to);
0433     if (ret)
0434         return ret;
0435 
0436     /* Configure clock source */
0437     mtk_gpt_setup(&to, TIMER_CLK_SRC, GPT_CTRL_OP_FREERUN);
0438     clocksource_mmio_init(timer_of_base(&to) + GPT_CNT_REG(TIMER_CLK_SRC),
0439                   node->name, timer_of_rate(&to), 300, 32,
0440                   clocksource_mmio_readl_up);
0441     gpt_sched_reg = timer_of_base(&to) + GPT_CNT_REG(TIMER_CLK_SRC);
0442     sched_clock_register(mtk_gpt_read_sched_clock, 32, timer_of_rate(&to));
0443 
0444     /* Configure clock event */
0445     mtk_gpt_setup(&to, TIMER_CLK_EVT, GPT_CTRL_OP_REPEAT);
0446     clockevents_config_and_register(&to.clkevt, timer_of_rate(&to),
0447                     TIMER_SYNC_TICKS, 0xffffffff);
0448 
0449     mtk_gpt_enable_irq(&to, TIMER_CLK_EVT);
0450 
0451     return 0;
0452 }
0453 TIMER_OF_DECLARE(mtk_mt6577, "mediatek,mt6577-timer", mtk_gpt_init);
0454 TIMER_OF_DECLARE(mtk_mt6765, "mediatek,mt6765-timer", mtk_syst_init);
0455 TIMER_OF_DECLARE(mtk_mt6795, "mediatek,mt6795-systimer", mtk_cpux_init);