core.c 42 KB

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