1 From ad817ad0a57d0fa6041071395d4cf0d2e9a44635 Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Sat, 19 Jun 2010 04:08:09 +0000
4 Subject: [PATCH] MIPS: JZ4740: Add timer support
6 Add support for the timer/counter unit on a JZ4740 SoC. This code is used
7 as a common base for the JZ4740 clocksource/clockevent implementation and
10 Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
11 Cc: linux-mips@linux-mips.org
12 Cc: linux-kernel@vger.kernel.org
13 Patchwork: https://patchwork.linux-mips.org/patch/1396/
14 Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
16 arch/mips/include/asm/mach-jz4740/timer.h | 22 +++++
17 arch/mips/jz4740/timer.c | 48 ++++++++++
18 arch/mips/jz4740/timer.h | 136 +++++++++++++++++++++++++++++
19 3 files changed, 206 insertions(+), 0 deletions(-)
20 create mode 100644 arch/mips/include/asm/mach-jz4740/timer.h
21 create mode 100644 arch/mips/jz4740/timer.c
22 create mode 100644 arch/mips/jz4740/timer.h
25 +++ b/arch/mips/include/asm/mach-jz4740/timer.h
28 + * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
29 + * JZ4740 platform timer support
31 + * This program is free software; you can redistribute it and/or modify it
32 + * under the terms of the GNU General Public License as published by the
33 + * Free Software Foundation; either version 2 of the License, or (at your
34 + * option) any later version.
36 + * You should have received a copy of the GNU General Public License along
37 + * with this program; if not, write to the Free Software Foundation, Inc.,
38 + * 675 Mass Ave, Cambridge, MA 02139, USA.
42 +#ifndef __ASM_MACH_JZ4740_TIMER
43 +#define __ASM_MACH_JZ4740_TIMER
45 +void jz4740_timer_enable_watchdog(void);
46 +void jz4740_timer_disable_watchdog(void);
50 +++ b/arch/mips/jz4740/timer.c
53 + * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
54 + * JZ4740 platform timer support
56 + * This program is free software; you can redistribute it and/or modify it
57 + * under the terms of the GNU General Public License as published by the
58 + * Free Software Foundation; either version 2 of the License, or (at your
59 + * option) any later version.
61 + * You should have received a copy of the GNU General Public License along
62 + * with this program; if not, write to the Free Software Foundation, Inc.,
63 + * 675 Mass Ave, Cambridge, MA 02139, USA.
67 +#include <linux/io.h>
68 +#include <linux/kernel.h>
69 +#include <linux/module.h>
73 +#include <asm/mach-jz4740/base.h>
75 +void __iomem *jz4740_timer_base;
77 +void jz4740_timer_enable_watchdog(void)
79 + writel(BIT(16), jz4740_timer_base + JZ_REG_TIMER_STOP_CLEAR);
82 +void jz4740_timer_disable_watchdog(void)
84 + writel(BIT(16), jz4740_timer_base + JZ_REG_TIMER_STOP_SET);
87 +void __init jz4740_timer_init(void)
89 + jz4740_timer_base = ioremap(JZ4740_TCU_BASE_ADDR, 0x100);
91 + if (!jz4740_timer_base)
92 + panic("Failed to ioremap timer registers");
94 + /* Disable all timer clocks except for those used as system timers */
95 + writel(0x000100fc, jz4740_timer_base + JZ_REG_TIMER_STOP_SET);
97 + /* Timer irqs are unmasked by default, mask them */
98 + writel(0x00ff00ff, jz4740_timer_base + JZ_REG_TIMER_MASK_SET);
101 +++ b/arch/mips/jz4740/timer.h
104 + * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
105 + * JZ4740 platform timer support
107 + * This program is free software; you can redistribute it and/or modify it
108 + * under the terms of the GNU General Public License as published by the
109 + * Free Software Foundation; either version 2 of the License, or (at your
110 + * option) any later version.
112 + * You should have received a copy of the GNU General Public License along
113 + * with this program; if not, write to the Free Software Foundation, Inc.,
114 + * 675 Mass Ave, Cambridge, MA 02139, USA.
118 +#ifndef __MIPS_JZ4740_TIMER_H__
119 +#define __MIPS_JZ4740_TIMER_H__
121 +#include <linux/module.h>
122 +#include <linux/io.h>
124 +#define JZ_REG_TIMER_STOP 0x0C
125 +#define JZ_REG_TIMER_STOP_SET 0x1C
126 +#define JZ_REG_TIMER_STOP_CLEAR 0x2C
127 +#define JZ_REG_TIMER_ENABLE 0x00
128 +#define JZ_REG_TIMER_ENABLE_SET 0x04
129 +#define JZ_REG_TIMER_ENABLE_CLEAR 0x08
130 +#define JZ_REG_TIMER_FLAG 0x10
131 +#define JZ_REG_TIMER_FLAG_SET 0x14
132 +#define JZ_REG_TIMER_FLAG_CLEAR 0x18
133 +#define JZ_REG_TIMER_MASK 0x20
134 +#define JZ_REG_TIMER_MASK_SET 0x24
135 +#define JZ_REG_TIMER_MASK_CLEAR 0x28
137 +#define JZ_REG_TIMER_DFR(x) (((x) * 0x10) + 0x30)
138 +#define JZ_REG_TIMER_DHR(x) (((x) * 0x10) + 0x34)
139 +#define JZ_REG_TIMER_CNT(x) (((x) * 0x10) + 0x38)
140 +#define JZ_REG_TIMER_CTRL(x) (((x) * 0x10) + 0x3C)
142 +#define JZ_TIMER_IRQ_HALF(x) BIT((x) + 0x10)
143 +#define JZ_TIMER_IRQ_FULL(x) BIT(x)
145 +#define JZ_TIMER_CTRL_PWM_ABBRUPT_SHUTDOWN BIT(9)
146 +#define JZ_TIMER_CTRL_PWM_ACTIVE_LOW BIT(8)
147 +#define JZ_TIMER_CTRL_PWM_ENABLE BIT(7)
148 +#define JZ_TIMER_CTRL_PRESCALE_MASK 0x1c
149 +#define JZ_TIMER_CTRL_PRESCALE_OFFSET 0x3
150 +#define JZ_TIMER_CTRL_PRESCALE_1 (0 << 3)
151 +#define JZ_TIMER_CTRL_PRESCALE_4 (1 << 3)
152 +#define JZ_TIMER_CTRL_PRESCALE_16 (2 << 3)
153 +#define JZ_TIMER_CTRL_PRESCALE_64 (3 << 3)
154 +#define JZ_TIMER_CTRL_PRESCALE_256 (4 << 3)
155 +#define JZ_TIMER_CTRL_PRESCALE_1024 (5 << 3)
157 +#define JZ_TIMER_CTRL_PRESCALER(x) ((x) << JZ_TIMER_CTRL_PRESCALE_OFFSET)
159 +#define JZ_TIMER_CTRL_SRC_EXT BIT(2)
160 +#define JZ_TIMER_CTRL_SRC_RTC BIT(1)
161 +#define JZ_TIMER_CTRL_SRC_PCLK BIT(0)
163 +extern void __iomem *jz4740_timer_base;
164 +void __init jz4740_timer_init(void);
166 +static inline void jz4740_timer_stop(unsigned int timer)
168 + writel(BIT(timer), jz4740_timer_base + JZ_REG_TIMER_STOP_SET);
171 +static inline void jz4740_timer_start(unsigned int timer)
173 + writel(BIT(timer), jz4740_timer_base + JZ_REG_TIMER_STOP_CLEAR);
176 +static inline bool jz4740_timer_is_enabled(unsigned int timer)
178 + return readb(jz4740_timer_base + JZ_REG_TIMER_ENABLE) & BIT(timer);
181 +static inline void jz4740_timer_enable(unsigned int timer)
183 + writeb(BIT(timer), jz4740_timer_base + JZ_REG_TIMER_ENABLE_SET);
186 +static inline void jz4740_timer_disable(unsigned int timer)
188 + writeb(BIT(timer), jz4740_timer_base + JZ_REG_TIMER_ENABLE_CLEAR);
192 +static inline void jz4740_timer_set_period(unsigned int timer, uint16_t period)
194 + writew(period, jz4740_timer_base + JZ_REG_TIMER_DFR(timer));
197 +static inline void jz4740_timer_set_duty(unsigned int timer, uint16_t duty)
199 + writew(duty, jz4740_timer_base + JZ_REG_TIMER_DHR(timer));
202 +static inline void jz4740_timer_set_count(unsigned int timer, uint16_t count)
204 + writew(count, jz4740_timer_base + JZ_REG_TIMER_CNT(timer));
207 +static inline uint16_t jz4740_timer_get_count(unsigned int timer)
209 + return readw(jz4740_timer_base + JZ_REG_TIMER_CNT(timer));
212 +static inline void jz4740_timer_ack_full(unsigned int timer)
214 + writel(JZ_TIMER_IRQ_FULL(timer), jz4740_timer_base + JZ_REG_TIMER_FLAG_CLEAR);
217 +static inline void jz4740_timer_irq_full_enable(unsigned int timer)
219 + writel(JZ_TIMER_IRQ_FULL(timer), jz4740_timer_base + JZ_REG_TIMER_FLAG_CLEAR);
220 + writel(JZ_TIMER_IRQ_FULL(timer), jz4740_timer_base + JZ_REG_TIMER_MASK_CLEAR);
223 +static inline void jz4740_timer_irq_full_disable(unsigned int timer)
225 + writel(JZ_TIMER_IRQ_FULL(timer), jz4740_timer_base + JZ_REG_TIMER_MASK_SET);
228 +static inline void jz4740_timer_set_ctrl(unsigned int timer, uint16_t ctrl)
230 + writew(ctrl, jz4740_timer_base + JZ_REG_TIMER_CTRL(timer));
233 +static inline uint16_t jz4740_timer_get_ctrl(unsigned int timer)
235 + return readw(jz4740_timer_base + JZ_REG_TIMER_CTRL(timer));