blob: 08420244c0585102d2892a2edc03921d95e99900 [file] [log] [blame]
Russell King3a083222011-11-05 17:38:32 +00001/*
2 * linux/arch/arm/mach-clps711x/core.c
3 *
4 * Core support for the CLPS711x-based machines.
5 *
6 * Copyright (C) 2001,2011 Deep Blue Solutions Ltd
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
Alexander Shiyan61ae48c2012-08-21 20:59:35 +040022#include <linux/io.h>
Russell King3a083222011-11-05 17:38:32 +000023#include <linux/init.h>
Alexander Shiyan4a8355c2012-10-10 19:45:27 +040024#include <linux/sizes.h>
Russell King3a083222011-11-05 17:38:32 +000025#include <linux/interrupt.h>
Russell King3a083222011-11-05 17:38:32 +000026#include <linux/irq.h>
Alexander Shiyan61ae48c2012-08-21 20:59:35 +040027#include <linux/clk.h>
28#include <linux/clkdev.h>
Alexander Shiyan4a8355c2012-10-10 19:45:27 +040029#include <linux/clockchips.h>
Alexander Shiyan61ae48c2012-08-21 20:59:35 +040030#include <linux/clk-provider.h>
Russell King3a083222011-11-05 17:38:32 +000031
Alexander Shiyan99f04c82012-11-17 17:57:14 +040032#include <asm/exception.h>
Russell King3a083222011-11-05 17:38:32 +000033#include <asm/mach/map.h>
34#include <asm/mach/time.h>
David Howells9f97da72012-03-28 18:30:01 +010035#include <asm/system_misc.h>
Russell King3a083222011-11-05 17:38:32 +000036
Alexander Shiyan61ae48c2012-08-21 20:59:35 +040037#include <mach/hardware.h>
38
39static struct clk *clk_pll, *clk_bus, *clk_uart, *clk_timerl, *clk_timerh,
40 *clk_tint, *clk_spi;
Alexander Shiyan61ae48c2012-08-21 20:59:35 +040041
Russell King3a083222011-11-05 17:38:32 +000042/*
43 * This maps the generic CLPS711x registers
44 */
45static struct map_desc clps711x_io_desc[] __initdata = {
46 {
Alexander Shiyan304b2c62012-05-06 09:21:57 +040047 .virtual = (unsigned long)CLPS711X_VIRT_BASE,
48 .pfn = __phys_to_pfn(CLPS711X_PHYS_BASE),
Alexander Shiyan6cb1b142012-10-10 19:45:31 +040049 .length = SZ_64K,
Russell King3a083222011-11-05 17:38:32 +000050 .type = MT_DEVICE
51 }
52};
53
54void __init clps711x_map_io(void)
55{
56 iotable_init(clps711x_io_desc, ARRAY_SIZE(clps711x_io_desc));
57}
58
59static void int1_mask(struct irq_data *d)
60{
61 u32 intmr1;
62
63 intmr1 = clps_readl(INTMR1);
64 intmr1 &= ~(1 << d->irq);
65 clps_writel(intmr1, INTMR1);
66}
67
68static void int1_ack(struct irq_data *d)
69{
Alexander Shiyan74fde6d2012-10-10 19:45:29 +040070}
71
72static void int1_eoi(struct irq_data *d)
73{
Russell King3a083222011-11-05 17:38:32 +000074 switch (d->irq) {
75 case IRQ_CSINT: clps_writel(0, COEOI); break;
76 case IRQ_TC1OI: clps_writel(0, TC1EOI); break;
77 case IRQ_TC2OI: clps_writel(0, TC2EOI); break;
78 case IRQ_RTCMI: clps_writel(0, RTCEOI); break;
79 case IRQ_TINT: clps_writel(0, TEOI); break;
80 case IRQ_UMSINT: clps_writel(0, UMSEOI); break;
81 }
82}
83
84static void int1_unmask(struct irq_data *d)
85{
86 u32 intmr1;
87
88 intmr1 = clps_readl(INTMR1);
89 intmr1 |= 1 << d->irq;
90 clps_writel(intmr1, INTMR1);
91}
92
93static struct irq_chip int1_chip = {
Alexander Shiyan74fde6d2012-10-10 19:45:29 +040094 .name = "Interrupt Vector 1 ",
Russell King3a083222011-11-05 17:38:32 +000095 .irq_ack = int1_ack,
Alexander Shiyan74fde6d2012-10-10 19:45:29 +040096 .irq_eoi = int1_eoi,
Russell King3a083222011-11-05 17:38:32 +000097 .irq_mask = int1_mask,
98 .irq_unmask = int1_unmask,
99};
100
101static void int2_mask(struct irq_data *d)
102{
103 u32 intmr2;
104
105 intmr2 = clps_readl(INTMR2);
106 intmr2 &= ~(1 << (d->irq - 16));
107 clps_writel(intmr2, INTMR2);
108}
109
110static void int2_ack(struct irq_data *d)
111{
Alexander Shiyan74fde6d2012-10-10 19:45:29 +0400112}
113
114static void int2_eoi(struct irq_data *d)
115{
Russell King3a083222011-11-05 17:38:32 +0000116 switch (d->irq) {
117 case IRQ_KBDINT: clps_writel(0, KBDEOI); break;
118 }
119}
120
121static void int2_unmask(struct irq_data *d)
122{
123 u32 intmr2;
124
125 intmr2 = clps_readl(INTMR2);
126 intmr2 |= 1 << (d->irq - 16);
127 clps_writel(intmr2, INTMR2);
128}
129
130static struct irq_chip int2_chip = {
Alexander Shiyan74fde6d2012-10-10 19:45:29 +0400131 .name = "Interrupt Vector 2 ",
Russell King3a083222011-11-05 17:38:32 +0000132 .irq_ack = int2_ack,
Alexander Shiyan74fde6d2012-10-10 19:45:29 +0400133 .irq_eoi = int2_eoi,
Russell King3a083222011-11-05 17:38:32 +0000134 .irq_mask = int2_mask,
135 .irq_unmask = int2_unmask,
136};
137
Alexander Shiyan99f04c82012-11-17 17:57:14 +0400138static struct {
Alexander Shiyan74fde6d2012-10-10 19:45:29 +0400139 int nr;
140 struct irq_chip *chip;
141 irq_flow_handler_t handle;
Alexander Shiyan99f04c82012-11-17 17:57:14 +0400142} clps711x_irqdescs[] __initdata = {
Alexander Shiyan74fde6d2012-10-10 19:45:29 +0400143 { IRQ_CSINT, &int1_chip, handle_fasteoi_irq, },
144 { IRQ_EINT1, &int1_chip, handle_level_irq, },
145 { IRQ_EINT2, &int1_chip, handle_level_irq, },
146 { IRQ_EINT3, &int1_chip, handle_level_irq, },
147 { IRQ_TC1OI, &int1_chip, handle_fasteoi_irq, },
148 { IRQ_TC2OI, &int1_chip, handle_fasteoi_irq, },
149 { IRQ_RTCMI, &int1_chip, handle_fasteoi_irq, },
150 { IRQ_TINT, &int1_chip, handle_fasteoi_irq, },
151 { IRQ_UTXINT1, &int1_chip, handle_level_irq, },
152 { IRQ_URXINT1, &int1_chip, handle_level_irq, },
153 { IRQ_UMSINT, &int1_chip, handle_fasteoi_irq, },
154 { IRQ_SSEOTI, &int1_chip, handle_level_irq, },
155 { IRQ_KBDINT, &int2_chip, handle_fasteoi_irq, },
156 { IRQ_SS2RX, &int2_chip, handle_level_irq, },
157 { IRQ_SS2TX, &int2_chip, handle_level_irq, },
158 { IRQ_UTXINT2, &int2_chip, handle_level_irq, },
159 { IRQ_URXINT2, &int2_chip, handle_level_irq, },
160};
161
Russell King3a083222011-11-05 17:38:32 +0000162void __init clps711x_init_irq(void)
163{
164 unsigned int i;
165
Alexander Shiyan74fde6d2012-10-10 19:45:29 +0400166 /* Disable interrupts */
Russell King3a083222011-11-05 17:38:32 +0000167 clps_writel(0, INTMR1);
168 clps_writel(0, INTMR2);
Alexander Shiyan74fde6d2012-10-10 19:45:29 +0400169 clps_writel(0, INTMR3);
Russell King3a083222011-11-05 17:38:32 +0000170
Alexander Shiyan74fde6d2012-10-10 19:45:29 +0400171 /* Clear down any pending interrupts */
172 clps_writel(0, BLEOI);
173 clps_writel(0, MCEOI);
Russell King3a083222011-11-05 17:38:32 +0000174 clps_writel(0, COEOI);
175 clps_writel(0, TC1EOI);
176 clps_writel(0, TC2EOI);
177 clps_writel(0, RTCEOI);
178 clps_writel(0, TEOI);
179 clps_writel(0, UMSEOI);
Russell King3a083222011-11-05 17:38:32 +0000180 clps_writel(0, KBDEOI);
Alexander Shiyan74fde6d2012-10-10 19:45:29 +0400181 clps_writel(0, SRXEOF);
182 clps_writel(0xffffffff, DAISR);
183
184 for (i = 0; i < ARRAY_SIZE(clps711x_irqdescs); i++) {
185 irq_set_chip_and_handler(clps711x_irqdescs[i].nr,
186 clps711x_irqdescs[i].chip,
187 clps711x_irqdescs[i].handle);
188 set_irq_flags(clps711x_irqdescs[i].nr,
189 IRQF_VALID | IRQF_PROBE);
190 }
Russell King3a083222011-11-05 17:38:32 +0000191}
192
Alexander Shiyan99f04c82012-11-17 17:57:14 +0400193inline u32 fls16(u32 x)
194{
195 u32 r = 15;
196
197 if (!(x & 0xff00)) {
198 x <<= 8;
199 r -= 8;
200 }
201 if (!(x & 0xf000)) {
202 x <<= 4;
203 r -= 4;
204 }
205 if (!(x & 0xc000)) {
206 x <<= 2;
207 r -= 2;
208 }
209 if (!(x & 0x8000))
210 r--;
211
212 return r;
213}
214
215asmlinkage void __exception_irq_entry clps711x_handle_irq(struct pt_regs *regs)
216{
217 u32 irqstat;
218 void __iomem *base = CLPS711X_VIRT_BASE;
219
220 irqstat = readl_relaxed(base + INTSR1) & readl_relaxed(base + INTMR1);
221 if (irqstat) {
222 handle_IRQ(fls16(irqstat), regs);
223 return;
224 }
225
226 irqstat = readl_relaxed(base + INTSR2) & readl_relaxed(base + INTMR2);
227 if (likely(irqstat))
228 handle_IRQ(fls16(irqstat) + 16, regs);
229}
230
Alexander Shiyan4a8355c2012-10-10 19:45:27 +0400231static void clps711x_clockevent_set_mode(enum clock_event_mode mode,
232 struct clock_event_device *evt)
Russell King3a083222011-11-05 17:38:32 +0000233{
Russell King3a083222011-11-05 17:38:32 +0000234}
235
Alexander Shiyan4a8355c2012-10-10 19:45:27 +0400236static struct clock_event_device clockevent_clps711x = {
237 .name = "CLPS711x Clockevents",
238 .rating = 300,
239 .features = CLOCK_EVT_FEAT_PERIODIC,
240 .set_mode = clps711x_clockevent_set_mode,
241};
242
243static irqreturn_t clps711x_timer_interrupt(int irq, void *dev_id)
Russell King3a083222011-11-05 17:38:32 +0000244{
Alexander Shiyan4a8355c2012-10-10 19:45:27 +0400245 clockevent_clps711x.event_handler(&clockevent_clps711x);
246
Russell King3a083222011-11-05 17:38:32 +0000247 return IRQ_HANDLED;
248}
249
250static struct irqaction clps711x_timer_irq = {
251 .name = "CLPS711x Timer Tick",
252 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
Alexander Shiyan4a8355c2012-10-10 19:45:27 +0400253 .handler = clps711x_timer_interrupt,
Russell King3a083222011-11-05 17:38:32 +0000254};
255
Alexander Shiyan61ae48c2012-08-21 20:59:35 +0400256static void add_fixed_clk(struct clk *clk, const char *name, int rate)
257{
258 clk = clk_register_fixed_rate(NULL, name, NULL, CLK_IS_ROOT, rate);
259 clk_register_clkdev(clk, name, NULL);
260}
261
Russell King3a083222011-11-05 17:38:32 +0000262static void __init clps711x_timer_init(void)
263{
Alexander Shiyan61ae48c2012-08-21 20:59:35 +0400264 int osc, ext, pll, cpu, bus, timl, timh, uart, spi;
265 u32 tmp;
Russell King3a083222011-11-05 17:38:32 +0000266
Alexander Shiyan61ae48c2012-08-21 20:59:35 +0400267 osc = 3686400;
268 ext = 13000000;
Russell King3a083222011-11-05 17:38:32 +0000269
Alexander Shiyan61ae48c2012-08-21 20:59:35 +0400270 tmp = clps_readl(PLLR) >> 24;
271 if (tmp)
272 pll = (osc * tmp) / 2;
273 else
274 pll = 73728000; /* Default value */
275
276 tmp = clps_readl(SYSFLG2);
277 if (tmp & SYSFLG2_CKMODE) {
278 cpu = ext;
279 bus = cpu;
280 spi = 135400;
281 } else {
282 cpu = pll;
283 if (cpu >= 36864000)
284 bus = cpu / 2;
285 else
286 bus = 36864000 / 2;
287 spi = cpu / 576;
288 }
289
290 uart = bus / 10;
291
292 if (tmp & SYSFLG2_CKMODE) {
293 tmp = clps_readl(SYSCON2);
294 if (tmp & SYSCON2_OSTB)
295 timh = ext / 26;
296 else
297 timh = 541440;
298 } else
299 timh = cpu / 144;
300
301 timl = timh / 256;
302
303 /* All clocks are fixed */
304 add_fixed_clk(clk_pll, "pll", pll);
305 add_fixed_clk(clk_bus, "bus", bus);
306 add_fixed_clk(clk_uart, "uart", uart);
307 add_fixed_clk(clk_timerl, "timer_lf", timl);
308 add_fixed_clk(clk_timerh, "timer_hf", timh);
309 add_fixed_clk(clk_tint, "tint", 64);
310 add_fixed_clk(clk_spi, "spi", spi);
311
312 pr_info("CPU frequency set at %i Hz.\n", cpu);
313
Alexander Shiyan4a8355c2012-10-10 19:45:27 +0400314 clps_writew(DIV_ROUND_CLOSEST(timh, HZ), TC2D);
Alexander Shiyan61ae48c2012-08-21 20:59:35 +0400315
316 tmp = clps_readl(SYSCON1);
317 tmp |= SYSCON1_TC2S | SYSCON1_TC2M;
318 clps_writel(tmp, SYSCON1);
319
Alexander Shiyan4a8355c2012-10-10 19:45:27 +0400320 clockevents_config_and_register(&clockevent_clps711x, timh, 1, 0xffff);
Russell King3a083222011-11-05 17:38:32 +0000321
322 setup_irq(IRQ_TC2OI, &clps711x_timer_irq);
Russell King3a083222011-11-05 17:38:32 +0000323}
324
325struct sys_timer clps711x_timer = {
326 .init = clps711x_timer_init,
Russell King3a083222011-11-05 17:38:32 +0000327};
Russell King6c000712011-11-05 17:41:52 +0000328
329void clps711x_restart(char mode, const char *cmd)
330{
331 soft_restart(0);
332}
Nicolas Pitre71e256c2011-08-02 12:22:48 -0400333
334static void clps711x_idle(void)
335{
336 clps_writel(1, HALT);
337 __asm__ __volatile__(
338 "mov r0, r0\n\
339 mov r0, r0");
340}
341
342static int __init clps711x_idle_init(void)
343{
344 arm_pm_idle = clps711x_idle;
345 return 0;
346}
347
348arch_initcall(clps711x_idle_init);