core.c 76 KB

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