core.c 41 KB

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