core.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798
  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. * @mmc: the host to regulate
  660. * @supply: regulator to use
  661. * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
  662. *
  663. * Returns zero on success, else negative errno.
  664. *
  665. * MMC host drivers may use this to enable or disable a regulator using
  666. * a particular supply voltage. This would normally be called from the
  667. * set_ios() method.
  668. */
  669. int mmc_regulator_set_ocr(struct mmc_host *mmc,
  670. struct regulator *supply,
  671. unsigned short vdd_bit)
  672. {
  673. int result = 0;
  674. int min_uV, max_uV;
  675. if (vdd_bit) {
  676. int tmp;
  677. int voltage;
  678. /* REVISIT mmc_vddrange_to_ocrmask() may have set some
  679. * bits this regulator doesn't quite support ... don't
  680. * be too picky, most cards and regulators are OK with
  681. * a 0.1V range goof (it's a small error percentage).
  682. */
  683. tmp = vdd_bit - ilog2(MMC_VDD_165_195);
  684. if (tmp == 0) {
  685. min_uV = 1650 * 1000;
  686. max_uV = 1950 * 1000;
  687. } else {
  688. min_uV = 1900 * 1000 + tmp * 100 * 1000;
  689. max_uV = min_uV + 100 * 1000;
  690. }
  691. /* avoid needless changes to this voltage; the regulator
  692. * might not allow this operation
  693. */
  694. voltage = regulator_get_voltage(supply);
  695. if (voltage < 0)
  696. result = voltage;
  697. else if (voltage < min_uV || voltage > max_uV)
  698. result = regulator_set_voltage(supply, min_uV, max_uV);
  699. else
  700. result = 0;
  701. if (result == 0 && !mmc->regulator_enabled) {
  702. result = regulator_enable(supply);
  703. if (!result)
  704. mmc->regulator_enabled = true;
  705. }
  706. } else if (mmc->regulator_enabled) {
  707. result = regulator_disable(supply);
  708. if (result == 0)
  709. mmc->regulator_enabled = false;
  710. }
  711. if (result)
  712. dev_err(mmc_dev(mmc),
  713. "could not set regulator OCR (%d)\n", result);
  714. return result;
  715. }
  716. EXPORT_SYMBOL(mmc_regulator_set_ocr);
  717. #endif /* CONFIG_REGULATOR */
  718. /*
  719. * Mask off any voltages we don't support and select
  720. * the lowest voltage
  721. */
  722. u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
  723. {
  724. int bit;
  725. ocr &= host->ocr_avail;
  726. bit = ffs(ocr);
  727. if (bit) {
  728. bit -= 1;
  729. ocr &= 3 << bit;
  730. host->ios.vdd = bit;
  731. mmc_set_ios(host);
  732. } else {
  733. pr_warning("%s: host doesn't support card's voltages\n",
  734. mmc_hostname(host));
  735. ocr = 0;
  736. }
  737. return ocr;
  738. }
  739. /*
  740. * Select timing parameters for host.
  741. */
  742. void mmc_set_timing(struct mmc_host *host, unsigned int timing)
  743. {
  744. host->ios.timing = timing;
  745. mmc_set_ios(host);
  746. }
  747. /*
  748. * Apply power to the MMC stack. This is a two-stage process.
  749. * First, we enable power to the card without the clock running.
  750. * We then wait a bit for the power to stabilise. Finally,
  751. * enable the bus drivers and clock to the card.
  752. *
  753. * We must _NOT_ enable the clock prior to power stablising.
  754. *
  755. * If a host does all the power sequencing itself, ignore the
  756. * initial MMC_POWER_UP stage.
  757. */
  758. static void mmc_power_up(struct mmc_host *host)
  759. {
  760. int bit;
  761. /* If ocr is set, we use it */
  762. if (host->ocr)
  763. bit = ffs(host->ocr) - 1;
  764. else
  765. bit = fls(host->ocr_avail) - 1;
  766. host->ios.vdd = bit;
  767. if (mmc_host_is_spi(host)) {
  768. host->ios.chip_select = MMC_CS_HIGH;
  769. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  770. } else {
  771. host->ios.chip_select = MMC_CS_DONTCARE;
  772. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  773. }
  774. host->ios.power_mode = MMC_POWER_UP;
  775. host->ios.bus_width = MMC_BUS_WIDTH_1;
  776. host->ios.timing = MMC_TIMING_LEGACY;
  777. mmc_set_ios(host);
  778. /*
  779. * This delay should be sufficient to allow the power supply
  780. * to reach the minimum voltage.
  781. */
  782. mmc_delay(10);
  783. host->ios.clock = host->f_init;
  784. host->ios.power_mode = MMC_POWER_ON;
  785. mmc_set_ios(host);
  786. /*
  787. * This delay must be at least 74 clock sizes, or 1 ms, or the
  788. * time required to reach a stable voltage.
  789. */
  790. mmc_delay(10);
  791. }
  792. static void mmc_power_off(struct mmc_host *host)
  793. {
  794. host->ios.clock = 0;
  795. host->ios.vdd = 0;
  796. if (!mmc_host_is_spi(host)) {
  797. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  798. host->ios.chip_select = MMC_CS_DONTCARE;
  799. }
  800. host->ios.power_mode = MMC_POWER_OFF;
  801. host->ios.bus_width = MMC_BUS_WIDTH_1;
  802. host->ios.timing = MMC_TIMING_LEGACY;
  803. mmc_set_ios(host);
  804. }
  805. /*
  806. * Cleanup when the last reference to the bus operator is dropped.
  807. */
  808. static void __mmc_release_bus(struct mmc_host *host)
  809. {
  810. BUG_ON(!host);
  811. BUG_ON(host->bus_refs);
  812. BUG_ON(!host->bus_dead);
  813. host->bus_ops = NULL;
  814. }
  815. /*
  816. * Increase reference count of bus operator
  817. */
  818. static inline void mmc_bus_get(struct mmc_host *host)
  819. {
  820. unsigned long flags;
  821. spin_lock_irqsave(&host->lock, flags);
  822. host->bus_refs++;
  823. spin_unlock_irqrestore(&host->lock, flags);
  824. }
  825. /*
  826. * Decrease reference count of bus operator and free it if
  827. * it is the last reference.
  828. */
  829. static inline void mmc_bus_put(struct mmc_host *host)
  830. {
  831. unsigned long flags;
  832. spin_lock_irqsave(&host->lock, flags);
  833. host->bus_refs--;
  834. if ((host->bus_refs == 0) && host->bus_ops)
  835. __mmc_release_bus(host);
  836. spin_unlock_irqrestore(&host->lock, flags);
  837. }
  838. /*
  839. * Assign a mmc bus handler to a host. Only one bus handler may control a
  840. * host at any given time.
  841. */
  842. void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
  843. {
  844. unsigned long flags;
  845. BUG_ON(!host);
  846. BUG_ON(!ops);
  847. WARN_ON(!host->claimed);
  848. spin_lock_irqsave(&host->lock, flags);
  849. BUG_ON(host->bus_ops);
  850. BUG_ON(host->bus_refs);
  851. host->bus_ops = ops;
  852. host->bus_refs = 1;
  853. host->bus_dead = 0;
  854. spin_unlock_irqrestore(&host->lock, flags);
  855. }
  856. /*
  857. * Remove the current bus handler from a host. Assumes that there are
  858. * no interesting cards left, so the bus is powered down.
  859. */
  860. void mmc_detach_bus(struct mmc_host *host)
  861. {
  862. unsigned long flags;
  863. BUG_ON(!host);
  864. WARN_ON(!host->claimed);
  865. WARN_ON(!host->bus_ops);
  866. spin_lock_irqsave(&host->lock, flags);
  867. host->bus_dead = 1;
  868. spin_unlock_irqrestore(&host->lock, flags);
  869. mmc_power_off(host);
  870. mmc_bus_put(host);
  871. }
  872. /**
  873. * mmc_detect_change - process change of state on a MMC socket
  874. * @host: host which changed state.
  875. * @delay: optional delay to wait before detection (jiffies)
  876. *
  877. * MMC drivers should call this when they detect a card has been
  878. * inserted or removed. The MMC layer will confirm that any
  879. * present card is still functional, and initialize any newly
  880. * inserted.
  881. */
  882. void mmc_detect_change(struct mmc_host *host, unsigned long delay)
  883. {
  884. #ifdef CONFIG_MMC_DEBUG
  885. unsigned long flags;
  886. spin_lock_irqsave(&host->lock, flags);
  887. WARN_ON(host->removed);
  888. spin_unlock_irqrestore(&host->lock, flags);
  889. #endif
  890. mmc_schedule_delayed_work(&host->detect, delay);
  891. }
  892. EXPORT_SYMBOL(mmc_detect_change);
  893. void mmc_init_erase(struct mmc_card *card)
  894. {
  895. unsigned int sz;
  896. if (is_power_of_2(card->erase_size))
  897. card->erase_shift = ffs(card->erase_size) - 1;
  898. else
  899. card->erase_shift = 0;
  900. /*
  901. * It is possible to erase an arbitrarily large area of an SD or MMC
  902. * card. That is not desirable because it can take a long time
  903. * (minutes) potentially delaying more important I/O, and also the
  904. * timeout calculations become increasingly hugely over-estimated.
  905. * Consequently, 'pref_erase' is defined as a guide to limit erases
  906. * to that size and alignment.
  907. *
  908. * For SD cards that define Allocation Unit size, limit erases to one
  909. * Allocation Unit at a time. For MMC cards that define High Capacity
  910. * Erase Size, whether it is switched on or not, limit to that size.
  911. * Otherwise just have a stab at a good value. For modern cards it
  912. * will end up being 4MiB. Note that if the value is too small, it
  913. * can end up taking longer to erase.
  914. */
  915. if (mmc_card_sd(card) && card->ssr.au) {
  916. card->pref_erase = card->ssr.au;
  917. card->erase_shift = ffs(card->ssr.au) - 1;
  918. } else if (card->ext_csd.hc_erase_size) {
  919. card->pref_erase = card->ext_csd.hc_erase_size;
  920. } else {
  921. sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
  922. if (sz < 128)
  923. card->pref_erase = 512 * 1024 / 512;
  924. else if (sz < 512)
  925. card->pref_erase = 1024 * 1024 / 512;
  926. else if (sz < 1024)
  927. card->pref_erase = 2 * 1024 * 1024 / 512;
  928. else
  929. card->pref_erase = 4 * 1024 * 1024 / 512;
  930. if (card->pref_erase < card->erase_size)
  931. card->pref_erase = card->erase_size;
  932. else {
  933. sz = card->pref_erase % card->erase_size;
  934. if (sz)
  935. card->pref_erase += card->erase_size - sz;
  936. }
  937. }
  938. }
  939. static void mmc_set_mmc_erase_timeout(struct mmc_card *card,
  940. struct mmc_command *cmd,
  941. unsigned int arg, unsigned int qty)
  942. {
  943. unsigned int erase_timeout;
  944. if (card->ext_csd.erase_group_def & 1) {
  945. /* High Capacity Erase Group Size uses HC timeouts */
  946. if (arg == MMC_TRIM_ARG)
  947. erase_timeout = card->ext_csd.trim_timeout;
  948. else
  949. erase_timeout = card->ext_csd.hc_erase_timeout;
  950. } else {
  951. /* CSD Erase Group Size uses write timeout */
  952. unsigned int mult = (10 << card->csd.r2w_factor);
  953. unsigned int timeout_clks = card->csd.tacc_clks * mult;
  954. unsigned int timeout_us;
  955. /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
  956. if (card->csd.tacc_ns < 1000000)
  957. timeout_us = (card->csd.tacc_ns * mult) / 1000;
  958. else
  959. timeout_us = (card->csd.tacc_ns / 1000) * mult;
  960. /*
  961. * ios.clock is only a target. The real clock rate might be
  962. * less but not that much less, so fudge it by multiplying by 2.
  963. */
  964. timeout_clks <<= 1;
  965. timeout_us += (timeout_clks * 1000) /
  966. (card->host->ios.clock / 1000);
  967. erase_timeout = timeout_us / 1000;
  968. /*
  969. * Theoretically, the calculation could underflow so round up
  970. * to 1ms in that case.
  971. */
  972. if (!erase_timeout)
  973. erase_timeout = 1;
  974. }
  975. /* Multiplier for secure operations */
  976. if (arg & MMC_SECURE_ARGS) {
  977. if (arg == MMC_SECURE_ERASE_ARG)
  978. erase_timeout *= card->ext_csd.sec_erase_mult;
  979. else
  980. erase_timeout *= card->ext_csd.sec_trim_mult;
  981. }
  982. erase_timeout *= qty;
  983. /*
  984. * Ensure at least a 1 second timeout for SPI as per
  985. * 'mmc_set_data_timeout()'
  986. */
  987. if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
  988. erase_timeout = 1000;
  989. cmd->erase_timeout = erase_timeout;
  990. }
  991. static void mmc_set_sd_erase_timeout(struct mmc_card *card,
  992. struct mmc_command *cmd, unsigned int arg,
  993. unsigned int qty)
  994. {
  995. if (card->ssr.erase_timeout) {
  996. /* Erase timeout specified in SD Status Register (SSR) */
  997. cmd->erase_timeout = card->ssr.erase_timeout * qty +
  998. card->ssr.erase_offset;
  999. } else {
  1000. /*
  1001. * Erase timeout not specified in SD Status Register (SSR) so
  1002. * use 250ms per write block.
  1003. */
  1004. cmd->erase_timeout = 250 * qty;
  1005. }
  1006. /* Must not be less than 1 second */
  1007. if (cmd->erase_timeout < 1000)
  1008. cmd->erase_timeout = 1000;
  1009. }
  1010. static void mmc_set_erase_timeout(struct mmc_card *card,
  1011. struct mmc_command *cmd, unsigned int arg,
  1012. unsigned int qty)
  1013. {
  1014. if (mmc_card_sd(card))
  1015. mmc_set_sd_erase_timeout(card, cmd, arg, qty);
  1016. else
  1017. mmc_set_mmc_erase_timeout(card, cmd, arg, qty);
  1018. }
  1019. static int mmc_do_erase(struct mmc_card *card, unsigned int from,
  1020. unsigned int to, unsigned int arg)
  1021. {
  1022. struct mmc_command cmd;
  1023. unsigned int qty = 0;
  1024. int err;
  1025. /*
  1026. * qty is used to calculate the erase timeout which depends on how many
  1027. * erase groups (or allocation units in SD terminology) are affected.
  1028. * We count erasing part of an erase group as one erase group.
  1029. * For SD, the allocation units are always a power of 2. For MMC, the
  1030. * erase group size is almost certainly also power of 2, but it does not
  1031. * seem to insist on that in the JEDEC standard, so we fall back to
  1032. * division in that case. SD may not specify an allocation unit size,
  1033. * in which case the timeout is based on the number of write blocks.
  1034. *
  1035. * Note that the timeout for secure trim 2 will only be correct if the
  1036. * number of erase groups specified is the same as the total of all
  1037. * preceding secure trim 1 commands. Since the power may have been
  1038. * lost since the secure trim 1 commands occurred, it is generally
  1039. * impossible to calculate the secure trim 2 timeout correctly.
  1040. */
  1041. if (card->erase_shift)
  1042. qty += ((to >> card->erase_shift) -
  1043. (from >> card->erase_shift)) + 1;
  1044. else if (mmc_card_sd(card))
  1045. qty += to - from + 1;
  1046. else
  1047. qty += ((to / card->erase_size) -
  1048. (from / card->erase_size)) + 1;
  1049. if (!mmc_card_blockaddr(card)) {
  1050. from <<= 9;
  1051. to <<= 9;
  1052. }
  1053. memset(&cmd, 0, sizeof(struct mmc_command));
  1054. if (mmc_card_sd(card))
  1055. cmd.opcode = SD_ERASE_WR_BLK_START;
  1056. else
  1057. cmd.opcode = MMC_ERASE_GROUP_START;
  1058. cmd.arg = from;
  1059. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1060. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1061. if (err) {
  1062. printk(KERN_ERR "mmc_erase: group start error %d, "
  1063. "status %#x\n", err, cmd.resp[0]);
  1064. err = -EINVAL;
  1065. goto out;
  1066. }
  1067. memset(&cmd, 0, sizeof(struct mmc_command));
  1068. if (mmc_card_sd(card))
  1069. cmd.opcode = SD_ERASE_WR_BLK_END;
  1070. else
  1071. cmd.opcode = MMC_ERASE_GROUP_END;
  1072. cmd.arg = to;
  1073. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1074. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1075. if (err) {
  1076. printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
  1077. err, cmd.resp[0]);
  1078. err = -EINVAL;
  1079. goto out;
  1080. }
  1081. memset(&cmd, 0, sizeof(struct mmc_command));
  1082. cmd.opcode = MMC_ERASE;
  1083. cmd.arg = arg;
  1084. cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  1085. mmc_set_erase_timeout(card, &cmd, arg, qty);
  1086. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1087. if (err) {
  1088. printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
  1089. err, cmd.resp[0]);
  1090. err = -EIO;
  1091. goto out;
  1092. }
  1093. if (mmc_host_is_spi(card->host))
  1094. goto out;
  1095. do {
  1096. memset(&cmd, 0, sizeof(struct mmc_command));
  1097. cmd.opcode = MMC_SEND_STATUS;
  1098. cmd.arg = card->rca << 16;
  1099. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1100. /* Do not retry else we can't see errors */
  1101. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1102. if (err || (cmd.resp[0] & 0xFDF92000)) {
  1103. printk(KERN_ERR "error %d requesting status %#x\n",
  1104. err, cmd.resp[0]);
  1105. err = -EIO;
  1106. goto out;
  1107. }
  1108. } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
  1109. R1_CURRENT_STATE(cmd.resp[0]) == 7);
  1110. out:
  1111. return err;
  1112. }
  1113. /**
  1114. * mmc_erase - erase sectors.
  1115. * @card: card to erase
  1116. * @from: first sector to erase
  1117. * @nr: number of sectors to erase
  1118. * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
  1119. *
  1120. * Caller must claim host before calling this function.
  1121. */
  1122. int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
  1123. unsigned int arg)
  1124. {
  1125. unsigned int rem, to = from + nr;
  1126. if (!(card->host->caps & MMC_CAP_ERASE) ||
  1127. !(card->csd.cmdclass & CCC_ERASE))
  1128. return -EOPNOTSUPP;
  1129. if (!card->erase_size)
  1130. return -EOPNOTSUPP;
  1131. if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
  1132. return -EOPNOTSUPP;
  1133. if ((arg & MMC_SECURE_ARGS) &&
  1134. !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
  1135. return -EOPNOTSUPP;
  1136. if ((arg & MMC_TRIM_ARGS) &&
  1137. !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
  1138. return -EOPNOTSUPP;
  1139. if (arg == MMC_SECURE_ERASE_ARG) {
  1140. if (from % card->erase_size || nr % card->erase_size)
  1141. return -EINVAL;
  1142. }
  1143. if (arg == MMC_ERASE_ARG) {
  1144. rem = from % card->erase_size;
  1145. if (rem) {
  1146. rem = card->erase_size - rem;
  1147. from += rem;
  1148. if (nr > rem)
  1149. nr -= rem;
  1150. else
  1151. return 0;
  1152. }
  1153. rem = nr % card->erase_size;
  1154. if (rem)
  1155. nr -= rem;
  1156. }
  1157. if (nr == 0)
  1158. return 0;
  1159. to = from + nr;
  1160. if (to <= from)
  1161. return -EINVAL;
  1162. /* 'from' and 'to' are inclusive */
  1163. to -= 1;
  1164. return mmc_do_erase(card, from, to, arg);
  1165. }
  1166. EXPORT_SYMBOL(mmc_erase);
  1167. int mmc_can_erase(struct mmc_card *card)
  1168. {
  1169. if ((card->host->caps & MMC_CAP_ERASE) &&
  1170. (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
  1171. return 1;
  1172. return 0;
  1173. }
  1174. EXPORT_SYMBOL(mmc_can_erase);
  1175. int mmc_can_trim(struct mmc_card *card)
  1176. {
  1177. if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
  1178. return 1;
  1179. return 0;
  1180. }
  1181. EXPORT_SYMBOL(mmc_can_trim);
  1182. int mmc_can_secure_erase_trim(struct mmc_card *card)
  1183. {
  1184. if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
  1185. return 1;
  1186. return 0;
  1187. }
  1188. EXPORT_SYMBOL(mmc_can_secure_erase_trim);
  1189. int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
  1190. unsigned int nr)
  1191. {
  1192. if (!card->erase_size)
  1193. return 0;
  1194. if (from % card->erase_size || nr % card->erase_size)
  1195. return 0;
  1196. return 1;
  1197. }
  1198. EXPORT_SYMBOL(mmc_erase_group_aligned);
  1199. void mmc_rescan(struct work_struct *work)
  1200. {
  1201. struct mmc_host *host =
  1202. container_of(work, struct mmc_host, detect.work);
  1203. u32 ocr;
  1204. int err;
  1205. unsigned long flags;
  1206. int i;
  1207. const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
  1208. spin_lock_irqsave(&host->lock, flags);
  1209. if (host->rescan_disable) {
  1210. spin_unlock_irqrestore(&host->lock, flags);
  1211. return;
  1212. }
  1213. spin_unlock_irqrestore(&host->lock, flags);
  1214. mmc_bus_get(host);
  1215. /* if there is a card registered, check whether it is still present */
  1216. if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
  1217. host->bus_ops->detect(host);
  1218. mmc_bus_put(host);
  1219. mmc_bus_get(host);
  1220. /* if there still is a card present, stop here */
  1221. if (host->bus_ops != NULL) {
  1222. mmc_bus_put(host);
  1223. goto out;
  1224. }
  1225. /* detect a newly inserted card */
  1226. /*
  1227. * Only we can add a new handler, so it's safe to
  1228. * release the lock here.
  1229. */
  1230. mmc_bus_put(host);
  1231. if (host->ops->get_cd && host->ops->get_cd(host) == 0)
  1232. goto out;
  1233. for (i = 0; i < ARRAY_SIZE(freqs); i++) {
  1234. mmc_claim_host(host);
  1235. if (freqs[i] >= host->f_min)
  1236. host->f_init = freqs[i];
  1237. else if (!i || freqs[i-1] > host->f_min)
  1238. host->f_init = host->f_min;
  1239. else {
  1240. mmc_release_host(host);
  1241. goto out;
  1242. }
  1243. #ifdef CONFIG_MMC_DEBUG
  1244. pr_info("%s: %s: trying to init card at %u Hz\n",
  1245. mmc_hostname(host), __func__, host->f_init);
  1246. #endif
  1247. mmc_power_up(host);
  1248. sdio_reset(host);
  1249. mmc_go_idle(host);
  1250. mmc_send_if_cond(host, host->ocr_avail);
  1251. /*
  1252. * First we search for SDIO...
  1253. */
  1254. err = mmc_send_io_op_cond(host, 0, &ocr);
  1255. if (!err) {
  1256. if (mmc_attach_sdio(host, ocr)) {
  1257. mmc_claim_host(host);
  1258. /*
  1259. * Try SDMEM (but not MMC) even if SDIO
  1260. * is broken.
  1261. */
  1262. if (mmc_send_app_op_cond(host, 0, &ocr))
  1263. goto out_fail;
  1264. if (mmc_attach_sd(host, ocr))
  1265. mmc_power_off(host);
  1266. }
  1267. goto out;
  1268. }
  1269. /*
  1270. * ...then normal SD...
  1271. */
  1272. err = mmc_send_app_op_cond(host, 0, &ocr);
  1273. if (!err) {
  1274. if (mmc_attach_sd(host, ocr))
  1275. mmc_power_off(host);
  1276. goto out;
  1277. }
  1278. /*
  1279. * ...and finally MMC.
  1280. */
  1281. err = mmc_send_op_cond(host, 0, &ocr);
  1282. if (!err) {
  1283. if (mmc_attach_mmc(host, ocr))
  1284. mmc_power_off(host);
  1285. goto out;
  1286. }
  1287. out_fail:
  1288. mmc_release_host(host);
  1289. mmc_power_off(host);
  1290. }
  1291. out:
  1292. if (host->caps & MMC_CAP_NEEDS_POLL)
  1293. mmc_schedule_delayed_work(&host->detect, HZ);
  1294. }
  1295. void mmc_start_host(struct mmc_host *host)
  1296. {
  1297. mmc_power_off(host);
  1298. mmc_detect_change(host, 0);
  1299. }
  1300. void mmc_stop_host(struct mmc_host *host)
  1301. {
  1302. #ifdef CONFIG_MMC_DEBUG
  1303. unsigned long flags;
  1304. spin_lock_irqsave(&host->lock, flags);
  1305. host->removed = 1;
  1306. spin_unlock_irqrestore(&host->lock, flags);
  1307. #endif
  1308. if (host->caps & MMC_CAP_DISABLE)
  1309. cancel_delayed_work(&host->disable);
  1310. cancel_delayed_work(&host->detect);
  1311. mmc_flush_scheduled_work();
  1312. /* clear pm flags now and let card drivers set them as needed */
  1313. host->pm_flags = 0;
  1314. mmc_bus_get(host);
  1315. if (host->bus_ops && !host->bus_dead) {
  1316. if (host->bus_ops->remove)
  1317. host->bus_ops->remove(host);
  1318. mmc_claim_host(host);
  1319. mmc_detach_bus(host);
  1320. mmc_release_host(host);
  1321. mmc_bus_put(host);
  1322. return;
  1323. }
  1324. mmc_bus_put(host);
  1325. BUG_ON(host->card);
  1326. mmc_power_off(host);
  1327. }
  1328. void mmc_power_save_host(struct mmc_host *host)
  1329. {
  1330. mmc_bus_get(host);
  1331. if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
  1332. mmc_bus_put(host);
  1333. return;
  1334. }
  1335. if (host->bus_ops->power_save)
  1336. host->bus_ops->power_save(host);
  1337. mmc_bus_put(host);
  1338. mmc_power_off(host);
  1339. }
  1340. EXPORT_SYMBOL(mmc_power_save_host);
  1341. void mmc_power_restore_host(struct mmc_host *host)
  1342. {
  1343. mmc_bus_get(host);
  1344. if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
  1345. mmc_bus_put(host);
  1346. return;
  1347. }
  1348. mmc_power_up(host);
  1349. host->bus_ops->power_restore(host);
  1350. mmc_bus_put(host);
  1351. }
  1352. EXPORT_SYMBOL(mmc_power_restore_host);
  1353. int mmc_card_awake(struct mmc_host *host)
  1354. {
  1355. int err = -ENOSYS;
  1356. mmc_bus_get(host);
  1357. if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
  1358. err = host->bus_ops->awake(host);
  1359. mmc_bus_put(host);
  1360. return err;
  1361. }
  1362. EXPORT_SYMBOL(mmc_card_awake);
  1363. int mmc_card_sleep(struct mmc_host *host)
  1364. {
  1365. int err = -ENOSYS;
  1366. mmc_bus_get(host);
  1367. if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
  1368. err = host->bus_ops->sleep(host);
  1369. mmc_bus_put(host);
  1370. return err;
  1371. }
  1372. EXPORT_SYMBOL(mmc_card_sleep);
  1373. int mmc_card_can_sleep(struct mmc_host *host)
  1374. {
  1375. struct mmc_card *card = host->card;
  1376. if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
  1377. return 1;
  1378. return 0;
  1379. }
  1380. EXPORT_SYMBOL(mmc_card_can_sleep);
  1381. #ifdef CONFIG_PM
  1382. /**
  1383. * mmc_suspend_host - suspend a host
  1384. * @host: mmc host
  1385. */
  1386. int mmc_suspend_host(struct mmc_host *host)
  1387. {
  1388. int err = 0;
  1389. if (host->caps & MMC_CAP_DISABLE)
  1390. cancel_delayed_work(&host->disable);
  1391. cancel_delayed_work(&host->detect);
  1392. mmc_flush_scheduled_work();
  1393. mmc_bus_get(host);
  1394. if (host->bus_ops && !host->bus_dead) {
  1395. if (host->bus_ops->suspend)
  1396. err = host->bus_ops->suspend(host);
  1397. if (err == -ENOSYS || !host->bus_ops->resume) {
  1398. /*
  1399. * We simply "remove" the card in this case.
  1400. * It will be redetected on resume.
  1401. */
  1402. if (host->bus_ops->remove)
  1403. host->bus_ops->remove(host);
  1404. mmc_claim_host(host);
  1405. mmc_detach_bus(host);
  1406. mmc_release_host(host);
  1407. host->pm_flags = 0;
  1408. err = 0;
  1409. }
  1410. }
  1411. mmc_bus_put(host);
  1412. if (!err && !(host->pm_flags & MMC_PM_KEEP_POWER))
  1413. mmc_power_off(host);
  1414. return err;
  1415. }
  1416. EXPORT_SYMBOL(mmc_suspend_host);
  1417. /**
  1418. * mmc_resume_host - resume a previously suspended host
  1419. * @host: mmc host
  1420. */
  1421. int mmc_resume_host(struct mmc_host *host)
  1422. {
  1423. int err = 0;
  1424. mmc_bus_get(host);
  1425. if (host->bus_ops && !host->bus_dead) {
  1426. if (!(host->pm_flags & MMC_PM_KEEP_POWER)) {
  1427. mmc_power_up(host);
  1428. mmc_select_voltage(host, host->ocr);
  1429. }
  1430. BUG_ON(!host->bus_ops->resume);
  1431. err = host->bus_ops->resume(host);
  1432. if (err) {
  1433. printk(KERN_WARNING "%s: error %d during resume "
  1434. "(card was removed?)\n",
  1435. mmc_hostname(host), err);
  1436. err = 0;
  1437. }
  1438. }
  1439. mmc_bus_put(host);
  1440. return err;
  1441. }
  1442. EXPORT_SYMBOL(mmc_resume_host);
  1443. /* Do the card removal on suspend if card is assumed removeable
  1444. * Do that in pm notifier while userspace isn't yet frozen, so we will be able
  1445. to sync the card.
  1446. */
  1447. int mmc_pm_notify(struct notifier_block *notify_block,
  1448. unsigned long mode, void *unused)
  1449. {
  1450. struct mmc_host *host = container_of(
  1451. notify_block, struct mmc_host, pm_notify);
  1452. unsigned long flags;
  1453. switch (mode) {
  1454. case PM_HIBERNATION_PREPARE:
  1455. case PM_SUSPEND_PREPARE:
  1456. spin_lock_irqsave(&host->lock, flags);
  1457. host->rescan_disable = 1;
  1458. spin_unlock_irqrestore(&host->lock, flags);
  1459. cancel_delayed_work_sync(&host->detect);
  1460. if (!host->bus_ops || host->bus_ops->suspend)
  1461. break;
  1462. mmc_claim_host(host);
  1463. if (host->bus_ops->remove)
  1464. host->bus_ops->remove(host);
  1465. mmc_detach_bus(host);
  1466. mmc_release_host(host);
  1467. host->pm_flags = 0;
  1468. break;
  1469. case PM_POST_SUSPEND:
  1470. case PM_POST_HIBERNATION:
  1471. spin_lock_irqsave(&host->lock, flags);
  1472. host->rescan_disable = 0;
  1473. spin_unlock_irqrestore(&host->lock, flags);
  1474. mmc_detect_change(host, 0);
  1475. }
  1476. return 0;
  1477. }
  1478. #endif
  1479. static int __init mmc_init(void)
  1480. {
  1481. int ret;
  1482. workqueue = create_singlethread_workqueue("kmmcd");
  1483. if (!workqueue)
  1484. return -ENOMEM;
  1485. ret = mmc_register_bus();
  1486. if (ret)
  1487. goto destroy_workqueue;
  1488. ret = mmc_register_host_class();
  1489. if (ret)
  1490. goto unregister_bus;
  1491. ret = sdio_register_bus();
  1492. if (ret)
  1493. goto unregister_host_class;
  1494. return 0;
  1495. unregister_host_class:
  1496. mmc_unregister_host_class();
  1497. unregister_bus:
  1498. mmc_unregister_bus();
  1499. destroy_workqueue:
  1500. destroy_workqueue(workqueue);
  1501. return ret;
  1502. }
  1503. static void __exit mmc_exit(void)
  1504. {
  1505. sdio_unregister_bus();
  1506. mmc_unregister_host_class();
  1507. mmc_unregister_bus();
  1508. destroy_workqueue(workqueue);
  1509. }
  1510. subsys_initcall(mmc_init);
  1511. module_exit(mmc_exit);
  1512. MODULE_LICENSE("GPL");