ufs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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, 1U << 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_read_desc(DESC_TYPE_DEVICE, 0, (uintptr_t)desc_buf, DESC_DEVICE_MAX_SIZE);
  830. /*
  831. * getting vendor (manufacturerID) and Bank Index in big endian
  832. * format
  833. */
  834. card_data->wmanufacturerid = (uint16_t)((desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8) |
  835. (desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1]));
  836. }
  837. int ufs_init(const ufs_ops_t *ops, ufs_params_t *params)
  838. {
  839. int result;
  840. unsigned int data;
  841. uic_cmd_t cmd;
  842. struct ufs_dev_desc card = {0};
  843. assert((params != NULL) &&
  844. (params->reg_base != 0) &&
  845. (params->desc_base != 0) &&
  846. (params->desc_size >= UFS_DESC_SIZE));
  847. memcpy(&ufs_params, params, sizeof(ufs_params_t));
  848. /* 0 means 1 slot */
  849. nutrs = (mmio_read_32(ufs_params.reg_base + CAP) & CAP_NUTRS_MASK) + 1;
  850. if (nutrs > (ufs_params.desc_size / UFS_DESC_SIZE)) {
  851. nutrs = ufs_params.desc_size / UFS_DESC_SIZE;
  852. }
  853. if (ufs_params.flags & UFS_FLAGS_SKIPINIT) {
  854. mmio_write_32(ufs_params.reg_base + UTRLBA,
  855. ufs_params.desc_base & UINT32_MAX);
  856. mmio_write_32(ufs_params.reg_base + UTRLBAU,
  857. (ufs_params.desc_base >> 32) & UINT32_MAX);
  858. result = ufshc_dme_get(0x1571, 0, &data);
  859. assert(result == 0);
  860. result = ufshc_dme_get(0x41, 0, &data);
  861. assert(result == 0);
  862. if (data == 1) {
  863. /* prepare to exit hibernate mode */
  864. memset(&cmd, 0, sizeof(uic_cmd_t));
  865. cmd.op = DME_HIBERNATE_EXIT;
  866. result = ufshc_send_uic_cmd(ufs_params.reg_base,
  867. &cmd);
  868. assert(result == 0);
  869. data = mmio_read_32(ufs_params.reg_base + UCMDARG2);
  870. assert(data == 0);
  871. do {
  872. data = mmio_read_32(ufs_params.reg_base + IS);
  873. } while ((data & UFS_INT_UHXS) == 0);
  874. mmio_write_32(ufs_params.reg_base + IS, UFS_INT_UHXS);
  875. data = mmio_read_32(ufs_params.reg_base + HCS);
  876. assert((data & HCS_UPMCRS_MASK) == HCS_PWR_LOCAL);
  877. }
  878. result = ufshc_dme_get(0x1568, 0, &data);
  879. assert(result == 0);
  880. assert((data > 0) && (data <= 3));
  881. } else {
  882. assert((ops != NULL) && (ops->phy_init != NULL) &&
  883. (ops->phy_set_pwr_mode != NULL));
  884. result = ufshc_reset(ufs_params.reg_base);
  885. assert(result == 0);
  886. ops->phy_init(&ufs_params);
  887. result = ufshc_link_startup(ufs_params.reg_base);
  888. assert(result == 0);
  889. /* enable all interrupts */
  890. data = UFS_INT_UCCS | UFS_INT_UHES | UFS_INT_UHXS | UFS_INT_UPMS;
  891. data |= UFS_INT_UTRCS | UFS_INT_ERR;
  892. mmio_write_32(ufs_params.reg_base + IE, data);
  893. ufs_enum();
  894. ufs_get_device_info(&card);
  895. if (card.wmanufacturerid == UFS_VENDOR_SKHYNIX) {
  896. ufs_params.flags |= UFS_FLAGS_VENDOR_SKHYNIX;
  897. }
  898. ops->phy_set_pwr_mode(&ufs_params);
  899. }
  900. (void)result;
  901. return 0;
  902. }