blob: a3b2c75de416303c124afdf9b502c06c79e39b03 [file] [log] [blame]
Faiz Abbas7feafb02019-10-15 18:24:36 +05301/* SPDX-License-Identifier: GPL-2.0+ */
2#ifndef __UFS_H
3#define __UFS_H
4
Bhupesh Sharmaf5aa3d72023-08-11 11:50:16 +05305#include <asm/io.h>
Faiz Abbas7feafb02019-10-15 18:24:36 +05306#include "unipro.h"
7
Simon Glass98eb4ce2020-07-19 10:15:54 -06008struct udevice;
9
Faiz Abbas7feafb02019-10-15 18:24:36 +053010#define UFS_CDB_SIZE 16
11#define UPIU_TRANSACTION_UIC_CMD 0x1F
12#define UIC_CMD_SIZE (sizeof(u32) * 4)
13#define RESPONSE_UPIU_SENSE_DATA_LENGTH 18
14#define UFS_MAX_LUNS 0x7F
15
16enum {
17 TASK_REQ_UPIU_SIZE_DWORDS = 8,
18 TASK_RSP_UPIU_SIZE_DWORDS = 8,
19 ALIGNED_UPIU_SIZE = 512,
20};
21
22/* UFS device power modes */
23enum ufs_dev_pwr_mode {
24 UFS_ACTIVE_PWR_MODE = 1,
25 UFS_SLEEP_PWR_MODE = 2,
26 UFS_POWERDOWN_PWR_MODE = 3,
27};
28
29enum ufs_notify_change_status {
30 PRE_CHANGE,
31 POST_CHANGE,
32};
33
34struct ufs_pa_layer_attr {
35 u32 gear_rx;
36 u32 gear_tx;
37 u32 lane_rx;
38 u32 lane_tx;
39 u32 pwr_rx;
40 u32 pwr_tx;
41 u32 hs_rate;
42};
43
44struct ufs_pwr_mode_info {
45 bool is_valid;
46 struct ufs_pa_layer_attr info;
47};
48
49enum ufs_desc_def_size {
50 QUERY_DESC_DEVICE_DEF_SIZE = 0x40,
51 QUERY_DESC_CONFIGURATION_DEF_SIZE = 0x90,
52 QUERY_DESC_UNIT_DEF_SIZE = 0x23,
53 QUERY_DESC_INTERCONNECT_DEF_SIZE = 0x06,
54 QUERY_DESC_GEOMETRY_DEF_SIZE = 0x48,
55 QUERY_DESC_POWER_DEF_SIZE = 0x62,
56 QUERY_DESC_HEALTH_DEF_SIZE = 0x25,
57};
58
59struct ufs_desc_size {
60 int dev_desc;
61 int pwr_desc;
62 int geom_desc;
63 int interc_desc;
64 int unit_desc;
65 int conf_desc;
66 int hlth_desc;
67};
68
69/*
70 * Request Descriptor Definitions
71 */
72
73/* Transfer request command type */
74enum {
75 UTP_CMD_TYPE_SCSI = 0x0,
76 UTP_CMD_TYPE_UFS = 0x1,
77 UTP_CMD_TYPE_DEV_MANAGE = 0x2,
78};
79
80/* UTP Transfer Request Command Offset */
81#define UPIU_COMMAND_TYPE_OFFSET 28
82
83/* Offset of the response code in the UPIU header */
84#define UPIU_RSP_CODE_OFFSET 8
85
86/* To accommodate UFS2.0 required Command type */
87enum {
88 UTP_CMD_TYPE_UFS_STORAGE = 0x1,
89};
90
91enum {
92 UTP_SCSI_COMMAND = 0x00000000,
93 UTP_NATIVE_UFS_COMMAND = 0x10000000,
94 UTP_DEVICE_MANAGEMENT_FUNCTION = 0x20000000,
95 UTP_REQ_DESC_INT_CMD = 0x01000000,
96};
97
98/* UTP Transfer Request Data Direction (DD) */
99enum {
100 UTP_NO_DATA_TRANSFER = 0x00000000,
101 UTP_HOST_TO_DEVICE = 0x02000000,
102 UTP_DEVICE_TO_HOST = 0x04000000,
103};
104
105/* Overall command status values */
106enum {
107 OCS_SUCCESS = 0x0,
108 OCS_INVALID_CMD_TABLE_ATTR = 0x1,
109 OCS_INVALID_PRDT_ATTR = 0x2,
110 OCS_MISMATCH_DATA_BUF_SIZE = 0x3,
111 OCS_MISMATCH_RESP_UPIU_SIZE = 0x4,
112 OCS_PEER_COMM_FAILURE = 0x5,
113 OCS_ABORTED = 0x6,
114 OCS_FATAL_ERROR = 0x7,
115 OCS_INVALID_COMMAND_STATUS = 0x0F,
116 MASK_OCS = 0x0F,
117};
118
119/* The maximum length of the data byte count field in the PRDT is 256KB */
120#define PRDT_DATA_BYTE_COUNT_MAX (256 * 1024)
121/* The granularity of the data byte count field in the PRDT is 32-bit */
122#define PRDT_DATA_BYTE_COUNT_PAD 4
123
124#define GENERAL_UPIU_REQUEST_SIZE (sizeof(struct utp_upiu_req))
125#define QUERY_DESC_MAX_SIZE 255
126#define QUERY_DESC_MIN_SIZE 2
127#define QUERY_DESC_HDR_SIZE 2
128#define QUERY_OSF_SIZE (GENERAL_UPIU_REQUEST_SIZE - \
129 (sizeof(struct utp_upiu_header)))
130#define RESPONSE_UPIU_SENSE_DATA_LENGTH 18
131#define UPIU_HEADER_DWORD(byte3, byte2, byte1, byte0)\
132 cpu_to_be32((byte3 << 24) | (byte2 << 16) |\
133 (byte1 << 8) | (byte0))
134/*
135 * UFS Protocol Information Unit related definitions
136 */
137
138/* Task management functions */
139enum {
140 UFS_ABORT_TASK = 0x01,
141 UFS_ABORT_TASK_SET = 0x02,
142 UFS_CLEAR_TASK_SET = 0x04,
143 UFS_LOGICAL_RESET = 0x08,
144 UFS_QUERY_TASK = 0x80,
145 UFS_QUERY_TASK_SET = 0x81,
146};
147
148/* UTP UPIU Transaction Codes Initiator to Target */
149enum {
150 UPIU_TRANSACTION_NOP_OUT = 0x00,
151 UPIU_TRANSACTION_COMMAND = 0x01,
152 UPIU_TRANSACTION_DATA_OUT = 0x02,
153 UPIU_TRANSACTION_TASK_REQ = 0x04,
154 UPIU_TRANSACTION_QUERY_REQ = 0x16,
155};
156
157/* UTP UPIU Transaction Codes Target to Initiator */
158enum {
159 UPIU_TRANSACTION_NOP_IN = 0x20,
160 UPIU_TRANSACTION_RESPONSE = 0x21,
161 UPIU_TRANSACTION_DATA_IN = 0x22,
162 UPIU_TRANSACTION_TASK_RSP = 0x24,
163 UPIU_TRANSACTION_READY_XFER = 0x31,
164 UPIU_TRANSACTION_QUERY_RSP = 0x36,
165 UPIU_TRANSACTION_REJECT_UPIU = 0x3F,
166};
167
168/* UPIU Read/Write flags */
169enum {
170 UPIU_CMD_FLAGS_NONE = 0x00,
171 UPIU_CMD_FLAGS_WRITE = 0x20,
172 UPIU_CMD_FLAGS_READ = 0x40,
173};
174
175/* UPIU Task Attributes */
176enum {
177 UPIU_TASK_ATTR_SIMPLE = 0x00,
178 UPIU_TASK_ATTR_ORDERED = 0x01,
179 UPIU_TASK_ATTR_HEADQ = 0x02,
180 UPIU_TASK_ATTR_ACA = 0x03,
181};
182
183/* UPIU Query request function */
184enum {
185 UPIU_QUERY_FUNC_STANDARD_READ_REQUEST = 0x01,
186 UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST = 0x81,
187};
188
189/* Offset of the response code in the UPIU header */
190#define UPIU_RSP_CODE_OFFSET 8
191
192enum {
193 MASK_SCSI_STATUS = 0xFF,
194 MASK_TASK_RESPONSE = 0xFF00,
195 MASK_RSP_UPIU_RESULT = 0xFFFF,
196 MASK_QUERY_DATA_SEG_LEN = 0xFFFF,
197 MASK_RSP_UPIU_DATA_SEG_LEN = 0xFFFF,
198 MASK_RSP_EXCEPTION_EVENT = 0x10000,
199 MASK_TM_SERVICE_RESP = 0xFF,
200 MASK_TM_FUNC = 0xFF,
201};
202
203/* UTP QUERY Transaction Specific Fields OpCode */
204enum query_opcode {
205 UPIU_QUERY_OPCODE_NOP = 0x0,
206 UPIU_QUERY_OPCODE_READ_DESC = 0x1,
207 UPIU_QUERY_OPCODE_WRITE_DESC = 0x2,
208 UPIU_QUERY_OPCODE_READ_ATTR = 0x3,
209 UPIU_QUERY_OPCODE_WRITE_ATTR = 0x4,
210 UPIU_QUERY_OPCODE_READ_FLAG = 0x5,
211 UPIU_QUERY_OPCODE_SET_FLAG = 0x6,
212 UPIU_QUERY_OPCODE_CLEAR_FLAG = 0x7,
213 UPIU_QUERY_OPCODE_TOGGLE_FLAG = 0x8,
214};
215
216/* Query response result code */
217enum {
218 QUERY_RESULT_SUCCESS = 0x00,
219 QUERY_RESULT_NOT_READABLE = 0xF6,
220 QUERY_RESULT_NOT_WRITEABLE = 0xF7,
221 QUERY_RESULT_ALREADY_WRITTEN = 0xF8,
222 QUERY_RESULT_INVALID_LENGTH = 0xF9,
223 QUERY_RESULT_INVALID_VALUE = 0xFA,
224 QUERY_RESULT_INVALID_SELECTOR = 0xFB,
225 QUERY_RESULT_INVALID_INDEX = 0xFC,
226 QUERY_RESULT_INVALID_IDN = 0xFD,
227 QUERY_RESULT_INVALID_OPCODE = 0xFE,
228 QUERY_RESULT_GENERAL_FAILURE = 0xFF,
229};
230
231enum {
232 UPIU_COMMAND_SET_TYPE_SCSI = 0x0,
233 UPIU_COMMAND_SET_TYPE_UFS = 0x1,
234 UPIU_COMMAND_SET_TYPE_QUERY = 0x2,
235};
236
237/* Flag idn for Query Requests*/
238enum flag_idn {
239 QUERY_FLAG_IDN_FDEVICEINIT = 0x01,
240 QUERY_FLAG_IDN_PERMANENT_WPE = 0x02,
241 QUERY_FLAG_IDN_PWR_ON_WPE = 0x03,
242 QUERY_FLAG_IDN_BKOPS_EN = 0x04,
243 QUERY_FLAG_IDN_LIFE_SPAN_MODE_ENABLE = 0x05,
244 QUERY_FLAG_IDN_PURGE_ENABLE = 0x06,
245 QUERY_FLAG_IDN_RESERVED2 = 0x07,
246 QUERY_FLAG_IDN_FPHYRESOURCEREMOVAL = 0x08,
247 QUERY_FLAG_IDN_BUSY_RTC = 0x09,
248 QUERY_FLAG_IDN_RESERVED3 = 0x0A,
249 QUERY_FLAG_IDN_PERMANENTLY_DISABLE_FW_UPDATE = 0x0B,
250};
251
252/* Attribute idn for Query requests */
253enum attr_idn {
254 QUERY_ATTR_IDN_BOOT_LU_EN = 0x00,
255 QUERY_ATTR_IDN_RESERVED = 0x01,
256 QUERY_ATTR_IDN_POWER_MODE = 0x02,
257 QUERY_ATTR_IDN_ACTIVE_ICC_LVL = 0x03,
258 QUERY_ATTR_IDN_OOO_DATA_EN = 0x04,
259 QUERY_ATTR_IDN_BKOPS_STATUS = 0x05,
260 QUERY_ATTR_IDN_PURGE_STATUS = 0x06,
261 QUERY_ATTR_IDN_MAX_DATA_IN = 0x07,
262 QUERY_ATTR_IDN_MAX_DATA_OUT = 0x08,
263 QUERY_ATTR_IDN_DYN_CAP_NEEDED = 0x09,
264 QUERY_ATTR_IDN_REF_CLK_FREQ = 0x0A,
265 QUERY_ATTR_IDN_CONF_DESC_LOCK = 0x0B,
266 QUERY_ATTR_IDN_MAX_NUM_OF_RTT = 0x0C,
267 QUERY_ATTR_IDN_EE_CONTROL = 0x0D,
268 QUERY_ATTR_IDN_EE_STATUS = 0x0E,
269 QUERY_ATTR_IDN_SECONDS_PASSED = 0x0F,
270 QUERY_ATTR_IDN_CNTX_CONF = 0x10,
271 QUERY_ATTR_IDN_CORR_PRG_BLK_NUM = 0x11,
272 QUERY_ATTR_IDN_RESERVED2 = 0x12,
273 QUERY_ATTR_IDN_RESERVED3 = 0x13,
274 QUERY_ATTR_IDN_FFU_STATUS = 0x14,
275 QUERY_ATTR_IDN_PSA_STATE = 0x15,
276 QUERY_ATTR_IDN_PSA_DATA_SIZE = 0x16,
277};
278
279/* Descriptor idn for Query requests */
280enum desc_idn {
281 QUERY_DESC_IDN_DEVICE = 0x0,
282 QUERY_DESC_IDN_CONFIGURATION = 0x1,
283 QUERY_DESC_IDN_UNIT = 0x2,
284 QUERY_DESC_IDN_RFU_0 = 0x3,
285 QUERY_DESC_IDN_INTERCONNECT = 0x4,
286 QUERY_DESC_IDN_STRING = 0x5,
287 QUERY_DESC_IDN_RFU_1 = 0x6,
288 QUERY_DESC_IDN_GEOMETRY = 0x7,
289 QUERY_DESC_IDN_POWER = 0x8,
290 QUERY_DESC_IDN_HEALTH = 0x9,
291 QUERY_DESC_IDN_MAX,
292};
293
294enum desc_header_offset {
295 QUERY_DESC_LENGTH_OFFSET = 0x00,
296 QUERY_DESC_DESC_TYPE_OFFSET = 0x01,
297};
298
299struct ufshcd_sg_entry {
300 __le32 base_addr;
301 __le32 upper_addr;
302 __le32 reserved;
303 __le32 size;
304};
305
306#define MAX_BUFF 128
307/**
308 * struct utp_transfer_cmd_desc - UFS Command Descriptor structure
309 * @command_upiu: Command UPIU Frame address
310 * @response_upiu: Response UPIU Frame address
311 * @prd_table: Physical Region Descriptor
312 */
313struct utp_transfer_cmd_desc {
314 u8 command_upiu[ALIGNED_UPIU_SIZE];
315 u8 response_upiu[ALIGNED_UPIU_SIZE];
316 struct ufshcd_sg_entry prd_table[MAX_BUFF];
317};
318
319/**
320 * struct request_desc_header - Descriptor Header common to both UTRD and UTMRD
321 * @dword0: Descriptor Header DW0
322 * @dword1: Descriptor Header DW1
323 * @dword2: Descriptor Header DW2
324 * @dword3: Descriptor Header DW3
325 */
326struct request_desc_header {
327 __le32 dword_0;
328 __le32 dword_1;
329 __le32 dword_2;
330 __le32 dword_3;
331};
332
333/**
334 * struct utp_transfer_req_desc - UTRD structure
335 * @header: UTRD header DW-0 to DW-3
336 * @command_desc_base_addr_lo: UCD base address low DW-4
337 * @command_desc_base_addr_hi: UCD base address high DW-5
338 * @response_upiu_length: response UPIU length DW-6
339 * @response_upiu_offset: response UPIU offset DW-6
340 * @prd_table_length: Physical region descriptor length DW-7
341 * @prd_table_offset: Physical region descriptor offset DW-7
342 */
343struct utp_transfer_req_desc {
344 /* DW 0-3 */
345 struct request_desc_header header;
346
347 /* DW 4-5*/
348 __le32 command_desc_base_addr_lo;
349 __le32 command_desc_base_addr_hi;
350
351 /* DW 6 */
352 __le16 response_upiu_length;
353 __le16 response_upiu_offset;
354
355 /* DW 7 */
356 __le16 prd_table_length;
357 __le16 prd_table_offset;
358};
359
360/**
361 * struct utp_upiu_header - UPIU header structure
362 * @dword_0: UPIU header DW-0
363 * @dword_1: UPIU header DW-1
364 * @dword_2: UPIU header DW-2
365 */
366struct utp_upiu_header {
367 __be32 dword_0;
368 __be32 dword_1;
369 __be32 dword_2;
370};
371
372/**
373 * struct utp_upiu_query - upiu request buffer structure for
374 * query request.
375 * @opcode: command to perform B-0
376 * @idn: a value that indicates the particular type of data B-1
377 * @index: Index to further identify data B-2
378 * @selector: Index to further identify data B-3
379 * @reserved_osf: spec reserved field B-4,5
380 * @length: number of descriptor bytes to read/write B-6,7
381 * @value: Attribute value to be written DW-5
382 * @reserved: spec reserved DW-6,7
383 */
384struct utp_upiu_query {
385 __u8 opcode;
386 __u8 idn;
387 __u8 index;
388 __u8 selector;
389 __be16 reserved_osf;
390 __be16 length;
391 __be32 value;
392 __be32 reserved[2];
393};
394
395/**
396 * struct utp_upiu_cmd - Command UPIU structure
397 * @data_transfer_len: Data Transfer Length DW-3
398 * @cdb: Command Descriptor Block CDB DW-4 to DW-7
399 */
400struct utp_upiu_cmd {
401 __be32 exp_data_transfer_len;
402 u8 cdb[UFS_CDB_SIZE];
403};
404
405/*
406 * UTMRD structure.
407 */
408struct utp_task_req_desc {
409 /* DW 0-3 */
410 struct request_desc_header header;
411
412 /* DW 4-11 - Task request UPIU structure */
413 struct utp_upiu_header req_header;
414 __be32 input_param1;
415 __be32 input_param2;
416 __be32 input_param3;
417 __be32 __reserved1[2];
418
419 /* DW 12-19 - Task Management Response UPIU structure */
420 struct utp_upiu_header rsp_header;
421 __be32 output_param1;
422 __be32 output_param2;
423 __be32 __reserved2[3];
424};
425
426/**
427 * struct utp_upiu_req - general upiu request structure
428 * @header:UPIU header structure DW-0 to DW-2
429 * @sc: fields structure for scsi command DW-3 to DW-7
430 * @qr: fields structure for query request DW-3 to DW-7
431 */
432struct utp_upiu_req {
433 struct utp_upiu_header header;
434 union {
435 struct utp_upiu_cmd sc;
436 struct utp_upiu_query qr;
437 struct utp_upiu_query tr;
438 /* use utp_upiu_query to host the 4 dwords of uic command */
439 struct utp_upiu_query uc;
440 };
441};
442
443/**
444 * struct utp_cmd_rsp - Response UPIU structure
445 * @residual_transfer_count: Residual transfer count DW-3
446 * @reserved: Reserved double words DW-4 to DW-7
447 * @sense_data_len: Sense data length DW-8 U16
448 * @sense_data: Sense data field DW-8 to DW-12
449 */
450struct utp_cmd_rsp {
451 __be32 residual_transfer_count;
452 __be32 reserved[4];
453 __be16 sense_data_len;
454 u8 sense_data[RESPONSE_UPIU_SENSE_DATA_LENGTH];
455};
456
457/**
458 * struct utp_upiu_rsp - general upiu response structure
459 * @header: UPIU header structure DW-0 to DW-2
460 * @sr: fields structure for scsi command DW-3 to DW-12
461 * @qr: fields structure for query request DW-3 to DW-7
462 */
463struct utp_upiu_rsp {
464 struct utp_upiu_header header;
465 union {
466 struct utp_cmd_rsp sr;
467 struct utp_upiu_query qr;
468 };
469};
470
471#define MAX_MODEL_LEN 16
472/**
473 * ufs_dev_desc - ufs device details from the device descriptor
474 *
475 * @wmanufacturerid: card details
476 * @model: card model
477 */
478struct ufs_dev_desc {
479 u16 wmanufacturerid;
480 char model[MAX_MODEL_LEN + 1];
481};
482
483/* Device descriptor parameters offsets in bytes*/
484enum device_desc_param {
485 DEVICE_DESC_PARAM_LEN = 0x0,
486 DEVICE_DESC_PARAM_TYPE = 0x1,
487 DEVICE_DESC_PARAM_DEVICE_TYPE = 0x2,
488 DEVICE_DESC_PARAM_DEVICE_CLASS = 0x3,
489 DEVICE_DESC_PARAM_DEVICE_SUB_CLASS = 0x4,
490 DEVICE_DESC_PARAM_PRTCL = 0x5,
491 DEVICE_DESC_PARAM_NUM_LU = 0x6,
492 DEVICE_DESC_PARAM_NUM_WLU = 0x7,
493 DEVICE_DESC_PARAM_BOOT_ENBL = 0x8,
494 DEVICE_DESC_PARAM_DESC_ACCSS_ENBL = 0x9,
495 DEVICE_DESC_PARAM_INIT_PWR_MODE = 0xA,
496 DEVICE_DESC_PARAM_HIGH_PR_LUN = 0xB,
497 DEVICE_DESC_PARAM_SEC_RMV_TYPE = 0xC,
498 DEVICE_DESC_PARAM_SEC_LU = 0xD,
499 DEVICE_DESC_PARAM_BKOP_TERM_LT = 0xE,
500 DEVICE_DESC_PARAM_ACTVE_ICC_LVL = 0xF,
501 DEVICE_DESC_PARAM_SPEC_VER = 0x10,
502 DEVICE_DESC_PARAM_MANF_DATE = 0x12,
503 DEVICE_DESC_PARAM_MANF_NAME = 0x14,
504 DEVICE_DESC_PARAM_PRDCT_NAME = 0x15,
505 DEVICE_DESC_PARAM_SN = 0x16,
506 DEVICE_DESC_PARAM_OEM_ID = 0x17,
507 DEVICE_DESC_PARAM_MANF_ID = 0x18,
508 DEVICE_DESC_PARAM_UD_OFFSET = 0x1A,
509 DEVICE_DESC_PARAM_UD_LEN = 0x1B,
510 DEVICE_DESC_PARAM_RTT_CAP = 0x1C,
511 DEVICE_DESC_PARAM_FRQ_RTC = 0x1D,
512 DEVICE_DESC_PARAM_UFS_FEAT = 0x1F,
513 DEVICE_DESC_PARAM_FFU_TMT = 0x20,
514 DEVICE_DESC_PARAM_Q_DPTH = 0x21,
515 DEVICE_DESC_PARAM_DEV_VER = 0x22,
516 DEVICE_DESC_PARAM_NUM_SEC_WPA = 0x24,
517 DEVICE_DESC_PARAM_PSA_MAX_DATA = 0x25,
518 DEVICE_DESC_PARAM_PSA_TMT = 0x29,
519 DEVICE_DESC_PARAM_PRDCT_REV = 0x2A,
520};
521
522struct ufs_hba;
523
524enum {
525 UFSHCD_MAX_CHANNEL = 0,
526 UFSHCD_MAX_ID = 1,
527};
528
529enum dev_cmd_type {
530 DEV_CMD_TYPE_NOP = 0x0,
531 DEV_CMD_TYPE_QUERY = 0x1,
532};
533
534/**
535 * struct uic_command - UIC command structure
536 * @command: UIC command
537 * @argument1: UIC command argument 1
538 * @argument2: UIC command argument 2
539 * @argument3: UIC command argument 3
540 * @cmd_active: Indicate if UIC command is outstanding
541 * @result: UIC command result
542 * @done: UIC command completion
543 */
544struct uic_command {
545 u32 command;
546 u32 argument1;
547 u32 argument2;
548 u32 argument3;
549 int cmd_active;
550 int result;
551};
552
553/* GenSelectorIndex calculation macros for M-PHY attributes */
554#define UIC_ARG_MPHY_TX_GEN_SEL_INDEX(lane) (lane)
555#define UIC_ARG_MPHY_RX_GEN_SEL_INDEX(lane) (PA_MAXDATALANES + (lane))
556
557#define UIC_ARG_MIB_SEL(attr, sel) ((((attr) & 0xFFFF) << 16) |\
558 ((sel) & 0xFFFF))
559#define UIC_ARG_MIB(attr) UIC_ARG_MIB_SEL(attr, 0)
560#define UIC_ARG_ATTR_TYPE(t) (((t) & 0xFF) << 16)
561#define UIC_GET_ATTR_ID(v) (((v) >> 16) & 0xFFFF)
562
563/* Link Status*/
564enum link_status {
565 UFSHCD_LINK_IS_DOWN = 1,
566 UFSHCD_LINK_IS_UP = 2,
567};
568
569#define UIC_ARG_MIB_SEL(attr, sel) ((((attr) & 0xFFFF) << 16) |\
570 ((sel) & 0xFFFF))
571#define UIC_ARG_MIB(attr) UIC_ARG_MIB_SEL(attr, 0)
572#define UIC_ARG_ATTR_TYPE(t) (((t) & 0xFF) << 16)
573#define UIC_GET_ATTR_ID(v) (((v) >> 16) & 0xFFFF)
574
575/* UIC Commands */
576enum uic_cmd_dme {
577 UIC_CMD_DME_GET = 0x01,
578 UIC_CMD_DME_SET = 0x02,
579 UIC_CMD_DME_PEER_GET = 0x03,
580 UIC_CMD_DME_PEER_SET = 0x04,
581 UIC_CMD_DME_POWERON = 0x10,
582 UIC_CMD_DME_POWEROFF = 0x11,
583 UIC_CMD_DME_ENABLE = 0x12,
584 UIC_CMD_DME_RESET = 0x14,
585 UIC_CMD_DME_END_PT_RST = 0x15,
586 UIC_CMD_DME_LINK_STARTUP = 0x16,
587 UIC_CMD_DME_HIBER_ENTER = 0x17,
588 UIC_CMD_DME_HIBER_EXIT = 0x18,
589 UIC_CMD_DME_TEST_MODE = 0x1A,
590};
591
592/* UIC Config result code / Generic error code */
593enum {
594 UIC_CMD_RESULT_SUCCESS = 0x00,
595 UIC_CMD_RESULT_INVALID_ATTR = 0x01,
596 UIC_CMD_RESULT_FAILURE = 0x01,
597 UIC_CMD_RESULT_INVALID_ATTR_VALUE = 0x02,
598 UIC_CMD_RESULT_READ_ONLY_ATTR = 0x03,
599 UIC_CMD_RESULT_WRITE_ONLY_ATTR = 0x04,
600 UIC_CMD_RESULT_BAD_INDEX = 0x05,
601 UIC_CMD_RESULT_LOCKED_ATTR = 0x06,
602 UIC_CMD_RESULT_BAD_TEST_FEATURE_INDEX = 0x07,
603 UIC_CMD_RESULT_PEER_COMM_FAILURE = 0x08,
604 UIC_CMD_RESULT_BUSY = 0x09,
605 UIC_CMD_RESULT_DME_FAILURE = 0x0A,
606};
607
608#define MASK_UIC_COMMAND_RESULT 0xFF
609
610/* Host <-> Device UniPro Link state */
611enum uic_link_state {
612 UIC_LINK_OFF_STATE = 0, /* Link powered down or disabled */
613 UIC_LINK_ACTIVE_STATE = 1, /* Link is in Fast/Slow/Sleep state */
614 UIC_LINK_HIBERN8_STATE = 2, /* Link is in Hibernate state */
615};
616
617/* UIC command interfaces for DME primitives */
618#define DME_LOCAL 0
619#define DME_PEER 1
620#define ATTR_SET_NOR 0 /* NORMAL */
621#define ATTR_SET_ST 1 /* STATIC */
622
623int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
624 u8 attr_set, u32 mib_val, u8 peer);
625int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
626 u32 *mib_val, u8 peer);
627
628static inline int ufshcd_dme_set(struct ufs_hba *hba, u32 attr_sel,
629 u32 mib_val)
630{
631 return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,
632 mib_val, DME_LOCAL);
633}
634
635static inline int ufshcd_dme_get(struct ufs_hba *hba,
636 u32 attr_sel, u32 *mib_val)
637{
638 return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_LOCAL);
639}
640
641static inline int ufshcd_dme_peer_get(struct ufs_hba *hba,
642 u32 attr_sel, u32 *mib_val)
643{
644 return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_PEER);
645}
646
647static inline int ufshcd_dme_peer_set(struct ufs_hba *hba, u32 attr_sel,
648 u32 mib_val)
649{
650 return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,
651 mib_val, DME_PEER);
652}
653
654/**
655 * struct ufs_query_req - parameters for building a query request
656 * @query_func: UPIU header query function
657 * @upiu_req: the query request data
658 */
659struct ufs_query_req {
660 u8 query_func;
661 struct utp_upiu_query upiu_req;
662};
663
664/**
665 * struct ufs_query_resp - UPIU QUERY
666 * @response: device response code
667 * @upiu_res: query response data
668 */
669struct ufs_query_res {
670 u8 response;
671 struct utp_upiu_query upiu_res;
672};
673
674/**
675 * struct ufs_query - holds relevant data structures for query request
676 * @request: request upiu and function
677 * @descriptor: buffer for sending/receiving descriptor
678 * @response: response upiu and response
679 */
680struct ufs_query {
681 struct ufs_query_req request;
682 u8 *descriptor;
683 struct ufs_query_res response;
684};
685
686/**
687 * struct ufs_dev_cmd - all assosiated fields with device management commands
688 * @type: device management command type - Query, NOP OUT
689 * @tag_wq: wait queue until free command slot is available
690 */
691struct ufs_dev_cmd {
692 enum dev_cmd_type type;
693 struct ufs_query query;
694};
695
696struct ufs_hba_ops {
697 int (*init)(struct ufs_hba *hba);
Bhupesh Sharma1d099902023-08-11 12:02:29 +0530698 void (*dbg_register_dump)(struct ufs_hba *hba);
Faiz Abbas7feafb02019-10-15 18:24:36 +0530699 int (*hce_enable_notify)(struct ufs_hba *hba,
700 enum ufs_notify_change_status);
701 int (*link_startup_notify)(struct ufs_hba *hba,
702 enum ufs_notify_change_status);
703 int (*phy_initialization)(struct ufs_hba *hba);
704};
705
706struct ufs_hba {
707 struct udevice *dev;
708 void __iomem *mmio_base;
709 struct ufs_hba_ops *ops;
710 struct ufs_desc_size desc_size;
711 u32 capabilities;
712 u32 version;
713 u32 intr_mask;
714 u32 quirks;
Faiz Abbas7feafb02019-10-15 18:24:36 +0530715
Bhupesh Sharmacb578552023-08-11 12:05:58 +0530716 /* Interrupt aggregation support is broken */
717#define UFSHCD_QUIRK_BROKEN_INTR_AGGR (1 << 0)
Marek Vasut91913a12023-08-16 17:05:50 +0200718
Bhupesh Sharmacb578552023-08-11 12:05:58 +0530719 /*
720 * delay before each dme command is required as the unipro
721 * layer has shown instabilities
722 */
723#define UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS (1 << 1)
Marek Vasutf4301512023-08-16 17:05:51 +0200724
Bhupesh Sharmacb578552023-08-11 12:05:58 +0530725 /*
726 * If UFS host controller is having issue in processing LCC (Line
727 * Control Command) coming from device then enable this quirk.
728 * When this quirk is enabled, host controller driver should disable
729 * the LCC transmission on UFS device (by clearing TX_LCC_ENABLE
730 * attribute of device to 0).
731 */
732#define UFSHCD_QUIRK_BROKEN_LCC (1 << 2)
Marek Vasut8f0b1693c2023-08-14 01:52:26 +0200733
Bhupesh Sharmacb578552023-08-11 12:05:58 +0530734 /*
735 * The attribute PA_RXHSUNTERMCAP specifies whether or not the
736 * inbound Link supports unterminated line in HS mode. Setting this
737 * attribute to 1 fixes moving to HS gear.
738 */
739#define UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP (1 << 3)
740
741 /*
742 * This quirk needs to be enabled if the host controller only allows
743 * accessing the peer dme attributes in AUTO mode (FAST AUTO or
744 * SLOW AUTO).
745 */
746#define UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE (1 << 4)
747
748 /*
749 * This quirk needs to be enabled if the host controller doesn't
750 * advertise the correct version in UFS_VER register. If this quirk
751 * is enabled, standard UFS host driver will call the vendor specific
752 * ops (get_ufs_hci_version) to get the correct version.
753 */
754#define UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION (1 << 5)
755
756 /*
757 * Clear handling for transfer/task request list is just opposite.
758 */
759#define UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR (1 << 6)
760
761 /*
762 * This quirk needs to be enabled if host controller doesn't allow
763 * that the interrupt aggregation timer and counter are reset by s/w.
764 */
765#define UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR (1 << 7)
766
767 /*
768 * This quirks needs to be enabled if host controller cannot be
769 * enabled via HCE register.
770 */
771#define UFSHCI_QUIRK_BROKEN_HCE (1 << 8)
772
773 /*
774 * This quirk needs to be enabled if the host controller regards
775 * resolution of the values of PRDTO and PRDTL in UTRD as byte.
776 */
777#define UFSHCD_QUIRK_PRDT_BYTE_GRAN (1 << 9)
778
779 /*
780 * This quirk needs to be enabled if the host controller reports
781 * OCS FATAL ERROR with device error through sense data
782 */
783#define UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR (1 << 10)
784
785 /*
786 * This quirk needs to be enabled if the host controller has
787 * auto-hibernate capability but it doesn't work.
788 */
789#define UFSHCD_QUIRK_BROKEN_AUTO_HIBERN8 (1 << 11)
790
791 /*
792 * This quirk needs to disable manual flush for write booster
793 */
794#define UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL (1 << 12)
795
796 /*
797 * This quirk needs to disable unipro timeout values
798 * before power mode change
799 */
800#define UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING (1 << 13)
801
802 /*
803 * Align DMA SG entries on a 4 KiB boundary.
804 */
805#define UFSHCD_QUIRK_4KB_DMA_ALIGNMENT (1 << 14)
806
807 /*
808 * This quirk needs to be enabled if the host controller does not
809 * support UIC command
810 */
811#define UFSHCD_QUIRK_BROKEN_UIC_CMD (1 << 15)
812
813 /*
814 * This quirk needs to be enabled if the host controller cannot
815 * support physical host configuration.
816 */
817#define UFSHCD_QUIRK_SKIP_PH_CONFIGURATION (1 << 16)
818
819 /*
820 * This quirk needs to be enabled if the host controller has
821 * 64-bit addressing supported capability but it doesn't work.
822 */
823#define UFSHCD_QUIRK_BROKEN_64BIT_ADDRESS (1 << 17)
824
825 /*
826 * This quirk needs to be enabled if the host controller has
827 * auto-hibernate capability but it's FASTAUTO only.
828 */
829#define UFSHCD_QUIRK_HIBERN_FASTAUTO (1 << 18)
830
831 /*
832 * This quirk needs to be enabled if the host controller needs
833 * to reinit the device after switching to maximum gear.
834 */
835#define UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH (1 << 19)
Marek Vasut2277f422023-08-14 01:52:27 +0200836
Caleb Connolly33233d72023-10-12 17:57:00 +0100837 /*
838 * Skips configuring the power mode and assumes lowest gear.
839 */
840#define UFSHCD_QUIRK_SKIP_CHANGE_POWER_MODE (1 << 20)
841
Faiz Abbas7feafb02019-10-15 18:24:36 +0530842 /* Virtual memory reference */
843 struct utp_transfer_cmd_desc *ucdl;
844 struct utp_transfer_req_desc *utrdl;
845 /* TODO: Add Task Manegement Support */
846 struct utp_task_req_desc *utmrdl;
847
848 struct utp_upiu_req *ucd_req_ptr;
849 struct utp_upiu_rsp *ucd_rsp_ptr;
850 struct ufshcd_sg_entry *ucd_prdt_ptr;
851
852 /* Power Mode information */
853 enum ufs_dev_pwr_mode curr_dev_pwr_mode;
854 struct ufs_pa_layer_attr pwr_info;
855 struct ufs_pwr_mode_info max_pwr_info;
856
857 struct ufs_dev_cmd dev_cmd;
858};
859
860static inline int ufshcd_ops_init(struct ufs_hba *hba)
861{
862 if (hba->ops && hba->ops->init)
863 return hba->ops->init(hba);
864
865 return 0;
866}
867
Bhupesh Sharma1d099902023-08-11 12:02:29 +0530868static inline void ufshcd_ops_dbg_register_dump(struct ufs_hba *hba)
869{
870 if (hba->ops && hba->ops->dbg_register_dump)
871 hba->ops->dbg_register_dump(hba);
872}
873
Faiz Abbas7feafb02019-10-15 18:24:36 +0530874static inline int ufshcd_ops_hce_enable_notify(struct ufs_hba *hba,
875 bool status)
876{
877 if (hba->ops && hba->ops->hce_enable_notify)
878 return hba->ops->hce_enable_notify(hba, status);
879
880 return 0;
881}
882
883static inline int ufshcd_ops_link_startup_notify(struct ufs_hba *hba,
884 bool status)
885{
886 if (hba->ops && hba->ops->link_startup_notify)
887 return hba->ops->link_startup_notify(hba, status);
888
889 return 0;
890}
891
892/* Controller UFSHCI version */
893enum {
894 UFSHCI_VERSION_10 = 0x00010000, /* 1.0 */
895 UFSHCI_VERSION_11 = 0x00010100, /* 1.1 */
896 UFSHCI_VERSION_20 = 0x00000200, /* 2.0 */
897 UFSHCI_VERSION_21 = 0x00000210, /* 2.1 */
Marek Vasut2ff810a2023-08-16 17:05:52 +0200898 UFSHCI_VERSION_30 = 0x00000300, /* 3.0 */
Faiz Abbas7feafb02019-10-15 18:24:36 +0530899};
900
901/* Interrupt disable masks */
902enum {
903 /* Interrupt disable mask for UFSHCI v1.0 */
904 INTERRUPT_MASK_ALL_VER_10 = 0x30FFF,
905 INTERRUPT_MASK_RW_VER_10 = 0x30000,
906
907 /* Interrupt disable mask for UFSHCI v1.1 */
908 INTERRUPT_MASK_ALL_VER_11 = 0x31FFF,
909
910 /* Interrupt disable mask for UFSHCI v2.1 */
911 INTERRUPT_MASK_ALL_VER_21 = 0x71FFF,
912};
913
914/* UFSHCI Registers */
915enum {
916 REG_CONTROLLER_CAPABILITIES = 0x00,
917 REG_UFS_VERSION = 0x08,
918 REG_CONTROLLER_DEV_ID = 0x10,
919 REG_CONTROLLER_PROD_ID = 0x14,
920 REG_AUTO_HIBERNATE_IDLE_TIMER = 0x18,
921 REG_INTERRUPT_STATUS = 0x20,
922 REG_INTERRUPT_ENABLE = 0x24,
923 REG_CONTROLLER_STATUS = 0x30,
924 REG_CONTROLLER_ENABLE = 0x34,
925 REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER = 0x38,
926 REG_UIC_ERROR_CODE_DATA_LINK_LAYER = 0x3C,
927 REG_UIC_ERROR_CODE_NETWORK_LAYER = 0x40,
928 REG_UIC_ERROR_CODE_TRANSPORT_LAYER = 0x44,
929 REG_UIC_ERROR_CODE_DME = 0x48,
930 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL = 0x4C,
931 REG_UTP_TRANSFER_REQ_LIST_BASE_L = 0x50,
932 REG_UTP_TRANSFER_REQ_LIST_BASE_H = 0x54,
933 REG_UTP_TRANSFER_REQ_DOOR_BELL = 0x58,
934 REG_UTP_TRANSFER_REQ_LIST_CLEAR = 0x5C,
935 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP = 0x60,
936 REG_UTP_TASK_REQ_LIST_BASE_L = 0x70,
937 REG_UTP_TASK_REQ_LIST_BASE_H = 0x74,
938 REG_UTP_TASK_REQ_DOOR_BELL = 0x78,
939 REG_UTP_TASK_REQ_LIST_CLEAR = 0x7C,
940 REG_UTP_TASK_REQ_LIST_RUN_STOP = 0x80,
941 REG_UIC_COMMAND = 0x90,
942 REG_UIC_COMMAND_ARG_1 = 0x94,
943 REG_UIC_COMMAND_ARG_2 = 0x98,
944 REG_UIC_COMMAND_ARG_3 = 0x9C,
945
946 UFSHCI_REG_SPACE_SIZE = 0xA0,
947
948 REG_UFS_CCAP = 0x100,
949 REG_UFS_CRYPTOCAP = 0x104,
950
951 UFSHCI_CRYPTO_REG_SPACE_SIZE = 0x400,
952};
953
954/* Controller capability masks */
955enum {
956 MASK_TRANSFER_REQUESTS_SLOTS = 0x0000001F,
957 MASK_TASK_MANAGEMENT_REQUEST_SLOTS = 0x00070000,
958 MASK_AUTO_HIBERN8_SUPPORT = 0x00800000,
959 MASK_64_ADDRESSING_SUPPORT = 0x01000000,
960 MASK_OUT_OF_ORDER_DATA_DELIVERY_SUPPORT = 0x02000000,
961 MASK_UIC_DME_TEST_MODE_SUPPORT = 0x04000000,
962};
963
964/* Interrupt Status 20h */
965#define UTP_TRANSFER_REQ_COMPL 0x1
966#define UIC_DME_END_PT_RESET 0x2
967#define UIC_ERROR 0x4
968#define UIC_TEST_MODE 0x8
969#define UIC_POWER_MODE 0x10
970#define UIC_HIBERNATE_EXIT 0x20
971#define UIC_HIBERNATE_ENTER 0x40
972#define UIC_LINK_LOST 0x80
973#define UIC_LINK_STARTUP 0x100
974#define UTP_TASK_REQ_COMPL 0x200
975#define UIC_COMMAND_COMPL 0x400
976#define DEVICE_FATAL_ERROR 0x800
977#define CONTROLLER_FATAL_ERROR 0x10000
978#define SYSTEM_BUS_FATAL_ERROR 0x20000
979
980#define UFSHCD_UIC_PWR_MASK (UIC_HIBERNATE_ENTER |\
981 UIC_HIBERNATE_EXIT |\
982 UIC_POWER_MODE)
983
984#define UFSHCD_UIC_MASK (UIC_COMMAND_COMPL | UIC_POWER_MODE)
985
Caleb Connolly33233d72023-10-12 17:57:00 +0100986#define UFSHCD_ERROR_MASK (DEVICE_FATAL_ERROR |\
Faiz Abbas7feafb02019-10-15 18:24:36 +0530987 CONTROLLER_FATAL_ERROR |\
988 SYSTEM_BUS_FATAL_ERROR)
989
990#define INT_FATAL_ERRORS (DEVICE_FATAL_ERROR |\
991 CONTROLLER_FATAL_ERROR |\
992 SYSTEM_BUS_FATAL_ERROR)
993
994/* Host Controller Enable 0x34h */
995#define CONTROLLER_ENABLE 0x1
996#define CONTROLLER_DISABLE 0x0
997/* HCS - Host Controller Status 30h */
998#define DEVICE_PRESENT 0x1
999#define UTP_TRANSFER_REQ_LIST_READY 0x2
1000#define UTP_TASK_REQ_LIST_READY 0x4
1001#define UIC_COMMAND_READY 0x8
1002#define HOST_ERROR_INDICATOR 0x10
1003#define DEVICE_ERROR_INDICATOR 0x20
1004#define UIC_POWER_MODE_CHANGE_REQ_STATUS_MASK UFS_MASK(0x7, 8)
1005
1006#define UFSHCD_STATUS_READY (UTP_TRANSFER_REQ_LIST_READY |\
1007 UTP_TASK_REQ_LIST_READY |\
1008 UIC_COMMAND_READY)
1009
1010enum {
1011 PWR_OK = 0x0,
1012 PWR_LOCAL = 0x01,
1013 PWR_REMOTE = 0x02,
1014 PWR_BUSY = 0x03,
1015 PWR_ERROR_CAP = 0x04,
1016 PWR_FATAL_ERROR = 0x05,
1017};
1018
1019/* UICCMD - UIC Command */
1020#define COMMAND_OPCODE_MASK 0xFF
1021#define GEN_SELECTOR_INDEX_MASK 0xFFFF
1022
1023#define MIB_ATTRIBUTE_MASK UFS_MASK(0xFFFF, 16)
1024#define RESET_LEVEL 0xFF
1025
1026#define ATTR_SET_TYPE_MASK UFS_MASK(0xFF, 16)
Tom Rini6e7df1d2023-01-10 11:19:45 -05001027#define CFG_RESULT_CODE_MASK 0xFF
Faiz Abbas7feafb02019-10-15 18:24:36 +05301028#define GENERIC_ERROR_CODE_MASK 0xFF
1029
1030#define ufshcd_writel(hba, val, reg) \
1031 writel((val), (hba)->mmio_base + (reg))
1032#define ufshcd_readl(hba, reg) \
1033 readl((hba)->mmio_base + (reg))
1034
Bhupesh Sharmaf5aa3d72023-08-11 11:50:16 +05301035/**
1036 * ufshcd_rmwl - perform read/modify/write for a controller register
1037 * @hba: per adapter instance
1038 * @mask: mask to apply on read value
1039 * @val: actual value to write
1040 * @reg: register address
1041 */
1042static inline void ufshcd_rmwl(struct ufs_hba *hba, u32 mask, u32 val, u32 reg)
1043{
1044 u32 tmp;
1045
1046 tmp = ufshcd_readl(hba, reg);
1047 tmp &= ~mask;
1048 tmp |= (val & mask);
1049 ufshcd_writel(hba, tmp, reg);
1050}
1051
Faiz Abbas7feafb02019-10-15 18:24:36 +05301052/* UTRLRSR - UTP Transfer Request Run-Stop Register 60h */
1053#define UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT 0x1
1054
1055/* UTMRLRSR - UTP Task Management Request Run-Stop Register 80h */
1056#define UTP_TASK_REQ_LIST_RUN_STOP_BIT 0x1
1057
1058int ufshcd_probe(struct udevice *dev, struct ufs_hba_ops *hba_ops);
Bhupesh Sharma1d099902023-08-11 12:02:29 +05301059int ufshcd_dump_regs(struct ufs_hba *hba, size_t offset, size_t len,
1060 const char *prefix);
Faiz Abbas7feafb02019-10-15 18:24:36 +05301061
1062#endif