core.c 67 KB

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