core.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  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. led_trigger_event(host->led, LED_FULL);
  147. mrq->cmd->error = 0;
  148. mrq->cmd->mrq = mrq;
  149. if (mrq->data) {
  150. BUG_ON(mrq->data->blksz > host->max_blk_size);
  151. BUG_ON(mrq->data->blocks > host->max_blk_count);
  152. BUG_ON(mrq->data->blocks * mrq->data->blksz >
  153. host->max_req_size);
  154. #ifdef CONFIG_MMC_DEBUG
  155. sz = 0;
  156. for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
  157. sz += sg->length;
  158. BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
  159. #endif
  160. mrq->cmd->data = mrq->data;
  161. mrq->data->error = 0;
  162. mrq->data->mrq = mrq;
  163. if (mrq->stop) {
  164. mrq->data->stop = mrq->stop;
  165. mrq->stop->error = 0;
  166. mrq->stop->mrq = mrq;
  167. }
  168. }
  169. mmc_host_clk_ungate(host);
  170. 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. sdio_reset(host);
  1284. mmc_go_idle(host);
  1285. mmc_send_if_cond(host, host->ocr_avail);
  1286. /* Order's important: probe SDIO, then SD, then MMC */
  1287. if (!mmc_attach_sdio(host))
  1288. return 0;
  1289. if (!mmc_attach_sd(host))
  1290. return 0;
  1291. if (!mmc_attach_mmc(host))
  1292. return 0;
  1293. mmc_power_off(host);
  1294. return -EIO;
  1295. }
  1296. void mmc_rescan(struct work_struct *work)
  1297. {
  1298. static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
  1299. struct mmc_host *host =
  1300. container_of(work, struct mmc_host, detect.work);
  1301. int i;
  1302. if (host->rescan_disable)
  1303. return;
  1304. mmc_bus_get(host);
  1305. /*
  1306. * if there is a _removable_ card registered, check whether it is
  1307. * still present
  1308. */
  1309. if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
  1310. && mmc_card_is_removable(host))
  1311. host->bus_ops->detect(host);
  1312. /*
  1313. * Let mmc_bus_put() free the bus/bus_ops if we've found that
  1314. * the card is no longer present.
  1315. */
  1316. mmc_bus_put(host);
  1317. mmc_bus_get(host);
  1318. /* if there still is a card present, stop here */
  1319. if (host->bus_ops != NULL) {
  1320. mmc_bus_put(host);
  1321. goto out;
  1322. }
  1323. /*
  1324. * Only we can add a new handler, so it's safe to
  1325. * release the lock here.
  1326. */
  1327. mmc_bus_put(host);
  1328. if (host->ops->get_cd && host->ops->get_cd(host) == 0)
  1329. goto out;
  1330. mmc_claim_host(host);
  1331. for (i = 0; i < ARRAY_SIZE(freqs); i++) {
  1332. if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
  1333. break;
  1334. if (freqs[i] < host->f_min)
  1335. break;
  1336. }
  1337. mmc_release_host(host);
  1338. out:
  1339. if (host->caps & MMC_CAP_NEEDS_POLL)
  1340. mmc_schedule_delayed_work(&host->detect, HZ);
  1341. }
  1342. void mmc_start_host(struct mmc_host *host)
  1343. {
  1344. mmc_power_off(host);
  1345. mmc_detect_change(host, 0);
  1346. }
  1347. void mmc_stop_host(struct mmc_host *host)
  1348. {
  1349. #ifdef CONFIG_MMC_DEBUG
  1350. unsigned long flags;
  1351. spin_lock_irqsave(&host->lock, flags);
  1352. host->removed = 1;
  1353. spin_unlock_irqrestore(&host->lock, flags);
  1354. #endif
  1355. if (host->caps & MMC_CAP_DISABLE)
  1356. cancel_delayed_work(&host->disable);
  1357. cancel_delayed_work_sync(&host->detect);
  1358. mmc_flush_scheduled_work();
  1359. /* clear pm flags now and let card drivers set them as needed */
  1360. host->pm_flags = 0;
  1361. mmc_bus_get(host);
  1362. if (host->bus_ops && !host->bus_dead) {
  1363. if (host->bus_ops->remove)
  1364. host->bus_ops->remove(host);
  1365. mmc_claim_host(host);
  1366. mmc_detach_bus(host);
  1367. mmc_release_host(host);
  1368. mmc_bus_put(host);
  1369. return;
  1370. }
  1371. mmc_bus_put(host);
  1372. BUG_ON(host->card);
  1373. mmc_power_off(host);
  1374. }
  1375. int mmc_power_save_host(struct mmc_host *host)
  1376. {
  1377. int ret = 0;
  1378. mmc_bus_get(host);
  1379. if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
  1380. mmc_bus_put(host);
  1381. return -EINVAL;
  1382. }
  1383. if (host->bus_ops->power_save)
  1384. ret = host->bus_ops->power_save(host);
  1385. mmc_bus_put(host);
  1386. mmc_power_off(host);
  1387. return ret;
  1388. }
  1389. EXPORT_SYMBOL(mmc_power_save_host);
  1390. int mmc_power_restore_host(struct mmc_host *host)
  1391. {
  1392. int ret;
  1393. mmc_bus_get(host);
  1394. if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
  1395. mmc_bus_put(host);
  1396. return -EINVAL;
  1397. }
  1398. mmc_power_up(host);
  1399. ret = host->bus_ops->power_restore(host);
  1400. mmc_bus_put(host);
  1401. return ret;
  1402. }
  1403. EXPORT_SYMBOL(mmc_power_restore_host);
  1404. int mmc_card_awake(struct mmc_host *host)
  1405. {
  1406. int err = -ENOSYS;
  1407. mmc_bus_get(host);
  1408. if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
  1409. err = host->bus_ops->awake(host);
  1410. mmc_bus_put(host);
  1411. return err;
  1412. }
  1413. EXPORT_SYMBOL(mmc_card_awake);
  1414. int mmc_card_sleep(struct mmc_host *host)
  1415. {
  1416. int err = -ENOSYS;
  1417. mmc_bus_get(host);
  1418. if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
  1419. err = host->bus_ops->sleep(host);
  1420. mmc_bus_put(host);
  1421. return err;
  1422. }
  1423. EXPORT_SYMBOL(mmc_card_sleep);
  1424. int mmc_card_can_sleep(struct mmc_host *host)
  1425. {
  1426. struct mmc_card *card = host->card;
  1427. if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
  1428. return 1;
  1429. return 0;
  1430. }
  1431. EXPORT_SYMBOL(mmc_card_can_sleep);
  1432. #ifdef CONFIG_PM
  1433. /**
  1434. * mmc_suspend_host - suspend a host
  1435. * @host: mmc host
  1436. */
  1437. int mmc_suspend_host(struct mmc_host *host)
  1438. {
  1439. int err = 0;
  1440. if (host->caps & MMC_CAP_DISABLE)
  1441. cancel_delayed_work(&host->disable);
  1442. cancel_delayed_work(&host->detect);
  1443. mmc_flush_scheduled_work();
  1444. mmc_bus_get(host);
  1445. if (host->bus_ops && !host->bus_dead) {
  1446. if (host->bus_ops->suspend)
  1447. err = host->bus_ops->suspend(host);
  1448. if (err == -ENOSYS || !host->bus_ops->resume) {
  1449. /*
  1450. * We simply "remove" the card in this case.
  1451. * It will be redetected on resume.
  1452. */
  1453. if (host->bus_ops->remove)
  1454. host->bus_ops->remove(host);
  1455. mmc_claim_host(host);
  1456. mmc_detach_bus(host);
  1457. mmc_release_host(host);
  1458. host->pm_flags = 0;
  1459. err = 0;
  1460. }
  1461. }
  1462. mmc_bus_put(host);
  1463. if (!err && !(host->pm_flags & MMC_PM_KEEP_POWER))
  1464. mmc_power_off(host);
  1465. return err;
  1466. }
  1467. EXPORT_SYMBOL(mmc_suspend_host);
  1468. /**
  1469. * mmc_resume_host - resume a previously suspended host
  1470. * @host: mmc host
  1471. */
  1472. int mmc_resume_host(struct mmc_host *host)
  1473. {
  1474. int err = 0;
  1475. mmc_bus_get(host);
  1476. if (host->bus_ops && !host->bus_dead) {
  1477. if (!(host->pm_flags & MMC_PM_KEEP_POWER)) {
  1478. mmc_power_up(host);
  1479. mmc_select_voltage(host, host->ocr);
  1480. /*
  1481. * Tell runtime PM core we just powered up the card,
  1482. * since it still believes the card is powered off.
  1483. * Note that currently runtime PM is only enabled
  1484. * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
  1485. */
  1486. if (mmc_card_sdio(host->card) &&
  1487. (host->caps & MMC_CAP_POWER_OFF_CARD)) {
  1488. pm_runtime_disable(&host->card->dev);
  1489. pm_runtime_set_active(&host->card->dev);
  1490. pm_runtime_enable(&host->card->dev);
  1491. }
  1492. }
  1493. BUG_ON(!host->bus_ops->resume);
  1494. err = host->bus_ops->resume(host);
  1495. if (err) {
  1496. printk(KERN_WARNING "%s: error %d during resume "
  1497. "(card was removed?)\n",
  1498. mmc_hostname(host), err);
  1499. err = 0;
  1500. }
  1501. }
  1502. mmc_bus_put(host);
  1503. return err;
  1504. }
  1505. EXPORT_SYMBOL(mmc_resume_host);
  1506. /* Do the card removal on suspend if card is assumed removeable
  1507. * Do that in pm notifier while userspace isn't yet frozen, so we will be able
  1508. to sync the card.
  1509. */
  1510. int mmc_pm_notify(struct notifier_block *notify_block,
  1511. unsigned long mode, void *unused)
  1512. {
  1513. struct mmc_host *host = container_of(
  1514. notify_block, struct mmc_host, pm_notify);
  1515. unsigned long flags;
  1516. switch (mode) {
  1517. case PM_HIBERNATION_PREPARE:
  1518. case PM_SUSPEND_PREPARE:
  1519. spin_lock_irqsave(&host->lock, flags);
  1520. host->rescan_disable = 1;
  1521. spin_unlock_irqrestore(&host->lock, flags);
  1522. cancel_delayed_work_sync(&host->detect);
  1523. if (!host->bus_ops || host->bus_ops->suspend)
  1524. break;
  1525. mmc_claim_host(host);
  1526. if (host->bus_ops->remove)
  1527. host->bus_ops->remove(host);
  1528. mmc_detach_bus(host);
  1529. mmc_release_host(host);
  1530. host->pm_flags = 0;
  1531. break;
  1532. case PM_POST_SUSPEND:
  1533. case PM_POST_HIBERNATION:
  1534. case PM_POST_RESTORE:
  1535. spin_lock_irqsave(&host->lock, flags);
  1536. host->rescan_disable = 0;
  1537. spin_unlock_irqrestore(&host->lock, flags);
  1538. mmc_detect_change(host, 0);
  1539. }
  1540. return 0;
  1541. }
  1542. #endif
  1543. static int __init mmc_init(void)
  1544. {
  1545. int ret;
  1546. workqueue = alloc_ordered_workqueue("kmmcd", 0);
  1547. if (!workqueue)
  1548. return -ENOMEM;
  1549. ret = mmc_register_bus();
  1550. if (ret)
  1551. goto destroy_workqueue;
  1552. ret = mmc_register_host_class();
  1553. if (ret)
  1554. goto unregister_bus;
  1555. ret = sdio_register_bus();
  1556. if (ret)
  1557. goto unregister_host_class;
  1558. return 0;
  1559. unregister_host_class:
  1560. mmc_unregister_host_class();
  1561. unregister_bus:
  1562. mmc_unregister_bus();
  1563. destroy_workqueue:
  1564. destroy_workqueue(workqueue);
  1565. return ret;
  1566. }
  1567. static void __exit mmc_exit(void)
  1568. {
  1569. sdio_unregister_bus();
  1570. mmc_unregister_host_class();
  1571. mmc_unregister_bus();
  1572. destroy_workqueue(workqueue);
  1573. }
  1574. subsys_initcall(mmc_init);
  1575. module_exit(mmc_exit);
  1576. MODULE_LICENSE("GPL");