blob: 15d80aed9fd6689cd8faf9a791ceb7b97764ba1a [file] [log] [blame]
Haojian Zhuang20cd3232017-05-31 11:00:15 +08001/*
Wing Lib7e93082021-12-23 11:32:08 -08002 * Copyright (c) 2017-2021, ARM Limited and Contributors. All rights reserved.
Haojian Zhuang20cd3232017-05-31 11:00:15 +08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Haojian Zhuang20cd3232017-05-31 11:00:15 +08007#include <assert.h>
Haojian Zhuang20cd3232017-05-31 11:00:15 +08008#include <endian.h>
9#include <errno.h>
Haojian Zhuang20cd3232017-05-31 11:00:15 +080010#include <stdint.h>
11#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012
13#include <platform_def.h>
14
15#include <arch_helpers.h>
16#include <common/debug.h>
17#include <drivers/delay_timer.h>
18#include <drivers/ufs.h>
19#include <lib/mmio.h>
Haojian Zhuang20cd3232017-05-31 11:00:15 +080020
21#define CDB_ADDR_MASK 127
22#define ALIGN_CDB(x) (((x) + CDB_ADDR_MASK) & ~CDB_ADDR_MASK)
23#define ALIGN_8(x) (((x) + 7) & ~7)
24
25#define UFS_DESC_SIZE 0x400
26#define MAX_UFS_DESC_SIZE 0x8000 /* 32 descriptors */
27
28#define MAX_PRDT_SIZE 0x40000 /* 256KB */
29
30static ufs_params_t ufs_params;
31static int nutrs; /* Number of UTP Transfer Request Slots */
32
33int ufshc_send_uic_cmd(uintptr_t base, uic_cmd_t *cmd)
34{
35 unsigned int data;
36
Jorge Troncoso453e5e72021-09-23 17:14:29 -070037 if (base == 0 || cmd == NULL)
38 return -EINVAL;
39
Haojian Zhuang20cd3232017-05-31 11:00:15 +080040 data = mmio_read_32(base + HCS);
41 if ((data & HCS_UCRDY) == 0)
42 return -EBUSY;
43 mmio_write_32(base + IS, ~0);
44 mmio_write_32(base + UCMDARG1, cmd->arg1);
45 mmio_write_32(base + UCMDARG2, cmd->arg2);
46 mmio_write_32(base + UCMDARG3, cmd->arg3);
47 mmio_write_32(base + UICCMD, cmd->op);
48
49 do {
50 data = mmio_read_32(base + IS);
51 } while ((data & UFS_INT_UCCS) == 0);
52 mmio_write_32(base + IS, UFS_INT_UCCS);
Haojian Zhuang836eadc2017-06-12 22:18:15 +080053 return mmio_read_32(base + UCMDARG2) & CONFIG_RESULT_CODE_MASK;
Haojian Zhuang20cd3232017-05-31 11:00:15 +080054}
55
56int ufshc_dme_get(unsigned int attr, unsigned int idx, unsigned int *val)
57{
58 uintptr_t base;
59 unsigned int data;
Jorge Troncoso453e5e72021-09-23 17:14:29 -070060 int result, retries;
61 uic_cmd_t cmd;
Haojian Zhuang20cd3232017-05-31 11:00:15 +080062
Jorge Troncoso453e5e72021-09-23 17:14:29 -070063 assert(ufs_params.reg_base != 0);
64
65 if (val == NULL)
66 return -EINVAL;
Haojian Zhuang20cd3232017-05-31 11:00:15 +080067
68 base = ufs_params.reg_base;
69 for (retries = 0; retries < 100; retries++) {
70 data = mmio_read_32(base + HCS);
71 if ((data & HCS_UCRDY) != 0)
72 break;
73 mdelay(1);
74 }
75 if (retries >= 100)
76 return -EBUSY;
77
Jorge Troncoso453e5e72021-09-23 17:14:29 -070078 cmd.arg1 = (attr << 16) | GEN_SELECTOR_IDX(idx);
79 cmd.arg2 = 0;
80 cmd.arg3 = 0;
81 cmd.op = DME_GET;
82 for (retries = 0; retries < UFS_UIC_COMMAND_RETRIES; ++retries) {
83 result = ufshc_send_uic_cmd(base, &cmd);
84 if (result == 0)
85 break;
Haojian Zhuang20cd3232017-05-31 11:00:15 +080086 data = mmio_read_32(base + IS);
87 if (data & UFS_INT_UE)
88 return -EINVAL;
Jorge Troncoso453e5e72021-09-23 17:14:29 -070089 }
90 if (retries >= UFS_UIC_COMMAND_RETRIES)
91 return -EIO;
Haojian Zhuang20cd3232017-05-31 11:00:15 +080092
93 *val = mmio_read_32(base + UCMDARG3);
94 return 0;
95}
96
97int ufshc_dme_set(unsigned int attr, unsigned int idx, unsigned int val)
98{
99 uintptr_t base;
100 unsigned int data;
Jorge Troncoso453e5e72021-09-23 17:14:29 -0700101 int result, retries;
102 uic_cmd_t cmd;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800103
104 assert((ufs_params.reg_base != 0));
105
106 base = ufs_params.reg_base;
Jorge Troncoso453e5e72021-09-23 17:14:29 -0700107 cmd.arg1 = (attr << 16) | GEN_SELECTOR_IDX(idx);
108 cmd.arg2 = 0;
109 cmd.arg3 = val;
110 cmd.op = DME_SET;
111
112 for (retries = 0; retries < UFS_UIC_COMMAND_RETRIES; ++retries) {
113 result = ufshc_send_uic_cmd(base, &cmd);
114 if (result == 0)
115 break;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800116 data = mmio_read_32(base + IS);
117 if (data & UFS_INT_UE)
118 return -EINVAL;
Jorge Troncoso453e5e72021-09-23 17:14:29 -0700119 }
120 if (retries >= UFS_UIC_COMMAND_RETRIES)
121 return -EIO;
122
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800123 return 0;
124}
125
Jorge Troncoso5f449162021-09-30 16:29:32 -0700126static int ufshc_hce_enable(uintptr_t base)
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800127{
128 unsigned int data;
Jorge Troncoso5f449162021-09-30 16:29:32 -0700129 int retries;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800130
131 /* Enable Host Controller */
132 mmio_write_32(base + HCE, HCE_ENABLE);
Jorge Troncoso5f449162021-09-30 16:29:32 -0700133
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800134 /* Wait until basic initialization sequence completed */
Jorge Troncoso5f449162021-09-30 16:29:32 -0700135 for (retries = 0; retries < HCE_ENABLE_INNER_RETRIES; ++retries) {
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800136 data = mmio_read_32(base + HCE);
Jorge Troncoso5f449162021-09-30 16:29:32 -0700137 if (data & HCE_ENABLE) {
138 break;
139 }
140 udelay(HCE_ENABLE_TIMEOUT_US);
141 }
142 if (retries >= HCE_ENABLE_INNER_RETRIES) {
143 return -ETIMEDOUT;
144 }
145
146 return 0;
147}
148
149static int ufshc_reset(uintptr_t base)
150{
151 unsigned int data;
152 int retries, result;
153
154 for (retries = 0; retries < HCE_ENABLE_OUTER_RETRIES; ++retries) {
155 result = ufshc_hce_enable(base);
156 if (result == 0) {
157 break;
158 }
159 }
160 if (retries >= HCE_ENABLE_OUTER_RETRIES) {
161 return -EIO;
162 }
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800163
164 /* Enable Interrupts */
165 data = UFS_INT_UCCS | UFS_INT_ULSS | UFS_INT_UE | UFS_INT_UTPES |
166 UFS_INT_DFES | UFS_INT_HCFES | UFS_INT_SBFES;
167 mmio_write_32(base + IE, data);
Jorge Troncoso5f449162021-09-30 16:29:32 -0700168
169 return 0;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800170}
171
Jorge Troncoso63d7c162021-10-05 22:46:35 -0700172static int ufshc_dme_link_startup(uintptr_t base)
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800173{
174 uic_cmd_t cmd;
Jorge Troncoso63d7c162021-10-05 22:46:35 -0700175
176 memset(&cmd, 0, sizeof(cmd));
177 cmd.op = DME_LINKSTARTUP;
178 return ufshc_send_uic_cmd(base, &cmd);
179}
180
181static int ufshc_link_startup(uintptr_t base)
182{
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800183 int data, result;
184 int retries;
185
Jorge Troncoso63d7c162021-10-05 22:46:35 -0700186 for (retries = DME_LINKSTARTUP_RETRIES; retries > 0; retries--) {
187 result = ufshc_dme_link_startup(base);
188 if (result != 0) {
189 /* Reset controller before trying again */
190 result = ufshc_reset(base);
191 if (result != 0) {
192 return result;
193 }
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800194 continue;
Jorge Troncoso63d7c162021-10-05 22:46:35 -0700195 }
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800196 while ((mmio_read_32(base + HCS) & HCS_DP) == 0)
197 ;
198 data = mmio_read_32(base + IS);
199 if (data & UFS_INT_ULSS)
200 mmio_write_32(base + IS, UFS_INT_ULSS);
201 return 0;
202 }
203 return -EIO;
204}
205
206/* Check Door Bell register to get an empty slot */
207static int get_empty_slot(int *slot)
208{
209 unsigned int data;
210 int i;
211
212 data = mmio_read_32(ufs_params.reg_base + UTRLDBR);
213 for (i = 0; i < nutrs; i++) {
214 if ((data & 1) == 0)
215 break;
216 data = data >> 1;
217 }
218 if (i >= nutrs)
219 return -EBUSY;
220 *slot = i;
221 return 0;
222}
223
224static void get_utrd(utp_utrd_t *utrd)
225{
226 uintptr_t base;
227 int slot = 0, result;
228 utrd_header_t *hd;
229
230 assert(utrd != NULL);
231 result = get_empty_slot(&slot);
232 assert(result == 0);
233
234 /* clear utrd */
235 memset((void *)utrd, 0, sizeof(utp_utrd_t));
236 base = ufs_params.desc_base + (slot * UFS_DESC_SIZE);
237 /* clear the descriptor */
238 memset((void *)base, 0, UFS_DESC_SIZE);
239
240 utrd->header = base;
241 utrd->task_tag = slot + 1;
242 /* CDB address should be aligned with 128 bytes */
243 utrd->upiu = ALIGN_CDB(utrd->header + sizeof(utrd_header_t));
244 utrd->resp_upiu = ALIGN_8(utrd->upiu + sizeof(cmd_upiu_t));
245 utrd->size_upiu = utrd->resp_upiu - utrd->upiu;
246 utrd->size_resp_upiu = ALIGN_8(sizeof(resp_upiu_t));
247 utrd->prdt = utrd->resp_upiu + utrd->size_resp_upiu;
248
249 hd = (utrd_header_t *)utrd->header;
250 hd->ucdba = utrd->upiu & UINT32_MAX;
251 hd->ucdbau = (utrd->upiu >> 32) & UINT32_MAX;
252 /* Both RUL and RUO is based on DWORD */
253 hd->rul = utrd->size_resp_upiu >> 2;
254 hd->ruo = utrd->size_upiu >> 2;
255 (void)result;
256}
257
258/*
259 * Prepare UTRD, Command UPIU, Response UPIU.
260 */
261static int ufs_prepare_cmd(utp_utrd_t *utrd, uint8_t op, uint8_t lun,
262 int lba, uintptr_t buf, size_t length)
263{
264 utrd_header_t *hd;
265 cmd_upiu_t *upiu;
266 prdt_t *prdt;
267 unsigned int ulba;
268 unsigned int lba_cnt;
269 int prdt_size;
270
271
272 mmio_write_32(ufs_params.reg_base + UTRLBA,
273 utrd->header & UINT32_MAX);
274 mmio_write_32(ufs_params.reg_base + UTRLBAU,
275 (utrd->upiu >> 32) & UINT32_MAX);
276
277 hd = (utrd_header_t *)utrd->header;
278 upiu = (cmd_upiu_t *)utrd->upiu;
279
280 hd->i = 1;
281 hd->ct = CT_UFS_STORAGE;
282 hd->ocs = OCS_MASK;
283
284 upiu->trans_type = CMD_UPIU;
285 upiu->task_tag = utrd->task_tag;
286 upiu->cdb[0] = op;
287 ulba = (unsigned int)lba;
288 lba_cnt = (unsigned int)(length >> UFS_BLOCK_SHIFT);
289 switch (op) {
290 case CDBCMD_TEST_UNIT_READY:
291 break;
292 case CDBCMD_READ_CAPACITY_10:
293 hd->dd = DD_OUT;
294 upiu->flags = UPIU_FLAGS_R | UPIU_FLAGS_ATTR_S;
295 upiu->lun = lun;
296 break;
297 case CDBCMD_READ_10:
298 hd->dd = DD_OUT;
299 upiu->flags = UPIU_FLAGS_R | UPIU_FLAGS_ATTR_S;
300 upiu->lun = lun;
301 upiu->cdb[1] = RW_WITHOUT_CACHE;
302 /* set logical block address */
303 upiu->cdb[2] = (ulba >> 24) & 0xff;
304 upiu->cdb[3] = (ulba >> 16) & 0xff;
305 upiu->cdb[4] = (ulba >> 8) & 0xff;
306 upiu->cdb[5] = ulba & 0xff;
307 /* set transfer length */
308 upiu->cdb[7] = (lba_cnt >> 8) & 0xff;
309 upiu->cdb[8] = lba_cnt & 0xff;
310 break;
311 case CDBCMD_WRITE_10:
312 hd->dd = DD_IN;
313 upiu->flags = UPIU_FLAGS_W | UPIU_FLAGS_ATTR_S;
314 upiu->lun = lun;
315 upiu->cdb[1] = RW_WITHOUT_CACHE;
316 /* set logical block address */
317 upiu->cdb[2] = (ulba >> 24) & 0xff;
318 upiu->cdb[3] = (ulba >> 16) & 0xff;
319 upiu->cdb[4] = (ulba >> 8) & 0xff;
320 upiu->cdb[5] = ulba & 0xff;
321 /* set transfer length */
322 upiu->cdb[7] = (lba_cnt >> 8) & 0xff;
323 upiu->cdb[8] = lba_cnt & 0xff;
324 break;
325 default:
326 assert(0);
Jonathan Wright39b42212018-03-13 15:24:29 +0000327 break;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800328 }
329 if (hd->dd == DD_IN)
330 flush_dcache_range(buf, length);
331 else if (hd->dd == DD_OUT)
332 inv_dcache_range(buf, length);
333 if (length) {
334 upiu->exp_data_trans_len = htobe32(length);
335 assert(lba_cnt <= UINT16_MAX);
336 prdt = (prdt_t *)utrd->prdt;
337
338 prdt_size = 0;
339 while (length > 0) {
340 prdt->dba = (unsigned int)(buf & UINT32_MAX);
341 prdt->dbau = (unsigned int)((buf >> 32) & UINT32_MAX);
342 /* prdt->dbc counts from 0 */
343 if (length > MAX_PRDT_SIZE) {
344 prdt->dbc = MAX_PRDT_SIZE - 1;
345 length = length - MAX_PRDT_SIZE;
346 } else {
347 prdt->dbc = length - 1;
348 length = 0;
349 }
350 buf += MAX_PRDT_SIZE;
351 prdt++;
352 prdt_size += sizeof(prdt_t);
353 }
354 utrd->size_prdt = ALIGN_8(prdt_size);
355 hd->prdtl = utrd->size_prdt >> 2;
356 hd->prdto = (utrd->size_upiu + utrd->size_resp_upiu) >> 2;
357 }
358
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800359 flush_dcache_range((uintptr_t)utrd->header, UFS_DESC_SIZE);
360 return 0;
361}
362
363static int ufs_prepare_query(utp_utrd_t *utrd, uint8_t op, uint8_t idn,
364 uint8_t index, uint8_t sel,
365 uintptr_t buf, size_t length)
366{
367 utrd_header_t *hd;
368 query_upiu_t *query_upiu;
369
370
371 hd = (utrd_header_t *)utrd->header;
372 query_upiu = (query_upiu_t *)utrd->upiu;
373
374 mmio_write_32(ufs_params.reg_base + UTRLBA,
375 utrd->header & UINT32_MAX);
376 mmio_write_32(ufs_params.reg_base + UTRLBAU,
377 (utrd->header >> 32) & UINT32_MAX);
378
379
380 hd->i = 1;
381 hd->ct = CT_UFS_STORAGE;
382 hd->ocs = OCS_MASK;
383
384 query_upiu->trans_type = QUERY_REQUEST_UPIU;
385 query_upiu->task_tag = utrd->task_tag;
386 query_upiu->ts.desc.opcode = op;
387 query_upiu->ts.desc.idn = idn;
388 query_upiu->ts.desc.index = index;
389 query_upiu->ts.desc.selector = sel;
390 switch (op) {
391 case QUERY_READ_DESC:
392 query_upiu->query_func = QUERY_FUNC_STD_READ;
393 query_upiu->ts.desc.length = htobe16(length);
394 break;
395 case QUERY_WRITE_DESC:
396 query_upiu->query_func = QUERY_FUNC_STD_WRITE;
397 query_upiu->ts.desc.length = htobe16(length);
398 memcpy((void *)(utrd->upiu + sizeof(query_upiu_t)),
399 (void *)buf, length);
400 break;
401 case QUERY_READ_ATTR:
402 case QUERY_READ_FLAG:
403 query_upiu->query_func = QUERY_FUNC_STD_READ;
404 break;
405 case QUERY_CLEAR_FLAG:
406 case QUERY_SET_FLAG:
407 query_upiu->query_func = QUERY_FUNC_STD_WRITE;
408 break;
409 case QUERY_WRITE_ATTR:
410 query_upiu->query_func = QUERY_FUNC_STD_WRITE;
411 memcpy((void *)&query_upiu->ts.attr.value, (void *)buf, length);
412 break;
413 default:
414 assert(0);
Jonathan Wright39b42212018-03-13 15:24:29 +0000415 break;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800416 }
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800417 flush_dcache_range((uintptr_t)utrd->header, UFS_DESC_SIZE);
418 return 0;
419}
420
421static void ufs_prepare_nop_out(utp_utrd_t *utrd)
422{
423 utrd_header_t *hd;
424 nop_out_upiu_t *nop_out;
425
426 mmio_write_32(ufs_params.reg_base + UTRLBA,
427 utrd->header & UINT32_MAX);
428 mmio_write_32(ufs_params.reg_base + UTRLBAU,
429 (utrd->header >> 32) & UINT32_MAX);
430
431 hd = (utrd_header_t *)utrd->header;
432 nop_out = (nop_out_upiu_t *)utrd->upiu;
433
434 hd->i = 1;
435 hd->ct = CT_UFS_STORAGE;
436 hd->ocs = OCS_MASK;
437
438 nop_out->trans_type = 0;
439 nop_out->task_tag = utrd->task_tag;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800440 flush_dcache_range((uintptr_t)utrd->header, UFS_DESC_SIZE);
441}
442
443static void ufs_send_request(int task_tag)
444{
445 unsigned int data;
446 int slot;
447
448 slot = task_tag - 1;
449 /* clear all interrupts */
450 mmio_write_32(ufs_params.reg_base + IS, ~0);
451
452 mmio_write_32(ufs_params.reg_base + UTRLRSR, 1);
453 do {
454 data = mmio_read_32(ufs_params.reg_base + UTRLRSR);
455 } while (data == 0);
456
457 data = UTRIACR_IAEN | UTRIACR_CTR | UTRIACR_IACTH(0x1F) |
458 UTRIACR_IATOVAL(0xFF);
459 mmio_write_32(ufs_params.reg_base + UTRIACR, data);
460 /* send request */
461 mmio_setbits_32(ufs_params.reg_base + UTRLDBR, 1 << slot);
462}
463
464static int ufs_check_resp(utp_utrd_t *utrd, int trans_type)
465{
466 utrd_header_t *hd;
467 resp_upiu_t *resp;
468 unsigned int data;
469 int slot;
470
471 hd = (utrd_header_t *)utrd->header;
472 resp = (resp_upiu_t *)utrd->resp_upiu;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800473 do {
474 data = mmio_read_32(ufs_params.reg_base + IS);
475 if ((data & ~(UFS_INT_UCCS | UFS_INT_UTRCS)) != 0)
476 return -EIO;
477 } while ((data & UFS_INT_UTRCS) == 0);
478 slot = utrd->task_tag - 1;
479
480 data = mmio_read_32(ufs_params.reg_base + UTRLDBR);
481 assert((data & (1 << slot)) == 0);
Channagoud kadabie57e5802022-03-14 18:56:03 -0700482 /*
483 * Invalidate the header after DMA read operation has
484 * completed to avoid cpu referring to the prefetched
485 * data brought in before DMA completion.
486 */
487 inv_dcache_range((uintptr_t)hd, UFS_DESC_SIZE);
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800488 assert(hd->ocs == OCS_SUCCESS);
489 assert((resp->trans_type & TRANS_TYPE_CODE_MASK) == trans_type);
490 (void)resp;
491 (void)slot;
492 return 0;
493}
494
anans9cbd2b02022-03-11 20:07:39 +0530495static void ufs_send_cmd(utp_utrd_t *utrd, uint8_t cmd_op, uint8_t lun, int lba, uintptr_t buf,
496 size_t length)
497{
498 int result;
499
500 get_utrd(utrd);
501
502 result = ufs_prepare_cmd(utrd, cmd_op, lun, lba, buf, length);
503 assert(result == 0);
504 ufs_send_request(utrd->task_tag);
505 result = ufs_check_resp(utrd, RESPONSE_UPIU);
506 assert(result == 0);
507 (void)result;
508}
509
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800510#ifdef UFS_RESP_DEBUG
511static void dump_upiu(utp_utrd_t *utrd)
512{
513 utrd_header_t *hd;
514 int i;
515
516 hd = (utrd_header_t *)utrd->header;
517 INFO("utrd:0x%x, ruo:0x%x, rul:0x%x, ocs:0x%x, UTRLDBR:0x%x\n",
518 (unsigned int)(uintptr_t)utrd, hd->ruo, hd->rul, hd->ocs,
519 mmio_read_32(ufs_params.reg_base + UTRLDBR));
520 for (i = 0; i < sizeof(utrd_header_t); i += 4) {
521 INFO("[%lx]:0x%x\n",
522 (uintptr_t)utrd->header + i,
523 *(unsigned int *)((uintptr_t)utrd->header + i));
524 }
525
526 for (i = 0; i < sizeof(cmd_upiu_t); i += 4) {
527 INFO("cmd[%lx]:0x%x\n",
528 utrd->upiu + i,
529 *(unsigned int *)(utrd->upiu + i));
530 }
531 for (i = 0; i < sizeof(resp_upiu_t); i += 4) {
532 INFO("resp[%lx]:0x%x\n",
533 utrd->resp_upiu + i,
534 *(unsigned int *)(utrd->resp_upiu + i));
535 }
536 for (i = 0; i < sizeof(prdt_t); i += 4) {
537 INFO("prdt[%lx]:0x%x\n",
538 utrd->prdt + i,
539 *(unsigned int *)(utrd->prdt + i));
540 }
541}
542#endif
543
544static void ufs_verify_init(void)
545{
546 utp_utrd_t utrd;
547 int result;
548
549 get_utrd(&utrd);
550 ufs_prepare_nop_out(&utrd);
551 ufs_send_request(utrd.task_tag);
552 result = ufs_check_resp(&utrd, NOP_IN_UPIU);
553 assert(result == 0);
554 (void)result;
555}
556
557static void ufs_verify_ready(void)
558{
559 utp_utrd_t utrd;
anans9cbd2b02022-03-11 20:07:39 +0530560 ufs_send_cmd(&utrd, CDBCMD_TEST_UNIT_READY, 0, 0, 0, 0);
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800561}
562
563static void ufs_query(uint8_t op, uint8_t idn, uint8_t index, uint8_t sel,
564 uintptr_t buf, size_t size)
565{
566 utp_utrd_t utrd;
567 query_resp_upiu_t *resp;
568 int result;
569
570 switch (op) {
571 case QUERY_READ_FLAG:
572 case QUERY_READ_ATTR:
573 case QUERY_READ_DESC:
574 case QUERY_WRITE_DESC:
575 case QUERY_WRITE_ATTR:
576 assert(((buf & 3) == 0) && (size != 0));
577 break;
Jonathan Wright39b42212018-03-13 15:24:29 +0000578 default:
579 /* Do nothing in default case */
580 break;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800581 }
582 get_utrd(&utrd);
583 ufs_prepare_query(&utrd, op, idn, index, sel, buf, size);
584 ufs_send_request(utrd.task_tag);
585 result = ufs_check_resp(&utrd, QUERY_RESPONSE_UPIU);
586 assert(result == 0);
587 resp = (query_resp_upiu_t *)utrd.resp_upiu;
588#ifdef UFS_RESP_DEBUG
589 dump_upiu(&utrd);
590#endif
591 assert(resp->query_resp == QUERY_RESP_SUCCESS);
592
593 switch (op) {
594 case QUERY_READ_FLAG:
595 *(uint32_t *)buf = (uint32_t)resp->ts.flag.value;
596 break;
597 case QUERY_READ_ATTR:
598 case QUERY_READ_DESC:
599 memcpy((void *)buf,
600 (void *)(utrd.resp_upiu + sizeof(query_resp_upiu_t)),
601 size);
602 break;
Jonathan Wright39b42212018-03-13 15:24:29 +0000603 default:
604 /* Do nothing in default case */
605 break;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800606 }
607 (void)result;
608}
609
610unsigned int ufs_read_attr(int idn)
611{
612 unsigned int value;
613
614 ufs_query(QUERY_READ_ATTR, idn, 0, 0,
615 (uintptr_t)&value, sizeof(value));
616 return value;
617}
618
619void ufs_write_attr(int idn, unsigned int value)
620{
621 ufs_query(QUERY_WRITE_ATTR, idn, 0, 0,
622 (uintptr_t)&value, sizeof(value));
623}
624
625unsigned int ufs_read_flag(int idn)
626{
627 unsigned int value;
628
629 ufs_query(QUERY_READ_FLAG, idn, 0, 0,
630 (uintptr_t)&value, sizeof(value));
631 return value;
632}
633
634void ufs_set_flag(int idn)
635{
636 ufs_query(QUERY_SET_FLAG, idn, 0, 0, 0, 0);
637}
638
639void ufs_clear_flag(int idn)
640{
641 ufs_query(QUERY_CLEAR_FLAG, idn, 0, 0, 0, 0);
642}
643
644void ufs_read_desc(int idn, int index, uintptr_t buf, size_t size)
645{
646 ufs_query(QUERY_READ_DESC, idn, index, 0, buf, size);
647}
648
649void ufs_write_desc(int idn, int index, uintptr_t buf, size_t size)
650{
651 ufs_query(QUERY_WRITE_DESC, idn, index, 0, buf, size);
652}
653
Florian La Roche231c2442019-01-27 14:30:12 +0100654static void ufs_read_capacity(int lun, unsigned int *num, unsigned int *size)
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800655{
656 utp_utrd_t utrd;
657 resp_upiu_t *resp;
658 sense_data_t *sense;
659 unsigned char data[CACHE_WRITEBACK_GRANULE << 1];
660 uintptr_t buf;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800661 int retry;
662
663 assert((ufs_params.reg_base != 0) &&
664 (ufs_params.desc_base != 0) &&
665 (ufs_params.desc_size >= UFS_DESC_SIZE) &&
666 (num != NULL) && (size != NULL));
667
668 /* align buf address */
669 buf = (uintptr_t)data;
670 buf = (buf + CACHE_WRITEBACK_GRANULE - 1) &
671 ~(CACHE_WRITEBACK_GRANULE - 1);
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800672 do {
anans9cbd2b02022-03-11 20:07:39 +0530673 ufs_send_cmd(&utrd, CDBCMD_READ_CAPACITY_10, lun, 0,
674 buf, READ_CAPACITY_LENGTH);
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800675#ifdef UFS_RESP_DEBUG
676 dump_upiu(&utrd);
677#endif
678 resp = (resp_upiu_t *)utrd.resp_upiu;
679 retry = 0;
680 sense = &resp->sd.sense;
681 if (sense->resp_code == SENSE_DATA_VALID) {
682 if ((sense->sense_key == SENSE_KEY_UNIT_ATTENTION) &&
683 (sense->asc == 0x29) && (sense->ascq == 0)) {
684 retry = 1;
685 }
686 }
687 inv_dcache_range(buf, CACHE_WRITEBACK_GRANULE);
688 /* last logical block address */
689 *num = be32toh(*(unsigned int *)buf);
690 if (*num)
691 *num += 1;
692 /* logical block length in bytes */
693 *size = be32toh(*(unsigned int *)(buf + 4));
694 } while (retry);
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800695}
696
697size_t ufs_read_blocks(int lun, int lba, uintptr_t buf, size_t size)
698{
699 utp_utrd_t utrd;
700 resp_upiu_t *resp;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800701
702 assert((ufs_params.reg_base != 0) &&
703 (ufs_params.desc_base != 0) &&
704 (ufs_params.desc_size >= UFS_DESC_SIZE));
705
anans9cbd2b02022-03-11 20:07:39 +0530706 ufs_send_cmd(&utrd, CDBCMD_READ_10, lun, lba, buf, size);
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800707#ifdef UFS_RESP_DEBUG
708 dump_upiu(&utrd);
709#endif
Channagoud kadabie57e5802022-03-14 18:56:03 -0700710 /*
711 * Invalidate prefetched cache contents before cpu
712 * accesses the buf.
713 */
714 inv_dcache_range(buf, size);
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800715 resp = (resp_upiu_t *)utrd.resp_upiu;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800716 return size - resp->res_trans_cnt;
717}
718
719size_t ufs_write_blocks(int lun, int lba, const uintptr_t buf, size_t size)
720{
721 utp_utrd_t utrd;
722 resp_upiu_t *resp;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800723
724 assert((ufs_params.reg_base != 0) &&
725 (ufs_params.desc_base != 0) &&
726 (ufs_params.desc_size >= UFS_DESC_SIZE));
727
anans9cbd2b02022-03-11 20:07:39 +0530728 ufs_send_cmd(&utrd, CDBCMD_WRITE_10, lun, lba, buf, size);
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800729#ifdef UFS_RESP_DEBUG
730 dump_upiu(&utrd);
731#endif
732 resp = (resp_upiu_t *)utrd.resp_upiu;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800733 return size - resp->res_trans_cnt;
734}
735
736static void ufs_enum(void)
737{
738 unsigned int blk_num, blk_size;
739 int i;
740
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800741 ufs_verify_init();
742 ufs_verify_ready();
743
744 ufs_set_flag(FLAG_DEVICE_INIT);
John Stultz938d1dc2019-05-13 16:56:19 -0700745 mdelay(200);
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800746 /* dump available LUNs */
747 for (i = 0; i < UFS_MAX_LUNS; i++) {
748 ufs_read_capacity(i, &blk_num, &blk_size);
749 if (blk_num && blk_size) {
750 INFO("UFS LUN%d contains %d blocks with %d-byte size\n",
751 i, blk_num, blk_size);
752 }
753 }
754}
755
fengbaopeng44070ef2018-02-12 20:53:54 +0800756static void ufs_get_device_info(struct ufs_dev_desc *card_data)
757{
758 uint8_t desc_buf[DESC_DEVICE_MAX_SIZE];
759
760 ufs_query(QUERY_READ_DESC, DESC_TYPE_DEVICE, 0, 0,
761 (uintptr_t)desc_buf, DESC_DEVICE_MAX_SIZE);
762
763 /*
764 * getting vendor (manufacturerID) and Bank Index in big endian
765 * format
766 */
767 card_data->wmanufacturerid = (uint16_t)((desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8) |
768 (desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1]));
769}
770
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800771int ufs_init(const ufs_ops_t *ops, ufs_params_t *params)
772{
773 int result;
774 unsigned int data;
775 uic_cmd_t cmd;
fengbaopeng44070ef2018-02-12 20:53:54 +0800776 struct ufs_dev_desc card = {0};
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800777
778 assert((params != NULL) &&
779 (params->reg_base != 0) &&
780 (params->desc_base != 0) &&
781 (params->desc_size >= UFS_DESC_SIZE));
782
783 memcpy(&ufs_params, params, sizeof(ufs_params_t));
784
anans3696bd02022-03-15 13:37:37 +0530785 /* 0 means 1 slot */
786 nutrs = (mmio_read_32(ufs_params.reg_base + CAP) & CAP_NUTRS_MASK) + 1;
787 if (nutrs > (ufs_params.desc_size / UFS_DESC_SIZE)) {
788 nutrs = ufs_params.desc_size / UFS_DESC_SIZE;
789 }
790
791
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800792 if (ufs_params.flags & UFS_FLAGS_SKIPINIT) {
793 result = ufshc_dme_get(0x1571, 0, &data);
794 assert(result == 0);
795 result = ufshc_dme_get(0x41, 0, &data);
796 assert(result == 0);
797 if (data == 1) {
798 /* prepare to exit hibernate mode */
799 memset(&cmd, 0, sizeof(uic_cmd_t));
800 cmd.op = DME_HIBERNATE_EXIT;
801 result = ufshc_send_uic_cmd(ufs_params.reg_base,
802 &cmd);
803 assert(result == 0);
804 data = mmio_read_32(ufs_params.reg_base + UCMDARG2);
805 assert(data == 0);
806 do {
807 data = mmio_read_32(ufs_params.reg_base + IS);
808 } while ((data & UFS_INT_UHXS) == 0);
809 mmio_write_32(ufs_params.reg_base + IS, UFS_INT_UHXS);
810 data = mmio_read_32(ufs_params.reg_base + HCS);
811 assert((data & HCS_UPMCRS_MASK) == HCS_PWR_LOCAL);
812 }
813 result = ufshc_dme_get(0x1568, 0, &data);
814 assert(result == 0);
815 assert((data > 0) && (data <= 3));
816 } else {
817 assert((ops != NULL) && (ops->phy_init != NULL) &&
818 (ops->phy_set_pwr_mode != NULL));
819
Jorge Troncoso5f449162021-09-30 16:29:32 -0700820 result = ufshc_reset(ufs_params.reg_base);
821 assert(result == 0);
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800822 ops->phy_init(&ufs_params);
823 result = ufshc_link_startup(ufs_params.reg_base);
824 assert(result == 0);
fengbaopeng44070ef2018-02-12 20:53:54 +0800825
826 ufs_enum();
827
828 ufs_get_device_info(&card);
829 if (card.wmanufacturerid == UFS_VENDOR_SKHYNIX) {
830 ufs_params.flags |= UFS_FLAGS_VENDOR_SKHYNIX;
831 }
832
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800833 ops->phy_set_pwr_mode(&ufs_params);
834 }
835
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800836 (void)result;
837 return 0;
838}