1 /* Copyright 2018 SiFive, Inc */
2 /* SPDX-License-Identifier: Apache-2.0 */
4 #include <metal/machine/platform.h>
6 #ifdef METAL_SIFIVE_FU540_C000_L2
10 #include <metal/drivers/sifive_fu540-c000_l2.h>
11 #include <metal/machine.h>
13 #define L2_CONFIG_WAYS_SHIFT 8
14 #define L2_CONFIG_WAYS_MASK (0xFF << L2_CONFIG_WAYS_SHIFT)
16 void __metal_driver_sifive_fu540_c000_l2_init(struct metal_cache *l2, int ways);
18 static void metal_driver_sifive_fu540_c000_l2_init(void) __attribute__((constructor));
19 static void metal_driver_sifive_fu540_c000_l2_init(void)
21 #ifdef __METAL_DT_SIFIVE_FU540_C000_L2_HANDLE
22 /* Get the handle for the L2 cache controller */
23 struct metal_cache *l2 = __METAL_DT_SIFIVE_FU540_C000_L2_HANDLE;
28 /* Get the number of available ways per bank */
29 unsigned long control_base = __metal_driver_sifive_fu540_c000_l2_control_base(l2);
30 uint32_t ways = __METAL_ACCESS_ONCE((__metal_io_u32 *)(control_base + METAL_SIFIVE_FU540_C000_L2_CONFIG));
31 ways = ((ways & L2_CONFIG_WAYS_MASK) >> L2_CONFIG_WAYS_SHIFT);
33 /* Enable all the ways */
34 __metal_driver_sifive_fu540_c000_l2_init(l2, ways);
38 void __metal_driver_sifive_fu540_c000_l2_init(struct metal_cache *l2, int ways)
40 metal_cache_set_enabled_ways(l2, ways);
43 int __metal_driver_sifive_fu540_c000_l2_get_enabled_ways(struct metal_cache *cache)
45 unsigned long control_base = __metal_driver_sifive_fu540_c000_l2_control_base(cache);
47 uint32_t way_enable = __METAL_ACCESS_ONCE((__metal_io_u32 *)(control_base + METAL_SIFIVE_FU540_C000_L2_WAYENABLE));
49 /* The stored number is the index, so add one */
50 return (0xFF & way_enable) + 1;
53 int __metal_driver_sifive_fu540_c000_l2_set_enabled_ways(struct metal_cache *cache, int ways)
55 unsigned long control_base = __metal_driver_sifive_fu540_c000_l2_control_base(cache);
57 /* We can't decrease the number of enabled ways */
58 if(metal_cache_get_enabled_ways(cache) > ways) {
62 /* The stored value is the index, so subtract one */
63 uint32_t value = 0xFF & (ways - 1);
65 /* Set the number of enabled ways */
66 __METAL_ACCESS_ONCE((__metal_io_u32 *)(control_base + METAL_SIFIVE_FU540_C000_L2_WAYENABLE)) = value;
68 /* Make sure the number of ways was set correctly */
69 if(metal_cache_get_enabled_ways(cache) != ways) {
76 __METAL_DEFINE_VTABLE(__metal_driver_vtable_sifive_fu540_c000_l2) = {
77 .cache.init = __metal_driver_sifive_fu540_c000_l2_init,
78 .cache.get_enabled_ways = __metal_driver_sifive_fu540_c000_l2_get_enabled_ways,
79 .cache.set_enabled_ways = __metal_driver_sifive_fu540_c000_l2_set_enabled_ways,
84 typedef int no_empty_translation_units;