1*a1820845SGatien Chevallier // SPDX-License-Identifier: GPL-2.0-only 2*a1820845SGatien Chevallier /* 3*a1820845SGatien Chevallier * Copyright (C) 2023, STMicroelectronics - All Rights Reserved 4*a1820845SGatien Chevallier */ 5*a1820845SGatien Chevallier 6*a1820845SGatien Chevallier #include <linux/bitfield.h> 7*a1820845SGatien Chevallier #include <linux/bits.h> 8*a1820845SGatien Chevallier #include <linux/device.h> 9*a1820845SGatien Chevallier #include <linux/err.h> 10*a1820845SGatien Chevallier #include <linux/init.h> 11*a1820845SGatien Chevallier #include <linux/io.h> 12*a1820845SGatien Chevallier #include <linux/kernel.h> 13*a1820845SGatien Chevallier #include <linux/module.h> 14*a1820845SGatien Chevallier #include <linux/of.h> 15*a1820845SGatien Chevallier #include <linux/of_platform.h> 16*a1820845SGatien Chevallier #include <linux/platform_device.h> 17*a1820845SGatien Chevallier #include <linux/types.h> 18*a1820845SGatien Chevallier 19*a1820845SGatien Chevallier #include "stm32_firewall.h" 20*a1820845SGatien Chevallier 21*a1820845SGatien Chevallier /* 22*a1820845SGatien Chevallier * RIFSC offset register 23*a1820845SGatien Chevallier */ 24*a1820845SGatien Chevallier #define RIFSC_RISC_SECCFGR0 0x10 25*a1820845SGatien Chevallier #define RIFSC_RISC_PRIVCFGR0 0x30 26*a1820845SGatien Chevallier #define RIFSC_RISC_PER0_CIDCFGR 0x100 27*a1820845SGatien Chevallier #define RIFSC_RISC_PER0_SEMCR 0x104 28*a1820845SGatien Chevallier #define RIFSC_RISC_HWCFGR2 0xFEC 29*a1820845SGatien Chevallier 30*a1820845SGatien Chevallier /* 31*a1820845SGatien Chevallier * SEMCR register 32*a1820845SGatien Chevallier */ 33*a1820845SGatien Chevallier #define SEMCR_MUTEX BIT(0) 34*a1820845SGatien Chevallier 35*a1820845SGatien Chevallier /* 36*a1820845SGatien Chevallier * HWCFGR2 register 37*a1820845SGatien Chevallier */ 38*a1820845SGatien Chevallier #define HWCFGR2_CONF1_MASK GENMASK(15, 0) 39*a1820845SGatien Chevallier #define HWCFGR2_CONF2_MASK GENMASK(23, 16) 40*a1820845SGatien Chevallier #define HWCFGR2_CONF3_MASK GENMASK(31, 24) 41*a1820845SGatien Chevallier 42*a1820845SGatien Chevallier /* 43*a1820845SGatien Chevallier * RIFSC miscellaneous 44*a1820845SGatien Chevallier */ 45*a1820845SGatien Chevallier #define RIFSC_RISC_CFEN_MASK BIT(0) 46*a1820845SGatien Chevallier #define RIFSC_RISC_SEM_EN_MASK BIT(1) 47*a1820845SGatien Chevallier #define RIFSC_RISC_SCID_MASK GENMASK(6, 4) 48*a1820845SGatien Chevallier #define RIFSC_RISC_SEML_SHIFT 16 49*a1820845SGatien Chevallier #define RIFSC_RISC_SEMWL_MASK GENMASK(23, 16) 50*a1820845SGatien Chevallier #define RIFSC_RISC_PER_ID_MASK GENMASK(31, 24) 51*a1820845SGatien Chevallier 52*a1820845SGatien Chevallier #define RIFSC_RISC_PERx_CID_MASK (RIFSC_RISC_CFEN_MASK | \ 53*a1820845SGatien Chevallier RIFSC_RISC_SEM_EN_MASK | \ 54*a1820845SGatien Chevallier RIFSC_RISC_SCID_MASK | \ 55*a1820845SGatien Chevallier RIFSC_RISC_SEMWL_MASK) 56*a1820845SGatien Chevallier 57*a1820845SGatien Chevallier #define IDS_PER_RISC_SEC_PRIV_REGS 32 58*a1820845SGatien Chevallier 59*a1820845SGatien Chevallier /* RIF miscellaneous */ 60*a1820845SGatien Chevallier /* 61*a1820845SGatien Chevallier * CIDCFGR register fields 62*a1820845SGatien Chevallier */ 63*a1820845SGatien Chevallier #define CIDCFGR_CFEN BIT(0) 64*a1820845SGatien Chevallier #define CIDCFGR_SEMEN BIT(1) 65*a1820845SGatien Chevallier #define CIDCFGR_SEMWL(x) BIT(RIFSC_RISC_SEML_SHIFT + (x)) 66*a1820845SGatien Chevallier 67*a1820845SGatien Chevallier #define SEMWL_SHIFT 16 68*a1820845SGatien Chevallier 69*a1820845SGatien Chevallier /* Compartiment IDs */ 70*a1820845SGatien Chevallier #define RIF_CID0 0x0 71*a1820845SGatien Chevallier #define RIF_CID1 0x1 72*a1820845SGatien Chevallier 73*a1820845SGatien Chevallier static bool stm32_rifsc_is_semaphore_available(void __iomem *addr) 74*a1820845SGatien Chevallier { 75*a1820845SGatien Chevallier return !(readl(addr) & SEMCR_MUTEX); 76*a1820845SGatien Chevallier } 77*a1820845SGatien Chevallier 78*a1820845SGatien Chevallier static int stm32_rif_acquire_semaphore(struct stm32_firewall_controller *stm32_firewall_controller, 79*a1820845SGatien Chevallier int id) 80*a1820845SGatien Chevallier { 81*a1820845SGatien Chevallier void __iomem *addr = stm32_firewall_controller->mmio + RIFSC_RISC_PER0_SEMCR + 0x8 * id; 82*a1820845SGatien Chevallier 83*a1820845SGatien Chevallier writel(SEMCR_MUTEX, addr); 84*a1820845SGatien Chevallier 85*a1820845SGatien Chevallier /* Check that CID1 has the semaphore */ 86*a1820845SGatien Chevallier if (stm32_rifsc_is_semaphore_available(addr) || 87*a1820845SGatien Chevallier FIELD_GET(RIFSC_RISC_SCID_MASK, readl(addr)) != RIF_CID1) 88*a1820845SGatien Chevallier return -EACCES; 89*a1820845SGatien Chevallier 90*a1820845SGatien Chevallier return 0; 91*a1820845SGatien Chevallier } 92*a1820845SGatien Chevallier 93*a1820845SGatien Chevallier static void stm32_rif_release_semaphore(struct stm32_firewall_controller *stm32_firewall_controller, 94*a1820845SGatien Chevallier int id) 95*a1820845SGatien Chevallier { 96*a1820845SGatien Chevallier void __iomem *addr = stm32_firewall_controller->mmio + RIFSC_RISC_PER0_SEMCR + 0x8 * id; 97*a1820845SGatien Chevallier 98*a1820845SGatien Chevallier if (stm32_rifsc_is_semaphore_available(addr)) 99*a1820845SGatien Chevallier return; 100*a1820845SGatien Chevallier 101*a1820845SGatien Chevallier writel(SEMCR_MUTEX, addr); 102*a1820845SGatien Chevallier 103*a1820845SGatien Chevallier /* Ok if another compartment takes the semaphore before the check */ 104*a1820845SGatien Chevallier WARN_ON(!stm32_rifsc_is_semaphore_available(addr) && 105*a1820845SGatien Chevallier FIELD_GET(RIFSC_RISC_SCID_MASK, readl(addr)) == RIF_CID1); 106*a1820845SGatien Chevallier } 107*a1820845SGatien Chevallier 108*a1820845SGatien Chevallier static int stm32_rifsc_grant_access(struct stm32_firewall_controller *ctrl, u32 firewall_id) 109*a1820845SGatien Chevallier { 110*a1820845SGatien Chevallier struct stm32_firewall_controller *rifsc_controller = ctrl; 111*a1820845SGatien Chevallier u32 reg_offset, reg_id, sec_reg_value, cid_reg_value; 112*a1820845SGatien Chevallier int rc; 113*a1820845SGatien Chevallier 114*a1820845SGatien Chevallier if (firewall_id >= rifsc_controller->max_entries) { 115*a1820845SGatien Chevallier dev_err(rifsc_controller->dev, "Invalid sys bus ID %u", firewall_id); 116*a1820845SGatien Chevallier return -EINVAL; 117*a1820845SGatien Chevallier } 118*a1820845SGatien Chevallier 119*a1820845SGatien Chevallier /* 120*a1820845SGatien Chevallier * RIFSC_RISC_PRIVCFGRx and RIFSC_RISC_SECCFGRx both handle configuration access for 121*a1820845SGatien Chevallier * 32 peripherals. On the other hand, there is one _RIFSC_RISC_PERx_CIDCFGR register 122*a1820845SGatien Chevallier * per peripheral 123*a1820845SGatien Chevallier */ 124*a1820845SGatien Chevallier reg_id = firewall_id / IDS_PER_RISC_SEC_PRIV_REGS; 125*a1820845SGatien Chevallier reg_offset = firewall_id % IDS_PER_RISC_SEC_PRIV_REGS; 126*a1820845SGatien Chevallier sec_reg_value = readl(rifsc_controller->mmio + RIFSC_RISC_SECCFGR0 + 0x4 * reg_id); 127*a1820845SGatien Chevallier cid_reg_value = readl(rifsc_controller->mmio + RIFSC_RISC_PER0_CIDCFGR + 0x8 * firewall_id); 128*a1820845SGatien Chevallier 129*a1820845SGatien Chevallier /* First check conditions for semaphore mode, which doesn't take into account static CID. */ 130*a1820845SGatien Chevallier if ((cid_reg_value & CIDCFGR_SEMEN) && (cid_reg_value & CIDCFGR_CFEN)) { 131*a1820845SGatien Chevallier if (cid_reg_value & BIT(RIF_CID1 + SEMWL_SHIFT)) { 132*a1820845SGatien Chevallier /* Static CID is irrelevant if semaphore mode */ 133*a1820845SGatien Chevallier goto skip_cid_check; 134*a1820845SGatien Chevallier } else { 135*a1820845SGatien Chevallier dev_dbg(rifsc_controller->dev, 136*a1820845SGatien Chevallier "Invalid bus semaphore configuration: index %d\n", firewall_id); 137*a1820845SGatien Chevallier return -EACCES; 138*a1820845SGatien Chevallier } 139*a1820845SGatien Chevallier } 140*a1820845SGatien Chevallier 141*a1820845SGatien Chevallier /* 142*a1820845SGatien Chevallier * Skip CID check if CID filtering isn't enabled or filtering is enabled on CID0, which 143*a1820845SGatien Chevallier * corresponds to whatever CID. 144*a1820845SGatien Chevallier */ 145*a1820845SGatien Chevallier if (!(cid_reg_value & CIDCFGR_CFEN) || 146*a1820845SGatien Chevallier FIELD_GET(RIFSC_RISC_SCID_MASK, cid_reg_value) == RIF_CID0) 147*a1820845SGatien Chevallier goto skip_cid_check; 148*a1820845SGatien Chevallier 149*a1820845SGatien Chevallier /* Coherency check with the CID configuration */ 150*a1820845SGatien Chevallier if (FIELD_GET(RIFSC_RISC_SCID_MASK, cid_reg_value) != RIF_CID1) { 151*a1820845SGatien Chevallier dev_dbg(rifsc_controller->dev, "Invalid CID configuration for peripheral: %d\n", 152*a1820845SGatien Chevallier firewall_id); 153*a1820845SGatien Chevallier return -EACCES; 154*a1820845SGatien Chevallier } 155*a1820845SGatien Chevallier 156*a1820845SGatien Chevallier skip_cid_check: 157*a1820845SGatien Chevallier /* Check security configuration */ 158*a1820845SGatien Chevallier if (sec_reg_value & BIT(reg_offset)) { 159*a1820845SGatien Chevallier dev_dbg(rifsc_controller->dev, 160*a1820845SGatien Chevallier "Invalid security configuration for peripheral: %d\n", firewall_id); 161*a1820845SGatien Chevallier return -EACCES; 162*a1820845SGatien Chevallier } 163*a1820845SGatien Chevallier 164*a1820845SGatien Chevallier /* 165*a1820845SGatien Chevallier * If the peripheral is in semaphore mode, take the semaphore so that 166*a1820845SGatien Chevallier * the CID1 has the ownership. 167*a1820845SGatien Chevallier */ 168*a1820845SGatien Chevallier if ((cid_reg_value & CIDCFGR_SEMEN) && (cid_reg_value & CIDCFGR_CFEN)) { 169*a1820845SGatien Chevallier rc = stm32_rif_acquire_semaphore(rifsc_controller, firewall_id); 170*a1820845SGatien Chevallier if (rc) { 171*a1820845SGatien Chevallier dev_err(rifsc_controller->dev, 172*a1820845SGatien Chevallier "Couldn't acquire semaphore for peripheral: %d\n", firewall_id); 173*a1820845SGatien Chevallier return rc; 174*a1820845SGatien Chevallier } 175*a1820845SGatien Chevallier } 176*a1820845SGatien Chevallier 177*a1820845SGatien Chevallier return 0; 178*a1820845SGatien Chevallier } 179*a1820845SGatien Chevallier 180*a1820845SGatien Chevallier static void stm32_rifsc_release_access(struct stm32_firewall_controller *ctrl, u32 firewall_id) 181*a1820845SGatien Chevallier { 182*a1820845SGatien Chevallier stm32_rif_release_semaphore(ctrl, firewall_id); 183*a1820845SGatien Chevallier } 184*a1820845SGatien Chevallier 185*a1820845SGatien Chevallier static int stm32_rifsc_probe(struct platform_device *pdev) 186*a1820845SGatien Chevallier { 187*a1820845SGatien Chevallier struct stm32_firewall_controller *rifsc_controller; 188*a1820845SGatien Chevallier struct device_node *np = pdev->dev.of_node; 189*a1820845SGatien Chevallier u32 nb_risup, nb_rimu, nb_risal; 190*a1820845SGatien Chevallier struct resource *res; 191*a1820845SGatien Chevallier void __iomem *mmio; 192*a1820845SGatien Chevallier int rc; 193*a1820845SGatien Chevallier 194*a1820845SGatien Chevallier rifsc_controller = devm_kzalloc(&pdev->dev, sizeof(*rifsc_controller), GFP_KERNEL); 195*a1820845SGatien Chevallier if (!rifsc_controller) 196*a1820845SGatien Chevallier return -ENOMEM; 197*a1820845SGatien Chevallier 198*a1820845SGatien Chevallier mmio = devm_platform_get_and_ioremap_resource(pdev, 0, &res); 199*a1820845SGatien Chevallier if (IS_ERR(mmio)) 200*a1820845SGatien Chevallier return PTR_ERR(mmio); 201*a1820845SGatien Chevallier 202*a1820845SGatien Chevallier rifsc_controller->dev = &pdev->dev; 203*a1820845SGatien Chevallier rifsc_controller->mmio = mmio; 204*a1820845SGatien Chevallier rifsc_controller->name = dev_driver_string(rifsc_controller->dev); 205*a1820845SGatien Chevallier rifsc_controller->type = STM32_PERIPHERAL_FIREWALL | STM32_MEMORY_FIREWALL; 206*a1820845SGatien Chevallier rifsc_controller->grant_access = stm32_rifsc_grant_access; 207*a1820845SGatien Chevallier rifsc_controller->release_access = stm32_rifsc_release_access; 208*a1820845SGatien Chevallier 209*a1820845SGatien Chevallier /* Get number of RIFSC entries*/ 210*a1820845SGatien Chevallier nb_risup = readl(rifsc_controller->mmio + RIFSC_RISC_HWCFGR2) & HWCFGR2_CONF1_MASK; 211*a1820845SGatien Chevallier nb_rimu = readl(rifsc_controller->mmio + RIFSC_RISC_HWCFGR2) & HWCFGR2_CONF2_MASK; 212*a1820845SGatien Chevallier nb_risal = readl(rifsc_controller->mmio + RIFSC_RISC_HWCFGR2) & HWCFGR2_CONF3_MASK; 213*a1820845SGatien Chevallier rifsc_controller->max_entries = nb_risup + nb_rimu + nb_risal; 214*a1820845SGatien Chevallier 215*a1820845SGatien Chevallier platform_set_drvdata(pdev, rifsc_controller); 216*a1820845SGatien Chevallier 217*a1820845SGatien Chevallier rc = stm32_firewall_controller_register(rifsc_controller); 218*a1820845SGatien Chevallier if (rc) { 219*a1820845SGatien Chevallier dev_err(rifsc_controller->dev, "Couldn't register as a firewall controller: %d", 220*a1820845SGatien Chevallier rc); 221*a1820845SGatien Chevallier return rc; 222*a1820845SGatien Chevallier } 223*a1820845SGatien Chevallier 224*a1820845SGatien Chevallier rc = stm32_firewall_populate_bus(rifsc_controller); 225*a1820845SGatien Chevallier if (rc) { 226*a1820845SGatien Chevallier dev_err(rifsc_controller->dev, "Couldn't populate RIFSC bus: %d", 227*a1820845SGatien Chevallier rc); 228*a1820845SGatien Chevallier return rc; 229*a1820845SGatien Chevallier } 230*a1820845SGatien Chevallier 231*a1820845SGatien Chevallier /* Populate all allowed nodes */ 232*a1820845SGatien Chevallier return of_platform_populate(np, NULL, NULL, &pdev->dev); 233*a1820845SGatien Chevallier } 234*a1820845SGatien Chevallier 235*a1820845SGatien Chevallier static const struct of_device_id stm32_rifsc_of_match[] = { 236*a1820845SGatien Chevallier { .compatible = "st,stm32mp25-rifsc" }, 237*a1820845SGatien Chevallier {} 238*a1820845SGatien Chevallier }; 239*a1820845SGatien Chevallier MODULE_DEVICE_TABLE(of, stm32_rifsc_of_match); 240*a1820845SGatien Chevallier 241*a1820845SGatien Chevallier static struct platform_driver stm32_rifsc_driver = { 242*a1820845SGatien Chevallier .probe = stm32_rifsc_probe, 243*a1820845SGatien Chevallier .driver = { 244*a1820845SGatien Chevallier .name = "stm32-rifsc", 245*a1820845SGatien Chevallier .of_match_table = stm32_rifsc_of_match, 246*a1820845SGatien Chevallier }, 247*a1820845SGatien Chevallier }; 248*a1820845SGatien Chevallier module_platform_driver(stm32_rifsc_driver); 249*a1820845SGatien Chevallier 250*a1820845SGatien Chevallier MODULE_AUTHOR("Gatien Chevallier <gatien.chevallier@foss.st.com>"); 251*a1820845SGatien Chevallier MODULE_DESCRIPTION("STMicroelectronics RIFSC driver"); 252*a1820845SGatien Chevallier MODULE_LICENSE("GPL"); 253