core.c 66 KB

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