162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Universal Flash Storage Host controller driver
462306a36Sopenharmony_ci * Copyright (C) 2011-2013 Samsung India Software Operations
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Authors:
762306a36Sopenharmony_ci *	Santosh Yaraganavi <santosh.sy@samsung.com>
862306a36Sopenharmony_ci *	Vinayak Holikatti <h.vinayak@samsung.com>
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#ifndef _UFSHCI_H
1262306a36Sopenharmony_ci#define _UFSHCI_H
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include <linux/types.h>
1562306a36Sopenharmony_ci#include <ufs/ufs.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_cienum {
1862306a36Sopenharmony_ci	TASK_REQ_UPIU_SIZE_DWORDS	= 8,
1962306a36Sopenharmony_ci	TASK_RSP_UPIU_SIZE_DWORDS	= 8,
2062306a36Sopenharmony_ci	ALIGNED_UPIU_SIZE		= 512,
2162306a36Sopenharmony_ci};
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci/* UFSHCI Registers */
2462306a36Sopenharmony_cienum {
2562306a36Sopenharmony_ci	REG_CONTROLLER_CAPABILITIES		= 0x00,
2662306a36Sopenharmony_ci	REG_MCQCAP				= 0x04,
2762306a36Sopenharmony_ci	REG_UFS_VERSION				= 0x08,
2862306a36Sopenharmony_ci	REG_CONTROLLER_DEV_ID			= 0x10,
2962306a36Sopenharmony_ci	REG_CONTROLLER_PROD_ID			= 0x14,
3062306a36Sopenharmony_ci	REG_AUTO_HIBERNATE_IDLE_TIMER		= 0x18,
3162306a36Sopenharmony_ci	REG_INTERRUPT_STATUS			= 0x20,
3262306a36Sopenharmony_ci	REG_INTERRUPT_ENABLE			= 0x24,
3362306a36Sopenharmony_ci	REG_CONTROLLER_STATUS			= 0x30,
3462306a36Sopenharmony_ci	REG_CONTROLLER_ENABLE			= 0x34,
3562306a36Sopenharmony_ci	REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER	= 0x38,
3662306a36Sopenharmony_ci	REG_UIC_ERROR_CODE_DATA_LINK_LAYER	= 0x3C,
3762306a36Sopenharmony_ci	REG_UIC_ERROR_CODE_NETWORK_LAYER	= 0x40,
3862306a36Sopenharmony_ci	REG_UIC_ERROR_CODE_TRANSPORT_LAYER	= 0x44,
3962306a36Sopenharmony_ci	REG_UIC_ERROR_CODE_DME			= 0x48,
4062306a36Sopenharmony_ci	REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL	= 0x4C,
4162306a36Sopenharmony_ci	REG_UTP_TRANSFER_REQ_LIST_BASE_L	= 0x50,
4262306a36Sopenharmony_ci	REG_UTP_TRANSFER_REQ_LIST_BASE_H	= 0x54,
4362306a36Sopenharmony_ci	REG_UTP_TRANSFER_REQ_DOOR_BELL		= 0x58,
4462306a36Sopenharmony_ci	REG_UTP_TRANSFER_REQ_LIST_CLEAR		= 0x5C,
4562306a36Sopenharmony_ci	REG_UTP_TRANSFER_REQ_LIST_RUN_STOP	= 0x60,
4662306a36Sopenharmony_ci	REG_UTP_TASK_REQ_LIST_BASE_L		= 0x70,
4762306a36Sopenharmony_ci	REG_UTP_TASK_REQ_LIST_BASE_H		= 0x74,
4862306a36Sopenharmony_ci	REG_UTP_TASK_REQ_DOOR_BELL		= 0x78,
4962306a36Sopenharmony_ci	REG_UTP_TASK_REQ_LIST_CLEAR		= 0x7C,
5062306a36Sopenharmony_ci	REG_UTP_TASK_REQ_LIST_RUN_STOP		= 0x80,
5162306a36Sopenharmony_ci	REG_UIC_COMMAND				= 0x90,
5262306a36Sopenharmony_ci	REG_UIC_COMMAND_ARG_1			= 0x94,
5362306a36Sopenharmony_ci	REG_UIC_COMMAND_ARG_2			= 0x98,
5462306a36Sopenharmony_ci	REG_UIC_COMMAND_ARG_3			= 0x9C,
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	UFSHCI_REG_SPACE_SIZE			= 0xA0,
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	REG_UFS_CCAP				= 0x100,
5962306a36Sopenharmony_ci	REG_UFS_CRYPTOCAP			= 0x104,
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci	REG_UFS_MEM_CFG				= 0x300,
6262306a36Sopenharmony_ci	REG_UFS_MCQ_CFG				= 0x380,
6362306a36Sopenharmony_ci	REG_UFS_ESILBA				= 0x384,
6462306a36Sopenharmony_ci	REG_UFS_ESIUBA				= 0x388,
6562306a36Sopenharmony_ci	UFSHCI_CRYPTO_REG_SPACE_SIZE		= 0x400,
6662306a36Sopenharmony_ci};
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci/* Controller capability masks */
6962306a36Sopenharmony_cienum {
7062306a36Sopenharmony_ci	MASK_TRANSFER_REQUESTS_SLOTS		= 0x0000001F,
7162306a36Sopenharmony_ci	MASK_TASK_MANAGEMENT_REQUEST_SLOTS	= 0x00070000,
7262306a36Sopenharmony_ci	MASK_EHSLUTRD_SUPPORTED			= 0x00400000,
7362306a36Sopenharmony_ci	MASK_AUTO_HIBERN8_SUPPORT		= 0x00800000,
7462306a36Sopenharmony_ci	MASK_64_ADDRESSING_SUPPORT		= 0x01000000,
7562306a36Sopenharmony_ci	MASK_OUT_OF_ORDER_DATA_DELIVERY_SUPPORT	= 0x02000000,
7662306a36Sopenharmony_ci	MASK_UIC_DME_TEST_MODE_SUPPORT		= 0x04000000,
7762306a36Sopenharmony_ci	MASK_CRYPTO_SUPPORT			= 0x10000000,
7862306a36Sopenharmony_ci	MASK_MCQ_SUPPORT			= 0x40000000,
7962306a36Sopenharmony_ci};
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci/* MCQ capability mask */
8262306a36Sopenharmony_cienum {
8362306a36Sopenharmony_ci	MASK_EXT_IID_SUPPORT = 0x00000400,
8462306a36Sopenharmony_ci};
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_cienum {
8762306a36Sopenharmony_ci	REG_SQATTR		= 0x0,
8862306a36Sopenharmony_ci	REG_SQLBA		= 0x4,
8962306a36Sopenharmony_ci	REG_SQUBA		= 0x8,
9062306a36Sopenharmony_ci	REG_SQDAO		= 0xC,
9162306a36Sopenharmony_ci	REG_SQISAO		= 0x10,
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	REG_CQATTR		= 0x20,
9462306a36Sopenharmony_ci	REG_CQLBA		= 0x24,
9562306a36Sopenharmony_ci	REG_CQUBA		= 0x28,
9662306a36Sopenharmony_ci	REG_CQDAO		= 0x2C,
9762306a36Sopenharmony_ci	REG_CQISAO		= 0x30,
9862306a36Sopenharmony_ci};
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_cienum {
10162306a36Sopenharmony_ci	REG_SQHP		= 0x0,
10262306a36Sopenharmony_ci	REG_SQTP		= 0x4,
10362306a36Sopenharmony_ci	REG_SQRTC		= 0x8,
10462306a36Sopenharmony_ci	REG_SQCTI		= 0xC,
10562306a36Sopenharmony_ci	REG_SQRTS		= 0x10,
10662306a36Sopenharmony_ci};
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_cienum {
10962306a36Sopenharmony_ci	REG_CQHP		= 0x0,
11062306a36Sopenharmony_ci	REG_CQTP		= 0x4,
11162306a36Sopenharmony_ci};
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_cienum {
11462306a36Sopenharmony_ci	REG_CQIS		= 0x0,
11562306a36Sopenharmony_ci	REG_CQIE		= 0x4,
11662306a36Sopenharmony_ci};
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_cienum {
11962306a36Sopenharmony_ci	SQ_START		= 0x0,
12062306a36Sopenharmony_ci	SQ_STOP			= 0x1,
12162306a36Sopenharmony_ci	SQ_ICU			= 0x2,
12262306a36Sopenharmony_ci};
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_cienum {
12562306a36Sopenharmony_ci	SQ_STS			= 0x1,
12662306a36Sopenharmony_ci	SQ_CUS			= 0x2,
12762306a36Sopenharmony_ci};
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci#define SQ_ICU_ERR_CODE_MASK		GENMASK(7, 4)
13062306a36Sopenharmony_ci#define UFS_MASK(mask, offset)		((mask) << (offset))
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci/* UFS Version 08h */
13362306a36Sopenharmony_ci#define MINOR_VERSION_NUM_MASK		UFS_MASK(0xFFFF, 0)
13462306a36Sopenharmony_ci#define MAJOR_VERSION_NUM_MASK		UFS_MASK(0xFFFF, 16)
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci#define UFSHCD_NUM_RESERVED	1
13762306a36Sopenharmony_ci/*
13862306a36Sopenharmony_ci * Controller UFSHCI version
13962306a36Sopenharmony_ci * - 2.x and newer use the following scheme:
14062306a36Sopenharmony_ci *   major << 8 + minor << 4
14162306a36Sopenharmony_ci * - 1.x has been converted to match this in
14262306a36Sopenharmony_ci *   ufshcd_get_ufs_version()
14362306a36Sopenharmony_ci */
14462306a36Sopenharmony_cistatic inline u32 ufshci_version(u32 major, u32 minor)
14562306a36Sopenharmony_ci{
14662306a36Sopenharmony_ci	return (major << 8) + (minor << 4);
14762306a36Sopenharmony_ci}
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci/*
15062306a36Sopenharmony_ci * HCDDID - Host Controller Identification Descriptor
15162306a36Sopenharmony_ci *	  - Device ID and Device Class 10h
15262306a36Sopenharmony_ci */
15362306a36Sopenharmony_ci#define DEVICE_CLASS	UFS_MASK(0xFFFF, 0)
15462306a36Sopenharmony_ci#define DEVICE_ID	UFS_MASK(0xFF, 24)
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci/*
15762306a36Sopenharmony_ci * HCPMID - Host Controller Identification Descriptor
15862306a36Sopenharmony_ci *	  - Product/Manufacturer ID  14h
15962306a36Sopenharmony_ci */
16062306a36Sopenharmony_ci#define MANUFACTURE_ID_MASK	UFS_MASK(0xFFFF, 0)
16162306a36Sopenharmony_ci#define PRODUCT_ID_MASK		UFS_MASK(0xFFFF, 16)
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci/* AHIT - Auto-Hibernate Idle Timer */
16462306a36Sopenharmony_ci#define UFSHCI_AHIBERN8_TIMER_MASK		GENMASK(9, 0)
16562306a36Sopenharmony_ci#define UFSHCI_AHIBERN8_SCALE_MASK		GENMASK(12, 10)
16662306a36Sopenharmony_ci#define UFSHCI_AHIBERN8_SCALE_FACTOR		10
16762306a36Sopenharmony_ci#define UFSHCI_AHIBERN8_MAX			(1023 * 100000)
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci/*
17062306a36Sopenharmony_ci * IS - Interrupt Status - 20h
17162306a36Sopenharmony_ci */
17262306a36Sopenharmony_ci#define UTP_TRANSFER_REQ_COMPL			0x1
17362306a36Sopenharmony_ci#define UIC_DME_END_PT_RESET			0x2
17462306a36Sopenharmony_ci#define UIC_ERROR				0x4
17562306a36Sopenharmony_ci#define UIC_TEST_MODE				0x8
17662306a36Sopenharmony_ci#define UIC_POWER_MODE				0x10
17762306a36Sopenharmony_ci#define UIC_HIBERNATE_EXIT			0x20
17862306a36Sopenharmony_ci#define UIC_HIBERNATE_ENTER			0x40
17962306a36Sopenharmony_ci#define UIC_LINK_LOST				0x80
18062306a36Sopenharmony_ci#define UIC_LINK_STARTUP			0x100
18162306a36Sopenharmony_ci#define UTP_TASK_REQ_COMPL			0x200
18262306a36Sopenharmony_ci#define UIC_COMMAND_COMPL			0x400
18362306a36Sopenharmony_ci#define DEVICE_FATAL_ERROR			0x800
18462306a36Sopenharmony_ci#define CONTROLLER_FATAL_ERROR			0x10000
18562306a36Sopenharmony_ci#define SYSTEM_BUS_FATAL_ERROR			0x20000
18662306a36Sopenharmony_ci#define CRYPTO_ENGINE_FATAL_ERROR		0x40000
18762306a36Sopenharmony_ci#define MCQ_CQ_EVENT_STATUS			0x100000
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci#define UFSHCD_UIC_HIBERN8_MASK	(UIC_HIBERNATE_ENTER |\
19062306a36Sopenharmony_ci				UIC_HIBERNATE_EXIT)
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci#define UFSHCD_UIC_PWR_MASK	(UFSHCD_UIC_HIBERN8_MASK |\
19362306a36Sopenharmony_ci				UIC_POWER_MODE)
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci#define UFSHCD_UIC_MASK		(UIC_COMMAND_COMPL | UFSHCD_UIC_PWR_MASK)
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci#define UFSHCD_ERROR_MASK	(UIC_ERROR | INT_FATAL_ERRORS)
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci#define INT_FATAL_ERRORS	(DEVICE_FATAL_ERROR |\
20062306a36Sopenharmony_ci				CONTROLLER_FATAL_ERROR |\
20162306a36Sopenharmony_ci				SYSTEM_BUS_FATAL_ERROR |\
20262306a36Sopenharmony_ci				CRYPTO_ENGINE_FATAL_ERROR |\
20362306a36Sopenharmony_ci				UIC_LINK_LOST)
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci/* HCS - Host Controller Status 30h */
20662306a36Sopenharmony_ci#define DEVICE_PRESENT				0x1
20762306a36Sopenharmony_ci#define UTP_TRANSFER_REQ_LIST_READY		0x2
20862306a36Sopenharmony_ci#define UTP_TASK_REQ_LIST_READY			0x4
20962306a36Sopenharmony_ci#define UIC_COMMAND_READY			0x8
21062306a36Sopenharmony_ci#define HOST_ERROR_INDICATOR			0x10
21162306a36Sopenharmony_ci#define DEVICE_ERROR_INDICATOR			0x20
21262306a36Sopenharmony_ci#define UIC_POWER_MODE_CHANGE_REQ_STATUS_MASK	UFS_MASK(0x7, 8)
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci#define UFSHCD_STATUS_READY	(UTP_TRANSFER_REQ_LIST_READY |\
21562306a36Sopenharmony_ci				UTP_TASK_REQ_LIST_READY |\
21662306a36Sopenharmony_ci				UIC_COMMAND_READY)
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_cienum {
21962306a36Sopenharmony_ci	PWR_OK		= 0x0,
22062306a36Sopenharmony_ci	PWR_LOCAL	= 0x01,
22162306a36Sopenharmony_ci	PWR_REMOTE	= 0x02,
22262306a36Sopenharmony_ci	PWR_BUSY	= 0x03,
22362306a36Sopenharmony_ci	PWR_ERROR_CAP	= 0x04,
22462306a36Sopenharmony_ci	PWR_FATAL_ERROR	= 0x05,
22562306a36Sopenharmony_ci};
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci/* HCE - Host Controller Enable 34h */
22862306a36Sopenharmony_ci#define CONTROLLER_ENABLE	0x1
22962306a36Sopenharmony_ci#define CONTROLLER_DISABLE	0x0
23062306a36Sopenharmony_ci#define CRYPTO_GENERAL_ENABLE	0x2
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ci/* UECPA - Host UIC Error Code PHY Adapter Layer 38h */
23362306a36Sopenharmony_ci#define UIC_PHY_ADAPTER_LAYER_ERROR			0x80000000
23462306a36Sopenharmony_ci#define UIC_PHY_ADAPTER_LAYER_ERROR_CODE_MASK		0x1F
23562306a36Sopenharmony_ci#define UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK		0xF
23662306a36Sopenharmony_ci#define UIC_PHY_ADAPTER_LAYER_GENERIC_ERROR		0x10
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci/* UECDL - Host UIC Error Code Data Link Layer 3Ch */
23962306a36Sopenharmony_ci#define UIC_DATA_LINK_LAYER_ERROR		0x80000000
24062306a36Sopenharmony_ci#define UIC_DATA_LINK_LAYER_ERROR_CODE_MASK	0xFFFF
24162306a36Sopenharmony_ci#define UIC_DATA_LINK_LAYER_ERROR_TCX_REP_TIMER_EXP	0x2
24262306a36Sopenharmony_ci#define UIC_DATA_LINK_LAYER_ERROR_AFCX_REQ_TIMER_EXP	0x4
24362306a36Sopenharmony_ci#define UIC_DATA_LINK_LAYER_ERROR_FCX_PRO_TIMER_EXP	0x8
24462306a36Sopenharmony_ci#define UIC_DATA_LINK_LAYER_ERROR_RX_BUF_OF	0x20
24562306a36Sopenharmony_ci#define UIC_DATA_LINK_LAYER_ERROR_PA_INIT	0x2000
24662306a36Sopenharmony_ci#define UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED	0x0001
24762306a36Sopenharmony_ci#define UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT 0x0002
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci/* UECN - Host UIC Error Code Network Layer 40h */
25062306a36Sopenharmony_ci#define UIC_NETWORK_LAYER_ERROR			0x80000000
25162306a36Sopenharmony_ci#define UIC_NETWORK_LAYER_ERROR_CODE_MASK	0x7
25262306a36Sopenharmony_ci#define UIC_NETWORK_UNSUPPORTED_HEADER_TYPE	0x1
25362306a36Sopenharmony_ci#define UIC_NETWORK_BAD_DEVICEID_ENC		0x2
25462306a36Sopenharmony_ci#define UIC_NETWORK_LHDR_TRAP_PACKET_DROPPING	0x4
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci/* UECT - Host UIC Error Code Transport Layer 44h */
25762306a36Sopenharmony_ci#define UIC_TRANSPORT_LAYER_ERROR		0x80000000
25862306a36Sopenharmony_ci#define UIC_TRANSPORT_LAYER_ERROR_CODE_MASK	0x7F
25962306a36Sopenharmony_ci#define UIC_TRANSPORT_UNSUPPORTED_HEADER_TYPE	0x1
26062306a36Sopenharmony_ci#define UIC_TRANSPORT_UNKNOWN_CPORTID		0x2
26162306a36Sopenharmony_ci#define UIC_TRANSPORT_NO_CONNECTION_RX		0x4
26262306a36Sopenharmony_ci#define UIC_TRANSPORT_CONTROLLED_SEGMENT_DROPPING	0x8
26362306a36Sopenharmony_ci#define UIC_TRANSPORT_BAD_TC			0x10
26462306a36Sopenharmony_ci#define UIC_TRANSPORT_E2E_CREDIT_OVERFOW	0x20
26562306a36Sopenharmony_ci#define UIC_TRANSPORT_SAFETY_VALUE_DROPPING	0x40
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci/* UECDME - Host UIC Error Code DME 48h */
26862306a36Sopenharmony_ci#define UIC_DME_ERROR			0x80000000
26962306a36Sopenharmony_ci#define UIC_DME_ERROR_CODE_MASK		0x1
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ci/* UTRIACR - Interrupt Aggregation control register - 0x4Ch */
27262306a36Sopenharmony_ci#define INT_AGGR_TIMEOUT_VAL_MASK		0xFF
27362306a36Sopenharmony_ci#define INT_AGGR_COUNTER_THRESHOLD_MASK		UFS_MASK(0x1F, 8)
27462306a36Sopenharmony_ci#define INT_AGGR_COUNTER_AND_TIMER_RESET	0x10000
27562306a36Sopenharmony_ci#define INT_AGGR_STATUS_BIT			0x100000
27662306a36Sopenharmony_ci#define INT_AGGR_PARAM_WRITE			0x1000000
27762306a36Sopenharmony_ci#define INT_AGGR_ENABLE				0x80000000
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ci/* UTRLRSR - UTP Transfer Request Run-Stop Register 60h */
28062306a36Sopenharmony_ci#define UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT	0x1
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci/* UTMRLRSR - UTP Task Management Request Run-Stop Register 80h */
28362306a36Sopenharmony_ci#define UTP_TASK_REQ_LIST_RUN_STOP_BIT		0x1
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ci/* CQISy - CQ y Interrupt Status Register  */
28662306a36Sopenharmony_ci#define UFSHCD_MCQ_CQIS_TAIL_ENT_PUSH_STS	0x1
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci/* UICCMD - UIC Command */
28962306a36Sopenharmony_ci#define COMMAND_OPCODE_MASK		0xFF
29062306a36Sopenharmony_ci#define GEN_SELECTOR_INDEX_MASK		0xFFFF
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci#define MIB_ATTRIBUTE_MASK		UFS_MASK(0xFFFF, 16)
29362306a36Sopenharmony_ci#define RESET_LEVEL			0xFF
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci#define ATTR_SET_TYPE_MASK		UFS_MASK(0xFF, 16)
29662306a36Sopenharmony_ci#define CONFIG_RESULT_CODE_MASK		0xFF
29762306a36Sopenharmony_ci#define GENERIC_ERROR_CODE_MASK		0xFF
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci/* GenSelectorIndex calculation macros for M-PHY attributes */
30062306a36Sopenharmony_ci#define UIC_ARG_MPHY_TX_GEN_SEL_INDEX(lane) (lane)
30162306a36Sopenharmony_ci#define UIC_ARG_MPHY_RX_GEN_SEL_INDEX(lane) (PA_MAXDATALANES + (lane))
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci#define UIC_ARG_MIB_SEL(attr, sel)	((((attr) & 0xFFFF) << 16) |\
30462306a36Sopenharmony_ci					 ((sel) & 0xFFFF))
30562306a36Sopenharmony_ci#define UIC_ARG_MIB(attr)		UIC_ARG_MIB_SEL(attr, 0)
30662306a36Sopenharmony_ci#define UIC_ARG_ATTR_TYPE(t)		(((t) & 0xFF) << 16)
30762306a36Sopenharmony_ci#define UIC_GET_ATTR_ID(v)		(((v) >> 16) & 0xFFFF)
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_ci/* Link Status*/
31062306a36Sopenharmony_cienum link_status {
31162306a36Sopenharmony_ci	UFSHCD_LINK_IS_DOWN	= 1,
31262306a36Sopenharmony_ci	UFSHCD_LINK_IS_UP	= 2,
31362306a36Sopenharmony_ci};
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_ci/* UIC Commands */
31662306a36Sopenharmony_cienum uic_cmd_dme {
31762306a36Sopenharmony_ci	UIC_CMD_DME_GET			= 0x01,
31862306a36Sopenharmony_ci	UIC_CMD_DME_SET			= 0x02,
31962306a36Sopenharmony_ci	UIC_CMD_DME_PEER_GET		= 0x03,
32062306a36Sopenharmony_ci	UIC_CMD_DME_PEER_SET		= 0x04,
32162306a36Sopenharmony_ci	UIC_CMD_DME_POWERON		= 0x10,
32262306a36Sopenharmony_ci	UIC_CMD_DME_POWEROFF		= 0x11,
32362306a36Sopenharmony_ci	UIC_CMD_DME_ENABLE		= 0x12,
32462306a36Sopenharmony_ci	UIC_CMD_DME_RESET		= 0x14,
32562306a36Sopenharmony_ci	UIC_CMD_DME_END_PT_RST		= 0x15,
32662306a36Sopenharmony_ci	UIC_CMD_DME_LINK_STARTUP	= 0x16,
32762306a36Sopenharmony_ci	UIC_CMD_DME_HIBER_ENTER		= 0x17,
32862306a36Sopenharmony_ci	UIC_CMD_DME_HIBER_EXIT		= 0x18,
32962306a36Sopenharmony_ci	UIC_CMD_DME_TEST_MODE		= 0x1A,
33062306a36Sopenharmony_ci};
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci/* UIC Config result code / Generic error code */
33362306a36Sopenharmony_cienum {
33462306a36Sopenharmony_ci	UIC_CMD_RESULT_SUCCESS			= 0x00,
33562306a36Sopenharmony_ci	UIC_CMD_RESULT_INVALID_ATTR		= 0x01,
33662306a36Sopenharmony_ci	UIC_CMD_RESULT_FAILURE			= 0x01,
33762306a36Sopenharmony_ci	UIC_CMD_RESULT_INVALID_ATTR_VALUE	= 0x02,
33862306a36Sopenharmony_ci	UIC_CMD_RESULT_READ_ONLY_ATTR		= 0x03,
33962306a36Sopenharmony_ci	UIC_CMD_RESULT_WRITE_ONLY_ATTR		= 0x04,
34062306a36Sopenharmony_ci	UIC_CMD_RESULT_BAD_INDEX		= 0x05,
34162306a36Sopenharmony_ci	UIC_CMD_RESULT_LOCKED_ATTR		= 0x06,
34262306a36Sopenharmony_ci	UIC_CMD_RESULT_BAD_TEST_FEATURE_INDEX	= 0x07,
34362306a36Sopenharmony_ci	UIC_CMD_RESULT_PEER_COMM_FAILURE	= 0x08,
34462306a36Sopenharmony_ci	UIC_CMD_RESULT_BUSY			= 0x09,
34562306a36Sopenharmony_ci	UIC_CMD_RESULT_DME_FAILURE		= 0x0A,
34662306a36Sopenharmony_ci};
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ci#define MASK_UIC_COMMAND_RESULT			0xFF
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ci#define INT_AGGR_COUNTER_THLD_VAL(c)	(((c) & 0x1F) << 8)
35162306a36Sopenharmony_ci#define INT_AGGR_TIMEOUT_VAL(t)		(((t) & 0xFF) << 0)
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_ci/* Interrupt disable masks */
35462306a36Sopenharmony_cienum {
35562306a36Sopenharmony_ci	/* Interrupt disable mask for UFSHCI v1.0 */
35662306a36Sopenharmony_ci	INTERRUPT_MASK_ALL_VER_10	= 0x30FFF,
35762306a36Sopenharmony_ci	INTERRUPT_MASK_RW_VER_10	= 0x30000,
35862306a36Sopenharmony_ci
35962306a36Sopenharmony_ci	/* Interrupt disable mask for UFSHCI v1.1 */
36062306a36Sopenharmony_ci	INTERRUPT_MASK_ALL_VER_11	= 0x31FFF,
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci	/* Interrupt disable mask for UFSHCI v2.1 */
36362306a36Sopenharmony_ci	INTERRUPT_MASK_ALL_VER_21	= 0x71FFF,
36462306a36Sopenharmony_ci};
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_ci/* CCAP - Crypto Capability 100h */
36762306a36Sopenharmony_ciunion ufs_crypto_capabilities {
36862306a36Sopenharmony_ci	__le32 reg_val;
36962306a36Sopenharmony_ci	struct {
37062306a36Sopenharmony_ci		u8 num_crypto_cap;
37162306a36Sopenharmony_ci		u8 config_count;
37262306a36Sopenharmony_ci		u8 reserved;
37362306a36Sopenharmony_ci		u8 config_array_ptr;
37462306a36Sopenharmony_ci	};
37562306a36Sopenharmony_ci};
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_cienum ufs_crypto_key_size {
37862306a36Sopenharmony_ci	UFS_CRYPTO_KEY_SIZE_INVALID	= 0x0,
37962306a36Sopenharmony_ci	UFS_CRYPTO_KEY_SIZE_128		= 0x1,
38062306a36Sopenharmony_ci	UFS_CRYPTO_KEY_SIZE_192		= 0x2,
38162306a36Sopenharmony_ci	UFS_CRYPTO_KEY_SIZE_256		= 0x3,
38262306a36Sopenharmony_ci	UFS_CRYPTO_KEY_SIZE_512		= 0x4,
38362306a36Sopenharmony_ci};
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_cienum ufs_crypto_alg {
38662306a36Sopenharmony_ci	UFS_CRYPTO_ALG_AES_XTS			= 0x0,
38762306a36Sopenharmony_ci	UFS_CRYPTO_ALG_BITLOCKER_AES_CBC	= 0x1,
38862306a36Sopenharmony_ci	UFS_CRYPTO_ALG_AES_ECB			= 0x2,
38962306a36Sopenharmony_ci	UFS_CRYPTO_ALG_ESSIV_AES_CBC		= 0x3,
39062306a36Sopenharmony_ci};
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_ci/* x-CRYPTOCAP - Crypto Capability X */
39362306a36Sopenharmony_ciunion ufs_crypto_cap_entry {
39462306a36Sopenharmony_ci	__le32 reg_val;
39562306a36Sopenharmony_ci	struct {
39662306a36Sopenharmony_ci		u8 algorithm_id;
39762306a36Sopenharmony_ci		u8 sdus_mask; /* Supported data unit size mask */
39862306a36Sopenharmony_ci		u8 key_size;
39962306a36Sopenharmony_ci		u8 reserved;
40062306a36Sopenharmony_ci	};
40162306a36Sopenharmony_ci};
40262306a36Sopenharmony_ci
40362306a36Sopenharmony_ci#define UFS_CRYPTO_CONFIGURATION_ENABLE (1 << 7)
40462306a36Sopenharmony_ci#define UFS_CRYPTO_KEY_MAX_SIZE 64
40562306a36Sopenharmony_ci/* x-CRYPTOCFG - Crypto Configuration X */
40662306a36Sopenharmony_ciunion ufs_crypto_cfg_entry {
40762306a36Sopenharmony_ci	__le32 reg_val[32];
40862306a36Sopenharmony_ci	struct {
40962306a36Sopenharmony_ci		u8 crypto_key[UFS_CRYPTO_KEY_MAX_SIZE];
41062306a36Sopenharmony_ci		u8 data_unit_size;
41162306a36Sopenharmony_ci		u8 crypto_cap_idx;
41262306a36Sopenharmony_ci		u8 reserved_1;
41362306a36Sopenharmony_ci		u8 config_enable;
41462306a36Sopenharmony_ci		u8 reserved_multi_host;
41562306a36Sopenharmony_ci		u8 reserved_2;
41662306a36Sopenharmony_ci		u8 vsb[2];
41762306a36Sopenharmony_ci		u8 reserved_3[56];
41862306a36Sopenharmony_ci	};
41962306a36Sopenharmony_ci};
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_ci/*
42262306a36Sopenharmony_ci * Request Descriptor Definitions
42362306a36Sopenharmony_ci */
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_ci/* Transfer request command type */
42662306a36Sopenharmony_cienum {
42762306a36Sopenharmony_ci	UTP_CMD_TYPE_SCSI		= 0x0,
42862306a36Sopenharmony_ci	UTP_CMD_TYPE_UFS		= 0x1,
42962306a36Sopenharmony_ci	UTP_CMD_TYPE_DEV_MANAGE		= 0x2,
43062306a36Sopenharmony_ci};
43162306a36Sopenharmony_ci
43262306a36Sopenharmony_ci/* To accommodate UFS2.0 required Command type */
43362306a36Sopenharmony_cienum {
43462306a36Sopenharmony_ci	UTP_CMD_TYPE_UFS_STORAGE	= 0x1,
43562306a36Sopenharmony_ci};
43662306a36Sopenharmony_ci
43762306a36Sopenharmony_cienum {
43862306a36Sopenharmony_ci	UTP_SCSI_COMMAND		= 0x00000000,
43962306a36Sopenharmony_ci	UTP_NATIVE_UFS_COMMAND		= 0x10000000,
44062306a36Sopenharmony_ci	UTP_DEVICE_MANAGEMENT_FUNCTION	= 0x20000000,
44162306a36Sopenharmony_ci};
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci/* UTP Transfer Request Data Direction (DD) */
44462306a36Sopenharmony_cienum utp_data_direction {
44562306a36Sopenharmony_ci	UTP_NO_DATA_TRANSFER	= 0,
44662306a36Sopenharmony_ci	UTP_HOST_TO_DEVICE	= 1,
44762306a36Sopenharmony_ci	UTP_DEVICE_TO_HOST	= 2,
44862306a36Sopenharmony_ci};
44962306a36Sopenharmony_ci
45062306a36Sopenharmony_ci/* Overall command status values */
45162306a36Sopenharmony_cienum utp_ocs {
45262306a36Sopenharmony_ci	OCS_SUCCESS			= 0x0,
45362306a36Sopenharmony_ci	OCS_INVALID_CMD_TABLE_ATTR	= 0x1,
45462306a36Sopenharmony_ci	OCS_INVALID_PRDT_ATTR		= 0x2,
45562306a36Sopenharmony_ci	OCS_MISMATCH_DATA_BUF_SIZE	= 0x3,
45662306a36Sopenharmony_ci	OCS_MISMATCH_RESP_UPIU_SIZE	= 0x4,
45762306a36Sopenharmony_ci	OCS_PEER_COMM_FAILURE		= 0x5,
45862306a36Sopenharmony_ci	OCS_ABORTED			= 0x6,
45962306a36Sopenharmony_ci	OCS_FATAL_ERROR			= 0x7,
46062306a36Sopenharmony_ci	OCS_DEVICE_FATAL_ERROR		= 0x8,
46162306a36Sopenharmony_ci	OCS_INVALID_CRYPTO_CONFIG	= 0x9,
46262306a36Sopenharmony_ci	OCS_GENERAL_CRYPTO_ERROR	= 0xA,
46362306a36Sopenharmony_ci	OCS_INVALID_COMMAND_STATUS	= 0x0F,
46462306a36Sopenharmony_ci};
46562306a36Sopenharmony_ci
46662306a36Sopenharmony_cienum {
46762306a36Sopenharmony_ci	MASK_OCS			= 0x0F,
46862306a36Sopenharmony_ci};
46962306a36Sopenharmony_ci
47062306a36Sopenharmony_ci/* The maximum length of the data byte count field in the PRDT is 256KB */
47162306a36Sopenharmony_ci#define PRDT_DATA_BYTE_COUNT_MAX	SZ_256K
47262306a36Sopenharmony_ci/* The granularity of the data byte count field in the PRDT is 32-bit */
47362306a36Sopenharmony_ci#define PRDT_DATA_BYTE_COUNT_PAD	4
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ci/**
47662306a36Sopenharmony_ci * struct ufshcd_sg_entry - UFSHCI PRD Entry
47762306a36Sopenharmony_ci * @addr: Physical address; DW-0 and DW-1.
47862306a36Sopenharmony_ci * @reserved: Reserved for future use DW-2
47962306a36Sopenharmony_ci * @size: size of physical segment DW-3
48062306a36Sopenharmony_ci */
48162306a36Sopenharmony_cistruct ufshcd_sg_entry {
48262306a36Sopenharmony_ci	__le64    addr;
48362306a36Sopenharmony_ci	__le32    reserved;
48462306a36Sopenharmony_ci	__le32    size;
48562306a36Sopenharmony_ci	/*
48662306a36Sopenharmony_ci	 * followed by variant-specific fields if
48762306a36Sopenharmony_ci	 * CONFIG_SCSI_UFS_VARIABLE_SG_ENTRY_SIZE has been defined.
48862306a36Sopenharmony_ci	 */
48962306a36Sopenharmony_ci};
49062306a36Sopenharmony_ci
49162306a36Sopenharmony_ci/**
49262306a36Sopenharmony_ci * struct utp_transfer_cmd_desc - UTP Command Descriptor (UCD)
49362306a36Sopenharmony_ci * @command_upiu: Command UPIU Frame address
49462306a36Sopenharmony_ci * @response_upiu: Response UPIU Frame address
49562306a36Sopenharmony_ci * @prd_table: Physical Region Descriptor: an array of SG_ALL struct
49662306a36Sopenharmony_ci *	ufshcd_sg_entry's.  Variant-specific fields may be present after each.
49762306a36Sopenharmony_ci */
49862306a36Sopenharmony_cistruct utp_transfer_cmd_desc {
49962306a36Sopenharmony_ci	u8 command_upiu[ALIGNED_UPIU_SIZE];
50062306a36Sopenharmony_ci	u8 response_upiu[ALIGNED_UPIU_SIZE];
50162306a36Sopenharmony_ci	u8 prd_table[];
50262306a36Sopenharmony_ci};
50362306a36Sopenharmony_ci
50462306a36Sopenharmony_ci/**
50562306a36Sopenharmony_ci * struct request_desc_header - Descriptor Header common to both UTRD and UTMRD
50662306a36Sopenharmony_ci */
50762306a36Sopenharmony_cistruct request_desc_header {
50862306a36Sopenharmony_ci	u8 cci;
50962306a36Sopenharmony_ci	u8 ehs_length;
51062306a36Sopenharmony_ci#if defined(__BIG_ENDIAN)
51162306a36Sopenharmony_ci	u8 enable_crypto:1;
51262306a36Sopenharmony_ci	u8 reserved2:7;
51362306a36Sopenharmony_ci
51462306a36Sopenharmony_ci	u8 command_type:4;
51562306a36Sopenharmony_ci	u8 reserved1:1;
51662306a36Sopenharmony_ci	u8 data_direction:2;
51762306a36Sopenharmony_ci	u8 interrupt:1;
51862306a36Sopenharmony_ci#elif defined(__LITTLE_ENDIAN)
51962306a36Sopenharmony_ci	u8 reserved2:7;
52062306a36Sopenharmony_ci	u8 enable_crypto:1;
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_ci	u8 interrupt:1;
52362306a36Sopenharmony_ci	u8 data_direction:2;
52462306a36Sopenharmony_ci	u8 reserved1:1;
52562306a36Sopenharmony_ci	u8 command_type:4;
52662306a36Sopenharmony_ci#else
52762306a36Sopenharmony_ci#error
52862306a36Sopenharmony_ci#endif
52962306a36Sopenharmony_ci
53062306a36Sopenharmony_ci	__le32 dunl;
53162306a36Sopenharmony_ci	u8 ocs;
53262306a36Sopenharmony_ci	u8 cds;
53362306a36Sopenharmony_ci	__le16 ldbc;
53462306a36Sopenharmony_ci	__le32 dunu;
53562306a36Sopenharmony_ci};
53662306a36Sopenharmony_ci
53762306a36Sopenharmony_cistatic_assert(sizeof(struct request_desc_header) == 16);
53862306a36Sopenharmony_ci
53962306a36Sopenharmony_ci/**
54062306a36Sopenharmony_ci * struct utp_transfer_req_desc - UTP Transfer Request Descriptor (UTRD)
54162306a36Sopenharmony_ci * @header: UTRD header DW-0 to DW-3
54262306a36Sopenharmony_ci * @command_desc_base_addr: UCD base address DW 4-5
54362306a36Sopenharmony_ci * @response_upiu_length: response UPIU length DW-6
54462306a36Sopenharmony_ci * @response_upiu_offset: response UPIU offset DW-6
54562306a36Sopenharmony_ci * @prd_table_length: Physical region descriptor length DW-7
54662306a36Sopenharmony_ci * @prd_table_offset: Physical region descriptor offset DW-7
54762306a36Sopenharmony_ci */
54862306a36Sopenharmony_cistruct utp_transfer_req_desc {
54962306a36Sopenharmony_ci
55062306a36Sopenharmony_ci	/* DW 0-3 */
55162306a36Sopenharmony_ci	struct request_desc_header header;
55262306a36Sopenharmony_ci
55362306a36Sopenharmony_ci	/* DW 4-5*/
55462306a36Sopenharmony_ci	__le64  command_desc_base_addr;
55562306a36Sopenharmony_ci
55662306a36Sopenharmony_ci	/* DW 6 */
55762306a36Sopenharmony_ci	__le16  response_upiu_length;
55862306a36Sopenharmony_ci	__le16  response_upiu_offset;
55962306a36Sopenharmony_ci
56062306a36Sopenharmony_ci	/* DW 7 */
56162306a36Sopenharmony_ci	__le16  prd_table_length;
56262306a36Sopenharmony_ci	__le16  prd_table_offset;
56362306a36Sopenharmony_ci};
56462306a36Sopenharmony_ci
56562306a36Sopenharmony_ci/* MCQ Completion Queue Entry */
56662306a36Sopenharmony_cistruct cq_entry {
56762306a36Sopenharmony_ci	/* DW 0-1 */
56862306a36Sopenharmony_ci	__le64 command_desc_base_addr;
56962306a36Sopenharmony_ci
57062306a36Sopenharmony_ci	/* DW 2 */
57162306a36Sopenharmony_ci	__le16  response_upiu_length;
57262306a36Sopenharmony_ci	__le16  response_upiu_offset;
57362306a36Sopenharmony_ci
57462306a36Sopenharmony_ci	/* DW 3 */
57562306a36Sopenharmony_ci	__le16  prd_table_length;
57662306a36Sopenharmony_ci	__le16  prd_table_offset;
57762306a36Sopenharmony_ci
57862306a36Sopenharmony_ci	/* DW 4 */
57962306a36Sopenharmony_ci	__le32 status;
58062306a36Sopenharmony_ci
58162306a36Sopenharmony_ci	/* DW 5-7 */
58262306a36Sopenharmony_ci	__le32 reserved[3];
58362306a36Sopenharmony_ci};
58462306a36Sopenharmony_ci
58562306a36Sopenharmony_cistatic_assert(sizeof(struct cq_entry) == 32);
58662306a36Sopenharmony_ci
58762306a36Sopenharmony_ci/*
58862306a36Sopenharmony_ci * UTMRD structure.
58962306a36Sopenharmony_ci */
59062306a36Sopenharmony_cistruct utp_task_req_desc {
59162306a36Sopenharmony_ci	/* DW 0-3 */
59262306a36Sopenharmony_ci	struct request_desc_header header;
59362306a36Sopenharmony_ci
59462306a36Sopenharmony_ci	/* DW 4-11 - Task request UPIU structure */
59562306a36Sopenharmony_ci	struct {
59662306a36Sopenharmony_ci		struct utp_upiu_header	req_header;
59762306a36Sopenharmony_ci		__be32			input_param1;
59862306a36Sopenharmony_ci		__be32			input_param2;
59962306a36Sopenharmony_ci		__be32			input_param3;
60062306a36Sopenharmony_ci		__be32			__reserved1[2];
60162306a36Sopenharmony_ci	} upiu_req;
60262306a36Sopenharmony_ci
60362306a36Sopenharmony_ci	/* DW 12-19 - Task Management Response UPIU structure */
60462306a36Sopenharmony_ci	struct {
60562306a36Sopenharmony_ci		struct utp_upiu_header	rsp_header;
60662306a36Sopenharmony_ci		__be32			output_param1;
60762306a36Sopenharmony_ci		__be32			output_param2;
60862306a36Sopenharmony_ci		__be32			__reserved2[3];
60962306a36Sopenharmony_ci	} upiu_rsp;
61062306a36Sopenharmony_ci};
61162306a36Sopenharmony_ci
61262306a36Sopenharmony_ci#endif /* End of Header */
613