mmc.c 28 KB

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