core.c 45 KB

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