core.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461
  1. /*
  2. * linux/drivers/mmc/core/core.c
  3. *
  4. * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
  5. * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
  6. * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
  7. * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/completion.h>
  17. #include <linux/device.h>
  18. #include <linux/delay.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/err.h>
  21. #include <linux/leds.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/log2.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/suspend.h>
  27. #include <linux/fault-inject.h>
  28. #include <linux/random.h>
  29. #include <linux/mmc/card.h>
  30. #include <linux/mmc/host.h>
  31. #include <linux/mmc/mmc.h>
  32. #include <linux/mmc/sd.h>
  33. #include "core.h"
  34. #include "bus.h"
  35. #include "host.h"
  36. #include "sdio_bus.h"
  37. #include "mmc_ops.h"
  38. #include "sd_ops.h"
  39. #include "sdio_ops.h"
  40. static struct workqueue_struct *workqueue;
  41. /*
  42. * Enabling software CRCs on the data blocks can be a significant (30%)
  43. * performance cost, and for other reasons may not always be desired.
  44. * So we allow it it to be disabled.
  45. */
  46. bool use_spi_crc = 1;
  47. module_param(use_spi_crc, bool, 0);
  48. /*
  49. * We normally treat cards as removed during suspend if they are not
  50. * known to be on a non-removable bus, to avoid the risk of writing
  51. * back data to a different card after resume. Allow this to be
  52. * overridden if necessary.
  53. */
  54. #ifdef CONFIG_MMC_UNSAFE_RESUME
  55. bool mmc_assume_removable;
  56. #else
  57. bool mmc_assume_removable = 1;
  58. #endif
  59. EXPORT_SYMBOL(mmc_assume_removable);
  60. module_param_named(removable, mmc_assume_removable, bool, 0644);
  61. MODULE_PARM_DESC(
  62. removable,
  63. "MMC/SD cards are removable and may be removed during suspend");
  64. /*
  65. * Internal function. Schedule delayed work in the MMC work queue.
  66. */
  67. static int mmc_schedule_delayed_work(struct delayed_work *work,
  68. unsigned long delay)
  69. {
  70. return queue_delayed_work(workqueue, work, delay);
  71. }
  72. /*
  73. * Internal function. Flush all scheduled work from the MMC work queue.
  74. */
  75. static void mmc_flush_scheduled_work(void)
  76. {
  77. flush_workqueue(workqueue);
  78. }
  79. #ifdef CONFIG_FAIL_MMC_REQUEST
  80. /*
  81. * Internal function. Inject random data errors.
  82. * If mmc_data is NULL no errors are injected.
  83. */
  84. static void mmc_should_fail_request(struct mmc_host *host,
  85. struct mmc_request *mrq)
  86. {
  87. struct mmc_command *cmd = mrq->cmd;
  88. struct mmc_data *data = mrq->data;
  89. static const int data_errors[] = {
  90. -ETIMEDOUT,
  91. -EILSEQ,
  92. -EIO,
  93. };
  94. if (!data)
  95. return;
  96. if (cmd->error || data->error ||
  97. !should_fail(&host->fail_mmc_request, data->blksz * data->blocks))
  98. return;
  99. data->error = data_errors[random32() % ARRAY_SIZE(data_errors)];
  100. data->bytes_xfered = (random32() % (data->bytes_xfered >> 9)) << 9;
  101. }
  102. #else /* CONFIG_FAIL_MMC_REQUEST */
  103. static inline void mmc_should_fail_request(struct mmc_host *host,
  104. struct mmc_request *mrq)
  105. {
  106. }
  107. #endif /* CONFIG_FAIL_MMC_REQUEST */
  108. /**
  109. * mmc_request_done - finish processing an MMC request
  110. * @host: MMC host which completed request
  111. * @mrq: MMC request which request
  112. *
  113. * MMC drivers should call this function when they have completed
  114. * their processing of a request.
  115. */
  116. void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
  117. {
  118. struct mmc_command *cmd = mrq->cmd;
  119. int err = cmd->error;
  120. if (err && cmd->retries && mmc_host_is_spi(host)) {
  121. if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
  122. cmd->retries = 0;
  123. }
  124. if (err && cmd->retries && !mmc_card_removed(host->card)) {
  125. /*
  126. * Request starter must handle retries - see
  127. * mmc_wait_for_req_done().
  128. */
  129. if (mrq->done)
  130. mrq->done(mrq);
  131. } else {
  132. mmc_should_fail_request(host, mrq);
  133. led_trigger_event(host->led, LED_OFF);
  134. pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
  135. mmc_hostname(host), cmd->opcode, err,
  136. cmd->resp[0], cmd->resp[1],
  137. cmd->resp[2], cmd->resp[3]);
  138. if (mrq->data) {
  139. pr_debug("%s: %d bytes transferred: %d\n",
  140. mmc_hostname(host),
  141. mrq->data->bytes_xfered, mrq->data->error);
  142. }
  143. if (mrq->stop) {
  144. pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
  145. mmc_hostname(host), mrq->stop->opcode,
  146. mrq->stop->error,
  147. mrq->stop->resp[0], mrq->stop->resp[1],
  148. mrq->stop->resp[2], mrq->stop->resp[3]);
  149. }
  150. if (mrq->done)
  151. mrq->done(mrq);
  152. mmc_host_clk_release(host);
  153. }
  154. }
  155. EXPORT_SYMBOL(mmc_request_done);
  156. static void
  157. mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
  158. {
  159. #ifdef CONFIG_MMC_DEBUG
  160. unsigned int i, sz;
  161. struct scatterlist *sg;
  162. #endif
  163. if (mrq->sbc) {
  164. pr_debug("<%s: starting CMD%u arg %08x flags %08x>\n",
  165. mmc_hostname(host), mrq->sbc->opcode,
  166. mrq->sbc->arg, mrq->sbc->flags);
  167. }
  168. pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
  169. mmc_hostname(host), mrq->cmd->opcode,
  170. mrq->cmd->arg, mrq->cmd->flags);
  171. if (mrq->data) {
  172. pr_debug("%s: blksz %d blocks %d flags %08x "
  173. "tsac %d ms nsac %d\n",
  174. mmc_hostname(host), mrq->data->blksz,
  175. mrq->data->blocks, mrq->data->flags,
  176. mrq->data->timeout_ns / 1000000,
  177. mrq->data->timeout_clks);
  178. }
  179. if (mrq->stop) {
  180. pr_debug("%s: CMD%u arg %08x flags %08x\n",
  181. mmc_hostname(host), mrq->stop->opcode,
  182. mrq->stop->arg, mrq->stop->flags);
  183. }
  184. WARN_ON(!host->claimed);
  185. mrq->cmd->error = 0;
  186. mrq->cmd->mrq = mrq;
  187. if (mrq->data) {
  188. BUG_ON(mrq->data->blksz > host->max_blk_size);
  189. BUG_ON(mrq->data->blocks > host->max_blk_count);
  190. BUG_ON(mrq->data->blocks * mrq->data->blksz >
  191. host->max_req_size);
  192. #ifdef CONFIG_MMC_DEBUG
  193. sz = 0;
  194. for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
  195. sz += sg->length;
  196. BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
  197. #endif
  198. mrq->cmd->data = mrq->data;
  199. mrq->data->error = 0;
  200. mrq->data->mrq = mrq;
  201. if (mrq->stop) {
  202. mrq->data->stop = mrq->stop;
  203. mrq->stop->error = 0;
  204. mrq->stop->mrq = mrq;
  205. }
  206. }
  207. mmc_host_clk_hold(host);
  208. led_trigger_event(host->led, LED_FULL);
  209. host->ops->request(host, mrq);
  210. }
  211. static void mmc_wait_done(struct mmc_request *mrq)
  212. {
  213. complete(&mrq->completion);
  214. }
  215. static int __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
  216. {
  217. init_completion(&mrq->completion);
  218. mrq->done = mmc_wait_done;
  219. if (mmc_card_removed(host->card)) {
  220. mrq->cmd->error = -ENOMEDIUM;
  221. complete(&mrq->completion);
  222. return -ENOMEDIUM;
  223. }
  224. mmc_start_request(host, mrq);
  225. return 0;
  226. }
  227. static void mmc_wait_for_req_done(struct mmc_host *host,
  228. struct mmc_request *mrq)
  229. {
  230. struct mmc_command *cmd;
  231. while (1) {
  232. wait_for_completion(&mrq->completion);
  233. cmd = mrq->cmd;
  234. if (!cmd->error || !cmd->retries ||
  235. mmc_card_removed(host->card))
  236. break;
  237. pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
  238. mmc_hostname(host), cmd->opcode, cmd->error);
  239. cmd->retries--;
  240. cmd->error = 0;
  241. host->ops->request(host, mrq);
  242. }
  243. }
  244. /**
  245. * mmc_pre_req - Prepare for a new request
  246. * @host: MMC host to prepare command
  247. * @mrq: MMC request to prepare for
  248. * @is_first_req: true if there is no previous started request
  249. * that may run in parellel to this call, otherwise false
  250. *
  251. * mmc_pre_req() is called in prior to mmc_start_req() to let
  252. * host prepare for the new request. Preparation of a request may be
  253. * performed while another request is running on the host.
  254. */
  255. static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq,
  256. bool is_first_req)
  257. {
  258. if (host->ops->pre_req) {
  259. mmc_host_clk_hold(host);
  260. host->ops->pre_req(host, mrq, is_first_req);
  261. mmc_host_clk_release(host);
  262. }
  263. }
  264. /**
  265. * mmc_post_req - Post process a completed request
  266. * @host: MMC host to post process command
  267. * @mrq: MMC request to post process for
  268. * @err: Error, if non zero, clean up any resources made in pre_req
  269. *
  270. * Let the host post process a completed request. Post processing of
  271. * a request may be performed while another reuqest is running.
  272. */
  273. static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
  274. int err)
  275. {
  276. if (host->ops->post_req) {
  277. mmc_host_clk_hold(host);
  278. host->ops->post_req(host, mrq, err);
  279. mmc_host_clk_release(host);
  280. }
  281. }
  282. /**
  283. * mmc_start_req - start a non-blocking request
  284. * @host: MMC host to start command
  285. * @areq: async request to start
  286. * @error: out parameter returns 0 for success, otherwise non zero
  287. *
  288. * Start a new MMC custom command request for a host.
  289. * If there is on ongoing async request wait for completion
  290. * of that request and start the new one and return.
  291. * Does not wait for the new request to complete.
  292. *
  293. * Returns the completed request, NULL in case of none completed.
  294. * Wait for the an ongoing request (previoulsy started) to complete and
  295. * return the completed request. If there is no ongoing request, NULL
  296. * is returned without waiting. NULL is not an error condition.
  297. */
  298. struct mmc_async_req *mmc_start_req(struct mmc_host *host,
  299. struct mmc_async_req *areq, int *error)
  300. {
  301. int err = 0;
  302. int start_err = 0;
  303. struct mmc_async_req *data = host->areq;
  304. /* Prepare a new request */
  305. if (areq)
  306. mmc_pre_req(host, areq->mrq, !host->areq);
  307. if (host->areq) {
  308. mmc_wait_for_req_done(host, host->areq->mrq);
  309. err = host->areq->err_check(host->card, host->areq);
  310. }
  311. if (!err && areq)
  312. start_err = __mmc_start_req(host, areq->mrq);
  313. if (host->areq)
  314. mmc_post_req(host, host->areq->mrq, 0);
  315. /* Cancel a prepared request if it was not started. */
  316. if ((err || start_err) && areq)
  317. mmc_post_req(host, areq->mrq, -EINVAL);
  318. if (err)
  319. host->areq = NULL;
  320. else
  321. host->areq = areq;
  322. if (error)
  323. *error = err;
  324. return data;
  325. }
  326. EXPORT_SYMBOL(mmc_start_req);
  327. /**
  328. * mmc_wait_for_req - start a request and wait for completion
  329. * @host: MMC host to start command
  330. * @mrq: MMC request to start
  331. *
  332. * Start a new MMC custom command request for a host, and wait
  333. * for the command to complete. Does not attempt to parse the
  334. * response.
  335. */
  336. void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
  337. {
  338. __mmc_start_req(host, mrq);
  339. mmc_wait_for_req_done(host, mrq);
  340. }
  341. EXPORT_SYMBOL(mmc_wait_for_req);
  342. /**
  343. * mmc_interrupt_hpi - Issue for High priority Interrupt
  344. * @card: the MMC card associated with the HPI transfer
  345. *
  346. * Issued High Priority Interrupt, and check for card status
  347. * util out-of prg-state.
  348. */
  349. int mmc_interrupt_hpi(struct mmc_card *card)
  350. {
  351. int err;
  352. u32 status;
  353. BUG_ON(!card);
  354. if (!card->ext_csd.hpi_en) {
  355. pr_info("%s: HPI enable bit unset\n", mmc_hostname(card->host));
  356. return 1;
  357. }
  358. mmc_claim_host(card->host);
  359. err = mmc_send_status(card, &status);
  360. if (err) {
  361. pr_err("%s: Get card status fail\n", mmc_hostname(card->host));
  362. goto out;
  363. }
  364. /*
  365. * If the card status is in PRG-state, we can send the HPI command.
  366. */
  367. if (R1_CURRENT_STATE(status) == R1_STATE_PRG) {
  368. do {
  369. /*
  370. * We don't know when the HPI command will finish
  371. * processing, so we need to resend HPI until out
  372. * of prg-state, and keep checking the card status
  373. * with SEND_STATUS. If a timeout error occurs when
  374. * sending the HPI command, we are already out of
  375. * prg-state.
  376. */
  377. err = mmc_send_hpi_cmd(card, &status);
  378. if (err)
  379. pr_debug("%s: abort HPI (%d error)\n",
  380. mmc_hostname(card->host), err);
  381. err = mmc_send_status(card, &status);
  382. if (err)
  383. break;
  384. } while (R1_CURRENT_STATE(status) == R1_STATE_PRG);
  385. } else
  386. pr_debug("%s: Left prg-state\n", mmc_hostname(card->host));
  387. out:
  388. mmc_release_host(card->host);
  389. return err;
  390. }
  391. EXPORT_SYMBOL(mmc_interrupt_hpi);
  392. /**
  393. * mmc_wait_for_cmd - start a command and wait for completion
  394. * @host: MMC host to start command
  395. * @cmd: MMC command to start
  396. * @retries: maximum number of retries
  397. *
  398. * Start a new MMC command for a host, and wait for the command
  399. * to complete. Return any error that occurred while the command
  400. * was executing. Do not attempt to parse the response.
  401. */
  402. int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
  403. {
  404. struct mmc_request mrq = {NULL};
  405. WARN_ON(!host->claimed);
  406. memset(cmd->resp, 0, sizeof(cmd->resp));
  407. cmd->retries = retries;
  408. mrq.cmd = cmd;
  409. cmd->data = NULL;
  410. mmc_wait_for_req(host, &mrq);
  411. return cmd->error;
  412. }
  413. EXPORT_SYMBOL(mmc_wait_for_cmd);
  414. /**
  415. * mmc_set_data_timeout - set the timeout for a data command
  416. * @data: data phase for command
  417. * @card: the MMC card associated with the data transfer
  418. *
  419. * Computes the data timeout parameters according to the
  420. * correct algorithm given the card type.
  421. */
  422. void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
  423. {
  424. unsigned int mult;
  425. /*
  426. * SDIO cards only define an upper 1 s limit on access.
  427. */
  428. if (mmc_card_sdio(card)) {
  429. data->timeout_ns = 1000000000;
  430. data->timeout_clks = 0;
  431. return;
  432. }
  433. /*
  434. * SD cards use a 100 multiplier rather than 10
  435. */
  436. mult = mmc_card_sd(card) ? 100 : 10;
  437. /*
  438. * Scale up the multiplier (and therefore the timeout) by
  439. * the r2w factor for writes.
  440. */
  441. if (data->flags & MMC_DATA_WRITE)
  442. mult <<= card->csd.r2w_factor;
  443. data->timeout_ns = card->csd.tacc_ns * mult;
  444. data->timeout_clks = card->csd.tacc_clks * mult;
  445. /*
  446. * SD cards also have an upper limit on the timeout.
  447. */
  448. if (mmc_card_sd(card)) {
  449. unsigned int timeout_us, limit_us;
  450. timeout_us = data->timeout_ns / 1000;
  451. if (mmc_host_clk_rate(card->host))
  452. timeout_us += data->timeout_clks * 1000 /
  453. (mmc_host_clk_rate(card->host) / 1000);
  454. if (data->flags & MMC_DATA_WRITE)
  455. /*
  456. * The limit is really 250 ms, but that is
  457. * insufficient for some crappy cards.
  458. */
  459. limit_us = 300000;
  460. else
  461. limit_us = 100000;
  462. /*
  463. * SDHC cards always use these fixed values.
  464. */
  465. if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
  466. data->timeout_ns = limit_us * 1000;
  467. data->timeout_clks = 0;
  468. }
  469. }
  470. /*
  471. * Some cards require longer data read timeout than indicated in CSD.
  472. * Address this by setting the read timeout to a "reasonably high"
  473. * value. For the cards tested, 300ms has proven enough. If necessary,
  474. * this value can be increased if other problematic cards require this.
  475. */
  476. if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) {
  477. data->timeout_ns = 300000000;
  478. data->timeout_clks = 0;
  479. }
  480. /*
  481. * Some cards need very high timeouts if driven in SPI mode.
  482. * The worst observed timeout was 900ms after writing a
  483. * continuous stream of data until the internal logic
  484. * overflowed.
  485. */
  486. if (mmc_host_is_spi(card->host)) {
  487. if (data->flags & MMC_DATA_WRITE) {
  488. if (data->timeout_ns < 1000000000)
  489. data->timeout_ns = 1000000000; /* 1s */
  490. } else {
  491. if (data->timeout_ns < 100000000)
  492. data->timeout_ns = 100000000; /* 100ms */
  493. }
  494. }
  495. }
  496. EXPORT_SYMBOL(mmc_set_data_timeout);
  497. /**
  498. * mmc_align_data_size - pads a transfer size to a more optimal value
  499. * @card: the MMC card associated with the data transfer
  500. * @sz: original transfer size
  501. *
  502. * Pads the original data size with a number of extra bytes in
  503. * order to avoid controller bugs and/or performance hits
  504. * (e.g. some controllers revert to PIO for certain sizes).
  505. *
  506. * Returns the improved size, which might be unmodified.
  507. *
  508. * Note that this function is only relevant when issuing a
  509. * single scatter gather entry.
  510. */
  511. unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
  512. {
  513. /*
  514. * FIXME: We don't have a system for the controller to tell
  515. * the core about its problems yet, so for now we just 32-bit
  516. * align the size.
  517. */
  518. sz = ((sz + 3) / 4) * 4;
  519. return sz;
  520. }
  521. EXPORT_SYMBOL(mmc_align_data_size);
  522. /**
  523. * __mmc_claim_host - exclusively claim a host
  524. * @host: mmc host to claim
  525. * @abort: whether or not the operation should be aborted
  526. *
  527. * Claim a host for a set of operations. If @abort is non null and
  528. * dereference a non-zero value then this will return prematurely with
  529. * that non-zero value without acquiring the lock. Returns zero
  530. * with the lock held otherwise.
  531. */
  532. int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
  533. {
  534. DECLARE_WAITQUEUE(wait, current);
  535. unsigned long flags;
  536. int stop;
  537. might_sleep();
  538. add_wait_queue(&host->wq, &wait);
  539. spin_lock_irqsave(&host->lock, flags);
  540. while (1) {
  541. set_current_state(TASK_UNINTERRUPTIBLE);
  542. stop = abort ? atomic_read(abort) : 0;
  543. if (stop || !host->claimed || host->claimer == current)
  544. break;
  545. spin_unlock_irqrestore(&host->lock, flags);
  546. schedule();
  547. spin_lock_irqsave(&host->lock, flags);
  548. }
  549. set_current_state(TASK_RUNNING);
  550. if (!stop) {
  551. host->claimed = 1;
  552. host->claimer = current;
  553. host->claim_cnt += 1;
  554. } else
  555. wake_up(&host->wq);
  556. spin_unlock_irqrestore(&host->lock, flags);
  557. remove_wait_queue(&host->wq, &wait);
  558. if (host->ops->enable && !stop && host->claim_cnt == 1)
  559. host->ops->enable(host);
  560. return stop;
  561. }
  562. EXPORT_SYMBOL(__mmc_claim_host);
  563. /**
  564. * mmc_try_claim_host - try exclusively to claim a host
  565. * @host: mmc host to claim
  566. *
  567. * Returns %1 if the host is claimed, %0 otherwise.
  568. */
  569. int mmc_try_claim_host(struct mmc_host *host)
  570. {
  571. int claimed_host = 0;
  572. unsigned long flags;
  573. spin_lock_irqsave(&host->lock, flags);
  574. if (!host->claimed || host->claimer == current) {
  575. host->claimed = 1;
  576. host->claimer = current;
  577. host->claim_cnt += 1;
  578. claimed_host = 1;
  579. }
  580. spin_unlock_irqrestore(&host->lock, flags);
  581. if (host->ops->enable && claimed_host && host->claim_cnt == 1)
  582. host->ops->enable(host);
  583. return claimed_host;
  584. }
  585. EXPORT_SYMBOL(mmc_try_claim_host);
  586. /**
  587. * mmc_release_host - release a host
  588. * @host: mmc host to release
  589. *
  590. * Release a MMC host, allowing others to claim the host
  591. * for their operations.
  592. */
  593. void mmc_release_host(struct mmc_host *host)
  594. {
  595. unsigned long flags;
  596. WARN_ON(!host->claimed);
  597. if (host->ops->disable && host->claim_cnt == 1)
  598. host->ops->disable(host);
  599. spin_lock_irqsave(&host->lock, flags);
  600. if (--host->claim_cnt) {
  601. /* Release for nested claim */
  602. spin_unlock_irqrestore(&host->lock, flags);
  603. } else {
  604. host->claimed = 0;
  605. host->claimer = NULL;
  606. spin_unlock_irqrestore(&host->lock, flags);
  607. wake_up(&host->wq);
  608. }
  609. }
  610. EXPORT_SYMBOL(mmc_release_host);
  611. /*
  612. * Internal function that does the actual ios call to the host driver,
  613. * optionally printing some debug output.
  614. */
  615. static inline void mmc_set_ios(struct mmc_host *host)
  616. {
  617. struct mmc_ios *ios = &host->ios;
  618. pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
  619. "width %u timing %u\n",
  620. mmc_hostname(host), ios->clock, ios->bus_mode,
  621. ios->power_mode, ios->chip_select, ios->vdd,
  622. ios->bus_width, ios->timing);
  623. if (ios->clock > 0)
  624. mmc_set_ungated(host);
  625. host->ops->set_ios(host, ios);
  626. }
  627. /*
  628. * Control chip select pin on a host.
  629. */
  630. void mmc_set_chip_select(struct mmc_host *host, int mode)
  631. {
  632. mmc_host_clk_hold(host);
  633. host->ios.chip_select = mode;
  634. mmc_set_ios(host);
  635. mmc_host_clk_release(host);
  636. }
  637. /*
  638. * Sets the host clock to the highest possible frequency that
  639. * is below "hz".
  640. */
  641. static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
  642. {
  643. WARN_ON(hz < host->f_min);
  644. if (hz > host->f_max)
  645. hz = host->f_max;
  646. host->ios.clock = hz;
  647. mmc_set_ios(host);
  648. }
  649. void mmc_set_clock(struct mmc_host *host, unsigned int hz)
  650. {
  651. mmc_host_clk_hold(host);
  652. __mmc_set_clock(host, hz);
  653. mmc_host_clk_release(host);
  654. }
  655. #ifdef CONFIG_MMC_CLKGATE
  656. /*
  657. * This gates the clock by setting it to 0 Hz.
  658. */
  659. void mmc_gate_clock(struct mmc_host *host)
  660. {
  661. unsigned long flags;
  662. spin_lock_irqsave(&host->clk_lock, flags);
  663. host->clk_old = host->ios.clock;
  664. host->ios.clock = 0;
  665. host->clk_gated = true;
  666. spin_unlock_irqrestore(&host->clk_lock, flags);
  667. mmc_set_ios(host);
  668. }
  669. /*
  670. * This restores the clock from gating by using the cached
  671. * clock value.
  672. */
  673. void mmc_ungate_clock(struct mmc_host *host)
  674. {
  675. /*
  676. * We should previously have gated the clock, so the clock shall
  677. * be 0 here! The clock may however be 0 during initialization,
  678. * when some request operations are performed before setting
  679. * the frequency. When ungate is requested in that situation
  680. * we just ignore the call.
  681. */
  682. if (host->clk_old) {
  683. BUG_ON(host->ios.clock);
  684. /* This call will also set host->clk_gated to false */
  685. __mmc_set_clock(host, host->clk_old);
  686. }
  687. }
  688. void mmc_set_ungated(struct mmc_host *host)
  689. {
  690. unsigned long flags;
  691. /*
  692. * We've been given a new frequency while the clock is gated,
  693. * so make sure we regard this as ungating it.
  694. */
  695. spin_lock_irqsave(&host->clk_lock, flags);
  696. host->clk_gated = false;
  697. spin_unlock_irqrestore(&host->clk_lock, flags);
  698. }
  699. #else
  700. void mmc_set_ungated(struct mmc_host *host)
  701. {
  702. }
  703. #endif
  704. /*
  705. * Change the bus mode (open drain/push-pull) of a host.
  706. */
  707. void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
  708. {
  709. mmc_host_clk_hold(host);
  710. host->ios.bus_mode = mode;
  711. mmc_set_ios(host);
  712. mmc_host_clk_release(host);
  713. }
  714. /*
  715. * Change data bus width of a host.
  716. */
  717. void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
  718. {
  719. mmc_host_clk_hold(host);
  720. host->ios.bus_width = width;
  721. mmc_set_ios(host);
  722. mmc_host_clk_release(host);
  723. }
  724. /**
  725. * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
  726. * @vdd: voltage (mV)
  727. * @low_bits: prefer low bits in boundary cases
  728. *
  729. * This function returns the OCR bit number according to the provided @vdd
  730. * value. If conversion is not possible a negative errno value returned.
  731. *
  732. * Depending on the @low_bits flag the function prefers low or high OCR bits
  733. * on boundary voltages. For example,
  734. * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
  735. * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
  736. *
  737. * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
  738. */
  739. static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
  740. {
  741. const int max_bit = ilog2(MMC_VDD_35_36);
  742. int bit;
  743. if (vdd < 1650 || vdd > 3600)
  744. return -EINVAL;
  745. if (vdd >= 1650 && vdd <= 1950)
  746. return ilog2(MMC_VDD_165_195);
  747. if (low_bits)
  748. vdd -= 1;
  749. /* Base 2000 mV, step 100 mV, bit's base 8. */
  750. bit = (vdd - 2000) / 100 + 8;
  751. if (bit > max_bit)
  752. return max_bit;
  753. return bit;
  754. }
  755. /**
  756. * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
  757. * @vdd_min: minimum voltage value (mV)
  758. * @vdd_max: maximum voltage value (mV)
  759. *
  760. * This function returns the OCR mask bits according to the provided @vdd_min
  761. * and @vdd_max values. If conversion is not possible the function returns 0.
  762. *
  763. * Notes wrt boundary cases:
  764. * This function sets the OCR bits for all boundary voltages, for example
  765. * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
  766. * MMC_VDD_34_35 mask.
  767. */
  768. u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
  769. {
  770. u32 mask = 0;
  771. if (vdd_max < vdd_min)
  772. return 0;
  773. /* Prefer high bits for the boundary vdd_max values. */
  774. vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
  775. if (vdd_max < 0)
  776. return 0;
  777. /* Prefer low bits for the boundary vdd_min values. */
  778. vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
  779. if (vdd_min < 0)
  780. return 0;
  781. /* Fill the mask, from max bit to min bit. */
  782. while (vdd_max >= vdd_min)
  783. mask |= 1 << vdd_max--;
  784. return mask;
  785. }
  786. EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
  787. #ifdef CONFIG_REGULATOR
  788. /**
  789. * mmc_regulator_get_ocrmask - return mask of supported voltages
  790. * @supply: regulator to use
  791. *
  792. * This returns either a negative errno, or a mask of voltages that
  793. * can be provided to MMC/SD/SDIO devices using the specified voltage
  794. * regulator. This would normally be called before registering the
  795. * MMC host adapter.
  796. */
  797. int mmc_regulator_get_ocrmask(struct regulator *supply)
  798. {
  799. int result = 0;
  800. int count;
  801. int i;
  802. count = regulator_count_voltages(supply);
  803. if (count < 0)
  804. return count;
  805. for (i = 0; i < count; i++) {
  806. int vdd_uV;
  807. int vdd_mV;
  808. vdd_uV = regulator_list_voltage(supply, i);
  809. if (vdd_uV <= 0)
  810. continue;
  811. vdd_mV = vdd_uV / 1000;
  812. result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
  813. }
  814. return result;
  815. }
  816. EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
  817. /**
  818. * mmc_regulator_set_ocr - set regulator to match host->ios voltage
  819. * @mmc: the host to regulate
  820. * @supply: regulator to use
  821. * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
  822. *
  823. * Returns zero on success, else negative errno.
  824. *
  825. * MMC host drivers may use this to enable or disable a regulator using
  826. * a particular supply voltage. This would normally be called from the
  827. * set_ios() method.
  828. */
  829. int mmc_regulator_set_ocr(struct mmc_host *mmc,
  830. struct regulator *supply,
  831. unsigned short vdd_bit)
  832. {
  833. int result = 0;
  834. int min_uV, max_uV;
  835. if (vdd_bit) {
  836. int tmp;
  837. int voltage;
  838. /* REVISIT mmc_vddrange_to_ocrmask() may have set some
  839. * bits this regulator doesn't quite support ... don't
  840. * be too picky, most cards and regulators are OK with
  841. * a 0.1V range goof (it's a small error percentage).
  842. */
  843. tmp = vdd_bit - ilog2(MMC_VDD_165_195);
  844. if (tmp == 0) {
  845. min_uV = 1650 * 1000;
  846. max_uV = 1950 * 1000;
  847. } else {
  848. min_uV = 1900 * 1000 + tmp * 100 * 1000;
  849. max_uV = min_uV + 100 * 1000;
  850. }
  851. /* avoid needless changes to this voltage; the regulator
  852. * might not allow this operation
  853. */
  854. voltage = regulator_get_voltage(supply);
  855. if (mmc->caps2 & MMC_CAP2_BROKEN_VOLTAGE)
  856. min_uV = max_uV = voltage;
  857. if (voltage < 0)
  858. result = voltage;
  859. else if (voltage < min_uV || voltage > max_uV)
  860. result = regulator_set_voltage(supply, min_uV, max_uV);
  861. else
  862. result = 0;
  863. if (result == 0 && !mmc->regulator_enabled) {
  864. result = regulator_enable(supply);
  865. if (!result)
  866. mmc->regulator_enabled = true;
  867. }
  868. } else if (mmc->regulator_enabled) {
  869. result = regulator_disable(supply);
  870. if (result == 0)
  871. mmc->regulator_enabled = false;
  872. }
  873. if (result)
  874. dev_err(mmc_dev(mmc),
  875. "could not set regulator OCR (%d)\n", result);
  876. return result;
  877. }
  878. EXPORT_SYMBOL(mmc_regulator_set_ocr);
  879. #endif /* CONFIG_REGULATOR */
  880. /*
  881. * Mask off any voltages we don't support and select
  882. * the lowest voltage
  883. */
  884. u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
  885. {
  886. int bit;
  887. ocr &= host->ocr_avail;
  888. bit = ffs(ocr);
  889. if (bit) {
  890. bit -= 1;
  891. ocr &= 3 << bit;
  892. mmc_host_clk_hold(host);
  893. host->ios.vdd = bit;
  894. mmc_set_ios(host);
  895. mmc_host_clk_release(host);
  896. } else {
  897. pr_warning("%s: host doesn't support card's voltages\n",
  898. mmc_hostname(host));
  899. ocr = 0;
  900. }
  901. return ocr;
  902. }
  903. int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11)
  904. {
  905. struct mmc_command cmd = {0};
  906. int err = 0;
  907. BUG_ON(!host);
  908. /*
  909. * Send CMD11 only if the request is to switch the card to
  910. * 1.8V signalling.
  911. */
  912. if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) {
  913. cmd.opcode = SD_SWITCH_VOLTAGE;
  914. cmd.arg = 0;
  915. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  916. err = mmc_wait_for_cmd(host, &cmd, 0);
  917. if (err)
  918. return err;
  919. if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
  920. return -EIO;
  921. }
  922. host->ios.signal_voltage = signal_voltage;
  923. if (host->ops->start_signal_voltage_switch) {
  924. mmc_host_clk_hold(host);
  925. err = host->ops->start_signal_voltage_switch(host, &host->ios);
  926. mmc_host_clk_release(host);
  927. }
  928. return err;
  929. }
  930. /*
  931. * Select timing parameters for host.
  932. */
  933. void mmc_set_timing(struct mmc_host *host, unsigned int timing)
  934. {
  935. mmc_host_clk_hold(host);
  936. host->ios.timing = timing;
  937. mmc_set_ios(host);
  938. mmc_host_clk_release(host);
  939. }
  940. /*
  941. * Select appropriate driver type for host.
  942. */
  943. void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
  944. {
  945. mmc_host_clk_hold(host);
  946. host->ios.drv_type = drv_type;
  947. mmc_set_ios(host);
  948. mmc_host_clk_release(host);
  949. }
  950. static void mmc_poweroff_notify(struct mmc_host *host)
  951. {
  952. struct mmc_card *card;
  953. unsigned int timeout;
  954. unsigned int notify_type = EXT_CSD_NO_POWER_NOTIFICATION;
  955. int err = 0;
  956. card = host->card;
  957. mmc_claim_host(host);
  958. /*
  959. * Send power notify command only if card
  960. * is mmc and notify state is powered ON
  961. */
  962. if (card && mmc_card_mmc(card) &&
  963. (card->poweroff_notify_state == MMC_POWERED_ON)) {
  964. if (host->power_notify_type == MMC_HOST_PW_NOTIFY_SHORT) {
  965. notify_type = EXT_CSD_POWER_OFF_SHORT;
  966. timeout = card->ext_csd.generic_cmd6_time;
  967. card->poweroff_notify_state = MMC_POWEROFF_SHORT;
  968. } else {
  969. notify_type = EXT_CSD_POWER_OFF_LONG;
  970. timeout = card->ext_csd.power_off_longtime;
  971. card->poweroff_notify_state = MMC_POWEROFF_LONG;
  972. }
  973. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  974. EXT_CSD_POWER_OFF_NOTIFICATION,
  975. notify_type, timeout);
  976. if (err && err != -EBADMSG)
  977. pr_err("Device failed to respond within %d poweroff "
  978. "time. Forcefully powering down the device\n",
  979. timeout);
  980. /* Set the card state to no notification after the poweroff */
  981. card->poweroff_notify_state = MMC_NO_POWER_NOTIFICATION;
  982. }
  983. mmc_release_host(host);
  984. }
  985. /*
  986. * Apply power to the MMC stack. This is a two-stage process.
  987. * First, we enable power to the card without the clock running.
  988. * We then wait a bit for the power to stabilise. Finally,
  989. * enable the bus drivers and clock to the card.
  990. *
  991. * We must _NOT_ enable the clock prior to power stablising.
  992. *
  993. * If a host does all the power sequencing itself, ignore the
  994. * initial MMC_POWER_UP stage.
  995. */
  996. static void mmc_power_up(struct mmc_host *host)
  997. {
  998. int bit;
  999. mmc_host_clk_hold(host);
  1000. /* If ocr is set, we use it */
  1001. if (host->ocr)
  1002. bit = ffs(host->ocr) - 1;
  1003. else
  1004. bit = fls(host->ocr_avail) - 1;
  1005. host->ios.vdd = bit;
  1006. if (mmc_host_is_spi(host))
  1007. host->ios.chip_select = MMC_CS_HIGH;
  1008. else
  1009. host->ios.chip_select = MMC_CS_DONTCARE;
  1010. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  1011. host->ios.power_mode = MMC_POWER_UP;
  1012. host->ios.bus_width = MMC_BUS_WIDTH_1;
  1013. host->ios.timing = MMC_TIMING_LEGACY;
  1014. mmc_set_ios(host);
  1015. /*
  1016. * This delay should be sufficient to allow the power supply
  1017. * to reach the minimum voltage.
  1018. */
  1019. mmc_delay(10);
  1020. host->ios.clock = host->f_init;
  1021. host->ios.power_mode = MMC_POWER_ON;
  1022. mmc_set_ios(host);
  1023. /*
  1024. * This delay must be at least 74 clock sizes, or 1 ms, or the
  1025. * time required to reach a stable voltage.
  1026. */
  1027. mmc_delay(10);
  1028. mmc_host_clk_release(host);
  1029. }
  1030. void mmc_power_off(struct mmc_host *host)
  1031. {
  1032. int err = 0;
  1033. mmc_host_clk_hold(host);
  1034. host->ios.clock = 0;
  1035. host->ios.vdd = 0;
  1036. /*
  1037. * For eMMC 4.5 device send AWAKE command before
  1038. * POWER_OFF_NOTIFY command, because in sleep state
  1039. * eMMC 4.5 devices respond to only RESET and AWAKE cmd
  1040. */
  1041. if (host->card && mmc_card_is_sleep(host->card) &&
  1042. host->bus_ops->resume) {
  1043. err = host->bus_ops->resume(host);
  1044. if (!err)
  1045. mmc_poweroff_notify(host);
  1046. else
  1047. pr_warning("%s: error %d during resume "
  1048. "(continue with poweroff sequence)\n",
  1049. mmc_hostname(host), err);
  1050. }
  1051. /*
  1052. * Reset ocr mask to be the highest possible voltage supported for
  1053. * this mmc host. This value will be used at next power up.
  1054. */
  1055. host->ocr = 1 << (fls(host->ocr_avail) - 1);
  1056. if (!mmc_host_is_spi(host)) {
  1057. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  1058. host->ios.chip_select = MMC_CS_DONTCARE;
  1059. }
  1060. host->ios.power_mode = MMC_POWER_OFF;
  1061. host->ios.bus_width = MMC_BUS_WIDTH_1;
  1062. host->ios.timing = MMC_TIMING_LEGACY;
  1063. mmc_set_ios(host);
  1064. /*
  1065. * Some configurations, such as the 802.11 SDIO card in the OLPC
  1066. * XO-1.5, require a short delay after poweroff before the card
  1067. * can be successfully turned on again.
  1068. */
  1069. mmc_delay(1);
  1070. mmc_host_clk_release(host);
  1071. }
  1072. /*
  1073. * Cleanup when the last reference to the bus operator is dropped.
  1074. */
  1075. static void __mmc_release_bus(struct mmc_host *host)
  1076. {
  1077. BUG_ON(!host);
  1078. BUG_ON(host->bus_refs);
  1079. BUG_ON(!host->bus_dead);
  1080. host->bus_ops = NULL;
  1081. }
  1082. /*
  1083. * Increase reference count of bus operator
  1084. */
  1085. static inline void mmc_bus_get(struct mmc_host *host)
  1086. {
  1087. unsigned long flags;
  1088. spin_lock_irqsave(&host->lock, flags);
  1089. host->bus_refs++;
  1090. spin_unlock_irqrestore(&host->lock, flags);
  1091. }
  1092. /*
  1093. * Decrease reference count of bus operator and free it if
  1094. * it is the last reference.
  1095. */
  1096. static inline void mmc_bus_put(struct mmc_host *host)
  1097. {
  1098. unsigned long flags;
  1099. spin_lock_irqsave(&host->lock, flags);
  1100. host->bus_refs--;
  1101. if ((host->bus_refs == 0) && host->bus_ops)
  1102. __mmc_release_bus(host);
  1103. spin_unlock_irqrestore(&host->lock, flags);
  1104. }
  1105. /*
  1106. * Assign a mmc bus handler to a host. Only one bus handler may control a
  1107. * host at any given time.
  1108. */
  1109. void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
  1110. {
  1111. unsigned long flags;
  1112. BUG_ON(!host);
  1113. BUG_ON(!ops);
  1114. WARN_ON(!host->claimed);
  1115. spin_lock_irqsave(&host->lock, flags);
  1116. BUG_ON(host->bus_ops);
  1117. BUG_ON(host->bus_refs);
  1118. host->bus_ops = ops;
  1119. host->bus_refs = 1;
  1120. host->bus_dead = 0;
  1121. spin_unlock_irqrestore(&host->lock, flags);
  1122. }
  1123. /*
  1124. * Remove the current bus handler from a host.
  1125. */
  1126. void mmc_detach_bus(struct mmc_host *host)
  1127. {
  1128. unsigned long flags;
  1129. BUG_ON(!host);
  1130. WARN_ON(!host->claimed);
  1131. WARN_ON(!host->bus_ops);
  1132. spin_lock_irqsave(&host->lock, flags);
  1133. host->bus_dead = 1;
  1134. spin_unlock_irqrestore(&host->lock, flags);
  1135. mmc_bus_put(host);
  1136. }
  1137. /**
  1138. * mmc_detect_change - process change of state on a MMC socket
  1139. * @host: host which changed state.
  1140. * @delay: optional delay to wait before detection (jiffies)
  1141. *
  1142. * MMC drivers should call this when they detect a card has been
  1143. * inserted or removed. The MMC layer will confirm that any
  1144. * present card is still functional, and initialize any newly
  1145. * inserted.
  1146. */
  1147. void mmc_detect_change(struct mmc_host *host, unsigned long delay)
  1148. {
  1149. #ifdef CONFIG_MMC_DEBUG
  1150. unsigned long flags;
  1151. spin_lock_irqsave(&host->lock, flags);
  1152. WARN_ON(host->removed);
  1153. spin_unlock_irqrestore(&host->lock, flags);
  1154. #endif
  1155. host->detect_change = 1;
  1156. mmc_schedule_delayed_work(&host->detect, delay);
  1157. }
  1158. EXPORT_SYMBOL(mmc_detect_change);
  1159. void mmc_init_erase(struct mmc_card *card)
  1160. {
  1161. unsigned int sz;
  1162. if (is_power_of_2(card->erase_size))
  1163. card->erase_shift = ffs(card->erase_size) - 1;
  1164. else
  1165. card->erase_shift = 0;
  1166. /*
  1167. * It is possible to erase an arbitrarily large area of an SD or MMC
  1168. * card. That is not desirable because it can take a long time
  1169. * (minutes) potentially delaying more important I/O, and also the
  1170. * timeout calculations become increasingly hugely over-estimated.
  1171. * Consequently, 'pref_erase' is defined as a guide to limit erases
  1172. * to that size and alignment.
  1173. *
  1174. * For SD cards that define Allocation Unit size, limit erases to one
  1175. * Allocation Unit at a time. For MMC cards that define High Capacity
  1176. * Erase Size, whether it is switched on or not, limit to that size.
  1177. * Otherwise just have a stab at a good value. For modern cards it
  1178. * will end up being 4MiB. Note that if the value is too small, it
  1179. * can end up taking longer to erase.
  1180. */
  1181. if (mmc_card_sd(card) && card->ssr.au) {
  1182. card->pref_erase = card->ssr.au;
  1183. card->erase_shift = ffs(card->ssr.au) - 1;
  1184. } else if (card->ext_csd.hc_erase_size) {
  1185. card->pref_erase = card->ext_csd.hc_erase_size;
  1186. } else {
  1187. sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
  1188. if (sz < 128)
  1189. card->pref_erase = 512 * 1024 / 512;
  1190. else if (sz < 512)
  1191. card->pref_erase = 1024 * 1024 / 512;
  1192. else if (sz < 1024)
  1193. card->pref_erase = 2 * 1024 * 1024 / 512;
  1194. else
  1195. card->pref_erase = 4 * 1024 * 1024 / 512;
  1196. if (card->pref_erase < card->erase_size)
  1197. card->pref_erase = card->erase_size;
  1198. else {
  1199. sz = card->pref_erase % card->erase_size;
  1200. if (sz)
  1201. card->pref_erase += card->erase_size - sz;
  1202. }
  1203. }
  1204. }
  1205. static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
  1206. unsigned int arg, unsigned int qty)
  1207. {
  1208. unsigned int erase_timeout;
  1209. if (card->ext_csd.erase_group_def & 1) {
  1210. /* High Capacity Erase Group Size uses HC timeouts */
  1211. if (arg == MMC_TRIM_ARG)
  1212. erase_timeout = card->ext_csd.trim_timeout;
  1213. else
  1214. erase_timeout = card->ext_csd.hc_erase_timeout;
  1215. } else {
  1216. /* CSD Erase Group Size uses write timeout */
  1217. unsigned int mult = (10 << card->csd.r2w_factor);
  1218. unsigned int timeout_clks = card->csd.tacc_clks * mult;
  1219. unsigned int timeout_us;
  1220. /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
  1221. if (card->csd.tacc_ns < 1000000)
  1222. timeout_us = (card->csd.tacc_ns * mult) / 1000;
  1223. else
  1224. timeout_us = (card->csd.tacc_ns / 1000) * mult;
  1225. /*
  1226. * ios.clock is only a target. The real clock rate might be
  1227. * less but not that much less, so fudge it by multiplying by 2.
  1228. */
  1229. timeout_clks <<= 1;
  1230. timeout_us += (timeout_clks * 1000) /
  1231. (mmc_host_clk_rate(card->host) / 1000);
  1232. erase_timeout = timeout_us / 1000;
  1233. /*
  1234. * Theoretically, the calculation could underflow so round up
  1235. * to 1ms in that case.
  1236. */
  1237. if (!erase_timeout)
  1238. erase_timeout = 1;
  1239. }
  1240. /* Multiplier for secure operations */
  1241. if (arg & MMC_SECURE_ARGS) {
  1242. if (arg == MMC_SECURE_ERASE_ARG)
  1243. erase_timeout *= card->ext_csd.sec_erase_mult;
  1244. else
  1245. erase_timeout *= card->ext_csd.sec_trim_mult;
  1246. }
  1247. erase_timeout *= qty;
  1248. /*
  1249. * Ensure at least a 1 second timeout for SPI as per
  1250. * 'mmc_set_data_timeout()'
  1251. */
  1252. if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
  1253. erase_timeout = 1000;
  1254. return erase_timeout;
  1255. }
  1256. static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
  1257. unsigned int arg,
  1258. unsigned int qty)
  1259. {
  1260. unsigned int erase_timeout;
  1261. if (card->ssr.erase_timeout) {
  1262. /* Erase timeout specified in SD Status Register (SSR) */
  1263. erase_timeout = card->ssr.erase_timeout * qty +
  1264. card->ssr.erase_offset;
  1265. } else {
  1266. /*
  1267. * Erase timeout not specified in SD Status Register (SSR) so
  1268. * use 250ms per write block.
  1269. */
  1270. erase_timeout = 250 * qty;
  1271. }
  1272. /* Must not be less than 1 second */
  1273. if (erase_timeout < 1000)
  1274. erase_timeout = 1000;
  1275. return erase_timeout;
  1276. }
  1277. static unsigned int mmc_erase_timeout(struct mmc_card *card,
  1278. unsigned int arg,
  1279. unsigned int qty)
  1280. {
  1281. if (mmc_card_sd(card))
  1282. return mmc_sd_erase_timeout(card, arg, qty);
  1283. else
  1284. return mmc_mmc_erase_timeout(card, arg, qty);
  1285. }
  1286. static int mmc_do_erase(struct mmc_card *card, unsigned int from,
  1287. unsigned int to, unsigned int arg)
  1288. {
  1289. struct mmc_command cmd = {0};
  1290. unsigned int qty = 0;
  1291. int err;
  1292. /*
  1293. * qty is used to calculate the erase timeout which depends on how many
  1294. * erase groups (or allocation units in SD terminology) are affected.
  1295. * We count erasing part of an erase group as one erase group.
  1296. * For SD, the allocation units are always a power of 2. For MMC, the
  1297. * erase group size is almost certainly also power of 2, but it does not
  1298. * seem to insist on that in the JEDEC standard, so we fall back to
  1299. * division in that case. SD may not specify an allocation unit size,
  1300. * in which case the timeout is based on the number of write blocks.
  1301. *
  1302. * Note that the timeout for secure trim 2 will only be correct if the
  1303. * number of erase groups specified is the same as the total of all
  1304. * preceding secure trim 1 commands. Since the power may have been
  1305. * lost since the secure trim 1 commands occurred, it is generally
  1306. * impossible to calculate the secure trim 2 timeout correctly.
  1307. */
  1308. if (card->erase_shift)
  1309. qty += ((to >> card->erase_shift) -
  1310. (from >> card->erase_shift)) + 1;
  1311. else if (mmc_card_sd(card))
  1312. qty += to - from + 1;
  1313. else
  1314. qty += ((to / card->erase_size) -
  1315. (from / card->erase_size)) + 1;
  1316. if (!mmc_card_blockaddr(card)) {
  1317. from <<= 9;
  1318. to <<= 9;
  1319. }
  1320. if (mmc_card_sd(card))
  1321. cmd.opcode = SD_ERASE_WR_BLK_START;
  1322. else
  1323. cmd.opcode = MMC_ERASE_GROUP_START;
  1324. cmd.arg = from;
  1325. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1326. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1327. if (err) {
  1328. pr_err("mmc_erase: group start error %d, "
  1329. "status %#x\n", err, cmd.resp[0]);
  1330. err = -EIO;
  1331. goto out;
  1332. }
  1333. memset(&cmd, 0, sizeof(struct mmc_command));
  1334. if (mmc_card_sd(card))
  1335. cmd.opcode = SD_ERASE_WR_BLK_END;
  1336. else
  1337. cmd.opcode = MMC_ERASE_GROUP_END;
  1338. cmd.arg = to;
  1339. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1340. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1341. if (err) {
  1342. pr_err("mmc_erase: group end error %d, status %#x\n",
  1343. err, cmd.resp[0]);
  1344. err = -EIO;
  1345. goto out;
  1346. }
  1347. memset(&cmd, 0, sizeof(struct mmc_command));
  1348. cmd.opcode = MMC_ERASE;
  1349. cmd.arg = arg;
  1350. cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  1351. cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
  1352. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1353. if (err) {
  1354. pr_err("mmc_erase: erase error %d, status %#x\n",
  1355. err, cmd.resp[0]);
  1356. err = -EIO;
  1357. goto out;
  1358. }
  1359. if (mmc_host_is_spi(card->host))
  1360. goto out;
  1361. do {
  1362. memset(&cmd, 0, sizeof(struct mmc_command));
  1363. cmd.opcode = MMC_SEND_STATUS;
  1364. cmd.arg = card->rca << 16;
  1365. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1366. /* Do not retry else we can't see errors */
  1367. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1368. if (err || (cmd.resp[0] & 0xFDF92000)) {
  1369. pr_err("error %d requesting status %#x\n",
  1370. err, cmd.resp[0]);
  1371. err = -EIO;
  1372. goto out;
  1373. }
  1374. } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
  1375. R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG);
  1376. out:
  1377. return err;
  1378. }
  1379. /**
  1380. * mmc_erase - erase sectors.
  1381. * @card: card to erase
  1382. * @from: first sector to erase
  1383. * @nr: number of sectors to erase
  1384. * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
  1385. *
  1386. * Caller must claim host before calling this function.
  1387. */
  1388. int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
  1389. unsigned int arg)
  1390. {
  1391. unsigned int rem, to = from + nr;
  1392. if (!(card->host->caps & MMC_CAP_ERASE) ||
  1393. !(card->csd.cmdclass & CCC_ERASE))
  1394. return -EOPNOTSUPP;
  1395. if (!card->erase_size)
  1396. return -EOPNOTSUPP;
  1397. if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
  1398. return -EOPNOTSUPP;
  1399. if ((arg & MMC_SECURE_ARGS) &&
  1400. !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
  1401. return -EOPNOTSUPP;
  1402. if ((arg & MMC_TRIM_ARGS) &&
  1403. !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
  1404. return -EOPNOTSUPP;
  1405. if (arg == MMC_SECURE_ERASE_ARG) {
  1406. if (from % card->erase_size || nr % card->erase_size)
  1407. return -EINVAL;
  1408. }
  1409. if (arg == MMC_ERASE_ARG) {
  1410. rem = from % card->erase_size;
  1411. if (rem) {
  1412. rem = card->erase_size - rem;
  1413. from += rem;
  1414. if (nr > rem)
  1415. nr -= rem;
  1416. else
  1417. return 0;
  1418. }
  1419. rem = nr % card->erase_size;
  1420. if (rem)
  1421. nr -= rem;
  1422. }
  1423. if (nr == 0)
  1424. return 0;
  1425. to = from + nr;
  1426. if (to <= from)
  1427. return -EINVAL;
  1428. /* 'from' and 'to' are inclusive */
  1429. to -= 1;
  1430. return mmc_do_erase(card, from, to, arg);
  1431. }
  1432. EXPORT_SYMBOL(mmc_erase);
  1433. int mmc_can_erase(struct mmc_card *card)
  1434. {
  1435. if ((card->host->caps & MMC_CAP_ERASE) &&
  1436. (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
  1437. return 1;
  1438. return 0;
  1439. }
  1440. EXPORT_SYMBOL(mmc_can_erase);
  1441. int mmc_can_trim(struct mmc_card *card)
  1442. {
  1443. if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
  1444. return 1;
  1445. if (mmc_can_discard(card))
  1446. return 1;
  1447. return 0;
  1448. }
  1449. EXPORT_SYMBOL(mmc_can_trim);
  1450. int mmc_can_discard(struct mmc_card *card)
  1451. {
  1452. /*
  1453. * As there's no way to detect the discard support bit at v4.5
  1454. * use the s/w feature support filed.
  1455. */
  1456. if (card->ext_csd.feature_support & MMC_DISCARD_FEATURE)
  1457. return 1;
  1458. return 0;
  1459. }
  1460. EXPORT_SYMBOL(mmc_can_discard);
  1461. int mmc_can_sanitize(struct mmc_card *card)
  1462. {
  1463. if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE)
  1464. return 1;
  1465. return 0;
  1466. }
  1467. EXPORT_SYMBOL(mmc_can_sanitize);
  1468. int mmc_can_secure_erase_trim(struct mmc_card *card)
  1469. {
  1470. if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
  1471. return 1;
  1472. return 0;
  1473. }
  1474. EXPORT_SYMBOL(mmc_can_secure_erase_trim);
  1475. int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
  1476. unsigned int nr)
  1477. {
  1478. if (!card->erase_size)
  1479. return 0;
  1480. if (from % card->erase_size || nr % card->erase_size)
  1481. return 0;
  1482. return 1;
  1483. }
  1484. EXPORT_SYMBOL(mmc_erase_group_aligned);
  1485. static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
  1486. unsigned int arg)
  1487. {
  1488. struct mmc_host *host = card->host;
  1489. unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
  1490. unsigned int last_timeout = 0;
  1491. if (card->erase_shift)
  1492. max_qty = UINT_MAX >> card->erase_shift;
  1493. else if (mmc_card_sd(card))
  1494. max_qty = UINT_MAX;
  1495. else
  1496. max_qty = UINT_MAX / card->erase_size;
  1497. /* Find the largest qty with an OK timeout */
  1498. do {
  1499. y = 0;
  1500. for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
  1501. timeout = mmc_erase_timeout(card, arg, qty + x);
  1502. if (timeout > host->max_discard_to)
  1503. break;
  1504. if (timeout < last_timeout)
  1505. break;
  1506. last_timeout = timeout;
  1507. y = x;
  1508. }
  1509. qty += y;
  1510. } while (y);
  1511. if (!qty)
  1512. return 0;
  1513. if (qty == 1)
  1514. return 1;
  1515. /* Convert qty to sectors */
  1516. if (card->erase_shift)
  1517. max_discard = --qty << card->erase_shift;
  1518. else if (mmc_card_sd(card))
  1519. max_discard = qty;
  1520. else
  1521. max_discard = --qty * card->erase_size;
  1522. return max_discard;
  1523. }
  1524. unsigned int mmc_calc_max_discard(struct mmc_card *card)
  1525. {
  1526. struct mmc_host *host = card->host;
  1527. unsigned int max_discard, max_trim;
  1528. if (!host->max_discard_to)
  1529. return UINT_MAX;
  1530. /*
  1531. * Without erase_group_def set, MMC erase timeout depends on clock
  1532. * frequence which can change. In that case, the best choice is
  1533. * just the preferred erase size.
  1534. */
  1535. if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
  1536. return card->pref_erase;
  1537. max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
  1538. if (mmc_can_trim(card)) {
  1539. max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
  1540. if (max_trim < max_discard)
  1541. max_discard = max_trim;
  1542. } else if (max_discard < card->erase_size) {
  1543. max_discard = 0;
  1544. }
  1545. pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
  1546. mmc_hostname(host), max_discard, host->max_discard_to);
  1547. return max_discard;
  1548. }
  1549. EXPORT_SYMBOL(mmc_calc_max_discard);
  1550. int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
  1551. {
  1552. struct mmc_command cmd = {0};
  1553. if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
  1554. return 0;
  1555. cmd.opcode = MMC_SET_BLOCKLEN;
  1556. cmd.arg = blocklen;
  1557. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1558. return mmc_wait_for_cmd(card->host, &cmd, 5);
  1559. }
  1560. EXPORT_SYMBOL(mmc_set_blocklen);
  1561. static void mmc_hw_reset_for_init(struct mmc_host *host)
  1562. {
  1563. if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
  1564. return;
  1565. mmc_host_clk_hold(host);
  1566. host->ops->hw_reset(host);
  1567. mmc_host_clk_release(host);
  1568. }
  1569. int mmc_can_reset(struct mmc_card *card)
  1570. {
  1571. u8 rst_n_function;
  1572. if (!mmc_card_mmc(card))
  1573. return 0;
  1574. rst_n_function = card->ext_csd.rst_n_function;
  1575. if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
  1576. return 0;
  1577. return 1;
  1578. }
  1579. EXPORT_SYMBOL(mmc_can_reset);
  1580. static int mmc_do_hw_reset(struct mmc_host *host, int check)
  1581. {
  1582. struct mmc_card *card = host->card;
  1583. if (!host->bus_ops->power_restore)
  1584. return -EOPNOTSUPP;
  1585. if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
  1586. return -EOPNOTSUPP;
  1587. if (!card)
  1588. return -EINVAL;
  1589. if (!mmc_can_reset(card))
  1590. return -EOPNOTSUPP;
  1591. mmc_host_clk_hold(host);
  1592. mmc_set_clock(host, host->f_init);
  1593. host->ops->hw_reset(host);
  1594. /* If the reset has happened, then a status command will fail */
  1595. if (check) {
  1596. struct mmc_command cmd = {0};
  1597. int err;
  1598. cmd.opcode = MMC_SEND_STATUS;
  1599. if (!mmc_host_is_spi(card->host))
  1600. cmd.arg = card->rca << 16;
  1601. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  1602. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1603. if (!err) {
  1604. mmc_host_clk_release(host);
  1605. return -ENOSYS;
  1606. }
  1607. }
  1608. host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_DDR);
  1609. if (mmc_host_is_spi(host)) {
  1610. host->ios.chip_select = MMC_CS_HIGH;
  1611. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  1612. } else {
  1613. host->ios.chip_select = MMC_CS_DONTCARE;
  1614. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  1615. }
  1616. host->ios.bus_width = MMC_BUS_WIDTH_1;
  1617. host->ios.timing = MMC_TIMING_LEGACY;
  1618. mmc_set_ios(host);
  1619. mmc_host_clk_release(host);
  1620. return host->bus_ops->power_restore(host);
  1621. }
  1622. int mmc_hw_reset(struct mmc_host *host)
  1623. {
  1624. return mmc_do_hw_reset(host, 0);
  1625. }
  1626. EXPORT_SYMBOL(mmc_hw_reset);
  1627. int mmc_hw_reset_check(struct mmc_host *host)
  1628. {
  1629. return mmc_do_hw_reset(host, 1);
  1630. }
  1631. EXPORT_SYMBOL(mmc_hw_reset_check);
  1632. static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
  1633. {
  1634. host->f_init = freq;
  1635. #ifdef CONFIG_MMC_DEBUG
  1636. pr_info("%s: %s: trying to init card at %u Hz\n",
  1637. mmc_hostname(host), __func__, host->f_init);
  1638. #endif
  1639. mmc_power_up(host);
  1640. /*
  1641. * Some eMMCs (with VCCQ always on) may not be reset after power up, so
  1642. * do a hardware reset if possible.
  1643. */
  1644. mmc_hw_reset_for_init(host);
  1645. /* Initialization should be done at 3.3 V I/O voltage. */
  1646. mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0);
  1647. /*
  1648. * sdio_reset sends CMD52 to reset card. Since we do not know
  1649. * if the card is being re-initialized, just send it. CMD52
  1650. * should be ignored by SD/eMMC cards.
  1651. */
  1652. sdio_reset(host);
  1653. mmc_go_idle(host);
  1654. mmc_send_if_cond(host, host->ocr_avail);
  1655. /* Order's important: probe SDIO, then SD, then MMC */
  1656. if (!mmc_attach_sdio(host))
  1657. return 0;
  1658. if (!mmc_attach_sd(host))
  1659. return 0;
  1660. if (!mmc_attach_mmc(host))
  1661. return 0;
  1662. mmc_power_off(host);
  1663. return -EIO;
  1664. }
  1665. int _mmc_detect_card_removed(struct mmc_host *host)
  1666. {
  1667. int ret;
  1668. if ((host->caps & MMC_CAP_NONREMOVABLE) || !host->bus_ops->alive)
  1669. return 0;
  1670. if (!host->card || mmc_card_removed(host->card))
  1671. return 1;
  1672. ret = host->bus_ops->alive(host);
  1673. if (ret) {
  1674. mmc_card_set_removed(host->card);
  1675. pr_debug("%s: card remove detected\n", mmc_hostname(host));
  1676. }
  1677. return ret;
  1678. }
  1679. int mmc_detect_card_removed(struct mmc_host *host)
  1680. {
  1681. struct mmc_card *card = host->card;
  1682. int ret;
  1683. WARN_ON(!host->claimed);
  1684. if (!card)
  1685. return 1;
  1686. ret = mmc_card_removed(card);
  1687. /*
  1688. * The card will be considered unchanged unless we have been asked to
  1689. * detect a change or host requires polling to provide card detection.
  1690. */
  1691. if (!host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL) &&
  1692. !(host->caps2 & MMC_CAP2_DETECT_ON_ERR))
  1693. return ret;
  1694. host->detect_change = 0;
  1695. if (!ret) {
  1696. ret = _mmc_detect_card_removed(host);
  1697. if (ret && (host->caps2 & MMC_CAP2_DETECT_ON_ERR)) {
  1698. /*
  1699. * Schedule a detect work as soon as possible to let a
  1700. * rescan handle the card removal.
  1701. */
  1702. cancel_delayed_work(&host->detect);
  1703. mmc_detect_change(host, 0);
  1704. }
  1705. }
  1706. return ret;
  1707. }
  1708. EXPORT_SYMBOL(mmc_detect_card_removed);
  1709. void mmc_rescan(struct work_struct *work)
  1710. {
  1711. static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
  1712. struct mmc_host *host =
  1713. container_of(work, struct mmc_host, detect.work);
  1714. int i;
  1715. if (host->rescan_disable)
  1716. return;
  1717. mmc_bus_get(host);
  1718. /*
  1719. * if there is a _removable_ card registered, check whether it is
  1720. * still present
  1721. */
  1722. if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
  1723. && !(host->caps & MMC_CAP_NONREMOVABLE))
  1724. host->bus_ops->detect(host);
  1725. host->detect_change = 0;
  1726. /*
  1727. * Let mmc_bus_put() free the bus/bus_ops if we've found that
  1728. * the card is no longer present.
  1729. */
  1730. mmc_bus_put(host);
  1731. mmc_bus_get(host);
  1732. /* if there still is a card present, stop here */
  1733. if (host->bus_ops != NULL) {
  1734. mmc_bus_put(host);
  1735. goto out;
  1736. }
  1737. /*
  1738. * Only we can add a new handler, so it's safe to
  1739. * release the lock here.
  1740. */
  1741. mmc_bus_put(host);
  1742. if (host->ops->get_cd && host->ops->get_cd(host) == 0)
  1743. goto out;
  1744. mmc_claim_host(host);
  1745. for (i = 0; i < ARRAY_SIZE(freqs); i++) {
  1746. if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
  1747. break;
  1748. if (freqs[i] <= host->f_min)
  1749. break;
  1750. }
  1751. mmc_release_host(host);
  1752. out:
  1753. if (host->caps & MMC_CAP_NEEDS_POLL)
  1754. mmc_schedule_delayed_work(&host->detect, HZ);
  1755. }
  1756. void mmc_start_host(struct mmc_host *host)
  1757. {
  1758. mmc_power_off(host);
  1759. mmc_detect_change(host, 0);
  1760. }
  1761. void mmc_stop_host(struct mmc_host *host)
  1762. {
  1763. #ifdef CONFIG_MMC_DEBUG
  1764. unsigned long flags;
  1765. spin_lock_irqsave(&host->lock, flags);
  1766. host->removed = 1;
  1767. spin_unlock_irqrestore(&host->lock, flags);
  1768. #endif
  1769. cancel_delayed_work_sync(&host->detect);
  1770. mmc_flush_scheduled_work();
  1771. /* clear pm flags now and let card drivers set them as needed */
  1772. host->pm_flags = 0;
  1773. mmc_bus_get(host);
  1774. if (host->bus_ops && !host->bus_dead) {
  1775. /* Calling bus_ops->remove() with a claimed host can deadlock */
  1776. if (host->bus_ops->remove)
  1777. host->bus_ops->remove(host);
  1778. mmc_claim_host(host);
  1779. mmc_detach_bus(host);
  1780. mmc_power_off(host);
  1781. mmc_release_host(host);
  1782. mmc_bus_put(host);
  1783. return;
  1784. }
  1785. mmc_bus_put(host);
  1786. BUG_ON(host->card);
  1787. mmc_power_off(host);
  1788. }
  1789. int mmc_power_save_host(struct mmc_host *host)
  1790. {
  1791. int ret = 0;
  1792. #ifdef CONFIG_MMC_DEBUG
  1793. pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
  1794. #endif
  1795. mmc_bus_get(host);
  1796. if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
  1797. mmc_bus_put(host);
  1798. return -EINVAL;
  1799. }
  1800. if (host->bus_ops->power_save)
  1801. ret = host->bus_ops->power_save(host);
  1802. mmc_bus_put(host);
  1803. mmc_power_off(host);
  1804. return ret;
  1805. }
  1806. EXPORT_SYMBOL(mmc_power_save_host);
  1807. int mmc_power_restore_host(struct mmc_host *host)
  1808. {
  1809. int ret;
  1810. #ifdef CONFIG_MMC_DEBUG
  1811. pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
  1812. #endif
  1813. mmc_bus_get(host);
  1814. if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
  1815. mmc_bus_put(host);
  1816. return -EINVAL;
  1817. }
  1818. mmc_power_up(host);
  1819. ret = host->bus_ops->power_restore(host);
  1820. mmc_bus_put(host);
  1821. return ret;
  1822. }
  1823. EXPORT_SYMBOL(mmc_power_restore_host);
  1824. int mmc_card_awake(struct mmc_host *host)
  1825. {
  1826. int err = -ENOSYS;
  1827. if (host->caps2 & MMC_CAP2_NO_SLEEP_CMD)
  1828. return 0;
  1829. mmc_bus_get(host);
  1830. if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
  1831. err = host->bus_ops->awake(host);
  1832. mmc_bus_put(host);
  1833. return err;
  1834. }
  1835. EXPORT_SYMBOL(mmc_card_awake);
  1836. int mmc_card_sleep(struct mmc_host *host)
  1837. {
  1838. int err = -ENOSYS;
  1839. if (host->caps2 & MMC_CAP2_NO_SLEEP_CMD)
  1840. return 0;
  1841. mmc_bus_get(host);
  1842. if (host->bus_ops && !host->bus_dead && host->bus_ops->sleep)
  1843. err = host->bus_ops->sleep(host);
  1844. mmc_bus_put(host);
  1845. return err;
  1846. }
  1847. EXPORT_SYMBOL(mmc_card_sleep);
  1848. int mmc_card_can_sleep(struct mmc_host *host)
  1849. {
  1850. struct mmc_card *card = host->card;
  1851. if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
  1852. return 1;
  1853. return 0;
  1854. }
  1855. EXPORT_SYMBOL(mmc_card_can_sleep);
  1856. /*
  1857. * Flush the cache to the non-volatile storage.
  1858. */
  1859. int mmc_flush_cache(struct mmc_card *card)
  1860. {
  1861. struct mmc_host *host = card->host;
  1862. int err = 0;
  1863. if (!(host->caps2 & MMC_CAP2_CACHE_CTRL))
  1864. return err;
  1865. if (mmc_card_mmc(card) &&
  1866. (card->ext_csd.cache_size > 0) &&
  1867. (card->ext_csd.cache_ctrl & 1)) {
  1868. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1869. EXT_CSD_FLUSH_CACHE, 1, 0);
  1870. if (err)
  1871. pr_err("%s: cache flush error %d\n",
  1872. mmc_hostname(card->host), err);
  1873. }
  1874. return err;
  1875. }
  1876. EXPORT_SYMBOL(mmc_flush_cache);
  1877. /*
  1878. * Turn the cache ON/OFF.
  1879. * Turning the cache OFF shall trigger flushing of the data
  1880. * to the non-volatile storage.
  1881. */
  1882. int mmc_cache_ctrl(struct mmc_host *host, u8 enable)
  1883. {
  1884. struct mmc_card *card = host->card;
  1885. unsigned int timeout;
  1886. int err = 0;
  1887. if (!(host->caps2 & MMC_CAP2_CACHE_CTRL) ||
  1888. mmc_card_is_removable(host))
  1889. return err;
  1890. if (card && mmc_card_mmc(card) &&
  1891. (card->ext_csd.cache_size > 0)) {
  1892. enable = !!enable;
  1893. if (card->ext_csd.cache_ctrl ^ enable) {
  1894. timeout = enable ? card->ext_csd.generic_cmd6_time : 0;
  1895. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1896. EXT_CSD_CACHE_CTRL, enable, timeout);
  1897. if (err)
  1898. pr_err("%s: cache %s error %d\n",
  1899. mmc_hostname(card->host),
  1900. enable ? "on" : "off",
  1901. err);
  1902. else
  1903. card->ext_csd.cache_ctrl = enable;
  1904. }
  1905. }
  1906. return err;
  1907. }
  1908. EXPORT_SYMBOL(mmc_cache_ctrl);
  1909. #ifdef CONFIG_PM
  1910. /**
  1911. * mmc_suspend_host - suspend a host
  1912. * @host: mmc host
  1913. */
  1914. int mmc_suspend_host(struct mmc_host *host)
  1915. {
  1916. int err = 0;
  1917. cancel_delayed_work(&host->detect);
  1918. mmc_flush_scheduled_work();
  1919. if (mmc_try_claim_host(host)) {
  1920. err = mmc_cache_ctrl(host, 0);
  1921. mmc_release_host(host);
  1922. } else {
  1923. err = -EBUSY;
  1924. }
  1925. if (err)
  1926. goto out;
  1927. mmc_bus_get(host);
  1928. if (host->bus_ops && !host->bus_dead) {
  1929. /*
  1930. * A long response time is not acceptable for device drivers
  1931. * when doing suspend. Prevent mmc_claim_host in the suspend
  1932. * sequence, to potentially wait "forever" by trying to
  1933. * pre-claim the host.
  1934. */
  1935. if (mmc_try_claim_host(host)) {
  1936. if (host->bus_ops->suspend) {
  1937. err = host->bus_ops->suspend(host);
  1938. }
  1939. mmc_release_host(host);
  1940. if (err == -ENOSYS || !host->bus_ops->resume) {
  1941. /*
  1942. * We simply "remove" the card in this case.
  1943. * It will be redetected on resume. (Calling
  1944. * bus_ops->remove() with a claimed host can
  1945. * deadlock.)
  1946. */
  1947. if (host->bus_ops->remove)
  1948. host->bus_ops->remove(host);
  1949. mmc_claim_host(host);
  1950. mmc_detach_bus(host);
  1951. mmc_power_off(host);
  1952. mmc_release_host(host);
  1953. host->pm_flags = 0;
  1954. err = 0;
  1955. }
  1956. } else {
  1957. err = -EBUSY;
  1958. }
  1959. }
  1960. mmc_bus_put(host);
  1961. if (!err && !mmc_card_keep_power(host))
  1962. mmc_power_off(host);
  1963. out:
  1964. return err;
  1965. }
  1966. EXPORT_SYMBOL(mmc_suspend_host);
  1967. /**
  1968. * mmc_resume_host - resume a previously suspended host
  1969. * @host: mmc host
  1970. */
  1971. int mmc_resume_host(struct mmc_host *host)
  1972. {
  1973. int err = 0;
  1974. mmc_bus_get(host);
  1975. if (host->bus_ops && !host->bus_dead) {
  1976. if (!mmc_card_keep_power(host)) {
  1977. mmc_power_up(host);
  1978. mmc_select_voltage(host, host->ocr);
  1979. /*
  1980. * Tell runtime PM core we just powered up the card,
  1981. * since it still believes the card is powered off.
  1982. * Note that currently runtime PM is only enabled
  1983. * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
  1984. */
  1985. if (mmc_card_sdio(host->card) &&
  1986. (host->caps & MMC_CAP_POWER_OFF_CARD)) {
  1987. pm_runtime_disable(&host->card->dev);
  1988. pm_runtime_set_active(&host->card->dev);
  1989. pm_runtime_enable(&host->card->dev);
  1990. }
  1991. }
  1992. BUG_ON(!host->bus_ops->resume);
  1993. err = host->bus_ops->resume(host);
  1994. if (err) {
  1995. pr_warning("%s: error %d during resume "
  1996. "(card was removed?)\n",
  1997. mmc_hostname(host), err);
  1998. err = 0;
  1999. }
  2000. }
  2001. host->pm_flags &= ~MMC_PM_KEEP_POWER;
  2002. mmc_bus_put(host);
  2003. return err;
  2004. }
  2005. EXPORT_SYMBOL(mmc_resume_host);
  2006. /* Do the card removal on suspend if card is assumed removeable
  2007. * Do that in pm notifier while userspace isn't yet frozen, so we will be able
  2008. to sync the card.
  2009. */
  2010. int mmc_pm_notify(struct notifier_block *notify_block,
  2011. unsigned long mode, void *unused)
  2012. {
  2013. struct mmc_host *host = container_of(
  2014. notify_block, struct mmc_host, pm_notify);
  2015. unsigned long flags;
  2016. switch (mode) {
  2017. case PM_HIBERNATION_PREPARE:
  2018. case PM_SUSPEND_PREPARE:
  2019. spin_lock_irqsave(&host->lock, flags);
  2020. host->rescan_disable = 1;
  2021. host->power_notify_type = MMC_HOST_PW_NOTIFY_SHORT;
  2022. spin_unlock_irqrestore(&host->lock, flags);
  2023. cancel_delayed_work_sync(&host->detect);
  2024. if (!host->bus_ops || host->bus_ops->suspend)
  2025. break;
  2026. /* Calling bus_ops->remove() with a claimed host can deadlock */
  2027. if (host->bus_ops->remove)
  2028. host->bus_ops->remove(host);
  2029. mmc_claim_host(host);
  2030. mmc_detach_bus(host);
  2031. mmc_power_off(host);
  2032. mmc_release_host(host);
  2033. host->pm_flags = 0;
  2034. break;
  2035. case PM_POST_SUSPEND:
  2036. case PM_POST_HIBERNATION:
  2037. case PM_POST_RESTORE:
  2038. spin_lock_irqsave(&host->lock, flags);
  2039. host->rescan_disable = 0;
  2040. host->power_notify_type = MMC_HOST_PW_NOTIFY_LONG;
  2041. spin_unlock_irqrestore(&host->lock, flags);
  2042. mmc_detect_change(host, 0);
  2043. }
  2044. return 0;
  2045. }
  2046. #endif
  2047. static int __init mmc_init(void)
  2048. {
  2049. int ret;
  2050. workqueue = alloc_ordered_workqueue("kmmcd", 0);
  2051. if (!workqueue)
  2052. return -ENOMEM;
  2053. ret = mmc_register_bus();
  2054. if (ret)
  2055. goto destroy_workqueue;
  2056. ret = mmc_register_host_class();
  2057. if (ret)
  2058. goto unregister_bus;
  2059. ret = sdio_register_bus();
  2060. if (ret)
  2061. goto unregister_host_class;
  2062. return 0;
  2063. unregister_host_class:
  2064. mmc_unregister_host_class();
  2065. unregister_bus:
  2066. mmc_unregister_bus();
  2067. destroy_workqueue:
  2068. destroy_workqueue(workqueue);
  2069. return ret;
  2070. }
  2071. static void __exit mmc_exit(void)
  2072. {
  2073. sdio_unregister_bus();
  2074. mmc_unregister_host_class();
  2075. mmc_unregister_bus();
  2076. destroy_workqueue(workqueue);
  2077. }
  2078. subsys_initcall(mmc_init);
  2079. module_exit(mmc_exit);
  2080. MODULE_LICENSE("GPL");