blob: 7799c146b53f809f3afdb8a661d821c8d07b64f6 [file] [log] [blame]
wdenk32fe2872002-10-11 07:57:01 +00001/*
Marek Vasutd1bb9442011-11-26 10:02:41 +01002 * Marvell PXA2xx/3xx timer driver
wdenk32fe2872002-10-11 07:57:01 +00003 *
Marek Vasutd1bb9442011-11-26 10:02:41 +01004 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
wdenk32fe2872002-10-11 07:57:01 +00005 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 */
24
wdenk32fe2872002-10-11 07:57:01 +000025#include <asm/arch/pxa-regs.h>
Marek Vasut3ba8bf72010-09-09 09:50:39 +020026#include <asm/io.h>
27#include <common.h>
Jean-Christophe PLAGNIOL-VILLARDa922fdb2009-02-24 06:13:10 +010028#include <div64.h>
wdenk32fe2872002-10-11 07:57:01 +000029
Marek Vasutd1bb9442011-11-26 10:02:41 +010030DECLARE_GLOBAL_DATA_PTR;
31
32#define TIMER_LOAD_VAL 0xffffffff
33
Simon Glass66ee6922012-12-13 20:48:34 +000034#define timestamp (gd->arch.tbl)
Marek Vasutd1bb9442011-11-26 10:02:41 +010035#define lastinc (gd->lastinc)
wdenk32fe2872002-10-11 07:57:01 +000036
Marek Vasutabc20ab2011-11-26 07:20:07 +010037#if defined(CONFIG_CPU_PXA27X) || defined(CONFIG_CPU_MONAHANS)
Marek Vasutd1bb9442011-11-26 10:02:41 +010038#define TIMER_FREQ_HZ 3250000
Marek Vasutabc20ab2011-11-26 07:20:07 +010039#elif defined(CONFIG_CPU_PXA25X)
Marek Vasutd1bb9442011-11-26 10:02:41 +010040#define TIMER_FREQ_HZ 3686400
Micha Kalfon94a33122009-02-11 19:50:11 +020041#else
42#error "Timer frequency unknown - please config PXA CPU type"
43#endif
44
Marek Vasutd1bb9442011-11-26 10:02:41 +010045static unsigned long long tick_to_time(unsigned long long tick)
Jean-Christophe PLAGNIOL-VILLARDa922fdb2009-02-24 06:13:10 +010046{
Marek Vasutd1bb9442011-11-26 10:02:41 +010047 return tick * CONFIG_SYS_HZ / TIMER_FREQ_HZ;
Jean-Christophe PLAGNIOL-VILLARDa922fdb2009-02-24 06:13:10 +010048}
49
Marek Vasutd1bb9442011-11-26 10:02:41 +010050static unsigned long long us_to_tick(unsigned long long us)
Jean-Christophe PLAGNIOL-VILLARDa922fdb2009-02-24 06:13:10 +010051{
Marek Vasutd1bb9442011-11-26 10:02:41 +010052 return (us * TIMER_FREQ_HZ) / 1000000;
Jean-Christophe PLAGNIOL-VILLARDa922fdb2009-02-24 06:13:10 +010053}
54
Marek Vasutd1bb9442011-11-26 10:02:41 +010055int timer_init(void)
wdenk32fe2872002-10-11 07:57:01 +000056{
Graeme Russ17659d72011-07-15 02:21:14 +000057 writel(0, OSCR);
Jean-Christophe PLAGNIOL-VILLARDb54384e2009-05-15 23:47:02 +020058 return 0;
wdenk32fe2872002-10-11 07:57:01 +000059}
60
Marek Vasutd1bb9442011-11-26 10:02:41 +010061unsigned long long get_ticks(void)
wdenk32fe2872002-10-11 07:57:01 +000062{
Marek Vasutd1bb9442011-11-26 10:02:41 +010063 /* Current tick value */
64 uint32_t now = readl(OSCR);
65
66 if (now >= lastinc) {
67 /*
68 * Normal mode (non roll)
69 * Move stamp forward with absolute diff ticks
70 */
71 timestamp += (now - lastinc);
72 } else {
73 /* We have rollover of incrementer */
74 timestamp += (TIMER_LOAD_VAL - lastinc) + now;
75 }
76
77 lastinc = now;
78 return timestamp;
wdenk32fe2872002-10-11 07:57:01 +000079}
80
Marek Vasutd1bb9442011-11-26 10:02:41 +010081ulong get_timer(ulong base)
wdenk32fe2872002-10-11 07:57:01 +000082{
Marek Vasutd1bb9442011-11-26 10:02:41 +010083 return tick_to_time(get_ticks()) - base;
wdenk32fe2872002-10-11 07:57:01 +000084}
85
Marek Vasutd1bb9442011-11-26 10:02:41 +010086void __udelay(unsigned long usec)
wdenk32fe2872002-10-11 07:57:01 +000087{
Jean-Christophe PLAGNIOL-VILLARDa922fdb2009-02-24 06:13:10 +010088 unsigned long long tmp;
wdenk32fe2872002-10-11 07:57:01 +000089 ulong tmo;
90
Jean-Christophe PLAGNIOL-VILLARDa922fdb2009-02-24 06:13:10 +010091 tmo = us_to_tick(usec);
92 tmp = get_ticks() + tmo; /* get current timestamp */
wdenk32fe2872002-10-11 07:57:01 +000093
Jean-Christophe PLAGNIOL-VILLARDa922fdb2009-02-24 06:13:10 +010094 while (get_ticks() < tmp) /* loop till event */
95 /*NOP*/;
wdenkfc3e2162003-10-08 22:33:00 +000096}
Marek Vasut30a14ea2012-02-27 13:59:47 +010097
98ulong get_tbclk(void)
99{
100 return TIMER_FREQ_HZ;
101}