core.c 65 KB

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