1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
280ecbd24SJohn Crispin /*
362ee73d2SPaul Gortmaker * Ralink RT2880 timer
462ee73d2SPaul Gortmaker * Author: John Crispin
562ee73d2SPaul Gortmaker *
697b92108SJohn Crispin * Copyright (C) 2013 John Crispin <john@phrozen.org>
780ecbd24SJohn Crispin */
880ecbd24SJohn Crispin
980ecbd24SJohn Crispin #include <linux/platform_device.h>
1080ecbd24SJohn Crispin #include <linux/interrupt.h>
1180ecbd24SJohn Crispin #include <linux/timer.h>
1280ecbd24SJohn Crispin #include <linux/of_gpio.h>
1380ecbd24SJohn Crispin #include <linux/clk.h>
1480ecbd24SJohn Crispin
1580ecbd24SJohn Crispin #include <asm/mach-ralink/ralink_regs.h>
1680ecbd24SJohn Crispin
1780ecbd24SJohn Crispin #define TIMER_REG_TMRSTAT 0x00
1880ecbd24SJohn Crispin #define TIMER_REG_TMR0LOAD 0x10
1980ecbd24SJohn Crispin #define TIMER_REG_TMR0CTL 0x18
2080ecbd24SJohn Crispin
2180ecbd24SJohn Crispin #define TMRSTAT_TMR0INT BIT(0)
2280ecbd24SJohn Crispin
2380ecbd24SJohn Crispin #define TMR0CTL_ENABLE BIT(7)
2480ecbd24SJohn Crispin #define TMR0CTL_MODE_PERIODIC BIT(4)
2580ecbd24SJohn Crispin #define TMR0CTL_PRESCALER 1
2680ecbd24SJohn Crispin #define TMR0CTL_PRESCALE_VAL (0xf - TMR0CTL_PRESCALER)
2780ecbd24SJohn Crispin #define TMR0CTL_PRESCALE_DIV (65536 / BIT(TMR0CTL_PRESCALER))
2880ecbd24SJohn Crispin
2980ecbd24SJohn Crispin struct rt_timer {
3080ecbd24SJohn Crispin struct device *dev;
3180ecbd24SJohn Crispin void __iomem *membase;
3280ecbd24SJohn Crispin int irq;
3380ecbd24SJohn Crispin unsigned long timer_freq;
3480ecbd24SJohn Crispin unsigned long timer_div;
3580ecbd24SJohn Crispin };
3680ecbd24SJohn Crispin
rt_timer_w32(struct rt_timer * rt,u8 reg,u32 val)3780ecbd24SJohn Crispin static inline void rt_timer_w32(struct rt_timer *rt, u8 reg, u32 val)
3880ecbd24SJohn Crispin {
3980ecbd24SJohn Crispin __raw_writel(val, rt->membase + reg);
4080ecbd24SJohn Crispin }
4180ecbd24SJohn Crispin
rt_timer_r32(struct rt_timer * rt,u8 reg)4280ecbd24SJohn Crispin static inline u32 rt_timer_r32(struct rt_timer *rt, u8 reg)
4380ecbd24SJohn Crispin {
4480ecbd24SJohn Crispin return __raw_readl(rt->membase + reg);
4580ecbd24SJohn Crispin }
4680ecbd24SJohn Crispin
rt_timer_irq(int irq,void * _rt)4780ecbd24SJohn Crispin static irqreturn_t rt_timer_irq(int irq, void *_rt)
4880ecbd24SJohn Crispin {
4980ecbd24SJohn Crispin struct rt_timer *rt = (struct rt_timer *) _rt;
5080ecbd24SJohn Crispin
5180ecbd24SJohn Crispin rt_timer_w32(rt, TIMER_REG_TMR0LOAD, rt->timer_freq / rt->timer_div);
5280ecbd24SJohn Crispin rt_timer_w32(rt, TIMER_REG_TMRSTAT, TMRSTAT_TMR0INT);
5380ecbd24SJohn Crispin
5480ecbd24SJohn Crispin return IRQ_HANDLED;
5580ecbd24SJohn Crispin }
5680ecbd24SJohn Crispin
5780ecbd24SJohn Crispin
rt_timer_request(struct rt_timer * rt)5880ecbd24SJohn Crispin static int rt_timer_request(struct rt_timer *rt)
5980ecbd24SJohn Crispin {
6031d6f57dSMichael Opdenacker int err = request_irq(rt->irq, rt_timer_irq, 0,
6180ecbd24SJohn Crispin dev_name(rt->dev), rt);
6280ecbd24SJohn Crispin if (err) {
6380ecbd24SJohn Crispin dev_err(rt->dev, "failed to request irq\n");
6480ecbd24SJohn Crispin } else {
6580ecbd24SJohn Crispin u32 t = TMR0CTL_MODE_PERIODIC | TMR0CTL_PRESCALE_VAL;
6680ecbd24SJohn Crispin rt_timer_w32(rt, TIMER_REG_TMR0CTL, t);
6780ecbd24SJohn Crispin }
6880ecbd24SJohn Crispin return err;
6980ecbd24SJohn Crispin }
7080ecbd24SJohn Crispin
rt_timer_config(struct rt_timer * rt,unsigned long divisor)7180ecbd24SJohn Crispin static int rt_timer_config(struct rt_timer *rt, unsigned long divisor)
7280ecbd24SJohn Crispin {
7380ecbd24SJohn Crispin if (rt->timer_freq < divisor)
7480ecbd24SJohn Crispin rt->timer_div = rt->timer_freq;
7580ecbd24SJohn Crispin else
7680ecbd24SJohn Crispin rt->timer_div = divisor;
7780ecbd24SJohn Crispin
7880ecbd24SJohn Crispin rt_timer_w32(rt, TIMER_REG_TMR0LOAD, rt->timer_freq / rt->timer_div);
7980ecbd24SJohn Crispin
8080ecbd24SJohn Crispin return 0;
8180ecbd24SJohn Crispin }
8280ecbd24SJohn Crispin
rt_timer_enable(struct rt_timer * rt)8380ecbd24SJohn Crispin static int rt_timer_enable(struct rt_timer *rt)
8480ecbd24SJohn Crispin {
8580ecbd24SJohn Crispin u32 t;
8680ecbd24SJohn Crispin
8780ecbd24SJohn Crispin rt_timer_w32(rt, TIMER_REG_TMR0LOAD, rt->timer_freq / rt->timer_div);
8880ecbd24SJohn Crispin
8980ecbd24SJohn Crispin t = rt_timer_r32(rt, TIMER_REG_TMR0CTL);
9080ecbd24SJohn Crispin t |= TMR0CTL_ENABLE;
9180ecbd24SJohn Crispin rt_timer_w32(rt, TIMER_REG_TMR0CTL, t);
9280ecbd24SJohn Crispin
9380ecbd24SJohn Crispin return 0;
9480ecbd24SJohn Crispin }
9580ecbd24SJohn Crispin
rt_timer_probe(struct platform_device * pdev)9680ecbd24SJohn Crispin static int rt_timer_probe(struct platform_device *pdev)
9780ecbd24SJohn Crispin {
9880ecbd24SJohn Crispin struct rt_timer *rt;
9980ecbd24SJohn Crispin struct clk *clk;
10080ecbd24SJohn Crispin
10180ecbd24SJohn Crispin rt = devm_kzalloc(&pdev->dev, sizeof(*rt), GFP_KERNEL);
10280ecbd24SJohn Crispin if (!rt) {
10380ecbd24SJohn Crispin dev_err(&pdev->dev, "failed to allocate memory\n");
10480ecbd24SJohn Crispin return -ENOMEM;
10580ecbd24SJohn Crispin }
10680ecbd24SJohn Crispin
10780ecbd24SJohn Crispin rt->irq = platform_get_irq(pdev, 0);
108322e577bSStephen Boyd if (rt->irq < 0)
109b6ab1a13SArvind Yadav return rt->irq;
11080ecbd24SJohn Crispin
111*159c610aSYe Xingchen rt->membase = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
11280ecbd24SJohn Crispin if (IS_ERR(rt->membase))
11380ecbd24SJohn Crispin return PTR_ERR(rt->membase);
11480ecbd24SJohn Crispin
11580ecbd24SJohn Crispin clk = devm_clk_get(&pdev->dev, NULL);
11680ecbd24SJohn Crispin if (IS_ERR(clk)) {
11780ecbd24SJohn Crispin dev_err(&pdev->dev, "failed get clock rate\n");
11880ecbd24SJohn Crispin return PTR_ERR(clk);
11980ecbd24SJohn Crispin }
12080ecbd24SJohn Crispin
12180ecbd24SJohn Crispin rt->timer_freq = clk_get_rate(clk) / TMR0CTL_PRESCALE_DIV;
12280ecbd24SJohn Crispin if (!rt->timer_freq)
12380ecbd24SJohn Crispin return -EINVAL;
12480ecbd24SJohn Crispin
12580ecbd24SJohn Crispin rt->dev = &pdev->dev;
12680ecbd24SJohn Crispin platform_set_drvdata(pdev, rt);
12780ecbd24SJohn Crispin
12880ecbd24SJohn Crispin rt_timer_request(rt);
12980ecbd24SJohn Crispin rt_timer_config(rt, 2);
13080ecbd24SJohn Crispin rt_timer_enable(rt);
13180ecbd24SJohn Crispin
13277d84ff8SMasanari Iida dev_info(&pdev->dev, "maximum frequency is %luHz\n", rt->timer_freq);
13380ecbd24SJohn Crispin
13480ecbd24SJohn Crispin return 0;
13580ecbd24SJohn Crispin }
13680ecbd24SJohn Crispin
13780ecbd24SJohn Crispin static const struct of_device_id rt_timer_match[] = {
13880ecbd24SJohn Crispin { .compatible = "ralink,rt2880-timer" },
13980ecbd24SJohn Crispin {},
14080ecbd24SJohn Crispin };
14180ecbd24SJohn Crispin
14280ecbd24SJohn Crispin static struct platform_driver rt_timer_driver = {
14380ecbd24SJohn Crispin .probe = rt_timer_probe,
14480ecbd24SJohn Crispin .driver = {
14580ecbd24SJohn Crispin .name = "rt-timer",
14662ee73d2SPaul Gortmaker .of_match_table = rt_timer_match,
14762ee73d2SPaul Gortmaker .suppress_bind_attrs = true,
14880ecbd24SJohn Crispin },
14980ecbd24SJohn Crispin };
15062ee73d2SPaul Gortmaker builtin_platform_driver(rt_timer_driver);
151