John Crispin | 80ecbd2 | 2013-08-08 19:25:15 +0200 | [diff] [blame] | 1 | /* |
Paul Gortmaker | 62ee73d | 2016-08-15 16:30:53 -0400 | [diff] [blame^] | 2 | * Ralink RT2880 timer |
| 3 | * Author: John Crispin |
| 4 | * |
John Crispin | 80ecbd2 | 2013-08-08 19:25:15 +0200 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published |
| 7 | * by the Free Software Foundation. |
| 8 | * |
John Crispin | 97b9210 | 2016-05-05 09:57:56 +0200 | [diff] [blame] | 9 | * Copyright (C) 2013 John Crispin <john@phrozen.org> |
John Crispin | 80ecbd2 | 2013-08-08 19:25:15 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
John Crispin | 80ecbd2 | 2013-08-08 19:25:15 +0200 | [diff] [blame] | 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/timer.h> |
| 15 | #include <linux/of_gpio.h> |
| 16 | #include <linux/clk.h> |
| 17 | |
| 18 | #include <asm/mach-ralink/ralink_regs.h> |
| 19 | |
| 20 | #define TIMER_REG_TMRSTAT 0x00 |
| 21 | #define TIMER_REG_TMR0LOAD 0x10 |
| 22 | #define TIMER_REG_TMR0CTL 0x18 |
| 23 | |
| 24 | #define TMRSTAT_TMR0INT BIT(0) |
| 25 | |
| 26 | #define TMR0CTL_ENABLE BIT(7) |
| 27 | #define TMR0CTL_MODE_PERIODIC BIT(4) |
| 28 | #define TMR0CTL_PRESCALER 1 |
| 29 | #define TMR0CTL_PRESCALE_VAL (0xf - TMR0CTL_PRESCALER) |
| 30 | #define TMR0CTL_PRESCALE_DIV (65536 / BIT(TMR0CTL_PRESCALER)) |
| 31 | |
| 32 | struct rt_timer { |
| 33 | struct device *dev; |
| 34 | void __iomem *membase; |
| 35 | int irq; |
| 36 | unsigned long timer_freq; |
| 37 | unsigned long timer_div; |
| 38 | }; |
| 39 | |
| 40 | static inline void rt_timer_w32(struct rt_timer *rt, u8 reg, u32 val) |
| 41 | { |
| 42 | __raw_writel(val, rt->membase + reg); |
| 43 | } |
| 44 | |
| 45 | static inline u32 rt_timer_r32(struct rt_timer *rt, u8 reg) |
| 46 | { |
| 47 | return __raw_readl(rt->membase + reg); |
| 48 | } |
| 49 | |
| 50 | static irqreturn_t rt_timer_irq(int irq, void *_rt) |
| 51 | { |
| 52 | struct rt_timer *rt = (struct rt_timer *) _rt; |
| 53 | |
| 54 | rt_timer_w32(rt, TIMER_REG_TMR0LOAD, rt->timer_freq / rt->timer_div); |
| 55 | rt_timer_w32(rt, TIMER_REG_TMRSTAT, TMRSTAT_TMR0INT); |
| 56 | |
| 57 | return IRQ_HANDLED; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | static int rt_timer_request(struct rt_timer *rt) |
| 62 | { |
Michael Opdenacker | 31d6f57 | 2014-09-27 08:31:05 +0200 | [diff] [blame] | 63 | int err = request_irq(rt->irq, rt_timer_irq, 0, |
John Crispin | 80ecbd2 | 2013-08-08 19:25:15 +0200 | [diff] [blame] | 64 | dev_name(rt->dev), rt); |
| 65 | if (err) { |
| 66 | dev_err(rt->dev, "failed to request irq\n"); |
| 67 | } else { |
| 68 | u32 t = TMR0CTL_MODE_PERIODIC | TMR0CTL_PRESCALE_VAL; |
| 69 | rt_timer_w32(rt, TIMER_REG_TMR0CTL, t); |
| 70 | } |
| 71 | return err; |
| 72 | } |
| 73 | |
| 74 | static void rt_timer_free(struct rt_timer *rt) |
| 75 | { |
| 76 | free_irq(rt->irq, rt); |
| 77 | } |
| 78 | |
| 79 | static int rt_timer_config(struct rt_timer *rt, unsigned long divisor) |
| 80 | { |
| 81 | if (rt->timer_freq < divisor) |
| 82 | rt->timer_div = rt->timer_freq; |
| 83 | else |
| 84 | rt->timer_div = divisor; |
| 85 | |
| 86 | rt_timer_w32(rt, TIMER_REG_TMR0LOAD, rt->timer_freq / rt->timer_div); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static int rt_timer_enable(struct rt_timer *rt) |
| 92 | { |
| 93 | u32 t; |
| 94 | |
| 95 | rt_timer_w32(rt, TIMER_REG_TMR0LOAD, rt->timer_freq / rt->timer_div); |
| 96 | |
| 97 | t = rt_timer_r32(rt, TIMER_REG_TMR0CTL); |
| 98 | t |= TMR0CTL_ENABLE; |
| 99 | rt_timer_w32(rt, TIMER_REG_TMR0CTL, t); |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static void rt_timer_disable(struct rt_timer *rt) |
| 105 | { |
| 106 | u32 t; |
| 107 | |
| 108 | t = rt_timer_r32(rt, TIMER_REG_TMR0CTL); |
| 109 | t &= ~TMR0CTL_ENABLE; |
| 110 | rt_timer_w32(rt, TIMER_REG_TMR0CTL, t); |
| 111 | } |
| 112 | |
| 113 | static int rt_timer_probe(struct platform_device *pdev) |
| 114 | { |
| 115 | struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 116 | struct rt_timer *rt; |
| 117 | struct clk *clk; |
| 118 | |
| 119 | rt = devm_kzalloc(&pdev->dev, sizeof(*rt), GFP_KERNEL); |
| 120 | if (!rt) { |
| 121 | dev_err(&pdev->dev, "failed to allocate memory\n"); |
| 122 | return -ENOMEM; |
| 123 | } |
| 124 | |
| 125 | rt->irq = platform_get_irq(pdev, 0); |
| 126 | if (!rt->irq) { |
| 127 | dev_err(&pdev->dev, "failed to load irq\n"); |
| 128 | return -ENOENT; |
| 129 | } |
| 130 | |
Wei Yongjun | cd5d581 | 2013-10-31 15:51:38 +0800 | [diff] [blame] | 131 | rt->membase = devm_ioremap_resource(&pdev->dev, res); |
John Crispin | 80ecbd2 | 2013-08-08 19:25:15 +0200 | [diff] [blame] | 132 | if (IS_ERR(rt->membase)) |
| 133 | return PTR_ERR(rt->membase); |
| 134 | |
| 135 | clk = devm_clk_get(&pdev->dev, NULL); |
| 136 | if (IS_ERR(clk)) { |
| 137 | dev_err(&pdev->dev, "failed get clock rate\n"); |
| 138 | return PTR_ERR(clk); |
| 139 | } |
| 140 | |
| 141 | rt->timer_freq = clk_get_rate(clk) / TMR0CTL_PRESCALE_DIV; |
| 142 | if (!rt->timer_freq) |
| 143 | return -EINVAL; |
| 144 | |
| 145 | rt->dev = &pdev->dev; |
| 146 | platform_set_drvdata(pdev, rt); |
| 147 | |
| 148 | rt_timer_request(rt); |
| 149 | rt_timer_config(rt, 2); |
| 150 | rt_timer_enable(rt); |
| 151 | |
Masanari Iida | 77d84ff | 2013-12-09 00:22:53 +0900 | [diff] [blame] | 152 | dev_info(&pdev->dev, "maximum frequency is %luHz\n", rt->timer_freq); |
John Crispin | 80ecbd2 | 2013-08-08 19:25:15 +0200 | [diff] [blame] | 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
John Crispin | 80ecbd2 | 2013-08-08 19:25:15 +0200 | [diff] [blame] | 157 | static const struct of_device_id rt_timer_match[] = { |
| 158 | { .compatible = "ralink,rt2880-timer" }, |
| 159 | {}, |
| 160 | }; |
John Crispin | 80ecbd2 | 2013-08-08 19:25:15 +0200 | [diff] [blame] | 161 | |
| 162 | static struct platform_driver rt_timer_driver = { |
| 163 | .probe = rt_timer_probe, |
John Crispin | 80ecbd2 | 2013-08-08 19:25:15 +0200 | [diff] [blame] | 164 | .driver = { |
Paul Gortmaker | 62ee73d | 2016-08-15 16:30:53 -0400 | [diff] [blame^] | 165 | .name = "rt-timer", |
| 166 | .of_match_table = rt_timer_match, |
| 167 | .suppress_bind_attrs = true, |
John Crispin | 80ecbd2 | 2013-08-08 19:25:15 +0200 | [diff] [blame] | 168 | }, |
| 169 | }; |
Paul Gortmaker | 62ee73d | 2016-08-15 16:30:53 -0400 | [diff] [blame^] | 170 | builtin_platform_driver(rt_timer_driver); |