core.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037
  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/pm_wakeup.h>
  27. #include <linux/suspend.h>
  28. #include <linux/fault-inject.h>
  29. #include <linux/random.h>
  30. #include <linux/slab.h>
  31. #include <linux/of.h>
  32. #include <linux/mmc/card.h>
  33. #include <linux/mmc/host.h>
  34. #include <linux/mmc/mmc.h>
  35. #include <linux/mmc/sd.h>
  36. #include <linux/mmc/slot-gpio.h>
  37. #define CREATE_TRACE_POINTS
  38. #include <trace/events/mmc.h>
  39. #include "core.h"
  40. #include "bus.h"
  41. #include "host.h"
  42. #include "sdio_bus.h"
  43. #include "pwrseq.h"
  44. #include "mmc_ops.h"
  45. #include "sd_ops.h"
  46. #include "sdio_ops.h"
  47. /* If the device is not responding */
  48. #define MMC_CORE_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
  49. /*
  50. * Background operations can take a long time, depending on the housekeeping
  51. * operations the card has to perform.
  52. */
  53. #define MMC_BKOPS_MAX_TIMEOUT (4 * 60 * 1000) /* max time to wait in ms */
  54. /* The max erase timeout, used when host->max_busy_timeout isn't specified */
  55. #define MMC_ERASE_TIMEOUT_MS (60 * 1000) /* 60 s */
  56. static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
  57. /*
  58. * Enabling software CRCs on the data blocks can be a significant (30%)
  59. * performance cost, and for other reasons may not always be desired.
  60. * So we allow it it to be disabled.
  61. */
  62. bool use_spi_crc = 1;
  63. module_param(use_spi_crc, bool, 0);
  64. static int mmc_schedule_delayed_work(struct delayed_work *work,
  65. unsigned long delay)
  66. {
  67. /*
  68. * We use the system_freezable_wq, because of two reasons.
  69. * First, it allows several works (not the same work item) to be
  70. * executed simultaneously. Second, the queue becomes frozen when
  71. * userspace becomes frozen during system PM.
  72. */
  73. return queue_delayed_work(system_freezable_wq, work, delay);
  74. }
  75. #ifdef CONFIG_FAIL_MMC_REQUEST
  76. /*
  77. * Internal function. Inject random data errors.
  78. * If mmc_data is NULL no errors are injected.
  79. */
  80. static void mmc_should_fail_request(struct mmc_host *host,
  81. struct mmc_request *mrq)
  82. {
  83. struct mmc_command *cmd = mrq->cmd;
  84. struct mmc_data *data = mrq->data;
  85. static const int data_errors[] = {
  86. -ETIMEDOUT,
  87. -EILSEQ,
  88. -EIO,
  89. };
  90. if (!data)
  91. return;
  92. if (cmd->error || data->error ||
  93. !should_fail(&host->fail_mmc_request, data->blksz * data->blocks))
  94. return;
  95. data->error = data_errors[prandom_u32() % ARRAY_SIZE(data_errors)];
  96. data->bytes_xfered = (prandom_u32() % (data->bytes_xfered >> 9)) << 9;
  97. }
  98. #else /* CONFIG_FAIL_MMC_REQUEST */
  99. static inline void mmc_should_fail_request(struct mmc_host *host,
  100. struct mmc_request *mrq)
  101. {
  102. }
  103. #endif /* CONFIG_FAIL_MMC_REQUEST */
  104. static inline void mmc_complete_cmd(struct mmc_request *mrq)
  105. {
  106. if (mrq->cap_cmd_during_tfr && !completion_done(&mrq->cmd_completion))
  107. complete_all(&mrq->cmd_completion);
  108. }
  109. void mmc_command_done(struct mmc_host *host, struct mmc_request *mrq)
  110. {
  111. if (!mrq->cap_cmd_during_tfr)
  112. return;
  113. mmc_complete_cmd(mrq);
  114. pr_debug("%s: cmd done, tfr ongoing (CMD%u)\n",
  115. mmc_hostname(host), mrq->cmd->opcode);
  116. }
  117. EXPORT_SYMBOL(mmc_command_done);
  118. /**
  119. * mmc_request_done - finish processing an MMC request
  120. * @host: MMC host which completed request
  121. * @mrq: MMC request which request
  122. *
  123. * MMC drivers should call this function when they have completed
  124. * their processing of a request.
  125. */
  126. void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
  127. {
  128. struct mmc_command *cmd = mrq->cmd;
  129. int err = cmd->error;
  130. /* Flag re-tuning needed on CRC errors */
  131. if ((cmd->opcode != MMC_SEND_TUNING_BLOCK &&
  132. cmd->opcode != MMC_SEND_TUNING_BLOCK_HS200) &&
  133. (err == -EILSEQ || (mrq->sbc && mrq->sbc->error == -EILSEQ) ||
  134. (mrq->data && mrq->data->error == -EILSEQ) ||
  135. (mrq->stop && mrq->stop->error == -EILSEQ)))
  136. mmc_retune_needed(host);
  137. if (err && cmd->retries && mmc_host_is_spi(host)) {
  138. if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
  139. cmd->retries = 0;
  140. }
  141. if (host->ongoing_mrq == mrq)
  142. host->ongoing_mrq = NULL;
  143. mmc_complete_cmd(mrq);
  144. trace_mmc_request_done(host, mrq);
  145. if (err && cmd->retries && !mmc_card_removed(host->card)) {
  146. /*
  147. * Request starter must handle retries - see
  148. * mmc_wait_for_req_done().
  149. */
  150. if (mrq->done)
  151. mrq->done(mrq);
  152. } else {
  153. mmc_should_fail_request(host, mrq);
  154. if (!host->ongoing_mrq)
  155. led_trigger_event(host->led, LED_OFF);
  156. if (mrq->sbc) {
  157. pr_debug("%s: req done <CMD%u>: %d: %08x %08x %08x %08x\n",
  158. mmc_hostname(host), mrq->sbc->opcode,
  159. mrq->sbc->error,
  160. mrq->sbc->resp[0], mrq->sbc->resp[1],
  161. mrq->sbc->resp[2], mrq->sbc->resp[3]);
  162. }
  163. pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
  164. mmc_hostname(host), cmd->opcode, err,
  165. cmd->resp[0], cmd->resp[1],
  166. cmd->resp[2], cmd->resp[3]);
  167. if (mrq->data) {
  168. pr_debug("%s: %d bytes transferred: %d\n",
  169. mmc_hostname(host),
  170. mrq->data->bytes_xfered, mrq->data->error);
  171. }
  172. if (mrq->stop) {
  173. pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
  174. mmc_hostname(host), mrq->stop->opcode,
  175. mrq->stop->error,
  176. mrq->stop->resp[0], mrq->stop->resp[1],
  177. mrq->stop->resp[2], mrq->stop->resp[3]);
  178. }
  179. if (mrq->done)
  180. mrq->done(mrq);
  181. }
  182. }
  183. EXPORT_SYMBOL(mmc_request_done);
  184. static void __mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
  185. {
  186. int err;
  187. /* Assumes host controller has been runtime resumed by mmc_claim_host */
  188. err = mmc_retune(host);
  189. if (err) {
  190. mrq->cmd->error = err;
  191. mmc_request_done(host, mrq);
  192. return;
  193. }
  194. /*
  195. * For sdio rw commands we must wait for card busy otherwise some
  196. * sdio devices won't work properly.
  197. */
  198. if (mmc_is_io_op(mrq->cmd->opcode) && host->ops->card_busy) {
  199. int tries = 500; /* Wait aprox 500ms at maximum */
  200. while (host->ops->card_busy(host) && --tries)
  201. mmc_delay(1);
  202. if (tries == 0) {
  203. mrq->cmd->error = -EBUSY;
  204. mmc_request_done(host, mrq);
  205. return;
  206. }
  207. }
  208. if (mrq->cap_cmd_during_tfr) {
  209. host->ongoing_mrq = mrq;
  210. /*
  211. * Retry path could come through here without having waiting on
  212. * cmd_completion, so ensure it is reinitialised.
  213. */
  214. reinit_completion(&mrq->cmd_completion);
  215. }
  216. trace_mmc_request_start(host, mrq);
  217. host->ops->request(host, mrq);
  218. }
  219. static int mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
  220. {
  221. #ifdef CONFIG_MMC_DEBUG
  222. unsigned int i, sz;
  223. struct scatterlist *sg;
  224. #endif
  225. mmc_retune_hold(host);
  226. if (mmc_card_removed(host->card))
  227. return -ENOMEDIUM;
  228. if (mrq->sbc) {
  229. pr_debug("<%s: starting CMD%u arg %08x flags %08x>\n",
  230. mmc_hostname(host), mrq->sbc->opcode,
  231. mrq->sbc->arg, mrq->sbc->flags);
  232. }
  233. pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
  234. mmc_hostname(host), mrq->cmd->opcode,
  235. mrq->cmd->arg, mrq->cmd->flags);
  236. if (mrq->data) {
  237. pr_debug("%s: blksz %d blocks %d flags %08x "
  238. "tsac %d ms nsac %d\n",
  239. mmc_hostname(host), mrq->data->blksz,
  240. mrq->data->blocks, mrq->data->flags,
  241. mrq->data->timeout_ns / 1000000,
  242. mrq->data->timeout_clks);
  243. }
  244. if (mrq->stop) {
  245. pr_debug("%s: CMD%u arg %08x flags %08x\n",
  246. mmc_hostname(host), mrq->stop->opcode,
  247. mrq->stop->arg, mrq->stop->flags);
  248. }
  249. WARN_ON(!host->claimed);
  250. mrq->cmd->error = 0;
  251. mrq->cmd->mrq = mrq;
  252. if (mrq->sbc) {
  253. mrq->sbc->error = 0;
  254. mrq->sbc->mrq = mrq;
  255. }
  256. if (mrq->data) {
  257. if (mrq->data->blksz > host->max_blk_size ||
  258. mrq->data->blocks > host->max_blk_count ||
  259. mrq->data->blocks * mrq->data->blksz > host->max_req_size)
  260. return -EINVAL;
  261. #ifdef CONFIG_MMC_DEBUG
  262. sz = 0;
  263. for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
  264. sz += sg->length;
  265. if (sz != mrq->data->blocks * mrq->data->blksz)
  266. return -EINVAL;
  267. #endif
  268. mrq->cmd->data = mrq->data;
  269. mrq->data->error = 0;
  270. mrq->data->mrq = mrq;
  271. if (mrq->stop) {
  272. mrq->data->stop = mrq->stop;
  273. mrq->stop->error = 0;
  274. mrq->stop->mrq = mrq;
  275. }
  276. }
  277. led_trigger_event(host->led, LED_FULL);
  278. __mmc_start_request(host, mrq);
  279. return 0;
  280. }
  281. /**
  282. * mmc_start_bkops - start BKOPS for supported cards
  283. * @card: MMC card to start BKOPS
  284. * @form_exception: A flag to indicate if this function was
  285. * called due to an exception raised by the card
  286. *
  287. * Start background operations whenever requested.
  288. * When the urgent BKOPS bit is set in a R1 command response
  289. * then background operations should be started immediately.
  290. */
  291. void mmc_start_bkops(struct mmc_card *card, bool from_exception)
  292. {
  293. int err;
  294. int timeout;
  295. bool use_busy_signal;
  296. if (!card->ext_csd.man_bkops_en || mmc_card_doing_bkops(card))
  297. return;
  298. err = mmc_read_bkops_status(card);
  299. if (err) {
  300. pr_err("%s: Failed to read bkops status: %d\n",
  301. mmc_hostname(card->host), err);
  302. return;
  303. }
  304. if (!card->ext_csd.raw_bkops_status)
  305. return;
  306. if (card->ext_csd.raw_bkops_status < EXT_CSD_BKOPS_LEVEL_2 &&
  307. from_exception)
  308. return;
  309. mmc_claim_host(card->host);
  310. if (card->ext_csd.raw_bkops_status >= EXT_CSD_BKOPS_LEVEL_2) {
  311. timeout = MMC_BKOPS_MAX_TIMEOUT;
  312. use_busy_signal = true;
  313. } else {
  314. timeout = 0;
  315. use_busy_signal = false;
  316. }
  317. mmc_retune_hold(card->host);
  318. err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  319. EXT_CSD_BKOPS_START, 1, timeout, 0,
  320. use_busy_signal, true, false);
  321. if (err) {
  322. pr_warn("%s: Error %d starting bkops\n",
  323. mmc_hostname(card->host), err);
  324. mmc_retune_release(card->host);
  325. goto out;
  326. }
  327. /*
  328. * For urgent bkops status (LEVEL_2 and more)
  329. * bkops executed synchronously, otherwise
  330. * the operation is in progress
  331. */
  332. if (!use_busy_signal)
  333. mmc_card_set_doing_bkops(card);
  334. else
  335. mmc_retune_release(card->host);
  336. out:
  337. mmc_release_host(card->host);
  338. }
  339. EXPORT_SYMBOL(mmc_start_bkops);
  340. /*
  341. * mmc_wait_data_done() - done callback for data request
  342. * @mrq: done data request
  343. *
  344. * Wakes up mmc context, passed as a callback to host controller driver
  345. */
  346. static void mmc_wait_data_done(struct mmc_request *mrq)
  347. {
  348. struct mmc_context_info *context_info = &mrq->host->context_info;
  349. context_info->is_done_rcv = true;
  350. wake_up_interruptible(&context_info->wait);
  351. }
  352. static void mmc_wait_done(struct mmc_request *mrq)
  353. {
  354. complete(&mrq->completion);
  355. }
  356. static inline void mmc_wait_ongoing_tfr_cmd(struct mmc_host *host)
  357. {
  358. struct mmc_request *ongoing_mrq = READ_ONCE(host->ongoing_mrq);
  359. /*
  360. * If there is an ongoing transfer, wait for the command line to become
  361. * available.
  362. */
  363. if (ongoing_mrq && !completion_done(&ongoing_mrq->cmd_completion))
  364. wait_for_completion(&ongoing_mrq->cmd_completion);
  365. }
  366. /*
  367. *__mmc_start_data_req() - starts data request
  368. * @host: MMC host to start the request
  369. * @mrq: data request to start
  370. *
  371. * Sets the done callback to be called when request is completed by the card.
  372. * Starts data mmc request execution
  373. * If an ongoing transfer is already in progress, wait for the command line
  374. * to become available before sending another command.
  375. */
  376. static int __mmc_start_data_req(struct mmc_host *host, struct mmc_request *mrq)
  377. {
  378. int err;
  379. mmc_wait_ongoing_tfr_cmd(host);
  380. mrq->done = mmc_wait_data_done;
  381. mrq->host = host;
  382. init_completion(&mrq->cmd_completion);
  383. err = mmc_start_request(host, mrq);
  384. if (err) {
  385. mrq->cmd->error = err;
  386. mmc_complete_cmd(mrq);
  387. mmc_wait_data_done(mrq);
  388. }
  389. return err;
  390. }
  391. static int __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
  392. {
  393. int err;
  394. mmc_wait_ongoing_tfr_cmd(host);
  395. init_completion(&mrq->completion);
  396. mrq->done = mmc_wait_done;
  397. init_completion(&mrq->cmd_completion);
  398. err = mmc_start_request(host, mrq);
  399. if (err) {
  400. mrq->cmd->error = err;
  401. mmc_complete_cmd(mrq);
  402. complete(&mrq->completion);
  403. }
  404. return err;
  405. }
  406. /*
  407. * mmc_wait_for_data_req_done() - wait for request completed
  408. * @host: MMC host to prepare the command.
  409. * @mrq: MMC request to wait for
  410. *
  411. * Blocks MMC context till host controller will ack end of data request
  412. * execution or new request notification arrives from the block layer.
  413. * Handles command retries.
  414. *
  415. * Returns enum mmc_blk_status after checking errors.
  416. */
  417. static enum mmc_blk_status mmc_wait_for_data_req_done(struct mmc_host *host,
  418. struct mmc_request *mrq)
  419. {
  420. struct mmc_command *cmd;
  421. struct mmc_context_info *context_info = &host->context_info;
  422. enum mmc_blk_status status;
  423. while (1) {
  424. wait_event_interruptible(context_info->wait,
  425. (context_info->is_done_rcv ||
  426. context_info->is_new_req));
  427. if (context_info->is_done_rcv) {
  428. context_info->is_done_rcv = false;
  429. cmd = mrq->cmd;
  430. if (!cmd->error || !cmd->retries ||
  431. mmc_card_removed(host->card)) {
  432. status = host->areq->err_check(host->card,
  433. host->areq);
  434. break; /* return status */
  435. } else {
  436. mmc_retune_recheck(host);
  437. pr_info("%s: req failed (CMD%u): %d, retrying...\n",
  438. mmc_hostname(host),
  439. cmd->opcode, cmd->error);
  440. cmd->retries--;
  441. cmd->error = 0;
  442. __mmc_start_request(host, mrq);
  443. continue; /* wait for done/new event again */
  444. }
  445. }
  446. return MMC_BLK_NEW_REQUEST;
  447. }
  448. mmc_retune_release(host);
  449. return status;
  450. }
  451. void mmc_wait_for_req_done(struct mmc_host *host, struct mmc_request *mrq)
  452. {
  453. struct mmc_command *cmd;
  454. while (1) {
  455. wait_for_completion(&mrq->completion);
  456. cmd = mrq->cmd;
  457. /*
  458. * If host has timed out waiting for the sanitize
  459. * to complete, card might be still in programming state
  460. * so let's try to bring the card out of programming
  461. * state.
  462. */
  463. if (cmd->sanitize_busy && cmd->error == -ETIMEDOUT) {
  464. if (!mmc_interrupt_hpi(host->card)) {
  465. pr_warn("%s: %s: Interrupted sanitize\n",
  466. mmc_hostname(host), __func__);
  467. cmd->error = 0;
  468. break;
  469. } else {
  470. pr_err("%s: %s: Failed to interrupt sanitize\n",
  471. mmc_hostname(host), __func__);
  472. }
  473. }
  474. if (!cmd->error || !cmd->retries ||
  475. mmc_card_removed(host->card))
  476. break;
  477. mmc_retune_recheck(host);
  478. pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
  479. mmc_hostname(host), cmd->opcode, cmd->error);
  480. cmd->retries--;
  481. cmd->error = 0;
  482. __mmc_start_request(host, mrq);
  483. }
  484. mmc_retune_release(host);
  485. }
  486. EXPORT_SYMBOL(mmc_wait_for_req_done);
  487. /**
  488. * mmc_is_req_done - Determine if a 'cap_cmd_during_tfr' request is done
  489. * @host: MMC host
  490. * @mrq: MMC request
  491. *
  492. * mmc_is_req_done() is used with requests that have
  493. * mrq->cap_cmd_during_tfr = true. mmc_is_req_done() must be called after
  494. * starting a request and before waiting for it to complete. That is,
  495. * either in between calls to mmc_start_req(), or after mmc_wait_for_req()
  496. * and before mmc_wait_for_req_done(). If it is called at other times the
  497. * result is not meaningful.
  498. */
  499. bool mmc_is_req_done(struct mmc_host *host, struct mmc_request *mrq)
  500. {
  501. if (host->areq)
  502. return host->context_info.is_done_rcv;
  503. else
  504. return completion_done(&mrq->completion);
  505. }
  506. EXPORT_SYMBOL(mmc_is_req_done);
  507. /**
  508. * mmc_pre_req - Prepare for a new request
  509. * @host: MMC host to prepare command
  510. * @mrq: MMC request to prepare for
  511. *
  512. * mmc_pre_req() is called in prior to mmc_start_req() to let
  513. * host prepare for the new request. Preparation of a request may be
  514. * performed while another request is running on the host.
  515. */
  516. static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq)
  517. {
  518. if (host->ops->pre_req)
  519. host->ops->pre_req(host, mrq);
  520. }
  521. /**
  522. * mmc_post_req - Post process a completed request
  523. * @host: MMC host to post process command
  524. * @mrq: MMC request to post process for
  525. * @err: Error, if non zero, clean up any resources made in pre_req
  526. *
  527. * Let the host post process a completed request. Post processing of
  528. * a request may be performed while another reuqest is running.
  529. */
  530. static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
  531. int err)
  532. {
  533. if (host->ops->post_req)
  534. host->ops->post_req(host, mrq, err);
  535. }
  536. /**
  537. * mmc_start_req - start a non-blocking request
  538. * @host: MMC host to start command
  539. * @areq: async request to start
  540. * @error: out parameter returns 0 for success, otherwise non zero
  541. *
  542. * Start a new MMC custom command request for a host.
  543. * If there is on ongoing async request wait for completion
  544. * of that request and start the new one and return.
  545. * Does not wait for the new request to complete.
  546. *
  547. * Returns the completed request, NULL in case of none completed.
  548. * Wait for the an ongoing request (previoulsy started) to complete and
  549. * return the completed request. If there is no ongoing request, NULL
  550. * is returned without waiting. NULL is not an error condition.
  551. */
  552. struct mmc_async_req *mmc_start_req(struct mmc_host *host,
  553. struct mmc_async_req *areq,
  554. enum mmc_blk_status *ret_stat)
  555. {
  556. enum mmc_blk_status status = MMC_BLK_SUCCESS;
  557. int start_err = 0;
  558. struct mmc_async_req *data = host->areq;
  559. /* Prepare a new request */
  560. if (areq)
  561. mmc_pre_req(host, areq->mrq);
  562. if (host->areq) {
  563. status = mmc_wait_for_data_req_done(host, host->areq->mrq);
  564. if (status == MMC_BLK_NEW_REQUEST) {
  565. if (ret_stat)
  566. *ret_stat = status;
  567. /*
  568. * The previous request was not completed,
  569. * nothing to return
  570. */
  571. return NULL;
  572. }
  573. /*
  574. * Check BKOPS urgency for each R1 response
  575. */
  576. if (host->card && mmc_card_mmc(host->card) &&
  577. ((mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1) ||
  578. (mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1B)) &&
  579. (host->areq->mrq->cmd->resp[0] & R1_EXCEPTION_EVENT)) {
  580. /* Cancel the prepared request */
  581. if (areq)
  582. mmc_post_req(host, areq->mrq, -EINVAL);
  583. mmc_start_bkops(host->card, true);
  584. /* prepare the request again */
  585. if (areq)
  586. mmc_pre_req(host, areq->mrq);
  587. }
  588. }
  589. if (status == MMC_BLK_SUCCESS && areq)
  590. start_err = __mmc_start_data_req(host, areq->mrq);
  591. if (host->areq)
  592. mmc_post_req(host, host->areq->mrq, 0);
  593. /* Cancel a prepared request if it was not started. */
  594. if ((status != MMC_BLK_SUCCESS || start_err) && areq)
  595. mmc_post_req(host, areq->mrq, -EINVAL);
  596. if (status != MMC_BLK_SUCCESS)
  597. host->areq = NULL;
  598. else
  599. host->areq = areq;
  600. if (ret_stat)
  601. *ret_stat = status;
  602. return data;
  603. }
  604. EXPORT_SYMBOL(mmc_start_req);
  605. /**
  606. * mmc_wait_for_req - start a request and wait for completion
  607. * @host: MMC host to start command
  608. * @mrq: MMC request to start
  609. *
  610. * Start a new MMC custom command request for a host, and wait
  611. * for the command to complete. In the case of 'cap_cmd_during_tfr'
  612. * requests, the transfer is ongoing and the caller can issue further
  613. * commands that do not use the data lines, and then wait by calling
  614. * mmc_wait_for_req_done().
  615. * Does not attempt to parse the response.
  616. */
  617. void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
  618. {
  619. __mmc_start_req(host, mrq);
  620. if (!mrq->cap_cmd_during_tfr)
  621. mmc_wait_for_req_done(host, mrq);
  622. }
  623. EXPORT_SYMBOL(mmc_wait_for_req);
  624. /**
  625. * mmc_interrupt_hpi - Issue for High priority Interrupt
  626. * @card: the MMC card associated with the HPI transfer
  627. *
  628. * Issued High Priority Interrupt, and check for card status
  629. * until out-of prg-state.
  630. */
  631. int mmc_interrupt_hpi(struct mmc_card *card)
  632. {
  633. int err;
  634. u32 status;
  635. unsigned long prg_wait;
  636. if (!card->ext_csd.hpi_en) {
  637. pr_info("%s: HPI enable bit unset\n", mmc_hostname(card->host));
  638. return 1;
  639. }
  640. mmc_claim_host(card->host);
  641. err = mmc_send_status(card, &status);
  642. if (err) {
  643. pr_err("%s: Get card status fail\n", mmc_hostname(card->host));
  644. goto out;
  645. }
  646. switch (R1_CURRENT_STATE(status)) {
  647. case R1_STATE_IDLE:
  648. case R1_STATE_READY:
  649. case R1_STATE_STBY:
  650. case R1_STATE_TRAN:
  651. /*
  652. * In idle and transfer states, HPI is not needed and the caller
  653. * can issue the next intended command immediately
  654. */
  655. goto out;
  656. case R1_STATE_PRG:
  657. break;
  658. default:
  659. /* In all other states, it's illegal to issue HPI */
  660. pr_debug("%s: HPI cannot be sent. Card state=%d\n",
  661. mmc_hostname(card->host), R1_CURRENT_STATE(status));
  662. err = -EINVAL;
  663. goto out;
  664. }
  665. err = mmc_send_hpi_cmd(card, &status);
  666. if (err)
  667. goto out;
  668. prg_wait = jiffies + msecs_to_jiffies(card->ext_csd.out_of_int_time);
  669. do {
  670. err = mmc_send_status(card, &status);
  671. if (!err && R1_CURRENT_STATE(status) == R1_STATE_TRAN)
  672. break;
  673. if (time_after(jiffies, prg_wait))
  674. err = -ETIMEDOUT;
  675. } while (!err);
  676. out:
  677. mmc_release_host(card->host);
  678. return err;
  679. }
  680. EXPORT_SYMBOL(mmc_interrupt_hpi);
  681. /**
  682. * mmc_wait_for_cmd - start a command and wait for completion
  683. * @host: MMC host to start command
  684. * @cmd: MMC command to start
  685. * @retries: maximum number of retries
  686. *
  687. * Start a new MMC command for a host, and wait for the command
  688. * to complete. Return any error that occurred while the command
  689. * was executing. Do not attempt to parse the response.
  690. */
  691. int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
  692. {
  693. struct mmc_request mrq = {};
  694. WARN_ON(!host->claimed);
  695. memset(cmd->resp, 0, sizeof(cmd->resp));
  696. cmd->retries = retries;
  697. mrq.cmd = cmd;
  698. cmd->data = NULL;
  699. mmc_wait_for_req(host, &mrq);
  700. return cmd->error;
  701. }
  702. EXPORT_SYMBOL(mmc_wait_for_cmd);
  703. /**
  704. * mmc_stop_bkops - stop ongoing BKOPS
  705. * @card: MMC card to check BKOPS
  706. *
  707. * Send HPI command to stop ongoing background operations to
  708. * allow rapid servicing of foreground operations, e.g. read/
  709. * writes. Wait until the card comes out of the programming state
  710. * to avoid errors in servicing read/write requests.
  711. */
  712. int mmc_stop_bkops(struct mmc_card *card)
  713. {
  714. int err = 0;
  715. err = mmc_interrupt_hpi(card);
  716. /*
  717. * If err is EINVAL, we can't issue an HPI.
  718. * It should complete the BKOPS.
  719. */
  720. if (!err || (err == -EINVAL)) {
  721. mmc_card_clr_doing_bkops(card);
  722. mmc_retune_release(card->host);
  723. err = 0;
  724. }
  725. return err;
  726. }
  727. EXPORT_SYMBOL(mmc_stop_bkops);
  728. int mmc_read_bkops_status(struct mmc_card *card)
  729. {
  730. int err;
  731. u8 *ext_csd;
  732. mmc_claim_host(card->host);
  733. err = mmc_get_ext_csd(card, &ext_csd);
  734. mmc_release_host(card->host);
  735. if (err)
  736. return err;
  737. card->ext_csd.raw_bkops_status = ext_csd[EXT_CSD_BKOPS_STATUS];
  738. card->ext_csd.raw_exception_status = ext_csd[EXT_CSD_EXP_EVENTS_STATUS];
  739. kfree(ext_csd);
  740. return 0;
  741. }
  742. EXPORT_SYMBOL(mmc_read_bkops_status);
  743. /**
  744. * mmc_set_data_timeout - set the timeout for a data command
  745. * @data: data phase for command
  746. * @card: the MMC card associated with the data transfer
  747. *
  748. * Computes the data timeout parameters according to the
  749. * correct algorithm given the card type.
  750. */
  751. void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
  752. {
  753. unsigned int mult;
  754. /*
  755. * SDIO cards only define an upper 1 s limit on access.
  756. */
  757. if (mmc_card_sdio(card)) {
  758. data->timeout_ns = 1000000000;
  759. data->timeout_clks = 0;
  760. return;
  761. }
  762. /*
  763. * SD cards use a 100 multiplier rather than 10
  764. */
  765. mult = mmc_card_sd(card) ? 100 : 10;
  766. /*
  767. * Scale up the multiplier (and therefore the timeout) by
  768. * the r2w factor for writes.
  769. */
  770. if (data->flags & MMC_DATA_WRITE)
  771. mult <<= card->csd.r2w_factor;
  772. data->timeout_ns = card->csd.tacc_ns * mult;
  773. data->timeout_clks = card->csd.tacc_clks * mult;
  774. /*
  775. * SD cards also have an upper limit on the timeout.
  776. */
  777. if (mmc_card_sd(card)) {
  778. unsigned int timeout_us, limit_us;
  779. timeout_us = data->timeout_ns / 1000;
  780. if (card->host->ios.clock)
  781. timeout_us += data->timeout_clks * 1000 /
  782. (card->host->ios.clock / 1000);
  783. if (data->flags & MMC_DATA_WRITE)
  784. /*
  785. * The MMC spec "It is strongly recommended
  786. * for hosts to implement more than 500ms
  787. * timeout value even if the card indicates
  788. * the 250ms maximum busy length." Even the
  789. * previous value of 300ms is known to be
  790. * insufficient for some cards.
  791. */
  792. limit_us = 3000000;
  793. else
  794. limit_us = 100000;
  795. /*
  796. * SDHC cards always use these fixed values.
  797. */
  798. if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
  799. data->timeout_ns = limit_us * 1000;
  800. data->timeout_clks = 0;
  801. }
  802. /* assign limit value if invalid */
  803. if (timeout_us == 0)
  804. data->timeout_ns = limit_us * 1000;
  805. }
  806. /*
  807. * Some cards require longer data read timeout than indicated in CSD.
  808. * Address this by setting the read timeout to a "reasonably high"
  809. * value. For the cards tested, 600ms has proven enough. If necessary,
  810. * this value can be increased if other problematic cards require this.
  811. */
  812. if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) {
  813. data->timeout_ns = 600000000;
  814. data->timeout_clks = 0;
  815. }
  816. /*
  817. * Some cards need very high timeouts if driven in SPI mode.
  818. * The worst observed timeout was 900ms after writing a
  819. * continuous stream of data until the internal logic
  820. * overflowed.
  821. */
  822. if (mmc_host_is_spi(card->host)) {
  823. if (data->flags & MMC_DATA_WRITE) {
  824. if (data->timeout_ns < 1000000000)
  825. data->timeout_ns = 1000000000; /* 1s */
  826. } else {
  827. if (data->timeout_ns < 100000000)
  828. data->timeout_ns = 100000000; /* 100ms */
  829. }
  830. }
  831. }
  832. EXPORT_SYMBOL(mmc_set_data_timeout);
  833. /**
  834. * mmc_align_data_size - pads a transfer size to a more optimal value
  835. * @card: the MMC card associated with the data transfer
  836. * @sz: original transfer size
  837. *
  838. * Pads the original data size with a number of extra bytes in
  839. * order to avoid controller bugs and/or performance hits
  840. * (e.g. some controllers revert to PIO for certain sizes).
  841. *
  842. * Returns the improved size, which might be unmodified.
  843. *
  844. * Note that this function is only relevant when issuing a
  845. * single scatter gather entry.
  846. */
  847. unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
  848. {
  849. /*
  850. * FIXME: We don't have a system for the controller to tell
  851. * the core about its problems yet, so for now we just 32-bit
  852. * align the size.
  853. */
  854. sz = ((sz + 3) / 4) * 4;
  855. return sz;
  856. }
  857. EXPORT_SYMBOL(mmc_align_data_size);
  858. /**
  859. * __mmc_claim_host - exclusively claim a host
  860. * @host: mmc host to claim
  861. * @abort: whether or not the operation should be aborted
  862. *
  863. * Claim a host for a set of operations. If @abort is non null and
  864. * dereference a non-zero value then this will return prematurely with
  865. * that non-zero value without acquiring the lock. Returns zero
  866. * with the lock held otherwise.
  867. */
  868. int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
  869. {
  870. DECLARE_WAITQUEUE(wait, current);
  871. unsigned long flags;
  872. int stop;
  873. bool pm = false;
  874. might_sleep();
  875. add_wait_queue(&host->wq, &wait);
  876. spin_lock_irqsave(&host->lock, flags);
  877. while (1) {
  878. set_current_state(TASK_UNINTERRUPTIBLE);
  879. stop = abort ? atomic_read(abort) : 0;
  880. if (stop || !host->claimed || host->claimer == current)
  881. break;
  882. spin_unlock_irqrestore(&host->lock, flags);
  883. schedule();
  884. spin_lock_irqsave(&host->lock, flags);
  885. }
  886. set_current_state(TASK_RUNNING);
  887. if (!stop) {
  888. host->claimed = 1;
  889. host->claimer = current;
  890. host->claim_cnt += 1;
  891. if (host->claim_cnt == 1)
  892. pm = true;
  893. } else
  894. wake_up(&host->wq);
  895. spin_unlock_irqrestore(&host->lock, flags);
  896. remove_wait_queue(&host->wq, &wait);
  897. if (pm)
  898. pm_runtime_get_sync(mmc_dev(host));
  899. return stop;
  900. }
  901. EXPORT_SYMBOL(__mmc_claim_host);
  902. /**
  903. * mmc_release_host - release a host
  904. * @host: mmc host to release
  905. *
  906. * Release a MMC host, allowing others to claim the host
  907. * for their operations.
  908. */
  909. void mmc_release_host(struct mmc_host *host)
  910. {
  911. unsigned long flags;
  912. WARN_ON(!host->claimed);
  913. spin_lock_irqsave(&host->lock, flags);
  914. if (--host->claim_cnt) {
  915. /* Release for nested claim */
  916. spin_unlock_irqrestore(&host->lock, flags);
  917. } else {
  918. host->claimed = 0;
  919. host->claimer = NULL;
  920. spin_unlock_irqrestore(&host->lock, flags);
  921. wake_up(&host->wq);
  922. pm_runtime_mark_last_busy(mmc_dev(host));
  923. pm_runtime_put_autosuspend(mmc_dev(host));
  924. }
  925. }
  926. EXPORT_SYMBOL(mmc_release_host);
  927. /*
  928. * This is a helper function, which fetches a runtime pm reference for the
  929. * card device and also claims the host.
  930. */
  931. void mmc_get_card(struct mmc_card *card)
  932. {
  933. pm_runtime_get_sync(&card->dev);
  934. mmc_claim_host(card->host);
  935. }
  936. EXPORT_SYMBOL(mmc_get_card);
  937. /*
  938. * This is a helper function, which releases the host and drops the runtime
  939. * pm reference for the card device.
  940. */
  941. void mmc_put_card(struct mmc_card *card)
  942. {
  943. mmc_release_host(card->host);
  944. pm_runtime_mark_last_busy(&card->dev);
  945. pm_runtime_put_autosuspend(&card->dev);
  946. }
  947. EXPORT_SYMBOL(mmc_put_card);
  948. /*
  949. * Internal function that does the actual ios call to the host driver,
  950. * optionally printing some debug output.
  951. */
  952. static inline void mmc_set_ios(struct mmc_host *host)
  953. {
  954. struct mmc_ios *ios = &host->ios;
  955. pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
  956. "width %u timing %u\n",
  957. mmc_hostname(host), ios->clock, ios->bus_mode,
  958. ios->power_mode, ios->chip_select, ios->vdd,
  959. 1 << ios->bus_width, ios->timing);
  960. host->ops->set_ios(host, ios);
  961. }
  962. /*
  963. * Control chip select pin on a host.
  964. */
  965. void mmc_set_chip_select(struct mmc_host *host, int mode)
  966. {
  967. host->ios.chip_select = mode;
  968. mmc_set_ios(host);
  969. }
  970. /*
  971. * Sets the host clock to the highest possible frequency that
  972. * is below "hz".
  973. */
  974. void mmc_set_clock(struct mmc_host *host, unsigned int hz)
  975. {
  976. WARN_ON(hz && hz < host->f_min);
  977. if (hz > host->f_max)
  978. hz = host->f_max;
  979. host->ios.clock = hz;
  980. mmc_set_ios(host);
  981. }
  982. int mmc_execute_tuning(struct mmc_card *card)
  983. {
  984. struct mmc_host *host = card->host;
  985. u32 opcode;
  986. int err;
  987. if (!host->ops->execute_tuning)
  988. return 0;
  989. if (mmc_card_mmc(card))
  990. opcode = MMC_SEND_TUNING_BLOCK_HS200;
  991. else
  992. opcode = MMC_SEND_TUNING_BLOCK;
  993. err = host->ops->execute_tuning(host, opcode);
  994. if (err)
  995. pr_err("%s: tuning execution failed: %d\n",
  996. mmc_hostname(host), err);
  997. else
  998. mmc_retune_enable(host);
  999. return err;
  1000. }
  1001. /*
  1002. * Change the bus mode (open drain/push-pull) of a host.
  1003. */
  1004. void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
  1005. {
  1006. host->ios.bus_mode = mode;
  1007. mmc_set_ios(host);
  1008. }
  1009. /*
  1010. * Change data bus width of a host.
  1011. */
  1012. void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
  1013. {
  1014. host->ios.bus_width = width;
  1015. mmc_set_ios(host);
  1016. }
  1017. /*
  1018. * Set initial state after a power cycle or a hw_reset.
  1019. */
  1020. void mmc_set_initial_state(struct mmc_host *host)
  1021. {
  1022. mmc_retune_disable(host);
  1023. if (mmc_host_is_spi(host))
  1024. host->ios.chip_select = MMC_CS_HIGH;
  1025. else
  1026. host->ios.chip_select = MMC_CS_DONTCARE;
  1027. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  1028. host->ios.bus_width = MMC_BUS_WIDTH_1;
  1029. host->ios.timing = MMC_TIMING_LEGACY;
  1030. host->ios.drv_type = 0;
  1031. host->ios.enhanced_strobe = false;
  1032. /*
  1033. * Make sure we are in non-enhanced strobe mode before we
  1034. * actually enable it in ext_csd.
  1035. */
  1036. if ((host->caps2 & MMC_CAP2_HS400_ES) &&
  1037. host->ops->hs400_enhanced_strobe)
  1038. host->ops->hs400_enhanced_strobe(host, &host->ios);
  1039. mmc_set_ios(host);
  1040. }
  1041. /**
  1042. * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
  1043. * @vdd: voltage (mV)
  1044. * @low_bits: prefer low bits in boundary cases
  1045. *
  1046. * This function returns the OCR bit number according to the provided @vdd
  1047. * value. If conversion is not possible a negative errno value returned.
  1048. *
  1049. * Depending on the @low_bits flag the function prefers low or high OCR bits
  1050. * on boundary voltages. For example,
  1051. * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
  1052. * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
  1053. *
  1054. * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
  1055. */
  1056. static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
  1057. {
  1058. const int max_bit = ilog2(MMC_VDD_35_36);
  1059. int bit;
  1060. if (vdd < 1650 || vdd > 3600)
  1061. return -EINVAL;
  1062. if (vdd >= 1650 && vdd <= 1950)
  1063. return ilog2(MMC_VDD_165_195);
  1064. if (low_bits)
  1065. vdd -= 1;
  1066. /* Base 2000 mV, step 100 mV, bit's base 8. */
  1067. bit = (vdd - 2000) / 100 + 8;
  1068. if (bit > max_bit)
  1069. return max_bit;
  1070. return bit;
  1071. }
  1072. /**
  1073. * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
  1074. * @vdd_min: minimum voltage value (mV)
  1075. * @vdd_max: maximum voltage value (mV)
  1076. *
  1077. * This function returns the OCR mask bits according to the provided @vdd_min
  1078. * and @vdd_max values. If conversion is not possible the function returns 0.
  1079. *
  1080. * Notes wrt boundary cases:
  1081. * This function sets the OCR bits for all boundary voltages, for example
  1082. * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
  1083. * MMC_VDD_34_35 mask.
  1084. */
  1085. u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
  1086. {
  1087. u32 mask = 0;
  1088. if (vdd_max < vdd_min)
  1089. return 0;
  1090. /* Prefer high bits for the boundary vdd_max values. */
  1091. vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
  1092. if (vdd_max < 0)
  1093. return 0;
  1094. /* Prefer low bits for the boundary vdd_min values. */
  1095. vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
  1096. if (vdd_min < 0)
  1097. return 0;
  1098. /* Fill the mask, from max bit to min bit. */
  1099. while (vdd_max >= vdd_min)
  1100. mask |= 1 << vdd_max--;
  1101. return mask;
  1102. }
  1103. EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
  1104. #ifdef CONFIG_OF
  1105. /**
  1106. * mmc_of_parse_voltage - return mask of supported voltages
  1107. * @np: The device node need to be parsed.
  1108. * @mask: mask of voltages available for MMC/SD/SDIO
  1109. *
  1110. * Parse the "voltage-ranges" DT property, returning zero if it is not
  1111. * found, negative errno if the voltage-range specification is invalid,
  1112. * or one if the voltage-range is specified and successfully parsed.
  1113. */
  1114. int mmc_of_parse_voltage(struct device_node *np, u32 *mask)
  1115. {
  1116. const u32 *voltage_ranges;
  1117. int num_ranges, i;
  1118. voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
  1119. num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
  1120. if (!voltage_ranges) {
  1121. pr_debug("%s: voltage-ranges unspecified\n", np->full_name);
  1122. return 0;
  1123. }
  1124. if (!num_ranges) {
  1125. pr_err("%s: voltage-ranges empty\n", np->full_name);
  1126. return -EINVAL;
  1127. }
  1128. for (i = 0; i < num_ranges; i++) {
  1129. const int j = i * 2;
  1130. u32 ocr_mask;
  1131. ocr_mask = mmc_vddrange_to_ocrmask(
  1132. be32_to_cpu(voltage_ranges[j]),
  1133. be32_to_cpu(voltage_ranges[j + 1]));
  1134. if (!ocr_mask) {
  1135. pr_err("%s: voltage-range #%d is invalid\n",
  1136. np->full_name, i);
  1137. return -EINVAL;
  1138. }
  1139. *mask |= ocr_mask;
  1140. }
  1141. return 1;
  1142. }
  1143. EXPORT_SYMBOL(mmc_of_parse_voltage);
  1144. #endif /* CONFIG_OF */
  1145. static int mmc_of_get_func_num(struct device_node *node)
  1146. {
  1147. u32 reg;
  1148. int ret;
  1149. ret = of_property_read_u32(node, "reg", &reg);
  1150. if (ret < 0)
  1151. return ret;
  1152. return reg;
  1153. }
  1154. struct device_node *mmc_of_find_child_device(struct mmc_host *host,
  1155. unsigned func_num)
  1156. {
  1157. struct device_node *node;
  1158. if (!host->parent || !host->parent->of_node)
  1159. return NULL;
  1160. for_each_child_of_node(host->parent->of_node, node) {
  1161. if (mmc_of_get_func_num(node) == func_num)
  1162. return node;
  1163. }
  1164. return NULL;
  1165. }
  1166. #ifdef CONFIG_REGULATOR
  1167. /**
  1168. * mmc_ocrbitnum_to_vdd - Convert a OCR bit number to its voltage
  1169. * @vdd_bit: OCR bit number
  1170. * @min_uV: minimum voltage value (mV)
  1171. * @max_uV: maximum voltage value (mV)
  1172. *
  1173. * This function returns the voltage range according to the provided OCR
  1174. * bit number. If conversion is not possible a negative errno value returned.
  1175. */
  1176. static int mmc_ocrbitnum_to_vdd(int vdd_bit, int *min_uV, int *max_uV)
  1177. {
  1178. int tmp;
  1179. if (!vdd_bit)
  1180. return -EINVAL;
  1181. /*
  1182. * REVISIT mmc_vddrange_to_ocrmask() may have set some
  1183. * bits this regulator doesn't quite support ... don't
  1184. * be too picky, most cards and regulators are OK with
  1185. * a 0.1V range goof (it's a small error percentage).
  1186. */
  1187. tmp = vdd_bit - ilog2(MMC_VDD_165_195);
  1188. if (tmp == 0) {
  1189. *min_uV = 1650 * 1000;
  1190. *max_uV = 1950 * 1000;
  1191. } else {
  1192. *min_uV = 1900 * 1000 + tmp * 100 * 1000;
  1193. *max_uV = *min_uV + 100 * 1000;
  1194. }
  1195. return 0;
  1196. }
  1197. /**
  1198. * mmc_regulator_get_ocrmask - return mask of supported voltages
  1199. * @supply: regulator to use
  1200. *
  1201. * This returns either a negative errno, or a mask of voltages that
  1202. * can be provided to MMC/SD/SDIO devices using the specified voltage
  1203. * regulator. This would normally be called before registering the
  1204. * MMC host adapter.
  1205. */
  1206. int mmc_regulator_get_ocrmask(struct regulator *supply)
  1207. {
  1208. int result = 0;
  1209. int count;
  1210. int i;
  1211. int vdd_uV;
  1212. int vdd_mV;
  1213. count = regulator_count_voltages(supply);
  1214. if (count < 0)
  1215. return count;
  1216. for (i = 0; i < count; i++) {
  1217. vdd_uV = regulator_list_voltage(supply, i);
  1218. if (vdd_uV <= 0)
  1219. continue;
  1220. vdd_mV = vdd_uV / 1000;
  1221. result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
  1222. }
  1223. if (!result) {
  1224. vdd_uV = regulator_get_voltage(supply);
  1225. if (vdd_uV <= 0)
  1226. return vdd_uV;
  1227. vdd_mV = vdd_uV / 1000;
  1228. result = mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
  1229. }
  1230. return result;
  1231. }
  1232. EXPORT_SYMBOL_GPL(mmc_regulator_get_ocrmask);
  1233. /**
  1234. * mmc_regulator_set_ocr - set regulator to match host->ios voltage
  1235. * @mmc: the host to regulate
  1236. * @supply: regulator to use
  1237. * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
  1238. *
  1239. * Returns zero on success, else negative errno.
  1240. *
  1241. * MMC host drivers may use this to enable or disable a regulator using
  1242. * a particular supply voltage. This would normally be called from the
  1243. * set_ios() method.
  1244. */
  1245. int mmc_regulator_set_ocr(struct mmc_host *mmc,
  1246. struct regulator *supply,
  1247. unsigned short vdd_bit)
  1248. {
  1249. int result = 0;
  1250. int min_uV, max_uV;
  1251. if (vdd_bit) {
  1252. mmc_ocrbitnum_to_vdd(vdd_bit, &min_uV, &max_uV);
  1253. result = regulator_set_voltage(supply, min_uV, max_uV);
  1254. if (result == 0 && !mmc->regulator_enabled) {
  1255. result = regulator_enable(supply);
  1256. if (!result)
  1257. mmc->regulator_enabled = true;
  1258. }
  1259. } else if (mmc->regulator_enabled) {
  1260. result = regulator_disable(supply);
  1261. if (result == 0)
  1262. mmc->regulator_enabled = false;
  1263. }
  1264. if (result)
  1265. dev_err(mmc_dev(mmc),
  1266. "could not set regulator OCR (%d)\n", result);
  1267. return result;
  1268. }
  1269. EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr);
  1270. static int mmc_regulator_set_voltage_if_supported(struct regulator *regulator,
  1271. int min_uV, int target_uV,
  1272. int max_uV)
  1273. {
  1274. /*
  1275. * Check if supported first to avoid errors since we may try several
  1276. * signal levels during power up and don't want to show errors.
  1277. */
  1278. if (!regulator_is_supported_voltage(regulator, min_uV, max_uV))
  1279. return -EINVAL;
  1280. return regulator_set_voltage_triplet(regulator, min_uV, target_uV,
  1281. max_uV);
  1282. }
  1283. /**
  1284. * mmc_regulator_set_vqmmc - Set VQMMC as per the ios
  1285. *
  1286. * For 3.3V signaling, we try to match VQMMC to VMMC as closely as possible.
  1287. * That will match the behavior of old boards where VQMMC and VMMC were supplied
  1288. * by the same supply. The Bus Operating conditions for 3.3V signaling in the
  1289. * SD card spec also define VQMMC in terms of VMMC.
  1290. * If this is not possible we'll try the full 2.7-3.6V of the spec.
  1291. *
  1292. * For 1.2V and 1.8V signaling we'll try to get as close as possible to the
  1293. * requested voltage. This is definitely a good idea for UHS where there's a
  1294. * separate regulator on the card that's trying to make 1.8V and it's best if
  1295. * we match.
  1296. *
  1297. * This function is expected to be used by a controller's
  1298. * start_signal_voltage_switch() function.
  1299. */
  1300. int mmc_regulator_set_vqmmc(struct mmc_host *mmc, struct mmc_ios *ios)
  1301. {
  1302. struct device *dev = mmc_dev(mmc);
  1303. int ret, volt, min_uV, max_uV;
  1304. /* If no vqmmc supply then we can't change the voltage */
  1305. if (IS_ERR(mmc->supply.vqmmc))
  1306. return -EINVAL;
  1307. switch (ios->signal_voltage) {
  1308. case MMC_SIGNAL_VOLTAGE_120:
  1309. return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
  1310. 1100000, 1200000, 1300000);
  1311. case MMC_SIGNAL_VOLTAGE_180:
  1312. return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
  1313. 1700000, 1800000, 1950000);
  1314. case MMC_SIGNAL_VOLTAGE_330:
  1315. ret = mmc_ocrbitnum_to_vdd(mmc->ios.vdd, &volt, &max_uV);
  1316. if (ret < 0)
  1317. return ret;
  1318. dev_dbg(dev, "%s: found vmmc voltage range of %d-%duV\n",
  1319. __func__, volt, max_uV);
  1320. min_uV = max(volt - 300000, 2700000);
  1321. max_uV = min(max_uV + 200000, 3600000);
  1322. /*
  1323. * Due to a limitation in the current implementation of
  1324. * regulator_set_voltage_triplet() which is taking the lowest
  1325. * voltage possible if below the target, search for a suitable
  1326. * voltage in two steps and try to stay close to vmmc
  1327. * with a 0.3V tolerance at first.
  1328. */
  1329. if (!mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
  1330. min_uV, volt, max_uV))
  1331. return 0;
  1332. return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
  1333. 2700000, volt, 3600000);
  1334. default:
  1335. return -EINVAL;
  1336. }
  1337. }
  1338. EXPORT_SYMBOL_GPL(mmc_regulator_set_vqmmc);
  1339. #endif /* CONFIG_REGULATOR */
  1340. int mmc_regulator_get_supply(struct mmc_host *mmc)
  1341. {
  1342. struct device *dev = mmc_dev(mmc);
  1343. int ret;
  1344. mmc->supply.vmmc = devm_regulator_get_optional(dev, "vmmc");
  1345. mmc->supply.vqmmc = devm_regulator_get_optional(dev, "vqmmc");
  1346. if (IS_ERR(mmc->supply.vmmc)) {
  1347. if (PTR_ERR(mmc->supply.vmmc) == -EPROBE_DEFER)
  1348. return -EPROBE_DEFER;
  1349. dev_dbg(dev, "No vmmc regulator found\n");
  1350. } else {
  1351. ret = mmc_regulator_get_ocrmask(mmc->supply.vmmc);
  1352. if (ret > 0)
  1353. mmc->ocr_avail = ret;
  1354. else
  1355. dev_warn(dev, "Failed getting OCR mask: %d\n", ret);
  1356. }
  1357. if (IS_ERR(mmc->supply.vqmmc)) {
  1358. if (PTR_ERR(mmc->supply.vqmmc) == -EPROBE_DEFER)
  1359. return -EPROBE_DEFER;
  1360. dev_dbg(dev, "No vqmmc regulator found\n");
  1361. }
  1362. return 0;
  1363. }
  1364. EXPORT_SYMBOL_GPL(mmc_regulator_get_supply);
  1365. /*
  1366. * Mask off any voltages we don't support and select
  1367. * the lowest voltage
  1368. */
  1369. u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
  1370. {
  1371. int bit;
  1372. /*
  1373. * Sanity check the voltages that the card claims to
  1374. * support.
  1375. */
  1376. if (ocr & 0x7F) {
  1377. dev_warn(mmc_dev(host),
  1378. "card claims to support voltages below defined range\n");
  1379. ocr &= ~0x7F;
  1380. }
  1381. ocr &= host->ocr_avail;
  1382. if (!ocr) {
  1383. dev_warn(mmc_dev(host), "no support for card's volts\n");
  1384. return 0;
  1385. }
  1386. if (host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) {
  1387. bit = ffs(ocr) - 1;
  1388. ocr &= 3 << bit;
  1389. mmc_power_cycle(host, ocr);
  1390. } else {
  1391. bit = fls(ocr) - 1;
  1392. ocr &= 3 << bit;
  1393. if (bit != host->ios.vdd)
  1394. dev_warn(mmc_dev(host), "exceeding card's volts\n");
  1395. }
  1396. return ocr;
  1397. }
  1398. int __mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage)
  1399. {
  1400. int err = 0;
  1401. int old_signal_voltage = host->ios.signal_voltage;
  1402. host->ios.signal_voltage = signal_voltage;
  1403. if (host->ops->start_signal_voltage_switch)
  1404. err = host->ops->start_signal_voltage_switch(host, &host->ios);
  1405. if (err)
  1406. host->ios.signal_voltage = old_signal_voltage;
  1407. return err;
  1408. }
  1409. int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, u32 ocr)
  1410. {
  1411. struct mmc_command cmd = {};
  1412. int err = 0;
  1413. u32 clock;
  1414. /*
  1415. * Send CMD11 only if the request is to switch the card to
  1416. * 1.8V signalling.
  1417. */
  1418. if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
  1419. return __mmc_set_signal_voltage(host, signal_voltage);
  1420. /*
  1421. * If we cannot switch voltages, return failure so the caller
  1422. * can continue without UHS mode
  1423. */
  1424. if (!host->ops->start_signal_voltage_switch)
  1425. return -EPERM;
  1426. if (!host->ops->card_busy)
  1427. pr_warn("%s: cannot verify signal voltage switch\n",
  1428. mmc_hostname(host));
  1429. cmd.opcode = SD_SWITCH_VOLTAGE;
  1430. cmd.arg = 0;
  1431. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1432. err = mmc_wait_for_cmd(host, &cmd, 0);
  1433. if (err)
  1434. return err;
  1435. if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
  1436. return -EIO;
  1437. /*
  1438. * The card should drive cmd and dat[0:3] low immediately
  1439. * after the response of cmd11, but wait 1 ms to be sure
  1440. */
  1441. mmc_delay(1);
  1442. if (host->ops->card_busy && !host->ops->card_busy(host)) {
  1443. err = -EAGAIN;
  1444. goto power_cycle;
  1445. }
  1446. /*
  1447. * During a signal voltage level switch, the clock must be gated
  1448. * for 5 ms according to the SD spec
  1449. */
  1450. clock = host->ios.clock;
  1451. host->ios.clock = 0;
  1452. mmc_set_ios(host);
  1453. if (__mmc_set_signal_voltage(host, signal_voltage)) {
  1454. /*
  1455. * Voltages may not have been switched, but we've already
  1456. * sent CMD11, so a power cycle is required anyway
  1457. */
  1458. err = -EAGAIN;
  1459. goto power_cycle;
  1460. }
  1461. /* Keep clock gated for at least 10 ms, though spec only says 5 ms */
  1462. mmc_delay(10);
  1463. host->ios.clock = clock;
  1464. mmc_set_ios(host);
  1465. /* Wait for at least 1 ms according to spec */
  1466. mmc_delay(1);
  1467. /*
  1468. * Failure to switch is indicated by the card holding
  1469. * dat[0:3] low
  1470. */
  1471. if (host->ops->card_busy && host->ops->card_busy(host))
  1472. err = -EAGAIN;
  1473. power_cycle:
  1474. if (err) {
  1475. pr_debug("%s: Signal voltage switch failed, "
  1476. "power cycling card\n", mmc_hostname(host));
  1477. mmc_power_cycle(host, ocr);
  1478. }
  1479. return err;
  1480. }
  1481. /*
  1482. * Select timing parameters for host.
  1483. */
  1484. void mmc_set_timing(struct mmc_host *host, unsigned int timing)
  1485. {
  1486. host->ios.timing = timing;
  1487. mmc_set_ios(host);
  1488. }
  1489. /*
  1490. * Select appropriate driver type for host.
  1491. */
  1492. void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
  1493. {
  1494. host->ios.drv_type = drv_type;
  1495. mmc_set_ios(host);
  1496. }
  1497. int mmc_select_drive_strength(struct mmc_card *card, unsigned int max_dtr,
  1498. int card_drv_type, int *drv_type)
  1499. {
  1500. struct mmc_host *host = card->host;
  1501. int host_drv_type = SD_DRIVER_TYPE_B;
  1502. *drv_type = 0;
  1503. if (!host->ops->select_drive_strength)
  1504. return 0;
  1505. /* Use SD definition of driver strength for hosts */
  1506. if (host->caps & MMC_CAP_DRIVER_TYPE_A)
  1507. host_drv_type |= SD_DRIVER_TYPE_A;
  1508. if (host->caps & MMC_CAP_DRIVER_TYPE_C)
  1509. host_drv_type |= SD_DRIVER_TYPE_C;
  1510. if (host->caps & MMC_CAP_DRIVER_TYPE_D)
  1511. host_drv_type |= SD_DRIVER_TYPE_D;
  1512. /*
  1513. * The drive strength that the hardware can support
  1514. * depends on the board design. Pass the appropriate
  1515. * information and let the hardware specific code
  1516. * return what is possible given the options
  1517. */
  1518. return host->ops->select_drive_strength(card, max_dtr,
  1519. host_drv_type,
  1520. card_drv_type,
  1521. drv_type);
  1522. }
  1523. /*
  1524. * Apply power to the MMC stack. This is a two-stage process.
  1525. * First, we enable power to the card without the clock running.
  1526. * We then wait a bit for the power to stabilise. Finally,
  1527. * enable the bus drivers and clock to the card.
  1528. *
  1529. * We must _NOT_ enable the clock prior to power stablising.
  1530. *
  1531. * If a host does all the power sequencing itself, ignore the
  1532. * initial MMC_POWER_UP stage.
  1533. */
  1534. void mmc_power_up(struct mmc_host *host, u32 ocr)
  1535. {
  1536. if (host->ios.power_mode == MMC_POWER_ON)
  1537. return;
  1538. mmc_pwrseq_pre_power_on(host);
  1539. host->ios.vdd = fls(ocr) - 1;
  1540. host->ios.power_mode = MMC_POWER_UP;
  1541. /* Set initial state and call mmc_set_ios */
  1542. mmc_set_initial_state(host);
  1543. /* Try to set signal voltage to 3.3V but fall back to 1.8v or 1.2v */
  1544. if (__mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330) == 0)
  1545. dev_dbg(mmc_dev(host), "Initial signal voltage of 3.3v\n");
  1546. else if (__mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180) == 0)
  1547. dev_dbg(mmc_dev(host), "Initial signal voltage of 1.8v\n");
  1548. else if (__mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120) == 0)
  1549. dev_dbg(mmc_dev(host), "Initial signal voltage of 1.2v\n");
  1550. /*
  1551. * This delay should be sufficient to allow the power supply
  1552. * to reach the minimum voltage.
  1553. */
  1554. mmc_delay(10);
  1555. mmc_pwrseq_post_power_on(host);
  1556. host->ios.clock = host->f_init;
  1557. host->ios.power_mode = MMC_POWER_ON;
  1558. mmc_set_ios(host);
  1559. /*
  1560. * This delay must be at least 74 clock sizes, or 1 ms, or the
  1561. * time required to reach a stable voltage.
  1562. */
  1563. mmc_delay(10);
  1564. }
  1565. void mmc_power_off(struct mmc_host *host)
  1566. {
  1567. if (host->ios.power_mode == MMC_POWER_OFF)
  1568. return;
  1569. mmc_pwrseq_power_off(host);
  1570. host->ios.clock = 0;
  1571. host->ios.vdd = 0;
  1572. host->ios.power_mode = MMC_POWER_OFF;
  1573. /* Set initial state and call mmc_set_ios */
  1574. mmc_set_initial_state(host);
  1575. /*
  1576. * Some configurations, such as the 802.11 SDIO card in the OLPC
  1577. * XO-1.5, require a short delay after poweroff before the card
  1578. * can be successfully turned on again.
  1579. */
  1580. mmc_delay(1);
  1581. }
  1582. void mmc_power_cycle(struct mmc_host *host, u32 ocr)
  1583. {
  1584. mmc_power_off(host);
  1585. /* Wait at least 1 ms according to SD spec */
  1586. mmc_delay(1);
  1587. mmc_power_up(host, ocr);
  1588. }
  1589. /*
  1590. * Cleanup when the last reference to the bus operator is dropped.
  1591. */
  1592. static void __mmc_release_bus(struct mmc_host *host)
  1593. {
  1594. WARN_ON(!host->bus_dead);
  1595. host->bus_ops = NULL;
  1596. }
  1597. /*
  1598. * Increase reference count of bus operator
  1599. */
  1600. static inline void mmc_bus_get(struct mmc_host *host)
  1601. {
  1602. unsigned long flags;
  1603. spin_lock_irqsave(&host->lock, flags);
  1604. host->bus_refs++;
  1605. spin_unlock_irqrestore(&host->lock, flags);
  1606. }
  1607. /*
  1608. * Decrease reference count of bus operator and free it if
  1609. * it is the last reference.
  1610. */
  1611. static inline void mmc_bus_put(struct mmc_host *host)
  1612. {
  1613. unsigned long flags;
  1614. spin_lock_irqsave(&host->lock, flags);
  1615. host->bus_refs--;
  1616. if ((host->bus_refs == 0) && host->bus_ops)
  1617. __mmc_release_bus(host);
  1618. spin_unlock_irqrestore(&host->lock, flags);
  1619. }
  1620. /*
  1621. * Assign a mmc bus handler to a host. Only one bus handler may control a
  1622. * host at any given time.
  1623. */
  1624. void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
  1625. {
  1626. unsigned long flags;
  1627. WARN_ON(!host->claimed);
  1628. spin_lock_irqsave(&host->lock, flags);
  1629. WARN_ON(host->bus_ops);
  1630. WARN_ON(host->bus_refs);
  1631. host->bus_ops = ops;
  1632. host->bus_refs = 1;
  1633. host->bus_dead = 0;
  1634. spin_unlock_irqrestore(&host->lock, flags);
  1635. }
  1636. /*
  1637. * Remove the current bus handler from a host.
  1638. */
  1639. void mmc_detach_bus(struct mmc_host *host)
  1640. {
  1641. unsigned long flags;
  1642. WARN_ON(!host->claimed);
  1643. WARN_ON(!host->bus_ops);
  1644. spin_lock_irqsave(&host->lock, flags);
  1645. host->bus_dead = 1;
  1646. spin_unlock_irqrestore(&host->lock, flags);
  1647. mmc_bus_put(host);
  1648. }
  1649. static void _mmc_detect_change(struct mmc_host *host, unsigned long delay,
  1650. bool cd_irq)
  1651. {
  1652. #ifdef CONFIG_MMC_DEBUG
  1653. unsigned long flags;
  1654. spin_lock_irqsave(&host->lock, flags);
  1655. WARN_ON(host->removed);
  1656. spin_unlock_irqrestore(&host->lock, flags);
  1657. #endif
  1658. /*
  1659. * If the device is configured as wakeup, we prevent a new sleep for
  1660. * 5 s to give provision for user space to consume the event.
  1661. */
  1662. if (cd_irq && !(host->caps & MMC_CAP_NEEDS_POLL) &&
  1663. device_can_wakeup(mmc_dev(host)))
  1664. pm_wakeup_event(mmc_dev(host), 5000);
  1665. host->detect_change = 1;
  1666. mmc_schedule_delayed_work(&host->detect, delay);
  1667. }
  1668. /**
  1669. * mmc_detect_change - process change of state on a MMC socket
  1670. * @host: host which changed state.
  1671. * @delay: optional delay to wait before detection (jiffies)
  1672. *
  1673. * MMC drivers should call this when they detect a card has been
  1674. * inserted or removed. The MMC layer will confirm that any
  1675. * present card is still functional, and initialize any newly
  1676. * inserted.
  1677. */
  1678. void mmc_detect_change(struct mmc_host *host, unsigned long delay)
  1679. {
  1680. _mmc_detect_change(host, delay, true);
  1681. }
  1682. EXPORT_SYMBOL(mmc_detect_change);
  1683. void mmc_init_erase(struct mmc_card *card)
  1684. {
  1685. unsigned int sz;
  1686. if (is_power_of_2(card->erase_size))
  1687. card->erase_shift = ffs(card->erase_size) - 1;
  1688. else
  1689. card->erase_shift = 0;
  1690. /*
  1691. * It is possible to erase an arbitrarily large area of an SD or MMC
  1692. * card. That is not desirable because it can take a long time
  1693. * (minutes) potentially delaying more important I/O, and also the
  1694. * timeout calculations become increasingly hugely over-estimated.
  1695. * Consequently, 'pref_erase' is defined as a guide to limit erases
  1696. * to that size and alignment.
  1697. *
  1698. * For SD cards that define Allocation Unit size, limit erases to one
  1699. * Allocation Unit at a time.
  1700. * For MMC, have a stab at ai good value and for modern cards it will
  1701. * end up being 4MiB. Note that if the value is too small, it can end
  1702. * up taking longer to erase. Also note, erase_size is already set to
  1703. * High Capacity Erase Size if available when this function is called.
  1704. */
  1705. if (mmc_card_sd(card) && card->ssr.au) {
  1706. card->pref_erase = card->ssr.au;
  1707. card->erase_shift = ffs(card->ssr.au) - 1;
  1708. } else if (card->erase_size) {
  1709. sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
  1710. if (sz < 128)
  1711. card->pref_erase = 512 * 1024 / 512;
  1712. else if (sz < 512)
  1713. card->pref_erase = 1024 * 1024 / 512;
  1714. else if (sz < 1024)
  1715. card->pref_erase = 2 * 1024 * 1024 / 512;
  1716. else
  1717. card->pref_erase = 4 * 1024 * 1024 / 512;
  1718. if (card->pref_erase < card->erase_size)
  1719. card->pref_erase = card->erase_size;
  1720. else {
  1721. sz = card->pref_erase % card->erase_size;
  1722. if (sz)
  1723. card->pref_erase += card->erase_size - sz;
  1724. }
  1725. } else
  1726. card->pref_erase = 0;
  1727. }
  1728. static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
  1729. unsigned int arg, unsigned int qty)
  1730. {
  1731. unsigned int erase_timeout;
  1732. if (arg == MMC_DISCARD_ARG ||
  1733. (arg == MMC_TRIM_ARG && card->ext_csd.rev >= 6)) {
  1734. erase_timeout = card->ext_csd.trim_timeout;
  1735. } else if (card->ext_csd.erase_group_def & 1) {
  1736. /* High Capacity Erase Group Size uses HC timeouts */
  1737. if (arg == MMC_TRIM_ARG)
  1738. erase_timeout = card->ext_csd.trim_timeout;
  1739. else
  1740. erase_timeout = card->ext_csd.hc_erase_timeout;
  1741. } else {
  1742. /* CSD Erase Group Size uses write timeout */
  1743. unsigned int mult = (10 << card->csd.r2w_factor);
  1744. unsigned int timeout_clks = card->csd.tacc_clks * mult;
  1745. unsigned int timeout_us;
  1746. /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
  1747. if (card->csd.tacc_ns < 1000000)
  1748. timeout_us = (card->csd.tacc_ns * mult) / 1000;
  1749. else
  1750. timeout_us = (card->csd.tacc_ns / 1000) * mult;
  1751. /*
  1752. * ios.clock is only a target. The real clock rate might be
  1753. * less but not that much less, so fudge it by multiplying by 2.
  1754. */
  1755. timeout_clks <<= 1;
  1756. timeout_us += (timeout_clks * 1000) /
  1757. (card->host->ios.clock / 1000);
  1758. erase_timeout = timeout_us / 1000;
  1759. /*
  1760. * Theoretically, the calculation could underflow so round up
  1761. * to 1ms in that case.
  1762. */
  1763. if (!erase_timeout)
  1764. erase_timeout = 1;
  1765. }
  1766. /* Multiplier for secure operations */
  1767. if (arg & MMC_SECURE_ARGS) {
  1768. if (arg == MMC_SECURE_ERASE_ARG)
  1769. erase_timeout *= card->ext_csd.sec_erase_mult;
  1770. else
  1771. erase_timeout *= card->ext_csd.sec_trim_mult;
  1772. }
  1773. erase_timeout *= qty;
  1774. /*
  1775. * Ensure at least a 1 second timeout for SPI as per
  1776. * 'mmc_set_data_timeout()'
  1777. */
  1778. if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
  1779. erase_timeout = 1000;
  1780. return erase_timeout;
  1781. }
  1782. static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
  1783. unsigned int arg,
  1784. unsigned int qty)
  1785. {
  1786. unsigned int erase_timeout;
  1787. if (card->ssr.erase_timeout) {
  1788. /* Erase timeout specified in SD Status Register (SSR) */
  1789. erase_timeout = card->ssr.erase_timeout * qty +
  1790. card->ssr.erase_offset;
  1791. } else {
  1792. /*
  1793. * Erase timeout not specified in SD Status Register (SSR) so
  1794. * use 250ms per write block.
  1795. */
  1796. erase_timeout = 250 * qty;
  1797. }
  1798. /* Must not be less than 1 second */
  1799. if (erase_timeout < 1000)
  1800. erase_timeout = 1000;
  1801. return erase_timeout;
  1802. }
  1803. static unsigned int mmc_erase_timeout(struct mmc_card *card,
  1804. unsigned int arg,
  1805. unsigned int qty)
  1806. {
  1807. if (mmc_card_sd(card))
  1808. return mmc_sd_erase_timeout(card, arg, qty);
  1809. else
  1810. return mmc_mmc_erase_timeout(card, arg, qty);
  1811. }
  1812. static int mmc_do_erase(struct mmc_card *card, unsigned int from,
  1813. unsigned int to, unsigned int arg)
  1814. {
  1815. struct mmc_command cmd = {};
  1816. unsigned int qty = 0, busy_timeout = 0;
  1817. bool use_r1b_resp = false;
  1818. unsigned long timeout;
  1819. int err;
  1820. mmc_retune_hold(card->host);
  1821. /*
  1822. * qty is used to calculate the erase timeout which depends on how many
  1823. * erase groups (or allocation units in SD terminology) are affected.
  1824. * We count erasing part of an erase group as one erase group.
  1825. * For SD, the allocation units are always a power of 2. For MMC, the
  1826. * erase group size is almost certainly also power of 2, but it does not
  1827. * seem to insist on that in the JEDEC standard, so we fall back to
  1828. * division in that case. SD may not specify an allocation unit size,
  1829. * in which case the timeout is based on the number of write blocks.
  1830. *
  1831. * Note that the timeout for secure trim 2 will only be correct if the
  1832. * number of erase groups specified is the same as the total of all
  1833. * preceding secure trim 1 commands. Since the power may have been
  1834. * lost since the secure trim 1 commands occurred, it is generally
  1835. * impossible to calculate the secure trim 2 timeout correctly.
  1836. */
  1837. if (card->erase_shift)
  1838. qty += ((to >> card->erase_shift) -
  1839. (from >> card->erase_shift)) + 1;
  1840. else if (mmc_card_sd(card))
  1841. qty += to - from + 1;
  1842. else
  1843. qty += ((to / card->erase_size) -
  1844. (from / card->erase_size)) + 1;
  1845. if (!mmc_card_blockaddr(card)) {
  1846. from <<= 9;
  1847. to <<= 9;
  1848. }
  1849. if (mmc_card_sd(card))
  1850. cmd.opcode = SD_ERASE_WR_BLK_START;
  1851. else
  1852. cmd.opcode = MMC_ERASE_GROUP_START;
  1853. cmd.arg = from;
  1854. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1855. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1856. if (err) {
  1857. pr_err("mmc_erase: group start error %d, "
  1858. "status %#x\n", err, cmd.resp[0]);
  1859. err = -EIO;
  1860. goto out;
  1861. }
  1862. memset(&cmd, 0, sizeof(struct mmc_command));
  1863. if (mmc_card_sd(card))
  1864. cmd.opcode = SD_ERASE_WR_BLK_END;
  1865. else
  1866. cmd.opcode = MMC_ERASE_GROUP_END;
  1867. cmd.arg = to;
  1868. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1869. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1870. if (err) {
  1871. pr_err("mmc_erase: group end error %d, status %#x\n",
  1872. err, cmd.resp[0]);
  1873. err = -EIO;
  1874. goto out;
  1875. }
  1876. memset(&cmd, 0, sizeof(struct mmc_command));
  1877. cmd.opcode = MMC_ERASE;
  1878. cmd.arg = arg;
  1879. busy_timeout = mmc_erase_timeout(card, arg, qty);
  1880. /*
  1881. * If the host controller supports busy signalling and the timeout for
  1882. * the erase operation does not exceed the max_busy_timeout, we should
  1883. * use R1B response. Or we need to prevent the host from doing hw busy
  1884. * detection, which is done by converting to a R1 response instead.
  1885. */
  1886. if (card->host->max_busy_timeout &&
  1887. busy_timeout > card->host->max_busy_timeout) {
  1888. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1889. } else {
  1890. cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  1891. cmd.busy_timeout = busy_timeout;
  1892. use_r1b_resp = true;
  1893. }
  1894. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1895. if (err) {
  1896. pr_err("mmc_erase: erase error %d, status %#x\n",
  1897. err, cmd.resp[0]);
  1898. err = -EIO;
  1899. goto out;
  1900. }
  1901. if (mmc_host_is_spi(card->host))
  1902. goto out;
  1903. /*
  1904. * In case of when R1B + MMC_CAP_WAIT_WHILE_BUSY is used, the polling
  1905. * shall be avoided.
  1906. */
  1907. if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
  1908. goto out;
  1909. timeout = jiffies + msecs_to_jiffies(busy_timeout);
  1910. do {
  1911. memset(&cmd, 0, sizeof(struct mmc_command));
  1912. cmd.opcode = MMC_SEND_STATUS;
  1913. cmd.arg = card->rca << 16;
  1914. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1915. /* Do not retry else we can't see errors */
  1916. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1917. if (err || (cmd.resp[0] & 0xFDF92000)) {
  1918. pr_err("error %d requesting status %#x\n",
  1919. err, cmd.resp[0]);
  1920. err = -EIO;
  1921. goto out;
  1922. }
  1923. /* Timeout if the device never becomes ready for data and
  1924. * never leaves the program state.
  1925. */
  1926. if (time_after(jiffies, timeout)) {
  1927. pr_err("%s: Card stuck in programming state! %s\n",
  1928. mmc_hostname(card->host), __func__);
  1929. err = -EIO;
  1930. goto out;
  1931. }
  1932. } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
  1933. (R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG));
  1934. out:
  1935. mmc_retune_release(card->host);
  1936. return err;
  1937. }
  1938. static unsigned int mmc_align_erase_size(struct mmc_card *card,
  1939. unsigned int *from,
  1940. unsigned int *to,
  1941. unsigned int nr)
  1942. {
  1943. unsigned int from_new = *from, nr_new = nr, rem;
  1944. /*
  1945. * When the 'card->erase_size' is power of 2, we can use round_up/down()
  1946. * to align the erase size efficiently.
  1947. */
  1948. if (is_power_of_2(card->erase_size)) {
  1949. unsigned int temp = from_new;
  1950. from_new = round_up(temp, card->erase_size);
  1951. rem = from_new - temp;
  1952. if (nr_new > rem)
  1953. nr_new -= rem;
  1954. else
  1955. return 0;
  1956. nr_new = round_down(nr_new, card->erase_size);
  1957. } else {
  1958. rem = from_new % card->erase_size;
  1959. if (rem) {
  1960. rem = card->erase_size - rem;
  1961. from_new += rem;
  1962. if (nr_new > rem)
  1963. nr_new -= rem;
  1964. else
  1965. return 0;
  1966. }
  1967. rem = nr_new % card->erase_size;
  1968. if (rem)
  1969. nr_new -= rem;
  1970. }
  1971. if (nr_new == 0)
  1972. return 0;
  1973. *to = from_new + nr_new;
  1974. *from = from_new;
  1975. return nr_new;
  1976. }
  1977. /**
  1978. * mmc_erase - erase sectors.
  1979. * @card: card to erase
  1980. * @from: first sector to erase
  1981. * @nr: number of sectors to erase
  1982. * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
  1983. *
  1984. * Caller must claim host before calling this function.
  1985. */
  1986. int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
  1987. unsigned int arg)
  1988. {
  1989. unsigned int rem, to = from + nr;
  1990. int err;
  1991. if (!(card->host->caps & MMC_CAP_ERASE) ||
  1992. !(card->csd.cmdclass & CCC_ERASE))
  1993. return -EOPNOTSUPP;
  1994. if (!card->erase_size)
  1995. return -EOPNOTSUPP;
  1996. if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
  1997. return -EOPNOTSUPP;
  1998. if ((arg & MMC_SECURE_ARGS) &&
  1999. !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
  2000. return -EOPNOTSUPP;
  2001. if ((arg & MMC_TRIM_ARGS) &&
  2002. !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
  2003. return -EOPNOTSUPP;
  2004. if (arg == MMC_SECURE_ERASE_ARG) {
  2005. if (from % card->erase_size || nr % card->erase_size)
  2006. return -EINVAL;
  2007. }
  2008. if (arg == MMC_ERASE_ARG)
  2009. nr = mmc_align_erase_size(card, &from, &to, nr);
  2010. if (nr == 0)
  2011. return 0;
  2012. if (to <= from)
  2013. return -EINVAL;
  2014. /* 'from' and 'to' are inclusive */
  2015. to -= 1;
  2016. /*
  2017. * Special case where only one erase-group fits in the timeout budget:
  2018. * If the region crosses an erase-group boundary on this particular
  2019. * case, we will be trimming more than one erase-group which, does not
  2020. * fit in the timeout budget of the controller, so we need to split it
  2021. * and call mmc_do_erase() twice if necessary. This special case is
  2022. * identified by the card->eg_boundary flag.
  2023. */
  2024. rem = card->erase_size - (from % card->erase_size);
  2025. if ((arg & MMC_TRIM_ARGS) && (card->eg_boundary) && (nr > rem)) {
  2026. err = mmc_do_erase(card, from, from + rem - 1, arg);
  2027. from += rem;
  2028. if ((err) || (to <= from))
  2029. return err;
  2030. }
  2031. return mmc_do_erase(card, from, to, arg);
  2032. }
  2033. EXPORT_SYMBOL(mmc_erase);
  2034. int mmc_can_erase(struct mmc_card *card)
  2035. {
  2036. if ((card->host->caps & MMC_CAP_ERASE) &&
  2037. (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
  2038. return 1;
  2039. return 0;
  2040. }
  2041. EXPORT_SYMBOL(mmc_can_erase);
  2042. int mmc_can_trim(struct mmc_card *card)
  2043. {
  2044. if ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) &&
  2045. (!(card->quirks & MMC_QUIRK_TRIM_BROKEN)))
  2046. return 1;
  2047. return 0;
  2048. }
  2049. EXPORT_SYMBOL(mmc_can_trim);
  2050. int mmc_can_discard(struct mmc_card *card)
  2051. {
  2052. /*
  2053. * As there's no way to detect the discard support bit at v4.5
  2054. * use the s/w feature support filed.
  2055. */
  2056. if (card->ext_csd.feature_support & MMC_DISCARD_FEATURE)
  2057. return 1;
  2058. return 0;
  2059. }
  2060. EXPORT_SYMBOL(mmc_can_discard);
  2061. int mmc_can_sanitize(struct mmc_card *card)
  2062. {
  2063. if (!mmc_can_trim(card) && !mmc_can_erase(card))
  2064. return 0;
  2065. if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE)
  2066. return 1;
  2067. return 0;
  2068. }
  2069. EXPORT_SYMBOL(mmc_can_sanitize);
  2070. int mmc_can_secure_erase_trim(struct mmc_card *card)
  2071. {
  2072. if ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) &&
  2073. !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
  2074. return 1;
  2075. return 0;
  2076. }
  2077. EXPORT_SYMBOL(mmc_can_secure_erase_trim);
  2078. int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
  2079. unsigned int nr)
  2080. {
  2081. if (!card->erase_size)
  2082. return 0;
  2083. if (from % card->erase_size || nr % card->erase_size)
  2084. return 0;
  2085. return 1;
  2086. }
  2087. EXPORT_SYMBOL(mmc_erase_group_aligned);
  2088. static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
  2089. unsigned int arg)
  2090. {
  2091. struct mmc_host *host = card->host;
  2092. unsigned int max_discard, x, y, qty = 0, max_qty, min_qty, timeout;
  2093. unsigned int last_timeout = 0;
  2094. unsigned int max_busy_timeout = host->max_busy_timeout ?
  2095. host->max_busy_timeout : MMC_ERASE_TIMEOUT_MS;
  2096. if (card->erase_shift) {
  2097. max_qty = UINT_MAX >> card->erase_shift;
  2098. min_qty = card->pref_erase >> card->erase_shift;
  2099. } else if (mmc_card_sd(card)) {
  2100. max_qty = UINT_MAX;
  2101. min_qty = card->pref_erase;
  2102. } else {
  2103. max_qty = UINT_MAX / card->erase_size;
  2104. min_qty = card->pref_erase / card->erase_size;
  2105. }
  2106. /*
  2107. * We should not only use 'host->max_busy_timeout' as the limitation
  2108. * when deciding the max discard sectors. We should set a balance value
  2109. * to improve the erase speed, and it can not get too long timeout at
  2110. * the same time.
  2111. *
  2112. * Here we set 'card->pref_erase' as the minimal discard sectors no
  2113. * matter what size of 'host->max_busy_timeout', but if the
  2114. * 'host->max_busy_timeout' is large enough for more discard sectors,
  2115. * then we can continue to increase the max discard sectors until we
  2116. * get a balance value. In cases when the 'host->max_busy_timeout'
  2117. * isn't specified, use the default max erase timeout.
  2118. */
  2119. do {
  2120. y = 0;
  2121. for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
  2122. timeout = mmc_erase_timeout(card, arg, qty + x);
  2123. if (qty + x > min_qty && timeout > max_busy_timeout)
  2124. break;
  2125. if (timeout < last_timeout)
  2126. break;
  2127. last_timeout = timeout;
  2128. y = x;
  2129. }
  2130. qty += y;
  2131. } while (y);
  2132. if (!qty)
  2133. return 0;
  2134. /*
  2135. * When specifying a sector range to trim, chances are we might cross
  2136. * an erase-group boundary even if the amount of sectors is less than
  2137. * one erase-group.
  2138. * If we can only fit one erase-group in the controller timeout budget,
  2139. * we have to care that erase-group boundaries are not crossed by a
  2140. * single trim operation. We flag that special case with "eg_boundary".
  2141. * In all other cases we can just decrement qty and pretend that we
  2142. * always touch (qty + 1) erase-groups as a simple optimization.
  2143. */
  2144. if (qty == 1)
  2145. card->eg_boundary = 1;
  2146. else
  2147. qty--;
  2148. /* Convert qty to sectors */
  2149. if (card->erase_shift)
  2150. max_discard = qty << card->erase_shift;
  2151. else if (mmc_card_sd(card))
  2152. max_discard = qty + 1;
  2153. else
  2154. max_discard = qty * card->erase_size;
  2155. return max_discard;
  2156. }
  2157. unsigned int mmc_calc_max_discard(struct mmc_card *card)
  2158. {
  2159. struct mmc_host *host = card->host;
  2160. unsigned int max_discard, max_trim;
  2161. /*
  2162. * Without erase_group_def set, MMC erase timeout depends on clock
  2163. * frequence which can change. In that case, the best choice is
  2164. * just the preferred erase size.
  2165. */
  2166. if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
  2167. return card->pref_erase;
  2168. max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
  2169. if (mmc_can_trim(card)) {
  2170. max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
  2171. if (max_trim < max_discard)
  2172. max_discard = max_trim;
  2173. } else if (max_discard < card->erase_size) {
  2174. max_discard = 0;
  2175. }
  2176. pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
  2177. mmc_hostname(host), max_discard, host->max_busy_timeout ?
  2178. host->max_busy_timeout : MMC_ERASE_TIMEOUT_MS);
  2179. return max_discard;
  2180. }
  2181. EXPORT_SYMBOL(mmc_calc_max_discard);
  2182. int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
  2183. {
  2184. struct mmc_command cmd = {};
  2185. if (mmc_card_blockaddr(card) || mmc_card_ddr52(card) ||
  2186. mmc_card_hs400(card) || mmc_card_hs400es(card))
  2187. return 0;
  2188. cmd.opcode = MMC_SET_BLOCKLEN;
  2189. cmd.arg = blocklen;
  2190. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  2191. return mmc_wait_for_cmd(card->host, &cmd, 5);
  2192. }
  2193. EXPORT_SYMBOL(mmc_set_blocklen);
  2194. int mmc_set_blockcount(struct mmc_card *card, unsigned int blockcount,
  2195. bool is_rel_write)
  2196. {
  2197. struct mmc_command cmd = {};
  2198. cmd.opcode = MMC_SET_BLOCK_COUNT;
  2199. cmd.arg = blockcount & 0x0000FFFF;
  2200. if (is_rel_write)
  2201. cmd.arg |= 1 << 31;
  2202. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  2203. return mmc_wait_for_cmd(card->host, &cmd, 5);
  2204. }
  2205. EXPORT_SYMBOL(mmc_set_blockcount);
  2206. static void mmc_hw_reset_for_init(struct mmc_host *host)
  2207. {
  2208. if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
  2209. return;
  2210. host->ops->hw_reset(host);
  2211. }
  2212. int mmc_hw_reset(struct mmc_host *host)
  2213. {
  2214. int ret;
  2215. if (!host->card)
  2216. return -EINVAL;
  2217. mmc_bus_get(host);
  2218. if (!host->bus_ops || host->bus_dead || !host->bus_ops->reset) {
  2219. mmc_bus_put(host);
  2220. return -EOPNOTSUPP;
  2221. }
  2222. ret = host->bus_ops->reset(host);
  2223. mmc_bus_put(host);
  2224. if (ret)
  2225. pr_warn("%s: tried to reset card, got error %d\n",
  2226. mmc_hostname(host), ret);
  2227. return ret;
  2228. }
  2229. EXPORT_SYMBOL(mmc_hw_reset);
  2230. static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
  2231. {
  2232. host->f_init = freq;
  2233. #ifdef CONFIG_MMC_DEBUG
  2234. pr_info("%s: %s: trying to init card at %u Hz\n",
  2235. mmc_hostname(host), __func__, host->f_init);
  2236. #endif
  2237. mmc_power_up(host, host->ocr_avail);
  2238. /*
  2239. * Some eMMCs (with VCCQ always on) may not be reset after power up, so
  2240. * do a hardware reset if possible.
  2241. */
  2242. mmc_hw_reset_for_init(host);
  2243. /*
  2244. * sdio_reset sends CMD52 to reset card. Since we do not know
  2245. * if the card is being re-initialized, just send it. CMD52
  2246. * should be ignored by SD/eMMC cards.
  2247. * Skip it if we already know that we do not support SDIO commands
  2248. */
  2249. if (!(host->caps2 & MMC_CAP2_NO_SDIO))
  2250. sdio_reset(host);
  2251. mmc_go_idle(host);
  2252. if (!(host->caps2 & MMC_CAP2_NO_SD))
  2253. mmc_send_if_cond(host, host->ocr_avail);
  2254. /* Order's important: probe SDIO, then SD, then MMC */
  2255. if (!(host->caps2 & MMC_CAP2_NO_SDIO))
  2256. if (!mmc_attach_sdio(host))
  2257. return 0;
  2258. if (!(host->caps2 & MMC_CAP2_NO_SD))
  2259. if (!mmc_attach_sd(host))
  2260. return 0;
  2261. if (!(host->caps2 & MMC_CAP2_NO_MMC))
  2262. if (!mmc_attach_mmc(host))
  2263. return 0;
  2264. mmc_power_off(host);
  2265. return -EIO;
  2266. }
  2267. int _mmc_detect_card_removed(struct mmc_host *host)
  2268. {
  2269. int ret;
  2270. if (!host->card || mmc_card_removed(host->card))
  2271. return 1;
  2272. ret = host->bus_ops->alive(host);
  2273. /*
  2274. * Card detect status and alive check may be out of sync if card is
  2275. * removed slowly, when card detect switch changes while card/slot
  2276. * pads are still contacted in hardware (refer to "SD Card Mechanical
  2277. * Addendum, Appendix C: Card Detection Switch"). So reschedule a
  2278. * detect work 200ms later for this case.
  2279. */
  2280. if (!ret && host->ops->get_cd && !host->ops->get_cd(host)) {
  2281. mmc_detect_change(host, msecs_to_jiffies(200));
  2282. pr_debug("%s: card removed too slowly\n", mmc_hostname(host));
  2283. }
  2284. if (ret) {
  2285. mmc_card_set_removed(host->card);
  2286. pr_debug("%s: card remove detected\n", mmc_hostname(host));
  2287. }
  2288. return ret;
  2289. }
  2290. int mmc_detect_card_removed(struct mmc_host *host)
  2291. {
  2292. struct mmc_card *card = host->card;
  2293. int ret;
  2294. WARN_ON(!host->claimed);
  2295. if (!card)
  2296. return 1;
  2297. if (!mmc_card_is_removable(host))
  2298. return 0;
  2299. ret = mmc_card_removed(card);
  2300. /*
  2301. * The card will be considered unchanged unless we have been asked to
  2302. * detect a change or host requires polling to provide card detection.
  2303. */
  2304. if (!host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL))
  2305. return ret;
  2306. host->detect_change = 0;
  2307. if (!ret) {
  2308. ret = _mmc_detect_card_removed(host);
  2309. if (ret && (host->caps & MMC_CAP_NEEDS_POLL)) {
  2310. /*
  2311. * Schedule a detect work as soon as possible to let a
  2312. * rescan handle the card removal.
  2313. */
  2314. cancel_delayed_work(&host->detect);
  2315. _mmc_detect_change(host, 0, false);
  2316. }
  2317. }
  2318. return ret;
  2319. }
  2320. EXPORT_SYMBOL(mmc_detect_card_removed);
  2321. void mmc_rescan(struct work_struct *work)
  2322. {
  2323. struct mmc_host *host =
  2324. container_of(work, struct mmc_host, detect.work);
  2325. int i;
  2326. if (host->rescan_disable)
  2327. return;
  2328. /* If there is a non-removable card registered, only scan once */
  2329. if (!mmc_card_is_removable(host) && host->rescan_entered)
  2330. return;
  2331. host->rescan_entered = 1;
  2332. if (host->trigger_card_event && host->ops->card_event) {
  2333. mmc_claim_host(host);
  2334. host->ops->card_event(host);
  2335. mmc_release_host(host);
  2336. host->trigger_card_event = false;
  2337. }
  2338. mmc_bus_get(host);
  2339. /*
  2340. * if there is a _removable_ card registered, check whether it is
  2341. * still present
  2342. */
  2343. if (host->bus_ops && !host->bus_dead && mmc_card_is_removable(host))
  2344. host->bus_ops->detect(host);
  2345. host->detect_change = 0;
  2346. /*
  2347. * Let mmc_bus_put() free the bus/bus_ops if we've found that
  2348. * the card is no longer present.
  2349. */
  2350. mmc_bus_put(host);
  2351. mmc_bus_get(host);
  2352. /* if there still is a card present, stop here */
  2353. if (host->bus_ops != NULL) {
  2354. mmc_bus_put(host);
  2355. goto out;
  2356. }
  2357. /*
  2358. * Only we can add a new handler, so it's safe to
  2359. * release the lock here.
  2360. */
  2361. mmc_bus_put(host);
  2362. mmc_claim_host(host);
  2363. if (mmc_card_is_removable(host) && host->ops->get_cd &&
  2364. host->ops->get_cd(host) == 0) {
  2365. mmc_power_off(host);
  2366. mmc_release_host(host);
  2367. goto out;
  2368. }
  2369. for (i = 0; i < ARRAY_SIZE(freqs); i++) {
  2370. if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
  2371. break;
  2372. if (freqs[i] <= host->f_min)
  2373. break;
  2374. }
  2375. mmc_release_host(host);
  2376. out:
  2377. if (host->caps & MMC_CAP_NEEDS_POLL)
  2378. mmc_schedule_delayed_work(&host->detect, HZ);
  2379. }
  2380. void mmc_start_host(struct mmc_host *host)
  2381. {
  2382. host->f_init = max(freqs[0], host->f_min);
  2383. host->rescan_disable = 0;
  2384. host->ios.power_mode = MMC_POWER_UNDEFINED;
  2385. if (!(host->caps2 & MMC_CAP2_NO_PRESCAN_POWERUP)) {
  2386. mmc_claim_host(host);
  2387. mmc_power_up(host, host->ocr_avail);
  2388. mmc_release_host(host);
  2389. }
  2390. mmc_gpiod_request_cd_irq(host);
  2391. _mmc_detect_change(host, 0, false);
  2392. }
  2393. void mmc_stop_host(struct mmc_host *host)
  2394. {
  2395. #ifdef CONFIG_MMC_DEBUG
  2396. unsigned long flags;
  2397. spin_lock_irqsave(&host->lock, flags);
  2398. host->removed = 1;
  2399. spin_unlock_irqrestore(&host->lock, flags);
  2400. #endif
  2401. if (host->slot.cd_irq >= 0)
  2402. disable_irq(host->slot.cd_irq);
  2403. host->rescan_disable = 1;
  2404. cancel_delayed_work_sync(&host->detect);
  2405. /* clear pm flags now and let card drivers set them as needed */
  2406. host->pm_flags = 0;
  2407. mmc_bus_get(host);
  2408. if (host->bus_ops && !host->bus_dead) {
  2409. /* Calling bus_ops->remove() with a claimed host can deadlock */
  2410. host->bus_ops->remove(host);
  2411. mmc_claim_host(host);
  2412. mmc_detach_bus(host);
  2413. mmc_power_off(host);
  2414. mmc_release_host(host);
  2415. mmc_bus_put(host);
  2416. return;
  2417. }
  2418. mmc_bus_put(host);
  2419. mmc_claim_host(host);
  2420. mmc_power_off(host);
  2421. mmc_release_host(host);
  2422. }
  2423. int mmc_power_save_host(struct mmc_host *host)
  2424. {
  2425. int ret = 0;
  2426. #ifdef CONFIG_MMC_DEBUG
  2427. pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
  2428. #endif
  2429. mmc_bus_get(host);
  2430. if (!host->bus_ops || host->bus_dead) {
  2431. mmc_bus_put(host);
  2432. return -EINVAL;
  2433. }
  2434. if (host->bus_ops->power_save)
  2435. ret = host->bus_ops->power_save(host);
  2436. mmc_bus_put(host);
  2437. mmc_power_off(host);
  2438. return ret;
  2439. }
  2440. EXPORT_SYMBOL(mmc_power_save_host);
  2441. int mmc_power_restore_host(struct mmc_host *host)
  2442. {
  2443. int ret;
  2444. #ifdef CONFIG_MMC_DEBUG
  2445. pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
  2446. #endif
  2447. mmc_bus_get(host);
  2448. if (!host->bus_ops || host->bus_dead) {
  2449. mmc_bus_put(host);
  2450. return -EINVAL;
  2451. }
  2452. mmc_power_up(host, host->card->ocr);
  2453. ret = host->bus_ops->power_restore(host);
  2454. mmc_bus_put(host);
  2455. return ret;
  2456. }
  2457. EXPORT_SYMBOL(mmc_power_restore_host);
  2458. /*
  2459. * Flush the cache to the non-volatile storage.
  2460. */
  2461. int mmc_flush_cache(struct mmc_card *card)
  2462. {
  2463. int err = 0;
  2464. if (mmc_card_mmc(card) &&
  2465. (card->ext_csd.cache_size > 0) &&
  2466. (card->ext_csd.cache_ctrl & 1)) {
  2467. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  2468. EXT_CSD_FLUSH_CACHE, 1, 0);
  2469. if (err)
  2470. pr_err("%s: cache flush error %d\n",
  2471. mmc_hostname(card->host), err);
  2472. }
  2473. return err;
  2474. }
  2475. EXPORT_SYMBOL(mmc_flush_cache);
  2476. #ifdef CONFIG_PM_SLEEP
  2477. /* Do the card removal on suspend if card is assumed removeable
  2478. * Do that in pm notifier while userspace isn't yet frozen, so we will be able
  2479. to sync the card.
  2480. */
  2481. static int mmc_pm_notify(struct notifier_block *notify_block,
  2482. unsigned long mode, void *unused)
  2483. {
  2484. struct mmc_host *host = container_of(
  2485. notify_block, struct mmc_host, pm_notify);
  2486. unsigned long flags;
  2487. int err = 0;
  2488. switch (mode) {
  2489. case PM_HIBERNATION_PREPARE:
  2490. case PM_SUSPEND_PREPARE:
  2491. case PM_RESTORE_PREPARE:
  2492. spin_lock_irqsave(&host->lock, flags);
  2493. host->rescan_disable = 1;
  2494. spin_unlock_irqrestore(&host->lock, flags);
  2495. cancel_delayed_work_sync(&host->detect);
  2496. if (!host->bus_ops)
  2497. break;
  2498. /* Validate prerequisites for suspend */
  2499. if (host->bus_ops->pre_suspend)
  2500. err = host->bus_ops->pre_suspend(host);
  2501. if (!err)
  2502. break;
  2503. /* Calling bus_ops->remove() with a claimed host can deadlock */
  2504. host->bus_ops->remove(host);
  2505. mmc_claim_host(host);
  2506. mmc_detach_bus(host);
  2507. mmc_power_off(host);
  2508. mmc_release_host(host);
  2509. host->pm_flags = 0;
  2510. break;
  2511. case PM_POST_SUSPEND:
  2512. case PM_POST_HIBERNATION:
  2513. case PM_POST_RESTORE:
  2514. spin_lock_irqsave(&host->lock, flags);
  2515. host->rescan_disable = 0;
  2516. spin_unlock_irqrestore(&host->lock, flags);
  2517. _mmc_detect_change(host, 0, false);
  2518. }
  2519. return 0;
  2520. }
  2521. void mmc_register_pm_notifier(struct mmc_host *host)
  2522. {
  2523. host->pm_notify.notifier_call = mmc_pm_notify;
  2524. register_pm_notifier(&host->pm_notify);
  2525. }
  2526. void mmc_unregister_pm_notifier(struct mmc_host *host)
  2527. {
  2528. unregister_pm_notifier(&host->pm_notify);
  2529. }
  2530. #endif
  2531. /**
  2532. * mmc_init_context_info() - init synchronization context
  2533. * @host: mmc host
  2534. *
  2535. * Init struct context_info needed to implement asynchronous
  2536. * request mechanism, used by mmc core, host driver and mmc requests
  2537. * supplier.
  2538. */
  2539. void mmc_init_context_info(struct mmc_host *host)
  2540. {
  2541. host->context_info.is_new_req = false;
  2542. host->context_info.is_done_rcv = false;
  2543. host->context_info.is_waiting_last_req = false;
  2544. init_waitqueue_head(&host->context_info.wait);
  2545. }
  2546. static int __init mmc_init(void)
  2547. {
  2548. int ret;
  2549. ret = mmc_register_bus();
  2550. if (ret)
  2551. return ret;
  2552. ret = mmc_register_host_class();
  2553. if (ret)
  2554. goto unregister_bus;
  2555. ret = sdio_register_bus();
  2556. if (ret)
  2557. goto unregister_host_class;
  2558. return 0;
  2559. unregister_host_class:
  2560. mmc_unregister_host_class();
  2561. unregister_bus:
  2562. mmc_unregister_bus();
  2563. return ret;
  2564. }
  2565. static void __exit mmc_exit(void)
  2566. {
  2567. sdio_unregister_bus();
  2568. mmc_unregister_host_class();
  2569. mmc_unregister_bus();
  2570. }
  2571. subsys_initcall(mmc_init);
  2572. module_exit(mmc_exit);
  2573. MODULE_LICENSE("GPL");