core.c 58 KB

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