core.c 75 KB

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