core.c 45 KB

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