18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Driver for EIP97 cryptographic accelerator. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2016 Ryder Lee <ryder.lee@mediatek.com> 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/clk.h> 98c2ecf20Sopenharmony_ci#include <linux/init.h> 108c2ecf20Sopenharmony_ci#include <linux/kernel.h> 118c2ecf20Sopenharmony_ci#include <linux/module.h> 128c2ecf20Sopenharmony_ci#include <linux/mod_devicetable.h> 138c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 148c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h> 158c2ecf20Sopenharmony_ci#include "mtk-platform.h" 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#define MTK_BURST_SIZE_MSK GENMASK(7, 4) 188c2ecf20Sopenharmony_ci#define MTK_BURST_SIZE(x) ((x) << 4) 198c2ecf20Sopenharmony_ci#define MTK_DESC_SIZE(x) ((x) << 0) 208c2ecf20Sopenharmony_ci#define MTK_DESC_OFFSET(x) ((x) << 16) 218c2ecf20Sopenharmony_ci#define MTK_DESC_FETCH_SIZE(x) ((x) << 0) 228c2ecf20Sopenharmony_ci#define MTK_DESC_FETCH_THRESH(x) ((x) << 16) 238c2ecf20Sopenharmony_ci#define MTK_DESC_OVL_IRQ_EN BIT(25) 248c2ecf20Sopenharmony_ci#define MTK_DESC_ATP_PRESENT BIT(30) 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#define MTK_DFSE_IDLE GENMASK(3, 0) 278c2ecf20Sopenharmony_ci#define MTK_DFSE_THR_CTRL_EN BIT(30) 288c2ecf20Sopenharmony_ci#define MTK_DFSE_THR_CTRL_RESET BIT(31) 298c2ecf20Sopenharmony_ci#define MTK_DFSE_RING_ID(x) (((x) >> 12) & GENMASK(3, 0)) 308c2ecf20Sopenharmony_ci#define MTK_DFSE_MIN_DATA(x) ((x) << 0) 318c2ecf20Sopenharmony_ci#define MTK_DFSE_MAX_DATA(x) ((x) << 8) 328c2ecf20Sopenharmony_ci#define MTK_DFE_MIN_CTRL(x) ((x) << 16) 338c2ecf20Sopenharmony_ci#define MTK_DFE_MAX_CTRL(x) ((x) << 24) 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci#define MTK_IN_BUF_MIN_THRESH(x) ((x) << 8) 368c2ecf20Sopenharmony_ci#define MTK_IN_BUF_MAX_THRESH(x) ((x) << 12) 378c2ecf20Sopenharmony_ci#define MTK_OUT_BUF_MIN_THRESH(x) ((x) << 0) 388c2ecf20Sopenharmony_ci#define MTK_OUT_BUF_MAX_THRESH(x) ((x) << 4) 398c2ecf20Sopenharmony_ci#define MTK_IN_TBUF_SIZE(x) (((x) >> 4) & GENMASK(3, 0)) 408c2ecf20Sopenharmony_ci#define MTK_IN_DBUF_SIZE(x) (((x) >> 8) & GENMASK(3, 0)) 418c2ecf20Sopenharmony_ci#define MTK_OUT_DBUF_SIZE(x) (((x) >> 16) & GENMASK(3, 0)) 428c2ecf20Sopenharmony_ci#define MTK_CMD_FIFO_SIZE(x) (((x) >> 8) & GENMASK(3, 0)) 438c2ecf20Sopenharmony_ci#define MTK_RES_FIFO_SIZE(x) (((x) >> 12) & GENMASK(3, 0)) 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci#define MTK_PE_TK_LOC_AVL BIT(2) 468c2ecf20Sopenharmony_ci#define MTK_PE_PROC_HELD BIT(14) 478c2ecf20Sopenharmony_ci#define MTK_PE_TK_TIMEOUT_EN BIT(22) 488c2ecf20Sopenharmony_ci#define MTK_PE_INPUT_DMA_ERR BIT(0) 498c2ecf20Sopenharmony_ci#define MTK_PE_OUTPUT_DMA_ERR BIT(1) 508c2ecf20Sopenharmony_ci#define MTK_PE_PKT_PORC_ERR BIT(2) 518c2ecf20Sopenharmony_ci#define MTK_PE_PKT_TIMEOUT BIT(3) 528c2ecf20Sopenharmony_ci#define MTK_PE_FATAL_ERR BIT(14) 538c2ecf20Sopenharmony_ci#define MTK_PE_INPUT_DMA_ERR_EN BIT(16) 548c2ecf20Sopenharmony_ci#define MTK_PE_OUTPUT_DMA_ERR_EN BIT(17) 558c2ecf20Sopenharmony_ci#define MTK_PE_PKT_PORC_ERR_EN BIT(18) 568c2ecf20Sopenharmony_ci#define MTK_PE_PKT_TIMEOUT_EN BIT(19) 578c2ecf20Sopenharmony_ci#define MTK_PE_FATAL_ERR_EN BIT(30) 588c2ecf20Sopenharmony_ci#define MTK_PE_INT_OUT_EN BIT(31) 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci#define MTK_HIA_SIGNATURE ((u16)0x35ca) 618c2ecf20Sopenharmony_ci#define MTK_HIA_DATA_WIDTH(x) (((x) >> 25) & GENMASK(1, 0)) 628c2ecf20Sopenharmony_ci#define MTK_HIA_DMA_LENGTH(x) (((x) >> 20) & GENMASK(4, 0)) 638c2ecf20Sopenharmony_ci#define MTK_CDR_STAT_CLR GENMASK(4, 0) 648c2ecf20Sopenharmony_ci#define MTK_RDR_STAT_CLR GENMASK(7, 0) 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci#define MTK_AIC_INT_MSK GENMASK(5, 0) 678c2ecf20Sopenharmony_ci#define MTK_AIC_VER_MSK (GENMASK(15, 0) | GENMASK(27, 20)) 688c2ecf20Sopenharmony_ci#define MTK_AIC_VER11 0x011036c9 698c2ecf20Sopenharmony_ci#define MTK_AIC_VER12 0x012036c9 708c2ecf20Sopenharmony_ci#define MTK_AIC_G_CLR GENMASK(30, 20) 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci/** 738c2ecf20Sopenharmony_ci * EIP97 is an integrated security subsystem to accelerate cryptographic 748c2ecf20Sopenharmony_ci * functions and protocols to offload the host processor. 758c2ecf20Sopenharmony_ci * Some important hardware modules are briefly introduced below: 768c2ecf20Sopenharmony_ci * 778c2ecf20Sopenharmony_ci * Host Interface Adapter(HIA) - the main interface between the host 788c2ecf20Sopenharmony_ci * system and the hardware subsystem. It is responsible for attaching 798c2ecf20Sopenharmony_ci * processing engine to the specific host bus interface and provides a 808c2ecf20Sopenharmony_ci * standardized software view for off loading tasks to the engine. 818c2ecf20Sopenharmony_ci * 828c2ecf20Sopenharmony_ci * Command Descriptor Ring Manager(CDR Manager) - keeps track of how many 838c2ecf20Sopenharmony_ci * CD the host has prepared in the CDR. It monitors the fill level of its 848c2ecf20Sopenharmony_ci * CD-FIFO and if there's sufficient space for the next block of descriptors, 858c2ecf20Sopenharmony_ci * then it fires off a DMA request to fetch a block of CDs. 868c2ecf20Sopenharmony_ci * 878c2ecf20Sopenharmony_ci * Data fetch engine(DFE) - It is responsible for parsing the CD and 888c2ecf20Sopenharmony_ci * setting up the required control and packet data DMA transfers from 898c2ecf20Sopenharmony_ci * system memory to the processing engine. 908c2ecf20Sopenharmony_ci * 918c2ecf20Sopenharmony_ci * Result Descriptor Ring Manager(RDR Manager) - same as CDR Manager, 928c2ecf20Sopenharmony_ci * but target is result descriptors, Moreover, it also handles the RD 938c2ecf20Sopenharmony_ci * updates under control of the DSE. For each packet data segment 948c2ecf20Sopenharmony_ci * processed, the DSE triggers the RDR Manager to write the updated RD. 958c2ecf20Sopenharmony_ci * If triggered to update, the RDR Manager sets up a DMA operation to 968c2ecf20Sopenharmony_ci * copy the RD from the DSE to the correct location in the RDR. 978c2ecf20Sopenharmony_ci * 988c2ecf20Sopenharmony_ci * Data Store Engine(DSE) - It is responsible for parsing the prepared RD 998c2ecf20Sopenharmony_ci * and setting up the required control and packet data DMA transfers from 1008c2ecf20Sopenharmony_ci * the processing engine to system memory. 1018c2ecf20Sopenharmony_ci * 1028c2ecf20Sopenharmony_ci * Advanced Interrupt Controllers(AICs) - receive interrupt request signals 1038c2ecf20Sopenharmony_ci * from various sources and combine them into one interrupt output. 1048c2ecf20Sopenharmony_ci * The AICs are used by: 1058c2ecf20Sopenharmony_ci * - One for the HIA global and processing engine interrupts. 1068c2ecf20Sopenharmony_ci * - The others for the descriptor ring interrupts. 1078c2ecf20Sopenharmony_ci */ 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci/* Cryptographic engine capabilities */ 1108c2ecf20Sopenharmony_cistruct mtk_sys_cap { 1118c2ecf20Sopenharmony_ci /* host interface adapter */ 1128c2ecf20Sopenharmony_ci u32 hia_ver; 1138c2ecf20Sopenharmony_ci u32 hia_opt; 1148c2ecf20Sopenharmony_ci /* packet engine */ 1158c2ecf20Sopenharmony_ci u32 pkt_eng_opt; 1168c2ecf20Sopenharmony_ci /* global hardware */ 1178c2ecf20Sopenharmony_ci u32 hw_opt; 1188c2ecf20Sopenharmony_ci}; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_cistatic void mtk_desc_ring_link(struct mtk_cryp *cryp, u32 mask) 1218c2ecf20Sopenharmony_ci{ 1228c2ecf20Sopenharmony_ci /* Assign rings to DFE/DSE thread and enable it */ 1238c2ecf20Sopenharmony_ci writel(MTK_DFSE_THR_CTRL_EN | mask, cryp->base + DFE_THR_CTRL); 1248c2ecf20Sopenharmony_ci writel(MTK_DFSE_THR_CTRL_EN | mask, cryp->base + DSE_THR_CTRL); 1258c2ecf20Sopenharmony_ci} 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_cistatic void mtk_dfe_dse_buf_setup(struct mtk_cryp *cryp, 1288c2ecf20Sopenharmony_ci struct mtk_sys_cap *cap) 1298c2ecf20Sopenharmony_ci{ 1308c2ecf20Sopenharmony_ci u32 width = MTK_HIA_DATA_WIDTH(cap->hia_opt) + 2; 1318c2ecf20Sopenharmony_ci u32 len = MTK_HIA_DMA_LENGTH(cap->hia_opt) - 1; 1328c2ecf20Sopenharmony_ci u32 ipbuf = min((u32)MTK_IN_DBUF_SIZE(cap->hw_opt) + width, len); 1338c2ecf20Sopenharmony_ci u32 opbuf = min((u32)MTK_OUT_DBUF_SIZE(cap->hw_opt) + width, len); 1348c2ecf20Sopenharmony_ci u32 itbuf = min((u32)MTK_IN_TBUF_SIZE(cap->hw_opt) + width, len); 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci writel(MTK_DFSE_MIN_DATA(ipbuf - 1) | 1378c2ecf20Sopenharmony_ci MTK_DFSE_MAX_DATA(ipbuf) | 1388c2ecf20Sopenharmony_ci MTK_DFE_MIN_CTRL(itbuf - 1) | 1398c2ecf20Sopenharmony_ci MTK_DFE_MAX_CTRL(itbuf), 1408c2ecf20Sopenharmony_ci cryp->base + DFE_CFG); 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci writel(MTK_DFSE_MIN_DATA(opbuf - 1) | 1438c2ecf20Sopenharmony_ci MTK_DFSE_MAX_DATA(opbuf), 1448c2ecf20Sopenharmony_ci cryp->base + DSE_CFG); 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci writel(MTK_IN_BUF_MIN_THRESH(ipbuf - 1) | 1478c2ecf20Sopenharmony_ci MTK_IN_BUF_MAX_THRESH(ipbuf), 1488c2ecf20Sopenharmony_ci cryp->base + PE_IN_DBUF_THRESH); 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci writel(MTK_IN_BUF_MIN_THRESH(itbuf - 1) | 1518c2ecf20Sopenharmony_ci MTK_IN_BUF_MAX_THRESH(itbuf), 1528c2ecf20Sopenharmony_ci cryp->base + PE_IN_TBUF_THRESH); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci writel(MTK_OUT_BUF_MIN_THRESH(opbuf - 1) | 1558c2ecf20Sopenharmony_ci MTK_OUT_BUF_MAX_THRESH(opbuf), 1568c2ecf20Sopenharmony_ci cryp->base + PE_OUT_DBUF_THRESH); 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci writel(0, cryp->base + PE_OUT_TBUF_THRESH); 1598c2ecf20Sopenharmony_ci writel(0, cryp->base + PE_OUT_BUF_CTRL); 1608c2ecf20Sopenharmony_ci} 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_cistatic int mtk_dfe_dse_state_check(struct mtk_cryp *cryp) 1638c2ecf20Sopenharmony_ci{ 1648c2ecf20Sopenharmony_ci int ret = -EINVAL; 1658c2ecf20Sopenharmony_ci u32 val; 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci /* Check for completion of all DMA transfers */ 1688c2ecf20Sopenharmony_ci val = readl(cryp->base + DFE_THR_STAT); 1698c2ecf20Sopenharmony_ci if (MTK_DFSE_RING_ID(val) == MTK_DFSE_IDLE) { 1708c2ecf20Sopenharmony_ci val = readl(cryp->base + DSE_THR_STAT); 1718c2ecf20Sopenharmony_ci if (MTK_DFSE_RING_ID(val) == MTK_DFSE_IDLE) 1728c2ecf20Sopenharmony_ci ret = 0; 1738c2ecf20Sopenharmony_ci } 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci if (!ret) { 1768c2ecf20Sopenharmony_ci /* Take DFE/DSE thread out of reset */ 1778c2ecf20Sopenharmony_ci writel(0, cryp->base + DFE_THR_CTRL); 1788c2ecf20Sopenharmony_ci writel(0, cryp->base + DSE_THR_CTRL); 1798c2ecf20Sopenharmony_ci } else { 1808c2ecf20Sopenharmony_ci return -EBUSY; 1818c2ecf20Sopenharmony_ci } 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci return 0; 1848c2ecf20Sopenharmony_ci} 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_cistatic int mtk_dfe_dse_reset(struct mtk_cryp *cryp) 1878c2ecf20Sopenharmony_ci{ 1888c2ecf20Sopenharmony_ci /* Reset DSE/DFE and correct system priorities for all rings. */ 1898c2ecf20Sopenharmony_ci writel(MTK_DFSE_THR_CTRL_RESET, cryp->base + DFE_THR_CTRL); 1908c2ecf20Sopenharmony_ci writel(0, cryp->base + DFE_PRIO_0); 1918c2ecf20Sopenharmony_ci writel(0, cryp->base + DFE_PRIO_1); 1928c2ecf20Sopenharmony_ci writel(0, cryp->base + DFE_PRIO_2); 1938c2ecf20Sopenharmony_ci writel(0, cryp->base + DFE_PRIO_3); 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci writel(MTK_DFSE_THR_CTRL_RESET, cryp->base + DSE_THR_CTRL); 1968c2ecf20Sopenharmony_ci writel(0, cryp->base + DSE_PRIO_0); 1978c2ecf20Sopenharmony_ci writel(0, cryp->base + DSE_PRIO_1); 1988c2ecf20Sopenharmony_ci writel(0, cryp->base + DSE_PRIO_2); 1998c2ecf20Sopenharmony_ci writel(0, cryp->base + DSE_PRIO_3); 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci return mtk_dfe_dse_state_check(cryp); 2028c2ecf20Sopenharmony_ci} 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_cistatic void mtk_cmd_desc_ring_setup(struct mtk_cryp *cryp, 2058c2ecf20Sopenharmony_ci int i, struct mtk_sys_cap *cap) 2068c2ecf20Sopenharmony_ci{ 2078c2ecf20Sopenharmony_ci /* Full descriptor that fits FIFO minus one */ 2088c2ecf20Sopenharmony_ci u32 count = 2098c2ecf20Sopenharmony_ci ((1 << MTK_CMD_FIFO_SIZE(cap->hia_opt)) / MTK_DESC_SZ) - 1; 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci /* Temporarily disable external triggering */ 2128c2ecf20Sopenharmony_ci writel(0, cryp->base + CDR_CFG(i)); 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci /* Clear CDR count */ 2158c2ecf20Sopenharmony_ci writel(MTK_CNT_RST, cryp->base + CDR_PREP_COUNT(i)); 2168c2ecf20Sopenharmony_ci writel(MTK_CNT_RST, cryp->base + CDR_PROC_COUNT(i)); 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci writel(0, cryp->base + CDR_PREP_PNTR(i)); 2198c2ecf20Sopenharmony_ci writel(0, cryp->base + CDR_PROC_PNTR(i)); 2208c2ecf20Sopenharmony_ci writel(0, cryp->base + CDR_DMA_CFG(i)); 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci /* Configure CDR host address space */ 2238c2ecf20Sopenharmony_ci writel(0, cryp->base + CDR_BASE_ADDR_HI(i)); 2248c2ecf20Sopenharmony_ci writel(cryp->ring[i]->cmd_dma, cryp->base + CDR_BASE_ADDR_LO(i)); 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci writel(MTK_DESC_RING_SZ, cryp->base + CDR_RING_SIZE(i)); 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci /* Clear and disable all CDR interrupts */ 2298c2ecf20Sopenharmony_ci writel(MTK_CDR_STAT_CLR, cryp->base + CDR_STAT(i)); 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci /* 2328c2ecf20Sopenharmony_ci * Set command descriptor offset and enable additional 2338c2ecf20Sopenharmony_ci * token present in descriptor. 2348c2ecf20Sopenharmony_ci */ 2358c2ecf20Sopenharmony_ci writel(MTK_DESC_SIZE(MTK_DESC_SZ) | 2368c2ecf20Sopenharmony_ci MTK_DESC_OFFSET(MTK_DESC_OFF) | 2378c2ecf20Sopenharmony_ci MTK_DESC_ATP_PRESENT, 2388c2ecf20Sopenharmony_ci cryp->base + CDR_DESC_SIZE(i)); 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci writel(MTK_DESC_FETCH_SIZE(count * MTK_DESC_OFF) | 2418c2ecf20Sopenharmony_ci MTK_DESC_FETCH_THRESH(count * MTK_DESC_SZ), 2428c2ecf20Sopenharmony_ci cryp->base + CDR_CFG(i)); 2438c2ecf20Sopenharmony_ci} 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_cistatic void mtk_res_desc_ring_setup(struct mtk_cryp *cryp, 2468c2ecf20Sopenharmony_ci int i, struct mtk_sys_cap *cap) 2478c2ecf20Sopenharmony_ci{ 2488c2ecf20Sopenharmony_ci u32 rndup = 2; 2498c2ecf20Sopenharmony_ci u32 count = ((1 << MTK_RES_FIFO_SIZE(cap->hia_opt)) / rndup) - 1; 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci /* Temporarily disable external triggering */ 2528c2ecf20Sopenharmony_ci writel(0, cryp->base + RDR_CFG(i)); 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci /* Clear RDR count */ 2558c2ecf20Sopenharmony_ci writel(MTK_CNT_RST, cryp->base + RDR_PREP_COUNT(i)); 2568c2ecf20Sopenharmony_ci writel(MTK_CNT_RST, cryp->base + RDR_PROC_COUNT(i)); 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci writel(0, cryp->base + RDR_PREP_PNTR(i)); 2598c2ecf20Sopenharmony_ci writel(0, cryp->base + RDR_PROC_PNTR(i)); 2608c2ecf20Sopenharmony_ci writel(0, cryp->base + RDR_DMA_CFG(i)); 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci /* Configure RDR host address space */ 2638c2ecf20Sopenharmony_ci writel(0, cryp->base + RDR_BASE_ADDR_HI(i)); 2648c2ecf20Sopenharmony_ci writel(cryp->ring[i]->res_dma, cryp->base + RDR_BASE_ADDR_LO(i)); 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ci writel(MTK_DESC_RING_SZ, cryp->base + RDR_RING_SIZE(i)); 2678c2ecf20Sopenharmony_ci writel(MTK_RDR_STAT_CLR, cryp->base + RDR_STAT(i)); 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci /* 2708c2ecf20Sopenharmony_ci * RDR manager generates update interrupts on a per-completed-packet, 2718c2ecf20Sopenharmony_ci * and the rd_proc_thresh_irq interrupt is fired when proc_pkt_count 2728c2ecf20Sopenharmony_ci * for the RDR exceeds the number of packets. 2738c2ecf20Sopenharmony_ci */ 2748c2ecf20Sopenharmony_ci writel(MTK_RDR_PROC_THRESH | MTK_RDR_PROC_MODE, 2758c2ecf20Sopenharmony_ci cryp->base + RDR_THRESH(i)); 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci /* 2788c2ecf20Sopenharmony_ci * Configure a threshold and time-out value for the processed 2798c2ecf20Sopenharmony_ci * result descriptors (or complete packets) that are written to 2808c2ecf20Sopenharmony_ci * the RDR. 2818c2ecf20Sopenharmony_ci */ 2828c2ecf20Sopenharmony_ci writel(MTK_DESC_SIZE(MTK_DESC_SZ) | MTK_DESC_OFFSET(MTK_DESC_OFF), 2838c2ecf20Sopenharmony_ci cryp->base + RDR_DESC_SIZE(i)); 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci /* 2868c2ecf20Sopenharmony_ci * Configure HIA fetch size and fetch threshold that are used to 2878c2ecf20Sopenharmony_ci * fetch blocks of multiple descriptors. 2888c2ecf20Sopenharmony_ci */ 2898c2ecf20Sopenharmony_ci writel(MTK_DESC_FETCH_SIZE(count * MTK_DESC_OFF) | 2908c2ecf20Sopenharmony_ci MTK_DESC_FETCH_THRESH(count * rndup) | 2918c2ecf20Sopenharmony_ci MTK_DESC_OVL_IRQ_EN, 2928c2ecf20Sopenharmony_ci cryp->base + RDR_CFG(i)); 2938c2ecf20Sopenharmony_ci} 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_cistatic int mtk_packet_engine_setup(struct mtk_cryp *cryp) 2968c2ecf20Sopenharmony_ci{ 2978c2ecf20Sopenharmony_ci struct mtk_sys_cap cap; 2988c2ecf20Sopenharmony_ci int i, err; 2998c2ecf20Sopenharmony_ci u32 val; 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci cap.hia_ver = readl(cryp->base + HIA_VERSION); 3028c2ecf20Sopenharmony_ci cap.hia_opt = readl(cryp->base + HIA_OPTIONS); 3038c2ecf20Sopenharmony_ci cap.hw_opt = readl(cryp->base + EIP97_OPTIONS); 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci if (!(((u16)cap.hia_ver) == MTK_HIA_SIGNATURE)) 3068c2ecf20Sopenharmony_ci return -EINVAL; 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_ci /* Configure endianness conversion method for master (DMA) interface */ 3098c2ecf20Sopenharmony_ci writel(0, cryp->base + EIP97_MST_CTRL); 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci /* Set HIA burst size */ 3128c2ecf20Sopenharmony_ci val = readl(cryp->base + HIA_MST_CTRL); 3138c2ecf20Sopenharmony_ci val &= ~MTK_BURST_SIZE_MSK; 3148c2ecf20Sopenharmony_ci val |= MTK_BURST_SIZE(5); 3158c2ecf20Sopenharmony_ci writel(val, cryp->base + HIA_MST_CTRL); 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci err = mtk_dfe_dse_reset(cryp); 3188c2ecf20Sopenharmony_ci if (err) { 3198c2ecf20Sopenharmony_ci dev_err(cryp->dev, "Failed to reset DFE and DSE.\n"); 3208c2ecf20Sopenharmony_ci return err; 3218c2ecf20Sopenharmony_ci } 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci mtk_dfe_dse_buf_setup(cryp, &cap); 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci /* Enable the 4 rings for the packet engines. */ 3268c2ecf20Sopenharmony_ci mtk_desc_ring_link(cryp, 0xf); 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci for (i = 0; i < MTK_RING_MAX; i++) { 3298c2ecf20Sopenharmony_ci mtk_cmd_desc_ring_setup(cryp, i, &cap); 3308c2ecf20Sopenharmony_ci mtk_res_desc_ring_setup(cryp, i, &cap); 3318c2ecf20Sopenharmony_ci } 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_ci writel(MTK_PE_TK_LOC_AVL | MTK_PE_PROC_HELD | MTK_PE_TK_TIMEOUT_EN, 3348c2ecf20Sopenharmony_ci cryp->base + PE_TOKEN_CTRL_STAT); 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_ci /* Clear all pending interrupts */ 3378c2ecf20Sopenharmony_ci writel(MTK_AIC_G_CLR, cryp->base + AIC_G_ACK); 3388c2ecf20Sopenharmony_ci writel(MTK_PE_INPUT_DMA_ERR | MTK_PE_OUTPUT_DMA_ERR | 3398c2ecf20Sopenharmony_ci MTK_PE_PKT_PORC_ERR | MTK_PE_PKT_TIMEOUT | 3408c2ecf20Sopenharmony_ci MTK_PE_FATAL_ERR | MTK_PE_INPUT_DMA_ERR_EN | 3418c2ecf20Sopenharmony_ci MTK_PE_OUTPUT_DMA_ERR_EN | MTK_PE_PKT_PORC_ERR_EN | 3428c2ecf20Sopenharmony_ci MTK_PE_PKT_TIMEOUT_EN | MTK_PE_FATAL_ERR_EN | 3438c2ecf20Sopenharmony_ci MTK_PE_INT_OUT_EN, 3448c2ecf20Sopenharmony_ci cryp->base + PE_INTERRUPT_CTRL_STAT); 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_ci return 0; 3478c2ecf20Sopenharmony_ci} 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_cistatic int mtk_aic_cap_check(struct mtk_cryp *cryp, int hw) 3508c2ecf20Sopenharmony_ci{ 3518c2ecf20Sopenharmony_ci u32 val; 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci if (hw == MTK_RING_MAX) 3548c2ecf20Sopenharmony_ci val = readl(cryp->base + AIC_G_VERSION); 3558c2ecf20Sopenharmony_ci else 3568c2ecf20Sopenharmony_ci val = readl(cryp->base + AIC_VERSION(hw)); 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci val &= MTK_AIC_VER_MSK; 3598c2ecf20Sopenharmony_ci if (val != MTK_AIC_VER11 && val != MTK_AIC_VER12) 3608c2ecf20Sopenharmony_ci return -ENXIO; 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_ci if (hw == MTK_RING_MAX) 3638c2ecf20Sopenharmony_ci val = readl(cryp->base + AIC_G_OPTIONS); 3648c2ecf20Sopenharmony_ci else 3658c2ecf20Sopenharmony_ci val = readl(cryp->base + AIC_OPTIONS(hw)); 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci val &= MTK_AIC_INT_MSK; 3688c2ecf20Sopenharmony_ci if (!val || val > 32) 3698c2ecf20Sopenharmony_ci return -ENXIO; 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ci return 0; 3728c2ecf20Sopenharmony_ci} 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_cistatic int mtk_aic_init(struct mtk_cryp *cryp, int hw) 3758c2ecf20Sopenharmony_ci{ 3768c2ecf20Sopenharmony_ci int err; 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci err = mtk_aic_cap_check(cryp, hw); 3798c2ecf20Sopenharmony_ci if (err) 3808c2ecf20Sopenharmony_ci return err; 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_ci /* Disable all interrupts and set initial configuration */ 3838c2ecf20Sopenharmony_ci if (hw == MTK_RING_MAX) { 3848c2ecf20Sopenharmony_ci writel(0, cryp->base + AIC_G_ENABLE_CTRL); 3858c2ecf20Sopenharmony_ci writel(0, cryp->base + AIC_G_POL_CTRL); 3868c2ecf20Sopenharmony_ci writel(0, cryp->base + AIC_G_TYPE_CTRL); 3878c2ecf20Sopenharmony_ci writel(0, cryp->base + AIC_G_ENABLE_SET); 3888c2ecf20Sopenharmony_ci } else { 3898c2ecf20Sopenharmony_ci writel(0, cryp->base + AIC_ENABLE_CTRL(hw)); 3908c2ecf20Sopenharmony_ci writel(0, cryp->base + AIC_POL_CTRL(hw)); 3918c2ecf20Sopenharmony_ci writel(0, cryp->base + AIC_TYPE_CTRL(hw)); 3928c2ecf20Sopenharmony_ci writel(0, cryp->base + AIC_ENABLE_SET(hw)); 3938c2ecf20Sopenharmony_ci } 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci return 0; 3968c2ecf20Sopenharmony_ci} 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_cistatic int mtk_accelerator_init(struct mtk_cryp *cryp) 3998c2ecf20Sopenharmony_ci{ 4008c2ecf20Sopenharmony_ci int i, err; 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci /* Initialize advanced interrupt controller(AIC) */ 4038c2ecf20Sopenharmony_ci for (i = 0; i < MTK_IRQ_NUM; i++) { 4048c2ecf20Sopenharmony_ci err = mtk_aic_init(cryp, i); 4058c2ecf20Sopenharmony_ci if (err) { 4068c2ecf20Sopenharmony_ci dev_err(cryp->dev, "Failed to initialize AIC.\n"); 4078c2ecf20Sopenharmony_ci return err; 4088c2ecf20Sopenharmony_ci } 4098c2ecf20Sopenharmony_ci } 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_ci /* Initialize packet engine */ 4128c2ecf20Sopenharmony_ci err = mtk_packet_engine_setup(cryp); 4138c2ecf20Sopenharmony_ci if (err) { 4148c2ecf20Sopenharmony_ci dev_err(cryp->dev, "Failed to configure packet engine.\n"); 4158c2ecf20Sopenharmony_ci return err; 4168c2ecf20Sopenharmony_ci } 4178c2ecf20Sopenharmony_ci 4188c2ecf20Sopenharmony_ci return 0; 4198c2ecf20Sopenharmony_ci} 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_cistatic void mtk_desc_dma_free(struct mtk_cryp *cryp) 4228c2ecf20Sopenharmony_ci{ 4238c2ecf20Sopenharmony_ci int i; 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci for (i = 0; i < MTK_RING_MAX; i++) { 4268c2ecf20Sopenharmony_ci dma_free_coherent(cryp->dev, MTK_DESC_RING_SZ, 4278c2ecf20Sopenharmony_ci cryp->ring[i]->res_base, 4288c2ecf20Sopenharmony_ci cryp->ring[i]->res_dma); 4298c2ecf20Sopenharmony_ci dma_free_coherent(cryp->dev, MTK_DESC_RING_SZ, 4308c2ecf20Sopenharmony_ci cryp->ring[i]->cmd_base, 4318c2ecf20Sopenharmony_ci cryp->ring[i]->cmd_dma); 4328c2ecf20Sopenharmony_ci kfree(cryp->ring[i]); 4338c2ecf20Sopenharmony_ci } 4348c2ecf20Sopenharmony_ci} 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_cistatic int mtk_desc_ring_alloc(struct mtk_cryp *cryp) 4378c2ecf20Sopenharmony_ci{ 4388c2ecf20Sopenharmony_ci struct mtk_ring **ring = cryp->ring; 4398c2ecf20Sopenharmony_ci int i; 4408c2ecf20Sopenharmony_ci 4418c2ecf20Sopenharmony_ci for (i = 0; i < MTK_RING_MAX; i++) { 4428c2ecf20Sopenharmony_ci ring[i] = kzalloc(sizeof(**ring), GFP_KERNEL); 4438c2ecf20Sopenharmony_ci if (!ring[i]) 4448c2ecf20Sopenharmony_ci goto err_cleanup; 4458c2ecf20Sopenharmony_ci 4468c2ecf20Sopenharmony_ci ring[i]->cmd_base = dma_alloc_coherent(cryp->dev, 4478c2ecf20Sopenharmony_ci MTK_DESC_RING_SZ, 4488c2ecf20Sopenharmony_ci &ring[i]->cmd_dma, 4498c2ecf20Sopenharmony_ci GFP_KERNEL); 4508c2ecf20Sopenharmony_ci if (!ring[i]->cmd_base) 4518c2ecf20Sopenharmony_ci goto err_cleanup; 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_ci ring[i]->res_base = dma_alloc_coherent(cryp->dev, 4548c2ecf20Sopenharmony_ci MTK_DESC_RING_SZ, 4558c2ecf20Sopenharmony_ci &ring[i]->res_dma, 4568c2ecf20Sopenharmony_ci GFP_KERNEL); 4578c2ecf20Sopenharmony_ci if (!ring[i]->res_base) 4588c2ecf20Sopenharmony_ci goto err_cleanup; 4598c2ecf20Sopenharmony_ci 4608c2ecf20Sopenharmony_ci ring[i]->cmd_next = ring[i]->cmd_base; 4618c2ecf20Sopenharmony_ci ring[i]->res_next = ring[i]->res_base; 4628c2ecf20Sopenharmony_ci } 4638c2ecf20Sopenharmony_ci return 0; 4648c2ecf20Sopenharmony_ci 4658c2ecf20Sopenharmony_cierr_cleanup: 4668c2ecf20Sopenharmony_ci do { 4678c2ecf20Sopenharmony_ci dma_free_coherent(cryp->dev, MTK_DESC_RING_SZ, 4688c2ecf20Sopenharmony_ci ring[i]->res_base, ring[i]->res_dma); 4698c2ecf20Sopenharmony_ci dma_free_coherent(cryp->dev, MTK_DESC_RING_SZ, 4708c2ecf20Sopenharmony_ci ring[i]->cmd_base, ring[i]->cmd_dma); 4718c2ecf20Sopenharmony_ci kfree(ring[i]); 4728c2ecf20Sopenharmony_ci } while (i--); 4738c2ecf20Sopenharmony_ci return -ENOMEM; 4748c2ecf20Sopenharmony_ci} 4758c2ecf20Sopenharmony_ci 4768c2ecf20Sopenharmony_cistatic int mtk_crypto_probe(struct platform_device *pdev) 4778c2ecf20Sopenharmony_ci{ 4788c2ecf20Sopenharmony_ci struct mtk_cryp *cryp; 4798c2ecf20Sopenharmony_ci int i, err; 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci cryp = devm_kzalloc(&pdev->dev, sizeof(*cryp), GFP_KERNEL); 4828c2ecf20Sopenharmony_ci if (!cryp) 4838c2ecf20Sopenharmony_ci return -ENOMEM; 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci cryp->base = devm_platform_ioremap_resource(pdev, 0); 4868c2ecf20Sopenharmony_ci if (IS_ERR(cryp->base)) 4878c2ecf20Sopenharmony_ci return PTR_ERR(cryp->base); 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ci for (i = 0; i < MTK_IRQ_NUM; i++) { 4908c2ecf20Sopenharmony_ci cryp->irq[i] = platform_get_irq(pdev, i); 4918c2ecf20Sopenharmony_ci if (cryp->irq[i] < 0) 4928c2ecf20Sopenharmony_ci return cryp->irq[i]; 4938c2ecf20Sopenharmony_ci } 4948c2ecf20Sopenharmony_ci 4958c2ecf20Sopenharmony_ci cryp->clk_cryp = devm_clk_get(&pdev->dev, "cryp"); 4968c2ecf20Sopenharmony_ci if (IS_ERR(cryp->clk_cryp)) 4978c2ecf20Sopenharmony_ci return -EPROBE_DEFER; 4988c2ecf20Sopenharmony_ci 4998c2ecf20Sopenharmony_ci cryp->dev = &pdev->dev; 5008c2ecf20Sopenharmony_ci pm_runtime_enable(cryp->dev); 5018c2ecf20Sopenharmony_ci pm_runtime_get_sync(cryp->dev); 5028c2ecf20Sopenharmony_ci 5038c2ecf20Sopenharmony_ci err = clk_prepare_enable(cryp->clk_cryp); 5048c2ecf20Sopenharmony_ci if (err) 5058c2ecf20Sopenharmony_ci goto err_clk_cryp; 5068c2ecf20Sopenharmony_ci 5078c2ecf20Sopenharmony_ci /* Allocate four command/result descriptor rings */ 5088c2ecf20Sopenharmony_ci err = mtk_desc_ring_alloc(cryp); 5098c2ecf20Sopenharmony_ci if (err) { 5108c2ecf20Sopenharmony_ci dev_err(cryp->dev, "Unable to allocate descriptor rings.\n"); 5118c2ecf20Sopenharmony_ci goto err_resource; 5128c2ecf20Sopenharmony_ci } 5138c2ecf20Sopenharmony_ci 5148c2ecf20Sopenharmony_ci /* Initialize hardware modules */ 5158c2ecf20Sopenharmony_ci err = mtk_accelerator_init(cryp); 5168c2ecf20Sopenharmony_ci if (err) { 5178c2ecf20Sopenharmony_ci dev_err(cryp->dev, "Failed to initialize cryptographic engine.\n"); 5188c2ecf20Sopenharmony_ci goto err_engine; 5198c2ecf20Sopenharmony_ci } 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_ci err = mtk_cipher_alg_register(cryp); 5228c2ecf20Sopenharmony_ci if (err) { 5238c2ecf20Sopenharmony_ci dev_err(cryp->dev, "Unable to register cipher algorithm.\n"); 5248c2ecf20Sopenharmony_ci goto err_cipher; 5258c2ecf20Sopenharmony_ci } 5268c2ecf20Sopenharmony_ci 5278c2ecf20Sopenharmony_ci err = mtk_hash_alg_register(cryp); 5288c2ecf20Sopenharmony_ci if (err) { 5298c2ecf20Sopenharmony_ci dev_err(cryp->dev, "Unable to register hash algorithm.\n"); 5308c2ecf20Sopenharmony_ci goto err_hash; 5318c2ecf20Sopenharmony_ci } 5328c2ecf20Sopenharmony_ci 5338c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, cryp); 5348c2ecf20Sopenharmony_ci return 0; 5358c2ecf20Sopenharmony_ci 5368c2ecf20Sopenharmony_cierr_hash: 5378c2ecf20Sopenharmony_ci mtk_cipher_alg_release(cryp); 5388c2ecf20Sopenharmony_cierr_cipher: 5398c2ecf20Sopenharmony_ci mtk_dfe_dse_reset(cryp); 5408c2ecf20Sopenharmony_cierr_engine: 5418c2ecf20Sopenharmony_ci mtk_desc_dma_free(cryp); 5428c2ecf20Sopenharmony_cierr_resource: 5438c2ecf20Sopenharmony_ci clk_disable_unprepare(cryp->clk_cryp); 5448c2ecf20Sopenharmony_cierr_clk_cryp: 5458c2ecf20Sopenharmony_ci pm_runtime_put_sync(cryp->dev); 5468c2ecf20Sopenharmony_ci pm_runtime_disable(cryp->dev); 5478c2ecf20Sopenharmony_ci 5488c2ecf20Sopenharmony_ci return err; 5498c2ecf20Sopenharmony_ci} 5508c2ecf20Sopenharmony_ci 5518c2ecf20Sopenharmony_cistatic int mtk_crypto_remove(struct platform_device *pdev) 5528c2ecf20Sopenharmony_ci{ 5538c2ecf20Sopenharmony_ci struct mtk_cryp *cryp = platform_get_drvdata(pdev); 5548c2ecf20Sopenharmony_ci 5558c2ecf20Sopenharmony_ci mtk_hash_alg_release(cryp); 5568c2ecf20Sopenharmony_ci mtk_cipher_alg_release(cryp); 5578c2ecf20Sopenharmony_ci mtk_desc_dma_free(cryp); 5588c2ecf20Sopenharmony_ci 5598c2ecf20Sopenharmony_ci clk_disable_unprepare(cryp->clk_cryp); 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ci pm_runtime_put_sync(cryp->dev); 5628c2ecf20Sopenharmony_ci pm_runtime_disable(cryp->dev); 5638c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, NULL); 5648c2ecf20Sopenharmony_ci 5658c2ecf20Sopenharmony_ci return 0; 5668c2ecf20Sopenharmony_ci} 5678c2ecf20Sopenharmony_ci 5688c2ecf20Sopenharmony_cistatic const struct of_device_id of_crypto_id[] = { 5698c2ecf20Sopenharmony_ci { .compatible = "mediatek,eip97-crypto" }, 5708c2ecf20Sopenharmony_ci {}, 5718c2ecf20Sopenharmony_ci}; 5728c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, of_crypto_id); 5738c2ecf20Sopenharmony_ci 5748c2ecf20Sopenharmony_cistatic struct platform_driver mtk_crypto_driver = { 5758c2ecf20Sopenharmony_ci .probe = mtk_crypto_probe, 5768c2ecf20Sopenharmony_ci .remove = mtk_crypto_remove, 5778c2ecf20Sopenharmony_ci .driver = { 5788c2ecf20Sopenharmony_ci .name = "mtk-crypto", 5798c2ecf20Sopenharmony_ci .of_match_table = of_crypto_id, 5808c2ecf20Sopenharmony_ci }, 5818c2ecf20Sopenharmony_ci}; 5828c2ecf20Sopenharmony_cimodule_platform_driver(mtk_crypto_driver); 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 5858c2ecf20Sopenharmony_ciMODULE_AUTHOR("Ryder Lee <ryder.lee@mediatek.com>"); 5868c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Cryptographic accelerator driver for EIP97"); 587