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