core.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710
  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_warning("%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 < 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. count = regulator_count_voltages(supply);
  1072. if (count < 0)
  1073. return count;
  1074. for (i = 0; i < count; i++) {
  1075. int vdd_uV;
  1076. int vdd_mV;
  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. return result;
  1084. }
  1085. EXPORT_SYMBOL_GPL(mmc_regulator_get_ocrmask);
  1086. /**
  1087. * mmc_regulator_set_ocr - set regulator to match host->ios voltage
  1088. * @mmc: the host to regulate
  1089. * @supply: regulator to use
  1090. * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
  1091. *
  1092. * Returns zero on success, else negative errno.
  1093. *
  1094. * MMC host drivers may use this to enable or disable a regulator using
  1095. * a particular supply voltage. This would normally be called from the
  1096. * set_ios() method.
  1097. */
  1098. int mmc_regulator_set_ocr(struct mmc_host *mmc,
  1099. struct regulator *supply,
  1100. unsigned short vdd_bit)
  1101. {
  1102. int result = 0;
  1103. int min_uV, max_uV;
  1104. if (vdd_bit) {
  1105. int tmp;
  1106. int voltage;
  1107. /*
  1108. * REVISIT mmc_vddrange_to_ocrmask() may have set some
  1109. * bits this regulator doesn't quite support ... don't
  1110. * be too picky, most cards and regulators are OK with
  1111. * a 0.1V range goof (it's a small error percentage).
  1112. */
  1113. tmp = vdd_bit - ilog2(MMC_VDD_165_195);
  1114. if (tmp == 0) {
  1115. min_uV = 1650 * 1000;
  1116. max_uV = 1950 * 1000;
  1117. } else {
  1118. min_uV = 1900 * 1000 + tmp * 100 * 1000;
  1119. max_uV = min_uV + 100 * 1000;
  1120. }
  1121. /*
  1122. * If we're using a fixed/static regulator, don't call
  1123. * regulator_set_voltage; it would fail.
  1124. */
  1125. voltage = regulator_get_voltage(supply);
  1126. if (!regulator_can_change_voltage(supply))
  1127. min_uV = max_uV = voltage;
  1128. if (voltage < 0)
  1129. result = voltage;
  1130. else if (voltage < min_uV || voltage > max_uV)
  1131. result = regulator_set_voltage(supply, min_uV, max_uV);
  1132. else
  1133. result = 0;
  1134. if (result == 0 && !mmc->regulator_enabled) {
  1135. result = regulator_enable(supply);
  1136. if (!result)
  1137. mmc->regulator_enabled = true;
  1138. }
  1139. } else if (mmc->regulator_enabled) {
  1140. result = regulator_disable(supply);
  1141. if (result == 0)
  1142. mmc->regulator_enabled = false;
  1143. }
  1144. if (result)
  1145. dev_err(mmc_dev(mmc),
  1146. "could not set regulator OCR (%d)\n", result);
  1147. return result;
  1148. }
  1149. EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr);
  1150. int mmc_regulator_get_supply(struct mmc_host *mmc)
  1151. {
  1152. struct device *dev = mmc_dev(mmc);
  1153. struct regulator *supply;
  1154. int ret;
  1155. supply = devm_regulator_get(dev, "vmmc");
  1156. mmc->supply.vmmc = supply;
  1157. mmc->supply.vqmmc = devm_regulator_get_optional(dev, "vqmmc");
  1158. if (IS_ERR(supply))
  1159. return PTR_ERR(supply);
  1160. ret = mmc_regulator_get_ocrmask(supply);
  1161. if (ret > 0)
  1162. mmc->ocr_avail = ret;
  1163. else
  1164. dev_warn(mmc_dev(mmc), "Failed getting OCR mask: %d\n", ret);
  1165. return 0;
  1166. }
  1167. EXPORT_SYMBOL_GPL(mmc_regulator_get_supply);
  1168. #endif /* CONFIG_REGULATOR */
  1169. /*
  1170. * Mask off any voltages we don't support and select
  1171. * the lowest voltage
  1172. */
  1173. u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
  1174. {
  1175. int bit;
  1176. /*
  1177. * Sanity check the voltages that the card claims to
  1178. * support.
  1179. */
  1180. if (ocr & 0x7F) {
  1181. dev_warn(mmc_dev(host),
  1182. "card claims to support voltages below defined range\n");
  1183. ocr &= ~0x7F;
  1184. }
  1185. ocr &= host->ocr_avail;
  1186. if (!ocr) {
  1187. dev_warn(mmc_dev(host), "no support for card's volts\n");
  1188. return 0;
  1189. }
  1190. if (host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) {
  1191. bit = ffs(ocr) - 1;
  1192. ocr &= 3 << bit;
  1193. mmc_power_cycle(host, ocr);
  1194. } else {
  1195. bit = fls(ocr) - 1;
  1196. ocr &= 3 << bit;
  1197. if (bit != host->ios.vdd)
  1198. dev_warn(mmc_dev(host), "exceeding card's volts\n");
  1199. }
  1200. return ocr;
  1201. }
  1202. int __mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage)
  1203. {
  1204. int err = 0;
  1205. int old_signal_voltage = host->ios.signal_voltage;
  1206. host->ios.signal_voltage = signal_voltage;
  1207. if (host->ops->start_signal_voltage_switch) {
  1208. mmc_host_clk_hold(host);
  1209. err = host->ops->start_signal_voltage_switch(host, &host->ios);
  1210. mmc_host_clk_release(host);
  1211. }
  1212. if (err)
  1213. host->ios.signal_voltage = old_signal_voltage;
  1214. return err;
  1215. }
  1216. int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, u32 ocr)
  1217. {
  1218. struct mmc_command cmd = {0};
  1219. int err = 0;
  1220. u32 clock;
  1221. BUG_ON(!host);
  1222. /*
  1223. * Send CMD11 only if the request is to switch the card to
  1224. * 1.8V signalling.
  1225. */
  1226. if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
  1227. return __mmc_set_signal_voltage(host, signal_voltage);
  1228. /*
  1229. * If we cannot switch voltages, return failure so the caller
  1230. * can continue without UHS mode
  1231. */
  1232. if (!host->ops->start_signal_voltage_switch)
  1233. return -EPERM;
  1234. if (!host->ops->card_busy)
  1235. pr_warning("%s: cannot verify signal voltage switch\n",
  1236. mmc_hostname(host));
  1237. cmd.opcode = SD_SWITCH_VOLTAGE;
  1238. cmd.arg = 0;
  1239. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1240. err = mmc_wait_for_cmd(host, &cmd, 0);
  1241. if (err)
  1242. return err;
  1243. if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
  1244. return -EIO;
  1245. mmc_host_clk_hold(host);
  1246. /*
  1247. * The card should drive cmd and dat[0:3] low immediately
  1248. * after the response of cmd11, but wait 1 ms to be sure
  1249. */
  1250. mmc_delay(1);
  1251. if (host->ops->card_busy && !host->ops->card_busy(host)) {
  1252. err = -EAGAIN;
  1253. goto power_cycle;
  1254. }
  1255. /*
  1256. * During a signal voltage level switch, the clock must be gated
  1257. * for 5 ms according to the SD spec
  1258. */
  1259. clock = host->ios.clock;
  1260. host->ios.clock = 0;
  1261. mmc_set_ios(host);
  1262. if (__mmc_set_signal_voltage(host, signal_voltage)) {
  1263. /*
  1264. * Voltages may not have been switched, but we've already
  1265. * sent CMD11, so a power cycle is required anyway
  1266. */
  1267. err = -EAGAIN;
  1268. goto power_cycle;
  1269. }
  1270. /* Keep clock gated for at least 5 ms */
  1271. mmc_delay(5);
  1272. host->ios.clock = clock;
  1273. mmc_set_ios(host);
  1274. /* Wait for at least 1 ms according to spec */
  1275. mmc_delay(1);
  1276. /*
  1277. * Failure to switch is indicated by the card holding
  1278. * dat[0:3] low
  1279. */
  1280. if (host->ops->card_busy && host->ops->card_busy(host))
  1281. err = -EAGAIN;
  1282. power_cycle:
  1283. if (err) {
  1284. pr_debug("%s: Signal voltage switch failed, "
  1285. "power cycling card\n", mmc_hostname(host));
  1286. mmc_power_cycle(host, ocr);
  1287. }
  1288. mmc_host_clk_release(host);
  1289. return err;
  1290. }
  1291. /*
  1292. * Select timing parameters for host.
  1293. */
  1294. void mmc_set_timing(struct mmc_host *host, unsigned int timing)
  1295. {
  1296. mmc_host_clk_hold(host);
  1297. host->ios.timing = timing;
  1298. mmc_set_ios(host);
  1299. mmc_host_clk_release(host);
  1300. }
  1301. /*
  1302. * Select appropriate driver type for host.
  1303. */
  1304. void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
  1305. {
  1306. mmc_host_clk_hold(host);
  1307. host->ios.drv_type = drv_type;
  1308. mmc_set_ios(host);
  1309. mmc_host_clk_release(host);
  1310. }
  1311. /*
  1312. * Apply power to the MMC stack. This is a two-stage process.
  1313. * First, we enable power to the card without the clock running.
  1314. * We then wait a bit for the power to stabilise. Finally,
  1315. * enable the bus drivers and clock to the card.
  1316. *
  1317. * We must _NOT_ enable the clock prior to power stablising.
  1318. *
  1319. * If a host does all the power sequencing itself, ignore the
  1320. * initial MMC_POWER_UP stage.
  1321. */
  1322. void mmc_power_up(struct mmc_host *host, u32 ocr)
  1323. {
  1324. if (host->ios.power_mode == MMC_POWER_ON)
  1325. return;
  1326. mmc_host_clk_hold(host);
  1327. host->ios.vdd = fls(ocr) - 1;
  1328. if (mmc_host_is_spi(host))
  1329. host->ios.chip_select = MMC_CS_HIGH;
  1330. else
  1331. host->ios.chip_select = MMC_CS_DONTCARE;
  1332. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  1333. host->ios.power_mode = MMC_POWER_UP;
  1334. host->ios.bus_width = MMC_BUS_WIDTH_1;
  1335. host->ios.timing = MMC_TIMING_LEGACY;
  1336. mmc_set_ios(host);
  1337. /* Set signal voltage to 3.3V */
  1338. __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
  1339. /*
  1340. * This delay should be sufficient to allow the power supply
  1341. * to reach the minimum voltage.
  1342. */
  1343. mmc_delay(10);
  1344. host->ios.clock = host->f_init;
  1345. host->ios.power_mode = MMC_POWER_ON;
  1346. mmc_set_ios(host);
  1347. /*
  1348. * This delay must be at least 74 clock sizes, or 1 ms, or the
  1349. * time required to reach a stable voltage.
  1350. */
  1351. mmc_delay(10);
  1352. mmc_host_clk_release(host);
  1353. }
  1354. void mmc_power_off(struct mmc_host *host)
  1355. {
  1356. if (host->ios.power_mode == MMC_POWER_OFF)
  1357. return;
  1358. mmc_host_clk_hold(host);
  1359. host->ios.clock = 0;
  1360. host->ios.vdd = 0;
  1361. if (!mmc_host_is_spi(host)) {
  1362. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  1363. host->ios.chip_select = MMC_CS_DONTCARE;
  1364. }
  1365. host->ios.power_mode = MMC_POWER_OFF;
  1366. host->ios.bus_width = MMC_BUS_WIDTH_1;
  1367. host->ios.timing = MMC_TIMING_LEGACY;
  1368. mmc_set_ios(host);
  1369. /*
  1370. * Some configurations, such as the 802.11 SDIO card in the OLPC
  1371. * XO-1.5, require a short delay after poweroff before the card
  1372. * can be successfully turned on again.
  1373. */
  1374. mmc_delay(1);
  1375. mmc_host_clk_release(host);
  1376. }
  1377. void mmc_power_cycle(struct mmc_host *host, u32 ocr)
  1378. {
  1379. mmc_power_off(host);
  1380. /* Wait at least 1 ms according to SD spec */
  1381. mmc_delay(1);
  1382. mmc_power_up(host, ocr);
  1383. }
  1384. /*
  1385. * Cleanup when the last reference to the bus operator is dropped.
  1386. */
  1387. static void __mmc_release_bus(struct mmc_host *host)
  1388. {
  1389. BUG_ON(!host);
  1390. BUG_ON(host->bus_refs);
  1391. BUG_ON(!host->bus_dead);
  1392. host->bus_ops = NULL;
  1393. }
  1394. /*
  1395. * Increase reference count of bus operator
  1396. */
  1397. static inline void mmc_bus_get(struct mmc_host *host)
  1398. {
  1399. unsigned long flags;
  1400. spin_lock_irqsave(&host->lock, flags);
  1401. host->bus_refs++;
  1402. spin_unlock_irqrestore(&host->lock, flags);
  1403. }
  1404. /*
  1405. * Decrease reference count of bus operator and free it if
  1406. * it is the last reference.
  1407. */
  1408. static inline void mmc_bus_put(struct mmc_host *host)
  1409. {
  1410. unsigned long flags;
  1411. spin_lock_irqsave(&host->lock, flags);
  1412. host->bus_refs--;
  1413. if ((host->bus_refs == 0) && host->bus_ops)
  1414. __mmc_release_bus(host);
  1415. spin_unlock_irqrestore(&host->lock, flags);
  1416. }
  1417. /*
  1418. * Assign a mmc bus handler to a host. Only one bus handler may control a
  1419. * host at any given time.
  1420. */
  1421. void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
  1422. {
  1423. unsigned long flags;
  1424. BUG_ON(!host);
  1425. BUG_ON(!ops);
  1426. WARN_ON(!host->claimed);
  1427. spin_lock_irqsave(&host->lock, flags);
  1428. BUG_ON(host->bus_ops);
  1429. BUG_ON(host->bus_refs);
  1430. host->bus_ops = ops;
  1431. host->bus_refs = 1;
  1432. host->bus_dead = 0;
  1433. spin_unlock_irqrestore(&host->lock, flags);
  1434. }
  1435. /*
  1436. * Remove the current bus handler from a host.
  1437. */
  1438. void mmc_detach_bus(struct mmc_host *host)
  1439. {
  1440. unsigned long flags;
  1441. BUG_ON(!host);
  1442. WARN_ON(!host->claimed);
  1443. WARN_ON(!host->bus_ops);
  1444. spin_lock_irqsave(&host->lock, flags);
  1445. host->bus_dead = 1;
  1446. spin_unlock_irqrestore(&host->lock, flags);
  1447. mmc_bus_put(host);
  1448. }
  1449. static void _mmc_detect_change(struct mmc_host *host, unsigned long delay,
  1450. bool cd_irq)
  1451. {
  1452. #ifdef CONFIG_MMC_DEBUG
  1453. unsigned long flags;
  1454. spin_lock_irqsave(&host->lock, flags);
  1455. WARN_ON(host->removed);
  1456. spin_unlock_irqrestore(&host->lock, flags);
  1457. #endif
  1458. /*
  1459. * If the device is configured as wakeup, we prevent a new sleep for
  1460. * 5 s to give provision for user space to consume the event.
  1461. */
  1462. if (cd_irq && !(host->caps & MMC_CAP_NEEDS_POLL) &&
  1463. device_can_wakeup(mmc_dev(host)))
  1464. pm_wakeup_event(mmc_dev(host), 5000);
  1465. host->detect_change = 1;
  1466. mmc_schedule_delayed_work(&host->detect, delay);
  1467. }
  1468. /**
  1469. * mmc_detect_change - process change of state on a MMC socket
  1470. * @host: host which changed state.
  1471. * @delay: optional delay to wait before detection (jiffies)
  1472. *
  1473. * MMC drivers should call this when they detect a card has been
  1474. * inserted or removed. The MMC layer will confirm that any
  1475. * present card is still functional, and initialize any newly
  1476. * inserted.
  1477. */
  1478. void mmc_detect_change(struct mmc_host *host, unsigned long delay)
  1479. {
  1480. _mmc_detect_change(host, delay, true);
  1481. }
  1482. EXPORT_SYMBOL(mmc_detect_change);
  1483. void mmc_init_erase(struct mmc_card *card)
  1484. {
  1485. unsigned int sz;
  1486. if (is_power_of_2(card->erase_size))
  1487. card->erase_shift = ffs(card->erase_size) - 1;
  1488. else
  1489. card->erase_shift = 0;
  1490. /*
  1491. * It is possible to erase an arbitrarily large area of an SD or MMC
  1492. * card. That is not desirable because it can take a long time
  1493. * (minutes) potentially delaying more important I/O, and also the
  1494. * timeout calculations become increasingly hugely over-estimated.
  1495. * Consequently, 'pref_erase' is defined as a guide to limit erases
  1496. * to that size and alignment.
  1497. *
  1498. * For SD cards that define Allocation Unit size, limit erases to one
  1499. * Allocation Unit at a time. For MMC cards that define High Capacity
  1500. * Erase Size, whether it is switched on or not, limit to that size.
  1501. * Otherwise just have a stab at a good value. For modern cards it
  1502. * will end up being 4MiB. Note that if the value is too small, it
  1503. * can end up taking longer to erase.
  1504. */
  1505. if (mmc_card_sd(card) && card->ssr.au) {
  1506. card->pref_erase = card->ssr.au;
  1507. card->erase_shift = ffs(card->ssr.au) - 1;
  1508. } else if (card->ext_csd.hc_erase_size) {
  1509. card->pref_erase = card->ext_csd.hc_erase_size;
  1510. } else {
  1511. sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
  1512. if (sz < 128)
  1513. card->pref_erase = 512 * 1024 / 512;
  1514. else if (sz < 512)
  1515. card->pref_erase = 1024 * 1024 / 512;
  1516. else if (sz < 1024)
  1517. card->pref_erase = 2 * 1024 * 1024 / 512;
  1518. else
  1519. card->pref_erase = 4 * 1024 * 1024 / 512;
  1520. if (card->pref_erase < card->erase_size)
  1521. card->pref_erase = card->erase_size;
  1522. else {
  1523. sz = card->pref_erase % card->erase_size;
  1524. if (sz)
  1525. card->pref_erase += card->erase_size - sz;
  1526. }
  1527. }
  1528. }
  1529. static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
  1530. unsigned int arg, unsigned int qty)
  1531. {
  1532. unsigned int erase_timeout;
  1533. if (arg == MMC_DISCARD_ARG ||
  1534. (arg == MMC_TRIM_ARG && card->ext_csd.rev >= 6)) {
  1535. erase_timeout = card->ext_csd.trim_timeout;
  1536. } else if (card->ext_csd.erase_group_def & 1) {
  1537. /* High Capacity Erase Group Size uses HC timeouts */
  1538. if (arg == MMC_TRIM_ARG)
  1539. erase_timeout = card->ext_csd.trim_timeout;
  1540. else
  1541. erase_timeout = card->ext_csd.hc_erase_timeout;
  1542. } else {
  1543. /* CSD Erase Group Size uses write timeout */
  1544. unsigned int mult = (10 << card->csd.r2w_factor);
  1545. unsigned int timeout_clks = card->csd.tacc_clks * mult;
  1546. unsigned int timeout_us;
  1547. /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
  1548. if (card->csd.tacc_ns < 1000000)
  1549. timeout_us = (card->csd.tacc_ns * mult) / 1000;
  1550. else
  1551. timeout_us = (card->csd.tacc_ns / 1000) * mult;
  1552. /*
  1553. * ios.clock is only a target. The real clock rate might be
  1554. * less but not that much less, so fudge it by multiplying by 2.
  1555. */
  1556. timeout_clks <<= 1;
  1557. timeout_us += (timeout_clks * 1000) /
  1558. (mmc_host_clk_rate(card->host) / 1000);
  1559. erase_timeout = timeout_us / 1000;
  1560. /*
  1561. * Theoretically, the calculation could underflow so round up
  1562. * to 1ms in that case.
  1563. */
  1564. if (!erase_timeout)
  1565. erase_timeout = 1;
  1566. }
  1567. /* Multiplier for secure operations */
  1568. if (arg & MMC_SECURE_ARGS) {
  1569. if (arg == MMC_SECURE_ERASE_ARG)
  1570. erase_timeout *= card->ext_csd.sec_erase_mult;
  1571. else
  1572. erase_timeout *= card->ext_csd.sec_trim_mult;
  1573. }
  1574. erase_timeout *= qty;
  1575. /*
  1576. * Ensure at least a 1 second timeout for SPI as per
  1577. * 'mmc_set_data_timeout()'
  1578. */
  1579. if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
  1580. erase_timeout = 1000;
  1581. return erase_timeout;
  1582. }
  1583. static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
  1584. unsigned int arg,
  1585. unsigned int qty)
  1586. {
  1587. unsigned int erase_timeout;
  1588. if (card->ssr.erase_timeout) {
  1589. /* Erase timeout specified in SD Status Register (SSR) */
  1590. erase_timeout = card->ssr.erase_timeout * qty +
  1591. card->ssr.erase_offset;
  1592. } else {
  1593. /*
  1594. * Erase timeout not specified in SD Status Register (SSR) so
  1595. * use 250ms per write block.
  1596. */
  1597. erase_timeout = 250 * qty;
  1598. }
  1599. /* Must not be less than 1 second */
  1600. if (erase_timeout < 1000)
  1601. erase_timeout = 1000;
  1602. return erase_timeout;
  1603. }
  1604. static unsigned int mmc_erase_timeout(struct mmc_card *card,
  1605. unsigned int arg,
  1606. unsigned int qty)
  1607. {
  1608. if (mmc_card_sd(card))
  1609. return mmc_sd_erase_timeout(card, arg, qty);
  1610. else
  1611. return mmc_mmc_erase_timeout(card, arg, qty);
  1612. }
  1613. static int mmc_do_erase(struct mmc_card *card, unsigned int from,
  1614. unsigned int to, unsigned int arg)
  1615. {
  1616. struct mmc_command cmd = {0};
  1617. unsigned int qty = 0;
  1618. unsigned long timeout;
  1619. int err;
  1620. /*
  1621. * qty is used to calculate the erase timeout which depends on how many
  1622. * erase groups (or allocation units in SD terminology) are affected.
  1623. * We count erasing part of an erase group as one erase group.
  1624. * For SD, the allocation units are always a power of 2. For MMC, the
  1625. * erase group size is almost certainly also power of 2, but it does not
  1626. * seem to insist on that in the JEDEC standard, so we fall back to
  1627. * division in that case. SD may not specify an allocation unit size,
  1628. * in which case the timeout is based on the number of write blocks.
  1629. *
  1630. * Note that the timeout for secure trim 2 will only be correct if the
  1631. * number of erase groups specified is the same as the total of all
  1632. * preceding secure trim 1 commands. Since the power may have been
  1633. * lost since the secure trim 1 commands occurred, it is generally
  1634. * impossible to calculate the secure trim 2 timeout correctly.
  1635. */
  1636. if (card->erase_shift)
  1637. qty += ((to >> card->erase_shift) -
  1638. (from >> card->erase_shift)) + 1;
  1639. else if (mmc_card_sd(card))
  1640. qty += to - from + 1;
  1641. else
  1642. qty += ((to / card->erase_size) -
  1643. (from / card->erase_size)) + 1;
  1644. if (!mmc_card_blockaddr(card)) {
  1645. from <<= 9;
  1646. to <<= 9;
  1647. }
  1648. if (mmc_card_sd(card))
  1649. cmd.opcode = SD_ERASE_WR_BLK_START;
  1650. else
  1651. cmd.opcode = MMC_ERASE_GROUP_START;
  1652. cmd.arg = from;
  1653. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1654. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1655. if (err) {
  1656. pr_err("mmc_erase: group start error %d, "
  1657. "status %#x\n", err, cmd.resp[0]);
  1658. err = -EIO;
  1659. goto out;
  1660. }
  1661. memset(&cmd, 0, sizeof(struct mmc_command));
  1662. if (mmc_card_sd(card))
  1663. cmd.opcode = SD_ERASE_WR_BLK_END;
  1664. else
  1665. cmd.opcode = MMC_ERASE_GROUP_END;
  1666. cmd.arg = to;
  1667. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1668. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1669. if (err) {
  1670. pr_err("mmc_erase: group end error %d, status %#x\n",
  1671. err, cmd.resp[0]);
  1672. err = -EIO;
  1673. goto out;
  1674. }
  1675. memset(&cmd, 0, sizeof(struct mmc_command));
  1676. cmd.opcode = MMC_ERASE;
  1677. cmd.arg = arg;
  1678. cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  1679. cmd.busy_timeout = mmc_erase_timeout(card, arg, qty);
  1680. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1681. if (err) {
  1682. pr_err("mmc_erase: erase error %d, status %#x\n",
  1683. err, cmd.resp[0]);
  1684. err = -EIO;
  1685. goto out;
  1686. }
  1687. if (mmc_host_is_spi(card->host))
  1688. goto out;
  1689. timeout = jiffies + msecs_to_jiffies(MMC_CORE_TIMEOUT_MS);
  1690. do {
  1691. memset(&cmd, 0, sizeof(struct mmc_command));
  1692. cmd.opcode = MMC_SEND_STATUS;
  1693. cmd.arg = card->rca << 16;
  1694. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1695. /* Do not retry else we can't see errors */
  1696. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1697. if (err || (cmd.resp[0] & 0xFDF92000)) {
  1698. pr_err("error %d requesting status %#x\n",
  1699. err, cmd.resp[0]);
  1700. err = -EIO;
  1701. goto out;
  1702. }
  1703. /* Timeout if the device never becomes ready for data and
  1704. * never leaves the program state.
  1705. */
  1706. if (time_after(jiffies, timeout)) {
  1707. pr_err("%s: Card stuck in programming state! %s\n",
  1708. mmc_hostname(card->host), __func__);
  1709. err = -EIO;
  1710. goto out;
  1711. }
  1712. } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
  1713. (R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG));
  1714. out:
  1715. return err;
  1716. }
  1717. /**
  1718. * mmc_erase - erase sectors.
  1719. * @card: card to erase
  1720. * @from: first sector to erase
  1721. * @nr: number of sectors to erase
  1722. * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
  1723. *
  1724. * Caller must claim host before calling this function.
  1725. */
  1726. int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
  1727. unsigned int arg)
  1728. {
  1729. unsigned int rem, to = from + nr;
  1730. if (!(card->host->caps & MMC_CAP_ERASE) ||
  1731. !(card->csd.cmdclass & CCC_ERASE))
  1732. return -EOPNOTSUPP;
  1733. if (!card->erase_size)
  1734. return -EOPNOTSUPP;
  1735. if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
  1736. return -EOPNOTSUPP;
  1737. if ((arg & MMC_SECURE_ARGS) &&
  1738. !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
  1739. return -EOPNOTSUPP;
  1740. if ((arg & MMC_TRIM_ARGS) &&
  1741. !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
  1742. return -EOPNOTSUPP;
  1743. if (arg == MMC_SECURE_ERASE_ARG) {
  1744. if (from % card->erase_size || nr % card->erase_size)
  1745. return -EINVAL;
  1746. }
  1747. if (arg == MMC_ERASE_ARG) {
  1748. rem = from % card->erase_size;
  1749. if (rem) {
  1750. rem = card->erase_size - rem;
  1751. from += rem;
  1752. if (nr > rem)
  1753. nr -= rem;
  1754. else
  1755. return 0;
  1756. }
  1757. rem = nr % card->erase_size;
  1758. if (rem)
  1759. nr -= rem;
  1760. }
  1761. if (nr == 0)
  1762. return 0;
  1763. to = from + nr;
  1764. if (to <= from)
  1765. return -EINVAL;
  1766. /* 'from' and 'to' are inclusive */
  1767. to -= 1;
  1768. return mmc_do_erase(card, from, to, arg);
  1769. }
  1770. EXPORT_SYMBOL(mmc_erase);
  1771. int mmc_can_erase(struct mmc_card *card)
  1772. {
  1773. if ((card->host->caps & MMC_CAP_ERASE) &&
  1774. (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
  1775. return 1;
  1776. return 0;
  1777. }
  1778. EXPORT_SYMBOL(mmc_can_erase);
  1779. int mmc_can_trim(struct mmc_card *card)
  1780. {
  1781. if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
  1782. return 1;
  1783. return 0;
  1784. }
  1785. EXPORT_SYMBOL(mmc_can_trim);
  1786. int mmc_can_discard(struct mmc_card *card)
  1787. {
  1788. /*
  1789. * As there's no way to detect the discard support bit at v4.5
  1790. * use the s/w feature support filed.
  1791. */
  1792. if (card->ext_csd.feature_support & MMC_DISCARD_FEATURE)
  1793. return 1;
  1794. return 0;
  1795. }
  1796. EXPORT_SYMBOL(mmc_can_discard);
  1797. int mmc_can_sanitize(struct mmc_card *card)
  1798. {
  1799. if (!mmc_can_trim(card) && !mmc_can_erase(card))
  1800. return 0;
  1801. if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE)
  1802. return 1;
  1803. return 0;
  1804. }
  1805. EXPORT_SYMBOL(mmc_can_sanitize);
  1806. int mmc_can_secure_erase_trim(struct mmc_card *card)
  1807. {
  1808. if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
  1809. return 1;
  1810. return 0;
  1811. }
  1812. EXPORT_SYMBOL(mmc_can_secure_erase_trim);
  1813. int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
  1814. unsigned int nr)
  1815. {
  1816. if (!card->erase_size)
  1817. return 0;
  1818. if (from % card->erase_size || nr % card->erase_size)
  1819. return 0;
  1820. return 1;
  1821. }
  1822. EXPORT_SYMBOL(mmc_erase_group_aligned);
  1823. static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
  1824. unsigned int arg)
  1825. {
  1826. struct mmc_host *host = card->host;
  1827. unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
  1828. unsigned int last_timeout = 0;
  1829. if (card->erase_shift)
  1830. max_qty = UINT_MAX >> card->erase_shift;
  1831. else if (mmc_card_sd(card))
  1832. max_qty = UINT_MAX;
  1833. else
  1834. max_qty = UINT_MAX / card->erase_size;
  1835. /* Find the largest qty with an OK timeout */
  1836. do {
  1837. y = 0;
  1838. for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
  1839. timeout = mmc_erase_timeout(card, arg, qty + x);
  1840. if (timeout > host->max_busy_timeout)
  1841. break;
  1842. if (timeout < last_timeout)
  1843. break;
  1844. last_timeout = timeout;
  1845. y = x;
  1846. }
  1847. qty += y;
  1848. } while (y);
  1849. if (!qty)
  1850. return 0;
  1851. if (qty == 1)
  1852. return 1;
  1853. /* Convert qty to sectors */
  1854. if (card->erase_shift)
  1855. max_discard = --qty << card->erase_shift;
  1856. else if (mmc_card_sd(card))
  1857. max_discard = qty;
  1858. else
  1859. max_discard = --qty * card->erase_size;
  1860. return max_discard;
  1861. }
  1862. unsigned int mmc_calc_max_discard(struct mmc_card *card)
  1863. {
  1864. struct mmc_host *host = card->host;
  1865. unsigned int max_discard, max_trim;
  1866. if (!host->max_busy_timeout)
  1867. return UINT_MAX;
  1868. /*
  1869. * Without erase_group_def set, MMC erase timeout depends on clock
  1870. * frequence which can change. In that case, the best choice is
  1871. * just the preferred erase size.
  1872. */
  1873. if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
  1874. return card->pref_erase;
  1875. max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
  1876. if (mmc_can_trim(card)) {
  1877. max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
  1878. if (max_trim < max_discard)
  1879. max_discard = max_trim;
  1880. } else if (max_discard < card->erase_size) {
  1881. max_discard = 0;
  1882. }
  1883. pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
  1884. mmc_hostname(host), max_discard, host->max_busy_timeout);
  1885. return max_discard;
  1886. }
  1887. EXPORT_SYMBOL(mmc_calc_max_discard);
  1888. int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
  1889. {
  1890. struct mmc_command cmd = {0};
  1891. if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
  1892. return 0;
  1893. cmd.opcode = MMC_SET_BLOCKLEN;
  1894. cmd.arg = blocklen;
  1895. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1896. return mmc_wait_for_cmd(card->host, &cmd, 5);
  1897. }
  1898. EXPORT_SYMBOL(mmc_set_blocklen);
  1899. int mmc_set_blockcount(struct mmc_card *card, unsigned int blockcount,
  1900. bool is_rel_write)
  1901. {
  1902. struct mmc_command cmd = {0};
  1903. cmd.opcode = MMC_SET_BLOCK_COUNT;
  1904. cmd.arg = blockcount & 0x0000FFFF;
  1905. if (is_rel_write)
  1906. cmd.arg |= 1 << 31;
  1907. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1908. return mmc_wait_for_cmd(card->host, &cmd, 5);
  1909. }
  1910. EXPORT_SYMBOL(mmc_set_blockcount);
  1911. static void mmc_hw_reset_for_init(struct mmc_host *host)
  1912. {
  1913. if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
  1914. return;
  1915. mmc_host_clk_hold(host);
  1916. host->ops->hw_reset(host);
  1917. mmc_host_clk_release(host);
  1918. }
  1919. int mmc_can_reset(struct mmc_card *card)
  1920. {
  1921. u8 rst_n_function;
  1922. if (!mmc_card_mmc(card))
  1923. return 0;
  1924. rst_n_function = card->ext_csd.rst_n_function;
  1925. if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
  1926. return 0;
  1927. return 1;
  1928. }
  1929. EXPORT_SYMBOL(mmc_can_reset);
  1930. static int mmc_do_hw_reset(struct mmc_host *host, int check)
  1931. {
  1932. struct mmc_card *card = host->card;
  1933. if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
  1934. return -EOPNOTSUPP;
  1935. if (!card)
  1936. return -EINVAL;
  1937. if (!mmc_can_reset(card))
  1938. return -EOPNOTSUPP;
  1939. mmc_host_clk_hold(host);
  1940. mmc_set_clock(host, host->f_init);
  1941. host->ops->hw_reset(host);
  1942. /* If the reset has happened, then a status command will fail */
  1943. if (check) {
  1944. struct mmc_command cmd = {0};
  1945. int err;
  1946. cmd.opcode = MMC_SEND_STATUS;
  1947. if (!mmc_host_is_spi(card->host))
  1948. cmd.arg = card->rca << 16;
  1949. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  1950. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  1951. if (!err) {
  1952. mmc_host_clk_release(host);
  1953. return -ENOSYS;
  1954. }
  1955. }
  1956. host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_DDR);
  1957. if (mmc_host_is_spi(host)) {
  1958. host->ios.chip_select = MMC_CS_HIGH;
  1959. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  1960. } else {
  1961. host->ios.chip_select = MMC_CS_DONTCARE;
  1962. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  1963. }
  1964. host->ios.bus_width = MMC_BUS_WIDTH_1;
  1965. host->ios.timing = MMC_TIMING_LEGACY;
  1966. mmc_set_ios(host);
  1967. mmc_host_clk_release(host);
  1968. return host->bus_ops->power_restore(host);
  1969. }
  1970. int mmc_hw_reset(struct mmc_host *host)
  1971. {
  1972. return mmc_do_hw_reset(host, 0);
  1973. }
  1974. EXPORT_SYMBOL(mmc_hw_reset);
  1975. int mmc_hw_reset_check(struct mmc_host *host)
  1976. {
  1977. return mmc_do_hw_reset(host, 1);
  1978. }
  1979. EXPORT_SYMBOL(mmc_hw_reset_check);
  1980. static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
  1981. {
  1982. host->f_init = freq;
  1983. #ifdef CONFIG_MMC_DEBUG
  1984. pr_info("%s: %s: trying to init card at %u Hz\n",
  1985. mmc_hostname(host), __func__, host->f_init);
  1986. #endif
  1987. mmc_power_up(host, host->ocr_avail);
  1988. /*
  1989. * Some eMMCs (with VCCQ always on) may not be reset after power up, so
  1990. * do a hardware reset if possible.
  1991. */
  1992. mmc_hw_reset_for_init(host);
  1993. /*
  1994. * sdio_reset sends CMD52 to reset card. Since we do not know
  1995. * if the card is being re-initialized, just send it. CMD52
  1996. * should be ignored by SD/eMMC cards.
  1997. */
  1998. sdio_reset(host);
  1999. mmc_go_idle(host);
  2000. mmc_send_if_cond(host, host->ocr_avail);
  2001. /* Order's important: probe SDIO, then SD, then MMC */
  2002. if (!mmc_attach_sdio(host))
  2003. return 0;
  2004. if (!mmc_attach_sd(host))
  2005. return 0;
  2006. if (!mmc_attach_mmc(host))
  2007. return 0;
  2008. mmc_power_off(host);
  2009. return -EIO;
  2010. }
  2011. int _mmc_detect_card_removed(struct mmc_host *host)
  2012. {
  2013. int ret;
  2014. if (host->caps & MMC_CAP_NONREMOVABLE)
  2015. return 0;
  2016. if (!host->card || mmc_card_removed(host->card))
  2017. return 1;
  2018. ret = host->bus_ops->alive(host);
  2019. /*
  2020. * Card detect status and alive check may be out of sync if card is
  2021. * removed slowly, when card detect switch changes while card/slot
  2022. * pads are still contacted in hardware (refer to "SD Card Mechanical
  2023. * Addendum, Appendix C: Card Detection Switch"). So reschedule a
  2024. * detect work 200ms later for this case.
  2025. */
  2026. if (!ret && host->ops->get_cd && !host->ops->get_cd(host)) {
  2027. mmc_detect_change(host, msecs_to_jiffies(200));
  2028. pr_debug("%s: card removed too slowly\n", mmc_hostname(host));
  2029. }
  2030. if (ret) {
  2031. mmc_card_set_removed(host->card);
  2032. pr_debug("%s: card remove detected\n", mmc_hostname(host));
  2033. }
  2034. return ret;
  2035. }
  2036. int mmc_detect_card_removed(struct mmc_host *host)
  2037. {
  2038. struct mmc_card *card = host->card;
  2039. int ret;
  2040. WARN_ON(!host->claimed);
  2041. if (!card)
  2042. return 1;
  2043. ret = mmc_card_removed(card);
  2044. /*
  2045. * The card will be considered unchanged unless we have been asked to
  2046. * detect a change or host requires polling to provide card detection.
  2047. */
  2048. if (!host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL))
  2049. return ret;
  2050. host->detect_change = 0;
  2051. if (!ret) {
  2052. ret = _mmc_detect_card_removed(host);
  2053. if (ret && (host->caps & MMC_CAP_NEEDS_POLL)) {
  2054. /*
  2055. * Schedule a detect work as soon as possible to let a
  2056. * rescan handle the card removal.
  2057. */
  2058. cancel_delayed_work(&host->detect);
  2059. _mmc_detect_change(host, 0, false);
  2060. }
  2061. }
  2062. return ret;
  2063. }
  2064. EXPORT_SYMBOL(mmc_detect_card_removed);
  2065. void mmc_rescan(struct work_struct *work)
  2066. {
  2067. struct mmc_host *host =
  2068. container_of(work, struct mmc_host, detect.work);
  2069. int i;
  2070. if (host->trigger_card_event && host->ops->card_event) {
  2071. host->ops->card_event(host);
  2072. host->trigger_card_event = false;
  2073. }
  2074. if (host->rescan_disable)
  2075. return;
  2076. /* If there is a non-removable card registered, only scan once */
  2077. if ((host->caps & MMC_CAP_NONREMOVABLE) && host->rescan_entered)
  2078. return;
  2079. host->rescan_entered = 1;
  2080. mmc_bus_get(host);
  2081. /*
  2082. * if there is a _removable_ card registered, check whether it is
  2083. * still present
  2084. */
  2085. if (host->bus_ops && !host->bus_dead
  2086. && !(host->caps & MMC_CAP_NONREMOVABLE))
  2087. host->bus_ops->detect(host);
  2088. host->detect_change = 0;
  2089. /*
  2090. * Let mmc_bus_put() free the bus/bus_ops if we've found that
  2091. * the card is no longer present.
  2092. */
  2093. mmc_bus_put(host);
  2094. mmc_bus_get(host);
  2095. /* if there still is a card present, stop here */
  2096. if (host->bus_ops != NULL) {
  2097. mmc_bus_put(host);
  2098. goto out;
  2099. }
  2100. /*
  2101. * Only we can add a new handler, so it's safe to
  2102. * release the lock here.
  2103. */
  2104. mmc_bus_put(host);
  2105. if (!(host->caps & MMC_CAP_NONREMOVABLE) && host->ops->get_cd &&
  2106. host->ops->get_cd(host) == 0) {
  2107. mmc_claim_host(host);
  2108. mmc_power_off(host);
  2109. mmc_release_host(host);
  2110. goto out;
  2111. }
  2112. mmc_claim_host(host);
  2113. for (i = 0; i < ARRAY_SIZE(freqs); i++) {
  2114. if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
  2115. break;
  2116. if (freqs[i] <= host->f_min)
  2117. break;
  2118. }
  2119. mmc_release_host(host);
  2120. out:
  2121. if (host->caps & MMC_CAP_NEEDS_POLL)
  2122. mmc_schedule_delayed_work(&host->detect, HZ);
  2123. }
  2124. void mmc_start_host(struct mmc_host *host)
  2125. {
  2126. host->f_init = max(freqs[0], host->f_min);
  2127. host->rescan_disable = 0;
  2128. if (host->caps2 & MMC_CAP2_NO_PRESCAN_POWERUP)
  2129. mmc_power_off(host);
  2130. else
  2131. mmc_power_up(host, host->ocr_avail);
  2132. mmc_gpiod_request_cd_irq(host);
  2133. _mmc_detect_change(host, 0, false);
  2134. }
  2135. void mmc_stop_host(struct mmc_host *host)
  2136. {
  2137. #ifdef CONFIG_MMC_DEBUG
  2138. unsigned long flags;
  2139. spin_lock_irqsave(&host->lock, flags);
  2140. host->removed = 1;
  2141. spin_unlock_irqrestore(&host->lock, flags);
  2142. #endif
  2143. if (host->slot.cd_irq >= 0)
  2144. disable_irq(host->slot.cd_irq);
  2145. host->rescan_disable = 1;
  2146. cancel_delayed_work_sync(&host->detect);
  2147. mmc_flush_scheduled_work();
  2148. /* clear pm flags now and let card drivers set them as needed */
  2149. host->pm_flags = 0;
  2150. mmc_bus_get(host);
  2151. if (host->bus_ops && !host->bus_dead) {
  2152. /* Calling bus_ops->remove() with a claimed host can deadlock */
  2153. host->bus_ops->remove(host);
  2154. mmc_claim_host(host);
  2155. mmc_detach_bus(host);
  2156. mmc_power_off(host);
  2157. mmc_release_host(host);
  2158. mmc_bus_put(host);
  2159. return;
  2160. }
  2161. mmc_bus_put(host);
  2162. BUG_ON(host->card);
  2163. mmc_power_off(host);
  2164. }
  2165. int mmc_power_save_host(struct mmc_host *host)
  2166. {
  2167. int ret = 0;
  2168. #ifdef CONFIG_MMC_DEBUG
  2169. pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
  2170. #endif
  2171. mmc_bus_get(host);
  2172. if (!host->bus_ops || host->bus_dead) {
  2173. mmc_bus_put(host);
  2174. return -EINVAL;
  2175. }
  2176. if (host->bus_ops->power_save)
  2177. ret = host->bus_ops->power_save(host);
  2178. mmc_bus_put(host);
  2179. mmc_power_off(host);
  2180. return ret;
  2181. }
  2182. EXPORT_SYMBOL(mmc_power_save_host);
  2183. int mmc_power_restore_host(struct mmc_host *host)
  2184. {
  2185. int ret;
  2186. #ifdef CONFIG_MMC_DEBUG
  2187. pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
  2188. #endif
  2189. mmc_bus_get(host);
  2190. if (!host->bus_ops || host->bus_dead) {
  2191. mmc_bus_put(host);
  2192. return -EINVAL;
  2193. }
  2194. mmc_power_up(host, host->card->ocr);
  2195. ret = host->bus_ops->power_restore(host);
  2196. mmc_bus_put(host);
  2197. return ret;
  2198. }
  2199. EXPORT_SYMBOL(mmc_power_restore_host);
  2200. /*
  2201. * Flush the cache to the non-volatile storage.
  2202. */
  2203. int mmc_flush_cache(struct mmc_card *card)
  2204. {
  2205. int err = 0;
  2206. if (mmc_card_mmc(card) &&
  2207. (card->ext_csd.cache_size > 0) &&
  2208. (card->ext_csd.cache_ctrl & 1)) {
  2209. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  2210. EXT_CSD_FLUSH_CACHE, 1, 0);
  2211. if (err)
  2212. pr_err("%s: cache flush error %d\n",
  2213. mmc_hostname(card->host), err);
  2214. }
  2215. return err;
  2216. }
  2217. EXPORT_SYMBOL(mmc_flush_cache);
  2218. #ifdef CONFIG_PM
  2219. /* Do the card removal on suspend if card is assumed removeable
  2220. * Do that in pm notifier while userspace isn't yet frozen, so we will be able
  2221. to sync the card.
  2222. */
  2223. int mmc_pm_notify(struct notifier_block *notify_block,
  2224. unsigned long mode, void *unused)
  2225. {
  2226. struct mmc_host *host = container_of(
  2227. notify_block, struct mmc_host, pm_notify);
  2228. unsigned long flags;
  2229. int err = 0;
  2230. switch (mode) {
  2231. case PM_HIBERNATION_PREPARE:
  2232. case PM_SUSPEND_PREPARE:
  2233. spin_lock_irqsave(&host->lock, flags);
  2234. host->rescan_disable = 1;
  2235. spin_unlock_irqrestore(&host->lock, flags);
  2236. cancel_delayed_work_sync(&host->detect);
  2237. if (!host->bus_ops)
  2238. break;
  2239. /* Validate prerequisites for suspend */
  2240. if (host->bus_ops->pre_suspend)
  2241. err = host->bus_ops->pre_suspend(host);
  2242. if (!err)
  2243. break;
  2244. /* Calling bus_ops->remove() with a claimed host can deadlock */
  2245. host->bus_ops->remove(host);
  2246. mmc_claim_host(host);
  2247. mmc_detach_bus(host);
  2248. mmc_power_off(host);
  2249. mmc_release_host(host);
  2250. host->pm_flags = 0;
  2251. break;
  2252. case PM_POST_SUSPEND:
  2253. case PM_POST_HIBERNATION:
  2254. case PM_POST_RESTORE:
  2255. spin_lock_irqsave(&host->lock, flags);
  2256. host->rescan_disable = 0;
  2257. spin_unlock_irqrestore(&host->lock, flags);
  2258. _mmc_detect_change(host, 0, false);
  2259. }
  2260. return 0;
  2261. }
  2262. #endif
  2263. /**
  2264. * mmc_init_context_info() - init synchronization context
  2265. * @host: mmc host
  2266. *
  2267. * Init struct context_info needed to implement asynchronous
  2268. * request mechanism, used by mmc core, host driver and mmc requests
  2269. * supplier.
  2270. */
  2271. void mmc_init_context_info(struct mmc_host *host)
  2272. {
  2273. spin_lock_init(&host->context_info.lock);
  2274. host->context_info.is_new_req = false;
  2275. host->context_info.is_done_rcv = false;
  2276. host->context_info.is_waiting_last_req = false;
  2277. init_waitqueue_head(&host->context_info.wait);
  2278. }
  2279. static int __init mmc_init(void)
  2280. {
  2281. int ret;
  2282. workqueue = alloc_ordered_workqueue("kmmcd", 0);
  2283. if (!workqueue)
  2284. return -ENOMEM;
  2285. ret = mmc_register_bus();
  2286. if (ret)
  2287. goto destroy_workqueue;
  2288. ret = mmc_register_host_class();
  2289. if (ret)
  2290. goto unregister_bus;
  2291. ret = sdio_register_bus();
  2292. if (ret)
  2293. goto unregister_host_class;
  2294. return 0;
  2295. unregister_host_class:
  2296. mmc_unregister_host_class();
  2297. unregister_bus:
  2298. mmc_unregister_bus();
  2299. destroy_workqueue:
  2300. destroy_workqueue(workqueue);
  2301. return ret;
  2302. }
  2303. static void __exit mmc_exit(void)
  2304. {
  2305. sdio_unregister_bus();
  2306. mmc_unregister_host_class();
  2307. mmc_unregister_bus();
  2308. destroy_workqueue(workqueue);
  2309. }
  2310. subsys_initcall(mmc_init);
  2311. module_exit(mmc_exit);
  2312. MODULE_LICENSE("GPL");