core.c 60 KB

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