core.c 62 KB

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