core.c 61 KB

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