blob: 1e4c2142b1317cea432c950af40486b35876fe53 [file] [log] [blame]
wdenk8ed96042005-01-09 23:16:25 +00001/*
2 * (C) Copyright 2004 Texas Insturments
3 *
4 * (C) Copyright 2002
5 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Marius Groeger <mgroeger@sysgo.de>
7 *
8 * (C) Copyright 2002
Detlev Zundel792a09e2009-05-13 10:54:10 +02009 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
wdenk8ed96042005-01-09 23:16:25 +000010 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020011 * SPDX-License-Identifier: GPL-2.0+
wdenk8ed96042005-01-09 23:16:25 +000012 */
13
14/*
15 * CPU specific code
16 */
17
18#include <common.h>
19#include <command.h>
Jean-Christophe PLAGNIOL-VILLARD677e62f2009-04-05 13:02:43 +020020#include <asm/system.h>
wdenk8ed96042005-01-09 23:16:25 +000021
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +020022static void cache_flush(void);
wdenk8ed96042005-01-09 23:16:25 +000023
wdenk8ed96042005-01-09 23:16:25 +000024int cleanup_before_linux (void)
25{
26 /*
27 * this function is called just before we call linux
28 * it prepares the processor for linux
29 *
30 * we turn off caches etc ...
31 */
32
wdenk8ed96042005-01-09 23:16:25 +000033 disable_interrupts ();
34
wdenk8ed96042005-01-09 23:16:25 +000035 /* turn off I/D-cache */
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +020036 icache_disable();
37 dcache_disable();
wdenk8ed96042005-01-09 23:16:25 +000038 /* flush I/D-cache */
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +020039 cache_flush();
40
41 return 0;
wdenk8ed96042005-01-09 23:16:25 +000042}
43
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +020044static void cache_flush(void)
wdenk8ed96042005-01-09 23:16:25 +000045{
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +020046 unsigned long i = 0;
Stefano Babicfbf4a072012-04-09 13:33:04 +020047 /* clean entire data cache */
48 asm volatile("mcr p15, 0, %0, c7, c10, 0" : : "r" (i));
49 /* invalidate both caches and flush btb */
50 asm volatile("mcr p15, 0, %0, c7, c7, 0" : : "r" (i));
51 /* mem barrier to sync things */
52 asm volatile("mcr p15, 0, %0, c7, c10, 4" : : "r" (i));
wdenk8ed96042005-01-09 23:16:25 +000053}
Anatolij Gustschin219872c2012-04-02 06:18:00 +000054
55#ifndef CONFIG_SYS_DCACHE_OFF
56
57#ifndef CONFIG_SYS_CACHELINE_SIZE
58#define CONFIG_SYS_CACHELINE_SIZE 32
59#endif
60
61void invalidate_dcache_all(void)
62{
Stefano Babicfbf4a072012-04-09 13:33:04 +020063 asm volatile("mcr p15, 0, %0, c7, c6, 0" : : "r" (0));
Anatolij Gustschin219872c2012-04-02 06:18:00 +000064}
65
66void flush_dcache_all(void)
67{
Stefano Babicfbf4a072012-04-09 13:33:04 +020068 asm volatile("mcr p15, 0, %0, c7, c10, 0" : : "r" (0));
69 asm volatile("mcr p15, 0, %0, c7, c10, 4" : : "r" (0));
Anatolij Gustschin219872c2012-04-02 06:18:00 +000070}
71
Benoît Thébaudeauf8f09dd2012-07-19 01:35:32 +000072static int check_cache_range(unsigned long start, unsigned long stop)
Anatolij Gustschin219872c2012-04-02 06:18:00 +000073{
74 int ok = 1;
75
76 if (start & (CONFIG_SYS_CACHELINE_SIZE - 1))
77 ok = 0;
78
79 if (stop & (CONFIG_SYS_CACHELINE_SIZE - 1))
80 ok = 0;
81
82 if (!ok)
83 debug("CACHE: Misaligned operation at range [%08lx, %08lx]\n",
84 start, stop);
85
86 return ok;
87}
88
89void invalidate_dcache_range(unsigned long start, unsigned long stop)
90{
Benoît Thébaudeauf8f09dd2012-07-19 01:35:32 +000091 if (!check_cache_range(start, stop))
Anatolij Gustschin219872c2012-04-02 06:18:00 +000092 return;
93
94 while (start < stop) {
Stefano Babicfbf4a072012-04-09 13:33:04 +020095 asm volatile("mcr p15, 0, %0, c7, c6, 1" : : "r" (start));
Anatolij Gustschin219872c2012-04-02 06:18:00 +000096 start += CONFIG_SYS_CACHELINE_SIZE;
97 }
98}
99
100void flush_dcache_range(unsigned long start, unsigned long stop)
101{
Benoît Thébaudeauf8f09dd2012-07-19 01:35:32 +0000102 if (!check_cache_range(start, stop))
Anatolij Gustschin219872c2012-04-02 06:18:00 +0000103 return;
104
105 while (start < stop) {
Stefano Babicfbf4a072012-04-09 13:33:04 +0200106 asm volatile("mcr p15, 0, %0, c7, c14, 1" : : "r" (start));
Anatolij Gustschin219872c2012-04-02 06:18:00 +0000107 start += CONFIG_SYS_CACHELINE_SIZE;
108 }
109
Stefano Babicfbf4a072012-04-09 13:33:04 +0200110 asm volatile("mcr p15, 0, %0, c7, c10, 4" : : "r" (0));
Anatolij Gustschin219872c2012-04-02 06:18:00 +0000111}
112
Anatolij Gustschin219872c2012-04-02 06:18:00 +0000113#else /* #ifndef CONFIG_SYS_DCACHE_OFF */
114void invalidate_dcache_all(void)
115{
116}
117
118void flush_dcache_all(void)
119{
120}
Anatolij Gustschin219872c2012-04-02 06:18:00 +0000121#endif /* #ifndef CONFIG_SYS_DCACHE_OFF */
Benoît Thébaudeauccfa3982012-10-04 10:04:02 +0000122
123#if !defined(CONFIG_SYS_ICACHE_OFF) || !defined(CONFIG_SYS_DCACHE_OFF)
124void enable_caches(void)
125{
126#ifndef CONFIG_SYS_ICACHE_OFF
127 icache_enable();
128#endif
129#ifndef CONFIG_SYS_DCACHE_OFF
130 dcache_enable();
131#endif
132}
133#endif