core.c 61 KB

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