1 /*
2  * Copyright (c) 2019, Intel Corporation. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <common/debug.h>
9 #include <errno.h>
10 #include <lib/mmio.h>
11 
12 #include "ncore_ccu.h"
13 
14 uint32_t poll_active_bit(uint32_t dir);
15 
16 static coh_ss_id_t subsystem_id;
17 
18 
get_subsystem_id(void)19 void get_subsystem_id(void)
20 {
21 	uint32_t snoop_filter, directory, coh_agent;
22 
23 	snoop_filter = CSIDR_NUM_SF(mmio_read_32(NCORE_CCU_CSR(NCORE_CSIDR)));
24 	directory = CSUIDR_NUM_DIR(mmio_read_32(NCORE_CCU_CSR(NCORE_CSUIDR)));
25 	coh_agent = CSUIDR_NUM_CAI(mmio_read_32(NCORE_CCU_CSR(NCORE_CSUIDR)));
26 
27 	subsystem_id.num_snoop_filter = snoop_filter + 1;
28 	subsystem_id.num_directory = directory;
29 	subsystem_id.num_coh_agent = coh_agent;
30 }
31 
directory_init(void)32 uint32_t directory_init(void)
33 {
34 	uint32_t dir_sf_mtn, dir_sf_en;
35 	uint32_t dir, sf, ret;
36 
37 	for (dir = 0; dir < subsystem_id.num_directory; dir++) {
38 		for (sf = 0; sf < subsystem_id.num_snoop_filter; sf++) {
39 			dir_sf_mtn = DIRECTORY_UNIT(dir, NCORE_DIRUSFMCR);
40 			dir_sf_en = DIRECTORY_UNIT(dir, NCORE_DIRUSFER);
41 
42 			/* Initialize All Entries */
43 			mmio_write_32(dir_sf_mtn, SNOOP_FILTER_ID(dir));
44 
45 			/* Poll Active Bit */
46 			ret = poll_active_bit(dir);
47 			if (ret != 0) {
48 				ERROR("Timeout during active bit polling");
49 				return -ETIMEDOUT;
50 			}
51 
52 			/* Snoope Filter Enable */
53 			mmio_setbits_32(dir_sf_en, BIT(sf));
54 		}
55 	}
56 
57 	return 0;
58 }
59 
coherent_agent_intfc_init(void)60 uint32_t coherent_agent_intfc_init(void)
61 {
62 	uint32_t dir, ca, ca_id, ca_type, ca_snoop_en;
63 
64 	for (dir = 0; dir < subsystem_id.num_directory; dir++) {
65 		for (ca = 0; ca < subsystem_id.num_coh_agent; ca++) {
66 			ca_snoop_en = DIRECTORY_UNIT(ca, NCORE_DIRUCASER0);
67 			ca_id = mmio_read_32(COH_AGENT_UNIT(ca, NCORE_CAIUIDR));
68 
69 			/* Coh Agent Snoop Enable */
70 			if (CACHING_AGENT_BIT(ca_id))
71 				mmio_write_32(ca_snoop_en, BIT(ca));
72 
73 			/* Coh Agent Snoop DVM Enable */
74 			ca_type = CACHING_AGENT_TYPE(ca_id);
75 			if (ca_type == ACE_W_DVM || ca_type == ACE_L_W_DVM)
76 				mmio_write_32(NCORE_CCU_CSR(NCORE_CSADSER0),
77 				BIT(ca));
78 		}
79 	}
80 
81 	return 0;
82 }
83 
poll_active_bit(uint32_t dir)84 uint32_t poll_active_bit(uint32_t dir)
85 {
86 	uint32_t timeout = 80000;
87 	uint32_t poll_dir =  DIRECTORY_UNIT(dir, NCORE_DIRUSFMAR);
88 
89 	while (timeout > 0) {
90 		if (mmio_read_32(poll_dir) == 0)
91 			return 0;
92 		timeout--;
93 	}
94 
95 	return -1;
96 }
97 
bypass_ocram_firewall(void)98 void bypass_ocram_firewall(void)
99 {
100 	mmio_clrbits_32(COH_CPU0_BYPASS_REG(NCORE_FW_OCRAM_BLK_CGF1),
101 			OCRAM_PRIVILEGED_MASK | OCRAM_SECURE_MASK);
102 	mmio_clrbits_32(COH_CPU0_BYPASS_REG(NCORE_FW_OCRAM_BLK_CGF2),
103 			OCRAM_PRIVILEGED_MASK | OCRAM_SECURE_MASK);
104 	mmio_clrbits_32(COH_CPU0_BYPASS_REG(NCORE_FW_OCRAM_BLK_CGF3),
105 			OCRAM_PRIVILEGED_MASK | OCRAM_SECURE_MASK);
106 	mmio_clrbits_32(COH_CPU0_BYPASS_REG(NCORE_FW_OCRAM_BLK_CGF4),
107 			OCRAM_PRIVILEGED_MASK | OCRAM_SECURE_MASK);
108 }
109 
init_ncore_ccu(void)110 uint32_t init_ncore_ccu(void)
111 {
112 	uint32_t status;
113 
114 	get_subsystem_id();
115 	status = directory_init();
116 	status = coherent_agent_intfc_init();
117 	bypass_ocram_firewall();
118 
119 	return status;
120 }
121