core.c 58 KB

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