core.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474
  1. /*
  2. * linux/drivers/mmc/core/core.c
  3. *
  4. * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
  5. * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
  6. * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
  7. * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/completion.h>
  17. #include <linux/device.h>
  18. #include <linux/delay.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/err.h>
  21. #include <linux/leds.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/log2.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/suspend.h>
  27. #include <linux/fault-inject.h>
  28. #include <linux/random.h>
  29. #include <linux/mmc/card.h>
  30. #include <linux/mmc/host.h>
  31. #include <linux/mmc/mmc.h>
  32. #include <linux/mmc/sd.h>
  33. #include "core.h"
  34. #include "bus.h"
  35. #include "host.h"
  36. #include "sdio_bus.h"
  37. #include "mmc_ops.h"
  38. #include "sd_ops.h"
  39. #include "sdio_ops.h"
  40. static struct workqueue_struct *workqueue;
  41. static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
  42. /*
  43. * Enabling software CRCs on the data blocks can be a significant (30%)
  44. * performance cost, and for other reasons may not always be desired.
  45. * So we allow it it to be disabled.
  46. */
  47. bool use_spi_crc = 1;
  48. module_param(use_spi_crc, bool, 0);
  49. /*
  50. * We normally treat cards as removed during suspend if they are not
  51. * known to be on a non-removable bus, to avoid the risk of writing
  52. * back data to a different card after resume. Allow this to be
  53. * overridden if necessary.
  54. */
  55. #ifdef CONFIG_MMC_UNSAFE_RESUME
  56. bool mmc_assume_removable;
  57. #else
  58. bool mmc_assume_removable = 1;
  59. #endif
  60. EXPORT_SYMBOL(mmc_assume_removable);
  61. module_param_named(removable, mmc_assume_removable, bool, 0644);
  62. MODULE_PARM_DESC(
  63. removable,
  64. "MMC/SD cards are removable and may be removed during suspend");
  65. /*
  66. * Internal function. Schedule delayed work in the MMC work queue.
  67. */
  68. static int mmc_schedule_delayed_work(struct delayed_work *work,
  69. unsigned long delay)
  70. {
  71. return queue_delayed_work(workqueue, work, delay);
  72. }
  73. /*
  74. * Internal function. Flush all scheduled work from the MMC work queue.
  75. */
  76. static void mmc_flush_scheduled_work(void)
  77. {
  78. flush_workqueue(workqueue);
  79. }
  80. #ifdef CONFIG_FAIL_MMC_REQUEST
  81. /*
  82. * Internal function. Inject random data errors.
  83. * If mmc_data is NULL no errors are injected.
  84. */
  85. static void mmc_should_fail_request(struct mmc_host *host,
  86. struct mmc_request *mrq)
  87. {
  88. struct mmc_command *cmd = mrq->cmd;
  89. struct mmc_data *data = mrq->data;
  90. static const int data_errors[] = {
  91. -ETIMEDOUT,
  92. -EILSEQ,
  93. -EIO,
  94. };
  95. if (!data)
  96. return;
  97. if (cmd->error || data->error ||
  98. !should_fail(&host->fail_mmc_request, data->blksz * data->blocks))
  99. return;
  100. data->error = data_errors[random32() % ARRAY_SIZE(data_errors)];
  101. data->bytes_xfered = (random32() % (data->bytes_xfered >> 9)) << 9;
  102. }
  103. #else /* CONFIG_FAIL_MMC_REQUEST */
  104. static inline void mmc_should_fail_request(struct mmc_host *host,
  105. struct mmc_request *mrq)
  106. {
  107. }
  108. #endif /* CONFIG_FAIL_MMC_REQUEST */
  109. /**
  110. * mmc_request_done - finish processing an MMC request
  111. * @host: MMC host which completed request
  112. * @mrq: MMC request which request
  113. *
  114. * MMC drivers should call this function when they have completed
  115. * their processing of a request.
  116. */
  117. void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
  118. {
  119. struct mmc_command *cmd = mrq->cmd;
  120. int err = cmd->error;
  121. if (err && cmd->retries && mmc_host_is_spi(host)) {
  122. if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
  123. cmd->retries = 0;
  124. }
  125. if (err && cmd->retries && !mmc_card_removed(host->card)) {
  126. /*
  127. * Request starter must handle retries - see
  128. * mmc_wait_for_req_done().
  129. */
  130. if (mrq->done)
  131. mrq->done(mrq);
  132. } else {
  133. mmc_should_fail_request(host, mrq);
  134. led_trigger_event(host->led, LED_OFF);
  135. pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
  136. mmc_hostname(host), cmd->opcode, err,
  137. cmd->resp[0], cmd->resp[1],
  138. cmd->resp[2], cmd->resp[3]);
  139. if (mrq->data) {
  140. pr_debug("%s: %d bytes transferred: %d\n",
  141. mmc_hostname(host),
  142. mrq->data->bytes_xfered, mrq->data->error);
  143. }
  144. if (mrq->stop) {
  145. pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
  146. mmc_hostname(host), mrq->stop->opcode,
  147. mrq->stop->error,
  148. mrq->stop->resp[0], mrq->stop->resp[1],
  149. mrq->stop->resp[2], mrq->stop->resp[3]);
  150. }
  151. if (mrq->done)
  152. mrq->done(mrq);
  153. mmc_host_clk_release(host);
  154. }
  155. }
  156. EXPORT_SYMBOL(mmc_request_done);
  157. static void
  158. mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
  159. {
  160. #ifdef CONFIG_MMC_DEBUG
  161. unsigned int i, sz;
  162. struct scatterlist *sg;
  163. #endif
  164. if (mrq->sbc) {
  165. pr_debug("<%s: starting CMD%u arg %08x flags %08x>\n",
  166. mmc_hostname(host), mrq->sbc->opcode,
  167. mrq->sbc->arg, mrq->sbc->flags);
  168. }
  169. pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
  170. mmc_hostname(host), mrq->cmd->opcode,
  171. mrq->cmd->arg, mrq->cmd->flags);
  172. if (mrq->data) {
  173. pr_debug("%s: blksz %d blocks %d flags %08x "
  174. "tsac %d ms nsac %d\n",
  175. mmc_hostname(host), mrq->data->blksz,
  176. mrq->data->blocks, mrq->data->flags,
  177. mrq->data->timeout_ns / 1000000,
  178. mrq->data->timeout_clks);
  179. }
  180. if (mrq->stop) {
  181. pr_debug("%s: CMD%u arg %08x flags %08x\n",
  182. mmc_hostname(host), mrq->stop->opcode,
  183. mrq->stop->arg, mrq->stop->flags);
  184. }
  185. WARN_ON(!host->claimed);
  186. mrq->cmd->error = 0;
  187. mrq->cmd->mrq = mrq;
  188. if (mrq->data) {
  189. BUG_ON(mrq->data->blksz > host->max_blk_size);
  190. BUG_ON(mrq->data->blocks > host->max_blk_count);
  191. BUG_ON(mrq->data->blocks * mrq->data->blksz >
  192. host->max_req_size);
  193. #ifdef CONFIG_MMC_DEBUG
  194. sz = 0;
  195. for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
  196. sz += sg->length;
  197. BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
  198. #endif
  199. mrq->cmd->data = mrq->data;
  200. mrq->data->error = 0;
  201. mrq->data->mrq = mrq;
  202. if (mrq->stop) {
  203. mrq->data->stop = mrq->stop;
  204. mrq->stop->error = 0;
  205. mrq->stop->mrq = mrq;
  206. }
  207. }
  208. mmc_host_clk_hold(host);
  209. led_trigger_event(host->led, LED_FULL);
  210. host->ops->request(host, mrq);
  211. }
  212. static void mmc_wait_done(struct mmc_request *mrq)
  213. {
  214. complete(&mrq->completion);
  215. }
  216. static int __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
  217. {
  218. init_completion(&mrq->completion);
  219. mrq->done = mmc_wait_done;
  220. if (mmc_card_removed(host->card)) {
  221. mrq->cmd->error = -ENOMEDIUM;
  222. complete(&mrq->completion);
  223. return -ENOMEDIUM;
  224. }
  225. mmc_start_request(host, mrq);
  226. return 0;
  227. }
  228. static void mmc_wait_for_req_done(struct mmc_host *host,
  229. struct mmc_request *mrq)
  230. {
  231. struct mmc_command *cmd;
  232. while (1) {
  233. wait_for_completion(&mrq->completion);
  234. cmd = mrq->cmd;
  235. if (!cmd->error || !cmd->retries ||
  236. mmc_card_removed(host->card))
  237. break;
  238. pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
  239. mmc_hostname(host), cmd->opcode, cmd->error);
  240. cmd->retries--;
  241. cmd->error = 0;
  242. host->ops->request(host, mrq);
  243. }
  244. }
  245. /**
  246. * mmc_pre_req - Prepare for a new request
  247. * @host: MMC host to prepare command
  248. * @mrq: MMC request to prepare for
  249. * @is_first_req: true if there is no previous started request
  250. * that may run in parellel to this call, otherwise false
  251. *
  252. * mmc_pre_req() is called in prior to mmc_start_req() to let
  253. * host prepare for the new request. Preparation of a request may be
  254. * performed while another request is running on the host.
  255. */
  256. static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq,
  257. bool is_first_req)
  258. {
  259. if (host->ops->pre_req) {
  260. mmc_host_clk_hold(host);
  261. host->ops->pre_req(host, mrq, is_first_req);
  262. mmc_host_clk_release(host);
  263. }
  264. }
  265. /**
  266. * mmc_post_req - Post process a completed request
  267. * @host: MMC host to post process command
  268. * @mrq: MMC request to post process for
  269. * @err: Error, if non zero, clean up any resources made in pre_req
  270. *
  271. * Let the host post process a completed request. Post processing of
  272. * a request may be performed while another reuqest is running.
  273. */
  274. static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
  275. int err)
  276. {
  277. if (host->ops->post_req) {
  278. mmc_host_clk_hold(host);
  279. host->ops->post_req(host, mrq, err);
  280. mmc_host_clk_release(host);
  281. }
  282. }
  283. /**
  284. * mmc_start_req - start a non-blocking request
  285. * @host: MMC host to start command
  286. * @areq: async request to start
  287. * @error: out parameter returns 0 for success, otherwise non zero
  288. *
  289. * Start a new MMC custom command request for a host.
  290. * If there is on ongoing async request wait for completion
  291. * of that request and start the new one and return.
  292. * Does not wait for the new request to complete.
  293. *
  294. * Returns the completed request, NULL in case of none completed.
  295. * Wait for the an ongoing request (previoulsy started) to complete and
  296. * return the completed request. If there is no ongoing request, NULL
  297. * is returned without waiting. NULL is not an error condition.
  298. */
  299. struct mmc_async_req *mmc_start_req(struct mmc_host *host,
  300. struct mmc_async_req *areq, int *error)
  301. {
  302. int err = 0;
  303. int start_err = 0;
  304. struct mmc_async_req *data = host->areq;
  305. /* Prepare a new request */
  306. if (areq)
  307. mmc_pre_req(host, areq->mrq, !host->areq);
  308. if (host->areq) {
  309. mmc_wait_for_req_done(host, host->areq->mrq);
  310. err = host->areq->err_check(host->card, host->areq);
  311. }
  312. if (!err && areq)
  313. start_err = __mmc_start_req(host, areq->mrq);
  314. if (host->areq)
  315. mmc_post_req(host, host->areq->mrq, 0);
  316. /* Cancel a prepared request if it was not started. */
  317. if ((err || start_err) && areq)
  318. mmc_post_req(host, areq->mrq, -EINVAL);
  319. if (err)
  320. host->areq = NULL;
  321. else
  322. host->areq = areq;
  323. if (error)
  324. *error = err;
  325. return data;
  326. }
  327. EXPORT_SYMBOL(mmc_start_req);
  328. /**
  329. * mmc_wait_for_req - start a request and wait for completion
  330. * @host: MMC host to start command
  331. * @mrq: MMC request to start
  332. *
  333. * Start a new MMC custom command request for a host, and wait
  334. * for the command to complete. Does not attempt to parse the
  335. * response.
  336. */
  337. void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
  338. {
  339. __mmc_start_req(host, mrq);
  340. mmc_wait_for_req_done(host, mrq);
  341. }
  342. EXPORT_SYMBOL(mmc_wait_for_req);
  343. /**
  344. * mmc_interrupt_hpi - Issue for High priority Interrupt
  345. * @card: the MMC card associated with the HPI transfer
  346. *
  347. * Issued High Priority Interrupt, and check for card status
  348. * util out-of prg-state.
  349. */
  350. int mmc_interrupt_hpi(struct mmc_card *card)
  351. {
  352. int err;
  353. u32 status;
  354. unsigned long prg_wait;
  355. BUG_ON(!card);
  356. if (!card->ext_csd.hpi_en) {
  357. pr_info("%s: HPI enable bit unset\n", mmc_hostname(card->host));
  358. return 1;
  359. }
  360. mmc_claim_host(card->host);
  361. err = mmc_send_status(card, &status);
  362. if (err) {
  363. pr_err("%s: Get card status fail\n", mmc_hostname(card->host));
  364. goto out;
  365. }
  366. switch (R1_CURRENT_STATE(status)) {
  367. case R1_STATE_IDLE:
  368. case R1_STATE_READY:
  369. case R1_STATE_STBY:
  370. /*
  371. * In idle states, HPI is not needed and the caller
  372. * can issue the next intended command immediately
  373. */
  374. goto out;
  375. case R1_STATE_PRG:
  376. break;
  377. default:
  378. /* In all other states, it's illegal to issue HPI */
  379. pr_debug("%s: HPI cannot be sent. Card state=%d\n",
  380. mmc_hostname(card->host), R1_CURRENT_STATE(status));
  381. err = -EINVAL;
  382. goto out;
  383. }
  384. err = mmc_send_hpi_cmd(card, &status);
  385. if (err)
  386. goto out;
  387. prg_wait = jiffies + msecs_to_jiffies(card->ext_csd.out_of_int_time);
  388. do {
  389. err = mmc_send_status(card, &status);
  390. if (!err && R1_CURRENT_STATE(status) == R1_STATE_TRAN)
  391. break;
  392. if (time_after(jiffies, prg_wait))
  393. err = -ETIMEDOUT;
  394. } while (!err);
  395. out:
  396. mmc_release_host(card->host);
  397. return err;
  398. }
  399. EXPORT_SYMBOL(mmc_interrupt_hpi);
  400. /**
  401. * mmc_wait_for_cmd - start a command and wait for completion
  402. * @host: MMC host to start command
  403. * @cmd: MMC command to start
  404. * @retries: maximum number of retries
  405. *
  406. * Start a new MMC command for a host, and wait for the command
  407. * to complete. Return any error that occurred while the command
  408. * was executing. Do not attempt to parse the response.
  409. */
  410. int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
  411. {
  412. struct mmc_request mrq = {NULL};
  413. WARN_ON(!host->claimed);
  414. memset(cmd->resp, 0, sizeof(cmd->resp));
  415. cmd->retries = retries;
  416. mrq.cmd = cmd;
  417. cmd->data = NULL;
  418. mmc_wait_for_req(host, &mrq);
  419. return cmd->error;
  420. }
  421. EXPORT_SYMBOL(mmc_wait_for_cmd);
  422. /**
  423. * mmc_set_data_timeout - set the timeout for a data command
  424. * @data: data phase for command
  425. * @card: the MMC card associated with the data transfer
  426. *
  427. * Computes the data timeout parameters according to the
  428. * correct algorithm given the card type.
  429. */
  430. void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
  431. {
  432. unsigned int mult;
  433. /*
  434. * SDIO cards only define an upper 1 s limit on access.
  435. */
  436. if (mmc_card_sdio(card)) {
  437. data->timeout_ns = 1000000000;
  438. data->timeout_clks = 0;
  439. return;
  440. }
  441. /*
  442. * SD cards use a 100 multiplier rather than 10
  443. */
  444. mult = mmc_card_sd(card) ? 100 : 10;
  445. /*
  446. * Scale up the multiplier (and therefore the timeout) by
  447. * the r2w factor for writes.
  448. */
  449. if (data->flags & MMC_DATA_WRITE)
  450. mult <<= card->csd.r2w_factor;
  451. data->timeout_ns = card->csd.tacc_ns * mult;
  452. data->timeout_clks = card->csd.tacc_clks * mult;
  453. /*
  454. * SD cards also have an upper limit on the timeout.
  455. */
  456. if (mmc_card_sd(card)) {
  457. unsigned int timeout_us, limit_us;
  458. timeout_us = data->timeout_ns / 1000;
  459. if (mmc_host_clk_rate(card->host))
  460. timeout_us += data->timeout_clks * 1000 /
  461. (mmc_host_clk_rate(card->host) / 1000);
  462. if (data->flags & MMC_DATA_WRITE)
  463. /*
  464. * The MMC spec "It is strongly recommended
  465. * for hosts to implement more than 500ms
  466. * timeout value even if the card indicates
  467. * the 250ms maximum busy length." Even the
  468. * previous value of 300ms is known to be
  469. * insufficient for some cards.
  470. */
  471. limit_us = 3000000;
  472. else
  473. limit_us = 100000;
  474. /*
  475. * SDHC cards always use these fixed values.
  476. */
  477. if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
  478. data->timeout_ns = limit_us * 1000;
  479. data->timeout_clks = 0;
  480. }
  481. }
  482. /*
  483. * Some cards require longer data read timeout than indicated in CSD.
  484. * Address this by setting the read timeout to a "reasonably high"
  485. * value. For the cards tested, 300ms has proven enough. If necessary,
  486. * this value can be increased if other problematic cards require this.
  487. */
  488. if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) {
  489. data->timeout_ns = 300000000;
  490. data->timeout_clks = 0;
  491. }
  492. /*
  493. * Some cards need very high timeouts if driven in SPI mode.
  494. * The worst observed timeout was 900ms after writing a
  495. * continuous stream of data until the internal logic
  496. * overflowed.
  497. */
  498. if (mmc_host_is_spi(card->host)) {
  499. if (data->flags & MMC_DATA_WRITE) {
  500. if (data->timeout_ns < 1000000000)
  501. data->timeout_ns = 1000000000; /* 1s */
  502. } else {
  503. if (data->timeout_ns < 100000000)
  504. data->timeout_ns = 100000000; /* 100ms */
  505. }
  506. }
  507. }
  508. EXPORT_SYMBOL(mmc_set_data_timeout);
  509. /**
  510. * mmc_align_data_size - pads a transfer size to a more optimal value
  511. * @card: the MMC card associated with the data transfer
  512. * @sz: original transfer size
  513. *
  514. * Pads the original data size with a number of extra bytes in
  515. * order to avoid controller bugs and/or performance hits
  516. * (e.g. some controllers revert to PIO for certain sizes).
  517. *
  518. * Returns the improved size, which might be unmodified.
  519. *
  520. * Note that this function is only relevant when issuing a
  521. * single scatter gather entry.
  522. */
  523. unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
  524. {
  525. /*
  526. * FIXME: We don't have a system for the controller to tell
  527. * the core about its problems yet, so for now we just 32-bit
  528. * align the size.
  529. */
  530. sz = ((sz + 3) / 4) * 4;
  531. return sz;
  532. }
  533. EXPORT_SYMBOL(mmc_align_data_size);
  534. /**
  535. * __mmc_claim_host - exclusively claim a host
  536. * @host: mmc host to claim
  537. * @abort: whether or not the operation should be aborted
  538. *
  539. * Claim a host for a set of operations. If @abort is non null and
  540. * dereference a non-zero value then this will return prematurely with
  541. * that non-zero value without acquiring the lock. Returns zero
  542. * with the lock held otherwise.
  543. */
  544. int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
  545. {
  546. DECLARE_WAITQUEUE(wait, current);
  547. unsigned long flags;
  548. int stop;
  549. might_sleep();
  550. add_wait_queue(&host->wq, &wait);
  551. spin_lock_irqsave(&host->lock, flags);
  552. while (1) {
  553. set_current_state(TASK_UNINTERRUPTIBLE);
  554. stop = abort ? atomic_read(abort) : 0;
  555. if (stop || !host->claimed || host->claimer == current)
  556. break;
  557. spin_unlock_irqrestore(&host->lock, flags);
  558. schedule();
  559. spin_lock_irqsave(&host->lock, flags);
  560. }
  561. set_current_state(TASK_RUNNING);
  562. if (!stop) {
  563. host->claimed = 1;
  564. host->claimer = current;
  565. host->claim_cnt += 1;
  566. } else
  567. wake_up(&host->wq);
  568. spin_unlock_irqrestore(&host->lock, flags);
  569. remove_wait_queue(&host->wq, &wait);
  570. if (host->ops->enable && !stop && host->claim_cnt == 1)
  571. host->ops->enable(host);
  572. return stop;
  573. }
  574. EXPORT_SYMBOL(__mmc_claim_host);
  575. /**
  576. * mmc_try_claim_host - try exclusively to claim a host
  577. * @host: mmc host to claim
  578. *
  579. * Returns %1 if the host is claimed, %0 otherwise.
  580. */
  581. int mmc_try_claim_host(struct mmc_host *host)
  582. {
  583. int claimed_host = 0;
  584. unsigned long flags;
  585. spin_lock_irqsave(&host->lock, flags);
  586. if (!host->claimed || host->claimer == current) {
  587. host->claimed = 1;
  588. host->claimer = current;
  589. host->claim_cnt += 1;
  590. claimed_host = 1;
  591. }
  592. spin_unlock_irqrestore(&host->lock, flags);
  593. if (host->ops->enable && claimed_host && host->claim_cnt == 1)
  594. host->ops->enable(host);
  595. return claimed_host;
  596. }
  597. EXPORT_SYMBOL(mmc_try_claim_host);
  598. /**
  599. * mmc_release_host - release a host
  600. * @host: mmc host to release
  601. *
  602. * Release a MMC host, allowing others to claim the host
  603. * for their operations.
  604. */
  605. void mmc_release_host(struct mmc_host *host)
  606. {
  607. unsigned long flags;
  608. WARN_ON(!host->claimed);
  609. if (host->ops->disable && host->claim_cnt == 1)
  610. host->ops->disable(host);
  611. spin_lock_irqsave(&host->lock, flags);
  612. if (--host->claim_cnt) {
  613. /* Release for nested claim */
  614. spin_unlock_irqrestore(&host->lock, flags);
  615. } else {
  616. host->claimed = 0;
  617. host->claimer = NULL;
  618. spin_unlock_irqrestore(&host->lock, flags);
  619. wake_up(&host->wq);
  620. }
  621. }
  622. EXPORT_SYMBOL(mmc_release_host);
  623. /*
  624. * Internal function that does the actual ios call to the host driver,
  625. * optionally printing some debug output.
  626. */
  627. static inline void mmc_set_ios(struct mmc_host *host)
  628. {
  629. struct mmc_ios *ios = &host->ios;
  630. pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
  631. "width %u timing %u\n",
  632. mmc_hostname(host), ios->clock, ios->bus_mode,
  633. ios->power_mode, ios->chip_select, ios->vdd,
  634. ios->bus_width, ios->timing);
  635. if (ios->clock > 0)
  636. mmc_set_ungated(host);
  637. host->ops->set_ios(host, ios);
  638. }
  639. /*
  640. * Control chip select pin on a host.
  641. */
  642. void mmc_set_chip_select(struct mmc_host *host, int mode)
  643. {
  644. mmc_host_clk_hold(host);
  645. host->ios.chip_select = mode;
  646. mmc_set_ios(host);
  647. mmc_host_clk_release(host);
  648. }
  649. /*
  650. * Sets the host clock to the highest possible frequency that
  651. * is below "hz".
  652. */
  653. static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
  654. {
  655. WARN_ON(hz < host->f_min);
  656. if (hz > host->f_max)
  657. hz = host->f_max;
  658. host->ios.clock = hz;
  659. mmc_set_ios(host);
  660. }
  661. void mmc_set_clock(struct mmc_host *host, unsigned int hz)
  662. {
  663. mmc_host_clk_hold(host);
  664. __mmc_set_clock(host, hz);
  665. mmc_host_clk_release(host);
  666. }
  667. #ifdef CONFIG_MMC_CLKGATE
  668. /*
  669. * This gates the clock by setting it to 0 Hz.
  670. */
  671. void mmc_gate_clock(struct mmc_host *host)
  672. {
  673. unsigned long flags;
  674. spin_lock_irqsave(&host->clk_lock, flags);
  675. host->clk_old = host->ios.clock;
  676. host->ios.clock = 0;
  677. host->clk_gated = true;
  678. spin_unlock_irqrestore(&host->clk_lock, flags);
  679. mmc_set_ios(host);
  680. }
  681. /*
  682. * This restores the clock from gating by using the cached
  683. * clock value.
  684. */
  685. void mmc_ungate_clock(struct mmc_host *host)
  686. {
  687. /*
  688. * We should previously have gated the clock, so the clock shall
  689. * be 0 here! The clock may however be 0 during initialization,
  690. * when some request operations are performed before setting
  691. * the frequency. When ungate is requested in that situation
  692. * we just ignore the call.
  693. */
  694. if (host->clk_old) {
  695. BUG_ON(host->ios.clock);
  696. /* This call will also set host->clk_gated to false */
  697. __mmc_set_clock(host, host->clk_old);
  698. }
  699. }
  700. void mmc_set_ungated(struct mmc_host *host)
  701. {
  702. unsigned long flags;
  703. /*
  704. * We've been given a new frequency while the clock is gated,
  705. * so make sure we regard this as ungating it.
  706. */
  707. spin_lock_irqsave(&host->clk_lock, flags);
  708. host->clk_gated = false;
  709. spin_unlock_irqrestore(&host->clk_lock, flags);
  710. }
  711. #else
  712. void mmc_set_ungated(struct mmc_host *host)
  713. {
  714. }
  715. #endif
  716. /*
  717. * Change the bus mode (open drain/push-pull) of a host.
  718. */
  719. void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
  720. {
  721. mmc_host_clk_hold(host);
  722. host->ios.bus_mode = mode;
  723. mmc_set_ios(host);
  724. mmc_host_clk_release(host);
  725. }
  726. /*
  727. * Change data bus width of a host.
  728. */
  729. void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
  730. {
  731. mmc_host_clk_hold(host);
  732. host->ios.bus_width = width;
  733. mmc_set_ios(host);
  734. mmc_host_clk_release(host);
  735. }
  736. /**
  737. * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
  738. * @vdd: voltage (mV)
  739. * @low_bits: prefer low bits in boundary cases
  740. *
  741. * This function returns the OCR bit number according to the provided @vdd
  742. * value. If conversion is not possible a negative errno value returned.
  743. *
  744. * Depending on the @low_bits flag the function prefers low or high OCR bits
  745. * on boundary voltages. For example,
  746. * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
  747. * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
  748. *
  749. * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
  750. */
  751. static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
  752. {
  753. const int max_bit = ilog2(MMC_VDD_35_36);
  754. int bit;
  755. if (vdd < 1650 || vdd > 3600)
  756. return -EINVAL;
  757. if (vdd >= 1650 && vdd <= 1950)
  758. return ilog2(MMC_VDD_165_195);
  759. if (low_bits)
  760. vdd -= 1;
  761. /* Base 2000 mV, step 100 mV, bit's base 8. */
  762. bit = (vdd - 2000) / 100 + 8;
  763. if (bit > max_bit)
  764. return max_bit;
  765. return bit;
  766. }
  767. /**
  768. * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
  769. * @vdd_min: minimum voltage value (mV)
  770. * @vdd_max: maximum voltage value (mV)
  771. *
  772. * This function returns the OCR mask bits according to the provided @vdd_min
  773. * and @vdd_max values. If conversion is not possible the function returns 0.
  774. *
  775. * Notes wrt boundary cases:
  776. * This function sets the OCR bits for all boundary voltages, for example
  777. * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
  778. * MMC_VDD_34_35 mask.
  779. */
  780. u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
  781. {
  782. u32 mask = 0;
  783. if (vdd_max < vdd_min)
  784. return 0;
  785. /* Prefer high bits for the boundary vdd_max values. */
  786. vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
  787. if (vdd_max < 0)
  788. return 0;
  789. /* Prefer low bits for the boundary vdd_min values. */
  790. vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
  791. if (vdd_min < 0)
  792. return 0;
  793. /* Fill the mask, from max bit to min bit. */
  794. while (vdd_max >= vdd_min)
  795. mask |= 1 << vdd_max--;
  796. return mask;
  797. }
  798. EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
  799. #ifdef CONFIG_REGULATOR
  800. /**
  801. * mmc_regulator_get_ocrmask - return mask of supported voltages
  802. * @supply: regulator to use
  803. *
  804. * This returns either a negative errno, or a mask of voltages that
  805. * can be provided to MMC/SD/SDIO devices using the specified voltage
  806. * regulator. This would normally be called before registering the
  807. * MMC host adapter.
  808. */
  809. int mmc_regulator_get_ocrmask(struct regulator *supply)
  810. {
  811. int result = 0;
  812. int count;
  813. int i;
  814. count = regulator_count_voltages(supply);
  815. if (count < 0)
  816. return count;
  817. for (i = 0; i < count; i++) {
  818. int vdd_uV;
  819. int vdd_mV;
  820. vdd_uV = regulator_list_voltage(supply, i);
  821. if (vdd_uV <= 0)
  822. continue;
  823. vdd_mV = vdd_uV / 1000;
  824. result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
  825. }
  826. return result;
  827. }
  828. EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
  829. /**
  830. * mmc_regulator_set_ocr - set regulator to match host->ios voltage
  831. * @mmc: the host to regulate
  832. * @supply: regulator to use
  833. * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
  834. *
  835. * Returns zero on success, else negative errno.
  836. *
  837. * MMC host drivers may use this to enable or disable a regulator using
  838. * a particular supply voltage. This would normally be called from the
  839. * set_ios() method.
  840. */
  841. int mmc_regulator_set_ocr(struct mmc_host *mmc,
  842. struct regulator *supply,
  843. unsigned short vdd_bit)
  844. {
  845. int result = 0;
  846. int min_uV, max_uV;
  847. if (vdd_bit) {
  848. int tmp;
  849. int voltage;
  850. /* REVISIT mmc_vddrange_to_ocrmask() may have set some
  851. * bits this regulator doesn't quite support ... don't
  852. * be too picky, most cards and regulators are OK with
  853. * a 0.1V range goof (it's a small error percentage).
  854. */
  855. tmp = vdd_bit - ilog2(MMC_VDD_165_195);
  856. if (tmp == 0) {
  857. min_uV = 1650 * 1000;
  858. max_uV = 1950 * 1000;
  859. } else {
  860. min_uV = 1900 * 1000 + tmp * 100 * 1000;
  861. max_uV = min_uV + 100 * 1000;
  862. }
  863. /* avoid needless changes to this voltage; the regulator
  864. * might not allow this operation
  865. */
  866. voltage = regulator_get_voltage(supply);
  867. if (mmc->caps2 & MMC_CAP2_BROKEN_VOLTAGE)
  868. min_uV = max_uV = voltage;
  869. if (voltage < 0)
  870. result = voltage;
  871. else if (voltage < min_uV || voltage > max_uV)
  872. result = regulator_set_voltage(supply, min_uV, max_uV);
  873. else
  874. result = 0;
  875. if (result == 0 && !mmc->regulator_enabled) {
  876. result = regulator_enable(supply);
  877. if (!result)
  878. mmc->regulator_enabled = true;
  879. }
  880. } else if (mmc->regulator_enabled) {
  881. result = regulator_disable(supply);
  882. if (result == 0)
  883. mmc->regulator_enabled = false;
  884. }
  885. if (result)
  886. dev_err(mmc_dev(mmc),
  887. "could not set regulator OCR (%d)\n", result);
  888. return result;
  889. }
  890. EXPORT_SYMBOL(mmc_regulator_set_ocr);
  891. #endif /* CONFIG_REGULATOR */
  892. /*
  893. * Mask off any voltages we don't support and select
  894. * the lowest voltage
  895. */
  896. u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
  897. {
  898. int bit;
  899. ocr &= host->ocr_avail;
  900. bit = ffs(ocr);
  901. if (bit) {
  902. bit -= 1;
  903. ocr &= 3 << bit;
  904. mmc_host_clk_hold(host);
  905. host->ios.vdd = bit;
  906. mmc_set_ios(host);
  907. mmc_host_clk_release(host);
  908. } else {
  909. pr_warning("%s: host doesn't support card's voltages\n",
  910. mmc_hostname(host));
  911. ocr = 0;
  912. }
  913. return ocr;
  914. }
  915. int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11)
  916. {
  917. struct mmc_command cmd = {0};
  918. int err = 0;
  919. BUG_ON(!host);
  920. /*
  921. * Send CMD11 only if the request is to switch the card to
  922. * 1.8V signalling.
  923. */
  924. if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) {
  925. cmd.opcode = SD_SWITCH_VOLTAGE;
  926. cmd.arg = 0;
  927. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  928. err = mmc_wait_for_cmd(host, &cmd, 0);
  929. if (err)
  930. return err;
  931. if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
  932. return -EIO;
  933. }
  934. host->ios.signal_voltage = signal_voltage;
  935. if (host->ops->start_signal_voltage_switch) {
  936. mmc_host_clk_hold(host);
  937. err = host->ops->start_signal_voltage_switch(host, &host->ios);
  938. mmc_host_clk_release(host);
  939. }
  940. return err;
  941. }
  942. /*
  943. * Select timing parameters for host.
  944. */
  945. void mmc_set_timing(struct mmc_host *host, unsigned int timing)
  946. {
  947. mmc_host_clk_hold(host);
  948. host->ios.timing = timing;
  949. mmc_set_ios(host);
  950. mmc_host_clk_release(host);
  951. }
  952. /*
  953. * Select appropriate driver type for host.
  954. */
  955. void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
  956. {
  957. mmc_host_clk_hold(host);
  958. host->ios.drv_type = drv_type;
  959. mmc_set_ios(host);
  960. mmc_host_clk_release(host);
  961. }
  962. static void mmc_poweroff_notify(struct mmc_host *host)
  963. {
  964. struct mmc_card *card;
  965. unsigned int timeout;
  966. unsigned int notify_type = EXT_CSD_NO_POWER_NOTIFICATION;
  967. int err = 0;
  968. card = host->card;
  969. mmc_claim_host(host);
  970. /*
  971. * Send power notify command only if card
  972. * is mmc and notify state is powered ON
  973. */
  974. if (card && mmc_card_mmc(card) &&
  975. (card->poweroff_notify_state == MMC_POWERED_ON)) {
  976. if (host->power_notify_type == MMC_HOST_PW_NOTIFY_SHORT) {
  977. notify_type = EXT_CSD_POWER_OFF_SHORT;
  978. timeout = card->ext_csd.generic_cmd6_time;
  979. card->poweroff_notify_state = MMC_POWEROFF_SHORT;
  980. } else {
  981. notify_type = EXT_CSD_POWER_OFF_LONG;
  982. timeout = card->ext_csd.power_off_longtime;
  983. card->poweroff_notify_state = MMC_POWEROFF_LONG;
  984. }
  985. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  986. EXT_CSD_POWER_OFF_NOTIFICATION,
  987. notify_type, timeout);
  988. if (err && err != -EBADMSG)
  989. pr_err("Device failed to respond within %d poweroff "
  990. "time. Forcefully powering down the device\n",
  991. timeout);
  992. /* Set the card state to no notification after the poweroff */
  993. card->poweroff_notify_state = MMC_NO_POWER_NOTIFICATION;
  994. }
  995. mmc_release_host(host);
  996. }
  997. /*
  998. * Apply power to the MMC stack. This is a two-stage process.
  999. * First, we enable power to the card without the clock running.
  1000. * We then wait a bit for the power to stabilise. Finally,
  1001. * enable the bus drivers and clock to the card.
  1002. *
  1003. * We must _NOT_ enable the clock prior to power stablising.
  1004. *
  1005. * If a host does all the power sequencing itself, ignore the
  1006. * initial MMC_POWER_UP stage.
  1007. */
  1008. static void mmc_power_up(struct mmc_host *host)
  1009. {
  1010. int bit;
  1011. if (host->ios.power_mode == MMC_POWER_ON)
  1012. return;
  1013. mmc_host_clk_hold(host);
  1014. /* If ocr is set, we use it */
  1015. if (host->ocr)
  1016. bit = ffs(host->ocr) - 1;
  1017. else
  1018. bit = fls(host->ocr_avail) - 1;
  1019. host->ios.vdd = bit;
  1020. if (mmc_host_is_spi(host))
  1021. host->ios.chip_select = MMC_CS_HIGH;
  1022. else
  1023. host->ios.chip_select = MMC_CS_DONTCARE;
  1024. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  1025. host->ios.power_mode = MMC_POWER_UP;
  1026. host->ios.bus_width = MMC_BUS_WIDTH_1;
  1027. host->ios.timing = MMC_TIMING_LEGACY;
  1028. mmc_set_ios(host);
  1029. /*
  1030. * This delay should be sufficient to allow the power supply
  1031. * to reach the minimum voltage.
  1032. */
  1033. mmc_delay(10);
  1034. host->ios.clock = host->f_init;
  1035. host->ios.power_mode = MMC_POWER_ON;
  1036. mmc_set_ios(host);
  1037. /*
  1038. * This delay must be at least 74 clock sizes, or 1 ms, or the
  1039. * time required to reach a stable voltage.
  1040. */
  1041. mmc_delay(10);
  1042. mmc_host_clk_release(host);
  1043. }
  1044. void mmc_power_off(struct mmc_host *host)
  1045. {
  1046. int err = 0;
  1047. if (host->ios.power_mode == MMC_POWER_OFF)
  1048. return;
  1049. mmc_host_clk_hold(host);
  1050. host->ios.clock = 0;
  1051. host->ios.vdd = 0;
  1052. /*
  1053. * For eMMC 4.5 device send AWAKE command before
  1054. * POWER_OFF_NOTIFY command, because in sleep state
  1055. * eMMC 4.5 devices respond to only RESET and AWAKE cmd
  1056. */
  1057. if (host->card && mmc_card_is_sleep(host->card) &&
  1058. host->bus_ops->resume) {
  1059. err = host->bus_ops->resume(host);
  1060. if (!err)
  1061. mmc_poweroff_notify(host);
  1062. else
  1063. pr_warning("%s: error %d during resume "
  1064. "(continue with poweroff sequence)\n",
  1065. mmc_hostname(host), err);
  1066. }
  1067. /*
  1068. * Reset ocr mask to be the highest possible voltage supported for
  1069. * this mmc host. This value will be used at next power up.
  1070. */
  1071. host->ocr = 1 << (fls(host->ocr_avail) - 1);
  1072. if (!mmc_host_is_spi(host)) {
  1073. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  1074. host->ios.chip_select = MMC_CS_DONTCARE;
  1075. }
  1076. host->ios.power_mode = MMC_POWER_OFF;
  1077. host->ios.bus_width = MMC_BUS_WIDTH_1;
  1078. host->ios.timing = MMC_TIMING_LEGACY;
  1079. mmc_set_ios(host);
  1080. /*
  1081. * Some configurations, such as the 802.11 SDIO card in the OLPC
  1082. * XO-1.5, require a short delay after poweroff before the card
  1083. * can be successfully turned on again.
  1084. */
  1085. mmc_delay(1);
  1086. mmc_host_clk_release(host);
  1087. }
  1088. /*
  1089. * Cleanup when the last reference to the bus operator is dropped.
  1090. */
  1091. static void __mmc_release_bus(struct mmc_host *host)
  1092. {
  1093. BUG_ON(!host);
  1094. BUG_ON(host->bus_refs);
  1095. BUG_ON(!host->bus_dead);
  1096. host->bus_ops = NULL;
  1097. }
  1098. /*
  1099. * Increase reference count of bus operator
  1100. */
  1101. static inline void mmc_bus_get(struct mmc_host *host)
  1102. {
  1103. unsigned long flags;
  1104. spin_lock_irqsave(&host->lock, flags);
  1105. host->bus_refs++;
  1106. spin_unlock_irqrestore(&host->lock, flags);
  1107. }
  1108. /*
  1109. * Decrease reference count of bus operator and free it if
  1110. * it is the last reference.
  1111. */
  1112. static inline void mmc_bus_put(struct mmc_host *host)
  1113. {
  1114. unsigned long flags;
  1115. spin_lock_irqsave(&host->lock, flags);
  1116. host->bus_refs--;
  1117. if ((host->bus_refs == 0) && host->bus_ops)
  1118. __mmc_release_bus(host);
  1119. spin_unlock_irqrestore(&host->lock, flags);
  1120. }
  1121. /*
  1122. * Assign a mmc bus handler to a host. Only one bus handler may control a
  1123. * host at any given time.
  1124. */
  1125. void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
  1126. {
  1127. unsigned long flags;
  1128. BUG_ON(!host);
  1129. BUG_ON(!ops);
  1130. WARN_ON(!host->claimed);
  1131. spin_lock_irqsave(&host->lock, flags);
  1132. BUG_ON(host->bus_ops);
  1133. BUG_ON(host->bus_refs);
  1134. host->bus_ops = ops;
  1135. host->bus_refs = 1;
  1136. host->bus_dead = 0;
  1137. spin_unlock_irqrestore(&host->lock, flags);
  1138. }
  1139. /*
  1140. * Remove the current bus handler from a host.
  1141. */
  1142. void mmc_detach_bus(struct mmc_host *host)
  1143. {
  1144. unsigned long flags;
  1145. BUG_ON(!host);
  1146. WARN_ON(!host->claimed);
  1147. WARN_ON(!host->bus_ops);
  1148. spin_lock_irqsave(&host->lock, flags);
  1149. host->bus_dead = 1;
  1150. spin_unlock_irqrestore(&host->lock, flags);
  1151. mmc_bus_put(host);
  1152. }
  1153. /**
  1154. * mmc_detect_change - process change of state on a MMC socket
  1155. * @host: host which changed state.
  1156. * @delay: optional delay to wait before detection (jiffies)
  1157. *
  1158. * MMC drivers should call this when they detect a card has been
  1159. * inserted or removed. The MMC layer will confirm that any
  1160. * present card is still functional, and initialize any newly
  1161. * inserted.
  1162. */
  1163. void mmc_detect_change(struct mmc_host *host, unsigned long delay)
  1164. {
  1165. #ifdef CONFIG_MMC_DEBUG
  1166. unsigned long flags;
  1167. spin_lock_irqsave(&host->lock, flags);
  1168. WARN_ON(host->removed);
  1169. spin_unlock_irqrestore(&host->lock, flags);
  1170. #endif
  1171. host->detect_change = 1;
  1172. mmc_schedule_delayed_work(&host->detect, delay);
  1173. }
  1174. EXPORT_SYMBOL(mmc_detect_change);
  1175. void mmc_init_erase(struct mmc_card *card)
  1176. {
  1177. unsigned int sz;
  1178. if (is_power_of_2(card->erase_size))
  1179. card->erase_shift = ffs(card->erase_size) - 1;
  1180. else
  1181. card->erase_shift = 0;
  1182. /*
  1183. * It is possible to erase an arbitrarily large area of an SD or MMC
  1184. * card. That is not desirable because it can take a long time
  1185. * (minutes) potentially delaying more important I/O, and also the
  1186. * timeout calculations become increasingly hugely over-estimated.
  1187. * Consequently, 'pref_erase' is defined as a guide to limit erases
  1188. * to that size and alignment.
  1189. *
  1190. * For SD cards that define Allocation Unit size, limit erases to one
  1191. * Allocation Unit at a time. For MMC cards that define High Capacity
  1192. * Erase Size, whether it is switched on or not, limit to that size.
  1193. * Otherwise just have a stab at a good value. For modern cards it
  1194. * will end up being 4MiB. Note that if the value is too small, it
  1195. * can end up taking longer to erase.
  1196. */
  1197. if (mmc_card_sd(card) && card->ssr.au) {
  1198. card->pref_erase = card->ssr.au;
  1199. card->erase_shift = ffs(card->ssr.au) - 1;
  1200. } else if (card->ext_csd.hc_erase_size) {
  1201. card->pref_erase = card->ext_csd.hc_erase_size;
  1202. } else {
  1203. sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
  1204. if (sz < 128)
  1205. card->pref_erase = 512 * 1024 / 512;
  1206. else if (sz < 512)
  1207. card->pref_erase = 1024 * 1024 / 512;
  1208. else if (sz < 1024)
  1209. card->pref_erase = 2 * 1024 * 1024 / 512;
  1210. else
  1211. card->pref_erase = 4 * 1024 * 1024 / 512;
  1212. if (card->pref_erase < card->erase_size)
  1213. card->pref_erase = card->erase_size;
  1214. else {
  1215. sz = card->pref_erase % card->erase_size;
  1216. if (sz)
  1217. card->pref_erase += card->erase_size - sz;
  1218. }
  1219. }
  1220. }
  1221. static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
  1222. unsigned int arg, unsigned int qty)
  1223. {
  1224. unsigned int erase_timeout;
  1225. if (arg == MMC_DISCARD_ARG ||
  1226. (arg == MMC_TRIM_ARG && card->ext_csd.rev >= 6)) {
  1227. erase_timeout = card->ext_csd.trim_timeout;
  1228. } else if (card->ext_csd.erase_group_def & 1) {
  1229. /* High Capacity Erase Group Size uses HC timeouts */
  1230. if (arg == MMC_TRIM_ARG)
  1231. erase_timeout = card->ext_csd.trim_timeout;
  1232. else
  1233. erase_timeout = card->ext_csd.hc_erase_timeout;
  1234. } else {
  1235. /* CSD Erase Group Size uses write timeout */
  1236. unsigned int mult = (10 << card->csd.r2w_factor);
  1237. unsigned int timeout_clks = card->csd.tacc_clks * mult;
  1238. unsigned int timeout_us;
  1239. /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
  1240. if (card->csd.tacc_ns < 1000000)
  1241. timeout_us = (card->csd.tacc_ns * mult) / 1000;
  1242. else
  1243. timeout_us = (card->csd.tacc_ns / 1000) * mult;
  1244. /*
  1245. * ios.clock is only a target. The real clock rate might be
  1246. * less but not that much less, so fudge it by multiplying by 2.
  1247. */
  1248. timeout_clks <<= 1;
  1249. timeout_us += (timeout_clks * 1000) /
  1250. (mmc_host_clk_rate(card->host) / 1000);
  1251. erase_timeout = timeout_us / 1000;
  1252. /*
  1253. * Theoretically, the calculation could underflow so round up
  1254. * to 1ms in that case.
  1255. */
  1256. if (!erase_timeout)
  1257. erase_timeout = 1;
  1258. }
  1259. /* Multiplier for secure operations */
  1260. if (arg & MMC_SECURE_ARGS) {
  1261. if (arg == MMC_SECURE_ERASE_ARG)
  1262. erase_timeout *= card->ext_csd.sec_erase_mult;
  1263. else
  1264. erase_timeout *= card->ext_csd.sec_trim_mult;
  1265. }
  1266. erase_timeout *= qty;
  1267. /*
  1268. * Ensure at least a 1 second timeout for SPI as per
  1269. * 'mmc_set_data_timeout()'
  1270. */
  1271. if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
  1272. erase_timeout = 1000;
  1273. return erase_timeout;
  1274. }
  1275. static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
  1276. unsigned int arg,
  1277. unsigned int qty)
  1278. {
  1279. unsigned int erase_timeout;
  1280. if (card->ssr.erase_timeout) {
  1281. /* Erase timeout specified in SD Status Register (SSR) */
  1282. erase_timeout = card->ssr.erase_timeout * qty +
  1283. card->ssr.erase_offset;
  1284. } else {
  1285. /*
  1286. * Erase timeout not specified in SD Status Register (SSR) so
  1287. * use 250ms per write block.
  1288. */
  1289. erase_timeout = 250 * qty;
  1290. }
  1291. /* Must not be less than 1 second */
  1292. if (erase_timeout < 1000)
  1293. erase_timeout = 1000;
  1294. return erase_timeout;
  1295. }
  1296. static unsigned int mmc_erase_timeout(struct mmc_card *card,
  1297. unsigned int arg,
  1298. unsigned int qty)
  1299. {
  1300. if (mmc_card_sd(card))
  1301. return mmc_sd_erase_timeout(card, arg, qty);
  1302. else
  1303. return mmc_mmc_erase_timeout(card, arg, qty);
  1304. }
  1305. static int mmc_do_erase(struct mmc_card *card, unsigned int from,
  1306. unsigned int to, unsigned int arg)
  1307. {
  1308. struct mmc_command cmd = {0};
  1309. unsigned int qty = 0;
  1310. int err;
  1311. /*
  1312. * qty is used to calculate the erase timeout which depends on how many
  1313. * erase groups (or allocation units in SD terminology) are affected.
  1314. * We count erasing part of an erase group as one erase group.
  1315. * For SD, the allocation units are always a power of 2. For MMC, the
  1316. * erase group size is almost certainly also power of 2, but it does not
  1317. * seem to insist on that in the JEDEC standard, so we fall back to
  1318. * division in that case. SD may not specify an allocation unit size,
  1319. * in which case the timeout is based on the number of write blocks.
  1320. *
  1321. * Note that the timeout for secure trim 2 will only be correct if the
  1322. * number of erase groups specified is the same as the total of all
  1323. * preceding secure trim 1 commands. Since the power may have been
  1324. * lost since the secure trim 1 commands occurred, it is generally
  1325. * impossible to calculate the secure trim 2 timeout correctly.
  1326. */
  1327. if (card->erase_shift)
  1328. qty += ((to >> card->erase_shift) -
  1329. (from >> card->erase_shift)) + 1;
  1330. else if (mmc_card_sd(card))
  1331. qty += to - from + 1;
  1332. else
  1333. qty += ((to / card->erase_size) -
  1334. (from / card->erase_size)) + 1;
  1335. if (!mmc_card_blockaddr(card)) {
  1336. from <<= 9;
  1337. to <<= 9;
  1338. }
  1339. if (mmc_card_sd(card))
  1340. cmd.opcode = SD_ERASE_WR_BLK_START;
  1341. else
  1342. cmd.opcode = MMC_ERASE_GROUP_START;
  1343. cmd.arg = from;
  1344. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1345. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1346. if (err) {
  1347. pr_err("mmc_erase: group start error %d, "
  1348. "status %#x\n", err, cmd.resp[0]);
  1349. err = -EIO;
  1350. goto out;
  1351. }
  1352. memset(&cmd, 0, sizeof(struct mmc_command));
  1353. if (mmc_card_sd(card))
  1354. cmd.opcode = SD_ERASE_WR_BLK_END;
  1355. else
  1356. cmd.opcode = MMC_ERASE_GROUP_END;
  1357. cmd.arg = to;
  1358. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1359. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1360. if (err) {
  1361. pr_err("mmc_erase: group end error %d, status %#x\n",
  1362. err, cmd.resp[0]);
  1363. err = -EIO;
  1364. goto out;
  1365. }
  1366. memset(&cmd, 0, sizeof(struct mmc_command));
  1367. cmd.opcode = MMC_ERASE;
  1368. cmd.arg = arg;
  1369. cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  1370. cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
  1371. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1372. if (err) {
  1373. pr_err("mmc_erase: erase error %d, status %#x\n",
  1374. err, cmd.resp[0]);
  1375. err = -EIO;
  1376. goto out;
  1377. }
  1378. if (mmc_host_is_spi(card->host))
  1379. goto out;
  1380. do {
  1381. memset(&cmd, 0, sizeof(struct mmc_command));
  1382. cmd.opcode = MMC_SEND_STATUS;
  1383. cmd.arg = card->rca << 16;
  1384. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1385. /* Do not retry else we can't see errors */
  1386. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1387. if (err || (cmd.resp[0] & 0xFDF92000)) {
  1388. pr_err("error %d requesting status %#x\n",
  1389. err, cmd.resp[0]);
  1390. err = -EIO;
  1391. goto out;
  1392. }
  1393. } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
  1394. R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG);
  1395. out:
  1396. return err;
  1397. }
  1398. /**
  1399. * mmc_erase - erase sectors.
  1400. * @card: card to erase
  1401. * @from: first sector to erase
  1402. * @nr: number of sectors to erase
  1403. * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
  1404. *
  1405. * Caller must claim host before calling this function.
  1406. */
  1407. int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
  1408. unsigned int arg)
  1409. {
  1410. unsigned int rem, to = from + nr;
  1411. if (!(card->host->caps & MMC_CAP_ERASE) ||
  1412. !(card->csd.cmdclass & CCC_ERASE))
  1413. return -EOPNOTSUPP;
  1414. if (!card->erase_size)
  1415. return -EOPNOTSUPP;
  1416. if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
  1417. return -EOPNOTSUPP;
  1418. if ((arg & MMC_SECURE_ARGS) &&
  1419. !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
  1420. return -EOPNOTSUPP;
  1421. if ((arg & MMC_TRIM_ARGS) &&
  1422. !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
  1423. return -EOPNOTSUPP;
  1424. if (arg == MMC_SECURE_ERASE_ARG) {
  1425. if (from % card->erase_size || nr % card->erase_size)
  1426. return -EINVAL;
  1427. }
  1428. if (arg == MMC_ERASE_ARG) {
  1429. rem = from % card->erase_size;
  1430. if (rem) {
  1431. rem = card->erase_size - rem;
  1432. from += rem;
  1433. if (nr > rem)
  1434. nr -= rem;
  1435. else
  1436. return 0;
  1437. }
  1438. rem = nr % card->erase_size;
  1439. if (rem)
  1440. nr -= rem;
  1441. }
  1442. if (nr == 0)
  1443. return 0;
  1444. to = from + nr;
  1445. if (to <= from)
  1446. return -EINVAL;
  1447. /* 'from' and 'to' are inclusive */
  1448. to -= 1;
  1449. return mmc_do_erase(card, from, to, arg);
  1450. }
  1451. EXPORT_SYMBOL(mmc_erase);
  1452. int mmc_can_erase(struct mmc_card *card)
  1453. {
  1454. if ((card->host->caps & MMC_CAP_ERASE) &&
  1455. (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
  1456. return 1;
  1457. return 0;
  1458. }
  1459. EXPORT_SYMBOL(mmc_can_erase);
  1460. int mmc_can_trim(struct mmc_card *card)
  1461. {
  1462. if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
  1463. return 1;
  1464. return 0;
  1465. }
  1466. EXPORT_SYMBOL(mmc_can_trim);
  1467. int mmc_can_discard(struct mmc_card *card)
  1468. {
  1469. /*
  1470. * As there's no way to detect the discard support bit at v4.5
  1471. * use the s/w feature support filed.
  1472. */
  1473. if (card->ext_csd.feature_support & MMC_DISCARD_FEATURE)
  1474. return 1;
  1475. return 0;
  1476. }
  1477. EXPORT_SYMBOL(mmc_can_discard);
  1478. int mmc_can_sanitize(struct mmc_card *card)
  1479. {
  1480. if (!mmc_can_trim(card) && !mmc_can_erase(card))
  1481. return 0;
  1482. if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE)
  1483. return 1;
  1484. return 0;
  1485. }
  1486. EXPORT_SYMBOL(mmc_can_sanitize);
  1487. int mmc_can_secure_erase_trim(struct mmc_card *card)
  1488. {
  1489. if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
  1490. return 1;
  1491. return 0;
  1492. }
  1493. EXPORT_SYMBOL(mmc_can_secure_erase_trim);
  1494. int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
  1495. unsigned int nr)
  1496. {
  1497. if (!card->erase_size)
  1498. return 0;
  1499. if (from % card->erase_size || nr % card->erase_size)
  1500. return 0;
  1501. return 1;
  1502. }
  1503. EXPORT_SYMBOL(mmc_erase_group_aligned);
  1504. static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
  1505. unsigned int arg)
  1506. {
  1507. struct mmc_host *host = card->host;
  1508. unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
  1509. unsigned int last_timeout = 0;
  1510. if (card->erase_shift)
  1511. max_qty = UINT_MAX >> card->erase_shift;
  1512. else if (mmc_card_sd(card))
  1513. max_qty = UINT_MAX;
  1514. else
  1515. max_qty = UINT_MAX / card->erase_size;
  1516. /* Find the largest qty with an OK timeout */
  1517. do {
  1518. y = 0;
  1519. for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
  1520. timeout = mmc_erase_timeout(card, arg, qty + x);
  1521. if (timeout > host->max_discard_to)
  1522. break;
  1523. if (timeout < last_timeout)
  1524. break;
  1525. last_timeout = timeout;
  1526. y = x;
  1527. }
  1528. qty += y;
  1529. } while (y);
  1530. if (!qty)
  1531. return 0;
  1532. if (qty == 1)
  1533. return 1;
  1534. /* Convert qty to sectors */
  1535. if (card->erase_shift)
  1536. max_discard = --qty << card->erase_shift;
  1537. else if (mmc_card_sd(card))
  1538. max_discard = qty;
  1539. else
  1540. max_discard = --qty * card->erase_size;
  1541. return max_discard;
  1542. }
  1543. unsigned int mmc_calc_max_discard(struct mmc_card *card)
  1544. {
  1545. struct mmc_host *host = card->host;
  1546. unsigned int max_discard, max_trim;
  1547. if (!host->max_discard_to)
  1548. return UINT_MAX;
  1549. /*
  1550. * Without erase_group_def set, MMC erase timeout depends on clock
  1551. * frequence which can change. In that case, the best choice is
  1552. * just the preferred erase size.
  1553. */
  1554. if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
  1555. return card->pref_erase;
  1556. max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
  1557. if (mmc_can_trim(card)) {
  1558. max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
  1559. if (max_trim < max_discard)
  1560. max_discard = max_trim;
  1561. } else if (max_discard < card->erase_size) {
  1562. max_discard = 0;
  1563. }
  1564. pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
  1565. mmc_hostname(host), max_discard, host->max_discard_to);
  1566. return max_discard;
  1567. }
  1568. EXPORT_SYMBOL(mmc_calc_max_discard);
  1569. int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
  1570. {
  1571. struct mmc_command cmd = {0};
  1572. if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
  1573. return 0;
  1574. cmd.opcode = MMC_SET_BLOCKLEN;
  1575. cmd.arg = blocklen;
  1576. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1577. return mmc_wait_for_cmd(card->host, &cmd, 5);
  1578. }
  1579. EXPORT_SYMBOL(mmc_set_blocklen);
  1580. static void mmc_hw_reset_for_init(struct mmc_host *host)
  1581. {
  1582. if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
  1583. return;
  1584. mmc_host_clk_hold(host);
  1585. host->ops->hw_reset(host);
  1586. mmc_host_clk_release(host);
  1587. }
  1588. int mmc_can_reset(struct mmc_card *card)
  1589. {
  1590. u8 rst_n_function;
  1591. if (!mmc_card_mmc(card))
  1592. return 0;
  1593. rst_n_function = card->ext_csd.rst_n_function;
  1594. if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
  1595. return 0;
  1596. return 1;
  1597. }
  1598. EXPORT_SYMBOL(mmc_can_reset);
  1599. static int mmc_do_hw_reset(struct mmc_host *host, int check)
  1600. {
  1601. struct mmc_card *card = host->card;
  1602. if (!host->bus_ops->power_restore)
  1603. return -EOPNOTSUPP;
  1604. if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
  1605. return -EOPNOTSUPP;
  1606. if (!card)
  1607. return -EINVAL;
  1608. if (!mmc_can_reset(card))
  1609. return -EOPNOTSUPP;
  1610. mmc_host_clk_hold(host);
  1611. mmc_set_clock(host, host->f_init);
  1612. host->ops->hw_reset(host);
  1613. /* If the reset has happened, then a status command will fail */
  1614. if (check) {
  1615. struct mmc_command cmd = {0};
  1616. int err;
  1617. cmd.opcode = MMC_SEND_STATUS;
  1618. if (!mmc_host_is_spi(card->host))
  1619. cmd.arg = card->rca << 16;
  1620. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  1621. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1622. if (!err) {
  1623. mmc_host_clk_release(host);
  1624. return -ENOSYS;
  1625. }
  1626. }
  1627. host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_DDR);
  1628. if (mmc_host_is_spi(host)) {
  1629. host->ios.chip_select = MMC_CS_HIGH;
  1630. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  1631. } else {
  1632. host->ios.chip_select = MMC_CS_DONTCARE;
  1633. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  1634. }
  1635. host->ios.bus_width = MMC_BUS_WIDTH_1;
  1636. host->ios.timing = MMC_TIMING_LEGACY;
  1637. mmc_set_ios(host);
  1638. mmc_host_clk_release(host);
  1639. return host->bus_ops->power_restore(host);
  1640. }
  1641. int mmc_hw_reset(struct mmc_host *host)
  1642. {
  1643. return mmc_do_hw_reset(host, 0);
  1644. }
  1645. EXPORT_SYMBOL(mmc_hw_reset);
  1646. int mmc_hw_reset_check(struct mmc_host *host)
  1647. {
  1648. return mmc_do_hw_reset(host, 1);
  1649. }
  1650. EXPORT_SYMBOL(mmc_hw_reset_check);
  1651. static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
  1652. {
  1653. host->f_init = freq;
  1654. #ifdef CONFIG_MMC_DEBUG
  1655. pr_info("%s: %s: trying to init card at %u Hz\n",
  1656. mmc_hostname(host), __func__, host->f_init);
  1657. #endif
  1658. mmc_power_up(host);
  1659. /*
  1660. * Some eMMCs (with VCCQ always on) may not be reset after power up, so
  1661. * do a hardware reset if possible.
  1662. */
  1663. mmc_hw_reset_for_init(host);
  1664. /* Initialization should be done at 3.3 V I/O voltage. */
  1665. mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0);
  1666. /*
  1667. * sdio_reset sends CMD52 to reset card. Since we do not know
  1668. * if the card is being re-initialized, just send it. CMD52
  1669. * should be ignored by SD/eMMC cards.
  1670. */
  1671. sdio_reset(host);
  1672. mmc_go_idle(host);
  1673. mmc_send_if_cond(host, host->ocr_avail);
  1674. /* Order's important: probe SDIO, then SD, then MMC */
  1675. if (!mmc_attach_sdio(host))
  1676. return 0;
  1677. if (!mmc_attach_sd(host))
  1678. return 0;
  1679. if (!mmc_attach_mmc(host))
  1680. return 0;
  1681. mmc_power_off(host);
  1682. return -EIO;
  1683. }
  1684. int _mmc_detect_card_removed(struct mmc_host *host)
  1685. {
  1686. int ret;
  1687. if ((host->caps & MMC_CAP_NONREMOVABLE) || !host->bus_ops->alive)
  1688. return 0;
  1689. if (!host->card || mmc_card_removed(host->card))
  1690. return 1;
  1691. ret = host->bus_ops->alive(host);
  1692. if (ret) {
  1693. mmc_card_set_removed(host->card);
  1694. pr_debug("%s: card remove detected\n", mmc_hostname(host));
  1695. }
  1696. return ret;
  1697. }
  1698. int mmc_detect_card_removed(struct mmc_host *host)
  1699. {
  1700. struct mmc_card *card = host->card;
  1701. int ret;
  1702. WARN_ON(!host->claimed);
  1703. if (!card)
  1704. return 1;
  1705. ret = mmc_card_removed(card);
  1706. /*
  1707. * The card will be considered unchanged unless we have been asked to
  1708. * detect a change or host requires polling to provide card detection.
  1709. */
  1710. if (!host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL) &&
  1711. !(host->caps2 & MMC_CAP2_DETECT_ON_ERR))
  1712. return ret;
  1713. host->detect_change = 0;
  1714. if (!ret) {
  1715. ret = _mmc_detect_card_removed(host);
  1716. if (ret && (host->caps2 & MMC_CAP2_DETECT_ON_ERR)) {
  1717. /*
  1718. * Schedule a detect work as soon as possible to let a
  1719. * rescan handle the card removal.
  1720. */
  1721. cancel_delayed_work(&host->detect);
  1722. mmc_detect_change(host, 0);
  1723. }
  1724. }
  1725. return ret;
  1726. }
  1727. EXPORT_SYMBOL(mmc_detect_card_removed);
  1728. void mmc_rescan(struct work_struct *work)
  1729. {
  1730. struct mmc_host *host =
  1731. container_of(work, struct mmc_host, detect.work);
  1732. int i;
  1733. if (host->rescan_disable)
  1734. return;
  1735. mmc_bus_get(host);
  1736. /*
  1737. * if there is a _removable_ card registered, check whether it is
  1738. * still present
  1739. */
  1740. if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
  1741. && !(host->caps & MMC_CAP_NONREMOVABLE))
  1742. host->bus_ops->detect(host);
  1743. host->detect_change = 0;
  1744. /*
  1745. * Let mmc_bus_put() free the bus/bus_ops if we've found that
  1746. * the card is no longer present.
  1747. */
  1748. mmc_bus_put(host);
  1749. mmc_bus_get(host);
  1750. /* if there still is a card present, stop here */
  1751. if (host->bus_ops != NULL) {
  1752. mmc_bus_put(host);
  1753. goto out;
  1754. }
  1755. /*
  1756. * Only we can add a new handler, so it's safe to
  1757. * release the lock here.
  1758. */
  1759. mmc_bus_put(host);
  1760. if (host->ops->get_cd && host->ops->get_cd(host) == 0) {
  1761. mmc_claim_host(host);
  1762. mmc_power_off(host);
  1763. mmc_release_host(host);
  1764. goto out;
  1765. }
  1766. mmc_claim_host(host);
  1767. for (i = 0; i < ARRAY_SIZE(freqs); i++) {
  1768. if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
  1769. break;
  1770. if (freqs[i] <= host->f_min)
  1771. break;
  1772. }
  1773. mmc_release_host(host);
  1774. out:
  1775. if (host->caps & MMC_CAP_NEEDS_POLL)
  1776. mmc_schedule_delayed_work(&host->detect, HZ);
  1777. }
  1778. void mmc_start_host(struct mmc_host *host)
  1779. {
  1780. host->f_init = max(freqs[0], host->f_min);
  1781. mmc_power_up(host);
  1782. mmc_detect_change(host, 0);
  1783. }
  1784. void mmc_stop_host(struct mmc_host *host)
  1785. {
  1786. #ifdef CONFIG_MMC_DEBUG
  1787. unsigned long flags;
  1788. spin_lock_irqsave(&host->lock, flags);
  1789. host->removed = 1;
  1790. spin_unlock_irqrestore(&host->lock, flags);
  1791. #endif
  1792. cancel_delayed_work_sync(&host->detect);
  1793. mmc_flush_scheduled_work();
  1794. /* clear pm flags now and let card drivers set them as needed */
  1795. host->pm_flags = 0;
  1796. mmc_bus_get(host);
  1797. if (host->bus_ops && !host->bus_dead) {
  1798. /* Calling bus_ops->remove() with a claimed host can deadlock */
  1799. if (host->bus_ops->remove)
  1800. host->bus_ops->remove(host);
  1801. mmc_claim_host(host);
  1802. mmc_detach_bus(host);
  1803. mmc_power_off(host);
  1804. mmc_release_host(host);
  1805. mmc_bus_put(host);
  1806. return;
  1807. }
  1808. mmc_bus_put(host);
  1809. BUG_ON(host->card);
  1810. mmc_power_off(host);
  1811. }
  1812. int mmc_power_save_host(struct mmc_host *host)
  1813. {
  1814. int ret = 0;
  1815. #ifdef CONFIG_MMC_DEBUG
  1816. pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
  1817. #endif
  1818. mmc_bus_get(host);
  1819. if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
  1820. mmc_bus_put(host);
  1821. return -EINVAL;
  1822. }
  1823. if (host->bus_ops->power_save)
  1824. ret = host->bus_ops->power_save(host);
  1825. mmc_bus_put(host);
  1826. mmc_power_off(host);
  1827. return ret;
  1828. }
  1829. EXPORT_SYMBOL(mmc_power_save_host);
  1830. int mmc_power_restore_host(struct mmc_host *host)
  1831. {
  1832. int ret;
  1833. #ifdef CONFIG_MMC_DEBUG
  1834. pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
  1835. #endif
  1836. mmc_bus_get(host);
  1837. if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
  1838. mmc_bus_put(host);
  1839. return -EINVAL;
  1840. }
  1841. mmc_power_up(host);
  1842. ret = host->bus_ops->power_restore(host);
  1843. mmc_bus_put(host);
  1844. return ret;
  1845. }
  1846. EXPORT_SYMBOL(mmc_power_restore_host);
  1847. int mmc_card_awake(struct mmc_host *host)
  1848. {
  1849. int err = -ENOSYS;
  1850. if (host->caps2 & MMC_CAP2_NO_SLEEP_CMD)
  1851. return 0;
  1852. mmc_bus_get(host);
  1853. if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
  1854. err = host->bus_ops->awake(host);
  1855. mmc_bus_put(host);
  1856. return err;
  1857. }
  1858. EXPORT_SYMBOL(mmc_card_awake);
  1859. int mmc_card_sleep(struct mmc_host *host)
  1860. {
  1861. int err = -ENOSYS;
  1862. if (host->caps2 & MMC_CAP2_NO_SLEEP_CMD)
  1863. return 0;
  1864. mmc_bus_get(host);
  1865. if (host->bus_ops && !host->bus_dead && host->bus_ops->sleep)
  1866. err = host->bus_ops->sleep(host);
  1867. mmc_bus_put(host);
  1868. return err;
  1869. }
  1870. EXPORT_SYMBOL(mmc_card_sleep);
  1871. int mmc_card_can_sleep(struct mmc_host *host)
  1872. {
  1873. struct mmc_card *card = host->card;
  1874. if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
  1875. return 1;
  1876. return 0;
  1877. }
  1878. EXPORT_SYMBOL(mmc_card_can_sleep);
  1879. /*
  1880. * Flush the cache to the non-volatile storage.
  1881. */
  1882. int mmc_flush_cache(struct mmc_card *card)
  1883. {
  1884. struct mmc_host *host = card->host;
  1885. int err = 0;
  1886. if (!(host->caps2 & MMC_CAP2_CACHE_CTRL))
  1887. return err;
  1888. if (mmc_card_mmc(card) &&
  1889. (card->ext_csd.cache_size > 0) &&
  1890. (card->ext_csd.cache_ctrl & 1)) {
  1891. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1892. EXT_CSD_FLUSH_CACHE, 1, 0);
  1893. if (err)
  1894. pr_err("%s: cache flush error %d\n",
  1895. mmc_hostname(card->host), err);
  1896. }
  1897. return err;
  1898. }
  1899. EXPORT_SYMBOL(mmc_flush_cache);
  1900. /*
  1901. * Turn the cache ON/OFF.
  1902. * Turning the cache OFF shall trigger flushing of the data
  1903. * to the non-volatile storage.
  1904. */
  1905. int mmc_cache_ctrl(struct mmc_host *host, u8 enable)
  1906. {
  1907. struct mmc_card *card = host->card;
  1908. unsigned int timeout;
  1909. int err = 0;
  1910. if (!(host->caps2 & MMC_CAP2_CACHE_CTRL) ||
  1911. mmc_card_is_removable(host))
  1912. return err;
  1913. mmc_claim_host(host);
  1914. if (card && mmc_card_mmc(card) &&
  1915. (card->ext_csd.cache_size > 0)) {
  1916. enable = !!enable;
  1917. if (card->ext_csd.cache_ctrl ^ enable) {
  1918. timeout = enable ? card->ext_csd.generic_cmd6_time : 0;
  1919. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1920. EXT_CSD_CACHE_CTRL, enable, timeout);
  1921. if (err)
  1922. pr_err("%s: cache %s error %d\n",
  1923. mmc_hostname(card->host),
  1924. enable ? "on" : "off",
  1925. err);
  1926. else
  1927. card->ext_csd.cache_ctrl = enable;
  1928. }
  1929. }
  1930. mmc_release_host(host);
  1931. return err;
  1932. }
  1933. EXPORT_SYMBOL(mmc_cache_ctrl);
  1934. #ifdef CONFIG_PM
  1935. /**
  1936. * mmc_suspend_host - suspend a host
  1937. * @host: mmc host
  1938. */
  1939. int mmc_suspend_host(struct mmc_host *host)
  1940. {
  1941. int err = 0;
  1942. cancel_delayed_work(&host->detect);
  1943. mmc_flush_scheduled_work();
  1944. err = mmc_cache_ctrl(host, 0);
  1945. if (err)
  1946. goto out;
  1947. mmc_bus_get(host);
  1948. if (host->bus_ops && !host->bus_dead) {
  1949. if (host->bus_ops->suspend)
  1950. err = host->bus_ops->suspend(host);
  1951. if (err == -ENOSYS || !host->bus_ops->resume) {
  1952. /*
  1953. * We simply "remove" the card in this case.
  1954. * It will be redetected on resume. (Calling
  1955. * bus_ops->remove() with a claimed host can
  1956. * deadlock.)
  1957. */
  1958. if (host->bus_ops->remove)
  1959. host->bus_ops->remove(host);
  1960. mmc_claim_host(host);
  1961. mmc_detach_bus(host);
  1962. mmc_power_off(host);
  1963. mmc_release_host(host);
  1964. host->pm_flags = 0;
  1965. err = 0;
  1966. }
  1967. }
  1968. mmc_bus_put(host);
  1969. if (!err && !mmc_card_keep_power(host))
  1970. mmc_power_off(host);
  1971. out:
  1972. return err;
  1973. }
  1974. EXPORT_SYMBOL(mmc_suspend_host);
  1975. /**
  1976. * mmc_resume_host - resume a previously suspended host
  1977. * @host: mmc host
  1978. */
  1979. int mmc_resume_host(struct mmc_host *host)
  1980. {
  1981. int err = 0;
  1982. mmc_bus_get(host);
  1983. if (host->bus_ops && !host->bus_dead) {
  1984. if (!mmc_card_keep_power(host)) {
  1985. mmc_power_up(host);
  1986. mmc_select_voltage(host, host->ocr);
  1987. /*
  1988. * Tell runtime PM core we just powered up the card,
  1989. * since it still believes the card is powered off.
  1990. * Note that currently runtime PM is only enabled
  1991. * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
  1992. */
  1993. if (mmc_card_sdio(host->card) &&
  1994. (host->caps & MMC_CAP_POWER_OFF_CARD)) {
  1995. pm_runtime_disable(&host->card->dev);
  1996. pm_runtime_set_active(&host->card->dev);
  1997. pm_runtime_enable(&host->card->dev);
  1998. }
  1999. }
  2000. BUG_ON(!host->bus_ops->resume);
  2001. err = host->bus_ops->resume(host);
  2002. if (err) {
  2003. pr_warning("%s: error %d during resume "
  2004. "(card was removed?)\n",
  2005. mmc_hostname(host), err);
  2006. err = 0;
  2007. }
  2008. }
  2009. host->pm_flags &= ~MMC_PM_KEEP_POWER;
  2010. mmc_bus_put(host);
  2011. return err;
  2012. }
  2013. EXPORT_SYMBOL(mmc_resume_host);
  2014. /* Do the card removal on suspend if card is assumed removeable
  2015. * Do that in pm notifier while userspace isn't yet frozen, so we will be able
  2016. to sync the card.
  2017. */
  2018. int mmc_pm_notify(struct notifier_block *notify_block,
  2019. unsigned long mode, void *unused)
  2020. {
  2021. struct mmc_host *host = container_of(
  2022. notify_block, struct mmc_host, pm_notify);
  2023. unsigned long flags;
  2024. switch (mode) {
  2025. case PM_HIBERNATION_PREPARE:
  2026. case PM_SUSPEND_PREPARE:
  2027. spin_lock_irqsave(&host->lock, flags);
  2028. host->rescan_disable = 1;
  2029. host->power_notify_type = MMC_HOST_PW_NOTIFY_SHORT;
  2030. spin_unlock_irqrestore(&host->lock, flags);
  2031. cancel_delayed_work_sync(&host->detect);
  2032. if (!host->bus_ops || host->bus_ops->suspend)
  2033. break;
  2034. /* Calling bus_ops->remove() with a claimed host can deadlock */
  2035. if (host->bus_ops->remove)
  2036. host->bus_ops->remove(host);
  2037. mmc_claim_host(host);
  2038. mmc_detach_bus(host);
  2039. mmc_power_off(host);
  2040. mmc_release_host(host);
  2041. host->pm_flags = 0;
  2042. break;
  2043. case PM_POST_SUSPEND:
  2044. case PM_POST_HIBERNATION:
  2045. case PM_POST_RESTORE:
  2046. spin_lock_irqsave(&host->lock, flags);
  2047. host->rescan_disable = 0;
  2048. host->power_notify_type = MMC_HOST_PW_NOTIFY_LONG;
  2049. spin_unlock_irqrestore(&host->lock, flags);
  2050. mmc_detect_change(host, 0);
  2051. }
  2052. return 0;
  2053. }
  2054. #endif
  2055. static int __init mmc_init(void)
  2056. {
  2057. int ret;
  2058. workqueue = alloc_ordered_workqueue("kmmcd", 0);
  2059. if (!workqueue)
  2060. return -ENOMEM;
  2061. ret = mmc_register_bus();
  2062. if (ret)
  2063. goto destroy_workqueue;
  2064. ret = mmc_register_host_class();
  2065. if (ret)
  2066. goto unregister_bus;
  2067. ret = sdio_register_bus();
  2068. if (ret)
  2069. goto unregister_host_class;
  2070. return 0;
  2071. unregister_host_class:
  2072. mmc_unregister_host_class();
  2073. unregister_bus:
  2074. mmc_unregister_bus();
  2075. destroy_workqueue:
  2076. destroy_workqueue(workqueue);
  2077. return ret;
  2078. }
  2079. static void __exit mmc_exit(void)
  2080. {
  2081. sdio_unregister_bus();
  2082. mmc_unregister_host_class();
  2083. mmc_unregister_bus();
  2084. destroy_workqueue(workqueue);
  2085. }
  2086. subsys_initcall(mmc_init);
  2087. module_exit(mmc_exit);
  2088. MODULE_LICENSE("GPL");