1 // SPDX-License-Identifier: GPL-2.0+
3 * ufs.c - Universal Flash Storage (UFS) driver
5 * Taken from Linux Kernel v5.2 (drivers/scsi/ufs/ufshcd.c) and ported
8 * Copyright (C) 2019 Texas Instruments Incorporated - https://www.ti.com
11 #include <bouncebuf.h>
15 #include <dm/device_compat.h>
16 #include <dm/devres.h>
18 #include <dm/device-internal.h>
23 #include <asm/dma-mapping.h>
24 #include <linux/bitops.h>
25 #include <linux/delay.h>
26 #include <linux/dma-mapping.h>
30 #define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
33 /* maximum number of link-startup retries */
34 #define DME_LINKSTARTUP_RETRIES 3
36 /* maximum number of retries for a general UIC command */
37 #define UFS_UIC_COMMAND_RETRIES 3
39 /* Query request retries */
40 #define QUERY_REQ_RETRIES 3
41 /* Query request timeout */
42 #define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */
44 /* maximum timeout in ms for a general UIC command */
45 #define UFS_UIC_CMD_TIMEOUT 1000
46 /* NOP OUT retries waiting for NOP IN response */
47 #define NOP_OUT_RETRIES 10
48 /* Timeout after 30 msecs if NOP OUT hangs without response */
49 #define NOP_OUT_TIMEOUT 30 /* msecs */
51 /* Only use one Task Tag for all requests */
54 /* Expose the flag value from utp_upiu_query.value */
55 #define MASK_QUERY_UPIU_FLAG_LOC 0xFF
57 #define MAX_PRDT_ENTRY 262144
59 /* maximum bytes per request */
60 #define UFS_MAX_BYTES (128 * 256 * 1024)
62 static inline bool ufshcd_is_hba_active(struct ufs_hba *hba);
63 static inline void ufshcd_hba_stop(struct ufs_hba *hba);
64 static int ufshcd_hba_enable(struct ufs_hba *hba);
67 * ufshcd_wait_for_register - wait for register value to change
69 static int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
70 u32 val, unsigned long timeout_ms)
73 unsigned long start = get_timer(0);
75 /* ignore bits that we don't intend to wait on */
78 while ((ufshcd_readl(hba, reg) & mask) != val) {
79 if (get_timer(start) > timeout_ms) {
80 if ((ufshcd_readl(hba, reg) & mask) != val)
90 * ufshcd_init_pwr_info - setting the POR (power on reset)
91 * values in hba power info
93 static void ufshcd_init_pwr_info(struct ufs_hba *hba)
95 hba->pwr_info.gear_rx = UFS_PWM_G1;
96 hba->pwr_info.gear_tx = UFS_PWM_G1;
97 hba->pwr_info.lane_rx = 1;
98 hba->pwr_info.lane_tx = 1;
99 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
100 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
101 hba->pwr_info.hs_rate = 0;
105 * ufshcd_print_pwr_info - print power params as saved in hba
108 static void ufshcd_print_pwr_info(struct ufs_hba *hba)
110 static const char * const names[] = {
120 dev_err(hba->dev, "[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n",
121 hba->pwr_info.gear_rx, hba->pwr_info.gear_tx,
122 hba->pwr_info.lane_rx, hba->pwr_info.lane_tx,
123 names[hba->pwr_info.pwr_rx],
124 names[hba->pwr_info.pwr_tx],
125 hba->pwr_info.hs_rate);
129 * ufshcd_ready_for_uic_cmd - Check if controller is ready
130 * to accept UIC commands
132 static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
134 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
141 * ufshcd_get_uic_cmd_result - Get the UIC command result
143 static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
145 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
146 MASK_UIC_COMMAND_RESULT;
150 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
152 static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
154 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
158 * ufshcd_is_device_present - Check if any device connected to
159 * the host controller
161 static inline bool ufshcd_is_device_present(struct ufs_hba *hba)
163 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
164 DEVICE_PRESENT) ? true : false;
168 * ufshcd_send_uic_cmd - UFS Interconnect layer command API
171 static int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
173 unsigned long start = 0;
175 u32 enabled_intr_status;
177 if (!ufshcd_ready_for_uic_cmd(hba)) {
179 "Controller not ready to accept UIC commands\n");
183 debug("sending uic command:%d\n", uic_cmd->command);
186 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
187 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
188 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
191 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
194 start = get_timer(0);
196 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
197 enabled_intr_status = intr_status & hba->intr_mask;
198 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
200 if (get_timer(start) > UFS_UIC_CMD_TIMEOUT) {
202 "Timedout waiting for UIC response\n");
207 if (enabled_intr_status & UFSHCD_ERROR_MASK) {
208 dev_err(hba->dev, "Error in status:%08x\n",
209 enabled_intr_status);
213 } while (!(enabled_intr_status & UFSHCD_UIC_MASK));
215 uic_cmd->argument2 = ufshcd_get_uic_cmd_result(hba);
216 uic_cmd->argument3 = ufshcd_get_dme_attr_val(hba);
218 debug("Sent successfully\n");
224 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
227 int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel, u8 attr_set,
228 u32 mib_val, u8 peer)
230 struct uic_command uic_cmd = {0};
231 static const char *const action[] = {
235 const char *set = action[!!peer];
237 int retries = UFS_UIC_COMMAND_RETRIES;
239 uic_cmd.command = peer ?
240 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
241 uic_cmd.argument1 = attr_sel;
242 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
243 uic_cmd.argument3 = mib_val;
246 /* for peer attributes we retry upon failure */
247 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
249 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
250 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
251 } while (ret && peer && --retries);
254 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
255 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
256 UFS_UIC_COMMAND_RETRIES - retries);
262 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
265 int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
266 u32 *mib_val, u8 peer)
268 struct uic_command uic_cmd = {0};
269 static const char *const action[] = {
273 const char *get = action[!!peer];
275 int retries = UFS_UIC_COMMAND_RETRIES;
277 uic_cmd.command = peer ?
278 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
279 uic_cmd.argument1 = attr_sel;
282 /* for peer attributes we retry upon failure */
283 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
285 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
286 get, UIC_GET_ATTR_ID(attr_sel), ret);
287 } while (ret && peer && --retries);
290 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
291 get, UIC_GET_ATTR_ID(attr_sel),
292 UFS_UIC_COMMAND_RETRIES - retries);
295 *mib_val = uic_cmd.argument3;
300 static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
302 u32 tx_lanes, i, err = 0;
305 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
308 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
310 for (i = 0; i < tx_lanes; i++) {
312 err = ufshcd_dme_set(hba,
313 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
314 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
317 err = ufshcd_dme_peer_set(hba,
318 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
319 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
322 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d\n",
323 __func__, peer, i, err);
331 static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
333 return ufshcd_disable_tx_lcc(hba, true);
337 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
340 static int ufshcd_dme_link_startup(struct ufs_hba *hba)
342 struct uic_command uic_cmd = {0};
345 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
347 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
350 "dme-link-startup: error code %d\n", ret);
355 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
358 static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
360 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
364 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
366 static inline int ufshcd_get_lists_status(u32 reg)
368 return !((reg & UFSHCD_STATUS_READY) == UFSHCD_STATUS_READY);
372 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
373 * When run-stop registers are set to 1, it indicates the
374 * host controller that it can process the requests
376 static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
378 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
379 REG_UTP_TASK_REQ_LIST_RUN_STOP);
380 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
381 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
385 * ufshcd_enable_intr - enable interrupts
387 static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
389 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
392 if (hba->version == UFSHCI_VERSION_10) {
393 rw = set & INTERRUPT_MASK_RW_VER_10;
394 set = rw | ((set ^ intrs) & intrs);
399 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
401 hba->intr_mask = set;
405 * ufshcd_make_hba_operational - Make UFS controller operational
407 * To bring UFS host controller to operational state,
408 * 1. Enable required interrupts
409 * 2. Configure interrupt aggregation
410 * 3. Program UTRL and UTMRL base address
411 * 4. Configure run-stop-registers
414 static int ufshcd_make_hba_operational(struct ufs_hba *hba)
419 /* Enable required interrupts */
420 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
422 /* Disable interrupt aggregation */
423 ufshcd_disable_intr_aggr(hba);
425 /* Configure UTRL and UTMRL base address registers */
426 ufshcd_writel(hba, lower_32_bits((dma_addr_t)hba->utrdl),
427 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
428 ufshcd_writel(hba, upper_32_bits((dma_addr_t)hba->utrdl),
429 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
430 ufshcd_writel(hba, lower_32_bits((dma_addr_t)hba->utmrdl),
431 REG_UTP_TASK_REQ_LIST_BASE_L);
432 ufshcd_writel(hba, upper_32_bits((dma_addr_t)hba->utmrdl),
433 REG_UTP_TASK_REQ_LIST_BASE_H);
436 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
438 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
439 if (!(ufshcd_get_lists_status(reg))) {
440 ufshcd_enable_run_stop_reg(hba);
443 "Host controller not ready to process requests\n");
453 * ufshcd_link_startup - Initialize unipro link startup
455 static int ufshcd_link_startup(struct ufs_hba *hba)
458 int retries = DME_LINKSTARTUP_RETRIES;
459 bool link_startup_again = true;
463 ufshcd_ops_link_startup_notify(hba, PRE_CHANGE);
465 ret = ufshcd_dme_link_startup(hba);
467 /* check if device is detected by inter-connect layer */
468 if (!ret && !ufshcd_is_device_present(hba)) {
469 dev_err(hba->dev, "%s: Device not present\n", __func__);
475 * DME link lost indication is only received when link is up,
476 * but we can't be sure if the link is up until link startup
477 * succeeds. So reset the local Uni-Pro and try again.
479 if (ret && ufshcd_hba_enable(hba))
481 } while (ret && retries--);
484 /* failed to get the link up... retire */
487 if (link_startup_again) {
488 link_startup_again = false;
489 retries = DME_LINKSTARTUP_RETRIES;
493 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
494 ufshcd_init_pwr_info(hba);
496 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
497 ret = ufshcd_disable_device_tx_lcc(hba);
502 /* Include any host controller configuration via UIC commands */
503 ret = ufshcd_ops_link_startup_notify(hba, POST_CHANGE);
507 ret = ufshcd_make_hba_operational(hba);
510 dev_err(hba->dev, "link startup failed %d\n", ret);
516 * ufshcd_hba_stop - Send controller to reset state
518 static inline void ufshcd_hba_stop(struct ufs_hba *hba)
522 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
523 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
524 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
527 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
531 * ufshcd_is_hba_active - Get controller state
533 static inline bool ufshcd_is_hba_active(struct ufs_hba *hba)
535 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & CONTROLLER_ENABLE)
540 * ufshcd_hba_start - Start controller initialization sequence
542 static inline void ufshcd_hba_start(struct ufs_hba *hba)
544 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
548 * ufshcd_hba_enable - initialize the controller
550 static int ufshcd_hba_enable(struct ufs_hba *hba)
554 if (!ufshcd_is_hba_active(hba))
555 /* change controller state to "reset state" */
556 ufshcd_hba_stop(hba);
558 ufshcd_ops_hce_enable_notify(hba, PRE_CHANGE);
560 /* start controller initialization sequence */
561 ufshcd_hba_start(hba);
564 * To initialize a UFS host controller HCE bit must be set to 1.
565 * During initialization the HCE bit value changes from 1->0->1.
566 * When the host controller completes initialization sequence
567 * it sets the value of HCE bit to 1. The same HCE bit is read back
568 * to check if the controller has completed initialization sequence.
569 * So without this delay the value HCE = 1, set in the previous
570 * instruction might be read back.
571 * This delay can be changed based on the controller.
575 /* wait for the host controller to complete initialization */
577 while (ufshcd_is_hba_active(hba)) {
581 dev_err(hba->dev, "Controller enable failed\n");
587 /* enable UIC related interrupts */
588 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
590 ufshcd_ops_hce_enable_notify(hba, POST_CHANGE);
596 * ufshcd_host_memory_configure - configure local reference block with
599 static void ufshcd_host_memory_configure(struct ufs_hba *hba)
601 struct utp_transfer_req_desc *utrdlp;
602 dma_addr_t cmd_desc_dma_addr;
607 cmd_desc_dma_addr = (dma_addr_t)hba->ucdl;
609 utrdlp->command_desc_base_addr_lo =
610 cpu_to_le32(lower_32_bits(cmd_desc_dma_addr));
611 utrdlp->command_desc_base_addr_hi =
612 cpu_to_le32(upper_32_bits(cmd_desc_dma_addr));
614 response_offset = offsetof(struct utp_transfer_cmd_desc, response_upiu);
615 prdt_offset = offsetof(struct utp_transfer_cmd_desc, prd_table);
617 utrdlp->response_upiu_offset = cpu_to_le16(response_offset >> 2);
618 utrdlp->prd_table_offset = cpu_to_le16(prdt_offset >> 2);
619 utrdlp->response_upiu_length = cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
621 hba->ucd_req_ptr = (struct utp_upiu_req *)hba->ucdl;
623 (struct utp_upiu_rsp *)&hba->ucdl->response_upiu;
625 (struct ufshcd_sg_entry *)&hba->ucdl->prd_table;
629 * ufshcd_memory_alloc - allocate memory for host memory space data structures
631 static int ufshcd_memory_alloc(struct ufs_hba *hba)
633 /* Allocate one Transfer Request Descriptor
634 * Should be aligned to 1k boundary.
636 hba->utrdl = memalign(1024, sizeof(struct utp_transfer_req_desc));
638 dev_err(hba->dev, "Transfer Descriptor memory allocation failed\n");
642 /* Allocate one Command Descriptor
643 * Should be aligned to 1k boundary.
645 hba->ucdl = memalign(1024, sizeof(struct utp_transfer_cmd_desc));
647 dev_err(hba->dev, "Command descriptor memory allocation failed\n");
655 * ufshcd_get_intr_mask - Get the interrupt bit mask
657 static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
661 switch (hba->version) {
662 case UFSHCI_VERSION_10:
663 intr_mask = INTERRUPT_MASK_ALL_VER_10;
665 case UFSHCI_VERSION_11:
666 case UFSHCI_VERSION_20:
667 intr_mask = INTERRUPT_MASK_ALL_VER_11;
669 case UFSHCI_VERSION_21:
671 intr_mask = INTERRUPT_MASK_ALL_VER_21;
679 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
681 static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
683 return ufshcd_readl(hba, REG_UFS_VERSION);
687 * ufshcd_get_upmcrs - Get the power mode change request status
689 static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
691 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
695 * ufshcd_cache_flush_and_invalidate - Flush and invalidate cache
697 * Flush and invalidate cache in aligned address..address+size range.
698 * The invalidation is in place to avoid stale data in cache.
700 static void ufshcd_cache_flush_and_invalidate(void *addr, unsigned long size)
702 uintptr_t aaddr = (uintptr_t)addr & ~(ARCH_DMA_MINALIGN - 1);
703 unsigned long asize = ALIGN(size, ARCH_DMA_MINALIGN);
705 flush_dcache_range(aaddr, aaddr + asize);
706 invalidate_dcache_range(aaddr, aaddr + asize);
710 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
711 * descriptor according to request
713 static void ufshcd_prepare_req_desc_hdr(struct ufs_hba *hba,
715 enum dma_data_direction cmd_dir)
717 struct utp_transfer_req_desc *req_desc = hba->utrdl;
721 if (cmd_dir == DMA_FROM_DEVICE) {
722 data_direction = UTP_DEVICE_TO_HOST;
723 *upiu_flags = UPIU_CMD_FLAGS_READ;
724 } else if (cmd_dir == DMA_TO_DEVICE) {
725 data_direction = UTP_HOST_TO_DEVICE;
726 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
728 data_direction = UTP_NO_DATA_TRANSFER;
729 *upiu_flags = UPIU_CMD_FLAGS_NONE;
732 dword_0 = data_direction | (0x1 << UPIU_COMMAND_TYPE_OFFSET);
734 /* Enable Interrupt for command */
735 dword_0 |= UTP_REQ_DESC_INT_CMD;
737 /* Transfer request descriptor header fields */
738 req_desc->header.dword_0 = cpu_to_le32(dword_0);
739 /* dword_1 is reserved, hence it is set to 0 */
740 req_desc->header.dword_1 = 0;
742 * assigning invalid value for command status. Controller
743 * updates OCS on command completion, with the command
746 req_desc->header.dword_2 =
747 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
748 /* dword_3 is reserved, hence it is set to 0 */
749 req_desc->header.dword_3 = 0;
751 req_desc->prd_table_length = 0;
753 ufshcd_cache_flush_and_invalidate(req_desc, sizeof(*req_desc));
756 static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
759 struct utp_upiu_req *ucd_req_ptr = hba->ucd_req_ptr;
760 struct ufs_query *query = &hba->dev_cmd.query;
761 u16 len = be16_to_cpu(query->request.upiu_req.length);
763 /* Query request header */
764 ucd_req_ptr->header.dword_0 =
765 UPIU_HEADER_DWORD(UPIU_TRANSACTION_QUERY_REQ,
766 upiu_flags, 0, TASK_TAG);
767 ucd_req_ptr->header.dword_1 =
768 UPIU_HEADER_DWORD(0, query->request.query_func,
771 /* Data segment length only need for WRITE_DESC */
772 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
773 ucd_req_ptr->header.dword_2 =
774 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
776 ucd_req_ptr->header.dword_2 = 0;
778 /* Copy the Query Request buffer as is */
779 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req, QUERY_OSF_SIZE);
781 /* Copy the Descriptor */
782 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) {
783 memcpy(ucd_req_ptr + 1, query->descriptor, len);
784 ufshcd_cache_flush_and_invalidate(ucd_req_ptr, 2 * sizeof(*ucd_req_ptr));
786 ufshcd_cache_flush_and_invalidate(ucd_req_ptr, sizeof(*ucd_req_ptr));
789 memset(hba->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
790 ufshcd_cache_flush_and_invalidate(hba->ucd_rsp_ptr, sizeof(*hba->ucd_rsp_ptr));
793 static inline void ufshcd_prepare_utp_nop_upiu(struct ufs_hba *hba)
795 struct utp_upiu_req *ucd_req_ptr = hba->ucd_req_ptr;
797 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
799 /* command descriptor fields */
800 ucd_req_ptr->header.dword_0 =
801 UPIU_HEADER_DWORD(UPIU_TRANSACTION_NOP_OUT, 0, 0, TASK_TAG);
802 /* clear rest of the fields of basic header */
803 ucd_req_ptr->header.dword_1 = 0;
804 ucd_req_ptr->header.dword_2 = 0;
806 memset(hba->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
808 ufshcd_cache_flush_and_invalidate(ucd_req_ptr, sizeof(*ucd_req_ptr));
809 ufshcd_cache_flush_and_invalidate(hba->ucd_rsp_ptr, sizeof(*hba->ucd_rsp_ptr));
813 * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU)
814 * for Device Management Purposes
816 static int ufshcd_comp_devman_upiu(struct ufs_hba *hba,
817 enum dev_cmd_type cmd_type)
822 hba->dev_cmd.type = cmd_type;
824 ufshcd_prepare_req_desc_hdr(hba, &upiu_flags, DMA_NONE);
826 case DEV_CMD_TYPE_QUERY:
827 ufshcd_prepare_utp_query_req_upiu(hba, upiu_flags);
829 case DEV_CMD_TYPE_NOP:
830 ufshcd_prepare_utp_nop_upiu(hba);
839 static int ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
843 u32 enabled_intr_status;
845 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
847 start = get_timer(0);
849 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
850 enabled_intr_status = intr_status & hba->intr_mask;
851 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
853 if (get_timer(start) > QUERY_REQ_TIMEOUT) {
855 "Timedout waiting for UTP response\n");
860 if (enabled_intr_status & UFSHCD_ERROR_MASK) {
861 dev_err(hba->dev, "Error in status:%08x\n",
862 enabled_intr_status);
866 } while (!(enabled_intr_status & UTP_TRANSFER_REQ_COMPL));
872 * ufshcd_get_req_rsp - returns the TR response transaction type
874 static inline int ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
876 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
880 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
883 static inline int ufshcd_get_tr_ocs(struct ufs_hba *hba)
885 struct utp_transfer_req_desc *req_desc = hba->utrdl;
887 return le32_to_cpu(req_desc->header.dword_2) & MASK_OCS;
890 static inline int ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
892 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
895 static int ufshcd_check_query_response(struct ufs_hba *hba)
897 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
899 /* Get the UPIU response */
900 query_res->response = ufshcd_get_rsp_upiu_result(hba->ucd_rsp_ptr) >>
901 UPIU_RSP_CODE_OFFSET;
902 return query_res->response;
906 * ufshcd_copy_query_response() - Copy the Query Response and the data
909 static int ufshcd_copy_query_response(struct ufs_hba *hba)
911 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
913 memcpy(&query_res->upiu_res, &hba->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
915 /* Get the descriptor */
916 if (hba->dev_cmd.query.descriptor &&
917 hba->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
918 u8 *descp = (u8 *)hba->ucd_rsp_ptr +
919 GENERAL_UPIU_REQUEST_SIZE;
923 /* data segment length */
924 resp_len = be32_to_cpu(hba->ucd_rsp_ptr->header.dword_2) &
925 MASK_QUERY_DATA_SEG_LEN;
927 be16_to_cpu(hba->dev_cmd.query.request.upiu_req.length);
928 if (likely(buf_len >= resp_len)) {
929 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
932 "%s: Response size is bigger than buffer\n",
942 * ufshcd_exec_dev_cmd - API for sending device management requests
944 static int ufshcd_exec_dev_cmd(struct ufs_hba *hba, enum dev_cmd_type cmd_type,
950 err = ufshcd_comp_devman_upiu(hba, cmd_type);
954 err = ufshcd_send_command(hba, TASK_TAG);
958 err = ufshcd_get_tr_ocs(hba);
960 dev_err(hba->dev, "Error in OCS:%d\n", err);
964 resp = ufshcd_get_req_rsp(hba->ucd_rsp_ptr);
966 case UPIU_TRANSACTION_NOP_IN:
968 case UPIU_TRANSACTION_QUERY_RSP:
969 err = ufshcd_check_query_response(hba);
971 err = ufshcd_copy_query_response(hba);
973 case UPIU_TRANSACTION_REJECT_UPIU:
974 /* TODO: handle Reject UPIU Response */
976 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
981 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
989 * ufshcd_init_query() - init the query response and request parameters
991 static inline void ufshcd_init_query(struct ufs_hba *hba,
992 struct ufs_query_req **request,
993 struct ufs_query_res **response,
994 enum query_opcode opcode,
995 u8 idn, u8 index, u8 selector)
997 *request = &hba->dev_cmd.query.request;
998 *response = &hba->dev_cmd.query.response;
999 memset(*request, 0, sizeof(struct ufs_query_req));
1000 memset(*response, 0, sizeof(struct ufs_query_res));
1001 (*request)->upiu_req.opcode = opcode;
1002 (*request)->upiu_req.idn = idn;
1003 (*request)->upiu_req.index = index;
1004 (*request)->upiu_req.selector = selector;
1008 * ufshcd_query_flag() - API function for sending flag query requests
1010 int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
1011 enum flag_idn idn, bool *flag_res)
1013 struct ufs_query_req *request = NULL;
1014 struct ufs_query_res *response = NULL;
1015 int err, index = 0, selector = 0;
1016 int timeout = QUERY_REQ_TIMEOUT;
1018 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1022 case UPIU_QUERY_OPCODE_SET_FLAG:
1023 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
1024 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
1025 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1027 case UPIU_QUERY_OPCODE_READ_FLAG:
1028 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1030 /* No dummy reads */
1031 dev_err(hba->dev, "%s: Invalid argument for read request\n",
1039 "%s: Expected query flag opcode but got = %d\n",
1045 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
1049 "%s: Sending flag query for idn %d failed, err = %d\n",
1050 __func__, idn, err);
1055 *flag_res = (be32_to_cpu(response->upiu_res.value) &
1056 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
1062 static int ufshcd_query_flag_retry(struct ufs_hba *hba,
1063 enum query_opcode opcode,
1064 enum flag_idn idn, bool *flag_res)
1069 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
1070 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
1073 "%s: failed with error %d, retries %d\n",
1074 __func__, ret, retries);
1081 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
1082 __func__, opcode, idn, ret, retries);
1086 static int __ufshcd_query_descriptor(struct ufs_hba *hba,
1087 enum query_opcode opcode,
1088 enum desc_idn idn, u8 index, u8 selector,
1089 u8 *desc_buf, int *buf_len)
1091 struct ufs_query_req *request = NULL;
1092 struct ufs_query_res *response = NULL;
1096 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
1102 if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
1103 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
1104 __func__, *buf_len);
1109 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1111 hba->dev_cmd.query.descriptor = desc_buf;
1112 request->upiu_req.length = cpu_to_be16(*buf_len);
1115 case UPIU_QUERY_OPCODE_WRITE_DESC:
1116 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1118 case UPIU_QUERY_OPCODE_READ_DESC:
1119 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1122 dev_err(hba->dev, "%s: Expected query descriptor opcode but got = 0x%.2x\n",
1128 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
1131 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
1132 __func__, opcode, idn, index, err);
1136 hba->dev_cmd.query.descriptor = NULL;
1137 *buf_len = be16_to_cpu(response->upiu_res.length);
1144 * ufshcd_query_descriptor_retry - API function for sending descriptor requests
1146 int ufshcd_query_descriptor_retry(struct ufs_hba *hba, enum query_opcode opcode,
1147 enum desc_idn idn, u8 index, u8 selector,
1148 u8 *desc_buf, int *buf_len)
1153 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
1154 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
1155 selector, desc_buf, buf_len);
1156 if (!err || err == -EINVAL)
1164 * ufshcd_read_desc_length - read the specified descriptor length from header
1166 static int ufshcd_read_desc_length(struct ufs_hba *hba, enum desc_idn desc_id,
1167 int desc_index, int *desc_length)
1170 u8 header[QUERY_DESC_HDR_SIZE];
1171 int header_len = QUERY_DESC_HDR_SIZE;
1173 if (desc_id >= QUERY_DESC_IDN_MAX)
1176 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
1177 desc_id, desc_index, 0, header,
1181 dev_err(hba->dev, "%s: Failed to get descriptor header id %d\n",
1184 } else if (desc_id != header[QUERY_DESC_DESC_TYPE_OFFSET]) {
1185 dev_warn(hba->dev, "%s: descriptor header id %d and desc_id %d mismatch\n",
1186 __func__, header[QUERY_DESC_DESC_TYPE_OFFSET],
1191 *desc_length = header[QUERY_DESC_LENGTH_OFFSET];
1196 static void ufshcd_init_desc_sizes(struct ufs_hba *hba)
1200 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_DEVICE, 0,
1201 &hba->desc_size.dev_desc);
1203 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
1205 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_POWER, 0,
1206 &hba->desc_size.pwr_desc);
1208 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
1210 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_INTERCONNECT, 0,
1211 &hba->desc_size.interc_desc);
1213 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
1215 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_CONFIGURATION, 0,
1216 &hba->desc_size.conf_desc);
1218 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
1220 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_UNIT, 0,
1221 &hba->desc_size.unit_desc);
1223 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
1225 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_GEOMETRY, 0,
1226 &hba->desc_size.geom_desc);
1228 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
1230 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_HEALTH, 0,
1231 &hba->desc_size.hlth_desc);
1233 hba->desc_size.hlth_desc = QUERY_DESC_HEALTH_DEF_SIZE;
1237 * ufshcd_map_desc_id_to_length - map descriptor IDN to its length
1240 int ufshcd_map_desc_id_to_length(struct ufs_hba *hba, enum desc_idn desc_id,
1244 case QUERY_DESC_IDN_DEVICE:
1245 *desc_len = hba->desc_size.dev_desc;
1247 case QUERY_DESC_IDN_POWER:
1248 *desc_len = hba->desc_size.pwr_desc;
1250 case QUERY_DESC_IDN_GEOMETRY:
1251 *desc_len = hba->desc_size.geom_desc;
1253 case QUERY_DESC_IDN_CONFIGURATION:
1254 *desc_len = hba->desc_size.conf_desc;
1256 case QUERY_DESC_IDN_UNIT:
1257 *desc_len = hba->desc_size.unit_desc;
1259 case QUERY_DESC_IDN_INTERCONNECT:
1260 *desc_len = hba->desc_size.interc_desc;
1262 case QUERY_DESC_IDN_STRING:
1263 *desc_len = QUERY_DESC_MAX_SIZE;
1265 case QUERY_DESC_IDN_HEALTH:
1266 *desc_len = hba->desc_size.hlth_desc;
1268 case QUERY_DESC_IDN_RFU_0:
1269 case QUERY_DESC_IDN_RFU_1:
1278 EXPORT_SYMBOL(ufshcd_map_desc_id_to_length);
1281 * ufshcd_read_desc_param - read the specified descriptor parameter
1284 int ufshcd_read_desc_param(struct ufs_hba *hba, enum desc_idn desc_id,
1285 int desc_index, u8 param_offset, u8 *param_read_buf,
1291 bool is_kmalloc = true;
1294 if (desc_id >= QUERY_DESC_IDN_MAX || !param_size)
1297 /* Get the max length of descriptor from structure filled up at probe
1300 ret = ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len);
1303 if (ret || !buff_len) {
1304 dev_err(hba->dev, "%s: Failed to get full descriptor length\n",
1309 /* Check whether we need temp memory */
1310 if (param_offset != 0 || param_size < buff_len) {
1311 desc_buf = kmalloc(buff_len, GFP_KERNEL);
1315 desc_buf = param_read_buf;
1319 /* Request for full descriptor */
1320 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
1321 desc_id, desc_index, 0, desc_buf,
1325 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d\n",
1326 __func__, desc_id, desc_index, param_offset, ret);
1331 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
1332 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header\n",
1333 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
1338 /* Check wherher we will not copy more data, than available */
1339 if (is_kmalloc && param_size > buff_len)
1340 param_size = buff_len;
1343 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
1350 /* replace non-printable or non-ASCII characters with spaces */
1351 static inline void ufshcd_remove_non_printable(uint8_t *val)
1356 if (*val < 0x20 || *val > 0x7e)
1361 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
1362 * state) and waits for it to take effect.
1365 static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
1367 unsigned long start = 0;
1371 ret = ufshcd_send_uic_cmd(hba, cmd);
1374 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
1375 cmd->command, cmd->argument3, ret);
1380 start = get_timer(0);
1382 status = ufshcd_get_upmcrs(hba);
1383 if (get_timer(start) > UFS_UIC_CMD_TIMEOUT) {
1385 "pwr ctrl cmd 0x%x failed, host upmcrs:0x%x\n",
1386 cmd->command, status);
1387 ret = (status != PWR_OK) ? status : -1;
1390 } while (status != PWR_LOCAL);
1396 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode change
1397 * using DME_SET primitives.
1399 static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
1401 struct uic_command uic_cmd = {0};
1404 uic_cmd.command = UIC_CMD_DME_SET;
1405 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
1406 uic_cmd.argument3 = mode;
1407 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
1413 void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufs_hba *hba,
1414 struct scsi_cmd *pccb, u32 upiu_flags)
1416 struct utp_upiu_req *ucd_req_ptr = hba->ucd_req_ptr;
1417 unsigned int cdb_len;
1419 /* command descriptor fields */
1420 ucd_req_ptr->header.dword_0 =
1421 UPIU_HEADER_DWORD(UPIU_TRANSACTION_COMMAND, upiu_flags,
1422 pccb->lun, TASK_TAG);
1423 ucd_req_ptr->header.dword_1 =
1424 UPIU_HEADER_DWORD(UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
1426 /* Total EHS length and Data segment length will be zero */
1427 ucd_req_ptr->header.dword_2 = 0;
1429 ucd_req_ptr->sc.exp_data_transfer_len = cpu_to_be32(pccb->datalen);
1431 cdb_len = min_t(unsigned short, pccb->cmdlen, UFS_CDB_SIZE);
1432 memset(ucd_req_ptr->sc.cdb, 0, UFS_CDB_SIZE);
1433 memcpy(ucd_req_ptr->sc.cdb, pccb->cmd, cdb_len);
1435 memset(hba->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
1436 ufshcd_cache_flush_and_invalidate(ucd_req_ptr, sizeof(*ucd_req_ptr));
1437 ufshcd_cache_flush_and_invalidate(hba->ucd_rsp_ptr, sizeof(*hba->ucd_rsp_ptr));
1440 static inline void prepare_prdt_desc(struct ufshcd_sg_entry *entry,
1441 unsigned char *buf, ulong len)
1443 entry->size = cpu_to_le32(len) | GENMASK(1, 0);
1444 entry->base_addr = cpu_to_le32(lower_32_bits((unsigned long)buf));
1445 entry->upper_addr = cpu_to_le32(upper_32_bits((unsigned long)buf));
1448 static void prepare_prdt_table(struct ufs_hba *hba, struct scsi_cmd *pccb)
1450 struct utp_transfer_req_desc *req_desc = hba->utrdl;
1451 struct ufshcd_sg_entry *prd_table = hba->ucd_prdt_ptr;
1452 uintptr_t aaddr = (uintptr_t)(pccb->pdata) & ~(ARCH_DMA_MINALIGN - 1);
1453 ulong datalen = pccb->datalen;
1459 req_desc->prd_table_length = 0;
1460 ufshcd_cache_flush_and_invalidate(req_desc, sizeof(*req_desc));
1464 if (pccb->dma_dir == DMA_TO_DEVICE) { /* Write to device */
1465 flush_dcache_range(aaddr, aaddr +
1466 ALIGN(datalen, ARCH_DMA_MINALIGN));
1469 /* In any case, invalidate cache to avoid stale data in it. */
1470 invalidate_dcache_range(aaddr, aaddr +
1471 ALIGN(datalen, ARCH_DMA_MINALIGN));
1473 table_length = DIV_ROUND_UP(pccb->datalen, MAX_PRDT_ENTRY);
1477 prepare_prdt_desc(&prd_table[table_length - i - 1], buf,
1478 MAX_PRDT_ENTRY - 1);
1479 buf += MAX_PRDT_ENTRY;
1480 datalen -= MAX_PRDT_ENTRY;
1483 prepare_prdt_desc(&prd_table[table_length - i - 1], buf, datalen - 1);
1485 req_desc->prd_table_length = table_length;
1486 ufshcd_cache_flush_and_invalidate(prd_table, sizeof(*prd_table) * table_length);
1487 ufshcd_cache_flush_and_invalidate(req_desc, sizeof(*req_desc));
1490 static int ufs_scsi_exec(struct udevice *scsi_dev, struct scsi_cmd *pccb)
1492 struct ufs_hba *hba = dev_get_uclass_priv(scsi_dev->parent);
1494 int ocs, result = 0;
1497 ufshcd_prepare_req_desc_hdr(hba, &upiu_flags, pccb->dma_dir);
1498 ufshcd_prepare_utp_scsi_cmd_upiu(hba, pccb, upiu_flags);
1499 prepare_prdt_table(hba, pccb);
1501 ufshcd_send_command(hba, TASK_TAG);
1503 ocs = ufshcd_get_tr_ocs(hba);
1506 result = ufshcd_get_req_rsp(hba->ucd_rsp_ptr);
1508 case UPIU_TRANSACTION_RESPONSE:
1509 result = ufshcd_get_rsp_upiu_result(hba->ucd_rsp_ptr);
1511 scsi_status = result & MASK_SCSI_STATUS;
1516 case UPIU_TRANSACTION_REJECT_UPIU:
1517 /* TODO: handle Reject UPIU Response */
1519 "Reject UPIU not fully implemented\n");
1523 "Unexpected request response code = %x\n",
1529 dev_err(hba->dev, "OCS error from controller = %x\n", ocs);
1536 static inline int ufshcd_read_desc(struct ufs_hba *hba, enum desc_idn desc_id,
1537 int desc_index, u8 *buf, u32 size)
1539 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
1542 static int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size)
1544 return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size);
1548 * ufshcd_read_string_desc - read string descriptor
1551 int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index,
1552 u8 *buf, u32 size, bool ascii)
1556 err = ufshcd_read_desc(hba, QUERY_DESC_IDN_STRING, desc_index, buf,
1560 dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n",
1561 __func__, QUERY_REQ_RETRIES, err);
1572 /* remove header and divide by 2 to move from UTF16 to UTF8 */
1573 ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1;
1574 if (size < ascii_len + QUERY_DESC_HDR_SIZE) {
1575 dev_err(hba->dev, "%s: buffer allocated size is too small\n",
1581 buff_ascii = kmalloc(ascii_len, GFP_KERNEL);
1588 * the descriptor contains string in UTF16 format
1589 * we need to convert to utf-8 so it can be displayed
1591 utf16_to_utf8(buff_ascii,
1592 (uint16_t *)&buf[QUERY_DESC_HDR_SIZE], ascii_len);
1594 /* replace non-printable or non-ASCII characters with spaces */
1595 for (i = 0; i < ascii_len; i++)
1596 ufshcd_remove_non_printable(&buff_ascii[i]);
1598 memset(buf + QUERY_DESC_HDR_SIZE, 0,
1599 size - QUERY_DESC_HDR_SIZE);
1600 memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len);
1601 buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE;
1608 static int ufs_get_device_desc(struct ufs_hba *hba,
1609 struct ufs_dev_desc *dev_desc)
1616 buff_len = max_t(size_t, hba->desc_size.dev_desc,
1617 QUERY_DESC_MAX_SIZE + 1);
1618 desc_buf = kmalloc(buff_len, GFP_KERNEL);
1624 err = ufshcd_read_device_desc(hba, desc_buf, hba->desc_size.dev_desc);
1626 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
1632 * getting vendor (manufacturerID) and Bank Index in big endian
1635 dev_desc->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
1636 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
1638 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
1640 /* Zero-pad entire buffer for string termination. */
1641 memset(desc_buf, 0, buff_len);
1643 err = ufshcd_read_string_desc(hba, model_index, desc_buf,
1644 QUERY_DESC_MAX_SIZE, true/*ASCII*/);
1646 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
1651 desc_buf[QUERY_DESC_MAX_SIZE] = '\0';
1652 strlcpy(dev_desc->model, (char *)(desc_buf + QUERY_DESC_HDR_SIZE),
1653 min_t(u8, desc_buf[QUERY_DESC_LENGTH_OFFSET],
1656 /* Null terminate the model string */
1657 dev_desc->model[MAX_MODEL_LEN] = '\0';
1665 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
1667 static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
1669 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
1671 if (hba->max_pwr_info.is_valid)
1674 if (hba->quirks & UFSHCD_QUIRK_HIBERN_FASTAUTO) {
1675 pwr_info->pwr_tx = FASTAUTO_MODE;
1676 pwr_info->pwr_rx = FASTAUTO_MODE;
1678 pwr_info->pwr_tx = FAST_MODE;
1679 pwr_info->pwr_rx = FAST_MODE;
1681 pwr_info->hs_rate = PA_HS_MODE_B;
1683 /* Get the connected lane count */
1684 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
1685 &pwr_info->lane_rx);
1686 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
1687 &pwr_info->lane_tx);
1689 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
1690 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
1691 __func__, pwr_info->lane_rx, pwr_info->lane_tx);
1696 * First, get the maximum gears of HS speed.
1697 * If a zero value, it means there is no HSGEAR capability.
1698 * Then, get the maximum gears of PWM speed.
1700 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
1701 if (!pwr_info->gear_rx) {
1702 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
1703 &pwr_info->gear_rx);
1704 if (!pwr_info->gear_rx) {
1705 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
1706 __func__, pwr_info->gear_rx);
1709 pwr_info->pwr_rx = SLOW_MODE;
1712 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
1713 &pwr_info->gear_tx);
1714 if (!pwr_info->gear_tx) {
1715 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
1716 &pwr_info->gear_tx);
1717 if (!pwr_info->gear_tx) {
1718 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
1719 __func__, pwr_info->gear_tx);
1722 pwr_info->pwr_tx = SLOW_MODE;
1725 hba->max_pwr_info.is_valid = true;
1729 static int ufshcd_change_power_mode(struct ufs_hba *hba,
1730 struct ufs_pa_layer_attr *pwr_mode)
1734 /* if already configured to the requested pwr_mode */
1735 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
1736 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
1737 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
1738 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
1739 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
1740 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
1741 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
1742 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
1747 * Configure attributes for power mode change with below.
1748 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
1749 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
1752 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
1753 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
1755 if (pwr_mode->pwr_rx == FASTAUTO_MODE || pwr_mode->pwr_rx == FAST_MODE)
1756 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
1758 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
1760 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
1761 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
1763 if (pwr_mode->pwr_tx == FASTAUTO_MODE || pwr_mode->pwr_tx == FAST_MODE)
1764 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
1766 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
1768 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
1769 pwr_mode->pwr_tx == FASTAUTO_MODE ||
1770 pwr_mode->pwr_rx == FAST_MODE ||
1771 pwr_mode->pwr_tx == FAST_MODE)
1772 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
1775 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4 |
1780 "%s: power mode change failed %d\n", __func__, ret);
1785 /* Copy new Power Mode to power info */
1786 memcpy(&hba->pwr_info, pwr_mode, sizeof(struct ufs_pa_layer_attr));
1792 * ufshcd_verify_dev_init() - Verify device initialization
1795 static int ufshcd_verify_dev_init(struct ufs_hba *hba)
1800 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
1801 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
1803 if (!err || err == -ETIMEDOUT)
1806 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
1810 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
1816 * ufshcd_complete_dev_init() - checks device readiness
1818 static int ufshcd_complete_dev_init(struct ufs_hba *hba)
1824 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
1825 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
1828 "%s setting fDeviceInit flag failed with error %d\n",
1833 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
1834 for (i = 0; i < 1000 && !err && flag_res; i++)
1835 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
1836 QUERY_FLAG_IDN_FDEVICEINIT,
1841 "%s reading fDeviceInit flag failed with error %d\n",
1845 "%s fDeviceInit was not cleared by the device\n",
1852 static void ufshcd_def_desc_sizes(struct ufs_hba *hba)
1854 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
1855 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
1856 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
1857 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
1858 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
1859 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
1860 hba->desc_size.hlth_desc = QUERY_DESC_HEALTH_DEF_SIZE;
1863 int ufs_start(struct ufs_hba *hba)
1865 struct ufs_dev_desc card = {0};
1868 ret = ufshcd_link_startup(hba);
1872 ret = ufshcd_verify_dev_init(hba);
1876 ret = ufshcd_complete_dev_init(hba);
1880 /* Init check for device descriptor sizes */
1881 ufshcd_init_desc_sizes(hba);
1883 ret = ufs_get_device_desc(hba, &card);
1885 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
1891 if (ufshcd_get_max_pwr_mode(hba)) {
1893 "%s: Failed getting max supported power mode\n",
1896 ret = ufshcd_change_power_mode(hba, &hba->max_pwr_info.info);
1898 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
1904 printf("Device at %s up at:", hba->dev->name);
1905 ufshcd_print_pwr_info(hba);
1911 int ufshcd_probe(struct udevice *ufs_dev, struct ufs_hba_ops *hba_ops)
1913 struct ufs_hba *hba = dev_get_uclass_priv(ufs_dev);
1914 struct scsi_plat *scsi_plat;
1915 struct udevice *scsi_dev;
1916 void __iomem *mmio_base;
1919 device_find_first_child(ufs_dev, &scsi_dev);
1923 scsi_plat = dev_get_uclass_plat(scsi_dev);
1924 scsi_plat->max_id = UFSHCD_MAX_ID;
1925 scsi_plat->max_lun = UFS_MAX_LUNS;
1926 scsi_plat->max_bytes_per_req = UFS_MAX_BYTES;
1931 if (device_is_on_pci_bus(ufs_dev)) {
1932 mmio_base = dm_pci_map_bar(ufs_dev, PCI_BASE_ADDRESS_0, 0, 0,
1933 PCI_REGION_TYPE, PCI_REGION_MEM);
1935 mmio_base = dev_read_addr_ptr(ufs_dev);
1937 hba->mmio_base = mmio_base;
1939 /* Set descriptor lengths to specification defaults */
1940 ufshcd_def_desc_sizes(hba);
1942 ufshcd_ops_init(hba);
1944 /* Read capabilties registers */
1945 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
1946 if (hba->quirks & UFSHCD_QUIRK_BROKEN_64BIT_ADDRESS)
1947 hba->capabilities &= ~MASK_64_ADDRESSING_SUPPORT;
1949 /* Get UFS version supported by the controller */
1950 hba->version = ufshcd_get_ufs_version(hba);
1951 if (hba->version != UFSHCI_VERSION_10 &&
1952 hba->version != UFSHCI_VERSION_11 &&
1953 hba->version != UFSHCI_VERSION_20 &&
1954 hba->version != UFSHCI_VERSION_21 &&
1955 hba->version != UFSHCI_VERSION_30 &&
1956 hba->version != UFSHCI_VERSION_31)
1957 dev_err(hba->dev, "invalid UFS version 0x%x\n",
1960 /* Get Interrupt bit mask per version */
1961 hba->intr_mask = ufshcd_get_intr_mask(hba);
1963 /* Allocate memory for host memory space */
1964 err = ufshcd_memory_alloc(hba);
1966 dev_err(hba->dev, "Memory allocation failed\n");
1970 /* Configure Local data structures */
1971 ufshcd_host_memory_configure(hba);
1974 * In order to avoid any spurious interrupt immediately after
1975 * registering UFS controller interrupt handler, clear any pending UFS
1976 * interrupt status and disable all the UFS interrupts.
1978 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
1979 REG_INTERRUPT_STATUS);
1980 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
1982 err = ufshcd_hba_enable(hba);
1984 dev_err(hba->dev, "Host controller enable failed\n");
1988 err = ufs_start(hba);
1995 int ufs_scsi_bind(struct udevice *ufs_dev, struct udevice **scsi_devp)
1997 int ret = device_bind_driver(ufs_dev, "ufs_scsi", "ufs_scsi",
2003 #if IS_ENABLED(CONFIG_BOUNCE_BUFFER)
2004 static int ufs_scsi_buffer_aligned(struct udevice *scsi_dev, struct bounce_buffer *state)
2006 #ifdef CONFIG_PHYS_64BIT
2007 struct ufs_hba *hba = dev_get_uclass_priv(scsi_dev->parent);
2008 uintptr_t ubuf = (uintptr_t)state->user_buffer;
2009 size_t len = state->len_aligned;
2011 /* Check if below 32bit boundary */
2012 if ((hba->quirks & UFSHCD_QUIRK_BROKEN_64BIT_ADDRESS) &&
2013 ((ubuf >> 32) || (ubuf + len) >> 32)) {
2014 dev_dbg(scsi_dev, "Buffer above 32bit boundary %lx-%lx\n",
2021 #endif /* CONFIG_BOUNCE_BUFFER */
2023 static struct scsi_ops ufs_ops = {
2024 .exec = ufs_scsi_exec,
2025 #if IS_ENABLED(CONFIG_BOUNCE_BUFFER)
2026 .buffer_aligned = ufs_scsi_buffer_aligned,
2027 #endif /* CONFIG_BOUNCE_BUFFER */
2030 int ufs_probe_dev(int index)
2032 struct udevice *dev;
2034 return uclass_get_device(UCLASS_UFS, index, &dev);
2039 struct udevice *dev;
2043 ret = uclass_get_device(UCLASS_UFS, i, &dev);
2051 U_BOOT_DRIVER(ufs_scsi) = {