core.c 71 KB

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