core.c 63 KB

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