blob: 72e405df0fb07dd4b363153f149742875725f7ad [file] [log] [blame]
Leo Chen278a6752009-08-07 19:58:26 +01001/*
2 * derived from linux/arch/arm/mach-versatile/core.c
3 * linux/arch/arm/mach-bcmring/core.c
4 *
5 * Copyright (C) 1999 - 2003 ARM Limited
6 * Copyright (C) 2000 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 */
22/* Portions copyright Broadcom 2008 */
23
24#include <linux/init.h>
25#include <linux/device.h>
26#include <linux/dma-mapping.h>
27#include <linux/platform_device.h>
28#include <linux/sysdev.h>
29#include <linux/interrupt.h>
30#include <linux/amba/bus.h>
31#include <linux/clocksource.h>
32#include <linux/clockchips.h>
33
Leo Chen278a6752009-08-07 19:58:26 +010034#include <mach/csp/mm_addr.h>
35#include <mach/hardware.h>
36#include <asm/clkdev.h>
37#include <linux/io.h>
38#include <asm/irq.h>
39#include <asm/hardware/arm_timer.h>
40#include <asm/mach-types.h>
41
42#include <asm/mach/arch.h>
43#include <asm/mach/flash.h>
44#include <asm/mach/irq.h>
45#include <asm/mach/time.h>
46#include <asm/mach/map.h>
Leo Chen278a6752009-08-07 19:58:26 +010047
48#include <cfg_global.h>
49
50#include "clock.h"
51
52#include <csp/secHw.h>
53#include <mach/csp/secHw_def.h>
54#include <mach/csp/chipcHw_inline.h>
55#include <mach/csp/tmrHw_reg.h>
56
57#define AMBA_DEVICE(name, initname, base, plat, size) \
58static struct amba_device name##_device = { \
59 .dev = { \
60 .coherent_dma_mask = ~0, \
61 .init_name = initname, \
62 .platform_data = plat \
63 }, \
64 .res = { \
65 .start = MM_ADDR_IO_##base, \
66 .end = MM_ADDR_IO_##base + (size) - 1, \
67 .flags = IORESOURCE_MEM \
68 }, \
69 .dma_mask = ~0, \
70 .irq = { \
71 IRQ_##base \
72 } \
73}
74
75
76AMBA_DEVICE(uartA, "uarta", UARTA, NULL, SZ_4K);
77AMBA_DEVICE(uartB, "uartb", UARTB, NULL, SZ_4K);
78
79static struct clk pll1_clk = {
80 .name = "PLL1",
81 .type = CLK_TYPE_PRIMARY | CLK_TYPE_PLL1,
82 .rate_hz = 2000000000,
83 .use_cnt = 7,
84};
85
86static struct clk uart_clk = {
87 .name = "UART",
88 .type = CLK_TYPE_PROGRAMMABLE,
89 .csp_id = chipcHw_CLOCK_UART,
90 .rate_hz = HW_CFG_UART_CLK_HZ,
91 .parent = &pll1_clk,
92};
93
94static struct clk_lookup lookups[] = {
95 { /* UART0 */
96 .dev_id = "uarta",
97 .clk = &uart_clk,
98 }, { /* UART1 */
99 .dev_id = "uartb",
100 .clk = &uart_clk,
101 }
102};
103
104static struct amba_device *amba_devs[] __initdata = {
105 &uartA_device,
106 &uartB_device,
107};
108
109void __init bcmring_amba_init(void)
110{
111 int i;
112 u32 bus_clock;
113
114/* Linux is run initially in non-secure mode. Secure peripherals */
115/* generate FIQ, and must be handled in secure mode. Until we have */
116/* a linux security monitor implementation, keep everything in */
117/* non-secure mode. */
118 chipcHw_busInterfaceClockEnable(chipcHw_REG_BUS_CLOCK_SPU);
119 secHw_setUnsecure(secHw_BLK_MASK_CHIP_CONTROL |
120 secHw_BLK_MASK_KEY_SCAN |
121 secHw_BLK_MASK_TOUCH_SCREEN |
122 secHw_BLK_MASK_UART0 |
123 secHw_BLK_MASK_UART1 |
124 secHw_BLK_MASK_WATCHDOG |
125 secHw_BLK_MASK_SPUM |
126 secHw_BLK_MASK_DDR2 |
127 secHw_BLK_MASK_SPU |
128 secHw_BLK_MASK_PKA |
129 secHw_BLK_MASK_RNG |
130 secHw_BLK_MASK_RTC |
131 secHw_BLK_MASK_OTP |
132 secHw_BLK_MASK_BOOT |
133 secHw_BLK_MASK_MPU |
134 secHw_BLK_MASK_TZCTRL | secHw_BLK_MASK_INTR);
135
136 /* Only the devices attached to the AMBA bus are enabled just before the bus is */
137 /* scanned and the drivers are loaded. The clocks need to be on for the AMBA bus */
138 /* driver to access these blocks. The bus is probed, and the drivers are loaded. */
139 /* FIXME Need to remove enable of PIF once CLCD clock enable used properly in FPGA. */
140 bus_clock = chipcHw_REG_BUS_CLOCK_GE
141 | chipcHw_REG_BUS_CLOCK_SDIO0 | chipcHw_REG_BUS_CLOCK_SDIO1;
142
143 chipcHw_busInterfaceClockEnable(bus_clock);
144
Russell King0a0300d2010-01-12 12:28:00 +0000145 clkdev_add_table(lookups, ARRAY_SIZE(lookups));
Leo Chen278a6752009-08-07 19:58:26 +0100146
147 for (i = 0; i < ARRAY_SIZE(amba_devs); i++) {
148 struct amba_device *d = amba_devs[i];
149 amba_device_register(d, &iomem_resource);
150 }
151}
152
153/*
154 * Where is the timer (VA)?
155 */
156#define TIMER0_VA_BASE MM_IO_BASE_TMR
157#define TIMER1_VA_BASE (MM_IO_BASE_TMR + 0x20)
158#define TIMER2_VA_BASE (MM_IO_BASE_TMR + 0x40)
159#define TIMER3_VA_BASE (MM_IO_BASE_TMR + 0x60)
160
161/* Timer 0 - 25 MHz, Timer3 at bus clock rate, typically 150-166 MHz */
162#if defined(CONFIG_ARCH_FPGA11107)
163/* fpga cpu/bus are currently 30 times slower so scale frequency as well to */
164/* slow down Linux's sense of time */
165#define TIMER0_FREQUENCY_MHZ (tmrHw_LOW_FREQUENCY_MHZ * 30)
166#define TIMER1_FREQUENCY_MHZ (tmrHw_LOW_FREQUENCY_MHZ * 30)
167#define TIMER3_FREQUENCY_MHZ (tmrHw_HIGH_FREQUENCY_MHZ * 30)
168#define TIMER3_FREQUENCY_KHZ (tmrHw_HIGH_FREQUENCY_HZ / 1000 * 30)
169#else
170#define TIMER0_FREQUENCY_MHZ tmrHw_LOW_FREQUENCY_MHZ
171#define TIMER1_FREQUENCY_MHZ tmrHw_LOW_FREQUENCY_MHZ
172#define TIMER3_FREQUENCY_MHZ tmrHw_HIGH_FREQUENCY_MHZ
173#define TIMER3_FREQUENCY_KHZ (tmrHw_HIGH_FREQUENCY_HZ / 1000)
174#endif
175
176#define TICKS_PER_uSEC TIMER0_FREQUENCY_MHZ
177
178/*
179 * These are useconds NOT ticks.
180 *
181 */
182#define mSEC_1 1000
183#define mSEC_5 (mSEC_1 * 5)
184#define mSEC_10 (mSEC_1 * 10)
185#define mSEC_25 (mSEC_1 * 25)
186#define SEC_1 (mSEC_1 * 1000)
187
188/*
189 * How long is the timer interval?
190 */
191#define TIMER_INTERVAL (TICKS_PER_uSEC * mSEC_10)
192#if TIMER_INTERVAL >= 0x100000
193#define TIMER_RELOAD (TIMER_INTERVAL >> 8)
194#define TIMER_DIVISOR (TIMER_CTRL_DIV256)
195#define TICKS2USECS(x) (256 * (x) / TICKS_PER_uSEC)
196#elif TIMER_INTERVAL >= 0x10000
197#define TIMER_RELOAD (TIMER_INTERVAL >> 4) /* Divide by 16 */
198#define TIMER_DIVISOR (TIMER_CTRL_DIV16)
199#define TICKS2USECS(x) (16 * (x) / TICKS_PER_uSEC)
200#else
201#define TIMER_RELOAD (TIMER_INTERVAL)
202#define TIMER_DIVISOR (TIMER_CTRL_DIV1)
203#define TICKS2USECS(x) ((x) / TICKS_PER_uSEC)
204#endif
205
206static void timer_set_mode(enum clock_event_mode mode,
207 struct clock_event_device *clk)
208{
209 unsigned long ctrl;
210
211 switch (mode) {
212 case CLOCK_EVT_MODE_PERIODIC:
213 writel(TIMER_RELOAD, TIMER0_VA_BASE + TIMER_LOAD);
214
215 ctrl = TIMER_CTRL_PERIODIC;
216 ctrl |=
217 TIMER_DIVISOR | TIMER_CTRL_32BIT | TIMER_CTRL_IE |
218 TIMER_CTRL_ENABLE;
219 break;
220 case CLOCK_EVT_MODE_ONESHOT:
221 /* period set, and timer enabled in 'next_event' hook */
222 ctrl = TIMER_CTRL_ONESHOT;
223 ctrl |= TIMER_DIVISOR | TIMER_CTRL_32BIT | TIMER_CTRL_IE;
224 break;
225 case CLOCK_EVT_MODE_UNUSED:
226 case CLOCK_EVT_MODE_SHUTDOWN:
227 default:
228 ctrl = 0;
229 }
230
231 writel(ctrl, TIMER0_VA_BASE + TIMER_CTRL);
232}
233
234static int timer_set_next_event(unsigned long evt,
235 struct clock_event_device *unused)
236{
237 unsigned long ctrl = readl(TIMER0_VA_BASE + TIMER_CTRL);
238
239 writel(evt, TIMER0_VA_BASE + TIMER_LOAD);
240 writel(ctrl | TIMER_CTRL_ENABLE, TIMER0_VA_BASE + TIMER_CTRL);
241
242 return 0;
243}
244
245static struct clock_event_device timer0_clockevent = {
246 .name = "timer0",
247 .shift = 32,
248 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
249 .set_mode = timer_set_mode,
250 .set_next_event = timer_set_next_event,
251};
252
253/*
254 * IRQ handler for the timer
255 */
256static irqreturn_t bcmring_timer_interrupt(int irq, void *dev_id)
257{
258 struct clock_event_device *evt = &timer0_clockevent;
259
260 writel(1, TIMER0_VA_BASE + TIMER_INTCLR);
261
262 evt->event_handler(evt);
263
264 return IRQ_HANDLED;
265}
266
267static struct irqaction bcmring_timer_irq = {
268 .name = "bcmring Timer Tick",
269 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
270 .handler = bcmring_timer_interrupt,
271};
272
Leo Chenb6234382009-10-06 19:30:40 +0100273static cycle_t bcmring_get_cycles_timer1(struct clocksource *cs)
Leo Chen278a6752009-08-07 19:58:26 +0100274{
275 return ~readl(TIMER1_VA_BASE + TIMER_VALUE);
276}
277
Leo Chenb6234382009-10-06 19:30:40 +0100278static cycle_t bcmring_get_cycles_timer3(struct clocksource *cs)
Leo Chen278a6752009-08-07 19:58:26 +0100279{
280 return ~readl(TIMER3_VA_BASE + TIMER_VALUE);
281}
282
283static struct clocksource clocksource_bcmring_timer1 = {
284 .name = "timer1",
285 .rating = 200,
286 .read = bcmring_get_cycles_timer1,
287 .mask = CLOCKSOURCE_MASK(32),
288 .shift = 20,
289 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
290};
291
292static struct clocksource clocksource_bcmring_timer3 = {
293 .name = "timer3",
294 .rating = 100,
295 .read = bcmring_get_cycles_timer3,
296 .mask = CLOCKSOURCE_MASK(32),
297 .shift = 20,
298 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
299};
300
301static int __init bcmring_clocksource_init(void)
302{
303 /* setup timer1 as free-running clocksource */
304 writel(0, TIMER1_VA_BASE + TIMER_CTRL);
305 writel(0xffffffff, TIMER1_VA_BASE + TIMER_LOAD);
306 writel(0xffffffff, TIMER1_VA_BASE + TIMER_VALUE);
307 writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
308 TIMER1_VA_BASE + TIMER_CTRL);
309
310 clocksource_bcmring_timer1.mult =
311 clocksource_khz2mult(TIMER1_FREQUENCY_MHZ * 1000,
312 clocksource_bcmring_timer1.shift);
313 clocksource_register(&clocksource_bcmring_timer1);
314
315 /* setup timer3 as free-running clocksource */
316 writel(0, TIMER3_VA_BASE + TIMER_CTRL);
317 writel(0xffffffff, TIMER3_VA_BASE + TIMER_LOAD);
318 writel(0xffffffff, TIMER3_VA_BASE + TIMER_VALUE);
319 writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
320 TIMER3_VA_BASE + TIMER_CTRL);
321
322 clocksource_bcmring_timer3.mult =
323 clocksource_khz2mult(TIMER3_FREQUENCY_KHZ,
324 clocksource_bcmring_timer3.shift);
325 clocksource_register(&clocksource_bcmring_timer3);
326
327 return 0;
328}
329
330/*
331 * Set up timer interrupt, and return the current time in seconds.
332 */
333void __init bcmring_init_timer(void)
334{
335 printk(KERN_INFO "bcmring_init_timer\n");
336 /*
337 * Initialise to a known state (all timers off)
338 */
339 writel(0, TIMER0_VA_BASE + TIMER_CTRL);
340 writel(0, TIMER1_VA_BASE + TIMER_CTRL);
341 writel(0, TIMER2_VA_BASE + TIMER_CTRL);
342 writel(0, TIMER3_VA_BASE + TIMER_CTRL);
343
344 /*
345 * Make irqs happen for the system timer
346 */
347 setup_irq(IRQ_TIMER0, &bcmring_timer_irq);
348
349 bcmring_clocksource_init();
350
351 timer0_clockevent.mult =
352 div_sc(1000000, NSEC_PER_SEC, timer0_clockevent.shift);
353 timer0_clockevent.max_delta_ns =
354 clockevent_delta2ns(0xffffffff, &timer0_clockevent);
355 timer0_clockevent.min_delta_ns =
356 clockevent_delta2ns(0xf, &timer0_clockevent);
357
358 timer0_clockevent.cpumask = cpumask_of(0);
359 clockevents_register_device(&timer0_clockevent);
360}
361
362struct sys_timer bcmring_timer = {
363 .init = bcmring_init_timer,
364};