blob: d8baf08aa85238645e8a49e5a8788979c2d9c81c [file] [log] [blame]
Paul Burton30374f92015-01-29 01:27:57 +00001/*
2 * (C) Copyright 2003
3 * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <asm/cacheops.h>
10#include <asm/mipsregs.h>
11
Paul Burton8cb48172016-09-21 11:18:48 +010012DECLARE_GLOBAL_DATA_PTR;
Paul Burton37228622016-05-27 14:28:05 +010013
Paul Burton8cb48172016-09-21 11:18:48 +010014void mips_cache_probe(void)
15{
16#ifdef CONFIG_SYS_CACHE_SIZE_AUTO
17 unsigned long conf1, il, dl;
Paul Burton37228622016-05-27 14:28:05 +010018
Paul Burton30374f92015-01-29 01:27:57 +000019 conf1 = read_c0_config1();
Paul Burton8cb48172016-09-21 11:18:48 +010020
Daniel Schwierzecka3ab2ae2016-01-12 21:48:26 +010021 il = (conf1 & MIPS_CONF1_IL) >> MIPS_CONF1_IL_SHF;
Paul Burton8cb48172016-09-21 11:18:48 +010022 dl = (conf1 & MIPS_CONF1_DL) >> MIPS_CONF1_DL_SHF;
23
24 gd->arch.l1i_line_size = il ? (2 << il) : 0;
25 gd->arch.l1d_line_size = dl ? (2 << dl) : 0;
26#endif
27}
28
29static inline unsigned long icache_line_size(void)
30{
31#ifdef CONFIG_SYS_CACHE_SIZE_AUTO
32 return gd->arch.l1i_line_size;
33#else
34 return CONFIG_SYS_ICACHE_LINE_SIZE;
35#endif
Paul Burton30374f92015-01-29 01:27:57 +000036}
37
38static inline unsigned long dcache_line_size(void)
39{
Paul Burton8cb48172016-09-21 11:18:48 +010040#ifdef CONFIG_SYS_CACHE_SIZE_AUTO
41 return gd->arch.l1d_line_size;
42#else
43 return CONFIG_SYS_DCACHE_LINE_SIZE;
44#endif
Paul Burton30374f92015-01-29 01:27:57 +000045}
46
Paul Burtonfb64cda2016-05-27 14:28:06 +010047#define cache_loop(start, end, lsize, ops...) do { \
48 const void *addr = (const void *)(start & ~(lsize - 1)); \
49 const void *aend = (const void *)((end - 1) & ~(lsize - 1)); \
50 const unsigned int cache_ops[] = { ops }; \
51 unsigned int i; \
52 \
53 for (; addr <= aend; addr += lsize) { \
54 for (i = 0; i < ARRAY_SIZE(cache_ops); i++) \
55 mips_cache(cache_ops[i], addr); \
56 } \
57} while (0)
58
Paul Burton30374f92015-01-29 01:27:57 +000059void flush_cache(ulong start_addr, ulong size)
60{
61 unsigned long ilsize = icache_line_size();
62 unsigned long dlsize = dcache_line_size();
Paul Burton30374f92015-01-29 01:27:57 +000063
64 /* aend will be miscalculated when size is zero, so we return here */
65 if (size == 0)
66 return;
67
Paul Burton30374f92015-01-29 01:27:57 +000068 if (ilsize == dlsize) {
69 /* flush I-cache & D-cache simultaneously */
Paul Burtonfb64cda2016-05-27 14:28:06 +010070 cache_loop(start_addr, start_addr + size, ilsize,
71 HIT_WRITEBACK_INV_D, HIT_INVALIDATE_I);
Paul Burton30374f92015-01-29 01:27:57 +000072 return;
73 }
74
75 /* flush D-cache */
Paul Burtonfb64cda2016-05-27 14:28:06 +010076 cache_loop(start_addr, start_addr + size, dlsize, HIT_WRITEBACK_INV_D);
Paul Burton30374f92015-01-29 01:27:57 +000077
78 /* flush I-cache */
Paul Burtonfb64cda2016-05-27 14:28:06 +010079 cache_loop(start_addr, start_addr + size, ilsize, HIT_INVALIDATE_I);
Paul Burton30374f92015-01-29 01:27:57 +000080}
81
82void flush_dcache_range(ulong start_addr, ulong stop)
83{
84 unsigned long lsize = dcache_line_size();
Paul Burton30374f92015-01-29 01:27:57 +000085
Marek Vasutfbb0de02016-01-27 03:13:59 +010086 /* aend will be miscalculated when size is zero, so we return here */
87 if (start_addr == stop)
88 return;
89
Paul Burtonfb64cda2016-05-27 14:28:06 +010090 cache_loop(start_addr, stop, lsize, HIT_WRITEBACK_INV_D);
Paul Burton30374f92015-01-29 01:27:57 +000091}
92
93void invalidate_dcache_range(ulong start_addr, ulong stop)
94{
95 unsigned long lsize = dcache_line_size();
Paul Burton30374f92015-01-29 01:27:57 +000096
Marek Vasutfbb0de02016-01-27 03:13:59 +010097 /* aend will be miscalculated when size is zero, so we return here */
98 if (start_addr == stop)
99 return;
100
Paul Burtona95800e2016-06-09 13:09:51 +0100101 cache_loop(start_addr, stop, lsize, HIT_INVALIDATE_D);
Paul Burton30374f92015-01-29 01:27:57 +0000102}