core.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126
  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_release(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_hold(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. mmc_host_clk_hold(host);
  634. host->ios.chip_select = mode;
  635. mmc_set_ios(host);
  636. mmc_host_clk_release(host);
  637. }
  638. /*
  639. * Sets the host clock to the highest possible frequency that
  640. * is below "hz".
  641. */
  642. static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
  643. {
  644. WARN_ON(hz < host->f_min);
  645. if (hz > host->f_max)
  646. hz = host->f_max;
  647. host->ios.clock = hz;
  648. mmc_set_ios(host);
  649. }
  650. void mmc_set_clock(struct mmc_host *host, unsigned int hz)
  651. {
  652. mmc_host_clk_hold(host);
  653. __mmc_set_clock(host, hz);
  654. mmc_host_clk_release(host);
  655. }
  656. #ifdef CONFIG_MMC_CLKGATE
  657. /*
  658. * This gates the clock by setting it to 0 Hz.
  659. */
  660. void mmc_gate_clock(struct mmc_host *host)
  661. {
  662. unsigned long flags;
  663. spin_lock_irqsave(&host->clk_lock, flags);
  664. host->clk_old = host->ios.clock;
  665. host->ios.clock = 0;
  666. host->clk_gated = true;
  667. spin_unlock_irqrestore(&host->clk_lock, flags);
  668. mmc_set_ios(host);
  669. }
  670. /*
  671. * This restores the clock from gating by using the cached
  672. * clock value.
  673. */
  674. void mmc_ungate_clock(struct mmc_host *host)
  675. {
  676. /*
  677. * We should previously have gated the clock, so the clock shall
  678. * be 0 here! The clock may however be 0 during initialization,
  679. * when some request operations are performed before setting
  680. * the frequency. When ungate is requested in that situation
  681. * we just ignore the call.
  682. */
  683. if (host->clk_old) {
  684. BUG_ON(host->ios.clock);
  685. /* This call will also set host->clk_gated to false */
  686. __mmc_set_clock(host, host->clk_old);
  687. }
  688. }
  689. void mmc_set_ungated(struct mmc_host *host)
  690. {
  691. unsigned long flags;
  692. /*
  693. * We've been given a new frequency while the clock is gated,
  694. * so make sure we regard this as ungating it.
  695. */
  696. spin_lock_irqsave(&host->clk_lock, flags);
  697. host->clk_gated = false;
  698. spin_unlock_irqrestore(&host->clk_lock, flags);
  699. }
  700. #else
  701. void mmc_set_ungated(struct mmc_host *host)
  702. {
  703. }
  704. #endif
  705. /*
  706. * Change the bus mode (open drain/push-pull) of a host.
  707. */
  708. void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
  709. {
  710. mmc_host_clk_hold(host);
  711. host->ios.bus_mode = mode;
  712. mmc_set_ios(host);
  713. mmc_host_clk_release(host);
  714. }
  715. /*
  716. * Change data bus width of a host.
  717. */
  718. void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
  719. {
  720. mmc_host_clk_hold(host);
  721. host->ios.bus_width = width;
  722. mmc_set_ios(host);
  723. mmc_host_clk_release(host);
  724. }
  725. /**
  726. * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
  727. * @vdd: voltage (mV)
  728. * @low_bits: prefer low bits in boundary cases
  729. *
  730. * This function returns the OCR bit number according to the provided @vdd
  731. * value. If conversion is not possible a negative errno value returned.
  732. *
  733. * Depending on the @low_bits flag the function prefers low or high OCR bits
  734. * on boundary voltages. For example,
  735. * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
  736. * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
  737. *
  738. * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
  739. */
  740. static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
  741. {
  742. const int max_bit = ilog2(MMC_VDD_35_36);
  743. int bit;
  744. if (vdd < 1650 || vdd > 3600)
  745. return -EINVAL;
  746. if (vdd >= 1650 && vdd <= 1950)
  747. return ilog2(MMC_VDD_165_195);
  748. if (low_bits)
  749. vdd -= 1;
  750. /* Base 2000 mV, step 100 mV, bit's base 8. */
  751. bit = (vdd - 2000) / 100 + 8;
  752. if (bit > max_bit)
  753. return max_bit;
  754. return bit;
  755. }
  756. /**
  757. * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
  758. * @vdd_min: minimum voltage value (mV)
  759. * @vdd_max: maximum voltage value (mV)
  760. *
  761. * This function returns the OCR mask bits according to the provided @vdd_min
  762. * and @vdd_max values. If conversion is not possible the function returns 0.
  763. *
  764. * Notes wrt boundary cases:
  765. * This function sets the OCR bits for all boundary voltages, for example
  766. * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
  767. * MMC_VDD_34_35 mask.
  768. */
  769. u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
  770. {
  771. u32 mask = 0;
  772. if (vdd_max < vdd_min)
  773. return 0;
  774. /* Prefer high bits for the boundary vdd_max values. */
  775. vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
  776. if (vdd_max < 0)
  777. return 0;
  778. /* Prefer low bits for the boundary vdd_min values. */
  779. vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
  780. if (vdd_min < 0)
  781. return 0;
  782. /* Fill the mask, from max bit to min bit. */
  783. while (vdd_max >= vdd_min)
  784. mask |= 1 << vdd_max--;
  785. return mask;
  786. }
  787. EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
  788. #ifdef CONFIG_REGULATOR
  789. /**
  790. * mmc_regulator_get_ocrmask - return mask of supported voltages
  791. * @supply: regulator to use
  792. *
  793. * This returns either a negative errno, or a mask of voltages that
  794. * can be provided to MMC/SD/SDIO devices using the specified voltage
  795. * regulator. This would normally be called before registering the
  796. * MMC host adapter.
  797. */
  798. int mmc_regulator_get_ocrmask(struct regulator *supply)
  799. {
  800. int result = 0;
  801. int count;
  802. int i;
  803. count = regulator_count_voltages(supply);
  804. if (count < 0)
  805. return count;
  806. for (i = 0; i < count; i++) {
  807. int vdd_uV;
  808. int vdd_mV;
  809. vdd_uV = regulator_list_voltage(supply, i);
  810. if (vdd_uV <= 0)
  811. continue;
  812. vdd_mV = vdd_uV / 1000;
  813. result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
  814. }
  815. return result;
  816. }
  817. EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
  818. /**
  819. * mmc_regulator_set_ocr - set regulator to match host->ios voltage
  820. * @mmc: the host to regulate
  821. * @supply: regulator to use
  822. * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
  823. *
  824. * Returns zero on success, else negative errno.
  825. *
  826. * MMC host drivers may use this to enable or disable a regulator using
  827. * a particular supply voltage. This would normally be called from the
  828. * set_ios() method.
  829. */
  830. int mmc_regulator_set_ocr(struct mmc_host *mmc,
  831. struct regulator *supply,
  832. unsigned short vdd_bit)
  833. {
  834. int result = 0;
  835. int min_uV, max_uV;
  836. if (vdd_bit) {
  837. int tmp;
  838. int voltage;
  839. /* REVISIT mmc_vddrange_to_ocrmask() may have set some
  840. * bits this regulator doesn't quite support ... don't
  841. * be too picky, most cards and regulators are OK with
  842. * a 0.1V range goof (it's a small error percentage).
  843. */
  844. tmp = vdd_bit - ilog2(MMC_VDD_165_195);
  845. if (tmp == 0) {
  846. min_uV = 1650 * 1000;
  847. max_uV = 1950 * 1000;
  848. } else {
  849. min_uV = 1900 * 1000 + tmp * 100 * 1000;
  850. max_uV = min_uV + 100 * 1000;
  851. }
  852. /* avoid needless changes to this voltage; the regulator
  853. * might not allow this operation
  854. */
  855. voltage = regulator_get_voltage(supply);
  856. if (voltage < 0)
  857. result = voltage;
  858. else if (voltage < min_uV || voltage > max_uV)
  859. result = regulator_set_voltage(supply, min_uV, max_uV);
  860. else
  861. result = 0;
  862. if (result == 0 && !mmc->regulator_enabled) {
  863. result = regulator_enable(supply);
  864. if (!result)
  865. mmc->regulator_enabled = true;
  866. }
  867. } else if (mmc->regulator_enabled) {
  868. result = regulator_disable(supply);
  869. if (result == 0)
  870. mmc->regulator_enabled = false;
  871. }
  872. if (result)
  873. dev_err(mmc_dev(mmc),
  874. "could not set regulator OCR (%d)\n", result);
  875. return result;
  876. }
  877. EXPORT_SYMBOL(mmc_regulator_set_ocr);
  878. #endif /* CONFIG_REGULATOR */
  879. /*
  880. * Mask off any voltages we don't support and select
  881. * the lowest voltage
  882. */
  883. u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
  884. {
  885. int bit;
  886. ocr &= host->ocr_avail;
  887. bit = ffs(ocr);
  888. if (bit) {
  889. bit -= 1;
  890. ocr &= 3 << bit;
  891. mmc_host_clk_hold(host);
  892. host->ios.vdd = bit;
  893. mmc_set_ios(host);
  894. mmc_host_clk_release(host);
  895. } else {
  896. pr_warning("%s: host doesn't support card's voltages\n",
  897. mmc_hostname(host));
  898. ocr = 0;
  899. }
  900. return ocr;
  901. }
  902. int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11)
  903. {
  904. struct mmc_command cmd = {0};
  905. int err = 0;
  906. BUG_ON(!host);
  907. /*
  908. * Send CMD11 only if the request is to switch the card to
  909. * 1.8V signalling.
  910. */
  911. if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) {
  912. cmd.opcode = SD_SWITCH_VOLTAGE;
  913. cmd.arg = 0;
  914. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  915. err = mmc_wait_for_cmd(host, &cmd, 0);
  916. if (err)
  917. return err;
  918. if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
  919. return -EIO;
  920. }
  921. host->ios.signal_voltage = signal_voltage;
  922. if (host->ops->start_signal_voltage_switch)
  923. err = host->ops->start_signal_voltage_switch(host, &host->ios);
  924. return err;
  925. }
  926. /*
  927. * Select timing parameters for host.
  928. */
  929. void mmc_set_timing(struct mmc_host *host, unsigned int timing)
  930. {
  931. mmc_host_clk_hold(host);
  932. host->ios.timing = timing;
  933. mmc_set_ios(host);
  934. mmc_host_clk_release(host);
  935. }
  936. /*
  937. * Select appropriate driver type for host.
  938. */
  939. void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
  940. {
  941. mmc_host_clk_hold(host);
  942. host->ios.drv_type = drv_type;
  943. mmc_set_ios(host);
  944. mmc_host_clk_release(host);
  945. }
  946. /*
  947. * Apply power to the MMC stack. This is a two-stage process.
  948. * First, we enable power to the card without the clock running.
  949. * We then wait a bit for the power to stabilise. Finally,
  950. * enable the bus drivers and clock to the card.
  951. *
  952. * We must _NOT_ enable the clock prior to power stablising.
  953. *
  954. * If a host does all the power sequencing itself, ignore the
  955. * initial MMC_POWER_UP stage.
  956. */
  957. static void mmc_power_up(struct mmc_host *host)
  958. {
  959. int bit;
  960. mmc_host_clk_hold(host);
  961. /* If ocr is set, we use it */
  962. if (host->ocr)
  963. bit = ffs(host->ocr) - 1;
  964. else
  965. bit = fls(host->ocr_avail) - 1;
  966. host->ios.vdd = bit;
  967. if (mmc_host_is_spi(host)) {
  968. host->ios.chip_select = MMC_CS_HIGH;
  969. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  970. } else {
  971. host->ios.chip_select = MMC_CS_DONTCARE;
  972. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  973. }
  974. host->ios.power_mode = MMC_POWER_UP;
  975. host->ios.bus_width = MMC_BUS_WIDTH_1;
  976. host->ios.timing = MMC_TIMING_LEGACY;
  977. mmc_set_ios(host);
  978. /*
  979. * This delay should be sufficient to allow the power supply
  980. * to reach the minimum voltage.
  981. */
  982. mmc_delay(10);
  983. host->ios.clock = host->f_init;
  984. host->ios.power_mode = MMC_POWER_ON;
  985. mmc_set_ios(host);
  986. /*
  987. * This delay must be at least 74 clock sizes, or 1 ms, or the
  988. * time required to reach a stable voltage.
  989. */
  990. mmc_delay(10);
  991. mmc_host_clk_release(host);
  992. }
  993. static void mmc_power_off(struct mmc_host *host)
  994. {
  995. mmc_host_clk_hold(host);
  996. host->ios.clock = 0;
  997. host->ios.vdd = 0;
  998. /*
  999. * Reset ocr mask to be the highest possible voltage supported for
  1000. * this mmc host. This value will be used at next power up.
  1001. */
  1002. host->ocr = 1 << (fls(host->ocr_avail) - 1);
  1003. if (!mmc_host_is_spi(host)) {
  1004. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  1005. host->ios.chip_select = MMC_CS_DONTCARE;
  1006. }
  1007. host->ios.power_mode = MMC_POWER_OFF;
  1008. host->ios.bus_width = MMC_BUS_WIDTH_1;
  1009. host->ios.timing = MMC_TIMING_LEGACY;
  1010. mmc_set_ios(host);
  1011. mmc_host_clk_release(host);
  1012. }
  1013. /*
  1014. * Cleanup when the last reference to the bus operator is dropped.
  1015. */
  1016. static void __mmc_release_bus(struct mmc_host *host)
  1017. {
  1018. BUG_ON(!host);
  1019. BUG_ON(host->bus_refs);
  1020. BUG_ON(!host->bus_dead);
  1021. host->bus_ops = NULL;
  1022. }
  1023. /*
  1024. * Increase reference count of bus operator
  1025. */
  1026. static inline void mmc_bus_get(struct mmc_host *host)
  1027. {
  1028. unsigned long flags;
  1029. spin_lock_irqsave(&host->lock, flags);
  1030. host->bus_refs++;
  1031. spin_unlock_irqrestore(&host->lock, flags);
  1032. }
  1033. /*
  1034. * Decrease reference count of bus operator and free it if
  1035. * it is the last reference.
  1036. */
  1037. static inline void mmc_bus_put(struct mmc_host *host)
  1038. {
  1039. unsigned long flags;
  1040. spin_lock_irqsave(&host->lock, flags);
  1041. host->bus_refs--;
  1042. if ((host->bus_refs == 0) && host->bus_ops)
  1043. __mmc_release_bus(host);
  1044. spin_unlock_irqrestore(&host->lock, flags);
  1045. }
  1046. /*
  1047. * Assign a mmc bus handler to a host. Only one bus handler may control a
  1048. * host at any given time.
  1049. */
  1050. void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
  1051. {
  1052. unsigned long flags;
  1053. BUG_ON(!host);
  1054. BUG_ON(!ops);
  1055. WARN_ON(!host->claimed);
  1056. spin_lock_irqsave(&host->lock, flags);
  1057. BUG_ON(host->bus_ops);
  1058. BUG_ON(host->bus_refs);
  1059. host->bus_ops = ops;
  1060. host->bus_refs = 1;
  1061. host->bus_dead = 0;
  1062. spin_unlock_irqrestore(&host->lock, flags);
  1063. }
  1064. /*
  1065. * Remove the current bus handler from a host. Assumes that there are
  1066. * no interesting cards left, so the bus is powered down.
  1067. */
  1068. void mmc_detach_bus(struct mmc_host *host)
  1069. {
  1070. unsigned long flags;
  1071. BUG_ON(!host);
  1072. WARN_ON(!host->claimed);
  1073. WARN_ON(!host->bus_ops);
  1074. spin_lock_irqsave(&host->lock, flags);
  1075. host->bus_dead = 1;
  1076. spin_unlock_irqrestore(&host->lock, flags);
  1077. mmc_power_off(host);
  1078. mmc_bus_put(host);
  1079. }
  1080. /**
  1081. * mmc_detect_change - process change of state on a MMC socket
  1082. * @host: host which changed state.
  1083. * @delay: optional delay to wait before detection (jiffies)
  1084. *
  1085. * MMC drivers should call this when they detect a card has been
  1086. * inserted or removed. The MMC layer will confirm that any
  1087. * present card is still functional, and initialize any newly
  1088. * inserted.
  1089. */
  1090. void mmc_detect_change(struct mmc_host *host, unsigned long delay)
  1091. {
  1092. #ifdef CONFIG_MMC_DEBUG
  1093. unsigned long flags;
  1094. spin_lock_irqsave(&host->lock, flags);
  1095. WARN_ON(host->removed);
  1096. spin_unlock_irqrestore(&host->lock, flags);
  1097. #endif
  1098. mmc_schedule_delayed_work(&host->detect, delay);
  1099. }
  1100. EXPORT_SYMBOL(mmc_detect_change);
  1101. void mmc_init_erase(struct mmc_card *card)
  1102. {
  1103. unsigned int sz;
  1104. if (is_power_of_2(card->erase_size))
  1105. card->erase_shift = ffs(card->erase_size) - 1;
  1106. else
  1107. card->erase_shift = 0;
  1108. /*
  1109. * It is possible to erase an arbitrarily large area of an SD or MMC
  1110. * card. That is not desirable because it can take a long time
  1111. * (minutes) potentially delaying more important I/O, and also the
  1112. * timeout calculations become increasingly hugely over-estimated.
  1113. * Consequently, 'pref_erase' is defined as a guide to limit erases
  1114. * to that size and alignment.
  1115. *
  1116. * For SD cards that define Allocation Unit size, limit erases to one
  1117. * Allocation Unit at a time. For MMC cards that define High Capacity
  1118. * Erase Size, whether it is switched on or not, limit to that size.
  1119. * Otherwise just have a stab at a good value. For modern cards it
  1120. * will end up being 4MiB. Note that if the value is too small, it
  1121. * can end up taking longer to erase.
  1122. */
  1123. if (mmc_card_sd(card) && card->ssr.au) {
  1124. card->pref_erase = card->ssr.au;
  1125. card->erase_shift = ffs(card->ssr.au) - 1;
  1126. } else if (card->ext_csd.hc_erase_size) {
  1127. card->pref_erase = card->ext_csd.hc_erase_size;
  1128. } else {
  1129. sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
  1130. if (sz < 128)
  1131. card->pref_erase = 512 * 1024 / 512;
  1132. else if (sz < 512)
  1133. card->pref_erase = 1024 * 1024 / 512;
  1134. else if (sz < 1024)
  1135. card->pref_erase = 2 * 1024 * 1024 / 512;
  1136. else
  1137. card->pref_erase = 4 * 1024 * 1024 / 512;
  1138. if (card->pref_erase < card->erase_size)
  1139. card->pref_erase = card->erase_size;
  1140. else {
  1141. sz = card->pref_erase % card->erase_size;
  1142. if (sz)
  1143. card->pref_erase += card->erase_size - sz;
  1144. }
  1145. }
  1146. }
  1147. static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
  1148. unsigned int arg, unsigned int qty)
  1149. {
  1150. unsigned int erase_timeout;
  1151. if (card->ext_csd.erase_group_def & 1) {
  1152. /* High Capacity Erase Group Size uses HC timeouts */
  1153. if (arg == MMC_TRIM_ARG)
  1154. erase_timeout = card->ext_csd.trim_timeout;
  1155. else
  1156. erase_timeout = card->ext_csd.hc_erase_timeout;
  1157. } else {
  1158. /* CSD Erase Group Size uses write timeout */
  1159. unsigned int mult = (10 << card->csd.r2w_factor);
  1160. unsigned int timeout_clks = card->csd.tacc_clks * mult;
  1161. unsigned int timeout_us;
  1162. /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
  1163. if (card->csd.tacc_ns < 1000000)
  1164. timeout_us = (card->csd.tacc_ns * mult) / 1000;
  1165. else
  1166. timeout_us = (card->csd.tacc_ns / 1000) * mult;
  1167. /*
  1168. * ios.clock is only a target. The real clock rate might be
  1169. * less but not that much less, so fudge it by multiplying by 2.
  1170. */
  1171. timeout_clks <<= 1;
  1172. timeout_us += (timeout_clks * 1000) /
  1173. (mmc_host_clk_rate(card->host) / 1000);
  1174. erase_timeout = timeout_us / 1000;
  1175. /*
  1176. * Theoretically, the calculation could underflow so round up
  1177. * to 1ms in that case.
  1178. */
  1179. if (!erase_timeout)
  1180. erase_timeout = 1;
  1181. }
  1182. /* Multiplier for secure operations */
  1183. if (arg & MMC_SECURE_ARGS) {
  1184. if (arg == MMC_SECURE_ERASE_ARG)
  1185. erase_timeout *= card->ext_csd.sec_erase_mult;
  1186. else
  1187. erase_timeout *= card->ext_csd.sec_trim_mult;
  1188. }
  1189. erase_timeout *= qty;
  1190. /*
  1191. * Ensure at least a 1 second timeout for SPI as per
  1192. * 'mmc_set_data_timeout()'
  1193. */
  1194. if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
  1195. erase_timeout = 1000;
  1196. return erase_timeout;
  1197. }
  1198. static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
  1199. unsigned int arg,
  1200. unsigned int qty)
  1201. {
  1202. unsigned int erase_timeout;
  1203. if (card->ssr.erase_timeout) {
  1204. /* Erase timeout specified in SD Status Register (SSR) */
  1205. erase_timeout = card->ssr.erase_timeout * qty +
  1206. card->ssr.erase_offset;
  1207. } else {
  1208. /*
  1209. * Erase timeout not specified in SD Status Register (SSR) so
  1210. * use 250ms per write block.
  1211. */
  1212. erase_timeout = 250 * qty;
  1213. }
  1214. /* Must not be less than 1 second */
  1215. if (erase_timeout < 1000)
  1216. erase_timeout = 1000;
  1217. return erase_timeout;
  1218. }
  1219. static unsigned int mmc_erase_timeout(struct mmc_card *card,
  1220. unsigned int arg,
  1221. unsigned int qty)
  1222. {
  1223. if (mmc_card_sd(card))
  1224. return mmc_sd_erase_timeout(card, arg, qty);
  1225. else
  1226. return mmc_mmc_erase_timeout(card, arg, qty);
  1227. }
  1228. static int mmc_do_erase(struct mmc_card *card, unsigned int from,
  1229. unsigned int to, unsigned int arg)
  1230. {
  1231. struct mmc_command cmd = {0};
  1232. unsigned int qty = 0;
  1233. int err;
  1234. /*
  1235. * qty is used to calculate the erase timeout which depends on how many
  1236. * erase groups (or allocation units in SD terminology) are affected.
  1237. * We count erasing part of an erase group as one erase group.
  1238. * For SD, the allocation units are always a power of 2. For MMC, the
  1239. * erase group size is almost certainly also power of 2, but it does not
  1240. * seem to insist on that in the JEDEC standard, so we fall back to
  1241. * division in that case. SD may not specify an allocation unit size,
  1242. * in which case the timeout is based on the number of write blocks.
  1243. *
  1244. * Note that the timeout for secure trim 2 will only be correct if the
  1245. * number of erase groups specified is the same as the total of all
  1246. * preceding secure trim 1 commands. Since the power may have been
  1247. * lost since the secure trim 1 commands occurred, it is generally
  1248. * impossible to calculate the secure trim 2 timeout correctly.
  1249. */
  1250. if (card->erase_shift)
  1251. qty += ((to >> card->erase_shift) -
  1252. (from >> card->erase_shift)) + 1;
  1253. else if (mmc_card_sd(card))
  1254. qty += to - from + 1;
  1255. else
  1256. qty += ((to / card->erase_size) -
  1257. (from / card->erase_size)) + 1;
  1258. if (!mmc_card_blockaddr(card)) {
  1259. from <<= 9;
  1260. to <<= 9;
  1261. }
  1262. if (mmc_card_sd(card))
  1263. cmd.opcode = SD_ERASE_WR_BLK_START;
  1264. else
  1265. cmd.opcode = MMC_ERASE_GROUP_START;
  1266. cmd.arg = from;
  1267. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1268. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1269. if (err) {
  1270. printk(KERN_ERR "mmc_erase: group start error %d, "
  1271. "status %#x\n", err, cmd.resp[0]);
  1272. err = -EINVAL;
  1273. goto out;
  1274. }
  1275. memset(&cmd, 0, sizeof(struct mmc_command));
  1276. if (mmc_card_sd(card))
  1277. cmd.opcode = SD_ERASE_WR_BLK_END;
  1278. else
  1279. cmd.opcode = MMC_ERASE_GROUP_END;
  1280. cmd.arg = to;
  1281. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1282. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1283. if (err) {
  1284. printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
  1285. err, cmd.resp[0]);
  1286. err = -EINVAL;
  1287. goto out;
  1288. }
  1289. memset(&cmd, 0, sizeof(struct mmc_command));
  1290. cmd.opcode = MMC_ERASE;
  1291. cmd.arg = arg;
  1292. cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  1293. cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
  1294. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1295. if (err) {
  1296. printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
  1297. err, cmd.resp[0]);
  1298. err = -EIO;
  1299. goto out;
  1300. }
  1301. if (mmc_host_is_spi(card->host))
  1302. goto out;
  1303. do {
  1304. memset(&cmd, 0, sizeof(struct mmc_command));
  1305. cmd.opcode = MMC_SEND_STATUS;
  1306. cmd.arg = card->rca << 16;
  1307. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1308. /* Do not retry else we can't see errors */
  1309. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1310. if (err || (cmd.resp[0] & 0xFDF92000)) {
  1311. printk(KERN_ERR "error %d requesting status %#x\n",
  1312. err, cmd.resp[0]);
  1313. err = -EIO;
  1314. goto out;
  1315. }
  1316. } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
  1317. R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG);
  1318. out:
  1319. return err;
  1320. }
  1321. /**
  1322. * mmc_erase - erase sectors.
  1323. * @card: card to erase
  1324. * @from: first sector to erase
  1325. * @nr: number of sectors to erase
  1326. * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
  1327. *
  1328. * Caller must claim host before calling this function.
  1329. */
  1330. int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
  1331. unsigned int arg)
  1332. {
  1333. unsigned int rem, to = from + nr;
  1334. if (!(card->host->caps & MMC_CAP_ERASE) ||
  1335. !(card->csd.cmdclass & CCC_ERASE))
  1336. return -EOPNOTSUPP;
  1337. if (!card->erase_size)
  1338. return -EOPNOTSUPP;
  1339. if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
  1340. return -EOPNOTSUPP;
  1341. if ((arg & MMC_SECURE_ARGS) &&
  1342. !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
  1343. return -EOPNOTSUPP;
  1344. if ((arg & MMC_TRIM_ARGS) &&
  1345. !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
  1346. return -EOPNOTSUPP;
  1347. if (arg == MMC_SECURE_ERASE_ARG) {
  1348. if (from % card->erase_size || nr % card->erase_size)
  1349. return -EINVAL;
  1350. }
  1351. if (arg == MMC_ERASE_ARG) {
  1352. rem = from % card->erase_size;
  1353. if (rem) {
  1354. rem = card->erase_size - rem;
  1355. from += rem;
  1356. if (nr > rem)
  1357. nr -= rem;
  1358. else
  1359. return 0;
  1360. }
  1361. rem = nr % card->erase_size;
  1362. if (rem)
  1363. nr -= rem;
  1364. }
  1365. if (nr == 0)
  1366. return 0;
  1367. to = from + nr;
  1368. if (to <= from)
  1369. return -EINVAL;
  1370. /* 'from' and 'to' are inclusive */
  1371. to -= 1;
  1372. return mmc_do_erase(card, from, to, arg);
  1373. }
  1374. EXPORT_SYMBOL(mmc_erase);
  1375. int mmc_can_erase(struct mmc_card *card)
  1376. {
  1377. if ((card->host->caps & MMC_CAP_ERASE) &&
  1378. (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
  1379. return 1;
  1380. return 0;
  1381. }
  1382. EXPORT_SYMBOL(mmc_can_erase);
  1383. int mmc_can_trim(struct mmc_card *card)
  1384. {
  1385. if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
  1386. return 1;
  1387. return 0;
  1388. }
  1389. EXPORT_SYMBOL(mmc_can_trim);
  1390. int mmc_can_secure_erase_trim(struct mmc_card *card)
  1391. {
  1392. if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
  1393. return 1;
  1394. return 0;
  1395. }
  1396. EXPORT_SYMBOL(mmc_can_secure_erase_trim);
  1397. int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
  1398. unsigned int nr)
  1399. {
  1400. if (!card->erase_size)
  1401. return 0;
  1402. if (from % card->erase_size || nr % card->erase_size)
  1403. return 0;
  1404. return 1;
  1405. }
  1406. EXPORT_SYMBOL(mmc_erase_group_aligned);
  1407. static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
  1408. unsigned int arg)
  1409. {
  1410. struct mmc_host *host = card->host;
  1411. unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
  1412. unsigned int last_timeout = 0;
  1413. if (card->erase_shift)
  1414. max_qty = UINT_MAX >> card->erase_shift;
  1415. else if (mmc_card_sd(card))
  1416. max_qty = UINT_MAX;
  1417. else
  1418. max_qty = UINT_MAX / card->erase_size;
  1419. /* Find the largest qty with an OK timeout */
  1420. do {
  1421. y = 0;
  1422. for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
  1423. timeout = mmc_erase_timeout(card, arg, qty + x);
  1424. if (timeout > host->max_discard_to)
  1425. break;
  1426. if (timeout < last_timeout)
  1427. break;
  1428. last_timeout = timeout;
  1429. y = x;
  1430. }
  1431. qty += y;
  1432. } while (y);
  1433. if (!qty)
  1434. return 0;
  1435. if (qty == 1)
  1436. return 1;
  1437. /* Convert qty to sectors */
  1438. if (card->erase_shift)
  1439. max_discard = --qty << card->erase_shift;
  1440. else if (mmc_card_sd(card))
  1441. max_discard = qty;
  1442. else
  1443. max_discard = --qty * card->erase_size;
  1444. return max_discard;
  1445. }
  1446. unsigned int mmc_calc_max_discard(struct mmc_card *card)
  1447. {
  1448. struct mmc_host *host = card->host;
  1449. unsigned int max_discard, max_trim;
  1450. if (!host->max_discard_to)
  1451. return UINT_MAX;
  1452. /*
  1453. * Without erase_group_def set, MMC erase timeout depends on clock
  1454. * frequence which can change. In that case, the best choice is
  1455. * just the preferred erase size.
  1456. */
  1457. if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
  1458. return card->pref_erase;
  1459. max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
  1460. if (mmc_can_trim(card)) {
  1461. max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
  1462. if (max_trim < max_discard)
  1463. max_discard = max_trim;
  1464. } else if (max_discard < card->erase_size) {
  1465. max_discard = 0;
  1466. }
  1467. pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
  1468. mmc_hostname(host), max_discard, host->max_discard_to);
  1469. return max_discard;
  1470. }
  1471. EXPORT_SYMBOL(mmc_calc_max_discard);
  1472. int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
  1473. {
  1474. struct mmc_command cmd = {0};
  1475. if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
  1476. return 0;
  1477. cmd.opcode = MMC_SET_BLOCKLEN;
  1478. cmd.arg = blocklen;
  1479. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1480. return mmc_wait_for_cmd(card->host, &cmd, 5);
  1481. }
  1482. EXPORT_SYMBOL(mmc_set_blocklen);
  1483. static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
  1484. {
  1485. host->f_init = freq;
  1486. #ifdef CONFIG_MMC_DEBUG
  1487. pr_info("%s: %s: trying to init card at %u Hz\n",
  1488. mmc_hostname(host), __func__, host->f_init);
  1489. #endif
  1490. mmc_power_up(host);
  1491. /*
  1492. * sdio_reset sends CMD52 to reset card. Since we do not know
  1493. * if the card is being re-initialized, just send it. CMD52
  1494. * should be ignored by SD/eMMC cards.
  1495. */
  1496. sdio_reset(host);
  1497. mmc_go_idle(host);
  1498. mmc_send_if_cond(host, host->ocr_avail);
  1499. /* Order's important: probe SDIO, then SD, then MMC */
  1500. if (!mmc_attach_sdio(host))
  1501. return 0;
  1502. if (!mmc_attach_sd(host))
  1503. return 0;
  1504. if (!mmc_attach_mmc(host))
  1505. return 0;
  1506. mmc_power_off(host);
  1507. return -EIO;
  1508. }
  1509. void mmc_rescan(struct work_struct *work)
  1510. {
  1511. static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
  1512. struct mmc_host *host =
  1513. container_of(work, struct mmc_host, detect.work);
  1514. int i;
  1515. if (host->rescan_disable)
  1516. return;
  1517. mmc_bus_get(host);
  1518. /*
  1519. * if there is a _removable_ card registered, check whether it is
  1520. * still present
  1521. */
  1522. if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
  1523. && !(host->caps & MMC_CAP_NONREMOVABLE))
  1524. host->bus_ops->detect(host);
  1525. /*
  1526. * Let mmc_bus_put() free the bus/bus_ops if we've found that
  1527. * the card is no longer present.
  1528. */
  1529. mmc_bus_put(host);
  1530. mmc_bus_get(host);
  1531. /* if there still is a card present, stop here */
  1532. if (host->bus_ops != NULL) {
  1533. mmc_bus_put(host);
  1534. goto out;
  1535. }
  1536. /*
  1537. * Only we can add a new handler, so it's safe to
  1538. * release the lock here.
  1539. */
  1540. mmc_bus_put(host);
  1541. if (host->ops->get_cd && host->ops->get_cd(host) == 0)
  1542. goto out;
  1543. mmc_claim_host(host);
  1544. for (i = 0; i < ARRAY_SIZE(freqs); i++) {
  1545. if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
  1546. break;
  1547. if (freqs[i] <= host->f_min)
  1548. break;
  1549. }
  1550. mmc_release_host(host);
  1551. out:
  1552. if (host->caps & MMC_CAP_NEEDS_POLL)
  1553. mmc_schedule_delayed_work(&host->detect, HZ);
  1554. }
  1555. void mmc_start_host(struct mmc_host *host)
  1556. {
  1557. mmc_power_off(host);
  1558. mmc_detect_change(host, 0);
  1559. }
  1560. void mmc_stop_host(struct mmc_host *host)
  1561. {
  1562. #ifdef CONFIG_MMC_DEBUG
  1563. unsigned long flags;
  1564. spin_lock_irqsave(&host->lock, flags);
  1565. host->removed = 1;
  1566. spin_unlock_irqrestore(&host->lock, flags);
  1567. #endif
  1568. if (host->caps & MMC_CAP_DISABLE)
  1569. cancel_delayed_work(&host->disable);
  1570. cancel_delayed_work_sync(&host->detect);
  1571. mmc_flush_scheduled_work();
  1572. /* clear pm flags now and let card drivers set them as needed */
  1573. host->pm_flags = 0;
  1574. mmc_bus_get(host);
  1575. if (host->bus_ops && !host->bus_dead) {
  1576. if (host->bus_ops->remove)
  1577. host->bus_ops->remove(host);
  1578. mmc_claim_host(host);
  1579. mmc_detach_bus(host);
  1580. mmc_release_host(host);
  1581. mmc_bus_put(host);
  1582. return;
  1583. }
  1584. mmc_bus_put(host);
  1585. BUG_ON(host->card);
  1586. mmc_power_off(host);
  1587. }
  1588. int mmc_power_save_host(struct mmc_host *host)
  1589. {
  1590. int ret = 0;
  1591. #ifdef CONFIG_MMC_DEBUG
  1592. pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
  1593. #endif
  1594. mmc_bus_get(host);
  1595. if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
  1596. mmc_bus_put(host);
  1597. return -EINVAL;
  1598. }
  1599. if (host->bus_ops->power_save)
  1600. ret = host->bus_ops->power_save(host);
  1601. mmc_bus_put(host);
  1602. mmc_power_off(host);
  1603. return ret;
  1604. }
  1605. EXPORT_SYMBOL(mmc_power_save_host);
  1606. int mmc_power_restore_host(struct mmc_host *host)
  1607. {
  1608. int ret;
  1609. #ifdef CONFIG_MMC_DEBUG
  1610. pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
  1611. #endif
  1612. mmc_bus_get(host);
  1613. if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
  1614. mmc_bus_put(host);
  1615. return -EINVAL;
  1616. }
  1617. mmc_power_up(host);
  1618. ret = host->bus_ops->power_restore(host);
  1619. mmc_bus_put(host);
  1620. return ret;
  1621. }
  1622. EXPORT_SYMBOL(mmc_power_restore_host);
  1623. int mmc_card_awake(struct mmc_host *host)
  1624. {
  1625. int err = -ENOSYS;
  1626. mmc_bus_get(host);
  1627. if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
  1628. err = host->bus_ops->awake(host);
  1629. mmc_bus_put(host);
  1630. return err;
  1631. }
  1632. EXPORT_SYMBOL(mmc_card_awake);
  1633. int mmc_card_sleep(struct mmc_host *host)
  1634. {
  1635. int err = -ENOSYS;
  1636. mmc_bus_get(host);
  1637. if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
  1638. err = host->bus_ops->sleep(host);
  1639. mmc_bus_put(host);
  1640. return err;
  1641. }
  1642. EXPORT_SYMBOL(mmc_card_sleep);
  1643. int mmc_card_can_sleep(struct mmc_host *host)
  1644. {
  1645. struct mmc_card *card = host->card;
  1646. if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
  1647. return 1;
  1648. return 0;
  1649. }
  1650. EXPORT_SYMBOL(mmc_card_can_sleep);
  1651. #ifdef CONFIG_PM
  1652. /**
  1653. * mmc_suspend_host - suspend a host
  1654. * @host: mmc host
  1655. */
  1656. int mmc_suspend_host(struct mmc_host *host)
  1657. {
  1658. int err = 0;
  1659. if (host->caps & MMC_CAP_DISABLE)
  1660. cancel_delayed_work(&host->disable);
  1661. cancel_delayed_work(&host->detect);
  1662. mmc_flush_scheduled_work();
  1663. mmc_bus_get(host);
  1664. if (host->bus_ops && !host->bus_dead) {
  1665. if (host->bus_ops->suspend)
  1666. err = host->bus_ops->suspend(host);
  1667. if (err == -ENOSYS || !host->bus_ops->resume) {
  1668. /*
  1669. * We simply "remove" the card in this case.
  1670. * It will be redetected on resume.
  1671. */
  1672. if (host->bus_ops->remove)
  1673. host->bus_ops->remove(host);
  1674. mmc_claim_host(host);
  1675. mmc_detach_bus(host);
  1676. mmc_release_host(host);
  1677. host->pm_flags = 0;
  1678. err = 0;
  1679. }
  1680. }
  1681. mmc_bus_put(host);
  1682. if (!err && !mmc_card_keep_power(host))
  1683. mmc_power_off(host);
  1684. return err;
  1685. }
  1686. EXPORT_SYMBOL(mmc_suspend_host);
  1687. /**
  1688. * mmc_resume_host - resume a previously suspended host
  1689. * @host: mmc host
  1690. */
  1691. int mmc_resume_host(struct mmc_host *host)
  1692. {
  1693. int err = 0;
  1694. mmc_bus_get(host);
  1695. if (host->bus_ops && !host->bus_dead) {
  1696. if (!mmc_card_keep_power(host)) {
  1697. mmc_power_up(host);
  1698. mmc_select_voltage(host, host->ocr);
  1699. /*
  1700. * Tell runtime PM core we just powered up the card,
  1701. * since it still believes the card is powered off.
  1702. * Note that currently runtime PM is only enabled
  1703. * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
  1704. */
  1705. if (mmc_card_sdio(host->card) &&
  1706. (host->caps & MMC_CAP_POWER_OFF_CARD)) {
  1707. pm_runtime_disable(&host->card->dev);
  1708. pm_runtime_set_active(&host->card->dev);
  1709. pm_runtime_enable(&host->card->dev);
  1710. }
  1711. }
  1712. BUG_ON(!host->bus_ops->resume);
  1713. err = host->bus_ops->resume(host);
  1714. if (err) {
  1715. printk(KERN_WARNING "%s: error %d during resume "
  1716. "(card was removed?)\n",
  1717. mmc_hostname(host), err);
  1718. err = 0;
  1719. }
  1720. }
  1721. host->pm_flags &= ~MMC_PM_KEEP_POWER;
  1722. mmc_bus_put(host);
  1723. return err;
  1724. }
  1725. EXPORT_SYMBOL(mmc_resume_host);
  1726. /* Do the card removal on suspend if card is assumed removeable
  1727. * Do that in pm notifier while userspace isn't yet frozen, so we will be able
  1728. to sync the card.
  1729. */
  1730. int mmc_pm_notify(struct notifier_block *notify_block,
  1731. unsigned long mode, void *unused)
  1732. {
  1733. struct mmc_host *host = container_of(
  1734. notify_block, struct mmc_host, pm_notify);
  1735. unsigned long flags;
  1736. switch (mode) {
  1737. case PM_HIBERNATION_PREPARE:
  1738. case PM_SUSPEND_PREPARE:
  1739. spin_lock_irqsave(&host->lock, flags);
  1740. host->rescan_disable = 1;
  1741. spin_unlock_irqrestore(&host->lock, flags);
  1742. cancel_delayed_work_sync(&host->detect);
  1743. if (!host->bus_ops || host->bus_ops->suspend)
  1744. break;
  1745. mmc_claim_host(host);
  1746. if (host->bus_ops->remove)
  1747. host->bus_ops->remove(host);
  1748. mmc_detach_bus(host);
  1749. mmc_release_host(host);
  1750. host->pm_flags = 0;
  1751. break;
  1752. case PM_POST_SUSPEND:
  1753. case PM_POST_HIBERNATION:
  1754. case PM_POST_RESTORE:
  1755. spin_lock_irqsave(&host->lock, flags);
  1756. host->rescan_disable = 0;
  1757. spin_unlock_irqrestore(&host->lock, flags);
  1758. mmc_detect_change(host, 0);
  1759. }
  1760. return 0;
  1761. }
  1762. #endif
  1763. static int __init mmc_init(void)
  1764. {
  1765. int ret;
  1766. workqueue = alloc_ordered_workqueue("kmmcd", 0);
  1767. if (!workqueue)
  1768. return -ENOMEM;
  1769. ret = mmc_register_bus();
  1770. if (ret)
  1771. goto destroy_workqueue;
  1772. ret = mmc_register_host_class();
  1773. if (ret)
  1774. goto unregister_bus;
  1775. ret = sdio_register_bus();
  1776. if (ret)
  1777. goto unregister_host_class;
  1778. return 0;
  1779. unregister_host_class:
  1780. mmc_unregister_host_class();
  1781. unregister_bus:
  1782. mmc_unregister_bus();
  1783. destroy_workqueue:
  1784. destroy_workqueue(workqueue);
  1785. return ret;
  1786. }
  1787. static void __exit mmc_exit(void)
  1788. {
  1789. sdio_unregister_bus();
  1790. mmc_unregister_host_class();
  1791. mmc_unregister_bus();
  1792. destroy_workqueue(workqueue);
  1793. }
  1794. subsys_initcall(mmc_init);
  1795. module_exit(mmc_exit);
  1796. MODULE_LICENSE("GPL");