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