ade25bfb0d99ba03069b76b4e3887adb479f57a6
[openwrt/svn-archive/archive.git] / target / linux / lantiq / patches-3.2 / 0050-MIPS-adds-gptu-driver.patch
1 From ce8fccecad845349cc5f6783b3812a17a074d39c 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 50/73] MIPS: adds gptu driver
5
6 ---
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
10
11 diff --git a/arch/mips/lantiq/xway/gptu.c b/arch/mips/lantiq/xway/gptu.c
12 new file mode 100644
13 index 0000000..ac82c37
14 --- /dev/null
15 +++ b/arch/mips/lantiq/xway/gptu.c
16 @@ -0,0 +1,176 @@
17 +/*
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.
21 + *
22 + * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
23 + */
24 +
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>
33 +
34 +#include <lantiq_soc.h>
35 +#include "../clk.h"
36 +
37 +#include "../devices.h"
38 +
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))
41 +
42 +
43 +/* the magic ID byte of the core */
44 +#define GPTU_MAGIC 0x59
45 +/* clock control register */
46 +#define GPTU_CLC 0x00
47 +/* id register */
48 +#define GPTU_ID 0x08
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)
66 +
67 +/* GPTU_CON(x) */
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)
72 +
73 +/* GPTU_RUN(x) */
74 +#define RUN_SEN BIT(0)
75 +#define RUN_RL BIT(2)
76 +
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)
83 +
84 +#define TIMER_INTERRUPT (INT_NUM_IM3_IRL0 + 22)
85 +
86 +enum gptu_timer {
87 + TIMER1A = 0,
88 + TIMER1B,
89 + TIMER2A,
90 + TIMER2B,
91 + TIMER3A,
92 + TIMER3B
93 +};
94 +
95 +static struct resource ltq_gptu_resource =
96 + MEM_RES("GPTU", LTQ_GPTU_BASE_ADDR, LTQ_GPTU_SIZE);
97 +
98 +static void __iomem *ltq_gptu_membase;
99 +
100 +static irqreturn_t timer_irq_handler(int irq, void *priv)
101 +{
102 + int timer = irq - TIMER_INTERRUPT;
103 + ltq_gptu_w32(1 << timer, GPTU_IRNCR);
104 + return IRQ_HANDLED;
105 +}
106 +
107 +static void gptu_hwinit(void)
108 +{
109 + struct clk *clk = clk_get_sys("ltq_gptu", NULL);
110 + clk_enable(clk);
111 + ltq_gptu_w32(0x00, GPTU_IRNEN);
112 + ltq_gptu_w32(0xff, GPTU_IRNCR);
113 + ltq_gptu_w32(CLC_RMC | CLC_SUSPEND, GPTU_CLC);
114 +}
115 +
116 +static void gptu_hwexit(void)
117 +{
118 + ltq_gptu_w32(0x00, GPTU_IRNEN);
119 + ltq_gptu_w32(0xff, GPTU_IRNCR);
120 + ltq_gptu_w32(CLC_DISABLE, GPTU_CLC);
121 +}
122 +
123 +static int ltq_gptu_enable(struct clk *clk)
124 +{
125 + int ret = request_irq(TIMER_INTERRUPT + clk->bits, timer_irq_handler,
126 + IRQF_TIMER, "timer", NULL);
127 + if (ret) {
128 + pr_err("gptu: failed to request irq\n");
129 + return ret;
130 + }
131 +
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));
137 + return 0;
138 +}
139 +
140 +static void ltq_gptu_disable(struct clk *clk)
141 +{
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);
147 +}
148 +
149 +static inline void clkdev_add_gptu(const char *con, unsigned int timer)
150 +{
151 + struct clk *clk = kzalloc(sizeof(struct clk), GFP_KERNEL);
152 +
153 + clk->cl.dev_id = "ltq_gptu";
154 + clk->cl.con_id = con;
155 + clk->cl.clk = clk;
156 + clk->enable = ltq_gptu_enable;
157 + clk->disable = ltq_gptu_disable;
158 + clk->bits = timer;
159 + clkdev_add(&clk->cl);
160 +}
161 +
162 +static int __init gptu_setup(void)
163 +{
164 + /* remap gptu register range */
165 + ltq_gptu_membase = ltq_remap_resource(&ltq_gptu_resource);
166 + if (!ltq_gptu_membase)
167 + panic("Failed to remap gptu memory");
168 +
169 + /* power up the core */
170 + gptu_hwinit();
171 +
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");
175 + gptu_hwexit();
176 + return -ENAVAIL;
177 + }
178 +
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);
186 +
187 + pr_info("gptu: 6 timers loaded\n");
188 +
189 + return 0;
190 +}
191 +
192 +arch_initcall(gptu_setup);
193 --
194 1.7.9.1
195