mmc.c 29 KB

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