cci.c 4.24 KB
Newer Older
1
/*
2
 * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
5
6
7
 */

#include <assert.h>
8
#include <stdbool.h>
9
#include <stdint.h>
10

11
12
13
14
15
16
#include <arch.h>
#include <arch_helpers.h>
#include <common/debug.h>
#include <drivers/arm/cci.h>
#include <lib/mmio.h>

17
18
19
#define MAKE_CCI_PART_NUMBER(hi, lo)	(((hi) << 8) | (lo))
#define CCI_PART_LO_MASK		U(0xff)
#define CCI_PART_HI_MASK		U(0xf)
20
21
22
23
24
25
26
27
28
29
30
31

/* CCI part number codes read from Peripheral ID registers 0 and 1 */
#define CCI400_PART_NUM		0x420
#define CCI500_PART_NUM		0x422
#define CCI550_PART_NUM		0x423

#define CCI400_SLAVE_PORTS	5
#define CCI500_SLAVE_PORTS	7
#define CCI550_SLAVE_PORTS	7

static uintptr_t cci_base;
static const int *cci_slave_if_map;
32

33
#if ENABLE_ASSERTIONS
34
35
36
static unsigned int max_master_id;
static int cci_num_slave_ports;

37
static bool validate_cci_map(const int *map)
38
{
39
	unsigned int valid_cci_map = 0U;
40
	int slave_if_id;
41
	unsigned int i;
42
43

	/* Validate the map */
44
	for (i = 0U; i <= max_master_id; i++) {
45
46
47
48
49
		slave_if_id = map[i];

		if (slave_if_id < 0)
			continue;

50
		if (slave_if_id >= cci_num_slave_ports) {
51
			ERROR("Slave interface ID is invalid\n");
52
			return false;
53
54
		}

55
		if ((valid_cci_map & (1U << slave_if_id)) != 0U) {
56
			ERROR("Multiple masters are assigned same slave interface ID\n");
57
			return false;
58
		}
59
		valid_cci_map |= 1U << slave_if_id;
60
61
	}

62
	if (valid_cci_map == 0U) {
63
		ERROR("No master is assigned a valid slave interface\n");
64
		return false;
65
66
	}

67
	return true;
68
}
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

/*
 * Read CCI part number from Peripheral ID registers
 */
static unsigned int read_cci_part_number(uintptr_t base)
{
	unsigned int part_lo, part_hi;

	part_lo = mmio_read_32(base + PERIPHERAL_ID0) & CCI_PART_LO_MASK;
	part_hi = mmio_read_32(base + PERIPHERAL_ID1) & CCI_PART_HI_MASK;

	return MAKE_CCI_PART_NUMBER(part_hi, part_lo);
}

/*
 * Identify a CCI device, and return the number of slaves. Return -1 for an
 * unidentified device.
 */
static int get_slave_ports(unsigned int part_num)
{
89
	int num_slave_ports = -1;
90
91
92

	switch (part_num) {

93
94
95
96
97
98
99
100
101
	case CCI400_PART_NUM:
		num_slave_ports = CCI400_SLAVE_PORTS;
		break;
	case CCI500_PART_NUM:
		num_slave_ports = CCI500_SLAVE_PORTS;
		break;
	case CCI550_PART_NUM:
		num_slave_ports = CCI550_SLAVE_PORTS;
		break;
102
	default:
103
104
		/* Do nothing in default case */
		break;
105
106
	}

107
	return num_slave_ports;
108
}
109
#endif /* ENABLE_ASSERTIONS */
110

111
112
void __init cci_init(uintptr_t base, const int *map,
				unsigned int num_cci_masters)
113
{
114
115
	assert(map != NULL);
	assert(base != 0U);
116

117
118
	cci_base = base;
	cci_slave_if_map = map;
119

120
#if ENABLE_ASSERTIONS
121
122
123
124
	/*
	 * Master Id's are assigned from zero, So in an array of size n
	 * the max master id is (n - 1).
	 */
125
	max_master_id = num_cci_masters - 1U;
126
127
128
	cci_num_slave_ports = get_slave_ports(read_cci_part_number(base));
#endif
	assert(cci_num_slave_ports >= 0);
129
130
131
132
133
134

	assert(validate_cci_map(map));
}

void cci_enable_snoop_dvm_reqs(unsigned int master_id)
{
135
	int slave_if_id = cci_slave_if_map[master_id];
136

137
138
	assert(master_id <= max_master_id);
	assert((slave_if_id < cci_num_slave_ports) && (slave_if_id >= 0));
139
	assert(cci_base != 0U);
140
141
142
143
144

	/*
	 * Enable Snoops and DVM messages, no need for Read/Modify/Write as
	 * rest of bits are write ignore
	 */
145
146
147
	mmio_write_32(cci_base +
		      SLAVE_IFACE_OFFSET(slave_if_id) + SNOOP_CTRL_REG,
		      DVM_EN_BIT | SNOOP_EN_BIT);
148

149
150
151
152
	/*
	 * Wait for the completion of the write to the Snoop Control Register
	 * before testing the change_pending bit
	 */
153
	dsbish();
154

155
	/* Wait for the dust to settle down */
156
	while ((mmio_read_32(cci_base + STATUS_REG) & CHANGE_PENDING_BIT) != 0U)
157
158
159
160
161
		;
}

void cci_disable_snoop_dvm_reqs(unsigned int master_id)
{
162
	int slave_if_id = cci_slave_if_map[master_id];
163

164
165
	assert(master_id <= max_master_id);
	assert((slave_if_id < cci_num_slave_ports) && (slave_if_id >= 0));
166
	assert(cci_base != 0U);
167
168
169
170
171

	/*
	 * Disable Snoops and DVM messages, no need for Read/Modify/Write as
	 * rest of bits are write ignore.
	 */
172
173
174
	mmio_write_32(cci_base +
		      SLAVE_IFACE_OFFSET(slave_if_id) + SNOOP_CTRL_REG,
		      ~(DVM_EN_BIT | SNOOP_EN_BIT));
175

176
177
178
179
	/*
	 * Wait for the completion of the write to the Snoop Control Register
	 * before testing the change_pending bit
	 */
180
	dsbish();
181

182
	/* Wait for the dust to settle down */
183
	while ((mmio_read_32(cci_base + STATUS_REG) & CHANGE_PENDING_BIT) != 0U)
184
185
186
		;
}