core.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  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-2007 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 <asm/scatterlist.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/mmc/card.h>
  24. #include <linux/mmc/host.h>
  25. #include <linux/mmc/mmc.h>
  26. #include <linux/mmc/sd.h>
  27. #include "core.h"
  28. #include "sysfs.h"
  29. #include "mmc_ops.h"
  30. #include "sd_ops.h"
  31. #define CMD_RETRIES 3
  32. /*
  33. * OCR Bit positions to 10s of Vdd mV.
  34. */
  35. static const unsigned short mmc_ocr_bit_to_vdd[] = {
  36. 150, 155, 160, 165, 170, 180, 190, 200,
  37. 210, 220, 230, 240, 250, 260, 270, 280,
  38. 290, 300, 310, 320, 330, 340, 350, 360
  39. };
  40. static const unsigned int tran_exp[] = {
  41. 10000, 100000, 1000000, 10000000,
  42. 0, 0, 0, 0
  43. };
  44. static const unsigned char tran_mant[] = {
  45. 0, 10, 12, 13, 15, 20, 25, 30,
  46. 35, 40, 45, 50, 55, 60, 70, 80,
  47. };
  48. static const unsigned int tacc_exp[] = {
  49. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  50. };
  51. static const unsigned int tacc_mant[] = {
  52. 0, 10, 12, 13, 15, 20, 25, 30,
  53. 35, 40, 45, 50, 55, 60, 70, 80,
  54. };
  55. /**
  56. * mmc_request_done - finish processing an MMC request
  57. * @host: MMC host which completed request
  58. * @mrq: MMC request which request
  59. *
  60. * MMC drivers should call this function when they have completed
  61. * their processing of a request.
  62. */
  63. void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
  64. {
  65. struct mmc_command *cmd = mrq->cmd;
  66. int err = cmd->error;
  67. pr_debug("%s: req done (CMD%u): %d/%d/%d: %08x %08x %08x %08x\n",
  68. mmc_hostname(host), cmd->opcode, err,
  69. mrq->data ? mrq->data->error : 0,
  70. mrq->stop ? mrq->stop->error : 0,
  71. cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
  72. if (err && cmd->retries) {
  73. cmd->retries--;
  74. cmd->error = 0;
  75. host->ops->request(host, mrq);
  76. } else if (mrq->done) {
  77. mrq->done(mrq);
  78. }
  79. }
  80. EXPORT_SYMBOL(mmc_request_done);
  81. /**
  82. * mmc_start_request - start a command on a host
  83. * @host: MMC host to start command on
  84. * @mrq: MMC request to start
  85. *
  86. * Queue a command on the specified host. We expect the
  87. * caller to be holding the host lock with interrupts disabled.
  88. */
  89. void
  90. mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
  91. {
  92. #ifdef CONFIG_MMC_DEBUG
  93. unsigned int i, sz;
  94. #endif
  95. pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
  96. mmc_hostname(host), mrq->cmd->opcode,
  97. mrq->cmd->arg, mrq->cmd->flags);
  98. WARN_ON(!host->claimed);
  99. mrq->cmd->error = 0;
  100. mrq->cmd->mrq = mrq;
  101. if (mrq->data) {
  102. BUG_ON(mrq->data->blksz > host->max_blk_size);
  103. BUG_ON(mrq->data->blocks > host->max_blk_count);
  104. BUG_ON(mrq->data->blocks * mrq->data->blksz >
  105. host->max_req_size);
  106. #ifdef CONFIG_MMC_DEBUG
  107. sz = 0;
  108. for (i = 0;i < mrq->data->sg_len;i++)
  109. sz += mrq->data->sg[i].length;
  110. BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
  111. #endif
  112. mrq->cmd->data = mrq->data;
  113. mrq->data->error = 0;
  114. mrq->data->mrq = mrq;
  115. if (mrq->stop) {
  116. mrq->data->stop = mrq->stop;
  117. mrq->stop->error = 0;
  118. mrq->stop->mrq = mrq;
  119. }
  120. }
  121. host->ops->request(host, mrq);
  122. }
  123. EXPORT_SYMBOL(mmc_start_request);
  124. static void mmc_wait_done(struct mmc_request *mrq)
  125. {
  126. complete(mrq->done_data);
  127. }
  128. int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
  129. {
  130. DECLARE_COMPLETION_ONSTACK(complete);
  131. mrq->done_data = &complete;
  132. mrq->done = mmc_wait_done;
  133. mmc_start_request(host, mrq);
  134. wait_for_completion(&complete);
  135. return 0;
  136. }
  137. EXPORT_SYMBOL(mmc_wait_for_req);
  138. /**
  139. * mmc_wait_for_cmd - start a command and wait for completion
  140. * @host: MMC host to start command
  141. * @cmd: MMC command to start
  142. * @retries: maximum number of retries
  143. *
  144. * Start a new MMC command for a host, and wait for the command
  145. * to complete. Return any error that occurred while the command
  146. * was executing. Do not attempt to parse the response.
  147. */
  148. int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
  149. {
  150. struct mmc_request mrq;
  151. BUG_ON(!host->claimed);
  152. memset(&mrq, 0, sizeof(struct mmc_request));
  153. memset(cmd->resp, 0, sizeof(cmd->resp));
  154. cmd->retries = retries;
  155. mrq.cmd = cmd;
  156. cmd->data = NULL;
  157. mmc_wait_for_req(host, &mrq);
  158. return cmd->error;
  159. }
  160. EXPORT_SYMBOL(mmc_wait_for_cmd);
  161. /**
  162. * mmc_set_data_timeout - set the timeout for a data command
  163. * @data: data phase for command
  164. * @card: the MMC card associated with the data transfer
  165. * @write: flag to differentiate reads from writes
  166. */
  167. void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
  168. int write)
  169. {
  170. unsigned int mult;
  171. /*
  172. * SD cards use a 100 multiplier rather than 10
  173. */
  174. mult = mmc_card_sd(card) ? 100 : 10;
  175. /*
  176. * Scale up the multiplier (and therefore the timeout) by
  177. * the r2w factor for writes.
  178. */
  179. if (write)
  180. mult <<= card->csd.r2w_factor;
  181. data->timeout_ns = card->csd.tacc_ns * mult;
  182. data->timeout_clks = card->csd.tacc_clks * mult;
  183. /*
  184. * SD cards also have an upper limit on the timeout.
  185. */
  186. if (mmc_card_sd(card)) {
  187. unsigned int timeout_us, limit_us;
  188. timeout_us = data->timeout_ns / 1000;
  189. timeout_us += data->timeout_clks * 1000 /
  190. (card->host->ios.clock / 1000);
  191. if (write)
  192. limit_us = 250000;
  193. else
  194. limit_us = 100000;
  195. /*
  196. * SDHC cards always use these fixed values.
  197. */
  198. if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
  199. data->timeout_ns = limit_us * 1000;
  200. data->timeout_clks = 0;
  201. }
  202. }
  203. }
  204. EXPORT_SYMBOL(mmc_set_data_timeout);
  205. /**
  206. * __mmc_claim_host - exclusively claim a host
  207. * @host: mmc host to claim
  208. * @card: mmc card to claim host for
  209. *
  210. * Claim a host for a set of operations. If a valid card
  211. * is passed and this wasn't the last card selected, select
  212. * the card before returning.
  213. *
  214. * Note: you should use mmc_card_claim_host or mmc_claim_host.
  215. */
  216. void mmc_claim_host(struct mmc_host *host)
  217. {
  218. DECLARE_WAITQUEUE(wait, current);
  219. unsigned long flags;
  220. add_wait_queue(&host->wq, &wait);
  221. spin_lock_irqsave(&host->lock, flags);
  222. while (1) {
  223. set_current_state(TASK_UNINTERRUPTIBLE);
  224. if (!host->claimed)
  225. break;
  226. spin_unlock_irqrestore(&host->lock, flags);
  227. schedule();
  228. spin_lock_irqsave(&host->lock, flags);
  229. }
  230. set_current_state(TASK_RUNNING);
  231. host->claimed = 1;
  232. spin_unlock_irqrestore(&host->lock, flags);
  233. remove_wait_queue(&host->wq, &wait);
  234. }
  235. EXPORT_SYMBOL(mmc_claim_host);
  236. /**
  237. * mmc_release_host - release a host
  238. * @host: mmc host to release
  239. *
  240. * Release a MMC host, allowing others to claim the host
  241. * for their operations.
  242. */
  243. void mmc_release_host(struct mmc_host *host)
  244. {
  245. unsigned long flags;
  246. BUG_ON(!host->claimed);
  247. spin_lock_irqsave(&host->lock, flags);
  248. host->claimed = 0;
  249. spin_unlock_irqrestore(&host->lock, flags);
  250. wake_up(&host->wq);
  251. }
  252. EXPORT_SYMBOL(mmc_release_host);
  253. static inline void mmc_set_ios(struct mmc_host *host)
  254. {
  255. struct mmc_ios *ios = &host->ios;
  256. pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
  257. "width %u timing %u\n",
  258. mmc_hostname(host), ios->clock, ios->bus_mode,
  259. ios->power_mode, ios->chip_select, ios->vdd,
  260. ios->bus_width, ios->timing);
  261. host->ops->set_ios(host, ios);
  262. }
  263. void mmc_set_chip_select(struct mmc_host *host, int mode)
  264. {
  265. host->ios.chip_select = mode;
  266. mmc_set_ios(host);
  267. }
  268. /*
  269. * Mask off any voltages we don't support and select
  270. * the lowest voltage
  271. */
  272. static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
  273. {
  274. int bit;
  275. ocr &= host->ocr_avail;
  276. bit = ffs(ocr);
  277. if (bit) {
  278. bit -= 1;
  279. ocr &= 3 << bit;
  280. host->ios.vdd = bit;
  281. mmc_set_ios(host);
  282. } else {
  283. ocr = 0;
  284. }
  285. return ocr;
  286. }
  287. #define UNSTUFF_BITS(resp,start,size) \
  288. ({ \
  289. const int __size = size; \
  290. const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
  291. const int __off = 3 - ((start) / 32); \
  292. const int __shft = (start) & 31; \
  293. u32 __res; \
  294. \
  295. __res = resp[__off] >> __shft; \
  296. if (__size + __shft > 32) \
  297. __res |= resp[__off-1] << ((32 - __shft) % 32); \
  298. __res & __mask; \
  299. })
  300. /*
  301. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  302. */
  303. static void mmc_decode_cid(struct mmc_card *card)
  304. {
  305. u32 *resp = card->raw_cid;
  306. memset(&card->cid, 0, sizeof(struct mmc_cid));
  307. if (mmc_card_sd(card)) {
  308. /*
  309. * SD doesn't currently have a version field so we will
  310. * have to assume we can parse this.
  311. */
  312. card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
  313. card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
  314. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  315. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  316. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  317. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  318. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  319. card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
  320. card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
  321. card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
  322. card->cid.year = UNSTUFF_BITS(resp, 12, 8);
  323. card->cid.month = UNSTUFF_BITS(resp, 8, 4);
  324. card->cid.year += 2000; /* SD cards year offset */
  325. } else {
  326. /*
  327. * The selection of the format here is based upon published
  328. * specs from sandisk and from what people have reported.
  329. */
  330. switch (card->csd.mmca_vsn) {
  331. case 0: /* MMC v1.0 - v1.2 */
  332. case 1: /* MMC v1.4 */
  333. card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
  334. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  335. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  336. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  337. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  338. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  339. card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  340. card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
  341. card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
  342. card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
  343. card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
  344. card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  345. card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
  346. break;
  347. case 2: /* MMC v2.0 - v2.2 */
  348. case 3: /* MMC v3.1 - v3.3 */
  349. case 4: /* MMC v4 */
  350. card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
  351. card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
  352. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  353. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  354. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  355. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  356. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  357. card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  358. card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
  359. card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  360. card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
  361. break;
  362. default:
  363. printk("%s: card has unknown MMCA version %d\n",
  364. mmc_hostname(card->host), card->csd.mmca_vsn);
  365. mmc_card_set_bad(card);
  366. break;
  367. }
  368. }
  369. }
  370. /*
  371. * Given a 128-bit response, decode to our card CSD structure.
  372. */
  373. static void mmc_decode_csd(struct mmc_card *card)
  374. {
  375. struct mmc_csd *csd = &card->csd;
  376. unsigned int e, m, csd_struct;
  377. u32 *resp = card->raw_csd;
  378. if (mmc_card_sd(card)) {
  379. csd_struct = UNSTUFF_BITS(resp, 126, 2);
  380. switch (csd_struct) {
  381. case 0:
  382. m = UNSTUFF_BITS(resp, 115, 4);
  383. e = UNSTUFF_BITS(resp, 112, 3);
  384. csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
  385. csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
  386. m = UNSTUFF_BITS(resp, 99, 4);
  387. e = UNSTUFF_BITS(resp, 96, 3);
  388. csd->max_dtr = tran_exp[e] * tran_mant[m];
  389. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  390. e = UNSTUFF_BITS(resp, 47, 3);
  391. m = UNSTUFF_BITS(resp, 62, 12);
  392. csd->capacity = (1 + m) << (e + 2);
  393. csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
  394. csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
  395. csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
  396. csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
  397. csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
  398. csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
  399. csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
  400. break;
  401. case 1:
  402. /*
  403. * This is a block-addressed SDHC card. Most
  404. * interesting fields are unused and have fixed
  405. * values. To avoid getting tripped by buggy cards,
  406. * we assume those fixed values ourselves.
  407. */
  408. mmc_card_set_blockaddr(card);
  409. csd->tacc_ns = 0; /* Unused */
  410. csd->tacc_clks = 0; /* Unused */
  411. m = UNSTUFF_BITS(resp, 99, 4);
  412. e = UNSTUFF_BITS(resp, 96, 3);
  413. csd->max_dtr = tran_exp[e] * tran_mant[m];
  414. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  415. m = UNSTUFF_BITS(resp, 48, 22);
  416. csd->capacity = (1 + m) << 10;
  417. csd->read_blkbits = 9;
  418. csd->read_partial = 0;
  419. csd->write_misalign = 0;
  420. csd->read_misalign = 0;
  421. csd->r2w_factor = 4; /* Unused */
  422. csd->write_blkbits = 9;
  423. csd->write_partial = 0;
  424. break;
  425. default:
  426. printk("%s: unrecognised CSD structure version %d\n",
  427. mmc_hostname(card->host), csd_struct);
  428. mmc_card_set_bad(card);
  429. return;
  430. }
  431. } else {
  432. /*
  433. * We only understand CSD structure v1.1 and v1.2.
  434. * v1.2 has extra information in bits 15, 11 and 10.
  435. */
  436. csd_struct = UNSTUFF_BITS(resp, 126, 2);
  437. if (csd_struct != 1 && csd_struct != 2) {
  438. printk("%s: unrecognised CSD structure version %d\n",
  439. mmc_hostname(card->host), csd_struct);
  440. mmc_card_set_bad(card);
  441. return;
  442. }
  443. csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
  444. m = UNSTUFF_BITS(resp, 115, 4);
  445. e = UNSTUFF_BITS(resp, 112, 3);
  446. csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
  447. csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
  448. m = UNSTUFF_BITS(resp, 99, 4);
  449. e = UNSTUFF_BITS(resp, 96, 3);
  450. csd->max_dtr = tran_exp[e] * tran_mant[m];
  451. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  452. e = UNSTUFF_BITS(resp, 47, 3);
  453. m = UNSTUFF_BITS(resp, 62, 12);
  454. csd->capacity = (1 + m) << (e + 2);
  455. csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
  456. csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
  457. csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
  458. csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
  459. csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
  460. csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
  461. csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
  462. }
  463. }
  464. /*
  465. * Given a 64-bit response, decode to our card SCR structure.
  466. */
  467. static void mmc_decode_scr(struct mmc_card *card)
  468. {
  469. struct sd_scr *scr = &card->scr;
  470. unsigned int scr_struct;
  471. u32 resp[4];
  472. BUG_ON(!mmc_card_sd(card));
  473. resp[3] = card->raw_scr[1];
  474. resp[2] = card->raw_scr[0];
  475. scr_struct = UNSTUFF_BITS(resp, 60, 4);
  476. if (scr_struct != 0) {
  477. printk("%s: unrecognised SCR structure version %d\n",
  478. mmc_hostname(card->host), scr_struct);
  479. mmc_card_set_bad(card);
  480. return;
  481. }
  482. scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
  483. scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
  484. }
  485. /*
  486. * Allocate a new MMC card
  487. */
  488. static struct mmc_card *
  489. mmc_alloc_card(struct mmc_host *host, u32 *raw_cid)
  490. {
  491. struct mmc_card *card;
  492. card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
  493. if (!card)
  494. return ERR_PTR(-ENOMEM);
  495. mmc_init_card(card, host);
  496. memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
  497. return card;
  498. }
  499. /*
  500. * Apply power to the MMC stack. This is a two-stage process.
  501. * First, we enable power to the card without the clock running.
  502. * We then wait a bit for the power to stabilise. Finally,
  503. * enable the bus drivers and clock to the card.
  504. *
  505. * We must _NOT_ enable the clock prior to power stablising.
  506. *
  507. * If a host does all the power sequencing itself, ignore the
  508. * initial MMC_POWER_UP stage.
  509. */
  510. static void mmc_power_up(struct mmc_host *host)
  511. {
  512. int bit = fls(host->ocr_avail) - 1;
  513. host->ios.vdd = bit;
  514. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  515. host->ios.chip_select = MMC_CS_DONTCARE;
  516. host->ios.power_mode = MMC_POWER_UP;
  517. host->ios.bus_width = MMC_BUS_WIDTH_1;
  518. host->ios.timing = MMC_TIMING_LEGACY;
  519. mmc_set_ios(host);
  520. mmc_delay(1);
  521. host->ios.clock = host->f_min;
  522. host->ios.power_mode = MMC_POWER_ON;
  523. mmc_set_ios(host);
  524. mmc_delay(2);
  525. }
  526. static void mmc_power_off(struct mmc_host *host)
  527. {
  528. host->ios.clock = 0;
  529. host->ios.vdd = 0;
  530. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  531. host->ios.chip_select = MMC_CS_DONTCARE;
  532. host->ios.power_mode = MMC_POWER_OFF;
  533. host->ios.bus_width = MMC_BUS_WIDTH_1;
  534. host->ios.timing = MMC_TIMING_LEGACY;
  535. mmc_set_ios(host);
  536. }
  537. /*
  538. * Discover the card by requesting its CID.
  539. *
  540. * Create a mmc_card entry for the discovered card, assigning
  541. * it an RCA, and save the raw CID for decoding later.
  542. */
  543. static void mmc_discover_card(struct mmc_host *host)
  544. {
  545. unsigned int err;
  546. u32 cid[4];
  547. BUG_ON(host->card);
  548. err = mmc_all_send_cid(host, cid);
  549. if (err != MMC_ERR_NONE) {
  550. printk(KERN_ERR "%s: error requesting CID: %d\n",
  551. mmc_hostname(host), err);
  552. return;
  553. }
  554. host->card = mmc_alloc_card(host, cid);
  555. if (IS_ERR(host->card)) {
  556. err = PTR_ERR(host->card);
  557. host->card = NULL;
  558. return;
  559. }
  560. if (host->mode == MMC_MODE_SD) {
  561. host->card->type = MMC_TYPE_SD;
  562. err = mmc_send_relative_addr(host, &host->card->rca);
  563. if (err != MMC_ERR_NONE)
  564. mmc_card_set_dead(host->card);
  565. else {
  566. if (!host->ops->get_ro) {
  567. printk(KERN_WARNING "%s: host does not "
  568. "support reading read-only "
  569. "switch. assuming write-enable.\n",
  570. mmc_hostname(host));
  571. } else {
  572. if (host->ops->get_ro(host))
  573. mmc_card_set_readonly(host->card);
  574. }
  575. }
  576. } else {
  577. host->card->type = MMC_TYPE_MMC;
  578. host->card->rca = 1;
  579. err = mmc_set_relative_addr(host->card);
  580. if (err != MMC_ERR_NONE)
  581. mmc_card_set_dead(host->card);
  582. }
  583. }
  584. static void mmc_read_csd(struct mmc_host *host)
  585. {
  586. int err;
  587. if (!host->card)
  588. return;
  589. if (mmc_card_dead(host->card))
  590. return;
  591. err = mmc_send_csd(host->card, host->card->raw_csd);
  592. if (err != MMC_ERR_NONE) {
  593. mmc_card_set_dead(host->card);
  594. return;
  595. }
  596. mmc_decode_csd(host->card);
  597. mmc_decode_cid(host->card);
  598. }
  599. static void mmc_process_ext_csd(struct mmc_host *host)
  600. {
  601. int err;
  602. u8 *ext_csd;
  603. if (!host->card)
  604. return;
  605. if (mmc_card_dead(host->card))
  606. return;
  607. if (mmc_card_sd(host->card))
  608. return;
  609. if (host->card->csd.mmca_vsn < CSD_SPEC_VER_4)
  610. return;
  611. /*
  612. * As the ext_csd is so large and mostly unused, we don't store the
  613. * raw block in mmc_card.
  614. */
  615. ext_csd = kmalloc(512, GFP_KERNEL);
  616. if (!ext_csd) {
  617. printk("%s: could not allocate a buffer to receive the ext_csd."
  618. "mmc v4 cards will be treated as v3.\n",
  619. mmc_hostname(host));
  620. return;
  621. }
  622. err = mmc_send_ext_csd(host->card, ext_csd);
  623. if (err != MMC_ERR_NONE) {
  624. if (host->card->csd.capacity == (4096 * 512)) {
  625. printk(KERN_ERR "%s: unable to read EXT_CSD "
  626. "on a possible high capacity card. "
  627. "Card will be ignored.\n",
  628. mmc_hostname(host));
  629. mmc_card_set_dead(host->card);
  630. } else {
  631. printk(KERN_WARNING "%s: unable to read "
  632. "EXT_CSD, performance might "
  633. "suffer.\n",
  634. mmc_hostname(host));
  635. }
  636. goto out;
  637. }
  638. host->card->ext_csd.sectors =
  639. ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
  640. ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
  641. ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
  642. ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
  643. if (host->card->ext_csd.sectors)
  644. mmc_card_set_blockaddr(host->card);
  645. switch (ext_csd[EXT_CSD_CARD_TYPE]) {
  646. case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
  647. host->card->ext_csd.hs_max_dtr = 52000000;
  648. break;
  649. case EXT_CSD_CARD_TYPE_26:
  650. host->card->ext_csd.hs_max_dtr = 26000000;
  651. break;
  652. default:
  653. /* MMC v4 spec says this cannot happen */
  654. printk("%s: card is mmc v4 but doesn't support "
  655. "any high-speed modes.\n",
  656. mmc_hostname(host));
  657. goto out;
  658. }
  659. if (host->caps & MMC_CAP_MMC_HIGHSPEED) {
  660. /* Activate highspeed support. */
  661. err = mmc_switch(host->card, MMC_SWITCH_MODE_WRITE_BYTE,
  662. EXT_CSD_HS_TIMING, 1);
  663. if (err != MMC_ERR_NONE) {
  664. printk("%s: failed to switch card to mmc v4 "
  665. "high-speed mode.\n",
  666. mmc_hostname(host));
  667. goto out;
  668. }
  669. mmc_card_set_highspeed(host->card);
  670. host->ios.timing = MMC_TIMING_MMC_HS;
  671. mmc_set_ios(host);
  672. }
  673. /* Check for host support for wide-bus modes. */
  674. if (host->caps & MMC_CAP_4_BIT_DATA) {
  675. /* Activate 4-bit support. */
  676. err = mmc_switch(host->card, MMC_SWITCH_MODE_WRITE_BYTE,
  677. EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_4 |
  678. EXT_CSD_CMD_SET_NORMAL);
  679. if (err != MMC_ERR_NONE) {
  680. printk("%s: failed to switch card to "
  681. "mmc v4 4-bit bus mode.\n",
  682. mmc_hostname(host));
  683. goto out;
  684. }
  685. host->ios.bus_width = MMC_BUS_WIDTH_4;
  686. mmc_set_ios(host);
  687. }
  688. out:
  689. kfree(ext_csd);
  690. }
  691. static void mmc_read_scr(struct mmc_host *host)
  692. {
  693. int err;
  694. if (!host->card)
  695. return;
  696. if (mmc_card_dead(host->card))
  697. return;
  698. if (!mmc_card_sd(host->card))
  699. return;
  700. err = mmc_app_send_scr(host->card, host->card->raw_scr);
  701. if (err != MMC_ERR_NONE) {
  702. mmc_card_set_dead(host->card);
  703. return;
  704. }
  705. mmc_decode_scr(host->card);
  706. }
  707. static void mmc_read_switch_caps(struct mmc_host *host)
  708. {
  709. int err;
  710. unsigned char *status;
  711. if (!(host->caps & MMC_CAP_SD_HIGHSPEED))
  712. return;
  713. if (!host->card)
  714. return;
  715. if (mmc_card_dead(host->card))
  716. return;
  717. if (!mmc_card_sd(host->card))
  718. return;
  719. if (host->card->scr.sda_vsn < SCR_SPEC_VER_1)
  720. return;
  721. status = kmalloc(64, GFP_KERNEL);
  722. if (!status) {
  723. printk(KERN_WARNING "%s: Unable to allocate buffer for "
  724. "reading switch capabilities.\n",
  725. mmc_hostname(host));
  726. return;
  727. }
  728. err = mmc_sd_switch(host->card, SD_SWITCH_CHECK,
  729. SD_SWITCH_GRP_ACCESS, SD_SWITCH_ACCESS_HS, status);
  730. if (err != MMC_ERR_NONE) {
  731. printk("%s: unable to read switch capabilities, "
  732. "performance might suffer.\n",
  733. mmc_hostname(host));
  734. goto out;
  735. }
  736. if (status[13] & 0x02)
  737. host->card->sw_caps.hs_max_dtr = 50000000;
  738. err = mmc_sd_switch(host->card, SD_SWITCH_SET,
  739. SD_SWITCH_GRP_ACCESS, SD_SWITCH_ACCESS_HS, status);
  740. if (err != MMC_ERR_NONE || (status[16] & 0xF) != 1) {
  741. printk(KERN_WARNING "%s: Problem switching card "
  742. "into high-speed mode!\n",
  743. mmc_hostname(host));
  744. goto out;
  745. }
  746. mmc_card_set_highspeed(host->card);
  747. host->ios.timing = MMC_TIMING_SD_HS;
  748. mmc_set_ios(host);
  749. out:
  750. kfree(status);
  751. }
  752. static unsigned int mmc_calculate_clock(struct mmc_host *host)
  753. {
  754. unsigned int max_dtr = host->f_max;
  755. if (host->card && !mmc_card_dead(host->card)) {
  756. if (mmc_card_highspeed(host->card) && mmc_card_sd(host->card)) {
  757. if (max_dtr > host->card->sw_caps.hs_max_dtr)
  758. max_dtr = host->card->sw_caps.hs_max_dtr;
  759. } else if (mmc_card_highspeed(host->card) && !mmc_card_sd(host->card)) {
  760. if (max_dtr > host->card->ext_csd.hs_max_dtr)
  761. max_dtr = host->card->ext_csd.hs_max_dtr;
  762. } else if (max_dtr > host->card->csd.max_dtr) {
  763. max_dtr = host->card->csd.max_dtr;
  764. }
  765. }
  766. pr_debug("%s: selected %d.%03dMHz transfer rate\n",
  767. mmc_hostname(host),
  768. max_dtr / 1000000, (max_dtr / 1000) % 1000);
  769. return max_dtr;
  770. }
  771. /*
  772. * Check whether cards we already know about are still present.
  773. * We do this by requesting status, and checking whether a card
  774. * responds.
  775. *
  776. * A request for status does not cause a state change in data
  777. * transfer mode.
  778. */
  779. static void mmc_check_card(struct mmc_card *card)
  780. {
  781. int err;
  782. BUG_ON(!card);
  783. err = mmc_send_status(card, NULL);
  784. if (err == MMC_ERR_NONE)
  785. return;
  786. mmc_card_set_dead(card);
  787. }
  788. static void mmc_setup(struct mmc_host *host)
  789. {
  790. int err;
  791. u32 ocr;
  792. host->mode = MMC_MODE_SD;
  793. mmc_power_up(host);
  794. mmc_go_idle(host);
  795. err = mmc_send_if_cond(host, host->ocr_avail);
  796. if (err != MMC_ERR_NONE) {
  797. return;
  798. }
  799. err = mmc_send_app_op_cond(host, 0, &ocr);
  800. /*
  801. * If we fail to detect any SD cards then try
  802. * searching for MMC cards.
  803. */
  804. if (err != MMC_ERR_NONE) {
  805. host->mode = MMC_MODE_MMC;
  806. err = mmc_send_op_cond(host, 0, &ocr);
  807. if (err != MMC_ERR_NONE)
  808. return;
  809. }
  810. host->ocr = mmc_select_voltage(host, ocr);
  811. if (host->ocr == 0)
  812. return;
  813. /*
  814. * Since we're changing the OCR value, we seem to
  815. * need to tell some cards to go back to the idle
  816. * state. We wait 1ms to give cards time to
  817. * respond.
  818. */
  819. mmc_go_idle(host);
  820. /*
  821. * Send the selected OCR multiple times... until the cards
  822. * all get the idea that they should be ready for CMD2.
  823. * (My SanDisk card seems to need this.)
  824. */
  825. if (host->mode == MMC_MODE_SD) {
  826. /*
  827. * If SD_SEND_IF_COND indicates an SD 2.0
  828. * compliant card and we should set bit 30
  829. * of the ocr to indicate that we can handle
  830. * block-addressed SDHC cards.
  831. */
  832. err = mmc_send_if_cond(host, host->ocr);
  833. if (err == MMC_ERR_NONE)
  834. ocr = host->ocr | (1 << 30);
  835. mmc_send_app_op_cond(host, ocr, NULL);
  836. } else {
  837. /* The extra bit indicates that we support high capacity */
  838. mmc_send_op_cond(host, host->ocr | (1 << 30), NULL);
  839. }
  840. mmc_discover_card(host);
  841. /*
  842. * Ok, now switch to push-pull mode.
  843. */
  844. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  845. mmc_set_ios(host);
  846. mmc_read_csd(host);
  847. if (host->card && !mmc_card_dead(host->card)) {
  848. err = mmc_select_card(host->card);
  849. if (err != MMC_ERR_NONE)
  850. mmc_card_set_dead(host->card);
  851. }
  852. /*
  853. * The card is in 1 bit mode by default so
  854. * we only need to change if it supports the
  855. * wider version.
  856. */
  857. if (host->card && !mmc_card_dead(host->card) &&
  858. mmc_card_sd(host->card) &&
  859. (host->card->scr.bus_widths & SD_SCR_BUS_WIDTH_4) &&
  860. (host->card->host->caps & MMC_CAP_4_BIT_DATA)) {
  861. err = mmc_app_set_bus_width(host->card, SD_BUS_WIDTH_4);
  862. if (err != MMC_ERR_NONE)
  863. mmc_card_set_dead(host->card);
  864. else {
  865. host->ios.bus_width = MMC_BUS_WIDTH_4;
  866. mmc_set_ios(host);
  867. }
  868. }
  869. if (host->mode == MMC_MODE_SD) {
  870. mmc_read_scr(host);
  871. mmc_read_switch_caps(host);
  872. } else
  873. mmc_process_ext_csd(host);
  874. }
  875. /**
  876. * mmc_detect_change - process change of state on a MMC socket
  877. * @host: host which changed state.
  878. * @delay: optional delay to wait before detection (jiffies)
  879. *
  880. * All we know is that card(s) have been inserted or removed
  881. * from the socket(s). We don't know which socket or cards.
  882. */
  883. void mmc_detect_change(struct mmc_host *host, unsigned long delay)
  884. {
  885. #ifdef CONFIG_MMC_DEBUG
  886. mmc_claim_host(host);
  887. BUG_ON(host->removed);
  888. mmc_release_host(host);
  889. #endif
  890. mmc_schedule_delayed_work(&host->detect, delay);
  891. }
  892. EXPORT_SYMBOL(mmc_detect_change);
  893. static void mmc_rescan(struct work_struct *work)
  894. {
  895. struct mmc_host *host =
  896. container_of(work, struct mmc_host, detect.work);
  897. mmc_claim_host(host);
  898. /*
  899. * Check for removed card and newly inserted ones. We check for
  900. * removed cards first so we can intelligently re-select the VDD.
  901. */
  902. if (host->card) {
  903. mmc_check_card(host->card);
  904. mmc_release_host(host);
  905. if (mmc_card_dead(host->card)) {
  906. mmc_remove_card(host->card);
  907. host->card = NULL;
  908. }
  909. goto out;
  910. }
  911. mmc_setup(host);
  912. if (host->card && !mmc_card_dead(host->card)) {
  913. /*
  914. * (Re-)calculate the fastest clock rate which the
  915. * attached cards and the host support.
  916. */
  917. host->ios.clock = mmc_calculate_clock(host);
  918. mmc_set_ios(host);
  919. }
  920. mmc_release_host(host);
  921. /*
  922. * If this is a new and good card, register it.
  923. */
  924. if (host->card && !mmc_card_dead(host->card)) {
  925. if (mmc_register_card(host->card))
  926. mmc_card_set_dead(host->card);
  927. }
  928. /*
  929. * If this card is dead, destroy it.
  930. */
  931. if (host->card && mmc_card_dead(host->card)) {
  932. mmc_remove_card(host->card);
  933. host->card = NULL;
  934. }
  935. out:
  936. /*
  937. * If we discover that there are no cards on the
  938. * bus, turn off the clock and power down.
  939. */
  940. if (!host->card)
  941. mmc_power_off(host);
  942. }
  943. /**
  944. * mmc_alloc_host - initialise the per-host structure.
  945. * @extra: sizeof private data structure
  946. * @dev: pointer to host device model structure
  947. *
  948. * Initialise the per-host structure.
  949. */
  950. struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
  951. {
  952. struct mmc_host *host;
  953. host = mmc_alloc_host_sysfs(extra, dev);
  954. if (host) {
  955. spin_lock_init(&host->lock);
  956. init_waitqueue_head(&host->wq);
  957. INIT_DELAYED_WORK(&host->detect, mmc_rescan);
  958. /*
  959. * By default, hosts do not support SGIO or large requests.
  960. * They have to set these according to their abilities.
  961. */
  962. host->max_hw_segs = 1;
  963. host->max_phys_segs = 1;
  964. host->max_seg_size = PAGE_CACHE_SIZE;
  965. host->max_req_size = PAGE_CACHE_SIZE;
  966. host->max_blk_size = 512;
  967. host->max_blk_count = PAGE_CACHE_SIZE / 512;
  968. }
  969. return host;
  970. }
  971. EXPORT_SYMBOL(mmc_alloc_host);
  972. /**
  973. * mmc_add_host - initialise host hardware
  974. * @host: mmc host
  975. */
  976. int mmc_add_host(struct mmc_host *host)
  977. {
  978. int ret;
  979. ret = mmc_add_host_sysfs(host);
  980. if (ret == 0) {
  981. mmc_power_off(host);
  982. mmc_detect_change(host, 0);
  983. }
  984. return ret;
  985. }
  986. EXPORT_SYMBOL(mmc_add_host);
  987. /**
  988. * mmc_remove_host - remove host hardware
  989. * @host: mmc host
  990. *
  991. * Unregister and remove all cards associated with this host,
  992. * and power down the MMC bus.
  993. */
  994. void mmc_remove_host(struct mmc_host *host)
  995. {
  996. #ifdef CONFIG_MMC_DEBUG
  997. mmc_claim_host(host);
  998. host->removed = 1;
  999. mmc_release_host(host);
  1000. #endif
  1001. mmc_flush_scheduled_work();
  1002. if (host->card) {
  1003. mmc_remove_card(host->card);
  1004. host->card = NULL;
  1005. }
  1006. mmc_power_off(host);
  1007. mmc_remove_host_sysfs(host);
  1008. }
  1009. EXPORT_SYMBOL(mmc_remove_host);
  1010. /**
  1011. * mmc_free_host - free the host structure
  1012. * @host: mmc host
  1013. *
  1014. * Free the host once all references to it have been dropped.
  1015. */
  1016. void mmc_free_host(struct mmc_host *host)
  1017. {
  1018. mmc_free_host_sysfs(host);
  1019. }
  1020. EXPORT_SYMBOL(mmc_free_host);
  1021. #ifdef CONFIG_PM
  1022. /**
  1023. * mmc_suspend_host - suspend a host
  1024. * @host: mmc host
  1025. * @state: suspend mode (PM_SUSPEND_xxx)
  1026. */
  1027. int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
  1028. {
  1029. mmc_flush_scheduled_work();
  1030. if (host->card) {
  1031. mmc_remove_card(host->card);
  1032. host->card = NULL;
  1033. }
  1034. mmc_power_off(host);
  1035. return 0;
  1036. }
  1037. EXPORT_SYMBOL(mmc_suspend_host);
  1038. /**
  1039. * mmc_resume_host - resume a previously suspended host
  1040. * @host: mmc host
  1041. */
  1042. int mmc_resume_host(struct mmc_host *host)
  1043. {
  1044. mmc_rescan(&host->detect.work);
  1045. return 0;
  1046. }
  1047. EXPORT_SYMBOL(mmc_resume_host);
  1048. #endif
  1049. MODULE_LICENSE("GPL");