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