ARM: uniphier: refactor outer cache operation slightly
[oweals/u-boot.git] / arch / arm / mach-uniphier / cache_uniphier.c
1 /*
2  * Copyright (C) 2012-2015 Masahiro Yamada <yamada.masahiro@socionext.com>
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6
7 #include <common.h>
8 #include <linux/io.h>
9 #include <asm/armv7.h>
10
11 #include "ssc-regs.h"
12
13 #ifdef CONFIG_UNIPHIER_L2CACHE_ON
14 static void uniphier_cache_maint_all(u32 operation)
15 {
16         /* try until the command is successfully set */
17         do {
18                 writel(SSCOQM_S_ALL | SSCOQM_CE | operation, SSCOQM);
19         } while (readl(SSCOPPQSEF) & (SSCOPPQSEF_FE | SSCOPPQSEF_OE));
20
21         /* wait until the operation is completed */
22         while (readl(SSCOLPQS) != SSCOLPQS_EF)
23                 ;
24
25         /* clear the complete notification flag */
26         writel(SSCOLPQS_EF, SSCOLPQS);
27
28         writel(SSCOPE_CM_SYNC, SSCOPE); /* drain internal buffers */
29         readl(SSCOPE); /* need a read back to confirm */
30 }
31
32 void v7_outer_cache_flush_all(void)
33 {
34         uniphier_cache_maint_all(SSCOQM_CM_WB_INV);
35 }
36
37 void v7_outer_cache_inval_all(void)
38 {
39         uniphier_cache_maint_all(SSCOQM_CM_INV);
40 }
41
42 static void __uniphier_cache_maint_range(u32 start, u32 size, u32 operation)
43 {
44         /* try until the command is successfully set */
45         do {
46                 writel(SSCOQM_S_ADDRESS | SSCOQM_CE | operation, SSCOQM);
47                 writel(start, SSCOQAD);
48                 writel(size, SSCOQSZ);
49
50         } while (readl(SSCOPPQSEF) & (SSCOPPQSEF_FE | SSCOPPQSEF_OE));
51
52         /* wait until the operation is completed */
53         while (readl(SSCOLPQS) != SSCOLPQS_EF)
54                 ;
55
56         /* clear the complete notification flag */
57         writel(SSCOLPQS_EF, SSCOLPQS);
58 }
59
60 static void uniphier_cache_maint_range(u32 start, u32 end, u32 operation)
61 {
62         u32 size;
63
64         /*
65          * If start address is not aligned to cache-line,
66          * do cache operation for the first cache-line
67          */
68         start = start & ~(SSC_LINE_SIZE - 1);
69
70         size = end - start;
71
72         if (unlikely(size >= (u32)(-SSC_LINE_SIZE))) {
73                 /* this means cache operation for all range */
74                 uniphier_cache_maint_all(operation);
75                 return;
76         }
77
78         /*
79          * If end address is not aligned to cache-line,
80          * do cache operation for the last cache-line
81          */
82         size = ALIGN(size, SSC_LINE_SIZE);
83
84         while (size) {
85                 u32 chunk_size = size > SSC_RANGE_OP_MAX_SIZE ?
86                                                 SSC_RANGE_OP_MAX_SIZE : size;
87                 __uniphier_cache_maint_range(start, chunk_size, operation);
88
89                 start += chunk_size;
90                 size -= chunk_size;
91         }
92
93         writel(SSCOPE_CM_SYNC, SSCOPE); /* drain internal buffers */
94         readl(SSCOPE); /* need a read back to confirm */
95 }
96
97 void v7_outer_cache_flush_range(u32 start, u32 end)
98 {
99         uniphier_cache_maint_range(start, end, SSCOQM_CM_WB_INV);
100 }
101
102 void v7_outer_cache_inval_range(u32 start, u32 end)
103 {
104         uniphier_cache_maint_range(start, end, SSCOQM_CM_INV);
105 }
106
107 void v7_outer_cache_enable(void)
108 {
109         u32 tmp;
110         tmp = readl(SSCC);
111         tmp |= SSCC_ON;
112         writel(tmp, SSCC);
113 }
114 #endif
115
116 void v7_outer_cache_disable(void)
117 {
118         u32 tmp;
119         tmp = readl(SSCC);
120         tmp &= ~SSCC_ON;
121         writel(tmp, SSCC);
122 }
123
124 void enable_caches(void)
125 {
126         dcache_enable();
127 }