Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Nobuhiro Iwamatsu | 547b67f | 2007-09-23 02:12:30 +0900 | [diff] [blame] | 2 | /* |
Vladimir Zapolskiy | f34743c | 2016-11-28 00:15:18 +0200 | [diff] [blame] | 3 | * (C) Copyright 2016 Vladimir Zapolskiy <vz@mleia.com> |
| 4 | * (C) Copyright 2007 Nobuhiro Iwamatsu <iwamatsu@nigauri.org> |
Nobuhiro Iwamatsu | 547b67f | 2007-09-23 02:12:30 +0900 | [diff] [blame] | 5 | */ |
| 6 | |
Nobuhiro Iwamatsu | 547b67f | 2007-09-23 02:12:30 +0900 | [diff] [blame] | 7 | #include <command.h> |
Simon Glass | 1d91ba7 | 2019-11-14 12:57:37 -0700 | [diff] [blame] | 8 | #include <cpu_func.h> |
Tom Rini | 15c6469 | 2024-06-19 15:27:56 -0600 | [diff] [blame] | 9 | #include <stdio.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 10 | #include <asm/cache.h> |
Nobuhiro Iwamatsu | 547b67f | 2007-09-23 02:12:30 +0900 | [diff] [blame] | 11 | #include <asm/io.h> |
Vladimir Zapolskiy | 57e56ef | 2016-11-28 00:15:16 +0200 | [diff] [blame] | 12 | #include <asm/processor.h> |
| 13 | #include <asm/system.h> |
Nobuhiro Iwamatsu | 547b67f | 2007-09-23 02:12:30 +0900 | [diff] [blame] | 14 | |
| 15 | #define CACHE_VALID 1 |
| 16 | #define CACHE_UPDATED 2 |
| 17 | |
| 18 | static inline void cache_wback_all(void) |
| 19 | { |
| 20 | unsigned long addr, data, i, j; |
| 21 | |
Vladimir Zapolskiy | e852996 | 2016-11-28 00:15:17 +0200 | [diff] [blame] | 22 | for (i = 0; i < CACHE_OC_NUM_ENTRIES; i++) { |
Nobuhiro Iwamatsu | 547b67f | 2007-09-23 02:12:30 +0900 | [diff] [blame] | 23 | for (j = 0; j < CACHE_OC_NUM_WAYS; j++) { |
Vladimir Zapolskiy | e852996 | 2016-11-28 00:15:17 +0200 | [diff] [blame] | 24 | addr = CACHE_OC_ADDRESS_ARRAY |
| 25 | | (j << CACHE_OC_WAY_SHIFT) |
Nobuhiro Iwamatsu | 547b67f | 2007-09-23 02:12:30 +0900 | [diff] [blame] | 26 | | (i << CACHE_OC_ENTRY_SHIFT); |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 27 | data = inl(addr); |
Nobuhiro Iwamatsu | 547b67f | 2007-09-23 02:12:30 +0900 | [diff] [blame] | 28 | if (data & CACHE_UPDATED) { |
| 29 | data &= ~CACHE_UPDATED; |
| 30 | outl(data, addr); |
| 31 | } |
| 32 | } |
| 33 | } |
Nobuhiro Iwamatsu | 547b67f | 2007-09-23 02:12:30 +0900 | [diff] [blame] | 34 | } |
| 35 | |
Marek Vasut | c5afa02 | 2024-09-10 01:18:09 +0200 | [diff] [blame] | 36 | #define CACHE_ENABLE 0 |
| 37 | #define CACHE_DISABLE 1 |
| 38 | #define CACHE_INVALIDATE 2 |
Nobuhiro Iwamatsu | 547b67f | 2007-09-23 02:12:30 +0900 | [diff] [blame] | 39 | |
Vladimir Zapolskiy | f34743c | 2016-11-28 00:15:18 +0200 | [diff] [blame] | 40 | static int cache_control(unsigned int cmd) |
Nobuhiro Iwamatsu | 547b67f | 2007-09-23 02:12:30 +0900 | [diff] [blame] | 41 | { |
| 42 | unsigned long ccr; |
| 43 | |
| 44 | jump_to_P2(); |
| 45 | ccr = inl(CCR); |
| 46 | |
| 47 | if (ccr & CCR_CACHE_ENABLE) |
| 48 | cache_wback_all(); |
| 49 | |
Marek Vasut | c5afa02 | 2024-09-10 01:18:09 +0200 | [diff] [blame] | 50 | if (cmd == CACHE_INVALIDATE) |
| 51 | outl(CCR_CACHE_ICI | ccr, CCR); |
| 52 | else if (cmd == CACHE_DISABLE) |
Nobuhiro Iwamatsu | 547b67f | 2007-09-23 02:12:30 +0900 | [diff] [blame] | 53 | outl(CCR_CACHE_STOP, CCR); |
| 54 | else |
| 55 | outl(CCR_CACHE_INIT, CCR); |
| 56 | back_to_P1(); |
| 57 | |
| 58 | return 0; |
| 59 | } |
Mike Frysinger | b99910e | 2011-10-27 04:59:59 -0400 | [diff] [blame] | 60 | |
Nobuhiro Iwamatsu | 5b96baf | 2013-08-22 08:43:47 +0900 | [diff] [blame] | 61 | void flush_dcache_range(unsigned long start, unsigned long end) |
Mike Frysinger | b99910e | 2011-10-27 04:59:59 -0400 | [diff] [blame] | 62 | { |
| 63 | u32 v; |
| 64 | |
| 65 | start &= ~(L1_CACHE_BYTES - 1); |
| 66 | for (v = start; v < end; v += L1_CACHE_BYTES) { |
Vladimir Zapolskiy | 7a22f7a | 2016-11-28 00:15:13 +0200 | [diff] [blame] | 67 | asm volatile ("ocbp %0" : /* no output */ |
Mike Frysinger | b99910e | 2011-10-27 04:59:59 -0400 | [diff] [blame] | 68 | : "m" (__m(v))); |
| 69 | } |
| 70 | } |
| 71 | |
Tom Rini | c1beb76 | 2024-06-19 15:27:55 -0600 | [diff] [blame] | 72 | /* |
| 73 | * Default implementation: |
| 74 | * do a range flush for the entire range |
| 75 | */ |
| 76 | void flush_dcache_all(void) |
| 77 | { |
| 78 | flush_dcache_range(0, ~0); |
| 79 | } |
| 80 | |
Nobuhiro Iwamatsu | 5b96baf | 2013-08-22 08:43:47 +0900 | [diff] [blame] | 81 | void invalidate_dcache_range(unsigned long start, unsigned long end) |
Mike Frysinger | b99910e | 2011-10-27 04:59:59 -0400 | [diff] [blame] | 82 | { |
| 83 | u32 v; |
| 84 | |
| 85 | start &= ~(L1_CACHE_BYTES - 1); |
| 86 | for (v = start; v < end; v += L1_CACHE_BYTES) { |
| 87 | asm volatile ("ocbi %0" : /* no output */ |
| 88 | : "m" (__m(v))); |
| 89 | } |
| 90 | } |
Vladimir Zapolskiy | f34743c | 2016-11-28 00:15:18 +0200 | [diff] [blame] | 91 | |
| 92 | void flush_cache(unsigned long addr, unsigned long size) |
| 93 | { |
| 94 | flush_dcache_range(addr , addr + size); |
| 95 | } |
| 96 | |
| 97 | void icache_enable(void) |
| 98 | { |
| 99 | cache_control(CACHE_ENABLE); |
| 100 | } |
| 101 | |
| 102 | void icache_disable(void) |
| 103 | { |
| 104 | cache_control(CACHE_DISABLE); |
| 105 | } |
| 106 | |
Tom Rini | 15c6469 | 2024-06-19 15:27:56 -0600 | [diff] [blame] | 107 | void invalidate_icache_all(void) |
| 108 | { |
Marek Vasut | c5afa02 | 2024-09-10 01:18:09 +0200 | [diff] [blame] | 109 | cache_control(CACHE_INVALIDATE); |
Tom Rini | 15c6469 | 2024-06-19 15:27:56 -0600 | [diff] [blame] | 110 | } |
| 111 | |
Vladimir Zapolskiy | f34743c | 2016-11-28 00:15:18 +0200 | [diff] [blame] | 112 | int icache_status(void) |
| 113 | { |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | void dcache_enable(void) |
| 118 | { |
| 119 | } |
| 120 | |
| 121 | void dcache_disable(void) |
| 122 | { |
| 123 | } |
| 124 | |
| 125 | int dcache_status(void) |
| 126 | { |
| 127 | return 0; |
| 128 | } |