blob: f7d8c40c448c6865b7c04e03bab6f3f93a44a591 [file] [log] [blame]
Faiz Abbas5cc51072019-10-15 18:24:36 +05301// SPDX-License-Identifier: GPL-2.0+
2/**
Bin Meng3816bea2023-10-11 21:15:44 +08003 * ufs.c - Universal Flash Storage (UFS) driver
Faiz Abbas5cc51072019-10-15 18:24:36 +05304 *
5 * Taken from Linux Kernel v5.2 (drivers/scsi/ufs/ufshcd.c) and ported
6 * to u-boot.
7 *
Nishanth Menoneaa39c62023-11-01 15:56:03 -05008 * Copyright (C) 2019 Texas Instruments Incorporated - https://www.ti.com
Faiz Abbas5cc51072019-10-15 18:24:36 +05309 */
10
Marek Vasut12ec15e2023-08-16 17:05:50 +020011#include <bouncebuf.h>
Faiz Abbas5cc51072019-10-15 18:24:36 +053012#include <charset.h>
Faiz Abbas5cc51072019-10-15 18:24:36 +053013#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070016#include <dm/devres.h>
Faiz Abbas5cc51072019-10-15 18:24:36 +053017#include <dm/lists.h>
18#include <dm/device-internal.h>
19#include <malloc.h>
20#include <hexdump.h>
21#include <scsi.h>
Simon Glass0b700eb2020-07-19 10:15:54 -060022#include <asm/io.h>
23#include <asm/dma-mapping.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060024#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060025#include <linux/delay.h>
Masahiro Yamada6373a172020-02-14 16:40:19 +090026#include <linux/dma-mapping.h>
Faiz Abbas5cc51072019-10-15 18:24:36 +053027
28#include "ufs.h"
29
30#define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
31 UTP_TASK_REQ_COMPL |\
32 UFSHCD_ERROR_MASK)
33/* maximum number of link-startup retries */
34#define DME_LINKSTARTUP_RETRIES 3
35
36/* maximum number of retries for a general UIC command */
37#define UFS_UIC_COMMAND_RETRIES 3
38
39/* Query request retries */
40#define QUERY_REQ_RETRIES 3
41/* Query request timeout */
42#define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */
43
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 */
50
51/* Only use one Task Tag for all requests */
52#define TASK_TAG 0
53
54/* Expose the flag value from utp_upiu_query.value */
55#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
56
57#define MAX_PRDT_ENTRY 262144
58
59/* maximum bytes per request */
60#define UFS_MAX_BYTES (128 * 256 * 1024)
61
62static inline bool ufshcd_is_hba_active(struct ufs_hba *hba);
63static inline void ufshcd_hba_stop(struct ufs_hba *hba);
64static int ufshcd_hba_enable(struct ufs_hba *hba);
65
66/*
67 * ufshcd_wait_for_register - wait for register value to change
68 */
69static int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
70 u32 val, unsigned long timeout_ms)
71{
72 int err = 0;
73 unsigned long start = get_timer(0);
74
75 /* ignore bits that we don't intend to wait on */
76 val = val & mask;
77
78 while ((ufshcd_readl(hba, reg) & mask) != val) {
79 if (get_timer(start) > timeout_ms) {
80 if ((ufshcd_readl(hba, reg) & mask) != val)
81 err = -ETIMEDOUT;
82 break;
83 }
84 }
85
86 return err;
87}
88
89/**
90 * ufshcd_init_pwr_info - setting the POR (power on reset)
91 * values in hba power info
92 */
93static void ufshcd_init_pwr_info(struct ufs_hba *hba)
94{
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;
102}
103
104/**
105 * ufshcd_print_pwr_info - print power params as saved in hba
106 * power info
107 */
108static void ufshcd_print_pwr_info(struct ufs_hba *hba)
109{
110 static const char * const names[] = {
111 "INVALID MODE",
112 "FAST MODE",
113 "SLOW_MODE",
114 "INVALID MODE",
115 "FASTAUTO_MODE",
116 "SLOWAUTO_MODE",
117 "INVALID MODE",
118 };
119
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);
126}
127
Neil Armstrong5168a8b2024-09-10 11:50:10 +0200128static void ufshcd_device_reset(struct ufs_hba *hba)
129{
130 ufshcd_vops_device_reset(hba);
131}
132
Faiz Abbas5cc51072019-10-15 18:24:36 +0530133/**
134 * ufshcd_ready_for_uic_cmd - Check if controller is ready
135 * to accept UIC commands
136 */
137static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
138{
139 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
140 return true;
141 else
142 return false;
143}
144
145/**
146 * ufshcd_get_uic_cmd_result - Get the UIC command result
147 */
148static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
149{
150 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
151 MASK_UIC_COMMAND_RESULT;
152}
153
154/**
155 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
156 */
157static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
158{
159 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
160}
161
162/**
163 * ufshcd_is_device_present - Check if any device connected to
164 * the host controller
165 */
166static inline bool ufshcd_is_device_present(struct ufs_hba *hba)
167{
168 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
169 DEVICE_PRESENT) ? true : false;
170}
171
172/**
173 * ufshcd_send_uic_cmd - UFS Interconnect layer command API
174 *
175 */
176static int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
177{
178 unsigned long start = 0;
179 u32 intr_status;
180 u32 enabled_intr_status;
181
182 if (!ufshcd_ready_for_uic_cmd(hba)) {
183 dev_err(hba->dev,
184 "Controller not ready to accept UIC commands\n");
185 return -EIO;
186 }
187
188 debug("sending uic command:%d\n", uic_cmd->command);
189
190 /* Write Args */
191 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
192 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
193 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
194
195 /* Write UIC Cmd */
196 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
197 REG_UIC_COMMAND);
198
199 start = get_timer(0);
200 do {
201 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
202 enabled_intr_status = intr_status & hba->intr_mask;
203 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
204
205 if (get_timer(start) > UFS_UIC_CMD_TIMEOUT) {
206 dev_err(hba->dev,
207 "Timedout waiting for UIC response\n");
208
209 return -ETIMEDOUT;
210 }
211
212 if (enabled_intr_status & UFSHCD_ERROR_MASK) {
213 dev_err(hba->dev, "Error in status:%08x\n",
214 enabled_intr_status);
215
216 return -1;
217 }
218 } while (!(enabled_intr_status & UFSHCD_UIC_MASK));
219
220 uic_cmd->argument2 = ufshcd_get_uic_cmd_result(hba);
221 uic_cmd->argument3 = ufshcd_get_dme_attr_val(hba);
222
223 debug("Sent successfully\n");
224
225 return 0;
226}
227
228/**
229 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
230 *
231 */
232int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel, u8 attr_set,
233 u32 mib_val, u8 peer)
234{
235 struct uic_command uic_cmd = {0};
236 static const char *const action[] = {
237 "dme-set",
238 "dme-peer-set"
239 };
240 const char *set = action[!!peer];
241 int ret;
242 int retries = UFS_UIC_COMMAND_RETRIES;
243
244 uic_cmd.command = peer ?
245 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
246 uic_cmd.argument1 = attr_sel;
247 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
248 uic_cmd.argument3 = mib_val;
249
250 do {
251 /* for peer attributes we retry upon failure */
252 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
253 if (ret)
254 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
255 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
256 } while (ret && peer && --retries);
257
258 if (ret)
259 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
260 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
261 UFS_UIC_COMMAND_RETRIES - retries);
262
263 return ret;
264}
265
266/**
267 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
268 *
269 */
270int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
271 u32 *mib_val, u8 peer)
272{
273 struct uic_command uic_cmd = {0};
274 static const char *const action[] = {
275 "dme-get",
276 "dme-peer-get"
277 };
278 const char *get = action[!!peer];
279 int ret;
280 int retries = UFS_UIC_COMMAND_RETRIES;
281
282 uic_cmd.command = peer ?
283 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
284 uic_cmd.argument1 = attr_sel;
285
286 do {
287 /* for peer attributes we retry upon failure */
288 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
289 if (ret)
290 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
291 get, UIC_GET_ATTR_ID(attr_sel), ret);
292 } while (ret && peer && --retries);
293
294 if (ret)
295 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
296 get, UIC_GET_ATTR_ID(attr_sel),
297 UFS_UIC_COMMAND_RETRIES - retries);
298
299 if (mib_val && !ret)
300 *mib_val = uic_cmd.argument3;
301
302 return ret;
303}
304
305static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
306{
307 u32 tx_lanes, i, err = 0;
308
309 if (!peer)
310 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
311 &tx_lanes);
312 else
313 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
314 &tx_lanes);
315 for (i = 0; i < tx_lanes; i++) {
316 if (!peer)
317 err = ufshcd_dme_set(hba,
318 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
319 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
320 0);
321 else
322 err = ufshcd_dme_peer_set(hba,
323 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
324 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
325 0);
326 if (err) {
Bin Meng618eb6a2023-10-11 21:15:45 +0800327 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d\n",
Faiz Abbas5cc51072019-10-15 18:24:36 +0530328 __func__, peer, i, err);
329 break;
330 }
331 }
332
333 return err;
334}
335
336static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
337{
338 return ufshcd_disable_tx_lcc(hba, true);
339}
340
341/**
342 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
343 *
344 */
345static int ufshcd_dme_link_startup(struct ufs_hba *hba)
346{
347 struct uic_command uic_cmd = {0};
348 int ret;
349
350 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
351
352 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
353 if (ret)
354 dev_dbg(hba->dev,
355 "dme-link-startup: error code %d\n", ret);
356 return ret;
357}
358
359/**
360 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
361 *
362 */
363static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
364{
365 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
366}
367
368/**
369 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
370 */
371static inline int ufshcd_get_lists_status(u32 reg)
372{
373 return !((reg & UFSHCD_STATUS_READY) == UFSHCD_STATUS_READY);
374}
375
376/**
377 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
378 * When run-stop registers are set to 1, it indicates the
379 * host controller that it can process the requests
380 */
381static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
382{
383 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
384 REG_UTP_TASK_REQ_LIST_RUN_STOP);
385 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
386 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
387}
388
389/**
390 * ufshcd_enable_intr - enable interrupts
391 */
392static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
393{
394 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
395 u32 rw;
396
397 if (hba->version == UFSHCI_VERSION_10) {
398 rw = set & INTERRUPT_MASK_RW_VER_10;
399 set = rw | ((set ^ intrs) & intrs);
400 } else {
401 set |= intrs;
402 }
403
404 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
405
406 hba->intr_mask = set;
407}
408
409/**
410 * ufshcd_make_hba_operational - Make UFS controller operational
411 *
412 * To bring UFS host controller to operational state,
413 * 1. Enable required interrupts
414 * 2. Configure interrupt aggregation
415 * 3. Program UTRL and UTMRL base address
416 * 4. Configure run-stop-registers
417 *
418 */
419static int ufshcd_make_hba_operational(struct ufs_hba *hba)
420{
421 int err = 0;
422 u32 reg;
423
424 /* Enable required interrupts */
425 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
426
427 /* Disable interrupt aggregation */
428 ufshcd_disable_intr_aggr(hba);
429
430 /* Configure UTRL and UTMRL base address registers */
431 ufshcd_writel(hba, lower_32_bits((dma_addr_t)hba->utrdl),
432 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
433 ufshcd_writel(hba, upper_32_bits((dma_addr_t)hba->utrdl),
434 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
435 ufshcd_writel(hba, lower_32_bits((dma_addr_t)hba->utmrdl),
436 REG_UTP_TASK_REQ_LIST_BASE_L);
437 ufshcd_writel(hba, upper_32_bits((dma_addr_t)hba->utmrdl),
438 REG_UTP_TASK_REQ_LIST_BASE_H);
439
440 /*
Bhupesh Sharmafdd3d842024-09-30 14:44:32 +0200441 * Make sure base address and interrupt setup are updated before
442 * enabling the run/stop registers below.
443 */
444 wmb();
445
446 /*
Faiz Abbas5cc51072019-10-15 18:24:36 +0530447 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
448 */
449 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
450 if (!(ufshcd_get_lists_status(reg))) {
451 ufshcd_enable_run_stop_reg(hba);
452 } else {
453 dev_err(hba->dev,
Bin Meng618eb6a2023-10-11 21:15:45 +0800454 "Host controller not ready to process requests\n");
Faiz Abbas5cc51072019-10-15 18:24:36 +0530455 err = -EIO;
456 goto out;
457 }
458
459out:
460 return err;
461}
462
463/**
464 * ufshcd_link_startup - Initialize unipro link startup
465 */
466static int ufshcd_link_startup(struct ufs_hba *hba)
467{
468 int ret;
469 int retries = DME_LINKSTARTUP_RETRIES;
Faiz Abbas5cc51072019-10-15 18:24:36 +0530470
Faiz Abbas5cc51072019-10-15 18:24:36 +0530471 do {
472 ufshcd_ops_link_startup_notify(hba, PRE_CHANGE);
473
474 ret = ufshcd_dme_link_startup(hba);
475
476 /* check if device is detected by inter-connect layer */
477 if (!ret && !ufshcd_is_device_present(hba)) {
478 dev_err(hba->dev, "%s: Device not present\n", __func__);
479 ret = -ENXIO;
480 goto out;
481 }
482
483 /*
484 * DME link lost indication is only received when link is up,
485 * but we can't be sure if the link is up until link startup
486 * succeeds. So reset the local Uni-Pro and try again.
487 */
488 if (ret && ufshcd_hba_enable(hba))
489 goto out;
490 } while (ret && retries--);
491
492 if (ret)
493 /* failed to get the link up... retire */
494 goto out;
495
Faiz Abbas5cc51072019-10-15 18:24:36 +0530496 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
497 ufshcd_init_pwr_info(hba);
498
499 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
500 ret = ufshcd_disable_device_tx_lcc(hba);
501 if (ret)
502 goto out;
503 }
504
505 /* Include any host controller configuration via UIC commands */
506 ret = ufshcd_ops_link_startup_notify(hba, POST_CHANGE);
507 if (ret)
508 goto out;
509
Bhupesh Sharma9f952302024-09-30 14:44:30 +0200510 /* Clear UECPA once due to LINERESET has happened during LINK_STARTUP */
511 ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
Faiz Abbas5cc51072019-10-15 18:24:36 +0530512 ret = ufshcd_make_hba_operational(hba);
513out:
514 if (ret)
515 dev_err(hba->dev, "link startup failed %d\n", ret);
516
517 return ret;
518}
519
520/**
521 * ufshcd_hba_stop - Send controller to reset state
522 */
523static inline void ufshcd_hba_stop(struct ufs_hba *hba)
524{
525 int err;
526
527 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
528 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
529 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
530 10);
531 if (err)
532 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
533}
534
535/**
536 * ufshcd_is_hba_active - Get controller state
537 */
538static inline bool ufshcd_is_hba_active(struct ufs_hba *hba)
539{
540 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & CONTROLLER_ENABLE)
541 ? false : true;
542}
543
544/**
545 * ufshcd_hba_start - Start controller initialization sequence
546 */
547static inline void ufshcd_hba_start(struct ufs_hba *hba)
548{
549 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
550}
551
552/**
553 * ufshcd_hba_enable - initialize the controller
554 */
555static int ufshcd_hba_enable(struct ufs_hba *hba)
556{
557 int retry;
558
559 if (!ufshcd_is_hba_active(hba))
560 /* change controller state to "reset state" */
561 ufshcd_hba_stop(hba);
562
563 ufshcd_ops_hce_enable_notify(hba, PRE_CHANGE);
564
565 /* start controller initialization sequence */
566 ufshcd_hba_start(hba);
567
568 /*
569 * To initialize a UFS host controller HCE bit must be set to 1.
570 * During initialization the HCE bit value changes from 1->0->1.
571 * When the host controller completes initialization sequence
572 * it sets the value of HCE bit to 1. The same HCE bit is read back
573 * to check if the controller has completed initialization sequence.
574 * So without this delay the value HCE = 1, set in the previous
575 * instruction might be read back.
576 * This delay can be changed based on the controller.
577 */
578 mdelay(1);
579
580 /* wait for the host controller to complete initialization */
581 retry = 10;
582 while (ufshcd_is_hba_active(hba)) {
583 if (retry) {
584 retry--;
585 } else {
586 dev_err(hba->dev, "Controller enable failed\n");
587 return -EIO;
588 }
589 mdelay(5);
590 }
591
592 /* enable UIC related interrupts */
593 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
594
595 ufshcd_ops_hce_enable_notify(hba, POST_CHANGE);
596
597 return 0;
598}
599
600/**
601 * ufshcd_host_memory_configure - configure local reference block with
602 * memory offsets
603 */
604static void ufshcd_host_memory_configure(struct ufs_hba *hba)
605{
606 struct utp_transfer_req_desc *utrdlp;
607 dma_addr_t cmd_desc_dma_addr;
608 u16 response_offset;
609 u16 prdt_offset;
610
611 utrdlp = hba->utrdl;
612 cmd_desc_dma_addr = (dma_addr_t)hba->ucdl;
613
614 utrdlp->command_desc_base_addr_lo =
615 cpu_to_le32(lower_32_bits(cmd_desc_dma_addr));
616 utrdlp->command_desc_base_addr_hi =
617 cpu_to_le32(upper_32_bits(cmd_desc_dma_addr));
618
619 response_offset = offsetof(struct utp_transfer_cmd_desc, response_upiu);
620 prdt_offset = offsetof(struct utp_transfer_cmd_desc, prd_table);
621
622 utrdlp->response_upiu_offset = cpu_to_le16(response_offset >> 2);
623 utrdlp->prd_table_offset = cpu_to_le16(prdt_offset >> 2);
624 utrdlp->response_upiu_length = cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
625
626 hba->ucd_req_ptr = (struct utp_upiu_req *)hba->ucdl;
627 hba->ucd_rsp_ptr =
628 (struct utp_upiu_rsp *)&hba->ucdl->response_upiu;
629 hba->ucd_prdt_ptr =
630 (struct ufshcd_sg_entry *)&hba->ucdl->prd_table;
631}
632
633/**
634 * ufshcd_memory_alloc - allocate memory for host memory space data structures
635 */
636static int ufshcd_memory_alloc(struct ufs_hba *hba)
637{
638 /* Allocate one Transfer Request Descriptor
639 * Should be aligned to 1k boundary.
640 */
Neil Armstrongceb2bf42024-09-30 14:44:23 +0200641 hba->utrdl = memalign(1024,
642 ALIGN(sizeof(struct utp_transfer_req_desc),
643 ARCH_DMA_MINALIGN));
Faiz Abbas5cc51072019-10-15 18:24:36 +0530644 if (!hba->utrdl) {
645 dev_err(hba->dev, "Transfer Descriptor memory allocation failed\n");
646 return -ENOMEM;
647 }
648
649 /* Allocate one Command Descriptor
650 * Should be aligned to 1k boundary.
651 */
Neil Armstrongceb2bf42024-09-30 14:44:23 +0200652 hba->ucdl = memalign(1024,
653 ALIGN(sizeof(struct utp_transfer_cmd_desc),
654 ARCH_DMA_MINALIGN));
Faiz Abbas5cc51072019-10-15 18:24:36 +0530655 if (!hba->ucdl) {
656 dev_err(hba->dev, "Command descriptor memory allocation failed\n");
657 return -ENOMEM;
658 }
659
660 return 0;
661}
662
663/**
664 * ufshcd_get_intr_mask - Get the interrupt bit mask
665 */
666static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
667{
668 u32 intr_mask = 0;
669
670 switch (hba->version) {
671 case UFSHCI_VERSION_10:
672 intr_mask = INTERRUPT_MASK_ALL_VER_10;
673 break;
674 case UFSHCI_VERSION_11:
675 case UFSHCI_VERSION_20:
676 intr_mask = INTERRUPT_MASK_ALL_VER_11;
677 break;
678 case UFSHCI_VERSION_21:
679 default:
680 intr_mask = INTERRUPT_MASK_ALL_VER_21;
681 break;
682 }
683
684 return intr_mask;
685}
686
687/**
688 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
689 */
690static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
691{
692 return ufshcd_readl(hba, REG_UFS_VERSION);
693}
694
695/**
696 * ufshcd_get_upmcrs - Get the power mode change request status
697 */
698static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
699{
700 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
701}
702
703/**
Neil Armstrongead42192024-09-30 14:44:25 +0200704 * ufshcd_cache_flush - Flush cache
Marek Vasut8426fe82023-08-16 17:05:55 +0200705 *
Neil Armstrongead42192024-09-30 14:44:25 +0200706 * Flush cache in aligned address..address+size range.
Marek Vasut8426fe82023-08-16 17:05:55 +0200707 */
Neil Armstrongead42192024-09-30 14:44:25 +0200708static void ufshcd_cache_flush(void *addr, unsigned long size)
Marek Vasut8426fe82023-08-16 17:05:55 +0200709{
Neil Armstronga9b880f2024-09-30 14:44:24 +0200710 uintptr_t start_addr = (uintptr_t)addr & ~(ARCH_DMA_MINALIGN - 1);
711 uintptr_t end_addr = ALIGN((uintptr_t)addr + size, ARCH_DMA_MINALIGN);
Marek Vasut8426fe82023-08-16 17:05:55 +0200712
Neil Armstronga9b880f2024-09-30 14:44:24 +0200713 flush_dcache_range(start_addr, end_addr);
Neil Armstrongead42192024-09-30 14:44:25 +0200714}
715
716/**
717 * ufshcd_cache_invalidate - Invalidate cache
718 *
719 * Invalidate cache in aligned address..address+size range.
720 */
721static void ufshcd_cache_invalidate(void *addr, unsigned long size)
722{
723 uintptr_t start_addr = (uintptr_t)addr & ~(ARCH_DMA_MINALIGN - 1);
724 uintptr_t end_addr = ALIGN((uintptr_t)addr + size, ARCH_DMA_MINALIGN);
725
Neil Armstronga9b880f2024-09-30 14:44:24 +0200726 invalidate_dcache_range(start_addr, end_addr);
Marek Vasut8426fe82023-08-16 17:05:55 +0200727}
728
729/**
Faiz Abbas5cc51072019-10-15 18:24:36 +0530730 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
731 * descriptor according to request
732 */
Marek Vasutbc3786f2023-08-16 17:05:53 +0200733static void ufshcd_prepare_req_desc_hdr(struct ufs_hba *hba,
Faiz Abbas5cc51072019-10-15 18:24:36 +0530734 u32 *upiu_flags,
735 enum dma_data_direction cmd_dir)
736{
Marek Vasutbc3786f2023-08-16 17:05:53 +0200737 struct utp_transfer_req_desc *req_desc = hba->utrdl;
Faiz Abbas5cc51072019-10-15 18:24:36 +0530738 u32 data_direction;
739 u32 dword_0;
740
741 if (cmd_dir == DMA_FROM_DEVICE) {
742 data_direction = UTP_DEVICE_TO_HOST;
743 *upiu_flags = UPIU_CMD_FLAGS_READ;
744 } else if (cmd_dir == DMA_TO_DEVICE) {
745 data_direction = UTP_HOST_TO_DEVICE;
746 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
747 } else {
748 data_direction = UTP_NO_DATA_TRANSFER;
749 *upiu_flags = UPIU_CMD_FLAGS_NONE;
750 }
751
752 dword_0 = data_direction | (0x1 << UPIU_COMMAND_TYPE_OFFSET);
753
754 /* Enable Interrupt for command */
755 dword_0 |= UTP_REQ_DESC_INT_CMD;
756
757 /* Transfer request descriptor header fields */
758 req_desc->header.dword_0 = cpu_to_le32(dword_0);
759 /* dword_1 is reserved, hence it is set to 0 */
760 req_desc->header.dword_1 = 0;
761 /*
762 * assigning invalid value for command status. Controller
763 * updates OCS on command completion, with the command
764 * status
765 */
766 req_desc->header.dword_2 =
767 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
768 /* dword_3 is reserved, hence it is set to 0 */
769 req_desc->header.dword_3 = 0;
770
771 req_desc->prd_table_length = 0;
Marek Vasut8426fe82023-08-16 17:05:55 +0200772
Neil Armstrongead42192024-09-30 14:44:25 +0200773 ufshcd_cache_flush(req_desc, sizeof(*req_desc));
Faiz Abbas5cc51072019-10-15 18:24:36 +0530774}
775
776static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
777 u32 upiu_flags)
778{
779 struct utp_upiu_req *ucd_req_ptr = hba->ucd_req_ptr;
780 struct ufs_query *query = &hba->dev_cmd.query;
781 u16 len = be16_to_cpu(query->request.upiu_req.length);
782
783 /* Query request header */
784 ucd_req_ptr->header.dword_0 =
785 UPIU_HEADER_DWORD(UPIU_TRANSACTION_QUERY_REQ,
786 upiu_flags, 0, TASK_TAG);
787 ucd_req_ptr->header.dword_1 =
788 UPIU_HEADER_DWORD(0, query->request.query_func,
789 0, 0);
790
791 /* Data segment length only need for WRITE_DESC */
792 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
793 ucd_req_ptr->header.dword_2 =
794 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
795 else
796 ucd_req_ptr->header.dword_2 = 0;
797
798 /* Copy the Query Request buffer as is */
799 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req, QUERY_OSF_SIZE);
800
801 /* Copy the Descriptor */
Marek Vasut8426fe82023-08-16 17:05:55 +0200802 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) {
Faiz Abbas5cc51072019-10-15 18:24:36 +0530803 memcpy(ucd_req_ptr + 1, query->descriptor, len);
Neil Armstrongead42192024-09-30 14:44:25 +0200804 ufshcd_cache_flush(ucd_req_ptr, 2 * sizeof(*ucd_req_ptr));
Marek Vasut8426fe82023-08-16 17:05:55 +0200805 } else {
Neil Armstrongead42192024-09-30 14:44:25 +0200806 ufshcd_cache_flush(ucd_req_ptr, sizeof(*ucd_req_ptr));
Marek Vasut8426fe82023-08-16 17:05:55 +0200807 }
Faiz Abbas5cc51072019-10-15 18:24:36 +0530808
809 memset(hba->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Neil Armstrongead42192024-09-30 14:44:25 +0200810 ufshcd_cache_flush(hba->ucd_rsp_ptr, sizeof(*hba->ucd_rsp_ptr));
Faiz Abbas5cc51072019-10-15 18:24:36 +0530811}
812
813static inline void ufshcd_prepare_utp_nop_upiu(struct ufs_hba *hba)
814{
815 struct utp_upiu_req *ucd_req_ptr = hba->ucd_req_ptr;
816
817 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
818
819 /* command descriptor fields */
820 ucd_req_ptr->header.dword_0 =
Bhupesh Sharmafa10fb22023-07-03 00:39:12 +0530821 UPIU_HEADER_DWORD(UPIU_TRANSACTION_NOP_OUT, 0, 0, TASK_TAG);
Faiz Abbas5cc51072019-10-15 18:24:36 +0530822 /* clear rest of the fields of basic header */
823 ucd_req_ptr->header.dword_1 = 0;
824 ucd_req_ptr->header.dword_2 = 0;
825
826 memset(hba->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Marek Vasut8426fe82023-08-16 17:05:55 +0200827
Neil Armstrongead42192024-09-30 14:44:25 +0200828 ufshcd_cache_flush(ucd_req_ptr, sizeof(*ucd_req_ptr));
829 ufshcd_cache_flush(hba->ucd_rsp_ptr, sizeof(*hba->ucd_rsp_ptr));
Faiz Abbas5cc51072019-10-15 18:24:36 +0530830}
831
832/**
833 * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU)
834 * for Device Management Purposes
835 */
836static int ufshcd_comp_devman_upiu(struct ufs_hba *hba,
837 enum dev_cmd_type cmd_type)
838{
839 u32 upiu_flags;
840 int ret = 0;
Faiz Abbas5cc51072019-10-15 18:24:36 +0530841
842 hba->dev_cmd.type = cmd_type;
843
Marek Vasutbc3786f2023-08-16 17:05:53 +0200844 ufshcd_prepare_req_desc_hdr(hba, &upiu_flags, DMA_NONE);
Faiz Abbas5cc51072019-10-15 18:24:36 +0530845 switch (cmd_type) {
846 case DEV_CMD_TYPE_QUERY:
847 ufshcd_prepare_utp_query_req_upiu(hba, upiu_flags);
848 break;
849 case DEV_CMD_TYPE_NOP:
850 ufshcd_prepare_utp_nop_upiu(hba);
851 break;
852 default:
853 ret = -EINVAL;
854 }
855
856 return ret;
857}
858
859static int ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
860{
861 unsigned long start;
862 u32 intr_status;
863 u32 enabled_intr_status;
864
865 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
866
Bhupesh Sharmafdd3d842024-09-30 14:44:32 +0200867 /* Make sure doorbell reg is updated before reading interrupt status */
868 wmb();
869
Faiz Abbas5cc51072019-10-15 18:24:36 +0530870 start = get_timer(0);
871 do {
872 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
873 enabled_intr_status = intr_status & hba->intr_mask;
874 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
875
876 if (get_timer(start) > QUERY_REQ_TIMEOUT) {
877 dev_err(hba->dev,
878 "Timedout waiting for UTP response\n");
879
880 return -ETIMEDOUT;
881 }
882
883 if (enabled_intr_status & UFSHCD_ERROR_MASK) {
884 dev_err(hba->dev, "Error in status:%08x\n",
885 enabled_intr_status);
886
887 return -1;
888 }
889 } while (!(enabled_intr_status & UTP_TRANSFER_REQ_COMPL));
890
891 return 0;
892}
893
894/**
895 * ufshcd_get_req_rsp - returns the TR response transaction type
896 */
897static inline int ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
898{
Neil Armstrongead42192024-09-30 14:44:25 +0200899 ufshcd_cache_invalidate(ucd_rsp_ptr, sizeof(*ucd_rsp_ptr));
900
Faiz Abbas5cc51072019-10-15 18:24:36 +0530901 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
902}
903
904/**
905 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
906 *
907 */
908static inline int ufshcd_get_tr_ocs(struct ufs_hba *hba)
909{
Marek Vasut53c6bf32023-08-16 17:05:54 +0200910 struct utp_transfer_req_desc *req_desc = hba->utrdl;
911
Neil Armstrongead42192024-09-30 14:44:25 +0200912 ufshcd_cache_invalidate(req_desc, sizeof(*req_desc));
913
Marek Vasut53c6bf32023-08-16 17:05:54 +0200914 return le32_to_cpu(req_desc->header.dword_2) & MASK_OCS;
Faiz Abbas5cc51072019-10-15 18:24:36 +0530915}
916
917static inline int ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
918{
919 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
920}
921
922static int ufshcd_check_query_response(struct ufs_hba *hba)
923{
924 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
925
926 /* Get the UPIU response */
927 query_res->response = ufshcd_get_rsp_upiu_result(hba->ucd_rsp_ptr) >>
928 UPIU_RSP_CODE_OFFSET;
929 return query_res->response;
930}
931
932/**
933 * ufshcd_copy_query_response() - Copy the Query Response and the data
934 * descriptor
935 */
936static int ufshcd_copy_query_response(struct ufs_hba *hba)
937{
938 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
939
940 memcpy(&query_res->upiu_res, &hba->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
941
942 /* Get the descriptor */
943 if (hba->dev_cmd.query.descriptor &&
944 hba->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
945 u8 *descp = (u8 *)hba->ucd_rsp_ptr +
946 GENERAL_UPIU_REQUEST_SIZE;
947 u16 resp_len;
948 u16 buf_len;
949
950 /* data segment length */
951 resp_len = be32_to_cpu(hba->ucd_rsp_ptr->header.dword_2) &
952 MASK_QUERY_DATA_SEG_LEN;
953 buf_len =
954 be16_to_cpu(hba->dev_cmd.query.request.upiu_req.length);
955 if (likely(buf_len >= resp_len)) {
956 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
957 } else {
958 dev_warn(hba->dev,
Bin Meng618eb6a2023-10-11 21:15:45 +0800959 "%s: Response size is bigger than buffer\n",
Faiz Abbas5cc51072019-10-15 18:24:36 +0530960 __func__);
961 return -EINVAL;
962 }
963 }
964
965 return 0;
966}
967
968/**
969 * ufshcd_exec_dev_cmd - API for sending device management requests
970 */
971static int ufshcd_exec_dev_cmd(struct ufs_hba *hba, enum dev_cmd_type cmd_type,
972 int timeout)
973{
974 int err;
975 int resp;
976
977 err = ufshcd_comp_devman_upiu(hba, cmd_type);
978 if (err)
979 return err;
980
981 err = ufshcd_send_command(hba, TASK_TAG);
982 if (err)
983 return err;
984
985 err = ufshcd_get_tr_ocs(hba);
986 if (err) {
987 dev_err(hba->dev, "Error in OCS:%d\n", err);
988 return -EINVAL;
989 }
990
991 resp = ufshcd_get_req_rsp(hba->ucd_rsp_ptr);
992 switch (resp) {
993 case UPIU_TRANSACTION_NOP_IN:
994 break;
995 case UPIU_TRANSACTION_QUERY_RSP:
996 err = ufshcd_check_query_response(hba);
997 if (!err)
998 err = ufshcd_copy_query_response(hba);
999 break;
1000 case UPIU_TRANSACTION_REJECT_UPIU:
1001 /* TODO: handle Reject UPIU Response */
1002 err = -EPERM;
1003 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
1004 __func__);
1005 break;
1006 default:
1007 err = -EINVAL;
1008 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
1009 __func__, resp);
1010 }
1011
1012 return err;
1013}
1014
1015/**
1016 * ufshcd_init_query() - init the query response and request parameters
1017 */
1018static inline void ufshcd_init_query(struct ufs_hba *hba,
1019 struct ufs_query_req **request,
1020 struct ufs_query_res **response,
1021 enum query_opcode opcode,
1022 u8 idn, u8 index, u8 selector)
1023{
1024 *request = &hba->dev_cmd.query.request;
1025 *response = &hba->dev_cmd.query.response;
1026 memset(*request, 0, sizeof(struct ufs_query_req));
1027 memset(*response, 0, sizeof(struct ufs_query_res));
1028 (*request)->upiu_req.opcode = opcode;
1029 (*request)->upiu_req.idn = idn;
1030 (*request)->upiu_req.index = index;
1031 (*request)->upiu_req.selector = selector;
1032}
1033
1034/**
1035 * ufshcd_query_flag() - API function for sending flag query requests
1036 */
1037int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
1038 enum flag_idn idn, bool *flag_res)
1039{
1040 struct ufs_query_req *request = NULL;
1041 struct ufs_query_res *response = NULL;
1042 int err, index = 0, selector = 0;
1043 int timeout = QUERY_REQ_TIMEOUT;
1044
1045 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1046 selector);
1047
1048 switch (opcode) {
1049 case UPIU_QUERY_OPCODE_SET_FLAG:
1050 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
1051 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
1052 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1053 break;
1054 case UPIU_QUERY_OPCODE_READ_FLAG:
1055 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1056 if (!flag_res) {
1057 /* No dummy reads */
1058 dev_err(hba->dev, "%s: Invalid argument for read request\n",
1059 __func__);
1060 err = -EINVAL;
1061 goto out;
1062 }
1063 break;
1064 default:
1065 dev_err(hba->dev,
1066 "%s: Expected query flag opcode but got = %d\n",
1067 __func__, opcode);
1068 err = -EINVAL;
1069 goto out;
1070 }
1071
1072 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
1073
1074 if (err) {
1075 dev_err(hba->dev,
1076 "%s: Sending flag query for idn %d failed, err = %d\n",
1077 __func__, idn, err);
1078 goto out;
1079 }
1080
1081 if (flag_res)
1082 *flag_res = (be32_to_cpu(response->upiu_res.value) &
1083 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
1084
1085out:
1086 return err;
1087}
1088
1089static int ufshcd_query_flag_retry(struct ufs_hba *hba,
1090 enum query_opcode opcode,
1091 enum flag_idn idn, bool *flag_res)
1092{
1093 int ret;
1094 int retries;
1095
1096 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
1097 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
1098 if (ret)
1099 dev_dbg(hba->dev,
1100 "%s: failed with error %d, retries %d\n",
1101 __func__, ret, retries);
1102 else
1103 break;
1104 }
1105
1106 if (ret)
1107 dev_err(hba->dev,
1108 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
1109 __func__, opcode, idn, ret, retries);
1110 return ret;
1111}
1112
1113static int __ufshcd_query_descriptor(struct ufs_hba *hba,
1114 enum query_opcode opcode,
1115 enum desc_idn idn, u8 index, u8 selector,
1116 u8 *desc_buf, int *buf_len)
1117{
1118 struct ufs_query_req *request = NULL;
1119 struct ufs_query_res *response = NULL;
1120 int err;
1121
1122 if (!desc_buf) {
1123 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
1124 __func__, opcode);
1125 err = -EINVAL;
1126 goto out;
1127 }
1128
1129 if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
1130 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
1131 __func__, *buf_len);
1132 err = -EINVAL;
1133 goto out;
1134 }
1135
1136 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1137 selector);
1138 hba->dev_cmd.query.descriptor = desc_buf;
1139 request->upiu_req.length = cpu_to_be16(*buf_len);
1140
1141 switch (opcode) {
1142 case UPIU_QUERY_OPCODE_WRITE_DESC:
1143 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1144 break;
1145 case UPIU_QUERY_OPCODE_READ_DESC:
1146 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1147 break;
1148 default:
1149 dev_err(hba->dev, "%s: Expected query descriptor opcode but got = 0x%.2x\n",
1150 __func__, opcode);
1151 err = -EINVAL;
1152 goto out;
1153 }
1154
1155 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
1156
1157 if (err) {
1158 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
1159 __func__, opcode, idn, index, err);
1160 goto out;
1161 }
1162
1163 hba->dev_cmd.query.descriptor = NULL;
1164 *buf_len = be16_to_cpu(response->upiu_res.length);
1165
1166out:
1167 return err;
1168}
1169
1170/**
1171 * ufshcd_query_descriptor_retry - API function for sending descriptor requests
1172 */
1173int ufshcd_query_descriptor_retry(struct ufs_hba *hba, enum query_opcode opcode,
1174 enum desc_idn idn, u8 index, u8 selector,
1175 u8 *desc_buf, int *buf_len)
1176{
1177 int err;
1178 int retries;
1179
1180 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
1181 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
1182 selector, desc_buf, buf_len);
1183 if (!err || err == -EINVAL)
1184 break;
1185 }
1186
1187 return err;
1188}
1189
1190/**
1191 * ufshcd_read_desc_length - read the specified descriptor length from header
1192 */
1193static int ufshcd_read_desc_length(struct ufs_hba *hba, enum desc_idn desc_id,
1194 int desc_index, int *desc_length)
1195{
1196 int ret;
1197 u8 header[QUERY_DESC_HDR_SIZE];
1198 int header_len = QUERY_DESC_HDR_SIZE;
1199
1200 if (desc_id >= QUERY_DESC_IDN_MAX)
1201 return -EINVAL;
1202
1203 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
1204 desc_id, desc_index, 0, header,
1205 &header_len);
1206
1207 if (ret) {
Bin Meng618eb6a2023-10-11 21:15:45 +08001208 dev_err(hba->dev, "%s: Failed to get descriptor header id %d\n",
Faiz Abbas5cc51072019-10-15 18:24:36 +05301209 __func__, desc_id);
1210 return ret;
1211 } else if (desc_id != header[QUERY_DESC_DESC_TYPE_OFFSET]) {
Bin Meng618eb6a2023-10-11 21:15:45 +08001212 dev_warn(hba->dev, "%s: descriptor header id %d and desc_id %d mismatch\n",
Faiz Abbas5cc51072019-10-15 18:24:36 +05301213 __func__, header[QUERY_DESC_DESC_TYPE_OFFSET],
1214 desc_id);
1215 ret = -EINVAL;
1216 }
1217
1218 *desc_length = header[QUERY_DESC_LENGTH_OFFSET];
1219
1220 return ret;
1221}
1222
1223static void ufshcd_init_desc_sizes(struct ufs_hba *hba)
1224{
1225 int err;
1226
1227 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_DEVICE, 0,
1228 &hba->desc_size.dev_desc);
1229 if (err)
1230 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
1231
1232 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_POWER, 0,
1233 &hba->desc_size.pwr_desc);
1234 if (err)
1235 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
1236
1237 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_INTERCONNECT, 0,
1238 &hba->desc_size.interc_desc);
1239 if (err)
1240 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
1241
1242 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_CONFIGURATION, 0,
1243 &hba->desc_size.conf_desc);
1244 if (err)
1245 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
1246
1247 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_UNIT, 0,
1248 &hba->desc_size.unit_desc);
1249 if (err)
1250 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
1251
1252 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_GEOMETRY, 0,
1253 &hba->desc_size.geom_desc);
1254 if (err)
1255 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
1256
1257 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_HEALTH, 0,
1258 &hba->desc_size.hlth_desc);
1259 if (err)
1260 hba->desc_size.hlth_desc = QUERY_DESC_HEALTH_DEF_SIZE;
1261}
1262
1263/**
1264 * ufshcd_map_desc_id_to_length - map descriptor IDN to its length
1265 *
1266 */
1267int ufshcd_map_desc_id_to_length(struct ufs_hba *hba, enum desc_idn desc_id,
1268 int *desc_len)
1269{
1270 switch (desc_id) {
1271 case QUERY_DESC_IDN_DEVICE:
1272 *desc_len = hba->desc_size.dev_desc;
1273 break;
1274 case QUERY_DESC_IDN_POWER:
1275 *desc_len = hba->desc_size.pwr_desc;
1276 break;
1277 case QUERY_DESC_IDN_GEOMETRY:
1278 *desc_len = hba->desc_size.geom_desc;
1279 break;
1280 case QUERY_DESC_IDN_CONFIGURATION:
1281 *desc_len = hba->desc_size.conf_desc;
1282 break;
1283 case QUERY_DESC_IDN_UNIT:
1284 *desc_len = hba->desc_size.unit_desc;
1285 break;
1286 case QUERY_DESC_IDN_INTERCONNECT:
1287 *desc_len = hba->desc_size.interc_desc;
1288 break;
1289 case QUERY_DESC_IDN_STRING:
1290 *desc_len = QUERY_DESC_MAX_SIZE;
1291 break;
1292 case QUERY_DESC_IDN_HEALTH:
1293 *desc_len = hba->desc_size.hlth_desc;
1294 break;
1295 case QUERY_DESC_IDN_RFU_0:
1296 case QUERY_DESC_IDN_RFU_1:
1297 *desc_len = 0;
1298 break;
1299 default:
1300 *desc_len = 0;
1301 return -EINVAL;
1302 }
1303 return 0;
1304}
1305EXPORT_SYMBOL(ufshcd_map_desc_id_to_length);
1306
1307/**
1308 * ufshcd_read_desc_param - read the specified descriptor parameter
1309 *
1310 */
1311int ufshcd_read_desc_param(struct ufs_hba *hba, enum desc_idn desc_id,
1312 int desc_index, u8 param_offset, u8 *param_read_buf,
1313 u8 param_size)
1314{
1315 int ret;
1316 u8 *desc_buf;
1317 int buff_len;
1318 bool is_kmalloc = true;
1319
1320 /* Safety check */
1321 if (desc_id >= QUERY_DESC_IDN_MAX || !param_size)
1322 return -EINVAL;
1323
1324 /* Get the max length of descriptor from structure filled up at probe
1325 * time.
1326 */
1327 ret = ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len);
1328
1329 /* Sanity checks */
1330 if (ret || !buff_len) {
Bin Meng618eb6a2023-10-11 21:15:45 +08001331 dev_err(hba->dev, "%s: Failed to get full descriptor length\n",
Faiz Abbas5cc51072019-10-15 18:24:36 +05301332 __func__);
1333 return ret;
1334 }
1335
1336 /* Check whether we need temp memory */
1337 if (param_offset != 0 || param_size < buff_len) {
1338 desc_buf = kmalloc(buff_len, GFP_KERNEL);
1339 if (!desc_buf)
1340 return -ENOMEM;
1341 } else {
1342 desc_buf = param_read_buf;
1343 is_kmalloc = false;
1344 }
1345
1346 /* Request for full descriptor */
1347 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
1348 desc_id, desc_index, 0, desc_buf,
1349 &buff_len);
1350
1351 if (ret) {
Bin Meng618eb6a2023-10-11 21:15:45 +08001352 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d\n",
Faiz Abbas5cc51072019-10-15 18:24:36 +05301353 __func__, desc_id, desc_index, param_offset, ret);
1354 goto out;
1355 }
1356
1357 /* Sanity check */
1358 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
Bin Meng618eb6a2023-10-11 21:15:45 +08001359 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header\n",
Faiz Abbas5cc51072019-10-15 18:24:36 +05301360 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
1361 ret = -EINVAL;
1362 goto out;
1363 }
1364
1365 /* Check wherher we will not copy more data, than available */
1366 if (is_kmalloc && param_size > buff_len)
1367 param_size = buff_len;
1368
1369 if (is_kmalloc)
1370 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
1371out:
1372 if (is_kmalloc)
1373 kfree(desc_buf);
1374 return ret;
1375}
1376
1377/* replace non-printable or non-ASCII characters with spaces */
1378static inline void ufshcd_remove_non_printable(uint8_t *val)
1379{
1380 if (!val)
1381 return;
1382
1383 if (*val < 0x20 || *val > 0x7e)
1384 *val = ' ';
1385}
1386
1387/**
1388 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
1389 * state) and waits for it to take effect.
1390 *
1391 */
1392static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
1393{
1394 unsigned long start = 0;
1395 u8 status;
1396 int ret;
1397
1398 ret = ufshcd_send_uic_cmd(hba, cmd);
1399 if (ret) {
1400 dev_err(hba->dev,
1401 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
1402 cmd->command, cmd->argument3, ret);
1403
1404 return ret;
1405 }
1406
1407 start = get_timer(0);
1408 do {
1409 status = ufshcd_get_upmcrs(hba);
1410 if (get_timer(start) > UFS_UIC_CMD_TIMEOUT) {
1411 dev_err(hba->dev,
1412 "pwr ctrl cmd 0x%x failed, host upmcrs:0x%x\n",
1413 cmd->command, status);
1414 ret = (status != PWR_OK) ? status : -1;
1415 break;
1416 }
1417 } while (status != PWR_LOCAL);
1418
1419 return ret;
1420}
1421
1422/**
1423 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode change
1424 * using DME_SET primitives.
1425 */
1426static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
1427{
1428 struct uic_command uic_cmd = {0};
1429 int ret;
1430
1431 uic_cmd.command = UIC_CMD_DME_SET;
1432 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
1433 uic_cmd.argument3 = mode;
1434 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
1435
1436 return ret;
1437}
1438
1439static
1440void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufs_hba *hba,
1441 struct scsi_cmd *pccb, u32 upiu_flags)
1442{
1443 struct utp_upiu_req *ucd_req_ptr = hba->ucd_req_ptr;
1444 unsigned int cdb_len;
1445
1446 /* command descriptor fields */
1447 ucd_req_ptr->header.dword_0 =
1448 UPIU_HEADER_DWORD(UPIU_TRANSACTION_COMMAND, upiu_flags,
1449 pccb->lun, TASK_TAG);
1450 ucd_req_ptr->header.dword_1 =
1451 UPIU_HEADER_DWORD(UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
1452
1453 /* Total EHS length and Data segment length will be zero */
1454 ucd_req_ptr->header.dword_2 = 0;
1455
1456 ucd_req_ptr->sc.exp_data_transfer_len = cpu_to_be32(pccb->datalen);
1457
1458 cdb_len = min_t(unsigned short, pccb->cmdlen, UFS_CDB_SIZE);
1459 memset(ucd_req_ptr->sc.cdb, 0, UFS_CDB_SIZE);
1460 memcpy(ucd_req_ptr->sc.cdb, pccb->cmd, cdb_len);
1461
1462 memset(hba->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Neil Armstrongead42192024-09-30 14:44:25 +02001463 ufshcd_cache_flush(ucd_req_ptr, sizeof(*ucd_req_ptr));
1464 ufshcd_cache_flush(hba->ucd_rsp_ptr, sizeof(*hba->ucd_rsp_ptr));
Faiz Abbas5cc51072019-10-15 18:24:36 +05301465}
1466
1467static inline void prepare_prdt_desc(struct ufshcd_sg_entry *entry,
1468 unsigned char *buf, ulong len)
1469{
1470 entry->size = cpu_to_le32(len) | GENMASK(1, 0);
1471 entry->base_addr = cpu_to_le32(lower_32_bits((unsigned long)buf));
1472 entry->upper_addr = cpu_to_le32(upper_32_bits((unsigned long)buf));
1473}
1474
1475static void prepare_prdt_table(struct ufs_hba *hba, struct scsi_cmd *pccb)
1476{
1477 struct utp_transfer_req_desc *req_desc = hba->utrdl;
1478 struct ufshcd_sg_entry *prd_table = hba->ucd_prdt_ptr;
1479 ulong datalen = pccb->datalen;
1480 int table_length;
1481 u8 *buf;
1482 int i;
1483
1484 if (!datalen) {
1485 req_desc->prd_table_length = 0;
Neil Armstrongead42192024-09-30 14:44:25 +02001486 ufshcd_cache_flush(req_desc, sizeof(*req_desc));
Faiz Abbas5cc51072019-10-15 18:24:36 +05301487 return;
1488 }
1489
1490 table_length = DIV_ROUND_UP(pccb->datalen, MAX_PRDT_ENTRY);
1491 buf = pccb->pdata;
1492 i = table_length;
1493 while (--i) {
1494 prepare_prdt_desc(&prd_table[table_length - i - 1], buf,
1495 MAX_PRDT_ENTRY - 1);
1496 buf += MAX_PRDT_ENTRY;
1497 datalen -= MAX_PRDT_ENTRY;
1498 }
1499
1500 prepare_prdt_desc(&prd_table[table_length - i - 1], buf, datalen - 1);
1501
1502 req_desc->prd_table_length = table_length;
Neil Armstrongead42192024-09-30 14:44:25 +02001503 ufshcd_cache_flush(prd_table, sizeof(*prd_table) * table_length);
1504 ufshcd_cache_flush(req_desc, sizeof(*req_desc));
Faiz Abbas5cc51072019-10-15 18:24:36 +05301505}
1506
1507static int ufs_scsi_exec(struct udevice *scsi_dev, struct scsi_cmd *pccb)
1508{
1509 struct ufs_hba *hba = dev_get_uclass_priv(scsi_dev->parent);
Faiz Abbas5cc51072019-10-15 18:24:36 +05301510 u32 upiu_flags;
1511 int ocs, result = 0;
1512 u8 scsi_status;
1513
Marek Vasutbc3786f2023-08-16 17:05:53 +02001514 ufshcd_prepare_req_desc_hdr(hba, &upiu_flags, pccb->dma_dir);
Faiz Abbas5cc51072019-10-15 18:24:36 +05301515 ufshcd_prepare_utp_scsi_cmd_upiu(hba, pccb, upiu_flags);
1516 prepare_prdt_table(hba, pccb);
1517
Neil Armstrongc865da12024-09-30 14:44:26 +02001518 ufshcd_cache_flush(pccb->pdata, pccb->datalen);
1519
Faiz Abbas5cc51072019-10-15 18:24:36 +05301520 ufshcd_send_command(hba, TASK_TAG);
1521
Neil Armstrongc865da12024-09-30 14:44:26 +02001522 ufshcd_cache_invalidate(pccb->pdata, pccb->datalen);
1523
Faiz Abbas5cc51072019-10-15 18:24:36 +05301524 ocs = ufshcd_get_tr_ocs(hba);
1525 switch (ocs) {
1526 case OCS_SUCCESS:
1527 result = ufshcd_get_req_rsp(hba->ucd_rsp_ptr);
1528 switch (result) {
1529 case UPIU_TRANSACTION_RESPONSE:
1530 result = ufshcd_get_rsp_upiu_result(hba->ucd_rsp_ptr);
1531
1532 scsi_status = result & MASK_SCSI_STATUS;
1533 if (scsi_status)
1534 return -EINVAL;
1535
1536 break;
1537 case UPIU_TRANSACTION_REJECT_UPIU:
1538 /* TODO: handle Reject UPIU Response */
1539 dev_err(hba->dev,
1540 "Reject UPIU not fully implemented\n");
1541 return -EINVAL;
1542 default:
1543 dev_err(hba->dev,
1544 "Unexpected request response code = %x\n",
1545 result);
1546 return -EINVAL;
1547 }
1548 break;
1549 default:
1550 dev_err(hba->dev, "OCS error from controller = %x\n", ocs);
1551 return -EINVAL;
1552 }
1553
1554 return 0;
1555}
1556
1557static inline int ufshcd_read_desc(struct ufs_hba *hba, enum desc_idn desc_id,
1558 int desc_index, u8 *buf, u32 size)
1559{
1560 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
1561}
1562
1563static int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size)
1564{
1565 return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size);
1566}
1567
1568/**
1569 * ufshcd_read_string_desc - read string descriptor
1570 *
1571 */
1572int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index,
1573 u8 *buf, u32 size, bool ascii)
1574{
1575 int err = 0;
1576
1577 err = ufshcd_read_desc(hba, QUERY_DESC_IDN_STRING, desc_index, buf,
1578 size);
1579
1580 if (err) {
1581 dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n",
1582 __func__, QUERY_REQ_RETRIES, err);
1583 goto out;
1584 }
1585
1586 if (ascii) {
1587 int desc_len;
1588 int ascii_len;
1589 int i;
1590 u8 *buff_ascii;
1591
1592 desc_len = buf[0];
1593 /* remove header and divide by 2 to move from UTF16 to UTF8 */
1594 ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1;
1595 if (size < ascii_len + QUERY_DESC_HDR_SIZE) {
1596 dev_err(hba->dev, "%s: buffer allocated size is too small\n",
1597 __func__);
1598 err = -ENOMEM;
1599 goto out;
1600 }
1601
1602 buff_ascii = kmalloc(ascii_len, GFP_KERNEL);
1603 if (!buff_ascii) {
1604 err = -ENOMEM;
1605 goto out;
1606 }
1607
1608 /*
1609 * the descriptor contains string in UTF16 format
1610 * we need to convert to utf-8 so it can be displayed
1611 */
1612 utf16_to_utf8(buff_ascii,
1613 (uint16_t *)&buf[QUERY_DESC_HDR_SIZE], ascii_len);
1614
1615 /* replace non-printable or non-ASCII characters with spaces */
1616 for (i = 0; i < ascii_len; i++)
1617 ufshcd_remove_non_printable(&buff_ascii[i]);
1618
1619 memset(buf + QUERY_DESC_HDR_SIZE, 0,
1620 size - QUERY_DESC_HDR_SIZE);
1621 memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len);
1622 buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE;
1623 kfree(buff_ascii);
1624 }
1625out:
1626 return err;
1627}
1628
1629static int ufs_get_device_desc(struct ufs_hba *hba,
1630 struct ufs_dev_desc *dev_desc)
1631{
1632 int err;
1633 size_t buff_len;
1634 u8 model_index;
1635 u8 *desc_buf;
1636
1637 buff_len = max_t(size_t, hba->desc_size.dev_desc,
1638 QUERY_DESC_MAX_SIZE + 1);
1639 desc_buf = kmalloc(buff_len, GFP_KERNEL);
1640 if (!desc_buf) {
1641 err = -ENOMEM;
1642 goto out;
1643 }
1644
1645 err = ufshcd_read_device_desc(hba, desc_buf, hba->desc_size.dev_desc);
1646 if (err) {
1647 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
1648 __func__, err);
1649 goto out;
1650 }
1651
1652 /*
1653 * getting vendor (manufacturerID) and Bank Index in big endian
1654 * format
1655 */
1656 dev_desc->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
1657 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
1658
1659 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
1660
1661 /* Zero-pad entire buffer for string termination. */
1662 memset(desc_buf, 0, buff_len);
1663
1664 err = ufshcd_read_string_desc(hba, model_index, desc_buf,
1665 QUERY_DESC_MAX_SIZE, true/*ASCII*/);
1666 if (err) {
1667 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
1668 __func__, err);
1669 goto out;
1670 }
1671
1672 desc_buf[QUERY_DESC_MAX_SIZE] = '\0';
1673 strlcpy(dev_desc->model, (char *)(desc_buf + QUERY_DESC_HDR_SIZE),
1674 min_t(u8, desc_buf[QUERY_DESC_LENGTH_OFFSET],
1675 MAX_MODEL_LEN));
1676
1677 /* Null terminate the model string */
1678 dev_desc->model[MAX_MODEL_LEN] = '\0';
1679
1680out:
1681 kfree(desc_buf);
1682 return err;
1683}
1684
1685/**
1686 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
1687 */
1688static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
1689{
1690 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
1691
1692 if (hba->max_pwr_info.is_valid)
1693 return 0;
1694
Marek Vasut4020fd12023-08-16 17:05:51 +02001695 if (hba->quirks & UFSHCD_QUIRK_HIBERN_FASTAUTO) {
1696 pwr_info->pwr_tx = FASTAUTO_MODE;
1697 pwr_info->pwr_rx = FASTAUTO_MODE;
1698 } else {
1699 pwr_info->pwr_tx = FAST_MODE;
1700 pwr_info->pwr_rx = FAST_MODE;
1701 }
Faiz Abbas5cc51072019-10-15 18:24:36 +05301702 pwr_info->hs_rate = PA_HS_MODE_B;
1703
1704 /* Get the connected lane count */
1705 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
1706 &pwr_info->lane_rx);
1707 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
1708 &pwr_info->lane_tx);
1709
1710 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
1711 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
1712 __func__, pwr_info->lane_rx, pwr_info->lane_tx);
1713 return -EINVAL;
1714 }
1715
1716 /*
1717 * First, get the maximum gears of HS speed.
1718 * If a zero value, it means there is no HSGEAR capability.
1719 * Then, get the maximum gears of PWM speed.
1720 */
1721 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
1722 if (!pwr_info->gear_rx) {
1723 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
1724 &pwr_info->gear_rx);
1725 if (!pwr_info->gear_rx) {
1726 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
1727 __func__, pwr_info->gear_rx);
1728 return -EINVAL;
1729 }
1730 pwr_info->pwr_rx = SLOW_MODE;
1731 }
1732
1733 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
1734 &pwr_info->gear_tx);
1735 if (!pwr_info->gear_tx) {
1736 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
1737 &pwr_info->gear_tx);
1738 if (!pwr_info->gear_tx) {
1739 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
1740 __func__, pwr_info->gear_tx);
1741 return -EINVAL;
1742 }
1743 pwr_info->pwr_tx = SLOW_MODE;
1744 }
1745
1746 hba->max_pwr_info.is_valid = true;
Neil Armstrong8bbf6de2024-09-10 11:50:11 +02001747 return ufshcd_ops_get_max_pwr_mode(hba, &hba->max_pwr_info);
Faiz Abbas5cc51072019-10-15 18:24:36 +05301748}
1749
1750static int ufshcd_change_power_mode(struct ufs_hba *hba,
1751 struct ufs_pa_layer_attr *pwr_mode)
1752{
1753 int ret;
1754
1755 /* if already configured to the requested pwr_mode */
1756 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
1757 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
1758 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
1759 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
1760 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
1761 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
1762 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
1763 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
1764 return 0;
1765 }
1766
1767 /*
1768 * Configure attributes for power mode change with below.
1769 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
1770 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
1771 * - PA_HSSERIES
1772 */
1773 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
1774 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
1775 pwr_mode->lane_rx);
1776 if (pwr_mode->pwr_rx == FASTAUTO_MODE || pwr_mode->pwr_rx == FAST_MODE)
1777 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
1778 else
1779 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
1780
1781 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
1782 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
1783 pwr_mode->lane_tx);
1784 if (pwr_mode->pwr_tx == FASTAUTO_MODE || pwr_mode->pwr_tx == FAST_MODE)
1785 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
1786 else
1787 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
1788
1789 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
1790 pwr_mode->pwr_tx == FASTAUTO_MODE ||
1791 pwr_mode->pwr_rx == FAST_MODE ||
1792 pwr_mode->pwr_tx == FAST_MODE)
1793 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
1794 pwr_mode->hs_rate);
1795
1796 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4 |
1797 pwr_mode->pwr_tx);
1798
1799 if (ret) {
1800 dev_err(hba->dev,
1801 "%s: power mode change failed %d\n", __func__, ret);
1802
1803 return ret;
1804 }
1805
1806 /* Copy new Power Mode to power info */
1807 memcpy(&hba->pwr_info, pwr_mode, sizeof(struct ufs_pa_layer_attr));
1808
1809 return ret;
1810}
1811
1812/**
1813 * ufshcd_verify_dev_init() - Verify device initialization
1814 *
1815 */
1816static int ufshcd_verify_dev_init(struct ufs_hba *hba)
1817{
1818 int retries;
1819 int err;
1820
1821 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
1822 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
1823 NOP_OUT_TIMEOUT);
1824 if (!err || err == -ETIMEDOUT)
1825 break;
1826
1827 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
1828 }
1829
1830 if (err)
1831 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
1832
1833 return err;
1834}
1835
1836/**
1837 * ufshcd_complete_dev_init() - checks device readiness
1838 */
1839static int ufshcd_complete_dev_init(struct ufs_hba *hba)
1840{
1841 int i;
1842 int err;
1843 bool flag_res = 1;
1844
1845 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
1846 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
1847 if (err) {
1848 dev_err(hba->dev,
1849 "%s setting fDeviceInit flag failed with error %d\n",
1850 __func__, err);
1851 goto out;
1852 }
1853
1854 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
1855 for (i = 0; i < 1000 && !err && flag_res; i++)
1856 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
1857 QUERY_FLAG_IDN_FDEVICEINIT,
1858 &flag_res);
1859
1860 if (err)
1861 dev_err(hba->dev,
1862 "%s reading fDeviceInit flag failed with error %d\n",
1863 __func__, err);
1864 else if (flag_res)
1865 dev_err(hba->dev,
1866 "%s fDeviceInit was not cleared by the device\n",
1867 __func__);
1868
1869out:
1870 return err;
1871}
1872
1873static void ufshcd_def_desc_sizes(struct ufs_hba *hba)
1874{
1875 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
1876 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
1877 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
1878 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
1879 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
1880 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
1881 hba->desc_size.hlth_desc = QUERY_DESC_HEALTH_DEF_SIZE;
1882}
1883
1884int ufs_start(struct ufs_hba *hba)
1885{
1886 struct ufs_dev_desc card = {0};
1887 int ret;
1888
1889 ret = ufshcd_link_startup(hba);
1890 if (ret)
1891 return ret;
1892
1893 ret = ufshcd_verify_dev_init(hba);
1894 if (ret)
1895 return ret;
1896
1897 ret = ufshcd_complete_dev_init(hba);
1898 if (ret)
1899 return ret;
1900
1901 /* Init check for device descriptor sizes */
1902 ufshcd_init_desc_sizes(hba);
1903
1904 ret = ufs_get_device_desc(hba, &card);
1905 if (ret) {
1906 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
1907 __func__, ret);
1908
1909 return ret;
1910 }
1911
1912 if (ufshcd_get_max_pwr_mode(hba)) {
1913 dev_err(hba->dev,
1914 "%s: Failed getting max supported power mode\n",
1915 __func__);
1916 } else {
1917 ret = ufshcd_change_power_mode(hba, &hba->max_pwr_info.info);
1918 if (ret) {
1919 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
1920 __func__, ret);
1921
1922 return ret;
1923 }
1924
Bhupesh Sharmae11301a2024-09-30 14:44:33 +02001925 debug("UFS Device %s is up!\n", hba->dev->name);
Faiz Abbas5cc51072019-10-15 18:24:36 +05301926 ufshcd_print_pwr_info(hba);
1927 }
1928
1929 return 0;
1930}
1931
1932int ufshcd_probe(struct udevice *ufs_dev, struct ufs_hba_ops *hba_ops)
1933{
1934 struct ufs_hba *hba = dev_get_uclass_priv(ufs_dev);
Simon Glassb75b15b2020-12-03 16:55:23 -07001935 struct scsi_plat *scsi_plat;
Faiz Abbas5cc51072019-10-15 18:24:36 +05301936 struct udevice *scsi_dev;
Bin Meng1ac020d2023-10-11 21:15:49 +08001937 void __iomem *mmio_base;
Faiz Abbas5cc51072019-10-15 18:24:36 +05301938 int err;
1939
1940 device_find_first_child(ufs_dev, &scsi_dev);
1941 if (!scsi_dev)
1942 return -ENODEV;
1943
Simon Glass71fa5b42020-12-03 16:55:18 -07001944 scsi_plat = dev_get_uclass_plat(scsi_dev);
Faiz Abbas5cc51072019-10-15 18:24:36 +05301945 scsi_plat->max_id = UFSHCD_MAX_ID;
1946 scsi_plat->max_lun = UFS_MAX_LUNS;
1947 scsi_plat->max_bytes_per_req = UFS_MAX_BYTES;
1948
1949 hba->dev = ufs_dev;
1950 hba->ops = hba_ops;
Bin Meng1ac020d2023-10-11 21:15:49 +08001951
1952 if (device_is_on_pci_bus(ufs_dev)) {
1953 mmio_base = dm_pci_map_bar(ufs_dev, PCI_BASE_ADDRESS_0, 0, 0,
1954 PCI_REGION_TYPE, PCI_REGION_MEM);
1955 } else {
1956 mmio_base = dev_read_addr_ptr(ufs_dev);
1957 }
1958 hba->mmio_base = mmio_base;
Faiz Abbas5cc51072019-10-15 18:24:36 +05301959
1960 /* Set descriptor lengths to specification defaults */
1961 ufshcd_def_desc_sizes(hba);
1962
1963 ufshcd_ops_init(hba);
1964
1965 /* Read capabilties registers */
1966 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Marek Vasut12ec15e2023-08-16 17:05:50 +02001967 if (hba->quirks & UFSHCD_QUIRK_BROKEN_64BIT_ADDRESS)
1968 hba->capabilities &= ~MASK_64_ADDRESSING_SUPPORT;
Faiz Abbas5cc51072019-10-15 18:24:36 +05301969
1970 /* Get UFS version supported by the controller */
1971 hba->version = ufshcd_get_ufs_version(hba);
1972 if (hba->version != UFSHCI_VERSION_10 &&
1973 hba->version != UFSHCI_VERSION_11 &&
1974 hba->version != UFSHCI_VERSION_20 &&
Marek Vasut3f21c662023-08-16 17:05:52 +02001975 hba->version != UFSHCI_VERSION_21 &&
Bin Meng3a478f92023-10-11 21:15:51 +08001976 hba->version != UFSHCI_VERSION_30 &&
Neil Armstrong99e4f0a2024-09-10 11:50:12 +02001977 hba->version != UFSHCI_VERSION_31 &&
1978 hba->version != UFSHCI_VERSION_40)
Faiz Abbas5cc51072019-10-15 18:24:36 +05301979 dev_err(hba->dev, "invalid UFS version 0x%x\n",
1980 hba->version);
1981
1982 /* Get Interrupt bit mask per version */
1983 hba->intr_mask = ufshcd_get_intr_mask(hba);
1984
1985 /* Allocate memory for host memory space */
1986 err = ufshcd_memory_alloc(hba);
1987 if (err) {
1988 dev_err(hba->dev, "Memory allocation failed\n");
1989 return err;
1990 }
1991
1992 /* Configure Local data structures */
1993 ufshcd_host_memory_configure(hba);
1994
1995 /*
1996 * In order to avoid any spurious interrupt immediately after
1997 * registering UFS controller interrupt handler, clear any pending UFS
1998 * interrupt status and disable all the UFS interrupts.
1999 */
2000 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
2001 REG_INTERRUPT_STATUS);
2002 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
2003
Bhupesh Sharmafdd3d842024-09-30 14:44:32 +02002004 mb();
2005
Neil Armstrong5168a8b2024-09-10 11:50:10 +02002006 /* Reset the attached device */
2007 ufshcd_device_reset(hba);
2008
Faiz Abbas5cc51072019-10-15 18:24:36 +05302009 err = ufshcd_hba_enable(hba);
2010 if (err) {
2011 dev_err(hba->dev, "Host controller enable failed\n");
2012 return err;
2013 }
2014
2015 err = ufs_start(hba);
2016 if (err)
2017 return err;
2018
2019 return 0;
2020}
2021
2022int ufs_scsi_bind(struct udevice *ufs_dev, struct udevice **scsi_devp)
2023{
2024 int ret = device_bind_driver(ufs_dev, "ufs_scsi", "ufs_scsi",
2025 scsi_devp);
2026
2027 return ret;
2028}
2029
Marek Vasut12ec15e2023-08-16 17:05:50 +02002030#if IS_ENABLED(CONFIG_BOUNCE_BUFFER)
2031static int ufs_scsi_buffer_aligned(struct udevice *scsi_dev, struct bounce_buffer *state)
2032{
2033#ifdef CONFIG_PHYS_64BIT
2034 struct ufs_hba *hba = dev_get_uclass_priv(scsi_dev->parent);
2035 uintptr_t ubuf = (uintptr_t)state->user_buffer;
2036 size_t len = state->len_aligned;
2037
2038 /* Check if below 32bit boundary */
2039 if ((hba->quirks & UFSHCD_QUIRK_BROKEN_64BIT_ADDRESS) &&
2040 ((ubuf >> 32) || (ubuf + len) >> 32)) {
2041 dev_dbg(scsi_dev, "Buffer above 32bit boundary %lx-%lx\n",
2042 ubuf, ubuf + len);
2043 return 0;
2044 }
2045#endif
2046 return 1;
2047}
2048#endif /* CONFIG_BOUNCE_BUFFER */
2049
Faiz Abbas5cc51072019-10-15 18:24:36 +05302050static struct scsi_ops ufs_ops = {
2051 .exec = ufs_scsi_exec,
Marek Vasut12ec15e2023-08-16 17:05:50 +02002052#if IS_ENABLED(CONFIG_BOUNCE_BUFFER)
2053 .buffer_aligned = ufs_scsi_buffer_aligned,
2054#endif /* CONFIG_BOUNCE_BUFFER */
Faiz Abbas5cc51072019-10-15 18:24:36 +05302055};
2056
2057int ufs_probe_dev(int index)
2058{
2059 struct udevice *dev;
2060
2061 return uclass_get_device(UCLASS_UFS, index, &dev);
2062}
2063
2064int ufs_probe(void)
2065{
2066 struct udevice *dev;
2067 int ret, i;
2068
2069 for (i = 0;; i++) {
2070 ret = uclass_get_device(UCLASS_UFS, i, &dev);
2071 if (ret == -ENODEV)
2072 break;
2073 }
2074
2075 return 0;
2076}
2077
2078U_BOOT_DRIVER(ufs_scsi) = {
2079 .id = UCLASS_SCSI,
2080 .name = "ufs_scsi",
2081 .ops = &ufs_ops,
2082};