core.c 54 KB

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