ufs.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /*
  2. * Copyright (c) 2017-2021, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <endian.h>
  8. #include <errno.h>
  9. #include <stdint.h>
  10. #include <string.h>
  11. #include <platform_def.h>
  12. #include <arch_helpers.h>
  13. #include <common/debug.h>
  14. #include <drivers/delay_timer.h>
  15. #include <drivers/ufs.h>
  16. #include <lib/mmio.h>
  17. #define CDB_ADDR_MASK 127
  18. #define ALIGN_CDB(x) (((x) + CDB_ADDR_MASK) & ~CDB_ADDR_MASK)
  19. #define ALIGN_8(x) (((x) + 7) & ~7)
  20. #define UFS_DESC_SIZE 0x400
  21. #define MAX_UFS_DESC_SIZE 0x8000 /* 32 descriptors */
  22. #define MAX_PRDT_SIZE 0x40000 /* 256KB */
  23. static ufs_params_t ufs_params;
  24. static int nutrs; /* Number of UTP Transfer Request Slots */
  25. /*
  26. * ufs_uic_error_handler - UIC error interrupts handler
  27. * @ignore_linereset: set to ignore PA_LAYER_GEN_ERR (UIC error)
  28. *
  29. * Returns
  30. * 0 - ignore error
  31. * -EIO - fatal error, needs re-init
  32. * -EAGAIN - non-fatal error, retries are sufficient
  33. */
  34. static int ufs_uic_error_handler(bool ignore_linereset)
  35. {
  36. uint32_t data;
  37. int result = 0;
  38. data = mmio_read_32(ufs_params.reg_base + UECPA);
  39. if (data & UFS_UIC_PA_ERROR_MASK) {
  40. if (data & PA_LAYER_GEN_ERR) {
  41. if (!ignore_linereset) {
  42. return -EIO;
  43. }
  44. } else {
  45. result = -EAGAIN;
  46. }
  47. }
  48. data = mmio_read_32(ufs_params.reg_base + UECDL);
  49. if (data & UFS_UIC_DL_ERROR_MASK) {
  50. if (data & PA_INIT_ERR) {
  51. return -EIO;
  52. }
  53. result = -EAGAIN;
  54. }
  55. /* NL/TL/DME error requires retries */
  56. data = mmio_read_32(ufs_params.reg_base + UECN);
  57. if (data & UFS_UIC_NL_ERROR_MASK) {
  58. result = -EAGAIN;
  59. }
  60. data = mmio_read_32(ufs_params.reg_base + UECT);
  61. if (data & UFS_UIC_TL_ERROR_MASK) {
  62. result = -EAGAIN;
  63. }
  64. data = mmio_read_32(ufs_params.reg_base + UECDME);
  65. if (data & UFS_UIC_DME_ERROR_MASK) {
  66. result = -EAGAIN;
  67. }
  68. return result;
  69. }
  70. /*
  71. * ufs_error_handler - error interrupts handler
  72. * @status: interrupt status
  73. * @ignore_linereset: set to ignore PA_LAYER_GEN_ERR (UIC error)
  74. *
  75. * Returns
  76. * 0 - ignore error
  77. * -EIO - fatal error, needs re-init
  78. * -EAGAIN - non-fatal error, retries are sufficient
  79. */
  80. static int ufs_error_handler(uint32_t status, bool ignore_linereset)
  81. {
  82. int result;
  83. if (status & UFS_INT_UE) {
  84. result = ufs_uic_error_handler(ignore_linereset);
  85. if (result != 0) {
  86. return result;
  87. }
  88. }
  89. /* Return I/O error on fatal error, it is upto the caller to re-init UFS */
  90. if (status & UFS_INT_FATAL) {
  91. return -EIO;
  92. }
  93. /* retry for non-fatal errors */
  94. return -EAGAIN;
  95. }
  96. /*
  97. * ufs_wait_for_int_status - wait for expected interrupt status
  98. * @expected: expected interrupt status bit
  99. * @timeout_ms: timeout in milliseconds to poll for
  100. * @ignore_linereset: set to ignore PA_LAYER_GEN_ERR (UIC error)
  101. *
  102. * Returns
  103. * 0 - received expected interrupt and cleared it
  104. * -EIO - fatal error, needs re-init
  105. * -EAGAIN - non-fatal error, caller can retry
  106. * -ETIMEDOUT - timed out waiting for interrupt status
  107. */
  108. static int ufs_wait_for_int_status(const uint32_t expected_status,
  109. unsigned int timeout_ms,
  110. bool ignore_linereset)
  111. {
  112. uint32_t interrupt_status, interrupts_enabled;
  113. int result = 0;
  114. interrupts_enabled = mmio_read_32(ufs_params.reg_base + IE);
  115. do {
  116. interrupt_status = mmio_read_32(ufs_params.reg_base + IS) & interrupts_enabled;
  117. if (interrupt_status & UFS_INT_ERR) {
  118. mmio_write_32(ufs_params.reg_base + IS, interrupt_status & UFS_INT_ERR);
  119. result = ufs_error_handler(interrupt_status, ignore_linereset);
  120. if (result != 0) {
  121. return result;
  122. }
  123. }
  124. if (interrupt_status & expected_status) {
  125. break;
  126. }
  127. mdelay(1);
  128. } while (timeout_ms-- > 0);
  129. if (!(interrupt_status & expected_status)) {
  130. return -ETIMEDOUT;
  131. }
  132. mmio_write_32(ufs_params.reg_base + IS, expected_status);
  133. return result;
  134. }
  135. int ufshc_send_uic_cmd(uintptr_t base, uic_cmd_t *cmd)
  136. {
  137. unsigned int data;
  138. int result, retries;
  139. if (base == 0 || cmd == NULL)
  140. return -EINVAL;
  141. for (retries = 0; retries < 100; retries++) {
  142. data = mmio_read_32(base + HCS);
  143. if ((data & HCS_UCRDY) != 0) {
  144. break;
  145. }
  146. mdelay(1);
  147. }
  148. if (retries >= 100) {
  149. return -EBUSY;
  150. }
  151. mmio_write_32(base + IS, ~0);
  152. mmio_write_32(base + UCMDARG1, cmd->arg1);
  153. mmio_write_32(base + UCMDARG2, cmd->arg2);
  154. mmio_write_32(base + UCMDARG3, cmd->arg3);
  155. mmio_write_32(base + UICCMD, cmd->op);
  156. result = ufs_wait_for_int_status(UFS_INT_UCCS, UIC_CMD_TIMEOUT_MS,
  157. cmd->op == DME_SET);
  158. if (result != 0) {
  159. return result;
  160. }
  161. return mmio_read_32(base + UCMDARG2) & CONFIG_RESULT_CODE_MASK;
  162. }
  163. int ufshc_dme_get(unsigned int attr, unsigned int idx, unsigned int *val)
  164. {
  165. uintptr_t base;
  166. int result, retries;
  167. uic_cmd_t cmd;
  168. assert(ufs_params.reg_base != 0);
  169. if (val == NULL)
  170. return -EINVAL;
  171. base = ufs_params.reg_base;
  172. cmd.arg1 = (attr << 16) | GEN_SELECTOR_IDX(idx);
  173. cmd.arg2 = 0;
  174. cmd.arg3 = 0;
  175. cmd.op = DME_GET;
  176. for (retries = 0; retries < UFS_UIC_COMMAND_RETRIES; ++retries) {
  177. result = ufshc_send_uic_cmd(base, &cmd);
  178. if (result == 0)
  179. break;
  180. /* -EIO requires UFS re-init */
  181. if (result == -EIO) {
  182. return result;
  183. }
  184. }
  185. if (retries >= UFS_UIC_COMMAND_RETRIES)
  186. return -EIO;
  187. *val = mmio_read_32(base + UCMDARG3);
  188. return 0;
  189. }
  190. int ufshc_dme_set(unsigned int attr, unsigned int idx, unsigned int val)
  191. {
  192. uintptr_t base;
  193. int result, retries;
  194. uic_cmd_t cmd;
  195. assert((ufs_params.reg_base != 0));
  196. base = ufs_params.reg_base;
  197. cmd.arg1 = (attr << 16) | GEN_SELECTOR_IDX(idx);
  198. cmd.arg2 = 0;
  199. cmd.arg3 = val;
  200. cmd.op = DME_SET;
  201. for (retries = 0; retries < UFS_UIC_COMMAND_RETRIES; ++retries) {
  202. result = ufshc_send_uic_cmd(base, &cmd);
  203. if (result == 0)
  204. break;
  205. /* -EIO requires UFS re-init */
  206. if (result == -EIO) {
  207. return result;
  208. }
  209. }
  210. if (retries >= UFS_UIC_COMMAND_RETRIES)
  211. return -EIO;
  212. return 0;
  213. }
  214. static int ufshc_hce_enable(uintptr_t base)
  215. {
  216. unsigned int data;
  217. int retries;
  218. /* Enable Host Controller */
  219. mmio_write_32(base + HCE, HCE_ENABLE);
  220. /* Wait until basic initialization sequence completed */
  221. for (retries = 0; retries < HCE_ENABLE_INNER_RETRIES; ++retries) {
  222. data = mmio_read_32(base + HCE);
  223. if (data & HCE_ENABLE) {
  224. break;
  225. }
  226. udelay(HCE_ENABLE_TIMEOUT_US);
  227. }
  228. if (retries >= HCE_ENABLE_INNER_RETRIES) {
  229. return -ETIMEDOUT;
  230. }
  231. return 0;
  232. }
  233. static int ufshc_hce_disable(uintptr_t base)
  234. {
  235. unsigned int data;
  236. int timeout;
  237. /* Disable Host Controller */
  238. mmio_write_32(base + HCE, HCE_DISABLE);
  239. timeout = HCE_DISABLE_TIMEOUT_US;
  240. do {
  241. data = mmio_read_32(base + HCE);
  242. if ((data & HCE_ENABLE) == HCE_DISABLE) {
  243. break;
  244. }
  245. udelay(1);
  246. } while (--timeout > 0);
  247. if (timeout <= 0) {
  248. return -ETIMEDOUT;
  249. }
  250. return 0;
  251. }
  252. static int ufshc_reset(uintptr_t base)
  253. {
  254. unsigned int data;
  255. int retries, result;
  256. /* disable controller if enabled */
  257. if (mmio_read_32(base + HCE) & HCE_ENABLE) {
  258. result = ufshc_hce_disable(base);
  259. if (result != 0) {
  260. return -EIO;
  261. }
  262. }
  263. for (retries = 0; retries < HCE_ENABLE_OUTER_RETRIES; ++retries) {
  264. result = ufshc_hce_enable(base);
  265. if (result == 0) {
  266. break;
  267. }
  268. }
  269. if (retries >= HCE_ENABLE_OUTER_RETRIES) {
  270. return -EIO;
  271. }
  272. /* Enable UIC Interrupts alone. We can ignore other interrupts until
  273. * link is up as there might be spurious error interrupts during link-up
  274. */
  275. data = UFS_INT_UCCS | UFS_INT_UHES | UFS_INT_UHXS | UFS_INT_UPMS;
  276. mmio_write_32(base + IE, data);
  277. return 0;
  278. }
  279. static int ufshc_dme_link_startup(uintptr_t base)
  280. {
  281. uic_cmd_t cmd;
  282. memset(&cmd, 0, sizeof(cmd));
  283. cmd.op = DME_LINKSTARTUP;
  284. return ufshc_send_uic_cmd(base, &cmd);
  285. }
  286. static int ufshc_link_startup(uintptr_t base)
  287. {
  288. int data, result;
  289. int retries;
  290. for (retries = DME_LINKSTARTUP_RETRIES; retries > 0; retries--) {
  291. result = ufshc_dme_link_startup(base);
  292. if (result != 0) {
  293. /* Reset controller before trying again */
  294. result = ufshc_reset(base);
  295. if (result != 0) {
  296. return result;
  297. }
  298. continue;
  299. }
  300. assert(mmio_read_32(base + HCS) & HCS_DP);
  301. data = mmio_read_32(base + IS);
  302. if (data & UFS_INT_ULSS)
  303. mmio_write_32(base + IS, UFS_INT_ULSS);
  304. /* clear UE set due to line-reset */
  305. if (data & UFS_INT_UE) {
  306. mmio_write_32(base + IS, UFS_INT_UE);
  307. }
  308. /* clearing line-reset, UECPA is cleared on read */
  309. mmio_read_32(base + UECPA);
  310. return 0;
  311. }
  312. return -EIO;
  313. }
  314. /* Read Door Bell register to check if slot zero is available */
  315. static int is_slot_available(void)
  316. {
  317. if (mmio_read_32(ufs_params.reg_base + UTRLDBR) & 0x1) {
  318. return -EBUSY;
  319. }
  320. return 0;
  321. }
  322. static void get_utrd(utp_utrd_t *utrd)
  323. {
  324. uintptr_t base;
  325. int result;
  326. utrd_header_t *hd;
  327. assert(utrd != NULL);
  328. result = is_slot_available();
  329. assert(result == 0);
  330. /* clear utrd */
  331. memset((void *)utrd, 0, sizeof(utp_utrd_t));
  332. base = ufs_params.desc_base;
  333. /* clear the descriptor */
  334. memset((void *)base, 0, UFS_DESC_SIZE);
  335. utrd->header = base;
  336. utrd->task_tag = 1; /* We always use the first slot */
  337. /* CDB address should be aligned with 128 bytes */
  338. utrd->upiu = ALIGN_CDB(utrd->header + sizeof(utrd_header_t));
  339. utrd->resp_upiu = ALIGN_8(utrd->upiu + sizeof(cmd_upiu_t));
  340. utrd->size_upiu = utrd->resp_upiu - utrd->upiu;
  341. utrd->size_resp_upiu = ALIGN_8(sizeof(resp_upiu_t));
  342. utrd->prdt = utrd->resp_upiu + utrd->size_resp_upiu;
  343. hd = (utrd_header_t *)utrd->header;
  344. hd->ucdba = utrd->upiu & UINT32_MAX;
  345. hd->ucdbau = (utrd->upiu >> 32) & UINT32_MAX;
  346. /* Both RUL and RUO is based on DWORD */
  347. hd->rul = utrd->size_resp_upiu >> 2;
  348. hd->ruo = utrd->size_upiu >> 2;
  349. (void)result;
  350. }
  351. /*
  352. * Prepare UTRD, Command UPIU, Response UPIU.
  353. */
  354. static int ufs_prepare_cmd(utp_utrd_t *utrd, uint8_t op, uint8_t lun,
  355. int lba, uintptr_t buf, size_t length)
  356. {
  357. utrd_header_t *hd;
  358. cmd_upiu_t *upiu;
  359. prdt_t *prdt;
  360. unsigned int ulba;
  361. unsigned int lba_cnt;
  362. uintptr_t desc_limit;
  363. uintptr_t prdt_end;
  364. hd = (utrd_header_t *)utrd->header;
  365. upiu = (cmd_upiu_t *)utrd->upiu;
  366. hd->i = 1;
  367. hd->ct = CT_UFS_STORAGE;
  368. hd->ocs = OCS_MASK;
  369. upiu->trans_type = CMD_UPIU;
  370. upiu->task_tag = utrd->task_tag;
  371. upiu->cdb[0] = op;
  372. ulba = (unsigned int)lba;
  373. lba_cnt = (unsigned int)(length >> UFS_BLOCK_SHIFT);
  374. switch (op) {
  375. case CDBCMD_TEST_UNIT_READY:
  376. break;
  377. case CDBCMD_READ_CAPACITY_10:
  378. hd->dd = DD_OUT;
  379. upiu->flags = UPIU_FLAGS_R | UPIU_FLAGS_ATTR_S;
  380. upiu->lun = lun;
  381. break;
  382. case CDBCMD_READ_10:
  383. hd->dd = DD_OUT;
  384. upiu->flags = UPIU_FLAGS_R | UPIU_FLAGS_ATTR_S;
  385. upiu->lun = lun;
  386. upiu->cdb[1] = RW_WITHOUT_CACHE;
  387. /* set logical block address */
  388. upiu->cdb[2] = (ulba >> 24) & 0xff;
  389. upiu->cdb[3] = (ulba >> 16) & 0xff;
  390. upiu->cdb[4] = (ulba >> 8) & 0xff;
  391. upiu->cdb[5] = ulba & 0xff;
  392. /* set transfer length */
  393. upiu->cdb[7] = (lba_cnt >> 8) & 0xff;
  394. upiu->cdb[8] = lba_cnt & 0xff;
  395. break;
  396. case CDBCMD_WRITE_10:
  397. hd->dd = DD_IN;
  398. upiu->flags = UPIU_FLAGS_W | UPIU_FLAGS_ATTR_S;
  399. upiu->lun = lun;
  400. upiu->cdb[1] = RW_WITHOUT_CACHE;
  401. /* set logical block address */
  402. upiu->cdb[2] = (ulba >> 24) & 0xff;
  403. upiu->cdb[3] = (ulba >> 16) & 0xff;
  404. upiu->cdb[4] = (ulba >> 8) & 0xff;
  405. upiu->cdb[5] = ulba & 0xff;
  406. /* set transfer length */
  407. upiu->cdb[7] = (lba_cnt >> 8) & 0xff;
  408. upiu->cdb[8] = lba_cnt & 0xff;
  409. break;
  410. default:
  411. assert(0);
  412. break;
  413. }
  414. if (hd->dd == DD_IN) {
  415. flush_dcache_range(buf, length);
  416. } else if (hd->dd == DD_OUT) {
  417. inv_dcache_range(buf, length);
  418. }
  419. utrd->prdt_length = 0;
  420. if (length) {
  421. upiu->exp_data_trans_len = htobe32(length);
  422. assert(lba_cnt <= UINT16_MAX);
  423. prdt = (prdt_t *)utrd->prdt;
  424. desc_limit = ufs_params.desc_base + ufs_params.desc_size;
  425. while (length > 0) {
  426. if ((uintptr_t)prdt + sizeof(prdt_t) > desc_limit) {
  427. ERROR("UFS: Exceeded descriptor limit. Image is too large\n");
  428. panic();
  429. }
  430. prdt->dba = (unsigned int)(buf & UINT32_MAX);
  431. prdt->dbau = (unsigned int)((buf >> 32) & UINT32_MAX);
  432. /* prdt->dbc counts from 0 */
  433. if (length > MAX_PRDT_SIZE) {
  434. prdt->dbc = MAX_PRDT_SIZE - 1;
  435. length = length - MAX_PRDT_SIZE;
  436. } else {
  437. prdt->dbc = length - 1;
  438. length = 0;
  439. }
  440. buf += MAX_PRDT_SIZE;
  441. prdt++;
  442. utrd->prdt_length++;
  443. }
  444. hd->prdtl = utrd->prdt_length;
  445. hd->prdto = (utrd->size_upiu + utrd->size_resp_upiu) >> 2;
  446. }
  447. prdt_end = utrd->prdt + utrd->prdt_length * sizeof(prdt_t);
  448. flush_dcache_range(utrd->header, prdt_end - utrd->header);
  449. return 0;
  450. }
  451. static int ufs_prepare_query(utp_utrd_t *utrd, uint8_t op, uint8_t idn,
  452. uint8_t index, uint8_t sel,
  453. uintptr_t buf, size_t length)
  454. {
  455. utrd_header_t *hd;
  456. query_upiu_t *query_upiu;
  457. hd = (utrd_header_t *)utrd->header;
  458. query_upiu = (query_upiu_t *)utrd->upiu;
  459. hd->i = 1;
  460. hd->ct = CT_UFS_STORAGE;
  461. hd->ocs = OCS_MASK;
  462. query_upiu->trans_type = QUERY_REQUEST_UPIU;
  463. query_upiu->task_tag = utrd->task_tag;
  464. query_upiu->data_segment_len = htobe16(length);
  465. query_upiu->ts.desc.opcode = op;
  466. query_upiu->ts.desc.idn = idn;
  467. query_upiu->ts.desc.index = index;
  468. query_upiu->ts.desc.selector = sel;
  469. switch (op) {
  470. case QUERY_READ_DESC:
  471. query_upiu->query_func = QUERY_FUNC_STD_READ;
  472. query_upiu->ts.desc.length = htobe16(length);
  473. break;
  474. case QUERY_WRITE_DESC:
  475. query_upiu->query_func = QUERY_FUNC_STD_WRITE;
  476. query_upiu->ts.desc.length = htobe16(length);
  477. memcpy((void *)(utrd->upiu + sizeof(query_upiu_t)),
  478. (void *)buf, length);
  479. break;
  480. case QUERY_READ_ATTR:
  481. case QUERY_READ_FLAG:
  482. query_upiu->query_func = QUERY_FUNC_STD_READ;
  483. break;
  484. case QUERY_CLEAR_FLAG:
  485. case QUERY_SET_FLAG:
  486. query_upiu->query_func = QUERY_FUNC_STD_WRITE;
  487. break;
  488. case QUERY_WRITE_ATTR:
  489. query_upiu->query_func = QUERY_FUNC_STD_WRITE;
  490. query_upiu->ts.attr.value = htobe32(*((uint32_t *)buf));
  491. break;
  492. default:
  493. assert(0);
  494. break;
  495. }
  496. flush_dcache_range((uintptr_t)utrd->header, UFS_DESC_SIZE);
  497. return 0;
  498. }
  499. static void ufs_prepare_nop_out(utp_utrd_t *utrd)
  500. {
  501. utrd_header_t *hd;
  502. nop_out_upiu_t *nop_out;
  503. hd = (utrd_header_t *)utrd->header;
  504. nop_out = (nop_out_upiu_t *)utrd->upiu;
  505. hd->i = 1;
  506. hd->ct = CT_UFS_STORAGE;
  507. hd->ocs = OCS_MASK;
  508. nop_out->trans_type = 0;
  509. nop_out->task_tag = utrd->task_tag;
  510. flush_dcache_range((uintptr_t)utrd->header, UFS_DESC_SIZE);
  511. }
  512. static void ufs_send_request(int task_tag)
  513. {
  514. unsigned int data;
  515. int slot;
  516. slot = task_tag - 1;
  517. /* clear all interrupts */
  518. mmio_write_32(ufs_params.reg_base + IS, ~0);
  519. mmio_write_32(ufs_params.reg_base + UTRLRSR, 1);
  520. assert(mmio_read_32(ufs_params.reg_base + UTRLRSR) == 1);
  521. data = UTRIACR_IAEN | UTRIACR_CTR | UTRIACR_IACTH(0x1F) |
  522. UTRIACR_IATOVAL(0xFF);
  523. mmio_write_32(ufs_params.reg_base + UTRIACR, data);
  524. /* send request */
  525. mmio_setbits_32(ufs_params.reg_base + UTRLDBR, 1 << slot);
  526. }
  527. static int ufs_check_resp(utp_utrd_t *utrd, int trans_type, unsigned int timeout_ms)
  528. {
  529. utrd_header_t *hd;
  530. resp_upiu_t *resp;
  531. sense_data_t *sense;
  532. unsigned int data;
  533. int slot, result;
  534. hd = (utrd_header_t *)utrd->header;
  535. resp = (resp_upiu_t *)utrd->resp_upiu;
  536. result = ufs_wait_for_int_status(UFS_INT_UTRCS, timeout_ms, false);
  537. if (result != 0) {
  538. return result;
  539. }
  540. slot = utrd->task_tag - 1;
  541. data = mmio_read_32(ufs_params.reg_base + UTRLDBR);
  542. assert((data & (1 << slot)) == 0);
  543. /*
  544. * Invalidate the header after DMA read operation has
  545. * completed to avoid cpu referring to the prefetched
  546. * data brought in before DMA completion.
  547. */
  548. inv_dcache_range((uintptr_t)hd, UFS_DESC_SIZE);
  549. assert(hd->ocs == OCS_SUCCESS);
  550. assert((resp->trans_type & TRANS_TYPE_CODE_MASK) == trans_type);
  551. sense = &resp->sd.sense;
  552. if (sense->resp_code == SENSE_DATA_VALID &&
  553. sense->sense_key == SENSE_KEY_UNIT_ATTENTION && sense->asc == 0x29 &&
  554. sense->ascq == 0) {
  555. WARN("Unit Attention Condition\n");
  556. return -EAGAIN;
  557. }
  558. (void)resp;
  559. (void)slot;
  560. (void)data;
  561. return 0;
  562. }
  563. static void ufs_send_cmd(utp_utrd_t *utrd, uint8_t cmd_op, uint8_t lun, int lba, uintptr_t buf,
  564. size_t length)
  565. {
  566. int result, i;
  567. for (i = 0; i < UFS_CMD_RETRIES; ++i) {
  568. get_utrd(utrd);
  569. result = ufs_prepare_cmd(utrd, cmd_op, lun, lba, buf, length);
  570. assert(result == 0);
  571. ufs_send_request(utrd->task_tag);
  572. result = ufs_check_resp(utrd, RESPONSE_UPIU, CMD_TIMEOUT_MS);
  573. if (result == 0 || result == -EIO) {
  574. break;
  575. }
  576. }
  577. assert(result == 0);
  578. (void)result;
  579. }
  580. #ifdef UFS_RESP_DEBUG
  581. static void dump_upiu(utp_utrd_t *utrd)
  582. {
  583. utrd_header_t *hd;
  584. int i;
  585. hd = (utrd_header_t *)utrd->header;
  586. INFO("utrd:0x%x, ruo:0x%x, rul:0x%x, ocs:0x%x, UTRLDBR:0x%x\n",
  587. (unsigned int)(uintptr_t)utrd, hd->ruo, hd->rul, hd->ocs,
  588. mmio_read_32(ufs_params.reg_base + UTRLDBR));
  589. for (i = 0; i < sizeof(utrd_header_t); i += 4) {
  590. INFO("[%lx]:0x%x\n",
  591. (uintptr_t)utrd->header + i,
  592. *(unsigned int *)((uintptr_t)utrd->header + i));
  593. }
  594. for (i = 0; i < sizeof(cmd_upiu_t); i += 4) {
  595. INFO("cmd[%lx]:0x%x\n",
  596. utrd->upiu + i,
  597. *(unsigned int *)(utrd->upiu + i));
  598. }
  599. for (i = 0; i < sizeof(resp_upiu_t); i += 4) {
  600. INFO("resp[%lx]:0x%x\n",
  601. utrd->resp_upiu + i,
  602. *(unsigned int *)(utrd->resp_upiu + i));
  603. }
  604. for (i = 0; i < sizeof(prdt_t); i += 4) {
  605. INFO("prdt[%lx]:0x%x\n",
  606. utrd->prdt + i,
  607. *(unsigned int *)(utrd->prdt + i));
  608. }
  609. }
  610. #endif
  611. static void ufs_verify_init(void)
  612. {
  613. utp_utrd_t utrd;
  614. int result;
  615. get_utrd(&utrd);
  616. ufs_prepare_nop_out(&utrd);
  617. ufs_send_request(utrd.task_tag);
  618. result = ufs_check_resp(&utrd, NOP_IN_UPIU, NOP_OUT_TIMEOUT_MS);
  619. assert(result == 0);
  620. (void)result;
  621. }
  622. static void ufs_verify_ready(void)
  623. {
  624. utp_utrd_t utrd;
  625. ufs_send_cmd(&utrd, CDBCMD_TEST_UNIT_READY, 0, 0, 0, 0);
  626. }
  627. static void ufs_query(uint8_t op, uint8_t idn, uint8_t index, uint8_t sel,
  628. uintptr_t buf, size_t size)
  629. {
  630. utp_utrd_t utrd;
  631. query_resp_upiu_t *resp;
  632. int result;
  633. switch (op) {
  634. case QUERY_READ_FLAG:
  635. case QUERY_READ_ATTR:
  636. case QUERY_READ_DESC:
  637. case QUERY_WRITE_DESC:
  638. case QUERY_WRITE_ATTR:
  639. assert(((buf & 3) == 0) && (size != 0));
  640. break;
  641. default:
  642. /* Do nothing in default case */
  643. break;
  644. }
  645. get_utrd(&utrd);
  646. ufs_prepare_query(&utrd, op, idn, index, sel, buf, size);
  647. ufs_send_request(utrd.task_tag);
  648. result = ufs_check_resp(&utrd, QUERY_RESPONSE_UPIU, QUERY_REQ_TIMEOUT_MS);
  649. assert(result == 0);
  650. resp = (query_resp_upiu_t *)utrd.resp_upiu;
  651. #ifdef UFS_RESP_DEBUG
  652. dump_upiu(&utrd);
  653. #endif
  654. assert(resp->query_resp == QUERY_RESP_SUCCESS);
  655. switch (op) {
  656. case QUERY_READ_FLAG:
  657. *(uint32_t *)buf = (uint32_t)resp->ts.flag.value;
  658. break;
  659. case QUERY_READ_DESC:
  660. memcpy((void *)buf,
  661. (void *)(utrd.resp_upiu + sizeof(query_resp_upiu_t)),
  662. size);
  663. break;
  664. case QUERY_READ_ATTR:
  665. *(uint32_t *)buf = htobe32(resp->ts.attr.value);
  666. break;
  667. default:
  668. /* Do nothing in default case */
  669. break;
  670. }
  671. (void)result;
  672. }
  673. unsigned int ufs_read_attr(int idn)
  674. {
  675. unsigned int value;
  676. ufs_query(QUERY_READ_ATTR, idn, 0, 0,
  677. (uintptr_t)&value, sizeof(value));
  678. return value;
  679. }
  680. void ufs_write_attr(int idn, unsigned int value)
  681. {
  682. ufs_query(QUERY_WRITE_ATTR, idn, 0, 0,
  683. (uintptr_t)&value, sizeof(value));
  684. }
  685. unsigned int ufs_read_flag(int idn)
  686. {
  687. unsigned int value;
  688. ufs_query(QUERY_READ_FLAG, idn, 0, 0,
  689. (uintptr_t)&value, sizeof(value));
  690. return value;
  691. }
  692. void ufs_set_flag(int idn)
  693. {
  694. ufs_query(QUERY_SET_FLAG, idn, 0, 0, 0, 0);
  695. }
  696. void ufs_clear_flag(int idn)
  697. {
  698. ufs_query(QUERY_CLEAR_FLAG, idn, 0, 0, 0, 0);
  699. }
  700. void ufs_read_desc(int idn, int index, uintptr_t buf, size_t size)
  701. {
  702. ufs_query(QUERY_READ_DESC, idn, index, 0, buf, size);
  703. }
  704. void ufs_write_desc(int idn, int index, uintptr_t buf, size_t size)
  705. {
  706. ufs_query(QUERY_WRITE_DESC, idn, index, 0, buf, size);
  707. }
  708. static int ufs_read_capacity(int lun, unsigned int *num, unsigned int *size)
  709. {
  710. utp_utrd_t utrd;
  711. resp_upiu_t *resp;
  712. sense_data_t *sense;
  713. unsigned char data[CACHE_WRITEBACK_GRANULE << 1];
  714. uintptr_t buf;
  715. int retries = UFS_READ_CAPACITY_RETRIES;
  716. assert((ufs_params.reg_base != 0) &&
  717. (ufs_params.desc_base != 0) &&
  718. (ufs_params.desc_size >= UFS_DESC_SIZE) &&
  719. (num != NULL) && (size != NULL));
  720. /* align buf address */
  721. buf = (uintptr_t)data;
  722. buf = (buf + CACHE_WRITEBACK_GRANULE - 1) &
  723. ~(CACHE_WRITEBACK_GRANULE - 1);
  724. do {
  725. ufs_send_cmd(&utrd, CDBCMD_READ_CAPACITY_10, lun, 0,
  726. buf, READ_CAPACITY_LENGTH);
  727. #ifdef UFS_RESP_DEBUG
  728. dump_upiu(&utrd);
  729. #endif
  730. resp = (resp_upiu_t *)utrd.resp_upiu;
  731. sense = &resp->sd.sense;
  732. if (!((sense->resp_code == SENSE_DATA_VALID) &&
  733. (sense->sense_key == SENSE_KEY_UNIT_ATTENTION) &&
  734. (sense->asc == 0x29) && (sense->ascq == 0))) {
  735. inv_dcache_range(buf, CACHE_WRITEBACK_GRANULE);
  736. /* last logical block address */
  737. *num = be32toh(*(unsigned int *)buf);
  738. if (*num)
  739. *num += 1;
  740. /* logical block length in bytes */
  741. *size = be32toh(*(unsigned int *)(buf + 4));
  742. return 0;
  743. }
  744. } while (retries-- > 0);
  745. return -ETIMEDOUT;
  746. }
  747. size_t ufs_read_blocks(int lun, int lba, uintptr_t buf, size_t size)
  748. {
  749. utp_utrd_t utrd;
  750. resp_upiu_t *resp;
  751. assert((ufs_params.reg_base != 0) &&
  752. (ufs_params.desc_base != 0) &&
  753. (ufs_params.desc_size >= UFS_DESC_SIZE));
  754. ufs_send_cmd(&utrd, CDBCMD_READ_10, lun, lba, buf, size);
  755. #ifdef UFS_RESP_DEBUG
  756. dump_upiu(&utrd);
  757. #endif
  758. /*
  759. * Invalidate prefetched cache contents before cpu
  760. * accesses the buf.
  761. */
  762. inv_dcache_range(buf, size);
  763. resp = (resp_upiu_t *)utrd.resp_upiu;
  764. return size - resp->res_trans_cnt;
  765. }
  766. size_t ufs_write_blocks(int lun, int lba, const uintptr_t buf, size_t size)
  767. {
  768. utp_utrd_t utrd;
  769. resp_upiu_t *resp;
  770. assert((ufs_params.reg_base != 0) &&
  771. (ufs_params.desc_base != 0) &&
  772. (ufs_params.desc_size >= UFS_DESC_SIZE));
  773. ufs_send_cmd(&utrd, CDBCMD_WRITE_10, lun, lba, buf, size);
  774. #ifdef UFS_RESP_DEBUG
  775. dump_upiu(&utrd);
  776. #endif
  777. resp = (resp_upiu_t *)utrd.resp_upiu;
  778. return size - resp->res_trans_cnt;
  779. }
  780. static int ufs_set_fdevice_init(void)
  781. {
  782. unsigned int result;
  783. int timeout;
  784. ufs_set_flag(FLAG_DEVICE_INIT);
  785. timeout = FDEVICEINIT_TIMEOUT_MS;
  786. do {
  787. result = ufs_read_flag(FLAG_DEVICE_INIT);
  788. if (!result) {
  789. break;
  790. }
  791. mdelay(5);
  792. timeout -= 5;
  793. } while (timeout > 0);
  794. if (result != 0U) {
  795. return -ETIMEDOUT;
  796. }
  797. return 0;
  798. }
  799. static void ufs_enum(void)
  800. {
  801. unsigned int blk_num, blk_size;
  802. int i, result;
  803. mmio_write_32(ufs_params.reg_base + UTRLBA,
  804. ufs_params.desc_base & UINT32_MAX);
  805. mmio_write_32(ufs_params.reg_base + UTRLBAU,
  806. (ufs_params.desc_base >> 32) & UINT32_MAX);
  807. ufs_verify_init();
  808. ufs_verify_ready();
  809. result = ufs_set_fdevice_init();
  810. assert(result == 0);
  811. blk_num = 0;
  812. blk_size = 0;
  813. /* dump available LUNs */
  814. for (i = 0; i < UFS_MAX_LUNS; i++) {
  815. result = ufs_read_capacity(i, &blk_num, &blk_size);
  816. if (result != 0) {
  817. WARN("UFS LUN%d dump failed\n", i);
  818. }
  819. if (blk_num && blk_size) {
  820. INFO("UFS LUN%d contains %d blocks with %d-byte size\n",
  821. i, blk_num, blk_size);
  822. }
  823. }
  824. (void)result;
  825. }
  826. static void ufs_get_device_info(struct ufs_dev_desc *card_data)
  827. {
  828. uint8_t desc_buf[DESC_DEVICE_MAX_SIZE];
  829. ufs_query(QUERY_READ_DESC, DESC_TYPE_DEVICE, 0, 0,
  830. (uintptr_t)desc_buf, DESC_DEVICE_MAX_SIZE);
  831. /*
  832. * getting vendor (manufacturerID) and Bank Index in big endian
  833. * format
  834. */
  835. card_data->wmanufacturerid = (uint16_t)((desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8) |
  836. (desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1]));
  837. }
  838. int ufs_init(const ufs_ops_t *ops, ufs_params_t *params)
  839. {
  840. int result;
  841. unsigned int data;
  842. uic_cmd_t cmd;
  843. struct ufs_dev_desc card = {0};
  844. assert((params != NULL) &&
  845. (params->reg_base != 0) &&
  846. (params->desc_base != 0) &&
  847. (params->desc_size >= UFS_DESC_SIZE));
  848. memcpy(&ufs_params, params, sizeof(ufs_params_t));
  849. /* 0 means 1 slot */
  850. nutrs = (mmio_read_32(ufs_params.reg_base + CAP) & CAP_NUTRS_MASK) + 1;
  851. if (nutrs > (ufs_params.desc_size / UFS_DESC_SIZE)) {
  852. nutrs = ufs_params.desc_size / UFS_DESC_SIZE;
  853. }
  854. if (ufs_params.flags & UFS_FLAGS_SKIPINIT) {
  855. mmio_write_32(ufs_params.reg_base + UTRLBA,
  856. ufs_params.desc_base & UINT32_MAX);
  857. mmio_write_32(ufs_params.reg_base + UTRLBAU,
  858. (ufs_params.desc_base >> 32) & UINT32_MAX);
  859. result = ufshc_dme_get(0x1571, 0, &data);
  860. assert(result == 0);
  861. result = ufshc_dme_get(0x41, 0, &data);
  862. assert(result == 0);
  863. if (data == 1) {
  864. /* prepare to exit hibernate mode */
  865. memset(&cmd, 0, sizeof(uic_cmd_t));
  866. cmd.op = DME_HIBERNATE_EXIT;
  867. result = ufshc_send_uic_cmd(ufs_params.reg_base,
  868. &cmd);
  869. assert(result == 0);
  870. data = mmio_read_32(ufs_params.reg_base + UCMDARG2);
  871. assert(data == 0);
  872. do {
  873. data = mmio_read_32(ufs_params.reg_base + IS);
  874. } while ((data & UFS_INT_UHXS) == 0);
  875. mmio_write_32(ufs_params.reg_base + IS, UFS_INT_UHXS);
  876. data = mmio_read_32(ufs_params.reg_base + HCS);
  877. assert((data & HCS_UPMCRS_MASK) == HCS_PWR_LOCAL);
  878. }
  879. result = ufshc_dme_get(0x1568, 0, &data);
  880. assert(result == 0);
  881. assert((data > 0) && (data <= 3));
  882. } else {
  883. assert((ops != NULL) && (ops->phy_init != NULL) &&
  884. (ops->phy_set_pwr_mode != NULL));
  885. result = ufshc_reset(ufs_params.reg_base);
  886. assert(result == 0);
  887. ops->phy_init(&ufs_params);
  888. result = ufshc_link_startup(ufs_params.reg_base);
  889. assert(result == 0);
  890. /* enable all interrupts */
  891. data = UFS_INT_UCCS | UFS_INT_UHES | UFS_INT_UHXS | UFS_INT_UPMS;
  892. data |= UFS_INT_UTRCS | UFS_INT_ERR;
  893. mmio_write_32(ufs_params.reg_base + IE, data);
  894. ufs_enum();
  895. ufs_get_device_info(&card);
  896. if (card.wmanufacturerid == UFS_VENDOR_SKHYNIX) {
  897. ufs_params.flags |= UFS_FLAGS_VENDOR_SKHYNIX;
  898. }
  899. ops->phy_set_pwr_mode(&ufs_params);
  900. }
  901. (void)result;
  902. return 0;
  903. }