core.c 59 KB

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