blob: 53137fae3a81ce87d744dd56c49db2a8ffba1d3d [file] [log] [blame]
Faiz Abbas5cc51072019-10-15 18:24:36 +05301/* SPDX-License-Identifier: GPL-2.0+ */
2#ifndef __UFS_H
3#define __UFS_H
4
Tom Rinidec7ea02024-05-20 13:35:03 -06005#include <linux/types.h>
Bhupesh Sharma4531d7c2024-09-30 14:44:29 +02006#include <asm/io.h>
Neil Armstrongac4fa422024-12-30 11:30:58 +01007#include "ufshci.h"
Faiz Abbas5cc51072019-10-15 18:24:36 +05308#include "unipro.h"
9
Simon Glass0b700eb2020-07-19 10:15:54 -060010struct udevice;
11
Faiz Abbas5cc51072019-10-15 18:24:36 +053012#define UFS_CDB_SIZE 16
13#define UPIU_TRANSACTION_UIC_CMD 0x1F
14#define UIC_CMD_SIZE (sizeof(u32) * 4)
15#define RESPONSE_UPIU_SENSE_DATA_LENGTH 18
16#define UFS_MAX_LUNS 0x7F
17
Faiz Abbas5cc51072019-10-15 18:24:36 +053018
19/* UFS device power modes */
20enum ufs_dev_pwr_mode {
21 UFS_ACTIVE_PWR_MODE = 1,
22 UFS_SLEEP_PWR_MODE = 2,
23 UFS_POWERDOWN_PWR_MODE = 3,
24};
25
26enum ufs_notify_change_status {
27 PRE_CHANGE,
28 POST_CHANGE,
29};
30
31struct ufs_pa_layer_attr {
32 u32 gear_rx;
33 u32 gear_tx;
34 u32 lane_rx;
35 u32 lane_tx;
36 u32 pwr_rx;
37 u32 pwr_tx;
38 u32 hs_rate;
39};
40
41struct ufs_pwr_mode_info {
42 bool is_valid;
43 struct ufs_pa_layer_attr info;
44};
45
46enum ufs_desc_def_size {
47 QUERY_DESC_DEVICE_DEF_SIZE = 0x40,
48 QUERY_DESC_CONFIGURATION_DEF_SIZE = 0x90,
49 QUERY_DESC_UNIT_DEF_SIZE = 0x23,
50 QUERY_DESC_INTERCONNECT_DEF_SIZE = 0x06,
51 QUERY_DESC_GEOMETRY_DEF_SIZE = 0x48,
52 QUERY_DESC_POWER_DEF_SIZE = 0x62,
53 QUERY_DESC_HEALTH_DEF_SIZE = 0x25,
54};
55
56struct ufs_desc_size {
57 int dev_desc;
58 int pwr_desc;
59 int geom_desc;
60 int interc_desc;
61 int unit_desc;
62 int conf_desc;
63 int hlth_desc;
64};
65
66/*
67 * Request Descriptor Definitions
68 */
69
70/* Transfer request command type */
71enum {
72 UTP_CMD_TYPE_SCSI = 0x0,
73 UTP_CMD_TYPE_UFS = 0x1,
74 UTP_CMD_TYPE_DEV_MANAGE = 0x2,
75};
76
77/* UTP Transfer Request Command Offset */
78#define UPIU_COMMAND_TYPE_OFFSET 28
79
80/* Offset of the response code in the UPIU header */
81#define UPIU_RSP_CODE_OFFSET 8
82
Faiz Abbas5cc51072019-10-15 18:24:36 +053083#define GENERAL_UPIU_REQUEST_SIZE (sizeof(struct utp_upiu_req))
84#define QUERY_DESC_MAX_SIZE 255
85#define QUERY_DESC_MIN_SIZE 2
86#define QUERY_DESC_HDR_SIZE 2
87#define QUERY_OSF_SIZE (GENERAL_UPIU_REQUEST_SIZE - \
88 (sizeof(struct utp_upiu_header)))
89#define RESPONSE_UPIU_SENSE_DATA_LENGTH 18
90#define UPIU_HEADER_DWORD(byte3, byte2, byte1, byte0)\
Neil Armstrong671feab2024-12-30 11:30:57 +010091 cpu_to_be32(((byte3) << 24) | ((byte2) << 16) |\
92 ((byte1) << 8) | (byte0))
Faiz Abbas5cc51072019-10-15 18:24:36 +053093/*
94 * UFS Protocol Information Unit related definitions
95 */
96
97/* Task management functions */
98enum {
99 UFS_ABORT_TASK = 0x01,
100 UFS_ABORT_TASK_SET = 0x02,
101 UFS_CLEAR_TASK_SET = 0x04,
102 UFS_LOGICAL_RESET = 0x08,
103 UFS_QUERY_TASK = 0x80,
104 UFS_QUERY_TASK_SET = 0x81,
105};
106
107/* UTP UPIU Transaction Codes Initiator to Target */
108enum {
109 UPIU_TRANSACTION_NOP_OUT = 0x00,
110 UPIU_TRANSACTION_COMMAND = 0x01,
111 UPIU_TRANSACTION_DATA_OUT = 0x02,
112 UPIU_TRANSACTION_TASK_REQ = 0x04,
113 UPIU_TRANSACTION_QUERY_REQ = 0x16,
114};
115
116/* UTP UPIU Transaction Codes Target to Initiator */
117enum {
118 UPIU_TRANSACTION_NOP_IN = 0x20,
119 UPIU_TRANSACTION_RESPONSE = 0x21,
120 UPIU_TRANSACTION_DATA_IN = 0x22,
121 UPIU_TRANSACTION_TASK_RSP = 0x24,
122 UPIU_TRANSACTION_READY_XFER = 0x31,
123 UPIU_TRANSACTION_QUERY_RSP = 0x36,
124 UPIU_TRANSACTION_REJECT_UPIU = 0x3F,
125};
126
127/* UPIU Read/Write flags */
128enum {
129 UPIU_CMD_FLAGS_NONE = 0x00,
130 UPIU_CMD_FLAGS_WRITE = 0x20,
131 UPIU_CMD_FLAGS_READ = 0x40,
132};
133
134/* UPIU Task Attributes */
135enum {
136 UPIU_TASK_ATTR_SIMPLE = 0x00,
137 UPIU_TASK_ATTR_ORDERED = 0x01,
138 UPIU_TASK_ATTR_HEADQ = 0x02,
139 UPIU_TASK_ATTR_ACA = 0x03,
140};
141
142/* UPIU Query request function */
143enum {
144 UPIU_QUERY_FUNC_STANDARD_READ_REQUEST = 0x01,
145 UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST = 0x81,
146};
147
148/* Offset of the response code in the UPIU header */
149#define UPIU_RSP_CODE_OFFSET 8
150
151enum {
152 MASK_SCSI_STATUS = 0xFF,
153 MASK_TASK_RESPONSE = 0xFF00,
154 MASK_RSP_UPIU_RESULT = 0xFFFF,
155 MASK_QUERY_DATA_SEG_LEN = 0xFFFF,
156 MASK_RSP_UPIU_DATA_SEG_LEN = 0xFFFF,
157 MASK_RSP_EXCEPTION_EVENT = 0x10000,
158 MASK_TM_SERVICE_RESP = 0xFF,
159 MASK_TM_FUNC = 0xFF,
160};
161
162/* UTP QUERY Transaction Specific Fields OpCode */
163enum query_opcode {
164 UPIU_QUERY_OPCODE_NOP = 0x0,
165 UPIU_QUERY_OPCODE_READ_DESC = 0x1,
166 UPIU_QUERY_OPCODE_WRITE_DESC = 0x2,
167 UPIU_QUERY_OPCODE_READ_ATTR = 0x3,
168 UPIU_QUERY_OPCODE_WRITE_ATTR = 0x4,
169 UPIU_QUERY_OPCODE_READ_FLAG = 0x5,
170 UPIU_QUERY_OPCODE_SET_FLAG = 0x6,
171 UPIU_QUERY_OPCODE_CLEAR_FLAG = 0x7,
172 UPIU_QUERY_OPCODE_TOGGLE_FLAG = 0x8,
173};
174
175/* Query response result code */
176enum {
177 QUERY_RESULT_SUCCESS = 0x00,
178 QUERY_RESULT_NOT_READABLE = 0xF6,
179 QUERY_RESULT_NOT_WRITEABLE = 0xF7,
180 QUERY_RESULT_ALREADY_WRITTEN = 0xF8,
181 QUERY_RESULT_INVALID_LENGTH = 0xF9,
182 QUERY_RESULT_INVALID_VALUE = 0xFA,
183 QUERY_RESULT_INVALID_SELECTOR = 0xFB,
184 QUERY_RESULT_INVALID_INDEX = 0xFC,
185 QUERY_RESULT_INVALID_IDN = 0xFD,
186 QUERY_RESULT_INVALID_OPCODE = 0xFE,
187 QUERY_RESULT_GENERAL_FAILURE = 0xFF,
188};
189
190enum {
191 UPIU_COMMAND_SET_TYPE_SCSI = 0x0,
192 UPIU_COMMAND_SET_TYPE_UFS = 0x1,
193 UPIU_COMMAND_SET_TYPE_QUERY = 0x2,
194};
195
196/* Flag idn for Query Requests*/
197enum flag_idn {
198 QUERY_FLAG_IDN_FDEVICEINIT = 0x01,
199 QUERY_FLAG_IDN_PERMANENT_WPE = 0x02,
200 QUERY_FLAG_IDN_PWR_ON_WPE = 0x03,
201 QUERY_FLAG_IDN_BKOPS_EN = 0x04,
202 QUERY_FLAG_IDN_LIFE_SPAN_MODE_ENABLE = 0x05,
203 QUERY_FLAG_IDN_PURGE_ENABLE = 0x06,
204 QUERY_FLAG_IDN_RESERVED2 = 0x07,
205 QUERY_FLAG_IDN_FPHYRESOURCEREMOVAL = 0x08,
206 QUERY_FLAG_IDN_BUSY_RTC = 0x09,
207 QUERY_FLAG_IDN_RESERVED3 = 0x0A,
208 QUERY_FLAG_IDN_PERMANENTLY_DISABLE_FW_UPDATE = 0x0B,
209};
210
211/* Attribute idn for Query requests */
212enum attr_idn {
213 QUERY_ATTR_IDN_BOOT_LU_EN = 0x00,
214 QUERY_ATTR_IDN_RESERVED = 0x01,
215 QUERY_ATTR_IDN_POWER_MODE = 0x02,
216 QUERY_ATTR_IDN_ACTIVE_ICC_LVL = 0x03,
217 QUERY_ATTR_IDN_OOO_DATA_EN = 0x04,
218 QUERY_ATTR_IDN_BKOPS_STATUS = 0x05,
219 QUERY_ATTR_IDN_PURGE_STATUS = 0x06,
220 QUERY_ATTR_IDN_MAX_DATA_IN = 0x07,
221 QUERY_ATTR_IDN_MAX_DATA_OUT = 0x08,
222 QUERY_ATTR_IDN_DYN_CAP_NEEDED = 0x09,
223 QUERY_ATTR_IDN_REF_CLK_FREQ = 0x0A,
224 QUERY_ATTR_IDN_CONF_DESC_LOCK = 0x0B,
225 QUERY_ATTR_IDN_MAX_NUM_OF_RTT = 0x0C,
226 QUERY_ATTR_IDN_EE_CONTROL = 0x0D,
227 QUERY_ATTR_IDN_EE_STATUS = 0x0E,
228 QUERY_ATTR_IDN_SECONDS_PASSED = 0x0F,
229 QUERY_ATTR_IDN_CNTX_CONF = 0x10,
230 QUERY_ATTR_IDN_CORR_PRG_BLK_NUM = 0x11,
231 QUERY_ATTR_IDN_RESERVED2 = 0x12,
232 QUERY_ATTR_IDN_RESERVED3 = 0x13,
233 QUERY_ATTR_IDN_FFU_STATUS = 0x14,
234 QUERY_ATTR_IDN_PSA_STATE = 0x15,
235 QUERY_ATTR_IDN_PSA_DATA_SIZE = 0x16,
236};
237
238/* Descriptor idn for Query requests */
239enum desc_idn {
240 QUERY_DESC_IDN_DEVICE = 0x0,
241 QUERY_DESC_IDN_CONFIGURATION = 0x1,
242 QUERY_DESC_IDN_UNIT = 0x2,
243 QUERY_DESC_IDN_RFU_0 = 0x3,
244 QUERY_DESC_IDN_INTERCONNECT = 0x4,
245 QUERY_DESC_IDN_STRING = 0x5,
246 QUERY_DESC_IDN_RFU_1 = 0x6,
247 QUERY_DESC_IDN_GEOMETRY = 0x7,
248 QUERY_DESC_IDN_POWER = 0x8,
249 QUERY_DESC_IDN_HEALTH = 0x9,
250 QUERY_DESC_IDN_MAX,
251};
252
253enum desc_header_offset {
254 QUERY_DESC_LENGTH_OFFSET = 0x00,
255 QUERY_DESC_DESC_TYPE_OFFSET = 0x01,
256};
257
Faiz Abbas5cc51072019-10-15 18:24:36 +0530258/**
259 * struct utp_upiu_query - upiu request buffer structure for
260 * query request.
261 * @opcode: command to perform B-0
262 * @idn: a value that indicates the particular type of data B-1
263 * @index: Index to further identify data B-2
264 * @selector: Index to further identify data B-3
265 * @reserved_osf: spec reserved field B-4,5
266 * @length: number of descriptor bytes to read/write B-6,7
267 * @value: Attribute value to be written DW-5
268 * @reserved: spec reserved DW-6,7
269 */
270struct utp_upiu_query {
271 __u8 opcode;
272 __u8 idn;
273 __u8 index;
274 __u8 selector;
275 __be16 reserved_osf;
276 __be16 length;
277 __be32 value;
278 __be32 reserved[2];
279};
280
281/**
282 * struct utp_upiu_cmd - Command UPIU structure
283 * @data_transfer_len: Data Transfer Length DW-3
284 * @cdb: Command Descriptor Block CDB DW-4 to DW-7
285 */
286struct utp_upiu_cmd {
287 __be32 exp_data_transfer_len;
288 u8 cdb[UFS_CDB_SIZE];
289};
290
Faiz Abbas5cc51072019-10-15 18:24:36 +0530291/**
292 * struct utp_upiu_req - general upiu request structure
293 * @header:UPIU header structure DW-0 to DW-2
294 * @sc: fields structure for scsi command DW-3 to DW-7
295 * @qr: fields structure for query request DW-3 to DW-7
296 */
297struct utp_upiu_req {
298 struct utp_upiu_header header;
299 union {
300 struct utp_upiu_cmd sc;
301 struct utp_upiu_query qr;
302 struct utp_upiu_query tr;
303 /* use utp_upiu_query to host the 4 dwords of uic command */
304 struct utp_upiu_query uc;
305 };
306};
307
308/**
309 * struct utp_cmd_rsp - Response UPIU structure
310 * @residual_transfer_count: Residual transfer count DW-3
311 * @reserved: Reserved double words DW-4 to DW-7
312 * @sense_data_len: Sense data length DW-8 U16
313 * @sense_data: Sense data field DW-8 to DW-12
314 */
315struct utp_cmd_rsp {
316 __be32 residual_transfer_count;
317 __be32 reserved[4];
318 __be16 sense_data_len;
319 u8 sense_data[RESPONSE_UPIU_SENSE_DATA_LENGTH];
320};
321
322/**
323 * struct utp_upiu_rsp - general upiu response structure
324 * @header: UPIU header structure DW-0 to DW-2
325 * @sr: fields structure for scsi command DW-3 to DW-12
326 * @qr: fields structure for query request DW-3 to DW-7
327 */
328struct utp_upiu_rsp {
329 struct utp_upiu_header header;
330 union {
331 struct utp_cmd_rsp sr;
332 struct utp_upiu_query qr;
333 };
334};
335
336#define MAX_MODEL_LEN 16
337/**
338 * ufs_dev_desc - ufs device details from the device descriptor
339 *
340 * @wmanufacturerid: card details
341 * @model: card model
342 */
343struct ufs_dev_desc {
344 u16 wmanufacturerid;
345 char model[MAX_MODEL_LEN + 1];
346};
347
348/* Device descriptor parameters offsets in bytes*/
349enum device_desc_param {
350 DEVICE_DESC_PARAM_LEN = 0x0,
351 DEVICE_DESC_PARAM_TYPE = 0x1,
352 DEVICE_DESC_PARAM_DEVICE_TYPE = 0x2,
353 DEVICE_DESC_PARAM_DEVICE_CLASS = 0x3,
354 DEVICE_DESC_PARAM_DEVICE_SUB_CLASS = 0x4,
355 DEVICE_DESC_PARAM_PRTCL = 0x5,
356 DEVICE_DESC_PARAM_NUM_LU = 0x6,
357 DEVICE_DESC_PARAM_NUM_WLU = 0x7,
358 DEVICE_DESC_PARAM_BOOT_ENBL = 0x8,
359 DEVICE_DESC_PARAM_DESC_ACCSS_ENBL = 0x9,
360 DEVICE_DESC_PARAM_INIT_PWR_MODE = 0xA,
361 DEVICE_DESC_PARAM_HIGH_PR_LUN = 0xB,
362 DEVICE_DESC_PARAM_SEC_RMV_TYPE = 0xC,
363 DEVICE_DESC_PARAM_SEC_LU = 0xD,
364 DEVICE_DESC_PARAM_BKOP_TERM_LT = 0xE,
365 DEVICE_DESC_PARAM_ACTVE_ICC_LVL = 0xF,
366 DEVICE_DESC_PARAM_SPEC_VER = 0x10,
367 DEVICE_DESC_PARAM_MANF_DATE = 0x12,
368 DEVICE_DESC_PARAM_MANF_NAME = 0x14,
369 DEVICE_DESC_PARAM_PRDCT_NAME = 0x15,
370 DEVICE_DESC_PARAM_SN = 0x16,
371 DEVICE_DESC_PARAM_OEM_ID = 0x17,
372 DEVICE_DESC_PARAM_MANF_ID = 0x18,
373 DEVICE_DESC_PARAM_UD_OFFSET = 0x1A,
374 DEVICE_DESC_PARAM_UD_LEN = 0x1B,
375 DEVICE_DESC_PARAM_RTT_CAP = 0x1C,
376 DEVICE_DESC_PARAM_FRQ_RTC = 0x1D,
377 DEVICE_DESC_PARAM_UFS_FEAT = 0x1F,
378 DEVICE_DESC_PARAM_FFU_TMT = 0x20,
379 DEVICE_DESC_PARAM_Q_DPTH = 0x21,
380 DEVICE_DESC_PARAM_DEV_VER = 0x22,
381 DEVICE_DESC_PARAM_NUM_SEC_WPA = 0x24,
382 DEVICE_DESC_PARAM_PSA_MAX_DATA = 0x25,
383 DEVICE_DESC_PARAM_PSA_TMT = 0x29,
384 DEVICE_DESC_PARAM_PRDCT_REV = 0x2A,
385};
386
387struct ufs_hba;
388
389enum {
390 UFSHCD_MAX_CHANNEL = 0,
391 UFSHCD_MAX_ID = 1,
392};
393
394enum dev_cmd_type {
395 DEV_CMD_TYPE_NOP = 0x0,
396 DEV_CMD_TYPE_QUERY = 0x1,
397};
398
399/**
400 * struct uic_command - UIC command structure
401 * @command: UIC command
402 * @argument1: UIC command argument 1
403 * @argument2: UIC command argument 2
404 * @argument3: UIC command argument 3
405 * @cmd_active: Indicate if UIC command is outstanding
406 * @result: UIC command result
407 * @done: UIC command completion
408 */
409struct uic_command {
410 u32 command;
411 u32 argument1;
412 u32 argument2;
413 u32 argument3;
414 int cmd_active;
415 int result;
416};
417
Faiz Abbas5cc51072019-10-15 18:24:36 +0530418/* Host <-> Device UniPro Link state */
419enum uic_link_state {
420 UIC_LINK_OFF_STATE = 0, /* Link powered down or disabled */
421 UIC_LINK_ACTIVE_STATE = 1, /* Link is in Fast/Slow/Sleep state */
422 UIC_LINK_HIBERN8_STATE = 2, /* Link is in Hibernate state */
423};
424
425/* UIC command interfaces for DME primitives */
426#define DME_LOCAL 0
427#define DME_PEER 1
428#define ATTR_SET_NOR 0 /* NORMAL */
429#define ATTR_SET_ST 1 /* STATIC */
430
431int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
432 u8 attr_set, u32 mib_val, u8 peer);
433int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
434 u32 *mib_val, u8 peer);
435
436static inline int ufshcd_dme_set(struct ufs_hba *hba, u32 attr_sel,
437 u32 mib_val)
438{
439 return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,
440 mib_val, DME_LOCAL);
441}
442
443static inline int ufshcd_dme_get(struct ufs_hba *hba,
444 u32 attr_sel, u32 *mib_val)
445{
446 return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_LOCAL);
447}
448
449static inline int ufshcd_dme_peer_get(struct ufs_hba *hba,
450 u32 attr_sel, u32 *mib_val)
451{
452 return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_PEER);
453}
454
455static inline int ufshcd_dme_peer_set(struct ufs_hba *hba, u32 attr_sel,
456 u32 mib_val)
457{
458 return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,
459 mib_val, DME_PEER);
460}
461
462/**
463 * struct ufs_query_req - parameters for building a query request
464 * @query_func: UPIU header query function
465 * @upiu_req: the query request data
466 */
467struct ufs_query_req {
468 u8 query_func;
469 struct utp_upiu_query upiu_req;
470};
471
472/**
473 * struct ufs_query_resp - UPIU QUERY
474 * @response: device response code
475 * @upiu_res: query response data
476 */
477struct ufs_query_res {
478 u8 response;
479 struct utp_upiu_query upiu_res;
480};
481
482/**
483 * struct ufs_query - holds relevant data structures for query request
484 * @request: request upiu and function
485 * @descriptor: buffer for sending/receiving descriptor
486 * @response: response upiu and response
487 */
488struct ufs_query {
489 struct ufs_query_req request;
490 u8 *descriptor;
491 struct ufs_query_res response;
492};
493
494/**
495 * struct ufs_dev_cmd - all assosiated fields with device management commands
496 * @type: device management command type - Query, NOP OUT
497 * @tag_wq: wait queue until free command slot is available
498 */
499struct ufs_dev_cmd {
500 enum dev_cmd_type type;
501 struct ufs_query query;
502};
503
504struct ufs_hba_ops {
505 int (*init)(struct ufs_hba *hba);
Neil Armstrong8bbf6de2024-09-10 11:50:11 +0200506 int (*get_max_pwr_mode)(struct ufs_hba *hba,
507 struct ufs_pwr_mode_info *max_pwr_info);
Faiz Abbas5cc51072019-10-15 18:24:36 +0530508 int (*hce_enable_notify)(struct ufs_hba *hba,
509 enum ufs_notify_change_status);
510 int (*link_startup_notify)(struct ufs_hba *hba,
511 enum ufs_notify_change_status);
512 int (*phy_initialization)(struct ufs_hba *hba);
Neil Armstrong5168a8b2024-09-10 11:50:10 +0200513 int (*device_reset)(struct ufs_hba *hba);
Faiz Abbas5cc51072019-10-15 18:24:36 +0530514};
515
Bhupesh Sharma6da82892024-09-30 14:44:31 +0200516enum ufshcd_quirks {
517 /* Interrupt aggregation support is broken */
518 UFSHCD_QUIRK_BROKEN_INTR_AGGR = 1 << 0,
519
520 /*
521 * delay before each dme command is required as the unipro
522 * layer has shown instabilities
523 */
524 UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS = 1 << 1,
525
526 /*
527 * If UFS host controller is having issue in processing LCC (Line
528 * Control Command) coming from device then enable this quirk.
529 * When this quirk is enabled, host controller driver should disable
530 * the LCC transmission on UFS device (by clearing TX_LCC_ENABLE
531 * attribute of device to 0).
532 */
533 UFSHCD_QUIRK_BROKEN_LCC = 1 << 2,
534
535 /*
536 * The attribute PA_RXHSUNTERMCAP specifies whether or not the
537 * inbound Link supports unterminated line in HS mode. Setting this
538 * attribute to 1 fixes moving to HS gear.
539 */
540 UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP = 1 << 3,
541
542 /*
543 * This quirk needs to be enabled if the host controller only allows
544 * accessing the peer dme attributes in AUTO mode (FAST AUTO or
545 * SLOW AUTO).
546 */
547 UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE = 1 << 4,
548
549 /*
550 * This quirk needs to be enabled if the host controller doesn't
551 * advertise the correct version in UFS_VER register. If this quirk
552 * is enabled, standard UFS host driver will call the vendor specific
553 * ops (get_ufs_hci_version) to get the correct version.
554 */
555 UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION = 1 << 5,
556
557 /*
558 * Clear handling for transfer/task request list is just opposite.
559 */
560 UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR = 1 << 6,
561
562 /*
563 * This quirk needs to be enabled if host controller doesn't allow
564 * that the interrupt aggregation timer and counter are reset by s/w.
565 */
566 UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR = 1 << 7,
567
568 /*
569 * This quirks needs to be enabled if host controller cannot be
570 * enabled via HCE register.
571 */
572 UFSHCI_QUIRK_BROKEN_HCE = 1 << 8,
573
574 /*
575 * This quirk needs to be enabled if the host controller regards
576 * resolution of the values of PRDTO and PRDTL in UTRD as byte.
577 */
578 UFSHCD_QUIRK_PRDT_BYTE_GRAN = 1 << 9,
579
580 /*
581 * This quirk needs to be enabled if the host controller reports
582 * OCS FATAL ERROR with device error through sense data
583 */
584 UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR = 1 << 10,
585
586 /*
587 * This quirk needs to be enabled if the host controller has
588 * auto-hibernate capability but it doesn't work.
589 */
590 UFSHCD_QUIRK_BROKEN_AUTO_HIBERN8 = 1 << 11,
591
592 /*
593 * This quirk needs to disable manual flush for write booster
594 */
595 UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL = 1 << 12,
596
597 /*
598 * This quirk needs to disable unipro timeout values
599 * before power mode change
600 */
601 UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING = 1 << 13,
602
603 /*
604 * This quirk needs to be enabled if the host controller does not
605 * support UIC command
606 */
607 UFSHCD_QUIRK_BROKEN_UIC_CMD = 1 << 15,
608
609 /*
610 * This quirk needs to be enabled if the host controller cannot
611 * support physical host configuration.
612 */
613 UFSHCD_QUIRK_SKIP_PH_CONFIGURATION = 1 << 16,
614
615 /*
616 * This quirk needs to be enabled if the host controller has
617 * 64-bit addressing supported capability but it doesn't work.
618 */
619 UFSHCD_QUIRK_BROKEN_64BIT_ADDRESS = 1 << 17,
620
621 /*
622 * This quirk needs to be enabled if the host controller has
623 * auto-hibernate capability but it's FASTAUTO only.
624 */
625 UFSHCD_QUIRK_HIBERN_FASTAUTO = 1 << 18,
626
627 /*
628 * This quirk needs to be enabled if the host controller needs
629 * to reinit the device after switching to maximum gear.
630 */
631 UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH = 1 << 19,
632
633 /*
634 * Some host raises interrupt (per queue) in addition to
635 * CQES (traditional) when ESI is disabled.
636 * Enable this quirk will disable CQES and use per queue interrupt.
637 */
638 UFSHCD_QUIRK_MCQ_BROKEN_INTR = 1 << 20,
639
640 /*
641 * Some host does not implement SQ Run Time Command (SQRTC) register
642 * thus need this quirk to skip related flow.
643 */
644 UFSHCD_QUIRK_MCQ_BROKEN_RTC = 1 << 21,
645
646 /*
647 * This quirk needs to be enabled if the host controller supports inline
648 * encryption but it needs to initialize the crypto capabilities in a
649 * nonstandard way and/or needs to override blk_crypto_ll_ops. If
650 * enabled, the standard code won't initialize the blk_crypto_profile;
651 * ufs_hba_variant_ops::init() must do it instead.
652 */
653 UFSHCD_QUIRK_CUSTOM_CRYPTO_PROFILE = 1 << 22,
654
655 /*
656 * This quirk needs to be enabled if the host controller supports inline
657 * encryption but does not support the CRYPTO_GENERAL_ENABLE bit, i.e.
658 * host controller initialization fails if that bit is set.
659 */
660 UFSHCD_QUIRK_BROKEN_CRYPTO_ENABLE = 1 << 23,
661
662 /*
663 * This quirk needs to be enabled if the host controller driver copies
664 * cryptographic keys into the PRDT in order to send them to hardware,
665 * and therefore the PRDT should be zeroized after each request (as per
666 * the standard best practice for managing keys).
667 */
668 UFSHCD_QUIRK_KEYS_IN_PRDT = 1 << 24,
669
670 /*
671 * This quirk indicates that the controller reports the value 1 (not
672 * supported) in the Legacy Single DoorBell Support (LSDBS) bit of the
673 * Controller Capabilities register although it supports the legacy
674 * single doorbell mode.
675 */
676 UFSHCD_QUIRK_BROKEN_LSDBS_CAP = 1 << 25,
677};
678
Faiz Abbas5cc51072019-10-15 18:24:36 +0530679struct ufs_hba {
680 struct udevice *dev;
681 void __iomem *mmio_base;
682 struct ufs_hba_ops *ops;
683 struct ufs_desc_size desc_size;
684 u32 capabilities;
685 u32 version;
686 u32 intr_mask;
Bhupesh Sharma6da82892024-09-30 14:44:31 +0200687 enum ufshcd_quirks quirks;
Marek Vasutc2403ff2024-09-30 14:44:28 +0200688
Faiz Abbas5cc51072019-10-15 18:24:36 +0530689 /* Virtual memory reference */
690 struct utp_transfer_cmd_desc *ucdl;
691 struct utp_transfer_req_desc *utrdl;
692 /* TODO: Add Task Manegement Support */
693 struct utp_task_req_desc *utmrdl;
694
695 struct utp_upiu_req *ucd_req_ptr;
696 struct utp_upiu_rsp *ucd_rsp_ptr;
697 struct ufshcd_sg_entry *ucd_prdt_ptr;
698
699 /* Power Mode information */
700 enum ufs_dev_pwr_mode curr_dev_pwr_mode;
701 struct ufs_pa_layer_attr pwr_info;
702 struct ufs_pwr_mode_info max_pwr_info;
703
704 struct ufs_dev_cmd dev_cmd;
705};
706
707static inline int ufshcd_ops_init(struct ufs_hba *hba)
708{
709 if (hba->ops && hba->ops->init)
710 return hba->ops->init(hba);
711
712 return 0;
713}
714
Neil Armstrong8bbf6de2024-09-10 11:50:11 +0200715static inline int ufshcd_ops_get_max_pwr_mode(struct ufs_hba *hba,
716 struct ufs_pwr_mode_info *max_pwr_info)
717{
718 if (hba->ops && hba->ops->get_max_pwr_mode)
719 return hba->ops->get_max_pwr_mode(hba, max_pwr_info);
720
721 return 0;
722}
723
Faiz Abbas5cc51072019-10-15 18:24:36 +0530724static inline int ufshcd_ops_hce_enable_notify(struct ufs_hba *hba,
Neil Armstrong671feab2024-12-30 11:30:57 +0100725 bool status)
Faiz Abbas5cc51072019-10-15 18:24:36 +0530726{
727 if (hba->ops && hba->ops->hce_enable_notify)
728 return hba->ops->hce_enable_notify(hba, status);
729
730 return 0;
731}
732
733static inline int ufshcd_ops_link_startup_notify(struct ufs_hba *hba,
734 bool status)
735{
736 if (hba->ops && hba->ops->link_startup_notify)
737 return hba->ops->link_startup_notify(hba, status);
738
739 return 0;
740}
741
Neil Armstrong5168a8b2024-09-10 11:50:10 +0200742static inline int ufshcd_vops_device_reset(struct ufs_hba *hba)
743{
744 if (hba->ops && hba->ops->device_reset)
745 return hba->ops->device_reset(hba);
746
747 return 0;
748}
749
Faiz Abbas5cc51072019-10-15 18:24:36 +0530750/* Interrupt disable masks */
751enum {
752 /* Interrupt disable mask for UFSHCI v1.0 */
753 INTERRUPT_MASK_ALL_VER_10 = 0x30FFF,
754 INTERRUPT_MASK_RW_VER_10 = 0x30000,
755
756 /* Interrupt disable mask for UFSHCI v1.1 */
757 INTERRUPT_MASK_ALL_VER_11 = 0x31FFF,
758
759 /* Interrupt disable mask for UFSHCI v2.1 */
760 INTERRUPT_MASK_ALL_VER_21 = 0x71FFF,
761};
762
Faiz Abbas5cc51072019-10-15 18:24:36 +0530763#define ufshcd_writel(hba, val, reg) \
764 writel((val), (hba)->mmio_base + (reg))
765#define ufshcd_readl(hba, reg) \
766 readl((hba)->mmio_base + (reg))
767
Bhupesh Sharma4531d7c2024-09-30 14:44:29 +0200768/**
769 * ufshcd_rmwl - perform read/modify/write for a controller register
770 * @hba: per adapter instance
771 * @mask: mask to apply on read value
772 * @val: actual value to write
773 * @reg: register address
774 */
775static inline void ufshcd_rmwl(struct ufs_hba *hba, u32 mask, u32 val, u32 reg)
776{
777 u32 tmp;
778
779 tmp = ufshcd_readl(hba, reg);
780 tmp &= ~mask;
781 tmp |= (val & mask);
782 ufshcd_writel(hba, tmp, reg);
783}
784
Faiz Abbas5cc51072019-10-15 18:24:36 +0530785int ufshcd_probe(struct udevice *dev, struct ufs_hba_ops *hba_ops);
786
787#endif