core.c 66 KB

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