18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci// Copyright (C) 2005-2017 Andes Technology Corporation
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#ifndef L2_CACHE_H
58c2ecf20Sopenharmony_ci#define L2_CACHE_H
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci/* CCTL_CMD_OP */
88c2ecf20Sopenharmony_ci#define L2_CA_CONF_OFF		0x0
98c2ecf20Sopenharmony_ci#define L2_IF_CONF_OFF		0x4
108c2ecf20Sopenharmony_ci#define L2CC_SETUP_OFF		0x8
118c2ecf20Sopenharmony_ci#define L2CC_PROT_OFF		0xC
128c2ecf20Sopenharmony_ci#define L2CC_CTRL_OFF		0x10
138c2ecf20Sopenharmony_ci#define L2_INT_EN_OFF           0x20
148c2ecf20Sopenharmony_ci#define L2_STA_OFF              0x24
158c2ecf20Sopenharmony_ci#define RDERR_ADDR_OFF		0x28
168c2ecf20Sopenharmony_ci#define WRERR_ADDR_OFF		0x2c
178c2ecf20Sopenharmony_ci#define EVDPTERR_ADDR_OFF	0x30
188c2ecf20Sopenharmony_ci#define IMPL3ERR_ADDR_OFF	0x34
198c2ecf20Sopenharmony_ci#define L2_CNT0_CTRL_OFF        0x40
208c2ecf20Sopenharmony_ci#define L2_EVNT_CNT0_OFF        0x44
218c2ecf20Sopenharmony_ci#define L2_CNT1_CTRL_OFF        0x48
228c2ecf20Sopenharmony_ci#define L2_EVNT_CNT1_OFF        0x4c
238c2ecf20Sopenharmony_ci#define L2_CCTL_CMD_OFF		0x60
248c2ecf20Sopenharmony_ci#define L2_CCTL_STATUS_OFF	0x64
258c2ecf20Sopenharmony_ci#define L2_LINE_TAG_OFF		0x68
268c2ecf20Sopenharmony_ci#define L2_LINE_DPT_OFF		0x70
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define CCTL_CMD_L2_IX_INVAL    0x0
298c2ecf20Sopenharmony_ci#define CCTL_CMD_L2_PA_INVAL    0x1
308c2ecf20Sopenharmony_ci#define CCTL_CMD_L2_IX_WB       0x2
318c2ecf20Sopenharmony_ci#define CCTL_CMD_L2_PA_WB       0x3
328c2ecf20Sopenharmony_ci#define CCTL_CMD_L2_PA_WBINVAL  0x5
338c2ecf20Sopenharmony_ci#define CCTL_CMD_L2_SYNC        0xa
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* CCTL_CMD_TYPE */
368c2ecf20Sopenharmony_ci#define CCTL_SINGLE_CMD         0
378c2ecf20Sopenharmony_ci#define CCTL_BLOCK_CMD          0x10
388c2ecf20Sopenharmony_ci#define CCTL_ALL_CMD		0x10
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/******************************************************************************
418c2ecf20Sopenharmony_ci * L2_CA_CONF (Cache architecture configuration)
428c2ecf20Sopenharmony_ci *****************************************************************************/
438c2ecf20Sopenharmony_ci#define L2_CA_CONF_offL2SET		0
448c2ecf20Sopenharmony_ci#define L2_CA_CONF_offL2WAY		4
458c2ecf20Sopenharmony_ci#define L2_CA_CONF_offL2CLSZ            8
468c2ecf20Sopenharmony_ci#define L2_CA_CONF_offL2DW		11
478c2ecf20Sopenharmony_ci#define L2_CA_CONF_offL2PT		14
488c2ecf20Sopenharmony_ci#define L2_CA_CONF_offL2VER		16
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define L2_CA_CONF_mskL2SET	(0xFUL << L2_CA_CONF_offL2SET)
518c2ecf20Sopenharmony_ci#define L2_CA_CONF_mskL2WAY	(0xFUL << L2_CA_CONF_offL2WAY)
528c2ecf20Sopenharmony_ci#define L2_CA_CONF_mskL2CLSZ    (0x7UL << L2_CA_CONF_offL2CLSZ)
538c2ecf20Sopenharmony_ci#define L2_CA_CONF_mskL2DW	(0x7UL << L2_CA_CONF_offL2DW)
548c2ecf20Sopenharmony_ci#define L2_CA_CONF_mskL2PT	(0x3UL << L2_CA_CONF_offL2PT)
558c2ecf20Sopenharmony_ci#define L2_CA_CONF_mskL2VER	(0xFFFFUL << L2_CA_CONF_offL2VER)
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/******************************************************************************
588c2ecf20Sopenharmony_ci * L2CC_SETUP (L2CC Setup register)
598c2ecf20Sopenharmony_ci *****************************************************************************/
608c2ecf20Sopenharmony_ci#define L2CC_SETUP_offPART              0
618c2ecf20Sopenharmony_ci#define L2CC_SETUP_mskPART              (0x3UL << L2CC_SETUP_offPART)
628c2ecf20Sopenharmony_ci#define L2CC_SETUP_offDDLATC            4
638c2ecf20Sopenharmony_ci#define L2CC_SETUP_mskDDLATC            (0x3UL << L2CC_SETUP_offDDLATC)
648c2ecf20Sopenharmony_ci#define L2CC_SETUP_offTDLATC            8
658c2ecf20Sopenharmony_ci#define L2CC_SETUP_mskTDLATC            (0x3UL << L2CC_SETUP_offTDLATC)
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/******************************************************************************
688c2ecf20Sopenharmony_ci * L2CC_PROT (L2CC Protect register)
698c2ecf20Sopenharmony_ci *****************************************************************************/
708c2ecf20Sopenharmony_ci#define L2CC_PROT_offMRWEN              31
718c2ecf20Sopenharmony_ci#define L2CC_PROT_mskMRWEN      (0x1UL << L2CC_PROT_offMRWEN)
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci/******************************************************************************
748c2ecf20Sopenharmony_ci * L2_CCTL_STATUS_Mn (The L2CCTL command working status for Master n)
758c2ecf20Sopenharmony_ci *****************************************************************************/
768c2ecf20Sopenharmony_ci#define L2CC_CTRL_offEN                 31
778c2ecf20Sopenharmony_ci#define L2CC_CTRL_mskEN                 (0x1UL << L2CC_CTRL_offEN)
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/******************************************************************************
808c2ecf20Sopenharmony_ci * L2_CCTL_STATUS_Mn (The L2CCTL command working status for Master n)
818c2ecf20Sopenharmony_ci *****************************************************************************/
828c2ecf20Sopenharmony_ci#define L2_CCTL_STATUS_offCMD_COMP      31
838c2ecf20Sopenharmony_ci#define L2_CCTL_STATUS_mskCMD_COMP      (0x1 << L2_CCTL_STATUS_offCMD_COMP)
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ciextern void __iomem *atl2c_base;
868c2ecf20Sopenharmony_ci#include <linux/smp.h>
878c2ecf20Sopenharmony_ci#include <asm/io.h>
888c2ecf20Sopenharmony_ci#include <asm/bitfield.h>
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci#define L2C_R_REG(offset)               readl(atl2c_base + offset)
918c2ecf20Sopenharmony_ci#define L2C_W_REG(offset, value)        writel(value, atl2c_base + offset)
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci#define L2_CMD_RDY()    \
948c2ecf20Sopenharmony_ci        do{;}while((L2C_R_REG(L2_CCTL_STATUS_OFF) & L2_CCTL_STATUS_mskCMD_COMP) == 0)
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic inline unsigned long L2_CACHE_SET(void)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	return 64 << ((L2C_R_REG(L2_CA_CONF_OFF) & L2_CA_CONF_mskL2SET) >>
998c2ecf20Sopenharmony_ci		      L2_CA_CONF_offL2SET);
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic inline unsigned long L2_CACHE_WAY(void)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	return 1 +
1058c2ecf20Sopenharmony_ci	    ((L2C_R_REG(L2_CA_CONF_OFF) & L2_CA_CONF_mskL2WAY) >>
1068c2ecf20Sopenharmony_ci	     L2_CA_CONF_offL2WAY);
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic inline unsigned long L2_CACHE_LINE_SIZE(void)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	return 4 << ((L2C_R_REG(L2_CA_CONF_OFF) & L2_CA_CONF_mskL2CLSZ) >>
1138c2ecf20Sopenharmony_ci		     L2_CA_CONF_offL2CLSZ);
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic inline unsigned long GET_L2CC_CTRL_CPU(unsigned long cpu)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	if (cpu == smp_processor_id())
1198c2ecf20Sopenharmony_ci		return L2C_R_REG(L2CC_CTRL_OFF);
1208c2ecf20Sopenharmony_ci	return L2C_R_REG(L2CC_CTRL_OFF + (cpu << 8));
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic inline void SET_L2CC_CTRL_CPU(unsigned long cpu, unsigned long val)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	if (cpu == smp_processor_id())
1268c2ecf20Sopenharmony_ci		L2C_W_REG(L2CC_CTRL_OFF, val);
1278c2ecf20Sopenharmony_ci	else
1288c2ecf20Sopenharmony_ci		L2C_W_REG(L2CC_CTRL_OFF + (cpu << 8), val);
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic inline unsigned long GET_L2CC_STATUS_CPU(unsigned long cpu)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	if (cpu == smp_processor_id())
1348c2ecf20Sopenharmony_ci		return L2C_R_REG(L2_CCTL_STATUS_OFF);
1358c2ecf20Sopenharmony_ci	return L2C_R_REG(L2_CCTL_STATUS_OFF + (cpu << 8));
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci#endif
138