core.c 67 KB

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