core.c 71 KB

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