blob: aee81fa46ccfc4454e5ed9d1082a853965100c97 [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 Shiyanc99f72a2013-05-13 21:07:32 +040030#include <linux/clocksource.h>
Alexander Shiyan61ae48c2012-08-21 20:59:35 +040031#include <linux/clk-provider.h>
Stephen Boyd5bd8e162013-10-03 12:49:42 -070032#include <linux/sched_clock.h>
Russell King3a083222011-11-05 17:38:32 +000033
Russell King3a083222011-11-05 17:38:32 +000034#include <asm/mach/map.h>
35#include <asm/mach/time.h>
David Howells9f97da72012-03-28 18:30:01 +010036#include <asm/system_misc.h>
Russell King3a083222011-11-05 17:38:32 +000037
Alexander Shiyan61ae48c2012-08-21 20:59:35 +040038#include <mach/hardware.h>
39
Alexander Shiyan6c41a992014-02-02 12:09:01 +040040#include "common.h"
41
Alexander Shiyan61ae48c2012-08-21 20:59:35 +040042static struct clk *clk_pll, *clk_bus, *clk_uart, *clk_timerl, *clk_timerh,
43 *clk_tint, *clk_spi;
Alexander Shiyan61ae48c2012-08-21 20:59:35 +040044
Russell King3a083222011-11-05 17:38:32 +000045/*
46 * This maps the generic CLPS711x registers
47 */
48static struct map_desc clps711x_io_desc[] __initdata = {
49 {
Alexander Shiyan304b2c62012-05-06 09:21:57 +040050 .virtual = (unsigned long)CLPS711X_VIRT_BASE,
51 .pfn = __phys_to_pfn(CLPS711X_PHYS_BASE),
Alexander Shiyan6cb1b142012-10-10 19:45:31 +040052 .length = SZ_64K,
Russell King3a083222011-11-05 17:38:32 +000053 .type = MT_DEVICE
54 }
55};
56
57void __init clps711x_map_io(void)
58{
59 iotable_init(clps711x_io_desc, ARRAY_SIZE(clps711x_io_desc));
60}
61
Russell King3a083222011-11-05 17:38:32 +000062void __init clps711x_init_irq(void)
63{
Alexander Shiyan6c41a992014-02-02 12:09:01 +040064 clps711x_intc_init(CLPS711X_PHYS_BASE, SZ_16K);
Alexander Shiyan99f04c82012-11-17 17:57:14 +040065}
66
Stephen Boyd161f4082013-11-15 15:26:10 -080067static u64 notrace clps711x_sched_clock_read(void)
Alexander Shiyanc99f72a2013-05-13 21:07:32 +040068{
69 return ~readw_relaxed(CLPS711X_VIRT_BASE + TC1D);
70}
71
Alexander Shiyan4a8355c2012-10-10 19:45:27 +040072static void clps711x_clockevent_set_mode(enum clock_event_mode mode,
73 struct clock_event_device *evt)
Russell King3a083222011-11-05 17:38:32 +000074{
Alexander Shiyanc99f72a2013-05-13 21:07:32 +040075 disable_irq(IRQ_TC2OI);
76
77 switch (mode) {
78 case CLOCK_EVT_MODE_PERIODIC:
79 enable_irq(IRQ_TC2OI);
80 break;
81 case CLOCK_EVT_MODE_ONESHOT:
82 /* Not supported */
83 case CLOCK_EVT_MODE_SHUTDOWN:
84 case CLOCK_EVT_MODE_UNUSED:
85 case CLOCK_EVT_MODE_RESUME:
86 /* Left event sources disabled, no more interrupts appear */
87 break;
88 }
Russell King3a083222011-11-05 17:38:32 +000089}
90
Alexander Shiyan4a8355c2012-10-10 19:45:27 +040091static struct clock_event_device clockevent_clps711x = {
Alexander Shiyanc99f72a2013-05-13 21:07:32 +040092 .name = "clps711x-clockevent",
Alexander Shiyan4a8355c2012-10-10 19:45:27 +040093 .rating = 300,
94 .features = CLOCK_EVT_FEAT_PERIODIC,
95 .set_mode = clps711x_clockevent_set_mode,
96};
97
98static irqreturn_t clps711x_timer_interrupt(int irq, void *dev_id)
Russell King3a083222011-11-05 17:38:32 +000099{
Alexander Shiyan4a8355c2012-10-10 19:45:27 +0400100 clockevent_clps711x.event_handler(&clockevent_clps711x);
101
Russell King3a083222011-11-05 17:38:32 +0000102 return IRQ_HANDLED;
103}
104
105static struct irqaction clps711x_timer_irq = {
Alexander Shiyanc99f72a2013-05-13 21:07:32 +0400106 .name = "clps711x-timer",
107 .flags = IRQF_TIMER | IRQF_IRQPOLL,
Alexander Shiyan4a8355c2012-10-10 19:45:27 +0400108 .handler = clps711x_timer_interrupt,
Russell King3a083222011-11-05 17:38:32 +0000109};
110
Alexander Shiyan61ae48c2012-08-21 20:59:35 +0400111static void add_fixed_clk(struct clk *clk, const char *name, int rate)
112{
113 clk = clk_register_fixed_rate(NULL, name, NULL, CLK_IS_ROOT, rate);
114 clk_register_clkdev(clk, name, NULL);
115}
116
Stephen Warren6bb27d72012-11-08 12:40:59 -0700117void __init clps711x_timer_init(void)
Russell King3a083222011-11-05 17:38:32 +0000118{
Alexander Shiyan61ae48c2012-08-21 20:59:35 +0400119 int osc, ext, pll, cpu, bus, timl, timh, uart, spi;
120 u32 tmp;
Russell King3a083222011-11-05 17:38:32 +0000121
Alexander Shiyan61ae48c2012-08-21 20:59:35 +0400122 osc = 3686400;
123 ext = 13000000;
Russell King3a083222011-11-05 17:38:32 +0000124
Alexander Shiyan61ae48c2012-08-21 20:59:35 +0400125 tmp = clps_readl(PLLR) >> 24;
126 if (tmp)
127 pll = (osc * tmp) / 2;
128 else
129 pll = 73728000; /* Default value */
130
131 tmp = clps_readl(SYSFLG2);
132 if (tmp & SYSFLG2_CKMODE) {
133 cpu = ext;
134 bus = cpu;
135 spi = 135400;
Alexander Shiyan2a6f0612013-05-13 21:07:23 +0400136 pll = 0;
Alexander Shiyan61ae48c2012-08-21 20:59:35 +0400137 } else {
138 cpu = pll;
139 if (cpu >= 36864000)
140 bus = cpu / 2;
141 else
142 bus = 36864000 / 2;
143 spi = cpu / 576;
144 }
145
146 uart = bus / 10;
147
148 if (tmp & SYSFLG2_CKMODE) {
149 tmp = clps_readl(SYSCON2);
150 if (tmp & SYSCON2_OSTB)
151 timh = ext / 26;
152 else
153 timh = 541440;
154 } else
Alexander Shiyanc99f72a2013-05-13 21:07:32 +0400155 timh = DIV_ROUND_CLOSEST(cpu, 144);
Alexander Shiyan61ae48c2012-08-21 20:59:35 +0400156
Alexander Shiyanc99f72a2013-05-13 21:07:32 +0400157 timl = DIV_ROUND_CLOSEST(timh, 256);
Alexander Shiyan61ae48c2012-08-21 20:59:35 +0400158
159 /* All clocks are fixed */
160 add_fixed_clk(clk_pll, "pll", pll);
161 add_fixed_clk(clk_bus, "bus", bus);
162 add_fixed_clk(clk_uart, "uart", uart);
163 add_fixed_clk(clk_timerl, "timer_lf", timl);
164 add_fixed_clk(clk_timerh, "timer_hf", timh);
165 add_fixed_clk(clk_tint, "tint", 64);
166 add_fixed_clk(clk_spi, "spi", spi);
167
168 pr_info("CPU frequency set at %i Hz.\n", cpu);
169
Alexander Shiyanc99f72a2013-05-13 21:07:32 +0400170 /* Start Timer1 in free running mode (Low frequency) */
171 tmp = clps_readl(SYSCON1) & ~(SYSCON1_TC1S | SYSCON1_TC1M);
Alexander Shiyan61ae48c2012-08-21 20:59:35 +0400172 clps_writel(tmp, SYSCON1);
173
Stephen Boyd161f4082013-11-15 15:26:10 -0800174 sched_clock_register(clps711x_sched_clock_read, 16, timl);
Alexander Shiyanc99f72a2013-05-13 21:07:32 +0400175
176 clocksource_mmio_init(CLPS711X_VIRT_BASE + TC1D,
177 "clps711x_clocksource", timl, 300, 16,
178 clocksource_mmio_readw_down);
179
180 /* Set Timer2 prescaler */
181 clps_writew(DIV_ROUND_CLOSEST(timh, HZ), TC2D);
182
183 /* Start Timer2 in prescale mode (High frequency)*/
184 tmp = clps_readl(SYSCON1) | SYSCON1_TC2M | SYSCON1_TC2S;
185 clps_writel(tmp, SYSCON1);
186
187 clockevents_config_and_register(&clockevent_clps711x, timh, 0, 0);
Russell King3a083222011-11-05 17:38:32 +0000188
189 setup_irq(IRQ_TC2OI, &clps711x_timer_irq);
Russell King3a083222011-11-05 17:38:32 +0000190}
191
Robin Holt7b6d8642013-07-08 16:01:40 -0700192void clps711x_restart(enum reboot_mode mode, const char *cmd)
Russell King6c000712011-11-05 17:41:52 +0000193{
194 soft_restart(0);
195}
Nicolas Pitre71e256c2011-08-02 12:22:48 -0400196
197static void clps711x_idle(void)
198{
199 clps_writel(1, HALT);
Alexander Shiyand0ad52a2013-05-13 21:07:31 +0400200 asm("mov r0, r0");
201 asm("mov r0, r0");
Nicolas Pitre71e256c2011-08-02 12:22:48 -0400202}
203
Alexander Shiyand0ad52a2013-05-13 21:07:31 +0400204void __init clps711x_init_early(void)
Nicolas Pitre71e256c2011-08-02 12:22:48 -0400205{
206 arm_pm_idle = clps711x_idle;
Nicolas Pitre71e256c2011-08-02 12:22:48 -0400207}