180ecbd24SJohn Crispin /* 262ee73d2SPaul Gortmaker * Ralink RT2880 timer 362ee73d2SPaul Gortmaker * Author: John Crispin 462ee73d2SPaul Gortmaker * 580ecbd24SJohn Crispin * This program is free software; you can redistribute it and/or modify it 680ecbd24SJohn Crispin * under the terms of the GNU General Public License version 2 as published 780ecbd24SJohn Crispin * by the Free Software Foundation. 880ecbd24SJohn Crispin * 997b92108SJohn Crispin * Copyright (C) 2013 John Crispin <john@phrozen.org> 1080ecbd24SJohn Crispin */ 1180ecbd24SJohn Crispin 1280ecbd24SJohn Crispin #include <linux/platform_device.h> 1380ecbd24SJohn Crispin #include <linux/interrupt.h> 1480ecbd24SJohn Crispin #include <linux/timer.h> 1580ecbd24SJohn Crispin #include <linux/of_gpio.h> 1680ecbd24SJohn Crispin #include <linux/clk.h> 1780ecbd24SJohn Crispin 1880ecbd24SJohn Crispin #include <asm/mach-ralink/ralink_regs.h> 1980ecbd24SJohn Crispin 2080ecbd24SJohn Crispin #define TIMER_REG_TMRSTAT 0x00 2180ecbd24SJohn Crispin #define TIMER_REG_TMR0LOAD 0x10 2280ecbd24SJohn Crispin #define TIMER_REG_TMR0CTL 0x18 2380ecbd24SJohn Crispin 2480ecbd24SJohn Crispin #define TMRSTAT_TMR0INT BIT(0) 2580ecbd24SJohn Crispin 2680ecbd24SJohn Crispin #define TMR0CTL_ENABLE BIT(7) 2780ecbd24SJohn Crispin #define TMR0CTL_MODE_PERIODIC BIT(4) 2880ecbd24SJohn Crispin #define TMR0CTL_PRESCALER 1 2980ecbd24SJohn Crispin #define TMR0CTL_PRESCALE_VAL (0xf - TMR0CTL_PRESCALER) 3080ecbd24SJohn Crispin #define TMR0CTL_PRESCALE_DIV (65536 / BIT(TMR0CTL_PRESCALER)) 3180ecbd24SJohn Crispin 3280ecbd24SJohn Crispin struct rt_timer { 3380ecbd24SJohn Crispin struct device *dev; 3480ecbd24SJohn Crispin void __iomem *membase; 3580ecbd24SJohn Crispin int irq; 3680ecbd24SJohn Crispin unsigned long timer_freq; 3780ecbd24SJohn Crispin unsigned long timer_div; 3880ecbd24SJohn Crispin }; 3980ecbd24SJohn Crispin 4080ecbd24SJohn Crispin static inline void rt_timer_w32(struct rt_timer *rt, u8 reg, u32 val) 4180ecbd24SJohn Crispin { 4280ecbd24SJohn Crispin __raw_writel(val, rt->membase + reg); 4380ecbd24SJohn Crispin } 4480ecbd24SJohn Crispin 4580ecbd24SJohn Crispin static inline u32 rt_timer_r32(struct rt_timer *rt, u8 reg) 4680ecbd24SJohn Crispin { 4780ecbd24SJohn Crispin return __raw_readl(rt->membase + reg); 4880ecbd24SJohn Crispin } 4980ecbd24SJohn Crispin 5080ecbd24SJohn Crispin static irqreturn_t rt_timer_irq(int irq, void *_rt) 5180ecbd24SJohn Crispin { 5280ecbd24SJohn Crispin struct rt_timer *rt = (struct rt_timer *) _rt; 5380ecbd24SJohn Crispin 5480ecbd24SJohn Crispin rt_timer_w32(rt, TIMER_REG_TMR0LOAD, rt->timer_freq / rt->timer_div); 5580ecbd24SJohn Crispin rt_timer_w32(rt, TIMER_REG_TMRSTAT, TMRSTAT_TMR0INT); 5680ecbd24SJohn Crispin 5780ecbd24SJohn Crispin return IRQ_HANDLED; 5880ecbd24SJohn Crispin } 5980ecbd24SJohn Crispin 6080ecbd24SJohn Crispin 6180ecbd24SJohn Crispin static int rt_timer_request(struct rt_timer *rt) 6280ecbd24SJohn Crispin { 6331d6f57dSMichael Opdenacker int err = request_irq(rt->irq, rt_timer_irq, 0, 6480ecbd24SJohn Crispin dev_name(rt->dev), rt); 6580ecbd24SJohn Crispin if (err) { 6680ecbd24SJohn Crispin dev_err(rt->dev, "failed to request irq\n"); 6780ecbd24SJohn Crispin } else { 6880ecbd24SJohn Crispin u32 t = TMR0CTL_MODE_PERIODIC | TMR0CTL_PRESCALE_VAL; 6980ecbd24SJohn Crispin rt_timer_w32(rt, TIMER_REG_TMR0CTL, t); 7080ecbd24SJohn Crispin } 7180ecbd24SJohn Crispin return err; 7280ecbd24SJohn Crispin } 7380ecbd24SJohn Crispin 7480ecbd24SJohn Crispin static int rt_timer_config(struct rt_timer *rt, unsigned long divisor) 7580ecbd24SJohn Crispin { 7680ecbd24SJohn Crispin if (rt->timer_freq < divisor) 7780ecbd24SJohn Crispin rt->timer_div = rt->timer_freq; 7880ecbd24SJohn Crispin else 7980ecbd24SJohn Crispin rt->timer_div = divisor; 8080ecbd24SJohn Crispin 8180ecbd24SJohn Crispin rt_timer_w32(rt, TIMER_REG_TMR0LOAD, rt->timer_freq / rt->timer_div); 8280ecbd24SJohn Crispin 8380ecbd24SJohn Crispin return 0; 8480ecbd24SJohn Crispin } 8580ecbd24SJohn Crispin 8680ecbd24SJohn Crispin static int rt_timer_enable(struct rt_timer *rt) 8780ecbd24SJohn Crispin { 8880ecbd24SJohn Crispin u32 t; 8980ecbd24SJohn Crispin 9080ecbd24SJohn Crispin rt_timer_w32(rt, TIMER_REG_TMR0LOAD, rt->timer_freq / rt->timer_div); 9180ecbd24SJohn Crispin 9280ecbd24SJohn Crispin t = rt_timer_r32(rt, TIMER_REG_TMR0CTL); 9380ecbd24SJohn Crispin t |= TMR0CTL_ENABLE; 9480ecbd24SJohn Crispin rt_timer_w32(rt, TIMER_REG_TMR0CTL, t); 9580ecbd24SJohn Crispin 9680ecbd24SJohn Crispin return 0; 9780ecbd24SJohn Crispin } 9880ecbd24SJohn Crispin 9980ecbd24SJohn Crispin static int rt_timer_probe(struct platform_device *pdev) 10080ecbd24SJohn Crispin { 10180ecbd24SJohn Crispin struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 10280ecbd24SJohn Crispin struct rt_timer *rt; 10380ecbd24SJohn Crispin struct clk *clk; 10480ecbd24SJohn Crispin 10580ecbd24SJohn Crispin rt = devm_kzalloc(&pdev->dev, sizeof(*rt), GFP_KERNEL); 10680ecbd24SJohn Crispin if (!rt) { 10780ecbd24SJohn Crispin dev_err(&pdev->dev, "failed to allocate memory\n"); 10880ecbd24SJohn Crispin return -ENOMEM; 10980ecbd24SJohn Crispin } 11080ecbd24SJohn Crispin 11180ecbd24SJohn Crispin rt->irq = platform_get_irq(pdev, 0); 112*b6ab1a13SArvind Yadav if (rt->irq < 0) { 11380ecbd24SJohn Crispin dev_err(&pdev->dev, "failed to load irq\n"); 114*b6ab1a13SArvind Yadav return rt->irq; 11580ecbd24SJohn Crispin } 11680ecbd24SJohn Crispin 117cd5d5810SWei Yongjun rt->membase = devm_ioremap_resource(&pdev->dev, res); 11880ecbd24SJohn Crispin if (IS_ERR(rt->membase)) 11980ecbd24SJohn Crispin return PTR_ERR(rt->membase); 12080ecbd24SJohn Crispin 12180ecbd24SJohn Crispin clk = devm_clk_get(&pdev->dev, NULL); 12280ecbd24SJohn Crispin if (IS_ERR(clk)) { 12380ecbd24SJohn Crispin dev_err(&pdev->dev, "failed get clock rate\n"); 12480ecbd24SJohn Crispin return PTR_ERR(clk); 12580ecbd24SJohn Crispin } 12680ecbd24SJohn Crispin 12780ecbd24SJohn Crispin rt->timer_freq = clk_get_rate(clk) / TMR0CTL_PRESCALE_DIV; 12880ecbd24SJohn Crispin if (!rt->timer_freq) 12980ecbd24SJohn Crispin return -EINVAL; 13080ecbd24SJohn Crispin 13180ecbd24SJohn Crispin rt->dev = &pdev->dev; 13280ecbd24SJohn Crispin platform_set_drvdata(pdev, rt); 13380ecbd24SJohn Crispin 13480ecbd24SJohn Crispin rt_timer_request(rt); 13580ecbd24SJohn Crispin rt_timer_config(rt, 2); 13680ecbd24SJohn Crispin rt_timer_enable(rt); 13780ecbd24SJohn Crispin 13877d84ff8SMasanari Iida dev_info(&pdev->dev, "maximum frequency is %luHz\n", rt->timer_freq); 13980ecbd24SJohn Crispin 14080ecbd24SJohn Crispin return 0; 14180ecbd24SJohn Crispin } 14280ecbd24SJohn Crispin 14380ecbd24SJohn Crispin static const struct of_device_id rt_timer_match[] = { 14480ecbd24SJohn Crispin { .compatible = "ralink,rt2880-timer" }, 14580ecbd24SJohn Crispin {}, 14680ecbd24SJohn Crispin }; 14780ecbd24SJohn Crispin 14880ecbd24SJohn Crispin static struct platform_driver rt_timer_driver = { 14980ecbd24SJohn Crispin .probe = rt_timer_probe, 15080ecbd24SJohn Crispin .driver = { 15180ecbd24SJohn Crispin .name = "rt-timer", 15262ee73d2SPaul Gortmaker .of_match_table = rt_timer_match, 15362ee73d2SPaul Gortmaker .suppress_bind_attrs = true, 15480ecbd24SJohn Crispin }, 15580ecbd24SJohn Crispin }; 15662ee73d2SPaul Gortmaker builtin_platform_driver(rt_timer_driver); 157