core.c 68 KB

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