1 From a7c55f5e927b69bb30912fe1c3e5bcd8751e8381 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Wed, 14 Mar 2012 15:37:19 +0100
4 Subject: [PATCH 51/70] MIPS: adds gptu driver
7 arch/mips/lantiq/xway/gptu.c | 176 ++++++++++++++++++++++++++++++++++++++++++
8 1 files changed, 176 insertions(+), 0 deletions(-)
9 create mode 100644 arch/mips/lantiq/xway/gptu.c
11 diff --git a/arch/mips/lantiq/xway/gptu.c b/arch/mips/lantiq/xway/gptu.c
13 index 0000000..ac82c37
15 +++ b/arch/mips/lantiq/xway/gptu.c
18 + * This program is free software; you can redistribute it and/or modify it
19 + * under the terms of the GNU General Public License version 2 as published
20 + * by the Free Software Foundation.
22 + * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
25 +#include <linux/init.h>
26 +#include <linux/io.h>
27 +#include <linux/ioport.h>
28 +#include <linux/pm.h>
29 +#include <linux/export.h>
30 +#include <linux/delay.h>
31 +#include <linux/interrupt.h>
32 +#include <asm/reboot.h>
34 +#include <lantiq_soc.h>
37 +#include "../devices.h"
39 +#define ltq_gptu_w32(x, y) ltq_w32((x), ltq_gptu_membase + (y))
40 +#define ltq_gptu_r32(x) ltq_r32(ltq_gptu_membase + (x))
43 +/* the magic ID byte of the core */
44 +#define GPTU_MAGIC 0x59
45 +/* clock control register */
46 +#define GPTU_CLC 0x00
49 +/* interrupt node enable */
50 +#define GPTU_IRNEN 0xf4
51 +/* interrupt control register */
52 +#define GPTU_IRCR 0xf8
53 +/* interrupt capture register */
54 +#define GPTU_IRNCR 0xfc
55 +/* there are 3 identical blocks of 2 timers. calculate register offsets */
56 +#define GPTU_SHIFT(x) (x % 2 ? 4 : 0)
57 +#define GPTU_BASE(x) (((x >> 1) * 0x20) + 0x10)
58 +/* timer control register */
59 +#define GPTU_CON(x) (GPTU_BASE(x) + GPTU_SHIFT(x) + 0x00)
60 +/* timer auto reload register */
61 +#define GPTU_RUN(x) (GPTU_BASE(x) + GPTU_SHIFT(x) + 0x08)
62 +/* timer manual reload register */
63 +#define GPTU_RLD(x) (GPTU_BASE(x) + GPTU_SHIFT(x) + 0x10)
64 +/* timer count register */
65 +#define GPTU_CNT(x) (GPTU_BASE(x) + GPTU_SHIFT(x) + 0x18)
68 +#define CON_CNT BIT(2)
69 +#define CON_EDGE_FALL BIT(7)
70 +#define CON_SYNC BIT(8)
71 +#define CON_CLK_INT BIT(10)
74 +#define RUN_SEN BIT(0)
75 +#define RUN_RL BIT(2)
77 +/* set clock to runmode */
78 +#define CLC_RMC BIT(8)
79 +/* bring core out of suspend */
80 +#define CLC_SUSPEND BIT(4)
81 +/* the disable bit */
82 +#define CLC_DISABLE BIT(0)
84 +#define TIMER_INTERRUPT (INT_NUM_IM3_IRL0 + 22)
95 +static struct resource ltq_gptu_resource =
96 + MEM_RES("GPTU", LTQ_GPTU_BASE_ADDR, LTQ_GPTU_SIZE);
98 +static void __iomem *ltq_gptu_membase;
100 +static irqreturn_t timer_irq_handler(int irq, void *priv)
102 + int timer = irq - TIMER_INTERRUPT;
103 + ltq_gptu_w32(1 << timer, GPTU_IRNCR);
104 + return IRQ_HANDLED;
107 +static void gptu_hwinit(void)
109 + struct clk *clk = clk_get_sys("ltq_gptu", NULL);
111 + ltq_gptu_w32(0x00, GPTU_IRNEN);
112 + ltq_gptu_w32(0xff, GPTU_IRNCR);
113 + ltq_gptu_w32(CLC_RMC | CLC_SUSPEND, GPTU_CLC);
116 +static void gptu_hwexit(void)
118 + ltq_gptu_w32(0x00, GPTU_IRNEN);
119 + ltq_gptu_w32(0xff, GPTU_IRNCR);
120 + ltq_gptu_w32(CLC_DISABLE, GPTU_CLC);
123 +static int ltq_gptu_enable(struct clk *clk)
125 + int ret = request_irq(TIMER_INTERRUPT + clk->bits, timer_irq_handler,
126 + IRQF_TIMER, "timer", NULL);
128 + pr_err("gptu: failed to request irq\n");
132 + ltq_gptu_w32(CON_CNT | CON_EDGE_FALL | CON_SYNC | CON_CLK_INT,
133 + GPTU_CON(clk->bits));
134 + ltq_gptu_w32(1, GPTU_RLD(clk->bits));
135 + ltq_gptu_w32(ltq_gptu_r32(GPTU_IRNEN) | clk->bits, GPTU_IRNEN);
136 + ltq_gptu_w32(RUN_SEN | RUN_RL, GPTU_RUN(clk->bits));
140 +static void ltq_gptu_disable(struct clk *clk)
142 + ltq_gptu_w32(0, GPTU_RUN(clk->bits));
143 + ltq_gptu_w32(0, GPTU_CON(clk->bits));
144 + ltq_gptu_w32(0, GPTU_RLD(clk->bits));
145 + ltq_gptu_w32(ltq_gptu_r32(GPTU_IRNEN) & ~clk->bits, GPTU_IRNEN);
146 + free_irq(TIMER_INTERRUPT + clk->bits, NULL);
149 +static inline void clkdev_add_gptu(const char *con, unsigned int timer)
151 + struct clk *clk = kzalloc(sizeof(struct clk), GFP_KERNEL);
153 + clk->cl.dev_id = "ltq_gptu";
154 + clk->cl.con_id = con;
156 + clk->enable = ltq_gptu_enable;
157 + clk->disable = ltq_gptu_disable;
159 + clkdev_add(&clk->cl);
162 +static int __init gptu_setup(void)
164 + /* remap gptu register range */
165 + ltq_gptu_membase = ltq_remap_resource(<q_gptu_resource);
166 + if (!ltq_gptu_membase)
167 + panic("Failed to remap gptu memory");
169 + /* power up the core */
172 + /* the gptu has a ID register */
173 + if (((ltq_gptu_r32(GPTU_ID) >> 8) & 0xff) != GPTU_MAGIC) {
174 + pr_err("gptu: failed to find magic\n");
179 + /* register the clocks */
180 + clkdev_add_gptu("timer1a", TIMER1A);
181 + clkdev_add_gptu("timer1b", TIMER1B);
182 + clkdev_add_gptu("timer2a", TIMER2A);
183 + clkdev_add_gptu("timer2b", TIMER2B);
184 + clkdev_add_gptu("timer3a", TIMER3A);
185 + clkdev_add_gptu("timer3b", TIMER3B);
187 + pr_info("gptu: 6 timers loaded\n");
192 +arch_initcall(gptu_setup);