blob: 90cbf87a3a4a85afc4190e7031802284da27dc95 [file] [log] [blame]
Neil Armstrongac4fa422024-12-30 11:30:58 +01001/* SPDX-License-Identifier: GPL-2.0+ */
2#ifndef __UFSHCI_H
3#define __UFSHCI_H
4
5enum {
6 TASK_REQ_UPIU_SIZE_DWORDS = 8,
7 TASK_RSP_UPIU_SIZE_DWORDS = 8,
8 ALIGNED_UPIU_SIZE = 512,
9};
10
Neil Armstrongac4fa422024-12-30 11:30:58 +010011/* UFSHCI Registers */
12enum {
13 REG_CONTROLLER_CAPABILITIES = 0x00,
Neil Armstrong23bc5e82024-12-30 11:31:00 +010014 REG_MCQCAP = 0x04,
Neil Armstrongac4fa422024-12-30 11:30:58 +010015 REG_UFS_VERSION = 0x08,
Neil Armstrong23bc5e82024-12-30 11:31:00 +010016 REG_EXT_CONTROLLER_CAPABILITIES = 0x0C,
17 REG_CONTROLLER_PID = 0x10,
18 REG_CONTROLLER_MID = 0x14,
Neil Armstrongac4fa422024-12-30 11:30:58 +010019 REG_AUTO_HIBERNATE_IDLE_TIMER = 0x18,
20 REG_INTERRUPT_STATUS = 0x20,
21 REG_INTERRUPT_ENABLE = 0x24,
22 REG_CONTROLLER_STATUS = 0x30,
23 REG_CONTROLLER_ENABLE = 0x34,
24 REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER = 0x38,
25 REG_UIC_ERROR_CODE_DATA_LINK_LAYER = 0x3C,
26 REG_UIC_ERROR_CODE_NETWORK_LAYER = 0x40,
27 REG_UIC_ERROR_CODE_TRANSPORT_LAYER = 0x44,
28 REG_UIC_ERROR_CODE_DME = 0x48,
29 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL = 0x4C,
30 REG_UTP_TRANSFER_REQ_LIST_BASE_L = 0x50,
31 REG_UTP_TRANSFER_REQ_LIST_BASE_H = 0x54,
32 REG_UTP_TRANSFER_REQ_DOOR_BELL = 0x58,
33 REG_UTP_TRANSFER_REQ_LIST_CLEAR = 0x5C,
34 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP = 0x60,
35 REG_UTP_TASK_REQ_LIST_BASE_L = 0x70,
36 REG_UTP_TASK_REQ_LIST_BASE_H = 0x74,
37 REG_UTP_TASK_REQ_DOOR_BELL = 0x78,
38 REG_UTP_TASK_REQ_LIST_CLEAR = 0x7C,
39 REG_UTP_TASK_REQ_LIST_RUN_STOP = 0x80,
40 REG_UIC_COMMAND = 0x90,
41 REG_UIC_COMMAND_ARG_1 = 0x94,
42 REG_UIC_COMMAND_ARG_2 = 0x98,
43 REG_UIC_COMMAND_ARG_3 = 0x9C,
44
45 UFSHCI_REG_SPACE_SIZE = 0xA0,
46
47 REG_UFS_CCAP = 0x100,
48 REG_UFS_CRYPTOCAP = 0x104,
49
Neil Armstrong23bc5e82024-12-30 11:31:00 +010050 REG_UFS_MEM_CFG = 0x300,
51 REG_UFS_MCQ_CFG = 0x380,
52 REG_UFS_ESILBA = 0x384,
53 REG_UFS_ESIUBA = 0x388,
Neil Armstrongac4fa422024-12-30 11:30:58 +010054 UFSHCI_CRYPTO_REG_SPACE_SIZE = 0x400,
55};
56
57/* Controller capability masks */
58enum {
Neil Armstrong23bc5e82024-12-30 11:31:00 +010059 MASK_TRANSFER_REQUESTS_SLOTS_SDB = 0x0000001F,
60 MASK_TRANSFER_REQUESTS_SLOTS_MCQ = 0x000000FF,
61 MASK_NUMBER_OUTSTANDING_RTT = 0x0000FF00,
Neil Armstrongac4fa422024-12-30 11:30:58 +010062 MASK_TASK_MANAGEMENT_REQUEST_SLOTS = 0x00070000,
Neil Armstrong23bc5e82024-12-30 11:31:00 +010063 MASK_EHSLUTRD_SUPPORTED = 0x00400000,
Neil Armstrongac4fa422024-12-30 11:30:58 +010064 MASK_AUTO_HIBERN8_SUPPORT = 0x00800000,
65 MASK_64_ADDRESSING_SUPPORT = 0x01000000,
66 MASK_OUT_OF_ORDER_DATA_DELIVERY_SUPPORT = 0x02000000,
67 MASK_UIC_DME_TEST_MODE_SUPPORT = 0x04000000,
Neil Armstrong23bc5e82024-12-30 11:31:00 +010068 MASK_CRYPTO_SUPPORT = 0x10000000,
69 MASK_LSDB_SUPPORT = 0x20000000,
70 MASK_MCQ_SUPPORT = 0x40000000,
71};
72
73/* MCQ capability mask */
74enum {
75 MASK_EXT_IID_SUPPORT = 0x00000400,
76};
77
78enum {
79 REG_SQATTR = 0x0,
80 REG_SQLBA = 0x4,
81 REG_SQUBA = 0x8,
82 REG_SQDAO = 0xC,
83 REG_SQISAO = 0x10,
84
85 REG_CQATTR = 0x20,
86 REG_CQLBA = 0x24,
87 REG_CQUBA = 0x28,
88 REG_CQDAO = 0x2C,
89 REG_CQISAO = 0x30,
90};
91
92enum {
93 REG_SQHP = 0x0,
94 REG_SQTP = 0x4,
95 REG_SQRTC = 0x8,
96 REG_SQCTI = 0xC,
97 REG_SQRTS = 0x10,
98};
99
100enum {
101 REG_CQHP = 0x0,
102 REG_CQTP = 0x4,
103};
104
105enum {
106 REG_CQIS = 0x0,
107 REG_CQIE = 0x4,
108};
109
110enum {
111 SQ_START = 0x0,
112 SQ_STOP = 0x1,
113 SQ_ICU = 0x2,
114};
115
116enum {
117 SQ_STS = 0x1,
118 SQ_CUS = 0x2,
Neil Armstrongac4fa422024-12-30 11:30:58 +0100119};
120
Neil Armstrong23bc5e82024-12-30 11:31:00 +0100121#define SQ_ICU_ERR_CODE_MASK GENMASK(7, 4)
122#define UFS_MASK(mask, offset) ((mask) << (offset))
123
124/* UFS Version 08h */
125#define MINOR_VERSION_NUM_MASK UFS_MASK(0xFFFF, 0)
126#define MAJOR_VERSION_NUM_MASK UFS_MASK(0xFFFF, 16)
127
128/* Controller UFSHCI version */
129enum {
130 UFSHCI_VERSION_10 = 0x00010000, /* 1.0 */
131 UFSHCI_VERSION_11 = 0x00010100, /* 1.1 */
132 UFSHCI_VERSION_20 = 0x00000200, /* 2.0 */
133 UFSHCI_VERSION_21 = 0x00000210, /* 2.1 */
134 UFSHCI_VERSION_30 = 0x00000300, /* 3.0 */
135 UFSHCI_VERSION_31 = 0x00000310, /* 3.1 */
136 UFSHCI_VERSION_40 = 0x00000400, /* 4.0 */
137};
138
139/*
140 * IS - Interrupt Status - 20h
141 */
Neil Armstrongac4fa422024-12-30 11:30:58 +0100142#define UTP_TRANSFER_REQ_COMPL 0x1
143#define UIC_DME_END_PT_RESET 0x2
144#define UIC_ERROR 0x4
145#define UIC_TEST_MODE 0x8
146#define UIC_POWER_MODE 0x10
147#define UIC_HIBERNATE_EXIT 0x20
148#define UIC_HIBERNATE_ENTER 0x40
149#define UIC_LINK_LOST 0x80
150#define UIC_LINK_STARTUP 0x100
151#define UTP_TASK_REQ_COMPL 0x200
152#define UIC_COMMAND_COMPL 0x400
153#define DEVICE_FATAL_ERROR 0x800
154#define CONTROLLER_FATAL_ERROR 0x10000
155#define SYSTEM_BUS_FATAL_ERROR 0x20000
Neil Armstrong23bc5e82024-12-30 11:31:00 +0100156#define CRYPTO_ENGINE_FATAL_ERROR 0x40000
157#define MCQ_CQ_EVENT_STATUS 0x100000
158
159#define UFSHCD_UIC_HIBERN8_MASK (UIC_HIBERNATE_ENTER |\
160 UIC_HIBERNATE_EXIT)
Neil Armstrongac4fa422024-12-30 11:30:58 +0100161
Neil Armstrong23bc5e82024-12-30 11:31:00 +0100162#define UFSHCD_UIC_PWR_MASK (UFSHCD_UIC_HIBERN8_MASK |\
Neil Armstrongac4fa422024-12-30 11:30:58 +0100163 UIC_POWER_MODE)
164
Neil Armstrong23bc5e82024-12-30 11:31:00 +0100165#define UFSHCD_UIC_MASK (UIC_COMMAND_COMPL | UFSHCD_UIC_PWR_MASK)
Neil Armstrongac4fa422024-12-30 11:30:58 +0100166
Neil Armstrong23bc5e82024-12-30 11:31:00 +0100167#define UFSHCD_ERROR_MASK (UIC_ERROR | INT_FATAL_ERRORS)
Neil Armstrongac4fa422024-12-30 11:30:58 +0100168
169#define INT_FATAL_ERRORS (DEVICE_FATAL_ERROR |\
170 CONTROLLER_FATAL_ERROR |\
Neil Armstrong23bc5e82024-12-30 11:31:00 +0100171 SYSTEM_BUS_FATAL_ERROR |\
172 CRYPTO_ENGINE_FATAL_ERROR |\
173 UIC_LINK_LOST)
Neil Armstrongac4fa422024-12-30 11:30:58 +0100174
Neil Armstrongac4fa422024-12-30 11:30:58 +0100175/* HCS - Host Controller Status 30h */
176#define DEVICE_PRESENT 0x1
177#define UTP_TRANSFER_REQ_LIST_READY 0x2
178#define UTP_TASK_REQ_LIST_READY 0x4
179#define UIC_COMMAND_READY 0x8
180#define HOST_ERROR_INDICATOR 0x10
181#define DEVICE_ERROR_INDICATOR 0x20
182#define UIC_POWER_MODE_CHANGE_REQ_STATUS_MASK UFS_MASK(0x7, 8)
183
184#define UFSHCD_STATUS_READY (UTP_TRANSFER_REQ_LIST_READY |\
185 UTP_TASK_REQ_LIST_READY |\
186 UIC_COMMAND_READY)
187
188enum {
189 PWR_OK = 0x0,
190 PWR_LOCAL = 0x01,
191 PWR_REMOTE = 0x02,
192 PWR_BUSY = 0x03,
193 PWR_ERROR_CAP = 0x04,
194 PWR_FATAL_ERROR = 0x05,
195};
196
Neil Armstrong23bc5e82024-12-30 11:31:00 +0100197/* HCE - Host Controller Enable 34h */
198#define CONTROLLER_ENABLE 0x1
199#define CONTROLLER_DISABLE 0x0
200#define CRYPTO_GENERAL_ENABLE 0x2
201
202/* UECPA - Host UIC Error Code PHY Adapter Layer 38h */
203#define UIC_PHY_ADAPTER_LAYER_ERROR 0x80000000
204#define UIC_PHY_ADAPTER_LAYER_ERROR_CODE_MASK 0x1F
205#define UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK 0xF
206#define UIC_PHY_ADAPTER_LAYER_GENERIC_ERROR 0x10
207
208/* UECDL - Host UIC Error Code Data Link Layer 3Ch */
209#define UIC_DATA_LINK_LAYER_ERROR 0x80000000
210#define UIC_DATA_LINK_LAYER_ERROR_CODE_MASK 0xFFFF
211#define UIC_DATA_LINK_LAYER_ERROR_TCX_REP_TIMER_EXP 0x2
212#define UIC_DATA_LINK_LAYER_ERROR_AFCX_REQ_TIMER_EXP 0x4
213#define UIC_DATA_LINK_LAYER_ERROR_FCX_PRO_TIMER_EXP 0x8
214#define UIC_DATA_LINK_LAYER_ERROR_RX_BUF_OF 0x20
215#define UIC_DATA_LINK_LAYER_ERROR_PA_INIT 0x2000
216#define UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED 0x0001
217#define UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT 0x0002
218
219/* UECN - Host UIC Error Code Network Layer 40h */
220#define UIC_NETWORK_LAYER_ERROR 0x80000000
221#define UIC_NETWORK_LAYER_ERROR_CODE_MASK 0x7
222#define UIC_NETWORK_UNSUPPORTED_HEADER_TYPE 0x1
223#define UIC_NETWORK_BAD_DEVICEID_ENC 0x2
224#define UIC_NETWORK_LHDR_TRAP_PACKET_DROPPING 0x4
225
226/* UECT - Host UIC Error Code Transport Layer 44h */
227#define UIC_TRANSPORT_LAYER_ERROR 0x80000000
228#define UIC_TRANSPORT_LAYER_ERROR_CODE_MASK 0x7F
229#define UIC_TRANSPORT_UNSUPPORTED_HEADER_TYPE 0x1
230#define UIC_TRANSPORT_UNKNOWN_CPORTID 0x2
231#define UIC_TRANSPORT_NO_CONNECTION_RX 0x4
232#define UIC_TRANSPORT_CONTROLLED_SEGMENT_DROPPING 0x8
233#define UIC_TRANSPORT_BAD_TC 0x10
234#define UIC_TRANSPORT_E2E_CREDIT_OVERFOW 0x20
235#define UIC_TRANSPORT_SAFETY_VALUE_DROPPING 0x40
236
237/* UECDME - Host UIC Error Code DME 48h */
238#define UIC_DME_ERROR 0x80000000
239#define UIC_DME_ERROR_CODE_MASK 0x1
240
241/* UTRIACR - Interrupt Aggregation control register - 0x4Ch */
242#define INT_AGGR_TIMEOUT_VAL_MASK 0xFF
243#define INT_AGGR_COUNTER_THRESHOLD_MASK UFS_MASK(0x1F, 8)
244#define INT_AGGR_COUNTER_AND_TIMER_RESET 0x10000
245#define INT_AGGR_STATUS_BIT 0x100000
246#define INT_AGGR_PARAM_WRITE 0x1000000
247#define INT_AGGR_ENABLE 0x80000000
248
249/* UTRLRSR - UTP Transfer Request Run-Stop Register 60h */
250#define UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT 0x1
251
252/* UTMRLRSR - UTP Task Management Request Run-Stop Register 80h */
253#define UTP_TASK_REQ_LIST_RUN_STOP_BIT 0x1
254
255/* REG_UFS_MEM_CFG - Global Config Registers 300h */
256#define MCQ_MODE_SELECT BIT(0)
257
258/* CQISy - CQ y Interrupt Status Register */
259#define UFSHCD_MCQ_CQIS_TAIL_ENT_PUSH_STS 0x1
260
Neil Armstrongac4fa422024-12-30 11:30:58 +0100261/* UICCMD - UIC Command */
262#define COMMAND_OPCODE_MASK 0xFF
263#define GEN_SELECTOR_INDEX_MASK 0xFFFF
264
265#define MIB_ATTRIBUTE_MASK UFS_MASK(0xFFFF, 16)
266#define RESET_LEVEL 0xFF
267
268#define ATTR_SET_TYPE_MASK UFS_MASK(0xFF, 16)
269#define CFG_RESULT_CODE_MASK 0xFF
270#define GENERIC_ERROR_CODE_MASK 0xFF
271
272/* GenSelectorIndex calculation macros for M-PHY attributes */
273#define UIC_ARG_MPHY_TX_GEN_SEL_INDEX(lane) (lane)
274#define UIC_ARG_MPHY_RX_GEN_SEL_INDEX(lane) (PA_MAXDATALANES + (lane))
275
276#define UIC_ARG_MIB_SEL(attr, sel) ((((attr) & 0xFFFF) << 16) |\
277 ((sel) & 0xFFFF))
278#define UIC_ARG_MIB(attr) UIC_ARG_MIB_SEL(attr, 0)
279#define UIC_ARG_ATTR_TYPE(t) (((t) & 0xFF) << 16)
280#define UIC_GET_ATTR_ID(v) (((v) >> 16) & 0xFFFF)
281
282/* Link Status*/
283enum link_status {
284 UFSHCD_LINK_IS_DOWN = 1,
285 UFSHCD_LINK_IS_UP = 2,
286};
287
Neil Armstrongac4fa422024-12-30 11:30:58 +0100288/* UIC Commands */
289enum uic_cmd_dme {
290 UIC_CMD_DME_GET = 0x01,
291 UIC_CMD_DME_SET = 0x02,
292 UIC_CMD_DME_PEER_GET = 0x03,
293 UIC_CMD_DME_PEER_SET = 0x04,
294 UIC_CMD_DME_POWERON = 0x10,
295 UIC_CMD_DME_POWEROFF = 0x11,
296 UIC_CMD_DME_ENABLE = 0x12,
297 UIC_CMD_DME_RESET = 0x14,
298 UIC_CMD_DME_END_PT_RST = 0x15,
299 UIC_CMD_DME_LINK_STARTUP = 0x16,
300 UIC_CMD_DME_HIBER_ENTER = 0x17,
301 UIC_CMD_DME_HIBER_EXIT = 0x18,
302 UIC_CMD_DME_TEST_MODE = 0x1A,
303};
304
305/* UIC Config result code / Generic error code */
306enum {
307 UIC_CMD_RESULT_SUCCESS = 0x00,
308 UIC_CMD_RESULT_INVALID_ATTR = 0x01,
309 UIC_CMD_RESULT_FAILURE = 0x01,
310 UIC_CMD_RESULT_INVALID_ATTR_VALUE = 0x02,
311 UIC_CMD_RESULT_READ_ONLY_ATTR = 0x03,
312 UIC_CMD_RESULT_WRITE_ONLY_ATTR = 0x04,
313 UIC_CMD_RESULT_BAD_INDEX = 0x05,
314 UIC_CMD_RESULT_LOCKED_ATTR = 0x06,
315 UIC_CMD_RESULT_BAD_TEST_FEATURE_INDEX = 0x07,
316 UIC_CMD_RESULT_PEER_COMM_FAILURE = 0x08,
317 UIC_CMD_RESULT_BUSY = 0x09,
318 UIC_CMD_RESULT_DME_FAILURE = 0x0A,
319};
320
321#define MASK_UIC_COMMAND_RESULT 0xFF
322
Neil Armstrong23bc5e82024-12-30 11:31:00 +0100323#define INT_AGGR_COUNTER_THLD_VAL(c) (((c) & 0x1F) << 8)
324#define INT_AGGR_TIMEOUT_VAL(t) (((t) & 0xFF) << 0)
Neil Armstrongac4fa422024-12-30 11:30:58 +0100325
Neil Armstrong23bc5e82024-12-30 11:31:00 +0100326/*
327 * Request Descriptor Definitions
328 */
329
330/* To accommodate UFS2.0 required Command type */
331enum {
332 UTP_CMD_TYPE_UFS_STORAGE = 0x1,
333};
334
335enum {
336 UTP_SCSI_COMMAND = 0x00000000,
337 UTP_REQ_DESC_INT_CMD = 0x01000000,
338 UTP_NATIVE_UFS_COMMAND = 0x10000000,
339 UTP_DEVICE_MANAGEMENT_FUNCTION = 0x20000000,
340};
341
342/* UTP Transfer Request Data Direction (DD) */
343enum utp_data_direction {
344 UTP_NO_DATA_TRANSFER = 0,
345 UTP_HOST_TO_DEVICE = 1,
346 UTP_DEVICE_TO_HOST = 2,
347};
348
349/* Overall command status values */
350enum utp_ocs {
351 OCS_SUCCESS = 0x0,
352 OCS_INVALID_CMD_TABLE_ATTR = 0x1,
353 OCS_INVALID_PRDT_ATTR = 0x2,
354 OCS_MISMATCH_DATA_BUF_SIZE = 0x3,
355 OCS_MISMATCH_RESP_UPIU_SIZE = 0x4,
356 OCS_PEER_COMM_FAILURE = 0x5,
357 OCS_ABORTED = 0x6,
358 OCS_FATAL_ERROR = 0x7,
359 OCS_DEVICE_FATAL_ERROR = 0x8,
360 OCS_INVALID_CRYPTO_CONFIG = 0x9,
361 OCS_GENERAL_CRYPTO_ERROR = 0xA,
362 OCS_INVALID_COMMAND_STATUS = 0x0F,
363};
364
365enum {
366 MASK_OCS = 0x0F,
367};
368
369/* The maximum length of the data byte count field in the PRDT is 256KB */
370#define PRDT_DATA_BYTE_COUNT_MAX SZ_256K
371/* The granularity of the data byte count field in the PRDT is 32-bit */
372#define PRDT_DATA_BYTE_COUNT_PAD 4
373
Neil Armstrongac4fa422024-12-30 11:30:58 +0100374
375struct ufshcd_sg_entry {
376 __le32 base_addr;
377 __le32 upper_addr;
378 __le32 reserved;
379 __le32 size;
380};
381
382#define MAX_BUFF 128
383/**
384 * struct utp_transfer_cmd_desc - UFS Command Descriptor structure
385 * @command_upiu: Command UPIU Frame address
386 * @response_upiu: Response UPIU Frame address
387 * @prd_table: Physical Region Descriptor
388 */
389struct utp_transfer_cmd_desc {
390 u8 command_upiu[ALIGNED_UPIU_SIZE];
391 u8 response_upiu[ALIGNED_UPIU_SIZE];
392 struct ufshcd_sg_entry prd_table[MAX_BUFF];
393};
394
395/**
396 * struct request_desc_header - Descriptor Header common to both UTRD and UTMRD
397 * @dword0: Descriptor Header DW0
398 * @dword1: Descriptor Header DW1
399 * @dword2: Descriptor Header DW2
400 * @dword3: Descriptor Header DW3
401 */
402struct request_desc_header {
403 __le32 dword_0;
404 __le32 dword_1;
405 __le32 dword_2;
406 __le32 dword_3;
407};
408
409/**
410 * struct utp_transfer_req_desc - UTRD structure
411 * @header: UTRD header DW-0 to DW-3
412 * @command_desc_base_addr_lo: UCD base address low DW-4
413 * @command_desc_base_addr_hi: UCD base address high DW-5
414 * @response_upiu_length: response UPIU length DW-6
415 * @response_upiu_offset: response UPIU offset DW-6
416 * @prd_table_length: Physical region descriptor length DW-7
417 * @prd_table_offset: Physical region descriptor offset DW-7
418 */
419struct utp_transfer_req_desc {
420 /* DW 0-3 */
421 struct request_desc_header header;
422
423 /* DW 4-5*/
424 __le32 command_desc_base_addr_lo;
425 __le32 command_desc_base_addr_hi;
426
427 /* DW 6 */
428 __le16 response_upiu_length;
429 __le16 response_upiu_offset;
430
431 /* DW 7 */
432 __le16 prd_table_length;
433 __le16 prd_table_offset;
434};
435
436/**
437 * struct utp_upiu_header - UPIU header structure
438 * @dword_0: UPIU header DW-0
439 * @dword_1: UPIU header DW-1
440 * @dword_2: UPIU header DW-2
441 */
442struct utp_upiu_header {
443 __be32 dword_0;
444 __be32 dword_1;
445 __be32 dword_2;
446};
447
448/*
449 * UTMRD structure.
450 */
451struct utp_task_req_desc {
452 /* DW 0-3 */
453 struct request_desc_header header;
454
455 /* DW 4-11 - Task request UPIU structure */
456 struct utp_upiu_header req_header;
457 __be32 input_param1;
458 __be32 input_param2;
459 __be32 input_param3;
460 __be32 __reserved1[2];
461
462 /* DW 12-19 - Task Management Response UPIU structure */
463 struct utp_upiu_header rsp_header;
464 __be32 output_param1;
465 __be32 output_param2;
466 __be32 __reserved2[3];
467};
468
469#endif