2 * Coherency fabric (Aurora) support for Armada 370 and XP platforms.
4 * Copyright (C) 2012 Marvell
6 * Yehuda Yitschak <yehuday@marvell.com>
7 * Gregory Clement <gregory.clement@free-electrons.com>
8 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
10 * This file is licensed under the terms of the GNU General Public
11 * License version 2. This program is licensed "as is" without any
12 * warranty of any kind, whether express or implied.
14 * The Armada 370 and Armada XP SOCs have a coherency fabric which is
15 * responsible for ensuring hardware coherency between all CPUs and between
16 * CPUs and I/O masters. This file initializes the coherency fabric and
17 * supplies basic routines for configuring and controlling hardware coherency
20 #define pr_fmt(fmt) "mvebu-coherency: " fmt
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/of_address.h>
26 #include <linux/smp.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/platform_device.h>
29 #include <linux/slab.h>
30 #include <linux/mbus.h>
31 #include <linux/clk.h>
32 #include <asm/smp_plat.h>
33 #include <asm/cacheflush.h>
34 #include "armada-370-xp.h"
35 #include "coherency.h"
37 unsigned long coherency_phys_base
;
38 static void __iomem
*coherency_base
;
39 static void __iomem
*coherency_cpu_base
;
41 /* Coherency fabric registers */
42 #define COHERENCY_FABRIC_CFG_OFFSET 0x4
44 #define IO_SYNC_BARRIER_CTL_OFFSET 0x0
47 COHERENCY_FABRIC_TYPE_NONE
,
48 COHERENCY_FABRIC_TYPE_ARMADA_370_XP
,
49 COHERENCY_FABRIC_TYPE_ARMADA_375
,
50 COHERENCY_FABRIC_TYPE_ARMADA_380
,
53 static struct of_device_id of_coherency_table
[] = {
54 {.compatible
= "marvell,coherency-fabric",
55 .data
= (void *) COHERENCY_FABRIC_TYPE_ARMADA_370_XP
},
56 {.compatible
= "marvell,armada-375-coherency-fabric",
57 .data
= (void *) COHERENCY_FABRIC_TYPE_ARMADA_375
},
58 {.compatible
= "marvell,armada-380-coherency-fabric",
59 .data
= (void *) COHERENCY_FABRIC_TYPE_ARMADA_380
},
60 { /* end of list */ },
63 /* Function defined in coherency_ll.S */
64 int ll_set_cpu_coherent(void __iomem
*base_addr
, unsigned int hw_cpu_id
);
66 int set_cpu_coherent(unsigned int hw_cpu_id
, int smp_group_id
)
68 if (!coherency_base
) {
69 pr_warn("Can't make CPU %d cache coherent.\n", hw_cpu_id
);
70 pr_warn("Coherency fabric is not initialized\n");
74 return ll_set_cpu_coherent(coherency_base
, hw_cpu_id
);
78 * The below code implements the I/O coherency workaround on Armada
79 * 375. This workaround consists in using the two channels of the
80 * first XOR engine to trigger a XOR transaction that serves as the
81 * I/O coherency barrier.
84 static void __iomem
*xor_base
, *xor_high_base
;
85 static dma_addr_t coherency_wa_buf_phys
[CONFIG_NR_CPUS
];
86 static void *coherency_wa_buf
[CONFIG_NR_CPUS
];
87 static bool coherency_wa_enabled
;
89 #define XOR_CONFIG(chan) (0x10 + (chan * 4))
90 #define XOR_ACTIVATION(chan) (0x20 + (chan * 4))
91 #define WINDOW_BAR_ENABLE(chan) (0x240 + ((chan) << 2))
92 #define WINDOW_BASE(w) (0x250 + ((w) << 2))
93 #define WINDOW_SIZE(w) (0x270 + ((w) << 2))
94 #define WINDOW_REMAP_HIGH(w) (0x290 + ((w) << 2))
95 #define WINDOW_OVERRIDE_CTRL(chan) (0x2A0 + ((chan) << 2))
96 #define XOR_DEST_POINTER(chan) (0x2B0 + (chan * 4))
97 #define XOR_BLOCK_SIZE(chan) (0x2C0 + (chan * 4))
98 #define XOR_INIT_VALUE_LOW 0x2E0
99 #define XOR_INIT_VALUE_HIGH 0x2E4
101 static inline void mvebu_hwcc_armada375_sync_io_barrier_wa(void)
103 int idx
= smp_processor_id();
105 /* Write '1' to the first word of the buffer */
106 writel(0x1, coherency_wa_buf
[idx
]);
108 /* Wait until the engine is idle */
109 while ((readl(xor_base
+ XOR_ACTIVATION(idx
)) >> 4) & 0x3)
114 /* Trigger channel */
115 writel(0x1, xor_base
+ XOR_ACTIVATION(idx
));
117 /* Poll the data until it is cleared by the XOR transaction */
118 while (readl(coherency_wa_buf
[idx
]))
122 static void __init
armada_375_coherency_init_wa(void)
124 const struct mbus_dram_target_info
*dram
;
125 struct device_node
*xor_node
;
126 struct property
*xor_status
;
131 pr_warn("enabling coherency workaround for Armada 375 Z1, one XOR engine disabled\n");
134 * Since the workaround uses one XOR engine, we grab a
135 * reference to its Device Tree node first.
137 xor_node
= of_find_compatible_node(NULL
, NULL
, "marvell,orion-xor");
141 * Then we mark it as disabled so that the real XOR driver
144 xor_status
= kzalloc(sizeof(struct property
), GFP_KERNEL
);
147 xor_status
->value
= kstrdup("disabled", GFP_KERNEL
);
148 BUG_ON(!xor_status
->value
);
150 xor_status
->length
= 8;
151 xor_status
->name
= kstrdup("status", GFP_KERNEL
);
152 BUG_ON(!xor_status
->name
);
154 of_update_property(xor_node
, xor_status
);
157 * And we remap the registers, get the clock, and do the
158 * initial configuration of the XOR engine.
160 xor_base
= of_iomap(xor_node
, 0);
161 xor_high_base
= of_iomap(xor_node
, 1);
163 xor_clk
= of_clk_get_by_name(xor_node
, NULL
);
166 clk_prepare_enable(xor_clk
);
168 dram
= mv_mbus_dram_info();
170 for (i
= 0; i
< 8; i
++) {
171 writel(0, xor_base
+ WINDOW_BASE(i
));
172 writel(0, xor_base
+ WINDOW_SIZE(i
));
174 writel(0, xor_base
+ WINDOW_REMAP_HIGH(i
));
177 for (i
= 0; i
< dram
->num_cs
; i
++) {
178 const struct mbus_dram_window
*cs
= dram
->cs
+ i
;
179 writel((cs
->base
& 0xffff0000) |
180 (cs
->mbus_attr
<< 8) |
181 dram
->mbus_dram_target_id
, xor_base
+ WINDOW_BASE(i
));
182 writel((cs
->size
- 1) & 0xffff0000, xor_base
+ WINDOW_SIZE(i
));
184 win_enable
|= (1 << i
);
185 win_enable
|= 3 << (16 + (2 * i
));
188 writel(win_enable
, xor_base
+ WINDOW_BAR_ENABLE(0));
189 writel(win_enable
, xor_base
+ WINDOW_BAR_ENABLE(1));
190 writel(0, xor_base
+ WINDOW_OVERRIDE_CTRL(0));
191 writel(0, xor_base
+ WINDOW_OVERRIDE_CTRL(1));
193 for (i
= 0; i
< CONFIG_NR_CPUS
; i
++) {
194 coherency_wa_buf
[i
] = kzalloc(PAGE_SIZE
, GFP_KERNEL
);
195 BUG_ON(!coherency_wa_buf
[i
]);
198 * We can't use the DMA mapping API, since we don't
199 * have a valid 'struct device' pointer
201 coherency_wa_buf_phys
[i
] =
202 virt_to_phys(coherency_wa_buf
[i
]);
203 BUG_ON(!coherency_wa_buf_phys
[i
]);
206 * Configure the XOR engine for memset operation, with
207 * a 128 bytes block size
209 writel(0x444, xor_base
+ XOR_CONFIG(i
));
210 writel(128, xor_base
+ XOR_BLOCK_SIZE(i
));
211 writel(coherency_wa_buf_phys
[i
],
212 xor_base
+ XOR_DEST_POINTER(i
));
215 writel(0x0, xor_base
+ XOR_INIT_VALUE_LOW
);
216 writel(0x0, xor_base
+ XOR_INIT_VALUE_HIGH
);
218 coherency_wa_enabled
= true;
221 static inline void mvebu_hwcc_sync_io_barrier(void)
223 if (coherency_wa_enabled
) {
224 mvebu_hwcc_armada375_sync_io_barrier_wa();
228 writel(0x1, coherency_cpu_base
+ IO_SYNC_BARRIER_CTL_OFFSET
);
229 while (readl(coherency_cpu_base
+ IO_SYNC_BARRIER_CTL_OFFSET
) & 0x1);
232 static dma_addr_t
mvebu_hwcc_dma_map_page(struct device
*dev
, struct page
*page
,
233 unsigned long offset
, size_t size
,
234 enum dma_data_direction dir
,
235 struct dma_attrs
*attrs
)
237 if (dir
!= DMA_TO_DEVICE
)
238 mvebu_hwcc_sync_io_barrier();
239 return pfn_to_dma(dev
, page_to_pfn(page
)) + offset
;
243 static void mvebu_hwcc_dma_unmap_page(struct device
*dev
, dma_addr_t dma_handle
,
244 size_t size
, enum dma_data_direction dir
,
245 struct dma_attrs
*attrs
)
247 if (dir
!= DMA_TO_DEVICE
)
248 mvebu_hwcc_sync_io_barrier();
251 static void mvebu_hwcc_dma_sync(struct device
*dev
, dma_addr_t dma_handle
,
252 size_t size
, enum dma_data_direction dir
)
254 if (dir
!= DMA_TO_DEVICE
)
255 mvebu_hwcc_sync_io_barrier();
258 static struct dma_map_ops mvebu_hwcc_dma_ops
= {
259 .alloc
= arm_dma_alloc
,
260 .free
= arm_dma_free
,
261 .mmap
= arm_dma_mmap
,
262 .map_page
= mvebu_hwcc_dma_map_page
,
263 .unmap_page
= mvebu_hwcc_dma_unmap_page
,
264 .get_sgtable
= arm_dma_get_sgtable
,
265 .map_sg
= arm_dma_map_sg
,
266 .unmap_sg
= arm_dma_unmap_sg
,
267 .sync_single_for_cpu
= mvebu_hwcc_dma_sync
,
268 .sync_single_for_device
= mvebu_hwcc_dma_sync
,
269 .sync_sg_for_cpu
= arm_dma_sync_sg_for_cpu
,
270 .sync_sg_for_device
= arm_dma_sync_sg_for_device
,
271 .set_dma_mask
= arm_dma_set_mask
,
274 static int mvebu_hwcc_platform_notifier(struct notifier_block
*nb
,
275 unsigned long event
, void *__dev
)
277 struct device
*dev
= __dev
;
279 if (event
!= BUS_NOTIFY_ADD_DEVICE
)
281 set_dma_ops(dev
, &mvebu_hwcc_dma_ops
);
286 static struct notifier_block mvebu_hwcc_platform_nb
= {
287 .notifier_call
= mvebu_hwcc_platform_notifier
,
290 static void __init
armada_370_coherency_init(struct device_node
*np
)
294 of_address_to_resource(np
, 0, &res
);
295 coherency_phys_base
= res
.start
;
297 * Ensure secondary CPUs will see the updated value,
298 * which they read before they join the coherency
299 * fabric, and therefore before they are coherent with
300 * the boot CPU cache.
302 sync_cache_w(&coherency_phys_base
);
303 coherency_base
= of_iomap(np
, 0);
304 coherency_cpu_base
= of_iomap(np
, 1);
305 set_cpu_coherent(cpu_logical_map(smp_processor_id()), 0);
308 static void __init
armada_375_380_coherency_init(struct device_node
*np
)
310 coherency_cpu_base
= of_iomap(np
, 0);
313 static int coherency_type(void)
315 struct device_node
*np
;
316 const struct of_device_id
*match
;
318 np
= of_find_matching_node_and_match(NULL
, of_coherency_table
, &match
);
320 int type
= (int) match
->data
;
322 /* Armada 370/XP coherency works in both UP and SMP */
323 if (type
== COHERENCY_FABRIC_TYPE_ARMADA_370_XP
)
326 /* Armada 375 coherency works only on SMP */
327 else if (type
== COHERENCY_FABRIC_TYPE_ARMADA_375
&& is_smp())
330 /* Armada 380 coherency works only on SMP */
331 else if (type
== COHERENCY_FABRIC_TYPE_ARMADA_380
&& is_smp())
337 return COHERENCY_FABRIC_TYPE_NONE
;
340 int coherency_available(void)
342 return coherency_type() != COHERENCY_FABRIC_TYPE_NONE
;
345 int __init
coherency_init(void)
347 int type
= coherency_type();
348 struct device_node
*np
;
350 np
= of_find_matching_node(NULL
, of_coherency_table
);
352 if (type
== COHERENCY_FABRIC_TYPE_ARMADA_370_XP
)
353 armada_370_coherency_init(np
);
354 else if (type
== COHERENCY_FABRIC_TYPE_ARMADA_375
||
355 type
== COHERENCY_FABRIC_TYPE_ARMADA_380
)
356 armada_375_380_coherency_init(np
);
361 static int __init
coherency_late_init(void)
363 int type
= coherency_type();
365 if (type
== COHERENCY_FABRIC_TYPE_NONE
)
368 if (type
== COHERENCY_FABRIC_TYPE_ARMADA_375
)
369 armada_375_coherency_init_wa();
371 bus_register_notifier(&platform_bus_type
,
372 &mvebu_hwcc_platform_nb
);
377 postcore_initcall(coherency_late_init
);