core.c 61 KB

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