blob: 720b732351815644a6d209a461b089d20e047ae3 [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
anansa93159e2022-03-21 09:59:18 +0530149static int ufshc_hce_disable(uintptr_t base)
150{
151 unsigned int data;
152 int timeout;
153
154 /* Disable Host Controller */
155 mmio_write_32(base + HCE, HCE_DISABLE);
156 timeout = HCE_DISABLE_TIMEOUT_US;
157 do {
158 data = mmio_read_32(base + HCE);
159 if ((data & HCE_ENABLE) == HCE_DISABLE) {
160 break;
161 }
162 udelay(1);
163 } while (--timeout > 0);
164
165 if (timeout <= 0) {
166 return -ETIMEDOUT;
167 }
168
169 return 0;
170}
171
172
Jorge Troncoso5f449162021-09-30 16:29:32 -0700173static int ufshc_reset(uintptr_t base)
174{
175 unsigned int data;
176 int retries, result;
177
anansa93159e2022-03-21 09:59:18 +0530178 /* disable controller if enabled */
179 if (mmio_read_32(base + HCE) & HCE_ENABLE) {
180 result = ufshc_hce_disable(base);
181 if (result != 0) {
182 return -EIO;
183 }
184 }
185
Jorge Troncoso5f449162021-09-30 16:29:32 -0700186 for (retries = 0; retries < HCE_ENABLE_OUTER_RETRIES; ++retries) {
187 result = ufshc_hce_enable(base);
188 if (result == 0) {
189 break;
190 }
191 }
192 if (retries >= HCE_ENABLE_OUTER_RETRIES) {
193 return -EIO;
194 }
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800195
196 /* Enable Interrupts */
197 data = UFS_INT_UCCS | UFS_INT_ULSS | UFS_INT_UE | UFS_INT_UTPES |
198 UFS_INT_DFES | UFS_INT_HCFES | UFS_INT_SBFES;
199 mmio_write_32(base + IE, data);
Jorge Troncoso5f449162021-09-30 16:29:32 -0700200
201 return 0;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800202}
203
Jorge Troncoso63d7c162021-10-05 22:46:35 -0700204static int ufshc_dme_link_startup(uintptr_t base)
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800205{
206 uic_cmd_t cmd;
Jorge Troncoso63d7c162021-10-05 22:46:35 -0700207
208 memset(&cmd, 0, sizeof(cmd));
209 cmd.op = DME_LINKSTARTUP;
210 return ufshc_send_uic_cmd(base, &cmd);
211}
212
213static int ufshc_link_startup(uintptr_t base)
214{
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800215 int data, result;
216 int retries;
217
Jorge Troncoso63d7c162021-10-05 22:46:35 -0700218 for (retries = DME_LINKSTARTUP_RETRIES; retries > 0; retries--) {
219 result = ufshc_dme_link_startup(base);
220 if (result != 0) {
221 /* Reset controller before trying again */
222 result = ufshc_reset(base);
223 if (result != 0) {
224 return result;
225 }
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800226 continue;
Jorge Troncoso63d7c162021-10-05 22:46:35 -0700227 }
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800228 while ((mmio_read_32(base + HCS) & HCS_DP) == 0)
229 ;
230 data = mmio_read_32(base + IS);
231 if (data & UFS_INT_ULSS)
232 mmio_write_32(base + IS, UFS_INT_ULSS);
233 return 0;
234 }
235 return -EIO;
236}
237
238/* Check Door Bell register to get an empty slot */
239static int get_empty_slot(int *slot)
240{
241 unsigned int data;
242 int i;
243
244 data = mmio_read_32(ufs_params.reg_base + UTRLDBR);
245 for (i = 0; i < nutrs; i++) {
246 if ((data & 1) == 0)
247 break;
248 data = data >> 1;
249 }
250 if (i >= nutrs)
251 return -EBUSY;
252 *slot = i;
253 return 0;
254}
255
256static void get_utrd(utp_utrd_t *utrd)
257{
258 uintptr_t base;
259 int slot = 0, result;
260 utrd_header_t *hd;
261
262 assert(utrd != NULL);
263 result = get_empty_slot(&slot);
264 assert(result == 0);
265
266 /* clear utrd */
267 memset((void *)utrd, 0, sizeof(utp_utrd_t));
268 base = ufs_params.desc_base + (slot * UFS_DESC_SIZE);
269 /* clear the descriptor */
270 memset((void *)base, 0, UFS_DESC_SIZE);
271
272 utrd->header = base;
273 utrd->task_tag = slot + 1;
274 /* CDB address should be aligned with 128 bytes */
275 utrd->upiu = ALIGN_CDB(utrd->header + sizeof(utrd_header_t));
276 utrd->resp_upiu = ALIGN_8(utrd->upiu + sizeof(cmd_upiu_t));
277 utrd->size_upiu = utrd->resp_upiu - utrd->upiu;
278 utrd->size_resp_upiu = ALIGN_8(sizeof(resp_upiu_t));
279 utrd->prdt = utrd->resp_upiu + utrd->size_resp_upiu;
280
281 hd = (utrd_header_t *)utrd->header;
282 hd->ucdba = utrd->upiu & UINT32_MAX;
283 hd->ucdbau = (utrd->upiu >> 32) & UINT32_MAX;
284 /* Both RUL and RUO is based on DWORD */
285 hd->rul = utrd->size_resp_upiu >> 2;
286 hd->ruo = utrd->size_upiu >> 2;
287 (void)result;
288}
289
290/*
291 * Prepare UTRD, Command UPIU, Response UPIU.
292 */
293static int ufs_prepare_cmd(utp_utrd_t *utrd, uint8_t op, uint8_t lun,
294 int lba, uintptr_t buf, size_t length)
295{
296 utrd_header_t *hd;
297 cmd_upiu_t *upiu;
298 prdt_t *prdt;
299 unsigned int ulba;
300 unsigned int lba_cnt;
301 int prdt_size;
302
303
304 mmio_write_32(ufs_params.reg_base + UTRLBA,
305 utrd->header & UINT32_MAX);
306 mmio_write_32(ufs_params.reg_base + UTRLBAU,
307 (utrd->upiu >> 32) & UINT32_MAX);
308
309 hd = (utrd_header_t *)utrd->header;
310 upiu = (cmd_upiu_t *)utrd->upiu;
311
312 hd->i = 1;
313 hd->ct = CT_UFS_STORAGE;
314 hd->ocs = OCS_MASK;
315
316 upiu->trans_type = CMD_UPIU;
317 upiu->task_tag = utrd->task_tag;
318 upiu->cdb[0] = op;
319 ulba = (unsigned int)lba;
320 lba_cnt = (unsigned int)(length >> UFS_BLOCK_SHIFT);
321 switch (op) {
322 case CDBCMD_TEST_UNIT_READY:
323 break;
324 case CDBCMD_READ_CAPACITY_10:
325 hd->dd = DD_OUT;
326 upiu->flags = UPIU_FLAGS_R | UPIU_FLAGS_ATTR_S;
327 upiu->lun = lun;
328 break;
329 case CDBCMD_READ_10:
330 hd->dd = DD_OUT;
331 upiu->flags = UPIU_FLAGS_R | UPIU_FLAGS_ATTR_S;
332 upiu->lun = lun;
333 upiu->cdb[1] = RW_WITHOUT_CACHE;
334 /* set logical block address */
335 upiu->cdb[2] = (ulba >> 24) & 0xff;
336 upiu->cdb[3] = (ulba >> 16) & 0xff;
337 upiu->cdb[4] = (ulba >> 8) & 0xff;
338 upiu->cdb[5] = ulba & 0xff;
339 /* set transfer length */
340 upiu->cdb[7] = (lba_cnt >> 8) & 0xff;
341 upiu->cdb[8] = lba_cnt & 0xff;
342 break;
343 case CDBCMD_WRITE_10:
344 hd->dd = DD_IN;
345 upiu->flags = UPIU_FLAGS_W | UPIU_FLAGS_ATTR_S;
346 upiu->lun = lun;
347 upiu->cdb[1] = RW_WITHOUT_CACHE;
348 /* set logical block address */
349 upiu->cdb[2] = (ulba >> 24) & 0xff;
350 upiu->cdb[3] = (ulba >> 16) & 0xff;
351 upiu->cdb[4] = (ulba >> 8) & 0xff;
352 upiu->cdb[5] = ulba & 0xff;
353 /* set transfer length */
354 upiu->cdb[7] = (lba_cnt >> 8) & 0xff;
355 upiu->cdb[8] = lba_cnt & 0xff;
356 break;
357 default:
358 assert(0);
Jonathan Wright39b42212018-03-13 15:24:29 +0000359 break;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800360 }
361 if (hd->dd == DD_IN)
362 flush_dcache_range(buf, length);
363 else if (hd->dd == DD_OUT)
364 inv_dcache_range(buf, length);
365 if (length) {
366 upiu->exp_data_trans_len = htobe32(length);
367 assert(lba_cnt <= UINT16_MAX);
368 prdt = (prdt_t *)utrd->prdt;
369
370 prdt_size = 0;
371 while (length > 0) {
372 prdt->dba = (unsigned int)(buf & UINT32_MAX);
373 prdt->dbau = (unsigned int)((buf >> 32) & UINT32_MAX);
374 /* prdt->dbc counts from 0 */
375 if (length > MAX_PRDT_SIZE) {
376 prdt->dbc = MAX_PRDT_SIZE - 1;
377 length = length - MAX_PRDT_SIZE;
378 } else {
379 prdt->dbc = length - 1;
380 length = 0;
381 }
382 buf += MAX_PRDT_SIZE;
383 prdt++;
384 prdt_size += sizeof(prdt_t);
385 }
386 utrd->size_prdt = ALIGN_8(prdt_size);
387 hd->prdtl = utrd->size_prdt >> 2;
388 hd->prdto = (utrd->size_upiu + utrd->size_resp_upiu) >> 2;
389 }
390
391 flush_dcache_range((uintptr_t)utrd, sizeof(utp_utrd_t));
392 flush_dcache_range((uintptr_t)utrd->header, UFS_DESC_SIZE);
393 return 0;
394}
395
396static int ufs_prepare_query(utp_utrd_t *utrd, uint8_t op, uint8_t idn,
397 uint8_t index, uint8_t sel,
398 uintptr_t buf, size_t length)
399{
400 utrd_header_t *hd;
401 query_upiu_t *query_upiu;
402
403
404 hd = (utrd_header_t *)utrd->header;
405 query_upiu = (query_upiu_t *)utrd->upiu;
406
407 mmio_write_32(ufs_params.reg_base + UTRLBA,
408 utrd->header & UINT32_MAX);
409 mmio_write_32(ufs_params.reg_base + UTRLBAU,
410 (utrd->header >> 32) & UINT32_MAX);
411
412
413 hd->i = 1;
414 hd->ct = CT_UFS_STORAGE;
415 hd->ocs = OCS_MASK;
416
417 query_upiu->trans_type = QUERY_REQUEST_UPIU;
418 query_upiu->task_tag = utrd->task_tag;
419 query_upiu->ts.desc.opcode = op;
420 query_upiu->ts.desc.idn = idn;
421 query_upiu->ts.desc.index = index;
422 query_upiu->ts.desc.selector = sel;
423 switch (op) {
424 case QUERY_READ_DESC:
425 query_upiu->query_func = QUERY_FUNC_STD_READ;
426 query_upiu->ts.desc.length = htobe16(length);
427 break;
428 case QUERY_WRITE_DESC:
429 query_upiu->query_func = QUERY_FUNC_STD_WRITE;
430 query_upiu->ts.desc.length = htobe16(length);
431 memcpy((void *)(utrd->upiu + sizeof(query_upiu_t)),
432 (void *)buf, length);
433 break;
434 case QUERY_READ_ATTR:
435 case QUERY_READ_FLAG:
436 query_upiu->query_func = QUERY_FUNC_STD_READ;
437 break;
438 case QUERY_CLEAR_FLAG:
439 case QUERY_SET_FLAG:
440 query_upiu->query_func = QUERY_FUNC_STD_WRITE;
441 break;
442 case QUERY_WRITE_ATTR:
443 query_upiu->query_func = QUERY_FUNC_STD_WRITE;
444 memcpy((void *)&query_upiu->ts.attr.value, (void *)buf, length);
445 break;
446 default:
447 assert(0);
Jonathan Wright39b42212018-03-13 15:24:29 +0000448 break;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800449 }
450 flush_dcache_range((uintptr_t)utrd, sizeof(utp_utrd_t));
451 flush_dcache_range((uintptr_t)utrd->header, UFS_DESC_SIZE);
452 return 0;
453}
454
455static void ufs_prepare_nop_out(utp_utrd_t *utrd)
456{
457 utrd_header_t *hd;
458 nop_out_upiu_t *nop_out;
459
460 mmio_write_32(ufs_params.reg_base + UTRLBA,
461 utrd->header & UINT32_MAX);
462 mmio_write_32(ufs_params.reg_base + UTRLBAU,
463 (utrd->header >> 32) & UINT32_MAX);
464
465 hd = (utrd_header_t *)utrd->header;
466 nop_out = (nop_out_upiu_t *)utrd->upiu;
467
468 hd->i = 1;
469 hd->ct = CT_UFS_STORAGE;
470 hd->ocs = OCS_MASK;
471
472 nop_out->trans_type = 0;
473 nop_out->task_tag = utrd->task_tag;
474 flush_dcache_range((uintptr_t)utrd, sizeof(utp_utrd_t));
475 flush_dcache_range((uintptr_t)utrd->header, UFS_DESC_SIZE);
476}
477
478static void ufs_send_request(int task_tag)
479{
480 unsigned int data;
481 int slot;
482
483 slot = task_tag - 1;
484 /* clear all interrupts */
485 mmio_write_32(ufs_params.reg_base + IS, ~0);
486
487 mmio_write_32(ufs_params.reg_base + UTRLRSR, 1);
488 do {
489 data = mmio_read_32(ufs_params.reg_base + UTRLRSR);
490 } while (data == 0);
491
492 data = UTRIACR_IAEN | UTRIACR_CTR | UTRIACR_IACTH(0x1F) |
493 UTRIACR_IATOVAL(0xFF);
494 mmio_write_32(ufs_params.reg_base + UTRIACR, data);
495 /* send request */
496 mmio_setbits_32(ufs_params.reg_base + UTRLDBR, 1 << slot);
497}
498
499static int ufs_check_resp(utp_utrd_t *utrd, int trans_type)
500{
501 utrd_header_t *hd;
502 resp_upiu_t *resp;
503 unsigned int data;
504 int slot;
505
506 hd = (utrd_header_t *)utrd->header;
507 resp = (resp_upiu_t *)utrd->resp_upiu;
508 inv_dcache_range((uintptr_t)hd, UFS_DESC_SIZE);
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800509 do {
510 data = mmio_read_32(ufs_params.reg_base + IS);
511 if ((data & ~(UFS_INT_UCCS | UFS_INT_UTRCS)) != 0)
512 return -EIO;
513 } while ((data & UFS_INT_UTRCS) == 0);
514 slot = utrd->task_tag - 1;
515
516 data = mmio_read_32(ufs_params.reg_base + UTRLDBR);
517 assert((data & (1 << slot)) == 0);
518 assert(hd->ocs == OCS_SUCCESS);
519 assert((resp->trans_type & TRANS_TYPE_CODE_MASK) == trans_type);
520 (void)resp;
521 (void)slot;
522 return 0;
523}
524
anans9cbd2b02022-03-11 20:07:39 +0530525static void ufs_send_cmd(utp_utrd_t *utrd, uint8_t cmd_op, uint8_t lun, int lba, uintptr_t buf,
526 size_t length)
527{
528 int result;
529
530 get_utrd(utrd);
531
532 result = ufs_prepare_cmd(utrd, cmd_op, lun, lba, buf, length);
533 assert(result == 0);
534 ufs_send_request(utrd->task_tag);
535 result = ufs_check_resp(utrd, RESPONSE_UPIU);
536 assert(result == 0);
537 (void)result;
538}
539
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800540#ifdef UFS_RESP_DEBUG
541static void dump_upiu(utp_utrd_t *utrd)
542{
543 utrd_header_t *hd;
544 int i;
545
546 hd = (utrd_header_t *)utrd->header;
547 INFO("utrd:0x%x, ruo:0x%x, rul:0x%x, ocs:0x%x, UTRLDBR:0x%x\n",
548 (unsigned int)(uintptr_t)utrd, hd->ruo, hd->rul, hd->ocs,
549 mmio_read_32(ufs_params.reg_base + UTRLDBR));
550 for (i = 0; i < sizeof(utrd_header_t); i += 4) {
551 INFO("[%lx]:0x%x\n",
552 (uintptr_t)utrd->header + i,
553 *(unsigned int *)((uintptr_t)utrd->header + i));
554 }
555
556 for (i = 0; i < sizeof(cmd_upiu_t); i += 4) {
557 INFO("cmd[%lx]:0x%x\n",
558 utrd->upiu + i,
559 *(unsigned int *)(utrd->upiu + i));
560 }
561 for (i = 0; i < sizeof(resp_upiu_t); i += 4) {
562 INFO("resp[%lx]:0x%x\n",
563 utrd->resp_upiu + i,
564 *(unsigned int *)(utrd->resp_upiu + i));
565 }
566 for (i = 0; i < sizeof(prdt_t); i += 4) {
567 INFO("prdt[%lx]:0x%x\n",
568 utrd->prdt + i,
569 *(unsigned int *)(utrd->prdt + i));
570 }
571}
572#endif
573
574static void ufs_verify_init(void)
575{
576 utp_utrd_t utrd;
577 int result;
578
579 get_utrd(&utrd);
580 ufs_prepare_nop_out(&utrd);
581 ufs_send_request(utrd.task_tag);
582 result = ufs_check_resp(&utrd, NOP_IN_UPIU);
583 assert(result == 0);
584 (void)result;
585}
586
587static void ufs_verify_ready(void)
588{
589 utp_utrd_t utrd;
anans9cbd2b02022-03-11 20:07:39 +0530590 ufs_send_cmd(&utrd, CDBCMD_TEST_UNIT_READY, 0, 0, 0, 0);
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800591}
592
593static void ufs_query(uint8_t op, uint8_t idn, uint8_t index, uint8_t sel,
594 uintptr_t buf, size_t size)
595{
596 utp_utrd_t utrd;
597 query_resp_upiu_t *resp;
598 int result;
599
600 switch (op) {
601 case QUERY_READ_FLAG:
602 case QUERY_READ_ATTR:
603 case QUERY_READ_DESC:
604 case QUERY_WRITE_DESC:
605 case QUERY_WRITE_ATTR:
606 assert(((buf & 3) == 0) && (size != 0));
607 break;
Jonathan Wright39b42212018-03-13 15:24:29 +0000608 default:
609 /* Do nothing in default case */
610 break;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800611 }
612 get_utrd(&utrd);
613 ufs_prepare_query(&utrd, op, idn, index, sel, buf, size);
614 ufs_send_request(utrd.task_tag);
615 result = ufs_check_resp(&utrd, QUERY_RESPONSE_UPIU);
616 assert(result == 0);
617 resp = (query_resp_upiu_t *)utrd.resp_upiu;
618#ifdef UFS_RESP_DEBUG
619 dump_upiu(&utrd);
620#endif
621 assert(resp->query_resp == QUERY_RESP_SUCCESS);
622
623 switch (op) {
624 case QUERY_READ_FLAG:
625 *(uint32_t *)buf = (uint32_t)resp->ts.flag.value;
626 break;
627 case QUERY_READ_ATTR:
628 case QUERY_READ_DESC:
629 memcpy((void *)buf,
630 (void *)(utrd.resp_upiu + sizeof(query_resp_upiu_t)),
631 size);
632 break;
Jonathan Wright39b42212018-03-13 15:24:29 +0000633 default:
634 /* Do nothing in default case */
635 break;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800636 }
637 (void)result;
638}
639
640unsigned int ufs_read_attr(int idn)
641{
642 unsigned int value;
643
644 ufs_query(QUERY_READ_ATTR, idn, 0, 0,
645 (uintptr_t)&value, sizeof(value));
646 return value;
647}
648
649void ufs_write_attr(int idn, unsigned int value)
650{
651 ufs_query(QUERY_WRITE_ATTR, idn, 0, 0,
652 (uintptr_t)&value, sizeof(value));
653}
654
655unsigned int ufs_read_flag(int idn)
656{
657 unsigned int value;
658
659 ufs_query(QUERY_READ_FLAG, idn, 0, 0,
660 (uintptr_t)&value, sizeof(value));
661 return value;
662}
663
664void ufs_set_flag(int idn)
665{
666 ufs_query(QUERY_SET_FLAG, idn, 0, 0, 0, 0);
667}
668
669void ufs_clear_flag(int idn)
670{
671 ufs_query(QUERY_CLEAR_FLAG, idn, 0, 0, 0, 0);
672}
673
674void ufs_read_desc(int idn, int index, uintptr_t buf, size_t size)
675{
676 ufs_query(QUERY_READ_DESC, idn, index, 0, buf, size);
677}
678
679void ufs_write_desc(int idn, int index, uintptr_t buf, size_t size)
680{
681 ufs_query(QUERY_WRITE_DESC, idn, index, 0, buf, size);
682}
683
Florian La Roche231c2442019-01-27 14:30:12 +0100684static void ufs_read_capacity(int lun, unsigned int *num, unsigned int *size)
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800685{
686 utp_utrd_t utrd;
687 resp_upiu_t *resp;
688 sense_data_t *sense;
689 unsigned char data[CACHE_WRITEBACK_GRANULE << 1];
690 uintptr_t buf;
691 int result;
692 int retry;
693
694 assert((ufs_params.reg_base != 0) &&
695 (ufs_params.desc_base != 0) &&
696 (ufs_params.desc_size >= UFS_DESC_SIZE) &&
697 (num != NULL) && (size != NULL));
698
699 /* align buf address */
700 buf = (uintptr_t)data;
701 buf = (buf + CACHE_WRITEBACK_GRANULE - 1) &
702 ~(CACHE_WRITEBACK_GRANULE - 1);
703 memset((void *)buf, 0, CACHE_WRITEBACK_GRANULE);
704 flush_dcache_range(buf, CACHE_WRITEBACK_GRANULE);
705 do {
anans9cbd2b02022-03-11 20:07:39 +0530706 ufs_send_cmd(&utrd, CDBCMD_READ_CAPACITY_10, lun, 0,
707 buf, READ_CAPACITY_LENGTH);
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800708#ifdef UFS_RESP_DEBUG
709 dump_upiu(&utrd);
710#endif
711 resp = (resp_upiu_t *)utrd.resp_upiu;
712 retry = 0;
713 sense = &resp->sd.sense;
714 if (sense->resp_code == SENSE_DATA_VALID) {
715 if ((sense->sense_key == SENSE_KEY_UNIT_ATTENTION) &&
716 (sense->asc == 0x29) && (sense->ascq == 0)) {
717 retry = 1;
718 }
719 }
720 inv_dcache_range(buf, CACHE_WRITEBACK_GRANULE);
721 /* last logical block address */
722 *num = be32toh(*(unsigned int *)buf);
723 if (*num)
724 *num += 1;
725 /* logical block length in bytes */
726 *size = be32toh(*(unsigned int *)(buf + 4));
727 } while (retry);
728 (void)result;
729}
730
731size_t ufs_read_blocks(int lun, int lba, uintptr_t buf, size_t size)
732{
733 utp_utrd_t utrd;
734 resp_upiu_t *resp;
735 int result;
736
737 assert((ufs_params.reg_base != 0) &&
738 (ufs_params.desc_base != 0) &&
739 (ufs_params.desc_size >= UFS_DESC_SIZE));
740
anans9cbd2b02022-03-11 20:07:39 +0530741 ufs_send_cmd(&utrd, CDBCMD_READ_10, lun, lba, buf, size);
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800742#ifdef UFS_RESP_DEBUG
743 dump_upiu(&utrd);
744#endif
745 resp = (resp_upiu_t *)utrd.resp_upiu;
746 (void)result;
747 return size - resp->res_trans_cnt;
748}
749
750size_t ufs_write_blocks(int lun, int lba, const uintptr_t buf, size_t size)
751{
752 utp_utrd_t utrd;
753 resp_upiu_t *resp;
754 int result;
755
756 assert((ufs_params.reg_base != 0) &&
757 (ufs_params.desc_base != 0) &&
758 (ufs_params.desc_size >= UFS_DESC_SIZE));
759
anans9cbd2b02022-03-11 20:07:39 +0530760 ufs_send_cmd(&utrd, CDBCMD_WRITE_10, lun, lba, buf, size);
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800761#ifdef UFS_RESP_DEBUG
762 dump_upiu(&utrd);
763#endif
764 resp = (resp_upiu_t *)utrd.resp_upiu;
765 (void)result;
766 return size - resp->res_trans_cnt;
767}
768
anans44756ec2022-03-21 09:43:11 +0530769static int ufs_set_fdevice_init(void)
770{
771 unsigned int result;
772 int timeout;
773
774 ufs_set_flag(FLAG_DEVICE_INIT);
775
776 timeout = FDEVICEINIT_TIMEOUT_MS;
777 do {
778 result = ufs_read_flag(FLAG_DEVICE_INIT);
779 if (!result) {
780 break;
781 }
782 mdelay(5);
783 timeout -= 5;
784 } while (timeout > 0);
785
786 if (result != 0U) {
787 return -ETIMEDOUT;
788 }
789
790 return 0;
791}
792
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800793static void ufs_enum(void)
794{
795 unsigned int blk_num, blk_size;
anans44756ec2022-03-21 09:43:11 +0530796 int i, result;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800797
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800798 ufs_verify_init();
799 ufs_verify_ready();
800
anans44756ec2022-03-21 09:43:11 +0530801 result = ufs_set_fdevice_init();
802 assert(result == 0);
803
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800804 /* dump available LUNs */
805 for (i = 0; i < UFS_MAX_LUNS; i++) {
806 ufs_read_capacity(i, &blk_num, &blk_size);
807 if (blk_num && blk_size) {
808 INFO("UFS LUN%d contains %d blocks with %d-byte size\n",
809 i, blk_num, blk_size);
810 }
811 }
anans44756ec2022-03-21 09:43:11 +0530812
813 (void)result;
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800814}
815
fengbaopeng44070ef2018-02-12 20:53:54 +0800816static void ufs_get_device_info(struct ufs_dev_desc *card_data)
817{
818 uint8_t desc_buf[DESC_DEVICE_MAX_SIZE];
819
820 ufs_query(QUERY_READ_DESC, DESC_TYPE_DEVICE, 0, 0,
821 (uintptr_t)desc_buf, DESC_DEVICE_MAX_SIZE);
822
823 /*
824 * getting vendor (manufacturerID) and Bank Index in big endian
825 * format
826 */
827 card_data->wmanufacturerid = (uint16_t)((desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8) |
828 (desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1]));
829}
830
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800831int ufs_init(const ufs_ops_t *ops, ufs_params_t *params)
832{
833 int result;
834 unsigned int data;
835 uic_cmd_t cmd;
fengbaopeng44070ef2018-02-12 20:53:54 +0800836 struct ufs_dev_desc card = {0};
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800837
838 assert((params != NULL) &&
839 (params->reg_base != 0) &&
840 (params->desc_base != 0) &&
841 (params->desc_size >= UFS_DESC_SIZE));
842
843 memcpy(&ufs_params, params, sizeof(ufs_params_t));
844
anans3696bd02022-03-15 13:37:37 +0530845 /* 0 means 1 slot */
846 nutrs = (mmio_read_32(ufs_params.reg_base + CAP) & CAP_NUTRS_MASK) + 1;
847 if (nutrs > (ufs_params.desc_size / UFS_DESC_SIZE)) {
848 nutrs = ufs_params.desc_size / UFS_DESC_SIZE;
849 }
850
851
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800852 if (ufs_params.flags & UFS_FLAGS_SKIPINIT) {
853 result = ufshc_dme_get(0x1571, 0, &data);
854 assert(result == 0);
855 result = ufshc_dme_get(0x41, 0, &data);
856 assert(result == 0);
857 if (data == 1) {
858 /* prepare to exit hibernate mode */
859 memset(&cmd, 0, sizeof(uic_cmd_t));
860 cmd.op = DME_HIBERNATE_EXIT;
861 result = ufshc_send_uic_cmd(ufs_params.reg_base,
862 &cmd);
863 assert(result == 0);
864 data = mmio_read_32(ufs_params.reg_base + UCMDARG2);
865 assert(data == 0);
866 do {
867 data = mmio_read_32(ufs_params.reg_base + IS);
868 } while ((data & UFS_INT_UHXS) == 0);
869 mmio_write_32(ufs_params.reg_base + IS, UFS_INT_UHXS);
870 data = mmio_read_32(ufs_params.reg_base + HCS);
871 assert((data & HCS_UPMCRS_MASK) == HCS_PWR_LOCAL);
872 }
873 result = ufshc_dme_get(0x1568, 0, &data);
874 assert(result == 0);
875 assert((data > 0) && (data <= 3));
876 } else {
877 assert((ops != NULL) && (ops->phy_init != NULL) &&
878 (ops->phy_set_pwr_mode != NULL));
879
Jorge Troncoso5f449162021-09-30 16:29:32 -0700880 result = ufshc_reset(ufs_params.reg_base);
881 assert(result == 0);
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800882 ops->phy_init(&ufs_params);
883 result = ufshc_link_startup(ufs_params.reg_base);
884 assert(result == 0);
fengbaopeng44070ef2018-02-12 20:53:54 +0800885
886 ufs_enum();
887
888 ufs_get_device_info(&card);
889 if (card.wmanufacturerid == UFS_VENDOR_SKHYNIX) {
890 ufs_params.flags |= UFS_FLAGS_VENDOR_SKHYNIX;
891 }
892
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800893 ops->phy_set_pwr_mode(&ufs_params);
894 }
895
Haojian Zhuang20cd3232017-05-31 11:00:15 +0800896 (void)result;
897 return 0;
898}