blob: 45c92c3d3e703a227d53b8c8fde648038c88b211 [file] [log] [blame]
Aubrey Li51185db2007-03-20 18:16:24 +08001/*
Bin Meng75574052016-02-05 19:30:11 -08002 * U-Boot - interrupts.c Interrupt related routines
Aubrey Li51185db2007-03-20 18:16:24 +08003 *
Mike Frysinger94bae5c2008-03-30 15:46:13 -04004 * Copyright (c) 2005-2008 Analog Devices Inc.
Aubrey Li51185db2007-03-20 18:16:24 +08005 *
6 * This file is based on interrupts.c
7 * Copyright 1996 Roman Zippel
8 * Copyright 1999 D. Jeff Dionne <jeff@uclinux.org>
9 * Copyright 2000-2001 Lineo, Inc. D. Jefff Dionne <jeff@lineo.ca>
10 * Copyright 2002 Arcturus Networks Inc. MaTed <mated@sympatico.ca>
11 * Copyright 2003 Metrowerks/Motorola
12 * Copyright 2003 Bas Vermeulen <bas@buyways.nl>,
13 * BuyWays B.V. (www.buyways.nl)
14 *
15 * (C) Copyright 2000-2004
16 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Aubrey Li51185db2007-03-20 18:16:24 +080017 *
Mike Frysinger94bae5c2008-03-30 15:46:13 -040018 * Licensed under the GPL-2 or later.
Aubrey Li51185db2007-03-20 18:16:24 +080019 */
20
21#include <common.h>
Aubrey Li51185db2007-03-20 18:16:24 +080022#include <config.h>
Mike Frysingerf3352e32009-10-09 02:24:33 -040023#include <watchdog.h>
Aubrey Li51185db2007-03-20 18:16:24 +080024#include <asm/blackfin.h>
25#include "cpu.h"
26
27static ulong timestamp;
28static ulong last_time;
29static int int_flag;
30
31int irq_flags; /* needed by asm-blackfin/system.h */
32
33/* Functions just to satisfy the linker */
34
35/*
36 * This function is derived from PowerPC code (read timebase as long long).
Mike Frysinger94bae5c2008-03-30 15:46:13 -040037 * On Blackfin it just returns the timer value.
Aubrey Li51185db2007-03-20 18:16:24 +080038 */
39unsigned long long get_ticks(void)
40{
41 return get_timer(0);
42}
43
44/*
45 * This function is derived from PowerPC code (timebase clock frequency).
Mike Frysinger94bae5c2008-03-30 15:46:13 -040046 * On Blackfin it returns the number of timer ticks per second.
Aubrey Li51185db2007-03-20 18:16:24 +080047 */
48ulong get_tbclk(void)
49{
50 ulong tbclk;
51
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020052 tbclk = CONFIG_SYS_HZ;
Aubrey Li51185db2007-03-20 18:16:24 +080053 return tbclk;
54}
55
56void enable_interrupts(void)
57{
Mike Frysinger94bae5c2008-03-30 15:46:13 -040058 local_irq_restore(int_flag);
Aubrey Li51185db2007-03-20 18:16:24 +080059}
60
61int disable_interrupts(void)
62{
Mike Frysinger94bae5c2008-03-30 15:46:13 -040063 local_irq_save(int_flag);
Aubrey Li51185db2007-03-20 18:16:24 +080064 return 1;
65}
66
Ingo van Lilf0f778a2009-11-24 14:09:21 +010067void __udelay(unsigned long usec)
Aubrey Li51185db2007-03-20 18:16:24 +080068{
69 unsigned long delay, start, stop;
70 unsigned long cclk;
71 cclk = (CONFIG_CCLK_HZ);
72
73 while (usec > 1) {
Mike Frysingerf3352e32009-10-09 02:24:33 -040074 WATCHDOG_RESET();
75
Aubrey Li51185db2007-03-20 18:16:24 +080076 /*
77 * how many clock ticks to delay?
78 * - request(in useconds) * clock_ticks(Hz) / useconds/second
79 */
80 if (usec < 1000) {
81 delay = (usec * (cclk / 244)) >> 12;
82 usec = 0;
83 } else {
84 delay = (1000 * (cclk / 244)) >> 12;
85 usec -= 1000;
86 }
87
Mike Frysinger94bae5c2008-03-30 15:46:13 -040088 asm volatile (" %0 = CYCLES;" : "=r" (start));
Aubrey Li51185db2007-03-20 18:16:24 +080089 do {
Mike Frysinger94bae5c2008-03-30 15:46:13 -040090 asm volatile (" %0 = CYCLES; " : "=r" (stop));
Aubrey Li51185db2007-03-20 18:16:24 +080091 } while (stop - start < delay);
92 }
93
94 return;
95}
96
Mike Frysinger94bae5c2008-03-30 15:46:13 -040097#define MAX_TIM_LOAD 0xFFFFFFFF
98int timer_init(void)
Aubrey Li51185db2007-03-20 18:16:24 +080099{
Mike Frysinger3b7ed5a2009-11-12 18:42:53 -0500100 bfin_write_TCNTL(0x1);
Graf Yang52840a72009-05-19 04:40:08 -0400101 CSYNC();
Mike Frysinger3b7ed5a2009-11-12 18:42:53 -0500102 bfin_write_TSCALE(0x0);
103 bfin_write_TCOUNT(MAX_TIM_LOAD);
104 bfin_write_TPERIOD(MAX_TIM_LOAD);
105 bfin_write_TCNTL(0x7);
Graf Yang52840a72009-05-19 04:40:08 -0400106 CSYNC();
Aubrey Li51185db2007-03-20 18:16:24 +0800107
108 timestamp = 0;
109 last_time = 0;
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400110
111 return 0;
Aubrey Li51185db2007-03-20 18:16:24 +0800112}
113
114/*
115 * Any network command or flash
116 * command is started get_timer shall
117 * be called before TCOUNT gets reset,
118 * to implement the accurate timeouts.
119 *
120 * How ever milliconds doesn't return
121 * the number that has been elapsed from
122 * the last reset.
123 *
124 * As get_timer is used in the u-boot
125 * only for timeouts this should be
126 * sufficient
127 */
128ulong get_timer(ulong base)
129{
130 ulong milisec;
131
132 /* Number of clocks elapsed */
Mike Frysinger3b7ed5a2009-11-12 18:42:53 -0500133 ulong clocks = (MAX_TIM_LOAD - bfin_read_TCOUNT());
Aubrey Li51185db2007-03-20 18:16:24 +0800134
135 /*
136 * Find if the TCOUNT is reset
137 * timestamp gives the number of times
138 * TCOUNT got reset
139 */
140 if (clocks < last_time)
141 timestamp++;
142 last_time = clocks;
143
144 /* Get the number of milliseconds */
145 milisec = clocks / (CONFIG_CCLK_HZ / 1000);
146
147 /*
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400148 * Find the number of millisonds that
149 * got elapsed before this TCOUNT cycle
Aubrey Li51185db2007-03-20 18:16:24 +0800150 */
151 milisec += timestamp * (MAX_TIM_LOAD / (CONFIG_CCLK_HZ / 1000));
152
153 return (milisec - base);
154}