core.c 69 KB

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