Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Thomas Chou | 741085b | 2015-10-23 07:58:20 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw> |
| 4 | * Copyright (C) 2009, Wind River Systems Inc |
| 5 | * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com |
Thomas Chou | 741085b | 2015-10-23 07:58:20 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 8 | #include <common.h> |
Simon Glass | 1d91ba7 | 2019-11-14 12:57:37 -0700 | [diff] [blame] | 9 | #include <cpu_func.h> |
Thomas Chou | 741085b | 2015-10-23 07:58:20 +0800 | [diff] [blame] | 10 | #include <asm/cache.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Thomas Chou | 741085b | 2015-10-23 07:58:20 +0800 | [diff] [blame] | 12 | |
| 13 | DECLARE_GLOBAL_DATA_PTR; |
| 14 | |
| 15 | static void __flush_dcache(unsigned long start, unsigned long end) |
| 16 | { |
| 17 | unsigned long addr; |
| 18 | |
| 19 | start &= ~(gd->arch.dcache_line_size - 1); |
| 20 | end += (gd->arch.dcache_line_size - 1); |
| 21 | end &= ~(gd->arch.dcache_line_size - 1); |
| 22 | |
| 23 | for (addr = start; addr < end; addr += gd->arch.dcache_line_size) { |
| 24 | __asm__ __volatile__ (" flushda 0(%0)\n" |
| 25 | : /* Outputs */ |
| 26 | : /* Inputs */ "r"(addr) |
| 27 | /* : No clobber */); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | static void __flush_dcache_all(unsigned long start, unsigned long end) |
| 32 | { |
| 33 | unsigned long addr; |
| 34 | |
| 35 | start &= ~(gd->arch.dcache_line_size - 1); |
| 36 | end += (gd->arch.dcache_line_size - 1); |
| 37 | end &= ~(gd->arch.dcache_line_size - 1); |
| 38 | |
| 39 | if (end > start + gd->arch.dcache_size) |
| 40 | end = start + gd->arch.dcache_size; |
| 41 | |
| 42 | for (addr = start; addr < end; addr += gd->arch.dcache_line_size) { |
| 43 | __asm__ __volatile__ (" flushd 0(%0)\n" |
| 44 | : /* Outputs */ |
| 45 | : /* Inputs */ "r"(addr) |
| 46 | /* : No clobber */); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | static void __invalidate_dcache(unsigned long start, unsigned long end) |
| 51 | { |
| 52 | unsigned long addr; |
| 53 | |
| 54 | start &= ~(gd->arch.dcache_line_size - 1); |
| 55 | end += (gd->arch.dcache_line_size - 1); |
| 56 | end &= ~(gd->arch.dcache_line_size - 1); |
| 57 | |
| 58 | for (addr = start; addr < end; addr += gd->arch.dcache_line_size) { |
| 59 | __asm__ __volatile__ (" initda 0(%0)\n" |
| 60 | : /* Outputs */ |
| 61 | : /* Inputs */ "r"(addr) |
| 62 | /* : No clobber */); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static void __flush_icache(unsigned long start, unsigned long end) |
| 67 | { |
| 68 | unsigned long addr; |
| 69 | |
| 70 | start &= ~(gd->arch.icache_line_size - 1); |
| 71 | end += (gd->arch.icache_line_size - 1); |
| 72 | end &= ~(gd->arch.icache_line_size - 1); |
| 73 | |
| 74 | if (end > start + gd->arch.icache_size) |
| 75 | end = start + gd->arch.icache_size; |
| 76 | |
| 77 | for (addr = start; addr < end; addr += gd->arch.icache_line_size) { |
| 78 | __asm__ __volatile__ (" flushi %0\n" |
| 79 | : /* Outputs */ |
| 80 | : /* Inputs */ "r"(addr) |
| 81 | /* : No clobber */); |
| 82 | } |
| 83 | __asm__ __volatile(" flushp\n"); |
| 84 | } |
| 85 | |
| 86 | void flush_dcache_all(void) |
| 87 | { |
| 88 | __flush_dcache_all(0, gd->arch.dcache_size); |
| 89 | __flush_icache(0, gd->arch.icache_size); |
| 90 | } |
| 91 | |
| 92 | void flush_dcache_range(unsigned long start, unsigned long end) |
| 93 | { |
| 94 | if (gd->arch.has_initda) |
| 95 | __flush_dcache(start, end); |
| 96 | else |
| 97 | __flush_dcache_all(start, end); |
| 98 | } |
| 99 | |
| 100 | void flush_cache(unsigned long start, unsigned long size) |
| 101 | { |
| 102 | if (gd->arch.has_initda) |
| 103 | __flush_dcache(start, start + size); |
| 104 | else |
| 105 | __flush_dcache_all(start, start + size); |
| 106 | __flush_icache(start, start + size); |
| 107 | } |
| 108 | |
| 109 | void invalidate_dcache_range(unsigned long start, unsigned long end) |
| 110 | { |
| 111 | if (gd->arch.has_initda) |
| 112 | __invalidate_dcache(start, end); |
| 113 | else |
| 114 | __flush_dcache_all(start, end); |
| 115 | } |
| 116 | |
| 117 | int dcache_status(void) |
| 118 | { |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | void dcache_enable(void) |
| 123 | { |
| 124 | flush_dcache_all(); |
| 125 | } |
| 126 | |
| 127 | void dcache_disable(void) |
| 128 | { |
| 129 | flush_dcache_all(); |
| 130 | } |