core.c 49 KB

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