core.c 76 KB

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