sd_uhs2.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2021 Linaro Ltd
  4. * Author: Ulf Hansson <ulf.hansson@linaro.org>
  5. *
  6. * Copyright (C) 2014 Intel Corp, All Rights Reserved.
  7. * Author: Yi Sun <yi.y.sun@intel.com>
  8. *
  9. * Copyright (C) 2020 Genesys Logic, Inc.
  10. * Authors: Ben Chuang <ben.chuang@genesyslogic.com.tw>
  11. *
  12. * Copyright (C) 2020 Linaro Limited
  13. * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
  14. *
  15. * Copyright (C) 2022 Genesys Logic, Inc.
  16. * Authors: Jason Lai <jason.lai@genesyslogic.com.tw>
  17. *
  18. * Copyright (C) 2023 Genesys Logic, Inc.
  19. * Authors: Victor Shih <victor.shih@genesyslogic.com.tw>
  20. *
  21. * Support for SD UHS-II cards
  22. */
  23. #include <linux/err.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/mmc/host.h>
  26. #include <linux/mmc/card.h>
  27. #include <linux/mmc/mmc.h>
  28. #include <linux/mmc/sd.h>
  29. #include <linux/mmc/sd_uhs2.h>
  30. #include "card.h"
  31. #include "core.h"
  32. #include "bus.h"
  33. #include "sd.h"
  34. #include "sd_ops.h"
  35. #include "mmc_ops.h"
  36. #define UHS2_WAIT_CFG_COMPLETE_PERIOD_US (1 * 1000)
  37. #define UHS2_WAIT_CFG_COMPLETE_TIMEOUT_MS 100
  38. static const unsigned int sd_uhs2_freqs[] = { 52000000, 26000000 };
  39. struct sd_uhs2_wait_active_state_data {
  40. struct mmc_host *host;
  41. struct mmc_command *cmd;
  42. };
  43. static int sd_uhs2_power_up(struct mmc_host *host)
  44. {
  45. if (host->ios.power_mode == MMC_POWER_ON)
  46. return 0;
  47. host->ios.vdd = fls(host->ocr_avail) - 1;
  48. host->ios.clock = host->f_init;
  49. host->ios.timing = MMC_TIMING_UHS2_SPEED_A;
  50. host->ios.power_mode = MMC_POWER_ON;
  51. return host->ops->uhs2_control(host, UHS2_SET_IOS);
  52. }
  53. static int sd_uhs2_power_off(struct mmc_host *host)
  54. {
  55. int err;
  56. if (host->ios.power_mode == MMC_POWER_OFF)
  57. return 0;
  58. host->ios.vdd = 0;
  59. host->ios.clock = 0;
  60. host->ios.power_mode = MMC_POWER_OFF;
  61. host->uhs2_sd_tran = false;
  62. err = host->ops->uhs2_control(host, UHS2_SET_IOS);
  63. if (err)
  64. return err;
  65. /* For consistency, let's restore the initial timing. */
  66. host->ios.timing = MMC_TIMING_LEGACY;
  67. return 0;
  68. }
  69. /*
  70. * Run the phy initialization sequence, which mainly relies on the UHS-II host
  71. * to check that we reach the expected electrical state, between the host and
  72. * the card.
  73. */
  74. static int sd_uhs2_phy_init(struct mmc_host *host)
  75. {
  76. int err;
  77. err = host->ops->uhs2_control(host, UHS2_PHY_INIT);
  78. if (err) {
  79. pr_debug("%s: failed to initial phy for UHS-II!\n",
  80. mmc_hostname(host));
  81. }
  82. return err;
  83. }
  84. /*
  85. * sd_uhs2_cmd_assemble() - build up UHS-II command packet which is embedded in
  86. * mmc_command structure
  87. * @cmd: MMC command to executed
  88. * @uhs2_cmd: UHS2 command corresponded to MMC command
  89. * @header: Header field of UHS-II command cxpacket
  90. * @arg: Argument field of UHS-II command packet
  91. * @payload: Payload field of UHS-II command packet
  92. * @plen: Payload length
  93. * @resp: Response buffer is allocated by caller and it is used to keep
  94. * the response of CM-TRAN command. For SD-TRAN command, uhs2_resp
  95. * should be null and SD-TRAN command response should be stored in
  96. * resp of mmc_command.
  97. * @resp_len: Response buffer length
  98. *
  99. * The uhs2_command structure contains message packets which are transmited/
  100. * received on UHS-II bus. This function fills in the contents of uhs2_command
  101. * structure and embededs UHS2 command into mmc_command structure, which is used
  102. * in legacy SD operation functions.
  103. *
  104. */
  105. static void sd_uhs2_cmd_assemble(struct mmc_command *cmd,
  106. struct uhs2_command *uhs2_cmd,
  107. u8 plen, u8 resp_len)
  108. {
  109. uhs2_cmd->payload_len = plen * sizeof(u32);
  110. uhs2_cmd->packet_len = uhs2_cmd->payload_len + 4;
  111. cmd->uhs2_cmd = uhs2_cmd;
  112. cmd->uhs2_cmd->uhs2_resp_len = resp_len;
  113. }
  114. /*
  115. * Do the early initialization of the card, by sending the device init broadcast
  116. * command and wait for the process to be completed.
  117. */
  118. static int sd_uhs2_dev_init(struct mmc_host *host)
  119. {
  120. struct mmc_command cmd = {0};
  121. struct uhs2_command uhs2_cmd = {};
  122. u32 cnt;
  123. u32 dap, gap, resp_gap;
  124. u32 payload0;
  125. u8 gd = 0;
  126. int err;
  127. dap = host->uhs2_caps.dap;
  128. gap = host->uhs2_caps.gap;
  129. /*
  130. * Refer to UHS-II Addendum Version 1.02 Figure 6-21 to see DEVICE_INIT CCMD format.
  131. * Head:
  132. * - Control Write(R/W=1) with 4-Byte payload(PLEN=01b).
  133. * - IOADR = CMD_BASE + 002h
  134. * Payload:
  135. * - bit [3:0] : GAP(Group Allocated Power)
  136. * - bit [7:4] : GD(Group Descriptor)
  137. * - bit [11] : Complete Flag
  138. * - bit [15:12]: DAP(Device Allocated Power)
  139. */
  140. uhs2_cmd.header = UHS2_NATIVE_PACKET | UHS2_PACKET_TYPE_CCMD;
  141. uhs2_cmd.arg = ((UHS2_DEV_CMD_DEVICE_INIT & 0xFF) << 8) |
  142. UHS2_NATIVE_CMD_WRITE |
  143. UHS2_NATIVE_CMD_PLEN_4B |
  144. (UHS2_DEV_CMD_DEVICE_INIT >> 8);
  145. /*
  146. * Refer to UHS-II Addendum Version 1.02 section 6.3.1.
  147. * Max. time from DEVICE_INIT CCMD EOP reception on Device
  148. * Rx to its SOP transmission on Device Tx(Tfwd_init_cmd) is
  149. * 1 second.
  150. */
  151. cmd.busy_timeout = 1000;
  152. /*
  153. * Refer to UHS-II Addendum Version 1.02 section 6.2.6.3.
  154. * Let's retry the DEVICE_INIT command no more than 30 times.
  155. */
  156. for (cnt = 0; cnt < 30; cnt++) {
  157. payload0 = ((dap & 0xF) << 12) |
  158. UHS2_DEV_INIT_COMPLETE_FLAG |
  159. ((gd & 0xF) << 4) |
  160. (gap & 0xF);
  161. uhs2_cmd.payload[0] = (__force __be32)payload0;
  162. sd_uhs2_cmd_assemble(&cmd, &uhs2_cmd, UHS2_DEV_INIT_PAYLOAD_LEN,
  163. UHS2_DEV_INIT_RESP_LEN);
  164. err = mmc_wait_for_cmd(host, &cmd, 0);
  165. if (err) {
  166. pr_err("%s: %s: UHS2 CMD send fail, err= 0x%x!\n",
  167. mmc_hostname(host), __func__, err);
  168. continue;
  169. }
  170. if (uhs2_cmd.uhs2_resp[3] != (UHS2_DEV_CMD_DEVICE_INIT & 0xFF)) {
  171. pr_err("%s: DEVICE_INIT response is wrong!\n",
  172. mmc_hostname(host));
  173. return -EIO;
  174. }
  175. if (uhs2_cmd.uhs2_resp[5] & 0x8) {
  176. host->uhs2_caps.group_desc = gd;
  177. return 0;
  178. }
  179. resp_gap = uhs2_cmd.uhs2_resp[4] & 0x0F;
  180. if (gap == resp_gap)
  181. gd++;
  182. }
  183. if (err) {
  184. pr_err("%s: %s: UHS2 CMD send fail, err= 0x%x!\n",
  185. mmc_hostname(host), __func__, err);
  186. return err;
  187. }
  188. return 0;
  189. }
  190. /*
  191. * Run the enumeration process by sending the enumerate command to the card.
  192. * Note that, we currently support only the point to point connection, which
  193. * means only one card can be attached per host/slot.
  194. */
  195. static int sd_uhs2_enum(struct mmc_host *host, u32 *node_id)
  196. {
  197. struct mmc_command cmd = {0};
  198. struct uhs2_command uhs2_cmd = {};
  199. u32 payload0;
  200. u8 id_f = 0xF, id_l = 0x0;
  201. int err;
  202. /*
  203. * Refer to UHS-II Addendum Version 1.02 Figure 6-28 to see ENUMERATE CCMD format.
  204. * Header:
  205. * - Control Write(R/W=1) with 4-Byte payload(PLEN=01b).
  206. * - IOADR = CMD_BASE + 003h
  207. * Payload:
  208. * - bit [3:0]: ID_L(Last Node ID)
  209. * - bit [7:4]: ID_F(First Node ID)
  210. */
  211. uhs2_cmd.header = UHS2_NATIVE_PACKET | UHS2_PACKET_TYPE_CCMD;
  212. uhs2_cmd.arg = ((UHS2_DEV_CMD_ENUMERATE & 0xFF) << 8) |
  213. UHS2_NATIVE_CMD_WRITE |
  214. UHS2_NATIVE_CMD_PLEN_4B |
  215. (UHS2_DEV_CMD_ENUMERATE >> 8);
  216. payload0 = (id_f << 4) | id_l;
  217. uhs2_cmd.payload[0] = cpu_to_be32(payload0);
  218. sd_uhs2_cmd_assemble(&cmd, &uhs2_cmd, UHS2_DEV_ENUM_PAYLOAD_LEN, UHS2_DEV_ENUM_RESP_LEN);
  219. err = mmc_wait_for_cmd(host, &cmd, 0);
  220. if (err) {
  221. pr_err("%s: %s: UHS2 CMD send fail, err= 0x%x!\n",
  222. mmc_hostname(host), __func__, err);
  223. return err;
  224. }
  225. if (uhs2_cmd.uhs2_resp[3] != (UHS2_DEV_CMD_ENUMERATE & 0xFF)) {
  226. pr_err("%s: ENUMERATE response is wrong!\n",
  227. mmc_hostname(host));
  228. return -EIO;
  229. }
  230. id_f = (uhs2_cmd.uhs2_resp[4] >> 4) & 0xF;
  231. id_l = uhs2_cmd.uhs2_resp[4] & 0xF;
  232. *node_id = id_f;
  233. return 0;
  234. }
  235. /*
  236. * Read the UHS-II configuration registers (CFG_REG) of the card, by sending it
  237. * commands and by parsing the responses. Store a copy of the relevant data in
  238. * card->uhs2_config.
  239. */
  240. static int sd_uhs2_config_read(struct mmc_host *host, struct mmc_card *card)
  241. {
  242. struct mmc_command cmd = {0};
  243. struct uhs2_command uhs2_cmd = {};
  244. u32 cap;
  245. int err;
  246. /*
  247. * Use Control Read CCMD to read Generic Capability from Configuration Register.
  248. * - Control Write(R/W=1) with 4-Byte payload(PLEN=01b).
  249. * - IOADR = Generic Capability Register(CFG_BASE + 000h)
  250. */
  251. uhs2_cmd.header = UHS2_NATIVE_PACKET | UHS2_PACKET_TYPE_CCMD | card->uhs2_config.node_id;
  252. uhs2_cmd.arg = ((UHS2_DEV_CONFIG_GEN_CAPS & 0xFF) << 8) |
  253. UHS2_NATIVE_CMD_READ |
  254. UHS2_NATIVE_CMD_PLEN_4B |
  255. (UHS2_DEV_CONFIG_GEN_CAPS >> 8);
  256. /*
  257. * There is no payload because per spec, there should be
  258. * no payload field for read CCMD.
  259. * Plen is set in arg. Per spec, plen for read CCMD
  260. * represents the len of read data which is assigned in payload
  261. * of following RES (p136).
  262. */
  263. sd_uhs2_cmd_assemble(&cmd, &uhs2_cmd, 0, 0);
  264. err = mmc_wait_for_cmd(host, &cmd, 0);
  265. if (err) {
  266. pr_err("%s: %s: UHS2 CMD send fail, err= 0x%x!\n",
  267. mmc_hostname(host), __func__, err);
  268. return err;
  269. }
  270. /*
  271. * Generic Capability Register:
  272. * bit [7:0] : Reserved
  273. * bit [13:8] : Device-Specific Number of Lanes and Functionality
  274. * bit 8: 2L-HD
  275. * bit 9: 2D-1U FD
  276. * bit 10: 1D-2U FD
  277. * bit 11: 2D-2U FD
  278. * Others: Reserved
  279. * bit [14] : DADR Length
  280. * 0: 4 bytes
  281. * 1: Reserved
  282. * bit [23:16]: Application Type
  283. * bit 16: 0=Non-SD memory, 1=SD memory
  284. * bit 17: 0=Non-SDIO, 1=SDIO
  285. * bit 18: 0=Card, 1=Embedded
  286. * bit [63:24]: Reserved
  287. */
  288. cap = cmd.resp[0];
  289. card->uhs2_config.n_lanes =
  290. (cap >> UHS2_DEV_CONFIG_N_LANES_POS) &
  291. UHS2_DEV_CONFIG_N_LANES_MASK;
  292. card->uhs2_config.dadr_len =
  293. (cap >> UHS2_DEV_CONFIG_DADR_POS) &
  294. UHS2_DEV_CONFIG_DADR_MASK;
  295. card->uhs2_config.app_type =
  296. (cap >> UHS2_DEV_CONFIG_APP_POS) &
  297. UHS2_DEV_CONFIG_APP_MASK;
  298. /*
  299. * Use Control Read CCMD to read PHY Capability from Configuration Register.
  300. * - Control Write(R/W=1) with 8-Byte payload(PLEN=10b).
  301. * - IOADR = PHY Capability Register(CFG_BASE + 002h)
  302. */
  303. uhs2_cmd.arg = ((UHS2_DEV_CONFIG_PHY_CAPS & 0xFF) << 8) |
  304. UHS2_NATIVE_CMD_READ |
  305. UHS2_NATIVE_CMD_PLEN_8B |
  306. (UHS2_DEV_CONFIG_PHY_CAPS >> 8);
  307. sd_uhs2_cmd_assemble(&cmd, &uhs2_cmd, 0, 0);
  308. err = mmc_wait_for_cmd(host, &cmd, 0);
  309. if (err) {
  310. pr_err("%s: %s: UHS2 CMD send fail, err= 0x%x!\n",
  311. mmc_hostname(host), __func__, err);
  312. return err;
  313. }
  314. /*
  315. * PHY Capability Register:
  316. * bit [3:0] : PHY Minor Revision
  317. * bit [5:4] : PHY Major Revision
  318. * bit [15] : Support Hibernate Mode
  319. * 0: Not support Hibernate Mode
  320. * 1: Support Hibernate Mode
  321. * bit [31:16]: Reserved
  322. * bit [35:32]: Device-Specific N_LSS_SYN
  323. * bit [39:36]: Device-Specific N_LSS_DIR
  324. * bit [63:40]: Reserved
  325. */
  326. cap = cmd.resp[0];
  327. card->uhs2_config.phy_minor_rev =
  328. cap & UHS2_DEV_CONFIG_PHY_MINOR_MASK;
  329. card->uhs2_config.phy_major_rev =
  330. (cap >> UHS2_DEV_CONFIG_PHY_MAJOR_POS) &
  331. UHS2_DEV_CONFIG_PHY_MAJOR_MASK;
  332. card->uhs2_config.can_hibernate =
  333. (cap >> UHS2_DEV_CONFIG_CAN_HIBER_POS) &
  334. UHS2_DEV_CONFIG_CAN_HIBER_MASK;
  335. cap = cmd.resp[1];
  336. card->uhs2_config.n_lss_sync =
  337. cap & UHS2_DEV_CONFIG_N_LSS_SYN_MASK;
  338. card->uhs2_config.n_lss_dir =
  339. (cap >> UHS2_DEV_CONFIG_N_LSS_DIR_POS) &
  340. UHS2_DEV_CONFIG_N_LSS_DIR_MASK;
  341. if (card->uhs2_config.n_lss_sync == 0)
  342. card->uhs2_config.n_lss_sync = 16 << 2;
  343. else
  344. card->uhs2_config.n_lss_sync <<= 2;
  345. if (card->uhs2_config.n_lss_dir == 0)
  346. card->uhs2_config.n_lss_dir = 16 << 3;
  347. else
  348. card->uhs2_config.n_lss_dir <<= 3;
  349. /*
  350. * Use Control Read CCMD to read LINK/TRAN Capability from Configuration Register.
  351. * - Control Write(R/W=1) with 8-Byte payload(PLEN=10b).
  352. * - IOADR = LINK/TRAN Capability Register(CFG_BASE + 004h)
  353. */
  354. uhs2_cmd.arg = ((UHS2_DEV_CONFIG_LINK_TRAN_CAPS & 0xFF) << 8) |
  355. UHS2_NATIVE_CMD_READ |
  356. UHS2_NATIVE_CMD_PLEN_8B |
  357. (UHS2_DEV_CONFIG_LINK_TRAN_CAPS >> 8);
  358. sd_uhs2_cmd_assemble(&cmd, &uhs2_cmd, 0, 0);
  359. err = mmc_wait_for_cmd(host, &cmd, 0);
  360. if (err) {
  361. pr_err("%s: %s: UHS2 CMD send fail, err= 0x%x!\n",
  362. mmc_hostname(host), __func__, err);
  363. return err;
  364. }
  365. /*
  366. * LINK/TRAN Capability Register:
  367. * bit [3:0] : LINK_TRAN Minor Revision
  368. * bit [5:4] : LINK/TRAN Major Revision
  369. * bit [7:6] : Reserved
  370. * bit [15:8] : Device-Specific N_FCU
  371. * bit [18:16]: Device Type
  372. * 001b=Host
  373. * 010b=Device
  374. * 011b=Reserved for CMD issuable Device
  375. * bit [19] : Reserved
  376. * bit [31:20]: Device-Specific MAX_BLKLEN
  377. * bit [39:32]: Device-Specific N_DATA_GAP
  378. * bit [63:40]: Reserved
  379. */
  380. cap = cmd.resp[0];
  381. card->uhs2_config.link_minor_rev =
  382. cap & UHS2_DEV_CONFIG_LT_MINOR_MASK;
  383. card->uhs2_config.link_major_rev =
  384. (cap >> UHS2_DEV_CONFIG_LT_MAJOR_POS) &
  385. UHS2_DEV_CONFIG_LT_MAJOR_MASK;
  386. card->uhs2_config.n_fcu =
  387. (cap >> UHS2_DEV_CONFIG_N_FCU_POS) &
  388. UHS2_DEV_CONFIG_N_FCU_MASK;
  389. card->uhs2_config.dev_type =
  390. (cap >> UHS2_DEV_CONFIG_DEV_TYPE_POS) &
  391. UHS2_DEV_CONFIG_DEV_TYPE_MASK;
  392. card->uhs2_config.maxblk_len =
  393. (cap >> UHS2_DEV_CONFIG_MAX_BLK_LEN_POS) &
  394. UHS2_DEV_CONFIG_MAX_BLK_LEN_MASK;
  395. cap = cmd.resp[1];
  396. card->uhs2_config.n_data_gap =
  397. cap & UHS2_DEV_CONFIG_N_DATA_GAP_MASK;
  398. if (card->uhs2_config.n_fcu == 0)
  399. card->uhs2_config.n_fcu = 256;
  400. return 0;
  401. }
  402. /*
  403. * Based on the card's and host's UHS-II capabilities, let's update the
  404. * configuration of the card and the host. This may also include to move to a
  405. * greater speed range/mode. Depending on the updated configuration, we may need
  406. * to do a soft reset of the card via sending it a GO_DORMANT_STATE command.
  407. *
  408. * In the final step, let's check if the card signals "config completion", which
  409. * indicates that the card has moved from config state into active state.
  410. */
  411. static int sd_uhs2_config_write(struct mmc_host *host, struct mmc_card *card)
  412. {
  413. struct mmc_command cmd = {0};
  414. struct uhs2_command uhs2_cmd = {};
  415. u32 payload0, payload1;
  416. u8 nMinDataGap;
  417. int err;
  418. /*
  419. * Use Control Write CCMD to set Generic Setting in Configuration Register.
  420. * - Control Write(R/W=1) with 8-Byte payload(PLEN=10b).
  421. * - IOADR = Generic Setting Register(CFG_BASE + 008h)
  422. * - Payload = New contents to be written to Generic Setting Register
  423. */
  424. uhs2_cmd.header = UHS2_NATIVE_PACKET | UHS2_PACKET_TYPE_CCMD | card->uhs2_config.node_id;
  425. uhs2_cmd.arg = ((UHS2_DEV_CONFIG_GEN_SET & 0xFF) << 8) |
  426. UHS2_NATIVE_CMD_WRITE |
  427. UHS2_NATIVE_CMD_PLEN_8B |
  428. (UHS2_DEV_CONFIG_GEN_SET >> 8);
  429. /*
  430. * Most UHS-II cards only support FD and 2L-HD mode. Other lane numbers
  431. * defined in UHS-II addendem Ver1.01 are optional.
  432. */
  433. host->uhs2_caps.n_lanes_set = UHS2_DEV_CONFIG_GEN_SET_2L_FD_HD;
  434. card->uhs2_config.n_lanes_set = UHS2_DEV_CONFIG_GEN_SET_2L_FD_HD;
  435. payload0 = card->uhs2_config.n_lanes_set << UHS2_DEV_CONFIG_N_LANES_POS;
  436. payload1 = 0;
  437. uhs2_cmd.payload[0] = cpu_to_be32(payload0);
  438. uhs2_cmd.payload[1] = cpu_to_be32(payload1);
  439. /*
  440. * There is no payload because per spec, there should be
  441. * no payload field for read CCMD.
  442. * Plen is set in arg. Per spec, plen for read CCMD
  443. * represents the len of read data which is assigned in payload
  444. * of following RES (p136).
  445. */
  446. sd_uhs2_cmd_assemble(&cmd, &uhs2_cmd, UHS2_CFG_WRITE_PAYLOAD_LEN, 0);
  447. err = mmc_wait_for_cmd(host, &cmd, 0);
  448. if (err) {
  449. pr_err("%s: %s: UHS2 CMD send fail, err= 0x%x!\n",
  450. mmc_hostname(host), __func__, err);
  451. return err;
  452. }
  453. /*
  454. * Use Control Write CCMD to set PHY Setting in Configuration Register.
  455. * - Control Write(R/W=1) with 8-Byte payload(PLEN=10b).
  456. * - IOADR = PHY Setting Register(CFG_BASE + 00Ah)
  457. * - Payload = New contents to be written to PHY Setting Register
  458. */
  459. uhs2_cmd.arg = ((UHS2_DEV_CONFIG_PHY_SET & 0xFF) << 8) |
  460. UHS2_NATIVE_CMD_WRITE |
  461. UHS2_NATIVE_CMD_PLEN_8B |
  462. (UHS2_DEV_CONFIG_PHY_SET >> 8);
  463. if (host->uhs2_caps.speed_range == UHS2_DEV_CONFIG_PHY_SET_SPEED_B) {
  464. if (card->uhs2_config.n_lanes == UHS2_DEV_CONFIG_2L_HD_FD &&
  465. host->uhs2_caps.n_lanes == UHS2_DEV_CONFIG_2L_HD_FD) {
  466. /* Support HD */
  467. host->ios.timing = MMC_TIMING_UHS2_SPEED_B_HD;
  468. nMinDataGap = 1;
  469. } else {
  470. /* Only support 2L-FD so far */
  471. host->ios.timing = MMC_TIMING_UHS2_SPEED_B;
  472. nMinDataGap = 3;
  473. }
  474. card->uhs2_config.speed_range_set = UHS2_DEV_CONFIG_PHY_SET_SPEED_B;
  475. } else {
  476. if (card->uhs2_config.n_lanes == UHS2_DEV_CONFIG_2L_HD_FD &&
  477. host->uhs2_caps.n_lanes == UHS2_DEV_CONFIG_2L_HD_FD) {
  478. /* Support HD */
  479. host->ios.timing = MMC_TIMING_UHS2_SPEED_A_HD;
  480. nMinDataGap = 1;
  481. } else {
  482. /* Only support 2L-FD so far */
  483. host->ios.timing = MMC_TIMING_UHS2_SPEED_A;
  484. nMinDataGap = 3;
  485. }
  486. card->uhs2_config.speed_range_set = UHS2_DEV_CONFIG_PHY_SET_SPEED_A;
  487. }
  488. payload0 = card->uhs2_config.speed_range_set << UHS2_DEV_CONFIG_PHY_SET_SPEED_POS;
  489. card->uhs2_config.n_lss_sync_set = (max(card->uhs2_config.n_lss_sync,
  490. host->uhs2_caps.n_lss_sync) >> 2) &
  491. UHS2_DEV_CONFIG_N_LSS_SYN_MASK;
  492. host->uhs2_caps.n_lss_sync_set = card->uhs2_config.n_lss_sync_set;
  493. card->uhs2_config.n_lss_dir_set = (max(card->uhs2_config.n_lss_dir,
  494. host->uhs2_caps.n_lss_dir) >> 3) &
  495. UHS2_DEV_CONFIG_N_LSS_DIR_MASK;
  496. host->uhs2_caps.n_lss_dir_set = card->uhs2_config.n_lss_dir_set;
  497. payload1 = (card->uhs2_config.n_lss_dir_set << UHS2_DEV_CONFIG_N_LSS_DIR_POS) |
  498. card->uhs2_config.n_lss_sync_set;
  499. uhs2_cmd.payload[0] = cpu_to_be32(payload0);
  500. uhs2_cmd.payload[1] = cpu_to_be32(payload1);
  501. memset(uhs2_cmd.uhs2_resp, 0, sizeof(uhs2_cmd.uhs2_resp));
  502. sd_uhs2_cmd_assemble(&cmd, &uhs2_cmd, UHS2_CFG_WRITE_PAYLOAD_LEN,
  503. UHS2_CFG_WRITE_PHY_SET_RESP_LEN);
  504. err = mmc_wait_for_cmd(host, &cmd, 0);
  505. if (err) {
  506. pr_err("%s: %s: UHS2 CMD send fail, err= 0x%x!\n",
  507. mmc_hostname(host), __func__, err);
  508. return err;
  509. }
  510. if ((uhs2_cmd.uhs2_resp[2] & 0x80)) {
  511. pr_err("%s: %s: UHS2 CMD not accepted, resp= 0x%x!\n",
  512. mmc_hostname(host), __func__, uhs2_cmd.uhs2_resp[2]);
  513. return -EIO;
  514. }
  515. /*
  516. * Use Control Write CCMD to set LINK/TRAN Setting in Configuration Register.
  517. * - Control Write(R/W=1) with 8-Byte payload(PLEN=10b).
  518. * - IOADR = LINK/TRAN Setting Register(CFG_BASE + 00Ch)
  519. * - Payload = New contents to be written to LINK/TRAN Setting Register
  520. */
  521. uhs2_cmd.arg = ((UHS2_DEV_CONFIG_LINK_TRAN_SET & 0xFF) << 8) |
  522. UHS2_NATIVE_CMD_WRITE |
  523. UHS2_NATIVE_CMD_PLEN_8B |
  524. (UHS2_DEV_CONFIG_LINK_TRAN_SET >> 8);
  525. if (card->uhs2_config.app_type == UHS2_DEV_CONFIG_APP_SD_MEM)
  526. card->uhs2_config.maxblk_len_set = UHS2_DEV_CONFIG_LT_SET_MAX_BLK_LEN;
  527. else
  528. card->uhs2_config.maxblk_len_set = min(card->uhs2_config.maxblk_len,
  529. host->uhs2_caps.maxblk_len);
  530. host->uhs2_caps.maxblk_len_set = card->uhs2_config.maxblk_len_set;
  531. card->uhs2_config.n_fcu_set = min(card->uhs2_config.n_fcu, host->uhs2_caps.n_fcu);
  532. host->uhs2_caps.n_fcu_set = card->uhs2_config.n_fcu_set;
  533. card->uhs2_config.n_data_gap_set = max(nMinDataGap, card->uhs2_config.n_data_gap);
  534. host->uhs2_caps.n_data_gap_set = card->uhs2_config.n_data_gap_set;
  535. host->uhs2_caps.max_retry_set = 3;
  536. card->uhs2_config.max_retry_set = host->uhs2_caps.max_retry_set;
  537. payload0 = (card->uhs2_config.maxblk_len_set << UHS2_DEV_CONFIG_MAX_BLK_LEN_POS) |
  538. (card->uhs2_config.max_retry_set << UHS2_DEV_CONFIG_LT_SET_MAX_RETRY_POS) |
  539. (card->uhs2_config.n_fcu_set << UHS2_DEV_CONFIG_N_FCU_POS);
  540. payload1 = card->uhs2_config.n_data_gap_set;
  541. uhs2_cmd.payload[0] = cpu_to_be32(payload0);
  542. uhs2_cmd.payload[1] = cpu_to_be32(payload1);
  543. sd_uhs2_cmd_assemble(&cmd, &uhs2_cmd, UHS2_CFG_WRITE_PAYLOAD_LEN, 0);
  544. err = mmc_wait_for_cmd(host, &cmd, 0);
  545. if (err) {
  546. pr_err("%s: %s: UHS2 CMD send fail, err= 0x%x!\n",
  547. mmc_hostname(host), __func__, err);
  548. return err;
  549. }
  550. /*
  551. * Use Control Write CCMD to set Config Completion(payload bit 63) in Generic Setting
  552. * Register.
  553. * Header:
  554. * - Control Write(R/W=1) with 8-Byte payload(PLEN=10b).
  555. * - IOADR = PGeneric Setting Register(CFG_BASE + 008h)
  556. * Payload:
  557. * - bit [63]: Config Completion
  558. *
  559. * DLSM transits to Active state immediately when Config Completion is set to 1.
  560. */
  561. uhs2_cmd.arg = ((UHS2_DEV_CONFIG_GEN_SET & 0xFF) << 8) |
  562. UHS2_NATIVE_CMD_WRITE |
  563. UHS2_NATIVE_CMD_PLEN_8B |
  564. (UHS2_DEV_CONFIG_GEN_SET >> 8);
  565. payload0 = 0;
  566. payload1 = UHS2_DEV_CONFIG_GEN_SET_CFG_COMPLETE;
  567. uhs2_cmd.payload[0] = cpu_to_be32(payload0);
  568. uhs2_cmd.payload[1] = cpu_to_be32(payload1);
  569. memset(uhs2_cmd.uhs2_resp, 0, sizeof(uhs2_cmd.uhs2_resp));
  570. sd_uhs2_cmd_assemble(&cmd, &uhs2_cmd, UHS2_CFG_WRITE_PAYLOAD_LEN,
  571. UHS2_CFG_WRITE_GENERIC_SET_RESP_LEN);
  572. err = mmc_wait_for_cmd(host, &cmd, 0);
  573. if (err) {
  574. pr_err("%s: %s: UHS2 CMD send fail, err= 0x%x!\n",
  575. mmc_hostname(host), __func__, err);
  576. return err;
  577. }
  578. /* Set host Config Setting registers */
  579. err = host->ops->uhs2_control(host, UHS2_SET_CONFIG);
  580. if (err) {
  581. pr_err("%s: %s: UHS2 SET_CONFIG fail!\n", mmc_hostname(host), __func__);
  582. return err;
  583. }
  584. return 0;
  585. }
  586. static int sd_uhs2_go_dormant(struct mmc_host *host, u32 node_id)
  587. {
  588. struct mmc_command cmd = {0};
  589. struct uhs2_command uhs2_cmd = {};
  590. int err;
  591. /* Disable Normal INT */
  592. err = host->ops->uhs2_control(host, UHS2_DISABLE_INT);
  593. if (err) {
  594. pr_err("%s: %s: UHS2 DISABLE_INT fail!\n",
  595. mmc_hostname(host), __func__);
  596. return err;
  597. }
  598. /*
  599. * Refer to UHS-II Addendum Version 1.02 Figure 6-17 to see GO_DORMANT_STATE CCMD format.
  600. * Header:
  601. * - Control Write(R/W=1) with 4-Byte payload(PLEN=01b).
  602. * - IOADR = CMD_BASE + 001h
  603. * Payload:
  604. * - bit [7]: HBR(Entry to Hibernate Mode)
  605. * 1: Host intends to enter Hibernate mode during Dormant state.
  606. * The default setting is 0 because hibernate is currently not supported.
  607. */
  608. uhs2_cmd.header = UHS2_NATIVE_PACKET | UHS2_PACKET_TYPE_CCMD | node_id;
  609. uhs2_cmd.arg = ((UHS2_DEV_CMD_GO_DORMANT_STATE & 0xFF) << 8) |
  610. UHS2_NATIVE_CMD_WRITE |
  611. UHS2_NATIVE_CMD_PLEN_4B |
  612. (UHS2_DEV_CMD_GO_DORMANT_STATE >> 8);
  613. sd_uhs2_cmd_assemble(&cmd, &uhs2_cmd, UHS2_GO_DORMANT_PAYLOAD_LEN, 0);
  614. err = mmc_wait_for_cmd(host, &cmd, 0);
  615. if (err) {
  616. pr_err("%s: %s: UHS2 CMD send fail, err= 0x%x!\n",
  617. mmc_hostname(host), __func__, err);
  618. return err;
  619. }
  620. /* Check Dormant State in Present */
  621. err = host->ops->uhs2_control(host, UHS2_CHECK_DORMANT);
  622. if (err)
  623. return err;
  624. /* Disable UHS2 card clock */
  625. err = host->ops->uhs2_control(host, UHS2_DISABLE_CLK);
  626. if (err)
  627. return err;
  628. /* Restore sd clock */
  629. mmc_delay(5);
  630. err = host->ops->uhs2_control(host, UHS2_ENABLE_CLK);
  631. if (err)
  632. return err;
  633. /* Enable Normal INT */
  634. err = host->ops->uhs2_control(host, UHS2_ENABLE_INT);
  635. if (err)
  636. return err;
  637. /* Detect UHS2 */
  638. err = host->ops->uhs2_control(host, UHS2_PHY_INIT);
  639. if (err)
  640. return err;
  641. return 0;
  642. }
  643. static int sd_uhs2_wait_active_state_cb(void *cb_data, bool *busy)
  644. {
  645. struct sd_uhs2_wait_active_state_data *data = cb_data;
  646. struct mmc_host *host = data->host;
  647. struct mmc_command *cmd = data->cmd;
  648. int err;
  649. err = mmc_wait_for_cmd(host, cmd, 0);
  650. if (err)
  651. return err;
  652. if (cmd->resp[1] & UHS2_DEV_CONFIG_GEN_SET_CFG_COMPLETE)
  653. *busy = false;
  654. else
  655. *busy = true;
  656. return 0;
  657. }
  658. static int sd_uhs2_go_dormant_state(struct mmc_host *host, u32 node_id)
  659. {
  660. struct mmc_command cmd = {0};
  661. struct uhs2_command uhs2_cmd = {};
  662. int err;
  663. struct sd_uhs2_wait_active_state_data cb_data = {
  664. .host = host,
  665. .cmd = &cmd
  666. };
  667. err = sd_uhs2_go_dormant(host, node_id);
  668. if (err) {
  669. pr_err("%s: %s: UHS2 GO_DORMANT_STATE fail, err= 0x%x!\n",
  670. mmc_hostname(host), __func__, err);
  671. return err;
  672. }
  673. /*
  674. * Use Control Read CCMD to check Config Completion(bit 63) in Generic Setting Register.
  675. * - Control Read(R/W=0) with 8-Byte payload(PLEN=10b).
  676. * - IOADR = Generic Setting Register(CFG_BASE + 008h)
  677. *
  678. * When UHS-II card been switched to new speed mode, it will set Config Completion to 1.
  679. */
  680. uhs2_cmd.header = UHS2_NATIVE_PACKET | UHS2_PACKET_TYPE_CCMD | node_id;
  681. uhs2_cmd.arg = ((UHS2_DEV_CONFIG_GEN_SET & 0xFF) << 8) |
  682. UHS2_NATIVE_CMD_READ |
  683. UHS2_NATIVE_CMD_PLEN_8B |
  684. (UHS2_DEV_CONFIG_GEN_SET >> 8);
  685. sd_uhs2_cmd_assemble(&cmd, &uhs2_cmd, 0, 0);
  686. err = __mmc_poll_for_busy(host, UHS2_WAIT_CFG_COMPLETE_PERIOD_US,
  687. UHS2_WAIT_CFG_COMPLETE_TIMEOUT_MS,
  688. &sd_uhs2_wait_active_state_cb, &cb_data);
  689. if (err) {
  690. pr_err("%s: %s: Not switch to Active in 100 ms\n", mmc_hostname(host), __func__);
  691. return err;
  692. }
  693. return 0;
  694. }
  695. /*
  696. * Allocate the data structure for the mmc_card and run the UHS-II specific
  697. * initialization sequence.
  698. */
  699. static int sd_uhs2_init_card(struct mmc_host *host, struct mmc_card *oldcard)
  700. {
  701. struct mmc_card *card;
  702. u32 node_id = 0;
  703. int err;
  704. err = sd_uhs2_dev_init(host);
  705. if (err)
  706. return err;
  707. err = sd_uhs2_enum(host, &node_id);
  708. if (err)
  709. return err;
  710. if (oldcard) {
  711. card = oldcard;
  712. } else {
  713. card = mmc_alloc_card(host, &sd_type);
  714. if (IS_ERR(card))
  715. return PTR_ERR(card);
  716. }
  717. card->uhs2_config.node_id = node_id;
  718. card->type = MMC_TYPE_SD;
  719. err = sd_uhs2_config_read(host, card);
  720. if (err)
  721. goto err;
  722. err = sd_uhs2_config_write(host, card);
  723. if (err)
  724. goto err;
  725. /* If change speed to Range B, need to GO_DORMANT_STATE */
  726. if (host->ios.timing == MMC_TIMING_UHS2_SPEED_B ||
  727. host->ios.timing == MMC_TIMING_UHS2_SPEED_B_HD) {
  728. err = sd_uhs2_go_dormant_state(host, node_id);
  729. if (err)
  730. goto err;
  731. }
  732. host->uhs2_sd_tran = true;
  733. host->card = card;
  734. return 0;
  735. err:
  736. if (!oldcard)
  737. mmc_remove_card(card);
  738. return err;
  739. }
  740. /*
  741. * Initialize the UHS-II card through the SD-TRAN transport layer. This enables
  742. * commands/requests to be backwards compatible through the legacy SD protocol.
  743. * UHS-II cards has a specific power limit specified for VDD1/VDD2, that should
  744. * be set through a legacy CMD6. Note that, the power limit that becomes set,
  745. * survives a soft reset through the GO_DORMANT_STATE command.
  746. */
  747. static int sd_uhs2_legacy_init(struct mmc_host *host, struct mmc_card *card,
  748. bool reinit)
  749. {
  750. int err;
  751. u32 cid[4];
  752. u32 ocr;
  753. u32 rocr;
  754. u8 *status;
  755. int ro;
  756. /* Send CMD0 to reset SD card */
  757. err = __mmc_go_idle(host);
  758. if (err)
  759. return err;
  760. mmc_delay(1);
  761. /* Send CMD8 to communicate SD interface operation condition */
  762. err = mmc_send_if_cond(host, host->ocr_avail);
  763. if (err)
  764. return err;
  765. /*
  766. * Probe SD card working voltage.
  767. */
  768. err = mmc_send_app_op_cond(host, 0, &ocr);
  769. if (err)
  770. return err;
  771. card->ocr = ocr;
  772. /*
  773. * Some SD cards claims an out of spec VDD voltage range. Let's treat
  774. * these bits as being in-valid and especially also bit7.
  775. */
  776. ocr &= ~0x7FFF;
  777. rocr = mmc_select_voltage(host, ocr);
  778. /*
  779. * Some cards have zero value of rocr in UHS-II mode. Assign host's
  780. * ocr value to rocr.
  781. */
  782. if (!rocr)
  783. rocr = host->ocr_avail;
  784. rocr |= (SD_OCR_CCS | SD_OCR_XPC);
  785. /* Wait SD power on ready */
  786. ocr = rocr;
  787. err = mmc_send_app_op_cond(host, ocr, &rocr);
  788. if (err)
  789. return err;
  790. err = mmc_send_cid(host, cid);
  791. if (err)
  792. return err;
  793. if (reinit) {
  794. if (memcmp(cid, card->raw_cid, sizeof(cid)) != 0) {
  795. pr_debug("%s: Perhaps the card was replaced\n",
  796. mmc_hostname(host));
  797. return -ENOENT;
  798. }
  799. } else {
  800. memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
  801. mmc_decode_cid(card);
  802. }
  803. /*
  804. * For native busses: get card RCA and quit open drain mode.
  805. */
  806. err = mmc_send_relative_addr(host, &card->rca);
  807. if (err)
  808. return err;
  809. err = mmc_sd_get_csd(card, false);
  810. if (err)
  811. return err;
  812. /*
  813. * Select card, as all following commands rely on that.
  814. */
  815. err = mmc_select_card(card);
  816. if (err)
  817. return err;
  818. /*
  819. * Fetch SCR from card.
  820. */
  821. err = mmc_app_send_scr(card);
  822. if (err)
  823. return err;
  824. err = mmc_decode_scr(card);
  825. if (err)
  826. return err;
  827. /*
  828. * Switch to high power consumption mode.
  829. * Even switch failed, sd card can still work at lower power consumption mode, but
  830. * performance will be lower than high power consumption mode.
  831. */
  832. status = kmalloc(64, GFP_KERNEL);
  833. if (!status)
  834. return -ENOMEM;
  835. if (!(card->csd.cmdclass & CCC_SWITCH)) {
  836. pr_warn("%s: card lacks mandatory switch function, performance might suffer\n",
  837. mmc_hostname(card->host));
  838. } else {
  839. /*
  840. * Send CMD6 to set Maximum Power Consumption to get better
  841. * performance. Ignore errors and continue.
  842. */
  843. err = mmc_sd_switch(card, 0, 3, SD4_SET_POWER_LIMIT_1_80W, status);
  844. if (!err)
  845. mmc_sd_switch(card, 1, 3, SD4_SET_POWER_LIMIT_1_80W, status);
  846. }
  847. /*
  848. * Check if read-only switch is active.
  849. */
  850. ro = mmc_sd_get_ro(host);
  851. if (ro < 0)
  852. pr_warn("%s: host does not support read-only switch, assuming write-enable\n",
  853. mmc_hostname(host));
  854. else if (ro > 0)
  855. mmc_card_set_readonly(card);
  856. kfree(status);
  857. return 0;
  858. }
  859. static int sd_uhs2_reinit(struct mmc_host *host)
  860. {
  861. struct mmc_card *card = host->card;
  862. int err;
  863. err = sd_uhs2_power_up(host);
  864. if (err)
  865. return err;
  866. err = sd_uhs2_phy_init(host);
  867. if (err)
  868. return err;
  869. err = sd_uhs2_init_card(host, card);
  870. if (err)
  871. return err;
  872. return sd_uhs2_legacy_init(host, card, true);
  873. }
  874. static void sd_uhs2_remove(struct mmc_host *host)
  875. {
  876. mmc_remove_card(host->card);
  877. host->card = NULL;
  878. }
  879. static int sd_uhs2_alive(struct mmc_host *host)
  880. {
  881. return mmc_send_status(host->card, NULL);
  882. }
  883. static void sd_uhs2_detect(struct mmc_host *host)
  884. {
  885. int err;
  886. mmc_get_card(host->card, NULL);
  887. err = _mmc_detect_card_removed(host);
  888. mmc_put_card(host->card, NULL);
  889. if (err) {
  890. sd_uhs2_remove(host);
  891. mmc_claim_host(host);
  892. mmc_detach_bus(host);
  893. sd_uhs2_power_off(host);
  894. mmc_release_host(host);
  895. }
  896. }
  897. static int _sd_uhs2_suspend(struct mmc_host *host)
  898. {
  899. struct mmc_card *card = host->card;
  900. mmc_claim_host(host);
  901. if (mmc_card_suspended(card))
  902. goto out;
  903. sd_uhs2_power_off(host);
  904. mmc_card_set_suspended(card);
  905. out:
  906. mmc_release_host(host);
  907. return 0;
  908. }
  909. /*
  910. * Callback for suspend
  911. */
  912. static int sd_uhs2_suspend(struct mmc_host *host)
  913. {
  914. int err;
  915. err = _sd_uhs2_suspend(host);
  916. if (!err) {
  917. pm_runtime_disable(&host->card->dev);
  918. pm_runtime_set_suspended(&host->card->dev);
  919. }
  920. return err;
  921. }
  922. /*
  923. * This function tries to determine if the same card is still present
  924. * and, if so, restore all state to it.
  925. */
  926. static int _mmc_sd_uhs2_resume(struct mmc_host *host)
  927. {
  928. int err = 0;
  929. mmc_claim_host(host);
  930. if (!mmc_card_suspended(host->card))
  931. goto out;
  932. /* Power up UHS2 SD card and re-initialize it. */
  933. err = sd_uhs2_reinit(host);
  934. mmc_card_clr_suspended(host->card);
  935. out:
  936. mmc_release_host(host);
  937. return err;
  938. }
  939. /*
  940. * Callback for resume
  941. */
  942. static int sd_uhs2_resume(struct mmc_host *host)
  943. {
  944. pm_runtime_enable(&host->card->dev);
  945. return 0;
  946. }
  947. /*
  948. * Callback for runtime_suspend.
  949. */
  950. static int sd_uhs2_runtime_suspend(struct mmc_host *host)
  951. {
  952. int err;
  953. if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
  954. return 0;
  955. err = _sd_uhs2_suspend(host);
  956. if (err)
  957. pr_err("%s: error %d doing aggressive suspend\n", mmc_hostname(host), err);
  958. return err;
  959. }
  960. static int sd_uhs2_runtime_resume(struct mmc_host *host)
  961. {
  962. int err;
  963. err = _mmc_sd_uhs2_resume(host);
  964. if (err && err != -ENOMEDIUM)
  965. pr_err("%s: error %d doing runtime resume\n", mmc_hostname(host), err);
  966. return err;
  967. }
  968. static int sd_uhs2_hw_reset(struct mmc_host *host)
  969. {
  970. sd_uhs2_power_off(host);
  971. /* Wait at least 1 ms according to SD spec */
  972. mmc_delay(1);
  973. return sd_uhs2_reinit(host);
  974. }
  975. static const struct mmc_bus_ops sd_uhs2_ops = {
  976. .remove = sd_uhs2_remove,
  977. .alive = sd_uhs2_alive,
  978. .detect = sd_uhs2_detect,
  979. .suspend = sd_uhs2_suspend,
  980. .resume = sd_uhs2_resume,
  981. .runtime_suspend = sd_uhs2_runtime_suspend,
  982. .runtime_resume = sd_uhs2_runtime_resume,
  983. .shutdown = sd_uhs2_suspend,
  984. .hw_reset = sd_uhs2_hw_reset,
  985. };
  986. static int sd_uhs2_attach(struct mmc_host *host)
  987. {
  988. int err;
  989. err = sd_uhs2_power_up(host);
  990. if (err)
  991. goto err;
  992. err = sd_uhs2_phy_init(host);
  993. if (err)
  994. goto err;
  995. err = sd_uhs2_init_card(host, NULL);
  996. if (err)
  997. goto err;
  998. err = sd_uhs2_legacy_init(host, host->card, false);
  999. if (err)
  1000. goto remove_card;
  1001. mmc_attach_bus(host, &sd_uhs2_ops);
  1002. mmc_release_host(host);
  1003. err = mmc_add_card(host->card);
  1004. if (err)
  1005. goto remove_card;
  1006. mmc_claim_host(host);
  1007. return 0;
  1008. remove_card:
  1009. sd_uhs2_remove(host);
  1010. mmc_claim_host(host);
  1011. err:
  1012. mmc_detach_bus(host);
  1013. sd_uhs2_power_off(host);
  1014. return err;
  1015. }
  1016. /**
  1017. * mmc_attach_sd_uhs2 - select UHS2 interface
  1018. * @host: MMC host
  1019. *
  1020. * Try to select UHS2 interface and initialize the bus for a given
  1021. * frequency, @freq.
  1022. *
  1023. * Return: 0 on success, non-zero error on failure
  1024. */
  1025. int mmc_attach_sd_uhs2(struct mmc_host *host)
  1026. {
  1027. int i, err;
  1028. if (!(host->caps2 & MMC_CAP2_SD_UHS2))
  1029. return -EOPNOTSUPP;
  1030. /* Turn off the legacy SD interface before trying with UHS-II. */
  1031. mmc_power_off(host);
  1032. /*
  1033. * Start UHS-II initialization at 52MHz and possibly make a retry at
  1034. * 26MHz according to the spec. It's required that the host driver
  1035. * validates ios->clock, to set a rate within the correct range.
  1036. */
  1037. for (i = 0; i < ARRAY_SIZE(sd_uhs2_freqs); i++) {
  1038. host->f_init = sd_uhs2_freqs[i];
  1039. pr_debug("%s: %s: trying to init UHS-II card at %u Hz\n",
  1040. mmc_hostname(host), __func__, host->f_init);
  1041. err = sd_uhs2_attach(host);
  1042. if (!err)
  1043. break;
  1044. }
  1045. return err;
  1046. }
  1047. /*
  1048. * mmc_uhs2_prepare_cmd - prepare for SD command packet
  1049. * @host: MMC host
  1050. * @mrq: MMC request
  1051. *
  1052. * Initialize and fill in a header and a payload of SD command packet.
  1053. * The caller should allocate uhs2_command in host->cmd->uhs2_cmd in
  1054. * advance.
  1055. *
  1056. * Return: 0 on success, non-zero error on failure
  1057. */
  1058. void mmc_uhs2_prepare_cmd(struct mmc_host *host, struct mmc_request *mrq)
  1059. {
  1060. struct mmc_command *cmd;
  1061. struct uhs2_command *uhs2_cmd;
  1062. u8 plen;
  1063. cmd = mrq->cmd;
  1064. cmd->uhs2_cmd = &mrq->uhs2_cmd;
  1065. uhs2_cmd = cmd->uhs2_cmd;
  1066. uhs2_cmd->header = host->card->uhs2_config.node_id;
  1067. if ((cmd->flags & MMC_CMD_MASK) == MMC_CMD_ADTC)
  1068. uhs2_cmd->header |= UHS2_PACKET_TYPE_DCMD;
  1069. else
  1070. uhs2_cmd->header |= UHS2_PACKET_TYPE_CCMD;
  1071. uhs2_cmd->arg = cmd->opcode << UHS2_SD_CMD_INDEX_POS;
  1072. if (host->uhs2_app_cmd) {
  1073. uhs2_cmd->arg |= UHS2_SD_CMD_APP;
  1074. host->uhs2_app_cmd = false;
  1075. }
  1076. /*
  1077. * UHS-II Addendum 7.2.1.2
  1078. * Host may set DM to 1 for DCMD which supports multi-block read/write regardless of
  1079. * data transfer length (e.g., CMD18, CMD25). Otherwise, it shall not set DM to 1.
  1080. * (e.g., CMD6, CMD17, CMD24). These rules are also applied to other multi-block read/write
  1081. * commands defined in other Part of SD specifications (for example, Host may set DM to 1
  1082. * for ACMD18 or ACMD25).
  1083. */
  1084. if (mmc_op_multi(cmd->opcode))
  1085. cmd->uhs2_cmd->tmode_half_duplex = mmc_card_uhs2_hd_mode(host);
  1086. else
  1087. cmd->uhs2_cmd->tmode_half_duplex = 0;
  1088. uhs2_cmd = cmd->uhs2_cmd;
  1089. plen = 2; /* at the maximum */
  1090. if ((cmd->flags & MMC_CMD_MASK) == MMC_CMD_ADTC &&
  1091. cmd->uhs2_cmd->tmode_half_duplex) {
  1092. if (mmc_card_uhs2_hd_mode(host))
  1093. uhs2_cmd->arg |= UHS2_DCMD_2L_HD_MODE;
  1094. uhs2_cmd->arg |= UHS2_DCMD_LM_TLEN_EXIST;
  1095. if (cmd->data->blocks == 1 &&
  1096. cmd->data->blksz != 512 &&
  1097. cmd->opcode != MMC_READ_SINGLE_BLOCK &&
  1098. cmd->opcode != MMC_WRITE_BLOCK) {
  1099. uhs2_cmd->arg |= UHS2_DCMD_TLUM_BYTE_MODE;
  1100. uhs2_cmd->payload[1] = cpu_to_be32(cmd->data->blksz);
  1101. } else {
  1102. uhs2_cmd->payload[1] = cpu_to_be32(cmd->data->blocks);
  1103. }
  1104. } else {
  1105. plen = 1;
  1106. }
  1107. uhs2_cmd->payload[0] = cpu_to_be32(cmd->arg);
  1108. sd_uhs2_cmd_assemble(cmd, uhs2_cmd, plen, 0);
  1109. }