core.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099
  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/mmc/card.h>
  28. #include <linux/mmc/host.h>
  29. #include <linux/mmc/mmc.h>
  30. #include <linux/mmc/sd.h>
  31. #include "core.h"
  32. #include "bus.h"
  33. #include "host.h"
  34. #include "sdio_bus.h"
  35. #include "mmc_ops.h"
  36. #include "sd_ops.h"
  37. #include "sdio_ops.h"
  38. static struct workqueue_struct *workqueue;
  39. /*
  40. * Enabling software CRCs on the data blocks can be a significant (30%)
  41. * performance cost, and for other reasons may not always be desired.
  42. * So we allow it it to be disabled.
  43. */
  44. int use_spi_crc = 1;
  45. module_param(use_spi_crc, bool, 0);
  46. /*
  47. * We normally treat cards as removed during suspend if they are not
  48. * known to be on a non-removable bus, to avoid the risk of writing
  49. * back data to a different card after resume. Allow this to be
  50. * overridden if necessary.
  51. */
  52. #ifdef CONFIG_MMC_UNSAFE_RESUME
  53. int mmc_assume_removable;
  54. #else
  55. int mmc_assume_removable = 1;
  56. #endif
  57. EXPORT_SYMBOL(mmc_assume_removable);
  58. module_param_named(removable, mmc_assume_removable, bool, 0644);
  59. MODULE_PARM_DESC(
  60. removable,
  61. "MMC/SD cards are removable and may be removed during suspend");
  62. /*
  63. * Internal function. Schedule delayed work in the MMC work queue.
  64. */
  65. static int mmc_schedule_delayed_work(struct delayed_work *work,
  66. unsigned long delay)
  67. {
  68. return queue_delayed_work(workqueue, work, delay);
  69. }
  70. /*
  71. * Internal function. Flush all scheduled work from the MMC work queue.
  72. */
  73. static void mmc_flush_scheduled_work(void)
  74. {
  75. flush_workqueue(workqueue);
  76. }
  77. /**
  78. * mmc_request_done - finish processing an MMC request
  79. * @host: MMC host which completed request
  80. * @mrq: MMC request which request
  81. *
  82. * MMC drivers should call this function when they have completed
  83. * their processing of a request.
  84. */
  85. void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
  86. {
  87. struct mmc_command *cmd = mrq->cmd;
  88. int err = cmd->error;
  89. if (err && cmd->retries && mmc_host_is_spi(host)) {
  90. if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
  91. cmd->retries = 0;
  92. }
  93. if (err && cmd->retries) {
  94. pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
  95. mmc_hostname(host), cmd->opcode, err);
  96. cmd->retries--;
  97. cmd->error = 0;
  98. host->ops->request(host, mrq);
  99. } else {
  100. led_trigger_event(host->led, LED_OFF);
  101. pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
  102. mmc_hostname(host), cmd->opcode, err,
  103. cmd->resp[0], cmd->resp[1],
  104. cmd->resp[2], cmd->resp[3]);
  105. if (mrq->data) {
  106. pr_debug("%s: %d bytes transferred: %d\n",
  107. mmc_hostname(host),
  108. mrq->data->bytes_xfered, mrq->data->error);
  109. }
  110. if (mrq->stop) {
  111. pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
  112. mmc_hostname(host), mrq->stop->opcode,
  113. mrq->stop->error,
  114. mrq->stop->resp[0], mrq->stop->resp[1],
  115. mrq->stop->resp[2], mrq->stop->resp[3]);
  116. }
  117. if (mrq->done)
  118. mrq->done(mrq);
  119. mmc_host_clk_gate(host);
  120. }
  121. }
  122. EXPORT_SYMBOL(mmc_request_done);
  123. static void
  124. mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
  125. {
  126. #ifdef CONFIG_MMC_DEBUG
  127. unsigned int i, sz;
  128. struct scatterlist *sg;
  129. #endif
  130. pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
  131. mmc_hostname(host), mrq->cmd->opcode,
  132. mrq->cmd->arg, mrq->cmd->flags);
  133. if (mrq->data) {
  134. pr_debug("%s: blksz %d blocks %d flags %08x "
  135. "tsac %d ms nsac %d\n",
  136. mmc_hostname(host), mrq->data->blksz,
  137. mrq->data->blocks, mrq->data->flags,
  138. mrq->data->timeout_ns / 1000000,
  139. mrq->data->timeout_clks);
  140. }
  141. if (mrq->stop) {
  142. pr_debug("%s: CMD%u arg %08x flags %08x\n",
  143. mmc_hostname(host), mrq->stop->opcode,
  144. mrq->stop->arg, mrq->stop->flags);
  145. }
  146. WARN_ON(!host->claimed);
  147. mrq->cmd->error = 0;
  148. mrq->cmd->mrq = mrq;
  149. if (mrq->data) {
  150. BUG_ON(mrq->data->blksz > host->max_blk_size);
  151. BUG_ON(mrq->data->blocks > host->max_blk_count);
  152. BUG_ON(mrq->data->blocks * mrq->data->blksz >
  153. host->max_req_size);
  154. #ifdef CONFIG_MMC_DEBUG
  155. sz = 0;
  156. for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
  157. sz += sg->length;
  158. BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
  159. #endif
  160. mrq->cmd->data = mrq->data;
  161. mrq->data->error = 0;
  162. mrq->data->mrq = mrq;
  163. if (mrq->stop) {
  164. mrq->data->stop = mrq->stop;
  165. mrq->stop->error = 0;
  166. mrq->stop->mrq = mrq;
  167. }
  168. }
  169. mmc_host_clk_ungate(host);
  170. led_trigger_event(host->led, LED_FULL);
  171. host->ops->request(host, mrq);
  172. }
  173. static void mmc_wait_done(struct mmc_request *mrq)
  174. {
  175. complete(&mrq->completion);
  176. }
  177. static void __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
  178. {
  179. init_completion(&mrq->completion);
  180. mrq->done = mmc_wait_done;
  181. mmc_start_request(host, mrq);
  182. }
  183. static void mmc_wait_for_req_done(struct mmc_host *host,
  184. struct mmc_request *mrq)
  185. {
  186. wait_for_completion(&mrq->completion);
  187. }
  188. /**
  189. * mmc_pre_req - Prepare for a new request
  190. * @host: MMC host to prepare command
  191. * @mrq: MMC request to prepare for
  192. * @is_first_req: true if there is no previous started request
  193. * that may run in parellel to this call, otherwise false
  194. *
  195. * mmc_pre_req() is called in prior to mmc_start_req() to let
  196. * host prepare for the new request. Preparation of a request may be
  197. * performed while another request is running on the host.
  198. */
  199. static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq,
  200. bool is_first_req)
  201. {
  202. if (host->ops->pre_req)
  203. host->ops->pre_req(host, mrq, is_first_req);
  204. }
  205. /**
  206. * mmc_post_req - Post process a completed request
  207. * @host: MMC host to post process command
  208. * @mrq: MMC request to post process for
  209. * @err: Error, if non zero, clean up any resources made in pre_req
  210. *
  211. * Let the host post process a completed request. Post processing of
  212. * a request may be performed while another reuqest is running.
  213. */
  214. static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
  215. int err)
  216. {
  217. if (host->ops->post_req)
  218. host->ops->post_req(host, mrq, err);
  219. }
  220. /**
  221. * mmc_start_req - start a non-blocking request
  222. * @host: MMC host to start command
  223. * @areq: async request to start
  224. * @error: out parameter returns 0 for success, otherwise non zero
  225. *
  226. * Start a new MMC custom command request for a host.
  227. * If there is on ongoing async request wait for completion
  228. * of that request and start the new one and return.
  229. * Does not wait for the new request to complete.
  230. *
  231. * Returns the completed request, NULL in case of none completed.
  232. * Wait for the an ongoing request (previoulsy started) to complete and
  233. * return the completed request. If there is no ongoing request, NULL
  234. * is returned without waiting. NULL is not an error condition.
  235. */
  236. struct mmc_async_req *mmc_start_req(struct mmc_host *host,
  237. struct mmc_async_req *areq, int *error)
  238. {
  239. int err = 0;
  240. struct mmc_async_req *data = host->areq;
  241. /* Prepare a new request */
  242. if (areq)
  243. mmc_pre_req(host, areq->mrq, !host->areq);
  244. if (host->areq) {
  245. mmc_wait_for_req_done(host, host->areq->mrq);
  246. err = host->areq->err_check(host->card, host->areq);
  247. if (err) {
  248. mmc_post_req(host, host->areq->mrq, 0);
  249. if (areq)
  250. mmc_post_req(host, areq->mrq, -EINVAL);
  251. host->areq = NULL;
  252. goto out;
  253. }
  254. }
  255. if (areq)
  256. __mmc_start_req(host, areq->mrq);
  257. if (host->areq)
  258. mmc_post_req(host, host->areq->mrq, 0);
  259. host->areq = areq;
  260. out:
  261. if (error)
  262. *error = err;
  263. return data;
  264. }
  265. EXPORT_SYMBOL(mmc_start_req);
  266. /**
  267. * mmc_wait_for_req - start a request and wait for completion
  268. * @host: MMC host to start command
  269. * @mrq: MMC request to start
  270. *
  271. * Start a new MMC custom command request for a host, and wait
  272. * for the command to complete. Does not attempt to parse the
  273. * response.
  274. */
  275. void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
  276. {
  277. __mmc_start_req(host, mrq);
  278. mmc_wait_for_req_done(host, mrq);
  279. }
  280. EXPORT_SYMBOL(mmc_wait_for_req);
  281. /**
  282. * mmc_wait_for_cmd - start a command and wait for completion
  283. * @host: MMC host to start command
  284. * @cmd: MMC command to start
  285. * @retries: maximum number of retries
  286. *
  287. * Start a new MMC command for a host, and wait for the command
  288. * to complete. Return any error that occurred while the command
  289. * was executing. Do not attempt to parse the response.
  290. */
  291. int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
  292. {
  293. struct mmc_request mrq = {0};
  294. WARN_ON(!host->claimed);
  295. memset(cmd->resp, 0, sizeof(cmd->resp));
  296. cmd->retries = retries;
  297. mrq.cmd = cmd;
  298. cmd->data = NULL;
  299. mmc_wait_for_req(host, &mrq);
  300. return cmd->error;
  301. }
  302. EXPORT_SYMBOL(mmc_wait_for_cmd);
  303. /**
  304. * mmc_set_data_timeout - set the timeout for a data command
  305. * @data: data phase for command
  306. * @card: the MMC card associated with the data transfer
  307. *
  308. * Computes the data timeout parameters according to the
  309. * correct algorithm given the card type.
  310. */
  311. void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
  312. {
  313. unsigned int mult;
  314. /*
  315. * SDIO cards only define an upper 1 s limit on access.
  316. */
  317. if (mmc_card_sdio(card)) {
  318. data->timeout_ns = 1000000000;
  319. data->timeout_clks = 0;
  320. return;
  321. }
  322. /*
  323. * SD cards use a 100 multiplier rather than 10
  324. */
  325. mult = mmc_card_sd(card) ? 100 : 10;
  326. /*
  327. * Scale up the multiplier (and therefore the timeout) by
  328. * the r2w factor for writes.
  329. */
  330. if (data->flags & MMC_DATA_WRITE)
  331. mult <<= card->csd.r2w_factor;
  332. data->timeout_ns = card->csd.tacc_ns * mult;
  333. data->timeout_clks = card->csd.tacc_clks * mult;
  334. /*
  335. * SD cards also have an upper limit on the timeout.
  336. */
  337. if (mmc_card_sd(card)) {
  338. unsigned int timeout_us, limit_us;
  339. timeout_us = data->timeout_ns / 1000;
  340. if (mmc_host_clk_rate(card->host))
  341. timeout_us += data->timeout_clks * 1000 /
  342. (mmc_host_clk_rate(card->host) / 1000);
  343. if (data->flags & MMC_DATA_WRITE)
  344. /*
  345. * The limit is really 250 ms, but that is
  346. * insufficient for some crappy cards.
  347. */
  348. limit_us = 300000;
  349. else
  350. limit_us = 100000;
  351. /*
  352. * SDHC cards always use these fixed values.
  353. */
  354. if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
  355. data->timeout_ns = limit_us * 1000;
  356. data->timeout_clks = 0;
  357. }
  358. }
  359. /*
  360. * Some cards need very high timeouts if driven in SPI mode.
  361. * The worst observed timeout was 900ms after writing a
  362. * continuous stream of data until the internal logic
  363. * overflowed.
  364. */
  365. if (mmc_host_is_spi(card->host)) {
  366. if (data->flags & MMC_DATA_WRITE) {
  367. if (data->timeout_ns < 1000000000)
  368. data->timeout_ns = 1000000000; /* 1s */
  369. } else {
  370. if (data->timeout_ns < 100000000)
  371. data->timeout_ns = 100000000; /* 100ms */
  372. }
  373. }
  374. }
  375. EXPORT_SYMBOL(mmc_set_data_timeout);
  376. /**
  377. * mmc_align_data_size - pads a transfer size to a more optimal value
  378. * @card: the MMC card associated with the data transfer
  379. * @sz: original transfer size
  380. *
  381. * Pads the original data size with a number of extra bytes in
  382. * order to avoid controller bugs and/or performance hits
  383. * (e.g. some controllers revert to PIO for certain sizes).
  384. *
  385. * Returns the improved size, which might be unmodified.
  386. *
  387. * Note that this function is only relevant when issuing a
  388. * single scatter gather entry.
  389. */
  390. unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
  391. {
  392. /*
  393. * FIXME: We don't have a system for the controller to tell
  394. * the core about its problems yet, so for now we just 32-bit
  395. * align the size.
  396. */
  397. sz = ((sz + 3) / 4) * 4;
  398. return sz;
  399. }
  400. EXPORT_SYMBOL(mmc_align_data_size);
  401. /**
  402. * mmc_host_enable - enable a host.
  403. * @host: mmc host to enable
  404. *
  405. * Hosts that support power saving can use the 'enable' and 'disable'
  406. * methods to exit and enter power saving states. For more information
  407. * see comments for struct mmc_host_ops.
  408. */
  409. int mmc_host_enable(struct mmc_host *host)
  410. {
  411. if (!(host->caps & MMC_CAP_DISABLE))
  412. return 0;
  413. if (host->en_dis_recurs)
  414. return 0;
  415. if (host->nesting_cnt++)
  416. return 0;
  417. cancel_delayed_work_sync(&host->disable);
  418. if (host->enabled)
  419. return 0;
  420. if (host->ops->enable) {
  421. int err;
  422. host->en_dis_recurs = 1;
  423. err = host->ops->enable(host);
  424. host->en_dis_recurs = 0;
  425. if (err) {
  426. pr_debug("%s: enable error %d\n",
  427. mmc_hostname(host), err);
  428. return err;
  429. }
  430. }
  431. host->enabled = 1;
  432. return 0;
  433. }
  434. EXPORT_SYMBOL(mmc_host_enable);
  435. static int mmc_host_do_disable(struct mmc_host *host, int lazy)
  436. {
  437. if (host->ops->disable) {
  438. int err;
  439. host->en_dis_recurs = 1;
  440. err = host->ops->disable(host, lazy);
  441. host->en_dis_recurs = 0;
  442. if (err < 0) {
  443. pr_debug("%s: disable error %d\n",
  444. mmc_hostname(host), err);
  445. return err;
  446. }
  447. if (err > 0) {
  448. unsigned long delay = msecs_to_jiffies(err);
  449. mmc_schedule_delayed_work(&host->disable, delay);
  450. }
  451. }
  452. host->enabled = 0;
  453. return 0;
  454. }
  455. /**
  456. * mmc_host_disable - disable a host.
  457. * @host: mmc host to disable
  458. *
  459. * Hosts that support power saving can use the 'enable' and 'disable'
  460. * methods to exit and enter power saving states. For more information
  461. * see comments for struct mmc_host_ops.
  462. */
  463. int mmc_host_disable(struct mmc_host *host)
  464. {
  465. int err;
  466. if (!(host->caps & MMC_CAP_DISABLE))
  467. return 0;
  468. if (host->en_dis_recurs)
  469. return 0;
  470. if (--host->nesting_cnt)
  471. return 0;
  472. if (!host->enabled)
  473. return 0;
  474. err = mmc_host_do_disable(host, 0);
  475. return err;
  476. }
  477. EXPORT_SYMBOL(mmc_host_disable);
  478. /**
  479. * __mmc_claim_host - exclusively claim a host
  480. * @host: mmc host to claim
  481. * @abort: whether or not the operation should be aborted
  482. *
  483. * Claim a host for a set of operations. If @abort is non null and
  484. * dereference a non-zero value then this will return prematurely with
  485. * that non-zero value without acquiring the lock. Returns zero
  486. * with the lock held otherwise.
  487. */
  488. int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
  489. {
  490. DECLARE_WAITQUEUE(wait, current);
  491. unsigned long flags;
  492. int stop;
  493. might_sleep();
  494. add_wait_queue(&host->wq, &wait);
  495. spin_lock_irqsave(&host->lock, flags);
  496. while (1) {
  497. set_current_state(TASK_UNINTERRUPTIBLE);
  498. stop = abort ? atomic_read(abort) : 0;
  499. if (stop || !host->claimed || host->claimer == current)
  500. break;
  501. spin_unlock_irqrestore(&host->lock, flags);
  502. schedule();
  503. spin_lock_irqsave(&host->lock, flags);
  504. }
  505. set_current_state(TASK_RUNNING);
  506. if (!stop) {
  507. host->claimed = 1;
  508. host->claimer = current;
  509. host->claim_cnt += 1;
  510. } else
  511. wake_up(&host->wq);
  512. spin_unlock_irqrestore(&host->lock, flags);
  513. remove_wait_queue(&host->wq, &wait);
  514. if (!stop)
  515. mmc_host_enable(host);
  516. return stop;
  517. }
  518. EXPORT_SYMBOL(__mmc_claim_host);
  519. /**
  520. * mmc_try_claim_host - try exclusively to claim a host
  521. * @host: mmc host to claim
  522. *
  523. * Returns %1 if the host is claimed, %0 otherwise.
  524. */
  525. int mmc_try_claim_host(struct mmc_host *host)
  526. {
  527. int claimed_host = 0;
  528. unsigned long flags;
  529. spin_lock_irqsave(&host->lock, flags);
  530. if (!host->claimed || host->claimer == current) {
  531. host->claimed = 1;
  532. host->claimer = current;
  533. host->claim_cnt += 1;
  534. claimed_host = 1;
  535. }
  536. spin_unlock_irqrestore(&host->lock, flags);
  537. return claimed_host;
  538. }
  539. EXPORT_SYMBOL(mmc_try_claim_host);
  540. /**
  541. * mmc_do_release_host - release a claimed host
  542. * @host: mmc host to release
  543. *
  544. * If you successfully claimed a host, this function will
  545. * release it again.
  546. */
  547. void mmc_do_release_host(struct mmc_host *host)
  548. {
  549. unsigned long flags;
  550. spin_lock_irqsave(&host->lock, flags);
  551. if (--host->claim_cnt) {
  552. /* Release for nested claim */
  553. spin_unlock_irqrestore(&host->lock, flags);
  554. } else {
  555. host->claimed = 0;
  556. host->claimer = NULL;
  557. spin_unlock_irqrestore(&host->lock, flags);
  558. wake_up(&host->wq);
  559. }
  560. }
  561. EXPORT_SYMBOL(mmc_do_release_host);
  562. void mmc_host_deeper_disable(struct work_struct *work)
  563. {
  564. struct mmc_host *host =
  565. container_of(work, struct mmc_host, disable.work);
  566. /* If the host is claimed then we do not want to disable it anymore */
  567. if (!mmc_try_claim_host(host))
  568. return;
  569. mmc_host_do_disable(host, 1);
  570. mmc_do_release_host(host);
  571. }
  572. /**
  573. * mmc_host_lazy_disable - lazily disable a host.
  574. * @host: mmc host to disable
  575. *
  576. * Hosts that support power saving can use the 'enable' and 'disable'
  577. * methods to exit and enter power saving states. For more information
  578. * see comments for struct mmc_host_ops.
  579. */
  580. int mmc_host_lazy_disable(struct mmc_host *host)
  581. {
  582. if (!(host->caps & MMC_CAP_DISABLE))
  583. return 0;
  584. if (host->en_dis_recurs)
  585. return 0;
  586. if (--host->nesting_cnt)
  587. return 0;
  588. if (!host->enabled)
  589. return 0;
  590. if (host->disable_delay) {
  591. mmc_schedule_delayed_work(&host->disable,
  592. msecs_to_jiffies(host->disable_delay));
  593. return 0;
  594. } else
  595. return mmc_host_do_disable(host, 1);
  596. }
  597. EXPORT_SYMBOL(mmc_host_lazy_disable);
  598. /**
  599. * mmc_release_host - release a host
  600. * @host: mmc host to release
  601. *
  602. * Release a MMC host, allowing others to claim the host
  603. * for their operations.
  604. */
  605. void mmc_release_host(struct mmc_host *host)
  606. {
  607. WARN_ON(!host->claimed);
  608. mmc_host_lazy_disable(host);
  609. mmc_do_release_host(host);
  610. }
  611. EXPORT_SYMBOL(mmc_release_host);
  612. /*
  613. * Internal function that does the actual ios call to the host driver,
  614. * optionally printing some debug output.
  615. */
  616. static inline void mmc_set_ios(struct mmc_host *host)
  617. {
  618. struct mmc_ios *ios = &host->ios;
  619. pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
  620. "width %u timing %u\n",
  621. mmc_hostname(host), ios->clock, ios->bus_mode,
  622. ios->power_mode, ios->chip_select, ios->vdd,
  623. ios->bus_width, ios->timing);
  624. if (ios->clock > 0)
  625. mmc_set_ungated(host);
  626. host->ops->set_ios(host, ios);
  627. }
  628. /*
  629. * Control chip select pin on a host.
  630. */
  631. void mmc_set_chip_select(struct mmc_host *host, int mode)
  632. {
  633. host->ios.chip_select = mode;
  634. mmc_set_ios(host);
  635. }
  636. /*
  637. * Sets the host clock to the highest possible frequency that
  638. * is below "hz".
  639. */
  640. void mmc_set_clock(struct mmc_host *host, unsigned int hz)
  641. {
  642. WARN_ON(hz < host->f_min);
  643. if (hz > host->f_max)
  644. hz = host->f_max;
  645. host->ios.clock = hz;
  646. mmc_set_ios(host);
  647. }
  648. #ifdef CONFIG_MMC_CLKGATE
  649. /*
  650. * This gates the clock by setting it to 0 Hz.
  651. */
  652. void mmc_gate_clock(struct mmc_host *host)
  653. {
  654. unsigned long flags;
  655. spin_lock_irqsave(&host->clk_lock, flags);
  656. host->clk_old = host->ios.clock;
  657. host->ios.clock = 0;
  658. host->clk_gated = true;
  659. spin_unlock_irqrestore(&host->clk_lock, flags);
  660. mmc_set_ios(host);
  661. }
  662. /*
  663. * This restores the clock from gating by using the cached
  664. * clock value.
  665. */
  666. void mmc_ungate_clock(struct mmc_host *host)
  667. {
  668. /*
  669. * We should previously have gated the clock, so the clock shall
  670. * be 0 here! The clock may however be 0 during initialization,
  671. * when some request operations are performed before setting
  672. * the frequency. When ungate is requested in that situation
  673. * we just ignore the call.
  674. */
  675. if (host->clk_old) {
  676. BUG_ON(host->ios.clock);
  677. /* This call will also set host->clk_gated to false */
  678. mmc_set_clock(host, host->clk_old);
  679. }
  680. }
  681. void mmc_set_ungated(struct mmc_host *host)
  682. {
  683. unsigned long flags;
  684. /*
  685. * We've been given a new frequency while the clock is gated,
  686. * so make sure we regard this as ungating it.
  687. */
  688. spin_lock_irqsave(&host->clk_lock, flags);
  689. host->clk_gated = false;
  690. spin_unlock_irqrestore(&host->clk_lock, flags);
  691. }
  692. #else
  693. void mmc_set_ungated(struct mmc_host *host)
  694. {
  695. }
  696. #endif
  697. /*
  698. * Change the bus mode (open drain/push-pull) of a host.
  699. */
  700. void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
  701. {
  702. host->ios.bus_mode = mode;
  703. mmc_set_ios(host);
  704. }
  705. /*
  706. * Change data bus width of a host.
  707. */
  708. void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
  709. {
  710. host->ios.bus_width = width;
  711. mmc_set_ios(host);
  712. }
  713. /**
  714. * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
  715. * @vdd: voltage (mV)
  716. * @low_bits: prefer low bits in boundary cases
  717. *
  718. * This function returns the OCR bit number according to the provided @vdd
  719. * value. If conversion is not possible a negative errno value returned.
  720. *
  721. * Depending on the @low_bits flag the function prefers low or high OCR bits
  722. * on boundary voltages. For example,
  723. * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
  724. * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
  725. *
  726. * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
  727. */
  728. static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
  729. {
  730. const int max_bit = ilog2(MMC_VDD_35_36);
  731. int bit;
  732. if (vdd < 1650 || vdd > 3600)
  733. return -EINVAL;
  734. if (vdd >= 1650 && vdd <= 1950)
  735. return ilog2(MMC_VDD_165_195);
  736. if (low_bits)
  737. vdd -= 1;
  738. /* Base 2000 mV, step 100 mV, bit's base 8. */
  739. bit = (vdd - 2000) / 100 + 8;
  740. if (bit > max_bit)
  741. return max_bit;
  742. return bit;
  743. }
  744. /**
  745. * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
  746. * @vdd_min: minimum voltage value (mV)
  747. * @vdd_max: maximum voltage value (mV)
  748. *
  749. * This function returns the OCR mask bits according to the provided @vdd_min
  750. * and @vdd_max values. If conversion is not possible the function returns 0.
  751. *
  752. * Notes wrt boundary cases:
  753. * This function sets the OCR bits for all boundary voltages, for example
  754. * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
  755. * MMC_VDD_34_35 mask.
  756. */
  757. u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
  758. {
  759. u32 mask = 0;
  760. if (vdd_max < vdd_min)
  761. return 0;
  762. /* Prefer high bits for the boundary vdd_max values. */
  763. vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
  764. if (vdd_max < 0)
  765. return 0;
  766. /* Prefer low bits for the boundary vdd_min values. */
  767. vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
  768. if (vdd_min < 0)
  769. return 0;
  770. /* Fill the mask, from max bit to min bit. */
  771. while (vdd_max >= vdd_min)
  772. mask |= 1 << vdd_max--;
  773. return mask;
  774. }
  775. EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
  776. #ifdef CONFIG_REGULATOR
  777. /**
  778. * mmc_regulator_get_ocrmask - return mask of supported voltages
  779. * @supply: regulator to use
  780. *
  781. * This returns either a negative errno, or a mask of voltages that
  782. * can be provided to MMC/SD/SDIO devices using the specified voltage
  783. * regulator. This would normally be called before registering the
  784. * MMC host adapter.
  785. */
  786. int mmc_regulator_get_ocrmask(struct regulator *supply)
  787. {
  788. int result = 0;
  789. int count;
  790. int i;
  791. count = regulator_count_voltages(supply);
  792. if (count < 0)
  793. return count;
  794. for (i = 0; i < count; i++) {
  795. int vdd_uV;
  796. int vdd_mV;
  797. vdd_uV = regulator_list_voltage(supply, i);
  798. if (vdd_uV <= 0)
  799. continue;
  800. vdd_mV = vdd_uV / 1000;
  801. result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
  802. }
  803. return result;
  804. }
  805. EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
  806. /**
  807. * mmc_regulator_set_ocr - set regulator to match host->ios voltage
  808. * @mmc: the host to regulate
  809. * @supply: regulator to use
  810. * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
  811. *
  812. * Returns zero on success, else negative errno.
  813. *
  814. * MMC host drivers may use this to enable or disable a regulator using
  815. * a particular supply voltage. This would normally be called from the
  816. * set_ios() method.
  817. */
  818. int mmc_regulator_set_ocr(struct mmc_host *mmc,
  819. struct regulator *supply,
  820. unsigned short vdd_bit)
  821. {
  822. int result = 0;
  823. int min_uV, max_uV;
  824. if (vdd_bit) {
  825. int tmp;
  826. int voltage;
  827. /* REVISIT mmc_vddrange_to_ocrmask() may have set some
  828. * bits this regulator doesn't quite support ... don't
  829. * be too picky, most cards and regulators are OK with
  830. * a 0.1V range goof (it's a small error percentage).
  831. */
  832. tmp = vdd_bit - ilog2(MMC_VDD_165_195);
  833. if (tmp == 0) {
  834. min_uV = 1650 * 1000;
  835. max_uV = 1950 * 1000;
  836. } else {
  837. min_uV = 1900 * 1000 + tmp * 100 * 1000;
  838. max_uV = min_uV + 100 * 1000;
  839. }
  840. /* avoid needless changes to this voltage; the regulator
  841. * might not allow this operation
  842. */
  843. voltage = regulator_get_voltage(supply);
  844. if (voltage < 0)
  845. result = voltage;
  846. else if (voltage < min_uV || voltage > max_uV)
  847. result = regulator_set_voltage(supply, min_uV, max_uV);
  848. else
  849. result = 0;
  850. if (result == 0 && !mmc->regulator_enabled) {
  851. result = regulator_enable(supply);
  852. if (!result)
  853. mmc->regulator_enabled = true;
  854. }
  855. } else if (mmc->regulator_enabled) {
  856. result = regulator_disable(supply);
  857. if (result == 0)
  858. mmc->regulator_enabled = false;
  859. }
  860. if (result)
  861. dev_err(mmc_dev(mmc),
  862. "could not set regulator OCR (%d)\n", result);
  863. return result;
  864. }
  865. EXPORT_SYMBOL(mmc_regulator_set_ocr);
  866. #endif /* CONFIG_REGULATOR */
  867. /*
  868. * Mask off any voltages we don't support and select
  869. * the lowest voltage
  870. */
  871. u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
  872. {
  873. int bit;
  874. ocr &= host->ocr_avail;
  875. bit = ffs(ocr);
  876. if (bit) {
  877. bit -= 1;
  878. ocr &= 3 << bit;
  879. host->ios.vdd = bit;
  880. mmc_set_ios(host);
  881. } else {
  882. pr_warning("%s: host doesn't support card's voltages\n",
  883. mmc_hostname(host));
  884. ocr = 0;
  885. }
  886. return ocr;
  887. }
  888. int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11)
  889. {
  890. struct mmc_command cmd = {0};
  891. int err = 0;
  892. BUG_ON(!host);
  893. /*
  894. * Send CMD11 only if the request is to switch the card to
  895. * 1.8V signalling.
  896. */
  897. if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) {
  898. cmd.opcode = SD_SWITCH_VOLTAGE;
  899. cmd.arg = 0;
  900. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  901. err = mmc_wait_for_cmd(host, &cmd, 0);
  902. if (err)
  903. return err;
  904. if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
  905. return -EIO;
  906. }
  907. host->ios.signal_voltage = signal_voltage;
  908. if (host->ops->start_signal_voltage_switch)
  909. err = host->ops->start_signal_voltage_switch(host, &host->ios);
  910. return err;
  911. }
  912. /*
  913. * Select timing parameters for host.
  914. */
  915. void mmc_set_timing(struct mmc_host *host, unsigned int timing)
  916. {
  917. host->ios.timing = timing;
  918. mmc_set_ios(host);
  919. }
  920. /*
  921. * Select appropriate driver type for host.
  922. */
  923. void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
  924. {
  925. host->ios.drv_type = drv_type;
  926. mmc_set_ios(host);
  927. }
  928. /*
  929. * Apply power to the MMC stack. This is a two-stage process.
  930. * First, we enable power to the card without the clock running.
  931. * We then wait a bit for the power to stabilise. Finally,
  932. * enable the bus drivers and clock to the card.
  933. *
  934. * We must _NOT_ enable the clock prior to power stablising.
  935. *
  936. * If a host does all the power sequencing itself, ignore the
  937. * initial MMC_POWER_UP stage.
  938. */
  939. static void mmc_power_up(struct mmc_host *host)
  940. {
  941. int bit;
  942. /* If ocr is set, we use it */
  943. if (host->ocr)
  944. bit = ffs(host->ocr) - 1;
  945. else
  946. bit = fls(host->ocr_avail) - 1;
  947. host->ios.vdd = bit;
  948. if (mmc_host_is_spi(host)) {
  949. host->ios.chip_select = MMC_CS_HIGH;
  950. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  951. } else {
  952. host->ios.chip_select = MMC_CS_DONTCARE;
  953. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  954. }
  955. host->ios.power_mode = MMC_POWER_UP;
  956. host->ios.bus_width = MMC_BUS_WIDTH_1;
  957. host->ios.timing = MMC_TIMING_LEGACY;
  958. mmc_set_ios(host);
  959. /*
  960. * This delay should be sufficient to allow the power supply
  961. * to reach the minimum voltage.
  962. */
  963. mmc_delay(10);
  964. host->ios.clock = host->f_init;
  965. host->ios.power_mode = MMC_POWER_ON;
  966. mmc_set_ios(host);
  967. /*
  968. * This delay must be at least 74 clock sizes, or 1 ms, or the
  969. * time required to reach a stable voltage.
  970. */
  971. mmc_delay(10);
  972. }
  973. static void mmc_power_off(struct mmc_host *host)
  974. {
  975. host->ios.clock = 0;
  976. host->ios.vdd = 0;
  977. /*
  978. * Reset ocr mask to be the highest possible voltage supported for
  979. * this mmc host. This value will be used at next power up.
  980. */
  981. host->ocr = 1 << (fls(host->ocr_avail) - 1);
  982. if (!mmc_host_is_spi(host)) {
  983. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  984. host->ios.chip_select = MMC_CS_DONTCARE;
  985. }
  986. host->ios.power_mode = MMC_POWER_OFF;
  987. host->ios.bus_width = MMC_BUS_WIDTH_1;
  988. host->ios.timing = MMC_TIMING_LEGACY;
  989. mmc_set_ios(host);
  990. }
  991. /*
  992. * Cleanup when the last reference to the bus operator is dropped.
  993. */
  994. static void __mmc_release_bus(struct mmc_host *host)
  995. {
  996. BUG_ON(!host);
  997. BUG_ON(host->bus_refs);
  998. BUG_ON(!host->bus_dead);
  999. host->bus_ops = NULL;
  1000. }
  1001. /*
  1002. * Increase reference count of bus operator
  1003. */
  1004. static inline void mmc_bus_get(struct mmc_host *host)
  1005. {
  1006. unsigned long flags;
  1007. spin_lock_irqsave(&host->lock, flags);
  1008. host->bus_refs++;
  1009. spin_unlock_irqrestore(&host->lock, flags);
  1010. }
  1011. /*
  1012. * Decrease reference count of bus operator and free it if
  1013. * it is the last reference.
  1014. */
  1015. static inline void mmc_bus_put(struct mmc_host *host)
  1016. {
  1017. unsigned long flags;
  1018. spin_lock_irqsave(&host->lock, flags);
  1019. host->bus_refs--;
  1020. if ((host->bus_refs == 0) && host->bus_ops)
  1021. __mmc_release_bus(host);
  1022. spin_unlock_irqrestore(&host->lock, flags);
  1023. }
  1024. /*
  1025. * Assign a mmc bus handler to a host. Only one bus handler may control a
  1026. * host at any given time.
  1027. */
  1028. void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
  1029. {
  1030. unsigned long flags;
  1031. BUG_ON(!host);
  1032. BUG_ON(!ops);
  1033. WARN_ON(!host->claimed);
  1034. spin_lock_irqsave(&host->lock, flags);
  1035. BUG_ON(host->bus_ops);
  1036. BUG_ON(host->bus_refs);
  1037. host->bus_ops = ops;
  1038. host->bus_refs = 1;
  1039. host->bus_dead = 0;
  1040. spin_unlock_irqrestore(&host->lock, flags);
  1041. }
  1042. /*
  1043. * Remove the current bus handler from a host. Assumes that there are
  1044. * no interesting cards left, so the bus is powered down.
  1045. */
  1046. void mmc_detach_bus(struct mmc_host *host)
  1047. {
  1048. unsigned long flags;
  1049. BUG_ON(!host);
  1050. WARN_ON(!host->claimed);
  1051. WARN_ON(!host->bus_ops);
  1052. spin_lock_irqsave(&host->lock, flags);
  1053. host->bus_dead = 1;
  1054. spin_unlock_irqrestore(&host->lock, flags);
  1055. mmc_power_off(host);
  1056. mmc_bus_put(host);
  1057. }
  1058. /**
  1059. * mmc_detect_change - process change of state on a MMC socket
  1060. * @host: host which changed state.
  1061. * @delay: optional delay to wait before detection (jiffies)
  1062. *
  1063. * MMC drivers should call this when they detect a card has been
  1064. * inserted or removed. The MMC layer will confirm that any
  1065. * present card is still functional, and initialize any newly
  1066. * inserted.
  1067. */
  1068. void mmc_detect_change(struct mmc_host *host, unsigned long delay)
  1069. {
  1070. #ifdef CONFIG_MMC_DEBUG
  1071. unsigned long flags;
  1072. spin_lock_irqsave(&host->lock, flags);
  1073. WARN_ON(host->removed);
  1074. spin_unlock_irqrestore(&host->lock, flags);
  1075. #endif
  1076. mmc_schedule_delayed_work(&host->detect, delay);
  1077. }
  1078. EXPORT_SYMBOL(mmc_detect_change);
  1079. void mmc_init_erase(struct mmc_card *card)
  1080. {
  1081. unsigned int sz;
  1082. if (is_power_of_2(card->erase_size))
  1083. card->erase_shift = ffs(card->erase_size) - 1;
  1084. else
  1085. card->erase_shift = 0;
  1086. /*
  1087. * It is possible to erase an arbitrarily large area of an SD or MMC
  1088. * card. That is not desirable because it can take a long time
  1089. * (minutes) potentially delaying more important I/O, and also the
  1090. * timeout calculations become increasingly hugely over-estimated.
  1091. * Consequently, 'pref_erase' is defined as a guide to limit erases
  1092. * to that size and alignment.
  1093. *
  1094. * For SD cards that define Allocation Unit size, limit erases to one
  1095. * Allocation Unit at a time. For MMC cards that define High Capacity
  1096. * Erase Size, whether it is switched on or not, limit to that size.
  1097. * Otherwise just have a stab at a good value. For modern cards it
  1098. * will end up being 4MiB. Note that if the value is too small, it
  1099. * can end up taking longer to erase.
  1100. */
  1101. if (mmc_card_sd(card) && card->ssr.au) {
  1102. card->pref_erase = card->ssr.au;
  1103. card->erase_shift = ffs(card->ssr.au) - 1;
  1104. } else if (card->ext_csd.hc_erase_size) {
  1105. card->pref_erase = card->ext_csd.hc_erase_size;
  1106. } else {
  1107. sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
  1108. if (sz < 128)
  1109. card->pref_erase = 512 * 1024 / 512;
  1110. else if (sz < 512)
  1111. card->pref_erase = 1024 * 1024 / 512;
  1112. else if (sz < 1024)
  1113. card->pref_erase = 2 * 1024 * 1024 / 512;
  1114. else
  1115. card->pref_erase = 4 * 1024 * 1024 / 512;
  1116. if (card->pref_erase < card->erase_size)
  1117. card->pref_erase = card->erase_size;
  1118. else {
  1119. sz = card->pref_erase % card->erase_size;
  1120. if (sz)
  1121. card->pref_erase += card->erase_size - sz;
  1122. }
  1123. }
  1124. }
  1125. static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
  1126. unsigned int arg, unsigned int qty)
  1127. {
  1128. unsigned int erase_timeout;
  1129. if (card->ext_csd.erase_group_def & 1) {
  1130. /* High Capacity Erase Group Size uses HC timeouts */
  1131. if (arg == MMC_TRIM_ARG)
  1132. erase_timeout = card->ext_csd.trim_timeout;
  1133. else
  1134. erase_timeout = card->ext_csd.hc_erase_timeout;
  1135. } else {
  1136. /* CSD Erase Group Size uses write timeout */
  1137. unsigned int mult = (10 << card->csd.r2w_factor);
  1138. unsigned int timeout_clks = card->csd.tacc_clks * mult;
  1139. unsigned int timeout_us;
  1140. /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
  1141. if (card->csd.tacc_ns < 1000000)
  1142. timeout_us = (card->csd.tacc_ns * mult) / 1000;
  1143. else
  1144. timeout_us = (card->csd.tacc_ns / 1000) * mult;
  1145. /*
  1146. * ios.clock is only a target. The real clock rate might be
  1147. * less but not that much less, so fudge it by multiplying by 2.
  1148. */
  1149. timeout_clks <<= 1;
  1150. timeout_us += (timeout_clks * 1000) /
  1151. (mmc_host_clk_rate(card->host) / 1000);
  1152. erase_timeout = timeout_us / 1000;
  1153. /*
  1154. * Theoretically, the calculation could underflow so round up
  1155. * to 1ms in that case.
  1156. */
  1157. if (!erase_timeout)
  1158. erase_timeout = 1;
  1159. }
  1160. /* Multiplier for secure operations */
  1161. if (arg & MMC_SECURE_ARGS) {
  1162. if (arg == MMC_SECURE_ERASE_ARG)
  1163. erase_timeout *= card->ext_csd.sec_erase_mult;
  1164. else
  1165. erase_timeout *= card->ext_csd.sec_trim_mult;
  1166. }
  1167. erase_timeout *= qty;
  1168. /*
  1169. * Ensure at least a 1 second timeout for SPI as per
  1170. * 'mmc_set_data_timeout()'
  1171. */
  1172. if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
  1173. erase_timeout = 1000;
  1174. return erase_timeout;
  1175. }
  1176. static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
  1177. unsigned int arg,
  1178. unsigned int qty)
  1179. {
  1180. unsigned int erase_timeout;
  1181. if (card->ssr.erase_timeout) {
  1182. /* Erase timeout specified in SD Status Register (SSR) */
  1183. erase_timeout = card->ssr.erase_timeout * qty +
  1184. card->ssr.erase_offset;
  1185. } else {
  1186. /*
  1187. * Erase timeout not specified in SD Status Register (SSR) so
  1188. * use 250ms per write block.
  1189. */
  1190. erase_timeout = 250 * qty;
  1191. }
  1192. /* Must not be less than 1 second */
  1193. if (erase_timeout < 1000)
  1194. erase_timeout = 1000;
  1195. return erase_timeout;
  1196. }
  1197. static unsigned int mmc_erase_timeout(struct mmc_card *card,
  1198. unsigned int arg,
  1199. unsigned int qty)
  1200. {
  1201. if (mmc_card_sd(card))
  1202. return mmc_sd_erase_timeout(card, arg, qty);
  1203. else
  1204. return mmc_mmc_erase_timeout(card, arg, qty);
  1205. }
  1206. static int mmc_do_erase(struct mmc_card *card, unsigned int from,
  1207. unsigned int to, unsigned int arg)
  1208. {
  1209. struct mmc_command cmd = {0};
  1210. unsigned int qty = 0;
  1211. int err;
  1212. /*
  1213. * qty is used to calculate the erase timeout which depends on how many
  1214. * erase groups (or allocation units in SD terminology) are affected.
  1215. * We count erasing part of an erase group as one erase group.
  1216. * For SD, the allocation units are always a power of 2. For MMC, the
  1217. * erase group size is almost certainly also power of 2, but it does not
  1218. * seem to insist on that in the JEDEC standard, so we fall back to
  1219. * division in that case. SD may not specify an allocation unit size,
  1220. * in which case the timeout is based on the number of write blocks.
  1221. *
  1222. * Note that the timeout for secure trim 2 will only be correct if the
  1223. * number of erase groups specified is the same as the total of all
  1224. * preceding secure trim 1 commands. Since the power may have been
  1225. * lost since the secure trim 1 commands occurred, it is generally
  1226. * impossible to calculate the secure trim 2 timeout correctly.
  1227. */
  1228. if (card->erase_shift)
  1229. qty += ((to >> card->erase_shift) -
  1230. (from >> card->erase_shift)) + 1;
  1231. else if (mmc_card_sd(card))
  1232. qty += to - from + 1;
  1233. else
  1234. qty += ((to / card->erase_size) -
  1235. (from / card->erase_size)) + 1;
  1236. if (!mmc_card_blockaddr(card)) {
  1237. from <<= 9;
  1238. to <<= 9;
  1239. }
  1240. if (mmc_card_sd(card))
  1241. cmd.opcode = SD_ERASE_WR_BLK_START;
  1242. else
  1243. cmd.opcode = MMC_ERASE_GROUP_START;
  1244. cmd.arg = from;
  1245. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1246. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1247. if (err) {
  1248. printk(KERN_ERR "mmc_erase: group start error %d, "
  1249. "status %#x\n", err, cmd.resp[0]);
  1250. err = -EINVAL;
  1251. goto out;
  1252. }
  1253. memset(&cmd, 0, sizeof(struct mmc_command));
  1254. if (mmc_card_sd(card))
  1255. cmd.opcode = SD_ERASE_WR_BLK_END;
  1256. else
  1257. cmd.opcode = MMC_ERASE_GROUP_END;
  1258. cmd.arg = to;
  1259. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1260. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1261. if (err) {
  1262. printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
  1263. err, cmd.resp[0]);
  1264. err = -EINVAL;
  1265. goto out;
  1266. }
  1267. memset(&cmd, 0, sizeof(struct mmc_command));
  1268. cmd.opcode = MMC_ERASE;
  1269. cmd.arg = arg;
  1270. cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  1271. cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
  1272. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1273. if (err) {
  1274. printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
  1275. err, cmd.resp[0]);
  1276. err = -EIO;
  1277. goto out;
  1278. }
  1279. if (mmc_host_is_spi(card->host))
  1280. goto out;
  1281. do {
  1282. memset(&cmd, 0, sizeof(struct mmc_command));
  1283. cmd.opcode = MMC_SEND_STATUS;
  1284. cmd.arg = card->rca << 16;
  1285. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1286. /* Do not retry else we can't see errors */
  1287. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1288. if (err || (cmd.resp[0] & 0xFDF92000)) {
  1289. printk(KERN_ERR "error %d requesting status %#x\n",
  1290. err, cmd.resp[0]);
  1291. err = -EIO;
  1292. goto out;
  1293. }
  1294. } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
  1295. R1_CURRENT_STATE(cmd.resp[0]) == 7);
  1296. out:
  1297. return err;
  1298. }
  1299. /**
  1300. * mmc_erase - erase sectors.
  1301. * @card: card to erase
  1302. * @from: first sector to erase
  1303. * @nr: number of sectors to erase
  1304. * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
  1305. *
  1306. * Caller must claim host before calling this function.
  1307. */
  1308. int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
  1309. unsigned int arg)
  1310. {
  1311. unsigned int rem, to = from + nr;
  1312. if (!(card->host->caps & MMC_CAP_ERASE) ||
  1313. !(card->csd.cmdclass & CCC_ERASE))
  1314. return -EOPNOTSUPP;
  1315. if (!card->erase_size)
  1316. return -EOPNOTSUPP;
  1317. if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
  1318. return -EOPNOTSUPP;
  1319. if ((arg & MMC_SECURE_ARGS) &&
  1320. !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
  1321. return -EOPNOTSUPP;
  1322. if ((arg & MMC_TRIM_ARGS) &&
  1323. !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
  1324. return -EOPNOTSUPP;
  1325. if (arg == MMC_SECURE_ERASE_ARG) {
  1326. if (from % card->erase_size || nr % card->erase_size)
  1327. return -EINVAL;
  1328. }
  1329. if (arg == MMC_ERASE_ARG) {
  1330. rem = from % card->erase_size;
  1331. if (rem) {
  1332. rem = card->erase_size - rem;
  1333. from += rem;
  1334. if (nr > rem)
  1335. nr -= rem;
  1336. else
  1337. return 0;
  1338. }
  1339. rem = nr % card->erase_size;
  1340. if (rem)
  1341. nr -= rem;
  1342. }
  1343. if (nr == 0)
  1344. return 0;
  1345. to = from + nr;
  1346. if (to <= from)
  1347. return -EINVAL;
  1348. /* 'from' and 'to' are inclusive */
  1349. to -= 1;
  1350. return mmc_do_erase(card, from, to, arg);
  1351. }
  1352. EXPORT_SYMBOL(mmc_erase);
  1353. int mmc_can_erase(struct mmc_card *card)
  1354. {
  1355. if ((card->host->caps & MMC_CAP_ERASE) &&
  1356. (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
  1357. return 1;
  1358. return 0;
  1359. }
  1360. EXPORT_SYMBOL(mmc_can_erase);
  1361. int mmc_can_trim(struct mmc_card *card)
  1362. {
  1363. if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
  1364. return 1;
  1365. return 0;
  1366. }
  1367. EXPORT_SYMBOL(mmc_can_trim);
  1368. int mmc_can_secure_erase_trim(struct mmc_card *card)
  1369. {
  1370. if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
  1371. return 1;
  1372. return 0;
  1373. }
  1374. EXPORT_SYMBOL(mmc_can_secure_erase_trim);
  1375. int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
  1376. unsigned int nr)
  1377. {
  1378. if (!card->erase_size)
  1379. return 0;
  1380. if (from % card->erase_size || nr % card->erase_size)
  1381. return 0;
  1382. return 1;
  1383. }
  1384. EXPORT_SYMBOL(mmc_erase_group_aligned);
  1385. static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
  1386. unsigned int arg)
  1387. {
  1388. struct mmc_host *host = card->host;
  1389. unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
  1390. unsigned int last_timeout = 0;
  1391. if (card->erase_shift)
  1392. max_qty = UINT_MAX >> card->erase_shift;
  1393. else if (mmc_card_sd(card))
  1394. max_qty = UINT_MAX;
  1395. else
  1396. max_qty = UINT_MAX / card->erase_size;
  1397. /* Find the largest qty with an OK timeout */
  1398. do {
  1399. y = 0;
  1400. for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
  1401. timeout = mmc_erase_timeout(card, arg, qty + x);
  1402. if (timeout > host->max_discard_to)
  1403. break;
  1404. if (timeout < last_timeout)
  1405. break;
  1406. last_timeout = timeout;
  1407. y = x;
  1408. }
  1409. qty += y;
  1410. } while (y);
  1411. if (!qty)
  1412. return 0;
  1413. if (qty == 1)
  1414. return 1;
  1415. /* Convert qty to sectors */
  1416. if (card->erase_shift)
  1417. max_discard = --qty << card->erase_shift;
  1418. else if (mmc_card_sd(card))
  1419. max_discard = qty;
  1420. else
  1421. max_discard = --qty * card->erase_size;
  1422. return max_discard;
  1423. }
  1424. unsigned int mmc_calc_max_discard(struct mmc_card *card)
  1425. {
  1426. struct mmc_host *host = card->host;
  1427. unsigned int max_discard, max_trim;
  1428. if (!host->max_discard_to)
  1429. return UINT_MAX;
  1430. /*
  1431. * Without erase_group_def set, MMC erase timeout depends on clock
  1432. * frequence which can change. In that case, the best choice is
  1433. * just the preferred erase size.
  1434. */
  1435. if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
  1436. return card->pref_erase;
  1437. max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
  1438. if (mmc_can_trim(card)) {
  1439. max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
  1440. if (max_trim < max_discard)
  1441. max_discard = max_trim;
  1442. } else if (max_discard < card->erase_size) {
  1443. max_discard = 0;
  1444. }
  1445. pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
  1446. mmc_hostname(host), max_discard, host->max_discard_to);
  1447. return max_discard;
  1448. }
  1449. EXPORT_SYMBOL(mmc_calc_max_discard);
  1450. int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
  1451. {
  1452. struct mmc_command cmd = {0};
  1453. if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
  1454. return 0;
  1455. cmd.opcode = MMC_SET_BLOCKLEN;
  1456. cmd.arg = blocklen;
  1457. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1458. return mmc_wait_for_cmd(card->host, &cmd, 5);
  1459. }
  1460. EXPORT_SYMBOL(mmc_set_blocklen);
  1461. static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
  1462. {
  1463. host->f_init = freq;
  1464. #ifdef CONFIG_MMC_DEBUG
  1465. pr_info("%s: %s: trying to init card at %u Hz\n",
  1466. mmc_hostname(host), __func__, host->f_init);
  1467. #endif
  1468. mmc_power_up(host);
  1469. /*
  1470. * sdio_reset sends CMD52 to reset card. Since we do not know
  1471. * if the card is being re-initialized, just send it. CMD52
  1472. * should be ignored by SD/eMMC cards.
  1473. */
  1474. sdio_reset(host);
  1475. mmc_go_idle(host);
  1476. mmc_send_if_cond(host, host->ocr_avail);
  1477. /* Order's important: probe SDIO, then SD, then MMC */
  1478. if (!mmc_attach_sdio(host))
  1479. return 0;
  1480. if (!mmc_attach_sd(host))
  1481. return 0;
  1482. if (!mmc_attach_mmc(host))
  1483. return 0;
  1484. mmc_power_off(host);
  1485. return -EIO;
  1486. }
  1487. void mmc_rescan(struct work_struct *work)
  1488. {
  1489. static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
  1490. struct mmc_host *host =
  1491. container_of(work, struct mmc_host, detect.work);
  1492. int i;
  1493. if (host->rescan_disable)
  1494. return;
  1495. mmc_bus_get(host);
  1496. /*
  1497. * if there is a _removable_ card registered, check whether it is
  1498. * still present
  1499. */
  1500. if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
  1501. && !(host->caps & MMC_CAP_NONREMOVABLE))
  1502. host->bus_ops->detect(host);
  1503. /*
  1504. * Let mmc_bus_put() free the bus/bus_ops if we've found that
  1505. * the card is no longer present.
  1506. */
  1507. mmc_bus_put(host);
  1508. mmc_bus_get(host);
  1509. /* if there still is a card present, stop here */
  1510. if (host->bus_ops != NULL) {
  1511. mmc_bus_put(host);
  1512. goto out;
  1513. }
  1514. /*
  1515. * Only we can add a new handler, so it's safe to
  1516. * release the lock here.
  1517. */
  1518. mmc_bus_put(host);
  1519. if (host->ops->get_cd && host->ops->get_cd(host) == 0)
  1520. goto out;
  1521. mmc_claim_host(host);
  1522. for (i = 0; i < ARRAY_SIZE(freqs); i++) {
  1523. if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
  1524. break;
  1525. if (freqs[i] <= host->f_min)
  1526. break;
  1527. }
  1528. mmc_release_host(host);
  1529. out:
  1530. if (host->caps & MMC_CAP_NEEDS_POLL)
  1531. mmc_schedule_delayed_work(&host->detect, HZ);
  1532. }
  1533. void mmc_start_host(struct mmc_host *host)
  1534. {
  1535. mmc_power_off(host);
  1536. mmc_detect_change(host, 0);
  1537. }
  1538. void mmc_stop_host(struct mmc_host *host)
  1539. {
  1540. #ifdef CONFIG_MMC_DEBUG
  1541. unsigned long flags;
  1542. spin_lock_irqsave(&host->lock, flags);
  1543. host->removed = 1;
  1544. spin_unlock_irqrestore(&host->lock, flags);
  1545. #endif
  1546. if (host->caps & MMC_CAP_DISABLE)
  1547. cancel_delayed_work(&host->disable);
  1548. cancel_delayed_work_sync(&host->detect);
  1549. mmc_flush_scheduled_work();
  1550. /* clear pm flags now and let card drivers set them as needed */
  1551. host->pm_flags = 0;
  1552. mmc_bus_get(host);
  1553. if (host->bus_ops && !host->bus_dead) {
  1554. if (host->bus_ops->remove)
  1555. host->bus_ops->remove(host);
  1556. mmc_claim_host(host);
  1557. mmc_detach_bus(host);
  1558. mmc_release_host(host);
  1559. mmc_bus_put(host);
  1560. return;
  1561. }
  1562. mmc_bus_put(host);
  1563. BUG_ON(host->card);
  1564. mmc_power_off(host);
  1565. }
  1566. int mmc_power_save_host(struct mmc_host *host)
  1567. {
  1568. int ret = 0;
  1569. #ifdef CONFIG_MMC_DEBUG
  1570. pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
  1571. #endif
  1572. mmc_bus_get(host);
  1573. if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
  1574. mmc_bus_put(host);
  1575. return -EINVAL;
  1576. }
  1577. if (host->bus_ops->power_save)
  1578. ret = host->bus_ops->power_save(host);
  1579. mmc_bus_put(host);
  1580. mmc_power_off(host);
  1581. return ret;
  1582. }
  1583. EXPORT_SYMBOL(mmc_power_save_host);
  1584. int mmc_power_restore_host(struct mmc_host *host)
  1585. {
  1586. int ret;
  1587. #ifdef CONFIG_MMC_DEBUG
  1588. pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
  1589. #endif
  1590. mmc_bus_get(host);
  1591. if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
  1592. mmc_bus_put(host);
  1593. return -EINVAL;
  1594. }
  1595. mmc_power_up(host);
  1596. ret = host->bus_ops->power_restore(host);
  1597. mmc_bus_put(host);
  1598. return ret;
  1599. }
  1600. EXPORT_SYMBOL(mmc_power_restore_host);
  1601. int mmc_card_awake(struct mmc_host *host)
  1602. {
  1603. int err = -ENOSYS;
  1604. mmc_bus_get(host);
  1605. if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
  1606. err = host->bus_ops->awake(host);
  1607. mmc_bus_put(host);
  1608. return err;
  1609. }
  1610. EXPORT_SYMBOL(mmc_card_awake);
  1611. int mmc_card_sleep(struct mmc_host *host)
  1612. {
  1613. int err = -ENOSYS;
  1614. mmc_bus_get(host);
  1615. if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
  1616. err = host->bus_ops->sleep(host);
  1617. mmc_bus_put(host);
  1618. return err;
  1619. }
  1620. EXPORT_SYMBOL(mmc_card_sleep);
  1621. int mmc_card_can_sleep(struct mmc_host *host)
  1622. {
  1623. struct mmc_card *card = host->card;
  1624. if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
  1625. return 1;
  1626. return 0;
  1627. }
  1628. EXPORT_SYMBOL(mmc_card_can_sleep);
  1629. #ifdef CONFIG_PM
  1630. /**
  1631. * mmc_suspend_host - suspend a host
  1632. * @host: mmc host
  1633. */
  1634. int mmc_suspend_host(struct mmc_host *host)
  1635. {
  1636. int err = 0;
  1637. if (host->caps & MMC_CAP_DISABLE)
  1638. cancel_delayed_work(&host->disable);
  1639. cancel_delayed_work(&host->detect);
  1640. mmc_flush_scheduled_work();
  1641. mmc_bus_get(host);
  1642. if (host->bus_ops && !host->bus_dead) {
  1643. if (host->bus_ops->suspend)
  1644. err = host->bus_ops->suspend(host);
  1645. if (err == -ENOSYS || !host->bus_ops->resume) {
  1646. /*
  1647. * We simply "remove" the card in this case.
  1648. * It will be redetected on resume.
  1649. */
  1650. if (host->bus_ops->remove)
  1651. host->bus_ops->remove(host);
  1652. mmc_claim_host(host);
  1653. mmc_detach_bus(host);
  1654. mmc_release_host(host);
  1655. host->pm_flags = 0;
  1656. err = 0;
  1657. }
  1658. }
  1659. mmc_bus_put(host);
  1660. if (!err && !mmc_card_keep_power(host))
  1661. mmc_power_off(host);
  1662. return err;
  1663. }
  1664. EXPORT_SYMBOL(mmc_suspend_host);
  1665. /**
  1666. * mmc_resume_host - resume a previously suspended host
  1667. * @host: mmc host
  1668. */
  1669. int mmc_resume_host(struct mmc_host *host)
  1670. {
  1671. int err = 0;
  1672. mmc_bus_get(host);
  1673. if (host->bus_ops && !host->bus_dead) {
  1674. if (!mmc_card_keep_power(host)) {
  1675. mmc_power_up(host);
  1676. mmc_select_voltage(host, host->ocr);
  1677. /*
  1678. * Tell runtime PM core we just powered up the card,
  1679. * since it still believes the card is powered off.
  1680. * Note that currently runtime PM is only enabled
  1681. * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
  1682. */
  1683. if (mmc_card_sdio(host->card) &&
  1684. (host->caps & MMC_CAP_POWER_OFF_CARD)) {
  1685. pm_runtime_disable(&host->card->dev);
  1686. pm_runtime_set_active(&host->card->dev);
  1687. pm_runtime_enable(&host->card->dev);
  1688. }
  1689. }
  1690. BUG_ON(!host->bus_ops->resume);
  1691. err = host->bus_ops->resume(host);
  1692. if (err) {
  1693. printk(KERN_WARNING "%s: error %d during resume "
  1694. "(card was removed?)\n",
  1695. mmc_hostname(host), err);
  1696. err = 0;
  1697. }
  1698. }
  1699. host->pm_flags &= ~MMC_PM_KEEP_POWER;
  1700. mmc_bus_put(host);
  1701. return err;
  1702. }
  1703. EXPORT_SYMBOL(mmc_resume_host);
  1704. /* Do the card removal on suspend if card is assumed removeable
  1705. * Do that in pm notifier while userspace isn't yet frozen, so we will be able
  1706. to sync the card.
  1707. */
  1708. int mmc_pm_notify(struct notifier_block *notify_block,
  1709. unsigned long mode, void *unused)
  1710. {
  1711. struct mmc_host *host = container_of(
  1712. notify_block, struct mmc_host, pm_notify);
  1713. unsigned long flags;
  1714. switch (mode) {
  1715. case PM_HIBERNATION_PREPARE:
  1716. case PM_SUSPEND_PREPARE:
  1717. spin_lock_irqsave(&host->lock, flags);
  1718. host->rescan_disable = 1;
  1719. spin_unlock_irqrestore(&host->lock, flags);
  1720. cancel_delayed_work_sync(&host->detect);
  1721. if (!host->bus_ops || host->bus_ops->suspend)
  1722. break;
  1723. mmc_claim_host(host);
  1724. if (host->bus_ops->remove)
  1725. host->bus_ops->remove(host);
  1726. mmc_detach_bus(host);
  1727. mmc_release_host(host);
  1728. host->pm_flags = 0;
  1729. break;
  1730. case PM_POST_SUSPEND:
  1731. case PM_POST_HIBERNATION:
  1732. case PM_POST_RESTORE:
  1733. spin_lock_irqsave(&host->lock, flags);
  1734. host->rescan_disable = 0;
  1735. spin_unlock_irqrestore(&host->lock, flags);
  1736. mmc_detect_change(host, 0);
  1737. }
  1738. return 0;
  1739. }
  1740. #endif
  1741. static int __init mmc_init(void)
  1742. {
  1743. int ret;
  1744. workqueue = alloc_ordered_workqueue("kmmcd", 0);
  1745. if (!workqueue)
  1746. return -ENOMEM;
  1747. ret = mmc_register_bus();
  1748. if (ret)
  1749. goto destroy_workqueue;
  1750. ret = mmc_register_host_class();
  1751. if (ret)
  1752. goto unregister_bus;
  1753. ret = sdio_register_bus();
  1754. if (ret)
  1755. goto unregister_host_class;
  1756. return 0;
  1757. unregister_host_class:
  1758. mmc_unregister_host_class();
  1759. unregister_bus:
  1760. mmc_unregister_bus();
  1761. destroy_workqueue:
  1762. destroy_workqueue(workqueue);
  1763. return ret;
  1764. }
  1765. static void __exit mmc_exit(void)
  1766. {
  1767. sdio_unregister_bus();
  1768. mmc_unregister_host_class();
  1769. mmc_unregister_bus();
  1770. destroy_workqueue(workqueue);
  1771. }
  1772. subsys_initcall(mmc_init);
  1773. module_exit(mmc_exit);
  1774. MODULE_LICENSE("GPL");