core.c 58 KB

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