123e47edeSVikram Kanigiri /* 2*d7b5f408SJimmy Brisson * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved. 323e47edeSVikram Kanigiri * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 523e47edeSVikram Kanigiri */ 623e47edeSVikram Kanigiri 723e47edeSVikram Kanigiri #include <assert.h> 84213e9baSAntonio Nino Diaz #include <stdbool.h> 902462972SJuan Castillo #include <stdint.h> 1023e47edeSVikram Kanigiri 1109d40e0eSAntonio Nino Diaz #include <arch.h> 1209d40e0eSAntonio Nino Diaz #include <arch_helpers.h> 1309d40e0eSAntonio Nino Diaz #include <common/debug.h> 1409d40e0eSAntonio Nino Diaz #include <drivers/arm/cci.h> 1509d40e0eSAntonio Nino Diaz #include <lib/mmio.h> 1609d40e0eSAntonio Nino Diaz 174213e9baSAntonio Nino Diaz #define MAKE_CCI_PART_NUMBER(hi, lo) (((hi) << 8) | (lo)) 184213e9baSAntonio Nino Diaz #define CCI_PART_LO_MASK U(0xff) 194213e9baSAntonio Nino Diaz #define CCI_PART_HI_MASK U(0xf) 20e33fd445SJeenu Viswambharan 21e33fd445SJeenu Viswambharan /* CCI part number codes read from Peripheral ID registers 0 and 1 */ 22e33fd445SJeenu Viswambharan #define CCI400_PART_NUM 0x420 23e33fd445SJeenu Viswambharan #define CCI500_PART_NUM 0x422 24e33fd445SJeenu Viswambharan #define CCI550_PART_NUM 0x423 25e33fd445SJeenu Viswambharan 26e33fd445SJeenu Viswambharan #define CCI400_SLAVE_PORTS 5 27e33fd445SJeenu Viswambharan #define CCI500_SLAVE_PORTS 7 28e33fd445SJeenu Viswambharan #define CCI550_SLAVE_PORTS 7 29e33fd445SJeenu Viswambharan 30e33fd445SJeenu Viswambharan static uintptr_t cci_base; 31e33fd445SJeenu Viswambharan static const int *cci_slave_if_map; 3223e47edeSVikram Kanigiri 33aa61368eSAntonio Nino Diaz #if ENABLE_ASSERTIONS 34e33fd445SJeenu Viswambharan static unsigned int max_master_id; 35e33fd445SJeenu Viswambharan static int cci_num_slave_ports; 36e33fd445SJeenu Viswambharan 374213e9baSAntonio Nino Diaz static bool validate_cci_map(const int *map) 3823e47edeSVikram Kanigiri { 394213e9baSAntonio Nino Diaz unsigned int valid_cci_map = 0U; 4023e47edeSVikram Kanigiri int slave_if_id; 414213e9baSAntonio Nino Diaz unsigned int i; 4223e47edeSVikram Kanigiri 4323e47edeSVikram Kanigiri /* Validate the map */ 444213e9baSAntonio Nino Diaz for (i = 0U; i <= max_master_id; i++) { 4523e47edeSVikram Kanigiri slave_if_id = map[i]; 4623e47edeSVikram Kanigiri 4723e47edeSVikram Kanigiri if (slave_if_id < 0) 4823e47edeSVikram Kanigiri continue; 4923e47edeSVikram Kanigiri 50e33fd445SJeenu Viswambharan if (slave_if_id >= cci_num_slave_ports) { 5138aecbb4SAntonio Nino Diaz ERROR("Slave interface ID is invalid\n"); 524213e9baSAntonio Nino Diaz return false; 5323e47edeSVikram Kanigiri } 5423e47edeSVikram Kanigiri 55*d7b5f408SJimmy Brisson if ((valid_cci_map & (1UL << slave_if_id)) != 0U) { 5638aecbb4SAntonio Nino Diaz ERROR("Multiple masters are assigned same slave interface ID\n"); 574213e9baSAntonio Nino Diaz return false; 5823e47edeSVikram Kanigiri } 59*d7b5f408SJimmy Brisson valid_cci_map |= 1UL << slave_if_id; 6023e47edeSVikram Kanigiri } 6123e47edeSVikram Kanigiri 624213e9baSAntonio Nino Diaz if (valid_cci_map == 0U) { 6338aecbb4SAntonio Nino Diaz ERROR("No master is assigned a valid slave interface\n"); 644213e9baSAntonio Nino Diaz return false; 6523e47edeSVikram Kanigiri } 6623e47edeSVikram Kanigiri 674213e9baSAntonio Nino Diaz return true; 6823e47edeSVikram Kanigiri } 69e33fd445SJeenu Viswambharan 70e33fd445SJeenu Viswambharan /* 71e33fd445SJeenu Viswambharan * Read CCI part number from Peripheral ID registers 72e33fd445SJeenu Viswambharan */ 73e33fd445SJeenu Viswambharan static unsigned int read_cci_part_number(uintptr_t base) 74e33fd445SJeenu Viswambharan { 75e33fd445SJeenu Viswambharan unsigned int part_lo, part_hi; 76e33fd445SJeenu Viswambharan 77e33fd445SJeenu Viswambharan part_lo = mmio_read_32(base + PERIPHERAL_ID0) & CCI_PART_LO_MASK; 78e33fd445SJeenu Viswambharan part_hi = mmio_read_32(base + PERIPHERAL_ID1) & CCI_PART_HI_MASK; 79e33fd445SJeenu Viswambharan 80e33fd445SJeenu Viswambharan return MAKE_CCI_PART_NUMBER(part_hi, part_lo); 81e33fd445SJeenu Viswambharan } 82e33fd445SJeenu Viswambharan 83e33fd445SJeenu Viswambharan /* 84e33fd445SJeenu Viswambharan * Identify a CCI device, and return the number of slaves. Return -1 for an 85e33fd445SJeenu Viswambharan * unidentified device. 86e33fd445SJeenu Viswambharan */ 87e33fd445SJeenu Viswambharan static int get_slave_ports(unsigned int part_num) 88e33fd445SJeenu Viswambharan { 895aa7498aSJonathan Wright int num_slave_ports = -1; 90e33fd445SJeenu Viswambharan 91e33fd445SJeenu Viswambharan switch (part_num) { 92e33fd445SJeenu Viswambharan 935aa7498aSJonathan Wright case CCI400_PART_NUM: 945aa7498aSJonathan Wright num_slave_ports = CCI400_SLAVE_PORTS; 955aa7498aSJonathan Wright break; 965aa7498aSJonathan Wright case CCI500_PART_NUM: 975aa7498aSJonathan Wright num_slave_ports = CCI500_SLAVE_PORTS; 985aa7498aSJonathan Wright break; 995aa7498aSJonathan Wright case CCI550_PART_NUM: 1005aa7498aSJonathan Wright num_slave_ports = CCI550_SLAVE_PORTS; 1015aa7498aSJonathan Wright break; 102e33fd445SJeenu Viswambharan default: 1035aa7498aSJonathan Wright /* Do nothing in default case */ 1045aa7498aSJonathan Wright break; 105e33fd445SJeenu Viswambharan } 106e33fd445SJeenu Viswambharan 1075aa7498aSJonathan Wright return num_slave_ports; 108e33fd445SJeenu Viswambharan } 109aa61368eSAntonio Nino Diaz #endif /* ENABLE_ASSERTIONS */ 11023e47edeSVikram Kanigiri 111c9263e62SDaniel Boulby void __init cci_init(uintptr_t base, const int *map, 112c9263e62SDaniel Boulby unsigned int num_cci_masters) 11323e47edeSVikram Kanigiri { 1144213e9baSAntonio Nino Diaz assert(map != NULL); 1154213e9baSAntonio Nino Diaz assert(base != 0U); 11623e47edeSVikram Kanigiri 117e33fd445SJeenu Viswambharan cci_base = base; 118e33fd445SJeenu Viswambharan cci_slave_if_map = map; 11923e47edeSVikram Kanigiri 120e33fd445SJeenu Viswambharan #if ENABLE_ASSERTIONS 12123e47edeSVikram Kanigiri /* 12223e47edeSVikram Kanigiri * Master Id's are assigned from zero, So in an array of size n 12323e47edeSVikram Kanigiri * the max master id is (n - 1). 12423e47edeSVikram Kanigiri */ 1254213e9baSAntonio Nino Diaz max_master_id = num_cci_masters - 1U; 126e33fd445SJeenu Viswambharan cci_num_slave_ports = get_slave_ports(read_cci_part_number(base)); 127e33fd445SJeenu Viswambharan #endif 128e33fd445SJeenu Viswambharan assert(cci_num_slave_ports >= 0); 12923e47edeSVikram Kanigiri 13023e47edeSVikram Kanigiri assert(validate_cci_map(map)); 13123e47edeSVikram Kanigiri } 13223e47edeSVikram Kanigiri 13323e47edeSVikram Kanigiri void cci_enable_snoop_dvm_reqs(unsigned int master_id) 13423e47edeSVikram Kanigiri { 135e33fd445SJeenu Viswambharan int slave_if_id = cci_slave_if_map[master_id]; 13623e47edeSVikram Kanigiri 137e33fd445SJeenu Viswambharan assert(master_id <= max_master_id); 138e33fd445SJeenu Viswambharan assert((slave_if_id < cci_num_slave_ports) && (slave_if_id >= 0)); 1394213e9baSAntonio Nino Diaz assert(cci_base != 0U); 14023e47edeSVikram Kanigiri 14123e47edeSVikram Kanigiri /* 14223e47edeSVikram Kanigiri * Enable Snoops and DVM messages, no need for Read/Modify/Write as 14323e47edeSVikram Kanigiri * rest of bits are write ignore 14423e47edeSVikram Kanigiri */ 145e33fd445SJeenu Viswambharan mmio_write_32(cci_base + 146e33fd445SJeenu Viswambharan SLAVE_IFACE_OFFSET(slave_if_id) + SNOOP_CTRL_REG, 147e33fd445SJeenu Viswambharan DVM_EN_BIT | SNOOP_EN_BIT); 14823e47edeSVikram Kanigiri 149ae551a13SRoberto Vargas /* 150ae551a13SRoberto Vargas * Wait for the completion of the write to the Snoop Control Register 151ae551a13SRoberto Vargas * before testing the change_pending bit 152ae551a13SRoberto Vargas */ 153fcb52dbfSRoberto Vargas dsbish(); 154ae551a13SRoberto Vargas 15523e47edeSVikram Kanigiri /* Wait for the dust to settle down */ 1564213e9baSAntonio Nino Diaz while ((mmio_read_32(cci_base + STATUS_REG) & CHANGE_PENDING_BIT) != 0U) 15723e47edeSVikram Kanigiri ; 15823e47edeSVikram Kanigiri } 15923e47edeSVikram Kanigiri 16023e47edeSVikram Kanigiri void cci_disable_snoop_dvm_reqs(unsigned int master_id) 16123e47edeSVikram Kanigiri { 162e33fd445SJeenu Viswambharan int slave_if_id = cci_slave_if_map[master_id]; 16323e47edeSVikram Kanigiri 164e33fd445SJeenu Viswambharan assert(master_id <= max_master_id); 165e33fd445SJeenu Viswambharan assert((slave_if_id < cci_num_slave_ports) && (slave_if_id >= 0)); 1664213e9baSAntonio Nino Diaz assert(cci_base != 0U); 16723e47edeSVikram Kanigiri 16823e47edeSVikram Kanigiri /* 16923e47edeSVikram Kanigiri * Disable Snoops and DVM messages, no need for Read/Modify/Write as 17023e47edeSVikram Kanigiri * rest of bits are write ignore. 17123e47edeSVikram Kanigiri */ 172e33fd445SJeenu Viswambharan mmio_write_32(cci_base + 173e33fd445SJeenu Viswambharan SLAVE_IFACE_OFFSET(slave_if_id) + SNOOP_CTRL_REG, 174e33fd445SJeenu Viswambharan ~(DVM_EN_BIT | SNOOP_EN_BIT)); 17523e47edeSVikram Kanigiri 176ae551a13SRoberto Vargas /* 177ae551a13SRoberto Vargas * Wait for the completion of the write to the Snoop Control Register 178ae551a13SRoberto Vargas * before testing the change_pending bit 179ae551a13SRoberto Vargas */ 180fcb52dbfSRoberto Vargas dsbish(); 181ae551a13SRoberto Vargas 18223e47edeSVikram Kanigiri /* Wait for the dust to settle down */ 1834213e9baSAntonio Nino Diaz while ((mmio_read_32(cci_base + STATUS_REG) & CHANGE_PENDING_BIT) != 0U) 18423e47edeSVikram Kanigiri ; 18523e47edeSVikram Kanigiri } 18623e47edeSVikram Kanigiri 187