blob: d99dcdef7d0cae057914cbe7b54342f77c19a804 [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 /*
436 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
437 */
438 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
439 if (!(ufshcd_get_lists_status(reg))) {
440 ufshcd_enable_run_stop_reg(hba);
441 } else {
442 dev_err(hba->dev,
Bin Meng1b3dab22023-10-11 21:15:45 +0800443 "Host controller not ready to process requests\n");
Faiz Abbas7feafb02019-10-15 18:24:36 +0530444 err = -EIO;
445 goto out;
446 }
447
448out:
449 return err;
450}
451
452/**
453 * ufshcd_link_startup - Initialize unipro link startup
454 */
455static int ufshcd_link_startup(struct ufs_hba *hba)
456{
457 int ret;
458 int retries = DME_LINKSTARTUP_RETRIES;
459 bool link_startup_again = true;
460
461link_startup:
462 do {
463 ufshcd_ops_link_startup_notify(hba, PRE_CHANGE);
464
465 ret = ufshcd_dme_link_startup(hba);
466
467 /* check if device is detected by inter-connect layer */
468 if (!ret && !ufshcd_is_device_present(hba)) {
469 dev_err(hba->dev, "%s: Device not present\n", __func__);
470 ret = -ENXIO;
471 goto out;
472 }
473
474 /*
475 * DME link lost indication is only received when link is up,
476 * but we can't be sure if the link is up until link startup
477 * succeeds. So reset the local Uni-Pro and try again.
478 */
479 if (ret && ufshcd_hba_enable(hba))
480 goto out;
481 } while (ret && retries--);
482
483 if (ret)
484 /* failed to get the link up... retire */
485 goto out;
486
487 if (link_startup_again) {
488 link_startup_again = false;
489 retries = DME_LINKSTARTUP_RETRIES;
490 goto link_startup;
491 }
492
493 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
494 ufshcd_init_pwr_info(hba);
495
496 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
497 ret = ufshcd_disable_device_tx_lcc(hba);
498 if (ret)
499 goto out;
500 }
501
502 /* Include any host controller configuration via UIC commands */
503 ret = ufshcd_ops_link_startup_notify(hba, POST_CHANGE);
504 if (ret)
505 goto out;
506
507 ret = ufshcd_make_hba_operational(hba);
508out:
509 if (ret)
510 dev_err(hba->dev, "link startup failed %d\n", ret);
511
512 return ret;
513}
514
515/**
516 * ufshcd_hba_stop - Send controller to reset state
517 */
518static inline void ufshcd_hba_stop(struct ufs_hba *hba)
519{
520 int err;
521
522 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
523 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
524 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
525 10);
526 if (err)
527 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
528}
529
530/**
531 * ufshcd_is_hba_active - Get controller state
532 */
533static inline bool ufshcd_is_hba_active(struct ufs_hba *hba)
534{
535 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & CONTROLLER_ENABLE)
536 ? false : true;
537}
538
539/**
540 * ufshcd_hba_start - Start controller initialization sequence
541 */
542static inline void ufshcd_hba_start(struct ufs_hba *hba)
543{
544 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
545}
546
547/**
548 * ufshcd_hba_enable - initialize the controller
549 */
550static int ufshcd_hba_enable(struct ufs_hba *hba)
551{
552 int retry;
553
554 if (!ufshcd_is_hba_active(hba))
555 /* change controller state to "reset state" */
556 ufshcd_hba_stop(hba);
557
558 ufshcd_ops_hce_enable_notify(hba, PRE_CHANGE);
559
560 /* start controller initialization sequence */
561 ufshcd_hba_start(hba);
562
563 /*
564 * To initialize a UFS host controller HCE bit must be set to 1.
565 * During initialization the HCE bit value changes from 1->0->1.
566 * When the host controller completes initialization sequence
567 * it sets the value of HCE bit to 1. The same HCE bit is read back
568 * to check if the controller has completed initialization sequence.
569 * So without this delay the value HCE = 1, set in the previous
570 * instruction might be read back.
571 * This delay can be changed based on the controller.
572 */
573 mdelay(1);
574
575 /* wait for the host controller to complete initialization */
576 retry = 10;
577 while (ufshcd_is_hba_active(hba)) {
578 if (retry) {
579 retry--;
580 } else {
581 dev_err(hba->dev, "Controller enable failed\n");
582 return -EIO;
583 }
584 mdelay(5);
585 }
586
587 /* enable UIC related interrupts */
588 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
589
590 ufshcd_ops_hce_enable_notify(hba, POST_CHANGE);
591
592 return 0;
593}
594
595/**
596 * ufshcd_host_memory_configure - configure local reference block with
597 * memory offsets
598 */
599static void ufshcd_host_memory_configure(struct ufs_hba *hba)
600{
601 struct utp_transfer_req_desc *utrdlp;
602 dma_addr_t cmd_desc_dma_addr;
603 u16 response_offset;
604 u16 prdt_offset;
605
606 utrdlp = hba->utrdl;
607 cmd_desc_dma_addr = (dma_addr_t)hba->ucdl;
608
609 utrdlp->command_desc_base_addr_lo =
610 cpu_to_le32(lower_32_bits(cmd_desc_dma_addr));
611 utrdlp->command_desc_base_addr_hi =
612 cpu_to_le32(upper_32_bits(cmd_desc_dma_addr));
613
614 response_offset = offsetof(struct utp_transfer_cmd_desc, response_upiu);
615 prdt_offset = offsetof(struct utp_transfer_cmd_desc, prd_table);
616
617 utrdlp->response_upiu_offset = cpu_to_le16(response_offset >> 2);
618 utrdlp->prd_table_offset = cpu_to_le16(prdt_offset >> 2);
619 utrdlp->response_upiu_length = cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
620
621 hba->ucd_req_ptr = (struct utp_upiu_req *)hba->ucdl;
622 hba->ucd_rsp_ptr =
623 (struct utp_upiu_rsp *)&hba->ucdl->response_upiu;
624 hba->ucd_prdt_ptr =
625 (struct ufshcd_sg_entry *)&hba->ucdl->prd_table;
626}
627
628/**
629 * ufshcd_memory_alloc - allocate memory for host memory space data structures
630 */
631static int ufshcd_memory_alloc(struct ufs_hba *hba)
632{
633 /* Allocate one Transfer Request Descriptor
634 * Should be aligned to 1k boundary.
635 */
Neil Armstrong9c223d82024-09-30 14:44:23 +0200636 hba->utrdl = memalign(1024,
637 ALIGN(sizeof(struct utp_transfer_req_desc),
638 ARCH_DMA_MINALIGN));
Faiz Abbas7feafb02019-10-15 18:24:36 +0530639 if (!hba->utrdl) {
640 dev_err(hba->dev, "Transfer Descriptor memory allocation failed\n");
641 return -ENOMEM;
642 }
643
644 /* Allocate one Command Descriptor
645 * Should be aligned to 1k boundary.
646 */
Neil Armstrong9c223d82024-09-30 14:44:23 +0200647 hba->ucdl = memalign(1024,
648 ALIGN(sizeof(struct utp_transfer_cmd_desc),
649 ARCH_DMA_MINALIGN));
Faiz Abbas7feafb02019-10-15 18:24:36 +0530650 if (!hba->ucdl) {
651 dev_err(hba->dev, "Command descriptor memory allocation failed\n");
652 return -ENOMEM;
653 }
654
655 return 0;
656}
657
658/**
659 * ufshcd_get_intr_mask - Get the interrupt bit mask
660 */
661static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
662{
663 u32 intr_mask = 0;
664
665 switch (hba->version) {
666 case UFSHCI_VERSION_10:
667 intr_mask = INTERRUPT_MASK_ALL_VER_10;
668 break;
669 case UFSHCI_VERSION_11:
670 case UFSHCI_VERSION_20:
671 intr_mask = INTERRUPT_MASK_ALL_VER_11;
672 break;
673 case UFSHCI_VERSION_21:
674 default:
675 intr_mask = INTERRUPT_MASK_ALL_VER_21;
676 break;
677 }
678
679 return intr_mask;
680}
681
682/**
683 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
684 */
685static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
686{
687 return ufshcd_readl(hba, REG_UFS_VERSION);
688}
689
690/**
691 * ufshcd_get_upmcrs - Get the power mode change request status
692 */
693static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
694{
695 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
696}
697
698/**
Neil Armstrong4139e562024-09-30 14:44:25 +0200699 * ufshcd_cache_flush - Flush cache
Marek Vasutc5b3e5c2023-08-16 17:05:55 +0200700 *
Neil Armstrong4139e562024-09-30 14:44:25 +0200701 * Flush cache in aligned address..address+size range.
Marek Vasutc5b3e5c2023-08-16 17:05:55 +0200702 */
Neil Armstrong4139e562024-09-30 14:44:25 +0200703static void ufshcd_cache_flush(void *addr, unsigned long size)
Marek Vasutc5b3e5c2023-08-16 17:05:55 +0200704{
Neil Armstrongc64d22b2024-09-30 14:44:24 +0200705 uintptr_t start_addr = (uintptr_t)addr & ~(ARCH_DMA_MINALIGN - 1);
706 uintptr_t end_addr = ALIGN((uintptr_t)addr + size, ARCH_DMA_MINALIGN);
Marek Vasutc5b3e5c2023-08-16 17:05:55 +0200707
Neil Armstrongc64d22b2024-09-30 14:44:24 +0200708 flush_dcache_range(start_addr, end_addr);
Neil Armstrong4139e562024-09-30 14:44:25 +0200709}
710
711/**
712 * ufshcd_cache_invalidate - Invalidate cache
713 *
714 * Invalidate cache in aligned address..address+size range.
715 */
716static void ufshcd_cache_invalidate(void *addr, unsigned long size)
717{
718 uintptr_t start_addr = (uintptr_t)addr & ~(ARCH_DMA_MINALIGN - 1);
719 uintptr_t end_addr = ALIGN((uintptr_t)addr + size, ARCH_DMA_MINALIGN);
720
Neil Armstrongc64d22b2024-09-30 14:44:24 +0200721 invalidate_dcache_range(start_addr, end_addr);
Marek Vasutc5b3e5c2023-08-16 17:05:55 +0200722}
723
724/**
Faiz Abbas7feafb02019-10-15 18:24:36 +0530725 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
726 * descriptor according to request
727 */
Marek Vasut7f26fcb2023-08-16 17:05:53 +0200728static void ufshcd_prepare_req_desc_hdr(struct ufs_hba *hba,
Faiz Abbas7feafb02019-10-15 18:24:36 +0530729 u32 *upiu_flags,
730 enum dma_data_direction cmd_dir)
731{
Marek Vasut7f26fcb2023-08-16 17:05:53 +0200732 struct utp_transfer_req_desc *req_desc = hba->utrdl;
Faiz Abbas7feafb02019-10-15 18:24:36 +0530733 u32 data_direction;
734 u32 dword_0;
735
736 if (cmd_dir == DMA_FROM_DEVICE) {
737 data_direction = UTP_DEVICE_TO_HOST;
738 *upiu_flags = UPIU_CMD_FLAGS_READ;
739 } else if (cmd_dir == DMA_TO_DEVICE) {
740 data_direction = UTP_HOST_TO_DEVICE;
741 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
742 } else {
743 data_direction = UTP_NO_DATA_TRANSFER;
744 *upiu_flags = UPIU_CMD_FLAGS_NONE;
745 }
746
747 dword_0 = data_direction | (0x1 << UPIU_COMMAND_TYPE_OFFSET);
748
749 /* Enable Interrupt for command */
750 dword_0 |= UTP_REQ_DESC_INT_CMD;
751
752 /* Transfer request descriptor header fields */
753 req_desc->header.dword_0 = cpu_to_le32(dword_0);
754 /* dword_1 is reserved, hence it is set to 0 */
755 req_desc->header.dword_1 = 0;
756 /*
757 * assigning invalid value for command status. Controller
758 * updates OCS on command completion, with the command
759 * status
760 */
761 req_desc->header.dword_2 =
762 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
763 /* dword_3 is reserved, hence it is set to 0 */
764 req_desc->header.dword_3 = 0;
765
766 req_desc->prd_table_length = 0;
Marek Vasutc5b3e5c2023-08-16 17:05:55 +0200767
Neil Armstrong4139e562024-09-30 14:44:25 +0200768 ufshcd_cache_flush(req_desc, sizeof(*req_desc));
Faiz Abbas7feafb02019-10-15 18:24:36 +0530769}
770
771static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
772 u32 upiu_flags)
773{
774 struct utp_upiu_req *ucd_req_ptr = hba->ucd_req_ptr;
775 struct ufs_query *query = &hba->dev_cmd.query;
776 u16 len = be16_to_cpu(query->request.upiu_req.length);
777
778 /* Query request header */
779 ucd_req_ptr->header.dword_0 =
780 UPIU_HEADER_DWORD(UPIU_TRANSACTION_QUERY_REQ,
781 upiu_flags, 0, TASK_TAG);
782 ucd_req_ptr->header.dword_1 =
783 UPIU_HEADER_DWORD(0, query->request.query_func,
784 0, 0);
785
786 /* Data segment length only need for WRITE_DESC */
787 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
788 ucd_req_ptr->header.dword_2 =
789 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
790 else
791 ucd_req_ptr->header.dword_2 = 0;
792
793 /* Copy the Query Request buffer as is */
794 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req, QUERY_OSF_SIZE);
795
796 /* Copy the Descriptor */
Marek Vasutc5b3e5c2023-08-16 17:05:55 +0200797 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) {
Faiz Abbas7feafb02019-10-15 18:24:36 +0530798 memcpy(ucd_req_ptr + 1, query->descriptor, len);
Neil Armstrong4139e562024-09-30 14:44:25 +0200799 ufshcd_cache_flush(ucd_req_ptr, 2 * sizeof(*ucd_req_ptr));
Marek Vasutc5b3e5c2023-08-16 17:05:55 +0200800 } else {
Neil Armstrong4139e562024-09-30 14:44:25 +0200801 ufshcd_cache_flush(ucd_req_ptr, sizeof(*ucd_req_ptr));
Marek Vasutc5b3e5c2023-08-16 17:05:55 +0200802 }
Faiz Abbas7feafb02019-10-15 18:24:36 +0530803
804 memset(hba->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Neil Armstrong4139e562024-09-30 14:44:25 +0200805 ufshcd_cache_flush(hba->ucd_rsp_ptr, sizeof(*hba->ucd_rsp_ptr));
Faiz Abbas7feafb02019-10-15 18:24:36 +0530806}
807
808static inline void ufshcd_prepare_utp_nop_upiu(struct ufs_hba *hba)
809{
810 struct utp_upiu_req *ucd_req_ptr = hba->ucd_req_ptr;
811
812 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
813
814 /* command descriptor fields */
815 ucd_req_ptr->header.dword_0 =
Bhupesh Sharma820801e2023-07-03 00:39:12 +0530816 UPIU_HEADER_DWORD(UPIU_TRANSACTION_NOP_OUT, 0, 0, TASK_TAG);
Faiz Abbas7feafb02019-10-15 18:24:36 +0530817 /* clear rest of the fields of basic header */
818 ucd_req_ptr->header.dword_1 = 0;
819 ucd_req_ptr->header.dword_2 = 0;
820
821 memset(hba->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Marek Vasutc5b3e5c2023-08-16 17:05:55 +0200822
Neil Armstrong4139e562024-09-30 14:44:25 +0200823 ufshcd_cache_flush(ucd_req_ptr, sizeof(*ucd_req_ptr));
824 ufshcd_cache_flush(hba->ucd_rsp_ptr, sizeof(*hba->ucd_rsp_ptr));
Faiz Abbas7feafb02019-10-15 18:24:36 +0530825}
826
827/**
828 * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU)
829 * for Device Management Purposes
830 */
831static int ufshcd_comp_devman_upiu(struct ufs_hba *hba,
832 enum dev_cmd_type cmd_type)
833{
834 u32 upiu_flags;
835 int ret = 0;
Faiz Abbas7feafb02019-10-15 18:24:36 +0530836
837 hba->dev_cmd.type = cmd_type;
838
Marek Vasut7f26fcb2023-08-16 17:05:53 +0200839 ufshcd_prepare_req_desc_hdr(hba, &upiu_flags, DMA_NONE);
Faiz Abbas7feafb02019-10-15 18:24:36 +0530840 switch (cmd_type) {
841 case DEV_CMD_TYPE_QUERY:
842 ufshcd_prepare_utp_query_req_upiu(hba, upiu_flags);
843 break;
844 case DEV_CMD_TYPE_NOP:
845 ufshcd_prepare_utp_nop_upiu(hba);
846 break;
847 default:
848 ret = -EINVAL;
849 }
850
851 return ret;
852}
853
854static int ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
855{
856 unsigned long start;
857 u32 intr_status;
858 u32 enabled_intr_status;
859
860 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
861
862 start = get_timer(0);
863 do {
864 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
865 enabled_intr_status = intr_status & hba->intr_mask;
866 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
867
868 if (get_timer(start) > QUERY_REQ_TIMEOUT) {
869 dev_err(hba->dev,
870 "Timedout waiting for UTP response\n");
871
872 return -ETIMEDOUT;
873 }
874
875 if (enabled_intr_status & UFSHCD_ERROR_MASK) {
876 dev_err(hba->dev, "Error in status:%08x\n",
877 enabled_intr_status);
878
879 return -1;
880 }
881 } while (!(enabled_intr_status & UTP_TRANSFER_REQ_COMPL));
882
883 return 0;
884}
885
886/**
887 * ufshcd_get_req_rsp - returns the TR response transaction type
888 */
889static inline int ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
890{
Neil Armstrong4139e562024-09-30 14:44:25 +0200891 ufshcd_cache_invalidate(ucd_rsp_ptr, sizeof(*ucd_rsp_ptr));
892
Faiz Abbas7feafb02019-10-15 18:24:36 +0530893 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
894}
895
896/**
897 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
898 *
899 */
900static inline int ufshcd_get_tr_ocs(struct ufs_hba *hba)
901{
Marek Vasut12675cb2023-08-16 17:05:54 +0200902 struct utp_transfer_req_desc *req_desc = hba->utrdl;
903
Neil Armstrong4139e562024-09-30 14:44:25 +0200904 ufshcd_cache_invalidate(req_desc, sizeof(*req_desc));
905
Marek Vasut12675cb2023-08-16 17:05:54 +0200906 return le32_to_cpu(req_desc->header.dword_2) & MASK_OCS;
Faiz Abbas7feafb02019-10-15 18:24:36 +0530907}
908
909static inline int ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
910{
911 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
912}
913
914static int ufshcd_check_query_response(struct ufs_hba *hba)
915{
916 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
917
918 /* Get the UPIU response */
919 query_res->response = ufshcd_get_rsp_upiu_result(hba->ucd_rsp_ptr) >>
920 UPIU_RSP_CODE_OFFSET;
921 return query_res->response;
922}
923
924/**
925 * ufshcd_copy_query_response() - Copy the Query Response and the data
926 * descriptor
927 */
928static int ufshcd_copy_query_response(struct ufs_hba *hba)
929{
930 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
931
932 memcpy(&query_res->upiu_res, &hba->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
933
934 /* Get the descriptor */
935 if (hba->dev_cmd.query.descriptor &&
936 hba->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
937 u8 *descp = (u8 *)hba->ucd_rsp_ptr +
938 GENERAL_UPIU_REQUEST_SIZE;
939 u16 resp_len;
940 u16 buf_len;
941
942 /* data segment length */
943 resp_len = be32_to_cpu(hba->ucd_rsp_ptr->header.dword_2) &
944 MASK_QUERY_DATA_SEG_LEN;
945 buf_len =
946 be16_to_cpu(hba->dev_cmd.query.request.upiu_req.length);
947 if (likely(buf_len >= resp_len)) {
948 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
949 } else {
950 dev_warn(hba->dev,
Bin Meng1b3dab22023-10-11 21:15:45 +0800951 "%s: Response size is bigger than buffer\n",
Faiz Abbas7feafb02019-10-15 18:24:36 +0530952 __func__);
953 return -EINVAL;
954 }
955 }
956
957 return 0;
958}
959
960/**
961 * ufshcd_exec_dev_cmd - API for sending device management requests
962 */
963static int ufshcd_exec_dev_cmd(struct ufs_hba *hba, enum dev_cmd_type cmd_type,
964 int timeout)
965{
966 int err;
967 int resp;
968
969 err = ufshcd_comp_devman_upiu(hba, cmd_type);
970 if (err)
971 return err;
972
973 err = ufshcd_send_command(hba, TASK_TAG);
974 if (err)
975 return err;
976
977 err = ufshcd_get_tr_ocs(hba);
978 if (err) {
979 dev_err(hba->dev, "Error in OCS:%d\n", err);
980 return -EINVAL;
981 }
982
983 resp = ufshcd_get_req_rsp(hba->ucd_rsp_ptr);
984 switch (resp) {
985 case UPIU_TRANSACTION_NOP_IN:
986 break;
987 case UPIU_TRANSACTION_QUERY_RSP:
988 err = ufshcd_check_query_response(hba);
989 if (!err)
990 err = ufshcd_copy_query_response(hba);
991 break;
992 case UPIU_TRANSACTION_REJECT_UPIU:
993 /* TODO: handle Reject UPIU Response */
994 err = -EPERM;
995 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
996 __func__);
997 break;
998 default:
999 err = -EINVAL;
1000 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
1001 __func__, resp);
1002 }
1003
1004 return err;
1005}
1006
1007/**
1008 * ufshcd_init_query() - init the query response and request parameters
1009 */
1010static inline void ufshcd_init_query(struct ufs_hba *hba,
1011 struct ufs_query_req **request,
1012 struct ufs_query_res **response,
1013 enum query_opcode opcode,
1014 u8 idn, u8 index, u8 selector)
1015{
1016 *request = &hba->dev_cmd.query.request;
1017 *response = &hba->dev_cmd.query.response;
1018 memset(*request, 0, sizeof(struct ufs_query_req));
1019 memset(*response, 0, sizeof(struct ufs_query_res));
1020 (*request)->upiu_req.opcode = opcode;
1021 (*request)->upiu_req.idn = idn;
1022 (*request)->upiu_req.index = index;
1023 (*request)->upiu_req.selector = selector;
1024}
1025
1026/**
1027 * ufshcd_query_flag() - API function for sending flag query requests
1028 */
1029int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
1030 enum flag_idn idn, bool *flag_res)
1031{
1032 struct ufs_query_req *request = NULL;
1033 struct ufs_query_res *response = NULL;
1034 int err, index = 0, selector = 0;
1035 int timeout = QUERY_REQ_TIMEOUT;
1036
1037 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1038 selector);
1039
1040 switch (opcode) {
1041 case UPIU_QUERY_OPCODE_SET_FLAG:
1042 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
1043 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
1044 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1045 break;
1046 case UPIU_QUERY_OPCODE_READ_FLAG:
1047 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1048 if (!flag_res) {
1049 /* No dummy reads */
1050 dev_err(hba->dev, "%s: Invalid argument for read request\n",
1051 __func__);
1052 err = -EINVAL;
1053 goto out;
1054 }
1055 break;
1056 default:
1057 dev_err(hba->dev,
1058 "%s: Expected query flag opcode but got = %d\n",
1059 __func__, opcode);
1060 err = -EINVAL;
1061 goto out;
1062 }
1063
1064 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
1065
1066 if (err) {
1067 dev_err(hba->dev,
1068 "%s: Sending flag query for idn %d failed, err = %d\n",
1069 __func__, idn, err);
1070 goto out;
1071 }
1072
1073 if (flag_res)
1074 *flag_res = (be32_to_cpu(response->upiu_res.value) &
1075 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
1076
1077out:
1078 return err;
1079}
1080
1081static int ufshcd_query_flag_retry(struct ufs_hba *hba,
1082 enum query_opcode opcode,
1083 enum flag_idn idn, bool *flag_res)
1084{
1085 int ret;
1086 int retries;
1087
1088 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
1089 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
1090 if (ret)
1091 dev_dbg(hba->dev,
1092 "%s: failed with error %d, retries %d\n",
1093 __func__, ret, retries);
1094 else
1095 break;
1096 }
1097
1098 if (ret)
1099 dev_err(hba->dev,
1100 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
1101 __func__, opcode, idn, ret, retries);
1102 return ret;
1103}
1104
1105static int __ufshcd_query_descriptor(struct ufs_hba *hba,
1106 enum query_opcode opcode,
1107 enum desc_idn idn, u8 index, u8 selector,
1108 u8 *desc_buf, int *buf_len)
1109{
1110 struct ufs_query_req *request = NULL;
1111 struct ufs_query_res *response = NULL;
1112 int err;
1113
1114 if (!desc_buf) {
1115 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
1116 __func__, opcode);
1117 err = -EINVAL;
1118 goto out;
1119 }
1120
1121 if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
1122 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
1123 __func__, *buf_len);
1124 err = -EINVAL;
1125 goto out;
1126 }
1127
1128 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1129 selector);
1130 hba->dev_cmd.query.descriptor = desc_buf;
1131 request->upiu_req.length = cpu_to_be16(*buf_len);
1132
1133 switch (opcode) {
1134 case UPIU_QUERY_OPCODE_WRITE_DESC:
1135 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1136 break;
1137 case UPIU_QUERY_OPCODE_READ_DESC:
1138 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1139 break;
1140 default:
1141 dev_err(hba->dev, "%s: Expected query descriptor opcode but got = 0x%.2x\n",
1142 __func__, opcode);
1143 err = -EINVAL;
1144 goto out;
1145 }
1146
1147 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
1148
1149 if (err) {
1150 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
1151 __func__, opcode, idn, index, err);
1152 goto out;
1153 }
1154
1155 hba->dev_cmd.query.descriptor = NULL;
1156 *buf_len = be16_to_cpu(response->upiu_res.length);
1157
1158out:
1159 return err;
1160}
1161
1162/**
1163 * ufshcd_query_descriptor_retry - API function for sending descriptor requests
1164 */
1165int ufshcd_query_descriptor_retry(struct ufs_hba *hba, enum query_opcode opcode,
1166 enum desc_idn idn, u8 index, u8 selector,
1167 u8 *desc_buf, int *buf_len)
1168{
1169 int err;
1170 int retries;
1171
1172 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
1173 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
1174 selector, desc_buf, buf_len);
1175 if (!err || err == -EINVAL)
1176 break;
1177 }
1178
1179 return err;
1180}
1181
1182/**
1183 * ufshcd_read_desc_length - read the specified descriptor length from header
1184 */
1185static int ufshcd_read_desc_length(struct ufs_hba *hba, enum desc_idn desc_id,
1186 int desc_index, int *desc_length)
1187{
1188 int ret;
1189 u8 header[QUERY_DESC_HDR_SIZE];
1190 int header_len = QUERY_DESC_HDR_SIZE;
1191
1192 if (desc_id >= QUERY_DESC_IDN_MAX)
1193 return -EINVAL;
1194
1195 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
1196 desc_id, desc_index, 0, header,
1197 &header_len);
1198
1199 if (ret) {
Bin Meng1b3dab22023-10-11 21:15:45 +08001200 dev_err(hba->dev, "%s: Failed to get descriptor header id %d\n",
Faiz Abbas7feafb02019-10-15 18:24:36 +05301201 __func__, desc_id);
1202 return ret;
1203 } else if (desc_id != header[QUERY_DESC_DESC_TYPE_OFFSET]) {
Bin Meng1b3dab22023-10-11 21:15:45 +08001204 dev_warn(hba->dev, "%s: descriptor header id %d and desc_id %d mismatch\n",
Faiz Abbas7feafb02019-10-15 18:24:36 +05301205 __func__, header[QUERY_DESC_DESC_TYPE_OFFSET],
1206 desc_id);
1207 ret = -EINVAL;
1208 }
1209
1210 *desc_length = header[QUERY_DESC_LENGTH_OFFSET];
1211
1212 return ret;
1213}
1214
1215static void ufshcd_init_desc_sizes(struct ufs_hba *hba)
1216{
1217 int err;
1218
1219 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_DEVICE, 0,
1220 &hba->desc_size.dev_desc);
1221 if (err)
1222 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
1223
1224 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_POWER, 0,
1225 &hba->desc_size.pwr_desc);
1226 if (err)
1227 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
1228
1229 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_INTERCONNECT, 0,
1230 &hba->desc_size.interc_desc);
1231 if (err)
1232 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
1233
1234 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_CONFIGURATION, 0,
1235 &hba->desc_size.conf_desc);
1236 if (err)
1237 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
1238
1239 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_UNIT, 0,
1240 &hba->desc_size.unit_desc);
1241 if (err)
1242 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
1243
1244 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_GEOMETRY, 0,
1245 &hba->desc_size.geom_desc);
1246 if (err)
1247 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
1248
1249 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_HEALTH, 0,
1250 &hba->desc_size.hlth_desc);
1251 if (err)
1252 hba->desc_size.hlth_desc = QUERY_DESC_HEALTH_DEF_SIZE;
1253}
1254
1255/**
1256 * ufshcd_map_desc_id_to_length - map descriptor IDN to its length
1257 *
1258 */
1259int ufshcd_map_desc_id_to_length(struct ufs_hba *hba, enum desc_idn desc_id,
1260 int *desc_len)
1261{
1262 switch (desc_id) {
1263 case QUERY_DESC_IDN_DEVICE:
1264 *desc_len = hba->desc_size.dev_desc;
1265 break;
1266 case QUERY_DESC_IDN_POWER:
1267 *desc_len = hba->desc_size.pwr_desc;
1268 break;
1269 case QUERY_DESC_IDN_GEOMETRY:
1270 *desc_len = hba->desc_size.geom_desc;
1271 break;
1272 case QUERY_DESC_IDN_CONFIGURATION:
1273 *desc_len = hba->desc_size.conf_desc;
1274 break;
1275 case QUERY_DESC_IDN_UNIT:
1276 *desc_len = hba->desc_size.unit_desc;
1277 break;
1278 case QUERY_DESC_IDN_INTERCONNECT:
1279 *desc_len = hba->desc_size.interc_desc;
1280 break;
1281 case QUERY_DESC_IDN_STRING:
1282 *desc_len = QUERY_DESC_MAX_SIZE;
1283 break;
1284 case QUERY_DESC_IDN_HEALTH:
1285 *desc_len = hba->desc_size.hlth_desc;
1286 break;
1287 case QUERY_DESC_IDN_RFU_0:
1288 case QUERY_DESC_IDN_RFU_1:
1289 *desc_len = 0;
1290 break;
1291 default:
1292 *desc_len = 0;
1293 return -EINVAL;
1294 }
1295 return 0;
1296}
1297EXPORT_SYMBOL(ufshcd_map_desc_id_to_length);
1298
1299/**
1300 * ufshcd_read_desc_param - read the specified descriptor parameter
1301 *
1302 */
1303int ufshcd_read_desc_param(struct ufs_hba *hba, enum desc_idn desc_id,
1304 int desc_index, u8 param_offset, u8 *param_read_buf,
1305 u8 param_size)
1306{
1307 int ret;
1308 u8 *desc_buf;
1309 int buff_len;
1310 bool is_kmalloc = true;
1311
1312 /* Safety check */
1313 if (desc_id >= QUERY_DESC_IDN_MAX || !param_size)
1314 return -EINVAL;
1315
1316 /* Get the max length of descriptor from structure filled up at probe
1317 * time.
1318 */
1319 ret = ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len);
1320
1321 /* Sanity checks */
1322 if (ret || !buff_len) {
Bin Meng1b3dab22023-10-11 21:15:45 +08001323 dev_err(hba->dev, "%s: Failed to get full descriptor length\n",
Faiz Abbas7feafb02019-10-15 18:24:36 +05301324 __func__);
1325 return ret;
1326 }
1327
1328 /* Check whether we need temp memory */
1329 if (param_offset != 0 || param_size < buff_len) {
1330 desc_buf = kmalloc(buff_len, GFP_KERNEL);
1331 if (!desc_buf)
1332 return -ENOMEM;
1333 } else {
1334 desc_buf = param_read_buf;
1335 is_kmalloc = false;
1336 }
1337
1338 /* Request for full descriptor */
1339 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
1340 desc_id, desc_index, 0, desc_buf,
1341 &buff_len);
1342
1343 if (ret) {
Bin Meng1b3dab22023-10-11 21:15:45 +08001344 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 +05301345 __func__, desc_id, desc_index, param_offset, ret);
1346 goto out;
1347 }
1348
1349 /* Sanity check */
1350 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
Bin Meng1b3dab22023-10-11 21:15:45 +08001351 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header\n",
Faiz Abbas7feafb02019-10-15 18:24:36 +05301352 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
1353 ret = -EINVAL;
1354 goto out;
1355 }
1356
1357 /* Check wherher we will not copy more data, than available */
1358 if (is_kmalloc && param_size > buff_len)
1359 param_size = buff_len;
1360
1361 if (is_kmalloc)
1362 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
1363out:
1364 if (is_kmalloc)
1365 kfree(desc_buf);
1366 return ret;
1367}
1368
1369/* replace non-printable or non-ASCII characters with spaces */
1370static inline void ufshcd_remove_non_printable(uint8_t *val)
1371{
1372 if (!val)
1373 return;
1374
1375 if (*val < 0x20 || *val > 0x7e)
1376 *val = ' ';
1377}
1378
1379/**
1380 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
1381 * state) and waits for it to take effect.
1382 *
1383 */
1384static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
1385{
1386 unsigned long start = 0;
1387 u8 status;
1388 int ret;
1389
1390 ret = ufshcd_send_uic_cmd(hba, cmd);
1391 if (ret) {
1392 dev_err(hba->dev,
1393 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
1394 cmd->command, cmd->argument3, ret);
1395
1396 return ret;
1397 }
1398
1399 start = get_timer(0);
1400 do {
1401 status = ufshcd_get_upmcrs(hba);
1402 if (get_timer(start) > UFS_UIC_CMD_TIMEOUT) {
1403 dev_err(hba->dev,
1404 "pwr ctrl cmd 0x%x failed, host upmcrs:0x%x\n",
1405 cmd->command, status);
1406 ret = (status != PWR_OK) ? status : -1;
1407 break;
1408 }
1409 } while (status != PWR_LOCAL);
1410
1411 return ret;
1412}
1413
1414/**
1415 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode change
1416 * using DME_SET primitives.
1417 */
1418static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
1419{
1420 struct uic_command uic_cmd = {0};
1421 int ret;
1422
1423 uic_cmd.command = UIC_CMD_DME_SET;
1424 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
1425 uic_cmd.argument3 = mode;
1426 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
1427
1428 return ret;
1429}
1430
1431static
1432void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufs_hba *hba,
1433 struct scsi_cmd *pccb, u32 upiu_flags)
1434{
1435 struct utp_upiu_req *ucd_req_ptr = hba->ucd_req_ptr;
1436 unsigned int cdb_len;
1437
1438 /* command descriptor fields */
1439 ucd_req_ptr->header.dword_0 =
1440 UPIU_HEADER_DWORD(UPIU_TRANSACTION_COMMAND, upiu_flags,
1441 pccb->lun, TASK_TAG);
1442 ucd_req_ptr->header.dword_1 =
1443 UPIU_HEADER_DWORD(UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
1444
1445 /* Total EHS length and Data segment length will be zero */
1446 ucd_req_ptr->header.dword_2 = 0;
1447
1448 ucd_req_ptr->sc.exp_data_transfer_len = cpu_to_be32(pccb->datalen);
1449
1450 cdb_len = min_t(unsigned short, pccb->cmdlen, UFS_CDB_SIZE);
1451 memset(ucd_req_ptr->sc.cdb, 0, UFS_CDB_SIZE);
1452 memcpy(ucd_req_ptr->sc.cdb, pccb->cmd, cdb_len);
1453
1454 memset(hba->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Neil Armstrong4139e562024-09-30 14:44:25 +02001455 ufshcd_cache_flush(ucd_req_ptr, sizeof(*ucd_req_ptr));
1456 ufshcd_cache_flush(hba->ucd_rsp_ptr, sizeof(*hba->ucd_rsp_ptr));
Faiz Abbas7feafb02019-10-15 18:24:36 +05301457}
1458
1459static inline void prepare_prdt_desc(struct ufshcd_sg_entry *entry,
1460 unsigned char *buf, ulong len)
1461{
1462 entry->size = cpu_to_le32(len) | GENMASK(1, 0);
1463 entry->base_addr = cpu_to_le32(lower_32_bits((unsigned long)buf));
1464 entry->upper_addr = cpu_to_le32(upper_32_bits((unsigned long)buf));
1465}
1466
1467static void prepare_prdt_table(struct ufs_hba *hba, struct scsi_cmd *pccb)
1468{
1469 struct utp_transfer_req_desc *req_desc = hba->utrdl;
1470 struct ufshcd_sg_entry *prd_table = hba->ucd_prdt_ptr;
1471 ulong datalen = pccb->datalen;
1472 int table_length;
1473 u8 *buf;
1474 int i;
1475
1476 if (!datalen) {
1477 req_desc->prd_table_length = 0;
Neil Armstrong4139e562024-09-30 14:44:25 +02001478 ufshcd_cache_flush(req_desc, sizeof(*req_desc));
Faiz Abbas7feafb02019-10-15 18:24:36 +05301479 return;
1480 }
1481
1482 table_length = DIV_ROUND_UP(pccb->datalen, MAX_PRDT_ENTRY);
1483 buf = pccb->pdata;
1484 i = table_length;
1485 while (--i) {
1486 prepare_prdt_desc(&prd_table[table_length - i - 1], buf,
1487 MAX_PRDT_ENTRY - 1);
1488 buf += MAX_PRDT_ENTRY;
1489 datalen -= MAX_PRDT_ENTRY;
1490 }
1491
1492 prepare_prdt_desc(&prd_table[table_length - i - 1], buf, datalen - 1);
1493
1494 req_desc->prd_table_length = table_length;
Neil Armstrong4139e562024-09-30 14:44:25 +02001495 ufshcd_cache_flush(prd_table, sizeof(*prd_table) * table_length);
1496 ufshcd_cache_flush(req_desc, sizeof(*req_desc));
Faiz Abbas7feafb02019-10-15 18:24:36 +05301497}
1498
1499static int ufs_scsi_exec(struct udevice *scsi_dev, struct scsi_cmd *pccb)
1500{
1501 struct ufs_hba *hba = dev_get_uclass_priv(scsi_dev->parent);
Faiz Abbas7feafb02019-10-15 18:24:36 +05301502 u32 upiu_flags;
1503 int ocs, result = 0;
1504 u8 scsi_status;
1505
Marek Vasut7f26fcb2023-08-16 17:05:53 +02001506 ufshcd_prepare_req_desc_hdr(hba, &upiu_flags, pccb->dma_dir);
Faiz Abbas7feafb02019-10-15 18:24:36 +05301507 ufshcd_prepare_utp_scsi_cmd_upiu(hba, pccb, upiu_flags);
1508 prepare_prdt_table(hba, pccb);
1509
Neil Armstrong589a7bf2024-09-30 14:44:26 +02001510 ufshcd_cache_flush(pccb->pdata, pccb->datalen);
1511
Faiz Abbas7feafb02019-10-15 18:24:36 +05301512 ufshcd_send_command(hba, TASK_TAG);
1513
Neil Armstrong589a7bf2024-09-30 14:44:26 +02001514 ufshcd_cache_invalidate(pccb->pdata, pccb->datalen);
1515
Faiz Abbas7feafb02019-10-15 18:24:36 +05301516 ocs = ufshcd_get_tr_ocs(hba);
1517 switch (ocs) {
1518 case OCS_SUCCESS:
1519 result = ufshcd_get_req_rsp(hba->ucd_rsp_ptr);
1520 switch (result) {
1521 case UPIU_TRANSACTION_RESPONSE:
1522 result = ufshcd_get_rsp_upiu_result(hba->ucd_rsp_ptr);
1523
1524 scsi_status = result & MASK_SCSI_STATUS;
1525 if (scsi_status)
1526 return -EINVAL;
1527
1528 break;
1529 case UPIU_TRANSACTION_REJECT_UPIU:
1530 /* TODO: handle Reject UPIU Response */
1531 dev_err(hba->dev,
1532 "Reject UPIU not fully implemented\n");
1533 return -EINVAL;
1534 default:
1535 dev_err(hba->dev,
1536 "Unexpected request response code = %x\n",
1537 result);
1538 return -EINVAL;
1539 }
1540 break;
1541 default:
1542 dev_err(hba->dev, "OCS error from controller = %x\n", ocs);
1543 return -EINVAL;
1544 }
1545
1546 return 0;
1547}
1548
1549static inline int ufshcd_read_desc(struct ufs_hba *hba, enum desc_idn desc_id,
1550 int desc_index, u8 *buf, u32 size)
1551{
1552 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
1553}
1554
1555static int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size)
1556{
1557 return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size);
1558}
1559
1560/**
1561 * ufshcd_read_string_desc - read string descriptor
1562 *
1563 */
1564int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index,
1565 u8 *buf, u32 size, bool ascii)
1566{
1567 int err = 0;
1568
1569 err = ufshcd_read_desc(hba, QUERY_DESC_IDN_STRING, desc_index, buf,
1570 size);
1571
1572 if (err) {
1573 dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n",
1574 __func__, QUERY_REQ_RETRIES, err);
1575 goto out;
1576 }
1577
1578 if (ascii) {
1579 int desc_len;
1580 int ascii_len;
1581 int i;
1582 u8 *buff_ascii;
1583
1584 desc_len = buf[0];
1585 /* remove header and divide by 2 to move from UTF16 to UTF8 */
1586 ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1;
1587 if (size < ascii_len + QUERY_DESC_HDR_SIZE) {
1588 dev_err(hba->dev, "%s: buffer allocated size is too small\n",
1589 __func__);
1590 err = -ENOMEM;
1591 goto out;
1592 }
1593
1594 buff_ascii = kmalloc(ascii_len, GFP_KERNEL);
1595 if (!buff_ascii) {
1596 err = -ENOMEM;
1597 goto out;
1598 }
1599
1600 /*
1601 * the descriptor contains string in UTF16 format
1602 * we need to convert to utf-8 so it can be displayed
1603 */
1604 utf16_to_utf8(buff_ascii,
1605 (uint16_t *)&buf[QUERY_DESC_HDR_SIZE], ascii_len);
1606
1607 /* replace non-printable or non-ASCII characters with spaces */
1608 for (i = 0; i < ascii_len; i++)
1609 ufshcd_remove_non_printable(&buff_ascii[i]);
1610
1611 memset(buf + QUERY_DESC_HDR_SIZE, 0,
1612 size - QUERY_DESC_HDR_SIZE);
1613 memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len);
1614 buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE;
1615 kfree(buff_ascii);
1616 }
1617out:
1618 return err;
1619}
1620
1621static int ufs_get_device_desc(struct ufs_hba *hba,
1622 struct ufs_dev_desc *dev_desc)
1623{
1624 int err;
1625 size_t buff_len;
1626 u8 model_index;
1627 u8 *desc_buf;
1628
1629 buff_len = max_t(size_t, hba->desc_size.dev_desc,
1630 QUERY_DESC_MAX_SIZE + 1);
1631 desc_buf = kmalloc(buff_len, GFP_KERNEL);
1632 if (!desc_buf) {
1633 err = -ENOMEM;
1634 goto out;
1635 }
1636
1637 err = ufshcd_read_device_desc(hba, desc_buf, hba->desc_size.dev_desc);
1638 if (err) {
1639 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
1640 __func__, err);
1641 goto out;
1642 }
1643
1644 /*
1645 * getting vendor (manufacturerID) and Bank Index in big endian
1646 * format
1647 */
1648 dev_desc->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
1649 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
1650
1651 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
1652
1653 /* Zero-pad entire buffer for string termination. */
1654 memset(desc_buf, 0, buff_len);
1655
1656 err = ufshcd_read_string_desc(hba, model_index, desc_buf,
1657 QUERY_DESC_MAX_SIZE, true/*ASCII*/);
1658 if (err) {
1659 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
1660 __func__, err);
1661 goto out;
1662 }
1663
1664 desc_buf[QUERY_DESC_MAX_SIZE] = '\0';
1665 strlcpy(dev_desc->model, (char *)(desc_buf + QUERY_DESC_HDR_SIZE),
1666 min_t(u8, desc_buf[QUERY_DESC_LENGTH_OFFSET],
1667 MAX_MODEL_LEN));
1668
1669 /* Null terminate the model string */
1670 dev_desc->model[MAX_MODEL_LEN] = '\0';
1671
1672out:
1673 kfree(desc_buf);
1674 return err;
1675}
1676
1677/**
1678 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
1679 */
1680static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
1681{
1682 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
1683
1684 if (hba->max_pwr_info.is_valid)
1685 return 0;
1686
Marek Vasutf4301512023-08-16 17:05:51 +02001687 if (hba->quirks & UFSHCD_QUIRK_HIBERN_FASTAUTO) {
1688 pwr_info->pwr_tx = FASTAUTO_MODE;
1689 pwr_info->pwr_rx = FASTAUTO_MODE;
1690 } else {
1691 pwr_info->pwr_tx = FAST_MODE;
1692 pwr_info->pwr_rx = FAST_MODE;
1693 }
Faiz Abbas7feafb02019-10-15 18:24:36 +05301694 pwr_info->hs_rate = PA_HS_MODE_B;
1695
1696 /* Get the connected lane count */
1697 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
1698 &pwr_info->lane_rx);
1699 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
1700 &pwr_info->lane_tx);
1701
1702 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
1703 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
1704 __func__, pwr_info->lane_rx, pwr_info->lane_tx);
1705 return -EINVAL;
1706 }
1707
1708 /*
1709 * First, get the maximum gears of HS speed.
1710 * If a zero value, it means there is no HSGEAR capability.
1711 * Then, get the maximum gears of PWM speed.
1712 */
1713 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
1714 if (!pwr_info->gear_rx) {
1715 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
1716 &pwr_info->gear_rx);
1717 if (!pwr_info->gear_rx) {
1718 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
1719 __func__, pwr_info->gear_rx);
1720 return -EINVAL;
1721 }
1722 pwr_info->pwr_rx = SLOW_MODE;
1723 }
1724
1725 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
1726 &pwr_info->gear_tx);
1727 if (!pwr_info->gear_tx) {
1728 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
1729 &pwr_info->gear_tx);
1730 if (!pwr_info->gear_tx) {
1731 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
1732 __func__, pwr_info->gear_tx);
1733 return -EINVAL;
1734 }
1735 pwr_info->pwr_tx = SLOW_MODE;
1736 }
1737
1738 hba->max_pwr_info.is_valid = true;
1739 return 0;
1740}
1741
1742static int ufshcd_change_power_mode(struct ufs_hba *hba,
1743 struct ufs_pa_layer_attr *pwr_mode)
1744{
1745 int ret;
1746
1747 /* if already configured to the requested pwr_mode */
1748 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
1749 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
1750 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
1751 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
1752 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
1753 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
1754 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
1755 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
1756 return 0;
1757 }
1758
1759 /*
1760 * Configure attributes for power mode change with below.
1761 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
1762 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
1763 * - PA_HSSERIES
1764 */
1765 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
1766 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
1767 pwr_mode->lane_rx);
1768 if (pwr_mode->pwr_rx == FASTAUTO_MODE || pwr_mode->pwr_rx == FAST_MODE)
1769 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
1770 else
1771 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
1772
1773 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
1774 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
1775 pwr_mode->lane_tx);
1776 if (pwr_mode->pwr_tx == FASTAUTO_MODE || pwr_mode->pwr_tx == FAST_MODE)
1777 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
1778 else
1779 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
1780
1781 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
1782 pwr_mode->pwr_tx == FASTAUTO_MODE ||
1783 pwr_mode->pwr_rx == FAST_MODE ||
1784 pwr_mode->pwr_tx == FAST_MODE)
1785 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
1786 pwr_mode->hs_rate);
1787
1788 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4 |
1789 pwr_mode->pwr_tx);
1790
1791 if (ret) {
1792 dev_err(hba->dev,
1793 "%s: power mode change failed %d\n", __func__, ret);
1794
1795 return ret;
1796 }
1797
1798 /* Copy new Power Mode to power info */
1799 memcpy(&hba->pwr_info, pwr_mode, sizeof(struct ufs_pa_layer_attr));
1800
1801 return ret;
1802}
1803
1804/**
1805 * ufshcd_verify_dev_init() - Verify device initialization
1806 *
1807 */
1808static int ufshcd_verify_dev_init(struct ufs_hba *hba)
1809{
1810 int retries;
1811 int err;
1812
1813 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
1814 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
1815 NOP_OUT_TIMEOUT);
1816 if (!err || err == -ETIMEDOUT)
1817 break;
1818
1819 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
1820 }
1821
1822 if (err)
1823 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
1824
1825 return err;
1826}
1827
1828/**
1829 * ufshcd_complete_dev_init() - checks device readiness
1830 */
1831static int ufshcd_complete_dev_init(struct ufs_hba *hba)
1832{
1833 int i;
1834 int err;
1835 bool flag_res = 1;
1836
1837 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
1838 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
1839 if (err) {
1840 dev_err(hba->dev,
1841 "%s setting fDeviceInit flag failed with error %d\n",
1842 __func__, err);
1843 goto out;
1844 }
1845
1846 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
1847 for (i = 0; i < 1000 && !err && flag_res; i++)
1848 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
1849 QUERY_FLAG_IDN_FDEVICEINIT,
1850 &flag_res);
1851
1852 if (err)
1853 dev_err(hba->dev,
1854 "%s reading fDeviceInit flag failed with error %d\n",
1855 __func__, err);
1856 else if (flag_res)
1857 dev_err(hba->dev,
1858 "%s fDeviceInit was not cleared by the device\n",
1859 __func__);
1860
1861out:
1862 return err;
1863}
1864
1865static void ufshcd_def_desc_sizes(struct ufs_hba *hba)
1866{
1867 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
1868 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
1869 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
1870 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
1871 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
1872 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
1873 hba->desc_size.hlth_desc = QUERY_DESC_HEALTH_DEF_SIZE;
1874}
1875
1876int ufs_start(struct ufs_hba *hba)
1877{
1878 struct ufs_dev_desc card = {0};
1879 int ret;
1880
1881 ret = ufshcd_link_startup(hba);
1882 if (ret)
1883 return ret;
1884
1885 ret = ufshcd_verify_dev_init(hba);
1886 if (ret)
1887 return ret;
1888
1889 ret = ufshcd_complete_dev_init(hba);
1890 if (ret)
1891 return ret;
1892
1893 /* Init check for device descriptor sizes */
1894 ufshcd_init_desc_sizes(hba);
1895
1896 ret = ufs_get_device_desc(hba, &card);
1897 if (ret) {
1898 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
1899 __func__, ret);
1900
1901 return ret;
1902 }
1903
1904 if (ufshcd_get_max_pwr_mode(hba)) {
1905 dev_err(hba->dev,
1906 "%s: Failed getting max supported power mode\n",
1907 __func__);
1908 } else {
1909 ret = ufshcd_change_power_mode(hba, &hba->max_pwr_info.info);
1910 if (ret) {
1911 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
1912 __func__, ret);
1913
1914 return ret;
1915 }
1916
1917 printf("Device at %s up at:", hba->dev->name);
1918 ufshcd_print_pwr_info(hba);
1919 }
1920
1921 return 0;
1922}
1923
1924int ufshcd_probe(struct udevice *ufs_dev, struct ufs_hba_ops *hba_ops)
1925{
1926 struct ufs_hba *hba = dev_get_uclass_priv(ufs_dev);
Simon Glass8a8d24b2020-12-03 16:55:23 -07001927 struct scsi_plat *scsi_plat;
Faiz Abbas7feafb02019-10-15 18:24:36 +05301928 struct udevice *scsi_dev;
Bin Menge5c19ce2023-10-11 21:15:49 +08001929 void __iomem *mmio_base;
Faiz Abbas7feafb02019-10-15 18:24:36 +05301930 int err;
1931
1932 device_find_first_child(ufs_dev, &scsi_dev);
1933 if (!scsi_dev)
1934 return -ENODEV;
1935
Simon Glasscaa4daa2020-12-03 16:55:18 -07001936 scsi_plat = dev_get_uclass_plat(scsi_dev);
Faiz Abbas7feafb02019-10-15 18:24:36 +05301937 scsi_plat->max_id = UFSHCD_MAX_ID;
1938 scsi_plat->max_lun = UFS_MAX_LUNS;
1939 scsi_plat->max_bytes_per_req = UFS_MAX_BYTES;
1940
1941 hba->dev = ufs_dev;
1942 hba->ops = hba_ops;
Bin Menge5c19ce2023-10-11 21:15:49 +08001943
1944 if (device_is_on_pci_bus(ufs_dev)) {
1945 mmio_base = dm_pci_map_bar(ufs_dev, PCI_BASE_ADDRESS_0, 0, 0,
1946 PCI_REGION_TYPE, PCI_REGION_MEM);
1947 } else {
1948 mmio_base = dev_read_addr_ptr(ufs_dev);
1949 }
1950 hba->mmio_base = mmio_base;
Faiz Abbas7feafb02019-10-15 18:24:36 +05301951
1952 /* Set descriptor lengths to specification defaults */
1953 ufshcd_def_desc_sizes(hba);
1954
1955 ufshcd_ops_init(hba);
1956
1957 /* Read capabilties registers */
1958 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Marek Vasut91913a12023-08-16 17:05:50 +02001959 if (hba->quirks & UFSHCD_QUIRK_BROKEN_64BIT_ADDRESS)
1960 hba->capabilities &= ~MASK_64_ADDRESSING_SUPPORT;
Faiz Abbas7feafb02019-10-15 18:24:36 +05301961
1962 /* Get UFS version supported by the controller */
1963 hba->version = ufshcd_get_ufs_version(hba);
1964 if (hba->version != UFSHCI_VERSION_10 &&
1965 hba->version != UFSHCI_VERSION_11 &&
1966 hba->version != UFSHCI_VERSION_20 &&
Marek Vasut2ff810a2023-08-16 17:05:52 +02001967 hba->version != UFSHCI_VERSION_21 &&
Bin Meng5b2d25a2023-10-11 21:15:51 +08001968 hba->version != UFSHCI_VERSION_30 &&
1969 hba->version != UFSHCI_VERSION_31)
Faiz Abbas7feafb02019-10-15 18:24:36 +05301970 dev_err(hba->dev, "invalid UFS version 0x%x\n",
1971 hba->version);
1972
1973 /* Get Interrupt bit mask per version */
1974 hba->intr_mask = ufshcd_get_intr_mask(hba);
1975
1976 /* Allocate memory for host memory space */
1977 err = ufshcd_memory_alloc(hba);
1978 if (err) {
1979 dev_err(hba->dev, "Memory allocation failed\n");
1980 return err;
1981 }
1982
1983 /* Configure Local data structures */
1984 ufshcd_host_memory_configure(hba);
1985
1986 /*
1987 * In order to avoid any spurious interrupt immediately after
1988 * registering UFS controller interrupt handler, clear any pending UFS
1989 * interrupt status and disable all the UFS interrupts.
1990 */
1991 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
1992 REG_INTERRUPT_STATUS);
1993 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
1994
1995 err = ufshcd_hba_enable(hba);
1996 if (err) {
1997 dev_err(hba->dev, "Host controller enable failed\n");
1998 return err;
1999 }
2000
2001 err = ufs_start(hba);
2002 if (err)
2003 return err;
2004
2005 return 0;
2006}
2007
2008int ufs_scsi_bind(struct udevice *ufs_dev, struct udevice **scsi_devp)
2009{
2010 int ret = device_bind_driver(ufs_dev, "ufs_scsi", "ufs_scsi",
2011 scsi_devp);
2012
2013 return ret;
2014}
2015
Marek Vasut91913a12023-08-16 17:05:50 +02002016#if IS_ENABLED(CONFIG_BOUNCE_BUFFER)
2017static int ufs_scsi_buffer_aligned(struct udevice *scsi_dev, struct bounce_buffer *state)
2018{
2019#ifdef CONFIG_PHYS_64BIT
2020 struct ufs_hba *hba = dev_get_uclass_priv(scsi_dev->parent);
2021 uintptr_t ubuf = (uintptr_t)state->user_buffer;
2022 size_t len = state->len_aligned;
2023
2024 /* Check if below 32bit boundary */
2025 if ((hba->quirks & UFSHCD_QUIRK_BROKEN_64BIT_ADDRESS) &&
2026 ((ubuf >> 32) || (ubuf + len) >> 32)) {
2027 dev_dbg(scsi_dev, "Buffer above 32bit boundary %lx-%lx\n",
2028 ubuf, ubuf + len);
2029 return 0;
2030 }
2031#endif
2032 return 1;
2033}
2034#endif /* CONFIG_BOUNCE_BUFFER */
2035
Faiz Abbas7feafb02019-10-15 18:24:36 +05302036static struct scsi_ops ufs_ops = {
2037 .exec = ufs_scsi_exec,
Marek Vasut91913a12023-08-16 17:05:50 +02002038#if IS_ENABLED(CONFIG_BOUNCE_BUFFER)
2039 .buffer_aligned = ufs_scsi_buffer_aligned,
2040#endif /* CONFIG_BOUNCE_BUFFER */
Faiz Abbas7feafb02019-10-15 18:24:36 +05302041};
2042
2043int ufs_probe_dev(int index)
2044{
2045 struct udevice *dev;
2046
2047 return uclass_get_device(UCLASS_UFS, index, &dev);
2048}
2049
2050int ufs_probe(void)
2051{
2052 struct udevice *dev;
2053 int ret, i;
2054
2055 for (i = 0;; i++) {
2056 ret = uclass_get_device(UCLASS_UFS, i, &dev);
2057 if (ret == -ENODEV)
2058 break;
2059 }
2060
2061 return 0;
2062}
2063
2064U_BOOT_DRIVER(ufs_scsi) = {
2065 .id = UCLASS_SCSI,
2066 .name = "ufs_scsi",
2067 .ops = &ufs_ops,
2068};