mmc.c 30 KB

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