core.c 44 KB

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