core.c 66 KB

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