core.c 59 KB

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