core.c 41 KB

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