mmc.c 30 KB

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